blob: f6d1ff2e3ab9b7e05a7bd6bf2b62e0127b1799b9 [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)
Philipp Reisner43de7c82011-11-10 13:16:13 +0100828 err = drbd_send_current_state(mdev);
Philipp Reisner907599e2011-02-08 11:25:37 +0100829 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
830 clear_bit(RESIZE_PENDING, &mdev->flags);
Philipp Reisner8b924f12011-03-01 11:08:28 +0100831 mod_timer(&mdev->request_timer, jiffies + HZ); /* just start it here. */
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100832 return err;
Philipp Reisner907599e2011-02-08 11:25:37 +0100833}
834
Philipp Reisnerb411b362009-09-25 16:07:19 -0700835/*
836 * return values:
837 * 1 yes, we have a valid connection
838 * 0 oops, did not work out, please try again
839 * -1 peer talks different language,
840 * no point in trying again, please go standalone.
841 * -2 We do not have a network config...
842 */
Philipp Reisner81fa2e62011-05-04 15:10:30 +0200843static int conn_connect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700844{
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200845 struct socket *sock, *msock;
Philipp Reisnerc141ebd2011-05-05 16:13:10 +0200846 struct drbd_conf *mdev;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200847 struct net_conf *nc;
Philipp Reisnerc141ebd2011-05-05 16:13:10 +0200848 int vnr, timeout, try, h, ok;
Philipp Reisner08b165b2011-09-05 16:22:33 +0200849 bool discard_my_data;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700850
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100851 if (conn_request_state(tconn, NS(conn, C_WF_CONNECTION), CS_VERBOSE) < SS_SUCCESS)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700852 return -2;
853
Philipp Reisner907599e2011-02-08 11:25:37 +0100854 clear_bit(DISCARD_CONCURRENT, &tconn->flags);
Andreas Gruenbacher0916e0e2011-03-21 14:10:15 +0100855
856 /* Assume that the peer only understands protocol 80 until we know better. */
857 tconn->agreed_pro_version = 80;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700858
Philipp Reisnerb411b362009-09-25 16:07:19 -0700859 do {
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200860 struct socket *s;
861
Philipp Reisnerb411b362009-09-25 16:07:19 -0700862 for (try = 0;;) {
863 /* 3 tries, this should take less than a second! */
Philipp Reisner907599e2011-02-08 11:25:37 +0100864 s = drbd_try_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700865 if (s || ++try >= 3)
866 break;
867 /* give the other side time to call bind() & listen() */
Philipp Reisner20ee6392011-01-18 15:28:59 +0100868 schedule_timeout_interruptible(HZ / 10);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700869 }
870
871 if (s) {
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200872 if (!tconn->data.socket) {
873 tconn->data.socket = s;
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200874 send_first_packet(tconn, &tconn->data, P_INITIAL_DATA);
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200875 } else if (!tconn->meta.socket) {
876 tconn->meta.socket = s;
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200877 send_first_packet(tconn, &tconn->meta, P_INITIAL_META);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700878 } else {
Philipp Reisner81fa2e62011-05-04 15:10:30 +0200879 conn_err(tconn, "Logic error in conn_connect()\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700880 goto out_release_sockets;
881 }
882 }
883
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200884 if (tconn->data.socket && tconn->meta.socket) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100885 schedule_timeout_interruptible(tconn->net_conf->ping_timeo*HZ/10);
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200886 ok = drbd_socket_okay(&tconn->data.socket);
887 ok = drbd_socket_okay(&tconn->meta.socket) && ok;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700888 if (ok)
889 break;
890 }
891
892retry:
Philipp Reisner907599e2011-02-08 11:25:37 +0100893 s = drbd_wait_for_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700894 if (s) {
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200895 try = receive_first_packet(tconn, s);
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200896 drbd_socket_okay(&tconn->data.socket);
897 drbd_socket_okay(&tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700898 switch (try) {
Andreas Gruenbachere5d6f332011-03-28 16:44:40 +0200899 case P_INITIAL_DATA:
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200900 if (tconn->data.socket) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100901 conn_warn(tconn, "initial packet S crossed\n");
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200902 sock_release(tconn->data.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700903 }
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200904 tconn->data.socket = s;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700905 break;
Andreas Gruenbachere5d6f332011-03-28 16:44:40 +0200906 case P_INITIAL_META:
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200907 if (tconn->meta.socket) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100908 conn_warn(tconn, "initial packet M crossed\n");
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200909 sock_release(tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700910 }
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200911 tconn->meta.socket = s;
Philipp Reisner907599e2011-02-08 11:25:37 +0100912 set_bit(DISCARD_CONCURRENT, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700913 break;
914 default:
Philipp Reisner907599e2011-02-08 11:25:37 +0100915 conn_warn(tconn, "Error receiving initial packet\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700916 sock_release(s);
917 if (random32() & 1)
918 goto retry;
919 }
920 }
921
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100922 if (tconn->cstate <= C_DISCONNECTING)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700923 goto out_release_sockets;
924 if (signal_pending(current)) {
925 flush_signals(current);
926 smp_rmb();
Philipp Reisner907599e2011-02-08 11:25:37 +0100927 if (get_t_state(&tconn->receiver) == EXITING)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700928 goto out_release_sockets;
929 }
930
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200931 if (tconn->data.socket && &tconn->meta.socket) {
932 ok = drbd_socket_okay(&tconn->data.socket);
933 ok = drbd_socket_okay(&tconn->meta.socket) && ok;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700934 if (ok)
935 break;
936 }
937 } while (1);
938
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200939 sock = tconn->data.socket;
940 msock = tconn->meta.socket;
941
Philipp Reisnerb411b362009-09-25 16:07:19 -0700942 msock->sk->sk_reuse = 1; /* SO_REUSEADDR */
943 sock->sk->sk_reuse = 1; /* SO_REUSEADDR */
944
945 sock->sk->sk_allocation = GFP_NOIO;
946 msock->sk->sk_allocation = GFP_NOIO;
947
948 sock->sk->sk_priority = TC_PRIO_INTERACTIVE_BULK;
949 msock->sk->sk_priority = TC_PRIO_INTERACTIVE;
950
Philipp Reisnerb411b362009-09-25 16:07:19 -0700951 /* NOT YET ...
Philipp Reisner907599e2011-02-08 11:25:37 +0100952 * sock->sk->sk_sndtimeo = tconn->net_conf->timeout*HZ/10;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700953 * sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
Andreas Gruenbacher60381782011-03-28 17:05:50 +0200954 * first set it to the P_CONNECTION_FEATURES timeout,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700955 * which we set to 4x the configured ping_timeout. */
Philipp Reisner44ed1672011-04-19 17:10:19 +0200956 rcu_read_lock();
957 nc = rcu_dereference(tconn->net_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700958
Philipp Reisner44ed1672011-04-19 17:10:19 +0200959 sock->sk->sk_sndtimeo =
960 sock->sk->sk_rcvtimeo = nc->ping_timeo*4*HZ/10;
961
962 msock->sk->sk_rcvtimeo = nc->ping_int*HZ;
963 timeout = nc->timeout * HZ / 10;
Philipp Reisner08b165b2011-09-05 16:22:33 +0200964 discard_my_data = nc->discard_my_data;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200965 rcu_read_unlock();
966
967 msock->sk->sk_sndtimeo = timeout;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700968
969 /* we don't want delays.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300970 * we use TCP_CORK where appropriate, though */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700971 drbd_tcp_nodelay(sock);
972 drbd_tcp_nodelay(msock);
973
Philipp Reisner907599e2011-02-08 11:25:37 +0100974 tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700975
Andreas Gruenbacher60381782011-03-28 17:05:50 +0200976 h = drbd_do_features(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700977 if (h <= 0)
978 return h;
979
Philipp Reisner907599e2011-02-08 11:25:37 +0100980 if (tconn->cram_hmac_tfm) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700981 /* drbd_request_state(mdev, NS(conn, WFAuth)); */
Philipp Reisner907599e2011-02-08 11:25:37 +0100982 switch (drbd_do_auth(tconn)) {
Johannes Thomab10d96c2010-01-07 16:02:50 +0100983 case -1:
Philipp Reisner907599e2011-02-08 11:25:37 +0100984 conn_err(tconn, "Authentication of peer failed\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700985 return -1;
Johannes Thomab10d96c2010-01-07 16:02:50 +0100986 case 0:
Philipp Reisner907599e2011-02-08 11:25:37 +0100987 conn_err(tconn, "Authentication of peer failed, trying again.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +0100988 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700989 }
990 }
991
Philipp Reisner44ed1672011-04-19 17:10:19 +0200992 sock->sk->sk_sndtimeo = timeout;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700993 sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
994
Andreas Gruenbacher387eb302011-03-16 01:05:37 +0100995 if (drbd_send_protocol(tconn) == -EOPNOTSUPP)
Philipp Reisner7e2455c2010-04-22 14:50:23 +0200996 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700997
Philipp Reisnerc141ebd2011-05-05 16:13:10 +0200998 rcu_read_lock();
999 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1000 kref_get(&mdev->kref);
1001 rcu_read_unlock();
Philipp Reisner08b165b2011-09-05 16:22:33 +02001002
1003 if (discard_my_data)
1004 set_bit(DISCARD_MY_DATA, &mdev->flags);
1005 else
1006 clear_bit(DISCARD_MY_DATA, &mdev->flags);
1007
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02001008 drbd_connected(mdev);
1009 kref_put(&mdev->kref, &drbd_minor_destroy);
1010 rcu_read_lock();
1011 }
1012 rcu_read_unlock();
1013
Philipp Reisner823bd832012-11-08 15:04:36 +01001014 if (conn_request_state(tconn, NS(conn, C_WF_REPORT_PARAMS), CS_VERBOSE) < SS_SUCCESS)
1015 return 0;
1016
1017 drbd_thread_start(&tconn->asender);
1018
Philipp Reisner08b165b2011-09-05 16:22:33 +02001019 mutex_lock(&tconn->conf_update);
1020 /* The discard_my_data flag is a single-shot modifier to the next
1021 * connection attempt, the handshake of which is now well underway.
1022 * No need for rcu style copying of the whole struct
1023 * just to clear a single value. */
1024 tconn->net_conf->discard_my_data = 0;
1025 mutex_unlock(&tconn->conf_update);
1026
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07001027 return h;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001028
1029out_release_sockets:
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +02001030 if (tconn->data.socket) {
1031 sock_release(tconn->data.socket);
1032 tconn->data.socket = NULL;
1033 }
1034 if (tconn->meta.socket) {
1035 sock_release(tconn->meta.socket);
1036 tconn->meta.socket = NULL;
1037 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001038 return -1;
1039}
1040
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001041static int decode_header(struct drbd_tconn *tconn, void *header, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001042{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001043 unsigned int header_size = drbd_header_size(tconn);
1044
Andreas Gruenbacher0c8e36d2011-03-30 16:00:17 +02001045 if (header_size == sizeof(struct p_header100) &&
1046 *(__be32 *)header == cpu_to_be32(DRBD_MAGIC_100)) {
1047 struct p_header100 *h = header;
1048 if (h->pad != 0) {
1049 conn_err(tconn, "Header padding is not zero\n");
1050 return -EINVAL;
1051 }
1052 pi->vnr = be16_to_cpu(h->volume);
1053 pi->cmd = be16_to_cpu(h->command);
1054 pi->size = be32_to_cpu(h->length);
1055 } else if (header_size == sizeof(struct p_header95) &&
1056 *(__be16 *)header == cpu_to_be16(DRBD_MAGIC_BIG)) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001057 struct p_header95 *h = header;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001058 pi->cmd = be16_to_cpu(h->command);
Andreas Gruenbacherb55d84b2011-03-22 13:17:47 +01001059 pi->size = be32_to_cpu(h->length);
1060 pi->vnr = 0;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001061 } else if (header_size == sizeof(struct p_header80) &&
1062 *(__be32 *)header == cpu_to_be32(DRBD_MAGIC)) {
1063 struct p_header80 *h = header;
1064 pi->cmd = be16_to_cpu(h->command);
1065 pi->size = be16_to_cpu(h->length);
Philipp Reisner77351055b2011-02-07 17:24:26 +01001066 pi->vnr = 0;
Philipp Reisner02918be2010-08-20 14:35:10 +02001067 } else {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001068 conn_err(tconn, "Wrong magic value 0x%08x in protocol version %d\n",
1069 be32_to_cpu(*(__be32 *)header),
1070 tconn->agreed_pro_version);
Andreas Gruenbacher8172f3e2011-03-16 17:22:39 +01001071 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001072 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001073 pi->data = header + header_size;
Andreas Gruenbacher8172f3e2011-03-16 17:22:39 +01001074 return 0;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001075}
1076
Philipp Reisner9ba7aa02011-02-07 17:32:41 +01001077static int drbd_recv_header(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner257d0af2011-01-26 12:15:29 +01001078{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001079 void *buffer = tconn->data.rbuf;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001080 int err;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001081
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001082 err = drbd_recv_all_warn(tconn, buffer, drbd_header_size(tconn));
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001083 if (err)
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001084 return err;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001085
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001086 err = decode_header(tconn, buffer, pi);
Philipp Reisner9ba7aa02011-02-07 17:32:41 +01001087 tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001088
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001089 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001090}
1091
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001092static void drbd_flush(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001093{
1094 int rv;
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001095 struct drbd_conf *mdev;
1096 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001097
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001098 if (tconn->write_ordering >= WO_bdev_flush) {
1099 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1100 if (get_ldev(mdev)) {
1101 rv = blkdev_issue_flush(mdev->ldev->backing_bdev, GFP_KERNEL,
1102 NULL);
1103 put_ldev(mdev);
1104
1105 if (rv) {
1106 dev_info(DEV, "local disk flush failed with status %d\n", rv);
1107 /* would rather check on EOPNOTSUPP, but that is not reliable.
1108 * don't try again for ANY return value != 0
1109 * if (rv == -EOPNOTSUPP) */
1110 drbd_bump_write_ordering(tconn, WO_drain_io);
1111 break;
1112 }
1113 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001114 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001115 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001116}
1117
1118/**
1119 * drbd_may_finish_epoch() - Applies an epoch_event to the epoch's state, eventually finishes it.
1120 * @mdev: DRBD device.
1121 * @epoch: Epoch object.
1122 * @ev: Epoch event.
1123 */
1124static enum finish_epoch drbd_may_finish_epoch(struct drbd_conf *mdev,
1125 struct drbd_epoch *epoch,
1126 enum epoch_event ev)
1127{
Philipp Reisner2451fc32010-08-24 13:43:11 +02001128 int epoch_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001129 struct drbd_epoch *next_epoch;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001130 enum finish_epoch rv = FE_STILL_LIVE;
1131
1132 spin_lock(&mdev->epoch_lock);
1133 do {
1134 next_epoch = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001135
1136 epoch_size = atomic_read(&epoch->epoch_size);
1137
1138 switch (ev & ~EV_CLEANUP) {
1139 case EV_PUT:
1140 atomic_dec(&epoch->active);
1141 break;
1142 case EV_GOT_BARRIER_NR:
1143 set_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001144 break;
1145 case EV_BECAME_LAST:
1146 /* nothing to do*/
1147 break;
1148 }
1149
Philipp Reisnerb411b362009-09-25 16:07:19 -07001150 if (epoch_size != 0 &&
1151 atomic_read(&epoch->active) == 0 &&
Philipp Reisner85d735132011-07-18 15:45:15 +02001152 (test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags) || ev & EV_CLEANUP)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001153 if (!(ev & EV_CLEANUP)) {
1154 spin_unlock(&mdev->epoch_lock);
1155 drbd_send_b_ack(mdev, epoch->barrier_nr, epoch_size);
1156 spin_lock(&mdev->epoch_lock);
1157 }
Philipp Reisner85d735132011-07-18 15:45:15 +02001158 if (test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags))
1159 dec_unacked(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001160
1161 if (mdev->current_epoch != epoch) {
1162 next_epoch = list_entry(epoch->list.next, struct drbd_epoch, list);
1163 list_del(&epoch->list);
1164 ev = EV_BECAME_LAST | (ev & EV_CLEANUP);
1165 mdev->epochs--;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001166 kfree(epoch);
1167
1168 if (rv == FE_STILL_LIVE)
1169 rv = FE_DESTROYED;
1170 } else {
1171 epoch->flags = 0;
1172 atomic_set(&epoch->epoch_size, 0);
Uwe Kleine-König698f9312010-07-02 20:41:51 +02001173 /* atomic_set(&epoch->active, 0); is already zero */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001174 if (rv == FE_STILL_LIVE)
1175 rv = FE_RECYCLED;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001176 wake_up(&mdev->ee_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001177 }
1178 }
1179
1180 if (!next_epoch)
1181 break;
1182
1183 epoch = next_epoch;
1184 } while (1);
1185
1186 spin_unlock(&mdev->epoch_lock);
1187
Philipp Reisnerb411b362009-09-25 16:07:19 -07001188 return rv;
1189}
1190
1191/**
1192 * drbd_bump_write_ordering() - Fall back to an other write ordering method
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001193 * @tconn: DRBD connection.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001194 * @wo: Write ordering method to try.
1195 */
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001196void drbd_bump_write_ordering(struct drbd_tconn *tconn, enum write_ordering_e wo)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001197{
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001198 struct disk_conf *dc;
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001199 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001200 enum write_ordering_e pwo;
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001201 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001202 static char *write_ordering_str[] = {
1203 [WO_none] = "none",
1204 [WO_drain_io] = "drain",
1205 [WO_bdev_flush] = "flush",
Philipp Reisnerb411b362009-09-25 16:07:19 -07001206 };
1207
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001208 pwo = tconn->write_ordering;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001209 wo = min(pwo, wo);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001210 rcu_read_lock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001211 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1212 if (!get_ldev(mdev))
1213 continue;
1214 dc = rcu_dereference(mdev->ldev->disk_conf);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001215
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001216 if (wo == WO_bdev_flush && !dc->disk_flushes)
1217 wo = WO_drain_io;
1218 if (wo == WO_drain_io && !dc->disk_drain)
1219 wo = WO_none;
1220 put_ldev(mdev);
1221 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001222 rcu_read_unlock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001223 tconn->write_ordering = wo;
1224 if (pwo != tconn->write_ordering || wo == WO_bdev_flush)
1225 conn_info(tconn, "Method to ensure write ordering: %s\n", write_ordering_str[tconn->write_ordering]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001226}
1227
1228/**
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001229 * drbd_submit_peer_request()
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001230 * @mdev: DRBD device.
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001231 * @peer_req: peer request
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001232 * @rw: flag field, see bio->bi_rw
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001233 *
1234 * May spread the pages to multiple bios,
1235 * depending on bio_add_page restrictions.
1236 *
1237 * Returns 0 if all bios have been submitted,
1238 * -ENOMEM if we could not allocate enough bios,
1239 * -ENOSPC (any better suggestion?) if we have not been able to bio_add_page a
1240 * single page to an empty bio (which should never happen and likely indicates
1241 * that the lower level IO stack is in some way broken). This has been observed
1242 * on certain Xen deployments.
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001243 */
1244/* TODO allocate from our own bio_set. */
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001245int drbd_submit_peer_request(struct drbd_conf *mdev,
1246 struct drbd_peer_request *peer_req,
1247 const unsigned rw, const int fault_type)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001248{
1249 struct bio *bios = NULL;
1250 struct bio *bio;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001251 struct page *page = peer_req->pages;
1252 sector_t sector = peer_req->i.sector;
1253 unsigned ds = peer_req->i.size;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001254 unsigned n_bios = 0;
1255 unsigned nr_pages = (ds + PAGE_SIZE -1) >> PAGE_SHIFT;
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001256 int err = -ENOMEM;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001257
1258 /* In most cases, we will only need one bio. But in case the lower
1259 * level restrictions happen to be different at this offset on this
1260 * side than those of the sending peer, we may need to submit the
Lars Ellenbergda4a75d2011-02-23 17:02:01 +01001261 * request in more than one bio.
1262 *
1263 * Plain bio_alloc is good enough here, this is no DRBD internally
1264 * generated bio, but a bio allocated on behalf of the peer.
1265 */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001266next_bio:
1267 bio = bio_alloc(GFP_NOIO, nr_pages);
1268 if (!bio) {
1269 dev_err(DEV, "submit_ee: Allocation of a bio failed\n");
1270 goto fail;
1271 }
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001272 /* > peer_req->i.sector, unless this is the first bio */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001273 bio->bi_sector = sector;
1274 bio->bi_bdev = mdev->ldev->backing_bdev;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001275 bio->bi_rw = rw;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001276 bio->bi_private = peer_req;
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01001277 bio->bi_end_io = drbd_peer_request_endio;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001278
1279 bio->bi_next = bios;
1280 bios = bio;
1281 ++n_bios;
1282
1283 page_chain_for_each(page) {
1284 unsigned len = min_t(unsigned, ds, PAGE_SIZE);
1285 if (!bio_add_page(bio, page, len, 0)) {
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001286 /* A single page must always be possible!
1287 * But in case it fails anyways,
1288 * we deal with it, and complain (below). */
1289 if (bio->bi_vcnt == 0) {
1290 dev_err(DEV,
1291 "bio_add_page failed for len=%u, "
1292 "bi_vcnt=0 (bi_sector=%llu)\n",
1293 len, (unsigned long long)bio->bi_sector);
1294 err = -ENOSPC;
1295 goto fail;
1296 }
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001297 goto next_bio;
1298 }
1299 ds -= len;
1300 sector += len >> 9;
1301 --nr_pages;
1302 }
1303 D_ASSERT(page == NULL);
1304 D_ASSERT(ds == 0);
1305
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001306 atomic_set(&peer_req->pending_bios, n_bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001307 do {
1308 bio = bios;
1309 bios = bios->bi_next;
1310 bio->bi_next = NULL;
1311
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001312 drbd_generic_make_request(mdev, fault_type, bio);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001313 } while (bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001314 return 0;
1315
1316fail:
1317 while (bios) {
1318 bio = bios;
1319 bios = bios->bi_next;
1320 bio_put(bio);
1321 }
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001322 return err;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001323}
1324
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001325static void drbd_remove_epoch_entry_interval(struct drbd_conf *mdev,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001326 struct drbd_peer_request *peer_req)
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001327{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001328 struct drbd_interval *i = &peer_req->i;
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001329
1330 drbd_remove_interval(&mdev->write_requests, i);
1331 drbd_clear_interval(i);
1332
Andreas Gruenbacher6c852be2011-02-04 15:38:52 +01001333 /* Wake up any processes waiting for this peer request to complete. */
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001334 if (i->waiting)
1335 wake_up(&mdev->misc_wait);
1336}
1337
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001338static int receive_Barrier(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001339{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001340 struct drbd_conf *mdev;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001341 int rv;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001342 struct p_barrier *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001343 struct drbd_epoch *epoch;
1344
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001345 mdev = vnr_to_mdev(tconn, pi->vnr);
1346 if (!mdev)
1347 return -EIO;
1348
Philipp Reisnerb411b362009-09-25 16:07:19 -07001349 inc_unacked(mdev);
1350
Philipp Reisnerb411b362009-09-25 16:07:19 -07001351 mdev->current_epoch->barrier_nr = p->barrier;
1352 rv = drbd_may_finish_epoch(mdev, mdev->current_epoch, EV_GOT_BARRIER_NR);
1353
1354 /* P_BARRIER_ACK may imply that the corresponding extent is dropped from
1355 * the activity log, which means it would not be resynced in case the
1356 * R_PRIMARY crashes now.
1357 * Therefore we must send the barrier_ack after the barrier request was
1358 * completed. */
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001359 switch (tconn->write_ordering) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001360 case WO_none:
1361 if (rv == FE_RECYCLED)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001362 return 0;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001363
1364 /* receiver context, in the writeout path of the other node.
1365 * avoid potential distributed deadlock */
1366 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1367 if (epoch)
1368 break;
1369 else
1370 dev_warn(DEV, "Allocation of an epoch failed, slowing down\n");
1371 /* Fall through */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001372
1373 case WO_bdev_flush:
1374 case WO_drain_io:
Philipp Reisnerb411b362009-09-25 16:07:19 -07001375 drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001376 drbd_flush(tconn);
Philipp Reisner2451fc32010-08-24 13:43:11 +02001377
1378 if (atomic_read(&mdev->current_epoch->epoch_size)) {
1379 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1380 if (epoch)
1381 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001382 }
1383
Philipp Reisner2451fc32010-08-24 13:43:11 +02001384 epoch = mdev->current_epoch;
1385 wait_event(mdev->ee_wait, atomic_read(&epoch->epoch_size) == 0);
1386
1387 D_ASSERT(atomic_read(&epoch->active) == 0);
1388 D_ASSERT(epoch->flags == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001389
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001390 return 0;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001391 default:
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001392 dev_err(DEV, "Strangeness in tconn->write_ordering %d\n", tconn->write_ordering);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001393 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001394 }
1395
1396 epoch->flags = 0;
1397 atomic_set(&epoch->epoch_size, 0);
1398 atomic_set(&epoch->active, 0);
1399
1400 spin_lock(&mdev->epoch_lock);
1401 if (atomic_read(&mdev->current_epoch->epoch_size)) {
1402 list_add(&epoch->list, &mdev->current_epoch->list);
1403 mdev->current_epoch = epoch;
1404 mdev->epochs++;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001405 } else {
1406 /* The current_epoch got recycled while we allocated this one... */
1407 kfree(epoch);
1408 }
1409 spin_unlock(&mdev->epoch_lock);
1410
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001411 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001412}
1413
1414/* used from receive_RSDataReply (recv_resync_read)
1415 * and from receive_Data */
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001416static struct drbd_peer_request *
1417read_in_block(struct drbd_conf *mdev, u64 id, sector_t sector,
1418 int data_size) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001419{
Lars Ellenberg66660322010-04-06 12:15:04 +02001420 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001421 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001422 struct page *page;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001423 int dgs, ds, err;
Philipp Reisnera0638452011-01-19 14:31:32 +01001424 void *dig_in = mdev->tconn->int_dig_in;
1425 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001426 unsigned long *data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001427
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001428 dgs = 0;
1429 if (mdev->tconn->peer_integrity_tfm) {
1430 dgs = crypto_hash_digestsize(mdev->tconn->peer_integrity_tfm);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001431 /*
1432 * FIXME: Receive the incoming digest into the receive buffer
1433 * here, together with its struct p_data?
1434 */
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001435 err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs);
1436 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001437 return NULL;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001438 data_size -= dgs;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001439 }
1440
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001441 if (!expect(data_size != 0))
1442 return NULL;
1443 if (!expect(IS_ALIGNED(data_size, 512)))
1444 return NULL;
1445 if (!expect(data_size <= DRBD_MAX_BIO_SIZE))
1446 return NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001447
Lars Ellenberg66660322010-04-06 12:15:04 +02001448 /* even though we trust out peer,
1449 * we sometimes have to double check. */
1450 if (sector + (data_size>>9) > capacity) {
Lars Ellenbergfdda6542011-01-24 15:11:01 +01001451 dev_err(DEV, "request from peer beyond end of local disk: "
1452 "capacity: %llus < sector: %llus + size: %u\n",
Lars Ellenberg66660322010-04-06 12:15:04 +02001453 (unsigned long long)capacity,
1454 (unsigned long long)sector, data_size);
1455 return NULL;
1456 }
1457
Philipp Reisnerb411b362009-09-25 16:07:19 -07001458 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
1459 * "criss-cross" setup, that might cause write-out on some other DRBD,
1460 * which in turn might block on the other node at this very place. */
Andreas Gruenbacher0db55362011-04-06 16:09:15 +02001461 peer_req = drbd_alloc_peer_req(mdev, id, sector, data_size, GFP_NOIO);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001462 if (!peer_req)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001463 return NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001464
Philipp Reisnerb411b362009-09-25 16:07:19 -07001465 ds = data_size;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001466 page = peer_req->pages;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001467 page_chain_for_each(page) {
1468 unsigned len = min_t(int, ds, PAGE_SIZE);
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001469 data = kmap(page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001470 err = drbd_recv_all_warn(mdev->tconn, data, len);
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +01001471 if (drbd_insert_fault(mdev, DRBD_FAULT_RECEIVE)) {
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001472 dev_err(DEV, "Fault injection: Corrupting data on receive\n");
1473 data[0] = data[0] ^ (unsigned long)-1;
1474 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001475 kunmap(page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001476 if (err) {
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001477 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001478 return NULL;
1479 }
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001480 ds -= len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001481 }
1482
1483 if (dgs) {
Andreas Gruenbacher5b614ab2011-04-27 21:00:12 +02001484 drbd_csum_ee(mdev, mdev->tconn->peer_integrity_tfm, peer_req, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001485 if (memcmp(dig_in, dig_vv, dgs)) {
Lars Ellenberg470be442010-11-10 10:36:52 +01001486 dev_err(DEV, "Digest integrity check FAILED: %llus +%u\n",
1487 (unsigned long long)sector, data_size);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001488 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001489 return NULL;
1490 }
1491 }
1492 mdev->recv_cnt += data_size>>9;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001493 return peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001494}
1495
1496/* drbd_drain_block() just takes a data block
1497 * out of the socket input buffer, and discards it.
1498 */
1499static int drbd_drain_block(struct drbd_conf *mdev, int data_size)
1500{
1501 struct page *page;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001502 int err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001503 void *data;
1504
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001505 if (!data_size)
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001506 return 0;
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001507
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +02001508 page = drbd_alloc_pages(mdev, 1, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001509
1510 data = kmap(page);
1511 while (data_size) {
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001512 unsigned int len = min_t(int, data_size, PAGE_SIZE);
1513
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001514 err = drbd_recv_all_warn(mdev->tconn, data, len);
1515 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001516 break;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001517 data_size -= len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001518 }
1519 kunmap(page);
Andreas Gruenbacher5cc287e2011-04-07 21:02:59 +02001520 drbd_free_pages(mdev, page, 0);
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001521 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001522}
1523
1524static int recv_dless_read(struct drbd_conf *mdev, struct drbd_request *req,
1525 sector_t sector, int data_size)
1526{
1527 struct bio_vec *bvec;
1528 struct bio *bio;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001529 int dgs, err, i, expect;
Philipp Reisnera0638452011-01-19 14:31:32 +01001530 void *dig_in = mdev->tconn->int_dig_in;
1531 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001532
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001533 dgs = 0;
1534 if (mdev->tconn->peer_integrity_tfm) {
1535 dgs = crypto_hash_digestsize(mdev->tconn->peer_integrity_tfm);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001536 err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs);
1537 if (err)
1538 return err;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001539 data_size -= dgs;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001540 }
1541
Philipp Reisnerb411b362009-09-25 16:07:19 -07001542 /* optimistically update recv_cnt. if receiving fails below,
1543 * we disconnect anyways, and counters will be reset. */
1544 mdev->recv_cnt += data_size>>9;
1545
1546 bio = req->master_bio;
1547 D_ASSERT(sector == bio->bi_sector);
1548
1549 bio_for_each_segment(bvec, bio, i) {
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001550 void *mapped = kmap(bvec->bv_page) + bvec->bv_offset;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001551 expect = min_t(int, data_size, bvec->bv_len);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001552 err = drbd_recv_all_warn(mdev->tconn, mapped, expect);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001553 kunmap(bvec->bv_page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001554 if (err)
1555 return err;
1556 data_size -= expect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001557 }
1558
1559 if (dgs) {
Andreas Gruenbacher5b614ab2011-04-27 21:00:12 +02001560 drbd_csum_bio(mdev, mdev->tconn->peer_integrity_tfm, bio, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001561 if (memcmp(dig_in, dig_vv, dgs)) {
1562 dev_err(DEV, "Digest integrity check FAILED. Broken NICs?\n");
Andreas Gruenbacher28284ce2011-03-16 17:54:02 +01001563 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001564 }
1565 }
1566
1567 D_ASSERT(data_size == 0);
Andreas Gruenbacher28284ce2011-03-16 17:54:02 +01001568 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001569}
1570
Andreas Gruenbachera990be42011-04-06 17:56:48 +02001571/*
1572 * e_end_resync_block() is called in asender context via
1573 * drbd_finish_peer_reqs().
1574 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001575static int e_end_resync_block(struct drbd_work *w, int unused)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001576{
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001577 struct drbd_peer_request *peer_req =
1578 container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001579 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001580 sector_t sector = peer_req->i.sector;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001581 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001582
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001583 D_ASSERT(drbd_interval_empty(&peer_req->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001584
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001585 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
1586 drbd_set_in_sync(mdev, sector, peer_req->i.size);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001587 err = drbd_send_ack(mdev, P_RS_WRITE_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001588 } else {
1589 /* Record failure to sync */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001590 drbd_rs_failed_io(mdev, sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001591
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001592 err = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001593 }
1594 dec_unacked(mdev);
1595
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001596 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001597}
1598
1599static int recv_resync_read(struct drbd_conf *mdev, sector_t sector, int data_size) __releases(local)
1600{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001601 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001602
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001603 peer_req = read_in_block(mdev, ID_SYNCER, sector, data_size);
1604 if (!peer_req)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001605 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001606
1607 dec_rs_pending(mdev);
1608
Philipp Reisnerb411b362009-09-25 16:07:19 -07001609 inc_unacked(mdev);
1610 /* corresponding dec_unacked() in e_end_resync_block()
1611 * respective _drbd_clear_done_ee */
1612
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001613 peer_req->w.cb = e_end_resync_block;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001614
Philipp Reisner87eeee42011-01-19 14:16:30 +01001615 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001616 list_add(&peer_req->w.list, &mdev->sync_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001617 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001618
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001619 atomic_add(data_size >> 9, &mdev->rs_sect_ev);
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001620 if (drbd_submit_peer_request(mdev, peer_req, WRITE, DRBD_FAULT_RS_WR) == 0)
Andreas Gruenbachere1c1b0f2011-03-16 17:58:27 +01001621 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001622
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001623 /* don't care for the reason here */
1624 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01001625 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001626 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001627 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02001628
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001629 drbd_free_peer_req(mdev, peer_req);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001630fail:
1631 put_ldev(mdev);
Andreas Gruenbachere1c1b0f2011-03-16 17:58:27 +01001632 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001633}
1634
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001635static struct drbd_request *
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001636find_request(struct drbd_conf *mdev, struct rb_root *root, u64 id,
1637 sector_t sector, bool missing_ok, const char *func)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001638{
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001639 struct drbd_request *req;
1640
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001641 /* Request object according to our peer */
1642 req = (struct drbd_request *)(unsigned long)id;
Andreas Gruenbacher5e472262011-01-27 14:42:51 +01001643 if (drbd_contains_interval(root, sector, &req->i) && req->i.local)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001644 return req;
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001645 if (!missing_ok) {
Andreas Gruenbacher5af172e2011-07-15 09:43:23 +02001646 dev_err(DEV, "%s: failed to find request 0x%lx, sector %llus\n", func,
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001647 (unsigned long)id, (unsigned long long)sector);
1648 }
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001649 return NULL;
1650}
1651
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001652static int receive_DataReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001653{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001654 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001655 struct drbd_request *req;
1656 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
Philipp Reisner87eeee42011-01-19 14:16:30 +01001666 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001667 req = find_request(mdev, &mdev->read_requests, p->block_id, sector, false, __func__);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001668 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001669 if (unlikely(!req))
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001670 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001671
Bart Van Assche24c48302011-05-21 18:32:29 +02001672 /* hlist_del(&req->collision) is done in _req_may_be_done, to avoid
Philipp Reisnerb411b362009-09-25 16:07:19 -07001673 * special casing it there for the various failure cases.
1674 * still no race with drbd_fail_pending_reads */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001675 err = recv_dless_read(mdev, req, sector, pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001676 if (!err)
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001677 req_mod(req, DATA_RECEIVED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001678 /* else: nothing. handled from drbd_disconnect...
1679 * I don't think we may complete this just yet
1680 * in case we are "on-disconnect: freeze" */
1681
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001682 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001683}
1684
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001685static int receive_RSDataReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001686{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001687 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001688 sector_t sector;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001689 int err;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001690 struct p_data *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001691
1692 mdev = vnr_to_mdev(tconn, pi->vnr);
1693 if (!mdev)
1694 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001695
1696 sector = be64_to_cpu(p->sector);
1697 D_ASSERT(p->block_id == ID_SYNCER);
1698
1699 if (get_ldev(mdev)) {
1700 /* data is submitted to disk within recv_resync_read.
1701 * corresponding put_ldev done below on error,
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01001702 * or in drbd_peer_request_endio. */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001703 err = recv_resync_read(mdev, sector, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001704 } else {
1705 if (__ratelimit(&drbd_ratelimit_state))
1706 dev_err(DEV, "Can not write resync data to local disk.\n");
1707
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001708 err = drbd_drain_block(mdev, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001709
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001710 drbd_send_ack_dp(mdev, P_NEG_ACK, p, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001711 }
1712
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001713 atomic_add(pi->size >> 9, &mdev->rs_sect_in);
Philipp Reisner778f2712010-07-06 11:14:00 +02001714
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001715 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001716}
1717
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001718static int w_restart_write(struct drbd_work *w, int cancel)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001719{
1720 struct drbd_request *req = container_of(w, struct drbd_request, w);
1721 struct drbd_conf *mdev = w->mdev;
1722 struct bio *bio;
1723 unsigned long start_time;
1724 unsigned long flags;
1725
1726 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
1727 if (!expect(req->rq_state & RQ_POSTPONED)) {
1728 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001729 return -EIO;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001730 }
1731 bio = req->master_bio;
1732 start_time = req->start_time;
1733 /* Postponed requests will not have their master_bio completed! */
1734 __req_mod(req, DISCARD_WRITE, NULL);
1735 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
1736
1737 while (__drbd_make_request(mdev, bio, start_time))
1738 /* retry */ ;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001739 return 0;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001740}
1741
1742static void restart_conflicting_writes(struct drbd_conf *mdev,
1743 sector_t sector, int size)
1744{
1745 struct drbd_interval *i;
1746 struct drbd_request *req;
1747
1748 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
1749 if (!i->local)
1750 continue;
1751 req = container_of(i, struct drbd_request, i);
1752 if (req->rq_state & RQ_LOCAL_PENDING ||
1753 !(req->rq_state & RQ_POSTPONED))
1754 continue;
1755 if (expect(list_empty(&req->w.list))) {
1756 req->w.mdev = mdev;
1757 req->w.cb = w_restart_write;
1758 drbd_queue_work(&mdev->tconn->data.work, &req->w);
1759 }
1760 }
1761}
1762
Andreas Gruenbachera990be42011-04-06 17:56:48 +02001763/*
1764 * e_end_block() is called in asender context via drbd_finish_peer_reqs().
Philipp Reisnerb411b362009-09-25 16:07:19 -07001765 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001766static int e_end_block(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001767{
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001768 struct drbd_peer_request *peer_req =
1769 container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001770 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001771 sector_t sector = peer_req->i.sector;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001772 int err = 0, pcmd;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001773
Philipp Reisner303d1442011-04-13 16:24:47 -07001774 if (peer_req->flags & EE_SEND_WRITE_ACK) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001775 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001776 pcmd = (mdev->state.conn >= C_SYNC_SOURCE &&
1777 mdev->state.conn <= C_PAUSED_SYNC_T &&
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001778 peer_req->flags & EE_MAY_SET_IN_SYNC) ?
Philipp Reisnerb411b362009-09-25 16:07:19 -07001779 P_RS_WRITE_ACK : P_WRITE_ACK;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001780 err = drbd_send_ack(mdev, pcmd, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001781 if (pcmd == P_RS_WRITE_ACK)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001782 drbd_set_in_sync(mdev, sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001783 } else {
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001784 err = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001785 /* we expect it to be marked out of sync anyways...
1786 * maybe assert this? */
1787 }
1788 dec_unacked(mdev);
1789 }
1790 /* we delete from the conflict detection hash _after_ we sent out the
1791 * P_WRITE_ACK / P_NEG_ACK, to get the sequence number right. */
Philipp Reisner302bdea2011-04-21 11:36:49 +02001792 if (peer_req->flags & EE_IN_INTERVAL_TREE) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01001793 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001794 D_ASSERT(!drbd_interval_empty(&peer_req->i));
1795 drbd_remove_epoch_entry_interval(mdev, peer_req);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001796 if (peer_req->flags & EE_RESTART_REQUESTS)
1797 restart_conflicting_writes(mdev, sector, peer_req->i.size);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001798 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbb3bfe92011-01-21 15:59:23 +01001799 } else
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001800 D_ASSERT(drbd_interval_empty(&peer_req->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001801
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001802 drbd_may_finish_epoch(mdev, peer_req->epoch, EV_PUT + (cancel ? EV_CLEANUP : 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001803
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001804 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001805}
1806
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001807static int e_send_ack(struct drbd_work *w, enum drbd_packet ack)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001808{
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001809 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001810 struct drbd_peer_request *peer_req =
1811 container_of(w, struct drbd_peer_request, w);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001812 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001813
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001814 err = drbd_send_ack(mdev, ack, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001815 dec_unacked(mdev);
1816
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001817 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001818}
1819
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001820static int e_send_discard_write(struct drbd_work *w, int unused)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001821{
1822 return e_send_ack(w, P_DISCARD_WRITE);
1823}
1824
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001825static int e_send_retry_write(struct drbd_work *w, int unused)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001826{
1827 struct drbd_tconn *tconn = w->mdev->tconn;
1828
1829 return e_send_ack(w, tconn->agreed_pro_version >= 100 ?
1830 P_RETRY_WRITE : P_DISCARD_WRITE);
1831}
1832
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001833static bool seq_greater(u32 a, u32 b)
1834{
1835 /*
1836 * We assume 32-bit wrap-around here.
1837 * For 24-bit wrap-around, we would have to shift:
1838 * a <<= 8; b <<= 8;
1839 */
1840 return (s32)a - (s32)b > 0;
1841}
1842
1843static u32 seq_max(u32 a, u32 b)
1844{
1845 return seq_greater(a, b) ? a : b;
1846}
1847
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001848static bool need_peer_seq(struct drbd_conf *mdev)
1849{
1850 struct drbd_tconn *tconn = mdev->tconn;
Philipp Reisner302bdea2011-04-21 11:36:49 +02001851 int tp;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001852
1853 /*
1854 * We only need to keep track of the last packet_seq number of our peer
1855 * if we are in dual-primary mode and we have the discard flag set; see
1856 * handle_write_conflicts().
1857 */
Philipp Reisner302bdea2011-04-21 11:36:49 +02001858
1859 rcu_read_lock();
1860 tp = rcu_dereference(mdev->tconn->net_conf)->two_primaries;
1861 rcu_read_unlock();
1862
1863 return tp && test_bit(DISCARD_CONCURRENT, &tconn->flags);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001864}
1865
Andreas Gruenbacher43ae0772011-02-03 18:42:08 +01001866static void update_peer_seq(struct drbd_conf *mdev, unsigned int peer_seq)
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001867{
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001868 unsigned int newest_peer_seq;
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001869
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001870 if (need_peer_seq(mdev)) {
1871 spin_lock(&mdev->peer_seq_lock);
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001872 newest_peer_seq = seq_max(mdev->peer_seq, peer_seq);
1873 mdev->peer_seq = newest_peer_seq;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001874 spin_unlock(&mdev->peer_seq_lock);
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001875 /* wake up only if we actually changed mdev->peer_seq */
1876 if (peer_seq == newest_peer_seq)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001877 wake_up(&mdev->seq_wait);
1878 }
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001879}
1880
Philipp Reisnerb411b362009-09-25 16:07:19 -07001881/* Called from receive_Data.
1882 * Synchronize packets on sock with packets on msock.
1883 *
1884 * This is here so even when a P_DATA packet traveling via sock overtook an Ack
1885 * packet traveling on msock, they are still processed in the order they have
1886 * been sent.
1887 *
1888 * Note: we don't care for Ack packets overtaking P_DATA packets.
1889 *
1890 * In case packet_seq is larger than mdev->peer_seq number, there are
1891 * outstanding packets on the msock. We wait for them to arrive.
1892 * In case we are the logically next packet, we update mdev->peer_seq
1893 * ourselves. Correctly handles 32bit wrap around.
1894 *
1895 * Assume we have a 10 GBit connection, that is about 1<<30 byte per second,
1896 * about 1<<21 sectors per second. So "worst" case, we have 1<<3 == 8 seconds
1897 * for the 24bit wrap (historical atomic_t guarantee on some archs), and we have
1898 * 1<<9 == 512 seconds aka ages for the 32bit wrap around...
1899 *
1900 * returns 0 if we may process the packet,
1901 * -ERESTARTSYS if we were interrupted (by disconnect signal). */
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001902static int wait_for_and_update_peer_seq(struct drbd_conf *mdev, const u32 peer_seq)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001903{
1904 DEFINE_WAIT(wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001905 long timeout;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001906 int ret;
1907
1908 if (!need_peer_seq(mdev))
1909 return 0;
1910
Philipp Reisnerb411b362009-09-25 16:07:19 -07001911 spin_lock(&mdev->peer_seq_lock);
1912 for (;;) {
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001913 if (!seq_greater(peer_seq - 1, mdev->peer_seq)) {
1914 mdev->peer_seq = seq_max(mdev->peer_seq, peer_seq);
1915 ret = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001916 break;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001917 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001918 if (signal_pending(current)) {
1919 ret = -ERESTARTSYS;
1920 break;
1921 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001922 prepare_to_wait(&mdev->seq_wait, &wait, TASK_INTERRUPTIBLE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001923 spin_unlock(&mdev->peer_seq_lock);
Philipp Reisner44ed1672011-04-19 17:10:19 +02001924 rcu_read_lock();
1925 timeout = rcu_dereference(mdev->tconn->net_conf)->ping_timeo*HZ/10;
1926 rcu_read_unlock();
Andreas Gruenbacher71b1c1e2011-03-01 15:40:43 +01001927 timeout = schedule_timeout(timeout);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001928 spin_lock(&mdev->peer_seq_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001929 if (!timeout) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001930 ret = -ETIMEDOUT;
Andreas Gruenbacher71b1c1e2011-03-01 15:40:43 +01001931 dev_err(DEV, "Timed out waiting for missing ack packets; disconnecting\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001932 break;
1933 }
1934 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001935 spin_unlock(&mdev->peer_seq_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001936 finish_wait(&mdev->seq_wait, &wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001937 return ret;
1938}
1939
Lars Ellenberg688593c2010-11-17 22:25:03 +01001940/* see also bio_flags_to_wire()
1941 * DRBD_REQ_*, because we need to semantically map the flags to data packet
1942 * flags and back. We may replicate to other kernel versions. */
1943static unsigned long wire_flags_to_bio(struct drbd_conf *mdev, u32 dpf)
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001944{
Lars Ellenberg688593c2010-11-17 22:25:03 +01001945 return (dpf & DP_RW_SYNC ? REQ_SYNC : 0) |
1946 (dpf & DP_FUA ? REQ_FUA : 0) |
1947 (dpf & DP_FLUSH ? REQ_FLUSH : 0) |
1948 (dpf & DP_DISCARD ? REQ_DISCARD : 0);
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001949}
1950
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001951static void fail_postponed_requests(struct drbd_conf *mdev, sector_t sector,
1952 unsigned int size)
1953{
1954 struct drbd_interval *i;
1955
1956 repeat:
1957 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
1958 struct drbd_request *req;
1959 struct bio_and_error m;
1960
1961 if (!i->local)
1962 continue;
1963 req = container_of(i, struct drbd_request, i);
1964 if (!(req->rq_state & RQ_POSTPONED))
1965 continue;
1966 req->rq_state &= ~RQ_POSTPONED;
1967 __req_mod(req, NEG_ACKED, &m);
1968 spin_unlock_irq(&mdev->tconn->req_lock);
1969 if (m.bio)
1970 complete_master_bio(mdev, &m);
1971 spin_lock_irq(&mdev->tconn->req_lock);
1972 goto repeat;
1973 }
1974}
1975
1976static int handle_write_conflicts(struct drbd_conf *mdev,
1977 struct drbd_peer_request *peer_req)
1978{
1979 struct drbd_tconn *tconn = mdev->tconn;
1980 bool resolve_conflicts = test_bit(DISCARD_CONCURRENT, &tconn->flags);
1981 sector_t sector = peer_req->i.sector;
1982 const unsigned int size = peer_req->i.size;
1983 struct drbd_interval *i;
1984 bool equal;
1985 int err;
1986
1987 /*
1988 * Inserting the peer request into the write_requests tree will prevent
1989 * new conflicting local requests from being added.
1990 */
1991 drbd_insert_interval(&mdev->write_requests, &peer_req->i);
1992
1993 repeat:
1994 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
1995 if (i == &peer_req->i)
1996 continue;
1997
1998 if (!i->local) {
1999 /*
2000 * Our peer has sent a conflicting remote request; this
2001 * should not happen in a two-node setup. Wait for the
2002 * earlier peer request to complete.
2003 */
2004 err = drbd_wait_misc(mdev, i);
2005 if (err)
2006 goto out;
2007 goto repeat;
2008 }
2009
2010 equal = i->sector == sector && i->size == size;
2011 if (resolve_conflicts) {
2012 /*
2013 * If the peer request is fully contained within the
2014 * overlapping request, it can be discarded; otherwise,
2015 * it will be retried once all overlapping requests
2016 * have completed.
2017 */
2018 bool discard = i->sector <= sector && i->sector +
2019 (i->size >> 9) >= sector + (size >> 9);
2020
2021 if (!equal)
2022 dev_alert(DEV, "Concurrent writes detected: "
2023 "local=%llus +%u, remote=%llus +%u, "
2024 "assuming %s came first\n",
2025 (unsigned long long)i->sector, i->size,
2026 (unsigned long long)sector, size,
2027 discard ? "local" : "remote");
2028
2029 inc_unacked(mdev);
2030 peer_req->w.cb = discard ? e_send_discard_write :
2031 e_send_retry_write;
2032 list_add_tail(&peer_req->w.list, &mdev->done_ee);
2033 wake_asender(mdev->tconn);
2034
2035 err = -ENOENT;
2036 goto out;
2037 } else {
2038 struct drbd_request *req =
2039 container_of(i, struct drbd_request, i);
2040
2041 if (!equal)
2042 dev_alert(DEV, "Concurrent writes detected: "
2043 "local=%llus +%u, remote=%llus +%u\n",
2044 (unsigned long long)i->sector, i->size,
2045 (unsigned long long)sector, size);
2046
2047 if (req->rq_state & RQ_LOCAL_PENDING ||
2048 !(req->rq_state & RQ_POSTPONED)) {
2049 /*
2050 * Wait for the node with the discard flag to
2051 * decide if this request will be discarded or
2052 * retried. Requests that are discarded will
2053 * disappear from the write_requests tree.
2054 *
2055 * In addition, wait for the conflicting
2056 * request to finish locally before submitting
2057 * the conflicting peer request.
2058 */
2059 err = drbd_wait_misc(mdev, &req->i);
2060 if (err) {
2061 _conn_request_state(mdev->tconn,
2062 NS(conn, C_TIMEOUT),
2063 CS_HARD);
2064 fail_postponed_requests(mdev, sector, size);
2065 goto out;
2066 }
2067 goto repeat;
2068 }
2069 /*
2070 * Remember to restart the conflicting requests after
2071 * the new peer request has completed.
2072 */
2073 peer_req->flags |= EE_RESTART_REQUESTS;
2074 }
2075 }
2076 err = 0;
2077
2078 out:
2079 if (err)
2080 drbd_remove_epoch_entry_interval(mdev, peer_req);
2081 return err;
2082}
2083
Philipp Reisnerb411b362009-09-25 16:07:19 -07002084/* mirrored write */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002085static int receive_Data(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002086{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002087 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002088 sector_t sector;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002089 struct drbd_peer_request *peer_req;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02002090 struct p_data *p = pi->data;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002091 u32 peer_seq = be32_to_cpu(p->seq_num);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002092 int rw = WRITE;
2093 u32 dp_flags;
Philipp Reisner302bdea2011-04-21 11:36:49 +02002094 int err, tp;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002095
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002096 mdev = vnr_to_mdev(tconn, pi->vnr);
2097 if (!mdev)
2098 return -EIO;
2099
Philipp Reisnerb411b362009-09-25 16:07:19 -07002100 if (!get_ldev(mdev)) {
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002101 int err2;
2102
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002103 err = wait_for_and_update_peer_seq(mdev, peer_seq);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002104 drbd_send_ack_dp(mdev, P_NEG_ACK, p, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002105 atomic_inc(&mdev->current_epoch->epoch_size);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002106 err2 = drbd_drain_block(mdev, pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002107 if (!err)
2108 err = err2;
2109 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002110 }
2111
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01002112 /*
2113 * Corresponding put_ldev done either below (on various errors), or in
2114 * drbd_peer_request_endio, if we successfully submit the data at the
2115 * end of this function.
2116 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002117
2118 sector = be64_to_cpu(p->sector);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002119 peer_req = read_in_block(mdev, p->block_id, sector, pi->size);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002120 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002121 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002122 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002123 }
2124
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002125 peer_req->w.cb = e_end_block;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002126
Lars Ellenberg688593c2010-11-17 22:25:03 +01002127 dp_flags = be32_to_cpu(p->dp_flags);
2128 rw |= wire_flags_to_bio(mdev, dp_flags);
2129
2130 if (dp_flags & DP_MAY_SET_IN_SYNC)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002131 peer_req->flags |= EE_MAY_SET_IN_SYNC;
Lars Ellenberg688593c2010-11-17 22:25:03 +01002132
Philipp Reisnerb411b362009-09-25 16:07:19 -07002133 spin_lock(&mdev->epoch_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002134 peer_req->epoch = mdev->current_epoch;
2135 atomic_inc(&peer_req->epoch->epoch_size);
2136 atomic_inc(&peer_req->epoch->active);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002137 spin_unlock(&mdev->epoch_lock);
2138
Philipp Reisner302bdea2011-04-21 11:36:49 +02002139 rcu_read_lock();
2140 tp = rcu_dereference(mdev->tconn->net_conf)->two_primaries;
2141 rcu_read_unlock();
2142 if (tp) {
2143 peer_req->flags |= EE_IN_INTERVAL_TREE;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002144 err = wait_for_and_update_peer_seq(mdev, peer_seq);
2145 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002146 goto out_interrupted;
Philipp Reisner87eeee42011-01-19 14:16:30 +01002147 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002148 err = handle_write_conflicts(mdev, peer_req);
2149 if (err) {
2150 spin_unlock_irq(&mdev->tconn->req_lock);
2151 if (err == -ENOENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002152 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002153 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002154 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002155 goto out_interrupted;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002156 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002157 } else
2158 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002159 list_add(&peer_req->w.list, &mdev->active_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002160 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002161
Philipp Reisner303d1442011-04-13 16:24:47 -07002162 if (mdev->tconn->agreed_pro_version < 100) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02002163 rcu_read_lock();
2164 switch (rcu_dereference(mdev->tconn->net_conf)->wire_protocol) {
Philipp Reisner303d1442011-04-13 16:24:47 -07002165 case DRBD_PROT_C:
2166 dp_flags |= DP_SEND_WRITE_ACK;
2167 break;
2168 case DRBD_PROT_B:
2169 dp_flags |= DP_SEND_RECEIVE_ACK;
2170 break;
2171 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002172 rcu_read_unlock();
Philipp Reisner303d1442011-04-13 16:24:47 -07002173 }
2174
2175 if (dp_flags & DP_SEND_WRITE_ACK) {
2176 peer_req->flags |= EE_SEND_WRITE_ACK;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002177 inc_unacked(mdev);
2178 /* corresponding dec_unacked() in e_end_block()
2179 * respective _drbd_clear_done_ee */
Philipp Reisner303d1442011-04-13 16:24:47 -07002180 }
2181
2182 if (dp_flags & DP_SEND_RECEIVE_ACK) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002183 /* I really don't like it that the receiver thread
2184 * sends on the msock, but anyways */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002185 drbd_send_ack(mdev, P_RECV_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002186 }
2187
Lars Ellenberg6719fb02010-10-18 23:04:07 +02002188 if (mdev->state.pdsk < D_INCONSISTENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002189 /* In case we have the only disk of the cluster, */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002190 drbd_set_out_of_sync(mdev, peer_req->i.sector, peer_req->i.size);
2191 peer_req->flags |= EE_CALL_AL_COMPLETE_IO;
2192 peer_req->flags &= ~EE_MAY_SET_IN_SYNC;
Lars Ellenberg181286a2011-03-31 15:18:56 +02002193 drbd_al_begin_io(mdev, &peer_req->i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002194 }
2195
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002196 err = drbd_submit_peer_request(mdev, peer_req, rw, DRBD_FAULT_DT_WR);
2197 if (!err)
2198 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002199
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01002200 /* don't care for the reason here */
2201 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002202 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002203 list_del(&peer_req->w.list);
2204 drbd_remove_epoch_entry_interval(mdev, peer_req);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002205 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002206 if (peer_req->flags & EE_CALL_AL_COMPLETE_IO)
Lars Ellenberg181286a2011-03-31 15:18:56 +02002207 drbd_al_complete_io(mdev, &peer_req->i);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002208
Philipp Reisnerb411b362009-09-25 16:07:19 -07002209out_interrupted:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002210 drbd_may_finish_epoch(mdev, peer_req->epoch, EV_PUT + EV_CLEANUP);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002211 put_ldev(mdev);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02002212 drbd_free_peer_req(mdev, peer_req);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002213 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002214}
2215
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002216/* We may throttle resync, if the lower device seems to be busy,
2217 * and current sync rate is above c_min_rate.
2218 *
2219 * To decide whether or not the lower device is busy, we use a scheme similar
2220 * to MD RAID is_mddev_idle(): if the partition stats reveal "significant"
2221 * (more than 64 sectors) of activity we cannot account for with our own resync
2222 * activity, it obviously is "busy".
2223 *
2224 * The current sync rate used here uses only the most recent two step marks,
2225 * to have a short time average so we can react faster.
2226 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002227int drbd_rs_should_slow_down(struct drbd_conf *mdev, sector_t sector)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002228{
2229 struct gendisk *disk = mdev->ldev->backing_bdev->bd_contains->bd_disk;
2230 unsigned long db, dt, dbdt;
Philipp Reisnere3555d82010-11-07 15:56:29 +01002231 struct lc_element *tmp;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002232 int curr_events;
2233 int throttle = 0;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002234 unsigned int c_min_rate;
2235
2236 rcu_read_lock();
2237 c_min_rate = rcu_dereference(mdev->ldev->disk_conf)->c_min_rate;
2238 rcu_read_unlock();
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002239
2240 /* feature disabled? */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002241 if (c_min_rate == 0)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002242 return 0;
2243
Philipp Reisnere3555d82010-11-07 15:56:29 +01002244 spin_lock_irq(&mdev->al_lock);
2245 tmp = lc_find(mdev->resync, BM_SECT_TO_EXT(sector));
2246 if (tmp) {
2247 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
2248 if (test_bit(BME_PRIORITY, &bm_ext->flags)) {
2249 spin_unlock_irq(&mdev->al_lock);
2250 return 0;
2251 }
2252 /* Do not slow down if app IO is already waiting for this extent */
2253 }
2254 spin_unlock_irq(&mdev->al_lock);
2255
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002256 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
2257 (int)part_stat_read(&disk->part0, sectors[1]) -
2258 atomic_read(&mdev->rs_sect_ev);
Philipp Reisnere3555d82010-11-07 15:56:29 +01002259
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002260 if (!mdev->rs_last_events || curr_events - mdev->rs_last_events > 64) {
2261 unsigned long rs_left;
2262 int i;
2263
2264 mdev->rs_last_events = curr_events;
2265
2266 /* sync speed average over the last 2*DRBD_SYNC_MARK_STEP,
2267 * approx. */
Lars Ellenberg2649f082010-11-05 10:05:47 +01002268 i = (mdev->rs_last_mark + DRBD_SYNC_MARKS-1) % DRBD_SYNC_MARKS;
2269
2270 if (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T)
2271 rs_left = mdev->ov_left;
2272 else
2273 rs_left = drbd_bm_total_weight(mdev) - mdev->rs_failed;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002274
2275 dt = ((long)jiffies - (long)mdev->rs_mark_time[i]) / HZ;
2276 if (!dt)
2277 dt++;
2278 db = mdev->rs_mark_left[i] - rs_left;
2279 dbdt = Bit2KB(db/dt);
2280
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002281 if (dbdt > c_min_rate)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002282 throttle = 1;
2283 }
2284 return throttle;
2285}
2286
2287
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002288static int receive_DataRequest(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002289{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002290 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002291 sector_t sector;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002292 sector_t capacity;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002293 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002294 struct digest_info *di = NULL;
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002295 int size, verb;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002296 unsigned int fault_type;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02002297 struct p_block_req *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002298
2299 mdev = vnr_to_mdev(tconn, pi->vnr);
2300 if (!mdev)
2301 return -EIO;
2302 capacity = drbd_get_capacity(mdev->this_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002303
2304 sector = be64_to_cpu(p->sector);
2305 size = be32_to_cpu(p->blksize);
2306
Andreas Gruenbacherc670a392011-02-21 12:41:39 +01002307 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002308 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2309 (unsigned long long)sector, size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002310 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002311 }
2312 if (sector + (size>>9) > capacity) {
2313 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2314 (unsigned long long)sector, size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002315 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002316 }
2317
2318 if (!get_ldev_if_state(mdev, D_UP_TO_DATE)) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002319 verb = 1;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002320 switch (pi->cmd) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002321 case P_DATA_REQUEST:
2322 drbd_send_ack_rp(mdev, P_NEG_DREPLY, p);
2323 break;
2324 case P_RS_DATA_REQUEST:
2325 case P_CSUM_RS_REQUEST:
2326 case P_OV_REQUEST:
2327 drbd_send_ack_rp(mdev, P_NEG_RS_DREPLY , p);
2328 break;
2329 case P_OV_REPLY:
2330 verb = 0;
2331 dec_rs_pending(mdev);
2332 drbd_send_ack_ex(mdev, P_OV_RESULT, sector, size, ID_IN_SYNC);
2333 break;
2334 default:
Andreas Gruenbacher49ba9b12011-03-25 00:35:45 +01002335 BUG();
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002336 }
2337 if (verb && __ratelimit(&drbd_ratelimit_state))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002338 dev_err(DEV, "Can not satisfy peer's read request, "
2339 "no local data.\n");
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002340
Lars Ellenberga821cc42010-09-06 12:31:37 +02002341 /* drain possibly payload */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002342 return drbd_drain_block(mdev, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002343 }
2344
2345 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
2346 * "criss-cross" setup, that might cause write-out on some other DRBD,
2347 * which in turn might block on the other node at this very place. */
Andreas Gruenbacher0db55362011-04-06 16:09:15 +02002348 peer_req = drbd_alloc_peer_req(mdev, p->block_id, sector, size, GFP_NOIO);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002349 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002350 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002351 return -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002352 }
2353
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002354 switch (pi->cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002355 case P_DATA_REQUEST:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002356 peer_req->w.cb = w_e_end_data_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002357 fault_type = DRBD_FAULT_DT_RD;
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002358 /* application IO, don't drbd_rs_begin_io */
2359 goto submit;
2360
Philipp Reisnerb411b362009-09-25 16:07:19 -07002361 case P_RS_DATA_REQUEST:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002362 peer_req->w.cb = w_e_end_rsdata_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002363 fault_type = DRBD_FAULT_RS_RD;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002364 /* used in the sector offset progress display */
2365 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002366 break;
2367
2368 case P_OV_REPLY:
2369 case P_CSUM_RS_REQUEST:
2370 fault_type = DRBD_FAULT_RS_RD;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002371 di = kmalloc(sizeof(*di) + pi->size, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002372 if (!di)
2373 goto out_free_e;
2374
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002375 di->digest_size = pi->size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002376 di->digest = (((char *)di)+sizeof(struct digest_info));
2377
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002378 peer_req->digest = di;
2379 peer_req->flags |= EE_HAS_DIGEST;
Lars Ellenbergc36c3ce2010-08-11 20:42:55 +02002380
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002381 if (drbd_recv_all(mdev->tconn, di->digest, pi->size))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002382 goto out_free_e;
2383
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002384 if (pi->cmd == P_CSUM_RS_REQUEST) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002385 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002386 peer_req->w.cb = w_e_end_csum_rs_req;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002387 /* used in the sector offset progress display */
2388 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002389 } else if (pi->cmd == P_OV_REPLY) {
Lars Ellenberg2649f082010-11-05 10:05:47 +01002390 /* track progress, we may need to throttle */
2391 atomic_add(size >> 9, &mdev->rs_sect_in);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002392 peer_req->w.cb = w_e_end_ov_reply;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002393 dec_rs_pending(mdev);
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002394 /* drbd_rs_begin_io done when we sent this request,
2395 * but accounting still needs to be done. */
2396 goto submit_for_resync;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002397 }
2398 break;
2399
2400 case P_OV_REQUEST:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002401 if (mdev->ov_start_sector == ~(sector_t)0 &&
Philipp Reisner31890f42011-01-19 14:12:51 +01002402 mdev->tconn->agreed_pro_version >= 90) {
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002403 unsigned long now = jiffies;
2404 int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002405 mdev->ov_start_sector = sector;
2406 mdev->ov_position = sector;
Lars Ellenberg30b743a2010-11-05 09:39:06 +01002407 mdev->ov_left = drbd_bm_bits(mdev) - BM_SECT_TO_BIT(sector);
2408 mdev->rs_total = mdev->ov_left;
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002409 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
2410 mdev->rs_mark_left[i] = mdev->ov_left;
2411 mdev->rs_mark_time[i] = now;
2412 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002413 dev_info(DEV, "Online Verify start sector: %llu\n",
2414 (unsigned long long)sector);
2415 }
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002416 peer_req->w.cb = w_e_end_ov_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002417 fault_type = DRBD_FAULT_RS_RD;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002418 break;
2419
Philipp Reisnerb411b362009-09-25 16:07:19 -07002420 default:
Andreas Gruenbacher49ba9b12011-03-25 00:35:45 +01002421 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002422 }
2423
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002424 /* Throttle, drbd_rs_begin_io and submit should become asynchronous
2425 * wrt the receiver, but it is not as straightforward as it may seem.
2426 * Various places in the resync start and stop logic assume resync
2427 * requests are processed in order, requeuing this on the worker thread
2428 * introduces a bunch of new code for synchronization between threads.
2429 *
2430 * Unlimited throttling before drbd_rs_begin_io may stall the resync
2431 * "forever", throttling after drbd_rs_begin_io will lock that extent
2432 * for application writes for the same time. For now, just throttle
2433 * here, where the rest of the code expects the receiver to sleep for
2434 * a while, anyways.
2435 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002436
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002437 /* Throttle before drbd_rs_begin_io, as that locks out application IO;
2438 * this defers syncer requests for some time, before letting at least
2439 * on request through. The resync controller on the receiving side
2440 * will adapt to the incoming rate accordingly.
2441 *
2442 * We cannot throttle here if remote is Primary/SyncTarget:
2443 * we would also throttle its application reads.
2444 * In that case, throttling is done on the SyncTarget only.
2445 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002446 if (mdev->state.peer != R_PRIMARY && drbd_rs_should_slow_down(mdev, sector))
2447 schedule_timeout_uninterruptible(HZ/10);
2448 if (drbd_rs_begin_io(mdev, sector))
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002449 goto out_free_e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002450
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002451submit_for_resync:
2452 atomic_add(size >> 9, &mdev->rs_sect_ev);
2453
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002454submit:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002455 inc_unacked(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002456 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002457 list_add_tail(&peer_req->w.list, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002458 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002459
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01002460 if (drbd_submit_peer_request(mdev, peer_req, READ, fault_type) == 0)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002461 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002462
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01002463 /* don't care for the reason here */
2464 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002465 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002466 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002467 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002468 /* no drbd_rs_complete_io(), we are dropping the connection anyways */
2469
Philipp Reisnerb411b362009-09-25 16:07:19 -07002470out_free_e:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002471 put_ldev(mdev);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02002472 drbd_free_peer_req(mdev, peer_req);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002473 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002474}
2475
2476static int drbd_asb_recover_0p(struct drbd_conf *mdev) __must_hold(local)
2477{
2478 int self, peer, rv = -100;
2479 unsigned long ch_self, ch_peer;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002480 enum drbd_after_sb_p after_sb_0p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002481
2482 self = mdev->ldev->md.uuid[UI_BITMAP] & 1;
2483 peer = mdev->p_uuid[UI_BITMAP] & 1;
2484
2485 ch_peer = mdev->p_uuid[UI_SIZE];
2486 ch_self = mdev->comm_bm_set;
2487
Philipp Reisner44ed1672011-04-19 17:10:19 +02002488 rcu_read_lock();
2489 after_sb_0p = rcu_dereference(mdev->tconn->net_conf)->after_sb_0p;
2490 rcu_read_unlock();
2491 switch (after_sb_0p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002492 case ASB_CONSENSUS:
2493 case ASB_DISCARD_SECONDARY:
2494 case ASB_CALL_HELPER:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002495 case ASB_VIOLENTLY:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002496 dev_err(DEV, "Configuration error.\n");
2497 break;
2498 case ASB_DISCONNECT:
2499 break;
2500 case ASB_DISCARD_YOUNGER_PRI:
2501 if (self == 0 && peer == 1) {
2502 rv = -1;
2503 break;
2504 }
2505 if (self == 1 && peer == 0) {
2506 rv = 1;
2507 break;
2508 }
2509 /* Else fall through to one of the other strategies... */
2510 case ASB_DISCARD_OLDER_PRI:
2511 if (self == 0 && peer == 1) {
2512 rv = 1;
2513 break;
2514 }
2515 if (self == 1 && peer == 0) {
2516 rv = -1;
2517 break;
2518 }
2519 /* Else fall through to one of the other strategies... */
Lars Ellenbergad19bf62009-10-14 09:36:49 +02002520 dev_warn(DEV, "Discard younger/older primary did not find a decision\n"
Philipp Reisnerb411b362009-09-25 16:07:19 -07002521 "Using discard-least-changes instead\n");
2522 case ASB_DISCARD_ZERO_CHG:
2523 if (ch_peer == 0 && ch_self == 0) {
Philipp Reisner25703f82011-02-07 14:35:25 +01002524 rv = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002525 ? -1 : 1;
2526 break;
2527 } else {
2528 if (ch_peer == 0) { rv = 1; break; }
2529 if (ch_self == 0) { rv = -1; break; }
2530 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002531 if (after_sb_0p == ASB_DISCARD_ZERO_CHG)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002532 break;
2533 case ASB_DISCARD_LEAST_CHG:
2534 if (ch_self < ch_peer)
2535 rv = -1;
2536 else if (ch_self > ch_peer)
2537 rv = 1;
2538 else /* ( ch_self == ch_peer ) */
2539 /* Well, then use something else. */
Philipp Reisner25703f82011-02-07 14:35:25 +01002540 rv = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002541 ? -1 : 1;
2542 break;
2543 case ASB_DISCARD_LOCAL:
2544 rv = -1;
2545 break;
2546 case ASB_DISCARD_REMOTE:
2547 rv = 1;
2548 }
2549
2550 return rv;
2551}
2552
2553static int drbd_asb_recover_1p(struct drbd_conf *mdev) __must_hold(local)
2554{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002555 int hg, rv = -100;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002556 enum drbd_after_sb_p after_sb_1p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002557
Philipp Reisner44ed1672011-04-19 17:10:19 +02002558 rcu_read_lock();
2559 after_sb_1p = rcu_dereference(mdev->tconn->net_conf)->after_sb_1p;
2560 rcu_read_unlock();
2561 switch (after_sb_1p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002562 case ASB_DISCARD_YOUNGER_PRI:
2563 case ASB_DISCARD_OLDER_PRI:
2564 case ASB_DISCARD_LEAST_CHG:
2565 case ASB_DISCARD_LOCAL:
2566 case ASB_DISCARD_REMOTE:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002567 case ASB_DISCARD_ZERO_CHG:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002568 dev_err(DEV, "Configuration error.\n");
2569 break;
2570 case ASB_DISCONNECT:
2571 break;
2572 case ASB_CONSENSUS:
2573 hg = drbd_asb_recover_0p(mdev);
2574 if (hg == -1 && mdev->state.role == R_SECONDARY)
2575 rv = hg;
2576 if (hg == 1 && mdev->state.role == R_PRIMARY)
2577 rv = hg;
2578 break;
2579 case ASB_VIOLENTLY:
2580 rv = drbd_asb_recover_0p(mdev);
2581 break;
2582 case ASB_DISCARD_SECONDARY:
2583 return mdev->state.role == R_PRIMARY ? 1 : -1;
2584 case ASB_CALL_HELPER:
2585 hg = drbd_asb_recover_0p(mdev);
2586 if (hg == -1 && mdev->state.role == R_PRIMARY) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002587 enum drbd_state_rv rv2;
2588
2589 drbd_set_role(mdev, R_SECONDARY, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002590 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2591 * we might be here in C_WF_REPORT_PARAMS which is transient.
2592 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002593 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2594 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002595 drbd_khelper(mdev, "pri-lost-after-sb");
2596 } else {
2597 dev_warn(DEV, "Successfully gave up primary role.\n");
2598 rv = hg;
2599 }
2600 } else
2601 rv = hg;
2602 }
2603
2604 return rv;
2605}
2606
2607static int drbd_asb_recover_2p(struct drbd_conf *mdev) __must_hold(local)
2608{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002609 int hg, rv = -100;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002610 enum drbd_after_sb_p after_sb_2p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002611
Philipp Reisner44ed1672011-04-19 17:10:19 +02002612 rcu_read_lock();
2613 after_sb_2p = rcu_dereference(mdev->tconn->net_conf)->after_sb_2p;
2614 rcu_read_unlock();
2615 switch (after_sb_2p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002616 case ASB_DISCARD_YOUNGER_PRI:
2617 case ASB_DISCARD_OLDER_PRI:
2618 case ASB_DISCARD_LEAST_CHG:
2619 case ASB_DISCARD_LOCAL:
2620 case ASB_DISCARD_REMOTE:
2621 case ASB_CONSENSUS:
2622 case ASB_DISCARD_SECONDARY:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002623 case ASB_DISCARD_ZERO_CHG:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002624 dev_err(DEV, "Configuration error.\n");
2625 break;
2626 case ASB_VIOLENTLY:
2627 rv = drbd_asb_recover_0p(mdev);
2628 break;
2629 case ASB_DISCONNECT:
2630 break;
2631 case ASB_CALL_HELPER:
2632 hg = drbd_asb_recover_0p(mdev);
2633 if (hg == -1) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002634 enum drbd_state_rv rv2;
2635
Philipp Reisnerb411b362009-09-25 16:07:19 -07002636 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2637 * we might be here in C_WF_REPORT_PARAMS which is transient.
2638 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002639 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2640 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002641 drbd_khelper(mdev, "pri-lost-after-sb");
2642 } else {
2643 dev_warn(DEV, "Successfully gave up primary role.\n");
2644 rv = hg;
2645 }
2646 } else
2647 rv = hg;
2648 }
2649
2650 return rv;
2651}
2652
2653static void drbd_uuid_dump(struct drbd_conf *mdev, char *text, u64 *uuid,
2654 u64 bits, u64 flags)
2655{
2656 if (!uuid) {
2657 dev_info(DEV, "%s uuid info vanished while I was looking!\n", text);
2658 return;
2659 }
2660 dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX bits:%llu flags:%llX\n",
2661 text,
2662 (unsigned long long)uuid[UI_CURRENT],
2663 (unsigned long long)uuid[UI_BITMAP],
2664 (unsigned long long)uuid[UI_HISTORY_START],
2665 (unsigned long long)uuid[UI_HISTORY_END],
2666 (unsigned long long)bits,
2667 (unsigned long long)flags);
2668}
2669
2670/*
2671 100 after split brain try auto recover
2672 2 C_SYNC_SOURCE set BitMap
2673 1 C_SYNC_SOURCE use BitMap
2674 0 no Sync
2675 -1 C_SYNC_TARGET use BitMap
2676 -2 C_SYNC_TARGET set BitMap
2677 -100 after split brain, disconnect
2678-1000 unrelated data
Philipp Reisner4a23f262011-01-11 17:42:17 +01002679-1091 requires proto 91
2680-1096 requires proto 96
Philipp Reisnerb411b362009-09-25 16:07:19 -07002681 */
2682static int drbd_uuid_compare(struct drbd_conf *mdev, int *rule_nr) __must_hold(local)
2683{
2684 u64 self, peer;
2685 int i, j;
2686
2687 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2688 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2689
2690 *rule_nr = 10;
2691 if (self == UUID_JUST_CREATED && peer == UUID_JUST_CREATED)
2692 return 0;
2693
2694 *rule_nr = 20;
2695 if ((self == UUID_JUST_CREATED || self == (u64)0) &&
2696 peer != UUID_JUST_CREATED)
2697 return -2;
2698
2699 *rule_nr = 30;
2700 if (self != UUID_JUST_CREATED &&
2701 (peer == UUID_JUST_CREATED || peer == (u64)0))
2702 return 2;
2703
2704 if (self == peer) {
2705 int rct, dc; /* roles at crash time */
2706
2707 if (mdev->p_uuid[UI_BITMAP] == (u64)0 && mdev->ldev->md.uuid[UI_BITMAP] != (u64)0) {
2708
Philipp Reisner31890f42011-01-19 14:12:51 +01002709 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002710 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002711
2712 if ((mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) &&
2713 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1))) {
2714 dev_info(DEV, "was SyncSource, missed the resync finished event, corrected myself:\n");
2715 drbd_uuid_set_bm(mdev, 0UL);
2716
2717 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2718 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2719 *rule_nr = 34;
2720 } else {
2721 dev_info(DEV, "was SyncSource (peer failed to write sync_uuid)\n");
2722 *rule_nr = 36;
2723 }
2724
2725 return 1;
2726 }
2727
2728 if (mdev->ldev->md.uuid[UI_BITMAP] == (u64)0 && mdev->p_uuid[UI_BITMAP] != (u64)0) {
2729
Philipp Reisner31890f42011-01-19 14:12:51 +01002730 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002731 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002732
2733 if ((mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_BITMAP] & ~((u64)1)) &&
2734 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1))) {
2735 dev_info(DEV, "was SyncTarget, peer missed the resync finished event, corrected peer:\n");
2736
2737 mdev->p_uuid[UI_HISTORY_START + 1] = mdev->p_uuid[UI_HISTORY_START];
2738 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_BITMAP];
2739 mdev->p_uuid[UI_BITMAP] = 0UL;
2740
2741 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2742 *rule_nr = 35;
2743 } else {
2744 dev_info(DEV, "was SyncTarget (failed to write sync_uuid)\n");
2745 *rule_nr = 37;
2746 }
2747
2748 return -1;
2749 }
2750
2751 /* Common power [off|failure] */
2752 rct = (test_bit(CRASHED_PRIMARY, &mdev->flags) ? 1 : 0) +
2753 (mdev->p_uuid[UI_FLAGS] & 2);
2754 /* lowest bit is set when we were primary,
2755 * next bit (weight 2) is set when peer was primary */
2756 *rule_nr = 40;
2757
2758 switch (rct) {
2759 case 0: /* !self_pri && !peer_pri */ return 0;
2760 case 1: /* self_pri && !peer_pri */ return 1;
2761 case 2: /* !self_pri && peer_pri */ return -1;
2762 case 3: /* self_pri && peer_pri */
Philipp Reisner25703f82011-02-07 14:35:25 +01002763 dc = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002764 return dc ? -1 : 1;
2765 }
2766 }
2767
2768 *rule_nr = 50;
2769 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2770 if (self == peer)
2771 return -1;
2772
2773 *rule_nr = 51;
2774 peer = mdev->p_uuid[UI_HISTORY_START] & ~((u64)1);
2775 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002776 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002777 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) ==
2778 (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1)) :
2779 peer + UUID_NEW_BM_OFFSET == (mdev->p_uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002780 /* The last P_SYNC_UUID did not get though. Undo the last start of
2781 resync as sync source modifications of the peer's UUIDs. */
2782
Philipp Reisner31890f42011-01-19 14:12:51 +01002783 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002784 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002785
2786 mdev->p_uuid[UI_BITMAP] = mdev->p_uuid[UI_HISTORY_START];
2787 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_HISTORY_START + 1];
Philipp Reisner4a23f262011-01-11 17:42:17 +01002788
2789 dev_info(DEV, "Did not got last syncUUID packet, corrected:\n");
2790 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2791
Philipp Reisnerb411b362009-09-25 16:07:19 -07002792 return -1;
2793 }
2794 }
2795
2796 *rule_nr = 60;
2797 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2798 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2799 peer = mdev->p_uuid[i] & ~((u64)1);
2800 if (self == peer)
2801 return -2;
2802 }
2803
2804 *rule_nr = 70;
2805 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2806 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2807 if (self == peer)
2808 return 1;
2809
2810 *rule_nr = 71;
2811 self = mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1);
2812 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002813 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002814 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) ==
2815 (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) :
2816 self + UUID_NEW_BM_OFFSET == (mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002817 /* The last P_SYNC_UUID did not get though. Undo the last start of
2818 resync as sync source modifications of our UUIDs. */
2819
Philipp Reisner31890f42011-01-19 14:12:51 +01002820 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002821 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002822
2823 _drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_HISTORY_START]);
2824 _drbd_uuid_set(mdev, UI_HISTORY_START, mdev->ldev->md.uuid[UI_HISTORY_START + 1]);
2825
Philipp Reisner4a23f262011-01-11 17:42:17 +01002826 dev_info(DEV, "Last syncUUID did not get through, corrected:\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002827 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2828 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2829
2830 return 1;
2831 }
2832 }
2833
2834
2835 *rule_nr = 80;
Philipp Reisnerd8c2a362009-11-18 15:52:51 +01002836 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002837 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2838 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2839 if (self == peer)
2840 return 2;
2841 }
2842
2843 *rule_nr = 90;
2844 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2845 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2846 if (self == peer && self != ((u64)0))
2847 return 100;
2848
2849 *rule_nr = 100;
2850 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2851 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2852 for (j = UI_HISTORY_START; j <= UI_HISTORY_END; j++) {
2853 peer = mdev->p_uuid[j] & ~((u64)1);
2854 if (self == peer)
2855 return -100;
2856 }
2857 }
2858
2859 return -1000;
2860}
2861
2862/* drbd_sync_handshake() returns the new conn state on success, or
2863 CONN_MASK (-1) on failure.
2864 */
2865static enum drbd_conns drbd_sync_handshake(struct drbd_conf *mdev, enum drbd_role peer_role,
2866 enum drbd_disk_state peer_disk) __must_hold(local)
2867{
Philipp Reisnerb411b362009-09-25 16:07:19 -07002868 enum drbd_conns rv = C_MASK;
2869 enum drbd_disk_state mydisk;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002870 struct net_conf *nc;
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02002871 int hg, rule_nr, rr_conflict, tentative;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002872
2873 mydisk = mdev->state.disk;
2874 if (mydisk == D_NEGOTIATING)
2875 mydisk = mdev->new_state_tmp.disk;
2876
2877 dev_info(DEV, "drbd_sync_handshake:\n");
2878 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid, mdev->comm_bm_set, 0);
2879 drbd_uuid_dump(mdev, "peer", mdev->p_uuid,
2880 mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2881
2882 hg = drbd_uuid_compare(mdev, &rule_nr);
2883
2884 dev_info(DEV, "uuid_compare()=%d by rule %d\n", hg, rule_nr);
2885
2886 if (hg == -1000) {
2887 dev_alert(DEV, "Unrelated data, aborting!\n");
2888 return C_MASK;
2889 }
Philipp Reisner4a23f262011-01-11 17:42:17 +01002890 if (hg < -1000) {
2891 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 -07002892 return C_MASK;
2893 }
2894
2895 if ((mydisk == D_INCONSISTENT && peer_disk > D_INCONSISTENT) ||
2896 (peer_disk == D_INCONSISTENT && mydisk > D_INCONSISTENT)) {
2897 int f = (hg == -100) || abs(hg) == 2;
2898 hg = mydisk > D_INCONSISTENT ? 1 : -1;
2899 if (f)
2900 hg = hg*2;
2901 dev_info(DEV, "Becoming sync %s due to disk states.\n",
2902 hg > 0 ? "source" : "target");
2903 }
2904
Adam Gandelman3a11a482010-04-08 16:48:23 -07002905 if (abs(hg) == 100)
2906 drbd_khelper(mdev, "initial-split-brain");
2907
Philipp Reisner44ed1672011-04-19 17:10:19 +02002908 rcu_read_lock();
2909 nc = rcu_dereference(mdev->tconn->net_conf);
2910
2911 if (hg == 100 || (hg == -100 && nc->always_asbp)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002912 int pcount = (mdev->state.role == R_PRIMARY)
2913 + (peer_role == R_PRIMARY);
2914 int forced = (hg == -100);
2915
2916 switch (pcount) {
2917 case 0:
2918 hg = drbd_asb_recover_0p(mdev);
2919 break;
2920 case 1:
2921 hg = drbd_asb_recover_1p(mdev);
2922 break;
2923 case 2:
2924 hg = drbd_asb_recover_2p(mdev);
2925 break;
2926 }
2927 if (abs(hg) < 100) {
2928 dev_warn(DEV, "Split-Brain detected, %d primaries, "
2929 "automatically solved. Sync from %s node\n",
2930 pcount, (hg < 0) ? "peer" : "this");
2931 if (forced) {
2932 dev_warn(DEV, "Doing a full sync, since"
2933 " UUIDs where ambiguous.\n");
2934 hg = hg*2;
2935 }
2936 }
2937 }
2938
2939 if (hg == -100) {
Philipp Reisner08b165b2011-09-05 16:22:33 +02002940 if (test_bit(DISCARD_MY_DATA, &mdev->flags) && !(mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002941 hg = -1;
Philipp Reisner08b165b2011-09-05 16:22:33 +02002942 if (!test_bit(DISCARD_MY_DATA, &mdev->flags) && (mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002943 hg = 1;
2944
2945 if (abs(hg) < 100)
2946 dev_warn(DEV, "Split-Brain detected, manually solved. "
2947 "Sync from %s node\n",
2948 (hg < 0) ? "peer" : "this");
2949 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002950 rr_conflict = nc->rr_conflict;
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02002951 tentative = nc->tentative;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002952 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002953
2954 if (hg == -100) {
Lars Ellenberg580b9762010-02-26 23:15:23 +01002955 /* FIXME this log message is not correct if we end up here
2956 * after an attempted attach on a diskless node.
2957 * We just refuse to attach -- well, we drop the "connection"
2958 * to that disk, in a way... */
Adam Gandelman3a11a482010-04-08 16:48:23 -07002959 dev_alert(DEV, "Split-Brain detected but unresolved, dropping connection!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002960 drbd_khelper(mdev, "split-brain");
2961 return C_MASK;
2962 }
2963
2964 if (hg > 0 && mydisk <= D_INCONSISTENT) {
2965 dev_err(DEV, "I shall become SyncSource, but I am inconsistent!\n");
2966 return C_MASK;
2967 }
2968
2969 if (hg < 0 && /* by intention we do not use mydisk here. */
2970 mdev->state.role == R_PRIMARY && mdev->state.disk >= D_CONSISTENT) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02002971 switch (rr_conflict) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002972 case ASB_CALL_HELPER:
2973 drbd_khelper(mdev, "pri-lost");
2974 /* fall through */
2975 case ASB_DISCONNECT:
2976 dev_err(DEV, "I shall become SyncTarget, but I am primary!\n");
2977 return C_MASK;
2978 case ASB_VIOLENTLY:
2979 dev_warn(DEV, "Becoming SyncTarget, violating the stable-data"
2980 "assumption\n");
2981 }
2982 }
2983
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02002984 if (tentative || test_bit(CONN_DRY_RUN, &mdev->tconn->flags)) {
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002985 if (hg == 0)
2986 dev_info(DEV, "dry-run connect: No resync, would become Connected immediately.\n");
2987 else
2988 dev_info(DEV, "dry-run connect: Would become %s, doing a %s resync.",
2989 drbd_conn_str(hg > 0 ? C_SYNC_SOURCE : C_SYNC_TARGET),
2990 abs(hg) >= 2 ? "full" : "bit-map based");
2991 return C_MASK;
2992 }
2993
Philipp Reisnerb411b362009-09-25 16:07:19 -07002994 if (abs(hg) >= 2) {
2995 dev_info(DEV, "Writing the whole bitmap, full sync required after drbd_sync_handshake.\n");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002996 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write, "set_n_write from sync_handshake",
2997 BM_LOCKED_SET_ALLOWED))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002998 return C_MASK;
2999 }
3000
3001 if (hg > 0) { /* become sync source. */
3002 rv = C_WF_BITMAP_S;
3003 } else if (hg < 0) { /* become sync target */
3004 rv = C_WF_BITMAP_T;
3005 } else {
3006 rv = C_CONNECTED;
3007 if (drbd_bm_total_weight(mdev)) {
3008 dev_info(DEV, "No resync, but %lu bits in bitmap!\n",
3009 drbd_bm_total_weight(mdev));
3010 }
3011 }
3012
3013 return rv;
3014}
3015
Philipp Reisnerf179d762011-05-16 17:31:47 +02003016static enum drbd_after_sb_p convert_after_sb(enum drbd_after_sb_p peer)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003017{
3018 /* ASB_DISCARD_REMOTE - ASB_DISCARD_LOCAL is valid */
Philipp Reisnerf179d762011-05-16 17:31:47 +02003019 if (peer == ASB_DISCARD_REMOTE)
3020 return ASB_DISCARD_LOCAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003021
3022 /* any other things with ASB_DISCARD_REMOTE or ASB_DISCARD_LOCAL are invalid */
Philipp Reisnerf179d762011-05-16 17:31:47 +02003023 if (peer == ASB_DISCARD_LOCAL)
3024 return ASB_DISCARD_REMOTE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003025
3026 /* everything else is valid if they are equal on both sides. */
Philipp Reisnerf179d762011-05-16 17:31:47 +02003027 return peer;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003028}
3029
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003030static int receive_protocol(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003031{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003032 struct p_protocol *p = pi->data;
Philipp Reisner036b17e2011-05-16 17:38:11 +02003033 enum drbd_after_sb_p p_after_sb_0p, p_after_sb_1p, p_after_sb_2p;
3034 int p_proto, p_discard_my_data, p_two_primaries, cf;
3035 struct net_conf *nc, *old_net_conf, *new_net_conf = NULL;
3036 char integrity_alg[SHARED_SECRET_MAX] = "";
Andreas Gruenbacheraccdbcc2011-07-15 17:41:09 +02003037 struct crypto_hash *peer_integrity_tfm = NULL;
Philipp Reisner7aca6c72011-05-17 10:12:56 +02003038 void *int_dig_in = NULL, *int_dig_vv = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003039
Philipp Reisnerb411b362009-09-25 16:07:19 -07003040 p_proto = be32_to_cpu(p->protocol);
3041 p_after_sb_0p = be32_to_cpu(p->after_sb_0p);
3042 p_after_sb_1p = be32_to_cpu(p->after_sb_1p);
3043 p_after_sb_2p = be32_to_cpu(p->after_sb_2p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003044 p_two_primaries = be32_to_cpu(p->two_primaries);
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003045 cf = be32_to_cpu(p->conn_flags);
Andreas Gruenbacher6139f602011-05-06 20:00:02 +02003046 p_discard_my_data = cf & CF_DISCARD_MY_DATA;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003047
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003048 if (tconn->agreed_pro_version >= 87) {
3049 int err;
3050
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02003051 if (pi->size > sizeof(integrity_alg))
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003052 return -EIO;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02003053 err = drbd_recv_all(tconn, integrity_alg, pi->size);
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003054 if (err)
3055 return err;
Philipp Reisner036b17e2011-05-16 17:38:11 +02003056 integrity_alg[SHARED_SECRET_MAX - 1] = 0;
3057 }
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003058
Andreas Gruenbacher7d4c7822011-07-17 23:06:12 +02003059 if (pi->cmd != P_PROTOCOL_UPDATE) {
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003060 clear_bit(CONN_DRY_RUN, &tconn->flags);
Philipp Reisner036b17e2011-05-16 17:38:11 +02003061
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003062 if (cf & CF_DRY_RUN)
3063 set_bit(CONN_DRY_RUN, &tconn->flags);
3064
3065 rcu_read_lock();
3066 nc = rcu_dereference(tconn->net_conf);
3067
3068 if (p_proto != nc->wire_protocol) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003069 conn_err(tconn, "incompatible %s settings\n", "protocol");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003070 goto disconnect_rcu_unlock;
3071 }
3072
3073 if (convert_after_sb(p_after_sb_0p) != nc->after_sb_0p) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003074 conn_err(tconn, "incompatible %s settings\n", "after-sb-0pri");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003075 goto disconnect_rcu_unlock;
3076 }
3077
3078 if (convert_after_sb(p_after_sb_1p) != nc->after_sb_1p) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003079 conn_err(tconn, "incompatible %s settings\n", "after-sb-1pri");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003080 goto disconnect_rcu_unlock;
3081 }
3082
3083 if (convert_after_sb(p_after_sb_2p) != nc->after_sb_2p) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003084 conn_err(tconn, "incompatible %s settings\n", "after-sb-2pri");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003085 goto disconnect_rcu_unlock;
3086 }
3087
3088 if (p_discard_my_data && nc->discard_my_data) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003089 conn_err(tconn, "incompatible %s settings\n", "discard-my-data");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003090 goto disconnect_rcu_unlock;
3091 }
3092
3093 if (p_two_primaries != nc->two_primaries) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003094 conn_err(tconn, "incompatible %s settings\n", "allow-two-primaries");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003095 goto disconnect_rcu_unlock;
3096 }
3097
3098 if (strcmp(integrity_alg, nc->integrity_alg)) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003099 conn_err(tconn, "incompatible %s settings\n", "data-integrity-alg");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003100 goto disconnect_rcu_unlock;
3101 }
3102
3103 rcu_read_unlock();
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003104 }
Andreas Gruenbacher7d4c7822011-07-17 23:06:12 +02003105
3106 if (integrity_alg[0]) {
3107 int hash_size;
3108
3109 /*
3110 * We can only change the peer data integrity algorithm
3111 * here. Changing our own data integrity algorithm
3112 * requires that we send a P_PROTOCOL_UPDATE packet at
3113 * the same time; otherwise, the peer has no way to
3114 * tell between which packets the algorithm should
3115 * change.
3116 */
3117
3118 peer_integrity_tfm = crypto_alloc_hash(integrity_alg, 0, CRYPTO_ALG_ASYNC);
3119 if (!peer_integrity_tfm) {
3120 conn_err(tconn, "peer data-integrity-alg %s not supported\n",
3121 integrity_alg);
3122 goto disconnect;
3123 }
3124
3125 hash_size = crypto_hash_digestsize(peer_integrity_tfm);
3126 int_dig_in = kmalloc(hash_size, GFP_KERNEL);
3127 int_dig_vv = kmalloc(hash_size, GFP_KERNEL);
3128 if (!(int_dig_in && int_dig_vv)) {
3129 conn_err(tconn, "Allocation of buffers for data integrity checking failed\n");
3130 goto disconnect;
3131 }
3132 }
3133
3134 new_net_conf = kmalloc(sizeof(struct net_conf), GFP_KERNEL);
3135 if (!new_net_conf) {
3136 conn_err(tconn, "Allocation of new net_conf failed\n");
3137 goto disconnect;
3138 }
3139
3140 mutex_lock(&tconn->data.mutex);
3141 mutex_lock(&tconn->conf_update);
3142 old_net_conf = tconn->net_conf;
3143 *new_net_conf = *old_net_conf;
3144
3145 new_net_conf->wire_protocol = p_proto;
3146 new_net_conf->after_sb_0p = convert_after_sb(p_after_sb_0p);
3147 new_net_conf->after_sb_1p = convert_after_sb(p_after_sb_1p);
3148 new_net_conf->after_sb_2p = convert_after_sb(p_after_sb_2p);
3149 new_net_conf->two_primaries = p_two_primaries;
3150
3151 rcu_assign_pointer(tconn->net_conf, new_net_conf);
3152 mutex_unlock(&tconn->conf_update);
3153 mutex_unlock(&tconn->data.mutex);
3154
3155 crypto_free_hash(tconn->peer_integrity_tfm);
3156 kfree(tconn->int_dig_in);
3157 kfree(tconn->int_dig_vv);
3158 tconn->peer_integrity_tfm = peer_integrity_tfm;
3159 tconn->int_dig_in = int_dig_in;
3160 tconn->int_dig_vv = int_dig_vv;
3161
3162 if (strcmp(old_net_conf->integrity_alg, integrity_alg))
3163 conn_info(tconn, "peer data-integrity-alg: %s\n",
3164 integrity_alg[0] ? integrity_alg : "(none)");
3165
3166 synchronize_rcu();
3167 kfree(old_net_conf);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003168 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003169
Philipp Reisner44ed1672011-04-19 17:10:19 +02003170disconnect_rcu_unlock:
3171 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07003172disconnect:
Andreas Gruenbacherb792c352011-07-15 16:48:49 +02003173 crypto_free_hash(peer_integrity_tfm);
Philipp Reisner036b17e2011-05-16 17:38:11 +02003174 kfree(int_dig_in);
3175 kfree(int_dig_vv);
Philipp Reisner72046242011-03-15 18:51:47 +01003176 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003177 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003178}
3179
3180/* helper function
3181 * input: alg name, feature name
3182 * return: NULL (alg name was "")
3183 * ERR_PTR(error) if something goes wrong
3184 * or the crypto hash ptr, if it worked out ok. */
3185struct crypto_hash *drbd_crypto_alloc_digest_safe(const struct drbd_conf *mdev,
3186 const char *alg, const char *name)
3187{
3188 struct crypto_hash *tfm;
3189
3190 if (!alg[0])
3191 return NULL;
3192
3193 tfm = crypto_alloc_hash(alg, 0, CRYPTO_ALG_ASYNC);
3194 if (IS_ERR(tfm)) {
3195 dev_err(DEV, "Can not allocate \"%s\" as %s (reason: %ld)\n",
3196 alg, name, PTR_ERR(tfm));
3197 return tfm;
3198 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003199 return tfm;
3200}
3201
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003202static int ignore_remaining_packet(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003203{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003204 void *buffer = tconn->data.rbuf;
3205 int size = pi->size;
3206
3207 while (size) {
3208 int s = min_t(int, size, DRBD_SOCKET_BUFFER_SIZE);
3209 s = drbd_recv(tconn, buffer, s);
3210 if (s <= 0) {
3211 if (s < 0)
3212 return s;
3213 break;
3214 }
3215 size -= s;
3216 }
3217 if (size)
3218 return -EIO;
3219 return 0;
3220}
3221
3222/*
3223 * config_unknown_volume - device configuration command for unknown volume
3224 *
3225 * When a device is added to an existing connection, the node on which the
3226 * device is added first will send configuration commands to its peer but the
3227 * peer will not know about the device yet. It will warn and ignore these
3228 * commands. Once the device is added on the second node, the second node will
3229 * send the same device configuration commands, but in the other direction.
3230 *
3231 * (We can also end up here if drbd is misconfigured.)
3232 */
3233static int config_unknown_volume(struct drbd_tconn *tconn, struct packet_info *pi)
3234{
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02003235 conn_warn(tconn, "%s packet received for volume %u, which is not configured locally\n",
3236 cmdname(pi->cmd), pi->vnr);
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003237 return ignore_remaining_packet(tconn, pi);
3238}
3239
3240static int receive_SyncParam(struct drbd_tconn *tconn, struct packet_info *pi)
3241{
3242 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003243 struct p_rs_param_95 *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003244 unsigned int header_size, data_size, exp_max_sz;
3245 struct crypto_hash *verify_tfm = NULL;
3246 struct crypto_hash *csums_tfm = NULL;
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003247 struct net_conf *old_net_conf, *new_net_conf = NULL;
Philipp Reisner813472c2011-05-03 16:47:02 +02003248 struct disk_conf *old_disk_conf = NULL, *new_disk_conf = NULL;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003249 const int apv = tconn->agreed_pro_version;
Philipp Reisner813472c2011-05-03 16:47:02 +02003250 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
Philipp Reisner778f2712010-07-06 11:14:00 +02003251 int fifo_size = 0;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003252 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003253
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003254 mdev = vnr_to_mdev(tconn, pi->vnr);
3255 if (!mdev)
3256 return config_unknown_volume(tconn, pi);
3257
Philipp Reisnerb411b362009-09-25 16:07:19 -07003258 exp_max_sz = apv <= 87 ? sizeof(struct p_rs_param)
3259 : apv == 88 ? sizeof(struct p_rs_param)
3260 + SHARED_SECRET_MAX
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003261 : apv <= 94 ? sizeof(struct p_rs_param_89)
3262 : /* apv >= 95 */ sizeof(struct p_rs_param_95);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003263
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003264 if (pi->size > exp_max_sz) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003265 dev_err(DEV, "SyncParam packet too long: received %u, expected <= %u bytes\n",
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003266 pi->size, exp_max_sz);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003267 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003268 }
3269
3270 if (apv <= 88) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003271 header_size = sizeof(struct p_rs_param);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003272 data_size = pi->size - header_size;
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003273 } else if (apv <= 94) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003274 header_size = sizeof(struct p_rs_param_89);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003275 data_size = pi->size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003276 D_ASSERT(data_size == 0);
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003277 } else {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003278 header_size = sizeof(struct p_rs_param_95);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003279 data_size = pi->size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003280 D_ASSERT(data_size == 0);
3281 }
3282
3283 /* initialize verify_alg and csums_alg */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003284 p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003285 memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
3286
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003287 err = drbd_recv_all(mdev->tconn, p, header_size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003288 if (err)
3289 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003290
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003291 mutex_lock(&mdev->tconn->conf_update);
3292 old_net_conf = mdev->tconn->net_conf;
Philipp Reisner813472c2011-05-03 16:47:02 +02003293 if (get_ldev(mdev)) {
3294 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
3295 if (!new_disk_conf) {
3296 put_ldev(mdev);
3297 mutex_unlock(&mdev->tconn->conf_update);
3298 dev_err(DEV, "Allocation of new disk_conf failed\n");
3299 return -ENOMEM;
3300 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003301
Philipp Reisner813472c2011-05-03 16:47:02 +02003302 old_disk_conf = mdev->ldev->disk_conf;
3303 *new_disk_conf = *old_disk_conf;
3304
Andreas Gruenbacher6394b932011-05-11 14:29:52 +02003305 new_disk_conf->resync_rate = be32_to_cpu(p->resync_rate);
Philipp Reisner813472c2011-05-03 16:47:02 +02003306 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003307
Philipp Reisnerb411b362009-09-25 16:07:19 -07003308 if (apv >= 88) {
3309 if (apv == 88) {
3310 if (data_size > SHARED_SECRET_MAX) {
3311 dev_err(DEV, "verify-alg too long, "
3312 "peer wants %u, accepting only %u byte\n",
3313 data_size, SHARED_SECRET_MAX);
Philipp Reisner813472c2011-05-03 16:47:02 +02003314 err = -EIO;
3315 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003316 }
3317
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003318 err = drbd_recv_all(mdev->tconn, p->verify_alg, data_size);
Philipp Reisner813472c2011-05-03 16:47:02 +02003319 if (err)
3320 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003321 /* we expect NUL terminated string */
3322 /* but just in case someone tries to be evil */
3323 D_ASSERT(p->verify_alg[data_size-1] == 0);
3324 p->verify_alg[data_size-1] = 0;
3325
3326 } else /* apv >= 89 */ {
3327 /* we still expect NUL terminated strings */
3328 /* but just in case someone tries to be evil */
3329 D_ASSERT(p->verify_alg[SHARED_SECRET_MAX-1] == 0);
3330 D_ASSERT(p->csums_alg[SHARED_SECRET_MAX-1] == 0);
3331 p->verify_alg[SHARED_SECRET_MAX-1] = 0;
3332 p->csums_alg[SHARED_SECRET_MAX-1] = 0;
3333 }
3334
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003335 if (strcmp(old_net_conf->verify_alg, p->verify_alg)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003336 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
3337 dev_err(DEV, "Different verify-alg settings. me=\"%s\" peer=\"%s\"\n",
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003338 old_net_conf->verify_alg, p->verify_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003339 goto disconnect;
3340 }
3341 verify_tfm = drbd_crypto_alloc_digest_safe(mdev,
3342 p->verify_alg, "verify-alg");
3343 if (IS_ERR(verify_tfm)) {
3344 verify_tfm = NULL;
3345 goto disconnect;
3346 }
3347 }
3348
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003349 if (apv >= 89 && strcmp(old_net_conf->csums_alg, p->csums_alg)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003350 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
3351 dev_err(DEV, "Different csums-alg settings. me=\"%s\" peer=\"%s\"\n",
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003352 old_net_conf->csums_alg, p->csums_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003353 goto disconnect;
3354 }
3355 csums_tfm = drbd_crypto_alloc_digest_safe(mdev,
3356 p->csums_alg, "csums-alg");
3357 if (IS_ERR(csums_tfm)) {
3358 csums_tfm = NULL;
3359 goto disconnect;
3360 }
3361 }
3362
Philipp Reisner813472c2011-05-03 16:47:02 +02003363 if (apv > 94 && new_disk_conf) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003364 new_disk_conf->c_plan_ahead = be32_to_cpu(p->c_plan_ahead);
3365 new_disk_conf->c_delay_target = be32_to_cpu(p->c_delay_target);
3366 new_disk_conf->c_fill_target = be32_to_cpu(p->c_fill_target);
3367 new_disk_conf->c_max_rate = be32_to_cpu(p->c_max_rate);
Philipp Reisner778f2712010-07-06 11:14:00 +02003368
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003369 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
Philipp Reisner9958c852011-05-03 16:19:31 +02003370 if (fifo_size != mdev->rs_plan_s->size) {
Philipp Reisner813472c2011-05-03 16:47:02 +02003371 new_plan = fifo_alloc(fifo_size);
3372 if (!new_plan) {
Philipp Reisner778f2712010-07-06 11:14:00 +02003373 dev_err(DEV, "kmalloc of fifo_buffer failed");
Lars Ellenbergf3990022011-03-23 14:31:09 +01003374 put_ldev(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02003375 goto disconnect;
3376 }
3377 }
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003378 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003379
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003380 if (verify_tfm || csums_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003381 new_net_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
3382 if (!new_net_conf) {
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003383 dev_err(DEV, "Allocation of new net_conf failed\n");
3384 goto disconnect;
3385 }
3386
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003387 *new_net_conf = *old_net_conf;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003388
3389 if (verify_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003390 strcpy(new_net_conf->verify_alg, p->verify_alg);
3391 new_net_conf->verify_alg_len = strlen(p->verify_alg) + 1;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003392 crypto_free_hash(mdev->tconn->verify_tfm);
3393 mdev->tconn->verify_tfm = verify_tfm;
3394 dev_info(DEV, "using verify-alg: \"%s\"\n", p->verify_alg);
3395 }
3396 if (csums_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003397 strcpy(new_net_conf->csums_alg, p->csums_alg);
3398 new_net_conf->csums_alg_len = strlen(p->csums_alg) + 1;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003399 crypto_free_hash(mdev->tconn->csums_tfm);
3400 mdev->tconn->csums_tfm = csums_tfm;
3401 dev_info(DEV, "using csums-alg: \"%s\"\n", p->csums_alg);
3402 }
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003403 rcu_assign_pointer(tconn->net_conf, new_net_conf);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003404 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003405 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003406
Philipp Reisner813472c2011-05-03 16:47:02 +02003407 if (new_disk_conf) {
3408 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
3409 put_ldev(mdev);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003410 }
Philipp Reisner813472c2011-05-03 16:47:02 +02003411
3412 if (new_plan) {
3413 old_plan = mdev->rs_plan_s;
3414 rcu_assign_pointer(mdev->rs_plan_s, new_plan);
3415 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003416
3417 mutex_unlock(&mdev->tconn->conf_update);
3418 synchronize_rcu();
3419 if (new_net_conf)
3420 kfree(old_net_conf);
3421 kfree(old_disk_conf);
Philipp Reisner813472c2011-05-03 16:47:02 +02003422 kfree(old_plan);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003423
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003424 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003425
Philipp Reisner813472c2011-05-03 16:47:02 +02003426reconnect:
3427 if (new_disk_conf) {
3428 put_ldev(mdev);
3429 kfree(new_disk_conf);
3430 }
3431 mutex_unlock(&mdev->tconn->conf_update);
3432 return -EIO;
3433
Philipp Reisnerb411b362009-09-25 16:07:19 -07003434disconnect:
Philipp Reisner813472c2011-05-03 16:47:02 +02003435 kfree(new_plan);
3436 if (new_disk_conf) {
3437 put_ldev(mdev);
3438 kfree(new_disk_conf);
3439 }
Philipp Reisnera0095502011-05-03 13:14:15 +02003440 mutex_unlock(&mdev->tconn->conf_update);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003441 /* just for completeness: actually not needed,
3442 * as this is not reached if csums_tfm was ok. */
3443 crypto_free_hash(csums_tfm);
3444 /* but free the verify_tfm again, if csums_tfm did not work out */
3445 crypto_free_hash(verify_tfm);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003446 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003447 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003448}
3449
Philipp Reisnerb411b362009-09-25 16:07:19 -07003450/* warn if the arguments differ by more than 12.5% */
3451static void warn_if_differ_considerably(struct drbd_conf *mdev,
3452 const char *s, sector_t a, sector_t b)
3453{
3454 sector_t d;
3455 if (a == 0 || b == 0)
3456 return;
3457 d = (a > b) ? (a - b) : (b - a);
3458 if (d > (a>>3) || d > (b>>3))
3459 dev_warn(DEV, "Considerable difference in %s: %llus vs. %llus\n", s,
3460 (unsigned long long)a, (unsigned long long)b);
3461}
3462
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003463static int receive_sizes(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003464{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003465 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003466 struct p_sizes *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003467 enum determine_dev_size dd = unchanged;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003468 sector_t p_size, p_usize, my_usize;
3469 int ldsc = 0; /* local disk size changed */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003470 enum dds_flags ddsf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003471
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003472 mdev = vnr_to_mdev(tconn, pi->vnr);
3473 if (!mdev)
3474 return config_unknown_volume(tconn, pi);
3475
Philipp Reisnerb411b362009-09-25 16:07:19 -07003476 p_size = be64_to_cpu(p->d_size);
3477 p_usize = be64_to_cpu(p->u_size);
3478
Philipp Reisnerb411b362009-09-25 16:07:19 -07003479 /* just store the peer's disk size for now.
3480 * we still need to figure out whether we accept that. */
3481 mdev->p_size = p_size;
3482
Philipp Reisnerb411b362009-09-25 16:07:19 -07003483 if (get_ldev(mdev)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003484 rcu_read_lock();
3485 my_usize = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
3486 rcu_read_unlock();
3487
Philipp Reisnerb411b362009-09-25 16:07:19 -07003488 warn_if_differ_considerably(mdev, "lower level device sizes",
3489 p_size, drbd_get_max_capacity(mdev->ldev));
3490 warn_if_differ_considerably(mdev, "user requested size",
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003491 p_usize, my_usize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003492
3493 /* if this is the first connect, or an otherwise expected
3494 * param exchange, choose the minimum */
3495 if (mdev->state.conn == C_WF_REPORT_PARAMS)
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003496 p_usize = min_not_zero(my_usize, p_usize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003497
3498 /* Never shrink a device with usable data during connect.
3499 But allow online shrinking if we are connected. */
Philipp Reisneref5e44a2011-05-03 13:27:43 +02003500 if (drbd_new_dev_size(mdev, mdev->ldev, p_usize, 0) <
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003501 drbd_get_capacity(mdev->this_bdev) &&
3502 mdev->state.disk >= D_OUTDATED &&
3503 mdev->state.conn < C_CONNECTED) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003504 dev_err(DEV, "The peer's disk size is too small!\n");
Philipp Reisner38fa9982011-03-15 18:24:49 +01003505 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003506 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003507 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003508 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003509
3510 if (my_usize != p_usize) {
3511 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
3512
3513 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
3514 if (!new_disk_conf) {
3515 dev_err(DEV, "Allocation of new disk_conf failed\n");
3516 put_ldev(mdev);
3517 return -ENOMEM;
3518 }
3519
3520 mutex_lock(&mdev->tconn->conf_update);
3521 old_disk_conf = mdev->ldev->disk_conf;
3522 *new_disk_conf = *old_disk_conf;
3523 new_disk_conf->disk_size = p_usize;
3524
3525 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
3526 mutex_unlock(&mdev->tconn->conf_update);
3527 synchronize_rcu();
3528 kfree(old_disk_conf);
3529
3530 dev_info(DEV, "Peer sets u_size to %lu sectors\n",
3531 (unsigned long)my_usize);
3532 }
3533
Philipp Reisnerb411b362009-09-25 16:07:19 -07003534 put_ldev(mdev);
3535 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003536
Philipp Reisnere89b5912010-03-24 17:11:33 +01003537 ddsf = be16_to_cpu(p->dds_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003538 if (get_ldev(mdev)) {
Bart Van Assche24c48302011-05-21 18:32:29 +02003539 dd = drbd_determine_dev_size(mdev, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003540 put_ldev(mdev);
3541 if (dd == dev_size_error)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003542 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003543 drbd_md_sync(mdev);
3544 } else {
3545 /* I am diskless, need to accept the peer's size. */
3546 drbd_set_my_capacity(mdev, p_size);
3547 }
3548
Philipp Reisner99432fc2011-05-20 16:39:13 +02003549 mdev->peer_max_bio_size = be32_to_cpu(p->max_bio_size);
3550 drbd_reconsider_max_bio_size(mdev);
3551
Philipp Reisnerb411b362009-09-25 16:07:19 -07003552 if (get_ldev(mdev)) {
3553 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev)) {
3554 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
3555 ldsc = 1;
3556 }
3557
Philipp Reisnerb411b362009-09-25 16:07:19 -07003558 put_ldev(mdev);
3559 }
3560
3561 if (mdev->state.conn > C_WF_REPORT_PARAMS) {
3562 if (be64_to_cpu(p->c_size) !=
3563 drbd_get_capacity(mdev->this_bdev) || ldsc) {
3564 /* we have different sizes, probably peer
3565 * needs to know my new size... */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003566 drbd_send_sizes(mdev, 0, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003567 }
3568 if (test_and_clear_bit(RESIZE_PENDING, &mdev->flags) ||
3569 (dd == grew && mdev->state.conn == C_CONNECTED)) {
3570 if (mdev->state.pdsk >= D_INCONSISTENT &&
Philipp Reisnere89b5912010-03-24 17:11:33 +01003571 mdev->state.disk >= D_INCONSISTENT) {
3572 if (ddsf & DDSF_NO_RESYNC)
3573 dev_info(DEV, "Resync of new storage suppressed with --assume-clean\n");
3574 else
3575 resync_after_online_grow(mdev);
3576 } else
Philipp Reisnerb411b362009-09-25 16:07:19 -07003577 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
3578 }
3579 }
3580
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003581 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003582}
3583
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003584static int receive_uuids(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003585{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003586 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003587 struct p_uuids *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003588 u64 *p_uuid;
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003589 int i, updated_uuids = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003590
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003591 mdev = vnr_to_mdev(tconn, pi->vnr);
3592 if (!mdev)
3593 return config_unknown_volume(tconn, pi);
3594
Philipp Reisnerb411b362009-09-25 16:07:19 -07003595 p_uuid = kmalloc(sizeof(u64)*UI_EXTENDED_SIZE, GFP_NOIO);
3596
3597 for (i = UI_CURRENT; i < UI_EXTENDED_SIZE; i++)
3598 p_uuid[i] = be64_to_cpu(p->uuid[i]);
3599
3600 kfree(mdev->p_uuid);
3601 mdev->p_uuid = p_uuid;
3602
3603 if (mdev->state.conn < C_CONNECTED &&
3604 mdev->state.disk < D_INCONSISTENT &&
3605 mdev->state.role == R_PRIMARY &&
3606 (mdev->ed_uuid & ~((u64)1)) != (p_uuid[UI_CURRENT] & ~((u64)1))) {
3607 dev_err(DEV, "Can only connect to data with current UUID=%016llX\n",
3608 (unsigned long long)mdev->ed_uuid);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003609 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003610 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003611 }
3612
3613 if (get_ldev(mdev)) {
3614 int skip_initial_sync =
3615 mdev->state.conn == C_CONNECTED &&
Philipp Reisner31890f42011-01-19 14:12:51 +01003616 mdev->tconn->agreed_pro_version >= 90 &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003617 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED &&
3618 (p_uuid[UI_FLAGS] & 8);
3619 if (skip_initial_sync) {
3620 dev_info(DEV, "Accepted new current UUID, preparing to skip initial sync\n");
3621 drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003622 "clear_n_write from receive_uuids",
3623 BM_LOCKED_TEST_ALLOWED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003624 _drbd_uuid_set(mdev, UI_CURRENT, p_uuid[UI_CURRENT]);
3625 _drbd_uuid_set(mdev, UI_BITMAP, 0);
3626 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
3627 CS_VERBOSE, NULL);
3628 drbd_md_sync(mdev);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003629 updated_uuids = 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003630 }
3631 put_ldev(mdev);
Philipp Reisner18a50fa2010-06-21 14:14:15 +02003632 } else if (mdev->state.disk < D_INCONSISTENT &&
3633 mdev->state.role == R_PRIMARY) {
3634 /* I am a diskless primary, the peer just created a new current UUID
3635 for me. */
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003636 updated_uuids = drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003637 }
3638
3639 /* Before we test for the disk state, we should wait until an eventually
3640 ongoing cluster wide state change is finished. That is important if
3641 we are primary and are detaching from our disk. We need to see the
3642 new disk state... */
Philipp Reisner8410da82011-02-11 20:11:10 +01003643 mutex_lock(mdev->state_mutex);
3644 mutex_unlock(mdev->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003645 if (mdev->state.conn >= C_CONNECTED && mdev->state.disk < D_INCONSISTENT)
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003646 updated_uuids |= drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
3647
3648 if (updated_uuids)
3649 drbd_print_uuids(mdev, "receiver updated UUIDs to");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003650
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003651 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003652}
3653
3654/**
3655 * convert_state() - Converts the peer's view of the cluster state to our point of view
3656 * @ps: The state as seen by the peer.
3657 */
3658static union drbd_state convert_state(union drbd_state ps)
3659{
3660 union drbd_state ms;
3661
3662 static enum drbd_conns c_tab[] = {
Philipp Reisner369bea62011-07-06 23:04:44 +02003663 [C_WF_REPORT_PARAMS] = C_WF_REPORT_PARAMS,
Philipp Reisnerb411b362009-09-25 16:07:19 -07003664 [C_CONNECTED] = C_CONNECTED,
3665
3666 [C_STARTING_SYNC_S] = C_STARTING_SYNC_T,
3667 [C_STARTING_SYNC_T] = C_STARTING_SYNC_S,
3668 [C_DISCONNECTING] = C_TEAR_DOWN, /* C_NETWORK_FAILURE, */
3669 [C_VERIFY_S] = C_VERIFY_T,
3670 [C_MASK] = C_MASK,
3671 };
3672
3673 ms.i = ps.i;
3674
3675 ms.conn = c_tab[ps.conn];
3676 ms.peer = ps.role;
3677 ms.role = ps.peer;
3678 ms.pdsk = ps.disk;
3679 ms.disk = ps.pdsk;
3680 ms.peer_isp = (ps.aftr_isp | ps.user_isp);
3681
3682 return ms;
3683}
3684
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003685static int receive_req_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003686{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003687 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003688 struct p_req_state *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003689 union drbd_state mask, val;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01003690 enum drbd_state_rv rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003691
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003692 mdev = vnr_to_mdev(tconn, pi->vnr);
3693 if (!mdev)
3694 return -EIO;
3695
Philipp Reisnerb411b362009-09-25 16:07:19 -07003696 mask.i = be32_to_cpu(p->mask);
3697 val.i = be32_to_cpu(p->val);
3698
Philipp Reisner25703f82011-02-07 14:35:25 +01003699 if (test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags) &&
Philipp Reisner8410da82011-02-11 20:11:10 +01003700 mutex_is_locked(mdev->state_mutex)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003701 drbd_send_sr_reply(mdev, SS_CONCURRENT_ST_CHG);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003702 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003703 }
3704
3705 mask = convert_state(mask);
3706 val = convert_state(val);
3707
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003708 rv = drbd_change_state(mdev, CS_VERBOSE, mask, val);
3709 drbd_send_sr_reply(mdev, rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003710
Philipp Reisnerb411b362009-09-25 16:07:19 -07003711 drbd_md_sync(mdev);
3712
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003713 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003714}
3715
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003716static int receive_req_conn_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003717{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003718 struct p_req_state *p = pi->data;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003719 union drbd_state mask, val;
3720 enum drbd_state_rv rv;
3721
3722 mask.i = be32_to_cpu(p->mask);
3723 val.i = be32_to_cpu(p->val);
3724
3725 if (test_bit(DISCARD_CONCURRENT, &tconn->flags) &&
3726 mutex_is_locked(&tconn->cstate_mutex)) {
3727 conn_send_sr_reply(tconn, SS_CONCURRENT_ST_CHG);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003728 return 0;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003729 }
3730
3731 mask = convert_state(mask);
3732 val = convert_state(val);
3733
Philipp Reisner778bcf22011-03-28 12:55:03 +02003734 rv = conn_request_state(tconn, mask, val, CS_VERBOSE | CS_LOCAL_ONLY | CS_IGN_OUTD_FAIL);
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003735 conn_send_sr_reply(tconn, rv);
3736
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003737 return 0;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003738}
3739
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003740static int receive_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003741{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003742 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003743 struct p_state *p = pi->data;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003744 union drbd_state os, ns, peer_state;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003745 enum drbd_disk_state real_peer_disk;
Philipp Reisner65d922c2010-06-16 16:18:09 +02003746 enum chg_state_flags cs_flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003747 int rv;
3748
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003749 mdev = vnr_to_mdev(tconn, pi->vnr);
3750 if (!mdev)
3751 return config_unknown_volume(tconn, pi);
3752
Philipp Reisnerb411b362009-09-25 16:07:19 -07003753 peer_state.i = be32_to_cpu(p->state);
3754
3755 real_peer_disk = peer_state.disk;
3756 if (peer_state.disk == D_NEGOTIATING) {
3757 real_peer_disk = mdev->p_uuid[UI_FLAGS] & 4 ? D_INCONSISTENT : D_CONSISTENT;
3758 dev_info(DEV, "real peer disk state = %s\n", drbd_disk_str(real_peer_disk));
3759 }
3760
Philipp Reisner87eeee42011-01-19 14:16:30 +01003761 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003762 retry:
Philipp Reisner78bae592011-03-28 15:40:12 +02003763 os = ns = drbd_read_state(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01003764 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003765
Philipp Reisner9bcd2522011-09-29 13:00:14 +02003766 /* If this is the "end of sync" confirmation, usually the peer disk
3767 * transitions from D_INCONSISTENT to D_UP_TO_DATE. For empty (0 bits
3768 * set) resync started in PausedSyncT, or if the timing of pause-/
3769 * unpause-sync events has been "just right", the peer disk may
3770 * transition from D_CONSISTENT to D_UP_TO_DATE as well.
3771 */
3772 if ((os.pdsk == D_INCONSISTENT || os.pdsk == D_CONSISTENT) &&
3773 real_peer_disk == D_UP_TO_DATE &&
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003774 os.conn > C_CONNECTED && os.disk == D_UP_TO_DATE) {
3775 /* If we are (becoming) SyncSource, but peer is still in sync
3776 * preparation, ignore its uptodate-ness to avoid flapping, it
3777 * will change to inconsistent once the peer reaches active
3778 * syncing states.
3779 * It may have changed syncer-paused flags, however, so we
3780 * cannot ignore this completely. */
3781 if (peer_state.conn > C_CONNECTED &&
3782 peer_state.conn < C_SYNC_SOURCE)
3783 real_peer_disk = D_INCONSISTENT;
3784
3785 /* if peer_state changes to connected at the same time,
3786 * it explicitly notifies us that it finished resync.
3787 * Maybe we should finish it up, too? */
3788 else if (os.conn >= C_SYNC_SOURCE &&
3789 peer_state.conn == C_CONNECTED) {
3790 if (drbd_bm_total_weight(mdev) <= mdev->rs_failed)
3791 drbd_resync_finished(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003792 return 0;
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003793 }
3794 }
3795
3796 /* peer says his disk is inconsistent, while we think it is uptodate,
3797 * and this happens while the peer still thinks we have a sync going on,
3798 * but we think we are already done with the sync.
3799 * We ignore this to avoid flapping pdsk.
3800 * This should not happen, if the peer is a recent version of drbd. */
3801 if (os.pdsk == D_UP_TO_DATE && real_peer_disk == D_INCONSISTENT &&
3802 os.conn == C_CONNECTED && peer_state.conn > C_SYNC_SOURCE)
3803 real_peer_disk = D_UP_TO_DATE;
3804
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003805 if (ns.conn == C_WF_REPORT_PARAMS)
3806 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003807
Philipp Reisner67531712010-10-27 12:21:30 +02003808 if (peer_state.conn == C_AHEAD)
3809 ns.conn = C_BEHIND;
3810
Philipp Reisnerb411b362009-09-25 16:07:19 -07003811 if (mdev->p_uuid && peer_state.disk >= D_NEGOTIATING &&
3812 get_ldev_if_state(mdev, D_NEGOTIATING)) {
3813 int cr; /* consider resync */
3814
3815 /* if we established a new connection */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003816 cr = (os.conn < C_CONNECTED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003817 /* if we had an established connection
3818 * and one of the nodes newly attaches a disk */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003819 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003820 (peer_state.disk == D_NEGOTIATING ||
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003821 os.disk == D_NEGOTIATING));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003822 /* if we have both been inconsistent, and the peer has been
3823 * forced to be UpToDate with --overwrite-data */
3824 cr |= test_bit(CONSIDER_RESYNC, &mdev->flags);
3825 /* if we had been plain connected, and the admin requested to
3826 * start a sync by "invalidate" or "invalidate-remote" */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003827 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003828 (peer_state.conn >= C_STARTING_SYNC_S &&
3829 peer_state.conn <= C_WF_BITMAP_T));
3830
3831 if (cr)
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003832 ns.conn = drbd_sync_handshake(mdev, peer_state.role, real_peer_disk);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003833
3834 put_ldev(mdev);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003835 if (ns.conn == C_MASK) {
3836 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003837 if (mdev->state.disk == D_NEGOTIATING) {
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02003838 drbd_force_state(mdev, NS(disk, D_FAILED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003839 } else if (peer_state.disk == D_NEGOTIATING) {
3840 dev_err(DEV, "Disk attach process on the peer node was aborted.\n");
3841 peer_state.disk = D_DISKLESS;
Lars Ellenberg580b9762010-02-26 23:15:23 +01003842 real_peer_disk = D_DISKLESS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003843 } else {
Philipp Reisner8169e412011-03-15 18:40:27 +01003844 if (test_and_clear_bit(CONN_DRY_RUN, &mdev->tconn->flags))
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003845 return -EIO;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003846 D_ASSERT(os.conn == C_WF_REPORT_PARAMS);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003847 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003848 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003849 }
3850 }
3851 }
3852
Philipp Reisner87eeee42011-01-19 14:16:30 +01003853 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner78bae592011-03-28 15:40:12 +02003854 if (os.i != drbd_read_state(mdev).i)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003855 goto retry;
3856 clear_bit(CONSIDER_RESYNC, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003857 ns.peer = peer_state.role;
3858 ns.pdsk = real_peer_disk;
3859 ns.peer_isp = (peer_state.aftr_isp | peer_state.user_isp);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003860 if ((ns.conn == C_CONNECTED || ns.conn == C_WF_BITMAP_S) && ns.disk == D_NEGOTIATING)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003861 ns.disk = mdev->new_state_tmp.disk;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003862 cs_flags = CS_VERBOSE + (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED ? 0 : CS_HARD);
Philipp Reisner2aebfab2011-03-28 16:48:11 +02003863 if (ns.pdsk == D_CONSISTENT && drbd_suspended(mdev) && ns.conn == C_CONNECTED && os.conn < C_CONNECTED &&
Philipp Reisner481c6f52010-06-22 14:03:27 +02003864 test_bit(NEW_CUR_UUID, &mdev->flags)) {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01003865 /* Do not allow tl_restart(RESEND) for a rebooted peer. We can only allow this
Philipp Reisner481c6f52010-06-22 14:03:27 +02003866 for temporal network outages! */
Philipp Reisner87eeee42011-01-19 14:16:30 +01003867 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003868 dev_err(DEV, "Aborting Connect, can not thaw IO with an only Consistent peer\n");
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01003869 tl_clear(mdev->tconn);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003870 drbd_uuid_new_current(mdev);
3871 clear_bit(NEW_CUR_UUID, &mdev->flags);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003872 conn_request_state(mdev->tconn, NS2(conn, C_PROTOCOL_ERROR, susp, 0), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003873 return -EIO;
Philipp Reisner481c6f52010-06-22 14:03:27 +02003874 }
Philipp Reisner65d922c2010-06-16 16:18:09 +02003875 rv = _drbd_set_state(mdev, ns, cs_flags, NULL);
Philipp Reisner78bae592011-03-28 15:40:12 +02003876 ns = drbd_read_state(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01003877 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003878
3879 if (rv < SS_SUCCESS) {
Philipp Reisner38fa9982011-03-15 18:24:49 +01003880 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003881 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003882 }
3883
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003884 if (os.conn > C_WF_REPORT_PARAMS) {
3885 if (ns.conn > C_CONNECTED && peer_state.conn <= C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003886 peer_state.disk != D_NEGOTIATING ) {
3887 /* we want resync, peer has not yet decided to sync... */
3888 /* Nowadays only used when forcing a node into primary role and
3889 setting its disk to UpToDate with that */
3890 drbd_send_uuids(mdev);
Philipp Reisner43de7c82011-11-10 13:16:13 +01003891 drbd_send_current_state(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003892 }
3893 }
3894
Philipp Reisner08b165b2011-09-05 16:22:33 +02003895 clear_bit(DISCARD_MY_DATA, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003896
3897 drbd_md_sync(mdev); /* update connected indicator, la_size, ... */
3898
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003899 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003900}
3901
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003902static int receive_sync_uuid(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003903{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003904 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003905 struct p_rs_uuid *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003906
3907 mdev = vnr_to_mdev(tconn, pi->vnr);
3908 if (!mdev)
3909 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003910
3911 wait_event(mdev->misc_wait,
3912 mdev->state.conn == C_WF_SYNC_UUID ||
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02003913 mdev->state.conn == C_BEHIND ||
Philipp Reisnerb411b362009-09-25 16:07:19 -07003914 mdev->state.conn < C_CONNECTED ||
3915 mdev->state.disk < D_NEGOTIATING);
3916
3917 /* D_ASSERT( mdev->state.conn == C_WF_SYNC_UUID ); */
3918
Philipp Reisnerb411b362009-09-25 16:07:19 -07003919 /* Here the _drbd_uuid_ functions are right, current should
3920 _not_ be rotated into the history */
3921 if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
3922 _drbd_uuid_set(mdev, UI_CURRENT, be64_to_cpu(p->uuid));
3923 _drbd_uuid_set(mdev, UI_BITMAP, 0UL);
3924
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003925 drbd_print_uuids(mdev, "updated sync uuid");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003926 drbd_start_resync(mdev, C_SYNC_TARGET);
3927
3928 put_ldev(mdev);
3929 } else
3930 dev_err(DEV, "Ignoring SyncUUID packet!\n");
3931
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003932 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003933}
3934
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003935/**
3936 * receive_bitmap_plain
3937 *
3938 * Return 0 when done, 1 when another iteration is needed, and a negative error
3939 * code upon failure.
3940 */
3941static int
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003942receive_bitmap_plain(struct drbd_conf *mdev, unsigned int size,
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003943 unsigned long *p, struct bm_xfer_ctx *c)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003944{
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003945 unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE -
3946 drbd_header_size(mdev->tconn);
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003947 unsigned int num_words = min_t(size_t, data_size / sizeof(*p),
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003948 c->bm_words - c->word_offset);
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003949 unsigned int want = num_words * sizeof(*p);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003950 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003951
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003952 if (want != size) {
3953 dev_err(DEV, "%s:want (%u) != size (%u)\n", __func__, want, size);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003954 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003955 }
3956 if (want == 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003957 return 0;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003958 err = drbd_recv_all(mdev->tconn, p, want);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003959 if (err)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003960 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003961
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003962 drbd_bm_merge_lel(mdev, c->word_offset, num_words, p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003963
3964 c->word_offset += num_words;
3965 c->bit_offset = c->word_offset * BITS_PER_LONG;
3966 if (c->bit_offset > c->bm_bits)
3967 c->bit_offset = c->bm_bits;
3968
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003969 return 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003970}
3971
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01003972static enum drbd_bitmap_code dcbp_get_code(struct p_compressed_bm *p)
3973{
3974 return (enum drbd_bitmap_code)(p->encoding & 0x0f);
3975}
3976
3977static int dcbp_get_start(struct p_compressed_bm *p)
3978{
3979 return (p->encoding & 0x80) != 0;
3980}
3981
3982static int dcbp_get_pad_bits(struct p_compressed_bm *p)
3983{
3984 return (p->encoding >> 4) & 0x7;
3985}
3986
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003987/**
3988 * recv_bm_rle_bits
3989 *
3990 * Return 0 when done, 1 when another iteration is needed, and a negative error
3991 * code upon failure.
3992 */
3993static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07003994recv_bm_rle_bits(struct drbd_conf *mdev,
3995 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01003996 struct bm_xfer_ctx *c,
3997 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003998{
3999 struct bitstream bs;
4000 u64 look_ahead;
4001 u64 rl;
4002 u64 tmp;
4003 unsigned long s = c->bit_offset;
4004 unsigned long e;
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004005 int toggle = dcbp_get_start(p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004006 int have;
4007 int bits;
4008
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004009 bitstream_init(&bs, p->code, len, dcbp_get_pad_bits(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07004010
4011 bits = bitstream_get_bits(&bs, &look_ahead, 64);
4012 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004013 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004014
4015 for (have = bits; have > 0; s += rl, toggle = !toggle) {
4016 bits = vli_decode_bits(&rl, look_ahead);
4017 if (bits <= 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004018 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004019
4020 if (toggle) {
4021 e = s + rl -1;
4022 if (e >= c->bm_bits) {
4023 dev_err(DEV, "bitmap overflow (e:%lu) while decoding bm RLE packet\n", e);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004024 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004025 }
4026 _drbd_bm_set_bits(mdev, s, e);
4027 }
4028
4029 if (have < bits) {
4030 dev_err(DEV, "bitmap decoding error: h:%d b:%d la:0x%08llx l:%u/%u\n",
4031 have, bits, look_ahead,
4032 (unsigned int)(bs.cur.b - p->code),
4033 (unsigned int)bs.buf_len);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004034 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004035 }
4036 look_ahead >>= bits;
4037 have -= bits;
4038
4039 bits = bitstream_get_bits(&bs, &tmp, 64 - have);
4040 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004041 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004042 look_ahead |= tmp << have;
4043 have += bits;
4044 }
4045
4046 c->bit_offset = s;
4047 bm_xfer_ctx_bit_to_word_offset(c);
4048
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004049 return (s != c->bm_bits);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004050}
4051
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004052/**
4053 * decode_bitmap_c
4054 *
4055 * Return 0 when done, 1 when another iteration is needed, and a negative error
4056 * code upon failure.
4057 */
4058static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07004059decode_bitmap_c(struct drbd_conf *mdev,
4060 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01004061 struct bm_xfer_ctx *c,
4062 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004063{
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004064 if (dcbp_get_code(p) == RLE_VLI_Bits)
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004065 return recv_bm_rle_bits(mdev, p, c, len - sizeof(*p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07004066
4067 /* other variants had been implemented for evaluation,
4068 * but have been dropped as this one turned out to be "best"
4069 * during all our tests. */
4070
4071 dev_err(DEV, "receive_bitmap_c: unknown encoding %u\n", p->encoding);
Philipp Reisner38fa9982011-03-15 18:24:49 +01004072 conn_request_state(mdev->tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004073 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004074}
4075
4076void INFO_bm_xfer_stats(struct drbd_conf *mdev,
4077 const char *direction, struct bm_xfer_ctx *c)
4078{
4079 /* what would it take to transfer it "plaintext" */
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004080 unsigned int header_size = drbd_header_size(mdev->tconn);
4081 unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE - header_size;
4082 unsigned int plain =
4083 header_size * (DIV_ROUND_UP(c->bm_words, data_size) + 1) +
4084 c->bm_words * sizeof(unsigned long);
4085 unsigned int total = c->bytes[0] + c->bytes[1];
4086 unsigned int r;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004087
4088 /* total can not be zero. but just in case: */
4089 if (total == 0)
4090 return;
4091
4092 /* don't report if not compressed */
4093 if (total >= plain)
4094 return;
4095
4096 /* total < plain. check for overflow, still */
4097 r = (total > UINT_MAX/1000) ? (total / (plain/1000))
4098 : (1000 * total / plain);
4099
4100 if (r > 1000)
4101 r = 1000;
4102
4103 r = 1000 - r;
4104 dev_info(DEV, "%s bitmap stats [Bytes(packets)]: plain %u(%u), RLE %u(%u), "
4105 "total %u; compression: %u.%u%%\n",
4106 direction,
4107 c->bytes[1], c->packets[1],
4108 c->bytes[0], c->packets[0],
4109 total, r/10, r % 10);
4110}
4111
4112/* Since we are processing the bitfield from lower addresses to higher,
4113 it does not matter if the process it in 32 bit chunks or 64 bit
4114 chunks as long as it is little endian. (Understand it as byte stream,
4115 beginning with the lowest byte...) If we would use big endian
4116 we would need to process it from the highest address to the lowest,
4117 in order to be agnostic to the 32 vs 64 bits issue.
4118
4119 returns 0 on failure, 1 if we successfully received it. */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004120static int receive_bitmap(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004121{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004122 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004123 struct bm_xfer_ctx c;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004124 int err;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004125
4126 mdev = vnr_to_mdev(tconn, pi->vnr);
4127 if (!mdev)
4128 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004129
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004130 drbd_bm_lock(mdev, "receive bitmap", BM_LOCKED_SET_ALLOWED);
4131 /* you are supposed to send additional out-of-sync information
4132 * if you actually set bits during this phase */
Philipp Reisnerb411b362009-09-25 16:07:19 -07004133
Philipp Reisnerb411b362009-09-25 16:07:19 -07004134 c = (struct bm_xfer_ctx) {
4135 .bm_bits = drbd_bm_bits(mdev),
4136 .bm_words = drbd_bm_words(mdev),
4137 };
4138
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004139 for(;;) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004140 if (pi->cmd == P_BITMAP)
4141 err = receive_bitmap_plain(mdev, pi->size, pi->data, &c);
4142 else if (pi->cmd == P_COMPRESSED_BITMAP) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004143 /* MAYBE: sanity check that we speak proto >= 90,
4144 * and the feature is enabled! */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004145 struct p_compressed_bm *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004146
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004147 if (pi->size > DRBD_SOCKET_BUFFER_SIZE - drbd_header_size(tconn)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004148 dev_err(DEV, "ReportCBitmap packet too large\n");
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004149 err = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004150 goto out;
4151 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004152 if (pi->size <= sizeof(*p)) {
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004153 dev_err(DEV, "ReportCBitmap packet too small (l:%u)\n", pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004154 err = -EIO;
Andreas Gruenbacher78fcbda2010-12-10 22:18:27 +01004155 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004156 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004157 err = drbd_recv_all(mdev->tconn, p, pi->size);
4158 if (err)
4159 goto out;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004160 err = decode_bitmap_c(mdev, p, &c, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004161 } else {
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004162 dev_warn(DEV, "receive_bitmap: cmd neither ReportBitMap nor ReportCBitMap (is 0x%x)", pi->cmd);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004163 err = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004164 goto out;
4165 }
4166
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004167 c.packets[pi->cmd == P_BITMAP]++;
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004168 c.bytes[pi->cmd == P_BITMAP] += drbd_header_size(tconn) + pi->size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004169
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004170 if (err <= 0) {
4171 if (err < 0)
4172 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004173 break;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004174 }
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004175 err = drbd_recv_header(mdev->tconn, pi);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004176 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004177 goto out;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004178 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004179
4180 INFO_bm_xfer_stats(mdev, "receive", &c);
4181
4182 if (mdev->state.conn == C_WF_BITMAP_T) {
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01004183 enum drbd_state_rv rv;
4184
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004185 err = drbd_send_bitmap(mdev);
4186 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004187 goto out;
4188 /* Omit CS_ORDERED with this state transition to avoid deadlocks. */
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01004189 rv = _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
4190 D_ASSERT(rv == SS_SUCCESS);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004191 } else if (mdev->state.conn != C_WF_BITMAP_S) {
4192 /* admin may have requested C_DISCONNECTING,
4193 * other threads may have noticed network errors */
4194 dev_info(DEV, "unexpected cstate (%s) in receive_bitmap\n",
4195 drbd_conn_str(mdev->state.conn));
4196 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004197 err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004198
Philipp Reisnerb411b362009-09-25 16:07:19 -07004199 out:
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004200 drbd_bm_unlock(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004201 if (!err && mdev->state.conn == C_WF_BITMAP_S)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004202 drbd_start_resync(mdev, C_SYNC_SOURCE);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004203 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004204}
4205
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004206static int receive_skip(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004207{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004208 conn_warn(tconn, "skipping unknown optional packet type %d, l: %d!\n",
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004209 pi->cmd, pi->size);
Philipp Reisner2de876e2011-03-15 14:38:01 +01004210
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004211 return ignore_remaining_packet(tconn, pi);
Philipp Reisner2de876e2011-03-15 14:38:01 +01004212}
4213
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004214static int receive_UnplugRemote(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004215{
Philipp Reisnerb411b362009-09-25 16:07:19 -07004216 /* Make sure we've acked all the TCP data associated
4217 * with the data requests being unplugged */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004218 drbd_tcp_quickack(tconn->data.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004219
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004220 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004221}
4222
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004223static int receive_out_of_sync(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner73a01a12010-10-27 14:33:00 +02004224{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004225 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004226 struct p_block_desc *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004227
4228 mdev = vnr_to_mdev(tconn, pi->vnr);
4229 if (!mdev)
4230 return -EIO;
Philipp Reisner73a01a12010-10-27 14:33:00 +02004231
Lars Ellenbergf735e3632010-12-17 21:06:18 +01004232 switch (mdev->state.conn) {
4233 case C_WF_SYNC_UUID:
4234 case C_WF_BITMAP_T:
4235 case C_BEHIND:
4236 break;
4237 default:
4238 dev_err(DEV, "ASSERT FAILED cstate = %s, expected: WFSyncUUID|WFBitMapT|Behind\n",
4239 drbd_conn_str(mdev->state.conn));
4240 }
4241
Philipp Reisner73a01a12010-10-27 14:33:00 +02004242 drbd_set_out_of_sync(mdev, be64_to_cpu(p->sector), be32_to_cpu(p->blksize));
4243
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004244 return 0;
Philipp Reisner73a01a12010-10-27 14:33:00 +02004245}
4246
Philipp Reisner02918be2010-08-20 14:35:10 +02004247struct data_cmd {
4248 int expect_payload;
4249 size_t pkt_size;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004250 int (*fn)(struct drbd_tconn *, struct packet_info *);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004251};
4252
Philipp Reisner02918be2010-08-20 14:35:10 +02004253static struct data_cmd drbd_cmd_handler[] = {
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004254 [P_DATA] = { 1, sizeof(struct p_data), receive_Data },
4255 [P_DATA_REPLY] = { 1, sizeof(struct p_data), receive_DataReply },
4256 [P_RS_DATA_REPLY] = { 1, sizeof(struct p_data), receive_RSDataReply } ,
4257 [P_BARRIER] = { 0, sizeof(struct p_barrier), receive_Barrier } ,
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004258 [P_BITMAP] = { 1, 0, receive_bitmap } ,
4259 [P_COMPRESSED_BITMAP] = { 1, 0, receive_bitmap } ,
4260 [P_UNPLUG_REMOTE] = { 0, 0, receive_UnplugRemote },
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004261 [P_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
4262 [P_RS_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004263 [P_SYNC_PARAM] = { 1, 0, receive_SyncParam },
4264 [P_SYNC_PARAM89] = { 1, 0, receive_SyncParam },
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004265 [P_PROTOCOL] = { 1, sizeof(struct p_protocol), receive_protocol },
4266 [P_UUIDS] = { 0, sizeof(struct p_uuids), receive_uuids },
4267 [P_SIZES] = { 0, sizeof(struct p_sizes), receive_sizes },
4268 [P_STATE] = { 0, sizeof(struct p_state), receive_state },
4269 [P_STATE_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_state },
4270 [P_SYNC_UUID] = { 0, sizeof(struct p_rs_uuid), receive_sync_uuid },
4271 [P_OV_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
4272 [P_OV_REPLY] = { 1, sizeof(struct p_block_req), receive_DataRequest },
4273 [P_CSUM_RS_REQUEST] = { 1, sizeof(struct p_block_req), receive_DataRequest },
4274 [P_DELAY_PROBE] = { 0, sizeof(struct p_delay_probe93), receive_skip },
4275 [P_OUT_OF_SYNC] = { 0, sizeof(struct p_block_desc), receive_out_of_sync },
4276 [P_CONN_ST_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_conn_state },
Philipp Reisner036b17e2011-05-16 17:38:11 +02004277 [P_PROTOCOL_UPDATE] = { 1, sizeof(struct p_protocol), receive_protocol },
Philipp Reisner02918be2010-08-20 14:35:10 +02004278};
4279
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004280static void drbdd(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004281{
Philipp Reisner77351055b2011-02-07 17:24:26 +01004282 struct packet_info pi;
Philipp Reisner02918be2010-08-20 14:35:10 +02004283 size_t shs; /* sub header size */
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004284 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004285
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004286 while (get_t_state(&tconn->receiver) == RUNNING) {
Andreas Gruenbacherdeebe192011-03-25 00:01:04 +01004287 struct data_cmd *cmd;
4288
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004289 drbd_thread_current_set_cpu(&tconn->receiver);
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004290 if (drbd_recv_header(tconn, &pi))
Philipp Reisner02918be2010-08-20 14:35:10 +02004291 goto err_out;
4292
Andreas Gruenbacherdeebe192011-03-25 00:01:04 +01004293 cmd = &drbd_cmd_handler[pi.cmd];
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004294 if (unlikely(pi.cmd >= ARRAY_SIZE(drbd_cmd_handler) || !cmd->fn)) {
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004295 conn_err(tconn, "Unexpected data packet %s (0x%04x)",
4296 cmdname(pi.cmd), pi.cmd);
Philipp Reisner02918be2010-08-20 14:35:10 +02004297 goto err_out;
Lars Ellenberg0b33a912009-11-16 15:58:04 +01004298 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004299
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004300 shs = cmd->pkt_size;
4301 if (pi.size > shs && !cmd->expect_payload) {
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004302 conn_err(tconn, "No payload expected %s l:%d\n",
4303 cmdname(pi.cmd), pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02004304 goto err_out;
4305 }
4306
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004307 if (shs) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004308 err = drbd_recv_all_warn(tconn, pi.data, shs);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004309 if (err)
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004310 goto err_out;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004311 pi.size -= shs;
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004312 }
4313
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004314 err = cmd->fn(tconn, &pi);
4315 if (err) {
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004316 conn_err(tconn, "error receiving %s, e: %d l: %d!\n",
4317 cmdname(pi.cmd), err, pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02004318 goto err_out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004319 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004320 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004321 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004322
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004323 err_out:
4324 conn_request_state(tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004325}
4326
Philipp Reisner0e29d162011-02-18 14:23:11 +01004327void conn_flush_workqueue(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004328{
4329 struct drbd_wq_barrier barr;
4330
4331 barr.w.cb = w_prev_work_done;
Philipp Reisner0e29d162011-02-18 14:23:11 +01004332 barr.w.tconn = tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004333 init_completion(&barr.done);
Philipp Reisner0e29d162011-02-18 14:23:11 +01004334 drbd_queue_work(&tconn->data.work, &barr.w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004335 wait_for_completion(&barr.done);
4336}
4337
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004338static void conn_disconnect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004339{
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004340 struct drbd_conf *mdev;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004341 enum drbd_conns oc;
Philipp Reisner376694a2011-11-07 10:54:28 +01004342 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004343
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004344 if (tconn->cstate == C_STANDALONE)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004345 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004346
4347 /* asender does not clean up anything. it must not interfere, either */
Philipp Reisner360cc742011-02-08 14:29:53 +01004348 drbd_thread_stop(&tconn->asender);
4349 drbd_free_sock(tconn);
4350
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004351 rcu_read_lock();
4352 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
4353 kref_get(&mdev->kref);
4354 rcu_read_unlock();
4355 drbd_disconnected(mdev);
4356 kref_put(&mdev->kref, &drbd_minor_destroy);
4357 rcu_read_lock();
4358 }
4359 rcu_read_unlock();
4360
Philipp Reisner360cc742011-02-08 14:29:53 +01004361 conn_info(tconn, "Connection closed\n");
4362
Philipp Reisnercb703452011-03-24 11:03:07 +01004363 if (conn_highest_role(tconn) == R_PRIMARY && conn_highest_pdsk(tconn) >= D_UNKNOWN)
4364 conn_try_outdate_peer_async(tconn);
4365
Philipp Reisner360cc742011-02-08 14:29:53 +01004366 spin_lock_irq(&tconn->req_lock);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004367 oc = tconn->cstate;
4368 if (oc >= C_UNCONNECTED)
Philipp Reisner376694a2011-11-07 10:54:28 +01004369 _conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004370
Philipp Reisner360cc742011-02-08 14:29:53 +01004371 spin_unlock_irq(&tconn->req_lock);
4372
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02004373 if (oc == C_DISCONNECTING)
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +02004374 conn_request_state(tconn, NS(conn, C_STANDALONE), CS_VERBOSE | CS_HARD);
Philipp Reisner360cc742011-02-08 14:29:53 +01004375}
4376
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004377static int drbd_disconnected(struct drbd_conf *mdev)
Philipp Reisner360cc742011-02-08 14:29:53 +01004378{
Philipp Reisner360cc742011-02-08 14:29:53 +01004379 unsigned int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004380
Philipp Reisner85719572010-07-21 10:20:17 +02004381 /* wait for current activity to cease. */
Philipp Reisner87eeee42011-01-19 14:16:30 +01004382 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004383 _drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
4384 _drbd_wait_ee_list_empty(mdev, &mdev->sync_ee);
4385 _drbd_wait_ee_list_empty(mdev, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004386 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004387
4388 /* We do not have data structures that would allow us to
4389 * get the rs_pending_cnt down to 0 again.
4390 * * On C_SYNC_TARGET we do not have any data structures describing
4391 * the pending RSDataRequest's we have sent.
4392 * * On C_SYNC_SOURCE there is no data structure that tracks
4393 * the P_RS_DATA_REPLY blocks that we sent to the SyncTarget.
4394 * And no, it is not the sum of the reference counts in the
4395 * resync_LRU. The resync_LRU tracks the whole operation including
4396 * the disk-IO, while the rs_pending_cnt only tracks the blocks
4397 * on the fly. */
4398 drbd_rs_cancel_all(mdev);
4399 mdev->rs_total = 0;
4400 mdev->rs_failed = 0;
4401 atomic_set(&mdev->rs_pending_cnt, 0);
4402 wake_up(&mdev->misc_wait);
4403
Philipp Reisnerb411b362009-09-25 16:07:19 -07004404 del_timer_sync(&mdev->resync_timer);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004405 resync_timer_fn((unsigned long)mdev);
4406
Philipp Reisnerb411b362009-09-25 16:07:19 -07004407 /* wait for all w_e_end_data_req, w_e_end_rsdata_req, w_send_barrier,
4408 * w_make_resync_request etc. which may still be on the worker queue
4409 * to be "canceled" */
Philipp Reisnera21e9292011-02-08 15:08:49 +01004410 drbd_flush_workqueue(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004411
Andreas Gruenbachera990be42011-04-06 17:56:48 +02004412 drbd_finish_peer_reqs(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004413
4414 kfree(mdev->p_uuid);
4415 mdev->p_uuid = NULL;
4416
Philipp Reisner2aebfab2011-03-28 16:48:11 +02004417 if (!drbd_suspended(mdev))
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01004418 tl_clear(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004419
Philipp Reisnerb411b362009-09-25 16:07:19 -07004420 drbd_md_sync(mdev);
4421
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004422 /* serialize with bitmap writeout triggered by the state change,
4423 * if any. */
4424 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
4425
Philipp Reisnerb411b362009-09-25 16:07:19 -07004426 /* tcp_close and release of sendpage pages can be deferred. I don't
4427 * want to use SO_LINGER, because apparently it can be deferred for
4428 * more than 20 seconds (longest time I checked).
4429 *
4430 * Actually we don't care for exactly when the network stack does its
4431 * put_page(), but release our reference on these pages right here.
4432 */
Andreas Gruenbacher7721f562011-04-06 17:14:02 +02004433 i = drbd_free_peer_reqs(mdev, &mdev->net_ee);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004434 if (i)
4435 dev_info(DEV, "net_ee not empty, killed %u entries\n", i);
Lars Ellenberg435f0742010-09-06 12:30:25 +02004436 i = atomic_read(&mdev->pp_in_use_by_net);
4437 if (i)
4438 dev_info(DEV, "pp_in_use_by_net = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004439 i = atomic_read(&mdev->pp_in_use);
4440 if (i)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02004441 dev_info(DEV, "pp_in_use = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004442
4443 D_ASSERT(list_empty(&mdev->read_ee));
4444 D_ASSERT(list_empty(&mdev->active_ee));
4445 D_ASSERT(list_empty(&mdev->sync_ee));
4446 D_ASSERT(list_empty(&mdev->done_ee));
4447
4448 /* ok, no more ee's on the fly, it is safe to reset the epoch_size */
4449 atomic_set(&mdev->current_epoch->epoch_size, 0);
4450 D_ASSERT(list_empty(&mdev->current_epoch->list));
Philipp Reisner360cc742011-02-08 14:29:53 +01004451
4452 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004453}
4454
4455/*
4456 * We support PRO_VERSION_MIN to PRO_VERSION_MAX. The protocol version
4457 * we can agree on is stored in agreed_pro_version.
4458 *
4459 * feature flags and the reserved array should be enough room for future
4460 * enhancements of the handshake protocol, and possible plugins...
4461 *
4462 * for now, they are expected to be zero, but ignored.
4463 */
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004464static int drbd_send_features(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004465{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004466 struct drbd_socket *sock;
4467 struct p_connection_features *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004468
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004469 sock = &tconn->data;
4470 p = conn_prepare_command(tconn, sock);
4471 if (!p)
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004472 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004473 memset(p, 0, sizeof(*p));
4474 p->protocol_min = cpu_to_be32(PRO_VERSION_MIN);
4475 p->protocol_max = cpu_to_be32(PRO_VERSION_MAX);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004476 return conn_send_command(tconn, sock, P_CONNECTION_FEATURES, sizeof(*p), NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004477}
4478
4479/*
4480 * return values:
4481 * 1 yes, we have a valid connection
4482 * 0 oops, did not work out, please try again
4483 * -1 peer talks different language,
4484 * no point in trying again, please go standalone.
4485 */
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004486static int drbd_do_features(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004487{
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004488 /* ASSERT current == tconn->receiver ... */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004489 struct p_connection_features *p;
4490 const int expect = sizeof(struct p_connection_features);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004491 struct packet_info pi;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004492 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004493
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004494 err = drbd_send_features(tconn);
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004495 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004496 return 0;
4497
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004498 err = drbd_recv_header(tconn, &pi);
4499 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004500 return 0;
4501
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004502 if (pi.cmd != P_CONNECTION_FEATURES) {
4503 conn_err(tconn, "expected ConnectionFeatures packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004504 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004505 return -1;
4506 }
4507
Philipp Reisner77351055b2011-02-07 17:24:26 +01004508 if (pi.size != expect) {
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004509 conn_err(tconn, "expected ConnectionFeatures length: %u, received: %u\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004510 expect, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004511 return -1;
4512 }
4513
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004514 p = pi.data;
4515 err = drbd_recv_all_warn(tconn, p, expect);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004516 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004517 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004518
Philipp Reisnerb411b362009-09-25 16:07:19 -07004519 p->protocol_min = be32_to_cpu(p->protocol_min);
4520 p->protocol_max = be32_to_cpu(p->protocol_max);
4521 if (p->protocol_max == 0)
4522 p->protocol_max = p->protocol_min;
4523
4524 if (PRO_VERSION_MAX < p->protocol_min ||
4525 PRO_VERSION_MIN > p->protocol_max)
4526 goto incompat;
4527
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004528 tconn->agreed_pro_version = min_t(int, PRO_VERSION_MAX, p->protocol_max);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004529
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004530 conn_info(tconn, "Handshake successful: "
4531 "Agreed network protocol version %d\n", tconn->agreed_pro_version);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004532
4533 return 1;
4534
4535 incompat:
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004536 conn_err(tconn, "incompatible DRBD dialects: "
Philipp Reisnerb411b362009-09-25 16:07:19 -07004537 "I support %d-%d, peer supports %d-%d\n",
4538 PRO_VERSION_MIN, PRO_VERSION_MAX,
4539 p->protocol_min, p->protocol_max);
4540 return -1;
4541}
4542
4543#if !defined(CONFIG_CRYPTO_HMAC) && !defined(CONFIG_CRYPTO_HMAC_MODULE)
Philipp Reisner13e60372011-02-08 09:54:40 +01004544static int drbd_do_auth(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004545{
4546 dev_err(DEV, "This kernel was build without CONFIG_CRYPTO_HMAC.\n");
4547 dev_err(DEV, "You need to disable 'cram-hmac-alg' in drbd.conf.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004548 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004549}
4550#else
4551#define CHALLENGE_LEN 64
Johannes Thomab10d96c2010-01-07 16:02:50 +01004552
4553/* Return value:
4554 1 - auth succeeded,
4555 0 - failed, try again (network error),
4556 -1 - auth failed, don't try again.
4557*/
4558
Philipp Reisner13e60372011-02-08 09:54:40 +01004559static int drbd_do_auth(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004560{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004561 struct drbd_socket *sock;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004562 char my_challenge[CHALLENGE_LEN]; /* 64 Bytes... */
4563 struct scatterlist sg;
4564 char *response = NULL;
4565 char *right_response = NULL;
4566 char *peers_ch = NULL;
Philipp Reisner44ed1672011-04-19 17:10:19 +02004567 unsigned int key_len;
4568 char secret[SHARED_SECRET_MAX]; /* 64 byte */
Philipp Reisnerb411b362009-09-25 16:07:19 -07004569 unsigned int resp_size;
4570 struct hash_desc desc;
Philipp Reisner77351055b2011-02-07 17:24:26 +01004571 struct packet_info pi;
Philipp Reisner44ed1672011-04-19 17:10:19 +02004572 struct net_conf *nc;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004573 int err, rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004574
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004575 /* FIXME: Put the challenge/response into the preallocated socket buffer. */
4576
Philipp Reisner44ed1672011-04-19 17:10:19 +02004577 rcu_read_lock();
4578 nc = rcu_dereference(tconn->net_conf);
4579 key_len = strlen(nc->shared_secret);
4580 memcpy(secret, nc->shared_secret, key_len);
4581 rcu_read_unlock();
4582
Philipp Reisner13e60372011-02-08 09:54:40 +01004583 desc.tfm = tconn->cram_hmac_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004584 desc.flags = 0;
4585
Philipp Reisner44ed1672011-04-19 17:10:19 +02004586 rv = crypto_hash_setkey(tconn->cram_hmac_tfm, (u8 *)secret, key_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004587 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004588 conn_err(tconn, "crypto_hash_setkey() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004589 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004590 goto fail;
4591 }
4592
4593 get_random_bytes(my_challenge, CHALLENGE_LEN);
4594
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004595 sock = &tconn->data;
4596 if (!conn_prepare_command(tconn, sock)) {
4597 rv = 0;
4598 goto fail;
4599 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004600 rv = !conn_send_command(tconn, sock, P_AUTH_CHALLENGE, 0,
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004601 my_challenge, CHALLENGE_LEN);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004602 if (!rv)
4603 goto fail;
4604
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004605 err = drbd_recv_header(tconn, &pi);
4606 if (err) {
4607 rv = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004608 goto fail;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004609 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004610
Philipp Reisner77351055b2011-02-07 17:24:26 +01004611 if (pi.cmd != P_AUTH_CHALLENGE) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004612 conn_err(tconn, "expected AuthChallenge packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004613 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004614 rv = 0;
4615 goto fail;
4616 }
4617
Philipp Reisner77351055b2011-02-07 17:24:26 +01004618 if (pi.size > CHALLENGE_LEN * 2) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004619 conn_err(tconn, "expected AuthChallenge payload too big.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004620 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004621 goto fail;
4622 }
4623
Philipp Reisner77351055b2011-02-07 17:24:26 +01004624 peers_ch = kmalloc(pi.size, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004625 if (peers_ch == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004626 conn_err(tconn, "kmalloc of peers_ch failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004627 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004628 goto fail;
4629 }
4630
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004631 err = drbd_recv_all_warn(tconn, peers_ch, pi.size);
4632 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004633 rv = 0;
4634 goto fail;
4635 }
4636
Philipp Reisner13e60372011-02-08 09:54:40 +01004637 resp_size = crypto_hash_digestsize(tconn->cram_hmac_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004638 response = kmalloc(resp_size, GFP_NOIO);
4639 if (response == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004640 conn_err(tconn, "kmalloc of response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004641 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004642 goto fail;
4643 }
4644
4645 sg_init_table(&sg, 1);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004646 sg_set_buf(&sg, peers_ch, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004647
4648 rv = crypto_hash_digest(&desc, &sg, sg.length, response);
4649 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004650 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004651 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004652 goto fail;
4653 }
4654
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004655 if (!conn_prepare_command(tconn, sock)) {
4656 rv = 0;
4657 goto fail;
4658 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004659 rv = !conn_send_command(tconn, sock, P_AUTH_RESPONSE, 0,
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004660 response, resp_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004661 if (!rv)
4662 goto fail;
4663
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004664 err = drbd_recv_header(tconn, &pi);
4665 if (err) {
4666 rv = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004667 goto fail;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004668 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004669
Philipp Reisner77351055b2011-02-07 17:24:26 +01004670 if (pi.cmd != P_AUTH_RESPONSE) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004671 conn_err(tconn, "expected AuthResponse packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004672 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004673 rv = 0;
4674 goto fail;
4675 }
4676
Philipp Reisner77351055b2011-02-07 17:24:26 +01004677 if (pi.size != resp_size) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004678 conn_err(tconn, "expected AuthResponse payload of wrong size\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004679 rv = 0;
4680 goto fail;
4681 }
4682
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004683 err = drbd_recv_all_warn(tconn, response , resp_size);
4684 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004685 rv = 0;
4686 goto fail;
4687 }
4688
4689 right_response = kmalloc(resp_size, GFP_NOIO);
Julia Lawall2d1ee872009-12-27 22:27:11 +01004690 if (right_response == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004691 conn_err(tconn, "kmalloc of right_response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004692 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004693 goto fail;
4694 }
4695
4696 sg_set_buf(&sg, my_challenge, CHALLENGE_LEN);
4697
4698 rv = crypto_hash_digest(&desc, &sg, sg.length, right_response);
4699 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004700 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004701 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004702 goto fail;
4703 }
4704
4705 rv = !memcmp(response, right_response, resp_size);
4706
4707 if (rv)
Philipp Reisner44ed1672011-04-19 17:10:19 +02004708 conn_info(tconn, "Peer authenticated using %d bytes HMAC\n",
4709 resp_size);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004710 else
4711 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004712
4713 fail:
4714 kfree(peers_ch);
4715 kfree(response);
4716 kfree(right_response);
4717
4718 return rv;
4719}
4720#endif
4721
4722int drbdd_init(struct drbd_thread *thi)
4723{
Philipp Reisner392c8802011-02-09 10:33:31 +01004724 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004725 int h;
4726
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004727 conn_info(tconn, "receiver (re)started\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004728
4729 do {
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004730 h = conn_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004731 if (h == 0) {
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004732 conn_disconnect(tconn);
Philipp Reisner20ee6392011-01-18 15:28:59 +01004733 schedule_timeout_interruptible(HZ);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004734 }
4735 if (h == -1) {
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004736 conn_warn(tconn, "Discarding network configuration.\n");
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004737 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004738 }
4739 } while (h == 0);
4740
Philipp Reisner91fd4da2011-04-20 17:47:29 +02004741 if (h > 0)
4742 drbdd(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004743
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004744 conn_disconnect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004745
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004746 conn_info(tconn, "receiver terminated\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004747 return 0;
4748}
4749
4750/* ********* acknowledge sender ******** */
4751
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004752static int got_conn_RqSReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004753{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004754 struct p_req_state_reply *p = pi->data;
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004755 int retcode = be32_to_cpu(p->retcode);
4756
4757 if (retcode >= SS_SUCCESS) {
4758 set_bit(CONN_WD_ST_CHG_OKAY, &tconn->flags);
4759 } else {
4760 set_bit(CONN_WD_ST_CHG_FAIL, &tconn->flags);
4761 conn_err(tconn, "Requested state change failed by peer: %s (%d)\n",
4762 drbd_set_st_err_str(retcode), retcode);
4763 }
4764 wake_up(&tconn->ping_wait);
4765
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004766 return 0;
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004767}
4768
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004769static int got_RqSReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004770{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004771 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004772 struct p_req_state_reply *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004773 int retcode = be32_to_cpu(p->retcode);
4774
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004775 mdev = vnr_to_mdev(tconn, pi->vnr);
4776 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004777 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004778
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004779 if (retcode >= SS_SUCCESS) {
4780 set_bit(CL_ST_CHG_SUCCESS, &mdev->flags);
4781 } else {
4782 set_bit(CL_ST_CHG_FAIL, &mdev->flags);
4783 dev_err(DEV, "Requested state change failed by peer: %s (%d)\n",
4784 drbd_set_st_err_str(retcode), retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004785 }
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004786 wake_up(&mdev->state_wait);
4787
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004788 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004789}
4790
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004791static int got_Ping(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004792{
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004793 return drbd_send_ping_ack(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004794
4795}
4796
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004797static int got_PingAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004798{
4799 /* restore idle timeout */
Philipp Reisner2a67d8b2011-02-09 14:10:32 +01004800 tconn->meta.socket->sk->sk_rcvtimeo = tconn->net_conf->ping_int*HZ;
4801 if (!test_and_set_bit(GOT_PING_ACK, &tconn->flags))
4802 wake_up(&tconn->ping_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004803
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004804 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004805}
4806
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004807static int got_IsInSync(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004808{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004809 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004810 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004811 sector_t sector = be64_to_cpu(p->sector);
4812 int blksize = be32_to_cpu(p->blksize);
4813
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004814 mdev = vnr_to_mdev(tconn, pi->vnr);
4815 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004816 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004817
Philipp Reisner31890f42011-01-19 14:12:51 +01004818 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004819
4820 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4821
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004822 if (get_ldev(mdev)) {
4823 drbd_rs_complete_io(mdev, sector);
4824 drbd_set_in_sync(mdev, sector, blksize);
4825 /* rs_same_csums is supposed to count in units of BM_BLOCK_SIZE */
4826 mdev->rs_same_csum += (blksize >> BM_BLOCK_SHIFT);
4827 put_ldev(mdev);
4828 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004829 dec_rs_pending(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02004830 atomic_add(blksize >> 9, &mdev->rs_sect_in);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004831
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004832 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004833}
4834
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004835static int
4836validate_req_change_req_state(struct drbd_conf *mdev, u64 id, sector_t sector,
4837 struct rb_root *root, const char *func,
4838 enum drbd_req_event what, bool missing_ok)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004839{
4840 struct drbd_request *req;
4841 struct bio_and_error m;
4842
Philipp Reisner87eeee42011-01-19 14:16:30 +01004843 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004844 req = find_request(mdev, root, id, sector, missing_ok, func);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004845 if (unlikely(!req)) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01004846 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004847 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004848 }
4849 __req_mod(req, what, &m);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004850 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004851
4852 if (m.bio)
4853 complete_master_bio(mdev, &m);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004854 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004855}
4856
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004857static int got_BlockAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004858{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004859 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004860 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004861 sector_t sector = be64_to_cpu(p->sector);
4862 int blksize = be32_to_cpu(p->blksize);
4863 enum drbd_req_event what;
4864
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004865 mdev = vnr_to_mdev(tconn, pi->vnr);
4866 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004867 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004868
Philipp Reisnerb411b362009-09-25 16:07:19 -07004869 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4870
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01004871 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004872 drbd_set_in_sync(mdev, sector, blksize);
4873 dec_rs_pending(mdev);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004874 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004875 }
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004876 switch (pi->cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004877 case P_RS_WRITE_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004878 what = WRITE_ACKED_BY_PEER_AND_SIS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004879 break;
4880 case P_WRITE_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004881 what = WRITE_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004882 break;
4883 case P_RECV_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004884 what = RECV_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004885 break;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004886 case P_DISCARD_WRITE:
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004887 what = DISCARD_WRITE;
4888 break;
4889 case P_RETRY_WRITE:
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004890 what = POSTPONE_WRITE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004891 break;
4892 default:
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004893 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -07004894 }
4895
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004896 return validate_req_change_req_state(mdev, p->block_id, sector,
4897 &mdev->write_requests, __func__,
4898 what, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004899}
4900
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004901static int got_NegAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004902{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004903 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004904 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004905 sector_t sector = be64_to_cpu(p->sector);
Philipp Reisner2deb8332011-01-17 18:39:18 +01004906 int size = be32_to_cpu(p->blksize);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004907 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004908
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004909 mdev = vnr_to_mdev(tconn, pi->vnr);
4910 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004911 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004912
Philipp Reisnerb411b362009-09-25 16:07:19 -07004913 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4914
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01004915 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004916 dec_rs_pending(mdev);
4917 drbd_rs_failed_io(mdev, sector, size);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004918 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004919 }
Philipp Reisner2deb8332011-01-17 18:39:18 +01004920
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004921 err = validate_req_change_req_state(mdev, p->block_id, sector,
4922 &mdev->write_requests, __func__,
Philipp Reisner303d1442011-04-13 16:24:47 -07004923 NEG_ACKED, true);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004924 if (err) {
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004925 /* Protocol A has no P_WRITE_ACKs, but has P_NEG_ACKs.
4926 The master bio might already be completed, therefore the
4927 request is no longer in the collision hash. */
4928 /* In Protocol B we might already have got a P_RECV_ACK
4929 but then get a P_NEG_ACK afterwards. */
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004930 drbd_set_out_of_sync(mdev, sector, size);
Philipp Reisner2deb8332011-01-17 18:39:18 +01004931 }
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004932 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004933}
4934
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004935static int got_NegDReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004936{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004937 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004938 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004939 sector_t sector = be64_to_cpu(p->sector);
4940
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004941 mdev = vnr_to_mdev(tconn, pi->vnr);
4942 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004943 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004944
Philipp Reisnerb411b362009-09-25 16:07:19 -07004945 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004946
Philipp Reisnerb411b362009-09-25 16:07:19 -07004947 dev_err(DEV, "Got NegDReply; Sector %llus, len %u; Fail original request.\n",
4948 (unsigned long long)sector, be32_to_cpu(p->blksize));
4949
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004950 return validate_req_change_req_state(mdev, p->block_id, sector,
4951 &mdev->read_requests, __func__,
4952 NEG_ACKED, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004953}
4954
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004955static int got_NegRSDReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004956{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004957 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004958 sector_t sector;
4959 int size;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004960 struct p_block_ack *p = pi->data;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004961
4962 mdev = vnr_to_mdev(tconn, pi->vnr);
4963 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004964 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004965
4966 sector = be64_to_cpu(p->sector);
4967 size = be32_to_cpu(p->blksize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004968
4969 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4970
4971 dec_rs_pending(mdev);
4972
4973 if (get_ldev_if_state(mdev, D_FAILED)) {
4974 drbd_rs_complete_io(mdev, sector);
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004975 switch (pi->cmd) {
Philipp Reisnerd612d302010-12-27 10:53:28 +01004976 case P_NEG_RS_DREPLY:
4977 drbd_rs_failed_io(mdev, sector, size);
4978 case P_RS_CANCEL:
4979 break;
4980 default:
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004981 BUG();
Philipp Reisnerd612d302010-12-27 10:53:28 +01004982 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004983 put_ldev(mdev);
4984 }
4985
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004986 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004987}
4988
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004989static int got_BarrierAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004990{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004991 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004992 struct p_barrier_ack *p = pi->data;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004993
4994 mdev = vnr_to_mdev(tconn, pi->vnr);
4995 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004996 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004997
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01004998 tl_release(mdev->tconn, p->barrier, be32_to_cpu(p->set_size));
Philipp Reisnerb411b362009-09-25 16:07:19 -07004999
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02005000 if (mdev->state.conn == C_AHEAD &&
5001 atomic_read(&mdev->ap_in_flight) == 0 &&
Philipp Reisner36baf612011-11-10 14:27:34 +01005002 !test_and_set_bit(AHEAD_TO_SYNC_SOURCE, &mdev->flags)) {
Philipp Reisner370a43e2011-01-14 16:03:11 +01005003 mdev->start_resync_timer.expires = jiffies + HZ;
5004 add_timer(&mdev->start_resync_timer);
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02005005 }
5006
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005007 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005008}
5009
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005010static int got_OVResult(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005011{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005012 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005013 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005014 struct drbd_work *w;
5015 sector_t sector;
5016 int size;
5017
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005018 mdev = vnr_to_mdev(tconn, pi->vnr);
5019 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005020 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005021
Philipp Reisnerb411b362009-09-25 16:07:19 -07005022 sector = be64_to_cpu(p->sector);
5023 size = be32_to_cpu(p->blksize);
5024
5025 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
5026
5027 if (be64_to_cpu(p->block_id) == ID_OUT_OF_SYNC)
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005028 drbd_ov_out_of_sync_found(mdev, sector, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005029 else
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005030 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005031
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005032 if (!get_ldev(mdev))
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005033 return 0;
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005034
Philipp Reisnerb411b362009-09-25 16:07:19 -07005035 drbd_rs_complete_io(mdev, sector);
5036 dec_rs_pending(mdev);
5037
Lars Ellenbergea5442a2010-11-05 09:48:01 +01005038 --mdev->ov_left;
5039
5040 /* let's advance progress step marks only for every other megabyte */
5041 if ((mdev->ov_left & 0x200) == 0x200)
5042 drbd_advance_rs_marks(mdev, mdev->ov_left);
5043
5044 if (mdev->ov_left == 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07005045 w = kmalloc(sizeof(*w), GFP_NOIO);
5046 if (w) {
5047 w->cb = w_ov_finished;
Philipp Reisnera21e9292011-02-08 15:08:49 +01005048 w->mdev = mdev;
Philipp Reisnere42325a2011-01-19 13:55:45 +01005049 drbd_queue_work_front(&mdev->tconn->data.work, w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005050 } else {
5051 dev_err(DEV, "kmalloc(w) failed.");
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005052 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005053 drbd_resync_finished(mdev);
5054 }
5055 }
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005056 put_ldev(mdev);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005057 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005058}
5059
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005060static int got_skip(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner0ced55a2010-04-30 15:26:20 +02005061{
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005062 return 0;
Philipp Reisner0ced55a2010-04-30 15:26:20 +02005063}
5064
Andreas Gruenbachera990be42011-04-06 17:56:48 +02005065static int tconn_finish_peer_reqs(struct drbd_tconn *tconn)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005066{
Philipp Reisner082a3432011-03-15 16:05:42 +01005067 struct drbd_conf *mdev;
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005068 int vnr, not_empty = 0;
Philipp Reisner32862ec2011-02-08 16:41:01 +01005069
5070 do {
5071 clear_bit(SIGNAL_ASENDER, &tconn->flags);
5072 flush_signals(current);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005073
5074 rcu_read_lock();
5075 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
5076 kref_get(&mdev->kref);
5077 rcu_read_unlock();
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07005078 if (drbd_finish_peer_reqs(mdev)) {
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005079 kref_put(&mdev->kref, &drbd_minor_destroy);
5080 return 1;
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07005081 }
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005082 kref_put(&mdev->kref, &drbd_minor_destroy);
5083 rcu_read_lock();
Philipp Reisner082a3432011-03-15 16:05:42 +01005084 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005085 set_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisner082a3432011-03-15 16:05:42 +01005086
5087 spin_lock_irq(&tconn->req_lock);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005088 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Philipp Reisner082a3432011-03-15 16:05:42 +01005089 not_empty = !list_empty(&mdev->done_ee);
5090 if (not_empty)
5091 break;
5092 }
5093 spin_unlock_irq(&tconn->req_lock);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005094 rcu_read_unlock();
Philipp Reisner32862ec2011-02-08 16:41:01 +01005095 } while (not_empty);
5096
5097 return 0;
5098}
5099
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005100struct asender_cmd {
5101 size_t pkt_size;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005102 int (*fn)(struct drbd_tconn *tconn, struct packet_info *);
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005103};
5104
5105static struct asender_cmd asender_tbl[] = {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005106 [P_PING] = { 0, got_Ping },
5107 [P_PING_ACK] = { 0, got_PingAck },
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005108 [P_RECV_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5109 [P_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5110 [P_RS_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5111 [P_DISCARD_WRITE] = { sizeof(struct p_block_ack), got_BlockAck },
5112 [P_NEG_ACK] = { sizeof(struct p_block_ack), got_NegAck },
5113 [P_NEG_DREPLY] = { sizeof(struct p_block_ack), got_NegDReply },
5114 [P_NEG_RS_DREPLY] = { sizeof(struct p_block_ack), got_NegRSDReply },
5115 [P_OV_RESULT] = { sizeof(struct p_block_ack), got_OVResult },
5116 [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), got_BarrierAck },
5117 [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), got_RqSReply },
5118 [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), got_IsInSync },
5119 [P_DELAY_PROBE] = { sizeof(struct p_delay_probe93), got_skip },
5120 [P_RS_CANCEL] = { sizeof(struct p_block_ack), got_NegRSDReply },
5121 [P_CONN_ST_CHG_REPLY]={ sizeof(struct p_req_state_reply), got_conn_RqSReply },
5122 [P_RETRY_WRITE] = { sizeof(struct p_block_ack), got_BlockAck },
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005123};
5124
Philipp Reisnerb411b362009-09-25 16:07:19 -07005125int drbd_asender(struct drbd_thread *thi)
5126{
Philipp Reisner392c8802011-02-09 10:33:31 +01005127 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005128 struct asender_cmd *cmd = NULL;
Philipp Reisner77351055b2011-02-07 17:24:26 +01005129 struct packet_info pi;
Philipp Reisner257d0af2011-01-26 12:15:29 +01005130 int rv;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005131 void *buf = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005132 int received = 0;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005133 unsigned int header_size = drbd_header_size(tconn);
5134 int expect = header_size;
Philipp Reisner44ed1672011-04-19 17:10:19 +02005135 bool ping_timeout_active = false;
5136 struct net_conf *nc;
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005137 int ping_timeo, tcp_cork, ping_int;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005138
Philipp Reisnerb411b362009-09-25 16:07:19 -07005139 current->policy = SCHED_RR; /* Make this a realtime task! */
5140 current->rt_priority = 2; /* more important than all other tasks */
5141
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +01005142 while (get_t_state(thi) == RUNNING) {
Philipp Reisner80822282011-02-08 12:46:30 +01005143 drbd_thread_current_set_cpu(thi);
Philipp Reisner44ed1672011-04-19 17:10:19 +02005144
5145 rcu_read_lock();
5146 nc = rcu_dereference(tconn->net_conf);
5147 ping_timeo = nc->ping_timeo;
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005148 tcp_cork = nc->tcp_cork;
Philipp Reisner44ed1672011-04-19 17:10:19 +02005149 ping_int = nc->ping_int;
5150 rcu_read_unlock();
5151
Philipp Reisner32862ec2011-02-08 16:41:01 +01005152 if (test_and_clear_bit(SEND_PING, &tconn->flags)) {
Andreas Gruenbachera17647a2011-04-01 12:49:42 +02005153 if (drbd_send_ping(tconn)) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005154 conn_err(tconn, "drbd_send_ping has failed\n");
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01005155 goto reconnect;
5156 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02005157 tconn->meta.socket->sk->sk_rcvtimeo = ping_timeo * HZ / 10;
5158 ping_timeout_active = true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005159 }
5160
Philipp Reisner32862ec2011-02-08 16:41:01 +01005161 /* TODO: conditionally cork; it may hurt latency if we cork without
5162 much to send */
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005163 if (tcp_cork)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005164 drbd_tcp_cork(tconn->meta.socket);
Andreas Gruenbachera990be42011-04-06 17:56:48 +02005165 if (tconn_finish_peer_reqs(tconn)) {
5166 conn_err(tconn, "tconn_finish_peer_reqs() failed\n");
Philipp Reisner32862ec2011-02-08 16:41:01 +01005167 goto reconnect;
Philipp Reisner082a3432011-03-15 16:05:42 +01005168 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005169 /* but unconditionally uncork unless disabled */
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005170 if (tcp_cork)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005171 drbd_tcp_uncork(tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005172
5173 /* short circuit, recv_msg would return EINTR anyways. */
5174 if (signal_pending(current))
5175 continue;
5176
Philipp Reisner32862ec2011-02-08 16:41:01 +01005177 rv = drbd_recv_short(tconn->meta.socket, buf, expect-received, 0);
5178 clear_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005179
5180 flush_signals(current);
5181
5182 /* Note:
5183 * -EINTR (on meta) we got a signal
5184 * -EAGAIN (on meta) rcvtimeo expired
5185 * -ECONNRESET other side closed the connection
5186 * -ERESTARTSYS (on data) we got a signal
5187 * rv < 0 other than above: unexpected error!
5188 * rv == expected: full header or command
5189 * rv < expected: "woken" by signal during receive
5190 * rv == 0 : "connection shut down by peer"
5191 */
5192 if (likely(rv > 0)) {
5193 received += rv;
5194 buf += rv;
5195 } else if (rv == 0) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005196 conn_err(tconn, "meta connection shut down by peer.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005197 goto reconnect;
5198 } else if (rv == -EAGAIN) {
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02005199 /* If the data socket received something meanwhile,
5200 * that is good enough: peer is still alive. */
Philipp Reisner32862ec2011-02-08 16:41:01 +01005201 if (time_after(tconn->last_received,
5202 jiffies - tconn->meta.socket->sk->sk_rcvtimeo))
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02005203 continue;
Lars Ellenbergf36af182011-03-09 22:44:55 +01005204 if (ping_timeout_active) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005205 conn_err(tconn, "PingAck did not arrive in time.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005206 goto reconnect;
5207 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005208 set_bit(SEND_PING, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005209 continue;
5210 } else if (rv == -EINTR) {
5211 continue;
5212 } else {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005213 conn_err(tconn, "sock_recvmsg returned %d\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005214 goto reconnect;
5215 }
5216
5217 if (received == expect && cmd == NULL) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005218 if (decode_header(tconn, tconn->meta.rbuf, &pi))
Philipp Reisnerb411b362009-09-25 16:07:19 -07005219 goto reconnect;
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005220 cmd = &asender_tbl[pi.cmd];
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005221 if (pi.cmd >= ARRAY_SIZE(asender_tbl) || !cmd->fn) {
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02005222 conn_err(tconn, "Unexpected meta packet %s (0x%04x)\n",
5223 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005224 goto disconnect;
5225 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005226 expect = header_size + cmd->pkt_size;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005227 if (pi.size != expect - header_size) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005228 conn_err(tconn, "Wrong packet size on meta (c: %d, l: %d)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01005229 pi.cmd, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005230 goto reconnect;
Philipp Reisner257d0af2011-01-26 12:15:29 +01005231 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005232 }
5233 if (received == expect) {
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005234 bool err;
Philipp Reisnera4fbda82011-03-16 11:13:17 +01005235
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005236 err = cmd->fn(tconn, &pi);
5237 if (err) {
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005238 conn_err(tconn, "%pf failed\n", cmd->fn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005239 goto reconnect;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005240 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005241
Philipp Reisnera4fbda82011-03-16 11:13:17 +01005242 tconn->last_received = jiffies;
5243
Philipp Reisner44ed1672011-04-19 17:10:19 +02005244 if (cmd == &asender_tbl[P_PING_ACK]) {
5245 /* restore idle timeout */
5246 tconn->meta.socket->sk->sk_rcvtimeo = ping_int * HZ;
5247 ping_timeout_active = false;
5248 }
Lars Ellenbergf36af182011-03-09 22:44:55 +01005249
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005250 buf = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005251 received = 0;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005252 expect = header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005253 cmd = NULL;
5254 }
5255 }
5256
5257 if (0) {
5258reconnect:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01005259 conn_request_state(tconn, NS(conn, C_NETWORK_FAILURE), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005260 }
5261 if (0) {
5262disconnect:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01005263 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005264 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005265 clear_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005266
Philipp Reisner32862ec2011-02-08 16:41:01 +01005267 conn_info(tconn, "asender terminated\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005268
5269 return 0;
5270}