blob: 9c888e5b648fa063df8587ed4394e33522fd2fbf [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{
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200845 struct socket *sock, *msock;
Philipp Reisnerc141ebd2011-05-05 16:13:10 +0200846 struct drbd_conf *mdev;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200847 struct net_conf *nc;
Philipp Reisnerc141ebd2011-05-05 16:13:10 +0200848 int vnr, timeout, try, h, ok;
Philipp Reisner08b165b2011-09-05 16:22:33 +0200849 bool discard_my_data;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700850
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100851 if (conn_request_state(tconn, NS(conn, C_WF_CONNECTION), CS_VERBOSE) < SS_SUCCESS)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700852 return -2;
853
Philipp Reisner907599e2011-02-08 11:25:37 +0100854 clear_bit(DISCARD_CONCURRENT, &tconn->flags);
Andreas Gruenbacher0916e0e2011-03-21 14:10:15 +0100855
856 /* Assume that the peer only understands protocol 80 until we know better. */
857 tconn->agreed_pro_version = 80;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700858
Philipp Reisnerb411b362009-09-25 16:07:19 -0700859 do {
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200860 struct socket *s;
861
Philipp Reisnerb411b362009-09-25 16:07:19 -0700862 for (try = 0;;) {
863 /* 3 tries, this should take less than a second! */
Philipp Reisner907599e2011-02-08 11:25:37 +0100864 s = drbd_try_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700865 if (s || ++try >= 3)
866 break;
867 /* give the other side time to call bind() & listen() */
Philipp Reisner20ee6392011-01-18 15:28:59 +0100868 schedule_timeout_interruptible(HZ / 10);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700869 }
870
871 if (s) {
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200872 if (!tconn->data.socket) {
873 tconn->data.socket = s;
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200874 send_first_packet(tconn, &tconn->data, P_INITIAL_DATA);
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200875 } else if (!tconn->meta.socket) {
876 tconn->meta.socket = s;
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200877 send_first_packet(tconn, &tconn->meta, P_INITIAL_META);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700878 } else {
Philipp Reisner81fa2e62011-05-04 15:10:30 +0200879 conn_err(tconn, "Logic error in conn_connect()\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700880 goto out_release_sockets;
881 }
882 }
883
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200884 if (tconn->data.socket && tconn->meta.socket) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100885 schedule_timeout_interruptible(tconn->net_conf->ping_timeo*HZ/10);
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200886 ok = drbd_socket_okay(&tconn->data.socket);
887 ok = drbd_socket_okay(&tconn->meta.socket) && ok;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700888 if (ok)
889 break;
890 }
891
892retry:
Philipp Reisner907599e2011-02-08 11:25:37 +0100893 s = drbd_wait_for_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700894 if (s) {
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200895 try = receive_first_packet(tconn, s);
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200896 drbd_socket_okay(&tconn->data.socket);
897 drbd_socket_okay(&tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700898 switch (try) {
Andreas Gruenbachere5d6f332011-03-28 16:44:40 +0200899 case P_INITIAL_DATA:
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200900 if (tconn->data.socket) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100901 conn_warn(tconn, "initial packet S crossed\n");
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200902 sock_release(tconn->data.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700903 }
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200904 tconn->data.socket = s;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700905 break;
Andreas Gruenbachere5d6f332011-03-28 16:44:40 +0200906 case P_INITIAL_META:
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200907 if (tconn->meta.socket) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100908 conn_warn(tconn, "initial packet M crossed\n");
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200909 sock_release(tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700910 }
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200911 tconn->meta.socket = s;
Philipp Reisner907599e2011-02-08 11:25:37 +0100912 set_bit(DISCARD_CONCURRENT, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700913 break;
914 default:
Philipp Reisner907599e2011-02-08 11:25:37 +0100915 conn_warn(tconn, "Error receiving initial packet\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700916 sock_release(s);
917 if (random32() & 1)
918 goto retry;
919 }
920 }
921
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100922 if (tconn->cstate <= C_DISCONNECTING)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700923 goto out_release_sockets;
924 if (signal_pending(current)) {
925 flush_signals(current);
926 smp_rmb();
Philipp Reisner907599e2011-02-08 11:25:37 +0100927 if (get_t_state(&tconn->receiver) == EXITING)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700928 goto out_release_sockets;
929 }
930
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200931 if (tconn->data.socket && &tconn->meta.socket) {
932 ok = drbd_socket_okay(&tconn->data.socket);
933 ok = drbd_socket_okay(&tconn->meta.socket) && ok;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700934 if (ok)
935 break;
936 }
937 } while (1);
938
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200939 sock = tconn->data.socket;
940 msock = tconn->meta.socket;
941
Philipp Reisnerb411b362009-09-25 16:07:19 -0700942 msock->sk->sk_reuse = 1; /* SO_REUSEADDR */
943 sock->sk->sk_reuse = 1; /* SO_REUSEADDR */
944
945 sock->sk->sk_allocation = GFP_NOIO;
946 msock->sk->sk_allocation = GFP_NOIO;
947
948 sock->sk->sk_priority = TC_PRIO_INTERACTIVE_BULK;
949 msock->sk->sk_priority = TC_PRIO_INTERACTIVE;
950
Philipp Reisnerb411b362009-09-25 16:07:19 -0700951 /* NOT YET ...
Philipp Reisner907599e2011-02-08 11:25:37 +0100952 * sock->sk->sk_sndtimeo = tconn->net_conf->timeout*HZ/10;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700953 * sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
Andreas Gruenbacher60381782011-03-28 17:05:50 +0200954 * first set it to the P_CONNECTION_FEATURES timeout,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700955 * which we set to 4x the configured ping_timeout. */
Philipp Reisner44ed1672011-04-19 17:10:19 +0200956 rcu_read_lock();
957 nc = rcu_dereference(tconn->net_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700958
Philipp Reisner44ed1672011-04-19 17:10:19 +0200959 sock->sk->sk_sndtimeo =
960 sock->sk->sk_rcvtimeo = nc->ping_timeo*4*HZ/10;
961
962 msock->sk->sk_rcvtimeo = nc->ping_int*HZ;
963 timeout = nc->timeout * HZ / 10;
Philipp Reisner08b165b2011-09-05 16:22:33 +0200964 discard_my_data = nc->discard_my_data;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200965 rcu_read_unlock();
966
967 msock->sk->sk_sndtimeo = timeout;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700968
969 /* we don't want delays.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300970 * we use TCP_CORK where appropriate, though */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700971 drbd_tcp_nodelay(sock);
972 drbd_tcp_nodelay(msock);
973
Philipp Reisner907599e2011-02-08 11:25:37 +0100974 tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700975
Andreas Gruenbacher60381782011-03-28 17:05:50 +0200976 h = drbd_do_features(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700977 if (h <= 0)
978 return h;
979
Philipp Reisner907599e2011-02-08 11:25:37 +0100980 if (tconn->cram_hmac_tfm) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700981 /* drbd_request_state(mdev, NS(conn, WFAuth)); */
Philipp Reisner907599e2011-02-08 11:25:37 +0100982 switch (drbd_do_auth(tconn)) {
Johannes Thomab10d96c2010-01-07 16:02:50 +0100983 case -1:
Philipp Reisner907599e2011-02-08 11:25:37 +0100984 conn_err(tconn, "Authentication of peer failed\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700985 return -1;
Johannes Thomab10d96c2010-01-07 16:02:50 +0100986 case 0:
Philipp Reisner907599e2011-02-08 11:25:37 +0100987 conn_err(tconn, "Authentication of peer failed, trying again.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +0100988 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700989 }
990 }
991
Philipp Reisner44ed1672011-04-19 17:10:19 +0200992 sock->sk->sk_sndtimeo = timeout;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700993 sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
994
Andreas Gruenbacher387eb302011-03-16 01:05:37 +0100995 if (drbd_send_protocol(tconn) == -EOPNOTSUPP)
Philipp Reisner7e2455c2010-04-22 14:50:23 +0200996 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700997
Philipp Reisnerc141ebd2011-05-05 16:13:10 +0200998 rcu_read_lock();
999 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1000 kref_get(&mdev->kref);
1001 rcu_read_unlock();
Philipp Reisner08b165b2011-09-05 16:22:33 +02001002
1003 if (discard_my_data)
1004 set_bit(DISCARD_MY_DATA, &mdev->flags);
1005 else
1006 clear_bit(DISCARD_MY_DATA, &mdev->flags);
1007
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02001008 drbd_connected(mdev);
1009 kref_put(&mdev->kref, &drbd_minor_destroy);
1010 rcu_read_lock();
1011 }
1012 rcu_read_unlock();
1013
Philipp Reisner823bd832012-11-08 15:04:36 +01001014 if (conn_request_state(tconn, NS(conn, C_WF_REPORT_PARAMS), CS_VERBOSE) < SS_SUCCESS)
1015 return 0;
1016
1017 drbd_thread_start(&tconn->asender);
1018
Philipp Reisner08b165b2011-09-05 16:22:33 +02001019 mutex_lock(&tconn->conf_update);
1020 /* The discard_my_data flag is a single-shot modifier to the next
1021 * connection attempt, the handshake of which is now well underway.
1022 * No need for rcu style copying of the whole struct
1023 * just to clear a single value. */
1024 tconn->net_conf->discard_my_data = 0;
1025 mutex_unlock(&tconn->conf_update);
1026
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07001027 return h;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001028
1029out_release_sockets:
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +02001030 if (tconn->data.socket) {
1031 sock_release(tconn->data.socket);
1032 tconn->data.socket = NULL;
1033 }
1034 if (tconn->meta.socket) {
1035 sock_release(tconn->meta.socket);
1036 tconn->meta.socket = NULL;
1037 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001038 return -1;
1039}
1040
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001041static int decode_header(struct drbd_tconn *tconn, void *header, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001042{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001043 unsigned int header_size = drbd_header_size(tconn);
1044
Andreas Gruenbacher0c8e36d2011-03-30 16:00:17 +02001045 if (header_size == sizeof(struct p_header100) &&
1046 *(__be32 *)header == cpu_to_be32(DRBD_MAGIC_100)) {
1047 struct p_header100 *h = header;
1048 if (h->pad != 0) {
1049 conn_err(tconn, "Header padding is not zero\n");
1050 return -EINVAL;
1051 }
1052 pi->vnr = be16_to_cpu(h->volume);
1053 pi->cmd = be16_to_cpu(h->command);
1054 pi->size = be32_to_cpu(h->length);
1055 } else if (header_size == sizeof(struct p_header95) &&
1056 *(__be16 *)header == cpu_to_be16(DRBD_MAGIC_BIG)) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001057 struct p_header95 *h = header;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001058 pi->cmd = be16_to_cpu(h->command);
Andreas Gruenbacherb55d84b2011-03-22 13:17:47 +01001059 pi->size = be32_to_cpu(h->length);
1060 pi->vnr = 0;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001061 } else if (header_size == sizeof(struct p_header80) &&
1062 *(__be32 *)header == cpu_to_be32(DRBD_MAGIC)) {
1063 struct p_header80 *h = header;
1064 pi->cmd = be16_to_cpu(h->command);
1065 pi->size = be16_to_cpu(h->length);
Philipp Reisner77351055b2011-02-07 17:24:26 +01001066 pi->vnr = 0;
Philipp Reisner02918be2010-08-20 14:35:10 +02001067 } else {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001068 conn_err(tconn, "Wrong magic value 0x%08x in protocol version %d\n",
1069 be32_to_cpu(*(__be32 *)header),
1070 tconn->agreed_pro_version);
Andreas Gruenbacher8172f3e2011-03-16 17:22:39 +01001071 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001072 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001073 pi->data = header + header_size;
Andreas Gruenbacher8172f3e2011-03-16 17:22:39 +01001074 return 0;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001075}
1076
Philipp Reisner9ba7aa02011-02-07 17:32:41 +01001077static int drbd_recv_header(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner257d0af2011-01-26 12:15:29 +01001078{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001079 void *buffer = tconn->data.rbuf;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001080 int err;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001081
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001082 err = drbd_recv_all_warn(tconn, buffer, drbd_header_size(tconn));
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001083 if (err)
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001084 return err;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001085
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001086 err = decode_header(tconn, buffer, pi);
Philipp Reisner9ba7aa02011-02-07 17:32:41 +01001087 tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001088
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001089 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001090}
1091
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001092static void drbd_flush(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001093{
1094 int rv;
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001095 struct drbd_conf *mdev;
1096 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001097
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001098 if (tconn->write_ordering >= WO_bdev_flush) {
Lars Ellenberg615e0872011-11-17 14:32:12 +01001099 rcu_read_lock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001100 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Lars Ellenberg615e0872011-11-17 14:32:12 +01001101 if (!get_ldev(mdev))
1102 continue;
1103 kref_get(&mdev->kref);
1104 rcu_read_unlock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001105
Lars Ellenberg615e0872011-11-17 14:32:12 +01001106 rv = blkdev_issue_flush(mdev->ldev->backing_bdev,
1107 GFP_NOIO, NULL);
1108 if (rv) {
1109 dev_info(DEV, "local disk flush failed with status %d\n", rv);
1110 /* would rather check on EOPNOTSUPP, but that is not reliable.
1111 * don't try again for ANY return value != 0
1112 * if (rv == -EOPNOTSUPP) */
1113 drbd_bump_write_ordering(tconn, WO_drain_io);
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001114 }
Lars Ellenberg615e0872011-11-17 14:32:12 +01001115 put_ldev(mdev);
1116 kref_put(&mdev->kref, &drbd_minor_destroy);
1117
1118 rcu_read_lock();
1119 if (rv)
1120 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001121 }
Lars Ellenberg615e0872011-11-17 14:32:12 +01001122 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07001123 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001124}
1125
1126/**
1127 * drbd_may_finish_epoch() - Applies an epoch_event to the epoch's state, eventually finishes it.
1128 * @mdev: DRBD device.
1129 * @epoch: Epoch object.
1130 * @ev: Epoch event.
1131 */
Philipp Reisner1e9dd292011-11-10 15:14:53 +01001132static enum finish_epoch drbd_may_finish_epoch(struct drbd_tconn *tconn,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001133 struct drbd_epoch *epoch,
1134 enum epoch_event ev)
1135{
Philipp Reisner2451fc32010-08-24 13:43:11 +02001136 int epoch_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001137 struct drbd_epoch *next_epoch;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001138 enum finish_epoch rv = FE_STILL_LIVE;
1139
Philipp Reisner12038a32011-11-09 19:18:00 +01001140 spin_lock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001141 do {
1142 next_epoch = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001143
1144 epoch_size = atomic_read(&epoch->epoch_size);
1145
1146 switch (ev & ~EV_CLEANUP) {
1147 case EV_PUT:
1148 atomic_dec(&epoch->active);
1149 break;
1150 case EV_GOT_BARRIER_NR:
1151 set_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001152 break;
1153 case EV_BECAME_LAST:
1154 /* nothing to do*/
1155 break;
1156 }
1157
Philipp Reisnerb411b362009-09-25 16:07:19 -07001158 if (epoch_size != 0 &&
1159 atomic_read(&epoch->active) == 0 &&
Philipp Reisner85d73512011-07-18 15:45:15 +02001160 (test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags) || ev & EV_CLEANUP)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001161 if (!(ev & EV_CLEANUP)) {
Philipp Reisner12038a32011-11-09 19:18:00 +01001162 spin_unlock(&tconn->epoch_lock);
Philipp Reisner1d2783d2011-11-10 14:56:07 +01001163 drbd_send_b_ack(epoch->mdev, epoch->barrier_nr, epoch_size);
Philipp Reisner12038a32011-11-09 19:18:00 +01001164 spin_lock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001165 }
Philipp Reisner85d73512011-07-18 15:45:15 +02001166 if (test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags))
Philipp Reisner1d2783d2011-11-10 14:56:07 +01001167 dec_unacked(epoch->mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001168
Philipp Reisner12038a32011-11-09 19:18:00 +01001169 if (tconn->current_epoch != epoch) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001170 next_epoch = list_entry(epoch->list.next, struct drbd_epoch, list);
1171 list_del(&epoch->list);
1172 ev = EV_BECAME_LAST | (ev & EV_CLEANUP);
Philipp Reisner12038a32011-11-09 19:18:00 +01001173 tconn->epochs--;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001174 kfree(epoch);
1175
1176 if (rv == FE_STILL_LIVE)
1177 rv = FE_DESTROYED;
1178 } else {
1179 epoch->flags = 0;
1180 atomic_set(&epoch->epoch_size, 0);
Uwe Kleine-König698f9312010-07-02 20:41:51 +02001181 /* atomic_set(&epoch->active, 0); is already zero */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001182 if (rv == FE_STILL_LIVE)
1183 rv = FE_RECYCLED;
1184 }
1185 }
1186
1187 if (!next_epoch)
1188 break;
1189
1190 epoch = next_epoch;
1191 } while (1);
1192
Philipp Reisner12038a32011-11-09 19:18:00 +01001193 spin_unlock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001194
Philipp Reisnerb411b362009-09-25 16:07:19 -07001195 return rv;
1196}
1197
1198/**
1199 * drbd_bump_write_ordering() - Fall back to an other write ordering method
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001200 * @tconn: DRBD connection.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001201 * @wo: Write ordering method to try.
1202 */
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001203void drbd_bump_write_ordering(struct drbd_tconn *tconn, enum write_ordering_e wo)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001204{
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001205 struct disk_conf *dc;
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001206 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001207 enum write_ordering_e pwo;
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001208 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001209 static char *write_ordering_str[] = {
1210 [WO_none] = "none",
1211 [WO_drain_io] = "drain",
1212 [WO_bdev_flush] = "flush",
Philipp Reisnerb411b362009-09-25 16:07:19 -07001213 };
1214
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001215 pwo = tconn->write_ordering;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001216 wo = min(pwo, wo);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001217 rcu_read_lock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001218 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1219 if (!get_ldev(mdev))
1220 continue;
1221 dc = rcu_dereference(mdev->ldev->disk_conf);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001222
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001223 if (wo == WO_bdev_flush && !dc->disk_flushes)
1224 wo = WO_drain_io;
1225 if (wo == WO_drain_io && !dc->disk_drain)
1226 wo = WO_none;
1227 put_ldev(mdev);
1228 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001229 rcu_read_unlock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001230 tconn->write_ordering = wo;
1231 if (pwo != tconn->write_ordering || wo == WO_bdev_flush)
1232 conn_info(tconn, "Method to ensure write ordering: %s\n", write_ordering_str[tconn->write_ordering]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001233}
1234
1235/**
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001236 * drbd_submit_peer_request()
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001237 * @mdev: DRBD device.
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001238 * @peer_req: peer request
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001239 * @rw: flag field, see bio->bi_rw
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001240 *
1241 * May spread the pages to multiple bios,
1242 * depending on bio_add_page restrictions.
1243 *
1244 * Returns 0 if all bios have been submitted,
1245 * -ENOMEM if we could not allocate enough bios,
1246 * -ENOSPC (any better suggestion?) if we have not been able to bio_add_page a
1247 * single page to an empty bio (which should never happen and likely indicates
1248 * that the lower level IO stack is in some way broken). This has been observed
1249 * on certain Xen deployments.
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001250 */
1251/* TODO allocate from our own bio_set. */
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001252int drbd_submit_peer_request(struct drbd_conf *mdev,
1253 struct drbd_peer_request *peer_req,
1254 const unsigned rw, const int fault_type)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001255{
1256 struct bio *bios = NULL;
1257 struct bio *bio;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001258 struct page *page = peer_req->pages;
1259 sector_t sector = peer_req->i.sector;
1260 unsigned ds = peer_req->i.size;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001261 unsigned n_bios = 0;
1262 unsigned nr_pages = (ds + PAGE_SIZE -1) >> PAGE_SHIFT;
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001263 int err = -ENOMEM;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001264
1265 /* In most cases, we will only need one bio. But in case the lower
1266 * level restrictions happen to be different at this offset on this
1267 * side than those of the sending peer, we may need to submit the
Lars Ellenbergda4a75d2011-02-23 17:02:01 +01001268 * request in more than one bio.
1269 *
1270 * Plain bio_alloc is good enough here, this is no DRBD internally
1271 * generated bio, but a bio allocated on behalf of the peer.
1272 */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001273next_bio:
1274 bio = bio_alloc(GFP_NOIO, nr_pages);
1275 if (!bio) {
1276 dev_err(DEV, "submit_ee: Allocation of a bio failed\n");
1277 goto fail;
1278 }
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001279 /* > peer_req->i.sector, unless this is the first bio */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001280 bio->bi_sector = sector;
1281 bio->bi_bdev = mdev->ldev->backing_bdev;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001282 bio->bi_rw = rw;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001283 bio->bi_private = peer_req;
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01001284 bio->bi_end_io = drbd_peer_request_endio;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001285
1286 bio->bi_next = bios;
1287 bios = bio;
1288 ++n_bios;
1289
1290 page_chain_for_each(page) {
1291 unsigned len = min_t(unsigned, ds, PAGE_SIZE);
1292 if (!bio_add_page(bio, page, len, 0)) {
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001293 /* A single page must always be possible!
1294 * But in case it fails anyways,
1295 * we deal with it, and complain (below). */
1296 if (bio->bi_vcnt == 0) {
1297 dev_err(DEV,
1298 "bio_add_page failed for len=%u, "
1299 "bi_vcnt=0 (bi_sector=%llu)\n",
1300 len, (unsigned long long)bio->bi_sector);
1301 err = -ENOSPC;
1302 goto fail;
1303 }
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001304 goto next_bio;
1305 }
1306 ds -= len;
1307 sector += len >> 9;
1308 --nr_pages;
1309 }
1310 D_ASSERT(page == NULL);
1311 D_ASSERT(ds == 0);
1312
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001313 atomic_set(&peer_req->pending_bios, n_bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001314 do {
1315 bio = bios;
1316 bios = bios->bi_next;
1317 bio->bi_next = NULL;
1318
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001319 drbd_generic_make_request(mdev, fault_type, bio);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001320 } while (bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001321 return 0;
1322
1323fail:
1324 while (bios) {
1325 bio = bios;
1326 bios = bios->bi_next;
1327 bio_put(bio);
1328 }
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001329 return err;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001330}
1331
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001332static void drbd_remove_epoch_entry_interval(struct drbd_conf *mdev,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001333 struct drbd_peer_request *peer_req)
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001334{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001335 struct drbd_interval *i = &peer_req->i;
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001336
1337 drbd_remove_interval(&mdev->write_requests, i);
1338 drbd_clear_interval(i);
1339
Andreas Gruenbacher6c852be2011-02-04 15:38:52 +01001340 /* Wake up any processes waiting for this peer request to complete. */
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001341 if (i->waiting)
1342 wake_up(&mdev->misc_wait);
1343}
1344
Philipp Reisner77fede52011-11-10 21:19:11 +01001345void conn_wait_active_ee_empty(struct drbd_tconn *tconn)
1346{
1347 struct drbd_conf *mdev;
1348 int vnr;
1349
1350 rcu_read_lock();
1351 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1352 kref_get(&mdev->kref);
1353 rcu_read_unlock();
1354 drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
1355 kref_put(&mdev->kref, &drbd_minor_destroy);
1356 rcu_read_lock();
1357 }
1358 rcu_read_unlock();
1359}
1360
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001361static int receive_Barrier(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001362{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001363 struct drbd_conf *mdev;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001364 int rv;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001365 struct p_barrier *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001366 struct drbd_epoch *epoch;
1367
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001368 mdev = vnr_to_mdev(tconn, pi->vnr);
1369 if (!mdev)
1370 return -EIO;
1371
Philipp Reisnerb411b362009-09-25 16:07:19 -07001372 inc_unacked(mdev);
1373
Philipp Reisner12038a32011-11-09 19:18:00 +01001374 tconn->current_epoch->barrier_nr = p->barrier;
1375 tconn->current_epoch->mdev = mdev;
Philipp Reisner1e9dd292011-11-10 15:14:53 +01001376 rv = drbd_may_finish_epoch(tconn, tconn->current_epoch, EV_GOT_BARRIER_NR);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001377
1378 /* P_BARRIER_ACK may imply that the corresponding extent is dropped from
1379 * the activity log, which means it would not be resynced in case the
1380 * R_PRIMARY crashes now.
1381 * Therefore we must send the barrier_ack after the barrier request was
1382 * completed. */
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001383 switch (tconn->write_ordering) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001384 case WO_none:
1385 if (rv == FE_RECYCLED)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001386 return 0;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001387
1388 /* receiver context, in the writeout path of the other node.
1389 * avoid potential distributed deadlock */
1390 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1391 if (epoch)
1392 break;
1393 else
1394 dev_warn(DEV, "Allocation of an epoch failed, slowing down\n");
1395 /* Fall through */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001396
1397 case WO_bdev_flush:
1398 case WO_drain_io:
Philipp Reisner77fede52011-11-10 21:19:11 +01001399 conn_wait_active_ee_empty(tconn);
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001400 drbd_flush(tconn);
Philipp Reisner2451fc32010-08-24 13:43:11 +02001401
Philipp Reisner12038a32011-11-09 19:18:00 +01001402 if (atomic_read(&tconn->current_epoch->epoch_size)) {
Philipp Reisner2451fc32010-08-24 13:43:11 +02001403 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1404 if (epoch)
1405 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001406 }
1407
Philipp Reisner12038a32011-11-09 19:18:00 +01001408 epoch = tconn->current_epoch;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001409 wait_event(mdev->ee_wait, atomic_read(&epoch->epoch_size) == 0);
1410
1411 D_ASSERT(atomic_read(&epoch->active) == 0);
1412 D_ASSERT(epoch->flags == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001413
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001414 return 0;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001415 default:
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001416 dev_err(DEV, "Strangeness in tconn->write_ordering %d\n", tconn->write_ordering);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001417 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001418 }
1419
1420 epoch->flags = 0;
1421 atomic_set(&epoch->epoch_size, 0);
1422 atomic_set(&epoch->active, 0);
1423
Philipp Reisner12038a32011-11-09 19:18:00 +01001424 spin_lock(&tconn->epoch_lock);
1425 if (atomic_read(&tconn->current_epoch->epoch_size)) {
1426 list_add(&epoch->list, &tconn->current_epoch->list);
1427 tconn->current_epoch = epoch;
1428 tconn->epochs++;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001429 } else {
1430 /* The current_epoch got recycled while we allocated this one... */
1431 kfree(epoch);
1432 }
Philipp Reisner12038a32011-11-09 19:18:00 +01001433 spin_unlock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001434
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001435 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001436}
1437
1438/* used from receive_RSDataReply (recv_resync_read)
1439 * and from receive_Data */
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001440static struct drbd_peer_request *
1441read_in_block(struct drbd_conf *mdev, u64 id, sector_t sector,
1442 int data_size) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001443{
Lars Ellenberg66660322010-04-06 12:15:04 +02001444 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001445 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001446 struct page *page;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001447 int dgs, ds, err;
Philipp Reisnera0638452011-01-19 14:31:32 +01001448 void *dig_in = mdev->tconn->int_dig_in;
1449 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001450 unsigned long *data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001451
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001452 dgs = 0;
1453 if (mdev->tconn->peer_integrity_tfm) {
1454 dgs = crypto_hash_digestsize(mdev->tconn->peer_integrity_tfm);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001455 /*
1456 * FIXME: Receive the incoming digest into the receive buffer
1457 * here, together with its struct p_data?
1458 */
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001459 err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs);
1460 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001461 return NULL;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001462 data_size -= dgs;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001463 }
1464
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001465 if (!expect(data_size != 0))
1466 return NULL;
1467 if (!expect(IS_ALIGNED(data_size, 512)))
1468 return NULL;
1469 if (!expect(data_size <= DRBD_MAX_BIO_SIZE))
1470 return NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001471
Lars Ellenberg66660322010-04-06 12:15:04 +02001472 /* even though we trust out peer,
1473 * we sometimes have to double check. */
1474 if (sector + (data_size>>9) > capacity) {
Lars Ellenbergfdda6542011-01-24 15:11:01 +01001475 dev_err(DEV, "request from peer beyond end of local disk: "
1476 "capacity: %llus < sector: %llus + size: %u\n",
Lars Ellenberg66660322010-04-06 12:15:04 +02001477 (unsigned long long)capacity,
1478 (unsigned long long)sector, data_size);
1479 return NULL;
1480 }
1481
Philipp Reisnerb411b362009-09-25 16:07:19 -07001482 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
1483 * "criss-cross" setup, that might cause write-out on some other DRBD,
1484 * which in turn might block on the other node at this very place. */
Andreas Gruenbacher0db55362011-04-06 16:09:15 +02001485 peer_req = drbd_alloc_peer_req(mdev, id, sector, data_size, GFP_NOIO);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001486 if (!peer_req)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001487 return NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001488
Philipp Reisnerb411b362009-09-25 16:07:19 -07001489 ds = data_size;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001490 page = peer_req->pages;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001491 page_chain_for_each(page) {
1492 unsigned len = min_t(int, ds, PAGE_SIZE);
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001493 data = kmap(page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001494 err = drbd_recv_all_warn(mdev->tconn, data, len);
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +01001495 if (drbd_insert_fault(mdev, DRBD_FAULT_RECEIVE)) {
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001496 dev_err(DEV, "Fault injection: Corrupting data on receive\n");
1497 data[0] = data[0] ^ (unsigned long)-1;
1498 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001499 kunmap(page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001500 if (err) {
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001501 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001502 return NULL;
1503 }
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001504 ds -= len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001505 }
1506
1507 if (dgs) {
Andreas Gruenbacher5b614ab2011-04-27 21:00:12 +02001508 drbd_csum_ee(mdev, mdev->tconn->peer_integrity_tfm, peer_req, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001509 if (memcmp(dig_in, dig_vv, dgs)) {
Lars Ellenberg470be442010-11-10 10:36:52 +01001510 dev_err(DEV, "Digest integrity check FAILED: %llus +%u\n",
1511 (unsigned long long)sector, data_size);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001512 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001513 return NULL;
1514 }
1515 }
1516 mdev->recv_cnt += data_size>>9;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001517 return peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001518}
1519
1520/* drbd_drain_block() just takes a data block
1521 * out of the socket input buffer, and discards it.
1522 */
1523static int drbd_drain_block(struct drbd_conf *mdev, int data_size)
1524{
1525 struct page *page;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001526 int err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001527 void *data;
1528
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001529 if (!data_size)
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001530 return 0;
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001531
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +02001532 page = drbd_alloc_pages(mdev, 1, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001533
1534 data = kmap(page);
1535 while (data_size) {
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001536 unsigned int len = min_t(int, data_size, PAGE_SIZE);
1537
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001538 err = drbd_recv_all_warn(mdev->tconn, data, len);
1539 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001540 break;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001541 data_size -= len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001542 }
1543 kunmap(page);
Andreas Gruenbacher5cc287e2011-04-07 21:02:59 +02001544 drbd_free_pages(mdev, page, 0);
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001545 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001546}
1547
1548static int recv_dless_read(struct drbd_conf *mdev, struct drbd_request *req,
1549 sector_t sector, int data_size)
1550{
1551 struct bio_vec *bvec;
1552 struct bio *bio;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001553 int dgs, err, i, expect;
Philipp Reisnera0638452011-01-19 14:31:32 +01001554 void *dig_in = mdev->tconn->int_dig_in;
1555 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001556
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001557 dgs = 0;
1558 if (mdev->tconn->peer_integrity_tfm) {
1559 dgs = crypto_hash_digestsize(mdev->tconn->peer_integrity_tfm);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001560 err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs);
1561 if (err)
1562 return err;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001563 data_size -= dgs;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001564 }
1565
Philipp Reisnerb411b362009-09-25 16:07:19 -07001566 /* optimistically update recv_cnt. if receiving fails below,
1567 * we disconnect anyways, and counters will be reset. */
1568 mdev->recv_cnt += data_size>>9;
1569
1570 bio = req->master_bio;
1571 D_ASSERT(sector == bio->bi_sector);
1572
1573 bio_for_each_segment(bvec, bio, i) {
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001574 void *mapped = kmap(bvec->bv_page) + bvec->bv_offset;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001575 expect = min_t(int, data_size, bvec->bv_len);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001576 err = drbd_recv_all_warn(mdev->tconn, mapped, expect);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001577 kunmap(bvec->bv_page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001578 if (err)
1579 return err;
1580 data_size -= expect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001581 }
1582
1583 if (dgs) {
Andreas Gruenbacher5b614ab2011-04-27 21:00:12 +02001584 drbd_csum_bio(mdev, mdev->tconn->peer_integrity_tfm, bio, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001585 if (memcmp(dig_in, dig_vv, dgs)) {
1586 dev_err(DEV, "Digest integrity check FAILED. Broken NICs?\n");
Andreas Gruenbacher28284ce2011-03-16 17:54:02 +01001587 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001588 }
1589 }
1590
1591 D_ASSERT(data_size == 0);
Andreas Gruenbacher28284ce2011-03-16 17:54:02 +01001592 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001593}
1594
Andreas Gruenbachera990be42011-04-06 17:56:48 +02001595/*
1596 * e_end_resync_block() is called in asender context via
1597 * drbd_finish_peer_reqs().
1598 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001599static int e_end_resync_block(struct drbd_work *w, int unused)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001600{
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001601 struct drbd_peer_request *peer_req =
1602 container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001603 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001604 sector_t sector = peer_req->i.sector;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001605 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001606
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001607 D_ASSERT(drbd_interval_empty(&peer_req->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001608
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001609 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
1610 drbd_set_in_sync(mdev, sector, peer_req->i.size);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001611 err = drbd_send_ack(mdev, P_RS_WRITE_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001612 } else {
1613 /* Record failure to sync */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001614 drbd_rs_failed_io(mdev, sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001615
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001616 err = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001617 }
1618 dec_unacked(mdev);
1619
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001620 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001621}
1622
1623static int recv_resync_read(struct drbd_conf *mdev, sector_t sector, int data_size) __releases(local)
1624{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001625 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001626
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001627 peer_req = read_in_block(mdev, ID_SYNCER, sector, data_size);
1628 if (!peer_req)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001629 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001630
1631 dec_rs_pending(mdev);
1632
Philipp Reisnerb411b362009-09-25 16:07:19 -07001633 inc_unacked(mdev);
1634 /* corresponding dec_unacked() in e_end_resync_block()
1635 * respective _drbd_clear_done_ee */
1636
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001637 peer_req->w.cb = e_end_resync_block;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001638
Philipp Reisner87eeee42011-01-19 14:16:30 +01001639 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001640 list_add(&peer_req->w.list, &mdev->sync_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001641 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001642
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001643 atomic_add(data_size >> 9, &mdev->rs_sect_ev);
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001644 if (drbd_submit_peer_request(mdev, peer_req, WRITE, DRBD_FAULT_RS_WR) == 0)
Andreas Gruenbachere1c1b0f2011-03-16 17:58:27 +01001645 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001646
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001647 /* don't care for the reason here */
1648 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01001649 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001650 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001651 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02001652
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001653 drbd_free_peer_req(mdev, peer_req);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001654fail:
1655 put_ldev(mdev);
Andreas Gruenbachere1c1b0f2011-03-16 17:58:27 +01001656 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001657}
1658
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001659static struct drbd_request *
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001660find_request(struct drbd_conf *mdev, struct rb_root *root, u64 id,
1661 sector_t sector, bool missing_ok, const char *func)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001662{
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001663 struct drbd_request *req;
1664
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001665 /* Request object according to our peer */
1666 req = (struct drbd_request *)(unsigned long)id;
Andreas Gruenbacher5e472262011-01-27 14:42:51 +01001667 if (drbd_contains_interval(root, sector, &req->i) && req->i.local)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001668 return req;
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001669 if (!missing_ok) {
Andreas Gruenbacher5af172e2011-07-15 09:43:23 +02001670 dev_err(DEV, "%s: failed to find request 0x%lx, sector %llus\n", func,
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001671 (unsigned long)id, (unsigned long long)sector);
1672 }
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001673 return NULL;
1674}
1675
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001676static int receive_DataReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001677{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001678 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001679 struct drbd_request *req;
1680 sector_t sector;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001681 int err;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001682 struct p_data *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001683
1684 mdev = vnr_to_mdev(tconn, pi->vnr);
1685 if (!mdev)
1686 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001687
1688 sector = be64_to_cpu(p->sector);
1689
Philipp Reisner87eeee42011-01-19 14:16:30 +01001690 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001691 req = find_request(mdev, &mdev->read_requests, p->block_id, sector, false, __func__);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001692 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001693 if (unlikely(!req))
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001694 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001695
Bart Van Assche24c48302011-05-21 18:32:29 +02001696 /* hlist_del(&req->collision) is done in _req_may_be_done, to avoid
Philipp Reisnerb411b362009-09-25 16:07:19 -07001697 * special casing it there for the various failure cases.
1698 * still no race with drbd_fail_pending_reads */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001699 err = recv_dless_read(mdev, req, sector, pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001700 if (!err)
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001701 req_mod(req, DATA_RECEIVED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001702 /* else: nothing. handled from drbd_disconnect...
1703 * I don't think we may complete this just yet
1704 * in case we are "on-disconnect: freeze" */
1705
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001706 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001707}
1708
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001709static int receive_RSDataReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001710{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001711 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001712 sector_t sector;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001713 int err;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001714 struct p_data *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001715
1716 mdev = vnr_to_mdev(tconn, pi->vnr);
1717 if (!mdev)
1718 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001719
1720 sector = be64_to_cpu(p->sector);
1721 D_ASSERT(p->block_id == ID_SYNCER);
1722
1723 if (get_ldev(mdev)) {
1724 /* data is submitted to disk within recv_resync_read.
1725 * corresponding put_ldev done below on error,
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01001726 * or in drbd_peer_request_endio. */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001727 err = recv_resync_read(mdev, sector, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001728 } else {
1729 if (__ratelimit(&drbd_ratelimit_state))
1730 dev_err(DEV, "Can not write resync data to local disk.\n");
1731
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001732 err = drbd_drain_block(mdev, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001733
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001734 drbd_send_ack_dp(mdev, P_NEG_ACK, p, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001735 }
1736
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001737 atomic_add(pi->size >> 9, &mdev->rs_sect_in);
Philipp Reisner778f2712010-07-06 11:14:00 +02001738
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001739 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001740}
1741
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001742static int w_restart_write(struct drbd_work *w, int cancel)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001743{
1744 struct drbd_request *req = container_of(w, struct drbd_request, w);
1745 struct drbd_conf *mdev = w->mdev;
1746 struct bio *bio;
1747 unsigned long start_time;
1748 unsigned long flags;
1749
1750 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
1751 if (!expect(req->rq_state & RQ_POSTPONED)) {
1752 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001753 return -EIO;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001754 }
1755 bio = req->master_bio;
1756 start_time = req->start_time;
1757 /* Postponed requests will not have their master_bio completed! */
1758 __req_mod(req, DISCARD_WRITE, NULL);
1759 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
1760
1761 while (__drbd_make_request(mdev, bio, start_time))
1762 /* retry */ ;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001763 return 0;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001764}
1765
1766static void restart_conflicting_writes(struct drbd_conf *mdev,
1767 sector_t sector, int size)
1768{
1769 struct drbd_interval *i;
1770 struct drbd_request *req;
1771
1772 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
1773 if (!i->local)
1774 continue;
1775 req = container_of(i, struct drbd_request, i);
1776 if (req->rq_state & RQ_LOCAL_PENDING ||
1777 !(req->rq_state & RQ_POSTPONED))
1778 continue;
1779 if (expect(list_empty(&req->w.list))) {
1780 req->w.mdev = mdev;
1781 req->w.cb = w_restart_write;
1782 drbd_queue_work(&mdev->tconn->data.work, &req->w);
1783 }
1784 }
1785}
1786
Andreas Gruenbachera990be42011-04-06 17:56:48 +02001787/*
1788 * e_end_block() is called in asender context via drbd_finish_peer_reqs().
Philipp Reisnerb411b362009-09-25 16:07:19 -07001789 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001790static int e_end_block(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001791{
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001792 struct drbd_peer_request *peer_req =
1793 container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001794 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001795 sector_t sector = peer_req->i.sector;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001796 int err = 0, pcmd;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001797
Philipp Reisner303d1442011-04-13 16:24:47 -07001798 if (peer_req->flags & EE_SEND_WRITE_ACK) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001799 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001800 pcmd = (mdev->state.conn >= C_SYNC_SOURCE &&
1801 mdev->state.conn <= C_PAUSED_SYNC_T &&
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001802 peer_req->flags & EE_MAY_SET_IN_SYNC) ?
Philipp Reisnerb411b362009-09-25 16:07:19 -07001803 P_RS_WRITE_ACK : P_WRITE_ACK;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001804 err = drbd_send_ack(mdev, pcmd, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001805 if (pcmd == P_RS_WRITE_ACK)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001806 drbd_set_in_sync(mdev, sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001807 } else {
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001808 err = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001809 /* we expect it to be marked out of sync anyways...
1810 * maybe assert this? */
1811 }
1812 dec_unacked(mdev);
1813 }
1814 /* we delete from the conflict detection hash _after_ we sent out the
1815 * P_WRITE_ACK / P_NEG_ACK, to get the sequence number right. */
Philipp Reisner302bdea2011-04-21 11:36:49 +02001816 if (peer_req->flags & EE_IN_INTERVAL_TREE) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01001817 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001818 D_ASSERT(!drbd_interval_empty(&peer_req->i));
1819 drbd_remove_epoch_entry_interval(mdev, peer_req);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001820 if (peer_req->flags & EE_RESTART_REQUESTS)
1821 restart_conflicting_writes(mdev, sector, peer_req->i.size);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001822 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbb3bfe92011-01-21 15:59:23 +01001823 } else
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001824 D_ASSERT(drbd_interval_empty(&peer_req->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001825
Philipp Reisner1e9dd292011-11-10 15:14:53 +01001826 drbd_may_finish_epoch(mdev->tconn, peer_req->epoch, EV_PUT + (cancel ? EV_CLEANUP : 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001827
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001828 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001829}
1830
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001831static int e_send_ack(struct drbd_work *w, enum drbd_packet ack)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001832{
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001833 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001834 struct drbd_peer_request *peer_req =
1835 container_of(w, struct drbd_peer_request, w);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001836 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001837
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001838 err = drbd_send_ack(mdev, ack, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001839 dec_unacked(mdev);
1840
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001841 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001842}
1843
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001844static int e_send_discard_write(struct drbd_work *w, int unused)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001845{
1846 return e_send_ack(w, P_DISCARD_WRITE);
1847}
1848
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001849static int e_send_retry_write(struct drbd_work *w, int unused)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001850{
1851 struct drbd_tconn *tconn = w->mdev->tconn;
1852
1853 return e_send_ack(w, tconn->agreed_pro_version >= 100 ?
1854 P_RETRY_WRITE : P_DISCARD_WRITE);
1855}
1856
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001857static bool seq_greater(u32 a, u32 b)
1858{
1859 /*
1860 * We assume 32-bit wrap-around here.
1861 * For 24-bit wrap-around, we would have to shift:
1862 * a <<= 8; b <<= 8;
1863 */
1864 return (s32)a - (s32)b > 0;
1865}
1866
1867static u32 seq_max(u32 a, u32 b)
1868{
1869 return seq_greater(a, b) ? a : b;
1870}
1871
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001872static bool need_peer_seq(struct drbd_conf *mdev)
1873{
1874 struct drbd_tconn *tconn = mdev->tconn;
Philipp Reisner302bdea2011-04-21 11:36:49 +02001875 int tp;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001876
1877 /*
1878 * We only need to keep track of the last packet_seq number of our peer
1879 * if we are in dual-primary mode and we have the discard flag set; see
1880 * handle_write_conflicts().
1881 */
Philipp Reisner302bdea2011-04-21 11:36:49 +02001882
1883 rcu_read_lock();
1884 tp = rcu_dereference(mdev->tconn->net_conf)->two_primaries;
1885 rcu_read_unlock();
1886
1887 return tp && test_bit(DISCARD_CONCURRENT, &tconn->flags);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001888}
1889
Andreas Gruenbacher43ae0772011-02-03 18:42:08 +01001890static void update_peer_seq(struct drbd_conf *mdev, unsigned int peer_seq)
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001891{
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001892 unsigned int newest_peer_seq;
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001893
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001894 if (need_peer_seq(mdev)) {
1895 spin_lock(&mdev->peer_seq_lock);
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001896 newest_peer_seq = seq_max(mdev->peer_seq, peer_seq);
1897 mdev->peer_seq = newest_peer_seq;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001898 spin_unlock(&mdev->peer_seq_lock);
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001899 /* wake up only if we actually changed mdev->peer_seq */
1900 if (peer_seq == newest_peer_seq)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001901 wake_up(&mdev->seq_wait);
1902 }
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001903}
1904
Philipp Reisnerb411b362009-09-25 16:07:19 -07001905/* Called from receive_Data.
1906 * Synchronize packets on sock with packets on msock.
1907 *
1908 * This is here so even when a P_DATA packet traveling via sock overtook an Ack
1909 * packet traveling on msock, they are still processed in the order they have
1910 * been sent.
1911 *
1912 * Note: we don't care for Ack packets overtaking P_DATA packets.
1913 *
1914 * In case packet_seq is larger than mdev->peer_seq number, there are
1915 * outstanding packets on the msock. We wait for them to arrive.
1916 * In case we are the logically next packet, we update mdev->peer_seq
1917 * ourselves. Correctly handles 32bit wrap around.
1918 *
1919 * Assume we have a 10 GBit connection, that is about 1<<30 byte per second,
1920 * about 1<<21 sectors per second. So "worst" case, we have 1<<3 == 8 seconds
1921 * for the 24bit wrap (historical atomic_t guarantee on some archs), and we have
1922 * 1<<9 == 512 seconds aka ages for the 32bit wrap around...
1923 *
1924 * returns 0 if we may process the packet,
1925 * -ERESTARTSYS if we were interrupted (by disconnect signal). */
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001926static int wait_for_and_update_peer_seq(struct drbd_conf *mdev, const u32 peer_seq)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001927{
1928 DEFINE_WAIT(wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001929 long timeout;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001930 int ret;
1931
1932 if (!need_peer_seq(mdev))
1933 return 0;
1934
Philipp Reisnerb411b362009-09-25 16:07:19 -07001935 spin_lock(&mdev->peer_seq_lock);
1936 for (;;) {
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001937 if (!seq_greater(peer_seq - 1, mdev->peer_seq)) {
1938 mdev->peer_seq = seq_max(mdev->peer_seq, peer_seq);
1939 ret = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001940 break;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001941 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001942 if (signal_pending(current)) {
1943 ret = -ERESTARTSYS;
1944 break;
1945 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001946 prepare_to_wait(&mdev->seq_wait, &wait, TASK_INTERRUPTIBLE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001947 spin_unlock(&mdev->peer_seq_lock);
Philipp Reisner44ed1672011-04-19 17:10:19 +02001948 rcu_read_lock();
1949 timeout = rcu_dereference(mdev->tconn->net_conf)->ping_timeo*HZ/10;
1950 rcu_read_unlock();
Andreas Gruenbacher71b1c1e2011-03-01 15:40:43 +01001951 timeout = schedule_timeout(timeout);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001952 spin_lock(&mdev->peer_seq_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001953 if (!timeout) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001954 ret = -ETIMEDOUT;
Andreas Gruenbacher71b1c1e2011-03-01 15:40:43 +01001955 dev_err(DEV, "Timed out waiting for missing ack packets; disconnecting\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001956 break;
1957 }
1958 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001959 spin_unlock(&mdev->peer_seq_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001960 finish_wait(&mdev->seq_wait, &wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001961 return ret;
1962}
1963
Lars Ellenberg688593c2010-11-17 22:25:03 +01001964/* see also bio_flags_to_wire()
1965 * DRBD_REQ_*, because we need to semantically map the flags to data packet
1966 * flags and back. We may replicate to other kernel versions. */
1967static unsigned long wire_flags_to_bio(struct drbd_conf *mdev, u32 dpf)
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001968{
Lars Ellenberg688593c2010-11-17 22:25:03 +01001969 return (dpf & DP_RW_SYNC ? REQ_SYNC : 0) |
1970 (dpf & DP_FUA ? REQ_FUA : 0) |
1971 (dpf & DP_FLUSH ? REQ_FLUSH : 0) |
1972 (dpf & DP_DISCARD ? REQ_DISCARD : 0);
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001973}
1974
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001975static void fail_postponed_requests(struct drbd_conf *mdev, sector_t sector,
1976 unsigned int size)
1977{
1978 struct drbd_interval *i;
1979
1980 repeat:
1981 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
1982 struct drbd_request *req;
1983 struct bio_and_error m;
1984
1985 if (!i->local)
1986 continue;
1987 req = container_of(i, struct drbd_request, i);
1988 if (!(req->rq_state & RQ_POSTPONED))
1989 continue;
1990 req->rq_state &= ~RQ_POSTPONED;
1991 __req_mod(req, NEG_ACKED, &m);
1992 spin_unlock_irq(&mdev->tconn->req_lock);
1993 if (m.bio)
1994 complete_master_bio(mdev, &m);
1995 spin_lock_irq(&mdev->tconn->req_lock);
1996 goto repeat;
1997 }
1998}
1999
2000static int handle_write_conflicts(struct drbd_conf *mdev,
2001 struct drbd_peer_request *peer_req)
2002{
2003 struct drbd_tconn *tconn = mdev->tconn;
2004 bool resolve_conflicts = test_bit(DISCARD_CONCURRENT, &tconn->flags);
2005 sector_t sector = peer_req->i.sector;
2006 const unsigned int size = peer_req->i.size;
2007 struct drbd_interval *i;
2008 bool equal;
2009 int err;
2010
2011 /*
2012 * Inserting the peer request into the write_requests tree will prevent
2013 * new conflicting local requests from being added.
2014 */
2015 drbd_insert_interval(&mdev->write_requests, &peer_req->i);
2016
2017 repeat:
2018 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
2019 if (i == &peer_req->i)
2020 continue;
2021
2022 if (!i->local) {
2023 /*
2024 * Our peer has sent a conflicting remote request; this
2025 * should not happen in a two-node setup. Wait for the
2026 * earlier peer request to complete.
2027 */
2028 err = drbd_wait_misc(mdev, i);
2029 if (err)
2030 goto out;
2031 goto repeat;
2032 }
2033
2034 equal = i->sector == sector && i->size == size;
2035 if (resolve_conflicts) {
2036 /*
2037 * If the peer request is fully contained within the
2038 * overlapping request, it can be discarded; otherwise,
2039 * it will be retried once all overlapping requests
2040 * have completed.
2041 */
2042 bool discard = i->sector <= sector && i->sector +
2043 (i->size >> 9) >= sector + (size >> 9);
2044
2045 if (!equal)
2046 dev_alert(DEV, "Concurrent writes detected: "
2047 "local=%llus +%u, remote=%llus +%u, "
2048 "assuming %s came first\n",
2049 (unsigned long long)i->sector, i->size,
2050 (unsigned long long)sector, size,
2051 discard ? "local" : "remote");
2052
2053 inc_unacked(mdev);
2054 peer_req->w.cb = discard ? e_send_discard_write :
2055 e_send_retry_write;
2056 list_add_tail(&peer_req->w.list, &mdev->done_ee);
2057 wake_asender(mdev->tconn);
2058
2059 err = -ENOENT;
2060 goto out;
2061 } else {
2062 struct drbd_request *req =
2063 container_of(i, struct drbd_request, i);
2064
2065 if (!equal)
2066 dev_alert(DEV, "Concurrent writes detected: "
2067 "local=%llus +%u, remote=%llus +%u\n",
2068 (unsigned long long)i->sector, i->size,
2069 (unsigned long long)sector, size);
2070
2071 if (req->rq_state & RQ_LOCAL_PENDING ||
2072 !(req->rq_state & RQ_POSTPONED)) {
2073 /*
2074 * Wait for the node with the discard flag to
2075 * decide if this request will be discarded or
2076 * retried. Requests that are discarded will
2077 * disappear from the write_requests tree.
2078 *
2079 * In addition, wait for the conflicting
2080 * request to finish locally before submitting
2081 * the conflicting peer request.
2082 */
2083 err = drbd_wait_misc(mdev, &req->i);
2084 if (err) {
2085 _conn_request_state(mdev->tconn,
2086 NS(conn, C_TIMEOUT),
2087 CS_HARD);
2088 fail_postponed_requests(mdev, sector, size);
2089 goto out;
2090 }
2091 goto repeat;
2092 }
2093 /*
2094 * Remember to restart the conflicting requests after
2095 * the new peer request has completed.
2096 */
2097 peer_req->flags |= EE_RESTART_REQUESTS;
2098 }
2099 }
2100 err = 0;
2101
2102 out:
2103 if (err)
2104 drbd_remove_epoch_entry_interval(mdev, peer_req);
2105 return err;
2106}
2107
Philipp Reisnerb411b362009-09-25 16:07:19 -07002108/* mirrored write */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002109static int receive_Data(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002110{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002111 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002112 sector_t sector;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002113 struct drbd_peer_request *peer_req;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02002114 struct p_data *p = pi->data;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002115 u32 peer_seq = be32_to_cpu(p->seq_num);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002116 int rw = WRITE;
2117 u32 dp_flags;
Philipp Reisner302bdea2011-04-21 11:36:49 +02002118 int err, tp;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002119
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002120 mdev = vnr_to_mdev(tconn, pi->vnr);
2121 if (!mdev)
2122 return -EIO;
2123
Philipp Reisnerb411b362009-09-25 16:07:19 -07002124 if (!get_ldev(mdev)) {
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002125 int err2;
2126
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002127 err = wait_for_and_update_peer_seq(mdev, peer_seq);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002128 drbd_send_ack_dp(mdev, P_NEG_ACK, p, pi->size);
Philipp Reisner12038a32011-11-09 19:18:00 +01002129 atomic_inc(&tconn->current_epoch->epoch_size);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002130 err2 = drbd_drain_block(mdev, pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002131 if (!err)
2132 err = err2;
2133 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002134 }
2135
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01002136 /*
2137 * Corresponding put_ldev done either below (on various errors), or in
2138 * drbd_peer_request_endio, if we successfully submit the data at the
2139 * end of this function.
2140 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002141
2142 sector = be64_to_cpu(p->sector);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002143 peer_req = read_in_block(mdev, p->block_id, sector, pi->size);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002144 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002145 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002146 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002147 }
2148
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002149 peer_req->w.cb = e_end_block;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002150
Lars Ellenberg688593c2010-11-17 22:25:03 +01002151 dp_flags = be32_to_cpu(p->dp_flags);
2152 rw |= wire_flags_to_bio(mdev, dp_flags);
2153
2154 if (dp_flags & DP_MAY_SET_IN_SYNC)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002155 peer_req->flags |= EE_MAY_SET_IN_SYNC;
Lars Ellenberg688593c2010-11-17 22:25:03 +01002156
Philipp Reisner12038a32011-11-09 19:18:00 +01002157 spin_lock(&tconn->epoch_lock);
2158 peer_req->epoch = tconn->current_epoch;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002159 atomic_inc(&peer_req->epoch->epoch_size);
2160 atomic_inc(&peer_req->epoch->active);
Philipp Reisner12038a32011-11-09 19:18:00 +01002161 spin_unlock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002162
Philipp Reisner302bdea2011-04-21 11:36:49 +02002163 rcu_read_lock();
2164 tp = rcu_dereference(mdev->tconn->net_conf)->two_primaries;
2165 rcu_read_unlock();
2166 if (tp) {
2167 peer_req->flags |= EE_IN_INTERVAL_TREE;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002168 err = wait_for_and_update_peer_seq(mdev, peer_seq);
2169 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002170 goto out_interrupted;
Philipp Reisner87eeee42011-01-19 14:16:30 +01002171 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002172 err = handle_write_conflicts(mdev, peer_req);
2173 if (err) {
2174 spin_unlock_irq(&mdev->tconn->req_lock);
2175 if (err == -ENOENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002176 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002177 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002178 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002179 goto out_interrupted;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002180 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002181 } else
2182 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002183 list_add(&peer_req->w.list, &mdev->active_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002184 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002185
Philipp Reisner303d1442011-04-13 16:24:47 -07002186 if (mdev->tconn->agreed_pro_version < 100) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02002187 rcu_read_lock();
2188 switch (rcu_dereference(mdev->tconn->net_conf)->wire_protocol) {
Philipp Reisner303d1442011-04-13 16:24:47 -07002189 case DRBD_PROT_C:
2190 dp_flags |= DP_SEND_WRITE_ACK;
2191 break;
2192 case DRBD_PROT_B:
2193 dp_flags |= DP_SEND_RECEIVE_ACK;
2194 break;
2195 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002196 rcu_read_unlock();
Philipp Reisner303d1442011-04-13 16:24:47 -07002197 }
2198
2199 if (dp_flags & DP_SEND_WRITE_ACK) {
2200 peer_req->flags |= EE_SEND_WRITE_ACK;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002201 inc_unacked(mdev);
2202 /* corresponding dec_unacked() in e_end_block()
2203 * respective _drbd_clear_done_ee */
Philipp Reisner303d1442011-04-13 16:24:47 -07002204 }
2205
2206 if (dp_flags & DP_SEND_RECEIVE_ACK) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002207 /* I really don't like it that the receiver thread
2208 * sends on the msock, but anyways */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002209 drbd_send_ack(mdev, P_RECV_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002210 }
2211
Lars Ellenberg6719fb02010-10-18 23:04:07 +02002212 if (mdev->state.pdsk < D_INCONSISTENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002213 /* In case we have the only disk of the cluster, */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002214 drbd_set_out_of_sync(mdev, peer_req->i.sector, peer_req->i.size);
2215 peer_req->flags |= EE_CALL_AL_COMPLETE_IO;
2216 peer_req->flags &= ~EE_MAY_SET_IN_SYNC;
Lars Ellenberg181286a2011-03-31 15:18:56 +02002217 drbd_al_begin_io(mdev, &peer_req->i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002218 }
2219
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002220 err = drbd_submit_peer_request(mdev, peer_req, rw, DRBD_FAULT_DT_WR);
2221 if (!err)
2222 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002223
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01002224 /* don't care for the reason here */
2225 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002226 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002227 list_del(&peer_req->w.list);
2228 drbd_remove_epoch_entry_interval(mdev, peer_req);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002229 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002230 if (peer_req->flags & EE_CALL_AL_COMPLETE_IO)
Lars Ellenberg181286a2011-03-31 15:18:56 +02002231 drbd_al_complete_io(mdev, &peer_req->i);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002232
Philipp Reisnerb411b362009-09-25 16:07:19 -07002233out_interrupted:
Philipp Reisner1e9dd292011-11-10 15:14:53 +01002234 drbd_may_finish_epoch(tconn, peer_req->epoch, EV_PUT + EV_CLEANUP);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002235 put_ldev(mdev);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02002236 drbd_free_peer_req(mdev, peer_req);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002237 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002238}
2239
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002240/* We may throttle resync, if the lower device seems to be busy,
2241 * and current sync rate is above c_min_rate.
2242 *
2243 * To decide whether or not the lower device is busy, we use a scheme similar
2244 * to MD RAID is_mddev_idle(): if the partition stats reveal "significant"
2245 * (more than 64 sectors) of activity we cannot account for with our own resync
2246 * activity, it obviously is "busy".
2247 *
2248 * The current sync rate used here uses only the most recent two step marks,
2249 * to have a short time average so we can react faster.
2250 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002251int drbd_rs_should_slow_down(struct drbd_conf *mdev, sector_t sector)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002252{
2253 struct gendisk *disk = mdev->ldev->backing_bdev->bd_contains->bd_disk;
2254 unsigned long db, dt, dbdt;
Philipp Reisnere3555d82010-11-07 15:56:29 +01002255 struct lc_element *tmp;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002256 int curr_events;
2257 int throttle = 0;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002258 unsigned int c_min_rate;
2259
2260 rcu_read_lock();
2261 c_min_rate = rcu_dereference(mdev->ldev->disk_conf)->c_min_rate;
2262 rcu_read_unlock();
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002263
2264 /* feature disabled? */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002265 if (c_min_rate == 0)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002266 return 0;
2267
Philipp Reisnere3555d82010-11-07 15:56:29 +01002268 spin_lock_irq(&mdev->al_lock);
2269 tmp = lc_find(mdev->resync, BM_SECT_TO_EXT(sector));
2270 if (tmp) {
2271 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
2272 if (test_bit(BME_PRIORITY, &bm_ext->flags)) {
2273 spin_unlock_irq(&mdev->al_lock);
2274 return 0;
2275 }
2276 /* Do not slow down if app IO is already waiting for this extent */
2277 }
2278 spin_unlock_irq(&mdev->al_lock);
2279
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002280 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
2281 (int)part_stat_read(&disk->part0, sectors[1]) -
2282 atomic_read(&mdev->rs_sect_ev);
Philipp Reisnere3555d82010-11-07 15:56:29 +01002283
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002284 if (!mdev->rs_last_events || curr_events - mdev->rs_last_events > 64) {
2285 unsigned long rs_left;
2286 int i;
2287
2288 mdev->rs_last_events = curr_events;
2289
2290 /* sync speed average over the last 2*DRBD_SYNC_MARK_STEP,
2291 * approx. */
Lars Ellenberg2649f082010-11-05 10:05:47 +01002292 i = (mdev->rs_last_mark + DRBD_SYNC_MARKS-1) % DRBD_SYNC_MARKS;
2293
2294 if (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T)
2295 rs_left = mdev->ov_left;
2296 else
2297 rs_left = drbd_bm_total_weight(mdev) - mdev->rs_failed;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002298
2299 dt = ((long)jiffies - (long)mdev->rs_mark_time[i]) / HZ;
2300 if (!dt)
2301 dt++;
2302 db = mdev->rs_mark_left[i] - rs_left;
2303 dbdt = Bit2KB(db/dt);
2304
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002305 if (dbdt > c_min_rate)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002306 throttle = 1;
2307 }
2308 return throttle;
2309}
2310
2311
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002312static int receive_DataRequest(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002313{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002314 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002315 sector_t sector;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002316 sector_t capacity;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002317 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002318 struct digest_info *di = NULL;
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002319 int size, verb;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002320 unsigned int fault_type;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02002321 struct p_block_req *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002322
2323 mdev = vnr_to_mdev(tconn, pi->vnr);
2324 if (!mdev)
2325 return -EIO;
2326 capacity = drbd_get_capacity(mdev->this_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002327
2328 sector = be64_to_cpu(p->sector);
2329 size = be32_to_cpu(p->blksize);
2330
Andreas Gruenbacherc670a392011-02-21 12:41:39 +01002331 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002332 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2333 (unsigned long long)sector, size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002334 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002335 }
2336 if (sector + (size>>9) > capacity) {
2337 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2338 (unsigned long long)sector, size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002339 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002340 }
2341
2342 if (!get_ldev_if_state(mdev, D_UP_TO_DATE)) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002343 verb = 1;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002344 switch (pi->cmd) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002345 case P_DATA_REQUEST:
2346 drbd_send_ack_rp(mdev, P_NEG_DREPLY, p);
2347 break;
2348 case P_RS_DATA_REQUEST:
2349 case P_CSUM_RS_REQUEST:
2350 case P_OV_REQUEST:
2351 drbd_send_ack_rp(mdev, P_NEG_RS_DREPLY , p);
2352 break;
2353 case P_OV_REPLY:
2354 verb = 0;
2355 dec_rs_pending(mdev);
2356 drbd_send_ack_ex(mdev, P_OV_RESULT, sector, size, ID_IN_SYNC);
2357 break;
2358 default:
Andreas Gruenbacher49ba9b12011-03-25 00:35:45 +01002359 BUG();
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002360 }
2361 if (verb && __ratelimit(&drbd_ratelimit_state))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002362 dev_err(DEV, "Can not satisfy peer's read request, "
2363 "no local data.\n");
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002364
Lars Ellenberga821cc42010-09-06 12:31:37 +02002365 /* drain possibly payload */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002366 return drbd_drain_block(mdev, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002367 }
2368
2369 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
2370 * "criss-cross" setup, that might cause write-out on some other DRBD,
2371 * which in turn might block on the other node at this very place. */
Andreas Gruenbacher0db55362011-04-06 16:09:15 +02002372 peer_req = drbd_alloc_peer_req(mdev, p->block_id, sector, size, GFP_NOIO);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002373 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002374 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002375 return -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002376 }
2377
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002378 switch (pi->cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002379 case P_DATA_REQUEST:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002380 peer_req->w.cb = w_e_end_data_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002381 fault_type = DRBD_FAULT_DT_RD;
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002382 /* application IO, don't drbd_rs_begin_io */
2383 goto submit;
2384
Philipp Reisnerb411b362009-09-25 16:07:19 -07002385 case P_RS_DATA_REQUEST:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002386 peer_req->w.cb = w_e_end_rsdata_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002387 fault_type = DRBD_FAULT_RS_RD;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002388 /* used in the sector offset progress display */
2389 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002390 break;
2391
2392 case P_OV_REPLY:
2393 case P_CSUM_RS_REQUEST:
2394 fault_type = DRBD_FAULT_RS_RD;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002395 di = kmalloc(sizeof(*di) + pi->size, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002396 if (!di)
2397 goto out_free_e;
2398
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002399 di->digest_size = pi->size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002400 di->digest = (((char *)di)+sizeof(struct digest_info));
2401
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002402 peer_req->digest = di;
2403 peer_req->flags |= EE_HAS_DIGEST;
Lars Ellenbergc36c3ce2010-08-11 20:42:55 +02002404
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002405 if (drbd_recv_all(mdev->tconn, di->digest, pi->size))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002406 goto out_free_e;
2407
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002408 if (pi->cmd == P_CSUM_RS_REQUEST) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002409 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002410 peer_req->w.cb = w_e_end_csum_rs_req;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002411 /* used in the sector offset progress display */
2412 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002413 } else if (pi->cmd == P_OV_REPLY) {
Lars Ellenberg2649f082010-11-05 10:05:47 +01002414 /* track progress, we may need to throttle */
2415 atomic_add(size >> 9, &mdev->rs_sect_in);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002416 peer_req->w.cb = w_e_end_ov_reply;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002417 dec_rs_pending(mdev);
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002418 /* drbd_rs_begin_io done when we sent this request,
2419 * but accounting still needs to be done. */
2420 goto submit_for_resync;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002421 }
2422 break;
2423
2424 case P_OV_REQUEST:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002425 if (mdev->ov_start_sector == ~(sector_t)0 &&
Philipp Reisner31890f42011-01-19 14:12:51 +01002426 mdev->tconn->agreed_pro_version >= 90) {
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002427 unsigned long now = jiffies;
2428 int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002429 mdev->ov_start_sector = sector;
2430 mdev->ov_position = sector;
Lars Ellenberg30b743a2010-11-05 09:39:06 +01002431 mdev->ov_left = drbd_bm_bits(mdev) - BM_SECT_TO_BIT(sector);
2432 mdev->rs_total = mdev->ov_left;
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002433 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
2434 mdev->rs_mark_left[i] = mdev->ov_left;
2435 mdev->rs_mark_time[i] = now;
2436 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002437 dev_info(DEV, "Online Verify start sector: %llu\n",
2438 (unsigned long long)sector);
2439 }
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002440 peer_req->w.cb = w_e_end_ov_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002441 fault_type = DRBD_FAULT_RS_RD;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002442 break;
2443
Philipp Reisnerb411b362009-09-25 16:07:19 -07002444 default:
Andreas Gruenbacher49ba9b12011-03-25 00:35:45 +01002445 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002446 }
2447
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002448 /* Throttle, drbd_rs_begin_io and submit should become asynchronous
2449 * wrt the receiver, but it is not as straightforward as it may seem.
2450 * Various places in the resync start and stop logic assume resync
2451 * requests are processed in order, requeuing this on the worker thread
2452 * introduces a bunch of new code for synchronization between threads.
2453 *
2454 * Unlimited throttling before drbd_rs_begin_io may stall the resync
2455 * "forever", throttling after drbd_rs_begin_io will lock that extent
2456 * for application writes for the same time. For now, just throttle
2457 * here, where the rest of the code expects the receiver to sleep for
2458 * a while, anyways.
2459 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002460
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002461 /* Throttle before drbd_rs_begin_io, as that locks out application IO;
2462 * this defers syncer requests for some time, before letting at least
2463 * on request through. The resync controller on the receiving side
2464 * will adapt to the incoming rate accordingly.
2465 *
2466 * We cannot throttle here if remote is Primary/SyncTarget:
2467 * we would also throttle its application reads.
2468 * In that case, throttling is done on the SyncTarget only.
2469 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002470 if (mdev->state.peer != R_PRIMARY && drbd_rs_should_slow_down(mdev, sector))
2471 schedule_timeout_uninterruptible(HZ/10);
2472 if (drbd_rs_begin_io(mdev, sector))
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002473 goto out_free_e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002474
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002475submit_for_resync:
2476 atomic_add(size >> 9, &mdev->rs_sect_ev);
2477
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002478submit:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002479 inc_unacked(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002480 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002481 list_add_tail(&peer_req->w.list, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002482 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002483
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01002484 if (drbd_submit_peer_request(mdev, peer_req, READ, fault_type) == 0)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002485 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002486
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01002487 /* don't care for the reason here */
2488 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002489 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002490 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002491 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002492 /* no drbd_rs_complete_io(), we are dropping the connection anyways */
2493
Philipp Reisnerb411b362009-09-25 16:07:19 -07002494out_free_e:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002495 put_ldev(mdev);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02002496 drbd_free_peer_req(mdev, peer_req);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002497 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002498}
2499
2500static int drbd_asb_recover_0p(struct drbd_conf *mdev) __must_hold(local)
2501{
2502 int self, peer, rv = -100;
2503 unsigned long ch_self, ch_peer;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002504 enum drbd_after_sb_p after_sb_0p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002505
2506 self = mdev->ldev->md.uuid[UI_BITMAP] & 1;
2507 peer = mdev->p_uuid[UI_BITMAP] & 1;
2508
2509 ch_peer = mdev->p_uuid[UI_SIZE];
2510 ch_self = mdev->comm_bm_set;
2511
Philipp Reisner44ed1672011-04-19 17:10:19 +02002512 rcu_read_lock();
2513 after_sb_0p = rcu_dereference(mdev->tconn->net_conf)->after_sb_0p;
2514 rcu_read_unlock();
2515 switch (after_sb_0p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002516 case ASB_CONSENSUS:
2517 case ASB_DISCARD_SECONDARY:
2518 case ASB_CALL_HELPER:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002519 case ASB_VIOLENTLY:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002520 dev_err(DEV, "Configuration error.\n");
2521 break;
2522 case ASB_DISCONNECT:
2523 break;
2524 case ASB_DISCARD_YOUNGER_PRI:
2525 if (self == 0 && peer == 1) {
2526 rv = -1;
2527 break;
2528 }
2529 if (self == 1 && peer == 0) {
2530 rv = 1;
2531 break;
2532 }
2533 /* Else fall through to one of the other strategies... */
2534 case ASB_DISCARD_OLDER_PRI:
2535 if (self == 0 && peer == 1) {
2536 rv = 1;
2537 break;
2538 }
2539 if (self == 1 && peer == 0) {
2540 rv = -1;
2541 break;
2542 }
2543 /* Else fall through to one of the other strategies... */
Lars Ellenbergad19bf62009-10-14 09:36:49 +02002544 dev_warn(DEV, "Discard younger/older primary did not find a decision\n"
Philipp Reisnerb411b362009-09-25 16:07:19 -07002545 "Using discard-least-changes instead\n");
2546 case ASB_DISCARD_ZERO_CHG:
2547 if (ch_peer == 0 && ch_self == 0) {
Philipp Reisner25703f82011-02-07 14:35:25 +01002548 rv = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002549 ? -1 : 1;
2550 break;
2551 } else {
2552 if (ch_peer == 0) { rv = 1; break; }
2553 if (ch_self == 0) { rv = -1; break; }
2554 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002555 if (after_sb_0p == ASB_DISCARD_ZERO_CHG)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002556 break;
2557 case ASB_DISCARD_LEAST_CHG:
2558 if (ch_self < ch_peer)
2559 rv = -1;
2560 else if (ch_self > ch_peer)
2561 rv = 1;
2562 else /* ( ch_self == ch_peer ) */
2563 /* Well, then use something else. */
Philipp Reisner25703f82011-02-07 14:35:25 +01002564 rv = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002565 ? -1 : 1;
2566 break;
2567 case ASB_DISCARD_LOCAL:
2568 rv = -1;
2569 break;
2570 case ASB_DISCARD_REMOTE:
2571 rv = 1;
2572 }
2573
2574 return rv;
2575}
2576
2577static int drbd_asb_recover_1p(struct drbd_conf *mdev) __must_hold(local)
2578{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002579 int hg, rv = -100;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002580 enum drbd_after_sb_p after_sb_1p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002581
Philipp Reisner44ed1672011-04-19 17:10:19 +02002582 rcu_read_lock();
2583 after_sb_1p = rcu_dereference(mdev->tconn->net_conf)->after_sb_1p;
2584 rcu_read_unlock();
2585 switch (after_sb_1p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002586 case ASB_DISCARD_YOUNGER_PRI:
2587 case ASB_DISCARD_OLDER_PRI:
2588 case ASB_DISCARD_LEAST_CHG:
2589 case ASB_DISCARD_LOCAL:
2590 case ASB_DISCARD_REMOTE:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002591 case ASB_DISCARD_ZERO_CHG:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002592 dev_err(DEV, "Configuration error.\n");
2593 break;
2594 case ASB_DISCONNECT:
2595 break;
2596 case ASB_CONSENSUS:
2597 hg = drbd_asb_recover_0p(mdev);
2598 if (hg == -1 && mdev->state.role == R_SECONDARY)
2599 rv = hg;
2600 if (hg == 1 && mdev->state.role == R_PRIMARY)
2601 rv = hg;
2602 break;
2603 case ASB_VIOLENTLY:
2604 rv = drbd_asb_recover_0p(mdev);
2605 break;
2606 case ASB_DISCARD_SECONDARY:
2607 return mdev->state.role == R_PRIMARY ? 1 : -1;
2608 case ASB_CALL_HELPER:
2609 hg = drbd_asb_recover_0p(mdev);
2610 if (hg == -1 && mdev->state.role == R_PRIMARY) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002611 enum drbd_state_rv rv2;
2612
2613 drbd_set_role(mdev, R_SECONDARY, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002614 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2615 * we might be here in C_WF_REPORT_PARAMS which is transient.
2616 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002617 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2618 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002619 drbd_khelper(mdev, "pri-lost-after-sb");
2620 } else {
2621 dev_warn(DEV, "Successfully gave up primary role.\n");
2622 rv = hg;
2623 }
2624 } else
2625 rv = hg;
2626 }
2627
2628 return rv;
2629}
2630
2631static int drbd_asb_recover_2p(struct drbd_conf *mdev) __must_hold(local)
2632{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002633 int hg, rv = -100;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002634 enum drbd_after_sb_p after_sb_2p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002635
Philipp Reisner44ed1672011-04-19 17:10:19 +02002636 rcu_read_lock();
2637 after_sb_2p = rcu_dereference(mdev->tconn->net_conf)->after_sb_2p;
2638 rcu_read_unlock();
2639 switch (after_sb_2p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002640 case ASB_DISCARD_YOUNGER_PRI:
2641 case ASB_DISCARD_OLDER_PRI:
2642 case ASB_DISCARD_LEAST_CHG:
2643 case ASB_DISCARD_LOCAL:
2644 case ASB_DISCARD_REMOTE:
2645 case ASB_CONSENSUS:
2646 case ASB_DISCARD_SECONDARY:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002647 case ASB_DISCARD_ZERO_CHG:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002648 dev_err(DEV, "Configuration error.\n");
2649 break;
2650 case ASB_VIOLENTLY:
2651 rv = drbd_asb_recover_0p(mdev);
2652 break;
2653 case ASB_DISCONNECT:
2654 break;
2655 case ASB_CALL_HELPER:
2656 hg = drbd_asb_recover_0p(mdev);
2657 if (hg == -1) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002658 enum drbd_state_rv rv2;
2659
Philipp Reisnerb411b362009-09-25 16:07:19 -07002660 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2661 * we might be here in C_WF_REPORT_PARAMS which is transient.
2662 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002663 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2664 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002665 drbd_khelper(mdev, "pri-lost-after-sb");
2666 } else {
2667 dev_warn(DEV, "Successfully gave up primary role.\n");
2668 rv = hg;
2669 }
2670 } else
2671 rv = hg;
2672 }
2673
2674 return rv;
2675}
2676
2677static void drbd_uuid_dump(struct drbd_conf *mdev, char *text, u64 *uuid,
2678 u64 bits, u64 flags)
2679{
2680 if (!uuid) {
2681 dev_info(DEV, "%s uuid info vanished while I was looking!\n", text);
2682 return;
2683 }
2684 dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX bits:%llu flags:%llX\n",
2685 text,
2686 (unsigned long long)uuid[UI_CURRENT],
2687 (unsigned long long)uuid[UI_BITMAP],
2688 (unsigned long long)uuid[UI_HISTORY_START],
2689 (unsigned long long)uuid[UI_HISTORY_END],
2690 (unsigned long long)bits,
2691 (unsigned long long)flags);
2692}
2693
2694/*
2695 100 after split brain try auto recover
2696 2 C_SYNC_SOURCE set BitMap
2697 1 C_SYNC_SOURCE use BitMap
2698 0 no Sync
2699 -1 C_SYNC_TARGET use BitMap
2700 -2 C_SYNC_TARGET set BitMap
2701 -100 after split brain, disconnect
2702-1000 unrelated data
Philipp Reisner4a23f262011-01-11 17:42:17 +01002703-1091 requires proto 91
2704-1096 requires proto 96
Philipp Reisnerb411b362009-09-25 16:07:19 -07002705 */
2706static int drbd_uuid_compare(struct drbd_conf *mdev, int *rule_nr) __must_hold(local)
2707{
2708 u64 self, peer;
2709 int i, j;
2710
2711 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2712 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2713
2714 *rule_nr = 10;
2715 if (self == UUID_JUST_CREATED && peer == UUID_JUST_CREATED)
2716 return 0;
2717
2718 *rule_nr = 20;
2719 if ((self == UUID_JUST_CREATED || self == (u64)0) &&
2720 peer != UUID_JUST_CREATED)
2721 return -2;
2722
2723 *rule_nr = 30;
2724 if (self != UUID_JUST_CREATED &&
2725 (peer == UUID_JUST_CREATED || peer == (u64)0))
2726 return 2;
2727
2728 if (self == peer) {
2729 int rct, dc; /* roles at crash time */
2730
2731 if (mdev->p_uuid[UI_BITMAP] == (u64)0 && mdev->ldev->md.uuid[UI_BITMAP] != (u64)0) {
2732
Philipp Reisner31890f42011-01-19 14:12:51 +01002733 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002734 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002735
2736 if ((mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) &&
2737 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1))) {
2738 dev_info(DEV, "was SyncSource, missed the resync finished event, corrected myself:\n");
2739 drbd_uuid_set_bm(mdev, 0UL);
2740
2741 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2742 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2743 *rule_nr = 34;
2744 } else {
2745 dev_info(DEV, "was SyncSource (peer failed to write sync_uuid)\n");
2746 *rule_nr = 36;
2747 }
2748
2749 return 1;
2750 }
2751
2752 if (mdev->ldev->md.uuid[UI_BITMAP] == (u64)0 && mdev->p_uuid[UI_BITMAP] != (u64)0) {
2753
Philipp Reisner31890f42011-01-19 14:12:51 +01002754 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002755 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002756
2757 if ((mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_BITMAP] & ~((u64)1)) &&
2758 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1))) {
2759 dev_info(DEV, "was SyncTarget, peer missed the resync finished event, corrected peer:\n");
2760
2761 mdev->p_uuid[UI_HISTORY_START + 1] = mdev->p_uuid[UI_HISTORY_START];
2762 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_BITMAP];
2763 mdev->p_uuid[UI_BITMAP] = 0UL;
2764
2765 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2766 *rule_nr = 35;
2767 } else {
2768 dev_info(DEV, "was SyncTarget (failed to write sync_uuid)\n");
2769 *rule_nr = 37;
2770 }
2771
2772 return -1;
2773 }
2774
2775 /* Common power [off|failure] */
2776 rct = (test_bit(CRASHED_PRIMARY, &mdev->flags) ? 1 : 0) +
2777 (mdev->p_uuid[UI_FLAGS] & 2);
2778 /* lowest bit is set when we were primary,
2779 * next bit (weight 2) is set when peer was primary */
2780 *rule_nr = 40;
2781
2782 switch (rct) {
2783 case 0: /* !self_pri && !peer_pri */ return 0;
2784 case 1: /* self_pri && !peer_pri */ return 1;
2785 case 2: /* !self_pri && peer_pri */ return -1;
2786 case 3: /* self_pri && peer_pri */
Philipp Reisner25703f82011-02-07 14:35:25 +01002787 dc = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002788 return dc ? -1 : 1;
2789 }
2790 }
2791
2792 *rule_nr = 50;
2793 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2794 if (self == peer)
2795 return -1;
2796
2797 *rule_nr = 51;
2798 peer = mdev->p_uuid[UI_HISTORY_START] & ~((u64)1);
2799 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002800 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002801 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) ==
2802 (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1)) :
2803 peer + UUID_NEW_BM_OFFSET == (mdev->p_uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002804 /* The last P_SYNC_UUID did not get though. Undo the last start of
2805 resync as sync source modifications of the peer's UUIDs. */
2806
Philipp Reisner31890f42011-01-19 14:12:51 +01002807 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002808 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002809
2810 mdev->p_uuid[UI_BITMAP] = mdev->p_uuid[UI_HISTORY_START];
2811 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_HISTORY_START + 1];
Philipp Reisner4a23f262011-01-11 17:42:17 +01002812
2813 dev_info(DEV, "Did not got last syncUUID packet, corrected:\n");
2814 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2815
Philipp Reisnerb411b362009-09-25 16:07:19 -07002816 return -1;
2817 }
2818 }
2819
2820 *rule_nr = 60;
2821 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2822 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2823 peer = mdev->p_uuid[i] & ~((u64)1);
2824 if (self == peer)
2825 return -2;
2826 }
2827
2828 *rule_nr = 70;
2829 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2830 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2831 if (self == peer)
2832 return 1;
2833
2834 *rule_nr = 71;
2835 self = mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1);
2836 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002837 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002838 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) ==
2839 (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) :
2840 self + UUID_NEW_BM_OFFSET == (mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002841 /* The last P_SYNC_UUID did not get though. Undo the last start of
2842 resync as sync source modifications of our UUIDs. */
2843
Philipp Reisner31890f42011-01-19 14:12:51 +01002844 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002845 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002846
2847 _drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_HISTORY_START]);
2848 _drbd_uuid_set(mdev, UI_HISTORY_START, mdev->ldev->md.uuid[UI_HISTORY_START + 1]);
2849
Philipp Reisner4a23f262011-01-11 17:42:17 +01002850 dev_info(DEV, "Last syncUUID did not get through, corrected:\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002851 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2852 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2853
2854 return 1;
2855 }
2856 }
2857
2858
2859 *rule_nr = 80;
Philipp Reisnerd8c2a362009-11-18 15:52:51 +01002860 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002861 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2862 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2863 if (self == peer)
2864 return 2;
2865 }
2866
2867 *rule_nr = 90;
2868 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2869 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2870 if (self == peer && self != ((u64)0))
2871 return 100;
2872
2873 *rule_nr = 100;
2874 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2875 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2876 for (j = UI_HISTORY_START; j <= UI_HISTORY_END; j++) {
2877 peer = mdev->p_uuid[j] & ~((u64)1);
2878 if (self == peer)
2879 return -100;
2880 }
2881 }
2882
2883 return -1000;
2884}
2885
2886/* drbd_sync_handshake() returns the new conn state on success, or
2887 CONN_MASK (-1) on failure.
2888 */
2889static enum drbd_conns drbd_sync_handshake(struct drbd_conf *mdev, enum drbd_role peer_role,
2890 enum drbd_disk_state peer_disk) __must_hold(local)
2891{
Philipp Reisnerb411b362009-09-25 16:07:19 -07002892 enum drbd_conns rv = C_MASK;
2893 enum drbd_disk_state mydisk;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002894 struct net_conf *nc;
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02002895 int hg, rule_nr, rr_conflict, tentative;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002896
2897 mydisk = mdev->state.disk;
2898 if (mydisk == D_NEGOTIATING)
2899 mydisk = mdev->new_state_tmp.disk;
2900
2901 dev_info(DEV, "drbd_sync_handshake:\n");
2902 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid, mdev->comm_bm_set, 0);
2903 drbd_uuid_dump(mdev, "peer", mdev->p_uuid,
2904 mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2905
2906 hg = drbd_uuid_compare(mdev, &rule_nr);
2907
2908 dev_info(DEV, "uuid_compare()=%d by rule %d\n", hg, rule_nr);
2909
2910 if (hg == -1000) {
2911 dev_alert(DEV, "Unrelated data, aborting!\n");
2912 return C_MASK;
2913 }
Philipp Reisner4a23f262011-01-11 17:42:17 +01002914 if (hg < -1000) {
2915 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 -07002916 return C_MASK;
2917 }
2918
2919 if ((mydisk == D_INCONSISTENT && peer_disk > D_INCONSISTENT) ||
2920 (peer_disk == D_INCONSISTENT && mydisk > D_INCONSISTENT)) {
2921 int f = (hg == -100) || abs(hg) == 2;
2922 hg = mydisk > D_INCONSISTENT ? 1 : -1;
2923 if (f)
2924 hg = hg*2;
2925 dev_info(DEV, "Becoming sync %s due to disk states.\n",
2926 hg > 0 ? "source" : "target");
2927 }
2928
Adam Gandelman3a11a482010-04-08 16:48:23 -07002929 if (abs(hg) == 100)
2930 drbd_khelper(mdev, "initial-split-brain");
2931
Philipp Reisner44ed1672011-04-19 17:10:19 +02002932 rcu_read_lock();
2933 nc = rcu_dereference(mdev->tconn->net_conf);
2934
2935 if (hg == 100 || (hg == -100 && nc->always_asbp)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002936 int pcount = (mdev->state.role == R_PRIMARY)
2937 + (peer_role == R_PRIMARY);
2938 int forced = (hg == -100);
2939
2940 switch (pcount) {
2941 case 0:
2942 hg = drbd_asb_recover_0p(mdev);
2943 break;
2944 case 1:
2945 hg = drbd_asb_recover_1p(mdev);
2946 break;
2947 case 2:
2948 hg = drbd_asb_recover_2p(mdev);
2949 break;
2950 }
2951 if (abs(hg) < 100) {
2952 dev_warn(DEV, "Split-Brain detected, %d primaries, "
2953 "automatically solved. Sync from %s node\n",
2954 pcount, (hg < 0) ? "peer" : "this");
2955 if (forced) {
2956 dev_warn(DEV, "Doing a full sync, since"
2957 " UUIDs where ambiguous.\n");
2958 hg = hg*2;
2959 }
2960 }
2961 }
2962
2963 if (hg == -100) {
Philipp Reisner08b165b2011-09-05 16:22:33 +02002964 if (test_bit(DISCARD_MY_DATA, &mdev->flags) && !(mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002965 hg = -1;
Philipp Reisner08b165b2011-09-05 16:22:33 +02002966 if (!test_bit(DISCARD_MY_DATA, &mdev->flags) && (mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002967 hg = 1;
2968
2969 if (abs(hg) < 100)
2970 dev_warn(DEV, "Split-Brain detected, manually solved. "
2971 "Sync from %s node\n",
2972 (hg < 0) ? "peer" : "this");
2973 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002974 rr_conflict = nc->rr_conflict;
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02002975 tentative = nc->tentative;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002976 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002977
2978 if (hg == -100) {
Lars Ellenberg580b9762010-02-26 23:15:23 +01002979 /* FIXME this log message is not correct if we end up here
2980 * after an attempted attach on a diskless node.
2981 * We just refuse to attach -- well, we drop the "connection"
2982 * to that disk, in a way... */
Adam Gandelman3a11a482010-04-08 16:48:23 -07002983 dev_alert(DEV, "Split-Brain detected but unresolved, dropping connection!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002984 drbd_khelper(mdev, "split-brain");
2985 return C_MASK;
2986 }
2987
2988 if (hg > 0 && mydisk <= D_INCONSISTENT) {
2989 dev_err(DEV, "I shall become SyncSource, but I am inconsistent!\n");
2990 return C_MASK;
2991 }
2992
2993 if (hg < 0 && /* by intention we do not use mydisk here. */
2994 mdev->state.role == R_PRIMARY && mdev->state.disk >= D_CONSISTENT) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02002995 switch (rr_conflict) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002996 case ASB_CALL_HELPER:
2997 drbd_khelper(mdev, "pri-lost");
2998 /* fall through */
2999 case ASB_DISCONNECT:
3000 dev_err(DEV, "I shall become SyncTarget, but I am primary!\n");
3001 return C_MASK;
3002 case ASB_VIOLENTLY:
3003 dev_warn(DEV, "Becoming SyncTarget, violating the stable-data"
3004 "assumption\n");
3005 }
3006 }
3007
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02003008 if (tentative || test_bit(CONN_DRY_RUN, &mdev->tconn->flags)) {
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003009 if (hg == 0)
3010 dev_info(DEV, "dry-run connect: No resync, would become Connected immediately.\n");
3011 else
3012 dev_info(DEV, "dry-run connect: Would become %s, doing a %s resync.",
3013 drbd_conn_str(hg > 0 ? C_SYNC_SOURCE : C_SYNC_TARGET),
3014 abs(hg) >= 2 ? "full" : "bit-map based");
3015 return C_MASK;
3016 }
3017
Philipp Reisnerb411b362009-09-25 16:07:19 -07003018 if (abs(hg) >= 2) {
3019 dev_info(DEV, "Writing the whole bitmap, full sync required after drbd_sync_handshake.\n");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003020 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write, "set_n_write from sync_handshake",
3021 BM_LOCKED_SET_ALLOWED))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003022 return C_MASK;
3023 }
3024
3025 if (hg > 0) { /* become sync source. */
3026 rv = C_WF_BITMAP_S;
3027 } else if (hg < 0) { /* become sync target */
3028 rv = C_WF_BITMAP_T;
3029 } else {
3030 rv = C_CONNECTED;
3031 if (drbd_bm_total_weight(mdev)) {
3032 dev_info(DEV, "No resync, but %lu bits in bitmap!\n",
3033 drbd_bm_total_weight(mdev));
3034 }
3035 }
3036
3037 return rv;
3038}
3039
Philipp Reisnerf179d762011-05-16 17:31:47 +02003040static enum drbd_after_sb_p convert_after_sb(enum drbd_after_sb_p peer)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003041{
3042 /* ASB_DISCARD_REMOTE - ASB_DISCARD_LOCAL is valid */
Philipp Reisnerf179d762011-05-16 17:31:47 +02003043 if (peer == ASB_DISCARD_REMOTE)
3044 return ASB_DISCARD_LOCAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003045
3046 /* any other things with ASB_DISCARD_REMOTE or ASB_DISCARD_LOCAL are invalid */
Philipp Reisnerf179d762011-05-16 17:31:47 +02003047 if (peer == ASB_DISCARD_LOCAL)
3048 return ASB_DISCARD_REMOTE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003049
3050 /* everything else is valid if they are equal on both sides. */
Philipp Reisnerf179d762011-05-16 17:31:47 +02003051 return peer;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003052}
3053
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003054static int receive_protocol(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003055{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003056 struct p_protocol *p = pi->data;
Philipp Reisner036b17e2011-05-16 17:38:11 +02003057 enum drbd_after_sb_p p_after_sb_0p, p_after_sb_1p, p_after_sb_2p;
3058 int p_proto, p_discard_my_data, p_two_primaries, cf;
3059 struct net_conf *nc, *old_net_conf, *new_net_conf = NULL;
3060 char integrity_alg[SHARED_SECRET_MAX] = "";
Andreas Gruenbacheraccdbcc2011-07-15 17:41:09 +02003061 struct crypto_hash *peer_integrity_tfm = NULL;
Philipp Reisner7aca6c72011-05-17 10:12:56 +02003062 void *int_dig_in = NULL, *int_dig_vv = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003063
Philipp Reisnerb411b362009-09-25 16:07:19 -07003064 p_proto = be32_to_cpu(p->protocol);
3065 p_after_sb_0p = be32_to_cpu(p->after_sb_0p);
3066 p_after_sb_1p = be32_to_cpu(p->after_sb_1p);
3067 p_after_sb_2p = be32_to_cpu(p->after_sb_2p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003068 p_two_primaries = be32_to_cpu(p->two_primaries);
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003069 cf = be32_to_cpu(p->conn_flags);
Andreas Gruenbacher6139f602011-05-06 20:00:02 +02003070 p_discard_my_data = cf & CF_DISCARD_MY_DATA;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003071
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003072 if (tconn->agreed_pro_version >= 87) {
3073 int err;
3074
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02003075 if (pi->size > sizeof(integrity_alg))
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003076 return -EIO;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02003077 err = drbd_recv_all(tconn, integrity_alg, pi->size);
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003078 if (err)
3079 return err;
Philipp Reisner036b17e2011-05-16 17:38:11 +02003080 integrity_alg[SHARED_SECRET_MAX - 1] = 0;
3081 }
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003082
Andreas Gruenbacher7d4c7822011-07-17 23:06:12 +02003083 if (pi->cmd != P_PROTOCOL_UPDATE) {
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003084 clear_bit(CONN_DRY_RUN, &tconn->flags);
Philipp Reisner036b17e2011-05-16 17:38:11 +02003085
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003086 if (cf & CF_DRY_RUN)
3087 set_bit(CONN_DRY_RUN, &tconn->flags);
3088
3089 rcu_read_lock();
3090 nc = rcu_dereference(tconn->net_conf);
3091
3092 if (p_proto != nc->wire_protocol) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003093 conn_err(tconn, "incompatible %s settings\n", "protocol");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003094 goto disconnect_rcu_unlock;
3095 }
3096
3097 if (convert_after_sb(p_after_sb_0p) != nc->after_sb_0p) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003098 conn_err(tconn, "incompatible %s settings\n", "after-sb-0pri");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003099 goto disconnect_rcu_unlock;
3100 }
3101
3102 if (convert_after_sb(p_after_sb_1p) != nc->after_sb_1p) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003103 conn_err(tconn, "incompatible %s settings\n", "after-sb-1pri");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003104 goto disconnect_rcu_unlock;
3105 }
3106
3107 if (convert_after_sb(p_after_sb_2p) != nc->after_sb_2p) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003108 conn_err(tconn, "incompatible %s settings\n", "after-sb-2pri");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003109 goto disconnect_rcu_unlock;
3110 }
3111
3112 if (p_discard_my_data && nc->discard_my_data) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003113 conn_err(tconn, "incompatible %s settings\n", "discard-my-data");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003114 goto disconnect_rcu_unlock;
3115 }
3116
3117 if (p_two_primaries != nc->two_primaries) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003118 conn_err(tconn, "incompatible %s settings\n", "allow-two-primaries");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003119 goto disconnect_rcu_unlock;
3120 }
3121
3122 if (strcmp(integrity_alg, nc->integrity_alg)) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003123 conn_err(tconn, "incompatible %s settings\n", "data-integrity-alg");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003124 goto disconnect_rcu_unlock;
3125 }
3126
3127 rcu_read_unlock();
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003128 }
Andreas Gruenbacher7d4c7822011-07-17 23:06:12 +02003129
3130 if (integrity_alg[0]) {
3131 int hash_size;
3132
3133 /*
3134 * We can only change the peer data integrity algorithm
3135 * here. Changing our own data integrity algorithm
3136 * requires that we send a P_PROTOCOL_UPDATE packet at
3137 * the same time; otherwise, the peer has no way to
3138 * tell between which packets the algorithm should
3139 * change.
3140 */
3141
3142 peer_integrity_tfm = crypto_alloc_hash(integrity_alg, 0, CRYPTO_ALG_ASYNC);
3143 if (!peer_integrity_tfm) {
3144 conn_err(tconn, "peer data-integrity-alg %s not supported\n",
3145 integrity_alg);
3146 goto disconnect;
3147 }
3148
3149 hash_size = crypto_hash_digestsize(peer_integrity_tfm);
3150 int_dig_in = kmalloc(hash_size, GFP_KERNEL);
3151 int_dig_vv = kmalloc(hash_size, GFP_KERNEL);
3152 if (!(int_dig_in && int_dig_vv)) {
3153 conn_err(tconn, "Allocation of buffers for data integrity checking failed\n");
3154 goto disconnect;
3155 }
3156 }
3157
3158 new_net_conf = kmalloc(sizeof(struct net_conf), GFP_KERNEL);
3159 if (!new_net_conf) {
3160 conn_err(tconn, "Allocation of new net_conf failed\n");
3161 goto disconnect;
3162 }
3163
3164 mutex_lock(&tconn->data.mutex);
3165 mutex_lock(&tconn->conf_update);
3166 old_net_conf = tconn->net_conf;
3167 *new_net_conf = *old_net_conf;
3168
3169 new_net_conf->wire_protocol = p_proto;
3170 new_net_conf->after_sb_0p = convert_after_sb(p_after_sb_0p);
3171 new_net_conf->after_sb_1p = convert_after_sb(p_after_sb_1p);
3172 new_net_conf->after_sb_2p = convert_after_sb(p_after_sb_2p);
3173 new_net_conf->two_primaries = p_two_primaries;
3174
3175 rcu_assign_pointer(tconn->net_conf, new_net_conf);
3176 mutex_unlock(&tconn->conf_update);
3177 mutex_unlock(&tconn->data.mutex);
3178
3179 crypto_free_hash(tconn->peer_integrity_tfm);
3180 kfree(tconn->int_dig_in);
3181 kfree(tconn->int_dig_vv);
3182 tconn->peer_integrity_tfm = peer_integrity_tfm;
3183 tconn->int_dig_in = int_dig_in;
3184 tconn->int_dig_vv = int_dig_vv;
3185
3186 if (strcmp(old_net_conf->integrity_alg, integrity_alg))
3187 conn_info(tconn, "peer data-integrity-alg: %s\n",
3188 integrity_alg[0] ? integrity_alg : "(none)");
3189
3190 synchronize_rcu();
3191 kfree(old_net_conf);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003192 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003193
Philipp Reisner44ed1672011-04-19 17:10:19 +02003194disconnect_rcu_unlock:
3195 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07003196disconnect:
Andreas Gruenbacherb792c352011-07-15 16:48:49 +02003197 crypto_free_hash(peer_integrity_tfm);
Philipp Reisner036b17e2011-05-16 17:38:11 +02003198 kfree(int_dig_in);
3199 kfree(int_dig_vv);
Philipp Reisner7204624c2011-03-15 18:51:47 +01003200 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003201 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003202}
3203
3204/* helper function
3205 * input: alg name, feature name
3206 * return: NULL (alg name was "")
3207 * ERR_PTR(error) if something goes wrong
3208 * or the crypto hash ptr, if it worked out ok. */
3209struct crypto_hash *drbd_crypto_alloc_digest_safe(const struct drbd_conf *mdev,
3210 const char *alg, const char *name)
3211{
3212 struct crypto_hash *tfm;
3213
3214 if (!alg[0])
3215 return NULL;
3216
3217 tfm = crypto_alloc_hash(alg, 0, CRYPTO_ALG_ASYNC);
3218 if (IS_ERR(tfm)) {
3219 dev_err(DEV, "Can not allocate \"%s\" as %s (reason: %ld)\n",
3220 alg, name, PTR_ERR(tfm));
3221 return tfm;
3222 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003223 return tfm;
3224}
3225
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003226static int ignore_remaining_packet(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003227{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003228 void *buffer = tconn->data.rbuf;
3229 int size = pi->size;
3230
3231 while (size) {
3232 int s = min_t(int, size, DRBD_SOCKET_BUFFER_SIZE);
3233 s = drbd_recv(tconn, buffer, s);
3234 if (s <= 0) {
3235 if (s < 0)
3236 return s;
3237 break;
3238 }
3239 size -= s;
3240 }
3241 if (size)
3242 return -EIO;
3243 return 0;
3244}
3245
3246/*
3247 * config_unknown_volume - device configuration command for unknown volume
3248 *
3249 * When a device is added to an existing connection, the node on which the
3250 * device is added first will send configuration commands to its peer but the
3251 * peer will not know about the device yet. It will warn and ignore these
3252 * commands. Once the device is added on the second node, the second node will
3253 * send the same device configuration commands, but in the other direction.
3254 *
3255 * (We can also end up here if drbd is misconfigured.)
3256 */
3257static int config_unknown_volume(struct drbd_tconn *tconn, struct packet_info *pi)
3258{
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02003259 conn_warn(tconn, "%s packet received for volume %u, which is not configured locally\n",
3260 cmdname(pi->cmd), pi->vnr);
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003261 return ignore_remaining_packet(tconn, pi);
3262}
3263
3264static int receive_SyncParam(struct drbd_tconn *tconn, struct packet_info *pi)
3265{
3266 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003267 struct p_rs_param_95 *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003268 unsigned int header_size, data_size, exp_max_sz;
3269 struct crypto_hash *verify_tfm = NULL;
3270 struct crypto_hash *csums_tfm = NULL;
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003271 struct net_conf *old_net_conf, *new_net_conf = NULL;
Philipp Reisner813472c2011-05-03 16:47:02 +02003272 struct disk_conf *old_disk_conf = NULL, *new_disk_conf = NULL;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003273 const int apv = tconn->agreed_pro_version;
Philipp Reisner813472c2011-05-03 16:47:02 +02003274 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
Philipp Reisner778f2712010-07-06 11:14:00 +02003275 int fifo_size = 0;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003276 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003277
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003278 mdev = vnr_to_mdev(tconn, pi->vnr);
3279 if (!mdev)
3280 return config_unknown_volume(tconn, pi);
3281
Philipp Reisnerb411b362009-09-25 16:07:19 -07003282 exp_max_sz = apv <= 87 ? sizeof(struct p_rs_param)
3283 : apv == 88 ? sizeof(struct p_rs_param)
3284 + SHARED_SECRET_MAX
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003285 : apv <= 94 ? sizeof(struct p_rs_param_89)
3286 : /* apv >= 95 */ sizeof(struct p_rs_param_95);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003287
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003288 if (pi->size > exp_max_sz) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003289 dev_err(DEV, "SyncParam packet too long: received %u, expected <= %u bytes\n",
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003290 pi->size, exp_max_sz);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003291 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003292 }
3293
3294 if (apv <= 88) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003295 header_size = sizeof(struct p_rs_param);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003296 data_size = pi->size - header_size;
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003297 } else if (apv <= 94) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003298 header_size = sizeof(struct p_rs_param_89);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003299 data_size = pi->size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003300 D_ASSERT(data_size == 0);
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003301 } else {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003302 header_size = sizeof(struct p_rs_param_95);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003303 data_size = pi->size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003304 D_ASSERT(data_size == 0);
3305 }
3306
3307 /* initialize verify_alg and csums_alg */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003308 p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003309 memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
3310
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003311 err = drbd_recv_all(mdev->tconn, p, header_size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003312 if (err)
3313 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003314
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003315 mutex_lock(&mdev->tconn->conf_update);
3316 old_net_conf = mdev->tconn->net_conf;
Philipp Reisner813472c2011-05-03 16:47:02 +02003317 if (get_ldev(mdev)) {
3318 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
3319 if (!new_disk_conf) {
3320 put_ldev(mdev);
3321 mutex_unlock(&mdev->tconn->conf_update);
3322 dev_err(DEV, "Allocation of new disk_conf failed\n");
3323 return -ENOMEM;
3324 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003325
Philipp Reisner813472c2011-05-03 16:47:02 +02003326 old_disk_conf = mdev->ldev->disk_conf;
3327 *new_disk_conf = *old_disk_conf;
3328
Andreas Gruenbacher6394b932011-05-11 14:29:52 +02003329 new_disk_conf->resync_rate = be32_to_cpu(p->resync_rate);
Philipp Reisner813472c2011-05-03 16:47:02 +02003330 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003331
Philipp Reisnerb411b362009-09-25 16:07:19 -07003332 if (apv >= 88) {
3333 if (apv == 88) {
3334 if (data_size > SHARED_SECRET_MAX) {
3335 dev_err(DEV, "verify-alg too long, "
3336 "peer wants %u, accepting only %u byte\n",
3337 data_size, SHARED_SECRET_MAX);
Philipp Reisner813472c2011-05-03 16:47:02 +02003338 err = -EIO;
3339 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003340 }
3341
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003342 err = drbd_recv_all(mdev->tconn, p->verify_alg, data_size);
Philipp Reisner813472c2011-05-03 16:47:02 +02003343 if (err)
3344 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003345 /* we expect NUL terminated string */
3346 /* but just in case someone tries to be evil */
3347 D_ASSERT(p->verify_alg[data_size-1] == 0);
3348 p->verify_alg[data_size-1] = 0;
3349
3350 } else /* apv >= 89 */ {
3351 /* we still expect NUL terminated strings */
3352 /* but just in case someone tries to be evil */
3353 D_ASSERT(p->verify_alg[SHARED_SECRET_MAX-1] == 0);
3354 D_ASSERT(p->csums_alg[SHARED_SECRET_MAX-1] == 0);
3355 p->verify_alg[SHARED_SECRET_MAX-1] = 0;
3356 p->csums_alg[SHARED_SECRET_MAX-1] = 0;
3357 }
3358
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003359 if (strcmp(old_net_conf->verify_alg, p->verify_alg)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003360 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
3361 dev_err(DEV, "Different verify-alg settings. me=\"%s\" peer=\"%s\"\n",
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003362 old_net_conf->verify_alg, p->verify_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003363 goto disconnect;
3364 }
3365 verify_tfm = drbd_crypto_alloc_digest_safe(mdev,
3366 p->verify_alg, "verify-alg");
3367 if (IS_ERR(verify_tfm)) {
3368 verify_tfm = NULL;
3369 goto disconnect;
3370 }
3371 }
3372
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003373 if (apv >= 89 && strcmp(old_net_conf->csums_alg, p->csums_alg)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003374 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
3375 dev_err(DEV, "Different csums-alg settings. me=\"%s\" peer=\"%s\"\n",
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003376 old_net_conf->csums_alg, p->csums_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003377 goto disconnect;
3378 }
3379 csums_tfm = drbd_crypto_alloc_digest_safe(mdev,
3380 p->csums_alg, "csums-alg");
3381 if (IS_ERR(csums_tfm)) {
3382 csums_tfm = NULL;
3383 goto disconnect;
3384 }
3385 }
3386
Philipp Reisner813472c2011-05-03 16:47:02 +02003387 if (apv > 94 && new_disk_conf) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003388 new_disk_conf->c_plan_ahead = be32_to_cpu(p->c_plan_ahead);
3389 new_disk_conf->c_delay_target = be32_to_cpu(p->c_delay_target);
3390 new_disk_conf->c_fill_target = be32_to_cpu(p->c_fill_target);
3391 new_disk_conf->c_max_rate = be32_to_cpu(p->c_max_rate);
Philipp Reisner778f2712010-07-06 11:14:00 +02003392
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003393 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
Philipp Reisner9958c852011-05-03 16:19:31 +02003394 if (fifo_size != mdev->rs_plan_s->size) {
Philipp Reisner813472c2011-05-03 16:47:02 +02003395 new_plan = fifo_alloc(fifo_size);
3396 if (!new_plan) {
Philipp Reisner778f2712010-07-06 11:14:00 +02003397 dev_err(DEV, "kmalloc of fifo_buffer failed");
Lars Ellenbergf3990022011-03-23 14:31:09 +01003398 put_ldev(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02003399 goto disconnect;
3400 }
3401 }
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003402 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003403
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003404 if (verify_tfm || csums_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003405 new_net_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
3406 if (!new_net_conf) {
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003407 dev_err(DEV, "Allocation of new net_conf failed\n");
3408 goto disconnect;
3409 }
3410
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003411 *new_net_conf = *old_net_conf;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003412
3413 if (verify_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003414 strcpy(new_net_conf->verify_alg, p->verify_alg);
3415 new_net_conf->verify_alg_len = strlen(p->verify_alg) + 1;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003416 crypto_free_hash(mdev->tconn->verify_tfm);
3417 mdev->tconn->verify_tfm = verify_tfm;
3418 dev_info(DEV, "using verify-alg: \"%s\"\n", p->verify_alg);
3419 }
3420 if (csums_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003421 strcpy(new_net_conf->csums_alg, p->csums_alg);
3422 new_net_conf->csums_alg_len = strlen(p->csums_alg) + 1;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003423 crypto_free_hash(mdev->tconn->csums_tfm);
3424 mdev->tconn->csums_tfm = csums_tfm;
3425 dev_info(DEV, "using csums-alg: \"%s\"\n", p->csums_alg);
3426 }
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003427 rcu_assign_pointer(tconn->net_conf, new_net_conf);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003428 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003429 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003430
Philipp Reisner813472c2011-05-03 16:47:02 +02003431 if (new_disk_conf) {
3432 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
3433 put_ldev(mdev);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003434 }
Philipp Reisner813472c2011-05-03 16:47:02 +02003435
3436 if (new_plan) {
3437 old_plan = mdev->rs_plan_s;
3438 rcu_assign_pointer(mdev->rs_plan_s, new_plan);
3439 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003440
3441 mutex_unlock(&mdev->tconn->conf_update);
3442 synchronize_rcu();
3443 if (new_net_conf)
3444 kfree(old_net_conf);
3445 kfree(old_disk_conf);
Philipp Reisner813472c2011-05-03 16:47:02 +02003446 kfree(old_plan);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003447
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003448 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003449
Philipp Reisner813472c2011-05-03 16:47:02 +02003450reconnect:
3451 if (new_disk_conf) {
3452 put_ldev(mdev);
3453 kfree(new_disk_conf);
3454 }
3455 mutex_unlock(&mdev->tconn->conf_update);
3456 return -EIO;
3457
Philipp Reisnerb411b362009-09-25 16:07:19 -07003458disconnect:
Philipp Reisner813472c2011-05-03 16:47:02 +02003459 kfree(new_plan);
3460 if (new_disk_conf) {
3461 put_ldev(mdev);
3462 kfree(new_disk_conf);
3463 }
Philipp Reisnera0095502011-05-03 13:14:15 +02003464 mutex_unlock(&mdev->tconn->conf_update);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003465 /* just for completeness: actually not needed,
3466 * as this is not reached if csums_tfm was ok. */
3467 crypto_free_hash(csums_tfm);
3468 /* but free the verify_tfm again, if csums_tfm did not work out */
3469 crypto_free_hash(verify_tfm);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003470 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003471 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003472}
3473
Philipp Reisnerb411b362009-09-25 16:07:19 -07003474/* warn if the arguments differ by more than 12.5% */
3475static void warn_if_differ_considerably(struct drbd_conf *mdev,
3476 const char *s, sector_t a, sector_t b)
3477{
3478 sector_t d;
3479 if (a == 0 || b == 0)
3480 return;
3481 d = (a > b) ? (a - b) : (b - a);
3482 if (d > (a>>3) || d > (b>>3))
3483 dev_warn(DEV, "Considerable difference in %s: %llus vs. %llus\n", s,
3484 (unsigned long long)a, (unsigned long long)b);
3485}
3486
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003487static int receive_sizes(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003488{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003489 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003490 struct p_sizes *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003491 enum determine_dev_size dd = unchanged;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003492 sector_t p_size, p_usize, my_usize;
3493 int ldsc = 0; /* local disk size changed */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003494 enum dds_flags ddsf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003495
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003496 mdev = vnr_to_mdev(tconn, pi->vnr);
3497 if (!mdev)
3498 return config_unknown_volume(tconn, pi);
3499
Philipp Reisnerb411b362009-09-25 16:07:19 -07003500 p_size = be64_to_cpu(p->d_size);
3501 p_usize = be64_to_cpu(p->u_size);
3502
Philipp Reisnerb411b362009-09-25 16:07:19 -07003503 /* just store the peer's disk size for now.
3504 * we still need to figure out whether we accept that. */
3505 mdev->p_size = p_size;
3506
Philipp Reisnerb411b362009-09-25 16:07:19 -07003507 if (get_ldev(mdev)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003508 rcu_read_lock();
3509 my_usize = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
3510 rcu_read_unlock();
3511
Philipp Reisnerb411b362009-09-25 16:07:19 -07003512 warn_if_differ_considerably(mdev, "lower level device sizes",
3513 p_size, drbd_get_max_capacity(mdev->ldev));
3514 warn_if_differ_considerably(mdev, "user requested size",
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003515 p_usize, my_usize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003516
3517 /* if this is the first connect, or an otherwise expected
3518 * param exchange, choose the minimum */
3519 if (mdev->state.conn == C_WF_REPORT_PARAMS)
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003520 p_usize = min_not_zero(my_usize, p_usize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003521
3522 /* Never shrink a device with usable data during connect.
3523 But allow online shrinking if we are connected. */
Philipp Reisneref5e44a2011-05-03 13:27:43 +02003524 if (drbd_new_dev_size(mdev, mdev->ldev, p_usize, 0) <
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003525 drbd_get_capacity(mdev->this_bdev) &&
3526 mdev->state.disk >= D_OUTDATED &&
3527 mdev->state.conn < C_CONNECTED) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003528 dev_err(DEV, "The peer's disk size is too small!\n");
Philipp Reisner38fa9982011-03-15 18:24:49 +01003529 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003530 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003531 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003532 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003533
3534 if (my_usize != p_usize) {
3535 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
3536
3537 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
3538 if (!new_disk_conf) {
3539 dev_err(DEV, "Allocation of new disk_conf failed\n");
3540 put_ldev(mdev);
3541 return -ENOMEM;
3542 }
3543
3544 mutex_lock(&mdev->tconn->conf_update);
3545 old_disk_conf = mdev->ldev->disk_conf;
3546 *new_disk_conf = *old_disk_conf;
3547 new_disk_conf->disk_size = p_usize;
3548
3549 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
3550 mutex_unlock(&mdev->tconn->conf_update);
3551 synchronize_rcu();
3552 kfree(old_disk_conf);
3553
3554 dev_info(DEV, "Peer sets u_size to %lu sectors\n",
3555 (unsigned long)my_usize);
3556 }
3557
Philipp Reisnerb411b362009-09-25 16:07:19 -07003558 put_ldev(mdev);
3559 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003560
Philipp Reisnere89b5912010-03-24 17:11:33 +01003561 ddsf = be16_to_cpu(p->dds_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003562 if (get_ldev(mdev)) {
Bart Van Assche24c48302011-05-21 18:32:29 +02003563 dd = drbd_determine_dev_size(mdev, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003564 put_ldev(mdev);
3565 if (dd == dev_size_error)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003566 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003567 drbd_md_sync(mdev);
3568 } else {
3569 /* I am diskless, need to accept the peer's size. */
3570 drbd_set_my_capacity(mdev, p_size);
3571 }
3572
Philipp Reisner99432fc2011-05-20 16:39:13 +02003573 mdev->peer_max_bio_size = be32_to_cpu(p->max_bio_size);
3574 drbd_reconsider_max_bio_size(mdev);
3575
Philipp Reisnerb411b362009-09-25 16:07:19 -07003576 if (get_ldev(mdev)) {
3577 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev)) {
3578 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
3579 ldsc = 1;
3580 }
3581
Philipp Reisnerb411b362009-09-25 16:07:19 -07003582 put_ldev(mdev);
3583 }
3584
3585 if (mdev->state.conn > C_WF_REPORT_PARAMS) {
3586 if (be64_to_cpu(p->c_size) !=
3587 drbd_get_capacity(mdev->this_bdev) || ldsc) {
3588 /* we have different sizes, probably peer
3589 * needs to know my new size... */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003590 drbd_send_sizes(mdev, 0, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003591 }
3592 if (test_and_clear_bit(RESIZE_PENDING, &mdev->flags) ||
3593 (dd == grew && mdev->state.conn == C_CONNECTED)) {
3594 if (mdev->state.pdsk >= D_INCONSISTENT &&
Philipp Reisnere89b5912010-03-24 17:11:33 +01003595 mdev->state.disk >= D_INCONSISTENT) {
3596 if (ddsf & DDSF_NO_RESYNC)
3597 dev_info(DEV, "Resync of new storage suppressed with --assume-clean\n");
3598 else
3599 resync_after_online_grow(mdev);
3600 } else
Philipp Reisnerb411b362009-09-25 16:07:19 -07003601 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
3602 }
3603 }
3604
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003605 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003606}
3607
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003608static int receive_uuids(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003609{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003610 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003611 struct p_uuids *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003612 u64 *p_uuid;
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003613 int i, updated_uuids = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003614
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003615 mdev = vnr_to_mdev(tconn, pi->vnr);
3616 if (!mdev)
3617 return config_unknown_volume(tconn, pi);
3618
Philipp Reisnerb411b362009-09-25 16:07:19 -07003619 p_uuid = kmalloc(sizeof(u64)*UI_EXTENDED_SIZE, GFP_NOIO);
3620
3621 for (i = UI_CURRENT; i < UI_EXTENDED_SIZE; i++)
3622 p_uuid[i] = be64_to_cpu(p->uuid[i]);
3623
3624 kfree(mdev->p_uuid);
3625 mdev->p_uuid = p_uuid;
3626
3627 if (mdev->state.conn < C_CONNECTED &&
3628 mdev->state.disk < D_INCONSISTENT &&
3629 mdev->state.role == R_PRIMARY &&
3630 (mdev->ed_uuid & ~((u64)1)) != (p_uuid[UI_CURRENT] & ~((u64)1))) {
3631 dev_err(DEV, "Can only connect to data with current UUID=%016llX\n",
3632 (unsigned long long)mdev->ed_uuid);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003633 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003634 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003635 }
3636
3637 if (get_ldev(mdev)) {
3638 int skip_initial_sync =
3639 mdev->state.conn == C_CONNECTED &&
Philipp Reisner31890f42011-01-19 14:12:51 +01003640 mdev->tconn->agreed_pro_version >= 90 &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003641 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED &&
3642 (p_uuid[UI_FLAGS] & 8);
3643 if (skip_initial_sync) {
3644 dev_info(DEV, "Accepted new current UUID, preparing to skip initial sync\n");
3645 drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003646 "clear_n_write from receive_uuids",
3647 BM_LOCKED_TEST_ALLOWED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003648 _drbd_uuid_set(mdev, UI_CURRENT, p_uuid[UI_CURRENT]);
3649 _drbd_uuid_set(mdev, UI_BITMAP, 0);
3650 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
3651 CS_VERBOSE, NULL);
3652 drbd_md_sync(mdev);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003653 updated_uuids = 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003654 }
3655 put_ldev(mdev);
Philipp Reisner18a50fa2010-06-21 14:14:15 +02003656 } else if (mdev->state.disk < D_INCONSISTENT &&
3657 mdev->state.role == R_PRIMARY) {
3658 /* I am a diskless primary, the peer just created a new current UUID
3659 for me. */
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003660 updated_uuids = drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003661 }
3662
3663 /* Before we test for the disk state, we should wait until an eventually
3664 ongoing cluster wide state change is finished. That is important if
3665 we are primary and are detaching from our disk. We need to see the
3666 new disk state... */
Philipp Reisner8410da82011-02-11 20:11:10 +01003667 mutex_lock(mdev->state_mutex);
3668 mutex_unlock(mdev->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003669 if (mdev->state.conn >= C_CONNECTED && mdev->state.disk < D_INCONSISTENT)
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003670 updated_uuids |= drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
3671
3672 if (updated_uuids)
3673 drbd_print_uuids(mdev, "receiver updated UUIDs to");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003674
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003675 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003676}
3677
3678/**
3679 * convert_state() - Converts the peer's view of the cluster state to our point of view
3680 * @ps: The state as seen by the peer.
3681 */
3682static union drbd_state convert_state(union drbd_state ps)
3683{
3684 union drbd_state ms;
3685
3686 static enum drbd_conns c_tab[] = {
Philipp Reisner369bea62011-07-06 23:04:44 +02003687 [C_WF_REPORT_PARAMS] = C_WF_REPORT_PARAMS,
Philipp Reisnerb411b362009-09-25 16:07:19 -07003688 [C_CONNECTED] = C_CONNECTED,
3689
3690 [C_STARTING_SYNC_S] = C_STARTING_SYNC_T,
3691 [C_STARTING_SYNC_T] = C_STARTING_SYNC_S,
3692 [C_DISCONNECTING] = C_TEAR_DOWN, /* C_NETWORK_FAILURE, */
3693 [C_VERIFY_S] = C_VERIFY_T,
3694 [C_MASK] = C_MASK,
3695 };
3696
3697 ms.i = ps.i;
3698
3699 ms.conn = c_tab[ps.conn];
3700 ms.peer = ps.role;
3701 ms.role = ps.peer;
3702 ms.pdsk = ps.disk;
3703 ms.disk = ps.pdsk;
3704 ms.peer_isp = (ps.aftr_isp | ps.user_isp);
3705
3706 return ms;
3707}
3708
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003709static int receive_req_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003710{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003711 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003712 struct p_req_state *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003713 union drbd_state mask, val;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01003714 enum drbd_state_rv rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003715
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003716 mdev = vnr_to_mdev(tconn, pi->vnr);
3717 if (!mdev)
3718 return -EIO;
3719
Philipp Reisnerb411b362009-09-25 16:07:19 -07003720 mask.i = be32_to_cpu(p->mask);
3721 val.i = be32_to_cpu(p->val);
3722
Philipp Reisner25703f82011-02-07 14:35:25 +01003723 if (test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags) &&
Philipp Reisner8410da82011-02-11 20:11:10 +01003724 mutex_is_locked(mdev->state_mutex)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003725 drbd_send_sr_reply(mdev, SS_CONCURRENT_ST_CHG);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003726 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003727 }
3728
3729 mask = convert_state(mask);
3730 val = convert_state(val);
3731
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003732 rv = drbd_change_state(mdev, CS_VERBOSE, mask, val);
3733 drbd_send_sr_reply(mdev, rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003734
Philipp Reisnerb411b362009-09-25 16:07:19 -07003735 drbd_md_sync(mdev);
3736
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003737 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003738}
3739
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003740static int receive_req_conn_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003741{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003742 struct p_req_state *p = pi->data;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003743 union drbd_state mask, val;
3744 enum drbd_state_rv rv;
3745
3746 mask.i = be32_to_cpu(p->mask);
3747 val.i = be32_to_cpu(p->val);
3748
3749 if (test_bit(DISCARD_CONCURRENT, &tconn->flags) &&
3750 mutex_is_locked(&tconn->cstate_mutex)) {
3751 conn_send_sr_reply(tconn, SS_CONCURRENT_ST_CHG);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003752 return 0;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003753 }
3754
3755 mask = convert_state(mask);
3756 val = convert_state(val);
3757
Philipp Reisner778bcf22011-03-28 12:55:03 +02003758 rv = conn_request_state(tconn, mask, val, CS_VERBOSE | CS_LOCAL_ONLY | CS_IGN_OUTD_FAIL);
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003759 conn_send_sr_reply(tconn, rv);
3760
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003761 return 0;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003762}
3763
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003764static int receive_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003765{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003766 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003767 struct p_state *p = pi->data;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003768 union drbd_state os, ns, peer_state;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003769 enum drbd_disk_state real_peer_disk;
Philipp Reisner65d922c2010-06-16 16:18:09 +02003770 enum chg_state_flags cs_flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003771 int rv;
3772
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003773 mdev = vnr_to_mdev(tconn, pi->vnr);
3774 if (!mdev)
3775 return config_unknown_volume(tconn, pi);
3776
Philipp Reisnerb411b362009-09-25 16:07:19 -07003777 peer_state.i = be32_to_cpu(p->state);
3778
3779 real_peer_disk = peer_state.disk;
3780 if (peer_state.disk == D_NEGOTIATING) {
3781 real_peer_disk = mdev->p_uuid[UI_FLAGS] & 4 ? D_INCONSISTENT : D_CONSISTENT;
3782 dev_info(DEV, "real peer disk state = %s\n", drbd_disk_str(real_peer_disk));
3783 }
3784
Philipp Reisner87eeee42011-01-19 14:16:30 +01003785 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003786 retry:
Philipp Reisner78bae592011-03-28 15:40:12 +02003787 os = ns = drbd_read_state(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01003788 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003789
Philipp Reisner9bcd2522011-09-29 13:00:14 +02003790 /* If this is the "end of sync" confirmation, usually the peer disk
3791 * transitions from D_INCONSISTENT to D_UP_TO_DATE. For empty (0 bits
3792 * set) resync started in PausedSyncT, or if the timing of pause-/
3793 * unpause-sync events has been "just right", the peer disk may
3794 * transition from D_CONSISTENT to D_UP_TO_DATE as well.
3795 */
3796 if ((os.pdsk == D_INCONSISTENT || os.pdsk == D_CONSISTENT) &&
3797 real_peer_disk == D_UP_TO_DATE &&
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003798 os.conn > C_CONNECTED && os.disk == D_UP_TO_DATE) {
3799 /* If we are (becoming) SyncSource, but peer is still in sync
3800 * preparation, ignore its uptodate-ness to avoid flapping, it
3801 * will change to inconsistent once the peer reaches active
3802 * syncing states.
3803 * It may have changed syncer-paused flags, however, so we
3804 * cannot ignore this completely. */
3805 if (peer_state.conn > C_CONNECTED &&
3806 peer_state.conn < C_SYNC_SOURCE)
3807 real_peer_disk = D_INCONSISTENT;
3808
3809 /* if peer_state changes to connected at the same time,
3810 * it explicitly notifies us that it finished resync.
3811 * Maybe we should finish it up, too? */
3812 else if (os.conn >= C_SYNC_SOURCE &&
3813 peer_state.conn == C_CONNECTED) {
3814 if (drbd_bm_total_weight(mdev) <= mdev->rs_failed)
3815 drbd_resync_finished(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003816 return 0;
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003817 }
3818 }
3819
3820 /* peer says his disk is inconsistent, while we think it is uptodate,
3821 * and this happens while the peer still thinks we have a sync going on,
3822 * but we think we are already done with the sync.
3823 * We ignore this to avoid flapping pdsk.
3824 * This should not happen, if the peer is a recent version of drbd. */
3825 if (os.pdsk == D_UP_TO_DATE && real_peer_disk == D_INCONSISTENT &&
3826 os.conn == C_CONNECTED && peer_state.conn > C_SYNC_SOURCE)
3827 real_peer_disk = D_UP_TO_DATE;
3828
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003829 if (ns.conn == C_WF_REPORT_PARAMS)
3830 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003831
Philipp Reisner67531712010-10-27 12:21:30 +02003832 if (peer_state.conn == C_AHEAD)
3833 ns.conn = C_BEHIND;
3834
Philipp Reisnerb411b362009-09-25 16:07:19 -07003835 if (mdev->p_uuid && peer_state.disk >= D_NEGOTIATING &&
3836 get_ldev_if_state(mdev, D_NEGOTIATING)) {
3837 int cr; /* consider resync */
3838
3839 /* if we established a new connection */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003840 cr = (os.conn < C_CONNECTED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003841 /* if we had an established connection
3842 * and one of the nodes newly attaches a disk */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003843 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003844 (peer_state.disk == D_NEGOTIATING ||
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003845 os.disk == D_NEGOTIATING));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003846 /* if we have both been inconsistent, and the peer has been
3847 * forced to be UpToDate with --overwrite-data */
3848 cr |= test_bit(CONSIDER_RESYNC, &mdev->flags);
3849 /* if we had been plain connected, and the admin requested to
3850 * start a sync by "invalidate" or "invalidate-remote" */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003851 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003852 (peer_state.conn >= C_STARTING_SYNC_S &&
3853 peer_state.conn <= C_WF_BITMAP_T));
3854
3855 if (cr)
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003856 ns.conn = drbd_sync_handshake(mdev, peer_state.role, real_peer_disk);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003857
3858 put_ldev(mdev);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003859 if (ns.conn == C_MASK) {
3860 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003861 if (mdev->state.disk == D_NEGOTIATING) {
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02003862 drbd_force_state(mdev, NS(disk, D_FAILED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003863 } else if (peer_state.disk == D_NEGOTIATING) {
3864 dev_err(DEV, "Disk attach process on the peer node was aborted.\n");
3865 peer_state.disk = D_DISKLESS;
Lars Ellenberg580b9762010-02-26 23:15:23 +01003866 real_peer_disk = D_DISKLESS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003867 } else {
Philipp Reisner8169e412011-03-15 18:40:27 +01003868 if (test_and_clear_bit(CONN_DRY_RUN, &mdev->tconn->flags))
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003869 return -EIO;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003870 D_ASSERT(os.conn == C_WF_REPORT_PARAMS);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003871 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003872 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003873 }
3874 }
3875 }
3876
Philipp Reisner87eeee42011-01-19 14:16:30 +01003877 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner78bae592011-03-28 15:40:12 +02003878 if (os.i != drbd_read_state(mdev).i)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003879 goto retry;
3880 clear_bit(CONSIDER_RESYNC, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003881 ns.peer = peer_state.role;
3882 ns.pdsk = real_peer_disk;
3883 ns.peer_isp = (peer_state.aftr_isp | peer_state.user_isp);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003884 if ((ns.conn == C_CONNECTED || ns.conn == C_WF_BITMAP_S) && ns.disk == D_NEGOTIATING)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003885 ns.disk = mdev->new_state_tmp.disk;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003886 cs_flags = CS_VERBOSE + (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED ? 0 : CS_HARD);
Philipp Reisner2aebfab2011-03-28 16:48:11 +02003887 if (ns.pdsk == D_CONSISTENT && drbd_suspended(mdev) && ns.conn == C_CONNECTED && os.conn < C_CONNECTED &&
Philipp Reisner481c6f52010-06-22 14:03:27 +02003888 test_bit(NEW_CUR_UUID, &mdev->flags)) {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01003889 /* Do not allow tl_restart(RESEND) for a rebooted peer. We can only allow this
Philipp Reisner481c6f52010-06-22 14:03:27 +02003890 for temporal network outages! */
Philipp Reisner87eeee42011-01-19 14:16:30 +01003891 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003892 dev_err(DEV, "Aborting Connect, can not thaw IO with an only Consistent peer\n");
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01003893 tl_clear(mdev->tconn);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003894 drbd_uuid_new_current(mdev);
3895 clear_bit(NEW_CUR_UUID, &mdev->flags);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003896 conn_request_state(mdev->tconn, NS2(conn, C_PROTOCOL_ERROR, susp, 0), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003897 return -EIO;
Philipp Reisner481c6f52010-06-22 14:03:27 +02003898 }
Philipp Reisner65d922c2010-06-16 16:18:09 +02003899 rv = _drbd_set_state(mdev, ns, cs_flags, NULL);
Philipp Reisner78bae592011-03-28 15:40:12 +02003900 ns = drbd_read_state(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01003901 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003902
3903 if (rv < SS_SUCCESS) {
Philipp Reisner38fa9982011-03-15 18:24:49 +01003904 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003905 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003906 }
3907
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003908 if (os.conn > C_WF_REPORT_PARAMS) {
3909 if (ns.conn > C_CONNECTED && peer_state.conn <= C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003910 peer_state.disk != D_NEGOTIATING ) {
3911 /* we want resync, peer has not yet decided to sync... */
3912 /* Nowadays only used when forcing a node into primary role and
3913 setting its disk to UpToDate with that */
3914 drbd_send_uuids(mdev);
Philipp Reisner43de7c82011-11-10 13:16:13 +01003915 drbd_send_current_state(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003916 }
3917 }
3918
Philipp Reisner08b165b2011-09-05 16:22:33 +02003919 clear_bit(DISCARD_MY_DATA, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003920
3921 drbd_md_sync(mdev); /* update connected indicator, la_size, ... */
3922
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003923 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003924}
3925
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003926static int receive_sync_uuid(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003927{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003928 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003929 struct p_rs_uuid *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003930
3931 mdev = vnr_to_mdev(tconn, pi->vnr);
3932 if (!mdev)
3933 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003934
3935 wait_event(mdev->misc_wait,
3936 mdev->state.conn == C_WF_SYNC_UUID ||
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02003937 mdev->state.conn == C_BEHIND ||
Philipp Reisnerb411b362009-09-25 16:07:19 -07003938 mdev->state.conn < C_CONNECTED ||
3939 mdev->state.disk < D_NEGOTIATING);
3940
3941 /* D_ASSERT( mdev->state.conn == C_WF_SYNC_UUID ); */
3942
Philipp Reisnerb411b362009-09-25 16:07:19 -07003943 /* Here the _drbd_uuid_ functions are right, current should
3944 _not_ be rotated into the history */
3945 if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
3946 _drbd_uuid_set(mdev, UI_CURRENT, be64_to_cpu(p->uuid));
3947 _drbd_uuid_set(mdev, UI_BITMAP, 0UL);
3948
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003949 drbd_print_uuids(mdev, "updated sync uuid");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003950 drbd_start_resync(mdev, C_SYNC_TARGET);
3951
3952 put_ldev(mdev);
3953 } else
3954 dev_err(DEV, "Ignoring SyncUUID packet!\n");
3955
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003956 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003957}
3958
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003959/**
3960 * receive_bitmap_plain
3961 *
3962 * Return 0 when done, 1 when another iteration is needed, and a negative error
3963 * code upon failure.
3964 */
3965static int
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003966receive_bitmap_plain(struct drbd_conf *mdev, unsigned int size,
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003967 unsigned long *p, struct bm_xfer_ctx *c)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003968{
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003969 unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE -
3970 drbd_header_size(mdev->tconn);
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003971 unsigned int num_words = min_t(size_t, data_size / sizeof(*p),
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003972 c->bm_words - c->word_offset);
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003973 unsigned int want = num_words * sizeof(*p);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003974 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003975
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003976 if (want != size) {
3977 dev_err(DEV, "%s:want (%u) != size (%u)\n", __func__, want, size);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003978 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003979 }
3980 if (want == 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003981 return 0;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003982 err = drbd_recv_all(mdev->tconn, p, want);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003983 if (err)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003984 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003985
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003986 drbd_bm_merge_lel(mdev, c->word_offset, num_words, p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003987
3988 c->word_offset += num_words;
3989 c->bit_offset = c->word_offset * BITS_PER_LONG;
3990 if (c->bit_offset > c->bm_bits)
3991 c->bit_offset = c->bm_bits;
3992
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003993 return 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003994}
3995
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01003996static enum drbd_bitmap_code dcbp_get_code(struct p_compressed_bm *p)
3997{
3998 return (enum drbd_bitmap_code)(p->encoding & 0x0f);
3999}
4000
4001static int dcbp_get_start(struct p_compressed_bm *p)
4002{
4003 return (p->encoding & 0x80) != 0;
4004}
4005
4006static int dcbp_get_pad_bits(struct p_compressed_bm *p)
4007{
4008 return (p->encoding >> 4) & 0x7;
4009}
4010
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004011/**
4012 * recv_bm_rle_bits
4013 *
4014 * Return 0 when done, 1 when another iteration is needed, and a negative error
4015 * code upon failure.
4016 */
4017static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07004018recv_bm_rle_bits(struct drbd_conf *mdev,
4019 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01004020 struct bm_xfer_ctx *c,
4021 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004022{
4023 struct bitstream bs;
4024 u64 look_ahead;
4025 u64 rl;
4026 u64 tmp;
4027 unsigned long s = c->bit_offset;
4028 unsigned long e;
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004029 int toggle = dcbp_get_start(p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004030 int have;
4031 int bits;
4032
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004033 bitstream_init(&bs, p->code, len, dcbp_get_pad_bits(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07004034
4035 bits = bitstream_get_bits(&bs, &look_ahead, 64);
4036 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004037 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004038
4039 for (have = bits; have > 0; s += rl, toggle = !toggle) {
4040 bits = vli_decode_bits(&rl, look_ahead);
4041 if (bits <= 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004042 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004043
4044 if (toggle) {
4045 e = s + rl -1;
4046 if (e >= c->bm_bits) {
4047 dev_err(DEV, "bitmap overflow (e:%lu) while decoding bm RLE packet\n", e);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004048 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004049 }
4050 _drbd_bm_set_bits(mdev, s, e);
4051 }
4052
4053 if (have < bits) {
4054 dev_err(DEV, "bitmap decoding error: h:%d b:%d la:0x%08llx l:%u/%u\n",
4055 have, bits, look_ahead,
4056 (unsigned int)(bs.cur.b - p->code),
4057 (unsigned int)bs.buf_len);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004058 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004059 }
4060 look_ahead >>= bits;
4061 have -= bits;
4062
4063 bits = bitstream_get_bits(&bs, &tmp, 64 - have);
4064 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004065 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004066 look_ahead |= tmp << have;
4067 have += bits;
4068 }
4069
4070 c->bit_offset = s;
4071 bm_xfer_ctx_bit_to_word_offset(c);
4072
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004073 return (s != c->bm_bits);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004074}
4075
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004076/**
4077 * decode_bitmap_c
4078 *
4079 * Return 0 when done, 1 when another iteration is needed, and a negative error
4080 * code upon failure.
4081 */
4082static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07004083decode_bitmap_c(struct drbd_conf *mdev,
4084 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01004085 struct bm_xfer_ctx *c,
4086 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004087{
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004088 if (dcbp_get_code(p) == RLE_VLI_Bits)
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004089 return recv_bm_rle_bits(mdev, p, c, len - sizeof(*p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07004090
4091 /* other variants had been implemented for evaluation,
4092 * but have been dropped as this one turned out to be "best"
4093 * during all our tests. */
4094
4095 dev_err(DEV, "receive_bitmap_c: unknown encoding %u\n", p->encoding);
Philipp Reisner38fa9982011-03-15 18:24:49 +01004096 conn_request_state(mdev->tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004097 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004098}
4099
4100void INFO_bm_xfer_stats(struct drbd_conf *mdev,
4101 const char *direction, struct bm_xfer_ctx *c)
4102{
4103 /* what would it take to transfer it "plaintext" */
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004104 unsigned int header_size = drbd_header_size(mdev->tconn);
4105 unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE - header_size;
4106 unsigned int plain =
4107 header_size * (DIV_ROUND_UP(c->bm_words, data_size) + 1) +
4108 c->bm_words * sizeof(unsigned long);
4109 unsigned int total = c->bytes[0] + c->bytes[1];
4110 unsigned int r;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004111
4112 /* total can not be zero. but just in case: */
4113 if (total == 0)
4114 return;
4115
4116 /* don't report if not compressed */
4117 if (total >= plain)
4118 return;
4119
4120 /* total < plain. check for overflow, still */
4121 r = (total > UINT_MAX/1000) ? (total / (plain/1000))
4122 : (1000 * total / plain);
4123
4124 if (r > 1000)
4125 r = 1000;
4126
4127 r = 1000 - r;
4128 dev_info(DEV, "%s bitmap stats [Bytes(packets)]: plain %u(%u), RLE %u(%u), "
4129 "total %u; compression: %u.%u%%\n",
4130 direction,
4131 c->bytes[1], c->packets[1],
4132 c->bytes[0], c->packets[0],
4133 total, r/10, r % 10);
4134}
4135
4136/* Since we are processing the bitfield from lower addresses to higher,
4137 it does not matter if the process it in 32 bit chunks or 64 bit
4138 chunks as long as it is little endian. (Understand it as byte stream,
4139 beginning with the lowest byte...) If we would use big endian
4140 we would need to process it from the highest address to the lowest,
4141 in order to be agnostic to the 32 vs 64 bits issue.
4142
4143 returns 0 on failure, 1 if we successfully received it. */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004144static int receive_bitmap(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004145{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004146 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004147 struct bm_xfer_ctx c;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004148 int err;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004149
4150 mdev = vnr_to_mdev(tconn, pi->vnr);
4151 if (!mdev)
4152 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004153
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004154 drbd_bm_lock(mdev, "receive bitmap", BM_LOCKED_SET_ALLOWED);
4155 /* you are supposed to send additional out-of-sync information
4156 * if you actually set bits during this phase */
Philipp Reisnerb411b362009-09-25 16:07:19 -07004157
Philipp Reisnerb411b362009-09-25 16:07:19 -07004158 c = (struct bm_xfer_ctx) {
4159 .bm_bits = drbd_bm_bits(mdev),
4160 .bm_words = drbd_bm_words(mdev),
4161 };
4162
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004163 for(;;) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004164 if (pi->cmd == P_BITMAP)
4165 err = receive_bitmap_plain(mdev, pi->size, pi->data, &c);
4166 else if (pi->cmd == P_COMPRESSED_BITMAP) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004167 /* MAYBE: sanity check that we speak proto >= 90,
4168 * and the feature is enabled! */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004169 struct p_compressed_bm *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004170
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004171 if (pi->size > DRBD_SOCKET_BUFFER_SIZE - drbd_header_size(tconn)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004172 dev_err(DEV, "ReportCBitmap packet too large\n");
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004173 err = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004174 goto out;
4175 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004176 if (pi->size <= sizeof(*p)) {
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004177 dev_err(DEV, "ReportCBitmap packet too small (l:%u)\n", pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004178 err = -EIO;
Andreas Gruenbacher78fcbda2010-12-10 22:18:27 +01004179 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004180 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004181 err = drbd_recv_all(mdev->tconn, p, pi->size);
4182 if (err)
4183 goto out;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004184 err = decode_bitmap_c(mdev, p, &c, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004185 } else {
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004186 dev_warn(DEV, "receive_bitmap: cmd neither ReportBitMap nor ReportCBitMap (is 0x%x)", pi->cmd);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004187 err = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004188 goto out;
4189 }
4190
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004191 c.packets[pi->cmd == P_BITMAP]++;
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004192 c.bytes[pi->cmd == P_BITMAP] += drbd_header_size(tconn) + pi->size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004193
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004194 if (err <= 0) {
4195 if (err < 0)
4196 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004197 break;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004198 }
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004199 err = drbd_recv_header(mdev->tconn, pi);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004200 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004201 goto out;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004202 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004203
4204 INFO_bm_xfer_stats(mdev, "receive", &c);
4205
4206 if (mdev->state.conn == C_WF_BITMAP_T) {
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01004207 enum drbd_state_rv rv;
4208
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004209 err = drbd_send_bitmap(mdev);
4210 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004211 goto out;
4212 /* Omit CS_ORDERED with this state transition to avoid deadlocks. */
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01004213 rv = _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
4214 D_ASSERT(rv == SS_SUCCESS);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004215 } else if (mdev->state.conn != C_WF_BITMAP_S) {
4216 /* admin may have requested C_DISCONNECTING,
4217 * other threads may have noticed network errors */
4218 dev_info(DEV, "unexpected cstate (%s) in receive_bitmap\n",
4219 drbd_conn_str(mdev->state.conn));
4220 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004221 err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004222
Philipp Reisnerb411b362009-09-25 16:07:19 -07004223 out:
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004224 drbd_bm_unlock(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004225 if (!err && mdev->state.conn == C_WF_BITMAP_S)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004226 drbd_start_resync(mdev, C_SYNC_SOURCE);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004227 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004228}
4229
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004230static int receive_skip(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004231{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004232 conn_warn(tconn, "skipping unknown optional packet type %d, l: %d!\n",
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004233 pi->cmd, pi->size);
Philipp Reisner2de876e2011-03-15 14:38:01 +01004234
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004235 return ignore_remaining_packet(tconn, pi);
Philipp Reisner2de876e2011-03-15 14:38:01 +01004236}
4237
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004238static int receive_UnplugRemote(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004239{
Philipp Reisnerb411b362009-09-25 16:07:19 -07004240 /* Make sure we've acked all the TCP data associated
4241 * with the data requests being unplugged */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004242 drbd_tcp_quickack(tconn->data.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004243
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004244 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004245}
4246
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004247static int receive_out_of_sync(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner73a01a12010-10-27 14:33:00 +02004248{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004249 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004250 struct p_block_desc *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004251
4252 mdev = vnr_to_mdev(tconn, pi->vnr);
4253 if (!mdev)
4254 return -EIO;
Philipp Reisner73a01a12010-10-27 14:33:00 +02004255
Lars Ellenbergf735e3632010-12-17 21:06:18 +01004256 switch (mdev->state.conn) {
4257 case C_WF_SYNC_UUID:
4258 case C_WF_BITMAP_T:
4259 case C_BEHIND:
4260 break;
4261 default:
4262 dev_err(DEV, "ASSERT FAILED cstate = %s, expected: WFSyncUUID|WFBitMapT|Behind\n",
4263 drbd_conn_str(mdev->state.conn));
4264 }
4265
Philipp Reisner73a01a12010-10-27 14:33:00 +02004266 drbd_set_out_of_sync(mdev, be64_to_cpu(p->sector), be32_to_cpu(p->blksize));
4267
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004268 return 0;
Philipp Reisner73a01a12010-10-27 14:33:00 +02004269}
4270
Philipp Reisner02918be2010-08-20 14:35:10 +02004271struct data_cmd {
4272 int expect_payload;
4273 size_t pkt_size;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004274 int (*fn)(struct drbd_tconn *, struct packet_info *);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004275};
4276
Philipp Reisner02918be2010-08-20 14:35:10 +02004277static struct data_cmd drbd_cmd_handler[] = {
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004278 [P_DATA] = { 1, sizeof(struct p_data), receive_Data },
4279 [P_DATA_REPLY] = { 1, sizeof(struct p_data), receive_DataReply },
4280 [P_RS_DATA_REPLY] = { 1, sizeof(struct p_data), receive_RSDataReply } ,
4281 [P_BARRIER] = { 0, sizeof(struct p_barrier), receive_Barrier } ,
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004282 [P_BITMAP] = { 1, 0, receive_bitmap } ,
4283 [P_COMPRESSED_BITMAP] = { 1, 0, receive_bitmap } ,
4284 [P_UNPLUG_REMOTE] = { 0, 0, receive_UnplugRemote },
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004285 [P_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
4286 [P_RS_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004287 [P_SYNC_PARAM] = { 1, 0, receive_SyncParam },
4288 [P_SYNC_PARAM89] = { 1, 0, receive_SyncParam },
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004289 [P_PROTOCOL] = { 1, sizeof(struct p_protocol), receive_protocol },
4290 [P_UUIDS] = { 0, sizeof(struct p_uuids), receive_uuids },
4291 [P_SIZES] = { 0, sizeof(struct p_sizes), receive_sizes },
4292 [P_STATE] = { 0, sizeof(struct p_state), receive_state },
4293 [P_STATE_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_state },
4294 [P_SYNC_UUID] = { 0, sizeof(struct p_rs_uuid), receive_sync_uuid },
4295 [P_OV_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
4296 [P_OV_REPLY] = { 1, sizeof(struct p_block_req), receive_DataRequest },
4297 [P_CSUM_RS_REQUEST] = { 1, sizeof(struct p_block_req), receive_DataRequest },
4298 [P_DELAY_PROBE] = { 0, sizeof(struct p_delay_probe93), receive_skip },
4299 [P_OUT_OF_SYNC] = { 0, sizeof(struct p_block_desc), receive_out_of_sync },
4300 [P_CONN_ST_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_conn_state },
Philipp Reisner036b17e2011-05-16 17:38:11 +02004301 [P_PROTOCOL_UPDATE] = { 1, sizeof(struct p_protocol), receive_protocol },
Philipp Reisner02918be2010-08-20 14:35:10 +02004302};
4303
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004304static void drbdd(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004305{
Philipp Reisner77351055b2011-02-07 17:24:26 +01004306 struct packet_info pi;
Philipp Reisner02918be2010-08-20 14:35:10 +02004307 size_t shs; /* sub header size */
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004308 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004309
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004310 while (get_t_state(&tconn->receiver) == RUNNING) {
Andreas Gruenbacherdeebe192011-03-25 00:01:04 +01004311 struct data_cmd *cmd;
4312
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004313 drbd_thread_current_set_cpu(&tconn->receiver);
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004314 if (drbd_recv_header(tconn, &pi))
Philipp Reisner02918be2010-08-20 14:35:10 +02004315 goto err_out;
4316
Andreas Gruenbacherdeebe192011-03-25 00:01:04 +01004317 cmd = &drbd_cmd_handler[pi.cmd];
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004318 if (unlikely(pi.cmd >= ARRAY_SIZE(drbd_cmd_handler) || !cmd->fn)) {
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004319 conn_err(tconn, "Unexpected data packet %s (0x%04x)",
4320 cmdname(pi.cmd), pi.cmd);
Philipp Reisner02918be2010-08-20 14:35:10 +02004321 goto err_out;
Lars Ellenberg0b33a912009-11-16 15:58:04 +01004322 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004323
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004324 shs = cmd->pkt_size;
4325 if (pi.size > shs && !cmd->expect_payload) {
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004326 conn_err(tconn, "No payload expected %s l:%d\n",
4327 cmdname(pi.cmd), pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02004328 goto err_out;
4329 }
4330
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004331 if (shs) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004332 err = drbd_recv_all_warn(tconn, pi.data, shs);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004333 if (err)
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004334 goto err_out;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004335 pi.size -= shs;
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004336 }
4337
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004338 err = cmd->fn(tconn, &pi);
4339 if (err) {
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004340 conn_err(tconn, "error receiving %s, e: %d l: %d!\n",
4341 cmdname(pi.cmd), err, pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02004342 goto err_out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004343 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004344 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004345 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004346
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004347 err_out:
4348 conn_request_state(tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004349}
4350
Philipp Reisner0e29d162011-02-18 14:23:11 +01004351void conn_flush_workqueue(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004352{
4353 struct drbd_wq_barrier barr;
4354
4355 barr.w.cb = w_prev_work_done;
Philipp Reisner0e29d162011-02-18 14:23:11 +01004356 barr.w.tconn = tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004357 init_completion(&barr.done);
Philipp Reisner0e29d162011-02-18 14:23:11 +01004358 drbd_queue_work(&tconn->data.work, &barr.w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004359 wait_for_completion(&barr.done);
4360}
4361
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004362static void conn_disconnect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004363{
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004364 struct drbd_conf *mdev;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004365 enum drbd_conns oc;
Philipp Reisner376694a2011-11-07 10:54:28 +01004366 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004367
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004368 if (tconn->cstate == C_STANDALONE)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004369 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004370
4371 /* asender does not clean up anything. it must not interfere, either */
Philipp Reisner360cc742011-02-08 14:29:53 +01004372 drbd_thread_stop(&tconn->asender);
4373 drbd_free_sock(tconn);
4374
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004375 rcu_read_lock();
4376 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
4377 kref_get(&mdev->kref);
4378 rcu_read_unlock();
4379 drbd_disconnected(mdev);
4380 kref_put(&mdev->kref, &drbd_minor_destroy);
4381 rcu_read_lock();
4382 }
4383 rcu_read_unlock();
4384
Philipp Reisner12038a32011-11-09 19:18:00 +01004385 if (!list_empty(&tconn->current_epoch->list))
4386 conn_err(tconn, "ASSERTION FAILED: tconn->current_epoch->list not empty\n");
4387 /* ok, no more ee's on the fly, it is safe to reset the epoch_size */
4388 atomic_set(&tconn->current_epoch->epoch_size, 0);
4389
Philipp Reisner360cc742011-02-08 14:29:53 +01004390 conn_info(tconn, "Connection closed\n");
4391
Philipp Reisnercb703452011-03-24 11:03:07 +01004392 if (conn_highest_role(tconn) == R_PRIMARY && conn_highest_pdsk(tconn) >= D_UNKNOWN)
4393 conn_try_outdate_peer_async(tconn);
4394
Philipp Reisner360cc742011-02-08 14:29:53 +01004395 spin_lock_irq(&tconn->req_lock);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004396 oc = tconn->cstate;
4397 if (oc >= C_UNCONNECTED)
Philipp Reisner376694a2011-11-07 10:54:28 +01004398 _conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004399
Philipp Reisner360cc742011-02-08 14:29:53 +01004400 spin_unlock_irq(&tconn->req_lock);
4401
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02004402 if (oc == C_DISCONNECTING)
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +02004403 conn_request_state(tconn, NS(conn, C_STANDALONE), CS_VERBOSE | CS_HARD);
Philipp Reisner360cc742011-02-08 14:29:53 +01004404}
4405
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004406static int drbd_disconnected(struct drbd_conf *mdev)
Philipp Reisner360cc742011-02-08 14:29:53 +01004407{
Philipp Reisner360cc742011-02-08 14:29:53 +01004408 unsigned int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004409
Philipp Reisner85719572010-07-21 10:20:17 +02004410 /* wait for current activity to cease. */
Philipp Reisner87eeee42011-01-19 14:16:30 +01004411 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004412 _drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
4413 _drbd_wait_ee_list_empty(mdev, &mdev->sync_ee);
4414 _drbd_wait_ee_list_empty(mdev, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004415 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004416
4417 /* We do not have data structures that would allow us to
4418 * get the rs_pending_cnt down to 0 again.
4419 * * On C_SYNC_TARGET we do not have any data structures describing
4420 * the pending RSDataRequest's we have sent.
4421 * * On C_SYNC_SOURCE there is no data structure that tracks
4422 * the P_RS_DATA_REPLY blocks that we sent to the SyncTarget.
4423 * And no, it is not the sum of the reference counts in the
4424 * resync_LRU. The resync_LRU tracks the whole operation including
4425 * the disk-IO, while the rs_pending_cnt only tracks the blocks
4426 * on the fly. */
4427 drbd_rs_cancel_all(mdev);
4428 mdev->rs_total = 0;
4429 mdev->rs_failed = 0;
4430 atomic_set(&mdev->rs_pending_cnt, 0);
4431 wake_up(&mdev->misc_wait);
4432
Philipp Reisnerb411b362009-09-25 16:07:19 -07004433 del_timer_sync(&mdev->resync_timer);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004434 resync_timer_fn((unsigned long)mdev);
4435
Philipp Reisnerb411b362009-09-25 16:07:19 -07004436 /* wait for all w_e_end_data_req, w_e_end_rsdata_req, w_send_barrier,
4437 * w_make_resync_request etc. which may still be on the worker queue
4438 * to be "canceled" */
Philipp Reisnera21e9292011-02-08 15:08:49 +01004439 drbd_flush_workqueue(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004440
Andreas Gruenbachera990be42011-04-06 17:56:48 +02004441 drbd_finish_peer_reqs(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004442
4443 kfree(mdev->p_uuid);
4444 mdev->p_uuid = NULL;
4445
Philipp Reisner2aebfab2011-03-28 16:48:11 +02004446 if (!drbd_suspended(mdev))
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01004447 tl_clear(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004448
Philipp Reisnerb411b362009-09-25 16:07:19 -07004449 drbd_md_sync(mdev);
4450
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004451 /* serialize with bitmap writeout triggered by the state change,
4452 * if any. */
4453 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
4454
Philipp Reisnerb411b362009-09-25 16:07:19 -07004455 /* tcp_close and release of sendpage pages can be deferred. I don't
4456 * want to use SO_LINGER, because apparently it can be deferred for
4457 * more than 20 seconds (longest time I checked).
4458 *
4459 * Actually we don't care for exactly when the network stack does its
4460 * put_page(), but release our reference on these pages right here.
4461 */
Andreas Gruenbacher7721f562011-04-06 17:14:02 +02004462 i = drbd_free_peer_reqs(mdev, &mdev->net_ee);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004463 if (i)
4464 dev_info(DEV, "net_ee not empty, killed %u entries\n", i);
Lars Ellenberg435f0742010-09-06 12:30:25 +02004465 i = atomic_read(&mdev->pp_in_use_by_net);
4466 if (i)
4467 dev_info(DEV, "pp_in_use_by_net = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004468 i = atomic_read(&mdev->pp_in_use);
4469 if (i)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02004470 dev_info(DEV, "pp_in_use = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004471
4472 D_ASSERT(list_empty(&mdev->read_ee));
4473 D_ASSERT(list_empty(&mdev->active_ee));
4474 D_ASSERT(list_empty(&mdev->sync_ee));
4475 D_ASSERT(list_empty(&mdev->done_ee));
4476
Philipp Reisner360cc742011-02-08 14:29:53 +01004477 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004478}
4479
4480/*
4481 * We support PRO_VERSION_MIN to PRO_VERSION_MAX. The protocol version
4482 * we can agree on is stored in agreed_pro_version.
4483 *
4484 * feature flags and the reserved array should be enough room for future
4485 * enhancements of the handshake protocol, and possible plugins...
4486 *
4487 * for now, they are expected to be zero, but ignored.
4488 */
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004489static int drbd_send_features(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004490{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004491 struct drbd_socket *sock;
4492 struct p_connection_features *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004493
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004494 sock = &tconn->data;
4495 p = conn_prepare_command(tconn, sock);
4496 if (!p)
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004497 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004498 memset(p, 0, sizeof(*p));
4499 p->protocol_min = cpu_to_be32(PRO_VERSION_MIN);
4500 p->protocol_max = cpu_to_be32(PRO_VERSION_MAX);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004501 return conn_send_command(tconn, sock, P_CONNECTION_FEATURES, sizeof(*p), NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004502}
4503
4504/*
4505 * return values:
4506 * 1 yes, we have a valid connection
4507 * 0 oops, did not work out, please try again
4508 * -1 peer talks different language,
4509 * no point in trying again, please go standalone.
4510 */
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004511static int drbd_do_features(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004512{
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004513 /* ASSERT current == tconn->receiver ... */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004514 struct p_connection_features *p;
4515 const int expect = sizeof(struct p_connection_features);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004516 struct packet_info pi;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004517 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004518
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004519 err = drbd_send_features(tconn);
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004520 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004521 return 0;
4522
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004523 err = drbd_recv_header(tconn, &pi);
4524 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004525 return 0;
4526
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004527 if (pi.cmd != P_CONNECTION_FEATURES) {
4528 conn_err(tconn, "expected ConnectionFeatures packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004529 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004530 return -1;
4531 }
4532
Philipp Reisner77351055b2011-02-07 17:24:26 +01004533 if (pi.size != expect) {
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004534 conn_err(tconn, "expected ConnectionFeatures length: %u, received: %u\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004535 expect, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004536 return -1;
4537 }
4538
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004539 p = pi.data;
4540 err = drbd_recv_all_warn(tconn, p, expect);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004541 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004542 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004543
Philipp Reisnerb411b362009-09-25 16:07:19 -07004544 p->protocol_min = be32_to_cpu(p->protocol_min);
4545 p->protocol_max = be32_to_cpu(p->protocol_max);
4546 if (p->protocol_max == 0)
4547 p->protocol_max = p->protocol_min;
4548
4549 if (PRO_VERSION_MAX < p->protocol_min ||
4550 PRO_VERSION_MIN > p->protocol_max)
4551 goto incompat;
4552
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004553 tconn->agreed_pro_version = min_t(int, PRO_VERSION_MAX, p->protocol_max);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004554
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004555 conn_info(tconn, "Handshake successful: "
4556 "Agreed network protocol version %d\n", tconn->agreed_pro_version);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004557
4558 return 1;
4559
4560 incompat:
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004561 conn_err(tconn, "incompatible DRBD dialects: "
Philipp Reisnerb411b362009-09-25 16:07:19 -07004562 "I support %d-%d, peer supports %d-%d\n",
4563 PRO_VERSION_MIN, PRO_VERSION_MAX,
4564 p->protocol_min, p->protocol_max);
4565 return -1;
4566}
4567
4568#if !defined(CONFIG_CRYPTO_HMAC) && !defined(CONFIG_CRYPTO_HMAC_MODULE)
Philipp Reisner13e60372011-02-08 09:54:40 +01004569static int drbd_do_auth(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004570{
4571 dev_err(DEV, "This kernel was build without CONFIG_CRYPTO_HMAC.\n");
4572 dev_err(DEV, "You need to disable 'cram-hmac-alg' in drbd.conf.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004573 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004574}
4575#else
4576#define CHALLENGE_LEN 64
Johannes Thomab10d96c2010-01-07 16:02:50 +01004577
4578/* Return value:
4579 1 - auth succeeded,
4580 0 - failed, try again (network error),
4581 -1 - auth failed, don't try again.
4582*/
4583
Philipp Reisner13e60372011-02-08 09:54:40 +01004584static int drbd_do_auth(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004585{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004586 struct drbd_socket *sock;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004587 char my_challenge[CHALLENGE_LEN]; /* 64 Bytes... */
4588 struct scatterlist sg;
4589 char *response = NULL;
4590 char *right_response = NULL;
4591 char *peers_ch = NULL;
Philipp Reisner44ed1672011-04-19 17:10:19 +02004592 unsigned int key_len;
4593 char secret[SHARED_SECRET_MAX]; /* 64 byte */
Philipp Reisnerb411b362009-09-25 16:07:19 -07004594 unsigned int resp_size;
4595 struct hash_desc desc;
Philipp Reisner77351055b2011-02-07 17:24:26 +01004596 struct packet_info pi;
Philipp Reisner44ed1672011-04-19 17:10:19 +02004597 struct net_conf *nc;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004598 int err, rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004599
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004600 /* FIXME: Put the challenge/response into the preallocated socket buffer. */
4601
Philipp Reisner44ed1672011-04-19 17:10:19 +02004602 rcu_read_lock();
4603 nc = rcu_dereference(tconn->net_conf);
4604 key_len = strlen(nc->shared_secret);
4605 memcpy(secret, nc->shared_secret, key_len);
4606 rcu_read_unlock();
4607
Philipp Reisner13e60372011-02-08 09:54:40 +01004608 desc.tfm = tconn->cram_hmac_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004609 desc.flags = 0;
4610
Philipp Reisner44ed1672011-04-19 17:10:19 +02004611 rv = crypto_hash_setkey(tconn->cram_hmac_tfm, (u8 *)secret, key_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004612 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004613 conn_err(tconn, "crypto_hash_setkey() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004614 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004615 goto fail;
4616 }
4617
4618 get_random_bytes(my_challenge, CHALLENGE_LEN);
4619
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004620 sock = &tconn->data;
4621 if (!conn_prepare_command(tconn, sock)) {
4622 rv = 0;
4623 goto fail;
4624 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004625 rv = !conn_send_command(tconn, sock, P_AUTH_CHALLENGE, 0,
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004626 my_challenge, CHALLENGE_LEN);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004627 if (!rv)
4628 goto fail;
4629
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004630 err = drbd_recv_header(tconn, &pi);
4631 if (err) {
4632 rv = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004633 goto fail;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004634 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004635
Philipp Reisner77351055b2011-02-07 17:24:26 +01004636 if (pi.cmd != P_AUTH_CHALLENGE) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004637 conn_err(tconn, "expected AuthChallenge packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004638 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004639 rv = 0;
4640 goto fail;
4641 }
4642
Philipp Reisner77351055b2011-02-07 17:24:26 +01004643 if (pi.size > CHALLENGE_LEN * 2) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004644 conn_err(tconn, "expected AuthChallenge payload too big.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004645 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004646 goto fail;
4647 }
4648
Philipp Reisner77351055b2011-02-07 17:24:26 +01004649 peers_ch = kmalloc(pi.size, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004650 if (peers_ch == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004651 conn_err(tconn, "kmalloc of peers_ch failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004652 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004653 goto fail;
4654 }
4655
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004656 err = drbd_recv_all_warn(tconn, peers_ch, pi.size);
4657 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004658 rv = 0;
4659 goto fail;
4660 }
4661
Philipp Reisner13e60372011-02-08 09:54:40 +01004662 resp_size = crypto_hash_digestsize(tconn->cram_hmac_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004663 response = kmalloc(resp_size, GFP_NOIO);
4664 if (response == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004665 conn_err(tconn, "kmalloc of response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004666 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004667 goto fail;
4668 }
4669
4670 sg_init_table(&sg, 1);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004671 sg_set_buf(&sg, peers_ch, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004672
4673 rv = crypto_hash_digest(&desc, &sg, sg.length, response);
4674 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004675 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004676 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004677 goto fail;
4678 }
4679
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004680 if (!conn_prepare_command(tconn, sock)) {
4681 rv = 0;
4682 goto fail;
4683 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004684 rv = !conn_send_command(tconn, sock, P_AUTH_RESPONSE, 0,
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004685 response, resp_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004686 if (!rv)
4687 goto fail;
4688
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004689 err = drbd_recv_header(tconn, &pi);
4690 if (err) {
4691 rv = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004692 goto fail;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004693 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004694
Philipp Reisner77351055b2011-02-07 17:24:26 +01004695 if (pi.cmd != P_AUTH_RESPONSE) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004696 conn_err(tconn, "expected AuthResponse packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004697 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004698 rv = 0;
4699 goto fail;
4700 }
4701
Philipp Reisner77351055b2011-02-07 17:24:26 +01004702 if (pi.size != resp_size) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004703 conn_err(tconn, "expected AuthResponse payload of wrong size\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004704 rv = 0;
4705 goto fail;
4706 }
4707
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004708 err = drbd_recv_all_warn(tconn, response , resp_size);
4709 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004710 rv = 0;
4711 goto fail;
4712 }
4713
4714 right_response = kmalloc(resp_size, GFP_NOIO);
Julia Lawall2d1ee872009-12-27 22:27:11 +01004715 if (right_response == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004716 conn_err(tconn, "kmalloc of right_response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004717 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004718 goto fail;
4719 }
4720
4721 sg_set_buf(&sg, my_challenge, CHALLENGE_LEN);
4722
4723 rv = crypto_hash_digest(&desc, &sg, sg.length, right_response);
4724 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004725 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004726 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004727 goto fail;
4728 }
4729
4730 rv = !memcmp(response, right_response, resp_size);
4731
4732 if (rv)
Philipp Reisner44ed1672011-04-19 17:10:19 +02004733 conn_info(tconn, "Peer authenticated using %d bytes HMAC\n",
4734 resp_size);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004735 else
4736 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004737
4738 fail:
4739 kfree(peers_ch);
4740 kfree(response);
4741 kfree(right_response);
4742
4743 return rv;
4744}
4745#endif
4746
4747int drbdd_init(struct drbd_thread *thi)
4748{
Philipp Reisner392c8802011-02-09 10:33:31 +01004749 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004750 int h;
4751
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004752 conn_info(tconn, "receiver (re)started\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004753
4754 do {
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004755 h = conn_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004756 if (h == 0) {
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004757 conn_disconnect(tconn);
Philipp Reisner20ee6392011-01-18 15:28:59 +01004758 schedule_timeout_interruptible(HZ);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004759 }
4760 if (h == -1) {
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004761 conn_warn(tconn, "Discarding network configuration.\n");
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004762 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004763 }
4764 } while (h == 0);
4765
Philipp Reisner91fd4da2011-04-20 17:47:29 +02004766 if (h > 0)
4767 drbdd(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004768
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004769 conn_disconnect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004770
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004771 conn_info(tconn, "receiver terminated\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004772 return 0;
4773}
4774
4775/* ********* acknowledge sender ******** */
4776
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004777static int got_conn_RqSReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004778{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004779 struct p_req_state_reply *p = pi->data;
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004780 int retcode = be32_to_cpu(p->retcode);
4781
4782 if (retcode >= SS_SUCCESS) {
4783 set_bit(CONN_WD_ST_CHG_OKAY, &tconn->flags);
4784 } else {
4785 set_bit(CONN_WD_ST_CHG_FAIL, &tconn->flags);
4786 conn_err(tconn, "Requested state change failed by peer: %s (%d)\n",
4787 drbd_set_st_err_str(retcode), retcode);
4788 }
4789 wake_up(&tconn->ping_wait);
4790
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004791 return 0;
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004792}
4793
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004794static int got_RqSReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004795{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004796 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004797 struct p_req_state_reply *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004798 int retcode = be32_to_cpu(p->retcode);
4799
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004800 mdev = vnr_to_mdev(tconn, pi->vnr);
4801 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004802 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004803
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004804 if (retcode >= SS_SUCCESS) {
4805 set_bit(CL_ST_CHG_SUCCESS, &mdev->flags);
4806 } else {
4807 set_bit(CL_ST_CHG_FAIL, &mdev->flags);
4808 dev_err(DEV, "Requested state change failed by peer: %s (%d)\n",
4809 drbd_set_st_err_str(retcode), retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004810 }
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004811 wake_up(&mdev->state_wait);
4812
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004813 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004814}
4815
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004816static int got_Ping(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004817{
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004818 return drbd_send_ping_ack(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004819
4820}
4821
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004822static int got_PingAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004823{
4824 /* restore idle timeout */
Philipp Reisner2a67d8b2011-02-09 14:10:32 +01004825 tconn->meta.socket->sk->sk_rcvtimeo = tconn->net_conf->ping_int*HZ;
4826 if (!test_and_set_bit(GOT_PING_ACK, &tconn->flags))
4827 wake_up(&tconn->ping_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004828
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004829 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004830}
4831
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004832static int got_IsInSync(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004833{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004834 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004835 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004836 sector_t sector = be64_to_cpu(p->sector);
4837 int blksize = be32_to_cpu(p->blksize);
4838
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004839 mdev = vnr_to_mdev(tconn, pi->vnr);
4840 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004841 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004842
Philipp Reisner31890f42011-01-19 14:12:51 +01004843 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004844
4845 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4846
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004847 if (get_ldev(mdev)) {
4848 drbd_rs_complete_io(mdev, sector);
4849 drbd_set_in_sync(mdev, sector, blksize);
4850 /* rs_same_csums is supposed to count in units of BM_BLOCK_SIZE */
4851 mdev->rs_same_csum += (blksize >> BM_BLOCK_SHIFT);
4852 put_ldev(mdev);
4853 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004854 dec_rs_pending(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02004855 atomic_add(blksize >> 9, &mdev->rs_sect_in);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004856
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004857 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004858}
4859
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004860static int
4861validate_req_change_req_state(struct drbd_conf *mdev, u64 id, sector_t sector,
4862 struct rb_root *root, const char *func,
4863 enum drbd_req_event what, bool missing_ok)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004864{
4865 struct drbd_request *req;
4866 struct bio_and_error m;
4867
Philipp Reisner87eeee42011-01-19 14:16:30 +01004868 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004869 req = find_request(mdev, root, id, sector, missing_ok, func);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004870 if (unlikely(!req)) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01004871 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004872 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004873 }
4874 __req_mod(req, what, &m);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004875 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004876
4877 if (m.bio)
4878 complete_master_bio(mdev, &m);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004879 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004880}
4881
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004882static int got_BlockAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004883{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004884 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004885 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004886 sector_t sector = be64_to_cpu(p->sector);
4887 int blksize = be32_to_cpu(p->blksize);
4888 enum drbd_req_event what;
4889
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004890 mdev = vnr_to_mdev(tconn, pi->vnr);
4891 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004892 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004893
Philipp Reisnerb411b362009-09-25 16:07:19 -07004894 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4895
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01004896 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004897 drbd_set_in_sync(mdev, sector, blksize);
4898 dec_rs_pending(mdev);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004899 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004900 }
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004901 switch (pi->cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004902 case P_RS_WRITE_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004903 what = WRITE_ACKED_BY_PEER_AND_SIS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004904 break;
4905 case P_WRITE_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004906 what = WRITE_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004907 break;
4908 case P_RECV_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004909 what = RECV_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004910 break;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004911 case P_DISCARD_WRITE:
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004912 what = DISCARD_WRITE;
4913 break;
4914 case P_RETRY_WRITE:
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004915 what = POSTPONE_WRITE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004916 break;
4917 default:
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004918 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -07004919 }
4920
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004921 return validate_req_change_req_state(mdev, p->block_id, sector,
4922 &mdev->write_requests, __func__,
4923 what, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004924}
4925
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004926static int got_NegAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004927{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004928 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004929 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004930 sector_t sector = be64_to_cpu(p->sector);
Philipp Reisner2deb8332011-01-17 18:39:18 +01004931 int size = be32_to_cpu(p->blksize);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004932 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004933
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004934 mdev = vnr_to_mdev(tconn, pi->vnr);
4935 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004936 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004937
Philipp Reisnerb411b362009-09-25 16:07:19 -07004938 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4939
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01004940 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004941 dec_rs_pending(mdev);
4942 drbd_rs_failed_io(mdev, sector, size);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004943 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004944 }
Philipp Reisner2deb8332011-01-17 18:39:18 +01004945
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004946 err = validate_req_change_req_state(mdev, p->block_id, sector,
4947 &mdev->write_requests, __func__,
Philipp Reisner303d1442011-04-13 16:24:47 -07004948 NEG_ACKED, true);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004949 if (err) {
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004950 /* Protocol A has no P_WRITE_ACKs, but has P_NEG_ACKs.
4951 The master bio might already be completed, therefore the
4952 request is no longer in the collision hash. */
4953 /* In Protocol B we might already have got a P_RECV_ACK
4954 but then get a P_NEG_ACK afterwards. */
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004955 drbd_set_out_of_sync(mdev, sector, size);
Philipp Reisner2deb8332011-01-17 18:39:18 +01004956 }
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004957 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004958}
4959
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004960static int got_NegDReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004961{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004962 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004963 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004964 sector_t sector = be64_to_cpu(p->sector);
4965
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004966 mdev = vnr_to_mdev(tconn, pi->vnr);
4967 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004968 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004969
Philipp Reisnerb411b362009-09-25 16:07:19 -07004970 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004971
Philipp Reisnerb411b362009-09-25 16:07:19 -07004972 dev_err(DEV, "Got NegDReply; Sector %llus, len %u; Fail original request.\n",
4973 (unsigned long long)sector, be32_to_cpu(p->blksize));
4974
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004975 return validate_req_change_req_state(mdev, p->block_id, sector,
4976 &mdev->read_requests, __func__,
4977 NEG_ACKED, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004978}
4979
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004980static int got_NegRSDReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004981{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004982 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004983 sector_t sector;
4984 int size;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004985 struct p_block_ack *p = pi->data;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004986
4987 mdev = vnr_to_mdev(tconn, pi->vnr);
4988 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004989 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004990
4991 sector = be64_to_cpu(p->sector);
4992 size = be32_to_cpu(p->blksize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004993
4994 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4995
4996 dec_rs_pending(mdev);
4997
4998 if (get_ldev_if_state(mdev, D_FAILED)) {
4999 drbd_rs_complete_io(mdev, sector);
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01005000 switch (pi->cmd) {
Philipp Reisnerd612d302010-12-27 10:53:28 +01005001 case P_NEG_RS_DREPLY:
5002 drbd_rs_failed_io(mdev, sector, size);
5003 case P_RS_CANCEL:
5004 break;
5005 default:
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005006 BUG();
Philipp Reisnerd612d302010-12-27 10:53:28 +01005007 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005008 put_ldev(mdev);
5009 }
5010
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005011 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005012}
5013
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005014static int got_BarrierAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005015{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005016 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005017 struct p_barrier_ack *p = pi->data;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005018
5019 mdev = vnr_to_mdev(tconn, pi->vnr);
5020 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005021 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005022
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01005023 tl_release(mdev->tconn, p->barrier, be32_to_cpu(p->set_size));
Philipp Reisnerb411b362009-09-25 16:07:19 -07005024
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02005025 if (mdev->state.conn == C_AHEAD &&
5026 atomic_read(&mdev->ap_in_flight) == 0 &&
Philipp Reisner36baf612011-11-10 14:27:34 +01005027 !test_and_set_bit(AHEAD_TO_SYNC_SOURCE, &mdev->flags)) {
Philipp Reisner370a43e2011-01-14 16:03:11 +01005028 mdev->start_resync_timer.expires = jiffies + HZ;
5029 add_timer(&mdev->start_resync_timer);
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02005030 }
5031
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005032 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005033}
5034
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005035static int got_OVResult(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005036{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005037 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005038 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005039 struct drbd_work *w;
5040 sector_t sector;
5041 int size;
5042
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005043 mdev = vnr_to_mdev(tconn, pi->vnr);
5044 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005045 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005046
Philipp Reisnerb411b362009-09-25 16:07:19 -07005047 sector = be64_to_cpu(p->sector);
5048 size = be32_to_cpu(p->blksize);
5049
5050 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
5051
5052 if (be64_to_cpu(p->block_id) == ID_OUT_OF_SYNC)
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005053 drbd_ov_out_of_sync_found(mdev, sector, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005054 else
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005055 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005056
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005057 if (!get_ldev(mdev))
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005058 return 0;
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005059
Philipp Reisnerb411b362009-09-25 16:07:19 -07005060 drbd_rs_complete_io(mdev, sector);
5061 dec_rs_pending(mdev);
5062
Lars Ellenbergea5442a2010-11-05 09:48:01 +01005063 --mdev->ov_left;
5064
5065 /* let's advance progress step marks only for every other megabyte */
5066 if ((mdev->ov_left & 0x200) == 0x200)
5067 drbd_advance_rs_marks(mdev, mdev->ov_left);
5068
5069 if (mdev->ov_left == 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07005070 w = kmalloc(sizeof(*w), GFP_NOIO);
5071 if (w) {
5072 w->cb = w_ov_finished;
Philipp Reisnera21e9292011-02-08 15:08:49 +01005073 w->mdev = mdev;
Philipp Reisnere42325a2011-01-19 13:55:45 +01005074 drbd_queue_work_front(&mdev->tconn->data.work, w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005075 } else {
5076 dev_err(DEV, "kmalloc(w) failed.");
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005077 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005078 drbd_resync_finished(mdev);
5079 }
5080 }
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005081 put_ldev(mdev);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005082 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005083}
5084
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005085static int got_skip(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner0ced55a2010-04-30 15:26:20 +02005086{
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005087 return 0;
Philipp Reisner0ced55a2010-04-30 15:26:20 +02005088}
5089
Andreas Gruenbachera990be42011-04-06 17:56:48 +02005090static int tconn_finish_peer_reqs(struct drbd_tconn *tconn)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005091{
Philipp Reisner082a3432011-03-15 16:05:42 +01005092 struct drbd_conf *mdev;
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005093 int vnr, not_empty = 0;
Philipp Reisner32862ec2011-02-08 16:41:01 +01005094
5095 do {
5096 clear_bit(SIGNAL_ASENDER, &tconn->flags);
5097 flush_signals(current);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005098
5099 rcu_read_lock();
5100 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
5101 kref_get(&mdev->kref);
5102 rcu_read_unlock();
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07005103 if (drbd_finish_peer_reqs(mdev)) {
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005104 kref_put(&mdev->kref, &drbd_minor_destroy);
5105 return 1;
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07005106 }
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005107 kref_put(&mdev->kref, &drbd_minor_destroy);
5108 rcu_read_lock();
Philipp Reisner082a3432011-03-15 16:05:42 +01005109 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005110 set_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisner082a3432011-03-15 16:05:42 +01005111
5112 spin_lock_irq(&tconn->req_lock);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005113 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Philipp Reisner082a3432011-03-15 16:05:42 +01005114 not_empty = !list_empty(&mdev->done_ee);
5115 if (not_empty)
5116 break;
5117 }
5118 spin_unlock_irq(&tconn->req_lock);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005119 rcu_read_unlock();
Philipp Reisner32862ec2011-02-08 16:41:01 +01005120 } while (not_empty);
5121
5122 return 0;
5123}
5124
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005125struct asender_cmd {
5126 size_t pkt_size;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005127 int (*fn)(struct drbd_tconn *tconn, struct packet_info *);
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005128};
5129
5130static struct asender_cmd asender_tbl[] = {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005131 [P_PING] = { 0, got_Ping },
5132 [P_PING_ACK] = { 0, got_PingAck },
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005133 [P_RECV_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5134 [P_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5135 [P_RS_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5136 [P_DISCARD_WRITE] = { sizeof(struct p_block_ack), got_BlockAck },
5137 [P_NEG_ACK] = { sizeof(struct p_block_ack), got_NegAck },
5138 [P_NEG_DREPLY] = { sizeof(struct p_block_ack), got_NegDReply },
5139 [P_NEG_RS_DREPLY] = { sizeof(struct p_block_ack), got_NegRSDReply },
5140 [P_OV_RESULT] = { sizeof(struct p_block_ack), got_OVResult },
5141 [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), got_BarrierAck },
5142 [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), got_RqSReply },
5143 [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), got_IsInSync },
5144 [P_DELAY_PROBE] = { sizeof(struct p_delay_probe93), got_skip },
5145 [P_RS_CANCEL] = { sizeof(struct p_block_ack), got_NegRSDReply },
5146 [P_CONN_ST_CHG_REPLY]={ sizeof(struct p_req_state_reply), got_conn_RqSReply },
5147 [P_RETRY_WRITE] = { sizeof(struct p_block_ack), got_BlockAck },
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005148};
5149
Philipp Reisnerb411b362009-09-25 16:07:19 -07005150int drbd_asender(struct drbd_thread *thi)
5151{
Philipp Reisner392c8802011-02-09 10:33:31 +01005152 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005153 struct asender_cmd *cmd = NULL;
Philipp Reisner77351055b2011-02-07 17:24:26 +01005154 struct packet_info pi;
Philipp Reisner257d0af2011-01-26 12:15:29 +01005155 int rv;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005156 void *buf = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005157 int received = 0;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005158 unsigned int header_size = drbd_header_size(tconn);
5159 int expect = header_size;
Philipp Reisner44ed1672011-04-19 17:10:19 +02005160 bool ping_timeout_active = false;
5161 struct net_conf *nc;
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005162 int ping_timeo, tcp_cork, ping_int;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005163
Philipp Reisnerb411b362009-09-25 16:07:19 -07005164 current->policy = SCHED_RR; /* Make this a realtime task! */
5165 current->rt_priority = 2; /* more important than all other tasks */
5166
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +01005167 while (get_t_state(thi) == RUNNING) {
Philipp Reisner80822282011-02-08 12:46:30 +01005168 drbd_thread_current_set_cpu(thi);
Philipp Reisner44ed1672011-04-19 17:10:19 +02005169
5170 rcu_read_lock();
5171 nc = rcu_dereference(tconn->net_conf);
5172 ping_timeo = nc->ping_timeo;
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005173 tcp_cork = nc->tcp_cork;
Philipp Reisner44ed1672011-04-19 17:10:19 +02005174 ping_int = nc->ping_int;
5175 rcu_read_unlock();
5176
Philipp Reisner32862ec2011-02-08 16:41:01 +01005177 if (test_and_clear_bit(SEND_PING, &tconn->flags)) {
Andreas Gruenbachera17647a2011-04-01 12:49:42 +02005178 if (drbd_send_ping(tconn)) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005179 conn_err(tconn, "drbd_send_ping has failed\n");
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01005180 goto reconnect;
5181 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02005182 tconn->meta.socket->sk->sk_rcvtimeo = ping_timeo * HZ / 10;
5183 ping_timeout_active = true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005184 }
5185
Philipp Reisner32862ec2011-02-08 16:41:01 +01005186 /* TODO: conditionally cork; it may hurt latency if we cork without
5187 much to send */
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005188 if (tcp_cork)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005189 drbd_tcp_cork(tconn->meta.socket);
Andreas Gruenbachera990be42011-04-06 17:56:48 +02005190 if (tconn_finish_peer_reqs(tconn)) {
5191 conn_err(tconn, "tconn_finish_peer_reqs() failed\n");
Philipp Reisner32862ec2011-02-08 16:41:01 +01005192 goto reconnect;
Philipp Reisner082a3432011-03-15 16:05:42 +01005193 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005194 /* but unconditionally uncork unless disabled */
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005195 if (tcp_cork)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005196 drbd_tcp_uncork(tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005197
5198 /* short circuit, recv_msg would return EINTR anyways. */
5199 if (signal_pending(current))
5200 continue;
5201
Philipp Reisner32862ec2011-02-08 16:41:01 +01005202 rv = drbd_recv_short(tconn->meta.socket, buf, expect-received, 0);
5203 clear_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005204
5205 flush_signals(current);
5206
5207 /* Note:
5208 * -EINTR (on meta) we got a signal
5209 * -EAGAIN (on meta) rcvtimeo expired
5210 * -ECONNRESET other side closed the connection
5211 * -ERESTARTSYS (on data) we got a signal
5212 * rv < 0 other than above: unexpected error!
5213 * rv == expected: full header or command
5214 * rv < expected: "woken" by signal during receive
5215 * rv == 0 : "connection shut down by peer"
5216 */
5217 if (likely(rv > 0)) {
5218 received += rv;
5219 buf += rv;
5220 } else if (rv == 0) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005221 conn_err(tconn, "meta connection shut down by peer.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005222 goto reconnect;
5223 } else if (rv == -EAGAIN) {
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02005224 /* If the data socket received something meanwhile,
5225 * that is good enough: peer is still alive. */
Philipp Reisner32862ec2011-02-08 16:41:01 +01005226 if (time_after(tconn->last_received,
5227 jiffies - tconn->meta.socket->sk->sk_rcvtimeo))
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02005228 continue;
Lars Ellenbergf36af182011-03-09 22:44:55 +01005229 if (ping_timeout_active) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005230 conn_err(tconn, "PingAck did not arrive in time.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005231 goto reconnect;
5232 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005233 set_bit(SEND_PING, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005234 continue;
5235 } else if (rv == -EINTR) {
5236 continue;
5237 } else {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005238 conn_err(tconn, "sock_recvmsg returned %d\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005239 goto reconnect;
5240 }
5241
5242 if (received == expect && cmd == NULL) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005243 if (decode_header(tconn, tconn->meta.rbuf, &pi))
Philipp Reisnerb411b362009-09-25 16:07:19 -07005244 goto reconnect;
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005245 cmd = &asender_tbl[pi.cmd];
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005246 if (pi.cmd >= ARRAY_SIZE(asender_tbl) || !cmd->fn) {
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02005247 conn_err(tconn, "Unexpected meta packet %s (0x%04x)\n",
5248 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005249 goto disconnect;
5250 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005251 expect = header_size + cmd->pkt_size;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005252 if (pi.size != expect - header_size) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005253 conn_err(tconn, "Wrong packet size on meta (c: %d, l: %d)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01005254 pi.cmd, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005255 goto reconnect;
Philipp Reisner257d0af2011-01-26 12:15:29 +01005256 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005257 }
5258 if (received == expect) {
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005259 bool err;
Philipp Reisnera4fbda82011-03-16 11:13:17 +01005260
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005261 err = cmd->fn(tconn, &pi);
5262 if (err) {
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005263 conn_err(tconn, "%pf failed\n", cmd->fn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005264 goto reconnect;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005265 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005266
Philipp Reisnera4fbda82011-03-16 11:13:17 +01005267 tconn->last_received = jiffies;
5268
Philipp Reisner44ed1672011-04-19 17:10:19 +02005269 if (cmd == &asender_tbl[P_PING_ACK]) {
5270 /* restore idle timeout */
5271 tconn->meta.socket->sk->sk_rcvtimeo = ping_int * HZ;
5272 ping_timeout_active = false;
5273 }
Lars Ellenbergf36af182011-03-09 22:44:55 +01005274
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005275 buf = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005276 received = 0;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005277 expect = header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005278 cmd = NULL;
5279 }
5280 }
5281
5282 if (0) {
5283reconnect:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01005284 conn_request_state(tconn, NS(conn, C_NETWORK_FAILURE), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005285 }
5286 if (0) {
5287disconnect:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01005288 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005289 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005290 clear_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005291
Philipp Reisner32862ec2011-02-08 16:41:01 +01005292 conn_info(tconn, "asender terminated\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005293
5294 return 0;
5295}