Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1 | /* |
| 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 Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 26 | #include <linux/module.h> |
| 27 | |
| 28 | #include <asm/uaccess.h> |
| 29 | #include <net/sock.h> |
| 30 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 31 | #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 Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 39 | #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 Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 44 | #include <linux/string.h> |
| 45 | #include <linux/scatterlist.h> |
| 46 | #include "drbd_int.h" |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 47 | #include "drbd_req.h" |
| 48 | |
| 49 | #include "drbd_vli.h" |
| 50 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 51 | enum finish_epoch { |
| 52 | FE_STILL_LIVE, |
| 53 | FE_DESTROYED, |
| 54 | FE_RECYCLED, |
| 55 | }; |
| 56 | |
| 57 | static int drbd_do_handshake(struct drbd_conf *mdev); |
| 58 | static int drbd_do_auth(struct drbd_conf *mdev); |
| 59 | |
| 60 | static enum finish_epoch drbd_may_finish_epoch(struct drbd_conf *, struct drbd_epoch *, enum epoch_event); |
| 61 | static int e_end_block(struct drbd_conf *, struct drbd_work *, int); |
| 62 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 63 | |
| 64 | #define GFP_TRY (__GFP_HIGHMEM | __GFP_NOWARN) |
| 65 | |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 66 | /* |
| 67 | * some helper functions to deal with single linked page lists, |
| 68 | * page->private being our "next" pointer. |
| 69 | */ |
| 70 | |
| 71 | /* If at least n pages are linked at head, get n pages off. |
| 72 | * Otherwise, don't modify head, and return NULL. |
| 73 | * Locking is the responsibility of the caller. |
| 74 | */ |
| 75 | static struct page *page_chain_del(struct page **head, int n) |
| 76 | { |
| 77 | struct page *page; |
| 78 | struct page *tmp; |
| 79 | |
| 80 | BUG_ON(!n); |
| 81 | BUG_ON(!head); |
| 82 | |
| 83 | page = *head; |
Philipp Reisner | 23ce422 | 2010-05-20 13:35:31 +0200 | [diff] [blame] | 84 | |
| 85 | if (!page) |
| 86 | return NULL; |
| 87 | |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 88 | while (page) { |
| 89 | tmp = page_chain_next(page); |
| 90 | if (--n == 0) |
| 91 | break; /* found sufficient pages */ |
| 92 | if (tmp == NULL) |
| 93 | /* insufficient pages, don't use any of them. */ |
| 94 | return NULL; |
| 95 | page = tmp; |
| 96 | } |
| 97 | |
| 98 | /* add end of list marker for the returned list */ |
| 99 | set_page_private(page, 0); |
| 100 | /* actual return value, and adjustment of head */ |
| 101 | page = *head; |
| 102 | *head = tmp; |
| 103 | return page; |
| 104 | } |
| 105 | |
| 106 | /* may be used outside of locks to find the tail of a (usually short) |
| 107 | * "private" page chain, before adding it back to a global chain head |
| 108 | * with page_chain_add() under a spinlock. */ |
| 109 | static struct page *page_chain_tail(struct page *page, int *len) |
| 110 | { |
| 111 | struct page *tmp; |
| 112 | int i = 1; |
| 113 | while ((tmp = page_chain_next(page))) |
| 114 | ++i, page = tmp; |
| 115 | if (len) |
| 116 | *len = i; |
| 117 | return page; |
| 118 | } |
| 119 | |
| 120 | static int page_chain_free(struct page *page) |
| 121 | { |
| 122 | struct page *tmp; |
| 123 | int i = 0; |
| 124 | page_chain_for_each_safe(page, tmp) { |
| 125 | put_page(page); |
| 126 | ++i; |
| 127 | } |
| 128 | return i; |
| 129 | } |
| 130 | |
| 131 | static void page_chain_add(struct page **head, |
| 132 | struct page *chain_first, struct page *chain_last) |
| 133 | { |
| 134 | #if 1 |
| 135 | struct page *tmp; |
| 136 | tmp = page_chain_tail(chain_first, NULL); |
| 137 | BUG_ON(tmp != chain_last); |
| 138 | #endif |
| 139 | |
| 140 | /* add chain to head */ |
| 141 | set_page_private(chain_last, (unsigned long)*head); |
| 142 | *head = chain_first; |
| 143 | } |
| 144 | |
| 145 | static struct page *drbd_pp_first_pages_or_try_alloc(struct drbd_conf *mdev, int number) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 146 | { |
| 147 | struct page *page = NULL; |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 148 | struct page *tmp = NULL; |
| 149 | int i = 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 150 | |
| 151 | /* Yes, testing drbd_pp_vacant outside the lock is racy. |
| 152 | * So what. It saves a spin_lock. */ |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 153 | if (drbd_pp_vacant >= number) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 154 | spin_lock(&drbd_pp_lock); |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 155 | page = page_chain_del(&drbd_pp_pool, number); |
| 156 | if (page) |
| 157 | drbd_pp_vacant -= number; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 158 | spin_unlock(&drbd_pp_lock); |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 159 | if (page) |
| 160 | return page; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 161 | } |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 162 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 163 | /* GFP_TRY, because we must not cause arbitrary write-out: in a DRBD |
| 164 | * "criss-cross" setup, that might cause write-out on some other DRBD, |
| 165 | * which in turn might block on the other node at this very place. */ |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 166 | for (i = 0; i < number; i++) { |
| 167 | tmp = alloc_page(GFP_TRY); |
| 168 | if (!tmp) |
| 169 | break; |
| 170 | set_page_private(tmp, (unsigned long)page); |
| 171 | page = tmp; |
| 172 | } |
| 173 | |
| 174 | if (i == number) |
| 175 | return page; |
| 176 | |
| 177 | /* Not enough pages immediately available this time. |
| 178 | * No need to jump around here, drbd_pp_alloc will retry this |
| 179 | * function "soon". */ |
| 180 | if (page) { |
| 181 | tmp = page_chain_tail(page, NULL); |
| 182 | spin_lock(&drbd_pp_lock); |
| 183 | page_chain_add(&drbd_pp_pool, page, tmp); |
| 184 | drbd_pp_vacant += i; |
| 185 | spin_unlock(&drbd_pp_lock); |
| 186 | } |
| 187 | return NULL; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 190 | static void reclaim_net_ee(struct drbd_conf *mdev, struct list_head *to_be_freed) |
| 191 | { |
| 192 | struct drbd_epoch_entry *e; |
| 193 | struct list_head *le, *tle; |
| 194 | |
| 195 | /* The EEs are always appended to the end of the list. Since |
| 196 | they are sent in order over the wire, they have to finish |
| 197 | in order. As soon as we see the first not finished we can |
| 198 | stop to examine the list... */ |
| 199 | |
| 200 | list_for_each_safe(le, tle, &mdev->net_ee) { |
| 201 | e = list_entry(le, struct drbd_epoch_entry, w.list); |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 202 | if (drbd_ee_has_active_page(e)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 203 | break; |
| 204 | list_move(le, to_be_freed); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | static void drbd_kick_lo_and_reclaim_net(struct drbd_conf *mdev) |
| 209 | { |
| 210 | LIST_HEAD(reclaimed); |
| 211 | struct drbd_epoch_entry *e, *t; |
| 212 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 213 | spin_lock_irq(&mdev->req_lock); |
| 214 | reclaim_net_ee(mdev, &reclaimed); |
| 215 | spin_unlock_irq(&mdev->req_lock); |
| 216 | |
| 217 | list_for_each_entry_safe(e, t, &reclaimed, w.list) |
Lars Ellenberg | 435f074 | 2010-09-06 12:30:25 +0200 | [diff] [blame] | 218 | drbd_free_net_ee(mdev, e); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | /** |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 222 | * drbd_pp_alloc() - Returns @number pages, retries forever (or until signalled) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 223 | * @mdev: DRBD device. |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 224 | * @number: number of pages requested |
| 225 | * @retry: whether to retry, if not enough pages are available right now |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 226 | * |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 227 | * Tries to allocate number pages, first from our own page pool, then from |
| 228 | * the kernel, unless this allocation would exceed the max_buffers setting. |
| 229 | * Possibly retry until DRBD frees sufficient pages somewhere else. |
| 230 | * |
| 231 | * Returns a page chain linked via page->private. |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 232 | */ |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 233 | static struct page *drbd_pp_alloc(struct drbd_conf *mdev, unsigned number, bool retry) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 234 | { |
| 235 | struct page *page = NULL; |
| 236 | DEFINE_WAIT(wait); |
| 237 | |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 238 | /* Yes, we may run up to @number over max_buffers. If we |
| 239 | * follow it strictly, the admin will get it wrong anyways. */ |
| 240 | if (atomic_read(&mdev->pp_in_use) < mdev->net_conf->max_buffers) |
| 241 | page = drbd_pp_first_pages_or_try_alloc(mdev, number); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 242 | |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 243 | while (page == NULL) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 244 | prepare_to_wait(&drbd_pp_wait, &wait, TASK_INTERRUPTIBLE); |
| 245 | |
| 246 | drbd_kick_lo_and_reclaim_net(mdev); |
| 247 | |
| 248 | if (atomic_read(&mdev->pp_in_use) < mdev->net_conf->max_buffers) { |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 249 | page = drbd_pp_first_pages_or_try_alloc(mdev, number); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 250 | if (page) |
| 251 | break; |
| 252 | } |
| 253 | |
| 254 | if (!retry) |
| 255 | break; |
| 256 | |
| 257 | if (signal_pending(current)) { |
| 258 | dev_warn(DEV, "drbd_pp_alloc interrupted!\n"); |
| 259 | break; |
| 260 | } |
| 261 | |
| 262 | schedule(); |
| 263 | } |
| 264 | finish_wait(&drbd_pp_wait, &wait); |
| 265 | |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 266 | if (page) |
| 267 | atomic_add(number, &mdev->pp_in_use); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 268 | return page; |
| 269 | } |
| 270 | |
| 271 | /* Must not be used from irq, as that may deadlock: see drbd_pp_alloc. |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 272 | * Is also used from inside an other spin_lock_irq(&mdev->req_lock); |
| 273 | * Either links the page chain back to the global pool, |
| 274 | * or returns all pages to the system. */ |
Lars Ellenberg | 435f074 | 2010-09-06 12:30:25 +0200 | [diff] [blame] | 275 | static void drbd_pp_free(struct drbd_conf *mdev, struct page *page, int is_net) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 276 | { |
Lars Ellenberg | 435f074 | 2010-09-06 12:30:25 +0200 | [diff] [blame] | 277 | atomic_t *a = is_net ? &mdev->pp_in_use_by_net : &mdev->pp_in_use; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 278 | int i; |
Lars Ellenberg | 435f074 | 2010-09-06 12:30:25 +0200 | [diff] [blame] | 279 | |
Lars Ellenberg | 1816a2b | 2010-11-11 15:19:07 +0100 | [diff] [blame] | 280 | if (drbd_pp_vacant > (DRBD_MAX_BIO_SIZE/PAGE_SIZE)*minor_count) |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 281 | i = page_chain_free(page); |
| 282 | else { |
| 283 | struct page *tmp; |
| 284 | tmp = page_chain_tail(page, &i); |
| 285 | spin_lock(&drbd_pp_lock); |
| 286 | page_chain_add(&drbd_pp_pool, page, tmp); |
| 287 | drbd_pp_vacant += i; |
| 288 | spin_unlock(&drbd_pp_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 289 | } |
Lars Ellenberg | 435f074 | 2010-09-06 12:30:25 +0200 | [diff] [blame] | 290 | i = atomic_sub_return(i, a); |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 291 | if (i < 0) |
Lars Ellenberg | 435f074 | 2010-09-06 12:30:25 +0200 | [diff] [blame] | 292 | dev_warn(DEV, "ASSERTION FAILED: %s: %d < 0\n", |
| 293 | is_net ? "pp_in_use_by_net" : "pp_in_use", i); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 294 | wake_up(&drbd_pp_wait); |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | You need to hold the req_lock: |
| 299 | _drbd_wait_ee_list_empty() |
| 300 | |
| 301 | You must not have the req_lock: |
| 302 | drbd_free_ee() |
| 303 | drbd_alloc_ee() |
| 304 | drbd_init_ee() |
| 305 | drbd_release_ee() |
| 306 | drbd_ee_fix_bhs() |
| 307 | drbd_process_done_ee() |
| 308 | drbd_clear_done_ee() |
| 309 | drbd_wait_ee_list_empty() |
| 310 | */ |
| 311 | |
| 312 | struct drbd_epoch_entry *drbd_alloc_ee(struct drbd_conf *mdev, |
| 313 | u64 id, |
| 314 | sector_t sector, |
| 315 | unsigned int data_size, |
| 316 | gfp_t gfp_mask) __must_hold(local) |
| 317 | { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 318 | struct drbd_epoch_entry *e; |
| 319 | struct page *page; |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 320 | unsigned nr_pages = (data_size + PAGE_SIZE -1) >> PAGE_SHIFT; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 321 | |
Andreas Gruenbacher | 0cf9d27 | 2010-12-07 10:43:29 +0100 | [diff] [blame] | 322 | if (drbd_insert_fault(mdev, DRBD_FAULT_AL_EE)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 323 | return NULL; |
| 324 | |
| 325 | e = mempool_alloc(drbd_ee_mempool, gfp_mask & ~__GFP_HIGHMEM); |
| 326 | if (!e) { |
| 327 | if (!(gfp_mask & __GFP_NOWARN)) |
| 328 | dev_err(DEV, "alloc_ee: Allocation of an EE failed\n"); |
| 329 | return NULL; |
| 330 | } |
| 331 | |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 332 | page = drbd_pp_alloc(mdev, nr_pages, (gfp_mask & __GFP_WAIT)); |
| 333 | if (!page) |
| 334 | goto fail; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 335 | |
Bart Van Assche | 24c4830 | 2011-05-21 18:32:29 +0200 | [diff] [blame] | 336 | INIT_HLIST_NODE(&e->collision); |
Andreas Gruenbacher | 8b94625 | 2011-01-20 15:23:07 +0100 | [diff] [blame^] | 337 | drbd_clear_interval(&e->i); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 338 | e->epoch = NULL; |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 339 | e->mdev = mdev; |
| 340 | e->pages = page; |
| 341 | atomic_set(&e->pending_bios, 0); |
Andreas Gruenbacher | 010f6e6 | 2011-01-14 20:59:35 +0100 | [diff] [blame] | 342 | e->i.size = data_size; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 343 | e->flags = 0; |
Andreas Gruenbacher | 010f6e6 | 2011-01-14 20:59:35 +0100 | [diff] [blame] | 344 | e->i.sector = sector; |
Andreas Gruenbacher | 9a8e775 | 2011-01-11 14:04:09 +0100 | [diff] [blame] | 345 | /* |
| 346 | * The block_id is opaque to the receiver. It is not endianness |
| 347 | * converted, and sent back to the sender unchanged. |
| 348 | */ |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 349 | e->block_id = id; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 350 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 351 | return e; |
| 352 | |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 353 | fail: |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 354 | mempool_free(e, drbd_ee_mempool); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 355 | return NULL; |
| 356 | } |
| 357 | |
Lars Ellenberg | 435f074 | 2010-09-06 12:30:25 +0200 | [diff] [blame] | 358 | void drbd_free_some_ee(struct drbd_conf *mdev, struct drbd_epoch_entry *e, int is_net) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 359 | { |
Lars Ellenberg | c36c3ce | 2010-08-11 20:42:55 +0200 | [diff] [blame] | 360 | if (e->flags & EE_HAS_DIGEST) |
| 361 | kfree(e->digest); |
Lars Ellenberg | 435f074 | 2010-09-06 12:30:25 +0200 | [diff] [blame] | 362 | drbd_pp_free(mdev, e->pages, is_net); |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 363 | D_ASSERT(atomic_read(&e->pending_bios) == 0); |
Bart Van Assche | 24c4830 | 2011-05-21 18:32:29 +0200 | [diff] [blame] | 364 | D_ASSERT(hlist_unhashed(&e->collision)); |
Andreas Gruenbacher | 8b94625 | 2011-01-20 15:23:07 +0100 | [diff] [blame^] | 365 | D_ASSERT(drbd_interval_empty(&e->i)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 366 | mempool_free(e, drbd_ee_mempool); |
| 367 | } |
| 368 | |
| 369 | int drbd_release_ee(struct drbd_conf *mdev, struct list_head *list) |
| 370 | { |
| 371 | LIST_HEAD(work_list); |
| 372 | struct drbd_epoch_entry *e, *t; |
| 373 | int count = 0; |
Lars Ellenberg | 435f074 | 2010-09-06 12:30:25 +0200 | [diff] [blame] | 374 | int is_net = list == &mdev->net_ee; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 375 | |
| 376 | spin_lock_irq(&mdev->req_lock); |
| 377 | list_splice_init(list, &work_list); |
| 378 | spin_unlock_irq(&mdev->req_lock); |
| 379 | |
| 380 | list_for_each_entry_safe(e, t, &work_list, w.list) { |
Lars Ellenberg | 435f074 | 2010-09-06 12:30:25 +0200 | [diff] [blame] | 381 | drbd_free_some_ee(mdev, e, is_net); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 382 | count++; |
| 383 | } |
| 384 | return count; |
| 385 | } |
| 386 | |
| 387 | |
| 388 | /* |
| 389 | * This function is called from _asender only_ |
| 390 | * but see also comments in _req_mod(,barrier_acked) |
| 391 | * and receive_Barrier. |
| 392 | * |
| 393 | * Move entries from net_ee to done_ee, if ready. |
| 394 | * Grab done_ee, call all callbacks, free the entries. |
| 395 | * The callbacks typically send out ACKs. |
| 396 | */ |
| 397 | static int drbd_process_done_ee(struct drbd_conf *mdev) |
| 398 | { |
| 399 | LIST_HEAD(work_list); |
| 400 | LIST_HEAD(reclaimed); |
| 401 | struct drbd_epoch_entry *e, *t; |
| 402 | int ok = (mdev->state.conn >= C_WF_REPORT_PARAMS); |
| 403 | |
| 404 | spin_lock_irq(&mdev->req_lock); |
| 405 | reclaim_net_ee(mdev, &reclaimed); |
| 406 | list_splice_init(&mdev->done_ee, &work_list); |
| 407 | spin_unlock_irq(&mdev->req_lock); |
| 408 | |
| 409 | list_for_each_entry_safe(e, t, &reclaimed, w.list) |
Lars Ellenberg | 435f074 | 2010-09-06 12:30:25 +0200 | [diff] [blame] | 410 | drbd_free_net_ee(mdev, e); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 411 | |
| 412 | /* possible callbacks here: |
| 413 | * e_end_block, and e_end_resync_block, e_send_discard_ack. |
| 414 | * all ignore the last argument. |
| 415 | */ |
| 416 | list_for_each_entry_safe(e, t, &work_list, w.list) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 417 | /* list_del not necessary, next/prev members not touched */ |
| 418 | ok = e->w.cb(mdev, &e->w, !ok) && ok; |
| 419 | drbd_free_ee(mdev, e); |
| 420 | } |
| 421 | wake_up(&mdev->ee_wait); |
| 422 | |
| 423 | return ok; |
| 424 | } |
| 425 | |
| 426 | void _drbd_wait_ee_list_empty(struct drbd_conf *mdev, struct list_head *head) |
| 427 | { |
| 428 | DEFINE_WAIT(wait); |
| 429 | |
| 430 | /* avoids spin_lock/unlock |
| 431 | * and calling prepare_to_wait in the fast path */ |
| 432 | while (!list_empty(head)) { |
| 433 | prepare_to_wait(&mdev->ee_wait, &wait, TASK_UNINTERRUPTIBLE); |
| 434 | spin_unlock_irq(&mdev->req_lock); |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 435 | io_schedule(); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 436 | finish_wait(&mdev->ee_wait, &wait); |
| 437 | spin_lock_irq(&mdev->req_lock); |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | void drbd_wait_ee_list_empty(struct drbd_conf *mdev, struct list_head *head) |
| 442 | { |
| 443 | spin_lock_irq(&mdev->req_lock); |
| 444 | _drbd_wait_ee_list_empty(mdev, head); |
| 445 | spin_unlock_irq(&mdev->req_lock); |
| 446 | } |
| 447 | |
| 448 | /* see also kernel_accept; which is only present since 2.6.18. |
| 449 | * also we want to log which part of it failed, exactly */ |
| 450 | static int drbd_accept(struct drbd_conf *mdev, const char **what, |
| 451 | struct socket *sock, struct socket **newsock) |
| 452 | { |
| 453 | struct sock *sk = sock->sk; |
| 454 | int err = 0; |
| 455 | |
| 456 | *what = "listen"; |
| 457 | err = sock->ops->listen(sock, 5); |
| 458 | if (err < 0) |
| 459 | goto out; |
| 460 | |
| 461 | *what = "sock_create_lite"; |
| 462 | err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol, |
| 463 | newsock); |
| 464 | if (err < 0) |
| 465 | goto out; |
| 466 | |
| 467 | *what = "accept"; |
| 468 | err = sock->ops->accept(sock, *newsock, 0); |
| 469 | if (err < 0) { |
| 470 | sock_release(*newsock); |
| 471 | *newsock = NULL; |
| 472 | goto out; |
| 473 | } |
| 474 | (*newsock)->ops = sock->ops; |
| 475 | |
| 476 | out: |
| 477 | return err; |
| 478 | } |
| 479 | |
| 480 | static int drbd_recv_short(struct drbd_conf *mdev, struct socket *sock, |
| 481 | void *buf, size_t size, int flags) |
| 482 | { |
| 483 | mm_segment_t oldfs; |
| 484 | struct kvec iov = { |
| 485 | .iov_base = buf, |
| 486 | .iov_len = size, |
| 487 | }; |
| 488 | struct msghdr msg = { |
| 489 | .msg_iovlen = 1, |
| 490 | .msg_iov = (struct iovec *)&iov, |
| 491 | .msg_flags = (flags ? flags : MSG_WAITALL | MSG_NOSIGNAL) |
| 492 | }; |
| 493 | int rv; |
| 494 | |
| 495 | oldfs = get_fs(); |
| 496 | set_fs(KERNEL_DS); |
| 497 | rv = sock_recvmsg(sock, &msg, size, msg.msg_flags); |
| 498 | set_fs(oldfs); |
| 499 | |
| 500 | return rv; |
| 501 | } |
| 502 | |
| 503 | static int drbd_recv(struct drbd_conf *mdev, void *buf, size_t size) |
| 504 | { |
| 505 | mm_segment_t oldfs; |
| 506 | struct kvec iov = { |
| 507 | .iov_base = buf, |
| 508 | .iov_len = size, |
| 509 | }; |
| 510 | struct msghdr msg = { |
| 511 | .msg_iovlen = 1, |
| 512 | .msg_iov = (struct iovec *)&iov, |
| 513 | .msg_flags = MSG_WAITALL | MSG_NOSIGNAL |
| 514 | }; |
| 515 | int rv; |
| 516 | |
| 517 | oldfs = get_fs(); |
| 518 | set_fs(KERNEL_DS); |
| 519 | |
| 520 | for (;;) { |
| 521 | rv = sock_recvmsg(mdev->data.socket, &msg, size, msg.msg_flags); |
| 522 | if (rv == size) |
| 523 | break; |
| 524 | |
| 525 | /* Note: |
| 526 | * ECONNRESET other side closed the connection |
| 527 | * ERESTARTSYS (on sock) we got a signal |
| 528 | */ |
| 529 | |
| 530 | if (rv < 0) { |
| 531 | if (rv == -ECONNRESET) |
| 532 | dev_info(DEV, "sock was reset by peer\n"); |
| 533 | else if (rv != -ERESTARTSYS) |
| 534 | dev_err(DEV, "sock_recvmsg returned %d\n", rv); |
| 535 | break; |
| 536 | } else if (rv == 0) { |
| 537 | dev_info(DEV, "sock was shut down by peer\n"); |
| 538 | break; |
| 539 | } else { |
| 540 | /* signal came in, or peer/link went down, |
| 541 | * after we read a partial message |
| 542 | */ |
| 543 | /* D_ASSERT(signal_pending(current)); */ |
| 544 | break; |
| 545 | } |
| 546 | }; |
| 547 | |
| 548 | set_fs(oldfs); |
| 549 | |
| 550 | if (rv != size) |
| 551 | drbd_force_state(mdev, NS(conn, C_BROKEN_PIPE)); |
| 552 | |
| 553 | return rv; |
| 554 | } |
| 555 | |
Lars Ellenberg | 5dbf167 | 2010-05-25 16:18:01 +0200 | [diff] [blame] | 556 | /* quoting tcp(7): |
| 557 | * On individual connections, the socket buffer size must be set prior to the |
| 558 | * listen(2) or connect(2) calls in order to have it take effect. |
| 559 | * This is our wrapper to do so. |
| 560 | */ |
| 561 | static void drbd_setbufsize(struct socket *sock, unsigned int snd, |
| 562 | unsigned int rcv) |
| 563 | { |
| 564 | /* open coded SO_SNDBUF, SO_RCVBUF */ |
| 565 | if (snd) { |
| 566 | sock->sk->sk_sndbuf = snd; |
| 567 | sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK; |
| 568 | } |
| 569 | if (rcv) { |
| 570 | sock->sk->sk_rcvbuf = rcv; |
| 571 | sock->sk->sk_userlocks |= SOCK_RCVBUF_LOCK; |
| 572 | } |
| 573 | } |
| 574 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 575 | static struct socket *drbd_try_connect(struct drbd_conf *mdev) |
| 576 | { |
| 577 | const char *what; |
| 578 | struct socket *sock; |
| 579 | struct sockaddr_in6 src_in6; |
| 580 | int err; |
| 581 | int disconnect_on_error = 1; |
| 582 | |
| 583 | if (!get_net_conf(mdev)) |
| 584 | return NULL; |
| 585 | |
| 586 | what = "sock_create_kern"; |
| 587 | err = sock_create_kern(((struct sockaddr *)mdev->net_conf->my_addr)->sa_family, |
| 588 | SOCK_STREAM, IPPROTO_TCP, &sock); |
| 589 | if (err < 0) { |
| 590 | sock = NULL; |
| 591 | goto out; |
| 592 | } |
| 593 | |
| 594 | sock->sk->sk_rcvtimeo = |
| 595 | sock->sk->sk_sndtimeo = mdev->net_conf->try_connect_int*HZ; |
Lars Ellenberg | 5dbf167 | 2010-05-25 16:18:01 +0200 | [diff] [blame] | 596 | drbd_setbufsize(sock, mdev->net_conf->sndbuf_size, |
| 597 | mdev->net_conf->rcvbuf_size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 598 | |
| 599 | /* explicitly bind to the configured IP as source IP |
| 600 | * for the outgoing connections. |
| 601 | * This is needed for multihomed hosts and to be |
| 602 | * able to use lo: interfaces for drbd. |
| 603 | * Make sure to use 0 as port number, so linux selects |
| 604 | * a free one dynamically. |
| 605 | */ |
| 606 | memcpy(&src_in6, mdev->net_conf->my_addr, |
| 607 | min_t(int, mdev->net_conf->my_addr_len, sizeof(src_in6))); |
| 608 | if (((struct sockaddr *)mdev->net_conf->my_addr)->sa_family == AF_INET6) |
| 609 | src_in6.sin6_port = 0; |
| 610 | else |
| 611 | ((struct sockaddr_in *)&src_in6)->sin_port = 0; /* AF_INET & AF_SCI */ |
| 612 | |
| 613 | what = "bind before connect"; |
| 614 | err = sock->ops->bind(sock, |
| 615 | (struct sockaddr *) &src_in6, |
| 616 | mdev->net_conf->my_addr_len); |
| 617 | if (err < 0) |
| 618 | goto out; |
| 619 | |
| 620 | /* connect may fail, peer not yet available. |
| 621 | * stay C_WF_CONNECTION, don't go Disconnecting! */ |
| 622 | disconnect_on_error = 0; |
| 623 | what = "connect"; |
| 624 | err = sock->ops->connect(sock, |
| 625 | (struct sockaddr *)mdev->net_conf->peer_addr, |
| 626 | mdev->net_conf->peer_addr_len, 0); |
| 627 | |
| 628 | out: |
| 629 | if (err < 0) { |
| 630 | if (sock) { |
| 631 | sock_release(sock); |
| 632 | sock = NULL; |
| 633 | } |
| 634 | switch (-err) { |
| 635 | /* timeout, busy, signal pending */ |
| 636 | case ETIMEDOUT: case EAGAIN: case EINPROGRESS: |
| 637 | case EINTR: case ERESTARTSYS: |
| 638 | /* peer not (yet) available, network problem */ |
| 639 | case ECONNREFUSED: case ENETUNREACH: |
| 640 | case EHOSTDOWN: case EHOSTUNREACH: |
| 641 | disconnect_on_error = 0; |
| 642 | break; |
| 643 | default: |
| 644 | dev_err(DEV, "%s failed, err = %d\n", what, err); |
| 645 | } |
| 646 | if (disconnect_on_error) |
| 647 | drbd_force_state(mdev, NS(conn, C_DISCONNECTING)); |
| 648 | } |
| 649 | put_net_conf(mdev); |
| 650 | return sock; |
| 651 | } |
| 652 | |
| 653 | static struct socket *drbd_wait_for_connect(struct drbd_conf *mdev) |
| 654 | { |
| 655 | int timeo, err; |
| 656 | struct socket *s_estab = NULL, *s_listen; |
| 657 | const char *what; |
| 658 | |
| 659 | if (!get_net_conf(mdev)) |
| 660 | return NULL; |
| 661 | |
| 662 | what = "sock_create_kern"; |
| 663 | err = sock_create_kern(((struct sockaddr *)mdev->net_conf->my_addr)->sa_family, |
| 664 | SOCK_STREAM, IPPROTO_TCP, &s_listen); |
| 665 | if (err) { |
| 666 | s_listen = NULL; |
| 667 | goto out; |
| 668 | } |
| 669 | |
| 670 | timeo = mdev->net_conf->try_connect_int * HZ; |
| 671 | timeo += (random32() & 1) ? timeo / 7 : -timeo / 7; /* 28.5% random jitter */ |
| 672 | |
| 673 | s_listen->sk->sk_reuse = 1; /* SO_REUSEADDR */ |
| 674 | s_listen->sk->sk_rcvtimeo = timeo; |
| 675 | s_listen->sk->sk_sndtimeo = timeo; |
Lars Ellenberg | 5dbf167 | 2010-05-25 16:18:01 +0200 | [diff] [blame] | 676 | drbd_setbufsize(s_listen, mdev->net_conf->sndbuf_size, |
| 677 | mdev->net_conf->rcvbuf_size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 678 | |
| 679 | what = "bind before listen"; |
| 680 | err = s_listen->ops->bind(s_listen, |
| 681 | (struct sockaddr *) mdev->net_conf->my_addr, |
| 682 | mdev->net_conf->my_addr_len); |
| 683 | if (err < 0) |
| 684 | goto out; |
| 685 | |
| 686 | err = drbd_accept(mdev, &what, s_listen, &s_estab); |
| 687 | |
| 688 | out: |
| 689 | if (s_listen) |
| 690 | sock_release(s_listen); |
| 691 | if (err < 0) { |
| 692 | if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) { |
| 693 | dev_err(DEV, "%s failed, err = %d\n", what, err); |
| 694 | drbd_force_state(mdev, NS(conn, C_DISCONNECTING)); |
| 695 | } |
| 696 | } |
| 697 | put_net_conf(mdev); |
| 698 | |
| 699 | return s_estab; |
| 700 | } |
| 701 | |
| 702 | static int drbd_send_fp(struct drbd_conf *mdev, |
| 703 | struct socket *sock, enum drbd_packets cmd) |
| 704 | { |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 705 | struct p_header80 *h = &mdev->data.sbuf.header.h80; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 706 | |
| 707 | return _drbd_send_cmd(mdev, sock, cmd, h, sizeof(*h), 0); |
| 708 | } |
| 709 | |
| 710 | static enum drbd_packets drbd_recv_fp(struct drbd_conf *mdev, struct socket *sock) |
| 711 | { |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 712 | struct p_header80 *h = &mdev->data.rbuf.header.h80; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 713 | int rr; |
| 714 | |
| 715 | rr = drbd_recv_short(mdev, sock, h, sizeof(*h), 0); |
| 716 | |
Andreas Gruenbacher | ca9bc12 | 2011-01-11 13:47:24 +0100 | [diff] [blame] | 717 | if (rr == sizeof(*h) && h->magic == cpu_to_be32(DRBD_MAGIC)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 718 | return be16_to_cpu(h->command); |
| 719 | |
| 720 | return 0xffff; |
| 721 | } |
| 722 | |
| 723 | /** |
| 724 | * drbd_socket_okay() - Free the socket if its connection is not okay |
| 725 | * @mdev: DRBD device. |
| 726 | * @sock: pointer to the pointer to the socket. |
| 727 | */ |
| 728 | static int drbd_socket_okay(struct drbd_conf *mdev, struct socket **sock) |
| 729 | { |
| 730 | int rr; |
| 731 | char tb[4]; |
| 732 | |
| 733 | if (!*sock) |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 734 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 735 | |
| 736 | rr = drbd_recv_short(mdev, *sock, tb, 4, MSG_DONTWAIT | MSG_PEEK); |
| 737 | |
| 738 | if (rr > 0 || rr == -EAGAIN) { |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 739 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 740 | } else { |
| 741 | sock_release(*sock); |
| 742 | *sock = NULL; |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 743 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 744 | } |
| 745 | } |
| 746 | |
| 747 | /* |
| 748 | * return values: |
| 749 | * 1 yes, we have a valid connection |
| 750 | * 0 oops, did not work out, please try again |
| 751 | * -1 peer talks different language, |
| 752 | * no point in trying again, please go standalone. |
| 753 | * -2 We do not have a network config... |
| 754 | */ |
| 755 | static int drbd_connect(struct drbd_conf *mdev) |
| 756 | { |
| 757 | struct socket *s, *sock, *msock; |
| 758 | int try, h, ok; |
| 759 | |
| 760 | D_ASSERT(!mdev->data.socket); |
| 761 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 762 | if (drbd_request_state(mdev, NS(conn, C_WF_CONNECTION)) < SS_SUCCESS) |
| 763 | return -2; |
| 764 | |
| 765 | clear_bit(DISCARD_CONCURRENT, &mdev->flags); |
| 766 | |
| 767 | sock = NULL; |
| 768 | msock = NULL; |
| 769 | |
| 770 | do { |
| 771 | for (try = 0;;) { |
| 772 | /* 3 tries, this should take less than a second! */ |
| 773 | s = drbd_try_connect(mdev); |
| 774 | if (s || ++try >= 3) |
| 775 | break; |
| 776 | /* give the other side time to call bind() & listen() */ |
Philipp Reisner | 20ee639 | 2011-01-18 15:28:59 +0100 | [diff] [blame] | 777 | schedule_timeout_interruptible(HZ / 10); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | if (s) { |
| 781 | if (!sock) { |
| 782 | drbd_send_fp(mdev, s, P_HAND_SHAKE_S); |
| 783 | sock = s; |
| 784 | s = NULL; |
| 785 | } else if (!msock) { |
| 786 | drbd_send_fp(mdev, s, P_HAND_SHAKE_M); |
| 787 | msock = s; |
| 788 | s = NULL; |
| 789 | } else { |
| 790 | dev_err(DEV, "Logic error in drbd_connect()\n"); |
| 791 | goto out_release_sockets; |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | if (sock && msock) { |
Philipp Reisner | a8e4079 | 2011-05-13 12:03:55 +0200 | [diff] [blame] | 796 | schedule_timeout_interruptible(mdev->net_conf->ping_timeo*HZ/10); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 797 | ok = drbd_socket_okay(mdev, &sock); |
| 798 | ok = drbd_socket_okay(mdev, &msock) && ok; |
| 799 | if (ok) |
| 800 | break; |
| 801 | } |
| 802 | |
| 803 | retry: |
| 804 | s = drbd_wait_for_connect(mdev); |
| 805 | if (s) { |
| 806 | try = drbd_recv_fp(mdev, s); |
| 807 | drbd_socket_okay(mdev, &sock); |
| 808 | drbd_socket_okay(mdev, &msock); |
| 809 | switch (try) { |
| 810 | case P_HAND_SHAKE_S: |
| 811 | if (sock) { |
| 812 | dev_warn(DEV, "initial packet S crossed\n"); |
| 813 | sock_release(sock); |
| 814 | } |
| 815 | sock = s; |
| 816 | break; |
| 817 | case P_HAND_SHAKE_M: |
| 818 | if (msock) { |
| 819 | dev_warn(DEV, "initial packet M crossed\n"); |
| 820 | sock_release(msock); |
| 821 | } |
| 822 | msock = s; |
| 823 | set_bit(DISCARD_CONCURRENT, &mdev->flags); |
| 824 | break; |
| 825 | default: |
| 826 | dev_warn(DEV, "Error receiving initial packet\n"); |
| 827 | sock_release(s); |
| 828 | if (random32() & 1) |
| 829 | goto retry; |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | if (mdev->state.conn <= C_DISCONNECTING) |
| 834 | goto out_release_sockets; |
| 835 | if (signal_pending(current)) { |
| 836 | flush_signals(current); |
| 837 | smp_rmb(); |
| 838 | if (get_t_state(&mdev->receiver) == Exiting) |
| 839 | goto out_release_sockets; |
| 840 | } |
| 841 | |
| 842 | if (sock && msock) { |
| 843 | ok = drbd_socket_okay(mdev, &sock); |
| 844 | ok = drbd_socket_okay(mdev, &msock) && ok; |
| 845 | if (ok) |
| 846 | break; |
| 847 | } |
| 848 | } while (1); |
| 849 | |
| 850 | msock->sk->sk_reuse = 1; /* SO_REUSEADDR */ |
| 851 | sock->sk->sk_reuse = 1; /* SO_REUSEADDR */ |
| 852 | |
| 853 | sock->sk->sk_allocation = GFP_NOIO; |
| 854 | msock->sk->sk_allocation = GFP_NOIO; |
| 855 | |
| 856 | sock->sk->sk_priority = TC_PRIO_INTERACTIVE_BULK; |
| 857 | msock->sk->sk_priority = TC_PRIO_INTERACTIVE; |
| 858 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 859 | /* NOT YET ... |
| 860 | * sock->sk->sk_sndtimeo = mdev->net_conf->timeout*HZ/10; |
| 861 | * sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT; |
| 862 | * first set it to the P_HAND_SHAKE timeout, |
| 863 | * which we set to 4x the configured ping_timeout. */ |
| 864 | sock->sk->sk_sndtimeo = |
| 865 | sock->sk->sk_rcvtimeo = mdev->net_conf->ping_timeo*4*HZ/10; |
| 866 | |
| 867 | msock->sk->sk_sndtimeo = mdev->net_conf->timeout*HZ/10; |
| 868 | msock->sk->sk_rcvtimeo = mdev->net_conf->ping_int*HZ; |
| 869 | |
| 870 | /* we don't want delays. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 871 | * we use TCP_CORK where appropriate, though */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 872 | drbd_tcp_nodelay(sock); |
| 873 | drbd_tcp_nodelay(msock); |
| 874 | |
| 875 | mdev->data.socket = sock; |
| 876 | mdev->meta.socket = msock; |
| 877 | mdev->last_received = jiffies; |
| 878 | |
| 879 | D_ASSERT(mdev->asender.task == NULL); |
| 880 | |
| 881 | h = drbd_do_handshake(mdev); |
| 882 | if (h <= 0) |
| 883 | return h; |
| 884 | |
| 885 | if (mdev->cram_hmac_tfm) { |
| 886 | /* drbd_request_state(mdev, NS(conn, WFAuth)); */ |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 887 | switch (drbd_do_auth(mdev)) { |
| 888 | case -1: |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 889 | dev_err(DEV, "Authentication of peer failed\n"); |
| 890 | return -1; |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 891 | case 0: |
| 892 | dev_err(DEV, "Authentication of peer failed, trying again.\n"); |
| 893 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 894 | } |
| 895 | } |
| 896 | |
| 897 | if (drbd_request_state(mdev, NS(conn, C_WF_REPORT_PARAMS)) < SS_SUCCESS) |
| 898 | return 0; |
| 899 | |
| 900 | sock->sk->sk_sndtimeo = mdev->net_conf->timeout*HZ/10; |
| 901 | sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT; |
| 902 | |
| 903 | atomic_set(&mdev->packet_seq, 0); |
| 904 | mdev->peer_seq = 0; |
| 905 | |
| 906 | drbd_thread_start(&mdev->asender); |
| 907 | |
Philipp Reisner | 148efa1 | 2011-01-15 00:21:15 +0100 | [diff] [blame] | 908 | if (drbd_send_protocol(mdev) == -1) |
Philipp Reisner | 7e2455c | 2010-04-22 14:50:23 +0200 | [diff] [blame] | 909 | return -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 910 | drbd_send_sync_param(mdev, &mdev->sync_conf); |
Philipp Reisner | e89b591 | 2010-03-24 17:11:33 +0100 | [diff] [blame] | 911 | drbd_send_sizes(mdev, 0, 0); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 912 | drbd_send_uuids(mdev); |
| 913 | drbd_send_state(mdev); |
| 914 | clear_bit(USE_DEGR_WFC_T, &mdev->flags); |
| 915 | clear_bit(RESIZE_PENDING, &mdev->flags); |
Philipp Reisner | 7fde2be | 2011-03-01 11:08:28 +0100 | [diff] [blame] | 916 | mod_timer(&mdev->request_timer, jiffies + HZ); /* just start it here. */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 917 | |
| 918 | return 1; |
| 919 | |
| 920 | out_release_sockets: |
| 921 | if (sock) |
| 922 | sock_release(sock); |
| 923 | if (msock) |
| 924 | sock_release(msock); |
| 925 | return -1; |
| 926 | } |
| 927 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 928 | static int drbd_recv_header(struct drbd_conf *mdev, enum drbd_packets *cmd, unsigned int *packet_size) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 929 | { |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 930 | union p_header *h = &mdev->data.rbuf.header; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 931 | int r; |
| 932 | |
| 933 | r = drbd_recv(mdev, h, sizeof(*h)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 934 | if (unlikely(r != sizeof(*h))) { |
Lars Ellenberg | 0ddc554 | 2011-01-21 12:35:15 +0100 | [diff] [blame] | 935 | if (!signal_pending(current)) |
| 936 | dev_warn(DEV, "short read expecting header on sock: r=%d\n", r); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 937 | return false; |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 938 | } |
| 939 | |
Andreas Gruenbacher | ca9bc12 | 2011-01-11 13:47:24 +0100 | [diff] [blame] | 940 | if (likely(h->h80.magic == cpu_to_be32(DRBD_MAGIC))) { |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 941 | *cmd = be16_to_cpu(h->h80.command); |
| 942 | *packet_size = be16_to_cpu(h->h80.length); |
Andreas Gruenbacher | ca9bc12 | 2011-01-11 13:47:24 +0100 | [diff] [blame] | 943 | } else if (h->h95.magic == cpu_to_be16(DRBD_MAGIC_BIG)) { |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 944 | *cmd = be16_to_cpu(h->h95.command); |
| 945 | *packet_size = be32_to_cpu(h->h95.length); |
| 946 | } else { |
Lars Ellenberg | 004352f | 2010-10-05 20:13:58 +0200 | [diff] [blame] | 947 | dev_err(DEV, "magic?? on data m: 0x%08x c: %d l: %d\n", |
| 948 | be32_to_cpu(h->h80.magic), |
| 949 | be16_to_cpu(h->h80.command), |
| 950 | be16_to_cpu(h->h80.length)); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 951 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 952 | } |
| 953 | mdev->last_received = jiffies; |
| 954 | |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 955 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 956 | } |
| 957 | |
Philipp Reisner | 2451fc3 | 2010-08-24 13:43:11 +0200 | [diff] [blame] | 958 | static void drbd_flush(struct drbd_conf *mdev) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 959 | { |
| 960 | int rv; |
| 961 | |
| 962 | if (mdev->write_ordering >= WO_bdev_flush && get_ldev(mdev)) { |
Dmitry Monakhov | fbd9b09 | 2010-04-28 17:55:06 +0400 | [diff] [blame] | 963 | rv = blkdev_issue_flush(mdev->ldev->backing_bdev, GFP_KERNEL, |
Christoph Hellwig | dd3932e | 2010-09-16 20:51:46 +0200 | [diff] [blame] | 964 | NULL); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 965 | if (rv) { |
| 966 | dev_err(DEV, "local disk flush failed with status %d\n", rv); |
| 967 | /* would rather check on EOPNOTSUPP, but that is not reliable. |
| 968 | * don't try again for ANY return value != 0 |
| 969 | * if (rv == -EOPNOTSUPP) */ |
| 970 | drbd_bump_write_ordering(mdev, WO_drain_io); |
| 971 | } |
| 972 | put_ldev(mdev); |
| 973 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | /** |
| 977 | * drbd_may_finish_epoch() - Applies an epoch_event to the epoch's state, eventually finishes it. |
| 978 | * @mdev: DRBD device. |
| 979 | * @epoch: Epoch object. |
| 980 | * @ev: Epoch event. |
| 981 | */ |
| 982 | static enum finish_epoch drbd_may_finish_epoch(struct drbd_conf *mdev, |
| 983 | struct drbd_epoch *epoch, |
| 984 | enum epoch_event ev) |
| 985 | { |
Philipp Reisner | 2451fc3 | 2010-08-24 13:43:11 +0200 | [diff] [blame] | 986 | int epoch_size; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 987 | struct drbd_epoch *next_epoch; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 988 | enum finish_epoch rv = FE_STILL_LIVE; |
| 989 | |
| 990 | spin_lock(&mdev->epoch_lock); |
| 991 | do { |
| 992 | next_epoch = NULL; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 993 | |
| 994 | epoch_size = atomic_read(&epoch->epoch_size); |
| 995 | |
| 996 | switch (ev & ~EV_CLEANUP) { |
| 997 | case EV_PUT: |
| 998 | atomic_dec(&epoch->active); |
| 999 | break; |
| 1000 | case EV_GOT_BARRIER_NR: |
| 1001 | set_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1002 | break; |
| 1003 | case EV_BECAME_LAST: |
| 1004 | /* nothing to do*/ |
| 1005 | break; |
| 1006 | } |
| 1007 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1008 | if (epoch_size != 0 && |
| 1009 | atomic_read(&epoch->active) == 0 && |
Philipp Reisner | 2451fc3 | 2010-08-24 13:43:11 +0200 | [diff] [blame] | 1010 | test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags)) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1011 | if (!(ev & EV_CLEANUP)) { |
| 1012 | spin_unlock(&mdev->epoch_lock); |
| 1013 | drbd_send_b_ack(mdev, epoch->barrier_nr, epoch_size); |
| 1014 | spin_lock(&mdev->epoch_lock); |
| 1015 | } |
| 1016 | dec_unacked(mdev); |
| 1017 | |
| 1018 | if (mdev->current_epoch != epoch) { |
| 1019 | next_epoch = list_entry(epoch->list.next, struct drbd_epoch, list); |
| 1020 | list_del(&epoch->list); |
| 1021 | ev = EV_BECAME_LAST | (ev & EV_CLEANUP); |
| 1022 | mdev->epochs--; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1023 | kfree(epoch); |
| 1024 | |
| 1025 | if (rv == FE_STILL_LIVE) |
| 1026 | rv = FE_DESTROYED; |
| 1027 | } else { |
| 1028 | epoch->flags = 0; |
| 1029 | atomic_set(&epoch->epoch_size, 0); |
Uwe Kleine-König | 698f931 | 2010-07-02 20:41:51 +0200 | [diff] [blame] | 1030 | /* atomic_set(&epoch->active, 0); is already zero */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1031 | if (rv == FE_STILL_LIVE) |
| 1032 | rv = FE_RECYCLED; |
Philipp Reisner | 2451fc3 | 2010-08-24 13:43:11 +0200 | [diff] [blame] | 1033 | wake_up(&mdev->ee_wait); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | if (!next_epoch) |
| 1038 | break; |
| 1039 | |
| 1040 | epoch = next_epoch; |
| 1041 | } while (1); |
| 1042 | |
| 1043 | spin_unlock(&mdev->epoch_lock); |
| 1044 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1045 | return rv; |
| 1046 | } |
| 1047 | |
| 1048 | /** |
| 1049 | * drbd_bump_write_ordering() - Fall back to an other write ordering method |
| 1050 | * @mdev: DRBD device. |
| 1051 | * @wo: Write ordering method to try. |
| 1052 | */ |
| 1053 | void drbd_bump_write_ordering(struct drbd_conf *mdev, enum write_ordering_e wo) __must_hold(local) |
| 1054 | { |
| 1055 | enum write_ordering_e pwo; |
| 1056 | static char *write_ordering_str[] = { |
| 1057 | [WO_none] = "none", |
| 1058 | [WO_drain_io] = "drain", |
| 1059 | [WO_bdev_flush] = "flush", |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1060 | }; |
| 1061 | |
| 1062 | pwo = mdev->write_ordering; |
| 1063 | wo = min(pwo, wo); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1064 | if (wo == WO_bdev_flush && mdev->ldev->dc.no_disk_flush) |
| 1065 | wo = WO_drain_io; |
| 1066 | if (wo == WO_drain_io && mdev->ldev->dc.no_disk_drain) |
| 1067 | wo = WO_none; |
| 1068 | mdev->write_ordering = wo; |
Philipp Reisner | 2451fc3 | 2010-08-24 13:43:11 +0200 | [diff] [blame] | 1069 | if (pwo != mdev->write_ordering || wo == WO_bdev_flush) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1070 | dev_info(DEV, "Method to ensure write ordering: %s\n", write_ordering_str[mdev->write_ordering]); |
| 1071 | } |
| 1072 | |
| 1073 | /** |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1074 | * drbd_submit_ee() |
| 1075 | * @mdev: DRBD device. |
| 1076 | * @e: epoch entry |
| 1077 | * @rw: flag field, see bio->bi_rw |
Lars Ellenberg | 10f6d992 | 2011-01-24 14:47:09 +0100 | [diff] [blame] | 1078 | * |
| 1079 | * May spread the pages to multiple bios, |
| 1080 | * depending on bio_add_page restrictions. |
| 1081 | * |
| 1082 | * Returns 0 if all bios have been submitted, |
| 1083 | * -ENOMEM if we could not allocate enough bios, |
| 1084 | * -ENOSPC (any better suggestion?) if we have not been able to bio_add_page a |
| 1085 | * single page to an empty bio (which should never happen and likely indicates |
| 1086 | * that the lower level IO stack is in some way broken). This has been observed |
| 1087 | * on certain Xen deployments. |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1088 | */ |
| 1089 | /* TODO allocate from our own bio_set. */ |
| 1090 | int drbd_submit_ee(struct drbd_conf *mdev, struct drbd_epoch_entry *e, |
| 1091 | const unsigned rw, const int fault_type) |
| 1092 | { |
| 1093 | struct bio *bios = NULL; |
| 1094 | struct bio *bio; |
| 1095 | struct page *page = e->pages; |
Andreas Gruenbacher | 010f6e6 | 2011-01-14 20:59:35 +0100 | [diff] [blame] | 1096 | sector_t sector = e->i.sector; |
| 1097 | unsigned ds = e->i.size; |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1098 | unsigned n_bios = 0; |
| 1099 | unsigned nr_pages = (ds + PAGE_SIZE -1) >> PAGE_SHIFT; |
Lars Ellenberg | 10f6d992 | 2011-01-24 14:47:09 +0100 | [diff] [blame] | 1100 | int err = -ENOMEM; |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1101 | |
| 1102 | /* In most cases, we will only need one bio. But in case the lower |
| 1103 | * level restrictions happen to be different at this offset on this |
| 1104 | * side than those of the sending peer, we may need to submit the |
| 1105 | * request in more than one bio. */ |
| 1106 | next_bio: |
| 1107 | bio = bio_alloc(GFP_NOIO, nr_pages); |
| 1108 | if (!bio) { |
| 1109 | dev_err(DEV, "submit_ee: Allocation of a bio failed\n"); |
| 1110 | goto fail; |
| 1111 | } |
Andreas Gruenbacher | 010f6e6 | 2011-01-14 20:59:35 +0100 | [diff] [blame] | 1112 | /* > e->i.sector, unless this is the first bio */ |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1113 | bio->bi_sector = sector; |
| 1114 | bio->bi_bdev = mdev->ldev->backing_bdev; |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1115 | bio->bi_rw = rw; |
| 1116 | bio->bi_private = e; |
| 1117 | bio->bi_end_io = drbd_endio_sec; |
| 1118 | |
| 1119 | bio->bi_next = bios; |
| 1120 | bios = bio; |
| 1121 | ++n_bios; |
| 1122 | |
| 1123 | page_chain_for_each(page) { |
| 1124 | unsigned len = min_t(unsigned, ds, PAGE_SIZE); |
| 1125 | if (!bio_add_page(bio, page, len, 0)) { |
Lars Ellenberg | 10f6d992 | 2011-01-24 14:47:09 +0100 | [diff] [blame] | 1126 | /* A single page must always be possible! |
| 1127 | * But in case it fails anyways, |
| 1128 | * we deal with it, and complain (below). */ |
| 1129 | if (bio->bi_vcnt == 0) { |
| 1130 | dev_err(DEV, |
| 1131 | "bio_add_page failed for len=%u, " |
| 1132 | "bi_vcnt=0 (bi_sector=%llu)\n", |
| 1133 | len, (unsigned long long)bio->bi_sector); |
| 1134 | err = -ENOSPC; |
| 1135 | goto fail; |
| 1136 | } |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1137 | goto next_bio; |
| 1138 | } |
| 1139 | ds -= len; |
| 1140 | sector += len >> 9; |
| 1141 | --nr_pages; |
| 1142 | } |
| 1143 | D_ASSERT(page == NULL); |
| 1144 | D_ASSERT(ds == 0); |
| 1145 | |
| 1146 | atomic_set(&e->pending_bios, n_bios); |
| 1147 | do { |
| 1148 | bio = bios; |
| 1149 | bios = bios->bi_next; |
| 1150 | bio->bi_next = NULL; |
| 1151 | |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1152 | drbd_generic_make_request(mdev, fault_type, bio); |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1153 | } while (bios); |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1154 | return 0; |
| 1155 | |
| 1156 | fail: |
| 1157 | while (bios) { |
| 1158 | bio = bios; |
| 1159 | bios = bios->bi_next; |
| 1160 | bio_put(bio); |
| 1161 | } |
Lars Ellenberg | 10f6d992 | 2011-01-24 14:47:09 +0100 | [diff] [blame] | 1162 | return err; |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1163 | } |
| 1164 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 1165 | static int receive_Barrier(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1166 | { |
Philipp Reisner | 2451fc3 | 2010-08-24 13:43:11 +0200 | [diff] [blame] | 1167 | int rv; |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 1168 | struct p_barrier *p = &mdev->data.rbuf.barrier; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1169 | struct drbd_epoch *epoch; |
| 1170 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1171 | inc_unacked(mdev); |
| 1172 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1173 | mdev->current_epoch->barrier_nr = p->barrier; |
| 1174 | rv = drbd_may_finish_epoch(mdev, mdev->current_epoch, EV_GOT_BARRIER_NR); |
| 1175 | |
| 1176 | /* P_BARRIER_ACK may imply that the corresponding extent is dropped from |
| 1177 | * the activity log, which means it would not be resynced in case the |
| 1178 | * R_PRIMARY crashes now. |
| 1179 | * Therefore we must send the barrier_ack after the barrier request was |
| 1180 | * completed. */ |
| 1181 | switch (mdev->write_ordering) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1182 | case WO_none: |
| 1183 | if (rv == FE_RECYCLED) |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 1184 | return true; |
Philipp Reisner | 2451fc3 | 2010-08-24 13:43:11 +0200 | [diff] [blame] | 1185 | |
| 1186 | /* receiver context, in the writeout path of the other node. |
| 1187 | * avoid potential distributed deadlock */ |
| 1188 | epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO); |
| 1189 | if (epoch) |
| 1190 | break; |
| 1191 | else |
| 1192 | dev_warn(DEV, "Allocation of an epoch failed, slowing down\n"); |
| 1193 | /* Fall through */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1194 | |
| 1195 | case WO_bdev_flush: |
| 1196 | case WO_drain_io: |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1197 | drbd_wait_ee_list_empty(mdev, &mdev->active_ee); |
Philipp Reisner | 2451fc3 | 2010-08-24 13:43:11 +0200 | [diff] [blame] | 1198 | drbd_flush(mdev); |
| 1199 | |
| 1200 | if (atomic_read(&mdev->current_epoch->epoch_size)) { |
| 1201 | epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO); |
| 1202 | if (epoch) |
| 1203 | break; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1204 | } |
| 1205 | |
Philipp Reisner | 2451fc3 | 2010-08-24 13:43:11 +0200 | [diff] [blame] | 1206 | epoch = mdev->current_epoch; |
| 1207 | wait_event(mdev->ee_wait, atomic_read(&epoch->epoch_size) == 0); |
| 1208 | |
| 1209 | D_ASSERT(atomic_read(&epoch->active) == 0); |
| 1210 | D_ASSERT(epoch->flags == 0); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1211 | |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 1212 | return true; |
Philipp Reisner | 2451fc3 | 2010-08-24 13:43:11 +0200 | [diff] [blame] | 1213 | default: |
| 1214 | dev_err(DEV, "Strangeness in mdev->write_ordering %d\n", mdev->write_ordering); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 1215 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | epoch->flags = 0; |
| 1219 | atomic_set(&epoch->epoch_size, 0); |
| 1220 | atomic_set(&epoch->active, 0); |
| 1221 | |
| 1222 | spin_lock(&mdev->epoch_lock); |
| 1223 | if (atomic_read(&mdev->current_epoch->epoch_size)) { |
| 1224 | list_add(&epoch->list, &mdev->current_epoch->list); |
| 1225 | mdev->current_epoch = epoch; |
| 1226 | mdev->epochs++; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1227 | } else { |
| 1228 | /* The current_epoch got recycled while we allocated this one... */ |
| 1229 | kfree(epoch); |
| 1230 | } |
| 1231 | spin_unlock(&mdev->epoch_lock); |
| 1232 | |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 1233 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | /* used from receive_RSDataReply (recv_resync_read) |
| 1237 | * and from receive_Data */ |
| 1238 | static struct drbd_epoch_entry * |
| 1239 | read_in_block(struct drbd_conf *mdev, u64 id, sector_t sector, int data_size) __must_hold(local) |
| 1240 | { |
Lars Ellenberg | 6666032 | 2010-04-06 12:15:04 +0200 | [diff] [blame] | 1241 | const sector_t capacity = drbd_get_capacity(mdev->this_bdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1242 | struct drbd_epoch_entry *e; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1243 | struct page *page; |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1244 | int dgs, ds, rr; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1245 | void *dig_in = mdev->int_dig_in; |
| 1246 | void *dig_vv = mdev->int_dig_vv; |
Philipp Reisner | 6b4388a | 2010-04-26 14:11:45 +0200 | [diff] [blame] | 1247 | unsigned long *data; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1248 | |
| 1249 | dgs = (mdev->agreed_pro_version >= 87 && mdev->integrity_r_tfm) ? |
| 1250 | crypto_hash_digestsize(mdev->integrity_r_tfm) : 0; |
| 1251 | |
| 1252 | if (dgs) { |
| 1253 | rr = drbd_recv(mdev, dig_in, dgs); |
| 1254 | if (rr != dgs) { |
Lars Ellenberg | 0ddc554 | 2011-01-21 12:35:15 +0100 | [diff] [blame] | 1255 | if (!signal_pending(current)) |
| 1256 | dev_warn(DEV, |
| 1257 | "short read receiving data digest: read %d expected %d\n", |
| 1258 | rr, dgs); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1259 | return NULL; |
| 1260 | } |
| 1261 | } |
| 1262 | |
| 1263 | data_size -= dgs; |
| 1264 | |
Philipp Reisner | d07c9c1 | 2011-01-20 16:49:33 +0100 | [diff] [blame] | 1265 | ERR_IF(data_size == 0) return NULL; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1266 | ERR_IF(data_size & 0x1ff) return NULL; |
Lars Ellenberg | 1816a2b | 2010-11-11 15:19:07 +0100 | [diff] [blame] | 1267 | ERR_IF(data_size > DRBD_MAX_BIO_SIZE) return NULL; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1268 | |
Lars Ellenberg | 6666032 | 2010-04-06 12:15:04 +0200 | [diff] [blame] | 1269 | /* even though we trust out peer, |
| 1270 | * we sometimes have to double check. */ |
| 1271 | if (sector + (data_size>>9) > capacity) { |
Lars Ellenberg | fdda654 | 2011-01-24 15:11:01 +0100 | [diff] [blame] | 1272 | dev_err(DEV, "request from peer beyond end of local disk: " |
| 1273 | "capacity: %llus < sector: %llus + size: %u\n", |
Lars Ellenberg | 6666032 | 2010-04-06 12:15:04 +0200 | [diff] [blame] | 1274 | (unsigned long long)capacity, |
| 1275 | (unsigned long long)sector, data_size); |
| 1276 | return NULL; |
| 1277 | } |
| 1278 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1279 | /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD |
| 1280 | * "criss-cross" setup, that might cause write-out on some other DRBD, |
| 1281 | * which in turn might block on the other node at this very place. */ |
| 1282 | e = drbd_alloc_ee(mdev, id, sector, data_size, GFP_NOIO); |
| 1283 | if (!e) |
| 1284 | return NULL; |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1285 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1286 | ds = data_size; |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1287 | page = e->pages; |
| 1288 | page_chain_for_each(page) { |
| 1289 | unsigned len = min_t(int, ds, PAGE_SIZE); |
Philipp Reisner | 6b4388a | 2010-04-26 14:11:45 +0200 | [diff] [blame] | 1290 | data = kmap(page); |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1291 | rr = drbd_recv(mdev, data, len); |
Andreas Gruenbacher | 0cf9d27 | 2010-12-07 10:43:29 +0100 | [diff] [blame] | 1292 | if (drbd_insert_fault(mdev, DRBD_FAULT_RECEIVE)) { |
Philipp Reisner | 6b4388a | 2010-04-26 14:11:45 +0200 | [diff] [blame] | 1293 | dev_err(DEV, "Fault injection: Corrupting data on receive\n"); |
| 1294 | data[0] = data[0] ^ (unsigned long)-1; |
| 1295 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1296 | kunmap(page); |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1297 | if (rr != len) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1298 | drbd_free_ee(mdev, e); |
Lars Ellenberg | 0ddc554 | 2011-01-21 12:35:15 +0100 | [diff] [blame] | 1299 | if (!signal_pending(current)) |
| 1300 | dev_warn(DEV, "short read receiving data: read %d expected %d\n", |
| 1301 | rr, len); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1302 | return NULL; |
| 1303 | } |
| 1304 | ds -= rr; |
| 1305 | } |
| 1306 | |
| 1307 | if (dgs) { |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1308 | drbd_csum_ee(mdev, mdev->integrity_r_tfm, e, dig_vv); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1309 | if (memcmp(dig_in, dig_vv, dgs)) { |
Lars Ellenberg | 470be44 | 2010-11-10 10:36:52 +0100 | [diff] [blame] | 1310 | dev_err(DEV, "Digest integrity check FAILED: %llus +%u\n", |
| 1311 | (unsigned long long)sector, data_size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1312 | drbd_bcast_ee(mdev, "digest failed", |
| 1313 | dgs, dig_in, dig_vv, e); |
| 1314 | drbd_free_ee(mdev, e); |
| 1315 | return NULL; |
| 1316 | } |
| 1317 | } |
| 1318 | mdev->recv_cnt += data_size>>9; |
| 1319 | return e; |
| 1320 | } |
| 1321 | |
| 1322 | /* drbd_drain_block() just takes a data block |
| 1323 | * out of the socket input buffer, and discards it. |
| 1324 | */ |
| 1325 | static int drbd_drain_block(struct drbd_conf *mdev, int data_size) |
| 1326 | { |
| 1327 | struct page *page; |
| 1328 | int rr, rv = 1; |
| 1329 | void *data; |
| 1330 | |
Lars Ellenberg | c3470cd | 2010-04-01 16:57:19 +0200 | [diff] [blame] | 1331 | if (!data_size) |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 1332 | return true; |
Lars Ellenberg | c3470cd | 2010-04-01 16:57:19 +0200 | [diff] [blame] | 1333 | |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1334 | page = drbd_pp_alloc(mdev, 1, 1); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1335 | |
| 1336 | data = kmap(page); |
| 1337 | while (data_size) { |
| 1338 | rr = drbd_recv(mdev, data, min_t(int, data_size, PAGE_SIZE)); |
| 1339 | if (rr != min_t(int, data_size, PAGE_SIZE)) { |
| 1340 | rv = 0; |
Lars Ellenberg | 0ddc554 | 2011-01-21 12:35:15 +0100 | [diff] [blame] | 1341 | if (!signal_pending(current)) |
| 1342 | dev_warn(DEV, |
| 1343 | "short read receiving data: read %d expected %d\n", |
| 1344 | rr, min_t(int, data_size, PAGE_SIZE)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1345 | break; |
| 1346 | } |
| 1347 | data_size -= rr; |
| 1348 | } |
| 1349 | kunmap(page); |
Lars Ellenberg | 435f074 | 2010-09-06 12:30:25 +0200 | [diff] [blame] | 1350 | drbd_pp_free(mdev, page, 0); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1351 | return rv; |
| 1352 | } |
| 1353 | |
| 1354 | static int recv_dless_read(struct drbd_conf *mdev, struct drbd_request *req, |
| 1355 | sector_t sector, int data_size) |
| 1356 | { |
| 1357 | struct bio_vec *bvec; |
| 1358 | struct bio *bio; |
| 1359 | int dgs, rr, i, expect; |
| 1360 | void *dig_in = mdev->int_dig_in; |
| 1361 | void *dig_vv = mdev->int_dig_vv; |
| 1362 | |
| 1363 | dgs = (mdev->agreed_pro_version >= 87 && mdev->integrity_r_tfm) ? |
| 1364 | crypto_hash_digestsize(mdev->integrity_r_tfm) : 0; |
| 1365 | |
| 1366 | if (dgs) { |
| 1367 | rr = drbd_recv(mdev, dig_in, dgs); |
| 1368 | if (rr != dgs) { |
Lars Ellenberg | 0ddc554 | 2011-01-21 12:35:15 +0100 | [diff] [blame] | 1369 | if (!signal_pending(current)) |
| 1370 | dev_warn(DEV, |
| 1371 | "short read receiving data reply digest: read %d expected %d\n", |
| 1372 | rr, dgs); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1373 | return 0; |
| 1374 | } |
| 1375 | } |
| 1376 | |
| 1377 | data_size -= dgs; |
| 1378 | |
| 1379 | /* optimistically update recv_cnt. if receiving fails below, |
| 1380 | * we disconnect anyways, and counters will be reset. */ |
| 1381 | mdev->recv_cnt += data_size>>9; |
| 1382 | |
| 1383 | bio = req->master_bio; |
| 1384 | D_ASSERT(sector == bio->bi_sector); |
| 1385 | |
| 1386 | bio_for_each_segment(bvec, bio, i) { |
| 1387 | expect = min_t(int, data_size, bvec->bv_len); |
| 1388 | rr = drbd_recv(mdev, |
| 1389 | kmap(bvec->bv_page)+bvec->bv_offset, |
| 1390 | expect); |
| 1391 | kunmap(bvec->bv_page); |
| 1392 | if (rr != expect) { |
Lars Ellenberg | 0ddc554 | 2011-01-21 12:35:15 +0100 | [diff] [blame] | 1393 | if (!signal_pending(current)) |
| 1394 | dev_warn(DEV, "short read receiving data reply: " |
| 1395 | "read %d expected %d\n", |
| 1396 | rr, expect); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1397 | return 0; |
| 1398 | } |
| 1399 | data_size -= rr; |
| 1400 | } |
| 1401 | |
| 1402 | if (dgs) { |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1403 | drbd_csum_bio(mdev, mdev->integrity_r_tfm, bio, dig_vv); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1404 | if (memcmp(dig_in, dig_vv, dgs)) { |
| 1405 | dev_err(DEV, "Digest integrity check FAILED. Broken NICs?\n"); |
| 1406 | return 0; |
| 1407 | } |
| 1408 | } |
| 1409 | |
| 1410 | D_ASSERT(data_size == 0); |
| 1411 | return 1; |
| 1412 | } |
| 1413 | |
| 1414 | /* e_end_resync_block() is called via |
| 1415 | * drbd_process_done_ee() by asender only */ |
| 1416 | static int e_end_resync_block(struct drbd_conf *mdev, struct drbd_work *w, int unused) |
| 1417 | { |
| 1418 | struct drbd_epoch_entry *e = (struct drbd_epoch_entry *)w; |
Andreas Gruenbacher | 010f6e6 | 2011-01-14 20:59:35 +0100 | [diff] [blame] | 1419 | sector_t sector = e->i.sector; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1420 | int ok; |
| 1421 | |
Bart Van Assche | 24c4830 | 2011-05-21 18:32:29 +0200 | [diff] [blame] | 1422 | D_ASSERT(hlist_unhashed(&e->collision)); |
Andreas Gruenbacher | 8b94625 | 2011-01-20 15:23:07 +0100 | [diff] [blame^] | 1423 | D_ASSERT(drbd_interval_empty(&e->i)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1424 | |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1425 | if (likely((e->flags & EE_WAS_ERROR) == 0)) { |
Andreas Gruenbacher | 010f6e6 | 2011-01-14 20:59:35 +0100 | [diff] [blame] | 1426 | drbd_set_in_sync(mdev, sector, e->i.size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1427 | ok = drbd_send_ack(mdev, P_RS_WRITE_ACK, e); |
| 1428 | } else { |
| 1429 | /* Record failure to sync */ |
Andreas Gruenbacher | 010f6e6 | 2011-01-14 20:59:35 +0100 | [diff] [blame] | 1430 | drbd_rs_failed_io(mdev, sector, e->i.size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1431 | |
| 1432 | ok = drbd_send_ack(mdev, P_NEG_ACK, e); |
| 1433 | } |
| 1434 | dec_unacked(mdev); |
| 1435 | |
| 1436 | return ok; |
| 1437 | } |
| 1438 | |
| 1439 | static int recv_resync_read(struct drbd_conf *mdev, sector_t sector, int data_size) __releases(local) |
| 1440 | { |
| 1441 | struct drbd_epoch_entry *e; |
| 1442 | |
| 1443 | e = read_in_block(mdev, ID_SYNCER, sector, data_size); |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1444 | if (!e) |
| 1445 | goto fail; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1446 | |
| 1447 | dec_rs_pending(mdev); |
| 1448 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1449 | inc_unacked(mdev); |
| 1450 | /* corresponding dec_unacked() in e_end_resync_block() |
| 1451 | * respective _drbd_clear_done_ee */ |
| 1452 | |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1453 | e->w.cb = e_end_resync_block; |
| 1454 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1455 | spin_lock_irq(&mdev->req_lock); |
| 1456 | list_add(&e->w.list, &mdev->sync_ee); |
| 1457 | spin_unlock_irq(&mdev->req_lock); |
| 1458 | |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 1459 | atomic_add(data_size >> 9, &mdev->rs_sect_ev); |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1460 | if (drbd_submit_ee(mdev, e, WRITE, DRBD_FAULT_RS_WR) == 0) |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 1461 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1462 | |
Lars Ellenberg | 10f6d992 | 2011-01-24 14:47:09 +0100 | [diff] [blame] | 1463 | /* don't care for the reason here */ |
| 1464 | dev_err(DEV, "submit failed, triggering re-connect\n"); |
Lars Ellenberg | 22cc37a | 2010-09-14 20:40:41 +0200 | [diff] [blame] | 1465 | spin_lock_irq(&mdev->req_lock); |
| 1466 | list_del(&e->w.list); |
| 1467 | spin_unlock_irq(&mdev->req_lock); |
| 1468 | |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1469 | drbd_free_ee(mdev, e); |
| 1470 | fail: |
| 1471 | put_ldev(mdev); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 1472 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1473 | } |
| 1474 | |
Andreas Gruenbacher | 668eebc | 2011-01-20 17:14:26 +0100 | [diff] [blame] | 1475 | static struct drbd_request * |
Andreas Gruenbacher | bc9c5c4 | 2011-01-21 18:00:55 +0100 | [diff] [blame] | 1476 | find_request(struct drbd_conf *mdev, struct rb_root *root, u64 id, |
| 1477 | sector_t sector, bool missing_ok, const char *func) |
Andreas Gruenbacher | 668eebc | 2011-01-20 17:14:26 +0100 | [diff] [blame] | 1478 | { |
Andreas Gruenbacher | 668eebc | 2011-01-20 17:14:26 +0100 | [diff] [blame] | 1479 | struct drbd_request *req; |
| 1480 | |
Andreas Gruenbacher | bc9c5c4 | 2011-01-21 18:00:55 +0100 | [diff] [blame] | 1481 | /* Request object according to our peer */ |
| 1482 | req = (struct drbd_request *)(unsigned long)id; |
| 1483 | if (drbd_contains_interval(root, sector, &req->i)) |
Andreas Gruenbacher | 668eebc | 2011-01-20 17:14:26 +0100 | [diff] [blame] | 1484 | return req; |
Andreas Gruenbacher | c3afd8f | 2011-01-20 22:25:40 +0100 | [diff] [blame] | 1485 | if (!missing_ok) { |
| 1486 | dev_err(DEV, "%s: failed to find request %lu, sector %llus\n", func, |
| 1487 | (unsigned long)id, (unsigned long long)sector); |
| 1488 | } |
Andreas Gruenbacher | 668eebc | 2011-01-20 17:14:26 +0100 | [diff] [blame] | 1489 | return NULL; |
| 1490 | } |
| 1491 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 1492 | static int receive_DataReply(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1493 | { |
| 1494 | struct drbd_request *req; |
| 1495 | sector_t sector; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1496 | int ok; |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 1497 | struct p_data *p = &mdev->data.rbuf.data; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1498 | |
| 1499 | sector = be64_to_cpu(p->sector); |
| 1500 | |
| 1501 | spin_lock_irq(&mdev->req_lock); |
Andreas Gruenbacher | bc9c5c4 | 2011-01-21 18:00:55 +0100 | [diff] [blame] | 1502 | req = find_request(mdev, &mdev->read_requests, p->block_id, sector, false, __func__); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1503 | spin_unlock_irq(&mdev->req_lock); |
Andreas Gruenbacher | c3afd8f | 2011-01-20 22:25:40 +0100 | [diff] [blame] | 1504 | if (unlikely(!req)) |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 1505 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1506 | |
Bart Van Assche | 24c4830 | 2011-05-21 18:32:29 +0200 | [diff] [blame] | 1507 | /* hlist_del(&req->collision) is done in _req_may_be_done, to avoid |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1508 | * special casing it there for the various failure cases. |
| 1509 | * still no race with drbd_fail_pending_reads */ |
| 1510 | ok = recv_dless_read(mdev, req, sector, data_size); |
| 1511 | |
| 1512 | if (ok) |
| 1513 | req_mod(req, data_received); |
| 1514 | /* else: nothing. handled from drbd_disconnect... |
| 1515 | * I don't think we may complete this just yet |
| 1516 | * in case we are "on-disconnect: freeze" */ |
| 1517 | |
| 1518 | return ok; |
| 1519 | } |
| 1520 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 1521 | static int receive_RSDataReply(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1522 | { |
| 1523 | sector_t sector; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1524 | int ok; |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 1525 | struct p_data *p = &mdev->data.rbuf.data; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1526 | |
| 1527 | sector = be64_to_cpu(p->sector); |
| 1528 | D_ASSERT(p->block_id == ID_SYNCER); |
| 1529 | |
| 1530 | if (get_ldev(mdev)) { |
| 1531 | /* data is submitted to disk within recv_resync_read. |
| 1532 | * corresponding put_ldev done below on error, |
Andreas Gruenbacher | 9c50842 | 2011-01-14 21:19:36 +0100 | [diff] [blame] | 1533 | * or in drbd_endio_sec. */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1534 | ok = recv_resync_read(mdev, sector, data_size); |
| 1535 | } else { |
| 1536 | if (__ratelimit(&drbd_ratelimit_state)) |
| 1537 | dev_err(DEV, "Can not write resync data to local disk.\n"); |
| 1538 | |
| 1539 | ok = drbd_drain_block(mdev, data_size); |
| 1540 | |
Lars Ellenberg | 2b2bf21 | 2010-10-06 11:46:55 +0200 | [diff] [blame] | 1541 | drbd_send_ack_dp(mdev, P_NEG_ACK, p, data_size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1542 | } |
| 1543 | |
Philipp Reisner | 778f271 | 2010-07-06 11:14:00 +0200 | [diff] [blame] | 1544 | atomic_add(data_size >> 9, &mdev->rs_sect_in); |
| 1545 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1546 | return ok; |
| 1547 | } |
| 1548 | |
| 1549 | /* e_end_block() is called via drbd_process_done_ee(). |
| 1550 | * this means this function only runs in the asender thread |
| 1551 | */ |
| 1552 | static int e_end_block(struct drbd_conf *mdev, struct drbd_work *w, int cancel) |
| 1553 | { |
| 1554 | struct drbd_epoch_entry *e = (struct drbd_epoch_entry *)w; |
Andreas Gruenbacher | 010f6e6 | 2011-01-14 20:59:35 +0100 | [diff] [blame] | 1555 | sector_t sector = e->i.sector; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1556 | int ok = 1, pcmd; |
| 1557 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1558 | if (mdev->net_conf->wire_protocol == DRBD_PROT_C) { |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1559 | if (likely((e->flags & EE_WAS_ERROR) == 0)) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1560 | pcmd = (mdev->state.conn >= C_SYNC_SOURCE && |
| 1561 | mdev->state.conn <= C_PAUSED_SYNC_T && |
| 1562 | e->flags & EE_MAY_SET_IN_SYNC) ? |
| 1563 | P_RS_WRITE_ACK : P_WRITE_ACK; |
| 1564 | ok &= drbd_send_ack(mdev, pcmd, e); |
| 1565 | if (pcmd == P_RS_WRITE_ACK) |
Andreas Gruenbacher | 010f6e6 | 2011-01-14 20:59:35 +0100 | [diff] [blame] | 1566 | drbd_set_in_sync(mdev, sector, e->i.size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1567 | } else { |
| 1568 | ok = drbd_send_ack(mdev, P_NEG_ACK, e); |
| 1569 | /* we expect it to be marked out of sync anyways... |
| 1570 | * maybe assert this? */ |
| 1571 | } |
| 1572 | dec_unacked(mdev); |
| 1573 | } |
| 1574 | /* we delete from the conflict detection hash _after_ we sent out the |
| 1575 | * P_WRITE_ACK / P_NEG_ACK, to get the sequence number right. */ |
| 1576 | if (mdev->net_conf->two_primaries) { |
| 1577 | spin_lock_irq(&mdev->req_lock); |
Bart Van Assche | 24c4830 | 2011-05-21 18:32:29 +0200 | [diff] [blame] | 1578 | D_ASSERT(!hlist_unhashed(&e->collision)); |
| 1579 | hlist_del_init(&e->collision); |
Andreas Gruenbacher | 8b94625 | 2011-01-20 15:23:07 +0100 | [diff] [blame^] | 1580 | D_ASSERT(!drbd_interval_empty(&e->i)); |
| 1581 | drbd_remove_interval(&mdev->epoch_entries, &e->i); |
| 1582 | drbd_clear_interval(&e->i); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1583 | spin_unlock_irq(&mdev->req_lock); |
| 1584 | } else { |
Bart Van Assche | 24c4830 | 2011-05-21 18:32:29 +0200 | [diff] [blame] | 1585 | D_ASSERT(hlist_unhashed(&e->collision)); |
Andreas Gruenbacher | 8b94625 | 2011-01-20 15:23:07 +0100 | [diff] [blame^] | 1586 | D_ASSERT(drbd_interval_empty(&e->i)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | drbd_may_finish_epoch(mdev, e->epoch, EV_PUT + (cancel ? EV_CLEANUP : 0)); |
| 1590 | |
| 1591 | return ok; |
| 1592 | } |
| 1593 | |
| 1594 | static int e_send_discard_ack(struct drbd_conf *mdev, struct drbd_work *w, int unused) |
| 1595 | { |
| 1596 | struct drbd_epoch_entry *e = (struct drbd_epoch_entry *)w; |
| 1597 | int ok = 1; |
| 1598 | |
| 1599 | D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_C); |
| 1600 | ok = drbd_send_ack(mdev, P_DISCARD_ACK, e); |
| 1601 | |
| 1602 | spin_lock_irq(&mdev->req_lock); |
Bart Van Assche | 24c4830 | 2011-05-21 18:32:29 +0200 | [diff] [blame] | 1603 | D_ASSERT(!hlist_unhashed(&e->collision)); |
| 1604 | hlist_del_init(&e->collision); |
Andreas Gruenbacher | 8b94625 | 2011-01-20 15:23:07 +0100 | [diff] [blame^] | 1605 | D_ASSERT(!drbd_interval_empty(&e->i)); |
| 1606 | drbd_remove_interval(&mdev->epoch_entries, &e->i); |
| 1607 | drbd_clear_interval(&e->i); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1608 | spin_unlock_irq(&mdev->req_lock); |
| 1609 | |
| 1610 | dec_unacked(mdev); |
| 1611 | |
| 1612 | return ok; |
| 1613 | } |
| 1614 | |
| 1615 | /* Called from receive_Data. |
| 1616 | * Synchronize packets on sock with packets on msock. |
| 1617 | * |
| 1618 | * This is here so even when a P_DATA packet traveling via sock overtook an Ack |
| 1619 | * packet traveling on msock, they are still processed in the order they have |
| 1620 | * been sent. |
| 1621 | * |
| 1622 | * Note: we don't care for Ack packets overtaking P_DATA packets. |
| 1623 | * |
| 1624 | * In case packet_seq is larger than mdev->peer_seq number, there are |
| 1625 | * outstanding packets on the msock. We wait for them to arrive. |
| 1626 | * In case we are the logically next packet, we update mdev->peer_seq |
| 1627 | * ourselves. Correctly handles 32bit wrap around. |
| 1628 | * |
| 1629 | * Assume we have a 10 GBit connection, that is about 1<<30 byte per second, |
| 1630 | * about 1<<21 sectors per second. So "worst" case, we have 1<<3 == 8 seconds |
| 1631 | * for the 24bit wrap (historical atomic_t guarantee on some archs), and we have |
| 1632 | * 1<<9 == 512 seconds aka ages for the 32bit wrap around... |
| 1633 | * |
| 1634 | * returns 0 if we may process the packet, |
| 1635 | * -ERESTARTSYS if we were interrupted (by disconnect signal). */ |
| 1636 | static int drbd_wait_peer_seq(struct drbd_conf *mdev, const u32 packet_seq) |
| 1637 | { |
| 1638 | DEFINE_WAIT(wait); |
| 1639 | unsigned int p_seq; |
| 1640 | long timeout; |
| 1641 | int ret = 0; |
| 1642 | spin_lock(&mdev->peer_seq_lock); |
| 1643 | for (;;) { |
| 1644 | prepare_to_wait(&mdev->seq_wait, &wait, TASK_INTERRUPTIBLE); |
| 1645 | if (seq_le(packet_seq, mdev->peer_seq+1)) |
| 1646 | break; |
| 1647 | if (signal_pending(current)) { |
| 1648 | ret = -ERESTARTSYS; |
| 1649 | break; |
| 1650 | } |
| 1651 | p_seq = mdev->peer_seq; |
| 1652 | spin_unlock(&mdev->peer_seq_lock); |
| 1653 | timeout = schedule_timeout(30*HZ); |
| 1654 | spin_lock(&mdev->peer_seq_lock); |
| 1655 | if (timeout == 0 && p_seq == mdev->peer_seq) { |
| 1656 | ret = -ETIMEDOUT; |
| 1657 | dev_err(DEV, "ASSERT FAILED waited 30 seconds for sequence update, forcing reconnect\n"); |
| 1658 | break; |
| 1659 | } |
| 1660 | } |
| 1661 | finish_wait(&mdev->seq_wait, &wait); |
| 1662 | if (mdev->peer_seq+1 == packet_seq) |
| 1663 | mdev->peer_seq++; |
| 1664 | spin_unlock(&mdev->peer_seq_lock); |
| 1665 | return ret; |
| 1666 | } |
| 1667 | |
Lars Ellenberg | 688593c | 2010-11-17 22:25:03 +0100 | [diff] [blame] | 1668 | /* see also bio_flags_to_wire() |
| 1669 | * DRBD_REQ_*, because we need to semantically map the flags to data packet |
| 1670 | * flags and back. We may replicate to other kernel versions. */ |
| 1671 | static unsigned long wire_flags_to_bio(struct drbd_conf *mdev, u32 dpf) |
Philipp Reisner | 76d2e7e | 2010-08-25 11:58:05 +0200 | [diff] [blame] | 1672 | { |
Lars Ellenberg | 688593c | 2010-11-17 22:25:03 +0100 | [diff] [blame] | 1673 | return (dpf & DP_RW_SYNC ? REQ_SYNC : 0) | |
| 1674 | (dpf & DP_FUA ? REQ_FUA : 0) | |
| 1675 | (dpf & DP_FLUSH ? REQ_FLUSH : 0) | |
| 1676 | (dpf & DP_DISCARD ? REQ_DISCARD : 0); |
Philipp Reisner | 76d2e7e | 2010-08-25 11:58:05 +0200 | [diff] [blame] | 1677 | } |
| 1678 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1679 | /* mirrored write */ |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 1680 | static int receive_Data(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1681 | { |
| 1682 | sector_t sector; |
| 1683 | struct drbd_epoch_entry *e; |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 1684 | struct p_data *p = &mdev->data.rbuf.data; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1685 | int rw = WRITE; |
| 1686 | u32 dp_flags; |
| 1687 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1688 | if (!get_ldev(mdev)) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1689 | spin_lock(&mdev->peer_seq_lock); |
| 1690 | if (mdev->peer_seq+1 == be32_to_cpu(p->seq_num)) |
| 1691 | mdev->peer_seq++; |
| 1692 | spin_unlock(&mdev->peer_seq_lock); |
| 1693 | |
Lars Ellenberg | 2b2bf21 | 2010-10-06 11:46:55 +0200 | [diff] [blame] | 1694 | drbd_send_ack_dp(mdev, P_NEG_ACK, p, data_size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1695 | atomic_inc(&mdev->current_epoch->epoch_size); |
| 1696 | return drbd_drain_block(mdev, data_size); |
| 1697 | } |
| 1698 | |
| 1699 | /* get_ldev(mdev) successful. |
| 1700 | * Corresponding put_ldev done either below (on various errors), |
Andreas Gruenbacher | 9c50842 | 2011-01-14 21:19:36 +0100 | [diff] [blame] | 1701 | * or in drbd_endio_sec, if we successfully submit the data at |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1702 | * the end of this function. */ |
| 1703 | |
| 1704 | sector = be64_to_cpu(p->sector); |
| 1705 | e = read_in_block(mdev, p->block_id, sector, data_size); |
| 1706 | if (!e) { |
| 1707 | put_ldev(mdev); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 1708 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1709 | } |
| 1710 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1711 | e->w.cb = e_end_block; |
| 1712 | |
Lars Ellenberg | 688593c | 2010-11-17 22:25:03 +0100 | [diff] [blame] | 1713 | dp_flags = be32_to_cpu(p->dp_flags); |
| 1714 | rw |= wire_flags_to_bio(mdev, dp_flags); |
| 1715 | |
| 1716 | if (dp_flags & DP_MAY_SET_IN_SYNC) |
| 1717 | e->flags |= EE_MAY_SET_IN_SYNC; |
| 1718 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1719 | spin_lock(&mdev->epoch_lock); |
| 1720 | e->epoch = mdev->current_epoch; |
| 1721 | atomic_inc(&e->epoch->epoch_size); |
| 1722 | atomic_inc(&e->epoch->active); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1723 | spin_unlock(&mdev->epoch_lock); |
| 1724 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1725 | /* I'm the receiver, I do hold a net_cnt reference. */ |
| 1726 | if (!mdev->net_conf->two_primaries) { |
| 1727 | spin_lock_irq(&mdev->req_lock); |
| 1728 | } else { |
| 1729 | /* don't get the req_lock yet, |
| 1730 | * we may sleep in drbd_wait_peer_seq */ |
Andreas Gruenbacher | 010f6e6 | 2011-01-14 20:59:35 +0100 | [diff] [blame] | 1731 | const int size = e->i.size; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1732 | const int discard = test_bit(DISCARD_CONCURRENT, &mdev->flags); |
| 1733 | DEFINE_WAIT(wait); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1734 | int first; |
| 1735 | |
| 1736 | D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_C); |
| 1737 | BUG_ON(mdev->ee_hash == NULL); |
| 1738 | BUG_ON(mdev->tl_hash == NULL); |
| 1739 | |
| 1740 | /* conflict detection and handling: |
| 1741 | * 1. wait on the sequence number, |
| 1742 | * in case this data packet overtook ACK packets. |
| 1743 | * 2. check our hash tables for conflicting requests. |
| 1744 | * we only need to walk the tl_hash, since an ee can not |
| 1745 | * have a conflict with an other ee: on the submitting |
| 1746 | * node, the corresponding req had already been conflicting, |
| 1747 | * and a conflicting req is never sent. |
| 1748 | * |
| 1749 | * Note: for two_primaries, we are protocol C, |
| 1750 | * so there cannot be any request that is DONE |
| 1751 | * but still on the transfer log. |
| 1752 | * |
| 1753 | * unconditionally add to the ee_hash. |
| 1754 | * |
| 1755 | * if no conflicting request is found: |
| 1756 | * submit. |
| 1757 | * |
| 1758 | * if any conflicting request is found |
| 1759 | * that has not yet been acked, |
| 1760 | * AND I have the "discard concurrent writes" flag: |
| 1761 | * queue (via done_ee) the P_DISCARD_ACK; OUT. |
| 1762 | * |
| 1763 | * if any conflicting request is found: |
| 1764 | * block the receiver, waiting on misc_wait |
| 1765 | * until no more conflicting requests are there, |
| 1766 | * or we get interrupted (disconnect). |
| 1767 | * |
| 1768 | * we do not just write after local io completion of those |
| 1769 | * requests, but only after req is done completely, i.e. |
| 1770 | * we wait for the P_DISCARD_ACK to arrive! |
| 1771 | * |
| 1772 | * then proceed normally, i.e. submit. |
| 1773 | */ |
| 1774 | if (drbd_wait_peer_seq(mdev, be32_to_cpu(p->seq_num))) |
| 1775 | goto out_interrupted; |
| 1776 | |
| 1777 | spin_lock_irq(&mdev->req_lock); |
| 1778 | |
Bart Van Assche | 24c4830 | 2011-05-21 18:32:29 +0200 | [diff] [blame] | 1779 | hlist_add_head(&e->collision, ee_hash_slot(mdev, sector)); |
Andreas Gruenbacher | 8b94625 | 2011-01-20 15:23:07 +0100 | [diff] [blame^] | 1780 | drbd_insert_interval(&mdev->epoch_entries, &e->i); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1781 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1782 | first = 1; |
| 1783 | for (;;) { |
Andreas Gruenbacher | de69671 | 2011-01-20 15:00:24 +0100 | [diff] [blame] | 1784 | struct drbd_interval *i; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1785 | int have_unacked = 0; |
| 1786 | int have_conflict = 0; |
| 1787 | prepare_to_wait(&mdev->misc_wait, &wait, |
| 1788 | TASK_INTERRUPTIBLE); |
Andreas Gruenbacher | de69671 | 2011-01-20 15:00:24 +0100 | [diff] [blame] | 1789 | |
| 1790 | i = drbd_find_overlap(&mdev->write_requests, sector, size); |
| 1791 | if (i) { |
| 1792 | struct drbd_request *req2 = |
| 1793 | container_of(i, struct drbd_request, i); |
| 1794 | |
| 1795 | /* only ALERT on first iteration, |
| 1796 | * we may be woken up early... */ |
| 1797 | if (first) |
| 1798 | dev_alert(DEV, "%s[%u] Concurrent local write detected!" |
| 1799 | " new: %llus +%u; pending: %llus +%u\n", |
| 1800 | current->comm, current->pid, |
| 1801 | (unsigned long long)sector, size, |
| 1802 | (unsigned long long)req2->i.sector, req2->i.size); |
| 1803 | if (req2->rq_state & RQ_NET_PENDING) |
| 1804 | ++have_unacked; |
| 1805 | ++have_conflict; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1806 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1807 | if (!have_conflict) |
| 1808 | break; |
| 1809 | |
| 1810 | /* Discard Ack only for the _first_ iteration */ |
| 1811 | if (first && discard && have_unacked) { |
| 1812 | dev_alert(DEV, "Concurrent write! [DISCARD BY FLAG] sec=%llus\n", |
| 1813 | (unsigned long long)sector); |
| 1814 | inc_unacked(mdev); |
| 1815 | e->w.cb = e_send_discard_ack; |
| 1816 | list_add_tail(&e->w.list, &mdev->done_ee); |
| 1817 | |
| 1818 | spin_unlock_irq(&mdev->req_lock); |
| 1819 | |
| 1820 | /* we could probably send that P_DISCARD_ACK ourselves, |
| 1821 | * but I don't like the receiver using the msock */ |
| 1822 | |
| 1823 | put_ldev(mdev); |
| 1824 | wake_asender(mdev); |
| 1825 | finish_wait(&mdev->misc_wait, &wait); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 1826 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1827 | } |
| 1828 | |
| 1829 | if (signal_pending(current)) { |
Bart Van Assche | 24c4830 | 2011-05-21 18:32:29 +0200 | [diff] [blame] | 1830 | hlist_del_init(&e->collision); |
Andreas Gruenbacher | 8b94625 | 2011-01-20 15:23:07 +0100 | [diff] [blame^] | 1831 | drbd_remove_interval(&mdev->epoch_entries, &e->i); |
| 1832 | drbd_clear_interval(&e->i); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1833 | |
| 1834 | spin_unlock_irq(&mdev->req_lock); |
| 1835 | |
| 1836 | finish_wait(&mdev->misc_wait, &wait); |
| 1837 | goto out_interrupted; |
| 1838 | } |
| 1839 | |
| 1840 | spin_unlock_irq(&mdev->req_lock); |
| 1841 | if (first) { |
| 1842 | first = 0; |
| 1843 | dev_alert(DEV, "Concurrent write! [W AFTERWARDS] " |
| 1844 | "sec=%llus\n", (unsigned long long)sector); |
| 1845 | } else if (discard) { |
| 1846 | /* we had none on the first iteration. |
| 1847 | * there must be none now. */ |
| 1848 | D_ASSERT(have_unacked == 0); |
| 1849 | } |
| 1850 | schedule(); |
| 1851 | spin_lock_irq(&mdev->req_lock); |
| 1852 | } |
| 1853 | finish_wait(&mdev->misc_wait, &wait); |
| 1854 | } |
| 1855 | |
| 1856 | list_add(&e->w.list, &mdev->active_ee); |
| 1857 | spin_unlock_irq(&mdev->req_lock); |
| 1858 | |
| 1859 | switch (mdev->net_conf->wire_protocol) { |
| 1860 | case DRBD_PROT_C: |
| 1861 | inc_unacked(mdev); |
| 1862 | /* corresponding dec_unacked() in e_end_block() |
| 1863 | * respective _drbd_clear_done_ee */ |
| 1864 | break; |
| 1865 | case DRBD_PROT_B: |
| 1866 | /* I really don't like it that the receiver thread |
| 1867 | * sends on the msock, but anyways */ |
| 1868 | drbd_send_ack(mdev, P_RECV_ACK, e); |
| 1869 | break; |
| 1870 | case DRBD_PROT_A: |
| 1871 | /* nothing to do */ |
| 1872 | break; |
| 1873 | } |
| 1874 | |
Lars Ellenberg | 6719fb0 | 2010-10-18 23:04:07 +0200 | [diff] [blame] | 1875 | if (mdev->state.pdsk < D_INCONSISTENT) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1876 | /* In case we have the only disk of the cluster, */ |
Andreas Gruenbacher | 010f6e6 | 2011-01-14 20:59:35 +0100 | [diff] [blame] | 1877 | drbd_set_out_of_sync(mdev, e->i.sector, e->i.size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1878 | e->flags |= EE_CALL_AL_COMPLETE_IO; |
Lars Ellenberg | 6719fb0 | 2010-10-18 23:04:07 +0200 | [diff] [blame] | 1879 | e->flags &= ~EE_MAY_SET_IN_SYNC; |
Andreas Gruenbacher | 010f6e6 | 2011-01-14 20:59:35 +0100 | [diff] [blame] | 1880 | drbd_al_begin_io(mdev, e->i.sector); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1881 | } |
| 1882 | |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1883 | if (drbd_submit_ee(mdev, e, rw, DRBD_FAULT_DT_WR) == 0) |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 1884 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1885 | |
Lars Ellenberg | 10f6d992 | 2011-01-24 14:47:09 +0100 | [diff] [blame] | 1886 | /* don't care for the reason here */ |
| 1887 | dev_err(DEV, "submit failed, triggering re-connect\n"); |
Lars Ellenberg | 22cc37a | 2010-09-14 20:40:41 +0200 | [diff] [blame] | 1888 | spin_lock_irq(&mdev->req_lock); |
| 1889 | list_del(&e->w.list); |
Bart Van Assche | 24c4830 | 2011-05-21 18:32:29 +0200 | [diff] [blame] | 1890 | hlist_del_init(&e->collision); |
Andreas Gruenbacher | 8b94625 | 2011-01-20 15:23:07 +0100 | [diff] [blame^] | 1891 | drbd_remove_interval(&mdev->epoch_entries, &e->i); |
| 1892 | drbd_clear_interval(&e->i); |
Lars Ellenberg | 22cc37a | 2010-09-14 20:40:41 +0200 | [diff] [blame] | 1893 | spin_unlock_irq(&mdev->req_lock); |
| 1894 | if (e->flags & EE_CALL_AL_COMPLETE_IO) |
Andreas Gruenbacher | 010f6e6 | 2011-01-14 20:59:35 +0100 | [diff] [blame] | 1895 | drbd_al_complete_io(mdev, e->i.sector); |
Lars Ellenberg | 22cc37a | 2010-09-14 20:40:41 +0200 | [diff] [blame] | 1896 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1897 | out_interrupted: |
Lars Ellenberg | 10f6d992 | 2011-01-24 14:47:09 +0100 | [diff] [blame] | 1898 | drbd_may_finish_epoch(mdev, e->epoch, EV_PUT + EV_CLEANUP); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1899 | put_ldev(mdev); |
| 1900 | drbd_free_ee(mdev, e); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 1901 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1902 | } |
| 1903 | |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 1904 | /* We may throttle resync, if the lower device seems to be busy, |
| 1905 | * and current sync rate is above c_min_rate. |
| 1906 | * |
| 1907 | * To decide whether or not the lower device is busy, we use a scheme similar |
| 1908 | * to MD RAID is_mddev_idle(): if the partition stats reveal "significant" |
| 1909 | * (more than 64 sectors) of activity we cannot account for with our own resync |
| 1910 | * activity, it obviously is "busy". |
| 1911 | * |
| 1912 | * The current sync rate used here uses only the most recent two step marks, |
| 1913 | * to have a short time average so we can react faster. |
| 1914 | */ |
Philipp Reisner | e3555d8 | 2010-11-07 15:56:29 +0100 | [diff] [blame] | 1915 | int drbd_rs_should_slow_down(struct drbd_conf *mdev, sector_t sector) |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 1916 | { |
| 1917 | struct gendisk *disk = mdev->ldev->backing_bdev->bd_contains->bd_disk; |
| 1918 | unsigned long db, dt, dbdt; |
Philipp Reisner | e3555d8 | 2010-11-07 15:56:29 +0100 | [diff] [blame] | 1919 | struct lc_element *tmp; |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 1920 | int curr_events; |
| 1921 | int throttle = 0; |
| 1922 | |
| 1923 | /* feature disabled? */ |
| 1924 | if (mdev->sync_conf.c_min_rate == 0) |
| 1925 | return 0; |
| 1926 | |
Philipp Reisner | e3555d8 | 2010-11-07 15:56:29 +0100 | [diff] [blame] | 1927 | spin_lock_irq(&mdev->al_lock); |
| 1928 | tmp = lc_find(mdev->resync, BM_SECT_TO_EXT(sector)); |
| 1929 | if (tmp) { |
| 1930 | struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce); |
| 1931 | if (test_bit(BME_PRIORITY, &bm_ext->flags)) { |
| 1932 | spin_unlock_irq(&mdev->al_lock); |
| 1933 | return 0; |
| 1934 | } |
| 1935 | /* Do not slow down if app IO is already waiting for this extent */ |
| 1936 | } |
| 1937 | spin_unlock_irq(&mdev->al_lock); |
| 1938 | |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 1939 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 1940 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 1941 | atomic_read(&mdev->rs_sect_ev); |
Philipp Reisner | e3555d8 | 2010-11-07 15:56:29 +0100 | [diff] [blame] | 1942 | |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 1943 | if (!mdev->rs_last_events || curr_events - mdev->rs_last_events > 64) { |
| 1944 | unsigned long rs_left; |
| 1945 | int i; |
| 1946 | |
| 1947 | mdev->rs_last_events = curr_events; |
| 1948 | |
| 1949 | /* sync speed average over the last 2*DRBD_SYNC_MARK_STEP, |
| 1950 | * approx. */ |
Lars Ellenberg | 2649f08 | 2010-11-05 10:05:47 +0100 | [diff] [blame] | 1951 | i = (mdev->rs_last_mark + DRBD_SYNC_MARKS-1) % DRBD_SYNC_MARKS; |
| 1952 | |
| 1953 | if (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T) |
| 1954 | rs_left = mdev->ov_left; |
| 1955 | else |
| 1956 | rs_left = drbd_bm_total_weight(mdev) - mdev->rs_failed; |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 1957 | |
| 1958 | dt = ((long)jiffies - (long)mdev->rs_mark_time[i]) / HZ; |
| 1959 | if (!dt) |
| 1960 | dt++; |
| 1961 | db = mdev->rs_mark_left[i] - rs_left; |
| 1962 | dbdt = Bit2KB(db/dt); |
| 1963 | |
| 1964 | if (dbdt > mdev->sync_conf.c_min_rate) |
| 1965 | throttle = 1; |
| 1966 | } |
| 1967 | return throttle; |
| 1968 | } |
| 1969 | |
| 1970 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 1971 | static int receive_DataRequest(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int digest_size) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1972 | { |
| 1973 | sector_t sector; |
| 1974 | const sector_t capacity = drbd_get_capacity(mdev->this_bdev); |
| 1975 | struct drbd_epoch_entry *e; |
| 1976 | struct digest_info *di = NULL; |
Philipp Reisner | b18b37b | 2010-10-13 15:32:44 +0200 | [diff] [blame] | 1977 | int size, verb; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1978 | unsigned int fault_type; |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 1979 | struct p_block_req *p = &mdev->data.rbuf.block_req; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1980 | |
| 1981 | sector = be64_to_cpu(p->sector); |
| 1982 | size = be32_to_cpu(p->blksize); |
| 1983 | |
Lars Ellenberg | 1816a2b | 2010-11-11 15:19:07 +0100 | [diff] [blame] | 1984 | if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1985 | dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__, |
| 1986 | (unsigned long long)sector, size); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 1987 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1988 | } |
| 1989 | if (sector + (size>>9) > capacity) { |
| 1990 | dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__, |
| 1991 | (unsigned long long)sector, size); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 1992 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1993 | } |
| 1994 | |
| 1995 | if (!get_ldev_if_state(mdev, D_UP_TO_DATE)) { |
Philipp Reisner | b18b37b | 2010-10-13 15:32:44 +0200 | [diff] [blame] | 1996 | verb = 1; |
| 1997 | switch (cmd) { |
| 1998 | case P_DATA_REQUEST: |
| 1999 | drbd_send_ack_rp(mdev, P_NEG_DREPLY, p); |
| 2000 | break; |
| 2001 | case P_RS_DATA_REQUEST: |
| 2002 | case P_CSUM_RS_REQUEST: |
| 2003 | case P_OV_REQUEST: |
| 2004 | drbd_send_ack_rp(mdev, P_NEG_RS_DREPLY , p); |
| 2005 | break; |
| 2006 | case P_OV_REPLY: |
| 2007 | verb = 0; |
| 2008 | dec_rs_pending(mdev); |
| 2009 | drbd_send_ack_ex(mdev, P_OV_RESULT, sector, size, ID_IN_SYNC); |
| 2010 | break; |
| 2011 | default: |
| 2012 | dev_err(DEV, "unexpected command (%s) in receive_DataRequest\n", |
| 2013 | cmdname(cmd)); |
| 2014 | } |
| 2015 | if (verb && __ratelimit(&drbd_ratelimit_state)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2016 | dev_err(DEV, "Can not satisfy peer's read request, " |
| 2017 | "no local data.\n"); |
Philipp Reisner | b18b37b | 2010-10-13 15:32:44 +0200 | [diff] [blame] | 2018 | |
Lars Ellenberg | a821cc4 | 2010-09-06 12:31:37 +0200 | [diff] [blame] | 2019 | /* drain possibly payload */ |
| 2020 | return drbd_drain_block(mdev, digest_size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2021 | } |
| 2022 | |
| 2023 | /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD |
| 2024 | * "criss-cross" setup, that might cause write-out on some other DRBD, |
| 2025 | * which in turn might block on the other node at this very place. */ |
| 2026 | e = drbd_alloc_ee(mdev, p->block_id, sector, size, GFP_NOIO); |
| 2027 | if (!e) { |
| 2028 | put_ldev(mdev); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 2029 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2030 | } |
| 2031 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 2032 | switch (cmd) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2033 | case P_DATA_REQUEST: |
| 2034 | e->w.cb = w_e_end_data_req; |
| 2035 | fault_type = DRBD_FAULT_DT_RD; |
Lars Ellenberg | 80a40e4 | 2010-08-11 23:28:00 +0200 | [diff] [blame] | 2036 | /* application IO, don't drbd_rs_begin_io */ |
| 2037 | goto submit; |
| 2038 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2039 | case P_RS_DATA_REQUEST: |
| 2040 | e->w.cb = w_e_end_rsdata_req; |
| 2041 | fault_type = DRBD_FAULT_RS_RD; |
Lars Ellenberg | 5f9915b | 2010-11-09 14:15:24 +0100 | [diff] [blame] | 2042 | /* used in the sector offset progress display */ |
| 2043 | mdev->bm_resync_fo = BM_SECT_TO_BIT(sector); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2044 | break; |
| 2045 | |
| 2046 | case P_OV_REPLY: |
| 2047 | case P_CSUM_RS_REQUEST: |
| 2048 | fault_type = DRBD_FAULT_RS_RD; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2049 | di = kmalloc(sizeof(*di) + digest_size, GFP_NOIO); |
| 2050 | if (!di) |
| 2051 | goto out_free_e; |
| 2052 | |
| 2053 | di->digest_size = digest_size; |
| 2054 | di->digest = (((char *)di)+sizeof(struct digest_info)); |
| 2055 | |
Lars Ellenberg | c36c3ce | 2010-08-11 20:42:55 +0200 | [diff] [blame] | 2056 | e->digest = di; |
| 2057 | e->flags |= EE_HAS_DIGEST; |
| 2058 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2059 | if (drbd_recv(mdev, di->digest, digest_size) != digest_size) |
| 2060 | goto out_free_e; |
| 2061 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 2062 | if (cmd == P_CSUM_RS_REQUEST) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2063 | D_ASSERT(mdev->agreed_pro_version >= 89); |
| 2064 | e->w.cb = w_e_end_csum_rs_req; |
Lars Ellenberg | 5f9915b | 2010-11-09 14:15:24 +0100 | [diff] [blame] | 2065 | /* used in the sector offset progress display */ |
| 2066 | mdev->bm_resync_fo = BM_SECT_TO_BIT(sector); |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 2067 | } else if (cmd == P_OV_REPLY) { |
Lars Ellenberg | 2649f08 | 2010-11-05 10:05:47 +0100 | [diff] [blame] | 2068 | /* track progress, we may need to throttle */ |
| 2069 | atomic_add(size >> 9, &mdev->rs_sect_in); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2070 | e->w.cb = w_e_end_ov_reply; |
| 2071 | dec_rs_pending(mdev); |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 2072 | /* drbd_rs_begin_io done when we sent this request, |
| 2073 | * but accounting still needs to be done. */ |
| 2074 | goto submit_for_resync; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2075 | } |
| 2076 | break; |
| 2077 | |
| 2078 | case P_OV_REQUEST: |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2079 | if (mdev->ov_start_sector == ~(sector_t)0 && |
| 2080 | mdev->agreed_pro_version >= 90) { |
Lars Ellenberg | de228bb | 2010-11-05 09:43:15 +0100 | [diff] [blame] | 2081 | unsigned long now = jiffies; |
| 2082 | int i; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2083 | mdev->ov_start_sector = sector; |
| 2084 | mdev->ov_position = sector; |
Lars Ellenberg | 30b743a | 2010-11-05 09:39:06 +0100 | [diff] [blame] | 2085 | mdev->ov_left = drbd_bm_bits(mdev) - BM_SECT_TO_BIT(sector); |
| 2086 | mdev->rs_total = mdev->ov_left; |
Lars Ellenberg | de228bb | 2010-11-05 09:43:15 +0100 | [diff] [blame] | 2087 | for (i = 0; i < DRBD_SYNC_MARKS; i++) { |
| 2088 | mdev->rs_mark_left[i] = mdev->ov_left; |
| 2089 | mdev->rs_mark_time[i] = now; |
| 2090 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2091 | dev_info(DEV, "Online Verify start sector: %llu\n", |
| 2092 | (unsigned long long)sector); |
| 2093 | } |
| 2094 | e->w.cb = w_e_end_ov_req; |
| 2095 | fault_type = DRBD_FAULT_RS_RD; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2096 | break; |
| 2097 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2098 | default: |
| 2099 | dev_err(DEV, "unexpected command (%s) in receive_DataRequest\n", |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 2100 | cmdname(cmd)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2101 | fault_type = DRBD_FAULT_MAX; |
Lars Ellenberg | 80a40e4 | 2010-08-11 23:28:00 +0200 | [diff] [blame] | 2102 | goto out_free_e; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2103 | } |
| 2104 | |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 2105 | /* Throttle, drbd_rs_begin_io and submit should become asynchronous |
| 2106 | * wrt the receiver, but it is not as straightforward as it may seem. |
| 2107 | * Various places in the resync start and stop logic assume resync |
| 2108 | * requests are processed in order, requeuing this on the worker thread |
| 2109 | * introduces a bunch of new code for synchronization between threads. |
| 2110 | * |
| 2111 | * Unlimited throttling before drbd_rs_begin_io may stall the resync |
| 2112 | * "forever", throttling after drbd_rs_begin_io will lock that extent |
| 2113 | * for application writes for the same time. For now, just throttle |
| 2114 | * here, where the rest of the code expects the receiver to sleep for |
| 2115 | * a while, anyways. |
| 2116 | */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2117 | |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 2118 | /* Throttle before drbd_rs_begin_io, as that locks out application IO; |
| 2119 | * this defers syncer requests for some time, before letting at least |
| 2120 | * on request through. The resync controller on the receiving side |
| 2121 | * will adapt to the incoming rate accordingly. |
| 2122 | * |
| 2123 | * We cannot throttle here if remote is Primary/SyncTarget: |
| 2124 | * we would also throttle its application reads. |
| 2125 | * In that case, throttling is done on the SyncTarget only. |
| 2126 | */ |
Philipp Reisner | e3555d8 | 2010-11-07 15:56:29 +0100 | [diff] [blame] | 2127 | if (mdev->state.peer != R_PRIMARY && drbd_rs_should_slow_down(mdev, sector)) |
| 2128 | schedule_timeout_uninterruptible(HZ/10); |
| 2129 | if (drbd_rs_begin_io(mdev, sector)) |
Lars Ellenberg | 80a40e4 | 2010-08-11 23:28:00 +0200 | [diff] [blame] | 2130 | goto out_free_e; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2131 | |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 2132 | submit_for_resync: |
| 2133 | atomic_add(size >> 9, &mdev->rs_sect_ev); |
| 2134 | |
Lars Ellenberg | 80a40e4 | 2010-08-11 23:28:00 +0200 | [diff] [blame] | 2135 | submit: |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2136 | inc_unacked(mdev); |
Lars Ellenberg | 80a40e4 | 2010-08-11 23:28:00 +0200 | [diff] [blame] | 2137 | spin_lock_irq(&mdev->req_lock); |
| 2138 | list_add_tail(&e->w.list, &mdev->read_ee); |
| 2139 | spin_unlock_irq(&mdev->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2140 | |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 2141 | if (drbd_submit_ee(mdev, e, READ, fault_type) == 0) |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 2142 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2143 | |
Lars Ellenberg | 10f6d992 | 2011-01-24 14:47:09 +0100 | [diff] [blame] | 2144 | /* don't care for the reason here */ |
| 2145 | dev_err(DEV, "submit failed, triggering re-connect\n"); |
Lars Ellenberg | 22cc37a | 2010-09-14 20:40:41 +0200 | [diff] [blame] | 2146 | spin_lock_irq(&mdev->req_lock); |
| 2147 | list_del(&e->w.list); |
| 2148 | spin_unlock_irq(&mdev->req_lock); |
| 2149 | /* no drbd_rs_complete_io(), we are dropping the connection anyways */ |
| 2150 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2151 | out_free_e: |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2152 | put_ldev(mdev); |
| 2153 | drbd_free_ee(mdev, e); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 2154 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2155 | } |
| 2156 | |
| 2157 | static int drbd_asb_recover_0p(struct drbd_conf *mdev) __must_hold(local) |
| 2158 | { |
| 2159 | int self, peer, rv = -100; |
| 2160 | unsigned long ch_self, ch_peer; |
| 2161 | |
| 2162 | self = mdev->ldev->md.uuid[UI_BITMAP] & 1; |
| 2163 | peer = mdev->p_uuid[UI_BITMAP] & 1; |
| 2164 | |
| 2165 | ch_peer = mdev->p_uuid[UI_SIZE]; |
| 2166 | ch_self = mdev->comm_bm_set; |
| 2167 | |
| 2168 | switch (mdev->net_conf->after_sb_0p) { |
| 2169 | case ASB_CONSENSUS: |
| 2170 | case ASB_DISCARD_SECONDARY: |
| 2171 | case ASB_CALL_HELPER: |
| 2172 | dev_err(DEV, "Configuration error.\n"); |
| 2173 | break; |
| 2174 | case ASB_DISCONNECT: |
| 2175 | break; |
| 2176 | case ASB_DISCARD_YOUNGER_PRI: |
| 2177 | if (self == 0 && peer == 1) { |
| 2178 | rv = -1; |
| 2179 | break; |
| 2180 | } |
| 2181 | if (self == 1 && peer == 0) { |
| 2182 | rv = 1; |
| 2183 | break; |
| 2184 | } |
| 2185 | /* Else fall through to one of the other strategies... */ |
| 2186 | case ASB_DISCARD_OLDER_PRI: |
| 2187 | if (self == 0 && peer == 1) { |
| 2188 | rv = 1; |
| 2189 | break; |
| 2190 | } |
| 2191 | if (self == 1 && peer == 0) { |
| 2192 | rv = -1; |
| 2193 | break; |
| 2194 | } |
| 2195 | /* Else fall through to one of the other strategies... */ |
Lars Ellenberg | ad19bf6 | 2009-10-14 09:36:49 +0200 | [diff] [blame] | 2196 | dev_warn(DEV, "Discard younger/older primary did not find a decision\n" |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2197 | "Using discard-least-changes instead\n"); |
| 2198 | case ASB_DISCARD_ZERO_CHG: |
| 2199 | if (ch_peer == 0 && ch_self == 0) { |
| 2200 | rv = test_bit(DISCARD_CONCURRENT, &mdev->flags) |
| 2201 | ? -1 : 1; |
| 2202 | break; |
| 2203 | } else { |
| 2204 | if (ch_peer == 0) { rv = 1; break; } |
| 2205 | if (ch_self == 0) { rv = -1; break; } |
| 2206 | } |
| 2207 | if (mdev->net_conf->after_sb_0p == ASB_DISCARD_ZERO_CHG) |
| 2208 | break; |
| 2209 | case ASB_DISCARD_LEAST_CHG: |
| 2210 | if (ch_self < ch_peer) |
| 2211 | rv = -1; |
| 2212 | else if (ch_self > ch_peer) |
| 2213 | rv = 1; |
| 2214 | else /* ( ch_self == ch_peer ) */ |
| 2215 | /* Well, then use something else. */ |
| 2216 | rv = test_bit(DISCARD_CONCURRENT, &mdev->flags) |
| 2217 | ? -1 : 1; |
| 2218 | break; |
| 2219 | case ASB_DISCARD_LOCAL: |
| 2220 | rv = -1; |
| 2221 | break; |
| 2222 | case ASB_DISCARD_REMOTE: |
| 2223 | rv = 1; |
| 2224 | } |
| 2225 | |
| 2226 | return rv; |
| 2227 | } |
| 2228 | |
| 2229 | static int drbd_asb_recover_1p(struct drbd_conf *mdev) __must_hold(local) |
| 2230 | { |
Andreas Gruenbacher | 6184ea2 | 2010-12-09 14:23:27 +0100 | [diff] [blame] | 2231 | int hg, rv = -100; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2232 | |
| 2233 | switch (mdev->net_conf->after_sb_1p) { |
| 2234 | case ASB_DISCARD_YOUNGER_PRI: |
| 2235 | case ASB_DISCARD_OLDER_PRI: |
| 2236 | case ASB_DISCARD_LEAST_CHG: |
| 2237 | case ASB_DISCARD_LOCAL: |
| 2238 | case ASB_DISCARD_REMOTE: |
| 2239 | dev_err(DEV, "Configuration error.\n"); |
| 2240 | break; |
| 2241 | case ASB_DISCONNECT: |
| 2242 | break; |
| 2243 | case ASB_CONSENSUS: |
| 2244 | hg = drbd_asb_recover_0p(mdev); |
| 2245 | if (hg == -1 && mdev->state.role == R_SECONDARY) |
| 2246 | rv = hg; |
| 2247 | if (hg == 1 && mdev->state.role == R_PRIMARY) |
| 2248 | rv = hg; |
| 2249 | break; |
| 2250 | case ASB_VIOLENTLY: |
| 2251 | rv = drbd_asb_recover_0p(mdev); |
| 2252 | break; |
| 2253 | case ASB_DISCARD_SECONDARY: |
| 2254 | return mdev->state.role == R_PRIMARY ? 1 : -1; |
| 2255 | case ASB_CALL_HELPER: |
| 2256 | hg = drbd_asb_recover_0p(mdev); |
| 2257 | if (hg == -1 && mdev->state.role == R_PRIMARY) { |
Andreas Gruenbacher | bb43794 | 2010-12-09 14:02:35 +0100 | [diff] [blame] | 2258 | enum drbd_state_rv rv2; |
| 2259 | |
| 2260 | drbd_set_role(mdev, R_SECONDARY, 0); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2261 | /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE, |
| 2262 | * we might be here in C_WF_REPORT_PARAMS which is transient. |
| 2263 | * we do not need to wait for the after state change work either. */ |
Andreas Gruenbacher | bb43794 | 2010-12-09 14:02:35 +0100 | [diff] [blame] | 2264 | rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY)); |
| 2265 | if (rv2 != SS_SUCCESS) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2266 | drbd_khelper(mdev, "pri-lost-after-sb"); |
| 2267 | } else { |
| 2268 | dev_warn(DEV, "Successfully gave up primary role.\n"); |
| 2269 | rv = hg; |
| 2270 | } |
| 2271 | } else |
| 2272 | rv = hg; |
| 2273 | } |
| 2274 | |
| 2275 | return rv; |
| 2276 | } |
| 2277 | |
| 2278 | static int drbd_asb_recover_2p(struct drbd_conf *mdev) __must_hold(local) |
| 2279 | { |
Andreas Gruenbacher | 6184ea2 | 2010-12-09 14:23:27 +0100 | [diff] [blame] | 2280 | int hg, rv = -100; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2281 | |
| 2282 | switch (mdev->net_conf->after_sb_2p) { |
| 2283 | case ASB_DISCARD_YOUNGER_PRI: |
| 2284 | case ASB_DISCARD_OLDER_PRI: |
| 2285 | case ASB_DISCARD_LEAST_CHG: |
| 2286 | case ASB_DISCARD_LOCAL: |
| 2287 | case ASB_DISCARD_REMOTE: |
| 2288 | case ASB_CONSENSUS: |
| 2289 | case ASB_DISCARD_SECONDARY: |
| 2290 | dev_err(DEV, "Configuration error.\n"); |
| 2291 | break; |
| 2292 | case ASB_VIOLENTLY: |
| 2293 | rv = drbd_asb_recover_0p(mdev); |
| 2294 | break; |
| 2295 | case ASB_DISCONNECT: |
| 2296 | break; |
| 2297 | case ASB_CALL_HELPER: |
| 2298 | hg = drbd_asb_recover_0p(mdev); |
| 2299 | if (hg == -1) { |
Andreas Gruenbacher | bb43794 | 2010-12-09 14:02:35 +0100 | [diff] [blame] | 2300 | enum drbd_state_rv rv2; |
| 2301 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2302 | /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE, |
| 2303 | * we might be here in C_WF_REPORT_PARAMS which is transient. |
| 2304 | * we do not need to wait for the after state change work either. */ |
Andreas Gruenbacher | bb43794 | 2010-12-09 14:02:35 +0100 | [diff] [blame] | 2305 | rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY)); |
| 2306 | if (rv2 != SS_SUCCESS) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2307 | drbd_khelper(mdev, "pri-lost-after-sb"); |
| 2308 | } else { |
| 2309 | dev_warn(DEV, "Successfully gave up primary role.\n"); |
| 2310 | rv = hg; |
| 2311 | } |
| 2312 | } else |
| 2313 | rv = hg; |
| 2314 | } |
| 2315 | |
| 2316 | return rv; |
| 2317 | } |
| 2318 | |
| 2319 | static void drbd_uuid_dump(struct drbd_conf *mdev, char *text, u64 *uuid, |
| 2320 | u64 bits, u64 flags) |
| 2321 | { |
| 2322 | if (!uuid) { |
| 2323 | dev_info(DEV, "%s uuid info vanished while I was looking!\n", text); |
| 2324 | return; |
| 2325 | } |
| 2326 | dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX bits:%llu flags:%llX\n", |
| 2327 | text, |
| 2328 | (unsigned long long)uuid[UI_CURRENT], |
| 2329 | (unsigned long long)uuid[UI_BITMAP], |
| 2330 | (unsigned long long)uuid[UI_HISTORY_START], |
| 2331 | (unsigned long long)uuid[UI_HISTORY_END], |
| 2332 | (unsigned long long)bits, |
| 2333 | (unsigned long long)flags); |
| 2334 | } |
| 2335 | |
| 2336 | /* |
| 2337 | 100 after split brain try auto recover |
| 2338 | 2 C_SYNC_SOURCE set BitMap |
| 2339 | 1 C_SYNC_SOURCE use BitMap |
| 2340 | 0 no Sync |
| 2341 | -1 C_SYNC_TARGET use BitMap |
| 2342 | -2 C_SYNC_TARGET set BitMap |
| 2343 | -100 after split brain, disconnect |
| 2344 | -1000 unrelated data |
Philipp Reisner | 4a23f26 | 2011-01-11 17:42:17 +0100 | [diff] [blame] | 2345 | -1091 requires proto 91 |
| 2346 | -1096 requires proto 96 |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2347 | */ |
| 2348 | static int drbd_uuid_compare(struct drbd_conf *mdev, int *rule_nr) __must_hold(local) |
| 2349 | { |
| 2350 | u64 self, peer; |
| 2351 | int i, j; |
| 2352 | |
| 2353 | self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1); |
| 2354 | peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1); |
| 2355 | |
| 2356 | *rule_nr = 10; |
| 2357 | if (self == UUID_JUST_CREATED && peer == UUID_JUST_CREATED) |
| 2358 | return 0; |
| 2359 | |
| 2360 | *rule_nr = 20; |
| 2361 | if ((self == UUID_JUST_CREATED || self == (u64)0) && |
| 2362 | peer != UUID_JUST_CREATED) |
| 2363 | return -2; |
| 2364 | |
| 2365 | *rule_nr = 30; |
| 2366 | if (self != UUID_JUST_CREATED && |
| 2367 | (peer == UUID_JUST_CREATED || peer == (u64)0)) |
| 2368 | return 2; |
| 2369 | |
| 2370 | if (self == peer) { |
| 2371 | int rct, dc; /* roles at crash time */ |
| 2372 | |
| 2373 | if (mdev->p_uuid[UI_BITMAP] == (u64)0 && mdev->ldev->md.uuid[UI_BITMAP] != (u64)0) { |
| 2374 | |
| 2375 | if (mdev->agreed_pro_version < 91) |
Philipp Reisner | 4a23f26 | 2011-01-11 17:42:17 +0100 | [diff] [blame] | 2376 | return -1091; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2377 | |
| 2378 | if ((mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) && |
| 2379 | (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1))) { |
| 2380 | dev_info(DEV, "was SyncSource, missed the resync finished event, corrected myself:\n"); |
| 2381 | drbd_uuid_set_bm(mdev, 0UL); |
| 2382 | |
| 2383 | drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid, |
| 2384 | mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0); |
| 2385 | *rule_nr = 34; |
| 2386 | } else { |
| 2387 | dev_info(DEV, "was SyncSource (peer failed to write sync_uuid)\n"); |
| 2388 | *rule_nr = 36; |
| 2389 | } |
| 2390 | |
| 2391 | return 1; |
| 2392 | } |
| 2393 | |
| 2394 | if (mdev->ldev->md.uuid[UI_BITMAP] == (u64)0 && mdev->p_uuid[UI_BITMAP] != (u64)0) { |
| 2395 | |
| 2396 | if (mdev->agreed_pro_version < 91) |
Philipp Reisner | 4a23f26 | 2011-01-11 17:42:17 +0100 | [diff] [blame] | 2397 | return -1091; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2398 | |
| 2399 | if ((mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_BITMAP] & ~((u64)1)) && |
| 2400 | (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1))) { |
| 2401 | dev_info(DEV, "was SyncTarget, peer missed the resync finished event, corrected peer:\n"); |
| 2402 | |
| 2403 | mdev->p_uuid[UI_HISTORY_START + 1] = mdev->p_uuid[UI_HISTORY_START]; |
| 2404 | mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_BITMAP]; |
| 2405 | mdev->p_uuid[UI_BITMAP] = 0UL; |
| 2406 | |
| 2407 | drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]); |
| 2408 | *rule_nr = 35; |
| 2409 | } else { |
| 2410 | dev_info(DEV, "was SyncTarget (failed to write sync_uuid)\n"); |
| 2411 | *rule_nr = 37; |
| 2412 | } |
| 2413 | |
| 2414 | return -1; |
| 2415 | } |
| 2416 | |
| 2417 | /* Common power [off|failure] */ |
| 2418 | rct = (test_bit(CRASHED_PRIMARY, &mdev->flags) ? 1 : 0) + |
| 2419 | (mdev->p_uuid[UI_FLAGS] & 2); |
| 2420 | /* lowest bit is set when we were primary, |
| 2421 | * next bit (weight 2) is set when peer was primary */ |
| 2422 | *rule_nr = 40; |
| 2423 | |
| 2424 | switch (rct) { |
| 2425 | case 0: /* !self_pri && !peer_pri */ return 0; |
| 2426 | case 1: /* self_pri && !peer_pri */ return 1; |
| 2427 | case 2: /* !self_pri && peer_pri */ return -1; |
| 2428 | case 3: /* self_pri && peer_pri */ |
| 2429 | dc = test_bit(DISCARD_CONCURRENT, &mdev->flags); |
| 2430 | return dc ? -1 : 1; |
| 2431 | } |
| 2432 | } |
| 2433 | |
| 2434 | *rule_nr = 50; |
| 2435 | peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1); |
| 2436 | if (self == peer) |
| 2437 | return -1; |
| 2438 | |
| 2439 | *rule_nr = 51; |
| 2440 | peer = mdev->p_uuid[UI_HISTORY_START] & ~((u64)1); |
| 2441 | if (self == peer) { |
Philipp Reisner | 4a23f26 | 2011-01-11 17:42:17 +0100 | [diff] [blame] | 2442 | if (mdev->agreed_pro_version < 96 ? |
| 2443 | (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == |
| 2444 | (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1)) : |
| 2445 | peer + UUID_NEW_BM_OFFSET == (mdev->p_uuid[UI_BITMAP] & ~((u64)1))) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2446 | /* The last P_SYNC_UUID did not get though. Undo the last start of |
| 2447 | resync as sync source modifications of the peer's UUIDs. */ |
| 2448 | |
| 2449 | if (mdev->agreed_pro_version < 91) |
Philipp Reisner | 4a23f26 | 2011-01-11 17:42:17 +0100 | [diff] [blame] | 2450 | return -1091; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2451 | |
| 2452 | mdev->p_uuid[UI_BITMAP] = mdev->p_uuid[UI_HISTORY_START]; |
| 2453 | mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_HISTORY_START + 1]; |
Philipp Reisner | 4a23f26 | 2011-01-11 17:42:17 +0100 | [diff] [blame] | 2454 | |
| 2455 | dev_info(DEV, "Did not got last syncUUID packet, corrected:\n"); |
| 2456 | drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]); |
| 2457 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2458 | return -1; |
| 2459 | } |
| 2460 | } |
| 2461 | |
| 2462 | *rule_nr = 60; |
| 2463 | self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1); |
| 2464 | for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) { |
| 2465 | peer = mdev->p_uuid[i] & ~((u64)1); |
| 2466 | if (self == peer) |
| 2467 | return -2; |
| 2468 | } |
| 2469 | |
| 2470 | *rule_nr = 70; |
| 2471 | self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1); |
| 2472 | peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1); |
| 2473 | if (self == peer) |
| 2474 | return 1; |
| 2475 | |
| 2476 | *rule_nr = 71; |
| 2477 | self = mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1); |
| 2478 | if (self == peer) { |
Philipp Reisner | 4a23f26 | 2011-01-11 17:42:17 +0100 | [diff] [blame] | 2479 | if (mdev->agreed_pro_version < 96 ? |
| 2480 | (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == |
| 2481 | (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) : |
| 2482 | self + UUID_NEW_BM_OFFSET == (mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1))) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2483 | /* The last P_SYNC_UUID did not get though. Undo the last start of |
| 2484 | resync as sync source modifications of our UUIDs. */ |
| 2485 | |
| 2486 | if (mdev->agreed_pro_version < 91) |
Philipp Reisner | 4a23f26 | 2011-01-11 17:42:17 +0100 | [diff] [blame] | 2487 | return -1091; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2488 | |
| 2489 | _drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_HISTORY_START]); |
| 2490 | _drbd_uuid_set(mdev, UI_HISTORY_START, mdev->ldev->md.uuid[UI_HISTORY_START + 1]); |
| 2491 | |
Philipp Reisner | 4a23f26 | 2011-01-11 17:42:17 +0100 | [diff] [blame] | 2492 | dev_info(DEV, "Last syncUUID did not get through, corrected:\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2493 | drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid, |
| 2494 | mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0); |
| 2495 | |
| 2496 | return 1; |
| 2497 | } |
| 2498 | } |
| 2499 | |
| 2500 | |
| 2501 | *rule_nr = 80; |
Philipp Reisner | d8c2a36 | 2009-11-18 15:52:51 +0100 | [diff] [blame] | 2502 | peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2503 | for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) { |
| 2504 | self = mdev->ldev->md.uuid[i] & ~((u64)1); |
| 2505 | if (self == peer) |
| 2506 | return 2; |
| 2507 | } |
| 2508 | |
| 2509 | *rule_nr = 90; |
| 2510 | self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1); |
| 2511 | peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1); |
| 2512 | if (self == peer && self != ((u64)0)) |
| 2513 | return 100; |
| 2514 | |
| 2515 | *rule_nr = 100; |
| 2516 | for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) { |
| 2517 | self = mdev->ldev->md.uuid[i] & ~((u64)1); |
| 2518 | for (j = UI_HISTORY_START; j <= UI_HISTORY_END; j++) { |
| 2519 | peer = mdev->p_uuid[j] & ~((u64)1); |
| 2520 | if (self == peer) |
| 2521 | return -100; |
| 2522 | } |
| 2523 | } |
| 2524 | |
| 2525 | return -1000; |
| 2526 | } |
| 2527 | |
| 2528 | /* drbd_sync_handshake() returns the new conn state on success, or |
| 2529 | CONN_MASK (-1) on failure. |
| 2530 | */ |
| 2531 | static enum drbd_conns drbd_sync_handshake(struct drbd_conf *mdev, enum drbd_role peer_role, |
| 2532 | enum drbd_disk_state peer_disk) __must_hold(local) |
| 2533 | { |
| 2534 | int hg, rule_nr; |
| 2535 | enum drbd_conns rv = C_MASK; |
| 2536 | enum drbd_disk_state mydisk; |
| 2537 | |
| 2538 | mydisk = mdev->state.disk; |
| 2539 | if (mydisk == D_NEGOTIATING) |
| 2540 | mydisk = mdev->new_state_tmp.disk; |
| 2541 | |
| 2542 | dev_info(DEV, "drbd_sync_handshake:\n"); |
| 2543 | drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid, mdev->comm_bm_set, 0); |
| 2544 | drbd_uuid_dump(mdev, "peer", mdev->p_uuid, |
| 2545 | mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]); |
| 2546 | |
| 2547 | hg = drbd_uuid_compare(mdev, &rule_nr); |
| 2548 | |
| 2549 | dev_info(DEV, "uuid_compare()=%d by rule %d\n", hg, rule_nr); |
| 2550 | |
| 2551 | if (hg == -1000) { |
| 2552 | dev_alert(DEV, "Unrelated data, aborting!\n"); |
| 2553 | return C_MASK; |
| 2554 | } |
Philipp Reisner | 4a23f26 | 2011-01-11 17:42:17 +0100 | [diff] [blame] | 2555 | if (hg < -1000) { |
| 2556 | dev_alert(DEV, "To resolve this both sides have to support at least protocol %d\n", -hg - 1000); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2557 | return C_MASK; |
| 2558 | } |
| 2559 | |
| 2560 | if ((mydisk == D_INCONSISTENT && peer_disk > D_INCONSISTENT) || |
| 2561 | (peer_disk == D_INCONSISTENT && mydisk > D_INCONSISTENT)) { |
| 2562 | int f = (hg == -100) || abs(hg) == 2; |
| 2563 | hg = mydisk > D_INCONSISTENT ? 1 : -1; |
| 2564 | if (f) |
| 2565 | hg = hg*2; |
| 2566 | dev_info(DEV, "Becoming sync %s due to disk states.\n", |
| 2567 | hg > 0 ? "source" : "target"); |
| 2568 | } |
| 2569 | |
Adam Gandelman | 3a11a48 | 2010-04-08 16:48:23 -0700 | [diff] [blame] | 2570 | if (abs(hg) == 100) |
| 2571 | drbd_khelper(mdev, "initial-split-brain"); |
| 2572 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2573 | if (hg == 100 || (hg == -100 && mdev->net_conf->always_asbp)) { |
| 2574 | int pcount = (mdev->state.role == R_PRIMARY) |
| 2575 | + (peer_role == R_PRIMARY); |
| 2576 | int forced = (hg == -100); |
| 2577 | |
| 2578 | switch (pcount) { |
| 2579 | case 0: |
| 2580 | hg = drbd_asb_recover_0p(mdev); |
| 2581 | break; |
| 2582 | case 1: |
| 2583 | hg = drbd_asb_recover_1p(mdev); |
| 2584 | break; |
| 2585 | case 2: |
| 2586 | hg = drbd_asb_recover_2p(mdev); |
| 2587 | break; |
| 2588 | } |
| 2589 | if (abs(hg) < 100) { |
| 2590 | dev_warn(DEV, "Split-Brain detected, %d primaries, " |
| 2591 | "automatically solved. Sync from %s node\n", |
| 2592 | pcount, (hg < 0) ? "peer" : "this"); |
| 2593 | if (forced) { |
| 2594 | dev_warn(DEV, "Doing a full sync, since" |
| 2595 | " UUIDs where ambiguous.\n"); |
| 2596 | hg = hg*2; |
| 2597 | } |
| 2598 | } |
| 2599 | } |
| 2600 | |
| 2601 | if (hg == -100) { |
| 2602 | if (mdev->net_conf->want_lose && !(mdev->p_uuid[UI_FLAGS]&1)) |
| 2603 | hg = -1; |
| 2604 | if (!mdev->net_conf->want_lose && (mdev->p_uuid[UI_FLAGS]&1)) |
| 2605 | hg = 1; |
| 2606 | |
| 2607 | if (abs(hg) < 100) |
| 2608 | dev_warn(DEV, "Split-Brain detected, manually solved. " |
| 2609 | "Sync from %s node\n", |
| 2610 | (hg < 0) ? "peer" : "this"); |
| 2611 | } |
| 2612 | |
| 2613 | if (hg == -100) { |
Lars Ellenberg | 580b976 | 2010-02-26 23:15:23 +0100 | [diff] [blame] | 2614 | /* FIXME this log message is not correct if we end up here |
| 2615 | * after an attempted attach on a diskless node. |
| 2616 | * We just refuse to attach -- well, we drop the "connection" |
| 2617 | * to that disk, in a way... */ |
Adam Gandelman | 3a11a48 | 2010-04-08 16:48:23 -0700 | [diff] [blame] | 2618 | dev_alert(DEV, "Split-Brain detected but unresolved, dropping connection!\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2619 | drbd_khelper(mdev, "split-brain"); |
| 2620 | return C_MASK; |
| 2621 | } |
| 2622 | |
| 2623 | if (hg > 0 && mydisk <= D_INCONSISTENT) { |
| 2624 | dev_err(DEV, "I shall become SyncSource, but I am inconsistent!\n"); |
| 2625 | return C_MASK; |
| 2626 | } |
| 2627 | |
| 2628 | if (hg < 0 && /* by intention we do not use mydisk here. */ |
| 2629 | mdev->state.role == R_PRIMARY && mdev->state.disk >= D_CONSISTENT) { |
| 2630 | switch (mdev->net_conf->rr_conflict) { |
| 2631 | case ASB_CALL_HELPER: |
| 2632 | drbd_khelper(mdev, "pri-lost"); |
| 2633 | /* fall through */ |
| 2634 | case ASB_DISCONNECT: |
| 2635 | dev_err(DEV, "I shall become SyncTarget, but I am primary!\n"); |
| 2636 | return C_MASK; |
| 2637 | case ASB_VIOLENTLY: |
| 2638 | dev_warn(DEV, "Becoming SyncTarget, violating the stable-data" |
| 2639 | "assumption\n"); |
| 2640 | } |
| 2641 | } |
| 2642 | |
Philipp Reisner | cf14c2e | 2010-02-02 21:03:50 +0100 | [diff] [blame] | 2643 | if (mdev->net_conf->dry_run || test_bit(CONN_DRY_RUN, &mdev->flags)) { |
| 2644 | if (hg == 0) |
| 2645 | dev_info(DEV, "dry-run connect: No resync, would become Connected immediately.\n"); |
| 2646 | else |
| 2647 | dev_info(DEV, "dry-run connect: Would become %s, doing a %s resync.", |
| 2648 | drbd_conn_str(hg > 0 ? C_SYNC_SOURCE : C_SYNC_TARGET), |
| 2649 | abs(hg) >= 2 ? "full" : "bit-map based"); |
| 2650 | return C_MASK; |
| 2651 | } |
| 2652 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2653 | if (abs(hg) >= 2) { |
| 2654 | dev_info(DEV, "Writing the whole bitmap, full sync required after drbd_sync_handshake.\n"); |
Lars Ellenberg | 20ceb2b | 2011-01-21 10:56:44 +0100 | [diff] [blame] | 2655 | if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write, "set_n_write from sync_handshake", |
| 2656 | BM_LOCKED_SET_ALLOWED)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2657 | return C_MASK; |
| 2658 | } |
| 2659 | |
| 2660 | if (hg > 0) { /* become sync source. */ |
| 2661 | rv = C_WF_BITMAP_S; |
| 2662 | } else if (hg < 0) { /* become sync target */ |
| 2663 | rv = C_WF_BITMAP_T; |
| 2664 | } else { |
| 2665 | rv = C_CONNECTED; |
| 2666 | if (drbd_bm_total_weight(mdev)) { |
| 2667 | dev_info(DEV, "No resync, but %lu bits in bitmap!\n", |
| 2668 | drbd_bm_total_weight(mdev)); |
| 2669 | } |
| 2670 | } |
| 2671 | |
| 2672 | return rv; |
| 2673 | } |
| 2674 | |
| 2675 | /* returns 1 if invalid */ |
| 2676 | static int cmp_after_sb(enum drbd_after_sb_p peer, enum drbd_after_sb_p self) |
| 2677 | { |
| 2678 | /* ASB_DISCARD_REMOTE - ASB_DISCARD_LOCAL is valid */ |
| 2679 | if ((peer == ASB_DISCARD_REMOTE && self == ASB_DISCARD_LOCAL) || |
| 2680 | (self == ASB_DISCARD_REMOTE && peer == ASB_DISCARD_LOCAL)) |
| 2681 | return 0; |
| 2682 | |
| 2683 | /* any other things with ASB_DISCARD_REMOTE or ASB_DISCARD_LOCAL are invalid */ |
| 2684 | if (peer == ASB_DISCARD_REMOTE || peer == ASB_DISCARD_LOCAL || |
| 2685 | self == ASB_DISCARD_REMOTE || self == ASB_DISCARD_LOCAL) |
| 2686 | return 1; |
| 2687 | |
| 2688 | /* everything else is valid if they are equal on both sides. */ |
| 2689 | if (peer == self) |
| 2690 | return 0; |
| 2691 | |
| 2692 | /* everything es is invalid. */ |
| 2693 | return 1; |
| 2694 | } |
| 2695 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 2696 | static int receive_protocol(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2697 | { |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 2698 | struct p_protocol *p = &mdev->data.rbuf.protocol; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2699 | int p_proto, p_after_sb_0p, p_after_sb_1p, p_after_sb_2p; |
Philipp Reisner | cf14c2e | 2010-02-02 21:03:50 +0100 | [diff] [blame] | 2700 | int p_want_lose, p_two_primaries, cf; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2701 | char p_integrity_alg[SHARED_SECRET_MAX] = ""; |
| 2702 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2703 | p_proto = be32_to_cpu(p->protocol); |
| 2704 | p_after_sb_0p = be32_to_cpu(p->after_sb_0p); |
| 2705 | p_after_sb_1p = be32_to_cpu(p->after_sb_1p); |
| 2706 | p_after_sb_2p = be32_to_cpu(p->after_sb_2p); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2707 | p_two_primaries = be32_to_cpu(p->two_primaries); |
Philipp Reisner | cf14c2e | 2010-02-02 21:03:50 +0100 | [diff] [blame] | 2708 | cf = be32_to_cpu(p->conn_flags); |
| 2709 | p_want_lose = cf & CF_WANT_LOSE; |
| 2710 | |
| 2711 | clear_bit(CONN_DRY_RUN, &mdev->flags); |
| 2712 | |
| 2713 | if (cf & CF_DRY_RUN) |
| 2714 | set_bit(CONN_DRY_RUN, &mdev->flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2715 | |
| 2716 | if (p_proto != mdev->net_conf->wire_protocol) { |
| 2717 | dev_err(DEV, "incompatible communication protocols\n"); |
| 2718 | goto disconnect; |
| 2719 | } |
| 2720 | |
| 2721 | if (cmp_after_sb(p_after_sb_0p, mdev->net_conf->after_sb_0p)) { |
| 2722 | dev_err(DEV, "incompatible after-sb-0pri settings\n"); |
| 2723 | goto disconnect; |
| 2724 | } |
| 2725 | |
| 2726 | if (cmp_after_sb(p_after_sb_1p, mdev->net_conf->after_sb_1p)) { |
| 2727 | dev_err(DEV, "incompatible after-sb-1pri settings\n"); |
| 2728 | goto disconnect; |
| 2729 | } |
| 2730 | |
| 2731 | if (cmp_after_sb(p_after_sb_2p, mdev->net_conf->after_sb_2p)) { |
| 2732 | dev_err(DEV, "incompatible after-sb-2pri settings\n"); |
| 2733 | goto disconnect; |
| 2734 | } |
| 2735 | |
| 2736 | if (p_want_lose && mdev->net_conf->want_lose) { |
| 2737 | dev_err(DEV, "both sides have the 'want_lose' flag set\n"); |
| 2738 | goto disconnect; |
| 2739 | } |
| 2740 | |
| 2741 | if (p_two_primaries != mdev->net_conf->two_primaries) { |
| 2742 | dev_err(DEV, "incompatible setting of the two-primaries options\n"); |
| 2743 | goto disconnect; |
| 2744 | } |
| 2745 | |
| 2746 | if (mdev->agreed_pro_version >= 87) { |
| 2747 | unsigned char *my_alg = mdev->net_conf->integrity_alg; |
| 2748 | |
| 2749 | if (drbd_recv(mdev, p_integrity_alg, data_size) != data_size) |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 2750 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2751 | |
| 2752 | p_integrity_alg[SHARED_SECRET_MAX-1] = 0; |
| 2753 | if (strcmp(p_integrity_alg, my_alg)) { |
| 2754 | dev_err(DEV, "incompatible setting of the data-integrity-alg\n"); |
| 2755 | goto disconnect; |
| 2756 | } |
| 2757 | dev_info(DEV, "data-integrity-alg: %s\n", |
| 2758 | my_alg[0] ? my_alg : (unsigned char *)"<not-used>"); |
| 2759 | } |
| 2760 | |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 2761 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2762 | |
| 2763 | disconnect: |
| 2764 | drbd_force_state(mdev, NS(conn, C_DISCONNECTING)); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 2765 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2766 | } |
| 2767 | |
| 2768 | /* helper function |
| 2769 | * input: alg name, feature name |
| 2770 | * return: NULL (alg name was "") |
| 2771 | * ERR_PTR(error) if something goes wrong |
| 2772 | * or the crypto hash ptr, if it worked out ok. */ |
| 2773 | struct crypto_hash *drbd_crypto_alloc_digest_safe(const struct drbd_conf *mdev, |
| 2774 | const char *alg, const char *name) |
| 2775 | { |
| 2776 | struct crypto_hash *tfm; |
| 2777 | |
| 2778 | if (!alg[0]) |
| 2779 | return NULL; |
| 2780 | |
| 2781 | tfm = crypto_alloc_hash(alg, 0, CRYPTO_ALG_ASYNC); |
| 2782 | if (IS_ERR(tfm)) { |
| 2783 | dev_err(DEV, "Can not allocate \"%s\" as %s (reason: %ld)\n", |
| 2784 | alg, name, PTR_ERR(tfm)); |
| 2785 | return tfm; |
| 2786 | } |
| 2787 | if (!drbd_crypto_is_hash(crypto_hash_tfm(tfm))) { |
| 2788 | crypto_free_hash(tfm); |
| 2789 | dev_err(DEV, "\"%s\" is not a digest (%s)\n", alg, name); |
| 2790 | return ERR_PTR(-EINVAL); |
| 2791 | } |
| 2792 | return tfm; |
| 2793 | } |
| 2794 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 2795 | static int receive_SyncParam(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int packet_size) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2796 | { |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 2797 | int ok = true; |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 2798 | struct p_rs_param_95 *p = &mdev->data.rbuf.rs_param_95; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2799 | unsigned int header_size, data_size, exp_max_sz; |
| 2800 | struct crypto_hash *verify_tfm = NULL; |
| 2801 | struct crypto_hash *csums_tfm = NULL; |
| 2802 | const int apv = mdev->agreed_pro_version; |
Philipp Reisner | 778f271 | 2010-07-06 11:14:00 +0200 | [diff] [blame] | 2803 | int *rs_plan_s = NULL; |
| 2804 | int fifo_size = 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2805 | |
| 2806 | exp_max_sz = apv <= 87 ? sizeof(struct p_rs_param) |
| 2807 | : apv == 88 ? sizeof(struct p_rs_param) |
| 2808 | + SHARED_SECRET_MAX |
Philipp Reisner | 8e26f9c | 2010-07-06 17:25:54 +0200 | [diff] [blame] | 2809 | : apv <= 94 ? sizeof(struct p_rs_param_89) |
| 2810 | : /* apv >= 95 */ sizeof(struct p_rs_param_95); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2811 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 2812 | if (packet_size > exp_max_sz) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2813 | dev_err(DEV, "SyncParam packet too long: received %u, expected <= %u bytes\n", |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 2814 | packet_size, exp_max_sz); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 2815 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2816 | } |
| 2817 | |
| 2818 | if (apv <= 88) { |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 2819 | header_size = sizeof(struct p_rs_param) - sizeof(struct p_header80); |
| 2820 | data_size = packet_size - header_size; |
Philipp Reisner | 8e26f9c | 2010-07-06 17:25:54 +0200 | [diff] [blame] | 2821 | } else if (apv <= 94) { |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 2822 | header_size = sizeof(struct p_rs_param_89) - sizeof(struct p_header80); |
| 2823 | data_size = packet_size - header_size; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2824 | D_ASSERT(data_size == 0); |
Philipp Reisner | 8e26f9c | 2010-07-06 17:25:54 +0200 | [diff] [blame] | 2825 | } else { |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 2826 | header_size = sizeof(struct p_rs_param_95) - sizeof(struct p_header80); |
| 2827 | data_size = packet_size - header_size; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2828 | D_ASSERT(data_size == 0); |
| 2829 | } |
| 2830 | |
| 2831 | /* initialize verify_alg and csums_alg */ |
| 2832 | memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX); |
| 2833 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 2834 | if (drbd_recv(mdev, &p->head.payload, header_size) != header_size) |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 2835 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2836 | |
| 2837 | mdev->sync_conf.rate = be32_to_cpu(p->rate); |
| 2838 | |
| 2839 | if (apv >= 88) { |
| 2840 | if (apv == 88) { |
| 2841 | if (data_size > SHARED_SECRET_MAX) { |
| 2842 | dev_err(DEV, "verify-alg too long, " |
| 2843 | "peer wants %u, accepting only %u byte\n", |
| 2844 | data_size, SHARED_SECRET_MAX); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 2845 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2846 | } |
| 2847 | |
| 2848 | if (drbd_recv(mdev, p->verify_alg, data_size) != data_size) |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 2849 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2850 | |
| 2851 | /* we expect NUL terminated string */ |
| 2852 | /* but just in case someone tries to be evil */ |
| 2853 | D_ASSERT(p->verify_alg[data_size-1] == 0); |
| 2854 | p->verify_alg[data_size-1] = 0; |
| 2855 | |
| 2856 | } else /* apv >= 89 */ { |
| 2857 | /* we still expect NUL terminated strings */ |
| 2858 | /* but just in case someone tries to be evil */ |
| 2859 | D_ASSERT(p->verify_alg[SHARED_SECRET_MAX-1] == 0); |
| 2860 | D_ASSERT(p->csums_alg[SHARED_SECRET_MAX-1] == 0); |
| 2861 | p->verify_alg[SHARED_SECRET_MAX-1] = 0; |
| 2862 | p->csums_alg[SHARED_SECRET_MAX-1] = 0; |
| 2863 | } |
| 2864 | |
| 2865 | if (strcmp(mdev->sync_conf.verify_alg, p->verify_alg)) { |
| 2866 | if (mdev->state.conn == C_WF_REPORT_PARAMS) { |
| 2867 | dev_err(DEV, "Different verify-alg settings. me=\"%s\" peer=\"%s\"\n", |
| 2868 | mdev->sync_conf.verify_alg, p->verify_alg); |
| 2869 | goto disconnect; |
| 2870 | } |
| 2871 | verify_tfm = drbd_crypto_alloc_digest_safe(mdev, |
| 2872 | p->verify_alg, "verify-alg"); |
| 2873 | if (IS_ERR(verify_tfm)) { |
| 2874 | verify_tfm = NULL; |
| 2875 | goto disconnect; |
| 2876 | } |
| 2877 | } |
| 2878 | |
| 2879 | if (apv >= 89 && strcmp(mdev->sync_conf.csums_alg, p->csums_alg)) { |
| 2880 | if (mdev->state.conn == C_WF_REPORT_PARAMS) { |
| 2881 | dev_err(DEV, "Different csums-alg settings. me=\"%s\" peer=\"%s\"\n", |
| 2882 | mdev->sync_conf.csums_alg, p->csums_alg); |
| 2883 | goto disconnect; |
| 2884 | } |
| 2885 | csums_tfm = drbd_crypto_alloc_digest_safe(mdev, |
| 2886 | p->csums_alg, "csums-alg"); |
| 2887 | if (IS_ERR(csums_tfm)) { |
| 2888 | csums_tfm = NULL; |
| 2889 | goto disconnect; |
| 2890 | } |
| 2891 | } |
| 2892 | |
Philipp Reisner | 8e26f9c | 2010-07-06 17:25:54 +0200 | [diff] [blame] | 2893 | if (apv > 94) { |
| 2894 | mdev->sync_conf.rate = be32_to_cpu(p->rate); |
| 2895 | mdev->sync_conf.c_plan_ahead = be32_to_cpu(p->c_plan_ahead); |
| 2896 | mdev->sync_conf.c_delay_target = be32_to_cpu(p->c_delay_target); |
| 2897 | mdev->sync_conf.c_fill_target = be32_to_cpu(p->c_fill_target); |
| 2898 | mdev->sync_conf.c_max_rate = be32_to_cpu(p->c_max_rate); |
Philipp Reisner | 778f271 | 2010-07-06 11:14:00 +0200 | [diff] [blame] | 2899 | |
| 2900 | fifo_size = (mdev->sync_conf.c_plan_ahead * 10 * SLEEP_TIME) / HZ; |
| 2901 | if (fifo_size != mdev->rs_plan_s.size && fifo_size > 0) { |
| 2902 | rs_plan_s = kzalloc(sizeof(int) * fifo_size, GFP_KERNEL); |
| 2903 | if (!rs_plan_s) { |
| 2904 | dev_err(DEV, "kmalloc of fifo_buffer failed"); |
| 2905 | goto disconnect; |
| 2906 | } |
| 2907 | } |
Philipp Reisner | 8e26f9c | 2010-07-06 17:25:54 +0200 | [diff] [blame] | 2908 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2909 | |
| 2910 | spin_lock(&mdev->peer_seq_lock); |
| 2911 | /* lock against drbd_nl_syncer_conf() */ |
| 2912 | if (verify_tfm) { |
| 2913 | strcpy(mdev->sync_conf.verify_alg, p->verify_alg); |
| 2914 | mdev->sync_conf.verify_alg_len = strlen(p->verify_alg) + 1; |
| 2915 | crypto_free_hash(mdev->verify_tfm); |
| 2916 | mdev->verify_tfm = verify_tfm; |
| 2917 | dev_info(DEV, "using verify-alg: \"%s\"\n", p->verify_alg); |
| 2918 | } |
| 2919 | if (csums_tfm) { |
| 2920 | strcpy(mdev->sync_conf.csums_alg, p->csums_alg); |
| 2921 | mdev->sync_conf.csums_alg_len = strlen(p->csums_alg) + 1; |
| 2922 | crypto_free_hash(mdev->csums_tfm); |
| 2923 | mdev->csums_tfm = csums_tfm; |
| 2924 | dev_info(DEV, "using csums-alg: \"%s\"\n", p->csums_alg); |
| 2925 | } |
Philipp Reisner | 778f271 | 2010-07-06 11:14:00 +0200 | [diff] [blame] | 2926 | if (fifo_size != mdev->rs_plan_s.size) { |
| 2927 | kfree(mdev->rs_plan_s.values); |
| 2928 | mdev->rs_plan_s.values = rs_plan_s; |
| 2929 | mdev->rs_plan_s.size = fifo_size; |
| 2930 | mdev->rs_planed = 0; |
| 2931 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2932 | spin_unlock(&mdev->peer_seq_lock); |
| 2933 | } |
| 2934 | |
| 2935 | return ok; |
| 2936 | disconnect: |
| 2937 | /* just for completeness: actually not needed, |
| 2938 | * as this is not reached if csums_tfm was ok. */ |
| 2939 | crypto_free_hash(csums_tfm); |
| 2940 | /* but free the verify_tfm again, if csums_tfm did not work out */ |
| 2941 | crypto_free_hash(verify_tfm); |
| 2942 | drbd_force_state(mdev, NS(conn, C_DISCONNECTING)); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 2943 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2944 | } |
| 2945 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2946 | /* warn if the arguments differ by more than 12.5% */ |
| 2947 | static void warn_if_differ_considerably(struct drbd_conf *mdev, |
| 2948 | const char *s, sector_t a, sector_t b) |
| 2949 | { |
| 2950 | sector_t d; |
| 2951 | if (a == 0 || b == 0) |
| 2952 | return; |
| 2953 | d = (a > b) ? (a - b) : (b - a); |
| 2954 | if (d > (a>>3) || d > (b>>3)) |
| 2955 | dev_warn(DEV, "Considerable difference in %s: %llus vs. %llus\n", s, |
| 2956 | (unsigned long long)a, (unsigned long long)b); |
| 2957 | } |
| 2958 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 2959 | static int receive_sizes(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2960 | { |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 2961 | struct p_sizes *p = &mdev->data.rbuf.sizes; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2962 | enum determine_dev_size dd = unchanged; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2963 | sector_t p_size, p_usize, my_usize; |
| 2964 | int ldsc = 0; /* local disk size changed */ |
Philipp Reisner | e89b591 | 2010-03-24 17:11:33 +0100 | [diff] [blame] | 2965 | enum dds_flags ddsf; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2966 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2967 | p_size = be64_to_cpu(p->d_size); |
| 2968 | p_usize = be64_to_cpu(p->u_size); |
| 2969 | |
| 2970 | if (p_size == 0 && mdev->state.disk == D_DISKLESS) { |
| 2971 | dev_err(DEV, "some backing storage is needed\n"); |
| 2972 | drbd_force_state(mdev, NS(conn, C_DISCONNECTING)); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 2973 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2974 | } |
| 2975 | |
| 2976 | /* just store the peer's disk size for now. |
| 2977 | * we still need to figure out whether we accept that. */ |
| 2978 | mdev->p_size = p_size; |
| 2979 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2980 | if (get_ldev(mdev)) { |
| 2981 | warn_if_differ_considerably(mdev, "lower level device sizes", |
| 2982 | p_size, drbd_get_max_capacity(mdev->ldev)); |
| 2983 | warn_if_differ_considerably(mdev, "user requested size", |
| 2984 | p_usize, mdev->ldev->dc.disk_size); |
| 2985 | |
| 2986 | /* if this is the first connect, or an otherwise expected |
| 2987 | * param exchange, choose the minimum */ |
| 2988 | if (mdev->state.conn == C_WF_REPORT_PARAMS) |
| 2989 | p_usize = min_not_zero((sector_t)mdev->ldev->dc.disk_size, |
| 2990 | p_usize); |
| 2991 | |
| 2992 | my_usize = mdev->ldev->dc.disk_size; |
| 2993 | |
| 2994 | if (mdev->ldev->dc.disk_size != p_usize) { |
| 2995 | mdev->ldev->dc.disk_size = p_usize; |
| 2996 | dev_info(DEV, "Peer sets u_size to %lu sectors\n", |
| 2997 | (unsigned long)mdev->ldev->dc.disk_size); |
| 2998 | } |
| 2999 | |
| 3000 | /* Never shrink a device with usable data during connect. |
| 3001 | But allow online shrinking if we are connected. */ |
Philipp Reisner | a393db6 | 2009-12-22 13:35:52 +0100 | [diff] [blame] | 3002 | if (drbd_new_dev_size(mdev, mdev->ldev, 0) < |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3003 | drbd_get_capacity(mdev->this_bdev) && |
| 3004 | mdev->state.disk >= D_OUTDATED && |
| 3005 | mdev->state.conn < C_CONNECTED) { |
| 3006 | dev_err(DEV, "The peer's disk size is too small!\n"); |
| 3007 | drbd_force_state(mdev, NS(conn, C_DISCONNECTING)); |
| 3008 | mdev->ldev->dc.disk_size = my_usize; |
| 3009 | put_ldev(mdev); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3010 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3011 | } |
| 3012 | put_ldev(mdev); |
| 3013 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3014 | |
Philipp Reisner | e89b591 | 2010-03-24 17:11:33 +0100 | [diff] [blame] | 3015 | ddsf = be16_to_cpu(p->dds_flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3016 | if (get_ldev(mdev)) { |
Bart Van Assche | 24c4830 | 2011-05-21 18:32:29 +0200 | [diff] [blame] | 3017 | dd = drbd_determine_dev_size(mdev, ddsf); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3018 | put_ldev(mdev); |
| 3019 | if (dd == dev_size_error) |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3020 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3021 | drbd_md_sync(mdev); |
| 3022 | } else { |
| 3023 | /* I am diskless, need to accept the peer's size. */ |
| 3024 | drbd_set_my_capacity(mdev, p_size); |
| 3025 | } |
| 3026 | |
Philipp Reisner | 99432fc | 2011-05-20 16:39:13 +0200 | [diff] [blame] | 3027 | mdev->peer_max_bio_size = be32_to_cpu(p->max_bio_size); |
| 3028 | drbd_reconsider_max_bio_size(mdev); |
| 3029 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3030 | if (get_ldev(mdev)) { |
| 3031 | if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev)) { |
| 3032 | mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev); |
| 3033 | ldsc = 1; |
| 3034 | } |
| 3035 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3036 | put_ldev(mdev); |
| 3037 | } |
| 3038 | |
| 3039 | if (mdev->state.conn > C_WF_REPORT_PARAMS) { |
| 3040 | if (be64_to_cpu(p->c_size) != |
| 3041 | drbd_get_capacity(mdev->this_bdev) || ldsc) { |
| 3042 | /* we have different sizes, probably peer |
| 3043 | * needs to know my new size... */ |
Philipp Reisner | e89b591 | 2010-03-24 17:11:33 +0100 | [diff] [blame] | 3044 | drbd_send_sizes(mdev, 0, ddsf); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3045 | } |
| 3046 | if (test_and_clear_bit(RESIZE_PENDING, &mdev->flags) || |
| 3047 | (dd == grew && mdev->state.conn == C_CONNECTED)) { |
| 3048 | if (mdev->state.pdsk >= D_INCONSISTENT && |
Philipp Reisner | e89b591 | 2010-03-24 17:11:33 +0100 | [diff] [blame] | 3049 | mdev->state.disk >= D_INCONSISTENT) { |
| 3050 | if (ddsf & DDSF_NO_RESYNC) |
| 3051 | dev_info(DEV, "Resync of new storage suppressed with --assume-clean\n"); |
| 3052 | else |
| 3053 | resync_after_online_grow(mdev); |
| 3054 | } else |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3055 | set_bit(RESYNC_AFTER_NEG, &mdev->flags); |
| 3056 | } |
| 3057 | } |
| 3058 | |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3059 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3060 | } |
| 3061 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3062 | static int receive_uuids(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3063 | { |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3064 | struct p_uuids *p = &mdev->data.rbuf.uuids; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3065 | u64 *p_uuid; |
Lars Ellenberg | 62b0da3 | 2011-01-20 13:25:21 +0100 | [diff] [blame] | 3066 | int i, updated_uuids = 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3067 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3068 | p_uuid = kmalloc(sizeof(u64)*UI_EXTENDED_SIZE, GFP_NOIO); |
| 3069 | |
| 3070 | for (i = UI_CURRENT; i < UI_EXTENDED_SIZE; i++) |
| 3071 | p_uuid[i] = be64_to_cpu(p->uuid[i]); |
| 3072 | |
| 3073 | kfree(mdev->p_uuid); |
| 3074 | mdev->p_uuid = p_uuid; |
| 3075 | |
| 3076 | if (mdev->state.conn < C_CONNECTED && |
| 3077 | mdev->state.disk < D_INCONSISTENT && |
| 3078 | mdev->state.role == R_PRIMARY && |
| 3079 | (mdev->ed_uuid & ~((u64)1)) != (p_uuid[UI_CURRENT] & ~((u64)1))) { |
| 3080 | dev_err(DEV, "Can only connect to data with current UUID=%016llX\n", |
| 3081 | (unsigned long long)mdev->ed_uuid); |
| 3082 | drbd_force_state(mdev, NS(conn, C_DISCONNECTING)); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3083 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3084 | } |
| 3085 | |
| 3086 | if (get_ldev(mdev)) { |
| 3087 | int skip_initial_sync = |
| 3088 | mdev->state.conn == C_CONNECTED && |
| 3089 | mdev->agreed_pro_version >= 90 && |
| 3090 | mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && |
| 3091 | (p_uuid[UI_FLAGS] & 8); |
| 3092 | if (skip_initial_sync) { |
| 3093 | dev_info(DEV, "Accepted new current UUID, preparing to skip initial sync\n"); |
| 3094 | drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write, |
Lars Ellenberg | 20ceb2b | 2011-01-21 10:56:44 +0100 | [diff] [blame] | 3095 | "clear_n_write from receive_uuids", |
| 3096 | BM_LOCKED_TEST_ALLOWED); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3097 | _drbd_uuid_set(mdev, UI_CURRENT, p_uuid[UI_CURRENT]); |
| 3098 | _drbd_uuid_set(mdev, UI_BITMAP, 0); |
| 3099 | _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE), |
| 3100 | CS_VERBOSE, NULL); |
| 3101 | drbd_md_sync(mdev); |
Lars Ellenberg | 62b0da3 | 2011-01-20 13:25:21 +0100 | [diff] [blame] | 3102 | updated_uuids = 1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3103 | } |
| 3104 | put_ldev(mdev); |
Philipp Reisner | 18a50fa | 2010-06-21 14:14:15 +0200 | [diff] [blame] | 3105 | } else if (mdev->state.disk < D_INCONSISTENT && |
| 3106 | mdev->state.role == R_PRIMARY) { |
| 3107 | /* I am a diskless primary, the peer just created a new current UUID |
| 3108 | for me. */ |
Lars Ellenberg | 62b0da3 | 2011-01-20 13:25:21 +0100 | [diff] [blame] | 3109 | updated_uuids = drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3110 | } |
| 3111 | |
| 3112 | /* Before we test for the disk state, we should wait until an eventually |
| 3113 | ongoing cluster wide state change is finished. That is important if |
| 3114 | we are primary and are detaching from our disk. We need to see the |
| 3115 | new disk state... */ |
| 3116 | wait_event(mdev->misc_wait, !test_bit(CLUSTER_ST_CHANGE, &mdev->flags)); |
| 3117 | if (mdev->state.conn >= C_CONNECTED && mdev->state.disk < D_INCONSISTENT) |
Lars Ellenberg | 62b0da3 | 2011-01-20 13:25:21 +0100 | [diff] [blame] | 3118 | updated_uuids |= drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]); |
| 3119 | |
| 3120 | if (updated_uuids) |
| 3121 | drbd_print_uuids(mdev, "receiver updated UUIDs to"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3122 | |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3123 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3124 | } |
| 3125 | |
| 3126 | /** |
| 3127 | * convert_state() - Converts the peer's view of the cluster state to our point of view |
| 3128 | * @ps: The state as seen by the peer. |
| 3129 | */ |
| 3130 | static union drbd_state convert_state(union drbd_state ps) |
| 3131 | { |
| 3132 | union drbd_state ms; |
| 3133 | |
| 3134 | static enum drbd_conns c_tab[] = { |
| 3135 | [C_CONNECTED] = C_CONNECTED, |
| 3136 | |
| 3137 | [C_STARTING_SYNC_S] = C_STARTING_SYNC_T, |
| 3138 | [C_STARTING_SYNC_T] = C_STARTING_SYNC_S, |
| 3139 | [C_DISCONNECTING] = C_TEAR_DOWN, /* C_NETWORK_FAILURE, */ |
| 3140 | [C_VERIFY_S] = C_VERIFY_T, |
| 3141 | [C_MASK] = C_MASK, |
| 3142 | }; |
| 3143 | |
| 3144 | ms.i = ps.i; |
| 3145 | |
| 3146 | ms.conn = c_tab[ps.conn]; |
| 3147 | ms.peer = ps.role; |
| 3148 | ms.role = ps.peer; |
| 3149 | ms.pdsk = ps.disk; |
| 3150 | ms.disk = ps.pdsk; |
| 3151 | ms.peer_isp = (ps.aftr_isp | ps.user_isp); |
| 3152 | |
| 3153 | return ms; |
| 3154 | } |
| 3155 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3156 | static int receive_req_state(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3157 | { |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3158 | struct p_req_state *p = &mdev->data.rbuf.req_state; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3159 | union drbd_state mask, val; |
Andreas Gruenbacher | bf885f8 | 2010-12-08 00:39:32 +0100 | [diff] [blame] | 3160 | enum drbd_state_rv rv; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3161 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3162 | mask.i = be32_to_cpu(p->mask); |
| 3163 | val.i = be32_to_cpu(p->val); |
| 3164 | |
| 3165 | if (test_bit(DISCARD_CONCURRENT, &mdev->flags) && |
| 3166 | test_bit(CLUSTER_ST_CHANGE, &mdev->flags)) { |
| 3167 | drbd_send_sr_reply(mdev, SS_CONCURRENT_ST_CHG); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3168 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3169 | } |
| 3170 | |
| 3171 | mask = convert_state(mask); |
| 3172 | val = convert_state(val); |
| 3173 | |
| 3174 | rv = drbd_change_state(mdev, CS_VERBOSE, mask, val); |
| 3175 | |
| 3176 | drbd_send_sr_reply(mdev, rv); |
| 3177 | drbd_md_sync(mdev); |
| 3178 | |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3179 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3180 | } |
| 3181 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3182 | static int receive_state(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3183 | { |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3184 | struct p_state *p = &mdev->data.rbuf.state; |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3185 | union drbd_state os, ns, peer_state; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3186 | enum drbd_disk_state real_peer_disk; |
Philipp Reisner | 65d922c | 2010-06-16 16:18:09 +0200 | [diff] [blame] | 3187 | enum chg_state_flags cs_flags; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3188 | int rv; |
| 3189 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3190 | peer_state.i = be32_to_cpu(p->state); |
| 3191 | |
| 3192 | real_peer_disk = peer_state.disk; |
| 3193 | if (peer_state.disk == D_NEGOTIATING) { |
| 3194 | real_peer_disk = mdev->p_uuid[UI_FLAGS] & 4 ? D_INCONSISTENT : D_CONSISTENT; |
| 3195 | dev_info(DEV, "real peer disk state = %s\n", drbd_disk_str(real_peer_disk)); |
| 3196 | } |
| 3197 | |
| 3198 | spin_lock_irq(&mdev->req_lock); |
| 3199 | retry: |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3200 | os = ns = mdev->state; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3201 | spin_unlock_irq(&mdev->req_lock); |
| 3202 | |
Lars Ellenberg | e9ef7bb | 2010-10-07 15:55:39 +0200 | [diff] [blame] | 3203 | /* peer says his disk is uptodate, while we think it is inconsistent, |
| 3204 | * and this happens while we think we have a sync going on. */ |
| 3205 | if (os.pdsk == D_INCONSISTENT && real_peer_disk == D_UP_TO_DATE && |
| 3206 | os.conn > C_CONNECTED && os.disk == D_UP_TO_DATE) { |
| 3207 | /* If we are (becoming) SyncSource, but peer is still in sync |
| 3208 | * preparation, ignore its uptodate-ness to avoid flapping, it |
| 3209 | * will change to inconsistent once the peer reaches active |
| 3210 | * syncing states. |
| 3211 | * It may have changed syncer-paused flags, however, so we |
| 3212 | * cannot ignore this completely. */ |
| 3213 | if (peer_state.conn > C_CONNECTED && |
| 3214 | peer_state.conn < C_SYNC_SOURCE) |
| 3215 | real_peer_disk = D_INCONSISTENT; |
| 3216 | |
| 3217 | /* if peer_state changes to connected at the same time, |
| 3218 | * it explicitly notifies us that it finished resync. |
| 3219 | * Maybe we should finish it up, too? */ |
| 3220 | else if (os.conn >= C_SYNC_SOURCE && |
| 3221 | peer_state.conn == C_CONNECTED) { |
| 3222 | if (drbd_bm_total_weight(mdev) <= mdev->rs_failed) |
| 3223 | drbd_resync_finished(mdev); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3224 | return true; |
Lars Ellenberg | e9ef7bb | 2010-10-07 15:55:39 +0200 | [diff] [blame] | 3225 | } |
| 3226 | } |
| 3227 | |
| 3228 | /* peer says his disk is inconsistent, while we think it is uptodate, |
| 3229 | * and this happens while the peer still thinks we have a sync going on, |
| 3230 | * but we think we are already done with the sync. |
| 3231 | * We ignore this to avoid flapping pdsk. |
| 3232 | * This should not happen, if the peer is a recent version of drbd. */ |
| 3233 | if (os.pdsk == D_UP_TO_DATE && real_peer_disk == D_INCONSISTENT && |
| 3234 | os.conn == C_CONNECTED && peer_state.conn > C_SYNC_SOURCE) |
| 3235 | real_peer_disk = D_UP_TO_DATE; |
| 3236 | |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3237 | if (ns.conn == C_WF_REPORT_PARAMS) |
| 3238 | ns.conn = C_CONNECTED; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3239 | |
Philipp Reisner | 6753171 | 2010-10-27 12:21:30 +0200 | [diff] [blame] | 3240 | if (peer_state.conn == C_AHEAD) |
| 3241 | ns.conn = C_BEHIND; |
| 3242 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3243 | if (mdev->p_uuid && peer_state.disk >= D_NEGOTIATING && |
| 3244 | get_ldev_if_state(mdev, D_NEGOTIATING)) { |
| 3245 | int cr; /* consider resync */ |
| 3246 | |
| 3247 | /* if we established a new connection */ |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3248 | cr = (os.conn < C_CONNECTED); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3249 | /* if we had an established connection |
| 3250 | * and one of the nodes newly attaches a disk */ |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3251 | cr |= (os.conn == C_CONNECTED && |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3252 | (peer_state.disk == D_NEGOTIATING || |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3253 | os.disk == D_NEGOTIATING)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3254 | /* if we have both been inconsistent, and the peer has been |
| 3255 | * forced to be UpToDate with --overwrite-data */ |
| 3256 | cr |= test_bit(CONSIDER_RESYNC, &mdev->flags); |
| 3257 | /* if we had been plain connected, and the admin requested to |
| 3258 | * start a sync by "invalidate" or "invalidate-remote" */ |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3259 | cr |= (os.conn == C_CONNECTED && |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3260 | (peer_state.conn >= C_STARTING_SYNC_S && |
| 3261 | peer_state.conn <= C_WF_BITMAP_T)); |
| 3262 | |
| 3263 | if (cr) |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3264 | ns.conn = drbd_sync_handshake(mdev, peer_state.role, real_peer_disk); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3265 | |
| 3266 | put_ldev(mdev); |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3267 | if (ns.conn == C_MASK) { |
| 3268 | ns.conn = C_CONNECTED; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3269 | if (mdev->state.disk == D_NEGOTIATING) { |
Lars Ellenberg | 82f59cc | 2010-10-16 12:13:47 +0200 | [diff] [blame] | 3270 | drbd_force_state(mdev, NS(disk, D_FAILED)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3271 | } else if (peer_state.disk == D_NEGOTIATING) { |
| 3272 | dev_err(DEV, "Disk attach process on the peer node was aborted.\n"); |
| 3273 | peer_state.disk = D_DISKLESS; |
Lars Ellenberg | 580b976 | 2010-02-26 23:15:23 +0100 | [diff] [blame] | 3274 | real_peer_disk = D_DISKLESS; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3275 | } else { |
Philipp Reisner | cf14c2e | 2010-02-02 21:03:50 +0100 | [diff] [blame] | 3276 | if (test_and_clear_bit(CONN_DRY_RUN, &mdev->flags)) |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3277 | return false; |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3278 | D_ASSERT(os.conn == C_WF_REPORT_PARAMS); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3279 | drbd_force_state(mdev, NS(conn, C_DISCONNECTING)); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3280 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3281 | } |
| 3282 | } |
| 3283 | } |
| 3284 | |
| 3285 | spin_lock_irq(&mdev->req_lock); |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3286 | if (mdev->state.i != os.i) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3287 | goto retry; |
| 3288 | clear_bit(CONSIDER_RESYNC, &mdev->flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3289 | ns.peer = peer_state.role; |
| 3290 | ns.pdsk = real_peer_disk; |
| 3291 | ns.peer_isp = (peer_state.aftr_isp | peer_state.user_isp); |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3292 | if ((ns.conn == C_CONNECTED || ns.conn == C_WF_BITMAP_S) && ns.disk == D_NEGOTIATING) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3293 | ns.disk = mdev->new_state_tmp.disk; |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3294 | cs_flags = CS_VERBOSE + (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED ? 0 : CS_HARD); |
| 3295 | if (ns.pdsk == D_CONSISTENT && is_susp(ns) && ns.conn == C_CONNECTED && os.conn < C_CONNECTED && |
Philipp Reisner | 481c6f5 | 2010-06-22 14:03:27 +0200 | [diff] [blame] | 3296 | test_bit(NEW_CUR_UUID, &mdev->flags)) { |
| 3297 | /* Do not allow tl_restart(resend) for a rebooted peer. We can only allow this |
| 3298 | for temporal network outages! */ |
| 3299 | spin_unlock_irq(&mdev->req_lock); |
| 3300 | dev_err(DEV, "Aborting Connect, can not thaw IO with an only Consistent peer\n"); |
| 3301 | tl_clear(mdev); |
| 3302 | drbd_uuid_new_current(mdev); |
| 3303 | clear_bit(NEW_CUR_UUID, &mdev->flags); |
| 3304 | drbd_force_state(mdev, NS2(conn, C_PROTOCOL_ERROR, susp, 0)); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3305 | return false; |
Philipp Reisner | 481c6f5 | 2010-06-22 14:03:27 +0200 | [diff] [blame] | 3306 | } |
Philipp Reisner | 65d922c | 2010-06-16 16:18:09 +0200 | [diff] [blame] | 3307 | rv = _drbd_set_state(mdev, ns, cs_flags, NULL); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3308 | ns = mdev->state; |
| 3309 | spin_unlock_irq(&mdev->req_lock); |
| 3310 | |
| 3311 | if (rv < SS_SUCCESS) { |
| 3312 | drbd_force_state(mdev, NS(conn, C_DISCONNECTING)); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3313 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3314 | } |
| 3315 | |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3316 | if (os.conn > C_WF_REPORT_PARAMS) { |
| 3317 | if (ns.conn > C_CONNECTED && peer_state.conn <= C_CONNECTED && |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3318 | peer_state.disk != D_NEGOTIATING ) { |
| 3319 | /* we want resync, peer has not yet decided to sync... */ |
| 3320 | /* Nowadays only used when forcing a node into primary role and |
| 3321 | setting its disk to UpToDate with that */ |
| 3322 | drbd_send_uuids(mdev); |
| 3323 | drbd_send_state(mdev); |
| 3324 | } |
| 3325 | } |
| 3326 | |
| 3327 | mdev->net_conf->want_lose = 0; |
| 3328 | |
| 3329 | drbd_md_sync(mdev); /* update connected indicator, la_size, ... */ |
| 3330 | |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3331 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3332 | } |
| 3333 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3334 | static int receive_sync_uuid(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3335 | { |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3336 | struct p_rs_uuid *p = &mdev->data.rbuf.rs_uuid; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3337 | |
| 3338 | wait_event(mdev->misc_wait, |
| 3339 | mdev->state.conn == C_WF_SYNC_UUID || |
Philipp Reisner | c4752ef | 2010-10-27 17:32:36 +0200 | [diff] [blame] | 3340 | mdev->state.conn == C_BEHIND || |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3341 | mdev->state.conn < C_CONNECTED || |
| 3342 | mdev->state.disk < D_NEGOTIATING); |
| 3343 | |
| 3344 | /* D_ASSERT( mdev->state.conn == C_WF_SYNC_UUID ); */ |
| 3345 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3346 | /* Here the _drbd_uuid_ functions are right, current should |
| 3347 | _not_ be rotated into the history */ |
| 3348 | if (get_ldev_if_state(mdev, D_NEGOTIATING)) { |
| 3349 | _drbd_uuid_set(mdev, UI_CURRENT, be64_to_cpu(p->uuid)); |
| 3350 | _drbd_uuid_set(mdev, UI_BITMAP, 0UL); |
| 3351 | |
Lars Ellenberg | 62b0da3 | 2011-01-20 13:25:21 +0100 | [diff] [blame] | 3352 | drbd_print_uuids(mdev, "updated sync uuid"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3353 | drbd_start_resync(mdev, C_SYNC_TARGET); |
| 3354 | |
| 3355 | put_ldev(mdev); |
| 3356 | } else |
| 3357 | dev_err(DEV, "Ignoring SyncUUID packet!\n"); |
| 3358 | |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3359 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3360 | } |
| 3361 | |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3362 | /** |
| 3363 | * receive_bitmap_plain |
| 3364 | * |
| 3365 | * Return 0 when done, 1 when another iteration is needed, and a negative error |
| 3366 | * code upon failure. |
| 3367 | */ |
| 3368 | static int |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3369 | receive_bitmap_plain(struct drbd_conf *mdev, unsigned int data_size, |
| 3370 | unsigned long *buffer, struct bm_xfer_ctx *c) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3371 | { |
| 3372 | unsigned num_words = min_t(size_t, BM_PACKET_WORDS, c->bm_words - c->word_offset); |
| 3373 | unsigned want = num_words * sizeof(long); |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3374 | int err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3375 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3376 | if (want != data_size) { |
| 3377 | dev_err(DEV, "%s:want (%u) != data_size (%u)\n", __func__, want, data_size); |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3378 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3379 | } |
| 3380 | if (want == 0) |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3381 | return 0; |
| 3382 | err = drbd_recv(mdev, buffer, want); |
| 3383 | if (err != want) { |
| 3384 | if (err >= 0) |
| 3385 | err = -EIO; |
| 3386 | return err; |
| 3387 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3388 | |
| 3389 | drbd_bm_merge_lel(mdev, c->word_offset, num_words, buffer); |
| 3390 | |
| 3391 | c->word_offset += num_words; |
| 3392 | c->bit_offset = c->word_offset * BITS_PER_LONG; |
| 3393 | if (c->bit_offset > c->bm_bits) |
| 3394 | c->bit_offset = c->bm_bits; |
| 3395 | |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3396 | return 1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3397 | } |
| 3398 | |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3399 | /** |
| 3400 | * recv_bm_rle_bits |
| 3401 | * |
| 3402 | * Return 0 when done, 1 when another iteration is needed, and a negative error |
| 3403 | * code upon failure. |
| 3404 | */ |
| 3405 | static int |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3406 | recv_bm_rle_bits(struct drbd_conf *mdev, |
| 3407 | struct p_compressed_bm *p, |
| 3408 | struct bm_xfer_ctx *c) |
| 3409 | { |
| 3410 | struct bitstream bs; |
| 3411 | u64 look_ahead; |
| 3412 | u64 rl; |
| 3413 | u64 tmp; |
| 3414 | unsigned long s = c->bit_offset; |
| 3415 | unsigned long e; |
Lars Ellenberg | 004352f | 2010-10-05 20:13:58 +0200 | [diff] [blame] | 3416 | int len = be16_to_cpu(p->head.length) - (sizeof(*p) - sizeof(p->head)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3417 | int toggle = DCBP_get_start(p); |
| 3418 | int have; |
| 3419 | int bits; |
| 3420 | |
| 3421 | bitstream_init(&bs, p->code, len, DCBP_get_pad_bits(p)); |
| 3422 | |
| 3423 | bits = bitstream_get_bits(&bs, &look_ahead, 64); |
| 3424 | if (bits < 0) |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3425 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3426 | |
| 3427 | for (have = bits; have > 0; s += rl, toggle = !toggle) { |
| 3428 | bits = vli_decode_bits(&rl, look_ahead); |
| 3429 | if (bits <= 0) |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3430 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3431 | |
| 3432 | if (toggle) { |
| 3433 | e = s + rl -1; |
| 3434 | if (e >= c->bm_bits) { |
| 3435 | dev_err(DEV, "bitmap overflow (e:%lu) while decoding bm RLE packet\n", e); |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3436 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3437 | } |
| 3438 | _drbd_bm_set_bits(mdev, s, e); |
| 3439 | } |
| 3440 | |
| 3441 | if (have < bits) { |
| 3442 | dev_err(DEV, "bitmap decoding error: h:%d b:%d la:0x%08llx l:%u/%u\n", |
| 3443 | have, bits, look_ahead, |
| 3444 | (unsigned int)(bs.cur.b - p->code), |
| 3445 | (unsigned int)bs.buf_len); |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3446 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3447 | } |
| 3448 | look_ahead >>= bits; |
| 3449 | have -= bits; |
| 3450 | |
| 3451 | bits = bitstream_get_bits(&bs, &tmp, 64 - have); |
| 3452 | if (bits < 0) |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3453 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3454 | look_ahead |= tmp << have; |
| 3455 | have += bits; |
| 3456 | } |
| 3457 | |
| 3458 | c->bit_offset = s; |
| 3459 | bm_xfer_ctx_bit_to_word_offset(c); |
| 3460 | |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3461 | return (s != c->bm_bits); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3462 | } |
| 3463 | |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3464 | /** |
| 3465 | * decode_bitmap_c |
| 3466 | * |
| 3467 | * Return 0 when done, 1 when another iteration is needed, and a negative error |
| 3468 | * code upon failure. |
| 3469 | */ |
| 3470 | static int |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3471 | decode_bitmap_c(struct drbd_conf *mdev, |
| 3472 | struct p_compressed_bm *p, |
| 3473 | struct bm_xfer_ctx *c) |
| 3474 | { |
| 3475 | if (DCBP_get_code(p) == RLE_VLI_Bits) |
| 3476 | return recv_bm_rle_bits(mdev, p, c); |
| 3477 | |
| 3478 | /* other variants had been implemented for evaluation, |
| 3479 | * but have been dropped as this one turned out to be "best" |
| 3480 | * during all our tests. */ |
| 3481 | |
| 3482 | dev_err(DEV, "receive_bitmap_c: unknown encoding %u\n", p->encoding); |
| 3483 | drbd_force_state(mdev, NS(conn, C_PROTOCOL_ERROR)); |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3484 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3485 | } |
| 3486 | |
| 3487 | void INFO_bm_xfer_stats(struct drbd_conf *mdev, |
| 3488 | const char *direction, struct bm_xfer_ctx *c) |
| 3489 | { |
| 3490 | /* what would it take to transfer it "plaintext" */ |
Philipp Reisner | 0b70a13 | 2010-08-20 13:36:10 +0200 | [diff] [blame] | 3491 | unsigned plain = sizeof(struct p_header80) * |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3492 | ((c->bm_words+BM_PACKET_WORDS-1)/BM_PACKET_WORDS+1) |
| 3493 | + c->bm_words * sizeof(long); |
| 3494 | unsigned total = c->bytes[0] + c->bytes[1]; |
| 3495 | unsigned r; |
| 3496 | |
| 3497 | /* total can not be zero. but just in case: */ |
| 3498 | if (total == 0) |
| 3499 | return; |
| 3500 | |
| 3501 | /* don't report if not compressed */ |
| 3502 | if (total >= plain) |
| 3503 | return; |
| 3504 | |
| 3505 | /* total < plain. check for overflow, still */ |
| 3506 | r = (total > UINT_MAX/1000) ? (total / (plain/1000)) |
| 3507 | : (1000 * total / plain); |
| 3508 | |
| 3509 | if (r > 1000) |
| 3510 | r = 1000; |
| 3511 | |
| 3512 | r = 1000 - r; |
| 3513 | dev_info(DEV, "%s bitmap stats [Bytes(packets)]: plain %u(%u), RLE %u(%u), " |
| 3514 | "total %u; compression: %u.%u%%\n", |
| 3515 | direction, |
| 3516 | c->bytes[1], c->packets[1], |
| 3517 | c->bytes[0], c->packets[0], |
| 3518 | total, r/10, r % 10); |
| 3519 | } |
| 3520 | |
| 3521 | /* Since we are processing the bitfield from lower addresses to higher, |
| 3522 | it does not matter if the process it in 32 bit chunks or 64 bit |
| 3523 | chunks as long as it is little endian. (Understand it as byte stream, |
| 3524 | beginning with the lowest byte...) If we would use big endian |
| 3525 | we would need to process it from the highest address to the lowest, |
| 3526 | in order to be agnostic to the 32 vs 64 bits issue. |
| 3527 | |
| 3528 | returns 0 on failure, 1 if we successfully received it. */ |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3529 | static int receive_bitmap(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3530 | { |
| 3531 | struct bm_xfer_ctx c; |
| 3532 | void *buffer; |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3533 | int err; |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3534 | int ok = false; |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3535 | struct p_header80 *h = &mdev->data.rbuf.header.h80; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3536 | |
Lars Ellenberg | 20ceb2b | 2011-01-21 10:56:44 +0100 | [diff] [blame] | 3537 | drbd_bm_lock(mdev, "receive bitmap", BM_LOCKED_SET_ALLOWED); |
| 3538 | /* you are supposed to send additional out-of-sync information |
| 3539 | * if you actually set bits during this phase */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3540 | |
| 3541 | /* maybe we should use some per thread scratch page, |
| 3542 | * and allocate that during initial device creation? */ |
| 3543 | buffer = (unsigned long *) __get_free_page(GFP_NOIO); |
| 3544 | if (!buffer) { |
| 3545 | dev_err(DEV, "failed to allocate one page buffer in %s\n", __func__); |
| 3546 | goto out; |
| 3547 | } |
| 3548 | |
| 3549 | c = (struct bm_xfer_ctx) { |
| 3550 | .bm_bits = drbd_bm_bits(mdev), |
| 3551 | .bm_words = drbd_bm_words(mdev), |
| 3552 | }; |
| 3553 | |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3554 | for(;;) { |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3555 | if (cmd == P_BITMAP) { |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3556 | err = receive_bitmap_plain(mdev, data_size, buffer, &c); |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3557 | } else if (cmd == P_COMPRESSED_BITMAP) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3558 | /* MAYBE: sanity check that we speak proto >= 90, |
| 3559 | * and the feature is enabled! */ |
| 3560 | struct p_compressed_bm *p; |
| 3561 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3562 | if (data_size > BM_PACKET_PAYLOAD_BYTES) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3563 | dev_err(DEV, "ReportCBitmap packet too large\n"); |
| 3564 | goto out; |
| 3565 | } |
| 3566 | /* use the page buff */ |
| 3567 | p = buffer; |
| 3568 | memcpy(p, h, sizeof(*h)); |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3569 | if (drbd_recv(mdev, p->head.payload, data_size) != data_size) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3570 | goto out; |
Lars Ellenberg | 004352f | 2010-10-05 20:13:58 +0200 | [diff] [blame] | 3571 | if (data_size <= (sizeof(*p) - sizeof(p->head))) { |
| 3572 | dev_err(DEV, "ReportCBitmap packet too small (l:%u)\n", data_size); |
Andreas Gruenbacher | 78fcbda | 2010-12-10 22:18:27 +0100 | [diff] [blame] | 3573 | goto out; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3574 | } |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3575 | err = decode_bitmap_c(mdev, p, &c); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3576 | } else { |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3577 | dev_warn(DEV, "receive_bitmap: cmd neither ReportBitMap nor ReportCBitMap (is 0x%x)", cmd); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3578 | goto out; |
| 3579 | } |
| 3580 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3581 | c.packets[cmd == P_BITMAP]++; |
| 3582 | c.bytes[cmd == P_BITMAP] += sizeof(struct p_header80) + data_size; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3583 | |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3584 | if (err <= 0) { |
| 3585 | if (err < 0) |
| 3586 | goto out; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3587 | break; |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3588 | } |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3589 | if (!drbd_recv_header(mdev, &cmd, &data_size)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3590 | goto out; |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3591 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3592 | |
| 3593 | INFO_bm_xfer_stats(mdev, "receive", &c); |
| 3594 | |
| 3595 | if (mdev->state.conn == C_WF_BITMAP_T) { |
Andreas Gruenbacher | de1f8e4 | 2010-12-10 21:04:00 +0100 | [diff] [blame] | 3596 | enum drbd_state_rv rv; |
| 3597 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3598 | ok = !drbd_send_bitmap(mdev); |
| 3599 | if (!ok) |
| 3600 | goto out; |
| 3601 | /* Omit CS_ORDERED with this state transition to avoid deadlocks. */ |
Andreas Gruenbacher | de1f8e4 | 2010-12-10 21:04:00 +0100 | [diff] [blame] | 3602 | rv = _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE); |
| 3603 | D_ASSERT(rv == SS_SUCCESS); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3604 | } else if (mdev->state.conn != C_WF_BITMAP_S) { |
| 3605 | /* admin may have requested C_DISCONNECTING, |
| 3606 | * other threads may have noticed network errors */ |
| 3607 | dev_info(DEV, "unexpected cstate (%s) in receive_bitmap\n", |
| 3608 | drbd_conn_str(mdev->state.conn)); |
| 3609 | } |
| 3610 | |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3611 | ok = true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3612 | out: |
Lars Ellenberg | 20ceb2b | 2011-01-21 10:56:44 +0100 | [diff] [blame] | 3613 | drbd_bm_unlock(mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3614 | if (ok && mdev->state.conn == C_WF_BITMAP_S) |
| 3615 | drbd_start_resync(mdev, C_SYNC_SOURCE); |
| 3616 | free_page((unsigned long) buffer); |
| 3617 | return ok; |
| 3618 | } |
| 3619 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3620 | static int receive_skip(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3621 | { |
| 3622 | /* TODO zero copy sink :) */ |
| 3623 | static char sink[128]; |
| 3624 | int size, want, r; |
| 3625 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3626 | dev_warn(DEV, "skipping unknown optional packet type %d, l: %d!\n", |
| 3627 | cmd, data_size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3628 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3629 | size = data_size; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3630 | while (size > 0) { |
| 3631 | want = min_t(int, size, sizeof(sink)); |
| 3632 | r = drbd_recv(mdev, sink, want); |
| 3633 | ERR_IF(r <= 0) break; |
| 3634 | size -= r; |
| 3635 | } |
| 3636 | return size == 0; |
| 3637 | } |
| 3638 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3639 | static int receive_UnplugRemote(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3640 | { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3641 | /* Make sure we've acked all the TCP data associated |
| 3642 | * with the data requests being unplugged */ |
| 3643 | drbd_tcp_quickack(mdev->data.socket); |
| 3644 | |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3645 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3646 | } |
| 3647 | |
Philipp Reisner | 73a01a1 | 2010-10-27 14:33:00 +0200 | [diff] [blame] | 3648 | static int receive_out_of_sync(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size) |
| 3649 | { |
| 3650 | struct p_block_desc *p = &mdev->data.rbuf.block_desc; |
| 3651 | |
Lars Ellenberg | f735e363 | 2010-12-17 21:06:18 +0100 | [diff] [blame] | 3652 | switch (mdev->state.conn) { |
| 3653 | case C_WF_SYNC_UUID: |
| 3654 | case C_WF_BITMAP_T: |
| 3655 | case C_BEHIND: |
| 3656 | break; |
| 3657 | default: |
| 3658 | dev_err(DEV, "ASSERT FAILED cstate = %s, expected: WFSyncUUID|WFBitMapT|Behind\n", |
| 3659 | drbd_conn_str(mdev->state.conn)); |
| 3660 | } |
| 3661 | |
Philipp Reisner | 73a01a1 | 2010-10-27 14:33:00 +0200 | [diff] [blame] | 3662 | drbd_set_out_of_sync(mdev, be64_to_cpu(p->sector), be32_to_cpu(p->blksize)); |
| 3663 | |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3664 | return true; |
Philipp Reisner | 73a01a1 | 2010-10-27 14:33:00 +0200 | [diff] [blame] | 3665 | } |
| 3666 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3667 | typedef int (*drbd_cmd_handler_f)(struct drbd_conf *, enum drbd_packets cmd, unsigned int to_receive); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3668 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3669 | struct data_cmd { |
| 3670 | int expect_payload; |
| 3671 | size_t pkt_size; |
| 3672 | drbd_cmd_handler_f function; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3673 | }; |
| 3674 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3675 | static struct data_cmd drbd_cmd_handler[] = { |
| 3676 | [P_DATA] = { 1, sizeof(struct p_data), receive_Data }, |
| 3677 | [P_DATA_REPLY] = { 1, sizeof(struct p_data), receive_DataReply }, |
| 3678 | [P_RS_DATA_REPLY] = { 1, sizeof(struct p_data), receive_RSDataReply } , |
| 3679 | [P_BARRIER] = { 0, sizeof(struct p_barrier), receive_Barrier } , |
| 3680 | [P_BITMAP] = { 1, sizeof(struct p_header80), receive_bitmap } , |
| 3681 | [P_COMPRESSED_BITMAP] = { 1, sizeof(struct p_header80), receive_bitmap } , |
| 3682 | [P_UNPLUG_REMOTE] = { 0, sizeof(struct p_header80), receive_UnplugRemote }, |
| 3683 | [P_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest }, |
| 3684 | [P_RS_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest }, |
| 3685 | [P_SYNC_PARAM] = { 1, sizeof(struct p_header80), receive_SyncParam }, |
| 3686 | [P_SYNC_PARAM89] = { 1, sizeof(struct p_header80), receive_SyncParam }, |
| 3687 | [P_PROTOCOL] = { 1, sizeof(struct p_protocol), receive_protocol }, |
| 3688 | [P_UUIDS] = { 0, sizeof(struct p_uuids), receive_uuids }, |
| 3689 | [P_SIZES] = { 0, sizeof(struct p_sizes), receive_sizes }, |
| 3690 | [P_STATE] = { 0, sizeof(struct p_state), receive_state }, |
| 3691 | [P_STATE_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_state }, |
| 3692 | [P_SYNC_UUID] = { 0, sizeof(struct p_rs_uuid), receive_sync_uuid }, |
| 3693 | [P_OV_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest }, |
| 3694 | [P_OV_REPLY] = { 1, sizeof(struct p_block_req), receive_DataRequest }, |
| 3695 | [P_CSUM_RS_REQUEST] = { 1, sizeof(struct p_block_req), receive_DataRequest }, |
| 3696 | [P_DELAY_PROBE] = { 0, sizeof(struct p_delay_probe93), receive_skip }, |
Philipp Reisner | 73a01a1 | 2010-10-27 14:33:00 +0200 | [diff] [blame] | 3697 | [P_OUT_OF_SYNC] = { 0, sizeof(struct p_block_desc), receive_out_of_sync }, |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3698 | /* anything missing from this table is in |
| 3699 | * the asender_tbl, see get_asender_cmd */ |
| 3700 | [P_MAX_CMD] = { 0, 0, NULL }, |
| 3701 | }; |
| 3702 | |
| 3703 | /* All handler functions that expect a sub-header get that sub-heder in |
| 3704 | mdev->data.rbuf.header.head.payload. |
| 3705 | |
| 3706 | Usually in mdev->data.rbuf.header.head the callback can find the usual |
| 3707 | p_header, but they may not rely on that. Since there is also p_header95 ! |
| 3708 | */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3709 | |
| 3710 | static void drbdd(struct drbd_conf *mdev) |
| 3711 | { |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3712 | union p_header *header = &mdev->data.rbuf.header; |
| 3713 | unsigned int packet_size; |
| 3714 | enum drbd_packets cmd; |
| 3715 | size_t shs; /* sub header size */ |
| 3716 | int rv; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3717 | |
| 3718 | while (get_t_state(&mdev->receiver) == Running) { |
| 3719 | drbd_thread_current_set_cpu(mdev); |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3720 | if (!drbd_recv_header(mdev, &cmd, &packet_size)) |
| 3721 | goto err_out; |
| 3722 | |
| 3723 | if (unlikely(cmd >= P_MAX_CMD || !drbd_cmd_handler[cmd].function)) { |
| 3724 | dev_err(DEV, "unknown packet type %d, l: %d!\n", cmd, packet_size); |
| 3725 | goto err_out; |
Lars Ellenberg | 0b33a91 | 2009-11-16 15:58:04 +0100 | [diff] [blame] | 3726 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3727 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3728 | shs = drbd_cmd_handler[cmd].pkt_size - sizeof(union p_header); |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3729 | if (packet_size - shs > 0 && !drbd_cmd_handler[cmd].expect_payload) { |
| 3730 | dev_err(DEV, "No payload expected %s l:%d\n", cmdname(cmd), packet_size); |
| 3731 | goto err_out; |
| 3732 | } |
| 3733 | |
Lars Ellenberg | c13f7e1 | 2010-10-29 23:32:01 +0200 | [diff] [blame] | 3734 | if (shs) { |
| 3735 | rv = drbd_recv(mdev, &header->h80.payload, shs); |
| 3736 | if (unlikely(rv != shs)) { |
Lars Ellenberg | 0ddc554 | 2011-01-21 12:35:15 +0100 | [diff] [blame] | 3737 | if (!signal_pending(current)) |
| 3738 | dev_warn(DEV, "short read while reading sub header: rv=%d\n", rv); |
Lars Ellenberg | c13f7e1 | 2010-10-29 23:32:01 +0200 | [diff] [blame] | 3739 | goto err_out; |
| 3740 | } |
| 3741 | } |
| 3742 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3743 | rv = drbd_cmd_handler[cmd].function(mdev, cmd, packet_size - shs); |
| 3744 | |
| 3745 | if (unlikely(!rv)) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3746 | dev_err(DEV, "error receiving %s, l: %d!\n", |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3747 | cmdname(cmd), packet_size); |
| 3748 | goto err_out; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3749 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3750 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3751 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3752 | if (0) { |
| 3753 | err_out: |
| 3754 | drbd_force_state(mdev, NS(conn, C_PROTOCOL_ERROR)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3755 | } |
Lars Ellenberg | 856c50c | 2010-10-14 13:37:40 +0200 | [diff] [blame] | 3756 | /* If we leave here, we probably want to update at least the |
| 3757 | * "Connected" indicator on stable storage. Do so explicitly here. */ |
| 3758 | drbd_md_sync(mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3759 | } |
| 3760 | |
| 3761 | void drbd_flush_workqueue(struct drbd_conf *mdev) |
| 3762 | { |
| 3763 | struct drbd_wq_barrier barr; |
| 3764 | |
| 3765 | barr.w.cb = w_prev_work_done; |
| 3766 | init_completion(&barr.done); |
| 3767 | drbd_queue_work(&mdev->data.work, &barr.w); |
| 3768 | wait_for_completion(&barr.done); |
| 3769 | } |
| 3770 | |
| 3771 | static void drbd_disconnect(struct drbd_conf *mdev) |
| 3772 | { |
| 3773 | enum drbd_fencing_p fp; |
| 3774 | union drbd_state os, ns; |
| 3775 | int rv = SS_UNKNOWN_ERROR; |
| 3776 | unsigned int i; |
| 3777 | |
| 3778 | if (mdev->state.conn == C_STANDALONE) |
| 3779 | return; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3780 | |
| 3781 | /* asender does not clean up anything. it must not interfere, either */ |
| 3782 | drbd_thread_stop(&mdev->asender); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3783 | drbd_free_sock(mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3784 | |
Philipp Reisner | 8571957 | 2010-07-21 10:20:17 +0200 | [diff] [blame] | 3785 | /* wait for current activity to cease. */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3786 | spin_lock_irq(&mdev->req_lock); |
| 3787 | _drbd_wait_ee_list_empty(mdev, &mdev->active_ee); |
| 3788 | _drbd_wait_ee_list_empty(mdev, &mdev->sync_ee); |
| 3789 | _drbd_wait_ee_list_empty(mdev, &mdev->read_ee); |
| 3790 | spin_unlock_irq(&mdev->req_lock); |
| 3791 | |
| 3792 | /* We do not have data structures that would allow us to |
| 3793 | * get the rs_pending_cnt down to 0 again. |
| 3794 | * * On C_SYNC_TARGET we do not have any data structures describing |
| 3795 | * the pending RSDataRequest's we have sent. |
| 3796 | * * On C_SYNC_SOURCE there is no data structure that tracks |
| 3797 | * the P_RS_DATA_REPLY blocks that we sent to the SyncTarget. |
| 3798 | * And no, it is not the sum of the reference counts in the |
| 3799 | * resync_LRU. The resync_LRU tracks the whole operation including |
| 3800 | * the disk-IO, while the rs_pending_cnt only tracks the blocks |
| 3801 | * on the fly. */ |
| 3802 | drbd_rs_cancel_all(mdev); |
| 3803 | mdev->rs_total = 0; |
| 3804 | mdev->rs_failed = 0; |
| 3805 | atomic_set(&mdev->rs_pending_cnt, 0); |
| 3806 | wake_up(&mdev->misc_wait); |
| 3807 | |
Philipp Reisner | 7fde2be | 2011-03-01 11:08:28 +0100 | [diff] [blame] | 3808 | del_timer(&mdev->request_timer); |
| 3809 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3810 | /* make sure syncer is stopped and w_resume_next_sg queued */ |
| 3811 | del_timer_sync(&mdev->resync_timer); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3812 | resync_timer_fn((unsigned long)mdev); |
| 3813 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3814 | /* wait for all w_e_end_data_req, w_e_end_rsdata_req, w_send_barrier, |
| 3815 | * w_make_resync_request etc. which may still be on the worker queue |
| 3816 | * to be "canceled" */ |
| 3817 | drbd_flush_workqueue(mdev); |
| 3818 | |
| 3819 | /* This also does reclaim_net_ee(). If we do this too early, we might |
| 3820 | * miss some resync ee and pages.*/ |
| 3821 | drbd_process_done_ee(mdev); |
| 3822 | |
| 3823 | kfree(mdev->p_uuid); |
| 3824 | mdev->p_uuid = NULL; |
| 3825 | |
Philipp Reisner | fb22c40 | 2010-09-08 23:20:21 +0200 | [diff] [blame] | 3826 | if (!is_susp(mdev->state)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3827 | tl_clear(mdev); |
| 3828 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3829 | dev_info(DEV, "Connection closed\n"); |
| 3830 | |
| 3831 | drbd_md_sync(mdev); |
| 3832 | |
| 3833 | fp = FP_DONT_CARE; |
| 3834 | if (get_ldev(mdev)) { |
| 3835 | fp = mdev->ldev->dc.fencing; |
| 3836 | put_ldev(mdev); |
| 3837 | } |
| 3838 | |
Philipp Reisner | 87f7be4 | 2010-06-11 13:56:33 +0200 | [diff] [blame] | 3839 | if (mdev->state.role == R_PRIMARY && fp >= FP_RESOURCE && mdev->state.pdsk >= D_UNKNOWN) |
| 3840 | drbd_try_outdate_peer_async(mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3841 | |
| 3842 | spin_lock_irq(&mdev->req_lock); |
| 3843 | os = mdev->state; |
| 3844 | if (os.conn >= C_UNCONNECTED) { |
| 3845 | /* Do not restart in case we are C_DISCONNECTING */ |
| 3846 | ns = os; |
| 3847 | ns.conn = C_UNCONNECTED; |
| 3848 | rv = _drbd_set_state(mdev, ns, CS_VERBOSE, NULL); |
| 3849 | } |
| 3850 | spin_unlock_irq(&mdev->req_lock); |
| 3851 | |
| 3852 | if (os.conn == C_DISCONNECTING) { |
Philipp Reisner | 84dfb9f | 2010-06-23 11:20:05 +0200 | [diff] [blame] | 3853 | wait_event(mdev->net_cnt_wait, atomic_read(&mdev->net_cnt) == 0); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3854 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3855 | crypto_free_hash(mdev->cram_hmac_tfm); |
| 3856 | mdev->cram_hmac_tfm = NULL; |
| 3857 | |
| 3858 | kfree(mdev->net_conf); |
| 3859 | mdev->net_conf = NULL; |
| 3860 | drbd_request_state(mdev, NS(conn, C_STANDALONE)); |
| 3861 | } |
| 3862 | |
Lars Ellenberg | 20ceb2b | 2011-01-21 10:56:44 +0100 | [diff] [blame] | 3863 | /* serialize with bitmap writeout triggered by the state change, |
| 3864 | * if any. */ |
| 3865 | wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags)); |
| 3866 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3867 | /* tcp_close and release of sendpage pages can be deferred. I don't |
| 3868 | * want to use SO_LINGER, because apparently it can be deferred for |
| 3869 | * more than 20 seconds (longest time I checked). |
| 3870 | * |
| 3871 | * Actually we don't care for exactly when the network stack does its |
| 3872 | * put_page(), but release our reference on these pages right here. |
| 3873 | */ |
| 3874 | i = drbd_release_ee(mdev, &mdev->net_ee); |
| 3875 | if (i) |
| 3876 | dev_info(DEV, "net_ee not empty, killed %u entries\n", i); |
Lars Ellenberg | 435f074 | 2010-09-06 12:30:25 +0200 | [diff] [blame] | 3877 | i = atomic_read(&mdev->pp_in_use_by_net); |
| 3878 | if (i) |
| 3879 | dev_info(DEV, "pp_in_use_by_net = %d, expected 0\n", i); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3880 | i = atomic_read(&mdev->pp_in_use); |
| 3881 | if (i) |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 3882 | dev_info(DEV, "pp_in_use = %d, expected 0\n", i); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3883 | |
| 3884 | D_ASSERT(list_empty(&mdev->read_ee)); |
| 3885 | D_ASSERT(list_empty(&mdev->active_ee)); |
| 3886 | D_ASSERT(list_empty(&mdev->sync_ee)); |
| 3887 | D_ASSERT(list_empty(&mdev->done_ee)); |
| 3888 | |
| 3889 | /* ok, no more ee's on the fly, it is safe to reset the epoch_size */ |
| 3890 | atomic_set(&mdev->current_epoch->epoch_size, 0); |
| 3891 | D_ASSERT(list_empty(&mdev->current_epoch->list)); |
| 3892 | } |
| 3893 | |
| 3894 | /* |
| 3895 | * We support PRO_VERSION_MIN to PRO_VERSION_MAX. The protocol version |
| 3896 | * we can agree on is stored in agreed_pro_version. |
| 3897 | * |
| 3898 | * feature flags and the reserved array should be enough room for future |
| 3899 | * enhancements of the handshake protocol, and possible plugins... |
| 3900 | * |
| 3901 | * for now, they are expected to be zero, but ignored. |
| 3902 | */ |
| 3903 | static int drbd_send_handshake(struct drbd_conf *mdev) |
| 3904 | { |
| 3905 | /* ASSERT current == mdev->receiver ... */ |
| 3906 | struct p_handshake *p = &mdev->data.sbuf.handshake; |
| 3907 | int ok; |
| 3908 | |
| 3909 | if (mutex_lock_interruptible(&mdev->data.mutex)) { |
| 3910 | dev_err(DEV, "interrupted during initial handshake\n"); |
| 3911 | return 0; /* interrupted. not ok. */ |
| 3912 | } |
| 3913 | |
| 3914 | if (mdev->data.socket == NULL) { |
| 3915 | mutex_unlock(&mdev->data.mutex); |
| 3916 | return 0; |
| 3917 | } |
| 3918 | |
| 3919 | memset(p, 0, sizeof(*p)); |
| 3920 | p->protocol_min = cpu_to_be32(PRO_VERSION_MIN); |
| 3921 | p->protocol_max = cpu_to_be32(PRO_VERSION_MAX); |
| 3922 | ok = _drbd_send_cmd( mdev, mdev->data.socket, P_HAND_SHAKE, |
Philipp Reisner | 0b70a13 | 2010-08-20 13:36:10 +0200 | [diff] [blame] | 3923 | (struct p_header80 *)p, sizeof(*p), 0 ); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3924 | mutex_unlock(&mdev->data.mutex); |
| 3925 | return ok; |
| 3926 | } |
| 3927 | |
| 3928 | /* |
| 3929 | * return values: |
| 3930 | * 1 yes, we have a valid connection |
| 3931 | * 0 oops, did not work out, please try again |
| 3932 | * -1 peer talks different language, |
| 3933 | * no point in trying again, please go standalone. |
| 3934 | */ |
| 3935 | static int drbd_do_handshake(struct drbd_conf *mdev) |
| 3936 | { |
| 3937 | /* ASSERT current == mdev->receiver ... */ |
| 3938 | struct p_handshake *p = &mdev->data.rbuf.handshake; |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3939 | const int expect = sizeof(struct p_handshake) - sizeof(struct p_header80); |
| 3940 | unsigned int length; |
| 3941 | enum drbd_packets cmd; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3942 | int rv; |
| 3943 | |
| 3944 | rv = drbd_send_handshake(mdev); |
| 3945 | if (!rv) |
| 3946 | return 0; |
| 3947 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3948 | rv = drbd_recv_header(mdev, &cmd, &length); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3949 | if (!rv) |
| 3950 | return 0; |
| 3951 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3952 | if (cmd != P_HAND_SHAKE) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3953 | dev_err(DEV, "expected HandShake packet, received: %s (0x%04x)\n", |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3954 | cmdname(cmd), cmd); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3955 | return -1; |
| 3956 | } |
| 3957 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3958 | if (length != expect) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3959 | dev_err(DEV, "expected HandShake length: %u, received: %u\n", |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3960 | expect, length); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3961 | return -1; |
| 3962 | } |
| 3963 | |
| 3964 | rv = drbd_recv(mdev, &p->head.payload, expect); |
| 3965 | |
| 3966 | if (rv != expect) { |
Lars Ellenberg | 0ddc554 | 2011-01-21 12:35:15 +0100 | [diff] [blame] | 3967 | if (!signal_pending(current)) |
| 3968 | dev_warn(DEV, "short read receiving handshake packet: l=%u\n", rv); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3969 | return 0; |
| 3970 | } |
| 3971 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3972 | p->protocol_min = be32_to_cpu(p->protocol_min); |
| 3973 | p->protocol_max = be32_to_cpu(p->protocol_max); |
| 3974 | if (p->protocol_max == 0) |
| 3975 | p->protocol_max = p->protocol_min; |
| 3976 | |
| 3977 | if (PRO_VERSION_MAX < p->protocol_min || |
| 3978 | PRO_VERSION_MIN > p->protocol_max) |
| 3979 | goto incompat; |
| 3980 | |
| 3981 | mdev->agreed_pro_version = min_t(int, PRO_VERSION_MAX, p->protocol_max); |
| 3982 | |
| 3983 | dev_info(DEV, "Handshake successful: " |
| 3984 | "Agreed network protocol version %d\n", mdev->agreed_pro_version); |
| 3985 | |
| 3986 | return 1; |
| 3987 | |
| 3988 | incompat: |
| 3989 | dev_err(DEV, "incompatible DRBD dialects: " |
| 3990 | "I support %d-%d, peer supports %d-%d\n", |
| 3991 | PRO_VERSION_MIN, PRO_VERSION_MAX, |
| 3992 | p->protocol_min, p->protocol_max); |
| 3993 | return -1; |
| 3994 | } |
| 3995 | |
| 3996 | #if !defined(CONFIG_CRYPTO_HMAC) && !defined(CONFIG_CRYPTO_HMAC_MODULE) |
| 3997 | static int drbd_do_auth(struct drbd_conf *mdev) |
| 3998 | { |
| 3999 | dev_err(DEV, "This kernel was build without CONFIG_CRYPTO_HMAC.\n"); |
| 4000 | dev_err(DEV, "You need to disable 'cram-hmac-alg' in drbd.conf.\n"); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 4001 | return -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4002 | } |
| 4003 | #else |
| 4004 | #define CHALLENGE_LEN 64 |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 4005 | |
| 4006 | /* Return value: |
| 4007 | 1 - auth succeeded, |
| 4008 | 0 - failed, try again (network error), |
| 4009 | -1 - auth failed, don't try again. |
| 4010 | */ |
| 4011 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4012 | static int drbd_do_auth(struct drbd_conf *mdev) |
| 4013 | { |
| 4014 | char my_challenge[CHALLENGE_LEN]; /* 64 Bytes... */ |
| 4015 | struct scatterlist sg; |
| 4016 | char *response = NULL; |
| 4017 | char *right_response = NULL; |
| 4018 | char *peers_ch = NULL; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4019 | unsigned int key_len = strlen(mdev->net_conf->shared_secret); |
| 4020 | unsigned int resp_size; |
| 4021 | struct hash_desc desc; |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4022 | enum drbd_packets cmd; |
| 4023 | unsigned int length; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4024 | int rv; |
| 4025 | |
| 4026 | desc.tfm = mdev->cram_hmac_tfm; |
| 4027 | desc.flags = 0; |
| 4028 | |
| 4029 | rv = crypto_hash_setkey(mdev->cram_hmac_tfm, |
| 4030 | (u8 *)mdev->net_conf->shared_secret, key_len); |
| 4031 | if (rv) { |
| 4032 | dev_err(DEV, "crypto_hash_setkey() failed with %d\n", rv); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 4033 | rv = -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4034 | goto fail; |
| 4035 | } |
| 4036 | |
| 4037 | get_random_bytes(my_challenge, CHALLENGE_LEN); |
| 4038 | |
| 4039 | rv = drbd_send_cmd2(mdev, P_AUTH_CHALLENGE, my_challenge, CHALLENGE_LEN); |
| 4040 | if (!rv) |
| 4041 | goto fail; |
| 4042 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4043 | rv = drbd_recv_header(mdev, &cmd, &length); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4044 | if (!rv) |
| 4045 | goto fail; |
| 4046 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4047 | if (cmd != P_AUTH_CHALLENGE) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4048 | dev_err(DEV, "expected AuthChallenge packet, received: %s (0x%04x)\n", |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4049 | cmdname(cmd), cmd); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4050 | rv = 0; |
| 4051 | goto fail; |
| 4052 | } |
| 4053 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4054 | if (length > CHALLENGE_LEN * 2) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4055 | dev_err(DEV, "expected AuthChallenge payload too big.\n"); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 4056 | rv = -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4057 | goto fail; |
| 4058 | } |
| 4059 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4060 | peers_ch = kmalloc(length, GFP_NOIO); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4061 | if (peers_ch == NULL) { |
| 4062 | dev_err(DEV, "kmalloc of peers_ch failed\n"); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 4063 | rv = -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4064 | goto fail; |
| 4065 | } |
| 4066 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4067 | rv = drbd_recv(mdev, peers_ch, length); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4068 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4069 | if (rv != length) { |
Lars Ellenberg | 0ddc554 | 2011-01-21 12:35:15 +0100 | [diff] [blame] | 4070 | if (!signal_pending(current)) |
| 4071 | dev_warn(DEV, "short read AuthChallenge: l=%u\n", rv); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4072 | rv = 0; |
| 4073 | goto fail; |
| 4074 | } |
| 4075 | |
| 4076 | resp_size = crypto_hash_digestsize(mdev->cram_hmac_tfm); |
| 4077 | response = kmalloc(resp_size, GFP_NOIO); |
| 4078 | if (response == NULL) { |
| 4079 | dev_err(DEV, "kmalloc of response failed\n"); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 4080 | rv = -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4081 | goto fail; |
| 4082 | } |
| 4083 | |
| 4084 | sg_init_table(&sg, 1); |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4085 | sg_set_buf(&sg, peers_ch, length); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4086 | |
| 4087 | rv = crypto_hash_digest(&desc, &sg, sg.length, response); |
| 4088 | if (rv) { |
| 4089 | dev_err(DEV, "crypto_hash_digest() failed with %d\n", rv); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 4090 | rv = -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4091 | goto fail; |
| 4092 | } |
| 4093 | |
| 4094 | rv = drbd_send_cmd2(mdev, P_AUTH_RESPONSE, response, resp_size); |
| 4095 | if (!rv) |
| 4096 | goto fail; |
| 4097 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4098 | rv = drbd_recv_header(mdev, &cmd, &length); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4099 | if (!rv) |
| 4100 | goto fail; |
| 4101 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4102 | if (cmd != P_AUTH_RESPONSE) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4103 | dev_err(DEV, "expected AuthResponse packet, received: %s (0x%04x)\n", |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4104 | cmdname(cmd), cmd); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4105 | rv = 0; |
| 4106 | goto fail; |
| 4107 | } |
| 4108 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4109 | if (length != resp_size) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4110 | dev_err(DEV, "expected AuthResponse payload of wrong size\n"); |
| 4111 | rv = 0; |
| 4112 | goto fail; |
| 4113 | } |
| 4114 | |
| 4115 | rv = drbd_recv(mdev, response , resp_size); |
| 4116 | |
| 4117 | if (rv != resp_size) { |
Lars Ellenberg | 0ddc554 | 2011-01-21 12:35:15 +0100 | [diff] [blame] | 4118 | if (!signal_pending(current)) |
| 4119 | dev_warn(DEV, "short read receiving AuthResponse: l=%u\n", rv); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4120 | rv = 0; |
| 4121 | goto fail; |
| 4122 | } |
| 4123 | |
| 4124 | right_response = kmalloc(resp_size, GFP_NOIO); |
Julia Lawall | 2d1ee87 | 2009-12-27 22:27:11 +0100 | [diff] [blame] | 4125 | if (right_response == NULL) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4126 | dev_err(DEV, "kmalloc of right_response failed\n"); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 4127 | rv = -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4128 | goto fail; |
| 4129 | } |
| 4130 | |
| 4131 | sg_set_buf(&sg, my_challenge, CHALLENGE_LEN); |
| 4132 | |
| 4133 | rv = crypto_hash_digest(&desc, &sg, sg.length, right_response); |
| 4134 | if (rv) { |
| 4135 | dev_err(DEV, "crypto_hash_digest() failed with %d\n", rv); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 4136 | rv = -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4137 | goto fail; |
| 4138 | } |
| 4139 | |
| 4140 | rv = !memcmp(response, right_response, resp_size); |
| 4141 | |
| 4142 | if (rv) |
| 4143 | dev_info(DEV, "Peer authenticated using %d bytes of '%s' HMAC\n", |
| 4144 | resp_size, mdev->net_conf->cram_hmac_alg); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 4145 | else |
| 4146 | rv = -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4147 | |
| 4148 | fail: |
| 4149 | kfree(peers_ch); |
| 4150 | kfree(response); |
| 4151 | kfree(right_response); |
| 4152 | |
| 4153 | return rv; |
| 4154 | } |
| 4155 | #endif |
| 4156 | |
| 4157 | int drbdd_init(struct drbd_thread *thi) |
| 4158 | { |
| 4159 | struct drbd_conf *mdev = thi->mdev; |
| 4160 | unsigned int minor = mdev_to_minor(mdev); |
| 4161 | int h; |
| 4162 | |
| 4163 | sprintf(current->comm, "drbd%d_receiver", minor); |
| 4164 | |
| 4165 | dev_info(DEV, "receiver (re)started\n"); |
| 4166 | |
| 4167 | do { |
| 4168 | h = drbd_connect(mdev); |
| 4169 | if (h == 0) { |
| 4170 | drbd_disconnect(mdev); |
Philipp Reisner | 20ee639 | 2011-01-18 15:28:59 +0100 | [diff] [blame] | 4171 | schedule_timeout_interruptible(HZ); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4172 | } |
| 4173 | if (h == -1) { |
| 4174 | dev_warn(DEV, "Discarding network configuration.\n"); |
| 4175 | drbd_force_state(mdev, NS(conn, C_DISCONNECTING)); |
| 4176 | } |
| 4177 | } while (h == 0); |
| 4178 | |
| 4179 | if (h > 0) { |
| 4180 | if (get_net_conf(mdev)) { |
| 4181 | drbdd(mdev); |
| 4182 | put_net_conf(mdev); |
| 4183 | } |
| 4184 | } |
| 4185 | |
| 4186 | drbd_disconnect(mdev); |
| 4187 | |
| 4188 | dev_info(DEV, "receiver terminated\n"); |
| 4189 | return 0; |
| 4190 | } |
| 4191 | |
| 4192 | /* ********* acknowledge sender ******** */ |
| 4193 | |
Philipp Reisner | 0b70a13 | 2010-08-20 13:36:10 +0200 | [diff] [blame] | 4194 | static int got_RqSReply(struct drbd_conf *mdev, struct p_header80 *h) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4195 | { |
| 4196 | struct p_req_state_reply *p = (struct p_req_state_reply *)h; |
| 4197 | |
| 4198 | int retcode = be32_to_cpu(p->retcode); |
| 4199 | |
| 4200 | if (retcode >= SS_SUCCESS) { |
| 4201 | set_bit(CL_ST_CHG_SUCCESS, &mdev->flags); |
| 4202 | } else { |
| 4203 | set_bit(CL_ST_CHG_FAIL, &mdev->flags); |
| 4204 | dev_err(DEV, "Requested state change failed by peer: %s (%d)\n", |
| 4205 | drbd_set_st_err_str(retcode), retcode); |
| 4206 | } |
| 4207 | wake_up(&mdev->state_wait); |
| 4208 | |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 4209 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4210 | } |
| 4211 | |
Philipp Reisner | 0b70a13 | 2010-08-20 13:36:10 +0200 | [diff] [blame] | 4212 | static int got_Ping(struct drbd_conf *mdev, struct p_header80 *h) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4213 | { |
| 4214 | return drbd_send_ping_ack(mdev); |
| 4215 | |
| 4216 | } |
| 4217 | |
Philipp Reisner | 0b70a13 | 2010-08-20 13:36:10 +0200 | [diff] [blame] | 4218 | static int got_PingAck(struct drbd_conf *mdev, struct p_header80 *h) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4219 | { |
| 4220 | /* restore idle timeout */ |
| 4221 | mdev->meta.socket->sk->sk_rcvtimeo = mdev->net_conf->ping_int*HZ; |
Philipp Reisner | 309d160 | 2010-03-02 15:03:44 +0100 | [diff] [blame] | 4222 | if (!test_and_set_bit(GOT_PING_ACK, &mdev->flags)) |
| 4223 | wake_up(&mdev->misc_wait); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4224 | |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 4225 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4226 | } |
| 4227 | |
Philipp Reisner | 0b70a13 | 2010-08-20 13:36:10 +0200 | [diff] [blame] | 4228 | static int got_IsInSync(struct drbd_conf *mdev, struct p_header80 *h) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4229 | { |
| 4230 | struct p_block_ack *p = (struct p_block_ack *)h; |
| 4231 | sector_t sector = be64_to_cpu(p->sector); |
| 4232 | int blksize = be32_to_cpu(p->blksize); |
| 4233 | |
| 4234 | D_ASSERT(mdev->agreed_pro_version >= 89); |
| 4235 | |
| 4236 | update_peer_seq(mdev, be32_to_cpu(p->seq_num)); |
| 4237 | |
Lars Ellenberg | 1d53f09 | 2010-09-05 01:13:24 +0200 | [diff] [blame] | 4238 | if (get_ldev(mdev)) { |
| 4239 | drbd_rs_complete_io(mdev, sector); |
| 4240 | drbd_set_in_sync(mdev, sector, blksize); |
| 4241 | /* rs_same_csums is supposed to count in units of BM_BLOCK_SIZE */ |
| 4242 | mdev->rs_same_csum += (blksize >> BM_BLOCK_SHIFT); |
| 4243 | put_ldev(mdev); |
| 4244 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4245 | dec_rs_pending(mdev); |
Philipp Reisner | 778f271 | 2010-07-06 11:14:00 +0200 | [diff] [blame] | 4246 | atomic_add(blksize >> 9, &mdev->rs_sect_in); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4247 | |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 4248 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4249 | } |
| 4250 | |
Andreas Gruenbacher | bc9c5c4 | 2011-01-21 18:00:55 +0100 | [diff] [blame] | 4251 | static int |
| 4252 | validate_req_change_req_state(struct drbd_conf *mdev, u64 id, sector_t sector, |
| 4253 | struct rb_root *root, const char *func, |
| 4254 | enum drbd_req_event what, bool missing_ok) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4255 | { |
| 4256 | struct drbd_request *req; |
| 4257 | struct bio_and_error m; |
| 4258 | |
| 4259 | spin_lock_irq(&mdev->req_lock); |
Andreas Gruenbacher | bc9c5c4 | 2011-01-21 18:00:55 +0100 | [diff] [blame] | 4260 | req = find_request(mdev, root, id, sector, missing_ok, func); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4261 | if (unlikely(!req)) { |
| 4262 | spin_unlock_irq(&mdev->req_lock); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 4263 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4264 | } |
| 4265 | __req_mod(req, what, &m); |
| 4266 | spin_unlock_irq(&mdev->req_lock); |
| 4267 | |
| 4268 | if (m.bio) |
| 4269 | complete_master_bio(mdev, &m); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 4270 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4271 | } |
| 4272 | |
Philipp Reisner | 0b70a13 | 2010-08-20 13:36:10 +0200 | [diff] [blame] | 4273 | static int got_BlockAck(struct drbd_conf *mdev, struct p_header80 *h) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4274 | { |
| 4275 | struct p_block_ack *p = (struct p_block_ack *)h; |
| 4276 | sector_t sector = be64_to_cpu(p->sector); |
| 4277 | int blksize = be32_to_cpu(p->blksize); |
| 4278 | enum drbd_req_event what; |
| 4279 | |
| 4280 | update_peer_seq(mdev, be32_to_cpu(p->seq_num)); |
| 4281 | |
Andreas Gruenbacher | 579b57e | 2011-01-13 18:40:57 +0100 | [diff] [blame] | 4282 | if (p->block_id == ID_SYNCER) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4283 | drbd_set_in_sync(mdev, sector, blksize); |
| 4284 | dec_rs_pending(mdev); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 4285 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4286 | } |
| 4287 | switch (be16_to_cpu(h->command)) { |
| 4288 | case P_RS_WRITE_ACK: |
| 4289 | D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_C); |
| 4290 | what = write_acked_by_peer_and_sis; |
| 4291 | break; |
| 4292 | case P_WRITE_ACK: |
| 4293 | D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_C); |
| 4294 | what = write_acked_by_peer; |
| 4295 | break; |
| 4296 | case P_RECV_ACK: |
| 4297 | D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_B); |
| 4298 | what = recv_acked_by_peer; |
| 4299 | break; |
| 4300 | case P_DISCARD_ACK: |
| 4301 | D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_C); |
| 4302 | what = conflict_discarded_by_peer; |
| 4303 | break; |
| 4304 | default: |
| 4305 | D_ASSERT(0); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 4306 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4307 | } |
| 4308 | |
| 4309 | return validate_req_change_req_state(mdev, p->block_id, sector, |
Andreas Gruenbacher | bc9c5c4 | 2011-01-21 18:00:55 +0100 | [diff] [blame] | 4310 | &mdev->write_requests, __func__, |
| 4311 | what, false); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4312 | } |
| 4313 | |
Philipp Reisner | 0b70a13 | 2010-08-20 13:36:10 +0200 | [diff] [blame] | 4314 | static int got_NegAck(struct drbd_conf *mdev, struct p_header80 *h) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4315 | { |
| 4316 | struct p_block_ack *p = (struct p_block_ack *)h; |
| 4317 | sector_t sector = be64_to_cpu(p->sector); |
Philipp Reisner | 2deb833 | 2011-01-17 18:39:18 +0100 | [diff] [blame] | 4318 | int size = be32_to_cpu(p->blksize); |
Andreas Gruenbacher | c3afd8f | 2011-01-20 22:25:40 +0100 | [diff] [blame] | 4319 | bool missing_ok = mdev->net_conf->wire_protocol == DRBD_PROT_A || |
| 4320 | mdev->net_conf->wire_protocol == DRBD_PROT_B; |
| 4321 | bool found; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4322 | |
| 4323 | update_peer_seq(mdev, be32_to_cpu(p->seq_num)); |
| 4324 | |
Andreas Gruenbacher | 579b57e | 2011-01-13 18:40:57 +0100 | [diff] [blame] | 4325 | if (p->block_id == ID_SYNCER) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4326 | dec_rs_pending(mdev); |
| 4327 | drbd_rs_failed_io(mdev, sector, size); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 4328 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4329 | } |
Philipp Reisner | 2deb833 | 2011-01-17 18:39:18 +0100 | [diff] [blame] | 4330 | |
Andreas Gruenbacher | c3afd8f | 2011-01-20 22:25:40 +0100 | [diff] [blame] | 4331 | found = validate_req_change_req_state(mdev, p->block_id, sector, |
Andreas Gruenbacher | bc9c5c4 | 2011-01-21 18:00:55 +0100 | [diff] [blame] | 4332 | &mdev->write_requests, __func__, |
Andreas Gruenbacher | c3afd8f | 2011-01-20 22:25:40 +0100 | [diff] [blame] | 4333 | neg_acked, missing_ok); |
| 4334 | if (!found) { |
| 4335 | /* Protocol A has no P_WRITE_ACKs, but has P_NEG_ACKs. |
| 4336 | The master bio might already be completed, therefore the |
| 4337 | request is no longer in the collision hash. */ |
| 4338 | /* In Protocol B we might already have got a P_RECV_ACK |
| 4339 | but then get a P_NEG_ACK afterwards. */ |
| 4340 | if (!missing_ok) |
Philipp Reisner | 2deb833 | 2011-01-17 18:39:18 +0100 | [diff] [blame] | 4341 | return false; |
Andreas Gruenbacher | c3afd8f | 2011-01-20 22:25:40 +0100 | [diff] [blame] | 4342 | drbd_set_out_of_sync(mdev, sector, size); |
Philipp Reisner | 2deb833 | 2011-01-17 18:39:18 +0100 | [diff] [blame] | 4343 | } |
Philipp Reisner | 2deb833 | 2011-01-17 18:39:18 +0100 | [diff] [blame] | 4344 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4345 | } |
| 4346 | |
Philipp Reisner | 0b70a13 | 2010-08-20 13:36:10 +0200 | [diff] [blame] | 4347 | static int got_NegDReply(struct drbd_conf *mdev, struct p_header80 *h) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4348 | { |
| 4349 | struct p_block_ack *p = (struct p_block_ack *)h; |
| 4350 | sector_t sector = be64_to_cpu(p->sector); |
| 4351 | |
| 4352 | update_peer_seq(mdev, be32_to_cpu(p->seq_num)); |
| 4353 | dev_err(DEV, "Got NegDReply; Sector %llus, len %u; Fail original request.\n", |
| 4354 | (unsigned long long)sector, be32_to_cpu(p->blksize)); |
| 4355 | |
| 4356 | return validate_req_change_req_state(mdev, p->block_id, sector, |
Andreas Gruenbacher | bc9c5c4 | 2011-01-21 18:00:55 +0100 | [diff] [blame] | 4357 | &mdev->read_requests, __func__, |
| 4358 | neg_acked, false); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4359 | } |
| 4360 | |
Philipp Reisner | 0b70a13 | 2010-08-20 13:36:10 +0200 | [diff] [blame] | 4361 | static int got_NegRSDReply(struct drbd_conf *mdev, struct p_header80 *h) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4362 | { |
| 4363 | sector_t sector; |
| 4364 | int size; |
| 4365 | struct p_block_ack *p = (struct p_block_ack *)h; |
| 4366 | |
| 4367 | sector = be64_to_cpu(p->sector); |
| 4368 | size = be32_to_cpu(p->blksize); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4369 | |
| 4370 | update_peer_seq(mdev, be32_to_cpu(p->seq_num)); |
| 4371 | |
| 4372 | dec_rs_pending(mdev); |
| 4373 | |
| 4374 | if (get_ldev_if_state(mdev, D_FAILED)) { |
| 4375 | drbd_rs_complete_io(mdev, sector); |
Philipp Reisner | d612d30 | 2010-12-27 10:53:28 +0100 | [diff] [blame] | 4376 | switch (be16_to_cpu(h->command)) { |
| 4377 | case P_NEG_RS_DREPLY: |
| 4378 | drbd_rs_failed_io(mdev, sector, size); |
| 4379 | case P_RS_CANCEL: |
| 4380 | break; |
| 4381 | default: |
| 4382 | D_ASSERT(0); |
| 4383 | put_ldev(mdev); |
| 4384 | return false; |
| 4385 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4386 | put_ldev(mdev); |
| 4387 | } |
| 4388 | |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 4389 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4390 | } |
| 4391 | |
Philipp Reisner | 0b70a13 | 2010-08-20 13:36:10 +0200 | [diff] [blame] | 4392 | static int got_BarrierAck(struct drbd_conf *mdev, struct p_header80 *h) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4393 | { |
| 4394 | struct p_barrier_ack *p = (struct p_barrier_ack *)h; |
| 4395 | |
| 4396 | tl_release(mdev, p->barrier, be32_to_cpu(p->set_size)); |
| 4397 | |
Philipp Reisner | c4752ef | 2010-10-27 17:32:36 +0200 | [diff] [blame] | 4398 | if (mdev->state.conn == C_AHEAD && |
| 4399 | atomic_read(&mdev->ap_in_flight) == 0 && |
Philipp Reisner | 370a43e | 2011-01-14 16:03:11 +0100 | [diff] [blame] | 4400 | !test_and_set_bit(AHEAD_TO_SYNC_SOURCE, &mdev->current_epoch->flags)) { |
| 4401 | mdev->start_resync_timer.expires = jiffies + HZ; |
| 4402 | add_timer(&mdev->start_resync_timer); |
Philipp Reisner | c4752ef | 2010-10-27 17:32:36 +0200 | [diff] [blame] | 4403 | } |
| 4404 | |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 4405 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4406 | } |
| 4407 | |
Philipp Reisner | 0b70a13 | 2010-08-20 13:36:10 +0200 | [diff] [blame] | 4408 | static int got_OVResult(struct drbd_conf *mdev, struct p_header80 *h) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4409 | { |
| 4410 | struct p_block_ack *p = (struct p_block_ack *)h; |
| 4411 | struct drbd_work *w; |
| 4412 | sector_t sector; |
| 4413 | int size; |
| 4414 | |
| 4415 | sector = be64_to_cpu(p->sector); |
| 4416 | size = be32_to_cpu(p->blksize); |
| 4417 | |
| 4418 | update_peer_seq(mdev, be32_to_cpu(p->seq_num)); |
| 4419 | |
| 4420 | if (be64_to_cpu(p->block_id) == ID_OUT_OF_SYNC) |
| 4421 | drbd_ov_oos_found(mdev, sector, size); |
| 4422 | else |
| 4423 | ov_oos_print(mdev); |
| 4424 | |
Lars Ellenberg | 1d53f09 | 2010-09-05 01:13:24 +0200 | [diff] [blame] | 4425 | if (!get_ldev(mdev)) |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 4426 | return true; |
Lars Ellenberg | 1d53f09 | 2010-09-05 01:13:24 +0200 | [diff] [blame] | 4427 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4428 | drbd_rs_complete_io(mdev, sector); |
| 4429 | dec_rs_pending(mdev); |
| 4430 | |
Lars Ellenberg | ea5442a | 2010-11-05 09:48:01 +0100 | [diff] [blame] | 4431 | --mdev->ov_left; |
| 4432 | |
| 4433 | /* let's advance progress step marks only for every other megabyte */ |
| 4434 | if ((mdev->ov_left & 0x200) == 0x200) |
| 4435 | drbd_advance_rs_marks(mdev, mdev->ov_left); |
| 4436 | |
| 4437 | if (mdev->ov_left == 0) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4438 | w = kmalloc(sizeof(*w), GFP_NOIO); |
| 4439 | if (w) { |
| 4440 | w->cb = w_ov_finished; |
| 4441 | drbd_queue_work_front(&mdev->data.work, w); |
| 4442 | } else { |
| 4443 | dev_err(DEV, "kmalloc(w) failed."); |
| 4444 | ov_oos_print(mdev); |
| 4445 | drbd_resync_finished(mdev); |
| 4446 | } |
| 4447 | } |
Lars Ellenberg | 1d53f09 | 2010-09-05 01:13:24 +0200 | [diff] [blame] | 4448 | put_ldev(mdev); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 4449 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4450 | } |
| 4451 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4452 | static int got_skip(struct drbd_conf *mdev, struct p_header80 *h) |
Philipp Reisner | 0ced55a | 2010-04-30 15:26:20 +0200 | [diff] [blame] | 4453 | { |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 4454 | return true; |
Philipp Reisner | 0ced55a | 2010-04-30 15:26:20 +0200 | [diff] [blame] | 4455 | } |
| 4456 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4457 | struct asender_cmd { |
| 4458 | size_t pkt_size; |
Philipp Reisner | 0b70a13 | 2010-08-20 13:36:10 +0200 | [diff] [blame] | 4459 | int (*process)(struct drbd_conf *mdev, struct p_header80 *h); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4460 | }; |
| 4461 | |
| 4462 | static struct asender_cmd *get_asender_cmd(int cmd) |
| 4463 | { |
| 4464 | static struct asender_cmd asender_tbl[] = { |
| 4465 | /* anything missing from this table is in |
| 4466 | * the drbd_cmd_handler (drbd_default_handler) table, |
| 4467 | * see the beginning of drbdd() */ |
Philipp Reisner | 0b70a13 | 2010-08-20 13:36:10 +0200 | [diff] [blame] | 4468 | [P_PING] = { sizeof(struct p_header80), got_Ping }, |
| 4469 | [P_PING_ACK] = { sizeof(struct p_header80), got_PingAck }, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4470 | [P_RECV_ACK] = { sizeof(struct p_block_ack), got_BlockAck }, |
| 4471 | [P_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck }, |
| 4472 | [P_RS_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck }, |
| 4473 | [P_DISCARD_ACK] = { sizeof(struct p_block_ack), got_BlockAck }, |
| 4474 | [P_NEG_ACK] = { sizeof(struct p_block_ack), got_NegAck }, |
| 4475 | [P_NEG_DREPLY] = { sizeof(struct p_block_ack), got_NegDReply }, |
| 4476 | [P_NEG_RS_DREPLY] = { sizeof(struct p_block_ack), got_NegRSDReply}, |
| 4477 | [P_OV_RESULT] = { sizeof(struct p_block_ack), got_OVResult }, |
| 4478 | [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), got_BarrierAck }, |
| 4479 | [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), got_RqSReply }, |
| 4480 | [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), got_IsInSync }, |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4481 | [P_DELAY_PROBE] = { sizeof(struct p_delay_probe93), got_skip }, |
Philipp Reisner | d612d30 | 2010-12-27 10:53:28 +0100 | [diff] [blame] | 4482 | [P_RS_CANCEL] = { sizeof(struct p_block_ack), got_NegRSDReply}, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4483 | [P_MAX_CMD] = { 0, NULL }, |
| 4484 | }; |
| 4485 | if (cmd > P_MAX_CMD || asender_tbl[cmd].process == NULL) |
| 4486 | return NULL; |
| 4487 | return &asender_tbl[cmd]; |
| 4488 | } |
| 4489 | |
| 4490 | int drbd_asender(struct drbd_thread *thi) |
| 4491 | { |
| 4492 | struct drbd_conf *mdev = thi->mdev; |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4493 | struct p_header80 *h = &mdev->meta.rbuf.header.h80; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4494 | struct asender_cmd *cmd = NULL; |
| 4495 | |
| 4496 | int rv, len; |
| 4497 | void *buf = h; |
| 4498 | int received = 0; |
Philipp Reisner | 0b70a13 | 2010-08-20 13:36:10 +0200 | [diff] [blame] | 4499 | int expect = sizeof(struct p_header80); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4500 | int empty; |
Lars Ellenberg | f36af18 | 2011-03-09 22:44:55 +0100 | [diff] [blame] | 4501 | int ping_timeout_active = 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4502 | |
| 4503 | sprintf(current->comm, "drbd%d_asender", mdev_to_minor(mdev)); |
| 4504 | |
| 4505 | current->policy = SCHED_RR; /* Make this a realtime task! */ |
| 4506 | current->rt_priority = 2; /* more important than all other tasks */ |
| 4507 | |
| 4508 | while (get_t_state(thi) == Running) { |
| 4509 | drbd_thread_current_set_cpu(mdev); |
| 4510 | if (test_and_clear_bit(SEND_PING, &mdev->flags)) { |
| 4511 | ERR_IF(!drbd_send_ping(mdev)) goto reconnect; |
| 4512 | mdev->meta.socket->sk->sk_rcvtimeo = |
| 4513 | mdev->net_conf->ping_timeo*HZ/10; |
Lars Ellenberg | f36af18 | 2011-03-09 22:44:55 +0100 | [diff] [blame] | 4514 | ping_timeout_active = 1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4515 | } |
| 4516 | |
| 4517 | /* conditionally cork; |
| 4518 | * it may hurt latency if we cork without much to send */ |
| 4519 | if (!mdev->net_conf->no_cork && |
| 4520 | 3 < atomic_read(&mdev->unacked_cnt)) |
| 4521 | drbd_tcp_cork(mdev->meta.socket); |
| 4522 | while (1) { |
| 4523 | clear_bit(SIGNAL_ASENDER, &mdev->flags); |
| 4524 | flush_signals(current); |
Lars Ellenberg | 0f8488e | 2010-10-13 18:19:23 +0200 | [diff] [blame] | 4525 | if (!drbd_process_done_ee(mdev)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4526 | goto reconnect; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4527 | /* to avoid race with newly queued ACKs */ |
| 4528 | set_bit(SIGNAL_ASENDER, &mdev->flags); |
| 4529 | spin_lock_irq(&mdev->req_lock); |
| 4530 | empty = list_empty(&mdev->done_ee); |
| 4531 | spin_unlock_irq(&mdev->req_lock); |
| 4532 | /* new ack may have been queued right here, |
| 4533 | * but then there is also a signal pending, |
| 4534 | * and we start over... */ |
| 4535 | if (empty) |
| 4536 | break; |
| 4537 | } |
| 4538 | /* but unconditionally uncork unless disabled */ |
| 4539 | if (!mdev->net_conf->no_cork) |
| 4540 | drbd_tcp_uncork(mdev->meta.socket); |
| 4541 | |
| 4542 | /* short circuit, recv_msg would return EINTR anyways. */ |
| 4543 | if (signal_pending(current)) |
| 4544 | continue; |
| 4545 | |
| 4546 | rv = drbd_recv_short(mdev, mdev->meta.socket, |
| 4547 | buf, expect-received, 0); |
| 4548 | clear_bit(SIGNAL_ASENDER, &mdev->flags); |
| 4549 | |
| 4550 | flush_signals(current); |
| 4551 | |
| 4552 | /* Note: |
| 4553 | * -EINTR (on meta) we got a signal |
| 4554 | * -EAGAIN (on meta) rcvtimeo expired |
| 4555 | * -ECONNRESET other side closed the connection |
| 4556 | * -ERESTARTSYS (on data) we got a signal |
| 4557 | * rv < 0 other than above: unexpected error! |
| 4558 | * rv == expected: full header or command |
| 4559 | * rv < expected: "woken" by signal during receive |
| 4560 | * rv == 0 : "connection shut down by peer" |
| 4561 | */ |
| 4562 | if (likely(rv > 0)) { |
| 4563 | received += rv; |
| 4564 | buf += rv; |
| 4565 | } else if (rv == 0) { |
| 4566 | dev_err(DEV, "meta connection shut down by peer.\n"); |
| 4567 | goto reconnect; |
| 4568 | } else if (rv == -EAGAIN) { |
Lars Ellenberg | cb6518c | 2011-06-20 14:44:45 +0200 | [diff] [blame] | 4569 | /* If the data socket received something meanwhile, |
| 4570 | * that is good enough: peer is still alive. */ |
| 4571 | if (time_after(mdev->last_received, |
| 4572 | jiffies - mdev->meta.socket->sk->sk_rcvtimeo)) |
| 4573 | continue; |
Lars Ellenberg | f36af18 | 2011-03-09 22:44:55 +0100 | [diff] [blame] | 4574 | if (ping_timeout_active) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4575 | dev_err(DEV, "PingAck did not arrive in time.\n"); |
| 4576 | goto reconnect; |
| 4577 | } |
| 4578 | set_bit(SEND_PING, &mdev->flags); |
| 4579 | continue; |
| 4580 | } else if (rv == -EINTR) { |
| 4581 | continue; |
| 4582 | } else { |
| 4583 | dev_err(DEV, "sock_recvmsg returned %d\n", rv); |
| 4584 | goto reconnect; |
| 4585 | } |
| 4586 | |
| 4587 | if (received == expect && cmd == NULL) { |
Andreas Gruenbacher | ca9bc12 | 2011-01-11 13:47:24 +0100 | [diff] [blame] | 4588 | if (unlikely(h->magic != cpu_to_be32(DRBD_MAGIC))) { |
Lars Ellenberg | 004352f | 2010-10-05 20:13:58 +0200 | [diff] [blame] | 4589 | dev_err(DEV, "magic?? on meta m: 0x%08x c: %d l: %d\n", |
| 4590 | be32_to_cpu(h->magic), |
| 4591 | be16_to_cpu(h->command), |
| 4592 | be16_to_cpu(h->length)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4593 | goto reconnect; |
| 4594 | } |
| 4595 | cmd = get_asender_cmd(be16_to_cpu(h->command)); |
| 4596 | len = be16_to_cpu(h->length); |
| 4597 | if (unlikely(cmd == NULL)) { |
Lars Ellenberg | 004352f | 2010-10-05 20:13:58 +0200 | [diff] [blame] | 4598 | dev_err(DEV, "unknown command?? on meta m: 0x%08x c: %d l: %d\n", |
| 4599 | be32_to_cpu(h->magic), |
| 4600 | be16_to_cpu(h->command), |
| 4601 | be16_to_cpu(h->length)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4602 | goto disconnect; |
| 4603 | } |
| 4604 | expect = cmd->pkt_size; |
Philipp Reisner | 0b70a13 | 2010-08-20 13:36:10 +0200 | [diff] [blame] | 4605 | ERR_IF(len != expect-sizeof(struct p_header80)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4606 | goto reconnect; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4607 | } |
| 4608 | if (received == expect) { |
Lars Ellenberg | cb6518c | 2011-06-20 14:44:45 +0200 | [diff] [blame] | 4609 | mdev->last_received = jiffies; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4610 | D_ASSERT(cmd != NULL); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4611 | if (!cmd->process(mdev, h)) |
| 4612 | goto reconnect; |
| 4613 | |
Lars Ellenberg | f36af18 | 2011-03-09 22:44:55 +0100 | [diff] [blame] | 4614 | /* the idle_timeout (ping-int) |
| 4615 | * has been restored in got_PingAck() */ |
| 4616 | if (cmd == get_asender_cmd(P_PING_ACK)) |
| 4617 | ping_timeout_active = 0; |
| 4618 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4619 | buf = h; |
| 4620 | received = 0; |
Philipp Reisner | 0b70a13 | 2010-08-20 13:36:10 +0200 | [diff] [blame] | 4621 | expect = sizeof(struct p_header80); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4622 | cmd = NULL; |
| 4623 | } |
| 4624 | } |
| 4625 | |
| 4626 | if (0) { |
| 4627 | reconnect: |
| 4628 | drbd_force_state(mdev, NS(conn, C_NETWORK_FAILURE)); |
Lars Ellenberg | 856c50c | 2010-10-14 13:37:40 +0200 | [diff] [blame] | 4629 | drbd_md_sync(mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4630 | } |
| 4631 | if (0) { |
| 4632 | disconnect: |
| 4633 | drbd_force_state(mdev, NS(conn, C_DISCONNECTING)); |
Lars Ellenberg | 856c50c | 2010-10-14 13:37:40 +0200 | [diff] [blame] | 4634 | drbd_md_sync(mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4635 | } |
| 4636 | clear_bit(SIGNAL_ASENDER, &mdev->flags); |
| 4637 | |
| 4638 | D_ASSERT(mdev->state.conn < C_CONNECTED); |
| 4639 | dev_info(DEV, "asender terminated\n"); |
| 4640 | |
| 4641 | return 0; |
| 4642 | } |