blob: 9aac1c4033c7bf00be82adffce18d17e324ce14c [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
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100464static int drbd_recv_short(struct socket *sock, void *buf, size_t size, int flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700465{
466 mm_segment_t oldfs;
467 struct kvec iov = {
468 .iov_base = buf,
469 .iov_len = size,
470 };
471 struct msghdr msg = {
472 .msg_iovlen = 1,
473 .msg_iov = (struct iovec *)&iov,
474 .msg_flags = (flags ? flags : MSG_WAITALL | MSG_NOSIGNAL)
475 };
476 int rv;
477
478 oldfs = get_fs();
479 set_fs(KERNEL_DS);
480 rv = sock_recvmsg(sock, &msg, size, msg.msg_flags);
481 set_fs(oldfs);
482
483 return rv;
484}
485
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100486static int drbd_recv(struct drbd_tconn *tconn, void *buf, size_t size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700487{
488 mm_segment_t oldfs;
489 struct kvec iov = {
490 .iov_base = buf,
491 .iov_len = size,
492 };
493 struct msghdr msg = {
494 .msg_iovlen = 1,
495 .msg_iov = (struct iovec *)&iov,
496 .msg_flags = MSG_WAITALL | MSG_NOSIGNAL
497 };
498 int rv;
499
500 oldfs = get_fs();
501 set_fs(KERNEL_DS);
502
503 for (;;) {
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100504 rv = sock_recvmsg(tconn->data.socket, &msg, size, msg.msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700505 if (rv == size)
506 break;
507
508 /* Note:
509 * ECONNRESET other side closed the connection
510 * ERESTARTSYS (on sock) we got a signal
511 */
512
513 if (rv < 0) {
514 if (rv == -ECONNRESET)
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100515 conn_info(tconn, "sock was reset by peer\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700516 else if (rv != -ERESTARTSYS)
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100517 conn_err(tconn, "sock_recvmsg returned %d\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700518 break;
519 } else if (rv == 0) {
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100520 conn_info(tconn, "sock was shut down by peer\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700521 break;
522 } else {
523 /* signal came in, or peer/link went down,
524 * after we read a partial message
525 */
526 /* D_ASSERT(signal_pending(current)); */
527 break;
528 }
529 };
530
531 set_fs(oldfs);
532
533 if (rv != size)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100534 conn_request_state(tconn, NS(conn, C_BROKEN_PIPE), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700535
536 return rv;
537}
538
Andreas Gruenbacherc6967742011-03-17 17:15:20 +0100539static int drbd_recv_all(struct drbd_tconn *tconn, void *buf, size_t size)
540{
541 int err;
542
543 err = drbd_recv(tconn, buf, size);
544 if (err != size) {
545 if (err >= 0)
546 err = -EIO;
547 } else
548 err = 0;
549 return err;
550}
551
Andreas Gruenbachera5c31902011-03-24 03:28:04 +0100552static int drbd_recv_all_warn(struct drbd_tconn *tconn, void *buf, size_t size)
553{
554 int err;
555
556 err = drbd_recv_all(tconn, buf, size);
557 if (err && !signal_pending(current))
558 conn_warn(tconn, "short read (expected size %d)\n", (int)size);
559 return err;
560}
561
Lars Ellenberg5dbf1672010-05-25 16:18:01 +0200562/* quoting tcp(7):
563 * On individual connections, the socket buffer size must be set prior to the
564 * listen(2) or connect(2) calls in order to have it take effect.
565 * This is our wrapper to do so.
566 */
567static void drbd_setbufsize(struct socket *sock, unsigned int snd,
568 unsigned int rcv)
569{
570 /* open coded SO_SNDBUF, SO_RCVBUF */
571 if (snd) {
572 sock->sk->sk_sndbuf = snd;
573 sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
574 }
575 if (rcv) {
576 sock->sk->sk_rcvbuf = rcv;
577 sock->sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
578 }
579}
580
Philipp Reisnereac3e992011-02-07 14:05:07 +0100581static struct socket *drbd_try_connect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700582{
583 const char *what;
584 struct socket *sock;
585 struct sockaddr_in6 src_in6;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200586 struct sockaddr_in6 peer_in6;
587 struct net_conf *nc;
588 int err, peer_addr_len, my_addr_len;
Andreas Gruenbacher69ef82d2011-05-11 14:34:35 +0200589 int sndbuf_size, rcvbuf_size, connect_int;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700590 int disconnect_on_error = 1;
591
Philipp Reisner44ed1672011-04-19 17:10:19 +0200592 rcu_read_lock();
593 nc = rcu_dereference(tconn->net_conf);
594 if (!nc) {
595 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700596 return NULL;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200597 }
Philipp Reisner44ed1672011-04-19 17:10:19 +0200598 sndbuf_size = nc->sndbuf_size;
599 rcvbuf_size = nc->rcvbuf_size;
Andreas Gruenbacher69ef82d2011-05-11 14:34:35 +0200600 connect_int = nc->connect_int;
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200601 rcu_read_unlock();
Philipp Reisner44ed1672011-04-19 17:10:19 +0200602
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200603 my_addr_len = min_t(int, tconn->my_addr_len, sizeof(src_in6));
604 memcpy(&src_in6, &tconn->my_addr, my_addr_len);
Philipp Reisner44ed1672011-04-19 17:10:19 +0200605
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200606 if (((struct sockaddr *)&tconn->my_addr)->sa_family == AF_INET6)
Philipp Reisner44ed1672011-04-19 17:10:19 +0200607 src_in6.sin6_port = 0;
608 else
609 ((struct sockaddr_in *)&src_in6)->sin_port = 0; /* AF_INET & AF_SCI */
610
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200611 peer_addr_len = min_t(int, tconn->peer_addr_len, sizeof(src_in6));
612 memcpy(&peer_in6, &tconn->peer_addr, peer_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700613
614 what = "sock_create_kern";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200615 err = sock_create_kern(((struct sockaddr *)&src_in6)->sa_family,
616 SOCK_STREAM, IPPROTO_TCP, &sock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700617 if (err < 0) {
618 sock = NULL;
619 goto out;
620 }
621
622 sock->sk->sk_rcvtimeo =
Andreas Gruenbacher69ef82d2011-05-11 14:34:35 +0200623 sock->sk->sk_sndtimeo = connect_int * HZ;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200624 drbd_setbufsize(sock, sndbuf_size, rcvbuf_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700625
626 /* explicitly bind to the configured IP as source IP
627 * for the outgoing connections.
628 * This is needed for multihomed hosts and to be
629 * able to use lo: interfaces for drbd.
630 * Make sure to use 0 as port number, so linux selects
631 * a free one dynamically.
632 */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700633 what = "bind before connect";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200634 err = sock->ops->bind(sock, (struct sockaddr *) &src_in6, my_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700635 if (err < 0)
636 goto out;
637
638 /* connect may fail, peer not yet available.
639 * stay C_WF_CONNECTION, don't go Disconnecting! */
640 disconnect_on_error = 0;
641 what = "connect";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200642 err = sock->ops->connect(sock, (struct sockaddr *) &peer_in6, peer_addr_len, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700643
644out:
645 if (err < 0) {
646 if (sock) {
647 sock_release(sock);
648 sock = NULL;
649 }
650 switch (-err) {
651 /* timeout, busy, signal pending */
652 case ETIMEDOUT: case EAGAIN: case EINPROGRESS:
653 case EINTR: case ERESTARTSYS:
654 /* peer not (yet) available, network problem */
655 case ECONNREFUSED: case ENETUNREACH:
656 case EHOSTDOWN: case EHOSTUNREACH:
657 disconnect_on_error = 0;
658 break;
659 default:
Philipp Reisnereac3e992011-02-07 14:05:07 +0100660 conn_err(tconn, "%s failed, err = %d\n", what, err);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700661 }
662 if (disconnect_on_error)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100663 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700664 }
Philipp Reisner44ed1672011-04-19 17:10:19 +0200665
Philipp Reisnerb411b362009-09-25 16:07:19 -0700666 return sock;
667}
668
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200669struct accept_wait_data {
670 struct drbd_tconn *tconn;
671 struct socket *s_listen;
672 struct completion door_bell;
673 void (*original_sk_state_change)(struct sock *sk);
674
675};
676
677static void incomming_connection(struct sock *sk)
678{
679 struct accept_wait_data *ad = sk->sk_user_data;
680 struct drbd_tconn *tconn = ad->tconn;
681
682 if (sk->sk_state != TCP_ESTABLISHED)
683 conn_warn(tconn, "unexpected tcp state change. sk_state = %d\n", sk->sk_state);
684
685 write_lock_bh(&sk->sk_callback_lock);
686 sk->sk_state_change = ad->original_sk_state_change;
687 sk->sk_user_data = NULL;
688 write_unlock_bh(&sk->sk_callback_lock);
689
690 sk->sk_state_change(sk);
691 complete(&ad->door_bell);
692}
693
694static int prepare_listen_socket(struct drbd_tconn *tconn, struct accept_wait_data *ad)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700695{
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200696 int err, sndbuf_size, rcvbuf_size, my_addr_len;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200697 struct sockaddr_in6 my_addr;
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200698 struct socket *s_listen;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200699 struct net_conf *nc;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700700 const char *what;
701
Philipp Reisner44ed1672011-04-19 17:10:19 +0200702 rcu_read_lock();
703 nc = rcu_dereference(tconn->net_conf);
704 if (!nc) {
705 rcu_read_unlock();
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200706 return -EIO;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200707 }
Philipp Reisner44ed1672011-04-19 17:10:19 +0200708 sndbuf_size = nc->sndbuf_size;
709 rcvbuf_size = nc->rcvbuf_size;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200710 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700711
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200712 my_addr_len = min_t(int, tconn->my_addr_len, sizeof(struct sockaddr_in6));
713 memcpy(&my_addr, &tconn->my_addr, my_addr_len);
714
Philipp Reisnerb411b362009-09-25 16:07:19 -0700715 what = "sock_create_kern";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200716 err = sock_create_kern(((struct sockaddr *)&my_addr)->sa_family,
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200717 SOCK_STREAM, IPPROTO_TCP, &s_listen);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700718 if (err) {
719 s_listen = NULL;
720 goto out;
721 }
722
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200723 s_listen->sk->sk_reuse = 1; /* SO_REUSEADDR */
Philipp Reisner44ed1672011-04-19 17:10:19 +0200724 drbd_setbufsize(s_listen, sndbuf_size, rcvbuf_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700725
726 what = "bind before listen";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200727 err = s_listen->ops->bind(s_listen, (struct sockaddr *)&my_addr, my_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700728 if (err < 0)
729 goto out;
730
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200731 ad->s_listen = s_listen;
732 write_lock_bh(&s_listen->sk->sk_callback_lock);
733 ad->original_sk_state_change = s_listen->sk->sk_state_change;
734 s_listen->sk->sk_state_change = incomming_connection;
735 s_listen->sk->sk_user_data = ad;
736 write_unlock_bh(&s_listen->sk->sk_callback_lock);
737
Philipp Reisner2820fd32012-07-12 10:22:48 +0200738 what = "listen";
739 err = s_listen->ops->listen(s_listen, 5);
740 if (err < 0)
741 goto out;
742
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200743 return 0;
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200744out:
745 if (s_listen)
746 sock_release(s_listen);
747 if (err < 0) {
748 if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) {
749 conn_err(tconn, "%s failed, err = %d\n", what, err);
750 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
751 }
752 }
753
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200754 return -EIO;
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200755}
756
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200757static struct socket *drbd_wait_for_connect(struct drbd_tconn *tconn, struct accept_wait_data *ad)
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200758{
759 int timeo, connect_int, err = 0;
760 struct socket *s_estab = NULL;
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200761 struct net_conf *nc;
762
763 rcu_read_lock();
764 nc = rcu_dereference(tconn->net_conf);
765 if (!nc) {
766 rcu_read_unlock();
767 return NULL;
768 }
769 connect_int = nc->connect_int;
770 rcu_read_unlock();
771
772 timeo = connect_int * HZ;
773 timeo += (random32() & 1) ? timeo / 7 : -timeo / 7; /* 28.5% random jitter */
774
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200775 err = wait_for_completion_interruptible_timeout(&ad->door_bell, timeo);
776 if (err <= 0)
777 return NULL;
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200778
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200779 err = kernel_accept(ad->s_listen, &s_estab, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700780 if (err < 0) {
781 if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) {
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200782 conn_err(tconn, "accept failed, err = %d\n", err);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100783 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700784 }
785 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700786
787 return s_estab;
788}
789
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200790static int decode_header(struct drbd_tconn *, void *, struct packet_info *);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700791
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200792static int send_first_packet(struct drbd_tconn *tconn, struct drbd_socket *sock,
793 enum drbd_packet cmd)
794{
795 if (!conn_prepare_command(tconn, sock))
796 return -EIO;
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200797 return conn_send_command(tconn, sock, cmd, 0, NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700798}
799
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200800static int receive_first_packet(struct drbd_tconn *tconn, struct socket *sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700801{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200802 unsigned int header_size = drbd_header_size(tconn);
803 struct packet_info pi;
804 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700805
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200806 err = drbd_recv_short(sock, tconn->data.rbuf, header_size, 0);
807 if (err != header_size) {
808 if (err >= 0)
809 err = -EIO;
810 return err;
811 }
812 err = decode_header(tconn, tconn->data.rbuf, &pi);
813 if (err)
814 return err;
815 return pi.cmd;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700816}
817
818/**
819 * drbd_socket_okay() - Free the socket if its connection is not okay
Philipp Reisnerb411b362009-09-25 16:07:19 -0700820 * @sock: pointer to the pointer to the socket.
821 */
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100822static int drbd_socket_okay(struct socket **sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700823{
824 int rr;
825 char tb[4];
826
827 if (!*sock)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100828 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700829
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100830 rr = drbd_recv_short(*sock, tb, 4, MSG_DONTWAIT | MSG_PEEK);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700831
832 if (rr > 0 || rr == -EAGAIN) {
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100833 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700834 } else {
835 sock_release(*sock);
836 *sock = NULL;
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100837 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700838 }
839}
Philipp Reisner2325eb62011-03-15 16:56:18 +0100840/* Gets called if a connection is established, or if a new minor gets created
841 in a connection */
Philipp Reisnerc141ebd2011-05-05 16:13:10 +0200842int drbd_connected(struct drbd_conf *mdev)
Philipp Reisner907599e2011-02-08 11:25:37 +0100843{
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100844 int err;
Philipp Reisner907599e2011-02-08 11:25:37 +0100845
846 atomic_set(&mdev->packet_seq, 0);
847 mdev->peer_seq = 0;
848
Philipp Reisner8410da82011-02-11 20:11:10 +0100849 mdev->state_mutex = mdev->tconn->agreed_pro_version < 100 ?
850 &mdev->tconn->cstate_mutex :
851 &mdev->own_state_mutex;
852
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100853 err = drbd_send_sync_param(mdev);
854 if (!err)
855 err = drbd_send_sizes(mdev, 0, 0);
856 if (!err)
857 err = drbd_send_uuids(mdev);
858 if (!err)
Philipp Reisner43de7c82011-11-10 13:16:13 +0100859 err = drbd_send_current_state(mdev);
Philipp Reisner907599e2011-02-08 11:25:37 +0100860 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
861 clear_bit(RESIZE_PENDING, &mdev->flags);
Philipp Reisner8b924f12011-03-01 11:08:28 +0100862 mod_timer(&mdev->request_timer, jiffies + HZ); /* just start it here. */
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100863 return err;
Philipp Reisner907599e2011-02-08 11:25:37 +0100864}
865
Philipp Reisnerb411b362009-09-25 16:07:19 -0700866/*
867 * return values:
868 * 1 yes, we have a valid connection
869 * 0 oops, did not work out, please try again
870 * -1 peer talks different language,
871 * no point in trying again, please go standalone.
872 * -2 We do not have a network config...
873 */
Philipp Reisner81fa2e62011-05-04 15:10:30 +0200874static int conn_connect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700875{
Philipp Reisner7da35862011-12-19 22:42:56 +0100876 struct drbd_socket sock, msock;
Philipp Reisnerc141ebd2011-05-05 16:13:10 +0200877 struct drbd_conf *mdev;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200878 struct net_conf *nc;
Philipp Reisnerc141ebd2011-05-05 16:13:10 +0200879 int vnr, timeout, try, h, ok;
Philipp Reisner08b165b2011-09-05 16:22:33 +0200880 bool discard_my_data;
Philipp Reisnera1096a62012-04-06 12:07:34 +0200881 enum drbd_state_rv rv;
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200882 struct accept_wait_data ad = {
883 .tconn = tconn,
884 .door_bell = COMPLETION_INITIALIZER_ONSTACK(ad.door_bell),
885 };
Philipp Reisnerb411b362009-09-25 16:07:19 -0700886
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100887 if (conn_request_state(tconn, NS(conn, C_WF_CONNECTION), CS_VERBOSE) < SS_SUCCESS)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700888 return -2;
889
Philipp Reisner7da35862011-12-19 22:42:56 +0100890 mutex_init(&sock.mutex);
891 sock.sbuf = tconn->data.sbuf;
892 sock.rbuf = tconn->data.rbuf;
893 sock.socket = NULL;
894 mutex_init(&msock.mutex);
895 msock.sbuf = tconn->meta.sbuf;
896 msock.rbuf = tconn->meta.rbuf;
897 msock.socket = NULL;
898
Philipp Reisner907599e2011-02-08 11:25:37 +0100899 clear_bit(DISCARD_CONCURRENT, &tconn->flags);
Andreas Gruenbacher0916e0e2011-03-21 14:10:15 +0100900
901 /* Assume that the peer only understands protocol 80 until we know better. */
902 tconn->agreed_pro_version = 80;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700903
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200904 if (prepare_listen_socket(tconn, &ad))
905 return 0;
906
Philipp Reisnerb411b362009-09-25 16:07:19 -0700907 do {
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200908 struct socket *s;
909
Philipp Reisnerb411b362009-09-25 16:07:19 -0700910 for (try = 0;;) {
911 /* 3 tries, this should take less than a second! */
Philipp Reisner907599e2011-02-08 11:25:37 +0100912 s = drbd_try_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700913 if (s || ++try >= 3)
914 break;
915 /* give the other side time to call bind() & listen() */
Philipp Reisner20ee6392011-01-18 15:28:59 +0100916 schedule_timeout_interruptible(HZ / 10);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700917 }
918
919 if (s) {
Philipp Reisner7da35862011-12-19 22:42:56 +0100920 if (!sock.socket) {
921 sock.socket = s;
922 send_first_packet(tconn, &sock, P_INITIAL_DATA);
923 } else if (!msock.socket) {
924 msock.socket = s;
925 send_first_packet(tconn, &msock, P_INITIAL_META);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700926 } else {
Philipp Reisner81fa2e62011-05-04 15:10:30 +0200927 conn_err(tconn, "Logic error in conn_connect()\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700928 goto out_release_sockets;
929 }
930 }
931
Philipp Reisner7da35862011-12-19 22:42:56 +0100932 if (sock.socket && msock.socket) {
933 rcu_read_lock();
934 nc = rcu_dereference(tconn->net_conf);
935 timeout = nc->ping_timeo * HZ / 10;
936 rcu_read_unlock();
937 schedule_timeout_interruptible(timeout);
938 ok = drbd_socket_okay(&sock.socket);
939 ok = drbd_socket_okay(&msock.socket) && ok;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700940 if (ok)
941 break;
942 }
943
944retry:
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200945 s = drbd_wait_for_connect(tconn, &ad);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700946 if (s) {
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200947 try = receive_first_packet(tconn, s);
Philipp Reisner7da35862011-12-19 22:42:56 +0100948 drbd_socket_okay(&sock.socket);
949 drbd_socket_okay(&msock.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700950 switch (try) {
Andreas Gruenbachere5d6f332011-03-28 16:44:40 +0200951 case P_INITIAL_DATA:
Philipp Reisner7da35862011-12-19 22:42:56 +0100952 if (sock.socket) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100953 conn_warn(tconn, "initial packet S crossed\n");
Philipp Reisner7da35862011-12-19 22:42:56 +0100954 sock_release(sock.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700955 }
Philipp Reisner7da35862011-12-19 22:42:56 +0100956 sock.socket = s;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700957 break;
Andreas Gruenbachere5d6f332011-03-28 16:44:40 +0200958 case P_INITIAL_META:
Philipp Reisner7da35862011-12-19 22:42:56 +0100959 if (msock.socket) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100960 conn_warn(tconn, "initial packet M crossed\n");
Philipp Reisner7da35862011-12-19 22:42:56 +0100961 sock_release(msock.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700962 }
Philipp Reisner7da35862011-12-19 22:42:56 +0100963 msock.socket = s;
Philipp Reisner907599e2011-02-08 11:25:37 +0100964 set_bit(DISCARD_CONCURRENT, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700965 break;
966 default:
Philipp Reisner907599e2011-02-08 11:25:37 +0100967 conn_warn(tconn, "Error receiving initial packet\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700968 sock_release(s);
969 if (random32() & 1)
970 goto retry;
971 }
972 }
973
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100974 if (tconn->cstate <= C_DISCONNECTING)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700975 goto out_release_sockets;
976 if (signal_pending(current)) {
977 flush_signals(current);
978 smp_rmb();
Philipp Reisner907599e2011-02-08 11:25:37 +0100979 if (get_t_state(&tconn->receiver) == EXITING)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700980 goto out_release_sockets;
981 }
982
Philipp Reisner7da35862011-12-19 22:42:56 +0100983 if (sock.socket && &msock.socket) {
984 ok = drbd_socket_okay(&sock.socket);
985 ok = drbd_socket_okay(&msock.socket) && ok;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700986 if (ok)
987 break;
988 }
989 } while (1);
990
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200991 if (ad.s_listen)
992 sock_release(ad.s_listen);
993
Philipp Reisner7da35862011-12-19 22:42:56 +0100994 sock.socket->sk->sk_reuse = 1; /* SO_REUSEADDR */
995 msock.socket->sk->sk_reuse = 1; /* SO_REUSEADDR */
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200996
Philipp Reisner7da35862011-12-19 22:42:56 +0100997 sock.socket->sk->sk_allocation = GFP_NOIO;
998 msock.socket->sk->sk_allocation = GFP_NOIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700999
Philipp Reisner7da35862011-12-19 22:42:56 +01001000 sock.socket->sk->sk_priority = TC_PRIO_INTERACTIVE_BULK;
1001 msock.socket->sk->sk_priority = TC_PRIO_INTERACTIVE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001002
Philipp Reisnerb411b362009-09-25 16:07:19 -07001003 /* NOT YET ...
Philipp Reisner7da35862011-12-19 22:42:56 +01001004 * sock.socket->sk->sk_sndtimeo = tconn->net_conf->timeout*HZ/10;
1005 * sock.socket->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
Andreas Gruenbacher60381782011-03-28 17:05:50 +02001006 * first set it to the P_CONNECTION_FEATURES timeout,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001007 * which we set to 4x the configured ping_timeout. */
Philipp Reisner44ed1672011-04-19 17:10:19 +02001008 rcu_read_lock();
1009 nc = rcu_dereference(tconn->net_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001010
Philipp Reisner7da35862011-12-19 22:42:56 +01001011 sock.socket->sk->sk_sndtimeo =
1012 sock.socket->sk->sk_rcvtimeo = nc->ping_timeo*4*HZ/10;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001013
Philipp Reisner7da35862011-12-19 22:42:56 +01001014 msock.socket->sk->sk_rcvtimeo = nc->ping_int*HZ;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001015 timeout = nc->timeout * HZ / 10;
Philipp Reisner08b165b2011-09-05 16:22:33 +02001016 discard_my_data = nc->discard_my_data;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001017 rcu_read_unlock();
1018
Philipp Reisner7da35862011-12-19 22:42:56 +01001019 msock.socket->sk->sk_sndtimeo = timeout;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001020
1021 /* we don't want delays.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001022 * we use TCP_CORK where appropriate, though */
Philipp Reisner7da35862011-12-19 22:42:56 +01001023 drbd_tcp_nodelay(sock.socket);
1024 drbd_tcp_nodelay(msock.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001025
Philipp Reisner7da35862011-12-19 22:42:56 +01001026 tconn->data.socket = sock.socket;
1027 tconn->meta.socket = msock.socket;
Philipp Reisner907599e2011-02-08 11:25:37 +01001028 tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001029
Andreas Gruenbacher60381782011-03-28 17:05:50 +02001030 h = drbd_do_features(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001031 if (h <= 0)
1032 return h;
1033
Philipp Reisner907599e2011-02-08 11:25:37 +01001034 if (tconn->cram_hmac_tfm) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001035 /* drbd_request_state(mdev, NS(conn, WFAuth)); */
Philipp Reisner907599e2011-02-08 11:25:37 +01001036 switch (drbd_do_auth(tconn)) {
Johannes Thomab10d96c2010-01-07 16:02:50 +01001037 case -1:
Philipp Reisner907599e2011-02-08 11:25:37 +01001038 conn_err(tconn, "Authentication of peer failed\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001039 return -1;
Johannes Thomab10d96c2010-01-07 16:02:50 +01001040 case 0:
Philipp Reisner907599e2011-02-08 11:25:37 +01001041 conn_err(tconn, "Authentication of peer failed, trying again.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01001042 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001043 }
1044 }
1045
Philipp Reisner7da35862011-12-19 22:42:56 +01001046 tconn->data.socket->sk->sk_sndtimeo = timeout;
1047 tconn->data.socket->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001048
Andreas Gruenbacher387eb302011-03-16 01:05:37 +01001049 if (drbd_send_protocol(tconn) == -EOPNOTSUPP)
Philipp Reisner7e2455c2010-04-22 14:50:23 +02001050 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001051
Philipp Reisnera1096a62012-04-06 12:07:34 +02001052 set_bit(STATE_SENT, &tconn->flags);
1053
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02001054 rcu_read_lock();
1055 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1056 kref_get(&mdev->kref);
1057 rcu_read_unlock();
Philipp Reisner08b165b2011-09-05 16:22:33 +02001058
1059 if (discard_my_data)
1060 set_bit(DISCARD_MY_DATA, &mdev->flags);
1061 else
1062 clear_bit(DISCARD_MY_DATA, &mdev->flags);
1063
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02001064 drbd_connected(mdev);
1065 kref_put(&mdev->kref, &drbd_minor_destroy);
1066 rcu_read_lock();
1067 }
1068 rcu_read_unlock();
1069
Philipp Reisnera1096a62012-04-06 12:07:34 +02001070 rv = conn_request_state(tconn, NS(conn, C_WF_REPORT_PARAMS), CS_VERBOSE);
1071 if (rv < SS_SUCCESS) {
1072 clear_bit(STATE_SENT, &tconn->flags);
Philipp Reisner823bd832012-11-08 15:04:36 +01001073 return 0;
Philipp Reisnera1096a62012-04-06 12:07:34 +02001074 }
Philipp Reisner823bd832012-11-08 15:04:36 +01001075
1076 drbd_thread_start(&tconn->asender);
1077
Philipp Reisner08b165b2011-09-05 16:22:33 +02001078 mutex_lock(&tconn->conf_update);
1079 /* The discard_my_data flag is a single-shot modifier to the next
1080 * connection attempt, the handshake of which is now well underway.
1081 * No need for rcu style copying of the whole struct
1082 * just to clear a single value. */
1083 tconn->net_conf->discard_my_data = 0;
1084 mutex_unlock(&tconn->conf_update);
1085
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07001086 return h;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001087
1088out_release_sockets:
Philipp Reisner7a426fd2012-07-12 14:22:37 +02001089 if (ad.s_listen)
1090 sock_release(ad.s_listen);
Philipp Reisner7da35862011-12-19 22:42:56 +01001091 if (sock.socket)
1092 sock_release(sock.socket);
1093 if (msock.socket)
1094 sock_release(msock.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001095 return -1;
1096}
1097
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001098static int decode_header(struct drbd_tconn *tconn, void *header, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001099{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001100 unsigned int header_size = drbd_header_size(tconn);
1101
Andreas Gruenbacher0c8e36d2011-03-30 16:00:17 +02001102 if (header_size == sizeof(struct p_header100) &&
1103 *(__be32 *)header == cpu_to_be32(DRBD_MAGIC_100)) {
1104 struct p_header100 *h = header;
1105 if (h->pad != 0) {
1106 conn_err(tconn, "Header padding is not zero\n");
1107 return -EINVAL;
1108 }
1109 pi->vnr = be16_to_cpu(h->volume);
1110 pi->cmd = be16_to_cpu(h->command);
1111 pi->size = be32_to_cpu(h->length);
1112 } else if (header_size == sizeof(struct p_header95) &&
1113 *(__be16 *)header == cpu_to_be16(DRBD_MAGIC_BIG)) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001114 struct p_header95 *h = header;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001115 pi->cmd = be16_to_cpu(h->command);
Andreas Gruenbacherb55d84b2011-03-22 13:17:47 +01001116 pi->size = be32_to_cpu(h->length);
1117 pi->vnr = 0;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001118 } else if (header_size == sizeof(struct p_header80) &&
1119 *(__be32 *)header == cpu_to_be32(DRBD_MAGIC)) {
1120 struct p_header80 *h = header;
1121 pi->cmd = be16_to_cpu(h->command);
1122 pi->size = be16_to_cpu(h->length);
Philipp Reisner77351055b2011-02-07 17:24:26 +01001123 pi->vnr = 0;
Philipp Reisner02918be2010-08-20 14:35:10 +02001124 } else {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001125 conn_err(tconn, "Wrong magic value 0x%08x in protocol version %d\n",
1126 be32_to_cpu(*(__be32 *)header),
1127 tconn->agreed_pro_version);
Andreas Gruenbacher8172f3e2011-03-16 17:22:39 +01001128 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001129 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001130 pi->data = header + header_size;
Andreas Gruenbacher8172f3e2011-03-16 17:22:39 +01001131 return 0;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001132}
1133
Philipp Reisner9ba7aa02011-02-07 17:32:41 +01001134static int drbd_recv_header(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner257d0af2011-01-26 12:15:29 +01001135{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001136 void *buffer = tconn->data.rbuf;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001137 int err;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001138
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001139 err = drbd_recv_all_warn(tconn, buffer, drbd_header_size(tconn));
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001140 if (err)
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001141 return err;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001142
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001143 err = decode_header(tconn, buffer, pi);
Philipp Reisner9ba7aa02011-02-07 17:32:41 +01001144 tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001145
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001146 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001147}
1148
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001149static void drbd_flush(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001150{
1151 int rv;
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001152 struct drbd_conf *mdev;
1153 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001154
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001155 if (tconn->write_ordering >= WO_bdev_flush) {
Lars Ellenberg615e0872011-11-17 14:32:12 +01001156 rcu_read_lock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001157 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Lars Ellenberg615e0872011-11-17 14:32:12 +01001158 if (!get_ldev(mdev))
1159 continue;
1160 kref_get(&mdev->kref);
1161 rcu_read_unlock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001162
Lars Ellenberg615e0872011-11-17 14:32:12 +01001163 rv = blkdev_issue_flush(mdev->ldev->backing_bdev,
1164 GFP_NOIO, NULL);
1165 if (rv) {
1166 dev_info(DEV, "local disk flush failed with status %d\n", rv);
1167 /* would rather check on EOPNOTSUPP, but that is not reliable.
1168 * don't try again for ANY return value != 0
1169 * if (rv == -EOPNOTSUPP) */
1170 drbd_bump_write_ordering(tconn, WO_drain_io);
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001171 }
Lars Ellenberg615e0872011-11-17 14:32:12 +01001172 put_ldev(mdev);
1173 kref_put(&mdev->kref, &drbd_minor_destroy);
1174
1175 rcu_read_lock();
1176 if (rv)
1177 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001178 }
Lars Ellenberg615e0872011-11-17 14:32:12 +01001179 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07001180 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001181}
1182
1183/**
1184 * drbd_may_finish_epoch() - Applies an epoch_event to the epoch's state, eventually finishes it.
1185 * @mdev: DRBD device.
1186 * @epoch: Epoch object.
1187 * @ev: Epoch event.
1188 */
Philipp Reisner1e9dd292011-11-10 15:14:53 +01001189static enum finish_epoch drbd_may_finish_epoch(struct drbd_tconn *tconn,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001190 struct drbd_epoch *epoch,
1191 enum epoch_event ev)
1192{
Philipp Reisner2451fc32010-08-24 13:43:11 +02001193 int epoch_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001194 struct drbd_epoch *next_epoch;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001195 enum finish_epoch rv = FE_STILL_LIVE;
1196
Philipp Reisner12038a32011-11-09 19:18:00 +01001197 spin_lock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001198 do {
1199 next_epoch = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001200
1201 epoch_size = atomic_read(&epoch->epoch_size);
1202
1203 switch (ev & ~EV_CLEANUP) {
1204 case EV_PUT:
1205 atomic_dec(&epoch->active);
1206 break;
1207 case EV_GOT_BARRIER_NR:
1208 set_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001209 break;
1210 case EV_BECAME_LAST:
1211 /* nothing to do*/
1212 break;
1213 }
1214
Philipp Reisnerb411b362009-09-25 16:07:19 -07001215 if (epoch_size != 0 &&
1216 atomic_read(&epoch->active) == 0 &&
Philipp Reisner85d735132011-07-18 15:45:15 +02001217 (test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags) || ev & EV_CLEANUP)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001218 if (!(ev & EV_CLEANUP)) {
Philipp Reisner12038a32011-11-09 19:18:00 +01001219 spin_unlock(&tconn->epoch_lock);
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001220 drbd_send_b_ack(epoch->tconn, epoch->barrier_nr, epoch_size);
Philipp Reisner12038a32011-11-09 19:18:00 +01001221 spin_lock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001222 }
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001223#if 0
1224 /* FIXME: dec unacked on connection, once we have
1225 * something to count pending connection packets in. */
Philipp Reisner85d735132011-07-18 15:45:15 +02001226 if (test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags))
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001227 dec_unacked(epoch->tconn);
1228#endif
Philipp Reisnerb411b362009-09-25 16:07:19 -07001229
Philipp Reisner12038a32011-11-09 19:18:00 +01001230 if (tconn->current_epoch != epoch) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001231 next_epoch = list_entry(epoch->list.next, struct drbd_epoch, list);
1232 list_del(&epoch->list);
1233 ev = EV_BECAME_LAST | (ev & EV_CLEANUP);
Philipp Reisner12038a32011-11-09 19:18:00 +01001234 tconn->epochs--;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001235 kfree(epoch);
1236
1237 if (rv == FE_STILL_LIVE)
1238 rv = FE_DESTROYED;
1239 } else {
1240 epoch->flags = 0;
1241 atomic_set(&epoch->epoch_size, 0);
Uwe Kleine-König698f9312010-07-02 20:41:51 +02001242 /* atomic_set(&epoch->active, 0); is already zero */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001243 if (rv == FE_STILL_LIVE)
1244 rv = FE_RECYCLED;
1245 }
1246 }
1247
1248 if (!next_epoch)
1249 break;
1250
1251 epoch = next_epoch;
1252 } while (1);
1253
Philipp Reisner12038a32011-11-09 19:18:00 +01001254 spin_unlock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001255
Philipp Reisnerb411b362009-09-25 16:07:19 -07001256 return rv;
1257}
1258
1259/**
1260 * drbd_bump_write_ordering() - Fall back to an other write ordering method
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001261 * @tconn: DRBD connection.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001262 * @wo: Write ordering method to try.
1263 */
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001264void drbd_bump_write_ordering(struct drbd_tconn *tconn, enum write_ordering_e wo)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001265{
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001266 struct disk_conf *dc;
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001267 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001268 enum write_ordering_e pwo;
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001269 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001270 static char *write_ordering_str[] = {
1271 [WO_none] = "none",
1272 [WO_drain_io] = "drain",
1273 [WO_bdev_flush] = "flush",
Philipp Reisnerb411b362009-09-25 16:07:19 -07001274 };
1275
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001276 pwo = tconn->write_ordering;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001277 wo = min(pwo, wo);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001278 rcu_read_lock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001279 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Philipp Reisner27eb13e2012-03-30 14:12:15 +02001280 if (!get_ldev_if_state(mdev, D_ATTACHING))
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001281 continue;
1282 dc = rcu_dereference(mdev->ldev->disk_conf);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001283
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001284 if (wo == WO_bdev_flush && !dc->disk_flushes)
1285 wo = WO_drain_io;
1286 if (wo == WO_drain_io && !dc->disk_drain)
1287 wo = WO_none;
1288 put_ldev(mdev);
1289 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001290 rcu_read_unlock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001291 tconn->write_ordering = wo;
1292 if (pwo != tconn->write_ordering || wo == WO_bdev_flush)
1293 conn_info(tconn, "Method to ensure write ordering: %s\n", write_ordering_str[tconn->write_ordering]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001294}
1295
1296/**
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001297 * drbd_submit_peer_request()
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001298 * @mdev: DRBD device.
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001299 * @peer_req: peer request
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001300 * @rw: flag field, see bio->bi_rw
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001301 *
1302 * May spread the pages to multiple bios,
1303 * depending on bio_add_page restrictions.
1304 *
1305 * Returns 0 if all bios have been submitted,
1306 * -ENOMEM if we could not allocate enough bios,
1307 * -ENOSPC (any better suggestion?) if we have not been able to bio_add_page a
1308 * single page to an empty bio (which should never happen and likely indicates
1309 * that the lower level IO stack is in some way broken). This has been observed
1310 * on certain Xen deployments.
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001311 */
1312/* TODO allocate from our own bio_set. */
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001313int drbd_submit_peer_request(struct drbd_conf *mdev,
1314 struct drbd_peer_request *peer_req,
1315 const unsigned rw, const int fault_type)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001316{
1317 struct bio *bios = NULL;
1318 struct bio *bio;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001319 struct page *page = peer_req->pages;
1320 sector_t sector = peer_req->i.sector;
1321 unsigned ds = peer_req->i.size;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001322 unsigned n_bios = 0;
1323 unsigned nr_pages = (ds + PAGE_SIZE -1) >> PAGE_SHIFT;
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001324 int err = -ENOMEM;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001325
1326 /* In most cases, we will only need one bio. But in case the lower
1327 * level restrictions happen to be different at this offset on this
1328 * side than those of the sending peer, we may need to submit the
Lars Ellenbergda4a75d2011-02-23 17:02:01 +01001329 * request in more than one bio.
1330 *
1331 * Plain bio_alloc is good enough here, this is no DRBD internally
1332 * generated bio, but a bio allocated on behalf of the peer.
1333 */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001334next_bio:
1335 bio = bio_alloc(GFP_NOIO, nr_pages);
1336 if (!bio) {
1337 dev_err(DEV, "submit_ee: Allocation of a bio failed\n");
1338 goto fail;
1339 }
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001340 /* > peer_req->i.sector, unless this is the first bio */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001341 bio->bi_sector = sector;
1342 bio->bi_bdev = mdev->ldev->backing_bdev;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001343 bio->bi_rw = rw;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001344 bio->bi_private = peer_req;
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01001345 bio->bi_end_io = drbd_peer_request_endio;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001346
1347 bio->bi_next = bios;
1348 bios = bio;
1349 ++n_bios;
1350
1351 page_chain_for_each(page) {
1352 unsigned len = min_t(unsigned, ds, PAGE_SIZE);
1353 if (!bio_add_page(bio, page, len, 0)) {
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001354 /* A single page must always be possible!
1355 * But in case it fails anyways,
1356 * we deal with it, and complain (below). */
1357 if (bio->bi_vcnt == 0) {
1358 dev_err(DEV,
1359 "bio_add_page failed for len=%u, "
1360 "bi_vcnt=0 (bi_sector=%llu)\n",
1361 len, (unsigned long long)bio->bi_sector);
1362 err = -ENOSPC;
1363 goto fail;
1364 }
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001365 goto next_bio;
1366 }
1367 ds -= len;
1368 sector += len >> 9;
1369 --nr_pages;
1370 }
1371 D_ASSERT(page == NULL);
1372 D_ASSERT(ds == 0);
1373
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001374 atomic_set(&peer_req->pending_bios, n_bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001375 do {
1376 bio = bios;
1377 bios = bios->bi_next;
1378 bio->bi_next = NULL;
1379
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001380 drbd_generic_make_request(mdev, fault_type, bio);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001381 } while (bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001382 return 0;
1383
1384fail:
1385 while (bios) {
1386 bio = bios;
1387 bios = bios->bi_next;
1388 bio_put(bio);
1389 }
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001390 return err;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001391}
1392
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001393static void drbd_remove_epoch_entry_interval(struct drbd_conf *mdev,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001394 struct drbd_peer_request *peer_req)
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001395{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001396 struct drbd_interval *i = &peer_req->i;
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001397
1398 drbd_remove_interval(&mdev->write_requests, i);
1399 drbd_clear_interval(i);
1400
Andreas Gruenbacher6c852be2011-02-04 15:38:52 +01001401 /* Wake up any processes waiting for this peer request to complete. */
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001402 if (i->waiting)
1403 wake_up(&mdev->misc_wait);
1404}
1405
Philipp Reisner77fede52011-11-10 21:19:11 +01001406void conn_wait_active_ee_empty(struct drbd_tconn *tconn)
1407{
1408 struct drbd_conf *mdev;
1409 int vnr;
1410
1411 rcu_read_lock();
1412 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1413 kref_get(&mdev->kref);
1414 rcu_read_unlock();
1415 drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
1416 kref_put(&mdev->kref, &drbd_minor_destroy);
1417 rcu_read_lock();
1418 }
1419 rcu_read_unlock();
1420}
1421
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001422static int receive_Barrier(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001423{
Philipp Reisner2451fc32010-08-24 13:43:11 +02001424 int rv;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001425 struct p_barrier *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001426 struct drbd_epoch *epoch;
1427
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001428 /* FIXME these are unacked on connection,
1429 * not a specific (peer)device.
1430 */
Philipp Reisner12038a32011-11-09 19:18:00 +01001431 tconn->current_epoch->barrier_nr = p->barrier;
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001432 tconn->current_epoch->tconn = tconn;
Philipp Reisner1e9dd292011-11-10 15:14:53 +01001433 rv = drbd_may_finish_epoch(tconn, tconn->current_epoch, EV_GOT_BARRIER_NR);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001434
1435 /* P_BARRIER_ACK may imply that the corresponding extent is dropped from
1436 * the activity log, which means it would not be resynced in case the
1437 * R_PRIMARY crashes now.
1438 * Therefore we must send the barrier_ack after the barrier request was
1439 * completed. */
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001440 switch (tconn->write_ordering) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001441 case WO_none:
1442 if (rv == FE_RECYCLED)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001443 return 0;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001444
1445 /* receiver context, in the writeout path of the other node.
1446 * avoid potential distributed deadlock */
1447 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1448 if (epoch)
1449 break;
1450 else
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001451 conn_warn(tconn, "Allocation of an epoch failed, slowing down\n");
Philipp Reisner2451fc32010-08-24 13:43:11 +02001452 /* Fall through */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001453
1454 case WO_bdev_flush:
1455 case WO_drain_io:
Philipp Reisner77fede52011-11-10 21:19:11 +01001456 conn_wait_active_ee_empty(tconn);
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001457 drbd_flush(tconn);
Philipp Reisner2451fc32010-08-24 13:43:11 +02001458
Philipp Reisner12038a32011-11-09 19:18:00 +01001459 if (atomic_read(&tconn->current_epoch->epoch_size)) {
Philipp Reisner2451fc32010-08-24 13:43:11 +02001460 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1461 if (epoch)
1462 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001463 }
1464
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001465 return 0;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001466 default:
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001467 conn_err(tconn, "Strangeness in tconn->write_ordering %d\n", tconn->write_ordering);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001468 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001469 }
1470
1471 epoch->flags = 0;
1472 atomic_set(&epoch->epoch_size, 0);
1473 atomic_set(&epoch->active, 0);
1474
Philipp Reisner12038a32011-11-09 19:18:00 +01001475 spin_lock(&tconn->epoch_lock);
1476 if (atomic_read(&tconn->current_epoch->epoch_size)) {
1477 list_add(&epoch->list, &tconn->current_epoch->list);
1478 tconn->current_epoch = epoch;
1479 tconn->epochs++;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001480 } else {
1481 /* The current_epoch got recycled while we allocated this one... */
1482 kfree(epoch);
1483 }
Philipp Reisner12038a32011-11-09 19:18:00 +01001484 spin_unlock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001485
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001486 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001487}
1488
1489/* used from receive_RSDataReply (recv_resync_read)
1490 * and from receive_Data */
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001491static struct drbd_peer_request *
1492read_in_block(struct drbd_conf *mdev, u64 id, sector_t sector,
1493 int data_size) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001494{
Lars Ellenberg66660322010-04-06 12:15:04 +02001495 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001496 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001497 struct page *page;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001498 int dgs, ds, err;
Philipp Reisnera0638452011-01-19 14:31:32 +01001499 void *dig_in = mdev->tconn->int_dig_in;
1500 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001501 unsigned long *data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001502
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001503 dgs = 0;
1504 if (mdev->tconn->peer_integrity_tfm) {
1505 dgs = crypto_hash_digestsize(mdev->tconn->peer_integrity_tfm);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001506 /*
1507 * FIXME: Receive the incoming digest into the receive buffer
1508 * here, together with its struct p_data?
1509 */
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001510 err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs);
1511 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001512 return NULL;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001513 data_size -= dgs;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001514 }
1515
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001516 if (!expect(data_size != 0))
1517 return NULL;
1518 if (!expect(IS_ALIGNED(data_size, 512)))
1519 return NULL;
1520 if (!expect(data_size <= DRBD_MAX_BIO_SIZE))
1521 return NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001522
Lars Ellenberg66660322010-04-06 12:15:04 +02001523 /* even though we trust out peer,
1524 * we sometimes have to double check. */
1525 if (sector + (data_size>>9) > capacity) {
Lars Ellenbergfdda6542011-01-24 15:11:01 +01001526 dev_err(DEV, "request from peer beyond end of local disk: "
1527 "capacity: %llus < sector: %llus + size: %u\n",
Lars Ellenberg66660322010-04-06 12:15:04 +02001528 (unsigned long long)capacity,
1529 (unsigned long long)sector, data_size);
1530 return NULL;
1531 }
1532
Philipp Reisnerb411b362009-09-25 16:07:19 -07001533 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
1534 * "criss-cross" setup, that might cause write-out on some other DRBD,
1535 * which in turn might block on the other node at this very place. */
Andreas Gruenbacher0db55362011-04-06 16:09:15 +02001536 peer_req = drbd_alloc_peer_req(mdev, id, sector, data_size, GFP_NOIO);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001537 if (!peer_req)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001538 return NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001539
Philipp Reisnerb411b362009-09-25 16:07:19 -07001540 ds = data_size;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001541 page = peer_req->pages;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001542 page_chain_for_each(page) {
1543 unsigned len = min_t(int, ds, PAGE_SIZE);
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001544 data = kmap(page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001545 err = drbd_recv_all_warn(mdev->tconn, data, len);
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +01001546 if (drbd_insert_fault(mdev, DRBD_FAULT_RECEIVE)) {
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001547 dev_err(DEV, "Fault injection: Corrupting data on receive\n");
1548 data[0] = data[0] ^ (unsigned long)-1;
1549 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001550 kunmap(page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001551 if (err) {
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001552 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001553 return NULL;
1554 }
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001555 ds -= len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001556 }
1557
1558 if (dgs) {
Andreas Gruenbacher5b614ab2011-04-27 21:00:12 +02001559 drbd_csum_ee(mdev, mdev->tconn->peer_integrity_tfm, peer_req, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001560 if (memcmp(dig_in, dig_vv, dgs)) {
Lars Ellenberg470be442010-11-10 10:36:52 +01001561 dev_err(DEV, "Digest integrity check FAILED: %llus +%u\n",
1562 (unsigned long long)sector, data_size);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001563 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001564 return NULL;
1565 }
1566 }
1567 mdev->recv_cnt += data_size>>9;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001568 return peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001569}
1570
1571/* drbd_drain_block() just takes a data block
1572 * out of the socket input buffer, and discards it.
1573 */
1574static int drbd_drain_block(struct drbd_conf *mdev, int data_size)
1575{
1576 struct page *page;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001577 int err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001578 void *data;
1579
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001580 if (!data_size)
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001581 return 0;
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001582
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +02001583 page = drbd_alloc_pages(mdev, 1, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001584
1585 data = kmap(page);
1586 while (data_size) {
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001587 unsigned int len = min_t(int, data_size, PAGE_SIZE);
1588
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001589 err = drbd_recv_all_warn(mdev->tconn, data, len);
1590 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001591 break;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001592 data_size -= len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001593 }
1594 kunmap(page);
Andreas Gruenbacher5cc287e2011-04-07 21:02:59 +02001595 drbd_free_pages(mdev, page, 0);
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001596 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001597}
1598
1599static int recv_dless_read(struct drbd_conf *mdev, struct drbd_request *req,
1600 sector_t sector, int data_size)
1601{
1602 struct bio_vec *bvec;
1603 struct bio *bio;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001604 int dgs, err, i, expect;
Philipp Reisnera0638452011-01-19 14:31:32 +01001605 void *dig_in = mdev->tconn->int_dig_in;
1606 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001607
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001608 dgs = 0;
1609 if (mdev->tconn->peer_integrity_tfm) {
1610 dgs = crypto_hash_digestsize(mdev->tconn->peer_integrity_tfm);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001611 err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs);
1612 if (err)
1613 return err;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001614 data_size -= dgs;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001615 }
1616
Philipp Reisnerb411b362009-09-25 16:07:19 -07001617 /* optimistically update recv_cnt. if receiving fails below,
1618 * we disconnect anyways, and counters will be reset. */
1619 mdev->recv_cnt += data_size>>9;
1620
1621 bio = req->master_bio;
1622 D_ASSERT(sector == bio->bi_sector);
1623
1624 bio_for_each_segment(bvec, bio, i) {
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001625 void *mapped = kmap(bvec->bv_page) + bvec->bv_offset;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001626 expect = min_t(int, data_size, bvec->bv_len);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001627 err = drbd_recv_all_warn(mdev->tconn, mapped, expect);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001628 kunmap(bvec->bv_page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001629 if (err)
1630 return err;
1631 data_size -= expect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001632 }
1633
1634 if (dgs) {
Andreas Gruenbacher5b614ab2011-04-27 21:00:12 +02001635 drbd_csum_bio(mdev, mdev->tconn->peer_integrity_tfm, bio, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001636 if (memcmp(dig_in, dig_vv, dgs)) {
1637 dev_err(DEV, "Digest integrity check FAILED. Broken NICs?\n");
Andreas Gruenbacher28284ce2011-03-16 17:54:02 +01001638 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001639 }
1640 }
1641
1642 D_ASSERT(data_size == 0);
Andreas Gruenbacher28284ce2011-03-16 17:54:02 +01001643 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001644}
1645
Andreas Gruenbachera990be42011-04-06 17:56:48 +02001646/*
1647 * e_end_resync_block() is called in asender context via
1648 * drbd_finish_peer_reqs().
1649 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001650static int e_end_resync_block(struct drbd_work *w, int unused)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001651{
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001652 struct drbd_peer_request *peer_req =
1653 container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001654 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001655 sector_t sector = peer_req->i.sector;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001656 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001657
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001658 D_ASSERT(drbd_interval_empty(&peer_req->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001659
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001660 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
1661 drbd_set_in_sync(mdev, sector, peer_req->i.size);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001662 err = drbd_send_ack(mdev, P_RS_WRITE_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001663 } else {
1664 /* Record failure to sync */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001665 drbd_rs_failed_io(mdev, sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001666
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001667 err = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001668 }
1669 dec_unacked(mdev);
1670
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001671 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001672}
1673
1674static int recv_resync_read(struct drbd_conf *mdev, sector_t sector, int data_size) __releases(local)
1675{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001676 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001677
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001678 peer_req = read_in_block(mdev, ID_SYNCER, sector, data_size);
1679 if (!peer_req)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001680 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001681
1682 dec_rs_pending(mdev);
1683
Philipp Reisnerb411b362009-09-25 16:07:19 -07001684 inc_unacked(mdev);
1685 /* corresponding dec_unacked() in e_end_resync_block()
1686 * respective _drbd_clear_done_ee */
1687
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001688 peer_req->w.cb = e_end_resync_block;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001689
Philipp Reisner87eeee42011-01-19 14:16:30 +01001690 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001691 list_add(&peer_req->w.list, &mdev->sync_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001692 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001693
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001694 atomic_add(data_size >> 9, &mdev->rs_sect_ev);
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001695 if (drbd_submit_peer_request(mdev, peer_req, WRITE, DRBD_FAULT_RS_WR) == 0)
Andreas Gruenbachere1c1b0f2011-03-16 17:58:27 +01001696 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001697
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001698 /* don't care for the reason here */
1699 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01001700 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001701 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001702 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02001703
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001704 drbd_free_peer_req(mdev, peer_req);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001705fail:
1706 put_ldev(mdev);
Andreas Gruenbachere1c1b0f2011-03-16 17:58:27 +01001707 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001708}
1709
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001710static struct drbd_request *
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001711find_request(struct drbd_conf *mdev, struct rb_root *root, u64 id,
1712 sector_t sector, bool missing_ok, const char *func)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001713{
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001714 struct drbd_request *req;
1715
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001716 /* Request object according to our peer */
1717 req = (struct drbd_request *)(unsigned long)id;
Andreas Gruenbacher5e472262011-01-27 14:42:51 +01001718 if (drbd_contains_interval(root, sector, &req->i) && req->i.local)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001719 return req;
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001720 if (!missing_ok) {
Andreas Gruenbacher5af172e2011-07-15 09:43:23 +02001721 dev_err(DEV, "%s: failed to find request 0x%lx, sector %llus\n", func,
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001722 (unsigned long)id, (unsigned long long)sector);
1723 }
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001724 return NULL;
1725}
1726
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001727static int receive_DataReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001728{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001729 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001730 struct drbd_request *req;
1731 sector_t sector;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001732 int err;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001733 struct p_data *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001734
1735 mdev = vnr_to_mdev(tconn, pi->vnr);
1736 if (!mdev)
1737 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001738
1739 sector = be64_to_cpu(p->sector);
1740
Philipp Reisner87eeee42011-01-19 14:16:30 +01001741 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001742 req = find_request(mdev, &mdev->read_requests, p->block_id, sector, false, __func__);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001743 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001744 if (unlikely(!req))
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001745 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001746
Bart Van Assche24c48302011-05-21 18:32:29 +02001747 /* hlist_del(&req->collision) is done in _req_may_be_done, to avoid
Philipp Reisnerb411b362009-09-25 16:07:19 -07001748 * special casing it there for the various failure cases.
1749 * still no race with drbd_fail_pending_reads */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001750 err = recv_dless_read(mdev, req, sector, pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001751 if (!err)
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001752 req_mod(req, DATA_RECEIVED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001753 /* else: nothing. handled from drbd_disconnect...
1754 * I don't think we may complete this just yet
1755 * in case we are "on-disconnect: freeze" */
1756
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001757 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001758}
1759
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001760static int receive_RSDataReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001761{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001762 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001763 sector_t sector;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001764 int err;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001765 struct p_data *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001766
1767 mdev = vnr_to_mdev(tconn, pi->vnr);
1768 if (!mdev)
1769 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001770
1771 sector = be64_to_cpu(p->sector);
1772 D_ASSERT(p->block_id == ID_SYNCER);
1773
1774 if (get_ldev(mdev)) {
1775 /* data is submitted to disk within recv_resync_read.
1776 * corresponding put_ldev done below on error,
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01001777 * or in drbd_peer_request_endio. */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001778 err = recv_resync_read(mdev, sector, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001779 } else {
1780 if (__ratelimit(&drbd_ratelimit_state))
1781 dev_err(DEV, "Can not write resync data to local disk.\n");
1782
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001783 err = drbd_drain_block(mdev, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001784
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001785 drbd_send_ack_dp(mdev, P_NEG_ACK, p, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001786 }
1787
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001788 atomic_add(pi->size >> 9, &mdev->rs_sect_in);
Philipp Reisner778f2712010-07-06 11:14:00 +02001789
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001790 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001791}
1792
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001793static void restart_conflicting_writes(struct drbd_conf *mdev,
1794 sector_t sector, int size)
1795{
1796 struct drbd_interval *i;
1797 struct drbd_request *req;
1798
1799 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
1800 if (!i->local)
1801 continue;
1802 req = container_of(i, struct drbd_request, i);
1803 if (req->rq_state & RQ_LOCAL_PENDING ||
1804 !(req->rq_state & RQ_POSTPONED))
1805 continue;
Lars Ellenberg2312f0b32011-11-24 10:36:25 +01001806 /* as it is RQ_POSTPONED, this will cause it to
1807 * be queued on the retry workqueue. */
1808 __req_mod(req, DISCARD_WRITE, NULL);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001809 }
1810}
1811
Andreas Gruenbachera990be42011-04-06 17:56:48 +02001812/*
1813 * e_end_block() is called in asender context via drbd_finish_peer_reqs().
Philipp Reisnerb411b362009-09-25 16:07:19 -07001814 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001815static int e_end_block(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001816{
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001817 struct drbd_peer_request *peer_req =
1818 container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001819 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001820 sector_t sector = peer_req->i.sector;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001821 int err = 0, pcmd;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001822
Philipp Reisner303d1442011-04-13 16:24:47 -07001823 if (peer_req->flags & EE_SEND_WRITE_ACK) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001824 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001825 pcmd = (mdev->state.conn >= C_SYNC_SOURCE &&
1826 mdev->state.conn <= C_PAUSED_SYNC_T &&
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001827 peer_req->flags & EE_MAY_SET_IN_SYNC) ?
Philipp Reisnerb411b362009-09-25 16:07:19 -07001828 P_RS_WRITE_ACK : P_WRITE_ACK;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001829 err = drbd_send_ack(mdev, pcmd, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001830 if (pcmd == P_RS_WRITE_ACK)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001831 drbd_set_in_sync(mdev, sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001832 } else {
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001833 err = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001834 /* we expect it to be marked out of sync anyways...
1835 * maybe assert this? */
1836 }
1837 dec_unacked(mdev);
1838 }
1839 /* we delete from the conflict detection hash _after_ we sent out the
1840 * P_WRITE_ACK / P_NEG_ACK, to get the sequence number right. */
Philipp Reisner302bdea2011-04-21 11:36:49 +02001841 if (peer_req->flags & EE_IN_INTERVAL_TREE) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01001842 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001843 D_ASSERT(!drbd_interval_empty(&peer_req->i));
1844 drbd_remove_epoch_entry_interval(mdev, peer_req);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001845 if (peer_req->flags & EE_RESTART_REQUESTS)
1846 restart_conflicting_writes(mdev, sector, peer_req->i.size);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001847 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbb3bfe92011-01-21 15:59:23 +01001848 } else
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001849 D_ASSERT(drbd_interval_empty(&peer_req->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001850
Philipp Reisner1e9dd292011-11-10 15:14:53 +01001851 drbd_may_finish_epoch(mdev->tconn, peer_req->epoch, EV_PUT + (cancel ? EV_CLEANUP : 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001852
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001853 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001854}
1855
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001856static int e_send_ack(struct drbd_work *w, enum drbd_packet ack)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001857{
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001858 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001859 struct drbd_peer_request *peer_req =
1860 container_of(w, struct drbd_peer_request, w);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001861 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001862
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001863 err = drbd_send_ack(mdev, ack, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001864 dec_unacked(mdev);
1865
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001866 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001867}
1868
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001869static int e_send_discard_write(struct drbd_work *w, int unused)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001870{
1871 return e_send_ack(w, P_DISCARD_WRITE);
1872}
1873
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001874static int e_send_retry_write(struct drbd_work *w, int unused)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001875{
1876 struct drbd_tconn *tconn = w->mdev->tconn;
1877
1878 return e_send_ack(w, tconn->agreed_pro_version >= 100 ?
1879 P_RETRY_WRITE : P_DISCARD_WRITE);
1880}
1881
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001882static bool seq_greater(u32 a, u32 b)
1883{
1884 /*
1885 * We assume 32-bit wrap-around here.
1886 * For 24-bit wrap-around, we would have to shift:
1887 * a <<= 8; b <<= 8;
1888 */
1889 return (s32)a - (s32)b > 0;
1890}
1891
1892static u32 seq_max(u32 a, u32 b)
1893{
1894 return seq_greater(a, b) ? a : b;
1895}
1896
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001897static bool need_peer_seq(struct drbd_conf *mdev)
1898{
1899 struct drbd_tconn *tconn = mdev->tconn;
Philipp Reisner302bdea2011-04-21 11:36:49 +02001900 int tp;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001901
1902 /*
1903 * We only need to keep track of the last packet_seq number of our peer
1904 * if we are in dual-primary mode and we have the discard flag set; see
1905 * handle_write_conflicts().
1906 */
Philipp Reisner302bdea2011-04-21 11:36:49 +02001907
1908 rcu_read_lock();
1909 tp = rcu_dereference(mdev->tconn->net_conf)->two_primaries;
1910 rcu_read_unlock();
1911
1912 return tp && test_bit(DISCARD_CONCURRENT, &tconn->flags);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001913}
1914
Andreas Gruenbacher43ae0772011-02-03 18:42:08 +01001915static void update_peer_seq(struct drbd_conf *mdev, unsigned int peer_seq)
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001916{
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001917 unsigned int newest_peer_seq;
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001918
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001919 if (need_peer_seq(mdev)) {
1920 spin_lock(&mdev->peer_seq_lock);
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001921 newest_peer_seq = seq_max(mdev->peer_seq, peer_seq);
1922 mdev->peer_seq = newest_peer_seq;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001923 spin_unlock(&mdev->peer_seq_lock);
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001924 /* wake up only if we actually changed mdev->peer_seq */
1925 if (peer_seq == newest_peer_seq)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001926 wake_up(&mdev->seq_wait);
1927 }
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001928}
1929
Lars Ellenbergd93f6302012-03-26 15:49:13 +02001930static inline int overlaps(sector_t s1, int l1, sector_t s2, int l2)
1931{
1932 return !((s1 + (l1>>9) <= s2) || (s1 >= s2 + (l2>>9)));
1933}
1934
1935/* maybe change sync_ee into interval trees as well? */
Philipp Reisner3ea35df2012-04-06 12:13:18 +02001936static bool overlapping_resync_write(struct drbd_conf *mdev, struct drbd_peer_request *peer_req)
Lars Ellenbergd93f6302012-03-26 15:49:13 +02001937{
1938 struct drbd_peer_request *rs_req;
1939 bool rv = 0;
1940
1941 spin_lock_irq(&mdev->tconn->req_lock);
1942 list_for_each_entry(rs_req, &mdev->sync_ee, w.list) {
1943 if (overlaps(peer_req->i.sector, peer_req->i.size,
1944 rs_req->i.sector, rs_req->i.size)) {
1945 rv = 1;
1946 break;
1947 }
1948 }
1949 spin_unlock_irq(&mdev->tconn->req_lock);
1950
Lars Ellenbergd93f6302012-03-26 15:49:13 +02001951 return rv;
1952}
1953
Philipp Reisnerb411b362009-09-25 16:07:19 -07001954/* Called from receive_Data.
1955 * Synchronize packets on sock with packets on msock.
1956 *
1957 * This is here so even when a P_DATA packet traveling via sock overtook an Ack
1958 * packet traveling on msock, they are still processed in the order they have
1959 * been sent.
1960 *
1961 * Note: we don't care for Ack packets overtaking P_DATA packets.
1962 *
1963 * In case packet_seq is larger than mdev->peer_seq number, there are
1964 * outstanding packets on the msock. We wait for them to arrive.
1965 * In case we are the logically next packet, we update mdev->peer_seq
1966 * ourselves. Correctly handles 32bit wrap around.
1967 *
1968 * Assume we have a 10 GBit connection, that is about 1<<30 byte per second,
1969 * about 1<<21 sectors per second. So "worst" case, we have 1<<3 == 8 seconds
1970 * for the 24bit wrap (historical atomic_t guarantee on some archs), and we have
1971 * 1<<9 == 512 seconds aka ages for the 32bit wrap around...
1972 *
1973 * returns 0 if we may process the packet,
1974 * -ERESTARTSYS if we were interrupted (by disconnect signal). */
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001975static int wait_for_and_update_peer_seq(struct drbd_conf *mdev, const u32 peer_seq)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001976{
1977 DEFINE_WAIT(wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001978 long timeout;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001979 int ret;
1980
1981 if (!need_peer_seq(mdev))
1982 return 0;
1983
Philipp Reisnerb411b362009-09-25 16:07:19 -07001984 spin_lock(&mdev->peer_seq_lock);
1985 for (;;) {
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001986 if (!seq_greater(peer_seq - 1, mdev->peer_seq)) {
1987 mdev->peer_seq = seq_max(mdev->peer_seq, peer_seq);
1988 ret = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001989 break;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001990 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001991 if (signal_pending(current)) {
1992 ret = -ERESTARTSYS;
1993 break;
1994 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001995 prepare_to_wait(&mdev->seq_wait, &wait, TASK_INTERRUPTIBLE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001996 spin_unlock(&mdev->peer_seq_lock);
Philipp Reisner44ed1672011-04-19 17:10:19 +02001997 rcu_read_lock();
1998 timeout = rcu_dereference(mdev->tconn->net_conf)->ping_timeo*HZ/10;
1999 rcu_read_unlock();
Andreas Gruenbacher71b1c1e2011-03-01 15:40:43 +01002000 timeout = schedule_timeout(timeout);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002001 spin_lock(&mdev->peer_seq_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002002 if (!timeout) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002003 ret = -ETIMEDOUT;
Andreas Gruenbacher71b1c1e2011-03-01 15:40:43 +01002004 dev_err(DEV, "Timed out waiting for missing ack packets; disconnecting\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002005 break;
2006 }
2007 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002008 spin_unlock(&mdev->peer_seq_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002009 finish_wait(&mdev->seq_wait, &wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002010 return ret;
2011}
2012
Lars Ellenberg688593c2010-11-17 22:25:03 +01002013/* see also bio_flags_to_wire()
2014 * DRBD_REQ_*, because we need to semantically map the flags to data packet
2015 * flags and back. We may replicate to other kernel versions. */
2016static unsigned long wire_flags_to_bio(struct drbd_conf *mdev, u32 dpf)
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02002017{
Lars Ellenberg688593c2010-11-17 22:25:03 +01002018 return (dpf & DP_RW_SYNC ? REQ_SYNC : 0) |
2019 (dpf & DP_FUA ? REQ_FUA : 0) |
2020 (dpf & DP_FLUSH ? REQ_FLUSH : 0) |
2021 (dpf & DP_DISCARD ? REQ_DISCARD : 0);
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02002022}
2023
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002024static void fail_postponed_requests(struct drbd_conf *mdev, sector_t sector,
2025 unsigned int size)
2026{
2027 struct drbd_interval *i;
2028
2029 repeat:
2030 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
2031 struct drbd_request *req;
2032 struct bio_and_error m;
2033
2034 if (!i->local)
2035 continue;
2036 req = container_of(i, struct drbd_request, i);
2037 if (!(req->rq_state & RQ_POSTPONED))
2038 continue;
2039 req->rq_state &= ~RQ_POSTPONED;
2040 __req_mod(req, NEG_ACKED, &m);
2041 spin_unlock_irq(&mdev->tconn->req_lock);
2042 if (m.bio)
2043 complete_master_bio(mdev, &m);
2044 spin_lock_irq(&mdev->tconn->req_lock);
2045 goto repeat;
2046 }
2047}
2048
2049static int handle_write_conflicts(struct drbd_conf *mdev,
2050 struct drbd_peer_request *peer_req)
2051{
2052 struct drbd_tconn *tconn = mdev->tconn;
2053 bool resolve_conflicts = test_bit(DISCARD_CONCURRENT, &tconn->flags);
2054 sector_t sector = peer_req->i.sector;
2055 const unsigned int size = peer_req->i.size;
2056 struct drbd_interval *i;
2057 bool equal;
2058 int err;
2059
2060 /*
2061 * Inserting the peer request into the write_requests tree will prevent
2062 * new conflicting local requests from being added.
2063 */
2064 drbd_insert_interval(&mdev->write_requests, &peer_req->i);
2065
2066 repeat:
2067 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
2068 if (i == &peer_req->i)
2069 continue;
2070
2071 if (!i->local) {
2072 /*
2073 * Our peer has sent a conflicting remote request; this
2074 * should not happen in a two-node setup. Wait for the
2075 * earlier peer request to complete.
2076 */
2077 err = drbd_wait_misc(mdev, i);
2078 if (err)
2079 goto out;
2080 goto repeat;
2081 }
2082
2083 equal = i->sector == sector && i->size == size;
2084 if (resolve_conflicts) {
2085 /*
2086 * If the peer request is fully contained within the
2087 * overlapping request, it can be discarded; otherwise,
2088 * it will be retried once all overlapping requests
2089 * have completed.
2090 */
2091 bool discard = i->sector <= sector && i->sector +
2092 (i->size >> 9) >= sector + (size >> 9);
2093
2094 if (!equal)
2095 dev_alert(DEV, "Concurrent writes detected: "
2096 "local=%llus +%u, remote=%llus +%u, "
2097 "assuming %s came first\n",
2098 (unsigned long long)i->sector, i->size,
2099 (unsigned long long)sector, size,
2100 discard ? "local" : "remote");
2101
2102 inc_unacked(mdev);
2103 peer_req->w.cb = discard ? e_send_discard_write :
2104 e_send_retry_write;
2105 list_add_tail(&peer_req->w.list, &mdev->done_ee);
2106 wake_asender(mdev->tconn);
2107
2108 err = -ENOENT;
2109 goto out;
2110 } else {
2111 struct drbd_request *req =
2112 container_of(i, struct drbd_request, i);
2113
2114 if (!equal)
2115 dev_alert(DEV, "Concurrent writes detected: "
2116 "local=%llus +%u, remote=%llus +%u\n",
2117 (unsigned long long)i->sector, i->size,
2118 (unsigned long long)sector, size);
2119
2120 if (req->rq_state & RQ_LOCAL_PENDING ||
2121 !(req->rq_state & RQ_POSTPONED)) {
2122 /*
2123 * Wait for the node with the discard flag to
2124 * decide if this request will be discarded or
2125 * retried. Requests that are discarded will
2126 * disappear from the write_requests tree.
2127 *
2128 * In addition, wait for the conflicting
2129 * request to finish locally before submitting
2130 * the conflicting peer request.
2131 */
2132 err = drbd_wait_misc(mdev, &req->i);
2133 if (err) {
2134 _conn_request_state(mdev->tconn,
2135 NS(conn, C_TIMEOUT),
2136 CS_HARD);
2137 fail_postponed_requests(mdev, sector, size);
2138 goto out;
2139 }
2140 goto repeat;
2141 }
2142 /*
2143 * Remember to restart the conflicting requests after
2144 * the new peer request has completed.
2145 */
2146 peer_req->flags |= EE_RESTART_REQUESTS;
2147 }
2148 }
2149 err = 0;
2150
2151 out:
2152 if (err)
2153 drbd_remove_epoch_entry_interval(mdev, peer_req);
2154 return err;
2155}
2156
Philipp Reisnerb411b362009-09-25 16:07:19 -07002157/* mirrored write */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002158static int receive_Data(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002159{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002160 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002161 sector_t sector;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002162 struct drbd_peer_request *peer_req;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02002163 struct p_data *p = pi->data;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002164 u32 peer_seq = be32_to_cpu(p->seq_num);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002165 int rw = WRITE;
2166 u32 dp_flags;
Philipp Reisner302bdea2011-04-21 11:36:49 +02002167 int err, tp;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002168
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002169 mdev = vnr_to_mdev(tconn, pi->vnr);
2170 if (!mdev)
2171 return -EIO;
2172
Philipp Reisnerb411b362009-09-25 16:07:19 -07002173 if (!get_ldev(mdev)) {
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002174 int err2;
2175
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002176 err = wait_for_and_update_peer_seq(mdev, peer_seq);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002177 drbd_send_ack_dp(mdev, P_NEG_ACK, p, pi->size);
Philipp Reisner12038a32011-11-09 19:18:00 +01002178 atomic_inc(&tconn->current_epoch->epoch_size);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002179 err2 = drbd_drain_block(mdev, pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002180 if (!err)
2181 err = err2;
2182 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002183 }
2184
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01002185 /*
2186 * Corresponding put_ldev done either below (on various errors), or in
2187 * drbd_peer_request_endio, if we successfully submit the data at the
2188 * end of this function.
2189 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002190
2191 sector = be64_to_cpu(p->sector);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002192 peer_req = read_in_block(mdev, p->block_id, sector, pi->size);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002193 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002194 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002195 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002196 }
2197
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002198 peer_req->w.cb = e_end_block;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002199
Lars Ellenberg688593c2010-11-17 22:25:03 +01002200 dp_flags = be32_to_cpu(p->dp_flags);
2201 rw |= wire_flags_to_bio(mdev, dp_flags);
2202
2203 if (dp_flags & DP_MAY_SET_IN_SYNC)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002204 peer_req->flags |= EE_MAY_SET_IN_SYNC;
Lars Ellenberg688593c2010-11-17 22:25:03 +01002205
Philipp Reisner12038a32011-11-09 19:18:00 +01002206 spin_lock(&tconn->epoch_lock);
2207 peer_req->epoch = tconn->current_epoch;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002208 atomic_inc(&peer_req->epoch->epoch_size);
2209 atomic_inc(&peer_req->epoch->active);
Philipp Reisner12038a32011-11-09 19:18:00 +01002210 spin_unlock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002211
Philipp Reisner302bdea2011-04-21 11:36:49 +02002212 rcu_read_lock();
2213 tp = rcu_dereference(mdev->tconn->net_conf)->two_primaries;
2214 rcu_read_unlock();
2215 if (tp) {
2216 peer_req->flags |= EE_IN_INTERVAL_TREE;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002217 err = wait_for_and_update_peer_seq(mdev, peer_seq);
2218 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002219 goto out_interrupted;
Philipp Reisner87eeee42011-01-19 14:16:30 +01002220 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002221 err = handle_write_conflicts(mdev, peer_req);
2222 if (err) {
2223 spin_unlock_irq(&mdev->tconn->req_lock);
2224 if (err == -ENOENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002225 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002226 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002227 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002228 goto out_interrupted;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002229 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002230 } else
2231 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002232 list_add(&peer_req->w.list, &mdev->active_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002233 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002234
Lars Ellenbergd93f6302012-03-26 15:49:13 +02002235 if (mdev->state.conn == C_SYNC_TARGET)
Philipp Reisner3ea35df2012-04-06 12:13:18 +02002236 wait_event(mdev->ee_wait, !overlapping_resync_write(mdev, peer_req));
Lars Ellenbergd93f6302012-03-26 15:49:13 +02002237
Philipp Reisner303d1442011-04-13 16:24:47 -07002238 if (mdev->tconn->agreed_pro_version < 100) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02002239 rcu_read_lock();
2240 switch (rcu_dereference(mdev->tconn->net_conf)->wire_protocol) {
Philipp Reisner303d1442011-04-13 16:24:47 -07002241 case DRBD_PROT_C:
2242 dp_flags |= DP_SEND_WRITE_ACK;
2243 break;
2244 case DRBD_PROT_B:
2245 dp_flags |= DP_SEND_RECEIVE_ACK;
2246 break;
2247 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002248 rcu_read_unlock();
Philipp Reisner303d1442011-04-13 16:24:47 -07002249 }
2250
2251 if (dp_flags & DP_SEND_WRITE_ACK) {
2252 peer_req->flags |= EE_SEND_WRITE_ACK;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002253 inc_unacked(mdev);
2254 /* corresponding dec_unacked() in e_end_block()
2255 * respective _drbd_clear_done_ee */
Philipp Reisner303d1442011-04-13 16:24:47 -07002256 }
2257
2258 if (dp_flags & DP_SEND_RECEIVE_ACK) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002259 /* I really don't like it that the receiver thread
2260 * sends on the msock, but anyways */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002261 drbd_send_ack(mdev, P_RECV_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002262 }
2263
Lars Ellenberg6719fb02010-10-18 23:04:07 +02002264 if (mdev->state.pdsk < D_INCONSISTENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002265 /* In case we have the only disk of the cluster, */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002266 drbd_set_out_of_sync(mdev, peer_req->i.sector, peer_req->i.size);
2267 peer_req->flags |= EE_CALL_AL_COMPLETE_IO;
2268 peer_req->flags &= ~EE_MAY_SET_IN_SYNC;
Lars Ellenberg181286a2011-03-31 15:18:56 +02002269 drbd_al_begin_io(mdev, &peer_req->i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002270 }
2271
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002272 err = drbd_submit_peer_request(mdev, peer_req, rw, DRBD_FAULT_DT_WR);
2273 if (!err)
2274 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002275
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01002276 /* don't care for the reason here */
2277 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002278 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002279 list_del(&peer_req->w.list);
2280 drbd_remove_epoch_entry_interval(mdev, peer_req);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002281 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002282 if (peer_req->flags & EE_CALL_AL_COMPLETE_IO)
Lars Ellenberg181286a2011-03-31 15:18:56 +02002283 drbd_al_complete_io(mdev, &peer_req->i);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002284
Philipp Reisnerb411b362009-09-25 16:07:19 -07002285out_interrupted:
Philipp Reisner1e9dd292011-11-10 15:14:53 +01002286 drbd_may_finish_epoch(tconn, peer_req->epoch, EV_PUT + EV_CLEANUP);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002287 put_ldev(mdev);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02002288 drbd_free_peer_req(mdev, peer_req);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002289 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002290}
2291
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002292/* We may throttle resync, if the lower device seems to be busy,
2293 * and current sync rate is above c_min_rate.
2294 *
2295 * To decide whether or not the lower device is busy, we use a scheme similar
2296 * to MD RAID is_mddev_idle(): if the partition stats reveal "significant"
2297 * (more than 64 sectors) of activity we cannot account for with our own resync
2298 * activity, it obviously is "busy".
2299 *
2300 * The current sync rate used here uses only the most recent two step marks,
2301 * to have a short time average so we can react faster.
2302 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002303int drbd_rs_should_slow_down(struct drbd_conf *mdev, sector_t sector)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002304{
2305 struct gendisk *disk = mdev->ldev->backing_bdev->bd_contains->bd_disk;
2306 unsigned long db, dt, dbdt;
Philipp Reisnere3555d82010-11-07 15:56:29 +01002307 struct lc_element *tmp;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002308 int curr_events;
2309 int throttle = 0;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002310 unsigned int c_min_rate;
2311
2312 rcu_read_lock();
2313 c_min_rate = rcu_dereference(mdev->ldev->disk_conf)->c_min_rate;
2314 rcu_read_unlock();
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002315
2316 /* feature disabled? */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002317 if (c_min_rate == 0)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002318 return 0;
2319
Philipp Reisnere3555d82010-11-07 15:56:29 +01002320 spin_lock_irq(&mdev->al_lock);
2321 tmp = lc_find(mdev->resync, BM_SECT_TO_EXT(sector));
2322 if (tmp) {
2323 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
2324 if (test_bit(BME_PRIORITY, &bm_ext->flags)) {
2325 spin_unlock_irq(&mdev->al_lock);
2326 return 0;
2327 }
2328 /* Do not slow down if app IO is already waiting for this extent */
2329 }
2330 spin_unlock_irq(&mdev->al_lock);
2331
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002332 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
2333 (int)part_stat_read(&disk->part0, sectors[1]) -
2334 atomic_read(&mdev->rs_sect_ev);
Philipp Reisnere3555d82010-11-07 15:56:29 +01002335
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002336 if (!mdev->rs_last_events || curr_events - mdev->rs_last_events > 64) {
2337 unsigned long rs_left;
2338 int i;
2339
2340 mdev->rs_last_events = curr_events;
2341
2342 /* sync speed average over the last 2*DRBD_SYNC_MARK_STEP,
2343 * approx. */
Lars Ellenberg2649f082010-11-05 10:05:47 +01002344 i = (mdev->rs_last_mark + DRBD_SYNC_MARKS-1) % DRBD_SYNC_MARKS;
2345
2346 if (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T)
2347 rs_left = mdev->ov_left;
2348 else
2349 rs_left = drbd_bm_total_weight(mdev) - mdev->rs_failed;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002350
2351 dt = ((long)jiffies - (long)mdev->rs_mark_time[i]) / HZ;
2352 if (!dt)
2353 dt++;
2354 db = mdev->rs_mark_left[i] - rs_left;
2355 dbdt = Bit2KB(db/dt);
2356
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002357 if (dbdt > c_min_rate)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002358 throttle = 1;
2359 }
2360 return throttle;
2361}
2362
2363
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002364static int receive_DataRequest(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002365{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002366 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002367 sector_t sector;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002368 sector_t capacity;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002369 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002370 struct digest_info *di = NULL;
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002371 int size, verb;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002372 unsigned int fault_type;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02002373 struct p_block_req *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002374
2375 mdev = vnr_to_mdev(tconn, pi->vnr);
2376 if (!mdev)
2377 return -EIO;
2378 capacity = drbd_get_capacity(mdev->this_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002379
2380 sector = be64_to_cpu(p->sector);
2381 size = be32_to_cpu(p->blksize);
2382
Andreas Gruenbacherc670a392011-02-21 12:41:39 +01002383 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002384 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2385 (unsigned long long)sector, size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002386 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002387 }
2388 if (sector + (size>>9) > capacity) {
2389 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2390 (unsigned long long)sector, size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002391 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002392 }
2393
2394 if (!get_ldev_if_state(mdev, D_UP_TO_DATE)) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002395 verb = 1;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002396 switch (pi->cmd) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002397 case P_DATA_REQUEST:
2398 drbd_send_ack_rp(mdev, P_NEG_DREPLY, p);
2399 break;
2400 case P_RS_DATA_REQUEST:
2401 case P_CSUM_RS_REQUEST:
2402 case P_OV_REQUEST:
2403 drbd_send_ack_rp(mdev, P_NEG_RS_DREPLY , p);
2404 break;
2405 case P_OV_REPLY:
2406 verb = 0;
2407 dec_rs_pending(mdev);
2408 drbd_send_ack_ex(mdev, P_OV_RESULT, sector, size, ID_IN_SYNC);
2409 break;
2410 default:
Andreas Gruenbacher49ba9b12011-03-25 00:35:45 +01002411 BUG();
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002412 }
2413 if (verb && __ratelimit(&drbd_ratelimit_state))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002414 dev_err(DEV, "Can not satisfy peer's read request, "
2415 "no local data.\n");
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002416
Lars Ellenberga821cc42010-09-06 12:31:37 +02002417 /* drain possibly payload */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002418 return drbd_drain_block(mdev, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002419 }
2420
2421 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
2422 * "criss-cross" setup, that might cause write-out on some other DRBD,
2423 * which in turn might block on the other node at this very place. */
Andreas Gruenbacher0db55362011-04-06 16:09:15 +02002424 peer_req = drbd_alloc_peer_req(mdev, p->block_id, sector, size, GFP_NOIO);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002425 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002426 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002427 return -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002428 }
2429
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002430 switch (pi->cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002431 case P_DATA_REQUEST:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002432 peer_req->w.cb = w_e_end_data_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002433 fault_type = DRBD_FAULT_DT_RD;
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002434 /* application IO, don't drbd_rs_begin_io */
2435 goto submit;
2436
Philipp Reisnerb411b362009-09-25 16:07:19 -07002437 case P_RS_DATA_REQUEST:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002438 peer_req->w.cb = w_e_end_rsdata_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002439 fault_type = DRBD_FAULT_RS_RD;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002440 /* used in the sector offset progress display */
2441 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002442 break;
2443
2444 case P_OV_REPLY:
2445 case P_CSUM_RS_REQUEST:
2446 fault_type = DRBD_FAULT_RS_RD;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002447 di = kmalloc(sizeof(*di) + pi->size, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002448 if (!di)
2449 goto out_free_e;
2450
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002451 di->digest_size = pi->size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002452 di->digest = (((char *)di)+sizeof(struct digest_info));
2453
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002454 peer_req->digest = di;
2455 peer_req->flags |= EE_HAS_DIGEST;
Lars Ellenbergc36c3ce2010-08-11 20:42:55 +02002456
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002457 if (drbd_recv_all(mdev->tconn, di->digest, pi->size))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002458 goto out_free_e;
2459
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002460 if (pi->cmd == P_CSUM_RS_REQUEST) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002461 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002462 peer_req->w.cb = w_e_end_csum_rs_req;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002463 /* used in the sector offset progress display */
2464 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002465 } else if (pi->cmd == P_OV_REPLY) {
Lars Ellenberg2649f082010-11-05 10:05:47 +01002466 /* track progress, we may need to throttle */
2467 atomic_add(size >> 9, &mdev->rs_sect_in);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002468 peer_req->w.cb = w_e_end_ov_reply;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002469 dec_rs_pending(mdev);
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002470 /* drbd_rs_begin_io done when we sent this request,
2471 * but accounting still needs to be done. */
2472 goto submit_for_resync;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002473 }
2474 break;
2475
2476 case P_OV_REQUEST:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002477 if (mdev->ov_start_sector == ~(sector_t)0 &&
Philipp Reisner31890f42011-01-19 14:12:51 +01002478 mdev->tconn->agreed_pro_version >= 90) {
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002479 unsigned long now = jiffies;
2480 int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002481 mdev->ov_start_sector = sector;
2482 mdev->ov_position = sector;
Lars Ellenberg30b743a2010-11-05 09:39:06 +01002483 mdev->ov_left = drbd_bm_bits(mdev) - BM_SECT_TO_BIT(sector);
2484 mdev->rs_total = mdev->ov_left;
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002485 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
2486 mdev->rs_mark_left[i] = mdev->ov_left;
2487 mdev->rs_mark_time[i] = now;
2488 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002489 dev_info(DEV, "Online Verify start sector: %llu\n",
2490 (unsigned long long)sector);
2491 }
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002492 peer_req->w.cb = w_e_end_ov_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002493 fault_type = DRBD_FAULT_RS_RD;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002494 break;
2495
Philipp Reisnerb411b362009-09-25 16:07:19 -07002496 default:
Andreas Gruenbacher49ba9b12011-03-25 00:35:45 +01002497 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002498 }
2499
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002500 /* Throttle, drbd_rs_begin_io and submit should become asynchronous
2501 * wrt the receiver, but it is not as straightforward as it may seem.
2502 * Various places in the resync start and stop logic assume resync
2503 * requests are processed in order, requeuing this on the worker thread
2504 * introduces a bunch of new code for synchronization between threads.
2505 *
2506 * Unlimited throttling before drbd_rs_begin_io may stall the resync
2507 * "forever", throttling after drbd_rs_begin_io will lock that extent
2508 * for application writes for the same time. For now, just throttle
2509 * here, where the rest of the code expects the receiver to sleep for
2510 * a while, anyways.
2511 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002512
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002513 /* Throttle before drbd_rs_begin_io, as that locks out application IO;
2514 * this defers syncer requests for some time, before letting at least
2515 * on request through. The resync controller on the receiving side
2516 * will adapt to the incoming rate accordingly.
2517 *
2518 * We cannot throttle here if remote is Primary/SyncTarget:
2519 * we would also throttle its application reads.
2520 * In that case, throttling is done on the SyncTarget only.
2521 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002522 if (mdev->state.peer != R_PRIMARY && drbd_rs_should_slow_down(mdev, sector))
2523 schedule_timeout_uninterruptible(HZ/10);
2524 if (drbd_rs_begin_io(mdev, sector))
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002525 goto out_free_e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002526
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002527submit_for_resync:
2528 atomic_add(size >> 9, &mdev->rs_sect_ev);
2529
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002530submit:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002531 inc_unacked(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002532 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002533 list_add_tail(&peer_req->w.list, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002534 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002535
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01002536 if (drbd_submit_peer_request(mdev, peer_req, READ, fault_type) == 0)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002537 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002538
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01002539 /* don't care for the reason here */
2540 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002541 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002542 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002543 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002544 /* no drbd_rs_complete_io(), we are dropping the connection anyways */
2545
Philipp Reisnerb411b362009-09-25 16:07:19 -07002546out_free_e:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002547 put_ldev(mdev);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02002548 drbd_free_peer_req(mdev, peer_req);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002549 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002550}
2551
2552static int drbd_asb_recover_0p(struct drbd_conf *mdev) __must_hold(local)
2553{
2554 int self, peer, rv = -100;
2555 unsigned long ch_self, ch_peer;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002556 enum drbd_after_sb_p after_sb_0p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002557
2558 self = mdev->ldev->md.uuid[UI_BITMAP] & 1;
2559 peer = mdev->p_uuid[UI_BITMAP] & 1;
2560
2561 ch_peer = mdev->p_uuid[UI_SIZE];
2562 ch_self = mdev->comm_bm_set;
2563
Philipp Reisner44ed1672011-04-19 17:10:19 +02002564 rcu_read_lock();
2565 after_sb_0p = rcu_dereference(mdev->tconn->net_conf)->after_sb_0p;
2566 rcu_read_unlock();
2567 switch (after_sb_0p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002568 case ASB_CONSENSUS:
2569 case ASB_DISCARD_SECONDARY:
2570 case ASB_CALL_HELPER:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002571 case ASB_VIOLENTLY:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002572 dev_err(DEV, "Configuration error.\n");
2573 break;
2574 case ASB_DISCONNECT:
2575 break;
2576 case ASB_DISCARD_YOUNGER_PRI:
2577 if (self == 0 && peer == 1) {
2578 rv = -1;
2579 break;
2580 }
2581 if (self == 1 && peer == 0) {
2582 rv = 1;
2583 break;
2584 }
2585 /* Else fall through to one of the other strategies... */
2586 case ASB_DISCARD_OLDER_PRI:
2587 if (self == 0 && peer == 1) {
2588 rv = 1;
2589 break;
2590 }
2591 if (self == 1 && peer == 0) {
2592 rv = -1;
2593 break;
2594 }
2595 /* Else fall through to one of the other strategies... */
Lars Ellenbergad19bf62009-10-14 09:36:49 +02002596 dev_warn(DEV, "Discard younger/older primary did not find a decision\n"
Philipp Reisnerb411b362009-09-25 16:07:19 -07002597 "Using discard-least-changes instead\n");
2598 case ASB_DISCARD_ZERO_CHG:
2599 if (ch_peer == 0 && ch_self == 0) {
Philipp Reisner25703f82011-02-07 14:35:25 +01002600 rv = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002601 ? -1 : 1;
2602 break;
2603 } else {
2604 if (ch_peer == 0) { rv = 1; break; }
2605 if (ch_self == 0) { rv = -1; break; }
2606 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002607 if (after_sb_0p == ASB_DISCARD_ZERO_CHG)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002608 break;
2609 case ASB_DISCARD_LEAST_CHG:
2610 if (ch_self < ch_peer)
2611 rv = -1;
2612 else if (ch_self > ch_peer)
2613 rv = 1;
2614 else /* ( ch_self == ch_peer ) */
2615 /* Well, then use something else. */
Philipp Reisner25703f82011-02-07 14:35:25 +01002616 rv = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002617 ? -1 : 1;
2618 break;
2619 case ASB_DISCARD_LOCAL:
2620 rv = -1;
2621 break;
2622 case ASB_DISCARD_REMOTE:
2623 rv = 1;
2624 }
2625
2626 return rv;
2627}
2628
2629static int drbd_asb_recover_1p(struct drbd_conf *mdev) __must_hold(local)
2630{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002631 int hg, rv = -100;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002632 enum drbd_after_sb_p after_sb_1p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002633
Philipp Reisner44ed1672011-04-19 17:10:19 +02002634 rcu_read_lock();
2635 after_sb_1p = rcu_dereference(mdev->tconn->net_conf)->after_sb_1p;
2636 rcu_read_unlock();
2637 switch (after_sb_1p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002638 case ASB_DISCARD_YOUNGER_PRI:
2639 case ASB_DISCARD_OLDER_PRI:
2640 case ASB_DISCARD_LEAST_CHG:
2641 case ASB_DISCARD_LOCAL:
2642 case ASB_DISCARD_REMOTE:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002643 case ASB_DISCARD_ZERO_CHG:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002644 dev_err(DEV, "Configuration error.\n");
2645 break;
2646 case ASB_DISCONNECT:
2647 break;
2648 case ASB_CONSENSUS:
2649 hg = drbd_asb_recover_0p(mdev);
2650 if (hg == -1 && mdev->state.role == R_SECONDARY)
2651 rv = hg;
2652 if (hg == 1 && mdev->state.role == R_PRIMARY)
2653 rv = hg;
2654 break;
2655 case ASB_VIOLENTLY:
2656 rv = drbd_asb_recover_0p(mdev);
2657 break;
2658 case ASB_DISCARD_SECONDARY:
2659 return mdev->state.role == R_PRIMARY ? 1 : -1;
2660 case ASB_CALL_HELPER:
2661 hg = drbd_asb_recover_0p(mdev);
2662 if (hg == -1 && mdev->state.role == R_PRIMARY) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002663 enum drbd_state_rv rv2;
2664
2665 drbd_set_role(mdev, R_SECONDARY, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002666 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2667 * we might be here in C_WF_REPORT_PARAMS which is transient.
2668 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002669 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2670 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002671 drbd_khelper(mdev, "pri-lost-after-sb");
2672 } else {
2673 dev_warn(DEV, "Successfully gave up primary role.\n");
2674 rv = hg;
2675 }
2676 } else
2677 rv = hg;
2678 }
2679
2680 return rv;
2681}
2682
2683static int drbd_asb_recover_2p(struct drbd_conf *mdev) __must_hold(local)
2684{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002685 int hg, rv = -100;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002686 enum drbd_after_sb_p after_sb_2p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002687
Philipp Reisner44ed1672011-04-19 17:10:19 +02002688 rcu_read_lock();
2689 after_sb_2p = rcu_dereference(mdev->tconn->net_conf)->after_sb_2p;
2690 rcu_read_unlock();
2691 switch (after_sb_2p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002692 case ASB_DISCARD_YOUNGER_PRI:
2693 case ASB_DISCARD_OLDER_PRI:
2694 case ASB_DISCARD_LEAST_CHG:
2695 case ASB_DISCARD_LOCAL:
2696 case ASB_DISCARD_REMOTE:
2697 case ASB_CONSENSUS:
2698 case ASB_DISCARD_SECONDARY:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002699 case ASB_DISCARD_ZERO_CHG:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002700 dev_err(DEV, "Configuration error.\n");
2701 break;
2702 case ASB_VIOLENTLY:
2703 rv = drbd_asb_recover_0p(mdev);
2704 break;
2705 case ASB_DISCONNECT:
2706 break;
2707 case ASB_CALL_HELPER:
2708 hg = drbd_asb_recover_0p(mdev);
2709 if (hg == -1) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002710 enum drbd_state_rv rv2;
2711
Philipp Reisnerb411b362009-09-25 16:07:19 -07002712 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2713 * we might be here in C_WF_REPORT_PARAMS which is transient.
2714 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002715 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2716 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002717 drbd_khelper(mdev, "pri-lost-after-sb");
2718 } else {
2719 dev_warn(DEV, "Successfully gave up primary role.\n");
2720 rv = hg;
2721 }
2722 } else
2723 rv = hg;
2724 }
2725
2726 return rv;
2727}
2728
2729static void drbd_uuid_dump(struct drbd_conf *mdev, char *text, u64 *uuid,
2730 u64 bits, u64 flags)
2731{
2732 if (!uuid) {
2733 dev_info(DEV, "%s uuid info vanished while I was looking!\n", text);
2734 return;
2735 }
2736 dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX bits:%llu flags:%llX\n",
2737 text,
2738 (unsigned long long)uuid[UI_CURRENT],
2739 (unsigned long long)uuid[UI_BITMAP],
2740 (unsigned long long)uuid[UI_HISTORY_START],
2741 (unsigned long long)uuid[UI_HISTORY_END],
2742 (unsigned long long)bits,
2743 (unsigned long long)flags);
2744}
2745
2746/*
2747 100 after split brain try auto recover
2748 2 C_SYNC_SOURCE set BitMap
2749 1 C_SYNC_SOURCE use BitMap
2750 0 no Sync
2751 -1 C_SYNC_TARGET use BitMap
2752 -2 C_SYNC_TARGET set BitMap
2753 -100 after split brain, disconnect
2754-1000 unrelated data
Philipp Reisner4a23f262011-01-11 17:42:17 +01002755-1091 requires proto 91
2756-1096 requires proto 96
Philipp Reisnerb411b362009-09-25 16:07:19 -07002757 */
2758static int drbd_uuid_compare(struct drbd_conf *mdev, int *rule_nr) __must_hold(local)
2759{
2760 u64 self, peer;
2761 int i, j;
2762
2763 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2764 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2765
2766 *rule_nr = 10;
2767 if (self == UUID_JUST_CREATED && peer == UUID_JUST_CREATED)
2768 return 0;
2769
2770 *rule_nr = 20;
2771 if ((self == UUID_JUST_CREATED || self == (u64)0) &&
2772 peer != UUID_JUST_CREATED)
2773 return -2;
2774
2775 *rule_nr = 30;
2776 if (self != UUID_JUST_CREATED &&
2777 (peer == UUID_JUST_CREATED || peer == (u64)0))
2778 return 2;
2779
2780 if (self == peer) {
2781 int rct, dc; /* roles at crash time */
2782
2783 if (mdev->p_uuid[UI_BITMAP] == (u64)0 && mdev->ldev->md.uuid[UI_BITMAP] != (u64)0) {
2784
Philipp Reisner31890f42011-01-19 14:12:51 +01002785 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002786 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002787
2788 if ((mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) &&
2789 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1))) {
2790 dev_info(DEV, "was SyncSource, missed the resync finished event, corrected myself:\n");
2791 drbd_uuid_set_bm(mdev, 0UL);
2792
2793 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2794 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2795 *rule_nr = 34;
2796 } else {
2797 dev_info(DEV, "was SyncSource (peer failed to write sync_uuid)\n");
2798 *rule_nr = 36;
2799 }
2800
2801 return 1;
2802 }
2803
2804 if (mdev->ldev->md.uuid[UI_BITMAP] == (u64)0 && mdev->p_uuid[UI_BITMAP] != (u64)0) {
2805
Philipp Reisner31890f42011-01-19 14:12:51 +01002806 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002807 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002808
2809 if ((mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_BITMAP] & ~((u64)1)) &&
2810 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1))) {
2811 dev_info(DEV, "was SyncTarget, peer missed the resync finished event, corrected peer:\n");
2812
2813 mdev->p_uuid[UI_HISTORY_START + 1] = mdev->p_uuid[UI_HISTORY_START];
2814 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_BITMAP];
2815 mdev->p_uuid[UI_BITMAP] = 0UL;
2816
2817 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2818 *rule_nr = 35;
2819 } else {
2820 dev_info(DEV, "was SyncTarget (failed to write sync_uuid)\n");
2821 *rule_nr = 37;
2822 }
2823
2824 return -1;
2825 }
2826
2827 /* Common power [off|failure] */
2828 rct = (test_bit(CRASHED_PRIMARY, &mdev->flags) ? 1 : 0) +
2829 (mdev->p_uuid[UI_FLAGS] & 2);
2830 /* lowest bit is set when we were primary,
2831 * next bit (weight 2) is set when peer was primary */
2832 *rule_nr = 40;
2833
2834 switch (rct) {
2835 case 0: /* !self_pri && !peer_pri */ return 0;
2836 case 1: /* self_pri && !peer_pri */ return 1;
2837 case 2: /* !self_pri && peer_pri */ return -1;
2838 case 3: /* self_pri && peer_pri */
Philipp Reisner25703f82011-02-07 14:35:25 +01002839 dc = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002840 return dc ? -1 : 1;
2841 }
2842 }
2843
2844 *rule_nr = 50;
2845 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2846 if (self == peer)
2847 return -1;
2848
2849 *rule_nr = 51;
2850 peer = mdev->p_uuid[UI_HISTORY_START] & ~((u64)1);
2851 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002852 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002853 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) ==
2854 (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1)) :
2855 peer + UUID_NEW_BM_OFFSET == (mdev->p_uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002856 /* The last P_SYNC_UUID did not get though. Undo the last start of
2857 resync as sync source modifications of the peer's UUIDs. */
2858
Philipp Reisner31890f42011-01-19 14:12:51 +01002859 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002860 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002861
2862 mdev->p_uuid[UI_BITMAP] = mdev->p_uuid[UI_HISTORY_START];
2863 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_HISTORY_START + 1];
Philipp Reisner4a23f262011-01-11 17:42:17 +01002864
Lars Ellenberg1882e222012-05-07 13:09:00 +02002865 dev_info(DEV, "Lost last syncUUID packet, corrected:\n");
Philipp Reisner4a23f262011-01-11 17:42:17 +01002866 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2867
Philipp Reisnerb411b362009-09-25 16:07:19 -07002868 return -1;
2869 }
2870 }
2871
2872 *rule_nr = 60;
2873 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2874 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2875 peer = mdev->p_uuid[i] & ~((u64)1);
2876 if (self == peer)
2877 return -2;
2878 }
2879
2880 *rule_nr = 70;
2881 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2882 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2883 if (self == peer)
2884 return 1;
2885
2886 *rule_nr = 71;
2887 self = mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1);
2888 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002889 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002890 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) ==
2891 (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) :
2892 self + UUID_NEW_BM_OFFSET == (mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002893 /* The last P_SYNC_UUID did not get though. Undo the last start of
2894 resync as sync source modifications of our UUIDs. */
2895
Philipp Reisner31890f42011-01-19 14:12:51 +01002896 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002897 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002898
2899 _drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_HISTORY_START]);
2900 _drbd_uuid_set(mdev, UI_HISTORY_START, mdev->ldev->md.uuid[UI_HISTORY_START + 1]);
2901
Philipp Reisner4a23f262011-01-11 17:42:17 +01002902 dev_info(DEV, "Last syncUUID did not get through, corrected:\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002903 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2904 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2905
2906 return 1;
2907 }
2908 }
2909
2910
2911 *rule_nr = 80;
Philipp Reisnerd8c2a362009-11-18 15:52:51 +01002912 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002913 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2914 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2915 if (self == peer)
2916 return 2;
2917 }
2918
2919 *rule_nr = 90;
2920 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2921 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2922 if (self == peer && self != ((u64)0))
2923 return 100;
2924
2925 *rule_nr = 100;
2926 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2927 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2928 for (j = UI_HISTORY_START; j <= UI_HISTORY_END; j++) {
2929 peer = mdev->p_uuid[j] & ~((u64)1);
2930 if (self == peer)
2931 return -100;
2932 }
2933 }
2934
2935 return -1000;
2936}
2937
2938/* drbd_sync_handshake() returns the new conn state on success, or
2939 CONN_MASK (-1) on failure.
2940 */
2941static enum drbd_conns drbd_sync_handshake(struct drbd_conf *mdev, enum drbd_role peer_role,
2942 enum drbd_disk_state peer_disk) __must_hold(local)
2943{
Philipp Reisnerb411b362009-09-25 16:07:19 -07002944 enum drbd_conns rv = C_MASK;
2945 enum drbd_disk_state mydisk;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002946 struct net_conf *nc;
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02002947 int hg, rule_nr, rr_conflict, tentative;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002948
2949 mydisk = mdev->state.disk;
2950 if (mydisk == D_NEGOTIATING)
2951 mydisk = mdev->new_state_tmp.disk;
2952
2953 dev_info(DEV, "drbd_sync_handshake:\n");
2954 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid, mdev->comm_bm_set, 0);
2955 drbd_uuid_dump(mdev, "peer", mdev->p_uuid,
2956 mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2957
2958 hg = drbd_uuid_compare(mdev, &rule_nr);
2959
2960 dev_info(DEV, "uuid_compare()=%d by rule %d\n", hg, rule_nr);
2961
2962 if (hg == -1000) {
2963 dev_alert(DEV, "Unrelated data, aborting!\n");
2964 return C_MASK;
2965 }
Philipp Reisner4a23f262011-01-11 17:42:17 +01002966 if (hg < -1000) {
2967 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 -07002968 return C_MASK;
2969 }
2970
2971 if ((mydisk == D_INCONSISTENT && peer_disk > D_INCONSISTENT) ||
2972 (peer_disk == D_INCONSISTENT && mydisk > D_INCONSISTENT)) {
2973 int f = (hg == -100) || abs(hg) == 2;
2974 hg = mydisk > D_INCONSISTENT ? 1 : -1;
2975 if (f)
2976 hg = hg*2;
2977 dev_info(DEV, "Becoming sync %s due to disk states.\n",
2978 hg > 0 ? "source" : "target");
2979 }
2980
Adam Gandelman3a11a482010-04-08 16:48:23 -07002981 if (abs(hg) == 100)
2982 drbd_khelper(mdev, "initial-split-brain");
2983
Philipp Reisner44ed1672011-04-19 17:10:19 +02002984 rcu_read_lock();
2985 nc = rcu_dereference(mdev->tconn->net_conf);
2986
2987 if (hg == 100 || (hg == -100 && nc->always_asbp)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002988 int pcount = (mdev->state.role == R_PRIMARY)
2989 + (peer_role == R_PRIMARY);
2990 int forced = (hg == -100);
2991
2992 switch (pcount) {
2993 case 0:
2994 hg = drbd_asb_recover_0p(mdev);
2995 break;
2996 case 1:
2997 hg = drbd_asb_recover_1p(mdev);
2998 break;
2999 case 2:
3000 hg = drbd_asb_recover_2p(mdev);
3001 break;
3002 }
3003 if (abs(hg) < 100) {
3004 dev_warn(DEV, "Split-Brain detected, %d primaries, "
3005 "automatically solved. Sync from %s node\n",
3006 pcount, (hg < 0) ? "peer" : "this");
3007 if (forced) {
3008 dev_warn(DEV, "Doing a full sync, since"
3009 " UUIDs where ambiguous.\n");
3010 hg = hg*2;
3011 }
3012 }
3013 }
3014
3015 if (hg == -100) {
Philipp Reisner08b165b2011-09-05 16:22:33 +02003016 if (test_bit(DISCARD_MY_DATA, &mdev->flags) && !(mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003017 hg = -1;
Philipp Reisner08b165b2011-09-05 16:22:33 +02003018 if (!test_bit(DISCARD_MY_DATA, &mdev->flags) && (mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003019 hg = 1;
3020
3021 if (abs(hg) < 100)
3022 dev_warn(DEV, "Split-Brain detected, manually solved. "
3023 "Sync from %s node\n",
3024 (hg < 0) ? "peer" : "this");
3025 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02003026 rr_conflict = nc->rr_conflict;
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02003027 tentative = nc->tentative;
Philipp Reisner44ed1672011-04-19 17:10:19 +02003028 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07003029
3030 if (hg == -100) {
Lars Ellenberg580b9762010-02-26 23:15:23 +01003031 /* FIXME this log message is not correct if we end up here
3032 * after an attempted attach on a diskless node.
3033 * We just refuse to attach -- well, we drop the "connection"
3034 * to that disk, in a way... */
Adam Gandelman3a11a482010-04-08 16:48:23 -07003035 dev_alert(DEV, "Split-Brain detected but unresolved, dropping connection!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003036 drbd_khelper(mdev, "split-brain");
3037 return C_MASK;
3038 }
3039
3040 if (hg > 0 && mydisk <= D_INCONSISTENT) {
3041 dev_err(DEV, "I shall become SyncSource, but I am inconsistent!\n");
3042 return C_MASK;
3043 }
3044
3045 if (hg < 0 && /* by intention we do not use mydisk here. */
3046 mdev->state.role == R_PRIMARY && mdev->state.disk >= D_CONSISTENT) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02003047 switch (rr_conflict) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003048 case ASB_CALL_HELPER:
3049 drbd_khelper(mdev, "pri-lost");
3050 /* fall through */
3051 case ASB_DISCONNECT:
3052 dev_err(DEV, "I shall become SyncTarget, but I am primary!\n");
3053 return C_MASK;
3054 case ASB_VIOLENTLY:
3055 dev_warn(DEV, "Becoming SyncTarget, violating the stable-data"
3056 "assumption\n");
3057 }
3058 }
3059
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02003060 if (tentative || test_bit(CONN_DRY_RUN, &mdev->tconn->flags)) {
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003061 if (hg == 0)
3062 dev_info(DEV, "dry-run connect: No resync, would become Connected immediately.\n");
3063 else
3064 dev_info(DEV, "dry-run connect: Would become %s, doing a %s resync.",
3065 drbd_conn_str(hg > 0 ? C_SYNC_SOURCE : C_SYNC_TARGET),
3066 abs(hg) >= 2 ? "full" : "bit-map based");
3067 return C_MASK;
3068 }
3069
Philipp Reisnerb411b362009-09-25 16:07:19 -07003070 if (abs(hg) >= 2) {
3071 dev_info(DEV, "Writing the whole bitmap, full sync required after drbd_sync_handshake.\n");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003072 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write, "set_n_write from sync_handshake",
3073 BM_LOCKED_SET_ALLOWED))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003074 return C_MASK;
3075 }
3076
3077 if (hg > 0) { /* become sync source. */
3078 rv = C_WF_BITMAP_S;
3079 } else if (hg < 0) { /* become sync target */
3080 rv = C_WF_BITMAP_T;
3081 } else {
3082 rv = C_CONNECTED;
3083 if (drbd_bm_total_weight(mdev)) {
3084 dev_info(DEV, "No resync, but %lu bits in bitmap!\n",
3085 drbd_bm_total_weight(mdev));
3086 }
3087 }
3088
3089 return rv;
3090}
3091
Philipp Reisnerf179d762011-05-16 17:31:47 +02003092static enum drbd_after_sb_p convert_after_sb(enum drbd_after_sb_p peer)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003093{
3094 /* ASB_DISCARD_REMOTE - ASB_DISCARD_LOCAL is valid */
Philipp Reisnerf179d762011-05-16 17:31:47 +02003095 if (peer == ASB_DISCARD_REMOTE)
3096 return ASB_DISCARD_LOCAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003097
3098 /* any other things with ASB_DISCARD_REMOTE or ASB_DISCARD_LOCAL are invalid */
Philipp Reisnerf179d762011-05-16 17:31:47 +02003099 if (peer == ASB_DISCARD_LOCAL)
3100 return ASB_DISCARD_REMOTE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003101
3102 /* everything else is valid if they are equal on both sides. */
Philipp Reisnerf179d762011-05-16 17:31:47 +02003103 return peer;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003104}
3105
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003106static int receive_protocol(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003107{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003108 struct p_protocol *p = pi->data;
Philipp Reisner036b17e2011-05-16 17:38:11 +02003109 enum drbd_after_sb_p p_after_sb_0p, p_after_sb_1p, p_after_sb_2p;
3110 int p_proto, p_discard_my_data, p_two_primaries, cf;
3111 struct net_conf *nc, *old_net_conf, *new_net_conf = NULL;
3112 char integrity_alg[SHARED_SECRET_MAX] = "";
Andreas Gruenbacheraccdbcc2011-07-15 17:41:09 +02003113 struct crypto_hash *peer_integrity_tfm = NULL;
Philipp Reisner7aca6c72011-05-17 10:12:56 +02003114 void *int_dig_in = NULL, *int_dig_vv = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003115
Philipp Reisnerb411b362009-09-25 16:07:19 -07003116 p_proto = be32_to_cpu(p->protocol);
3117 p_after_sb_0p = be32_to_cpu(p->after_sb_0p);
3118 p_after_sb_1p = be32_to_cpu(p->after_sb_1p);
3119 p_after_sb_2p = be32_to_cpu(p->after_sb_2p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003120 p_two_primaries = be32_to_cpu(p->two_primaries);
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003121 cf = be32_to_cpu(p->conn_flags);
Andreas Gruenbacher6139f602011-05-06 20:00:02 +02003122 p_discard_my_data = cf & CF_DISCARD_MY_DATA;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003123
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003124 if (tconn->agreed_pro_version >= 87) {
3125 int err;
3126
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02003127 if (pi->size > sizeof(integrity_alg))
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003128 return -EIO;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02003129 err = drbd_recv_all(tconn, integrity_alg, pi->size);
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003130 if (err)
3131 return err;
Philipp Reisner036b17e2011-05-16 17:38:11 +02003132 integrity_alg[SHARED_SECRET_MAX - 1] = 0;
3133 }
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003134
Andreas Gruenbacher7d4c7822011-07-17 23:06:12 +02003135 if (pi->cmd != P_PROTOCOL_UPDATE) {
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003136 clear_bit(CONN_DRY_RUN, &tconn->flags);
Philipp Reisner036b17e2011-05-16 17:38:11 +02003137
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003138 if (cf & CF_DRY_RUN)
3139 set_bit(CONN_DRY_RUN, &tconn->flags);
3140
3141 rcu_read_lock();
3142 nc = rcu_dereference(tconn->net_conf);
3143
3144 if (p_proto != nc->wire_protocol) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003145 conn_err(tconn, "incompatible %s settings\n", "protocol");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003146 goto disconnect_rcu_unlock;
3147 }
3148
3149 if (convert_after_sb(p_after_sb_0p) != nc->after_sb_0p) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003150 conn_err(tconn, "incompatible %s settings\n", "after-sb-0pri");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003151 goto disconnect_rcu_unlock;
3152 }
3153
3154 if (convert_after_sb(p_after_sb_1p) != nc->after_sb_1p) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003155 conn_err(tconn, "incompatible %s settings\n", "after-sb-1pri");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003156 goto disconnect_rcu_unlock;
3157 }
3158
3159 if (convert_after_sb(p_after_sb_2p) != nc->after_sb_2p) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003160 conn_err(tconn, "incompatible %s settings\n", "after-sb-2pri");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003161 goto disconnect_rcu_unlock;
3162 }
3163
3164 if (p_discard_my_data && nc->discard_my_data) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003165 conn_err(tconn, "incompatible %s settings\n", "discard-my-data");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003166 goto disconnect_rcu_unlock;
3167 }
3168
3169 if (p_two_primaries != nc->two_primaries) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003170 conn_err(tconn, "incompatible %s settings\n", "allow-two-primaries");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003171 goto disconnect_rcu_unlock;
3172 }
3173
3174 if (strcmp(integrity_alg, nc->integrity_alg)) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003175 conn_err(tconn, "incompatible %s settings\n", "data-integrity-alg");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003176 goto disconnect_rcu_unlock;
3177 }
3178
3179 rcu_read_unlock();
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003180 }
Andreas Gruenbacher7d4c7822011-07-17 23:06:12 +02003181
3182 if (integrity_alg[0]) {
3183 int hash_size;
3184
3185 /*
3186 * We can only change the peer data integrity algorithm
3187 * here. Changing our own data integrity algorithm
3188 * requires that we send a P_PROTOCOL_UPDATE packet at
3189 * the same time; otherwise, the peer has no way to
3190 * tell between which packets the algorithm should
3191 * change.
3192 */
3193
3194 peer_integrity_tfm = crypto_alloc_hash(integrity_alg, 0, CRYPTO_ALG_ASYNC);
3195 if (!peer_integrity_tfm) {
3196 conn_err(tconn, "peer data-integrity-alg %s not supported\n",
3197 integrity_alg);
3198 goto disconnect;
3199 }
3200
3201 hash_size = crypto_hash_digestsize(peer_integrity_tfm);
3202 int_dig_in = kmalloc(hash_size, GFP_KERNEL);
3203 int_dig_vv = kmalloc(hash_size, GFP_KERNEL);
3204 if (!(int_dig_in && int_dig_vv)) {
3205 conn_err(tconn, "Allocation of buffers for data integrity checking failed\n");
3206 goto disconnect;
3207 }
3208 }
3209
3210 new_net_conf = kmalloc(sizeof(struct net_conf), GFP_KERNEL);
3211 if (!new_net_conf) {
3212 conn_err(tconn, "Allocation of new net_conf failed\n");
3213 goto disconnect;
3214 }
3215
3216 mutex_lock(&tconn->data.mutex);
3217 mutex_lock(&tconn->conf_update);
3218 old_net_conf = tconn->net_conf;
3219 *new_net_conf = *old_net_conf;
3220
3221 new_net_conf->wire_protocol = p_proto;
3222 new_net_conf->after_sb_0p = convert_after_sb(p_after_sb_0p);
3223 new_net_conf->after_sb_1p = convert_after_sb(p_after_sb_1p);
3224 new_net_conf->after_sb_2p = convert_after_sb(p_after_sb_2p);
3225 new_net_conf->two_primaries = p_two_primaries;
3226
3227 rcu_assign_pointer(tconn->net_conf, new_net_conf);
3228 mutex_unlock(&tconn->conf_update);
3229 mutex_unlock(&tconn->data.mutex);
3230
3231 crypto_free_hash(tconn->peer_integrity_tfm);
3232 kfree(tconn->int_dig_in);
3233 kfree(tconn->int_dig_vv);
3234 tconn->peer_integrity_tfm = peer_integrity_tfm;
3235 tconn->int_dig_in = int_dig_in;
3236 tconn->int_dig_vv = int_dig_vv;
3237
3238 if (strcmp(old_net_conf->integrity_alg, integrity_alg))
3239 conn_info(tconn, "peer data-integrity-alg: %s\n",
3240 integrity_alg[0] ? integrity_alg : "(none)");
3241
3242 synchronize_rcu();
3243 kfree(old_net_conf);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003244 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003245
Philipp Reisner44ed1672011-04-19 17:10:19 +02003246disconnect_rcu_unlock:
3247 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07003248disconnect:
Andreas Gruenbacherb792c352011-07-15 16:48:49 +02003249 crypto_free_hash(peer_integrity_tfm);
Philipp Reisner036b17e2011-05-16 17:38:11 +02003250 kfree(int_dig_in);
3251 kfree(int_dig_vv);
Philipp Reisner72046242011-03-15 18:51:47 +01003252 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003253 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003254}
3255
3256/* helper function
3257 * input: alg name, feature name
3258 * return: NULL (alg name was "")
3259 * ERR_PTR(error) if something goes wrong
3260 * or the crypto hash ptr, if it worked out ok. */
3261struct crypto_hash *drbd_crypto_alloc_digest_safe(const struct drbd_conf *mdev,
3262 const char *alg, const char *name)
3263{
3264 struct crypto_hash *tfm;
3265
3266 if (!alg[0])
3267 return NULL;
3268
3269 tfm = crypto_alloc_hash(alg, 0, CRYPTO_ALG_ASYNC);
3270 if (IS_ERR(tfm)) {
3271 dev_err(DEV, "Can not allocate \"%s\" as %s (reason: %ld)\n",
3272 alg, name, PTR_ERR(tfm));
3273 return tfm;
3274 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003275 return tfm;
3276}
3277
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003278static int ignore_remaining_packet(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003279{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003280 void *buffer = tconn->data.rbuf;
3281 int size = pi->size;
3282
3283 while (size) {
3284 int s = min_t(int, size, DRBD_SOCKET_BUFFER_SIZE);
3285 s = drbd_recv(tconn, buffer, s);
3286 if (s <= 0) {
3287 if (s < 0)
3288 return s;
3289 break;
3290 }
3291 size -= s;
3292 }
3293 if (size)
3294 return -EIO;
3295 return 0;
3296}
3297
3298/*
3299 * config_unknown_volume - device configuration command for unknown volume
3300 *
3301 * When a device is added to an existing connection, the node on which the
3302 * device is added first will send configuration commands to its peer but the
3303 * peer will not know about the device yet. It will warn and ignore these
3304 * commands. Once the device is added on the second node, the second node will
3305 * send the same device configuration commands, but in the other direction.
3306 *
3307 * (We can also end up here if drbd is misconfigured.)
3308 */
3309static int config_unknown_volume(struct drbd_tconn *tconn, struct packet_info *pi)
3310{
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02003311 conn_warn(tconn, "%s packet received for volume %u, which is not configured locally\n",
3312 cmdname(pi->cmd), pi->vnr);
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003313 return ignore_remaining_packet(tconn, pi);
3314}
3315
3316static int receive_SyncParam(struct drbd_tconn *tconn, struct packet_info *pi)
3317{
3318 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003319 struct p_rs_param_95 *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003320 unsigned int header_size, data_size, exp_max_sz;
3321 struct crypto_hash *verify_tfm = NULL;
3322 struct crypto_hash *csums_tfm = NULL;
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003323 struct net_conf *old_net_conf, *new_net_conf = NULL;
Philipp Reisner813472c2011-05-03 16:47:02 +02003324 struct disk_conf *old_disk_conf = NULL, *new_disk_conf = NULL;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003325 const int apv = tconn->agreed_pro_version;
Philipp Reisner813472c2011-05-03 16:47:02 +02003326 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
Philipp Reisner778f2712010-07-06 11:14:00 +02003327 int fifo_size = 0;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003328 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003329
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003330 mdev = vnr_to_mdev(tconn, pi->vnr);
3331 if (!mdev)
3332 return config_unknown_volume(tconn, pi);
3333
Philipp Reisnerb411b362009-09-25 16:07:19 -07003334 exp_max_sz = apv <= 87 ? sizeof(struct p_rs_param)
3335 : apv == 88 ? sizeof(struct p_rs_param)
3336 + SHARED_SECRET_MAX
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003337 : apv <= 94 ? sizeof(struct p_rs_param_89)
3338 : /* apv >= 95 */ sizeof(struct p_rs_param_95);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003339
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003340 if (pi->size > exp_max_sz) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003341 dev_err(DEV, "SyncParam packet too long: received %u, expected <= %u bytes\n",
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003342 pi->size, exp_max_sz);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003343 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003344 }
3345
3346 if (apv <= 88) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003347 header_size = sizeof(struct p_rs_param);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003348 data_size = pi->size - header_size;
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003349 } else if (apv <= 94) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003350 header_size = sizeof(struct p_rs_param_89);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003351 data_size = pi->size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003352 D_ASSERT(data_size == 0);
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003353 } else {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003354 header_size = sizeof(struct p_rs_param_95);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003355 data_size = pi->size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003356 D_ASSERT(data_size == 0);
3357 }
3358
3359 /* initialize verify_alg and csums_alg */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003360 p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003361 memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
3362
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003363 err = drbd_recv_all(mdev->tconn, p, header_size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003364 if (err)
3365 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003366
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003367 mutex_lock(&mdev->tconn->conf_update);
3368 old_net_conf = mdev->tconn->net_conf;
Philipp Reisner813472c2011-05-03 16:47:02 +02003369 if (get_ldev(mdev)) {
3370 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
3371 if (!new_disk_conf) {
3372 put_ldev(mdev);
3373 mutex_unlock(&mdev->tconn->conf_update);
3374 dev_err(DEV, "Allocation of new disk_conf failed\n");
3375 return -ENOMEM;
3376 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003377
Philipp Reisner813472c2011-05-03 16:47:02 +02003378 old_disk_conf = mdev->ldev->disk_conf;
3379 *new_disk_conf = *old_disk_conf;
3380
Andreas Gruenbacher6394b932011-05-11 14:29:52 +02003381 new_disk_conf->resync_rate = be32_to_cpu(p->resync_rate);
Philipp Reisner813472c2011-05-03 16:47:02 +02003382 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003383
Philipp Reisnerb411b362009-09-25 16:07:19 -07003384 if (apv >= 88) {
3385 if (apv == 88) {
Philipp Reisnere4bad1b2012-04-06 12:08:51 +02003386 if (data_size > SHARED_SECRET_MAX || data_size == 0) {
3387 dev_err(DEV, "verify-alg of wrong size, "
3388 "peer wants %u, accepting only up to %u byte\n",
3389 data_size, SHARED_SECRET_MAX);
Philipp Reisner813472c2011-05-03 16:47:02 +02003390 err = -EIO;
3391 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003392 }
3393
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003394 err = drbd_recv_all(mdev->tconn, p->verify_alg, data_size);
Philipp Reisner813472c2011-05-03 16:47:02 +02003395 if (err)
3396 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003397 /* we expect NUL terminated string */
3398 /* but just in case someone tries to be evil */
3399 D_ASSERT(p->verify_alg[data_size-1] == 0);
3400 p->verify_alg[data_size-1] = 0;
3401
3402 } else /* apv >= 89 */ {
3403 /* we still expect NUL terminated strings */
3404 /* but just in case someone tries to be evil */
3405 D_ASSERT(p->verify_alg[SHARED_SECRET_MAX-1] == 0);
3406 D_ASSERT(p->csums_alg[SHARED_SECRET_MAX-1] == 0);
3407 p->verify_alg[SHARED_SECRET_MAX-1] = 0;
3408 p->csums_alg[SHARED_SECRET_MAX-1] = 0;
3409 }
3410
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003411 if (strcmp(old_net_conf->verify_alg, p->verify_alg)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003412 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
3413 dev_err(DEV, "Different verify-alg settings. me=\"%s\" peer=\"%s\"\n",
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003414 old_net_conf->verify_alg, p->verify_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003415 goto disconnect;
3416 }
3417 verify_tfm = drbd_crypto_alloc_digest_safe(mdev,
3418 p->verify_alg, "verify-alg");
3419 if (IS_ERR(verify_tfm)) {
3420 verify_tfm = NULL;
3421 goto disconnect;
3422 }
3423 }
3424
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003425 if (apv >= 89 && strcmp(old_net_conf->csums_alg, p->csums_alg)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003426 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
3427 dev_err(DEV, "Different csums-alg settings. me=\"%s\" peer=\"%s\"\n",
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003428 old_net_conf->csums_alg, p->csums_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003429 goto disconnect;
3430 }
3431 csums_tfm = drbd_crypto_alloc_digest_safe(mdev,
3432 p->csums_alg, "csums-alg");
3433 if (IS_ERR(csums_tfm)) {
3434 csums_tfm = NULL;
3435 goto disconnect;
3436 }
3437 }
3438
Philipp Reisner813472c2011-05-03 16:47:02 +02003439 if (apv > 94 && new_disk_conf) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003440 new_disk_conf->c_plan_ahead = be32_to_cpu(p->c_plan_ahead);
3441 new_disk_conf->c_delay_target = be32_to_cpu(p->c_delay_target);
3442 new_disk_conf->c_fill_target = be32_to_cpu(p->c_fill_target);
3443 new_disk_conf->c_max_rate = be32_to_cpu(p->c_max_rate);
Philipp Reisner778f2712010-07-06 11:14:00 +02003444
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003445 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
Philipp Reisner9958c852011-05-03 16:19:31 +02003446 if (fifo_size != mdev->rs_plan_s->size) {
Philipp Reisner813472c2011-05-03 16:47:02 +02003447 new_plan = fifo_alloc(fifo_size);
3448 if (!new_plan) {
Philipp Reisner778f2712010-07-06 11:14:00 +02003449 dev_err(DEV, "kmalloc of fifo_buffer failed");
Lars Ellenbergf3990022011-03-23 14:31:09 +01003450 put_ldev(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02003451 goto disconnect;
3452 }
3453 }
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003454 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003455
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003456 if (verify_tfm || csums_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003457 new_net_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
3458 if (!new_net_conf) {
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003459 dev_err(DEV, "Allocation of new net_conf failed\n");
3460 goto disconnect;
3461 }
3462
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003463 *new_net_conf = *old_net_conf;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003464
3465 if (verify_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003466 strcpy(new_net_conf->verify_alg, p->verify_alg);
3467 new_net_conf->verify_alg_len = strlen(p->verify_alg) + 1;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003468 crypto_free_hash(mdev->tconn->verify_tfm);
3469 mdev->tconn->verify_tfm = verify_tfm;
3470 dev_info(DEV, "using verify-alg: \"%s\"\n", p->verify_alg);
3471 }
3472 if (csums_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003473 strcpy(new_net_conf->csums_alg, p->csums_alg);
3474 new_net_conf->csums_alg_len = strlen(p->csums_alg) + 1;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003475 crypto_free_hash(mdev->tconn->csums_tfm);
3476 mdev->tconn->csums_tfm = csums_tfm;
3477 dev_info(DEV, "using csums-alg: \"%s\"\n", p->csums_alg);
3478 }
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003479 rcu_assign_pointer(tconn->net_conf, new_net_conf);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003480 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003481 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003482
Philipp Reisner813472c2011-05-03 16:47:02 +02003483 if (new_disk_conf) {
3484 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
3485 put_ldev(mdev);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003486 }
Philipp Reisner813472c2011-05-03 16:47:02 +02003487
3488 if (new_plan) {
3489 old_plan = mdev->rs_plan_s;
3490 rcu_assign_pointer(mdev->rs_plan_s, new_plan);
3491 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003492
3493 mutex_unlock(&mdev->tconn->conf_update);
3494 synchronize_rcu();
3495 if (new_net_conf)
3496 kfree(old_net_conf);
3497 kfree(old_disk_conf);
Philipp Reisner813472c2011-05-03 16:47:02 +02003498 kfree(old_plan);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003499
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003500 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003501
Philipp Reisner813472c2011-05-03 16:47:02 +02003502reconnect:
3503 if (new_disk_conf) {
3504 put_ldev(mdev);
3505 kfree(new_disk_conf);
3506 }
3507 mutex_unlock(&mdev->tconn->conf_update);
3508 return -EIO;
3509
Philipp Reisnerb411b362009-09-25 16:07:19 -07003510disconnect:
Philipp Reisner813472c2011-05-03 16:47:02 +02003511 kfree(new_plan);
3512 if (new_disk_conf) {
3513 put_ldev(mdev);
3514 kfree(new_disk_conf);
3515 }
Philipp Reisnera0095502011-05-03 13:14:15 +02003516 mutex_unlock(&mdev->tconn->conf_update);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003517 /* just for completeness: actually not needed,
3518 * as this is not reached if csums_tfm was ok. */
3519 crypto_free_hash(csums_tfm);
3520 /* but free the verify_tfm again, if csums_tfm did not work out */
3521 crypto_free_hash(verify_tfm);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003522 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003523 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003524}
3525
Philipp Reisnerb411b362009-09-25 16:07:19 -07003526/* warn if the arguments differ by more than 12.5% */
3527static void warn_if_differ_considerably(struct drbd_conf *mdev,
3528 const char *s, sector_t a, sector_t b)
3529{
3530 sector_t d;
3531 if (a == 0 || b == 0)
3532 return;
3533 d = (a > b) ? (a - b) : (b - a);
3534 if (d > (a>>3) || d > (b>>3))
3535 dev_warn(DEV, "Considerable difference in %s: %llus vs. %llus\n", s,
3536 (unsigned long long)a, (unsigned long long)b);
3537}
3538
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003539static int receive_sizes(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003540{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003541 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003542 struct p_sizes *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003543 enum determine_dev_size dd = unchanged;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003544 sector_t p_size, p_usize, my_usize;
3545 int ldsc = 0; /* local disk size changed */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003546 enum dds_flags ddsf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003547
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003548 mdev = vnr_to_mdev(tconn, pi->vnr);
3549 if (!mdev)
3550 return config_unknown_volume(tconn, pi);
3551
Philipp Reisnerb411b362009-09-25 16:07:19 -07003552 p_size = be64_to_cpu(p->d_size);
3553 p_usize = be64_to_cpu(p->u_size);
3554
Philipp Reisnerb411b362009-09-25 16:07:19 -07003555 /* just store the peer's disk size for now.
3556 * we still need to figure out whether we accept that. */
3557 mdev->p_size = p_size;
3558
Philipp Reisnerb411b362009-09-25 16:07:19 -07003559 if (get_ldev(mdev)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003560 rcu_read_lock();
3561 my_usize = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
3562 rcu_read_unlock();
3563
Philipp Reisnerb411b362009-09-25 16:07:19 -07003564 warn_if_differ_considerably(mdev, "lower level device sizes",
3565 p_size, drbd_get_max_capacity(mdev->ldev));
3566 warn_if_differ_considerably(mdev, "user requested size",
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003567 p_usize, my_usize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003568
3569 /* if this is the first connect, or an otherwise expected
3570 * param exchange, choose the minimum */
3571 if (mdev->state.conn == C_WF_REPORT_PARAMS)
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003572 p_usize = min_not_zero(my_usize, p_usize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003573
3574 /* Never shrink a device with usable data during connect.
3575 But allow online shrinking if we are connected. */
Philipp Reisneref5e44a2011-05-03 13:27:43 +02003576 if (drbd_new_dev_size(mdev, mdev->ldev, p_usize, 0) <
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003577 drbd_get_capacity(mdev->this_bdev) &&
3578 mdev->state.disk >= D_OUTDATED &&
3579 mdev->state.conn < C_CONNECTED) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003580 dev_err(DEV, "The peer's disk size is too small!\n");
Philipp Reisner38fa9982011-03-15 18:24:49 +01003581 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003582 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003583 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003584 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003585
3586 if (my_usize != p_usize) {
3587 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
3588
3589 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
3590 if (!new_disk_conf) {
3591 dev_err(DEV, "Allocation of new disk_conf failed\n");
3592 put_ldev(mdev);
3593 return -ENOMEM;
3594 }
3595
3596 mutex_lock(&mdev->tconn->conf_update);
3597 old_disk_conf = mdev->ldev->disk_conf;
3598 *new_disk_conf = *old_disk_conf;
3599 new_disk_conf->disk_size = p_usize;
3600
3601 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
3602 mutex_unlock(&mdev->tconn->conf_update);
3603 synchronize_rcu();
3604 kfree(old_disk_conf);
3605
3606 dev_info(DEV, "Peer sets u_size to %lu sectors\n",
3607 (unsigned long)my_usize);
3608 }
3609
Philipp Reisnerb411b362009-09-25 16:07:19 -07003610 put_ldev(mdev);
3611 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003612
Philipp Reisnere89b5912010-03-24 17:11:33 +01003613 ddsf = be16_to_cpu(p->dds_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003614 if (get_ldev(mdev)) {
Bart Van Assche24c48302011-05-21 18:32:29 +02003615 dd = drbd_determine_dev_size(mdev, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003616 put_ldev(mdev);
3617 if (dd == dev_size_error)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003618 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003619 drbd_md_sync(mdev);
3620 } else {
3621 /* I am diskless, need to accept the peer's size. */
3622 drbd_set_my_capacity(mdev, p_size);
3623 }
3624
Philipp Reisner99432fc2011-05-20 16:39:13 +02003625 mdev->peer_max_bio_size = be32_to_cpu(p->max_bio_size);
3626 drbd_reconsider_max_bio_size(mdev);
3627
Philipp Reisnerb411b362009-09-25 16:07:19 -07003628 if (get_ldev(mdev)) {
3629 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev)) {
3630 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
3631 ldsc = 1;
3632 }
3633
Philipp Reisnerb411b362009-09-25 16:07:19 -07003634 put_ldev(mdev);
3635 }
3636
3637 if (mdev->state.conn > C_WF_REPORT_PARAMS) {
3638 if (be64_to_cpu(p->c_size) !=
3639 drbd_get_capacity(mdev->this_bdev) || ldsc) {
3640 /* we have different sizes, probably peer
3641 * needs to know my new size... */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003642 drbd_send_sizes(mdev, 0, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003643 }
3644 if (test_and_clear_bit(RESIZE_PENDING, &mdev->flags) ||
3645 (dd == grew && mdev->state.conn == C_CONNECTED)) {
3646 if (mdev->state.pdsk >= D_INCONSISTENT &&
Philipp Reisnere89b5912010-03-24 17:11:33 +01003647 mdev->state.disk >= D_INCONSISTENT) {
3648 if (ddsf & DDSF_NO_RESYNC)
3649 dev_info(DEV, "Resync of new storage suppressed with --assume-clean\n");
3650 else
3651 resync_after_online_grow(mdev);
3652 } else
Philipp Reisnerb411b362009-09-25 16:07:19 -07003653 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
3654 }
3655 }
3656
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003657 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003658}
3659
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003660static int receive_uuids(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003661{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003662 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003663 struct p_uuids *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003664 u64 *p_uuid;
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003665 int i, updated_uuids = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003666
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003667 mdev = vnr_to_mdev(tconn, pi->vnr);
3668 if (!mdev)
3669 return config_unknown_volume(tconn, pi);
3670
Philipp Reisnerb411b362009-09-25 16:07:19 -07003671 p_uuid = kmalloc(sizeof(u64)*UI_EXTENDED_SIZE, GFP_NOIO);
3672
3673 for (i = UI_CURRENT; i < UI_EXTENDED_SIZE; i++)
3674 p_uuid[i] = be64_to_cpu(p->uuid[i]);
3675
3676 kfree(mdev->p_uuid);
3677 mdev->p_uuid = p_uuid;
3678
3679 if (mdev->state.conn < C_CONNECTED &&
3680 mdev->state.disk < D_INCONSISTENT &&
3681 mdev->state.role == R_PRIMARY &&
3682 (mdev->ed_uuid & ~((u64)1)) != (p_uuid[UI_CURRENT] & ~((u64)1))) {
3683 dev_err(DEV, "Can only connect to data with current UUID=%016llX\n",
3684 (unsigned long long)mdev->ed_uuid);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003685 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003686 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003687 }
3688
3689 if (get_ldev(mdev)) {
3690 int skip_initial_sync =
3691 mdev->state.conn == C_CONNECTED &&
Philipp Reisner31890f42011-01-19 14:12:51 +01003692 mdev->tconn->agreed_pro_version >= 90 &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003693 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED &&
3694 (p_uuid[UI_FLAGS] & 8);
3695 if (skip_initial_sync) {
3696 dev_info(DEV, "Accepted new current UUID, preparing to skip initial sync\n");
3697 drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003698 "clear_n_write from receive_uuids",
3699 BM_LOCKED_TEST_ALLOWED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003700 _drbd_uuid_set(mdev, UI_CURRENT, p_uuid[UI_CURRENT]);
3701 _drbd_uuid_set(mdev, UI_BITMAP, 0);
3702 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
3703 CS_VERBOSE, NULL);
3704 drbd_md_sync(mdev);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003705 updated_uuids = 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003706 }
3707 put_ldev(mdev);
Philipp Reisner18a50fa2010-06-21 14:14:15 +02003708 } else if (mdev->state.disk < D_INCONSISTENT &&
3709 mdev->state.role == R_PRIMARY) {
3710 /* I am a diskless primary, the peer just created a new current UUID
3711 for me. */
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003712 updated_uuids = drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003713 }
3714
3715 /* Before we test for the disk state, we should wait until an eventually
3716 ongoing cluster wide state change is finished. That is important if
3717 we are primary and are detaching from our disk. We need to see the
3718 new disk state... */
Philipp Reisner8410da82011-02-11 20:11:10 +01003719 mutex_lock(mdev->state_mutex);
3720 mutex_unlock(mdev->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003721 if (mdev->state.conn >= C_CONNECTED && mdev->state.disk < D_INCONSISTENT)
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003722 updated_uuids |= drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
3723
3724 if (updated_uuids)
3725 drbd_print_uuids(mdev, "receiver updated UUIDs to");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003726
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003727 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003728}
3729
3730/**
3731 * convert_state() - Converts the peer's view of the cluster state to our point of view
3732 * @ps: The state as seen by the peer.
3733 */
3734static union drbd_state convert_state(union drbd_state ps)
3735{
3736 union drbd_state ms;
3737
3738 static enum drbd_conns c_tab[] = {
Philipp Reisner369bea62011-07-06 23:04:44 +02003739 [C_WF_REPORT_PARAMS] = C_WF_REPORT_PARAMS,
Philipp Reisnerb411b362009-09-25 16:07:19 -07003740 [C_CONNECTED] = C_CONNECTED,
3741
3742 [C_STARTING_SYNC_S] = C_STARTING_SYNC_T,
3743 [C_STARTING_SYNC_T] = C_STARTING_SYNC_S,
3744 [C_DISCONNECTING] = C_TEAR_DOWN, /* C_NETWORK_FAILURE, */
3745 [C_VERIFY_S] = C_VERIFY_T,
3746 [C_MASK] = C_MASK,
3747 };
3748
3749 ms.i = ps.i;
3750
3751 ms.conn = c_tab[ps.conn];
3752 ms.peer = ps.role;
3753 ms.role = ps.peer;
3754 ms.pdsk = ps.disk;
3755 ms.disk = ps.pdsk;
3756 ms.peer_isp = (ps.aftr_isp | ps.user_isp);
3757
3758 return ms;
3759}
3760
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003761static int receive_req_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003762{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003763 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003764 struct p_req_state *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003765 union drbd_state mask, val;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01003766 enum drbd_state_rv rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003767
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003768 mdev = vnr_to_mdev(tconn, pi->vnr);
3769 if (!mdev)
3770 return -EIO;
3771
Philipp Reisnerb411b362009-09-25 16:07:19 -07003772 mask.i = be32_to_cpu(p->mask);
3773 val.i = be32_to_cpu(p->val);
3774
Philipp Reisner25703f82011-02-07 14:35:25 +01003775 if (test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags) &&
Philipp Reisner8410da82011-02-11 20:11:10 +01003776 mutex_is_locked(mdev->state_mutex)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003777 drbd_send_sr_reply(mdev, SS_CONCURRENT_ST_CHG);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003778 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003779 }
3780
3781 mask = convert_state(mask);
3782 val = convert_state(val);
3783
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003784 rv = drbd_change_state(mdev, CS_VERBOSE, mask, val);
3785 drbd_send_sr_reply(mdev, rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003786
Philipp Reisnerb411b362009-09-25 16:07:19 -07003787 drbd_md_sync(mdev);
3788
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003789 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003790}
3791
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003792static int receive_req_conn_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003793{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003794 struct p_req_state *p = pi->data;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003795 union drbd_state mask, val;
3796 enum drbd_state_rv rv;
3797
3798 mask.i = be32_to_cpu(p->mask);
3799 val.i = be32_to_cpu(p->val);
3800
3801 if (test_bit(DISCARD_CONCURRENT, &tconn->flags) &&
3802 mutex_is_locked(&tconn->cstate_mutex)) {
3803 conn_send_sr_reply(tconn, SS_CONCURRENT_ST_CHG);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003804 return 0;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003805 }
3806
3807 mask = convert_state(mask);
3808 val = convert_state(val);
3809
Philipp Reisner778bcf22011-03-28 12:55:03 +02003810 rv = conn_request_state(tconn, mask, val, CS_VERBOSE | CS_LOCAL_ONLY | CS_IGN_OUTD_FAIL);
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003811 conn_send_sr_reply(tconn, rv);
3812
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003813 return 0;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003814}
3815
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003816static int receive_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003817{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003818 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003819 struct p_state *p = pi->data;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003820 union drbd_state os, ns, peer_state;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003821 enum drbd_disk_state real_peer_disk;
Philipp Reisner65d922c2010-06-16 16:18:09 +02003822 enum chg_state_flags cs_flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003823 int rv;
3824
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003825 mdev = vnr_to_mdev(tconn, pi->vnr);
3826 if (!mdev)
3827 return config_unknown_volume(tconn, pi);
3828
Philipp Reisnerb411b362009-09-25 16:07:19 -07003829 peer_state.i = be32_to_cpu(p->state);
3830
3831 real_peer_disk = peer_state.disk;
3832 if (peer_state.disk == D_NEGOTIATING) {
3833 real_peer_disk = mdev->p_uuid[UI_FLAGS] & 4 ? D_INCONSISTENT : D_CONSISTENT;
3834 dev_info(DEV, "real peer disk state = %s\n", drbd_disk_str(real_peer_disk));
3835 }
3836
Philipp Reisner87eeee42011-01-19 14:16:30 +01003837 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003838 retry:
Philipp Reisner78bae592011-03-28 15:40:12 +02003839 os = ns = drbd_read_state(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01003840 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003841
Philipp Reisnerb8853db2011-12-13 11:09:16 +01003842 /* If some other part of the code (asender thread, timeout)
3843 * already decided to close the connection again,
3844 * we must not "re-establish" it here. */
3845 if (os.conn <= C_TEAR_DOWN)
3846 return false;
3847
Philipp Reisner9bcd2522011-09-29 13:00:14 +02003848 /* If this is the "end of sync" confirmation, usually the peer disk
3849 * transitions from D_INCONSISTENT to D_UP_TO_DATE. For empty (0 bits
3850 * set) resync started in PausedSyncT, or if the timing of pause-/
3851 * unpause-sync events has been "just right", the peer disk may
3852 * transition from D_CONSISTENT to D_UP_TO_DATE as well.
3853 */
3854 if ((os.pdsk == D_INCONSISTENT || os.pdsk == D_CONSISTENT) &&
3855 real_peer_disk == D_UP_TO_DATE &&
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003856 os.conn > C_CONNECTED && os.disk == D_UP_TO_DATE) {
3857 /* If we are (becoming) SyncSource, but peer is still in sync
3858 * preparation, ignore its uptodate-ness to avoid flapping, it
3859 * will change to inconsistent once the peer reaches active
3860 * syncing states.
3861 * It may have changed syncer-paused flags, however, so we
3862 * cannot ignore this completely. */
3863 if (peer_state.conn > C_CONNECTED &&
3864 peer_state.conn < C_SYNC_SOURCE)
3865 real_peer_disk = D_INCONSISTENT;
3866
3867 /* if peer_state changes to connected at the same time,
3868 * it explicitly notifies us that it finished resync.
3869 * Maybe we should finish it up, too? */
3870 else if (os.conn >= C_SYNC_SOURCE &&
3871 peer_state.conn == C_CONNECTED) {
3872 if (drbd_bm_total_weight(mdev) <= mdev->rs_failed)
3873 drbd_resync_finished(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003874 return 0;
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003875 }
3876 }
3877
3878 /* peer says his disk is inconsistent, while we think it is uptodate,
3879 * and this happens while the peer still thinks we have a sync going on,
3880 * but we think we are already done with the sync.
3881 * We ignore this to avoid flapping pdsk.
3882 * This should not happen, if the peer is a recent version of drbd. */
3883 if (os.pdsk == D_UP_TO_DATE && real_peer_disk == D_INCONSISTENT &&
3884 os.conn == C_CONNECTED && peer_state.conn > C_SYNC_SOURCE)
3885 real_peer_disk = D_UP_TO_DATE;
3886
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003887 if (ns.conn == C_WF_REPORT_PARAMS)
3888 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003889
Philipp Reisner67531712010-10-27 12:21:30 +02003890 if (peer_state.conn == C_AHEAD)
3891 ns.conn = C_BEHIND;
3892
Philipp Reisnerb411b362009-09-25 16:07:19 -07003893 if (mdev->p_uuid && peer_state.disk >= D_NEGOTIATING &&
3894 get_ldev_if_state(mdev, D_NEGOTIATING)) {
3895 int cr; /* consider resync */
3896
3897 /* if we established a new connection */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003898 cr = (os.conn < C_CONNECTED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003899 /* if we had an established connection
3900 * and one of the nodes newly attaches a disk */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003901 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003902 (peer_state.disk == D_NEGOTIATING ||
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003903 os.disk == D_NEGOTIATING));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003904 /* if we have both been inconsistent, and the peer has been
3905 * forced to be UpToDate with --overwrite-data */
3906 cr |= test_bit(CONSIDER_RESYNC, &mdev->flags);
3907 /* if we had been plain connected, and the admin requested to
3908 * start a sync by "invalidate" or "invalidate-remote" */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003909 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003910 (peer_state.conn >= C_STARTING_SYNC_S &&
3911 peer_state.conn <= C_WF_BITMAP_T));
3912
3913 if (cr)
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003914 ns.conn = drbd_sync_handshake(mdev, peer_state.role, real_peer_disk);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003915
3916 put_ldev(mdev);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003917 if (ns.conn == C_MASK) {
3918 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003919 if (mdev->state.disk == D_NEGOTIATING) {
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02003920 drbd_force_state(mdev, NS(disk, D_FAILED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003921 } else if (peer_state.disk == D_NEGOTIATING) {
3922 dev_err(DEV, "Disk attach process on the peer node was aborted.\n");
3923 peer_state.disk = D_DISKLESS;
Lars Ellenberg580b9762010-02-26 23:15:23 +01003924 real_peer_disk = D_DISKLESS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003925 } else {
Philipp Reisner8169e412011-03-15 18:40:27 +01003926 if (test_and_clear_bit(CONN_DRY_RUN, &mdev->tconn->flags))
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003927 return -EIO;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003928 D_ASSERT(os.conn == C_WF_REPORT_PARAMS);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003929 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003930 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003931 }
3932 }
3933 }
3934
Philipp Reisner87eeee42011-01-19 14:16:30 +01003935 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner78bae592011-03-28 15:40:12 +02003936 if (os.i != drbd_read_state(mdev).i)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003937 goto retry;
3938 clear_bit(CONSIDER_RESYNC, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003939 ns.peer = peer_state.role;
3940 ns.pdsk = real_peer_disk;
3941 ns.peer_isp = (peer_state.aftr_isp | peer_state.user_isp);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003942 if ((ns.conn == C_CONNECTED || ns.conn == C_WF_BITMAP_S) && ns.disk == D_NEGOTIATING)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003943 ns.disk = mdev->new_state_tmp.disk;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003944 cs_flags = CS_VERBOSE + (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED ? 0 : CS_HARD);
Philipp Reisner2aebfab2011-03-28 16:48:11 +02003945 if (ns.pdsk == D_CONSISTENT && drbd_suspended(mdev) && ns.conn == C_CONNECTED && os.conn < C_CONNECTED &&
Philipp Reisner481c6f52010-06-22 14:03:27 +02003946 test_bit(NEW_CUR_UUID, &mdev->flags)) {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01003947 /* Do not allow tl_restart(RESEND) for a rebooted peer. We can only allow this
Philipp Reisner481c6f52010-06-22 14:03:27 +02003948 for temporal network outages! */
Philipp Reisner87eeee42011-01-19 14:16:30 +01003949 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003950 dev_err(DEV, "Aborting Connect, can not thaw IO with an only Consistent peer\n");
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01003951 tl_clear(mdev->tconn);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003952 drbd_uuid_new_current(mdev);
3953 clear_bit(NEW_CUR_UUID, &mdev->flags);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003954 conn_request_state(mdev->tconn, NS2(conn, C_PROTOCOL_ERROR, susp, 0), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003955 return -EIO;
Philipp Reisner481c6f52010-06-22 14:03:27 +02003956 }
Philipp Reisner65d922c2010-06-16 16:18:09 +02003957 rv = _drbd_set_state(mdev, ns, cs_flags, NULL);
Philipp Reisner78bae592011-03-28 15:40:12 +02003958 ns = drbd_read_state(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01003959 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003960
3961 if (rv < SS_SUCCESS) {
Philipp Reisner38fa9982011-03-15 18:24:49 +01003962 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003963 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003964 }
3965
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003966 if (os.conn > C_WF_REPORT_PARAMS) {
3967 if (ns.conn > C_CONNECTED && peer_state.conn <= C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003968 peer_state.disk != D_NEGOTIATING ) {
3969 /* we want resync, peer has not yet decided to sync... */
3970 /* Nowadays only used when forcing a node into primary role and
3971 setting its disk to UpToDate with that */
3972 drbd_send_uuids(mdev);
Philipp Reisner43de7c82011-11-10 13:16:13 +01003973 drbd_send_current_state(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003974 }
3975 }
3976
Philipp Reisner08b165b2011-09-05 16:22:33 +02003977 clear_bit(DISCARD_MY_DATA, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003978
3979 drbd_md_sync(mdev); /* update connected indicator, la_size, ... */
3980
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003981 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003982}
3983
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003984static int receive_sync_uuid(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003985{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003986 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003987 struct p_rs_uuid *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003988
3989 mdev = vnr_to_mdev(tconn, pi->vnr);
3990 if (!mdev)
3991 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003992
3993 wait_event(mdev->misc_wait,
3994 mdev->state.conn == C_WF_SYNC_UUID ||
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02003995 mdev->state.conn == C_BEHIND ||
Philipp Reisnerb411b362009-09-25 16:07:19 -07003996 mdev->state.conn < C_CONNECTED ||
3997 mdev->state.disk < D_NEGOTIATING);
3998
3999 /* D_ASSERT( mdev->state.conn == C_WF_SYNC_UUID ); */
4000
Philipp Reisnerb411b362009-09-25 16:07:19 -07004001 /* Here the _drbd_uuid_ functions are right, current should
4002 _not_ be rotated into the history */
4003 if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
4004 _drbd_uuid_set(mdev, UI_CURRENT, be64_to_cpu(p->uuid));
4005 _drbd_uuid_set(mdev, UI_BITMAP, 0UL);
4006
Lars Ellenberg62b0da32011-01-20 13:25:21 +01004007 drbd_print_uuids(mdev, "updated sync uuid");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004008 drbd_start_resync(mdev, C_SYNC_TARGET);
4009
4010 put_ldev(mdev);
4011 } else
4012 dev_err(DEV, "Ignoring SyncUUID packet!\n");
4013
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004014 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004015}
4016
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004017/**
4018 * receive_bitmap_plain
4019 *
4020 * Return 0 when done, 1 when another iteration is needed, and a negative error
4021 * code upon failure.
4022 */
4023static int
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004024receive_bitmap_plain(struct drbd_conf *mdev, unsigned int size,
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004025 unsigned long *p, struct bm_xfer_ctx *c)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004026{
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004027 unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE -
4028 drbd_header_size(mdev->tconn);
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004029 unsigned int num_words = min_t(size_t, data_size / sizeof(*p),
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004030 c->bm_words - c->word_offset);
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004031 unsigned int want = num_words * sizeof(*p);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004032 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004033
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004034 if (want != size) {
4035 dev_err(DEV, "%s:want (%u) != size (%u)\n", __func__, want, size);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004036 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004037 }
4038 if (want == 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004039 return 0;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004040 err = drbd_recv_all(mdev->tconn, p, want);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004041 if (err)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004042 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004043
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004044 drbd_bm_merge_lel(mdev, c->word_offset, num_words, p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004045
4046 c->word_offset += num_words;
4047 c->bit_offset = c->word_offset * BITS_PER_LONG;
4048 if (c->bit_offset > c->bm_bits)
4049 c->bit_offset = c->bm_bits;
4050
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004051 return 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004052}
4053
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004054static enum drbd_bitmap_code dcbp_get_code(struct p_compressed_bm *p)
4055{
4056 return (enum drbd_bitmap_code)(p->encoding & 0x0f);
4057}
4058
4059static int dcbp_get_start(struct p_compressed_bm *p)
4060{
4061 return (p->encoding & 0x80) != 0;
4062}
4063
4064static int dcbp_get_pad_bits(struct p_compressed_bm *p)
4065{
4066 return (p->encoding >> 4) & 0x7;
4067}
4068
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004069/**
4070 * recv_bm_rle_bits
4071 *
4072 * Return 0 when done, 1 when another iteration is needed, and a negative error
4073 * code upon failure.
4074 */
4075static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07004076recv_bm_rle_bits(struct drbd_conf *mdev,
4077 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01004078 struct bm_xfer_ctx *c,
4079 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004080{
4081 struct bitstream bs;
4082 u64 look_ahead;
4083 u64 rl;
4084 u64 tmp;
4085 unsigned long s = c->bit_offset;
4086 unsigned long e;
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004087 int toggle = dcbp_get_start(p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004088 int have;
4089 int bits;
4090
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004091 bitstream_init(&bs, p->code, len, dcbp_get_pad_bits(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07004092
4093 bits = bitstream_get_bits(&bs, &look_ahead, 64);
4094 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004095 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004096
4097 for (have = bits; have > 0; s += rl, toggle = !toggle) {
4098 bits = vli_decode_bits(&rl, look_ahead);
4099 if (bits <= 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004100 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004101
4102 if (toggle) {
4103 e = s + rl -1;
4104 if (e >= c->bm_bits) {
4105 dev_err(DEV, "bitmap overflow (e:%lu) while decoding bm RLE packet\n", e);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004106 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004107 }
4108 _drbd_bm_set_bits(mdev, s, e);
4109 }
4110
4111 if (have < bits) {
4112 dev_err(DEV, "bitmap decoding error: h:%d b:%d la:0x%08llx l:%u/%u\n",
4113 have, bits, look_ahead,
4114 (unsigned int)(bs.cur.b - p->code),
4115 (unsigned int)bs.buf_len);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004116 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004117 }
4118 look_ahead >>= bits;
4119 have -= bits;
4120
4121 bits = bitstream_get_bits(&bs, &tmp, 64 - have);
4122 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004123 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004124 look_ahead |= tmp << have;
4125 have += bits;
4126 }
4127
4128 c->bit_offset = s;
4129 bm_xfer_ctx_bit_to_word_offset(c);
4130
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004131 return (s != c->bm_bits);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004132}
4133
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004134/**
4135 * decode_bitmap_c
4136 *
4137 * Return 0 when done, 1 when another iteration is needed, and a negative error
4138 * code upon failure.
4139 */
4140static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07004141decode_bitmap_c(struct drbd_conf *mdev,
4142 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01004143 struct bm_xfer_ctx *c,
4144 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004145{
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004146 if (dcbp_get_code(p) == RLE_VLI_Bits)
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004147 return recv_bm_rle_bits(mdev, p, c, len - sizeof(*p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07004148
4149 /* other variants had been implemented for evaluation,
4150 * but have been dropped as this one turned out to be "best"
4151 * during all our tests. */
4152
4153 dev_err(DEV, "receive_bitmap_c: unknown encoding %u\n", p->encoding);
Philipp Reisner38fa9982011-03-15 18:24:49 +01004154 conn_request_state(mdev->tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004155 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004156}
4157
4158void INFO_bm_xfer_stats(struct drbd_conf *mdev,
4159 const char *direction, struct bm_xfer_ctx *c)
4160{
4161 /* what would it take to transfer it "plaintext" */
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004162 unsigned int header_size = drbd_header_size(mdev->tconn);
4163 unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE - header_size;
4164 unsigned int plain =
4165 header_size * (DIV_ROUND_UP(c->bm_words, data_size) + 1) +
4166 c->bm_words * sizeof(unsigned long);
4167 unsigned int total = c->bytes[0] + c->bytes[1];
4168 unsigned int r;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004169
4170 /* total can not be zero. but just in case: */
4171 if (total == 0)
4172 return;
4173
4174 /* don't report if not compressed */
4175 if (total >= plain)
4176 return;
4177
4178 /* total < plain. check for overflow, still */
4179 r = (total > UINT_MAX/1000) ? (total / (plain/1000))
4180 : (1000 * total / plain);
4181
4182 if (r > 1000)
4183 r = 1000;
4184
4185 r = 1000 - r;
4186 dev_info(DEV, "%s bitmap stats [Bytes(packets)]: plain %u(%u), RLE %u(%u), "
4187 "total %u; compression: %u.%u%%\n",
4188 direction,
4189 c->bytes[1], c->packets[1],
4190 c->bytes[0], c->packets[0],
4191 total, r/10, r % 10);
4192}
4193
4194/* Since we are processing the bitfield from lower addresses to higher,
4195 it does not matter if the process it in 32 bit chunks or 64 bit
4196 chunks as long as it is little endian. (Understand it as byte stream,
4197 beginning with the lowest byte...) If we would use big endian
4198 we would need to process it from the highest address to the lowest,
4199 in order to be agnostic to the 32 vs 64 bits issue.
4200
4201 returns 0 on failure, 1 if we successfully received it. */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004202static int receive_bitmap(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004203{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004204 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004205 struct bm_xfer_ctx c;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004206 int err;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004207
4208 mdev = vnr_to_mdev(tconn, pi->vnr);
4209 if (!mdev)
4210 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004211
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004212 drbd_bm_lock(mdev, "receive bitmap", BM_LOCKED_SET_ALLOWED);
4213 /* you are supposed to send additional out-of-sync information
4214 * if you actually set bits during this phase */
Philipp Reisnerb411b362009-09-25 16:07:19 -07004215
Philipp Reisnerb411b362009-09-25 16:07:19 -07004216 c = (struct bm_xfer_ctx) {
4217 .bm_bits = drbd_bm_bits(mdev),
4218 .bm_words = drbd_bm_words(mdev),
4219 };
4220
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004221 for(;;) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004222 if (pi->cmd == P_BITMAP)
4223 err = receive_bitmap_plain(mdev, pi->size, pi->data, &c);
4224 else if (pi->cmd == P_COMPRESSED_BITMAP) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004225 /* MAYBE: sanity check that we speak proto >= 90,
4226 * and the feature is enabled! */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004227 struct p_compressed_bm *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004228
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004229 if (pi->size > DRBD_SOCKET_BUFFER_SIZE - drbd_header_size(tconn)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004230 dev_err(DEV, "ReportCBitmap packet too large\n");
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004231 err = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004232 goto out;
4233 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004234 if (pi->size <= sizeof(*p)) {
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004235 dev_err(DEV, "ReportCBitmap packet too small (l:%u)\n", pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004236 err = -EIO;
Andreas Gruenbacher78fcbda2010-12-10 22:18:27 +01004237 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004238 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004239 err = drbd_recv_all(mdev->tconn, p, pi->size);
4240 if (err)
4241 goto out;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004242 err = decode_bitmap_c(mdev, p, &c, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004243 } else {
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004244 dev_warn(DEV, "receive_bitmap: cmd neither ReportBitMap nor ReportCBitMap (is 0x%x)", pi->cmd);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004245 err = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004246 goto out;
4247 }
4248
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004249 c.packets[pi->cmd == P_BITMAP]++;
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004250 c.bytes[pi->cmd == P_BITMAP] += drbd_header_size(tconn) + pi->size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004251
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004252 if (err <= 0) {
4253 if (err < 0)
4254 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004255 break;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004256 }
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004257 err = drbd_recv_header(mdev->tconn, pi);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004258 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004259 goto out;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004260 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004261
4262 INFO_bm_xfer_stats(mdev, "receive", &c);
4263
4264 if (mdev->state.conn == C_WF_BITMAP_T) {
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01004265 enum drbd_state_rv rv;
4266
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004267 err = drbd_send_bitmap(mdev);
4268 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004269 goto out;
4270 /* Omit CS_ORDERED with this state transition to avoid deadlocks. */
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01004271 rv = _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
4272 D_ASSERT(rv == SS_SUCCESS);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004273 } else if (mdev->state.conn != C_WF_BITMAP_S) {
4274 /* admin may have requested C_DISCONNECTING,
4275 * other threads may have noticed network errors */
4276 dev_info(DEV, "unexpected cstate (%s) in receive_bitmap\n",
4277 drbd_conn_str(mdev->state.conn));
4278 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004279 err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004280
Philipp Reisnerb411b362009-09-25 16:07:19 -07004281 out:
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004282 drbd_bm_unlock(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004283 if (!err && mdev->state.conn == C_WF_BITMAP_S)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004284 drbd_start_resync(mdev, C_SYNC_SOURCE);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004285 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004286}
4287
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004288static int receive_skip(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004289{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004290 conn_warn(tconn, "skipping unknown optional packet type %d, l: %d!\n",
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004291 pi->cmd, pi->size);
Philipp Reisner2de876e2011-03-15 14:38:01 +01004292
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004293 return ignore_remaining_packet(tconn, pi);
Philipp Reisner2de876e2011-03-15 14:38:01 +01004294}
4295
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004296static int receive_UnplugRemote(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004297{
Philipp Reisnerb411b362009-09-25 16:07:19 -07004298 /* Make sure we've acked all the TCP data associated
4299 * with the data requests being unplugged */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004300 drbd_tcp_quickack(tconn->data.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004301
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004302 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004303}
4304
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004305static int receive_out_of_sync(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner73a01a12010-10-27 14:33:00 +02004306{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004307 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004308 struct p_block_desc *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004309
4310 mdev = vnr_to_mdev(tconn, pi->vnr);
4311 if (!mdev)
4312 return -EIO;
Philipp Reisner73a01a12010-10-27 14:33:00 +02004313
Lars Ellenbergf735e3632010-12-17 21:06:18 +01004314 switch (mdev->state.conn) {
4315 case C_WF_SYNC_UUID:
4316 case C_WF_BITMAP_T:
4317 case C_BEHIND:
4318 break;
4319 default:
4320 dev_err(DEV, "ASSERT FAILED cstate = %s, expected: WFSyncUUID|WFBitMapT|Behind\n",
4321 drbd_conn_str(mdev->state.conn));
4322 }
4323
Philipp Reisner73a01a12010-10-27 14:33:00 +02004324 drbd_set_out_of_sync(mdev, be64_to_cpu(p->sector), be32_to_cpu(p->blksize));
4325
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004326 return 0;
Philipp Reisner73a01a12010-10-27 14:33:00 +02004327}
4328
Philipp Reisner02918be2010-08-20 14:35:10 +02004329struct data_cmd {
4330 int expect_payload;
4331 size_t pkt_size;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004332 int (*fn)(struct drbd_tconn *, struct packet_info *);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004333};
4334
Philipp Reisner02918be2010-08-20 14:35:10 +02004335static struct data_cmd drbd_cmd_handler[] = {
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004336 [P_DATA] = { 1, sizeof(struct p_data), receive_Data },
4337 [P_DATA_REPLY] = { 1, sizeof(struct p_data), receive_DataReply },
4338 [P_RS_DATA_REPLY] = { 1, sizeof(struct p_data), receive_RSDataReply } ,
4339 [P_BARRIER] = { 0, sizeof(struct p_barrier), receive_Barrier } ,
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004340 [P_BITMAP] = { 1, 0, receive_bitmap } ,
4341 [P_COMPRESSED_BITMAP] = { 1, 0, receive_bitmap } ,
4342 [P_UNPLUG_REMOTE] = { 0, 0, receive_UnplugRemote },
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004343 [P_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
4344 [P_RS_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004345 [P_SYNC_PARAM] = { 1, 0, receive_SyncParam },
4346 [P_SYNC_PARAM89] = { 1, 0, receive_SyncParam },
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004347 [P_PROTOCOL] = { 1, sizeof(struct p_protocol), receive_protocol },
4348 [P_UUIDS] = { 0, sizeof(struct p_uuids), receive_uuids },
4349 [P_SIZES] = { 0, sizeof(struct p_sizes), receive_sizes },
4350 [P_STATE] = { 0, sizeof(struct p_state), receive_state },
4351 [P_STATE_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_state },
4352 [P_SYNC_UUID] = { 0, sizeof(struct p_rs_uuid), receive_sync_uuid },
4353 [P_OV_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
4354 [P_OV_REPLY] = { 1, sizeof(struct p_block_req), receive_DataRequest },
4355 [P_CSUM_RS_REQUEST] = { 1, sizeof(struct p_block_req), receive_DataRequest },
4356 [P_DELAY_PROBE] = { 0, sizeof(struct p_delay_probe93), receive_skip },
4357 [P_OUT_OF_SYNC] = { 0, sizeof(struct p_block_desc), receive_out_of_sync },
4358 [P_CONN_ST_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_conn_state },
Philipp Reisner036b17e2011-05-16 17:38:11 +02004359 [P_PROTOCOL_UPDATE] = { 1, sizeof(struct p_protocol), receive_protocol },
Philipp Reisner02918be2010-08-20 14:35:10 +02004360};
4361
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004362static void drbdd(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004363{
Philipp Reisner77351055b2011-02-07 17:24:26 +01004364 struct packet_info pi;
Philipp Reisner02918be2010-08-20 14:35:10 +02004365 size_t shs; /* sub header size */
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004366 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004367
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004368 while (get_t_state(&tconn->receiver) == RUNNING) {
Andreas Gruenbacherdeebe192011-03-25 00:01:04 +01004369 struct data_cmd *cmd;
4370
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004371 drbd_thread_current_set_cpu(&tconn->receiver);
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004372 if (drbd_recv_header(tconn, &pi))
Philipp Reisner02918be2010-08-20 14:35:10 +02004373 goto err_out;
4374
Andreas Gruenbacherdeebe192011-03-25 00:01:04 +01004375 cmd = &drbd_cmd_handler[pi.cmd];
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004376 if (unlikely(pi.cmd >= ARRAY_SIZE(drbd_cmd_handler) || !cmd->fn)) {
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004377 conn_err(tconn, "Unexpected data packet %s (0x%04x)",
4378 cmdname(pi.cmd), pi.cmd);
Philipp Reisner02918be2010-08-20 14:35:10 +02004379 goto err_out;
Lars Ellenberg0b33a912009-11-16 15:58:04 +01004380 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004381
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004382 shs = cmd->pkt_size;
4383 if (pi.size > shs && !cmd->expect_payload) {
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004384 conn_err(tconn, "No payload expected %s l:%d\n",
4385 cmdname(pi.cmd), pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02004386 goto err_out;
4387 }
4388
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004389 if (shs) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004390 err = drbd_recv_all_warn(tconn, pi.data, shs);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004391 if (err)
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004392 goto err_out;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004393 pi.size -= shs;
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004394 }
4395
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004396 err = cmd->fn(tconn, &pi);
4397 if (err) {
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004398 conn_err(tconn, "error receiving %s, e: %d l: %d!\n",
4399 cmdname(pi.cmd), err, pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02004400 goto err_out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004401 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004402 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004403 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004404
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004405 err_out:
4406 conn_request_state(tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004407}
4408
Philipp Reisner0e29d162011-02-18 14:23:11 +01004409void conn_flush_workqueue(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004410{
4411 struct drbd_wq_barrier barr;
4412
4413 barr.w.cb = w_prev_work_done;
Philipp Reisner0e29d162011-02-18 14:23:11 +01004414 barr.w.tconn = tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004415 init_completion(&barr.done);
Philipp Reisner0e29d162011-02-18 14:23:11 +01004416 drbd_queue_work(&tconn->data.work, &barr.w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004417 wait_for_completion(&barr.done);
4418}
4419
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004420static void conn_disconnect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004421{
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004422 struct drbd_conf *mdev;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004423 enum drbd_conns oc;
Philipp Reisner376694a2011-11-07 10:54:28 +01004424 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004425
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004426 if (tconn->cstate == C_STANDALONE)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004427 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004428
Philipp Reisnerb8853db2011-12-13 11:09:16 +01004429 /* We are about to start the cleanup after connection loss.
4430 * Make sure drbd_make_request knows about that.
4431 * Usually we should be in some network failure state already,
4432 * but just in case we are not, we fix it up here.
4433 */
4434 conn_request_state(tconn, NS(conn, C_NETWORK_FAILURE), CS_HARD);
4435
Philipp Reisnerb411b362009-09-25 16:07:19 -07004436 /* asender does not clean up anything. it must not interfere, either */
Philipp Reisner360cc742011-02-08 14:29:53 +01004437 drbd_thread_stop(&tconn->asender);
4438 drbd_free_sock(tconn);
4439
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004440 rcu_read_lock();
4441 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
4442 kref_get(&mdev->kref);
4443 rcu_read_unlock();
4444 drbd_disconnected(mdev);
4445 kref_put(&mdev->kref, &drbd_minor_destroy);
4446 rcu_read_lock();
4447 }
4448 rcu_read_unlock();
4449
Philipp Reisner12038a32011-11-09 19:18:00 +01004450 if (!list_empty(&tconn->current_epoch->list))
4451 conn_err(tconn, "ASSERTION FAILED: tconn->current_epoch->list not empty\n");
4452 /* ok, no more ee's on the fly, it is safe to reset the epoch_size */
4453 atomic_set(&tconn->current_epoch->epoch_size, 0);
4454
Philipp Reisner360cc742011-02-08 14:29:53 +01004455 conn_info(tconn, "Connection closed\n");
4456
Philipp Reisnercb703452011-03-24 11:03:07 +01004457 if (conn_highest_role(tconn) == R_PRIMARY && conn_highest_pdsk(tconn) >= D_UNKNOWN)
4458 conn_try_outdate_peer_async(tconn);
4459
Philipp Reisner360cc742011-02-08 14:29:53 +01004460 spin_lock_irq(&tconn->req_lock);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004461 oc = tconn->cstate;
4462 if (oc >= C_UNCONNECTED)
Philipp Reisner376694a2011-11-07 10:54:28 +01004463 _conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004464
Philipp Reisner360cc742011-02-08 14:29:53 +01004465 spin_unlock_irq(&tconn->req_lock);
4466
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02004467 if (oc == C_DISCONNECTING)
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +02004468 conn_request_state(tconn, NS(conn, C_STANDALONE), CS_VERBOSE | CS_HARD);
Philipp Reisner360cc742011-02-08 14:29:53 +01004469}
4470
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004471static int drbd_disconnected(struct drbd_conf *mdev)
Philipp Reisner360cc742011-02-08 14:29:53 +01004472{
Philipp Reisner360cc742011-02-08 14:29:53 +01004473 unsigned int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004474
Philipp Reisner85719572010-07-21 10:20:17 +02004475 /* wait for current activity to cease. */
Philipp Reisner87eeee42011-01-19 14:16:30 +01004476 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004477 _drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
4478 _drbd_wait_ee_list_empty(mdev, &mdev->sync_ee);
4479 _drbd_wait_ee_list_empty(mdev, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004480 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004481
4482 /* We do not have data structures that would allow us to
4483 * get the rs_pending_cnt down to 0 again.
4484 * * On C_SYNC_TARGET we do not have any data structures describing
4485 * the pending RSDataRequest's we have sent.
4486 * * On C_SYNC_SOURCE there is no data structure that tracks
4487 * the P_RS_DATA_REPLY blocks that we sent to the SyncTarget.
4488 * And no, it is not the sum of the reference counts in the
4489 * resync_LRU. The resync_LRU tracks the whole operation including
4490 * the disk-IO, while the rs_pending_cnt only tracks the blocks
4491 * on the fly. */
4492 drbd_rs_cancel_all(mdev);
4493 mdev->rs_total = 0;
4494 mdev->rs_failed = 0;
4495 atomic_set(&mdev->rs_pending_cnt, 0);
4496 wake_up(&mdev->misc_wait);
4497
Philipp Reisnerb411b362009-09-25 16:07:19 -07004498 del_timer_sync(&mdev->resync_timer);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004499 resync_timer_fn((unsigned long)mdev);
4500
Philipp Reisnerb411b362009-09-25 16:07:19 -07004501 /* wait for all w_e_end_data_req, w_e_end_rsdata_req, w_send_barrier,
4502 * w_make_resync_request etc. which may still be on the worker queue
4503 * to be "canceled" */
Philipp Reisnera21e9292011-02-08 15:08:49 +01004504 drbd_flush_workqueue(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004505
Andreas Gruenbachera990be42011-04-06 17:56:48 +02004506 drbd_finish_peer_reqs(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004507
Philipp Reisnerd10b4ea2011-11-30 23:25:36 +01004508 /* This second workqueue flush is necessary, since drbd_finish_peer_reqs()
4509 might have issued a work again. The one before drbd_finish_peer_reqs() is
4510 necessary to reclain net_ee in drbd_finish_peer_reqs(). */
4511 drbd_flush_workqueue(mdev);
4512
Philipp Reisnerb411b362009-09-25 16:07:19 -07004513 kfree(mdev->p_uuid);
4514 mdev->p_uuid = NULL;
4515
Philipp Reisner2aebfab2011-03-28 16:48:11 +02004516 if (!drbd_suspended(mdev))
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01004517 tl_clear(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004518
Philipp Reisnerb411b362009-09-25 16:07:19 -07004519 drbd_md_sync(mdev);
4520
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004521 /* serialize with bitmap writeout triggered by the state change,
4522 * if any. */
4523 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
4524
Philipp Reisnerb411b362009-09-25 16:07:19 -07004525 /* tcp_close and release of sendpage pages can be deferred. I don't
4526 * want to use SO_LINGER, because apparently it can be deferred for
4527 * more than 20 seconds (longest time I checked).
4528 *
4529 * Actually we don't care for exactly when the network stack does its
4530 * put_page(), but release our reference on these pages right here.
4531 */
Andreas Gruenbacher7721f562011-04-06 17:14:02 +02004532 i = drbd_free_peer_reqs(mdev, &mdev->net_ee);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004533 if (i)
4534 dev_info(DEV, "net_ee not empty, killed %u entries\n", i);
Lars Ellenberg435f0742010-09-06 12:30:25 +02004535 i = atomic_read(&mdev->pp_in_use_by_net);
4536 if (i)
4537 dev_info(DEV, "pp_in_use_by_net = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004538 i = atomic_read(&mdev->pp_in_use);
4539 if (i)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02004540 dev_info(DEV, "pp_in_use = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004541
4542 D_ASSERT(list_empty(&mdev->read_ee));
4543 D_ASSERT(list_empty(&mdev->active_ee));
4544 D_ASSERT(list_empty(&mdev->sync_ee));
4545 D_ASSERT(list_empty(&mdev->done_ee));
4546
Philipp Reisner360cc742011-02-08 14:29:53 +01004547 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004548}
4549
4550/*
4551 * We support PRO_VERSION_MIN to PRO_VERSION_MAX. The protocol version
4552 * we can agree on is stored in agreed_pro_version.
4553 *
4554 * feature flags and the reserved array should be enough room for future
4555 * enhancements of the handshake protocol, and possible plugins...
4556 *
4557 * for now, they are expected to be zero, but ignored.
4558 */
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004559static int drbd_send_features(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004560{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004561 struct drbd_socket *sock;
4562 struct p_connection_features *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004563
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004564 sock = &tconn->data;
4565 p = conn_prepare_command(tconn, sock);
4566 if (!p)
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004567 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004568 memset(p, 0, sizeof(*p));
4569 p->protocol_min = cpu_to_be32(PRO_VERSION_MIN);
4570 p->protocol_max = cpu_to_be32(PRO_VERSION_MAX);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004571 return conn_send_command(tconn, sock, P_CONNECTION_FEATURES, sizeof(*p), NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004572}
4573
4574/*
4575 * return values:
4576 * 1 yes, we have a valid connection
4577 * 0 oops, did not work out, please try again
4578 * -1 peer talks different language,
4579 * no point in trying again, please go standalone.
4580 */
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004581static int drbd_do_features(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004582{
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004583 /* ASSERT current == tconn->receiver ... */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004584 struct p_connection_features *p;
4585 const int expect = sizeof(struct p_connection_features);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004586 struct packet_info pi;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004587 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004588
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004589 err = drbd_send_features(tconn);
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004590 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004591 return 0;
4592
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004593 err = drbd_recv_header(tconn, &pi);
4594 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004595 return 0;
4596
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004597 if (pi.cmd != P_CONNECTION_FEATURES) {
4598 conn_err(tconn, "expected ConnectionFeatures packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004599 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004600 return -1;
4601 }
4602
Philipp Reisner77351055b2011-02-07 17:24:26 +01004603 if (pi.size != expect) {
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004604 conn_err(tconn, "expected ConnectionFeatures length: %u, received: %u\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004605 expect, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004606 return -1;
4607 }
4608
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004609 p = pi.data;
4610 err = drbd_recv_all_warn(tconn, p, expect);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004611 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004612 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004613
Philipp Reisnerb411b362009-09-25 16:07:19 -07004614 p->protocol_min = be32_to_cpu(p->protocol_min);
4615 p->protocol_max = be32_to_cpu(p->protocol_max);
4616 if (p->protocol_max == 0)
4617 p->protocol_max = p->protocol_min;
4618
4619 if (PRO_VERSION_MAX < p->protocol_min ||
4620 PRO_VERSION_MIN > p->protocol_max)
4621 goto incompat;
4622
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004623 tconn->agreed_pro_version = min_t(int, PRO_VERSION_MAX, p->protocol_max);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004624
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004625 conn_info(tconn, "Handshake successful: "
4626 "Agreed network protocol version %d\n", tconn->agreed_pro_version);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004627
4628 return 1;
4629
4630 incompat:
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004631 conn_err(tconn, "incompatible DRBD dialects: "
Philipp Reisnerb411b362009-09-25 16:07:19 -07004632 "I support %d-%d, peer supports %d-%d\n",
4633 PRO_VERSION_MIN, PRO_VERSION_MAX,
4634 p->protocol_min, p->protocol_max);
4635 return -1;
4636}
4637
4638#if !defined(CONFIG_CRYPTO_HMAC) && !defined(CONFIG_CRYPTO_HMAC_MODULE)
Philipp Reisner13e60372011-02-08 09:54:40 +01004639static int drbd_do_auth(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004640{
4641 dev_err(DEV, "This kernel was build without CONFIG_CRYPTO_HMAC.\n");
4642 dev_err(DEV, "You need to disable 'cram-hmac-alg' in drbd.conf.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004643 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004644}
4645#else
4646#define CHALLENGE_LEN 64
Johannes Thomab10d96c2010-01-07 16:02:50 +01004647
4648/* Return value:
4649 1 - auth succeeded,
4650 0 - failed, try again (network error),
4651 -1 - auth failed, don't try again.
4652*/
4653
Philipp Reisner13e60372011-02-08 09:54:40 +01004654static int drbd_do_auth(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004655{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004656 struct drbd_socket *sock;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004657 char my_challenge[CHALLENGE_LEN]; /* 64 Bytes... */
4658 struct scatterlist sg;
4659 char *response = NULL;
4660 char *right_response = NULL;
4661 char *peers_ch = NULL;
Philipp Reisner44ed1672011-04-19 17:10:19 +02004662 unsigned int key_len;
4663 char secret[SHARED_SECRET_MAX]; /* 64 byte */
Philipp Reisnerb411b362009-09-25 16:07:19 -07004664 unsigned int resp_size;
4665 struct hash_desc desc;
Philipp Reisner77351055b2011-02-07 17:24:26 +01004666 struct packet_info pi;
Philipp Reisner44ed1672011-04-19 17:10:19 +02004667 struct net_conf *nc;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004668 int err, rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004669
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004670 /* FIXME: Put the challenge/response into the preallocated socket buffer. */
4671
Philipp Reisner44ed1672011-04-19 17:10:19 +02004672 rcu_read_lock();
4673 nc = rcu_dereference(tconn->net_conf);
4674 key_len = strlen(nc->shared_secret);
4675 memcpy(secret, nc->shared_secret, key_len);
4676 rcu_read_unlock();
4677
Philipp Reisner13e60372011-02-08 09:54:40 +01004678 desc.tfm = tconn->cram_hmac_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004679 desc.flags = 0;
4680
Philipp Reisner44ed1672011-04-19 17:10:19 +02004681 rv = crypto_hash_setkey(tconn->cram_hmac_tfm, (u8 *)secret, key_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004682 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004683 conn_err(tconn, "crypto_hash_setkey() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004684 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004685 goto fail;
4686 }
4687
4688 get_random_bytes(my_challenge, CHALLENGE_LEN);
4689
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004690 sock = &tconn->data;
4691 if (!conn_prepare_command(tconn, sock)) {
4692 rv = 0;
4693 goto fail;
4694 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004695 rv = !conn_send_command(tconn, sock, P_AUTH_CHALLENGE, 0,
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004696 my_challenge, CHALLENGE_LEN);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004697 if (!rv)
4698 goto fail;
4699
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004700 err = drbd_recv_header(tconn, &pi);
4701 if (err) {
4702 rv = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004703 goto fail;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004704 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004705
Philipp Reisner77351055b2011-02-07 17:24:26 +01004706 if (pi.cmd != P_AUTH_CHALLENGE) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004707 conn_err(tconn, "expected AuthChallenge packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004708 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004709 rv = 0;
4710 goto fail;
4711 }
4712
Philipp Reisner77351055b2011-02-07 17:24:26 +01004713 if (pi.size > CHALLENGE_LEN * 2) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004714 conn_err(tconn, "expected AuthChallenge payload too big.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004715 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004716 goto fail;
4717 }
4718
Philipp Reisner77351055b2011-02-07 17:24:26 +01004719 peers_ch = kmalloc(pi.size, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004720 if (peers_ch == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004721 conn_err(tconn, "kmalloc of peers_ch failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004722 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004723 goto fail;
4724 }
4725
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004726 err = drbd_recv_all_warn(tconn, peers_ch, pi.size);
4727 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004728 rv = 0;
4729 goto fail;
4730 }
4731
Philipp Reisner13e60372011-02-08 09:54:40 +01004732 resp_size = crypto_hash_digestsize(tconn->cram_hmac_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004733 response = kmalloc(resp_size, GFP_NOIO);
4734 if (response == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004735 conn_err(tconn, "kmalloc of response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004736 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004737 goto fail;
4738 }
4739
4740 sg_init_table(&sg, 1);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004741 sg_set_buf(&sg, peers_ch, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004742
4743 rv = crypto_hash_digest(&desc, &sg, sg.length, response);
4744 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004745 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004746 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004747 goto fail;
4748 }
4749
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004750 if (!conn_prepare_command(tconn, sock)) {
4751 rv = 0;
4752 goto fail;
4753 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004754 rv = !conn_send_command(tconn, sock, P_AUTH_RESPONSE, 0,
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004755 response, resp_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004756 if (!rv)
4757 goto fail;
4758
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004759 err = drbd_recv_header(tconn, &pi);
4760 if (err) {
4761 rv = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004762 goto fail;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004763 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004764
Philipp Reisner77351055b2011-02-07 17:24:26 +01004765 if (pi.cmd != P_AUTH_RESPONSE) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004766 conn_err(tconn, "expected AuthResponse packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004767 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004768 rv = 0;
4769 goto fail;
4770 }
4771
Philipp Reisner77351055b2011-02-07 17:24:26 +01004772 if (pi.size != resp_size) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004773 conn_err(tconn, "expected AuthResponse payload of wrong size\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004774 rv = 0;
4775 goto fail;
4776 }
4777
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004778 err = drbd_recv_all_warn(tconn, response , resp_size);
4779 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004780 rv = 0;
4781 goto fail;
4782 }
4783
4784 right_response = kmalloc(resp_size, GFP_NOIO);
Julia Lawall2d1ee872009-12-27 22:27:11 +01004785 if (right_response == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004786 conn_err(tconn, "kmalloc of right_response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004787 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004788 goto fail;
4789 }
4790
4791 sg_set_buf(&sg, my_challenge, CHALLENGE_LEN);
4792
4793 rv = crypto_hash_digest(&desc, &sg, sg.length, right_response);
4794 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004795 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004796 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004797 goto fail;
4798 }
4799
4800 rv = !memcmp(response, right_response, resp_size);
4801
4802 if (rv)
Philipp Reisner44ed1672011-04-19 17:10:19 +02004803 conn_info(tconn, "Peer authenticated using %d bytes HMAC\n",
4804 resp_size);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004805 else
4806 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004807
4808 fail:
4809 kfree(peers_ch);
4810 kfree(response);
4811 kfree(right_response);
4812
4813 return rv;
4814}
4815#endif
4816
4817int drbdd_init(struct drbd_thread *thi)
4818{
Philipp Reisner392c8802011-02-09 10:33:31 +01004819 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004820 int h;
4821
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004822 conn_info(tconn, "receiver (re)started\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004823
4824 do {
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004825 h = conn_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004826 if (h == 0) {
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004827 conn_disconnect(tconn);
Philipp Reisner20ee6392011-01-18 15:28:59 +01004828 schedule_timeout_interruptible(HZ);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004829 }
4830 if (h == -1) {
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004831 conn_warn(tconn, "Discarding network configuration.\n");
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004832 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004833 }
4834 } while (h == 0);
4835
Philipp Reisner91fd4da2011-04-20 17:47:29 +02004836 if (h > 0)
4837 drbdd(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004838
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004839 conn_disconnect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004840
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004841 conn_info(tconn, "receiver terminated\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004842 return 0;
4843}
4844
4845/* ********* acknowledge sender ******** */
4846
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004847static int got_conn_RqSReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004848{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004849 struct p_req_state_reply *p = pi->data;
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004850 int retcode = be32_to_cpu(p->retcode);
4851
4852 if (retcode >= SS_SUCCESS) {
4853 set_bit(CONN_WD_ST_CHG_OKAY, &tconn->flags);
4854 } else {
4855 set_bit(CONN_WD_ST_CHG_FAIL, &tconn->flags);
4856 conn_err(tconn, "Requested state change failed by peer: %s (%d)\n",
4857 drbd_set_st_err_str(retcode), retcode);
4858 }
4859 wake_up(&tconn->ping_wait);
4860
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004861 return 0;
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004862}
4863
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004864static int got_RqSReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004865{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004866 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004867 struct p_req_state_reply *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004868 int retcode = be32_to_cpu(p->retcode);
4869
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004870 mdev = vnr_to_mdev(tconn, pi->vnr);
4871 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004872 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004873
Philipp Reisner4d0fc3f2012-01-20 13:52:27 +01004874 if (test_bit(CONN_WD_ST_CHG_REQ, &tconn->flags)) {
4875 D_ASSERT(tconn->agreed_pro_version < 100);
4876 return got_conn_RqSReply(tconn, pi);
4877 }
4878
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004879 if (retcode >= SS_SUCCESS) {
4880 set_bit(CL_ST_CHG_SUCCESS, &mdev->flags);
4881 } else {
4882 set_bit(CL_ST_CHG_FAIL, &mdev->flags);
4883 dev_err(DEV, "Requested state change failed by peer: %s (%d)\n",
4884 drbd_set_st_err_str(retcode), retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004885 }
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004886 wake_up(&mdev->state_wait);
4887
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004888 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004889}
4890
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004891static int got_Ping(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004892{
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004893 return drbd_send_ping_ack(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004894
4895}
4896
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004897static int got_PingAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004898{
4899 /* restore idle timeout */
Philipp Reisner2a67d8b2011-02-09 14:10:32 +01004900 tconn->meta.socket->sk->sk_rcvtimeo = tconn->net_conf->ping_int*HZ;
4901 if (!test_and_set_bit(GOT_PING_ACK, &tconn->flags))
4902 wake_up(&tconn->ping_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004903
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004904 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004905}
4906
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004907static int got_IsInSync(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004908{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004909 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004910 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004911 sector_t sector = be64_to_cpu(p->sector);
4912 int blksize = be32_to_cpu(p->blksize);
4913
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004914 mdev = vnr_to_mdev(tconn, pi->vnr);
4915 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004916 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004917
Philipp Reisner31890f42011-01-19 14:12:51 +01004918 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004919
4920 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4921
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004922 if (get_ldev(mdev)) {
4923 drbd_rs_complete_io(mdev, sector);
4924 drbd_set_in_sync(mdev, sector, blksize);
4925 /* rs_same_csums is supposed to count in units of BM_BLOCK_SIZE */
4926 mdev->rs_same_csum += (blksize >> BM_BLOCK_SHIFT);
4927 put_ldev(mdev);
4928 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004929 dec_rs_pending(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02004930 atomic_add(blksize >> 9, &mdev->rs_sect_in);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004931
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004932 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004933}
4934
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004935static int
4936validate_req_change_req_state(struct drbd_conf *mdev, u64 id, sector_t sector,
4937 struct rb_root *root, const char *func,
4938 enum drbd_req_event what, bool missing_ok)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004939{
4940 struct drbd_request *req;
4941 struct bio_and_error m;
4942
Philipp Reisner87eeee42011-01-19 14:16:30 +01004943 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004944 req = find_request(mdev, root, id, sector, missing_ok, func);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004945 if (unlikely(!req)) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01004946 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004947 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004948 }
4949 __req_mod(req, what, &m);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004950 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004951
4952 if (m.bio)
4953 complete_master_bio(mdev, &m);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004954 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004955}
4956
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004957static int got_BlockAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004958{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004959 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004960 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004961 sector_t sector = be64_to_cpu(p->sector);
4962 int blksize = be32_to_cpu(p->blksize);
4963 enum drbd_req_event what;
4964
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004965 mdev = vnr_to_mdev(tconn, pi->vnr);
4966 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004967 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004968
Philipp Reisnerb411b362009-09-25 16:07:19 -07004969 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4970
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01004971 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004972 drbd_set_in_sync(mdev, sector, blksize);
4973 dec_rs_pending(mdev);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004974 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004975 }
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004976 switch (pi->cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004977 case P_RS_WRITE_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004978 what = WRITE_ACKED_BY_PEER_AND_SIS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004979 break;
4980 case P_WRITE_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004981 what = WRITE_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004982 break;
4983 case P_RECV_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004984 what = RECV_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004985 break;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004986 case P_DISCARD_WRITE:
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004987 what = DISCARD_WRITE;
4988 break;
4989 case P_RETRY_WRITE:
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004990 what = POSTPONE_WRITE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004991 break;
4992 default:
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004993 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -07004994 }
4995
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004996 return validate_req_change_req_state(mdev, p->block_id, sector,
4997 &mdev->write_requests, __func__,
4998 what, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004999}
5000
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005001static int got_NegAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005002{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005003 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005004 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005005 sector_t sector = be64_to_cpu(p->sector);
Philipp Reisner2deb8332011-01-17 18:39:18 +01005006 int size = be32_to_cpu(p->blksize);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02005007 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005008
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005009 mdev = vnr_to_mdev(tconn, pi->vnr);
5010 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005011 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005012
Philipp Reisnerb411b362009-09-25 16:07:19 -07005013 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
5014
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01005015 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07005016 dec_rs_pending(mdev);
5017 drbd_rs_failed_io(mdev, sector, size);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005018 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005019 }
Philipp Reisner2deb8332011-01-17 18:39:18 +01005020
Andreas Gruenbacher85997672011-04-04 13:09:15 +02005021 err = validate_req_change_req_state(mdev, p->block_id, sector,
5022 &mdev->write_requests, __func__,
Philipp Reisner303d1442011-04-13 16:24:47 -07005023 NEG_ACKED, true);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02005024 if (err) {
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01005025 /* Protocol A has no P_WRITE_ACKs, but has P_NEG_ACKs.
5026 The master bio might already be completed, therefore the
5027 request is no longer in the collision hash. */
5028 /* In Protocol B we might already have got a P_RECV_ACK
5029 but then get a P_NEG_ACK afterwards. */
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01005030 drbd_set_out_of_sync(mdev, sector, size);
Philipp Reisner2deb8332011-01-17 18:39:18 +01005031 }
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005032 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005033}
5034
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005035static int got_NegDReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005036{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005037 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005038 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005039 sector_t sector = be64_to_cpu(p->sector);
5040
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005041 mdev = vnr_to_mdev(tconn, pi->vnr);
5042 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005043 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005044
Philipp Reisnerb411b362009-09-25 16:07:19 -07005045 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01005046
Philipp Reisner380207d2011-11-11 12:31:20 +01005047 dev_err(DEV, "Got NegDReply; Sector %llus, len %u.\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -07005048 (unsigned long long)sector, be32_to_cpu(p->blksize));
5049
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005050 return validate_req_change_req_state(mdev, p->block_id, sector,
5051 &mdev->read_requests, __func__,
5052 NEG_ACKED, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005053}
5054
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005055static int got_NegRSDReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005056{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005057 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005058 sector_t sector;
5059 int size;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005060 struct p_block_ack *p = pi->data;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005061
5062 mdev = vnr_to_mdev(tconn, pi->vnr);
5063 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005064 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005065
5066 sector = be64_to_cpu(p->sector);
5067 size = be32_to_cpu(p->blksize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005068
5069 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
5070
5071 dec_rs_pending(mdev);
5072
5073 if (get_ldev_if_state(mdev, D_FAILED)) {
5074 drbd_rs_complete_io(mdev, sector);
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01005075 switch (pi->cmd) {
Philipp Reisnerd612d302010-12-27 10:53:28 +01005076 case P_NEG_RS_DREPLY:
5077 drbd_rs_failed_io(mdev, sector, size);
5078 case P_RS_CANCEL:
5079 break;
5080 default:
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005081 BUG();
Philipp Reisnerd612d302010-12-27 10:53:28 +01005082 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005083 put_ldev(mdev);
5084 }
5085
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005086 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005087}
5088
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005089static int got_BarrierAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005090{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005091 struct p_barrier_ack *p = pi->data;
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02005092 struct drbd_conf *mdev;
5093 int vnr;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005094
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02005095 tl_release(tconn, p->barrier, be32_to_cpu(p->set_size));
Philipp Reisnerb411b362009-09-25 16:07:19 -07005096
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02005097 rcu_read_lock();
5098 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
5099 if (mdev->state.conn == C_AHEAD &&
5100 atomic_read(&mdev->ap_in_flight) == 0 &&
5101 !test_and_set_bit(AHEAD_TO_SYNC_SOURCE, &mdev->flags)) {
5102 mdev->start_resync_timer.expires = jiffies + HZ;
5103 add_timer(&mdev->start_resync_timer);
5104 }
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02005105 }
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02005106 rcu_read_unlock();
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02005107
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005108 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005109}
5110
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005111static int got_OVResult(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005112{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005113 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005114 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005115 struct drbd_work *w;
5116 sector_t sector;
5117 int size;
5118
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005119 mdev = vnr_to_mdev(tconn, pi->vnr);
5120 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005121 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005122
Philipp Reisnerb411b362009-09-25 16:07:19 -07005123 sector = be64_to_cpu(p->sector);
5124 size = be32_to_cpu(p->blksize);
5125
5126 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
5127
5128 if (be64_to_cpu(p->block_id) == ID_OUT_OF_SYNC)
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005129 drbd_ov_out_of_sync_found(mdev, sector, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005130 else
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005131 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005132
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005133 if (!get_ldev(mdev))
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005134 return 0;
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005135
Philipp Reisnerb411b362009-09-25 16:07:19 -07005136 drbd_rs_complete_io(mdev, sector);
5137 dec_rs_pending(mdev);
5138
Lars Ellenbergea5442a2010-11-05 09:48:01 +01005139 --mdev->ov_left;
5140
5141 /* let's advance progress step marks only for every other megabyte */
5142 if ((mdev->ov_left & 0x200) == 0x200)
5143 drbd_advance_rs_marks(mdev, mdev->ov_left);
5144
5145 if (mdev->ov_left == 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07005146 w = kmalloc(sizeof(*w), GFP_NOIO);
5147 if (w) {
5148 w->cb = w_ov_finished;
Philipp Reisnera21e9292011-02-08 15:08:49 +01005149 w->mdev = mdev;
Philipp Reisnere42325a2011-01-19 13:55:45 +01005150 drbd_queue_work_front(&mdev->tconn->data.work, w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005151 } else {
5152 dev_err(DEV, "kmalloc(w) failed.");
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005153 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005154 drbd_resync_finished(mdev);
5155 }
5156 }
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005157 put_ldev(mdev);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005158 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005159}
5160
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005161static int got_skip(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner0ced55a2010-04-30 15:26:20 +02005162{
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005163 return 0;
Philipp Reisner0ced55a2010-04-30 15:26:20 +02005164}
5165
Andreas Gruenbachera990be42011-04-06 17:56:48 +02005166static int tconn_finish_peer_reqs(struct drbd_tconn *tconn)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005167{
Philipp Reisner082a3432011-03-15 16:05:42 +01005168 struct drbd_conf *mdev;
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005169 int vnr, not_empty = 0;
Philipp Reisner32862ec2011-02-08 16:41:01 +01005170
5171 do {
5172 clear_bit(SIGNAL_ASENDER, &tconn->flags);
5173 flush_signals(current);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005174
5175 rcu_read_lock();
5176 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
5177 kref_get(&mdev->kref);
5178 rcu_read_unlock();
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07005179 if (drbd_finish_peer_reqs(mdev)) {
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005180 kref_put(&mdev->kref, &drbd_minor_destroy);
5181 return 1;
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07005182 }
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005183 kref_put(&mdev->kref, &drbd_minor_destroy);
5184 rcu_read_lock();
Philipp Reisner082a3432011-03-15 16:05:42 +01005185 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005186 set_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisner082a3432011-03-15 16:05:42 +01005187
5188 spin_lock_irq(&tconn->req_lock);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005189 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Philipp Reisner082a3432011-03-15 16:05:42 +01005190 not_empty = !list_empty(&mdev->done_ee);
5191 if (not_empty)
5192 break;
5193 }
5194 spin_unlock_irq(&tconn->req_lock);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005195 rcu_read_unlock();
Philipp Reisner32862ec2011-02-08 16:41:01 +01005196 } while (not_empty);
5197
5198 return 0;
5199}
5200
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005201struct asender_cmd {
5202 size_t pkt_size;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005203 int (*fn)(struct drbd_tconn *tconn, struct packet_info *);
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005204};
5205
5206static struct asender_cmd asender_tbl[] = {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005207 [P_PING] = { 0, got_Ping },
5208 [P_PING_ACK] = { 0, got_PingAck },
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005209 [P_RECV_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5210 [P_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5211 [P_RS_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5212 [P_DISCARD_WRITE] = { sizeof(struct p_block_ack), got_BlockAck },
5213 [P_NEG_ACK] = { sizeof(struct p_block_ack), got_NegAck },
5214 [P_NEG_DREPLY] = { sizeof(struct p_block_ack), got_NegDReply },
5215 [P_NEG_RS_DREPLY] = { sizeof(struct p_block_ack), got_NegRSDReply },
5216 [P_OV_RESULT] = { sizeof(struct p_block_ack), got_OVResult },
5217 [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), got_BarrierAck },
5218 [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), got_RqSReply },
5219 [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), got_IsInSync },
5220 [P_DELAY_PROBE] = { sizeof(struct p_delay_probe93), got_skip },
5221 [P_RS_CANCEL] = { sizeof(struct p_block_ack), got_NegRSDReply },
5222 [P_CONN_ST_CHG_REPLY]={ sizeof(struct p_req_state_reply), got_conn_RqSReply },
5223 [P_RETRY_WRITE] = { sizeof(struct p_block_ack), got_BlockAck },
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005224};
5225
Philipp Reisnerb411b362009-09-25 16:07:19 -07005226int drbd_asender(struct drbd_thread *thi)
5227{
Philipp Reisner392c8802011-02-09 10:33:31 +01005228 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005229 struct asender_cmd *cmd = NULL;
Philipp Reisner77351055b2011-02-07 17:24:26 +01005230 struct packet_info pi;
Philipp Reisner257d0af2011-01-26 12:15:29 +01005231 int rv;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005232 void *buf = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005233 int received = 0;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005234 unsigned int header_size = drbd_header_size(tconn);
5235 int expect = header_size;
Philipp Reisner44ed1672011-04-19 17:10:19 +02005236 bool ping_timeout_active = false;
5237 struct net_conf *nc;
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005238 int ping_timeo, tcp_cork, ping_int;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005239
Philipp Reisnerb411b362009-09-25 16:07:19 -07005240 current->policy = SCHED_RR; /* Make this a realtime task! */
5241 current->rt_priority = 2; /* more important than all other tasks */
5242
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +01005243 while (get_t_state(thi) == RUNNING) {
Philipp Reisner80822282011-02-08 12:46:30 +01005244 drbd_thread_current_set_cpu(thi);
Philipp Reisner44ed1672011-04-19 17:10:19 +02005245
5246 rcu_read_lock();
5247 nc = rcu_dereference(tconn->net_conf);
5248 ping_timeo = nc->ping_timeo;
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005249 tcp_cork = nc->tcp_cork;
Philipp Reisner44ed1672011-04-19 17:10:19 +02005250 ping_int = nc->ping_int;
5251 rcu_read_unlock();
5252
Philipp Reisner32862ec2011-02-08 16:41:01 +01005253 if (test_and_clear_bit(SEND_PING, &tconn->flags)) {
Andreas Gruenbachera17647a2011-04-01 12:49:42 +02005254 if (drbd_send_ping(tconn)) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005255 conn_err(tconn, "drbd_send_ping has failed\n");
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01005256 goto reconnect;
5257 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02005258 tconn->meta.socket->sk->sk_rcvtimeo = ping_timeo * HZ / 10;
5259 ping_timeout_active = true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005260 }
5261
Philipp Reisner32862ec2011-02-08 16:41:01 +01005262 /* TODO: conditionally cork; it may hurt latency if we cork without
5263 much to send */
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005264 if (tcp_cork)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005265 drbd_tcp_cork(tconn->meta.socket);
Andreas Gruenbachera990be42011-04-06 17:56:48 +02005266 if (tconn_finish_peer_reqs(tconn)) {
5267 conn_err(tconn, "tconn_finish_peer_reqs() failed\n");
Philipp Reisner32862ec2011-02-08 16:41:01 +01005268 goto reconnect;
Philipp Reisner082a3432011-03-15 16:05:42 +01005269 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005270 /* but unconditionally uncork unless disabled */
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005271 if (tcp_cork)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005272 drbd_tcp_uncork(tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005273
5274 /* short circuit, recv_msg would return EINTR anyways. */
5275 if (signal_pending(current))
5276 continue;
5277
Philipp Reisner32862ec2011-02-08 16:41:01 +01005278 rv = drbd_recv_short(tconn->meta.socket, buf, expect-received, 0);
5279 clear_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005280
5281 flush_signals(current);
5282
5283 /* Note:
5284 * -EINTR (on meta) we got a signal
5285 * -EAGAIN (on meta) rcvtimeo expired
5286 * -ECONNRESET other side closed the connection
5287 * -ERESTARTSYS (on data) we got a signal
5288 * rv < 0 other than above: unexpected error!
5289 * rv == expected: full header or command
5290 * rv < expected: "woken" by signal during receive
5291 * rv == 0 : "connection shut down by peer"
5292 */
5293 if (likely(rv > 0)) {
5294 received += rv;
5295 buf += rv;
5296 } else if (rv == 0) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005297 conn_err(tconn, "meta connection shut down by peer.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005298 goto reconnect;
5299 } else if (rv == -EAGAIN) {
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02005300 /* If the data socket received something meanwhile,
5301 * that is good enough: peer is still alive. */
Philipp Reisner32862ec2011-02-08 16:41:01 +01005302 if (time_after(tconn->last_received,
5303 jiffies - tconn->meta.socket->sk->sk_rcvtimeo))
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02005304 continue;
Lars Ellenbergf36af182011-03-09 22:44:55 +01005305 if (ping_timeout_active) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005306 conn_err(tconn, "PingAck did not arrive in time.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005307 goto reconnect;
5308 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005309 set_bit(SEND_PING, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005310 continue;
5311 } else if (rv == -EINTR) {
5312 continue;
5313 } else {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005314 conn_err(tconn, "sock_recvmsg returned %d\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005315 goto reconnect;
5316 }
5317
5318 if (received == expect && cmd == NULL) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005319 if (decode_header(tconn, tconn->meta.rbuf, &pi))
Philipp Reisnerb411b362009-09-25 16:07:19 -07005320 goto reconnect;
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005321 cmd = &asender_tbl[pi.cmd];
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005322 if (pi.cmd >= ARRAY_SIZE(asender_tbl) || !cmd->fn) {
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02005323 conn_err(tconn, "Unexpected meta packet %s (0x%04x)\n",
5324 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005325 goto disconnect;
5326 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005327 expect = header_size + cmd->pkt_size;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005328 if (pi.size != expect - header_size) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005329 conn_err(tconn, "Wrong packet size on meta (c: %d, l: %d)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01005330 pi.cmd, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005331 goto reconnect;
Philipp Reisner257d0af2011-01-26 12:15:29 +01005332 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005333 }
5334 if (received == expect) {
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005335 bool err;
Philipp Reisnera4fbda82011-03-16 11:13:17 +01005336
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005337 err = cmd->fn(tconn, &pi);
5338 if (err) {
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005339 conn_err(tconn, "%pf failed\n", cmd->fn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005340 goto reconnect;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005341 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005342
Philipp Reisnera4fbda82011-03-16 11:13:17 +01005343 tconn->last_received = jiffies;
5344
Philipp Reisner44ed1672011-04-19 17:10:19 +02005345 if (cmd == &asender_tbl[P_PING_ACK]) {
5346 /* restore idle timeout */
5347 tconn->meta.socket->sk->sk_rcvtimeo = ping_int * HZ;
5348 ping_timeout_active = false;
5349 }
Lars Ellenbergf36af182011-03-09 22:44:55 +01005350
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005351 buf = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005352 received = 0;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005353 expect = header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005354 cmd = NULL;
5355 }
5356 }
5357
5358 if (0) {
5359reconnect:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01005360 conn_request_state(tconn, NS(conn, C_NETWORK_FAILURE), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005361 }
5362 if (0) {
5363disconnect:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01005364 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005365 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005366 clear_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005367
Philipp Reisner32862ec2011-02-08 16:41:01 +01005368 conn_info(tconn, "asender terminated\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005369
5370 return 0;
5371}