blob: c8d3f38d539f9fa4ebe3b6c56efb52fffa2db526 [file] [log] [blame]
Philipp Reisnerb411b362009-09-25 16:07:19 -07001/*
2 drbd_receiver.c
3
4 This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
5
6 Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
7 Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
8 Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
9
10 drbd is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
13 any later version.
14
15 drbd is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with drbd; see the file COPYING. If not, write to
22 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25
Philipp Reisnerb411b362009-09-25 16:07:19 -070026#include <linux/module.h>
27
28#include <asm/uaccess.h>
29#include <net/sock.h>
30
Philipp Reisnerb411b362009-09-25 16:07:19 -070031#include <linux/drbd.h>
32#include <linux/fs.h>
33#include <linux/file.h>
34#include <linux/in.h>
35#include <linux/mm.h>
36#include <linux/memcontrol.h>
37#include <linux/mm_inline.h>
38#include <linux/slab.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070039#include <linux/pkt_sched.h>
40#define __KERNEL_SYSCALLS__
41#include <linux/unistd.h>
42#include <linux/vmalloc.h>
43#include <linux/random.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070044#include <linux/string.h>
45#include <linux/scatterlist.h>
46#include "drbd_int.h"
Philipp Reisnerb411b362009-09-25 16:07:19 -070047#include "drbd_req.h"
48
49#include "drbd_vli.h"
50
Philipp Reisner77351055b2011-02-07 17:24:26 +010051struct packet_info {
52 enum drbd_packet cmd;
Andreas Gruenbachere2857212011-03-25 00:57:38 +010053 unsigned int size;
54 unsigned int vnr;
Andreas Gruenbachere6589832011-03-30 12:54:42 +020055 void *data;
Philipp Reisner77351055b2011-02-07 17:24:26 +010056};
57
Philipp Reisnerb411b362009-09-25 16:07:19 -070058enum finish_epoch {
59 FE_STILL_LIVE,
60 FE_DESTROYED,
61 FE_RECYCLED,
62};
63
Andreas Gruenbacher60381782011-03-28 17:05:50 +020064static int drbd_do_features(struct drbd_tconn *tconn);
Philipp Reisner13e60372011-02-08 09:54:40 +010065static int drbd_do_auth(struct drbd_tconn *tconn);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +020066static int drbd_disconnected(struct drbd_conf *mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -070067
Philipp Reisner1e9dd292011-11-10 15:14:53 +010068static enum finish_epoch drbd_may_finish_epoch(struct drbd_tconn *, struct drbd_epoch *, enum epoch_event);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +010069static int e_end_block(struct drbd_work *, int);
Philipp Reisnerb411b362009-09-25 16:07:19 -070070
Philipp Reisnerb411b362009-09-25 16:07:19 -070071
72#define GFP_TRY (__GFP_HIGHMEM | __GFP_NOWARN)
73
Lars Ellenberg45bb9122010-05-14 17:10:48 +020074/*
75 * some helper functions to deal with single linked page lists,
76 * page->private being our "next" pointer.
77 */
78
79/* If at least n pages are linked at head, get n pages off.
80 * Otherwise, don't modify head, and return NULL.
81 * Locking is the responsibility of the caller.
82 */
83static struct page *page_chain_del(struct page **head, int n)
84{
85 struct page *page;
86 struct page *tmp;
87
88 BUG_ON(!n);
89 BUG_ON(!head);
90
91 page = *head;
Philipp Reisner23ce4222010-05-20 13:35:31 +020092
93 if (!page)
94 return NULL;
95
Lars Ellenberg45bb9122010-05-14 17:10:48 +020096 while (page) {
97 tmp = page_chain_next(page);
98 if (--n == 0)
99 break; /* found sufficient pages */
100 if (tmp == NULL)
101 /* insufficient pages, don't use any of them. */
102 return NULL;
103 page = tmp;
104 }
105
106 /* add end of list marker for the returned list */
107 set_page_private(page, 0);
108 /* actual return value, and adjustment of head */
109 page = *head;
110 *head = tmp;
111 return page;
112}
113
114/* may be used outside of locks to find the tail of a (usually short)
115 * "private" page chain, before adding it back to a global chain head
116 * with page_chain_add() under a spinlock. */
117static struct page *page_chain_tail(struct page *page, int *len)
118{
119 struct page *tmp;
120 int i = 1;
121 while ((tmp = page_chain_next(page)))
122 ++i, page = tmp;
123 if (len)
124 *len = i;
125 return page;
126}
127
128static int page_chain_free(struct page *page)
129{
130 struct page *tmp;
131 int i = 0;
132 page_chain_for_each_safe(page, tmp) {
133 put_page(page);
134 ++i;
135 }
136 return i;
137}
138
139static void page_chain_add(struct page **head,
140 struct page *chain_first, struct page *chain_last)
141{
142#if 1
143 struct page *tmp;
144 tmp = page_chain_tail(chain_first, NULL);
145 BUG_ON(tmp != chain_last);
146#endif
147
148 /* add chain to head */
149 set_page_private(chain_last, (unsigned long)*head);
150 *head = chain_first;
151}
152
Andreas Gruenbacher18c2d522011-04-07 21:08:50 +0200153static struct page *__drbd_alloc_pages(struct drbd_conf *mdev,
154 unsigned int number)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700155{
156 struct page *page = NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200157 struct page *tmp = NULL;
Andreas Gruenbacher18c2d522011-04-07 21:08:50 +0200158 unsigned int i = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700159
160 /* Yes, testing drbd_pp_vacant outside the lock is racy.
161 * So what. It saves a spin_lock. */
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200162 if (drbd_pp_vacant >= number) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700163 spin_lock(&drbd_pp_lock);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200164 page = page_chain_del(&drbd_pp_pool, number);
165 if (page)
166 drbd_pp_vacant -= number;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700167 spin_unlock(&drbd_pp_lock);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200168 if (page)
169 return page;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700170 }
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200171
Philipp Reisnerb411b362009-09-25 16:07:19 -0700172 /* GFP_TRY, because we must not cause arbitrary write-out: in a DRBD
173 * "criss-cross" setup, that might cause write-out on some other DRBD,
174 * which in turn might block on the other node at this very place. */
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200175 for (i = 0; i < number; i++) {
176 tmp = alloc_page(GFP_TRY);
177 if (!tmp)
178 break;
179 set_page_private(tmp, (unsigned long)page);
180 page = tmp;
181 }
182
183 if (i == number)
184 return page;
185
186 /* Not enough pages immediately available this time.
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +0200187 * No need to jump around here, drbd_alloc_pages will retry this
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200188 * function "soon". */
189 if (page) {
190 tmp = page_chain_tail(page, NULL);
191 spin_lock(&drbd_pp_lock);
192 page_chain_add(&drbd_pp_pool, page, tmp);
193 drbd_pp_vacant += i;
194 spin_unlock(&drbd_pp_lock);
195 }
196 return NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700197}
198
Andreas Gruenbachera990be42011-04-06 17:56:48 +0200199static void reclaim_finished_net_peer_reqs(struct drbd_conf *mdev,
200 struct list_head *to_be_freed)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700201{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100202 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700203 struct list_head *le, *tle;
204
205 /* The EEs are always appended to the end of the list. Since
206 they are sent in order over the wire, they have to finish
207 in order. As soon as we see the first not finished we can
208 stop to examine the list... */
209
210 list_for_each_safe(le, tle, &mdev->net_ee) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100211 peer_req = list_entry(le, struct drbd_peer_request, w.list);
Andreas Gruenbacher045417f2011-04-07 21:34:24 +0200212 if (drbd_peer_req_has_active_page(peer_req))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700213 break;
214 list_move(le, to_be_freed);
215 }
216}
217
218static void drbd_kick_lo_and_reclaim_net(struct drbd_conf *mdev)
219{
220 LIST_HEAD(reclaimed);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100221 struct drbd_peer_request *peer_req, *t;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700222
Philipp Reisner87eeee42011-01-19 14:16:30 +0100223 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbachera990be42011-04-06 17:56:48 +0200224 reclaim_finished_net_peer_reqs(mdev, &reclaimed);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100225 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700226
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100227 list_for_each_entry_safe(peer_req, t, &reclaimed, w.list)
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200228 drbd_free_net_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700229}
230
231/**
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +0200232 * drbd_alloc_pages() - Returns @number pages, retries forever (or until signalled)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700233 * @mdev: DRBD device.
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200234 * @number: number of pages requested
235 * @retry: whether to retry, if not enough pages are available right now
Philipp Reisnerb411b362009-09-25 16:07:19 -0700236 *
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200237 * Tries to allocate number pages, first from our own page pool, then from
238 * the kernel, unless this allocation would exceed the max_buffers setting.
239 * Possibly retry until DRBD frees sufficient pages somewhere else.
240 *
241 * Returns a page chain linked via page->private.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700242 */
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +0200243struct page *drbd_alloc_pages(struct drbd_conf *mdev, unsigned int number,
244 bool retry)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700245{
246 struct page *page = NULL;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200247 struct net_conf *nc;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700248 DEFINE_WAIT(wait);
Philipp Reisner44ed1672011-04-19 17:10:19 +0200249 int mxb;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700250
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200251 /* Yes, we may run up to @number over max_buffers. If we
252 * follow it strictly, the admin will get it wrong anyways. */
Philipp Reisner44ed1672011-04-19 17:10:19 +0200253 rcu_read_lock();
254 nc = rcu_dereference(mdev->tconn->net_conf);
255 mxb = nc ? nc->max_buffers : 1000000;
256 rcu_read_unlock();
257
258 if (atomic_read(&mdev->pp_in_use) < mxb)
Andreas Gruenbacher18c2d522011-04-07 21:08:50 +0200259 page = __drbd_alloc_pages(mdev, number);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700260
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200261 while (page == NULL) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700262 prepare_to_wait(&drbd_pp_wait, &wait, TASK_INTERRUPTIBLE);
263
264 drbd_kick_lo_and_reclaim_net(mdev);
265
Philipp Reisner44ed1672011-04-19 17:10:19 +0200266 if (atomic_read(&mdev->pp_in_use) < mxb) {
Andreas Gruenbacher18c2d522011-04-07 21:08:50 +0200267 page = __drbd_alloc_pages(mdev, number);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700268 if (page)
269 break;
270 }
271
272 if (!retry)
273 break;
274
275 if (signal_pending(current)) {
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +0200276 dev_warn(DEV, "drbd_alloc_pages interrupted!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700277 break;
278 }
279
280 schedule();
281 }
282 finish_wait(&drbd_pp_wait, &wait);
283
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200284 if (page)
285 atomic_add(number, &mdev->pp_in_use);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700286 return page;
287}
288
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +0200289/* Must not be used from irq, as that may deadlock: see drbd_alloc_pages.
Philipp Reisner87eeee42011-01-19 14:16:30 +0100290 * Is also used from inside an other spin_lock_irq(&mdev->tconn->req_lock);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200291 * Either links the page chain back to the global pool,
292 * or returns all pages to the system. */
Andreas Gruenbacher5cc287e2011-04-07 21:02:59 +0200293static void drbd_free_pages(struct drbd_conf *mdev, struct page *page, int is_net)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700294{
Lars Ellenberg435f0742010-09-06 12:30:25 +0200295 atomic_t *a = is_net ? &mdev->pp_in_use_by_net : &mdev->pp_in_use;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700296 int i;
Lars Ellenberg435f0742010-09-06 12:30:25 +0200297
Philipp Reisner81a5d602011-02-22 19:53:16 -0500298 if (drbd_pp_vacant > (DRBD_MAX_BIO_SIZE/PAGE_SIZE) * minor_count)
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200299 i = page_chain_free(page);
300 else {
301 struct page *tmp;
302 tmp = page_chain_tail(page, &i);
303 spin_lock(&drbd_pp_lock);
304 page_chain_add(&drbd_pp_pool, page, tmp);
305 drbd_pp_vacant += i;
306 spin_unlock(&drbd_pp_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700307 }
Lars Ellenberg435f0742010-09-06 12:30:25 +0200308 i = atomic_sub_return(i, a);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200309 if (i < 0)
Lars Ellenberg435f0742010-09-06 12:30:25 +0200310 dev_warn(DEV, "ASSERTION FAILED: %s: %d < 0\n",
311 is_net ? "pp_in_use_by_net" : "pp_in_use", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700312 wake_up(&drbd_pp_wait);
313}
314
315/*
316You need to hold the req_lock:
317 _drbd_wait_ee_list_empty()
318
319You must not have the req_lock:
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200320 drbd_free_peer_req()
Andreas Gruenbacher0db55362011-04-06 16:09:15 +0200321 drbd_alloc_peer_req()
Andreas Gruenbacher7721f562011-04-06 17:14:02 +0200322 drbd_free_peer_reqs()
Philipp Reisnerb411b362009-09-25 16:07:19 -0700323 drbd_ee_fix_bhs()
Andreas Gruenbachera990be42011-04-06 17:56:48 +0200324 drbd_finish_peer_reqs()
Philipp Reisnerb411b362009-09-25 16:07:19 -0700325 drbd_clear_done_ee()
326 drbd_wait_ee_list_empty()
327*/
328
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +0100329struct drbd_peer_request *
Andreas Gruenbacher0db55362011-04-06 16:09:15 +0200330drbd_alloc_peer_req(struct drbd_conf *mdev, u64 id, sector_t sector,
331 unsigned int data_size, gfp_t gfp_mask) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700332{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100333 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700334 struct page *page;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200335 unsigned nr_pages = (data_size + PAGE_SIZE -1) >> PAGE_SHIFT;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700336
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +0100337 if (drbd_insert_fault(mdev, DRBD_FAULT_AL_EE))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700338 return NULL;
339
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100340 peer_req = mempool_alloc(drbd_ee_mempool, gfp_mask & ~__GFP_HIGHMEM);
341 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700342 if (!(gfp_mask & __GFP_NOWARN))
Andreas Gruenbacher0db55362011-04-06 16:09:15 +0200343 dev_err(DEV, "%s: allocation failed\n", __func__);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700344 return NULL;
345 }
346
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +0200347 page = drbd_alloc_pages(mdev, nr_pages, (gfp_mask & __GFP_WAIT));
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200348 if (!page)
349 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700350
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100351 drbd_clear_interval(&peer_req->i);
352 peer_req->i.size = data_size;
353 peer_req->i.sector = sector;
354 peer_req->i.local = false;
355 peer_req->i.waiting = false;
Andreas Gruenbacher53840642011-01-28 10:31:04 +0100356
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100357 peer_req->epoch = NULL;
Philipp Reisnera21e9292011-02-08 15:08:49 +0100358 peer_req->w.mdev = mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100359 peer_req->pages = page;
360 atomic_set(&peer_req->pending_bios, 0);
361 peer_req->flags = 0;
Andreas Gruenbacher9a8e7752011-01-11 14:04:09 +0100362 /*
363 * The block_id is opaque to the receiver. It is not endianness
364 * converted, and sent back to the sender unchanged.
365 */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100366 peer_req->block_id = id;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700367
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100368 return peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700369
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200370 fail:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100371 mempool_free(peer_req, drbd_ee_mempool);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700372 return NULL;
373}
374
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200375void __drbd_free_peer_req(struct drbd_conf *mdev, struct drbd_peer_request *peer_req,
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +0100376 int is_net)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700377{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100378 if (peer_req->flags & EE_HAS_DIGEST)
379 kfree(peer_req->digest);
Andreas Gruenbacher5cc287e2011-04-07 21:02:59 +0200380 drbd_free_pages(mdev, peer_req->pages, is_net);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100381 D_ASSERT(atomic_read(&peer_req->pending_bios) == 0);
382 D_ASSERT(drbd_interval_empty(&peer_req->i));
383 mempool_free(peer_req, drbd_ee_mempool);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700384}
385
Andreas Gruenbacher7721f562011-04-06 17:14:02 +0200386int drbd_free_peer_reqs(struct drbd_conf *mdev, struct list_head *list)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700387{
388 LIST_HEAD(work_list);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100389 struct drbd_peer_request *peer_req, *t;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700390 int count = 0;
Lars Ellenberg435f0742010-09-06 12:30:25 +0200391 int is_net = list == &mdev->net_ee;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700392
Philipp Reisner87eeee42011-01-19 14:16:30 +0100393 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700394 list_splice_init(list, &work_list);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100395 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700396
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100397 list_for_each_entry_safe(peer_req, t, &work_list, w.list) {
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200398 __drbd_free_peer_req(mdev, peer_req, is_net);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700399 count++;
400 }
401 return count;
402}
403
Andreas Gruenbachera990be42011-04-06 17:56:48 +0200404/*
405 * See also comments in _req_mod(,BARRIER_ACKED) and receive_Barrier.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700406 */
Andreas Gruenbachera990be42011-04-06 17:56:48 +0200407static int drbd_finish_peer_reqs(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700408{
409 LIST_HEAD(work_list);
410 LIST_HEAD(reclaimed);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100411 struct drbd_peer_request *peer_req, *t;
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100412 int err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700413
Philipp Reisner87eeee42011-01-19 14:16:30 +0100414 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbachera990be42011-04-06 17:56:48 +0200415 reclaim_finished_net_peer_reqs(mdev, &reclaimed);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700416 list_splice_init(&mdev->done_ee, &work_list);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100417 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700418
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100419 list_for_each_entry_safe(peer_req, t, &reclaimed, w.list)
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200420 drbd_free_net_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700421
422 /* possible callbacks here:
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +0100423 * e_end_block, and e_end_resync_block, e_send_discard_write.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700424 * all ignore the last argument.
425 */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100426 list_for_each_entry_safe(peer_req, t, &work_list, w.list) {
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100427 int err2;
428
Philipp Reisnerb411b362009-09-25 16:07:19 -0700429 /* list_del not necessary, next/prev members not touched */
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100430 err2 = peer_req->w.cb(&peer_req->w, !!err);
431 if (!err)
432 err = err2;
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200433 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700434 }
435 wake_up(&mdev->ee_wait);
436
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100437 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700438}
439
Andreas Gruenbacherd4da1532011-04-07 00:06:56 +0200440static void _drbd_wait_ee_list_empty(struct drbd_conf *mdev,
441 struct list_head *head)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700442{
443 DEFINE_WAIT(wait);
444
445 /* avoids spin_lock/unlock
446 * and calling prepare_to_wait in the fast path */
447 while (!list_empty(head)) {
448 prepare_to_wait(&mdev->ee_wait, &wait, TASK_UNINTERRUPTIBLE);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100449 spin_unlock_irq(&mdev->tconn->req_lock);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100450 io_schedule();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700451 finish_wait(&mdev->ee_wait, &wait);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100452 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700453 }
454}
455
Andreas Gruenbacherd4da1532011-04-07 00:06:56 +0200456static void drbd_wait_ee_list_empty(struct drbd_conf *mdev,
457 struct list_head *head)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700458{
Philipp Reisner87eeee42011-01-19 14:16:30 +0100459 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700460 _drbd_wait_ee_list_empty(mdev, head);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100461 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700462}
463
464/* see also kernel_accept; which is only present since 2.6.18.
465 * also we want to log which part of it failed, exactly */
Philipp Reisner76536202011-02-07 14:09:54 +0100466static int drbd_accept(const char **what, struct socket *sock, struct socket **newsock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700467{
468 struct sock *sk = sock->sk;
469 int err = 0;
470
471 *what = "listen";
472 err = sock->ops->listen(sock, 5);
473 if (err < 0)
474 goto out;
475
476 *what = "sock_create_lite";
477 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
478 newsock);
479 if (err < 0)
480 goto out;
481
482 *what = "accept";
483 err = sock->ops->accept(sock, *newsock, 0);
484 if (err < 0) {
485 sock_release(*newsock);
486 *newsock = NULL;
487 goto out;
488 }
489 (*newsock)->ops = sock->ops;
490
491out:
492 return err;
493}
494
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100495static int drbd_recv_short(struct socket *sock, void *buf, size_t size, int flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700496{
497 mm_segment_t oldfs;
498 struct kvec iov = {
499 .iov_base = buf,
500 .iov_len = size,
501 };
502 struct msghdr msg = {
503 .msg_iovlen = 1,
504 .msg_iov = (struct iovec *)&iov,
505 .msg_flags = (flags ? flags : MSG_WAITALL | MSG_NOSIGNAL)
506 };
507 int rv;
508
509 oldfs = get_fs();
510 set_fs(KERNEL_DS);
511 rv = sock_recvmsg(sock, &msg, size, msg.msg_flags);
512 set_fs(oldfs);
513
514 return rv;
515}
516
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100517static int drbd_recv(struct drbd_tconn *tconn, void *buf, size_t size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700518{
519 mm_segment_t oldfs;
520 struct kvec iov = {
521 .iov_base = buf,
522 .iov_len = size,
523 };
524 struct msghdr msg = {
525 .msg_iovlen = 1,
526 .msg_iov = (struct iovec *)&iov,
527 .msg_flags = MSG_WAITALL | MSG_NOSIGNAL
528 };
529 int rv;
530
531 oldfs = get_fs();
532 set_fs(KERNEL_DS);
533
534 for (;;) {
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100535 rv = sock_recvmsg(tconn->data.socket, &msg, size, msg.msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700536 if (rv == size)
537 break;
538
539 /* Note:
540 * ECONNRESET other side closed the connection
541 * ERESTARTSYS (on sock) we got a signal
542 */
543
544 if (rv < 0) {
545 if (rv == -ECONNRESET)
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100546 conn_info(tconn, "sock was reset by peer\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700547 else if (rv != -ERESTARTSYS)
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100548 conn_err(tconn, "sock_recvmsg returned %d\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700549 break;
550 } else if (rv == 0) {
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100551 conn_info(tconn, "sock was shut down by peer\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700552 break;
553 } else {
554 /* signal came in, or peer/link went down,
555 * after we read a partial message
556 */
557 /* D_ASSERT(signal_pending(current)); */
558 break;
559 }
560 };
561
562 set_fs(oldfs);
563
564 if (rv != size)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100565 conn_request_state(tconn, NS(conn, C_BROKEN_PIPE), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700566
567 return rv;
568}
569
Andreas Gruenbacherc6967742011-03-17 17:15:20 +0100570static int drbd_recv_all(struct drbd_tconn *tconn, void *buf, size_t size)
571{
572 int err;
573
574 err = drbd_recv(tconn, buf, size);
575 if (err != size) {
576 if (err >= 0)
577 err = -EIO;
578 } else
579 err = 0;
580 return err;
581}
582
Andreas Gruenbachera5c31902011-03-24 03:28:04 +0100583static int drbd_recv_all_warn(struct drbd_tconn *tconn, void *buf, size_t size)
584{
585 int err;
586
587 err = drbd_recv_all(tconn, buf, size);
588 if (err && !signal_pending(current))
589 conn_warn(tconn, "short read (expected size %d)\n", (int)size);
590 return err;
591}
592
Lars Ellenberg5dbf1672010-05-25 16:18:01 +0200593/* quoting tcp(7):
594 * On individual connections, the socket buffer size must be set prior to the
595 * listen(2) or connect(2) calls in order to have it take effect.
596 * This is our wrapper to do so.
597 */
598static void drbd_setbufsize(struct socket *sock, unsigned int snd,
599 unsigned int rcv)
600{
601 /* open coded SO_SNDBUF, SO_RCVBUF */
602 if (snd) {
603 sock->sk->sk_sndbuf = snd;
604 sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
605 }
606 if (rcv) {
607 sock->sk->sk_rcvbuf = rcv;
608 sock->sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
609 }
610}
611
Philipp Reisnereac3e992011-02-07 14:05:07 +0100612static struct socket *drbd_try_connect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700613{
614 const char *what;
615 struct socket *sock;
616 struct sockaddr_in6 src_in6;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200617 struct sockaddr_in6 peer_in6;
618 struct net_conf *nc;
619 int err, peer_addr_len, my_addr_len;
Andreas Gruenbacher69ef82d2011-05-11 14:34:35 +0200620 int sndbuf_size, rcvbuf_size, connect_int;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700621 int disconnect_on_error = 1;
622
Philipp Reisner44ed1672011-04-19 17:10:19 +0200623 rcu_read_lock();
624 nc = rcu_dereference(tconn->net_conf);
625 if (!nc) {
626 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700627 return NULL;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200628 }
Philipp Reisner44ed1672011-04-19 17:10:19 +0200629 sndbuf_size = nc->sndbuf_size;
630 rcvbuf_size = nc->rcvbuf_size;
Andreas Gruenbacher69ef82d2011-05-11 14:34:35 +0200631 connect_int = nc->connect_int;
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200632 rcu_read_unlock();
Philipp Reisner44ed1672011-04-19 17:10:19 +0200633
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200634 my_addr_len = min_t(int, tconn->my_addr_len, sizeof(src_in6));
635 memcpy(&src_in6, &tconn->my_addr, my_addr_len);
Philipp Reisner44ed1672011-04-19 17:10:19 +0200636
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200637 if (((struct sockaddr *)&tconn->my_addr)->sa_family == AF_INET6)
Philipp Reisner44ed1672011-04-19 17:10:19 +0200638 src_in6.sin6_port = 0;
639 else
640 ((struct sockaddr_in *)&src_in6)->sin_port = 0; /* AF_INET & AF_SCI */
641
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200642 peer_addr_len = min_t(int, tconn->peer_addr_len, sizeof(src_in6));
643 memcpy(&peer_in6, &tconn->peer_addr, peer_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700644
645 what = "sock_create_kern";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200646 err = sock_create_kern(((struct sockaddr *)&src_in6)->sa_family,
647 SOCK_STREAM, IPPROTO_TCP, &sock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700648 if (err < 0) {
649 sock = NULL;
650 goto out;
651 }
652
653 sock->sk->sk_rcvtimeo =
Andreas Gruenbacher69ef82d2011-05-11 14:34:35 +0200654 sock->sk->sk_sndtimeo = connect_int * HZ;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200655 drbd_setbufsize(sock, sndbuf_size, rcvbuf_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700656
657 /* explicitly bind to the configured IP as source IP
658 * for the outgoing connections.
659 * This is needed for multihomed hosts and to be
660 * able to use lo: interfaces for drbd.
661 * Make sure to use 0 as port number, so linux selects
662 * a free one dynamically.
663 */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700664 what = "bind before connect";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200665 err = sock->ops->bind(sock, (struct sockaddr *) &src_in6, my_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700666 if (err < 0)
667 goto out;
668
669 /* connect may fail, peer not yet available.
670 * stay C_WF_CONNECTION, don't go Disconnecting! */
671 disconnect_on_error = 0;
672 what = "connect";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200673 err = sock->ops->connect(sock, (struct sockaddr *) &peer_in6, peer_addr_len, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700674
675out:
676 if (err < 0) {
677 if (sock) {
678 sock_release(sock);
679 sock = NULL;
680 }
681 switch (-err) {
682 /* timeout, busy, signal pending */
683 case ETIMEDOUT: case EAGAIN: case EINPROGRESS:
684 case EINTR: case ERESTARTSYS:
685 /* peer not (yet) available, network problem */
686 case ECONNREFUSED: case ENETUNREACH:
687 case EHOSTDOWN: case EHOSTUNREACH:
688 disconnect_on_error = 0;
689 break;
690 default:
Philipp Reisnereac3e992011-02-07 14:05:07 +0100691 conn_err(tconn, "%s failed, err = %d\n", what, err);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700692 }
693 if (disconnect_on_error)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100694 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700695 }
Philipp Reisner44ed1672011-04-19 17:10:19 +0200696
Philipp Reisnerb411b362009-09-25 16:07:19 -0700697 return sock;
698}
699
Philipp Reisner76536202011-02-07 14:09:54 +0100700static struct socket *drbd_wait_for_connect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700701{
Philipp Reisner44ed1672011-04-19 17:10:19 +0200702 int timeo, err, my_addr_len;
Andreas Gruenbacher69ef82d2011-05-11 14:34:35 +0200703 int sndbuf_size, rcvbuf_size, connect_int;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700704 struct socket *s_estab = NULL, *s_listen;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200705 struct sockaddr_in6 my_addr;
706 struct net_conf *nc;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700707 const char *what;
708
Philipp Reisner44ed1672011-04-19 17:10:19 +0200709 rcu_read_lock();
710 nc = rcu_dereference(tconn->net_conf);
711 if (!nc) {
712 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700713 return NULL;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200714 }
Philipp Reisner44ed1672011-04-19 17:10:19 +0200715 sndbuf_size = nc->sndbuf_size;
716 rcvbuf_size = nc->rcvbuf_size;
Andreas Gruenbacher69ef82d2011-05-11 14:34:35 +0200717 connect_int = nc->connect_int;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200718 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700719
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200720 my_addr_len = min_t(int, tconn->my_addr_len, sizeof(struct sockaddr_in6));
721 memcpy(&my_addr, &tconn->my_addr, my_addr_len);
722
Philipp Reisnerb411b362009-09-25 16:07:19 -0700723 what = "sock_create_kern";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200724 err = sock_create_kern(((struct sockaddr *)&my_addr)->sa_family,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700725 SOCK_STREAM, IPPROTO_TCP, &s_listen);
726 if (err) {
727 s_listen = NULL;
728 goto out;
729 }
730
Andreas Gruenbacher69ef82d2011-05-11 14:34:35 +0200731 timeo = connect_int * HZ;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700732 timeo += (random32() & 1) ? timeo / 7 : -timeo / 7; /* 28.5% random jitter */
733
734 s_listen->sk->sk_reuse = 1; /* SO_REUSEADDR */
735 s_listen->sk->sk_rcvtimeo = timeo;
736 s_listen->sk->sk_sndtimeo = timeo;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200737 drbd_setbufsize(s_listen, sndbuf_size, rcvbuf_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700738
739 what = "bind before listen";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200740 err = s_listen->ops->bind(s_listen, (struct sockaddr *)&my_addr, my_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700741 if (err < 0)
742 goto out;
743
Philipp Reisner76536202011-02-07 14:09:54 +0100744 err = drbd_accept(&what, s_listen, &s_estab);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700745
746out:
747 if (s_listen)
748 sock_release(s_listen);
749 if (err < 0) {
750 if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) {
Philipp Reisner76536202011-02-07 14:09:54 +0100751 conn_err(tconn, "%s failed, err = %d\n", what, err);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100752 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700753 }
754 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700755
756 return s_estab;
757}
758
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200759static int decode_header(struct drbd_tconn *, void *, struct packet_info *);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700760
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200761static int send_first_packet(struct drbd_tconn *tconn, struct drbd_socket *sock,
762 enum drbd_packet cmd)
763{
764 if (!conn_prepare_command(tconn, sock))
765 return -EIO;
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200766 return conn_send_command(tconn, sock, cmd, 0, NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700767}
768
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200769static int receive_first_packet(struct drbd_tconn *tconn, struct socket *sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700770{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200771 unsigned int header_size = drbd_header_size(tconn);
772 struct packet_info pi;
773 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700774
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200775 err = drbd_recv_short(sock, tconn->data.rbuf, header_size, 0);
776 if (err != header_size) {
777 if (err >= 0)
778 err = -EIO;
779 return err;
780 }
781 err = decode_header(tconn, tconn->data.rbuf, &pi);
782 if (err)
783 return err;
784 return pi.cmd;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700785}
786
787/**
788 * drbd_socket_okay() - Free the socket if its connection is not okay
Philipp Reisnerb411b362009-09-25 16:07:19 -0700789 * @sock: pointer to the pointer to the socket.
790 */
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100791static int drbd_socket_okay(struct socket **sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700792{
793 int rr;
794 char tb[4];
795
796 if (!*sock)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100797 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700798
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100799 rr = drbd_recv_short(*sock, tb, 4, MSG_DONTWAIT | MSG_PEEK);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700800
801 if (rr > 0 || rr == -EAGAIN) {
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100802 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700803 } else {
804 sock_release(*sock);
805 *sock = NULL;
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100806 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700807 }
808}
Philipp Reisner2325eb62011-03-15 16:56:18 +0100809/* Gets called if a connection is established, or if a new minor gets created
810 in a connection */
Philipp Reisnerc141ebd2011-05-05 16:13:10 +0200811int drbd_connected(struct drbd_conf *mdev)
Philipp Reisner907599e2011-02-08 11:25:37 +0100812{
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100813 int err;
Philipp Reisner907599e2011-02-08 11:25:37 +0100814
815 atomic_set(&mdev->packet_seq, 0);
816 mdev->peer_seq = 0;
817
Philipp 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{
Philipp Reisner7da35862011-12-19 22:42:56 +0100845 struct drbd_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 Reisner7da35862011-12-19 22:42:56 +0100854 mutex_init(&sock.mutex);
855 sock.sbuf = tconn->data.sbuf;
856 sock.rbuf = tconn->data.rbuf;
857 sock.socket = NULL;
858 mutex_init(&msock.mutex);
859 msock.sbuf = tconn->meta.sbuf;
860 msock.rbuf = tconn->meta.rbuf;
861 msock.socket = NULL;
862
Philipp Reisner907599e2011-02-08 11:25:37 +0100863 clear_bit(DISCARD_CONCURRENT, &tconn->flags);
Andreas Gruenbacher0916e0e2011-03-21 14:10:15 +0100864
865 /* Assume that the peer only understands protocol 80 until we know better. */
866 tconn->agreed_pro_version = 80;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700867
Philipp Reisnerb411b362009-09-25 16:07:19 -0700868 do {
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200869 struct socket *s;
870
Philipp Reisnerb411b362009-09-25 16:07:19 -0700871 for (try = 0;;) {
872 /* 3 tries, this should take less than a second! */
Philipp Reisner907599e2011-02-08 11:25:37 +0100873 s = drbd_try_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700874 if (s || ++try >= 3)
875 break;
876 /* give the other side time to call bind() & listen() */
Philipp Reisner20ee6392011-01-18 15:28:59 +0100877 schedule_timeout_interruptible(HZ / 10);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700878 }
879
880 if (s) {
Philipp Reisner7da35862011-12-19 22:42:56 +0100881 if (!sock.socket) {
882 sock.socket = s;
883 send_first_packet(tconn, &sock, P_INITIAL_DATA);
884 } else if (!msock.socket) {
885 msock.socket = s;
886 send_first_packet(tconn, &msock, P_INITIAL_META);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700887 } else {
Philipp Reisner81fa2e62011-05-04 15:10:30 +0200888 conn_err(tconn, "Logic error in conn_connect()\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700889 goto out_release_sockets;
890 }
891 }
892
Philipp Reisner7da35862011-12-19 22:42:56 +0100893 if (sock.socket && msock.socket) {
894 rcu_read_lock();
895 nc = rcu_dereference(tconn->net_conf);
896 timeout = nc->ping_timeo * HZ / 10;
897 rcu_read_unlock();
898 schedule_timeout_interruptible(timeout);
899 ok = drbd_socket_okay(&sock.socket);
900 ok = drbd_socket_okay(&msock.socket) && ok;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700901 if (ok)
902 break;
903 }
904
905retry:
Philipp Reisner907599e2011-02-08 11:25:37 +0100906 s = drbd_wait_for_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700907 if (s) {
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200908 try = receive_first_packet(tconn, s);
Philipp Reisner7da35862011-12-19 22:42:56 +0100909 drbd_socket_okay(&sock.socket);
910 drbd_socket_okay(&msock.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700911 switch (try) {
Andreas Gruenbachere5d6f332011-03-28 16:44:40 +0200912 case P_INITIAL_DATA:
Philipp Reisner7da35862011-12-19 22:42:56 +0100913 if (sock.socket) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100914 conn_warn(tconn, "initial packet S crossed\n");
Philipp Reisner7da35862011-12-19 22:42:56 +0100915 sock_release(sock.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700916 }
Philipp Reisner7da35862011-12-19 22:42:56 +0100917 sock.socket = s;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700918 break;
Andreas Gruenbachere5d6f332011-03-28 16:44:40 +0200919 case P_INITIAL_META:
Philipp Reisner7da35862011-12-19 22:42:56 +0100920 if (msock.socket) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100921 conn_warn(tconn, "initial packet M crossed\n");
Philipp Reisner7da35862011-12-19 22:42:56 +0100922 sock_release(msock.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700923 }
Philipp Reisner7da35862011-12-19 22:42:56 +0100924 msock.socket = s;
Philipp Reisner907599e2011-02-08 11:25:37 +0100925 set_bit(DISCARD_CONCURRENT, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700926 break;
927 default:
Philipp Reisner907599e2011-02-08 11:25:37 +0100928 conn_warn(tconn, "Error receiving initial packet\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700929 sock_release(s);
930 if (random32() & 1)
931 goto retry;
932 }
933 }
934
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100935 if (tconn->cstate <= C_DISCONNECTING)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700936 goto out_release_sockets;
937 if (signal_pending(current)) {
938 flush_signals(current);
939 smp_rmb();
Philipp Reisner907599e2011-02-08 11:25:37 +0100940 if (get_t_state(&tconn->receiver) == EXITING)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700941 goto out_release_sockets;
942 }
943
Philipp Reisner7da35862011-12-19 22:42:56 +0100944 if (sock.socket && &msock.socket) {
945 ok = drbd_socket_okay(&sock.socket);
946 ok = drbd_socket_okay(&msock.socket) && ok;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700947 if (ok)
948 break;
949 }
950 } while (1);
951
Philipp Reisner7da35862011-12-19 22:42:56 +0100952 sock.socket->sk->sk_reuse = 1; /* SO_REUSEADDR */
953 msock.socket->sk->sk_reuse = 1; /* SO_REUSEADDR */
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200954
Philipp Reisner7da35862011-12-19 22:42:56 +0100955 sock.socket->sk->sk_allocation = GFP_NOIO;
956 msock.socket->sk->sk_allocation = GFP_NOIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700957
Philipp Reisner7da35862011-12-19 22:42:56 +0100958 sock.socket->sk->sk_priority = TC_PRIO_INTERACTIVE_BULK;
959 msock.socket->sk->sk_priority = TC_PRIO_INTERACTIVE;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700960
Philipp Reisnerb411b362009-09-25 16:07:19 -0700961 /* NOT YET ...
Philipp Reisner7da35862011-12-19 22:42:56 +0100962 * sock.socket->sk->sk_sndtimeo = tconn->net_conf->timeout*HZ/10;
963 * sock.socket->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
Andreas Gruenbacher60381782011-03-28 17:05:50 +0200964 * first set it to the P_CONNECTION_FEATURES timeout,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700965 * which we set to 4x the configured ping_timeout. */
Philipp Reisner44ed1672011-04-19 17:10:19 +0200966 rcu_read_lock();
967 nc = rcu_dereference(tconn->net_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700968
Philipp Reisner7da35862011-12-19 22:42:56 +0100969 sock.socket->sk->sk_sndtimeo =
970 sock.socket->sk->sk_rcvtimeo = nc->ping_timeo*4*HZ/10;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200971
Philipp Reisner7da35862011-12-19 22:42:56 +0100972 msock.socket->sk->sk_rcvtimeo = nc->ping_int*HZ;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200973 timeout = nc->timeout * HZ / 10;
Philipp Reisner08b165b2011-09-05 16:22:33 +0200974 discard_my_data = nc->discard_my_data;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200975 rcu_read_unlock();
976
Philipp Reisner7da35862011-12-19 22:42:56 +0100977 msock.socket->sk->sk_sndtimeo = timeout;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700978
979 /* we don't want delays.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300980 * we use TCP_CORK where appropriate, though */
Philipp Reisner7da35862011-12-19 22:42:56 +0100981 drbd_tcp_nodelay(sock.socket);
982 drbd_tcp_nodelay(msock.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700983
Philipp Reisner7da35862011-12-19 22:42:56 +0100984 tconn->data.socket = sock.socket;
985 tconn->meta.socket = msock.socket;
Philipp Reisner907599e2011-02-08 11:25:37 +0100986 tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700987
Andreas Gruenbacher60381782011-03-28 17:05:50 +0200988 h = drbd_do_features(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700989 if (h <= 0)
990 return h;
991
Philipp Reisner907599e2011-02-08 11:25:37 +0100992 if (tconn->cram_hmac_tfm) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700993 /* drbd_request_state(mdev, NS(conn, WFAuth)); */
Philipp Reisner907599e2011-02-08 11:25:37 +0100994 switch (drbd_do_auth(tconn)) {
Johannes Thomab10d96c2010-01-07 16:02:50 +0100995 case -1:
Philipp Reisner907599e2011-02-08 11:25:37 +0100996 conn_err(tconn, "Authentication of peer failed\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700997 return -1;
Johannes Thomab10d96c2010-01-07 16:02:50 +0100998 case 0:
Philipp Reisner907599e2011-02-08 11:25:37 +0100999 conn_err(tconn, "Authentication of peer failed, trying again.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01001000 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001001 }
1002 }
1003
Philipp Reisner7da35862011-12-19 22:42:56 +01001004 tconn->data.socket->sk->sk_sndtimeo = timeout;
1005 tconn->data.socket->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001006
Andreas Gruenbacher387eb302011-03-16 01:05:37 +01001007 if (drbd_send_protocol(tconn) == -EOPNOTSUPP)
Philipp Reisner7e2455c2010-04-22 14:50:23 +02001008 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001009
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02001010 rcu_read_lock();
1011 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1012 kref_get(&mdev->kref);
1013 rcu_read_unlock();
Philipp Reisner08b165b2011-09-05 16:22:33 +02001014
1015 if (discard_my_data)
1016 set_bit(DISCARD_MY_DATA, &mdev->flags);
1017 else
1018 clear_bit(DISCARD_MY_DATA, &mdev->flags);
1019
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02001020 drbd_connected(mdev);
1021 kref_put(&mdev->kref, &drbd_minor_destroy);
1022 rcu_read_lock();
1023 }
1024 rcu_read_unlock();
1025
Philipp Reisner823bd832012-11-08 15:04:36 +01001026 if (conn_request_state(tconn, NS(conn, C_WF_REPORT_PARAMS), CS_VERBOSE) < SS_SUCCESS)
1027 return 0;
1028
1029 drbd_thread_start(&tconn->asender);
1030
Philipp Reisner08b165b2011-09-05 16:22:33 +02001031 mutex_lock(&tconn->conf_update);
1032 /* The discard_my_data flag is a single-shot modifier to the next
1033 * connection attempt, the handshake of which is now well underway.
1034 * No need for rcu style copying of the whole struct
1035 * just to clear a single value. */
1036 tconn->net_conf->discard_my_data = 0;
1037 mutex_unlock(&tconn->conf_update);
1038
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07001039 return h;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001040
1041out_release_sockets:
Philipp Reisner7da35862011-12-19 22:42:56 +01001042 if (sock.socket)
1043 sock_release(sock.socket);
1044 if (msock.socket)
1045 sock_release(msock.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001046 return -1;
1047}
1048
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001049static int decode_header(struct drbd_tconn *tconn, void *header, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001050{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001051 unsigned int header_size = drbd_header_size(tconn);
1052
Andreas Gruenbacher0c8e36d2011-03-30 16:00:17 +02001053 if (header_size == sizeof(struct p_header100) &&
1054 *(__be32 *)header == cpu_to_be32(DRBD_MAGIC_100)) {
1055 struct p_header100 *h = header;
1056 if (h->pad != 0) {
1057 conn_err(tconn, "Header padding is not zero\n");
1058 return -EINVAL;
1059 }
1060 pi->vnr = be16_to_cpu(h->volume);
1061 pi->cmd = be16_to_cpu(h->command);
1062 pi->size = be32_to_cpu(h->length);
1063 } else if (header_size == sizeof(struct p_header95) &&
1064 *(__be16 *)header == cpu_to_be16(DRBD_MAGIC_BIG)) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001065 struct p_header95 *h = header;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001066 pi->cmd = be16_to_cpu(h->command);
Andreas Gruenbacherb55d84b2011-03-22 13:17:47 +01001067 pi->size = be32_to_cpu(h->length);
1068 pi->vnr = 0;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001069 } else if (header_size == sizeof(struct p_header80) &&
1070 *(__be32 *)header == cpu_to_be32(DRBD_MAGIC)) {
1071 struct p_header80 *h = header;
1072 pi->cmd = be16_to_cpu(h->command);
1073 pi->size = be16_to_cpu(h->length);
Philipp Reisner77351055b2011-02-07 17:24:26 +01001074 pi->vnr = 0;
Philipp Reisner02918be2010-08-20 14:35:10 +02001075 } else {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001076 conn_err(tconn, "Wrong magic value 0x%08x in protocol version %d\n",
1077 be32_to_cpu(*(__be32 *)header),
1078 tconn->agreed_pro_version);
Andreas Gruenbacher8172f3e2011-03-16 17:22:39 +01001079 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001080 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001081 pi->data = header + header_size;
Andreas Gruenbacher8172f3e2011-03-16 17:22:39 +01001082 return 0;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001083}
1084
Philipp Reisner9ba7aa02011-02-07 17:32:41 +01001085static int drbd_recv_header(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner257d0af2011-01-26 12:15:29 +01001086{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001087 void *buffer = tconn->data.rbuf;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001088 int err;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001089
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001090 err = drbd_recv_all_warn(tconn, buffer, drbd_header_size(tconn));
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001091 if (err)
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001092 return err;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001093
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001094 err = decode_header(tconn, buffer, pi);
Philipp Reisner9ba7aa02011-02-07 17:32:41 +01001095 tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001096
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001097 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001098}
1099
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001100static void drbd_flush(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001101{
1102 int rv;
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001103 struct drbd_conf *mdev;
1104 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001105
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001106 if (tconn->write_ordering >= WO_bdev_flush) {
Lars Ellenberg615e0872011-11-17 14:32:12 +01001107 rcu_read_lock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001108 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Lars Ellenberg615e0872011-11-17 14:32:12 +01001109 if (!get_ldev(mdev))
1110 continue;
1111 kref_get(&mdev->kref);
1112 rcu_read_unlock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001113
Lars Ellenberg615e0872011-11-17 14:32:12 +01001114 rv = blkdev_issue_flush(mdev->ldev->backing_bdev,
1115 GFP_NOIO, NULL);
1116 if (rv) {
1117 dev_info(DEV, "local disk flush failed with status %d\n", rv);
1118 /* would rather check on EOPNOTSUPP, but that is not reliable.
1119 * don't try again for ANY return value != 0
1120 * if (rv == -EOPNOTSUPP) */
1121 drbd_bump_write_ordering(tconn, WO_drain_io);
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001122 }
Lars Ellenberg615e0872011-11-17 14:32:12 +01001123 put_ldev(mdev);
1124 kref_put(&mdev->kref, &drbd_minor_destroy);
1125
1126 rcu_read_lock();
1127 if (rv)
1128 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001129 }
Lars Ellenberg615e0872011-11-17 14:32:12 +01001130 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07001131 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001132}
1133
1134/**
1135 * drbd_may_finish_epoch() - Applies an epoch_event to the epoch's state, eventually finishes it.
1136 * @mdev: DRBD device.
1137 * @epoch: Epoch object.
1138 * @ev: Epoch event.
1139 */
Philipp Reisner1e9dd292011-11-10 15:14:53 +01001140static enum finish_epoch drbd_may_finish_epoch(struct drbd_tconn *tconn,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001141 struct drbd_epoch *epoch,
1142 enum epoch_event ev)
1143{
Philipp Reisner2451fc32010-08-24 13:43:11 +02001144 int epoch_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001145 struct drbd_epoch *next_epoch;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001146 enum finish_epoch rv = FE_STILL_LIVE;
1147
Philipp Reisner12038a32011-11-09 19:18:00 +01001148 spin_lock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001149 do {
1150 next_epoch = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001151
1152 epoch_size = atomic_read(&epoch->epoch_size);
1153
1154 switch (ev & ~EV_CLEANUP) {
1155 case EV_PUT:
1156 atomic_dec(&epoch->active);
1157 break;
1158 case EV_GOT_BARRIER_NR:
1159 set_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001160 break;
1161 case EV_BECAME_LAST:
1162 /* nothing to do*/
1163 break;
1164 }
1165
Philipp Reisnerb411b362009-09-25 16:07:19 -07001166 if (epoch_size != 0 &&
1167 atomic_read(&epoch->active) == 0 &&
Philipp Reisner85d735132011-07-18 15:45:15 +02001168 (test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags) || ev & EV_CLEANUP)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001169 if (!(ev & EV_CLEANUP)) {
Philipp Reisner12038a32011-11-09 19:18:00 +01001170 spin_unlock(&tconn->epoch_lock);
Philipp Reisner1d2783d2011-11-10 14:56:07 +01001171 drbd_send_b_ack(epoch->mdev, epoch->barrier_nr, epoch_size);
Philipp Reisner12038a32011-11-09 19:18:00 +01001172 spin_lock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001173 }
Philipp Reisner85d735132011-07-18 15:45:15 +02001174 if (test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags))
Philipp Reisner1d2783d2011-11-10 14:56:07 +01001175 dec_unacked(epoch->mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001176
Philipp Reisner12038a32011-11-09 19:18:00 +01001177 if (tconn->current_epoch != epoch) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001178 next_epoch = list_entry(epoch->list.next, struct drbd_epoch, list);
1179 list_del(&epoch->list);
1180 ev = EV_BECAME_LAST | (ev & EV_CLEANUP);
Philipp Reisner12038a32011-11-09 19:18:00 +01001181 tconn->epochs--;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001182 kfree(epoch);
1183
1184 if (rv == FE_STILL_LIVE)
1185 rv = FE_DESTROYED;
1186 } else {
1187 epoch->flags = 0;
1188 atomic_set(&epoch->epoch_size, 0);
Uwe Kleine-König698f9312010-07-02 20:41:51 +02001189 /* atomic_set(&epoch->active, 0); is already zero */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001190 if (rv == FE_STILL_LIVE)
1191 rv = FE_RECYCLED;
1192 }
1193 }
1194
1195 if (!next_epoch)
1196 break;
1197
1198 epoch = next_epoch;
1199 } while (1);
1200
Philipp Reisner12038a32011-11-09 19:18:00 +01001201 spin_unlock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001202
Philipp Reisnerb411b362009-09-25 16:07:19 -07001203 return rv;
1204}
1205
1206/**
1207 * drbd_bump_write_ordering() - Fall back to an other write ordering method
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001208 * @tconn: DRBD connection.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001209 * @wo: Write ordering method to try.
1210 */
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001211void drbd_bump_write_ordering(struct drbd_tconn *tconn, enum write_ordering_e wo)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001212{
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001213 struct disk_conf *dc;
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001214 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001215 enum write_ordering_e pwo;
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001216 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001217 static char *write_ordering_str[] = {
1218 [WO_none] = "none",
1219 [WO_drain_io] = "drain",
1220 [WO_bdev_flush] = "flush",
Philipp Reisnerb411b362009-09-25 16:07:19 -07001221 };
1222
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001223 pwo = tconn->write_ordering;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001224 wo = min(pwo, wo);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001225 rcu_read_lock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001226 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1227 if (!get_ldev(mdev))
1228 continue;
1229 dc = rcu_dereference(mdev->ldev->disk_conf);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001230
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001231 if (wo == WO_bdev_flush && !dc->disk_flushes)
1232 wo = WO_drain_io;
1233 if (wo == WO_drain_io && !dc->disk_drain)
1234 wo = WO_none;
1235 put_ldev(mdev);
1236 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001237 rcu_read_unlock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001238 tconn->write_ordering = wo;
1239 if (pwo != tconn->write_ordering || wo == WO_bdev_flush)
1240 conn_info(tconn, "Method to ensure write ordering: %s\n", write_ordering_str[tconn->write_ordering]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001241}
1242
1243/**
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001244 * drbd_submit_peer_request()
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001245 * @mdev: DRBD device.
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001246 * @peer_req: peer request
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001247 * @rw: flag field, see bio->bi_rw
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001248 *
1249 * May spread the pages to multiple bios,
1250 * depending on bio_add_page restrictions.
1251 *
1252 * Returns 0 if all bios have been submitted,
1253 * -ENOMEM if we could not allocate enough bios,
1254 * -ENOSPC (any better suggestion?) if we have not been able to bio_add_page a
1255 * single page to an empty bio (which should never happen and likely indicates
1256 * that the lower level IO stack is in some way broken). This has been observed
1257 * on certain Xen deployments.
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001258 */
1259/* TODO allocate from our own bio_set. */
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001260int drbd_submit_peer_request(struct drbd_conf *mdev,
1261 struct drbd_peer_request *peer_req,
1262 const unsigned rw, const int fault_type)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001263{
1264 struct bio *bios = NULL;
1265 struct bio *bio;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001266 struct page *page = peer_req->pages;
1267 sector_t sector = peer_req->i.sector;
1268 unsigned ds = peer_req->i.size;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001269 unsigned n_bios = 0;
1270 unsigned nr_pages = (ds + PAGE_SIZE -1) >> PAGE_SHIFT;
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001271 int err = -ENOMEM;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001272
1273 /* In most cases, we will only need one bio. But in case the lower
1274 * level restrictions happen to be different at this offset on this
1275 * side than those of the sending peer, we may need to submit the
Lars Ellenbergda4a75d2011-02-23 17:02:01 +01001276 * request in more than one bio.
1277 *
1278 * Plain bio_alloc is good enough here, this is no DRBD internally
1279 * generated bio, but a bio allocated on behalf of the peer.
1280 */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001281next_bio:
1282 bio = bio_alloc(GFP_NOIO, nr_pages);
1283 if (!bio) {
1284 dev_err(DEV, "submit_ee: Allocation of a bio failed\n");
1285 goto fail;
1286 }
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001287 /* > peer_req->i.sector, unless this is the first bio */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001288 bio->bi_sector = sector;
1289 bio->bi_bdev = mdev->ldev->backing_bdev;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001290 bio->bi_rw = rw;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001291 bio->bi_private = peer_req;
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01001292 bio->bi_end_io = drbd_peer_request_endio;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001293
1294 bio->bi_next = bios;
1295 bios = bio;
1296 ++n_bios;
1297
1298 page_chain_for_each(page) {
1299 unsigned len = min_t(unsigned, ds, PAGE_SIZE);
1300 if (!bio_add_page(bio, page, len, 0)) {
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001301 /* A single page must always be possible!
1302 * But in case it fails anyways,
1303 * we deal with it, and complain (below). */
1304 if (bio->bi_vcnt == 0) {
1305 dev_err(DEV,
1306 "bio_add_page failed for len=%u, "
1307 "bi_vcnt=0 (bi_sector=%llu)\n",
1308 len, (unsigned long long)bio->bi_sector);
1309 err = -ENOSPC;
1310 goto fail;
1311 }
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001312 goto next_bio;
1313 }
1314 ds -= len;
1315 sector += len >> 9;
1316 --nr_pages;
1317 }
1318 D_ASSERT(page == NULL);
1319 D_ASSERT(ds == 0);
1320
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001321 atomic_set(&peer_req->pending_bios, n_bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001322 do {
1323 bio = bios;
1324 bios = bios->bi_next;
1325 bio->bi_next = NULL;
1326
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001327 drbd_generic_make_request(mdev, fault_type, bio);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001328 } while (bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001329 return 0;
1330
1331fail:
1332 while (bios) {
1333 bio = bios;
1334 bios = bios->bi_next;
1335 bio_put(bio);
1336 }
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001337 return err;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001338}
1339
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001340static void drbd_remove_epoch_entry_interval(struct drbd_conf *mdev,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001341 struct drbd_peer_request *peer_req)
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001342{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001343 struct drbd_interval *i = &peer_req->i;
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001344
1345 drbd_remove_interval(&mdev->write_requests, i);
1346 drbd_clear_interval(i);
1347
Andreas Gruenbacher6c852be2011-02-04 15:38:52 +01001348 /* Wake up any processes waiting for this peer request to complete. */
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001349 if (i->waiting)
1350 wake_up(&mdev->misc_wait);
1351}
1352
Philipp Reisner77fede52011-11-10 21:19:11 +01001353void conn_wait_active_ee_empty(struct drbd_tconn *tconn)
1354{
1355 struct drbd_conf *mdev;
1356 int vnr;
1357
1358 rcu_read_lock();
1359 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1360 kref_get(&mdev->kref);
1361 rcu_read_unlock();
1362 drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
1363 kref_put(&mdev->kref, &drbd_minor_destroy);
1364 rcu_read_lock();
1365 }
1366 rcu_read_unlock();
1367}
1368
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001369static int receive_Barrier(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001370{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001371 struct drbd_conf *mdev;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001372 int rv;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001373 struct p_barrier *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001374 struct drbd_epoch *epoch;
1375
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001376 mdev = vnr_to_mdev(tconn, pi->vnr);
1377 if (!mdev)
1378 return -EIO;
1379
Philipp Reisnerb411b362009-09-25 16:07:19 -07001380 inc_unacked(mdev);
1381
Philipp Reisner12038a32011-11-09 19:18:00 +01001382 tconn->current_epoch->barrier_nr = p->barrier;
1383 tconn->current_epoch->mdev = mdev;
Philipp Reisner1e9dd292011-11-10 15:14:53 +01001384 rv = drbd_may_finish_epoch(tconn, tconn->current_epoch, EV_GOT_BARRIER_NR);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001385
1386 /* P_BARRIER_ACK may imply that the corresponding extent is dropped from
1387 * the activity log, which means it would not be resynced in case the
1388 * R_PRIMARY crashes now.
1389 * Therefore we must send the barrier_ack after the barrier request was
1390 * completed. */
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001391 switch (tconn->write_ordering) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001392 case WO_none:
1393 if (rv == FE_RECYCLED)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001394 return 0;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001395
1396 /* receiver context, in the writeout path of the other node.
1397 * avoid potential distributed deadlock */
1398 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1399 if (epoch)
1400 break;
1401 else
1402 dev_warn(DEV, "Allocation of an epoch failed, slowing down\n");
1403 /* Fall through */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001404
1405 case WO_bdev_flush:
1406 case WO_drain_io:
Philipp Reisner77fede52011-11-10 21:19:11 +01001407 conn_wait_active_ee_empty(tconn);
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001408 drbd_flush(tconn);
Philipp Reisner2451fc32010-08-24 13:43:11 +02001409
Philipp Reisner12038a32011-11-09 19:18:00 +01001410 if (atomic_read(&tconn->current_epoch->epoch_size)) {
Philipp Reisner2451fc32010-08-24 13:43:11 +02001411 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1412 if (epoch)
1413 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001414 }
1415
Philipp Reisner12038a32011-11-09 19:18:00 +01001416 epoch = tconn->current_epoch;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001417 wait_event(mdev->ee_wait, atomic_read(&epoch->epoch_size) == 0);
1418
1419 D_ASSERT(atomic_read(&epoch->active) == 0);
1420 D_ASSERT(epoch->flags == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001421
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001422 return 0;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001423 default:
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001424 dev_err(DEV, "Strangeness in tconn->write_ordering %d\n", tconn->write_ordering);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001425 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001426 }
1427
1428 epoch->flags = 0;
1429 atomic_set(&epoch->epoch_size, 0);
1430 atomic_set(&epoch->active, 0);
1431
Philipp Reisner12038a32011-11-09 19:18:00 +01001432 spin_lock(&tconn->epoch_lock);
1433 if (atomic_read(&tconn->current_epoch->epoch_size)) {
1434 list_add(&epoch->list, &tconn->current_epoch->list);
1435 tconn->current_epoch = epoch;
1436 tconn->epochs++;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001437 } else {
1438 /* The current_epoch got recycled while we allocated this one... */
1439 kfree(epoch);
1440 }
Philipp Reisner12038a32011-11-09 19:18:00 +01001441 spin_unlock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001442
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001443 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001444}
1445
1446/* used from receive_RSDataReply (recv_resync_read)
1447 * and from receive_Data */
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001448static struct drbd_peer_request *
1449read_in_block(struct drbd_conf *mdev, u64 id, sector_t sector,
1450 int data_size) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001451{
Lars Ellenberg66660322010-04-06 12:15:04 +02001452 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001453 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001454 struct page *page;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001455 int dgs, ds, err;
Philipp Reisnera0638452011-01-19 14:31:32 +01001456 void *dig_in = mdev->tconn->int_dig_in;
1457 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001458 unsigned long *data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001459
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001460 dgs = 0;
1461 if (mdev->tconn->peer_integrity_tfm) {
1462 dgs = crypto_hash_digestsize(mdev->tconn->peer_integrity_tfm);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001463 /*
1464 * FIXME: Receive the incoming digest into the receive buffer
1465 * here, together with its struct p_data?
1466 */
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001467 err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs);
1468 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001469 return NULL;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001470 data_size -= dgs;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001471 }
1472
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001473 if (!expect(data_size != 0))
1474 return NULL;
1475 if (!expect(IS_ALIGNED(data_size, 512)))
1476 return NULL;
1477 if (!expect(data_size <= DRBD_MAX_BIO_SIZE))
1478 return NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001479
Lars Ellenberg66660322010-04-06 12:15:04 +02001480 /* even though we trust out peer,
1481 * we sometimes have to double check. */
1482 if (sector + (data_size>>9) > capacity) {
Lars Ellenbergfdda6542011-01-24 15:11:01 +01001483 dev_err(DEV, "request from peer beyond end of local disk: "
1484 "capacity: %llus < sector: %llus + size: %u\n",
Lars Ellenberg66660322010-04-06 12:15:04 +02001485 (unsigned long long)capacity,
1486 (unsigned long long)sector, data_size);
1487 return NULL;
1488 }
1489
Philipp Reisnerb411b362009-09-25 16:07:19 -07001490 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
1491 * "criss-cross" setup, that might cause write-out on some other DRBD,
1492 * which in turn might block on the other node at this very place. */
Andreas Gruenbacher0db55362011-04-06 16:09:15 +02001493 peer_req = drbd_alloc_peer_req(mdev, id, sector, data_size, GFP_NOIO);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001494 if (!peer_req)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001495 return NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001496
Philipp Reisnerb411b362009-09-25 16:07:19 -07001497 ds = data_size;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001498 page = peer_req->pages;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001499 page_chain_for_each(page) {
1500 unsigned len = min_t(int, ds, PAGE_SIZE);
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001501 data = kmap(page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001502 err = drbd_recv_all_warn(mdev->tconn, data, len);
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +01001503 if (drbd_insert_fault(mdev, DRBD_FAULT_RECEIVE)) {
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001504 dev_err(DEV, "Fault injection: Corrupting data on receive\n");
1505 data[0] = data[0] ^ (unsigned long)-1;
1506 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001507 kunmap(page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001508 if (err) {
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001509 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001510 return NULL;
1511 }
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001512 ds -= len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001513 }
1514
1515 if (dgs) {
Andreas Gruenbacher5b614ab2011-04-27 21:00:12 +02001516 drbd_csum_ee(mdev, mdev->tconn->peer_integrity_tfm, peer_req, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001517 if (memcmp(dig_in, dig_vv, dgs)) {
Lars Ellenberg470be442010-11-10 10:36:52 +01001518 dev_err(DEV, "Digest integrity check FAILED: %llus +%u\n",
1519 (unsigned long long)sector, data_size);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001520 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001521 return NULL;
1522 }
1523 }
1524 mdev->recv_cnt += data_size>>9;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001525 return peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001526}
1527
1528/* drbd_drain_block() just takes a data block
1529 * out of the socket input buffer, and discards it.
1530 */
1531static int drbd_drain_block(struct drbd_conf *mdev, int data_size)
1532{
1533 struct page *page;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001534 int err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001535 void *data;
1536
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001537 if (!data_size)
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001538 return 0;
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001539
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +02001540 page = drbd_alloc_pages(mdev, 1, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001541
1542 data = kmap(page);
1543 while (data_size) {
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001544 unsigned int len = min_t(int, data_size, PAGE_SIZE);
1545
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001546 err = drbd_recv_all_warn(mdev->tconn, data, len);
1547 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001548 break;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001549 data_size -= len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001550 }
1551 kunmap(page);
Andreas Gruenbacher5cc287e2011-04-07 21:02:59 +02001552 drbd_free_pages(mdev, page, 0);
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001553 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001554}
1555
1556static int recv_dless_read(struct drbd_conf *mdev, struct drbd_request *req,
1557 sector_t sector, int data_size)
1558{
1559 struct bio_vec *bvec;
1560 struct bio *bio;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001561 int dgs, err, i, expect;
Philipp Reisnera0638452011-01-19 14:31:32 +01001562 void *dig_in = mdev->tconn->int_dig_in;
1563 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001564
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001565 dgs = 0;
1566 if (mdev->tconn->peer_integrity_tfm) {
1567 dgs = crypto_hash_digestsize(mdev->tconn->peer_integrity_tfm);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001568 err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs);
1569 if (err)
1570 return err;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001571 data_size -= dgs;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001572 }
1573
Philipp Reisnerb411b362009-09-25 16:07:19 -07001574 /* optimistically update recv_cnt. if receiving fails below,
1575 * we disconnect anyways, and counters will be reset. */
1576 mdev->recv_cnt += data_size>>9;
1577
1578 bio = req->master_bio;
1579 D_ASSERT(sector == bio->bi_sector);
1580
1581 bio_for_each_segment(bvec, bio, i) {
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001582 void *mapped = kmap(bvec->bv_page) + bvec->bv_offset;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001583 expect = min_t(int, data_size, bvec->bv_len);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001584 err = drbd_recv_all_warn(mdev->tconn, mapped, expect);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001585 kunmap(bvec->bv_page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001586 if (err)
1587 return err;
1588 data_size -= expect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001589 }
1590
1591 if (dgs) {
Andreas Gruenbacher5b614ab2011-04-27 21:00:12 +02001592 drbd_csum_bio(mdev, mdev->tconn->peer_integrity_tfm, bio, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001593 if (memcmp(dig_in, dig_vv, dgs)) {
1594 dev_err(DEV, "Digest integrity check FAILED. Broken NICs?\n");
Andreas Gruenbacher28284ce2011-03-16 17:54:02 +01001595 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001596 }
1597 }
1598
1599 D_ASSERT(data_size == 0);
Andreas Gruenbacher28284ce2011-03-16 17:54:02 +01001600 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001601}
1602
Andreas Gruenbachera990be42011-04-06 17:56:48 +02001603/*
1604 * e_end_resync_block() is called in asender context via
1605 * drbd_finish_peer_reqs().
1606 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001607static int e_end_resync_block(struct drbd_work *w, int unused)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001608{
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001609 struct drbd_peer_request *peer_req =
1610 container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001611 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001612 sector_t sector = peer_req->i.sector;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001613 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001614
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001615 D_ASSERT(drbd_interval_empty(&peer_req->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001616
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001617 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
1618 drbd_set_in_sync(mdev, sector, peer_req->i.size);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001619 err = drbd_send_ack(mdev, P_RS_WRITE_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001620 } else {
1621 /* Record failure to sync */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001622 drbd_rs_failed_io(mdev, sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001623
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001624 err = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001625 }
1626 dec_unacked(mdev);
1627
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001628 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001629}
1630
1631static int recv_resync_read(struct drbd_conf *mdev, sector_t sector, int data_size) __releases(local)
1632{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001633 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001634
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001635 peer_req = read_in_block(mdev, ID_SYNCER, sector, data_size);
1636 if (!peer_req)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001637 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001638
1639 dec_rs_pending(mdev);
1640
Philipp Reisnerb411b362009-09-25 16:07:19 -07001641 inc_unacked(mdev);
1642 /* corresponding dec_unacked() in e_end_resync_block()
1643 * respective _drbd_clear_done_ee */
1644
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001645 peer_req->w.cb = e_end_resync_block;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001646
Philipp Reisner87eeee42011-01-19 14:16:30 +01001647 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001648 list_add(&peer_req->w.list, &mdev->sync_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001649 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001650
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001651 atomic_add(data_size >> 9, &mdev->rs_sect_ev);
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001652 if (drbd_submit_peer_request(mdev, peer_req, WRITE, DRBD_FAULT_RS_WR) == 0)
Andreas Gruenbachere1c1b0f2011-03-16 17:58:27 +01001653 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001654
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001655 /* don't care for the reason here */
1656 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01001657 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001658 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001659 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02001660
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001661 drbd_free_peer_req(mdev, peer_req);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001662fail:
1663 put_ldev(mdev);
Andreas Gruenbachere1c1b0f2011-03-16 17:58:27 +01001664 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001665}
1666
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001667static struct drbd_request *
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001668find_request(struct drbd_conf *mdev, struct rb_root *root, u64 id,
1669 sector_t sector, bool missing_ok, const char *func)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001670{
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001671 struct drbd_request *req;
1672
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001673 /* Request object according to our peer */
1674 req = (struct drbd_request *)(unsigned long)id;
Andreas Gruenbacher5e472262011-01-27 14:42:51 +01001675 if (drbd_contains_interval(root, sector, &req->i) && req->i.local)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001676 return req;
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001677 if (!missing_ok) {
Andreas Gruenbacher5af172e2011-07-15 09:43:23 +02001678 dev_err(DEV, "%s: failed to find request 0x%lx, sector %llus\n", func,
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001679 (unsigned long)id, (unsigned long long)sector);
1680 }
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001681 return NULL;
1682}
1683
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001684static int receive_DataReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001685{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001686 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001687 struct drbd_request *req;
1688 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
Philipp Reisner87eeee42011-01-19 14:16:30 +01001698 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001699 req = find_request(mdev, &mdev->read_requests, p->block_id, sector, false, __func__);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001700 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001701 if (unlikely(!req))
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001702 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001703
Bart Van Assche24c48302011-05-21 18:32:29 +02001704 /* hlist_del(&req->collision) is done in _req_may_be_done, to avoid
Philipp Reisnerb411b362009-09-25 16:07:19 -07001705 * special casing it there for the various failure cases.
1706 * still no race with drbd_fail_pending_reads */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001707 err = recv_dless_read(mdev, req, sector, pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001708 if (!err)
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001709 req_mod(req, DATA_RECEIVED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001710 /* else: nothing. handled from drbd_disconnect...
1711 * I don't think we may complete this just yet
1712 * in case we are "on-disconnect: freeze" */
1713
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001714 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001715}
1716
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001717static int receive_RSDataReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001718{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001719 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001720 sector_t sector;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001721 int err;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001722 struct p_data *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001723
1724 mdev = vnr_to_mdev(tconn, pi->vnr);
1725 if (!mdev)
1726 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001727
1728 sector = be64_to_cpu(p->sector);
1729 D_ASSERT(p->block_id == ID_SYNCER);
1730
1731 if (get_ldev(mdev)) {
1732 /* data is submitted to disk within recv_resync_read.
1733 * corresponding put_ldev done below on error,
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01001734 * or in drbd_peer_request_endio. */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001735 err = recv_resync_read(mdev, sector, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001736 } else {
1737 if (__ratelimit(&drbd_ratelimit_state))
1738 dev_err(DEV, "Can not write resync data to local disk.\n");
1739
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001740 err = drbd_drain_block(mdev, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001741
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001742 drbd_send_ack_dp(mdev, P_NEG_ACK, p, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001743 }
1744
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001745 atomic_add(pi->size >> 9, &mdev->rs_sect_in);
Philipp Reisner778f2712010-07-06 11:14:00 +02001746
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001747 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001748}
1749
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001750static int w_restart_write(struct drbd_work *w, int cancel)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001751{
1752 struct drbd_request *req = container_of(w, struct drbd_request, w);
1753 struct drbd_conf *mdev = w->mdev;
1754 struct bio *bio;
1755 unsigned long start_time;
1756 unsigned long flags;
1757
1758 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
1759 if (!expect(req->rq_state & RQ_POSTPONED)) {
1760 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001761 return -EIO;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001762 }
1763 bio = req->master_bio;
1764 start_time = req->start_time;
1765 /* Postponed requests will not have their master_bio completed! */
1766 __req_mod(req, DISCARD_WRITE, NULL);
1767 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
1768
1769 while (__drbd_make_request(mdev, bio, start_time))
1770 /* retry */ ;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001771 return 0;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001772}
1773
1774static void restart_conflicting_writes(struct drbd_conf *mdev,
1775 sector_t sector, int size)
1776{
1777 struct drbd_interval *i;
1778 struct drbd_request *req;
1779
1780 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
1781 if (!i->local)
1782 continue;
1783 req = container_of(i, struct drbd_request, i);
1784 if (req->rq_state & RQ_LOCAL_PENDING ||
1785 !(req->rq_state & RQ_POSTPONED))
1786 continue;
1787 if (expect(list_empty(&req->w.list))) {
1788 req->w.mdev = mdev;
1789 req->w.cb = w_restart_write;
1790 drbd_queue_work(&mdev->tconn->data.work, &req->w);
1791 }
1792 }
1793}
1794
Andreas Gruenbachera990be42011-04-06 17:56:48 +02001795/*
1796 * e_end_block() is called in asender context via drbd_finish_peer_reqs().
Philipp Reisnerb411b362009-09-25 16:07:19 -07001797 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001798static int e_end_block(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001799{
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001800 struct drbd_peer_request *peer_req =
1801 container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001802 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001803 sector_t sector = peer_req->i.sector;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001804 int err = 0, pcmd;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001805
Philipp Reisner303d1442011-04-13 16:24:47 -07001806 if (peer_req->flags & EE_SEND_WRITE_ACK) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001807 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001808 pcmd = (mdev->state.conn >= C_SYNC_SOURCE &&
1809 mdev->state.conn <= C_PAUSED_SYNC_T &&
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001810 peer_req->flags & EE_MAY_SET_IN_SYNC) ?
Philipp Reisnerb411b362009-09-25 16:07:19 -07001811 P_RS_WRITE_ACK : P_WRITE_ACK;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001812 err = drbd_send_ack(mdev, pcmd, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001813 if (pcmd == P_RS_WRITE_ACK)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001814 drbd_set_in_sync(mdev, sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001815 } else {
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001816 err = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001817 /* we expect it to be marked out of sync anyways...
1818 * maybe assert this? */
1819 }
1820 dec_unacked(mdev);
1821 }
1822 /* we delete from the conflict detection hash _after_ we sent out the
1823 * P_WRITE_ACK / P_NEG_ACK, to get the sequence number right. */
Philipp Reisner302bdea2011-04-21 11:36:49 +02001824 if (peer_req->flags & EE_IN_INTERVAL_TREE) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01001825 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001826 D_ASSERT(!drbd_interval_empty(&peer_req->i));
1827 drbd_remove_epoch_entry_interval(mdev, peer_req);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001828 if (peer_req->flags & EE_RESTART_REQUESTS)
1829 restart_conflicting_writes(mdev, sector, peer_req->i.size);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001830 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbb3bfe92011-01-21 15:59:23 +01001831 } else
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001832 D_ASSERT(drbd_interval_empty(&peer_req->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001833
Philipp Reisner1e9dd292011-11-10 15:14:53 +01001834 drbd_may_finish_epoch(mdev->tconn, peer_req->epoch, EV_PUT + (cancel ? EV_CLEANUP : 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001835
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001836 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001837}
1838
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001839static int e_send_ack(struct drbd_work *w, enum drbd_packet ack)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001840{
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001841 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001842 struct drbd_peer_request *peer_req =
1843 container_of(w, struct drbd_peer_request, w);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001844 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001845
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001846 err = drbd_send_ack(mdev, ack, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001847 dec_unacked(mdev);
1848
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001849 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001850}
1851
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001852static int e_send_discard_write(struct drbd_work *w, int unused)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001853{
1854 return e_send_ack(w, P_DISCARD_WRITE);
1855}
1856
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001857static int e_send_retry_write(struct drbd_work *w, int unused)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001858{
1859 struct drbd_tconn *tconn = w->mdev->tconn;
1860
1861 return e_send_ack(w, tconn->agreed_pro_version >= 100 ?
1862 P_RETRY_WRITE : P_DISCARD_WRITE);
1863}
1864
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001865static bool seq_greater(u32 a, u32 b)
1866{
1867 /*
1868 * We assume 32-bit wrap-around here.
1869 * For 24-bit wrap-around, we would have to shift:
1870 * a <<= 8; b <<= 8;
1871 */
1872 return (s32)a - (s32)b > 0;
1873}
1874
1875static u32 seq_max(u32 a, u32 b)
1876{
1877 return seq_greater(a, b) ? a : b;
1878}
1879
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001880static bool need_peer_seq(struct drbd_conf *mdev)
1881{
1882 struct drbd_tconn *tconn = mdev->tconn;
Philipp Reisner302bdea2011-04-21 11:36:49 +02001883 int tp;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001884
1885 /*
1886 * We only need to keep track of the last packet_seq number of our peer
1887 * if we are in dual-primary mode and we have the discard flag set; see
1888 * handle_write_conflicts().
1889 */
Philipp Reisner302bdea2011-04-21 11:36:49 +02001890
1891 rcu_read_lock();
1892 tp = rcu_dereference(mdev->tconn->net_conf)->two_primaries;
1893 rcu_read_unlock();
1894
1895 return tp && test_bit(DISCARD_CONCURRENT, &tconn->flags);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001896}
1897
Andreas Gruenbacher43ae0772011-02-03 18:42:08 +01001898static void update_peer_seq(struct drbd_conf *mdev, unsigned int peer_seq)
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001899{
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001900 unsigned int newest_peer_seq;
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001901
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001902 if (need_peer_seq(mdev)) {
1903 spin_lock(&mdev->peer_seq_lock);
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001904 newest_peer_seq = seq_max(mdev->peer_seq, peer_seq);
1905 mdev->peer_seq = newest_peer_seq;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001906 spin_unlock(&mdev->peer_seq_lock);
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001907 /* wake up only if we actually changed mdev->peer_seq */
1908 if (peer_seq == newest_peer_seq)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001909 wake_up(&mdev->seq_wait);
1910 }
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001911}
1912
Philipp Reisnerb411b362009-09-25 16:07:19 -07001913/* Called from receive_Data.
1914 * Synchronize packets on sock with packets on msock.
1915 *
1916 * This is here so even when a P_DATA packet traveling via sock overtook an Ack
1917 * packet traveling on msock, they are still processed in the order they have
1918 * been sent.
1919 *
1920 * Note: we don't care for Ack packets overtaking P_DATA packets.
1921 *
1922 * In case packet_seq is larger than mdev->peer_seq number, there are
1923 * outstanding packets on the msock. We wait for them to arrive.
1924 * In case we are the logically next packet, we update mdev->peer_seq
1925 * ourselves. Correctly handles 32bit wrap around.
1926 *
1927 * Assume we have a 10 GBit connection, that is about 1<<30 byte per second,
1928 * about 1<<21 sectors per second. So "worst" case, we have 1<<3 == 8 seconds
1929 * for the 24bit wrap (historical atomic_t guarantee on some archs), and we have
1930 * 1<<9 == 512 seconds aka ages for the 32bit wrap around...
1931 *
1932 * returns 0 if we may process the packet,
1933 * -ERESTARTSYS if we were interrupted (by disconnect signal). */
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001934static int wait_for_and_update_peer_seq(struct drbd_conf *mdev, const u32 peer_seq)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001935{
1936 DEFINE_WAIT(wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001937 long timeout;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001938 int ret;
1939
1940 if (!need_peer_seq(mdev))
1941 return 0;
1942
Philipp Reisnerb411b362009-09-25 16:07:19 -07001943 spin_lock(&mdev->peer_seq_lock);
1944 for (;;) {
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001945 if (!seq_greater(peer_seq - 1, mdev->peer_seq)) {
1946 mdev->peer_seq = seq_max(mdev->peer_seq, peer_seq);
1947 ret = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001948 break;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001949 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001950 if (signal_pending(current)) {
1951 ret = -ERESTARTSYS;
1952 break;
1953 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001954 prepare_to_wait(&mdev->seq_wait, &wait, TASK_INTERRUPTIBLE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001955 spin_unlock(&mdev->peer_seq_lock);
Philipp Reisner44ed1672011-04-19 17:10:19 +02001956 rcu_read_lock();
1957 timeout = rcu_dereference(mdev->tconn->net_conf)->ping_timeo*HZ/10;
1958 rcu_read_unlock();
Andreas Gruenbacher71b1c1e2011-03-01 15:40:43 +01001959 timeout = schedule_timeout(timeout);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001960 spin_lock(&mdev->peer_seq_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001961 if (!timeout) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001962 ret = -ETIMEDOUT;
Andreas Gruenbacher71b1c1e2011-03-01 15:40:43 +01001963 dev_err(DEV, "Timed out waiting for missing ack packets; disconnecting\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001964 break;
1965 }
1966 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001967 spin_unlock(&mdev->peer_seq_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001968 finish_wait(&mdev->seq_wait, &wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001969 return ret;
1970}
1971
Lars Ellenberg688593c2010-11-17 22:25:03 +01001972/* see also bio_flags_to_wire()
1973 * DRBD_REQ_*, because we need to semantically map the flags to data packet
1974 * flags and back. We may replicate to other kernel versions. */
1975static unsigned long wire_flags_to_bio(struct drbd_conf *mdev, u32 dpf)
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001976{
Lars Ellenberg688593c2010-11-17 22:25:03 +01001977 return (dpf & DP_RW_SYNC ? REQ_SYNC : 0) |
1978 (dpf & DP_FUA ? REQ_FUA : 0) |
1979 (dpf & DP_FLUSH ? REQ_FLUSH : 0) |
1980 (dpf & DP_DISCARD ? REQ_DISCARD : 0);
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001981}
1982
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001983static void fail_postponed_requests(struct drbd_conf *mdev, sector_t sector,
1984 unsigned int size)
1985{
1986 struct drbd_interval *i;
1987
1988 repeat:
1989 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
1990 struct drbd_request *req;
1991 struct bio_and_error m;
1992
1993 if (!i->local)
1994 continue;
1995 req = container_of(i, struct drbd_request, i);
1996 if (!(req->rq_state & RQ_POSTPONED))
1997 continue;
1998 req->rq_state &= ~RQ_POSTPONED;
1999 __req_mod(req, NEG_ACKED, &m);
2000 spin_unlock_irq(&mdev->tconn->req_lock);
2001 if (m.bio)
2002 complete_master_bio(mdev, &m);
2003 spin_lock_irq(&mdev->tconn->req_lock);
2004 goto repeat;
2005 }
2006}
2007
2008static int handle_write_conflicts(struct drbd_conf *mdev,
2009 struct drbd_peer_request *peer_req)
2010{
2011 struct drbd_tconn *tconn = mdev->tconn;
2012 bool resolve_conflicts = test_bit(DISCARD_CONCURRENT, &tconn->flags);
2013 sector_t sector = peer_req->i.sector;
2014 const unsigned int size = peer_req->i.size;
2015 struct drbd_interval *i;
2016 bool equal;
2017 int err;
2018
2019 /*
2020 * Inserting the peer request into the write_requests tree will prevent
2021 * new conflicting local requests from being added.
2022 */
2023 drbd_insert_interval(&mdev->write_requests, &peer_req->i);
2024
2025 repeat:
2026 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
2027 if (i == &peer_req->i)
2028 continue;
2029
2030 if (!i->local) {
2031 /*
2032 * Our peer has sent a conflicting remote request; this
2033 * should not happen in a two-node setup. Wait for the
2034 * earlier peer request to complete.
2035 */
2036 err = drbd_wait_misc(mdev, i);
2037 if (err)
2038 goto out;
2039 goto repeat;
2040 }
2041
2042 equal = i->sector == sector && i->size == size;
2043 if (resolve_conflicts) {
2044 /*
2045 * If the peer request is fully contained within the
2046 * overlapping request, it can be discarded; otherwise,
2047 * it will be retried once all overlapping requests
2048 * have completed.
2049 */
2050 bool discard = i->sector <= sector && i->sector +
2051 (i->size >> 9) >= sector + (size >> 9);
2052
2053 if (!equal)
2054 dev_alert(DEV, "Concurrent writes detected: "
2055 "local=%llus +%u, remote=%llus +%u, "
2056 "assuming %s came first\n",
2057 (unsigned long long)i->sector, i->size,
2058 (unsigned long long)sector, size,
2059 discard ? "local" : "remote");
2060
2061 inc_unacked(mdev);
2062 peer_req->w.cb = discard ? e_send_discard_write :
2063 e_send_retry_write;
2064 list_add_tail(&peer_req->w.list, &mdev->done_ee);
2065 wake_asender(mdev->tconn);
2066
2067 err = -ENOENT;
2068 goto out;
2069 } else {
2070 struct drbd_request *req =
2071 container_of(i, struct drbd_request, i);
2072
2073 if (!equal)
2074 dev_alert(DEV, "Concurrent writes detected: "
2075 "local=%llus +%u, remote=%llus +%u\n",
2076 (unsigned long long)i->sector, i->size,
2077 (unsigned long long)sector, size);
2078
2079 if (req->rq_state & RQ_LOCAL_PENDING ||
2080 !(req->rq_state & RQ_POSTPONED)) {
2081 /*
2082 * Wait for the node with the discard flag to
2083 * decide if this request will be discarded or
2084 * retried. Requests that are discarded will
2085 * disappear from the write_requests tree.
2086 *
2087 * In addition, wait for the conflicting
2088 * request to finish locally before submitting
2089 * the conflicting peer request.
2090 */
2091 err = drbd_wait_misc(mdev, &req->i);
2092 if (err) {
2093 _conn_request_state(mdev->tconn,
2094 NS(conn, C_TIMEOUT),
2095 CS_HARD);
2096 fail_postponed_requests(mdev, sector, size);
2097 goto out;
2098 }
2099 goto repeat;
2100 }
2101 /*
2102 * Remember to restart the conflicting requests after
2103 * the new peer request has completed.
2104 */
2105 peer_req->flags |= EE_RESTART_REQUESTS;
2106 }
2107 }
2108 err = 0;
2109
2110 out:
2111 if (err)
2112 drbd_remove_epoch_entry_interval(mdev, peer_req);
2113 return err;
2114}
2115
Philipp Reisnerb411b362009-09-25 16:07:19 -07002116/* mirrored write */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002117static int receive_Data(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002118{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002119 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002120 sector_t sector;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002121 struct drbd_peer_request *peer_req;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02002122 struct p_data *p = pi->data;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002123 u32 peer_seq = be32_to_cpu(p->seq_num);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002124 int rw = WRITE;
2125 u32 dp_flags;
Philipp Reisner302bdea2011-04-21 11:36:49 +02002126 int err, tp;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002127
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002128 mdev = vnr_to_mdev(tconn, pi->vnr);
2129 if (!mdev)
2130 return -EIO;
2131
Philipp Reisnerb411b362009-09-25 16:07:19 -07002132 if (!get_ldev(mdev)) {
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002133 int err2;
2134
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002135 err = wait_for_and_update_peer_seq(mdev, peer_seq);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002136 drbd_send_ack_dp(mdev, P_NEG_ACK, p, pi->size);
Philipp Reisner12038a32011-11-09 19:18:00 +01002137 atomic_inc(&tconn->current_epoch->epoch_size);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002138 err2 = drbd_drain_block(mdev, pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002139 if (!err)
2140 err = err2;
2141 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002142 }
2143
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01002144 /*
2145 * Corresponding put_ldev done either below (on various errors), or in
2146 * drbd_peer_request_endio, if we successfully submit the data at the
2147 * end of this function.
2148 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002149
2150 sector = be64_to_cpu(p->sector);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002151 peer_req = read_in_block(mdev, p->block_id, sector, pi->size);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002152 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002153 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002154 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002155 }
2156
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002157 peer_req->w.cb = e_end_block;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002158
Lars Ellenberg688593c2010-11-17 22:25:03 +01002159 dp_flags = be32_to_cpu(p->dp_flags);
2160 rw |= wire_flags_to_bio(mdev, dp_flags);
2161
2162 if (dp_flags & DP_MAY_SET_IN_SYNC)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002163 peer_req->flags |= EE_MAY_SET_IN_SYNC;
Lars Ellenberg688593c2010-11-17 22:25:03 +01002164
Philipp Reisner12038a32011-11-09 19:18:00 +01002165 spin_lock(&tconn->epoch_lock);
2166 peer_req->epoch = tconn->current_epoch;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002167 atomic_inc(&peer_req->epoch->epoch_size);
2168 atomic_inc(&peer_req->epoch->active);
Philipp Reisner12038a32011-11-09 19:18:00 +01002169 spin_unlock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002170
Philipp Reisner302bdea2011-04-21 11:36:49 +02002171 rcu_read_lock();
2172 tp = rcu_dereference(mdev->tconn->net_conf)->two_primaries;
2173 rcu_read_unlock();
2174 if (tp) {
2175 peer_req->flags |= EE_IN_INTERVAL_TREE;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002176 err = wait_for_and_update_peer_seq(mdev, peer_seq);
2177 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002178 goto out_interrupted;
Philipp Reisner87eeee42011-01-19 14:16:30 +01002179 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002180 err = handle_write_conflicts(mdev, peer_req);
2181 if (err) {
2182 spin_unlock_irq(&mdev->tconn->req_lock);
2183 if (err == -ENOENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002184 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002185 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002186 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002187 goto out_interrupted;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002188 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002189 } else
2190 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002191 list_add(&peer_req->w.list, &mdev->active_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002192 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002193
Philipp Reisner303d1442011-04-13 16:24:47 -07002194 if (mdev->tconn->agreed_pro_version < 100) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02002195 rcu_read_lock();
2196 switch (rcu_dereference(mdev->tconn->net_conf)->wire_protocol) {
Philipp Reisner303d1442011-04-13 16:24:47 -07002197 case DRBD_PROT_C:
2198 dp_flags |= DP_SEND_WRITE_ACK;
2199 break;
2200 case DRBD_PROT_B:
2201 dp_flags |= DP_SEND_RECEIVE_ACK;
2202 break;
2203 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002204 rcu_read_unlock();
Philipp Reisner303d1442011-04-13 16:24:47 -07002205 }
2206
2207 if (dp_flags & DP_SEND_WRITE_ACK) {
2208 peer_req->flags |= EE_SEND_WRITE_ACK;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002209 inc_unacked(mdev);
2210 /* corresponding dec_unacked() in e_end_block()
2211 * respective _drbd_clear_done_ee */
Philipp Reisner303d1442011-04-13 16:24:47 -07002212 }
2213
2214 if (dp_flags & DP_SEND_RECEIVE_ACK) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002215 /* I really don't like it that the receiver thread
2216 * sends on the msock, but anyways */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002217 drbd_send_ack(mdev, P_RECV_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002218 }
2219
Lars Ellenberg6719fb02010-10-18 23:04:07 +02002220 if (mdev->state.pdsk < D_INCONSISTENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002221 /* In case we have the only disk of the cluster, */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002222 drbd_set_out_of_sync(mdev, peer_req->i.sector, peer_req->i.size);
2223 peer_req->flags |= EE_CALL_AL_COMPLETE_IO;
2224 peer_req->flags &= ~EE_MAY_SET_IN_SYNC;
Lars Ellenberg181286a2011-03-31 15:18:56 +02002225 drbd_al_begin_io(mdev, &peer_req->i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002226 }
2227
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002228 err = drbd_submit_peer_request(mdev, peer_req, rw, DRBD_FAULT_DT_WR);
2229 if (!err)
2230 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002231
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01002232 /* don't care for the reason here */
2233 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002234 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002235 list_del(&peer_req->w.list);
2236 drbd_remove_epoch_entry_interval(mdev, peer_req);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002237 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002238 if (peer_req->flags & EE_CALL_AL_COMPLETE_IO)
Lars Ellenberg181286a2011-03-31 15:18:56 +02002239 drbd_al_complete_io(mdev, &peer_req->i);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002240
Philipp Reisnerb411b362009-09-25 16:07:19 -07002241out_interrupted:
Philipp Reisner1e9dd292011-11-10 15:14:53 +01002242 drbd_may_finish_epoch(tconn, peer_req->epoch, EV_PUT + EV_CLEANUP);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002243 put_ldev(mdev);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02002244 drbd_free_peer_req(mdev, peer_req);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002245 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002246}
2247
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002248/* We may throttle resync, if the lower device seems to be busy,
2249 * and current sync rate is above c_min_rate.
2250 *
2251 * To decide whether or not the lower device is busy, we use a scheme similar
2252 * to MD RAID is_mddev_idle(): if the partition stats reveal "significant"
2253 * (more than 64 sectors) of activity we cannot account for with our own resync
2254 * activity, it obviously is "busy".
2255 *
2256 * The current sync rate used here uses only the most recent two step marks,
2257 * to have a short time average so we can react faster.
2258 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002259int drbd_rs_should_slow_down(struct drbd_conf *mdev, sector_t sector)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002260{
2261 struct gendisk *disk = mdev->ldev->backing_bdev->bd_contains->bd_disk;
2262 unsigned long db, dt, dbdt;
Philipp Reisnere3555d82010-11-07 15:56:29 +01002263 struct lc_element *tmp;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002264 int curr_events;
2265 int throttle = 0;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002266 unsigned int c_min_rate;
2267
2268 rcu_read_lock();
2269 c_min_rate = rcu_dereference(mdev->ldev->disk_conf)->c_min_rate;
2270 rcu_read_unlock();
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002271
2272 /* feature disabled? */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002273 if (c_min_rate == 0)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002274 return 0;
2275
Philipp Reisnere3555d82010-11-07 15:56:29 +01002276 spin_lock_irq(&mdev->al_lock);
2277 tmp = lc_find(mdev->resync, BM_SECT_TO_EXT(sector));
2278 if (tmp) {
2279 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
2280 if (test_bit(BME_PRIORITY, &bm_ext->flags)) {
2281 spin_unlock_irq(&mdev->al_lock);
2282 return 0;
2283 }
2284 /* Do not slow down if app IO is already waiting for this extent */
2285 }
2286 spin_unlock_irq(&mdev->al_lock);
2287
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002288 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
2289 (int)part_stat_read(&disk->part0, sectors[1]) -
2290 atomic_read(&mdev->rs_sect_ev);
Philipp Reisnere3555d82010-11-07 15:56:29 +01002291
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002292 if (!mdev->rs_last_events || curr_events - mdev->rs_last_events > 64) {
2293 unsigned long rs_left;
2294 int i;
2295
2296 mdev->rs_last_events = curr_events;
2297
2298 /* sync speed average over the last 2*DRBD_SYNC_MARK_STEP,
2299 * approx. */
Lars Ellenberg2649f082010-11-05 10:05:47 +01002300 i = (mdev->rs_last_mark + DRBD_SYNC_MARKS-1) % DRBD_SYNC_MARKS;
2301
2302 if (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T)
2303 rs_left = mdev->ov_left;
2304 else
2305 rs_left = drbd_bm_total_weight(mdev) - mdev->rs_failed;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002306
2307 dt = ((long)jiffies - (long)mdev->rs_mark_time[i]) / HZ;
2308 if (!dt)
2309 dt++;
2310 db = mdev->rs_mark_left[i] - rs_left;
2311 dbdt = Bit2KB(db/dt);
2312
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002313 if (dbdt > c_min_rate)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002314 throttle = 1;
2315 }
2316 return throttle;
2317}
2318
2319
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002320static int receive_DataRequest(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002321{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002322 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002323 sector_t sector;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002324 sector_t capacity;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002325 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002326 struct digest_info *di = NULL;
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002327 int size, verb;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002328 unsigned int fault_type;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02002329 struct p_block_req *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002330
2331 mdev = vnr_to_mdev(tconn, pi->vnr);
2332 if (!mdev)
2333 return -EIO;
2334 capacity = drbd_get_capacity(mdev->this_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002335
2336 sector = be64_to_cpu(p->sector);
2337 size = be32_to_cpu(p->blksize);
2338
Andreas Gruenbacherc670a392011-02-21 12:41:39 +01002339 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002340 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2341 (unsigned long long)sector, size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002342 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002343 }
2344 if (sector + (size>>9) > capacity) {
2345 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2346 (unsigned long long)sector, size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002347 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002348 }
2349
2350 if (!get_ldev_if_state(mdev, D_UP_TO_DATE)) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002351 verb = 1;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002352 switch (pi->cmd) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002353 case P_DATA_REQUEST:
2354 drbd_send_ack_rp(mdev, P_NEG_DREPLY, p);
2355 break;
2356 case P_RS_DATA_REQUEST:
2357 case P_CSUM_RS_REQUEST:
2358 case P_OV_REQUEST:
2359 drbd_send_ack_rp(mdev, P_NEG_RS_DREPLY , p);
2360 break;
2361 case P_OV_REPLY:
2362 verb = 0;
2363 dec_rs_pending(mdev);
2364 drbd_send_ack_ex(mdev, P_OV_RESULT, sector, size, ID_IN_SYNC);
2365 break;
2366 default:
Andreas Gruenbacher49ba9b12011-03-25 00:35:45 +01002367 BUG();
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002368 }
2369 if (verb && __ratelimit(&drbd_ratelimit_state))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002370 dev_err(DEV, "Can not satisfy peer's read request, "
2371 "no local data.\n");
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002372
Lars Ellenberga821cc42010-09-06 12:31:37 +02002373 /* drain possibly payload */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002374 return drbd_drain_block(mdev, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002375 }
2376
2377 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
2378 * "criss-cross" setup, that might cause write-out on some other DRBD,
2379 * which in turn might block on the other node at this very place. */
Andreas Gruenbacher0db55362011-04-06 16:09:15 +02002380 peer_req = drbd_alloc_peer_req(mdev, p->block_id, sector, size, GFP_NOIO);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002381 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002382 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002383 return -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002384 }
2385
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002386 switch (pi->cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002387 case P_DATA_REQUEST:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002388 peer_req->w.cb = w_e_end_data_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002389 fault_type = DRBD_FAULT_DT_RD;
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002390 /* application IO, don't drbd_rs_begin_io */
2391 goto submit;
2392
Philipp Reisnerb411b362009-09-25 16:07:19 -07002393 case P_RS_DATA_REQUEST:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002394 peer_req->w.cb = w_e_end_rsdata_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002395 fault_type = DRBD_FAULT_RS_RD;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002396 /* used in the sector offset progress display */
2397 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002398 break;
2399
2400 case P_OV_REPLY:
2401 case P_CSUM_RS_REQUEST:
2402 fault_type = DRBD_FAULT_RS_RD;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002403 di = kmalloc(sizeof(*di) + pi->size, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002404 if (!di)
2405 goto out_free_e;
2406
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002407 di->digest_size = pi->size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002408 di->digest = (((char *)di)+sizeof(struct digest_info));
2409
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002410 peer_req->digest = di;
2411 peer_req->flags |= EE_HAS_DIGEST;
Lars Ellenbergc36c3ce2010-08-11 20:42:55 +02002412
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002413 if (drbd_recv_all(mdev->tconn, di->digest, pi->size))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002414 goto out_free_e;
2415
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002416 if (pi->cmd == P_CSUM_RS_REQUEST) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002417 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002418 peer_req->w.cb = w_e_end_csum_rs_req;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002419 /* used in the sector offset progress display */
2420 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002421 } else if (pi->cmd == P_OV_REPLY) {
Lars Ellenberg2649f082010-11-05 10:05:47 +01002422 /* track progress, we may need to throttle */
2423 atomic_add(size >> 9, &mdev->rs_sect_in);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002424 peer_req->w.cb = w_e_end_ov_reply;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002425 dec_rs_pending(mdev);
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002426 /* drbd_rs_begin_io done when we sent this request,
2427 * but accounting still needs to be done. */
2428 goto submit_for_resync;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002429 }
2430 break;
2431
2432 case P_OV_REQUEST:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002433 if (mdev->ov_start_sector == ~(sector_t)0 &&
Philipp Reisner31890f42011-01-19 14:12:51 +01002434 mdev->tconn->agreed_pro_version >= 90) {
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002435 unsigned long now = jiffies;
2436 int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002437 mdev->ov_start_sector = sector;
2438 mdev->ov_position = sector;
Lars Ellenberg30b743a2010-11-05 09:39:06 +01002439 mdev->ov_left = drbd_bm_bits(mdev) - BM_SECT_TO_BIT(sector);
2440 mdev->rs_total = mdev->ov_left;
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002441 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
2442 mdev->rs_mark_left[i] = mdev->ov_left;
2443 mdev->rs_mark_time[i] = now;
2444 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002445 dev_info(DEV, "Online Verify start sector: %llu\n",
2446 (unsigned long long)sector);
2447 }
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002448 peer_req->w.cb = w_e_end_ov_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002449 fault_type = DRBD_FAULT_RS_RD;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002450 break;
2451
Philipp Reisnerb411b362009-09-25 16:07:19 -07002452 default:
Andreas Gruenbacher49ba9b12011-03-25 00:35:45 +01002453 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002454 }
2455
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002456 /* Throttle, drbd_rs_begin_io and submit should become asynchronous
2457 * wrt the receiver, but it is not as straightforward as it may seem.
2458 * Various places in the resync start and stop logic assume resync
2459 * requests are processed in order, requeuing this on the worker thread
2460 * introduces a bunch of new code for synchronization between threads.
2461 *
2462 * Unlimited throttling before drbd_rs_begin_io may stall the resync
2463 * "forever", throttling after drbd_rs_begin_io will lock that extent
2464 * for application writes for the same time. For now, just throttle
2465 * here, where the rest of the code expects the receiver to sleep for
2466 * a while, anyways.
2467 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002468
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002469 /* Throttle before drbd_rs_begin_io, as that locks out application IO;
2470 * this defers syncer requests for some time, before letting at least
2471 * on request through. The resync controller on the receiving side
2472 * will adapt to the incoming rate accordingly.
2473 *
2474 * We cannot throttle here if remote is Primary/SyncTarget:
2475 * we would also throttle its application reads.
2476 * In that case, throttling is done on the SyncTarget only.
2477 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002478 if (mdev->state.peer != R_PRIMARY && drbd_rs_should_slow_down(mdev, sector))
2479 schedule_timeout_uninterruptible(HZ/10);
2480 if (drbd_rs_begin_io(mdev, sector))
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002481 goto out_free_e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002482
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002483submit_for_resync:
2484 atomic_add(size >> 9, &mdev->rs_sect_ev);
2485
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002486submit:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002487 inc_unacked(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002488 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002489 list_add_tail(&peer_req->w.list, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002490 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002491
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01002492 if (drbd_submit_peer_request(mdev, peer_req, READ, fault_type) == 0)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002493 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002494
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01002495 /* don't care for the reason here */
2496 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002497 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002498 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002499 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002500 /* no drbd_rs_complete_io(), we are dropping the connection anyways */
2501
Philipp Reisnerb411b362009-09-25 16:07:19 -07002502out_free_e:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002503 put_ldev(mdev);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02002504 drbd_free_peer_req(mdev, peer_req);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002505 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002506}
2507
2508static int drbd_asb_recover_0p(struct drbd_conf *mdev) __must_hold(local)
2509{
2510 int self, peer, rv = -100;
2511 unsigned long ch_self, ch_peer;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002512 enum drbd_after_sb_p after_sb_0p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002513
2514 self = mdev->ldev->md.uuid[UI_BITMAP] & 1;
2515 peer = mdev->p_uuid[UI_BITMAP] & 1;
2516
2517 ch_peer = mdev->p_uuid[UI_SIZE];
2518 ch_self = mdev->comm_bm_set;
2519
Philipp Reisner44ed1672011-04-19 17:10:19 +02002520 rcu_read_lock();
2521 after_sb_0p = rcu_dereference(mdev->tconn->net_conf)->after_sb_0p;
2522 rcu_read_unlock();
2523 switch (after_sb_0p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002524 case ASB_CONSENSUS:
2525 case ASB_DISCARD_SECONDARY:
2526 case ASB_CALL_HELPER:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002527 case ASB_VIOLENTLY:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002528 dev_err(DEV, "Configuration error.\n");
2529 break;
2530 case ASB_DISCONNECT:
2531 break;
2532 case ASB_DISCARD_YOUNGER_PRI:
2533 if (self == 0 && peer == 1) {
2534 rv = -1;
2535 break;
2536 }
2537 if (self == 1 && peer == 0) {
2538 rv = 1;
2539 break;
2540 }
2541 /* Else fall through to one of the other strategies... */
2542 case ASB_DISCARD_OLDER_PRI:
2543 if (self == 0 && peer == 1) {
2544 rv = 1;
2545 break;
2546 }
2547 if (self == 1 && peer == 0) {
2548 rv = -1;
2549 break;
2550 }
2551 /* Else fall through to one of the other strategies... */
Lars Ellenbergad19bf62009-10-14 09:36:49 +02002552 dev_warn(DEV, "Discard younger/older primary did not find a decision\n"
Philipp Reisnerb411b362009-09-25 16:07:19 -07002553 "Using discard-least-changes instead\n");
2554 case ASB_DISCARD_ZERO_CHG:
2555 if (ch_peer == 0 && ch_self == 0) {
Philipp Reisner25703f82011-02-07 14:35:25 +01002556 rv = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002557 ? -1 : 1;
2558 break;
2559 } else {
2560 if (ch_peer == 0) { rv = 1; break; }
2561 if (ch_self == 0) { rv = -1; break; }
2562 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002563 if (after_sb_0p == ASB_DISCARD_ZERO_CHG)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002564 break;
2565 case ASB_DISCARD_LEAST_CHG:
2566 if (ch_self < ch_peer)
2567 rv = -1;
2568 else if (ch_self > ch_peer)
2569 rv = 1;
2570 else /* ( ch_self == ch_peer ) */
2571 /* Well, then use something else. */
Philipp Reisner25703f82011-02-07 14:35:25 +01002572 rv = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002573 ? -1 : 1;
2574 break;
2575 case ASB_DISCARD_LOCAL:
2576 rv = -1;
2577 break;
2578 case ASB_DISCARD_REMOTE:
2579 rv = 1;
2580 }
2581
2582 return rv;
2583}
2584
2585static int drbd_asb_recover_1p(struct drbd_conf *mdev) __must_hold(local)
2586{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002587 int hg, rv = -100;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002588 enum drbd_after_sb_p after_sb_1p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002589
Philipp Reisner44ed1672011-04-19 17:10:19 +02002590 rcu_read_lock();
2591 after_sb_1p = rcu_dereference(mdev->tconn->net_conf)->after_sb_1p;
2592 rcu_read_unlock();
2593 switch (after_sb_1p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002594 case ASB_DISCARD_YOUNGER_PRI:
2595 case ASB_DISCARD_OLDER_PRI:
2596 case ASB_DISCARD_LEAST_CHG:
2597 case ASB_DISCARD_LOCAL:
2598 case ASB_DISCARD_REMOTE:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002599 case ASB_DISCARD_ZERO_CHG:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002600 dev_err(DEV, "Configuration error.\n");
2601 break;
2602 case ASB_DISCONNECT:
2603 break;
2604 case ASB_CONSENSUS:
2605 hg = drbd_asb_recover_0p(mdev);
2606 if (hg == -1 && mdev->state.role == R_SECONDARY)
2607 rv = hg;
2608 if (hg == 1 && mdev->state.role == R_PRIMARY)
2609 rv = hg;
2610 break;
2611 case ASB_VIOLENTLY:
2612 rv = drbd_asb_recover_0p(mdev);
2613 break;
2614 case ASB_DISCARD_SECONDARY:
2615 return mdev->state.role == R_PRIMARY ? 1 : -1;
2616 case ASB_CALL_HELPER:
2617 hg = drbd_asb_recover_0p(mdev);
2618 if (hg == -1 && mdev->state.role == R_PRIMARY) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002619 enum drbd_state_rv rv2;
2620
2621 drbd_set_role(mdev, R_SECONDARY, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002622 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2623 * we might be here in C_WF_REPORT_PARAMS which is transient.
2624 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002625 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2626 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002627 drbd_khelper(mdev, "pri-lost-after-sb");
2628 } else {
2629 dev_warn(DEV, "Successfully gave up primary role.\n");
2630 rv = hg;
2631 }
2632 } else
2633 rv = hg;
2634 }
2635
2636 return rv;
2637}
2638
2639static int drbd_asb_recover_2p(struct drbd_conf *mdev) __must_hold(local)
2640{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002641 int hg, rv = -100;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002642 enum drbd_after_sb_p after_sb_2p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002643
Philipp Reisner44ed1672011-04-19 17:10:19 +02002644 rcu_read_lock();
2645 after_sb_2p = rcu_dereference(mdev->tconn->net_conf)->after_sb_2p;
2646 rcu_read_unlock();
2647 switch (after_sb_2p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002648 case ASB_DISCARD_YOUNGER_PRI:
2649 case ASB_DISCARD_OLDER_PRI:
2650 case ASB_DISCARD_LEAST_CHG:
2651 case ASB_DISCARD_LOCAL:
2652 case ASB_DISCARD_REMOTE:
2653 case ASB_CONSENSUS:
2654 case ASB_DISCARD_SECONDARY:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002655 case ASB_DISCARD_ZERO_CHG:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002656 dev_err(DEV, "Configuration error.\n");
2657 break;
2658 case ASB_VIOLENTLY:
2659 rv = drbd_asb_recover_0p(mdev);
2660 break;
2661 case ASB_DISCONNECT:
2662 break;
2663 case ASB_CALL_HELPER:
2664 hg = drbd_asb_recover_0p(mdev);
2665 if (hg == -1) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002666 enum drbd_state_rv rv2;
2667
Philipp Reisnerb411b362009-09-25 16:07:19 -07002668 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2669 * we might be here in C_WF_REPORT_PARAMS which is transient.
2670 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002671 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2672 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002673 drbd_khelper(mdev, "pri-lost-after-sb");
2674 } else {
2675 dev_warn(DEV, "Successfully gave up primary role.\n");
2676 rv = hg;
2677 }
2678 } else
2679 rv = hg;
2680 }
2681
2682 return rv;
2683}
2684
2685static void drbd_uuid_dump(struct drbd_conf *mdev, char *text, u64 *uuid,
2686 u64 bits, u64 flags)
2687{
2688 if (!uuid) {
2689 dev_info(DEV, "%s uuid info vanished while I was looking!\n", text);
2690 return;
2691 }
2692 dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX bits:%llu flags:%llX\n",
2693 text,
2694 (unsigned long long)uuid[UI_CURRENT],
2695 (unsigned long long)uuid[UI_BITMAP],
2696 (unsigned long long)uuid[UI_HISTORY_START],
2697 (unsigned long long)uuid[UI_HISTORY_END],
2698 (unsigned long long)bits,
2699 (unsigned long long)flags);
2700}
2701
2702/*
2703 100 after split brain try auto recover
2704 2 C_SYNC_SOURCE set BitMap
2705 1 C_SYNC_SOURCE use BitMap
2706 0 no Sync
2707 -1 C_SYNC_TARGET use BitMap
2708 -2 C_SYNC_TARGET set BitMap
2709 -100 after split brain, disconnect
2710-1000 unrelated data
Philipp Reisner4a23f262011-01-11 17:42:17 +01002711-1091 requires proto 91
2712-1096 requires proto 96
Philipp Reisnerb411b362009-09-25 16:07:19 -07002713 */
2714static int drbd_uuid_compare(struct drbd_conf *mdev, int *rule_nr) __must_hold(local)
2715{
2716 u64 self, peer;
2717 int i, j;
2718
2719 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2720 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2721
2722 *rule_nr = 10;
2723 if (self == UUID_JUST_CREATED && peer == UUID_JUST_CREATED)
2724 return 0;
2725
2726 *rule_nr = 20;
2727 if ((self == UUID_JUST_CREATED || self == (u64)0) &&
2728 peer != UUID_JUST_CREATED)
2729 return -2;
2730
2731 *rule_nr = 30;
2732 if (self != UUID_JUST_CREATED &&
2733 (peer == UUID_JUST_CREATED || peer == (u64)0))
2734 return 2;
2735
2736 if (self == peer) {
2737 int rct, dc; /* roles at crash time */
2738
2739 if (mdev->p_uuid[UI_BITMAP] == (u64)0 && mdev->ldev->md.uuid[UI_BITMAP] != (u64)0) {
2740
Philipp Reisner31890f42011-01-19 14:12:51 +01002741 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002742 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002743
2744 if ((mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) &&
2745 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1))) {
2746 dev_info(DEV, "was SyncSource, missed the resync finished event, corrected myself:\n");
2747 drbd_uuid_set_bm(mdev, 0UL);
2748
2749 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2750 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2751 *rule_nr = 34;
2752 } else {
2753 dev_info(DEV, "was SyncSource (peer failed to write sync_uuid)\n");
2754 *rule_nr = 36;
2755 }
2756
2757 return 1;
2758 }
2759
2760 if (mdev->ldev->md.uuid[UI_BITMAP] == (u64)0 && mdev->p_uuid[UI_BITMAP] != (u64)0) {
2761
Philipp Reisner31890f42011-01-19 14:12:51 +01002762 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002763 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002764
2765 if ((mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_BITMAP] & ~((u64)1)) &&
2766 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1))) {
2767 dev_info(DEV, "was SyncTarget, peer missed the resync finished event, corrected peer:\n");
2768
2769 mdev->p_uuid[UI_HISTORY_START + 1] = mdev->p_uuid[UI_HISTORY_START];
2770 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_BITMAP];
2771 mdev->p_uuid[UI_BITMAP] = 0UL;
2772
2773 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2774 *rule_nr = 35;
2775 } else {
2776 dev_info(DEV, "was SyncTarget (failed to write sync_uuid)\n");
2777 *rule_nr = 37;
2778 }
2779
2780 return -1;
2781 }
2782
2783 /* Common power [off|failure] */
2784 rct = (test_bit(CRASHED_PRIMARY, &mdev->flags) ? 1 : 0) +
2785 (mdev->p_uuid[UI_FLAGS] & 2);
2786 /* lowest bit is set when we were primary,
2787 * next bit (weight 2) is set when peer was primary */
2788 *rule_nr = 40;
2789
2790 switch (rct) {
2791 case 0: /* !self_pri && !peer_pri */ return 0;
2792 case 1: /* self_pri && !peer_pri */ return 1;
2793 case 2: /* !self_pri && peer_pri */ return -1;
2794 case 3: /* self_pri && peer_pri */
Philipp Reisner25703f82011-02-07 14:35:25 +01002795 dc = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002796 return dc ? -1 : 1;
2797 }
2798 }
2799
2800 *rule_nr = 50;
2801 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2802 if (self == peer)
2803 return -1;
2804
2805 *rule_nr = 51;
2806 peer = mdev->p_uuid[UI_HISTORY_START] & ~((u64)1);
2807 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002808 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002809 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) ==
2810 (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1)) :
2811 peer + UUID_NEW_BM_OFFSET == (mdev->p_uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002812 /* The last P_SYNC_UUID did not get though. Undo the last start of
2813 resync as sync source modifications of the peer's UUIDs. */
2814
Philipp Reisner31890f42011-01-19 14:12:51 +01002815 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002816 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002817
2818 mdev->p_uuid[UI_BITMAP] = mdev->p_uuid[UI_HISTORY_START];
2819 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_HISTORY_START + 1];
Philipp Reisner4a23f262011-01-11 17:42:17 +01002820
2821 dev_info(DEV, "Did not got last syncUUID packet, corrected:\n");
2822 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2823
Philipp Reisnerb411b362009-09-25 16:07:19 -07002824 return -1;
2825 }
2826 }
2827
2828 *rule_nr = 60;
2829 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2830 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2831 peer = mdev->p_uuid[i] & ~((u64)1);
2832 if (self == peer)
2833 return -2;
2834 }
2835
2836 *rule_nr = 70;
2837 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2838 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2839 if (self == peer)
2840 return 1;
2841
2842 *rule_nr = 71;
2843 self = mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1);
2844 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002845 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002846 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) ==
2847 (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) :
2848 self + UUID_NEW_BM_OFFSET == (mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002849 /* The last P_SYNC_UUID did not get though. Undo the last start of
2850 resync as sync source modifications of our UUIDs. */
2851
Philipp Reisner31890f42011-01-19 14:12:51 +01002852 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002853 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002854
2855 _drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_HISTORY_START]);
2856 _drbd_uuid_set(mdev, UI_HISTORY_START, mdev->ldev->md.uuid[UI_HISTORY_START + 1]);
2857
Philipp Reisner4a23f262011-01-11 17:42:17 +01002858 dev_info(DEV, "Last syncUUID did not get through, corrected:\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002859 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2860 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2861
2862 return 1;
2863 }
2864 }
2865
2866
2867 *rule_nr = 80;
Philipp Reisnerd8c2a362009-11-18 15:52:51 +01002868 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002869 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2870 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2871 if (self == peer)
2872 return 2;
2873 }
2874
2875 *rule_nr = 90;
2876 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2877 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2878 if (self == peer && self != ((u64)0))
2879 return 100;
2880
2881 *rule_nr = 100;
2882 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2883 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2884 for (j = UI_HISTORY_START; j <= UI_HISTORY_END; j++) {
2885 peer = mdev->p_uuid[j] & ~((u64)1);
2886 if (self == peer)
2887 return -100;
2888 }
2889 }
2890
2891 return -1000;
2892}
2893
2894/* drbd_sync_handshake() returns the new conn state on success, or
2895 CONN_MASK (-1) on failure.
2896 */
2897static enum drbd_conns drbd_sync_handshake(struct drbd_conf *mdev, enum drbd_role peer_role,
2898 enum drbd_disk_state peer_disk) __must_hold(local)
2899{
Philipp Reisnerb411b362009-09-25 16:07:19 -07002900 enum drbd_conns rv = C_MASK;
2901 enum drbd_disk_state mydisk;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002902 struct net_conf *nc;
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02002903 int hg, rule_nr, rr_conflict, tentative;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002904
2905 mydisk = mdev->state.disk;
2906 if (mydisk == D_NEGOTIATING)
2907 mydisk = mdev->new_state_tmp.disk;
2908
2909 dev_info(DEV, "drbd_sync_handshake:\n");
2910 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid, mdev->comm_bm_set, 0);
2911 drbd_uuid_dump(mdev, "peer", mdev->p_uuid,
2912 mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2913
2914 hg = drbd_uuid_compare(mdev, &rule_nr);
2915
2916 dev_info(DEV, "uuid_compare()=%d by rule %d\n", hg, rule_nr);
2917
2918 if (hg == -1000) {
2919 dev_alert(DEV, "Unrelated data, aborting!\n");
2920 return C_MASK;
2921 }
Philipp Reisner4a23f262011-01-11 17:42:17 +01002922 if (hg < -1000) {
2923 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 -07002924 return C_MASK;
2925 }
2926
2927 if ((mydisk == D_INCONSISTENT && peer_disk > D_INCONSISTENT) ||
2928 (peer_disk == D_INCONSISTENT && mydisk > D_INCONSISTENT)) {
2929 int f = (hg == -100) || abs(hg) == 2;
2930 hg = mydisk > D_INCONSISTENT ? 1 : -1;
2931 if (f)
2932 hg = hg*2;
2933 dev_info(DEV, "Becoming sync %s due to disk states.\n",
2934 hg > 0 ? "source" : "target");
2935 }
2936
Adam Gandelman3a11a482010-04-08 16:48:23 -07002937 if (abs(hg) == 100)
2938 drbd_khelper(mdev, "initial-split-brain");
2939
Philipp Reisner44ed1672011-04-19 17:10:19 +02002940 rcu_read_lock();
2941 nc = rcu_dereference(mdev->tconn->net_conf);
2942
2943 if (hg == 100 || (hg == -100 && nc->always_asbp)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002944 int pcount = (mdev->state.role == R_PRIMARY)
2945 + (peer_role == R_PRIMARY);
2946 int forced = (hg == -100);
2947
2948 switch (pcount) {
2949 case 0:
2950 hg = drbd_asb_recover_0p(mdev);
2951 break;
2952 case 1:
2953 hg = drbd_asb_recover_1p(mdev);
2954 break;
2955 case 2:
2956 hg = drbd_asb_recover_2p(mdev);
2957 break;
2958 }
2959 if (abs(hg) < 100) {
2960 dev_warn(DEV, "Split-Brain detected, %d primaries, "
2961 "automatically solved. Sync from %s node\n",
2962 pcount, (hg < 0) ? "peer" : "this");
2963 if (forced) {
2964 dev_warn(DEV, "Doing a full sync, since"
2965 " UUIDs where ambiguous.\n");
2966 hg = hg*2;
2967 }
2968 }
2969 }
2970
2971 if (hg == -100) {
Philipp Reisner08b165b2011-09-05 16:22:33 +02002972 if (test_bit(DISCARD_MY_DATA, &mdev->flags) && !(mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002973 hg = -1;
Philipp Reisner08b165b2011-09-05 16:22:33 +02002974 if (!test_bit(DISCARD_MY_DATA, &mdev->flags) && (mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002975 hg = 1;
2976
2977 if (abs(hg) < 100)
2978 dev_warn(DEV, "Split-Brain detected, manually solved. "
2979 "Sync from %s node\n",
2980 (hg < 0) ? "peer" : "this");
2981 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002982 rr_conflict = nc->rr_conflict;
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02002983 tentative = nc->tentative;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002984 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002985
2986 if (hg == -100) {
Lars Ellenberg580b9762010-02-26 23:15:23 +01002987 /* FIXME this log message is not correct if we end up here
2988 * after an attempted attach on a diskless node.
2989 * We just refuse to attach -- well, we drop the "connection"
2990 * to that disk, in a way... */
Adam Gandelman3a11a482010-04-08 16:48:23 -07002991 dev_alert(DEV, "Split-Brain detected but unresolved, dropping connection!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002992 drbd_khelper(mdev, "split-brain");
2993 return C_MASK;
2994 }
2995
2996 if (hg > 0 && mydisk <= D_INCONSISTENT) {
2997 dev_err(DEV, "I shall become SyncSource, but I am inconsistent!\n");
2998 return C_MASK;
2999 }
3000
3001 if (hg < 0 && /* by intention we do not use mydisk here. */
3002 mdev->state.role == R_PRIMARY && mdev->state.disk >= D_CONSISTENT) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02003003 switch (rr_conflict) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003004 case ASB_CALL_HELPER:
3005 drbd_khelper(mdev, "pri-lost");
3006 /* fall through */
3007 case ASB_DISCONNECT:
3008 dev_err(DEV, "I shall become SyncTarget, but I am primary!\n");
3009 return C_MASK;
3010 case ASB_VIOLENTLY:
3011 dev_warn(DEV, "Becoming SyncTarget, violating the stable-data"
3012 "assumption\n");
3013 }
3014 }
3015
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02003016 if (tentative || test_bit(CONN_DRY_RUN, &mdev->tconn->flags)) {
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003017 if (hg == 0)
3018 dev_info(DEV, "dry-run connect: No resync, would become Connected immediately.\n");
3019 else
3020 dev_info(DEV, "dry-run connect: Would become %s, doing a %s resync.",
3021 drbd_conn_str(hg > 0 ? C_SYNC_SOURCE : C_SYNC_TARGET),
3022 abs(hg) >= 2 ? "full" : "bit-map based");
3023 return C_MASK;
3024 }
3025
Philipp Reisnerb411b362009-09-25 16:07:19 -07003026 if (abs(hg) >= 2) {
3027 dev_info(DEV, "Writing the whole bitmap, full sync required after drbd_sync_handshake.\n");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003028 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write, "set_n_write from sync_handshake",
3029 BM_LOCKED_SET_ALLOWED))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003030 return C_MASK;
3031 }
3032
3033 if (hg > 0) { /* become sync source. */
3034 rv = C_WF_BITMAP_S;
3035 } else if (hg < 0) { /* become sync target */
3036 rv = C_WF_BITMAP_T;
3037 } else {
3038 rv = C_CONNECTED;
3039 if (drbd_bm_total_weight(mdev)) {
3040 dev_info(DEV, "No resync, but %lu bits in bitmap!\n",
3041 drbd_bm_total_weight(mdev));
3042 }
3043 }
3044
3045 return rv;
3046}
3047
Philipp Reisnerf179d762011-05-16 17:31:47 +02003048static enum drbd_after_sb_p convert_after_sb(enum drbd_after_sb_p peer)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003049{
3050 /* ASB_DISCARD_REMOTE - ASB_DISCARD_LOCAL is valid */
Philipp Reisnerf179d762011-05-16 17:31:47 +02003051 if (peer == ASB_DISCARD_REMOTE)
3052 return ASB_DISCARD_LOCAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003053
3054 /* any other things with ASB_DISCARD_REMOTE or ASB_DISCARD_LOCAL are invalid */
Philipp Reisnerf179d762011-05-16 17:31:47 +02003055 if (peer == ASB_DISCARD_LOCAL)
3056 return ASB_DISCARD_REMOTE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003057
3058 /* everything else is valid if they are equal on both sides. */
Philipp Reisnerf179d762011-05-16 17:31:47 +02003059 return peer;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003060}
3061
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003062static int receive_protocol(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003063{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003064 struct p_protocol *p = pi->data;
Philipp Reisner036b17e2011-05-16 17:38:11 +02003065 enum drbd_after_sb_p p_after_sb_0p, p_after_sb_1p, p_after_sb_2p;
3066 int p_proto, p_discard_my_data, p_two_primaries, cf;
3067 struct net_conf *nc, *old_net_conf, *new_net_conf = NULL;
3068 char integrity_alg[SHARED_SECRET_MAX] = "";
Andreas Gruenbacheraccdbcc2011-07-15 17:41:09 +02003069 struct crypto_hash *peer_integrity_tfm = NULL;
Philipp Reisner7aca6c72011-05-17 10:12:56 +02003070 void *int_dig_in = NULL, *int_dig_vv = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003071
Philipp Reisnerb411b362009-09-25 16:07:19 -07003072 p_proto = be32_to_cpu(p->protocol);
3073 p_after_sb_0p = be32_to_cpu(p->after_sb_0p);
3074 p_after_sb_1p = be32_to_cpu(p->after_sb_1p);
3075 p_after_sb_2p = be32_to_cpu(p->after_sb_2p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003076 p_two_primaries = be32_to_cpu(p->two_primaries);
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003077 cf = be32_to_cpu(p->conn_flags);
Andreas Gruenbacher6139f602011-05-06 20:00:02 +02003078 p_discard_my_data = cf & CF_DISCARD_MY_DATA;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003079
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003080 if (tconn->agreed_pro_version >= 87) {
3081 int err;
3082
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02003083 if (pi->size > sizeof(integrity_alg))
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003084 return -EIO;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02003085 err = drbd_recv_all(tconn, integrity_alg, pi->size);
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003086 if (err)
3087 return err;
Philipp Reisner036b17e2011-05-16 17:38:11 +02003088 integrity_alg[SHARED_SECRET_MAX - 1] = 0;
3089 }
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003090
Andreas Gruenbacher7d4c7822011-07-17 23:06:12 +02003091 if (pi->cmd != P_PROTOCOL_UPDATE) {
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003092 clear_bit(CONN_DRY_RUN, &tconn->flags);
Philipp Reisner036b17e2011-05-16 17:38:11 +02003093
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003094 if (cf & CF_DRY_RUN)
3095 set_bit(CONN_DRY_RUN, &tconn->flags);
3096
3097 rcu_read_lock();
3098 nc = rcu_dereference(tconn->net_conf);
3099
3100 if (p_proto != nc->wire_protocol) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003101 conn_err(tconn, "incompatible %s settings\n", "protocol");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003102 goto disconnect_rcu_unlock;
3103 }
3104
3105 if (convert_after_sb(p_after_sb_0p) != nc->after_sb_0p) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003106 conn_err(tconn, "incompatible %s settings\n", "after-sb-0pri");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003107 goto disconnect_rcu_unlock;
3108 }
3109
3110 if (convert_after_sb(p_after_sb_1p) != nc->after_sb_1p) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003111 conn_err(tconn, "incompatible %s settings\n", "after-sb-1pri");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003112 goto disconnect_rcu_unlock;
3113 }
3114
3115 if (convert_after_sb(p_after_sb_2p) != nc->after_sb_2p) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003116 conn_err(tconn, "incompatible %s settings\n", "after-sb-2pri");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003117 goto disconnect_rcu_unlock;
3118 }
3119
3120 if (p_discard_my_data && nc->discard_my_data) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003121 conn_err(tconn, "incompatible %s settings\n", "discard-my-data");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003122 goto disconnect_rcu_unlock;
3123 }
3124
3125 if (p_two_primaries != nc->two_primaries) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003126 conn_err(tconn, "incompatible %s settings\n", "allow-two-primaries");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003127 goto disconnect_rcu_unlock;
3128 }
3129
3130 if (strcmp(integrity_alg, nc->integrity_alg)) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003131 conn_err(tconn, "incompatible %s settings\n", "data-integrity-alg");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003132 goto disconnect_rcu_unlock;
3133 }
3134
3135 rcu_read_unlock();
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003136 }
Andreas Gruenbacher7d4c7822011-07-17 23:06:12 +02003137
3138 if (integrity_alg[0]) {
3139 int hash_size;
3140
3141 /*
3142 * We can only change the peer data integrity algorithm
3143 * here. Changing our own data integrity algorithm
3144 * requires that we send a P_PROTOCOL_UPDATE packet at
3145 * the same time; otherwise, the peer has no way to
3146 * tell between which packets the algorithm should
3147 * change.
3148 */
3149
3150 peer_integrity_tfm = crypto_alloc_hash(integrity_alg, 0, CRYPTO_ALG_ASYNC);
3151 if (!peer_integrity_tfm) {
3152 conn_err(tconn, "peer data-integrity-alg %s not supported\n",
3153 integrity_alg);
3154 goto disconnect;
3155 }
3156
3157 hash_size = crypto_hash_digestsize(peer_integrity_tfm);
3158 int_dig_in = kmalloc(hash_size, GFP_KERNEL);
3159 int_dig_vv = kmalloc(hash_size, GFP_KERNEL);
3160 if (!(int_dig_in && int_dig_vv)) {
3161 conn_err(tconn, "Allocation of buffers for data integrity checking failed\n");
3162 goto disconnect;
3163 }
3164 }
3165
3166 new_net_conf = kmalloc(sizeof(struct net_conf), GFP_KERNEL);
3167 if (!new_net_conf) {
3168 conn_err(tconn, "Allocation of new net_conf failed\n");
3169 goto disconnect;
3170 }
3171
3172 mutex_lock(&tconn->data.mutex);
3173 mutex_lock(&tconn->conf_update);
3174 old_net_conf = tconn->net_conf;
3175 *new_net_conf = *old_net_conf;
3176
3177 new_net_conf->wire_protocol = p_proto;
3178 new_net_conf->after_sb_0p = convert_after_sb(p_after_sb_0p);
3179 new_net_conf->after_sb_1p = convert_after_sb(p_after_sb_1p);
3180 new_net_conf->after_sb_2p = convert_after_sb(p_after_sb_2p);
3181 new_net_conf->two_primaries = p_two_primaries;
3182
3183 rcu_assign_pointer(tconn->net_conf, new_net_conf);
3184 mutex_unlock(&tconn->conf_update);
3185 mutex_unlock(&tconn->data.mutex);
3186
3187 crypto_free_hash(tconn->peer_integrity_tfm);
3188 kfree(tconn->int_dig_in);
3189 kfree(tconn->int_dig_vv);
3190 tconn->peer_integrity_tfm = peer_integrity_tfm;
3191 tconn->int_dig_in = int_dig_in;
3192 tconn->int_dig_vv = int_dig_vv;
3193
3194 if (strcmp(old_net_conf->integrity_alg, integrity_alg))
3195 conn_info(tconn, "peer data-integrity-alg: %s\n",
3196 integrity_alg[0] ? integrity_alg : "(none)");
3197
3198 synchronize_rcu();
3199 kfree(old_net_conf);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003200 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003201
Philipp Reisner44ed1672011-04-19 17:10:19 +02003202disconnect_rcu_unlock:
3203 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07003204disconnect:
Andreas Gruenbacherb792c352011-07-15 16:48:49 +02003205 crypto_free_hash(peer_integrity_tfm);
Philipp Reisner036b17e2011-05-16 17:38:11 +02003206 kfree(int_dig_in);
3207 kfree(int_dig_vv);
Philipp Reisner72046242011-03-15 18:51:47 +01003208 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003209 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003210}
3211
3212/* helper function
3213 * input: alg name, feature name
3214 * return: NULL (alg name was "")
3215 * ERR_PTR(error) if something goes wrong
3216 * or the crypto hash ptr, if it worked out ok. */
3217struct crypto_hash *drbd_crypto_alloc_digest_safe(const struct drbd_conf *mdev,
3218 const char *alg, const char *name)
3219{
3220 struct crypto_hash *tfm;
3221
3222 if (!alg[0])
3223 return NULL;
3224
3225 tfm = crypto_alloc_hash(alg, 0, CRYPTO_ALG_ASYNC);
3226 if (IS_ERR(tfm)) {
3227 dev_err(DEV, "Can not allocate \"%s\" as %s (reason: %ld)\n",
3228 alg, name, PTR_ERR(tfm));
3229 return tfm;
3230 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003231 return tfm;
3232}
3233
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003234static int ignore_remaining_packet(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003235{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003236 void *buffer = tconn->data.rbuf;
3237 int size = pi->size;
3238
3239 while (size) {
3240 int s = min_t(int, size, DRBD_SOCKET_BUFFER_SIZE);
3241 s = drbd_recv(tconn, buffer, s);
3242 if (s <= 0) {
3243 if (s < 0)
3244 return s;
3245 break;
3246 }
3247 size -= s;
3248 }
3249 if (size)
3250 return -EIO;
3251 return 0;
3252}
3253
3254/*
3255 * config_unknown_volume - device configuration command for unknown volume
3256 *
3257 * When a device is added to an existing connection, the node on which the
3258 * device is added first will send configuration commands to its peer but the
3259 * peer will not know about the device yet. It will warn and ignore these
3260 * commands. Once the device is added on the second node, the second node will
3261 * send the same device configuration commands, but in the other direction.
3262 *
3263 * (We can also end up here if drbd is misconfigured.)
3264 */
3265static int config_unknown_volume(struct drbd_tconn *tconn, struct packet_info *pi)
3266{
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02003267 conn_warn(tconn, "%s packet received for volume %u, which is not configured locally\n",
3268 cmdname(pi->cmd), pi->vnr);
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003269 return ignore_remaining_packet(tconn, pi);
3270}
3271
3272static int receive_SyncParam(struct drbd_tconn *tconn, struct packet_info *pi)
3273{
3274 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003275 struct p_rs_param_95 *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003276 unsigned int header_size, data_size, exp_max_sz;
3277 struct crypto_hash *verify_tfm = NULL;
3278 struct crypto_hash *csums_tfm = NULL;
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003279 struct net_conf *old_net_conf, *new_net_conf = NULL;
Philipp Reisner813472c2011-05-03 16:47:02 +02003280 struct disk_conf *old_disk_conf = NULL, *new_disk_conf = NULL;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003281 const int apv = tconn->agreed_pro_version;
Philipp Reisner813472c2011-05-03 16:47:02 +02003282 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
Philipp Reisner778f2712010-07-06 11:14:00 +02003283 int fifo_size = 0;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003284 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003285
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003286 mdev = vnr_to_mdev(tconn, pi->vnr);
3287 if (!mdev)
3288 return config_unknown_volume(tconn, pi);
3289
Philipp Reisnerb411b362009-09-25 16:07:19 -07003290 exp_max_sz = apv <= 87 ? sizeof(struct p_rs_param)
3291 : apv == 88 ? sizeof(struct p_rs_param)
3292 + SHARED_SECRET_MAX
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003293 : apv <= 94 ? sizeof(struct p_rs_param_89)
3294 : /* apv >= 95 */ sizeof(struct p_rs_param_95);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003295
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003296 if (pi->size > exp_max_sz) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003297 dev_err(DEV, "SyncParam packet too long: received %u, expected <= %u bytes\n",
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003298 pi->size, exp_max_sz);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003299 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003300 }
3301
3302 if (apv <= 88) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003303 header_size = sizeof(struct p_rs_param);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003304 data_size = pi->size - header_size;
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003305 } else if (apv <= 94) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003306 header_size = sizeof(struct p_rs_param_89);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003307 data_size = pi->size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003308 D_ASSERT(data_size == 0);
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003309 } else {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003310 header_size = sizeof(struct p_rs_param_95);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003311 data_size = pi->size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003312 D_ASSERT(data_size == 0);
3313 }
3314
3315 /* initialize verify_alg and csums_alg */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003316 p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003317 memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
3318
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003319 err = drbd_recv_all(mdev->tconn, p, header_size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003320 if (err)
3321 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003322
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003323 mutex_lock(&mdev->tconn->conf_update);
3324 old_net_conf = mdev->tconn->net_conf;
Philipp Reisner813472c2011-05-03 16:47:02 +02003325 if (get_ldev(mdev)) {
3326 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
3327 if (!new_disk_conf) {
3328 put_ldev(mdev);
3329 mutex_unlock(&mdev->tconn->conf_update);
3330 dev_err(DEV, "Allocation of new disk_conf failed\n");
3331 return -ENOMEM;
3332 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003333
Philipp Reisner813472c2011-05-03 16:47:02 +02003334 old_disk_conf = mdev->ldev->disk_conf;
3335 *new_disk_conf = *old_disk_conf;
3336
Andreas Gruenbacher6394b932011-05-11 14:29:52 +02003337 new_disk_conf->resync_rate = be32_to_cpu(p->resync_rate);
Philipp Reisner813472c2011-05-03 16:47:02 +02003338 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003339
Philipp Reisnerb411b362009-09-25 16:07:19 -07003340 if (apv >= 88) {
3341 if (apv == 88) {
3342 if (data_size > SHARED_SECRET_MAX) {
3343 dev_err(DEV, "verify-alg too long, "
3344 "peer wants %u, accepting only %u byte\n",
3345 data_size, SHARED_SECRET_MAX);
Philipp Reisner813472c2011-05-03 16:47:02 +02003346 err = -EIO;
3347 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003348 }
3349
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003350 err = drbd_recv_all(mdev->tconn, p->verify_alg, data_size);
Philipp Reisner813472c2011-05-03 16:47:02 +02003351 if (err)
3352 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003353 /* we expect NUL terminated string */
3354 /* but just in case someone tries to be evil */
3355 D_ASSERT(p->verify_alg[data_size-1] == 0);
3356 p->verify_alg[data_size-1] = 0;
3357
3358 } else /* apv >= 89 */ {
3359 /* we still expect NUL terminated strings */
3360 /* but just in case someone tries to be evil */
3361 D_ASSERT(p->verify_alg[SHARED_SECRET_MAX-1] == 0);
3362 D_ASSERT(p->csums_alg[SHARED_SECRET_MAX-1] == 0);
3363 p->verify_alg[SHARED_SECRET_MAX-1] = 0;
3364 p->csums_alg[SHARED_SECRET_MAX-1] = 0;
3365 }
3366
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003367 if (strcmp(old_net_conf->verify_alg, p->verify_alg)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003368 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
3369 dev_err(DEV, "Different verify-alg settings. me=\"%s\" peer=\"%s\"\n",
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003370 old_net_conf->verify_alg, p->verify_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003371 goto disconnect;
3372 }
3373 verify_tfm = drbd_crypto_alloc_digest_safe(mdev,
3374 p->verify_alg, "verify-alg");
3375 if (IS_ERR(verify_tfm)) {
3376 verify_tfm = NULL;
3377 goto disconnect;
3378 }
3379 }
3380
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003381 if (apv >= 89 && strcmp(old_net_conf->csums_alg, p->csums_alg)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003382 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
3383 dev_err(DEV, "Different csums-alg settings. me=\"%s\" peer=\"%s\"\n",
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003384 old_net_conf->csums_alg, p->csums_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003385 goto disconnect;
3386 }
3387 csums_tfm = drbd_crypto_alloc_digest_safe(mdev,
3388 p->csums_alg, "csums-alg");
3389 if (IS_ERR(csums_tfm)) {
3390 csums_tfm = NULL;
3391 goto disconnect;
3392 }
3393 }
3394
Philipp Reisner813472c2011-05-03 16:47:02 +02003395 if (apv > 94 && new_disk_conf) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003396 new_disk_conf->c_plan_ahead = be32_to_cpu(p->c_plan_ahead);
3397 new_disk_conf->c_delay_target = be32_to_cpu(p->c_delay_target);
3398 new_disk_conf->c_fill_target = be32_to_cpu(p->c_fill_target);
3399 new_disk_conf->c_max_rate = be32_to_cpu(p->c_max_rate);
Philipp Reisner778f2712010-07-06 11:14:00 +02003400
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003401 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
Philipp Reisner9958c852011-05-03 16:19:31 +02003402 if (fifo_size != mdev->rs_plan_s->size) {
Philipp Reisner813472c2011-05-03 16:47:02 +02003403 new_plan = fifo_alloc(fifo_size);
3404 if (!new_plan) {
Philipp Reisner778f2712010-07-06 11:14:00 +02003405 dev_err(DEV, "kmalloc of fifo_buffer failed");
Lars Ellenbergf3990022011-03-23 14:31:09 +01003406 put_ldev(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02003407 goto disconnect;
3408 }
3409 }
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003410 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003411
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003412 if (verify_tfm || csums_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003413 new_net_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
3414 if (!new_net_conf) {
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003415 dev_err(DEV, "Allocation of new net_conf failed\n");
3416 goto disconnect;
3417 }
3418
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003419 *new_net_conf = *old_net_conf;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003420
3421 if (verify_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003422 strcpy(new_net_conf->verify_alg, p->verify_alg);
3423 new_net_conf->verify_alg_len = strlen(p->verify_alg) + 1;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003424 crypto_free_hash(mdev->tconn->verify_tfm);
3425 mdev->tconn->verify_tfm = verify_tfm;
3426 dev_info(DEV, "using verify-alg: \"%s\"\n", p->verify_alg);
3427 }
3428 if (csums_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003429 strcpy(new_net_conf->csums_alg, p->csums_alg);
3430 new_net_conf->csums_alg_len = strlen(p->csums_alg) + 1;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003431 crypto_free_hash(mdev->tconn->csums_tfm);
3432 mdev->tconn->csums_tfm = csums_tfm;
3433 dev_info(DEV, "using csums-alg: \"%s\"\n", p->csums_alg);
3434 }
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003435 rcu_assign_pointer(tconn->net_conf, new_net_conf);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003436 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003437 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003438
Philipp Reisner813472c2011-05-03 16:47:02 +02003439 if (new_disk_conf) {
3440 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
3441 put_ldev(mdev);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003442 }
Philipp Reisner813472c2011-05-03 16:47:02 +02003443
3444 if (new_plan) {
3445 old_plan = mdev->rs_plan_s;
3446 rcu_assign_pointer(mdev->rs_plan_s, new_plan);
3447 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003448
3449 mutex_unlock(&mdev->tconn->conf_update);
3450 synchronize_rcu();
3451 if (new_net_conf)
3452 kfree(old_net_conf);
3453 kfree(old_disk_conf);
Philipp Reisner813472c2011-05-03 16:47:02 +02003454 kfree(old_plan);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003455
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003456 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003457
Philipp Reisner813472c2011-05-03 16:47:02 +02003458reconnect:
3459 if (new_disk_conf) {
3460 put_ldev(mdev);
3461 kfree(new_disk_conf);
3462 }
3463 mutex_unlock(&mdev->tconn->conf_update);
3464 return -EIO;
3465
Philipp Reisnerb411b362009-09-25 16:07:19 -07003466disconnect:
Philipp Reisner813472c2011-05-03 16:47:02 +02003467 kfree(new_plan);
3468 if (new_disk_conf) {
3469 put_ldev(mdev);
3470 kfree(new_disk_conf);
3471 }
Philipp Reisnera0095502011-05-03 13:14:15 +02003472 mutex_unlock(&mdev->tconn->conf_update);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003473 /* just for completeness: actually not needed,
3474 * as this is not reached if csums_tfm was ok. */
3475 crypto_free_hash(csums_tfm);
3476 /* but free the verify_tfm again, if csums_tfm did not work out */
3477 crypto_free_hash(verify_tfm);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003478 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003479 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003480}
3481
Philipp Reisnerb411b362009-09-25 16:07:19 -07003482/* warn if the arguments differ by more than 12.5% */
3483static void warn_if_differ_considerably(struct drbd_conf *mdev,
3484 const char *s, sector_t a, sector_t b)
3485{
3486 sector_t d;
3487 if (a == 0 || b == 0)
3488 return;
3489 d = (a > b) ? (a - b) : (b - a);
3490 if (d > (a>>3) || d > (b>>3))
3491 dev_warn(DEV, "Considerable difference in %s: %llus vs. %llus\n", s,
3492 (unsigned long long)a, (unsigned long long)b);
3493}
3494
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003495static int receive_sizes(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003496{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003497 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003498 struct p_sizes *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003499 enum determine_dev_size dd = unchanged;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003500 sector_t p_size, p_usize, my_usize;
3501 int ldsc = 0; /* local disk size changed */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003502 enum dds_flags ddsf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003503
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003504 mdev = vnr_to_mdev(tconn, pi->vnr);
3505 if (!mdev)
3506 return config_unknown_volume(tconn, pi);
3507
Philipp Reisnerb411b362009-09-25 16:07:19 -07003508 p_size = be64_to_cpu(p->d_size);
3509 p_usize = be64_to_cpu(p->u_size);
3510
Philipp Reisnerb411b362009-09-25 16:07:19 -07003511 /* just store the peer's disk size for now.
3512 * we still need to figure out whether we accept that. */
3513 mdev->p_size = p_size;
3514
Philipp Reisnerb411b362009-09-25 16:07:19 -07003515 if (get_ldev(mdev)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003516 rcu_read_lock();
3517 my_usize = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
3518 rcu_read_unlock();
3519
Philipp Reisnerb411b362009-09-25 16:07:19 -07003520 warn_if_differ_considerably(mdev, "lower level device sizes",
3521 p_size, drbd_get_max_capacity(mdev->ldev));
3522 warn_if_differ_considerably(mdev, "user requested size",
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003523 p_usize, my_usize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003524
3525 /* if this is the first connect, or an otherwise expected
3526 * param exchange, choose the minimum */
3527 if (mdev->state.conn == C_WF_REPORT_PARAMS)
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003528 p_usize = min_not_zero(my_usize, p_usize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003529
3530 /* Never shrink a device with usable data during connect.
3531 But allow online shrinking if we are connected. */
Philipp Reisneref5e44a2011-05-03 13:27:43 +02003532 if (drbd_new_dev_size(mdev, mdev->ldev, p_usize, 0) <
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003533 drbd_get_capacity(mdev->this_bdev) &&
3534 mdev->state.disk >= D_OUTDATED &&
3535 mdev->state.conn < C_CONNECTED) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003536 dev_err(DEV, "The peer's disk size is too small!\n");
Philipp Reisner38fa9982011-03-15 18:24:49 +01003537 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003538 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003539 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003540 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003541
3542 if (my_usize != p_usize) {
3543 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
3544
3545 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
3546 if (!new_disk_conf) {
3547 dev_err(DEV, "Allocation of new disk_conf failed\n");
3548 put_ldev(mdev);
3549 return -ENOMEM;
3550 }
3551
3552 mutex_lock(&mdev->tconn->conf_update);
3553 old_disk_conf = mdev->ldev->disk_conf;
3554 *new_disk_conf = *old_disk_conf;
3555 new_disk_conf->disk_size = p_usize;
3556
3557 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
3558 mutex_unlock(&mdev->tconn->conf_update);
3559 synchronize_rcu();
3560 kfree(old_disk_conf);
3561
3562 dev_info(DEV, "Peer sets u_size to %lu sectors\n",
3563 (unsigned long)my_usize);
3564 }
3565
Philipp Reisnerb411b362009-09-25 16:07:19 -07003566 put_ldev(mdev);
3567 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003568
Philipp Reisnere89b5912010-03-24 17:11:33 +01003569 ddsf = be16_to_cpu(p->dds_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003570 if (get_ldev(mdev)) {
Bart Van Assche24c48302011-05-21 18:32:29 +02003571 dd = drbd_determine_dev_size(mdev, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003572 put_ldev(mdev);
3573 if (dd == dev_size_error)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003574 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003575 drbd_md_sync(mdev);
3576 } else {
3577 /* I am diskless, need to accept the peer's size. */
3578 drbd_set_my_capacity(mdev, p_size);
3579 }
3580
Philipp Reisner99432fc2011-05-20 16:39:13 +02003581 mdev->peer_max_bio_size = be32_to_cpu(p->max_bio_size);
3582 drbd_reconsider_max_bio_size(mdev);
3583
Philipp Reisnerb411b362009-09-25 16:07:19 -07003584 if (get_ldev(mdev)) {
3585 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev)) {
3586 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
3587 ldsc = 1;
3588 }
3589
Philipp Reisnerb411b362009-09-25 16:07:19 -07003590 put_ldev(mdev);
3591 }
3592
3593 if (mdev->state.conn > C_WF_REPORT_PARAMS) {
3594 if (be64_to_cpu(p->c_size) !=
3595 drbd_get_capacity(mdev->this_bdev) || ldsc) {
3596 /* we have different sizes, probably peer
3597 * needs to know my new size... */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003598 drbd_send_sizes(mdev, 0, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003599 }
3600 if (test_and_clear_bit(RESIZE_PENDING, &mdev->flags) ||
3601 (dd == grew && mdev->state.conn == C_CONNECTED)) {
3602 if (mdev->state.pdsk >= D_INCONSISTENT &&
Philipp Reisnere89b5912010-03-24 17:11:33 +01003603 mdev->state.disk >= D_INCONSISTENT) {
3604 if (ddsf & DDSF_NO_RESYNC)
3605 dev_info(DEV, "Resync of new storage suppressed with --assume-clean\n");
3606 else
3607 resync_after_online_grow(mdev);
3608 } else
Philipp Reisnerb411b362009-09-25 16:07:19 -07003609 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
3610 }
3611 }
3612
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003613 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003614}
3615
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003616static int receive_uuids(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003617{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003618 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003619 struct p_uuids *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003620 u64 *p_uuid;
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003621 int i, updated_uuids = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003622
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003623 mdev = vnr_to_mdev(tconn, pi->vnr);
3624 if (!mdev)
3625 return config_unknown_volume(tconn, pi);
3626
Philipp Reisnerb411b362009-09-25 16:07:19 -07003627 p_uuid = kmalloc(sizeof(u64)*UI_EXTENDED_SIZE, GFP_NOIO);
3628
3629 for (i = UI_CURRENT; i < UI_EXTENDED_SIZE; i++)
3630 p_uuid[i] = be64_to_cpu(p->uuid[i]);
3631
3632 kfree(mdev->p_uuid);
3633 mdev->p_uuid = p_uuid;
3634
3635 if (mdev->state.conn < C_CONNECTED &&
3636 mdev->state.disk < D_INCONSISTENT &&
3637 mdev->state.role == R_PRIMARY &&
3638 (mdev->ed_uuid & ~((u64)1)) != (p_uuid[UI_CURRENT] & ~((u64)1))) {
3639 dev_err(DEV, "Can only connect to data with current UUID=%016llX\n",
3640 (unsigned long long)mdev->ed_uuid);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003641 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003642 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003643 }
3644
3645 if (get_ldev(mdev)) {
3646 int skip_initial_sync =
3647 mdev->state.conn == C_CONNECTED &&
Philipp Reisner31890f42011-01-19 14:12:51 +01003648 mdev->tconn->agreed_pro_version >= 90 &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003649 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED &&
3650 (p_uuid[UI_FLAGS] & 8);
3651 if (skip_initial_sync) {
3652 dev_info(DEV, "Accepted new current UUID, preparing to skip initial sync\n");
3653 drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003654 "clear_n_write from receive_uuids",
3655 BM_LOCKED_TEST_ALLOWED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003656 _drbd_uuid_set(mdev, UI_CURRENT, p_uuid[UI_CURRENT]);
3657 _drbd_uuid_set(mdev, UI_BITMAP, 0);
3658 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
3659 CS_VERBOSE, NULL);
3660 drbd_md_sync(mdev);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003661 updated_uuids = 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003662 }
3663 put_ldev(mdev);
Philipp Reisner18a50fa2010-06-21 14:14:15 +02003664 } else if (mdev->state.disk < D_INCONSISTENT &&
3665 mdev->state.role == R_PRIMARY) {
3666 /* I am a diskless primary, the peer just created a new current UUID
3667 for me. */
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003668 updated_uuids = drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003669 }
3670
3671 /* Before we test for the disk state, we should wait until an eventually
3672 ongoing cluster wide state change is finished. That is important if
3673 we are primary and are detaching from our disk. We need to see the
3674 new disk state... */
Philipp Reisner8410da82011-02-11 20:11:10 +01003675 mutex_lock(mdev->state_mutex);
3676 mutex_unlock(mdev->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003677 if (mdev->state.conn >= C_CONNECTED && mdev->state.disk < D_INCONSISTENT)
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003678 updated_uuids |= drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
3679
3680 if (updated_uuids)
3681 drbd_print_uuids(mdev, "receiver updated UUIDs to");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003682
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003683 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003684}
3685
3686/**
3687 * convert_state() - Converts the peer's view of the cluster state to our point of view
3688 * @ps: The state as seen by the peer.
3689 */
3690static union drbd_state convert_state(union drbd_state ps)
3691{
3692 union drbd_state ms;
3693
3694 static enum drbd_conns c_tab[] = {
Philipp Reisner369bea62011-07-06 23:04:44 +02003695 [C_WF_REPORT_PARAMS] = C_WF_REPORT_PARAMS,
Philipp Reisnerb411b362009-09-25 16:07:19 -07003696 [C_CONNECTED] = C_CONNECTED,
3697
3698 [C_STARTING_SYNC_S] = C_STARTING_SYNC_T,
3699 [C_STARTING_SYNC_T] = C_STARTING_SYNC_S,
3700 [C_DISCONNECTING] = C_TEAR_DOWN, /* C_NETWORK_FAILURE, */
3701 [C_VERIFY_S] = C_VERIFY_T,
3702 [C_MASK] = C_MASK,
3703 };
3704
3705 ms.i = ps.i;
3706
3707 ms.conn = c_tab[ps.conn];
3708 ms.peer = ps.role;
3709 ms.role = ps.peer;
3710 ms.pdsk = ps.disk;
3711 ms.disk = ps.pdsk;
3712 ms.peer_isp = (ps.aftr_isp | ps.user_isp);
3713
3714 return ms;
3715}
3716
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003717static int receive_req_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003718{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003719 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003720 struct p_req_state *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003721 union drbd_state mask, val;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01003722 enum drbd_state_rv rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003723
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003724 mdev = vnr_to_mdev(tconn, pi->vnr);
3725 if (!mdev)
3726 return -EIO;
3727
Philipp Reisnerb411b362009-09-25 16:07:19 -07003728 mask.i = be32_to_cpu(p->mask);
3729 val.i = be32_to_cpu(p->val);
3730
Philipp Reisner25703f82011-02-07 14:35:25 +01003731 if (test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags) &&
Philipp Reisner8410da82011-02-11 20:11:10 +01003732 mutex_is_locked(mdev->state_mutex)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003733 drbd_send_sr_reply(mdev, SS_CONCURRENT_ST_CHG);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003734 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003735 }
3736
3737 mask = convert_state(mask);
3738 val = convert_state(val);
3739
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003740 rv = drbd_change_state(mdev, CS_VERBOSE, mask, val);
3741 drbd_send_sr_reply(mdev, rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003742
Philipp Reisnerb411b362009-09-25 16:07:19 -07003743 drbd_md_sync(mdev);
3744
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003745 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003746}
3747
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003748static int receive_req_conn_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003749{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003750 struct p_req_state *p = pi->data;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003751 union drbd_state mask, val;
3752 enum drbd_state_rv rv;
3753
3754 mask.i = be32_to_cpu(p->mask);
3755 val.i = be32_to_cpu(p->val);
3756
3757 if (test_bit(DISCARD_CONCURRENT, &tconn->flags) &&
3758 mutex_is_locked(&tconn->cstate_mutex)) {
3759 conn_send_sr_reply(tconn, SS_CONCURRENT_ST_CHG);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003760 return 0;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003761 }
3762
3763 mask = convert_state(mask);
3764 val = convert_state(val);
3765
Philipp Reisner778bcf22011-03-28 12:55:03 +02003766 rv = conn_request_state(tconn, mask, val, CS_VERBOSE | CS_LOCAL_ONLY | CS_IGN_OUTD_FAIL);
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003767 conn_send_sr_reply(tconn, rv);
3768
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003769 return 0;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003770}
3771
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003772static int receive_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003773{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003774 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003775 struct p_state *p = pi->data;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003776 union drbd_state os, ns, peer_state;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003777 enum drbd_disk_state real_peer_disk;
Philipp Reisner65d922c2010-06-16 16:18:09 +02003778 enum chg_state_flags cs_flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003779 int rv;
3780
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003781 mdev = vnr_to_mdev(tconn, pi->vnr);
3782 if (!mdev)
3783 return config_unknown_volume(tconn, pi);
3784
Philipp Reisnerb411b362009-09-25 16:07:19 -07003785 peer_state.i = be32_to_cpu(p->state);
3786
3787 real_peer_disk = peer_state.disk;
3788 if (peer_state.disk == D_NEGOTIATING) {
3789 real_peer_disk = mdev->p_uuid[UI_FLAGS] & 4 ? D_INCONSISTENT : D_CONSISTENT;
3790 dev_info(DEV, "real peer disk state = %s\n", drbd_disk_str(real_peer_disk));
3791 }
3792
Philipp Reisner87eeee42011-01-19 14:16:30 +01003793 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003794 retry:
Philipp Reisner78bae592011-03-28 15:40:12 +02003795 os = ns = drbd_read_state(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01003796 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003797
Philipp Reisnerb8853db2011-12-13 11:09:16 +01003798 /* If some other part of the code (asender thread, timeout)
3799 * already decided to close the connection again,
3800 * we must not "re-establish" it here. */
3801 if (os.conn <= C_TEAR_DOWN)
3802 return false;
3803
Philipp Reisner9bcd2522011-09-29 13:00:14 +02003804 /* If this is the "end of sync" confirmation, usually the peer disk
3805 * transitions from D_INCONSISTENT to D_UP_TO_DATE. For empty (0 bits
3806 * set) resync started in PausedSyncT, or if the timing of pause-/
3807 * unpause-sync events has been "just right", the peer disk may
3808 * transition from D_CONSISTENT to D_UP_TO_DATE as well.
3809 */
3810 if ((os.pdsk == D_INCONSISTENT || os.pdsk == D_CONSISTENT) &&
3811 real_peer_disk == D_UP_TO_DATE &&
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003812 os.conn > C_CONNECTED && os.disk == D_UP_TO_DATE) {
3813 /* If we are (becoming) SyncSource, but peer is still in sync
3814 * preparation, ignore its uptodate-ness to avoid flapping, it
3815 * will change to inconsistent once the peer reaches active
3816 * syncing states.
3817 * It may have changed syncer-paused flags, however, so we
3818 * cannot ignore this completely. */
3819 if (peer_state.conn > C_CONNECTED &&
3820 peer_state.conn < C_SYNC_SOURCE)
3821 real_peer_disk = D_INCONSISTENT;
3822
3823 /* if peer_state changes to connected at the same time,
3824 * it explicitly notifies us that it finished resync.
3825 * Maybe we should finish it up, too? */
3826 else if (os.conn >= C_SYNC_SOURCE &&
3827 peer_state.conn == C_CONNECTED) {
3828 if (drbd_bm_total_weight(mdev) <= mdev->rs_failed)
3829 drbd_resync_finished(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003830 return 0;
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003831 }
3832 }
3833
3834 /* peer says his disk is inconsistent, while we think it is uptodate,
3835 * and this happens while the peer still thinks we have a sync going on,
3836 * but we think we are already done with the sync.
3837 * We ignore this to avoid flapping pdsk.
3838 * This should not happen, if the peer is a recent version of drbd. */
3839 if (os.pdsk == D_UP_TO_DATE && real_peer_disk == D_INCONSISTENT &&
3840 os.conn == C_CONNECTED && peer_state.conn > C_SYNC_SOURCE)
3841 real_peer_disk = D_UP_TO_DATE;
3842
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003843 if (ns.conn == C_WF_REPORT_PARAMS)
3844 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003845
Philipp Reisner67531712010-10-27 12:21:30 +02003846 if (peer_state.conn == C_AHEAD)
3847 ns.conn = C_BEHIND;
3848
Philipp Reisnerb411b362009-09-25 16:07:19 -07003849 if (mdev->p_uuid && peer_state.disk >= D_NEGOTIATING &&
3850 get_ldev_if_state(mdev, D_NEGOTIATING)) {
3851 int cr; /* consider resync */
3852
3853 /* if we established a new connection */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003854 cr = (os.conn < C_CONNECTED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003855 /* if we had an established connection
3856 * and one of the nodes newly attaches a disk */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003857 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003858 (peer_state.disk == D_NEGOTIATING ||
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003859 os.disk == D_NEGOTIATING));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003860 /* if we have both been inconsistent, and the peer has been
3861 * forced to be UpToDate with --overwrite-data */
3862 cr |= test_bit(CONSIDER_RESYNC, &mdev->flags);
3863 /* if we had been plain connected, and the admin requested to
3864 * start a sync by "invalidate" or "invalidate-remote" */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003865 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003866 (peer_state.conn >= C_STARTING_SYNC_S &&
3867 peer_state.conn <= C_WF_BITMAP_T));
3868
3869 if (cr)
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003870 ns.conn = drbd_sync_handshake(mdev, peer_state.role, real_peer_disk);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003871
3872 put_ldev(mdev);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003873 if (ns.conn == C_MASK) {
3874 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003875 if (mdev->state.disk == D_NEGOTIATING) {
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02003876 drbd_force_state(mdev, NS(disk, D_FAILED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003877 } else if (peer_state.disk == D_NEGOTIATING) {
3878 dev_err(DEV, "Disk attach process on the peer node was aborted.\n");
3879 peer_state.disk = D_DISKLESS;
Lars Ellenberg580b9762010-02-26 23:15:23 +01003880 real_peer_disk = D_DISKLESS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003881 } else {
Philipp Reisner8169e412011-03-15 18:40:27 +01003882 if (test_and_clear_bit(CONN_DRY_RUN, &mdev->tconn->flags))
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003883 return -EIO;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003884 D_ASSERT(os.conn == C_WF_REPORT_PARAMS);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003885 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003886 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003887 }
3888 }
3889 }
3890
Philipp Reisner87eeee42011-01-19 14:16:30 +01003891 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner78bae592011-03-28 15:40:12 +02003892 if (os.i != drbd_read_state(mdev).i)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003893 goto retry;
3894 clear_bit(CONSIDER_RESYNC, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003895 ns.peer = peer_state.role;
3896 ns.pdsk = real_peer_disk;
3897 ns.peer_isp = (peer_state.aftr_isp | peer_state.user_isp);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003898 if ((ns.conn == C_CONNECTED || ns.conn == C_WF_BITMAP_S) && ns.disk == D_NEGOTIATING)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003899 ns.disk = mdev->new_state_tmp.disk;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003900 cs_flags = CS_VERBOSE + (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED ? 0 : CS_HARD);
Philipp Reisner2aebfab2011-03-28 16:48:11 +02003901 if (ns.pdsk == D_CONSISTENT && drbd_suspended(mdev) && ns.conn == C_CONNECTED && os.conn < C_CONNECTED &&
Philipp Reisner481c6f52010-06-22 14:03:27 +02003902 test_bit(NEW_CUR_UUID, &mdev->flags)) {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01003903 /* Do not allow tl_restart(RESEND) for a rebooted peer. We can only allow this
Philipp Reisner481c6f52010-06-22 14:03:27 +02003904 for temporal network outages! */
Philipp Reisner87eeee42011-01-19 14:16:30 +01003905 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003906 dev_err(DEV, "Aborting Connect, can not thaw IO with an only Consistent peer\n");
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01003907 tl_clear(mdev->tconn);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003908 drbd_uuid_new_current(mdev);
3909 clear_bit(NEW_CUR_UUID, &mdev->flags);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003910 conn_request_state(mdev->tconn, NS2(conn, C_PROTOCOL_ERROR, susp, 0), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003911 return -EIO;
Philipp Reisner481c6f52010-06-22 14:03:27 +02003912 }
Philipp Reisner65d922c2010-06-16 16:18:09 +02003913 rv = _drbd_set_state(mdev, ns, cs_flags, NULL);
Philipp Reisner78bae592011-03-28 15:40:12 +02003914 ns = drbd_read_state(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01003915 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003916
3917 if (rv < SS_SUCCESS) {
Philipp Reisner38fa9982011-03-15 18:24:49 +01003918 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003919 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003920 }
3921
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003922 if (os.conn > C_WF_REPORT_PARAMS) {
3923 if (ns.conn > C_CONNECTED && peer_state.conn <= C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003924 peer_state.disk != D_NEGOTIATING ) {
3925 /* we want resync, peer has not yet decided to sync... */
3926 /* Nowadays only used when forcing a node into primary role and
3927 setting its disk to UpToDate with that */
3928 drbd_send_uuids(mdev);
Philipp Reisner43de7c82011-11-10 13:16:13 +01003929 drbd_send_current_state(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003930 }
3931 }
3932
Philipp Reisner08b165b2011-09-05 16:22:33 +02003933 clear_bit(DISCARD_MY_DATA, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003934
3935 drbd_md_sync(mdev); /* update connected indicator, la_size, ... */
3936
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003937 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003938}
3939
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003940static int receive_sync_uuid(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003941{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003942 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003943 struct p_rs_uuid *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003944
3945 mdev = vnr_to_mdev(tconn, pi->vnr);
3946 if (!mdev)
3947 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003948
3949 wait_event(mdev->misc_wait,
3950 mdev->state.conn == C_WF_SYNC_UUID ||
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02003951 mdev->state.conn == C_BEHIND ||
Philipp Reisnerb411b362009-09-25 16:07:19 -07003952 mdev->state.conn < C_CONNECTED ||
3953 mdev->state.disk < D_NEGOTIATING);
3954
3955 /* D_ASSERT( mdev->state.conn == C_WF_SYNC_UUID ); */
3956
Philipp Reisnerb411b362009-09-25 16:07:19 -07003957 /* Here the _drbd_uuid_ functions are right, current should
3958 _not_ be rotated into the history */
3959 if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
3960 _drbd_uuid_set(mdev, UI_CURRENT, be64_to_cpu(p->uuid));
3961 _drbd_uuid_set(mdev, UI_BITMAP, 0UL);
3962
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003963 drbd_print_uuids(mdev, "updated sync uuid");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003964 drbd_start_resync(mdev, C_SYNC_TARGET);
3965
3966 put_ldev(mdev);
3967 } else
3968 dev_err(DEV, "Ignoring SyncUUID packet!\n");
3969
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003970 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003971}
3972
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003973/**
3974 * receive_bitmap_plain
3975 *
3976 * Return 0 when done, 1 when another iteration is needed, and a negative error
3977 * code upon failure.
3978 */
3979static int
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003980receive_bitmap_plain(struct drbd_conf *mdev, unsigned int size,
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003981 unsigned long *p, struct bm_xfer_ctx *c)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003982{
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003983 unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE -
3984 drbd_header_size(mdev->tconn);
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003985 unsigned int num_words = min_t(size_t, data_size / sizeof(*p),
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003986 c->bm_words - c->word_offset);
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003987 unsigned int want = num_words * sizeof(*p);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003988 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003989
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003990 if (want != size) {
3991 dev_err(DEV, "%s:want (%u) != size (%u)\n", __func__, want, size);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003992 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003993 }
3994 if (want == 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003995 return 0;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003996 err = drbd_recv_all(mdev->tconn, p, want);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003997 if (err)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003998 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003999
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004000 drbd_bm_merge_lel(mdev, c->word_offset, num_words, p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004001
4002 c->word_offset += num_words;
4003 c->bit_offset = c->word_offset * BITS_PER_LONG;
4004 if (c->bit_offset > c->bm_bits)
4005 c->bit_offset = c->bm_bits;
4006
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004007 return 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004008}
4009
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004010static enum drbd_bitmap_code dcbp_get_code(struct p_compressed_bm *p)
4011{
4012 return (enum drbd_bitmap_code)(p->encoding & 0x0f);
4013}
4014
4015static int dcbp_get_start(struct p_compressed_bm *p)
4016{
4017 return (p->encoding & 0x80) != 0;
4018}
4019
4020static int dcbp_get_pad_bits(struct p_compressed_bm *p)
4021{
4022 return (p->encoding >> 4) & 0x7;
4023}
4024
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004025/**
4026 * recv_bm_rle_bits
4027 *
4028 * Return 0 when done, 1 when another iteration is needed, and a negative error
4029 * code upon failure.
4030 */
4031static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07004032recv_bm_rle_bits(struct drbd_conf *mdev,
4033 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01004034 struct bm_xfer_ctx *c,
4035 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004036{
4037 struct bitstream bs;
4038 u64 look_ahead;
4039 u64 rl;
4040 u64 tmp;
4041 unsigned long s = c->bit_offset;
4042 unsigned long e;
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004043 int toggle = dcbp_get_start(p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004044 int have;
4045 int bits;
4046
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004047 bitstream_init(&bs, p->code, len, dcbp_get_pad_bits(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07004048
4049 bits = bitstream_get_bits(&bs, &look_ahead, 64);
4050 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004051 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004052
4053 for (have = bits; have > 0; s += rl, toggle = !toggle) {
4054 bits = vli_decode_bits(&rl, look_ahead);
4055 if (bits <= 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004056 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004057
4058 if (toggle) {
4059 e = s + rl -1;
4060 if (e >= c->bm_bits) {
4061 dev_err(DEV, "bitmap overflow (e:%lu) while decoding bm RLE packet\n", e);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004062 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004063 }
4064 _drbd_bm_set_bits(mdev, s, e);
4065 }
4066
4067 if (have < bits) {
4068 dev_err(DEV, "bitmap decoding error: h:%d b:%d la:0x%08llx l:%u/%u\n",
4069 have, bits, look_ahead,
4070 (unsigned int)(bs.cur.b - p->code),
4071 (unsigned int)bs.buf_len);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004072 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004073 }
4074 look_ahead >>= bits;
4075 have -= bits;
4076
4077 bits = bitstream_get_bits(&bs, &tmp, 64 - have);
4078 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004079 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004080 look_ahead |= tmp << have;
4081 have += bits;
4082 }
4083
4084 c->bit_offset = s;
4085 bm_xfer_ctx_bit_to_word_offset(c);
4086
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004087 return (s != c->bm_bits);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004088}
4089
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004090/**
4091 * decode_bitmap_c
4092 *
4093 * Return 0 when done, 1 when another iteration is needed, and a negative error
4094 * code upon failure.
4095 */
4096static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07004097decode_bitmap_c(struct drbd_conf *mdev,
4098 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01004099 struct bm_xfer_ctx *c,
4100 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004101{
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004102 if (dcbp_get_code(p) == RLE_VLI_Bits)
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004103 return recv_bm_rle_bits(mdev, p, c, len - sizeof(*p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07004104
4105 /* other variants had been implemented for evaluation,
4106 * but have been dropped as this one turned out to be "best"
4107 * during all our tests. */
4108
4109 dev_err(DEV, "receive_bitmap_c: unknown encoding %u\n", p->encoding);
Philipp Reisner38fa9982011-03-15 18:24:49 +01004110 conn_request_state(mdev->tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004111 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004112}
4113
4114void INFO_bm_xfer_stats(struct drbd_conf *mdev,
4115 const char *direction, struct bm_xfer_ctx *c)
4116{
4117 /* what would it take to transfer it "plaintext" */
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004118 unsigned int header_size = drbd_header_size(mdev->tconn);
4119 unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE - header_size;
4120 unsigned int plain =
4121 header_size * (DIV_ROUND_UP(c->bm_words, data_size) + 1) +
4122 c->bm_words * sizeof(unsigned long);
4123 unsigned int total = c->bytes[0] + c->bytes[1];
4124 unsigned int r;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004125
4126 /* total can not be zero. but just in case: */
4127 if (total == 0)
4128 return;
4129
4130 /* don't report if not compressed */
4131 if (total >= plain)
4132 return;
4133
4134 /* total < plain. check for overflow, still */
4135 r = (total > UINT_MAX/1000) ? (total / (plain/1000))
4136 : (1000 * total / plain);
4137
4138 if (r > 1000)
4139 r = 1000;
4140
4141 r = 1000 - r;
4142 dev_info(DEV, "%s bitmap stats [Bytes(packets)]: plain %u(%u), RLE %u(%u), "
4143 "total %u; compression: %u.%u%%\n",
4144 direction,
4145 c->bytes[1], c->packets[1],
4146 c->bytes[0], c->packets[0],
4147 total, r/10, r % 10);
4148}
4149
4150/* Since we are processing the bitfield from lower addresses to higher,
4151 it does not matter if the process it in 32 bit chunks or 64 bit
4152 chunks as long as it is little endian. (Understand it as byte stream,
4153 beginning with the lowest byte...) If we would use big endian
4154 we would need to process it from the highest address to the lowest,
4155 in order to be agnostic to the 32 vs 64 bits issue.
4156
4157 returns 0 on failure, 1 if we successfully received it. */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004158static int receive_bitmap(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004159{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004160 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004161 struct bm_xfer_ctx c;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004162 int err;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004163
4164 mdev = vnr_to_mdev(tconn, pi->vnr);
4165 if (!mdev)
4166 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004167
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004168 drbd_bm_lock(mdev, "receive bitmap", BM_LOCKED_SET_ALLOWED);
4169 /* you are supposed to send additional out-of-sync information
4170 * if you actually set bits during this phase */
Philipp Reisnerb411b362009-09-25 16:07:19 -07004171
Philipp Reisnerb411b362009-09-25 16:07:19 -07004172 c = (struct bm_xfer_ctx) {
4173 .bm_bits = drbd_bm_bits(mdev),
4174 .bm_words = drbd_bm_words(mdev),
4175 };
4176
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004177 for(;;) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004178 if (pi->cmd == P_BITMAP)
4179 err = receive_bitmap_plain(mdev, pi->size, pi->data, &c);
4180 else if (pi->cmd == P_COMPRESSED_BITMAP) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004181 /* MAYBE: sanity check that we speak proto >= 90,
4182 * and the feature is enabled! */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004183 struct p_compressed_bm *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004184
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004185 if (pi->size > DRBD_SOCKET_BUFFER_SIZE - drbd_header_size(tconn)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004186 dev_err(DEV, "ReportCBitmap packet too large\n");
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004187 err = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004188 goto out;
4189 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004190 if (pi->size <= sizeof(*p)) {
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004191 dev_err(DEV, "ReportCBitmap packet too small (l:%u)\n", pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004192 err = -EIO;
Andreas Gruenbacher78fcbda2010-12-10 22:18:27 +01004193 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004194 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004195 err = drbd_recv_all(mdev->tconn, p, pi->size);
4196 if (err)
4197 goto out;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004198 err = decode_bitmap_c(mdev, p, &c, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004199 } else {
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004200 dev_warn(DEV, "receive_bitmap: cmd neither ReportBitMap nor ReportCBitMap (is 0x%x)", pi->cmd);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004201 err = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004202 goto out;
4203 }
4204
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004205 c.packets[pi->cmd == P_BITMAP]++;
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004206 c.bytes[pi->cmd == P_BITMAP] += drbd_header_size(tconn) + pi->size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004207
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004208 if (err <= 0) {
4209 if (err < 0)
4210 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004211 break;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004212 }
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004213 err = drbd_recv_header(mdev->tconn, pi);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004214 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004215 goto out;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004216 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004217
4218 INFO_bm_xfer_stats(mdev, "receive", &c);
4219
4220 if (mdev->state.conn == C_WF_BITMAP_T) {
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01004221 enum drbd_state_rv rv;
4222
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004223 err = drbd_send_bitmap(mdev);
4224 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004225 goto out;
4226 /* Omit CS_ORDERED with this state transition to avoid deadlocks. */
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01004227 rv = _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
4228 D_ASSERT(rv == SS_SUCCESS);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004229 } else if (mdev->state.conn != C_WF_BITMAP_S) {
4230 /* admin may have requested C_DISCONNECTING,
4231 * other threads may have noticed network errors */
4232 dev_info(DEV, "unexpected cstate (%s) in receive_bitmap\n",
4233 drbd_conn_str(mdev->state.conn));
4234 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004235 err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004236
Philipp Reisnerb411b362009-09-25 16:07:19 -07004237 out:
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004238 drbd_bm_unlock(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004239 if (!err && mdev->state.conn == C_WF_BITMAP_S)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004240 drbd_start_resync(mdev, C_SYNC_SOURCE);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004241 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004242}
4243
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004244static int receive_skip(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004245{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004246 conn_warn(tconn, "skipping unknown optional packet type %d, l: %d!\n",
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004247 pi->cmd, pi->size);
Philipp Reisner2de876e2011-03-15 14:38:01 +01004248
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004249 return ignore_remaining_packet(tconn, pi);
Philipp Reisner2de876e2011-03-15 14:38:01 +01004250}
4251
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004252static int receive_UnplugRemote(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004253{
Philipp Reisnerb411b362009-09-25 16:07:19 -07004254 /* Make sure we've acked all the TCP data associated
4255 * with the data requests being unplugged */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004256 drbd_tcp_quickack(tconn->data.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004257
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004258 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004259}
4260
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004261static int receive_out_of_sync(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner73a01a12010-10-27 14:33:00 +02004262{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004263 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004264 struct p_block_desc *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004265
4266 mdev = vnr_to_mdev(tconn, pi->vnr);
4267 if (!mdev)
4268 return -EIO;
Philipp Reisner73a01a12010-10-27 14:33:00 +02004269
Lars Ellenbergf735e3632010-12-17 21:06:18 +01004270 switch (mdev->state.conn) {
4271 case C_WF_SYNC_UUID:
4272 case C_WF_BITMAP_T:
4273 case C_BEHIND:
4274 break;
4275 default:
4276 dev_err(DEV, "ASSERT FAILED cstate = %s, expected: WFSyncUUID|WFBitMapT|Behind\n",
4277 drbd_conn_str(mdev->state.conn));
4278 }
4279
Philipp Reisner73a01a12010-10-27 14:33:00 +02004280 drbd_set_out_of_sync(mdev, be64_to_cpu(p->sector), be32_to_cpu(p->blksize));
4281
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004282 return 0;
Philipp Reisner73a01a12010-10-27 14:33:00 +02004283}
4284
Philipp Reisner02918be2010-08-20 14:35:10 +02004285struct data_cmd {
4286 int expect_payload;
4287 size_t pkt_size;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004288 int (*fn)(struct drbd_tconn *, struct packet_info *);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004289};
4290
Philipp Reisner02918be2010-08-20 14:35:10 +02004291static struct data_cmd drbd_cmd_handler[] = {
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004292 [P_DATA] = { 1, sizeof(struct p_data), receive_Data },
4293 [P_DATA_REPLY] = { 1, sizeof(struct p_data), receive_DataReply },
4294 [P_RS_DATA_REPLY] = { 1, sizeof(struct p_data), receive_RSDataReply } ,
4295 [P_BARRIER] = { 0, sizeof(struct p_barrier), receive_Barrier } ,
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004296 [P_BITMAP] = { 1, 0, receive_bitmap } ,
4297 [P_COMPRESSED_BITMAP] = { 1, 0, receive_bitmap } ,
4298 [P_UNPLUG_REMOTE] = { 0, 0, receive_UnplugRemote },
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004299 [P_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
4300 [P_RS_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004301 [P_SYNC_PARAM] = { 1, 0, receive_SyncParam },
4302 [P_SYNC_PARAM89] = { 1, 0, receive_SyncParam },
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004303 [P_PROTOCOL] = { 1, sizeof(struct p_protocol), receive_protocol },
4304 [P_UUIDS] = { 0, sizeof(struct p_uuids), receive_uuids },
4305 [P_SIZES] = { 0, sizeof(struct p_sizes), receive_sizes },
4306 [P_STATE] = { 0, sizeof(struct p_state), receive_state },
4307 [P_STATE_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_state },
4308 [P_SYNC_UUID] = { 0, sizeof(struct p_rs_uuid), receive_sync_uuid },
4309 [P_OV_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
4310 [P_OV_REPLY] = { 1, sizeof(struct p_block_req), receive_DataRequest },
4311 [P_CSUM_RS_REQUEST] = { 1, sizeof(struct p_block_req), receive_DataRequest },
4312 [P_DELAY_PROBE] = { 0, sizeof(struct p_delay_probe93), receive_skip },
4313 [P_OUT_OF_SYNC] = { 0, sizeof(struct p_block_desc), receive_out_of_sync },
4314 [P_CONN_ST_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_conn_state },
Philipp Reisner036b17e2011-05-16 17:38:11 +02004315 [P_PROTOCOL_UPDATE] = { 1, sizeof(struct p_protocol), receive_protocol },
Philipp Reisner02918be2010-08-20 14:35:10 +02004316};
4317
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004318static void drbdd(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004319{
Philipp Reisner77351055b2011-02-07 17:24:26 +01004320 struct packet_info pi;
Philipp Reisner02918be2010-08-20 14:35:10 +02004321 size_t shs; /* sub header size */
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004322 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004323
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004324 while (get_t_state(&tconn->receiver) == RUNNING) {
Andreas Gruenbacherdeebe192011-03-25 00:01:04 +01004325 struct data_cmd *cmd;
4326
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004327 drbd_thread_current_set_cpu(&tconn->receiver);
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004328 if (drbd_recv_header(tconn, &pi))
Philipp Reisner02918be2010-08-20 14:35:10 +02004329 goto err_out;
4330
Andreas Gruenbacherdeebe192011-03-25 00:01:04 +01004331 cmd = &drbd_cmd_handler[pi.cmd];
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004332 if (unlikely(pi.cmd >= ARRAY_SIZE(drbd_cmd_handler) || !cmd->fn)) {
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004333 conn_err(tconn, "Unexpected data packet %s (0x%04x)",
4334 cmdname(pi.cmd), pi.cmd);
Philipp Reisner02918be2010-08-20 14:35:10 +02004335 goto err_out;
Lars Ellenberg0b33a912009-11-16 15:58:04 +01004336 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004337
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004338 shs = cmd->pkt_size;
4339 if (pi.size > shs && !cmd->expect_payload) {
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004340 conn_err(tconn, "No payload expected %s l:%d\n",
4341 cmdname(pi.cmd), pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02004342 goto err_out;
4343 }
4344
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004345 if (shs) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004346 err = drbd_recv_all_warn(tconn, pi.data, shs);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004347 if (err)
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004348 goto err_out;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004349 pi.size -= shs;
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004350 }
4351
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004352 err = cmd->fn(tconn, &pi);
4353 if (err) {
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004354 conn_err(tconn, "error receiving %s, e: %d l: %d!\n",
4355 cmdname(pi.cmd), err, pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02004356 goto err_out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004357 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004358 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004359 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004360
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004361 err_out:
4362 conn_request_state(tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004363}
4364
Philipp Reisner0e29d162011-02-18 14:23:11 +01004365void conn_flush_workqueue(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004366{
4367 struct drbd_wq_barrier barr;
4368
4369 barr.w.cb = w_prev_work_done;
Philipp Reisner0e29d162011-02-18 14:23:11 +01004370 barr.w.tconn = tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004371 init_completion(&barr.done);
Philipp Reisner0e29d162011-02-18 14:23:11 +01004372 drbd_queue_work(&tconn->data.work, &barr.w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004373 wait_for_completion(&barr.done);
4374}
4375
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004376static void conn_disconnect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004377{
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004378 struct drbd_conf *mdev;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004379 enum drbd_conns oc;
Philipp Reisner376694a2011-11-07 10:54:28 +01004380 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004381
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004382 if (tconn->cstate == C_STANDALONE)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004383 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004384
Philipp Reisnerb8853db2011-12-13 11:09:16 +01004385 /* We are about to start the cleanup after connection loss.
4386 * Make sure drbd_make_request knows about that.
4387 * Usually we should be in some network failure state already,
4388 * but just in case we are not, we fix it up here.
4389 */
4390 conn_request_state(tconn, NS(conn, C_NETWORK_FAILURE), CS_HARD);
4391
Philipp Reisnerb411b362009-09-25 16:07:19 -07004392 /* asender does not clean up anything. it must not interfere, either */
Philipp Reisner360cc742011-02-08 14:29:53 +01004393 drbd_thread_stop(&tconn->asender);
4394 drbd_free_sock(tconn);
4395
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004396 rcu_read_lock();
4397 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
4398 kref_get(&mdev->kref);
4399 rcu_read_unlock();
4400 drbd_disconnected(mdev);
4401 kref_put(&mdev->kref, &drbd_minor_destroy);
4402 rcu_read_lock();
4403 }
4404 rcu_read_unlock();
4405
Philipp Reisner12038a32011-11-09 19:18:00 +01004406 if (!list_empty(&tconn->current_epoch->list))
4407 conn_err(tconn, "ASSERTION FAILED: tconn->current_epoch->list not empty\n");
4408 /* ok, no more ee's on the fly, it is safe to reset the epoch_size */
4409 atomic_set(&tconn->current_epoch->epoch_size, 0);
4410
Philipp Reisner360cc742011-02-08 14:29:53 +01004411 conn_info(tconn, "Connection closed\n");
4412
Philipp Reisnercb703452011-03-24 11:03:07 +01004413 if (conn_highest_role(tconn) == R_PRIMARY && conn_highest_pdsk(tconn) >= D_UNKNOWN)
4414 conn_try_outdate_peer_async(tconn);
4415
Philipp Reisner360cc742011-02-08 14:29:53 +01004416 spin_lock_irq(&tconn->req_lock);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004417 oc = tconn->cstate;
4418 if (oc >= C_UNCONNECTED)
Philipp Reisner376694a2011-11-07 10:54:28 +01004419 _conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004420
Philipp Reisner360cc742011-02-08 14:29:53 +01004421 spin_unlock_irq(&tconn->req_lock);
4422
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02004423 if (oc == C_DISCONNECTING)
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +02004424 conn_request_state(tconn, NS(conn, C_STANDALONE), CS_VERBOSE | CS_HARD);
Philipp Reisner360cc742011-02-08 14:29:53 +01004425}
4426
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004427static int drbd_disconnected(struct drbd_conf *mdev)
Philipp Reisner360cc742011-02-08 14:29:53 +01004428{
Philipp Reisner360cc742011-02-08 14:29:53 +01004429 unsigned int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004430
Philipp Reisner85719572010-07-21 10:20:17 +02004431 /* wait for current activity to cease. */
Philipp Reisner87eeee42011-01-19 14:16:30 +01004432 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004433 _drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
4434 _drbd_wait_ee_list_empty(mdev, &mdev->sync_ee);
4435 _drbd_wait_ee_list_empty(mdev, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004436 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004437
4438 /* We do not have data structures that would allow us to
4439 * get the rs_pending_cnt down to 0 again.
4440 * * On C_SYNC_TARGET we do not have any data structures describing
4441 * the pending RSDataRequest's we have sent.
4442 * * On C_SYNC_SOURCE there is no data structure that tracks
4443 * the P_RS_DATA_REPLY blocks that we sent to the SyncTarget.
4444 * And no, it is not the sum of the reference counts in the
4445 * resync_LRU. The resync_LRU tracks the whole operation including
4446 * the disk-IO, while the rs_pending_cnt only tracks the blocks
4447 * on the fly. */
4448 drbd_rs_cancel_all(mdev);
4449 mdev->rs_total = 0;
4450 mdev->rs_failed = 0;
4451 atomic_set(&mdev->rs_pending_cnt, 0);
4452 wake_up(&mdev->misc_wait);
4453
Philipp Reisnerb411b362009-09-25 16:07:19 -07004454 del_timer_sync(&mdev->resync_timer);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004455 resync_timer_fn((unsigned long)mdev);
4456
Philipp Reisnerb411b362009-09-25 16:07:19 -07004457 /* wait for all w_e_end_data_req, w_e_end_rsdata_req, w_send_barrier,
4458 * w_make_resync_request etc. which may still be on the worker queue
4459 * to be "canceled" */
Philipp Reisnera21e9292011-02-08 15:08:49 +01004460 drbd_flush_workqueue(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004461
Andreas Gruenbachera990be42011-04-06 17:56:48 +02004462 drbd_finish_peer_reqs(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004463
Philipp Reisnerd10b4ea2011-11-30 23:25:36 +01004464 /* This second workqueue flush is necessary, since drbd_finish_peer_reqs()
4465 might have issued a work again. The one before drbd_finish_peer_reqs() is
4466 necessary to reclain net_ee in drbd_finish_peer_reqs(). */
4467 drbd_flush_workqueue(mdev);
4468
Philipp Reisnerb411b362009-09-25 16:07:19 -07004469 kfree(mdev->p_uuid);
4470 mdev->p_uuid = NULL;
4471
Philipp Reisner2aebfab2011-03-28 16:48:11 +02004472 if (!drbd_suspended(mdev))
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01004473 tl_clear(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004474
Philipp Reisnerb411b362009-09-25 16:07:19 -07004475 drbd_md_sync(mdev);
4476
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004477 /* serialize with bitmap writeout triggered by the state change,
4478 * if any. */
4479 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
4480
Philipp Reisnerb411b362009-09-25 16:07:19 -07004481 /* tcp_close and release of sendpage pages can be deferred. I don't
4482 * want to use SO_LINGER, because apparently it can be deferred for
4483 * more than 20 seconds (longest time I checked).
4484 *
4485 * Actually we don't care for exactly when the network stack does its
4486 * put_page(), but release our reference on these pages right here.
4487 */
Andreas Gruenbacher7721f562011-04-06 17:14:02 +02004488 i = drbd_free_peer_reqs(mdev, &mdev->net_ee);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004489 if (i)
4490 dev_info(DEV, "net_ee not empty, killed %u entries\n", i);
Lars Ellenberg435f0742010-09-06 12:30:25 +02004491 i = atomic_read(&mdev->pp_in_use_by_net);
4492 if (i)
4493 dev_info(DEV, "pp_in_use_by_net = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004494 i = atomic_read(&mdev->pp_in_use);
4495 if (i)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02004496 dev_info(DEV, "pp_in_use = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004497
4498 D_ASSERT(list_empty(&mdev->read_ee));
4499 D_ASSERT(list_empty(&mdev->active_ee));
4500 D_ASSERT(list_empty(&mdev->sync_ee));
4501 D_ASSERT(list_empty(&mdev->done_ee));
4502
Philipp Reisner360cc742011-02-08 14:29:53 +01004503 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004504}
4505
4506/*
4507 * We support PRO_VERSION_MIN to PRO_VERSION_MAX. The protocol version
4508 * we can agree on is stored in agreed_pro_version.
4509 *
4510 * feature flags and the reserved array should be enough room for future
4511 * enhancements of the handshake protocol, and possible plugins...
4512 *
4513 * for now, they are expected to be zero, but ignored.
4514 */
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004515static int drbd_send_features(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004516{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004517 struct drbd_socket *sock;
4518 struct p_connection_features *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004519
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004520 sock = &tconn->data;
4521 p = conn_prepare_command(tconn, sock);
4522 if (!p)
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004523 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004524 memset(p, 0, sizeof(*p));
4525 p->protocol_min = cpu_to_be32(PRO_VERSION_MIN);
4526 p->protocol_max = cpu_to_be32(PRO_VERSION_MAX);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004527 return conn_send_command(tconn, sock, P_CONNECTION_FEATURES, sizeof(*p), NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004528}
4529
4530/*
4531 * return values:
4532 * 1 yes, we have a valid connection
4533 * 0 oops, did not work out, please try again
4534 * -1 peer talks different language,
4535 * no point in trying again, please go standalone.
4536 */
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004537static int drbd_do_features(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004538{
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004539 /* ASSERT current == tconn->receiver ... */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004540 struct p_connection_features *p;
4541 const int expect = sizeof(struct p_connection_features);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004542 struct packet_info pi;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004543 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004544
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004545 err = drbd_send_features(tconn);
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004546 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004547 return 0;
4548
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004549 err = drbd_recv_header(tconn, &pi);
4550 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004551 return 0;
4552
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004553 if (pi.cmd != P_CONNECTION_FEATURES) {
4554 conn_err(tconn, "expected ConnectionFeatures packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004555 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004556 return -1;
4557 }
4558
Philipp Reisner77351055b2011-02-07 17:24:26 +01004559 if (pi.size != expect) {
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004560 conn_err(tconn, "expected ConnectionFeatures length: %u, received: %u\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004561 expect, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004562 return -1;
4563 }
4564
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004565 p = pi.data;
4566 err = drbd_recv_all_warn(tconn, p, expect);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004567 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004568 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004569
Philipp Reisnerb411b362009-09-25 16:07:19 -07004570 p->protocol_min = be32_to_cpu(p->protocol_min);
4571 p->protocol_max = be32_to_cpu(p->protocol_max);
4572 if (p->protocol_max == 0)
4573 p->protocol_max = p->protocol_min;
4574
4575 if (PRO_VERSION_MAX < p->protocol_min ||
4576 PRO_VERSION_MIN > p->protocol_max)
4577 goto incompat;
4578
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004579 tconn->agreed_pro_version = min_t(int, PRO_VERSION_MAX, p->protocol_max);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004580
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004581 conn_info(tconn, "Handshake successful: "
4582 "Agreed network protocol version %d\n", tconn->agreed_pro_version);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004583
4584 return 1;
4585
4586 incompat:
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004587 conn_err(tconn, "incompatible DRBD dialects: "
Philipp Reisnerb411b362009-09-25 16:07:19 -07004588 "I support %d-%d, peer supports %d-%d\n",
4589 PRO_VERSION_MIN, PRO_VERSION_MAX,
4590 p->protocol_min, p->protocol_max);
4591 return -1;
4592}
4593
4594#if !defined(CONFIG_CRYPTO_HMAC) && !defined(CONFIG_CRYPTO_HMAC_MODULE)
Philipp Reisner13e60372011-02-08 09:54:40 +01004595static int drbd_do_auth(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004596{
4597 dev_err(DEV, "This kernel was build without CONFIG_CRYPTO_HMAC.\n");
4598 dev_err(DEV, "You need to disable 'cram-hmac-alg' in drbd.conf.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004599 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004600}
4601#else
4602#define CHALLENGE_LEN 64
Johannes Thomab10d96c2010-01-07 16:02:50 +01004603
4604/* Return value:
4605 1 - auth succeeded,
4606 0 - failed, try again (network error),
4607 -1 - auth failed, don't try again.
4608*/
4609
Philipp Reisner13e60372011-02-08 09:54:40 +01004610static int drbd_do_auth(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004611{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004612 struct drbd_socket *sock;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004613 char my_challenge[CHALLENGE_LEN]; /* 64 Bytes... */
4614 struct scatterlist sg;
4615 char *response = NULL;
4616 char *right_response = NULL;
4617 char *peers_ch = NULL;
Philipp Reisner44ed1672011-04-19 17:10:19 +02004618 unsigned int key_len;
4619 char secret[SHARED_SECRET_MAX]; /* 64 byte */
Philipp Reisnerb411b362009-09-25 16:07:19 -07004620 unsigned int resp_size;
4621 struct hash_desc desc;
Philipp Reisner77351055b2011-02-07 17:24:26 +01004622 struct packet_info pi;
Philipp Reisner44ed1672011-04-19 17:10:19 +02004623 struct net_conf *nc;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004624 int err, rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004625
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004626 /* FIXME: Put the challenge/response into the preallocated socket buffer. */
4627
Philipp Reisner44ed1672011-04-19 17:10:19 +02004628 rcu_read_lock();
4629 nc = rcu_dereference(tconn->net_conf);
4630 key_len = strlen(nc->shared_secret);
4631 memcpy(secret, nc->shared_secret, key_len);
4632 rcu_read_unlock();
4633
Philipp Reisner13e60372011-02-08 09:54:40 +01004634 desc.tfm = tconn->cram_hmac_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004635 desc.flags = 0;
4636
Philipp Reisner44ed1672011-04-19 17:10:19 +02004637 rv = crypto_hash_setkey(tconn->cram_hmac_tfm, (u8 *)secret, key_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004638 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004639 conn_err(tconn, "crypto_hash_setkey() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004640 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004641 goto fail;
4642 }
4643
4644 get_random_bytes(my_challenge, CHALLENGE_LEN);
4645
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004646 sock = &tconn->data;
4647 if (!conn_prepare_command(tconn, sock)) {
4648 rv = 0;
4649 goto fail;
4650 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004651 rv = !conn_send_command(tconn, sock, P_AUTH_CHALLENGE, 0,
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004652 my_challenge, CHALLENGE_LEN);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004653 if (!rv)
4654 goto fail;
4655
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004656 err = drbd_recv_header(tconn, &pi);
4657 if (err) {
4658 rv = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004659 goto fail;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004660 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004661
Philipp Reisner77351055b2011-02-07 17:24:26 +01004662 if (pi.cmd != P_AUTH_CHALLENGE) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004663 conn_err(tconn, "expected AuthChallenge packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004664 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004665 rv = 0;
4666 goto fail;
4667 }
4668
Philipp Reisner77351055b2011-02-07 17:24:26 +01004669 if (pi.size > CHALLENGE_LEN * 2) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004670 conn_err(tconn, "expected AuthChallenge payload too big.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004671 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004672 goto fail;
4673 }
4674
Philipp Reisner77351055b2011-02-07 17:24:26 +01004675 peers_ch = kmalloc(pi.size, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004676 if (peers_ch == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004677 conn_err(tconn, "kmalloc of peers_ch failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004678 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004679 goto fail;
4680 }
4681
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004682 err = drbd_recv_all_warn(tconn, peers_ch, pi.size);
4683 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004684 rv = 0;
4685 goto fail;
4686 }
4687
Philipp Reisner13e60372011-02-08 09:54:40 +01004688 resp_size = crypto_hash_digestsize(tconn->cram_hmac_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004689 response = kmalloc(resp_size, GFP_NOIO);
4690 if (response == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004691 conn_err(tconn, "kmalloc of 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_init_table(&sg, 1);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004697 sg_set_buf(&sg, peers_ch, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004698
4699 rv = crypto_hash_digest(&desc, &sg, sg.length, response);
4700 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004701 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004702 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004703 goto fail;
4704 }
4705
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004706 if (!conn_prepare_command(tconn, sock)) {
4707 rv = 0;
4708 goto fail;
4709 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004710 rv = !conn_send_command(tconn, sock, P_AUTH_RESPONSE, 0,
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004711 response, resp_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004712 if (!rv)
4713 goto fail;
4714
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004715 err = drbd_recv_header(tconn, &pi);
4716 if (err) {
4717 rv = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004718 goto fail;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004719 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004720
Philipp Reisner77351055b2011-02-07 17:24:26 +01004721 if (pi.cmd != P_AUTH_RESPONSE) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004722 conn_err(tconn, "expected AuthResponse packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004723 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004724 rv = 0;
4725 goto fail;
4726 }
4727
Philipp Reisner77351055b2011-02-07 17:24:26 +01004728 if (pi.size != resp_size) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004729 conn_err(tconn, "expected AuthResponse payload of wrong size\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004730 rv = 0;
4731 goto fail;
4732 }
4733
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004734 err = drbd_recv_all_warn(tconn, response , resp_size);
4735 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004736 rv = 0;
4737 goto fail;
4738 }
4739
4740 right_response = kmalloc(resp_size, GFP_NOIO);
Julia Lawall2d1ee872009-12-27 22:27:11 +01004741 if (right_response == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004742 conn_err(tconn, "kmalloc of right_response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004743 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004744 goto fail;
4745 }
4746
4747 sg_set_buf(&sg, my_challenge, CHALLENGE_LEN);
4748
4749 rv = crypto_hash_digest(&desc, &sg, sg.length, right_response);
4750 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004751 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004752 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004753 goto fail;
4754 }
4755
4756 rv = !memcmp(response, right_response, resp_size);
4757
4758 if (rv)
Philipp Reisner44ed1672011-04-19 17:10:19 +02004759 conn_info(tconn, "Peer authenticated using %d bytes HMAC\n",
4760 resp_size);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004761 else
4762 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004763
4764 fail:
4765 kfree(peers_ch);
4766 kfree(response);
4767 kfree(right_response);
4768
4769 return rv;
4770}
4771#endif
4772
4773int drbdd_init(struct drbd_thread *thi)
4774{
Philipp Reisner392c8802011-02-09 10:33:31 +01004775 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004776 int h;
4777
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004778 conn_info(tconn, "receiver (re)started\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004779
4780 do {
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004781 h = conn_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004782 if (h == 0) {
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004783 conn_disconnect(tconn);
Philipp Reisner20ee6392011-01-18 15:28:59 +01004784 schedule_timeout_interruptible(HZ);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004785 }
4786 if (h == -1) {
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004787 conn_warn(tconn, "Discarding network configuration.\n");
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004788 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004789 }
4790 } while (h == 0);
4791
Philipp Reisner91fd4da2011-04-20 17:47:29 +02004792 if (h > 0)
4793 drbdd(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004794
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004795 conn_disconnect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004796
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004797 conn_info(tconn, "receiver terminated\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004798 return 0;
4799}
4800
4801/* ********* acknowledge sender ******** */
4802
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004803static int got_conn_RqSReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004804{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004805 struct p_req_state_reply *p = pi->data;
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004806 int retcode = be32_to_cpu(p->retcode);
4807
4808 if (retcode >= SS_SUCCESS) {
4809 set_bit(CONN_WD_ST_CHG_OKAY, &tconn->flags);
4810 } else {
4811 set_bit(CONN_WD_ST_CHG_FAIL, &tconn->flags);
4812 conn_err(tconn, "Requested state change failed by peer: %s (%d)\n",
4813 drbd_set_st_err_str(retcode), retcode);
4814 }
4815 wake_up(&tconn->ping_wait);
4816
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004817 return 0;
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004818}
4819
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004820static int got_RqSReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004821{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004822 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004823 struct p_req_state_reply *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004824 int retcode = be32_to_cpu(p->retcode);
4825
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004826 mdev = vnr_to_mdev(tconn, pi->vnr);
4827 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004828 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004829
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004830 if (retcode >= SS_SUCCESS) {
4831 set_bit(CL_ST_CHG_SUCCESS, &mdev->flags);
4832 } else {
4833 set_bit(CL_ST_CHG_FAIL, &mdev->flags);
4834 dev_err(DEV, "Requested state change failed by peer: %s (%d)\n",
4835 drbd_set_st_err_str(retcode), retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004836 }
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004837 wake_up(&mdev->state_wait);
4838
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004839 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004840}
4841
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004842static int got_Ping(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004843{
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004844 return drbd_send_ping_ack(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004845
4846}
4847
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004848static int got_PingAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004849{
4850 /* restore idle timeout */
Philipp Reisner2a67d8b2011-02-09 14:10:32 +01004851 tconn->meta.socket->sk->sk_rcvtimeo = tconn->net_conf->ping_int*HZ;
4852 if (!test_and_set_bit(GOT_PING_ACK, &tconn->flags))
4853 wake_up(&tconn->ping_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004854
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004855 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004856}
4857
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004858static int got_IsInSync(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004859{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004860 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004861 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004862 sector_t sector = be64_to_cpu(p->sector);
4863 int blksize = be32_to_cpu(p->blksize);
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 Reisner31890f42011-01-19 14:12:51 +01004869 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004870
4871 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4872
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004873 if (get_ldev(mdev)) {
4874 drbd_rs_complete_io(mdev, sector);
4875 drbd_set_in_sync(mdev, sector, blksize);
4876 /* rs_same_csums is supposed to count in units of BM_BLOCK_SIZE */
4877 mdev->rs_same_csum += (blksize >> BM_BLOCK_SHIFT);
4878 put_ldev(mdev);
4879 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004880 dec_rs_pending(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02004881 atomic_add(blksize >> 9, &mdev->rs_sect_in);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004882
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004883 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004884}
4885
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004886static int
4887validate_req_change_req_state(struct drbd_conf *mdev, u64 id, sector_t sector,
4888 struct rb_root *root, const char *func,
4889 enum drbd_req_event what, bool missing_ok)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004890{
4891 struct drbd_request *req;
4892 struct bio_and_error m;
4893
Philipp Reisner87eeee42011-01-19 14:16:30 +01004894 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004895 req = find_request(mdev, root, id, sector, missing_ok, func);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004896 if (unlikely(!req)) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01004897 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004898 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004899 }
4900 __req_mod(req, what, &m);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004901 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004902
4903 if (m.bio)
4904 complete_master_bio(mdev, &m);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004905 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004906}
4907
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004908static int got_BlockAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004909{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004910 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004911 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004912 sector_t sector = be64_to_cpu(p->sector);
4913 int blksize = be32_to_cpu(p->blksize);
4914 enum drbd_req_event what;
4915
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004916 mdev = vnr_to_mdev(tconn, pi->vnr);
4917 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004918 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004919
Philipp Reisnerb411b362009-09-25 16:07:19 -07004920 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4921
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01004922 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004923 drbd_set_in_sync(mdev, sector, blksize);
4924 dec_rs_pending(mdev);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004925 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004926 }
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004927 switch (pi->cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004928 case P_RS_WRITE_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004929 what = WRITE_ACKED_BY_PEER_AND_SIS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004930 break;
4931 case P_WRITE_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004932 what = WRITE_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004933 break;
4934 case P_RECV_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004935 what = RECV_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004936 break;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004937 case P_DISCARD_WRITE:
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004938 what = DISCARD_WRITE;
4939 break;
4940 case P_RETRY_WRITE:
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004941 what = POSTPONE_WRITE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004942 break;
4943 default:
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004944 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -07004945 }
4946
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004947 return validate_req_change_req_state(mdev, p->block_id, sector,
4948 &mdev->write_requests, __func__,
4949 what, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004950}
4951
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004952static int got_NegAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004953{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004954 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004955 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004956 sector_t sector = be64_to_cpu(p->sector);
Philipp Reisner2deb8332011-01-17 18:39:18 +01004957 int size = be32_to_cpu(p->blksize);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004958 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004959
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004960 mdev = vnr_to_mdev(tconn, pi->vnr);
4961 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004962 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004963
Philipp Reisnerb411b362009-09-25 16:07:19 -07004964 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4965
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01004966 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004967 dec_rs_pending(mdev);
4968 drbd_rs_failed_io(mdev, sector, size);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004969 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004970 }
Philipp Reisner2deb8332011-01-17 18:39:18 +01004971
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004972 err = validate_req_change_req_state(mdev, p->block_id, sector,
4973 &mdev->write_requests, __func__,
Philipp Reisner303d1442011-04-13 16:24:47 -07004974 NEG_ACKED, true);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004975 if (err) {
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004976 /* Protocol A has no P_WRITE_ACKs, but has P_NEG_ACKs.
4977 The master bio might already be completed, therefore the
4978 request is no longer in the collision hash. */
4979 /* In Protocol B we might already have got a P_RECV_ACK
4980 but then get a P_NEG_ACK afterwards. */
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004981 drbd_set_out_of_sync(mdev, sector, size);
Philipp Reisner2deb8332011-01-17 18:39:18 +01004982 }
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004983 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004984}
4985
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004986static int got_NegDReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004987{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004988 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004989 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004990 sector_t sector = be64_to_cpu(p->sector);
4991
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004992 mdev = vnr_to_mdev(tconn, pi->vnr);
4993 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004994 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004995
Philipp Reisnerb411b362009-09-25 16:07:19 -07004996 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004997
Philipp Reisner380207d2011-11-11 12:31:20 +01004998 dev_err(DEV, "Got NegDReply; Sector %llus, len %u.\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -07004999 (unsigned long long)sector, be32_to_cpu(p->blksize));
5000
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005001 return validate_req_change_req_state(mdev, p->block_id, sector,
5002 &mdev->read_requests, __func__,
5003 NEG_ACKED, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005004}
5005
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005006static int got_NegRSDReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005007{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005008 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005009 sector_t sector;
5010 int size;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005011 struct p_block_ack *p = pi->data;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005012
5013 mdev = vnr_to_mdev(tconn, pi->vnr);
5014 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005015 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005016
5017 sector = be64_to_cpu(p->sector);
5018 size = be32_to_cpu(p->blksize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005019
5020 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
5021
5022 dec_rs_pending(mdev);
5023
5024 if (get_ldev_if_state(mdev, D_FAILED)) {
5025 drbd_rs_complete_io(mdev, sector);
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01005026 switch (pi->cmd) {
Philipp Reisnerd612d302010-12-27 10:53:28 +01005027 case P_NEG_RS_DREPLY:
5028 drbd_rs_failed_io(mdev, sector, size);
5029 case P_RS_CANCEL:
5030 break;
5031 default:
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005032 BUG();
Philipp Reisnerd612d302010-12-27 10:53:28 +01005033 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005034 put_ldev(mdev);
5035 }
5036
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005037 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005038}
5039
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005040static int got_BarrierAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005041{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005042 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005043 struct p_barrier_ack *p = pi->data;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005044
5045 mdev = vnr_to_mdev(tconn, pi->vnr);
5046 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005047 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005048
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01005049 tl_release(mdev->tconn, p->barrier, be32_to_cpu(p->set_size));
Philipp Reisnerb411b362009-09-25 16:07:19 -07005050
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02005051 if (mdev->state.conn == C_AHEAD &&
5052 atomic_read(&mdev->ap_in_flight) == 0 &&
Philipp Reisner36baf612011-11-10 14:27:34 +01005053 !test_and_set_bit(AHEAD_TO_SYNC_SOURCE, &mdev->flags)) {
Philipp Reisner370a43e2011-01-14 16:03:11 +01005054 mdev->start_resync_timer.expires = jiffies + HZ;
5055 add_timer(&mdev->start_resync_timer);
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02005056 }
5057
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005058 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005059}
5060
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005061static int got_OVResult(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005062{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005063 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005064 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005065 struct drbd_work *w;
5066 sector_t sector;
5067 int size;
5068
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005069 mdev = vnr_to_mdev(tconn, pi->vnr);
5070 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005071 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005072
Philipp Reisnerb411b362009-09-25 16:07:19 -07005073 sector = be64_to_cpu(p->sector);
5074 size = be32_to_cpu(p->blksize);
5075
5076 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
5077
5078 if (be64_to_cpu(p->block_id) == ID_OUT_OF_SYNC)
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005079 drbd_ov_out_of_sync_found(mdev, sector, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005080 else
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005081 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005082
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005083 if (!get_ldev(mdev))
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005084 return 0;
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005085
Philipp Reisnerb411b362009-09-25 16:07:19 -07005086 drbd_rs_complete_io(mdev, sector);
5087 dec_rs_pending(mdev);
5088
Lars Ellenbergea5442a2010-11-05 09:48:01 +01005089 --mdev->ov_left;
5090
5091 /* let's advance progress step marks only for every other megabyte */
5092 if ((mdev->ov_left & 0x200) == 0x200)
5093 drbd_advance_rs_marks(mdev, mdev->ov_left);
5094
5095 if (mdev->ov_left == 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07005096 w = kmalloc(sizeof(*w), GFP_NOIO);
5097 if (w) {
5098 w->cb = w_ov_finished;
Philipp Reisnera21e9292011-02-08 15:08:49 +01005099 w->mdev = mdev;
Philipp Reisnere42325a2011-01-19 13:55:45 +01005100 drbd_queue_work_front(&mdev->tconn->data.work, w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005101 } else {
5102 dev_err(DEV, "kmalloc(w) failed.");
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005103 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005104 drbd_resync_finished(mdev);
5105 }
5106 }
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005107 put_ldev(mdev);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005108 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005109}
5110
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005111static int got_skip(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner0ced55a2010-04-30 15:26:20 +02005112{
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005113 return 0;
Philipp Reisner0ced55a2010-04-30 15:26:20 +02005114}
5115
Andreas Gruenbachera990be42011-04-06 17:56:48 +02005116static int tconn_finish_peer_reqs(struct drbd_tconn *tconn)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005117{
Philipp Reisner082a3432011-03-15 16:05:42 +01005118 struct drbd_conf *mdev;
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005119 int vnr, not_empty = 0;
Philipp Reisner32862ec2011-02-08 16:41:01 +01005120
5121 do {
5122 clear_bit(SIGNAL_ASENDER, &tconn->flags);
5123 flush_signals(current);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005124
5125 rcu_read_lock();
5126 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
5127 kref_get(&mdev->kref);
5128 rcu_read_unlock();
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07005129 if (drbd_finish_peer_reqs(mdev)) {
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005130 kref_put(&mdev->kref, &drbd_minor_destroy);
5131 return 1;
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07005132 }
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005133 kref_put(&mdev->kref, &drbd_minor_destroy);
5134 rcu_read_lock();
Philipp Reisner082a3432011-03-15 16:05:42 +01005135 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005136 set_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisner082a3432011-03-15 16:05:42 +01005137
5138 spin_lock_irq(&tconn->req_lock);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005139 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Philipp Reisner082a3432011-03-15 16:05:42 +01005140 not_empty = !list_empty(&mdev->done_ee);
5141 if (not_empty)
5142 break;
5143 }
5144 spin_unlock_irq(&tconn->req_lock);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005145 rcu_read_unlock();
Philipp Reisner32862ec2011-02-08 16:41:01 +01005146 } while (not_empty);
5147
5148 return 0;
5149}
5150
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005151struct asender_cmd {
5152 size_t pkt_size;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005153 int (*fn)(struct drbd_tconn *tconn, struct packet_info *);
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005154};
5155
5156static struct asender_cmd asender_tbl[] = {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005157 [P_PING] = { 0, got_Ping },
5158 [P_PING_ACK] = { 0, got_PingAck },
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005159 [P_RECV_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5160 [P_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5161 [P_RS_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5162 [P_DISCARD_WRITE] = { sizeof(struct p_block_ack), got_BlockAck },
5163 [P_NEG_ACK] = { sizeof(struct p_block_ack), got_NegAck },
5164 [P_NEG_DREPLY] = { sizeof(struct p_block_ack), got_NegDReply },
5165 [P_NEG_RS_DREPLY] = { sizeof(struct p_block_ack), got_NegRSDReply },
5166 [P_OV_RESULT] = { sizeof(struct p_block_ack), got_OVResult },
5167 [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), got_BarrierAck },
5168 [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), got_RqSReply },
5169 [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), got_IsInSync },
5170 [P_DELAY_PROBE] = { sizeof(struct p_delay_probe93), got_skip },
5171 [P_RS_CANCEL] = { sizeof(struct p_block_ack), got_NegRSDReply },
5172 [P_CONN_ST_CHG_REPLY]={ sizeof(struct p_req_state_reply), got_conn_RqSReply },
5173 [P_RETRY_WRITE] = { sizeof(struct p_block_ack), got_BlockAck },
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005174};
5175
Philipp Reisnerb411b362009-09-25 16:07:19 -07005176int drbd_asender(struct drbd_thread *thi)
5177{
Philipp Reisner392c8802011-02-09 10:33:31 +01005178 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005179 struct asender_cmd *cmd = NULL;
Philipp Reisner77351055b2011-02-07 17:24:26 +01005180 struct packet_info pi;
Philipp Reisner257d0af2011-01-26 12:15:29 +01005181 int rv;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005182 void *buf = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005183 int received = 0;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005184 unsigned int header_size = drbd_header_size(tconn);
5185 int expect = header_size;
Philipp Reisner44ed1672011-04-19 17:10:19 +02005186 bool ping_timeout_active = false;
5187 struct net_conf *nc;
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005188 int ping_timeo, tcp_cork, ping_int;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005189
Philipp Reisnerb411b362009-09-25 16:07:19 -07005190 current->policy = SCHED_RR; /* Make this a realtime task! */
5191 current->rt_priority = 2; /* more important than all other tasks */
5192
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +01005193 while (get_t_state(thi) == RUNNING) {
Philipp Reisner80822282011-02-08 12:46:30 +01005194 drbd_thread_current_set_cpu(thi);
Philipp Reisner44ed1672011-04-19 17:10:19 +02005195
5196 rcu_read_lock();
5197 nc = rcu_dereference(tconn->net_conf);
5198 ping_timeo = nc->ping_timeo;
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005199 tcp_cork = nc->tcp_cork;
Philipp Reisner44ed1672011-04-19 17:10:19 +02005200 ping_int = nc->ping_int;
5201 rcu_read_unlock();
5202
Philipp Reisner32862ec2011-02-08 16:41:01 +01005203 if (test_and_clear_bit(SEND_PING, &tconn->flags)) {
Andreas Gruenbachera17647a2011-04-01 12:49:42 +02005204 if (drbd_send_ping(tconn)) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005205 conn_err(tconn, "drbd_send_ping has failed\n");
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01005206 goto reconnect;
5207 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02005208 tconn->meta.socket->sk->sk_rcvtimeo = ping_timeo * HZ / 10;
5209 ping_timeout_active = true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005210 }
5211
Philipp Reisner32862ec2011-02-08 16:41:01 +01005212 /* TODO: conditionally cork; it may hurt latency if we cork without
5213 much to send */
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005214 if (tcp_cork)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005215 drbd_tcp_cork(tconn->meta.socket);
Andreas Gruenbachera990be42011-04-06 17:56:48 +02005216 if (tconn_finish_peer_reqs(tconn)) {
5217 conn_err(tconn, "tconn_finish_peer_reqs() failed\n");
Philipp Reisner32862ec2011-02-08 16:41:01 +01005218 goto reconnect;
Philipp Reisner082a3432011-03-15 16:05:42 +01005219 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005220 /* but unconditionally uncork unless disabled */
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005221 if (tcp_cork)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005222 drbd_tcp_uncork(tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005223
5224 /* short circuit, recv_msg would return EINTR anyways. */
5225 if (signal_pending(current))
5226 continue;
5227
Philipp Reisner32862ec2011-02-08 16:41:01 +01005228 rv = drbd_recv_short(tconn->meta.socket, buf, expect-received, 0);
5229 clear_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005230
5231 flush_signals(current);
5232
5233 /* Note:
5234 * -EINTR (on meta) we got a signal
5235 * -EAGAIN (on meta) rcvtimeo expired
5236 * -ECONNRESET other side closed the connection
5237 * -ERESTARTSYS (on data) we got a signal
5238 * rv < 0 other than above: unexpected error!
5239 * rv == expected: full header or command
5240 * rv < expected: "woken" by signal during receive
5241 * rv == 0 : "connection shut down by peer"
5242 */
5243 if (likely(rv > 0)) {
5244 received += rv;
5245 buf += rv;
5246 } else if (rv == 0) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005247 conn_err(tconn, "meta connection shut down by peer.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005248 goto reconnect;
5249 } else if (rv == -EAGAIN) {
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02005250 /* If the data socket received something meanwhile,
5251 * that is good enough: peer is still alive. */
Philipp Reisner32862ec2011-02-08 16:41:01 +01005252 if (time_after(tconn->last_received,
5253 jiffies - tconn->meta.socket->sk->sk_rcvtimeo))
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02005254 continue;
Lars Ellenbergf36af182011-03-09 22:44:55 +01005255 if (ping_timeout_active) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005256 conn_err(tconn, "PingAck did not arrive in time.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005257 goto reconnect;
5258 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005259 set_bit(SEND_PING, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005260 continue;
5261 } else if (rv == -EINTR) {
5262 continue;
5263 } else {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005264 conn_err(tconn, "sock_recvmsg returned %d\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005265 goto reconnect;
5266 }
5267
5268 if (received == expect && cmd == NULL) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005269 if (decode_header(tconn, tconn->meta.rbuf, &pi))
Philipp Reisnerb411b362009-09-25 16:07:19 -07005270 goto reconnect;
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005271 cmd = &asender_tbl[pi.cmd];
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005272 if (pi.cmd >= ARRAY_SIZE(asender_tbl) || !cmd->fn) {
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02005273 conn_err(tconn, "Unexpected meta packet %s (0x%04x)\n",
5274 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005275 goto disconnect;
5276 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005277 expect = header_size + cmd->pkt_size;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005278 if (pi.size != expect - header_size) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005279 conn_err(tconn, "Wrong packet size on meta (c: %d, l: %d)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01005280 pi.cmd, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005281 goto reconnect;
Philipp Reisner257d0af2011-01-26 12:15:29 +01005282 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005283 }
5284 if (received == expect) {
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005285 bool err;
Philipp Reisnera4fbda82011-03-16 11:13:17 +01005286
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005287 err = cmd->fn(tconn, &pi);
5288 if (err) {
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005289 conn_err(tconn, "%pf failed\n", cmd->fn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005290 goto reconnect;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005291 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005292
Philipp Reisnera4fbda82011-03-16 11:13:17 +01005293 tconn->last_received = jiffies;
5294
Philipp Reisner44ed1672011-04-19 17:10:19 +02005295 if (cmd == &asender_tbl[P_PING_ACK]) {
5296 /* restore idle timeout */
5297 tconn->meta.socket->sk->sk_rcvtimeo = ping_int * HZ;
5298 ping_timeout_active = false;
5299 }
Lars Ellenbergf36af182011-03-09 22:44:55 +01005300
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005301 buf = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005302 received = 0;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005303 expect = header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005304 cmd = NULL;
5305 }
5306 }
5307
5308 if (0) {
5309reconnect:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01005310 conn_request_state(tconn, NS(conn, C_NETWORK_FAILURE), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005311 }
5312 if (0) {
5313disconnect:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01005314 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005315 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005316 clear_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005317
Philipp Reisner32862ec2011-02-08 16:41:01 +01005318 conn_info(tconn, "asender terminated\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005319
5320 return 0;
5321}