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 | |
Philipp Reisner | 907599e | 2011-02-08 11:25:37 +0100 | [diff] [blame] | 766 | ok &= drbd_send_sync_param(mdev, &mdev->sync_conf); |
| 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? */ |
| 2088 | if (mdev->sync_conf.c_min_rate == 0) |
| 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 | |
| 2128 | if (dbdt > mdev->sync_conf.c_min_rate) |
| 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 | 7204624 | 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 | 7204624 | 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 | 7204624 | 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 | 7204624 | 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 | 7204624 | 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 | 7204624 | 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 | 7204624 | 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 | 7204624 | 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 | 7204624 | 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 | 7204624 | 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 | 7204624 | 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 | 7204624 | 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 | 7204624 | 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 | 7204624 | 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 | 7204624 | 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 | |
| 3004 | mdev->sync_conf.rate = be32_to_cpu(p->rate); |
| 3005 | |
| 3006 | if (apv >= 88) { |
| 3007 | if (apv == 88) { |
| 3008 | if (data_size > SHARED_SECRET_MAX) { |
| 3009 | dev_err(DEV, "verify-alg too long, " |
| 3010 | "peer wants %u, accepting only %u byte\n", |
| 3011 | data_size, SHARED_SECRET_MAX); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3012 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3013 | } |
| 3014 | |
Philipp Reisner | de0ff33 | 2011-02-07 16:56:20 +0100 | [diff] [blame] | 3015 | if (drbd_recv(mdev->tconn, p->verify_alg, data_size) != data_size) |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3016 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3017 | |
| 3018 | /* we expect NUL terminated string */ |
| 3019 | /* but just in case someone tries to be evil */ |
| 3020 | D_ASSERT(p->verify_alg[data_size-1] == 0); |
| 3021 | p->verify_alg[data_size-1] = 0; |
| 3022 | |
| 3023 | } else /* apv >= 89 */ { |
| 3024 | /* we still expect NUL terminated strings */ |
| 3025 | /* but just in case someone tries to be evil */ |
| 3026 | D_ASSERT(p->verify_alg[SHARED_SECRET_MAX-1] == 0); |
| 3027 | D_ASSERT(p->csums_alg[SHARED_SECRET_MAX-1] == 0); |
| 3028 | p->verify_alg[SHARED_SECRET_MAX-1] = 0; |
| 3029 | p->csums_alg[SHARED_SECRET_MAX-1] = 0; |
| 3030 | } |
| 3031 | |
| 3032 | if (strcmp(mdev->sync_conf.verify_alg, p->verify_alg)) { |
| 3033 | if (mdev->state.conn == C_WF_REPORT_PARAMS) { |
| 3034 | dev_err(DEV, "Different verify-alg settings. me=\"%s\" peer=\"%s\"\n", |
| 3035 | mdev->sync_conf.verify_alg, p->verify_alg); |
| 3036 | goto disconnect; |
| 3037 | } |
| 3038 | verify_tfm = drbd_crypto_alloc_digest_safe(mdev, |
| 3039 | p->verify_alg, "verify-alg"); |
| 3040 | if (IS_ERR(verify_tfm)) { |
| 3041 | verify_tfm = NULL; |
| 3042 | goto disconnect; |
| 3043 | } |
| 3044 | } |
| 3045 | |
| 3046 | if (apv >= 89 && strcmp(mdev->sync_conf.csums_alg, p->csums_alg)) { |
| 3047 | if (mdev->state.conn == C_WF_REPORT_PARAMS) { |
| 3048 | dev_err(DEV, "Different csums-alg settings. me=\"%s\" peer=\"%s\"\n", |
| 3049 | mdev->sync_conf.csums_alg, p->csums_alg); |
| 3050 | goto disconnect; |
| 3051 | } |
| 3052 | csums_tfm = drbd_crypto_alloc_digest_safe(mdev, |
| 3053 | p->csums_alg, "csums-alg"); |
| 3054 | if (IS_ERR(csums_tfm)) { |
| 3055 | csums_tfm = NULL; |
| 3056 | goto disconnect; |
| 3057 | } |
| 3058 | } |
| 3059 | |
Philipp Reisner | 8e26f9c | 2010-07-06 17:25:54 +0200 | [diff] [blame] | 3060 | if (apv > 94) { |
| 3061 | mdev->sync_conf.rate = be32_to_cpu(p->rate); |
| 3062 | mdev->sync_conf.c_plan_ahead = be32_to_cpu(p->c_plan_ahead); |
| 3063 | mdev->sync_conf.c_delay_target = be32_to_cpu(p->c_delay_target); |
| 3064 | mdev->sync_conf.c_fill_target = be32_to_cpu(p->c_fill_target); |
| 3065 | mdev->sync_conf.c_max_rate = be32_to_cpu(p->c_max_rate); |
Philipp Reisner | 778f271 | 2010-07-06 11:14:00 +0200 | [diff] [blame] | 3066 | |
| 3067 | fifo_size = (mdev->sync_conf.c_plan_ahead * 10 * SLEEP_TIME) / HZ; |
| 3068 | if (fifo_size != mdev->rs_plan_s.size && fifo_size > 0) { |
| 3069 | rs_plan_s = kzalloc(sizeof(int) * fifo_size, GFP_KERNEL); |
| 3070 | if (!rs_plan_s) { |
| 3071 | dev_err(DEV, "kmalloc of fifo_buffer failed"); |
| 3072 | goto disconnect; |
| 3073 | } |
| 3074 | } |
Philipp Reisner | 8e26f9c | 2010-07-06 17:25:54 +0200 | [diff] [blame] | 3075 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3076 | |
| 3077 | spin_lock(&mdev->peer_seq_lock); |
| 3078 | /* lock against drbd_nl_syncer_conf() */ |
| 3079 | if (verify_tfm) { |
| 3080 | strcpy(mdev->sync_conf.verify_alg, p->verify_alg); |
| 3081 | mdev->sync_conf.verify_alg_len = strlen(p->verify_alg) + 1; |
| 3082 | crypto_free_hash(mdev->verify_tfm); |
| 3083 | mdev->verify_tfm = verify_tfm; |
| 3084 | dev_info(DEV, "using verify-alg: \"%s\"\n", p->verify_alg); |
| 3085 | } |
| 3086 | if (csums_tfm) { |
| 3087 | strcpy(mdev->sync_conf.csums_alg, p->csums_alg); |
| 3088 | mdev->sync_conf.csums_alg_len = strlen(p->csums_alg) + 1; |
| 3089 | crypto_free_hash(mdev->csums_tfm); |
| 3090 | mdev->csums_tfm = csums_tfm; |
| 3091 | dev_info(DEV, "using csums-alg: \"%s\"\n", p->csums_alg); |
| 3092 | } |
Philipp Reisner | 778f271 | 2010-07-06 11:14:00 +0200 | [diff] [blame] | 3093 | if (fifo_size != mdev->rs_plan_s.size) { |
| 3094 | kfree(mdev->rs_plan_s.values); |
| 3095 | mdev->rs_plan_s.values = rs_plan_s; |
| 3096 | mdev->rs_plan_s.size = fifo_size; |
| 3097 | mdev->rs_planed = 0; |
| 3098 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3099 | spin_unlock(&mdev->peer_seq_lock); |
| 3100 | } |
| 3101 | |
| 3102 | return ok; |
| 3103 | disconnect: |
| 3104 | /* just for completeness: actually not needed, |
| 3105 | * as this is not reached if csums_tfm was ok. */ |
| 3106 | crypto_free_hash(csums_tfm); |
| 3107 | /* but free the verify_tfm again, if csums_tfm did not work out */ |
| 3108 | crypto_free_hash(verify_tfm); |
Philipp Reisner | 38fa998 | 2011-03-15 18:24:49 +0100 | [diff] [blame] | 3109 | conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3110 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3111 | } |
| 3112 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3113 | /* warn if the arguments differ by more than 12.5% */ |
| 3114 | static void warn_if_differ_considerably(struct drbd_conf *mdev, |
| 3115 | const char *s, sector_t a, sector_t b) |
| 3116 | { |
| 3117 | sector_t d; |
| 3118 | if (a == 0 || b == 0) |
| 3119 | return; |
| 3120 | d = (a > b) ? (a - b) : (b - a); |
| 3121 | if (d > (a>>3) || d > (b>>3)) |
| 3122 | dev_warn(DEV, "Considerable difference in %s: %llus vs. %llus\n", s, |
| 3123 | (unsigned long long)a, (unsigned long long)b); |
| 3124 | } |
| 3125 | |
Andreas Gruenbacher | d876302 | 2011-01-26 17:39:41 +0100 | [diff] [blame] | 3126 | static int receive_sizes(struct drbd_conf *mdev, enum drbd_packet cmd, |
| 3127 | unsigned int data_size) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3128 | { |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 3129 | struct p_sizes *p = &mdev->tconn->data.rbuf.sizes; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3130 | enum determine_dev_size dd = unchanged; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3131 | sector_t p_size, p_usize, my_usize; |
| 3132 | int ldsc = 0; /* local disk size changed */ |
Philipp Reisner | e89b591 | 2010-03-24 17:11:33 +0100 | [diff] [blame] | 3133 | enum dds_flags ddsf; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3134 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3135 | p_size = be64_to_cpu(p->d_size); |
| 3136 | p_usize = be64_to_cpu(p->u_size); |
| 3137 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3138 | /* just store the peer's disk size for now. |
| 3139 | * we still need to figure out whether we accept that. */ |
| 3140 | mdev->p_size = p_size; |
| 3141 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3142 | if (get_ldev(mdev)) { |
| 3143 | warn_if_differ_considerably(mdev, "lower level device sizes", |
| 3144 | p_size, drbd_get_max_capacity(mdev->ldev)); |
| 3145 | warn_if_differ_considerably(mdev, "user requested size", |
| 3146 | p_usize, mdev->ldev->dc.disk_size); |
| 3147 | |
| 3148 | /* if this is the first connect, or an otherwise expected |
| 3149 | * param exchange, choose the minimum */ |
| 3150 | if (mdev->state.conn == C_WF_REPORT_PARAMS) |
| 3151 | p_usize = min_not_zero((sector_t)mdev->ldev->dc.disk_size, |
| 3152 | p_usize); |
| 3153 | |
| 3154 | my_usize = mdev->ldev->dc.disk_size; |
| 3155 | |
| 3156 | if (mdev->ldev->dc.disk_size != p_usize) { |
| 3157 | mdev->ldev->dc.disk_size = p_usize; |
| 3158 | dev_info(DEV, "Peer sets u_size to %lu sectors\n", |
| 3159 | (unsigned long)mdev->ldev->dc.disk_size); |
| 3160 | } |
| 3161 | |
| 3162 | /* Never shrink a device with usable data during connect. |
| 3163 | But allow online shrinking if we are connected. */ |
Philipp Reisner | a393db6 | 2009-12-22 13:35:52 +0100 | [diff] [blame] | 3164 | if (drbd_new_dev_size(mdev, mdev->ldev, 0) < |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3165 | drbd_get_capacity(mdev->this_bdev) && |
| 3166 | mdev->state.disk >= D_OUTDATED && |
| 3167 | mdev->state.conn < C_CONNECTED) { |
| 3168 | dev_err(DEV, "The peer's disk size is too small!\n"); |
Philipp Reisner | 38fa998 | 2011-03-15 18:24:49 +0100 | [diff] [blame] | 3169 | conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3170 | mdev->ldev->dc.disk_size = my_usize; |
| 3171 | put_ldev(mdev); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3172 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3173 | } |
| 3174 | put_ldev(mdev); |
| 3175 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3176 | |
Philipp Reisner | e89b591 | 2010-03-24 17:11:33 +0100 | [diff] [blame] | 3177 | ddsf = be16_to_cpu(p->dds_flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3178 | if (get_ldev(mdev)) { |
Bart Van Assche | 24c4830 | 2011-05-21 18:32:29 +0200 | [diff] [blame] | 3179 | dd = drbd_determine_dev_size(mdev, ddsf); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3180 | put_ldev(mdev); |
| 3181 | if (dd == dev_size_error) |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3182 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3183 | drbd_md_sync(mdev); |
| 3184 | } else { |
| 3185 | /* I am diskless, need to accept the peer's size. */ |
| 3186 | drbd_set_my_capacity(mdev, p_size); |
| 3187 | } |
| 3188 | |
Philipp Reisner | 99432fc | 2011-05-20 16:39:13 +0200 | [diff] [blame] | 3189 | mdev->peer_max_bio_size = be32_to_cpu(p->max_bio_size); |
| 3190 | drbd_reconsider_max_bio_size(mdev); |
| 3191 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3192 | if (get_ldev(mdev)) { |
| 3193 | if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev)) { |
| 3194 | mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev); |
| 3195 | ldsc = 1; |
| 3196 | } |
| 3197 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3198 | put_ldev(mdev); |
| 3199 | } |
| 3200 | |
| 3201 | if (mdev->state.conn > C_WF_REPORT_PARAMS) { |
| 3202 | if (be64_to_cpu(p->c_size) != |
| 3203 | drbd_get_capacity(mdev->this_bdev) || ldsc) { |
| 3204 | /* we have different sizes, probably peer |
| 3205 | * needs to know my new size... */ |
Philipp Reisner | e89b591 | 2010-03-24 17:11:33 +0100 | [diff] [blame] | 3206 | drbd_send_sizes(mdev, 0, ddsf); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3207 | } |
| 3208 | if (test_and_clear_bit(RESIZE_PENDING, &mdev->flags) || |
| 3209 | (dd == grew && mdev->state.conn == C_CONNECTED)) { |
| 3210 | if (mdev->state.pdsk >= D_INCONSISTENT && |
Philipp Reisner | e89b591 | 2010-03-24 17:11:33 +0100 | [diff] [blame] | 3211 | mdev->state.disk >= D_INCONSISTENT) { |
| 3212 | if (ddsf & DDSF_NO_RESYNC) |
| 3213 | dev_info(DEV, "Resync of new storage suppressed with --assume-clean\n"); |
| 3214 | else |
| 3215 | resync_after_online_grow(mdev); |
| 3216 | } else |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3217 | set_bit(RESYNC_AFTER_NEG, &mdev->flags); |
| 3218 | } |
| 3219 | } |
| 3220 | |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3221 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3222 | } |
| 3223 | |
Andreas Gruenbacher | d876302 | 2011-01-26 17:39:41 +0100 | [diff] [blame] | 3224 | static int receive_uuids(struct drbd_conf *mdev, enum drbd_packet cmd, |
| 3225 | unsigned int data_size) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3226 | { |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 3227 | struct p_uuids *p = &mdev->tconn->data.rbuf.uuids; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3228 | u64 *p_uuid; |
Lars Ellenberg | 62b0da3 | 2011-01-20 13:25:21 +0100 | [diff] [blame] | 3229 | int i, updated_uuids = 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3230 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3231 | p_uuid = kmalloc(sizeof(u64)*UI_EXTENDED_SIZE, GFP_NOIO); |
| 3232 | |
| 3233 | for (i = UI_CURRENT; i < UI_EXTENDED_SIZE; i++) |
| 3234 | p_uuid[i] = be64_to_cpu(p->uuid[i]); |
| 3235 | |
| 3236 | kfree(mdev->p_uuid); |
| 3237 | mdev->p_uuid = p_uuid; |
| 3238 | |
| 3239 | if (mdev->state.conn < C_CONNECTED && |
| 3240 | mdev->state.disk < D_INCONSISTENT && |
| 3241 | mdev->state.role == R_PRIMARY && |
| 3242 | (mdev->ed_uuid & ~((u64)1)) != (p_uuid[UI_CURRENT] & ~((u64)1))) { |
| 3243 | dev_err(DEV, "Can only connect to data with current UUID=%016llX\n", |
| 3244 | (unsigned long long)mdev->ed_uuid); |
Philipp Reisner | 38fa998 | 2011-03-15 18:24:49 +0100 | [diff] [blame] | 3245 | conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3246 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3247 | } |
| 3248 | |
| 3249 | if (get_ldev(mdev)) { |
| 3250 | int skip_initial_sync = |
| 3251 | mdev->state.conn == C_CONNECTED && |
Philipp Reisner | 31890f4 | 2011-01-19 14:12:51 +0100 | [diff] [blame] | 3252 | mdev->tconn->agreed_pro_version >= 90 && |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3253 | mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && |
| 3254 | (p_uuid[UI_FLAGS] & 8); |
| 3255 | if (skip_initial_sync) { |
| 3256 | dev_info(DEV, "Accepted new current UUID, preparing to skip initial sync\n"); |
| 3257 | drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write, |
Lars Ellenberg | 20ceb2b | 2011-01-21 10:56:44 +0100 | [diff] [blame] | 3258 | "clear_n_write from receive_uuids", |
| 3259 | BM_LOCKED_TEST_ALLOWED); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3260 | _drbd_uuid_set(mdev, UI_CURRENT, p_uuid[UI_CURRENT]); |
| 3261 | _drbd_uuid_set(mdev, UI_BITMAP, 0); |
| 3262 | _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE), |
| 3263 | CS_VERBOSE, NULL); |
| 3264 | drbd_md_sync(mdev); |
Lars Ellenberg | 62b0da3 | 2011-01-20 13:25:21 +0100 | [diff] [blame] | 3265 | updated_uuids = 1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3266 | } |
| 3267 | put_ldev(mdev); |
Philipp Reisner | 18a50fa | 2010-06-21 14:14:15 +0200 | [diff] [blame] | 3268 | } else if (mdev->state.disk < D_INCONSISTENT && |
| 3269 | mdev->state.role == R_PRIMARY) { |
| 3270 | /* I am a diskless primary, the peer just created a new current UUID |
| 3271 | for me. */ |
Lars Ellenberg | 62b0da3 | 2011-01-20 13:25:21 +0100 | [diff] [blame] | 3272 | updated_uuids = drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3273 | } |
| 3274 | |
| 3275 | /* Before we test for the disk state, we should wait until an eventually |
| 3276 | ongoing cluster wide state change is finished. That is important if |
| 3277 | we are primary and are detaching from our disk. We need to see the |
| 3278 | new disk state... */ |
Philipp Reisner | 8410da8 | 2011-02-11 20:11:10 +0100 | [diff] [blame] | 3279 | mutex_lock(mdev->state_mutex); |
| 3280 | mutex_unlock(mdev->state_mutex); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3281 | if (mdev->state.conn >= C_CONNECTED && mdev->state.disk < D_INCONSISTENT) |
Lars Ellenberg | 62b0da3 | 2011-01-20 13:25:21 +0100 | [diff] [blame] | 3282 | updated_uuids |= drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]); |
| 3283 | |
| 3284 | if (updated_uuids) |
| 3285 | drbd_print_uuids(mdev, "receiver updated UUIDs to"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3286 | |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3287 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3288 | } |
| 3289 | |
| 3290 | /** |
| 3291 | * convert_state() - Converts the peer's view of the cluster state to our point of view |
| 3292 | * @ps: The state as seen by the peer. |
| 3293 | */ |
| 3294 | static union drbd_state convert_state(union drbd_state ps) |
| 3295 | { |
| 3296 | union drbd_state ms; |
| 3297 | |
| 3298 | static enum drbd_conns c_tab[] = { |
| 3299 | [C_CONNECTED] = C_CONNECTED, |
| 3300 | |
| 3301 | [C_STARTING_SYNC_S] = C_STARTING_SYNC_T, |
| 3302 | [C_STARTING_SYNC_T] = C_STARTING_SYNC_S, |
| 3303 | [C_DISCONNECTING] = C_TEAR_DOWN, /* C_NETWORK_FAILURE, */ |
| 3304 | [C_VERIFY_S] = C_VERIFY_T, |
| 3305 | [C_MASK] = C_MASK, |
| 3306 | }; |
| 3307 | |
| 3308 | ms.i = ps.i; |
| 3309 | |
| 3310 | ms.conn = c_tab[ps.conn]; |
| 3311 | ms.peer = ps.role; |
| 3312 | ms.role = ps.peer; |
| 3313 | ms.pdsk = ps.disk; |
| 3314 | ms.disk = ps.pdsk; |
| 3315 | ms.peer_isp = (ps.aftr_isp | ps.user_isp); |
| 3316 | |
| 3317 | return ms; |
| 3318 | } |
| 3319 | |
Andreas Gruenbacher | d876302 | 2011-01-26 17:39:41 +0100 | [diff] [blame] | 3320 | static int receive_req_state(struct drbd_conf *mdev, enum drbd_packet cmd, |
| 3321 | unsigned int data_size) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3322 | { |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 3323 | struct p_req_state *p = &mdev->tconn->data.rbuf.req_state; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3324 | union drbd_state mask, val; |
Andreas Gruenbacher | bf885f8 | 2010-12-08 00:39:32 +0100 | [diff] [blame] | 3325 | enum drbd_state_rv rv; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3326 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3327 | mask.i = be32_to_cpu(p->mask); |
| 3328 | val.i = be32_to_cpu(p->val); |
| 3329 | |
Philipp Reisner | 25703f8 | 2011-02-07 14:35:25 +0100 | [diff] [blame] | 3330 | if (test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags) && |
Philipp Reisner | 8410da8 | 2011-02-11 20:11:10 +0100 | [diff] [blame] | 3331 | mutex_is_locked(mdev->state_mutex)) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3332 | drbd_send_sr_reply(mdev, SS_CONCURRENT_ST_CHG); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3333 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3334 | } |
| 3335 | |
| 3336 | mask = convert_state(mask); |
| 3337 | val = convert_state(val); |
| 3338 | |
Philipp Reisner | dfafcc8 | 2011-03-16 10:55:07 +0100 | [diff] [blame] | 3339 | rv = drbd_change_state(mdev, CS_VERBOSE, mask, val); |
| 3340 | drbd_send_sr_reply(mdev, rv); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3341 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3342 | drbd_md_sync(mdev); |
| 3343 | |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3344 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3345 | } |
| 3346 | |
Philipp Reisner | dfafcc8 | 2011-03-16 10:55:07 +0100 | [diff] [blame] | 3347 | static int receive_req_conn_state(struct drbd_tconn *tconn, enum drbd_packet cmd, |
| 3348 | unsigned int data_size) |
| 3349 | { |
| 3350 | struct p_req_state *p = &tconn->data.rbuf.req_state; |
| 3351 | union drbd_state mask, val; |
| 3352 | enum drbd_state_rv rv; |
| 3353 | |
| 3354 | mask.i = be32_to_cpu(p->mask); |
| 3355 | val.i = be32_to_cpu(p->val); |
| 3356 | |
| 3357 | if (test_bit(DISCARD_CONCURRENT, &tconn->flags) && |
| 3358 | mutex_is_locked(&tconn->cstate_mutex)) { |
| 3359 | conn_send_sr_reply(tconn, SS_CONCURRENT_ST_CHG); |
| 3360 | return true; |
| 3361 | } |
| 3362 | |
| 3363 | mask = convert_state(mask); |
| 3364 | val = convert_state(val); |
| 3365 | |
| 3366 | rv = conn_request_state(tconn, mask, val, CS_VERBOSE | CS_LOCAL_ONLY); |
| 3367 | conn_send_sr_reply(tconn, rv); |
| 3368 | |
| 3369 | return true; |
| 3370 | } |
| 3371 | |
Andreas Gruenbacher | d876302 | 2011-01-26 17:39:41 +0100 | [diff] [blame] | 3372 | static int receive_state(struct drbd_conf *mdev, enum drbd_packet cmd, |
| 3373 | unsigned int data_size) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3374 | { |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 3375 | struct p_state *p = &mdev->tconn->data.rbuf.state; |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3376 | union drbd_state os, ns, peer_state; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3377 | enum drbd_disk_state real_peer_disk; |
Philipp Reisner | 65d922c | 2010-06-16 16:18:09 +0200 | [diff] [blame] | 3378 | enum chg_state_flags cs_flags; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3379 | int rv; |
| 3380 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3381 | peer_state.i = be32_to_cpu(p->state); |
| 3382 | |
| 3383 | real_peer_disk = peer_state.disk; |
| 3384 | if (peer_state.disk == D_NEGOTIATING) { |
| 3385 | real_peer_disk = mdev->p_uuid[UI_FLAGS] & 4 ? D_INCONSISTENT : D_CONSISTENT; |
| 3386 | dev_info(DEV, "real peer disk state = %s\n", drbd_disk_str(real_peer_disk)); |
| 3387 | } |
| 3388 | |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 3389 | spin_lock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3390 | retry: |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3391 | os = ns = mdev->state; |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 3392 | spin_unlock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3393 | |
Lars Ellenberg | e9ef7bb | 2010-10-07 15:55:39 +0200 | [diff] [blame] | 3394 | /* peer says his disk is uptodate, while we think it is inconsistent, |
| 3395 | * and this happens while we think we have a sync going on. */ |
| 3396 | if (os.pdsk == D_INCONSISTENT && real_peer_disk == D_UP_TO_DATE && |
| 3397 | os.conn > C_CONNECTED && os.disk == D_UP_TO_DATE) { |
| 3398 | /* If we are (becoming) SyncSource, but peer is still in sync |
| 3399 | * preparation, ignore its uptodate-ness to avoid flapping, it |
| 3400 | * will change to inconsistent once the peer reaches active |
| 3401 | * syncing states. |
| 3402 | * It may have changed syncer-paused flags, however, so we |
| 3403 | * cannot ignore this completely. */ |
| 3404 | if (peer_state.conn > C_CONNECTED && |
| 3405 | peer_state.conn < C_SYNC_SOURCE) |
| 3406 | real_peer_disk = D_INCONSISTENT; |
| 3407 | |
| 3408 | /* if peer_state changes to connected at the same time, |
| 3409 | * it explicitly notifies us that it finished resync. |
| 3410 | * Maybe we should finish it up, too? */ |
| 3411 | else if (os.conn >= C_SYNC_SOURCE && |
| 3412 | peer_state.conn == C_CONNECTED) { |
| 3413 | if (drbd_bm_total_weight(mdev) <= mdev->rs_failed) |
| 3414 | drbd_resync_finished(mdev); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3415 | return true; |
Lars Ellenberg | e9ef7bb | 2010-10-07 15:55:39 +0200 | [diff] [blame] | 3416 | } |
| 3417 | } |
| 3418 | |
| 3419 | /* peer says his disk is inconsistent, while we think it is uptodate, |
| 3420 | * and this happens while the peer still thinks we have a sync going on, |
| 3421 | * but we think we are already done with the sync. |
| 3422 | * We ignore this to avoid flapping pdsk. |
| 3423 | * This should not happen, if the peer is a recent version of drbd. */ |
| 3424 | if (os.pdsk == D_UP_TO_DATE && real_peer_disk == D_INCONSISTENT && |
| 3425 | os.conn == C_CONNECTED && peer_state.conn > C_SYNC_SOURCE) |
| 3426 | real_peer_disk = D_UP_TO_DATE; |
| 3427 | |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3428 | if (ns.conn == C_WF_REPORT_PARAMS) |
| 3429 | ns.conn = C_CONNECTED; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3430 | |
Philipp Reisner | 6753171 | 2010-10-27 12:21:30 +0200 | [diff] [blame] | 3431 | if (peer_state.conn == C_AHEAD) |
| 3432 | ns.conn = C_BEHIND; |
| 3433 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3434 | if (mdev->p_uuid && peer_state.disk >= D_NEGOTIATING && |
| 3435 | get_ldev_if_state(mdev, D_NEGOTIATING)) { |
| 3436 | int cr; /* consider resync */ |
| 3437 | |
| 3438 | /* if we established a new connection */ |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3439 | cr = (os.conn < C_CONNECTED); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3440 | /* if we had an established connection |
| 3441 | * and one of the nodes newly attaches a disk */ |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3442 | cr |= (os.conn == C_CONNECTED && |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3443 | (peer_state.disk == D_NEGOTIATING || |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3444 | os.disk == D_NEGOTIATING)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3445 | /* if we have both been inconsistent, and the peer has been |
| 3446 | * forced to be UpToDate with --overwrite-data */ |
| 3447 | cr |= test_bit(CONSIDER_RESYNC, &mdev->flags); |
| 3448 | /* if we had been plain connected, and the admin requested to |
| 3449 | * start a sync by "invalidate" or "invalidate-remote" */ |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3450 | cr |= (os.conn == C_CONNECTED && |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3451 | (peer_state.conn >= C_STARTING_SYNC_S && |
| 3452 | peer_state.conn <= C_WF_BITMAP_T)); |
| 3453 | |
| 3454 | if (cr) |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3455 | ns.conn = drbd_sync_handshake(mdev, peer_state.role, real_peer_disk); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3456 | |
| 3457 | put_ldev(mdev); |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3458 | if (ns.conn == C_MASK) { |
| 3459 | ns.conn = C_CONNECTED; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3460 | if (mdev->state.disk == D_NEGOTIATING) { |
Lars Ellenberg | 82f59cc | 2010-10-16 12:13:47 +0200 | [diff] [blame] | 3461 | drbd_force_state(mdev, NS(disk, D_FAILED)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3462 | } else if (peer_state.disk == D_NEGOTIATING) { |
| 3463 | dev_err(DEV, "Disk attach process on the peer node was aborted.\n"); |
| 3464 | peer_state.disk = D_DISKLESS; |
Lars Ellenberg | 580b976 | 2010-02-26 23:15:23 +0100 | [diff] [blame] | 3465 | real_peer_disk = D_DISKLESS; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3466 | } else { |
Philipp Reisner | 8169e41 | 2011-03-15 18:40:27 +0100 | [diff] [blame] | 3467 | if (test_and_clear_bit(CONN_DRY_RUN, &mdev->tconn->flags)) |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3468 | return false; |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3469 | D_ASSERT(os.conn == C_WF_REPORT_PARAMS); |
Philipp Reisner | 38fa998 | 2011-03-15 18:24:49 +0100 | [diff] [blame] | 3470 | conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3471 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3472 | } |
| 3473 | } |
| 3474 | } |
| 3475 | |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 3476 | spin_lock_irq(&mdev->tconn->req_lock); |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3477 | if (mdev->state.i != os.i) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3478 | goto retry; |
| 3479 | clear_bit(CONSIDER_RESYNC, &mdev->flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3480 | ns.peer = peer_state.role; |
| 3481 | ns.pdsk = real_peer_disk; |
| 3482 | ns.peer_isp = (peer_state.aftr_isp | peer_state.user_isp); |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3483 | 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] | 3484 | ns.disk = mdev->new_state_tmp.disk; |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3485 | cs_flags = CS_VERBOSE + (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED ? 0 : CS_HARD); |
| 3486 | 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] | 3487 | test_bit(NEW_CUR_UUID, &mdev->flags)) { |
Andreas Gruenbacher | 8554df1 | 2011-01-25 15:37:43 +0100 | [diff] [blame] | 3488 | /* 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] | 3489 | for temporal network outages! */ |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 3490 | spin_unlock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | 481c6f5 | 2010-06-22 14:03:27 +0200 | [diff] [blame] | 3491 | 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] | 3492 | tl_clear(mdev->tconn); |
Philipp Reisner | 481c6f5 | 2010-06-22 14:03:27 +0200 | [diff] [blame] | 3493 | drbd_uuid_new_current(mdev); |
| 3494 | clear_bit(NEW_CUR_UUID, &mdev->flags); |
Philipp Reisner | 38fa998 | 2011-03-15 18:24:49 +0100 | [diff] [blame] | 3495 | 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] | 3496 | return false; |
Philipp Reisner | 481c6f5 | 2010-06-22 14:03:27 +0200 | [diff] [blame] | 3497 | } |
Philipp Reisner | 65d922c | 2010-06-16 16:18:09 +0200 | [diff] [blame] | 3498 | rv = _drbd_set_state(mdev, ns, cs_flags, NULL); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3499 | ns = mdev->state; |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 3500 | spin_unlock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3501 | |
| 3502 | if (rv < SS_SUCCESS) { |
Philipp Reisner | 38fa998 | 2011-03-15 18:24:49 +0100 | [diff] [blame] | 3503 | conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3504 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3505 | } |
| 3506 | |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3507 | if (os.conn > C_WF_REPORT_PARAMS) { |
| 3508 | if (ns.conn > C_CONNECTED && peer_state.conn <= C_CONNECTED && |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3509 | peer_state.disk != D_NEGOTIATING ) { |
| 3510 | /* we want resync, peer has not yet decided to sync... */ |
| 3511 | /* Nowadays only used when forcing a node into primary role and |
| 3512 | setting its disk to UpToDate with that */ |
| 3513 | drbd_send_uuids(mdev); |
| 3514 | drbd_send_state(mdev); |
| 3515 | } |
| 3516 | } |
| 3517 | |
Philipp Reisner | 89e58e7 | 2011-01-19 13:12:45 +0100 | [diff] [blame] | 3518 | mdev->tconn->net_conf->want_lose = 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3519 | |
| 3520 | drbd_md_sync(mdev); /* update connected indicator, la_size, ... */ |
| 3521 | |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3522 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3523 | } |
| 3524 | |
Andreas Gruenbacher | d876302 | 2011-01-26 17:39:41 +0100 | [diff] [blame] | 3525 | static int receive_sync_uuid(struct drbd_conf *mdev, enum drbd_packet cmd, |
| 3526 | unsigned int data_size) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3527 | { |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 3528 | struct p_rs_uuid *p = &mdev->tconn->data.rbuf.rs_uuid; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3529 | |
| 3530 | wait_event(mdev->misc_wait, |
| 3531 | mdev->state.conn == C_WF_SYNC_UUID || |
Philipp Reisner | c4752ef | 2010-10-27 17:32:36 +0200 | [diff] [blame] | 3532 | mdev->state.conn == C_BEHIND || |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3533 | mdev->state.conn < C_CONNECTED || |
| 3534 | mdev->state.disk < D_NEGOTIATING); |
| 3535 | |
| 3536 | /* D_ASSERT( mdev->state.conn == C_WF_SYNC_UUID ); */ |
| 3537 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3538 | /* Here the _drbd_uuid_ functions are right, current should |
| 3539 | _not_ be rotated into the history */ |
| 3540 | if (get_ldev_if_state(mdev, D_NEGOTIATING)) { |
| 3541 | _drbd_uuid_set(mdev, UI_CURRENT, be64_to_cpu(p->uuid)); |
| 3542 | _drbd_uuid_set(mdev, UI_BITMAP, 0UL); |
| 3543 | |
Lars Ellenberg | 62b0da3 | 2011-01-20 13:25:21 +0100 | [diff] [blame] | 3544 | drbd_print_uuids(mdev, "updated sync uuid"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3545 | drbd_start_resync(mdev, C_SYNC_TARGET); |
| 3546 | |
| 3547 | put_ldev(mdev); |
| 3548 | } else |
| 3549 | dev_err(DEV, "Ignoring SyncUUID packet!\n"); |
| 3550 | |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3551 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3552 | } |
| 3553 | |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3554 | /** |
| 3555 | * receive_bitmap_plain |
| 3556 | * |
| 3557 | * Return 0 when done, 1 when another iteration is needed, and a negative error |
| 3558 | * code upon failure. |
| 3559 | */ |
| 3560 | static int |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3561 | receive_bitmap_plain(struct drbd_conf *mdev, unsigned int data_size, |
| 3562 | unsigned long *buffer, struct bm_xfer_ctx *c) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3563 | { |
| 3564 | unsigned num_words = min_t(size_t, BM_PACKET_WORDS, c->bm_words - c->word_offset); |
| 3565 | unsigned want = num_words * sizeof(long); |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3566 | int err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3567 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3568 | if (want != data_size) { |
| 3569 | 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] | 3570 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3571 | } |
| 3572 | if (want == 0) |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3573 | return 0; |
Philipp Reisner | de0ff33 | 2011-02-07 16:56:20 +0100 | [diff] [blame] | 3574 | err = drbd_recv(mdev->tconn, buffer, want); |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3575 | if (err != want) { |
| 3576 | if (err >= 0) |
| 3577 | err = -EIO; |
| 3578 | return err; |
| 3579 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3580 | |
| 3581 | drbd_bm_merge_lel(mdev, c->word_offset, num_words, buffer); |
| 3582 | |
| 3583 | c->word_offset += num_words; |
| 3584 | c->bit_offset = c->word_offset * BITS_PER_LONG; |
| 3585 | if (c->bit_offset > c->bm_bits) |
| 3586 | c->bit_offset = c->bm_bits; |
| 3587 | |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3588 | return 1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3589 | } |
| 3590 | |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3591 | /** |
| 3592 | * recv_bm_rle_bits |
| 3593 | * |
| 3594 | * Return 0 when done, 1 when another iteration is needed, and a negative error |
| 3595 | * code upon failure. |
| 3596 | */ |
| 3597 | static int |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3598 | recv_bm_rle_bits(struct drbd_conf *mdev, |
| 3599 | struct p_compressed_bm *p, |
Philipp Reisner | c6d25cf | 2011-01-19 16:13:06 +0100 | [diff] [blame] | 3600 | struct bm_xfer_ctx *c, |
| 3601 | unsigned int len) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3602 | { |
| 3603 | struct bitstream bs; |
| 3604 | u64 look_ahead; |
| 3605 | u64 rl; |
| 3606 | u64 tmp; |
| 3607 | unsigned long s = c->bit_offset; |
| 3608 | unsigned long e; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3609 | int toggle = DCBP_get_start(p); |
| 3610 | int have; |
| 3611 | int bits; |
| 3612 | |
| 3613 | bitstream_init(&bs, p->code, len, DCBP_get_pad_bits(p)); |
| 3614 | |
| 3615 | bits = bitstream_get_bits(&bs, &look_ahead, 64); |
| 3616 | if (bits < 0) |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3617 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3618 | |
| 3619 | for (have = bits; have > 0; s += rl, toggle = !toggle) { |
| 3620 | bits = vli_decode_bits(&rl, look_ahead); |
| 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 | if (toggle) { |
| 3625 | e = s + rl -1; |
| 3626 | if (e >= c->bm_bits) { |
| 3627 | 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] | 3628 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3629 | } |
| 3630 | _drbd_bm_set_bits(mdev, s, e); |
| 3631 | } |
| 3632 | |
| 3633 | if (have < bits) { |
| 3634 | dev_err(DEV, "bitmap decoding error: h:%d b:%d la:0x%08llx l:%u/%u\n", |
| 3635 | have, bits, look_ahead, |
| 3636 | (unsigned int)(bs.cur.b - p->code), |
| 3637 | (unsigned int)bs.buf_len); |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3638 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3639 | } |
| 3640 | look_ahead >>= bits; |
| 3641 | have -= bits; |
| 3642 | |
| 3643 | bits = bitstream_get_bits(&bs, &tmp, 64 - have); |
| 3644 | if (bits < 0) |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3645 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3646 | look_ahead |= tmp << have; |
| 3647 | have += bits; |
| 3648 | } |
| 3649 | |
| 3650 | c->bit_offset = s; |
| 3651 | bm_xfer_ctx_bit_to_word_offset(c); |
| 3652 | |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3653 | return (s != c->bm_bits); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3654 | } |
| 3655 | |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3656 | /** |
| 3657 | * decode_bitmap_c |
| 3658 | * |
| 3659 | * Return 0 when done, 1 when another iteration is needed, and a negative error |
| 3660 | * code upon failure. |
| 3661 | */ |
| 3662 | static int |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3663 | decode_bitmap_c(struct drbd_conf *mdev, |
| 3664 | struct p_compressed_bm *p, |
Philipp Reisner | c6d25cf | 2011-01-19 16:13:06 +0100 | [diff] [blame] | 3665 | struct bm_xfer_ctx *c, |
| 3666 | unsigned int len) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3667 | { |
| 3668 | if (DCBP_get_code(p) == RLE_VLI_Bits) |
Philipp Reisner | c6d25cf | 2011-01-19 16:13:06 +0100 | [diff] [blame] | 3669 | return recv_bm_rle_bits(mdev, p, c, len); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3670 | |
| 3671 | /* other variants had been implemented for evaluation, |
| 3672 | * but have been dropped as this one turned out to be "best" |
| 3673 | * during all our tests. */ |
| 3674 | |
| 3675 | dev_err(DEV, "receive_bitmap_c: unknown encoding %u\n", p->encoding); |
Philipp Reisner | 38fa998 | 2011-03-15 18:24:49 +0100 | [diff] [blame] | 3676 | conn_request_state(mdev->tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD); |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3677 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3678 | } |
| 3679 | |
| 3680 | void INFO_bm_xfer_stats(struct drbd_conf *mdev, |
| 3681 | const char *direction, struct bm_xfer_ctx *c) |
| 3682 | { |
| 3683 | /* what would it take to transfer it "plaintext" */ |
Philipp Reisner | c012949 | 2011-01-19 16:58:16 +0100 | [diff] [blame] | 3684 | unsigned plain = sizeof(struct p_header) * |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3685 | ((c->bm_words+BM_PACKET_WORDS-1)/BM_PACKET_WORDS+1) |
| 3686 | + c->bm_words * sizeof(long); |
| 3687 | unsigned total = c->bytes[0] + c->bytes[1]; |
| 3688 | unsigned r; |
| 3689 | |
| 3690 | /* total can not be zero. but just in case: */ |
| 3691 | if (total == 0) |
| 3692 | return; |
| 3693 | |
| 3694 | /* don't report if not compressed */ |
| 3695 | if (total >= plain) |
| 3696 | return; |
| 3697 | |
| 3698 | /* total < plain. check for overflow, still */ |
| 3699 | r = (total > UINT_MAX/1000) ? (total / (plain/1000)) |
| 3700 | : (1000 * total / plain); |
| 3701 | |
| 3702 | if (r > 1000) |
| 3703 | r = 1000; |
| 3704 | |
| 3705 | r = 1000 - r; |
| 3706 | dev_info(DEV, "%s bitmap stats [Bytes(packets)]: plain %u(%u), RLE %u(%u), " |
| 3707 | "total %u; compression: %u.%u%%\n", |
| 3708 | direction, |
| 3709 | c->bytes[1], c->packets[1], |
| 3710 | c->bytes[0], c->packets[0], |
| 3711 | total, r/10, r % 10); |
| 3712 | } |
| 3713 | |
| 3714 | /* Since we are processing the bitfield from lower addresses to higher, |
| 3715 | it does not matter if the process it in 32 bit chunks or 64 bit |
| 3716 | chunks as long as it is little endian. (Understand it as byte stream, |
| 3717 | beginning with the lowest byte...) If we would use big endian |
| 3718 | we would need to process it from the highest address to the lowest, |
| 3719 | in order to be agnostic to the 32 vs 64 bits issue. |
| 3720 | |
| 3721 | returns 0 on failure, 1 if we successfully received it. */ |
Andreas Gruenbacher | d876302 | 2011-01-26 17:39:41 +0100 | [diff] [blame] | 3722 | static int receive_bitmap(struct drbd_conf *mdev, enum drbd_packet cmd, |
| 3723 | unsigned int data_size) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3724 | { |
| 3725 | struct bm_xfer_ctx c; |
| 3726 | void *buffer; |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3727 | int err; |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3728 | int ok = false; |
Philipp Reisner | 257d0af | 2011-01-26 12:15:29 +0100 | [diff] [blame] | 3729 | struct p_header *h = &mdev->tconn->data.rbuf.header; |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 3730 | struct packet_info pi; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3731 | |
Lars Ellenberg | 20ceb2b | 2011-01-21 10:56:44 +0100 | [diff] [blame] | 3732 | drbd_bm_lock(mdev, "receive bitmap", BM_LOCKED_SET_ALLOWED); |
| 3733 | /* you are supposed to send additional out-of-sync information |
| 3734 | * if you actually set bits during this phase */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3735 | |
| 3736 | /* maybe we should use some per thread scratch page, |
| 3737 | * and allocate that during initial device creation? */ |
| 3738 | buffer = (unsigned long *) __get_free_page(GFP_NOIO); |
| 3739 | if (!buffer) { |
| 3740 | dev_err(DEV, "failed to allocate one page buffer in %s\n", __func__); |
| 3741 | goto out; |
| 3742 | } |
| 3743 | |
| 3744 | c = (struct bm_xfer_ctx) { |
| 3745 | .bm_bits = drbd_bm_bits(mdev), |
| 3746 | .bm_words = drbd_bm_words(mdev), |
| 3747 | }; |
| 3748 | |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3749 | for(;;) { |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3750 | if (cmd == P_BITMAP) { |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3751 | err = receive_bitmap_plain(mdev, data_size, buffer, &c); |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3752 | } else if (cmd == P_COMPRESSED_BITMAP) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3753 | /* MAYBE: sanity check that we speak proto >= 90, |
| 3754 | * and the feature is enabled! */ |
| 3755 | struct p_compressed_bm *p; |
| 3756 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3757 | if (data_size > BM_PACKET_PAYLOAD_BYTES) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3758 | dev_err(DEV, "ReportCBitmap packet too large\n"); |
| 3759 | goto out; |
| 3760 | } |
| 3761 | /* use the page buff */ |
| 3762 | p = buffer; |
| 3763 | memcpy(p, h, sizeof(*h)); |
Philipp Reisner | de0ff33 | 2011-02-07 16:56:20 +0100 | [diff] [blame] | 3764 | if (drbd_recv(mdev->tconn, p->head.payload, data_size) != data_size) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3765 | goto out; |
Lars Ellenberg | 004352f | 2010-10-05 20:13:58 +0200 | [diff] [blame] | 3766 | if (data_size <= (sizeof(*p) - sizeof(p->head))) { |
| 3767 | dev_err(DEV, "ReportCBitmap packet too small (l:%u)\n", data_size); |
Andreas Gruenbacher | 78fcbda | 2010-12-10 22:18:27 +0100 | [diff] [blame] | 3768 | goto out; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3769 | } |
Philipp Reisner | c6d25cf | 2011-01-19 16:13:06 +0100 | [diff] [blame] | 3770 | err = decode_bitmap_c(mdev, p, &c, data_size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3771 | } else { |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3772 | 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] | 3773 | goto out; |
| 3774 | } |
| 3775 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3776 | c.packets[cmd == P_BITMAP]++; |
Philipp Reisner | 257d0af | 2011-01-26 12:15:29 +0100 | [diff] [blame] | 3777 | c.bytes[cmd == P_BITMAP] += sizeof(struct p_header) + data_size; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3778 | |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3779 | if (err <= 0) { |
| 3780 | if (err < 0) |
| 3781 | goto out; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3782 | break; |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3783 | } |
Philipp Reisner | 9ba7aa0 | 2011-02-07 17:32:41 +0100 | [diff] [blame] | 3784 | if (!drbd_recv_header(mdev->tconn, &pi)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3785 | goto out; |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 3786 | cmd = pi.cmd; |
| 3787 | data_size = pi.size; |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3788 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3789 | |
| 3790 | INFO_bm_xfer_stats(mdev, "receive", &c); |
| 3791 | |
| 3792 | if (mdev->state.conn == C_WF_BITMAP_T) { |
Andreas Gruenbacher | de1f8e4 | 2010-12-10 21:04:00 +0100 | [diff] [blame] | 3793 | enum drbd_state_rv rv; |
| 3794 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3795 | ok = !drbd_send_bitmap(mdev); |
| 3796 | if (!ok) |
| 3797 | goto out; |
| 3798 | /* Omit CS_ORDERED with this state transition to avoid deadlocks. */ |
Andreas Gruenbacher | de1f8e4 | 2010-12-10 21:04:00 +0100 | [diff] [blame] | 3799 | rv = _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE); |
| 3800 | D_ASSERT(rv == SS_SUCCESS); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3801 | } else if (mdev->state.conn != C_WF_BITMAP_S) { |
| 3802 | /* admin may have requested C_DISCONNECTING, |
| 3803 | * other threads may have noticed network errors */ |
| 3804 | dev_info(DEV, "unexpected cstate (%s) in receive_bitmap\n", |
| 3805 | drbd_conn_str(mdev->state.conn)); |
| 3806 | } |
| 3807 | |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3808 | ok = true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3809 | out: |
Lars Ellenberg | 20ceb2b | 2011-01-21 10:56:44 +0100 | [diff] [blame] | 3810 | drbd_bm_unlock(mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3811 | if (ok && mdev->state.conn == C_WF_BITMAP_S) |
| 3812 | drbd_start_resync(mdev, C_SYNC_SOURCE); |
| 3813 | free_page((unsigned long) buffer); |
| 3814 | return ok; |
| 3815 | } |
| 3816 | |
Philipp Reisner | 2de876e | 2011-03-15 14:38:01 +0100 | [diff] [blame] | 3817 | 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] | 3818 | { |
| 3819 | /* TODO zero copy sink :) */ |
| 3820 | static char sink[128]; |
| 3821 | int size, want, r; |
| 3822 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3823 | size = data_size; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3824 | while (size > 0) { |
| 3825 | want = min_t(int, size, sizeof(sink)); |
Philipp Reisner | 2de876e | 2011-03-15 14:38:01 +0100 | [diff] [blame] | 3826 | r = drbd_recv(tconn, sink, want); |
| 3827 | if (r <= 0) |
Andreas Gruenbacher | 841ce24 | 2010-12-15 19:31:20 +0100 | [diff] [blame] | 3828 | break; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3829 | size -= r; |
| 3830 | } |
| 3831 | return size == 0; |
| 3832 | } |
| 3833 | |
Philipp Reisner | 2de876e | 2011-03-15 14:38:01 +0100 | [diff] [blame] | 3834 | static int receive_skip(struct drbd_conf *mdev, enum drbd_packet cmd, |
| 3835 | unsigned int data_size) |
| 3836 | { |
| 3837 | dev_warn(DEV, "skipping unknown optional packet type %d, l: %d!\n", |
| 3838 | cmd, data_size); |
| 3839 | |
| 3840 | return _tconn_receive_skip(mdev->tconn, data_size); |
| 3841 | } |
| 3842 | |
| 3843 | static int tconn_receive_skip(struct drbd_tconn *tconn, enum drbd_packet cmd, unsigned int data_size) |
| 3844 | { |
| 3845 | conn_warn(tconn, "skipping packet for non existing volume type %d, l: %d!\n", |
| 3846 | cmd, data_size); |
| 3847 | |
| 3848 | return _tconn_receive_skip(tconn, data_size); |
| 3849 | } |
| 3850 | |
Andreas Gruenbacher | d876302 | 2011-01-26 17:39:41 +0100 | [diff] [blame] | 3851 | static int receive_UnplugRemote(struct drbd_conf *mdev, enum drbd_packet cmd, |
| 3852 | unsigned int data_size) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3853 | { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3854 | /* Make sure we've acked all the TCP data associated |
| 3855 | * with the data requests being unplugged */ |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 3856 | drbd_tcp_quickack(mdev->tconn->data.socket); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3857 | |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3858 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3859 | } |
| 3860 | |
Andreas Gruenbacher | d876302 | 2011-01-26 17:39:41 +0100 | [diff] [blame] | 3861 | static int receive_out_of_sync(struct drbd_conf *mdev, enum drbd_packet cmd, |
| 3862 | unsigned int data_size) |
Philipp Reisner | 73a01a1 | 2010-10-27 14:33:00 +0200 | [diff] [blame] | 3863 | { |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 3864 | struct p_block_desc *p = &mdev->tconn->data.rbuf.block_desc; |
Philipp Reisner | 73a01a1 | 2010-10-27 14:33:00 +0200 | [diff] [blame] | 3865 | |
Lars Ellenberg | f735e363 | 2010-12-17 21:06:18 +0100 | [diff] [blame] | 3866 | switch (mdev->state.conn) { |
| 3867 | case C_WF_SYNC_UUID: |
| 3868 | case C_WF_BITMAP_T: |
| 3869 | case C_BEHIND: |
| 3870 | break; |
| 3871 | default: |
| 3872 | dev_err(DEV, "ASSERT FAILED cstate = %s, expected: WFSyncUUID|WFBitMapT|Behind\n", |
| 3873 | drbd_conn_str(mdev->state.conn)); |
| 3874 | } |
| 3875 | |
Philipp Reisner | 73a01a1 | 2010-10-27 14:33:00 +0200 | [diff] [blame] | 3876 | drbd_set_out_of_sync(mdev, be64_to_cpu(p->sector), be32_to_cpu(p->blksize)); |
| 3877 | |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 3878 | return true; |
Philipp Reisner | 73a01a1 | 2010-10-27 14:33:00 +0200 | [diff] [blame] | 3879 | } |
| 3880 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3881 | struct data_cmd { |
| 3882 | int expect_payload; |
| 3883 | size_t pkt_size; |
Philipp Reisner | a4fbda8 | 2011-03-16 11:13:17 +0100 | [diff] [blame] | 3884 | enum mdev_or_conn fa_type; /* first argument's type */ |
Philipp Reisner | d9ae84e | 2011-03-15 18:50:22 +0100 | [diff] [blame] | 3885 | union { |
| 3886 | int (*mdev_fn)(struct drbd_conf *, enum drbd_packet cmd, |
| 3887 | unsigned int to_receive); |
| 3888 | int (*conn_fn)(struct drbd_tconn *, enum drbd_packet cmd, |
| 3889 | unsigned int to_receive); |
| 3890 | }; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3891 | }; |
| 3892 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3893 | static struct data_cmd drbd_cmd_handler[] = { |
Philipp Reisner | d9ae84e | 2011-03-15 18:50:22 +0100 | [diff] [blame] | 3894 | [P_DATA] = { 1, sizeof(struct p_data), MDEV, { receive_Data } }, |
| 3895 | [P_DATA_REPLY] = { 1, sizeof(struct p_data), MDEV, { receive_DataReply } }, |
| 3896 | [P_RS_DATA_REPLY] = { 1, sizeof(struct p_data), MDEV, { receive_RSDataReply } } , |
| 3897 | [P_BARRIER] = { 0, sizeof(struct p_barrier), MDEV, { receive_Barrier } } , |
| 3898 | [P_BITMAP] = { 1, sizeof(struct p_header), MDEV, { receive_bitmap } } , |
| 3899 | [P_COMPRESSED_BITMAP] = { 1, sizeof(struct p_header), MDEV, { receive_bitmap } } , |
| 3900 | [P_UNPLUG_REMOTE] = { 0, sizeof(struct p_header), MDEV, { receive_UnplugRemote } }, |
| 3901 | [P_DATA_REQUEST] = { 0, sizeof(struct p_block_req), MDEV, { receive_DataRequest } }, |
| 3902 | [P_RS_DATA_REQUEST] = { 0, sizeof(struct p_block_req), MDEV, { receive_DataRequest } }, |
| 3903 | [P_SYNC_PARAM] = { 1, sizeof(struct p_header), MDEV, { receive_SyncParam } }, |
| 3904 | [P_SYNC_PARAM89] = { 1, sizeof(struct p_header), MDEV, { receive_SyncParam } }, |
Philipp Reisner | 7204624 | 2011-03-15 18:51:47 +0100 | [diff] [blame] | 3905 | [P_PROTOCOL] = { 1, sizeof(struct p_protocol), CONN, { .conn_fn = receive_protocol } }, |
Philipp Reisner | d9ae84e | 2011-03-15 18:50:22 +0100 | [diff] [blame] | 3906 | [P_UUIDS] = { 0, sizeof(struct p_uuids), MDEV, { receive_uuids } }, |
| 3907 | [P_SIZES] = { 0, sizeof(struct p_sizes), MDEV, { receive_sizes } }, |
| 3908 | [P_STATE] = { 0, sizeof(struct p_state), MDEV, { receive_state } }, |
| 3909 | [P_STATE_CHG_REQ] = { 0, sizeof(struct p_req_state), MDEV, { receive_req_state } }, |
| 3910 | [P_SYNC_UUID] = { 0, sizeof(struct p_rs_uuid), MDEV, { receive_sync_uuid } }, |
| 3911 | [P_OV_REQUEST] = { 0, sizeof(struct p_block_req), MDEV, { receive_DataRequest } }, |
| 3912 | [P_OV_REPLY] = { 1, sizeof(struct p_block_req), MDEV, { receive_DataRequest } }, |
| 3913 | [P_CSUM_RS_REQUEST] = { 1, sizeof(struct p_block_req), MDEV, { receive_DataRequest } }, |
| 3914 | [P_DELAY_PROBE] = { 0, sizeof(struct p_delay_probe93), MDEV, { receive_skip } }, |
| 3915 | [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] | 3916 | [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] | 3917 | }; |
| 3918 | |
| 3919 | /* 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] | 3920 | mdev->tconn->data.rbuf.header.head.payload. |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3921 | |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 3922 | 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] | 3923 | p_header, but they may not rely on that. Since there is also p_header95 ! |
| 3924 | */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3925 | |
Philipp Reisner | eefc2f7 | 2011-02-08 12:55:24 +0100 | [diff] [blame] | 3926 | static void drbdd(struct drbd_tconn *tconn) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3927 | { |
Philipp Reisner | eefc2f7 | 2011-02-08 12:55:24 +0100 | [diff] [blame] | 3928 | struct p_header *header = &tconn->data.rbuf.header; |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 3929 | struct packet_info pi; |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3930 | size_t shs; /* sub header size */ |
| 3931 | int rv; |
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 | while (get_t_state(&tconn->receiver) == RUNNING) { |
| 3934 | drbd_thread_current_set_cpu(&tconn->receiver); |
| 3935 | if (!drbd_recv_header(tconn, &pi)) |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3936 | goto err_out; |
| 3937 | |
Andreas Gruenbacher | 6e849ce | 2011-03-14 17:27:45 +0100 | [diff] [blame] | 3938 | if (unlikely(pi.cmd >= ARRAY_SIZE(drbd_cmd_handler) || |
Philipp Reisner | d9ae84e | 2011-03-15 18:50:22 +0100 | [diff] [blame] | 3939 | !drbd_cmd_handler[pi.cmd].mdev_fn)) { |
Philipp Reisner | eefc2f7 | 2011-02-08 12:55:24 +0100 | [diff] [blame] | 3940 | 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] | 3941 | goto err_out; |
Lars Ellenberg | 0b33a91 | 2009-11-16 15:58:04 +0100 | [diff] [blame] | 3942 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3943 | |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 3944 | shs = drbd_cmd_handler[pi.cmd].pkt_size - sizeof(struct p_header); |
| 3945 | if (pi.size - shs > 0 && !drbd_cmd_handler[pi.cmd].expect_payload) { |
Philipp Reisner | eefc2f7 | 2011-02-08 12:55:24 +0100 | [diff] [blame] | 3946 | 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] | 3947 | goto err_out; |
| 3948 | } |
| 3949 | |
Lars Ellenberg | c13f7e1 | 2010-10-29 23:32:01 +0200 | [diff] [blame] | 3950 | if (shs) { |
Philipp Reisner | eefc2f7 | 2011-02-08 12:55:24 +0100 | [diff] [blame] | 3951 | rv = drbd_recv(tconn, &header->payload, shs); |
Lars Ellenberg | c13f7e1 | 2010-10-29 23:32:01 +0200 | [diff] [blame] | 3952 | if (unlikely(rv != shs)) { |
Lars Ellenberg | 0ddc554 | 2011-01-21 12:35:15 +0100 | [diff] [blame] | 3953 | if (!signal_pending(current)) |
Philipp Reisner | eefc2f7 | 2011-02-08 12:55:24 +0100 | [diff] [blame] | 3954 | 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] | 3955 | goto err_out; |
| 3956 | } |
| 3957 | } |
| 3958 | |
Philipp Reisner | a4fbda8 | 2011-03-16 11:13:17 +0100 | [diff] [blame] | 3959 | if (drbd_cmd_handler[pi.cmd].fa_type == CONN) { |
Philipp Reisner | d9ae84e | 2011-03-15 18:50:22 +0100 | [diff] [blame] | 3960 | rv = drbd_cmd_handler[pi.cmd].conn_fn(tconn, pi.cmd, pi.size - shs); |
| 3961 | } else { |
| 3962 | struct drbd_conf *mdev = vnr_to_mdev(tconn, pi.vnr); |
| 3963 | rv = mdev ? |
| 3964 | drbd_cmd_handler[pi.cmd].mdev_fn(mdev, pi.cmd, pi.size - shs) : |
| 3965 | tconn_receive_skip(tconn, pi.cmd, pi.size - shs); |
| 3966 | } |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3967 | |
| 3968 | if (unlikely(!rv)) { |
Philipp Reisner | eefc2f7 | 2011-02-08 12:55:24 +0100 | [diff] [blame] | 3969 | conn_err(tconn, "error receiving %s, l: %d!\n", |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 3970 | cmdname(pi.cmd), pi.size); |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3971 | goto err_out; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3972 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3973 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3974 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 3975 | if (0) { |
| 3976 | err_out: |
Philipp Reisner | bbeb641 | 2011-02-10 13:45:46 +0100 | [diff] [blame] | 3977 | conn_request_state(tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD); |
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 | } |
| 3980 | |
Philipp Reisner | 0e29d16 | 2011-02-18 14:23:11 +0100 | [diff] [blame] | 3981 | void conn_flush_workqueue(struct drbd_tconn *tconn) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3982 | { |
| 3983 | struct drbd_wq_barrier barr; |
| 3984 | |
| 3985 | barr.w.cb = w_prev_work_done; |
Philipp Reisner | 0e29d16 | 2011-02-18 14:23:11 +0100 | [diff] [blame] | 3986 | barr.w.tconn = tconn; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3987 | init_completion(&barr.done); |
Philipp Reisner | 0e29d16 | 2011-02-18 14:23:11 +0100 | [diff] [blame] | 3988 | drbd_queue_work(&tconn->data.work, &barr.w); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3989 | wait_for_completion(&barr.done); |
| 3990 | } |
| 3991 | |
Philipp Reisner | 360cc74 | 2011-02-08 14:29:53 +0100 | [diff] [blame] | 3992 | static void drbd_disconnect(struct drbd_tconn *tconn) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3993 | { |
Philipp Reisner | bbeb641 | 2011-02-10 13:45:46 +0100 | [diff] [blame] | 3994 | enum drbd_conns oc; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3995 | int rv = SS_UNKNOWN_ERROR; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3996 | |
Philipp Reisner | bbeb641 | 2011-02-10 13:45:46 +0100 | [diff] [blame] | 3997 | if (tconn->cstate == C_STANDALONE) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3998 | return; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3999 | |
| 4000 | /* asender does not clean up anything. it must not interfere, either */ |
Philipp Reisner | 360cc74 | 2011-02-08 14:29:53 +0100 | [diff] [blame] | 4001 | drbd_thread_stop(&tconn->asender); |
| 4002 | drbd_free_sock(tconn); |
| 4003 | |
| 4004 | idr_for_each(&tconn->volumes, drbd_disconnected, tconn); |
| 4005 | |
| 4006 | conn_info(tconn, "Connection closed\n"); |
| 4007 | |
| 4008 | spin_lock_irq(&tconn->req_lock); |
Philipp Reisner | bbeb641 | 2011-02-10 13:45:46 +0100 | [diff] [blame] | 4009 | oc = tconn->cstate; |
| 4010 | if (oc >= C_UNCONNECTED) |
| 4011 | rv = _conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE); |
| 4012 | |
Philipp Reisner | 360cc74 | 2011-02-08 14:29:53 +0100 | [diff] [blame] | 4013 | spin_unlock_irq(&tconn->req_lock); |
| 4014 | |
Philipp Reisner | bbeb641 | 2011-02-10 13:45:46 +0100 | [diff] [blame] | 4015 | if (oc == C_DISCONNECTING) { |
Philipp Reisner | 360cc74 | 2011-02-08 14:29:53 +0100 | [diff] [blame] | 4016 | wait_event(tconn->net_cnt_wait, atomic_read(&tconn->net_cnt) == 0); |
| 4017 | |
| 4018 | crypto_free_hash(tconn->cram_hmac_tfm); |
| 4019 | tconn->cram_hmac_tfm = NULL; |
| 4020 | |
| 4021 | kfree(tconn->net_conf); |
| 4022 | tconn->net_conf = NULL; |
Philipp Reisner | bbeb641 | 2011-02-10 13:45:46 +0100 | [diff] [blame] | 4023 | conn_request_state(tconn, NS(conn, C_STANDALONE), CS_VERBOSE); |
Philipp Reisner | 360cc74 | 2011-02-08 14:29:53 +0100 | [diff] [blame] | 4024 | } |
| 4025 | } |
| 4026 | |
| 4027 | static int drbd_disconnected(int vnr, void *p, void *data) |
| 4028 | { |
| 4029 | struct drbd_conf *mdev = (struct drbd_conf *)p; |
| 4030 | enum drbd_fencing_p fp; |
| 4031 | unsigned int i; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4032 | |
Philipp Reisner | 8571957 | 2010-07-21 10:20:17 +0200 | [diff] [blame] | 4033 | /* wait for current activity to cease. */ |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 4034 | spin_lock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4035 | _drbd_wait_ee_list_empty(mdev, &mdev->active_ee); |
| 4036 | _drbd_wait_ee_list_empty(mdev, &mdev->sync_ee); |
| 4037 | _drbd_wait_ee_list_empty(mdev, &mdev->read_ee); |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 4038 | spin_unlock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4039 | |
| 4040 | /* We do not have data structures that would allow us to |
| 4041 | * get the rs_pending_cnt down to 0 again. |
| 4042 | * * On C_SYNC_TARGET we do not have any data structures describing |
| 4043 | * the pending RSDataRequest's we have sent. |
| 4044 | * * On C_SYNC_SOURCE there is no data structure that tracks |
| 4045 | * the P_RS_DATA_REPLY blocks that we sent to the SyncTarget. |
| 4046 | * And no, it is not the sum of the reference counts in the |
| 4047 | * resync_LRU. The resync_LRU tracks the whole operation including |
| 4048 | * the disk-IO, while the rs_pending_cnt only tracks the blocks |
| 4049 | * on the fly. */ |
| 4050 | drbd_rs_cancel_all(mdev); |
| 4051 | mdev->rs_total = 0; |
| 4052 | mdev->rs_failed = 0; |
| 4053 | atomic_set(&mdev->rs_pending_cnt, 0); |
| 4054 | wake_up(&mdev->misc_wait); |
| 4055 | |
Philipp Reisner | 7fde2be | 2011-03-01 11:08:28 +0100 | [diff] [blame] | 4056 | del_timer(&mdev->request_timer); |
| 4057 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4058 | del_timer_sync(&mdev->resync_timer); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4059 | resync_timer_fn((unsigned long)mdev); |
| 4060 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4061 | /* wait for all w_e_end_data_req, w_e_end_rsdata_req, w_send_barrier, |
| 4062 | * w_make_resync_request etc. which may still be on the worker queue |
| 4063 | * to be "canceled" */ |
Philipp Reisner | a21e929 | 2011-02-08 15:08:49 +0100 | [diff] [blame] | 4064 | drbd_flush_workqueue(mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4065 | |
| 4066 | /* This also does reclaim_net_ee(). If we do this too early, we might |
| 4067 | * miss some resync ee and pages.*/ |
| 4068 | drbd_process_done_ee(mdev); |
| 4069 | |
| 4070 | kfree(mdev->p_uuid); |
| 4071 | mdev->p_uuid = NULL; |
| 4072 | |
Philipp Reisner | fb22c40 | 2010-09-08 23:20:21 +0200 | [diff] [blame] | 4073 | if (!is_susp(mdev->state)) |
Philipp Reisner | 2f5cdd0 | 2011-02-21 14:29:27 +0100 | [diff] [blame] | 4074 | tl_clear(mdev->tconn); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4075 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4076 | drbd_md_sync(mdev); |
| 4077 | |
| 4078 | fp = FP_DONT_CARE; |
| 4079 | if (get_ldev(mdev)) { |
| 4080 | fp = mdev->ldev->dc.fencing; |
| 4081 | put_ldev(mdev); |
| 4082 | } |
| 4083 | |
Philipp Reisner | 87f7be4 | 2010-06-11 13:56:33 +0200 | [diff] [blame] | 4084 | if (mdev->state.role == R_PRIMARY && fp >= FP_RESOURCE && mdev->state.pdsk >= D_UNKNOWN) |
| 4085 | drbd_try_outdate_peer_async(mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4086 | |
Lars Ellenberg | 20ceb2b | 2011-01-21 10:56:44 +0100 | [diff] [blame] | 4087 | /* serialize with bitmap writeout triggered by the state change, |
| 4088 | * if any. */ |
| 4089 | wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags)); |
| 4090 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4091 | /* tcp_close and release of sendpage pages can be deferred. I don't |
| 4092 | * want to use SO_LINGER, because apparently it can be deferred for |
| 4093 | * more than 20 seconds (longest time I checked). |
| 4094 | * |
| 4095 | * Actually we don't care for exactly when the network stack does its |
| 4096 | * put_page(), but release our reference on these pages right here. |
| 4097 | */ |
| 4098 | i = drbd_release_ee(mdev, &mdev->net_ee); |
| 4099 | if (i) |
| 4100 | dev_info(DEV, "net_ee not empty, killed %u entries\n", i); |
Lars Ellenberg | 435f074 | 2010-09-06 12:30:25 +0200 | [diff] [blame] | 4101 | i = atomic_read(&mdev->pp_in_use_by_net); |
| 4102 | if (i) |
| 4103 | 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] | 4104 | i = atomic_read(&mdev->pp_in_use); |
| 4105 | if (i) |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 4106 | dev_info(DEV, "pp_in_use = %d, expected 0\n", i); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4107 | |
| 4108 | D_ASSERT(list_empty(&mdev->read_ee)); |
| 4109 | D_ASSERT(list_empty(&mdev->active_ee)); |
| 4110 | D_ASSERT(list_empty(&mdev->sync_ee)); |
| 4111 | D_ASSERT(list_empty(&mdev->done_ee)); |
| 4112 | |
| 4113 | /* ok, no more ee's on the fly, it is safe to reset the epoch_size */ |
| 4114 | atomic_set(&mdev->current_epoch->epoch_size, 0); |
| 4115 | D_ASSERT(list_empty(&mdev->current_epoch->list)); |
Philipp Reisner | 360cc74 | 2011-02-08 14:29:53 +0100 | [diff] [blame] | 4116 | |
| 4117 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4118 | } |
| 4119 | |
| 4120 | /* |
| 4121 | * We support PRO_VERSION_MIN to PRO_VERSION_MAX. The protocol version |
| 4122 | * we can agree on is stored in agreed_pro_version. |
| 4123 | * |
| 4124 | * feature flags and the reserved array should be enough room for future |
| 4125 | * enhancements of the handshake protocol, and possible plugins... |
| 4126 | * |
| 4127 | * for now, they are expected to be zero, but ignored. |
| 4128 | */ |
Philipp Reisner | 8a22ccc | 2011-02-07 16:47:12 +0100 | [diff] [blame] | 4129 | static int drbd_send_handshake(struct drbd_tconn *tconn) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4130 | { |
Philipp Reisner | e6b3ea8 | 2011-01-19 14:02:01 +0100 | [diff] [blame] | 4131 | /* ASSERT current == mdev->tconn->receiver ... */ |
Philipp Reisner | 8a22ccc | 2011-02-07 16:47:12 +0100 | [diff] [blame] | 4132 | struct p_handshake *p = &tconn->data.sbuf.handshake; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4133 | int ok; |
| 4134 | |
Philipp Reisner | 8a22ccc | 2011-02-07 16:47:12 +0100 | [diff] [blame] | 4135 | if (mutex_lock_interruptible(&tconn->data.mutex)) { |
| 4136 | conn_err(tconn, "interrupted during initial handshake\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4137 | return 0; /* interrupted. not ok. */ |
| 4138 | } |
| 4139 | |
Philipp Reisner | 8a22ccc | 2011-02-07 16:47:12 +0100 | [diff] [blame] | 4140 | if (tconn->data.socket == NULL) { |
| 4141 | mutex_unlock(&tconn->data.mutex); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4142 | return 0; |
| 4143 | } |
| 4144 | |
| 4145 | memset(p, 0, sizeof(*p)); |
| 4146 | p->protocol_min = cpu_to_be32(PRO_VERSION_MIN); |
| 4147 | p->protocol_max = cpu_to_be32(PRO_VERSION_MAX); |
Philipp Reisner | 8a22ccc | 2011-02-07 16:47:12 +0100 | [diff] [blame] | 4148 | ok = _conn_send_cmd(tconn, 0, tconn->data.socket, P_HAND_SHAKE, |
| 4149 | &p->head, sizeof(*p), 0); |
| 4150 | mutex_unlock(&tconn->data.mutex); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4151 | return ok; |
| 4152 | } |
| 4153 | |
| 4154 | /* |
| 4155 | * return values: |
| 4156 | * 1 yes, we have a valid connection |
| 4157 | * 0 oops, did not work out, please try again |
| 4158 | * -1 peer talks different language, |
| 4159 | * no point in trying again, please go standalone. |
| 4160 | */ |
Philipp Reisner | 65d11ed | 2011-02-07 17:35:59 +0100 | [diff] [blame] | 4161 | static int drbd_do_handshake(struct drbd_tconn *tconn) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4162 | { |
Philipp Reisner | 65d11ed | 2011-02-07 17:35:59 +0100 | [diff] [blame] | 4163 | /* ASSERT current == tconn->receiver ... */ |
| 4164 | struct p_handshake *p = &tconn->data.rbuf.handshake; |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4165 | const int expect = sizeof(struct p_handshake) - sizeof(struct p_header80); |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4166 | struct packet_info pi; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4167 | int rv; |
| 4168 | |
Philipp Reisner | 65d11ed | 2011-02-07 17:35:59 +0100 | [diff] [blame] | 4169 | rv = drbd_send_handshake(tconn); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4170 | if (!rv) |
| 4171 | return 0; |
| 4172 | |
Philipp Reisner | 65d11ed | 2011-02-07 17:35:59 +0100 | [diff] [blame] | 4173 | rv = drbd_recv_header(tconn, &pi); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4174 | if (!rv) |
| 4175 | return 0; |
| 4176 | |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4177 | if (pi.cmd != P_HAND_SHAKE) { |
Philipp Reisner | 65d11ed | 2011-02-07 17:35:59 +0100 | [diff] [blame] | 4178 | conn_err(tconn, "expected HandShake packet, received: %s (0x%04x)\n", |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4179 | cmdname(pi.cmd), pi.cmd); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4180 | return -1; |
| 4181 | } |
| 4182 | |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4183 | if (pi.size != expect) { |
Philipp Reisner | 65d11ed | 2011-02-07 17:35:59 +0100 | [diff] [blame] | 4184 | conn_err(tconn, "expected HandShake length: %u, received: %u\n", |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4185 | expect, pi.size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4186 | return -1; |
| 4187 | } |
| 4188 | |
Philipp Reisner | 65d11ed | 2011-02-07 17:35:59 +0100 | [diff] [blame] | 4189 | rv = drbd_recv(tconn, &p->head.payload, expect); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4190 | |
| 4191 | if (rv != expect) { |
Lars Ellenberg | 0ddc554 | 2011-01-21 12:35:15 +0100 | [diff] [blame] | 4192 | if (!signal_pending(current)) |
Philipp Reisner | 65d11ed | 2011-02-07 17:35:59 +0100 | [diff] [blame] | 4193 | conn_warn(tconn, "short read receiving handshake packet: l=%u\n", rv); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4194 | return 0; |
| 4195 | } |
| 4196 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4197 | p->protocol_min = be32_to_cpu(p->protocol_min); |
| 4198 | p->protocol_max = be32_to_cpu(p->protocol_max); |
| 4199 | if (p->protocol_max == 0) |
| 4200 | p->protocol_max = p->protocol_min; |
| 4201 | |
| 4202 | if (PRO_VERSION_MAX < p->protocol_min || |
| 4203 | PRO_VERSION_MIN > p->protocol_max) |
| 4204 | goto incompat; |
| 4205 | |
Philipp Reisner | 65d11ed | 2011-02-07 17:35:59 +0100 | [diff] [blame] | 4206 | 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] | 4207 | |
Philipp Reisner | 65d11ed | 2011-02-07 17:35:59 +0100 | [diff] [blame] | 4208 | conn_info(tconn, "Handshake successful: " |
| 4209 | "Agreed network protocol version %d\n", tconn->agreed_pro_version); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4210 | |
| 4211 | return 1; |
| 4212 | |
| 4213 | incompat: |
Philipp Reisner | 65d11ed | 2011-02-07 17:35:59 +0100 | [diff] [blame] | 4214 | conn_err(tconn, "incompatible DRBD dialects: " |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4215 | "I support %d-%d, peer supports %d-%d\n", |
| 4216 | PRO_VERSION_MIN, PRO_VERSION_MAX, |
| 4217 | p->protocol_min, p->protocol_max); |
| 4218 | return -1; |
| 4219 | } |
| 4220 | |
| 4221 | #if !defined(CONFIG_CRYPTO_HMAC) && !defined(CONFIG_CRYPTO_HMAC_MODULE) |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4222 | static int drbd_do_auth(struct drbd_tconn *tconn) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4223 | { |
| 4224 | dev_err(DEV, "This kernel was build without CONFIG_CRYPTO_HMAC.\n"); |
| 4225 | 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] | 4226 | return -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4227 | } |
| 4228 | #else |
| 4229 | #define CHALLENGE_LEN 64 |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 4230 | |
| 4231 | /* Return value: |
| 4232 | 1 - auth succeeded, |
| 4233 | 0 - failed, try again (network error), |
| 4234 | -1 - auth failed, don't try again. |
| 4235 | */ |
| 4236 | |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4237 | static int drbd_do_auth(struct drbd_tconn *tconn) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4238 | { |
| 4239 | char my_challenge[CHALLENGE_LEN]; /* 64 Bytes... */ |
| 4240 | struct scatterlist sg; |
| 4241 | char *response = NULL; |
| 4242 | char *right_response = NULL; |
| 4243 | char *peers_ch = NULL; |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4244 | unsigned int key_len = strlen(tconn->net_conf->shared_secret); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4245 | unsigned int resp_size; |
| 4246 | struct hash_desc desc; |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4247 | struct packet_info pi; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4248 | int rv; |
| 4249 | |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4250 | desc.tfm = tconn->cram_hmac_tfm; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4251 | desc.flags = 0; |
| 4252 | |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4253 | rv = crypto_hash_setkey(tconn->cram_hmac_tfm, |
| 4254 | (u8 *)tconn->net_conf->shared_secret, key_len); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4255 | if (rv) { |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4256 | conn_err(tconn, "crypto_hash_setkey() failed with %d\n", rv); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 4257 | rv = -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4258 | goto fail; |
| 4259 | } |
| 4260 | |
| 4261 | get_random_bytes(my_challenge, CHALLENGE_LEN); |
| 4262 | |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4263 | rv = conn_send_cmd2(tconn, P_AUTH_CHALLENGE, my_challenge, CHALLENGE_LEN); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4264 | if (!rv) |
| 4265 | goto fail; |
| 4266 | |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4267 | rv = drbd_recv_header(tconn, &pi); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4268 | if (!rv) |
| 4269 | goto fail; |
| 4270 | |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4271 | if (pi.cmd != P_AUTH_CHALLENGE) { |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4272 | conn_err(tconn, "expected AuthChallenge packet, received: %s (0x%04x)\n", |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4273 | cmdname(pi.cmd), pi.cmd); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4274 | rv = 0; |
| 4275 | goto fail; |
| 4276 | } |
| 4277 | |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4278 | if (pi.size > CHALLENGE_LEN * 2) { |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4279 | conn_err(tconn, "expected AuthChallenge payload too big.\n"); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 4280 | rv = -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4281 | goto fail; |
| 4282 | } |
| 4283 | |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4284 | peers_ch = kmalloc(pi.size, GFP_NOIO); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4285 | if (peers_ch == NULL) { |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4286 | conn_err(tconn, "kmalloc of peers_ch failed\n"); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 4287 | rv = -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4288 | goto fail; |
| 4289 | } |
| 4290 | |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4291 | rv = drbd_recv(tconn, peers_ch, pi.size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4292 | |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4293 | if (rv != pi.size) { |
Lars Ellenberg | 0ddc554 | 2011-01-21 12:35:15 +0100 | [diff] [blame] | 4294 | if (!signal_pending(current)) |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4295 | conn_warn(tconn, "short read AuthChallenge: l=%u\n", rv); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4296 | rv = 0; |
| 4297 | goto fail; |
| 4298 | } |
| 4299 | |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4300 | resp_size = crypto_hash_digestsize(tconn->cram_hmac_tfm); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4301 | response = kmalloc(resp_size, GFP_NOIO); |
| 4302 | if (response == NULL) { |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4303 | conn_err(tconn, "kmalloc of response failed\n"); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 4304 | rv = -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4305 | goto fail; |
| 4306 | } |
| 4307 | |
| 4308 | sg_init_table(&sg, 1); |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4309 | sg_set_buf(&sg, peers_ch, pi.size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4310 | |
| 4311 | rv = crypto_hash_digest(&desc, &sg, sg.length, response); |
| 4312 | if (rv) { |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4313 | conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 4314 | rv = -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4315 | goto fail; |
| 4316 | } |
| 4317 | |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4318 | rv = conn_send_cmd2(tconn, P_AUTH_RESPONSE, response, resp_size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4319 | if (!rv) |
| 4320 | goto fail; |
| 4321 | |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4322 | rv = drbd_recv_header(tconn, &pi); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4323 | if (!rv) |
| 4324 | goto fail; |
| 4325 | |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4326 | if (pi.cmd != P_AUTH_RESPONSE) { |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4327 | conn_err(tconn, "expected AuthResponse packet, received: %s (0x%04x)\n", |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4328 | cmdname(pi.cmd), pi.cmd); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4329 | rv = 0; |
| 4330 | goto fail; |
| 4331 | } |
| 4332 | |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4333 | if (pi.size != resp_size) { |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4334 | conn_err(tconn, "expected AuthResponse payload of wrong size\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4335 | rv = 0; |
| 4336 | goto fail; |
| 4337 | } |
| 4338 | |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4339 | rv = drbd_recv(tconn, response , resp_size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4340 | |
| 4341 | if (rv != resp_size) { |
Lars Ellenberg | 0ddc554 | 2011-01-21 12:35:15 +0100 | [diff] [blame] | 4342 | if (!signal_pending(current)) |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4343 | conn_warn(tconn, "short read receiving AuthResponse: l=%u\n", rv); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4344 | rv = 0; |
| 4345 | goto fail; |
| 4346 | } |
| 4347 | |
| 4348 | right_response = kmalloc(resp_size, GFP_NOIO); |
Julia Lawall | 2d1ee87 | 2009-12-27 22:27:11 +0100 | [diff] [blame] | 4349 | if (right_response == NULL) { |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4350 | conn_err(tconn, "kmalloc of right_response failed\n"); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 4351 | rv = -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4352 | goto fail; |
| 4353 | } |
| 4354 | |
| 4355 | sg_set_buf(&sg, my_challenge, CHALLENGE_LEN); |
| 4356 | |
| 4357 | rv = crypto_hash_digest(&desc, &sg, sg.length, right_response); |
| 4358 | if (rv) { |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4359 | conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 4360 | rv = -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4361 | goto fail; |
| 4362 | } |
| 4363 | |
| 4364 | rv = !memcmp(response, right_response, resp_size); |
| 4365 | |
| 4366 | if (rv) |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4367 | conn_info(tconn, "Peer authenticated using %d bytes of '%s' HMAC\n", |
| 4368 | resp_size, tconn->net_conf->cram_hmac_alg); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 4369 | else |
| 4370 | rv = -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4371 | |
| 4372 | fail: |
| 4373 | kfree(peers_ch); |
| 4374 | kfree(response); |
| 4375 | kfree(right_response); |
| 4376 | |
| 4377 | return rv; |
| 4378 | } |
| 4379 | #endif |
| 4380 | |
| 4381 | int drbdd_init(struct drbd_thread *thi) |
| 4382 | { |
Philipp Reisner | 392c880 | 2011-02-09 10:33:31 +0100 | [diff] [blame] | 4383 | struct drbd_tconn *tconn = thi->tconn; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4384 | int h; |
| 4385 | |
Philipp Reisner | 4d641dd | 2011-02-08 15:40:24 +0100 | [diff] [blame] | 4386 | conn_info(tconn, "receiver (re)started\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4387 | |
| 4388 | do { |
Philipp Reisner | 4d641dd | 2011-02-08 15:40:24 +0100 | [diff] [blame] | 4389 | h = drbd_connect(tconn); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4390 | if (h == 0) { |
Philipp Reisner | 4d641dd | 2011-02-08 15:40:24 +0100 | [diff] [blame] | 4391 | drbd_disconnect(tconn); |
Philipp Reisner | 20ee639 | 2011-01-18 15:28:59 +0100 | [diff] [blame] | 4392 | schedule_timeout_interruptible(HZ); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4393 | } |
| 4394 | if (h == -1) { |
Philipp Reisner | 4d641dd | 2011-02-08 15:40:24 +0100 | [diff] [blame] | 4395 | conn_warn(tconn, "Discarding network configuration.\n"); |
Philipp Reisner | bbeb641 | 2011-02-10 13:45:46 +0100 | [diff] [blame] | 4396 | conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4397 | } |
| 4398 | } while (h == 0); |
| 4399 | |
| 4400 | if (h > 0) { |
Philipp Reisner | 4d641dd | 2011-02-08 15:40:24 +0100 | [diff] [blame] | 4401 | if (get_net_conf(tconn)) { |
| 4402 | drbdd(tconn); |
| 4403 | put_net_conf(tconn); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4404 | } |
| 4405 | } |
| 4406 | |
Philipp Reisner | 4d641dd | 2011-02-08 15:40:24 +0100 | [diff] [blame] | 4407 | drbd_disconnect(tconn); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4408 | |
Philipp Reisner | 4d641dd | 2011-02-08 15:40:24 +0100 | [diff] [blame] | 4409 | conn_info(tconn, "receiver terminated\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4410 | return 0; |
| 4411 | } |
| 4412 | |
| 4413 | /* ********* acknowledge sender ******** */ |
| 4414 | |
Andreas Gruenbacher | d876302 | 2011-01-26 17:39:41 +0100 | [diff] [blame] | 4415 | static int got_RqSReply(struct drbd_conf *mdev, enum drbd_packet cmd) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4416 | { |
Philipp Reisner | 257d0af | 2011-01-26 12:15:29 +0100 | [diff] [blame] | 4417 | struct p_req_state_reply *p = &mdev->tconn->meta.rbuf.req_state_reply; |
Philipp Reisner | fc3b10a | 2011-02-15 11:07:59 +0100 | [diff] [blame] | 4418 | struct drbd_tconn *tconn = mdev->tconn; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4419 | |
| 4420 | int retcode = be32_to_cpu(p->retcode); |
| 4421 | |
Philipp Reisner | fc3b10a | 2011-02-15 11:07:59 +0100 | [diff] [blame] | 4422 | if (cmd == P_STATE_CHG_REPLY) { |
| 4423 | if (retcode >= SS_SUCCESS) { |
| 4424 | set_bit(CL_ST_CHG_SUCCESS, &mdev->flags); |
| 4425 | } else { |
| 4426 | set_bit(CL_ST_CHG_FAIL, &mdev->flags); |
| 4427 | dev_err(DEV, "Requested state change failed by peer: %s (%d)\n", |
| 4428 | drbd_set_st_err_str(retcode), retcode); |
| 4429 | } |
| 4430 | wake_up(&mdev->state_wait); |
| 4431 | } else /* conn == P_CONN_ST_CHG_REPLY */ { |
| 4432 | if (retcode >= SS_SUCCESS) { |
| 4433 | set_bit(CONN_WD_ST_CHG_OKAY, &tconn->flags); |
| 4434 | } else { |
| 4435 | set_bit(CONN_WD_ST_CHG_FAIL, &tconn->flags); |
| 4436 | conn_err(tconn, "Requested state change failed by peer: %s (%d)\n", |
| 4437 | drbd_set_st_err_str(retcode), retcode); |
| 4438 | } |
| 4439 | wake_up(&tconn->ping_wait); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4440 | } |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 4441 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4442 | } |
| 4443 | |
Philipp Reisner | f19e4f8 | 2011-03-16 11:21:50 +0100 | [diff] [blame^] | 4444 | static int got_Ping(struct drbd_tconn *tconn, enum drbd_packet cmd) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4445 | { |
Philipp Reisner | f19e4f8 | 2011-03-16 11:21:50 +0100 | [diff] [blame^] | 4446 | return drbd_send_ping_ack(tconn); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4447 | |
| 4448 | } |
| 4449 | |
Philipp Reisner | f19e4f8 | 2011-03-16 11:21:50 +0100 | [diff] [blame^] | 4450 | static int got_PingAck(struct drbd_tconn *tconn, enum drbd_packet cmd) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4451 | { |
| 4452 | /* restore idle timeout */ |
Philipp Reisner | 2a67d8b | 2011-02-09 14:10:32 +0100 | [diff] [blame] | 4453 | tconn->meta.socket->sk->sk_rcvtimeo = tconn->net_conf->ping_int*HZ; |
| 4454 | if (!test_and_set_bit(GOT_PING_ACK, &tconn->flags)) |
| 4455 | wake_up(&tconn->ping_wait); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4456 | |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 4457 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4458 | } |
| 4459 | |
Andreas Gruenbacher | d876302 | 2011-01-26 17:39:41 +0100 | [diff] [blame] | 4460 | static int got_IsInSync(struct drbd_conf *mdev, enum drbd_packet cmd) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4461 | { |
Philipp Reisner | 257d0af | 2011-01-26 12:15:29 +0100 | [diff] [blame] | 4462 | struct p_block_ack *p = &mdev->tconn->meta.rbuf.block_ack; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4463 | sector_t sector = be64_to_cpu(p->sector); |
| 4464 | int blksize = be32_to_cpu(p->blksize); |
| 4465 | |
Philipp Reisner | 31890f4 | 2011-01-19 14:12:51 +0100 | [diff] [blame] | 4466 | D_ASSERT(mdev->tconn->agreed_pro_version >= 89); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4467 | |
| 4468 | update_peer_seq(mdev, be32_to_cpu(p->seq_num)); |
| 4469 | |
Lars Ellenberg | 1d53f09 | 2010-09-05 01:13:24 +0200 | [diff] [blame] | 4470 | if (get_ldev(mdev)) { |
| 4471 | drbd_rs_complete_io(mdev, sector); |
| 4472 | drbd_set_in_sync(mdev, sector, blksize); |
| 4473 | /* rs_same_csums is supposed to count in units of BM_BLOCK_SIZE */ |
| 4474 | mdev->rs_same_csum += (blksize >> BM_BLOCK_SHIFT); |
| 4475 | put_ldev(mdev); |
| 4476 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4477 | dec_rs_pending(mdev); |
Philipp Reisner | 778f271 | 2010-07-06 11:14:00 +0200 | [diff] [blame] | 4478 | atomic_add(blksize >> 9, &mdev->rs_sect_in); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4479 | |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 4480 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4481 | } |
| 4482 | |
Andreas Gruenbacher | bc9c5c4 | 2011-01-21 18:00:55 +0100 | [diff] [blame] | 4483 | static int |
| 4484 | validate_req_change_req_state(struct drbd_conf *mdev, u64 id, sector_t sector, |
| 4485 | struct rb_root *root, const char *func, |
| 4486 | enum drbd_req_event what, bool missing_ok) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4487 | { |
| 4488 | struct drbd_request *req; |
| 4489 | struct bio_and_error m; |
| 4490 | |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 4491 | spin_lock_irq(&mdev->tconn->req_lock); |
Andreas Gruenbacher | bc9c5c4 | 2011-01-21 18:00:55 +0100 | [diff] [blame] | 4492 | req = find_request(mdev, root, id, sector, missing_ok, func); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4493 | if (unlikely(!req)) { |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 4494 | spin_unlock_irq(&mdev->tconn->req_lock); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 4495 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4496 | } |
| 4497 | __req_mod(req, what, &m); |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 4498 | spin_unlock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4499 | |
| 4500 | if (m.bio) |
| 4501 | complete_master_bio(mdev, &m); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 4502 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4503 | } |
| 4504 | |
Andreas Gruenbacher | d876302 | 2011-01-26 17:39:41 +0100 | [diff] [blame] | 4505 | static int got_BlockAck(struct drbd_conf *mdev, enum drbd_packet cmd) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4506 | { |
Philipp Reisner | 257d0af | 2011-01-26 12:15:29 +0100 | [diff] [blame] | 4507 | struct p_block_ack *p = &mdev->tconn->meta.rbuf.block_ack; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4508 | sector_t sector = be64_to_cpu(p->sector); |
| 4509 | int blksize = be32_to_cpu(p->blksize); |
| 4510 | enum drbd_req_event what; |
| 4511 | |
| 4512 | update_peer_seq(mdev, be32_to_cpu(p->seq_num)); |
| 4513 | |
Andreas Gruenbacher | 579b57e | 2011-01-13 18:40:57 +0100 | [diff] [blame] | 4514 | if (p->block_id == ID_SYNCER) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4515 | drbd_set_in_sync(mdev, sector, blksize); |
| 4516 | dec_rs_pending(mdev); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 4517 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4518 | } |
Philipp Reisner | 257d0af | 2011-01-26 12:15:29 +0100 | [diff] [blame] | 4519 | switch (cmd) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4520 | case P_RS_WRITE_ACK: |
Philipp Reisner | 89e58e7 | 2011-01-19 13:12:45 +0100 | [diff] [blame] | 4521 | D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C); |
Andreas Gruenbacher | 8554df1 | 2011-01-25 15:37:43 +0100 | [diff] [blame] | 4522 | what = WRITE_ACKED_BY_PEER_AND_SIS; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4523 | break; |
| 4524 | case P_WRITE_ACK: |
Philipp Reisner | 89e58e7 | 2011-01-19 13:12:45 +0100 | [diff] [blame] | 4525 | D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C); |
Andreas Gruenbacher | 8554df1 | 2011-01-25 15:37:43 +0100 | [diff] [blame] | 4526 | what = WRITE_ACKED_BY_PEER; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4527 | break; |
| 4528 | case P_RECV_ACK: |
Philipp Reisner | 89e58e7 | 2011-01-19 13:12:45 +0100 | [diff] [blame] | 4529 | D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_B); |
Andreas Gruenbacher | 8554df1 | 2011-01-25 15:37:43 +0100 | [diff] [blame] | 4530 | what = RECV_ACKED_BY_PEER; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4531 | break; |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 4532 | case P_DISCARD_WRITE: |
Philipp Reisner | 89e58e7 | 2011-01-19 13:12:45 +0100 | [diff] [blame] | 4533 | D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C); |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 4534 | what = DISCARD_WRITE; |
| 4535 | break; |
| 4536 | case P_RETRY_WRITE: |
| 4537 | D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C); |
| 4538 | what = POSTPONE_WRITE; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4539 | break; |
| 4540 | default: |
| 4541 | D_ASSERT(0); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 4542 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4543 | } |
| 4544 | |
| 4545 | return validate_req_change_req_state(mdev, p->block_id, sector, |
Andreas Gruenbacher | bc9c5c4 | 2011-01-21 18:00:55 +0100 | [diff] [blame] | 4546 | &mdev->write_requests, __func__, |
| 4547 | what, false); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4548 | } |
| 4549 | |
Andreas Gruenbacher | d876302 | 2011-01-26 17:39:41 +0100 | [diff] [blame] | 4550 | static int got_NegAck(struct drbd_conf *mdev, enum drbd_packet cmd) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4551 | { |
Philipp Reisner | 257d0af | 2011-01-26 12:15:29 +0100 | [diff] [blame] | 4552 | struct p_block_ack *p = &mdev->tconn->meta.rbuf.block_ack; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4553 | sector_t sector = be64_to_cpu(p->sector); |
Philipp Reisner | 2deb833 | 2011-01-17 18:39:18 +0100 | [diff] [blame] | 4554 | int size = be32_to_cpu(p->blksize); |
Philipp Reisner | 89e58e7 | 2011-01-19 13:12:45 +0100 | [diff] [blame] | 4555 | bool missing_ok = mdev->tconn->net_conf->wire_protocol == DRBD_PROT_A || |
| 4556 | mdev->tconn->net_conf->wire_protocol == DRBD_PROT_B; |
Andreas Gruenbacher | c3afd8f | 2011-01-20 22:25:40 +0100 | [diff] [blame] | 4557 | bool found; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4558 | |
| 4559 | update_peer_seq(mdev, be32_to_cpu(p->seq_num)); |
| 4560 | |
Andreas Gruenbacher | 579b57e | 2011-01-13 18:40:57 +0100 | [diff] [blame] | 4561 | if (p->block_id == ID_SYNCER) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4562 | dec_rs_pending(mdev); |
| 4563 | drbd_rs_failed_io(mdev, sector, size); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 4564 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4565 | } |
Philipp Reisner | 2deb833 | 2011-01-17 18:39:18 +0100 | [diff] [blame] | 4566 | |
Andreas Gruenbacher | c3afd8f | 2011-01-20 22:25:40 +0100 | [diff] [blame] | 4567 | found = validate_req_change_req_state(mdev, p->block_id, sector, |
Andreas Gruenbacher | bc9c5c4 | 2011-01-21 18:00:55 +0100 | [diff] [blame] | 4568 | &mdev->write_requests, __func__, |
Andreas Gruenbacher | 8554df1 | 2011-01-25 15:37:43 +0100 | [diff] [blame] | 4569 | NEG_ACKED, missing_ok); |
Andreas Gruenbacher | c3afd8f | 2011-01-20 22:25:40 +0100 | [diff] [blame] | 4570 | if (!found) { |
| 4571 | /* Protocol A has no P_WRITE_ACKs, but has P_NEG_ACKs. |
| 4572 | The master bio might already be completed, therefore the |
| 4573 | request is no longer in the collision hash. */ |
| 4574 | /* In Protocol B we might already have got a P_RECV_ACK |
| 4575 | but then get a P_NEG_ACK afterwards. */ |
| 4576 | if (!missing_ok) |
Philipp Reisner | 2deb833 | 2011-01-17 18:39:18 +0100 | [diff] [blame] | 4577 | return false; |
Andreas Gruenbacher | c3afd8f | 2011-01-20 22:25:40 +0100 | [diff] [blame] | 4578 | drbd_set_out_of_sync(mdev, sector, size); |
Philipp Reisner | 2deb833 | 2011-01-17 18:39:18 +0100 | [diff] [blame] | 4579 | } |
Philipp Reisner | 2deb833 | 2011-01-17 18:39:18 +0100 | [diff] [blame] | 4580 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4581 | } |
| 4582 | |
Andreas Gruenbacher | d876302 | 2011-01-26 17:39:41 +0100 | [diff] [blame] | 4583 | static int got_NegDReply(struct drbd_conf *mdev, enum drbd_packet cmd) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4584 | { |
Philipp Reisner | 257d0af | 2011-01-26 12:15:29 +0100 | [diff] [blame] | 4585 | struct p_block_ack *p = &mdev->tconn->meta.rbuf.block_ack; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4586 | sector_t sector = be64_to_cpu(p->sector); |
| 4587 | |
| 4588 | update_peer_seq(mdev, be32_to_cpu(p->seq_num)); |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 4589 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4590 | dev_err(DEV, "Got NegDReply; Sector %llus, len %u; Fail original request.\n", |
| 4591 | (unsigned long long)sector, be32_to_cpu(p->blksize)); |
| 4592 | |
| 4593 | return validate_req_change_req_state(mdev, p->block_id, sector, |
Andreas Gruenbacher | bc9c5c4 | 2011-01-21 18:00:55 +0100 | [diff] [blame] | 4594 | &mdev->read_requests, __func__, |
Andreas Gruenbacher | 8554df1 | 2011-01-25 15:37:43 +0100 | [diff] [blame] | 4595 | NEG_ACKED, false); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4596 | } |
| 4597 | |
Andreas Gruenbacher | d876302 | 2011-01-26 17:39:41 +0100 | [diff] [blame] | 4598 | static int got_NegRSDReply(struct drbd_conf *mdev, enum drbd_packet cmd) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4599 | { |
| 4600 | sector_t sector; |
| 4601 | int size; |
Philipp Reisner | 257d0af | 2011-01-26 12:15:29 +0100 | [diff] [blame] | 4602 | struct p_block_ack *p = &mdev->tconn->meta.rbuf.block_ack; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4603 | |
| 4604 | sector = be64_to_cpu(p->sector); |
| 4605 | size = be32_to_cpu(p->blksize); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4606 | |
| 4607 | update_peer_seq(mdev, be32_to_cpu(p->seq_num)); |
| 4608 | |
| 4609 | dec_rs_pending(mdev); |
| 4610 | |
| 4611 | if (get_ldev_if_state(mdev, D_FAILED)) { |
| 4612 | drbd_rs_complete_io(mdev, sector); |
Philipp Reisner | 257d0af | 2011-01-26 12:15:29 +0100 | [diff] [blame] | 4613 | switch (cmd) { |
Philipp Reisner | d612d30 | 2010-12-27 10:53:28 +0100 | [diff] [blame] | 4614 | case P_NEG_RS_DREPLY: |
| 4615 | drbd_rs_failed_io(mdev, sector, size); |
| 4616 | case P_RS_CANCEL: |
| 4617 | break; |
| 4618 | default: |
| 4619 | D_ASSERT(0); |
| 4620 | put_ldev(mdev); |
| 4621 | return false; |
| 4622 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4623 | put_ldev(mdev); |
| 4624 | } |
| 4625 | |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 4626 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4627 | } |
| 4628 | |
Andreas Gruenbacher | d876302 | 2011-01-26 17:39:41 +0100 | [diff] [blame] | 4629 | static int got_BarrierAck(struct drbd_conf *mdev, enum drbd_packet cmd) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4630 | { |
Philipp Reisner | 257d0af | 2011-01-26 12:15:29 +0100 | [diff] [blame] | 4631 | struct p_barrier_ack *p = &mdev->tconn->meta.rbuf.barrier_ack; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4632 | |
Philipp Reisner | 2f5cdd0 | 2011-02-21 14:29:27 +0100 | [diff] [blame] | 4633 | tl_release(mdev->tconn, p->barrier, be32_to_cpu(p->set_size)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4634 | |
Philipp Reisner | c4752ef | 2010-10-27 17:32:36 +0200 | [diff] [blame] | 4635 | if (mdev->state.conn == C_AHEAD && |
| 4636 | atomic_read(&mdev->ap_in_flight) == 0 && |
Philipp Reisner | 370a43e | 2011-01-14 16:03:11 +0100 | [diff] [blame] | 4637 | !test_and_set_bit(AHEAD_TO_SYNC_SOURCE, &mdev->current_epoch->flags)) { |
| 4638 | mdev->start_resync_timer.expires = jiffies + HZ; |
| 4639 | add_timer(&mdev->start_resync_timer); |
Philipp Reisner | c4752ef | 2010-10-27 17:32:36 +0200 | [diff] [blame] | 4640 | } |
| 4641 | |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 4642 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4643 | } |
| 4644 | |
Andreas Gruenbacher | d876302 | 2011-01-26 17:39:41 +0100 | [diff] [blame] | 4645 | static int got_OVResult(struct drbd_conf *mdev, enum drbd_packet cmd) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4646 | { |
Philipp Reisner | 257d0af | 2011-01-26 12:15:29 +0100 | [diff] [blame] | 4647 | struct p_block_ack *p = &mdev->tconn->meta.rbuf.block_ack; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4648 | struct drbd_work *w; |
| 4649 | sector_t sector; |
| 4650 | int size; |
| 4651 | |
| 4652 | sector = be64_to_cpu(p->sector); |
| 4653 | size = be32_to_cpu(p->blksize); |
| 4654 | |
| 4655 | update_peer_seq(mdev, be32_to_cpu(p->seq_num)); |
| 4656 | |
| 4657 | if (be64_to_cpu(p->block_id) == ID_OUT_OF_SYNC) |
| 4658 | drbd_ov_oos_found(mdev, sector, size); |
| 4659 | else |
| 4660 | ov_oos_print(mdev); |
| 4661 | |
Lars Ellenberg | 1d53f09 | 2010-09-05 01:13:24 +0200 | [diff] [blame] | 4662 | if (!get_ldev(mdev)) |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 4663 | return true; |
Lars Ellenberg | 1d53f09 | 2010-09-05 01:13:24 +0200 | [diff] [blame] | 4664 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4665 | drbd_rs_complete_io(mdev, sector); |
| 4666 | dec_rs_pending(mdev); |
| 4667 | |
Lars Ellenberg | ea5442a | 2010-11-05 09:48:01 +0100 | [diff] [blame] | 4668 | --mdev->ov_left; |
| 4669 | |
| 4670 | /* let's advance progress step marks only for every other megabyte */ |
| 4671 | if ((mdev->ov_left & 0x200) == 0x200) |
| 4672 | drbd_advance_rs_marks(mdev, mdev->ov_left); |
| 4673 | |
| 4674 | if (mdev->ov_left == 0) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4675 | w = kmalloc(sizeof(*w), GFP_NOIO); |
| 4676 | if (w) { |
| 4677 | w->cb = w_ov_finished; |
Philipp Reisner | a21e929 | 2011-02-08 15:08:49 +0100 | [diff] [blame] | 4678 | w->mdev = mdev; |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 4679 | drbd_queue_work_front(&mdev->tconn->data.work, w); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4680 | } else { |
| 4681 | dev_err(DEV, "kmalloc(w) failed."); |
| 4682 | ov_oos_print(mdev); |
| 4683 | drbd_resync_finished(mdev); |
| 4684 | } |
| 4685 | } |
Lars Ellenberg | 1d53f09 | 2010-09-05 01:13:24 +0200 | [diff] [blame] | 4686 | put_ldev(mdev); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 4687 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4688 | } |
| 4689 | |
Andreas Gruenbacher | d876302 | 2011-01-26 17:39:41 +0100 | [diff] [blame] | 4690 | static int got_skip(struct drbd_conf *mdev, enum drbd_packet cmd) |
Philipp Reisner | 0ced55a | 2010-04-30 15:26:20 +0200 | [diff] [blame] | 4691 | { |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 4692 | return true; |
Philipp Reisner | 0ced55a | 2010-04-30 15:26:20 +0200 | [diff] [blame] | 4693 | } |
| 4694 | |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 4695 | static int tconn_process_done_ee(struct drbd_tconn *tconn) |
| 4696 | { |
Philipp Reisner | 082a343 | 2011-03-15 16:05:42 +0100 | [diff] [blame] | 4697 | struct drbd_conf *mdev; |
| 4698 | int i, not_empty = 0; |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 4699 | |
| 4700 | do { |
| 4701 | clear_bit(SIGNAL_ASENDER, &tconn->flags); |
| 4702 | flush_signals(current); |
Philipp Reisner | 082a343 | 2011-03-15 16:05:42 +0100 | [diff] [blame] | 4703 | idr_for_each_entry(&tconn->volumes, mdev, i) { |
| 4704 | if (!drbd_process_done_ee(mdev)) |
| 4705 | return 1; /* error */ |
| 4706 | } |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 4707 | set_bit(SIGNAL_ASENDER, &tconn->flags); |
Philipp Reisner | 082a343 | 2011-03-15 16:05:42 +0100 | [diff] [blame] | 4708 | |
| 4709 | spin_lock_irq(&tconn->req_lock); |
| 4710 | idr_for_each_entry(&tconn->volumes, mdev, i) { |
| 4711 | not_empty = !list_empty(&mdev->done_ee); |
| 4712 | if (not_empty) |
| 4713 | break; |
| 4714 | } |
| 4715 | spin_unlock_irq(&tconn->req_lock); |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 4716 | } while (not_empty); |
| 4717 | |
| 4718 | return 0; |
| 4719 | } |
| 4720 | |
Andreas Gruenbacher | 7201b97 | 2011-03-14 18:23:00 +0100 | [diff] [blame] | 4721 | struct asender_cmd { |
| 4722 | size_t pkt_size; |
Philipp Reisner | a4fbda8 | 2011-03-16 11:13:17 +0100 | [diff] [blame] | 4723 | enum mdev_or_conn fa_type; /* first argument's type */ |
| 4724 | union { |
| 4725 | int (*mdev_fn)(struct drbd_conf *mdev, enum drbd_packet cmd); |
| 4726 | int (*conn_fn)(struct drbd_tconn *tconn, enum drbd_packet cmd); |
| 4727 | }; |
Andreas Gruenbacher | 7201b97 | 2011-03-14 18:23:00 +0100 | [diff] [blame] | 4728 | }; |
| 4729 | |
| 4730 | static struct asender_cmd asender_tbl[] = { |
Philipp Reisner | f19e4f8 | 2011-03-16 11:21:50 +0100 | [diff] [blame^] | 4731 | [P_PING] = { sizeof(struct p_header), CONN, { .conn_fn = got_Ping } }, |
| 4732 | [P_PING_ACK] = { sizeof(struct p_header), CONN, { .conn_fn = got_PingAck } }, |
Philipp Reisner | a4fbda8 | 2011-03-16 11:13:17 +0100 | [diff] [blame] | 4733 | [P_RECV_ACK] = { sizeof(struct p_block_ack), MDEV, { got_BlockAck } }, |
| 4734 | [P_WRITE_ACK] = { sizeof(struct p_block_ack), MDEV, { got_BlockAck } }, |
| 4735 | [P_RS_WRITE_ACK] = { sizeof(struct p_block_ack), MDEV, { got_BlockAck } }, |
| 4736 | [P_DISCARD_WRITE] = { sizeof(struct p_block_ack), MDEV, { got_BlockAck } }, |
| 4737 | [P_NEG_ACK] = { sizeof(struct p_block_ack), MDEV, { got_NegAck } }, |
| 4738 | [P_NEG_DREPLY] = { sizeof(struct p_block_ack), MDEV, { got_NegDReply } }, |
| 4739 | [P_NEG_RS_DREPLY] = { sizeof(struct p_block_ack), MDEV, { got_NegRSDReply } }, |
| 4740 | [P_OV_RESULT] = { sizeof(struct p_block_ack), MDEV, { got_OVResult } }, |
| 4741 | [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), MDEV, { got_BarrierAck } }, |
| 4742 | [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), MDEV, { got_RqSReply } }, |
| 4743 | [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), MDEV, { got_IsInSync } }, |
| 4744 | [P_DELAY_PROBE] = { sizeof(struct p_delay_probe93), MDEV, { got_skip } }, |
| 4745 | [P_RS_CANCEL] = { sizeof(struct p_block_ack), MDEV, { got_NegRSDReply } }, |
| 4746 | [P_CONN_ST_CHG_REPLY]={ sizeof(struct p_req_state_reply), MDEV, { got_RqSReply } }, |
| 4747 | [P_RETRY_WRITE] = { sizeof(struct p_block_ack), MDEV, { got_BlockAck } }, |
Andreas Gruenbacher | 7201b97 | 2011-03-14 18:23:00 +0100 | [diff] [blame] | 4748 | }; |
| 4749 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4750 | int drbd_asender(struct drbd_thread *thi) |
| 4751 | { |
Philipp Reisner | 392c880 | 2011-02-09 10:33:31 +0100 | [diff] [blame] | 4752 | struct drbd_tconn *tconn = thi->tconn; |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 4753 | struct p_header *h = &tconn->meta.rbuf.header; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4754 | struct asender_cmd *cmd = NULL; |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4755 | struct packet_info pi; |
Philipp Reisner | 257d0af | 2011-01-26 12:15:29 +0100 | [diff] [blame] | 4756 | int rv; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4757 | void *buf = h; |
| 4758 | int received = 0; |
Philipp Reisner | 257d0af | 2011-01-26 12:15:29 +0100 | [diff] [blame] | 4759 | int expect = sizeof(struct p_header); |
Lars Ellenberg | f36af18 | 2011-03-09 22:44:55 +0100 | [diff] [blame] | 4760 | int ping_timeout_active = 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4761 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4762 | current->policy = SCHED_RR; /* Make this a realtime task! */ |
| 4763 | current->rt_priority = 2; /* more important than all other tasks */ |
| 4764 | |
Andreas Gruenbacher | e77a0a5 | 2011-01-25 15:43:39 +0100 | [diff] [blame] | 4765 | while (get_t_state(thi) == RUNNING) { |
Philipp Reisner | 8082228 | 2011-02-08 12:46:30 +0100 | [diff] [blame] | 4766 | drbd_thread_current_set_cpu(thi); |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 4767 | if (test_and_clear_bit(SEND_PING, &tconn->flags)) { |
Philipp Reisner | 2a67d8b | 2011-02-09 14:10:32 +0100 | [diff] [blame] | 4768 | if (!drbd_send_ping(tconn)) { |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 4769 | conn_err(tconn, "drbd_send_ping has failed\n"); |
Andreas Gruenbacher | 841ce24 | 2010-12-15 19:31:20 +0100 | [diff] [blame] | 4770 | goto reconnect; |
| 4771 | } |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 4772 | tconn->meta.socket->sk->sk_rcvtimeo = |
| 4773 | tconn->net_conf->ping_timeo*HZ/10; |
Lars Ellenberg | f36af18 | 2011-03-09 22:44:55 +0100 | [diff] [blame] | 4774 | ping_timeout_active = 1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4775 | } |
| 4776 | |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 4777 | /* TODO: conditionally cork; it may hurt latency if we cork without |
| 4778 | much to send */ |
| 4779 | if (!tconn->net_conf->no_cork) |
| 4780 | drbd_tcp_cork(tconn->meta.socket); |
Philipp Reisner | 082a343 | 2011-03-15 16:05:42 +0100 | [diff] [blame] | 4781 | if (tconn_process_done_ee(tconn)) { |
| 4782 | conn_err(tconn, "tconn_process_done_ee() failed\n"); |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 4783 | goto reconnect; |
Philipp Reisner | 082a343 | 2011-03-15 16:05:42 +0100 | [diff] [blame] | 4784 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4785 | /* but unconditionally uncork unless disabled */ |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 4786 | if (!tconn->net_conf->no_cork) |
| 4787 | drbd_tcp_uncork(tconn->meta.socket); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4788 | |
| 4789 | /* short circuit, recv_msg would return EINTR anyways. */ |
| 4790 | if (signal_pending(current)) |
| 4791 | continue; |
| 4792 | |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 4793 | rv = drbd_recv_short(tconn->meta.socket, buf, expect-received, 0); |
| 4794 | clear_bit(SIGNAL_ASENDER, &tconn->flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4795 | |
| 4796 | flush_signals(current); |
| 4797 | |
| 4798 | /* Note: |
| 4799 | * -EINTR (on meta) we got a signal |
| 4800 | * -EAGAIN (on meta) rcvtimeo expired |
| 4801 | * -ECONNRESET other side closed the connection |
| 4802 | * -ERESTARTSYS (on data) we got a signal |
| 4803 | * rv < 0 other than above: unexpected error! |
| 4804 | * rv == expected: full header or command |
| 4805 | * rv < expected: "woken" by signal during receive |
| 4806 | * rv == 0 : "connection shut down by peer" |
| 4807 | */ |
| 4808 | if (likely(rv > 0)) { |
| 4809 | received += rv; |
| 4810 | buf += rv; |
| 4811 | } else if (rv == 0) { |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 4812 | conn_err(tconn, "meta connection shut down by peer.\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4813 | goto reconnect; |
| 4814 | } else if (rv == -EAGAIN) { |
Lars Ellenberg | cb6518c | 2011-06-20 14:44:45 +0200 | [diff] [blame] | 4815 | /* If the data socket received something meanwhile, |
| 4816 | * that is good enough: peer is still alive. */ |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 4817 | if (time_after(tconn->last_received, |
| 4818 | jiffies - tconn->meta.socket->sk->sk_rcvtimeo)) |
Lars Ellenberg | cb6518c | 2011-06-20 14:44:45 +0200 | [diff] [blame] | 4819 | continue; |
Lars Ellenberg | f36af18 | 2011-03-09 22:44:55 +0100 | [diff] [blame] | 4820 | if (ping_timeout_active) { |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 4821 | conn_err(tconn, "PingAck did not arrive in time.\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4822 | goto reconnect; |
| 4823 | } |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 4824 | set_bit(SEND_PING, &tconn->flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4825 | continue; |
| 4826 | } else if (rv == -EINTR) { |
| 4827 | continue; |
| 4828 | } else { |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 4829 | conn_err(tconn, "sock_recvmsg returned %d\n", rv); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4830 | goto reconnect; |
| 4831 | } |
| 4832 | |
| 4833 | if (received == expect && cmd == NULL) { |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 4834 | if (!decode_header(tconn, h, &pi)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4835 | goto reconnect; |
Andreas Gruenbacher | 7201b97 | 2011-03-14 18:23:00 +0100 | [diff] [blame] | 4836 | cmd = &asender_tbl[pi.cmd]; |
| 4837 | if (pi.cmd >= ARRAY_SIZE(asender_tbl) || !cmd) { |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 4838 | conn_err(tconn, "unknown command %d on meta (l: %d)\n", |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4839 | pi.cmd, pi.size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4840 | goto disconnect; |
| 4841 | } |
| 4842 | expect = cmd->pkt_size; |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4843 | if (pi.size != expect - sizeof(struct p_header)) { |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 4844 | 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] | 4845 | pi.cmd, pi.size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4846 | goto reconnect; |
Philipp Reisner | 257d0af | 2011-01-26 12:15:29 +0100 | [diff] [blame] | 4847 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4848 | } |
| 4849 | if (received == expect) { |
Philipp Reisner | a4fbda8 | 2011-03-16 11:13:17 +0100 | [diff] [blame] | 4850 | bool rv; |
| 4851 | |
| 4852 | if (cmd->fa_type == CONN) { |
| 4853 | rv = cmd->conn_fn(tconn, pi.cmd); |
| 4854 | } else { |
| 4855 | struct drbd_conf *mdev = vnr_to_mdev(tconn, pi.vnr); |
| 4856 | rv = cmd->mdev_fn(mdev, pi.cmd); |
| 4857 | } |
| 4858 | |
| 4859 | if (!rv) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4860 | goto reconnect; |
| 4861 | |
Philipp Reisner | a4fbda8 | 2011-03-16 11:13:17 +0100 | [diff] [blame] | 4862 | tconn->last_received = jiffies; |
| 4863 | |
Lars Ellenberg | f36af18 | 2011-03-09 22:44:55 +0100 | [diff] [blame] | 4864 | /* the idle_timeout (ping-int) |
| 4865 | * has been restored in got_PingAck() */ |
Andreas Gruenbacher | 7201b97 | 2011-03-14 18:23:00 +0100 | [diff] [blame] | 4866 | if (cmd == &asender_tbl[P_PING_ACK]) |
Lars Ellenberg | f36af18 | 2011-03-09 22:44:55 +0100 | [diff] [blame] | 4867 | ping_timeout_active = 0; |
| 4868 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4869 | buf = h; |
| 4870 | received = 0; |
Philipp Reisner | 257d0af | 2011-01-26 12:15:29 +0100 | [diff] [blame] | 4871 | expect = sizeof(struct p_header); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4872 | cmd = NULL; |
| 4873 | } |
| 4874 | } |
| 4875 | |
| 4876 | if (0) { |
| 4877 | reconnect: |
Philipp Reisner | bbeb641 | 2011-02-10 13:45:46 +0100 | [diff] [blame] | 4878 | conn_request_state(tconn, NS(conn, C_NETWORK_FAILURE), CS_HARD); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4879 | } |
| 4880 | if (0) { |
| 4881 | disconnect: |
Philipp Reisner | bbeb641 | 2011-02-10 13:45:46 +0100 | [diff] [blame] | 4882 | conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4883 | } |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 4884 | clear_bit(SIGNAL_ASENDER, &tconn->flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4885 | |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 4886 | conn_info(tconn, "asender terminated\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4887 | |
| 4888 | return 0; |
| 4889 | } |