blob: 06bbf0f79fc5683feedc2d5b22ae2114d4438390 [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 Reisnerbbeb6412011-02-10 13:45:46 +0100990 if (conn_request_state(tconn, NS(conn, C_WF_REPORT_PARAMS), CS_VERBOSE) < SS_SUCCESS)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700991 return 0;
992
Philipp Reisner44ed1672011-04-19 17:10:19 +0200993 sock->sk->sk_sndtimeo = timeout;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700994 sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
995
Philipp Reisner907599e2011-02-08 11:25:37 +0100996 drbd_thread_start(&tconn->asender);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700997
Andreas Gruenbacher387eb302011-03-16 01:05:37 +0100998 if (drbd_send_protocol(tconn) == -EOPNOTSUPP)
Philipp Reisner7e2455c2010-04-22 14:50:23 +0200999 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001000
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02001001 rcu_read_lock();
1002 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1003 kref_get(&mdev->kref);
1004 rcu_read_unlock();
1005 drbd_connected(mdev);
1006 kref_put(&mdev->kref, &drbd_minor_destroy);
1007 rcu_read_lock();
1008 }
1009 rcu_read_unlock();
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 Reisner2451fc32010-08-24 13:43:11 +02001128 test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags)) {
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 }
1134 dec_unacked(mdev);
1135
1136 if (mdev->current_epoch != epoch) {
1137 next_epoch = list_entry(epoch->list.next, struct drbd_epoch, list);
1138 list_del(&epoch->list);
1139 ev = EV_BECAME_LAST | (ev & EV_CLEANUP);
1140 mdev->epochs--;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001141 kfree(epoch);
1142
1143 if (rv == FE_STILL_LIVE)
1144 rv = FE_DESTROYED;
1145 } else {
1146 epoch->flags = 0;
1147 atomic_set(&epoch->epoch_size, 0);
Uwe Kleine-König698f9312010-07-02 20:41:51 +02001148 /* atomic_set(&epoch->active, 0); is already zero */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001149 if (rv == FE_STILL_LIVE)
1150 rv = FE_RECYCLED;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001151 wake_up(&mdev->ee_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001152 }
1153 }
1154
1155 if (!next_epoch)
1156 break;
1157
1158 epoch = next_epoch;
1159 } while (1);
1160
1161 spin_unlock(&mdev->epoch_lock);
1162
Philipp Reisnerb411b362009-09-25 16:07:19 -07001163 return rv;
1164}
1165
1166/**
1167 * drbd_bump_write_ordering() - Fall back to an other write ordering method
1168 * @mdev: DRBD device.
1169 * @wo: Write ordering method to try.
1170 */
1171void drbd_bump_write_ordering(struct drbd_conf *mdev, enum write_ordering_e wo) __must_hold(local)
1172{
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001173 struct disk_conf *dc;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001174 enum write_ordering_e pwo;
1175 static char *write_ordering_str[] = {
1176 [WO_none] = "none",
1177 [WO_drain_io] = "drain",
1178 [WO_bdev_flush] = "flush",
Philipp Reisnerb411b362009-09-25 16:07:19 -07001179 };
1180
1181 pwo = mdev->write_ordering;
1182 wo = min(pwo, wo);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001183 rcu_read_lock();
1184 dc = rcu_dereference(mdev->ldev->disk_conf);
1185
Andreas Gruenbacher66b2f6b2011-05-04 15:25:35 +02001186 if (wo == WO_bdev_flush && !dc->disk_flushes)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001187 wo = WO_drain_io;
Andreas Gruenbacherd0c980e2011-05-04 15:25:35 +02001188 if (wo == WO_drain_io && !dc->disk_drain)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001189 wo = WO_none;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001190 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07001191 mdev->write_ordering = wo;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001192 if (pwo != mdev->write_ordering || wo == WO_bdev_flush)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001193 dev_info(DEV, "Method to ensure write ordering: %s\n", write_ordering_str[mdev->write_ordering]);
1194}
1195
1196/**
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001197 * drbd_submit_peer_request()
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001198 * @mdev: DRBD device.
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001199 * @peer_req: peer request
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001200 * @rw: flag field, see bio->bi_rw
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001201 *
1202 * May spread the pages to multiple bios,
1203 * depending on bio_add_page restrictions.
1204 *
1205 * Returns 0 if all bios have been submitted,
1206 * -ENOMEM if we could not allocate enough bios,
1207 * -ENOSPC (any better suggestion?) if we have not been able to bio_add_page a
1208 * single page to an empty bio (which should never happen and likely indicates
1209 * that the lower level IO stack is in some way broken). This has been observed
1210 * on certain Xen deployments.
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001211 */
1212/* TODO allocate from our own bio_set. */
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001213int drbd_submit_peer_request(struct drbd_conf *mdev,
1214 struct drbd_peer_request *peer_req,
1215 const unsigned rw, const int fault_type)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001216{
1217 struct bio *bios = NULL;
1218 struct bio *bio;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001219 struct page *page = peer_req->pages;
1220 sector_t sector = peer_req->i.sector;
1221 unsigned ds = peer_req->i.size;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001222 unsigned n_bios = 0;
1223 unsigned nr_pages = (ds + PAGE_SIZE -1) >> PAGE_SHIFT;
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001224 int err = -ENOMEM;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001225
1226 /* In most cases, we will only need one bio. But in case the lower
1227 * level restrictions happen to be different at this offset on this
1228 * side than those of the sending peer, we may need to submit the
Lars Ellenbergda4a75d2011-02-23 17:02:01 +01001229 * request in more than one bio.
1230 *
1231 * Plain bio_alloc is good enough here, this is no DRBD internally
1232 * generated bio, but a bio allocated on behalf of the peer.
1233 */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001234next_bio:
1235 bio = bio_alloc(GFP_NOIO, nr_pages);
1236 if (!bio) {
1237 dev_err(DEV, "submit_ee: Allocation of a bio failed\n");
1238 goto fail;
1239 }
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001240 /* > peer_req->i.sector, unless this is the first bio */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001241 bio->bi_sector = sector;
1242 bio->bi_bdev = mdev->ldev->backing_bdev;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001243 bio->bi_rw = rw;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001244 bio->bi_private = peer_req;
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01001245 bio->bi_end_io = drbd_peer_request_endio;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001246
1247 bio->bi_next = bios;
1248 bios = bio;
1249 ++n_bios;
1250
1251 page_chain_for_each(page) {
1252 unsigned len = min_t(unsigned, ds, PAGE_SIZE);
1253 if (!bio_add_page(bio, page, len, 0)) {
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001254 /* A single page must always be possible!
1255 * But in case it fails anyways,
1256 * we deal with it, and complain (below). */
1257 if (bio->bi_vcnt == 0) {
1258 dev_err(DEV,
1259 "bio_add_page failed for len=%u, "
1260 "bi_vcnt=0 (bi_sector=%llu)\n",
1261 len, (unsigned long long)bio->bi_sector);
1262 err = -ENOSPC;
1263 goto fail;
1264 }
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001265 goto next_bio;
1266 }
1267 ds -= len;
1268 sector += len >> 9;
1269 --nr_pages;
1270 }
1271 D_ASSERT(page == NULL);
1272 D_ASSERT(ds == 0);
1273
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001274 atomic_set(&peer_req->pending_bios, n_bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001275 do {
1276 bio = bios;
1277 bios = bios->bi_next;
1278 bio->bi_next = NULL;
1279
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001280 drbd_generic_make_request(mdev, fault_type, bio);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001281 } while (bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001282 return 0;
1283
1284fail:
1285 while (bios) {
1286 bio = bios;
1287 bios = bios->bi_next;
1288 bio_put(bio);
1289 }
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001290 return err;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001291}
1292
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001293static void drbd_remove_epoch_entry_interval(struct drbd_conf *mdev,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001294 struct drbd_peer_request *peer_req)
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001295{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001296 struct drbd_interval *i = &peer_req->i;
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001297
1298 drbd_remove_interval(&mdev->write_requests, i);
1299 drbd_clear_interval(i);
1300
Andreas Gruenbacher6c852be2011-02-04 15:38:52 +01001301 /* Wake up any processes waiting for this peer request to complete. */
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001302 if (i->waiting)
1303 wake_up(&mdev->misc_wait);
1304}
1305
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001306static int receive_Barrier(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001307{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001308 struct drbd_conf *mdev;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001309 int rv;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001310 struct p_barrier *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001311 struct drbd_epoch *epoch;
1312
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001313 mdev = vnr_to_mdev(tconn, pi->vnr);
1314 if (!mdev)
1315 return -EIO;
1316
Philipp Reisnerb411b362009-09-25 16:07:19 -07001317 inc_unacked(mdev);
1318
Philipp Reisnerb411b362009-09-25 16:07:19 -07001319 mdev->current_epoch->barrier_nr = p->barrier;
1320 rv = drbd_may_finish_epoch(mdev, mdev->current_epoch, EV_GOT_BARRIER_NR);
1321
1322 /* P_BARRIER_ACK may imply that the corresponding extent is dropped from
1323 * the activity log, which means it would not be resynced in case the
1324 * R_PRIMARY crashes now.
1325 * Therefore we must send the barrier_ack after the barrier request was
1326 * completed. */
1327 switch (mdev->write_ordering) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001328 case WO_none:
1329 if (rv == FE_RECYCLED)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001330 return 0;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001331
1332 /* receiver context, in the writeout path of the other node.
1333 * avoid potential distributed deadlock */
1334 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1335 if (epoch)
1336 break;
1337 else
1338 dev_warn(DEV, "Allocation of an epoch failed, slowing down\n");
1339 /* Fall through */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001340
1341 case WO_bdev_flush:
1342 case WO_drain_io:
Philipp Reisnerb411b362009-09-25 16:07:19 -07001343 drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
Philipp Reisner2451fc32010-08-24 13:43:11 +02001344 drbd_flush(mdev);
1345
1346 if (atomic_read(&mdev->current_epoch->epoch_size)) {
1347 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1348 if (epoch)
1349 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001350 }
1351
Philipp Reisner2451fc32010-08-24 13:43:11 +02001352 epoch = mdev->current_epoch;
1353 wait_event(mdev->ee_wait, atomic_read(&epoch->epoch_size) == 0);
1354
1355 D_ASSERT(atomic_read(&epoch->active) == 0);
1356 D_ASSERT(epoch->flags == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001357
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001358 return 0;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001359 default:
1360 dev_err(DEV, "Strangeness in mdev->write_ordering %d\n", mdev->write_ordering);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001361 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001362 }
1363
1364 epoch->flags = 0;
1365 atomic_set(&epoch->epoch_size, 0);
1366 atomic_set(&epoch->active, 0);
1367
1368 spin_lock(&mdev->epoch_lock);
1369 if (atomic_read(&mdev->current_epoch->epoch_size)) {
1370 list_add(&epoch->list, &mdev->current_epoch->list);
1371 mdev->current_epoch = epoch;
1372 mdev->epochs++;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001373 } else {
1374 /* The current_epoch got recycled while we allocated this one... */
1375 kfree(epoch);
1376 }
1377 spin_unlock(&mdev->epoch_lock);
1378
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001379 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001380}
1381
1382/* used from receive_RSDataReply (recv_resync_read)
1383 * and from receive_Data */
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001384static struct drbd_peer_request *
1385read_in_block(struct drbd_conf *mdev, u64 id, sector_t sector,
1386 int data_size) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001387{
Lars Ellenberg66660322010-04-06 12:15:04 +02001388 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001389 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001390 struct page *page;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001391 int dgs, ds, err;
Philipp Reisnera0638452011-01-19 14:31:32 +01001392 void *dig_in = mdev->tconn->int_dig_in;
1393 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001394 unsigned long *data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001395
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001396 dgs = 0;
1397 if (mdev->tconn->peer_integrity_tfm) {
1398 dgs = crypto_hash_digestsize(mdev->tconn->peer_integrity_tfm);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001399 /*
1400 * FIXME: Receive the incoming digest into the receive buffer
1401 * here, together with its struct p_data?
1402 */
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001403 err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs);
1404 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001405 return NULL;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001406 data_size -= dgs;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001407 }
1408
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001409 if (!expect(data_size != 0))
1410 return NULL;
1411 if (!expect(IS_ALIGNED(data_size, 512)))
1412 return NULL;
1413 if (!expect(data_size <= DRBD_MAX_BIO_SIZE))
1414 return NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001415
Lars Ellenberg66660322010-04-06 12:15:04 +02001416 /* even though we trust out peer,
1417 * we sometimes have to double check. */
1418 if (sector + (data_size>>9) > capacity) {
Lars Ellenbergfdda6542011-01-24 15:11:01 +01001419 dev_err(DEV, "request from peer beyond end of local disk: "
1420 "capacity: %llus < sector: %llus + size: %u\n",
Lars Ellenberg66660322010-04-06 12:15:04 +02001421 (unsigned long long)capacity,
1422 (unsigned long long)sector, data_size);
1423 return NULL;
1424 }
1425
Philipp Reisnerb411b362009-09-25 16:07:19 -07001426 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
1427 * "criss-cross" setup, that might cause write-out on some other DRBD,
1428 * which in turn might block on the other node at this very place. */
Andreas Gruenbacher0db55362011-04-06 16:09:15 +02001429 peer_req = drbd_alloc_peer_req(mdev, id, sector, data_size, GFP_NOIO);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001430 if (!peer_req)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001431 return NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001432
Philipp Reisnerb411b362009-09-25 16:07:19 -07001433 ds = data_size;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001434 page = peer_req->pages;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001435 page_chain_for_each(page) {
1436 unsigned len = min_t(int, ds, PAGE_SIZE);
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001437 data = kmap(page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001438 err = drbd_recv_all_warn(mdev->tconn, data, len);
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +01001439 if (drbd_insert_fault(mdev, DRBD_FAULT_RECEIVE)) {
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001440 dev_err(DEV, "Fault injection: Corrupting data on receive\n");
1441 data[0] = data[0] ^ (unsigned long)-1;
1442 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001443 kunmap(page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001444 if (err) {
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001445 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001446 return NULL;
1447 }
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001448 ds -= len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001449 }
1450
1451 if (dgs) {
Andreas Gruenbacher5b614ab2011-04-27 21:00:12 +02001452 drbd_csum_ee(mdev, mdev->tconn->peer_integrity_tfm, peer_req, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001453 if (memcmp(dig_in, dig_vv, dgs)) {
Lars Ellenberg470be442010-11-10 10:36:52 +01001454 dev_err(DEV, "Digest integrity check FAILED: %llus +%u\n",
1455 (unsigned long long)sector, data_size);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001456 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001457 return NULL;
1458 }
1459 }
1460 mdev->recv_cnt += data_size>>9;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001461 return peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001462}
1463
1464/* drbd_drain_block() just takes a data block
1465 * out of the socket input buffer, and discards it.
1466 */
1467static int drbd_drain_block(struct drbd_conf *mdev, int data_size)
1468{
1469 struct page *page;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001470 int err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001471 void *data;
1472
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001473 if (!data_size)
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001474 return 0;
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001475
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +02001476 page = drbd_alloc_pages(mdev, 1, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001477
1478 data = kmap(page);
1479 while (data_size) {
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001480 unsigned int len = min_t(int, data_size, PAGE_SIZE);
1481
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001482 err = drbd_recv_all_warn(mdev->tconn, data, len);
1483 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001484 break;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001485 data_size -= len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001486 }
1487 kunmap(page);
Andreas Gruenbacher5cc287e2011-04-07 21:02:59 +02001488 drbd_free_pages(mdev, page, 0);
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001489 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001490}
1491
1492static int recv_dless_read(struct drbd_conf *mdev, struct drbd_request *req,
1493 sector_t sector, int data_size)
1494{
1495 struct bio_vec *bvec;
1496 struct bio *bio;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001497 int dgs, err, i, expect;
Philipp Reisnera0638452011-01-19 14:31:32 +01001498 void *dig_in = mdev->tconn->int_dig_in;
1499 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001500
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001501 dgs = 0;
1502 if (mdev->tconn->peer_integrity_tfm) {
1503 dgs = crypto_hash_digestsize(mdev->tconn->peer_integrity_tfm);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001504 err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs);
1505 if (err)
1506 return err;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001507 data_size -= dgs;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001508 }
1509
Philipp Reisnerb411b362009-09-25 16:07:19 -07001510 /* optimistically update recv_cnt. if receiving fails below,
1511 * we disconnect anyways, and counters will be reset. */
1512 mdev->recv_cnt += data_size>>9;
1513
1514 bio = req->master_bio;
1515 D_ASSERT(sector == bio->bi_sector);
1516
1517 bio_for_each_segment(bvec, bio, i) {
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001518 void *mapped = kmap(bvec->bv_page) + bvec->bv_offset;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001519 expect = min_t(int, data_size, bvec->bv_len);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001520 err = drbd_recv_all_warn(mdev->tconn, mapped, expect);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001521 kunmap(bvec->bv_page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001522 if (err)
1523 return err;
1524 data_size -= expect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001525 }
1526
1527 if (dgs) {
Andreas Gruenbacher5b614ab2011-04-27 21:00:12 +02001528 drbd_csum_bio(mdev, mdev->tconn->peer_integrity_tfm, bio, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001529 if (memcmp(dig_in, dig_vv, dgs)) {
1530 dev_err(DEV, "Digest integrity check FAILED. Broken NICs?\n");
Andreas Gruenbacher28284ce2011-03-16 17:54:02 +01001531 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001532 }
1533 }
1534
1535 D_ASSERT(data_size == 0);
Andreas Gruenbacher28284ce2011-03-16 17:54:02 +01001536 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001537}
1538
Andreas Gruenbachera990be42011-04-06 17:56:48 +02001539/*
1540 * e_end_resync_block() is called in asender context via
1541 * drbd_finish_peer_reqs().
1542 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001543static int e_end_resync_block(struct drbd_work *w, int unused)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001544{
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001545 struct drbd_peer_request *peer_req =
1546 container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001547 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001548 sector_t sector = peer_req->i.sector;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001549 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001550
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001551 D_ASSERT(drbd_interval_empty(&peer_req->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001552
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001553 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
1554 drbd_set_in_sync(mdev, sector, peer_req->i.size);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001555 err = drbd_send_ack(mdev, P_RS_WRITE_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001556 } else {
1557 /* Record failure to sync */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001558 drbd_rs_failed_io(mdev, sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001559
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001560 err = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001561 }
1562 dec_unacked(mdev);
1563
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001564 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001565}
1566
1567static int recv_resync_read(struct drbd_conf *mdev, sector_t sector, int data_size) __releases(local)
1568{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001569 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001570
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001571 peer_req = read_in_block(mdev, ID_SYNCER, sector, data_size);
1572 if (!peer_req)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001573 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001574
1575 dec_rs_pending(mdev);
1576
Philipp Reisnerb411b362009-09-25 16:07:19 -07001577 inc_unacked(mdev);
1578 /* corresponding dec_unacked() in e_end_resync_block()
1579 * respective _drbd_clear_done_ee */
1580
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001581 peer_req->w.cb = e_end_resync_block;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001582
Philipp Reisner87eeee42011-01-19 14:16:30 +01001583 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001584 list_add(&peer_req->w.list, &mdev->sync_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001585 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001586
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001587 atomic_add(data_size >> 9, &mdev->rs_sect_ev);
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001588 if (drbd_submit_peer_request(mdev, peer_req, WRITE, DRBD_FAULT_RS_WR) == 0)
Andreas Gruenbachere1c1b0f2011-03-16 17:58:27 +01001589 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001590
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001591 /* don't care for the reason here */
1592 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01001593 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001594 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001595 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02001596
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001597 drbd_free_peer_req(mdev, peer_req);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001598fail:
1599 put_ldev(mdev);
Andreas Gruenbachere1c1b0f2011-03-16 17:58:27 +01001600 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001601}
1602
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001603static struct drbd_request *
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001604find_request(struct drbd_conf *mdev, struct rb_root *root, u64 id,
1605 sector_t sector, bool missing_ok, const char *func)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001606{
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001607 struct drbd_request *req;
1608
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001609 /* Request object according to our peer */
1610 req = (struct drbd_request *)(unsigned long)id;
Andreas Gruenbacher5e472262011-01-27 14:42:51 +01001611 if (drbd_contains_interval(root, sector, &req->i) && req->i.local)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001612 return req;
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001613 if (!missing_ok) {
Andreas Gruenbacher5af172e2011-07-15 09:43:23 +02001614 dev_err(DEV, "%s: failed to find request 0x%lx, sector %llus\n", func,
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001615 (unsigned long)id, (unsigned long long)sector);
1616 }
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001617 return NULL;
1618}
1619
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001620static int receive_DataReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001621{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001622 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001623 struct drbd_request *req;
1624 sector_t sector;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001625 int err;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001626 struct p_data *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001627
1628 mdev = vnr_to_mdev(tconn, pi->vnr);
1629 if (!mdev)
1630 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001631
1632 sector = be64_to_cpu(p->sector);
1633
Philipp Reisner87eeee42011-01-19 14:16:30 +01001634 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001635 req = find_request(mdev, &mdev->read_requests, p->block_id, sector, false, __func__);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001636 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001637 if (unlikely(!req))
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001638 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001639
Bart Van Assche24c48302011-05-21 18:32:29 +02001640 /* hlist_del(&req->collision) is done in _req_may_be_done, to avoid
Philipp Reisnerb411b362009-09-25 16:07:19 -07001641 * special casing it there for the various failure cases.
1642 * still no race with drbd_fail_pending_reads */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001643 err = recv_dless_read(mdev, req, sector, pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001644 if (!err)
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001645 req_mod(req, DATA_RECEIVED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001646 /* else: nothing. handled from drbd_disconnect...
1647 * I don't think we may complete this just yet
1648 * in case we are "on-disconnect: freeze" */
1649
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001650 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001651}
1652
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001653static int receive_RSDataReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001654{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001655 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001656 sector_t sector;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001657 int err;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001658 struct p_data *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001659
1660 mdev = vnr_to_mdev(tconn, pi->vnr);
1661 if (!mdev)
1662 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001663
1664 sector = be64_to_cpu(p->sector);
1665 D_ASSERT(p->block_id == ID_SYNCER);
1666
1667 if (get_ldev(mdev)) {
1668 /* data is submitted to disk within recv_resync_read.
1669 * corresponding put_ldev done below on error,
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01001670 * or in drbd_peer_request_endio. */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001671 err = recv_resync_read(mdev, sector, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001672 } else {
1673 if (__ratelimit(&drbd_ratelimit_state))
1674 dev_err(DEV, "Can not write resync data to local disk.\n");
1675
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001676 err = drbd_drain_block(mdev, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001677
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001678 drbd_send_ack_dp(mdev, P_NEG_ACK, p, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001679 }
1680
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001681 atomic_add(pi->size >> 9, &mdev->rs_sect_in);
Philipp Reisner778f2712010-07-06 11:14:00 +02001682
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001683 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001684}
1685
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001686static int w_restart_write(struct drbd_work *w, int cancel)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001687{
1688 struct drbd_request *req = container_of(w, struct drbd_request, w);
1689 struct drbd_conf *mdev = w->mdev;
1690 struct bio *bio;
1691 unsigned long start_time;
1692 unsigned long flags;
1693
1694 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
1695 if (!expect(req->rq_state & RQ_POSTPONED)) {
1696 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001697 return -EIO;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001698 }
1699 bio = req->master_bio;
1700 start_time = req->start_time;
1701 /* Postponed requests will not have their master_bio completed! */
1702 __req_mod(req, DISCARD_WRITE, NULL);
1703 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
1704
1705 while (__drbd_make_request(mdev, bio, start_time))
1706 /* retry */ ;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001707 return 0;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001708}
1709
1710static void restart_conflicting_writes(struct drbd_conf *mdev,
1711 sector_t sector, int size)
1712{
1713 struct drbd_interval *i;
1714 struct drbd_request *req;
1715
1716 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
1717 if (!i->local)
1718 continue;
1719 req = container_of(i, struct drbd_request, i);
1720 if (req->rq_state & RQ_LOCAL_PENDING ||
1721 !(req->rq_state & RQ_POSTPONED))
1722 continue;
1723 if (expect(list_empty(&req->w.list))) {
1724 req->w.mdev = mdev;
1725 req->w.cb = w_restart_write;
1726 drbd_queue_work(&mdev->tconn->data.work, &req->w);
1727 }
1728 }
1729}
1730
Andreas Gruenbachera990be42011-04-06 17:56:48 +02001731/*
1732 * e_end_block() is called in asender context via drbd_finish_peer_reqs().
Philipp Reisnerb411b362009-09-25 16:07:19 -07001733 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001734static int e_end_block(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001735{
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001736 struct drbd_peer_request *peer_req =
1737 container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001738 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001739 sector_t sector = peer_req->i.sector;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001740 int err = 0, pcmd;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001741
Philipp Reisner303d1442011-04-13 16:24:47 -07001742 if (peer_req->flags & EE_SEND_WRITE_ACK) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001743 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001744 pcmd = (mdev->state.conn >= C_SYNC_SOURCE &&
1745 mdev->state.conn <= C_PAUSED_SYNC_T &&
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001746 peer_req->flags & EE_MAY_SET_IN_SYNC) ?
Philipp Reisnerb411b362009-09-25 16:07:19 -07001747 P_RS_WRITE_ACK : P_WRITE_ACK;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001748 err = drbd_send_ack(mdev, pcmd, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001749 if (pcmd == P_RS_WRITE_ACK)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001750 drbd_set_in_sync(mdev, sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001751 } else {
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001752 err = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001753 /* we expect it to be marked out of sync anyways...
1754 * maybe assert this? */
1755 }
1756 dec_unacked(mdev);
1757 }
1758 /* we delete from the conflict detection hash _after_ we sent out the
1759 * P_WRITE_ACK / P_NEG_ACK, to get the sequence number right. */
Philipp Reisner302bdea2011-04-21 11:36:49 +02001760 if (peer_req->flags & EE_IN_INTERVAL_TREE) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01001761 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001762 D_ASSERT(!drbd_interval_empty(&peer_req->i));
1763 drbd_remove_epoch_entry_interval(mdev, peer_req);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001764 if (peer_req->flags & EE_RESTART_REQUESTS)
1765 restart_conflicting_writes(mdev, sector, peer_req->i.size);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001766 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbb3bfe92011-01-21 15:59:23 +01001767 } else
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001768 D_ASSERT(drbd_interval_empty(&peer_req->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001769
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001770 drbd_may_finish_epoch(mdev, peer_req->epoch, EV_PUT + (cancel ? EV_CLEANUP : 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001771
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001772 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001773}
1774
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001775static int e_send_ack(struct drbd_work *w, enum drbd_packet ack)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001776{
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001777 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001778 struct drbd_peer_request *peer_req =
1779 container_of(w, struct drbd_peer_request, w);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001780 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001781
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001782 err = drbd_send_ack(mdev, ack, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001783 dec_unacked(mdev);
1784
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001785 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001786}
1787
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001788static int e_send_discard_write(struct drbd_work *w, int unused)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001789{
1790 return e_send_ack(w, P_DISCARD_WRITE);
1791}
1792
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001793static int e_send_retry_write(struct drbd_work *w, int unused)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001794{
1795 struct drbd_tconn *tconn = w->mdev->tconn;
1796
1797 return e_send_ack(w, tconn->agreed_pro_version >= 100 ?
1798 P_RETRY_WRITE : P_DISCARD_WRITE);
1799}
1800
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001801static bool seq_greater(u32 a, u32 b)
1802{
1803 /*
1804 * We assume 32-bit wrap-around here.
1805 * For 24-bit wrap-around, we would have to shift:
1806 * a <<= 8; b <<= 8;
1807 */
1808 return (s32)a - (s32)b > 0;
1809}
1810
1811static u32 seq_max(u32 a, u32 b)
1812{
1813 return seq_greater(a, b) ? a : b;
1814}
1815
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001816static bool need_peer_seq(struct drbd_conf *mdev)
1817{
1818 struct drbd_tconn *tconn = mdev->tconn;
Philipp Reisner302bdea2011-04-21 11:36:49 +02001819 int tp;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001820
1821 /*
1822 * We only need to keep track of the last packet_seq number of our peer
1823 * if we are in dual-primary mode and we have the discard flag set; see
1824 * handle_write_conflicts().
1825 */
Philipp Reisner302bdea2011-04-21 11:36:49 +02001826
1827 rcu_read_lock();
1828 tp = rcu_dereference(mdev->tconn->net_conf)->two_primaries;
1829 rcu_read_unlock();
1830
1831 return tp && test_bit(DISCARD_CONCURRENT, &tconn->flags);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001832}
1833
Andreas Gruenbacher43ae0772011-02-03 18:42:08 +01001834static void update_peer_seq(struct drbd_conf *mdev, unsigned int peer_seq)
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001835{
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001836 unsigned int newest_peer_seq;
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001837
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001838 if (need_peer_seq(mdev)) {
1839 spin_lock(&mdev->peer_seq_lock);
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001840 newest_peer_seq = seq_max(mdev->peer_seq, peer_seq);
1841 mdev->peer_seq = newest_peer_seq;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001842 spin_unlock(&mdev->peer_seq_lock);
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001843 /* wake up only if we actually changed mdev->peer_seq */
1844 if (peer_seq == newest_peer_seq)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001845 wake_up(&mdev->seq_wait);
1846 }
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001847}
1848
Philipp Reisnerb411b362009-09-25 16:07:19 -07001849/* Called from receive_Data.
1850 * Synchronize packets on sock with packets on msock.
1851 *
1852 * This is here so even when a P_DATA packet traveling via sock overtook an Ack
1853 * packet traveling on msock, they are still processed in the order they have
1854 * been sent.
1855 *
1856 * Note: we don't care for Ack packets overtaking P_DATA packets.
1857 *
1858 * In case packet_seq is larger than mdev->peer_seq number, there are
1859 * outstanding packets on the msock. We wait for them to arrive.
1860 * In case we are the logically next packet, we update mdev->peer_seq
1861 * ourselves. Correctly handles 32bit wrap around.
1862 *
1863 * Assume we have a 10 GBit connection, that is about 1<<30 byte per second,
1864 * about 1<<21 sectors per second. So "worst" case, we have 1<<3 == 8 seconds
1865 * for the 24bit wrap (historical atomic_t guarantee on some archs), and we have
1866 * 1<<9 == 512 seconds aka ages for the 32bit wrap around...
1867 *
1868 * returns 0 if we may process the packet,
1869 * -ERESTARTSYS if we were interrupted (by disconnect signal). */
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001870static int wait_for_and_update_peer_seq(struct drbd_conf *mdev, const u32 peer_seq)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001871{
1872 DEFINE_WAIT(wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001873 long timeout;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001874 int ret;
1875
1876 if (!need_peer_seq(mdev))
1877 return 0;
1878
Philipp Reisnerb411b362009-09-25 16:07:19 -07001879 spin_lock(&mdev->peer_seq_lock);
1880 for (;;) {
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001881 if (!seq_greater(peer_seq - 1, mdev->peer_seq)) {
1882 mdev->peer_seq = seq_max(mdev->peer_seq, peer_seq);
1883 ret = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001884 break;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001885 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001886 if (signal_pending(current)) {
1887 ret = -ERESTARTSYS;
1888 break;
1889 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001890 prepare_to_wait(&mdev->seq_wait, &wait, TASK_INTERRUPTIBLE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001891 spin_unlock(&mdev->peer_seq_lock);
Philipp Reisner44ed1672011-04-19 17:10:19 +02001892 rcu_read_lock();
1893 timeout = rcu_dereference(mdev->tconn->net_conf)->ping_timeo*HZ/10;
1894 rcu_read_unlock();
Andreas Gruenbacher71b1c1e2011-03-01 15:40:43 +01001895 timeout = schedule_timeout(timeout);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001896 spin_lock(&mdev->peer_seq_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001897 if (!timeout) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001898 ret = -ETIMEDOUT;
Andreas Gruenbacher71b1c1e2011-03-01 15:40:43 +01001899 dev_err(DEV, "Timed out waiting for missing ack packets; disconnecting\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001900 break;
1901 }
1902 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001903 spin_unlock(&mdev->peer_seq_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001904 finish_wait(&mdev->seq_wait, &wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001905 return ret;
1906}
1907
Lars Ellenberg688593c2010-11-17 22:25:03 +01001908/* see also bio_flags_to_wire()
1909 * DRBD_REQ_*, because we need to semantically map the flags to data packet
1910 * flags and back. We may replicate to other kernel versions. */
1911static unsigned long wire_flags_to_bio(struct drbd_conf *mdev, u32 dpf)
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001912{
Lars Ellenberg688593c2010-11-17 22:25:03 +01001913 return (dpf & DP_RW_SYNC ? REQ_SYNC : 0) |
1914 (dpf & DP_FUA ? REQ_FUA : 0) |
1915 (dpf & DP_FLUSH ? REQ_FLUSH : 0) |
1916 (dpf & DP_DISCARD ? REQ_DISCARD : 0);
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001917}
1918
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001919static void fail_postponed_requests(struct drbd_conf *mdev, sector_t sector,
1920 unsigned int size)
1921{
1922 struct drbd_interval *i;
1923
1924 repeat:
1925 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
1926 struct drbd_request *req;
1927 struct bio_and_error m;
1928
1929 if (!i->local)
1930 continue;
1931 req = container_of(i, struct drbd_request, i);
1932 if (!(req->rq_state & RQ_POSTPONED))
1933 continue;
1934 req->rq_state &= ~RQ_POSTPONED;
1935 __req_mod(req, NEG_ACKED, &m);
1936 spin_unlock_irq(&mdev->tconn->req_lock);
1937 if (m.bio)
1938 complete_master_bio(mdev, &m);
1939 spin_lock_irq(&mdev->tconn->req_lock);
1940 goto repeat;
1941 }
1942}
1943
1944static int handle_write_conflicts(struct drbd_conf *mdev,
1945 struct drbd_peer_request *peer_req)
1946{
1947 struct drbd_tconn *tconn = mdev->tconn;
1948 bool resolve_conflicts = test_bit(DISCARD_CONCURRENT, &tconn->flags);
1949 sector_t sector = peer_req->i.sector;
1950 const unsigned int size = peer_req->i.size;
1951 struct drbd_interval *i;
1952 bool equal;
1953 int err;
1954
1955 /*
1956 * Inserting the peer request into the write_requests tree will prevent
1957 * new conflicting local requests from being added.
1958 */
1959 drbd_insert_interval(&mdev->write_requests, &peer_req->i);
1960
1961 repeat:
1962 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
1963 if (i == &peer_req->i)
1964 continue;
1965
1966 if (!i->local) {
1967 /*
1968 * Our peer has sent a conflicting remote request; this
1969 * should not happen in a two-node setup. Wait for the
1970 * earlier peer request to complete.
1971 */
1972 err = drbd_wait_misc(mdev, i);
1973 if (err)
1974 goto out;
1975 goto repeat;
1976 }
1977
1978 equal = i->sector == sector && i->size == size;
1979 if (resolve_conflicts) {
1980 /*
1981 * If the peer request is fully contained within the
1982 * overlapping request, it can be discarded; otherwise,
1983 * it will be retried once all overlapping requests
1984 * have completed.
1985 */
1986 bool discard = i->sector <= sector && i->sector +
1987 (i->size >> 9) >= sector + (size >> 9);
1988
1989 if (!equal)
1990 dev_alert(DEV, "Concurrent writes detected: "
1991 "local=%llus +%u, remote=%llus +%u, "
1992 "assuming %s came first\n",
1993 (unsigned long long)i->sector, i->size,
1994 (unsigned long long)sector, size,
1995 discard ? "local" : "remote");
1996
1997 inc_unacked(mdev);
1998 peer_req->w.cb = discard ? e_send_discard_write :
1999 e_send_retry_write;
2000 list_add_tail(&peer_req->w.list, &mdev->done_ee);
2001 wake_asender(mdev->tconn);
2002
2003 err = -ENOENT;
2004 goto out;
2005 } else {
2006 struct drbd_request *req =
2007 container_of(i, struct drbd_request, i);
2008
2009 if (!equal)
2010 dev_alert(DEV, "Concurrent writes detected: "
2011 "local=%llus +%u, remote=%llus +%u\n",
2012 (unsigned long long)i->sector, i->size,
2013 (unsigned long long)sector, size);
2014
2015 if (req->rq_state & RQ_LOCAL_PENDING ||
2016 !(req->rq_state & RQ_POSTPONED)) {
2017 /*
2018 * Wait for the node with the discard flag to
2019 * decide if this request will be discarded or
2020 * retried. Requests that are discarded will
2021 * disappear from the write_requests tree.
2022 *
2023 * In addition, wait for the conflicting
2024 * request to finish locally before submitting
2025 * the conflicting peer request.
2026 */
2027 err = drbd_wait_misc(mdev, &req->i);
2028 if (err) {
2029 _conn_request_state(mdev->tconn,
2030 NS(conn, C_TIMEOUT),
2031 CS_HARD);
2032 fail_postponed_requests(mdev, sector, size);
2033 goto out;
2034 }
2035 goto repeat;
2036 }
2037 /*
2038 * Remember to restart the conflicting requests after
2039 * the new peer request has completed.
2040 */
2041 peer_req->flags |= EE_RESTART_REQUESTS;
2042 }
2043 }
2044 err = 0;
2045
2046 out:
2047 if (err)
2048 drbd_remove_epoch_entry_interval(mdev, peer_req);
2049 return err;
2050}
2051
Philipp Reisnerb411b362009-09-25 16:07:19 -07002052/* mirrored write */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002053static int receive_Data(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002054{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002055 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002056 sector_t sector;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002057 struct drbd_peer_request *peer_req;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02002058 struct p_data *p = pi->data;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002059 u32 peer_seq = be32_to_cpu(p->seq_num);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002060 int rw = WRITE;
2061 u32 dp_flags;
Philipp Reisner302bdea2011-04-21 11:36:49 +02002062 int err, tp;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002063
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002064 mdev = vnr_to_mdev(tconn, pi->vnr);
2065 if (!mdev)
2066 return -EIO;
2067
Philipp Reisnerb411b362009-09-25 16:07:19 -07002068 if (!get_ldev(mdev)) {
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002069 int err2;
2070
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002071 err = wait_for_and_update_peer_seq(mdev, peer_seq);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002072 drbd_send_ack_dp(mdev, P_NEG_ACK, p, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002073 atomic_inc(&mdev->current_epoch->epoch_size);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002074 err2 = drbd_drain_block(mdev, pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002075 if (!err)
2076 err = err2;
2077 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002078 }
2079
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01002080 /*
2081 * Corresponding put_ldev done either below (on various errors), or in
2082 * drbd_peer_request_endio, if we successfully submit the data at the
2083 * end of this function.
2084 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002085
2086 sector = be64_to_cpu(p->sector);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002087 peer_req = read_in_block(mdev, p->block_id, sector, pi->size);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002088 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002089 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002090 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002091 }
2092
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002093 peer_req->w.cb = e_end_block;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002094
Lars Ellenberg688593c2010-11-17 22:25:03 +01002095 dp_flags = be32_to_cpu(p->dp_flags);
2096 rw |= wire_flags_to_bio(mdev, dp_flags);
2097
2098 if (dp_flags & DP_MAY_SET_IN_SYNC)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002099 peer_req->flags |= EE_MAY_SET_IN_SYNC;
Lars Ellenberg688593c2010-11-17 22:25:03 +01002100
Philipp Reisnerb411b362009-09-25 16:07:19 -07002101 spin_lock(&mdev->epoch_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002102 peer_req->epoch = mdev->current_epoch;
2103 atomic_inc(&peer_req->epoch->epoch_size);
2104 atomic_inc(&peer_req->epoch->active);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002105 spin_unlock(&mdev->epoch_lock);
2106
Philipp Reisner302bdea2011-04-21 11:36:49 +02002107 rcu_read_lock();
2108 tp = rcu_dereference(mdev->tconn->net_conf)->two_primaries;
2109 rcu_read_unlock();
2110 if (tp) {
2111 peer_req->flags |= EE_IN_INTERVAL_TREE;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002112 err = wait_for_and_update_peer_seq(mdev, peer_seq);
2113 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002114 goto out_interrupted;
Philipp Reisner87eeee42011-01-19 14:16:30 +01002115 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002116 err = handle_write_conflicts(mdev, peer_req);
2117 if (err) {
2118 spin_unlock_irq(&mdev->tconn->req_lock);
2119 if (err == -ENOENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002120 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002121 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002122 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002123 goto out_interrupted;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002124 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002125 } else
2126 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002127 list_add(&peer_req->w.list, &mdev->active_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002128 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002129
Philipp Reisner303d1442011-04-13 16:24:47 -07002130 if (mdev->tconn->agreed_pro_version < 100) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02002131 rcu_read_lock();
2132 switch (rcu_dereference(mdev->tconn->net_conf)->wire_protocol) {
Philipp Reisner303d1442011-04-13 16:24:47 -07002133 case DRBD_PROT_C:
2134 dp_flags |= DP_SEND_WRITE_ACK;
2135 break;
2136 case DRBD_PROT_B:
2137 dp_flags |= DP_SEND_RECEIVE_ACK;
2138 break;
2139 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002140 rcu_read_unlock();
Philipp Reisner303d1442011-04-13 16:24:47 -07002141 }
2142
2143 if (dp_flags & DP_SEND_WRITE_ACK) {
2144 peer_req->flags |= EE_SEND_WRITE_ACK;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002145 inc_unacked(mdev);
2146 /* corresponding dec_unacked() in e_end_block()
2147 * respective _drbd_clear_done_ee */
Philipp Reisner303d1442011-04-13 16:24:47 -07002148 }
2149
2150 if (dp_flags & DP_SEND_RECEIVE_ACK) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002151 /* I really don't like it that the receiver thread
2152 * sends on the msock, but anyways */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002153 drbd_send_ack(mdev, P_RECV_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002154 }
2155
Lars Ellenberg6719fb02010-10-18 23:04:07 +02002156 if (mdev->state.pdsk < D_INCONSISTENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002157 /* In case we have the only disk of the cluster, */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002158 drbd_set_out_of_sync(mdev, peer_req->i.sector, peer_req->i.size);
2159 peer_req->flags |= EE_CALL_AL_COMPLETE_IO;
2160 peer_req->flags &= ~EE_MAY_SET_IN_SYNC;
Lars Ellenberg181286a2011-03-31 15:18:56 +02002161 drbd_al_begin_io(mdev, &peer_req->i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002162 }
2163
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002164 err = drbd_submit_peer_request(mdev, peer_req, rw, DRBD_FAULT_DT_WR);
2165 if (!err)
2166 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002167
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01002168 /* don't care for the reason here */
2169 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002170 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002171 list_del(&peer_req->w.list);
2172 drbd_remove_epoch_entry_interval(mdev, peer_req);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002173 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002174 if (peer_req->flags & EE_CALL_AL_COMPLETE_IO)
Lars Ellenberg181286a2011-03-31 15:18:56 +02002175 drbd_al_complete_io(mdev, &peer_req->i);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002176
Philipp Reisnerb411b362009-09-25 16:07:19 -07002177out_interrupted:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002178 drbd_may_finish_epoch(mdev, peer_req->epoch, EV_PUT + EV_CLEANUP);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002179 put_ldev(mdev);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02002180 drbd_free_peer_req(mdev, peer_req);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002181 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002182}
2183
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002184/* We may throttle resync, if the lower device seems to be busy,
2185 * and current sync rate is above c_min_rate.
2186 *
2187 * To decide whether or not the lower device is busy, we use a scheme similar
2188 * to MD RAID is_mddev_idle(): if the partition stats reveal "significant"
2189 * (more than 64 sectors) of activity we cannot account for with our own resync
2190 * activity, it obviously is "busy".
2191 *
2192 * The current sync rate used here uses only the most recent two step marks,
2193 * to have a short time average so we can react faster.
2194 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002195int drbd_rs_should_slow_down(struct drbd_conf *mdev, sector_t sector)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002196{
2197 struct gendisk *disk = mdev->ldev->backing_bdev->bd_contains->bd_disk;
2198 unsigned long db, dt, dbdt;
Philipp Reisnere3555d82010-11-07 15:56:29 +01002199 struct lc_element *tmp;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002200 int curr_events;
2201 int throttle = 0;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002202 unsigned int c_min_rate;
2203
2204 rcu_read_lock();
2205 c_min_rate = rcu_dereference(mdev->ldev->disk_conf)->c_min_rate;
2206 rcu_read_unlock();
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002207
2208 /* feature disabled? */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002209 if (c_min_rate == 0)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002210 return 0;
2211
Philipp Reisnere3555d82010-11-07 15:56:29 +01002212 spin_lock_irq(&mdev->al_lock);
2213 tmp = lc_find(mdev->resync, BM_SECT_TO_EXT(sector));
2214 if (tmp) {
2215 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
2216 if (test_bit(BME_PRIORITY, &bm_ext->flags)) {
2217 spin_unlock_irq(&mdev->al_lock);
2218 return 0;
2219 }
2220 /* Do not slow down if app IO is already waiting for this extent */
2221 }
2222 spin_unlock_irq(&mdev->al_lock);
2223
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002224 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
2225 (int)part_stat_read(&disk->part0, sectors[1]) -
2226 atomic_read(&mdev->rs_sect_ev);
Philipp Reisnere3555d82010-11-07 15:56:29 +01002227
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002228 if (!mdev->rs_last_events || curr_events - mdev->rs_last_events > 64) {
2229 unsigned long rs_left;
2230 int i;
2231
2232 mdev->rs_last_events = curr_events;
2233
2234 /* sync speed average over the last 2*DRBD_SYNC_MARK_STEP,
2235 * approx. */
Lars Ellenberg2649f082010-11-05 10:05:47 +01002236 i = (mdev->rs_last_mark + DRBD_SYNC_MARKS-1) % DRBD_SYNC_MARKS;
2237
2238 if (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T)
2239 rs_left = mdev->ov_left;
2240 else
2241 rs_left = drbd_bm_total_weight(mdev) - mdev->rs_failed;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002242
2243 dt = ((long)jiffies - (long)mdev->rs_mark_time[i]) / HZ;
2244 if (!dt)
2245 dt++;
2246 db = mdev->rs_mark_left[i] - rs_left;
2247 dbdt = Bit2KB(db/dt);
2248
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002249 if (dbdt > c_min_rate)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002250 throttle = 1;
2251 }
2252 return throttle;
2253}
2254
2255
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002256static int receive_DataRequest(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002257{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002258 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002259 sector_t sector;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002260 sector_t capacity;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002261 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002262 struct digest_info *di = NULL;
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002263 int size, verb;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002264 unsigned int fault_type;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02002265 struct p_block_req *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002266
2267 mdev = vnr_to_mdev(tconn, pi->vnr);
2268 if (!mdev)
2269 return -EIO;
2270 capacity = drbd_get_capacity(mdev->this_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002271
2272 sector = be64_to_cpu(p->sector);
2273 size = be32_to_cpu(p->blksize);
2274
Andreas Gruenbacherc670a392011-02-21 12:41:39 +01002275 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002276 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2277 (unsigned long long)sector, size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002278 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002279 }
2280 if (sector + (size>>9) > capacity) {
2281 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2282 (unsigned long long)sector, size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002283 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002284 }
2285
2286 if (!get_ldev_if_state(mdev, D_UP_TO_DATE)) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002287 verb = 1;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002288 switch (pi->cmd) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002289 case P_DATA_REQUEST:
2290 drbd_send_ack_rp(mdev, P_NEG_DREPLY, p);
2291 break;
2292 case P_RS_DATA_REQUEST:
2293 case P_CSUM_RS_REQUEST:
2294 case P_OV_REQUEST:
2295 drbd_send_ack_rp(mdev, P_NEG_RS_DREPLY , p);
2296 break;
2297 case P_OV_REPLY:
2298 verb = 0;
2299 dec_rs_pending(mdev);
2300 drbd_send_ack_ex(mdev, P_OV_RESULT, sector, size, ID_IN_SYNC);
2301 break;
2302 default:
Andreas Gruenbacher49ba9b12011-03-25 00:35:45 +01002303 BUG();
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002304 }
2305 if (verb && __ratelimit(&drbd_ratelimit_state))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002306 dev_err(DEV, "Can not satisfy peer's read request, "
2307 "no local data.\n");
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002308
Lars Ellenberga821cc42010-09-06 12:31:37 +02002309 /* drain possibly payload */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002310 return drbd_drain_block(mdev, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002311 }
2312
2313 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
2314 * "criss-cross" setup, that might cause write-out on some other DRBD,
2315 * which in turn might block on the other node at this very place. */
Andreas Gruenbacher0db55362011-04-06 16:09:15 +02002316 peer_req = drbd_alloc_peer_req(mdev, p->block_id, sector, size, GFP_NOIO);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002317 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002318 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002319 return -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002320 }
2321
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002322 switch (pi->cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002323 case P_DATA_REQUEST:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002324 peer_req->w.cb = w_e_end_data_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002325 fault_type = DRBD_FAULT_DT_RD;
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002326 /* application IO, don't drbd_rs_begin_io */
2327 goto submit;
2328
Philipp Reisnerb411b362009-09-25 16:07:19 -07002329 case P_RS_DATA_REQUEST:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002330 peer_req->w.cb = w_e_end_rsdata_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002331 fault_type = DRBD_FAULT_RS_RD;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002332 /* used in the sector offset progress display */
2333 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002334 break;
2335
2336 case P_OV_REPLY:
2337 case P_CSUM_RS_REQUEST:
2338 fault_type = DRBD_FAULT_RS_RD;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002339 di = kmalloc(sizeof(*di) + pi->size, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002340 if (!di)
2341 goto out_free_e;
2342
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002343 di->digest_size = pi->size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002344 di->digest = (((char *)di)+sizeof(struct digest_info));
2345
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002346 peer_req->digest = di;
2347 peer_req->flags |= EE_HAS_DIGEST;
Lars Ellenbergc36c3ce2010-08-11 20:42:55 +02002348
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002349 if (drbd_recv_all(mdev->tconn, di->digest, pi->size))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002350 goto out_free_e;
2351
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002352 if (pi->cmd == P_CSUM_RS_REQUEST) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002353 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002354 peer_req->w.cb = w_e_end_csum_rs_req;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002355 /* used in the sector offset progress display */
2356 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002357 } else if (pi->cmd == P_OV_REPLY) {
Lars Ellenberg2649f082010-11-05 10:05:47 +01002358 /* track progress, we may need to throttle */
2359 atomic_add(size >> 9, &mdev->rs_sect_in);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002360 peer_req->w.cb = w_e_end_ov_reply;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002361 dec_rs_pending(mdev);
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002362 /* drbd_rs_begin_io done when we sent this request,
2363 * but accounting still needs to be done. */
2364 goto submit_for_resync;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002365 }
2366 break;
2367
2368 case P_OV_REQUEST:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002369 if (mdev->ov_start_sector == ~(sector_t)0 &&
Philipp Reisner31890f42011-01-19 14:12:51 +01002370 mdev->tconn->agreed_pro_version >= 90) {
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002371 unsigned long now = jiffies;
2372 int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002373 mdev->ov_start_sector = sector;
2374 mdev->ov_position = sector;
Lars Ellenberg30b743a2010-11-05 09:39:06 +01002375 mdev->ov_left = drbd_bm_bits(mdev) - BM_SECT_TO_BIT(sector);
2376 mdev->rs_total = mdev->ov_left;
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002377 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
2378 mdev->rs_mark_left[i] = mdev->ov_left;
2379 mdev->rs_mark_time[i] = now;
2380 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002381 dev_info(DEV, "Online Verify start sector: %llu\n",
2382 (unsigned long long)sector);
2383 }
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002384 peer_req->w.cb = w_e_end_ov_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002385 fault_type = DRBD_FAULT_RS_RD;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002386 break;
2387
Philipp Reisnerb411b362009-09-25 16:07:19 -07002388 default:
Andreas Gruenbacher49ba9b12011-03-25 00:35:45 +01002389 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002390 }
2391
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002392 /* Throttle, drbd_rs_begin_io and submit should become asynchronous
2393 * wrt the receiver, but it is not as straightforward as it may seem.
2394 * Various places in the resync start and stop logic assume resync
2395 * requests are processed in order, requeuing this on the worker thread
2396 * introduces a bunch of new code for synchronization between threads.
2397 *
2398 * Unlimited throttling before drbd_rs_begin_io may stall the resync
2399 * "forever", throttling after drbd_rs_begin_io will lock that extent
2400 * for application writes for the same time. For now, just throttle
2401 * here, where the rest of the code expects the receiver to sleep for
2402 * a while, anyways.
2403 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002404
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002405 /* Throttle before drbd_rs_begin_io, as that locks out application IO;
2406 * this defers syncer requests for some time, before letting at least
2407 * on request through. The resync controller on the receiving side
2408 * will adapt to the incoming rate accordingly.
2409 *
2410 * We cannot throttle here if remote is Primary/SyncTarget:
2411 * we would also throttle its application reads.
2412 * In that case, throttling is done on the SyncTarget only.
2413 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002414 if (mdev->state.peer != R_PRIMARY && drbd_rs_should_slow_down(mdev, sector))
2415 schedule_timeout_uninterruptible(HZ/10);
2416 if (drbd_rs_begin_io(mdev, sector))
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002417 goto out_free_e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002418
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002419submit_for_resync:
2420 atomic_add(size >> 9, &mdev->rs_sect_ev);
2421
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002422submit:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002423 inc_unacked(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002424 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002425 list_add_tail(&peer_req->w.list, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002426 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002427
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01002428 if (drbd_submit_peer_request(mdev, peer_req, READ, fault_type) == 0)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002429 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002430
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01002431 /* don't care for the reason here */
2432 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002433 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002434 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002435 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002436 /* no drbd_rs_complete_io(), we are dropping the connection anyways */
2437
Philipp Reisnerb411b362009-09-25 16:07:19 -07002438out_free_e:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002439 put_ldev(mdev);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02002440 drbd_free_peer_req(mdev, peer_req);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002441 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002442}
2443
2444static int drbd_asb_recover_0p(struct drbd_conf *mdev) __must_hold(local)
2445{
2446 int self, peer, rv = -100;
2447 unsigned long ch_self, ch_peer;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002448 enum drbd_after_sb_p after_sb_0p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002449
2450 self = mdev->ldev->md.uuid[UI_BITMAP] & 1;
2451 peer = mdev->p_uuid[UI_BITMAP] & 1;
2452
2453 ch_peer = mdev->p_uuid[UI_SIZE];
2454 ch_self = mdev->comm_bm_set;
2455
Philipp Reisner44ed1672011-04-19 17:10:19 +02002456 rcu_read_lock();
2457 after_sb_0p = rcu_dereference(mdev->tconn->net_conf)->after_sb_0p;
2458 rcu_read_unlock();
2459 switch (after_sb_0p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002460 case ASB_CONSENSUS:
2461 case ASB_DISCARD_SECONDARY:
2462 case ASB_CALL_HELPER:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002463 case ASB_VIOLENTLY:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002464 dev_err(DEV, "Configuration error.\n");
2465 break;
2466 case ASB_DISCONNECT:
2467 break;
2468 case ASB_DISCARD_YOUNGER_PRI:
2469 if (self == 0 && peer == 1) {
2470 rv = -1;
2471 break;
2472 }
2473 if (self == 1 && peer == 0) {
2474 rv = 1;
2475 break;
2476 }
2477 /* Else fall through to one of the other strategies... */
2478 case ASB_DISCARD_OLDER_PRI:
2479 if (self == 0 && peer == 1) {
2480 rv = 1;
2481 break;
2482 }
2483 if (self == 1 && peer == 0) {
2484 rv = -1;
2485 break;
2486 }
2487 /* Else fall through to one of the other strategies... */
Lars Ellenbergad19bf62009-10-14 09:36:49 +02002488 dev_warn(DEV, "Discard younger/older primary did not find a decision\n"
Philipp Reisnerb411b362009-09-25 16:07:19 -07002489 "Using discard-least-changes instead\n");
2490 case ASB_DISCARD_ZERO_CHG:
2491 if (ch_peer == 0 && ch_self == 0) {
Philipp Reisner25703f82011-02-07 14:35:25 +01002492 rv = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002493 ? -1 : 1;
2494 break;
2495 } else {
2496 if (ch_peer == 0) { rv = 1; break; }
2497 if (ch_self == 0) { rv = -1; break; }
2498 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002499 if (after_sb_0p == ASB_DISCARD_ZERO_CHG)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002500 break;
2501 case ASB_DISCARD_LEAST_CHG:
2502 if (ch_self < ch_peer)
2503 rv = -1;
2504 else if (ch_self > ch_peer)
2505 rv = 1;
2506 else /* ( ch_self == ch_peer ) */
2507 /* Well, then use something else. */
Philipp Reisner25703f82011-02-07 14:35:25 +01002508 rv = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002509 ? -1 : 1;
2510 break;
2511 case ASB_DISCARD_LOCAL:
2512 rv = -1;
2513 break;
2514 case ASB_DISCARD_REMOTE:
2515 rv = 1;
2516 }
2517
2518 return rv;
2519}
2520
2521static int drbd_asb_recover_1p(struct drbd_conf *mdev) __must_hold(local)
2522{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002523 int hg, rv = -100;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002524 enum drbd_after_sb_p after_sb_1p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002525
Philipp Reisner44ed1672011-04-19 17:10:19 +02002526 rcu_read_lock();
2527 after_sb_1p = rcu_dereference(mdev->tconn->net_conf)->after_sb_1p;
2528 rcu_read_unlock();
2529 switch (after_sb_1p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002530 case ASB_DISCARD_YOUNGER_PRI:
2531 case ASB_DISCARD_OLDER_PRI:
2532 case ASB_DISCARD_LEAST_CHG:
2533 case ASB_DISCARD_LOCAL:
2534 case ASB_DISCARD_REMOTE:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002535 case ASB_DISCARD_ZERO_CHG:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002536 dev_err(DEV, "Configuration error.\n");
2537 break;
2538 case ASB_DISCONNECT:
2539 break;
2540 case ASB_CONSENSUS:
2541 hg = drbd_asb_recover_0p(mdev);
2542 if (hg == -1 && mdev->state.role == R_SECONDARY)
2543 rv = hg;
2544 if (hg == 1 && mdev->state.role == R_PRIMARY)
2545 rv = hg;
2546 break;
2547 case ASB_VIOLENTLY:
2548 rv = drbd_asb_recover_0p(mdev);
2549 break;
2550 case ASB_DISCARD_SECONDARY:
2551 return mdev->state.role == R_PRIMARY ? 1 : -1;
2552 case ASB_CALL_HELPER:
2553 hg = drbd_asb_recover_0p(mdev);
2554 if (hg == -1 && mdev->state.role == R_PRIMARY) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002555 enum drbd_state_rv rv2;
2556
2557 drbd_set_role(mdev, R_SECONDARY, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002558 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2559 * we might be here in C_WF_REPORT_PARAMS which is transient.
2560 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002561 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2562 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002563 drbd_khelper(mdev, "pri-lost-after-sb");
2564 } else {
2565 dev_warn(DEV, "Successfully gave up primary role.\n");
2566 rv = hg;
2567 }
2568 } else
2569 rv = hg;
2570 }
2571
2572 return rv;
2573}
2574
2575static int drbd_asb_recover_2p(struct drbd_conf *mdev) __must_hold(local)
2576{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002577 int hg, rv = -100;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002578 enum drbd_after_sb_p after_sb_2p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002579
Philipp Reisner44ed1672011-04-19 17:10:19 +02002580 rcu_read_lock();
2581 after_sb_2p = rcu_dereference(mdev->tconn->net_conf)->after_sb_2p;
2582 rcu_read_unlock();
2583 switch (after_sb_2p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002584 case ASB_DISCARD_YOUNGER_PRI:
2585 case ASB_DISCARD_OLDER_PRI:
2586 case ASB_DISCARD_LEAST_CHG:
2587 case ASB_DISCARD_LOCAL:
2588 case ASB_DISCARD_REMOTE:
2589 case ASB_CONSENSUS:
2590 case ASB_DISCARD_SECONDARY:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002591 case ASB_DISCARD_ZERO_CHG:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002592 dev_err(DEV, "Configuration error.\n");
2593 break;
2594 case ASB_VIOLENTLY:
2595 rv = drbd_asb_recover_0p(mdev);
2596 break;
2597 case ASB_DISCONNECT:
2598 break;
2599 case ASB_CALL_HELPER:
2600 hg = drbd_asb_recover_0p(mdev);
2601 if (hg == -1) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002602 enum drbd_state_rv rv2;
2603
Philipp Reisnerb411b362009-09-25 16:07:19 -07002604 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2605 * we might be here in C_WF_REPORT_PARAMS which is transient.
2606 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002607 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2608 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002609 drbd_khelper(mdev, "pri-lost-after-sb");
2610 } else {
2611 dev_warn(DEV, "Successfully gave up primary role.\n");
2612 rv = hg;
2613 }
2614 } else
2615 rv = hg;
2616 }
2617
2618 return rv;
2619}
2620
2621static void drbd_uuid_dump(struct drbd_conf *mdev, char *text, u64 *uuid,
2622 u64 bits, u64 flags)
2623{
2624 if (!uuid) {
2625 dev_info(DEV, "%s uuid info vanished while I was looking!\n", text);
2626 return;
2627 }
2628 dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX bits:%llu flags:%llX\n",
2629 text,
2630 (unsigned long long)uuid[UI_CURRENT],
2631 (unsigned long long)uuid[UI_BITMAP],
2632 (unsigned long long)uuid[UI_HISTORY_START],
2633 (unsigned long long)uuid[UI_HISTORY_END],
2634 (unsigned long long)bits,
2635 (unsigned long long)flags);
2636}
2637
2638/*
2639 100 after split brain try auto recover
2640 2 C_SYNC_SOURCE set BitMap
2641 1 C_SYNC_SOURCE use BitMap
2642 0 no Sync
2643 -1 C_SYNC_TARGET use BitMap
2644 -2 C_SYNC_TARGET set BitMap
2645 -100 after split brain, disconnect
2646-1000 unrelated data
Philipp Reisner4a23f262011-01-11 17:42:17 +01002647-1091 requires proto 91
2648-1096 requires proto 96
Philipp Reisnerb411b362009-09-25 16:07:19 -07002649 */
2650static int drbd_uuid_compare(struct drbd_conf *mdev, int *rule_nr) __must_hold(local)
2651{
2652 u64 self, peer;
2653 int i, j;
2654
2655 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2656 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2657
2658 *rule_nr = 10;
2659 if (self == UUID_JUST_CREATED && peer == UUID_JUST_CREATED)
2660 return 0;
2661
2662 *rule_nr = 20;
2663 if ((self == UUID_JUST_CREATED || self == (u64)0) &&
2664 peer != UUID_JUST_CREATED)
2665 return -2;
2666
2667 *rule_nr = 30;
2668 if (self != UUID_JUST_CREATED &&
2669 (peer == UUID_JUST_CREATED || peer == (u64)0))
2670 return 2;
2671
2672 if (self == peer) {
2673 int rct, dc; /* roles at crash time */
2674
2675 if (mdev->p_uuid[UI_BITMAP] == (u64)0 && mdev->ldev->md.uuid[UI_BITMAP] != (u64)0) {
2676
Philipp Reisner31890f42011-01-19 14:12:51 +01002677 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002678 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002679
2680 if ((mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) &&
2681 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1))) {
2682 dev_info(DEV, "was SyncSource, missed the resync finished event, corrected myself:\n");
2683 drbd_uuid_set_bm(mdev, 0UL);
2684
2685 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2686 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2687 *rule_nr = 34;
2688 } else {
2689 dev_info(DEV, "was SyncSource (peer failed to write sync_uuid)\n");
2690 *rule_nr = 36;
2691 }
2692
2693 return 1;
2694 }
2695
2696 if (mdev->ldev->md.uuid[UI_BITMAP] == (u64)0 && mdev->p_uuid[UI_BITMAP] != (u64)0) {
2697
Philipp Reisner31890f42011-01-19 14:12:51 +01002698 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002699 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002700
2701 if ((mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_BITMAP] & ~((u64)1)) &&
2702 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1))) {
2703 dev_info(DEV, "was SyncTarget, peer missed the resync finished event, corrected peer:\n");
2704
2705 mdev->p_uuid[UI_HISTORY_START + 1] = mdev->p_uuid[UI_HISTORY_START];
2706 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_BITMAP];
2707 mdev->p_uuid[UI_BITMAP] = 0UL;
2708
2709 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2710 *rule_nr = 35;
2711 } else {
2712 dev_info(DEV, "was SyncTarget (failed to write sync_uuid)\n");
2713 *rule_nr = 37;
2714 }
2715
2716 return -1;
2717 }
2718
2719 /* Common power [off|failure] */
2720 rct = (test_bit(CRASHED_PRIMARY, &mdev->flags) ? 1 : 0) +
2721 (mdev->p_uuid[UI_FLAGS] & 2);
2722 /* lowest bit is set when we were primary,
2723 * next bit (weight 2) is set when peer was primary */
2724 *rule_nr = 40;
2725
2726 switch (rct) {
2727 case 0: /* !self_pri && !peer_pri */ return 0;
2728 case 1: /* self_pri && !peer_pri */ return 1;
2729 case 2: /* !self_pri && peer_pri */ return -1;
2730 case 3: /* self_pri && peer_pri */
Philipp Reisner25703f82011-02-07 14:35:25 +01002731 dc = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002732 return dc ? -1 : 1;
2733 }
2734 }
2735
2736 *rule_nr = 50;
2737 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2738 if (self == peer)
2739 return -1;
2740
2741 *rule_nr = 51;
2742 peer = mdev->p_uuid[UI_HISTORY_START] & ~((u64)1);
2743 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002744 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002745 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) ==
2746 (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1)) :
2747 peer + UUID_NEW_BM_OFFSET == (mdev->p_uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002748 /* The last P_SYNC_UUID did not get though. Undo the last start of
2749 resync as sync source modifications of the peer's UUIDs. */
2750
Philipp Reisner31890f42011-01-19 14:12:51 +01002751 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002752 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002753
2754 mdev->p_uuid[UI_BITMAP] = mdev->p_uuid[UI_HISTORY_START];
2755 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_HISTORY_START + 1];
Philipp Reisner4a23f262011-01-11 17:42:17 +01002756
2757 dev_info(DEV, "Did not got last syncUUID packet, corrected:\n");
2758 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2759
Philipp Reisnerb411b362009-09-25 16:07:19 -07002760 return -1;
2761 }
2762 }
2763
2764 *rule_nr = 60;
2765 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2766 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2767 peer = mdev->p_uuid[i] & ~((u64)1);
2768 if (self == peer)
2769 return -2;
2770 }
2771
2772 *rule_nr = 70;
2773 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2774 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2775 if (self == peer)
2776 return 1;
2777
2778 *rule_nr = 71;
2779 self = mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1);
2780 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002781 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002782 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) ==
2783 (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) :
2784 self + UUID_NEW_BM_OFFSET == (mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002785 /* The last P_SYNC_UUID did not get though. Undo the last start of
2786 resync as sync source modifications of our UUIDs. */
2787
Philipp Reisner31890f42011-01-19 14:12:51 +01002788 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002789 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002790
2791 _drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_HISTORY_START]);
2792 _drbd_uuid_set(mdev, UI_HISTORY_START, mdev->ldev->md.uuid[UI_HISTORY_START + 1]);
2793
Philipp Reisner4a23f262011-01-11 17:42:17 +01002794 dev_info(DEV, "Last syncUUID did not get through, corrected:\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002795 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2796 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2797
2798 return 1;
2799 }
2800 }
2801
2802
2803 *rule_nr = 80;
Philipp Reisnerd8c2a362009-11-18 15:52:51 +01002804 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002805 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2806 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2807 if (self == peer)
2808 return 2;
2809 }
2810
2811 *rule_nr = 90;
2812 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2813 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2814 if (self == peer && self != ((u64)0))
2815 return 100;
2816
2817 *rule_nr = 100;
2818 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2819 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2820 for (j = UI_HISTORY_START; j <= UI_HISTORY_END; j++) {
2821 peer = mdev->p_uuid[j] & ~((u64)1);
2822 if (self == peer)
2823 return -100;
2824 }
2825 }
2826
2827 return -1000;
2828}
2829
2830/* drbd_sync_handshake() returns the new conn state on success, or
2831 CONN_MASK (-1) on failure.
2832 */
2833static enum drbd_conns drbd_sync_handshake(struct drbd_conf *mdev, enum drbd_role peer_role,
2834 enum drbd_disk_state peer_disk) __must_hold(local)
2835{
Philipp Reisnerb411b362009-09-25 16:07:19 -07002836 enum drbd_conns rv = C_MASK;
2837 enum drbd_disk_state mydisk;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002838 struct net_conf *nc;
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02002839 int hg, rule_nr, rr_conflict, tentative;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002840
2841 mydisk = mdev->state.disk;
2842 if (mydisk == D_NEGOTIATING)
2843 mydisk = mdev->new_state_tmp.disk;
2844
2845 dev_info(DEV, "drbd_sync_handshake:\n");
2846 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid, mdev->comm_bm_set, 0);
2847 drbd_uuid_dump(mdev, "peer", mdev->p_uuid,
2848 mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2849
2850 hg = drbd_uuid_compare(mdev, &rule_nr);
2851
2852 dev_info(DEV, "uuid_compare()=%d by rule %d\n", hg, rule_nr);
2853
2854 if (hg == -1000) {
2855 dev_alert(DEV, "Unrelated data, aborting!\n");
2856 return C_MASK;
2857 }
Philipp Reisner4a23f262011-01-11 17:42:17 +01002858 if (hg < -1000) {
2859 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 -07002860 return C_MASK;
2861 }
2862
2863 if ((mydisk == D_INCONSISTENT && peer_disk > D_INCONSISTENT) ||
2864 (peer_disk == D_INCONSISTENT && mydisk > D_INCONSISTENT)) {
2865 int f = (hg == -100) || abs(hg) == 2;
2866 hg = mydisk > D_INCONSISTENT ? 1 : -1;
2867 if (f)
2868 hg = hg*2;
2869 dev_info(DEV, "Becoming sync %s due to disk states.\n",
2870 hg > 0 ? "source" : "target");
2871 }
2872
Adam Gandelman3a11a482010-04-08 16:48:23 -07002873 if (abs(hg) == 100)
2874 drbd_khelper(mdev, "initial-split-brain");
2875
Philipp Reisner44ed1672011-04-19 17:10:19 +02002876 rcu_read_lock();
2877 nc = rcu_dereference(mdev->tconn->net_conf);
2878
2879 if (hg == 100 || (hg == -100 && nc->always_asbp)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002880 int pcount = (mdev->state.role == R_PRIMARY)
2881 + (peer_role == R_PRIMARY);
2882 int forced = (hg == -100);
2883
2884 switch (pcount) {
2885 case 0:
2886 hg = drbd_asb_recover_0p(mdev);
2887 break;
2888 case 1:
2889 hg = drbd_asb_recover_1p(mdev);
2890 break;
2891 case 2:
2892 hg = drbd_asb_recover_2p(mdev);
2893 break;
2894 }
2895 if (abs(hg) < 100) {
2896 dev_warn(DEV, "Split-Brain detected, %d primaries, "
2897 "automatically solved. Sync from %s node\n",
2898 pcount, (hg < 0) ? "peer" : "this");
2899 if (forced) {
2900 dev_warn(DEV, "Doing a full sync, since"
2901 " UUIDs where ambiguous.\n");
2902 hg = hg*2;
2903 }
2904 }
2905 }
2906
2907 if (hg == -100) {
Andreas Gruenbacher6139f602011-05-06 20:00:02 +02002908 if (nc->discard_my_data && !(mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002909 hg = -1;
Andreas Gruenbacher6139f602011-05-06 20:00:02 +02002910 if (!nc->discard_my_data && (mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002911 hg = 1;
2912
2913 if (abs(hg) < 100)
2914 dev_warn(DEV, "Split-Brain detected, manually solved. "
2915 "Sync from %s node\n",
2916 (hg < 0) ? "peer" : "this");
2917 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002918 rr_conflict = nc->rr_conflict;
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02002919 tentative = nc->tentative;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002920 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002921
2922 if (hg == -100) {
Lars Ellenberg580b9762010-02-26 23:15:23 +01002923 /* FIXME this log message is not correct if we end up here
2924 * after an attempted attach on a diskless node.
2925 * We just refuse to attach -- well, we drop the "connection"
2926 * to that disk, in a way... */
Adam Gandelman3a11a482010-04-08 16:48:23 -07002927 dev_alert(DEV, "Split-Brain detected but unresolved, dropping connection!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002928 drbd_khelper(mdev, "split-brain");
2929 return C_MASK;
2930 }
2931
2932 if (hg > 0 && mydisk <= D_INCONSISTENT) {
2933 dev_err(DEV, "I shall become SyncSource, but I am inconsistent!\n");
2934 return C_MASK;
2935 }
2936
2937 if (hg < 0 && /* by intention we do not use mydisk here. */
2938 mdev->state.role == R_PRIMARY && mdev->state.disk >= D_CONSISTENT) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02002939 switch (rr_conflict) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002940 case ASB_CALL_HELPER:
2941 drbd_khelper(mdev, "pri-lost");
2942 /* fall through */
2943 case ASB_DISCONNECT:
2944 dev_err(DEV, "I shall become SyncTarget, but I am primary!\n");
2945 return C_MASK;
2946 case ASB_VIOLENTLY:
2947 dev_warn(DEV, "Becoming SyncTarget, violating the stable-data"
2948 "assumption\n");
2949 }
2950 }
2951
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02002952 if (tentative || test_bit(CONN_DRY_RUN, &mdev->tconn->flags)) {
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002953 if (hg == 0)
2954 dev_info(DEV, "dry-run connect: No resync, would become Connected immediately.\n");
2955 else
2956 dev_info(DEV, "dry-run connect: Would become %s, doing a %s resync.",
2957 drbd_conn_str(hg > 0 ? C_SYNC_SOURCE : C_SYNC_TARGET),
2958 abs(hg) >= 2 ? "full" : "bit-map based");
2959 return C_MASK;
2960 }
2961
Philipp Reisnerb411b362009-09-25 16:07:19 -07002962 if (abs(hg) >= 2) {
2963 dev_info(DEV, "Writing the whole bitmap, full sync required after drbd_sync_handshake.\n");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002964 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write, "set_n_write from sync_handshake",
2965 BM_LOCKED_SET_ALLOWED))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002966 return C_MASK;
2967 }
2968
2969 if (hg > 0) { /* become sync source. */
2970 rv = C_WF_BITMAP_S;
2971 } else if (hg < 0) { /* become sync target */
2972 rv = C_WF_BITMAP_T;
2973 } else {
2974 rv = C_CONNECTED;
2975 if (drbd_bm_total_weight(mdev)) {
2976 dev_info(DEV, "No resync, but %lu bits in bitmap!\n",
2977 drbd_bm_total_weight(mdev));
2978 }
2979 }
2980
2981 return rv;
2982}
2983
Philipp Reisnerf179d762011-05-16 17:31:47 +02002984static enum drbd_after_sb_p convert_after_sb(enum drbd_after_sb_p peer)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002985{
2986 /* ASB_DISCARD_REMOTE - ASB_DISCARD_LOCAL is valid */
Philipp Reisnerf179d762011-05-16 17:31:47 +02002987 if (peer == ASB_DISCARD_REMOTE)
2988 return ASB_DISCARD_LOCAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002989
2990 /* any other things with ASB_DISCARD_REMOTE or ASB_DISCARD_LOCAL are invalid */
Philipp Reisnerf179d762011-05-16 17:31:47 +02002991 if (peer == ASB_DISCARD_LOCAL)
2992 return ASB_DISCARD_REMOTE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002993
2994 /* everything else is valid if they are equal on both sides. */
Philipp Reisnerf179d762011-05-16 17:31:47 +02002995 return peer;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002996}
2997
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002998static int receive_protocol(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002999{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003000 struct p_protocol *p = pi->data;
Philipp Reisner036b17e2011-05-16 17:38:11 +02003001 enum drbd_after_sb_p p_after_sb_0p, p_after_sb_1p, p_after_sb_2p;
3002 int p_proto, p_discard_my_data, p_two_primaries, cf;
3003 struct net_conf *nc, *old_net_conf, *new_net_conf = NULL;
3004 char integrity_alg[SHARED_SECRET_MAX] = "";
Andreas Gruenbacherb792c352011-07-15 16:48:49 +02003005 struct crypto_hash *peer_integrity_tfm = NULL, *integrity_tfm = NULL;
Philipp Reisner7aca6c72011-05-17 10:12:56 +02003006 void *int_dig_in = NULL, *int_dig_vv = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003007
Philipp Reisnerb411b362009-09-25 16:07:19 -07003008 p_proto = be32_to_cpu(p->protocol);
3009 p_after_sb_0p = be32_to_cpu(p->after_sb_0p);
3010 p_after_sb_1p = be32_to_cpu(p->after_sb_1p);
3011 p_after_sb_2p = be32_to_cpu(p->after_sb_2p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003012 p_two_primaries = be32_to_cpu(p->two_primaries);
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003013 cf = be32_to_cpu(p->conn_flags);
Andreas Gruenbacher6139f602011-05-06 20:00:02 +02003014 p_discard_my_data = cf & CF_DISCARD_MY_DATA;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003015
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003016 if (tconn->agreed_pro_version >= 87) {
3017 int err;
3018
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02003019 if (pi->size > sizeof(integrity_alg))
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003020 return -EIO;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02003021 err = drbd_recv_all(tconn, integrity_alg, pi->size);
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003022 if (err)
3023 return err;
Philipp Reisner036b17e2011-05-16 17:38:11 +02003024 integrity_alg[SHARED_SECRET_MAX - 1] = 0;
3025 }
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003026
Philipp Reisner036b17e2011-05-16 17:38:11 +02003027 if (pi->cmd == P_PROTOCOL_UPDATE) {
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02003028 if (integrity_alg[0]) {
Philipp Reisner7aca6c72011-05-17 10:12:56 +02003029 int hash_size;
3030
Andreas Gruenbacherb792c352011-07-15 16:48:49 +02003031 peer_integrity_tfm = crypto_alloc_hash(integrity_alg, 0, CRYPTO_ALG_ASYNC);
3032 integrity_tfm = crypto_alloc_hash(integrity_alg, 0, CRYPTO_ALG_ASYNC);
3033 if (!(peer_integrity_tfm && integrity_tfm)) {
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02003034 conn_err(tconn, "peer data-integrity-alg %s not supported\n",
3035 integrity_alg);
3036 goto disconnect;
3037 }
Philipp Reisner7aca6c72011-05-17 10:12:56 +02003038
Andreas Gruenbacherb792c352011-07-15 16:48:49 +02003039 hash_size = crypto_hash_digestsize(integrity_tfm);
Philipp Reisner7aca6c72011-05-17 10:12:56 +02003040 int_dig_in = kmalloc(hash_size, GFP_KERNEL);
3041 int_dig_vv = kmalloc(hash_size, GFP_KERNEL);
3042 if (!(int_dig_in && int_dig_vv)) {
Philipp Reisner036b17e2011-05-16 17:38:11 +02003043 conn_err(tconn, "Allocation of buffers for data integrity checking failed\n");
Philipp Reisner7aca6c72011-05-17 10:12:56 +02003044 goto disconnect;
3045 }
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02003046 }
3047
Philipp Reisner036b17e2011-05-16 17:38:11 +02003048 new_net_conf = kmalloc(sizeof(struct net_conf), GFP_KERNEL);
3049 if (!new_net_conf) {
3050 conn_err(tconn, "Allocation of new net_conf failed\n");
3051 goto disconnect;
3052 }
3053
3054 mutex_lock(&tconn->data.mutex);
3055 mutex_lock(&tconn->conf_update);
3056 old_net_conf = tconn->net_conf;
3057 *new_net_conf = *old_net_conf;
3058
3059 new_net_conf->wire_protocol = p_proto;
3060 new_net_conf->after_sb_0p = convert_after_sb(p_after_sb_0p);
3061 new_net_conf->after_sb_1p = convert_after_sb(p_after_sb_1p);
3062 new_net_conf->after_sb_2p = convert_after_sb(p_after_sb_2p);
3063 new_net_conf->two_primaries = p_two_primaries;
3064 strcpy(new_net_conf->integrity_alg, integrity_alg);
3065 new_net_conf->integrity_alg_len = strlen(integrity_alg) + 1;
3066
3067 crypto_free_hash(tconn->integrity_tfm);
Andreas Gruenbacherb792c352011-07-15 16:48:49 +02003068 tconn->integrity_tfm = integrity_tfm;
Philipp Reisner036b17e2011-05-16 17:38:11 +02003069
3070 rcu_assign_pointer(tconn->net_conf, new_net_conf);
3071 mutex_unlock(&tconn->conf_update);
3072 mutex_unlock(&tconn->data.mutex);
3073
3074 crypto_free_hash(tconn->peer_integrity_tfm);
Philipp Reisner7aca6c72011-05-17 10:12:56 +02003075 kfree(tconn->int_dig_in);
3076 kfree(tconn->int_dig_vv);
Andreas Gruenbacherb792c352011-07-15 16:48:49 +02003077 tconn->peer_integrity_tfm = peer_integrity_tfm;
Philipp Reisner7aca6c72011-05-17 10:12:56 +02003078 tconn->int_dig_in = int_dig_in;
3079 tconn->int_dig_vv = int_dig_vv;
Philipp Reisner036b17e2011-05-16 17:38:11 +02003080
3081 if (strcmp(old_net_conf->integrity_alg, integrity_alg))
3082 conn_info(tconn, "peer data-integrity-alg: %s\n", integrity_alg);
3083
3084 synchronize_rcu();
3085 kfree(old_net_conf);
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003086 } else {
3087 clear_bit(CONN_DRY_RUN, &tconn->flags);
Philipp Reisner036b17e2011-05-16 17:38:11 +02003088
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003089 if (cf & CF_DRY_RUN)
3090 set_bit(CONN_DRY_RUN, &tconn->flags);
3091
3092 rcu_read_lock();
3093 nc = rcu_dereference(tconn->net_conf);
3094
3095 if (p_proto != nc->wire_protocol) {
3096 conn_err(tconn, "incompatible communication protocols\n");
3097 goto disconnect_rcu_unlock;
3098 }
3099
3100 if (convert_after_sb(p_after_sb_0p) != nc->after_sb_0p) {
3101 conn_err(tconn, "incompatible after-sb-0pri settings\n");
3102 goto disconnect_rcu_unlock;
3103 }
3104
3105 if (convert_after_sb(p_after_sb_1p) != nc->after_sb_1p) {
3106 conn_err(tconn, "incompatible after-sb-1pri settings\n");
3107 goto disconnect_rcu_unlock;
3108 }
3109
3110 if (convert_after_sb(p_after_sb_2p) != nc->after_sb_2p) {
3111 conn_err(tconn, "incompatible after-sb-2pri settings\n");
3112 goto disconnect_rcu_unlock;
3113 }
3114
3115 if (p_discard_my_data && nc->discard_my_data) {
3116 conn_err(tconn, "both sides have the 'discard_my_data' flag set\n");
3117 goto disconnect_rcu_unlock;
3118 }
3119
3120 if (p_two_primaries != nc->two_primaries) {
3121 conn_err(tconn, "incompatible setting of the two-primaries options\n");
3122 goto disconnect_rcu_unlock;
3123 }
3124
3125 if (strcmp(integrity_alg, nc->integrity_alg)) {
3126 conn_err(tconn, "incompatible setting of the data-integrity-alg\n");
3127 goto disconnect_rcu_unlock;
3128 }
3129
3130 rcu_read_unlock();
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003131 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003132 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003133
Philipp Reisner44ed1672011-04-19 17:10:19 +02003134disconnect_rcu_unlock:
3135 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07003136disconnect:
Andreas Gruenbacherb792c352011-07-15 16:48:49 +02003137 crypto_free_hash(peer_integrity_tfm);
3138 crypto_free_hash(integrity_tfm);
Philipp Reisner036b17e2011-05-16 17:38:11 +02003139 kfree(int_dig_in);
3140 kfree(int_dig_vv);
Philipp Reisner72046242011-03-15 18:51:47 +01003141 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003142 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003143}
3144
3145/* helper function
3146 * input: alg name, feature name
3147 * return: NULL (alg name was "")
3148 * ERR_PTR(error) if something goes wrong
3149 * or the crypto hash ptr, if it worked out ok. */
3150struct crypto_hash *drbd_crypto_alloc_digest_safe(const struct drbd_conf *mdev,
3151 const char *alg, const char *name)
3152{
3153 struct crypto_hash *tfm;
3154
3155 if (!alg[0])
3156 return NULL;
3157
3158 tfm = crypto_alloc_hash(alg, 0, CRYPTO_ALG_ASYNC);
3159 if (IS_ERR(tfm)) {
3160 dev_err(DEV, "Can not allocate \"%s\" as %s (reason: %ld)\n",
3161 alg, name, PTR_ERR(tfm));
3162 return tfm;
3163 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003164 return tfm;
3165}
3166
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003167static int ignore_remaining_packet(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003168{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003169 void *buffer = tconn->data.rbuf;
3170 int size = pi->size;
3171
3172 while (size) {
3173 int s = min_t(int, size, DRBD_SOCKET_BUFFER_SIZE);
3174 s = drbd_recv(tconn, buffer, s);
3175 if (s <= 0) {
3176 if (s < 0)
3177 return s;
3178 break;
3179 }
3180 size -= s;
3181 }
3182 if (size)
3183 return -EIO;
3184 return 0;
3185}
3186
3187/*
3188 * config_unknown_volume - device configuration command for unknown volume
3189 *
3190 * When a device is added to an existing connection, the node on which the
3191 * device is added first will send configuration commands to its peer but the
3192 * peer will not know about the device yet. It will warn and ignore these
3193 * commands. Once the device is added on the second node, the second node will
3194 * send the same device configuration commands, but in the other direction.
3195 *
3196 * (We can also end up here if drbd is misconfigured.)
3197 */
3198static int config_unknown_volume(struct drbd_tconn *tconn, struct packet_info *pi)
3199{
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02003200 conn_warn(tconn, "%s packet received for volume %u, which is not configured locally\n",
3201 cmdname(pi->cmd), pi->vnr);
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003202 return ignore_remaining_packet(tconn, pi);
3203}
3204
3205static int receive_SyncParam(struct drbd_tconn *tconn, struct packet_info *pi)
3206{
3207 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003208 struct p_rs_param_95 *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003209 unsigned int header_size, data_size, exp_max_sz;
3210 struct crypto_hash *verify_tfm = NULL;
3211 struct crypto_hash *csums_tfm = NULL;
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003212 struct net_conf *old_net_conf, *new_net_conf = NULL;
Philipp Reisner813472c2011-05-03 16:47:02 +02003213 struct disk_conf *old_disk_conf = NULL, *new_disk_conf = NULL;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003214 const int apv = tconn->agreed_pro_version;
Philipp Reisner813472c2011-05-03 16:47:02 +02003215 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
Philipp Reisner778f2712010-07-06 11:14:00 +02003216 int fifo_size = 0;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003217 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003218
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003219 mdev = vnr_to_mdev(tconn, pi->vnr);
3220 if (!mdev)
3221 return config_unknown_volume(tconn, pi);
3222
Philipp Reisnerb411b362009-09-25 16:07:19 -07003223 exp_max_sz = apv <= 87 ? sizeof(struct p_rs_param)
3224 : apv == 88 ? sizeof(struct p_rs_param)
3225 + SHARED_SECRET_MAX
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003226 : apv <= 94 ? sizeof(struct p_rs_param_89)
3227 : /* apv >= 95 */ sizeof(struct p_rs_param_95);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003228
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003229 if (pi->size > exp_max_sz) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003230 dev_err(DEV, "SyncParam packet too long: received %u, expected <= %u bytes\n",
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003231 pi->size, exp_max_sz);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003232 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003233 }
3234
3235 if (apv <= 88) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003236 header_size = sizeof(struct p_rs_param);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003237 data_size = pi->size - header_size;
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003238 } else if (apv <= 94) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003239 header_size = sizeof(struct p_rs_param_89);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003240 data_size = pi->size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003241 D_ASSERT(data_size == 0);
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003242 } else {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003243 header_size = sizeof(struct p_rs_param_95);
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);
3246 }
3247
3248 /* initialize verify_alg and csums_alg */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003249 p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003250 memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
3251
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003252 err = drbd_recv_all(mdev->tconn, p, header_size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003253 if (err)
3254 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003255
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003256 mutex_lock(&mdev->tconn->conf_update);
3257 old_net_conf = mdev->tconn->net_conf;
Philipp Reisner813472c2011-05-03 16:47:02 +02003258 if (get_ldev(mdev)) {
3259 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
3260 if (!new_disk_conf) {
3261 put_ldev(mdev);
3262 mutex_unlock(&mdev->tconn->conf_update);
3263 dev_err(DEV, "Allocation of new disk_conf failed\n");
3264 return -ENOMEM;
3265 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003266
Philipp Reisner813472c2011-05-03 16:47:02 +02003267 old_disk_conf = mdev->ldev->disk_conf;
3268 *new_disk_conf = *old_disk_conf;
3269
Andreas Gruenbacher6394b932011-05-11 14:29:52 +02003270 new_disk_conf->resync_rate = be32_to_cpu(p->resync_rate);
Philipp Reisner813472c2011-05-03 16:47:02 +02003271 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003272
Philipp Reisnerb411b362009-09-25 16:07:19 -07003273 if (apv >= 88) {
3274 if (apv == 88) {
3275 if (data_size > SHARED_SECRET_MAX) {
3276 dev_err(DEV, "verify-alg too long, "
3277 "peer wants %u, accepting only %u byte\n",
3278 data_size, SHARED_SECRET_MAX);
Philipp Reisner813472c2011-05-03 16:47:02 +02003279 err = -EIO;
3280 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003281 }
3282
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003283 err = drbd_recv_all(mdev->tconn, p->verify_alg, data_size);
Philipp Reisner813472c2011-05-03 16:47:02 +02003284 if (err)
3285 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003286 /* we expect NUL terminated string */
3287 /* but just in case someone tries to be evil */
3288 D_ASSERT(p->verify_alg[data_size-1] == 0);
3289 p->verify_alg[data_size-1] = 0;
3290
3291 } else /* apv >= 89 */ {
3292 /* we still expect NUL terminated strings */
3293 /* but just in case someone tries to be evil */
3294 D_ASSERT(p->verify_alg[SHARED_SECRET_MAX-1] == 0);
3295 D_ASSERT(p->csums_alg[SHARED_SECRET_MAX-1] == 0);
3296 p->verify_alg[SHARED_SECRET_MAX-1] = 0;
3297 p->csums_alg[SHARED_SECRET_MAX-1] = 0;
3298 }
3299
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003300 if (strcmp(old_net_conf->verify_alg, p->verify_alg)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003301 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
3302 dev_err(DEV, "Different verify-alg settings. me=\"%s\" peer=\"%s\"\n",
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003303 old_net_conf->verify_alg, p->verify_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003304 goto disconnect;
3305 }
3306 verify_tfm = drbd_crypto_alloc_digest_safe(mdev,
3307 p->verify_alg, "verify-alg");
3308 if (IS_ERR(verify_tfm)) {
3309 verify_tfm = NULL;
3310 goto disconnect;
3311 }
3312 }
3313
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003314 if (apv >= 89 && strcmp(old_net_conf->csums_alg, p->csums_alg)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003315 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
3316 dev_err(DEV, "Different csums-alg settings. me=\"%s\" peer=\"%s\"\n",
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003317 old_net_conf->csums_alg, p->csums_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003318 goto disconnect;
3319 }
3320 csums_tfm = drbd_crypto_alloc_digest_safe(mdev,
3321 p->csums_alg, "csums-alg");
3322 if (IS_ERR(csums_tfm)) {
3323 csums_tfm = NULL;
3324 goto disconnect;
3325 }
3326 }
3327
Philipp Reisner813472c2011-05-03 16:47:02 +02003328 if (apv > 94 && new_disk_conf) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003329 new_disk_conf->c_plan_ahead = be32_to_cpu(p->c_plan_ahead);
3330 new_disk_conf->c_delay_target = be32_to_cpu(p->c_delay_target);
3331 new_disk_conf->c_fill_target = be32_to_cpu(p->c_fill_target);
3332 new_disk_conf->c_max_rate = be32_to_cpu(p->c_max_rate);
Philipp Reisner778f2712010-07-06 11:14:00 +02003333
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003334 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
Philipp Reisner9958c852011-05-03 16:19:31 +02003335 if (fifo_size != mdev->rs_plan_s->size) {
Philipp Reisner813472c2011-05-03 16:47:02 +02003336 new_plan = fifo_alloc(fifo_size);
3337 if (!new_plan) {
Philipp Reisner778f2712010-07-06 11:14:00 +02003338 dev_err(DEV, "kmalloc of fifo_buffer failed");
Lars Ellenbergf3990022011-03-23 14:31:09 +01003339 put_ldev(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02003340 goto disconnect;
3341 }
3342 }
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003343 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003344
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003345 if (verify_tfm || csums_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003346 new_net_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
3347 if (!new_net_conf) {
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003348 dev_err(DEV, "Allocation of new net_conf failed\n");
3349 goto disconnect;
3350 }
3351
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003352 *new_net_conf = *old_net_conf;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003353
3354 if (verify_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003355 strcpy(new_net_conf->verify_alg, p->verify_alg);
3356 new_net_conf->verify_alg_len = strlen(p->verify_alg) + 1;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003357 crypto_free_hash(mdev->tconn->verify_tfm);
3358 mdev->tconn->verify_tfm = verify_tfm;
3359 dev_info(DEV, "using verify-alg: \"%s\"\n", p->verify_alg);
3360 }
3361 if (csums_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003362 strcpy(new_net_conf->csums_alg, p->csums_alg);
3363 new_net_conf->csums_alg_len = strlen(p->csums_alg) + 1;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003364 crypto_free_hash(mdev->tconn->csums_tfm);
3365 mdev->tconn->csums_tfm = csums_tfm;
3366 dev_info(DEV, "using csums-alg: \"%s\"\n", p->csums_alg);
3367 }
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003368 rcu_assign_pointer(tconn->net_conf, new_net_conf);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003369 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003370 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003371
Philipp Reisner813472c2011-05-03 16:47:02 +02003372 if (new_disk_conf) {
3373 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
3374 put_ldev(mdev);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003375 }
Philipp Reisner813472c2011-05-03 16:47:02 +02003376
3377 if (new_plan) {
3378 old_plan = mdev->rs_plan_s;
3379 rcu_assign_pointer(mdev->rs_plan_s, new_plan);
3380 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003381
3382 mutex_unlock(&mdev->tconn->conf_update);
3383 synchronize_rcu();
3384 if (new_net_conf)
3385 kfree(old_net_conf);
3386 kfree(old_disk_conf);
Philipp Reisner813472c2011-05-03 16:47:02 +02003387 kfree(old_plan);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003388
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003389 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003390
Philipp Reisner813472c2011-05-03 16:47:02 +02003391reconnect:
3392 if (new_disk_conf) {
3393 put_ldev(mdev);
3394 kfree(new_disk_conf);
3395 }
3396 mutex_unlock(&mdev->tconn->conf_update);
3397 return -EIO;
3398
Philipp Reisnerb411b362009-09-25 16:07:19 -07003399disconnect:
Philipp Reisner813472c2011-05-03 16:47:02 +02003400 kfree(new_plan);
3401 if (new_disk_conf) {
3402 put_ldev(mdev);
3403 kfree(new_disk_conf);
3404 }
Philipp Reisnera0095502011-05-03 13:14:15 +02003405 mutex_unlock(&mdev->tconn->conf_update);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003406 /* just for completeness: actually not needed,
3407 * as this is not reached if csums_tfm was ok. */
3408 crypto_free_hash(csums_tfm);
3409 /* but free the verify_tfm again, if csums_tfm did not work out */
3410 crypto_free_hash(verify_tfm);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003411 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003412 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003413}
3414
Philipp Reisnerb411b362009-09-25 16:07:19 -07003415/* warn if the arguments differ by more than 12.5% */
3416static void warn_if_differ_considerably(struct drbd_conf *mdev,
3417 const char *s, sector_t a, sector_t b)
3418{
3419 sector_t d;
3420 if (a == 0 || b == 0)
3421 return;
3422 d = (a > b) ? (a - b) : (b - a);
3423 if (d > (a>>3) || d > (b>>3))
3424 dev_warn(DEV, "Considerable difference in %s: %llus vs. %llus\n", s,
3425 (unsigned long long)a, (unsigned long long)b);
3426}
3427
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003428static int receive_sizes(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003429{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003430 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003431 struct p_sizes *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003432 enum determine_dev_size dd = unchanged;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003433 sector_t p_size, p_usize, my_usize;
3434 int ldsc = 0; /* local disk size changed */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003435 enum dds_flags ddsf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003436
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003437 mdev = vnr_to_mdev(tconn, pi->vnr);
3438 if (!mdev)
3439 return config_unknown_volume(tconn, pi);
3440
Philipp Reisnerb411b362009-09-25 16:07:19 -07003441 p_size = be64_to_cpu(p->d_size);
3442 p_usize = be64_to_cpu(p->u_size);
3443
Philipp Reisnerb411b362009-09-25 16:07:19 -07003444 /* just store the peer's disk size for now.
3445 * we still need to figure out whether we accept that. */
3446 mdev->p_size = p_size;
3447
Philipp Reisnerb411b362009-09-25 16:07:19 -07003448 if (get_ldev(mdev)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003449 rcu_read_lock();
3450 my_usize = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
3451 rcu_read_unlock();
3452
Philipp Reisnerb411b362009-09-25 16:07:19 -07003453 warn_if_differ_considerably(mdev, "lower level device sizes",
3454 p_size, drbd_get_max_capacity(mdev->ldev));
3455 warn_if_differ_considerably(mdev, "user requested size",
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003456 p_usize, my_usize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003457
3458 /* if this is the first connect, or an otherwise expected
3459 * param exchange, choose the minimum */
3460 if (mdev->state.conn == C_WF_REPORT_PARAMS)
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003461 p_usize = min_not_zero(my_usize, p_usize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003462
3463 /* Never shrink a device with usable data during connect.
3464 But allow online shrinking if we are connected. */
Philipp Reisneref5e44a2011-05-03 13:27:43 +02003465 if (drbd_new_dev_size(mdev, mdev->ldev, p_usize, 0) <
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003466 drbd_get_capacity(mdev->this_bdev) &&
3467 mdev->state.disk >= D_OUTDATED &&
3468 mdev->state.conn < C_CONNECTED) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003469 dev_err(DEV, "The peer's disk size is too small!\n");
Philipp Reisner38fa9982011-03-15 18:24:49 +01003470 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003471 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003472 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003473 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003474
3475 if (my_usize != p_usize) {
3476 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
3477
3478 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
3479 if (!new_disk_conf) {
3480 dev_err(DEV, "Allocation of new disk_conf failed\n");
3481 put_ldev(mdev);
3482 return -ENOMEM;
3483 }
3484
3485 mutex_lock(&mdev->tconn->conf_update);
3486 old_disk_conf = mdev->ldev->disk_conf;
3487 *new_disk_conf = *old_disk_conf;
3488 new_disk_conf->disk_size = p_usize;
3489
3490 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
3491 mutex_unlock(&mdev->tconn->conf_update);
3492 synchronize_rcu();
3493 kfree(old_disk_conf);
3494
3495 dev_info(DEV, "Peer sets u_size to %lu sectors\n",
3496 (unsigned long)my_usize);
3497 }
3498
Philipp Reisnerb411b362009-09-25 16:07:19 -07003499 put_ldev(mdev);
3500 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003501
Philipp Reisnere89b5912010-03-24 17:11:33 +01003502 ddsf = be16_to_cpu(p->dds_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003503 if (get_ldev(mdev)) {
Bart Van Assche24c48302011-05-21 18:32:29 +02003504 dd = drbd_determine_dev_size(mdev, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003505 put_ldev(mdev);
3506 if (dd == dev_size_error)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003507 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003508 drbd_md_sync(mdev);
3509 } else {
3510 /* I am diskless, need to accept the peer's size. */
3511 drbd_set_my_capacity(mdev, p_size);
3512 }
3513
Philipp Reisner99432fc2011-05-20 16:39:13 +02003514 mdev->peer_max_bio_size = be32_to_cpu(p->max_bio_size);
3515 drbd_reconsider_max_bio_size(mdev);
3516
Philipp Reisnerb411b362009-09-25 16:07:19 -07003517 if (get_ldev(mdev)) {
3518 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev)) {
3519 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
3520 ldsc = 1;
3521 }
3522
Philipp Reisnerb411b362009-09-25 16:07:19 -07003523 put_ldev(mdev);
3524 }
3525
3526 if (mdev->state.conn > C_WF_REPORT_PARAMS) {
3527 if (be64_to_cpu(p->c_size) !=
3528 drbd_get_capacity(mdev->this_bdev) || ldsc) {
3529 /* we have different sizes, probably peer
3530 * needs to know my new size... */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003531 drbd_send_sizes(mdev, 0, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003532 }
3533 if (test_and_clear_bit(RESIZE_PENDING, &mdev->flags) ||
3534 (dd == grew && mdev->state.conn == C_CONNECTED)) {
3535 if (mdev->state.pdsk >= D_INCONSISTENT &&
Philipp Reisnere89b5912010-03-24 17:11:33 +01003536 mdev->state.disk >= D_INCONSISTENT) {
3537 if (ddsf & DDSF_NO_RESYNC)
3538 dev_info(DEV, "Resync of new storage suppressed with --assume-clean\n");
3539 else
3540 resync_after_online_grow(mdev);
3541 } else
Philipp Reisnerb411b362009-09-25 16:07:19 -07003542 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
3543 }
3544 }
3545
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003546 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003547}
3548
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003549static int receive_uuids(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003550{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003551 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003552 struct p_uuids *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003553 u64 *p_uuid;
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003554 int i, updated_uuids = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003555
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003556 mdev = vnr_to_mdev(tconn, pi->vnr);
3557 if (!mdev)
3558 return config_unknown_volume(tconn, pi);
3559
Philipp Reisnerb411b362009-09-25 16:07:19 -07003560 p_uuid = kmalloc(sizeof(u64)*UI_EXTENDED_SIZE, GFP_NOIO);
3561
3562 for (i = UI_CURRENT; i < UI_EXTENDED_SIZE; i++)
3563 p_uuid[i] = be64_to_cpu(p->uuid[i]);
3564
3565 kfree(mdev->p_uuid);
3566 mdev->p_uuid = p_uuid;
3567
3568 if (mdev->state.conn < C_CONNECTED &&
3569 mdev->state.disk < D_INCONSISTENT &&
3570 mdev->state.role == R_PRIMARY &&
3571 (mdev->ed_uuid & ~((u64)1)) != (p_uuid[UI_CURRENT] & ~((u64)1))) {
3572 dev_err(DEV, "Can only connect to data with current UUID=%016llX\n",
3573 (unsigned long long)mdev->ed_uuid);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003574 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003575 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003576 }
3577
3578 if (get_ldev(mdev)) {
3579 int skip_initial_sync =
3580 mdev->state.conn == C_CONNECTED &&
Philipp Reisner31890f42011-01-19 14:12:51 +01003581 mdev->tconn->agreed_pro_version >= 90 &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003582 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED &&
3583 (p_uuid[UI_FLAGS] & 8);
3584 if (skip_initial_sync) {
3585 dev_info(DEV, "Accepted new current UUID, preparing to skip initial sync\n");
3586 drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003587 "clear_n_write from receive_uuids",
3588 BM_LOCKED_TEST_ALLOWED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003589 _drbd_uuid_set(mdev, UI_CURRENT, p_uuid[UI_CURRENT]);
3590 _drbd_uuid_set(mdev, UI_BITMAP, 0);
3591 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
3592 CS_VERBOSE, NULL);
3593 drbd_md_sync(mdev);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003594 updated_uuids = 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003595 }
3596 put_ldev(mdev);
Philipp Reisner18a50fa2010-06-21 14:14:15 +02003597 } else if (mdev->state.disk < D_INCONSISTENT &&
3598 mdev->state.role == R_PRIMARY) {
3599 /* I am a diskless primary, the peer just created a new current UUID
3600 for me. */
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003601 updated_uuids = drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003602 }
3603
3604 /* Before we test for the disk state, we should wait until an eventually
3605 ongoing cluster wide state change is finished. That is important if
3606 we are primary and are detaching from our disk. We need to see the
3607 new disk state... */
Philipp Reisner8410da82011-02-11 20:11:10 +01003608 mutex_lock(mdev->state_mutex);
3609 mutex_unlock(mdev->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003610 if (mdev->state.conn >= C_CONNECTED && mdev->state.disk < D_INCONSISTENT)
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003611 updated_uuids |= drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
3612
3613 if (updated_uuids)
3614 drbd_print_uuids(mdev, "receiver updated UUIDs to");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003615
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003616 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003617}
3618
3619/**
3620 * convert_state() - Converts the peer's view of the cluster state to our point of view
3621 * @ps: The state as seen by the peer.
3622 */
3623static union drbd_state convert_state(union drbd_state ps)
3624{
3625 union drbd_state ms;
3626
3627 static enum drbd_conns c_tab[] = {
Philipp Reisner369bea62011-07-06 23:04:44 +02003628 [C_WF_REPORT_PARAMS] = C_WF_REPORT_PARAMS,
Philipp Reisnerb411b362009-09-25 16:07:19 -07003629 [C_CONNECTED] = C_CONNECTED,
3630
3631 [C_STARTING_SYNC_S] = C_STARTING_SYNC_T,
3632 [C_STARTING_SYNC_T] = C_STARTING_SYNC_S,
3633 [C_DISCONNECTING] = C_TEAR_DOWN, /* C_NETWORK_FAILURE, */
3634 [C_VERIFY_S] = C_VERIFY_T,
3635 [C_MASK] = C_MASK,
3636 };
3637
3638 ms.i = ps.i;
3639
3640 ms.conn = c_tab[ps.conn];
3641 ms.peer = ps.role;
3642 ms.role = ps.peer;
3643 ms.pdsk = ps.disk;
3644 ms.disk = ps.pdsk;
3645 ms.peer_isp = (ps.aftr_isp | ps.user_isp);
3646
3647 return ms;
3648}
3649
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003650static int receive_req_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003651{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003652 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003653 struct p_req_state *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003654 union drbd_state mask, val;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01003655 enum drbd_state_rv rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003656
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003657 mdev = vnr_to_mdev(tconn, pi->vnr);
3658 if (!mdev)
3659 return -EIO;
3660
Philipp Reisnerb411b362009-09-25 16:07:19 -07003661 mask.i = be32_to_cpu(p->mask);
3662 val.i = be32_to_cpu(p->val);
3663
Philipp Reisner25703f82011-02-07 14:35:25 +01003664 if (test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags) &&
Philipp Reisner8410da82011-02-11 20:11:10 +01003665 mutex_is_locked(mdev->state_mutex)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003666 drbd_send_sr_reply(mdev, SS_CONCURRENT_ST_CHG);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003667 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003668 }
3669
3670 mask = convert_state(mask);
3671 val = convert_state(val);
3672
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003673 rv = drbd_change_state(mdev, CS_VERBOSE, mask, val);
3674 drbd_send_sr_reply(mdev, rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003675
Philipp Reisnerb411b362009-09-25 16:07:19 -07003676 drbd_md_sync(mdev);
3677
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003678 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003679}
3680
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003681static int receive_req_conn_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003682{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003683 struct p_req_state *p = pi->data;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003684 union drbd_state mask, val;
3685 enum drbd_state_rv rv;
3686
3687 mask.i = be32_to_cpu(p->mask);
3688 val.i = be32_to_cpu(p->val);
3689
3690 if (test_bit(DISCARD_CONCURRENT, &tconn->flags) &&
3691 mutex_is_locked(&tconn->cstate_mutex)) {
3692 conn_send_sr_reply(tconn, SS_CONCURRENT_ST_CHG);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003693 return 0;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003694 }
3695
3696 mask = convert_state(mask);
3697 val = convert_state(val);
3698
Philipp Reisner778bcf22011-03-28 12:55:03 +02003699 rv = conn_request_state(tconn, mask, val, CS_VERBOSE | CS_LOCAL_ONLY | CS_IGN_OUTD_FAIL);
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003700 conn_send_sr_reply(tconn, rv);
3701
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003702 return 0;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003703}
3704
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003705static int receive_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003706{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003707 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003708 struct p_state *p = pi->data;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003709 union drbd_state os, ns, peer_state;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003710 enum drbd_disk_state real_peer_disk;
Philipp Reisner65d922c2010-06-16 16:18:09 +02003711 enum chg_state_flags cs_flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003712 int rv;
3713
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003714 mdev = vnr_to_mdev(tconn, pi->vnr);
3715 if (!mdev)
3716 return config_unknown_volume(tconn, pi);
3717
Philipp Reisnerb411b362009-09-25 16:07:19 -07003718 peer_state.i = be32_to_cpu(p->state);
3719
3720 real_peer_disk = peer_state.disk;
3721 if (peer_state.disk == D_NEGOTIATING) {
3722 real_peer_disk = mdev->p_uuid[UI_FLAGS] & 4 ? D_INCONSISTENT : D_CONSISTENT;
3723 dev_info(DEV, "real peer disk state = %s\n", drbd_disk_str(real_peer_disk));
3724 }
3725
Philipp Reisner87eeee42011-01-19 14:16:30 +01003726 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003727 retry:
Philipp Reisner78bae592011-03-28 15:40:12 +02003728 os = ns = drbd_read_state(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01003729 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003730
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003731 /* peer says his disk is uptodate, while we think it is inconsistent,
3732 * and this happens while we think we have a sync going on. */
3733 if (os.pdsk == D_INCONSISTENT && real_peer_disk == D_UP_TO_DATE &&
3734 os.conn > C_CONNECTED && os.disk == D_UP_TO_DATE) {
3735 /* If we are (becoming) SyncSource, but peer is still in sync
3736 * preparation, ignore its uptodate-ness to avoid flapping, it
3737 * will change to inconsistent once the peer reaches active
3738 * syncing states.
3739 * It may have changed syncer-paused flags, however, so we
3740 * cannot ignore this completely. */
3741 if (peer_state.conn > C_CONNECTED &&
3742 peer_state.conn < C_SYNC_SOURCE)
3743 real_peer_disk = D_INCONSISTENT;
3744
3745 /* if peer_state changes to connected at the same time,
3746 * it explicitly notifies us that it finished resync.
3747 * Maybe we should finish it up, too? */
3748 else if (os.conn >= C_SYNC_SOURCE &&
3749 peer_state.conn == C_CONNECTED) {
3750 if (drbd_bm_total_weight(mdev) <= mdev->rs_failed)
3751 drbd_resync_finished(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003752 return 0;
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003753 }
3754 }
3755
3756 /* peer says his disk is inconsistent, while we think it is uptodate,
3757 * and this happens while the peer still thinks we have a sync going on,
3758 * but we think we are already done with the sync.
3759 * We ignore this to avoid flapping pdsk.
3760 * This should not happen, if the peer is a recent version of drbd. */
3761 if (os.pdsk == D_UP_TO_DATE && real_peer_disk == D_INCONSISTENT &&
3762 os.conn == C_CONNECTED && peer_state.conn > C_SYNC_SOURCE)
3763 real_peer_disk = D_UP_TO_DATE;
3764
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003765 if (ns.conn == C_WF_REPORT_PARAMS)
3766 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003767
Philipp Reisner67531712010-10-27 12:21:30 +02003768 if (peer_state.conn == C_AHEAD)
3769 ns.conn = C_BEHIND;
3770
Philipp Reisnerb411b362009-09-25 16:07:19 -07003771 if (mdev->p_uuid && peer_state.disk >= D_NEGOTIATING &&
3772 get_ldev_if_state(mdev, D_NEGOTIATING)) {
3773 int cr; /* consider resync */
3774
3775 /* if we established a new connection */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003776 cr = (os.conn < C_CONNECTED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003777 /* if we had an established connection
3778 * and one of the nodes newly attaches a disk */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003779 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003780 (peer_state.disk == D_NEGOTIATING ||
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003781 os.disk == D_NEGOTIATING));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003782 /* if we have both been inconsistent, and the peer has been
3783 * forced to be UpToDate with --overwrite-data */
3784 cr |= test_bit(CONSIDER_RESYNC, &mdev->flags);
3785 /* if we had been plain connected, and the admin requested to
3786 * start a sync by "invalidate" or "invalidate-remote" */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003787 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003788 (peer_state.conn >= C_STARTING_SYNC_S &&
3789 peer_state.conn <= C_WF_BITMAP_T));
3790
3791 if (cr)
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003792 ns.conn = drbd_sync_handshake(mdev, peer_state.role, real_peer_disk);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003793
3794 put_ldev(mdev);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003795 if (ns.conn == C_MASK) {
3796 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003797 if (mdev->state.disk == D_NEGOTIATING) {
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02003798 drbd_force_state(mdev, NS(disk, D_FAILED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003799 } else if (peer_state.disk == D_NEGOTIATING) {
3800 dev_err(DEV, "Disk attach process on the peer node was aborted.\n");
3801 peer_state.disk = D_DISKLESS;
Lars Ellenberg580b9762010-02-26 23:15:23 +01003802 real_peer_disk = D_DISKLESS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003803 } else {
Philipp Reisner8169e412011-03-15 18:40:27 +01003804 if (test_and_clear_bit(CONN_DRY_RUN, &mdev->tconn->flags))
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003805 return -EIO;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003806 D_ASSERT(os.conn == C_WF_REPORT_PARAMS);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003807 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003808 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003809 }
3810 }
3811 }
3812
Philipp Reisner87eeee42011-01-19 14:16:30 +01003813 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner78bae592011-03-28 15:40:12 +02003814 if (os.i != drbd_read_state(mdev).i)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003815 goto retry;
3816 clear_bit(CONSIDER_RESYNC, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003817 ns.peer = peer_state.role;
3818 ns.pdsk = real_peer_disk;
3819 ns.peer_isp = (peer_state.aftr_isp | peer_state.user_isp);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003820 if ((ns.conn == C_CONNECTED || ns.conn == C_WF_BITMAP_S) && ns.disk == D_NEGOTIATING)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003821 ns.disk = mdev->new_state_tmp.disk;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003822 cs_flags = CS_VERBOSE + (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED ? 0 : CS_HARD);
Philipp Reisner2aebfab2011-03-28 16:48:11 +02003823 if (ns.pdsk == D_CONSISTENT && drbd_suspended(mdev) && ns.conn == C_CONNECTED && os.conn < C_CONNECTED &&
Philipp Reisner481c6f52010-06-22 14:03:27 +02003824 test_bit(NEW_CUR_UUID, &mdev->flags)) {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01003825 /* Do not allow tl_restart(RESEND) for a rebooted peer. We can only allow this
Philipp Reisner481c6f52010-06-22 14:03:27 +02003826 for temporal network outages! */
Philipp Reisner87eeee42011-01-19 14:16:30 +01003827 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003828 dev_err(DEV, "Aborting Connect, can not thaw IO with an only Consistent peer\n");
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01003829 tl_clear(mdev->tconn);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003830 drbd_uuid_new_current(mdev);
3831 clear_bit(NEW_CUR_UUID, &mdev->flags);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003832 conn_request_state(mdev->tconn, NS2(conn, C_PROTOCOL_ERROR, susp, 0), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003833 return -EIO;
Philipp Reisner481c6f52010-06-22 14:03:27 +02003834 }
Philipp Reisner65d922c2010-06-16 16:18:09 +02003835 rv = _drbd_set_state(mdev, ns, cs_flags, NULL);
Philipp Reisner78bae592011-03-28 15:40:12 +02003836 ns = drbd_read_state(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01003837 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003838
3839 if (rv < SS_SUCCESS) {
Philipp Reisner38fa9982011-03-15 18:24:49 +01003840 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003841 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003842 }
3843
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003844 if (os.conn > C_WF_REPORT_PARAMS) {
3845 if (ns.conn > C_CONNECTED && peer_state.conn <= C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003846 peer_state.disk != D_NEGOTIATING ) {
3847 /* we want resync, peer has not yet decided to sync... */
3848 /* Nowadays only used when forcing a node into primary role and
3849 setting its disk to UpToDate with that */
3850 drbd_send_uuids(mdev);
3851 drbd_send_state(mdev);
3852 }
3853 }
3854
Philipp Reisnera0095502011-05-03 13:14:15 +02003855 mutex_lock(&mdev->tconn->conf_update);
Andreas Gruenbacher6139f602011-05-06 20:00:02 +02003856 mdev->tconn->net_conf->discard_my_data = 0; /* without copy; single bit op is atomic */
Philipp Reisnera0095502011-05-03 13:14:15 +02003857 mutex_unlock(&mdev->tconn->conf_update);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003858
3859 drbd_md_sync(mdev); /* update connected indicator, la_size, ... */
3860
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003861 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003862}
3863
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003864static int receive_sync_uuid(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003865{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003866 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003867 struct p_rs_uuid *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003868
3869 mdev = vnr_to_mdev(tconn, pi->vnr);
3870 if (!mdev)
3871 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003872
3873 wait_event(mdev->misc_wait,
3874 mdev->state.conn == C_WF_SYNC_UUID ||
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02003875 mdev->state.conn == C_BEHIND ||
Philipp Reisnerb411b362009-09-25 16:07:19 -07003876 mdev->state.conn < C_CONNECTED ||
3877 mdev->state.disk < D_NEGOTIATING);
3878
3879 /* D_ASSERT( mdev->state.conn == C_WF_SYNC_UUID ); */
3880
Philipp Reisnerb411b362009-09-25 16:07:19 -07003881 /* Here the _drbd_uuid_ functions are right, current should
3882 _not_ be rotated into the history */
3883 if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
3884 _drbd_uuid_set(mdev, UI_CURRENT, be64_to_cpu(p->uuid));
3885 _drbd_uuid_set(mdev, UI_BITMAP, 0UL);
3886
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003887 drbd_print_uuids(mdev, "updated sync uuid");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003888 drbd_start_resync(mdev, C_SYNC_TARGET);
3889
3890 put_ldev(mdev);
3891 } else
3892 dev_err(DEV, "Ignoring SyncUUID packet!\n");
3893
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003894 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003895}
3896
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003897/**
3898 * receive_bitmap_plain
3899 *
3900 * Return 0 when done, 1 when another iteration is needed, and a negative error
3901 * code upon failure.
3902 */
3903static int
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003904receive_bitmap_plain(struct drbd_conf *mdev, unsigned int size,
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003905 unsigned long *p, struct bm_xfer_ctx *c)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003906{
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003907 unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE -
3908 drbd_header_size(mdev->tconn);
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003909 unsigned int num_words = min_t(size_t, data_size / sizeof(*p),
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003910 c->bm_words - c->word_offset);
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003911 unsigned int want = num_words * sizeof(*p);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003912 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003913
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003914 if (want != size) {
3915 dev_err(DEV, "%s:want (%u) != size (%u)\n", __func__, want, size);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003916 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003917 }
3918 if (want == 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003919 return 0;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003920 err = drbd_recv_all(mdev->tconn, p, want);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003921 if (err)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003922 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003923
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003924 drbd_bm_merge_lel(mdev, c->word_offset, num_words, p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003925
3926 c->word_offset += num_words;
3927 c->bit_offset = c->word_offset * BITS_PER_LONG;
3928 if (c->bit_offset > c->bm_bits)
3929 c->bit_offset = c->bm_bits;
3930
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003931 return 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003932}
3933
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01003934static enum drbd_bitmap_code dcbp_get_code(struct p_compressed_bm *p)
3935{
3936 return (enum drbd_bitmap_code)(p->encoding & 0x0f);
3937}
3938
3939static int dcbp_get_start(struct p_compressed_bm *p)
3940{
3941 return (p->encoding & 0x80) != 0;
3942}
3943
3944static int dcbp_get_pad_bits(struct p_compressed_bm *p)
3945{
3946 return (p->encoding >> 4) & 0x7;
3947}
3948
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003949/**
3950 * recv_bm_rle_bits
3951 *
3952 * Return 0 when done, 1 when another iteration is needed, and a negative error
3953 * code upon failure.
3954 */
3955static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07003956recv_bm_rle_bits(struct drbd_conf *mdev,
3957 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01003958 struct bm_xfer_ctx *c,
3959 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003960{
3961 struct bitstream bs;
3962 u64 look_ahead;
3963 u64 rl;
3964 u64 tmp;
3965 unsigned long s = c->bit_offset;
3966 unsigned long e;
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01003967 int toggle = dcbp_get_start(p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003968 int have;
3969 int bits;
3970
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01003971 bitstream_init(&bs, p->code, len, dcbp_get_pad_bits(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003972
3973 bits = bitstream_get_bits(&bs, &look_ahead, 64);
3974 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003975 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003976
3977 for (have = bits; have > 0; s += rl, toggle = !toggle) {
3978 bits = vli_decode_bits(&rl, look_ahead);
3979 if (bits <= 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003980 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003981
3982 if (toggle) {
3983 e = s + rl -1;
3984 if (e >= c->bm_bits) {
3985 dev_err(DEV, "bitmap overflow (e:%lu) while decoding bm RLE packet\n", e);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003986 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003987 }
3988 _drbd_bm_set_bits(mdev, s, e);
3989 }
3990
3991 if (have < bits) {
3992 dev_err(DEV, "bitmap decoding error: h:%d b:%d la:0x%08llx l:%u/%u\n",
3993 have, bits, look_ahead,
3994 (unsigned int)(bs.cur.b - p->code),
3995 (unsigned int)bs.buf_len);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003996 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003997 }
3998 look_ahead >>= bits;
3999 have -= bits;
4000
4001 bits = bitstream_get_bits(&bs, &tmp, 64 - have);
4002 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004003 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004004 look_ahead |= tmp << have;
4005 have += bits;
4006 }
4007
4008 c->bit_offset = s;
4009 bm_xfer_ctx_bit_to_word_offset(c);
4010
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004011 return (s != c->bm_bits);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004012}
4013
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004014/**
4015 * decode_bitmap_c
4016 *
4017 * Return 0 when done, 1 when another iteration is needed, and a negative error
4018 * code upon failure.
4019 */
4020static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07004021decode_bitmap_c(struct drbd_conf *mdev,
4022 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01004023 struct bm_xfer_ctx *c,
4024 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004025{
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004026 if (dcbp_get_code(p) == RLE_VLI_Bits)
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004027 return recv_bm_rle_bits(mdev, p, c, len - sizeof(*p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07004028
4029 /* other variants had been implemented for evaluation,
4030 * but have been dropped as this one turned out to be "best"
4031 * during all our tests. */
4032
4033 dev_err(DEV, "receive_bitmap_c: unknown encoding %u\n", p->encoding);
Philipp Reisner38fa9982011-03-15 18:24:49 +01004034 conn_request_state(mdev->tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004035 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004036}
4037
4038void INFO_bm_xfer_stats(struct drbd_conf *mdev,
4039 const char *direction, struct bm_xfer_ctx *c)
4040{
4041 /* what would it take to transfer it "plaintext" */
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004042 unsigned int header_size = drbd_header_size(mdev->tconn);
4043 unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE - header_size;
4044 unsigned int plain =
4045 header_size * (DIV_ROUND_UP(c->bm_words, data_size) + 1) +
4046 c->bm_words * sizeof(unsigned long);
4047 unsigned int total = c->bytes[0] + c->bytes[1];
4048 unsigned int r;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004049
4050 /* total can not be zero. but just in case: */
4051 if (total == 0)
4052 return;
4053
4054 /* don't report if not compressed */
4055 if (total >= plain)
4056 return;
4057
4058 /* total < plain. check for overflow, still */
4059 r = (total > UINT_MAX/1000) ? (total / (plain/1000))
4060 : (1000 * total / plain);
4061
4062 if (r > 1000)
4063 r = 1000;
4064
4065 r = 1000 - r;
4066 dev_info(DEV, "%s bitmap stats [Bytes(packets)]: plain %u(%u), RLE %u(%u), "
4067 "total %u; compression: %u.%u%%\n",
4068 direction,
4069 c->bytes[1], c->packets[1],
4070 c->bytes[0], c->packets[0],
4071 total, r/10, r % 10);
4072}
4073
4074/* Since we are processing the bitfield from lower addresses to higher,
4075 it does not matter if the process it in 32 bit chunks or 64 bit
4076 chunks as long as it is little endian. (Understand it as byte stream,
4077 beginning with the lowest byte...) If we would use big endian
4078 we would need to process it from the highest address to the lowest,
4079 in order to be agnostic to the 32 vs 64 bits issue.
4080
4081 returns 0 on failure, 1 if we successfully received it. */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004082static int receive_bitmap(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004083{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004084 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004085 struct bm_xfer_ctx c;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004086 int err;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004087
4088 mdev = vnr_to_mdev(tconn, pi->vnr);
4089 if (!mdev)
4090 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004091
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004092 drbd_bm_lock(mdev, "receive bitmap", BM_LOCKED_SET_ALLOWED);
4093 /* you are supposed to send additional out-of-sync information
4094 * if you actually set bits during this phase */
Philipp Reisnerb411b362009-09-25 16:07:19 -07004095
Philipp Reisnerb411b362009-09-25 16:07:19 -07004096 c = (struct bm_xfer_ctx) {
4097 .bm_bits = drbd_bm_bits(mdev),
4098 .bm_words = drbd_bm_words(mdev),
4099 };
4100
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004101 for(;;) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004102 if (pi->cmd == P_BITMAP)
4103 err = receive_bitmap_plain(mdev, pi->size, pi->data, &c);
4104 else if (pi->cmd == P_COMPRESSED_BITMAP) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004105 /* MAYBE: sanity check that we speak proto >= 90,
4106 * and the feature is enabled! */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004107 struct p_compressed_bm *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004108
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004109 if (pi->size > DRBD_SOCKET_BUFFER_SIZE - drbd_header_size(tconn)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004110 dev_err(DEV, "ReportCBitmap packet too large\n");
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004111 err = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004112 goto out;
4113 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004114 if (pi->size <= sizeof(*p)) {
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004115 dev_err(DEV, "ReportCBitmap packet too small (l:%u)\n", pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004116 err = -EIO;
Andreas Gruenbacher78fcbda2010-12-10 22:18:27 +01004117 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004118 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004119 err = drbd_recv_all(mdev->tconn, p, pi->size);
4120 if (err)
4121 goto out;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004122 err = decode_bitmap_c(mdev, p, &c, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004123 } else {
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004124 dev_warn(DEV, "receive_bitmap: cmd neither ReportBitMap nor ReportCBitMap (is 0x%x)", pi->cmd);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004125 err = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004126 goto out;
4127 }
4128
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004129 c.packets[pi->cmd == P_BITMAP]++;
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004130 c.bytes[pi->cmd == P_BITMAP] += drbd_header_size(tconn) + pi->size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004131
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004132 if (err <= 0) {
4133 if (err < 0)
4134 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004135 break;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004136 }
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004137 err = drbd_recv_header(mdev->tconn, pi);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004138 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004139 goto out;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004140 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004141
4142 INFO_bm_xfer_stats(mdev, "receive", &c);
4143
4144 if (mdev->state.conn == C_WF_BITMAP_T) {
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01004145 enum drbd_state_rv rv;
4146
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004147 err = drbd_send_bitmap(mdev);
4148 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004149 goto out;
4150 /* Omit CS_ORDERED with this state transition to avoid deadlocks. */
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01004151 rv = _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
4152 D_ASSERT(rv == SS_SUCCESS);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004153 } else if (mdev->state.conn != C_WF_BITMAP_S) {
4154 /* admin may have requested C_DISCONNECTING,
4155 * other threads may have noticed network errors */
4156 dev_info(DEV, "unexpected cstate (%s) in receive_bitmap\n",
4157 drbd_conn_str(mdev->state.conn));
4158 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004159 err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004160
Philipp Reisnerb411b362009-09-25 16:07:19 -07004161 out:
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004162 drbd_bm_unlock(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004163 if (!err && mdev->state.conn == C_WF_BITMAP_S)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004164 drbd_start_resync(mdev, C_SYNC_SOURCE);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004165 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004166}
4167
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004168static int receive_skip(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004169{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004170 conn_warn(tconn, "skipping unknown optional packet type %d, l: %d!\n",
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004171 pi->cmd, pi->size);
Philipp Reisner2de876e2011-03-15 14:38:01 +01004172
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004173 return ignore_remaining_packet(tconn, pi);
Philipp Reisner2de876e2011-03-15 14:38:01 +01004174}
4175
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004176static int receive_UnplugRemote(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004177{
Philipp Reisnerb411b362009-09-25 16:07:19 -07004178 /* Make sure we've acked all the TCP data associated
4179 * with the data requests being unplugged */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004180 drbd_tcp_quickack(tconn->data.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004181
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004182 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004183}
4184
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004185static int receive_out_of_sync(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner73a01a12010-10-27 14:33:00 +02004186{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004187 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004188 struct p_block_desc *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004189
4190 mdev = vnr_to_mdev(tconn, pi->vnr);
4191 if (!mdev)
4192 return -EIO;
Philipp Reisner73a01a12010-10-27 14:33:00 +02004193
Lars Ellenbergf735e3632010-12-17 21:06:18 +01004194 switch (mdev->state.conn) {
4195 case C_WF_SYNC_UUID:
4196 case C_WF_BITMAP_T:
4197 case C_BEHIND:
4198 break;
4199 default:
4200 dev_err(DEV, "ASSERT FAILED cstate = %s, expected: WFSyncUUID|WFBitMapT|Behind\n",
4201 drbd_conn_str(mdev->state.conn));
4202 }
4203
Philipp Reisner73a01a12010-10-27 14:33:00 +02004204 drbd_set_out_of_sync(mdev, be64_to_cpu(p->sector), be32_to_cpu(p->blksize));
4205
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004206 return 0;
Philipp Reisner73a01a12010-10-27 14:33:00 +02004207}
4208
Philipp Reisner02918be2010-08-20 14:35:10 +02004209struct data_cmd {
4210 int expect_payload;
4211 size_t pkt_size;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004212 int (*fn)(struct drbd_tconn *, struct packet_info *);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004213};
4214
Philipp Reisner02918be2010-08-20 14:35:10 +02004215static struct data_cmd drbd_cmd_handler[] = {
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004216 [P_DATA] = { 1, sizeof(struct p_data), receive_Data },
4217 [P_DATA_REPLY] = { 1, sizeof(struct p_data), receive_DataReply },
4218 [P_RS_DATA_REPLY] = { 1, sizeof(struct p_data), receive_RSDataReply } ,
4219 [P_BARRIER] = { 0, sizeof(struct p_barrier), receive_Barrier } ,
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004220 [P_BITMAP] = { 1, 0, receive_bitmap } ,
4221 [P_COMPRESSED_BITMAP] = { 1, 0, receive_bitmap } ,
4222 [P_UNPLUG_REMOTE] = { 0, 0, receive_UnplugRemote },
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004223 [P_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
4224 [P_RS_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004225 [P_SYNC_PARAM] = { 1, 0, receive_SyncParam },
4226 [P_SYNC_PARAM89] = { 1, 0, receive_SyncParam },
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004227 [P_PROTOCOL] = { 1, sizeof(struct p_protocol), receive_protocol },
4228 [P_UUIDS] = { 0, sizeof(struct p_uuids), receive_uuids },
4229 [P_SIZES] = { 0, sizeof(struct p_sizes), receive_sizes },
4230 [P_STATE] = { 0, sizeof(struct p_state), receive_state },
4231 [P_STATE_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_state },
4232 [P_SYNC_UUID] = { 0, sizeof(struct p_rs_uuid), receive_sync_uuid },
4233 [P_OV_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
4234 [P_OV_REPLY] = { 1, sizeof(struct p_block_req), receive_DataRequest },
4235 [P_CSUM_RS_REQUEST] = { 1, sizeof(struct p_block_req), receive_DataRequest },
4236 [P_DELAY_PROBE] = { 0, sizeof(struct p_delay_probe93), receive_skip },
4237 [P_OUT_OF_SYNC] = { 0, sizeof(struct p_block_desc), receive_out_of_sync },
4238 [P_CONN_ST_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_conn_state },
Philipp Reisner036b17e2011-05-16 17:38:11 +02004239 [P_PROTOCOL_UPDATE] = { 1, sizeof(struct p_protocol), receive_protocol },
Philipp Reisner02918be2010-08-20 14:35:10 +02004240};
4241
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004242static void drbdd(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004243{
Philipp Reisner77351055b2011-02-07 17:24:26 +01004244 struct packet_info pi;
Philipp Reisner02918be2010-08-20 14:35:10 +02004245 size_t shs; /* sub header size */
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004246 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004247
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004248 while (get_t_state(&tconn->receiver) == RUNNING) {
Andreas Gruenbacherdeebe192011-03-25 00:01:04 +01004249 struct data_cmd *cmd;
4250
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004251 drbd_thread_current_set_cpu(&tconn->receiver);
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004252 if (drbd_recv_header(tconn, &pi))
Philipp Reisner02918be2010-08-20 14:35:10 +02004253 goto err_out;
4254
Andreas Gruenbacherdeebe192011-03-25 00:01:04 +01004255 cmd = &drbd_cmd_handler[pi.cmd];
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004256 if (unlikely(pi.cmd >= ARRAY_SIZE(drbd_cmd_handler) || !cmd->fn)) {
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004257 conn_err(tconn, "Unexpected data packet %s (0x%04x)",
4258 cmdname(pi.cmd), pi.cmd);
Philipp Reisner02918be2010-08-20 14:35:10 +02004259 goto err_out;
Lars Ellenberg0b33a912009-11-16 15:58:04 +01004260 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004261
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004262 shs = cmd->pkt_size;
4263 if (pi.size > shs && !cmd->expect_payload) {
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004264 conn_err(tconn, "No payload expected %s l:%d\n",
4265 cmdname(pi.cmd), pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02004266 goto err_out;
4267 }
4268
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004269 if (shs) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004270 err = drbd_recv_all_warn(tconn, pi.data, shs);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004271 if (err)
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004272 goto err_out;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004273 pi.size -= shs;
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004274 }
4275
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004276 err = cmd->fn(tconn, &pi);
4277 if (err) {
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004278 conn_err(tconn, "error receiving %s, e: %d l: %d!\n",
4279 cmdname(pi.cmd), err, pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02004280 goto err_out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004281 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004282 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004283 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004284
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004285 err_out:
4286 conn_request_state(tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004287}
4288
Philipp Reisner0e29d162011-02-18 14:23:11 +01004289void conn_flush_workqueue(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004290{
4291 struct drbd_wq_barrier barr;
4292
4293 barr.w.cb = w_prev_work_done;
Philipp Reisner0e29d162011-02-18 14:23:11 +01004294 barr.w.tconn = tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004295 init_completion(&barr.done);
Philipp Reisner0e29d162011-02-18 14:23:11 +01004296 drbd_queue_work(&tconn->data.work, &barr.w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004297 wait_for_completion(&barr.done);
4298}
4299
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004300static void conn_disconnect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004301{
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004302 struct drbd_conf *mdev;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004303 enum drbd_conns oc;
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004304 int vnr, rv = SS_UNKNOWN_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004305
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004306 if (tconn->cstate == C_STANDALONE)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004307 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004308
4309 /* asender does not clean up anything. it must not interfere, either */
Philipp Reisner360cc742011-02-08 14:29:53 +01004310 drbd_thread_stop(&tconn->asender);
4311 drbd_free_sock(tconn);
4312
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004313 rcu_read_lock();
4314 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
4315 kref_get(&mdev->kref);
4316 rcu_read_unlock();
4317 drbd_disconnected(mdev);
4318 kref_put(&mdev->kref, &drbd_minor_destroy);
4319 rcu_read_lock();
4320 }
4321 rcu_read_unlock();
4322
Philipp Reisner360cc742011-02-08 14:29:53 +01004323 conn_info(tconn, "Connection closed\n");
4324
Philipp Reisnercb703452011-03-24 11:03:07 +01004325 if (conn_highest_role(tconn) == R_PRIMARY && conn_highest_pdsk(tconn) >= D_UNKNOWN)
4326 conn_try_outdate_peer_async(tconn);
4327
Philipp Reisner360cc742011-02-08 14:29:53 +01004328 spin_lock_irq(&tconn->req_lock);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004329 oc = tconn->cstate;
4330 if (oc >= C_UNCONNECTED)
4331 rv = _conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE);
4332
Philipp Reisner360cc742011-02-08 14:29:53 +01004333 spin_unlock_irq(&tconn->req_lock);
4334
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02004335 if (oc == C_DISCONNECTING)
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +02004336 conn_request_state(tconn, NS(conn, C_STANDALONE), CS_VERBOSE | CS_HARD);
Philipp Reisner360cc742011-02-08 14:29:53 +01004337}
4338
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004339static int drbd_disconnected(struct drbd_conf *mdev)
Philipp Reisner360cc742011-02-08 14:29:53 +01004340{
Philipp Reisner360cc742011-02-08 14:29:53 +01004341 enum drbd_fencing_p fp;
4342 unsigned int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004343
Philipp Reisner85719572010-07-21 10:20:17 +02004344 /* wait for current activity to cease. */
Philipp Reisner87eeee42011-01-19 14:16:30 +01004345 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004346 _drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
4347 _drbd_wait_ee_list_empty(mdev, &mdev->sync_ee);
4348 _drbd_wait_ee_list_empty(mdev, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004349 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004350
4351 /* We do not have data structures that would allow us to
4352 * get the rs_pending_cnt down to 0 again.
4353 * * On C_SYNC_TARGET we do not have any data structures describing
4354 * the pending RSDataRequest's we have sent.
4355 * * On C_SYNC_SOURCE there is no data structure that tracks
4356 * the P_RS_DATA_REPLY blocks that we sent to the SyncTarget.
4357 * And no, it is not the sum of the reference counts in the
4358 * resync_LRU. The resync_LRU tracks the whole operation including
4359 * the disk-IO, while the rs_pending_cnt only tracks the blocks
4360 * on the fly. */
4361 drbd_rs_cancel_all(mdev);
4362 mdev->rs_total = 0;
4363 mdev->rs_failed = 0;
4364 atomic_set(&mdev->rs_pending_cnt, 0);
4365 wake_up(&mdev->misc_wait);
4366
Philipp Reisnerb411b362009-09-25 16:07:19 -07004367 del_timer_sync(&mdev->resync_timer);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004368 resync_timer_fn((unsigned long)mdev);
4369
Philipp Reisnerb411b362009-09-25 16:07:19 -07004370 /* wait for all w_e_end_data_req, w_e_end_rsdata_req, w_send_barrier,
4371 * w_make_resync_request etc. which may still be on the worker queue
4372 * to be "canceled" */
Philipp Reisnera21e9292011-02-08 15:08:49 +01004373 drbd_flush_workqueue(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004374
Andreas Gruenbachera990be42011-04-06 17:56:48 +02004375 drbd_finish_peer_reqs(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004376
4377 kfree(mdev->p_uuid);
4378 mdev->p_uuid = NULL;
4379
Philipp Reisner2aebfab2011-03-28 16:48:11 +02004380 if (!drbd_suspended(mdev))
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01004381 tl_clear(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004382
Philipp Reisnerb411b362009-09-25 16:07:19 -07004383 drbd_md_sync(mdev);
4384
4385 fp = FP_DONT_CARE;
4386 if (get_ldev(mdev)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02004387 rcu_read_lock();
4388 fp = rcu_dereference(mdev->ldev->disk_conf)->fencing;
4389 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07004390 put_ldev(mdev);
4391 }
4392
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004393 /* serialize with bitmap writeout triggered by the state change,
4394 * if any. */
4395 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
4396
Philipp Reisnerb411b362009-09-25 16:07:19 -07004397 /* tcp_close and release of sendpage pages can be deferred. I don't
4398 * want to use SO_LINGER, because apparently it can be deferred for
4399 * more than 20 seconds (longest time I checked).
4400 *
4401 * Actually we don't care for exactly when the network stack does its
4402 * put_page(), but release our reference on these pages right here.
4403 */
Andreas Gruenbacher7721f562011-04-06 17:14:02 +02004404 i = drbd_free_peer_reqs(mdev, &mdev->net_ee);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004405 if (i)
4406 dev_info(DEV, "net_ee not empty, killed %u entries\n", i);
Lars Ellenberg435f0742010-09-06 12:30:25 +02004407 i = atomic_read(&mdev->pp_in_use_by_net);
4408 if (i)
4409 dev_info(DEV, "pp_in_use_by_net = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004410 i = atomic_read(&mdev->pp_in_use);
4411 if (i)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02004412 dev_info(DEV, "pp_in_use = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004413
4414 D_ASSERT(list_empty(&mdev->read_ee));
4415 D_ASSERT(list_empty(&mdev->active_ee));
4416 D_ASSERT(list_empty(&mdev->sync_ee));
4417 D_ASSERT(list_empty(&mdev->done_ee));
4418
4419 /* ok, no more ee's on the fly, it is safe to reset the epoch_size */
4420 atomic_set(&mdev->current_epoch->epoch_size, 0);
4421 D_ASSERT(list_empty(&mdev->current_epoch->list));
Philipp Reisner360cc742011-02-08 14:29:53 +01004422
4423 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004424}
4425
4426/*
4427 * We support PRO_VERSION_MIN to PRO_VERSION_MAX. The protocol version
4428 * we can agree on is stored in agreed_pro_version.
4429 *
4430 * feature flags and the reserved array should be enough room for future
4431 * enhancements of the handshake protocol, and possible plugins...
4432 *
4433 * for now, they are expected to be zero, but ignored.
4434 */
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004435static int drbd_send_features(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004436{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004437 struct drbd_socket *sock;
4438 struct p_connection_features *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004439
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004440 sock = &tconn->data;
4441 p = conn_prepare_command(tconn, sock);
4442 if (!p)
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004443 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004444 memset(p, 0, sizeof(*p));
4445 p->protocol_min = cpu_to_be32(PRO_VERSION_MIN);
4446 p->protocol_max = cpu_to_be32(PRO_VERSION_MAX);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004447 return conn_send_command(tconn, sock, P_CONNECTION_FEATURES, sizeof(*p), NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004448}
4449
4450/*
4451 * return values:
4452 * 1 yes, we have a valid connection
4453 * 0 oops, did not work out, please try again
4454 * -1 peer talks different language,
4455 * no point in trying again, please go standalone.
4456 */
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004457static int drbd_do_features(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004458{
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004459 /* ASSERT current == tconn->receiver ... */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004460 struct p_connection_features *p;
4461 const int expect = sizeof(struct p_connection_features);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004462 struct packet_info pi;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004463 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004464
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004465 err = drbd_send_features(tconn);
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004466 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004467 return 0;
4468
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004469 err = drbd_recv_header(tconn, &pi);
4470 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004471 return 0;
4472
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004473 if (pi.cmd != P_CONNECTION_FEATURES) {
4474 conn_err(tconn, "expected ConnectionFeatures packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004475 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004476 return -1;
4477 }
4478
Philipp Reisner77351055b2011-02-07 17:24:26 +01004479 if (pi.size != expect) {
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004480 conn_err(tconn, "expected ConnectionFeatures length: %u, received: %u\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004481 expect, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004482 return -1;
4483 }
4484
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004485 p = pi.data;
4486 err = drbd_recv_all_warn(tconn, p, expect);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004487 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004488 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004489
Philipp Reisnerb411b362009-09-25 16:07:19 -07004490 p->protocol_min = be32_to_cpu(p->protocol_min);
4491 p->protocol_max = be32_to_cpu(p->protocol_max);
4492 if (p->protocol_max == 0)
4493 p->protocol_max = p->protocol_min;
4494
4495 if (PRO_VERSION_MAX < p->protocol_min ||
4496 PRO_VERSION_MIN > p->protocol_max)
4497 goto incompat;
4498
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004499 tconn->agreed_pro_version = min_t(int, PRO_VERSION_MAX, p->protocol_max);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004500
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004501 conn_info(tconn, "Handshake successful: "
4502 "Agreed network protocol version %d\n", tconn->agreed_pro_version);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004503
4504 return 1;
4505
4506 incompat:
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004507 conn_err(tconn, "incompatible DRBD dialects: "
Philipp Reisnerb411b362009-09-25 16:07:19 -07004508 "I support %d-%d, peer supports %d-%d\n",
4509 PRO_VERSION_MIN, PRO_VERSION_MAX,
4510 p->protocol_min, p->protocol_max);
4511 return -1;
4512}
4513
4514#if !defined(CONFIG_CRYPTO_HMAC) && !defined(CONFIG_CRYPTO_HMAC_MODULE)
Philipp Reisner13e60372011-02-08 09:54:40 +01004515static int drbd_do_auth(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004516{
4517 dev_err(DEV, "This kernel was build without CONFIG_CRYPTO_HMAC.\n");
4518 dev_err(DEV, "You need to disable 'cram-hmac-alg' in drbd.conf.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004519 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004520}
4521#else
4522#define CHALLENGE_LEN 64
Johannes Thomab10d96c2010-01-07 16:02:50 +01004523
4524/* Return value:
4525 1 - auth succeeded,
4526 0 - failed, try again (network error),
4527 -1 - auth failed, don't try again.
4528*/
4529
Philipp Reisner13e60372011-02-08 09:54:40 +01004530static int drbd_do_auth(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004531{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004532 struct drbd_socket *sock;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004533 char my_challenge[CHALLENGE_LEN]; /* 64 Bytes... */
4534 struct scatterlist sg;
4535 char *response = NULL;
4536 char *right_response = NULL;
4537 char *peers_ch = NULL;
Philipp Reisner44ed1672011-04-19 17:10:19 +02004538 unsigned int key_len;
4539 char secret[SHARED_SECRET_MAX]; /* 64 byte */
Philipp Reisnerb411b362009-09-25 16:07:19 -07004540 unsigned int resp_size;
4541 struct hash_desc desc;
Philipp Reisner77351055b2011-02-07 17:24:26 +01004542 struct packet_info pi;
Philipp Reisner44ed1672011-04-19 17:10:19 +02004543 struct net_conf *nc;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004544 int err, rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004545
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004546 /* FIXME: Put the challenge/response into the preallocated socket buffer. */
4547
Philipp Reisner44ed1672011-04-19 17:10:19 +02004548 rcu_read_lock();
4549 nc = rcu_dereference(tconn->net_conf);
4550 key_len = strlen(nc->shared_secret);
4551 memcpy(secret, nc->shared_secret, key_len);
4552 rcu_read_unlock();
4553
Philipp Reisner13e60372011-02-08 09:54:40 +01004554 desc.tfm = tconn->cram_hmac_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004555 desc.flags = 0;
4556
Philipp Reisner44ed1672011-04-19 17:10:19 +02004557 rv = crypto_hash_setkey(tconn->cram_hmac_tfm, (u8 *)secret, key_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004558 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004559 conn_err(tconn, "crypto_hash_setkey() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004560 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004561 goto fail;
4562 }
4563
4564 get_random_bytes(my_challenge, CHALLENGE_LEN);
4565
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004566 sock = &tconn->data;
4567 if (!conn_prepare_command(tconn, sock)) {
4568 rv = 0;
4569 goto fail;
4570 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004571 rv = !conn_send_command(tconn, sock, P_AUTH_CHALLENGE, 0,
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004572 my_challenge, CHALLENGE_LEN);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004573 if (!rv)
4574 goto fail;
4575
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004576 err = drbd_recv_header(tconn, &pi);
4577 if (err) {
4578 rv = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004579 goto fail;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004580 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004581
Philipp Reisner77351055b2011-02-07 17:24:26 +01004582 if (pi.cmd != P_AUTH_CHALLENGE) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004583 conn_err(tconn, "expected AuthChallenge packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004584 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004585 rv = 0;
4586 goto fail;
4587 }
4588
Philipp Reisner77351055b2011-02-07 17:24:26 +01004589 if (pi.size > CHALLENGE_LEN * 2) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004590 conn_err(tconn, "expected AuthChallenge payload too big.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004591 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004592 goto fail;
4593 }
4594
Philipp Reisner77351055b2011-02-07 17:24:26 +01004595 peers_ch = kmalloc(pi.size, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004596 if (peers_ch == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004597 conn_err(tconn, "kmalloc of peers_ch failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004598 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004599 goto fail;
4600 }
4601
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004602 err = drbd_recv_all_warn(tconn, peers_ch, pi.size);
4603 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004604 rv = 0;
4605 goto fail;
4606 }
4607
Philipp Reisner13e60372011-02-08 09:54:40 +01004608 resp_size = crypto_hash_digestsize(tconn->cram_hmac_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004609 response = kmalloc(resp_size, GFP_NOIO);
4610 if (response == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004611 conn_err(tconn, "kmalloc of response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004612 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004613 goto fail;
4614 }
4615
4616 sg_init_table(&sg, 1);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004617 sg_set_buf(&sg, peers_ch, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004618
4619 rv = crypto_hash_digest(&desc, &sg, sg.length, response);
4620 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004621 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004622 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004623 goto fail;
4624 }
4625
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004626 if (!conn_prepare_command(tconn, sock)) {
4627 rv = 0;
4628 goto fail;
4629 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004630 rv = !conn_send_command(tconn, sock, P_AUTH_RESPONSE, 0,
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004631 response, resp_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004632 if (!rv)
4633 goto fail;
4634
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004635 err = drbd_recv_header(tconn, &pi);
4636 if (err) {
4637 rv = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004638 goto fail;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004639 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004640
Philipp Reisner77351055b2011-02-07 17:24:26 +01004641 if (pi.cmd != P_AUTH_RESPONSE) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004642 conn_err(tconn, "expected AuthResponse packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004643 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004644 rv = 0;
4645 goto fail;
4646 }
4647
Philipp Reisner77351055b2011-02-07 17:24:26 +01004648 if (pi.size != resp_size) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004649 conn_err(tconn, "expected AuthResponse payload of wrong size\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004650 rv = 0;
4651 goto fail;
4652 }
4653
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004654 err = drbd_recv_all_warn(tconn, response , resp_size);
4655 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004656 rv = 0;
4657 goto fail;
4658 }
4659
4660 right_response = kmalloc(resp_size, GFP_NOIO);
Julia Lawall2d1ee872009-12-27 22:27:11 +01004661 if (right_response == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004662 conn_err(tconn, "kmalloc of right_response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004663 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004664 goto fail;
4665 }
4666
4667 sg_set_buf(&sg, my_challenge, CHALLENGE_LEN);
4668
4669 rv = crypto_hash_digest(&desc, &sg, sg.length, right_response);
4670 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004671 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004672 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004673 goto fail;
4674 }
4675
4676 rv = !memcmp(response, right_response, resp_size);
4677
4678 if (rv)
Philipp Reisner44ed1672011-04-19 17:10:19 +02004679 conn_info(tconn, "Peer authenticated using %d bytes HMAC\n",
4680 resp_size);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004681 else
4682 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004683
4684 fail:
4685 kfree(peers_ch);
4686 kfree(response);
4687 kfree(right_response);
4688
4689 return rv;
4690}
4691#endif
4692
4693int drbdd_init(struct drbd_thread *thi)
4694{
Philipp Reisner392c8802011-02-09 10:33:31 +01004695 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004696 int h;
4697
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004698 conn_info(tconn, "receiver (re)started\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004699
4700 do {
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004701 h = conn_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004702 if (h == 0) {
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004703 conn_disconnect(tconn);
Philipp Reisner20ee6392011-01-18 15:28:59 +01004704 schedule_timeout_interruptible(HZ);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004705 }
4706 if (h == -1) {
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004707 conn_warn(tconn, "Discarding network configuration.\n");
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004708 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004709 }
4710 } while (h == 0);
4711
Philipp Reisner91fd4da2011-04-20 17:47:29 +02004712 if (h > 0)
4713 drbdd(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004714
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004715 conn_disconnect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004716
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004717 conn_info(tconn, "receiver terminated\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004718 return 0;
4719}
4720
4721/* ********* acknowledge sender ******** */
4722
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004723static int got_conn_RqSReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004724{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004725 struct p_req_state_reply *p = pi->data;
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004726 int retcode = be32_to_cpu(p->retcode);
4727
4728 if (retcode >= SS_SUCCESS) {
4729 set_bit(CONN_WD_ST_CHG_OKAY, &tconn->flags);
4730 } else {
4731 set_bit(CONN_WD_ST_CHG_FAIL, &tconn->flags);
4732 conn_err(tconn, "Requested state change failed by peer: %s (%d)\n",
4733 drbd_set_st_err_str(retcode), retcode);
4734 }
4735 wake_up(&tconn->ping_wait);
4736
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004737 return 0;
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004738}
4739
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004740static int got_RqSReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004741{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004742 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004743 struct p_req_state_reply *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004744 int retcode = be32_to_cpu(p->retcode);
4745
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004746 mdev = vnr_to_mdev(tconn, pi->vnr);
4747 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004748 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004749
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004750 if (retcode >= SS_SUCCESS) {
4751 set_bit(CL_ST_CHG_SUCCESS, &mdev->flags);
4752 } else {
4753 set_bit(CL_ST_CHG_FAIL, &mdev->flags);
4754 dev_err(DEV, "Requested state change failed by peer: %s (%d)\n",
4755 drbd_set_st_err_str(retcode), retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004756 }
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004757 wake_up(&mdev->state_wait);
4758
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004759 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004760}
4761
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004762static int got_Ping(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004763{
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004764 return drbd_send_ping_ack(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004765
4766}
4767
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004768static int got_PingAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004769{
4770 /* restore idle timeout */
Philipp Reisner2a67d8b2011-02-09 14:10:32 +01004771 tconn->meta.socket->sk->sk_rcvtimeo = tconn->net_conf->ping_int*HZ;
4772 if (!test_and_set_bit(GOT_PING_ACK, &tconn->flags))
4773 wake_up(&tconn->ping_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004774
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004775 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004776}
4777
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004778static int got_IsInSync(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004779{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004780 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004781 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004782 sector_t sector = be64_to_cpu(p->sector);
4783 int blksize = be32_to_cpu(p->blksize);
4784
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004785 mdev = vnr_to_mdev(tconn, pi->vnr);
4786 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004787 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004788
Philipp Reisner31890f42011-01-19 14:12:51 +01004789 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004790
4791 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4792
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004793 if (get_ldev(mdev)) {
4794 drbd_rs_complete_io(mdev, sector);
4795 drbd_set_in_sync(mdev, sector, blksize);
4796 /* rs_same_csums is supposed to count in units of BM_BLOCK_SIZE */
4797 mdev->rs_same_csum += (blksize >> BM_BLOCK_SHIFT);
4798 put_ldev(mdev);
4799 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004800 dec_rs_pending(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02004801 atomic_add(blksize >> 9, &mdev->rs_sect_in);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004802
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004803 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004804}
4805
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004806static int
4807validate_req_change_req_state(struct drbd_conf *mdev, u64 id, sector_t sector,
4808 struct rb_root *root, const char *func,
4809 enum drbd_req_event what, bool missing_ok)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004810{
4811 struct drbd_request *req;
4812 struct bio_and_error m;
4813
Philipp Reisner87eeee42011-01-19 14:16:30 +01004814 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004815 req = find_request(mdev, root, id, sector, missing_ok, func);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004816 if (unlikely(!req)) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01004817 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004818 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004819 }
4820 __req_mod(req, what, &m);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004821 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004822
4823 if (m.bio)
4824 complete_master_bio(mdev, &m);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004825 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004826}
4827
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004828static int got_BlockAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004829{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004830 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004831 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004832 sector_t sector = be64_to_cpu(p->sector);
4833 int blksize = be32_to_cpu(p->blksize);
4834 enum drbd_req_event what;
4835
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004836 mdev = vnr_to_mdev(tconn, pi->vnr);
4837 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004838 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004839
Philipp Reisnerb411b362009-09-25 16:07:19 -07004840 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4841
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01004842 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004843 drbd_set_in_sync(mdev, sector, blksize);
4844 dec_rs_pending(mdev);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004845 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004846 }
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004847 switch (pi->cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004848 case P_RS_WRITE_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004849 what = WRITE_ACKED_BY_PEER_AND_SIS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004850 break;
4851 case P_WRITE_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004852 what = WRITE_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004853 break;
4854 case P_RECV_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004855 what = RECV_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004856 break;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004857 case P_DISCARD_WRITE:
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004858 what = DISCARD_WRITE;
4859 break;
4860 case P_RETRY_WRITE:
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004861 what = POSTPONE_WRITE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004862 break;
4863 default:
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004864 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -07004865 }
4866
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004867 return validate_req_change_req_state(mdev, p->block_id, sector,
4868 &mdev->write_requests, __func__,
4869 what, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004870}
4871
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004872static int got_NegAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004873{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004874 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004875 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004876 sector_t sector = be64_to_cpu(p->sector);
Philipp Reisner2deb8332011-01-17 18:39:18 +01004877 int size = be32_to_cpu(p->blksize);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004878 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004879
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004880 mdev = vnr_to_mdev(tconn, pi->vnr);
4881 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004882 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004883
Philipp Reisnerb411b362009-09-25 16:07:19 -07004884 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4885
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01004886 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004887 dec_rs_pending(mdev);
4888 drbd_rs_failed_io(mdev, sector, size);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004889 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004890 }
Philipp Reisner2deb8332011-01-17 18:39:18 +01004891
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004892 err = validate_req_change_req_state(mdev, p->block_id, sector,
4893 &mdev->write_requests, __func__,
Philipp Reisner303d1442011-04-13 16:24:47 -07004894 NEG_ACKED, true);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004895 if (err) {
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004896 /* Protocol A has no P_WRITE_ACKs, but has P_NEG_ACKs.
4897 The master bio might already be completed, therefore the
4898 request is no longer in the collision hash. */
4899 /* In Protocol B we might already have got a P_RECV_ACK
4900 but then get a P_NEG_ACK afterwards. */
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004901 drbd_set_out_of_sync(mdev, sector, size);
Philipp Reisner2deb8332011-01-17 18:39:18 +01004902 }
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004903 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004904}
4905
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004906static int got_NegDReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004907{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004908 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004909 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004910 sector_t sector = be64_to_cpu(p->sector);
4911
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004912 mdev = vnr_to_mdev(tconn, pi->vnr);
4913 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004914 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004915
Philipp Reisnerb411b362009-09-25 16:07:19 -07004916 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004917
Philipp Reisnerb411b362009-09-25 16:07:19 -07004918 dev_err(DEV, "Got NegDReply; Sector %llus, len %u; Fail original request.\n",
4919 (unsigned long long)sector, be32_to_cpu(p->blksize));
4920
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004921 return validate_req_change_req_state(mdev, p->block_id, sector,
4922 &mdev->read_requests, __func__,
4923 NEG_ACKED, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004924}
4925
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004926static int got_NegRSDReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004927{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004928 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004929 sector_t sector;
4930 int size;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004931 struct p_block_ack *p = pi->data;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004932
4933 mdev = vnr_to_mdev(tconn, pi->vnr);
4934 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004935 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004936
4937 sector = be64_to_cpu(p->sector);
4938 size = be32_to_cpu(p->blksize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004939
4940 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4941
4942 dec_rs_pending(mdev);
4943
4944 if (get_ldev_if_state(mdev, D_FAILED)) {
4945 drbd_rs_complete_io(mdev, sector);
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004946 switch (pi->cmd) {
Philipp Reisnerd612d302010-12-27 10:53:28 +01004947 case P_NEG_RS_DREPLY:
4948 drbd_rs_failed_io(mdev, sector, size);
4949 case P_RS_CANCEL:
4950 break;
4951 default:
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004952 BUG();
Philipp Reisnerd612d302010-12-27 10:53:28 +01004953 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004954 put_ldev(mdev);
4955 }
4956
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004957 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004958}
4959
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004960static int got_BarrierAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004961{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004962 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004963 struct p_barrier_ack *p = pi->data;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004964
4965 mdev = vnr_to_mdev(tconn, pi->vnr);
4966 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004967 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004968
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01004969 tl_release(mdev->tconn, p->barrier, be32_to_cpu(p->set_size));
Philipp Reisnerb411b362009-09-25 16:07:19 -07004970
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02004971 if (mdev->state.conn == C_AHEAD &&
4972 atomic_read(&mdev->ap_in_flight) == 0 &&
Philipp Reisner370a43e2011-01-14 16:03:11 +01004973 !test_and_set_bit(AHEAD_TO_SYNC_SOURCE, &mdev->current_epoch->flags)) {
4974 mdev->start_resync_timer.expires = jiffies + HZ;
4975 add_timer(&mdev->start_resync_timer);
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02004976 }
4977
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004978 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004979}
4980
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004981static int got_OVResult(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004982{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004983 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004984 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004985 struct drbd_work *w;
4986 sector_t sector;
4987 int size;
4988
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004989 mdev = vnr_to_mdev(tconn, pi->vnr);
4990 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004991 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004992
Philipp Reisnerb411b362009-09-25 16:07:19 -07004993 sector = be64_to_cpu(p->sector);
4994 size = be32_to_cpu(p->blksize);
4995
4996 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4997
4998 if (be64_to_cpu(p->block_id) == ID_OUT_OF_SYNC)
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01004999 drbd_ov_out_of_sync_found(mdev, sector, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005000 else
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005001 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005002
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005003 if (!get_ldev(mdev))
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005004 return 0;
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005005
Philipp Reisnerb411b362009-09-25 16:07:19 -07005006 drbd_rs_complete_io(mdev, sector);
5007 dec_rs_pending(mdev);
5008
Lars Ellenbergea5442a2010-11-05 09:48:01 +01005009 --mdev->ov_left;
5010
5011 /* let's advance progress step marks only for every other megabyte */
5012 if ((mdev->ov_left & 0x200) == 0x200)
5013 drbd_advance_rs_marks(mdev, mdev->ov_left);
5014
5015 if (mdev->ov_left == 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07005016 w = kmalloc(sizeof(*w), GFP_NOIO);
5017 if (w) {
5018 w->cb = w_ov_finished;
Philipp Reisnera21e9292011-02-08 15:08:49 +01005019 w->mdev = mdev;
Philipp Reisnere42325a2011-01-19 13:55:45 +01005020 drbd_queue_work_front(&mdev->tconn->data.work, w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005021 } else {
5022 dev_err(DEV, "kmalloc(w) failed.");
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005023 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005024 drbd_resync_finished(mdev);
5025 }
5026 }
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005027 put_ldev(mdev);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005028 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005029}
5030
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005031static int got_skip(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner0ced55a2010-04-30 15:26:20 +02005032{
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005033 return 0;
Philipp Reisner0ced55a2010-04-30 15:26:20 +02005034}
5035
Andreas Gruenbachera990be42011-04-06 17:56:48 +02005036static int tconn_finish_peer_reqs(struct drbd_tconn *tconn)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005037{
Philipp Reisner082a3432011-03-15 16:05:42 +01005038 struct drbd_conf *mdev;
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005039 int vnr, not_empty = 0;
Philipp Reisner32862ec2011-02-08 16:41:01 +01005040
5041 do {
5042 clear_bit(SIGNAL_ASENDER, &tconn->flags);
5043 flush_signals(current);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005044
5045 rcu_read_lock();
5046 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
5047 kref_get(&mdev->kref);
5048 rcu_read_unlock();
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07005049 if (drbd_finish_peer_reqs(mdev)) {
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005050 kref_put(&mdev->kref, &drbd_minor_destroy);
5051 return 1;
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07005052 }
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005053 kref_put(&mdev->kref, &drbd_minor_destroy);
5054 rcu_read_lock();
Philipp Reisner082a3432011-03-15 16:05:42 +01005055 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005056 set_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisner082a3432011-03-15 16:05:42 +01005057
5058 spin_lock_irq(&tconn->req_lock);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005059 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Philipp Reisner082a3432011-03-15 16:05:42 +01005060 not_empty = !list_empty(&mdev->done_ee);
5061 if (not_empty)
5062 break;
5063 }
5064 spin_unlock_irq(&tconn->req_lock);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005065 rcu_read_unlock();
Philipp Reisner32862ec2011-02-08 16:41:01 +01005066 } while (not_empty);
5067
5068 return 0;
5069}
5070
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005071struct asender_cmd {
5072 size_t pkt_size;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005073 int (*fn)(struct drbd_tconn *tconn, struct packet_info *);
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005074};
5075
5076static struct asender_cmd asender_tbl[] = {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005077 [P_PING] = { 0, got_Ping },
5078 [P_PING_ACK] = { 0, got_PingAck },
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005079 [P_RECV_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5080 [P_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5081 [P_RS_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5082 [P_DISCARD_WRITE] = { sizeof(struct p_block_ack), got_BlockAck },
5083 [P_NEG_ACK] = { sizeof(struct p_block_ack), got_NegAck },
5084 [P_NEG_DREPLY] = { sizeof(struct p_block_ack), got_NegDReply },
5085 [P_NEG_RS_DREPLY] = { sizeof(struct p_block_ack), got_NegRSDReply },
5086 [P_OV_RESULT] = { sizeof(struct p_block_ack), got_OVResult },
5087 [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), got_BarrierAck },
5088 [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), got_RqSReply },
5089 [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), got_IsInSync },
5090 [P_DELAY_PROBE] = { sizeof(struct p_delay_probe93), got_skip },
5091 [P_RS_CANCEL] = { sizeof(struct p_block_ack), got_NegRSDReply },
5092 [P_CONN_ST_CHG_REPLY]={ sizeof(struct p_req_state_reply), got_conn_RqSReply },
5093 [P_RETRY_WRITE] = { sizeof(struct p_block_ack), got_BlockAck },
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005094};
5095
Philipp Reisnerb411b362009-09-25 16:07:19 -07005096int drbd_asender(struct drbd_thread *thi)
5097{
Philipp Reisner392c8802011-02-09 10:33:31 +01005098 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005099 struct asender_cmd *cmd = NULL;
Philipp Reisner77351055b2011-02-07 17:24:26 +01005100 struct packet_info pi;
Philipp Reisner257d0af2011-01-26 12:15:29 +01005101 int rv;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005102 void *buf = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005103 int received = 0;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005104 unsigned int header_size = drbd_header_size(tconn);
5105 int expect = header_size;
Philipp Reisner44ed1672011-04-19 17:10:19 +02005106 bool ping_timeout_active = false;
5107 struct net_conf *nc;
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005108 int ping_timeo, tcp_cork, ping_int;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005109
Philipp Reisnerb411b362009-09-25 16:07:19 -07005110 current->policy = SCHED_RR; /* Make this a realtime task! */
5111 current->rt_priority = 2; /* more important than all other tasks */
5112
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +01005113 while (get_t_state(thi) == RUNNING) {
Philipp Reisner80822282011-02-08 12:46:30 +01005114 drbd_thread_current_set_cpu(thi);
Philipp Reisner44ed1672011-04-19 17:10:19 +02005115
5116 rcu_read_lock();
5117 nc = rcu_dereference(tconn->net_conf);
5118 ping_timeo = nc->ping_timeo;
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005119 tcp_cork = nc->tcp_cork;
Philipp Reisner44ed1672011-04-19 17:10:19 +02005120 ping_int = nc->ping_int;
5121 rcu_read_unlock();
5122
Philipp Reisner32862ec2011-02-08 16:41:01 +01005123 if (test_and_clear_bit(SEND_PING, &tconn->flags)) {
Andreas Gruenbachera17647a2011-04-01 12:49:42 +02005124 if (drbd_send_ping(tconn)) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005125 conn_err(tconn, "drbd_send_ping has failed\n");
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01005126 goto reconnect;
5127 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02005128 tconn->meta.socket->sk->sk_rcvtimeo = ping_timeo * HZ / 10;
5129 ping_timeout_active = true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005130 }
5131
Philipp Reisner32862ec2011-02-08 16:41:01 +01005132 /* TODO: conditionally cork; it may hurt latency if we cork without
5133 much to send */
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005134 if (tcp_cork)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005135 drbd_tcp_cork(tconn->meta.socket);
Andreas Gruenbachera990be42011-04-06 17:56:48 +02005136 if (tconn_finish_peer_reqs(tconn)) {
5137 conn_err(tconn, "tconn_finish_peer_reqs() failed\n");
Philipp Reisner32862ec2011-02-08 16:41:01 +01005138 goto reconnect;
Philipp Reisner082a3432011-03-15 16:05:42 +01005139 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005140 /* but unconditionally uncork unless disabled */
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005141 if (tcp_cork)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005142 drbd_tcp_uncork(tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005143
5144 /* short circuit, recv_msg would return EINTR anyways. */
5145 if (signal_pending(current))
5146 continue;
5147
Philipp Reisner32862ec2011-02-08 16:41:01 +01005148 rv = drbd_recv_short(tconn->meta.socket, buf, expect-received, 0);
5149 clear_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005150
5151 flush_signals(current);
5152
5153 /* Note:
5154 * -EINTR (on meta) we got a signal
5155 * -EAGAIN (on meta) rcvtimeo expired
5156 * -ECONNRESET other side closed the connection
5157 * -ERESTARTSYS (on data) we got a signal
5158 * rv < 0 other than above: unexpected error!
5159 * rv == expected: full header or command
5160 * rv < expected: "woken" by signal during receive
5161 * rv == 0 : "connection shut down by peer"
5162 */
5163 if (likely(rv > 0)) {
5164 received += rv;
5165 buf += rv;
5166 } else if (rv == 0) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005167 conn_err(tconn, "meta connection shut down by peer.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005168 goto reconnect;
5169 } else if (rv == -EAGAIN) {
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02005170 /* If the data socket received something meanwhile,
5171 * that is good enough: peer is still alive. */
Philipp Reisner32862ec2011-02-08 16:41:01 +01005172 if (time_after(tconn->last_received,
5173 jiffies - tconn->meta.socket->sk->sk_rcvtimeo))
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02005174 continue;
Lars Ellenbergf36af182011-03-09 22:44:55 +01005175 if (ping_timeout_active) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005176 conn_err(tconn, "PingAck did not arrive in time.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005177 goto reconnect;
5178 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005179 set_bit(SEND_PING, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005180 continue;
5181 } else if (rv == -EINTR) {
5182 continue;
5183 } else {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005184 conn_err(tconn, "sock_recvmsg returned %d\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005185 goto reconnect;
5186 }
5187
5188 if (received == expect && cmd == NULL) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005189 if (decode_header(tconn, tconn->meta.rbuf, &pi))
Philipp Reisnerb411b362009-09-25 16:07:19 -07005190 goto reconnect;
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005191 cmd = &asender_tbl[pi.cmd];
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005192 if (pi.cmd >= ARRAY_SIZE(asender_tbl) || !cmd->fn) {
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02005193 conn_err(tconn, "Unexpected meta packet %s (0x%04x)\n",
5194 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005195 goto disconnect;
5196 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005197 expect = header_size + cmd->pkt_size;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005198 if (pi.size != expect - header_size) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005199 conn_err(tconn, "Wrong packet size on meta (c: %d, l: %d)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01005200 pi.cmd, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005201 goto reconnect;
Philipp Reisner257d0af2011-01-26 12:15:29 +01005202 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005203 }
5204 if (received == expect) {
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005205 bool err;
Philipp Reisnera4fbda82011-03-16 11:13:17 +01005206
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005207 err = cmd->fn(tconn, &pi);
5208 if (err) {
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005209 conn_err(tconn, "%pf failed\n", cmd->fn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005210 goto reconnect;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005211 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005212
Philipp Reisnera4fbda82011-03-16 11:13:17 +01005213 tconn->last_received = jiffies;
5214
Philipp Reisner44ed1672011-04-19 17:10:19 +02005215 if (cmd == &asender_tbl[P_PING_ACK]) {
5216 /* restore idle timeout */
5217 tconn->meta.socket->sk->sk_rcvtimeo = ping_int * HZ;
5218 ping_timeout_active = false;
5219 }
Lars Ellenbergf36af182011-03-09 22:44:55 +01005220
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005221 buf = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005222 received = 0;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005223 expect = header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005224 cmd = NULL;
5225 }
5226 }
5227
5228 if (0) {
5229reconnect:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01005230 conn_request_state(tconn, NS(conn, C_NETWORK_FAILURE), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005231 }
5232 if (0) {
5233disconnect:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01005234 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005235 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005236 clear_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005237
Philipp Reisner32862ec2011-02-08 16:41:01 +01005238 conn_info(tconn, "asender terminated\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005239
5240 return 0;
5241}