blob: cd140bc0b56fd3d9bd0a3c5751c0f730c7629bf5 [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
68static enum finish_epoch drbd_may_finish_epoch(struct drbd_conf *, struct drbd_epoch *, enum epoch_event);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +010069static int e_end_block(struct drbd_work *, int);
Philipp Reisnerb411b362009-09-25 16:07:19 -070070
Philipp Reisnerb411b362009-09-25 16:07:19 -070071
72#define GFP_TRY (__GFP_HIGHMEM | __GFP_NOWARN)
73
Lars Ellenberg45bb9122010-05-14 17:10:48 +020074/*
75 * some helper functions to deal with single linked page lists,
76 * page->private being our "next" pointer.
77 */
78
79/* If at least n pages are linked at head, get n pages off.
80 * Otherwise, don't modify head, and return NULL.
81 * Locking is the responsibility of the caller.
82 */
83static struct page *page_chain_del(struct page **head, int n)
84{
85 struct page *page;
86 struct page *tmp;
87
88 BUG_ON(!n);
89 BUG_ON(!head);
90
91 page = *head;
Philipp Reisner23ce4222010-05-20 13:35:31 +020092
93 if (!page)
94 return NULL;
95
Lars Ellenberg45bb9122010-05-14 17:10:48 +020096 while (page) {
97 tmp = page_chain_next(page);
98 if (--n == 0)
99 break; /* found sufficient pages */
100 if (tmp == NULL)
101 /* insufficient pages, don't use any of them. */
102 return NULL;
103 page = tmp;
104 }
105
106 /* add end of list marker for the returned list */
107 set_page_private(page, 0);
108 /* actual return value, and adjustment of head */
109 page = *head;
110 *head = tmp;
111 return page;
112}
113
114/* may be used outside of locks to find the tail of a (usually short)
115 * "private" page chain, before adding it back to a global chain head
116 * with page_chain_add() under a spinlock. */
117static struct page *page_chain_tail(struct page *page, int *len)
118{
119 struct page *tmp;
120 int i = 1;
121 while ((tmp = page_chain_next(page)))
122 ++i, page = tmp;
123 if (len)
124 *len = i;
125 return page;
126}
127
128static int page_chain_free(struct page *page)
129{
130 struct page *tmp;
131 int i = 0;
132 page_chain_for_each_safe(page, tmp) {
133 put_page(page);
134 ++i;
135 }
136 return i;
137}
138
139static void page_chain_add(struct page **head,
140 struct page *chain_first, struct page *chain_last)
141{
142#if 1
143 struct page *tmp;
144 tmp = page_chain_tail(chain_first, NULL);
145 BUG_ON(tmp != chain_last);
146#endif
147
148 /* add chain to head */
149 set_page_private(chain_last, (unsigned long)*head);
150 *head = chain_first;
151}
152
Andreas Gruenbacher18c2d522011-04-07 21:08:50 +0200153static struct page *__drbd_alloc_pages(struct drbd_conf *mdev,
154 unsigned int number)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700155{
156 struct page *page = NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200157 struct page *tmp = NULL;
Andreas Gruenbacher18c2d522011-04-07 21:08:50 +0200158 unsigned int i = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700159
160 /* Yes, testing drbd_pp_vacant outside the lock is racy.
161 * So what. It saves a spin_lock. */
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200162 if (drbd_pp_vacant >= number) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700163 spin_lock(&drbd_pp_lock);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200164 page = page_chain_del(&drbd_pp_pool, number);
165 if (page)
166 drbd_pp_vacant -= number;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700167 spin_unlock(&drbd_pp_lock);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200168 if (page)
169 return page;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700170 }
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200171
Philipp Reisnerb411b362009-09-25 16:07:19 -0700172 /* GFP_TRY, because we must not cause arbitrary write-out: in a DRBD
173 * "criss-cross" setup, that might cause write-out on some other DRBD,
174 * which in turn might block on the other node at this very place. */
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200175 for (i = 0; i < number; i++) {
176 tmp = alloc_page(GFP_TRY);
177 if (!tmp)
178 break;
179 set_page_private(tmp, (unsigned long)page);
180 page = tmp;
181 }
182
183 if (i == number)
184 return page;
185
186 /* Not enough pages immediately available this time.
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +0200187 * No need to jump around here, drbd_alloc_pages will retry this
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200188 * function "soon". */
189 if (page) {
190 tmp = page_chain_tail(page, NULL);
191 spin_lock(&drbd_pp_lock);
192 page_chain_add(&drbd_pp_pool, page, tmp);
193 drbd_pp_vacant += i;
194 spin_unlock(&drbd_pp_lock);
195 }
196 return NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700197}
198
Andreas Gruenbachera990be42011-04-06 17:56:48 +0200199static void reclaim_finished_net_peer_reqs(struct drbd_conf *mdev,
200 struct list_head *to_be_freed)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700201{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100202 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700203 struct list_head *le, *tle;
204
205 /* The EEs are always appended to the end of the list. Since
206 they are sent in order over the wire, they have to finish
207 in order. As soon as we see the first not finished we can
208 stop to examine the list... */
209
210 list_for_each_safe(le, tle, &mdev->net_ee) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100211 peer_req = list_entry(le, struct drbd_peer_request, w.list);
Andreas Gruenbacher045417f2011-04-07 21:34:24 +0200212 if (drbd_peer_req_has_active_page(peer_req))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700213 break;
214 list_move(le, to_be_freed);
215 }
216}
217
218static void drbd_kick_lo_and_reclaim_net(struct drbd_conf *mdev)
219{
220 LIST_HEAD(reclaimed);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100221 struct drbd_peer_request *peer_req, *t;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700222
Philipp Reisner87eeee42011-01-19 14:16:30 +0100223 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbachera990be42011-04-06 17:56:48 +0200224 reclaim_finished_net_peer_reqs(mdev, &reclaimed);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100225 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700226
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100227 list_for_each_entry_safe(peer_req, t, &reclaimed, w.list)
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200228 drbd_free_net_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700229}
230
231/**
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +0200232 * drbd_alloc_pages() - Returns @number pages, retries forever (or until signalled)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700233 * @mdev: DRBD device.
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200234 * @number: number of pages requested
235 * @retry: whether to retry, if not enough pages are available right now
Philipp Reisnerb411b362009-09-25 16:07:19 -0700236 *
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200237 * Tries to allocate number pages, first from our own page pool, then from
238 * the kernel, unless this allocation would exceed the max_buffers setting.
239 * Possibly retry until DRBD frees sufficient pages somewhere else.
240 *
241 * Returns a page chain linked via page->private.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700242 */
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +0200243struct page *drbd_alloc_pages(struct drbd_conf *mdev, unsigned int number,
244 bool retry)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700245{
246 struct page *page = NULL;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200247 struct net_conf *nc;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700248 DEFINE_WAIT(wait);
Philipp Reisner44ed1672011-04-19 17:10:19 +0200249 int mxb;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700250
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200251 /* Yes, we may run up to @number over max_buffers. If we
252 * follow it strictly, the admin will get it wrong anyways. */
Philipp Reisner44ed1672011-04-19 17:10:19 +0200253 rcu_read_lock();
254 nc = rcu_dereference(mdev->tconn->net_conf);
255 mxb = nc ? nc->max_buffers : 1000000;
256 rcu_read_unlock();
257
258 if (atomic_read(&mdev->pp_in_use) < mxb)
Andreas Gruenbacher18c2d522011-04-07 21:08:50 +0200259 page = __drbd_alloc_pages(mdev, number);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700260
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200261 while (page == NULL) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700262 prepare_to_wait(&drbd_pp_wait, &wait, TASK_INTERRUPTIBLE);
263
264 drbd_kick_lo_and_reclaim_net(mdev);
265
Philipp Reisner44ed1672011-04-19 17:10:19 +0200266 if (atomic_read(&mdev->pp_in_use) < mxb) {
Andreas Gruenbacher18c2d522011-04-07 21:08:50 +0200267 page = __drbd_alloc_pages(mdev, number);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700268 if (page)
269 break;
270 }
271
272 if (!retry)
273 break;
274
275 if (signal_pending(current)) {
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +0200276 dev_warn(DEV, "drbd_alloc_pages interrupted!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700277 break;
278 }
279
280 schedule();
281 }
282 finish_wait(&drbd_pp_wait, &wait);
283
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200284 if (page)
285 atomic_add(number, &mdev->pp_in_use);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700286 return page;
287}
288
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +0200289/* Must not be used from irq, as that may deadlock: see drbd_alloc_pages.
Philipp Reisner87eeee42011-01-19 14:16:30 +0100290 * Is also used from inside an other spin_lock_irq(&mdev->tconn->req_lock);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200291 * Either links the page chain back to the global pool,
292 * or returns all pages to the system. */
Andreas Gruenbacher5cc287e2011-04-07 21:02:59 +0200293static void drbd_free_pages(struct drbd_conf *mdev, struct page *page, int is_net)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700294{
Lars Ellenberg435f0742010-09-06 12:30:25 +0200295 atomic_t *a = is_net ? &mdev->pp_in_use_by_net : &mdev->pp_in_use;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700296 int i;
Lars Ellenberg435f0742010-09-06 12:30:25 +0200297
Philipp Reisner81a5d602011-02-22 19:53:16 -0500298 if (drbd_pp_vacant > (DRBD_MAX_BIO_SIZE/PAGE_SIZE) * minor_count)
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200299 i = page_chain_free(page);
300 else {
301 struct page *tmp;
302 tmp = page_chain_tail(page, &i);
303 spin_lock(&drbd_pp_lock);
304 page_chain_add(&drbd_pp_pool, page, tmp);
305 drbd_pp_vacant += i;
306 spin_unlock(&drbd_pp_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700307 }
Lars Ellenberg435f0742010-09-06 12:30:25 +0200308 i = atomic_sub_return(i, a);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200309 if (i < 0)
Lars Ellenberg435f0742010-09-06 12:30:25 +0200310 dev_warn(DEV, "ASSERTION FAILED: %s: %d < 0\n",
311 is_net ? "pp_in_use_by_net" : "pp_in_use", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700312 wake_up(&drbd_pp_wait);
313}
314
315/*
316You need to hold the req_lock:
317 _drbd_wait_ee_list_empty()
318
319You must not have the req_lock:
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200320 drbd_free_peer_req()
Andreas Gruenbacher0db55362011-04-06 16:09:15 +0200321 drbd_alloc_peer_req()
Andreas Gruenbacher7721f562011-04-06 17:14:02 +0200322 drbd_free_peer_reqs()
Philipp Reisnerb411b362009-09-25 16:07:19 -0700323 drbd_ee_fix_bhs()
Andreas Gruenbachera990be42011-04-06 17:56:48 +0200324 drbd_finish_peer_reqs()
Philipp Reisnerb411b362009-09-25 16:07:19 -0700325 drbd_clear_done_ee()
326 drbd_wait_ee_list_empty()
327*/
328
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +0100329struct drbd_peer_request *
Andreas Gruenbacher0db55362011-04-06 16:09:15 +0200330drbd_alloc_peer_req(struct drbd_conf *mdev, u64 id, sector_t sector,
331 unsigned int data_size, gfp_t gfp_mask) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700332{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100333 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700334 struct page *page;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200335 unsigned nr_pages = (data_size + PAGE_SIZE -1) >> PAGE_SHIFT;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700336
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +0100337 if (drbd_insert_fault(mdev, DRBD_FAULT_AL_EE))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700338 return NULL;
339
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100340 peer_req = mempool_alloc(drbd_ee_mempool, gfp_mask & ~__GFP_HIGHMEM);
341 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700342 if (!(gfp_mask & __GFP_NOWARN))
Andreas Gruenbacher0db55362011-04-06 16:09:15 +0200343 dev_err(DEV, "%s: allocation failed\n", __func__);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700344 return NULL;
345 }
346
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +0200347 page = drbd_alloc_pages(mdev, nr_pages, (gfp_mask & __GFP_WAIT));
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200348 if (!page)
349 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700350
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100351 drbd_clear_interval(&peer_req->i);
352 peer_req->i.size = data_size;
353 peer_req->i.sector = sector;
354 peer_req->i.local = false;
355 peer_req->i.waiting = false;
Andreas Gruenbacher53840642011-01-28 10:31:04 +0100356
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100357 peer_req->epoch = NULL;
Philipp Reisnera21e9292011-02-08 15:08:49 +0100358 peer_req->w.mdev = mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100359 peer_req->pages = page;
360 atomic_set(&peer_req->pending_bios, 0);
361 peer_req->flags = 0;
Andreas Gruenbacher9a8e7752011-01-11 14:04:09 +0100362 /*
363 * The block_id is opaque to the receiver. It is not endianness
364 * converted, and sent back to the sender unchanged.
365 */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100366 peer_req->block_id = id;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700367
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100368 return peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700369
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200370 fail:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100371 mempool_free(peer_req, drbd_ee_mempool);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700372 return NULL;
373}
374
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200375void __drbd_free_peer_req(struct drbd_conf *mdev, struct drbd_peer_request *peer_req,
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +0100376 int is_net)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700377{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100378 if (peer_req->flags & EE_HAS_DIGEST)
379 kfree(peer_req->digest);
Andreas Gruenbacher5cc287e2011-04-07 21:02:59 +0200380 drbd_free_pages(mdev, peer_req->pages, is_net);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100381 D_ASSERT(atomic_read(&peer_req->pending_bios) == 0);
382 D_ASSERT(drbd_interval_empty(&peer_req->i));
383 mempool_free(peer_req, drbd_ee_mempool);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700384}
385
Andreas Gruenbacher7721f562011-04-06 17:14:02 +0200386int drbd_free_peer_reqs(struct drbd_conf *mdev, struct list_head *list)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700387{
388 LIST_HEAD(work_list);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100389 struct drbd_peer_request *peer_req, *t;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700390 int count = 0;
Lars Ellenberg435f0742010-09-06 12:30:25 +0200391 int is_net = list == &mdev->net_ee;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700392
Philipp Reisner87eeee42011-01-19 14:16:30 +0100393 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700394 list_splice_init(list, &work_list);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100395 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700396
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100397 list_for_each_entry_safe(peer_req, t, &work_list, w.list) {
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200398 __drbd_free_peer_req(mdev, peer_req, is_net);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700399 count++;
400 }
401 return count;
402}
403
Andreas Gruenbachera990be42011-04-06 17:56:48 +0200404/*
405 * See also comments in _req_mod(,BARRIER_ACKED) and receive_Barrier.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700406 */
Andreas Gruenbachera990be42011-04-06 17:56:48 +0200407static int drbd_finish_peer_reqs(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700408{
409 LIST_HEAD(work_list);
410 LIST_HEAD(reclaimed);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100411 struct drbd_peer_request *peer_req, *t;
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100412 int err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700413
Philipp Reisner87eeee42011-01-19 14:16:30 +0100414 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbachera990be42011-04-06 17:56:48 +0200415 reclaim_finished_net_peer_reqs(mdev, &reclaimed);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700416 list_splice_init(&mdev->done_ee, &work_list);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100417 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700418
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100419 list_for_each_entry_safe(peer_req, t, &reclaimed, w.list)
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200420 drbd_free_net_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700421
422 /* possible callbacks here:
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +0100423 * e_end_block, and e_end_resync_block, e_send_discard_write.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700424 * all ignore the last argument.
425 */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100426 list_for_each_entry_safe(peer_req, t, &work_list, w.list) {
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100427 int err2;
428
Philipp Reisnerb411b362009-09-25 16:07:19 -0700429 /* list_del not necessary, next/prev members not touched */
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100430 err2 = peer_req->w.cb(&peer_req->w, !!err);
431 if (!err)
432 err = err2;
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200433 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700434 }
435 wake_up(&mdev->ee_wait);
436
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100437 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700438}
439
Andreas Gruenbacherd4da1532011-04-07 00:06:56 +0200440static void _drbd_wait_ee_list_empty(struct drbd_conf *mdev,
441 struct list_head *head)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700442{
443 DEFINE_WAIT(wait);
444
445 /* avoids spin_lock/unlock
446 * and calling prepare_to_wait in the fast path */
447 while (!list_empty(head)) {
448 prepare_to_wait(&mdev->ee_wait, &wait, TASK_UNINTERRUPTIBLE);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100449 spin_unlock_irq(&mdev->tconn->req_lock);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100450 io_schedule();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700451 finish_wait(&mdev->ee_wait, &wait);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100452 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700453 }
454}
455
Andreas Gruenbacherd4da1532011-04-07 00:06:56 +0200456static void drbd_wait_ee_list_empty(struct drbd_conf *mdev,
457 struct list_head *head)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700458{
Philipp Reisner87eeee42011-01-19 14:16:30 +0100459 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700460 _drbd_wait_ee_list_empty(mdev, head);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100461 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700462}
463
464/* see also kernel_accept; which is only present since 2.6.18.
465 * also we want to log which part of it failed, exactly */
Philipp Reisner76536202011-02-07 14:09:54 +0100466static int drbd_accept(const char **what, struct socket *sock, struct socket **newsock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700467{
468 struct sock *sk = sock->sk;
469 int err = 0;
470
471 *what = "listen";
472 err = sock->ops->listen(sock, 5);
473 if (err < 0)
474 goto out;
475
476 *what = "sock_create_lite";
477 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
478 newsock);
479 if (err < 0)
480 goto out;
481
482 *what = "accept";
483 err = sock->ops->accept(sock, *newsock, 0);
484 if (err < 0) {
485 sock_release(*newsock);
486 *newsock = NULL;
487 goto out;
488 }
489 (*newsock)->ops = sock->ops;
490
491out:
492 return err;
493}
494
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100495static int drbd_recv_short(struct socket *sock, void *buf, size_t size, int flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700496{
497 mm_segment_t oldfs;
498 struct kvec iov = {
499 .iov_base = buf,
500 .iov_len = size,
501 };
502 struct msghdr msg = {
503 .msg_iovlen = 1,
504 .msg_iov = (struct iovec *)&iov,
505 .msg_flags = (flags ? flags : MSG_WAITALL | MSG_NOSIGNAL)
506 };
507 int rv;
508
509 oldfs = get_fs();
510 set_fs(KERNEL_DS);
511 rv = sock_recvmsg(sock, &msg, size, msg.msg_flags);
512 set_fs(oldfs);
513
514 return rv;
515}
516
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100517static int drbd_recv(struct drbd_tconn *tconn, void *buf, size_t size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700518{
519 mm_segment_t oldfs;
520 struct kvec iov = {
521 .iov_base = buf,
522 .iov_len = size,
523 };
524 struct msghdr msg = {
525 .msg_iovlen = 1,
526 .msg_iov = (struct iovec *)&iov,
527 .msg_flags = MSG_WAITALL | MSG_NOSIGNAL
528 };
529 int rv;
530
531 oldfs = get_fs();
532 set_fs(KERNEL_DS);
533
534 for (;;) {
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100535 rv = sock_recvmsg(tconn->data.socket, &msg, size, msg.msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700536 if (rv == size)
537 break;
538
539 /* Note:
540 * ECONNRESET other side closed the connection
541 * ERESTARTSYS (on sock) we got a signal
542 */
543
544 if (rv < 0) {
545 if (rv == -ECONNRESET)
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100546 conn_info(tconn, "sock was reset by peer\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700547 else if (rv != -ERESTARTSYS)
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100548 conn_err(tconn, "sock_recvmsg returned %d\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700549 break;
550 } else if (rv == 0) {
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100551 conn_info(tconn, "sock was shut down by peer\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700552 break;
553 } else {
554 /* signal came in, or peer/link went down,
555 * after we read a partial message
556 */
557 /* D_ASSERT(signal_pending(current)); */
558 break;
559 }
560 };
561
562 set_fs(oldfs);
563
564 if (rv != size)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100565 conn_request_state(tconn, NS(conn, C_BROKEN_PIPE), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700566
567 return rv;
568}
569
Andreas Gruenbacherc6967742011-03-17 17:15:20 +0100570static int drbd_recv_all(struct drbd_tconn *tconn, void *buf, size_t size)
571{
572 int err;
573
574 err = drbd_recv(tconn, buf, size);
575 if (err != size) {
576 if (err >= 0)
577 err = -EIO;
578 } else
579 err = 0;
580 return err;
581}
582
Andreas Gruenbachera5c31902011-03-24 03:28:04 +0100583static int drbd_recv_all_warn(struct drbd_tconn *tconn, void *buf, size_t size)
584{
585 int err;
586
587 err = drbd_recv_all(tconn, buf, size);
588 if (err && !signal_pending(current))
589 conn_warn(tconn, "short read (expected size %d)\n", (int)size);
590 return err;
591}
592
Lars Ellenberg5dbf1672010-05-25 16:18:01 +0200593/* quoting tcp(7):
594 * On individual connections, the socket buffer size must be set prior to the
595 * listen(2) or connect(2) calls in order to have it take effect.
596 * This is our wrapper to do so.
597 */
598static void drbd_setbufsize(struct socket *sock, unsigned int snd,
599 unsigned int rcv)
600{
601 /* open coded SO_SNDBUF, SO_RCVBUF */
602 if (snd) {
603 sock->sk->sk_sndbuf = snd;
604 sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
605 }
606 if (rcv) {
607 sock->sk->sk_rcvbuf = rcv;
608 sock->sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
609 }
610}
611
Philipp Reisnereac3e992011-02-07 14:05:07 +0100612static struct socket *drbd_try_connect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700613{
614 const char *what;
615 struct socket *sock;
616 struct sockaddr_in6 src_in6;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200617 struct sockaddr_in6 peer_in6;
618 struct net_conf *nc;
619 int err, peer_addr_len, my_addr_len;
Andreas Gruenbacher69ef82d2011-05-11 14:34:35 +0200620 int sndbuf_size, rcvbuf_size, connect_int;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700621 int disconnect_on_error = 1;
622
Philipp Reisner44ed1672011-04-19 17:10:19 +0200623 rcu_read_lock();
624 nc = rcu_dereference(tconn->net_conf);
625 if (!nc) {
626 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700627 return NULL;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200628 }
Philipp Reisner44ed1672011-04-19 17:10:19 +0200629 sndbuf_size = nc->sndbuf_size;
630 rcvbuf_size = nc->rcvbuf_size;
Andreas Gruenbacher69ef82d2011-05-11 14:34:35 +0200631 connect_int = nc->connect_int;
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200632 rcu_read_unlock();
Philipp Reisner44ed1672011-04-19 17:10:19 +0200633
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200634 my_addr_len = min_t(int, tconn->my_addr_len, sizeof(src_in6));
635 memcpy(&src_in6, &tconn->my_addr, my_addr_len);
Philipp Reisner44ed1672011-04-19 17:10:19 +0200636
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200637 if (((struct sockaddr *)&tconn->my_addr)->sa_family == AF_INET6)
Philipp Reisner44ed1672011-04-19 17:10:19 +0200638 src_in6.sin6_port = 0;
639 else
640 ((struct sockaddr_in *)&src_in6)->sin_port = 0; /* AF_INET & AF_SCI */
641
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200642 peer_addr_len = min_t(int, tconn->peer_addr_len, sizeof(src_in6));
643 memcpy(&peer_in6, &tconn->peer_addr, peer_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700644
645 what = "sock_create_kern";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200646 err = sock_create_kern(((struct sockaddr *)&src_in6)->sa_family,
647 SOCK_STREAM, IPPROTO_TCP, &sock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700648 if (err < 0) {
649 sock = NULL;
650 goto out;
651 }
652
653 sock->sk->sk_rcvtimeo =
Andreas Gruenbacher69ef82d2011-05-11 14:34:35 +0200654 sock->sk->sk_sndtimeo = connect_int * HZ;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200655 drbd_setbufsize(sock, sndbuf_size, rcvbuf_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700656
657 /* explicitly bind to the configured IP as source IP
658 * for the outgoing connections.
659 * This is needed for multihomed hosts and to be
660 * able to use lo: interfaces for drbd.
661 * Make sure to use 0 as port number, so linux selects
662 * a free one dynamically.
663 */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700664 what = "bind before connect";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200665 err = sock->ops->bind(sock, (struct sockaddr *) &src_in6, my_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700666 if (err < 0)
667 goto out;
668
669 /* connect may fail, peer not yet available.
670 * stay C_WF_CONNECTION, don't go Disconnecting! */
671 disconnect_on_error = 0;
672 what = "connect";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200673 err = sock->ops->connect(sock, (struct sockaddr *) &peer_in6, peer_addr_len, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700674
675out:
676 if (err < 0) {
677 if (sock) {
678 sock_release(sock);
679 sock = NULL;
680 }
681 switch (-err) {
682 /* timeout, busy, signal pending */
683 case ETIMEDOUT: case EAGAIN: case EINPROGRESS:
684 case EINTR: case ERESTARTSYS:
685 /* peer not (yet) available, network problem */
686 case ECONNREFUSED: case ENETUNREACH:
687 case EHOSTDOWN: case EHOSTUNREACH:
688 disconnect_on_error = 0;
689 break;
690 default:
Philipp Reisnereac3e992011-02-07 14:05:07 +0100691 conn_err(tconn, "%s failed, err = %d\n", what, err);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700692 }
693 if (disconnect_on_error)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100694 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700695 }
Philipp Reisner44ed1672011-04-19 17:10:19 +0200696
Philipp Reisnerb411b362009-09-25 16:07:19 -0700697 return sock;
698}
699
Philipp Reisner76536202011-02-07 14:09:54 +0100700static struct socket *drbd_wait_for_connect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700701{
Philipp Reisner44ed1672011-04-19 17:10:19 +0200702 int timeo, err, my_addr_len;
Andreas Gruenbacher69ef82d2011-05-11 14:34:35 +0200703 int sndbuf_size, rcvbuf_size, connect_int;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700704 struct socket *s_estab = NULL, *s_listen;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200705 struct sockaddr_in6 my_addr;
706 struct net_conf *nc;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700707 const char *what;
708
Philipp Reisner44ed1672011-04-19 17:10:19 +0200709 rcu_read_lock();
710 nc = rcu_dereference(tconn->net_conf);
711 if (!nc) {
712 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700713 return NULL;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200714 }
Philipp Reisner44ed1672011-04-19 17:10:19 +0200715 sndbuf_size = nc->sndbuf_size;
716 rcvbuf_size = nc->rcvbuf_size;
Andreas Gruenbacher69ef82d2011-05-11 14:34:35 +0200717 connect_int = nc->connect_int;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200718 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700719
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200720 my_addr_len = min_t(int, tconn->my_addr_len, sizeof(struct sockaddr_in6));
721 memcpy(&my_addr, &tconn->my_addr, my_addr_len);
722
Philipp Reisnerb411b362009-09-25 16:07:19 -0700723 what = "sock_create_kern";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200724 err = sock_create_kern(((struct sockaddr *)&my_addr)->sa_family,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700725 SOCK_STREAM, IPPROTO_TCP, &s_listen);
726 if (err) {
727 s_listen = NULL;
728 goto out;
729 }
730
Andreas Gruenbacher69ef82d2011-05-11 14:34:35 +0200731 timeo = connect_int * HZ;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700732 timeo += (random32() & 1) ? timeo / 7 : -timeo / 7; /* 28.5% random jitter */
733
734 s_listen->sk->sk_reuse = 1; /* SO_REUSEADDR */
735 s_listen->sk->sk_rcvtimeo = timeo;
736 s_listen->sk->sk_sndtimeo = timeo;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200737 drbd_setbufsize(s_listen, sndbuf_size, rcvbuf_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700738
739 what = "bind before listen";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200740 err = s_listen->ops->bind(s_listen, (struct sockaddr *)&my_addr, my_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700741 if (err < 0)
742 goto out;
743
Philipp Reisner76536202011-02-07 14:09:54 +0100744 err = drbd_accept(&what, s_listen, &s_estab);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700745
746out:
747 if (s_listen)
748 sock_release(s_listen);
749 if (err < 0) {
750 if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) {
Philipp Reisner76536202011-02-07 14:09:54 +0100751 conn_err(tconn, "%s failed, err = %d\n", what, err);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100752 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700753 }
754 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700755
756 return s_estab;
757}
758
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200759static int decode_header(struct drbd_tconn *, void *, struct packet_info *);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700760
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200761static int send_first_packet(struct drbd_tconn *tconn, struct drbd_socket *sock,
762 enum drbd_packet cmd)
763{
764 if (!conn_prepare_command(tconn, sock))
765 return -EIO;
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200766 return conn_send_command(tconn, sock, cmd, 0, NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700767}
768
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200769static int receive_first_packet(struct drbd_tconn *tconn, struct socket *sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700770{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200771 unsigned int header_size = drbd_header_size(tconn);
772 struct packet_info pi;
773 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700774
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200775 err = drbd_recv_short(sock, tconn->data.rbuf, header_size, 0);
776 if (err != header_size) {
777 if (err >= 0)
778 err = -EIO;
779 return err;
780 }
781 err = decode_header(tconn, tconn->data.rbuf, &pi);
782 if (err)
783 return err;
784 return pi.cmd;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700785}
786
787/**
788 * drbd_socket_okay() - Free the socket if its connection is not okay
Philipp Reisnerb411b362009-09-25 16:07:19 -0700789 * @sock: pointer to the pointer to the socket.
790 */
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100791static int drbd_socket_okay(struct socket **sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700792{
793 int rr;
794 char tb[4];
795
796 if (!*sock)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100797 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700798
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100799 rr = drbd_recv_short(*sock, tb, 4, MSG_DONTWAIT | MSG_PEEK);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700800
801 if (rr > 0 || rr == -EAGAIN) {
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100802 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700803 } else {
804 sock_release(*sock);
805 *sock = NULL;
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100806 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700807 }
808}
Philipp Reisner2325eb62011-03-15 16:56:18 +0100809/* Gets called if a connection is established, or if a new minor gets created
810 in a connection */
Philipp Reisnerc141ebd2011-05-05 16:13:10 +0200811int drbd_connected(struct drbd_conf *mdev)
Philipp Reisner907599e2011-02-08 11:25:37 +0100812{
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100813 int err;
Philipp Reisner907599e2011-02-08 11:25:37 +0100814
815 atomic_set(&mdev->packet_seq, 0);
816 mdev->peer_seq = 0;
817
Philipp Reisner8410da82011-02-11 20:11:10 +0100818 mdev->state_mutex = mdev->tconn->agreed_pro_version < 100 ?
819 &mdev->tconn->cstate_mutex :
820 &mdev->own_state_mutex;
821
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100822 err = drbd_send_sync_param(mdev);
823 if (!err)
824 err = drbd_send_sizes(mdev, 0, 0);
825 if (!err)
826 err = drbd_send_uuids(mdev);
827 if (!err)
828 err = drbd_send_state(mdev);
Philipp Reisner907599e2011-02-08 11:25:37 +0100829 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
830 clear_bit(RESIZE_PENDING, &mdev->flags);
Philipp Reisner8b924f12011-03-01 11:08:28 +0100831 mod_timer(&mdev->request_timer, jiffies + HZ); /* just start it here. */
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100832 return err;
Philipp Reisner907599e2011-02-08 11:25:37 +0100833}
834
Philipp Reisnerb411b362009-09-25 16:07:19 -0700835/*
836 * return values:
837 * 1 yes, we have a valid connection
838 * 0 oops, did not work out, please try again
839 * -1 peer talks different language,
840 * no point in trying again, please go standalone.
841 * -2 We do not have a network config...
842 */
Philipp Reisner81fa2e62011-05-04 15:10:30 +0200843static int conn_connect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700844{
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200845 struct socket *sock, *msock;
Philipp Reisnerc141ebd2011-05-05 16:13:10 +0200846 struct drbd_conf *mdev;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200847 struct net_conf *nc;
Philipp Reisnerc141ebd2011-05-05 16:13:10 +0200848 int vnr, timeout, try, h, ok;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700849
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100850 if (conn_request_state(tconn, NS(conn, C_WF_CONNECTION), CS_VERBOSE) < SS_SUCCESS)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700851 return -2;
852
Philipp Reisner907599e2011-02-08 11:25:37 +0100853 clear_bit(DISCARD_CONCURRENT, &tconn->flags);
Andreas Gruenbacher0916e0e2011-03-21 14:10:15 +0100854
855 /* Assume that the peer only understands protocol 80 until we know better. */
856 tconn->agreed_pro_version = 80;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700857
Philipp Reisnerb411b362009-09-25 16:07:19 -0700858 do {
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200859 struct socket *s;
860
Philipp Reisnerb411b362009-09-25 16:07:19 -0700861 for (try = 0;;) {
862 /* 3 tries, this should take less than a second! */
Philipp Reisner907599e2011-02-08 11:25:37 +0100863 s = drbd_try_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700864 if (s || ++try >= 3)
865 break;
866 /* give the other side time to call bind() & listen() */
Philipp Reisner20ee6392011-01-18 15:28:59 +0100867 schedule_timeout_interruptible(HZ / 10);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700868 }
869
870 if (s) {
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200871 if (!tconn->data.socket) {
872 tconn->data.socket = s;
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200873 send_first_packet(tconn, &tconn->data, P_INITIAL_DATA);
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200874 } else if (!tconn->meta.socket) {
875 tconn->meta.socket = s;
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200876 send_first_packet(tconn, &tconn->meta, P_INITIAL_META);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700877 } else {
Philipp Reisner81fa2e62011-05-04 15:10:30 +0200878 conn_err(tconn, "Logic error in conn_connect()\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700879 goto out_release_sockets;
880 }
881 }
882
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200883 if (tconn->data.socket && tconn->meta.socket) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100884 schedule_timeout_interruptible(tconn->net_conf->ping_timeo*HZ/10);
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200885 ok = drbd_socket_okay(&tconn->data.socket);
886 ok = drbd_socket_okay(&tconn->meta.socket) && ok;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700887 if (ok)
888 break;
889 }
890
891retry:
Philipp Reisner907599e2011-02-08 11:25:37 +0100892 s = drbd_wait_for_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700893 if (s) {
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200894 try = receive_first_packet(tconn, s);
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200895 drbd_socket_okay(&tconn->data.socket);
896 drbd_socket_okay(&tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700897 switch (try) {
Andreas Gruenbachere5d6f332011-03-28 16:44:40 +0200898 case P_INITIAL_DATA:
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200899 if (tconn->data.socket) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100900 conn_warn(tconn, "initial packet S crossed\n");
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200901 sock_release(tconn->data.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700902 }
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200903 tconn->data.socket = s;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700904 break;
Andreas Gruenbachere5d6f332011-03-28 16:44:40 +0200905 case P_INITIAL_META:
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200906 if (tconn->meta.socket) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100907 conn_warn(tconn, "initial packet M crossed\n");
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200908 sock_release(tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700909 }
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200910 tconn->meta.socket = s;
Philipp Reisner907599e2011-02-08 11:25:37 +0100911 set_bit(DISCARD_CONCURRENT, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700912 break;
913 default:
Philipp Reisner907599e2011-02-08 11:25:37 +0100914 conn_warn(tconn, "Error receiving initial packet\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700915 sock_release(s);
916 if (random32() & 1)
917 goto retry;
918 }
919 }
920
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100921 if (tconn->cstate <= C_DISCONNECTING)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700922 goto out_release_sockets;
923 if (signal_pending(current)) {
924 flush_signals(current);
925 smp_rmb();
Philipp Reisner907599e2011-02-08 11:25:37 +0100926 if (get_t_state(&tconn->receiver) == EXITING)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700927 goto out_release_sockets;
928 }
929
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200930 if (tconn->data.socket && &tconn->meta.socket) {
931 ok = drbd_socket_okay(&tconn->data.socket);
932 ok = drbd_socket_okay(&tconn->meta.socket) && ok;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700933 if (ok)
934 break;
935 }
936 } while (1);
937
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200938 sock = tconn->data.socket;
939 msock = tconn->meta.socket;
940
Philipp Reisnerb411b362009-09-25 16:07:19 -0700941 msock->sk->sk_reuse = 1; /* SO_REUSEADDR */
942 sock->sk->sk_reuse = 1; /* SO_REUSEADDR */
943
944 sock->sk->sk_allocation = GFP_NOIO;
945 msock->sk->sk_allocation = GFP_NOIO;
946
947 sock->sk->sk_priority = TC_PRIO_INTERACTIVE_BULK;
948 msock->sk->sk_priority = TC_PRIO_INTERACTIVE;
949
Philipp Reisnerb411b362009-09-25 16:07:19 -0700950 /* NOT YET ...
Philipp Reisner907599e2011-02-08 11:25:37 +0100951 * sock->sk->sk_sndtimeo = tconn->net_conf->timeout*HZ/10;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700952 * sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
Andreas Gruenbacher60381782011-03-28 17:05:50 +0200953 * first set it to the P_CONNECTION_FEATURES timeout,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700954 * which we set to 4x the configured ping_timeout. */
Philipp Reisner44ed1672011-04-19 17:10:19 +0200955 rcu_read_lock();
956 nc = rcu_dereference(tconn->net_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700957
Philipp Reisner44ed1672011-04-19 17:10:19 +0200958 sock->sk->sk_sndtimeo =
959 sock->sk->sk_rcvtimeo = nc->ping_timeo*4*HZ/10;
960
961 msock->sk->sk_rcvtimeo = nc->ping_int*HZ;
962 timeout = nc->timeout * HZ / 10;
963 rcu_read_unlock();
964
965 msock->sk->sk_sndtimeo = timeout;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700966
967 /* we don't want delays.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300968 * we use TCP_CORK where appropriate, though */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700969 drbd_tcp_nodelay(sock);
970 drbd_tcp_nodelay(msock);
971
Philipp Reisner907599e2011-02-08 11:25:37 +0100972 tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700973
Andreas Gruenbacher60381782011-03-28 17:05:50 +0200974 h = drbd_do_features(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700975 if (h <= 0)
976 return h;
977
Philipp Reisner907599e2011-02-08 11:25:37 +0100978 if (tconn->cram_hmac_tfm) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700979 /* drbd_request_state(mdev, NS(conn, WFAuth)); */
Philipp Reisner907599e2011-02-08 11:25:37 +0100980 switch (drbd_do_auth(tconn)) {
Johannes Thomab10d96c2010-01-07 16:02:50 +0100981 case -1:
Philipp Reisner907599e2011-02-08 11:25:37 +0100982 conn_err(tconn, "Authentication of peer failed\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700983 return -1;
Johannes Thomab10d96c2010-01-07 16:02:50 +0100984 case 0:
Philipp Reisner907599e2011-02-08 11:25:37 +0100985 conn_err(tconn, "Authentication of peer failed, trying again.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +0100986 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700987 }
988 }
989
Philipp Reisner44ed1672011-04-19 17:10:19 +0200990 sock->sk->sk_sndtimeo = timeout;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700991 sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
992
Andreas Gruenbacher387eb302011-03-16 01:05:37 +0100993 if (drbd_send_protocol(tconn) == -EOPNOTSUPP)
Philipp Reisner7e2455c2010-04-22 14:50:23 +0200994 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700995
Philipp Reisnerc141ebd2011-05-05 16:13:10 +0200996 rcu_read_lock();
997 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
998 kref_get(&mdev->kref);
999 rcu_read_unlock();
1000 drbd_connected(mdev);
1001 kref_put(&mdev->kref, &drbd_minor_destroy);
1002 rcu_read_lock();
1003 }
1004 rcu_read_unlock();
1005
Philipp Reisner823bd832012-11-08 15:04:36 +01001006 if (conn_request_state(tconn, NS(conn, C_WF_REPORT_PARAMS), CS_VERBOSE) < SS_SUCCESS)
1007 return 0;
1008
1009 drbd_thread_start(&tconn->asender);
1010
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07001011 return h;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001012
1013out_release_sockets:
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +02001014 if (tconn->data.socket) {
1015 sock_release(tconn->data.socket);
1016 tconn->data.socket = NULL;
1017 }
1018 if (tconn->meta.socket) {
1019 sock_release(tconn->meta.socket);
1020 tconn->meta.socket = NULL;
1021 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001022 return -1;
1023}
1024
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001025static int decode_header(struct drbd_tconn *tconn, void *header, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001026{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001027 unsigned int header_size = drbd_header_size(tconn);
1028
Andreas Gruenbacher0c8e36d2011-03-30 16:00:17 +02001029 if (header_size == sizeof(struct p_header100) &&
1030 *(__be32 *)header == cpu_to_be32(DRBD_MAGIC_100)) {
1031 struct p_header100 *h = header;
1032 if (h->pad != 0) {
1033 conn_err(tconn, "Header padding is not zero\n");
1034 return -EINVAL;
1035 }
1036 pi->vnr = be16_to_cpu(h->volume);
1037 pi->cmd = be16_to_cpu(h->command);
1038 pi->size = be32_to_cpu(h->length);
1039 } else if (header_size == sizeof(struct p_header95) &&
1040 *(__be16 *)header == cpu_to_be16(DRBD_MAGIC_BIG)) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001041 struct p_header95 *h = header;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001042 pi->cmd = be16_to_cpu(h->command);
Andreas Gruenbacherb55d84b2011-03-22 13:17:47 +01001043 pi->size = be32_to_cpu(h->length);
1044 pi->vnr = 0;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001045 } else if (header_size == sizeof(struct p_header80) &&
1046 *(__be32 *)header == cpu_to_be32(DRBD_MAGIC)) {
1047 struct p_header80 *h = header;
1048 pi->cmd = be16_to_cpu(h->command);
1049 pi->size = be16_to_cpu(h->length);
Philipp Reisner77351055b2011-02-07 17:24:26 +01001050 pi->vnr = 0;
Philipp Reisner02918be2010-08-20 14:35:10 +02001051 } else {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001052 conn_err(tconn, "Wrong magic value 0x%08x in protocol version %d\n",
1053 be32_to_cpu(*(__be32 *)header),
1054 tconn->agreed_pro_version);
Andreas Gruenbacher8172f3e2011-03-16 17:22:39 +01001055 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001056 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001057 pi->data = header + header_size;
Andreas Gruenbacher8172f3e2011-03-16 17:22:39 +01001058 return 0;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001059}
1060
Philipp Reisner9ba7aa02011-02-07 17:32:41 +01001061static int drbd_recv_header(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner257d0af2011-01-26 12:15:29 +01001062{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001063 void *buffer = tconn->data.rbuf;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001064 int err;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001065
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001066 err = drbd_recv_all_warn(tconn, buffer, drbd_header_size(tconn));
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001067 if (err)
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001068 return err;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001069
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001070 err = decode_header(tconn, buffer, pi);
Philipp Reisner9ba7aa02011-02-07 17:32:41 +01001071 tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001072
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001073 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001074}
1075
Philipp Reisner2451fc32010-08-24 13:43:11 +02001076static void drbd_flush(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001077{
1078 int rv;
1079
1080 if (mdev->write_ordering >= WO_bdev_flush && get_ldev(mdev)) {
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +04001081 rv = blkdev_issue_flush(mdev->ldev->backing_bdev, GFP_KERNEL,
Christoph Hellwigdd3932e2010-09-16 20:51:46 +02001082 NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001083 if (rv) {
Philipp Reisnera67b8132011-05-25 11:03:04 +02001084 dev_info(DEV, "local disk flush failed with status %d\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001085 /* would rather check on EOPNOTSUPP, but that is not reliable.
1086 * don't try again for ANY return value != 0
1087 * if (rv == -EOPNOTSUPP) */
1088 drbd_bump_write_ordering(mdev, WO_drain_io);
1089 }
1090 put_ldev(mdev);
1091 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001092}
1093
1094/**
1095 * drbd_may_finish_epoch() - Applies an epoch_event to the epoch's state, eventually finishes it.
1096 * @mdev: DRBD device.
1097 * @epoch: Epoch object.
1098 * @ev: Epoch event.
1099 */
1100static enum finish_epoch drbd_may_finish_epoch(struct drbd_conf *mdev,
1101 struct drbd_epoch *epoch,
1102 enum epoch_event ev)
1103{
Philipp Reisner2451fc32010-08-24 13:43:11 +02001104 int epoch_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001105 struct drbd_epoch *next_epoch;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001106 enum finish_epoch rv = FE_STILL_LIVE;
1107
1108 spin_lock(&mdev->epoch_lock);
1109 do {
1110 next_epoch = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001111
1112 epoch_size = atomic_read(&epoch->epoch_size);
1113
1114 switch (ev & ~EV_CLEANUP) {
1115 case EV_PUT:
1116 atomic_dec(&epoch->active);
1117 break;
1118 case EV_GOT_BARRIER_NR:
1119 set_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001120 break;
1121 case EV_BECAME_LAST:
1122 /* nothing to do*/
1123 break;
1124 }
1125
Philipp Reisnerb411b362009-09-25 16:07:19 -07001126 if (epoch_size != 0 &&
1127 atomic_read(&epoch->active) == 0 &&
Philipp Reisner85d735132011-07-18 15:45:15 +02001128 (test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags) || ev & EV_CLEANUP)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001129 if (!(ev & EV_CLEANUP)) {
1130 spin_unlock(&mdev->epoch_lock);
1131 drbd_send_b_ack(mdev, epoch->barrier_nr, epoch_size);
1132 spin_lock(&mdev->epoch_lock);
1133 }
Philipp Reisner85d735132011-07-18 15:45:15 +02001134 if (test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags))
1135 dec_unacked(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001136
1137 if (mdev->current_epoch != epoch) {
1138 next_epoch = list_entry(epoch->list.next, struct drbd_epoch, list);
1139 list_del(&epoch->list);
1140 ev = EV_BECAME_LAST | (ev & EV_CLEANUP);
1141 mdev->epochs--;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001142 kfree(epoch);
1143
1144 if (rv == FE_STILL_LIVE)
1145 rv = FE_DESTROYED;
1146 } else {
1147 epoch->flags = 0;
1148 atomic_set(&epoch->epoch_size, 0);
Uwe Kleine-König698f9312010-07-02 20:41:51 +02001149 /* atomic_set(&epoch->active, 0); is already zero */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001150 if (rv == FE_STILL_LIVE)
1151 rv = FE_RECYCLED;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001152 wake_up(&mdev->ee_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001153 }
1154 }
1155
1156 if (!next_epoch)
1157 break;
1158
1159 epoch = next_epoch;
1160 } while (1);
1161
1162 spin_unlock(&mdev->epoch_lock);
1163
Philipp Reisnerb411b362009-09-25 16:07:19 -07001164 return rv;
1165}
1166
1167/**
1168 * drbd_bump_write_ordering() - Fall back to an other write ordering method
1169 * @mdev: DRBD device.
1170 * @wo: Write ordering method to try.
1171 */
1172void drbd_bump_write_ordering(struct drbd_conf *mdev, enum write_ordering_e wo) __must_hold(local)
1173{
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001174 struct disk_conf *dc;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001175 enum write_ordering_e pwo;
1176 static char *write_ordering_str[] = {
1177 [WO_none] = "none",
1178 [WO_drain_io] = "drain",
1179 [WO_bdev_flush] = "flush",
Philipp Reisnerb411b362009-09-25 16:07:19 -07001180 };
1181
1182 pwo = mdev->write_ordering;
1183 wo = min(pwo, wo);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001184 rcu_read_lock();
1185 dc = rcu_dereference(mdev->ldev->disk_conf);
1186
Andreas Gruenbacher66b2f6b2011-05-04 15:25:35 +02001187 if (wo == WO_bdev_flush && !dc->disk_flushes)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001188 wo = WO_drain_io;
Andreas Gruenbacherd0c980e2011-05-04 15:25:35 +02001189 if (wo == WO_drain_io && !dc->disk_drain)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001190 wo = WO_none;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001191 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07001192 mdev->write_ordering = wo;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001193 if (pwo != mdev->write_ordering || wo == WO_bdev_flush)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001194 dev_info(DEV, "Method to ensure write ordering: %s\n", write_ordering_str[mdev->write_ordering]);
1195}
1196
1197/**
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001198 * drbd_submit_peer_request()
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001199 * @mdev: DRBD device.
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001200 * @peer_req: peer request
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001201 * @rw: flag field, see bio->bi_rw
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001202 *
1203 * May spread the pages to multiple bios,
1204 * depending on bio_add_page restrictions.
1205 *
1206 * Returns 0 if all bios have been submitted,
1207 * -ENOMEM if we could not allocate enough bios,
1208 * -ENOSPC (any better suggestion?) if we have not been able to bio_add_page a
1209 * single page to an empty bio (which should never happen and likely indicates
1210 * that the lower level IO stack is in some way broken). This has been observed
1211 * on certain Xen deployments.
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001212 */
1213/* TODO allocate from our own bio_set. */
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001214int drbd_submit_peer_request(struct drbd_conf *mdev,
1215 struct drbd_peer_request *peer_req,
1216 const unsigned rw, const int fault_type)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001217{
1218 struct bio *bios = NULL;
1219 struct bio *bio;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001220 struct page *page = peer_req->pages;
1221 sector_t sector = peer_req->i.sector;
1222 unsigned ds = peer_req->i.size;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001223 unsigned n_bios = 0;
1224 unsigned nr_pages = (ds + PAGE_SIZE -1) >> PAGE_SHIFT;
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001225 int err = -ENOMEM;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001226
1227 /* In most cases, we will only need one bio. But in case the lower
1228 * level restrictions happen to be different at this offset on this
1229 * side than those of the sending peer, we may need to submit the
Lars Ellenbergda4a75d2011-02-23 17:02:01 +01001230 * request in more than one bio.
1231 *
1232 * Plain bio_alloc is good enough here, this is no DRBD internally
1233 * generated bio, but a bio allocated on behalf of the peer.
1234 */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001235next_bio:
1236 bio = bio_alloc(GFP_NOIO, nr_pages);
1237 if (!bio) {
1238 dev_err(DEV, "submit_ee: Allocation of a bio failed\n");
1239 goto fail;
1240 }
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001241 /* > peer_req->i.sector, unless this is the first bio */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001242 bio->bi_sector = sector;
1243 bio->bi_bdev = mdev->ldev->backing_bdev;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001244 bio->bi_rw = rw;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001245 bio->bi_private = peer_req;
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01001246 bio->bi_end_io = drbd_peer_request_endio;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001247
1248 bio->bi_next = bios;
1249 bios = bio;
1250 ++n_bios;
1251
1252 page_chain_for_each(page) {
1253 unsigned len = min_t(unsigned, ds, PAGE_SIZE);
1254 if (!bio_add_page(bio, page, len, 0)) {
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001255 /* A single page must always be possible!
1256 * But in case it fails anyways,
1257 * we deal with it, and complain (below). */
1258 if (bio->bi_vcnt == 0) {
1259 dev_err(DEV,
1260 "bio_add_page failed for len=%u, "
1261 "bi_vcnt=0 (bi_sector=%llu)\n",
1262 len, (unsigned long long)bio->bi_sector);
1263 err = -ENOSPC;
1264 goto fail;
1265 }
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001266 goto next_bio;
1267 }
1268 ds -= len;
1269 sector += len >> 9;
1270 --nr_pages;
1271 }
1272 D_ASSERT(page == NULL);
1273 D_ASSERT(ds == 0);
1274
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001275 atomic_set(&peer_req->pending_bios, n_bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001276 do {
1277 bio = bios;
1278 bios = bios->bi_next;
1279 bio->bi_next = NULL;
1280
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001281 drbd_generic_make_request(mdev, fault_type, bio);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001282 } while (bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001283 return 0;
1284
1285fail:
1286 while (bios) {
1287 bio = bios;
1288 bios = bios->bi_next;
1289 bio_put(bio);
1290 }
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001291 return err;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001292}
1293
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001294static void drbd_remove_epoch_entry_interval(struct drbd_conf *mdev,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001295 struct drbd_peer_request *peer_req)
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001296{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001297 struct drbd_interval *i = &peer_req->i;
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001298
1299 drbd_remove_interval(&mdev->write_requests, i);
1300 drbd_clear_interval(i);
1301
Andreas Gruenbacher6c852be2011-02-04 15:38:52 +01001302 /* Wake up any processes waiting for this peer request to complete. */
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001303 if (i->waiting)
1304 wake_up(&mdev->misc_wait);
1305}
1306
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001307static int receive_Barrier(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001308{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001309 struct drbd_conf *mdev;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001310 int rv;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001311 struct p_barrier *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001312 struct drbd_epoch *epoch;
1313
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001314 mdev = vnr_to_mdev(tconn, pi->vnr);
1315 if (!mdev)
1316 return -EIO;
1317
Philipp Reisnerb411b362009-09-25 16:07:19 -07001318 inc_unacked(mdev);
1319
Philipp Reisnerb411b362009-09-25 16:07:19 -07001320 mdev->current_epoch->barrier_nr = p->barrier;
1321 rv = drbd_may_finish_epoch(mdev, mdev->current_epoch, EV_GOT_BARRIER_NR);
1322
1323 /* P_BARRIER_ACK may imply that the corresponding extent is dropped from
1324 * the activity log, which means it would not be resynced in case the
1325 * R_PRIMARY crashes now.
1326 * Therefore we must send the barrier_ack after the barrier request was
1327 * completed. */
1328 switch (mdev->write_ordering) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001329 case WO_none:
1330 if (rv == FE_RECYCLED)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001331 return 0;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001332
1333 /* receiver context, in the writeout path of the other node.
1334 * avoid potential distributed deadlock */
1335 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1336 if (epoch)
1337 break;
1338 else
1339 dev_warn(DEV, "Allocation of an epoch failed, slowing down\n");
1340 /* Fall through */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001341
1342 case WO_bdev_flush:
1343 case WO_drain_io:
Philipp Reisnerb411b362009-09-25 16:07:19 -07001344 drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
Philipp Reisner2451fc32010-08-24 13:43:11 +02001345 drbd_flush(mdev);
1346
1347 if (atomic_read(&mdev->current_epoch->epoch_size)) {
1348 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1349 if (epoch)
1350 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001351 }
1352
Philipp Reisner2451fc32010-08-24 13:43:11 +02001353 epoch = mdev->current_epoch;
1354 wait_event(mdev->ee_wait, atomic_read(&epoch->epoch_size) == 0);
1355
1356 D_ASSERT(atomic_read(&epoch->active) == 0);
1357 D_ASSERT(epoch->flags == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001358
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001359 return 0;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001360 default:
1361 dev_err(DEV, "Strangeness in mdev->write_ordering %d\n", mdev->write_ordering);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001362 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001363 }
1364
1365 epoch->flags = 0;
1366 atomic_set(&epoch->epoch_size, 0);
1367 atomic_set(&epoch->active, 0);
1368
1369 spin_lock(&mdev->epoch_lock);
1370 if (atomic_read(&mdev->current_epoch->epoch_size)) {
1371 list_add(&epoch->list, &mdev->current_epoch->list);
1372 mdev->current_epoch = epoch;
1373 mdev->epochs++;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001374 } else {
1375 /* The current_epoch got recycled while we allocated this one... */
1376 kfree(epoch);
1377 }
1378 spin_unlock(&mdev->epoch_lock);
1379
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001380 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001381}
1382
1383/* used from receive_RSDataReply (recv_resync_read)
1384 * and from receive_Data */
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001385static struct drbd_peer_request *
1386read_in_block(struct drbd_conf *mdev, u64 id, sector_t sector,
1387 int data_size) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001388{
Lars Ellenberg66660322010-04-06 12:15:04 +02001389 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001390 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001391 struct page *page;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001392 int dgs, ds, err;
Philipp Reisnera0638452011-01-19 14:31:32 +01001393 void *dig_in = mdev->tconn->int_dig_in;
1394 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001395 unsigned long *data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001396
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001397 dgs = 0;
1398 if (mdev->tconn->peer_integrity_tfm) {
1399 dgs = crypto_hash_digestsize(mdev->tconn->peer_integrity_tfm);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001400 /*
1401 * FIXME: Receive the incoming digest into the receive buffer
1402 * here, together with its struct p_data?
1403 */
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001404 err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs);
1405 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001406 return NULL;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001407 data_size -= dgs;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001408 }
1409
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001410 if (!expect(data_size != 0))
1411 return NULL;
1412 if (!expect(IS_ALIGNED(data_size, 512)))
1413 return NULL;
1414 if (!expect(data_size <= DRBD_MAX_BIO_SIZE))
1415 return NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001416
Lars Ellenberg66660322010-04-06 12:15:04 +02001417 /* even though we trust out peer,
1418 * we sometimes have to double check. */
1419 if (sector + (data_size>>9) > capacity) {
Lars Ellenbergfdda6542011-01-24 15:11:01 +01001420 dev_err(DEV, "request from peer beyond end of local disk: "
1421 "capacity: %llus < sector: %llus + size: %u\n",
Lars Ellenberg66660322010-04-06 12:15:04 +02001422 (unsigned long long)capacity,
1423 (unsigned long long)sector, data_size);
1424 return NULL;
1425 }
1426
Philipp Reisnerb411b362009-09-25 16:07:19 -07001427 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
1428 * "criss-cross" setup, that might cause write-out on some other DRBD,
1429 * which in turn might block on the other node at this very place. */
Andreas Gruenbacher0db55362011-04-06 16:09:15 +02001430 peer_req = drbd_alloc_peer_req(mdev, id, sector, data_size, GFP_NOIO);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001431 if (!peer_req)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001432 return NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001433
Philipp Reisnerb411b362009-09-25 16:07:19 -07001434 ds = data_size;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001435 page = peer_req->pages;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001436 page_chain_for_each(page) {
1437 unsigned len = min_t(int, ds, PAGE_SIZE);
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001438 data = kmap(page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001439 err = drbd_recv_all_warn(mdev->tconn, data, len);
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +01001440 if (drbd_insert_fault(mdev, DRBD_FAULT_RECEIVE)) {
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001441 dev_err(DEV, "Fault injection: Corrupting data on receive\n");
1442 data[0] = data[0] ^ (unsigned long)-1;
1443 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001444 kunmap(page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001445 if (err) {
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001446 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001447 return NULL;
1448 }
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001449 ds -= len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001450 }
1451
1452 if (dgs) {
Andreas Gruenbacher5b614ab2011-04-27 21:00:12 +02001453 drbd_csum_ee(mdev, mdev->tconn->peer_integrity_tfm, peer_req, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001454 if (memcmp(dig_in, dig_vv, dgs)) {
Lars Ellenberg470be442010-11-10 10:36:52 +01001455 dev_err(DEV, "Digest integrity check FAILED: %llus +%u\n",
1456 (unsigned long long)sector, data_size);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001457 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001458 return NULL;
1459 }
1460 }
1461 mdev->recv_cnt += data_size>>9;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001462 return peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001463}
1464
1465/* drbd_drain_block() just takes a data block
1466 * out of the socket input buffer, and discards it.
1467 */
1468static int drbd_drain_block(struct drbd_conf *mdev, int data_size)
1469{
1470 struct page *page;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001471 int err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001472 void *data;
1473
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001474 if (!data_size)
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001475 return 0;
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001476
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +02001477 page = drbd_alloc_pages(mdev, 1, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001478
1479 data = kmap(page);
1480 while (data_size) {
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001481 unsigned int len = min_t(int, data_size, PAGE_SIZE);
1482
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001483 err = drbd_recv_all_warn(mdev->tconn, data, len);
1484 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001485 break;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001486 data_size -= len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001487 }
1488 kunmap(page);
Andreas Gruenbacher5cc287e2011-04-07 21:02:59 +02001489 drbd_free_pages(mdev, page, 0);
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001490 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001491}
1492
1493static int recv_dless_read(struct drbd_conf *mdev, struct drbd_request *req,
1494 sector_t sector, int data_size)
1495{
1496 struct bio_vec *bvec;
1497 struct bio *bio;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001498 int dgs, err, i, expect;
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 Reisnerb411b362009-09-25 16:07:19 -07001501
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001502 dgs = 0;
1503 if (mdev->tconn->peer_integrity_tfm) {
1504 dgs = crypto_hash_digestsize(mdev->tconn->peer_integrity_tfm);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001505 err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs);
1506 if (err)
1507 return err;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001508 data_size -= dgs;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001509 }
1510
Philipp Reisnerb411b362009-09-25 16:07:19 -07001511 /* optimistically update recv_cnt. if receiving fails below,
1512 * we disconnect anyways, and counters will be reset. */
1513 mdev->recv_cnt += data_size>>9;
1514
1515 bio = req->master_bio;
1516 D_ASSERT(sector == bio->bi_sector);
1517
1518 bio_for_each_segment(bvec, bio, i) {
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001519 void *mapped = kmap(bvec->bv_page) + bvec->bv_offset;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001520 expect = min_t(int, data_size, bvec->bv_len);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001521 err = drbd_recv_all_warn(mdev->tconn, mapped, expect);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001522 kunmap(bvec->bv_page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001523 if (err)
1524 return err;
1525 data_size -= expect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001526 }
1527
1528 if (dgs) {
Andreas Gruenbacher5b614ab2011-04-27 21:00:12 +02001529 drbd_csum_bio(mdev, mdev->tconn->peer_integrity_tfm, bio, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001530 if (memcmp(dig_in, dig_vv, dgs)) {
1531 dev_err(DEV, "Digest integrity check FAILED. Broken NICs?\n");
Andreas Gruenbacher28284ce2011-03-16 17:54:02 +01001532 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001533 }
1534 }
1535
1536 D_ASSERT(data_size == 0);
Andreas Gruenbacher28284ce2011-03-16 17:54:02 +01001537 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001538}
1539
Andreas Gruenbachera990be42011-04-06 17:56:48 +02001540/*
1541 * e_end_resync_block() is called in asender context via
1542 * drbd_finish_peer_reqs().
1543 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001544static int e_end_resync_block(struct drbd_work *w, int unused)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001545{
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001546 struct drbd_peer_request *peer_req =
1547 container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001548 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001549 sector_t sector = peer_req->i.sector;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001550 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001551
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001552 D_ASSERT(drbd_interval_empty(&peer_req->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001553
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001554 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
1555 drbd_set_in_sync(mdev, sector, peer_req->i.size);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001556 err = drbd_send_ack(mdev, P_RS_WRITE_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001557 } else {
1558 /* Record failure to sync */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001559 drbd_rs_failed_io(mdev, sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001560
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001561 err = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001562 }
1563 dec_unacked(mdev);
1564
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001565 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001566}
1567
1568static int recv_resync_read(struct drbd_conf *mdev, sector_t sector, int data_size) __releases(local)
1569{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001570 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001571
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001572 peer_req = read_in_block(mdev, ID_SYNCER, sector, data_size);
1573 if (!peer_req)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001574 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001575
1576 dec_rs_pending(mdev);
1577
Philipp Reisnerb411b362009-09-25 16:07:19 -07001578 inc_unacked(mdev);
1579 /* corresponding dec_unacked() in e_end_resync_block()
1580 * respective _drbd_clear_done_ee */
1581
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001582 peer_req->w.cb = e_end_resync_block;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001583
Philipp Reisner87eeee42011-01-19 14:16:30 +01001584 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001585 list_add(&peer_req->w.list, &mdev->sync_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001586 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001587
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001588 atomic_add(data_size >> 9, &mdev->rs_sect_ev);
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001589 if (drbd_submit_peer_request(mdev, peer_req, WRITE, DRBD_FAULT_RS_WR) == 0)
Andreas Gruenbachere1c1b0f2011-03-16 17:58:27 +01001590 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001591
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001592 /* don't care for the reason here */
1593 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01001594 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001595 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001596 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02001597
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001598 drbd_free_peer_req(mdev, peer_req);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001599fail:
1600 put_ldev(mdev);
Andreas Gruenbachere1c1b0f2011-03-16 17:58:27 +01001601 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001602}
1603
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001604static struct drbd_request *
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001605find_request(struct drbd_conf *mdev, struct rb_root *root, u64 id,
1606 sector_t sector, bool missing_ok, const char *func)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001607{
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001608 struct drbd_request *req;
1609
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001610 /* Request object according to our peer */
1611 req = (struct drbd_request *)(unsigned long)id;
Andreas Gruenbacher5e472262011-01-27 14:42:51 +01001612 if (drbd_contains_interval(root, sector, &req->i) && req->i.local)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001613 return req;
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001614 if (!missing_ok) {
Andreas Gruenbacher5af172e2011-07-15 09:43:23 +02001615 dev_err(DEV, "%s: failed to find request 0x%lx, sector %llus\n", func,
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001616 (unsigned long)id, (unsigned long long)sector);
1617 }
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001618 return NULL;
1619}
1620
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001621static int receive_DataReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001622{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001623 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001624 struct drbd_request *req;
1625 sector_t sector;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001626 int err;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001627 struct p_data *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001628
1629 mdev = vnr_to_mdev(tconn, pi->vnr);
1630 if (!mdev)
1631 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001632
1633 sector = be64_to_cpu(p->sector);
1634
Philipp Reisner87eeee42011-01-19 14:16:30 +01001635 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001636 req = find_request(mdev, &mdev->read_requests, p->block_id, sector, false, __func__);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001637 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001638 if (unlikely(!req))
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001639 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001640
Bart Van Assche24c48302011-05-21 18:32:29 +02001641 /* hlist_del(&req->collision) is done in _req_may_be_done, to avoid
Philipp Reisnerb411b362009-09-25 16:07:19 -07001642 * special casing it there for the various failure cases.
1643 * still no race with drbd_fail_pending_reads */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001644 err = recv_dless_read(mdev, req, sector, pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001645 if (!err)
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001646 req_mod(req, DATA_RECEIVED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001647 /* else: nothing. handled from drbd_disconnect...
1648 * I don't think we may complete this just yet
1649 * in case we are "on-disconnect: freeze" */
1650
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001651 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001652}
1653
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001654static int receive_RSDataReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001655{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001656 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001657 sector_t sector;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001658 int err;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001659 struct p_data *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001660
1661 mdev = vnr_to_mdev(tconn, pi->vnr);
1662 if (!mdev)
1663 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001664
1665 sector = be64_to_cpu(p->sector);
1666 D_ASSERT(p->block_id == ID_SYNCER);
1667
1668 if (get_ldev(mdev)) {
1669 /* data is submitted to disk within recv_resync_read.
1670 * corresponding put_ldev done below on error,
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01001671 * or in drbd_peer_request_endio. */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001672 err = recv_resync_read(mdev, sector, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001673 } else {
1674 if (__ratelimit(&drbd_ratelimit_state))
1675 dev_err(DEV, "Can not write resync data to local disk.\n");
1676
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001677 err = drbd_drain_block(mdev, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001678
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001679 drbd_send_ack_dp(mdev, P_NEG_ACK, p, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001680 }
1681
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001682 atomic_add(pi->size >> 9, &mdev->rs_sect_in);
Philipp Reisner778f2712010-07-06 11:14:00 +02001683
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001684 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001685}
1686
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001687static int w_restart_write(struct drbd_work *w, int cancel)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001688{
1689 struct drbd_request *req = container_of(w, struct drbd_request, w);
1690 struct drbd_conf *mdev = w->mdev;
1691 struct bio *bio;
1692 unsigned long start_time;
1693 unsigned long flags;
1694
1695 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
1696 if (!expect(req->rq_state & RQ_POSTPONED)) {
1697 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001698 return -EIO;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001699 }
1700 bio = req->master_bio;
1701 start_time = req->start_time;
1702 /* Postponed requests will not have their master_bio completed! */
1703 __req_mod(req, DISCARD_WRITE, NULL);
1704 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
1705
1706 while (__drbd_make_request(mdev, bio, start_time))
1707 /* retry */ ;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001708 return 0;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001709}
1710
1711static void restart_conflicting_writes(struct drbd_conf *mdev,
1712 sector_t sector, int size)
1713{
1714 struct drbd_interval *i;
1715 struct drbd_request *req;
1716
1717 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
1718 if (!i->local)
1719 continue;
1720 req = container_of(i, struct drbd_request, i);
1721 if (req->rq_state & RQ_LOCAL_PENDING ||
1722 !(req->rq_state & RQ_POSTPONED))
1723 continue;
1724 if (expect(list_empty(&req->w.list))) {
1725 req->w.mdev = mdev;
1726 req->w.cb = w_restart_write;
1727 drbd_queue_work(&mdev->tconn->data.work, &req->w);
1728 }
1729 }
1730}
1731
Andreas Gruenbachera990be42011-04-06 17:56:48 +02001732/*
1733 * e_end_block() is called in asender context via drbd_finish_peer_reqs().
Philipp Reisnerb411b362009-09-25 16:07:19 -07001734 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001735static int e_end_block(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001736{
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001737 struct drbd_peer_request *peer_req =
1738 container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001739 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001740 sector_t sector = peer_req->i.sector;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001741 int err = 0, pcmd;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001742
Philipp Reisner303d1442011-04-13 16:24:47 -07001743 if (peer_req->flags & EE_SEND_WRITE_ACK) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001744 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001745 pcmd = (mdev->state.conn >= C_SYNC_SOURCE &&
1746 mdev->state.conn <= C_PAUSED_SYNC_T &&
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001747 peer_req->flags & EE_MAY_SET_IN_SYNC) ?
Philipp Reisnerb411b362009-09-25 16:07:19 -07001748 P_RS_WRITE_ACK : P_WRITE_ACK;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001749 err = drbd_send_ack(mdev, pcmd, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001750 if (pcmd == P_RS_WRITE_ACK)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001751 drbd_set_in_sync(mdev, sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001752 } else {
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001753 err = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001754 /* we expect it to be marked out of sync anyways...
1755 * maybe assert this? */
1756 }
1757 dec_unacked(mdev);
1758 }
1759 /* we delete from the conflict detection hash _after_ we sent out the
1760 * P_WRITE_ACK / P_NEG_ACK, to get the sequence number right. */
Philipp Reisner302bdea2011-04-21 11:36:49 +02001761 if (peer_req->flags & EE_IN_INTERVAL_TREE) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01001762 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001763 D_ASSERT(!drbd_interval_empty(&peer_req->i));
1764 drbd_remove_epoch_entry_interval(mdev, peer_req);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001765 if (peer_req->flags & EE_RESTART_REQUESTS)
1766 restart_conflicting_writes(mdev, sector, peer_req->i.size);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001767 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbb3bfe92011-01-21 15:59:23 +01001768 } else
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001769 D_ASSERT(drbd_interval_empty(&peer_req->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001770
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001771 drbd_may_finish_epoch(mdev, peer_req->epoch, EV_PUT + (cancel ? EV_CLEANUP : 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001772
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001773 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001774}
1775
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001776static int e_send_ack(struct drbd_work *w, enum drbd_packet ack)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001777{
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001778 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001779 struct drbd_peer_request *peer_req =
1780 container_of(w, struct drbd_peer_request, w);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001781 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001782
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001783 err = drbd_send_ack(mdev, ack, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001784 dec_unacked(mdev);
1785
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001786 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001787}
1788
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001789static int e_send_discard_write(struct drbd_work *w, int unused)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001790{
1791 return e_send_ack(w, P_DISCARD_WRITE);
1792}
1793
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001794static int e_send_retry_write(struct drbd_work *w, int unused)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001795{
1796 struct drbd_tconn *tconn = w->mdev->tconn;
1797
1798 return e_send_ack(w, tconn->agreed_pro_version >= 100 ?
1799 P_RETRY_WRITE : P_DISCARD_WRITE);
1800}
1801
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001802static bool seq_greater(u32 a, u32 b)
1803{
1804 /*
1805 * We assume 32-bit wrap-around here.
1806 * For 24-bit wrap-around, we would have to shift:
1807 * a <<= 8; b <<= 8;
1808 */
1809 return (s32)a - (s32)b > 0;
1810}
1811
1812static u32 seq_max(u32 a, u32 b)
1813{
1814 return seq_greater(a, b) ? a : b;
1815}
1816
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001817static bool need_peer_seq(struct drbd_conf *mdev)
1818{
1819 struct drbd_tconn *tconn = mdev->tconn;
Philipp Reisner302bdea2011-04-21 11:36:49 +02001820 int tp;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001821
1822 /*
1823 * We only need to keep track of the last packet_seq number of our peer
1824 * if we are in dual-primary mode and we have the discard flag set; see
1825 * handle_write_conflicts().
1826 */
Philipp Reisner302bdea2011-04-21 11:36:49 +02001827
1828 rcu_read_lock();
1829 tp = rcu_dereference(mdev->tconn->net_conf)->two_primaries;
1830 rcu_read_unlock();
1831
1832 return tp && test_bit(DISCARD_CONCURRENT, &tconn->flags);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001833}
1834
Andreas Gruenbacher43ae0772011-02-03 18:42:08 +01001835static void update_peer_seq(struct drbd_conf *mdev, unsigned int peer_seq)
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001836{
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001837 unsigned int newest_peer_seq;
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001838
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001839 if (need_peer_seq(mdev)) {
1840 spin_lock(&mdev->peer_seq_lock);
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001841 newest_peer_seq = seq_max(mdev->peer_seq, peer_seq);
1842 mdev->peer_seq = newest_peer_seq;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001843 spin_unlock(&mdev->peer_seq_lock);
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001844 /* wake up only if we actually changed mdev->peer_seq */
1845 if (peer_seq == newest_peer_seq)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001846 wake_up(&mdev->seq_wait);
1847 }
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001848}
1849
Philipp Reisnerb411b362009-09-25 16:07:19 -07001850/* Called from receive_Data.
1851 * Synchronize packets on sock with packets on msock.
1852 *
1853 * This is here so even when a P_DATA packet traveling via sock overtook an Ack
1854 * packet traveling on msock, they are still processed in the order they have
1855 * been sent.
1856 *
1857 * Note: we don't care for Ack packets overtaking P_DATA packets.
1858 *
1859 * In case packet_seq is larger than mdev->peer_seq number, there are
1860 * outstanding packets on the msock. We wait for them to arrive.
1861 * In case we are the logically next packet, we update mdev->peer_seq
1862 * ourselves. Correctly handles 32bit wrap around.
1863 *
1864 * Assume we have a 10 GBit connection, that is about 1<<30 byte per second,
1865 * about 1<<21 sectors per second. So "worst" case, we have 1<<3 == 8 seconds
1866 * for the 24bit wrap (historical atomic_t guarantee on some archs), and we have
1867 * 1<<9 == 512 seconds aka ages for the 32bit wrap around...
1868 *
1869 * returns 0 if we may process the packet,
1870 * -ERESTARTSYS if we were interrupted (by disconnect signal). */
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001871static int wait_for_and_update_peer_seq(struct drbd_conf *mdev, const u32 peer_seq)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001872{
1873 DEFINE_WAIT(wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001874 long timeout;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001875 int ret;
1876
1877 if (!need_peer_seq(mdev))
1878 return 0;
1879
Philipp Reisnerb411b362009-09-25 16:07:19 -07001880 spin_lock(&mdev->peer_seq_lock);
1881 for (;;) {
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001882 if (!seq_greater(peer_seq - 1, mdev->peer_seq)) {
1883 mdev->peer_seq = seq_max(mdev->peer_seq, peer_seq);
1884 ret = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001885 break;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001886 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001887 if (signal_pending(current)) {
1888 ret = -ERESTARTSYS;
1889 break;
1890 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001891 prepare_to_wait(&mdev->seq_wait, &wait, TASK_INTERRUPTIBLE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001892 spin_unlock(&mdev->peer_seq_lock);
Philipp Reisner44ed1672011-04-19 17:10:19 +02001893 rcu_read_lock();
1894 timeout = rcu_dereference(mdev->tconn->net_conf)->ping_timeo*HZ/10;
1895 rcu_read_unlock();
Andreas Gruenbacher71b1c1e2011-03-01 15:40:43 +01001896 timeout = schedule_timeout(timeout);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001897 spin_lock(&mdev->peer_seq_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001898 if (!timeout) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001899 ret = -ETIMEDOUT;
Andreas Gruenbacher71b1c1e2011-03-01 15:40:43 +01001900 dev_err(DEV, "Timed out waiting for missing ack packets; disconnecting\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001901 break;
1902 }
1903 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001904 spin_unlock(&mdev->peer_seq_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001905 finish_wait(&mdev->seq_wait, &wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001906 return ret;
1907}
1908
Lars Ellenberg688593c2010-11-17 22:25:03 +01001909/* see also bio_flags_to_wire()
1910 * DRBD_REQ_*, because we need to semantically map the flags to data packet
1911 * flags and back. We may replicate to other kernel versions. */
1912static unsigned long wire_flags_to_bio(struct drbd_conf *mdev, u32 dpf)
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001913{
Lars Ellenberg688593c2010-11-17 22:25:03 +01001914 return (dpf & DP_RW_SYNC ? REQ_SYNC : 0) |
1915 (dpf & DP_FUA ? REQ_FUA : 0) |
1916 (dpf & DP_FLUSH ? REQ_FLUSH : 0) |
1917 (dpf & DP_DISCARD ? REQ_DISCARD : 0);
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001918}
1919
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001920static void fail_postponed_requests(struct drbd_conf *mdev, sector_t sector,
1921 unsigned int size)
1922{
1923 struct drbd_interval *i;
1924
1925 repeat:
1926 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
1927 struct drbd_request *req;
1928 struct bio_and_error m;
1929
1930 if (!i->local)
1931 continue;
1932 req = container_of(i, struct drbd_request, i);
1933 if (!(req->rq_state & RQ_POSTPONED))
1934 continue;
1935 req->rq_state &= ~RQ_POSTPONED;
1936 __req_mod(req, NEG_ACKED, &m);
1937 spin_unlock_irq(&mdev->tconn->req_lock);
1938 if (m.bio)
1939 complete_master_bio(mdev, &m);
1940 spin_lock_irq(&mdev->tconn->req_lock);
1941 goto repeat;
1942 }
1943}
1944
1945static int handle_write_conflicts(struct drbd_conf *mdev,
1946 struct drbd_peer_request *peer_req)
1947{
1948 struct drbd_tconn *tconn = mdev->tconn;
1949 bool resolve_conflicts = test_bit(DISCARD_CONCURRENT, &tconn->flags);
1950 sector_t sector = peer_req->i.sector;
1951 const unsigned int size = peer_req->i.size;
1952 struct drbd_interval *i;
1953 bool equal;
1954 int err;
1955
1956 /*
1957 * Inserting the peer request into the write_requests tree will prevent
1958 * new conflicting local requests from being added.
1959 */
1960 drbd_insert_interval(&mdev->write_requests, &peer_req->i);
1961
1962 repeat:
1963 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
1964 if (i == &peer_req->i)
1965 continue;
1966
1967 if (!i->local) {
1968 /*
1969 * Our peer has sent a conflicting remote request; this
1970 * should not happen in a two-node setup. Wait for the
1971 * earlier peer request to complete.
1972 */
1973 err = drbd_wait_misc(mdev, i);
1974 if (err)
1975 goto out;
1976 goto repeat;
1977 }
1978
1979 equal = i->sector == sector && i->size == size;
1980 if (resolve_conflicts) {
1981 /*
1982 * If the peer request is fully contained within the
1983 * overlapping request, it can be discarded; otherwise,
1984 * it will be retried once all overlapping requests
1985 * have completed.
1986 */
1987 bool discard = i->sector <= sector && i->sector +
1988 (i->size >> 9) >= sector + (size >> 9);
1989
1990 if (!equal)
1991 dev_alert(DEV, "Concurrent writes detected: "
1992 "local=%llus +%u, remote=%llus +%u, "
1993 "assuming %s came first\n",
1994 (unsigned long long)i->sector, i->size,
1995 (unsigned long long)sector, size,
1996 discard ? "local" : "remote");
1997
1998 inc_unacked(mdev);
1999 peer_req->w.cb = discard ? e_send_discard_write :
2000 e_send_retry_write;
2001 list_add_tail(&peer_req->w.list, &mdev->done_ee);
2002 wake_asender(mdev->tconn);
2003
2004 err = -ENOENT;
2005 goto out;
2006 } else {
2007 struct drbd_request *req =
2008 container_of(i, struct drbd_request, i);
2009
2010 if (!equal)
2011 dev_alert(DEV, "Concurrent writes detected: "
2012 "local=%llus +%u, remote=%llus +%u\n",
2013 (unsigned long long)i->sector, i->size,
2014 (unsigned long long)sector, size);
2015
2016 if (req->rq_state & RQ_LOCAL_PENDING ||
2017 !(req->rq_state & RQ_POSTPONED)) {
2018 /*
2019 * Wait for the node with the discard flag to
2020 * decide if this request will be discarded or
2021 * retried. Requests that are discarded will
2022 * disappear from the write_requests tree.
2023 *
2024 * In addition, wait for the conflicting
2025 * request to finish locally before submitting
2026 * the conflicting peer request.
2027 */
2028 err = drbd_wait_misc(mdev, &req->i);
2029 if (err) {
2030 _conn_request_state(mdev->tconn,
2031 NS(conn, C_TIMEOUT),
2032 CS_HARD);
2033 fail_postponed_requests(mdev, sector, size);
2034 goto out;
2035 }
2036 goto repeat;
2037 }
2038 /*
2039 * Remember to restart the conflicting requests after
2040 * the new peer request has completed.
2041 */
2042 peer_req->flags |= EE_RESTART_REQUESTS;
2043 }
2044 }
2045 err = 0;
2046
2047 out:
2048 if (err)
2049 drbd_remove_epoch_entry_interval(mdev, peer_req);
2050 return err;
2051}
2052
Philipp Reisnerb411b362009-09-25 16:07:19 -07002053/* mirrored write */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002054static int receive_Data(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002055{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002056 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002057 sector_t sector;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002058 struct drbd_peer_request *peer_req;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02002059 struct p_data *p = pi->data;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002060 u32 peer_seq = be32_to_cpu(p->seq_num);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002061 int rw = WRITE;
2062 u32 dp_flags;
Philipp Reisner302bdea2011-04-21 11:36:49 +02002063 int err, tp;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002064
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002065 mdev = vnr_to_mdev(tconn, pi->vnr);
2066 if (!mdev)
2067 return -EIO;
2068
Philipp Reisnerb411b362009-09-25 16:07:19 -07002069 if (!get_ldev(mdev)) {
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002070 int err2;
2071
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002072 err = wait_for_and_update_peer_seq(mdev, peer_seq);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002073 drbd_send_ack_dp(mdev, P_NEG_ACK, p, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002074 atomic_inc(&mdev->current_epoch->epoch_size);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002075 err2 = drbd_drain_block(mdev, pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002076 if (!err)
2077 err = err2;
2078 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002079 }
2080
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01002081 /*
2082 * Corresponding put_ldev done either below (on various errors), or in
2083 * drbd_peer_request_endio, if we successfully submit the data at the
2084 * end of this function.
2085 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002086
2087 sector = be64_to_cpu(p->sector);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002088 peer_req = read_in_block(mdev, p->block_id, sector, pi->size);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002089 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002090 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002091 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002092 }
2093
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002094 peer_req->w.cb = e_end_block;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002095
Lars Ellenberg688593c2010-11-17 22:25:03 +01002096 dp_flags = be32_to_cpu(p->dp_flags);
2097 rw |= wire_flags_to_bio(mdev, dp_flags);
2098
2099 if (dp_flags & DP_MAY_SET_IN_SYNC)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002100 peer_req->flags |= EE_MAY_SET_IN_SYNC;
Lars Ellenberg688593c2010-11-17 22:25:03 +01002101
Philipp Reisnerb411b362009-09-25 16:07:19 -07002102 spin_lock(&mdev->epoch_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002103 peer_req->epoch = mdev->current_epoch;
2104 atomic_inc(&peer_req->epoch->epoch_size);
2105 atomic_inc(&peer_req->epoch->active);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002106 spin_unlock(&mdev->epoch_lock);
2107
Philipp Reisner302bdea2011-04-21 11:36:49 +02002108 rcu_read_lock();
2109 tp = rcu_dereference(mdev->tconn->net_conf)->two_primaries;
2110 rcu_read_unlock();
2111 if (tp) {
2112 peer_req->flags |= EE_IN_INTERVAL_TREE;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002113 err = wait_for_and_update_peer_seq(mdev, peer_seq);
2114 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002115 goto out_interrupted;
Philipp Reisner87eeee42011-01-19 14:16:30 +01002116 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002117 err = handle_write_conflicts(mdev, peer_req);
2118 if (err) {
2119 spin_unlock_irq(&mdev->tconn->req_lock);
2120 if (err == -ENOENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002121 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002122 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002123 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002124 goto out_interrupted;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002125 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002126 } else
2127 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002128 list_add(&peer_req->w.list, &mdev->active_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002129 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002130
Philipp Reisner303d1442011-04-13 16:24:47 -07002131 if (mdev->tconn->agreed_pro_version < 100) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02002132 rcu_read_lock();
2133 switch (rcu_dereference(mdev->tconn->net_conf)->wire_protocol) {
Philipp Reisner303d1442011-04-13 16:24:47 -07002134 case DRBD_PROT_C:
2135 dp_flags |= DP_SEND_WRITE_ACK;
2136 break;
2137 case DRBD_PROT_B:
2138 dp_flags |= DP_SEND_RECEIVE_ACK;
2139 break;
2140 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002141 rcu_read_unlock();
Philipp Reisner303d1442011-04-13 16:24:47 -07002142 }
2143
2144 if (dp_flags & DP_SEND_WRITE_ACK) {
2145 peer_req->flags |= EE_SEND_WRITE_ACK;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002146 inc_unacked(mdev);
2147 /* corresponding dec_unacked() in e_end_block()
2148 * respective _drbd_clear_done_ee */
Philipp Reisner303d1442011-04-13 16:24:47 -07002149 }
2150
2151 if (dp_flags & DP_SEND_RECEIVE_ACK) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002152 /* I really don't like it that the receiver thread
2153 * sends on the msock, but anyways */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002154 drbd_send_ack(mdev, P_RECV_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002155 }
2156
Lars Ellenberg6719fb02010-10-18 23:04:07 +02002157 if (mdev->state.pdsk < D_INCONSISTENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002158 /* In case we have the only disk of the cluster, */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002159 drbd_set_out_of_sync(mdev, peer_req->i.sector, peer_req->i.size);
2160 peer_req->flags |= EE_CALL_AL_COMPLETE_IO;
2161 peer_req->flags &= ~EE_MAY_SET_IN_SYNC;
Lars Ellenberg181286a2011-03-31 15:18:56 +02002162 drbd_al_begin_io(mdev, &peer_req->i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002163 }
2164
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002165 err = drbd_submit_peer_request(mdev, peer_req, rw, DRBD_FAULT_DT_WR);
2166 if (!err)
2167 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002168
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01002169 /* don't care for the reason here */
2170 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002171 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002172 list_del(&peer_req->w.list);
2173 drbd_remove_epoch_entry_interval(mdev, peer_req);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002174 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002175 if (peer_req->flags & EE_CALL_AL_COMPLETE_IO)
Lars Ellenberg181286a2011-03-31 15:18:56 +02002176 drbd_al_complete_io(mdev, &peer_req->i);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002177
Philipp Reisnerb411b362009-09-25 16:07:19 -07002178out_interrupted:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002179 drbd_may_finish_epoch(mdev, peer_req->epoch, EV_PUT + EV_CLEANUP);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002180 put_ldev(mdev);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02002181 drbd_free_peer_req(mdev, peer_req);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002182 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002183}
2184
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002185/* We may throttle resync, if the lower device seems to be busy,
2186 * and current sync rate is above c_min_rate.
2187 *
2188 * To decide whether or not the lower device is busy, we use a scheme similar
2189 * to MD RAID is_mddev_idle(): if the partition stats reveal "significant"
2190 * (more than 64 sectors) of activity we cannot account for with our own resync
2191 * activity, it obviously is "busy".
2192 *
2193 * The current sync rate used here uses only the most recent two step marks,
2194 * to have a short time average so we can react faster.
2195 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002196int drbd_rs_should_slow_down(struct drbd_conf *mdev, sector_t sector)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002197{
2198 struct gendisk *disk = mdev->ldev->backing_bdev->bd_contains->bd_disk;
2199 unsigned long db, dt, dbdt;
Philipp Reisnere3555d82010-11-07 15:56:29 +01002200 struct lc_element *tmp;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002201 int curr_events;
2202 int throttle = 0;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002203 unsigned int c_min_rate;
2204
2205 rcu_read_lock();
2206 c_min_rate = rcu_dereference(mdev->ldev->disk_conf)->c_min_rate;
2207 rcu_read_unlock();
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002208
2209 /* feature disabled? */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002210 if (c_min_rate == 0)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002211 return 0;
2212
Philipp Reisnere3555d82010-11-07 15:56:29 +01002213 spin_lock_irq(&mdev->al_lock);
2214 tmp = lc_find(mdev->resync, BM_SECT_TO_EXT(sector));
2215 if (tmp) {
2216 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
2217 if (test_bit(BME_PRIORITY, &bm_ext->flags)) {
2218 spin_unlock_irq(&mdev->al_lock);
2219 return 0;
2220 }
2221 /* Do not slow down if app IO is already waiting for this extent */
2222 }
2223 spin_unlock_irq(&mdev->al_lock);
2224
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002225 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
2226 (int)part_stat_read(&disk->part0, sectors[1]) -
2227 atomic_read(&mdev->rs_sect_ev);
Philipp Reisnere3555d82010-11-07 15:56:29 +01002228
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002229 if (!mdev->rs_last_events || curr_events - mdev->rs_last_events > 64) {
2230 unsigned long rs_left;
2231 int i;
2232
2233 mdev->rs_last_events = curr_events;
2234
2235 /* sync speed average over the last 2*DRBD_SYNC_MARK_STEP,
2236 * approx. */
Lars Ellenberg2649f082010-11-05 10:05:47 +01002237 i = (mdev->rs_last_mark + DRBD_SYNC_MARKS-1) % DRBD_SYNC_MARKS;
2238
2239 if (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T)
2240 rs_left = mdev->ov_left;
2241 else
2242 rs_left = drbd_bm_total_weight(mdev) - mdev->rs_failed;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002243
2244 dt = ((long)jiffies - (long)mdev->rs_mark_time[i]) / HZ;
2245 if (!dt)
2246 dt++;
2247 db = mdev->rs_mark_left[i] - rs_left;
2248 dbdt = Bit2KB(db/dt);
2249
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002250 if (dbdt > c_min_rate)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002251 throttle = 1;
2252 }
2253 return throttle;
2254}
2255
2256
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002257static int receive_DataRequest(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002258{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002259 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002260 sector_t sector;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002261 sector_t capacity;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002262 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002263 struct digest_info *di = NULL;
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002264 int size, verb;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002265 unsigned int fault_type;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02002266 struct p_block_req *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002267
2268 mdev = vnr_to_mdev(tconn, pi->vnr);
2269 if (!mdev)
2270 return -EIO;
2271 capacity = drbd_get_capacity(mdev->this_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002272
2273 sector = be64_to_cpu(p->sector);
2274 size = be32_to_cpu(p->blksize);
2275
Andreas Gruenbacherc670a392011-02-21 12:41:39 +01002276 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002277 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2278 (unsigned long long)sector, size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002279 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002280 }
2281 if (sector + (size>>9) > capacity) {
2282 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2283 (unsigned long long)sector, size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002284 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002285 }
2286
2287 if (!get_ldev_if_state(mdev, D_UP_TO_DATE)) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002288 verb = 1;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002289 switch (pi->cmd) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002290 case P_DATA_REQUEST:
2291 drbd_send_ack_rp(mdev, P_NEG_DREPLY, p);
2292 break;
2293 case P_RS_DATA_REQUEST:
2294 case P_CSUM_RS_REQUEST:
2295 case P_OV_REQUEST:
2296 drbd_send_ack_rp(mdev, P_NEG_RS_DREPLY , p);
2297 break;
2298 case P_OV_REPLY:
2299 verb = 0;
2300 dec_rs_pending(mdev);
2301 drbd_send_ack_ex(mdev, P_OV_RESULT, sector, size, ID_IN_SYNC);
2302 break;
2303 default:
Andreas Gruenbacher49ba9b12011-03-25 00:35:45 +01002304 BUG();
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002305 }
2306 if (verb && __ratelimit(&drbd_ratelimit_state))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002307 dev_err(DEV, "Can not satisfy peer's read request, "
2308 "no local data.\n");
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002309
Lars Ellenberga821cc42010-09-06 12:31:37 +02002310 /* drain possibly payload */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002311 return drbd_drain_block(mdev, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002312 }
2313
2314 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
2315 * "criss-cross" setup, that might cause write-out on some other DRBD,
2316 * which in turn might block on the other node at this very place. */
Andreas Gruenbacher0db55362011-04-06 16:09:15 +02002317 peer_req = drbd_alloc_peer_req(mdev, p->block_id, sector, size, GFP_NOIO);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002318 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002319 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002320 return -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002321 }
2322
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002323 switch (pi->cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002324 case P_DATA_REQUEST:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002325 peer_req->w.cb = w_e_end_data_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002326 fault_type = DRBD_FAULT_DT_RD;
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002327 /* application IO, don't drbd_rs_begin_io */
2328 goto submit;
2329
Philipp Reisnerb411b362009-09-25 16:07:19 -07002330 case P_RS_DATA_REQUEST:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002331 peer_req->w.cb = w_e_end_rsdata_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002332 fault_type = DRBD_FAULT_RS_RD;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002333 /* used in the sector offset progress display */
2334 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002335 break;
2336
2337 case P_OV_REPLY:
2338 case P_CSUM_RS_REQUEST:
2339 fault_type = DRBD_FAULT_RS_RD;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002340 di = kmalloc(sizeof(*di) + pi->size, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002341 if (!di)
2342 goto out_free_e;
2343
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002344 di->digest_size = pi->size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002345 di->digest = (((char *)di)+sizeof(struct digest_info));
2346
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002347 peer_req->digest = di;
2348 peer_req->flags |= EE_HAS_DIGEST;
Lars Ellenbergc36c3ce2010-08-11 20:42:55 +02002349
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002350 if (drbd_recv_all(mdev->tconn, di->digest, pi->size))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002351 goto out_free_e;
2352
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002353 if (pi->cmd == P_CSUM_RS_REQUEST) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002354 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002355 peer_req->w.cb = w_e_end_csum_rs_req;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002356 /* used in the sector offset progress display */
2357 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002358 } else if (pi->cmd == P_OV_REPLY) {
Lars Ellenberg2649f082010-11-05 10:05:47 +01002359 /* track progress, we may need to throttle */
2360 atomic_add(size >> 9, &mdev->rs_sect_in);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002361 peer_req->w.cb = w_e_end_ov_reply;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002362 dec_rs_pending(mdev);
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002363 /* drbd_rs_begin_io done when we sent this request,
2364 * but accounting still needs to be done. */
2365 goto submit_for_resync;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002366 }
2367 break;
2368
2369 case P_OV_REQUEST:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002370 if (mdev->ov_start_sector == ~(sector_t)0 &&
Philipp Reisner31890f42011-01-19 14:12:51 +01002371 mdev->tconn->agreed_pro_version >= 90) {
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002372 unsigned long now = jiffies;
2373 int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002374 mdev->ov_start_sector = sector;
2375 mdev->ov_position = sector;
Lars Ellenberg30b743a2010-11-05 09:39:06 +01002376 mdev->ov_left = drbd_bm_bits(mdev) - BM_SECT_TO_BIT(sector);
2377 mdev->rs_total = mdev->ov_left;
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002378 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
2379 mdev->rs_mark_left[i] = mdev->ov_left;
2380 mdev->rs_mark_time[i] = now;
2381 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002382 dev_info(DEV, "Online Verify start sector: %llu\n",
2383 (unsigned long long)sector);
2384 }
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002385 peer_req->w.cb = w_e_end_ov_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002386 fault_type = DRBD_FAULT_RS_RD;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002387 break;
2388
Philipp Reisnerb411b362009-09-25 16:07:19 -07002389 default:
Andreas Gruenbacher49ba9b12011-03-25 00:35:45 +01002390 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002391 }
2392
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002393 /* Throttle, drbd_rs_begin_io and submit should become asynchronous
2394 * wrt the receiver, but it is not as straightforward as it may seem.
2395 * Various places in the resync start and stop logic assume resync
2396 * requests are processed in order, requeuing this on the worker thread
2397 * introduces a bunch of new code for synchronization between threads.
2398 *
2399 * Unlimited throttling before drbd_rs_begin_io may stall the resync
2400 * "forever", throttling after drbd_rs_begin_io will lock that extent
2401 * for application writes for the same time. For now, just throttle
2402 * here, where the rest of the code expects the receiver to sleep for
2403 * a while, anyways.
2404 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002405
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002406 /* Throttle before drbd_rs_begin_io, as that locks out application IO;
2407 * this defers syncer requests for some time, before letting at least
2408 * on request through. The resync controller on the receiving side
2409 * will adapt to the incoming rate accordingly.
2410 *
2411 * We cannot throttle here if remote is Primary/SyncTarget:
2412 * we would also throttle its application reads.
2413 * In that case, throttling is done on the SyncTarget only.
2414 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002415 if (mdev->state.peer != R_PRIMARY && drbd_rs_should_slow_down(mdev, sector))
2416 schedule_timeout_uninterruptible(HZ/10);
2417 if (drbd_rs_begin_io(mdev, sector))
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002418 goto out_free_e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002419
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002420submit_for_resync:
2421 atomic_add(size >> 9, &mdev->rs_sect_ev);
2422
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002423submit:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002424 inc_unacked(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002425 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002426 list_add_tail(&peer_req->w.list, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002427 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002428
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01002429 if (drbd_submit_peer_request(mdev, peer_req, READ, fault_type) == 0)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002430 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002431
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01002432 /* don't care for the reason here */
2433 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002434 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002435 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002436 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002437 /* no drbd_rs_complete_io(), we are dropping the connection anyways */
2438
Philipp Reisnerb411b362009-09-25 16:07:19 -07002439out_free_e:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002440 put_ldev(mdev);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02002441 drbd_free_peer_req(mdev, peer_req);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002442 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002443}
2444
2445static int drbd_asb_recover_0p(struct drbd_conf *mdev) __must_hold(local)
2446{
2447 int self, peer, rv = -100;
2448 unsigned long ch_self, ch_peer;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002449 enum drbd_after_sb_p after_sb_0p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002450
2451 self = mdev->ldev->md.uuid[UI_BITMAP] & 1;
2452 peer = mdev->p_uuid[UI_BITMAP] & 1;
2453
2454 ch_peer = mdev->p_uuid[UI_SIZE];
2455 ch_self = mdev->comm_bm_set;
2456
Philipp Reisner44ed1672011-04-19 17:10:19 +02002457 rcu_read_lock();
2458 after_sb_0p = rcu_dereference(mdev->tconn->net_conf)->after_sb_0p;
2459 rcu_read_unlock();
2460 switch (after_sb_0p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002461 case ASB_CONSENSUS:
2462 case ASB_DISCARD_SECONDARY:
2463 case ASB_CALL_HELPER:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002464 case ASB_VIOLENTLY:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002465 dev_err(DEV, "Configuration error.\n");
2466 break;
2467 case ASB_DISCONNECT:
2468 break;
2469 case ASB_DISCARD_YOUNGER_PRI:
2470 if (self == 0 && peer == 1) {
2471 rv = -1;
2472 break;
2473 }
2474 if (self == 1 && peer == 0) {
2475 rv = 1;
2476 break;
2477 }
2478 /* Else fall through to one of the other strategies... */
2479 case ASB_DISCARD_OLDER_PRI:
2480 if (self == 0 && peer == 1) {
2481 rv = 1;
2482 break;
2483 }
2484 if (self == 1 && peer == 0) {
2485 rv = -1;
2486 break;
2487 }
2488 /* Else fall through to one of the other strategies... */
Lars Ellenbergad19bf62009-10-14 09:36:49 +02002489 dev_warn(DEV, "Discard younger/older primary did not find a decision\n"
Philipp Reisnerb411b362009-09-25 16:07:19 -07002490 "Using discard-least-changes instead\n");
2491 case ASB_DISCARD_ZERO_CHG:
2492 if (ch_peer == 0 && ch_self == 0) {
Philipp Reisner25703f82011-02-07 14:35:25 +01002493 rv = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002494 ? -1 : 1;
2495 break;
2496 } else {
2497 if (ch_peer == 0) { rv = 1; break; }
2498 if (ch_self == 0) { rv = -1; break; }
2499 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002500 if (after_sb_0p == ASB_DISCARD_ZERO_CHG)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002501 break;
2502 case ASB_DISCARD_LEAST_CHG:
2503 if (ch_self < ch_peer)
2504 rv = -1;
2505 else if (ch_self > ch_peer)
2506 rv = 1;
2507 else /* ( ch_self == ch_peer ) */
2508 /* Well, then use something else. */
Philipp Reisner25703f82011-02-07 14:35:25 +01002509 rv = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002510 ? -1 : 1;
2511 break;
2512 case ASB_DISCARD_LOCAL:
2513 rv = -1;
2514 break;
2515 case ASB_DISCARD_REMOTE:
2516 rv = 1;
2517 }
2518
2519 return rv;
2520}
2521
2522static int drbd_asb_recover_1p(struct drbd_conf *mdev) __must_hold(local)
2523{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002524 int hg, rv = -100;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002525 enum drbd_after_sb_p after_sb_1p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002526
Philipp Reisner44ed1672011-04-19 17:10:19 +02002527 rcu_read_lock();
2528 after_sb_1p = rcu_dereference(mdev->tconn->net_conf)->after_sb_1p;
2529 rcu_read_unlock();
2530 switch (after_sb_1p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002531 case ASB_DISCARD_YOUNGER_PRI:
2532 case ASB_DISCARD_OLDER_PRI:
2533 case ASB_DISCARD_LEAST_CHG:
2534 case ASB_DISCARD_LOCAL:
2535 case ASB_DISCARD_REMOTE:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002536 case ASB_DISCARD_ZERO_CHG:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002537 dev_err(DEV, "Configuration error.\n");
2538 break;
2539 case ASB_DISCONNECT:
2540 break;
2541 case ASB_CONSENSUS:
2542 hg = drbd_asb_recover_0p(mdev);
2543 if (hg == -1 && mdev->state.role == R_SECONDARY)
2544 rv = hg;
2545 if (hg == 1 && mdev->state.role == R_PRIMARY)
2546 rv = hg;
2547 break;
2548 case ASB_VIOLENTLY:
2549 rv = drbd_asb_recover_0p(mdev);
2550 break;
2551 case ASB_DISCARD_SECONDARY:
2552 return mdev->state.role == R_PRIMARY ? 1 : -1;
2553 case ASB_CALL_HELPER:
2554 hg = drbd_asb_recover_0p(mdev);
2555 if (hg == -1 && mdev->state.role == R_PRIMARY) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002556 enum drbd_state_rv rv2;
2557
2558 drbd_set_role(mdev, R_SECONDARY, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002559 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2560 * we might be here in C_WF_REPORT_PARAMS which is transient.
2561 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002562 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2563 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002564 drbd_khelper(mdev, "pri-lost-after-sb");
2565 } else {
2566 dev_warn(DEV, "Successfully gave up primary role.\n");
2567 rv = hg;
2568 }
2569 } else
2570 rv = hg;
2571 }
2572
2573 return rv;
2574}
2575
2576static int drbd_asb_recover_2p(struct drbd_conf *mdev) __must_hold(local)
2577{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002578 int hg, rv = -100;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002579 enum drbd_after_sb_p after_sb_2p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002580
Philipp Reisner44ed1672011-04-19 17:10:19 +02002581 rcu_read_lock();
2582 after_sb_2p = rcu_dereference(mdev->tconn->net_conf)->after_sb_2p;
2583 rcu_read_unlock();
2584 switch (after_sb_2p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002585 case ASB_DISCARD_YOUNGER_PRI:
2586 case ASB_DISCARD_OLDER_PRI:
2587 case ASB_DISCARD_LEAST_CHG:
2588 case ASB_DISCARD_LOCAL:
2589 case ASB_DISCARD_REMOTE:
2590 case ASB_CONSENSUS:
2591 case ASB_DISCARD_SECONDARY:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002592 case ASB_DISCARD_ZERO_CHG:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002593 dev_err(DEV, "Configuration error.\n");
2594 break;
2595 case ASB_VIOLENTLY:
2596 rv = drbd_asb_recover_0p(mdev);
2597 break;
2598 case ASB_DISCONNECT:
2599 break;
2600 case ASB_CALL_HELPER:
2601 hg = drbd_asb_recover_0p(mdev);
2602 if (hg == -1) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002603 enum drbd_state_rv rv2;
2604
Philipp Reisnerb411b362009-09-25 16:07:19 -07002605 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2606 * we might be here in C_WF_REPORT_PARAMS which is transient.
2607 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002608 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2609 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002610 drbd_khelper(mdev, "pri-lost-after-sb");
2611 } else {
2612 dev_warn(DEV, "Successfully gave up primary role.\n");
2613 rv = hg;
2614 }
2615 } else
2616 rv = hg;
2617 }
2618
2619 return rv;
2620}
2621
2622static void drbd_uuid_dump(struct drbd_conf *mdev, char *text, u64 *uuid,
2623 u64 bits, u64 flags)
2624{
2625 if (!uuid) {
2626 dev_info(DEV, "%s uuid info vanished while I was looking!\n", text);
2627 return;
2628 }
2629 dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX bits:%llu flags:%llX\n",
2630 text,
2631 (unsigned long long)uuid[UI_CURRENT],
2632 (unsigned long long)uuid[UI_BITMAP],
2633 (unsigned long long)uuid[UI_HISTORY_START],
2634 (unsigned long long)uuid[UI_HISTORY_END],
2635 (unsigned long long)bits,
2636 (unsigned long long)flags);
2637}
2638
2639/*
2640 100 after split brain try auto recover
2641 2 C_SYNC_SOURCE set BitMap
2642 1 C_SYNC_SOURCE use BitMap
2643 0 no Sync
2644 -1 C_SYNC_TARGET use BitMap
2645 -2 C_SYNC_TARGET set BitMap
2646 -100 after split brain, disconnect
2647-1000 unrelated data
Philipp Reisner4a23f262011-01-11 17:42:17 +01002648-1091 requires proto 91
2649-1096 requires proto 96
Philipp Reisnerb411b362009-09-25 16:07:19 -07002650 */
2651static int drbd_uuid_compare(struct drbd_conf *mdev, int *rule_nr) __must_hold(local)
2652{
2653 u64 self, peer;
2654 int i, j;
2655
2656 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2657 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2658
2659 *rule_nr = 10;
2660 if (self == UUID_JUST_CREATED && peer == UUID_JUST_CREATED)
2661 return 0;
2662
2663 *rule_nr = 20;
2664 if ((self == UUID_JUST_CREATED || self == (u64)0) &&
2665 peer != UUID_JUST_CREATED)
2666 return -2;
2667
2668 *rule_nr = 30;
2669 if (self != UUID_JUST_CREATED &&
2670 (peer == UUID_JUST_CREATED || peer == (u64)0))
2671 return 2;
2672
2673 if (self == peer) {
2674 int rct, dc; /* roles at crash time */
2675
2676 if (mdev->p_uuid[UI_BITMAP] == (u64)0 && mdev->ldev->md.uuid[UI_BITMAP] != (u64)0) {
2677
Philipp Reisner31890f42011-01-19 14:12:51 +01002678 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002679 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002680
2681 if ((mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) &&
2682 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1))) {
2683 dev_info(DEV, "was SyncSource, missed the resync finished event, corrected myself:\n");
2684 drbd_uuid_set_bm(mdev, 0UL);
2685
2686 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2687 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2688 *rule_nr = 34;
2689 } else {
2690 dev_info(DEV, "was SyncSource (peer failed to write sync_uuid)\n");
2691 *rule_nr = 36;
2692 }
2693
2694 return 1;
2695 }
2696
2697 if (mdev->ldev->md.uuid[UI_BITMAP] == (u64)0 && mdev->p_uuid[UI_BITMAP] != (u64)0) {
2698
Philipp Reisner31890f42011-01-19 14:12:51 +01002699 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002700 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002701
2702 if ((mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_BITMAP] & ~((u64)1)) &&
2703 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1))) {
2704 dev_info(DEV, "was SyncTarget, peer missed the resync finished event, corrected peer:\n");
2705
2706 mdev->p_uuid[UI_HISTORY_START + 1] = mdev->p_uuid[UI_HISTORY_START];
2707 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_BITMAP];
2708 mdev->p_uuid[UI_BITMAP] = 0UL;
2709
2710 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2711 *rule_nr = 35;
2712 } else {
2713 dev_info(DEV, "was SyncTarget (failed to write sync_uuid)\n");
2714 *rule_nr = 37;
2715 }
2716
2717 return -1;
2718 }
2719
2720 /* Common power [off|failure] */
2721 rct = (test_bit(CRASHED_PRIMARY, &mdev->flags) ? 1 : 0) +
2722 (mdev->p_uuid[UI_FLAGS] & 2);
2723 /* lowest bit is set when we were primary,
2724 * next bit (weight 2) is set when peer was primary */
2725 *rule_nr = 40;
2726
2727 switch (rct) {
2728 case 0: /* !self_pri && !peer_pri */ return 0;
2729 case 1: /* self_pri && !peer_pri */ return 1;
2730 case 2: /* !self_pri && peer_pri */ return -1;
2731 case 3: /* self_pri && peer_pri */
Philipp Reisner25703f82011-02-07 14:35:25 +01002732 dc = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002733 return dc ? -1 : 1;
2734 }
2735 }
2736
2737 *rule_nr = 50;
2738 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2739 if (self == peer)
2740 return -1;
2741
2742 *rule_nr = 51;
2743 peer = mdev->p_uuid[UI_HISTORY_START] & ~((u64)1);
2744 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002745 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002746 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) ==
2747 (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1)) :
2748 peer + UUID_NEW_BM_OFFSET == (mdev->p_uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002749 /* The last P_SYNC_UUID did not get though. Undo the last start of
2750 resync as sync source modifications of the peer's UUIDs. */
2751
Philipp Reisner31890f42011-01-19 14:12:51 +01002752 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002753 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002754
2755 mdev->p_uuid[UI_BITMAP] = mdev->p_uuid[UI_HISTORY_START];
2756 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_HISTORY_START + 1];
Philipp Reisner4a23f262011-01-11 17:42:17 +01002757
2758 dev_info(DEV, "Did not got last syncUUID packet, corrected:\n");
2759 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2760
Philipp Reisnerb411b362009-09-25 16:07:19 -07002761 return -1;
2762 }
2763 }
2764
2765 *rule_nr = 60;
2766 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2767 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2768 peer = mdev->p_uuid[i] & ~((u64)1);
2769 if (self == peer)
2770 return -2;
2771 }
2772
2773 *rule_nr = 70;
2774 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2775 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2776 if (self == peer)
2777 return 1;
2778
2779 *rule_nr = 71;
2780 self = mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1);
2781 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002782 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002783 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) ==
2784 (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) :
2785 self + UUID_NEW_BM_OFFSET == (mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002786 /* The last P_SYNC_UUID did not get though. Undo the last start of
2787 resync as sync source modifications of our UUIDs. */
2788
Philipp Reisner31890f42011-01-19 14:12:51 +01002789 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002790 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002791
2792 _drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_HISTORY_START]);
2793 _drbd_uuid_set(mdev, UI_HISTORY_START, mdev->ldev->md.uuid[UI_HISTORY_START + 1]);
2794
Philipp Reisner4a23f262011-01-11 17:42:17 +01002795 dev_info(DEV, "Last syncUUID did not get through, corrected:\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002796 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2797 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2798
2799 return 1;
2800 }
2801 }
2802
2803
2804 *rule_nr = 80;
Philipp Reisnerd8c2a362009-11-18 15:52:51 +01002805 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002806 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2807 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2808 if (self == peer)
2809 return 2;
2810 }
2811
2812 *rule_nr = 90;
2813 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2814 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2815 if (self == peer && self != ((u64)0))
2816 return 100;
2817
2818 *rule_nr = 100;
2819 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2820 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2821 for (j = UI_HISTORY_START; j <= UI_HISTORY_END; j++) {
2822 peer = mdev->p_uuid[j] & ~((u64)1);
2823 if (self == peer)
2824 return -100;
2825 }
2826 }
2827
2828 return -1000;
2829}
2830
2831/* drbd_sync_handshake() returns the new conn state on success, or
2832 CONN_MASK (-1) on failure.
2833 */
2834static enum drbd_conns drbd_sync_handshake(struct drbd_conf *mdev, enum drbd_role peer_role,
2835 enum drbd_disk_state peer_disk) __must_hold(local)
2836{
Philipp Reisnerb411b362009-09-25 16:07:19 -07002837 enum drbd_conns rv = C_MASK;
2838 enum drbd_disk_state mydisk;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002839 struct net_conf *nc;
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02002840 int hg, rule_nr, rr_conflict, tentative;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002841
2842 mydisk = mdev->state.disk;
2843 if (mydisk == D_NEGOTIATING)
2844 mydisk = mdev->new_state_tmp.disk;
2845
2846 dev_info(DEV, "drbd_sync_handshake:\n");
2847 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid, mdev->comm_bm_set, 0);
2848 drbd_uuid_dump(mdev, "peer", mdev->p_uuid,
2849 mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2850
2851 hg = drbd_uuid_compare(mdev, &rule_nr);
2852
2853 dev_info(DEV, "uuid_compare()=%d by rule %d\n", hg, rule_nr);
2854
2855 if (hg == -1000) {
2856 dev_alert(DEV, "Unrelated data, aborting!\n");
2857 return C_MASK;
2858 }
Philipp Reisner4a23f262011-01-11 17:42:17 +01002859 if (hg < -1000) {
2860 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 -07002861 return C_MASK;
2862 }
2863
2864 if ((mydisk == D_INCONSISTENT && peer_disk > D_INCONSISTENT) ||
2865 (peer_disk == D_INCONSISTENT && mydisk > D_INCONSISTENT)) {
2866 int f = (hg == -100) || abs(hg) == 2;
2867 hg = mydisk > D_INCONSISTENT ? 1 : -1;
2868 if (f)
2869 hg = hg*2;
2870 dev_info(DEV, "Becoming sync %s due to disk states.\n",
2871 hg > 0 ? "source" : "target");
2872 }
2873
Adam Gandelman3a11a482010-04-08 16:48:23 -07002874 if (abs(hg) == 100)
2875 drbd_khelper(mdev, "initial-split-brain");
2876
Philipp Reisner44ed1672011-04-19 17:10:19 +02002877 rcu_read_lock();
2878 nc = rcu_dereference(mdev->tconn->net_conf);
2879
2880 if (hg == 100 || (hg == -100 && nc->always_asbp)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002881 int pcount = (mdev->state.role == R_PRIMARY)
2882 + (peer_role == R_PRIMARY);
2883 int forced = (hg == -100);
2884
2885 switch (pcount) {
2886 case 0:
2887 hg = drbd_asb_recover_0p(mdev);
2888 break;
2889 case 1:
2890 hg = drbd_asb_recover_1p(mdev);
2891 break;
2892 case 2:
2893 hg = drbd_asb_recover_2p(mdev);
2894 break;
2895 }
2896 if (abs(hg) < 100) {
2897 dev_warn(DEV, "Split-Brain detected, %d primaries, "
2898 "automatically solved. Sync from %s node\n",
2899 pcount, (hg < 0) ? "peer" : "this");
2900 if (forced) {
2901 dev_warn(DEV, "Doing a full sync, since"
2902 " UUIDs where ambiguous.\n");
2903 hg = hg*2;
2904 }
2905 }
2906 }
2907
2908 if (hg == -100) {
Andreas Gruenbacher6139f602011-05-06 20:00:02 +02002909 if (nc->discard_my_data && !(mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002910 hg = -1;
Andreas Gruenbacher6139f602011-05-06 20:00:02 +02002911 if (!nc->discard_my_data && (mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002912 hg = 1;
2913
2914 if (abs(hg) < 100)
2915 dev_warn(DEV, "Split-Brain detected, manually solved. "
2916 "Sync from %s node\n",
2917 (hg < 0) ? "peer" : "this");
2918 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002919 rr_conflict = nc->rr_conflict;
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02002920 tentative = nc->tentative;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002921 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002922
2923 if (hg == -100) {
Lars Ellenberg580b9762010-02-26 23:15:23 +01002924 /* FIXME this log message is not correct if we end up here
2925 * after an attempted attach on a diskless node.
2926 * We just refuse to attach -- well, we drop the "connection"
2927 * to that disk, in a way... */
Adam Gandelman3a11a482010-04-08 16:48:23 -07002928 dev_alert(DEV, "Split-Brain detected but unresolved, dropping connection!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002929 drbd_khelper(mdev, "split-brain");
2930 return C_MASK;
2931 }
2932
2933 if (hg > 0 && mydisk <= D_INCONSISTENT) {
2934 dev_err(DEV, "I shall become SyncSource, but I am inconsistent!\n");
2935 return C_MASK;
2936 }
2937
2938 if (hg < 0 && /* by intention we do not use mydisk here. */
2939 mdev->state.role == R_PRIMARY && mdev->state.disk >= D_CONSISTENT) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02002940 switch (rr_conflict) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002941 case ASB_CALL_HELPER:
2942 drbd_khelper(mdev, "pri-lost");
2943 /* fall through */
2944 case ASB_DISCONNECT:
2945 dev_err(DEV, "I shall become SyncTarget, but I am primary!\n");
2946 return C_MASK;
2947 case ASB_VIOLENTLY:
2948 dev_warn(DEV, "Becoming SyncTarget, violating the stable-data"
2949 "assumption\n");
2950 }
2951 }
2952
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02002953 if (tentative || test_bit(CONN_DRY_RUN, &mdev->tconn->flags)) {
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002954 if (hg == 0)
2955 dev_info(DEV, "dry-run connect: No resync, would become Connected immediately.\n");
2956 else
2957 dev_info(DEV, "dry-run connect: Would become %s, doing a %s resync.",
2958 drbd_conn_str(hg > 0 ? C_SYNC_SOURCE : C_SYNC_TARGET),
2959 abs(hg) >= 2 ? "full" : "bit-map based");
2960 return C_MASK;
2961 }
2962
Philipp Reisnerb411b362009-09-25 16:07:19 -07002963 if (abs(hg) >= 2) {
2964 dev_info(DEV, "Writing the whole bitmap, full sync required after drbd_sync_handshake.\n");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002965 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write, "set_n_write from sync_handshake",
2966 BM_LOCKED_SET_ALLOWED))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002967 return C_MASK;
2968 }
2969
2970 if (hg > 0) { /* become sync source. */
2971 rv = C_WF_BITMAP_S;
2972 } else if (hg < 0) { /* become sync target */
2973 rv = C_WF_BITMAP_T;
2974 } else {
2975 rv = C_CONNECTED;
2976 if (drbd_bm_total_weight(mdev)) {
2977 dev_info(DEV, "No resync, but %lu bits in bitmap!\n",
2978 drbd_bm_total_weight(mdev));
2979 }
2980 }
2981
2982 return rv;
2983}
2984
Philipp Reisnerf179d762011-05-16 17:31:47 +02002985static enum drbd_after_sb_p convert_after_sb(enum drbd_after_sb_p peer)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002986{
2987 /* ASB_DISCARD_REMOTE - ASB_DISCARD_LOCAL is valid */
Philipp Reisnerf179d762011-05-16 17:31:47 +02002988 if (peer == ASB_DISCARD_REMOTE)
2989 return ASB_DISCARD_LOCAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002990
2991 /* any other things with ASB_DISCARD_REMOTE or ASB_DISCARD_LOCAL are invalid */
Philipp Reisnerf179d762011-05-16 17:31:47 +02002992 if (peer == ASB_DISCARD_LOCAL)
2993 return ASB_DISCARD_REMOTE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002994
2995 /* everything else is valid if they are equal on both sides. */
Philipp Reisnerf179d762011-05-16 17:31:47 +02002996 return peer;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002997}
2998
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002999static int receive_protocol(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003000{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003001 struct p_protocol *p = pi->data;
Philipp Reisner036b17e2011-05-16 17:38:11 +02003002 enum drbd_after_sb_p p_after_sb_0p, p_after_sb_1p, p_after_sb_2p;
3003 int p_proto, p_discard_my_data, p_two_primaries, cf;
3004 struct net_conf *nc, *old_net_conf, *new_net_conf = NULL;
3005 char integrity_alg[SHARED_SECRET_MAX] = "";
Andreas Gruenbacheraccdbcc2011-07-15 17:41:09 +02003006 struct crypto_hash *peer_integrity_tfm = NULL;
Philipp Reisner7aca6c72011-05-17 10:12:56 +02003007 void *int_dig_in = NULL, *int_dig_vv = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003008
Philipp Reisnerb411b362009-09-25 16:07:19 -07003009 p_proto = be32_to_cpu(p->protocol);
3010 p_after_sb_0p = be32_to_cpu(p->after_sb_0p);
3011 p_after_sb_1p = be32_to_cpu(p->after_sb_1p);
3012 p_after_sb_2p = be32_to_cpu(p->after_sb_2p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003013 p_two_primaries = be32_to_cpu(p->two_primaries);
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003014 cf = be32_to_cpu(p->conn_flags);
Andreas Gruenbacher6139f602011-05-06 20:00:02 +02003015 p_discard_my_data = cf & CF_DISCARD_MY_DATA;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003016
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003017 if (tconn->agreed_pro_version >= 87) {
3018 int err;
3019
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02003020 if (pi->size > sizeof(integrity_alg))
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003021 return -EIO;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02003022 err = drbd_recv_all(tconn, integrity_alg, pi->size);
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003023 if (err)
3024 return err;
Philipp Reisner036b17e2011-05-16 17:38:11 +02003025 integrity_alg[SHARED_SECRET_MAX - 1] = 0;
3026 }
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003027
Andreas Gruenbacher7d4c7822011-07-17 23:06:12 +02003028 if (pi->cmd != P_PROTOCOL_UPDATE) {
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003029 clear_bit(CONN_DRY_RUN, &tconn->flags);
Philipp Reisner036b17e2011-05-16 17:38:11 +02003030
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003031 if (cf & CF_DRY_RUN)
3032 set_bit(CONN_DRY_RUN, &tconn->flags);
3033
3034 rcu_read_lock();
3035 nc = rcu_dereference(tconn->net_conf);
3036
3037 if (p_proto != nc->wire_protocol) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003038 conn_err(tconn, "incompatible %s settings\n", "protocol");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003039 goto disconnect_rcu_unlock;
3040 }
3041
3042 if (convert_after_sb(p_after_sb_0p) != nc->after_sb_0p) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003043 conn_err(tconn, "incompatible %s settings\n", "after-sb-0pri");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003044 goto disconnect_rcu_unlock;
3045 }
3046
3047 if (convert_after_sb(p_after_sb_1p) != nc->after_sb_1p) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003048 conn_err(tconn, "incompatible %s settings\n", "after-sb-1pri");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003049 goto disconnect_rcu_unlock;
3050 }
3051
3052 if (convert_after_sb(p_after_sb_2p) != nc->after_sb_2p) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003053 conn_err(tconn, "incompatible %s settings\n", "after-sb-2pri");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003054 goto disconnect_rcu_unlock;
3055 }
3056
3057 if (p_discard_my_data && nc->discard_my_data) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003058 conn_err(tconn, "incompatible %s settings\n", "discard-my-data");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003059 goto disconnect_rcu_unlock;
3060 }
3061
3062 if (p_two_primaries != nc->two_primaries) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003063 conn_err(tconn, "incompatible %s settings\n", "allow-two-primaries");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003064 goto disconnect_rcu_unlock;
3065 }
3066
3067 if (strcmp(integrity_alg, nc->integrity_alg)) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003068 conn_err(tconn, "incompatible %s settings\n", "data-integrity-alg");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003069 goto disconnect_rcu_unlock;
3070 }
3071
3072 rcu_read_unlock();
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003073 }
Andreas Gruenbacher7d4c7822011-07-17 23:06:12 +02003074
3075 if (integrity_alg[0]) {
3076 int hash_size;
3077
3078 /*
3079 * We can only change the peer data integrity algorithm
3080 * here. Changing our own data integrity algorithm
3081 * requires that we send a P_PROTOCOL_UPDATE packet at
3082 * the same time; otherwise, the peer has no way to
3083 * tell between which packets the algorithm should
3084 * change.
3085 */
3086
3087 peer_integrity_tfm = crypto_alloc_hash(integrity_alg, 0, CRYPTO_ALG_ASYNC);
3088 if (!peer_integrity_tfm) {
3089 conn_err(tconn, "peer data-integrity-alg %s not supported\n",
3090 integrity_alg);
3091 goto disconnect;
3092 }
3093
3094 hash_size = crypto_hash_digestsize(peer_integrity_tfm);
3095 int_dig_in = kmalloc(hash_size, GFP_KERNEL);
3096 int_dig_vv = kmalloc(hash_size, GFP_KERNEL);
3097 if (!(int_dig_in && int_dig_vv)) {
3098 conn_err(tconn, "Allocation of buffers for data integrity checking failed\n");
3099 goto disconnect;
3100 }
3101 }
3102
3103 new_net_conf = kmalloc(sizeof(struct net_conf), GFP_KERNEL);
3104 if (!new_net_conf) {
3105 conn_err(tconn, "Allocation of new net_conf failed\n");
3106 goto disconnect;
3107 }
3108
3109 mutex_lock(&tconn->data.mutex);
3110 mutex_lock(&tconn->conf_update);
3111 old_net_conf = tconn->net_conf;
3112 *new_net_conf = *old_net_conf;
3113
3114 new_net_conf->wire_protocol = p_proto;
3115 new_net_conf->after_sb_0p = convert_after_sb(p_after_sb_0p);
3116 new_net_conf->after_sb_1p = convert_after_sb(p_after_sb_1p);
3117 new_net_conf->after_sb_2p = convert_after_sb(p_after_sb_2p);
3118 new_net_conf->two_primaries = p_two_primaries;
3119
3120 rcu_assign_pointer(tconn->net_conf, new_net_conf);
3121 mutex_unlock(&tconn->conf_update);
3122 mutex_unlock(&tconn->data.mutex);
3123
3124 crypto_free_hash(tconn->peer_integrity_tfm);
3125 kfree(tconn->int_dig_in);
3126 kfree(tconn->int_dig_vv);
3127 tconn->peer_integrity_tfm = peer_integrity_tfm;
3128 tconn->int_dig_in = int_dig_in;
3129 tconn->int_dig_vv = int_dig_vv;
3130
3131 if (strcmp(old_net_conf->integrity_alg, integrity_alg))
3132 conn_info(tconn, "peer data-integrity-alg: %s\n",
3133 integrity_alg[0] ? integrity_alg : "(none)");
3134
3135 synchronize_rcu();
3136 kfree(old_net_conf);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003137 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003138
Philipp Reisner44ed1672011-04-19 17:10:19 +02003139disconnect_rcu_unlock:
3140 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07003141disconnect:
Andreas Gruenbacherb792c352011-07-15 16:48:49 +02003142 crypto_free_hash(peer_integrity_tfm);
Philipp Reisner036b17e2011-05-16 17:38:11 +02003143 kfree(int_dig_in);
3144 kfree(int_dig_vv);
Philipp Reisner72046242011-03-15 18:51:47 +01003145 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003146 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003147}
3148
3149/* helper function
3150 * input: alg name, feature name
3151 * return: NULL (alg name was "")
3152 * ERR_PTR(error) if something goes wrong
3153 * or the crypto hash ptr, if it worked out ok. */
3154struct crypto_hash *drbd_crypto_alloc_digest_safe(const struct drbd_conf *mdev,
3155 const char *alg, const char *name)
3156{
3157 struct crypto_hash *tfm;
3158
3159 if (!alg[0])
3160 return NULL;
3161
3162 tfm = crypto_alloc_hash(alg, 0, CRYPTO_ALG_ASYNC);
3163 if (IS_ERR(tfm)) {
3164 dev_err(DEV, "Can not allocate \"%s\" as %s (reason: %ld)\n",
3165 alg, name, PTR_ERR(tfm));
3166 return tfm;
3167 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003168 return tfm;
3169}
3170
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003171static int ignore_remaining_packet(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003172{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003173 void *buffer = tconn->data.rbuf;
3174 int size = pi->size;
3175
3176 while (size) {
3177 int s = min_t(int, size, DRBD_SOCKET_BUFFER_SIZE);
3178 s = drbd_recv(tconn, buffer, s);
3179 if (s <= 0) {
3180 if (s < 0)
3181 return s;
3182 break;
3183 }
3184 size -= s;
3185 }
3186 if (size)
3187 return -EIO;
3188 return 0;
3189}
3190
3191/*
3192 * config_unknown_volume - device configuration command for unknown volume
3193 *
3194 * When a device is added to an existing connection, the node on which the
3195 * device is added first will send configuration commands to its peer but the
3196 * peer will not know about the device yet. It will warn and ignore these
3197 * commands. Once the device is added on the second node, the second node will
3198 * send the same device configuration commands, but in the other direction.
3199 *
3200 * (We can also end up here if drbd is misconfigured.)
3201 */
3202static int config_unknown_volume(struct drbd_tconn *tconn, struct packet_info *pi)
3203{
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02003204 conn_warn(tconn, "%s packet received for volume %u, which is not configured locally\n",
3205 cmdname(pi->cmd), pi->vnr);
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003206 return ignore_remaining_packet(tconn, pi);
3207}
3208
3209static int receive_SyncParam(struct drbd_tconn *tconn, struct packet_info *pi)
3210{
3211 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003212 struct p_rs_param_95 *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003213 unsigned int header_size, data_size, exp_max_sz;
3214 struct crypto_hash *verify_tfm = NULL;
3215 struct crypto_hash *csums_tfm = NULL;
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003216 struct net_conf *old_net_conf, *new_net_conf = NULL;
Philipp Reisner813472c2011-05-03 16:47:02 +02003217 struct disk_conf *old_disk_conf = NULL, *new_disk_conf = NULL;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003218 const int apv = tconn->agreed_pro_version;
Philipp Reisner813472c2011-05-03 16:47:02 +02003219 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
Philipp Reisner778f2712010-07-06 11:14:00 +02003220 int fifo_size = 0;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003221 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003222
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003223 mdev = vnr_to_mdev(tconn, pi->vnr);
3224 if (!mdev)
3225 return config_unknown_volume(tconn, pi);
3226
Philipp Reisnerb411b362009-09-25 16:07:19 -07003227 exp_max_sz = apv <= 87 ? sizeof(struct p_rs_param)
3228 : apv == 88 ? sizeof(struct p_rs_param)
3229 + SHARED_SECRET_MAX
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003230 : apv <= 94 ? sizeof(struct p_rs_param_89)
3231 : /* apv >= 95 */ sizeof(struct p_rs_param_95);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003232
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003233 if (pi->size > exp_max_sz) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003234 dev_err(DEV, "SyncParam packet too long: received %u, expected <= %u bytes\n",
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003235 pi->size, exp_max_sz);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003236 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003237 }
3238
3239 if (apv <= 88) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003240 header_size = sizeof(struct p_rs_param);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003241 data_size = pi->size - header_size;
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003242 } else if (apv <= 94) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003243 header_size = sizeof(struct p_rs_param_89);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003244 data_size = pi->size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003245 D_ASSERT(data_size == 0);
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003246 } else {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003247 header_size = sizeof(struct p_rs_param_95);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003248 data_size = pi->size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003249 D_ASSERT(data_size == 0);
3250 }
3251
3252 /* initialize verify_alg and csums_alg */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003253 p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003254 memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
3255
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003256 err = drbd_recv_all(mdev->tconn, p, header_size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003257 if (err)
3258 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003259
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003260 mutex_lock(&mdev->tconn->conf_update);
3261 old_net_conf = mdev->tconn->net_conf;
Philipp Reisner813472c2011-05-03 16:47:02 +02003262 if (get_ldev(mdev)) {
3263 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
3264 if (!new_disk_conf) {
3265 put_ldev(mdev);
3266 mutex_unlock(&mdev->tconn->conf_update);
3267 dev_err(DEV, "Allocation of new disk_conf failed\n");
3268 return -ENOMEM;
3269 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003270
Philipp Reisner813472c2011-05-03 16:47:02 +02003271 old_disk_conf = mdev->ldev->disk_conf;
3272 *new_disk_conf = *old_disk_conf;
3273
Andreas Gruenbacher6394b932011-05-11 14:29:52 +02003274 new_disk_conf->resync_rate = be32_to_cpu(p->resync_rate);
Philipp Reisner813472c2011-05-03 16:47:02 +02003275 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003276
Philipp Reisnerb411b362009-09-25 16:07:19 -07003277 if (apv >= 88) {
3278 if (apv == 88) {
3279 if (data_size > SHARED_SECRET_MAX) {
3280 dev_err(DEV, "verify-alg too long, "
3281 "peer wants %u, accepting only %u byte\n",
3282 data_size, SHARED_SECRET_MAX);
Philipp Reisner813472c2011-05-03 16:47:02 +02003283 err = -EIO;
3284 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003285 }
3286
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003287 err = drbd_recv_all(mdev->tconn, p->verify_alg, data_size);
Philipp Reisner813472c2011-05-03 16:47:02 +02003288 if (err)
3289 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003290 /* we expect NUL terminated string */
3291 /* but just in case someone tries to be evil */
3292 D_ASSERT(p->verify_alg[data_size-1] == 0);
3293 p->verify_alg[data_size-1] = 0;
3294
3295 } else /* apv >= 89 */ {
3296 /* we still expect NUL terminated strings */
3297 /* but just in case someone tries to be evil */
3298 D_ASSERT(p->verify_alg[SHARED_SECRET_MAX-1] == 0);
3299 D_ASSERT(p->csums_alg[SHARED_SECRET_MAX-1] == 0);
3300 p->verify_alg[SHARED_SECRET_MAX-1] = 0;
3301 p->csums_alg[SHARED_SECRET_MAX-1] = 0;
3302 }
3303
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003304 if (strcmp(old_net_conf->verify_alg, p->verify_alg)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003305 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
3306 dev_err(DEV, "Different verify-alg settings. me=\"%s\" peer=\"%s\"\n",
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003307 old_net_conf->verify_alg, p->verify_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003308 goto disconnect;
3309 }
3310 verify_tfm = drbd_crypto_alloc_digest_safe(mdev,
3311 p->verify_alg, "verify-alg");
3312 if (IS_ERR(verify_tfm)) {
3313 verify_tfm = NULL;
3314 goto disconnect;
3315 }
3316 }
3317
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003318 if (apv >= 89 && strcmp(old_net_conf->csums_alg, p->csums_alg)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003319 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
3320 dev_err(DEV, "Different csums-alg settings. me=\"%s\" peer=\"%s\"\n",
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003321 old_net_conf->csums_alg, p->csums_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003322 goto disconnect;
3323 }
3324 csums_tfm = drbd_crypto_alloc_digest_safe(mdev,
3325 p->csums_alg, "csums-alg");
3326 if (IS_ERR(csums_tfm)) {
3327 csums_tfm = NULL;
3328 goto disconnect;
3329 }
3330 }
3331
Philipp Reisner813472c2011-05-03 16:47:02 +02003332 if (apv > 94 && new_disk_conf) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003333 new_disk_conf->c_plan_ahead = be32_to_cpu(p->c_plan_ahead);
3334 new_disk_conf->c_delay_target = be32_to_cpu(p->c_delay_target);
3335 new_disk_conf->c_fill_target = be32_to_cpu(p->c_fill_target);
3336 new_disk_conf->c_max_rate = be32_to_cpu(p->c_max_rate);
Philipp Reisner778f2712010-07-06 11:14:00 +02003337
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003338 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
Philipp Reisner9958c852011-05-03 16:19:31 +02003339 if (fifo_size != mdev->rs_plan_s->size) {
Philipp Reisner813472c2011-05-03 16:47:02 +02003340 new_plan = fifo_alloc(fifo_size);
3341 if (!new_plan) {
Philipp Reisner778f2712010-07-06 11:14:00 +02003342 dev_err(DEV, "kmalloc of fifo_buffer failed");
Lars Ellenbergf3990022011-03-23 14:31:09 +01003343 put_ldev(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02003344 goto disconnect;
3345 }
3346 }
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003347 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003348
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003349 if (verify_tfm || csums_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003350 new_net_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
3351 if (!new_net_conf) {
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003352 dev_err(DEV, "Allocation of new net_conf failed\n");
3353 goto disconnect;
3354 }
3355
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003356 *new_net_conf = *old_net_conf;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003357
3358 if (verify_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003359 strcpy(new_net_conf->verify_alg, p->verify_alg);
3360 new_net_conf->verify_alg_len = strlen(p->verify_alg) + 1;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003361 crypto_free_hash(mdev->tconn->verify_tfm);
3362 mdev->tconn->verify_tfm = verify_tfm;
3363 dev_info(DEV, "using verify-alg: \"%s\"\n", p->verify_alg);
3364 }
3365 if (csums_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003366 strcpy(new_net_conf->csums_alg, p->csums_alg);
3367 new_net_conf->csums_alg_len = strlen(p->csums_alg) + 1;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003368 crypto_free_hash(mdev->tconn->csums_tfm);
3369 mdev->tconn->csums_tfm = csums_tfm;
3370 dev_info(DEV, "using csums-alg: \"%s\"\n", p->csums_alg);
3371 }
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003372 rcu_assign_pointer(tconn->net_conf, new_net_conf);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003373 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003374 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003375
Philipp Reisner813472c2011-05-03 16:47:02 +02003376 if (new_disk_conf) {
3377 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
3378 put_ldev(mdev);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003379 }
Philipp Reisner813472c2011-05-03 16:47:02 +02003380
3381 if (new_plan) {
3382 old_plan = mdev->rs_plan_s;
3383 rcu_assign_pointer(mdev->rs_plan_s, new_plan);
3384 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003385
3386 mutex_unlock(&mdev->tconn->conf_update);
3387 synchronize_rcu();
3388 if (new_net_conf)
3389 kfree(old_net_conf);
3390 kfree(old_disk_conf);
Philipp Reisner813472c2011-05-03 16:47:02 +02003391 kfree(old_plan);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003392
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003393 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003394
Philipp Reisner813472c2011-05-03 16:47:02 +02003395reconnect:
3396 if (new_disk_conf) {
3397 put_ldev(mdev);
3398 kfree(new_disk_conf);
3399 }
3400 mutex_unlock(&mdev->tconn->conf_update);
3401 return -EIO;
3402
Philipp Reisnerb411b362009-09-25 16:07:19 -07003403disconnect:
Philipp Reisner813472c2011-05-03 16:47:02 +02003404 kfree(new_plan);
3405 if (new_disk_conf) {
3406 put_ldev(mdev);
3407 kfree(new_disk_conf);
3408 }
Philipp Reisnera0095502011-05-03 13:14:15 +02003409 mutex_unlock(&mdev->tconn->conf_update);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003410 /* just for completeness: actually not needed,
3411 * as this is not reached if csums_tfm was ok. */
3412 crypto_free_hash(csums_tfm);
3413 /* but free the verify_tfm again, if csums_tfm did not work out */
3414 crypto_free_hash(verify_tfm);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003415 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003416 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003417}
3418
Philipp Reisnerb411b362009-09-25 16:07:19 -07003419/* warn if the arguments differ by more than 12.5% */
3420static void warn_if_differ_considerably(struct drbd_conf *mdev,
3421 const char *s, sector_t a, sector_t b)
3422{
3423 sector_t d;
3424 if (a == 0 || b == 0)
3425 return;
3426 d = (a > b) ? (a - b) : (b - a);
3427 if (d > (a>>3) || d > (b>>3))
3428 dev_warn(DEV, "Considerable difference in %s: %llus vs. %llus\n", s,
3429 (unsigned long long)a, (unsigned long long)b);
3430}
3431
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003432static int receive_sizes(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003433{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003434 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003435 struct p_sizes *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003436 enum determine_dev_size dd = unchanged;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003437 sector_t p_size, p_usize, my_usize;
3438 int ldsc = 0; /* local disk size changed */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003439 enum dds_flags ddsf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003440
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003441 mdev = vnr_to_mdev(tconn, pi->vnr);
3442 if (!mdev)
3443 return config_unknown_volume(tconn, pi);
3444
Philipp Reisnerb411b362009-09-25 16:07:19 -07003445 p_size = be64_to_cpu(p->d_size);
3446 p_usize = be64_to_cpu(p->u_size);
3447
Philipp Reisnerb411b362009-09-25 16:07:19 -07003448 /* just store the peer's disk size for now.
3449 * we still need to figure out whether we accept that. */
3450 mdev->p_size = p_size;
3451
Philipp Reisnerb411b362009-09-25 16:07:19 -07003452 if (get_ldev(mdev)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003453 rcu_read_lock();
3454 my_usize = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
3455 rcu_read_unlock();
3456
Philipp Reisnerb411b362009-09-25 16:07:19 -07003457 warn_if_differ_considerably(mdev, "lower level device sizes",
3458 p_size, drbd_get_max_capacity(mdev->ldev));
3459 warn_if_differ_considerably(mdev, "user requested size",
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003460 p_usize, my_usize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003461
3462 /* if this is the first connect, or an otherwise expected
3463 * param exchange, choose the minimum */
3464 if (mdev->state.conn == C_WF_REPORT_PARAMS)
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003465 p_usize = min_not_zero(my_usize, p_usize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003466
3467 /* Never shrink a device with usable data during connect.
3468 But allow online shrinking if we are connected. */
Philipp Reisneref5e44a2011-05-03 13:27:43 +02003469 if (drbd_new_dev_size(mdev, mdev->ldev, p_usize, 0) <
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003470 drbd_get_capacity(mdev->this_bdev) &&
3471 mdev->state.disk >= D_OUTDATED &&
3472 mdev->state.conn < C_CONNECTED) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003473 dev_err(DEV, "The peer's disk size is too small!\n");
Philipp Reisner38fa9982011-03-15 18:24:49 +01003474 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003475 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003476 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003477 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003478
3479 if (my_usize != p_usize) {
3480 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
3481
3482 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
3483 if (!new_disk_conf) {
3484 dev_err(DEV, "Allocation of new disk_conf failed\n");
3485 put_ldev(mdev);
3486 return -ENOMEM;
3487 }
3488
3489 mutex_lock(&mdev->tconn->conf_update);
3490 old_disk_conf = mdev->ldev->disk_conf;
3491 *new_disk_conf = *old_disk_conf;
3492 new_disk_conf->disk_size = p_usize;
3493
3494 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
3495 mutex_unlock(&mdev->tconn->conf_update);
3496 synchronize_rcu();
3497 kfree(old_disk_conf);
3498
3499 dev_info(DEV, "Peer sets u_size to %lu sectors\n",
3500 (unsigned long)my_usize);
3501 }
3502
Philipp Reisnerb411b362009-09-25 16:07:19 -07003503 put_ldev(mdev);
3504 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003505
Philipp Reisnere89b5912010-03-24 17:11:33 +01003506 ddsf = be16_to_cpu(p->dds_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003507 if (get_ldev(mdev)) {
Bart Van Assche24c48302011-05-21 18:32:29 +02003508 dd = drbd_determine_dev_size(mdev, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003509 put_ldev(mdev);
3510 if (dd == dev_size_error)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003511 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003512 drbd_md_sync(mdev);
3513 } else {
3514 /* I am diskless, need to accept the peer's size. */
3515 drbd_set_my_capacity(mdev, p_size);
3516 }
3517
Philipp Reisner99432fc2011-05-20 16:39:13 +02003518 mdev->peer_max_bio_size = be32_to_cpu(p->max_bio_size);
3519 drbd_reconsider_max_bio_size(mdev);
3520
Philipp Reisnerb411b362009-09-25 16:07:19 -07003521 if (get_ldev(mdev)) {
3522 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev)) {
3523 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
3524 ldsc = 1;
3525 }
3526
Philipp Reisnerb411b362009-09-25 16:07:19 -07003527 put_ldev(mdev);
3528 }
3529
3530 if (mdev->state.conn > C_WF_REPORT_PARAMS) {
3531 if (be64_to_cpu(p->c_size) !=
3532 drbd_get_capacity(mdev->this_bdev) || ldsc) {
3533 /* we have different sizes, probably peer
3534 * needs to know my new size... */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003535 drbd_send_sizes(mdev, 0, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003536 }
3537 if (test_and_clear_bit(RESIZE_PENDING, &mdev->flags) ||
3538 (dd == grew && mdev->state.conn == C_CONNECTED)) {
3539 if (mdev->state.pdsk >= D_INCONSISTENT &&
Philipp Reisnere89b5912010-03-24 17:11:33 +01003540 mdev->state.disk >= D_INCONSISTENT) {
3541 if (ddsf & DDSF_NO_RESYNC)
3542 dev_info(DEV, "Resync of new storage suppressed with --assume-clean\n");
3543 else
3544 resync_after_online_grow(mdev);
3545 } else
Philipp Reisnerb411b362009-09-25 16:07:19 -07003546 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
3547 }
3548 }
3549
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003550 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003551}
3552
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003553static int receive_uuids(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003554{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003555 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003556 struct p_uuids *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003557 u64 *p_uuid;
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003558 int i, updated_uuids = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003559
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003560 mdev = vnr_to_mdev(tconn, pi->vnr);
3561 if (!mdev)
3562 return config_unknown_volume(tconn, pi);
3563
Philipp Reisnerb411b362009-09-25 16:07:19 -07003564 p_uuid = kmalloc(sizeof(u64)*UI_EXTENDED_SIZE, GFP_NOIO);
3565
3566 for (i = UI_CURRENT; i < UI_EXTENDED_SIZE; i++)
3567 p_uuid[i] = be64_to_cpu(p->uuid[i]);
3568
3569 kfree(mdev->p_uuid);
3570 mdev->p_uuid = p_uuid;
3571
3572 if (mdev->state.conn < C_CONNECTED &&
3573 mdev->state.disk < D_INCONSISTENT &&
3574 mdev->state.role == R_PRIMARY &&
3575 (mdev->ed_uuid & ~((u64)1)) != (p_uuid[UI_CURRENT] & ~((u64)1))) {
3576 dev_err(DEV, "Can only connect to data with current UUID=%016llX\n",
3577 (unsigned long long)mdev->ed_uuid);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003578 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003579 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003580 }
3581
3582 if (get_ldev(mdev)) {
3583 int skip_initial_sync =
3584 mdev->state.conn == C_CONNECTED &&
Philipp Reisner31890f42011-01-19 14:12:51 +01003585 mdev->tconn->agreed_pro_version >= 90 &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003586 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED &&
3587 (p_uuid[UI_FLAGS] & 8);
3588 if (skip_initial_sync) {
3589 dev_info(DEV, "Accepted new current UUID, preparing to skip initial sync\n");
3590 drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003591 "clear_n_write from receive_uuids",
3592 BM_LOCKED_TEST_ALLOWED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003593 _drbd_uuid_set(mdev, UI_CURRENT, p_uuid[UI_CURRENT]);
3594 _drbd_uuid_set(mdev, UI_BITMAP, 0);
3595 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
3596 CS_VERBOSE, NULL);
3597 drbd_md_sync(mdev);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003598 updated_uuids = 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003599 }
3600 put_ldev(mdev);
Philipp Reisner18a50fa2010-06-21 14:14:15 +02003601 } else if (mdev->state.disk < D_INCONSISTENT &&
3602 mdev->state.role == R_PRIMARY) {
3603 /* I am a diskless primary, the peer just created a new current UUID
3604 for me. */
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003605 updated_uuids = drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003606 }
3607
3608 /* Before we test for the disk state, we should wait until an eventually
3609 ongoing cluster wide state change is finished. That is important if
3610 we are primary and are detaching from our disk. We need to see the
3611 new disk state... */
Philipp Reisner8410da82011-02-11 20:11:10 +01003612 mutex_lock(mdev->state_mutex);
3613 mutex_unlock(mdev->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003614 if (mdev->state.conn >= C_CONNECTED && mdev->state.disk < D_INCONSISTENT)
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003615 updated_uuids |= drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
3616
3617 if (updated_uuids)
3618 drbd_print_uuids(mdev, "receiver updated UUIDs to");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003619
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003620 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003621}
3622
3623/**
3624 * convert_state() - Converts the peer's view of the cluster state to our point of view
3625 * @ps: The state as seen by the peer.
3626 */
3627static union drbd_state convert_state(union drbd_state ps)
3628{
3629 union drbd_state ms;
3630
3631 static enum drbd_conns c_tab[] = {
Philipp Reisner369bea62011-07-06 23:04:44 +02003632 [C_WF_REPORT_PARAMS] = C_WF_REPORT_PARAMS,
Philipp Reisnerb411b362009-09-25 16:07:19 -07003633 [C_CONNECTED] = C_CONNECTED,
3634
3635 [C_STARTING_SYNC_S] = C_STARTING_SYNC_T,
3636 [C_STARTING_SYNC_T] = C_STARTING_SYNC_S,
3637 [C_DISCONNECTING] = C_TEAR_DOWN, /* C_NETWORK_FAILURE, */
3638 [C_VERIFY_S] = C_VERIFY_T,
3639 [C_MASK] = C_MASK,
3640 };
3641
3642 ms.i = ps.i;
3643
3644 ms.conn = c_tab[ps.conn];
3645 ms.peer = ps.role;
3646 ms.role = ps.peer;
3647 ms.pdsk = ps.disk;
3648 ms.disk = ps.pdsk;
3649 ms.peer_isp = (ps.aftr_isp | ps.user_isp);
3650
3651 return ms;
3652}
3653
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003654static int receive_req_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003655{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003656 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003657 struct p_req_state *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003658 union drbd_state mask, val;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01003659 enum drbd_state_rv rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003660
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003661 mdev = vnr_to_mdev(tconn, pi->vnr);
3662 if (!mdev)
3663 return -EIO;
3664
Philipp Reisnerb411b362009-09-25 16:07:19 -07003665 mask.i = be32_to_cpu(p->mask);
3666 val.i = be32_to_cpu(p->val);
3667
Philipp Reisner25703f82011-02-07 14:35:25 +01003668 if (test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags) &&
Philipp Reisner8410da82011-02-11 20:11:10 +01003669 mutex_is_locked(mdev->state_mutex)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003670 drbd_send_sr_reply(mdev, SS_CONCURRENT_ST_CHG);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003671 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003672 }
3673
3674 mask = convert_state(mask);
3675 val = convert_state(val);
3676
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003677 rv = drbd_change_state(mdev, CS_VERBOSE, mask, val);
3678 drbd_send_sr_reply(mdev, rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003679
Philipp Reisnerb411b362009-09-25 16:07:19 -07003680 drbd_md_sync(mdev);
3681
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003682 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003683}
3684
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003685static int receive_req_conn_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003686{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003687 struct p_req_state *p = pi->data;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003688 union drbd_state mask, val;
3689 enum drbd_state_rv rv;
3690
3691 mask.i = be32_to_cpu(p->mask);
3692 val.i = be32_to_cpu(p->val);
3693
3694 if (test_bit(DISCARD_CONCURRENT, &tconn->flags) &&
3695 mutex_is_locked(&tconn->cstate_mutex)) {
3696 conn_send_sr_reply(tconn, SS_CONCURRENT_ST_CHG);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003697 return 0;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003698 }
3699
3700 mask = convert_state(mask);
3701 val = convert_state(val);
3702
Philipp Reisner778bcf22011-03-28 12:55:03 +02003703 rv = conn_request_state(tconn, mask, val, CS_VERBOSE | CS_LOCAL_ONLY | CS_IGN_OUTD_FAIL);
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003704 conn_send_sr_reply(tconn, rv);
3705
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003706 return 0;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003707}
3708
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003709static int receive_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003710{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003711 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003712 struct p_state *p = pi->data;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003713 union drbd_state os, ns, peer_state;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003714 enum drbd_disk_state real_peer_disk;
Philipp Reisner65d922c2010-06-16 16:18:09 +02003715 enum chg_state_flags cs_flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003716 int rv;
3717
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003718 mdev = vnr_to_mdev(tconn, pi->vnr);
3719 if (!mdev)
3720 return config_unknown_volume(tconn, pi);
3721
Philipp Reisnerb411b362009-09-25 16:07:19 -07003722 peer_state.i = be32_to_cpu(p->state);
3723
3724 real_peer_disk = peer_state.disk;
3725 if (peer_state.disk == D_NEGOTIATING) {
3726 real_peer_disk = mdev->p_uuid[UI_FLAGS] & 4 ? D_INCONSISTENT : D_CONSISTENT;
3727 dev_info(DEV, "real peer disk state = %s\n", drbd_disk_str(real_peer_disk));
3728 }
3729
Philipp Reisner87eeee42011-01-19 14:16:30 +01003730 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003731 retry:
Philipp Reisner78bae592011-03-28 15:40:12 +02003732 os = ns = drbd_read_state(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01003733 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003734
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003735 /* peer says his disk is uptodate, while we think it is inconsistent,
3736 * and this happens while we think we have a sync going on. */
3737 if (os.pdsk == D_INCONSISTENT && real_peer_disk == D_UP_TO_DATE &&
3738 os.conn > C_CONNECTED && os.disk == D_UP_TO_DATE) {
3739 /* If we are (becoming) SyncSource, but peer is still in sync
3740 * preparation, ignore its uptodate-ness to avoid flapping, it
3741 * will change to inconsistent once the peer reaches active
3742 * syncing states.
3743 * It may have changed syncer-paused flags, however, so we
3744 * cannot ignore this completely. */
3745 if (peer_state.conn > C_CONNECTED &&
3746 peer_state.conn < C_SYNC_SOURCE)
3747 real_peer_disk = D_INCONSISTENT;
3748
3749 /* if peer_state changes to connected at the same time,
3750 * it explicitly notifies us that it finished resync.
3751 * Maybe we should finish it up, too? */
3752 else if (os.conn >= C_SYNC_SOURCE &&
3753 peer_state.conn == C_CONNECTED) {
3754 if (drbd_bm_total_weight(mdev) <= mdev->rs_failed)
3755 drbd_resync_finished(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003756 return 0;
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003757 }
3758 }
3759
3760 /* peer says his disk is inconsistent, while we think it is uptodate,
3761 * and this happens while the peer still thinks we have a sync going on,
3762 * but we think we are already done with the sync.
3763 * We ignore this to avoid flapping pdsk.
3764 * This should not happen, if the peer is a recent version of drbd. */
3765 if (os.pdsk == D_UP_TO_DATE && real_peer_disk == D_INCONSISTENT &&
3766 os.conn == C_CONNECTED && peer_state.conn > C_SYNC_SOURCE)
3767 real_peer_disk = D_UP_TO_DATE;
3768
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003769 if (ns.conn == C_WF_REPORT_PARAMS)
3770 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003771
Philipp Reisner67531712010-10-27 12:21:30 +02003772 if (peer_state.conn == C_AHEAD)
3773 ns.conn = C_BEHIND;
3774
Philipp Reisnerb411b362009-09-25 16:07:19 -07003775 if (mdev->p_uuid && peer_state.disk >= D_NEGOTIATING &&
3776 get_ldev_if_state(mdev, D_NEGOTIATING)) {
3777 int cr; /* consider resync */
3778
3779 /* if we established a new connection */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003780 cr = (os.conn < C_CONNECTED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003781 /* if we had an established connection
3782 * and one of the nodes newly attaches a disk */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003783 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003784 (peer_state.disk == D_NEGOTIATING ||
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003785 os.disk == D_NEGOTIATING));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003786 /* if we have both been inconsistent, and the peer has been
3787 * forced to be UpToDate with --overwrite-data */
3788 cr |= test_bit(CONSIDER_RESYNC, &mdev->flags);
3789 /* if we had been plain connected, and the admin requested to
3790 * start a sync by "invalidate" or "invalidate-remote" */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003791 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003792 (peer_state.conn >= C_STARTING_SYNC_S &&
3793 peer_state.conn <= C_WF_BITMAP_T));
3794
3795 if (cr)
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003796 ns.conn = drbd_sync_handshake(mdev, peer_state.role, real_peer_disk);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003797
3798 put_ldev(mdev);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003799 if (ns.conn == C_MASK) {
3800 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003801 if (mdev->state.disk == D_NEGOTIATING) {
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02003802 drbd_force_state(mdev, NS(disk, D_FAILED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003803 } else if (peer_state.disk == D_NEGOTIATING) {
3804 dev_err(DEV, "Disk attach process on the peer node was aborted.\n");
3805 peer_state.disk = D_DISKLESS;
Lars Ellenberg580b9762010-02-26 23:15:23 +01003806 real_peer_disk = D_DISKLESS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003807 } else {
Philipp Reisner8169e412011-03-15 18:40:27 +01003808 if (test_and_clear_bit(CONN_DRY_RUN, &mdev->tconn->flags))
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003809 return -EIO;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003810 D_ASSERT(os.conn == C_WF_REPORT_PARAMS);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003811 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003812 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003813 }
3814 }
3815 }
3816
Philipp Reisner87eeee42011-01-19 14:16:30 +01003817 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner78bae592011-03-28 15:40:12 +02003818 if (os.i != drbd_read_state(mdev).i)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003819 goto retry;
3820 clear_bit(CONSIDER_RESYNC, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003821 ns.peer = peer_state.role;
3822 ns.pdsk = real_peer_disk;
3823 ns.peer_isp = (peer_state.aftr_isp | peer_state.user_isp);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003824 if ((ns.conn == C_CONNECTED || ns.conn == C_WF_BITMAP_S) && ns.disk == D_NEGOTIATING)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003825 ns.disk = mdev->new_state_tmp.disk;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003826 cs_flags = CS_VERBOSE + (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED ? 0 : CS_HARD);
Philipp Reisner2aebfab2011-03-28 16:48:11 +02003827 if (ns.pdsk == D_CONSISTENT && drbd_suspended(mdev) && ns.conn == C_CONNECTED && os.conn < C_CONNECTED &&
Philipp Reisner481c6f52010-06-22 14:03:27 +02003828 test_bit(NEW_CUR_UUID, &mdev->flags)) {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01003829 /* Do not allow tl_restart(RESEND) for a rebooted peer. We can only allow this
Philipp Reisner481c6f52010-06-22 14:03:27 +02003830 for temporal network outages! */
Philipp Reisner87eeee42011-01-19 14:16:30 +01003831 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003832 dev_err(DEV, "Aborting Connect, can not thaw IO with an only Consistent peer\n");
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01003833 tl_clear(mdev->tconn);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003834 drbd_uuid_new_current(mdev);
3835 clear_bit(NEW_CUR_UUID, &mdev->flags);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003836 conn_request_state(mdev->tconn, NS2(conn, C_PROTOCOL_ERROR, susp, 0), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003837 return -EIO;
Philipp Reisner481c6f52010-06-22 14:03:27 +02003838 }
Philipp Reisner65d922c2010-06-16 16:18:09 +02003839 rv = _drbd_set_state(mdev, ns, cs_flags, NULL);
Philipp Reisner78bae592011-03-28 15:40:12 +02003840 ns = drbd_read_state(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01003841 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003842
3843 if (rv < SS_SUCCESS) {
Philipp Reisner38fa9982011-03-15 18:24:49 +01003844 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003845 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003846 }
3847
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003848 if (os.conn > C_WF_REPORT_PARAMS) {
3849 if (ns.conn > C_CONNECTED && peer_state.conn <= C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003850 peer_state.disk != D_NEGOTIATING ) {
3851 /* we want resync, peer has not yet decided to sync... */
3852 /* Nowadays only used when forcing a node into primary role and
3853 setting its disk to UpToDate with that */
3854 drbd_send_uuids(mdev);
3855 drbd_send_state(mdev);
3856 }
3857 }
3858
Philipp Reisnera0095502011-05-03 13:14:15 +02003859 mutex_lock(&mdev->tconn->conf_update);
Andreas Gruenbacher6139f602011-05-06 20:00:02 +02003860 mdev->tconn->net_conf->discard_my_data = 0; /* without copy; single bit op is atomic */
Philipp Reisnera0095502011-05-03 13:14:15 +02003861 mutex_unlock(&mdev->tconn->conf_update);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003862
3863 drbd_md_sync(mdev); /* update connected indicator, la_size, ... */
3864
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003865 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003866}
3867
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003868static int receive_sync_uuid(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003869{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003870 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003871 struct p_rs_uuid *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003872
3873 mdev = vnr_to_mdev(tconn, pi->vnr);
3874 if (!mdev)
3875 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003876
3877 wait_event(mdev->misc_wait,
3878 mdev->state.conn == C_WF_SYNC_UUID ||
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02003879 mdev->state.conn == C_BEHIND ||
Philipp Reisnerb411b362009-09-25 16:07:19 -07003880 mdev->state.conn < C_CONNECTED ||
3881 mdev->state.disk < D_NEGOTIATING);
3882
3883 /* D_ASSERT( mdev->state.conn == C_WF_SYNC_UUID ); */
3884
Philipp Reisnerb411b362009-09-25 16:07:19 -07003885 /* Here the _drbd_uuid_ functions are right, current should
3886 _not_ be rotated into the history */
3887 if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
3888 _drbd_uuid_set(mdev, UI_CURRENT, be64_to_cpu(p->uuid));
3889 _drbd_uuid_set(mdev, UI_BITMAP, 0UL);
3890
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003891 drbd_print_uuids(mdev, "updated sync uuid");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003892 drbd_start_resync(mdev, C_SYNC_TARGET);
3893
3894 put_ldev(mdev);
3895 } else
3896 dev_err(DEV, "Ignoring SyncUUID packet!\n");
3897
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003898 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003899}
3900
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003901/**
3902 * receive_bitmap_plain
3903 *
3904 * Return 0 when done, 1 when another iteration is needed, and a negative error
3905 * code upon failure.
3906 */
3907static int
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003908receive_bitmap_plain(struct drbd_conf *mdev, unsigned int size,
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003909 unsigned long *p, struct bm_xfer_ctx *c)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003910{
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003911 unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE -
3912 drbd_header_size(mdev->tconn);
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003913 unsigned int num_words = min_t(size_t, data_size / sizeof(*p),
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003914 c->bm_words - c->word_offset);
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003915 unsigned int want = num_words * sizeof(*p);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003916 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003917
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003918 if (want != size) {
3919 dev_err(DEV, "%s:want (%u) != size (%u)\n", __func__, want, size);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003920 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003921 }
3922 if (want == 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003923 return 0;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003924 err = drbd_recv_all(mdev->tconn, p, want);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003925 if (err)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003926 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003927
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003928 drbd_bm_merge_lel(mdev, c->word_offset, num_words, p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003929
3930 c->word_offset += num_words;
3931 c->bit_offset = c->word_offset * BITS_PER_LONG;
3932 if (c->bit_offset > c->bm_bits)
3933 c->bit_offset = c->bm_bits;
3934
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003935 return 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003936}
3937
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01003938static enum drbd_bitmap_code dcbp_get_code(struct p_compressed_bm *p)
3939{
3940 return (enum drbd_bitmap_code)(p->encoding & 0x0f);
3941}
3942
3943static int dcbp_get_start(struct p_compressed_bm *p)
3944{
3945 return (p->encoding & 0x80) != 0;
3946}
3947
3948static int dcbp_get_pad_bits(struct p_compressed_bm *p)
3949{
3950 return (p->encoding >> 4) & 0x7;
3951}
3952
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003953/**
3954 * recv_bm_rle_bits
3955 *
3956 * Return 0 when done, 1 when another iteration is needed, and a negative error
3957 * code upon failure.
3958 */
3959static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07003960recv_bm_rle_bits(struct drbd_conf *mdev,
3961 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01003962 struct bm_xfer_ctx *c,
3963 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003964{
3965 struct bitstream bs;
3966 u64 look_ahead;
3967 u64 rl;
3968 u64 tmp;
3969 unsigned long s = c->bit_offset;
3970 unsigned long e;
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01003971 int toggle = dcbp_get_start(p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003972 int have;
3973 int bits;
3974
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01003975 bitstream_init(&bs, p->code, len, dcbp_get_pad_bits(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003976
3977 bits = bitstream_get_bits(&bs, &look_ahead, 64);
3978 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003979 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003980
3981 for (have = bits; have > 0; s += rl, toggle = !toggle) {
3982 bits = vli_decode_bits(&rl, look_ahead);
3983 if (bits <= 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003984 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003985
3986 if (toggle) {
3987 e = s + rl -1;
3988 if (e >= c->bm_bits) {
3989 dev_err(DEV, "bitmap overflow (e:%lu) while decoding bm RLE packet\n", e);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003990 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003991 }
3992 _drbd_bm_set_bits(mdev, s, e);
3993 }
3994
3995 if (have < bits) {
3996 dev_err(DEV, "bitmap decoding error: h:%d b:%d la:0x%08llx l:%u/%u\n",
3997 have, bits, look_ahead,
3998 (unsigned int)(bs.cur.b - p->code),
3999 (unsigned int)bs.buf_len);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004000 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004001 }
4002 look_ahead >>= bits;
4003 have -= bits;
4004
4005 bits = bitstream_get_bits(&bs, &tmp, 64 - have);
4006 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004007 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004008 look_ahead |= tmp << have;
4009 have += bits;
4010 }
4011
4012 c->bit_offset = s;
4013 bm_xfer_ctx_bit_to_word_offset(c);
4014
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004015 return (s != c->bm_bits);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004016}
4017
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004018/**
4019 * decode_bitmap_c
4020 *
4021 * Return 0 when done, 1 when another iteration is needed, and a negative error
4022 * code upon failure.
4023 */
4024static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07004025decode_bitmap_c(struct drbd_conf *mdev,
4026 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01004027 struct bm_xfer_ctx *c,
4028 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004029{
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004030 if (dcbp_get_code(p) == RLE_VLI_Bits)
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004031 return recv_bm_rle_bits(mdev, p, c, len - sizeof(*p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07004032
4033 /* other variants had been implemented for evaluation,
4034 * but have been dropped as this one turned out to be "best"
4035 * during all our tests. */
4036
4037 dev_err(DEV, "receive_bitmap_c: unknown encoding %u\n", p->encoding);
Philipp Reisner38fa9982011-03-15 18:24:49 +01004038 conn_request_state(mdev->tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004039 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004040}
4041
4042void INFO_bm_xfer_stats(struct drbd_conf *mdev,
4043 const char *direction, struct bm_xfer_ctx *c)
4044{
4045 /* what would it take to transfer it "plaintext" */
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004046 unsigned int header_size = drbd_header_size(mdev->tconn);
4047 unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE - header_size;
4048 unsigned int plain =
4049 header_size * (DIV_ROUND_UP(c->bm_words, data_size) + 1) +
4050 c->bm_words * sizeof(unsigned long);
4051 unsigned int total = c->bytes[0] + c->bytes[1];
4052 unsigned int r;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004053
4054 /* total can not be zero. but just in case: */
4055 if (total == 0)
4056 return;
4057
4058 /* don't report if not compressed */
4059 if (total >= plain)
4060 return;
4061
4062 /* total < plain. check for overflow, still */
4063 r = (total > UINT_MAX/1000) ? (total / (plain/1000))
4064 : (1000 * total / plain);
4065
4066 if (r > 1000)
4067 r = 1000;
4068
4069 r = 1000 - r;
4070 dev_info(DEV, "%s bitmap stats [Bytes(packets)]: plain %u(%u), RLE %u(%u), "
4071 "total %u; compression: %u.%u%%\n",
4072 direction,
4073 c->bytes[1], c->packets[1],
4074 c->bytes[0], c->packets[0],
4075 total, r/10, r % 10);
4076}
4077
4078/* Since we are processing the bitfield from lower addresses to higher,
4079 it does not matter if the process it in 32 bit chunks or 64 bit
4080 chunks as long as it is little endian. (Understand it as byte stream,
4081 beginning with the lowest byte...) If we would use big endian
4082 we would need to process it from the highest address to the lowest,
4083 in order to be agnostic to the 32 vs 64 bits issue.
4084
4085 returns 0 on failure, 1 if we successfully received it. */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004086static int receive_bitmap(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004087{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004088 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004089 struct bm_xfer_ctx c;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004090 int err;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004091
4092 mdev = vnr_to_mdev(tconn, pi->vnr);
4093 if (!mdev)
4094 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004095
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004096 drbd_bm_lock(mdev, "receive bitmap", BM_LOCKED_SET_ALLOWED);
4097 /* you are supposed to send additional out-of-sync information
4098 * if you actually set bits during this phase */
Philipp Reisnerb411b362009-09-25 16:07:19 -07004099
Philipp Reisnerb411b362009-09-25 16:07:19 -07004100 c = (struct bm_xfer_ctx) {
4101 .bm_bits = drbd_bm_bits(mdev),
4102 .bm_words = drbd_bm_words(mdev),
4103 };
4104
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004105 for(;;) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004106 if (pi->cmd == P_BITMAP)
4107 err = receive_bitmap_plain(mdev, pi->size, pi->data, &c);
4108 else if (pi->cmd == P_COMPRESSED_BITMAP) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004109 /* MAYBE: sanity check that we speak proto >= 90,
4110 * and the feature is enabled! */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004111 struct p_compressed_bm *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004112
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004113 if (pi->size > DRBD_SOCKET_BUFFER_SIZE - drbd_header_size(tconn)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004114 dev_err(DEV, "ReportCBitmap packet too large\n");
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004115 err = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004116 goto out;
4117 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004118 if (pi->size <= sizeof(*p)) {
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004119 dev_err(DEV, "ReportCBitmap packet too small (l:%u)\n", pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004120 err = -EIO;
Andreas Gruenbacher78fcbda2010-12-10 22:18:27 +01004121 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004122 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004123 err = drbd_recv_all(mdev->tconn, p, pi->size);
4124 if (err)
4125 goto out;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004126 err = decode_bitmap_c(mdev, p, &c, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004127 } else {
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004128 dev_warn(DEV, "receive_bitmap: cmd neither ReportBitMap nor ReportCBitMap (is 0x%x)", pi->cmd);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004129 err = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004130 goto out;
4131 }
4132
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004133 c.packets[pi->cmd == P_BITMAP]++;
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004134 c.bytes[pi->cmd == P_BITMAP] += drbd_header_size(tconn) + pi->size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004135
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004136 if (err <= 0) {
4137 if (err < 0)
4138 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004139 break;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004140 }
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004141 err = drbd_recv_header(mdev->tconn, pi);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004142 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004143 goto out;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004144 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004145
4146 INFO_bm_xfer_stats(mdev, "receive", &c);
4147
4148 if (mdev->state.conn == C_WF_BITMAP_T) {
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01004149 enum drbd_state_rv rv;
4150
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004151 err = drbd_send_bitmap(mdev);
4152 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004153 goto out;
4154 /* Omit CS_ORDERED with this state transition to avoid deadlocks. */
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01004155 rv = _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
4156 D_ASSERT(rv == SS_SUCCESS);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004157 } else if (mdev->state.conn != C_WF_BITMAP_S) {
4158 /* admin may have requested C_DISCONNECTING,
4159 * other threads may have noticed network errors */
4160 dev_info(DEV, "unexpected cstate (%s) in receive_bitmap\n",
4161 drbd_conn_str(mdev->state.conn));
4162 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004163 err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004164
Philipp Reisnerb411b362009-09-25 16:07:19 -07004165 out:
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004166 drbd_bm_unlock(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004167 if (!err && mdev->state.conn == C_WF_BITMAP_S)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004168 drbd_start_resync(mdev, C_SYNC_SOURCE);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004169 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004170}
4171
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004172static int receive_skip(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004173{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004174 conn_warn(tconn, "skipping unknown optional packet type %d, l: %d!\n",
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004175 pi->cmd, pi->size);
Philipp Reisner2de876e2011-03-15 14:38:01 +01004176
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004177 return ignore_remaining_packet(tconn, pi);
Philipp Reisner2de876e2011-03-15 14:38:01 +01004178}
4179
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004180static int receive_UnplugRemote(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004181{
Philipp Reisnerb411b362009-09-25 16:07:19 -07004182 /* Make sure we've acked all the TCP data associated
4183 * with the data requests being unplugged */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004184 drbd_tcp_quickack(tconn->data.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004185
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004186 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004187}
4188
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004189static int receive_out_of_sync(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner73a01a12010-10-27 14:33:00 +02004190{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004191 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004192 struct p_block_desc *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004193
4194 mdev = vnr_to_mdev(tconn, pi->vnr);
4195 if (!mdev)
4196 return -EIO;
Philipp Reisner73a01a12010-10-27 14:33:00 +02004197
Lars Ellenbergf735e3632010-12-17 21:06:18 +01004198 switch (mdev->state.conn) {
4199 case C_WF_SYNC_UUID:
4200 case C_WF_BITMAP_T:
4201 case C_BEHIND:
4202 break;
4203 default:
4204 dev_err(DEV, "ASSERT FAILED cstate = %s, expected: WFSyncUUID|WFBitMapT|Behind\n",
4205 drbd_conn_str(mdev->state.conn));
4206 }
4207
Philipp Reisner73a01a12010-10-27 14:33:00 +02004208 drbd_set_out_of_sync(mdev, be64_to_cpu(p->sector), be32_to_cpu(p->blksize));
4209
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004210 return 0;
Philipp Reisner73a01a12010-10-27 14:33:00 +02004211}
4212
Philipp Reisner02918be2010-08-20 14:35:10 +02004213struct data_cmd {
4214 int expect_payload;
4215 size_t pkt_size;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004216 int (*fn)(struct drbd_tconn *, struct packet_info *);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004217};
4218
Philipp Reisner02918be2010-08-20 14:35:10 +02004219static struct data_cmd drbd_cmd_handler[] = {
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004220 [P_DATA] = { 1, sizeof(struct p_data), receive_Data },
4221 [P_DATA_REPLY] = { 1, sizeof(struct p_data), receive_DataReply },
4222 [P_RS_DATA_REPLY] = { 1, sizeof(struct p_data), receive_RSDataReply } ,
4223 [P_BARRIER] = { 0, sizeof(struct p_barrier), receive_Barrier } ,
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004224 [P_BITMAP] = { 1, 0, receive_bitmap } ,
4225 [P_COMPRESSED_BITMAP] = { 1, 0, receive_bitmap } ,
4226 [P_UNPLUG_REMOTE] = { 0, 0, receive_UnplugRemote },
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004227 [P_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
4228 [P_RS_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004229 [P_SYNC_PARAM] = { 1, 0, receive_SyncParam },
4230 [P_SYNC_PARAM89] = { 1, 0, receive_SyncParam },
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004231 [P_PROTOCOL] = { 1, sizeof(struct p_protocol), receive_protocol },
4232 [P_UUIDS] = { 0, sizeof(struct p_uuids), receive_uuids },
4233 [P_SIZES] = { 0, sizeof(struct p_sizes), receive_sizes },
4234 [P_STATE] = { 0, sizeof(struct p_state), receive_state },
4235 [P_STATE_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_state },
4236 [P_SYNC_UUID] = { 0, sizeof(struct p_rs_uuid), receive_sync_uuid },
4237 [P_OV_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
4238 [P_OV_REPLY] = { 1, sizeof(struct p_block_req), receive_DataRequest },
4239 [P_CSUM_RS_REQUEST] = { 1, sizeof(struct p_block_req), receive_DataRequest },
4240 [P_DELAY_PROBE] = { 0, sizeof(struct p_delay_probe93), receive_skip },
4241 [P_OUT_OF_SYNC] = { 0, sizeof(struct p_block_desc), receive_out_of_sync },
4242 [P_CONN_ST_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_conn_state },
Philipp Reisner036b17e2011-05-16 17:38:11 +02004243 [P_PROTOCOL_UPDATE] = { 1, sizeof(struct p_protocol), receive_protocol },
Philipp Reisner02918be2010-08-20 14:35:10 +02004244};
4245
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004246static void drbdd(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004247{
Philipp Reisner77351055b2011-02-07 17:24:26 +01004248 struct packet_info pi;
Philipp Reisner02918be2010-08-20 14:35:10 +02004249 size_t shs; /* sub header size */
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004250 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004251
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004252 while (get_t_state(&tconn->receiver) == RUNNING) {
Andreas Gruenbacherdeebe192011-03-25 00:01:04 +01004253 struct data_cmd *cmd;
4254
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004255 drbd_thread_current_set_cpu(&tconn->receiver);
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004256 if (drbd_recv_header(tconn, &pi))
Philipp Reisner02918be2010-08-20 14:35:10 +02004257 goto err_out;
4258
Andreas Gruenbacherdeebe192011-03-25 00:01:04 +01004259 cmd = &drbd_cmd_handler[pi.cmd];
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004260 if (unlikely(pi.cmd >= ARRAY_SIZE(drbd_cmd_handler) || !cmd->fn)) {
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004261 conn_err(tconn, "Unexpected data packet %s (0x%04x)",
4262 cmdname(pi.cmd), pi.cmd);
Philipp Reisner02918be2010-08-20 14:35:10 +02004263 goto err_out;
Lars Ellenberg0b33a912009-11-16 15:58:04 +01004264 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004265
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004266 shs = cmd->pkt_size;
4267 if (pi.size > shs && !cmd->expect_payload) {
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004268 conn_err(tconn, "No payload expected %s l:%d\n",
4269 cmdname(pi.cmd), pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02004270 goto err_out;
4271 }
4272
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004273 if (shs) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004274 err = drbd_recv_all_warn(tconn, pi.data, shs);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004275 if (err)
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004276 goto err_out;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004277 pi.size -= shs;
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004278 }
4279
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004280 err = cmd->fn(tconn, &pi);
4281 if (err) {
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004282 conn_err(tconn, "error receiving %s, e: %d l: %d!\n",
4283 cmdname(pi.cmd), err, pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02004284 goto err_out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004285 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004286 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004287 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004288
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004289 err_out:
4290 conn_request_state(tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004291}
4292
Philipp Reisner0e29d162011-02-18 14:23:11 +01004293void conn_flush_workqueue(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004294{
4295 struct drbd_wq_barrier barr;
4296
4297 barr.w.cb = w_prev_work_done;
Philipp Reisner0e29d162011-02-18 14:23:11 +01004298 barr.w.tconn = tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004299 init_completion(&barr.done);
Philipp Reisner0e29d162011-02-18 14:23:11 +01004300 drbd_queue_work(&tconn->data.work, &barr.w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004301 wait_for_completion(&barr.done);
4302}
4303
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004304static void conn_disconnect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004305{
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004306 struct drbd_conf *mdev;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004307 enum drbd_conns oc;
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004308 int vnr, rv = SS_UNKNOWN_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004309
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004310 if (tconn->cstate == C_STANDALONE)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004311 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004312
4313 /* asender does not clean up anything. it must not interfere, either */
Philipp Reisner360cc742011-02-08 14:29:53 +01004314 drbd_thread_stop(&tconn->asender);
4315 drbd_free_sock(tconn);
4316
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004317 rcu_read_lock();
4318 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
4319 kref_get(&mdev->kref);
4320 rcu_read_unlock();
4321 drbd_disconnected(mdev);
4322 kref_put(&mdev->kref, &drbd_minor_destroy);
4323 rcu_read_lock();
4324 }
4325 rcu_read_unlock();
4326
Philipp Reisner360cc742011-02-08 14:29:53 +01004327 conn_info(tconn, "Connection closed\n");
4328
Philipp Reisnercb703452011-03-24 11:03:07 +01004329 if (conn_highest_role(tconn) == R_PRIMARY && conn_highest_pdsk(tconn) >= D_UNKNOWN)
4330 conn_try_outdate_peer_async(tconn);
4331
Philipp Reisner360cc742011-02-08 14:29:53 +01004332 spin_lock_irq(&tconn->req_lock);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004333 oc = tconn->cstate;
4334 if (oc >= C_UNCONNECTED)
4335 rv = _conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE);
4336
Philipp Reisner360cc742011-02-08 14:29:53 +01004337 spin_unlock_irq(&tconn->req_lock);
4338
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02004339 if (oc == C_DISCONNECTING)
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +02004340 conn_request_state(tconn, NS(conn, C_STANDALONE), CS_VERBOSE | CS_HARD);
Philipp Reisner360cc742011-02-08 14:29:53 +01004341}
4342
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004343static int drbd_disconnected(struct drbd_conf *mdev)
Philipp Reisner360cc742011-02-08 14:29:53 +01004344{
Philipp Reisner360cc742011-02-08 14:29:53 +01004345 enum drbd_fencing_p fp;
4346 unsigned int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004347
Philipp Reisner85719572010-07-21 10:20:17 +02004348 /* wait for current activity to cease. */
Philipp Reisner87eeee42011-01-19 14:16:30 +01004349 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004350 _drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
4351 _drbd_wait_ee_list_empty(mdev, &mdev->sync_ee);
4352 _drbd_wait_ee_list_empty(mdev, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004353 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004354
4355 /* We do not have data structures that would allow us to
4356 * get the rs_pending_cnt down to 0 again.
4357 * * On C_SYNC_TARGET we do not have any data structures describing
4358 * the pending RSDataRequest's we have sent.
4359 * * On C_SYNC_SOURCE there is no data structure that tracks
4360 * the P_RS_DATA_REPLY blocks that we sent to the SyncTarget.
4361 * And no, it is not the sum of the reference counts in the
4362 * resync_LRU. The resync_LRU tracks the whole operation including
4363 * the disk-IO, while the rs_pending_cnt only tracks the blocks
4364 * on the fly. */
4365 drbd_rs_cancel_all(mdev);
4366 mdev->rs_total = 0;
4367 mdev->rs_failed = 0;
4368 atomic_set(&mdev->rs_pending_cnt, 0);
4369 wake_up(&mdev->misc_wait);
4370
Philipp Reisnerb411b362009-09-25 16:07:19 -07004371 del_timer_sync(&mdev->resync_timer);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004372 resync_timer_fn((unsigned long)mdev);
4373
Philipp Reisnerb411b362009-09-25 16:07:19 -07004374 /* wait for all w_e_end_data_req, w_e_end_rsdata_req, w_send_barrier,
4375 * w_make_resync_request etc. which may still be on the worker queue
4376 * to be "canceled" */
Philipp Reisnera21e9292011-02-08 15:08:49 +01004377 drbd_flush_workqueue(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004378
Andreas Gruenbachera990be42011-04-06 17:56:48 +02004379 drbd_finish_peer_reqs(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004380
4381 kfree(mdev->p_uuid);
4382 mdev->p_uuid = NULL;
4383
Philipp Reisner2aebfab2011-03-28 16:48:11 +02004384 if (!drbd_suspended(mdev))
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01004385 tl_clear(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004386
Philipp Reisnerb411b362009-09-25 16:07:19 -07004387 drbd_md_sync(mdev);
4388
4389 fp = FP_DONT_CARE;
4390 if (get_ldev(mdev)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02004391 rcu_read_lock();
4392 fp = rcu_dereference(mdev->ldev->disk_conf)->fencing;
4393 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07004394 put_ldev(mdev);
4395 }
4396
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004397 /* serialize with bitmap writeout triggered by the state change,
4398 * if any. */
4399 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
4400
Philipp Reisnerb411b362009-09-25 16:07:19 -07004401 /* tcp_close and release of sendpage pages can be deferred. I don't
4402 * want to use SO_LINGER, because apparently it can be deferred for
4403 * more than 20 seconds (longest time I checked).
4404 *
4405 * Actually we don't care for exactly when the network stack does its
4406 * put_page(), but release our reference on these pages right here.
4407 */
Andreas Gruenbacher7721f562011-04-06 17:14:02 +02004408 i = drbd_free_peer_reqs(mdev, &mdev->net_ee);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004409 if (i)
4410 dev_info(DEV, "net_ee not empty, killed %u entries\n", i);
Lars Ellenberg435f0742010-09-06 12:30:25 +02004411 i = atomic_read(&mdev->pp_in_use_by_net);
4412 if (i)
4413 dev_info(DEV, "pp_in_use_by_net = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004414 i = atomic_read(&mdev->pp_in_use);
4415 if (i)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02004416 dev_info(DEV, "pp_in_use = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004417
4418 D_ASSERT(list_empty(&mdev->read_ee));
4419 D_ASSERT(list_empty(&mdev->active_ee));
4420 D_ASSERT(list_empty(&mdev->sync_ee));
4421 D_ASSERT(list_empty(&mdev->done_ee));
4422
4423 /* ok, no more ee's on the fly, it is safe to reset the epoch_size */
4424 atomic_set(&mdev->current_epoch->epoch_size, 0);
4425 D_ASSERT(list_empty(&mdev->current_epoch->list));
Philipp Reisner360cc742011-02-08 14:29:53 +01004426
4427 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004428}
4429
4430/*
4431 * We support PRO_VERSION_MIN to PRO_VERSION_MAX. The protocol version
4432 * we can agree on is stored in agreed_pro_version.
4433 *
4434 * feature flags and the reserved array should be enough room for future
4435 * enhancements of the handshake protocol, and possible plugins...
4436 *
4437 * for now, they are expected to be zero, but ignored.
4438 */
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004439static int drbd_send_features(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004440{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004441 struct drbd_socket *sock;
4442 struct p_connection_features *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004443
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004444 sock = &tconn->data;
4445 p = conn_prepare_command(tconn, sock);
4446 if (!p)
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004447 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004448 memset(p, 0, sizeof(*p));
4449 p->protocol_min = cpu_to_be32(PRO_VERSION_MIN);
4450 p->protocol_max = cpu_to_be32(PRO_VERSION_MAX);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004451 return conn_send_command(tconn, sock, P_CONNECTION_FEATURES, sizeof(*p), NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004452}
4453
4454/*
4455 * return values:
4456 * 1 yes, we have a valid connection
4457 * 0 oops, did not work out, please try again
4458 * -1 peer talks different language,
4459 * no point in trying again, please go standalone.
4460 */
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004461static int drbd_do_features(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004462{
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004463 /* ASSERT current == tconn->receiver ... */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004464 struct p_connection_features *p;
4465 const int expect = sizeof(struct p_connection_features);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004466 struct packet_info pi;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004467 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004468
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004469 err = drbd_send_features(tconn);
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004470 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004471 return 0;
4472
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004473 err = drbd_recv_header(tconn, &pi);
4474 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004475 return 0;
4476
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004477 if (pi.cmd != P_CONNECTION_FEATURES) {
4478 conn_err(tconn, "expected ConnectionFeatures packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004479 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004480 return -1;
4481 }
4482
Philipp Reisner77351055b2011-02-07 17:24:26 +01004483 if (pi.size != expect) {
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004484 conn_err(tconn, "expected ConnectionFeatures length: %u, received: %u\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004485 expect, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004486 return -1;
4487 }
4488
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004489 p = pi.data;
4490 err = drbd_recv_all_warn(tconn, p, expect);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004491 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004492 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004493
Philipp Reisnerb411b362009-09-25 16:07:19 -07004494 p->protocol_min = be32_to_cpu(p->protocol_min);
4495 p->protocol_max = be32_to_cpu(p->protocol_max);
4496 if (p->protocol_max == 0)
4497 p->protocol_max = p->protocol_min;
4498
4499 if (PRO_VERSION_MAX < p->protocol_min ||
4500 PRO_VERSION_MIN > p->protocol_max)
4501 goto incompat;
4502
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004503 tconn->agreed_pro_version = min_t(int, PRO_VERSION_MAX, p->protocol_max);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004504
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004505 conn_info(tconn, "Handshake successful: "
4506 "Agreed network protocol version %d\n", tconn->agreed_pro_version);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004507
4508 return 1;
4509
4510 incompat:
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004511 conn_err(tconn, "incompatible DRBD dialects: "
Philipp Reisnerb411b362009-09-25 16:07:19 -07004512 "I support %d-%d, peer supports %d-%d\n",
4513 PRO_VERSION_MIN, PRO_VERSION_MAX,
4514 p->protocol_min, p->protocol_max);
4515 return -1;
4516}
4517
4518#if !defined(CONFIG_CRYPTO_HMAC) && !defined(CONFIG_CRYPTO_HMAC_MODULE)
Philipp Reisner13e60372011-02-08 09:54:40 +01004519static int drbd_do_auth(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004520{
4521 dev_err(DEV, "This kernel was build without CONFIG_CRYPTO_HMAC.\n");
4522 dev_err(DEV, "You need to disable 'cram-hmac-alg' in drbd.conf.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004523 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004524}
4525#else
4526#define CHALLENGE_LEN 64
Johannes Thomab10d96c2010-01-07 16:02:50 +01004527
4528/* Return value:
4529 1 - auth succeeded,
4530 0 - failed, try again (network error),
4531 -1 - auth failed, don't try again.
4532*/
4533
Philipp Reisner13e60372011-02-08 09:54:40 +01004534static int drbd_do_auth(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004535{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004536 struct drbd_socket *sock;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004537 char my_challenge[CHALLENGE_LEN]; /* 64 Bytes... */
4538 struct scatterlist sg;
4539 char *response = NULL;
4540 char *right_response = NULL;
4541 char *peers_ch = NULL;
Philipp Reisner44ed1672011-04-19 17:10:19 +02004542 unsigned int key_len;
4543 char secret[SHARED_SECRET_MAX]; /* 64 byte */
Philipp Reisnerb411b362009-09-25 16:07:19 -07004544 unsigned int resp_size;
4545 struct hash_desc desc;
Philipp Reisner77351055b2011-02-07 17:24:26 +01004546 struct packet_info pi;
Philipp Reisner44ed1672011-04-19 17:10:19 +02004547 struct net_conf *nc;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004548 int err, rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004549
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004550 /* FIXME: Put the challenge/response into the preallocated socket buffer. */
4551
Philipp Reisner44ed1672011-04-19 17:10:19 +02004552 rcu_read_lock();
4553 nc = rcu_dereference(tconn->net_conf);
4554 key_len = strlen(nc->shared_secret);
4555 memcpy(secret, nc->shared_secret, key_len);
4556 rcu_read_unlock();
4557
Philipp Reisner13e60372011-02-08 09:54:40 +01004558 desc.tfm = tconn->cram_hmac_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004559 desc.flags = 0;
4560
Philipp Reisner44ed1672011-04-19 17:10:19 +02004561 rv = crypto_hash_setkey(tconn->cram_hmac_tfm, (u8 *)secret, key_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004562 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004563 conn_err(tconn, "crypto_hash_setkey() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004564 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004565 goto fail;
4566 }
4567
4568 get_random_bytes(my_challenge, CHALLENGE_LEN);
4569
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004570 sock = &tconn->data;
4571 if (!conn_prepare_command(tconn, sock)) {
4572 rv = 0;
4573 goto fail;
4574 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004575 rv = !conn_send_command(tconn, sock, P_AUTH_CHALLENGE, 0,
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004576 my_challenge, CHALLENGE_LEN);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004577 if (!rv)
4578 goto fail;
4579
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004580 err = drbd_recv_header(tconn, &pi);
4581 if (err) {
4582 rv = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004583 goto fail;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004584 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004585
Philipp Reisner77351055b2011-02-07 17:24:26 +01004586 if (pi.cmd != P_AUTH_CHALLENGE) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004587 conn_err(tconn, "expected AuthChallenge packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004588 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004589 rv = 0;
4590 goto fail;
4591 }
4592
Philipp Reisner77351055b2011-02-07 17:24:26 +01004593 if (pi.size > CHALLENGE_LEN * 2) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004594 conn_err(tconn, "expected AuthChallenge payload too big.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004595 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004596 goto fail;
4597 }
4598
Philipp Reisner77351055b2011-02-07 17:24:26 +01004599 peers_ch = kmalloc(pi.size, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004600 if (peers_ch == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004601 conn_err(tconn, "kmalloc of peers_ch failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004602 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004603 goto fail;
4604 }
4605
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004606 err = drbd_recv_all_warn(tconn, peers_ch, pi.size);
4607 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004608 rv = 0;
4609 goto fail;
4610 }
4611
Philipp Reisner13e60372011-02-08 09:54:40 +01004612 resp_size = crypto_hash_digestsize(tconn->cram_hmac_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004613 response = kmalloc(resp_size, GFP_NOIO);
4614 if (response == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004615 conn_err(tconn, "kmalloc of response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004616 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004617 goto fail;
4618 }
4619
4620 sg_init_table(&sg, 1);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004621 sg_set_buf(&sg, peers_ch, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004622
4623 rv = crypto_hash_digest(&desc, &sg, sg.length, response);
4624 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004625 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004626 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004627 goto fail;
4628 }
4629
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004630 if (!conn_prepare_command(tconn, sock)) {
4631 rv = 0;
4632 goto fail;
4633 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004634 rv = !conn_send_command(tconn, sock, P_AUTH_RESPONSE, 0,
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004635 response, resp_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004636 if (!rv)
4637 goto fail;
4638
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004639 err = drbd_recv_header(tconn, &pi);
4640 if (err) {
4641 rv = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004642 goto fail;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004643 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004644
Philipp Reisner77351055b2011-02-07 17:24:26 +01004645 if (pi.cmd != P_AUTH_RESPONSE) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004646 conn_err(tconn, "expected AuthResponse packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004647 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004648 rv = 0;
4649 goto fail;
4650 }
4651
Philipp Reisner77351055b2011-02-07 17:24:26 +01004652 if (pi.size != resp_size) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004653 conn_err(tconn, "expected AuthResponse payload of wrong size\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004654 rv = 0;
4655 goto fail;
4656 }
4657
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004658 err = drbd_recv_all_warn(tconn, response , resp_size);
4659 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004660 rv = 0;
4661 goto fail;
4662 }
4663
4664 right_response = kmalloc(resp_size, GFP_NOIO);
Julia Lawall2d1ee872009-12-27 22:27:11 +01004665 if (right_response == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004666 conn_err(tconn, "kmalloc of right_response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004667 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004668 goto fail;
4669 }
4670
4671 sg_set_buf(&sg, my_challenge, CHALLENGE_LEN);
4672
4673 rv = crypto_hash_digest(&desc, &sg, sg.length, right_response);
4674 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004675 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004676 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004677 goto fail;
4678 }
4679
4680 rv = !memcmp(response, right_response, resp_size);
4681
4682 if (rv)
Philipp Reisner44ed1672011-04-19 17:10:19 +02004683 conn_info(tconn, "Peer authenticated using %d bytes HMAC\n",
4684 resp_size);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004685 else
4686 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004687
4688 fail:
4689 kfree(peers_ch);
4690 kfree(response);
4691 kfree(right_response);
4692
4693 return rv;
4694}
4695#endif
4696
4697int drbdd_init(struct drbd_thread *thi)
4698{
Philipp Reisner392c8802011-02-09 10:33:31 +01004699 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004700 int h;
4701
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004702 conn_info(tconn, "receiver (re)started\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004703
4704 do {
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004705 h = conn_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004706 if (h == 0) {
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004707 conn_disconnect(tconn);
Philipp Reisner20ee6392011-01-18 15:28:59 +01004708 schedule_timeout_interruptible(HZ);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004709 }
4710 if (h == -1) {
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004711 conn_warn(tconn, "Discarding network configuration.\n");
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004712 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004713 }
4714 } while (h == 0);
4715
Philipp Reisner91fd4da2011-04-20 17:47:29 +02004716 if (h > 0)
4717 drbdd(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004718
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004719 conn_disconnect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004720
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004721 conn_info(tconn, "receiver terminated\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004722 return 0;
4723}
4724
4725/* ********* acknowledge sender ******** */
4726
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004727static int got_conn_RqSReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004728{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004729 struct p_req_state_reply *p = pi->data;
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004730 int retcode = be32_to_cpu(p->retcode);
4731
4732 if (retcode >= SS_SUCCESS) {
4733 set_bit(CONN_WD_ST_CHG_OKAY, &tconn->flags);
4734 } else {
4735 set_bit(CONN_WD_ST_CHG_FAIL, &tconn->flags);
4736 conn_err(tconn, "Requested state change failed by peer: %s (%d)\n",
4737 drbd_set_st_err_str(retcode), retcode);
4738 }
4739 wake_up(&tconn->ping_wait);
4740
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004741 return 0;
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004742}
4743
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004744static int got_RqSReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004745{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004746 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004747 struct p_req_state_reply *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004748 int retcode = be32_to_cpu(p->retcode);
4749
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004750 mdev = vnr_to_mdev(tconn, pi->vnr);
4751 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004752 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004753
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004754 if (retcode >= SS_SUCCESS) {
4755 set_bit(CL_ST_CHG_SUCCESS, &mdev->flags);
4756 } else {
4757 set_bit(CL_ST_CHG_FAIL, &mdev->flags);
4758 dev_err(DEV, "Requested state change failed by peer: %s (%d)\n",
4759 drbd_set_st_err_str(retcode), retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004760 }
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004761 wake_up(&mdev->state_wait);
4762
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004763 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004764}
4765
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004766static int got_Ping(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004767{
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004768 return drbd_send_ping_ack(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004769
4770}
4771
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004772static int got_PingAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004773{
4774 /* restore idle timeout */
Philipp Reisner2a67d8b2011-02-09 14:10:32 +01004775 tconn->meta.socket->sk->sk_rcvtimeo = tconn->net_conf->ping_int*HZ;
4776 if (!test_and_set_bit(GOT_PING_ACK, &tconn->flags))
4777 wake_up(&tconn->ping_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004778
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004779 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004780}
4781
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004782static int got_IsInSync(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004783{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004784 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004785 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004786 sector_t sector = be64_to_cpu(p->sector);
4787 int blksize = be32_to_cpu(p->blksize);
4788
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004789 mdev = vnr_to_mdev(tconn, pi->vnr);
4790 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004791 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004792
Philipp Reisner31890f42011-01-19 14:12:51 +01004793 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004794
4795 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4796
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004797 if (get_ldev(mdev)) {
4798 drbd_rs_complete_io(mdev, sector);
4799 drbd_set_in_sync(mdev, sector, blksize);
4800 /* rs_same_csums is supposed to count in units of BM_BLOCK_SIZE */
4801 mdev->rs_same_csum += (blksize >> BM_BLOCK_SHIFT);
4802 put_ldev(mdev);
4803 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004804 dec_rs_pending(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02004805 atomic_add(blksize >> 9, &mdev->rs_sect_in);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004806
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004807 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004808}
4809
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004810static int
4811validate_req_change_req_state(struct drbd_conf *mdev, u64 id, sector_t sector,
4812 struct rb_root *root, const char *func,
4813 enum drbd_req_event what, bool missing_ok)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004814{
4815 struct drbd_request *req;
4816 struct bio_and_error m;
4817
Philipp Reisner87eeee42011-01-19 14:16:30 +01004818 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004819 req = find_request(mdev, root, id, sector, missing_ok, func);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004820 if (unlikely(!req)) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01004821 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004822 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004823 }
4824 __req_mod(req, what, &m);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004825 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004826
4827 if (m.bio)
4828 complete_master_bio(mdev, &m);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004829 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004830}
4831
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004832static int got_BlockAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004833{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004834 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004835 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004836 sector_t sector = be64_to_cpu(p->sector);
4837 int blksize = be32_to_cpu(p->blksize);
4838 enum drbd_req_event what;
4839
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004840 mdev = vnr_to_mdev(tconn, pi->vnr);
4841 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004842 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004843
Philipp Reisnerb411b362009-09-25 16:07:19 -07004844 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4845
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01004846 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004847 drbd_set_in_sync(mdev, sector, blksize);
4848 dec_rs_pending(mdev);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004849 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004850 }
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004851 switch (pi->cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004852 case P_RS_WRITE_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004853 what = WRITE_ACKED_BY_PEER_AND_SIS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004854 break;
4855 case P_WRITE_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004856 what = WRITE_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004857 break;
4858 case P_RECV_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004859 what = RECV_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004860 break;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004861 case P_DISCARD_WRITE:
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004862 what = DISCARD_WRITE;
4863 break;
4864 case P_RETRY_WRITE:
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004865 what = POSTPONE_WRITE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004866 break;
4867 default:
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004868 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -07004869 }
4870
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004871 return validate_req_change_req_state(mdev, p->block_id, sector,
4872 &mdev->write_requests, __func__,
4873 what, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004874}
4875
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004876static int got_NegAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004877{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004878 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004879 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004880 sector_t sector = be64_to_cpu(p->sector);
Philipp Reisner2deb8332011-01-17 18:39:18 +01004881 int size = be32_to_cpu(p->blksize);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004882 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004883
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004884 mdev = vnr_to_mdev(tconn, pi->vnr);
4885 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004886 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004887
Philipp Reisnerb411b362009-09-25 16:07:19 -07004888 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4889
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01004890 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004891 dec_rs_pending(mdev);
4892 drbd_rs_failed_io(mdev, sector, size);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004893 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004894 }
Philipp Reisner2deb8332011-01-17 18:39:18 +01004895
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004896 err = validate_req_change_req_state(mdev, p->block_id, sector,
4897 &mdev->write_requests, __func__,
Philipp Reisner303d1442011-04-13 16:24:47 -07004898 NEG_ACKED, true);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004899 if (err) {
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004900 /* Protocol A has no P_WRITE_ACKs, but has P_NEG_ACKs.
4901 The master bio might already be completed, therefore the
4902 request is no longer in the collision hash. */
4903 /* In Protocol B we might already have got a P_RECV_ACK
4904 but then get a P_NEG_ACK afterwards. */
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004905 drbd_set_out_of_sync(mdev, sector, size);
Philipp Reisner2deb8332011-01-17 18:39:18 +01004906 }
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004907 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004908}
4909
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004910static int got_NegDReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004911{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004912 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004913 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004914 sector_t sector = be64_to_cpu(p->sector);
4915
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004916 mdev = vnr_to_mdev(tconn, pi->vnr);
4917 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004918 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004919
Philipp Reisnerb411b362009-09-25 16:07:19 -07004920 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004921
Philipp Reisnerb411b362009-09-25 16:07:19 -07004922 dev_err(DEV, "Got NegDReply; Sector %llus, len %u; Fail original request.\n",
4923 (unsigned long long)sector, be32_to_cpu(p->blksize));
4924
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004925 return validate_req_change_req_state(mdev, p->block_id, sector,
4926 &mdev->read_requests, __func__,
4927 NEG_ACKED, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004928}
4929
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004930static int got_NegRSDReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004931{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004932 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004933 sector_t sector;
4934 int size;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004935 struct p_block_ack *p = pi->data;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004936
4937 mdev = vnr_to_mdev(tconn, pi->vnr);
4938 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004939 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004940
4941 sector = be64_to_cpu(p->sector);
4942 size = be32_to_cpu(p->blksize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004943
4944 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4945
4946 dec_rs_pending(mdev);
4947
4948 if (get_ldev_if_state(mdev, D_FAILED)) {
4949 drbd_rs_complete_io(mdev, sector);
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004950 switch (pi->cmd) {
Philipp Reisnerd612d302010-12-27 10:53:28 +01004951 case P_NEG_RS_DREPLY:
4952 drbd_rs_failed_io(mdev, sector, size);
4953 case P_RS_CANCEL:
4954 break;
4955 default:
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004956 BUG();
Philipp Reisnerd612d302010-12-27 10:53:28 +01004957 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004958 put_ldev(mdev);
4959 }
4960
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004961 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004962}
4963
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004964static int got_BarrierAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004965{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004966 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004967 struct p_barrier_ack *p = pi->data;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004968
4969 mdev = vnr_to_mdev(tconn, pi->vnr);
4970 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004971 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004972
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01004973 tl_release(mdev->tconn, p->barrier, be32_to_cpu(p->set_size));
Philipp Reisnerb411b362009-09-25 16:07:19 -07004974
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02004975 if (mdev->state.conn == C_AHEAD &&
4976 atomic_read(&mdev->ap_in_flight) == 0 &&
Philipp Reisner370a43e2011-01-14 16:03:11 +01004977 !test_and_set_bit(AHEAD_TO_SYNC_SOURCE, &mdev->current_epoch->flags)) {
4978 mdev->start_resync_timer.expires = jiffies + HZ;
4979 add_timer(&mdev->start_resync_timer);
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02004980 }
4981
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004982 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004983}
4984
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004985static int got_OVResult(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004986{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004987 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004988 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004989 struct drbd_work *w;
4990 sector_t sector;
4991 int size;
4992
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004993 mdev = vnr_to_mdev(tconn, pi->vnr);
4994 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004995 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004996
Philipp Reisnerb411b362009-09-25 16:07:19 -07004997 sector = be64_to_cpu(p->sector);
4998 size = be32_to_cpu(p->blksize);
4999
5000 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
5001
5002 if (be64_to_cpu(p->block_id) == ID_OUT_OF_SYNC)
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005003 drbd_ov_out_of_sync_found(mdev, sector, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005004 else
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005005 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005006
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005007 if (!get_ldev(mdev))
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005008 return 0;
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005009
Philipp Reisnerb411b362009-09-25 16:07:19 -07005010 drbd_rs_complete_io(mdev, sector);
5011 dec_rs_pending(mdev);
5012
Lars Ellenbergea5442a2010-11-05 09:48:01 +01005013 --mdev->ov_left;
5014
5015 /* let's advance progress step marks only for every other megabyte */
5016 if ((mdev->ov_left & 0x200) == 0x200)
5017 drbd_advance_rs_marks(mdev, mdev->ov_left);
5018
5019 if (mdev->ov_left == 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07005020 w = kmalloc(sizeof(*w), GFP_NOIO);
5021 if (w) {
5022 w->cb = w_ov_finished;
Philipp Reisnera21e9292011-02-08 15:08:49 +01005023 w->mdev = mdev;
Philipp Reisnere42325a2011-01-19 13:55:45 +01005024 drbd_queue_work_front(&mdev->tconn->data.work, w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005025 } else {
5026 dev_err(DEV, "kmalloc(w) failed.");
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005027 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005028 drbd_resync_finished(mdev);
5029 }
5030 }
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005031 put_ldev(mdev);
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_skip(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner0ced55a2010-04-30 15:26:20 +02005036{
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005037 return 0;
Philipp Reisner0ced55a2010-04-30 15:26:20 +02005038}
5039
Andreas Gruenbachera990be42011-04-06 17:56:48 +02005040static int tconn_finish_peer_reqs(struct drbd_tconn *tconn)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005041{
Philipp Reisner082a3432011-03-15 16:05:42 +01005042 struct drbd_conf *mdev;
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005043 int vnr, not_empty = 0;
Philipp Reisner32862ec2011-02-08 16:41:01 +01005044
5045 do {
5046 clear_bit(SIGNAL_ASENDER, &tconn->flags);
5047 flush_signals(current);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005048
5049 rcu_read_lock();
5050 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
5051 kref_get(&mdev->kref);
5052 rcu_read_unlock();
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07005053 if (drbd_finish_peer_reqs(mdev)) {
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005054 kref_put(&mdev->kref, &drbd_minor_destroy);
5055 return 1;
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07005056 }
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005057 kref_put(&mdev->kref, &drbd_minor_destroy);
5058 rcu_read_lock();
Philipp Reisner082a3432011-03-15 16:05:42 +01005059 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005060 set_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisner082a3432011-03-15 16:05:42 +01005061
5062 spin_lock_irq(&tconn->req_lock);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005063 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Philipp Reisner082a3432011-03-15 16:05:42 +01005064 not_empty = !list_empty(&mdev->done_ee);
5065 if (not_empty)
5066 break;
5067 }
5068 spin_unlock_irq(&tconn->req_lock);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005069 rcu_read_unlock();
Philipp Reisner32862ec2011-02-08 16:41:01 +01005070 } while (not_empty);
5071
5072 return 0;
5073}
5074
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005075struct asender_cmd {
5076 size_t pkt_size;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005077 int (*fn)(struct drbd_tconn *tconn, struct packet_info *);
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005078};
5079
5080static struct asender_cmd asender_tbl[] = {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005081 [P_PING] = { 0, got_Ping },
5082 [P_PING_ACK] = { 0, got_PingAck },
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005083 [P_RECV_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5084 [P_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5085 [P_RS_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5086 [P_DISCARD_WRITE] = { sizeof(struct p_block_ack), got_BlockAck },
5087 [P_NEG_ACK] = { sizeof(struct p_block_ack), got_NegAck },
5088 [P_NEG_DREPLY] = { sizeof(struct p_block_ack), got_NegDReply },
5089 [P_NEG_RS_DREPLY] = { sizeof(struct p_block_ack), got_NegRSDReply },
5090 [P_OV_RESULT] = { sizeof(struct p_block_ack), got_OVResult },
5091 [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), got_BarrierAck },
5092 [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), got_RqSReply },
5093 [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), got_IsInSync },
5094 [P_DELAY_PROBE] = { sizeof(struct p_delay_probe93), got_skip },
5095 [P_RS_CANCEL] = { sizeof(struct p_block_ack), got_NegRSDReply },
5096 [P_CONN_ST_CHG_REPLY]={ sizeof(struct p_req_state_reply), got_conn_RqSReply },
5097 [P_RETRY_WRITE] = { sizeof(struct p_block_ack), got_BlockAck },
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005098};
5099
Philipp Reisnerb411b362009-09-25 16:07:19 -07005100int drbd_asender(struct drbd_thread *thi)
5101{
Philipp Reisner392c8802011-02-09 10:33:31 +01005102 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005103 struct asender_cmd *cmd = NULL;
Philipp Reisner77351055b2011-02-07 17:24:26 +01005104 struct packet_info pi;
Philipp Reisner257d0af2011-01-26 12:15:29 +01005105 int rv;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005106 void *buf = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005107 int received = 0;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005108 unsigned int header_size = drbd_header_size(tconn);
5109 int expect = header_size;
Philipp Reisner44ed1672011-04-19 17:10:19 +02005110 bool ping_timeout_active = false;
5111 struct net_conf *nc;
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005112 int ping_timeo, tcp_cork, ping_int;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005113
Philipp Reisnerb411b362009-09-25 16:07:19 -07005114 current->policy = SCHED_RR; /* Make this a realtime task! */
5115 current->rt_priority = 2; /* more important than all other tasks */
5116
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +01005117 while (get_t_state(thi) == RUNNING) {
Philipp Reisner80822282011-02-08 12:46:30 +01005118 drbd_thread_current_set_cpu(thi);
Philipp Reisner44ed1672011-04-19 17:10:19 +02005119
5120 rcu_read_lock();
5121 nc = rcu_dereference(tconn->net_conf);
5122 ping_timeo = nc->ping_timeo;
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005123 tcp_cork = nc->tcp_cork;
Philipp Reisner44ed1672011-04-19 17:10:19 +02005124 ping_int = nc->ping_int;
5125 rcu_read_unlock();
5126
Philipp Reisner32862ec2011-02-08 16:41:01 +01005127 if (test_and_clear_bit(SEND_PING, &tconn->flags)) {
Andreas Gruenbachera17647a2011-04-01 12:49:42 +02005128 if (drbd_send_ping(tconn)) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005129 conn_err(tconn, "drbd_send_ping has failed\n");
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01005130 goto reconnect;
5131 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02005132 tconn->meta.socket->sk->sk_rcvtimeo = ping_timeo * HZ / 10;
5133 ping_timeout_active = true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005134 }
5135
Philipp Reisner32862ec2011-02-08 16:41:01 +01005136 /* TODO: conditionally cork; it may hurt latency if we cork without
5137 much to send */
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005138 if (tcp_cork)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005139 drbd_tcp_cork(tconn->meta.socket);
Andreas Gruenbachera990be42011-04-06 17:56:48 +02005140 if (tconn_finish_peer_reqs(tconn)) {
5141 conn_err(tconn, "tconn_finish_peer_reqs() failed\n");
Philipp Reisner32862ec2011-02-08 16:41:01 +01005142 goto reconnect;
Philipp Reisner082a3432011-03-15 16:05:42 +01005143 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005144 /* but unconditionally uncork unless disabled */
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005145 if (tcp_cork)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005146 drbd_tcp_uncork(tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005147
5148 /* short circuit, recv_msg would return EINTR anyways. */
5149 if (signal_pending(current))
5150 continue;
5151
Philipp Reisner32862ec2011-02-08 16:41:01 +01005152 rv = drbd_recv_short(tconn->meta.socket, buf, expect-received, 0);
5153 clear_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005154
5155 flush_signals(current);
5156
5157 /* Note:
5158 * -EINTR (on meta) we got a signal
5159 * -EAGAIN (on meta) rcvtimeo expired
5160 * -ECONNRESET other side closed the connection
5161 * -ERESTARTSYS (on data) we got a signal
5162 * rv < 0 other than above: unexpected error!
5163 * rv == expected: full header or command
5164 * rv < expected: "woken" by signal during receive
5165 * rv == 0 : "connection shut down by peer"
5166 */
5167 if (likely(rv > 0)) {
5168 received += rv;
5169 buf += rv;
5170 } else if (rv == 0) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005171 conn_err(tconn, "meta connection shut down by peer.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005172 goto reconnect;
5173 } else if (rv == -EAGAIN) {
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02005174 /* If the data socket received something meanwhile,
5175 * that is good enough: peer is still alive. */
Philipp Reisner32862ec2011-02-08 16:41:01 +01005176 if (time_after(tconn->last_received,
5177 jiffies - tconn->meta.socket->sk->sk_rcvtimeo))
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02005178 continue;
Lars Ellenbergf36af182011-03-09 22:44:55 +01005179 if (ping_timeout_active) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005180 conn_err(tconn, "PingAck did not arrive in time.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005181 goto reconnect;
5182 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005183 set_bit(SEND_PING, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005184 continue;
5185 } else if (rv == -EINTR) {
5186 continue;
5187 } else {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005188 conn_err(tconn, "sock_recvmsg returned %d\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005189 goto reconnect;
5190 }
5191
5192 if (received == expect && cmd == NULL) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005193 if (decode_header(tconn, tconn->meta.rbuf, &pi))
Philipp Reisnerb411b362009-09-25 16:07:19 -07005194 goto reconnect;
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005195 cmd = &asender_tbl[pi.cmd];
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005196 if (pi.cmd >= ARRAY_SIZE(asender_tbl) || !cmd->fn) {
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02005197 conn_err(tconn, "Unexpected meta packet %s (0x%04x)\n",
5198 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005199 goto disconnect;
5200 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005201 expect = header_size + cmd->pkt_size;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005202 if (pi.size != expect - header_size) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005203 conn_err(tconn, "Wrong packet size on meta (c: %d, l: %d)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01005204 pi.cmd, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005205 goto reconnect;
Philipp Reisner257d0af2011-01-26 12:15:29 +01005206 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005207 }
5208 if (received == expect) {
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005209 bool err;
Philipp Reisnera4fbda82011-03-16 11:13:17 +01005210
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005211 err = cmd->fn(tconn, &pi);
5212 if (err) {
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005213 conn_err(tconn, "%pf failed\n", cmd->fn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005214 goto reconnect;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005215 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005216
Philipp Reisnera4fbda82011-03-16 11:13:17 +01005217 tconn->last_received = jiffies;
5218
Philipp Reisner44ed1672011-04-19 17:10:19 +02005219 if (cmd == &asender_tbl[P_PING_ACK]) {
5220 /* restore idle timeout */
5221 tconn->meta.socket->sk->sk_rcvtimeo = ping_int * HZ;
5222 ping_timeout_active = false;
5223 }
Lars Ellenbergf36af182011-03-09 22:44:55 +01005224
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005225 buf = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005226 received = 0;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005227 expect = header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005228 cmd = NULL;
5229 }
5230 }
5231
5232 if (0) {
5233reconnect:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01005234 conn_request_state(tconn, NS(conn, C_NETWORK_FAILURE), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005235 }
5236 if (0) {
5237disconnect:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01005238 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005239 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005240 clear_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005241
Philipp Reisner32862ec2011-02-08 16:41:01 +01005242 conn_info(tconn, "asender terminated\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005243
5244 return 0;
5245}