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