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