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; |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 53 | unsigned int size; |
| 54 | unsigned int vnr; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 55 | void *data; |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 56 | }; |
| 57 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 58 | enum finish_epoch { |
| 59 | FE_STILL_LIVE, |
| 60 | FE_DESTROYED, |
| 61 | FE_RECYCLED, |
| 62 | }; |
| 63 | |
Andreas Gruenbacher | 6038178 | 2011-03-28 17:05:50 +0200 | [diff] [blame] | 64 | static int drbd_do_features(struct drbd_tconn *tconn); |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 65 | static int drbd_do_auth(struct drbd_tconn *tconn); |
Philipp Reisner | c141ebd | 2011-05-05 16:13:10 +0200 | [diff] [blame] | 66 | static int drbd_disconnected(struct drbd_conf *mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 67 | |
Philipp Reisner | 1e9dd29 | 2011-11-10 15:14:53 +0100 | [diff] [blame] | 68 | static enum finish_epoch drbd_may_finish_epoch(struct drbd_tconn *, struct drbd_epoch *, enum epoch_event); |
Andreas Gruenbacher | 99920dc | 2011-03-16 15:31:39 +0100 | [diff] [blame] | 69 | static int e_end_block(struct drbd_work *, int); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 70 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 71 | |
| 72 | #define GFP_TRY (__GFP_HIGHMEM | __GFP_NOWARN) |
| 73 | |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 74 | /* |
| 75 | * some helper functions to deal with single linked page lists, |
| 76 | * page->private being our "next" pointer. |
| 77 | */ |
| 78 | |
| 79 | /* If at least n pages are linked at head, get n pages off. |
| 80 | * Otherwise, don't modify head, and return NULL. |
| 81 | * Locking is the responsibility of the caller. |
| 82 | */ |
| 83 | static struct page *page_chain_del(struct page **head, int n) |
| 84 | { |
| 85 | struct page *page; |
| 86 | struct page *tmp; |
| 87 | |
| 88 | BUG_ON(!n); |
| 89 | BUG_ON(!head); |
| 90 | |
| 91 | page = *head; |
Philipp Reisner | 23ce422 | 2010-05-20 13:35:31 +0200 | [diff] [blame] | 92 | |
| 93 | if (!page) |
| 94 | return NULL; |
| 95 | |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 96 | while (page) { |
| 97 | tmp = page_chain_next(page); |
| 98 | if (--n == 0) |
| 99 | break; /* found sufficient pages */ |
| 100 | if (tmp == NULL) |
| 101 | /* insufficient pages, don't use any of them. */ |
| 102 | return NULL; |
| 103 | page = tmp; |
| 104 | } |
| 105 | |
| 106 | /* add end of list marker for the returned list */ |
| 107 | set_page_private(page, 0); |
| 108 | /* actual return value, and adjustment of head */ |
| 109 | page = *head; |
| 110 | *head = tmp; |
| 111 | return page; |
| 112 | } |
| 113 | |
| 114 | /* may be used outside of locks to find the tail of a (usually short) |
| 115 | * "private" page chain, before adding it back to a global chain head |
| 116 | * with page_chain_add() under a spinlock. */ |
| 117 | static struct page *page_chain_tail(struct page *page, int *len) |
| 118 | { |
| 119 | struct page *tmp; |
| 120 | int i = 1; |
| 121 | while ((tmp = page_chain_next(page))) |
| 122 | ++i, page = tmp; |
| 123 | if (len) |
| 124 | *len = i; |
| 125 | return page; |
| 126 | } |
| 127 | |
| 128 | static int page_chain_free(struct page *page) |
| 129 | { |
| 130 | struct page *tmp; |
| 131 | int i = 0; |
| 132 | page_chain_for_each_safe(page, tmp) { |
| 133 | put_page(page); |
| 134 | ++i; |
| 135 | } |
| 136 | return i; |
| 137 | } |
| 138 | |
| 139 | static void page_chain_add(struct page **head, |
| 140 | struct page *chain_first, struct page *chain_last) |
| 141 | { |
| 142 | #if 1 |
| 143 | struct page *tmp; |
| 144 | tmp = page_chain_tail(chain_first, NULL); |
| 145 | BUG_ON(tmp != chain_last); |
| 146 | #endif |
| 147 | |
| 148 | /* add chain to head */ |
| 149 | set_page_private(chain_last, (unsigned long)*head); |
| 150 | *head = chain_first; |
| 151 | } |
| 152 | |
Andreas Gruenbacher | 18c2d52 | 2011-04-07 21:08:50 +0200 | [diff] [blame] | 153 | static struct page *__drbd_alloc_pages(struct drbd_conf *mdev, |
| 154 | unsigned int number) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 155 | { |
| 156 | struct page *page = NULL; |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 157 | struct page *tmp = NULL; |
Andreas Gruenbacher | 18c2d52 | 2011-04-07 21:08:50 +0200 | [diff] [blame] | 158 | unsigned int i = 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 159 | |
| 160 | /* Yes, testing drbd_pp_vacant outside the lock is racy. |
| 161 | * So what. It saves a spin_lock. */ |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 162 | if (drbd_pp_vacant >= number) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 163 | spin_lock(&drbd_pp_lock); |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 164 | page = page_chain_del(&drbd_pp_pool, number); |
| 165 | if (page) |
| 166 | drbd_pp_vacant -= number; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 167 | spin_unlock(&drbd_pp_lock); |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 168 | if (page) |
| 169 | return page; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 170 | } |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 171 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 172 | /* GFP_TRY, because we must not cause arbitrary write-out: in a DRBD |
| 173 | * "criss-cross" setup, that might cause write-out on some other DRBD, |
| 174 | * which in turn might block on the other node at this very place. */ |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 175 | for (i = 0; i < number; i++) { |
| 176 | tmp = alloc_page(GFP_TRY); |
| 177 | if (!tmp) |
| 178 | break; |
| 179 | set_page_private(tmp, (unsigned long)page); |
| 180 | page = tmp; |
| 181 | } |
| 182 | |
| 183 | if (i == number) |
| 184 | return page; |
| 185 | |
| 186 | /* Not enough pages immediately available this time. |
Andreas Gruenbacher | c37c8ec | 2011-04-07 21:02:09 +0200 | [diff] [blame] | 187 | * No need to jump around here, drbd_alloc_pages will retry this |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 188 | * function "soon". */ |
| 189 | if (page) { |
| 190 | tmp = page_chain_tail(page, NULL); |
| 191 | spin_lock(&drbd_pp_lock); |
| 192 | page_chain_add(&drbd_pp_pool, page, tmp); |
| 193 | drbd_pp_vacant += i; |
| 194 | spin_unlock(&drbd_pp_lock); |
| 195 | } |
| 196 | return NULL; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Andreas Gruenbacher | a990be4 | 2011-04-06 17:56:48 +0200 | [diff] [blame] | 199 | static void reclaim_finished_net_peer_reqs(struct drbd_conf *mdev, |
| 200 | struct list_head *to_be_freed) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 201 | { |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 202 | struct drbd_peer_request *peer_req; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 203 | struct list_head *le, *tle; |
| 204 | |
| 205 | /* The EEs are always appended to the end of the list. Since |
| 206 | they are sent in order over the wire, they have to finish |
| 207 | in order. As soon as we see the first not finished we can |
| 208 | stop to examine the list... */ |
| 209 | |
| 210 | list_for_each_safe(le, tle, &mdev->net_ee) { |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 211 | peer_req = list_entry(le, struct drbd_peer_request, w.list); |
Andreas Gruenbacher | 045417f | 2011-04-07 21:34:24 +0200 | [diff] [blame] | 212 | if (drbd_peer_req_has_active_page(peer_req)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 213 | break; |
| 214 | list_move(le, to_be_freed); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | static void drbd_kick_lo_and_reclaim_net(struct drbd_conf *mdev) |
| 219 | { |
| 220 | LIST_HEAD(reclaimed); |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 221 | struct drbd_peer_request *peer_req, *t; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 222 | |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 223 | spin_lock_irq(&mdev->tconn->req_lock); |
Andreas Gruenbacher | a990be4 | 2011-04-06 17:56:48 +0200 | [diff] [blame] | 224 | reclaim_finished_net_peer_reqs(mdev, &reclaimed); |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 225 | spin_unlock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 226 | |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 227 | list_for_each_entry_safe(peer_req, t, &reclaimed, w.list) |
Andreas Gruenbacher | 3967deb | 2011-04-06 16:16:56 +0200 | [diff] [blame] | 228 | drbd_free_net_peer_req(mdev, peer_req); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /** |
Andreas Gruenbacher | c37c8ec | 2011-04-07 21:02:09 +0200 | [diff] [blame] | 232 | * drbd_alloc_pages() - Returns @number pages, retries forever (or until signalled) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 233 | * @mdev: DRBD device. |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 234 | * @number: number of pages requested |
| 235 | * @retry: whether to retry, if not enough pages are available right now |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 236 | * |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 237 | * Tries to allocate number pages, first from our own page pool, then from |
| 238 | * the kernel, unless this allocation would exceed the max_buffers setting. |
| 239 | * Possibly retry until DRBD frees sufficient pages somewhere else. |
| 240 | * |
| 241 | * Returns a page chain linked via page->private. |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 242 | */ |
Andreas Gruenbacher | c37c8ec | 2011-04-07 21:02:09 +0200 | [diff] [blame] | 243 | struct page *drbd_alloc_pages(struct drbd_conf *mdev, unsigned int number, |
| 244 | bool retry) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 245 | { |
| 246 | struct page *page = NULL; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 247 | struct net_conf *nc; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 248 | DEFINE_WAIT(wait); |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 249 | int mxb; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 250 | |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 251 | /* Yes, we may run up to @number over max_buffers. If we |
| 252 | * follow it strictly, the admin will get it wrong anyways. */ |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 253 | rcu_read_lock(); |
| 254 | nc = rcu_dereference(mdev->tconn->net_conf); |
| 255 | mxb = nc ? nc->max_buffers : 1000000; |
| 256 | rcu_read_unlock(); |
| 257 | |
| 258 | if (atomic_read(&mdev->pp_in_use) < mxb) |
Andreas Gruenbacher | 18c2d52 | 2011-04-07 21:08:50 +0200 | [diff] [blame] | 259 | page = __drbd_alloc_pages(mdev, number); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 260 | |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 261 | while (page == NULL) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 262 | prepare_to_wait(&drbd_pp_wait, &wait, TASK_INTERRUPTIBLE); |
| 263 | |
| 264 | drbd_kick_lo_and_reclaim_net(mdev); |
| 265 | |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 266 | if (atomic_read(&mdev->pp_in_use) < mxb) { |
Andreas Gruenbacher | 18c2d52 | 2011-04-07 21:08:50 +0200 | [diff] [blame] | 267 | page = __drbd_alloc_pages(mdev, number); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 268 | if (page) |
| 269 | break; |
| 270 | } |
| 271 | |
| 272 | if (!retry) |
| 273 | break; |
| 274 | |
| 275 | if (signal_pending(current)) { |
Andreas Gruenbacher | c37c8ec | 2011-04-07 21:02:09 +0200 | [diff] [blame] | 276 | dev_warn(DEV, "drbd_alloc_pages interrupted!\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 277 | break; |
| 278 | } |
| 279 | |
| 280 | schedule(); |
| 281 | } |
| 282 | finish_wait(&drbd_pp_wait, &wait); |
| 283 | |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 284 | if (page) |
| 285 | atomic_add(number, &mdev->pp_in_use); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 286 | return page; |
| 287 | } |
| 288 | |
Andreas Gruenbacher | c37c8ec | 2011-04-07 21:02:09 +0200 | [diff] [blame] | 289 | /* Must not be used from irq, as that may deadlock: see drbd_alloc_pages. |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 290 | * 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] | 291 | * Either links the page chain back to the global pool, |
| 292 | * or returns all pages to the system. */ |
Andreas Gruenbacher | 5cc287e | 2011-04-07 21:02:59 +0200 | [diff] [blame] | 293 | static void drbd_free_pages(struct drbd_conf *mdev, struct page *page, int is_net) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 294 | { |
Lars Ellenberg | 435f074 | 2010-09-06 12:30:25 +0200 | [diff] [blame] | 295 | 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] | 296 | int i; |
Lars Ellenberg | 435f074 | 2010-09-06 12:30:25 +0200 | [diff] [blame] | 297 | |
Philipp Reisner | 81a5d60 | 2011-02-22 19:53:16 -0500 | [diff] [blame] | 298 | if (drbd_pp_vacant > (DRBD_MAX_BIO_SIZE/PAGE_SIZE) * minor_count) |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 299 | i = page_chain_free(page); |
| 300 | else { |
| 301 | struct page *tmp; |
| 302 | tmp = page_chain_tail(page, &i); |
| 303 | spin_lock(&drbd_pp_lock); |
| 304 | page_chain_add(&drbd_pp_pool, page, tmp); |
| 305 | drbd_pp_vacant += i; |
| 306 | spin_unlock(&drbd_pp_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 307 | } |
Lars Ellenberg | 435f074 | 2010-09-06 12:30:25 +0200 | [diff] [blame] | 308 | i = atomic_sub_return(i, a); |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 309 | if (i < 0) |
Lars Ellenberg | 435f074 | 2010-09-06 12:30:25 +0200 | [diff] [blame] | 310 | dev_warn(DEV, "ASSERTION FAILED: %s: %d < 0\n", |
| 311 | is_net ? "pp_in_use_by_net" : "pp_in_use", i); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 312 | wake_up(&drbd_pp_wait); |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | You need to hold the req_lock: |
| 317 | _drbd_wait_ee_list_empty() |
| 318 | |
| 319 | You must not have the req_lock: |
Andreas Gruenbacher | 3967deb | 2011-04-06 16:16:56 +0200 | [diff] [blame] | 320 | drbd_free_peer_req() |
Andreas Gruenbacher | 0db5536 | 2011-04-06 16:09:15 +0200 | [diff] [blame] | 321 | drbd_alloc_peer_req() |
Andreas Gruenbacher | 7721f56 | 2011-04-06 17:14:02 +0200 | [diff] [blame] | 322 | drbd_free_peer_reqs() |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 323 | drbd_ee_fix_bhs() |
Andreas Gruenbacher | a990be4 | 2011-04-06 17:56:48 +0200 | [diff] [blame] | 324 | drbd_finish_peer_reqs() |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 325 | drbd_clear_done_ee() |
| 326 | drbd_wait_ee_list_empty() |
| 327 | */ |
| 328 | |
Andreas Gruenbacher | f6ffca9 | 2011-02-04 15:30:34 +0100 | [diff] [blame] | 329 | struct drbd_peer_request * |
Andreas Gruenbacher | 0db5536 | 2011-04-06 16:09:15 +0200 | [diff] [blame] | 330 | drbd_alloc_peer_req(struct drbd_conf *mdev, u64 id, sector_t sector, |
| 331 | unsigned int data_size, gfp_t gfp_mask) __must_hold(local) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 332 | { |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 333 | struct drbd_peer_request *peer_req; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 334 | struct page *page; |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 335 | unsigned nr_pages = (data_size + PAGE_SIZE -1) >> PAGE_SHIFT; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 336 | |
Andreas Gruenbacher | 0cf9d27 | 2010-12-07 10:43:29 +0100 | [diff] [blame] | 337 | if (drbd_insert_fault(mdev, DRBD_FAULT_AL_EE)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 338 | return NULL; |
| 339 | |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 340 | peer_req = mempool_alloc(drbd_ee_mempool, gfp_mask & ~__GFP_HIGHMEM); |
| 341 | if (!peer_req) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 342 | if (!(gfp_mask & __GFP_NOWARN)) |
Andreas Gruenbacher | 0db5536 | 2011-04-06 16:09:15 +0200 | [diff] [blame] | 343 | dev_err(DEV, "%s: allocation failed\n", __func__); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 344 | return NULL; |
| 345 | } |
| 346 | |
Andreas Gruenbacher | c37c8ec | 2011-04-07 21:02:09 +0200 | [diff] [blame] | 347 | page = drbd_alloc_pages(mdev, nr_pages, (gfp_mask & __GFP_WAIT)); |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 348 | if (!page) |
| 349 | goto fail; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 350 | |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 351 | drbd_clear_interval(&peer_req->i); |
| 352 | peer_req->i.size = data_size; |
| 353 | peer_req->i.sector = sector; |
| 354 | peer_req->i.local = false; |
| 355 | peer_req->i.waiting = false; |
Andreas Gruenbacher | 5384064 | 2011-01-28 10:31:04 +0100 | [diff] [blame] | 356 | |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 357 | peer_req->epoch = NULL; |
Philipp Reisner | a21e929 | 2011-02-08 15:08:49 +0100 | [diff] [blame] | 358 | peer_req->w.mdev = mdev; |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 359 | peer_req->pages = page; |
| 360 | atomic_set(&peer_req->pending_bios, 0); |
| 361 | peer_req->flags = 0; |
Andreas Gruenbacher | 9a8e775 | 2011-01-11 14:04:09 +0100 | [diff] [blame] | 362 | /* |
| 363 | * The block_id is opaque to the receiver. It is not endianness |
| 364 | * converted, and sent back to the sender unchanged. |
| 365 | */ |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 366 | peer_req->block_id = id; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 367 | |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 368 | return peer_req; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 369 | |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 370 | fail: |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 371 | mempool_free(peer_req, drbd_ee_mempool); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 372 | return NULL; |
| 373 | } |
| 374 | |
Andreas Gruenbacher | 3967deb | 2011-04-06 16:16:56 +0200 | [diff] [blame] | 375 | void __drbd_free_peer_req(struct drbd_conf *mdev, struct drbd_peer_request *peer_req, |
Andreas Gruenbacher | f6ffca9 | 2011-02-04 15:30:34 +0100 | [diff] [blame] | 376 | int is_net) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 377 | { |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 378 | if (peer_req->flags & EE_HAS_DIGEST) |
| 379 | kfree(peer_req->digest); |
Andreas Gruenbacher | 5cc287e | 2011-04-07 21:02:59 +0200 | [diff] [blame] | 380 | drbd_free_pages(mdev, peer_req->pages, is_net); |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 381 | D_ASSERT(atomic_read(&peer_req->pending_bios) == 0); |
| 382 | D_ASSERT(drbd_interval_empty(&peer_req->i)); |
| 383 | mempool_free(peer_req, drbd_ee_mempool); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Andreas Gruenbacher | 7721f56 | 2011-04-06 17:14:02 +0200 | [diff] [blame] | 386 | int drbd_free_peer_reqs(struct drbd_conf *mdev, struct list_head *list) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 387 | { |
| 388 | LIST_HEAD(work_list); |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 389 | struct drbd_peer_request *peer_req, *t; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 390 | int count = 0; |
Lars Ellenberg | 435f074 | 2010-09-06 12:30:25 +0200 | [diff] [blame] | 391 | int is_net = list == &mdev->net_ee; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 392 | |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 393 | spin_lock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 394 | list_splice_init(list, &work_list); |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 395 | spin_unlock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 396 | |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 397 | list_for_each_entry_safe(peer_req, t, &work_list, w.list) { |
Andreas Gruenbacher | 3967deb | 2011-04-06 16:16:56 +0200 | [diff] [blame] | 398 | __drbd_free_peer_req(mdev, peer_req, is_net); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 399 | count++; |
| 400 | } |
| 401 | return count; |
| 402 | } |
| 403 | |
Andreas Gruenbacher | a990be4 | 2011-04-06 17:56:48 +0200 | [diff] [blame] | 404 | /* |
| 405 | * See also comments in _req_mod(,BARRIER_ACKED) and receive_Barrier. |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 406 | */ |
Andreas Gruenbacher | a990be4 | 2011-04-06 17:56:48 +0200 | [diff] [blame] | 407 | static int drbd_finish_peer_reqs(struct drbd_conf *mdev) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 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; |
Andreas Gruenbacher | e2b3032 | 2011-03-16 17:16:12 +0100 | [diff] [blame] | 412 | int err = 0; |
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); |
Andreas Gruenbacher | a990be4 | 2011-04-06 17:56:48 +0200 | [diff] [blame] | 415 | reclaim_finished_net_peer_reqs(mdev, &reclaimed); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 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) |
Andreas Gruenbacher | 3967deb | 2011-04-06 16:16:56 +0200 | [diff] [blame] | 420 | drbd_free_net_peer_req(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) { |
Andreas Gruenbacher | e2b3032 | 2011-03-16 17:16:12 +0100 | [diff] [blame] | 427 | int err2; |
| 428 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 429 | /* list_del not necessary, next/prev members not touched */ |
Andreas Gruenbacher | e2b3032 | 2011-03-16 17:16:12 +0100 | [diff] [blame] | 430 | err2 = peer_req->w.cb(&peer_req->w, !!err); |
| 431 | if (!err) |
| 432 | err = err2; |
Andreas Gruenbacher | 3967deb | 2011-04-06 16:16:56 +0200 | [diff] [blame] | 433 | drbd_free_peer_req(mdev, peer_req); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 434 | } |
| 435 | wake_up(&mdev->ee_wait); |
| 436 | |
Andreas Gruenbacher | e2b3032 | 2011-03-16 17:16:12 +0100 | [diff] [blame] | 437 | return err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Andreas Gruenbacher | d4da153 | 2011-04-07 00:06:56 +0200 | [diff] [blame] | 440 | static void _drbd_wait_ee_list_empty(struct drbd_conf *mdev, |
| 441 | struct list_head *head) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 442 | { |
| 443 | DEFINE_WAIT(wait); |
| 444 | |
| 445 | /* avoids spin_lock/unlock |
| 446 | * and calling prepare_to_wait in the fast path */ |
| 447 | while (!list_empty(head)) { |
| 448 | prepare_to_wait(&mdev->ee_wait, &wait, TASK_UNINTERRUPTIBLE); |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 449 | spin_unlock_irq(&mdev->tconn->req_lock); |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 450 | io_schedule(); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 451 | finish_wait(&mdev->ee_wait, &wait); |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 452 | spin_lock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | |
Andreas Gruenbacher | d4da153 | 2011-04-07 00:06:56 +0200 | [diff] [blame] | 456 | static void drbd_wait_ee_list_empty(struct drbd_conf *mdev, |
| 457 | struct list_head *head) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 458 | { |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 459 | spin_lock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 460 | _drbd_wait_ee_list_empty(mdev, head); |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 461 | spin_unlock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | /* see also kernel_accept; which is only present since 2.6.18. |
| 465 | * also we want to log which part of it failed, exactly */ |
Philipp Reisner | 7653620 | 2011-02-07 14:09:54 +0100 | [diff] [blame] | 466 | 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] | 467 | { |
| 468 | struct sock *sk = sock->sk; |
| 469 | int err = 0; |
| 470 | |
| 471 | *what = "listen"; |
| 472 | err = sock->ops->listen(sock, 5); |
| 473 | if (err < 0) |
| 474 | goto out; |
| 475 | |
| 476 | *what = "sock_create_lite"; |
| 477 | err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol, |
| 478 | newsock); |
| 479 | if (err < 0) |
| 480 | goto out; |
| 481 | |
| 482 | *what = "accept"; |
| 483 | err = sock->ops->accept(sock, *newsock, 0); |
| 484 | if (err < 0) { |
| 485 | sock_release(*newsock); |
| 486 | *newsock = NULL; |
| 487 | goto out; |
| 488 | } |
| 489 | (*newsock)->ops = sock->ops; |
Philipp Reisner | dd9b360 | 2012-02-23 12:52:31 +0100 | [diff] [blame] | 490 | __module_get((*newsock)->ops->owner); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 491 | |
| 492 | out: |
| 493 | return err; |
| 494 | } |
| 495 | |
Philipp Reisner | dbd9eea | 2011-02-07 15:34:16 +0100 | [diff] [blame] | 496 | 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] | 497 | { |
| 498 | mm_segment_t oldfs; |
| 499 | struct kvec iov = { |
| 500 | .iov_base = buf, |
| 501 | .iov_len = size, |
| 502 | }; |
| 503 | struct msghdr msg = { |
| 504 | .msg_iovlen = 1, |
| 505 | .msg_iov = (struct iovec *)&iov, |
| 506 | .msg_flags = (flags ? flags : MSG_WAITALL | MSG_NOSIGNAL) |
| 507 | }; |
| 508 | int rv; |
| 509 | |
| 510 | oldfs = get_fs(); |
| 511 | set_fs(KERNEL_DS); |
| 512 | rv = sock_recvmsg(sock, &msg, size, msg.msg_flags); |
| 513 | set_fs(oldfs); |
| 514 | |
| 515 | return rv; |
| 516 | } |
| 517 | |
Philipp Reisner | de0ff33 | 2011-02-07 16:56:20 +0100 | [diff] [blame] | 518 | 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] | 519 | { |
| 520 | mm_segment_t oldfs; |
| 521 | struct kvec iov = { |
| 522 | .iov_base = buf, |
| 523 | .iov_len = size, |
| 524 | }; |
| 525 | struct msghdr msg = { |
| 526 | .msg_iovlen = 1, |
| 527 | .msg_iov = (struct iovec *)&iov, |
| 528 | .msg_flags = MSG_WAITALL | MSG_NOSIGNAL |
| 529 | }; |
| 530 | int rv; |
| 531 | |
| 532 | oldfs = get_fs(); |
| 533 | set_fs(KERNEL_DS); |
| 534 | |
| 535 | for (;;) { |
Philipp Reisner | de0ff33 | 2011-02-07 16:56:20 +0100 | [diff] [blame] | 536 | rv = sock_recvmsg(tconn->data.socket, &msg, size, msg.msg_flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 537 | if (rv == size) |
| 538 | break; |
| 539 | |
| 540 | /* Note: |
| 541 | * ECONNRESET other side closed the connection |
| 542 | * ERESTARTSYS (on sock) we got a signal |
| 543 | */ |
| 544 | |
| 545 | if (rv < 0) { |
| 546 | if (rv == -ECONNRESET) |
Philipp Reisner | de0ff33 | 2011-02-07 16:56:20 +0100 | [diff] [blame] | 547 | conn_info(tconn, "sock was reset by peer\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 548 | else if (rv != -ERESTARTSYS) |
Philipp Reisner | de0ff33 | 2011-02-07 16:56:20 +0100 | [diff] [blame] | 549 | conn_err(tconn, "sock_recvmsg returned %d\n", rv); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 550 | break; |
| 551 | } else if (rv == 0) { |
Philipp Reisner | de0ff33 | 2011-02-07 16:56:20 +0100 | [diff] [blame] | 552 | conn_info(tconn, "sock was shut down by peer\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 553 | break; |
| 554 | } else { |
| 555 | /* signal came in, or peer/link went down, |
| 556 | * after we read a partial message |
| 557 | */ |
| 558 | /* D_ASSERT(signal_pending(current)); */ |
| 559 | break; |
| 560 | } |
| 561 | }; |
| 562 | |
| 563 | set_fs(oldfs); |
| 564 | |
| 565 | if (rv != size) |
Philipp Reisner | bbeb641 | 2011-02-10 13:45:46 +0100 | [diff] [blame] | 566 | conn_request_state(tconn, NS(conn, C_BROKEN_PIPE), CS_HARD); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 567 | |
| 568 | return rv; |
| 569 | } |
| 570 | |
Andreas Gruenbacher | c696774 | 2011-03-17 17:15:20 +0100 | [diff] [blame] | 571 | static int drbd_recv_all(struct drbd_tconn *tconn, void *buf, size_t size) |
| 572 | { |
| 573 | int err; |
| 574 | |
| 575 | err = drbd_recv(tconn, buf, size); |
| 576 | if (err != size) { |
| 577 | if (err >= 0) |
| 578 | err = -EIO; |
| 579 | } else |
| 580 | err = 0; |
| 581 | return err; |
| 582 | } |
| 583 | |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 584 | static int drbd_recv_all_warn(struct drbd_tconn *tconn, void *buf, size_t size) |
| 585 | { |
| 586 | int err; |
| 587 | |
| 588 | err = drbd_recv_all(tconn, buf, size); |
| 589 | if (err && !signal_pending(current)) |
| 590 | conn_warn(tconn, "short read (expected size %d)\n", (int)size); |
| 591 | return err; |
| 592 | } |
| 593 | |
Lars Ellenberg | 5dbf167 | 2010-05-25 16:18:01 +0200 | [diff] [blame] | 594 | /* quoting tcp(7): |
| 595 | * On individual connections, the socket buffer size must be set prior to the |
| 596 | * listen(2) or connect(2) calls in order to have it take effect. |
| 597 | * This is our wrapper to do so. |
| 598 | */ |
| 599 | static void drbd_setbufsize(struct socket *sock, unsigned int snd, |
| 600 | unsigned int rcv) |
| 601 | { |
| 602 | /* open coded SO_SNDBUF, SO_RCVBUF */ |
| 603 | if (snd) { |
| 604 | sock->sk->sk_sndbuf = snd; |
| 605 | sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK; |
| 606 | } |
| 607 | if (rcv) { |
| 608 | sock->sk->sk_rcvbuf = rcv; |
| 609 | sock->sk->sk_userlocks |= SOCK_RCVBUF_LOCK; |
| 610 | } |
| 611 | } |
| 612 | |
Philipp Reisner | eac3e99 | 2011-02-07 14:05:07 +0100 | [diff] [blame] | 613 | static struct socket *drbd_try_connect(struct drbd_tconn *tconn) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 614 | { |
| 615 | const char *what; |
| 616 | struct socket *sock; |
| 617 | struct sockaddr_in6 src_in6; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 618 | struct sockaddr_in6 peer_in6; |
| 619 | struct net_conf *nc; |
| 620 | int err, peer_addr_len, my_addr_len; |
Andreas Gruenbacher | 69ef82d | 2011-05-11 14:34:35 +0200 | [diff] [blame] | 621 | int sndbuf_size, rcvbuf_size, connect_int; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 622 | int disconnect_on_error = 1; |
| 623 | |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 624 | rcu_read_lock(); |
| 625 | nc = rcu_dereference(tconn->net_conf); |
| 626 | if (!nc) { |
| 627 | rcu_read_unlock(); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 628 | return NULL; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 629 | } |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 630 | sndbuf_size = nc->sndbuf_size; |
| 631 | rcvbuf_size = nc->rcvbuf_size; |
Andreas Gruenbacher | 69ef82d | 2011-05-11 14:34:35 +0200 | [diff] [blame] | 632 | connect_int = nc->connect_int; |
Andreas Gruenbacher | 089c075 | 2011-06-14 18:28:09 +0200 | [diff] [blame] | 633 | rcu_read_unlock(); |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 634 | |
Andreas Gruenbacher | 089c075 | 2011-06-14 18:28:09 +0200 | [diff] [blame] | 635 | my_addr_len = min_t(int, tconn->my_addr_len, sizeof(src_in6)); |
| 636 | memcpy(&src_in6, &tconn->my_addr, my_addr_len); |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 637 | |
Andreas Gruenbacher | 089c075 | 2011-06-14 18:28:09 +0200 | [diff] [blame] | 638 | if (((struct sockaddr *)&tconn->my_addr)->sa_family == AF_INET6) |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 639 | src_in6.sin6_port = 0; |
| 640 | else |
| 641 | ((struct sockaddr_in *)&src_in6)->sin_port = 0; /* AF_INET & AF_SCI */ |
| 642 | |
Andreas Gruenbacher | 089c075 | 2011-06-14 18:28:09 +0200 | [diff] [blame] | 643 | peer_addr_len = min_t(int, tconn->peer_addr_len, sizeof(src_in6)); |
| 644 | memcpy(&peer_in6, &tconn->peer_addr, peer_addr_len); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 645 | |
| 646 | what = "sock_create_kern"; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 647 | err = sock_create_kern(((struct sockaddr *)&src_in6)->sa_family, |
| 648 | SOCK_STREAM, IPPROTO_TCP, &sock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 649 | if (err < 0) { |
| 650 | sock = NULL; |
| 651 | goto out; |
| 652 | } |
| 653 | |
| 654 | sock->sk->sk_rcvtimeo = |
Andreas Gruenbacher | 69ef82d | 2011-05-11 14:34:35 +0200 | [diff] [blame] | 655 | sock->sk->sk_sndtimeo = connect_int * HZ; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 656 | drbd_setbufsize(sock, sndbuf_size, rcvbuf_size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 657 | |
| 658 | /* explicitly bind to the configured IP as source IP |
| 659 | * for the outgoing connections. |
| 660 | * This is needed for multihomed hosts and to be |
| 661 | * able to use lo: interfaces for drbd. |
| 662 | * Make sure to use 0 as port number, so linux selects |
| 663 | * a free one dynamically. |
| 664 | */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 665 | what = "bind before connect"; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 666 | err = sock->ops->bind(sock, (struct sockaddr *) &src_in6, my_addr_len); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 667 | if (err < 0) |
| 668 | goto out; |
| 669 | |
| 670 | /* connect may fail, peer not yet available. |
| 671 | * stay C_WF_CONNECTION, don't go Disconnecting! */ |
| 672 | disconnect_on_error = 0; |
| 673 | what = "connect"; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 674 | err = sock->ops->connect(sock, (struct sockaddr *) &peer_in6, peer_addr_len, 0); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 675 | |
| 676 | out: |
| 677 | if (err < 0) { |
| 678 | if (sock) { |
| 679 | sock_release(sock); |
| 680 | sock = NULL; |
| 681 | } |
| 682 | switch (-err) { |
| 683 | /* timeout, busy, signal pending */ |
| 684 | case ETIMEDOUT: case EAGAIN: case EINPROGRESS: |
| 685 | case EINTR: case ERESTARTSYS: |
| 686 | /* peer not (yet) available, network problem */ |
| 687 | case ECONNREFUSED: case ENETUNREACH: |
| 688 | case EHOSTDOWN: case EHOSTUNREACH: |
| 689 | disconnect_on_error = 0; |
| 690 | break; |
| 691 | default: |
Philipp Reisner | eac3e99 | 2011-02-07 14:05:07 +0100 | [diff] [blame] | 692 | conn_err(tconn, "%s failed, err = %d\n", what, err); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 693 | } |
| 694 | if (disconnect_on_error) |
Philipp Reisner | bbeb641 | 2011-02-10 13:45:46 +0100 | [diff] [blame] | 695 | conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 696 | } |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 697 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 698 | return sock; |
| 699 | } |
| 700 | |
Philipp Reisner | 7653620 | 2011-02-07 14:09:54 +0100 | [diff] [blame] | 701 | static struct socket *drbd_wait_for_connect(struct drbd_tconn *tconn) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 702 | { |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 703 | int timeo, err, my_addr_len; |
Andreas Gruenbacher | 69ef82d | 2011-05-11 14:34:35 +0200 | [diff] [blame] | 704 | int sndbuf_size, rcvbuf_size, connect_int; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 705 | struct socket *s_estab = NULL, *s_listen; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 706 | struct sockaddr_in6 my_addr; |
| 707 | struct net_conf *nc; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 708 | const char *what; |
| 709 | |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 710 | rcu_read_lock(); |
| 711 | nc = rcu_dereference(tconn->net_conf); |
| 712 | if (!nc) { |
| 713 | rcu_read_unlock(); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 714 | return NULL; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 715 | } |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 716 | sndbuf_size = nc->sndbuf_size; |
| 717 | rcvbuf_size = nc->rcvbuf_size; |
Andreas Gruenbacher | 69ef82d | 2011-05-11 14:34:35 +0200 | [diff] [blame] | 718 | connect_int = nc->connect_int; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 719 | rcu_read_unlock(); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 720 | |
Andreas Gruenbacher | 089c075 | 2011-06-14 18:28:09 +0200 | [diff] [blame] | 721 | my_addr_len = min_t(int, tconn->my_addr_len, sizeof(struct sockaddr_in6)); |
| 722 | memcpy(&my_addr, &tconn->my_addr, my_addr_len); |
| 723 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 724 | what = "sock_create_kern"; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 725 | err = sock_create_kern(((struct sockaddr *)&my_addr)->sa_family, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 726 | SOCK_STREAM, IPPROTO_TCP, &s_listen); |
| 727 | if (err) { |
| 728 | s_listen = NULL; |
| 729 | goto out; |
| 730 | } |
| 731 | |
Andreas Gruenbacher | 69ef82d | 2011-05-11 14:34:35 +0200 | [diff] [blame] | 732 | timeo = connect_int * HZ; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 733 | timeo += (random32() & 1) ? timeo / 7 : -timeo / 7; /* 28.5% random jitter */ |
| 734 | |
| 735 | s_listen->sk->sk_reuse = 1; /* SO_REUSEADDR */ |
| 736 | s_listen->sk->sk_rcvtimeo = timeo; |
| 737 | s_listen->sk->sk_sndtimeo = timeo; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 738 | drbd_setbufsize(s_listen, sndbuf_size, rcvbuf_size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 739 | |
| 740 | what = "bind before listen"; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 741 | err = s_listen->ops->bind(s_listen, (struct sockaddr *)&my_addr, my_addr_len); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 742 | if (err < 0) |
| 743 | goto out; |
| 744 | |
Philipp Reisner | 7653620 | 2011-02-07 14:09:54 +0100 | [diff] [blame] | 745 | err = drbd_accept(&what, s_listen, &s_estab); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 746 | |
| 747 | out: |
| 748 | if (s_listen) |
| 749 | sock_release(s_listen); |
| 750 | if (err < 0) { |
| 751 | if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) { |
Philipp Reisner | 7653620 | 2011-02-07 14:09:54 +0100 | [diff] [blame] | 752 | conn_err(tconn, "%s failed, err = %d\n", what, err); |
Philipp Reisner | bbeb641 | 2011-02-10 13:45:46 +0100 | [diff] [blame] | 753 | conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 754 | } |
| 755 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 756 | |
| 757 | return s_estab; |
| 758 | } |
| 759 | |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 760 | static int decode_header(struct drbd_tconn *, void *, struct packet_info *); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 761 | |
Andreas Gruenbacher | 9f5bdc3 | 2011-03-28 14:23:08 +0200 | [diff] [blame] | 762 | static int send_first_packet(struct drbd_tconn *tconn, struct drbd_socket *sock, |
| 763 | enum drbd_packet cmd) |
| 764 | { |
| 765 | if (!conn_prepare_command(tconn, sock)) |
| 766 | return -EIO; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 767 | return conn_send_command(tconn, sock, cmd, 0, NULL, 0); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 768 | } |
| 769 | |
Andreas Gruenbacher | 9f5bdc3 | 2011-03-28 14:23:08 +0200 | [diff] [blame] | 770 | static int receive_first_packet(struct drbd_tconn *tconn, struct socket *sock) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 771 | { |
Andreas Gruenbacher | 9f5bdc3 | 2011-03-28 14:23:08 +0200 | [diff] [blame] | 772 | unsigned int header_size = drbd_header_size(tconn); |
| 773 | struct packet_info pi; |
| 774 | int err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 775 | |
Andreas Gruenbacher | 9f5bdc3 | 2011-03-28 14:23:08 +0200 | [diff] [blame] | 776 | err = drbd_recv_short(sock, tconn->data.rbuf, header_size, 0); |
| 777 | if (err != header_size) { |
| 778 | if (err >= 0) |
| 779 | err = -EIO; |
| 780 | return err; |
| 781 | } |
| 782 | err = decode_header(tconn, tconn->data.rbuf, &pi); |
| 783 | if (err) |
| 784 | return err; |
| 785 | return pi.cmd; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | /** |
| 789 | * drbd_socket_okay() - Free the socket if its connection is not okay |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 790 | * @sock: pointer to the pointer to the socket. |
| 791 | */ |
Philipp Reisner | dbd9eea | 2011-02-07 15:34:16 +0100 | [diff] [blame] | 792 | static int drbd_socket_okay(struct socket **sock) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 793 | { |
| 794 | int rr; |
| 795 | char tb[4]; |
| 796 | |
| 797 | if (!*sock) |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 798 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 799 | |
Philipp Reisner | dbd9eea | 2011-02-07 15:34:16 +0100 | [diff] [blame] | 800 | rr = drbd_recv_short(*sock, tb, 4, MSG_DONTWAIT | MSG_PEEK); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 801 | |
| 802 | if (rr > 0 || rr == -EAGAIN) { |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 803 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 804 | } else { |
| 805 | sock_release(*sock); |
| 806 | *sock = NULL; |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 807 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 808 | } |
| 809 | } |
Philipp Reisner | 2325eb6 | 2011-03-15 16:56:18 +0100 | [diff] [blame] | 810 | /* Gets called if a connection is established, or if a new minor gets created |
| 811 | in a connection */ |
Philipp Reisner | c141ebd | 2011-05-05 16:13:10 +0200 | [diff] [blame] | 812 | int drbd_connected(struct drbd_conf *mdev) |
Philipp Reisner | 907599e | 2011-02-08 11:25:37 +0100 | [diff] [blame] | 813 | { |
Andreas Gruenbacher | 0829f5e | 2011-03-24 14:31:22 +0100 | [diff] [blame] | 814 | int err; |
Philipp Reisner | 907599e | 2011-02-08 11:25:37 +0100 | [diff] [blame] | 815 | |
| 816 | atomic_set(&mdev->packet_seq, 0); |
| 817 | mdev->peer_seq = 0; |
| 818 | |
Philipp Reisner | 8410da8 | 2011-02-11 20:11:10 +0100 | [diff] [blame] | 819 | mdev->state_mutex = mdev->tconn->agreed_pro_version < 100 ? |
| 820 | &mdev->tconn->cstate_mutex : |
| 821 | &mdev->own_state_mutex; |
| 822 | |
Andreas Gruenbacher | 0829f5e | 2011-03-24 14:31:22 +0100 | [diff] [blame] | 823 | err = drbd_send_sync_param(mdev); |
| 824 | if (!err) |
| 825 | err = drbd_send_sizes(mdev, 0, 0); |
| 826 | if (!err) |
| 827 | err = drbd_send_uuids(mdev); |
| 828 | if (!err) |
Philipp Reisner | 43de7c8 | 2011-11-10 13:16:13 +0100 | [diff] [blame] | 829 | err = drbd_send_current_state(mdev); |
Philipp Reisner | 907599e | 2011-02-08 11:25:37 +0100 | [diff] [blame] | 830 | clear_bit(USE_DEGR_WFC_T, &mdev->flags); |
| 831 | clear_bit(RESIZE_PENDING, &mdev->flags); |
Philipp Reisner | 8b924f1 | 2011-03-01 11:08:28 +0100 | [diff] [blame] | 832 | mod_timer(&mdev->request_timer, jiffies + HZ); /* just start it here. */ |
Andreas Gruenbacher | 0829f5e | 2011-03-24 14:31:22 +0100 | [diff] [blame] | 833 | return err; |
Philipp Reisner | 907599e | 2011-02-08 11:25:37 +0100 | [diff] [blame] | 834 | } |
| 835 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 836 | /* |
| 837 | * return values: |
| 838 | * 1 yes, we have a valid connection |
| 839 | * 0 oops, did not work out, please try again |
| 840 | * -1 peer talks different language, |
| 841 | * no point in trying again, please go standalone. |
| 842 | * -2 We do not have a network config... |
| 843 | */ |
Philipp Reisner | 81fa2e6 | 2011-05-04 15:10:30 +0200 | [diff] [blame] | 844 | static int conn_connect(struct drbd_tconn *tconn) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 845 | { |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 846 | struct drbd_socket sock, msock; |
Philipp Reisner | c141ebd | 2011-05-05 16:13:10 +0200 | [diff] [blame] | 847 | struct drbd_conf *mdev; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 848 | struct net_conf *nc; |
Philipp Reisner | c141ebd | 2011-05-05 16:13:10 +0200 | [diff] [blame] | 849 | int vnr, timeout, try, h, ok; |
Philipp Reisner | 08b165b | 2011-09-05 16:22:33 +0200 | [diff] [blame] | 850 | bool discard_my_data; |
Philipp Reisner | a1096a6 | 2012-04-06 12:07:34 +0200 | [diff] [blame] | 851 | enum drbd_state_rv rv; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 852 | |
Philipp Reisner | bbeb641 | 2011-02-10 13:45:46 +0100 | [diff] [blame] | 853 | 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] | 854 | return -2; |
| 855 | |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 856 | mutex_init(&sock.mutex); |
| 857 | sock.sbuf = tconn->data.sbuf; |
| 858 | sock.rbuf = tconn->data.rbuf; |
| 859 | sock.socket = NULL; |
| 860 | mutex_init(&msock.mutex); |
| 861 | msock.sbuf = tconn->meta.sbuf; |
| 862 | msock.rbuf = tconn->meta.rbuf; |
| 863 | msock.socket = NULL; |
| 864 | |
Philipp Reisner | 907599e | 2011-02-08 11:25:37 +0100 | [diff] [blame] | 865 | clear_bit(DISCARD_CONCURRENT, &tconn->flags); |
Andreas Gruenbacher | 0916e0e | 2011-03-21 14:10:15 +0100 | [diff] [blame] | 866 | |
| 867 | /* Assume that the peer only understands protocol 80 until we know better. */ |
| 868 | tconn->agreed_pro_version = 80; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 869 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 870 | do { |
Andreas Gruenbacher | 2bf8962 | 2011-03-28 16:33:12 +0200 | [diff] [blame] | 871 | struct socket *s; |
| 872 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 873 | for (try = 0;;) { |
| 874 | /* 3 tries, this should take less than a second! */ |
Philipp Reisner | 907599e | 2011-02-08 11:25:37 +0100 | [diff] [blame] | 875 | s = drbd_try_connect(tconn); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 876 | if (s || ++try >= 3) |
| 877 | break; |
| 878 | /* give the other side time to call bind() & listen() */ |
Philipp Reisner | 20ee639 | 2011-01-18 15:28:59 +0100 | [diff] [blame] | 879 | schedule_timeout_interruptible(HZ / 10); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | if (s) { |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 883 | if (!sock.socket) { |
| 884 | sock.socket = s; |
| 885 | send_first_packet(tconn, &sock, P_INITIAL_DATA); |
| 886 | } else if (!msock.socket) { |
| 887 | msock.socket = s; |
| 888 | send_first_packet(tconn, &msock, P_INITIAL_META); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 889 | } else { |
Philipp Reisner | 81fa2e6 | 2011-05-04 15:10:30 +0200 | [diff] [blame] | 890 | conn_err(tconn, "Logic error in conn_connect()\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 891 | goto out_release_sockets; |
| 892 | } |
| 893 | } |
| 894 | |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 895 | if (sock.socket && msock.socket) { |
| 896 | rcu_read_lock(); |
| 897 | nc = rcu_dereference(tconn->net_conf); |
| 898 | timeout = nc->ping_timeo * HZ / 10; |
| 899 | rcu_read_unlock(); |
| 900 | schedule_timeout_interruptible(timeout); |
| 901 | ok = drbd_socket_okay(&sock.socket); |
| 902 | ok = drbd_socket_okay(&msock.socket) && ok; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 903 | if (ok) |
| 904 | break; |
| 905 | } |
| 906 | |
| 907 | retry: |
Philipp Reisner | 907599e | 2011-02-08 11:25:37 +0100 | [diff] [blame] | 908 | s = drbd_wait_for_connect(tconn); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 909 | if (s) { |
Andreas Gruenbacher | 9f5bdc3 | 2011-03-28 14:23:08 +0200 | [diff] [blame] | 910 | try = receive_first_packet(tconn, s); |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 911 | drbd_socket_okay(&sock.socket); |
| 912 | drbd_socket_okay(&msock.socket); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 913 | switch (try) { |
Andreas Gruenbacher | e5d6f33 | 2011-03-28 16:44:40 +0200 | [diff] [blame] | 914 | case P_INITIAL_DATA: |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 915 | if (sock.socket) { |
Philipp Reisner | 907599e | 2011-02-08 11:25:37 +0100 | [diff] [blame] | 916 | conn_warn(tconn, "initial packet S crossed\n"); |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 917 | sock_release(sock.socket); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 918 | } |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 919 | sock.socket = s; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 920 | break; |
Andreas Gruenbacher | e5d6f33 | 2011-03-28 16:44:40 +0200 | [diff] [blame] | 921 | case P_INITIAL_META: |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 922 | if (msock.socket) { |
Philipp Reisner | 907599e | 2011-02-08 11:25:37 +0100 | [diff] [blame] | 923 | conn_warn(tconn, "initial packet M crossed\n"); |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 924 | sock_release(msock.socket); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 925 | } |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 926 | msock.socket = s; |
Philipp Reisner | 907599e | 2011-02-08 11:25:37 +0100 | [diff] [blame] | 927 | set_bit(DISCARD_CONCURRENT, &tconn->flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 928 | break; |
| 929 | default: |
Philipp Reisner | 907599e | 2011-02-08 11:25:37 +0100 | [diff] [blame] | 930 | conn_warn(tconn, "Error receiving initial packet\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 931 | sock_release(s); |
| 932 | if (random32() & 1) |
| 933 | goto retry; |
| 934 | } |
| 935 | } |
| 936 | |
Philipp Reisner | bbeb641 | 2011-02-10 13:45:46 +0100 | [diff] [blame] | 937 | if (tconn->cstate <= C_DISCONNECTING) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 938 | goto out_release_sockets; |
| 939 | if (signal_pending(current)) { |
| 940 | flush_signals(current); |
| 941 | smp_rmb(); |
Philipp Reisner | 907599e | 2011-02-08 11:25:37 +0100 | [diff] [blame] | 942 | if (get_t_state(&tconn->receiver) == EXITING) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 943 | goto out_release_sockets; |
| 944 | } |
| 945 | |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 946 | if (sock.socket && &msock.socket) { |
| 947 | ok = drbd_socket_okay(&sock.socket); |
| 948 | ok = drbd_socket_okay(&msock.socket) && ok; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 949 | if (ok) |
| 950 | break; |
| 951 | } |
| 952 | } while (1); |
| 953 | |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 954 | sock.socket->sk->sk_reuse = 1; /* SO_REUSEADDR */ |
| 955 | msock.socket->sk->sk_reuse = 1; /* SO_REUSEADDR */ |
Andreas Gruenbacher | 2bf8962 | 2011-03-28 16:33:12 +0200 | [diff] [blame] | 956 | |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 957 | sock.socket->sk->sk_allocation = GFP_NOIO; |
| 958 | msock.socket->sk->sk_allocation = GFP_NOIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 959 | |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 960 | sock.socket->sk->sk_priority = TC_PRIO_INTERACTIVE_BULK; |
| 961 | msock.socket->sk->sk_priority = TC_PRIO_INTERACTIVE; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 962 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 963 | /* NOT YET ... |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 964 | * sock.socket->sk->sk_sndtimeo = tconn->net_conf->timeout*HZ/10; |
| 965 | * sock.socket->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT; |
Andreas Gruenbacher | 6038178 | 2011-03-28 17:05:50 +0200 | [diff] [blame] | 966 | * first set it to the P_CONNECTION_FEATURES timeout, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 967 | * which we set to 4x the configured ping_timeout. */ |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 968 | rcu_read_lock(); |
| 969 | nc = rcu_dereference(tconn->net_conf); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 970 | |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 971 | sock.socket->sk->sk_sndtimeo = |
| 972 | sock.socket->sk->sk_rcvtimeo = nc->ping_timeo*4*HZ/10; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 973 | |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 974 | msock.socket->sk->sk_rcvtimeo = nc->ping_int*HZ; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 975 | timeout = nc->timeout * HZ / 10; |
Philipp Reisner | 08b165b | 2011-09-05 16:22:33 +0200 | [diff] [blame] | 976 | discard_my_data = nc->discard_my_data; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 977 | rcu_read_unlock(); |
| 978 | |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 979 | msock.socket->sk->sk_sndtimeo = timeout; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 980 | |
| 981 | /* we don't want delays. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 982 | * we use TCP_CORK where appropriate, though */ |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 983 | drbd_tcp_nodelay(sock.socket); |
| 984 | drbd_tcp_nodelay(msock.socket); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 985 | |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 986 | tconn->data.socket = sock.socket; |
| 987 | tconn->meta.socket = msock.socket; |
Philipp Reisner | 907599e | 2011-02-08 11:25:37 +0100 | [diff] [blame] | 988 | tconn->last_received = jiffies; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 989 | |
Andreas Gruenbacher | 6038178 | 2011-03-28 17:05:50 +0200 | [diff] [blame] | 990 | h = drbd_do_features(tconn); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 991 | if (h <= 0) |
| 992 | return h; |
| 993 | |
Philipp Reisner | 907599e | 2011-02-08 11:25:37 +0100 | [diff] [blame] | 994 | if (tconn->cram_hmac_tfm) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 995 | /* drbd_request_state(mdev, NS(conn, WFAuth)); */ |
Philipp Reisner | 907599e | 2011-02-08 11:25:37 +0100 | [diff] [blame] | 996 | switch (drbd_do_auth(tconn)) { |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 997 | case -1: |
Philipp Reisner | 907599e | 2011-02-08 11:25:37 +0100 | [diff] [blame] | 998 | conn_err(tconn, "Authentication of peer failed\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 999 | return -1; |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 1000 | case 0: |
Philipp Reisner | 907599e | 2011-02-08 11:25:37 +0100 | [diff] [blame] | 1001 | conn_err(tconn, "Authentication of peer failed, trying again.\n"); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 1002 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1003 | } |
| 1004 | } |
| 1005 | |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 1006 | tconn->data.socket->sk->sk_sndtimeo = timeout; |
| 1007 | tconn->data.socket->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1008 | |
Andreas Gruenbacher | 387eb30 | 2011-03-16 01:05:37 +0100 | [diff] [blame] | 1009 | if (drbd_send_protocol(tconn) == -EOPNOTSUPP) |
Philipp Reisner | 7e2455c | 2010-04-22 14:50:23 +0200 | [diff] [blame] | 1010 | return -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1011 | |
Philipp Reisner | a1096a6 | 2012-04-06 12:07:34 +0200 | [diff] [blame] | 1012 | set_bit(STATE_SENT, &tconn->flags); |
| 1013 | |
Philipp Reisner | c141ebd | 2011-05-05 16:13:10 +0200 | [diff] [blame] | 1014 | rcu_read_lock(); |
| 1015 | idr_for_each_entry(&tconn->volumes, mdev, vnr) { |
| 1016 | kref_get(&mdev->kref); |
| 1017 | rcu_read_unlock(); |
Philipp Reisner | 08b165b | 2011-09-05 16:22:33 +0200 | [diff] [blame] | 1018 | |
| 1019 | if (discard_my_data) |
| 1020 | set_bit(DISCARD_MY_DATA, &mdev->flags); |
| 1021 | else |
| 1022 | clear_bit(DISCARD_MY_DATA, &mdev->flags); |
| 1023 | |
Philipp Reisner | c141ebd | 2011-05-05 16:13:10 +0200 | [diff] [blame] | 1024 | drbd_connected(mdev); |
| 1025 | kref_put(&mdev->kref, &drbd_minor_destroy); |
| 1026 | rcu_read_lock(); |
| 1027 | } |
| 1028 | rcu_read_unlock(); |
| 1029 | |
Philipp Reisner | a1096a6 | 2012-04-06 12:07:34 +0200 | [diff] [blame] | 1030 | rv = conn_request_state(tconn, NS(conn, C_WF_REPORT_PARAMS), CS_VERBOSE); |
| 1031 | if (rv < SS_SUCCESS) { |
| 1032 | clear_bit(STATE_SENT, &tconn->flags); |
Philipp Reisner | 823bd83 | 2012-11-08 15:04:36 +0100 | [diff] [blame] | 1033 | return 0; |
Philipp Reisner | a1096a6 | 2012-04-06 12:07:34 +0200 | [diff] [blame] | 1034 | } |
Philipp Reisner | 823bd83 | 2012-11-08 15:04:36 +0100 | [diff] [blame] | 1035 | |
| 1036 | drbd_thread_start(&tconn->asender); |
| 1037 | |
Philipp Reisner | 08b165b | 2011-09-05 16:22:33 +0200 | [diff] [blame] | 1038 | mutex_lock(&tconn->conf_update); |
| 1039 | /* The discard_my_data flag is a single-shot modifier to the next |
| 1040 | * connection attempt, the handshake of which is now well underway. |
| 1041 | * No need for rcu style copying of the whole struct |
| 1042 | * just to clear a single value. */ |
| 1043 | tconn->net_conf->discard_my_data = 0; |
| 1044 | mutex_unlock(&tconn->conf_update); |
| 1045 | |
Philipp Reisner | d3fcb49 | 2011-04-13 14:46:05 -0700 | [diff] [blame] | 1046 | return h; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1047 | |
| 1048 | out_release_sockets: |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 1049 | if (sock.socket) |
| 1050 | sock_release(sock.socket); |
| 1051 | if (msock.socket) |
| 1052 | sock_release(msock.socket); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1053 | return -1; |
| 1054 | } |
| 1055 | |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 1056 | static int decode_header(struct drbd_tconn *tconn, void *header, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1057 | { |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 1058 | unsigned int header_size = drbd_header_size(tconn); |
| 1059 | |
Andreas Gruenbacher | 0c8e36d | 2011-03-30 16:00:17 +0200 | [diff] [blame] | 1060 | if (header_size == sizeof(struct p_header100) && |
| 1061 | *(__be32 *)header == cpu_to_be32(DRBD_MAGIC_100)) { |
| 1062 | struct p_header100 *h = header; |
| 1063 | if (h->pad != 0) { |
| 1064 | conn_err(tconn, "Header padding is not zero\n"); |
| 1065 | return -EINVAL; |
| 1066 | } |
| 1067 | pi->vnr = be16_to_cpu(h->volume); |
| 1068 | pi->cmd = be16_to_cpu(h->command); |
| 1069 | pi->size = be32_to_cpu(h->length); |
| 1070 | } else if (header_size == sizeof(struct p_header95) && |
| 1071 | *(__be16 *)header == cpu_to_be16(DRBD_MAGIC_BIG)) { |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 1072 | struct p_header95 *h = header; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 1073 | pi->cmd = be16_to_cpu(h->command); |
Andreas Gruenbacher | b55d84b | 2011-03-22 13:17:47 +0100 | [diff] [blame] | 1074 | pi->size = be32_to_cpu(h->length); |
| 1075 | pi->vnr = 0; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 1076 | } else if (header_size == sizeof(struct p_header80) && |
| 1077 | *(__be32 *)header == cpu_to_be32(DRBD_MAGIC)) { |
| 1078 | struct p_header80 *h = header; |
| 1079 | pi->cmd = be16_to_cpu(h->command); |
| 1080 | pi->size = be16_to_cpu(h->length); |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 1081 | pi->vnr = 0; |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 1082 | } else { |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 1083 | conn_err(tconn, "Wrong magic value 0x%08x in protocol version %d\n", |
| 1084 | be32_to_cpu(*(__be32 *)header), |
| 1085 | tconn->agreed_pro_version); |
Andreas Gruenbacher | 8172f3e | 2011-03-16 17:22:39 +0100 | [diff] [blame] | 1086 | return -EINVAL; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1087 | } |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 1088 | pi->data = header + header_size; |
Andreas Gruenbacher | 8172f3e | 2011-03-16 17:22:39 +0100 | [diff] [blame] | 1089 | return 0; |
Philipp Reisner | 257d0af | 2011-01-26 12:15:29 +0100 | [diff] [blame] | 1090 | } |
| 1091 | |
Philipp Reisner | 9ba7aa0 | 2011-02-07 17:32:41 +0100 | [diff] [blame] | 1092 | 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] | 1093 | { |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 1094 | void *buffer = tconn->data.rbuf; |
Andreas Gruenbacher | 69bc7bc | 2011-03-16 17:31:52 +0100 | [diff] [blame] | 1095 | int err; |
Philipp Reisner | 257d0af | 2011-01-26 12:15:29 +0100 | [diff] [blame] | 1096 | |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 1097 | err = drbd_recv_all_warn(tconn, buffer, drbd_header_size(tconn)); |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 1098 | if (err) |
Andreas Gruenbacher | 69bc7bc | 2011-03-16 17:31:52 +0100 | [diff] [blame] | 1099 | return err; |
Philipp Reisner | 257d0af | 2011-01-26 12:15:29 +0100 | [diff] [blame] | 1100 | |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 1101 | err = decode_header(tconn, buffer, pi); |
Philipp Reisner | 9ba7aa0 | 2011-02-07 17:32:41 +0100 | [diff] [blame] | 1102 | tconn->last_received = jiffies; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1103 | |
Andreas Gruenbacher | 69bc7bc | 2011-03-16 17:31:52 +0100 | [diff] [blame] | 1104 | return err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
Philipp Reisner | 4b0007c | 2011-11-09 20:12:34 +0100 | [diff] [blame] | 1107 | static void drbd_flush(struct drbd_tconn *tconn) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1108 | { |
| 1109 | int rv; |
Philipp Reisner | 4b0007c | 2011-11-09 20:12:34 +0100 | [diff] [blame] | 1110 | struct drbd_conf *mdev; |
| 1111 | int vnr; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1112 | |
Philipp Reisner | 4b0007c | 2011-11-09 20:12:34 +0100 | [diff] [blame] | 1113 | if (tconn->write_ordering >= WO_bdev_flush) { |
Lars Ellenberg | 615e087 | 2011-11-17 14:32:12 +0100 | [diff] [blame] | 1114 | rcu_read_lock(); |
Philipp Reisner | 4b0007c | 2011-11-09 20:12:34 +0100 | [diff] [blame] | 1115 | idr_for_each_entry(&tconn->volumes, mdev, vnr) { |
Lars Ellenberg | 615e087 | 2011-11-17 14:32:12 +0100 | [diff] [blame] | 1116 | if (!get_ldev(mdev)) |
| 1117 | continue; |
| 1118 | kref_get(&mdev->kref); |
| 1119 | rcu_read_unlock(); |
Philipp Reisner | 4b0007c | 2011-11-09 20:12:34 +0100 | [diff] [blame] | 1120 | |
Lars Ellenberg | 615e087 | 2011-11-17 14:32:12 +0100 | [diff] [blame] | 1121 | rv = blkdev_issue_flush(mdev->ldev->backing_bdev, |
| 1122 | GFP_NOIO, NULL); |
| 1123 | if (rv) { |
| 1124 | dev_info(DEV, "local disk flush failed with status %d\n", rv); |
| 1125 | /* would rather check on EOPNOTSUPP, but that is not reliable. |
| 1126 | * don't try again for ANY return value != 0 |
| 1127 | * if (rv == -EOPNOTSUPP) */ |
| 1128 | drbd_bump_write_ordering(tconn, WO_drain_io); |
Philipp Reisner | 4b0007c | 2011-11-09 20:12:34 +0100 | [diff] [blame] | 1129 | } |
Lars Ellenberg | 615e087 | 2011-11-17 14:32:12 +0100 | [diff] [blame] | 1130 | put_ldev(mdev); |
| 1131 | kref_put(&mdev->kref, &drbd_minor_destroy); |
| 1132 | |
| 1133 | rcu_read_lock(); |
| 1134 | if (rv) |
| 1135 | break; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1136 | } |
Lars Ellenberg | 615e087 | 2011-11-17 14:32:12 +0100 | [diff] [blame] | 1137 | rcu_read_unlock(); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1138 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | /** |
| 1142 | * drbd_may_finish_epoch() - Applies an epoch_event to the epoch's state, eventually finishes it. |
| 1143 | * @mdev: DRBD device. |
| 1144 | * @epoch: Epoch object. |
| 1145 | * @ev: Epoch event. |
| 1146 | */ |
Philipp Reisner | 1e9dd29 | 2011-11-10 15:14:53 +0100 | [diff] [blame] | 1147 | static enum finish_epoch drbd_may_finish_epoch(struct drbd_tconn *tconn, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1148 | struct drbd_epoch *epoch, |
| 1149 | enum epoch_event ev) |
| 1150 | { |
Philipp Reisner | 2451fc3 | 2010-08-24 13:43:11 +0200 | [diff] [blame] | 1151 | int epoch_size; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1152 | struct drbd_epoch *next_epoch; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1153 | enum finish_epoch rv = FE_STILL_LIVE; |
| 1154 | |
Philipp Reisner | 12038a3 | 2011-11-09 19:18:00 +0100 | [diff] [blame] | 1155 | spin_lock(&tconn->epoch_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1156 | do { |
| 1157 | next_epoch = NULL; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1158 | |
| 1159 | epoch_size = atomic_read(&epoch->epoch_size); |
| 1160 | |
| 1161 | switch (ev & ~EV_CLEANUP) { |
| 1162 | case EV_PUT: |
| 1163 | atomic_dec(&epoch->active); |
| 1164 | break; |
| 1165 | case EV_GOT_BARRIER_NR: |
| 1166 | set_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1167 | break; |
| 1168 | case EV_BECAME_LAST: |
| 1169 | /* nothing to do*/ |
| 1170 | break; |
| 1171 | } |
| 1172 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1173 | if (epoch_size != 0 && |
| 1174 | atomic_read(&epoch->active) == 0 && |
Philipp Reisner | 85d73513 | 2011-07-18 15:45:15 +0200 | [diff] [blame] | 1175 | (test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags) || ev & EV_CLEANUP)) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1176 | if (!(ev & EV_CLEANUP)) { |
Philipp Reisner | 12038a3 | 2011-11-09 19:18:00 +0100 | [diff] [blame] | 1177 | spin_unlock(&tconn->epoch_lock); |
Lars Ellenberg | 9ed57dc | 2012-03-26 20:55:17 +0200 | [diff] [blame] | 1178 | drbd_send_b_ack(epoch->tconn, epoch->barrier_nr, epoch_size); |
Philipp Reisner | 12038a3 | 2011-11-09 19:18:00 +0100 | [diff] [blame] | 1179 | spin_lock(&tconn->epoch_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1180 | } |
Lars Ellenberg | 9ed57dc | 2012-03-26 20:55:17 +0200 | [diff] [blame] | 1181 | #if 0 |
| 1182 | /* FIXME: dec unacked on connection, once we have |
| 1183 | * something to count pending connection packets in. */ |
Philipp Reisner | 85d73513 | 2011-07-18 15:45:15 +0200 | [diff] [blame] | 1184 | if (test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags)) |
Lars Ellenberg | 9ed57dc | 2012-03-26 20:55:17 +0200 | [diff] [blame] | 1185 | dec_unacked(epoch->tconn); |
| 1186 | #endif |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1187 | |
Philipp Reisner | 12038a3 | 2011-11-09 19:18:00 +0100 | [diff] [blame] | 1188 | if (tconn->current_epoch != epoch) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1189 | next_epoch = list_entry(epoch->list.next, struct drbd_epoch, list); |
| 1190 | list_del(&epoch->list); |
| 1191 | ev = EV_BECAME_LAST | (ev & EV_CLEANUP); |
Philipp Reisner | 12038a3 | 2011-11-09 19:18:00 +0100 | [diff] [blame] | 1192 | tconn->epochs--; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1193 | kfree(epoch); |
| 1194 | |
| 1195 | if (rv == FE_STILL_LIVE) |
| 1196 | rv = FE_DESTROYED; |
| 1197 | } else { |
| 1198 | epoch->flags = 0; |
| 1199 | atomic_set(&epoch->epoch_size, 0); |
Uwe Kleine-König | 698f931 | 2010-07-02 20:41:51 +0200 | [diff] [blame] | 1200 | /* atomic_set(&epoch->active, 0); is already zero */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1201 | if (rv == FE_STILL_LIVE) |
| 1202 | rv = FE_RECYCLED; |
| 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | if (!next_epoch) |
| 1207 | break; |
| 1208 | |
| 1209 | epoch = next_epoch; |
| 1210 | } while (1); |
| 1211 | |
Philipp Reisner | 12038a3 | 2011-11-09 19:18:00 +0100 | [diff] [blame] | 1212 | spin_unlock(&tconn->epoch_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1213 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1214 | return rv; |
| 1215 | } |
| 1216 | |
| 1217 | /** |
| 1218 | * drbd_bump_write_ordering() - Fall back to an other write ordering method |
Philipp Reisner | 4b0007c | 2011-11-09 20:12:34 +0100 | [diff] [blame] | 1219 | * @tconn: DRBD connection. |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1220 | * @wo: Write ordering method to try. |
| 1221 | */ |
Philipp Reisner | 4b0007c | 2011-11-09 20:12:34 +0100 | [diff] [blame] | 1222 | void drbd_bump_write_ordering(struct drbd_tconn *tconn, enum write_ordering_e wo) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1223 | { |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 1224 | struct disk_conf *dc; |
Philipp Reisner | 4b0007c | 2011-11-09 20:12:34 +0100 | [diff] [blame] | 1225 | struct drbd_conf *mdev; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1226 | enum write_ordering_e pwo; |
Philipp Reisner | 4b0007c | 2011-11-09 20:12:34 +0100 | [diff] [blame] | 1227 | int vnr; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1228 | static char *write_ordering_str[] = { |
| 1229 | [WO_none] = "none", |
| 1230 | [WO_drain_io] = "drain", |
| 1231 | [WO_bdev_flush] = "flush", |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1232 | }; |
| 1233 | |
Philipp Reisner | 4b0007c | 2011-11-09 20:12:34 +0100 | [diff] [blame] | 1234 | pwo = tconn->write_ordering; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1235 | wo = min(pwo, wo); |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 1236 | rcu_read_lock(); |
Philipp Reisner | 4b0007c | 2011-11-09 20:12:34 +0100 | [diff] [blame] | 1237 | idr_for_each_entry(&tconn->volumes, mdev, vnr) { |
Philipp Reisner | 27eb13e | 2012-03-30 14:12:15 +0200 | [diff] [blame] | 1238 | if (!get_ldev_if_state(mdev, D_ATTACHING)) |
Philipp Reisner | 4b0007c | 2011-11-09 20:12:34 +0100 | [diff] [blame] | 1239 | continue; |
| 1240 | dc = rcu_dereference(mdev->ldev->disk_conf); |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 1241 | |
Philipp Reisner | 4b0007c | 2011-11-09 20:12:34 +0100 | [diff] [blame] | 1242 | if (wo == WO_bdev_flush && !dc->disk_flushes) |
| 1243 | wo = WO_drain_io; |
| 1244 | if (wo == WO_drain_io && !dc->disk_drain) |
| 1245 | wo = WO_none; |
| 1246 | put_ldev(mdev); |
| 1247 | } |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 1248 | rcu_read_unlock(); |
Philipp Reisner | 4b0007c | 2011-11-09 20:12:34 +0100 | [diff] [blame] | 1249 | tconn->write_ordering = wo; |
| 1250 | if (pwo != tconn->write_ordering || wo == WO_bdev_flush) |
| 1251 | conn_info(tconn, "Method to ensure write ordering: %s\n", write_ordering_str[tconn->write_ordering]); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1252 | } |
| 1253 | |
| 1254 | /** |
Andreas Gruenbacher | fbe29de | 2011-02-17 16:38:35 +0100 | [diff] [blame] | 1255 | * drbd_submit_peer_request() |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1256 | * @mdev: DRBD device. |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1257 | * @peer_req: peer request |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1258 | * @rw: flag field, see bio->bi_rw |
Lars Ellenberg | 10f6d992 | 2011-01-24 14:47:09 +0100 | [diff] [blame] | 1259 | * |
| 1260 | * May spread the pages to multiple bios, |
| 1261 | * depending on bio_add_page restrictions. |
| 1262 | * |
| 1263 | * Returns 0 if all bios have been submitted, |
| 1264 | * -ENOMEM if we could not allocate enough bios, |
| 1265 | * -ENOSPC (any better suggestion?) if we have not been able to bio_add_page a |
| 1266 | * single page to an empty bio (which should never happen and likely indicates |
| 1267 | * that the lower level IO stack is in some way broken). This has been observed |
| 1268 | * on certain Xen deployments. |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1269 | */ |
| 1270 | /* TODO allocate from our own bio_set. */ |
Andreas Gruenbacher | fbe29de | 2011-02-17 16:38:35 +0100 | [diff] [blame] | 1271 | int drbd_submit_peer_request(struct drbd_conf *mdev, |
| 1272 | struct drbd_peer_request *peer_req, |
| 1273 | const unsigned rw, const int fault_type) |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1274 | { |
| 1275 | struct bio *bios = NULL; |
| 1276 | struct bio *bio; |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1277 | struct page *page = peer_req->pages; |
| 1278 | sector_t sector = peer_req->i.sector; |
| 1279 | unsigned ds = peer_req->i.size; |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1280 | unsigned n_bios = 0; |
| 1281 | unsigned nr_pages = (ds + PAGE_SIZE -1) >> PAGE_SHIFT; |
Lars Ellenberg | 10f6d992 | 2011-01-24 14:47:09 +0100 | [diff] [blame] | 1282 | int err = -ENOMEM; |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1283 | |
| 1284 | /* In most cases, we will only need one bio. But in case the lower |
| 1285 | * level restrictions happen to be different at this offset on this |
| 1286 | * 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] | 1287 | * request in more than one bio. |
| 1288 | * |
| 1289 | * Plain bio_alloc is good enough here, this is no DRBD internally |
| 1290 | * generated bio, but a bio allocated on behalf of the peer. |
| 1291 | */ |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1292 | next_bio: |
| 1293 | bio = bio_alloc(GFP_NOIO, nr_pages); |
| 1294 | if (!bio) { |
| 1295 | dev_err(DEV, "submit_ee: Allocation of a bio failed\n"); |
| 1296 | goto fail; |
| 1297 | } |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1298 | /* > peer_req->i.sector, unless this is the first bio */ |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1299 | bio->bi_sector = sector; |
| 1300 | bio->bi_bdev = mdev->ldev->backing_bdev; |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1301 | bio->bi_rw = rw; |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1302 | bio->bi_private = peer_req; |
Andreas Gruenbacher | fcefa62 | 2011-02-17 16:46:59 +0100 | [diff] [blame] | 1303 | bio->bi_end_io = drbd_peer_request_endio; |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1304 | |
| 1305 | bio->bi_next = bios; |
| 1306 | bios = bio; |
| 1307 | ++n_bios; |
| 1308 | |
| 1309 | page_chain_for_each(page) { |
| 1310 | unsigned len = min_t(unsigned, ds, PAGE_SIZE); |
| 1311 | if (!bio_add_page(bio, page, len, 0)) { |
Lars Ellenberg | 10f6d992 | 2011-01-24 14:47:09 +0100 | [diff] [blame] | 1312 | /* A single page must always be possible! |
| 1313 | * But in case it fails anyways, |
| 1314 | * we deal with it, and complain (below). */ |
| 1315 | if (bio->bi_vcnt == 0) { |
| 1316 | dev_err(DEV, |
| 1317 | "bio_add_page failed for len=%u, " |
| 1318 | "bi_vcnt=0 (bi_sector=%llu)\n", |
| 1319 | len, (unsigned long long)bio->bi_sector); |
| 1320 | err = -ENOSPC; |
| 1321 | goto fail; |
| 1322 | } |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1323 | goto next_bio; |
| 1324 | } |
| 1325 | ds -= len; |
| 1326 | sector += len >> 9; |
| 1327 | --nr_pages; |
| 1328 | } |
| 1329 | D_ASSERT(page == NULL); |
| 1330 | D_ASSERT(ds == 0); |
| 1331 | |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1332 | atomic_set(&peer_req->pending_bios, n_bios); |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1333 | do { |
| 1334 | bio = bios; |
| 1335 | bios = bios->bi_next; |
| 1336 | bio->bi_next = NULL; |
| 1337 | |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1338 | drbd_generic_make_request(mdev, fault_type, bio); |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1339 | } while (bios); |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1340 | return 0; |
| 1341 | |
| 1342 | fail: |
| 1343 | while (bios) { |
| 1344 | bio = bios; |
| 1345 | bios = bios->bi_next; |
| 1346 | bio_put(bio); |
| 1347 | } |
Lars Ellenberg | 10f6d992 | 2011-01-24 14:47:09 +0100 | [diff] [blame] | 1348 | return err; |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1349 | } |
| 1350 | |
Andreas Gruenbacher | 5384064 | 2011-01-28 10:31:04 +0100 | [diff] [blame] | 1351 | static void drbd_remove_epoch_entry_interval(struct drbd_conf *mdev, |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1352 | struct drbd_peer_request *peer_req) |
Andreas Gruenbacher | 5384064 | 2011-01-28 10:31:04 +0100 | [diff] [blame] | 1353 | { |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1354 | struct drbd_interval *i = &peer_req->i; |
Andreas Gruenbacher | 5384064 | 2011-01-28 10:31:04 +0100 | [diff] [blame] | 1355 | |
| 1356 | drbd_remove_interval(&mdev->write_requests, i); |
| 1357 | drbd_clear_interval(i); |
| 1358 | |
Andreas Gruenbacher | 6c852be | 2011-02-04 15:38:52 +0100 | [diff] [blame] | 1359 | /* Wake up any processes waiting for this peer request to complete. */ |
Andreas Gruenbacher | 5384064 | 2011-01-28 10:31:04 +0100 | [diff] [blame] | 1360 | if (i->waiting) |
| 1361 | wake_up(&mdev->misc_wait); |
| 1362 | } |
| 1363 | |
Philipp Reisner | 77fede5 | 2011-11-10 21:19:11 +0100 | [diff] [blame] | 1364 | void conn_wait_active_ee_empty(struct drbd_tconn *tconn) |
| 1365 | { |
| 1366 | struct drbd_conf *mdev; |
| 1367 | int vnr; |
| 1368 | |
| 1369 | rcu_read_lock(); |
| 1370 | idr_for_each_entry(&tconn->volumes, mdev, vnr) { |
| 1371 | kref_get(&mdev->kref); |
| 1372 | rcu_read_unlock(); |
| 1373 | drbd_wait_ee_list_empty(mdev, &mdev->active_ee); |
| 1374 | kref_put(&mdev->kref, &drbd_minor_destroy); |
| 1375 | rcu_read_lock(); |
| 1376 | } |
| 1377 | rcu_read_unlock(); |
| 1378 | } |
| 1379 | |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 1380 | static int receive_Barrier(struct drbd_tconn *tconn, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1381 | { |
Philipp Reisner | 2451fc3 | 2010-08-24 13:43:11 +0200 | [diff] [blame] | 1382 | int rv; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 1383 | struct p_barrier *p = pi->data; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1384 | struct drbd_epoch *epoch; |
| 1385 | |
Lars Ellenberg | 9ed57dc | 2012-03-26 20:55:17 +0200 | [diff] [blame] | 1386 | /* FIXME these are unacked on connection, |
| 1387 | * not a specific (peer)device. |
| 1388 | */ |
Philipp Reisner | 12038a3 | 2011-11-09 19:18:00 +0100 | [diff] [blame] | 1389 | tconn->current_epoch->barrier_nr = p->barrier; |
Lars Ellenberg | 9ed57dc | 2012-03-26 20:55:17 +0200 | [diff] [blame] | 1390 | tconn->current_epoch->tconn = tconn; |
Philipp Reisner | 1e9dd29 | 2011-11-10 15:14:53 +0100 | [diff] [blame] | 1391 | rv = drbd_may_finish_epoch(tconn, tconn->current_epoch, EV_GOT_BARRIER_NR); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1392 | |
| 1393 | /* P_BARRIER_ACK may imply that the corresponding extent is dropped from |
| 1394 | * the activity log, which means it would not be resynced in case the |
| 1395 | * R_PRIMARY crashes now. |
| 1396 | * Therefore we must send the barrier_ack after the barrier request was |
| 1397 | * completed. */ |
Philipp Reisner | 4b0007c | 2011-11-09 20:12:34 +0100 | [diff] [blame] | 1398 | switch (tconn->write_ordering) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1399 | case WO_none: |
| 1400 | if (rv == FE_RECYCLED) |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 1401 | return 0; |
Philipp Reisner | 2451fc3 | 2010-08-24 13:43:11 +0200 | [diff] [blame] | 1402 | |
| 1403 | /* receiver context, in the writeout path of the other node. |
| 1404 | * avoid potential distributed deadlock */ |
| 1405 | epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO); |
| 1406 | if (epoch) |
| 1407 | break; |
| 1408 | else |
Lars Ellenberg | 9ed57dc | 2012-03-26 20:55:17 +0200 | [diff] [blame] | 1409 | conn_warn(tconn, "Allocation of an epoch failed, slowing down\n"); |
Philipp Reisner | 2451fc3 | 2010-08-24 13:43:11 +0200 | [diff] [blame] | 1410 | /* Fall through */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1411 | |
| 1412 | case WO_bdev_flush: |
| 1413 | case WO_drain_io: |
Philipp Reisner | 77fede5 | 2011-11-10 21:19:11 +0100 | [diff] [blame] | 1414 | conn_wait_active_ee_empty(tconn); |
Philipp Reisner | 4b0007c | 2011-11-09 20:12:34 +0100 | [diff] [blame] | 1415 | drbd_flush(tconn); |
Philipp Reisner | 2451fc3 | 2010-08-24 13:43:11 +0200 | [diff] [blame] | 1416 | |
Philipp Reisner | 12038a3 | 2011-11-09 19:18:00 +0100 | [diff] [blame] | 1417 | if (atomic_read(&tconn->current_epoch->epoch_size)) { |
Philipp Reisner | 2451fc3 | 2010-08-24 13:43:11 +0200 | [diff] [blame] | 1418 | epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO); |
| 1419 | if (epoch) |
| 1420 | break; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1421 | } |
| 1422 | |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 1423 | return 0; |
Philipp Reisner | 2451fc3 | 2010-08-24 13:43:11 +0200 | [diff] [blame] | 1424 | default: |
Lars Ellenberg | 9ed57dc | 2012-03-26 20:55:17 +0200 | [diff] [blame] | 1425 | conn_err(tconn, "Strangeness in tconn->write_ordering %d\n", tconn->write_ordering); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 1426 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | epoch->flags = 0; |
| 1430 | atomic_set(&epoch->epoch_size, 0); |
| 1431 | atomic_set(&epoch->active, 0); |
| 1432 | |
Philipp Reisner | 12038a3 | 2011-11-09 19:18:00 +0100 | [diff] [blame] | 1433 | spin_lock(&tconn->epoch_lock); |
| 1434 | if (atomic_read(&tconn->current_epoch->epoch_size)) { |
| 1435 | list_add(&epoch->list, &tconn->current_epoch->list); |
| 1436 | tconn->current_epoch = epoch; |
| 1437 | tconn->epochs++; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1438 | } else { |
| 1439 | /* The current_epoch got recycled while we allocated this one... */ |
| 1440 | kfree(epoch); |
| 1441 | } |
Philipp Reisner | 12038a3 | 2011-11-09 19:18:00 +0100 | [diff] [blame] | 1442 | spin_unlock(&tconn->epoch_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1443 | |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 1444 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | /* used from receive_RSDataReply (recv_resync_read) |
| 1448 | * and from receive_Data */ |
Andreas Gruenbacher | f6ffca9 | 2011-02-04 15:30:34 +0100 | [diff] [blame] | 1449 | static struct drbd_peer_request * |
| 1450 | read_in_block(struct drbd_conf *mdev, u64 id, sector_t sector, |
| 1451 | int data_size) __must_hold(local) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1452 | { |
Lars Ellenberg | 6666032 | 2010-04-06 12:15:04 +0200 | [diff] [blame] | 1453 | const sector_t capacity = drbd_get_capacity(mdev->this_bdev); |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1454 | struct drbd_peer_request *peer_req; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1455 | struct page *page; |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 1456 | int dgs, ds, err; |
Philipp Reisner | a063845 | 2011-01-19 14:31:32 +0100 | [diff] [blame] | 1457 | void *dig_in = mdev->tconn->int_dig_in; |
| 1458 | void *dig_vv = mdev->tconn->int_dig_vv; |
Philipp Reisner | 6b4388a | 2010-04-26 14:11:45 +0200 | [diff] [blame] | 1459 | unsigned long *data; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1460 | |
Andreas Gruenbacher | 88104ca | 2011-04-28 21:47:21 +0200 | [diff] [blame] | 1461 | dgs = 0; |
| 1462 | if (mdev->tconn->peer_integrity_tfm) { |
| 1463 | dgs = crypto_hash_digestsize(mdev->tconn->peer_integrity_tfm); |
Andreas Gruenbacher | 9f5bdc3 | 2011-03-28 14:23:08 +0200 | [diff] [blame] | 1464 | /* |
| 1465 | * FIXME: Receive the incoming digest into the receive buffer |
| 1466 | * here, together with its struct p_data? |
| 1467 | */ |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 1468 | err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs); |
| 1469 | if (err) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1470 | return NULL; |
Andreas Gruenbacher | 88104ca | 2011-04-28 21:47:21 +0200 | [diff] [blame] | 1471 | data_size -= dgs; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1472 | } |
| 1473 | |
Andreas Gruenbacher | 841ce24 | 2010-12-15 19:31:20 +0100 | [diff] [blame] | 1474 | if (!expect(data_size != 0)) |
| 1475 | return NULL; |
| 1476 | if (!expect(IS_ALIGNED(data_size, 512))) |
| 1477 | return NULL; |
| 1478 | if (!expect(data_size <= DRBD_MAX_BIO_SIZE)) |
| 1479 | return NULL; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1480 | |
Lars Ellenberg | 6666032 | 2010-04-06 12:15:04 +0200 | [diff] [blame] | 1481 | /* even though we trust out peer, |
| 1482 | * we sometimes have to double check. */ |
| 1483 | if (sector + (data_size>>9) > capacity) { |
Lars Ellenberg | fdda654 | 2011-01-24 15:11:01 +0100 | [diff] [blame] | 1484 | dev_err(DEV, "request from peer beyond end of local disk: " |
| 1485 | "capacity: %llus < sector: %llus + size: %u\n", |
Lars Ellenberg | 6666032 | 2010-04-06 12:15:04 +0200 | [diff] [blame] | 1486 | (unsigned long long)capacity, |
| 1487 | (unsigned long long)sector, data_size); |
| 1488 | return NULL; |
| 1489 | } |
| 1490 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1491 | /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD |
| 1492 | * "criss-cross" setup, that might cause write-out on some other DRBD, |
| 1493 | * which in turn might block on the other node at this very place. */ |
Andreas Gruenbacher | 0db5536 | 2011-04-06 16:09:15 +0200 | [diff] [blame] | 1494 | peer_req = drbd_alloc_peer_req(mdev, id, sector, data_size, GFP_NOIO); |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1495 | if (!peer_req) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1496 | return NULL; |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1497 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1498 | ds = data_size; |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1499 | page = peer_req->pages; |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1500 | page_chain_for_each(page) { |
| 1501 | unsigned len = min_t(int, ds, PAGE_SIZE); |
Philipp Reisner | 6b4388a | 2010-04-26 14:11:45 +0200 | [diff] [blame] | 1502 | data = kmap(page); |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 1503 | err = drbd_recv_all_warn(mdev->tconn, data, len); |
Andreas Gruenbacher | 0cf9d27 | 2010-12-07 10:43:29 +0100 | [diff] [blame] | 1504 | if (drbd_insert_fault(mdev, DRBD_FAULT_RECEIVE)) { |
Philipp Reisner | 6b4388a | 2010-04-26 14:11:45 +0200 | [diff] [blame] | 1505 | dev_err(DEV, "Fault injection: Corrupting data on receive\n"); |
| 1506 | data[0] = data[0] ^ (unsigned long)-1; |
| 1507 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1508 | kunmap(page); |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 1509 | if (err) { |
Andreas Gruenbacher | 3967deb | 2011-04-06 16:16:56 +0200 | [diff] [blame] | 1510 | drbd_free_peer_req(mdev, peer_req); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1511 | return NULL; |
| 1512 | } |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 1513 | ds -= len; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1514 | } |
| 1515 | |
| 1516 | if (dgs) { |
Andreas Gruenbacher | 5b614ab | 2011-04-27 21:00:12 +0200 | [diff] [blame] | 1517 | drbd_csum_ee(mdev, mdev->tconn->peer_integrity_tfm, peer_req, dig_vv); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1518 | if (memcmp(dig_in, dig_vv, dgs)) { |
Lars Ellenberg | 470be44 | 2010-11-10 10:36:52 +0100 | [diff] [blame] | 1519 | dev_err(DEV, "Digest integrity check FAILED: %llus +%u\n", |
| 1520 | (unsigned long long)sector, data_size); |
Andreas Gruenbacher | 3967deb | 2011-04-06 16:16:56 +0200 | [diff] [blame] | 1521 | drbd_free_peer_req(mdev, peer_req); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1522 | return NULL; |
| 1523 | } |
| 1524 | } |
| 1525 | mdev->recv_cnt += data_size>>9; |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1526 | return peer_req; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | /* drbd_drain_block() just takes a data block |
| 1530 | * out of the socket input buffer, and discards it. |
| 1531 | */ |
| 1532 | static int drbd_drain_block(struct drbd_conf *mdev, int data_size) |
| 1533 | { |
| 1534 | struct page *page; |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 1535 | int err = 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1536 | void *data; |
| 1537 | |
Lars Ellenberg | c3470cd | 2010-04-01 16:57:19 +0200 | [diff] [blame] | 1538 | if (!data_size) |
Andreas Gruenbacher | fc5be83 | 2011-03-16 17:50:50 +0100 | [diff] [blame] | 1539 | return 0; |
Lars Ellenberg | c3470cd | 2010-04-01 16:57:19 +0200 | [diff] [blame] | 1540 | |
Andreas Gruenbacher | c37c8ec | 2011-04-07 21:02:09 +0200 | [diff] [blame] | 1541 | page = drbd_alloc_pages(mdev, 1, 1); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1542 | |
| 1543 | data = kmap(page); |
| 1544 | while (data_size) { |
Andreas Gruenbacher | fc5be83 | 2011-03-16 17:50:50 +0100 | [diff] [blame] | 1545 | unsigned int len = min_t(int, data_size, PAGE_SIZE); |
| 1546 | |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 1547 | err = drbd_recv_all_warn(mdev->tconn, data, len); |
| 1548 | if (err) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1549 | break; |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 1550 | data_size -= len; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1551 | } |
| 1552 | kunmap(page); |
Andreas Gruenbacher | 5cc287e | 2011-04-07 21:02:59 +0200 | [diff] [blame] | 1553 | drbd_free_pages(mdev, page, 0); |
Andreas Gruenbacher | fc5be83 | 2011-03-16 17:50:50 +0100 | [diff] [blame] | 1554 | return err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | static int recv_dless_read(struct drbd_conf *mdev, struct drbd_request *req, |
| 1558 | sector_t sector, int data_size) |
| 1559 | { |
| 1560 | struct bio_vec *bvec; |
| 1561 | struct bio *bio; |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 1562 | int dgs, err, i, expect; |
Philipp Reisner | a063845 | 2011-01-19 14:31:32 +0100 | [diff] [blame] | 1563 | void *dig_in = mdev->tconn->int_dig_in; |
| 1564 | void *dig_vv = mdev->tconn->int_dig_vv; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1565 | |
Andreas Gruenbacher | 88104ca | 2011-04-28 21:47:21 +0200 | [diff] [blame] | 1566 | dgs = 0; |
| 1567 | if (mdev->tconn->peer_integrity_tfm) { |
| 1568 | dgs = crypto_hash_digestsize(mdev->tconn->peer_integrity_tfm); |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 1569 | err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs); |
| 1570 | if (err) |
| 1571 | return err; |
Andreas Gruenbacher | 88104ca | 2011-04-28 21:47:21 +0200 | [diff] [blame] | 1572 | data_size -= dgs; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1573 | } |
| 1574 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1575 | /* optimistically update recv_cnt. if receiving fails below, |
| 1576 | * we disconnect anyways, and counters will be reset. */ |
| 1577 | mdev->recv_cnt += data_size>>9; |
| 1578 | |
| 1579 | bio = req->master_bio; |
| 1580 | D_ASSERT(sector == bio->bi_sector); |
| 1581 | |
| 1582 | bio_for_each_segment(bvec, bio, i) { |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 1583 | void *mapped = kmap(bvec->bv_page) + bvec->bv_offset; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1584 | expect = min_t(int, data_size, bvec->bv_len); |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 1585 | err = drbd_recv_all_warn(mdev->tconn, mapped, expect); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1586 | kunmap(bvec->bv_page); |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 1587 | if (err) |
| 1588 | return err; |
| 1589 | data_size -= expect; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1590 | } |
| 1591 | |
| 1592 | if (dgs) { |
Andreas Gruenbacher | 5b614ab | 2011-04-27 21:00:12 +0200 | [diff] [blame] | 1593 | drbd_csum_bio(mdev, mdev->tconn->peer_integrity_tfm, bio, dig_vv); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1594 | if (memcmp(dig_in, dig_vv, dgs)) { |
| 1595 | dev_err(DEV, "Digest integrity check FAILED. Broken NICs?\n"); |
Andreas Gruenbacher | 28284ce | 2011-03-16 17:54:02 +0100 | [diff] [blame] | 1596 | return -EINVAL; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1597 | } |
| 1598 | } |
| 1599 | |
| 1600 | D_ASSERT(data_size == 0); |
Andreas Gruenbacher | 28284ce | 2011-03-16 17:54:02 +0100 | [diff] [blame] | 1601 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1602 | } |
| 1603 | |
Andreas Gruenbacher | a990be4 | 2011-04-06 17:56:48 +0200 | [diff] [blame] | 1604 | /* |
| 1605 | * e_end_resync_block() is called in asender context via |
| 1606 | * drbd_finish_peer_reqs(). |
| 1607 | */ |
Andreas Gruenbacher | 99920dc | 2011-03-16 15:31:39 +0100 | [diff] [blame] | 1608 | static int e_end_resync_block(struct drbd_work *w, int unused) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1609 | { |
Andreas Gruenbacher | 8050e6d | 2011-02-18 16:12:48 +0100 | [diff] [blame] | 1610 | struct drbd_peer_request *peer_req = |
| 1611 | container_of(w, struct drbd_peer_request, w); |
Philipp Reisner | 00d5694 | 2011-02-09 18:09:48 +0100 | [diff] [blame] | 1612 | struct drbd_conf *mdev = w->mdev; |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1613 | sector_t sector = peer_req->i.sector; |
Andreas Gruenbacher | 99920dc | 2011-03-16 15:31:39 +0100 | [diff] [blame] | 1614 | int err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1615 | |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1616 | D_ASSERT(drbd_interval_empty(&peer_req->i)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1617 | |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1618 | if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) { |
| 1619 | drbd_set_in_sync(mdev, sector, peer_req->i.size); |
Andreas Gruenbacher | 99920dc | 2011-03-16 15:31:39 +0100 | [diff] [blame] | 1620 | err = drbd_send_ack(mdev, P_RS_WRITE_ACK, peer_req); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1621 | } else { |
| 1622 | /* Record failure to sync */ |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1623 | drbd_rs_failed_io(mdev, sector, peer_req->i.size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1624 | |
Andreas Gruenbacher | 99920dc | 2011-03-16 15:31:39 +0100 | [diff] [blame] | 1625 | err = drbd_send_ack(mdev, P_NEG_ACK, peer_req); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1626 | } |
| 1627 | dec_unacked(mdev); |
| 1628 | |
Andreas Gruenbacher | 99920dc | 2011-03-16 15:31:39 +0100 | [diff] [blame] | 1629 | return err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | static int recv_resync_read(struct drbd_conf *mdev, sector_t sector, int data_size) __releases(local) |
| 1633 | { |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1634 | struct drbd_peer_request *peer_req; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1635 | |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1636 | peer_req = read_in_block(mdev, ID_SYNCER, sector, data_size); |
| 1637 | if (!peer_req) |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1638 | goto fail; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1639 | |
| 1640 | dec_rs_pending(mdev); |
| 1641 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1642 | inc_unacked(mdev); |
| 1643 | /* corresponding dec_unacked() in e_end_resync_block() |
| 1644 | * respective _drbd_clear_done_ee */ |
| 1645 | |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1646 | peer_req->w.cb = e_end_resync_block; |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1647 | |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 1648 | spin_lock_irq(&mdev->tconn->req_lock); |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1649 | list_add(&peer_req->w.list, &mdev->sync_ee); |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 1650 | spin_unlock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1651 | |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 1652 | atomic_add(data_size >> 9, &mdev->rs_sect_ev); |
Andreas Gruenbacher | fbe29de | 2011-02-17 16:38:35 +0100 | [diff] [blame] | 1653 | if (drbd_submit_peer_request(mdev, peer_req, WRITE, DRBD_FAULT_RS_WR) == 0) |
Andreas Gruenbacher | e1c1b0f | 2011-03-16 17:58:27 +0100 | [diff] [blame] | 1654 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1655 | |
Lars Ellenberg | 10f6d992 | 2011-01-24 14:47:09 +0100 | [diff] [blame] | 1656 | /* don't care for the reason here */ |
| 1657 | dev_err(DEV, "submit failed, triggering re-connect\n"); |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 1658 | spin_lock_irq(&mdev->tconn->req_lock); |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1659 | list_del(&peer_req->w.list); |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 1660 | spin_unlock_irq(&mdev->tconn->req_lock); |
Lars Ellenberg | 22cc37a | 2010-09-14 20:40:41 +0200 | [diff] [blame] | 1661 | |
Andreas Gruenbacher | 3967deb | 2011-04-06 16:16:56 +0200 | [diff] [blame] | 1662 | drbd_free_peer_req(mdev, peer_req); |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1663 | fail: |
| 1664 | put_ldev(mdev); |
Andreas Gruenbacher | e1c1b0f | 2011-03-16 17:58:27 +0100 | [diff] [blame] | 1665 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1666 | } |
| 1667 | |
Andreas Gruenbacher | 668eebc | 2011-01-20 17:14:26 +0100 | [diff] [blame] | 1668 | static struct drbd_request * |
Andreas Gruenbacher | bc9c5c4 | 2011-01-21 18:00:55 +0100 | [diff] [blame] | 1669 | find_request(struct drbd_conf *mdev, struct rb_root *root, u64 id, |
| 1670 | sector_t sector, bool missing_ok, const char *func) |
Andreas Gruenbacher | 668eebc | 2011-01-20 17:14:26 +0100 | [diff] [blame] | 1671 | { |
Andreas Gruenbacher | 668eebc | 2011-01-20 17:14:26 +0100 | [diff] [blame] | 1672 | struct drbd_request *req; |
| 1673 | |
Andreas Gruenbacher | bc9c5c4 | 2011-01-21 18:00:55 +0100 | [diff] [blame] | 1674 | /* Request object according to our peer */ |
| 1675 | req = (struct drbd_request *)(unsigned long)id; |
Andreas Gruenbacher | 5e47226 | 2011-01-27 14:42:51 +0100 | [diff] [blame] | 1676 | if (drbd_contains_interval(root, sector, &req->i) && req->i.local) |
Andreas Gruenbacher | 668eebc | 2011-01-20 17:14:26 +0100 | [diff] [blame] | 1677 | return req; |
Andreas Gruenbacher | c3afd8f | 2011-01-20 22:25:40 +0100 | [diff] [blame] | 1678 | if (!missing_ok) { |
Andreas Gruenbacher | 5af172e | 2011-07-15 09:43:23 +0200 | [diff] [blame] | 1679 | dev_err(DEV, "%s: failed to find request 0x%lx, sector %llus\n", func, |
Andreas Gruenbacher | c3afd8f | 2011-01-20 22:25:40 +0100 | [diff] [blame] | 1680 | (unsigned long)id, (unsigned long long)sector); |
| 1681 | } |
Andreas Gruenbacher | 668eebc | 2011-01-20 17:14:26 +0100 | [diff] [blame] | 1682 | return NULL; |
| 1683 | } |
| 1684 | |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 1685 | static int receive_DataReply(struct drbd_tconn *tconn, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1686 | { |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 1687 | struct drbd_conf *mdev; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1688 | struct drbd_request *req; |
| 1689 | sector_t sector; |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 1690 | int err; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 1691 | struct p_data *p = pi->data; |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 1692 | |
| 1693 | mdev = vnr_to_mdev(tconn, pi->vnr); |
| 1694 | if (!mdev) |
| 1695 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1696 | |
| 1697 | sector = be64_to_cpu(p->sector); |
| 1698 | |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 1699 | spin_lock_irq(&mdev->tconn->req_lock); |
Andreas Gruenbacher | bc9c5c4 | 2011-01-21 18:00:55 +0100 | [diff] [blame] | 1700 | 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] | 1701 | spin_unlock_irq(&mdev->tconn->req_lock); |
Andreas Gruenbacher | c3afd8f | 2011-01-20 22:25:40 +0100 | [diff] [blame] | 1702 | if (unlikely(!req)) |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 1703 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1704 | |
Bart Van Assche | 24c4830 | 2011-05-21 18:32:29 +0200 | [diff] [blame] | 1705 | /* 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] | 1706 | * special casing it there for the various failure cases. |
| 1707 | * still no race with drbd_fail_pending_reads */ |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 1708 | err = recv_dless_read(mdev, req, sector, pi->size); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 1709 | if (!err) |
Andreas Gruenbacher | 8554df1 | 2011-01-25 15:37:43 +0100 | [diff] [blame] | 1710 | req_mod(req, DATA_RECEIVED); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1711 | /* else: nothing. handled from drbd_disconnect... |
| 1712 | * I don't think we may complete this just yet |
| 1713 | * in case we are "on-disconnect: freeze" */ |
| 1714 | |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 1715 | return err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1716 | } |
| 1717 | |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 1718 | static int receive_RSDataReply(struct drbd_tconn *tconn, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1719 | { |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 1720 | struct drbd_conf *mdev; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1721 | sector_t sector; |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 1722 | int err; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 1723 | struct p_data *p = pi->data; |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 1724 | |
| 1725 | mdev = vnr_to_mdev(tconn, pi->vnr); |
| 1726 | if (!mdev) |
| 1727 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1728 | |
| 1729 | sector = be64_to_cpu(p->sector); |
| 1730 | D_ASSERT(p->block_id == ID_SYNCER); |
| 1731 | |
| 1732 | if (get_ldev(mdev)) { |
| 1733 | /* data is submitted to disk within recv_resync_read. |
| 1734 | * corresponding put_ldev done below on error, |
Andreas Gruenbacher | fcefa62 | 2011-02-17 16:46:59 +0100 | [diff] [blame] | 1735 | * or in drbd_peer_request_endio. */ |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 1736 | err = recv_resync_read(mdev, sector, pi->size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1737 | } else { |
| 1738 | if (__ratelimit(&drbd_ratelimit_state)) |
| 1739 | dev_err(DEV, "Can not write resync data to local disk.\n"); |
| 1740 | |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 1741 | err = drbd_drain_block(mdev, pi->size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1742 | |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 1743 | drbd_send_ack_dp(mdev, P_NEG_ACK, p, pi->size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1744 | } |
| 1745 | |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 1746 | atomic_add(pi->size >> 9, &mdev->rs_sect_in); |
Philipp Reisner | 778f271 | 2010-07-06 11:14:00 +0200 | [diff] [blame] | 1747 | |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 1748 | return err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1749 | } |
| 1750 | |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 1751 | static void restart_conflicting_writes(struct drbd_conf *mdev, |
| 1752 | sector_t sector, int size) |
| 1753 | { |
| 1754 | struct drbd_interval *i; |
| 1755 | struct drbd_request *req; |
| 1756 | |
| 1757 | drbd_for_each_overlap(i, &mdev->write_requests, sector, size) { |
| 1758 | if (!i->local) |
| 1759 | continue; |
| 1760 | req = container_of(i, struct drbd_request, i); |
| 1761 | if (req->rq_state & RQ_LOCAL_PENDING || |
| 1762 | !(req->rq_state & RQ_POSTPONED)) |
| 1763 | continue; |
Lars Ellenberg | 2312f0b3 | 2011-11-24 10:36:25 +0100 | [diff] [blame] | 1764 | /* as it is RQ_POSTPONED, this will cause it to |
| 1765 | * be queued on the retry workqueue. */ |
| 1766 | __req_mod(req, DISCARD_WRITE, NULL); |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 1767 | } |
| 1768 | } |
| 1769 | |
Andreas Gruenbacher | a990be4 | 2011-04-06 17:56:48 +0200 | [diff] [blame] | 1770 | /* |
| 1771 | * e_end_block() is called in asender context via drbd_finish_peer_reqs(). |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1772 | */ |
Andreas Gruenbacher | 99920dc | 2011-03-16 15:31:39 +0100 | [diff] [blame] | 1773 | static int e_end_block(struct drbd_work *w, int cancel) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1774 | { |
Andreas Gruenbacher | 8050e6d | 2011-02-18 16:12:48 +0100 | [diff] [blame] | 1775 | struct drbd_peer_request *peer_req = |
| 1776 | container_of(w, struct drbd_peer_request, w); |
Philipp Reisner | 00d5694 | 2011-02-09 18:09:48 +0100 | [diff] [blame] | 1777 | struct drbd_conf *mdev = w->mdev; |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1778 | sector_t sector = peer_req->i.sector; |
Andreas Gruenbacher | 99920dc | 2011-03-16 15:31:39 +0100 | [diff] [blame] | 1779 | int err = 0, pcmd; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1780 | |
Philipp Reisner | 303d144 | 2011-04-13 16:24:47 -0700 | [diff] [blame] | 1781 | if (peer_req->flags & EE_SEND_WRITE_ACK) { |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1782 | if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1783 | pcmd = (mdev->state.conn >= C_SYNC_SOURCE && |
| 1784 | mdev->state.conn <= C_PAUSED_SYNC_T && |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1785 | peer_req->flags & EE_MAY_SET_IN_SYNC) ? |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1786 | P_RS_WRITE_ACK : P_WRITE_ACK; |
Andreas Gruenbacher | 99920dc | 2011-03-16 15:31:39 +0100 | [diff] [blame] | 1787 | err = drbd_send_ack(mdev, pcmd, peer_req); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1788 | if (pcmd == P_RS_WRITE_ACK) |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1789 | drbd_set_in_sync(mdev, sector, peer_req->i.size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1790 | } else { |
Andreas Gruenbacher | 99920dc | 2011-03-16 15:31:39 +0100 | [diff] [blame] | 1791 | err = drbd_send_ack(mdev, P_NEG_ACK, peer_req); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1792 | /* we expect it to be marked out of sync anyways... |
| 1793 | * maybe assert this? */ |
| 1794 | } |
| 1795 | dec_unacked(mdev); |
| 1796 | } |
| 1797 | /* we delete from the conflict detection hash _after_ we sent out the |
| 1798 | * P_WRITE_ACK / P_NEG_ACK, to get the sequence number right. */ |
Philipp Reisner | 302bdea | 2011-04-21 11:36:49 +0200 | [diff] [blame] | 1799 | if (peer_req->flags & EE_IN_INTERVAL_TREE) { |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 1800 | spin_lock_irq(&mdev->tconn->req_lock); |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1801 | D_ASSERT(!drbd_interval_empty(&peer_req->i)); |
| 1802 | drbd_remove_epoch_entry_interval(mdev, peer_req); |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 1803 | if (peer_req->flags & EE_RESTART_REQUESTS) |
| 1804 | restart_conflicting_writes(mdev, sector, peer_req->i.size); |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 1805 | spin_unlock_irq(&mdev->tconn->req_lock); |
Andreas Gruenbacher | bb3bfe9 | 2011-01-21 15:59:23 +0100 | [diff] [blame] | 1806 | } else |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1807 | D_ASSERT(drbd_interval_empty(&peer_req->i)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1808 | |
Philipp Reisner | 1e9dd29 | 2011-11-10 15:14:53 +0100 | [diff] [blame] | 1809 | drbd_may_finish_epoch(mdev->tconn, peer_req->epoch, EV_PUT + (cancel ? EV_CLEANUP : 0)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1810 | |
Andreas Gruenbacher | 99920dc | 2011-03-16 15:31:39 +0100 | [diff] [blame] | 1811 | return err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1812 | } |
| 1813 | |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 1814 | 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] | 1815 | { |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 1816 | struct drbd_conf *mdev = w->mdev; |
Andreas Gruenbacher | 8050e6d | 2011-02-18 16:12:48 +0100 | [diff] [blame] | 1817 | struct drbd_peer_request *peer_req = |
| 1818 | container_of(w, struct drbd_peer_request, w); |
Andreas Gruenbacher | 99920dc | 2011-03-16 15:31:39 +0100 | [diff] [blame] | 1819 | int err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1820 | |
Andreas Gruenbacher | 99920dc | 2011-03-16 15:31:39 +0100 | [diff] [blame] | 1821 | err = drbd_send_ack(mdev, ack, peer_req); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1822 | dec_unacked(mdev); |
| 1823 | |
Andreas Gruenbacher | 99920dc | 2011-03-16 15:31:39 +0100 | [diff] [blame] | 1824 | return err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1825 | } |
| 1826 | |
Andreas Gruenbacher | 99920dc | 2011-03-16 15:31:39 +0100 | [diff] [blame] | 1827 | static int e_send_discard_write(struct drbd_work *w, int unused) |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 1828 | { |
| 1829 | return e_send_ack(w, P_DISCARD_WRITE); |
| 1830 | } |
| 1831 | |
Andreas Gruenbacher | 99920dc | 2011-03-16 15:31:39 +0100 | [diff] [blame] | 1832 | static int e_send_retry_write(struct drbd_work *w, int unused) |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 1833 | { |
| 1834 | struct drbd_tconn *tconn = w->mdev->tconn; |
| 1835 | |
| 1836 | return e_send_ack(w, tconn->agreed_pro_version >= 100 ? |
| 1837 | P_RETRY_WRITE : P_DISCARD_WRITE); |
| 1838 | } |
| 1839 | |
Andreas Gruenbacher | 3e394da | 2011-01-26 18:36:55 +0100 | [diff] [blame] | 1840 | static bool seq_greater(u32 a, u32 b) |
| 1841 | { |
| 1842 | /* |
| 1843 | * We assume 32-bit wrap-around here. |
| 1844 | * For 24-bit wrap-around, we would have to shift: |
| 1845 | * a <<= 8; b <<= 8; |
| 1846 | */ |
| 1847 | return (s32)a - (s32)b > 0; |
| 1848 | } |
| 1849 | |
| 1850 | static u32 seq_max(u32 a, u32 b) |
| 1851 | { |
| 1852 | return seq_greater(a, b) ? a : b; |
| 1853 | } |
| 1854 | |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 1855 | static bool need_peer_seq(struct drbd_conf *mdev) |
| 1856 | { |
| 1857 | struct drbd_tconn *tconn = mdev->tconn; |
Philipp Reisner | 302bdea | 2011-04-21 11:36:49 +0200 | [diff] [blame] | 1858 | int tp; |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 1859 | |
| 1860 | /* |
| 1861 | * We only need to keep track of the last packet_seq number of our peer |
| 1862 | * if we are in dual-primary mode and we have the discard flag set; see |
| 1863 | * handle_write_conflicts(). |
| 1864 | */ |
Philipp Reisner | 302bdea | 2011-04-21 11:36:49 +0200 | [diff] [blame] | 1865 | |
| 1866 | rcu_read_lock(); |
| 1867 | tp = rcu_dereference(mdev->tconn->net_conf)->two_primaries; |
| 1868 | rcu_read_unlock(); |
| 1869 | |
| 1870 | return tp && test_bit(DISCARD_CONCURRENT, &tconn->flags); |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 1871 | } |
| 1872 | |
Andreas Gruenbacher | 43ae077 | 2011-02-03 18:42:08 +0100 | [diff] [blame] | 1873 | 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] | 1874 | { |
Lars Ellenberg | 3c13b68 | 2011-02-23 16:10:01 +0100 | [diff] [blame] | 1875 | unsigned int newest_peer_seq; |
Andreas Gruenbacher | 3e394da | 2011-01-26 18:36:55 +0100 | [diff] [blame] | 1876 | |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 1877 | if (need_peer_seq(mdev)) { |
| 1878 | spin_lock(&mdev->peer_seq_lock); |
Lars Ellenberg | 3c13b68 | 2011-02-23 16:10:01 +0100 | [diff] [blame] | 1879 | newest_peer_seq = seq_max(mdev->peer_seq, peer_seq); |
| 1880 | mdev->peer_seq = newest_peer_seq; |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 1881 | spin_unlock(&mdev->peer_seq_lock); |
Lars Ellenberg | 3c13b68 | 2011-02-23 16:10:01 +0100 | [diff] [blame] | 1882 | /* wake up only if we actually changed mdev->peer_seq */ |
| 1883 | if (peer_seq == newest_peer_seq) |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 1884 | wake_up(&mdev->seq_wait); |
| 1885 | } |
Andreas Gruenbacher | 3e394da | 2011-01-26 18:36:55 +0100 | [diff] [blame] | 1886 | } |
| 1887 | |
Lars Ellenberg | d93f630 | 2012-03-26 15:49:13 +0200 | [diff] [blame] | 1888 | static inline int overlaps(sector_t s1, int l1, sector_t s2, int l2) |
| 1889 | { |
| 1890 | return !((s1 + (l1>>9) <= s2) || (s1 >= s2 + (l2>>9))); |
| 1891 | } |
| 1892 | |
| 1893 | /* maybe change sync_ee into interval trees as well? */ |
Philipp Reisner | 3ea35df | 2012-04-06 12:13:18 +0200 | [diff] [blame] | 1894 | static bool overlapping_resync_write(struct drbd_conf *mdev, struct drbd_peer_request *peer_req) |
Lars Ellenberg | d93f630 | 2012-03-26 15:49:13 +0200 | [diff] [blame] | 1895 | { |
| 1896 | struct drbd_peer_request *rs_req; |
| 1897 | bool rv = 0; |
| 1898 | |
| 1899 | spin_lock_irq(&mdev->tconn->req_lock); |
| 1900 | list_for_each_entry(rs_req, &mdev->sync_ee, w.list) { |
| 1901 | if (overlaps(peer_req->i.sector, peer_req->i.size, |
| 1902 | rs_req->i.sector, rs_req->i.size)) { |
| 1903 | rv = 1; |
| 1904 | break; |
| 1905 | } |
| 1906 | } |
| 1907 | spin_unlock_irq(&mdev->tconn->req_lock); |
| 1908 | |
Lars Ellenberg | d93f630 | 2012-03-26 15:49:13 +0200 | [diff] [blame] | 1909 | return rv; |
| 1910 | } |
| 1911 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1912 | /* Called from receive_Data. |
| 1913 | * Synchronize packets on sock with packets on msock. |
| 1914 | * |
| 1915 | * This is here so even when a P_DATA packet traveling via sock overtook an Ack |
| 1916 | * packet traveling on msock, they are still processed in the order they have |
| 1917 | * been sent. |
| 1918 | * |
| 1919 | * Note: we don't care for Ack packets overtaking P_DATA packets. |
| 1920 | * |
| 1921 | * In case packet_seq is larger than mdev->peer_seq number, there are |
| 1922 | * outstanding packets on the msock. We wait for them to arrive. |
| 1923 | * In case we are the logically next packet, we update mdev->peer_seq |
| 1924 | * ourselves. Correctly handles 32bit wrap around. |
| 1925 | * |
| 1926 | * Assume we have a 10 GBit connection, that is about 1<<30 byte per second, |
| 1927 | * about 1<<21 sectors per second. So "worst" case, we have 1<<3 == 8 seconds |
| 1928 | * for the 24bit wrap (historical atomic_t guarantee on some archs), and we have |
| 1929 | * 1<<9 == 512 seconds aka ages for the 32bit wrap around... |
| 1930 | * |
| 1931 | * returns 0 if we may process the packet, |
| 1932 | * -ERESTARTSYS if we were interrupted (by disconnect signal). */ |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 1933 | 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] | 1934 | { |
| 1935 | DEFINE_WAIT(wait); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1936 | long timeout; |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 1937 | int ret; |
| 1938 | |
| 1939 | if (!need_peer_seq(mdev)) |
| 1940 | return 0; |
| 1941 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1942 | spin_lock(&mdev->peer_seq_lock); |
| 1943 | for (;;) { |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 1944 | if (!seq_greater(peer_seq - 1, mdev->peer_seq)) { |
| 1945 | mdev->peer_seq = seq_max(mdev->peer_seq, peer_seq); |
| 1946 | ret = 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1947 | break; |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 1948 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1949 | if (signal_pending(current)) { |
| 1950 | ret = -ERESTARTSYS; |
| 1951 | break; |
| 1952 | } |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 1953 | prepare_to_wait(&mdev->seq_wait, &wait, TASK_INTERRUPTIBLE); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1954 | spin_unlock(&mdev->peer_seq_lock); |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 1955 | rcu_read_lock(); |
| 1956 | timeout = rcu_dereference(mdev->tconn->net_conf)->ping_timeo*HZ/10; |
| 1957 | rcu_read_unlock(); |
Andreas Gruenbacher | 71b1c1e | 2011-03-01 15:40:43 +0100 | [diff] [blame] | 1958 | timeout = schedule_timeout(timeout); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1959 | spin_lock(&mdev->peer_seq_lock); |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 1960 | if (!timeout) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1961 | ret = -ETIMEDOUT; |
Andreas Gruenbacher | 71b1c1e | 2011-03-01 15:40:43 +0100 | [diff] [blame] | 1962 | dev_err(DEV, "Timed out waiting for missing ack packets; disconnecting\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1963 | break; |
| 1964 | } |
| 1965 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1966 | spin_unlock(&mdev->peer_seq_lock); |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 1967 | finish_wait(&mdev->seq_wait, &wait); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1968 | return ret; |
| 1969 | } |
| 1970 | |
Lars Ellenberg | 688593c | 2010-11-17 22:25:03 +0100 | [diff] [blame] | 1971 | /* see also bio_flags_to_wire() |
| 1972 | * DRBD_REQ_*, because we need to semantically map the flags to data packet |
| 1973 | * flags and back. We may replicate to other kernel versions. */ |
| 1974 | 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] | 1975 | { |
Lars Ellenberg | 688593c | 2010-11-17 22:25:03 +0100 | [diff] [blame] | 1976 | return (dpf & DP_RW_SYNC ? REQ_SYNC : 0) | |
| 1977 | (dpf & DP_FUA ? REQ_FUA : 0) | |
| 1978 | (dpf & DP_FLUSH ? REQ_FLUSH : 0) | |
| 1979 | (dpf & DP_DISCARD ? REQ_DISCARD : 0); |
Philipp Reisner | 76d2e7e | 2010-08-25 11:58:05 +0200 | [diff] [blame] | 1980 | } |
| 1981 | |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 1982 | static void fail_postponed_requests(struct drbd_conf *mdev, sector_t sector, |
| 1983 | unsigned int size) |
| 1984 | { |
| 1985 | struct drbd_interval *i; |
| 1986 | |
| 1987 | repeat: |
| 1988 | drbd_for_each_overlap(i, &mdev->write_requests, sector, size) { |
| 1989 | struct drbd_request *req; |
| 1990 | struct bio_and_error m; |
| 1991 | |
| 1992 | if (!i->local) |
| 1993 | continue; |
| 1994 | req = container_of(i, struct drbd_request, i); |
| 1995 | if (!(req->rq_state & RQ_POSTPONED)) |
| 1996 | continue; |
| 1997 | req->rq_state &= ~RQ_POSTPONED; |
| 1998 | __req_mod(req, NEG_ACKED, &m); |
| 1999 | spin_unlock_irq(&mdev->tconn->req_lock); |
| 2000 | if (m.bio) |
| 2001 | complete_master_bio(mdev, &m); |
| 2002 | spin_lock_irq(&mdev->tconn->req_lock); |
| 2003 | goto repeat; |
| 2004 | } |
| 2005 | } |
| 2006 | |
| 2007 | static int handle_write_conflicts(struct drbd_conf *mdev, |
| 2008 | struct drbd_peer_request *peer_req) |
| 2009 | { |
| 2010 | struct drbd_tconn *tconn = mdev->tconn; |
| 2011 | bool resolve_conflicts = test_bit(DISCARD_CONCURRENT, &tconn->flags); |
| 2012 | sector_t sector = peer_req->i.sector; |
| 2013 | const unsigned int size = peer_req->i.size; |
| 2014 | struct drbd_interval *i; |
| 2015 | bool equal; |
| 2016 | int err; |
| 2017 | |
| 2018 | /* |
| 2019 | * Inserting the peer request into the write_requests tree will prevent |
| 2020 | * new conflicting local requests from being added. |
| 2021 | */ |
| 2022 | drbd_insert_interval(&mdev->write_requests, &peer_req->i); |
| 2023 | |
| 2024 | repeat: |
| 2025 | drbd_for_each_overlap(i, &mdev->write_requests, sector, size) { |
| 2026 | if (i == &peer_req->i) |
| 2027 | continue; |
| 2028 | |
| 2029 | if (!i->local) { |
| 2030 | /* |
| 2031 | * Our peer has sent a conflicting remote request; this |
| 2032 | * should not happen in a two-node setup. Wait for the |
| 2033 | * earlier peer request to complete. |
| 2034 | */ |
| 2035 | err = drbd_wait_misc(mdev, i); |
| 2036 | if (err) |
| 2037 | goto out; |
| 2038 | goto repeat; |
| 2039 | } |
| 2040 | |
| 2041 | equal = i->sector == sector && i->size == size; |
| 2042 | if (resolve_conflicts) { |
| 2043 | /* |
| 2044 | * If the peer request is fully contained within the |
| 2045 | * overlapping request, it can be discarded; otherwise, |
| 2046 | * it will be retried once all overlapping requests |
| 2047 | * have completed. |
| 2048 | */ |
| 2049 | bool discard = i->sector <= sector && i->sector + |
| 2050 | (i->size >> 9) >= sector + (size >> 9); |
| 2051 | |
| 2052 | if (!equal) |
| 2053 | dev_alert(DEV, "Concurrent writes detected: " |
| 2054 | "local=%llus +%u, remote=%llus +%u, " |
| 2055 | "assuming %s came first\n", |
| 2056 | (unsigned long long)i->sector, i->size, |
| 2057 | (unsigned long long)sector, size, |
| 2058 | discard ? "local" : "remote"); |
| 2059 | |
| 2060 | inc_unacked(mdev); |
| 2061 | peer_req->w.cb = discard ? e_send_discard_write : |
| 2062 | e_send_retry_write; |
| 2063 | list_add_tail(&peer_req->w.list, &mdev->done_ee); |
| 2064 | wake_asender(mdev->tconn); |
| 2065 | |
| 2066 | err = -ENOENT; |
| 2067 | goto out; |
| 2068 | } else { |
| 2069 | struct drbd_request *req = |
| 2070 | container_of(i, struct drbd_request, i); |
| 2071 | |
| 2072 | if (!equal) |
| 2073 | dev_alert(DEV, "Concurrent writes detected: " |
| 2074 | "local=%llus +%u, remote=%llus +%u\n", |
| 2075 | (unsigned long long)i->sector, i->size, |
| 2076 | (unsigned long long)sector, size); |
| 2077 | |
| 2078 | if (req->rq_state & RQ_LOCAL_PENDING || |
| 2079 | !(req->rq_state & RQ_POSTPONED)) { |
| 2080 | /* |
| 2081 | * Wait for the node with the discard flag to |
| 2082 | * decide if this request will be discarded or |
| 2083 | * retried. Requests that are discarded will |
| 2084 | * disappear from the write_requests tree. |
| 2085 | * |
| 2086 | * In addition, wait for the conflicting |
| 2087 | * request to finish locally before submitting |
| 2088 | * the conflicting peer request. |
| 2089 | */ |
| 2090 | err = drbd_wait_misc(mdev, &req->i); |
| 2091 | if (err) { |
| 2092 | _conn_request_state(mdev->tconn, |
| 2093 | NS(conn, C_TIMEOUT), |
| 2094 | CS_HARD); |
| 2095 | fail_postponed_requests(mdev, sector, size); |
| 2096 | goto out; |
| 2097 | } |
| 2098 | goto repeat; |
| 2099 | } |
| 2100 | /* |
| 2101 | * Remember to restart the conflicting requests after |
| 2102 | * the new peer request has completed. |
| 2103 | */ |
| 2104 | peer_req->flags |= EE_RESTART_REQUESTS; |
| 2105 | } |
| 2106 | } |
| 2107 | err = 0; |
| 2108 | |
| 2109 | out: |
| 2110 | if (err) |
| 2111 | drbd_remove_epoch_entry_interval(mdev, peer_req); |
| 2112 | return err; |
| 2113 | } |
| 2114 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2115 | /* mirrored write */ |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 2116 | static int receive_Data(struct drbd_tconn *tconn, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2117 | { |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 2118 | struct drbd_conf *mdev; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2119 | sector_t sector; |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 2120 | struct drbd_peer_request *peer_req; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 2121 | struct p_data *p = pi->data; |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2122 | u32 peer_seq = be32_to_cpu(p->seq_num); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2123 | int rw = WRITE; |
| 2124 | u32 dp_flags; |
Philipp Reisner | 302bdea | 2011-04-21 11:36:49 +0200 | [diff] [blame] | 2125 | int err, tp; |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2126 | |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 2127 | mdev = vnr_to_mdev(tconn, pi->vnr); |
| 2128 | if (!mdev) |
| 2129 | return -EIO; |
| 2130 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2131 | if (!get_ldev(mdev)) { |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 2132 | int err2; |
| 2133 | |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2134 | err = wait_for_and_update_peer_seq(mdev, peer_seq); |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 2135 | drbd_send_ack_dp(mdev, P_NEG_ACK, p, pi->size); |
Philipp Reisner | 12038a3 | 2011-11-09 19:18:00 +0100 | [diff] [blame] | 2136 | atomic_inc(&tconn->current_epoch->epoch_size); |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 2137 | err2 = drbd_drain_block(mdev, pi->size); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 2138 | if (!err) |
| 2139 | err = err2; |
| 2140 | return err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2141 | } |
| 2142 | |
Andreas Gruenbacher | fcefa62 | 2011-02-17 16:46:59 +0100 | [diff] [blame] | 2143 | /* |
| 2144 | * Corresponding put_ldev done either below (on various errors), or in |
| 2145 | * drbd_peer_request_endio, if we successfully submit the data at the |
| 2146 | * end of this function. |
| 2147 | */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2148 | |
| 2149 | sector = be64_to_cpu(p->sector); |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 2150 | peer_req = read_in_block(mdev, p->block_id, sector, pi->size); |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 2151 | if (!peer_req) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2152 | put_ldev(mdev); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 2153 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2154 | } |
| 2155 | |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 2156 | peer_req->w.cb = e_end_block; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2157 | |
Lars Ellenberg | 688593c | 2010-11-17 22:25:03 +0100 | [diff] [blame] | 2158 | dp_flags = be32_to_cpu(p->dp_flags); |
| 2159 | rw |= wire_flags_to_bio(mdev, dp_flags); |
| 2160 | |
| 2161 | if (dp_flags & DP_MAY_SET_IN_SYNC) |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 2162 | peer_req->flags |= EE_MAY_SET_IN_SYNC; |
Lars Ellenberg | 688593c | 2010-11-17 22:25:03 +0100 | [diff] [blame] | 2163 | |
Philipp Reisner | 12038a3 | 2011-11-09 19:18:00 +0100 | [diff] [blame] | 2164 | spin_lock(&tconn->epoch_lock); |
| 2165 | peer_req->epoch = tconn->current_epoch; |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 2166 | atomic_inc(&peer_req->epoch->epoch_size); |
| 2167 | atomic_inc(&peer_req->epoch->active); |
Philipp Reisner | 12038a3 | 2011-11-09 19:18:00 +0100 | [diff] [blame] | 2168 | spin_unlock(&tconn->epoch_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2169 | |
Philipp Reisner | 302bdea | 2011-04-21 11:36:49 +0200 | [diff] [blame] | 2170 | rcu_read_lock(); |
| 2171 | tp = rcu_dereference(mdev->tconn->net_conf)->two_primaries; |
| 2172 | rcu_read_unlock(); |
| 2173 | if (tp) { |
| 2174 | peer_req->flags |= EE_IN_INTERVAL_TREE; |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2175 | err = wait_for_and_update_peer_seq(mdev, peer_seq); |
| 2176 | if (err) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2177 | goto out_interrupted; |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 2178 | spin_lock_irq(&mdev->tconn->req_lock); |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2179 | err = handle_write_conflicts(mdev, peer_req); |
| 2180 | if (err) { |
| 2181 | spin_unlock_irq(&mdev->tconn->req_lock); |
| 2182 | if (err == -ENOENT) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2183 | put_ldev(mdev); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 2184 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2185 | } |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2186 | goto out_interrupted; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2187 | } |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2188 | } else |
| 2189 | spin_lock_irq(&mdev->tconn->req_lock); |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 2190 | list_add(&peer_req->w.list, &mdev->active_ee); |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 2191 | spin_unlock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2192 | |
Lars Ellenberg | d93f630 | 2012-03-26 15:49:13 +0200 | [diff] [blame] | 2193 | if (mdev->state.conn == C_SYNC_TARGET) |
Philipp Reisner | 3ea35df | 2012-04-06 12:13:18 +0200 | [diff] [blame] | 2194 | wait_event(mdev->ee_wait, !overlapping_resync_write(mdev, peer_req)); |
Lars Ellenberg | d93f630 | 2012-03-26 15:49:13 +0200 | [diff] [blame] | 2195 | |
Philipp Reisner | 303d144 | 2011-04-13 16:24:47 -0700 | [diff] [blame] | 2196 | if (mdev->tconn->agreed_pro_version < 100) { |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 2197 | rcu_read_lock(); |
| 2198 | switch (rcu_dereference(mdev->tconn->net_conf)->wire_protocol) { |
Philipp Reisner | 303d144 | 2011-04-13 16:24:47 -0700 | [diff] [blame] | 2199 | case DRBD_PROT_C: |
| 2200 | dp_flags |= DP_SEND_WRITE_ACK; |
| 2201 | break; |
| 2202 | case DRBD_PROT_B: |
| 2203 | dp_flags |= DP_SEND_RECEIVE_ACK; |
| 2204 | break; |
| 2205 | } |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 2206 | rcu_read_unlock(); |
Philipp Reisner | 303d144 | 2011-04-13 16:24:47 -0700 | [diff] [blame] | 2207 | } |
| 2208 | |
| 2209 | if (dp_flags & DP_SEND_WRITE_ACK) { |
| 2210 | peer_req->flags |= EE_SEND_WRITE_ACK; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2211 | inc_unacked(mdev); |
| 2212 | /* corresponding dec_unacked() in e_end_block() |
| 2213 | * respective _drbd_clear_done_ee */ |
Philipp Reisner | 303d144 | 2011-04-13 16:24:47 -0700 | [diff] [blame] | 2214 | } |
| 2215 | |
| 2216 | if (dp_flags & DP_SEND_RECEIVE_ACK) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2217 | /* I really don't like it that the receiver thread |
| 2218 | * sends on the msock, but anyways */ |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 2219 | drbd_send_ack(mdev, P_RECV_ACK, peer_req); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2220 | } |
| 2221 | |
Lars Ellenberg | 6719fb0 | 2010-10-18 23:04:07 +0200 | [diff] [blame] | 2222 | if (mdev->state.pdsk < D_INCONSISTENT) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2223 | /* In case we have the only disk of the cluster, */ |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 2224 | drbd_set_out_of_sync(mdev, peer_req->i.sector, peer_req->i.size); |
| 2225 | peer_req->flags |= EE_CALL_AL_COMPLETE_IO; |
| 2226 | peer_req->flags &= ~EE_MAY_SET_IN_SYNC; |
Lars Ellenberg | 181286a | 2011-03-31 15:18:56 +0200 | [diff] [blame] | 2227 | drbd_al_begin_io(mdev, &peer_req->i); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2228 | } |
| 2229 | |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 2230 | err = drbd_submit_peer_request(mdev, peer_req, rw, DRBD_FAULT_DT_WR); |
| 2231 | if (!err) |
| 2232 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2233 | |
Lars Ellenberg | 10f6d992 | 2011-01-24 14:47:09 +0100 | [diff] [blame] | 2234 | /* don't care for the reason here */ |
| 2235 | dev_err(DEV, "submit failed, triggering re-connect\n"); |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 2236 | spin_lock_irq(&mdev->tconn->req_lock); |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 2237 | list_del(&peer_req->w.list); |
| 2238 | drbd_remove_epoch_entry_interval(mdev, peer_req); |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 2239 | spin_unlock_irq(&mdev->tconn->req_lock); |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 2240 | if (peer_req->flags & EE_CALL_AL_COMPLETE_IO) |
Lars Ellenberg | 181286a | 2011-03-31 15:18:56 +0200 | [diff] [blame] | 2241 | drbd_al_complete_io(mdev, &peer_req->i); |
Lars Ellenberg | 22cc37a | 2010-09-14 20:40:41 +0200 | [diff] [blame] | 2242 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2243 | out_interrupted: |
Philipp Reisner | 1e9dd29 | 2011-11-10 15:14:53 +0100 | [diff] [blame] | 2244 | drbd_may_finish_epoch(tconn, peer_req->epoch, EV_PUT + EV_CLEANUP); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2245 | put_ldev(mdev); |
Andreas Gruenbacher | 3967deb | 2011-04-06 16:16:56 +0200 | [diff] [blame] | 2246 | drbd_free_peer_req(mdev, peer_req); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 2247 | return err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2248 | } |
| 2249 | |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 2250 | /* We may throttle resync, if the lower device seems to be busy, |
| 2251 | * and current sync rate is above c_min_rate. |
| 2252 | * |
| 2253 | * To decide whether or not the lower device is busy, we use a scheme similar |
| 2254 | * to MD RAID is_mddev_idle(): if the partition stats reveal "significant" |
| 2255 | * (more than 64 sectors) of activity we cannot account for with our own resync |
| 2256 | * activity, it obviously is "busy". |
| 2257 | * |
| 2258 | * The current sync rate used here uses only the most recent two step marks, |
| 2259 | * to have a short time average so we can react faster. |
| 2260 | */ |
Philipp Reisner | e3555d8 | 2010-11-07 15:56:29 +0100 | [diff] [blame] | 2261 | 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] | 2262 | { |
| 2263 | struct gendisk *disk = mdev->ldev->backing_bdev->bd_contains->bd_disk; |
| 2264 | unsigned long db, dt, dbdt; |
Philipp Reisner | e3555d8 | 2010-11-07 15:56:29 +0100 | [diff] [blame] | 2265 | struct lc_element *tmp; |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 2266 | int curr_events; |
| 2267 | int throttle = 0; |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 2268 | unsigned int c_min_rate; |
| 2269 | |
| 2270 | rcu_read_lock(); |
| 2271 | c_min_rate = rcu_dereference(mdev->ldev->disk_conf)->c_min_rate; |
| 2272 | rcu_read_unlock(); |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 2273 | |
| 2274 | /* feature disabled? */ |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 2275 | if (c_min_rate == 0) |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 2276 | return 0; |
| 2277 | |
Philipp Reisner | e3555d8 | 2010-11-07 15:56:29 +0100 | [diff] [blame] | 2278 | spin_lock_irq(&mdev->al_lock); |
| 2279 | tmp = lc_find(mdev->resync, BM_SECT_TO_EXT(sector)); |
| 2280 | if (tmp) { |
| 2281 | struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce); |
| 2282 | if (test_bit(BME_PRIORITY, &bm_ext->flags)) { |
| 2283 | spin_unlock_irq(&mdev->al_lock); |
| 2284 | return 0; |
| 2285 | } |
| 2286 | /* Do not slow down if app IO is already waiting for this extent */ |
| 2287 | } |
| 2288 | spin_unlock_irq(&mdev->al_lock); |
| 2289 | |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 2290 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 2291 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 2292 | atomic_read(&mdev->rs_sect_ev); |
Philipp Reisner | e3555d8 | 2010-11-07 15:56:29 +0100 | [diff] [blame] | 2293 | |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 2294 | if (!mdev->rs_last_events || curr_events - mdev->rs_last_events > 64) { |
| 2295 | unsigned long rs_left; |
| 2296 | int i; |
| 2297 | |
| 2298 | mdev->rs_last_events = curr_events; |
| 2299 | |
| 2300 | /* sync speed average over the last 2*DRBD_SYNC_MARK_STEP, |
| 2301 | * approx. */ |
Lars Ellenberg | 2649f08 | 2010-11-05 10:05:47 +0100 | [diff] [blame] | 2302 | i = (mdev->rs_last_mark + DRBD_SYNC_MARKS-1) % DRBD_SYNC_MARKS; |
| 2303 | |
| 2304 | if (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T) |
| 2305 | rs_left = mdev->ov_left; |
| 2306 | else |
| 2307 | rs_left = drbd_bm_total_weight(mdev) - mdev->rs_failed; |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 2308 | |
| 2309 | dt = ((long)jiffies - (long)mdev->rs_mark_time[i]) / HZ; |
| 2310 | if (!dt) |
| 2311 | dt++; |
| 2312 | db = mdev->rs_mark_left[i] - rs_left; |
| 2313 | dbdt = Bit2KB(db/dt); |
| 2314 | |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 2315 | if (dbdt > c_min_rate) |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 2316 | throttle = 1; |
| 2317 | } |
| 2318 | return throttle; |
| 2319 | } |
| 2320 | |
| 2321 | |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 2322 | static int receive_DataRequest(struct drbd_tconn *tconn, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2323 | { |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 2324 | struct drbd_conf *mdev; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2325 | sector_t sector; |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 2326 | sector_t capacity; |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 2327 | struct drbd_peer_request *peer_req; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2328 | struct digest_info *di = NULL; |
Philipp Reisner | b18b37b | 2010-10-13 15:32:44 +0200 | [diff] [blame] | 2329 | int size, verb; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2330 | unsigned int fault_type; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 2331 | struct p_block_req *p = pi->data; |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 2332 | |
| 2333 | mdev = vnr_to_mdev(tconn, pi->vnr); |
| 2334 | if (!mdev) |
| 2335 | return -EIO; |
| 2336 | capacity = drbd_get_capacity(mdev->this_bdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2337 | |
| 2338 | sector = be64_to_cpu(p->sector); |
| 2339 | size = be32_to_cpu(p->blksize); |
| 2340 | |
Andreas Gruenbacher | c670a39 | 2011-02-21 12:41:39 +0100 | [diff] [blame] | 2341 | if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2342 | dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__, |
| 2343 | (unsigned long long)sector, size); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 2344 | return -EINVAL; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2345 | } |
| 2346 | if (sector + (size>>9) > capacity) { |
| 2347 | dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__, |
| 2348 | (unsigned long long)sector, size); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 2349 | return -EINVAL; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2350 | } |
| 2351 | |
| 2352 | if (!get_ldev_if_state(mdev, D_UP_TO_DATE)) { |
Philipp Reisner | b18b37b | 2010-10-13 15:32:44 +0200 | [diff] [blame] | 2353 | verb = 1; |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 2354 | switch (pi->cmd) { |
Philipp Reisner | b18b37b | 2010-10-13 15:32:44 +0200 | [diff] [blame] | 2355 | case P_DATA_REQUEST: |
| 2356 | drbd_send_ack_rp(mdev, P_NEG_DREPLY, p); |
| 2357 | break; |
| 2358 | case P_RS_DATA_REQUEST: |
| 2359 | case P_CSUM_RS_REQUEST: |
| 2360 | case P_OV_REQUEST: |
| 2361 | drbd_send_ack_rp(mdev, P_NEG_RS_DREPLY , p); |
| 2362 | break; |
| 2363 | case P_OV_REPLY: |
| 2364 | verb = 0; |
| 2365 | dec_rs_pending(mdev); |
| 2366 | drbd_send_ack_ex(mdev, P_OV_RESULT, sector, size, ID_IN_SYNC); |
| 2367 | break; |
| 2368 | default: |
Andreas Gruenbacher | 49ba9b1 | 2011-03-25 00:35:45 +0100 | [diff] [blame] | 2369 | BUG(); |
Philipp Reisner | b18b37b | 2010-10-13 15:32:44 +0200 | [diff] [blame] | 2370 | } |
| 2371 | if (verb && __ratelimit(&drbd_ratelimit_state)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2372 | dev_err(DEV, "Can not satisfy peer's read request, " |
| 2373 | "no local data.\n"); |
Philipp Reisner | b18b37b | 2010-10-13 15:32:44 +0200 | [diff] [blame] | 2374 | |
Lars Ellenberg | a821cc4 | 2010-09-06 12:31:37 +0200 | [diff] [blame] | 2375 | /* drain possibly payload */ |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 2376 | return drbd_drain_block(mdev, pi->size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2377 | } |
| 2378 | |
| 2379 | /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD |
| 2380 | * "criss-cross" setup, that might cause write-out on some other DRBD, |
| 2381 | * which in turn might block on the other node at this very place. */ |
Andreas Gruenbacher | 0db5536 | 2011-04-06 16:09:15 +0200 | [diff] [blame] | 2382 | peer_req = drbd_alloc_peer_req(mdev, p->block_id, sector, size, GFP_NOIO); |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 2383 | if (!peer_req) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2384 | put_ldev(mdev); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 2385 | return -ENOMEM; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2386 | } |
| 2387 | |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 2388 | switch (pi->cmd) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2389 | case P_DATA_REQUEST: |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 2390 | peer_req->w.cb = w_e_end_data_req; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2391 | fault_type = DRBD_FAULT_DT_RD; |
Lars Ellenberg | 80a40e4 | 2010-08-11 23:28:00 +0200 | [diff] [blame] | 2392 | /* application IO, don't drbd_rs_begin_io */ |
| 2393 | goto submit; |
| 2394 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2395 | case P_RS_DATA_REQUEST: |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 2396 | peer_req->w.cb = w_e_end_rsdata_req; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2397 | fault_type = DRBD_FAULT_RS_RD; |
Lars Ellenberg | 5f9915b | 2010-11-09 14:15:24 +0100 | [diff] [blame] | 2398 | /* used in the sector offset progress display */ |
| 2399 | mdev->bm_resync_fo = BM_SECT_TO_BIT(sector); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2400 | break; |
| 2401 | |
| 2402 | case P_OV_REPLY: |
| 2403 | case P_CSUM_RS_REQUEST: |
| 2404 | fault_type = DRBD_FAULT_RS_RD; |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 2405 | di = kmalloc(sizeof(*di) + pi->size, GFP_NOIO); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2406 | if (!di) |
| 2407 | goto out_free_e; |
| 2408 | |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 2409 | di->digest_size = pi->size; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2410 | di->digest = (((char *)di)+sizeof(struct digest_info)); |
| 2411 | |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 2412 | peer_req->digest = di; |
| 2413 | peer_req->flags |= EE_HAS_DIGEST; |
Lars Ellenberg | c36c3ce | 2010-08-11 20:42:55 +0200 | [diff] [blame] | 2414 | |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 2415 | if (drbd_recv_all(mdev->tconn, di->digest, pi->size)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2416 | goto out_free_e; |
| 2417 | |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 2418 | if (pi->cmd == P_CSUM_RS_REQUEST) { |
Philipp Reisner | 31890f4 | 2011-01-19 14:12:51 +0100 | [diff] [blame] | 2419 | D_ASSERT(mdev->tconn->agreed_pro_version >= 89); |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 2420 | peer_req->w.cb = w_e_end_csum_rs_req; |
Lars Ellenberg | 5f9915b | 2010-11-09 14:15:24 +0100 | [diff] [blame] | 2421 | /* used in the sector offset progress display */ |
| 2422 | mdev->bm_resync_fo = BM_SECT_TO_BIT(sector); |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 2423 | } else if (pi->cmd == P_OV_REPLY) { |
Lars Ellenberg | 2649f08 | 2010-11-05 10:05:47 +0100 | [diff] [blame] | 2424 | /* track progress, we may need to throttle */ |
| 2425 | atomic_add(size >> 9, &mdev->rs_sect_in); |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 2426 | peer_req->w.cb = w_e_end_ov_reply; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2427 | dec_rs_pending(mdev); |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 2428 | /* drbd_rs_begin_io done when we sent this request, |
| 2429 | * but accounting still needs to be done. */ |
| 2430 | goto submit_for_resync; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2431 | } |
| 2432 | break; |
| 2433 | |
| 2434 | case P_OV_REQUEST: |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2435 | if (mdev->ov_start_sector == ~(sector_t)0 && |
Philipp Reisner | 31890f4 | 2011-01-19 14:12:51 +0100 | [diff] [blame] | 2436 | mdev->tconn->agreed_pro_version >= 90) { |
Lars Ellenberg | de228bb | 2010-11-05 09:43:15 +0100 | [diff] [blame] | 2437 | unsigned long now = jiffies; |
| 2438 | int i; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2439 | mdev->ov_start_sector = sector; |
| 2440 | mdev->ov_position = sector; |
Lars Ellenberg | 30b743a | 2010-11-05 09:39:06 +0100 | [diff] [blame] | 2441 | mdev->ov_left = drbd_bm_bits(mdev) - BM_SECT_TO_BIT(sector); |
| 2442 | mdev->rs_total = mdev->ov_left; |
Lars Ellenberg | de228bb | 2010-11-05 09:43:15 +0100 | [diff] [blame] | 2443 | for (i = 0; i < DRBD_SYNC_MARKS; i++) { |
| 2444 | mdev->rs_mark_left[i] = mdev->ov_left; |
| 2445 | mdev->rs_mark_time[i] = now; |
| 2446 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2447 | dev_info(DEV, "Online Verify start sector: %llu\n", |
| 2448 | (unsigned long long)sector); |
| 2449 | } |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 2450 | peer_req->w.cb = w_e_end_ov_req; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2451 | fault_type = DRBD_FAULT_RS_RD; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2452 | break; |
| 2453 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2454 | default: |
Andreas Gruenbacher | 49ba9b1 | 2011-03-25 00:35:45 +0100 | [diff] [blame] | 2455 | BUG(); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2456 | } |
| 2457 | |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 2458 | /* Throttle, drbd_rs_begin_io and submit should become asynchronous |
| 2459 | * wrt the receiver, but it is not as straightforward as it may seem. |
| 2460 | * Various places in the resync start and stop logic assume resync |
| 2461 | * requests are processed in order, requeuing this on the worker thread |
| 2462 | * introduces a bunch of new code for synchronization between threads. |
| 2463 | * |
| 2464 | * Unlimited throttling before drbd_rs_begin_io may stall the resync |
| 2465 | * "forever", throttling after drbd_rs_begin_io will lock that extent |
| 2466 | * for application writes for the same time. For now, just throttle |
| 2467 | * here, where the rest of the code expects the receiver to sleep for |
| 2468 | * a while, anyways. |
| 2469 | */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2470 | |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 2471 | /* Throttle before drbd_rs_begin_io, as that locks out application IO; |
| 2472 | * this defers syncer requests for some time, before letting at least |
| 2473 | * on request through. The resync controller on the receiving side |
| 2474 | * will adapt to the incoming rate accordingly. |
| 2475 | * |
| 2476 | * We cannot throttle here if remote is Primary/SyncTarget: |
| 2477 | * we would also throttle its application reads. |
| 2478 | * In that case, throttling is done on the SyncTarget only. |
| 2479 | */ |
Philipp Reisner | e3555d8 | 2010-11-07 15:56:29 +0100 | [diff] [blame] | 2480 | if (mdev->state.peer != R_PRIMARY && drbd_rs_should_slow_down(mdev, sector)) |
| 2481 | schedule_timeout_uninterruptible(HZ/10); |
| 2482 | if (drbd_rs_begin_io(mdev, sector)) |
Lars Ellenberg | 80a40e4 | 2010-08-11 23:28:00 +0200 | [diff] [blame] | 2483 | goto out_free_e; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2484 | |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 2485 | submit_for_resync: |
| 2486 | atomic_add(size >> 9, &mdev->rs_sect_ev); |
| 2487 | |
Lars Ellenberg | 80a40e4 | 2010-08-11 23:28:00 +0200 | [diff] [blame] | 2488 | submit: |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2489 | inc_unacked(mdev); |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 2490 | spin_lock_irq(&mdev->tconn->req_lock); |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 2491 | list_add_tail(&peer_req->w.list, &mdev->read_ee); |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 2492 | spin_unlock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2493 | |
Andreas Gruenbacher | fbe29de | 2011-02-17 16:38:35 +0100 | [diff] [blame] | 2494 | if (drbd_submit_peer_request(mdev, peer_req, READ, fault_type) == 0) |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 2495 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2496 | |
Lars Ellenberg | 10f6d992 | 2011-01-24 14:47:09 +0100 | [diff] [blame] | 2497 | /* don't care for the reason here */ |
| 2498 | dev_err(DEV, "submit failed, triggering re-connect\n"); |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 2499 | spin_lock_irq(&mdev->tconn->req_lock); |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 2500 | list_del(&peer_req->w.list); |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 2501 | spin_unlock_irq(&mdev->tconn->req_lock); |
Lars Ellenberg | 22cc37a | 2010-09-14 20:40:41 +0200 | [diff] [blame] | 2502 | /* no drbd_rs_complete_io(), we are dropping the connection anyways */ |
| 2503 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2504 | out_free_e: |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2505 | put_ldev(mdev); |
Andreas Gruenbacher | 3967deb | 2011-04-06 16:16:56 +0200 | [diff] [blame] | 2506 | drbd_free_peer_req(mdev, peer_req); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 2507 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2508 | } |
| 2509 | |
| 2510 | static int drbd_asb_recover_0p(struct drbd_conf *mdev) __must_hold(local) |
| 2511 | { |
| 2512 | int self, peer, rv = -100; |
| 2513 | unsigned long ch_self, ch_peer; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 2514 | enum drbd_after_sb_p after_sb_0p; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2515 | |
| 2516 | self = mdev->ldev->md.uuid[UI_BITMAP] & 1; |
| 2517 | peer = mdev->p_uuid[UI_BITMAP] & 1; |
| 2518 | |
| 2519 | ch_peer = mdev->p_uuid[UI_SIZE]; |
| 2520 | ch_self = mdev->comm_bm_set; |
| 2521 | |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 2522 | rcu_read_lock(); |
| 2523 | after_sb_0p = rcu_dereference(mdev->tconn->net_conf)->after_sb_0p; |
| 2524 | rcu_read_unlock(); |
| 2525 | switch (after_sb_0p) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2526 | case ASB_CONSENSUS: |
| 2527 | case ASB_DISCARD_SECONDARY: |
| 2528 | case ASB_CALL_HELPER: |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 2529 | case ASB_VIOLENTLY: |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2530 | dev_err(DEV, "Configuration error.\n"); |
| 2531 | break; |
| 2532 | case ASB_DISCONNECT: |
| 2533 | break; |
| 2534 | case ASB_DISCARD_YOUNGER_PRI: |
| 2535 | if (self == 0 && peer == 1) { |
| 2536 | rv = -1; |
| 2537 | break; |
| 2538 | } |
| 2539 | if (self == 1 && peer == 0) { |
| 2540 | rv = 1; |
| 2541 | break; |
| 2542 | } |
| 2543 | /* Else fall through to one of the other strategies... */ |
| 2544 | case ASB_DISCARD_OLDER_PRI: |
| 2545 | if (self == 0 && peer == 1) { |
| 2546 | rv = 1; |
| 2547 | break; |
| 2548 | } |
| 2549 | if (self == 1 && peer == 0) { |
| 2550 | rv = -1; |
| 2551 | break; |
| 2552 | } |
| 2553 | /* Else fall through to one of the other strategies... */ |
Lars Ellenberg | ad19bf6 | 2009-10-14 09:36:49 +0200 | [diff] [blame] | 2554 | 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] | 2555 | "Using discard-least-changes instead\n"); |
| 2556 | case ASB_DISCARD_ZERO_CHG: |
| 2557 | if (ch_peer == 0 && ch_self == 0) { |
Philipp Reisner | 25703f8 | 2011-02-07 14:35:25 +0100 | [diff] [blame] | 2558 | rv = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2559 | ? -1 : 1; |
| 2560 | break; |
| 2561 | } else { |
| 2562 | if (ch_peer == 0) { rv = 1; break; } |
| 2563 | if (ch_self == 0) { rv = -1; break; } |
| 2564 | } |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 2565 | if (after_sb_0p == ASB_DISCARD_ZERO_CHG) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2566 | break; |
| 2567 | case ASB_DISCARD_LEAST_CHG: |
| 2568 | if (ch_self < ch_peer) |
| 2569 | rv = -1; |
| 2570 | else if (ch_self > ch_peer) |
| 2571 | rv = 1; |
| 2572 | else /* ( ch_self == ch_peer ) */ |
| 2573 | /* Well, then use something else. */ |
Philipp Reisner | 25703f8 | 2011-02-07 14:35:25 +0100 | [diff] [blame] | 2574 | rv = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2575 | ? -1 : 1; |
| 2576 | break; |
| 2577 | case ASB_DISCARD_LOCAL: |
| 2578 | rv = -1; |
| 2579 | break; |
| 2580 | case ASB_DISCARD_REMOTE: |
| 2581 | rv = 1; |
| 2582 | } |
| 2583 | |
| 2584 | return rv; |
| 2585 | } |
| 2586 | |
| 2587 | static int drbd_asb_recover_1p(struct drbd_conf *mdev) __must_hold(local) |
| 2588 | { |
Andreas Gruenbacher | 6184ea2 | 2010-12-09 14:23:27 +0100 | [diff] [blame] | 2589 | int hg, rv = -100; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 2590 | enum drbd_after_sb_p after_sb_1p; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2591 | |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 2592 | rcu_read_lock(); |
| 2593 | after_sb_1p = rcu_dereference(mdev->tconn->net_conf)->after_sb_1p; |
| 2594 | rcu_read_unlock(); |
| 2595 | switch (after_sb_1p) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2596 | case ASB_DISCARD_YOUNGER_PRI: |
| 2597 | case ASB_DISCARD_OLDER_PRI: |
| 2598 | case ASB_DISCARD_LEAST_CHG: |
| 2599 | case ASB_DISCARD_LOCAL: |
| 2600 | case ASB_DISCARD_REMOTE: |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 2601 | case ASB_DISCARD_ZERO_CHG: |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2602 | dev_err(DEV, "Configuration error.\n"); |
| 2603 | break; |
| 2604 | case ASB_DISCONNECT: |
| 2605 | break; |
| 2606 | case ASB_CONSENSUS: |
| 2607 | hg = drbd_asb_recover_0p(mdev); |
| 2608 | if (hg == -1 && mdev->state.role == R_SECONDARY) |
| 2609 | rv = hg; |
| 2610 | if (hg == 1 && mdev->state.role == R_PRIMARY) |
| 2611 | rv = hg; |
| 2612 | break; |
| 2613 | case ASB_VIOLENTLY: |
| 2614 | rv = drbd_asb_recover_0p(mdev); |
| 2615 | break; |
| 2616 | case ASB_DISCARD_SECONDARY: |
| 2617 | return mdev->state.role == R_PRIMARY ? 1 : -1; |
| 2618 | case ASB_CALL_HELPER: |
| 2619 | hg = drbd_asb_recover_0p(mdev); |
| 2620 | if (hg == -1 && mdev->state.role == R_PRIMARY) { |
Andreas Gruenbacher | bb43794 | 2010-12-09 14:02:35 +0100 | [diff] [blame] | 2621 | enum drbd_state_rv rv2; |
| 2622 | |
| 2623 | drbd_set_role(mdev, R_SECONDARY, 0); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2624 | /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE, |
| 2625 | * we might be here in C_WF_REPORT_PARAMS which is transient. |
| 2626 | * 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] | 2627 | rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY)); |
| 2628 | if (rv2 != SS_SUCCESS) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2629 | drbd_khelper(mdev, "pri-lost-after-sb"); |
| 2630 | } else { |
| 2631 | dev_warn(DEV, "Successfully gave up primary role.\n"); |
| 2632 | rv = hg; |
| 2633 | } |
| 2634 | } else |
| 2635 | rv = hg; |
| 2636 | } |
| 2637 | |
| 2638 | return rv; |
| 2639 | } |
| 2640 | |
| 2641 | static int drbd_asb_recover_2p(struct drbd_conf *mdev) __must_hold(local) |
| 2642 | { |
Andreas Gruenbacher | 6184ea2 | 2010-12-09 14:23:27 +0100 | [diff] [blame] | 2643 | int hg, rv = -100; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 2644 | enum drbd_after_sb_p after_sb_2p; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2645 | |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 2646 | rcu_read_lock(); |
| 2647 | after_sb_2p = rcu_dereference(mdev->tconn->net_conf)->after_sb_2p; |
| 2648 | rcu_read_unlock(); |
| 2649 | switch (after_sb_2p) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2650 | case ASB_DISCARD_YOUNGER_PRI: |
| 2651 | case ASB_DISCARD_OLDER_PRI: |
| 2652 | case ASB_DISCARD_LEAST_CHG: |
| 2653 | case ASB_DISCARD_LOCAL: |
| 2654 | case ASB_DISCARD_REMOTE: |
| 2655 | case ASB_CONSENSUS: |
| 2656 | case ASB_DISCARD_SECONDARY: |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 2657 | case ASB_DISCARD_ZERO_CHG: |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2658 | dev_err(DEV, "Configuration error.\n"); |
| 2659 | break; |
| 2660 | case ASB_VIOLENTLY: |
| 2661 | rv = drbd_asb_recover_0p(mdev); |
| 2662 | break; |
| 2663 | case ASB_DISCONNECT: |
| 2664 | break; |
| 2665 | case ASB_CALL_HELPER: |
| 2666 | hg = drbd_asb_recover_0p(mdev); |
| 2667 | if (hg == -1) { |
Andreas Gruenbacher | bb43794 | 2010-12-09 14:02:35 +0100 | [diff] [blame] | 2668 | enum drbd_state_rv rv2; |
| 2669 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2670 | /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE, |
| 2671 | * we might be here in C_WF_REPORT_PARAMS which is transient. |
| 2672 | * 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] | 2673 | rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY)); |
| 2674 | if (rv2 != SS_SUCCESS) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2675 | drbd_khelper(mdev, "pri-lost-after-sb"); |
| 2676 | } else { |
| 2677 | dev_warn(DEV, "Successfully gave up primary role.\n"); |
| 2678 | rv = hg; |
| 2679 | } |
| 2680 | } else |
| 2681 | rv = hg; |
| 2682 | } |
| 2683 | |
| 2684 | return rv; |
| 2685 | } |
| 2686 | |
| 2687 | static void drbd_uuid_dump(struct drbd_conf *mdev, char *text, u64 *uuid, |
| 2688 | u64 bits, u64 flags) |
| 2689 | { |
| 2690 | if (!uuid) { |
| 2691 | dev_info(DEV, "%s uuid info vanished while I was looking!\n", text); |
| 2692 | return; |
| 2693 | } |
| 2694 | dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX bits:%llu flags:%llX\n", |
| 2695 | text, |
| 2696 | (unsigned long long)uuid[UI_CURRENT], |
| 2697 | (unsigned long long)uuid[UI_BITMAP], |
| 2698 | (unsigned long long)uuid[UI_HISTORY_START], |
| 2699 | (unsigned long long)uuid[UI_HISTORY_END], |
| 2700 | (unsigned long long)bits, |
| 2701 | (unsigned long long)flags); |
| 2702 | } |
| 2703 | |
| 2704 | /* |
| 2705 | 100 after split brain try auto recover |
| 2706 | 2 C_SYNC_SOURCE set BitMap |
| 2707 | 1 C_SYNC_SOURCE use BitMap |
| 2708 | 0 no Sync |
| 2709 | -1 C_SYNC_TARGET use BitMap |
| 2710 | -2 C_SYNC_TARGET set BitMap |
| 2711 | -100 after split brain, disconnect |
| 2712 | -1000 unrelated data |
Philipp Reisner | 4a23f26 | 2011-01-11 17:42:17 +0100 | [diff] [blame] | 2713 | -1091 requires proto 91 |
| 2714 | -1096 requires proto 96 |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2715 | */ |
| 2716 | static int drbd_uuid_compare(struct drbd_conf *mdev, int *rule_nr) __must_hold(local) |
| 2717 | { |
| 2718 | u64 self, peer; |
| 2719 | int i, j; |
| 2720 | |
| 2721 | self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1); |
| 2722 | peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1); |
| 2723 | |
| 2724 | *rule_nr = 10; |
| 2725 | if (self == UUID_JUST_CREATED && peer == UUID_JUST_CREATED) |
| 2726 | return 0; |
| 2727 | |
| 2728 | *rule_nr = 20; |
| 2729 | if ((self == UUID_JUST_CREATED || self == (u64)0) && |
| 2730 | peer != UUID_JUST_CREATED) |
| 2731 | return -2; |
| 2732 | |
| 2733 | *rule_nr = 30; |
| 2734 | if (self != UUID_JUST_CREATED && |
| 2735 | (peer == UUID_JUST_CREATED || peer == (u64)0)) |
| 2736 | return 2; |
| 2737 | |
| 2738 | if (self == peer) { |
| 2739 | int rct, dc; /* roles at crash time */ |
| 2740 | |
| 2741 | if (mdev->p_uuid[UI_BITMAP] == (u64)0 && mdev->ldev->md.uuid[UI_BITMAP] != (u64)0) { |
| 2742 | |
Philipp Reisner | 31890f4 | 2011-01-19 14:12:51 +0100 | [diff] [blame] | 2743 | if (mdev->tconn->agreed_pro_version < 91) |
Philipp Reisner | 4a23f26 | 2011-01-11 17:42:17 +0100 | [diff] [blame] | 2744 | return -1091; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2745 | |
| 2746 | if ((mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) && |
| 2747 | (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1))) { |
| 2748 | dev_info(DEV, "was SyncSource, missed the resync finished event, corrected myself:\n"); |
| 2749 | drbd_uuid_set_bm(mdev, 0UL); |
| 2750 | |
| 2751 | drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid, |
| 2752 | mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0); |
| 2753 | *rule_nr = 34; |
| 2754 | } else { |
| 2755 | dev_info(DEV, "was SyncSource (peer failed to write sync_uuid)\n"); |
| 2756 | *rule_nr = 36; |
| 2757 | } |
| 2758 | |
| 2759 | return 1; |
| 2760 | } |
| 2761 | |
| 2762 | if (mdev->ldev->md.uuid[UI_BITMAP] == (u64)0 && mdev->p_uuid[UI_BITMAP] != (u64)0) { |
| 2763 | |
Philipp Reisner | 31890f4 | 2011-01-19 14:12:51 +0100 | [diff] [blame] | 2764 | if (mdev->tconn->agreed_pro_version < 91) |
Philipp Reisner | 4a23f26 | 2011-01-11 17:42:17 +0100 | [diff] [blame] | 2765 | return -1091; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2766 | |
| 2767 | if ((mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_BITMAP] & ~((u64)1)) && |
| 2768 | (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1))) { |
| 2769 | dev_info(DEV, "was SyncTarget, peer missed the resync finished event, corrected peer:\n"); |
| 2770 | |
| 2771 | mdev->p_uuid[UI_HISTORY_START + 1] = mdev->p_uuid[UI_HISTORY_START]; |
| 2772 | mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_BITMAP]; |
| 2773 | mdev->p_uuid[UI_BITMAP] = 0UL; |
| 2774 | |
| 2775 | drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]); |
| 2776 | *rule_nr = 35; |
| 2777 | } else { |
| 2778 | dev_info(DEV, "was SyncTarget (failed to write sync_uuid)\n"); |
| 2779 | *rule_nr = 37; |
| 2780 | } |
| 2781 | |
| 2782 | return -1; |
| 2783 | } |
| 2784 | |
| 2785 | /* Common power [off|failure] */ |
| 2786 | rct = (test_bit(CRASHED_PRIMARY, &mdev->flags) ? 1 : 0) + |
| 2787 | (mdev->p_uuid[UI_FLAGS] & 2); |
| 2788 | /* lowest bit is set when we were primary, |
| 2789 | * next bit (weight 2) is set when peer was primary */ |
| 2790 | *rule_nr = 40; |
| 2791 | |
| 2792 | switch (rct) { |
| 2793 | case 0: /* !self_pri && !peer_pri */ return 0; |
| 2794 | case 1: /* self_pri && !peer_pri */ return 1; |
| 2795 | case 2: /* !self_pri && peer_pri */ return -1; |
| 2796 | case 3: /* self_pri && peer_pri */ |
Philipp Reisner | 25703f8 | 2011-02-07 14:35:25 +0100 | [diff] [blame] | 2797 | dc = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2798 | return dc ? -1 : 1; |
| 2799 | } |
| 2800 | } |
| 2801 | |
| 2802 | *rule_nr = 50; |
| 2803 | peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1); |
| 2804 | if (self == peer) |
| 2805 | return -1; |
| 2806 | |
| 2807 | *rule_nr = 51; |
| 2808 | peer = mdev->p_uuid[UI_HISTORY_START] & ~((u64)1); |
| 2809 | if (self == peer) { |
Philipp Reisner | 31890f4 | 2011-01-19 14:12:51 +0100 | [diff] [blame] | 2810 | if (mdev->tconn->agreed_pro_version < 96 ? |
Philipp Reisner | 4a23f26 | 2011-01-11 17:42:17 +0100 | [diff] [blame] | 2811 | (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == |
| 2812 | (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1)) : |
| 2813 | peer + UUID_NEW_BM_OFFSET == (mdev->p_uuid[UI_BITMAP] & ~((u64)1))) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2814 | /* The last P_SYNC_UUID did not get though. Undo the last start of |
| 2815 | resync as sync source modifications of the peer's UUIDs. */ |
| 2816 | |
Philipp Reisner | 31890f4 | 2011-01-19 14:12:51 +0100 | [diff] [blame] | 2817 | if (mdev->tconn->agreed_pro_version < 91) |
Philipp Reisner | 4a23f26 | 2011-01-11 17:42:17 +0100 | [diff] [blame] | 2818 | return -1091; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2819 | |
| 2820 | mdev->p_uuid[UI_BITMAP] = mdev->p_uuid[UI_HISTORY_START]; |
| 2821 | 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] | 2822 | |
| 2823 | dev_info(DEV, "Did not got last syncUUID packet, corrected:\n"); |
| 2824 | drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]); |
| 2825 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2826 | return -1; |
| 2827 | } |
| 2828 | } |
| 2829 | |
| 2830 | *rule_nr = 60; |
| 2831 | self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1); |
| 2832 | for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) { |
| 2833 | peer = mdev->p_uuid[i] & ~((u64)1); |
| 2834 | if (self == peer) |
| 2835 | return -2; |
| 2836 | } |
| 2837 | |
| 2838 | *rule_nr = 70; |
| 2839 | self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1); |
| 2840 | peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1); |
| 2841 | if (self == peer) |
| 2842 | return 1; |
| 2843 | |
| 2844 | *rule_nr = 71; |
| 2845 | self = mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1); |
| 2846 | if (self == peer) { |
Philipp Reisner | 31890f4 | 2011-01-19 14:12:51 +0100 | [diff] [blame] | 2847 | if (mdev->tconn->agreed_pro_version < 96 ? |
Philipp Reisner | 4a23f26 | 2011-01-11 17:42:17 +0100 | [diff] [blame] | 2848 | (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == |
| 2849 | (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) : |
| 2850 | 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] | 2851 | /* The last P_SYNC_UUID did not get though. Undo the last start of |
| 2852 | resync as sync source modifications of our UUIDs. */ |
| 2853 | |
Philipp Reisner | 31890f4 | 2011-01-19 14:12:51 +0100 | [diff] [blame] | 2854 | if (mdev->tconn->agreed_pro_version < 91) |
Philipp Reisner | 4a23f26 | 2011-01-11 17:42:17 +0100 | [diff] [blame] | 2855 | return -1091; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2856 | |
| 2857 | _drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_HISTORY_START]); |
| 2858 | _drbd_uuid_set(mdev, UI_HISTORY_START, mdev->ldev->md.uuid[UI_HISTORY_START + 1]); |
| 2859 | |
Philipp Reisner | 4a23f26 | 2011-01-11 17:42:17 +0100 | [diff] [blame] | 2860 | dev_info(DEV, "Last syncUUID did not get through, corrected:\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2861 | drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid, |
| 2862 | mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0); |
| 2863 | |
| 2864 | return 1; |
| 2865 | } |
| 2866 | } |
| 2867 | |
| 2868 | |
| 2869 | *rule_nr = 80; |
Philipp Reisner | d8c2a36 | 2009-11-18 15:52:51 +0100 | [diff] [blame] | 2870 | peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2871 | for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) { |
| 2872 | self = mdev->ldev->md.uuid[i] & ~((u64)1); |
| 2873 | if (self == peer) |
| 2874 | return 2; |
| 2875 | } |
| 2876 | |
| 2877 | *rule_nr = 90; |
| 2878 | self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1); |
| 2879 | peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1); |
| 2880 | if (self == peer && self != ((u64)0)) |
| 2881 | return 100; |
| 2882 | |
| 2883 | *rule_nr = 100; |
| 2884 | for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) { |
| 2885 | self = mdev->ldev->md.uuid[i] & ~((u64)1); |
| 2886 | for (j = UI_HISTORY_START; j <= UI_HISTORY_END; j++) { |
| 2887 | peer = mdev->p_uuid[j] & ~((u64)1); |
| 2888 | if (self == peer) |
| 2889 | return -100; |
| 2890 | } |
| 2891 | } |
| 2892 | |
| 2893 | return -1000; |
| 2894 | } |
| 2895 | |
| 2896 | /* drbd_sync_handshake() returns the new conn state on success, or |
| 2897 | CONN_MASK (-1) on failure. |
| 2898 | */ |
| 2899 | static enum drbd_conns drbd_sync_handshake(struct drbd_conf *mdev, enum drbd_role peer_role, |
| 2900 | enum drbd_disk_state peer_disk) __must_hold(local) |
| 2901 | { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2902 | enum drbd_conns rv = C_MASK; |
| 2903 | enum drbd_disk_state mydisk; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 2904 | struct net_conf *nc; |
Andreas Gruenbacher | 6dff290 | 2011-06-28 14:18:12 +0200 | [diff] [blame] | 2905 | int hg, rule_nr, rr_conflict, tentative; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2906 | |
| 2907 | mydisk = mdev->state.disk; |
| 2908 | if (mydisk == D_NEGOTIATING) |
| 2909 | mydisk = mdev->new_state_tmp.disk; |
| 2910 | |
| 2911 | dev_info(DEV, "drbd_sync_handshake:\n"); |
| 2912 | drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid, mdev->comm_bm_set, 0); |
| 2913 | drbd_uuid_dump(mdev, "peer", mdev->p_uuid, |
| 2914 | mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]); |
| 2915 | |
| 2916 | hg = drbd_uuid_compare(mdev, &rule_nr); |
| 2917 | |
| 2918 | dev_info(DEV, "uuid_compare()=%d by rule %d\n", hg, rule_nr); |
| 2919 | |
| 2920 | if (hg == -1000) { |
| 2921 | dev_alert(DEV, "Unrelated data, aborting!\n"); |
| 2922 | return C_MASK; |
| 2923 | } |
Philipp Reisner | 4a23f26 | 2011-01-11 17:42:17 +0100 | [diff] [blame] | 2924 | if (hg < -1000) { |
| 2925 | 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] | 2926 | return C_MASK; |
| 2927 | } |
| 2928 | |
| 2929 | if ((mydisk == D_INCONSISTENT && peer_disk > D_INCONSISTENT) || |
| 2930 | (peer_disk == D_INCONSISTENT && mydisk > D_INCONSISTENT)) { |
| 2931 | int f = (hg == -100) || abs(hg) == 2; |
| 2932 | hg = mydisk > D_INCONSISTENT ? 1 : -1; |
| 2933 | if (f) |
| 2934 | hg = hg*2; |
| 2935 | dev_info(DEV, "Becoming sync %s due to disk states.\n", |
| 2936 | hg > 0 ? "source" : "target"); |
| 2937 | } |
| 2938 | |
Adam Gandelman | 3a11a48 | 2010-04-08 16:48:23 -0700 | [diff] [blame] | 2939 | if (abs(hg) == 100) |
| 2940 | drbd_khelper(mdev, "initial-split-brain"); |
| 2941 | |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 2942 | rcu_read_lock(); |
| 2943 | nc = rcu_dereference(mdev->tconn->net_conf); |
| 2944 | |
| 2945 | if (hg == 100 || (hg == -100 && nc->always_asbp)) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2946 | int pcount = (mdev->state.role == R_PRIMARY) |
| 2947 | + (peer_role == R_PRIMARY); |
| 2948 | int forced = (hg == -100); |
| 2949 | |
| 2950 | switch (pcount) { |
| 2951 | case 0: |
| 2952 | hg = drbd_asb_recover_0p(mdev); |
| 2953 | break; |
| 2954 | case 1: |
| 2955 | hg = drbd_asb_recover_1p(mdev); |
| 2956 | break; |
| 2957 | case 2: |
| 2958 | hg = drbd_asb_recover_2p(mdev); |
| 2959 | break; |
| 2960 | } |
| 2961 | if (abs(hg) < 100) { |
| 2962 | dev_warn(DEV, "Split-Brain detected, %d primaries, " |
| 2963 | "automatically solved. Sync from %s node\n", |
| 2964 | pcount, (hg < 0) ? "peer" : "this"); |
| 2965 | if (forced) { |
| 2966 | dev_warn(DEV, "Doing a full sync, since" |
| 2967 | " UUIDs where ambiguous.\n"); |
| 2968 | hg = hg*2; |
| 2969 | } |
| 2970 | } |
| 2971 | } |
| 2972 | |
| 2973 | if (hg == -100) { |
Philipp Reisner | 08b165b | 2011-09-05 16:22:33 +0200 | [diff] [blame] | 2974 | if (test_bit(DISCARD_MY_DATA, &mdev->flags) && !(mdev->p_uuid[UI_FLAGS]&1)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2975 | hg = -1; |
Philipp Reisner | 08b165b | 2011-09-05 16:22:33 +0200 | [diff] [blame] | 2976 | if (!test_bit(DISCARD_MY_DATA, &mdev->flags) && (mdev->p_uuid[UI_FLAGS]&1)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2977 | hg = 1; |
| 2978 | |
| 2979 | if (abs(hg) < 100) |
| 2980 | dev_warn(DEV, "Split-Brain detected, manually solved. " |
| 2981 | "Sync from %s node\n", |
| 2982 | (hg < 0) ? "peer" : "this"); |
| 2983 | } |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 2984 | rr_conflict = nc->rr_conflict; |
Andreas Gruenbacher | 6dff290 | 2011-06-28 14:18:12 +0200 | [diff] [blame] | 2985 | tentative = nc->tentative; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 2986 | rcu_read_unlock(); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2987 | |
| 2988 | if (hg == -100) { |
Lars Ellenberg | 580b976 | 2010-02-26 23:15:23 +0100 | [diff] [blame] | 2989 | /* FIXME this log message is not correct if we end up here |
| 2990 | * after an attempted attach on a diskless node. |
| 2991 | * We just refuse to attach -- well, we drop the "connection" |
| 2992 | * to that disk, in a way... */ |
Adam Gandelman | 3a11a48 | 2010-04-08 16:48:23 -0700 | [diff] [blame] | 2993 | dev_alert(DEV, "Split-Brain detected but unresolved, dropping connection!\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2994 | drbd_khelper(mdev, "split-brain"); |
| 2995 | return C_MASK; |
| 2996 | } |
| 2997 | |
| 2998 | if (hg > 0 && mydisk <= D_INCONSISTENT) { |
| 2999 | dev_err(DEV, "I shall become SyncSource, but I am inconsistent!\n"); |
| 3000 | return C_MASK; |
| 3001 | } |
| 3002 | |
| 3003 | if (hg < 0 && /* by intention we do not use mydisk here. */ |
| 3004 | mdev->state.role == R_PRIMARY && mdev->state.disk >= D_CONSISTENT) { |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 3005 | switch (rr_conflict) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3006 | case ASB_CALL_HELPER: |
| 3007 | drbd_khelper(mdev, "pri-lost"); |
| 3008 | /* fall through */ |
| 3009 | case ASB_DISCONNECT: |
| 3010 | dev_err(DEV, "I shall become SyncTarget, but I am primary!\n"); |
| 3011 | return C_MASK; |
| 3012 | case ASB_VIOLENTLY: |
| 3013 | dev_warn(DEV, "Becoming SyncTarget, violating the stable-data" |
| 3014 | "assumption\n"); |
| 3015 | } |
| 3016 | } |
| 3017 | |
Andreas Gruenbacher | 6dff290 | 2011-06-28 14:18:12 +0200 | [diff] [blame] | 3018 | if (tentative || test_bit(CONN_DRY_RUN, &mdev->tconn->flags)) { |
Philipp Reisner | cf14c2e | 2010-02-02 21:03:50 +0100 | [diff] [blame] | 3019 | if (hg == 0) |
| 3020 | dev_info(DEV, "dry-run connect: No resync, would become Connected immediately.\n"); |
| 3021 | else |
| 3022 | dev_info(DEV, "dry-run connect: Would become %s, doing a %s resync.", |
| 3023 | drbd_conn_str(hg > 0 ? C_SYNC_SOURCE : C_SYNC_TARGET), |
| 3024 | abs(hg) >= 2 ? "full" : "bit-map based"); |
| 3025 | return C_MASK; |
| 3026 | } |
| 3027 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3028 | if (abs(hg) >= 2) { |
| 3029 | 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] | 3030 | if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write, "set_n_write from sync_handshake", |
| 3031 | BM_LOCKED_SET_ALLOWED)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3032 | return C_MASK; |
| 3033 | } |
| 3034 | |
| 3035 | if (hg > 0) { /* become sync source. */ |
| 3036 | rv = C_WF_BITMAP_S; |
| 3037 | } else if (hg < 0) { /* become sync target */ |
| 3038 | rv = C_WF_BITMAP_T; |
| 3039 | } else { |
| 3040 | rv = C_CONNECTED; |
| 3041 | if (drbd_bm_total_weight(mdev)) { |
| 3042 | dev_info(DEV, "No resync, but %lu bits in bitmap!\n", |
| 3043 | drbd_bm_total_weight(mdev)); |
| 3044 | } |
| 3045 | } |
| 3046 | |
| 3047 | return rv; |
| 3048 | } |
| 3049 | |
Philipp Reisner | f179d76 | 2011-05-16 17:31:47 +0200 | [diff] [blame] | 3050 | static enum drbd_after_sb_p convert_after_sb(enum drbd_after_sb_p peer) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3051 | { |
| 3052 | /* ASB_DISCARD_REMOTE - ASB_DISCARD_LOCAL is valid */ |
Philipp Reisner | f179d76 | 2011-05-16 17:31:47 +0200 | [diff] [blame] | 3053 | if (peer == ASB_DISCARD_REMOTE) |
| 3054 | return ASB_DISCARD_LOCAL; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3055 | |
| 3056 | /* any other things with ASB_DISCARD_REMOTE or ASB_DISCARD_LOCAL are invalid */ |
Philipp Reisner | f179d76 | 2011-05-16 17:31:47 +0200 | [diff] [blame] | 3057 | if (peer == ASB_DISCARD_LOCAL) |
| 3058 | return ASB_DISCARD_REMOTE; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3059 | |
| 3060 | /* everything else is valid if they are equal on both sides. */ |
Philipp Reisner | f179d76 | 2011-05-16 17:31:47 +0200 | [diff] [blame] | 3061 | return peer; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3062 | } |
| 3063 | |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 3064 | static int receive_protocol(struct drbd_tconn *tconn, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3065 | { |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 3066 | struct p_protocol *p = pi->data; |
Philipp Reisner | 036b17e | 2011-05-16 17:38:11 +0200 | [diff] [blame] | 3067 | enum drbd_after_sb_p p_after_sb_0p, p_after_sb_1p, p_after_sb_2p; |
| 3068 | int p_proto, p_discard_my_data, p_two_primaries, cf; |
| 3069 | struct net_conf *nc, *old_net_conf, *new_net_conf = NULL; |
| 3070 | char integrity_alg[SHARED_SECRET_MAX] = ""; |
Andreas Gruenbacher | accdbcc | 2011-07-15 17:41:09 +0200 | [diff] [blame] | 3071 | struct crypto_hash *peer_integrity_tfm = NULL; |
Philipp Reisner | 7aca6c7 | 2011-05-17 10:12:56 +0200 | [diff] [blame] | 3072 | void *int_dig_in = NULL, *int_dig_vv = NULL; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3073 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3074 | p_proto = be32_to_cpu(p->protocol); |
| 3075 | p_after_sb_0p = be32_to_cpu(p->after_sb_0p); |
| 3076 | p_after_sb_1p = be32_to_cpu(p->after_sb_1p); |
| 3077 | p_after_sb_2p = be32_to_cpu(p->after_sb_2p); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3078 | p_two_primaries = be32_to_cpu(p->two_primaries); |
Philipp Reisner | cf14c2e | 2010-02-02 21:03:50 +0100 | [diff] [blame] | 3079 | cf = be32_to_cpu(p->conn_flags); |
Andreas Gruenbacher | 6139f60 | 2011-05-06 20:00:02 +0200 | [diff] [blame] | 3080 | p_discard_my_data = cf & CF_DISCARD_MY_DATA; |
Philipp Reisner | cf14c2e | 2010-02-02 21:03:50 +0100 | [diff] [blame] | 3081 | |
Andreas Gruenbacher | 86db061 | 2011-04-28 15:24:18 +0200 | [diff] [blame] | 3082 | if (tconn->agreed_pro_version >= 87) { |
| 3083 | int err; |
| 3084 | |
Andreas Gruenbacher | 88104ca | 2011-04-28 21:47:21 +0200 | [diff] [blame] | 3085 | if (pi->size > sizeof(integrity_alg)) |
Andreas Gruenbacher | 86db061 | 2011-04-28 15:24:18 +0200 | [diff] [blame] | 3086 | return -EIO; |
Andreas Gruenbacher | 88104ca | 2011-04-28 21:47:21 +0200 | [diff] [blame] | 3087 | err = drbd_recv_all(tconn, integrity_alg, pi->size); |
Andreas Gruenbacher | 86db061 | 2011-04-28 15:24:18 +0200 | [diff] [blame] | 3088 | if (err) |
| 3089 | return err; |
Philipp Reisner | 036b17e | 2011-05-16 17:38:11 +0200 | [diff] [blame] | 3090 | integrity_alg[SHARED_SECRET_MAX - 1] = 0; |
| 3091 | } |
Andreas Gruenbacher | 86db061 | 2011-04-28 15:24:18 +0200 | [diff] [blame] | 3092 | |
Andreas Gruenbacher | 7d4c782 | 2011-07-17 23:06:12 +0200 | [diff] [blame] | 3093 | if (pi->cmd != P_PROTOCOL_UPDATE) { |
Andreas Gruenbacher | fbc12f4 | 2011-07-15 17:04:26 +0200 | [diff] [blame] | 3094 | clear_bit(CONN_DRY_RUN, &tconn->flags); |
Philipp Reisner | 036b17e | 2011-05-16 17:38:11 +0200 | [diff] [blame] | 3095 | |
Andreas Gruenbacher | fbc12f4 | 2011-07-15 17:04:26 +0200 | [diff] [blame] | 3096 | if (cf & CF_DRY_RUN) |
| 3097 | set_bit(CONN_DRY_RUN, &tconn->flags); |
| 3098 | |
| 3099 | rcu_read_lock(); |
| 3100 | nc = rcu_dereference(tconn->net_conf); |
| 3101 | |
| 3102 | if (p_proto != nc->wire_protocol) { |
Andreas Gruenbacher | d505d9b | 2011-07-15 17:19:18 +0200 | [diff] [blame] | 3103 | conn_err(tconn, "incompatible %s settings\n", "protocol"); |
Andreas Gruenbacher | fbc12f4 | 2011-07-15 17:04:26 +0200 | [diff] [blame] | 3104 | goto disconnect_rcu_unlock; |
| 3105 | } |
| 3106 | |
| 3107 | if (convert_after_sb(p_after_sb_0p) != nc->after_sb_0p) { |
Andreas Gruenbacher | d505d9b | 2011-07-15 17:19:18 +0200 | [diff] [blame] | 3108 | conn_err(tconn, "incompatible %s settings\n", "after-sb-0pri"); |
Andreas Gruenbacher | fbc12f4 | 2011-07-15 17:04:26 +0200 | [diff] [blame] | 3109 | goto disconnect_rcu_unlock; |
| 3110 | } |
| 3111 | |
| 3112 | if (convert_after_sb(p_after_sb_1p) != nc->after_sb_1p) { |
Andreas Gruenbacher | d505d9b | 2011-07-15 17:19:18 +0200 | [diff] [blame] | 3113 | conn_err(tconn, "incompatible %s settings\n", "after-sb-1pri"); |
Andreas Gruenbacher | fbc12f4 | 2011-07-15 17:04:26 +0200 | [diff] [blame] | 3114 | goto disconnect_rcu_unlock; |
| 3115 | } |
| 3116 | |
| 3117 | if (convert_after_sb(p_after_sb_2p) != nc->after_sb_2p) { |
Andreas Gruenbacher | d505d9b | 2011-07-15 17:19:18 +0200 | [diff] [blame] | 3118 | conn_err(tconn, "incompatible %s settings\n", "after-sb-2pri"); |
Andreas Gruenbacher | fbc12f4 | 2011-07-15 17:04:26 +0200 | [diff] [blame] | 3119 | goto disconnect_rcu_unlock; |
| 3120 | } |
| 3121 | |
| 3122 | if (p_discard_my_data && nc->discard_my_data) { |
Andreas Gruenbacher | d505d9b | 2011-07-15 17:19:18 +0200 | [diff] [blame] | 3123 | conn_err(tconn, "incompatible %s settings\n", "discard-my-data"); |
Andreas Gruenbacher | fbc12f4 | 2011-07-15 17:04:26 +0200 | [diff] [blame] | 3124 | goto disconnect_rcu_unlock; |
| 3125 | } |
| 3126 | |
| 3127 | if (p_two_primaries != nc->two_primaries) { |
Andreas Gruenbacher | d505d9b | 2011-07-15 17:19:18 +0200 | [diff] [blame] | 3128 | conn_err(tconn, "incompatible %s settings\n", "allow-two-primaries"); |
Andreas Gruenbacher | fbc12f4 | 2011-07-15 17:04:26 +0200 | [diff] [blame] | 3129 | goto disconnect_rcu_unlock; |
| 3130 | } |
| 3131 | |
| 3132 | if (strcmp(integrity_alg, nc->integrity_alg)) { |
Andreas Gruenbacher | d505d9b | 2011-07-15 17:19:18 +0200 | [diff] [blame] | 3133 | conn_err(tconn, "incompatible %s settings\n", "data-integrity-alg"); |
Andreas Gruenbacher | fbc12f4 | 2011-07-15 17:04:26 +0200 | [diff] [blame] | 3134 | goto disconnect_rcu_unlock; |
| 3135 | } |
| 3136 | |
| 3137 | rcu_read_unlock(); |
Andreas Gruenbacher | 86db061 | 2011-04-28 15:24:18 +0200 | [diff] [blame] | 3138 | } |
Andreas Gruenbacher | 7d4c782 | 2011-07-17 23:06:12 +0200 | [diff] [blame] | 3139 | |
| 3140 | if (integrity_alg[0]) { |
| 3141 | int hash_size; |
| 3142 | |
| 3143 | /* |
| 3144 | * We can only change the peer data integrity algorithm |
| 3145 | * here. Changing our own data integrity algorithm |
| 3146 | * requires that we send a P_PROTOCOL_UPDATE packet at |
| 3147 | * the same time; otherwise, the peer has no way to |
| 3148 | * tell between which packets the algorithm should |
| 3149 | * change. |
| 3150 | */ |
| 3151 | |
| 3152 | peer_integrity_tfm = crypto_alloc_hash(integrity_alg, 0, CRYPTO_ALG_ASYNC); |
| 3153 | if (!peer_integrity_tfm) { |
| 3154 | conn_err(tconn, "peer data-integrity-alg %s not supported\n", |
| 3155 | integrity_alg); |
| 3156 | goto disconnect; |
| 3157 | } |
| 3158 | |
| 3159 | hash_size = crypto_hash_digestsize(peer_integrity_tfm); |
| 3160 | int_dig_in = kmalloc(hash_size, GFP_KERNEL); |
| 3161 | int_dig_vv = kmalloc(hash_size, GFP_KERNEL); |
| 3162 | if (!(int_dig_in && int_dig_vv)) { |
| 3163 | conn_err(tconn, "Allocation of buffers for data integrity checking failed\n"); |
| 3164 | goto disconnect; |
| 3165 | } |
| 3166 | } |
| 3167 | |
| 3168 | new_net_conf = kmalloc(sizeof(struct net_conf), GFP_KERNEL); |
| 3169 | if (!new_net_conf) { |
| 3170 | conn_err(tconn, "Allocation of new net_conf failed\n"); |
| 3171 | goto disconnect; |
| 3172 | } |
| 3173 | |
| 3174 | mutex_lock(&tconn->data.mutex); |
| 3175 | mutex_lock(&tconn->conf_update); |
| 3176 | old_net_conf = tconn->net_conf; |
| 3177 | *new_net_conf = *old_net_conf; |
| 3178 | |
| 3179 | new_net_conf->wire_protocol = p_proto; |
| 3180 | new_net_conf->after_sb_0p = convert_after_sb(p_after_sb_0p); |
| 3181 | new_net_conf->after_sb_1p = convert_after_sb(p_after_sb_1p); |
| 3182 | new_net_conf->after_sb_2p = convert_after_sb(p_after_sb_2p); |
| 3183 | new_net_conf->two_primaries = p_two_primaries; |
| 3184 | |
| 3185 | rcu_assign_pointer(tconn->net_conf, new_net_conf); |
| 3186 | mutex_unlock(&tconn->conf_update); |
| 3187 | mutex_unlock(&tconn->data.mutex); |
| 3188 | |
| 3189 | crypto_free_hash(tconn->peer_integrity_tfm); |
| 3190 | kfree(tconn->int_dig_in); |
| 3191 | kfree(tconn->int_dig_vv); |
| 3192 | tconn->peer_integrity_tfm = peer_integrity_tfm; |
| 3193 | tconn->int_dig_in = int_dig_in; |
| 3194 | tconn->int_dig_vv = int_dig_vv; |
| 3195 | |
| 3196 | if (strcmp(old_net_conf->integrity_alg, integrity_alg)) |
| 3197 | conn_info(tconn, "peer data-integrity-alg: %s\n", |
| 3198 | integrity_alg[0] ? integrity_alg : "(none)"); |
| 3199 | |
| 3200 | synchronize_rcu(); |
| 3201 | kfree(old_net_conf); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3202 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3203 | |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 3204 | disconnect_rcu_unlock: |
| 3205 | rcu_read_unlock(); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3206 | disconnect: |
Andreas Gruenbacher | b792c35 | 2011-07-15 16:48:49 +0200 | [diff] [blame] | 3207 | crypto_free_hash(peer_integrity_tfm); |
Philipp Reisner | 036b17e | 2011-05-16 17:38:11 +0200 | [diff] [blame] | 3208 | kfree(int_dig_in); |
| 3209 | kfree(int_dig_vv); |
Philipp Reisner | 7204624 | 2011-03-15 18:51:47 +0100 | [diff] [blame] | 3210 | conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3211 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3212 | } |
| 3213 | |
| 3214 | /* helper function |
| 3215 | * input: alg name, feature name |
| 3216 | * return: NULL (alg name was "") |
| 3217 | * ERR_PTR(error) if something goes wrong |
| 3218 | * or the crypto hash ptr, if it worked out ok. */ |
| 3219 | struct crypto_hash *drbd_crypto_alloc_digest_safe(const struct drbd_conf *mdev, |
| 3220 | const char *alg, const char *name) |
| 3221 | { |
| 3222 | struct crypto_hash *tfm; |
| 3223 | |
| 3224 | if (!alg[0]) |
| 3225 | return NULL; |
| 3226 | |
| 3227 | tfm = crypto_alloc_hash(alg, 0, CRYPTO_ALG_ASYNC); |
| 3228 | if (IS_ERR(tfm)) { |
| 3229 | dev_err(DEV, "Can not allocate \"%s\" as %s (reason: %ld)\n", |
| 3230 | alg, name, PTR_ERR(tfm)); |
| 3231 | return tfm; |
| 3232 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3233 | return tfm; |
| 3234 | } |
| 3235 | |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 3236 | static int ignore_remaining_packet(struct drbd_tconn *tconn, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3237 | { |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 3238 | void *buffer = tconn->data.rbuf; |
| 3239 | int size = pi->size; |
| 3240 | |
| 3241 | while (size) { |
| 3242 | int s = min_t(int, size, DRBD_SOCKET_BUFFER_SIZE); |
| 3243 | s = drbd_recv(tconn, buffer, s); |
| 3244 | if (s <= 0) { |
| 3245 | if (s < 0) |
| 3246 | return s; |
| 3247 | break; |
| 3248 | } |
| 3249 | size -= s; |
| 3250 | } |
| 3251 | if (size) |
| 3252 | return -EIO; |
| 3253 | return 0; |
| 3254 | } |
| 3255 | |
| 3256 | /* |
| 3257 | * config_unknown_volume - device configuration command for unknown volume |
| 3258 | * |
| 3259 | * When a device is added to an existing connection, the node on which the |
| 3260 | * device is added first will send configuration commands to its peer but the |
| 3261 | * peer will not know about the device yet. It will warn and ignore these |
| 3262 | * commands. Once the device is added on the second node, the second node will |
| 3263 | * send the same device configuration commands, but in the other direction. |
| 3264 | * |
| 3265 | * (We can also end up here if drbd is misconfigured.) |
| 3266 | */ |
| 3267 | static int config_unknown_volume(struct drbd_tconn *tconn, struct packet_info *pi) |
| 3268 | { |
Andreas Gruenbacher | 2fcb8f3 | 2011-07-03 11:41:08 +0200 | [diff] [blame] | 3269 | conn_warn(tconn, "%s packet received for volume %u, which is not configured locally\n", |
| 3270 | cmdname(pi->cmd), pi->vnr); |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 3271 | return ignore_remaining_packet(tconn, pi); |
| 3272 | } |
| 3273 | |
| 3274 | static int receive_SyncParam(struct drbd_tconn *tconn, struct packet_info *pi) |
| 3275 | { |
| 3276 | struct drbd_conf *mdev; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 3277 | struct p_rs_param_95 *p; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3278 | unsigned int header_size, data_size, exp_max_sz; |
| 3279 | struct crypto_hash *verify_tfm = NULL; |
| 3280 | struct crypto_hash *csums_tfm = NULL; |
Philipp Reisner | 2ec91e0 | 2011-05-03 14:58:00 +0200 | [diff] [blame] | 3281 | struct net_conf *old_net_conf, *new_net_conf = NULL; |
Philipp Reisner | 813472c | 2011-05-03 16:47:02 +0200 | [diff] [blame] | 3282 | struct disk_conf *old_disk_conf = NULL, *new_disk_conf = NULL; |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 3283 | const int apv = tconn->agreed_pro_version; |
Philipp Reisner | 813472c | 2011-05-03 16:47:02 +0200 | [diff] [blame] | 3284 | struct fifo_buffer *old_plan = NULL, *new_plan = NULL; |
Philipp Reisner | 778f271 | 2010-07-06 11:14:00 +0200 | [diff] [blame] | 3285 | int fifo_size = 0; |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3286 | int err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3287 | |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 3288 | mdev = vnr_to_mdev(tconn, pi->vnr); |
| 3289 | if (!mdev) |
| 3290 | return config_unknown_volume(tconn, pi); |
| 3291 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3292 | exp_max_sz = apv <= 87 ? sizeof(struct p_rs_param) |
| 3293 | : apv == 88 ? sizeof(struct p_rs_param) |
| 3294 | + SHARED_SECRET_MAX |
Philipp Reisner | 8e26f9c | 2010-07-06 17:25:54 +0200 | [diff] [blame] | 3295 | : apv <= 94 ? sizeof(struct p_rs_param_89) |
| 3296 | : /* apv >= 95 */ sizeof(struct p_rs_param_95); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3297 | |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 3298 | if (pi->size > exp_max_sz) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3299 | dev_err(DEV, "SyncParam packet too long: received %u, expected <= %u bytes\n", |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 3300 | pi->size, exp_max_sz); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3301 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3302 | } |
| 3303 | |
| 3304 | if (apv <= 88) { |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 3305 | header_size = sizeof(struct p_rs_param); |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 3306 | data_size = pi->size - header_size; |
Philipp Reisner | 8e26f9c | 2010-07-06 17:25:54 +0200 | [diff] [blame] | 3307 | } else if (apv <= 94) { |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 3308 | header_size = sizeof(struct p_rs_param_89); |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 3309 | data_size = pi->size - header_size; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3310 | D_ASSERT(data_size == 0); |
Philipp Reisner | 8e26f9c | 2010-07-06 17:25:54 +0200 | [diff] [blame] | 3311 | } else { |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 3312 | header_size = sizeof(struct p_rs_param_95); |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 3313 | data_size = pi->size - header_size; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3314 | D_ASSERT(data_size == 0); |
| 3315 | } |
| 3316 | |
| 3317 | /* initialize verify_alg and csums_alg */ |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 3318 | p = pi->data; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3319 | memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX); |
| 3320 | |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 3321 | err = drbd_recv_all(mdev->tconn, p, header_size); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3322 | if (err) |
| 3323 | return err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3324 | |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 3325 | mutex_lock(&mdev->tconn->conf_update); |
| 3326 | old_net_conf = mdev->tconn->net_conf; |
Philipp Reisner | 813472c | 2011-05-03 16:47:02 +0200 | [diff] [blame] | 3327 | if (get_ldev(mdev)) { |
| 3328 | new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL); |
| 3329 | if (!new_disk_conf) { |
| 3330 | put_ldev(mdev); |
| 3331 | mutex_unlock(&mdev->tconn->conf_update); |
| 3332 | dev_err(DEV, "Allocation of new disk_conf failed\n"); |
| 3333 | return -ENOMEM; |
| 3334 | } |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 3335 | |
Philipp Reisner | 813472c | 2011-05-03 16:47:02 +0200 | [diff] [blame] | 3336 | old_disk_conf = mdev->ldev->disk_conf; |
| 3337 | *new_disk_conf = *old_disk_conf; |
| 3338 | |
Andreas Gruenbacher | 6394b93 | 2011-05-11 14:29:52 +0200 | [diff] [blame] | 3339 | new_disk_conf->resync_rate = be32_to_cpu(p->resync_rate); |
Philipp Reisner | 813472c | 2011-05-03 16:47:02 +0200 | [diff] [blame] | 3340 | } |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 3341 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3342 | if (apv >= 88) { |
| 3343 | if (apv == 88) { |
Philipp Reisner | e4bad1b | 2012-04-06 12:08:51 +0200 | [diff] [blame] | 3344 | if (data_size > SHARED_SECRET_MAX || data_size == 0) { |
| 3345 | dev_err(DEV, "verify-alg of wrong size, " |
| 3346 | "peer wants %u, accepting only up to %u byte\n", |
| 3347 | data_size, SHARED_SECRET_MAX); |
Philipp Reisner | 813472c | 2011-05-03 16:47:02 +0200 | [diff] [blame] | 3348 | err = -EIO; |
| 3349 | goto reconnect; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3350 | } |
| 3351 | |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3352 | err = drbd_recv_all(mdev->tconn, p->verify_alg, data_size); |
Philipp Reisner | 813472c | 2011-05-03 16:47:02 +0200 | [diff] [blame] | 3353 | if (err) |
| 3354 | goto reconnect; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3355 | /* we expect NUL terminated string */ |
| 3356 | /* but just in case someone tries to be evil */ |
| 3357 | D_ASSERT(p->verify_alg[data_size-1] == 0); |
| 3358 | p->verify_alg[data_size-1] = 0; |
| 3359 | |
| 3360 | } else /* apv >= 89 */ { |
| 3361 | /* we still expect NUL terminated strings */ |
| 3362 | /* but just in case someone tries to be evil */ |
| 3363 | D_ASSERT(p->verify_alg[SHARED_SECRET_MAX-1] == 0); |
| 3364 | D_ASSERT(p->csums_alg[SHARED_SECRET_MAX-1] == 0); |
| 3365 | p->verify_alg[SHARED_SECRET_MAX-1] = 0; |
| 3366 | p->csums_alg[SHARED_SECRET_MAX-1] = 0; |
| 3367 | } |
| 3368 | |
Philipp Reisner | 2ec91e0 | 2011-05-03 14:58:00 +0200 | [diff] [blame] | 3369 | if (strcmp(old_net_conf->verify_alg, p->verify_alg)) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3370 | if (mdev->state.conn == C_WF_REPORT_PARAMS) { |
| 3371 | dev_err(DEV, "Different verify-alg settings. me=\"%s\" peer=\"%s\"\n", |
Philipp Reisner | 2ec91e0 | 2011-05-03 14:58:00 +0200 | [diff] [blame] | 3372 | old_net_conf->verify_alg, p->verify_alg); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3373 | goto disconnect; |
| 3374 | } |
| 3375 | verify_tfm = drbd_crypto_alloc_digest_safe(mdev, |
| 3376 | p->verify_alg, "verify-alg"); |
| 3377 | if (IS_ERR(verify_tfm)) { |
| 3378 | verify_tfm = NULL; |
| 3379 | goto disconnect; |
| 3380 | } |
| 3381 | } |
| 3382 | |
Philipp Reisner | 2ec91e0 | 2011-05-03 14:58:00 +0200 | [diff] [blame] | 3383 | if (apv >= 89 && strcmp(old_net_conf->csums_alg, p->csums_alg)) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3384 | if (mdev->state.conn == C_WF_REPORT_PARAMS) { |
| 3385 | dev_err(DEV, "Different csums-alg settings. me=\"%s\" peer=\"%s\"\n", |
Philipp Reisner | 2ec91e0 | 2011-05-03 14:58:00 +0200 | [diff] [blame] | 3386 | old_net_conf->csums_alg, p->csums_alg); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3387 | goto disconnect; |
| 3388 | } |
| 3389 | csums_tfm = drbd_crypto_alloc_digest_safe(mdev, |
| 3390 | p->csums_alg, "csums-alg"); |
| 3391 | if (IS_ERR(csums_tfm)) { |
| 3392 | csums_tfm = NULL; |
| 3393 | goto disconnect; |
| 3394 | } |
| 3395 | } |
| 3396 | |
Philipp Reisner | 813472c | 2011-05-03 16:47:02 +0200 | [diff] [blame] | 3397 | if (apv > 94 && new_disk_conf) { |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 3398 | new_disk_conf->c_plan_ahead = be32_to_cpu(p->c_plan_ahead); |
| 3399 | new_disk_conf->c_delay_target = be32_to_cpu(p->c_delay_target); |
| 3400 | new_disk_conf->c_fill_target = be32_to_cpu(p->c_fill_target); |
| 3401 | new_disk_conf->c_max_rate = be32_to_cpu(p->c_max_rate); |
Philipp Reisner | 778f271 | 2010-07-06 11:14:00 +0200 | [diff] [blame] | 3402 | |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 3403 | fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ; |
Philipp Reisner | 9958c85 | 2011-05-03 16:19:31 +0200 | [diff] [blame] | 3404 | if (fifo_size != mdev->rs_plan_s->size) { |
Philipp Reisner | 813472c | 2011-05-03 16:47:02 +0200 | [diff] [blame] | 3405 | new_plan = fifo_alloc(fifo_size); |
| 3406 | if (!new_plan) { |
Philipp Reisner | 778f271 | 2010-07-06 11:14:00 +0200 | [diff] [blame] | 3407 | dev_err(DEV, "kmalloc of fifo_buffer failed"); |
Lars Ellenberg | f399002 | 2011-03-23 14:31:09 +0100 | [diff] [blame] | 3408 | put_ldev(mdev); |
Philipp Reisner | 778f271 | 2010-07-06 11:14:00 +0200 | [diff] [blame] | 3409 | goto disconnect; |
| 3410 | } |
| 3411 | } |
Philipp Reisner | 8e26f9c | 2010-07-06 17:25:54 +0200 | [diff] [blame] | 3412 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3413 | |
Philipp Reisner | 91fd4da | 2011-04-20 17:47:29 +0200 | [diff] [blame] | 3414 | if (verify_tfm || csums_tfm) { |
Philipp Reisner | 2ec91e0 | 2011-05-03 14:58:00 +0200 | [diff] [blame] | 3415 | new_net_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL); |
| 3416 | if (!new_net_conf) { |
Philipp Reisner | 91fd4da | 2011-04-20 17:47:29 +0200 | [diff] [blame] | 3417 | dev_err(DEV, "Allocation of new net_conf failed\n"); |
| 3418 | goto disconnect; |
| 3419 | } |
| 3420 | |
Philipp Reisner | 2ec91e0 | 2011-05-03 14:58:00 +0200 | [diff] [blame] | 3421 | *new_net_conf = *old_net_conf; |
Philipp Reisner | 91fd4da | 2011-04-20 17:47:29 +0200 | [diff] [blame] | 3422 | |
| 3423 | if (verify_tfm) { |
Philipp Reisner | 2ec91e0 | 2011-05-03 14:58:00 +0200 | [diff] [blame] | 3424 | strcpy(new_net_conf->verify_alg, p->verify_alg); |
| 3425 | new_net_conf->verify_alg_len = strlen(p->verify_alg) + 1; |
Philipp Reisner | 91fd4da | 2011-04-20 17:47:29 +0200 | [diff] [blame] | 3426 | crypto_free_hash(mdev->tconn->verify_tfm); |
| 3427 | mdev->tconn->verify_tfm = verify_tfm; |
| 3428 | dev_info(DEV, "using verify-alg: \"%s\"\n", p->verify_alg); |
| 3429 | } |
| 3430 | if (csums_tfm) { |
Philipp Reisner | 2ec91e0 | 2011-05-03 14:58:00 +0200 | [diff] [blame] | 3431 | strcpy(new_net_conf->csums_alg, p->csums_alg); |
| 3432 | new_net_conf->csums_alg_len = strlen(p->csums_alg) + 1; |
Philipp Reisner | 91fd4da | 2011-04-20 17:47:29 +0200 | [diff] [blame] | 3433 | crypto_free_hash(mdev->tconn->csums_tfm); |
| 3434 | mdev->tconn->csums_tfm = csums_tfm; |
| 3435 | dev_info(DEV, "using csums-alg: \"%s\"\n", p->csums_alg); |
| 3436 | } |
Philipp Reisner | 2ec91e0 | 2011-05-03 14:58:00 +0200 | [diff] [blame] | 3437 | rcu_assign_pointer(tconn->net_conf, new_net_conf); |
Philipp Reisner | 91fd4da | 2011-04-20 17:47:29 +0200 | [diff] [blame] | 3438 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3439 | } |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 3440 | |
Philipp Reisner | 813472c | 2011-05-03 16:47:02 +0200 | [diff] [blame] | 3441 | if (new_disk_conf) { |
| 3442 | rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf); |
| 3443 | put_ldev(mdev); |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 3444 | } |
Philipp Reisner | 813472c | 2011-05-03 16:47:02 +0200 | [diff] [blame] | 3445 | |
| 3446 | if (new_plan) { |
| 3447 | old_plan = mdev->rs_plan_s; |
| 3448 | rcu_assign_pointer(mdev->rs_plan_s, new_plan); |
| 3449 | } |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 3450 | |
| 3451 | mutex_unlock(&mdev->tconn->conf_update); |
| 3452 | synchronize_rcu(); |
| 3453 | if (new_net_conf) |
| 3454 | kfree(old_net_conf); |
| 3455 | kfree(old_disk_conf); |
Philipp Reisner | 813472c | 2011-05-03 16:47:02 +0200 | [diff] [blame] | 3456 | kfree(old_plan); |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 3457 | |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3458 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3459 | |
Philipp Reisner | 813472c | 2011-05-03 16:47:02 +0200 | [diff] [blame] | 3460 | reconnect: |
| 3461 | if (new_disk_conf) { |
| 3462 | put_ldev(mdev); |
| 3463 | kfree(new_disk_conf); |
| 3464 | } |
| 3465 | mutex_unlock(&mdev->tconn->conf_update); |
| 3466 | return -EIO; |
| 3467 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3468 | disconnect: |
Philipp Reisner | 813472c | 2011-05-03 16:47:02 +0200 | [diff] [blame] | 3469 | kfree(new_plan); |
| 3470 | if (new_disk_conf) { |
| 3471 | put_ldev(mdev); |
| 3472 | kfree(new_disk_conf); |
| 3473 | } |
Philipp Reisner | a009550 | 2011-05-03 13:14:15 +0200 | [diff] [blame] | 3474 | mutex_unlock(&mdev->tconn->conf_update); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3475 | /* just for completeness: actually not needed, |
| 3476 | * as this is not reached if csums_tfm was ok. */ |
| 3477 | crypto_free_hash(csums_tfm); |
| 3478 | /* but free the verify_tfm again, if csums_tfm did not work out */ |
| 3479 | crypto_free_hash(verify_tfm); |
Philipp Reisner | 38fa998 | 2011-03-15 18:24:49 +0100 | [diff] [blame] | 3480 | conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3481 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3482 | } |
| 3483 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3484 | /* warn if the arguments differ by more than 12.5% */ |
| 3485 | static void warn_if_differ_considerably(struct drbd_conf *mdev, |
| 3486 | const char *s, sector_t a, sector_t b) |
| 3487 | { |
| 3488 | sector_t d; |
| 3489 | if (a == 0 || b == 0) |
| 3490 | return; |
| 3491 | d = (a > b) ? (a - b) : (b - a); |
| 3492 | if (d > (a>>3) || d > (b>>3)) |
| 3493 | dev_warn(DEV, "Considerable difference in %s: %llus vs. %llus\n", s, |
| 3494 | (unsigned long long)a, (unsigned long long)b); |
| 3495 | } |
| 3496 | |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 3497 | static int receive_sizes(struct drbd_tconn *tconn, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3498 | { |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 3499 | struct drbd_conf *mdev; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 3500 | struct p_sizes *p = pi->data; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3501 | enum determine_dev_size dd = unchanged; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3502 | sector_t p_size, p_usize, my_usize; |
| 3503 | int ldsc = 0; /* local disk size changed */ |
Philipp Reisner | e89b591 | 2010-03-24 17:11:33 +0100 | [diff] [blame] | 3504 | enum dds_flags ddsf; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3505 | |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 3506 | mdev = vnr_to_mdev(tconn, pi->vnr); |
| 3507 | if (!mdev) |
| 3508 | return config_unknown_volume(tconn, pi); |
| 3509 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3510 | p_size = be64_to_cpu(p->d_size); |
| 3511 | p_usize = be64_to_cpu(p->u_size); |
| 3512 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3513 | /* just store the peer's disk size for now. |
| 3514 | * we still need to figure out whether we accept that. */ |
| 3515 | mdev->p_size = p_size; |
| 3516 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3517 | if (get_ldev(mdev)) { |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 3518 | rcu_read_lock(); |
| 3519 | my_usize = rcu_dereference(mdev->ldev->disk_conf)->disk_size; |
| 3520 | rcu_read_unlock(); |
| 3521 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3522 | warn_if_differ_considerably(mdev, "lower level device sizes", |
| 3523 | p_size, drbd_get_max_capacity(mdev->ldev)); |
| 3524 | warn_if_differ_considerably(mdev, "user requested size", |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 3525 | p_usize, my_usize); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3526 | |
| 3527 | /* if this is the first connect, or an otherwise expected |
| 3528 | * param exchange, choose the minimum */ |
| 3529 | if (mdev->state.conn == C_WF_REPORT_PARAMS) |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 3530 | p_usize = min_not_zero(my_usize, p_usize); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3531 | |
| 3532 | /* Never shrink a device with usable data during connect. |
| 3533 | But allow online shrinking if we are connected. */ |
Philipp Reisner | ef5e44a | 2011-05-03 13:27:43 +0200 | [diff] [blame] | 3534 | if (drbd_new_dev_size(mdev, mdev->ldev, p_usize, 0) < |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 3535 | drbd_get_capacity(mdev->this_bdev) && |
| 3536 | mdev->state.disk >= D_OUTDATED && |
| 3537 | mdev->state.conn < C_CONNECTED) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3538 | dev_err(DEV, "The peer's disk size is too small!\n"); |
Philipp Reisner | 38fa998 | 2011-03-15 18:24:49 +0100 | [diff] [blame] | 3539 | conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3540 | put_ldev(mdev); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3541 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3542 | } |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 3543 | |
| 3544 | if (my_usize != p_usize) { |
| 3545 | struct disk_conf *old_disk_conf, *new_disk_conf = NULL; |
| 3546 | |
| 3547 | new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL); |
| 3548 | if (!new_disk_conf) { |
| 3549 | dev_err(DEV, "Allocation of new disk_conf failed\n"); |
| 3550 | put_ldev(mdev); |
| 3551 | return -ENOMEM; |
| 3552 | } |
| 3553 | |
| 3554 | mutex_lock(&mdev->tconn->conf_update); |
| 3555 | old_disk_conf = mdev->ldev->disk_conf; |
| 3556 | *new_disk_conf = *old_disk_conf; |
| 3557 | new_disk_conf->disk_size = p_usize; |
| 3558 | |
| 3559 | rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf); |
| 3560 | mutex_unlock(&mdev->tconn->conf_update); |
| 3561 | synchronize_rcu(); |
| 3562 | kfree(old_disk_conf); |
| 3563 | |
| 3564 | dev_info(DEV, "Peer sets u_size to %lu sectors\n", |
| 3565 | (unsigned long)my_usize); |
| 3566 | } |
| 3567 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3568 | put_ldev(mdev); |
| 3569 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3570 | |
Philipp Reisner | e89b591 | 2010-03-24 17:11:33 +0100 | [diff] [blame] | 3571 | ddsf = be16_to_cpu(p->dds_flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3572 | if (get_ldev(mdev)) { |
Bart Van Assche | 24c4830 | 2011-05-21 18:32:29 +0200 | [diff] [blame] | 3573 | dd = drbd_determine_dev_size(mdev, ddsf); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3574 | put_ldev(mdev); |
| 3575 | if (dd == dev_size_error) |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3576 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3577 | drbd_md_sync(mdev); |
| 3578 | } else { |
| 3579 | /* I am diskless, need to accept the peer's size. */ |
| 3580 | drbd_set_my_capacity(mdev, p_size); |
| 3581 | } |
| 3582 | |
Philipp Reisner | 99432fc | 2011-05-20 16:39:13 +0200 | [diff] [blame] | 3583 | mdev->peer_max_bio_size = be32_to_cpu(p->max_bio_size); |
| 3584 | drbd_reconsider_max_bio_size(mdev); |
| 3585 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3586 | if (get_ldev(mdev)) { |
| 3587 | if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev)) { |
| 3588 | mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev); |
| 3589 | ldsc = 1; |
| 3590 | } |
| 3591 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3592 | put_ldev(mdev); |
| 3593 | } |
| 3594 | |
| 3595 | if (mdev->state.conn > C_WF_REPORT_PARAMS) { |
| 3596 | if (be64_to_cpu(p->c_size) != |
| 3597 | drbd_get_capacity(mdev->this_bdev) || ldsc) { |
| 3598 | /* we have different sizes, probably peer |
| 3599 | * needs to know my new size... */ |
Philipp Reisner | e89b591 | 2010-03-24 17:11:33 +0100 | [diff] [blame] | 3600 | drbd_send_sizes(mdev, 0, ddsf); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3601 | } |
| 3602 | if (test_and_clear_bit(RESIZE_PENDING, &mdev->flags) || |
| 3603 | (dd == grew && mdev->state.conn == C_CONNECTED)) { |
| 3604 | if (mdev->state.pdsk >= D_INCONSISTENT && |
Philipp Reisner | e89b591 | 2010-03-24 17:11:33 +0100 | [diff] [blame] | 3605 | mdev->state.disk >= D_INCONSISTENT) { |
| 3606 | if (ddsf & DDSF_NO_RESYNC) |
| 3607 | dev_info(DEV, "Resync of new storage suppressed with --assume-clean\n"); |
| 3608 | else |
| 3609 | resync_after_online_grow(mdev); |
| 3610 | } else |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3611 | set_bit(RESYNC_AFTER_NEG, &mdev->flags); |
| 3612 | } |
| 3613 | } |
| 3614 | |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3615 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3616 | } |
| 3617 | |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 3618 | static int receive_uuids(struct drbd_tconn *tconn, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3619 | { |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 3620 | struct drbd_conf *mdev; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 3621 | struct p_uuids *p = pi->data; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3622 | u64 *p_uuid; |
Lars Ellenberg | 62b0da3 | 2011-01-20 13:25:21 +0100 | [diff] [blame] | 3623 | int i, updated_uuids = 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3624 | |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 3625 | mdev = vnr_to_mdev(tconn, pi->vnr); |
| 3626 | if (!mdev) |
| 3627 | return config_unknown_volume(tconn, pi); |
| 3628 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3629 | p_uuid = kmalloc(sizeof(u64)*UI_EXTENDED_SIZE, GFP_NOIO); |
| 3630 | |
| 3631 | for (i = UI_CURRENT; i < UI_EXTENDED_SIZE; i++) |
| 3632 | p_uuid[i] = be64_to_cpu(p->uuid[i]); |
| 3633 | |
| 3634 | kfree(mdev->p_uuid); |
| 3635 | mdev->p_uuid = p_uuid; |
| 3636 | |
| 3637 | if (mdev->state.conn < C_CONNECTED && |
| 3638 | mdev->state.disk < D_INCONSISTENT && |
| 3639 | mdev->state.role == R_PRIMARY && |
| 3640 | (mdev->ed_uuid & ~((u64)1)) != (p_uuid[UI_CURRENT] & ~((u64)1))) { |
| 3641 | dev_err(DEV, "Can only connect to data with current UUID=%016llX\n", |
| 3642 | (unsigned long long)mdev->ed_uuid); |
Philipp Reisner | 38fa998 | 2011-03-15 18:24:49 +0100 | [diff] [blame] | 3643 | conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3644 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3645 | } |
| 3646 | |
| 3647 | if (get_ldev(mdev)) { |
| 3648 | int skip_initial_sync = |
| 3649 | mdev->state.conn == C_CONNECTED && |
Philipp Reisner | 31890f4 | 2011-01-19 14:12:51 +0100 | [diff] [blame] | 3650 | mdev->tconn->agreed_pro_version >= 90 && |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3651 | mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && |
| 3652 | (p_uuid[UI_FLAGS] & 8); |
| 3653 | if (skip_initial_sync) { |
| 3654 | dev_info(DEV, "Accepted new current UUID, preparing to skip initial sync\n"); |
| 3655 | drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write, |
Lars Ellenberg | 20ceb2b | 2011-01-21 10:56:44 +0100 | [diff] [blame] | 3656 | "clear_n_write from receive_uuids", |
| 3657 | BM_LOCKED_TEST_ALLOWED); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3658 | _drbd_uuid_set(mdev, UI_CURRENT, p_uuid[UI_CURRENT]); |
| 3659 | _drbd_uuid_set(mdev, UI_BITMAP, 0); |
| 3660 | _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE), |
| 3661 | CS_VERBOSE, NULL); |
| 3662 | drbd_md_sync(mdev); |
Lars Ellenberg | 62b0da3 | 2011-01-20 13:25:21 +0100 | [diff] [blame] | 3663 | updated_uuids = 1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3664 | } |
| 3665 | put_ldev(mdev); |
Philipp Reisner | 18a50fa | 2010-06-21 14:14:15 +0200 | [diff] [blame] | 3666 | } else if (mdev->state.disk < D_INCONSISTENT && |
| 3667 | mdev->state.role == R_PRIMARY) { |
| 3668 | /* I am a diskless primary, the peer just created a new current UUID |
| 3669 | for me. */ |
Lars Ellenberg | 62b0da3 | 2011-01-20 13:25:21 +0100 | [diff] [blame] | 3670 | updated_uuids = drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3671 | } |
| 3672 | |
| 3673 | /* Before we test for the disk state, we should wait until an eventually |
| 3674 | ongoing cluster wide state change is finished. That is important if |
| 3675 | we are primary and are detaching from our disk. We need to see the |
| 3676 | new disk state... */ |
Philipp Reisner | 8410da8 | 2011-02-11 20:11:10 +0100 | [diff] [blame] | 3677 | mutex_lock(mdev->state_mutex); |
| 3678 | mutex_unlock(mdev->state_mutex); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3679 | if (mdev->state.conn >= C_CONNECTED && mdev->state.disk < D_INCONSISTENT) |
Lars Ellenberg | 62b0da3 | 2011-01-20 13:25:21 +0100 | [diff] [blame] | 3680 | updated_uuids |= drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]); |
| 3681 | |
| 3682 | if (updated_uuids) |
| 3683 | drbd_print_uuids(mdev, "receiver updated UUIDs to"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3684 | |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3685 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3686 | } |
| 3687 | |
| 3688 | /** |
| 3689 | * convert_state() - Converts the peer's view of the cluster state to our point of view |
| 3690 | * @ps: The state as seen by the peer. |
| 3691 | */ |
| 3692 | static union drbd_state convert_state(union drbd_state ps) |
| 3693 | { |
| 3694 | union drbd_state ms; |
| 3695 | |
| 3696 | static enum drbd_conns c_tab[] = { |
Philipp Reisner | 369bea6 | 2011-07-06 23:04:44 +0200 | [diff] [blame] | 3697 | [C_WF_REPORT_PARAMS] = C_WF_REPORT_PARAMS, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3698 | [C_CONNECTED] = C_CONNECTED, |
| 3699 | |
| 3700 | [C_STARTING_SYNC_S] = C_STARTING_SYNC_T, |
| 3701 | [C_STARTING_SYNC_T] = C_STARTING_SYNC_S, |
| 3702 | [C_DISCONNECTING] = C_TEAR_DOWN, /* C_NETWORK_FAILURE, */ |
| 3703 | [C_VERIFY_S] = C_VERIFY_T, |
| 3704 | [C_MASK] = C_MASK, |
| 3705 | }; |
| 3706 | |
| 3707 | ms.i = ps.i; |
| 3708 | |
| 3709 | ms.conn = c_tab[ps.conn]; |
| 3710 | ms.peer = ps.role; |
| 3711 | ms.role = ps.peer; |
| 3712 | ms.pdsk = ps.disk; |
| 3713 | ms.disk = ps.pdsk; |
| 3714 | ms.peer_isp = (ps.aftr_isp | ps.user_isp); |
| 3715 | |
| 3716 | return ms; |
| 3717 | } |
| 3718 | |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 3719 | static int receive_req_state(struct drbd_tconn *tconn, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3720 | { |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 3721 | struct drbd_conf *mdev; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 3722 | struct p_req_state *p = pi->data; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3723 | union drbd_state mask, val; |
Andreas Gruenbacher | bf885f8 | 2010-12-08 00:39:32 +0100 | [diff] [blame] | 3724 | enum drbd_state_rv rv; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3725 | |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 3726 | mdev = vnr_to_mdev(tconn, pi->vnr); |
| 3727 | if (!mdev) |
| 3728 | return -EIO; |
| 3729 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3730 | mask.i = be32_to_cpu(p->mask); |
| 3731 | val.i = be32_to_cpu(p->val); |
| 3732 | |
Philipp Reisner | 25703f8 | 2011-02-07 14:35:25 +0100 | [diff] [blame] | 3733 | if (test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags) && |
Philipp Reisner | 8410da8 | 2011-02-11 20:11:10 +0100 | [diff] [blame] | 3734 | mutex_is_locked(mdev->state_mutex)) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3735 | drbd_send_sr_reply(mdev, SS_CONCURRENT_ST_CHG); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3736 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3737 | } |
| 3738 | |
| 3739 | mask = convert_state(mask); |
| 3740 | val = convert_state(val); |
| 3741 | |
Philipp Reisner | dfafcc8 | 2011-03-16 10:55:07 +0100 | [diff] [blame] | 3742 | rv = drbd_change_state(mdev, CS_VERBOSE, mask, val); |
| 3743 | drbd_send_sr_reply(mdev, rv); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3744 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3745 | drbd_md_sync(mdev); |
| 3746 | |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3747 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3748 | } |
| 3749 | |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 3750 | static int receive_req_conn_state(struct drbd_tconn *tconn, struct packet_info *pi) |
Philipp Reisner | dfafcc8 | 2011-03-16 10:55:07 +0100 | [diff] [blame] | 3751 | { |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 3752 | struct p_req_state *p = pi->data; |
Philipp Reisner | dfafcc8 | 2011-03-16 10:55:07 +0100 | [diff] [blame] | 3753 | union drbd_state mask, val; |
| 3754 | enum drbd_state_rv rv; |
| 3755 | |
| 3756 | mask.i = be32_to_cpu(p->mask); |
| 3757 | val.i = be32_to_cpu(p->val); |
| 3758 | |
| 3759 | if (test_bit(DISCARD_CONCURRENT, &tconn->flags) && |
| 3760 | mutex_is_locked(&tconn->cstate_mutex)) { |
| 3761 | conn_send_sr_reply(tconn, SS_CONCURRENT_ST_CHG); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3762 | return 0; |
Philipp Reisner | dfafcc8 | 2011-03-16 10:55:07 +0100 | [diff] [blame] | 3763 | } |
| 3764 | |
| 3765 | mask = convert_state(mask); |
| 3766 | val = convert_state(val); |
| 3767 | |
Philipp Reisner | 778bcf2 | 2011-03-28 12:55:03 +0200 | [diff] [blame] | 3768 | rv = conn_request_state(tconn, mask, val, CS_VERBOSE | CS_LOCAL_ONLY | CS_IGN_OUTD_FAIL); |
Philipp Reisner | dfafcc8 | 2011-03-16 10:55:07 +0100 | [diff] [blame] | 3769 | conn_send_sr_reply(tconn, rv); |
| 3770 | |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3771 | return 0; |
Philipp Reisner | dfafcc8 | 2011-03-16 10:55:07 +0100 | [diff] [blame] | 3772 | } |
| 3773 | |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 3774 | static int receive_state(struct drbd_tconn *tconn, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3775 | { |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 3776 | struct drbd_conf *mdev; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 3777 | struct p_state *p = pi->data; |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3778 | union drbd_state os, ns, peer_state; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3779 | enum drbd_disk_state real_peer_disk; |
Philipp Reisner | 65d922c | 2010-06-16 16:18:09 +0200 | [diff] [blame] | 3780 | enum chg_state_flags cs_flags; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3781 | int rv; |
| 3782 | |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 3783 | mdev = vnr_to_mdev(tconn, pi->vnr); |
| 3784 | if (!mdev) |
| 3785 | return config_unknown_volume(tconn, pi); |
| 3786 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3787 | peer_state.i = be32_to_cpu(p->state); |
| 3788 | |
| 3789 | real_peer_disk = peer_state.disk; |
| 3790 | if (peer_state.disk == D_NEGOTIATING) { |
| 3791 | real_peer_disk = mdev->p_uuid[UI_FLAGS] & 4 ? D_INCONSISTENT : D_CONSISTENT; |
| 3792 | dev_info(DEV, "real peer disk state = %s\n", drbd_disk_str(real_peer_disk)); |
| 3793 | } |
| 3794 | |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 3795 | spin_lock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3796 | retry: |
Philipp Reisner | 78bae59 | 2011-03-28 15:40:12 +0200 | [diff] [blame] | 3797 | os = ns = drbd_read_state(mdev); |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 3798 | spin_unlock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3799 | |
Philipp Reisner | b8853db | 2011-12-13 11:09:16 +0100 | [diff] [blame] | 3800 | /* If some other part of the code (asender thread, timeout) |
| 3801 | * already decided to close the connection again, |
| 3802 | * we must not "re-establish" it here. */ |
| 3803 | if (os.conn <= C_TEAR_DOWN) |
| 3804 | return false; |
| 3805 | |
Philipp Reisner | 9bcd252 | 2011-09-29 13:00:14 +0200 | [diff] [blame] | 3806 | /* If this is the "end of sync" confirmation, usually the peer disk |
| 3807 | * transitions from D_INCONSISTENT to D_UP_TO_DATE. For empty (0 bits |
| 3808 | * set) resync started in PausedSyncT, or if the timing of pause-/ |
| 3809 | * unpause-sync events has been "just right", the peer disk may |
| 3810 | * transition from D_CONSISTENT to D_UP_TO_DATE as well. |
| 3811 | */ |
| 3812 | if ((os.pdsk == D_INCONSISTENT || os.pdsk == D_CONSISTENT) && |
| 3813 | real_peer_disk == D_UP_TO_DATE && |
Lars Ellenberg | e9ef7bb | 2010-10-07 15:55:39 +0200 | [diff] [blame] | 3814 | os.conn > C_CONNECTED && os.disk == D_UP_TO_DATE) { |
| 3815 | /* If we are (becoming) SyncSource, but peer is still in sync |
| 3816 | * preparation, ignore its uptodate-ness to avoid flapping, it |
| 3817 | * will change to inconsistent once the peer reaches active |
| 3818 | * syncing states. |
| 3819 | * It may have changed syncer-paused flags, however, so we |
| 3820 | * cannot ignore this completely. */ |
| 3821 | if (peer_state.conn > C_CONNECTED && |
| 3822 | peer_state.conn < C_SYNC_SOURCE) |
| 3823 | real_peer_disk = D_INCONSISTENT; |
| 3824 | |
| 3825 | /* if peer_state changes to connected at the same time, |
| 3826 | * it explicitly notifies us that it finished resync. |
| 3827 | * Maybe we should finish it up, too? */ |
| 3828 | else if (os.conn >= C_SYNC_SOURCE && |
| 3829 | peer_state.conn == C_CONNECTED) { |
| 3830 | if (drbd_bm_total_weight(mdev) <= mdev->rs_failed) |
| 3831 | drbd_resync_finished(mdev); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3832 | return 0; |
Lars Ellenberg | e9ef7bb | 2010-10-07 15:55:39 +0200 | [diff] [blame] | 3833 | } |
| 3834 | } |
| 3835 | |
| 3836 | /* peer says his disk is inconsistent, while we think it is uptodate, |
| 3837 | * and this happens while the peer still thinks we have a sync going on, |
| 3838 | * but we think we are already done with the sync. |
| 3839 | * We ignore this to avoid flapping pdsk. |
| 3840 | * This should not happen, if the peer is a recent version of drbd. */ |
| 3841 | if (os.pdsk == D_UP_TO_DATE && real_peer_disk == D_INCONSISTENT && |
| 3842 | os.conn == C_CONNECTED && peer_state.conn > C_SYNC_SOURCE) |
| 3843 | real_peer_disk = D_UP_TO_DATE; |
| 3844 | |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3845 | if (ns.conn == C_WF_REPORT_PARAMS) |
| 3846 | ns.conn = C_CONNECTED; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3847 | |
Philipp Reisner | 6753171 | 2010-10-27 12:21:30 +0200 | [diff] [blame] | 3848 | if (peer_state.conn == C_AHEAD) |
| 3849 | ns.conn = C_BEHIND; |
| 3850 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3851 | if (mdev->p_uuid && peer_state.disk >= D_NEGOTIATING && |
| 3852 | get_ldev_if_state(mdev, D_NEGOTIATING)) { |
| 3853 | int cr; /* consider resync */ |
| 3854 | |
| 3855 | /* if we established a new connection */ |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3856 | cr = (os.conn < C_CONNECTED); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3857 | /* if we had an established connection |
| 3858 | * and one of the nodes newly attaches a disk */ |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3859 | cr |= (os.conn == C_CONNECTED && |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3860 | (peer_state.disk == D_NEGOTIATING || |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3861 | os.disk == D_NEGOTIATING)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3862 | /* if we have both been inconsistent, and the peer has been |
| 3863 | * forced to be UpToDate with --overwrite-data */ |
| 3864 | cr |= test_bit(CONSIDER_RESYNC, &mdev->flags); |
| 3865 | /* if we had been plain connected, and the admin requested to |
| 3866 | * start a sync by "invalidate" or "invalidate-remote" */ |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3867 | cr |= (os.conn == C_CONNECTED && |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3868 | (peer_state.conn >= C_STARTING_SYNC_S && |
| 3869 | peer_state.conn <= C_WF_BITMAP_T)); |
| 3870 | |
| 3871 | if (cr) |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3872 | ns.conn = drbd_sync_handshake(mdev, peer_state.role, real_peer_disk); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3873 | |
| 3874 | put_ldev(mdev); |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3875 | if (ns.conn == C_MASK) { |
| 3876 | ns.conn = C_CONNECTED; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3877 | if (mdev->state.disk == D_NEGOTIATING) { |
Lars Ellenberg | 82f59cc | 2010-10-16 12:13:47 +0200 | [diff] [blame] | 3878 | drbd_force_state(mdev, NS(disk, D_FAILED)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3879 | } else if (peer_state.disk == D_NEGOTIATING) { |
| 3880 | dev_err(DEV, "Disk attach process on the peer node was aborted.\n"); |
| 3881 | peer_state.disk = D_DISKLESS; |
Lars Ellenberg | 580b976 | 2010-02-26 23:15:23 +0100 | [diff] [blame] | 3882 | real_peer_disk = D_DISKLESS; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3883 | } else { |
Philipp Reisner | 8169e41 | 2011-03-15 18:40:27 +0100 | [diff] [blame] | 3884 | if (test_and_clear_bit(CONN_DRY_RUN, &mdev->tconn->flags)) |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3885 | return -EIO; |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3886 | D_ASSERT(os.conn == C_WF_REPORT_PARAMS); |
Philipp Reisner | 38fa998 | 2011-03-15 18:24:49 +0100 | [diff] [blame] | 3887 | conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3888 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3889 | } |
| 3890 | } |
| 3891 | } |
| 3892 | |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 3893 | spin_lock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | 78bae59 | 2011-03-28 15:40:12 +0200 | [diff] [blame] | 3894 | if (os.i != drbd_read_state(mdev).i) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3895 | goto retry; |
| 3896 | clear_bit(CONSIDER_RESYNC, &mdev->flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3897 | ns.peer = peer_state.role; |
| 3898 | ns.pdsk = real_peer_disk; |
| 3899 | ns.peer_isp = (peer_state.aftr_isp | peer_state.user_isp); |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3900 | 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] | 3901 | ns.disk = mdev->new_state_tmp.disk; |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3902 | cs_flags = CS_VERBOSE + (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED ? 0 : CS_HARD); |
Philipp Reisner | 2aebfab | 2011-03-28 16:48:11 +0200 | [diff] [blame] | 3903 | if (ns.pdsk == D_CONSISTENT && drbd_suspended(mdev) && ns.conn == C_CONNECTED && os.conn < C_CONNECTED && |
Philipp Reisner | 481c6f5 | 2010-06-22 14:03:27 +0200 | [diff] [blame] | 3904 | test_bit(NEW_CUR_UUID, &mdev->flags)) { |
Andreas Gruenbacher | 8554df1 | 2011-01-25 15:37:43 +0100 | [diff] [blame] | 3905 | /* 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] | 3906 | for temporal network outages! */ |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 3907 | spin_unlock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | 481c6f5 | 2010-06-22 14:03:27 +0200 | [diff] [blame] | 3908 | 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] | 3909 | tl_clear(mdev->tconn); |
Philipp Reisner | 481c6f5 | 2010-06-22 14:03:27 +0200 | [diff] [blame] | 3910 | drbd_uuid_new_current(mdev); |
| 3911 | clear_bit(NEW_CUR_UUID, &mdev->flags); |
Philipp Reisner | 38fa998 | 2011-03-15 18:24:49 +0100 | [diff] [blame] | 3912 | conn_request_state(mdev->tconn, NS2(conn, C_PROTOCOL_ERROR, susp, 0), CS_HARD); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3913 | return -EIO; |
Philipp Reisner | 481c6f5 | 2010-06-22 14:03:27 +0200 | [diff] [blame] | 3914 | } |
Philipp Reisner | 65d922c | 2010-06-16 16:18:09 +0200 | [diff] [blame] | 3915 | rv = _drbd_set_state(mdev, ns, cs_flags, NULL); |
Philipp Reisner | 78bae59 | 2011-03-28 15:40:12 +0200 | [diff] [blame] | 3916 | ns = drbd_read_state(mdev); |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 3917 | spin_unlock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3918 | |
| 3919 | if (rv < SS_SUCCESS) { |
Philipp Reisner | 38fa998 | 2011-03-15 18:24:49 +0100 | [diff] [blame] | 3920 | conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3921 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3922 | } |
| 3923 | |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 3924 | if (os.conn > C_WF_REPORT_PARAMS) { |
| 3925 | if (ns.conn > C_CONNECTED && peer_state.conn <= C_CONNECTED && |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3926 | peer_state.disk != D_NEGOTIATING ) { |
| 3927 | /* we want resync, peer has not yet decided to sync... */ |
| 3928 | /* Nowadays only used when forcing a node into primary role and |
| 3929 | setting its disk to UpToDate with that */ |
| 3930 | drbd_send_uuids(mdev); |
Philipp Reisner | 43de7c8 | 2011-11-10 13:16:13 +0100 | [diff] [blame] | 3931 | drbd_send_current_state(mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3932 | } |
| 3933 | } |
| 3934 | |
Philipp Reisner | 08b165b | 2011-09-05 16:22:33 +0200 | [diff] [blame] | 3935 | clear_bit(DISCARD_MY_DATA, &mdev->flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3936 | |
| 3937 | drbd_md_sync(mdev); /* update connected indicator, la_size, ... */ |
| 3938 | |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3939 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3940 | } |
| 3941 | |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 3942 | static int receive_sync_uuid(struct drbd_tconn *tconn, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3943 | { |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 3944 | struct drbd_conf *mdev; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 3945 | struct p_rs_uuid *p = pi->data; |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 3946 | |
| 3947 | mdev = vnr_to_mdev(tconn, pi->vnr); |
| 3948 | if (!mdev) |
| 3949 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3950 | |
| 3951 | wait_event(mdev->misc_wait, |
| 3952 | mdev->state.conn == C_WF_SYNC_UUID || |
Philipp Reisner | c4752ef | 2010-10-27 17:32:36 +0200 | [diff] [blame] | 3953 | mdev->state.conn == C_BEHIND || |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3954 | mdev->state.conn < C_CONNECTED || |
| 3955 | mdev->state.disk < D_NEGOTIATING); |
| 3956 | |
| 3957 | /* D_ASSERT( mdev->state.conn == C_WF_SYNC_UUID ); */ |
| 3958 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3959 | /* Here the _drbd_uuid_ functions are right, current should |
| 3960 | _not_ be rotated into the history */ |
| 3961 | if (get_ldev_if_state(mdev, D_NEGOTIATING)) { |
| 3962 | _drbd_uuid_set(mdev, UI_CURRENT, be64_to_cpu(p->uuid)); |
| 3963 | _drbd_uuid_set(mdev, UI_BITMAP, 0UL); |
| 3964 | |
Lars Ellenberg | 62b0da3 | 2011-01-20 13:25:21 +0100 | [diff] [blame] | 3965 | drbd_print_uuids(mdev, "updated sync uuid"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3966 | drbd_start_resync(mdev, C_SYNC_TARGET); |
| 3967 | |
| 3968 | put_ldev(mdev); |
| 3969 | } else |
| 3970 | dev_err(DEV, "Ignoring SyncUUID packet!\n"); |
| 3971 | |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3972 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3973 | } |
| 3974 | |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3975 | /** |
| 3976 | * receive_bitmap_plain |
| 3977 | * |
| 3978 | * Return 0 when done, 1 when another iteration is needed, and a negative error |
| 3979 | * code upon failure. |
| 3980 | */ |
| 3981 | static int |
Andreas Gruenbacher | 50d0b1a | 2011-03-30 11:53:51 +0200 | [diff] [blame] | 3982 | receive_bitmap_plain(struct drbd_conf *mdev, unsigned int size, |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 3983 | unsigned long *p, struct bm_xfer_ctx *c) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3984 | { |
Andreas Gruenbacher | 50d0b1a | 2011-03-30 11:53:51 +0200 | [diff] [blame] | 3985 | unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE - |
| 3986 | drbd_header_size(mdev->tconn); |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 3987 | unsigned int num_words = min_t(size_t, data_size / sizeof(*p), |
Andreas Gruenbacher | 50d0b1a | 2011-03-30 11:53:51 +0200 | [diff] [blame] | 3988 | c->bm_words - c->word_offset); |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 3989 | unsigned int want = num_words * sizeof(*p); |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3990 | int err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3991 | |
Andreas Gruenbacher | 50d0b1a | 2011-03-30 11:53:51 +0200 | [diff] [blame] | 3992 | if (want != size) { |
| 3993 | dev_err(DEV, "%s:want (%u) != size (%u)\n", __func__, want, size); |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3994 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3995 | } |
| 3996 | if (want == 0) |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 3997 | return 0; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 3998 | err = drbd_recv_all(mdev->tconn, p, want); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3999 | if (err) |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4000 | return err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4001 | |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4002 | drbd_bm_merge_lel(mdev, c->word_offset, num_words, p); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4003 | |
| 4004 | c->word_offset += num_words; |
| 4005 | c->bit_offset = c->word_offset * BITS_PER_LONG; |
| 4006 | if (c->bit_offset > c->bm_bits) |
| 4007 | c->bit_offset = c->bm_bits; |
| 4008 | |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4009 | return 1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4010 | } |
| 4011 | |
Andreas Gruenbacher | a02d124 | 2011-03-22 17:20:45 +0100 | [diff] [blame] | 4012 | static enum drbd_bitmap_code dcbp_get_code(struct p_compressed_bm *p) |
| 4013 | { |
| 4014 | return (enum drbd_bitmap_code)(p->encoding & 0x0f); |
| 4015 | } |
| 4016 | |
| 4017 | static int dcbp_get_start(struct p_compressed_bm *p) |
| 4018 | { |
| 4019 | return (p->encoding & 0x80) != 0; |
| 4020 | } |
| 4021 | |
| 4022 | static int dcbp_get_pad_bits(struct p_compressed_bm *p) |
| 4023 | { |
| 4024 | return (p->encoding >> 4) & 0x7; |
| 4025 | } |
| 4026 | |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4027 | /** |
| 4028 | * recv_bm_rle_bits |
| 4029 | * |
| 4030 | * Return 0 when done, 1 when another iteration is needed, and a negative error |
| 4031 | * code upon failure. |
| 4032 | */ |
| 4033 | static int |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4034 | recv_bm_rle_bits(struct drbd_conf *mdev, |
| 4035 | struct p_compressed_bm *p, |
Philipp Reisner | c6d25cf | 2011-01-19 16:13:06 +0100 | [diff] [blame] | 4036 | struct bm_xfer_ctx *c, |
| 4037 | unsigned int len) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4038 | { |
| 4039 | struct bitstream bs; |
| 4040 | u64 look_ahead; |
| 4041 | u64 rl; |
| 4042 | u64 tmp; |
| 4043 | unsigned long s = c->bit_offset; |
| 4044 | unsigned long e; |
Andreas Gruenbacher | a02d124 | 2011-03-22 17:20:45 +0100 | [diff] [blame] | 4045 | int toggle = dcbp_get_start(p); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4046 | int have; |
| 4047 | int bits; |
| 4048 | |
Andreas Gruenbacher | a02d124 | 2011-03-22 17:20:45 +0100 | [diff] [blame] | 4049 | bitstream_init(&bs, p->code, len, dcbp_get_pad_bits(p)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4050 | |
| 4051 | bits = bitstream_get_bits(&bs, &look_ahead, 64); |
| 4052 | if (bits < 0) |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4053 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4054 | |
| 4055 | for (have = bits; have > 0; s += rl, toggle = !toggle) { |
| 4056 | bits = vli_decode_bits(&rl, look_ahead); |
| 4057 | if (bits <= 0) |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4058 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4059 | |
| 4060 | if (toggle) { |
| 4061 | e = s + rl -1; |
| 4062 | if (e >= c->bm_bits) { |
| 4063 | 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] | 4064 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4065 | } |
| 4066 | _drbd_bm_set_bits(mdev, s, e); |
| 4067 | } |
| 4068 | |
| 4069 | if (have < bits) { |
| 4070 | dev_err(DEV, "bitmap decoding error: h:%d b:%d la:0x%08llx l:%u/%u\n", |
| 4071 | have, bits, look_ahead, |
| 4072 | (unsigned int)(bs.cur.b - p->code), |
| 4073 | (unsigned int)bs.buf_len); |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4074 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4075 | } |
| 4076 | look_ahead >>= bits; |
| 4077 | have -= bits; |
| 4078 | |
| 4079 | bits = bitstream_get_bits(&bs, &tmp, 64 - have); |
| 4080 | if (bits < 0) |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4081 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4082 | look_ahead |= tmp << have; |
| 4083 | have += bits; |
| 4084 | } |
| 4085 | |
| 4086 | c->bit_offset = s; |
| 4087 | bm_xfer_ctx_bit_to_word_offset(c); |
| 4088 | |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4089 | return (s != c->bm_bits); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4090 | } |
| 4091 | |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4092 | /** |
| 4093 | * decode_bitmap_c |
| 4094 | * |
| 4095 | * Return 0 when done, 1 when another iteration is needed, and a negative error |
| 4096 | * code upon failure. |
| 4097 | */ |
| 4098 | static int |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4099 | decode_bitmap_c(struct drbd_conf *mdev, |
| 4100 | struct p_compressed_bm *p, |
Philipp Reisner | c6d25cf | 2011-01-19 16:13:06 +0100 | [diff] [blame] | 4101 | struct bm_xfer_ctx *c, |
| 4102 | unsigned int len) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4103 | { |
Andreas Gruenbacher | a02d124 | 2011-03-22 17:20:45 +0100 | [diff] [blame] | 4104 | if (dcbp_get_code(p) == RLE_VLI_Bits) |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4105 | return recv_bm_rle_bits(mdev, p, c, len - sizeof(*p)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4106 | |
| 4107 | /* other variants had been implemented for evaluation, |
| 4108 | * but have been dropped as this one turned out to be "best" |
| 4109 | * during all our tests. */ |
| 4110 | |
| 4111 | dev_err(DEV, "receive_bitmap_c: unknown encoding %u\n", p->encoding); |
Philipp Reisner | 38fa998 | 2011-03-15 18:24:49 +0100 | [diff] [blame] | 4112 | conn_request_state(mdev->tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD); |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4113 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4114 | } |
| 4115 | |
| 4116 | void INFO_bm_xfer_stats(struct drbd_conf *mdev, |
| 4117 | const char *direction, struct bm_xfer_ctx *c) |
| 4118 | { |
| 4119 | /* what would it take to transfer it "plaintext" */ |
Andreas Gruenbacher | 50d0b1a | 2011-03-30 11:53:51 +0200 | [diff] [blame] | 4120 | unsigned int header_size = drbd_header_size(mdev->tconn); |
| 4121 | unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE - header_size; |
| 4122 | unsigned int plain = |
| 4123 | header_size * (DIV_ROUND_UP(c->bm_words, data_size) + 1) + |
| 4124 | c->bm_words * sizeof(unsigned long); |
| 4125 | unsigned int total = c->bytes[0] + c->bytes[1]; |
| 4126 | unsigned int r; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4127 | |
| 4128 | /* total can not be zero. but just in case: */ |
| 4129 | if (total == 0) |
| 4130 | return; |
| 4131 | |
| 4132 | /* don't report if not compressed */ |
| 4133 | if (total >= plain) |
| 4134 | return; |
| 4135 | |
| 4136 | /* total < plain. check for overflow, still */ |
| 4137 | r = (total > UINT_MAX/1000) ? (total / (plain/1000)) |
| 4138 | : (1000 * total / plain); |
| 4139 | |
| 4140 | if (r > 1000) |
| 4141 | r = 1000; |
| 4142 | |
| 4143 | r = 1000 - r; |
| 4144 | dev_info(DEV, "%s bitmap stats [Bytes(packets)]: plain %u(%u), RLE %u(%u), " |
| 4145 | "total %u; compression: %u.%u%%\n", |
| 4146 | direction, |
| 4147 | c->bytes[1], c->packets[1], |
| 4148 | c->bytes[0], c->packets[0], |
| 4149 | total, r/10, r % 10); |
| 4150 | } |
| 4151 | |
| 4152 | /* Since we are processing the bitfield from lower addresses to higher, |
| 4153 | it does not matter if the process it in 32 bit chunks or 64 bit |
| 4154 | chunks as long as it is little endian. (Understand it as byte stream, |
| 4155 | beginning with the lowest byte...) If we would use big endian |
| 4156 | we would need to process it from the highest address to the lowest, |
| 4157 | in order to be agnostic to the 32 vs 64 bits issue. |
| 4158 | |
| 4159 | returns 0 on failure, 1 if we successfully received it. */ |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 4160 | static int receive_bitmap(struct drbd_tconn *tconn, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4161 | { |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 4162 | struct drbd_conf *mdev; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4163 | struct bm_xfer_ctx c; |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4164 | int err; |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 4165 | |
| 4166 | mdev = vnr_to_mdev(tconn, pi->vnr); |
| 4167 | if (!mdev) |
| 4168 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4169 | |
Lars Ellenberg | 20ceb2b | 2011-01-21 10:56:44 +0100 | [diff] [blame] | 4170 | drbd_bm_lock(mdev, "receive bitmap", BM_LOCKED_SET_ALLOWED); |
| 4171 | /* you are supposed to send additional out-of-sync information |
| 4172 | * if you actually set bits during this phase */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4173 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4174 | c = (struct bm_xfer_ctx) { |
| 4175 | .bm_bits = drbd_bm_bits(mdev), |
| 4176 | .bm_words = drbd_bm_words(mdev), |
| 4177 | }; |
| 4178 | |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4179 | for(;;) { |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4180 | if (pi->cmd == P_BITMAP) |
| 4181 | err = receive_bitmap_plain(mdev, pi->size, pi->data, &c); |
| 4182 | else if (pi->cmd == P_COMPRESSED_BITMAP) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4183 | /* MAYBE: sanity check that we speak proto >= 90, |
| 4184 | * and the feature is enabled! */ |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4185 | struct p_compressed_bm *p = pi->data; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4186 | |
Andreas Gruenbacher | 50d0b1a | 2011-03-30 11:53:51 +0200 | [diff] [blame] | 4187 | if (pi->size > DRBD_SOCKET_BUFFER_SIZE - drbd_header_size(tconn)) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4188 | dev_err(DEV, "ReportCBitmap packet too large\n"); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4189 | err = -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4190 | goto out; |
| 4191 | } |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4192 | if (pi->size <= sizeof(*p)) { |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 4193 | dev_err(DEV, "ReportCBitmap packet too small (l:%u)\n", pi->size); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4194 | err = -EIO; |
Andreas Gruenbacher | 78fcbda | 2010-12-10 22:18:27 +0100 | [diff] [blame] | 4195 | goto out; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4196 | } |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4197 | err = drbd_recv_all(mdev->tconn, p, pi->size); |
| 4198 | if (err) |
| 4199 | goto out; |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 4200 | err = decode_bitmap_c(mdev, p, &c, pi->size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4201 | } else { |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 4202 | dev_warn(DEV, "receive_bitmap: cmd neither ReportBitMap nor ReportCBitMap (is 0x%x)", pi->cmd); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4203 | err = -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4204 | goto out; |
| 4205 | } |
| 4206 | |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 4207 | c.packets[pi->cmd == P_BITMAP]++; |
Andreas Gruenbacher | 50d0b1a | 2011-03-30 11:53:51 +0200 | [diff] [blame] | 4208 | c.bytes[pi->cmd == P_BITMAP] += drbd_header_size(tconn) + pi->size; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4209 | |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4210 | if (err <= 0) { |
| 4211 | if (err < 0) |
| 4212 | goto out; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4213 | break; |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4214 | } |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 4215 | err = drbd_recv_header(mdev->tconn, pi); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4216 | if (err) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4217 | goto out; |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4218 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4219 | |
| 4220 | INFO_bm_xfer_stats(mdev, "receive", &c); |
| 4221 | |
| 4222 | if (mdev->state.conn == C_WF_BITMAP_T) { |
Andreas Gruenbacher | de1f8e4 | 2010-12-10 21:04:00 +0100 | [diff] [blame] | 4223 | enum drbd_state_rv rv; |
| 4224 | |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4225 | err = drbd_send_bitmap(mdev); |
| 4226 | if (err) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4227 | goto out; |
| 4228 | /* Omit CS_ORDERED with this state transition to avoid deadlocks. */ |
Andreas Gruenbacher | de1f8e4 | 2010-12-10 21:04:00 +0100 | [diff] [blame] | 4229 | rv = _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE); |
| 4230 | D_ASSERT(rv == SS_SUCCESS); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4231 | } else if (mdev->state.conn != C_WF_BITMAP_S) { |
| 4232 | /* admin may have requested C_DISCONNECTING, |
| 4233 | * other threads may have noticed network errors */ |
| 4234 | dev_info(DEV, "unexpected cstate (%s) in receive_bitmap\n", |
| 4235 | drbd_conn_str(mdev->state.conn)); |
| 4236 | } |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4237 | err = 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4238 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4239 | out: |
Lars Ellenberg | 20ceb2b | 2011-01-21 10:56:44 +0100 | [diff] [blame] | 4240 | drbd_bm_unlock(mdev); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4241 | if (!err && mdev->state.conn == C_WF_BITMAP_S) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4242 | drbd_start_resync(mdev, C_SYNC_SOURCE); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4243 | return err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4244 | } |
| 4245 | |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 4246 | static int receive_skip(struct drbd_tconn *tconn, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4247 | { |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 4248 | conn_warn(tconn, "skipping unknown optional packet type %d, l: %d!\n", |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 4249 | pi->cmd, pi->size); |
Philipp Reisner | 2de876e | 2011-03-15 14:38:01 +0100 | [diff] [blame] | 4250 | |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 4251 | return ignore_remaining_packet(tconn, pi); |
Philipp Reisner | 2de876e | 2011-03-15 14:38:01 +0100 | [diff] [blame] | 4252 | } |
| 4253 | |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 4254 | static int receive_UnplugRemote(struct drbd_tconn *tconn, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4255 | { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4256 | /* Make sure we've acked all the TCP data associated |
| 4257 | * with the data requests being unplugged */ |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 4258 | drbd_tcp_quickack(tconn->data.socket); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4259 | |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4260 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4261 | } |
| 4262 | |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 4263 | static int receive_out_of_sync(struct drbd_tconn *tconn, struct packet_info *pi) |
Philipp Reisner | 73a01a1 | 2010-10-27 14:33:00 +0200 | [diff] [blame] | 4264 | { |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 4265 | struct drbd_conf *mdev; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4266 | struct p_block_desc *p = pi->data; |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 4267 | |
| 4268 | mdev = vnr_to_mdev(tconn, pi->vnr); |
| 4269 | if (!mdev) |
| 4270 | return -EIO; |
Philipp Reisner | 73a01a1 | 2010-10-27 14:33:00 +0200 | [diff] [blame] | 4271 | |
Lars Ellenberg | f735e363 | 2010-12-17 21:06:18 +0100 | [diff] [blame] | 4272 | switch (mdev->state.conn) { |
| 4273 | case C_WF_SYNC_UUID: |
| 4274 | case C_WF_BITMAP_T: |
| 4275 | case C_BEHIND: |
| 4276 | break; |
| 4277 | default: |
| 4278 | dev_err(DEV, "ASSERT FAILED cstate = %s, expected: WFSyncUUID|WFBitMapT|Behind\n", |
| 4279 | drbd_conn_str(mdev->state.conn)); |
| 4280 | } |
| 4281 | |
Philipp Reisner | 73a01a1 | 2010-10-27 14:33:00 +0200 | [diff] [blame] | 4282 | drbd_set_out_of_sync(mdev, be64_to_cpu(p->sector), be32_to_cpu(p->blksize)); |
| 4283 | |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4284 | return 0; |
Philipp Reisner | 73a01a1 | 2010-10-27 14:33:00 +0200 | [diff] [blame] | 4285 | } |
| 4286 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4287 | struct data_cmd { |
| 4288 | int expect_payload; |
| 4289 | size_t pkt_size; |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 4290 | int (*fn)(struct drbd_tconn *, struct packet_info *); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4291 | }; |
| 4292 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4293 | static struct data_cmd drbd_cmd_handler[] = { |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 4294 | [P_DATA] = { 1, sizeof(struct p_data), receive_Data }, |
| 4295 | [P_DATA_REPLY] = { 1, sizeof(struct p_data), receive_DataReply }, |
| 4296 | [P_RS_DATA_REPLY] = { 1, sizeof(struct p_data), receive_RSDataReply } , |
| 4297 | [P_BARRIER] = { 0, sizeof(struct p_barrier), receive_Barrier } , |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4298 | [P_BITMAP] = { 1, 0, receive_bitmap } , |
| 4299 | [P_COMPRESSED_BITMAP] = { 1, 0, receive_bitmap } , |
| 4300 | [P_UNPLUG_REMOTE] = { 0, 0, receive_UnplugRemote }, |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 4301 | [P_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest }, |
| 4302 | [P_RS_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest }, |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4303 | [P_SYNC_PARAM] = { 1, 0, receive_SyncParam }, |
| 4304 | [P_SYNC_PARAM89] = { 1, 0, receive_SyncParam }, |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 4305 | [P_PROTOCOL] = { 1, sizeof(struct p_protocol), receive_protocol }, |
| 4306 | [P_UUIDS] = { 0, sizeof(struct p_uuids), receive_uuids }, |
| 4307 | [P_SIZES] = { 0, sizeof(struct p_sizes), receive_sizes }, |
| 4308 | [P_STATE] = { 0, sizeof(struct p_state), receive_state }, |
| 4309 | [P_STATE_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_state }, |
| 4310 | [P_SYNC_UUID] = { 0, sizeof(struct p_rs_uuid), receive_sync_uuid }, |
| 4311 | [P_OV_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest }, |
| 4312 | [P_OV_REPLY] = { 1, sizeof(struct p_block_req), receive_DataRequest }, |
| 4313 | [P_CSUM_RS_REQUEST] = { 1, sizeof(struct p_block_req), receive_DataRequest }, |
| 4314 | [P_DELAY_PROBE] = { 0, sizeof(struct p_delay_probe93), receive_skip }, |
| 4315 | [P_OUT_OF_SYNC] = { 0, sizeof(struct p_block_desc), receive_out_of_sync }, |
| 4316 | [P_CONN_ST_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_conn_state }, |
Philipp Reisner | 036b17e | 2011-05-16 17:38:11 +0200 | [diff] [blame] | 4317 | [P_PROTOCOL_UPDATE] = { 1, sizeof(struct p_protocol), receive_protocol }, |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4318 | }; |
| 4319 | |
Philipp Reisner | eefc2f7 | 2011-02-08 12:55:24 +0100 | [diff] [blame] | 4320 | static void drbdd(struct drbd_tconn *tconn) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4321 | { |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4322 | struct packet_info pi; |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4323 | size_t shs; /* sub header size */ |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4324 | int err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4325 | |
Philipp Reisner | eefc2f7 | 2011-02-08 12:55:24 +0100 | [diff] [blame] | 4326 | while (get_t_state(&tconn->receiver) == RUNNING) { |
Andreas Gruenbacher | deebe19 | 2011-03-25 00:01:04 +0100 | [diff] [blame] | 4327 | struct data_cmd *cmd; |
| 4328 | |
Philipp Reisner | eefc2f7 | 2011-02-08 12:55:24 +0100 | [diff] [blame] | 4329 | drbd_thread_current_set_cpu(&tconn->receiver); |
Andreas Gruenbacher | 69bc7bc | 2011-03-16 17:31:52 +0100 | [diff] [blame] | 4330 | if (drbd_recv_header(tconn, &pi)) |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4331 | goto err_out; |
| 4332 | |
Andreas Gruenbacher | deebe19 | 2011-03-25 00:01:04 +0100 | [diff] [blame] | 4333 | cmd = &drbd_cmd_handler[pi.cmd]; |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 4334 | if (unlikely(pi.cmd >= ARRAY_SIZE(drbd_cmd_handler) || !cmd->fn)) { |
Andreas Gruenbacher | 2fcb8f3 | 2011-07-03 11:41:08 +0200 | [diff] [blame] | 4335 | conn_err(tconn, "Unexpected data packet %s (0x%04x)", |
| 4336 | cmdname(pi.cmd), pi.cmd); |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4337 | goto err_out; |
Lars Ellenberg | 0b33a91 | 2009-11-16 15:58:04 +0100 | [diff] [blame] | 4338 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4339 | |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4340 | shs = cmd->pkt_size; |
| 4341 | if (pi.size > shs && !cmd->expect_payload) { |
Andreas Gruenbacher | 2fcb8f3 | 2011-07-03 11:41:08 +0200 | [diff] [blame] | 4342 | conn_err(tconn, "No payload expected %s l:%d\n", |
| 4343 | cmdname(pi.cmd), pi.size); |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4344 | goto err_out; |
| 4345 | } |
| 4346 | |
Lars Ellenberg | c13f7e1 | 2010-10-29 23:32:01 +0200 | [diff] [blame] | 4347 | if (shs) { |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4348 | err = drbd_recv_all_warn(tconn, pi.data, shs); |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 4349 | if (err) |
Lars Ellenberg | c13f7e1 | 2010-10-29 23:32:01 +0200 | [diff] [blame] | 4350 | goto err_out; |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 4351 | pi.size -= shs; |
Lars Ellenberg | c13f7e1 | 2010-10-29 23:32:01 +0200 | [diff] [blame] | 4352 | } |
| 4353 | |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 4354 | err = cmd->fn(tconn, &pi); |
| 4355 | if (err) { |
Andreas Gruenbacher | 9f5bdc3 | 2011-03-28 14:23:08 +0200 | [diff] [blame] | 4356 | conn_err(tconn, "error receiving %s, e: %d l: %d!\n", |
| 4357 | cmdname(pi.cmd), err, pi.size); |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4358 | goto err_out; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4359 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4360 | } |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4361 | return; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4362 | |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4363 | err_out: |
| 4364 | conn_request_state(tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4365 | } |
| 4366 | |
Philipp Reisner | 0e29d16 | 2011-02-18 14:23:11 +0100 | [diff] [blame] | 4367 | void conn_flush_workqueue(struct drbd_tconn *tconn) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4368 | { |
| 4369 | struct drbd_wq_barrier barr; |
| 4370 | |
| 4371 | barr.w.cb = w_prev_work_done; |
Philipp Reisner | 0e29d16 | 2011-02-18 14:23:11 +0100 | [diff] [blame] | 4372 | barr.w.tconn = tconn; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4373 | init_completion(&barr.done); |
Philipp Reisner | 0e29d16 | 2011-02-18 14:23:11 +0100 | [diff] [blame] | 4374 | drbd_queue_work(&tconn->data.work, &barr.w); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4375 | wait_for_completion(&barr.done); |
| 4376 | } |
| 4377 | |
Philipp Reisner | 81fa2e6 | 2011-05-04 15:10:30 +0200 | [diff] [blame] | 4378 | static void conn_disconnect(struct drbd_tconn *tconn) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4379 | { |
Philipp Reisner | c141ebd | 2011-05-05 16:13:10 +0200 | [diff] [blame] | 4380 | struct drbd_conf *mdev; |
Philipp Reisner | bbeb641 | 2011-02-10 13:45:46 +0100 | [diff] [blame] | 4381 | enum drbd_conns oc; |
Philipp Reisner | 376694a | 2011-11-07 10:54:28 +0100 | [diff] [blame] | 4382 | int vnr; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4383 | |
Philipp Reisner | bbeb641 | 2011-02-10 13:45:46 +0100 | [diff] [blame] | 4384 | if (tconn->cstate == C_STANDALONE) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4385 | return; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4386 | |
Philipp Reisner | b8853db | 2011-12-13 11:09:16 +0100 | [diff] [blame] | 4387 | /* We are about to start the cleanup after connection loss. |
| 4388 | * Make sure drbd_make_request knows about that. |
| 4389 | * Usually we should be in some network failure state already, |
| 4390 | * but just in case we are not, we fix it up here. |
| 4391 | */ |
| 4392 | conn_request_state(tconn, NS(conn, C_NETWORK_FAILURE), CS_HARD); |
| 4393 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4394 | /* asender does not clean up anything. it must not interfere, either */ |
Philipp Reisner | 360cc74 | 2011-02-08 14:29:53 +0100 | [diff] [blame] | 4395 | drbd_thread_stop(&tconn->asender); |
| 4396 | drbd_free_sock(tconn); |
| 4397 | |
Philipp Reisner | c141ebd | 2011-05-05 16:13:10 +0200 | [diff] [blame] | 4398 | rcu_read_lock(); |
| 4399 | idr_for_each_entry(&tconn->volumes, mdev, vnr) { |
| 4400 | kref_get(&mdev->kref); |
| 4401 | rcu_read_unlock(); |
| 4402 | drbd_disconnected(mdev); |
| 4403 | kref_put(&mdev->kref, &drbd_minor_destroy); |
| 4404 | rcu_read_lock(); |
| 4405 | } |
| 4406 | rcu_read_unlock(); |
| 4407 | |
Philipp Reisner | 12038a3 | 2011-11-09 19:18:00 +0100 | [diff] [blame] | 4408 | if (!list_empty(&tconn->current_epoch->list)) |
| 4409 | conn_err(tconn, "ASSERTION FAILED: tconn->current_epoch->list not empty\n"); |
| 4410 | /* ok, no more ee's on the fly, it is safe to reset the epoch_size */ |
| 4411 | atomic_set(&tconn->current_epoch->epoch_size, 0); |
| 4412 | |
Philipp Reisner | 360cc74 | 2011-02-08 14:29:53 +0100 | [diff] [blame] | 4413 | conn_info(tconn, "Connection closed\n"); |
| 4414 | |
Philipp Reisner | cb70345 | 2011-03-24 11:03:07 +0100 | [diff] [blame] | 4415 | if (conn_highest_role(tconn) == R_PRIMARY && conn_highest_pdsk(tconn) >= D_UNKNOWN) |
| 4416 | conn_try_outdate_peer_async(tconn); |
| 4417 | |
Philipp Reisner | 360cc74 | 2011-02-08 14:29:53 +0100 | [diff] [blame] | 4418 | spin_lock_irq(&tconn->req_lock); |
Philipp Reisner | bbeb641 | 2011-02-10 13:45:46 +0100 | [diff] [blame] | 4419 | oc = tconn->cstate; |
| 4420 | if (oc >= C_UNCONNECTED) |
Philipp Reisner | 376694a | 2011-11-07 10:54:28 +0100 | [diff] [blame] | 4421 | _conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE); |
Philipp Reisner | bbeb641 | 2011-02-10 13:45:46 +0100 | [diff] [blame] | 4422 | |
Philipp Reisner | 360cc74 | 2011-02-08 14:29:53 +0100 | [diff] [blame] | 4423 | spin_unlock_irq(&tconn->req_lock); |
| 4424 | |
Lars Ellenberg | f3dfa40 | 2011-05-02 10:45:05 +0200 | [diff] [blame] | 4425 | if (oc == C_DISCONNECTING) |
Lars Ellenberg | d9cc6e2 | 2011-04-27 10:25:28 +0200 | [diff] [blame] | 4426 | conn_request_state(tconn, NS(conn, C_STANDALONE), CS_VERBOSE | CS_HARD); |
Philipp Reisner | 360cc74 | 2011-02-08 14:29:53 +0100 | [diff] [blame] | 4427 | } |
| 4428 | |
Philipp Reisner | c141ebd | 2011-05-05 16:13:10 +0200 | [diff] [blame] | 4429 | static int drbd_disconnected(struct drbd_conf *mdev) |
Philipp Reisner | 360cc74 | 2011-02-08 14:29:53 +0100 | [diff] [blame] | 4430 | { |
Philipp Reisner | 360cc74 | 2011-02-08 14:29:53 +0100 | [diff] [blame] | 4431 | unsigned int i; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4432 | |
Philipp Reisner | 8571957 | 2010-07-21 10:20:17 +0200 | [diff] [blame] | 4433 | /* wait for current activity to cease. */ |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 4434 | spin_lock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4435 | _drbd_wait_ee_list_empty(mdev, &mdev->active_ee); |
| 4436 | _drbd_wait_ee_list_empty(mdev, &mdev->sync_ee); |
| 4437 | _drbd_wait_ee_list_empty(mdev, &mdev->read_ee); |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 4438 | spin_unlock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4439 | |
| 4440 | /* We do not have data structures that would allow us to |
| 4441 | * get the rs_pending_cnt down to 0 again. |
| 4442 | * * On C_SYNC_TARGET we do not have any data structures describing |
| 4443 | * the pending RSDataRequest's we have sent. |
| 4444 | * * On C_SYNC_SOURCE there is no data structure that tracks |
| 4445 | * the P_RS_DATA_REPLY blocks that we sent to the SyncTarget. |
| 4446 | * And no, it is not the sum of the reference counts in the |
| 4447 | * resync_LRU. The resync_LRU tracks the whole operation including |
| 4448 | * the disk-IO, while the rs_pending_cnt only tracks the blocks |
| 4449 | * on the fly. */ |
| 4450 | drbd_rs_cancel_all(mdev); |
| 4451 | mdev->rs_total = 0; |
| 4452 | mdev->rs_failed = 0; |
| 4453 | atomic_set(&mdev->rs_pending_cnt, 0); |
| 4454 | wake_up(&mdev->misc_wait); |
| 4455 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4456 | del_timer_sync(&mdev->resync_timer); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4457 | resync_timer_fn((unsigned long)mdev); |
| 4458 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4459 | /* wait for all w_e_end_data_req, w_e_end_rsdata_req, w_send_barrier, |
| 4460 | * w_make_resync_request etc. which may still be on the worker queue |
| 4461 | * to be "canceled" */ |
Philipp Reisner | a21e929 | 2011-02-08 15:08:49 +0100 | [diff] [blame] | 4462 | drbd_flush_workqueue(mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4463 | |
Andreas Gruenbacher | a990be4 | 2011-04-06 17:56:48 +0200 | [diff] [blame] | 4464 | drbd_finish_peer_reqs(mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4465 | |
Philipp Reisner | d10b4ea | 2011-11-30 23:25:36 +0100 | [diff] [blame] | 4466 | /* This second workqueue flush is necessary, since drbd_finish_peer_reqs() |
| 4467 | might have issued a work again. The one before drbd_finish_peer_reqs() is |
| 4468 | necessary to reclain net_ee in drbd_finish_peer_reqs(). */ |
| 4469 | drbd_flush_workqueue(mdev); |
| 4470 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4471 | kfree(mdev->p_uuid); |
| 4472 | mdev->p_uuid = NULL; |
| 4473 | |
Philipp Reisner | 2aebfab | 2011-03-28 16:48:11 +0200 | [diff] [blame] | 4474 | if (!drbd_suspended(mdev)) |
Philipp Reisner | 2f5cdd0 | 2011-02-21 14:29:27 +0100 | [diff] [blame] | 4475 | tl_clear(mdev->tconn); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4476 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4477 | drbd_md_sync(mdev); |
| 4478 | |
Lars Ellenberg | 20ceb2b | 2011-01-21 10:56:44 +0100 | [diff] [blame] | 4479 | /* serialize with bitmap writeout triggered by the state change, |
| 4480 | * if any. */ |
| 4481 | wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags)); |
| 4482 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4483 | /* tcp_close and release of sendpage pages can be deferred. I don't |
| 4484 | * want to use SO_LINGER, because apparently it can be deferred for |
| 4485 | * more than 20 seconds (longest time I checked). |
| 4486 | * |
| 4487 | * Actually we don't care for exactly when the network stack does its |
| 4488 | * put_page(), but release our reference on these pages right here. |
| 4489 | */ |
Andreas Gruenbacher | 7721f56 | 2011-04-06 17:14:02 +0200 | [diff] [blame] | 4490 | i = drbd_free_peer_reqs(mdev, &mdev->net_ee); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4491 | if (i) |
| 4492 | dev_info(DEV, "net_ee not empty, killed %u entries\n", i); |
Lars Ellenberg | 435f074 | 2010-09-06 12:30:25 +0200 | [diff] [blame] | 4493 | i = atomic_read(&mdev->pp_in_use_by_net); |
| 4494 | if (i) |
| 4495 | 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] | 4496 | i = atomic_read(&mdev->pp_in_use); |
| 4497 | if (i) |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 4498 | dev_info(DEV, "pp_in_use = %d, expected 0\n", i); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4499 | |
| 4500 | D_ASSERT(list_empty(&mdev->read_ee)); |
| 4501 | D_ASSERT(list_empty(&mdev->active_ee)); |
| 4502 | D_ASSERT(list_empty(&mdev->sync_ee)); |
| 4503 | D_ASSERT(list_empty(&mdev->done_ee)); |
| 4504 | |
Philipp Reisner | 360cc74 | 2011-02-08 14:29:53 +0100 | [diff] [blame] | 4505 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4506 | } |
| 4507 | |
| 4508 | /* |
| 4509 | * We support PRO_VERSION_MIN to PRO_VERSION_MAX. The protocol version |
| 4510 | * we can agree on is stored in agreed_pro_version. |
| 4511 | * |
| 4512 | * feature flags and the reserved array should be enough room for future |
| 4513 | * enhancements of the handshake protocol, and possible plugins... |
| 4514 | * |
| 4515 | * for now, they are expected to be zero, but ignored. |
| 4516 | */ |
Andreas Gruenbacher | 6038178 | 2011-03-28 17:05:50 +0200 | [diff] [blame] | 4517 | static int drbd_send_features(struct drbd_tconn *tconn) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4518 | { |
Andreas Gruenbacher | 9f5bdc3 | 2011-03-28 14:23:08 +0200 | [diff] [blame] | 4519 | struct drbd_socket *sock; |
| 4520 | struct p_connection_features *p; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4521 | |
Andreas Gruenbacher | 9f5bdc3 | 2011-03-28 14:23:08 +0200 | [diff] [blame] | 4522 | sock = &tconn->data; |
| 4523 | p = conn_prepare_command(tconn, sock); |
| 4524 | if (!p) |
Andreas Gruenbacher | e8d17b0 | 2011-03-16 00:54:19 +0100 | [diff] [blame] | 4525 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4526 | memset(p, 0, sizeof(*p)); |
| 4527 | p->protocol_min = cpu_to_be32(PRO_VERSION_MIN); |
| 4528 | p->protocol_max = cpu_to_be32(PRO_VERSION_MAX); |
Andreas Gruenbacher | 9f5bdc3 | 2011-03-28 14:23:08 +0200 | [diff] [blame] | 4529 | return conn_send_command(tconn, sock, P_CONNECTION_FEATURES, sizeof(*p), NULL, 0); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4530 | } |
| 4531 | |
| 4532 | /* |
| 4533 | * return values: |
| 4534 | * 1 yes, we have a valid connection |
| 4535 | * 0 oops, did not work out, please try again |
| 4536 | * -1 peer talks different language, |
| 4537 | * no point in trying again, please go standalone. |
| 4538 | */ |
Andreas Gruenbacher | 6038178 | 2011-03-28 17:05:50 +0200 | [diff] [blame] | 4539 | static int drbd_do_features(struct drbd_tconn *tconn) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4540 | { |
Philipp Reisner | 65d11ed | 2011-02-07 17:35:59 +0100 | [diff] [blame] | 4541 | /* ASSERT current == tconn->receiver ... */ |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4542 | struct p_connection_features *p; |
| 4543 | const int expect = sizeof(struct p_connection_features); |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4544 | struct packet_info pi; |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 4545 | int err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4546 | |
Andreas Gruenbacher | 6038178 | 2011-03-28 17:05:50 +0200 | [diff] [blame] | 4547 | err = drbd_send_features(tconn); |
Andreas Gruenbacher | e8d17b0 | 2011-03-16 00:54:19 +0100 | [diff] [blame] | 4548 | if (err) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4549 | return 0; |
| 4550 | |
Andreas Gruenbacher | 69bc7bc | 2011-03-16 17:31:52 +0100 | [diff] [blame] | 4551 | err = drbd_recv_header(tconn, &pi); |
| 4552 | if (err) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4553 | return 0; |
| 4554 | |
Andreas Gruenbacher | 6038178 | 2011-03-28 17:05:50 +0200 | [diff] [blame] | 4555 | if (pi.cmd != P_CONNECTION_FEATURES) { |
| 4556 | conn_err(tconn, "expected ConnectionFeatures packet, received: %s (0x%04x)\n", |
Andreas Gruenbacher | 2fcb8f3 | 2011-07-03 11:41:08 +0200 | [diff] [blame] | 4557 | cmdname(pi.cmd), pi.cmd); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4558 | return -1; |
| 4559 | } |
| 4560 | |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4561 | if (pi.size != expect) { |
Andreas Gruenbacher | 6038178 | 2011-03-28 17:05:50 +0200 | [diff] [blame] | 4562 | conn_err(tconn, "expected ConnectionFeatures length: %u, received: %u\n", |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4563 | expect, pi.size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4564 | return -1; |
| 4565 | } |
| 4566 | |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4567 | p = pi.data; |
| 4568 | err = drbd_recv_all_warn(tconn, p, expect); |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 4569 | if (err) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4570 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4571 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4572 | p->protocol_min = be32_to_cpu(p->protocol_min); |
| 4573 | p->protocol_max = be32_to_cpu(p->protocol_max); |
| 4574 | if (p->protocol_max == 0) |
| 4575 | p->protocol_max = p->protocol_min; |
| 4576 | |
| 4577 | if (PRO_VERSION_MAX < p->protocol_min || |
| 4578 | PRO_VERSION_MIN > p->protocol_max) |
| 4579 | goto incompat; |
| 4580 | |
Philipp Reisner | 65d11ed | 2011-02-07 17:35:59 +0100 | [diff] [blame] | 4581 | 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] | 4582 | |
Philipp Reisner | 65d11ed | 2011-02-07 17:35:59 +0100 | [diff] [blame] | 4583 | conn_info(tconn, "Handshake successful: " |
| 4584 | "Agreed network protocol version %d\n", tconn->agreed_pro_version); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4585 | |
| 4586 | return 1; |
| 4587 | |
| 4588 | incompat: |
Philipp Reisner | 65d11ed | 2011-02-07 17:35:59 +0100 | [diff] [blame] | 4589 | conn_err(tconn, "incompatible DRBD dialects: " |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4590 | "I support %d-%d, peer supports %d-%d\n", |
| 4591 | PRO_VERSION_MIN, PRO_VERSION_MAX, |
| 4592 | p->protocol_min, p->protocol_max); |
| 4593 | return -1; |
| 4594 | } |
| 4595 | |
| 4596 | #if !defined(CONFIG_CRYPTO_HMAC) && !defined(CONFIG_CRYPTO_HMAC_MODULE) |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4597 | static int drbd_do_auth(struct drbd_tconn *tconn) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4598 | { |
| 4599 | dev_err(DEV, "This kernel was build without CONFIG_CRYPTO_HMAC.\n"); |
| 4600 | 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] | 4601 | return -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4602 | } |
| 4603 | #else |
| 4604 | #define CHALLENGE_LEN 64 |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 4605 | |
| 4606 | /* Return value: |
| 4607 | 1 - auth succeeded, |
| 4608 | 0 - failed, try again (network error), |
| 4609 | -1 - auth failed, don't try again. |
| 4610 | */ |
| 4611 | |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4612 | static int drbd_do_auth(struct drbd_tconn *tconn) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4613 | { |
Andreas Gruenbacher | 9f5bdc3 | 2011-03-28 14:23:08 +0200 | [diff] [blame] | 4614 | struct drbd_socket *sock; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4615 | char my_challenge[CHALLENGE_LEN]; /* 64 Bytes... */ |
| 4616 | struct scatterlist sg; |
| 4617 | char *response = NULL; |
| 4618 | char *right_response = NULL; |
| 4619 | char *peers_ch = NULL; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 4620 | unsigned int key_len; |
| 4621 | char secret[SHARED_SECRET_MAX]; /* 64 byte */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4622 | unsigned int resp_size; |
| 4623 | struct hash_desc desc; |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4624 | struct packet_info pi; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 4625 | struct net_conf *nc; |
Andreas Gruenbacher | 69bc7bc | 2011-03-16 17:31:52 +0100 | [diff] [blame] | 4626 | int err, rv; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4627 | |
Andreas Gruenbacher | 9f5bdc3 | 2011-03-28 14:23:08 +0200 | [diff] [blame] | 4628 | /* FIXME: Put the challenge/response into the preallocated socket buffer. */ |
| 4629 | |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 4630 | rcu_read_lock(); |
| 4631 | nc = rcu_dereference(tconn->net_conf); |
| 4632 | key_len = strlen(nc->shared_secret); |
| 4633 | memcpy(secret, nc->shared_secret, key_len); |
| 4634 | rcu_read_unlock(); |
| 4635 | |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4636 | desc.tfm = tconn->cram_hmac_tfm; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4637 | desc.flags = 0; |
| 4638 | |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 4639 | rv = crypto_hash_setkey(tconn->cram_hmac_tfm, (u8 *)secret, key_len); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4640 | if (rv) { |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4641 | conn_err(tconn, "crypto_hash_setkey() failed with %d\n", rv); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 4642 | rv = -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4643 | goto fail; |
| 4644 | } |
| 4645 | |
| 4646 | get_random_bytes(my_challenge, CHALLENGE_LEN); |
| 4647 | |
Andreas Gruenbacher | 9f5bdc3 | 2011-03-28 14:23:08 +0200 | [diff] [blame] | 4648 | sock = &tconn->data; |
| 4649 | if (!conn_prepare_command(tconn, sock)) { |
| 4650 | rv = 0; |
| 4651 | goto fail; |
| 4652 | } |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4653 | rv = !conn_send_command(tconn, sock, P_AUTH_CHALLENGE, 0, |
Andreas Gruenbacher | 9f5bdc3 | 2011-03-28 14:23:08 +0200 | [diff] [blame] | 4654 | my_challenge, CHALLENGE_LEN); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4655 | if (!rv) |
| 4656 | goto fail; |
| 4657 | |
Andreas Gruenbacher | 69bc7bc | 2011-03-16 17:31:52 +0100 | [diff] [blame] | 4658 | err = drbd_recv_header(tconn, &pi); |
| 4659 | if (err) { |
| 4660 | rv = 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4661 | goto fail; |
Andreas Gruenbacher | 69bc7bc | 2011-03-16 17:31:52 +0100 | [diff] [blame] | 4662 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4663 | |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4664 | if (pi.cmd != P_AUTH_CHALLENGE) { |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4665 | conn_err(tconn, "expected AuthChallenge packet, received: %s (0x%04x)\n", |
Andreas Gruenbacher | 2fcb8f3 | 2011-07-03 11:41:08 +0200 | [diff] [blame] | 4666 | cmdname(pi.cmd), pi.cmd); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4667 | rv = 0; |
| 4668 | goto fail; |
| 4669 | } |
| 4670 | |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4671 | if (pi.size > CHALLENGE_LEN * 2) { |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4672 | conn_err(tconn, "expected AuthChallenge payload too big.\n"); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 4673 | rv = -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4674 | goto fail; |
| 4675 | } |
| 4676 | |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4677 | peers_ch = kmalloc(pi.size, GFP_NOIO); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4678 | if (peers_ch == NULL) { |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4679 | conn_err(tconn, "kmalloc of peers_ch failed\n"); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 4680 | rv = -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4681 | goto fail; |
| 4682 | } |
| 4683 | |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 4684 | err = drbd_recv_all_warn(tconn, peers_ch, pi.size); |
| 4685 | if (err) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4686 | rv = 0; |
| 4687 | goto fail; |
| 4688 | } |
| 4689 | |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4690 | resp_size = crypto_hash_digestsize(tconn->cram_hmac_tfm); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4691 | response = kmalloc(resp_size, GFP_NOIO); |
| 4692 | if (response == NULL) { |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4693 | conn_err(tconn, "kmalloc of response failed\n"); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 4694 | rv = -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4695 | goto fail; |
| 4696 | } |
| 4697 | |
| 4698 | sg_init_table(&sg, 1); |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4699 | sg_set_buf(&sg, peers_ch, pi.size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4700 | |
| 4701 | rv = crypto_hash_digest(&desc, &sg, sg.length, response); |
| 4702 | if (rv) { |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4703 | conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 4704 | rv = -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4705 | goto fail; |
| 4706 | } |
| 4707 | |
Andreas Gruenbacher | 9f5bdc3 | 2011-03-28 14:23:08 +0200 | [diff] [blame] | 4708 | if (!conn_prepare_command(tconn, sock)) { |
| 4709 | rv = 0; |
| 4710 | goto fail; |
| 4711 | } |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4712 | rv = !conn_send_command(tconn, sock, P_AUTH_RESPONSE, 0, |
Andreas Gruenbacher | 9f5bdc3 | 2011-03-28 14:23:08 +0200 | [diff] [blame] | 4713 | response, resp_size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4714 | if (!rv) |
| 4715 | goto fail; |
| 4716 | |
Andreas Gruenbacher | 69bc7bc | 2011-03-16 17:31:52 +0100 | [diff] [blame] | 4717 | err = drbd_recv_header(tconn, &pi); |
| 4718 | if (err) { |
| 4719 | rv = 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4720 | goto fail; |
Andreas Gruenbacher | 69bc7bc | 2011-03-16 17:31:52 +0100 | [diff] [blame] | 4721 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4722 | |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4723 | if (pi.cmd != P_AUTH_RESPONSE) { |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4724 | conn_err(tconn, "expected AuthResponse packet, received: %s (0x%04x)\n", |
Andreas Gruenbacher | 2fcb8f3 | 2011-07-03 11:41:08 +0200 | [diff] [blame] | 4725 | cmdname(pi.cmd), pi.cmd); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4726 | rv = 0; |
| 4727 | goto fail; |
| 4728 | } |
| 4729 | |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4730 | if (pi.size != resp_size) { |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4731 | conn_err(tconn, "expected AuthResponse payload of wrong size\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4732 | rv = 0; |
| 4733 | goto fail; |
| 4734 | } |
| 4735 | |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 4736 | err = drbd_recv_all_warn(tconn, response , resp_size); |
| 4737 | if (err) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4738 | rv = 0; |
| 4739 | goto fail; |
| 4740 | } |
| 4741 | |
| 4742 | right_response = kmalloc(resp_size, GFP_NOIO); |
Julia Lawall | 2d1ee87 | 2009-12-27 22:27:11 +0100 | [diff] [blame] | 4743 | if (right_response == NULL) { |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4744 | conn_err(tconn, "kmalloc of right_response failed\n"); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 4745 | rv = -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4746 | goto fail; |
| 4747 | } |
| 4748 | |
| 4749 | sg_set_buf(&sg, my_challenge, CHALLENGE_LEN); |
| 4750 | |
| 4751 | rv = crypto_hash_digest(&desc, &sg, sg.length, right_response); |
| 4752 | if (rv) { |
Philipp Reisner | 13e6037 | 2011-02-08 09:54:40 +0100 | [diff] [blame] | 4753 | conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 4754 | rv = -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4755 | goto fail; |
| 4756 | } |
| 4757 | |
| 4758 | rv = !memcmp(response, right_response, resp_size); |
| 4759 | |
| 4760 | if (rv) |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 4761 | conn_info(tconn, "Peer authenticated using %d bytes HMAC\n", |
| 4762 | resp_size); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 4763 | else |
| 4764 | rv = -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4765 | |
| 4766 | fail: |
| 4767 | kfree(peers_ch); |
| 4768 | kfree(response); |
| 4769 | kfree(right_response); |
| 4770 | |
| 4771 | return rv; |
| 4772 | } |
| 4773 | #endif |
| 4774 | |
| 4775 | int drbdd_init(struct drbd_thread *thi) |
| 4776 | { |
Philipp Reisner | 392c880 | 2011-02-09 10:33:31 +0100 | [diff] [blame] | 4777 | struct drbd_tconn *tconn = thi->tconn; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4778 | int h; |
| 4779 | |
Philipp Reisner | 4d641dd | 2011-02-08 15:40:24 +0100 | [diff] [blame] | 4780 | conn_info(tconn, "receiver (re)started\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4781 | |
| 4782 | do { |
Philipp Reisner | 81fa2e6 | 2011-05-04 15:10:30 +0200 | [diff] [blame] | 4783 | h = conn_connect(tconn); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4784 | if (h == 0) { |
Philipp Reisner | 81fa2e6 | 2011-05-04 15:10:30 +0200 | [diff] [blame] | 4785 | conn_disconnect(tconn); |
Philipp Reisner | 20ee639 | 2011-01-18 15:28:59 +0100 | [diff] [blame] | 4786 | schedule_timeout_interruptible(HZ); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4787 | } |
| 4788 | if (h == -1) { |
Philipp Reisner | 4d641dd | 2011-02-08 15:40:24 +0100 | [diff] [blame] | 4789 | conn_warn(tconn, "Discarding network configuration.\n"); |
Philipp Reisner | bbeb641 | 2011-02-10 13:45:46 +0100 | [diff] [blame] | 4790 | conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4791 | } |
| 4792 | } while (h == 0); |
| 4793 | |
Philipp Reisner | 91fd4da | 2011-04-20 17:47:29 +0200 | [diff] [blame] | 4794 | if (h > 0) |
| 4795 | drbdd(tconn); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4796 | |
Philipp Reisner | 81fa2e6 | 2011-05-04 15:10:30 +0200 | [diff] [blame] | 4797 | conn_disconnect(tconn); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4798 | |
Philipp Reisner | 4d641dd | 2011-02-08 15:40:24 +0100 | [diff] [blame] | 4799 | conn_info(tconn, "receiver terminated\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4800 | return 0; |
| 4801 | } |
| 4802 | |
| 4803 | /* ********* acknowledge sender ******** */ |
| 4804 | |
Andreas Gruenbacher | e05e1e5 | 2011-03-25 15:16:26 +0100 | [diff] [blame] | 4805 | static int got_conn_RqSReply(struct drbd_tconn *tconn, struct packet_info *pi) |
Philipp Reisner | e4f78ed | 2011-03-16 11:27:48 +0100 | [diff] [blame] | 4806 | { |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4807 | struct p_req_state_reply *p = pi->data; |
Philipp Reisner | e4f78ed | 2011-03-16 11:27:48 +0100 | [diff] [blame] | 4808 | int retcode = be32_to_cpu(p->retcode); |
| 4809 | |
| 4810 | if (retcode >= SS_SUCCESS) { |
| 4811 | set_bit(CONN_WD_ST_CHG_OKAY, &tconn->flags); |
| 4812 | } else { |
| 4813 | set_bit(CONN_WD_ST_CHG_FAIL, &tconn->flags); |
| 4814 | conn_err(tconn, "Requested state change failed by peer: %s (%d)\n", |
| 4815 | drbd_set_st_err_str(retcode), retcode); |
| 4816 | } |
| 4817 | wake_up(&tconn->ping_wait); |
| 4818 | |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 4819 | return 0; |
Philipp Reisner | e4f78ed | 2011-03-16 11:27:48 +0100 | [diff] [blame] | 4820 | } |
| 4821 | |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 4822 | static int got_RqSReply(struct drbd_tconn *tconn, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4823 | { |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 4824 | struct drbd_conf *mdev; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4825 | struct p_req_state_reply *p = pi->data; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4826 | int retcode = be32_to_cpu(p->retcode); |
| 4827 | |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 4828 | mdev = vnr_to_mdev(tconn, pi->vnr); |
| 4829 | if (!mdev) |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 4830 | return -EIO; |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 4831 | |
Philipp Reisner | 4d0fc3f | 2012-01-20 13:52:27 +0100 | [diff] [blame] | 4832 | if (test_bit(CONN_WD_ST_CHG_REQ, &tconn->flags)) { |
| 4833 | D_ASSERT(tconn->agreed_pro_version < 100); |
| 4834 | return got_conn_RqSReply(tconn, pi); |
| 4835 | } |
| 4836 | |
Philipp Reisner | e4f78ed | 2011-03-16 11:27:48 +0100 | [diff] [blame] | 4837 | if (retcode >= SS_SUCCESS) { |
| 4838 | set_bit(CL_ST_CHG_SUCCESS, &mdev->flags); |
| 4839 | } else { |
| 4840 | set_bit(CL_ST_CHG_FAIL, &mdev->flags); |
| 4841 | dev_err(DEV, "Requested state change failed by peer: %s (%d)\n", |
| 4842 | drbd_set_st_err_str(retcode), retcode); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4843 | } |
Philipp Reisner | e4f78ed | 2011-03-16 11:27:48 +0100 | [diff] [blame] | 4844 | wake_up(&mdev->state_wait); |
| 4845 | |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 4846 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4847 | } |
| 4848 | |
Andreas Gruenbacher | e05e1e5 | 2011-03-25 15:16:26 +0100 | [diff] [blame] | 4849 | static int got_Ping(struct drbd_tconn *tconn, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4850 | { |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 4851 | return drbd_send_ping_ack(tconn); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4852 | |
| 4853 | } |
| 4854 | |
Andreas Gruenbacher | e05e1e5 | 2011-03-25 15:16:26 +0100 | [diff] [blame] | 4855 | static int got_PingAck(struct drbd_tconn *tconn, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4856 | { |
| 4857 | /* restore idle timeout */ |
Philipp Reisner | 2a67d8b | 2011-02-09 14:10:32 +0100 | [diff] [blame] | 4858 | tconn->meta.socket->sk->sk_rcvtimeo = tconn->net_conf->ping_int*HZ; |
| 4859 | if (!test_and_set_bit(GOT_PING_ACK, &tconn->flags)) |
| 4860 | wake_up(&tconn->ping_wait); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4861 | |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 4862 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4863 | } |
| 4864 | |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 4865 | static int got_IsInSync(struct drbd_tconn *tconn, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4866 | { |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 4867 | struct drbd_conf *mdev; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4868 | struct p_block_ack *p = pi->data; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4869 | sector_t sector = be64_to_cpu(p->sector); |
| 4870 | int blksize = be32_to_cpu(p->blksize); |
| 4871 | |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 4872 | mdev = vnr_to_mdev(tconn, pi->vnr); |
| 4873 | if (!mdev) |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 4874 | return -EIO; |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 4875 | |
Philipp Reisner | 31890f4 | 2011-01-19 14:12:51 +0100 | [diff] [blame] | 4876 | D_ASSERT(mdev->tconn->agreed_pro_version >= 89); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4877 | |
| 4878 | update_peer_seq(mdev, be32_to_cpu(p->seq_num)); |
| 4879 | |
Lars Ellenberg | 1d53f09 | 2010-09-05 01:13:24 +0200 | [diff] [blame] | 4880 | if (get_ldev(mdev)) { |
| 4881 | drbd_rs_complete_io(mdev, sector); |
| 4882 | drbd_set_in_sync(mdev, sector, blksize); |
| 4883 | /* rs_same_csums is supposed to count in units of BM_BLOCK_SIZE */ |
| 4884 | mdev->rs_same_csum += (blksize >> BM_BLOCK_SHIFT); |
| 4885 | put_ldev(mdev); |
| 4886 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4887 | dec_rs_pending(mdev); |
Philipp Reisner | 778f271 | 2010-07-06 11:14:00 +0200 | [diff] [blame] | 4888 | atomic_add(blksize >> 9, &mdev->rs_sect_in); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4889 | |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 4890 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4891 | } |
| 4892 | |
Andreas Gruenbacher | bc9c5c4 | 2011-01-21 18:00:55 +0100 | [diff] [blame] | 4893 | static int |
| 4894 | validate_req_change_req_state(struct drbd_conf *mdev, u64 id, sector_t sector, |
| 4895 | struct rb_root *root, const char *func, |
| 4896 | enum drbd_req_event what, bool missing_ok) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4897 | { |
| 4898 | struct drbd_request *req; |
| 4899 | struct bio_and_error m; |
| 4900 | |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 4901 | spin_lock_irq(&mdev->tconn->req_lock); |
Andreas Gruenbacher | bc9c5c4 | 2011-01-21 18:00:55 +0100 | [diff] [blame] | 4902 | req = find_request(mdev, root, id, sector, missing_ok, func); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4903 | if (unlikely(!req)) { |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 4904 | spin_unlock_irq(&mdev->tconn->req_lock); |
Andreas Gruenbacher | 8599767 | 2011-04-04 13:09:15 +0200 | [diff] [blame] | 4905 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4906 | } |
| 4907 | __req_mod(req, what, &m); |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 4908 | spin_unlock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4909 | |
| 4910 | if (m.bio) |
| 4911 | complete_master_bio(mdev, &m); |
Andreas Gruenbacher | 8599767 | 2011-04-04 13:09:15 +0200 | [diff] [blame] | 4912 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4913 | } |
| 4914 | |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 4915 | static int got_BlockAck(struct drbd_tconn *tconn, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4916 | { |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 4917 | struct drbd_conf *mdev; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4918 | struct p_block_ack *p = pi->data; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4919 | sector_t sector = be64_to_cpu(p->sector); |
| 4920 | int blksize = be32_to_cpu(p->blksize); |
| 4921 | enum drbd_req_event what; |
| 4922 | |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 4923 | mdev = vnr_to_mdev(tconn, pi->vnr); |
| 4924 | if (!mdev) |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 4925 | return -EIO; |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 4926 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4927 | update_peer_seq(mdev, be32_to_cpu(p->seq_num)); |
| 4928 | |
Andreas Gruenbacher | 579b57e | 2011-01-13 18:40:57 +0100 | [diff] [blame] | 4929 | if (p->block_id == ID_SYNCER) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4930 | drbd_set_in_sync(mdev, sector, blksize); |
| 4931 | dec_rs_pending(mdev); |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 4932 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4933 | } |
Andreas Gruenbacher | e05e1e5 | 2011-03-25 15:16:26 +0100 | [diff] [blame] | 4934 | switch (pi->cmd) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4935 | case P_RS_WRITE_ACK: |
Andreas Gruenbacher | 8554df1 | 2011-01-25 15:37:43 +0100 | [diff] [blame] | 4936 | what = WRITE_ACKED_BY_PEER_AND_SIS; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4937 | break; |
| 4938 | case P_WRITE_ACK: |
Andreas Gruenbacher | 8554df1 | 2011-01-25 15:37:43 +0100 | [diff] [blame] | 4939 | what = WRITE_ACKED_BY_PEER; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4940 | break; |
| 4941 | case P_RECV_ACK: |
Andreas Gruenbacher | 8554df1 | 2011-01-25 15:37:43 +0100 | [diff] [blame] | 4942 | what = RECV_ACKED_BY_PEER; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4943 | break; |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 4944 | case P_DISCARD_WRITE: |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 4945 | what = DISCARD_WRITE; |
| 4946 | break; |
| 4947 | case P_RETRY_WRITE: |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 4948 | what = POSTPONE_WRITE; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4949 | break; |
| 4950 | default: |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 4951 | BUG(); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4952 | } |
| 4953 | |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 4954 | return validate_req_change_req_state(mdev, p->block_id, sector, |
| 4955 | &mdev->write_requests, __func__, |
| 4956 | what, false); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4957 | } |
| 4958 | |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 4959 | static int got_NegAck(struct drbd_tconn *tconn, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4960 | { |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 4961 | struct drbd_conf *mdev; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4962 | struct p_block_ack *p = pi->data; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4963 | sector_t sector = be64_to_cpu(p->sector); |
Philipp Reisner | 2deb833 | 2011-01-17 18:39:18 +0100 | [diff] [blame] | 4964 | int size = be32_to_cpu(p->blksize); |
Andreas Gruenbacher | 8599767 | 2011-04-04 13:09:15 +0200 | [diff] [blame] | 4965 | int err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4966 | |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 4967 | mdev = vnr_to_mdev(tconn, pi->vnr); |
| 4968 | if (!mdev) |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 4969 | return -EIO; |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 4970 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4971 | update_peer_seq(mdev, be32_to_cpu(p->seq_num)); |
| 4972 | |
Andreas Gruenbacher | 579b57e | 2011-01-13 18:40:57 +0100 | [diff] [blame] | 4973 | if (p->block_id == ID_SYNCER) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4974 | dec_rs_pending(mdev); |
| 4975 | drbd_rs_failed_io(mdev, sector, size); |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 4976 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4977 | } |
Philipp Reisner | 2deb833 | 2011-01-17 18:39:18 +0100 | [diff] [blame] | 4978 | |
Andreas Gruenbacher | 8599767 | 2011-04-04 13:09:15 +0200 | [diff] [blame] | 4979 | err = validate_req_change_req_state(mdev, p->block_id, sector, |
| 4980 | &mdev->write_requests, __func__, |
Philipp Reisner | 303d144 | 2011-04-13 16:24:47 -0700 | [diff] [blame] | 4981 | NEG_ACKED, true); |
Andreas Gruenbacher | 8599767 | 2011-04-04 13:09:15 +0200 | [diff] [blame] | 4982 | if (err) { |
Andreas Gruenbacher | c3afd8f | 2011-01-20 22:25:40 +0100 | [diff] [blame] | 4983 | /* Protocol A has no P_WRITE_ACKs, but has P_NEG_ACKs. |
| 4984 | The master bio might already be completed, therefore the |
| 4985 | request is no longer in the collision hash. */ |
| 4986 | /* In Protocol B we might already have got a P_RECV_ACK |
| 4987 | but then get a P_NEG_ACK afterwards. */ |
Andreas Gruenbacher | c3afd8f | 2011-01-20 22:25:40 +0100 | [diff] [blame] | 4988 | drbd_set_out_of_sync(mdev, sector, size); |
Philipp Reisner | 2deb833 | 2011-01-17 18:39:18 +0100 | [diff] [blame] | 4989 | } |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 4990 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4991 | } |
| 4992 | |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 4993 | static int got_NegDReply(struct drbd_tconn *tconn, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4994 | { |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 4995 | struct drbd_conf *mdev; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4996 | struct p_block_ack *p = pi->data; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4997 | sector_t sector = be64_to_cpu(p->sector); |
| 4998 | |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 4999 | mdev = vnr_to_mdev(tconn, pi->vnr); |
| 5000 | if (!mdev) |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5001 | return -EIO; |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 5002 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5003 | update_peer_seq(mdev, be32_to_cpu(p->seq_num)); |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 5004 | |
Philipp Reisner | 380207d | 2011-11-11 12:31:20 +0100 | [diff] [blame] | 5005 | dev_err(DEV, "Got NegDReply; Sector %llus, len %u.\n", |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5006 | (unsigned long long)sector, be32_to_cpu(p->blksize)); |
| 5007 | |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5008 | return validate_req_change_req_state(mdev, p->block_id, sector, |
| 5009 | &mdev->read_requests, __func__, |
| 5010 | NEG_ACKED, false); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5011 | } |
| 5012 | |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 5013 | static int got_NegRSDReply(struct drbd_tconn *tconn, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5014 | { |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 5015 | struct drbd_conf *mdev; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5016 | sector_t sector; |
| 5017 | int size; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 5018 | struct p_block_ack *p = pi->data; |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 5019 | |
| 5020 | mdev = vnr_to_mdev(tconn, pi->vnr); |
| 5021 | if (!mdev) |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5022 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5023 | |
| 5024 | sector = be64_to_cpu(p->sector); |
| 5025 | size = be32_to_cpu(p->blksize); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5026 | |
| 5027 | update_peer_seq(mdev, be32_to_cpu(p->seq_num)); |
| 5028 | |
| 5029 | dec_rs_pending(mdev); |
| 5030 | |
| 5031 | if (get_ldev_if_state(mdev, D_FAILED)) { |
| 5032 | drbd_rs_complete_io(mdev, sector); |
Andreas Gruenbacher | e05e1e5 | 2011-03-25 15:16:26 +0100 | [diff] [blame] | 5033 | switch (pi->cmd) { |
Philipp Reisner | d612d30 | 2010-12-27 10:53:28 +0100 | [diff] [blame] | 5034 | case P_NEG_RS_DREPLY: |
| 5035 | drbd_rs_failed_io(mdev, sector, size); |
| 5036 | case P_RS_CANCEL: |
| 5037 | break; |
| 5038 | default: |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5039 | BUG(); |
Philipp Reisner | d612d30 | 2010-12-27 10:53:28 +0100 | [diff] [blame] | 5040 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5041 | put_ldev(mdev); |
| 5042 | } |
| 5043 | |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5044 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5045 | } |
| 5046 | |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 5047 | static int got_BarrierAck(struct drbd_tconn *tconn, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5048 | { |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 5049 | struct p_barrier_ack *p = pi->data; |
Lars Ellenberg | 9ed57dc | 2012-03-26 20:55:17 +0200 | [diff] [blame] | 5050 | struct drbd_conf *mdev; |
| 5051 | int vnr; |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 5052 | |
Lars Ellenberg | 9ed57dc | 2012-03-26 20:55:17 +0200 | [diff] [blame] | 5053 | tl_release(tconn, p->barrier, be32_to_cpu(p->set_size)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5054 | |
Lars Ellenberg | 9ed57dc | 2012-03-26 20:55:17 +0200 | [diff] [blame] | 5055 | rcu_read_lock(); |
| 5056 | idr_for_each_entry(&tconn->volumes, mdev, vnr) { |
| 5057 | if (mdev->state.conn == C_AHEAD && |
| 5058 | atomic_read(&mdev->ap_in_flight) == 0 && |
| 5059 | !test_and_set_bit(AHEAD_TO_SYNC_SOURCE, &mdev->flags)) { |
| 5060 | mdev->start_resync_timer.expires = jiffies + HZ; |
| 5061 | add_timer(&mdev->start_resync_timer); |
| 5062 | } |
Philipp Reisner | c4752ef | 2010-10-27 17:32:36 +0200 | [diff] [blame] | 5063 | } |
Lars Ellenberg | 9ed57dc | 2012-03-26 20:55:17 +0200 | [diff] [blame] | 5064 | rcu_read_unlock(); |
Philipp Reisner | c4752ef | 2010-10-27 17:32:36 +0200 | [diff] [blame] | 5065 | |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5066 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5067 | } |
| 5068 | |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 5069 | static int got_OVResult(struct drbd_tconn *tconn, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5070 | { |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 5071 | struct drbd_conf *mdev; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 5072 | struct p_block_ack *p = pi->data; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5073 | struct drbd_work *w; |
| 5074 | sector_t sector; |
| 5075 | int size; |
| 5076 | |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 5077 | mdev = vnr_to_mdev(tconn, pi->vnr); |
| 5078 | if (!mdev) |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5079 | return -EIO; |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 5080 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5081 | sector = be64_to_cpu(p->sector); |
| 5082 | size = be32_to_cpu(p->blksize); |
| 5083 | |
| 5084 | update_peer_seq(mdev, be32_to_cpu(p->seq_num)); |
| 5085 | |
| 5086 | if (be64_to_cpu(p->block_id) == ID_OUT_OF_SYNC) |
Andreas Gruenbacher | 8f7bed7 | 2010-12-19 23:53:14 +0100 | [diff] [blame] | 5087 | drbd_ov_out_of_sync_found(mdev, sector, size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5088 | else |
Andreas Gruenbacher | 8f7bed7 | 2010-12-19 23:53:14 +0100 | [diff] [blame] | 5089 | ov_out_of_sync_print(mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5090 | |
Lars Ellenberg | 1d53f09 | 2010-09-05 01:13:24 +0200 | [diff] [blame] | 5091 | if (!get_ldev(mdev)) |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5092 | return 0; |
Lars Ellenberg | 1d53f09 | 2010-09-05 01:13:24 +0200 | [diff] [blame] | 5093 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5094 | drbd_rs_complete_io(mdev, sector); |
| 5095 | dec_rs_pending(mdev); |
| 5096 | |
Lars Ellenberg | ea5442a | 2010-11-05 09:48:01 +0100 | [diff] [blame] | 5097 | --mdev->ov_left; |
| 5098 | |
| 5099 | /* let's advance progress step marks only for every other megabyte */ |
| 5100 | if ((mdev->ov_left & 0x200) == 0x200) |
| 5101 | drbd_advance_rs_marks(mdev, mdev->ov_left); |
| 5102 | |
| 5103 | if (mdev->ov_left == 0) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5104 | w = kmalloc(sizeof(*w), GFP_NOIO); |
| 5105 | if (w) { |
| 5106 | w->cb = w_ov_finished; |
Philipp Reisner | a21e929 | 2011-02-08 15:08:49 +0100 | [diff] [blame] | 5107 | w->mdev = mdev; |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 5108 | drbd_queue_work_front(&mdev->tconn->data.work, w); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5109 | } else { |
| 5110 | dev_err(DEV, "kmalloc(w) failed."); |
Andreas Gruenbacher | 8f7bed7 | 2010-12-19 23:53:14 +0100 | [diff] [blame] | 5111 | ov_out_of_sync_print(mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5112 | drbd_resync_finished(mdev); |
| 5113 | } |
| 5114 | } |
Lars Ellenberg | 1d53f09 | 2010-09-05 01:13:24 +0200 | [diff] [blame] | 5115 | put_ldev(mdev); |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5116 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5117 | } |
| 5118 | |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 5119 | static int got_skip(struct drbd_tconn *tconn, struct packet_info *pi) |
Philipp Reisner | 0ced55a | 2010-04-30 15:26:20 +0200 | [diff] [blame] | 5120 | { |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5121 | return 0; |
Philipp Reisner | 0ced55a | 2010-04-30 15:26:20 +0200 | [diff] [blame] | 5122 | } |
| 5123 | |
Andreas Gruenbacher | a990be4 | 2011-04-06 17:56:48 +0200 | [diff] [blame] | 5124 | static int tconn_finish_peer_reqs(struct drbd_tconn *tconn) |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 5125 | { |
Philipp Reisner | 082a343 | 2011-03-15 16:05:42 +0100 | [diff] [blame] | 5126 | struct drbd_conf *mdev; |
Philipp Reisner | c141ebd | 2011-05-05 16:13:10 +0200 | [diff] [blame] | 5127 | int vnr, not_empty = 0; |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 5128 | |
| 5129 | do { |
| 5130 | clear_bit(SIGNAL_ASENDER, &tconn->flags); |
| 5131 | flush_signals(current); |
Philipp Reisner | c141ebd | 2011-05-05 16:13:10 +0200 | [diff] [blame] | 5132 | |
| 5133 | rcu_read_lock(); |
| 5134 | idr_for_each_entry(&tconn->volumes, mdev, vnr) { |
| 5135 | kref_get(&mdev->kref); |
| 5136 | rcu_read_unlock(); |
Philipp Reisner | d3fcb49 | 2011-04-13 14:46:05 -0700 | [diff] [blame] | 5137 | if (drbd_finish_peer_reqs(mdev)) { |
Philipp Reisner | c141ebd | 2011-05-05 16:13:10 +0200 | [diff] [blame] | 5138 | kref_put(&mdev->kref, &drbd_minor_destroy); |
| 5139 | return 1; |
Philipp Reisner | d3fcb49 | 2011-04-13 14:46:05 -0700 | [diff] [blame] | 5140 | } |
Philipp Reisner | c141ebd | 2011-05-05 16:13:10 +0200 | [diff] [blame] | 5141 | kref_put(&mdev->kref, &drbd_minor_destroy); |
| 5142 | rcu_read_lock(); |
Philipp Reisner | 082a343 | 2011-03-15 16:05:42 +0100 | [diff] [blame] | 5143 | } |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 5144 | set_bit(SIGNAL_ASENDER, &tconn->flags); |
Philipp Reisner | 082a343 | 2011-03-15 16:05:42 +0100 | [diff] [blame] | 5145 | |
| 5146 | spin_lock_irq(&tconn->req_lock); |
Philipp Reisner | c141ebd | 2011-05-05 16:13:10 +0200 | [diff] [blame] | 5147 | idr_for_each_entry(&tconn->volumes, mdev, vnr) { |
Philipp Reisner | 082a343 | 2011-03-15 16:05:42 +0100 | [diff] [blame] | 5148 | not_empty = !list_empty(&mdev->done_ee); |
| 5149 | if (not_empty) |
| 5150 | break; |
| 5151 | } |
| 5152 | spin_unlock_irq(&tconn->req_lock); |
Philipp Reisner | c141ebd | 2011-05-05 16:13:10 +0200 | [diff] [blame] | 5153 | rcu_read_unlock(); |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 5154 | } while (not_empty); |
| 5155 | |
| 5156 | return 0; |
| 5157 | } |
| 5158 | |
Andreas Gruenbacher | 7201b97 | 2011-03-14 18:23:00 +0100 | [diff] [blame] | 5159 | struct asender_cmd { |
| 5160 | size_t pkt_size; |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 5161 | int (*fn)(struct drbd_tconn *tconn, struct packet_info *); |
Andreas Gruenbacher | 7201b97 | 2011-03-14 18:23:00 +0100 | [diff] [blame] | 5162 | }; |
| 5163 | |
| 5164 | static struct asender_cmd asender_tbl[] = { |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 5165 | [P_PING] = { 0, got_Ping }, |
| 5166 | [P_PING_ACK] = { 0, got_PingAck }, |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 5167 | [P_RECV_ACK] = { sizeof(struct p_block_ack), got_BlockAck }, |
| 5168 | [P_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck }, |
| 5169 | [P_RS_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck }, |
| 5170 | [P_DISCARD_WRITE] = { sizeof(struct p_block_ack), got_BlockAck }, |
| 5171 | [P_NEG_ACK] = { sizeof(struct p_block_ack), got_NegAck }, |
| 5172 | [P_NEG_DREPLY] = { sizeof(struct p_block_ack), got_NegDReply }, |
| 5173 | [P_NEG_RS_DREPLY] = { sizeof(struct p_block_ack), got_NegRSDReply }, |
| 5174 | [P_OV_RESULT] = { sizeof(struct p_block_ack), got_OVResult }, |
| 5175 | [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), got_BarrierAck }, |
| 5176 | [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), got_RqSReply }, |
| 5177 | [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), got_IsInSync }, |
| 5178 | [P_DELAY_PROBE] = { sizeof(struct p_delay_probe93), got_skip }, |
| 5179 | [P_RS_CANCEL] = { sizeof(struct p_block_ack), got_NegRSDReply }, |
| 5180 | [P_CONN_ST_CHG_REPLY]={ sizeof(struct p_req_state_reply), got_conn_RqSReply }, |
| 5181 | [P_RETRY_WRITE] = { sizeof(struct p_block_ack), got_BlockAck }, |
Andreas Gruenbacher | 7201b97 | 2011-03-14 18:23:00 +0100 | [diff] [blame] | 5182 | }; |
| 5183 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5184 | int drbd_asender(struct drbd_thread *thi) |
| 5185 | { |
Philipp Reisner | 392c880 | 2011-02-09 10:33:31 +0100 | [diff] [blame] | 5186 | struct drbd_tconn *tconn = thi->tconn; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5187 | struct asender_cmd *cmd = NULL; |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 5188 | struct packet_info pi; |
Philipp Reisner | 257d0af | 2011-01-26 12:15:29 +0100 | [diff] [blame] | 5189 | int rv; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 5190 | void *buf = tconn->meta.rbuf; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5191 | int received = 0; |
Andreas Gruenbacher | 52b061a | 2011-03-30 11:38:49 +0200 | [diff] [blame] | 5192 | unsigned int header_size = drbd_header_size(tconn); |
| 5193 | int expect = header_size; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 5194 | bool ping_timeout_active = false; |
| 5195 | struct net_conf *nc; |
Andreas Gruenbacher | bb77d34 | 2011-05-04 15:25:35 +0200 | [diff] [blame] | 5196 | int ping_timeo, tcp_cork, ping_int; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5197 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5198 | current->policy = SCHED_RR; /* Make this a realtime task! */ |
| 5199 | current->rt_priority = 2; /* more important than all other tasks */ |
| 5200 | |
Andreas Gruenbacher | e77a0a5 | 2011-01-25 15:43:39 +0100 | [diff] [blame] | 5201 | while (get_t_state(thi) == RUNNING) { |
Philipp Reisner | 8082228 | 2011-02-08 12:46:30 +0100 | [diff] [blame] | 5202 | drbd_thread_current_set_cpu(thi); |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 5203 | |
| 5204 | rcu_read_lock(); |
| 5205 | nc = rcu_dereference(tconn->net_conf); |
| 5206 | ping_timeo = nc->ping_timeo; |
Andreas Gruenbacher | bb77d34 | 2011-05-04 15:25:35 +0200 | [diff] [blame] | 5207 | tcp_cork = nc->tcp_cork; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 5208 | ping_int = nc->ping_int; |
| 5209 | rcu_read_unlock(); |
| 5210 | |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 5211 | if (test_and_clear_bit(SEND_PING, &tconn->flags)) { |
Andreas Gruenbacher | a17647a | 2011-04-01 12:49:42 +0200 | [diff] [blame] | 5212 | if (drbd_send_ping(tconn)) { |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 5213 | conn_err(tconn, "drbd_send_ping has failed\n"); |
Andreas Gruenbacher | 841ce24 | 2010-12-15 19:31:20 +0100 | [diff] [blame] | 5214 | goto reconnect; |
| 5215 | } |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 5216 | tconn->meta.socket->sk->sk_rcvtimeo = ping_timeo * HZ / 10; |
| 5217 | ping_timeout_active = true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5218 | } |
| 5219 | |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 5220 | /* TODO: conditionally cork; it may hurt latency if we cork without |
| 5221 | much to send */ |
Andreas Gruenbacher | bb77d34 | 2011-05-04 15:25:35 +0200 | [diff] [blame] | 5222 | if (tcp_cork) |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 5223 | drbd_tcp_cork(tconn->meta.socket); |
Andreas Gruenbacher | a990be4 | 2011-04-06 17:56:48 +0200 | [diff] [blame] | 5224 | if (tconn_finish_peer_reqs(tconn)) { |
| 5225 | conn_err(tconn, "tconn_finish_peer_reqs() failed\n"); |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 5226 | goto reconnect; |
Philipp Reisner | 082a343 | 2011-03-15 16:05:42 +0100 | [diff] [blame] | 5227 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5228 | /* but unconditionally uncork unless disabled */ |
Andreas Gruenbacher | bb77d34 | 2011-05-04 15:25:35 +0200 | [diff] [blame] | 5229 | if (tcp_cork) |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 5230 | drbd_tcp_uncork(tconn->meta.socket); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5231 | |
| 5232 | /* short circuit, recv_msg would return EINTR anyways. */ |
| 5233 | if (signal_pending(current)) |
| 5234 | continue; |
| 5235 | |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 5236 | rv = drbd_recv_short(tconn->meta.socket, buf, expect-received, 0); |
| 5237 | clear_bit(SIGNAL_ASENDER, &tconn->flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5238 | |
| 5239 | flush_signals(current); |
| 5240 | |
| 5241 | /* Note: |
| 5242 | * -EINTR (on meta) we got a signal |
| 5243 | * -EAGAIN (on meta) rcvtimeo expired |
| 5244 | * -ECONNRESET other side closed the connection |
| 5245 | * -ERESTARTSYS (on data) we got a signal |
| 5246 | * rv < 0 other than above: unexpected error! |
| 5247 | * rv == expected: full header or command |
| 5248 | * rv < expected: "woken" by signal during receive |
| 5249 | * rv == 0 : "connection shut down by peer" |
| 5250 | */ |
| 5251 | if (likely(rv > 0)) { |
| 5252 | received += rv; |
| 5253 | buf += rv; |
| 5254 | } else if (rv == 0) { |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 5255 | conn_err(tconn, "meta connection shut down by peer.\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5256 | goto reconnect; |
| 5257 | } else if (rv == -EAGAIN) { |
Lars Ellenberg | cb6518c | 2011-06-20 14:44:45 +0200 | [diff] [blame] | 5258 | /* If the data socket received something meanwhile, |
| 5259 | * that is good enough: peer is still alive. */ |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 5260 | if (time_after(tconn->last_received, |
| 5261 | jiffies - tconn->meta.socket->sk->sk_rcvtimeo)) |
Lars Ellenberg | cb6518c | 2011-06-20 14:44:45 +0200 | [diff] [blame] | 5262 | continue; |
Lars Ellenberg | f36af18 | 2011-03-09 22:44:55 +0100 | [diff] [blame] | 5263 | if (ping_timeout_active) { |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 5264 | conn_err(tconn, "PingAck did not arrive in time.\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5265 | goto reconnect; |
| 5266 | } |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 5267 | set_bit(SEND_PING, &tconn->flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5268 | continue; |
| 5269 | } else if (rv == -EINTR) { |
| 5270 | continue; |
| 5271 | } else { |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 5272 | conn_err(tconn, "sock_recvmsg returned %d\n", rv); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5273 | goto reconnect; |
| 5274 | } |
| 5275 | |
| 5276 | if (received == expect && cmd == NULL) { |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 5277 | if (decode_header(tconn, tconn->meta.rbuf, &pi)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5278 | goto reconnect; |
Andreas Gruenbacher | 7201b97 | 2011-03-14 18:23:00 +0100 | [diff] [blame] | 5279 | cmd = &asender_tbl[pi.cmd]; |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 5280 | if (pi.cmd >= ARRAY_SIZE(asender_tbl) || !cmd->fn) { |
Andreas Gruenbacher | 2fcb8f3 | 2011-07-03 11:41:08 +0200 | [diff] [blame] | 5281 | conn_err(tconn, "Unexpected meta packet %s (0x%04x)\n", |
| 5282 | cmdname(pi.cmd), pi.cmd); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5283 | goto disconnect; |
| 5284 | } |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 5285 | expect = header_size + cmd->pkt_size; |
Andreas Gruenbacher | 52b061a | 2011-03-30 11:38:49 +0200 | [diff] [blame] | 5286 | if (pi.size != expect - header_size) { |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 5287 | 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] | 5288 | pi.cmd, pi.size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5289 | goto reconnect; |
Philipp Reisner | 257d0af | 2011-01-26 12:15:29 +0100 | [diff] [blame] | 5290 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5291 | } |
| 5292 | if (received == expect) { |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5293 | bool err; |
Philipp Reisner | a4fbda8 | 2011-03-16 11:13:17 +0100 | [diff] [blame] | 5294 | |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5295 | err = cmd->fn(tconn, &pi); |
| 5296 | if (err) { |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 5297 | conn_err(tconn, "%pf failed\n", cmd->fn); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5298 | goto reconnect; |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 5299 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5300 | |
Philipp Reisner | a4fbda8 | 2011-03-16 11:13:17 +0100 | [diff] [blame] | 5301 | tconn->last_received = jiffies; |
| 5302 | |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 5303 | if (cmd == &asender_tbl[P_PING_ACK]) { |
| 5304 | /* restore idle timeout */ |
| 5305 | tconn->meta.socket->sk->sk_rcvtimeo = ping_int * HZ; |
| 5306 | ping_timeout_active = false; |
| 5307 | } |
Lars Ellenberg | f36af18 | 2011-03-09 22:44:55 +0100 | [diff] [blame] | 5308 | |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 5309 | buf = tconn->meta.rbuf; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5310 | received = 0; |
Andreas Gruenbacher | 52b061a | 2011-03-30 11:38:49 +0200 | [diff] [blame] | 5311 | expect = header_size; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5312 | cmd = NULL; |
| 5313 | } |
| 5314 | } |
| 5315 | |
| 5316 | if (0) { |
| 5317 | reconnect: |
Philipp Reisner | bbeb641 | 2011-02-10 13:45:46 +0100 | [diff] [blame] | 5318 | conn_request_state(tconn, NS(conn, C_NETWORK_FAILURE), CS_HARD); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5319 | } |
| 5320 | if (0) { |
| 5321 | disconnect: |
Philipp Reisner | bbeb641 | 2011-02-10 13:45:46 +0100 | [diff] [blame] | 5322 | conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5323 | } |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 5324 | clear_bit(SIGNAL_ASENDER, &tconn->flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5325 | |
Philipp Reisner | 32862ec | 2011-02-08 16:41:01 +0100 | [diff] [blame] | 5326 | conn_info(tconn, "asender terminated\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5327 | |
| 5328 | return 0; |
| 5329 | } |