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" |
Andreas Gruenbacher | a3603a6 | 2011-05-30 11:47:37 +0200 | [diff] [blame] | 47 | #include "drbd_protocol.h" |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 48 | #include "drbd_req.h" |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 49 | #include "drbd_vli.h" |
| 50 | |
Lars Ellenberg | 20c68fd | 2014-04-28 18:43:25 +0200 | [diff] [blame] | 51 | #define PRO_FEATURES (FF_TRIM) |
| 52 | |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 53 | struct packet_info { |
| 54 | enum drbd_packet cmd; |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 55 | unsigned int size; |
| 56 | unsigned int vnr; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 57 | void *data; |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 58 | }; |
| 59 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 60 | enum finish_epoch { |
| 61 | FE_STILL_LIVE, |
| 62 | FE_DESTROYED, |
| 63 | FE_RECYCLED, |
| 64 | }; |
| 65 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 66 | static int drbd_do_features(struct drbd_connection *connection); |
| 67 | static int drbd_do_auth(struct drbd_connection *connection); |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 68 | static int drbd_disconnected(struct drbd_peer_device *); |
Lars Ellenberg | a0fb3c4 | 2014-04-28 18:43:23 +0200 | [diff] [blame] | 69 | static void conn_wait_active_ee_empty(struct drbd_connection *connection); |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 70 | static enum finish_epoch drbd_may_finish_epoch(struct drbd_connection *, struct drbd_epoch *, enum epoch_event); |
Andreas Gruenbacher | 99920dc | 2011-03-16 15:31:39 +0100 | [diff] [blame] | 71 | static int e_end_block(struct drbd_work *, int); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 72 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 73 | |
| 74 | #define GFP_TRY (__GFP_HIGHMEM | __GFP_NOWARN) |
| 75 | |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 76 | /* |
| 77 | * some helper functions to deal with single linked page lists, |
| 78 | * page->private being our "next" pointer. |
| 79 | */ |
| 80 | |
| 81 | /* If at least n pages are linked at head, get n pages off. |
| 82 | * Otherwise, don't modify head, and return NULL. |
| 83 | * Locking is the responsibility of the caller. |
| 84 | */ |
| 85 | static struct page *page_chain_del(struct page **head, int n) |
| 86 | { |
| 87 | struct page *page; |
| 88 | struct page *tmp; |
| 89 | |
| 90 | BUG_ON(!n); |
| 91 | BUG_ON(!head); |
| 92 | |
| 93 | page = *head; |
Philipp Reisner | 23ce422 | 2010-05-20 13:35:31 +0200 | [diff] [blame] | 94 | |
| 95 | if (!page) |
| 96 | return NULL; |
| 97 | |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 98 | while (page) { |
| 99 | tmp = page_chain_next(page); |
| 100 | if (--n == 0) |
| 101 | break; /* found sufficient pages */ |
| 102 | if (tmp == NULL) |
| 103 | /* insufficient pages, don't use any of them. */ |
| 104 | return NULL; |
| 105 | page = tmp; |
| 106 | } |
| 107 | |
| 108 | /* add end of list marker for the returned list */ |
| 109 | set_page_private(page, 0); |
| 110 | /* actual return value, and adjustment of head */ |
| 111 | page = *head; |
| 112 | *head = tmp; |
| 113 | return page; |
| 114 | } |
| 115 | |
| 116 | /* may be used outside of locks to find the tail of a (usually short) |
| 117 | * "private" page chain, before adding it back to a global chain head |
| 118 | * with page_chain_add() under a spinlock. */ |
| 119 | static struct page *page_chain_tail(struct page *page, int *len) |
| 120 | { |
| 121 | struct page *tmp; |
| 122 | int i = 1; |
| 123 | while ((tmp = page_chain_next(page))) |
| 124 | ++i, page = tmp; |
| 125 | if (len) |
| 126 | *len = i; |
| 127 | return page; |
| 128 | } |
| 129 | |
| 130 | static int page_chain_free(struct page *page) |
| 131 | { |
| 132 | struct page *tmp; |
| 133 | int i = 0; |
| 134 | page_chain_for_each_safe(page, tmp) { |
| 135 | put_page(page); |
| 136 | ++i; |
| 137 | } |
| 138 | return i; |
| 139 | } |
| 140 | |
| 141 | static void page_chain_add(struct page **head, |
| 142 | struct page *chain_first, struct page *chain_last) |
| 143 | { |
| 144 | #if 1 |
| 145 | struct page *tmp; |
| 146 | tmp = page_chain_tail(chain_first, NULL); |
| 147 | BUG_ON(tmp != chain_last); |
| 148 | #endif |
| 149 | |
| 150 | /* add chain to head */ |
| 151 | set_page_private(chain_last, (unsigned long)*head); |
| 152 | *head = chain_first; |
| 153 | } |
| 154 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 155 | static struct page *__drbd_alloc_pages(struct drbd_device *device, |
Andreas Gruenbacher | 18c2d52 | 2011-04-07 21:08:50 +0200 | [diff] [blame] | 156 | unsigned int number) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 157 | { |
| 158 | struct page *page = NULL; |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 159 | struct page *tmp = NULL; |
Andreas Gruenbacher | 18c2d52 | 2011-04-07 21:08:50 +0200 | [diff] [blame] | 160 | unsigned int i = 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 161 | |
| 162 | /* Yes, testing drbd_pp_vacant outside the lock is racy. |
| 163 | * So what. It saves a spin_lock. */ |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 164 | if (drbd_pp_vacant >= number) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 165 | spin_lock(&drbd_pp_lock); |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 166 | page = page_chain_del(&drbd_pp_pool, number); |
| 167 | if (page) |
| 168 | drbd_pp_vacant -= number; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 169 | spin_unlock(&drbd_pp_lock); |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 170 | if (page) |
| 171 | return page; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 172 | } |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 173 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 174 | /* GFP_TRY, because we must not cause arbitrary write-out: in a DRBD |
| 175 | * "criss-cross" setup, that might cause write-out on some other DRBD, |
| 176 | * 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] | 177 | for (i = 0; i < number; i++) { |
| 178 | tmp = alloc_page(GFP_TRY); |
| 179 | if (!tmp) |
| 180 | break; |
| 181 | set_page_private(tmp, (unsigned long)page); |
| 182 | page = tmp; |
| 183 | } |
| 184 | |
| 185 | if (i == number) |
| 186 | return page; |
| 187 | |
| 188 | /* Not enough pages immediately available this time. |
Andreas Gruenbacher | c37c8ec | 2011-04-07 21:02:09 +0200 | [diff] [blame] | 189 | * No need to jump around here, drbd_alloc_pages will retry this |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 190 | * function "soon". */ |
| 191 | if (page) { |
| 192 | tmp = page_chain_tail(page, NULL); |
| 193 | spin_lock(&drbd_pp_lock); |
| 194 | page_chain_add(&drbd_pp_pool, page, tmp); |
| 195 | drbd_pp_vacant += i; |
| 196 | spin_unlock(&drbd_pp_lock); |
| 197 | } |
| 198 | return NULL; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 201 | static void reclaim_finished_net_peer_reqs(struct drbd_device *device, |
Andreas Gruenbacher | a990be4 | 2011-04-06 17:56:48 +0200 | [diff] [blame] | 202 | struct list_head *to_be_freed) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 203 | { |
Andreas Gruenbacher | a8cd15b | 2011-08-25 15:49:40 +0200 | [diff] [blame] | 204 | struct drbd_peer_request *peer_req, *tmp; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 205 | |
| 206 | /* The EEs are always appended to the end of the list. Since |
| 207 | they are sent in order over the wire, they have to finish |
| 208 | in order. As soon as we see the first not finished we can |
| 209 | stop to examine the list... */ |
| 210 | |
Andreas Gruenbacher | a8cd15b | 2011-08-25 15:49:40 +0200 | [diff] [blame] | 211 | list_for_each_entry_safe(peer_req, tmp, &device->net_ee, 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; |
Andreas Gruenbacher | a8cd15b | 2011-08-25 15:49:40 +0200 | [diff] [blame] | 214 | list_move(&peer_req->w.list, to_be_freed); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
Philipp Reisner | 668700b | 2015-03-16 16:08:29 +0100 | [diff] [blame] | 218 | static void drbd_reclaim_net_peer_reqs(struct drbd_device *device) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 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 | |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 223 | spin_lock_irq(&device->resource->req_lock); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 224 | reclaim_finished_net_peer_reqs(device, &reclaimed); |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 225 | spin_unlock_irq(&device->resource->req_lock); |
Andreas Gruenbacher | a8cd15b | 2011-08-25 15:49:40 +0200 | [diff] [blame] | 226 | list_for_each_entry_safe(peer_req, t, &reclaimed, w.list) |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 227 | drbd_free_net_peer_req(device, peer_req); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Philipp Reisner | 668700b | 2015-03-16 16:08:29 +0100 | [diff] [blame] | 230 | static void conn_reclaim_net_peer_reqs(struct drbd_connection *connection) |
| 231 | { |
| 232 | struct drbd_peer_device *peer_device; |
| 233 | int vnr; |
| 234 | |
| 235 | rcu_read_lock(); |
| 236 | idr_for_each_entry(&connection->peer_devices, peer_device, vnr) { |
| 237 | struct drbd_device *device = peer_device->device; |
| 238 | if (!atomic_read(&device->pp_in_use_by_net)) |
| 239 | continue; |
| 240 | |
| 241 | kref_get(&device->kref); |
| 242 | rcu_read_unlock(); |
| 243 | drbd_reclaim_net_peer_reqs(device); |
| 244 | kref_put(&device->kref, drbd_destroy_device); |
| 245 | rcu_read_lock(); |
| 246 | } |
| 247 | rcu_read_unlock(); |
| 248 | } |
| 249 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 250 | /** |
Andreas Gruenbacher | c37c8ec | 2011-04-07 21:02:09 +0200 | [diff] [blame] | 251 | * drbd_alloc_pages() - Returns @number pages, retries forever (or until signalled) |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 252 | * @device: DRBD device. |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 253 | * @number: number of pages requested |
| 254 | * @retry: whether to retry, if not enough pages are available right now |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 255 | * |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 256 | * Tries to allocate number pages, first from our own page pool, then from |
Lars Ellenberg | 0e49d7b | 2014-04-28 18:43:18 +0200 | [diff] [blame] | 257 | * the kernel. |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 258 | * Possibly retry until DRBD frees sufficient pages somewhere else. |
| 259 | * |
Lars Ellenberg | 0e49d7b | 2014-04-28 18:43:18 +0200 | [diff] [blame] | 260 | * If this allocation would exceed the max_buffers setting, we throttle |
| 261 | * allocation (schedule_timeout) to give the system some room to breathe. |
| 262 | * |
| 263 | * We do not use max-buffers as hard limit, because it could lead to |
| 264 | * congestion and further to a distributed deadlock during online-verify or |
| 265 | * (checksum based) resync, if the max-buffers, socket buffer sizes and |
| 266 | * resync-rate settings are mis-configured. |
| 267 | * |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 268 | * Returns a page chain linked via page->private. |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 269 | */ |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 270 | struct page *drbd_alloc_pages(struct drbd_peer_device *peer_device, unsigned int number, |
Andreas Gruenbacher | c37c8ec | 2011-04-07 21:02:09 +0200 | [diff] [blame] | 271 | bool retry) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 272 | { |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 273 | struct drbd_device *device = peer_device->device; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 274 | struct page *page = NULL; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 275 | struct net_conf *nc; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 276 | DEFINE_WAIT(wait); |
Lars Ellenberg | 0e49d7b | 2014-04-28 18:43:18 +0200 | [diff] [blame] | 277 | unsigned int mxb; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 278 | |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 279 | rcu_read_lock(); |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 280 | nc = rcu_dereference(peer_device->connection->net_conf); |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 281 | mxb = nc ? nc->max_buffers : 1000000; |
| 282 | rcu_read_unlock(); |
| 283 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 284 | if (atomic_read(&device->pp_in_use) < mxb) |
| 285 | page = __drbd_alloc_pages(device, number); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 286 | |
Philipp Reisner | 668700b | 2015-03-16 16:08:29 +0100 | [diff] [blame] | 287 | /* Try to keep the fast path fast, but occasionally we need |
| 288 | * to reclaim the pages we lended to the network stack. */ |
| 289 | if (page && atomic_read(&device->pp_in_use_by_net) > 512) |
| 290 | drbd_reclaim_net_peer_reqs(device); |
| 291 | |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 292 | while (page == NULL) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 293 | prepare_to_wait(&drbd_pp_wait, &wait, TASK_INTERRUPTIBLE); |
| 294 | |
Philipp Reisner | 668700b | 2015-03-16 16:08:29 +0100 | [diff] [blame] | 295 | drbd_reclaim_net_peer_reqs(device); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 296 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 297 | if (atomic_read(&device->pp_in_use) < mxb) { |
| 298 | page = __drbd_alloc_pages(device, number); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 299 | if (page) |
| 300 | break; |
| 301 | } |
| 302 | |
| 303 | if (!retry) |
| 304 | break; |
| 305 | |
| 306 | if (signal_pending(current)) { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 307 | drbd_warn(device, "drbd_alloc_pages interrupted!\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 308 | break; |
| 309 | } |
| 310 | |
Lars Ellenberg | 0e49d7b | 2014-04-28 18:43:18 +0200 | [diff] [blame] | 311 | if (schedule_timeout(HZ/10) == 0) |
| 312 | mxb = UINT_MAX; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 313 | } |
| 314 | finish_wait(&drbd_pp_wait, &wait); |
| 315 | |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 316 | if (page) |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 317 | atomic_add(number, &device->pp_in_use); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 318 | return page; |
| 319 | } |
| 320 | |
Andreas Gruenbacher | c37c8ec | 2011-04-07 21:02:09 +0200 | [diff] [blame] | 321 | /* Must not be used from irq, as that may deadlock: see drbd_alloc_pages. |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 322 | * Is also used from inside an other spin_lock_irq(&resource->req_lock); |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 323 | * Either links the page chain back to the global pool, |
| 324 | * or returns all pages to the system. */ |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 325 | static void drbd_free_pages(struct drbd_device *device, struct page *page, int is_net) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 326 | { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 327 | atomic_t *a = is_net ? &device->pp_in_use_by_net : &device->pp_in_use; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 328 | int i; |
Lars Ellenberg | 435f074 | 2010-09-06 12:30:25 +0200 | [diff] [blame] | 329 | |
Lars Ellenberg | a73ff32 | 2012-06-25 19:15:38 +0200 | [diff] [blame] | 330 | if (page == NULL) |
| 331 | return; |
| 332 | |
Philipp Reisner | 81a5d60 | 2011-02-22 19:53:16 -0500 | [diff] [blame] | 333 | if (drbd_pp_vacant > (DRBD_MAX_BIO_SIZE/PAGE_SIZE) * minor_count) |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 334 | i = page_chain_free(page); |
| 335 | else { |
| 336 | struct page *tmp; |
| 337 | tmp = page_chain_tail(page, &i); |
| 338 | spin_lock(&drbd_pp_lock); |
| 339 | page_chain_add(&drbd_pp_pool, page, tmp); |
| 340 | drbd_pp_vacant += i; |
| 341 | spin_unlock(&drbd_pp_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 342 | } |
Lars Ellenberg | 435f074 | 2010-09-06 12:30:25 +0200 | [diff] [blame] | 343 | i = atomic_sub_return(i, a); |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 344 | if (i < 0) |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 345 | drbd_warn(device, "ASSERTION FAILED: %s: %d < 0\n", |
Lars Ellenberg | 435f074 | 2010-09-06 12:30:25 +0200 | [diff] [blame] | 346 | is_net ? "pp_in_use_by_net" : "pp_in_use", i); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 347 | wake_up(&drbd_pp_wait); |
| 348 | } |
| 349 | |
| 350 | /* |
| 351 | You need to hold the req_lock: |
| 352 | _drbd_wait_ee_list_empty() |
| 353 | |
| 354 | You must not have the req_lock: |
Andreas Gruenbacher | 3967deb | 2011-04-06 16:16:56 +0200 | [diff] [blame] | 355 | drbd_free_peer_req() |
Andreas Gruenbacher | 0db5536 | 2011-04-06 16:09:15 +0200 | [diff] [blame] | 356 | drbd_alloc_peer_req() |
Andreas Gruenbacher | 7721f56 | 2011-04-06 17:14:02 +0200 | [diff] [blame] | 357 | drbd_free_peer_reqs() |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 358 | drbd_ee_fix_bhs() |
Andreas Gruenbacher | a990be4 | 2011-04-06 17:56:48 +0200 | [diff] [blame] | 359 | drbd_finish_peer_reqs() |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 360 | drbd_clear_done_ee() |
| 361 | drbd_wait_ee_list_empty() |
| 362 | */ |
| 363 | |
Andreas Gruenbacher | f6ffca9 | 2011-02-04 15:30:34 +0100 | [diff] [blame] | 364 | struct drbd_peer_request * |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 365 | drbd_alloc_peer_req(struct drbd_peer_device *peer_device, u64 id, sector_t sector, |
Lars Ellenberg | a0fb3c4 | 2014-04-28 18:43:23 +0200 | [diff] [blame] | 366 | unsigned int data_size, bool has_payload, gfp_t gfp_mask) __must_hold(local) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 367 | { |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 368 | struct drbd_device *device = peer_device->device; |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 369 | struct drbd_peer_request *peer_req; |
Lars Ellenberg | a73ff32 | 2012-06-25 19:15:38 +0200 | [diff] [blame] | 370 | struct page *page = NULL; |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 371 | unsigned nr_pages = (data_size + PAGE_SIZE -1) >> PAGE_SHIFT; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 372 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 373 | if (drbd_insert_fault(device, DRBD_FAULT_AL_EE)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 374 | return NULL; |
| 375 | |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 376 | peer_req = mempool_alloc(drbd_ee_mempool, gfp_mask & ~__GFP_HIGHMEM); |
| 377 | if (!peer_req) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 378 | if (!(gfp_mask & __GFP_NOWARN)) |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 379 | drbd_err(device, "%s: allocation failed\n", __func__); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 380 | return NULL; |
| 381 | } |
| 382 | |
Lars Ellenberg | a0fb3c4 | 2014-04-28 18:43:23 +0200 | [diff] [blame] | 383 | if (has_payload && data_size) { |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 384 | page = drbd_alloc_pages(peer_device, nr_pages, |
| 385 | gfpflags_allow_blocking(gfp_mask)); |
Lars Ellenberg | a73ff32 | 2012-06-25 19:15:38 +0200 | [diff] [blame] | 386 | if (!page) |
| 387 | goto fail; |
| 388 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 389 | |
Lars Ellenberg | c5a2c15 | 2014-05-08 10:08:05 +0200 | [diff] [blame] | 390 | memset(peer_req, 0, sizeof(*peer_req)); |
| 391 | INIT_LIST_HEAD(&peer_req->w.list); |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 392 | drbd_clear_interval(&peer_req->i); |
| 393 | peer_req->i.size = data_size; |
| 394 | peer_req->i.sector = sector; |
Lars Ellenberg | c5a2c15 | 2014-05-08 10:08:05 +0200 | [diff] [blame] | 395 | peer_req->submit_jif = jiffies; |
Andreas Gruenbacher | a8cd15b | 2011-08-25 15:49:40 +0200 | [diff] [blame] | 396 | peer_req->peer_device = peer_device; |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 397 | peer_req->pages = page; |
Andreas Gruenbacher | 9a8e775 | 2011-01-11 14:04:09 +0100 | [diff] [blame] | 398 | /* |
| 399 | * The block_id is opaque to the receiver. It is not endianness |
| 400 | * converted, and sent back to the sender unchanged. |
| 401 | */ |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 402 | peer_req->block_id = id; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 403 | |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 404 | return peer_req; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 405 | |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 406 | fail: |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 407 | mempool_free(peer_req, drbd_ee_mempool); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 408 | return NULL; |
| 409 | } |
| 410 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 411 | void __drbd_free_peer_req(struct drbd_device *device, struct drbd_peer_request *peer_req, |
Andreas Gruenbacher | f6ffca9 | 2011-02-04 15:30:34 +0100 | [diff] [blame] | 412 | int is_net) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 413 | { |
Lars Ellenberg | 21ae5d7 | 2014-05-05 23:42:24 +0200 | [diff] [blame] | 414 | might_sleep(); |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 415 | if (peer_req->flags & EE_HAS_DIGEST) |
| 416 | kfree(peer_req->digest); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 417 | drbd_free_pages(device, peer_req->pages, is_net); |
Andreas Gruenbacher | 0b0ba1e | 2011-06-27 16:23:33 +0200 | [diff] [blame] | 418 | D_ASSERT(device, atomic_read(&peer_req->pending_bios) == 0); |
| 419 | D_ASSERT(device, drbd_interval_empty(&peer_req->i)); |
Lars Ellenberg | 21ae5d7 | 2014-05-05 23:42:24 +0200 | [diff] [blame] | 420 | if (!expect(!(peer_req->flags & EE_CALL_AL_COMPLETE_IO))) { |
| 421 | peer_req->flags &= ~EE_CALL_AL_COMPLETE_IO; |
| 422 | drbd_al_complete_io(device, &peer_req->i); |
| 423 | } |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 424 | mempool_free(peer_req, drbd_ee_mempool); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 427 | int drbd_free_peer_reqs(struct drbd_device *device, struct list_head *list) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 428 | { |
| 429 | LIST_HEAD(work_list); |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 430 | struct drbd_peer_request *peer_req, *t; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 431 | int count = 0; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 432 | int is_net = list == &device->net_ee; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 433 | |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 434 | spin_lock_irq(&device->resource->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 435 | list_splice_init(list, &work_list); |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 436 | spin_unlock_irq(&device->resource->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 437 | |
Andreas Gruenbacher | a8cd15b | 2011-08-25 15:49:40 +0200 | [diff] [blame] | 438 | list_for_each_entry_safe(peer_req, t, &work_list, w.list) { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 439 | __drbd_free_peer_req(device, peer_req, is_net); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 440 | count++; |
| 441 | } |
| 442 | return count; |
| 443 | } |
| 444 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 445 | /* |
Andreas Gruenbacher | a990be4 | 2011-04-06 17:56:48 +0200 | [diff] [blame] | 446 | * See also comments in _req_mod(,BARRIER_ACKED) and receive_Barrier. |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 447 | */ |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 448 | static int drbd_finish_peer_reqs(struct drbd_device *device) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 449 | { |
| 450 | LIST_HEAD(work_list); |
| 451 | LIST_HEAD(reclaimed); |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 452 | struct drbd_peer_request *peer_req, *t; |
Andreas Gruenbacher | e2b3032 | 2011-03-16 17:16:12 +0100 | [diff] [blame] | 453 | int err = 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 454 | |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 455 | spin_lock_irq(&device->resource->req_lock); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 456 | reclaim_finished_net_peer_reqs(device, &reclaimed); |
| 457 | list_splice_init(&device->done_ee, &work_list); |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 458 | spin_unlock_irq(&device->resource->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 459 | |
Andreas Gruenbacher | a8cd15b | 2011-08-25 15:49:40 +0200 | [diff] [blame] | 460 | list_for_each_entry_safe(peer_req, t, &reclaimed, w.list) |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 461 | drbd_free_net_peer_req(device, peer_req); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 462 | |
| 463 | /* possible callbacks here: |
Lars Ellenberg | d4dabbe | 2012-08-01 12:33:51 +0200 | [diff] [blame] | 464 | * e_end_block, and e_end_resync_block, e_send_superseded. |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 465 | * all ignore the last argument. |
| 466 | */ |
Andreas Gruenbacher | a8cd15b | 2011-08-25 15:49:40 +0200 | [diff] [blame] | 467 | list_for_each_entry_safe(peer_req, t, &work_list, w.list) { |
Andreas Gruenbacher | e2b3032 | 2011-03-16 17:16:12 +0100 | [diff] [blame] | 468 | int err2; |
| 469 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 470 | /* list_del not necessary, next/prev members not touched */ |
Andreas Gruenbacher | a8cd15b | 2011-08-25 15:49:40 +0200 | [diff] [blame] | 471 | err2 = peer_req->w.cb(&peer_req->w, !!err); |
Andreas Gruenbacher | e2b3032 | 2011-03-16 17:16:12 +0100 | [diff] [blame] | 472 | if (!err) |
| 473 | err = err2; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 474 | drbd_free_peer_req(device, peer_req); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 475 | } |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 476 | wake_up(&device->ee_wait); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 477 | |
Andreas Gruenbacher | e2b3032 | 2011-03-16 17:16:12 +0100 | [diff] [blame] | 478 | return err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 481 | static void _drbd_wait_ee_list_empty(struct drbd_device *device, |
Andreas Gruenbacher | d4da153 | 2011-04-07 00:06:56 +0200 | [diff] [blame] | 482 | struct list_head *head) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 483 | { |
| 484 | DEFINE_WAIT(wait); |
| 485 | |
| 486 | /* avoids spin_lock/unlock |
| 487 | * and calling prepare_to_wait in the fast path */ |
| 488 | while (!list_empty(head)) { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 489 | prepare_to_wait(&device->ee_wait, &wait, TASK_UNINTERRUPTIBLE); |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 490 | spin_unlock_irq(&device->resource->req_lock); |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 491 | io_schedule(); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 492 | finish_wait(&device->ee_wait, &wait); |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 493 | spin_lock_irq(&device->resource->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 497 | static void drbd_wait_ee_list_empty(struct drbd_device *device, |
Andreas Gruenbacher | d4da153 | 2011-04-07 00:06:56 +0200 | [diff] [blame] | 498 | struct list_head *head) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 499 | { |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 500 | spin_lock_irq(&device->resource->req_lock); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 501 | _drbd_wait_ee_list_empty(device, head); |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 502 | spin_unlock_irq(&device->resource->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Philipp Reisner | dbd9eea | 2011-02-07 15:34:16 +0100 | [diff] [blame] | 505 | 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] | 506 | { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 507 | struct kvec iov = { |
| 508 | .iov_base = buf, |
| 509 | .iov_len = size, |
| 510 | }; |
| 511 | struct msghdr msg = { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 512 | .msg_flags = (flags ? flags : MSG_WAITALL | MSG_NOSIGNAL) |
| 513 | }; |
Al Viro | f730c84 | 2014-02-08 21:07:38 -0500 | [diff] [blame] | 514 | return kernel_recvmsg(sock, &msg, &iov, 1, size, msg.msg_flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 515 | } |
| 516 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 517 | static int drbd_recv(struct drbd_connection *connection, void *buf, size_t size) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 518 | { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 519 | int rv; |
| 520 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 521 | rv = drbd_recv_short(connection->data.socket, buf, size, 0); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 522 | |
Philipp Reisner | dbd0820 | 2012-08-17 16:55:47 +0200 | [diff] [blame] | 523 | if (rv < 0) { |
| 524 | if (rv == -ECONNRESET) |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 525 | drbd_info(connection, "sock was reset by peer\n"); |
Philipp Reisner | dbd0820 | 2012-08-17 16:55:47 +0200 | [diff] [blame] | 526 | else if (rv != -ERESTARTSYS) |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 527 | drbd_err(connection, "sock_recvmsg returned %d\n", rv); |
Philipp Reisner | dbd0820 | 2012-08-17 16:55:47 +0200 | [diff] [blame] | 528 | } else if (rv == 0) { |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 529 | if (test_bit(DISCONNECT_SENT, &connection->flags)) { |
Philipp Reisner | b66623e | 2012-08-08 21:19:09 +0200 | [diff] [blame] | 530 | long t; |
| 531 | rcu_read_lock(); |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 532 | t = rcu_dereference(connection->net_conf)->ping_timeo * HZ/10; |
Philipp Reisner | b66623e | 2012-08-08 21:19:09 +0200 | [diff] [blame] | 533 | rcu_read_unlock(); |
| 534 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 535 | t = wait_event_timeout(connection->ping_wait, connection->cstate < C_WF_REPORT_PARAMS, t); |
Philipp Reisner | b66623e | 2012-08-08 21:19:09 +0200 | [diff] [blame] | 536 | |
Philipp Reisner | 599377a | 2012-08-17 14:50:22 +0200 | [diff] [blame] | 537 | if (t) |
| 538 | goto out; |
| 539 | } |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 540 | drbd_info(connection, "sock was shut down by peer\n"); |
Philipp Reisner | 599377a | 2012-08-17 14:50:22 +0200 | [diff] [blame] | 541 | } |
| 542 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 543 | if (rv != size) |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 544 | conn_request_state(connection, NS(conn, C_BROKEN_PIPE), CS_HARD); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 545 | |
Philipp Reisner | 599377a | 2012-08-17 14:50:22 +0200 | [diff] [blame] | 546 | out: |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 547 | return rv; |
| 548 | } |
| 549 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 550 | static int drbd_recv_all(struct drbd_connection *connection, void *buf, size_t size) |
Andreas Gruenbacher | c696774 | 2011-03-17 17:15:20 +0100 | [diff] [blame] | 551 | { |
| 552 | int err; |
| 553 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 554 | err = drbd_recv(connection, buf, size); |
Andreas Gruenbacher | c696774 | 2011-03-17 17:15:20 +0100 | [diff] [blame] | 555 | if (err != size) { |
| 556 | if (err >= 0) |
| 557 | err = -EIO; |
| 558 | } else |
| 559 | err = 0; |
| 560 | return err; |
| 561 | } |
| 562 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 563 | static int drbd_recv_all_warn(struct drbd_connection *connection, void *buf, size_t size) |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 564 | { |
| 565 | int err; |
| 566 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 567 | err = drbd_recv_all(connection, buf, size); |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 568 | if (err && !signal_pending(current)) |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 569 | drbd_warn(connection, "short read (expected size %d)\n", (int)size); |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 570 | return err; |
| 571 | } |
| 572 | |
Lars Ellenberg | 5dbf167 | 2010-05-25 16:18:01 +0200 | [diff] [blame] | 573 | /* quoting tcp(7): |
| 574 | * On individual connections, the socket buffer size must be set prior to the |
| 575 | * listen(2) or connect(2) calls in order to have it take effect. |
| 576 | * This is our wrapper to do so. |
| 577 | */ |
| 578 | static void drbd_setbufsize(struct socket *sock, unsigned int snd, |
| 579 | unsigned int rcv) |
| 580 | { |
| 581 | /* open coded SO_SNDBUF, SO_RCVBUF */ |
| 582 | if (snd) { |
| 583 | sock->sk->sk_sndbuf = snd; |
| 584 | sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK; |
| 585 | } |
| 586 | if (rcv) { |
| 587 | sock->sk->sk_rcvbuf = rcv; |
| 588 | sock->sk->sk_userlocks |= SOCK_RCVBUF_LOCK; |
| 589 | } |
| 590 | } |
| 591 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 592 | static struct socket *drbd_try_connect(struct drbd_connection *connection) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 593 | { |
| 594 | const char *what; |
| 595 | struct socket *sock; |
| 596 | struct sockaddr_in6 src_in6; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 597 | struct sockaddr_in6 peer_in6; |
| 598 | struct net_conf *nc; |
| 599 | int err, peer_addr_len, my_addr_len; |
Andreas Gruenbacher | 69ef82d | 2011-05-11 14:34:35 +0200 | [diff] [blame] | 600 | int sndbuf_size, rcvbuf_size, connect_int; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 601 | int disconnect_on_error = 1; |
| 602 | |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 603 | rcu_read_lock(); |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 604 | nc = rcu_dereference(connection->net_conf); |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 605 | if (!nc) { |
| 606 | rcu_read_unlock(); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 607 | return NULL; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 608 | } |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 609 | sndbuf_size = nc->sndbuf_size; |
| 610 | rcvbuf_size = nc->rcvbuf_size; |
Andreas Gruenbacher | 69ef82d | 2011-05-11 14:34:35 +0200 | [diff] [blame] | 611 | connect_int = nc->connect_int; |
Andreas Gruenbacher | 089c075 | 2011-06-14 18:28:09 +0200 | [diff] [blame] | 612 | rcu_read_unlock(); |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 613 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 614 | my_addr_len = min_t(int, connection->my_addr_len, sizeof(src_in6)); |
| 615 | memcpy(&src_in6, &connection->my_addr, my_addr_len); |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 616 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 617 | if (((struct sockaddr *)&connection->my_addr)->sa_family == AF_INET6) |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 618 | src_in6.sin6_port = 0; |
| 619 | else |
| 620 | ((struct sockaddr_in *)&src_in6)->sin_port = 0; /* AF_INET & AF_SCI */ |
| 621 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 622 | peer_addr_len = min_t(int, connection->peer_addr_len, sizeof(src_in6)); |
| 623 | memcpy(&peer_in6, &connection->peer_addr, peer_addr_len); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 624 | |
| 625 | what = "sock_create_kern"; |
Eric W. Biederman | eeb1bd5 | 2015-05-08 21:08:05 -0500 | [diff] [blame] | 626 | err = sock_create_kern(&init_net, ((struct sockaddr *)&src_in6)->sa_family, |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 627 | SOCK_STREAM, IPPROTO_TCP, &sock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 628 | if (err < 0) { |
| 629 | sock = NULL; |
| 630 | goto out; |
| 631 | } |
| 632 | |
| 633 | sock->sk->sk_rcvtimeo = |
Andreas Gruenbacher | 69ef82d | 2011-05-11 14:34:35 +0200 | [diff] [blame] | 634 | sock->sk->sk_sndtimeo = connect_int * HZ; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 635 | drbd_setbufsize(sock, sndbuf_size, rcvbuf_size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 636 | |
| 637 | /* explicitly bind to the configured IP as source IP |
| 638 | * for the outgoing connections. |
| 639 | * This is needed for multihomed hosts and to be |
| 640 | * able to use lo: interfaces for drbd. |
| 641 | * Make sure to use 0 as port number, so linux selects |
| 642 | * a free one dynamically. |
| 643 | */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 644 | what = "bind before connect"; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 645 | err = sock->ops->bind(sock, (struct sockaddr *) &src_in6, my_addr_len); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 646 | if (err < 0) |
| 647 | goto out; |
| 648 | |
| 649 | /* connect may fail, peer not yet available. |
| 650 | * stay C_WF_CONNECTION, don't go Disconnecting! */ |
| 651 | disconnect_on_error = 0; |
| 652 | what = "connect"; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 653 | 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] | 654 | |
| 655 | out: |
| 656 | if (err < 0) { |
| 657 | if (sock) { |
| 658 | sock_release(sock); |
| 659 | sock = NULL; |
| 660 | } |
| 661 | switch (-err) { |
| 662 | /* timeout, busy, signal pending */ |
| 663 | case ETIMEDOUT: case EAGAIN: case EINPROGRESS: |
| 664 | case EINTR: case ERESTARTSYS: |
| 665 | /* peer not (yet) available, network problem */ |
| 666 | case ECONNREFUSED: case ENETUNREACH: |
| 667 | case EHOSTDOWN: case EHOSTUNREACH: |
| 668 | disconnect_on_error = 0; |
| 669 | break; |
| 670 | default: |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 671 | drbd_err(connection, "%s failed, err = %d\n", what, err); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 672 | } |
| 673 | if (disconnect_on_error) |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 674 | conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_HARD); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 675 | } |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 676 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 677 | return sock; |
| 678 | } |
| 679 | |
Philipp Reisner | 7a426fd | 2012-07-12 14:22:37 +0200 | [diff] [blame] | 680 | struct accept_wait_data { |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 681 | struct drbd_connection *connection; |
Philipp Reisner | 7a426fd | 2012-07-12 14:22:37 +0200 | [diff] [blame] | 682 | struct socket *s_listen; |
| 683 | struct completion door_bell; |
| 684 | void (*original_sk_state_change)(struct sock *sk); |
| 685 | |
| 686 | }; |
| 687 | |
Andreas Gruenbacher | 715306f | 2012-08-10 17:00:30 +0200 | [diff] [blame] | 688 | static void drbd_incoming_connection(struct sock *sk) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 689 | { |
Philipp Reisner | 7a426fd | 2012-07-12 14:22:37 +0200 | [diff] [blame] | 690 | struct accept_wait_data *ad = sk->sk_user_data; |
Andreas Gruenbacher | 715306f | 2012-08-10 17:00:30 +0200 | [diff] [blame] | 691 | void (*state_change)(struct sock *sk); |
Philipp Reisner | 7a426fd | 2012-07-12 14:22:37 +0200 | [diff] [blame] | 692 | |
Andreas Gruenbacher | 715306f | 2012-08-10 17:00:30 +0200 | [diff] [blame] | 693 | state_change = ad->original_sk_state_change; |
| 694 | if (sk->sk_state == TCP_ESTABLISHED) |
| 695 | complete(&ad->door_bell); |
| 696 | state_change(sk); |
Philipp Reisner | 7a426fd | 2012-07-12 14:22:37 +0200 | [diff] [blame] | 697 | } |
| 698 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 699 | static int prepare_listen_socket(struct drbd_connection *connection, struct accept_wait_data *ad) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 700 | { |
Philipp Reisner | 1f3e509 | 2012-07-12 11:08:34 +0200 | [diff] [blame] | 701 | int err, sndbuf_size, rcvbuf_size, my_addr_len; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 702 | struct sockaddr_in6 my_addr; |
Philipp Reisner | 1f3e509 | 2012-07-12 11:08:34 +0200 | [diff] [blame] | 703 | struct socket *s_listen; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 704 | struct net_conf *nc; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 705 | const char *what; |
| 706 | |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 707 | rcu_read_lock(); |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 708 | nc = rcu_dereference(connection->net_conf); |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 709 | if (!nc) { |
| 710 | rcu_read_unlock(); |
Philipp Reisner | 7a426fd | 2012-07-12 14:22:37 +0200 | [diff] [blame] | 711 | return -EIO; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 712 | } |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 713 | sndbuf_size = nc->sndbuf_size; |
| 714 | rcvbuf_size = nc->rcvbuf_size; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 715 | rcu_read_unlock(); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 716 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 717 | my_addr_len = min_t(int, connection->my_addr_len, sizeof(struct sockaddr_in6)); |
| 718 | memcpy(&my_addr, &connection->my_addr, my_addr_len); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 719 | |
| 720 | what = "sock_create_kern"; |
Eric W. Biederman | eeb1bd5 | 2015-05-08 21:08:05 -0500 | [diff] [blame] | 721 | err = sock_create_kern(&init_net, ((struct sockaddr *)&my_addr)->sa_family, |
Philipp Reisner | 1f3e509 | 2012-07-12 11:08:34 +0200 | [diff] [blame] | 722 | SOCK_STREAM, IPPROTO_TCP, &s_listen); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 723 | if (err) { |
| 724 | s_listen = NULL; |
| 725 | goto out; |
| 726 | } |
| 727 | |
Philipp Reisner | 9868365 | 2012-11-09 14:18:43 +0100 | [diff] [blame] | 728 | s_listen->sk->sk_reuse = SK_CAN_REUSE; /* SO_REUSEADDR */ |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 729 | drbd_setbufsize(s_listen, sndbuf_size, rcvbuf_size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 730 | |
| 731 | what = "bind before listen"; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 732 | 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] | 733 | if (err < 0) |
| 734 | goto out; |
| 735 | |
Philipp Reisner | 7a426fd | 2012-07-12 14:22:37 +0200 | [diff] [blame] | 736 | ad->s_listen = s_listen; |
| 737 | write_lock_bh(&s_listen->sk->sk_callback_lock); |
| 738 | ad->original_sk_state_change = s_listen->sk->sk_state_change; |
Andreas Gruenbacher | 715306f | 2012-08-10 17:00:30 +0200 | [diff] [blame] | 739 | s_listen->sk->sk_state_change = drbd_incoming_connection; |
Philipp Reisner | 7a426fd | 2012-07-12 14:22:37 +0200 | [diff] [blame] | 740 | s_listen->sk->sk_user_data = ad; |
| 741 | write_unlock_bh(&s_listen->sk->sk_callback_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 742 | |
Philipp Reisner | 2820fd3 | 2012-07-12 10:22:48 +0200 | [diff] [blame] | 743 | what = "listen"; |
| 744 | err = s_listen->ops->listen(s_listen, 5); |
| 745 | if (err < 0) |
| 746 | goto out; |
| 747 | |
Philipp Reisner | 7a426fd | 2012-07-12 14:22:37 +0200 | [diff] [blame] | 748 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 749 | out: |
| 750 | if (s_listen) |
| 751 | sock_release(s_listen); |
| 752 | if (err < 0) { |
| 753 | if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 754 | drbd_err(connection, "%s failed, err = %d\n", what, err); |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 755 | conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_HARD); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 756 | } |
| 757 | } |
Philipp Reisner | 1f3e509 | 2012-07-12 11:08:34 +0200 | [diff] [blame] | 758 | |
Philipp Reisner | 7a426fd | 2012-07-12 14:22:37 +0200 | [diff] [blame] | 759 | return -EIO; |
Philipp Reisner | 1f3e509 | 2012-07-12 11:08:34 +0200 | [diff] [blame] | 760 | } |
| 761 | |
Andreas Gruenbacher | 715306f | 2012-08-10 17:00:30 +0200 | [diff] [blame] | 762 | static void unregister_state_change(struct sock *sk, struct accept_wait_data *ad) |
| 763 | { |
| 764 | write_lock_bh(&sk->sk_callback_lock); |
| 765 | sk->sk_state_change = ad->original_sk_state_change; |
| 766 | sk->sk_user_data = NULL; |
| 767 | write_unlock_bh(&sk->sk_callback_lock); |
| 768 | } |
| 769 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 770 | static struct socket *drbd_wait_for_connect(struct drbd_connection *connection, struct accept_wait_data *ad) |
Philipp Reisner | 1f3e509 | 2012-07-12 11:08:34 +0200 | [diff] [blame] | 771 | { |
| 772 | int timeo, connect_int, err = 0; |
| 773 | struct socket *s_estab = NULL; |
Philipp Reisner | 1f3e509 | 2012-07-12 11:08:34 +0200 | [diff] [blame] | 774 | struct net_conf *nc; |
| 775 | |
| 776 | rcu_read_lock(); |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 777 | nc = rcu_dereference(connection->net_conf); |
Philipp Reisner | 1f3e509 | 2012-07-12 11:08:34 +0200 | [diff] [blame] | 778 | if (!nc) { |
| 779 | rcu_read_unlock(); |
| 780 | return NULL; |
| 781 | } |
| 782 | connect_int = nc->connect_int; |
| 783 | rcu_read_unlock(); |
| 784 | |
| 785 | timeo = connect_int * HZ; |
Akinobu Mita | 38b682b2 | 2013-04-29 16:21:31 -0700 | [diff] [blame] | 786 | /* 28.5% random jitter */ |
| 787 | timeo += (prandom_u32() & 1) ? timeo / 7 : -timeo / 7; |
Philipp Reisner | 1f3e509 | 2012-07-12 11:08:34 +0200 | [diff] [blame] | 788 | |
Philipp Reisner | 7a426fd | 2012-07-12 14:22:37 +0200 | [diff] [blame] | 789 | err = wait_for_completion_interruptible_timeout(&ad->door_bell, timeo); |
| 790 | if (err <= 0) |
| 791 | return NULL; |
Philipp Reisner | 1f3e509 | 2012-07-12 11:08:34 +0200 | [diff] [blame] | 792 | |
Philipp Reisner | 7a426fd | 2012-07-12 14:22:37 +0200 | [diff] [blame] | 793 | err = kernel_accept(ad->s_listen, &s_estab, 0); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 794 | if (err < 0) { |
| 795 | if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 796 | drbd_err(connection, "accept failed, err = %d\n", err); |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 797 | conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_HARD); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 798 | } |
| 799 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 800 | |
Andreas Gruenbacher | 715306f | 2012-08-10 17:00:30 +0200 | [diff] [blame] | 801 | if (s_estab) |
| 802 | unregister_state_change(s_estab->sk, ad); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 803 | |
| 804 | return s_estab; |
| 805 | } |
| 806 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 807 | static int decode_header(struct drbd_connection *, void *, struct packet_info *); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 808 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 809 | static int send_first_packet(struct drbd_connection *connection, struct drbd_socket *sock, |
Andreas Gruenbacher | 9f5bdc3 | 2011-03-28 14:23:08 +0200 | [diff] [blame] | 810 | enum drbd_packet cmd) |
| 811 | { |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 812 | if (!conn_prepare_command(connection, sock)) |
Andreas Gruenbacher | 9f5bdc3 | 2011-03-28 14:23:08 +0200 | [diff] [blame] | 813 | return -EIO; |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 814 | return conn_send_command(connection, sock, cmd, 0, NULL, 0); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 815 | } |
| 816 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 817 | static int receive_first_packet(struct drbd_connection *connection, struct socket *sock) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 818 | { |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 819 | unsigned int header_size = drbd_header_size(connection); |
Andreas Gruenbacher | 9f5bdc3 | 2011-03-28 14:23:08 +0200 | [diff] [blame] | 820 | struct packet_info pi; |
Philipp Reisner | 4920e37 | 2014-03-18 14:40:13 +0100 | [diff] [blame] | 821 | struct net_conf *nc; |
Andreas Gruenbacher | 9f5bdc3 | 2011-03-28 14:23:08 +0200 | [diff] [blame] | 822 | int err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 823 | |
Philipp Reisner | 4920e37 | 2014-03-18 14:40:13 +0100 | [diff] [blame] | 824 | rcu_read_lock(); |
| 825 | nc = rcu_dereference(connection->net_conf); |
| 826 | if (!nc) { |
| 827 | rcu_read_unlock(); |
| 828 | return -EIO; |
| 829 | } |
| 830 | sock->sk->sk_rcvtimeo = nc->ping_timeo * 4 * HZ / 10; |
| 831 | rcu_read_unlock(); |
| 832 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 833 | err = drbd_recv_short(sock, connection->data.rbuf, header_size, 0); |
Andreas Gruenbacher | 9f5bdc3 | 2011-03-28 14:23:08 +0200 | [diff] [blame] | 834 | if (err != header_size) { |
| 835 | if (err >= 0) |
| 836 | err = -EIO; |
| 837 | return err; |
| 838 | } |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 839 | err = decode_header(connection, connection->data.rbuf, &pi); |
Andreas Gruenbacher | 9f5bdc3 | 2011-03-28 14:23:08 +0200 | [diff] [blame] | 840 | if (err) |
| 841 | return err; |
| 842 | return pi.cmd; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | /** |
| 846 | * drbd_socket_okay() - Free the socket if its connection is not okay |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 847 | * @sock: pointer to the pointer to the socket. |
| 848 | */ |
Philipp Reisner | 5d0b17f | 2014-03-18 14:24:35 +0100 | [diff] [blame] | 849 | static bool drbd_socket_okay(struct socket **sock) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 850 | { |
| 851 | int rr; |
| 852 | char tb[4]; |
| 853 | |
| 854 | if (!*sock) |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 855 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 856 | |
Philipp Reisner | dbd9eea | 2011-02-07 15:34:16 +0100 | [diff] [blame] | 857 | rr = drbd_recv_short(*sock, tb, 4, MSG_DONTWAIT | MSG_PEEK); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 858 | |
| 859 | if (rr > 0 || rr == -EAGAIN) { |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 860 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 861 | } else { |
| 862 | sock_release(*sock); |
| 863 | *sock = NULL; |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 864 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 865 | } |
| 866 | } |
Philipp Reisner | 5d0b17f | 2014-03-18 14:24:35 +0100 | [diff] [blame] | 867 | |
| 868 | static bool connection_established(struct drbd_connection *connection, |
| 869 | struct socket **sock1, |
| 870 | struct socket **sock2) |
| 871 | { |
| 872 | struct net_conf *nc; |
| 873 | int timeout; |
| 874 | bool ok; |
| 875 | |
| 876 | if (!*sock1 || !*sock2) |
| 877 | return false; |
| 878 | |
| 879 | rcu_read_lock(); |
| 880 | nc = rcu_dereference(connection->net_conf); |
| 881 | timeout = (nc->sock_check_timeo ?: nc->ping_timeo) * HZ / 10; |
| 882 | rcu_read_unlock(); |
| 883 | schedule_timeout_interruptible(timeout); |
| 884 | |
| 885 | ok = drbd_socket_okay(sock1); |
| 886 | ok = drbd_socket_okay(sock2) && ok; |
| 887 | |
| 888 | return ok; |
| 889 | } |
| 890 | |
Philipp Reisner | 2325eb6 | 2011-03-15 16:56:18 +0100 | [diff] [blame] | 891 | /* Gets called if a connection is established, or if a new minor gets created |
| 892 | in a connection */ |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 893 | int drbd_connected(struct drbd_peer_device *peer_device) |
Philipp Reisner | 907599e | 2011-02-08 11:25:37 +0100 | [diff] [blame] | 894 | { |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 895 | struct drbd_device *device = peer_device->device; |
Andreas Gruenbacher | 0829f5e | 2011-03-24 14:31:22 +0100 | [diff] [blame] | 896 | int err; |
Philipp Reisner | 907599e | 2011-02-08 11:25:37 +0100 | [diff] [blame] | 897 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 898 | atomic_set(&device->packet_seq, 0); |
| 899 | device->peer_seq = 0; |
Philipp Reisner | 907599e | 2011-02-08 11:25:37 +0100 | [diff] [blame] | 900 | |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 901 | device->state_mutex = peer_device->connection->agreed_pro_version < 100 ? |
| 902 | &peer_device->connection->cstate_mutex : |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 903 | &device->own_state_mutex; |
Philipp Reisner | 8410da8f0 | 2011-02-11 20:11:10 +0100 | [diff] [blame] | 904 | |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 905 | err = drbd_send_sync_param(peer_device); |
Andreas Gruenbacher | 0829f5e | 2011-03-24 14:31:22 +0100 | [diff] [blame] | 906 | if (!err) |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 907 | err = drbd_send_sizes(peer_device, 0, 0); |
Andreas Gruenbacher | 0829f5e | 2011-03-24 14:31:22 +0100 | [diff] [blame] | 908 | if (!err) |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 909 | err = drbd_send_uuids(peer_device); |
Andreas Gruenbacher | 0829f5e | 2011-03-24 14:31:22 +0100 | [diff] [blame] | 910 | if (!err) |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 911 | err = drbd_send_current_state(peer_device); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 912 | clear_bit(USE_DEGR_WFC_T, &device->flags); |
| 913 | clear_bit(RESIZE_PENDING, &device->flags); |
| 914 | atomic_set(&device->ap_in_flight, 0); |
| 915 | mod_timer(&device->request_timer, jiffies + HZ); /* just start it here. */ |
Andreas Gruenbacher | 0829f5e | 2011-03-24 14:31:22 +0100 | [diff] [blame] | 916 | return err; |
Philipp Reisner | 907599e | 2011-02-08 11:25:37 +0100 | [diff] [blame] | 917 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 918 | |
| 919 | /* |
| 920 | * return values: |
| 921 | * 1 yes, we have a valid connection |
| 922 | * 0 oops, did not work out, please try again |
| 923 | * -1 peer talks different language, |
| 924 | * no point in trying again, please go standalone. |
| 925 | * -2 We do not have a network config... |
| 926 | */ |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 927 | static int conn_connect(struct drbd_connection *connection) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 928 | { |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 929 | struct drbd_socket sock, msock; |
Andreas Gruenbacher | c06ece6 | 2011-06-21 17:23:59 +0200 | [diff] [blame] | 930 | struct drbd_peer_device *peer_device; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 931 | struct net_conf *nc; |
Philipp Reisner | 5d0b17f | 2014-03-18 14:24:35 +0100 | [diff] [blame] | 932 | int vnr, timeout, h; |
| 933 | bool discard_my_data, ok; |
Philipp Reisner | 197296f | 2012-03-26 16:47:11 +0200 | [diff] [blame] | 934 | enum drbd_state_rv rv; |
Philipp Reisner | 7a426fd | 2012-07-12 14:22:37 +0200 | [diff] [blame] | 935 | struct accept_wait_data ad = { |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 936 | .connection = connection, |
Philipp Reisner | 7a426fd | 2012-07-12 14:22:37 +0200 | [diff] [blame] | 937 | .door_bell = COMPLETION_INITIALIZER_ONSTACK(ad.door_bell), |
| 938 | }; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 939 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 940 | clear_bit(DISCONNECT_SENT, &connection->flags); |
| 941 | if (conn_request_state(connection, NS(conn, C_WF_CONNECTION), CS_VERBOSE) < SS_SUCCESS) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 942 | return -2; |
| 943 | |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 944 | mutex_init(&sock.mutex); |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 945 | sock.sbuf = connection->data.sbuf; |
| 946 | sock.rbuf = connection->data.rbuf; |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 947 | sock.socket = NULL; |
| 948 | mutex_init(&msock.mutex); |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 949 | msock.sbuf = connection->meta.sbuf; |
| 950 | msock.rbuf = connection->meta.rbuf; |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 951 | msock.socket = NULL; |
| 952 | |
Andreas Gruenbacher | 0916e0e | 2011-03-21 14:10:15 +0100 | [diff] [blame] | 953 | /* Assume that the peer only understands protocol 80 until we know better. */ |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 954 | connection->agreed_pro_version = 80; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 955 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 956 | if (prepare_listen_socket(connection, &ad)) |
Philipp Reisner | 7a426fd | 2012-07-12 14:22:37 +0200 | [diff] [blame] | 957 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 958 | |
| 959 | do { |
Andreas Gruenbacher | 2bf8962 | 2011-03-28 16:33:12 +0200 | [diff] [blame] | 960 | struct socket *s; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 961 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 962 | s = drbd_try_connect(connection); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 963 | if (s) { |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 964 | if (!sock.socket) { |
| 965 | sock.socket = s; |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 966 | send_first_packet(connection, &sock, P_INITIAL_DATA); |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 967 | } else if (!msock.socket) { |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 968 | clear_bit(RESOLVE_CONFLICTS, &connection->flags); |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 969 | msock.socket = s; |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 970 | send_first_packet(connection, &msock, P_INITIAL_META); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 971 | } else { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 972 | drbd_err(connection, "Logic error in conn_connect()\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 973 | goto out_release_sockets; |
| 974 | } |
| 975 | } |
| 976 | |
Philipp Reisner | 5d0b17f | 2014-03-18 14:24:35 +0100 | [diff] [blame] | 977 | if (connection_established(connection, &sock.socket, &msock.socket)) |
| 978 | break; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 979 | |
| 980 | retry: |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 981 | s = drbd_wait_for_connect(connection, &ad); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 982 | if (s) { |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 983 | int fp = receive_first_packet(connection, s); |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 984 | drbd_socket_okay(&sock.socket); |
| 985 | drbd_socket_okay(&msock.socket); |
Philipp Reisner | 92f1495 | 2012-08-01 11:41:01 +0200 | [diff] [blame] | 986 | switch (fp) { |
Andreas Gruenbacher | e5d6f33 | 2011-03-28 16:44:40 +0200 | [diff] [blame] | 987 | case P_INITIAL_DATA: |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 988 | if (sock.socket) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 989 | drbd_warn(connection, "initial packet S crossed\n"); |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 990 | sock_release(sock.socket); |
Philipp Reisner | 80c6eed | 2012-08-01 14:53:39 +0200 | [diff] [blame] | 991 | sock.socket = s; |
| 992 | goto randomize; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 993 | } |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 994 | sock.socket = s; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 995 | break; |
Andreas Gruenbacher | e5d6f33 | 2011-03-28 16:44:40 +0200 | [diff] [blame] | 996 | case P_INITIAL_META: |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 997 | set_bit(RESOLVE_CONFLICTS, &connection->flags); |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 998 | if (msock.socket) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 999 | drbd_warn(connection, "initial packet M crossed\n"); |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 1000 | sock_release(msock.socket); |
Philipp Reisner | 80c6eed | 2012-08-01 14:53:39 +0200 | [diff] [blame] | 1001 | msock.socket = s; |
| 1002 | goto randomize; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1003 | } |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 1004 | msock.socket = s; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1005 | break; |
| 1006 | default: |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 1007 | drbd_warn(connection, "Error receiving initial packet\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1008 | sock_release(s); |
Philipp Reisner | 80c6eed | 2012-08-01 14:53:39 +0200 | [diff] [blame] | 1009 | randomize: |
Akinobu Mita | 38b682b2 | 2013-04-29 16:21:31 -0700 | [diff] [blame] | 1010 | if (prandom_u32() & 1) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1011 | goto retry; |
| 1012 | } |
| 1013 | } |
| 1014 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1015 | if (connection->cstate <= C_DISCONNECTING) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1016 | goto out_release_sockets; |
| 1017 | if (signal_pending(current)) { |
| 1018 | flush_signals(current); |
| 1019 | smp_rmb(); |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1020 | if (get_t_state(&connection->receiver) == EXITING) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1021 | goto out_release_sockets; |
| 1022 | } |
| 1023 | |
Philipp Reisner | 5d0b17f | 2014-03-18 14:24:35 +0100 | [diff] [blame] | 1024 | ok = connection_established(connection, &sock.socket, &msock.socket); |
Philipp Reisner | b666dbf | 2012-07-26 14:12:59 +0200 | [diff] [blame] | 1025 | } while (!ok); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1026 | |
Philipp Reisner | 7a426fd | 2012-07-12 14:22:37 +0200 | [diff] [blame] | 1027 | if (ad.s_listen) |
| 1028 | sock_release(ad.s_listen); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1029 | |
Philipp Reisner | 9868365 | 2012-11-09 14:18:43 +0100 | [diff] [blame] | 1030 | sock.socket->sk->sk_reuse = SK_CAN_REUSE; /* SO_REUSEADDR */ |
| 1031 | msock.socket->sk->sk_reuse = SK_CAN_REUSE; /* SO_REUSEADDR */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1032 | |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 1033 | sock.socket->sk->sk_allocation = GFP_NOIO; |
| 1034 | msock.socket->sk->sk_allocation = GFP_NOIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1035 | |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 1036 | sock.socket->sk->sk_priority = TC_PRIO_INTERACTIVE_BULK; |
| 1037 | msock.socket->sk->sk_priority = TC_PRIO_INTERACTIVE; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1038 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1039 | /* NOT YET ... |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1040 | * sock.socket->sk->sk_sndtimeo = connection->net_conf->timeout*HZ/10; |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 1041 | * sock.socket->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT; |
Andreas Gruenbacher | 6038178 | 2011-03-28 17:05:50 +0200 | [diff] [blame] | 1042 | * first set it to the P_CONNECTION_FEATURES timeout, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1043 | * which we set to 4x the configured ping_timeout. */ |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 1044 | rcu_read_lock(); |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1045 | nc = rcu_dereference(connection->net_conf); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1046 | |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 1047 | sock.socket->sk->sk_sndtimeo = |
| 1048 | sock.socket->sk->sk_rcvtimeo = nc->ping_timeo*4*HZ/10; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 1049 | |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 1050 | msock.socket->sk->sk_rcvtimeo = nc->ping_int*HZ; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 1051 | timeout = nc->timeout * HZ / 10; |
Philipp Reisner | 08b165b | 2011-09-05 16:22:33 +0200 | [diff] [blame] | 1052 | discard_my_data = nc->discard_my_data; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 1053 | rcu_read_unlock(); |
| 1054 | |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 1055 | msock.socket->sk->sk_sndtimeo = timeout; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1056 | |
| 1057 | /* we don't want delays. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1058 | * we use TCP_CORK where appropriate, though */ |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 1059 | drbd_tcp_nodelay(sock.socket); |
| 1060 | drbd_tcp_nodelay(msock.socket); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1061 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1062 | connection->data.socket = sock.socket; |
| 1063 | connection->meta.socket = msock.socket; |
| 1064 | connection->last_received = jiffies; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1065 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1066 | h = drbd_do_features(connection); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1067 | if (h <= 0) |
| 1068 | return h; |
| 1069 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1070 | if (connection->cram_hmac_tfm) { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1071 | /* drbd_request_state(device, NS(conn, WFAuth)); */ |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1072 | switch (drbd_do_auth(connection)) { |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 1073 | case -1: |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 1074 | drbd_err(connection, "Authentication of peer failed\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1075 | return -1; |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 1076 | case 0: |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 1077 | drbd_err(connection, "Authentication of peer failed, trying again.\n"); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 1078 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1079 | } |
| 1080 | } |
| 1081 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1082 | connection->data.socket->sk->sk_sndtimeo = timeout; |
| 1083 | connection->data.socket->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1084 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1085 | if (drbd_send_protocol(connection) == -EOPNOTSUPP) |
Philipp Reisner | 7e2455c | 2010-04-22 14:50:23 +0200 | [diff] [blame] | 1086 | return -1; |
Philipp Reisner | 1e86ac4 | 2011-08-04 10:33:08 +0200 | [diff] [blame] | 1087 | |
Philipp Reisner | 3100774 | 2014-04-28 18:43:12 +0200 | [diff] [blame] | 1088 | /* Prevent a race between resync-handshake and |
| 1089 | * being promoted to Primary. |
| 1090 | * |
| 1091 | * Grab and release the state mutex, so we know that any current |
| 1092 | * drbd_set_role() is finished, and any incoming drbd_set_role |
| 1093 | * will see the STATE_SENT flag, and wait for it to be cleared. |
| 1094 | */ |
| 1095 | idr_for_each_entry(&connection->peer_devices, peer_device, vnr) |
| 1096 | mutex_lock(peer_device->device->state_mutex); |
| 1097 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1098 | set_bit(STATE_SENT, &connection->flags); |
Philipp Reisner | 197296f | 2012-03-26 16:47:11 +0200 | [diff] [blame] | 1099 | |
Philipp Reisner | 3100774 | 2014-04-28 18:43:12 +0200 | [diff] [blame] | 1100 | idr_for_each_entry(&connection->peer_devices, peer_device, vnr) |
| 1101 | mutex_unlock(peer_device->device->state_mutex); |
| 1102 | |
Philipp Reisner | c141ebd | 2011-05-05 16:13:10 +0200 | [diff] [blame] | 1103 | rcu_read_lock(); |
Andreas Gruenbacher | c06ece6 | 2011-06-21 17:23:59 +0200 | [diff] [blame] | 1104 | idr_for_each_entry(&connection->peer_devices, peer_device, vnr) { |
| 1105 | struct drbd_device *device = peer_device->device; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1106 | kref_get(&device->kref); |
Andreas Gruenbacher | 26ea8f9 | 2013-06-25 16:50:03 +0200 | [diff] [blame] | 1107 | rcu_read_unlock(); |
| 1108 | |
Philipp Reisner | 08b165b | 2011-09-05 16:22:33 +0200 | [diff] [blame] | 1109 | if (discard_my_data) |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1110 | set_bit(DISCARD_MY_DATA, &device->flags); |
Philipp Reisner | 08b165b | 2011-09-05 16:22:33 +0200 | [diff] [blame] | 1111 | else |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1112 | clear_bit(DISCARD_MY_DATA, &device->flags); |
Philipp Reisner | 08b165b | 2011-09-05 16:22:33 +0200 | [diff] [blame] | 1113 | |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 1114 | drbd_connected(peer_device); |
Andreas Gruenbacher | 05a10ec | 2011-06-07 22:54:17 +0200 | [diff] [blame] | 1115 | kref_put(&device->kref, drbd_destroy_device); |
Philipp Reisner | c141ebd | 2011-05-05 16:13:10 +0200 | [diff] [blame] | 1116 | rcu_read_lock(); |
| 1117 | } |
| 1118 | rcu_read_unlock(); |
| 1119 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1120 | rv = conn_request_state(connection, NS(conn, C_WF_REPORT_PARAMS), CS_VERBOSE); |
| 1121 | if (rv < SS_SUCCESS || connection->cstate != C_WF_REPORT_PARAMS) { |
| 1122 | clear_bit(STATE_SENT, &connection->flags); |
Philipp Reisner | 1e86ac4 | 2011-08-04 10:33:08 +0200 | [diff] [blame] | 1123 | return 0; |
Philipp Reisner | a1096a6 | 2012-04-06 12:07:34 +0200 | [diff] [blame] | 1124 | } |
Philipp Reisner | 1e86ac4 | 2011-08-04 10:33:08 +0200 | [diff] [blame] | 1125 | |
Philipp Reisner | 1c03e52 | 2015-03-16 15:01:00 +0100 | [diff] [blame] | 1126 | drbd_thread_start(&connection->ack_receiver); |
Lars Ellenberg | 39e91a6 | 2015-03-24 10:40:26 +0100 | [diff] [blame] | 1127 | /* opencoded create_singlethread_workqueue(), |
| 1128 | * to be able to use format string arguments */ |
| 1129 | connection->ack_sender = |
| 1130 | alloc_ordered_workqueue("drbd_as_%s", WQ_MEM_RECLAIM, connection->resource->name); |
Philipp Reisner | 668700b | 2015-03-16 16:08:29 +0100 | [diff] [blame] | 1131 | if (!connection->ack_sender) { |
| 1132 | drbd_err(connection, "Failed to create workqueue ack_sender\n"); |
| 1133 | return 0; |
| 1134 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1135 | |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 1136 | mutex_lock(&connection->resource->conf_update); |
Philipp Reisner | 08b165b | 2011-09-05 16:22:33 +0200 | [diff] [blame] | 1137 | /* The discard_my_data flag is a single-shot modifier to the next |
| 1138 | * connection attempt, the handshake of which is now well underway. |
| 1139 | * No need for rcu style copying of the whole struct |
| 1140 | * just to clear a single value. */ |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1141 | connection->net_conf->discard_my_data = 0; |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 1142 | mutex_unlock(&connection->resource->conf_update); |
Philipp Reisner | 08b165b | 2011-09-05 16:22:33 +0200 | [diff] [blame] | 1143 | |
Philipp Reisner | d3fcb49 | 2011-04-13 14:46:05 -0700 | [diff] [blame] | 1144 | return h; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1145 | |
| 1146 | out_release_sockets: |
Philipp Reisner | 7a426fd | 2012-07-12 14:22:37 +0200 | [diff] [blame] | 1147 | if (ad.s_listen) |
| 1148 | sock_release(ad.s_listen); |
Philipp Reisner | 7da3586 | 2011-12-19 22:42:56 +0100 | [diff] [blame] | 1149 | if (sock.socket) |
| 1150 | sock_release(sock.socket); |
| 1151 | if (msock.socket) |
| 1152 | sock_release(msock.socket); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1153 | return -1; |
| 1154 | } |
| 1155 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1156 | static int decode_header(struct drbd_connection *connection, void *header, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1157 | { |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1158 | unsigned int header_size = drbd_header_size(connection); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1159 | |
Andreas Gruenbacher | 0c8e36d | 2011-03-30 16:00:17 +0200 | [diff] [blame] | 1160 | if (header_size == sizeof(struct p_header100) && |
| 1161 | *(__be32 *)header == cpu_to_be32(DRBD_MAGIC_100)) { |
| 1162 | struct p_header100 *h = header; |
| 1163 | if (h->pad != 0) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 1164 | drbd_err(connection, "Header padding is not zero\n"); |
Andreas Gruenbacher | 0c8e36d | 2011-03-30 16:00:17 +0200 | [diff] [blame] | 1165 | return -EINVAL; |
| 1166 | } |
| 1167 | pi->vnr = be16_to_cpu(h->volume); |
| 1168 | pi->cmd = be16_to_cpu(h->command); |
| 1169 | pi->size = be32_to_cpu(h->length); |
| 1170 | } else if (header_size == sizeof(struct p_header95) && |
| 1171 | *(__be16 *)header == cpu_to_be16(DRBD_MAGIC_BIG)) { |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 1172 | struct p_header95 *h = header; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 1173 | pi->cmd = be16_to_cpu(h->command); |
Andreas Gruenbacher | b55d84b | 2011-03-22 13:17:47 +0100 | [diff] [blame] | 1174 | pi->size = be32_to_cpu(h->length); |
| 1175 | pi->vnr = 0; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 1176 | } else if (header_size == sizeof(struct p_header80) && |
| 1177 | *(__be32 *)header == cpu_to_be32(DRBD_MAGIC)) { |
| 1178 | struct p_header80 *h = header; |
| 1179 | pi->cmd = be16_to_cpu(h->command); |
| 1180 | pi->size = be16_to_cpu(h->length); |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 1181 | pi->vnr = 0; |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 1182 | } else { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 1183 | drbd_err(connection, "Wrong magic value 0x%08x in protocol version %d\n", |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 1184 | be32_to_cpu(*(__be32 *)header), |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1185 | connection->agreed_pro_version); |
Andreas Gruenbacher | 8172f3e | 2011-03-16 17:22:39 +0100 | [diff] [blame] | 1186 | return -EINVAL; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1187 | } |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 1188 | pi->data = header + header_size; |
Andreas Gruenbacher | 8172f3e | 2011-03-16 17:22:39 +0100 | [diff] [blame] | 1189 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1190 | } |
| 1191 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1192 | static int drbd_recv_header(struct drbd_connection *connection, struct packet_info *pi) |
Philipp Reisner | 257d0af | 2011-01-26 12:15:29 +0100 | [diff] [blame] | 1193 | { |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1194 | void *buffer = connection->data.rbuf; |
Andreas Gruenbacher | 69bc7bc | 2011-03-16 17:31:52 +0100 | [diff] [blame] | 1195 | int err; |
Philipp Reisner | 257d0af | 2011-01-26 12:15:29 +0100 | [diff] [blame] | 1196 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1197 | err = drbd_recv_all_warn(connection, buffer, drbd_header_size(connection)); |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 1198 | if (err) |
Andreas Gruenbacher | 69bc7bc | 2011-03-16 17:31:52 +0100 | [diff] [blame] | 1199 | return err; |
Philipp Reisner | 257d0af | 2011-01-26 12:15:29 +0100 | [diff] [blame] | 1200 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1201 | err = decode_header(connection, buffer, pi); |
| 1202 | connection->last_received = jiffies; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1203 | |
Andreas Gruenbacher | 69bc7bc | 2011-03-16 17:31:52 +0100 | [diff] [blame] | 1204 | return err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1205 | } |
| 1206 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1207 | static void drbd_flush(struct drbd_connection *connection) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1208 | { |
| 1209 | int rv; |
Andreas Gruenbacher | c06ece6 | 2011-06-21 17:23:59 +0200 | [diff] [blame] | 1210 | struct drbd_peer_device *peer_device; |
Philipp Reisner | 4b0007c | 2011-11-09 20:12:34 +0100 | [diff] [blame] | 1211 | int vnr; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1212 | |
Andreas Gruenbacher | f6ba863 | 2014-08-13 18:33:55 +0200 | [diff] [blame] | 1213 | if (connection->resource->write_ordering >= WO_BDEV_FLUSH) { |
Lars Ellenberg | 615e087 | 2011-11-17 14:32:12 +0100 | [diff] [blame] | 1214 | rcu_read_lock(); |
Andreas Gruenbacher | c06ece6 | 2011-06-21 17:23:59 +0200 | [diff] [blame] | 1215 | idr_for_each_entry(&connection->peer_devices, peer_device, vnr) { |
| 1216 | struct drbd_device *device = peer_device->device; |
| 1217 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1218 | if (!get_ldev(device)) |
Lars Ellenberg | 615e087 | 2011-11-17 14:32:12 +0100 | [diff] [blame] | 1219 | continue; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1220 | kref_get(&device->kref); |
Lars Ellenberg | 615e087 | 2011-11-17 14:32:12 +0100 | [diff] [blame] | 1221 | rcu_read_unlock(); |
Philipp Reisner | 4b0007c | 2011-11-09 20:12:34 +0100 | [diff] [blame] | 1222 | |
Lars Ellenberg | f418815 | 2014-05-05 23:05:47 +0200 | [diff] [blame] | 1223 | /* Right now, we have only this one synchronous code path |
| 1224 | * for flushes between request epochs. |
| 1225 | * We may want to make those asynchronous, |
| 1226 | * or at least parallelize the flushes to the volume devices. |
| 1227 | */ |
| 1228 | device->flush_jif = jiffies; |
| 1229 | set_bit(FLUSH_PENDING, &device->flags); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1230 | rv = blkdev_issue_flush(device->ldev->backing_bdev, |
Lars Ellenberg | 615e087 | 2011-11-17 14:32:12 +0100 | [diff] [blame] | 1231 | GFP_NOIO, NULL); |
Lars Ellenberg | f418815 | 2014-05-05 23:05:47 +0200 | [diff] [blame] | 1232 | clear_bit(FLUSH_PENDING, &device->flags); |
Lars Ellenberg | 615e087 | 2011-11-17 14:32:12 +0100 | [diff] [blame] | 1233 | if (rv) { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 1234 | drbd_info(device, "local disk flush failed with status %d\n", rv); |
Lars Ellenberg | 615e087 | 2011-11-17 14:32:12 +0100 | [diff] [blame] | 1235 | /* would rather check on EOPNOTSUPP, but that is not reliable. |
| 1236 | * don't try again for ANY return value != 0 |
| 1237 | * if (rv == -EOPNOTSUPP) */ |
Andreas Gruenbacher | f6ba863 | 2014-08-13 18:33:55 +0200 | [diff] [blame] | 1238 | drbd_bump_write_ordering(connection->resource, NULL, WO_DRAIN_IO); |
Philipp Reisner | 4b0007c | 2011-11-09 20:12:34 +0100 | [diff] [blame] | 1239 | } |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1240 | put_ldev(device); |
Andreas Gruenbacher | 05a10ec | 2011-06-07 22:54:17 +0200 | [diff] [blame] | 1241 | kref_put(&device->kref, drbd_destroy_device); |
Lars Ellenberg | 615e087 | 2011-11-17 14:32:12 +0100 | [diff] [blame] | 1242 | |
| 1243 | rcu_read_lock(); |
| 1244 | if (rv) |
| 1245 | break; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1246 | } |
Lars Ellenberg | 615e087 | 2011-11-17 14:32:12 +0100 | [diff] [blame] | 1247 | rcu_read_unlock(); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1248 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | /** |
| 1252 | * drbd_may_finish_epoch() - Applies an epoch_event to the epoch's state, eventually finishes it. |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1253 | * @device: DRBD device. |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1254 | * @epoch: Epoch object. |
| 1255 | * @ev: Epoch event. |
| 1256 | */ |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1257 | static enum finish_epoch drbd_may_finish_epoch(struct drbd_connection *connection, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1258 | struct drbd_epoch *epoch, |
| 1259 | enum epoch_event ev) |
| 1260 | { |
Philipp Reisner | 2451fc3 | 2010-08-24 13:43:11 +0200 | [diff] [blame] | 1261 | int epoch_size; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1262 | struct drbd_epoch *next_epoch; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1263 | enum finish_epoch rv = FE_STILL_LIVE; |
| 1264 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1265 | spin_lock(&connection->epoch_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1266 | do { |
| 1267 | next_epoch = NULL; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1268 | |
| 1269 | epoch_size = atomic_read(&epoch->epoch_size); |
| 1270 | |
| 1271 | switch (ev & ~EV_CLEANUP) { |
| 1272 | case EV_PUT: |
| 1273 | atomic_dec(&epoch->active); |
| 1274 | break; |
| 1275 | case EV_GOT_BARRIER_NR: |
| 1276 | set_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1277 | break; |
| 1278 | case EV_BECAME_LAST: |
| 1279 | /* nothing to do*/ |
| 1280 | break; |
| 1281 | } |
| 1282 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1283 | if (epoch_size != 0 && |
| 1284 | atomic_read(&epoch->active) == 0 && |
Philipp Reisner | 80f9fd5 | 2011-07-18 15:45:15 +0200 | [diff] [blame] | 1285 | (test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags) || ev & EV_CLEANUP)) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1286 | if (!(ev & EV_CLEANUP)) { |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1287 | spin_unlock(&connection->epoch_lock); |
| 1288 | drbd_send_b_ack(epoch->connection, epoch->barrier_nr, epoch_size); |
| 1289 | spin_lock(&connection->epoch_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1290 | } |
Lars Ellenberg | 9ed57dc | 2012-03-26 20:55:17 +0200 | [diff] [blame] | 1291 | #if 0 |
| 1292 | /* FIXME: dec unacked on connection, once we have |
| 1293 | * something to count pending connection packets in. */ |
Philipp Reisner | 80f9fd5 | 2011-07-18 15:45:15 +0200 | [diff] [blame] | 1294 | if (test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags)) |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1295 | dec_unacked(epoch->connection); |
Lars Ellenberg | 9ed57dc | 2012-03-26 20:55:17 +0200 | [diff] [blame] | 1296 | #endif |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1297 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1298 | if (connection->current_epoch != epoch) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1299 | next_epoch = list_entry(epoch->list.next, struct drbd_epoch, list); |
| 1300 | list_del(&epoch->list); |
| 1301 | ev = EV_BECAME_LAST | (ev & EV_CLEANUP); |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1302 | connection->epochs--; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1303 | kfree(epoch); |
| 1304 | |
| 1305 | if (rv == FE_STILL_LIVE) |
| 1306 | rv = FE_DESTROYED; |
| 1307 | } else { |
| 1308 | epoch->flags = 0; |
| 1309 | atomic_set(&epoch->epoch_size, 0); |
Uwe Kleine-König | 698f931 | 2010-07-02 20:41:51 +0200 | [diff] [blame] | 1310 | /* atomic_set(&epoch->active, 0); is already zero */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1311 | if (rv == FE_STILL_LIVE) |
| 1312 | rv = FE_RECYCLED; |
| 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | if (!next_epoch) |
| 1317 | break; |
| 1318 | |
| 1319 | epoch = next_epoch; |
| 1320 | } while (1); |
| 1321 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1322 | spin_unlock(&connection->epoch_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1323 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1324 | return rv; |
| 1325 | } |
| 1326 | |
Philipp Reisner | 8fe39aa | 2013-11-22 13:22:13 +0100 | [diff] [blame] | 1327 | static enum write_ordering_e |
| 1328 | max_allowed_wo(struct drbd_backing_dev *bdev, enum write_ordering_e wo) |
| 1329 | { |
| 1330 | struct disk_conf *dc; |
| 1331 | |
| 1332 | dc = rcu_dereference(bdev->disk_conf); |
| 1333 | |
Andreas Gruenbacher | f6ba863 | 2014-08-13 18:33:55 +0200 | [diff] [blame] | 1334 | if (wo == WO_BDEV_FLUSH && !dc->disk_flushes) |
| 1335 | wo = WO_DRAIN_IO; |
| 1336 | if (wo == WO_DRAIN_IO && !dc->disk_drain) |
| 1337 | wo = WO_NONE; |
Philipp Reisner | 8fe39aa | 2013-11-22 13:22:13 +0100 | [diff] [blame] | 1338 | |
| 1339 | return wo; |
| 1340 | } |
| 1341 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1342 | /** |
| 1343 | * drbd_bump_write_ordering() - Fall back to an other write ordering method |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1344 | * @connection: DRBD connection. |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1345 | * @wo: Write ordering method to try. |
| 1346 | */ |
Philipp Reisner | 8fe39aa | 2013-11-22 13:22:13 +0100 | [diff] [blame] | 1347 | void drbd_bump_write_ordering(struct drbd_resource *resource, struct drbd_backing_dev *bdev, |
| 1348 | enum write_ordering_e wo) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1349 | { |
Philipp Reisner | e952658 | 2013-11-22 15:53:41 +0100 | [diff] [blame] | 1350 | struct drbd_device *device; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1351 | enum write_ordering_e pwo; |
Philipp Reisner | 4b0007c | 2011-11-09 20:12:34 +0100 | [diff] [blame] | 1352 | int vnr; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1353 | static char *write_ordering_str[] = { |
Andreas Gruenbacher | f6ba863 | 2014-08-13 18:33:55 +0200 | [diff] [blame] | 1354 | [WO_NONE] = "none", |
| 1355 | [WO_DRAIN_IO] = "drain", |
| 1356 | [WO_BDEV_FLUSH] = "flush", |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1357 | }; |
| 1358 | |
Philipp Reisner | e952658 | 2013-11-22 15:53:41 +0100 | [diff] [blame] | 1359 | pwo = resource->write_ordering; |
Andreas Gruenbacher | f6ba863 | 2014-08-13 18:33:55 +0200 | [diff] [blame] | 1360 | if (wo != WO_BDEV_FLUSH) |
Lars Ellenberg | 70df709 | 2013-12-20 11:17:02 +0100 | [diff] [blame] | 1361 | wo = min(pwo, wo); |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 1362 | rcu_read_lock(); |
Philipp Reisner | e952658 | 2013-11-22 15:53:41 +0100 | [diff] [blame] | 1363 | idr_for_each_entry(&resource->devices, device, vnr) { |
Philipp Reisner | 8fe39aa | 2013-11-22 13:22:13 +0100 | [diff] [blame] | 1364 | if (get_ldev(device)) { |
| 1365 | wo = max_allowed_wo(device->ldev, wo); |
| 1366 | if (device->ldev == bdev) |
| 1367 | bdev = NULL; |
| 1368 | put_ldev(device); |
| 1369 | } |
Philipp Reisner | 4b0007c | 2011-11-09 20:12:34 +0100 | [diff] [blame] | 1370 | } |
Philipp Reisner | 8fe39aa | 2013-11-22 13:22:13 +0100 | [diff] [blame] | 1371 | |
| 1372 | if (bdev) |
| 1373 | wo = max_allowed_wo(bdev, wo); |
| 1374 | |
Lars Ellenberg | 70df709 | 2013-12-20 11:17:02 +0100 | [diff] [blame] | 1375 | rcu_read_unlock(); |
| 1376 | |
Philipp Reisner | e952658 | 2013-11-22 15:53:41 +0100 | [diff] [blame] | 1377 | resource->write_ordering = wo; |
Andreas Gruenbacher | f6ba863 | 2014-08-13 18:33:55 +0200 | [diff] [blame] | 1378 | if (pwo != resource->write_ordering || wo == WO_BDEV_FLUSH) |
Philipp Reisner | e952658 | 2013-11-22 15:53:41 +0100 | [diff] [blame] | 1379 | drbd_info(resource, "Method to ensure write ordering: %s\n", write_ordering_str[resource->write_ordering]); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1380 | } |
| 1381 | |
| 1382 | /** |
Andreas Gruenbacher | fbe29de | 2011-02-17 16:38:35 +0100 | [diff] [blame] | 1383 | * drbd_submit_peer_request() |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1384 | * @device: DRBD device. |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1385 | * @peer_req: peer request |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1386 | * @rw: flag field, see bio->bi_rw |
Lars Ellenberg | 10f6d992 | 2011-01-24 14:47:09 +0100 | [diff] [blame] | 1387 | * |
| 1388 | * May spread the pages to multiple bios, |
| 1389 | * depending on bio_add_page restrictions. |
| 1390 | * |
| 1391 | * Returns 0 if all bios have been submitted, |
| 1392 | * -ENOMEM if we could not allocate enough bios, |
| 1393 | * -ENOSPC (any better suggestion?) if we have not been able to bio_add_page a |
| 1394 | * single page to an empty bio (which should never happen and likely indicates |
| 1395 | * that the lower level IO stack is in some way broken). This has been observed |
| 1396 | * on certain Xen deployments. |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1397 | */ |
| 1398 | /* TODO allocate from our own bio_set. */ |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1399 | int drbd_submit_peer_request(struct drbd_device *device, |
Andreas Gruenbacher | fbe29de | 2011-02-17 16:38:35 +0100 | [diff] [blame] | 1400 | struct drbd_peer_request *peer_req, |
Mike Christie | bb3cc85 | 2016-06-05 14:32:06 -0500 | [diff] [blame^] | 1401 | const unsigned op, const unsigned op_flags, |
| 1402 | const int fault_type) |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1403 | { |
| 1404 | struct bio *bios = NULL; |
| 1405 | struct bio *bio; |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1406 | struct page *page = peer_req->pages; |
| 1407 | sector_t sector = peer_req->i.sector; |
Andreas Gruenbacher | 11f8b2b | 2014-09-11 14:29:05 +0200 | [diff] [blame] | 1408 | unsigned data_size = peer_req->i.size; |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1409 | unsigned n_bios = 0; |
Andreas Gruenbacher | 11f8b2b | 2014-09-11 14:29:05 +0200 | [diff] [blame] | 1410 | unsigned nr_pages = (data_size + PAGE_SIZE -1) >> PAGE_SHIFT; |
Lars Ellenberg | 10f6d992 | 2011-01-24 14:47:09 +0100 | [diff] [blame] | 1411 | int err = -ENOMEM; |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1412 | |
Lars Ellenberg | a0fb3c4 | 2014-04-28 18:43:23 +0200 | [diff] [blame] | 1413 | if (peer_req->flags & EE_IS_TRIM_USE_ZEROOUT) { |
| 1414 | /* wait for all pending IO completions, before we start |
| 1415 | * zeroing things out. */ |
Andreas Gruenbacher | 5dd2ca1 | 2014-08-11 16:59:23 +0200 | [diff] [blame] | 1416 | conn_wait_active_ee_empty(peer_req->peer_device->connection); |
Lars Ellenberg | 45d2933 | 2014-04-23 12:25:23 +0200 | [diff] [blame] | 1417 | /* add it to the active list now, |
| 1418 | * so we can find it to present it in debugfs */ |
Lars Ellenberg | 21ae5d7 | 2014-05-05 23:42:24 +0200 | [diff] [blame] | 1419 | peer_req->submit_jif = jiffies; |
| 1420 | peer_req->flags |= EE_SUBMITTED; |
Lars Ellenberg | 45d2933 | 2014-04-23 12:25:23 +0200 | [diff] [blame] | 1421 | spin_lock_irq(&device->resource->req_lock); |
| 1422 | list_add_tail(&peer_req->w.list, &device->active_ee); |
| 1423 | spin_unlock_irq(&device->resource->req_lock); |
Lars Ellenberg | a0fb3c4 | 2014-04-28 18:43:23 +0200 | [diff] [blame] | 1424 | if (blkdev_issue_zeroout(device->ldev->backing_bdev, |
Martin K. Petersen | d93ba7a | 2015-01-20 20:06:30 -0500 | [diff] [blame] | 1425 | sector, data_size >> 9, GFP_NOIO, false)) |
Lars Ellenberg | a0fb3c4 | 2014-04-28 18:43:23 +0200 | [diff] [blame] | 1426 | peer_req->flags |= EE_WAS_ERROR; |
| 1427 | drbd_endio_write_sec_final(peer_req); |
| 1428 | return 0; |
| 1429 | } |
| 1430 | |
Lars Ellenberg | 54ed4ed | 2014-06-25 17:52:38 +0200 | [diff] [blame] | 1431 | /* Discards don't have any payload. |
| 1432 | * But the scsi layer still expects a bio_vec it can use internally, |
| 1433 | * see sd_setup_discard_cmnd() and blk_add_request_payload(). */ |
Lars Ellenberg | a0fb3c4 | 2014-04-28 18:43:23 +0200 | [diff] [blame] | 1434 | if (peer_req->flags & EE_IS_TRIM) |
Lars Ellenberg | 54ed4ed | 2014-06-25 17:52:38 +0200 | [diff] [blame] | 1435 | nr_pages = 1; |
Lars Ellenberg | a0fb3c4 | 2014-04-28 18:43:23 +0200 | [diff] [blame] | 1436 | |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1437 | /* In most cases, we will only need one bio. But in case the lower |
| 1438 | * level restrictions happen to be different at this offset on this |
| 1439 | * side than those of the sending peer, we may need to submit the |
Lars Ellenberg | 9476f39 | 2011-02-23 17:02:01 +0100 | [diff] [blame] | 1440 | * request in more than one bio. |
| 1441 | * |
| 1442 | * Plain bio_alloc is good enough here, this is no DRBD internally |
| 1443 | * generated bio, but a bio allocated on behalf of the peer. |
| 1444 | */ |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1445 | next_bio: |
| 1446 | bio = bio_alloc(GFP_NOIO, nr_pages); |
| 1447 | if (!bio) { |
Lars Ellenberg | a0fb3c4 | 2014-04-28 18:43:23 +0200 | [diff] [blame] | 1448 | drbd_err(device, "submit_ee: Allocation of a bio failed (nr_pages=%u)\n", nr_pages); |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1449 | goto fail; |
| 1450 | } |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1451 | /* > peer_req->i.sector, unless this is the first bio */ |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1452 | bio->bi_iter.bi_sector = sector; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1453 | bio->bi_bdev = device->ldev->backing_bdev; |
Mike Christie | bb3cc85 | 2016-06-05 14:32:06 -0500 | [diff] [blame^] | 1454 | bio_set_op_attrs(bio, op, op_flags); |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1455 | bio->bi_private = peer_req; |
Andreas Gruenbacher | fcefa62 | 2011-02-17 16:46:59 +0100 | [diff] [blame] | 1456 | bio->bi_end_io = drbd_peer_request_endio; |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1457 | |
| 1458 | bio->bi_next = bios; |
| 1459 | bios = bio; |
| 1460 | ++n_bios; |
| 1461 | |
Mike Christie | bb3cc85 | 2016-06-05 14:32:06 -0500 | [diff] [blame^] | 1462 | if (op == REQ_OP_DISCARD) { |
Andreas Gruenbacher | 11f8b2b | 2014-09-11 14:29:05 +0200 | [diff] [blame] | 1463 | bio->bi_iter.bi_size = data_size; |
Lars Ellenberg | a0fb3c4 | 2014-04-28 18:43:23 +0200 | [diff] [blame] | 1464 | goto submit; |
| 1465 | } |
| 1466 | |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1467 | page_chain_for_each(page) { |
Andreas Gruenbacher | 11f8b2b | 2014-09-11 14:29:05 +0200 | [diff] [blame] | 1468 | unsigned len = min_t(unsigned, data_size, PAGE_SIZE); |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1469 | if (!bio_add_page(bio, page, len, 0)) { |
Lars Ellenberg | 10f6d992 | 2011-01-24 14:47:09 +0100 | [diff] [blame] | 1470 | /* A single page must always be possible! |
| 1471 | * But in case it fails anyways, |
| 1472 | * we deal with it, and complain (below). */ |
| 1473 | if (bio->bi_vcnt == 0) { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 1474 | drbd_err(device, |
Lars Ellenberg | 10f6d992 | 2011-01-24 14:47:09 +0100 | [diff] [blame] | 1475 | "bio_add_page failed for len=%u, " |
| 1476 | "bi_vcnt=0 (bi_sector=%llu)\n", |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1477 | len, (uint64_t)bio->bi_iter.bi_sector); |
Lars Ellenberg | 10f6d992 | 2011-01-24 14:47:09 +0100 | [diff] [blame] | 1478 | err = -ENOSPC; |
| 1479 | goto fail; |
| 1480 | } |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1481 | goto next_bio; |
| 1482 | } |
Andreas Gruenbacher | 11f8b2b | 2014-09-11 14:29:05 +0200 | [diff] [blame] | 1483 | data_size -= len; |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1484 | sector += len >> 9; |
| 1485 | --nr_pages; |
| 1486 | } |
Andreas Gruenbacher | 11f8b2b | 2014-09-11 14:29:05 +0200 | [diff] [blame] | 1487 | D_ASSERT(device, data_size == 0); |
Lars Ellenberg | a0fb3c4 | 2014-04-28 18:43:23 +0200 | [diff] [blame] | 1488 | submit: |
| 1489 | D_ASSERT(device, page == NULL); |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1490 | |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1491 | atomic_set(&peer_req->pending_bios, n_bios); |
Lars Ellenberg | 21ae5d7 | 2014-05-05 23:42:24 +0200 | [diff] [blame] | 1492 | /* for debugfs: update timestamp, mark as submitted */ |
| 1493 | peer_req->submit_jif = jiffies; |
| 1494 | peer_req->flags |= EE_SUBMITTED; |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1495 | do { |
| 1496 | bio = bios; |
| 1497 | bios = bios->bi_next; |
| 1498 | bio->bi_next = NULL; |
| 1499 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1500 | drbd_generic_make_request(device, fault_type, bio); |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1501 | } while (bios); |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1502 | return 0; |
| 1503 | |
| 1504 | fail: |
| 1505 | while (bios) { |
| 1506 | bio = bios; |
| 1507 | bios = bios->bi_next; |
| 1508 | bio_put(bio); |
| 1509 | } |
Lars Ellenberg | 10f6d992 | 2011-01-24 14:47:09 +0100 | [diff] [blame] | 1510 | return err; |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1511 | } |
| 1512 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1513 | static void drbd_remove_epoch_entry_interval(struct drbd_device *device, |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1514 | struct drbd_peer_request *peer_req) |
Andreas Gruenbacher | 5384064 | 2011-01-28 10:31:04 +0100 | [diff] [blame] | 1515 | { |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1516 | struct drbd_interval *i = &peer_req->i; |
Andreas Gruenbacher | 5384064 | 2011-01-28 10:31:04 +0100 | [diff] [blame] | 1517 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1518 | drbd_remove_interval(&device->write_requests, i); |
Andreas Gruenbacher | 5384064 | 2011-01-28 10:31:04 +0100 | [diff] [blame] | 1519 | drbd_clear_interval(i); |
| 1520 | |
Andreas Gruenbacher | 6c852be | 2011-02-04 15:38:52 +0100 | [diff] [blame] | 1521 | /* Wake up any processes waiting for this peer request to complete. */ |
Andreas Gruenbacher | 5384064 | 2011-01-28 10:31:04 +0100 | [diff] [blame] | 1522 | if (i->waiting) |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1523 | wake_up(&device->misc_wait); |
Andreas Gruenbacher | 5384064 | 2011-01-28 10:31:04 +0100 | [diff] [blame] | 1524 | } |
| 1525 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1526 | static void conn_wait_active_ee_empty(struct drbd_connection *connection) |
Philipp Reisner | 77fede5 | 2011-11-10 21:19:11 +0100 | [diff] [blame] | 1527 | { |
Andreas Gruenbacher | c06ece6 | 2011-06-21 17:23:59 +0200 | [diff] [blame] | 1528 | struct drbd_peer_device *peer_device; |
Philipp Reisner | 77fede5 | 2011-11-10 21:19:11 +0100 | [diff] [blame] | 1529 | int vnr; |
| 1530 | |
| 1531 | rcu_read_lock(); |
Andreas Gruenbacher | c06ece6 | 2011-06-21 17:23:59 +0200 | [diff] [blame] | 1532 | idr_for_each_entry(&connection->peer_devices, peer_device, vnr) { |
| 1533 | struct drbd_device *device = peer_device->device; |
| 1534 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1535 | kref_get(&device->kref); |
Philipp Reisner | 77fede5 | 2011-11-10 21:19:11 +0100 | [diff] [blame] | 1536 | rcu_read_unlock(); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1537 | drbd_wait_ee_list_empty(device, &device->active_ee); |
Andreas Gruenbacher | 05a10ec | 2011-06-07 22:54:17 +0200 | [diff] [blame] | 1538 | kref_put(&device->kref, drbd_destroy_device); |
Philipp Reisner | 77fede5 | 2011-11-10 21:19:11 +0100 | [diff] [blame] | 1539 | rcu_read_lock(); |
| 1540 | } |
| 1541 | rcu_read_unlock(); |
| 1542 | } |
| 1543 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1544 | static int receive_Barrier(struct drbd_connection *connection, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1545 | { |
Philipp Reisner | 2451fc3 | 2010-08-24 13:43:11 +0200 | [diff] [blame] | 1546 | int rv; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 1547 | struct p_barrier *p = pi->data; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1548 | struct drbd_epoch *epoch; |
| 1549 | |
Lars Ellenberg | 9ed57dc | 2012-03-26 20:55:17 +0200 | [diff] [blame] | 1550 | /* FIXME these are unacked on connection, |
| 1551 | * not a specific (peer)device. |
| 1552 | */ |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1553 | connection->current_epoch->barrier_nr = p->barrier; |
| 1554 | connection->current_epoch->connection = connection; |
| 1555 | rv = drbd_may_finish_epoch(connection, connection->current_epoch, EV_GOT_BARRIER_NR); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1556 | |
| 1557 | /* P_BARRIER_ACK may imply that the corresponding extent is dropped from |
| 1558 | * the activity log, which means it would not be resynced in case the |
| 1559 | * R_PRIMARY crashes now. |
| 1560 | * Therefore we must send the barrier_ack after the barrier request was |
| 1561 | * completed. */ |
Philipp Reisner | e952658 | 2013-11-22 15:53:41 +0100 | [diff] [blame] | 1562 | switch (connection->resource->write_ordering) { |
Andreas Gruenbacher | f6ba863 | 2014-08-13 18:33:55 +0200 | [diff] [blame] | 1563 | case WO_NONE: |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1564 | if (rv == FE_RECYCLED) |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 1565 | return 0; |
Philipp Reisner | 2451fc3 | 2010-08-24 13:43:11 +0200 | [diff] [blame] | 1566 | |
| 1567 | /* receiver context, in the writeout path of the other node. |
| 1568 | * avoid potential distributed deadlock */ |
| 1569 | epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO); |
| 1570 | if (epoch) |
| 1571 | break; |
| 1572 | else |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 1573 | drbd_warn(connection, "Allocation of an epoch failed, slowing down\n"); |
Philipp Reisner | 2451fc3 | 2010-08-24 13:43:11 +0200 | [diff] [blame] | 1574 | /* Fall through */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1575 | |
Andreas Gruenbacher | f6ba863 | 2014-08-13 18:33:55 +0200 | [diff] [blame] | 1576 | case WO_BDEV_FLUSH: |
| 1577 | case WO_DRAIN_IO: |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1578 | conn_wait_active_ee_empty(connection); |
| 1579 | drbd_flush(connection); |
Philipp Reisner | 2451fc3 | 2010-08-24 13:43:11 +0200 | [diff] [blame] | 1580 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1581 | if (atomic_read(&connection->current_epoch->epoch_size)) { |
Philipp Reisner | 2451fc3 | 2010-08-24 13:43:11 +0200 | [diff] [blame] | 1582 | epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO); |
| 1583 | if (epoch) |
| 1584 | break; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1585 | } |
| 1586 | |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 1587 | return 0; |
Philipp Reisner | 2451fc3 | 2010-08-24 13:43:11 +0200 | [diff] [blame] | 1588 | default: |
Philipp Reisner | e952658 | 2013-11-22 15:53:41 +0100 | [diff] [blame] | 1589 | drbd_err(connection, "Strangeness in connection->write_ordering %d\n", |
| 1590 | connection->resource->write_ordering); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 1591 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1592 | } |
| 1593 | |
| 1594 | epoch->flags = 0; |
| 1595 | atomic_set(&epoch->epoch_size, 0); |
| 1596 | atomic_set(&epoch->active, 0); |
| 1597 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1598 | spin_lock(&connection->epoch_lock); |
| 1599 | if (atomic_read(&connection->current_epoch->epoch_size)) { |
| 1600 | list_add(&epoch->list, &connection->current_epoch->list); |
| 1601 | connection->current_epoch = epoch; |
| 1602 | connection->epochs++; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1603 | } else { |
| 1604 | /* The current_epoch got recycled while we allocated this one... */ |
| 1605 | kfree(epoch); |
| 1606 | } |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1607 | spin_unlock(&connection->epoch_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1608 | |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 1609 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1610 | } |
| 1611 | |
| 1612 | /* used from receive_RSDataReply (recv_resync_read) |
| 1613 | * and from receive_Data */ |
Andreas Gruenbacher | f6ffca9 | 2011-02-04 15:30:34 +0100 | [diff] [blame] | 1614 | static struct drbd_peer_request * |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 1615 | read_in_block(struct drbd_peer_device *peer_device, u64 id, sector_t sector, |
Lars Ellenberg | a0fb3c4 | 2014-04-28 18:43:23 +0200 | [diff] [blame] | 1616 | struct packet_info *pi) __must_hold(local) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1617 | { |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 1618 | struct drbd_device *device = peer_device->device; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1619 | const sector_t capacity = drbd_get_capacity(device->this_bdev); |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1620 | struct drbd_peer_request *peer_req; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1621 | struct page *page; |
Andreas Gruenbacher | 11f8b2b | 2014-09-11 14:29:05 +0200 | [diff] [blame] | 1622 | int digest_size, err; |
| 1623 | unsigned int data_size = pi->size, ds; |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 1624 | void *dig_in = peer_device->connection->int_dig_in; |
| 1625 | void *dig_vv = peer_device->connection->int_dig_vv; |
Philipp Reisner | 6b4388a | 2010-04-26 14:11:45 +0200 | [diff] [blame] | 1626 | unsigned long *data; |
Lars Ellenberg | a0fb3c4 | 2014-04-28 18:43:23 +0200 | [diff] [blame] | 1627 | struct p_trim *trim = (pi->cmd == P_TRIM) ? pi->data : NULL; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1628 | |
Andreas Gruenbacher | 11f8b2b | 2014-09-11 14:29:05 +0200 | [diff] [blame] | 1629 | digest_size = 0; |
Lars Ellenberg | a0fb3c4 | 2014-04-28 18:43:23 +0200 | [diff] [blame] | 1630 | if (!trim && peer_device->connection->peer_integrity_tfm) { |
Herbert Xu | 9534d67 | 2016-01-24 21:19:21 +0800 | [diff] [blame] | 1631 | digest_size = crypto_ahash_digestsize(peer_device->connection->peer_integrity_tfm); |
Andreas Gruenbacher | 9f5bdc3 | 2011-03-28 14:23:08 +0200 | [diff] [blame] | 1632 | /* |
| 1633 | * FIXME: Receive the incoming digest into the receive buffer |
| 1634 | * here, together with its struct p_data? |
| 1635 | */ |
Andreas Gruenbacher | 11f8b2b | 2014-09-11 14:29:05 +0200 | [diff] [blame] | 1636 | err = drbd_recv_all_warn(peer_device->connection, dig_in, digest_size); |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 1637 | if (err) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1638 | return NULL; |
Andreas Gruenbacher | 11f8b2b | 2014-09-11 14:29:05 +0200 | [diff] [blame] | 1639 | data_size -= digest_size; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1640 | } |
| 1641 | |
Lars Ellenberg | a0fb3c4 | 2014-04-28 18:43:23 +0200 | [diff] [blame] | 1642 | if (trim) { |
| 1643 | D_ASSERT(peer_device, data_size == 0); |
| 1644 | data_size = be32_to_cpu(trim->size); |
| 1645 | } |
| 1646 | |
Andreas Gruenbacher | 841ce24 | 2010-12-15 19:31:20 +0100 | [diff] [blame] | 1647 | if (!expect(IS_ALIGNED(data_size, 512))) |
| 1648 | return NULL; |
Lars Ellenberg | a0fb3c4 | 2014-04-28 18:43:23 +0200 | [diff] [blame] | 1649 | /* prepare for larger trim requests. */ |
| 1650 | if (!trim && !expect(data_size <= DRBD_MAX_BIO_SIZE)) |
Andreas Gruenbacher | 841ce24 | 2010-12-15 19:31:20 +0100 | [diff] [blame] | 1651 | return NULL; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1652 | |
Lars Ellenberg | 6666032 | 2010-04-06 12:15:04 +0200 | [diff] [blame] | 1653 | /* even though we trust out peer, |
| 1654 | * we sometimes have to double check. */ |
| 1655 | if (sector + (data_size>>9) > capacity) { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 1656 | drbd_err(device, "request from peer beyond end of local disk: " |
Lars Ellenberg | fdda654 | 2011-01-24 15:11:01 +0100 | [diff] [blame] | 1657 | "capacity: %llus < sector: %llus + size: %u\n", |
Lars Ellenberg | 6666032 | 2010-04-06 12:15:04 +0200 | [diff] [blame] | 1658 | (unsigned long long)capacity, |
| 1659 | (unsigned long long)sector, data_size); |
| 1660 | return NULL; |
| 1661 | } |
| 1662 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1663 | /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD |
| 1664 | * "criss-cross" setup, that might cause write-out on some other DRBD, |
| 1665 | * which in turn might block on the other node at this very place. */ |
Lars Ellenberg | a0fb3c4 | 2014-04-28 18:43:23 +0200 | [diff] [blame] | 1666 | peer_req = drbd_alloc_peer_req(peer_device, id, sector, data_size, trim == NULL, GFP_NOIO); |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1667 | if (!peer_req) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1668 | return NULL; |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1669 | |
Lars Ellenberg | 21ae5d7 | 2014-05-05 23:42:24 +0200 | [diff] [blame] | 1670 | peer_req->flags |= EE_WRITE; |
Lars Ellenberg | a0fb3c4 | 2014-04-28 18:43:23 +0200 | [diff] [blame] | 1671 | if (trim) |
Lars Ellenberg | 81a3537 | 2012-07-30 09:00:54 +0200 | [diff] [blame] | 1672 | return peer_req; |
Lars Ellenberg | a73ff32 | 2012-06-25 19:15:38 +0200 | [diff] [blame] | 1673 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1674 | ds = data_size; |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1675 | page = peer_req->pages; |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1676 | page_chain_for_each(page) { |
| 1677 | unsigned len = min_t(int, ds, PAGE_SIZE); |
Philipp Reisner | 6b4388a | 2010-04-26 14:11:45 +0200 | [diff] [blame] | 1678 | data = kmap(page); |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 1679 | err = drbd_recv_all_warn(peer_device->connection, data, len); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1680 | if (drbd_insert_fault(device, DRBD_FAULT_RECEIVE)) { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 1681 | drbd_err(device, "Fault injection: Corrupting data on receive\n"); |
Philipp Reisner | 6b4388a | 2010-04-26 14:11:45 +0200 | [diff] [blame] | 1682 | data[0] = data[0] ^ (unsigned long)-1; |
| 1683 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1684 | kunmap(page); |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 1685 | if (err) { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1686 | drbd_free_peer_req(device, peer_req); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1687 | return NULL; |
| 1688 | } |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 1689 | ds -= len; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1690 | } |
| 1691 | |
Andreas Gruenbacher | 11f8b2b | 2014-09-11 14:29:05 +0200 | [diff] [blame] | 1692 | if (digest_size) { |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 1693 | drbd_csum_ee(peer_device->connection->peer_integrity_tfm, peer_req, dig_vv); |
Andreas Gruenbacher | 11f8b2b | 2014-09-11 14:29:05 +0200 | [diff] [blame] | 1694 | if (memcmp(dig_in, dig_vv, digest_size)) { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 1695 | drbd_err(device, "Digest integrity check FAILED: %llus +%u\n", |
Lars Ellenberg | 470be44 | 2010-11-10 10:36:52 +0100 | [diff] [blame] | 1696 | (unsigned long long)sector, data_size); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1697 | drbd_free_peer_req(device, peer_req); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1698 | return NULL; |
| 1699 | } |
| 1700 | } |
Andreas Gruenbacher | 11f8b2b | 2014-09-11 14:29:05 +0200 | [diff] [blame] | 1701 | device->recv_cnt += data_size >> 9; |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1702 | return peer_req; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | /* drbd_drain_block() just takes a data block |
| 1706 | * out of the socket input buffer, and discards it. |
| 1707 | */ |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 1708 | static int drbd_drain_block(struct drbd_peer_device *peer_device, int data_size) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1709 | { |
| 1710 | struct page *page; |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 1711 | int err = 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1712 | void *data; |
| 1713 | |
Lars Ellenberg | c3470cd | 2010-04-01 16:57:19 +0200 | [diff] [blame] | 1714 | if (!data_size) |
Andreas Gruenbacher | fc5be83 | 2011-03-16 17:50:50 +0100 | [diff] [blame] | 1715 | return 0; |
Lars Ellenberg | c3470cd | 2010-04-01 16:57:19 +0200 | [diff] [blame] | 1716 | |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 1717 | page = drbd_alloc_pages(peer_device, 1, 1); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1718 | |
| 1719 | data = kmap(page); |
| 1720 | while (data_size) { |
Andreas Gruenbacher | fc5be83 | 2011-03-16 17:50:50 +0100 | [diff] [blame] | 1721 | unsigned int len = min_t(int, data_size, PAGE_SIZE); |
| 1722 | |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 1723 | err = drbd_recv_all_warn(peer_device->connection, data, len); |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 1724 | if (err) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1725 | break; |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 1726 | data_size -= len; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1727 | } |
| 1728 | kunmap(page); |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 1729 | drbd_free_pages(peer_device->device, page, 0); |
Andreas Gruenbacher | fc5be83 | 2011-03-16 17:50:50 +0100 | [diff] [blame] | 1730 | return err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1731 | } |
| 1732 | |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 1733 | static int recv_dless_read(struct drbd_peer_device *peer_device, struct drbd_request *req, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1734 | sector_t sector, int data_size) |
| 1735 | { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1736 | struct bio_vec bvec; |
| 1737 | struct bvec_iter iter; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1738 | struct bio *bio; |
Andreas Gruenbacher | 11f8b2b | 2014-09-11 14:29:05 +0200 | [diff] [blame] | 1739 | int digest_size, err, expect; |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 1740 | void *dig_in = peer_device->connection->int_dig_in; |
| 1741 | void *dig_vv = peer_device->connection->int_dig_vv; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1742 | |
Andreas Gruenbacher | 11f8b2b | 2014-09-11 14:29:05 +0200 | [diff] [blame] | 1743 | digest_size = 0; |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 1744 | if (peer_device->connection->peer_integrity_tfm) { |
Herbert Xu | 9534d67 | 2016-01-24 21:19:21 +0800 | [diff] [blame] | 1745 | digest_size = crypto_ahash_digestsize(peer_device->connection->peer_integrity_tfm); |
Andreas Gruenbacher | 11f8b2b | 2014-09-11 14:29:05 +0200 | [diff] [blame] | 1746 | err = drbd_recv_all_warn(peer_device->connection, dig_in, digest_size); |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 1747 | if (err) |
| 1748 | return err; |
Andreas Gruenbacher | 11f8b2b | 2014-09-11 14:29:05 +0200 | [diff] [blame] | 1749 | data_size -= digest_size; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1750 | } |
| 1751 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1752 | /* optimistically update recv_cnt. if receiving fails below, |
| 1753 | * we disconnect anyways, and counters will be reset. */ |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 1754 | peer_device->device->recv_cnt += data_size>>9; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1755 | |
| 1756 | bio = req->master_bio; |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 1757 | D_ASSERT(peer_device->device, sector == bio->bi_iter.bi_sector); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1758 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1759 | bio_for_each_segment(bvec, bio, iter) { |
| 1760 | void *mapped = kmap(bvec.bv_page) + bvec.bv_offset; |
| 1761 | expect = min_t(int, data_size, bvec.bv_len); |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 1762 | err = drbd_recv_all_warn(peer_device->connection, mapped, expect); |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1763 | kunmap(bvec.bv_page); |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 1764 | if (err) |
| 1765 | return err; |
| 1766 | data_size -= expect; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1767 | } |
| 1768 | |
Andreas Gruenbacher | 11f8b2b | 2014-09-11 14:29:05 +0200 | [diff] [blame] | 1769 | if (digest_size) { |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 1770 | drbd_csum_bio(peer_device->connection->peer_integrity_tfm, bio, dig_vv); |
Andreas Gruenbacher | 11f8b2b | 2014-09-11 14:29:05 +0200 | [diff] [blame] | 1771 | if (memcmp(dig_in, dig_vv, digest_size)) { |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 1772 | drbd_err(peer_device, "Digest integrity check FAILED. Broken NICs?\n"); |
Andreas Gruenbacher | 28284ce | 2011-03-16 17:54:02 +0100 | [diff] [blame] | 1773 | return -EINVAL; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1774 | } |
| 1775 | } |
| 1776 | |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 1777 | D_ASSERT(peer_device->device, data_size == 0); |
Andreas Gruenbacher | 28284ce | 2011-03-16 17:54:02 +0100 | [diff] [blame] | 1778 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1779 | } |
| 1780 | |
Andreas Gruenbacher | a990be4 | 2011-04-06 17:56:48 +0200 | [diff] [blame] | 1781 | /* |
Philipp Reisner | 668700b | 2015-03-16 16:08:29 +0100 | [diff] [blame] | 1782 | * e_end_resync_block() is called in ack_sender context via |
Andreas Gruenbacher | a990be4 | 2011-04-06 17:56:48 +0200 | [diff] [blame] | 1783 | * drbd_finish_peer_reqs(). |
| 1784 | */ |
Andreas Gruenbacher | 99920dc | 2011-03-16 15:31:39 +0100 | [diff] [blame] | 1785 | static int e_end_resync_block(struct drbd_work *w, int unused) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1786 | { |
Andreas Gruenbacher | 8050e6d | 2011-02-18 16:12:48 +0100 | [diff] [blame] | 1787 | struct drbd_peer_request *peer_req = |
Andreas Gruenbacher | a8cd15b | 2011-08-25 15:49:40 +0200 | [diff] [blame] | 1788 | container_of(w, struct drbd_peer_request, w); |
| 1789 | struct drbd_peer_device *peer_device = peer_req->peer_device; |
| 1790 | struct drbd_device *device = peer_device->device; |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1791 | sector_t sector = peer_req->i.sector; |
Andreas Gruenbacher | 99920dc | 2011-03-16 15:31:39 +0100 | [diff] [blame] | 1792 | int err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1793 | |
Andreas Gruenbacher | 0b0ba1e | 2011-06-27 16:23:33 +0200 | [diff] [blame] | 1794 | D_ASSERT(device, drbd_interval_empty(&peer_req->i)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1795 | |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1796 | if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1797 | drbd_set_in_sync(device, sector, peer_req->i.size); |
Andreas Gruenbacher | a8cd15b | 2011-08-25 15:49:40 +0200 | [diff] [blame] | 1798 | err = drbd_send_ack(peer_device, P_RS_WRITE_ACK, peer_req); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1799 | } else { |
| 1800 | /* Record failure to sync */ |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1801 | drbd_rs_failed_io(device, sector, peer_req->i.size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1802 | |
Andreas Gruenbacher | a8cd15b | 2011-08-25 15:49:40 +0200 | [diff] [blame] | 1803 | err = drbd_send_ack(peer_device, P_NEG_ACK, peer_req); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1804 | } |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1805 | dec_unacked(device); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1806 | |
Andreas Gruenbacher | 99920dc | 2011-03-16 15:31:39 +0100 | [diff] [blame] | 1807 | return err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1808 | } |
| 1809 | |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 1810 | static int recv_resync_read(struct drbd_peer_device *peer_device, sector_t sector, |
Lars Ellenberg | a0fb3c4 | 2014-04-28 18:43:23 +0200 | [diff] [blame] | 1811 | struct packet_info *pi) __releases(local) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1812 | { |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 1813 | struct drbd_device *device = peer_device->device; |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1814 | struct drbd_peer_request *peer_req; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1815 | |
Lars Ellenberg | a0fb3c4 | 2014-04-28 18:43:23 +0200 | [diff] [blame] | 1816 | peer_req = read_in_block(peer_device, ID_SYNCER, sector, pi); |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1817 | if (!peer_req) |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1818 | goto fail; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1819 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1820 | dec_rs_pending(device); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1821 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1822 | inc_unacked(device); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1823 | /* corresponding dec_unacked() in e_end_resync_block() |
| 1824 | * respective _drbd_clear_done_ee */ |
| 1825 | |
Andreas Gruenbacher | a8cd15b | 2011-08-25 15:49:40 +0200 | [diff] [blame] | 1826 | peer_req->w.cb = e_end_resync_block; |
Lars Ellenberg | 21ae5d7 | 2014-05-05 23:42:24 +0200 | [diff] [blame] | 1827 | peer_req->submit_jif = jiffies; |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1828 | |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 1829 | spin_lock_irq(&device->resource->req_lock); |
Lars Ellenberg | b9ed708 | 2014-04-23 12:15:35 +0200 | [diff] [blame] | 1830 | list_add_tail(&peer_req->w.list, &device->sync_ee); |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 1831 | spin_unlock_irq(&device->resource->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1832 | |
Lars Ellenberg | a0fb3c4 | 2014-04-28 18:43:23 +0200 | [diff] [blame] | 1833 | atomic_add(pi->size >> 9, &device->rs_sect_ev); |
Mike Christie | bb3cc85 | 2016-06-05 14:32:06 -0500 | [diff] [blame^] | 1834 | if (drbd_submit_peer_request(device, peer_req, REQ_OP_WRITE, 0, |
| 1835 | DRBD_FAULT_RS_WR) == 0) |
Andreas Gruenbacher | e1c1b0f | 2011-03-16 17:58:27 +0100 | [diff] [blame] | 1836 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1837 | |
Lars Ellenberg | 10f6d992 | 2011-01-24 14:47:09 +0100 | [diff] [blame] | 1838 | /* don't care for the reason here */ |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 1839 | drbd_err(device, "submit failed, triggering re-connect\n"); |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 1840 | spin_lock_irq(&device->resource->req_lock); |
Andreas Gruenbacher | a8cd15b | 2011-08-25 15:49:40 +0200 | [diff] [blame] | 1841 | list_del(&peer_req->w.list); |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 1842 | spin_unlock_irq(&device->resource->req_lock); |
Lars Ellenberg | 22cc37a | 2010-09-14 20:40:41 +0200 | [diff] [blame] | 1843 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1844 | drbd_free_peer_req(device, peer_req); |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1845 | fail: |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1846 | put_ldev(device); |
Andreas Gruenbacher | e1c1b0f | 2011-03-16 17:58:27 +0100 | [diff] [blame] | 1847 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1848 | } |
| 1849 | |
Andreas Gruenbacher | 668eebc | 2011-01-20 17:14:26 +0100 | [diff] [blame] | 1850 | static struct drbd_request * |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1851 | find_request(struct drbd_device *device, struct rb_root *root, u64 id, |
Andreas Gruenbacher | bc9c5c4 | 2011-01-21 18:00:55 +0100 | [diff] [blame] | 1852 | sector_t sector, bool missing_ok, const char *func) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1853 | { |
| 1854 | struct drbd_request *req; |
Andreas Gruenbacher | 668eebc | 2011-01-20 17:14:26 +0100 | [diff] [blame] | 1855 | |
Andreas Gruenbacher | bc9c5c4 | 2011-01-21 18:00:55 +0100 | [diff] [blame] | 1856 | /* Request object according to our peer */ |
| 1857 | req = (struct drbd_request *)(unsigned long)id; |
Andreas Gruenbacher | 5e47226 | 2011-01-27 14:42:51 +0100 | [diff] [blame] | 1858 | if (drbd_contains_interval(root, sector, &req->i) && req->i.local) |
Andreas Gruenbacher | 668eebc | 2011-01-20 17:14:26 +0100 | [diff] [blame] | 1859 | return req; |
Andreas Gruenbacher | c3afd8f | 2011-01-20 22:25:40 +0100 | [diff] [blame] | 1860 | if (!missing_ok) { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 1861 | drbd_err(device, "%s: failed to find request 0x%lx, sector %llus\n", func, |
Andreas Gruenbacher | c3afd8f | 2011-01-20 22:25:40 +0100 | [diff] [blame] | 1862 | (unsigned long)id, (unsigned long long)sector); |
| 1863 | } |
Andreas Gruenbacher | 668eebc | 2011-01-20 17:14:26 +0100 | [diff] [blame] | 1864 | return NULL; |
| 1865 | } |
| 1866 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1867 | static int receive_DataReply(struct drbd_connection *connection, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1868 | { |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 1869 | struct drbd_peer_device *peer_device; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1870 | struct drbd_device *device; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1871 | struct drbd_request *req; |
| 1872 | sector_t sector; |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 1873 | int err; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 1874 | struct p_data *p = pi->data; |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 1875 | |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 1876 | peer_device = conn_peer_device(connection, pi->vnr); |
| 1877 | if (!peer_device) |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 1878 | return -EIO; |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 1879 | device = peer_device->device; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1880 | |
| 1881 | sector = be64_to_cpu(p->sector); |
| 1882 | |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 1883 | spin_lock_irq(&device->resource->req_lock); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1884 | req = find_request(device, &device->read_requests, p->block_id, sector, false, __func__); |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 1885 | spin_unlock_irq(&device->resource->req_lock); |
Andreas Gruenbacher | c3afd8f | 2011-01-20 22:25:40 +0100 | [diff] [blame] | 1886 | if (unlikely(!req)) |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 1887 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1888 | |
Bart Van Assche | 24c4830 | 2011-05-21 18:32:29 +0200 | [diff] [blame] | 1889 | /* 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] | 1890 | * special casing it there for the various failure cases. |
| 1891 | * still no race with drbd_fail_pending_reads */ |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 1892 | err = recv_dless_read(peer_device, req, sector, pi->size); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 1893 | if (!err) |
Andreas Gruenbacher | 8554df1 | 2011-01-25 15:37:43 +0100 | [diff] [blame] | 1894 | req_mod(req, DATA_RECEIVED); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1895 | /* else: nothing. handled from drbd_disconnect... |
| 1896 | * I don't think we may complete this just yet |
| 1897 | * in case we are "on-disconnect: freeze" */ |
| 1898 | |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 1899 | return err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1900 | } |
| 1901 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 1902 | static int receive_RSDataReply(struct drbd_connection *connection, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1903 | { |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 1904 | struct drbd_peer_device *peer_device; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1905 | struct drbd_device *device; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1906 | sector_t sector; |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 1907 | int err; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 1908 | struct p_data *p = pi->data; |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 1909 | |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 1910 | peer_device = conn_peer_device(connection, pi->vnr); |
| 1911 | if (!peer_device) |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 1912 | return -EIO; |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 1913 | device = peer_device->device; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1914 | |
| 1915 | sector = be64_to_cpu(p->sector); |
Andreas Gruenbacher | 0b0ba1e | 2011-06-27 16:23:33 +0200 | [diff] [blame] | 1916 | D_ASSERT(device, p->block_id == ID_SYNCER); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1917 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1918 | if (get_ldev(device)) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1919 | /* data is submitted to disk within recv_resync_read. |
| 1920 | * corresponding put_ldev done below on error, |
Andreas Gruenbacher | fcefa62 | 2011-02-17 16:46:59 +0100 | [diff] [blame] | 1921 | * or in drbd_peer_request_endio. */ |
Lars Ellenberg | a0fb3c4 | 2014-04-28 18:43:23 +0200 | [diff] [blame] | 1922 | err = recv_resync_read(peer_device, sector, pi); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1923 | } else { |
| 1924 | if (__ratelimit(&drbd_ratelimit_state)) |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 1925 | drbd_err(device, "Can not write resync data to local disk.\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1926 | |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 1927 | err = drbd_drain_block(peer_device, pi->size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1928 | |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 1929 | drbd_send_ack_dp(peer_device, P_NEG_ACK, p, pi->size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1930 | } |
| 1931 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1932 | atomic_add(pi->size >> 9, &device->rs_sect_in); |
Philipp Reisner | 778f271 | 2010-07-06 11:14:00 +0200 | [diff] [blame] | 1933 | |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 1934 | return err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1935 | } |
| 1936 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1937 | static void restart_conflicting_writes(struct drbd_device *device, |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 1938 | sector_t sector, int size) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1939 | { |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 1940 | struct drbd_interval *i; |
| 1941 | struct drbd_request *req; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1942 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1943 | drbd_for_each_overlap(i, &device->write_requests, sector, size) { |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 1944 | if (!i->local) |
| 1945 | continue; |
| 1946 | req = container_of(i, struct drbd_request, i); |
| 1947 | if (req->rq_state & RQ_LOCAL_PENDING || |
| 1948 | !(req->rq_state & RQ_POSTPONED)) |
| 1949 | continue; |
Lars Ellenberg | 2312f0b3 | 2011-11-24 10:36:25 +0100 | [diff] [blame] | 1950 | /* as it is RQ_POSTPONED, this will cause it to |
| 1951 | * be queued on the retry workqueue. */ |
Lars Ellenberg | d4dabbe | 2012-08-01 12:33:51 +0200 | [diff] [blame] | 1952 | __req_mod(req, CONFLICT_RESOLVED, NULL); |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 1953 | } |
| 1954 | } |
| 1955 | |
Andreas Gruenbacher | a990be4 | 2011-04-06 17:56:48 +0200 | [diff] [blame] | 1956 | /* |
Philipp Reisner | 668700b | 2015-03-16 16:08:29 +0100 | [diff] [blame] | 1957 | * e_end_block() is called in ack_sender context via drbd_finish_peer_reqs(). |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1958 | */ |
Andreas Gruenbacher | 99920dc | 2011-03-16 15:31:39 +0100 | [diff] [blame] | 1959 | static int e_end_block(struct drbd_work *w, int cancel) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1960 | { |
Andreas Gruenbacher | 8050e6d | 2011-02-18 16:12:48 +0100 | [diff] [blame] | 1961 | struct drbd_peer_request *peer_req = |
Andreas Gruenbacher | a8cd15b | 2011-08-25 15:49:40 +0200 | [diff] [blame] | 1962 | container_of(w, struct drbd_peer_request, w); |
| 1963 | struct drbd_peer_device *peer_device = peer_req->peer_device; |
| 1964 | struct drbd_device *device = peer_device->device; |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1965 | sector_t sector = peer_req->i.sector; |
Andreas Gruenbacher | 99920dc | 2011-03-16 15:31:39 +0100 | [diff] [blame] | 1966 | int err = 0, pcmd; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1967 | |
Philipp Reisner | 303d144 | 2011-04-13 16:24:47 -0700 | [diff] [blame] | 1968 | if (peer_req->flags & EE_SEND_WRITE_ACK) { |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1969 | if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1970 | pcmd = (device->state.conn >= C_SYNC_SOURCE && |
| 1971 | device->state.conn <= C_PAUSED_SYNC_T && |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1972 | peer_req->flags & EE_MAY_SET_IN_SYNC) ? |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1973 | P_RS_WRITE_ACK : P_WRITE_ACK; |
Andreas Gruenbacher | a8cd15b | 2011-08-25 15:49:40 +0200 | [diff] [blame] | 1974 | err = drbd_send_ack(peer_device, pcmd, peer_req); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1975 | if (pcmd == P_RS_WRITE_ACK) |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1976 | drbd_set_in_sync(device, sector, peer_req->i.size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1977 | } else { |
Andreas Gruenbacher | a8cd15b | 2011-08-25 15:49:40 +0200 | [diff] [blame] | 1978 | err = drbd_send_ack(peer_device, P_NEG_ACK, peer_req); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1979 | /* we expect it to be marked out of sync anyways... |
| 1980 | * maybe assert this? */ |
| 1981 | } |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1982 | dec_unacked(device); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1983 | } |
Lars Ellenberg | 08d0dab | 2014-03-20 11:19:22 +0100 | [diff] [blame] | 1984 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1985 | /* we delete from the conflict detection hash _after_ we sent out the |
| 1986 | * P_WRITE_ACK / P_NEG_ACK, to get the sequence number right. */ |
Philipp Reisner | 302bdea | 2011-04-21 11:36:49 +0200 | [diff] [blame] | 1987 | if (peer_req->flags & EE_IN_INTERVAL_TREE) { |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 1988 | spin_lock_irq(&device->resource->req_lock); |
Andreas Gruenbacher | 0b0ba1e | 2011-06-27 16:23:33 +0200 | [diff] [blame] | 1989 | D_ASSERT(device, !drbd_interval_empty(&peer_req->i)); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1990 | drbd_remove_epoch_entry_interval(device, peer_req); |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 1991 | if (peer_req->flags & EE_RESTART_REQUESTS) |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 1992 | restart_conflicting_writes(device, sector, peer_req->i.size); |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 1993 | spin_unlock_irq(&device->resource->req_lock); |
Andreas Gruenbacher | bb3bfe9 | 2011-01-21 15:59:23 +0100 | [diff] [blame] | 1994 | } else |
Andreas Gruenbacher | 0b0ba1e | 2011-06-27 16:23:33 +0200 | [diff] [blame] | 1995 | D_ASSERT(device, drbd_interval_empty(&peer_req->i)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1996 | |
Andreas Gruenbacher | 5dd2ca1 | 2014-08-11 16:59:23 +0200 | [diff] [blame] | 1997 | drbd_may_finish_epoch(peer_device->connection, peer_req->epoch, EV_PUT + (cancel ? EV_CLEANUP : 0)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1998 | |
Andreas Gruenbacher | 99920dc | 2011-03-16 15:31:39 +0100 | [diff] [blame] | 1999 | return err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2000 | } |
| 2001 | |
Andreas Gruenbacher | a8cd15b | 2011-08-25 15:49:40 +0200 | [diff] [blame] | 2002 | 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] | 2003 | { |
Andreas Gruenbacher | 8050e6d | 2011-02-18 16:12:48 +0100 | [diff] [blame] | 2004 | struct drbd_peer_request *peer_req = |
Andreas Gruenbacher | a8cd15b | 2011-08-25 15:49:40 +0200 | [diff] [blame] | 2005 | container_of(w, struct drbd_peer_request, w); |
| 2006 | struct drbd_peer_device *peer_device = peer_req->peer_device; |
Andreas Gruenbacher | 99920dc | 2011-03-16 15:31:39 +0100 | [diff] [blame] | 2007 | int err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2008 | |
Andreas Gruenbacher | a8cd15b | 2011-08-25 15:49:40 +0200 | [diff] [blame] | 2009 | err = drbd_send_ack(peer_device, ack, peer_req); |
| 2010 | dec_unacked(peer_device->device); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2011 | |
Andreas Gruenbacher | 99920dc | 2011-03-16 15:31:39 +0100 | [diff] [blame] | 2012 | return err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2013 | } |
| 2014 | |
Lars Ellenberg | d4dabbe | 2012-08-01 12:33:51 +0200 | [diff] [blame] | 2015 | static int e_send_superseded(struct drbd_work *w, int unused) |
Philipp Reisner | b6a370ba | 2012-02-19 01:27:53 +0100 | [diff] [blame] | 2016 | { |
Andreas Gruenbacher | a8cd15b | 2011-08-25 15:49:40 +0200 | [diff] [blame] | 2017 | return e_send_ack(w, P_SUPERSEDED); |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2018 | } |
Philipp Reisner | b6a370ba | 2012-02-19 01:27:53 +0100 | [diff] [blame] | 2019 | |
Andreas Gruenbacher | 99920dc | 2011-03-16 15:31:39 +0100 | [diff] [blame] | 2020 | static int e_send_retry_write(struct drbd_work *w, int unused) |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2021 | { |
Andreas Gruenbacher | a8cd15b | 2011-08-25 15:49:40 +0200 | [diff] [blame] | 2022 | struct drbd_peer_request *peer_req = |
| 2023 | container_of(w, struct drbd_peer_request, w); |
| 2024 | struct drbd_connection *connection = peer_req->peer_device->connection; |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2025 | |
Andreas Gruenbacher | a8cd15b | 2011-08-25 15:49:40 +0200 | [diff] [blame] | 2026 | return e_send_ack(w, connection->agreed_pro_version >= 100 ? |
Lars Ellenberg | d4dabbe | 2012-08-01 12:33:51 +0200 | [diff] [blame] | 2027 | P_RETRY_WRITE : P_SUPERSEDED); |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2028 | } |
| 2029 | |
Andreas Gruenbacher | 3e394da | 2011-01-26 18:36:55 +0100 | [diff] [blame] | 2030 | static bool seq_greater(u32 a, u32 b) |
| 2031 | { |
| 2032 | /* |
| 2033 | * We assume 32-bit wrap-around here. |
| 2034 | * For 24-bit wrap-around, we would have to shift: |
| 2035 | * a <<= 8; b <<= 8; |
| 2036 | */ |
| 2037 | return (s32)a - (s32)b > 0; |
| 2038 | } |
| 2039 | |
| 2040 | static u32 seq_max(u32 a, u32 b) |
| 2041 | { |
| 2042 | return seq_greater(a, b) ? a : b; |
| 2043 | } |
| 2044 | |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 2045 | static void update_peer_seq(struct drbd_peer_device *peer_device, unsigned int peer_seq) |
Andreas Gruenbacher | 3e394da | 2011-01-26 18:36:55 +0100 | [diff] [blame] | 2046 | { |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 2047 | struct drbd_device *device = peer_device->device; |
Lars Ellenberg | 3c13b68 | 2011-02-23 16:10:01 +0100 | [diff] [blame] | 2048 | unsigned int newest_peer_seq; |
Andreas Gruenbacher | 3e394da | 2011-01-26 18:36:55 +0100 | [diff] [blame] | 2049 | |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 2050 | if (test_bit(RESOLVE_CONFLICTS, &peer_device->connection->flags)) { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2051 | spin_lock(&device->peer_seq_lock); |
| 2052 | newest_peer_seq = seq_max(device->peer_seq, peer_seq); |
| 2053 | device->peer_seq = newest_peer_seq; |
| 2054 | spin_unlock(&device->peer_seq_lock); |
| 2055 | /* wake up only if we actually changed device->peer_seq */ |
Lars Ellenberg | 3c13b68 | 2011-02-23 16:10:01 +0100 | [diff] [blame] | 2056 | if (peer_seq == newest_peer_seq) |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2057 | wake_up(&device->seq_wait); |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2058 | } |
Andreas Gruenbacher | 3e394da | 2011-01-26 18:36:55 +0100 | [diff] [blame] | 2059 | } |
| 2060 | |
Lars Ellenberg | d93f630 | 2012-03-26 15:49:13 +0200 | [diff] [blame] | 2061 | static inline int overlaps(sector_t s1, int l1, sector_t s2, int l2) |
| 2062 | { |
| 2063 | return !((s1 + (l1>>9) <= s2) || (s1 >= s2 + (l2>>9))); |
| 2064 | } |
| 2065 | |
| 2066 | /* maybe change sync_ee into interval trees as well? */ |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2067 | static bool overlapping_resync_write(struct drbd_device *device, struct drbd_peer_request *peer_req) |
Lars Ellenberg | d93f630 | 2012-03-26 15:49:13 +0200 | [diff] [blame] | 2068 | { |
| 2069 | struct drbd_peer_request *rs_req; |
Philipp Reisner | b6a370ba | 2012-02-19 01:27:53 +0100 | [diff] [blame] | 2070 | bool rv = 0; |
| 2071 | |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 2072 | spin_lock_irq(&device->resource->req_lock); |
Andreas Gruenbacher | a8cd15b | 2011-08-25 15:49:40 +0200 | [diff] [blame] | 2073 | list_for_each_entry(rs_req, &device->sync_ee, w.list) { |
Lars Ellenberg | d93f630 | 2012-03-26 15:49:13 +0200 | [diff] [blame] | 2074 | if (overlaps(peer_req->i.sector, peer_req->i.size, |
| 2075 | rs_req->i.sector, rs_req->i.size)) { |
Philipp Reisner | b6a370ba | 2012-02-19 01:27:53 +0100 | [diff] [blame] | 2076 | rv = 1; |
| 2077 | break; |
| 2078 | } |
| 2079 | } |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 2080 | spin_unlock_irq(&device->resource->req_lock); |
Philipp Reisner | b6a370ba | 2012-02-19 01:27:53 +0100 | [diff] [blame] | 2081 | |
| 2082 | return rv; |
| 2083 | } |
| 2084 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2085 | /* Called from receive_Data. |
| 2086 | * Synchronize packets on sock with packets on msock. |
| 2087 | * |
| 2088 | * This is here so even when a P_DATA packet traveling via sock overtook an Ack |
| 2089 | * packet traveling on msock, they are still processed in the order they have |
| 2090 | * been sent. |
| 2091 | * |
| 2092 | * Note: we don't care for Ack packets overtaking P_DATA packets. |
| 2093 | * |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2094 | * In case packet_seq is larger than device->peer_seq number, there are |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2095 | * outstanding packets on the msock. We wait for them to arrive. |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2096 | * In case we are the logically next packet, we update device->peer_seq |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2097 | * ourselves. Correctly handles 32bit wrap around. |
| 2098 | * |
| 2099 | * Assume we have a 10 GBit connection, that is about 1<<30 byte per second, |
| 2100 | * about 1<<21 sectors per second. So "worst" case, we have 1<<3 == 8 seconds |
| 2101 | * for the 24bit wrap (historical atomic_t guarantee on some archs), and we have |
| 2102 | * 1<<9 == 512 seconds aka ages for the 32bit wrap around... |
| 2103 | * |
| 2104 | * returns 0 if we may process the packet, |
| 2105 | * -ERESTARTSYS if we were interrupted (by disconnect signal). */ |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 2106 | static int wait_for_and_update_peer_seq(struct drbd_peer_device *peer_device, const u32 peer_seq) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2107 | { |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 2108 | struct drbd_device *device = peer_device->device; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2109 | DEFINE_WAIT(wait); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2110 | long timeout; |
Philipp Reisner | b874d23 | 2013-10-23 10:59:16 +0200 | [diff] [blame] | 2111 | int ret = 0, tp; |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2112 | |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 2113 | if (!test_bit(RESOLVE_CONFLICTS, &peer_device->connection->flags)) |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2114 | return 0; |
| 2115 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2116 | spin_lock(&device->peer_seq_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2117 | for (;;) { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2118 | if (!seq_greater(peer_seq - 1, device->peer_seq)) { |
| 2119 | device->peer_seq = seq_max(device->peer_seq, peer_seq); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2120 | break; |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2121 | } |
Philipp Reisner | b874d23 | 2013-10-23 10:59:16 +0200 | [diff] [blame] | 2122 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2123 | if (signal_pending(current)) { |
| 2124 | ret = -ERESTARTSYS; |
| 2125 | break; |
| 2126 | } |
Philipp Reisner | b874d23 | 2013-10-23 10:59:16 +0200 | [diff] [blame] | 2127 | |
| 2128 | rcu_read_lock(); |
Andreas Gruenbacher | 5dd2ca1 | 2014-08-11 16:59:23 +0200 | [diff] [blame] | 2129 | tp = rcu_dereference(peer_device->connection->net_conf)->two_primaries; |
Philipp Reisner | b874d23 | 2013-10-23 10:59:16 +0200 | [diff] [blame] | 2130 | rcu_read_unlock(); |
| 2131 | |
| 2132 | if (!tp) |
| 2133 | break; |
| 2134 | |
| 2135 | /* Only need to wait if two_primaries is enabled */ |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2136 | prepare_to_wait(&device->seq_wait, &wait, TASK_INTERRUPTIBLE); |
| 2137 | spin_unlock(&device->peer_seq_lock); |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 2138 | rcu_read_lock(); |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 2139 | timeout = rcu_dereference(peer_device->connection->net_conf)->ping_timeo*HZ/10; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 2140 | rcu_read_unlock(); |
Andreas Gruenbacher | 71b1c1e | 2011-03-01 15:40:43 +0100 | [diff] [blame] | 2141 | timeout = schedule_timeout(timeout); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2142 | spin_lock(&device->peer_seq_lock); |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2143 | if (!timeout) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2144 | ret = -ETIMEDOUT; |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 2145 | drbd_err(device, "Timed out waiting for missing ack packets; disconnecting\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2146 | break; |
| 2147 | } |
| 2148 | } |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2149 | spin_unlock(&device->peer_seq_lock); |
| 2150 | finish_wait(&device->seq_wait, &wait); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2151 | return ret; |
| 2152 | } |
| 2153 | |
Lars Ellenberg | 688593c | 2010-11-17 22:25:03 +0100 | [diff] [blame] | 2154 | /* see also bio_flags_to_wire() |
| 2155 | * DRBD_REQ_*, because we need to semantically map the flags to data packet |
| 2156 | * flags and back. We may replicate to other kernel versions. */ |
Mike Christie | bb3cc85 | 2016-06-05 14:32:06 -0500 | [diff] [blame^] | 2157 | static unsigned long wire_flags_to_bio_flags(u32 dpf) |
Philipp Reisner | 76d2e7e | 2010-08-25 11:58:05 +0200 | [diff] [blame] | 2158 | { |
Lars Ellenberg | 688593c | 2010-11-17 22:25:03 +0100 | [diff] [blame] | 2159 | return (dpf & DP_RW_SYNC ? REQ_SYNC : 0) | |
| 2160 | (dpf & DP_FUA ? REQ_FUA : 0) | |
Mike Christie | bb3cc85 | 2016-06-05 14:32:06 -0500 | [diff] [blame^] | 2161 | (dpf & DP_FLUSH ? REQ_FLUSH : 0); |
| 2162 | } |
| 2163 | |
| 2164 | static unsigned long wire_flags_to_bio_op(u32 dpf) |
| 2165 | { |
| 2166 | if (dpf & DP_DISCARD) |
| 2167 | return REQ_OP_DISCARD; |
| 2168 | else |
| 2169 | return REQ_OP_WRITE; |
Philipp Reisner | 76d2e7e | 2010-08-25 11:58:05 +0200 | [diff] [blame] | 2170 | } |
| 2171 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2172 | static void fail_postponed_requests(struct drbd_device *device, sector_t sector, |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2173 | unsigned int size) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2174 | { |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2175 | struct drbd_interval *i; |
| 2176 | |
| 2177 | repeat: |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2178 | drbd_for_each_overlap(i, &device->write_requests, sector, size) { |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2179 | struct drbd_request *req; |
| 2180 | struct bio_and_error m; |
| 2181 | |
| 2182 | if (!i->local) |
| 2183 | continue; |
| 2184 | req = container_of(i, struct drbd_request, i); |
| 2185 | if (!(req->rq_state & RQ_POSTPONED)) |
| 2186 | continue; |
| 2187 | req->rq_state &= ~RQ_POSTPONED; |
| 2188 | __req_mod(req, NEG_ACKED, &m); |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 2189 | spin_unlock_irq(&device->resource->req_lock); |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2190 | if (m.bio) |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2191 | complete_master_bio(device, &m); |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 2192 | spin_lock_irq(&device->resource->req_lock); |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2193 | goto repeat; |
| 2194 | } |
| 2195 | } |
| 2196 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2197 | static int handle_write_conflicts(struct drbd_device *device, |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2198 | struct drbd_peer_request *peer_req) |
| 2199 | { |
Andreas Gruenbacher | e33b32d | 2011-08-30 15:38:04 +0200 | [diff] [blame] | 2200 | struct drbd_connection *connection = peer_req->peer_device->connection; |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 2201 | bool resolve_conflicts = test_bit(RESOLVE_CONFLICTS, &connection->flags); |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2202 | sector_t sector = peer_req->i.sector; |
| 2203 | const unsigned int size = peer_req->i.size; |
| 2204 | struct drbd_interval *i; |
| 2205 | bool equal; |
| 2206 | int err; |
| 2207 | |
| 2208 | /* |
| 2209 | * Inserting the peer request into the write_requests tree will prevent |
| 2210 | * new conflicting local requests from being added. |
| 2211 | */ |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2212 | drbd_insert_interval(&device->write_requests, &peer_req->i); |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2213 | |
| 2214 | repeat: |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2215 | drbd_for_each_overlap(i, &device->write_requests, sector, size) { |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2216 | if (i == &peer_req->i) |
| 2217 | continue; |
Lars Ellenberg | 08d0dab | 2014-03-20 11:19:22 +0100 | [diff] [blame] | 2218 | if (i->completed) |
| 2219 | continue; |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2220 | |
| 2221 | if (!i->local) { |
| 2222 | /* |
| 2223 | * Our peer has sent a conflicting remote request; this |
| 2224 | * should not happen in a two-node setup. Wait for the |
| 2225 | * earlier peer request to complete. |
| 2226 | */ |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2227 | err = drbd_wait_misc(device, i); |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2228 | if (err) |
| 2229 | goto out; |
| 2230 | goto repeat; |
| 2231 | } |
| 2232 | |
| 2233 | equal = i->sector == sector && i->size == size; |
| 2234 | if (resolve_conflicts) { |
| 2235 | /* |
| 2236 | * If the peer request is fully contained within the |
Lars Ellenberg | d4dabbe | 2012-08-01 12:33:51 +0200 | [diff] [blame] | 2237 | * overlapping request, it can be considered overwritten |
| 2238 | * and thus superseded; otherwise, it will be retried |
| 2239 | * once all overlapping requests have completed. |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2240 | */ |
Lars Ellenberg | d4dabbe | 2012-08-01 12:33:51 +0200 | [diff] [blame] | 2241 | bool superseded = i->sector <= sector && i->sector + |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2242 | (i->size >> 9) >= sector + (size >> 9); |
| 2243 | |
| 2244 | if (!equal) |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 2245 | drbd_alert(device, "Concurrent writes detected: " |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2246 | "local=%llus +%u, remote=%llus +%u, " |
| 2247 | "assuming %s came first\n", |
| 2248 | (unsigned long long)i->sector, i->size, |
| 2249 | (unsigned long long)sector, size, |
Lars Ellenberg | d4dabbe | 2012-08-01 12:33:51 +0200 | [diff] [blame] | 2250 | superseded ? "local" : "remote"); |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2251 | |
Andreas Gruenbacher | a8cd15b | 2011-08-25 15:49:40 +0200 | [diff] [blame] | 2252 | peer_req->w.cb = superseded ? e_send_superseded : |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2253 | e_send_retry_write; |
Andreas Gruenbacher | a8cd15b | 2011-08-25 15:49:40 +0200 | [diff] [blame] | 2254 | list_add_tail(&peer_req->w.list, &device->done_ee); |
Philipp Reisner | 668700b | 2015-03-16 16:08:29 +0100 | [diff] [blame] | 2255 | queue_work(connection->ack_sender, &peer_req->peer_device->send_acks_work); |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2256 | |
| 2257 | err = -ENOENT; |
| 2258 | goto out; |
| 2259 | } else { |
| 2260 | struct drbd_request *req = |
| 2261 | container_of(i, struct drbd_request, i); |
| 2262 | |
| 2263 | if (!equal) |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 2264 | drbd_alert(device, "Concurrent writes detected: " |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2265 | "local=%llus +%u, remote=%llus +%u\n", |
| 2266 | (unsigned long long)i->sector, i->size, |
| 2267 | (unsigned long long)sector, size); |
| 2268 | |
| 2269 | if (req->rq_state & RQ_LOCAL_PENDING || |
| 2270 | !(req->rq_state & RQ_POSTPONED)) { |
| 2271 | /* |
| 2272 | * Wait for the node with the discard flag to |
Lars Ellenberg | d4dabbe | 2012-08-01 12:33:51 +0200 | [diff] [blame] | 2273 | * decide if this request has been superseded |
| 2274 | * or needs to be retried. |
| 2275 | * Requests that have been superseded will |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2276 | * disappear from the write_requests tree. |
| 2277 | * |
| 2278 | * In addition, wait for the conflicting |
| 2279 | * request to finish locally before submitting |
| 2280 | * the conflicting peer request. |
| 2281 | */ |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2282 | err = drbd_wait_misc(device, &req->i); |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2283 | if (err) { |
Andreas Gruenbacher | e33b32d | 2011-08-30 15:38:04 +0200 | [diff] [blame] | 2284 | _conn_request_state(connection, NS(conn, C_TIMEOUT), CS_HARD); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2285 | fail_postponed_requests(device, sector, size); |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2286 | goto out; |
| 2287 | } |
| 2288 | goto repeat; |
| 2289 | } |
| 2290 | /* |
| 2291 | * Remember to restart the conflicting requests after |
| 2292 | * the new peer request has completed. |
| 2293 | */ |
| 2294 | peer_req->flags |= EE_RESTART_REQUESTS; |
| 2295 | } |
| 2296 | } |
| 2297 | err = 0; |
| 2298 | |
| 2299 | out: |
| 2300 | if (err) |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2301 | drbd_remove_epoch_entry_interval(device, peer_req); |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2302 | return err; |
| 2303 | } |
| 2304 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2305 | /* mirrored write */ |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 2306 | static int receive_Data(struct drbd_connection *connection, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2307 | { |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 2308 | struct drbd_peer_device *peer_device; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2309 | struct drbd_device *device; |
Lars Ellenberg | 21ae5d7 | 2014-05-05 23:42:24 +0200 | [diff] [blame] | 2310 | struct net_conf *nc; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2311 | sector_t sector; |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 2312 | struct drbd_peer_request *peer_req; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 2313 | struct p_data *p = pi->data; |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2314 | u32 peer_seq = be32_to_cpu(p->seq_num); |
Mike Christie | bb3cc85 | 2016-06-05 14:32:06 -0500 | [diff] [blame^] | 2315 | int op, op_flags; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2316 | u32 dp_flags; |
Philipp Reisner | 302bdea | 2011-04-21 11:36:49 +0200 | [diff] [blame] | 2317 | int err, tp; |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2318 | |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 2319 | peer_device = conn_peer_device(connection, pi->vnr); |
| 2320 | if (!peer_device) |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 2321 | return -EIO; |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 2322 | device = peer_device->device; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2323 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2324 | if (!get_ldev(device)) { |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 2325 | int err2; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2326 | |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 2327 | err = wait_for_and_update_peer_seq(peer_device, peer_seq); |
| 2328 | drbd_send_ack_dp(peer_device, P_NEG_ACK, p, pi->size); |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 2329 | atomic_inc(&connection->current_epoch->epoch_size); |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 2330 | err2 = drbd_drain_block(peer_device, pi->size); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 2331 | if (!err) |
| 2332 | err = err2; |
| 2333 | return err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2334 | } |
| 2335 | |
Andreas Gruenbacher | fcefa62 | 2011-02-17 16:46:59 +0100 | [diff] [blame] | 2336 | /* |
| 2337 | * Corresponding put_ldev done either below (on various errors), or in |
| 2338 | * drbd_peer_request_endio, if we successfully submit the data at the |
| 2339 | * end of this function. |
| 2340 | */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2341 | |
| 2342 | sector = be64_to_cpu(p->sector); |
Lars Ellenberg | a0fb3c4 | 2014-04-28 18:43:23 +0200 | [diff] [blame] | 2343 | peer_req = read_in_block(peer_device, p->block_id, sector, pi); |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 2344 | if (!peer_req) { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2345 | put_ldev(device); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 2346 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2347 | } |
| 2348 | |
Andreas Gruenbacher | a8cd15b | 2011-08-25 15:49:40 +0200 | [diff] [blame] | 2349 | peer_req->w.cb = e_end_block; |
Lars Ellenberg | 21ae5d7 | 2014-05-05 23:42:24 +0200 | [diff] [blame] | 2350 | peer_req->submit_jif = jiffies; |
| 2351 | peer_req->flags |= EE_APPLICATION; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2352 | |
Lars Ellenberg | 688593c | 2010-11-17 22:25:03 +0100 | [diff] [blame] | 2353 | dp_flags = be32_to_cpu(p->dp_flags); |
Mike Christie | bb3cc85 | 2016-06-05 14:32:06 -0500 | [diff] [blame^] | 2354 | op = wire_flags_to_bio_op(dp_flags); |
| 2355 | op_flags = wire_flags_to_bio_flags(dp_flags); |
Lars Ellenberg | a0fb3c4 | 2014-04-28 18:43:23 +0200 | [diff] [blame] | 2356 | if (pi->cmd == P_TRIM) { |
| 2357 | struct request_queue *q = bdev_get_queue(device->ldev->backing_bdev); |
| 2358 | peer_req->flags |= EE_IS_TRIM; |
| 2359 | if (!blk_queue_discard(q)) |
| 2360 | peer_req->flags |= EE_IS_TRIM_USE_ZEROOUT; |
| 2361 | D_ASSERT(peer_device, peer_req->i.size > 0); |
Mike Christie | bb3cc85 | 2016-06-05 14:32:06 -0500 | [diff] [blame^] | 2362 | D_ASSERT(peer_device, op == REQ_OP_DISCARD); |
Lars Ellenberg | a0fb3c4 | 2014-04-28 18:43:23 +0200 | [diff] [blame] | 2363 | D_ASSERT(peer_device, peer_req->pages == NULL); |
| 2364 | } else if (peer_req->pages == NULL) { |
Andreas Gruenbacher | 0b0ba1e | 2011-06-27 16:23:33 +0200 | [diff] [blame] | 2365 | D_ASSERT(device, peer_req->i.size == 0); |
| 2366 | D_ASSERT(device, dp_flags & DP_FLUSH); |
Lars Ellenberg | a73ff32 | 2012-06-25 19:15:38 +0200 | [diff] [blame] | 2367 | } |
Lars Ellenberg | 688593c | 2010-11-17 22:25:03 +0100 | [diff] [blame] | 2368 | |
| 2369 | if (dp_flags & DP_MAY_SET_IN_SYNC) |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 2370 | peer_req->flags |= EE_MAY_SET_IN_SYNC; |
Lars Ellenberg | 688593c | 2010-11-17 22:25:03 +0100 | [diff] [blame] | 2371 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 2372 | spin_lock(&connection->epoch_lock); |
| 2373 | peer_req->epoch = connection->current_epoch; |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 2374 | atomic_inc(&peer_req->epoch->epoch_size); |
| 2375 | atomic_inc(&peer_req->epoch->active); |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 2376 | spin_unlock(&connection->epoch_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2377 | |
Philipp Reisner | 302bdea | 2011-04-21 11:36:49 +0200 | [diff] [blame] | 2378 | rcu_read_lock(); |
Lars Ellenberg | 21ae5d7 | 2014-05-05 23:42:24 +0200 | [diff] [blame] | 2379 | nc = rcu_dereference(peer_device->connection->net_conf); |
| 2380 | tp = nc->two_primaries; |
| 2381 | if (peer_device->connection->agreed_pro_version < 100) { |
| 2382 | switch (nc->wire_protocol) { |
| 2383 | case DRBD_PROT_C: |
| 2384 | dp_flags |= DP_SEND_WRITE_ACK; |
| 2385 | break; |
| 2386 | case DRBD_PROT_B: |
| 2387 | dp_flags |= DP_SEND_RECEIVE_ACK; |
| 2388 | break; |
| 2389 | } |
| 2390 | } |
Philipp Reisner | 302bdea | 2011-04-21 11:36:49 +0200 | [diff] [blame] | 2391 | rcu_read_unlock(); |
Lars Ellenberg | 21ae5d7 | 2014-05-05 23:42:24 +0200 | [diff] [blame] | 2392 | |
| 2393 | if (dp_flags & DP_SEND_WRITE_ACK) { |
| 2394 | peer_req->flags |= EE_SEND_WRITE_ACK; |
| 2395 | inc_unacked(device); |
| 2396 | /* corresponding dec_unacked() in e_end_block() |
| 2397 | * respective _drbd_clear_done_ee */ |
| 2398 | } |
| 2399 | |
| 2400 | if (dp_flags & DP_SEND_RECEIVE_ACK) { |
| 2401 | /* I really don't like it that the receiver thread |
| 2402 | * sends on the msock, but anyways */ |
Andreas Gruenbacher | 5dd2ca1 | 2014-08-11 16:59:23 +0200 | [diff] [blame] | 2403 | drbd_send_ack(peer_device, P_RECV_ACK, peer_req); |
Lars Ellenberg | 21ae5d7 | 2014-05-05 23:42:24 +0200 | [diff] [blame] | 2404 | } |
| 2405 | |
Philipp Reisner | 302bdea | 2011-04-21 11:36:49 +0200 | [diff] [blame] | 2406 | if (tp) { |
Lars Ellenberg | 21ae5d7 | 2014-05-05 23:42:24 +0200 | [diff] [blame] | 2407 | /* two primaries implies protocol C */ |
| 2408 | D_ASSERT(device, dp_flags & DP_SEND_WRITE_ACK); |
Philipp Reisner | 302bdea | 2011-04-21 11:36:49 +0200 | [diff] [blame] | 2409 | peer_req->flags |= EE_IN_INTERVAL_TREE; |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 2410 | err = wait_for_and_update_peer_seq(peer_device, peer_seq); |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2411 | if (err) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2412 | goto out_interrupted; |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 2413 | spin_lock_irq(&device->resource->req_lock); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2414 | err = handle_write_conflicts(device, peer_req); |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2415 | if (err) { |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 2416 | spin_unlock_irq(&device->resource->req_lock); |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2417 | if (err == -ENOENT) { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2418 | put_ldev(device); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 2419 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2420 | } |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 2421 | goto out_interrupted; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2422 | } |
Philipp Reisner | b874d23 | 2013-10-23 10:59:16 +0200 | [diff] [blame] | 2423 | } else { |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 2424 | update_peer_seq(peer_device, peer_seq); |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 2425 | spin_lock_irq(&device->resource->req_lock); |
Philipp Reisner | b874d23 | 2013-10-23 10:59:16 +0200 | [diff] [blame] | 2426 | } |
Lars Ellenberg | a0fb3c4 | 2014-04-28 18:43:23 +0200 | [diff] [blame] | 2427 | /* if we use the zeroout fallback code, we process synchronously |
| 2428 | * and we wait for all pending requests, respectively wait for |
| 2429 | * active_ee to become empty in drbd_submit_peer_request(); |
| 2430 | * better not add ourselves here. */ |
| 2431 | if ((peer_req->flags & EE_IS_TRIM_USE_ZEROOUT) == 0) |
Lars Ellenberg | b9ed708 | 2014-04-23 12:15:35 +0200 | [diff] [blame] | 2432 | list_add_tail(&peer_req->w.list, &device->active_ee); |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 2433 | spin_unlock_irq(&device->resource->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2434 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2435 | if (device->state.conn == C_SYNC_TARGET) |
| 2436 | wait_event(device->ee_wait, !overlapping_resync_write(device, peer_req)); |
Philipp Reisner | b6a370ba | 2012-02-19 01:27:53 +0100 | [diff] [blame] | 2437 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2438 | if (device->state.pdsk < D_INCONSISTENT) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2439 | /* In case we have the only disk of the cluster, */ |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2440 | drbd_set_out_of_sync(device, peer_req->i.sector, peer_req->i.size); |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 2441 | peer_req->flags &= ~EE_MAY_SET_IN_SYNC; |
Lars Ellenberg | 4dd726f | 2014-02-11 11:15:36 +0100 | [diff] [blame] | 2442 | drbd_al_begin_io(device, &peer_req->i); |
Lars Ellenberg | 21ae5d7 | 2014-05-05 23:42:24 +0200 | [diff] [blame] | 2443 | peer_req->flags |= EE_CALL_AL_COMPLETE_IO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2444 | } |
| 2445 | |
Mike Christie | bb3cc85 | 2016-06-05 14:32:06 -0500 | [diff] [blame^] | 2446 | err = drbd_submit_peer_request(device, peer_req, op, op_flags, |
| 2447 | DRBD_FAULT_DT_WR); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 2448 | if (!err) |
| 2449 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2450 | |
Lars Ellenberg | 10f6d992 | 2011-01-24 14:47:09 +0100 | [diff] [blame] | 2451 | /* don't care for the reason here */ |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 2452 | drbd_err(device, "submit failed, triggering re-connect\n"); |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 2453 | spin_lock_irq(&device->resource->req_lock); |
Andreas Gruenbacher | a8cd15b | 2011-08-25 15:49:40 +0200 | [diff] [blame] | 2454 | list_del(&peer_req->w.list); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2455 | drbd_remove_epoch_entry_interval(device, peer_req); |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 2456 | spin_unlock_irq(&device->resource->req_lock); |
Lars Ellenberg | 21ae5d7 | 2014-05-05 23:42:24 +0200 | [diff] [blame] | 2457 | if (peer_req->flags & EE_CALL_AL_COMPLETE_IO) { |
| 2458 | peer_req->flags &= ~EE_CALL_AL_COMPLETE_IO; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2459 | drbd_al_complete_io(device, &peer_req->i); |
Lars Ellenberg | 21ae5d7 | 2014-05-05 23:42:24 +0200 | [diff] [blame] | 2460 | } |
Lars Ellenberg | 22cc37a | 2010-09-14 20:40:41 +0200 | [diff] [blame] | 2461 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2462 | out_interrupted: |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 2463 | drbd_may_finish_epoch(connection, peer_req->epoch, EV_PUT + EV_CLEANUP); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2464 | put_ldev(device); |
| 2465 | drbd_free_peer_req(device, peer_req); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 2466 | return err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2467 | } |
| 2468 | |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 2469 | /* We may throttle resync, if the lower device seems to be busy, |
| 2470 | * and current sync rate is above c_min_rate. |
| 2471 | * |
| 2472 | * To decide whether or not the lower device is busy, we use a scheme similar |
| 2473 | * to MD RAID is_mddev_idle(): if the partition stats reveal "significant" |
| 2474 | * (more than 64 sectors) of activity we cannot account for with our own resync |
| 2475 | * activity, it obviously is "busy". |
| 2476 | * |
| 2477 | * The current sync rate used here uses only the most recent two step marks, |
| 2478 | * to have a short time average so we can react faster. |
| 2479 | */ |
Lars Ellenberg | ad3fee7 | 2013-12-20 11:22:13 +0100 | [diff] [blame] | 2480 | bool drbd_rs_should_slow_down(struct drbd_device *device, sector_t sector, |
| 2481 | bool throttle_if_app_is_waiting) |
Lars Ellenberg | e829987 | 2014-04-28 18:43:19 +0200 | [diff] [blame] | 2482 | { |
| 2483 | struct lc_element *tmp; |
Lars Ellenberg | ad3fee7 | 2013-12-20 11:22:13 +0100 | [diff] [blame] | 2484 | bool throttle = drbd_rs_c_min_rate_throttle(device); |
Lars Ellenberg | e829987 | 2014-04-28 18:43:19 +0200 | [diff] [blame] | 2485 | |
Lars Ellenberg | ad3fee7 | 2013-12-20 11:22:13 +0100 | [diff] [blame] | 2486 | if (!throttle || throttle_if_app_is_waiting) |
| 2487 | return throttle; |
Lars Ellenberg | e829987 | 2014-04-28 18:43:19 +0200 | [diff] [blame] | 2488 | |
| 2489 | spin_lock_irq(&device->al_lock); |
| 2490 | tmp = lc_find(device->resync, BM_SECT_TO_EXT(sector)); |
| 2491 | if (tmp) { |
| 2492 | struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce); |
| 2493 | if (test_bit(BME_PRIORITY, &bm_ext->flags)) |
| 2494 | throttle = false; |
Lars Ellenberg | ad3fee7 | 2013-12-20 11:22:13 +0100 | [diff] [blame] | 2495 | /* Do not slow down if app IO is already waiting for this extent, |
| 2496 | * and our progress is necessary for application IO to complete. */ |
Lars Ellenberg | e829987 | 2014-04-28 18:43:19 +0200 | [diff] [blame] | 2497 | } |
| 2498 | spin_unlock_irq(&device->al_lock); |
| 2499 | |
| 2500 | return throttle; |
| 2501 | } |
| 2502 | |
| 2503 | bool drbd_rs_c_min_rate_throttle(struct drbd_device *device) |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 2504 | { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2505 | struct gendisk *disk = device->ldev->backing_bdev->bd_contains->bd_disk; |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 2506 | unsigned long db, dt, dbdt; |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 2507 | unsigned int c_min_rate; |
Lars Ellenberg | e829987 | 2014-04-28 18:43:19 +0200 | [diff] [blame] | 2508 | int curr_events; |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 2509 | |
| 2510 | rcu_read_lock(); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2511 | c_min_rate = rcu_dereference(device->ldev->disk_conf)->c_min_rate; |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 2512 | rcu_read_unlock(); |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 2513 | |
| 2514 | /* feature disabled? */ |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 2515 | if (c_min_rate == 0) |
Lars Ellenberg | e829987 | 2014-04-28 18:43:19 +0200 | [diff] [blame] | 2516 | return false; |
Philipp Reisner | e3555d8 | 2010-11-07 15:56:29 +0100 | [diff] [blame] | 2517 | |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 2518 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 2519 | (int)part_stat_read(&disk->part0, sectors[1]) - |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2520 | atomic_read(&device->rs_sect_ev); |
Lars Ellenberg | ad3fee7 | 2013-12-20 11:22:13 +0100 | [diff] [blame] | 2521 | |
| 2522 | if (atomic_read(&device->ap_actlog_cnt) |
Lars Ellenberg | ff8bd88 | 2014-11-10 17:21:12 +0100 | [diff] [blame] | 2523 | || curr_events - device->rs_last_events > 64) { |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 2524 | unsigned long rs_left; |
| 2525 | int i; |
| 2526 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2527 | device->rs_last_events = curr_events; |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 2528 | |
| 2529 | /* sync speed average over the last 2*DRBD_SYNC_MARK_STEP, |
| 2530 | * approx. */ |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2531 | i = (device->rs_last_mark + DRBD_SYNC_MARKS-1) % DRBD_SYNC_MARKS; |
Lars Ellenberg | 2649f08 | 2010-11-05 10:05:47 +0100 | [diff] [blame] | 2532 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2533 | if (device->state.conn == C_VERIFY_S || device->state.conn == C_VERIFY_T) |
| 2534 | rs_left = device->ov_left; |
Lars Ellenberg | 2649f08 | 2010-11-05 10:05:47 +0100 | [diff] [blame] | 2535 | else |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2536 | rs_left = drbd_bm_total_weight(device) - device->rs_failed; |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 2537 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2538 | dt = ((long)jiffies - (long)device->rs_mark_time[i]) / HZ; |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 2539 | if (!dt) |
| 2540 | dt++; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2541 | db = device->rs_mark_left[i] - rs_left; |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 2542 | dbdt = Bit2KB(db/dt); |
| 2543 | |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 2544 | if (dbdt > c_min_rate) |
Lars Ellenberg | e829987 | 2014-04-28 18:43:19 +0200 | [diff] [blame] | 2545 | return true; |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 2546 | } |
Lars Ellenberg | e829987 | 2014-04-28 18:43:19 +0200 | [diff] [blame] | 2547 | return false; |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 2548 | } |
| 2549 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 2550 | static int receive_DataRequest(struct drbd_connection *connection, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2551 | { |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 2552 | struct drbd_peer_device *peer_device; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2553 | struct drbd_device *device; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2554 | sector_t sector; |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 2555 | sector_t capacity; |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 2556 | struct drbd_peer_request *peer_req; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2557 | struct digest_info *di = NULL; |
Philipp Reisner | b18b37b | 2010-10-13 15:32:44 +0200 | [diff] [blame] | 2558 | int size, verb; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2559 | unsigned int fault_type; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 2560 | struct p_block_req *p = pi->data; |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 2561 | |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 2562 | peer_device = conn_peer_device(connection, pi->vnr); |
| 2563 | if (!peer_device) |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 2564 | return -EIO; |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 2565 | device = peer_device->device; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2566 | capacity = drbd_get_capacity(device->this_bdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2567 | |
| 2568 | sector = be64_to_cpu(p->sector); |
| 2569 | size = be32_to_cpu(p->blksize); |
| 2570 | |
Andreas Gruenbacher | c670a39 | 2011-02-21 12:41:39 +0100 | [diff] [blame] | 2571 | if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 2572 | drbd_err(device, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2573 | (unsigned long long)sector, size); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 2574 | return -EINVAL; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2575 | } |
| 2576 | if (sector + (size>>9) > capacity) { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 2577 | drbd_err(device, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2578 | (unsigned long long)sector, size); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 2579 | return -EINVAL; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2580 | } |
| 2581 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2582 | if (!get_ldev_if_state(device, D_UP_TO_DATE)) { |
Philipp Reisner | b18b37b | 2010-10-13 15:32:44 +0200 | [diff] [blame] | 2583 | verb = 1; |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 2584 | switch (pi->cmd) { |
Philipp Reisner | b18b37b | 2010-10-13 15:32:44 +0200 | [diff] [blame] | 2585 | case P_DATA_REQUEST: |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 2586 | drbd_send_ack_rp(peer_device, P_NEG_DREPLY, p); |
Philipp Reisner | b18b37b | 2010-10-13 15:32:44 +0200 | [diff] [blame] | 2587 | break; |
| 2588 | case P_RS_DATA_REQUEST: |
| 2589 | case P_CSUM_RS_REQUEST: |
| 2590 | case P_OV_REQUEST: |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 2591 | drbd_send_ack_rp(peer_device, P_NEG_RS_DREPLY , p); |
Philipp Reisner | b18b37b | 2010-10-13 15:32:44 +0200 | [diff] [blame] | 2592 | break; |
| 2593 | case P_OV_REPLY: |
| 2594 | verb = 0; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2595 | dec_rs_pending(device); |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 2596 | drbd_send_ack_ex(peer_device, P_OV_RESULT, sector, size, ID_IN_SYNC); |
Philipp Reisner | b18b37b | 2010-10-13 15:32:44 +0200 | [diff] [blame] | 2597 | break; |
| 2598 | default: |
Andreas Gruenbacher | 49ba9b1 | 2011-03-25 00:35:45 +0100 | [diff] [blame] | 2599 | BUG(); |
Philipp Reisner | b18b37b | 2010-10-13 15:32:44 +0200 | [diff] [blame] | 2600 | } |
| 2601 | if (verb && __ratelimit(&drbd_ratelimit_state)) |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 2602 | drbd_err(device, "Can not satisfy peer's read request, " |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2603 | "no local data.\n"); |
Philipp Reisner | b18b37b | 2010-10-13 15:32:44 +0200 | [diff] [blame] | 2604 | |
Lars Ellenberg | a821cc4 | 2010-09-06 12:31:37 +0200 | [diff] [blame] | 2605 | /* drain possibly payload */ |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 2606 | return drbd_drain_block(peer_device, pi->size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2607 | } |
| 2608 | |
| 2609 | /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD |
| 2610 | * "criss-cross" setup, that might cause write-out on some other DRBD, |
| 2611 | * which in turn might block on the other node at this very place. */ |
Lars Ellenberg | a0fb3c4 | 2014-04-28 18:43:23 +0200 | [diff] [blame] | 2612 | peer_req = drbd_alloc_peer_req(peer_device, p->block_id, sector, size, |
| 2613 | true /* has real payload */, GFP_NOIO); |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 2614 | if (!peer_req) { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2615 | put_ldev(device); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 2616 | return -ENOMEM; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2617 | } |
| 2618 | |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 2619 | switch (pi->cmd) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2620 | case P_DATA_REQUEST: |
Andreas Gruenbacher | a8cd15b | 2011-08-25 15:49:40 +0200 | [diff] [blame] | 2621 | peer_req->w.cb = w_e_end_data_req; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2622 | fault_type = DRBD_FAULT_DT_RD; |
Lars Ellenberg | 80a40e4 | 2010-08-11 23:28:00 +0200 | [diff] [blame] | 2623 | /* application IO, don't drbd_rs_begin_io */ |
Lars Ellenberg | 21ae5d7 | 2014-05-05 23:42:24 +0200 | [diff] [blame] | 2624 | peer_req->flags |= EE_APPLICATION; |
Lars Ellenberg | 80a40e4 | 2010-08-11 23:28:00 +0200 | [diff] [blame] | 2625 | goto submit; |
| 2626 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2627 | case P_RS_DATA_REQUEST: |
Andreas Gruenbacher | a8cd15b | 2011-08-25 15:49:40 +0200 | [diff] [blame] | 2628 | peer_req->w.cb = w_e_end_rsdata_req; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2629 | fault_type = DRBD_FAULT_RS_RD; |
Lars Ellenberg | 5f9915b | 2010-11-09 14:15:24 +0100 | [diff] [blame] | 2630 | /* used in the sector offset progress display */ |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2631 | device->bm_resync_fo = BM_SECT_TO_BIT(sector); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2632 | break; |
| 2633 | |
| 2634 | case P_OV_REPLY: |
| 2635 | case P_CSUM_RS_REQUEST: |
| 2636 | fault_type = DRBD_FAULT_RS_RD; |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 2637 | di = kmalloc(sizeof(*di) + pi->size, GFP_NOIO); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2638 | if (!di) |
| 2639 | goto out_free_e; |
| 2640 | |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 2641 | di->digest_size = pi->size; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2642 | di->digest = (((char *)di)+sizeof(struct digest_info)); |
| 2643 | |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 2644 | peer_req->digest = di; |
| 2645 | peer_req->flags |= EE_HAS_DIGEST; |
Lars Ellenberg | c36c3ce | 2010-08-11 20:42:55 +0200 | [diff] [blame] | 2646 | |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 2647 | if (drbd_recv_all(peer_device->connection, di->digest, pi->size)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2648 | goto out_free_e; |
| 2649 | |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 2650 | if (pi->cmd == P_CSUM_RS_REQUEST) { |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 2651 | D_ASSERT(device, peer_device->connection->agreed_pro_version >= 89); |
Andreas Gruenbacher | a8cd15b | 2011-08-25 15:49:40 +0200 | [diff] [blame] | 2652 | peer_req->w.cb = w_e_end_csum_rs_req; |
Lars Ellenberg | 5f9915b | 2010-11-09 14:15:24 +0100 | [diff] [blame] | 2653 | /* used in the sector offset progress display */ |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2654 | device->bm_resync_fo = BM_SECT_TO_BIT(sector); |
Lars Ellenberg | aaaba34 | 2014-03-18 12:30:09 +0100 | [diff] [blame] | 2655 | /* remember to report stats in drbd_resync_finished */ |
| 2656 | device->use_csums = true; |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 2657 | } else if (pi->cmd == P_OV_REPLY) { |
Lars Ellenberg | 2649f08 | 2010-11-05 10:05:47 +0100 | [diff] [blame] | 2658 | /* track progress, we may need to throttle */ |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2659 | atomic_add(size >> 9, &device->rs_sect_in); |
Andreas Gruenbacher | a8cd15b | 2011-08-25 15:49:40 +0200 | [diff] [blame] | 2660 | peer_req->w.cb = w_e_end_ov_reply; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2661 | dec_rs_pending(device); |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 2662 | /* drbd_rs_begin_io done when we sent this request, |
| 2663 | * but accounting still needs to be done. */ |
| 2664 | goto submit_for_resync; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2665 | } |
| 2666 | break; |
| 2667 | |
| 2668 | case P_OV_REQUEST: |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2669 | if (device->ov_start_sector == ~(sector_t)0 && |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 2670 | peer_device->connection->agreed_pro_version >= 90) { |
Lars Ellenberg | de228bb | 2010-11-05 09:43:15 +0100 | [diff] [blame] | 2671 | unsigned long now = jiffies; |
| 2672 | int i; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2673 | device->ov_start_sector = sector; |
| 2674 | device->ov_position = sector; |
| 2675 | device->ov_left = drbd_bm_bits(device) - BM_SECT_TO_BIT(sector); |
| 2676 | device->rs_total = device->ov_left; |
Lars Ellenberg | de228bb | 2010-11-05 09:43:15 +0100 | [diff] [blame] | 2677 | for (i = 0; i < DRBD_SYNC_MARKS; i++) { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2678 | device->rs_mark_left[i] = device->ov_left; |
| 2679 | device->rs_mark_time[i] = now; |
Lars Ellenberg | de228bb | 2010-11-05 09:43:15 +0100 | [diff] [blame] | 2680 | } |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 2681 | drbd_info(device, "Online Verify start sector: %llu\n", |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2682 | (unsigned long long)sector); |
| 2683 | } |
Andreas Gruenbacher | a8cd15b | 2011-08-25 15:49:40 +0200 | [diff] [blame] | 2684 | peer_req->w.cb = w_e_end_ov_req; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2685 | fault_type = DRBD_FAULT_RS_RD; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2686 | break; |
| 2687 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2688 | default: |
Andreas Gruenbacher | 49ba9b1 | 2011-03-25 00:35:45 +0100 | [diff] [blame] | 2689 | BUG(); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2690 | } |
| 2691 | |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 2692 | /* Throttle, drbd_rs_begin_io and submit should become asynchronous |
| 2693 | * wrt the receiver, but it is not as straightforward as it may seem. |
| 2694 | * Various places in the resync start and stop logic assume resync |
| 2695 | * requests are processed in order, requeuing this on the worker thread |
| 2696 | * introduces a bunch of new code for synchronization between threads. |
| 2697 | * |
| 2698 | * Unlimited throttling before drbd_rs_begin_io may stall the resync |
| 2699 | * "forever", throttling after drbd_rs_begin_io will lock that extent |
| 2700 | * for application writes for the same time. For now, just throttle |
| 2701 | * here, where the rest of the code expects the receiver to sleep for |
| 2702 | * a while, anyways. |
| 2703 | */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2704 | |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 2705 | /* Throttle before drbd_rs_begin_io, as that locks out application IO; |
| 2706 | * this defers syncer requests for some time, before letting at least |
| 2707 | * on request through. The resync controller on the receiving side |
| 2708 | * will adapt to the incoming rate accordingly. |
| 2709 | * |
| 2710 | * We cannot throttle here if remote is Primary/SyncTarget: |
| 2711 | * we would also throttle its application reads. |
| 2712 | * In that case, throttling is done on the SyncTarget only. |
| 2713 | */ |
Lars Ellenberg | c5a2c15 | 2014-05-08 10:08:05 +0200 | [diff] [blame] | 2714 | |
| 2715 | /* Even though this may be a resync request, we do add to "read_ee"; |
| 2716 | * "sync_ee" is only used for resync WRITEs. |
| 2717 | * Add to list early, so debugfs can find this request |
| 2718 | * even if we have to sleep below. */ |
| 2719 | spin_lock_irq(&device->resource->req_lock); |
| 2720 | list_add_tail(&peer_req->w.list, &device->read_ee); |
| 2721 | spin_unlock_irq(&device->resource->req_lock); |
| 2722 | |
Lars Ellenberg | 944410e | 2014-05-06 15:02:05 +0200 | [diff] [blame] | 2723 | update_receiver_timing_details(connection, drbd_rs_should_slow_down); |
Lars Ellenberg | ad3fee7 | 2013-12-20 11:22:13 +0100 | [diff] [blame] | 2724 | if (device->state.peer != R_PRIMARY |
| 2725 | && drbd_rs_should_slow_down(device, sector, false)) |
Philipp Reisner | e3555d8 | 2010-11-07 15:56:29 +0100 | [diff] [blame] | 2726 | schedule_timeout_uninterruptible(HZ/10); |
Lars Ellenberg | 944410e | 2014-05-06 15:02:05 +0200 | [diff] [blame] | 2727 | update_receiver_timing_details(connection, drbd_rs_begin_io); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2728 | if (drbd_rs_begin_io(device, sector)) |
Lars Ellenberg | 80a40e4 | 2010-08-11 23:28:00 +0200 | [diff] [blame] | 2729 | goto out_free_e; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2730 | |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 2731 | submit_for_resync: |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2732 | atomic_add(size >> 9, &device->rs_sect_ev); |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 2733 | |
Lars Ellenberg | 80a40e4 | 2010-08-11 23:28:00 +0200 | [diff] [blame] | 2734 | submit: |
Lars Ellenberg | 944410e | 2014-05-06 15:02:05 +0200 | [diff] [blame] | 2735 | update_receiver_timing_details(connection, drbd_submit_peer_request); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2736 | inc_unacked(device); |
Mike Christie | bb3cc85 | 2016-06-05 14:32:06 -0500 | [diff] [blame^] | 2737 | if (drbd_submit_peer_request(device, peer_req, REQ_OP_READ, 0, |
| 2738 | fault_type) == 0) |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 2739 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2740 | |
Lars Ellenberg | 10f6d992 | 2011-01-24 14:47:09 +0100 | [diff] [blame] | 2741 | /* don't care for the reason here */ |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 2742 | drbd_err(device, "submit failed, triggering re-connect\n"); |
Lars Ellenberg | c5a2c15 | 2014-05-08 10:08:05 +0200 | [diff] [blame] | 2743 | |
| 2744 | out_free_e: |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 2745 | spin_lock_irq(&device->resource->req_lock); |
Andreas Gruenbacher | a8cd15b | 2011-08-25 15:49:40 +0200 | [diff] [blame] | 2746 | list_del(&peer_req->w.list); |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 2747 | spin_unlock_irq(&device->resource->req_lock); |
Lars Ellenberg | 22cc37a | 2010-09-14 20:40:41 +0200 | [diff] [blame] | 2748 | /* no drbd_rs_complete_io(), we are dropping the connection anyways */ |
| 2749 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2750 | put_ldev(device); |
| 2751 | drbd_free_peer_req(device, peer_req); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 2752 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2753 | } |
| 2754 | |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 2755 | /** |
| 2756 | * drbd_asb_recover_0p - Recover after split-brain with no remaining primaries |
| 2757 | */ |
| 2758 | static int drbd_asb_recover_0p(struct drbd_peer_device *peer_device) __must_hold(local) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2759 | { |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 2760 | struct drbd_device *device = peer_device->device; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2761 | int self, peer, rv = -100; |
| 2762 | unsigned long ch_self, ch_peer; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 2763 | enum drbd_after_sb_p after_sb_0p; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2764 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2765 | self = device->ldev->md.uuid[UI_BITMAP] & 1; |
| 2766 | peer = device->p_uuid[UI_BITMAP] & 1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2767 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2768 | ch_peer = device->p_uuid[UI_SIZE]; |
| 2769 | ch_self = device->comm_bm_set; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2770 | |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 2771 | rcu_read_lock(); |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 2772 | after_sb_0p = rcu_dereference(peer_device->connection->net_conf)->after_sb_0p; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 2773 | rcu_read_unlock(); |
| 2774 | switch (after_sb_0p) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2775 | case ASB_CONSENSUS: |
| 2776 | case ASB_DISCARD_SECONDARY: |
| 2777 | case ASB_CALL_HELPER: |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 2778 | case ASB_VIOLENTLY: |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 2779 | drbd_err(device, "Configuration error.\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2780 | break; |
| 2781 | case ASB_DISCONNECT: |
| 2782 | break; |
| 2783 | case ASB_DISCARD_YOUNGER_PRI: |
| 2784 | if (self == 0 && peer == 1) { |
| 2785 | rv = -1; |
| 2786 | break; |
| 2787 | } |
| 2788 | if (self == 1 && peer == 0) { |
| 2789 | rv = 1; |
| 2790 | break; |
| 2791 | } |
| 2792 | /* Else fall through to one of the other strategies... */ |
| 2793 | case ASB_DISCARD_OLDER_PRI: |
| 2794 | if (self == 0 && peer == 1) { |
| 2795 | rv = 1; |
| 2796 | break; |
| 2797 | } |
| 2798 | if (self == 1 && peer == 0) { |
| 2799 | rv = -1; |
| 2800 | break; |
| 2801 | } |
| 2802 | /* Else fall through to one of the other strategies... */ |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 2803 | drbd_warn(device, "Discard younger/older primary did not find a decision\n" |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2804 | "Using discard-least-changes instead\n"); |
| 2805 | case ASB_DISCARD_ZERO_CHG: |
| 2806 | if (ch_peer == 0 && ch_self == 0) { |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 2807 | rv = test_bit(RESOLVE_CONFLICTS, &peer_device->connection->flags) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2808 | ? -1 : 1; |
| 2809 | break; |
| 2810 | } else { |
| 2811 | if (ch_peer == 0) { rv = 1; break; } |
| 2812 | if (ch_self == 0) { rv = -1; break; } |
| 2813 | } |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 2814 | if (after_sb_0p == ASB_DISCARD_ZERO_CHG) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2815 | break; |
| 2816 | case ASB_DISCARD_LEAST_CHG: |
| 2817 | if (ch_self < ch_peer) |
| 2818 | rv = -1; |
| 2819 | else if (ch_self > ch_peer) |
| 2820 | rv = 1; |
| 2821 | else /* ( ch_self == ch_peer ) */ |
| 2822 | /* Well, then use something else. */ |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 2823 | rv = test_bit(RESOLVE_CONFLICTS, &peer_device->connection->flags) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2824 | ? -1 : 1; |
| 2825 | break; |
| 2826 | case ASB_DISCARD_LOCAL: |
| 2827 | rv = -1; |
| 2828 | break; |
| 2829 | case ASB_DISCARD_REMOTE: |
| 2830 | rv = 1; |
| 2831 | } |
| 2832 | |
| 2833 | return rv; |
| 2834 | } |
| 2835 | |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 2836 | /** |
| 2837 | * drbd_asb_recover_1p - Recover after split-brain with one remaining primary |
| 2838 | */ |
| 2839 | static int drbd_asb_recover_1p(struct drbd_peer_device *peer_device) __must_hold(local) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2840 | { |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 2841 | struct drbd_device *device = peer_device->device; |
Andreas Gruenbacher | 6184ea2 | 2010-12-09 14:23:27 +0100 | [diff] [blame] | 2842 | int hg, rv = -100; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 2843 | enum drbd_after_sb_p after_sb_1p; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2844 | |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 2845 | rcu_read_lock(); |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 2846 | after_sb_1p = rcu_dereference(peer_device->connection->net_conf)->after_sb_1p; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 2847 | rcu_read_unlock(); |
| 2848 | switch (after_sb_1p) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2849 | case ASB_DISCARD_YOUNGER_PRI: |
| 2850 | case ASB_DISCARD_OLDER_PRI: |
| 2851 | case ASB_DISCARD_LEAST_CHG: |
| 2852 | case ASB_DISCARD_LOCAL: |
| 2853 | case ASB_DISCARD_REMOTE: |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 2854 | case ASB_DISCARD_ZERO_CHG: |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 2855 | drbd_err(device, "Configuration error.\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2856 | break; |
| 2857 | case ASB_DISCONNECT: |
| 2858 | break; |
| 2859 | case ASB_CONSENSUS: |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 2860 | hg = drbd_asb_recover_0p(peer_device); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2861 | if (hg == -1 && device->state.role == R_SECONDARY) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2862 | rv = hg; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2863 | if (hg == 1 && device->state.role == R_PRIMARY) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2864 | rv = hg; |
| 2865 | break; |
| 2866 | case ASB_VIOLENTLY: |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 2867 | rv = drbd_asb_recover_0p(peer_device); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2868 | break; |
| 2869 | case ASB_DISCARD_SECONDARY: |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2870 | return device->state.role == R_PRIMARY ? 1 : -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2871 | case ASB_CALL_HELPER: |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 2872 | hg = drbd_asb_recover_0p(peer_device); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2873 | if (hg == -1 && device->state.role == R_PRIMARY) { |
Andreas Gruenbacher | bb43794 | 2010-12-09 14:02:35 +0100 | [diff] [blame] | 2874 | enum drbd_state_rv rv2; |
| 2875 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2876 | /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE, |
| 2877 | * we might be here in C_WF_REPORT_PARAMS which is transient. |
| 2878 | * we do not need to wait for the after state change work either. */ |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2879 | rv2 = drbd_change_state(device, CS_VERBOSE, NS(role, R_SECONDARY)); |
Andreas Gruenbacher | bb43794 | 2010-12-09 14:02:35 +0100 | [diff] [blame] | 2880 | if (rv2 != SS_SUCCESS) { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2881 | drbd_khelper(device, "pri-lost-after-sb"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2882 | } else { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 2883 | drbd_warn(device, "Successfully gave up primary role.\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2884 | rv = hg; |
| 2885 | } |
| 2886 | } else |
| 2887 | rv = hg; |
| 2888 | } |
| 2889 | |
| 2890 | return rv; |
| 2891 | } |
| 2892 | |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 2893 | /** |
| 2894 | * drbd_asb_recover_2p - Recover after split-brain with two remaining primaries |
| 2895 | */ |
| 2896 | static int drbd_asb_recover_2p(struct drbd_peer_device *peer_device) __must_hold(local) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2897 | { |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 2898 | struct drbd_device *device = peer_device->device; |
Andreas Gruenbacher | 6184ea2 | 2010-12-09 14:23:27 +0100 | [diff] [blame] | 2899 | int hg, rv = -100; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 2900 | enum drbd_after_sb_p after_sb_2p; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2901 | |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 2902 | rcu_read_lock(); |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 2903 | after_sb_2p = rcu_dereference(peer_device->connection->net_conf)->after_sb_2p; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 2904 | rcu_read_unlock(); |
| 2905 | switch (after_sb_2p) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2906 | case ASB_DISCARD_YOUNGER_PRI: |
| 2907 | case ASB_DISCARD_OLDER_PRI: |
| 2908 | case ASB_DISCARD_LEAST_CHG: |
| 2909 | case ASB_DISCARD_LOCAL: |
| 2910 | case ASB_DISCARD_REMOTE: |
| 2911 | case ASB_CONSENSUS: |
| 2912 | case ASB_DISCARD_SECONDARY: |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 2913 | case ASB_DISCARD_ZERO_CHG: |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 2914 | drbd_err(device, "Configuration error.\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2915 | break; |
| 2916 | case ASB_VIOLENTLY: |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 2917 | rv = drbd_asb_recover_0p(peer_device); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2918 | break; |
| 2919 | case ASB_DISCONNECT: |
| 2920 | break; |
| 2921 | case ASB_CALL_HELPER: |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 2922 | hg = drbd_asb_recover_0p(peer_device); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2923 | if (hg == -1) { |
Andreas Gruenbacher | bb43794 | 2010-12-09 14:02:35 +0100 | [diff] [blame] | 2924 | enum drbd_state_rv rv2; |
| 2925 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2926 | /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE, |
| 2927 | * we might be here in C_WF_REPORT_PARAMS which is transient. |
| 2928 | * we do not need to wait for the after state change work either. */ |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2929 | rv2 = drbd_change_state(device, CS_VERBOSE, NS(role, R_SECONDARY)); |
Andreas Gruenbacher | bb43794 | 2010-12-09 14:02:35 +0100 | [diff] [blame] | 2930 | if (rv2 != SS_SUCCESS) { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2931 | drbd_khelper(device, "pri-lost-after-sb"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2932 | } else { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 2933 | drbd_warn(device, "Successfully gave up primary role.\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2934 | rv = hg; |
| 2935 | } |
| 2936 | } else |
| 2937 | rv = hg; |
| 2938 | } |
| 2939 | |
| 2940 | return rv; |
| 2941 | } |
| 2942 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2943 | static void drbd_uuid_dump(struct drbd_device *device, char *text, u64 *uuid, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2944 | u64 bits, u64 flags) |
| 2945 | { |
| 2946 | if (!uuid) { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 2947 | drbd_info(device, "%s uuid info vanished while I was looking!\n", text); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2948 | return; |
| 2949 | } |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 2950 | drbd_info(device, "%s %016llX:%016llX:%016llX:%016llX bits:%llu flags:%llX\n", |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2951 | text, |
| 2952 | (unsigned long long)uuid[UI_CURRENT], |
| 2953 | (unsigned long long)uuid[UI_BITMAP], |
| 2954 | (unsigned long long)uuid[UI_HISTORY_START], |
| 2955 | (unsigned long long)uuid[UI_HISTORY_END], |
| 2956 | (unsigned long long)bits, |
| 2957 | (unsigned long long)flags); |
| 2958 | } |
| 2959 | |
| 2960 | /* |
| 2961 | 100 after split brain try auto recover |
| 2962 | 2 C_SYNC_SOURCE set BitMap |
| 2963 | 1 C_SYNC_SOURCE use BitMap |
| 2964 | 0 no Sync |
| 2965 | -1 C_SYNC_TARGET use BitMap |
| 2966 | -2 C_SYNC_TARGET set BitMap |
| 2967 | -100 after split brain, disconnect |
| 2968 | -1000 unrelated data |
Philipp Reisner | 4a23f26 | 2011-01-11 17:42:17 +0100 | [diff] [blame] | 2969 | -1091 requires proto 91 |
| 2970 | -1096 requires proto 96 |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2971 | */ |
Lars Ellenberg | 44a4d55 | 2013-11-22 12:40:58 +0100 | [diff] [blame] | 2972 | static int drbd_uuid_compare(struct drbd_device *const device, int *rule_nr) __must_hold(local) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2973 | { |
Lars Ellenberg | 44a4d55 | 2013-11-22 12:40:58 +0100 | [diff] [blame] | 2974 | struct drbd_peer_device *const peer_device = first_peer_device(device); |
| 2975 | struct drbd_connection *const connection = peer_device ? peer_device->connection : NULL; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2976 | u64 self, peer; |
| 2977 | int i, j; |
| 2978 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2979 | self = device->ldev->md.uuid[UI_CURRENT] & ~((u64)1); |
| 2980 | peer = device->p_uuid[UI_CURRENT] & ~((u64)1); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2981 | |
| 2982 | *rule_nr = 10; |
| 2983 | if (self == UUID_JUST_CREATED && peer == UUID_JUST_CREATED) |
| 2984 | return 0; |
| 2985 | |
| 2986 | *rule_nr = 20; |
| 2987 | if ((self == UUID_JUST_CREATED || self == (u64)0) && |
| 2988 | peer != UUID_JUST_CREATED) |
| 2989 | return -2; |
| 2990 | |
| 2991 | *rule_nr = 30; |
| 2992 | if (self != UUID_JUST_CREATED && |
| 2993 | (peer == UUID_JUST_CREATED || peer == (u64)0)) |
| 2994 | return 2; |
| 2995 | |
| 2996 | if (self == peer) { |
| 2997 | int rct, dc; /* roles at crash time */ |
| 2998 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 2999 | if (device->p_uuid[UI_BITMAP] == (u64)0 && device->ldev->md.uuid[UI_BITMAP] != (u64)0) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3000 | |
Lars Ellenberg | 44a4d55 | 2013-11-22 12:40:58 +0100 | [diff] [blame] | 3001 | if (connection->agreed_pro_version < 91) |
Philipp Reisner | 4a23f26 | 2011-01-11 17:42:17 +0100 | [diff] [blame] | 3002 | return -1091; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3003 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3004 | if ((device->ldev->md.uuid[UI_BITMAP] & ~((u64)1)) == (device->p_uuid[UI_HISTORY_START] & ~((u64)1)) && |
| 3005 | (device->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (device->p_uuid[UI_HISTORY_START + 1] & ~((u64)1))) { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3006 | drbd_info(device, "was SyncSource, missed the resync finished event, corrected myself:\n"); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3007 | drbd_uuid_move_history(device); |
| 3008 | device->ldev->md.uuid[UI_HISTORY_START] = device->ldev->md.uuid[UI_BITMAP]; |
| 3009 | device->ldev->md.uuid[UI_BITMAP] = 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3010 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3011 | drbd_uuid_dump(device, "self", device->ldev->md.uuid, |
| 3012 | device->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(device) : 0, 0); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3013 | *rule_nr = 34; |
| 3014 | } else { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3015 | drbd_info(device, "was SyncSource (peer failed to write sync_uuid)\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3016 | *rule_nr = 36; |
| 3017 | } |
| 3018 | |
| 3019 | return 1; |
| 3020 | } |
| 3021 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3022 | if (device->ldev->md.uuid[UI_BITMAP] == (u64)0 && device->p_uuid[UI_BITMAP] != (u64)0) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3023 | |
Lars Ellenberg | 44a4d55 | 2013-11-22 12:40:58 +0100 | [diff] [blame] | 3024 | if (connection->agreed_pro_version < 91) |
Philipp Reisner | 4a23f26 | 2011-01-11 17:42:17 +0100 | [diff] [blame] | 3025 | return -1091; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3026 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3027 | if ((device->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (device->p_uuid[UI_BITMAP] & ~((u64)1)) && |
| 3028 | (device->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == (device->p_uuid[UI_HISTORY_START] & ~((u64)1))) { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3029 | drbd_info(device, "was SyncTarget, peer missed the resync finished event, corrected peer:\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3030 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3031 | device->p_uuid[UI_HISTORY_START + 1] = device->p_uuid[UI_HISTORY_START]; |
| 3032 | device->p_uuid[UI_HISTORY_START] = device->p_uuid[UI_BITMAP]; |
| 3033 | device->p_uuid[UI_BITMAP] = 0UL; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3034 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3035 | drbd_uuid_dump(device, "peer", device->p_uuid, device->p_uuid[UI_SIZE], device->p_uuid[UI_FLAGS]); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3036 | *rule_nr = 35; |
| 3037 | } else { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3038 | drbd_info(device, "was SyncTarget (failed to write sync_uuid)\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3039 | *rule_nr = 37; |
| 3040 | } |
| 3041 | |
| 3042 | return -1; |
| 3043 | } |
| 3044 | |
| 3045 | /* Common power [off|failure] */ |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3046 | rct = (test_bit(CRASHED_PRIMARY, &device->flags) ? 1 : 0) + |
| 3047 | (device->p_uuid[UI_FLAGS] & 2); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3048 | /* lowest bit is set when we were primary, |
| 3049 | * next bit (weight 2) is set when peer was primary */ |
| 3050 | *rule_nr = 40; |
| 3051 | |
| 3052 | switch (rct) { |
| 3053 | case 0: /* !self_pri && !peer_pri */ return 0; |
| 3054 | case 1: /* self_pri && !peer_pri */ return 1; |
| 3055 | case 2: /* !self_pri && peer_pri */ return -1; |
| 3056 | case 3: /* self_pri && peer_pri */ |
Lars Ellenberg | 44a4d55 | 2013-11-22 12:40:58 +0100 | [diff] [blame] | 3057 | dc = test_bit(RESOLVE_CONFLICTS, &connection->flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3058 | return dc ? -1 : 1; |
| 3059 | } |
| 3060 | } |
| 3061 | |
| 3062 | *rule_nr = 50; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3063 | peer = device->p_uuid[UI_BITMAP] & ~((u64)1); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3064 | if (self == peer) |
| 3065 | return -1; |
| 3066 | |
| 3067 | *rule_nr = 51; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3068 | peer = device->p_uuid[UI_HISTORY_START] & ~((u64)1); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3069 | if (self == peer) { |
Lars Ellenberg | 44a4d55 | 2013-11-22 12:40:58 +0100 | [diff] [blame] | 3070 | if (connection->agreed_pro_version < 96 ? |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3071 | (device->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == |
| 3072 | (device->p_uuid[UI_HISTORY_START + 1] & ~((u64)1)) : |
| 3073 | peer + UUID_NEW_BM_OFFSET == (device->p_uuid[UI_BITMAP] & ~((u64)1))) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3074 | /* The last P_SYNC_UUID did not get though. Undo the last start of |
| 3075 | resync as sync source modifications of the peer's UUIDs. */ |
| 3076 | |
Lars Ellenberg | 44a4d55 | 2013-11-22 12:40:58 +0100 | [diff] [blame] | 3077 | if (connection->agreed_pro_version < 91) |
Philipp Reisner | 4a23f26 | 2011-01-11 17:42:17 +0100 | [diff] [blame] | 3078 | return -1091; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3079 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3080 | device->p_uuid[UI_BITMAP] = device->p_uuid[UI_HISTORY_START]; |
| 3081 | device->p_uuid[UI_HISTORY_START] = device->p_uuid[UI_HISTORY_START + 1]; |
Philipp Reisner | 4a23f26 | 2011-01-11 17:42:17 +0100 | [diff] [blame] | 3082 | |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3083 | drbd_info(device, "Lost last syncUUID packet, corrected:\n"); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3084 | drbd_uuid_dump(device, "peer", device->p_uuid, device->p_uuid[UI_SIZE], device->p_uuid[UI_FLAGS]); |
Philipp Reisner | 4a23f26 | 2011-01-11 17:42:17 +0100 | [diff] [blame] | 3085 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3086 | return -1; |
| 3087 | } |
| 3088 | } |
| 3089 | |
| 3090 | *rule_nr = 60; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3091 | self = device->ldev->md.uuid[UI_CURRENT] & ~((u64)1); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3092 | for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3093 | peer = device->p_uuid[i] & ~((u64)1); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3094 | if (self == peer) |
| 3095 | return -2; |
| 3096 | } |
| 3097 | |
| 3098 | *rule_nr = 70; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3099 | self = device->ldev->md.uuid[UI_BITMAP] & ~((u64)1); |
| 3100 | peer = device->p_uuid[UI_CURRENT] & ~((u64)1); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3101 | if (self == peer) |
| 3102 | return 1; |
| 3103 | |
| 3104 | *rule_nr = 71; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3105 | self = device->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3106 | if (self == peer) { |
Lars Ellenberg | 44a4d55 | 2013-11-22 12:40:58 +0100 | [diff] [blame] | 3107 | if (connection->agreed_pro_version < 96 ? |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3108 | (device->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == |
| 3109 | (device->p_uuid[UI_HISTORY_START] & ~((u64)1)) : |
| 3110 | self + UUID_NEW_BM_OFFSET == (device->ldev->md.uuid[UI_BITMAP] & ~((u64)1))) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3111 | /* The last P_SYNC_UUID did not get though. Undo the last start of |
| 3112 | resync as sync source modifications of our UUIDs. */ |
| 3113 | |
Lars Ellenberg | 44a4d55 | 2013-11-22 12:40:58 +0100 | [diff] [blame] | 3114 | if (connection->agreed_pro_version < 91) |
Philipp Reisner | 4a23f26 | 2011-01-11 17:42:17 +0100 | [diff] [blame] | 3115 | return -1091; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3116 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3117 | __drbd_uuid_set(device, UI_BITMAP, device->ldev->md.uuid[UI_HISTORY_START]); |
| 3118 | __drbd_uuid_set(device, UI_HISTORY_START, device->ldev->md.uuid[UI_HISTORY_START + 1]); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3119 | |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3120 | drbd_info(device, "Last syncUUID did not get through, corrected:\n"); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3121 | drbd_uuid_dump(device, "self", device->ldev->md.uuid, |
| 3122 | device->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(device) : 0, 0); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3123 | |
| 3124 | return 1; |
| 3125 | } |
| 3126 | } |
| 3127 | |
| 3128 | |
| 3129 | *rule_nr = 80; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3130 | peer = device->p_uuid[UI_CURRENT] & ~((u64)1); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3131 | for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3132 | self = device->ldev->md.uuid[i] & ~((u64)1); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3133 | if (self == peer) |
| 3134 | return 2; |
| 3135 | } |
| 3136 | |
| 3137 | *rule_nr = 90; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3138 | self = device->ldev->md.uuid[UI_BITMAP] & ~((u64)1); |
| 3139 | peer = device->p_uuid[UI_BITMAP] & ~((u64)1); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3140 | if (self == peer && self != ((u64)0)) |
| 3141 | return 100; |
| 3142 | |
| 3143 | *rule_nr = 100; |
| 3144 | for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3145 | self = device->ldev->md.uuid[i] & ~((u64)1); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3146 | for (j = UI_HISTORY_START; j <= UI_HISTORY_END; j++) { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3147 | peer = device->p_uuid[j] & ~((u64)1); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3148 | if (self == peer) |
| 3149 | return -100; |
| 3150 | } |
| 3151 | } |
| 3152 | |
| 3153 | return -1000; |
| 3154 | } |
| 3155 | |
| 3156 | /* drbd_sync_handshake() returns the new conn state on success, or |
| 3157 | CONN_MASK (-1) on failure. |
| 3158 | */ |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 3159 | static enum drbd_conns drbd_sync_handshake(struct drbd_peer_device *peer_device, |
| 3160 | enum drbd_role peer_role, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3161 | enum drbd_disk_state peer_disk) __must_hold(local) |
| 3162 | { |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 3163 | struct drbd_device *device = peer_device->device; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3164 | enum drbd_conns rv = C_MASK; |
| 3165 | enum drbd_disk_state mydisk; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 3166 | struct net_conf *nc; |
Andreas Gruenbacher | 6dff290 | 2011-06-28 14:18:12 +0200 | [diff] [blame] | 3167 | int hg, rule_nr, rr_conflict, tentative; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3168 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3169 | mydisk = device->state.disk; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3170 | if (mydisk == D_NEGOTIATING) |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3171 | mydisk = device->new_state_tmp.disk; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3172 | |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3173 | drbd_info(device, "drbd_sync_handshake:\n"); |
Philipp Reisner | 9f2247b | 2012-08-16 14:25:58 +0200 | [diff] [blame] | 3174 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3175 | spin_lock_irq(&device->ldev->md.uuid_lock); |
| 3176 | drbd_uuid_dump(device, "self", device->ldev->md.uuid, device->comm_bm_set, 0); |
| 3177 | drbd_uuid_dump(device, "peer", device->p_uuid, |
| 3178 | device->p_uuid[UI_SIZE], device->p_uuid[UI_FLAGS]); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3179 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3180 | hg = drbd_uuid_compare(device, &rule_nr); |
| 3181 | spin_unlock_irq(&device->ldev->md.uuid_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3182 | |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3183 | drbd_info(device, "uuid_compare()=%d by rule %d\n", hg, rule_nr); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3184 | |
| 3185 | if (hg == -1000) { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3186 | drbd_alert(device, "Unrelated data, aborting!\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3187 | return C_MASK; |
| 3188 | } |
Philipp Reisner | 4a23f26 | 2011-01-11 17:42:17 +0100 | [diff] [blame] | 3189 | if (hg < -1000) { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3190 | drbd_alert(device, "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] | 3191 | return C_MASK; |
| 3192 | } |
| 3193 | |
| 3194 | if ((mydisk == D_INCONSISTENT && peer_disk > D_INCONSISTENT) || |
| 3195 | (peer_disk == D_INCONSISTENT && mydisk > D_INCONSISTENT)) { |
| 3196 | int f = (hg == -100) || abs(hg) == 2; |
| 3197 | hg = mydisk > D_INCONSISTENT ? 1 : -1; |
| 3198 | if (f) |
| 3199 | hg = hg*2; |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3200 | drbd_info(device, "Becoming sync %s due to disk states.\n", |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3201 | hg > 0 ? "source" : "target"); |
| 3202 | } |
| 3203 | |
Adam Gandelman | 3a11a48 | 2010-04-08 16:48:23 -0700 | [diff] [blame] | 3204 | if (abs(hg) == 100) |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3205 | drbd_khelper(device, "initial-split-brain"); |
Adam Gandelman | 3a11a48 | 2010-04-08 16:48:23 -0700 | [diff] [blame] | 3206 | |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 3207 | rcu_read_lock(); |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 3208 | nc = rcu_dereference(peer_device->connection->net_conf); |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 3209 | |
| 3210 | if (hg == 100 || (hg == -100 && nc->always_asbp)) { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3211 | int pcount = (device->state.role == R_PRIMARY) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3212 | + (peer_role == R_PRIMARY); |
| 3213 | int forced = (hg == -100); |
| 3214 | |
| 3215 | switch (pcount) { |
| 3216 | case 0: |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 3217 | hg = drbd_asb_recover_0p(peer_device); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3218 | break; |
| 3219 | case 1: |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 3220 | hg = drbd_asb_recover_1p(peer_device); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3221 | break; |
| 3222 | case 2: |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 3223 | hg = drbd_asb_recover_2p(peer_device); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3224 | break; |
| 3225 | } |
| 3226 | if (abs(hg) < 100) { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3227 | drbd_warn(device, "Split-Brain detected, %d primaries, " |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3228 | "automatically solved. Sync from %s node\n", |
| 3229 | pcount, (hg < 0) ? "peer" : "this"); |
| 3230 | if (forced) { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3231 | drbd_warn(device, "Doing a full sync, since" |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3232 | " UUIDs where ambiguous.\n"); |
| 3233 | hg = hg*2; |
| 3234 | } |
| 3235 | } |
| 3236 | } |
| 3237 | |
| 3238 | if (hg == -100) { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3239 | if (test_bit(DISCARD_MY_DATA, &device->flags) && !(device->p_uuid[UI_FLAGS]&1)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3240 | hg = -1; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3241 | if (!test_bit(DISCARD_MY_DATA, &device->flags) && (device->p_uuid[UI_FLAGS]&1)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3242 | hg = 1; |
| 3243 | |
| 3244 | if (abs(hg) < 100) |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3245 | drbd_warn(device, "Split-Brain detected, manually solved. " |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3246 | "Sync from %s node\n", |
| 3247 | (hg < 0) ? "peer" : "this"); |
| 3248 | } |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 3249 | rr_conflict = nc->rr_conflict; |
Andreas Gruenbacher | 6dff290 | 2011-06-28 14:18:12 +0200 | [diff] [blame] | 3250 | tentative = nc->tentative; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 3251 | rcu_read_unlock(); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3252 | |
| 3253 | if (hg == -100) { |
Lars Ellenberg | 580b976 | 2010-02-26 23:15:23 +0100 | [diff] [blame] | 3254 | /* FIXME this log message is not correct if we end up here |
| 3255 | * after an attempted attach on a diskless node. |
| 3256 | * We just refuse to attach -- well, we drop the "connection" |
| 3257 | * to that disk, in a way... */ |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3258 | drbd_alert(device, "Split-Brain detected but unresolved, dropping connection!\n"); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3259 | drbd_khelper(device, "split-brain"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3260 | return C_MASK; |
| 3261 | } |
| 3262 | |
| 3263 | if (hg > 0 && mydisk <= D_INCONSISTENT) { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3264 | drbd_err(device, "I shall become SyncSource, but I am inconsistent!\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3265 | return C_MASK; |
| 3266 | } |
| 3267 | |
| 3268 | if (hg < 0 && /* by intention we do not use mydisk here. */ |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3269 | device->state.role == R_PRIMARY && device->state.disk >= D_CONSISTENT) { |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 3270 | switch (rr_conflict) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3271 | case ASB_CALL_HELPER: |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3272 | drbd_khelper(device, "pri-lost"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3273 | /* fall through */ |
| 3274 | case ASB_DISCONNECT: |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3275 | drbd_err(device, "I shall become SyncTarget, but I am primary!\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3276 | return C_MASK; |
| 3277 | case ASB_VIOLENTLY: |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3278 | drbd_warn(device, "Becoming SyncTarget, violating the stable-data" |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3279 | "assumption\n"); |
| 3280 | } |
| 3281 | } |
| 3282 | |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 3283 | if (tentative || test_bit(CONN_DRY_RUN, &peer_device->connection->flags)) { |
Philipp Reisner | cf14c2e | 2010-02-02 21:03:50 +0100 | [diff] [blame] | 3284 | if (hg == 0) |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3285 | drbd_info(device, "dry-run connect: No resync, would become Connected immediately.\n"); |
Philipp Reisner | cf14c2e | 2010-02-02 21:03:50 +0100 | [diff] [blame] | 3286 | else |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3287 | drbd_info(device, "dry-run connect: Would become %s, doing a %s resync.", |
Philipp Reisner | cf14c2e | 2010-02-02 21:03:50 +0100 | [diff] [blame] | 3288 | drbd_conn_str(hg > 0 ? C_SYNC_SOURCE : C_SYNC_TARGET), |
| 3289 | abs(hg) >= 2 ? "full" : "bit-map based"); |
| 3290 | return C_MASK; |
| 3291 | } |
| 3292 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3293 | if (abs(hg) >= 2) { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3294 | drbd_info(device, "Writing the whole bitmap, full sync required after drbd_sync_handshake.\n"); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3295 | if (drbd_bitmap_io(device, &drbd_bmio_set_n_write, "set_n_write from sync_handshake", |
Lars Ellenberg | 20ceb2b | 2011-01-21 10:56:44 +0100 | [diff] [blame] | 3296 | BM_LOCKED_SET_ALLOWED)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3297 | return C_MASK; |
| 3298 | } |
| 3299 | |
| 3300 | if (hg > 0) { /* become sync source. */ |
| 3301 | rv = C_WF_BITMAP_S; |
| 3302 | } else if (hg < 0) { /* become sync target */ |
| 3303 | rv = C_WF_BITMAP_T; |
| 3304 | } else { |
| 3305 | rv = C_CONNECTED; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3306 | if (drbd_bm_total_weight(device)) { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3307 | drbd_info(device, "No resync, but %lu bits in bitmap!\n", |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3308 | drbd_bm_total_weight(device)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3309 | } |
| 3310 | } |
| 3311 | |
| 3312 | return rv; |
| 3313 | } |
| 3314 | |
Philipp Reisner | f179d76 | 2011-05-16 17:31:47 +0200 | [diff] [blame] | 3315 | 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] | 3316 | { |
| 3317 | /* ASB_DISCARD_REMOTE - ASB_DISCARD_LOCAL is valid */ |
Philipp Reisner | f179d76 | 2011-05-16 17:31:47 +0200 | [diff] [blame] | 3318 | if (peer == ASB_DISCARD_REMOTE) |
| 3319 | return ASB_DISCARD_LOCAL; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3320 | |
| 3321 | /* 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] | 3322 | if (peer == ASB_DISCARD_LOCAL) |
| 3323 | return ASB_DISCARD_REMOTE; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3324 | |
| 3325 | /* everything else is valid if they are equal on both sides. */ |
Philipp Reisner | f179d76 | 2011-05-16 17:31:47 +0200 | [diff] [blame] | 3326 | return peer; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3327 | } |
| 3328 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 3329 | static int receive_protocol(struct drbd_connection *connection, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3330 | { |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 3331 | struct p_protocol *p = pi->data; |
Philipp Reisner | 036b17e | 2011-05-16 17:38:11 +0200 | [diff] [blame] | 3332 | enum drbd_after_sb_p p_after_sb_0p, p_after_sb_1p, p_after_sb_2p; |
| 3333 | int p_proto, p_discard_my_data, p_two_primaries, cf; |
| 3334 | struct net_conf *nc, *old_net_conf, *new_net_conf = NULL; |
| 3335 | char integrity_alg[SHARED_SECRET_MAX] = ""; |
Herbert Xu | 9534d67 | 2016-01-24 21:19:21 +0800 | [diff] [blame] | 3336 | struct crypto_ahash *peer_integrity_tfm = NULL; |
Philipp Reisner | 7aca6c7 | 2011-05-17 10:12:56 +0200 | [diff] [blame] | 3337 | void *int_dig_in = NULL, *int_dig_vv = NULL; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3338 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3339 | p_proto = be32_to_cpu(p->protocol); |
| 3340 | p_after_sb_0p = be32_to_cpu(p->after_sb_0p); |
| 3341 | p_after_sb_1p = be32_to_cpu(p->after_sb_1p); |
| 3342 | p_after_sb_2p = be32_to_cpu(p->after_sb_2p); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3343 | p_two_primaries = be32_to_cpu(p->two_primaries); |
Philipp Reisner | cf14c2e | 2010-02-02 21:03:50 +0100 | [diff] [blame] | 3344 | cf = be32_to_cpu(p->conn_flags); |
Andreas Gruenbacher | 6139f60 | 2011-05-06 20:00:02 +0200 | [diff] [blame] | 3345 | p_discard_my_data = cf & CF_DISCARD_MY_DATA; |
Philipp Reisner | cf14c2e | 2010-02-02 21:03:50 +0100 | [diff] [blame] | 3346 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 3347 | if (connection->agreed_pro_version >= 87) { |
Andreas Gruenbacher | 86db061 | 2011-04-28 15:24:18 +0200 | [diff] [blame] | 3348 | int err; |
Philipp Reisner | cf14c2e | 2010-02-02 21:03:50 +0100 | [diff] [blame] | 3349 | |
Andreas Gruenbacher | 88104ca | 2011-04-28 21:47:21 +0200 | [diff] [blame] | 3350 | if (pi->size > sizeof(integrity_alg)) |
Andreas Gruenbacher | 86db061 | 2011-04-28 15:24:18 +0200 | [diff] [blame] | 3351 | return -EIO; |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 3352 | err = drbd_recv_all(connection, integrity_alg, pi->size); |
Andreas Gruenbacher | 86db061 | 2011-04-28 15:24:18 +0200 | [diff] [blame] | 3353 | if (err) |
| 3354 | return err; |
Philipp Reisner | 036b17e | 2011-05-16 17:38:11 +0200 | [diff] [blame] | 3355 | integrity_alg[SHARED_SECRET_MAX - 1] = 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3356 | } |
| 3357 | |
Andreas Gruenbacher | 7d4c782 | 2011-07-17 23:06:12 +0200 | [diff] [blame] | 3358 | if (pi->cmd != P_PROTOCOL_UPDATE) { |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 3359 | clear_bit(CONN_DRY_RUN, &connection->flags); |
Philipp Reisner | 036b17e | 2011-05-16 17:38:11 +0200 | [diff] [blame] | 3360 | |
Andreas Gruenbacher | fbc12f4 | 2011-07-15 17:04:26 +0200 | [diff] [blame] | 3361 | if (cf & CF_DRY_RUN) |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 3362 | set_bit(CONN_DRY_RUN, &connection->flags); |
Andreas Gruenbacher | fbc12f4 | 2011-07-15 17:04:26 +0200 | [diff] [blame] | 3363 | |
| 3364 | rcu_read_lock(); |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 3365 | nc = rcu_dereference(connection->net_conf); |
Andreas Gruenbacher | fbc12f4 | 2011-07-15 17:04:26 +0200 | [diff] [blame] | 3366 | |
| 3367 | if (p_proto != nc->wire_protocol) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 3368 | drbd_err(connection, "incompatible %s settings\n", "protocol"); |
Andreas Gruenbacher | fbc12f4 | 2011-07-15 17:04:26 +0200 | [diff] [blame] | 3369 | goto disconnect_rcu_unlock; |
| 3370 | } |
| 3371 | |
| 3372 | if (convert_after_sb(p_after_sb_0p) != nc->after_sb_0p) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 3373 | drbd_err(connection, "incompatible %s settings\n", "after-sb-0pri"); |
Andreas Gruenbacher | fbc12f4 | 2011-07-15 17:04:26 +0200 | [diff] [blame] | 3374 | goto disconnect_rcu_unlock; |
| 3375 | } |
| 3376 | |
| 3377 | if (convert_after_sb(p_after_sb_1p) != nc->after_sb_1p) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 3378 | drbd_err(connection, "incompatible %s settings\n", "after-sb-1pri"); |
Andreas Gruenbacher | fbc12f4 | 2011-07-15 17:04:26 +0200 | [diff] [blame] | 3379 | goto disconnect_rcu_unlock; |
| 3380 | } |
| 3381 | |
| 3382 | if (convert_after_sb(p_after_sb_2p) != nc->after_sb_2p) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 3383 | drbd_err(connection, "incompatible %s settings\n", "after-sb-2pri"); |
Andreas Gruenbacher | fbc12f4 | 2011-07-15 17:04:26 +0200 | [diff] [blame] | 3384 | goto disconnect_rcu_unlock; |
| 3385 | } |
| 3386 | |
| 3387 | if (p_discard_my_data && nc->discard_my_data) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 3388 | drbd_err(connection, "incompatible %s settings\n", "discard-my-data"); |
Andreas Gruenbacher | fbc12f4 | 2011-07-15 17:04:26 +0200 | [diff] [blame] | 3389 | goto disconnect_rcu_unlock; |
| 3390 | } |
| 3391 | |
| 3392 | if (p_two_primaries != nc->two_primaries) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 3393 | drbd_err(connection, "incompatible %s settings\n", "allow-two-primaries"); |
Andreas Gruenbacher | fbc12f4 | 2011-07-15 17:04:26 +0200 | [diff] [blame] | 3394 | goto disconnect_rcu_unlock; |
| 3395 | } |
| 3396 | |
| 3397 | if (strcmp(integrity_alg, nc->integrity_alg)) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 3398 | drbd_err(connection, "incompatible %s settings\n", "data-integrity-alg"); |
Andreas Gruenbacher | fbc12f4 | 2011-07-15 17:04:26 +0200 | [diff] [blame] | 3399 | goto disconnect_rcu_unlock; |
| 3400 | } |
| 3401 | |
| 3402 | rcu_read_unlock(); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3403 | } |
| 3404 | |
Andreas Gruenbacher | 7d4c782 | 2011-07-17 23:06:12 +0200 | [diff] [blame] | 3405 | if (integrity_alg[0]) { |
| 3406 | int hash_size; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3407 | |
Andreas Gruenbacher | 7d4c782 | 2011-07-17 23:06:12 +0200 | [diff] [blame] | 3408 | /* |
| 3409 | * We can only change the peer data integrity algorithm |
| 3410 | * here. Changing our own data integrity algorithm |
| 3411 | * requires that we send a P_PROTOCOL_UPDATE packet at |
| 3412 | * the same time; otherwise, the peer has no way to |
| 3413 | * tell between which packets the algorithm should |
| 3414 | * change. |
| 3415 | */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3416 | |
Herbert Xu | 9534d67 | 2016-01-24 21:19:21 +0800 | [diff] [blame] | 3417 | peer_integrity_tfm = crypto_alloc_ahash(integrity_alg, 0, CRYPTO_ALG_ASYNC); |
Andreas Gruenbacher | 7d4c782 | 2011-07-17 23:06:12 +0200 | [diff] [blame] | 3418 | if (!peer_integrity_tfm) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 3419 | drbd_err(connection, "peer data-integrity-alg %s not supported\n", |
Andreas Gruenbacher | 7d4c782 | 2011-07-17 23:06:12 +0200 | [diff] [blame] | 3420 | integrity_alg); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3421 | goto disconnect; |
| 3422 | } |
Andreas Gruenbacher | 7d4c782 | 2011-07-17 23:06:12 +0200 | [diff] [blame] | 3423 | |
Herbert Xu | 9534d67 | 2016-01-24 21:19:21 +0800 | [diff] [blame] | 3424 | hash_size = crypto_ahash_digestsize(peer_integrity_tfm); |
Andreas Gruenbacher | 7d4c782 | 2011-07-17 23:06:12 +0200 | [diff] [blame] | 3425 | int_dig_in = kmalloc(hash_size, GFP_KERNEL); |
| 3426 | int_dig_vv = kmalloc(hash_size, GFP_KERNEL); |
| 3427 | if (!(int_dig_in && int_dig_vv)) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 3428 | drbd_err(connection, "Allocation of buffers for data integrity checking failed\n"); |
Andreas Gruenbacher | 7d4c782 | 2011-07-17 23:06:12 +0200 | [diff] [blame] | 3429 | goto disconnect; |
| 3430 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3431 | } |
| 3432 | |
Andreas Gruenbacher | 7d4c782 | 2011-07-17 23:06:12 +0200 | [diff] [blame] | 3433 | new_net_conf = kmalloc(sizeof(struct net_conf), GFP_KERNEL); |
| 3434 | if (!new_net_conf) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 3435 | drbd_err(connection, "Allocation of new net_conf failed\n"); |
Andreas Gruenbacher | 7d4c782 | 2011-07-17 23:06:12 +0200 | [diff] [blame] | 3436 | goto disconnect; |
| 3437 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3438 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 3439 | mutex_lock(&connection->data.mutex); |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 3440 | mutex_lock(&connection->resource->conf_update); |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 3441 | old_net_conf = connection->net_conf; |
Andreas Gruenbacher | 7d4c782 | 2011-07-17 23:06:12 +0200 | [diff] [blame] | 3442 | *new_net_conf = *old_net_conf; |
| 3443 | |
| 3444 | new_net_conf->wire_protocol = p_proto; |
| 3445 | new_net_conf->after_sb_0p = convert_after_sb(p_after_sb_0p); |
| 3446 | new_net_conf->after_sb_1p = convert_after_sb(p_after_sb_1p); |
| 3447 | new_net_conf->after_sb_2p = convert_after_sb(p_after_sb_2p); |
| 3448 | new_net_conf->two_primaries = p_two_primaries; |
| 3449 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 3450 | rcu_assign_pointer(connection->net_conf, new_net_conf); |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 3451 | mutex_unlock(&connection->resource->conf_update); |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 3452 | mutex_unlock(&connection->data.mutex); |
Andreas Gruenbacher | 7d4c782 | 2011-07-17 23:06:12 +0200 | [diff] [blame] | 3453 | |
Herbert Xu | 9534d67 | 2016-01-24 21:19:21 +0800 | [diff] [blame] | 3454 | crypto_free_ahash(connection->peer_integrity_tfm); |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 3455 | kfree(connection->int_dig_in); |
| 3456 | kfree(connection->int_dig_vv); |
| 3457 | connection->peer_integrity_tfm = peer_integrity_tfm; |
| 3458 | connection->int_dig_in = int_dig_in; |
| 3459 | connection->int_dig_vv = int_dig_vv; |
Andreas Gruenbacher | 7d4c782 | 2011-07-17 23:06:12 +0200 | [diff] [blame] | 3460 | |
| 3461 | if (strcmp(old_net_conf->integrity_alg, integrity_alg)) |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 3462 | drbd_info(connection, "peer data-integrity-alg: %s\n", |
Andreas Gruenbacher | 7d4c782 | 2011-07-17 23:06:12 +0200 | [diff] [blame] | 3463 | integrity_alg[0] ? integrity_alg : "(none)"); |
| 3464 | |
| 3465 | synchronize_rcu(); |
| 3466 | kfree(old_net_conf); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3467 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3468 | |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 3469 | disconnect_rcu_unlock: |
| 3470 | rcu_read_unlock(); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3471 | disconnect: |
Herbert Xu | 9534d67 | 2016-01-24 21:19:21 +0800 | [diff] [blame] | 3472 | crypto_free_ahash(peer_integrity_tfm); |
Philipp Reisner | 036b17e | 2011-05-16 17:38:11 +0200 | [diff] [blame] | 3473 | kfree(int_dig_in); |
| 3474 | kfree(int_dig_vv); |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 3475 | conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_HARD); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3476 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3477 | } |
| 3478 | |
| 3479 | /* helper function |
| 3480 | * input: alg name, feature name |
| 3481 | * return: NULL (alg name was "") |
| 3482 | * ERR_PTR(error) if something goes wrong |
| 3483 | * or the crypto hash ptr, if it worked out ok. */ |
Herbert Xu | 9534d67 | 2016-01-24 21:19:21 +0800 | [diff] [blame] | 3484 | static struct crypto_ahash *drbd_crypto_alloc_digest_safe(const struct drbd_device *device, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3485 | const char *alg, const char *name) |
| 3486 | { |
Herbert Xu | 9534d67 | 2016-01-24 21:19:21 +0800 | [diff] [blame] | 3487 | struct crypto_ahash *tfm; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3488 | |
| 3489 | if (!alg[0]) |
| 3490 | return NULL; |
| 3491 | |
Herbert Xu | 9534d67 | 2016-01-24 21:19:21 +0800 | [diff] [blame] | 3492 | tfm = crypto_alloc_ahash(alg, 0, CRYPTO_ALG_ASYNC); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3493 | if (IS_ERR(tfm)) { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3494 | drbd_err(device, "Can not allocate \"%s\" as %s (reason: %ld)\n", |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3495 | alg, name, PTR_ERR(tfm)); |
| 3496 | return tfm; |
| 3497 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3498 | return tfm; |
| 3499 | } |
| 3500 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 3501 | static int ignore_remaining_packet(struct drbd_connection *connection, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3502 | { |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 3503 | void *buffer = connection->data.rbuf; |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 3504 | int size = pi->size; |
| 3505 | |
| 3506 | while (size) { |
| 3507 | int s = min_t(int, size, DRBD_SOCKET_BUFFER_SIZE); |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 3508 | s = drbd_recv(connection, buffer, s); |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 3509 | if (s <= 0) { |
| 3510 | if (s < 0) |
| 3511 | return s; |
| 3512 | break; |
| 3513 | } |
| 3514 | size -= s; |
| 3515 | } |
| 3516 | if (size) |
| 3517 | return -EIO; |
| 3518 | return 0; |
| 3519 | } |
| 3520 | |
| 3521 | /* |
| 3522 | * config_unknown_volume - device configuration command for unknown volume |
| 3523 | * |
| 3524 | * When a device is added to an existing connection, the node on which the |
| 3525 | * device is added first will send configuration commands to its peer but the |
| 3526 | * peer will not know about the device yet. It will warn and ignore these |
| 3527 | * commands. Once the device is added on the second node, the second node will |
| 3528 | * send the same device configuration commands, but in the other direction. |
| 3529 | * |
| 3530 | * (We can also end up here if drbd is misconfigured.) |
| 3531 | */ |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 3532 | static int config_unknown_volume(struct drbd_connection *connection, struct packet_info *pi) |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 3533 | { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 3534 | drbd_warn(connection, "%s packet received for volume %u, which is not configured locally\n", |
Andreas Gruenbacher | 2fcb8f3 | 2011-07-03 11:41:08 +0200 | [diff] [blame] | 3535 | cmdname(pi->cmd), pi->vnr); |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 3536 | return ignore_remaining_packet(connection, pi); |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 3537 | } |
| 3538 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 3539 | static int receive_SyncParam(struct drbd_connection *connection, struct packet_info *pi) |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 3540 | { |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 3541 | struct drbd_peer_device *peer_device; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3542 | struct drbd_device *device; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 3543 | struct p_rs_param_95 *p; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3544 | unsigned int header_size, data_size, exp_max_sz; |
Herbert Xu | 9534d67 | 2016-01-24 21:19:21 +0800 | [diff] [blame] | 3545 | struct crypto_ahash *verify_tfm = NULL; |
| 3546 | struct crypto_ahash *csums_tfm = NULL; |
Philipp Reisner | 2ec91e0 | 2011-05-03 14:58:00 +0200 | [diff] [blame] | 3547 | struct net_conf *old_net_conf, *new_net_conf = NULL; |
Philipp Reisner | 813472c | 2011-05-03 16:47:02 +0200 | [diff] [blame] | 3548 | struct disk_conf *old_disk_conf = NULL, *new_disk_conf = NULL; |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 3549 | const int apv = connection->agreed_pro_version; |
Philipp Reisner | 813472c | 2011-05-03 16:47:02 +0200 | [diff] [blame] | 3550 | struct fifo_buffer *old_plan = NULL, *new_plan = NULL; |
Philipp Reisner | 778f271 | 2010-07-06 11:14:00 +0200 | [diff] [blame] | 3551 | int fifo_size = 0; |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3552 | int err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3553 | |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 3554 | peer_device = conn_peer_device(connection, pi->vnr); |
| 3555 | if (!peer_device) |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 3556 | return config_unknown_volume(connection, pi); |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 3557 | device = peer_device->device; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3558 | |
| 3559 | exp_max_sz = apv <= 87 ? sizeof(struct p_rs_param) |
| 3560 | : apv == 88 ? sizeof(struct p_rs_param) |
| 3561 | + SHARED_SECRET_MAX |
Philipp Reisner | 8e26f9c | 2010-07-06 17:25:54 +0200 | [diff] [blame] | 3562 | : apv <= 94 ? sizeof(struct p_rs_param_89) |
| 3563 | : /* apv >= 95 */ sizeof(struct p_rs_param_95); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3564 | |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 3565 | if (pi->size > exp_max_sz) { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3566 | drbd_err(device, "SyncParam packet too long: received %u, expected <= %u bytes\n", |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 3567 | pi->size, exp_max_sz); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3568 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3569 | } |
| 3570 | |
| 3571 | if (apv <= 88) { |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 3572 | header_size = sizeof(struct p_rs_param); |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 3573 | data_size = pi->size - header_size; |
Philipp Reisner | 8e26f9c | 2010-07-06 17:25:54 +0200 | [diff] [blame] | 3574 | } else if (apv <= 94) { |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 3575 | header_size = sizeof(struct p_rs_param_89); |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 3576 | data_size = pi->size - header_size; |
Andreas Gruenbacher | 0b0ba1e | 2011-06-27 16:23:33 +0200 | [diff] [blame] | 3577 | D_ASSERT(device, data_size == 0); |
Philipp Reisner | 8e26f9c | 2010-07-06 17:25:54 +0200 | [diff] [blame] | 3578 | } else { |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 3579 | header_size = sizeof(struct p_rs_param_95); |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 3580 | data_size = pi->size - header_size; |
Andreas Gruenbacher | 0b0ba1e | 2011-06-27 16:23:33 +0200 | [diff] [blame] | 3581 | D_ASSERT(device, data_size == 0); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3582 | } |
| 3583 | |
| 3584 | /* initialize verify_alg and csums_alg */ |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 3585 | p = pi->data; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3586 | memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX); |
| 3587 | |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 3588 | err = drbd_recv_all(peer_device->connection, p, header_size); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3589 | if (err) |
| 3590 | return err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3591 | |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 3592 | mutex_lock(&connection->resource->conf_update); |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 3593 | old_net_conf = peer_device->connection->net_conf; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3594 | if (get_ldev(device)) { |
Philipp Reisner | 813472c | 2011-05-03 16:47:02 +0200 | [diff] [blame] | 3595 | new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL); |
| 3596 | if (!new_disk_conf) { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3597 | put_ldev(device); |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 3598 | mutex_unlock(&connection->resource->conf_update); |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3599 | drbd_err(device, "Allocation of new disk_conf failed\n"); |
Philipp Reisner | 813472c | 2011-05-03 16:47:02 +0200 | [diff] [blame] | 3600 | return -ENOMEM; |
| 3601 | } |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 3602 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3603 | old_disk_conf = device->ldev->disk_conf; |
Philipp Reisner | 813472c | 2011-05-03 16:47:02 +0200 | [diff] [blame] | 3604 | *new_disk_conf = *old_disk_conf; |
| 3605 | |
Andreas Gruenbacher | 6394b93 | 2011-05-11 14:29:52 +0200 | [diff] [blame] | 3606 | new_disk_conf->resync_rate = be32_to_cpu(p->resync_rate); |
Philipp Reisner | 813472c | 2011-05-03 16:47:02 +0200 | [diff] [blame] | 3607 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3608 | |
| 3609 | if (apv >= 88) { |
| 3610 | if (apv == 88) { |
Philipp Reisner | 5de7382 | 2012-03-28 10:17:32 +0200 | [diff] [blame] | 3611 | if (data_size > SHARED_SECRET_MAX || data_size == 0) { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3612 | drbd_err(device, "verify-alg of wrong size, " |
Philipp Reisner | 5de7382 | 2012-03-28 10:17:32 +0200 | [diff] [blame] | 3613 | "peer wants %u, accepting only up to %u byte\n", |
| 3614 | data_size, SHARED_SECRET_MAX); |
Philipp Reisner | 813472c | 2011-05-03 16:47:02 +0200 | [diff] [blame] | 3615 | err = -EIO; |
| 3616 | goto reconnect; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3617 | } |
| 3618 | |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 3619 | err = drbd_recv_all(peer_device->connection, p->verify_alg, data_size); |
Philipp Reisner | 813472c | 2011-05-03 16:47:02 +0200 | [diff] [blame] | 3620 | if (err) |
| 3621 | goto reconnect; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3622 | /* we expect NUL terminated string */ |
| 3623 | /* but just in case someone tries to be evil */ |
Andreas Gruenbacher | 0b0ba1e | 2011-06-27 16:23:33 +0200 | [diff] [blame] | 3624 | D_ASSERT(device, p->verify_alg[data_size-1] == 0); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3625 | p->verify_alg[data_size-1] = 0; |
| 3626 | |
| 3627 | } else /* apv >= 89 */ { |
| 3628 | /* we still expect NUL terminated strings */ |
| 3629 | /* but just in case someone tries to be evil */ |
Andreas Gruenbacher | 0b0ba1e | 2011-06-27 16:23:33 +0200 | [diff] [blame] | 3630 | D_ASSERT(device, p->verify_alg[SHARED_SECRET_MAX-1] == 0); |
| 3631 | D_ASSERT(device, p->csums_alg[SHARED_SECRET_MAX-1] == 0); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3632 | p->verify_alg[SHARED_SECRET_MAX-1] = 0; |
| 3633 | p->csums_alg[SHARED_SECRET_MAX-1] = 0; |
| 3634 | } |
| 3635 | |
Philipp Reisner | 2ec91e0 | 2011-05-03 14:58:00 +0200 | [diff] [blame] | 3636 | if (strcmp(old_net_conf->verify_alg, p->verify_alg)) { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3637 | if (device->state.conn == C_WF_REPORT_PARAMS) { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3638 | drbd_err(device, "Different verify-alg settings. me=\"%s\" peer=\"%s\"\n", |
Philipp Reisner | 2ec91e0 | 2011-05-03 14:58:00 +0200 | [diff] [blame] | 3639 | old_net_conf->verify_alg, p->verify_alg); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3640 | goto disconnect; |
| 3641 | } |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3642 | verify_tfm = drbd_crypto_alloc_digest_safe(device, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3643 | p->verify_alg, "verify-alg"); |
| 3644 | if (IS_ERR(verify_tfm)) { |
| 3645 | verify_tfm = NULL; |
| 3646 | goto disconnect; |
| 3647 | } |
| 3648 | } |
| 3649 | |
Philipp Reisner | 2ec91e0 | 2011-05-03 14:58:00 +0200 | [diff] [blame] | 3650 | if (apv >= 89 && strcmp(old_net_conf->csums_alg, p->csums_alg)) { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3651 | if (device->state.conn == C_WF_REPORT_PARAMS) { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3652 | drbd_err(device, "Different csums-alg settings. me=\"%s\" peer=\"%s\"\n", |
Philipp Reisner | 2ec91e0 | 2011-05-03 14:58:00 +0200 | [diff] [blame] | 3653 | old_net_conf->csums_alg, p->csums_alg); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3654 | goto disconnect; |
| 3655 | } |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3656 | csums_tfm = drbd_crypto_alloc_digest_safe(device, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3657 | p->csums_alg, "csums-alg"); |
| 3658 | if (IS_ERR(csums_tfm)) { |
| 3659 | csums_tfm = NULL; |
| 3660 | goto disconnect; |
| 3661 | } |
| 3662 | } |
| 3663 | |
Philipp Reisner | 813472c | 2011-05-03 16:47:02 +0200 | [diff] [blame] | 3664 | if (apv > 94 && new_disk_conf) { |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 3665 | new_disk_conf->c_plan_ahead = be32_to_cpu(p->c_plan_ahead); |
| 3666 | new_disk_conf->c_delay_target = be32_to_cpu(p->c_delay_target); |
| 3667 | new_disk_conf->c_fill_target = be32_to_cpu(p->c_fill_target); |
| 3668 | 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] | 3669 | |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 3670 | fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3671 | if (fifo_size != device->rs_plan_s->size) { |
Philipp Reisner | 813472c | 2011-05-03 16:47:02 +0200 | [diff] [blame] | 3672 | new_plan = fifo_alloc(fifo_size); |
| 3673 | if (!new_plan) { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3674 | drbd_err(device, "kmalloc of fifo_buffer failed"); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3675 | put_ldev(device); |
Philipp Reisner | 778f271 | 2010-07-06 11:14:00 +0200 | [diff] [blame] | 3676 | goto disconnect; |
| 3677 | } |
| 3678 | } |
Philipp Reisner | 8e26f9c | 2010-07-06 17:25:54 +0200 | [diff] [blame] | 3679 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3680 | |
Philipp Reisner | 91fd4da | 2011-04-20 17:47:29 +0200 | [diff] [blame] | 3681 | if (verify_tfm || csums_tfm) { |
Philipp Reisner | 2ec91e0 | 2011-05-03 14:58:00 +0200 | [diff] [blame] | 3682 | new_net_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL); |
| 3683 | if (!new_net_conf) { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3684 | drbd_err(device, "Allocation of new net_conf failed\n"); |
Philipp Reisner | 91fd4da | 2011-04-20 17:47:29 +0200 | [diff] [blame] | 3685 | goto disconnect; |
| 3686 | } |
| 3687 | |
Philipp Reisner | 2ec91e0 | 2011-05-03 14:58:00 +0200 | [diff] [blame] | 3688 | *new_net_conf = *old_net_conf; |
Philipp Reisner | 91fd4da | 2011-04-20 17:47:29 +0200 | [diff] [blame] | 3689 | |
| 3690 | if (verify_tfm) { |
Philipp Reisner | 2ec91e0 | 2011-05-03 14:58:00 +0200 | [diff] [blame] | 3691 | strcpy(new_net_conf->verify_alg, p->verify_alg); |
| 3692 | new_net_conf->verify_alg_len = strlen(p->verify_alg) + 1; |
Herbert Xu | 9534d67 | 2016-01-24 21:19:21 +0800 | [diff] [blame] | 3693 | crypto_free_ahash(peer_device->connection->verify_tfm); |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 3694 | peer_device->connection->verify_tfm = verify_tfm; |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3695 | drbd_info(device, "using verify-alg: \"%s\"\n", p->verify_alg); |
Philipp Reisner | 91fd4da | 2011-04-20 17:47:29 +0200 | [diff] [blame] | 3696 | } |
| 3697 | if (csums_tfm) { |
Philipp Reisner | 2ec91e0 | 2011-05-03 14:58:00 +0200 | [diff] [blame] | 3698 | strcpy(new_net_conf->csums_alg, p->csums_alg); |
| 3699 | new_net_conf->csums_alg_len = strlen(p->csums_alg) + 1; |
Herbert Xu | 9534d67 | 2016-01-24 21:19:21 +0800 | [diff] [blame] | 3700 | crypto_free_ahash(peer_device->connection->csums_tfm); |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 3701 | peer_device->connection->csums_tfm = csums_tfm; |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3702 | drbd_info(device, "using csums-alg: \"%s\"\n", p->csums_alg); |
Philipp Reisner | 91fd4da | 2011-04-20 17:47:29 +0200 | [diff] [blame] | 3703 | } |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 3704 | rcu_assign_pointer(connection->net_conf, new_net_conf); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3705 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3706 | } |
| 3707 | |
Philipp Reisner | 813472c | 2011-05-03 16:47:02 +0200 | [diff] [blame] | 3708 | if (new_disk_conf) { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3709 | rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf); |
| 3710 | put_ldev(device); |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 3711 | } |
Philipp Reisner | 813472c | 2011-05-03 16:47:02 +0200 | [diff] [blame] | 3712 | |
| 3713 | if (new_plan) { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3714 | old_plan = device->rs_plan_s; |
| 3715 | rcu_assign_pointer(device->rs_plan_s, new_plan); |
Philipp Reisner | 813472c | 2011-05-03 16:47:02 +0200 | [diff] [blame] | 3716 | } |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 3717 | |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 3718 | mutex_unlock(&connection->resource->conf_update); |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 3719 | synchronize_rcu(); |
| 3720 | if (new_net_conf) |
| 3721 | kfree(old_net_conf); |
| 3722 | kfree(old_disk_conf); |
Philipp Reisner | 813472c | 2011-05-03 16:47:02 +0200 | [diff] [blame] | 3723 | kfree(old_plan); |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 3724 | |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3725 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3726 | |
Philipp Reisner | 813472c | 2011-05-03 16:47:02 +0200 | [diff] [blame] | 3727 | reconnect: |
| 3728 | if (new_disk_conf) { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3729 | put_ldev(device); |
Philipp Reisner | 813472c | 2011-05-03 16:47:02 +0200 | [diff] [blame] | 3730 | kfree(new_disk_conf); |
| 3731 | } |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 3732 | mutex_unlock(&connection->resource->conf_update); |
Philipp Reisner | 813472c | 2011-05-03 16:47:02 +0200 | [diff] [blame] | 3733 | return -EIO; |
| 3734 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3735 | disconnect: |
Philipp Reisner | 813472c | 2011-05-03 16:47:02 +0200 | [diff] [blame] | 3736 | kfree(new_plan); |
| 3737 | if (new_disk_conf) { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3738 | put_ldev(device); |
Philipp Reisner | 813472c | 2011-05-03 16:47:02 +0200 | [diff] [blame] | 3739 | kfree(new_disk_conf); |
| 3740 | } |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 3741 | mutex_unlock(&connection->resource->conf_update); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3742 | /* just for completeness: actually not needed, |
| 3743 | * as this is not reached if csums_tfm was ok. */ |
Herbert Xu | 9534d67 | 2016-01-24 21:19:21 +0800 | [diff] [blame] | 3744 | crypto_free_ahash(csums_tfm); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3745 | /* but free the verify_tfm again, if csums_tfm did not work out */ |
Herbert Xu | 9534d67 | 2016-01-24 21:19:21 +0800 | [diff] [blame] | 3746 | crypto_free_ahash(verify_tfm); |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 3747 | conn_request_state(peer_device->connection, NS(conn, C_DISCONNECTING), CS_HARD); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3748 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3749 | } |
| 3750 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3751 | /* warn if the arguments differ by more than 12.5% */ |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3752 | static void warn_if_differ_considerably(struct drbd_device *device, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3753 | const char *s, sector_t a, sector_t b) |
| 3754 | { |
| 3755 | sector_t d; |
| 3756 | if (a == 0 || b == 0) |
| 3757 | return; |
| 3758 | d = (a > b) ? (a - b) : (b - a); |
| 3759 | if (d > (a>>3) || d > (b>>3)) |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3760 | drbd_warn(device, "Considerable difference in %s: %llus vs. %llus\n", s, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3761 | (unsigned long long)a, (unsigned long long)b); |
| 3762 | } |
| 3763 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 3764 | static int receive_sizes(struct drbd_connection *connection, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3765 | { |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 3766 | struct drbd_peer_device *peer_device; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3767 | struct drbd_device *device; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 3768 | struct p_sizes *p = pi->data; |
Philipp Reisner | e96c963 | 2013-06-25 16:50:07 +0200 | [diff] [blame] | 3769 | enum determine_dev_size dd = DS_UNCHANGED; |
Lars Ellenberg | 6a8d68b | 2014-03-18 12:22:14 +0100 | [diff] [blame] | 3770 | sector_t p_size, p_usize, p_csize, my_usize; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3771 | int ldsc = 0; /* local disk size changed */ |
Philipp Reisner | e89b591 | 2010-03-24 17:11:33 +0100 | [diff] [blame] | 3772 | enum dds_flags ddsf; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3773 | |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 3774 | peer_device = conn_peer_device(connection, pi->vnr); |
| 3775 | if (!peer_device) |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 3776 | return config_unknown_volume(connection, pi); |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 3777 | device = peer_device->device; |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 3778 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3779 | p_size = be64_to_cpu(p->d_size); |
| 3780 | p_usize = be64_to_cpu(p->u_size); |
Lars Ellenberg | 6a8d68b | 2014-03-18 12:22:14 +0100 | [diff] [blame] | 3781 | p_csize = be64_to_cpu(p->c_size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3782 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3783 | /* just store the peer's disk size for now. |
| 3784 | * we still need to figure out whether we accept that. */ |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3785 | device->p_size = p_size; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3786 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3787 | if (get_ldev(device)) { |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 3788 | rcu_read_lock(); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3789 | my_usize = rcu_dereference(device->ldev->disk_conf)->disk_size; |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 3790 | rcu_read_unlock(); |
| 3791 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3792 | warn_if_differ_considerably(device, "lower level device sizes", |
| 3793 | p_size, drbd_get_max_capacity(device->ldev)); |
| 3794 | warn_if_differ_considerably(device, "user requested size", |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 3795 | p_usize, my_usize); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3796 | |
| 3797 | /* if this is the first connect, or an otherwise expected |
| 3798 | * param exchange, choose the minimum */ |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3799 | if (device->state.conn == C_WF_REPORT_PARAMS) |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 3800 | p_usize = min_not_zero(my_usize, p_usize); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3801 | |
| 3802 | /* Never shrink a device with usable data during connect. |
| 3803 | But allow online shrinking if we are connected. */ |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3804 | if (drbd_new_dev_size(device, device->ldev, p_usize, 0) < |
| 3805 | drbd_get_capacity(device->this_bdev) && |
| 3806 | device->state.disk >= D_OUTDATED && |
| 3807 | device->state.conn < C_CONNECTED) { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3808 | drbd_err(device, "The peer's disk size is too small!\n"); |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 3809 | conn_request_state(peer_device->connection, NS(conn, C_DISCONNECTING), CS_HARD); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3810 | put_ldev(device); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3811 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3812 | } |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 3813 | |
| 3814 | if (my_usize != p_usize) { |
| 3815 | struct disk_conf *old_disk_conf, *new_disk_conf = NULL; |
| 3816 | |
| 3817 | new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL); |
| 3818 | if (!new_disk_conf) { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3819 | drbd_err(device, "Allocation of new disk_conf failed\n"); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3820 | put_ldev(device); |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 3821 | return -ENOMEM; |
| 3822 | } |
| 3823 | |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 3824 | mutex_lock(&connection->resource->conf_update); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3825 | old_disk_conf = device->ldev->disk_conf; |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 3826 | *new_disk_conf = *old_disk_conf; |
| 3827 | new_disk_conf->disk_size = p_usize; |
| 3828 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3829 | rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf); |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 3830 | mutex_unlock(&connection->resource->conf_update); |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 3831 | synchronize_rcu(); |
| 3832 | kfree(old_disk_conf); |
| 3833 | |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3834 | drbd_info(device, "Peer sets u_size to %lu sectors\n", |
Philipp Reisner | daeda1c | 2011-05-03 15:00:55 +0200 | [diff] [blame] | 3835 | (unsigned long)my_usize); |
| 3836 | } |
| 3837 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3838 | put_ldev(device); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3839 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3840 | |
Lars Ellenberg | 20c68fd | 2014-04-28 18:43:25 +0200 | [diff] [blame] | 3841 | device->peer_max_bio_size = be32_to_cpu(p->max_bio_size); |
Lars Ellenberg | 20c68fd | 2014-04-28 18:43:25 +0200 | [diff] [blame] | 3842 | /* Leave drbd_reconsider_max_bio_size() before drbd_determine_dev_size(). |
| 3843 | In case we cleared the QUEUE_FLAG_DISCARD from our queue in |
| 3844 | drbd_reconsider_max_bio_size(), we can be sure that after |
| 3845 | drbd_determine_dev_size() no REQ_DISCARDs are in the queue. */ |
| 3846 | |
Philipp Reisner | e89b591 | 2010-03-24 17:11:33 +0100 | [diff] [blame] | 3847 | ddsf = be16_to_cpu(p->dds_flags); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3848 | if (get_ldev(device)) { |
Philipp Reisner | 8fe39aa | 2013-11-22 13:22:13 +0100 | [diff] [blame] | 3849 | drbd_reconsider_max_bio_size(device, device->ldev); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3850 | dd = drbd_determine_dev_size(device, ddsf, NULL); |
| 3851 | put_ldev(device); |
Philipp Reisner | e96c963 | 2013-06-25 16:50:07 +0200 | [diff] [blame] | 3852 | if (dd == DS_ERROR) |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3853 | return -EIO; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3854 | drbd_md_sync(device); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3855 | } else { |
Lars Ellenberg | 6a8d68b | 2014-03-18 12:22:14 +0100 | [diff] [blame] | 3856 | /* |
| 3857 | * I am diskless, need to accept the peer's *current* size. |
| 3858 | * I must NOT accept the peers backing disk size, |
| 3859 | * it may have been larger than mine all along... |
| 3860 | * |
| 3861 | * At this point, the peer knows more about my disk, or at |
| 3862 | * least about what we last agreed upon, than myself. |
| 3863 | * So if his c_size is less than his d_size, the most likely |
| 3864 | * reason is that *my* d_size was smaller last time we checked. |
| 3865 | * |
| 3866 | * However, if he sends a zero current size, |
| 3867 | * take his (user-capped or) backing disk size anyways. |
| 3868 | */ |
Philipp Reisner | 8fe39aa | 2013-11-22 13:22:13 +0100 | [diff] [blame] | 3869 | drbd_reconsider_max_bio_size(device, NULL); |
Lars Ellenberg | 6a8d68b | 2014-03-18 12:22:14 +0100 | [diff] [blame] | 3870 | drbd_set_my_capacity(device, p_csize ?: p_usize ?: p_size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3871 | } |
| 3872 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3873 | if (get_ldev(device)) { |
| 3874 | if (device->ldev->known_size != drbd_get_capacity(device->ldev->backing_bdev)) { |
| 3875 | device->ldev->known_size = drbd_get_capacity(device->ldev->backing_bdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3876 | ldsc = 1; |
| 3877 | } |
| 3878 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3879 | put_ldev(device); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3880 | } |
| 3881 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3882 | if (device->state.conn > C_WF_REPORT_PARAMS) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3883 | if (be64_to_cpu(p->c_size) != |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3884 | drbd_get_capacity(device->this_bdev) || ldsc) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3885 | /* we have different sizes, probably peer |
| 3886 | * needs to know my new size... */ |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 3887 | drbd_send_sizes(peer_device, 0, ddsf); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3888 | } |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3889 | if (test_and_clear_bit(RESIZE_PENDING, &device->flags) || |
| 3890 | (dd == DS_GREW && device->state.conn == C_CONNECTED)) { |
| 3891 | if (device->state.pdsk >= D_INCONSISTENT && |
| 3892 | device->state.disk >= D_INCONSISTENT) { |
Philipp Reisner | e89b591 | 2010-03-24 17:11:33 +0100 | [diff] [blame] | 3893 | if (ddsf & DDSF_NO_RESYNC) |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3894 | drbd_info(device, "Resync of new storage suppressed with --assume-clean\n"); |
Philipp Reisner | e89b591 | 2010-03-24 17:11:33 +0100 | [diff] [blame] | 3895 | else |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3896 | resync_after_online_grow(device); |
Philipp Reisner | e89b591 | 2010-03-24 17:11:33 +0100 | [diff] [blame] | 3897 | } else |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3898 | set_bit(RESYNC_AFTER_NEG, &device->flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3899 | } |
| 3900 | } |
| 3901 | |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3902 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3903 | } |
| 3904 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 3905 | static int receive_uuids(struct drbd_connection *connection, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3906 | { |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 3907 | struct drbd_peer_device *peer_device; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3908 | struct drbd_device *device; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 3909 | struct p_uuids *p = pi->data; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3910 | u64 *p_uuid; |
Lars Ellenberg | 62b0da3 | 2011-01-20 13:25:21 +0100 | [diff] [blame] | 3911 | int i, updated_uuids = 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3912 | |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 3913 | peer_device = conn_peer_device(connection, pi->vnr); |
| 3914 | if (!peer_device) |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 3915 | return config_unknown_volume(connection, pi); |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 3916 | device = peer_device->device; |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 3917 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3918 | p_uuid = kmalloc(sizeof(u64)*UI_EXTENDED_SIZE, GFP_NOIO); |
Jing Wang | 063eacf | 2012-10-25 15:00:56 +0800 | [diff] [blame] | 3919 | if (!p_uuid) { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3920 | drbd_err(device, "kmalloc of p_uuid failed\n"); |
Jing Wang | 063eacf | 2012-10-25 15:00:56 +0800 | [diff] [blame] | 3921 | return false; |
| 3922 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3923 | |
| 3924 | for (i = UI_CURRENT; i < UI_EXTENDED_SIZE; i++) |
| 3925 | p_uuid[i] = be64_to_cpu(p->uuid[i]); |
| 3926 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3927 | kfree(device->p_uuid); |
| 3928 | device->p_uuid = p_uuid; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3929 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3930 | if (device->state.conn < C_CONNECTED && |
| 3931 | device->state.disk < D_INCONSISTENT && |
| 3932 | device->state.role == R_PRIMARY && |
| 3933 | (device->ed_uuid & ~((u64)1)) != (p_uuid[UI_CURRENT] & ~((u64)1))) { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3934 | drbd_err(device, "Can only connect to data with current UUID=%016llX\n", |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3935 | (unsigned long long)device->ed_uuid); |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 3936 | conn_request_state(peer_device->connection, NS(conn, C_DISCONNECTING), CS_HARD); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3937 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3938 | } |
| 3939 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3940 | if (get_ldev(device)) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3941 | int skip_initial_sync = |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3942 | device->state.conn == C_CONNECTED && |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 3943 | peer_device->connection->agreed_pro_version >= 90 && |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3944 | device->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3945 | (p_uuid[UI_FLAGS] & 8); |
| 3946 | if (skip_initial_sync) { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 3947 | drbd_info(device, "Accepted new current UUID, preparing to skip initial sync\n"); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3948 | drbd_bitmap_io(device, &drbd_bmio_clear_n_write, |
Lars Ellenberg | 20ceb2b | 2011-01-21 10:56:44 +0100 | [diff] [blame] | 3949 | "clear_n_write from receive_uuids", |
| 3950 | BM_LOCKED_TEST_ALLOWED); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3951 | _drbd_uuid_set(device, UI_CURRENT, p_uuid[UI_CURRENT]); |
| 3952 | _drbd_uuid_set(device, UI_BITMAP, 0); |
| 3953 | _drbd_set_state(_NS2(device, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE), |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3954 | CS_VERBOSE, NULL); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3955 | drbd_md_sync(device); |
Lars Ellenberg | 62b0da3 | 2011-01-20 13:25:21 +0100 | [diff] [blame] | 3956 | updated_uuids = 1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3957 | } |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3958 | put_ldev(device); |
| 3959 | } else if (device->state.disk < D_INCONSISTENT && |
| 3960 | device->state.role == R_PRIMARY) { |
Philipp Reisner | 18a50fa | 2010-06-21 14:14:15 +0200 | [diff] [blame] | 3961 | /* I am a diskless primary, the peer just created a new current UUID |
| 3962 | for me. */ |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3963 | updated_uuids = drbd_set_ed_uuid(device, p_uuid[UI_CURRENT]); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3964 | } |
| 3965 | |
| 3966 | /* Before we test for the disk state, we should wait until an eventually |
| 3967 | ongoing cluster wide state change is finished. That is important if |
| 3968 | we are primary and are detaching from our disk. We need to see the |
| 3969 | new disk state... */ |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3970 | mutex_lock(device->state_mutex); |
| 3971 | mutex_unlock(device->state_mutex); |
| 3972 | if (device->state.conn >= C_CONNECTED && device->state.disk < D_INCONSISTENT) |
| 3973 | updated_uuids |= drbd_set_ed_uuid(device, p_uuid[UI_CURRENT]); |
Lars Ellenberg | 62b0da3 | 2011-01-20 13:25:21 +0100 | [diff] [blame] | 3974 | |
| 3975 | if (updated_uuids) |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 3976 | drbd_print_uuids(device, "receiver updated UUIDs to"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3977 | |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 3978 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3979 | } |
| 3980 | |
| 3981 | /** |
| 3982 | * convert_state() - Converts the peer's view of the cluster state to our point of view |
| 3983 | * @ps: The state as seen by the peer. |
| 3984 | */ |
| 3985 | static union drbd_state convert_state(union drbd_state ps) |
| 3986 | { |
| 3987 | union drbd_state ms; |
| 3988 | |
| 3989 | static enum drbd_conns c_tab[] = { |
Philipp Reisner | 369bea6 | 2011-07-06 23:04:44 +0200 | [diff] [blame] | 3990 | [C_WF_REPORT_PARAMS] = C_WF_REPORT_PARAMS, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3991 | [C_CONNECTED] = C_CONNECTED, |
| 3992 | |
| 3993 | [C_STARTING_SYNC_S] = C_STARTING_SYNC_T, |
| 3994 | [C_STARTING_SYNC_T] = C_STARTING_SYNC_S, |
| 3995 | [C_DISCONNECTING] = C_TEAR_DOWN, /* C_NETWORK_FAILURE, */ |
| 3996 | [C_VERIFY_S] = C_VERIFY_T, |
| 3997 | [C_MASK] = C_MASK, |
| 3998 | }; |
| 3999 | |
| 4000 | ms.i = ps.i; |
| 4001 | |
| 4002 | ms.conn = c_tab[ps.conn]; |
| 4003 | ms.peer = ps.role; |
| 4004 | ms.role = ps.peer; |
| 4005 | ms.pdsk = ps.disk; |
| 4006 | ms.disk = ps.pdsk; |
| 4007 | ms.peer_isp = (ps.aftr_isp | ps.user_isp); |
| 4008 | |
| 4009 | return ms; |
| 4010 | } |
| 4011 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4012 | static int receive_req_state(struct drbd_connection *connection, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4013 | { |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 4014 | struct drbd_peer_device *peer_device; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4015 | struct drbd_device *device; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4016 | struct p_req_state *p = pi->data; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4017 | union drbd_state mask, val; |
Andreas Gruenbacher | bf885f8 | 2010-12-08 00:39:32 +0100 | [diff] [blame] | 4018 | enum drbd_state_rv rv; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4019 | |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 4020 | peer_device = conn_peer_device(connection, pi->vnr); |
| 4021 | if (!peer_device) |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 4022 | return -EIO; |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 4023 | device = peer_device->device; |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 4024 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4025 | mask.i = be32_to_cpu(p->mask); |
| 4026 | val.i = be32_to_cpu(p->val); |
| 4027 | |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 4028 | if (test_bit(RESOLVE_CONFLICTS, &peer_device->connection->flags) && |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4029 | mutex_is_locked(device->state_mutex)) { |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 4030 | drbd_send_sr_reply(peer_device, SS_CONCURRENT_ST_CHG); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4031 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4032 | } |
| 4033 | |
| 4034 | mask = convert_state(mask); |
| 4035 | val = convert_state(val); |
| 4036 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4037 | rv = drbd_change_state(device, CS_VERBOSE, mask, val); |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 4038 | drbd_send_sr_reply(peer_device, rv); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4039 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4040 | drbd_md_sync(device); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4041 | |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4042 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4043 | } |
| 4044 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4045 | static int receive_req_conn_state(struct drbd_connection *connection, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4046 | { |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4047 | struct p_req_state *p = pi->data; |
Philipp Reisner | dfafcc8 | 2011-03-16 10:55:07 +0100 | [diff] [blame] | 4048 | union drbd_state mask, val; |
| 4049 | enum drbd_state_rv rv; |
| 4050 | |
| 4051 | mask.i = be32_to_cpu(p->mask); |
| 4052 | val.i = be32_to_cpu(p->val); |
| 4053 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4054 | if (test_bit(RESOLVE_CONFLICTS, &connection->flags) && |
| 4055 | mutex_is_locked(&connection->cstate_mutex)) { |
| 4056 | conn_send_sr_reply(connection, SS_CONCURRENT_ST_CHG); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4057 | return 0; |
Philipp Reisner | dfafcc8 | 2011-03-16 10:55:07 +0100 | [diff] [blame] | 4058 | } |
| 4059 | |
| 4060 | mask = convert_state(mask); |
| 4061 | val = convert_state(val); |
| 4062 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4063 | rv = conn_request_state(connection, mask, val, CS_VERBOSE | CS_LOCAL_ONLY | CS_IGN_OUTD_FAIL); |
| 4064 | conn_send_sr_reply(connection, rv); |
Philipp Reisner | dfafcc8 | 2011-03-16 10:55:07 +0100 | [diff] [blame] | 4065 | |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4066 | return 0; |
Philipp Reisner | dfafcc8 | 2011-03-16 10:55:07 +0100 | [diff] [blame] | 4067 | } |
| 4068 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4069 | static int receive_state(struct drbd_connection *connection, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4070 | { |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 4071 | struct drbd_peer_device *peer_device; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4072 | struct drbd_device *device; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4073 | struct p_state *p = pi->data; |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 4074 | union drbd_state os, ns, peer_state; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4075 | enum drbd_disk_state real_peer_disk; |
Philipp Reisner | 65d922c | 2010-06-16 16:18:09 +0200 | [diff] [blame] | 4076 | enum chg_state_flags cs_flags; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4077 | int rv; |
| 4078 | |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 4079 | peer_device = conn_peer_device(connection, pi->vnr); |
| 4080 | if (!peer_device) |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4081 | return config_unknown_volume(connection, pi); |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 4082 | device = peer_device->device; |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 4083 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4084 | peer_state.i = be32_to_cpu(p->state); |
| 4085 | |
| 4086 | real_peer_disk = peer_state.disk; |
| 4087 | if (peer_state.disk == D_NEGOTIATING) { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4088 | real_peer_disk = device->p_uuid[UI_FLAGS] & 4 ? D_INCONSISTENT : D_CONSISTENT; |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 4089 | drbd_info(device, "real peer disk state = %s\n", drbd_disk_str(real_peer_disk)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4090 | } |
| 4091 | |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 4092 | spin_lock_irq(&device->resource->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4093 | retry: |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4094 | os = ns = drbd_read_state(device); |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 4095 | spin_unlock_irq(&device->resource->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4096 | |
Philipp Reisner | 668700b | 2015-03-16 16:08:29 +0100 | [diff] [blame] | 4097 | /* If some other part of the code (ack_receiver thread, timeout) |
Lars Ellenberg | 545752d | 2011-12-05 14:39:25 +0100 | [diff] [blame] | 4098 | * already decided to close the connection again, |
| 4099 | * we must not "re-establish" it here. */ |
| 4100 | if (os.conn <= C_TEAR_DOWN) |
Lars Ellenberg | 58ffa58 | 2012-07-26 14:09:49 +0200 | [diff] [blame] | 4101 | return -ECONNRESET; |
Lars Ellenberg | 545752d | 2011-12-05 14:39:25 +0100 | [diff] [blame] | 4102 | |
Lars Ellenberg | 40424e4 | 2011-09-26 15:24:56 +0200 | [diff] [blame] | 4103 | /* If this is the "end of sync" confirmation, usually the peer disk |
| 4104 | * transitions from D_INCONSISTENT to D_UP_TO_DATE. For empty (0 bits |
| 4105 | * set) resync started in PausedSyncT, or if the timing of pause-/ |
| 4106 | * unpause-sync events has been "just right", the peer disk may |
| 4107 | * transition from D_CONSISTENT to D_UP_TO_DATE as well. |
| 4108 | */ |
| 4109 | if ((os.pdsk == D_INCONSISTENT || os.pdsk == D_CONSISTENT) && |
| 4110 | real_peer_disk == D_UP_TO_DATE && |
Lars Ellenberg | e9ef7bb | 2010-10-07 15:55:39 +0200 | [diff] [blame] | 4111 | os.conn > C_CONNECTED && os.disk == D_UP_TO_DATE) { |
| 4112 | /* If we are (becoming) SyncSource, but peer is still in sync |
| 4113 | * preparation, ignore its uptodate-ness to avoid flapping, it |
| 4114 | * will change to inconsistent once the peer reaches active |
| 4115 | * syncing states. |
| 4116 | * It may have changed syncer-paused flags, however, so we |
| 4117 | * cannot ignore this completely. */ |
| 4118 | if (peer_state.conn > C_CONNECTED && |
| 4119 | peer_state.conn < C_SYNC_SOURCE) |
| 4120 | real_peer_disk = D_INCONSISTENT; |
| 4121 | |
| 4122 | /* if peer_state changes to connected at the same time, |
| 4123 | * it explicitly notifies us that it finished resync. |
| 4124 | * Maybe we should finish it up, too? */ |
| 4125 | else if (os.conn >= C_SYNC_SOURCE && |
| 4126 | peer_state.conn == C_CONNECTED) { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4127 | if (drbd_bm_total_weight(device) <= device->rs_failed) |
| 4128 | drbd_resync_finished(device); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4129 | return 0; |
Lars Ellenberg | e9ef7bb | 2010-10-07 15:55:39 +0200 | [diff] [blame] | 4130 | } |
| 4131 | } |
| 4132 | |
Lars Ellenberg | 02b91b5 | 2012-06-28 18:26:52 +0200 | [diff] [blame] | 4133 | /* explicit verify finished notification, stop sector reached. */ |
| 4134 | if (os.conn == C_VERIFY_T && os.disk == D_UP_TO_DATE && |
| 4135 | peer_state.conn == C_CONNECTED && real_peer_disk == D_UP_TO_DATE) { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4136 | ov_out_of_sync_print(device); |
| 4137 | drbd_resync_finished(device); |
Lars Ellenberg | 58ffa58 | 2012-07-26 14:09:49 +0200 | [diff] [blame] | 4138 | return 0; |
Lars Ellenberg | 02b91b5 | 2012-06-28 18:26:52 +0200 | [diff] [blame] | 4139 | } |
| 4140 | |
Lars Ellenberg | e9ef7bb | 2010-10-07 15:55:39 +0200 | [diff] [blame] | 4141 | /* peer says his disk is inconsistent, while we think it is uptodate, |
| 4142 | * and this happens while the peer still thinks we have a sync going on, |
| 4143 | * but we think we are already done with the sync. |
| 4144 | * We ignore this to avoid flapping pdsk. |
| 4145 | * This should not happen, if the peer is a recent version of drbd. */ |
| 4146 | if (os.pdsk == D_UP_TO_DATE && real_peer_disk == D_INCONSISTENT && |
| 4147 | os.conn == C_CONNECTED && peer_state.conn > C_SYNC_SOURCE) |
| 4148 | real_peer_disk = D_UP_TO_DATE; |
| 4149 | |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 4150 | if (ns.conn == C_WF_REPORT_PARAMS) |
| 4151 | ns.conn = C_CONNECTED; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4152 | |
Philipp Reisner | 6753171 | 2010-10-27 12:21:30 +0200 | [diff] [blame] | 4153 | if (peer_state.conn == C_AHEAD) |
| 4154 | ns.conn = C_BEHIND; |
| 4155 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4156 | if (device->p_uuid && peer_state.disk >= D_NEGOTIATING && |
| 4157 | get_ldev_if_state(device, D_NEGOTIATING)) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4158 | int cr; /* consider resync */ |
| 4159 | |
| 4160 | /* if we established a new connection */ |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 4161 | cr = (os.conn < C_CONNECTED); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4162 | /* if we had an established connection |
| 4163 | * and one of the nodes newly attaches a disk */ |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 4164 | cr |= (os.conn == C_CONNECTED && |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4165 | (peer_state.disk == D_NEGOTIATING || |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 4166 | os.disk == D_NEGOTIATING)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4167 | /* if we have both been inconsistent, and the peer has been |
| 4168 | * forced to be UpToDate with --overwrite-data */ |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4169 | cr |= test_bit(CONSIDER_RESYNC, &device->flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4170 | /* if we had been plain connected, and the admin requested to |
| 4171 | * start a sync by "invalidate" or "invalidate-remote" */ |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 4172 | cr |= (os.conn == C_CONNECTED && |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4173 | (peer_state.conn >= C_STARTING_SYNC_S && |
| 4174 | peer_state.conn <= C_WF_BITMAP_T)); |
| 4175 | |
| 4176 | if (cr) |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 4177 | ns.conn = drbd_sync_handshake(peer_device, peer_state.role, real_peer_disk); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4178 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4179 | put_ldev(device); |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 4180 | if (ns.conn == C_MASK) { |
| 4181 | ns.conn = C_CONNECTED; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4182 | if (device->state.disk == D_NEGOTIATING) { |
| 4183 | drbd_force_state(device, NS(disk, D_FAILED)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4184 | } else if (peer_state.disk == D_NEGOTIATING) { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 4185 | drbd_err(device, "Disk attach process on the peer node was aborted.\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4186 | peer_state.disk = D_DISKLESS; |
Lars Ellenberg | 580b976 | 2010-02-26 23:15:23 +0100 | [diff] [blame] | 4187 | real_peer_disk = D_DISKLESS; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4188 | } else { |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 4189 | if (test_and_clear_bit(CONN_DRY_RUN, &peer_device->connection->flags)) |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4190 | return -EIO; |
Andreas Gruenbacher | 0b0ba1e | 2011-06-27 16:23:33 +0200 | [diff] [blame] | 4191 | D_ASSERT(device, os.conn == C_WF_REPORT_PARAMS); |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 4192 | conn_request_state(peer_device->connection, NS(conn, C_DISCONNECTING), CS_HARD); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4193 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4194 | } |
| 4195 | } |
| 4196 | } |
| 4197 | |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 4198 | spin_lock_irq(&device->resource->req_lock); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4199 | if (os.i != drbd_read_state(device).i) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4200 | goto retry; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4201 | clear_bit(CONSIDER_RESYNC, &device->flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4202 | ns.peer = peer_state.role; |
| 4203 | ns.pdsk = real_peer_disk; |
| 4204 | ns.peer_isp = (peer_state.aftr_isp | peer_state.user_isp); |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 4205 | if ((ns.conn == C_CONNECTED || ns.conn == C_WF_BITMAP_S) && ns.disk == D_NEGOTIATING) |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4206 | ns.disk = device->new_state_tmp.disk; |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 4207 | cs_flags = CS_VERBOSE + (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED ? 0 : CS_HARD); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4208 | if (ns.pdsk == D_CONSISTENT && drbd_suspended(device) && ns.conn == C_CONNECTED && os.conn < C_CONNECTED && |
| 4209 | test_bit(NEW_CUR_UUID, &device->flags)) { |
Andreas Gruenbacher | 8554df1 | 2011-01-25 15:37:43 +0100 | [diff] [blame] | 4210 | /* 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] | 4211 | for temporal network outages! */ |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 4212 | spin_unlock_irq(&device->resource->req_lock); |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 4213 | drbd_err(device, "Aborting Connect, can not thaw IO with an only Consistent peer\n"); |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 4214 | tl_clear(peer_device->connection); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4215 | drbd_uuid_new_current(device); |
| 4216 | clear_bit(NEW_CUR_UUID, &device->flags); |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 4217 | conn_request_state(peer_device->connection, NS2(conn, C_PROTOCOL_ERROR, susp, 0), CS_HARD); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4218 | return -EIO; |
Philipp Reisner | 481c6f5 | 2010-06-22 14:03:27 +0200 | [diff] [blame] | 4219 | } |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4220 | rv = _drbd_set_state(device, ns, cs_flags, NULL); |
| 4221 | ns = drbd_read_state(device); |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 4222 | spin_unlock_irq(&device->resource->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4223 | |
| 4224 | if (rv < SS_SUCCESS) { |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 4225 | conn_request_state(peer_device->connection, NS(conn, C_DISCONNECTING), CS_HARD); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4226 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4227 | } |
| 4228 | |
Lars Ellenberg | 4ac4aad | 2010-07-22 17:39:26 +0200 | [diff] [blame] | 4229 | if (os.conn > C_WF_REPORT_PARAMS) { |
| 4230 | if (ns.conn > C_CONNECTED && peer_state.conn <= C_CONNECTED && |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4231 | peer_state.disk != D_NEGOTIATING ) { |
| 4232 | /* we want resync, peer has not yet decided to sync... */ |
| 4233 | /* Nowadays only used when forcing a node into primary role and |
| 4234 | setting its disk to UpToDate with that */ |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 4235 | drbd_send_uuids(peer_device); |
| 4236 | drbd_send_current_state(peer_device); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4237 | } |
| 4238 | } |
| 4239 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4240 | clear_bit(DISCARD_MY_DATA, &device->flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4241 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4242 | drbd_md_sync(device); /* update connected indicator, la_size_sect, ... */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4243 | |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4244 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4245 | } |
| 4246 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4247 | static int receive_sync_uuid(struct drbd_connection *connection, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4248 | { |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 4249 | struct drbd_peer_device *peer_device; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4250 | struct drbd_device *device; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4251 | struct p_rs_uuid *p = pi->data; |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 4252 | |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 4253 | peer_device = conn_peer_device(connection, pi->vnr); |
| 4254 | if (!peer_device) |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 4255 | return -EIO; |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 4256 | device = peer_device->device; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4257 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4258 | wait_event(device->misc_wait, |
| 4259 | device->state.conn == C_WF_SYNC_UUID || |
| 4260 | device->state.conn == C_BEHIND || |
| 4261 | device->state.conn < C_CONNECTED || |
| 4262 | device->state.disk < D_NEGOTIATING); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4263 | |
Andreas Gruenbacher | 0b0ba1e | 2011-06-27 16:23:33 +0200 | [diff] [blame] | 4264 | /* D_ASSERT(device, device->state.conn == C_WF_SYNC_UUID ); */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4265 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4266 | /* Here the _drbd_uuid_ functions are right, current should |
| 4267 | _not_ be rotated into the history */ |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4268 | if (get_ldev_if_state(device, D_NEGOTIATING)) { |
| 4269 | _drbd_uuid_set(device, UI_CURRENT, be64_to_cpu(p->uuid)); |
| 4270 | _drbd_uuid_set(device, UI_BITMAP, 0UL); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4271 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4272 | drbd_print_uuids(device, "updated sync uuid"); |
| 4273 | drbd_start_resync(device, C_SYNC_TARGET); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4274 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4275 | put_ldev(device); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4276 | } else |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 4277 | drbd_err(device, "Ignoring SyncUUID packet!\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4278 | |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4279 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4280 | } |
| 4281 | |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4282 | /** |
| 4283 | * receive_bitmap_plain |
| 4284 | * |
| 4285 | * Return 0 when done, 1 when another iteration is needed, and a negative error |
| 4286 | * code upon failure. |
| 4287 | */ |
| 4288 | static int |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 4289 | receive_bitmap_plain(struct drbd_peer_device *peer_device, unsigned int size, |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4290 | unsigned long *p, struct bm_xfer_ctx *c) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4291 | { |
Andreas Gruenbacher | 50d0b1a | 2011-03-30 11:53:51 +0200 | [diff] [blame] | 4292 | unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE - |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 4293 | drbd_header_size(peer_device->connection); |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4294 | unsigned int num_words = min_t(size_t, data_size / sizeof(*p), |
Andreas Gruenbacher | 50d0b1a | 2011-03-30 11:53:51 +0200 | [diff] [blame] | 4295 | c->bm_words - c->word_offset); |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4296 | unsigned int want = num_words * sizeof(*p); |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4297 | int err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4298 | |
Andreas Gruenbacher | 50d0b1a | 2011-03-30 11:53:51 +0200 | [diff] [blame] | 4299 | if (want != size) { |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 4300 | drbd_err(peer_device, "%s:want (%u) != size (%u)\n", __func__, want, size); |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4301 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4302 | } |
| 4303 | if (want == 0) |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4304 | return 0; |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 4305 | err = drbd_recv_all(peer_device->connection, p, want); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4306 | if (err) |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4307 | return err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4308 | |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 4309 | drbd_bm_merge_lel(peer_device->device, c->word_offset, num_words, p); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4310 | |
| 4311 | c->word_offset += num_words; |
| 4312 | c->bit_offset = c->word_offset * BITS_PER_LONG; |
| 4313 | if (c->bit_offset > c->bm_bits) |
| 4314 | c->bit_offset = c->bm_bits; |
| 4315 | |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4316 | return 1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4317 | } |
| 4318 | |
Andreas Gruenbacher | a02d124 | 2011-03-22 17:20:45 +0100 | [diff] [blame] | 4319 | static enum drbd_bitmap_code dcbp_get_code(struct p_compressed_bm *p) |
| 4320 | { |
| 4321 | return (enum drbd_bitmap_code)(p->encoding & 0x0f); |
| 4322 | } |
| 4323 | |
| 4324 | static int dcbp_get_start(struct p_compressed_bm *p) |
| 4325 | { |
| 4326 | return (p->encoding & 0x80) != 0; |
| 4327 | } |
| 4328 | |
| 4329 | static int dcbp_get_pad_bits(struct p_compressed_bm *p) |
| 4330 | { |
| 4331 | return (p->encoding >> 4) & 0x7; |
| 4332 | } |
| 4333 | |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4334 | /** |
| 4335 | * recv_bm_rle_bits |
| 4336 | * |
| 4337 | * Return 0 when done, 1 when another iteration is needed, and a negative error |
| 4338 | * code upon failure. |
| 4339 | */ |
| 4340 | static int |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 4341 | recv_bm_rle_bits(struct drbd_peer_device *peer_device, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4342 | struct p_compressed_bm *p, |
Philipp Reisner | c6d25cf | 2011-01-19 16:13:06 +0100 | [diff] [blame] | 4343 | struct bm_xfer_ctx *c, |
| 4344 | unsigned int len) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4345 | { |
| 4346 | struct bitstream bs; |
| 4347 | u64 look_ahead; |
| 4348 | u64 rl; |
| 4349 | u64 tmp; |
| 4350 | unsigned long s = c->bit_offset; |
| 4351 | unsigned long e; |
Andreas Gruenbacher | a02d124 | 2011-03-22 17:20:45 +0100 | [diff] [blame] | 4352 | int toggle = dcbp_get_start(p); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4353 | int have; |
| 4354 | int bits; |
| 4355 | |
Andreas Gruenbacher | a02d124 | 2011-03-22 17:20:45 +0100 | [diff] [blame] | 4356 | bitstream_init(&bs, p->code, len, dcbp_get_pad_bits(p)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4357 | |
| 4358 | bits = bitstream_get_bits(&bs, &look_ahead, 64); |
| 4359 | if (bits < 0) |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4360 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4361 | |
| 4362 | for (have = bits; have > 0; s += rl, toggle = !toggle) { |
| 4363 | bits = vli_decode_bits(&rl, look_ahead); |
| 4364 | if (bits <= 0) |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4365 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4366 | |
| 4367 | if (toggle) { |
| 4368 | e = s + rl -1; |
| 4369 | if (e >= c->bm_bits) { |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 4370 | drbd_err(peer_device, "bitmap overflow (e:%lu) while decoding bm RLE packet\n", e); |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4371 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4372 | } |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 4373 | _drbd_bm_set_bits(peer_device->device, s, e); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4374 | } |
| 4375 | |
| 4376 | if (have < bits) { |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 4377 | drbd_err(peer_device, "bitmap decoding error: h:%d b:%d la:0x%08llx l:%u/%u\n", |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4378 | have, bits, look_ahead, |
| 4379 | (unsigned int)(bs.cur.b - p->code), |
| 4380 | (unsigned int)bs.buf_len); |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4381 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4382 | } |
Lars Ellenberg | d2da5b0 | 2013-10-23 10:59:18 +0200 | [diff] [blame] | 4383 | /* if we consumed all 64 bits, assign 0; >> 64 is "undefined"; */ |
| 4384 | if (likely(bits < 64)) |
| 4385 | look_ahead >>= bits; |
| 4386 | else |
| 4387 | look_ahead = 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4388 | have -= bits; |
| 4389 | |
| 4390 | bits = bitstream_get_bits(&bs, &tmp, 64 - have); |
| 4391 | if (bits < 0) |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4392 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4393 | look_ahead |= tmp << have; |
| 4394 | have += bits; |
| 4395 | } |
| 4396 | |
| 4397 | c->bit_offset = s; |
| 4398 | bm_xfer_ctx_bit_to_word_offset(c); |
| 4399 | |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4400 | return (s != c->bm_bits); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4401 | } |
| 4402 | |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4403 | /** |
| 4404 | * decode_bitmap_c |
| 4405 | * |
| 4406 | * Return 0 when done, 1 when another iteration is needed, and a negative error |
| 4407 | * code upon failure. |
| 4408 | */ |
| 4409 | static int |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 4410 | decode_bitmap_c(struct drbd_peer_device *peer_device, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4411 | struct p_compressed_bm *p, |
Philipp Reisner | c6d25cf | 2011-01-19 16:13:06 +0100 | [diff] [blame] | 4412 | struct bm_xfer_ctx *c, |
| 4413 | unsigned int len) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4414 | { |
Andreas Gruenbacher | a02d124 | 2011-03-22 17:20:45 +0100 | [diff] [blame] | 4415 | if (dcbp_get_code(p) == RLE_VLI_Bits) |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 4416 | return recv_bm_rle_bits(peer_device, p, c, len - sizeof(*p)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4417 | |
| 4418 | /* other variants had been implemented for evaluation, |
| 4419 | * but have been dropped as this one turned out to be "best" |
| 4420 | * during all our tests. */ |
| 4421 | |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 4422 | drbd_err(peer_device, "receive_bitmap_c: unknown encoding %u\n", p->encoding); |
| 4423 | conn_request_state(peer_device->connection, NS(conn, C_PROTOCOL_ERROR), CS_HARD); |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4424 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4425 | } |
| 4426 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4427 | void INFO_bm_xfer_stats(struct drbd_device *device, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4428 | const char *direction, struct bm_xfer_ctx *c) |
| 4429 | { |
| 4430 | /* what would it take to transfer it "plaintext" */ |
Andreas Gruenbacher | a6b32bc | 2011-05-31 14:33:49 +0200 | [diff] [blame] | 4431 | unsigned int header_size = drbd_header_size(first_peer_device(device)->connection); |
Andreas Gruenbacher | 50d0b1a | 2011-03-30 11:53:51 +0200 | [diff] [blame] | 4432 | unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE - header_size; |
| 4433 | unsigned int plain = |
| 4434 | header_size * (DIV_ROUND_UP(c->bm_words, data_size) + 1) + |
| 4435 | c->bm_words * sizeof(unsigned long); |
| 4436 | unsigned int total = c->bytes[0] + c->bytes[1]; |
| 4437 | unsigned int r; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4438 | |
| 4439 | /* total can not be zero. but just in case: */ |
| 4440 | if (total == 0) |
| 4441 | return; |
| 4442 | |
| 4443 | /* don't report if not compressed */ |
| 4444 | if (total >= plain) |
| 4445 | return; |
| 4446 | |
| 4447 | /* total < plain. check for overflow, still */ |
| 4448 | r = (total > UINT_MAX/1000) ? (total / (plain/1000)) |
| 4449 | : (1000 * total / plain); |
| 4450 | |
| 4451 | if (r > 1000) |
| 4452 | r = 1000; |
| 4453 | |
| 4454 | r = 1000 - r; |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 4455 | drbd_info(device, "%s bitmap stats [Bytes(packets)]: plain %u(%u), RLE %u(%u), " |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4456 | "total %u; compression: %u.%u%%\n", |
| 4457 | direction, |
| 4458 | c->bytes[1], c->packets[1], |
| 4459 | c->bytes[0], c->packets[0], |
| 4460 | total, r/10, r % 10); |
| 4461 | } |
| 4462 | |
| 4463 | /* Since we are processing the bitfield from lower addresses to higher, |
| 4464 | it does not matter if the process it in 32 bit chunks or 64 bit |
| 4465 | chunks as long as it is little endian. (Understand it as byte stream, |
| 4466 | beginning with the lowest byte...) If we would use big endian |
| 4467 | we would need to process it from the highest address to the lowest, |
| 4468 | in order to be agnostic to the 32 vs 64 bits issue. |
| 4469 | |
| 4470 | returns 0 on failure, 1 if we successfully received it. */ |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4471 | static int receive_bitmap(struct drbd_connection *connection, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4472 | { |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 4473 | struct drbd_peer_device *peer_device; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4474 | struct drbd_device *device; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4475 | struct bm_xfer_ctx c; |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4476 | int err; |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 4477 | |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 4478 | peer_device = conn_peer_device(connection, pi->vnr); |
| 4479 | if (!peer_device) |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 4480 | return -EIO; |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 4481 | device = peer_device->device; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4482 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4483 | drbd_bm_lock(device, "receive bitmap", BM_LOCKED_SET_ALLOWED); |
Lars Ellenberg | 20ceb2b | 2011-01-21 10:56:44 +0100 | [diff] [blame] | 4484 | /* you are supposed to send additional out-of-sync information |
| 4485 | * if you actually set bits during this phase */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4486 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4487 | c = (struct bm_xfer_ctx) { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4488 | .bm_bits = drbd_bm_bits(device), |
| 4489 | .bm_words = drbd_bm_words(device), |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4490 | }; |
| 4491 | |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4492 | for(;;) { |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4493 | if (pi->cmd == P_BITMAP) |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 4494 | err = receive_bitmap_plain(peer_device, pi->size, pi->data, &c); |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4495 | else if (pi->cmd == P_COMPRESSED_BITMAP) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4496 | /* MAYBE: sanity check that we speak proto >= 90, |
| 4497 | * and the feature is enabled! */ |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4498 | struct p_compressed_bm *p = pi->data; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4499 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4500 | if (pi->size > DRBD_SOCKET_BUFFER_SIZE - drbd_header_size(connection)) { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 4501 | drbd_err(device, "ReportCBitmap packet too large\n"); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4502 | err = -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4503 | goto out; |
| 4504 | } |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4505 | if (pi->size <= sizeof(*p)) { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 4506 | drbd_err(device, "ReportCBitmap packet too small (l:%u)\n", pi->size); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4507 | err = -EIO; |
Andreas Gruenbacher | 78fcbda | 2010-12-10 22:18:27 +0100 | [diff] [blame] | 4508 | goto out; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4509 | } |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 4510 | err = drbd_recv_all(peer_device->connection, p, pi->size); |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4511 | if (err) |
| 4512 | goto out; |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 4513 | err = decode_bitmap_c(peer_device, p, &c, pi->size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4514 | } else { |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 4515 | drbd_warn(device, "receive_bitmap: cmd neither ReportBitMap nor ReportCBitMap (is 0x%x)", pi->cmd); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4516 | err = -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4517 | goto out; |
| 4518 | } |
| 4519 | |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 4520 | c.packets[pi->cmd == P_BITMAP]++; |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4521 | c.bytes[pi->cmd == P_BITMAP] += drbd_header_size(connection) + pi->size; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4522 | |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4523 | if (err <= 0) { |
| 4524 | if (err < 0) |
| 4525 | goto out; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4526 | break; |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4527 | } |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 4528 | err = drbd_recv_header(peer_device->connection, pi); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4529 | if (err) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4530 | goto out; |
Andreas Gruenbacher | 2c46407 | 2010-12-11 21:53:12 +0100 | [diff] [blame] | 4531 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4532 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4533 | INFO_bm_xfer_stats(device, "receive", &c); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4534 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4535 | if (device->state.conn == C_WF_BITMAP_T) { |
Andreas Gruenbacher | de1f8e4 | 2010-12-10 21:04:00 +0100 | [diff] [blame] | 4536 | enum drbd_state_rv rv; |
| 4537 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4538 | err = drbd_send_bitmap(device); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4539 | if (err) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4540 | goto out; |
| 4541 | /* Omit CS_ORDERED with this state transition to avoid deadlocks. */ |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4542 | rv = _drbd_request_state(device, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE); |
Andreas Gruenbacher | 0b0ba1e | 2011-06-27 16:23:33 +0200 | [diff] [blame] | 4543 | D_ASSERT(device, rv == SS_SUCCESS); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4544 | } else if (device->state.conn != C_WF_BITMAP_S) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4545 | /* admin may have requested C_DISCONNECTING, |
| 4546 | * other threads may have noticed network errors */ |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 4547 | drbd_info(device, "unexpected cstate (%s) in receive_bitmap\n", |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4548 | drbd_conn_str(device->state.conn)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4549 | } |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4550 | err = 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4551 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4552 | out: |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4553 | drbd_bm_unlock(device); |
| 4554 | if (!err && device->state.conn == C_WF_BITMAP_S) |
| 4555 | drbd_start_resync(device, C_SYNC_SOURCE); |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4556 | return err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4557 | } |
| 4558 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4559 | static int receive_skip(struct drbd_connection *connection, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4560 | { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 4561 | drbd_warn(connection, "skipping unknown optional packet type %d, l: %d!\n", |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 4562 | pi->cmd, pi->size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4563 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4564 | return ignore_remaining_packet(connection, pi); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4565 | } |
| 4566 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4567 | static int receive_UnplugRemote(struct drbd_connection *connection, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4568 | { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4569 | /* Make sure we've acked all the TCP data associated |
| 4570 | * with the data requests being unplugged */ |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4571 | drbd_tcp_quickack(connection->data.socket); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4572 | |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4573 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4574 | } |
| 4575 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4576 | static int receive_out_of_sync(struct drbd_connection *connection, struct packet_info *pi) |
Philipp Reisner | 73a01a1 | 2010-10-27 14:33:00 +0200 | [diff] [blame] | 4577 | { |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 4578 | struct drbd_peer_device *peer_device; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4579 | struct drbd_device *device; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4580 | struct p_block_desc *p = pi->data; |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 4581 | |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 4582 | peer_device = conn_peer_device(connection, pi->vnr); |
| 4583 | if (!peer_device) |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 4584 | return -EIO; |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 4585 | device = peer_device->device; |
Philipp Reisner | 73a01a1 | 2010-10-27 14:33:00 +0200 | [diff] [blame] | 4586 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4587 | switch (device->state.conn) { |
Lars Ellenberg | f735e363 | 2010-12-17 21:06:18 +0100 | [diff] [blame] | 4588 | case C_WF_SYNC_UUID: |
| 4589 | case C_WF_BITMAP_T: |
| 4590 | case C_BEHIND: |
| 4591 | break; |
| 4592 | default: |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 4593 | drbd_err(device, "ASSERT FAILED cstate = %s, expected: WFSyncUUID|WFBitMapT|Behind\n", |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4594 | drbd_conn_str(device->state.conn)); |
Lars Ellenberg | f735e363 | 2010-12-17 21:06:18 +0100 | [diff] [blame] | 4595 | } |
| 4596 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4597 | drbd_set_out_of_sync(device, be64_to_cpu(p->sector), be32_to_cpu(p->blksize)); |
Philipp Reisner | 73a01a1 | 2010-10-27 14:33:00 +0200 | [diff] [blame] | 4598 | |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4599 | return 0; |
Philipp Reisner | 73a01a1 | 2010-10-27 14:33:00 +0200 | [diff] [blame] | 4600 | } |
| 4601 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4602 | struct data_cmd { |
| 4603 | int expect_payload; |
| 4604 | size_t pkt_size; |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4605 | int (*fn)(struct drbd_connection *, struct packet_info *); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4606 | }; |
| 4607 | |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4608 | static struct data_cmd drbd_cmd_handler[] = { |
| 4609 | [P_DATA] = { 1, sizeof(struct p_data), receive_Data }, |
| 4610 | [P_DATA_REPLY] = { 1, sizeof(struct p_data), receive_DataReply }, |
| 4611 | [P_RS_DATA_REPLY] = { 1, sizeof(struct p_data), receive_RSDataReply } , |
| 4612 | [P_BARRIER] = { 0, sizeof(struct p_barrier), receive_Barrier } , |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4613 | [P_BITMAP] = { 1, 0, receive_bitmap } , |
| 4614 | [P_COMPRESSED_BITMAP] = { 1, 0, receive_bitmap } , |
| 4615 | [P_UNPLUG_REMOTE] = { 0, 0, receive_UnplugRemote }, |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4616 | [P_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest }, |
| 4617 | [P_RS_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest }, |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4618 | [P_SYNC_PARAM] = { 1, 0, receive_SyncParam }, |
| 4619 | [P_SYNC_PARAM89] = { 1, 0, receive_SyncParam }, |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4620 | [P_PROTOCOL] = { 1, sizeof(struct p_protocol), receive_protocol }, |
| 4621 | [P_UUIDS] = { 0, sizeof(struct p_uuids), receive_uuids }, |
| 4622 | [P_SIZES] = { 0, sizeof(struct p_sizes), receive_sizes }, |
| 4623 | [P_STATE] = { 0, sizeof(struct p_state), receive_state }, |
| 4624 | [P_STATE_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_state }, |
| 4625 | [P_SYNC_UUID] = { 0, sizeof(struct p_rs_uuid), receive_sync_uuid }, |
| 4626 | [P_OV_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest }, |
| 4627 | [P_OV_REPLY] = { 1, sizeof(struct p_block_req), receive_DataRequest }, |
| 4628 | [P_CSUM_RS_REQUEST] = { 1, sizeof(struct p_block_req), receive_DataRequest }, |
| 4629 | [P_DELAY_PROBE] = { 0, sizeof(struct p_delay_probe93), receive_skip }, |
Philipp Reisner | 73a01a1 | 2010-10-27 14:33:00 +0200 | [diff] [blame] | 4630 | [P_OUT_OF_SYNC] = { 0, sizeof(struct p_block_desc), receive_out_of_sync }, |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 4631 | [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] | 4632 | [P_PROTOCOL_UPDATE] = { 1, sizeof(struct p_protocol), receive_protocol }, |
Lars Ellenberg | a0fb3c4 | 2014-04-28 18:43:23 +0200 | [diff] [blame] | 4633 | [P_TRIM] = { 0, sizeof(struct p_trim), receive_Data }, |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4634 | }; |
| 4635 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4636 | static void drbdd(struct drbd_connection *connection) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4637 | { |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4638 | struct packet_info pi; |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4639 | size_t shs; /* sub header size */ |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4640 | int err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4641 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4642 | while (get_t_state(&connection->receiver) == RUNNING) { |
Andreas Gruenbacher | deebe19 | 2011-03-25 00:01:04 +0100 | [diff] [blame] | 4643 | struct data_cmd *cmd; |
| 4644 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4645 | drbd_thread_current_set_cpu(&connection->receiver); |
Lars Ellenberg | 944410e | 2014-05-06 15:02:05 +0200 | [diff] [blame] | 4646 | update_receiver_timing_details(connection, drbd_recv_header); |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4647 | if (drbd_recv_header(connection, &pi)) |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4648 | goto err_out; |
| 4649 | |
Andreas Gruenbacher | deebe19 | 2011-03-25 00:01:04 +0100 | [diff] [blame] | 4650 | cmd = &drbd_cmd_handler[pi.cmd]; |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 4651 | if (unlikely(pi.cmd >= ARRAY_SIZE(drbd_cmd_handler) || !cmd->fn)) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 4652 | drbd_err(connection, "Unexpected data packet %s (0x%04x)", |
Andreas Gruenbacher | 2fcb8f3 | 2011-07-03 11:41:08 +0200 | [diff] [blame] | 4653 | cmdname(pi.cmd), pi.cmd); |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4654 | goto err_out; |
Lars Ellenberg | 0b33a91 | 2009-11-16 15:58:04 +0100 | [diff] [blame] | 4655 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4656 | |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4657 | shs = cmd->pkt_size; |
| 4658 | if (pi.size > shs && !cmd->expect_payload) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 4659 | drbd_err(connection, "No payload expected %s l:%d\n", |
Andreas Gruenbacher | 2fcb8f3 | 2011-07-03 11:41:08 +0200 | [diff] [blame] | 4660 | cmdname(pi.cmd), pi.size); |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4661 | goto err_out; |
| 4662 | } |
| 4663 | |
Lars Ellenberg | c13f7e1 | 2010-10-29 23:32:01 +0200 | [diff] [blame] | 4664 | if (shs) { |
Lars Ellenberg | 944410e | 2014-05-06 15:02:05 +0200 | [diff] [blame] | 4665 | update_receiver_timing_details(connection, drbd_recv_all_warn); |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4666 | err = drbd_recv_all_warn(connection, pi.data, shs); |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 4667 | if (err) |
Lars Ellenberg | c13f7e1 | 2010-10-29 23:32:01 +0200 | [diff] [blame] | 4668 | goto err_out; |
Andreas Gruenbacher | e285721 | 2011-03-25 00:57:38 +0100 | [diff] [blame] | 4669 | pi.size -= shs; |
Lars Ellenberg | c13f7e1 | 2010-10-29 23:32:01 +0200 | [diff] [blame] | 4670 | } |
| 4671 | |
Lars Ellenberg | 944410e | 2014-05-06 15:02:05 +0200 | [diff] [blame] | 4672 | update_receiver_timing_details(connection, cmd->fn); |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4673 | err = cmd->fn(connection, &pi); |
Andreas Gruenbacher | 4a76b16 | 2011-03-25 02:43:51 +0100 | [diff] [blame] | 4674 | if (err) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 4675 | drbd_err(connection, "error receiving %s, e: %d l: %d!\n", |
Andreas Gruenbacher | 9f5bdc3 | 2011-03-28 14:23:08 +0200 | [diff] [blame] | 4676 | cmdname(pi.cmd), err, pi.size); |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 4677 | goto err_out; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4678 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4679 | } |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4680 | return; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4681 | |
Andreas Gruenbacher | 82bc019 | 2011-03-17 12:10:19 +0100 | [diff] [blame] | 4682 | err_out: |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4683 | conn_request_state(connection, NS(conn, C_PROTOCOL_ERROR), CS_HARD); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4684 | } |
| 4685 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4686 | static void conn_disconnect(struct drbd_connection *connection) |
Philipp Reisner | f70b3511 | 2010-06-24 14:34:40 +0200 | [diff] [blame] | 4687 | { |
Andreas Gruenbacher | c06ece6 | 2011-06-21 17:23:59 +0200 | [diff] [blame] | 4688 | struct drbd_peer_device *peer_device; |
Philipp Reisner | bbeb641 | 2011-02-10 13:45:46 +0100 | [diff] [blame] | 4689 | enum drbd_conns oc; |
Philipp Reisner | 376694a | 2011-11-07 10:54:28 +0100 | [diff] [blame] | 4690 | int vnr; |
Philipp Reisner | f70b3511 | 2010-06-24 14:34:40 +0200 | [diff] [blame] | 4691 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4692 | if (connection->cstate == C_STANDALONE) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4693 | return; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4694 | |
Lars Ellenberg | 545752d | 2011-12-05 14:39:25 +0100 | [diff] [blame] | 4695 | /* We are about to start the cleanup after connection loss. |
| 4696 | * Make sure drbd_make_request knows about that. |
| 4697 | * Usually we should be in some network failure state already, |
| 4698 | * but just in case we are not, we fix it up here. |
| 4699 | */ |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4700 | conn_request_state(connection, NS(conn, C_NETWORK_FAILURE), CS_HARD); |
Lars Ellenberg | 545752d | 2011-12-05 14:39:25 +0100 | [diff] [blame] | 4701 | |
Philipp Reisner | 668700b | 2015-03-16 16:08:29 +0100 | [diff] [blame] | 4702 | /* ack_receiver does not clean up anything. it must not interfere, either */ |
Philipp Reisner | 1c03e52 | 2015-03-16 15:01:00 +0100 | [diff] [blame] | 4703 | drbd_thread_stop(&connection->ack_receiver); |
Philipp Reisner | 668700b | 2015-03-16 16:08:29 +0100 | [diff] [blame] | 4704 | if (connection->ack_sender) { |
| 4705 | destroy_workqueue(connection->ack_sender); |
| 4706 | connection->ack_sender = NULL; |
| 4707 | } |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4708 | drbd_free_sock(connection); |
Philipp Reisner | 360cc74 | 2011-02-08 14:29:53 +0100 | [diff] [blame] | 4709 | |
Philipp Reisner | c141ebd | 2011-05-05 16:13:10 +0200 | [diff] [blame] | 4710 | rcu_read_lock(); |
Andreas Gruenbacher | c06ece6 | 2011-06-21 17:23:59 +0200 | [diff] [blame] | 4711 | idr_for_each_entry(&connection->peer_devices, peer_device, vnr) { |
| 4712 | struct drbd_device *device = peer_device->device; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4713 | kref_get(&device->kref); |
Philipp Reisner | c141ebd | 2011-05-05 16:13:10 +0200 | [diff] [blame] | 4714 | rcu_read_unlock(); |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 4715 | drbd_disconnected(peer_device); |
Andreas Gruenbacher | c06ece6 | 2011-06-21 17:23:59 +0200 | [diff] [blame] | 4716 | kref_put(&device->kref, drbd_destroy_device); |
Philipp Reisner | c141ebd | 2011-05-05 16:13:10 +0200 | [diff] [blame] | 4717 | rcu_read_lock(); |
| 4718 | } |
| 4719 | rcu_read_unlock(); |
| 4720 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4721 | if (!list_empty(&connection->current_epoch->list)) |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 4722 | drbd_err(connection, "ASSERTION FAILED: connection->current_epoch->list not empty\n"); |
Philipp Reisner | 12038a3 | 2011-11-09 19:18:00 +0100 | [diff] [blame] | 4723 | /* ok, no more ee's on the fly, it is safe to reset the epoch_size */ |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4724 | atomic_set(&connection->current_epoch->epoch_size, 0); |
| 4725 | connection->send.seen_any_write_yet = false; |
Philipp Reisner | 12038a3 | 2011-11-09 19:18:00 +0100 | [diff] [blame] | 4726 | |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 4727 | drbd_info(connection, "Connection closed\n"); |
Philipp Reisner | 360cc74 | 2011-02-08 14:29:53 +0100 | [diff] [blame] | 4728 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4729 | if (conn_highest_role(connection) == R_PRIMARY && conn_highest_pdsk(connection) >= D_UNKNOWN) |
| 4730 | conn_try_outdate_peer_async(connection); |
Philipp Reisner | cb70345 | 2011-03-24 11:03:07 +0100 | [diff] [blame] | 4731 | |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 4732 | spin_lock_irq(&connection->resource->req_lock); |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4733 | oc = connection->cstate; |
Philipp Reisner | bbeb641 | 2011-02-10 13:45:46 +0100 | [diff] [blame] | 4734 | if (oc >= C_UNCONNECTED) |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4735 | _conn_request_state(connection, NS(conn, C_UNCONNECTED), CS_VERBOSE); |
Philipp Reisner | bbeb641 | 2011-02-10 13:45:46 +0100 | [diff] [blame] | 4736 | |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 4737 | spin_unlock_irq(&connection->resource->req_lock); |
Philipp Reisner | 360cc74 | 2011-02-08 14:29:53 +0100 | [diff] [blame] | 4738 | |
Lars Ellenberg | f3dfa40 | 2011-05-02 10:45:05 +0200 | [diff] [blame] | 4739 | if (oc == C_DISCONNECTING) |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4740 | conn_request_state(connection, NS(conn, C_STANDALONE), CS_VERBOSE | CS_HARD); |
Philipp Reisner | 360cc74 | 2011-02-08 14:29:53 +0100 | [diff] [blame] | 4741 | } |
| 4742 | |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 4743 | static int drbd_disconnected(struct drbd_peer_device *peer_device) |
Philipp Reisner | 360cc74 | 2011-02-08 14:29:53 +0100 | [diff] [blame] | 4744 | { |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 4745 | struct drbd_device *device = peer_device->device; |
Philipp Reisner | 360cc74 | 2011-02-08 14:29:53 +0100 | [diff] [blame] | 4746 | unsigned int i; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4747 | |
Philipp Reisner | 8571957 | 2010-07-21 10:20:17 +0200 | [diff] [blame] | 4748 | /* wait for current activity to cease. */ |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 4749 | spin_lock_irq(&device->resource->req_lock); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4750 | _drbd_wait_ee_list_empty(device, &device->active_ee); |
| 4751 | _drbd_wait_ee_list_empty(device, &device->sync_ee); |
| 4752 | _drbd_wait_ee_list_empty(device, &device->read_ee); |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 4753 | spin_unlock_irq(&device->resource->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4754 | |
| 4755 | /* We do not have data structures that would allow us to |
| 4756 | * get the rs_pending_cnt down to 0 again. |
| 4757 | * * On C_SYNC_TARGET we do not have any data structures describing |
| 4758 | * the pending RSDataRequest's we have sent. |
| 4759 | * * On C_SYNC_SOURCE there is no data structure that tracks |
| 4760 | * the P_RS_DATA_REPLY blocks that we sent to the SyncTarget. |
| 4761 | * And no, it is not the sum of the reference counts in the |
| 4762 | * resync_LRU. The resync_LRU tracks the whole operation including |
| 4763 | * the disk-IO, while the rs_pending_cnt only tracks the blocks |
| 4764 | * on the fly. */ |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4765 | drbd_rs_cancel_all(device); |
| 4766 | device->rs_total = 0; |
| 4767 | device->rs_failed = 0; |
| 4768 | atomic_set(&device->rs_pending_cnt, 0); |
| 4769 | wake_up(&device->misc_wait); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4770 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4771 | del_timer_sync(&device->resync_timer); |
| 4772 | resync_timer_fn((unsigned long)device); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4773 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4774 | /* wait for all w_e_end_data_req, w_e_end_rsdata_req, w_send_barrier, |
| 4775 | * w_make_resync_request etc. which may still be on the worker queue |
| 4776 | * to be "canceled" */ |
Andreas Gruenbacher | b5043c5 | 2011-07-28 15:56:02 +0200 | [diff] [blame] | 4777 | drbd_flush_workqueue(&peer_device->connection->sender_work); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4778 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4779 | drbd_finish_peer_reqs(device); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4780 | |
Philipp Reisner | d10b4ea | 2011-11-30 23:25:36 +0100 | [diff] [blame] | 4781 | /* This second workqueue flush is necessary, since drbd_finish_peer_reqs() |
| 4782 | might have issued a work again. The one before drbd_finish_peer_reqs() is |
| 4783 | necessary to reclain net_ee in drbd_finish_peer_reqs(). */ |
Andreas Gruenbacher | b5043c5 | 2011-07-28 15:56:02 +0200 | [diff] [blame] | 4784 | drbd_flush_workqueue(&peer_device->connection->sender_work); |
Philipp Reisner | d10b4ea | 2011-11-30 23:25:36 +0100 | [diff] [blame] | 4785 | |
Lars Ellenberg | 08332d7 | 2012-08-17 15:09:13 +0200 | [diff] [blame] | 4786 | /* need to do it again, drbd_finish_peer_reqs() may have populated it |
| 4787 | * again via drbd_try_clear_on_disk_bm(). */ |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4788 | drbd_rs_cancel_all(device); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4789 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4790 | kfree(device->p_uuid); |
| 4791 | device->p_uuid = NULL; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4792 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4793 | if (!drbd_suspended(device)) |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 4794 | tl_clear(peer_device->connection); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4795 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4796 | drbd_md_sync(device); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4797 | |
Lars Ellenberg | 20ceb2b | 2011-01-21 10:56:44 +0100 | [diff] [blame] | 4798 | /* serialize with bitmap writeout triggered by the state change, |
| 4799 | * if any. */ |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4800 | wait_event(device->misc_wait, !test_bit(BITMAP_IO, &device->flags)); |
Lars Ellenberg | 20ceb2b | 2011-01-21 10:56:44 +0100 | [diff] [blame] | 4801 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4802 | /* tcp_close and release of sendpage pages can be deferred. I don't |
| 4803 | * want to use SO_LINGER, because apparently it can be deferred for |
| 4804 | * more than 20 seconds (longest time I checked). |
| 4805 | * |
| 4806 | * Actually we don't care for exactly when the network stack does its |
| 4807 | * put_page(), but release our reference on these pages right here. |
| 4808 | */ |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4809 | i = drbd_free_peer_reqs(device, &device->net_ee); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4810 | if (i) |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 4811 | drbd_info(device, "net_ee not empty, killed %u entries\n", i); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4812 | i = atomic_read(&device->pp_in_use_by_net); |
Lars Ellenberg | 435f074 | 2010-09-06 12:30:25 +0200 | [diff] [blame] | 4813 | if (i) |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 4814 | drbd_info(device, "pp_in_use_by_net = %d, expected 0\n", i); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 4815 | i = atomic_read(&device->pp_in_use); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4816 | if (i) |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 4817 | drbd_info(device, "pp_in_use = %d, expected 0\n", i); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4818 | |
Andreas Gruenbacher | 0b0ba1e | 2011-06-27 16:23:33 +0200 | [diff] [blame] | 4819 | D_ASSERT(device, list_empty(&device->read_ee)); |
| 4820 | D_ASSERT(device, list_empty(&device->active_ee)); |
| 4821 | D_ASSERT(device, list_empty(&device->sync_ee)); |
| 4822 | D_ASSERT(device, list_empty(&device->done_ee)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4823 | |
Philipp Reisner | 360cc74 | 2011-02-08 14:29:53 +0100 | [diff] [blame] | 4824 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4825 | } |
| 4826 | |
| 4827 | /* |
| 4828 | * We support PRO_VERSION_MIN to PRO_VERSION_MAX. The protocol version |
| 4829 | * we can agree on is stored in agreed_pro_version. |
| 4830 | * |
| 4831 | * feature flags and the reserved array should be enough room for future |
| 4832 | * enhancements of the handshake protocol, and possible plugins... |
| 4833 | * |
| 4834 | * for now, they are expected to be zero, but ignored. |
| 4835 | */ |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4836 | static int drbd_send_features(struct drbd_connection *connection) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4837 | { |
Andreas Gruenbacher | 9f5bdc3 | 2011-03-28 14:23:08 +0200 | [diff] [blame] | 4838 | struct drbd_socket *sock; |
| 4839 | struct p_connection_features *p; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4840 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4841 | sock = &connection->data; |
| 4842 | p = conn_prepare_command(connection, sock); |
Andreas Gruenbacher | 9f5bdc3 | 2011-03-28 14:23:08 +0200 | [diff] [blame] | 4843 | if (!p) |
Andreas Gruenbacher | e8d17b0 | 2011-03-16 00:54:19 +0100 | [diff] [blame] | 4844 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4845 | memset(p, 0, sizeof(*p)); |
| 4846 | p->protocol_min = cpu_to_be32(PRO_VERSION_MIN); |
| 4847 | p->protocol_max = cpu_to_be32(PRO_VERSION_MAX); |
Lars Ellenberg | 20c68fd | 2014-04-28 18:43:25 +0200 | [diff] [blame] | 4848 | p->feature_flags = cpu_to_be32(PRO_FEATURES); |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4849 | return conn_send_command(connection, sock, P_CONNECTION_FEATURES, sizeof(*p), NULL, 0); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4850 | } |
| 4851 | |
| 4852 | /* |
| 4853 | * return values: |
| 4854 | * 1 yes, we have a valid connection |
| 4855 | * 0 oops, did not work out, please try again |
| 4856 | * -1 peer talks different language, |
| 4857 | * no point in trying again, please go standalone. |
| 4858 | */ |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4859 | static int drbd_do_features(struct drbd_connection *connection) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4860 | { |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4861 | /* ASSERT current == connection->receiver ... */ |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4862 | struct p_connection_features *p; |
| 4863 | const int expect = sizeof(struct p_connection_features); |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4864 | struct packet_info pi; |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 4865 | int err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4866 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4867 | err = drbd_send_features(connection); |
Andreas Gruenbacher | e8d17b0 | 2011-03-16 00:54:19 +0100 | [diff] [blame] | 4868 | if (err) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4869 | return 0; |
| 4870 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4871 | err = drbd_recv_header(connection, &pi); |
Andreas Gruenbacher | 69bc7bc | 2011-03-16 17:31:52 +0100 | [diff] [blame] | 4872 | if (err) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4873 | return 0; |
| 4874 | |
Andreas Gruenbacher | 6038178 | 2011-03-28 17:05:50 +0200 | [diff] [blame] | 4875 | if (pi.cmd != P_CONNECTION_FEATURES) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 4876 | drbd_err(connection, "expected ConnectionFeatures packet, received: %s (0x%04x)\n", |
Andreas Gruenbacher | 2fcb8f3 | 2011-07-03 11:41:08 +0200 | [diff] [blame] | 4877 | cmdname(pi.cmd), pi.cmd); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4878 | return -1; |
| 4879 | } |
| 4880 | |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4881 | if (pi.size != expect) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 4882 | drbd_err(connection, "expected ConnectionFeatures length: %u, received: %u\n", |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4883 | expect, pi.size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4884 | return -1; |
| 4885 | } |
| 4886 | |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 4887 | p = pi.data; |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4888 | err = drbd_recv_all_warn(connection, p, expect); |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 4889 | if (err) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4890 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4891 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4892 | p->protocol_min = be32_to_cpu(p->protocol_min); |
| 4893 | p->protocol_max = be32_to_cpu(p->protocol_max); |
| 4894 | if (p->protocol_max == 0) |
| 4895 | p->protocol_max = p->protocol_min; |
| 4896 | |
| 4897 | if (PRO_VERSION_MAX < p->protocol_min || |
| 4898 | PRO_VERSION_MIN > p->protocol_max) |
| 4899 | goto incompat; |
| 4900 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4901 | connection->agreed_pro_version = min_t(int, PRO_VERSION_MAX, p->protocol_max); |
Lars Ellenberg | 20c68fd | 2014-04-28 18:43:25 +0200 | [diff] [blame] | 4902 | connection->agreed_features = PRO_FEATURES & be32_to_cpu(p->feature_flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4903 | |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 4904 | drbd_info(connection, "Handshake successful: " |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4905 | "Agreed network protocol version %d\n", connection->agreed_pro_version); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4906 | |
Lars Ellenberg | 20c68fd | 2014-04-28 18:43:25 +0200 | [diff] [blame] | 4907 | drbd_info(connection, "Agreed to%ssupport TRIM on protocol level\n", |
| 4908 | connection->agreed_features & FF_TRIM ? " " : " not "); |
| 4909 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4910 | return 1; |
| 4911 | |
| 4912 | incompat: |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 4913 | drbd_err(connection, "incompatible DRBD dialects: " |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4914 | "I support %d-%d, peer supports %d-%d\n", |
| 4915 | PRO_VERSION_MIN, PRO_VERSION_MAX, |
| 4916 | p->protocol_min, p->protocol_max); |
| 4917 | return -1; |
| 4918 | } |
| 4919 | |
| 4920 | #if !defined(CONFIG_CRYPTO_HMAC) && !defined(CONFIG_CRYPTO_HMAC_MODULE) |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4921 | static int drbd_do_auth(struct drbd_connection *connection) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4922 | { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 4923 | drbd_err(connection, "This kernel was build without CONFIG_CRYPTO_HMAC.\n"); |
| 4924 | drbd_err(connection, "You need to disable 'cram-hmac-alg' in drbd.conf.\n"); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 4925 | return -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4926 | } |
| 4927 | #else |
| 4928 | #define CHALLENGE_LEN 64 |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 4929 | |
| 4930 | /* Return value: |
| 4931 | 1 - auth succeeded, |
| 4932 | 0 - failed, try again (network error), |
| 4933 | -1 - auth failed, don't try again. |
| 4934 | */ |
| 4935 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4936 | static int drbd_do_auth(struct drbd_connection *connection) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4937 | { |
Andreas Gruenbacher | 9f5bdc3 | 2011-03-28 14:23:08 +0200 | [diff] [blame] | 4938 | struct drbd_socket *sock; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4939 | char my_challenge[CHALLENGE_LEN]; /* 64 Bytes... */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4940 | char *response = NULL; |
| 4941 | char *right_response = NULL; |
| 4942 | char *peers_ch = NULL; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 4943 | unsigned int key_len; |
| 4944 | char secret[SHARED_SECRET_MAX]; /* 64 byte */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4945 | unsigned int resp_size; |
Herbert Xu | 9534d67 | 2016-01-24 21:19:21 +0800 | [diff] [blame] | 4946 | SHASH_DESC_ON_STACK(desc, connection->cram_hmac_tfm); |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4947 | struct packet_info pi; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 4948 | struct net_conf *nc; |
Andreas Gruenbacher | 69bc7bc | 2011-03-16 17:31:52 +0100 | [diff] [blame] | 4949 | int err, rv; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4950 | |
Andreas Gruenbacher | 9f5bdc3 | 2011-03-28 14:23:08 +0200 | [diff] [blame] | 4951 | /* FIXME: Put the challenge/response into the preallocated socket buffer. */ |
| 4952 | |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 4953 | rcu_read_lock(); |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4954 | nc = rcu_dereference(connection->net_conf); |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 4955 | key_len = strlen(nc->shared_secret); |
| 4956 | memcpy(secret, nc->shared_secret, key_len); |
| 4957 | rcu_read_unlock(); |
| 4958 | |
Herbert Xu | 9534d67 | 2016-01-24 21:19:21 +0800 | [diff] [blame] | 4959 | desc->tfm = connection->cram_hmac_tfm; |
| 4960 | desc->flags = 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4961 | |
Herbert Xu | 9534d67 | 2016-01-24 21:19:21 +0800 | [diff] [blame] | 4962 | rv = crypto_shash_setkey(connection->cram_hmac_tfm, (u8 *)secret, key_len); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4963 | if (rv) { |
Herbert Xu | 9534d67 | 2016-01-24 21:19:21 +0800 | [diff] [blame] | 4964 | drbd_err(connection, "crypto_shash_setkey() failed with %d\n", rv); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 4965 | rv = -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4966 | goto fail; |
| 4967 | } |
| 4968 | |
| 4969 | get_random_bytes(my_challenge, CHALLENGE_LEN); |
| 4970 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4971 | sock = &connection->data; |
| 4972 | if (!conn_prepare_command(connection, sock)) { |
Andreas Gruenbacher | 9f5bdc3 | 2011-03-28 14:23:08 +0200 | [diff] [blame] | 4973 | rv = 0; |
| 4974 | goto fail; |
| 4975 | } |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4976 | rv = !conn_send_command(connection, sock, P_AUTH_CHALLENGE, 0, |
Andreas Gruenbacher | 9f5bdc3 | 2011-03-28 14:23:08 +0200 | [diff] [blame] | 4977 | my_challenge, CHALLENGE_LEN); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4978 | if (!rv) |
| 4979 | goto fail; |
| 4980 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 4981 | err = drbd_recv_header(connection, &pi); |
Andreas Gruenbacher | 69bc7bc | 2011-03-16 17:31:52 +0100 | [diff] [blame] | 4982 | if (err) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4983 | rv = 0; |
| 4984 | goto fail; |
| 4985 | } |
| 4986 | |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4987 | if (pi.cmd != P_AUTH_CHALLENGE) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 4988 | drbd_err(connection, "expected AuthChallenge packet, received: %s (0x%04x)\n", |
Andreas Gruenbacher | 2fcb8f3 | 2011-07-03 11:41:08 +0200 | [diff] [blame] | 4989 | cmdname(pi.cmd), pi.cmd); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4990 | rv = 0; |
| 4991 | goto fail; |
| 4992 | } |
| 4993 | |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 4994 | if (pi.size > CHALLENGE_LEN * 2) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 4995 | drbd_err(connection, "expected AuthChallenge payload too big.\n"); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 4996 | rv = -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 4997 | goto fail; |
| 4998 | } |
| 4999 | |
Philipp Reisner | 67cca28 | 2014-04-28 18:43:30 +0200 | [diff] [blame] | 5000 | if (pi.size < CHALLENGE_LEN) { |
| 5001 | drbd_err(connection, "AuthChallenge payload too small.\n"); |
| 5002 | rv = -1; |
| 5003 | goto fail; |
| 5004 | } |
| 5005 | |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 5006 | peers_ch = kmalloc(pi.size, GFP_NOIO); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5007 | if (peers_ch == NULL) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 5008 | drbd_err(connection, "kmalloc of peers_ch failed\n"); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 5009 | rv = -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5010 | goto fail; |
| 5011 | } |
| 5012 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5013 | err = drbd_recv_all_warn(connection, peers_ch, pi.size); |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 5014 | if (err) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5015 | rv = 0; |
| 5016 | goto fail; |
| 5017 | } |
| 5018 | |
Philipp Reisner | 67cca28 | 2014-04-28 18:43:30 +0200 | [diff] [blame] | 5019 | if (!memcmp(my_challenge, peers_ch, CHALLENGE_LEN)) { |
| 5020 | drbd_err(connection, "Peer presented the same challenge!\n"); |
| 5021 | rv = -1; |
| 5022 | goto fail; |
| 5023 | } |
| 5024 | |
Herbert Xu | 9534d67 | 2016-01-24 21:19:21 +0800 | [diff] [blame] | 5025 | resp_size = crypto_shash_digestsize(connection->cram_hmac_tfm); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5026 | response = kmalloc(resp_size, GFP_NOIO); |
| 5027 | if (response == NULL) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 5028 | drbd_err(connection, "kmalloc of response failed\n"); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 5029 | rv = -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5030 | goto fail; |
| 5031 | } |
| 5032 | |
Herbert Xu | 9534d67 | 2016-01-24 21:19:21 +0800 | [diff] [blame] | 5033 | rv = crypto_shash_digest(desc, peers_ch, pi.size, response); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5034 | if (rv) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 5035 | drbd_err(connection, "crypto_hash_digest() failed with %d\n", rv); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 5036 | rv = -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5037 | goto fail; |
| 5038 | } |
| 5039 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5040 | if (!conn_prepare_command(connection, sock)) { |
Andreas Gruenbacher | 9f5bdc3 | 2011-03-28 14:23:08 +0200 | [diff] [blame] | 5041 | rv = 0; |
| 5042 | goto fail; |
| 5043 | } |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5044 | rv = !conn_send_command(connection, sock, P_AUTH_RESPONSE, 0, |
Andreas Gruenbacher | 9f5bdc3 | 2011-03-28 14:23:08 +0200 | [diff] [blame] | 5045 | response, resp_size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5046 | if (!rv) |
| 5047 | goto fail; |
| 5048 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5049 | err = drbd_recv_header(connection, &pi); |
Andreas Gruenbacher | 69bc7bc | 2011-03-16 17:31:52 +0100 | [diff] [blame] | 5050 | if (err) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5051 | rv = 0; |
| 5052 | goto fail; |
| 5053 | } |
| 5054 | |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 5055 | if (pi.cmd != P_AUTH_RESPONSE) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 5056 | drbd_err(connection, "expected AuthResponse packet, received: %s (0x%04x)\n", |
Andreas Gruenbacher | 2fcb8f3 | 2011-07-03 11:41:08 +0200 | [diff] [blame] | 5057 | cmdname(pi.cmd), pi.cmd); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5058 | rv = 0; |
| 5059 | goto fail; |
| 5060 | } |
| 5061 | |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 5062 | if (pi.size != resp_size) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 5063 | drbd_err(connection, "expected AuthResponse payload of wrong size\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5064 | rv = 0; |
| 5065 | goto fail; |
| 5066 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5067 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5068 | err = drbd_recv_all_warn(connection, response , resp_size); |
Andreas Gruenbacher | a5c3190 | 2011-03-24 03:28:04 +0100 | [diff] [blame] | 5069 | if (err) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5070 | rv = 0; |
| 5071 | goto fail; |
| 5072 | } |
| 5073 | |
| 5074 | right_response = kmalloc(resp_size, GFP_NOIO); |
Julia Lawall | 2d1ee87 | 2009-12-27 22:27:11 +0100 | [diff] [blame] | 5075 | if (right_response == NULL) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 5076 | drbd_err(connection, "kmalloc of right_response failed\n"); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 5077 | rv = -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5078 | goto fail; |
| 5079 | } |
| 5080 | |
Herbert Xu | 9534d67 | 2016-01-24 21:19:21 +0800 | [diff] [blame] | 5081 | rv = crypto_shash_digest(desc, my_challenge, CHALLENGE_LEN, |
| 5082 | right_response); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5083 | if (rv) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 5084 | drbd_err(connection, "crypto_hash_digest() failed with %d\n", rv); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 5085 | rv = -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5086 | goto fail; |
| 5087 | } |
| 5088 | |
| 5089 | rv = !memcmp(response, right_response, resp_size); |
| 5090 | |
| 5091 | if (rv) |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 5092 | drbd_info(connection, "Peer authenticated using %d bytes HMAC\n", |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 5093 | resp_size); |
Johannes Thoma | b10d96c | 2010-01-07 16:02:50 +0100 | [diff] [blame] | 5094 | else |
| 5095 | rv = -1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5096 | |
| 5097 | fail: |
| 5098 | kfree(peers_ch); |
| 5099 | kfree(response); |
| 5100 | kfree(right_response); |
Herbert Xu | 9534d67 | 2016-01-24 21:19:21 +0800 | [diff] [blame] | 5101 | shash_desc_zero(desc); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5102 | |
| 5103 | return rv; |
| 5104 | } |
| 5105 | #endif |
| 5106 | |
Andreas Gruenbacher | 8fe6055 | 2011-07-22 11:04:36 +0200 | [diff] [blame] | 5107 | int drbd_receiver(struct drbd_thread *thi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5108 | { |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5109 | struct drbd_connection *connection = thi->connection; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5110 | int h; |
| 5111 | |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 5112 | drbd_info(connection, "receiver (re)started\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5113 | |
| 5114 | do { |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5115 | h = conn_connect(connection); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5116 | if (h == 0) { |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5117 | conn_disconnect(connection); |
Philipp Reisner | 20ee639 | 2011-01-18 15:28:59 +0100 | [diff] [blame] | 5118 | schedule_timeout_interruptible(HZ); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5119 | } |
| 5120 | if (h == -1) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 5121 | drbd_warn(connection, "Discarding network configuration.\n"); |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5122 | conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_HARD); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5123 | } |
| 5124 | } while (h == 0); |
| 5125 | |
Philipp Reisner | 91fd4da | 2011-04-20 17:47:29 +0200 | [diff] [blame] | 5126 | if (h > 0) |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5127 | drbdd(connection); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5128 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5129 | conn_disconnect(connection); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5130 | |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 5131 | drbd_info(connection, "receiver terminated\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5132 | return 0; |
| 5133 | } |
| 5134 | |
| 5135 | /* ********* acknowledge sender ******** */ |
| 5136 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5137 | static int got_conn_RqSReply(struct drbd_connection *connection, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5138 | { |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 5139 | struct p_req_state_reply *p = pi->data; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5140 | int retcode = be32_to_cpu(p->retcode); |
| 5141 | |
| 5142 | if (retcode >= SS_SUCCESS) { |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5143 | set_bit(CONN_WD_ST_CHG_OKAY, &connection->flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5144 | } else { |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5145 | set_bit(CONN_WD_ST_CHG_FAIL, &connection->flags); |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 5146 | drbd_err(connection, "Requested state change failed by peer: %s (%d)\n", |
Philipp Reisner | e4f78ed | 2011-03-16 11:27:48 +0100 | [diff] [blame] | 5147 | drbd_set_st_err_str(retcode), retcode); |
| 5148 | } |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5149 | wake_up(&connection->ping_wait); |
Philipp Reisner | e4f78ed | 2011-03-16 11:27:48 +0100 | [diff] [blame] | 5150 | |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5151 | return 0; |
Philipp Reisner | e4f78ed | 2011-03-16 11:27:48 +0100 | [diff] [blame] | 5152 | } |
| 5153 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5154 | static int got_RqSReply(struct drbd_connection *connection, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5155 | { |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 5156 | struct drbd_peer_device *peer_device; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5157 | struct drbd_device *device; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 5158 | struct p_req_state_reply *p = pi->data; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5159 | int retcode = be32_to_cpu(p->retcode); |
| 5160 | |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 5161 | peer_device = conn_peer_device(connection, pi->vnr); |
| 5162 | if (!peer_device) |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5163 | return -EIO; |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 5164 | device = peer_device->device; |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 5165 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5166 | if (test_bit(CONN_WD_ST_CHG_REQ, &connection->flags)) { |
Andreas Gruenbacher | 0b0ba1e | 2011-06-27 16:23:33 +0200 | [diff] [blame] | 5167 | D_ASSERT(device, connection->agreed_pro_version < 100); |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5168 | return got_conn_RqSReply(connection, pi); |
Philipp Reisner | 4d0fc3f | 2012-01-20 13:52:27 +0100 | [diff] [blame] | 5169 | } |
| 5170 | |
Philipp Reisner | e4f78ed | 2011-03-16 11:27:48 +0100 | [diff] [blame] | 5171 | if (retcode >= SS_SUCCESS) { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5172 | set_bit(CL_ST_CHG_SUCCESS, &device->flags); |
Philipp Reisner | e4f78ed | 2011-03-16 11:27:48 +0100 | [diff] [blame] | 5173 | } else { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5174 | set_bit(CL_ST_CHG_FAIL, &device->flags); |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 5175 | drbd_err(device, "Requested state change failed by peer: %s (%d)\n", |
Philipp Reisner | e4f78ed | 2011-03-16 11:27:48 +0100 | [diff] [blame] | 5176 | drbd_set_st_err_str(retcode), retcode); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5177 | } |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5178 | wake_up(&device->state_wait); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5179 | |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5180 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5181 | } |
| 5182 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5183 | static int got_Ping(struct drbd_connection *connection, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5184 | { |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5185 | return drbd_send_ping_ack(connection); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5186 | |
| 5187 | } |
| 5188 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5189 | static int got_PingAck(struct drbd_connection *connection, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5190 | { |
| 5191 | /* restore idle timeout */ |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5192 | connection->meta.socket->sk->sk_rcvtimeo = connection->net_conf->ping_int*HZ; |
| 5193 | if (!test_and_set_bit(GOT_PING_ACK, &connection->flags)) |
| 5194 | wake_up(&connection->ping_wait); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5195 | |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5196 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5197 | } |
| 5198 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5199 | static int got_IsInSync(struct drbd_connection *connection, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5200 | { |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 5201 | struct drbd_peer_device *peer_device; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5202 | struct drbd_device *device; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 5203 | struct p_block_ack *p = pi->data; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5204 | sector_t sector = be64_to_cpu(p->sector); |
| 5205 | int blksize = be32_to_cpu(p->blksize); |
| 5206 | |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 5207 | peer_device = conn_peer_device(connection, pi->vnr); |
| 5208 | if (!peer_device) |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5209 | return -EIO; |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 5210 | device = peer_device->device; |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 5211 | |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 5212 | D_ASSERT(device, peer_device->connection->agreed_pro_version >= 89); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5213 | |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 5214 | update_peer_seq(peer_device, be32_to_cpu(p->seq_num)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5215 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5216 | if (get_ldev(device)) { |
| 5217 | drbd_rs_complete_io(device, sector); |
| 5218 | drbd_set_in_sync(device, sector, blksize); |
Lars Ellenberg | 1d53f09 | 2010-09-05 01:13:24 +0200 | [diff] [blame] | 5219 | /* rs_same_csums is supposed to count in units of BM_BLOCK_SIZE */ |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5220 | device->rs_same_csum += (blksize >> BM_BLOCK_SHIFT); |
| 5221 | put_ldev(device); |
Lars Ellenberg | 1d53f09 | 2010-09-05 01:13:24 +0200 | [diff] [blame] | 5222 | } |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5223 | dec_rs_pending(device); |
| 5224 | atomic_add(blksize >> 9, &device->rs_sect_in); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5225 | |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5226 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5227 | } |
| 5228 | |
Andreas Gruenbacher | bc9c5c4 | 2011-01-21 18:00:55 +0100 | [diff] [blame] | 5229 | static int |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5230 | validate_req_change_req_state(struct drbd_device *device, u64 id, sector_t sector, |
Andreas Gruenbacher | bc9c5c4 | 2011-01-21 18:00:55 +0100 | [diff] [blame] | 5231 | struct rb_root *root, const char *func, |
| 5232 | enum drbd_req_event what, bool missing_ok) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5233 | { |
| 5234 | struct drbd_request *req; |
| 5235 | struct bio_and_error m; |
| 5236 | |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 5237 | spin_lock_irq(&device->resource->req_lock); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5238 | req = find_request(device, root, id, sector, missing_ok, func); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5239 | if (unlikely(!req)) { |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 5240 | spin_unlock_irq(&device->resource->req_lock); |
Andreas Gruenbacher | 8599767 | 2011-04-04 13:09:15 +0200 | [diff] [blame] | 5241 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5242 | } |
| 5243 | __req_mod(req, what, &m); |
Andreas Gruenbacher | 0500813 | 2011-07-07 14:19:42 +0200 | [diff] [blame] | 5244 | spin_unlock_irq(&device->resource->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5245 | |
| 5246 | if (m.bio) |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5247 | complete_master_bio(device, &m); |
Andreas Gruenbacher | 8599767 | 2011-04-04 13:09:15 +0200 | [diff] [blame] | 5248 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5249 | } |
| 5250 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5251 | static int got_BlockAck(struct drbd_connection *connection, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5252 | { |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 5253 | struct drbd_peer_device *peer_device; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5254 | struct drbd_device *device; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 5255 | struct p_block_ack *p = pi->data; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5256 | sector_t sector = be64_to_cpu(p->sector); |
| 5257 | int blksize = be32_to_cpu(p->blksize); |
| 5258 | enum drbd_req_event what; |
| 5259 | |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 5260 | peer_device = conn_peer_device(connection, pi->vnr); |
| 5261 | if (!peer_device) |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5262 | return -EIO; |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 5263 | device = peer_device->device; |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 5264 | |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 5265 | update_peer_seq(peer_device, be32_to_cpu(p->seq_num)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5266 | |
Andreas Gruenbacher | 579b57e | 2011-01-13 18:40:57 +0100 | [diff] [blame] | 5267 | if (p->block_id == ID_SYNCER) { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5268 | drbd_set_in_sync(device, sector, blksize); |
| 5269 | dec_rs_pending(device); |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5270 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5271 | } |
Andreas Gruenbacher | e05e1e5 | 2011-03-25 15:16:26 +0100 | [diff] [blame] | 5272 | switch (pi->cmd) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5273 | case P_RS_WRITE_ACK: |
Andreas Gruenbacher | 8554df1 | 2011-01-25 15:37:43 +0100 | [diff] [blame] | 5274 | what = WRITE_ACKED_BY_PEER_AND_SIS; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5275 | break; |
| 5276 | case P_WRITE_ACK: |
Andreas Gruenbacher | 8554df1 | 2011-01-25 15:37:43 +0100 | [diff] [blame] | 5277 | what = WRITE_ACKED_BY_PEER; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5278 | break; |
| 5279 | case P_RECV_ACK: |
Andreas Gruenbacher | 8554df1 | 2011-01-25 15:37:43 +0100 | [diff] [blame] | 5280 | what = RECV_ACKED_BY_PEER; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5281 | break; |
Lars Ellenberg | d4dabbe | 2012-08-01 12:33:51 +0200 | [diff] [blame] | 5282 | case P_SUPERSEDED: |
| 5283 | what = CONFLICT_RESOLVED; |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 5284 | break; |
| 5285 | case P_RETRY_WRITE: |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 5286 | what = POSTPONE_WRITE; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5287 | break; |
| 5288 | default: |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5289 | BUG(); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5290 | } |
| 5291 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5292 | return validate_req_change_req_state(device, p->block_id, sector, |
| 5293 | &device->write_requests, __func__, |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5294 | what, false); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5295 | } |
| 5296 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5297 | static int got_NegAck(struct drbd_connection *connection, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5298 | { |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 5299 | struct drbd_peer_device *peer_device; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5300 | struct drbd_device *device; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 5301 | struct p_block_ack *p = pi->data; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5302 | sector_t sector = be64_to_cpu(p->sector); |
Philipp Reisner | 2deb833 | 2011-01-17 18:39:18 +0100 | [diff] [blame] | 5303 | int size = be32_to_cpu(p->blksize); |
Andreas Gruenbacher | 8599767 | 2011-04-04 13:09:15 +0200 | [diff] [blame] | 5304 | int err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5305 | |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 5306 | peer_device = conn_peer_device(connection, pi->vnr); |
| 5307 | if (!peer_device) |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5308 | return -EIO; |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 5309 | device = peer_device->device; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5310 | |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 5311 | update_peer_seq(peer_device, be32_to_cpu(p->seq_num)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5312 | |
Andreas Gruenbacher | 579b57e | 2011-01-13 18:40:57 +0100 | [diff] [blame] | 5313 | if (p->block_id == ID_SYNCER) { |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5314 | dec_rs_pending(device); |
| 5315 | drbd_rs_failed_io(device, sector, size); |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5316 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5317 | } |
Philipp Reisner | 2deb833 | 2011-01-17 18:39:18 +0100 | [diff] [blame] | 5318 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5319 | err = validate_req_change_req_state(device, p->block_id, sector, |
| 5320 | &device->write_requests, __func__, |
Philipp Reisner | 303d144 | 2011-04-13 16:24:47 -0700 | [diff] [blame] | 5321 | NEG_ACKED, true); |
Andreas Gruenbacher | 8599767 | 2011-04-04 13:09:15 +0200 | [diff] [blame] | 5322 | if (err) { |
Andreas Gruenbacher | c3afd8f | 2011-01-20 22:25:40 +0100 | [diff] [blame] | 5323 | /* Protocol A has no P_WRITE_ACKs, but has P_NEG_ACKs. |
| 5324 | The master bio might already be completed, therefore the |
| 5325 | request is no longer in the collision hash. */ |
| 5326 | /* In Protocol B we might already have got a P_RECV_ACK |
| 5327 | but then get a P_NEG_ACK afterwards. */ |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5328 | drbd_set_out_of_sync(device, sector, size); |
Philipp Reisner | 2deb833 | 2011-01-17 18:39:18 +0100 | [diff] [blame] | 5329 | } |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5330 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5331 | } |
| 5332 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5333 | static int got_NegDReply(struct drbd_connection *connection, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5334 | { |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 5335 | struct drbd_peer_device *peer_device; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5336 | struct drbd_device *device; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 5337 | struct p_block_ack *p = pi->data; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5338 | sector_t sector = be64_to_cpu(p->sector); |
| 5339 | |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 5340 | peer_device = conn_peer_device(connection, pi->vnr); |
| 5341 | if (!peer_device) |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5342 | return -EIO; |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 5343 | device = peer_device->device; |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 5344 | |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 5345 | update_peer_seq(peer_device, be32_to_cpu(p->seq_num)); |
Andreas Gruenbacher | 7be8da0 | 2011-02-22 02:15:32 +0100 | [diff] [blame] | 5346 | |
Andreas Gruenbacher | d018017 | 2011-07-03 17:53:52 +0200 | [diff] [blame] | 5347 | drbd_err(device, "Got NegDReply; Sector %llus, len %u.\n", |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5348 | (unsigned long long)sector, be32_to_cpu(p->blksize)); |
| 5349 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5350 | return validate_req_change_req_state(device, p->block_id, sector, |
| 5351 | &device->read_requests, __func__, |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5352 | NEG_ACKED, false); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5353 | } |
| 5354 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5355 | static int got_NegRSDReply(struct drbd_connection *connection, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5356 | { |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 5357 | struct drbd_peer_device *peer_device; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5358 | struct drbd_device *device; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5359 | sector_t sector; |
| 5360 | int size; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 5361 | struct p_block_ack *p = pi->data; |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 5362 | |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 5363 | peer_device = conn_peer_device(connection, pi->vnr); |
| 5364 | if (!peer_device) |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5365 | return -EIO; |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 5366 | device = peer_device->device; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5367 | |
| 5368 | sector = be64_to_cpu(p->sector); |
| 5369 | size = be32_to_cpu(p->blksize); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5370 | |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 5371 | update_peer_seq(peer_device, be32_to_cpu(p->seq_num)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5372 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5373 | dec_rs_pending(device); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5374 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5375 | if (get_ldev_if_state(device, D_FAILED)) { |
| 5376 | drbd_rs_complete_io(device, sector); |
Andreas Gruenbacher | e05e1e5 | 2011-03-25 15:16:26 +0100 | [diff] [blame] | 5377 | switch (pi->cmd) { |
Philipp Reisner | d612d30 | 2010-12-27 10:53:28 +0100 | [diff] [blame] | 5378 | case P_NEG_RS_DREPLY: |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5379 | drbd_rs_failed_io(device, sector, size); |
Philipp Reisner | d612d30 | 2010-12-27 10:53:28 +0100 | [diff] [blame] | 5380 | case P_RS_CANCEL: |
| 5381 | break; |
| 5382 | default: |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5383 | BUG(); |
Philipp Reisner | d612d30 | 2010-12-27 10:53:28 +0100 | [diff] [blame] | 5384 | } |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5385 | put_ldev(device); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5386 | } |
| 5387 | |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5388 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5389 | } |
| 5390 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5391 | static int got_BarrierAck(struct drbd_connection *connection, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5392 | { |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 5393 | struct p_barrier_ack *p = pi->data; |
Andreas Gruenbacher | c06ece6 | 2011-06-21 17:23:59 +0200 | [diff] [blame] | 5394 | struct drbd_peer_device *peer_device; |
Lars Ellenberg | 9ed57dc | 2012-03-26 20:55:17 +0200 | [diff] [blame] | 5395 | int vnr; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5396 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5397 | tl_release(connection, p->barrier, be32_to_cpu(p->set_size)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5398 | |
Lars Ellenberg | 9ed57dc | 2012-03-26 20:55:17 +0200 | [diff] [blame] | 5399 | rcu_read_lock(); |
Andreas Gruenbacher | c06ece6 | 2011-06-21 17:23:59 +0200 | [diff] [blame] | 5400 | idr_for_each_entry(&connection->peer_devices, peer_device, vnr) { |
| 5401 | struct drbd_device *device = peer_device->device; |
| 5402 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5403 | if (device->state.conn == C_AHEAD && |
| 5404 | atomic_read(&device->ap_in_flight) == 0 && |
| 5405 | !test_and_set_bit(AHEAD_TO_SYNC_SOURCE, &device->flags)) { |
| 5406 | device->start_resync_timer.expires = jiffies + HZ; |
| 5407 | add_timer(&device->start_resync_timer); |
Lars Ellenberg | 9ed57dc | 2012-03-26 20:55:17 +0200 | [diff] [blame] | 5408 | } |
Philipp Reisner | c4752ef | 2010-10-27 17:32:36 +0200 | [diff] [blame] | 5409 | } |
Lars Ellenberg | 9ed57dc | 2012-03-26 20:55:17 +0200 | [diff] [blame] | 5410 | rcu_read_unlock(); |
Philipp Reisner | c4752ef | 2010-10-27 17:32:36 +0200 | [diff] [blame] | 5411 | |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5412 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5413 | } |
| 5414 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5415 | static int got_OVResult(struct drbd_connection *connection, struct packet_info *pi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5416 | { |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 5417 | struct drbd_peer_device *peer_device; |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5418 | struct drbd_device *device; |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 5419 | struct p_block_ack *p = pi->data; |
Andreas Gruenbacher | 84b8c06 | 2011-07-28 15:27:51 +0200 | [diff] [blame] | 5420 | struct drbd_device_work *dw; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5421 | sector_t sector; |
| 5422 | int size; |
| 5423 | |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 5424 | peer_device = conn_peer_device(connection, pi->vnr); |
| 5425 | if (!peer_device) |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5426 | return -EIO; |
Andreas Gruenbacher | 9f4fe9a | 2011-08-09 03:54:55 +0200 | [diff] [blame] | 5427 | device = peer_device->device; |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 5428 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5429 | sector = be64_to_cpu(p->sector); |
| 5430 | size = be32_to_cpu(p->blksize); |
| 5431 | |
Andreas Gruenbacher | 69a2277 | 2011-08-09 00:47:13 +0200 | [diff] [blame] | 5432 | update_peer_seq(peer_device, be32_to_cpu(p->seq_num)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5433 | |
| 5434 | if (be64_to_cpu(p->block_id) == ID_OUT_OF_SYNC) |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5435 | drbd_ov_out_of_sync_found(device, sector, size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5436 | else |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5437 | ov_out_of_sync_print(device); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5438 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5439 | if (!get_ldev(device)) |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5440 | return 0; |
Lars Ellenberg | 1d53f09 | 2010-09-05 01:13:24 +0200 | [diff] [blame] | 5441 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5442 | drbd_rs_complete_io(device, sector); |
| 5443 | dec_rs_pending(device); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5444 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5445 | --device->ov_left; |
Lars Ellenberg | ea5442a | 2010-11-05 09:48:01 +0100 | [diff] [blame] | 5446 | |
| 5447 | /* let's advance progress step marks only for every other megabyte */ |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5448 | if ((device->ov_left & 0x200) == 0x200) |
| 5449 | drbd_advance_rs_marks(device, device->ov_left); |
Lars Ellenberg | ea5442a | 2010-11-05 09:48:01 +0100 | [diff] [blame] | 5450 | |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5451 | if (device->ov_left == 0) { |
Andreas Gruenbacher | 84b8c06 | 2011-07-28 15:27:51 +0200 | [diff] [blame] | 5452 | dw = kmalloc(sizeof(*dw), GFP_NOIO); |
| 5453 | if (dw) { |
| 5454 | dw->w.cb = w_ov_finished; |
| 5455 | dw->device = device; |
| 5456 | drbd_queue_work(&peer_device->connection->sender_work, &dw->w); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5457 | } else { |
Andreas Gruenbacher | 84b8c06 | 2011-07-28 15:27:51 +0200 | [diff] [blame] | 5458 | drbd_err(device, "kmalloc(dw) failed."); |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5459 | ov_out_of_sync_print(device); |
| 5460 | drbd_resync_finished(device); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5461 | } |
| 5462 | } |
Andreas Gruenbacher | b30ab79 | 2011-07-03 13:26:43 +0200 | [diff] [blame] | 5463 | put_ldev(device); |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5464 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5465 | } |
| 5466 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5467 | static int got_skip(struct drbd_connection *connection, struct packet_info *pi) |
Philipp Reisner | 0ced55a | 2010-04-30 15:26:20 +0200 | [diff] [blame] | 5468 | { |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5469 | return 0; |
Philipp Reisner | 0ced55a | 2010-04-30 15:26:20 +0200 | [diff] [blame] | 5470 | } |
| 5471 | |
Philipp Reisner | 668700b | 2015-03-16 16:08:29 +0100 | [diff] [blame] | 5472 | struct meta_sock_cmd { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5473 | size_t pkt_size; |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5474 | int (*fn)(struct drbd_connection *connection, struct packet_info *); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5475 | }; |
| 5476 | |
Philipp Reisner | 668700b | 2015-03-16 16:08:29 +0100 | [diff] [blame] | 5477 | static void set_rcvtimeo(struct drbd_connection *connection, bool ping_timeout) |
| 5478 | { |
| 5479 | long t; |
| 5480 | struct net_conf *nc; |
| 5481 | |
| 5482 | rcu_read_lock(); |
| 5483 | nc = rcu_dereference(connection->net_conf); |
| 5484 | t = ping_timeout ? nc->ping_timeo : nc->ping_int; |
| 5485 | rcu_read_unlock(); |
| 5486 | |
| 5487 | t *= HZ; |
| 5488 | if (ping_timeout) |
| 5489 | t /= 10; |
| 5490 | |
| 5491 | connection->meta.socket->sk->sk_rcvtimeo = t; |
| 5492 | } |
| 5493 | |
| 5494 | static void set_ping_timeout(struct drbd_connection *connection) |
| 5495 | { |
| 5496 | set_rcvtimeo(connection, 1); |
| 5497 | } |
| 5498 | |
| 5499 | static void set_idle_timeout(struct drbd_connection *connection) |
| 5500 | { |
| 5501 | set_rcvtimeo(connection, 0); |
| 5502 | } |
| 5503 | |
| 5504 | static struct meta_sock_cmd ack_receiver_tbl[] = { |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 5505 | [P_PING] = { 0, got_Ping }, |
| 5506 | [P_PING_ACK] = { 0, got_PingAck }, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5507 | [P_RECV_ACK] = { sizeof(struct p_block_ack), got_BlockAck }, |
| 5508 | [P_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck }, |
| 5509 | [P_RS_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck }, |
Lars Ellenberg | d4dabbe | 2012-08-01 12:33:51 +0200 | [diff] [blame] | 5510 | [P_SUPERSEDED] = { sizeof(struct p_block_ack), got_BlockAck }, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5511 | [P_NEG_ACK] = { sizeof(struct p_block_ack), got_NegAck }, |
| 5512 | [P_NEG_DREPLY] = { sizeof(struct p_block_ack), got_NegDReply }, |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 5513 | [P_NEG_RS_DREPLY] = { sizeof(struct p_block_ack), got_NegRSDReply }, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5514 | [P_OV_RESULT] = { sizeof(struct p_block_ack), got_OVResult }, |
| 5515 | [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), got_BarrierAck }, |
| 5516 | [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), got_RqSReply }, |
| 5517 | [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), got_IsInSync }, |
Philipp Reisner | 02918be | 2010-08-20 14:35:10 +0200 | [diff] [blame] | 5518 | [P_DELAY_PROBE] = { sizeof(struct p_delay_probe93), got_skip }, |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 5519 | [P_RS_CANCEL] = { sizeof(struct p_block_ack), got_NegRSDReply }, |
| 5520 | [P_CONN_ST_CHG_REPLY]={ sizeof(struct p_req_state_reply), got_conn_RqSReply }, |
| 5521 | [P_RETRY_WRITE] = { sizeof(struct p_block_ack), got_BlockAck }, |
Andreas Gruenbacher | 7201b97 | 2011-03-14 18:23:00 +0100 | [diff] [blame] | 5522 | }; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5523 | |
Philipp Reisner | 1c03e52 | 2015-03-16 15:01:00 +0100 | [diff] [blame] | 5524 | int drbd_ack_receiver(struct drbd_thread *thi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5525 | { |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5526 | struct drbd_connection *connection = thi->connection; |
Philipp Reisner | 668700b | 2015-03-16 16:08:29 +0100 | [diff] [blame] | 5527 | struct meta_sock_cmd *cmd = NULL; |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 5528 | struct packet_info pi; |
Philipp Reisner | 668700b | 2015-03-16 16:08:29 +0100 | [diff] [blame] | 5529 | unsigned long pre_recv_jif; |
Philipp Reisner | 257d0af | 2011-01-26 12:15:29 +0100 | [diff] [blame] | 5530 | int rv; |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5531 | void *buf = connection->meta.rbuf; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5532 | int received = 0; |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5533 | unsigned int header_size = drbd_header_size(connection); |
Andreas Gruenbacher | 52b061a | 2011-03-30 11:38:49 +0200 | [diff] [blame] | 5534 | int expect = header_size; |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 5535 | bool ping_timeout_active = false; |
Philipp Reisner | 3990e04 | 2013-03-27 14:08:48 +0100 | [diff] [blame] | 5536 | struct sched_param param = { .sched_priority = 2 }; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5537 | |
Philipp Reisner | 3990e04 | 2013-03-27 14:08:48 +0100 | [diff] [blame] | 5538 | rv = sched_setscheduler(current, SCHED_RR, ¶m); |
| 5539 | if (rv < 0) |
Philipp Reisner | 668700b | 2015-03-16 16:08:29 +0100 | [diff] [blame] | 5540 | drbd_err(connection, "drbd_ack_receiver: ERROR set priority, ret=%d\n", rv); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5541 | |
Andreas Gruenbacher | e77a0a5 | 2011-01-25 15:43:39 +0100 | [diff] [blame] | 5542 | while (get_t_state(thi) == RUNNING) { |
Philipp Reisner | 8082228 | 2011-02-08 12:46:30 +0100 | [diff] [blame] | 5543 | drbd_thread_current_set_cpu(thi); |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 5544 | |
Philipp Reisner | 668700b | 2015-03-16 16:08:29 +0100 | [diff] [blame] | 5545 | conn_reclaim_net_peer_reqs(connection); |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 5546 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5547 | if (test_and_clear_bit(SEND_PING, &connection->flags)) { |
| 5548 | if (drbd_send_ping(connection)) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 5549 | drbd_err(connection, "drbd_send_ping has failed\n"); |
Andreas Gruenbacher | 841ce24 | 2010-12-15 19:31:20 +0100 | [diff] [blame] | 5550 | goto reconnect; |
| 5551 | } |
Philipp Reisner | 668700b | 2015-03-16 16:08:29 +0100 | [diff] [blame] | 5552 | set_ping_timeout(connection); |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 5553 | ping_timeout_active = true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5554 | } |
| 5555 | |
Philipp Reisner | 668700b | 2015-03-16 16:08:29 +0100 | [diff] [blame] | 5556 | pre_recv_jif = jiffies; |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5557 | rv = drbd_recv_short(connection->meta.socket, buf, expect-received, 0); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5558 | |
| 5559 | /* Note: |
| 5560 | * -EINTR (on meta) we got a signal |
| 5561 | * -EAGAIN (on meta) rcvtimeo expired |
| 5562 | * -ECONNRESET other side closed the connection |
| 5563 | * -ERESTARTSYS (on data) we got a signal |
| 5564 | * rv < 0 other than above: unexpected error! |
| 5565 | * rv == expected: full header or command |
| 5566 | * rv < expected: "woken" by signal during receive |
| 5567 | * rv == 0 : "connection shut down by peer" |
| 5568 | */ |
| 5569 | if (likely(rv > 0)) { |
| 5570 | received += rv; |
| 5571 | buf += rv; |
| 5572 | } else if (rv == 0) { |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5573 | if (test_bit(DISCONNECT_SENT, &connection->flags)) { |
Philipp Reisner | b66623e | 2012-08-08 21:19:09 +0200 | [diff] [blame] | 5574 | long t; |
| 5575 | rcu_read_lock(); |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5576 | t = rcu_dereference(connection->net_conf)->ping_timeo * HZ/10; |
Philipp Reisner | b66623e | 2012-08-08 21:19:09 +0200 | [diff] [blame] | 5577 | rcu_read_unlock(); |
| 5578 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5579 | t = wait_event_timeout(connection->ping_wait, |
| 5580 | connection->cstate < C_WF_REPORT_PARAMS, |
Philipp Reisner | b66623e | 2012-08-08 21:19:09 +0200 | [diff] [blame] | 5581 | t); |
Philipp Reisner | 599377a | 2012-08-17 14:50:22 +0200 | [diff] [blame] | 5582 | if (t) |
| 5583 | break; |
| 5584 | } |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 5585 | drbd_err(connection, "meta connection shut down by peer.\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5586 | goto reconnect; |
| 5587 | } else if (rv == -EAGAIN) { |
Lars Ellenberg | cb6518c | 2011-06-20 14:44:45 +0200 | [diff] [blame] | 5588 | /* If the data socket received something meanwhile, |
| 5589 | * that is good enough: peer is still alive. */ |
Philipp Reisner | 668700b | 2015-03-16 16:08:29 +0100 | [diff] [blame] | 5590 | if (time_after(connection->last_received, pre_recv_jif)) |
Lars Ellenberg | cb6518c | 2011-06-20 14:44:45 +0200 | [diff] [blame] | 5591 | continue; |
Lars Ellenberg | f36af18 | 2011-03-09 22:44:55 +0100 | [diff] [blame] | 5592 | if (ping_timeout_active) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 5593 | drbd_err(connection, "PingAck did not arrive in time.\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5594 | goto reconnect; |
| 5595 | } |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5596 | set_bit(SEND_PING, &connection->flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5597 | continue; |
| 5598 | } else if (rv == -EINTR) { |
Philipp Reisner | 668700b | 2015-03-16 16:08:29 +0100 | [diff] [blame] | 5599 | /* maybe drbd_thread_stop(): the while condition will notice. |
| 5600 | * maybe woken for send_ping: we'll send a ping above, |
| 5601 | * and change the rcvtimeo */ |
| 5602 | flush_signals(current); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5603 | continue; |
| 5604 | } else { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 5605 | drbd_err(connection, "sock_recvmsg returned %d\n", rv); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5606 | goto reconnect; |
| 5607 | } |
| 5608 | |
| 5609 | if (received == expect && cmd == NULL) { |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5610 | if (decode_header(connection, connection->meta.rbuf, &pi)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5611 | goto reconnect; |
Philipp Reisner | 668700b | 2015-03-16 16:08:29 +0100 | [diff] [blame] | 5612 | cmd = &ack_receiver_tbl[pi.cmd]; |
| 5613 | if (pi.cmd >= ARRAY_SIZE(ack_receiver_tbl) || !cmd->fn) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 5614 | drbd_err(connection, "Unexpected meta packet %s (0x%04x)\n", |
Andreas Gruenbacher | 2fcb8f3 | 2011-07-03 11:41:08 +0200 | [diff] [blame] | 5615 | cmdname(pi.cmd), pi.cmd); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5616 | goto disconnect; |
| 5617 | } |
Andreas Gruenbacher | e658983 | 2011-03-30 12:54:42 +0200 | [diff] [blame] | 5618 | expect = header_size + cmd->pkt_size; |
Andreas Gruenbacher | 52b061a | 2011-03-30 11:38:49 +0200 | [diff] [blame] | 5619 | if (pi.size != expect - header_size) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 5620 | drbd_err(connection, "Wrong packet size on meta (c: %d, l: %d)\n", |
Philipp Reisner | 77351055b | 2011-02-07 17:24:26 +0100 | [diff] [blame] | 5621 | pi.cmd, pi.size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5622 | goto reconnect; |
Philipp Reisner | 257d0af | 2011-01-26 12:15:29 +0100 | [diff] [blame] | 5623 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5624 | } |
| 5625 | if (received == expect) { |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5626 | bool err; |
Philipp Reisner | a4fbda8 | 2011-03-16 11:13:17 +0100 | [diff] [blame] | 5627 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5628 | err = cmd->fn(connection, &pi); |
Andreas Gruenbacher | 2735a59 | 2011-04-04 15:30:24 +0200 | [diff] [blame] | 5629 | if (err) { |
Andreas Gruenbacher | 1ec861e | 2011-07-06 11:01:44 +0200 | [diff] [blame] | 5630 | drbd_err(connection, "%pf failed\n", cmd->fn); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5631 | goto reconnect; |
Andreas Gruenbacher | 1952e91 | 2011-03-25 15:37:43 +0100 | [diff] [blame] | 5632 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5633 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5634 | connection->last_received = jiffies; |
Lars Ellenberg | f36af18 | 2011-03-09 22:44:55 +0100 | [diff] [blame] | 5635 | |
Philipp Reisner | 668700b | 2015-03-16 16:08:29 +0100 | [diff] [blame] | 5636 | if (cmd == &ack_receiver_tbl[P_PING_ACK]) { |
| 5637 | set_idle_timeout(connection); |
Philipp Reisner | 44ed167 | 2011-04-19 17:10:19 +0200 | [diff] [blame] | 5638 | ping_timeout_active = false; |
| 5639 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5640 | |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5641 | buf = connection->meta.rbuf; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5642 | received = 0; |
Andreas Gruenbacher | 52b061a | 2011-03-30 11:38:49 +0200 | [diff] [blame] | 5643 | expect = header_size; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5644 | cmd = NULL; |
| 5645 | } |
| 5646 | } |
| 5647 | |
| 5648 | if (0) { |
| 5649 | reconnect: |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5650 | conn_request_state(connection, NS(conn, C_NETWORK_FAILURE), CS_HARD); |
| 5651 | conn_md_sync(connection); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5652 | } |
| 5653 | if (0) { |
| 5654 | disconnect: |
Andreas Gruenbacher | bde89a9 | 2011-05-30 16:32:41 +0200 | [diff] [blame] | 5655 | conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_HARD); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5656 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5657 | |
Philipp Reisner | 668700b | 2015-03-16 16:08:29 +0100 | [diff] [blame] | 5658 | drbd_info(connection, "ack_receiver terminated\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 5659 | |
| 5660 | return 0; |
| 5661 | } |
Philipp Reisner | 668700b | 2015-03-16 16:08:29 +0100 | [diff] [blame] | 5662 | |
| 5663 | void drbd_send_acks_wf(struct work_struct *ws) |
| 5664 | { |
| 5665 | struct drbd_peer_device *peer_device = |
| 5666 | container_of(ws, struct drbd_peer_device, send_acks_work); |
| 5667 | struct drbd_connection *connection = peer_device->connection; |
| 5668 | struct drbd_device *device = peer_device->device; |
| 5669 | struct net_conf *nc; |
| 5670 | int tcp_cork, err; |
| 5671 | |
| 5672 | rcu_read_lock(); |
| 5673 | nc = rcu_dereference(connection->net_conf); |
| 5674 | tcp_cork = nc->tcp_cork; |
| 5675 | rcu_read_unlock(); |
| 5676 | |
| 5677 | if (tcp_cork) |
| 5678 | drbd_tcp_cork(connection->meta.socket); |
| 5679 | |
| 5680 | err = drbd_finish_peer_reqs(device); |
| 5681 | kref_put(&device->kref, drbd_destroy_device); |
| 5682 | /* get is in drbd_endio_write_sec_final(). That is necessary to keep the |
| 5683 | struct work_struct send_acks_work alive, which is in the peer_device object */ |
| 5684 | |
| 5685 | if (err) { |
| 5686 | conn_request_state(connection, NS(conn, C_NETWORK_FAILURE), CS_HARD); |
| 5687 | return; |
| 5688 | } |
| 5689 | |
| 5690 | if (tcp_cork) |
| 5691 | drbd_tcp_uncork(connection->meta.socket); |
| 5692 | |
| 5693 | return; |
| 5694 | } |