Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | drbd.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 | Thanks to Carter Burden, Bart Grantham and Gennadiy Nerubayev |
| 11 | from Logicworks, Inc. for making SDP replication support possible. |
| 12 | |
| 13 | drbd is free software; you can redistribute it and/or modify |
| 14 | it under the terms of the GNU General Public License as published by |
| 15 | the Free Software Foundation; either version 2, or (at your option) |
| 16 | any later version. |
| 17 | |
| 18 | drbd is distributed in the hope that it will be useful, |
| 19 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | GNU General Public License for more details. |
| 22 | |
| 23 | You should have received a copy of the GNU General Public License |
| 24 | along with drbd; see the file COPYING. If not, write to |
| 25 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
| 26 | |
| 27 | */ |
| 28 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 29 | #include <linux/module.h> |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 30 | #include <linux/drbd.h> |
| 31 | #include <asm/uaccess.h> |
| 32 | #include <asm/types.h> |
| 33 | #include <net/sock.h> |
| 34 | #include <linux/ctype.h> |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 35 | #include <linux/mutex.h> |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 36 | #include <linux/fs.h> |
| 37 | #include <linux/file.h> |
| 38 | #include <linux/proc_fs.h> |
| 39 | #include <linux/init.h> |
| 40 | #include <linux/mm.h> |
| 41 | #include <linux/memcontrol.h> |
| 42 | #include <linux/mm_inline.h> |
| 43 | #include <linux/slab.h> |
| 44 | #include <linux/random.h> |
| 45 | #include <linux/reboot.h> |
| 46 | #include <linux/notifier.h> |
| 47 | #include <linux/kthread.h> |
| 48 | |
| 49 | #define __KERNEL_SYSCALLS__ |
| 50 | #include <linux/unistd.h> |
| 51 | #include <linux/vmalloc.h> |
| 52 | |
| 53 | #include <linux/drbd_limits.h> |
| 54 | #include "drbd_int.h" |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 55 | #include "drbd_req.h" /* only for _req_mod in tl_release and tl_clear */ |
| 56 | |
| 57 | #include "drbd_vli.h" |
| 58 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 59 | static DEFINE_MUTEX(drbd_main_mutex); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 60 | int drbdd_init(struct drbd_thread *); |
| 61 | int drbd_worker(struct drbd_thread *); |
| 62 | int drbd_asender(struct drbd_thread *); |
| 63 | |
| 64 | int drbd_init(void); |
| 65 | static int drbd_open(struct block_device *bdev, fmode_t mode); |
| 66 | static int drbd_release(struct gendisk *gd, fmode_t mode); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 67 | static int w_md_sync(struct drbd_conf *mdev, struct drbd_work *w, int unused); |
| 68 | static void md_sync_timer_fn(unsigned long data); |
| 69 | static int w_bitmap_io(struct drbd_conf *mdev, struct drbd_work *w, int unused); |
Lars Ellenberg | e9e6f3e | 2010-09-14 20:26:27 +0200 | [diff] [blame] | 70 | static int w_go_diskless(struct drbd_conf *mdev, struct drbd_work *w, int unused); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 71 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 72 | MODULE_AUTHOR("Philipp Reisner <phil@linbit.com>, " |
| 73 | "Lars Ellenberg <lars@linbit.com>"); |
| 74 | MODULE_DESCRIPTION("drbd - Distributed Replicated Block Device v" REL_VERSION); |
| 75 | MODULE_VERSION(REL_VERSION); |
| 76 | MODULE_LICENSE("GPL"); |
Philipp Reisner | 2b8a90b | 2011-01-10 11:15:17 +0100 | [diff] [blame] | 77 | MODULE_PARM_DESC(minor_count, "Maximum number of drbd devices (" |
| 78 | __stringify(DRBD_MINOR_COUNT_MIN) "-" __stringify(DRBD_MINOR_COUNT_MAX) ")"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 79 | MODULE_ALIAS_BLOCKDEV_MAJOR(DRBD_MAJOR); |
| 80 | |
| 81 | #include <linux/moduleparam.h> |
| 82 | /* allow_open_on_secondary */ |
| 83 | MODULE_PARM_DESC(allow_oos, "DONT USE!"); |
| 84 | /* thanks to these macros, if compiled into the kernel (not-module), |
| 85 | * this becomes the boot parameter drbd.minor_count */ |
| 86 | module_param(minor_count, uint, 0444); |
| 87 | module_param(disable_sendpage, bool, 0644); |
| 88 | module_param(allow_oos, bool, 0); |
| 89 | module_param(cn_idx, uint, 0444); |
| 90 | module_param(proc_details, int, 0644); |
| 91 | |
| 92 | #ifdef CONFIG_DRBD_FAULT_INJECTION |
| 93 | int enable_faults; |
| 94 | int fault_rate; |
| 95 | static int fault_count; |
| 96 | int fault_devs; |
| 97 | /* bitmap of enabled faults */ |
| 98 | module_param(enable_faults, int, 0664); |
| 99 | /* fault rate % value - applies to all enabled faults */ |
| 100 | module_param(fault_rate, int, 0664); |
| 101 | /* count of faults inserted */ |
| 102 | module_param(fault_count, int, 0664); |
| 103 | /* bitmap of devices to insert faults on */ |
| 104 | module_param(fault_devs, int, 0644); |
| 105 | #endif |
| 106 | |
| 107 | /* module parameter, defined */ |
Philipp Reisner | 2b8a90b | 2011-01-10 11:15:17 +0100 | [diff] [blame] | 108 | unsigned int minor_count = DRBD_MINOR_COUNT_DEF; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 109 | int disable_sendpage; |
| 110 | int allow_oos; |
| 111 | unsigned int cn_idx = CN_IDX_DRBD; |
| 112 | int proc_details; /* Detail level in proc drbd*/ |
| 113 | |
| 114 | /* Module parameter for setting the user mode helper program |
| 115 | * to run. Default is /sbin/drbdadm */ |
| 116 | char usermode_helper[80] = "/sbin/drbdadm"; |
| 117 | |
| 118 | module_param_string(usermode_helper, usermode_helper, sizeof(usermode_helper), 0644); |
| 119 | |
| 120 | /* in 2.6.x, our device mapping and config info contains our virtual gendisks |
| 121 | * as member "struct gendisk *vdisk;" |
| 122 | */ |
| 123 | struct drbd_conf **minor_table; |
Philipp Reisner | 2111438 | 2011-01-19 12:26:59 +0100 | [diff] [blame] | 124 | struct list_head drbd_tconns; /* list of struct drbd_tconn */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 125 | |
| 126 | struct kmem_cache *drbd_request_cache; |
Andreas Gruenbacher | 6c852be | 2011-02-04 15:38:52 +0100 | [diff] [blame] | 127 | struct kmem_cache *drbd_ee_cache; /* peer requests */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 128 | struct kmem_cache *drbd_bm_ext_cache; /* bitmap extents */ |
| 129 | struct kmem_cache *drbd_al_ext_cache; /* activity log extents */ |
| 130 | mempool_t *drbd_request_mempool; |
| 131 | mempool_t *drbd_ee_mempool; |
| 132 | |
| 133 | /* I do not use a standard mempool, because: |
| 134 | 1) I want to hand out the pre-allocated objects first. |
| 135 | 2) I want to be able to interrupt sleeping allocation with a signal. |
| 136 | Note: This is a single linked list, the next pointer is the private |
| 137 | member of struct page. |
| 138 | */ |
| 139 | struct page *drbd_pp_pool; |
| 140 | spinlock_t drbd_pp_lock; |
| 141 | int drbd_pp_vacant; |
| 142 | wait_queue_head_t drbd_pp_wait; |
| 143 | |
| 144 | DEFINE_RATELIMIT_STATE(drbd_ratelimit_state, 5 * HZ, 5); |
| 145 | |
Emese Revfy | 7d4e9d0 | 2009-12-14 00:59:30 +0100 | [diff] [blame] | 146 | static const struct block_device_operations drbd_ops = { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 147 | .owner = THIS_MODULE, |
| 148 | .open = drbd_open, |
| 149 | .release = drbd_release, |
| 150 | }; |
| 151 | |
| 152 | #define ARRY_SIZE(A) (sizeof(A)/sizeof(A[0])) |
| 153 | |
| 154 | #ifdef __CHECKER__ |
| 155 | /* When checking with sparse, and this is an inline function, sparse will |
| 156 | give tons of false positives. When this is a real functions sparse works. |
| 157 | */ |
| 158 | int _get_ldev_if_state(struct drbd_conf *mdev, enum drbd_disk_state mins) |
| 159 | { |
| 160 | int io_allowed; |
| 161 | |
| 162 | atomic_inc(&mdev->local_cnt); |
| 163 | io_allowed = (mdev->state.disk >= mins); |
| 164 | if (!io_allowed) { |
| 165 | if (atomic_dec_and_test(&mdev->local_cnt)) |
| 166 | wake_up(&mdev->misc_wait); |
| 167 | } |
| 168 | return io_allowed; |
| 169 | } |
| 170 | |
| 171 | #endif |
| 172 | |
| 173 | /** |
| 174 | * DOC: The transfer log |
| 175 | * |
| 176 | * The transfer log is a single linked list of &struct drbd_tl_epoch objects. |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 177 | * mdev->tconn->newest_tle points to the head, mdev->tconn->oldest_tle points to the tail |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 178 | * of the list. There is always at least one &struct drbd_tl_epoch object. |
| 179 | * |
| 180 | * Each &struct drbd_tl_epoch has a circular double linked list of requests |
| 181 | * attached. |
| 182 | */ |
| 183 | static int tl_init(struct drbd_conf *mdev) |
| 184 | { |
| 185 | struct drbd_tl_epoch *b; |
| 186 | |
| 187 | /* during device minor initialization, we may well use GFP_KERNEL */ |
| 188 | b = kmalloc(sizeof(struct drbd_tl_epoch), GFP_KERNEL); |
| 189 | if (!b) |
| 190 | return 0; |
| 191 | INIT_LIST_HEAD(&b->requests); |
| 192 | INIT_LIST_HEAD(&b->w.list); |
| 193 | b->next = NULL; |
| 194 | b->br_number = 4711; |
Philipp Reisner | 7e602c0 | 2010-05-27 14:49:27 +0200 | [diff] [blame] | 195 | b->n_writes = 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 196 | b->w.cb = NULL; /* if this is != NULL, we need to dec_ap_pending in tl_clear */ |
| 197 | |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 198 | mdev->tconn->oldest_tle = b; |
| 199 | mdev->tconn->newest_tle = b; |
| 200 | INIT_LIST_HEAD(&mdev->tconn->out_of_sequence_requests); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 201 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 202 | return 1; |
| 203 | } |
| 204 | |
| 205 | static void tl_cleanup(struct drbd_conf *mdev) |
| 206 | { |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 207 | D_ASSERT(mdev->tconn->oldest_tle == mdev->tconn->newest_tle); |
| 208 | D_ASSERT(list_empty(&mdev->tconn->out_of_sequence_requests)); |
| 209 | kfree(mdev->tconn->oldest_tle); |
| 210 | mdev->tconn->oldest_tle = NULL; |
| 211 | kfree(mdev->tconn->unused_spare_tle); |
| 212 | mdev->tconn->unused_spare_tle = NULL; |
Andreas Gruenbacher | d628769 | 2011-01-13 23:05:39 +0100 | [diff] [blame] | 213 | } |
| 214 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 215 | /** |
| 216 | * _tl_add_barrier() - Adds a barrier to the transfer log |
| 217 | * @mdev: DRBD device. |
| 218 | * @new: Barrier to be added before the current head of the TL. |
| 219 | * |
| 220 | * The caller must hold the req_lock. |
| 221 | */ |
| 222 | void _tl_add_barrier(struct drbd_conf *mdev, struct drbd_tl_epoch *new) |
| 223 | { |
| 224 | struct drbd_tl_epoch *newest_before; |
| 225 | |
| 226 | INIT_LIST_HEAD(&new->requests); |
| 227 | INIT_LIST_HEAD(&new->w.list); |
| 228 | new->w.cb = NULL; /* if this is != NULL, we need to dec_ap_pending in tl_clear */ |
| 229 | new->next = NULL; |
Philipp Reisner | 7e602c0 | 2010-05-27 14:49:27 +0200 | [diff] [blame] | 230 | new->n_writes = 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 231 | |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 232 | newest_before = mdev->tconn->newest_tle; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 233 | /* never send a barrier number == 0, because that is special-cased |
| 234 | * when using TCQ for our write ordering code */ |
| 235 | new->br_number = (newest_before->br_number+1) ?: 1; |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 236 | if (mdev->tconn->newest_tle != new) { |
| 237 | mdev->tconn->newest_tle->next = new; |
| 238 | mdev->tconn->newest_tle = new; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * tl_release() - Free or recycle the oldest &struct drbd_tl_epoch object of the TL |
| 244 | * @mdev: DRBD device. |
| 245 | * @barrier_nr: Expected identifier of the DRBD write barrier packet. |
| 246 | * @set_size: Expected number of requests before that barrier. |
| 247 | * |
| 248 | * In case the passed barrier_nr or set_size does not match the oldest |
| 249 | * &struct drbd_tl_epoch objects this function will cause a termination |
| 250 | * of the connection. |
| 251 | */ |
| 252 | void tl_release(struct drbd_conf *mdev, unsigned int barrier_nr, |
| 253 | unsigned int set_size) |
| 254 | { |
| 255 | struct drbd_tl_epoch *b, *nob; /* next old barrier */ |
| 256 | struct list_head *le, *tle; |
| 257 | struct drbd_request *r; |
| 258 | |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 259 | spin_lock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 260 | |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 261 | b = mdev->tconn->oldest_tle; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 262 | |
| 263 | /* first some paranoia code */ |
| 264 | if (b == NULL) { |
| 265 | dev_err(DEV, "BAD! BarrierAck #%u received, but no epoch in tl!?\n", |
| 266 | barrier_nr); |
| 267 | goto bail; |
| 268 | } |
| 269 | if (b->br_number != barrier_nr) { |
| 270 | dev_err(DEV, "BAD! BarrierAck #%u received, expected #%u!\n", |
| 271 | barrier_nr, b->br_number); |
| 272 | goto bail; |
| 273 | } |
Philipp Reisner | 7e602c0 | 2010-05-27 14:49:27 +0200 | [diff] [blame] | 274 | if (b->n_writes != set_size) { |
| 275 | dev_err(DEV, "BAD! BarrierAck #%u received with n_writes=%u, expected n_writes=%u!\n", |
| 276 | barrier_nr, set_size, b->n_writes); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 277 | goto bail; |
| 278 | } |
| 279 | |
| 280 | /* Clean up list of requests processed during current epoch */ |
| 281 | list_for_each_safe(le, tle, &b->requests) { |
| 282 | r = list_entry(le, struct drbd_request, tl_requests); |
Andreas Gruenbacher | 8554df1 | 2011-01-25 15:37:43 +0100 | [diff] [blame] | 283 | _req_mod(r, BARRIER_ACKED); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 284 | } |
| 285 | /* There could be requests on the list waiting for completion |
| 286 | of the write to the local disk. To avoid corruptions of |
| 287 | slab's data structures we have to remove the lists head. |
| 288 | |
| 289 | Also there could have been a barrier ack out of sequence, overtaking |
| 290 | the write acks - which would be a bug and violating write ordering. |
| 291 | To not deadlock in case we lose connection while such requests are |
| 292 | still pending, we need some way to find them for the |
Andreas Gruenbacher | 8554df1 | 2011-01-25 15:37:43 +0100 | [diff] [blame] | 293 | _req_mode(CONNECTION_LOST_WHILE_PENDING). |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 294 | |
| 295 | These have been list_move'd to the out_of_sequence_requests list in |
Andreas Gruenbacher | 8554df1 | 2011-01-25 15:37:43 +0100 | [diff] [blame] | 296 | _req_mod(, BARRIER_ACKED) above. |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 297 | */ |
| 298 | list_del_init(&b->requests); |
| 299 | |
| 300 | nob = b->next; |
| 301 | if (test_and_clear_bit(CREATE_BARRIER, &mdev->flags)) { |
| 302 | _tl_add_barrier(mdev, b); |
| 303 | if (nob) |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 304 | mdev->tconn->oldest_tle = nob; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 305 | /* if nob == NULL b was the only barrier, and becomes the new |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 306 | barrier. Therefore mdev->tconn->oldest_tle points already to b */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 307 | } else { |
| 308 | D_ASSERT(nob != NULL); |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 309 | mdev->tconn->oldest_tle = nob; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 310 | kfree(b); |
| 311 | } |
| 312 | |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 313 | spin_unlock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 314 | dec_ap_pending(mdev); |
| 315 | |
| 316 | return; |
| 317 | |
| 318 | bail: |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 319 | spin_unlock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 320 | drbd_force_state(mdev, NS(conn, C_PROTOCOL_ERROR)); |
| 321 | } |
| 322 | |
Philipp Reisner | 617049a | 2010-12-22 12:48:31 +0100 | [diff] [blame] | 323 | |
Philipp Reisner | 11b58e7 | 2010-05-12 17:08:26 +0200 | [diff] [blame] | 324 | /** |
| 325 | * _tl_restart() - Walks the transfer log, and applies an action to all requests |
| 326 | * @mdev: DRBD device. |
| 327 | * @what: The action/event to perform with all request objects |
| 328 | * |
Andreas Gruenbacher | 8554df1 | 2011-01-25 15:37:43 +0100 | [diff] [blame] | 329 | * @what might be one of CONNECTION_LOST_WHILE_PENDING, RESEND, FAIL_FROZEN_DISK_IO, |
| 330 | * RESTART_FROZEN_DISK_IO. |
Philipp Reisner | 11b58e7 | 2010-05-12 17:08:26 +0200 | [diff] [blame] | 331 | */ |
Philipp Reisner | b890733 | 2011-01-27 14:07:51 +0100 | [diff] [blame] | 332 | void _tl_restart(struct drbd_conf *mdev, enum drbd_req_event what) |
Philipp Reisner | 11b58e7 | 2010-05-12 17:08:26 +0200 | [diff] [blame] | 333 | { |
| 334 | struct drbd_tl_epoch *b, *tmp, **pn; |
Philipp Reisner | b9b9871 | 2010-06-22 11:26:48 +0200 | [diff] [blame] | 335 | struct list_head *le, *tle, carry_reads; |
Philipp Reisner | 11b58e7 | 2010-05-12 17:08:26 +0200 | [diff] [blame] | 336 | struct drbd_request *req; |
| 337 | int rv, n_writes, n_reads; |
| 338 | |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 339 | b = mdev->tconn->oldest_tle; |
| 340 | pn = &mdev->tconn->oldest_tle; |
Philipp Reisner | 11b58e7 | 2010-05-12 17:08:26 +0200 | [diff] [blame] | 341 | while (b) { |
| 342 | n_writes = 0; |
| 343 | n_reads = 0; |
Philipp Reisner | b9b9871 | 2010-06-22 11:26:48 +0200 | [diff] [blame] | 344 | INIT_LIST_HEAD(&carry_reads); |
Philipp Reisner | 11b58e7 | 2010-05-12 17:08:26 +0200 | [diff] [blame] | 345 | list_for_each_safe(le, tle, &b->requests) { |
| 346 | req = list_entry(le, struct drbd_request, tl_requests); |
| 347 | rv = _req_mod(req, what); |
| 348 | |
| 349 | n_writes += (rv & MR_WRITE) >> MR_WRITE_SHIFT; |
| 350 | n_reads += (rv & MR_READ) >> MR_READ_SHIFT; |
| 351 | } |
| 352 | tmp = b->next; |
| 353 | |
Philipp Reisner | b9b9871 | 2010-06-22 11:26:48 +0200 | [diff] [blame] | 354 | if (n_writes) { |
Andreas Gruenbacher | 8554df1 | 2011-01-25 15:37:43 +0100 | [diff] [blame] | 355 | if (what == RESEND) { |
Philipp Reisner | 11b58e7 | 2010-05-12 17:08:26 +0200 | [diff] [blame] | 356 | b->n_writes = n_writes; |
| 357 | if (b->w.cb == NULL) { |
| 358 | b->w.cb = w_send_barrier; |
| 359 | inc_ap_pending(mdev); |
| 360 | set_bit(CREATE_BARRIER, &mdev->flags); |
| 361 | } |
| 362 | |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 363 | drbd_queue_work(&mdev->tconn->data.work, &b->w); |
Philipp Reisner | 11b58e7 | 2010-05-12 17:08:26 +0200 | [diff] [blame] | 364 | } |
| 365 | pn = &b->next; |
| 366 | } else { |
Philipp Reisner | b9b9871 | 2010-06-22 11:26:48 +0200 | [diff] [blame] | 367 | if (n_reads) |
| 368 | list_add(&carry_reads, &b->requests); |
Philipp Reisner | 11b58e7 | 2010-05-12 17:08:26 +0200 | [diff] [blame] | 369 | /* there could still be requests on that ring list, |
| 370 | * in case local io is still pending */ |
| 371 | list_del(&b->requests); |
| 372 | |
| 373 | /* dec_ap_pending corresponding to queue_barrier. |
| 374 | * the newest barrier may not have been queued yet, |
| 375 | * in which case w.cb is still NULL. */ |
| 376 | if (b->w.cb != NULL) |
| 377 | dec_ap_pending(mdev); |
| 378 | |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 379 | if (b == mdev->tconn->newest_tle) { |
Philipp Reisner | 11b58e7 | 2010-05-12 17:08:26 +0200 | [diff] [blame] | 380 | /* recycle, but reinit! */ |
| 381 | D_ASSERT(tmp == NULL); |
| 382 | INIT_LIST_HEAD(&b->requests); |
Philipp Reisner | b9b9871 | 2010-06-22 11:26:48 +0200 | [diff] [blame] | 383 | list_splice(&carry_reads, &b->requests); |
Philipp Reisner | 11b58e7 | 2010-05-12 17:08:26 +0200 | [diff] [blame] | 384 | INIT_LIST_HEAD(&b->w.list); |
| 385 | b->w.cb = NULL; |
| 386 | b->br_number = net_random(); |
| 387 | b->n_writes = 0; |
| 388 | |
| 389 | *pn = b; |
| 390 | break; |
| 391 | } |
| 392 | *pn = tmp; |
| 393 | kfree(b); |
| 394 | } |
| 395 | b = tmp; |
Philipp Reisner | b9b9871 | 2010-06-22 11:26:48 +0200 | [diff] [blame] | 396 | list_splice(&carry_reads, &b->requests); |
Philipp Reisner | 11b58e7 | 2010-05-12 17:08:26 +0200 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 400 | |
| 401 | /** |
| 402 | * tl_clear() - Clears all requests and &struct drbd_tl_epoch objects out of the TL |
| 403 | * @mdev: DRBD device. |
| 404 | * |
| 405 | * This is called after the connection to the peer was lost. The storage covered |
| 406 | * by the requests on the transfer gets marked as our of sync. Called from the |
| 407 | * receiver thread and the worker thread. |
| 408 | */ |
| 409 | void tl_clear(struct drbd_conf *mdev) |
| 410 | { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 411 | struct list_head *le, *tle; |
| 412 | struct drbd_request *r; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 413 | |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 414 | spin_lock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 415 | |
Andreas Gruenbacher | 8554df1 | 2011-01-25 15:37:43 +0100 | [diff] [blame] | 416 | _tl_restart(mdev, CONNECTION_LOST_WHILE_PENDING); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 417 | |
| 418 | /* we expect this list to be empty. */ |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 419 | D_ASSERT(list_empty(&mdev->tconn->out_of_sequence_requests)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 420 | |
| 421 | /* but just in case, clean it up anyways! */ |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 422 | list_for_each_safe(le, tle, &mdev->tconn->out_of_sequence_requests) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 423 | r = list_entry(le, struct drbd_request, tl_requests); |
| 424 | /* It would be nice to complete outside of spinlock. |
| 425 | * But this is easier for now. */ |
Andreas Gruenbacher | 8554df1 | 2011-01-25 15:37:43 +0100 | [diff] [blame] | 426 | _req_mod(r, CONNECTION_LOST_WHILE_PENDING); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | /* ensure bit indicating barrier is required is clear */ |
| 430 | clear_bit(CREATE_BARRIER, &mdev->flags); |
| 431 | |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 432 | spin_unlock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Philipp Reisner | 11b58e7 | 2010-05-12 17:08:26 +0200 | [diff] [blame] | 435 | void tl_restart(struct drbd_conf *mdev, enum drbd_req_event what) |
| 436 | { |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 437 | spin_lock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | 11b58e7 | 2010-05-12 17:08:26 +0200 | [diff] [blame] | 438 | _tl_restart(mdev, what); |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 439 | spin_unlock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 442 | static int drbd_thread_setup(void *arg) |
| 443 | { |
| 444 | struct drbd_thread *thi = (struct drbd_thread *) arg; |
| 445 | struct drbd_conf *mdev = thi->mdev; |
| 446 | unsigned long flags; |
| 447 | int retval; |
| 448 | |
| 449 | restart: |
| 450 | retval = thi->function(thi); |
| 451 | |
| 452 | spin_lock_irqsave(&thi->t_lock, flags); |
| 453 | |
Andreas Gruenbacher | e77a0a5 | 2011-01-25 15:43:39 +0100 | [diff] [blame] | 454 | /* if the receiver has been "EXITING", the last thing it did |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 455 | * was set the conn state to "StandAlone", |
| 456 | * if now a re-connect request comes in, conn state goes C_UNCONNECTED, |
| 457 | * and receiver thread will be "started". |
Andreas Gruenbacher | e77a0a5 | 2011-01-25 15:43:39 +0100 | [diff] [blame] | 458 | * drbd_thread_start needs to set "RESTARTING" in that case. |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 459 | * t_state check and assignment needs to be within the same spinlock, |
Andreas Gruenbacher | e77a0a5 | 2011-01-25 15:43:39 +0100 | [diff] [blame] | 460 | * so either thread_start sees EXITING, and can remap to RESTARTING, |
| 461 | * or thread_start see NONE, and can proceed as normal. |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 462 | */ |
| 463 | |
Andreas Gruenbacher | e77a0a5 | 2011-01-25 15:43:39 +0100 | [diff] [blame] | 464 | if (thi->t_state == RESTARTING) { |
Philipp Reisner | bed879a | 2011-02-04 14:00:37 +0100 | [diff] [blame] | 465 | dev_info(DEV, "Restarting %s thread\n", thi->name); |
Andreas Gruenbacher | e77a0a5 | 2011-01-25 15:43:39 +0100 | [diff] [blame] | 466 | thi->t_state = RUNNING; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 467 | spin_unlock_irqrestore(&thi->t_lock, flags); |
| 468 | goto restart; |
| 469 | } |
| 470 | |
| 471 | thi->task = NULL; |
Andreas Gruenbacher | e77a0a5 | 2011-01-25 15:43:39 +0100 | [diff] [blame] | 472 | thi->t_state = NONE; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 473 | smp_mb(); |
| 474 | complete(&thi->stop); |
| 475 | spin_unlock_irqrestore(&thi->t_lock, flags); |
| 476 | |
| 477 | dev_info(DEV, "Terminating %s\n", current->comm); |
| 478 | |
| 479 | /* Release mod reference taken when thread was started */ |
| 480 | module_put(THIS_MODULE); |
| 481 | return retval; |
| 482 | } |
| 483 | |
| 484 | static void drbd_thread_init(struct drbd_conf *mdev, struct drbd_thread *thi, |
Philipp Reisner | bed879a | 2011-02-04 14:00:37 +0100 | [diff] [blame] | 485 | int (*func) (struct drbd_thread *), char *name) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 486 | { |
| 487 | spin_lock_init(&thi->t_lock); |
| 488 | thi->task = NULL; |
Andreas Gruenbacher | e77a0a5 | 2011-01-25 15:43:39 +0100 | [diff] [blame] | 489 | thi->t_state = NONE; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 490 | thi->function = func; |
| 491 | thi->mdev = mdev; |
Philipp Reisner | bed879a | 2011-02-04 14:00:37 +0100 | [diff] [blame] | 492 | strncpy(thi->name, name, ARRAY_SIZE(thi->name)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | int drbd_thread_start(struct drbd_thread *thi) |
| 496 | { |
| 497 | struct drbd_conf *mdev = thi->mdev; |
| 498 | struct task_struct *nt; |
| 499 | unsigned long flags; |
| 500 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 501 | /* is used from state engine doing drbd_thread_stop_nowait, |
| 502 | * while holding the req lock irqsave */ |
| 503 | spin_lock_irqsave(&thi->t_lock, flags); |
| 504 | |
| 505 | switch (thi->t_state) { |
Andreas Gruenbacher | e77a0a5 | 2011-01-25 15:43:39 +0100 | [diff] [blame] | 506 | case NONE: |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 507 | dev_info(DEV, "Starting %s thread (from %s [%d])\n", |
Philipp Reisner | bed879a | 2011-02-04 14:00:37 +0100 | [diff] [blame] | 508 | thi->name, current->comm, current->pid); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 509 | |
| 510 | /* Get ref on module for thread - this is released when thread exits */ |
| 511 | if (!try_module_get(THIS_MODULE)) { |
| 512 | dev_err(DEV, "Failed to get module reference in drbd_thread_start\n"); |
| 513 | spin_unlock_irqrestore(&thi->t_lock, flags); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 514 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | init_completion(&thi->stop); |
| 518 | D_ASSERT(thi->task == NULL); |
| 519 | thi->reset_cpu_mask = 1; |
Andreas Gruenbacher | e77a0a5 | 2011-01-25 15:43:39 +0100 | [diff] [blame] | 520 | thi->t_state = RUNNING; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 521 | spin_unlock_irqrestore(&thi->t_lock, flags); |
| 522 | flush_signals(current); /* otherw. may get -ERESTARTNOINTR */ |
| 523 | |
| 524 | nt = kthread_create(drbd_thread_setup, (void *) thi, |
Philipp Reisner | bed879a | 2011-02-04 14:00:37 +0100 | [diff] [blame] | 525 | "drbd%d_%s", mdev_to_minor(mdev), thi->name); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 526 | |
| 527 | if (IS_ERR(nt)) { |
| 528 | dev_err(DEV, "Couldn't start thread\n"); |
| 529 | |
| 530 | module_put(THIS_MODULE); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 531 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 532 | } |
| 533 | spin_lock_irqsave(&thi->t_lock, flags); |
| 534 | thi->task = nt; |
Andreas Gruenbacher | e77a0a5 | 2011-01-25 15:43:39 +0100 | [diff] [blame] | 535 | thi->t_state = RUNNING; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 536 | spin_unlock_irqrestore(&thi->t_lock, flags); |
| 537 | wake_up_process(nt); |
| 538 | break; |
Andreas Gruenbacher | e77a0a5 | 2011-01-25 15:43:39 +0100 | [diff] [blame] | 539 | case EXITING: |
| 540 | thi->t_state = RESTARTING; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 541 | dev_info(DEV, "Restarting %s thread (from %s [%d])\n", |
Philipp Reisner | bed879a | 2011-02-04 14:00:37 +0100 | [diff] [blame] | 542 | thi->name, current->comm, current->pid); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 543 | /* fall through */ |
Andreas Gruenbacher | e77a0a5 | 2011-01-25 15:43:39 +0100 | [diff] [blame] | 544 | case RUNNING: |
| 545 | case RESTARTING: |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 546 | default: |
| 547 | spin_unlock_irqrestore(&thi->t_lock, flags); |
| 548 | break; |
| 549 | } |
| 550 | |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 551 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | |
| 555 | void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait) |
| 556 | { |
| 557 | unsigned long flags; |
| 558 | |
Andreas Gruenbacher | e77a0a5 | 2011-01-25 15:43:39 +0100 | [diff] [blame] | 559 | enum drbd_thread_state ns = restart ? RESTARTING : EXITING; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 560 | |
| 561 | /* may be called from state engine, holding the req lock irqsave */ |
| 562 | spin_lock_irqsave(&thi->t_lock, flags); |
| 563 | |
Andreas Gruenbacher | e77a0a5 | 2011-01-25 15:43:39 +0100 | [diff] [blame] | 564 | if (thi->t_state == NONE) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 565 | spin_unlock_irqrestore(&thi->t_lock, flags); |
| 566 | if (restart) |
| 567 | drbd_thread_start(thi); |
| 568 | return; |
| 569 | } |
| 570 | |
| 571 | if (thi->t_state != ns) { |
| 572 | if (thi->task == NULL) { |
| 573 | spin_unlock_irqrestore(&thi->t_lock, flags); |
| 574 | return; |
| 575 | } |
| 576 | |
| 577 | thi->t_state = ns; |
| 578 | smp_mb(); |
| 579 | init_completion(&thi->stop); |
| 580 | if (thi->task != current) |
| 581 | force_sig(DRBD_SIGKILL, thi->task); |
| 582 | |
| 583 | } |
| 584 | |
| 585 | spin_unlock_irqrestore(&thi->t_lock, flags); |
| 586 | |
| 587 | if (wait) |
| 588 | wait_for_completion(&thi->stop); |
| 589 | } |
| 590 | |
Philipp Reisner | bed879a | 2011-02-04 14:00:37 +0100 | [diff] [blame] | 591 | static struct drbd_thread *drbd_task_to_thread(struct drbd_conf *mdev, struct task_struct *task) |
| 592 | { |
| 593 | struct drbd_tconn *tconn = mdev->tconn; |
| 594 | struct drbd_thread *thi = |
| 595 | task == tconn->receiver.task ? &tconn->receiver : |
| 596 | task == tconn->asender.task ? &tconn->asender : |
| 597 | task == tconn->worker.task ? &tconn->worker : NULL; |
| 598 | |
| 599 | return thi; |
| 600 | } |
| 601 | |
| 602 | char *drbd_task_to_thread_name(struct drbd_conf *mdev, struct task_struct *task) |
| 603 | { |
| 604 | struct drbd_thread *thi = drbd_task_to_thread(mdev, task); |
| 605 | return thi ? thi->name : task->comm; |
| 606 | } |
| 607 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 608 | #ifdef CONFIG_SMP |
| 609 | /** |
| 610 | * drbd_calc_cpu_mask() - Generate CPU masks, spread over all CPUs |
| 611 | * @mdev: DRBD device. |
| 612 | * |
| 613 | * Forces all threads of a device onto the same CPU. This is beneficial for |
| 614 | * DRBD's performance. May be overwritten by user's configuration. |
| 615 | */ |
| 616 | void drbd_calc_cpu_mask(struct drbd_conf *mdev) |
| 617 | { |
| 618 | int ord, cpu; |
| 619 | |
| 620 | /* user override. */ |
| 621 | if (cpumask_weight(mdev->cpu_mask)) |
| 622 | return; |
| 623 | |
| 624 | ord = mdev_to_minor(mdev) % cpumask_weight(cpu_online_mask); |
| 625 | for_each_online_cpu(cpu) { |
| 626 | if (ord-- == 0) { |
| 627 | cpumask_set_cpu(cpu, mdev->cpu_mask); |
| 628 | return; |
| 629 | } |
| 630 | } |
| 631 | /* should not be reached */ |
| 632 | cpumask_setall(mdev->cpu_mask); |
| 633 | } |
| 634 | |
| 635 | /** |
| 636 | * drbd_thread_current_set_cpu() - modifies the cpu mask of the _current_ thread |
| 637 | * @mdev: DRBD device. |
Philipp Reisner | bc31fe3 | 2011-02-07 11:14:38 +0100 | [diff] [blame] | 638 | * @thi: drbd_thread object |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 639 | * |
| 640 | * call in the "main loop" of _all_ threads, no need for any mutex, current won't die |
| 641 | * prematurely. |
| 642 | */ |
Philipp Reisner | bc31fe3 | 2011-02-07 11:14:38 +0100 | [diff] [blame] | 643 | void drbd_thread_current_set_cpu(struct drbd_conf *mdev, struct drbd_thread *thi) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 644 | { |
| 645 | struct task_struct *p = current; |
Philipp Reisner | bed879a | 2011-02-04 14:00:37 +0100 | [diff] [blame] | 646 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 647 | if (!thi->reset_cpu_mask) |
| 648 | return; |
| 649 | thi->reset_cpu_mask = 0; |
| 650 | set_cpus_allowed_ptr(p, mdev->cpu_mask); |
| 651 | } |
| 652 | #endif |
| 653 | |
Philipp Reisner | d38e787 | 2011-02-07 15:32:04 +0100 | [diff] [blame] | 654 | static void prepare_header80(struct p_header80 *h, enum drbd_packet cmd, int size) |
Philipp Reisner | fd340c1 | 2011-01-19 16:57:39 +0100 | [diff] [blame] | 655 | { |
| 656 | h->magic = cpu_to_be32(DRBD_MAGIC); |
| 657 | h->command = cpu_to_be16(cmd); |
| 658 | h->length = cpu_to_be16(size); |
| 659 | } |
| 660 | |
Philipp Reisner | d38e787 | 2011-02-07 15:32:04 +0100 | [diff] [blame] | 661 | static void prepare_header95(struct p_header95 *h, enum drbd_packet cmd, int size) |
Philipp Reisner | fd340c1 | 2011-01-19 16:57:39 +0100 | [diff] [blame] | 662 | { |
| 663 | h->magic = cpu_to_be16(DRBD_MAGIC_BIG); |
| 664 | h->command = cpu_to_be16(cmd); |
| 665 | h->length = cpu_to_be32(size); |
| 666 | } |
| 667 | |
Philipp Reisner | d38e787 | 2011-02-07 15:32:04 +0100 | [diff] [blame] | 668 | static void _prepare_header(struct drbd_tconn *tconn, int vnr, struct p_header *h, |
| 669 | enum drbd_packet cmd, int size) |
| 670 | { |
| 671 | if (tconn->agreed_pro_version >= 100 || size > DRBD_MAX_SIZE_H80_PACKET) |
| 672 | prepare_header95(&h->h95, cmd, size); |
| 673 | else |
| 674 | prepare_header80(&h->h80, cmd, size); |
| 675 | } |
| 676 | |
Philipp Reisner | fd340c1 | 2011-01-19 16:57:39 +0100 | [diff] [blame] | 677 | static void prepare_header(struct drbd_conf *mdev, struct p_header *h, |
Andreas Gruenbacher | d876302 | 2011-01-26 17:39:41 +0100 | [diff] [blame] | 678 | enum drbd_packet cmd, int size) |
Philipp Reisner | fd340c1 | 2011-01-19 16:57:39 +0100 | [diff] [blame] | 679 | { |
Philipp Reisner | d38e787 | 2011-02-07 15:32:04 +0100 | [diff] [blame] | 680 | _prepare_header(mdev->tconn, mdev->vnr, h, cmd, size); |
Philipp Reisner | fd340c1 | 2011-01-19 16:57:39 +0100 | [diff] [blame] | 681 | } |
| 682 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 683 | /* the appropriate socket mutex must be held already */ |
Philipp Reisner | d38e787 | 2011-02-07 15:32:04 +0100 | [diff] [blame] | 684 | int _conn_send_cmd(struct drbd_tconn *tconn, int vnr, struct socket *sock, |
Andreas Gruenbacher | d876302 | 2011-01-26 17:39:41 +0100 | [diff] [blame] | 685 | enum drbd_packet cmd, struct p_header *h, size_t size, |
| 686 | unsigned msg_flags) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 687 | { |
| 688 | int sent, ok; |
| 689 | |
Philipp Reisner | d38e787 | 2011-02-07 15:32:04 +0100 | [diff] [blame] | 690 | _prepare_header(tconn, vnr, h, cmd, size - sizeof(struct p_header)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 691 | |
Philipp Reisner | d38e787 | 2011-02-07 15:32:04 +0100 | [diff] [blame] | 692 | sent = drbd_send(tconn, sock, h, size, msg_flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 693 | |
| 694 | ok = (sent == size); |
Lars Ellenberg | 0ddc554 | 2011-01-21 12:35:15 +0100 | [diff] [blame] | 695 | if (!ok && !signal_pending(current)) |
Philipp Reisner | d38e787 | 2011-02-07 15:32:04 +0100 | [diff] [blame] | 696 | conn_warn(tconn, "short sent %s size=%d sent=%d\n", |
| 697 | cmdname(cmd), (int)size, sent); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 698 | return ok; |
| 699 | } |
| 700 | |
| 701 | /* don't pass the socket. we may only look at it |
| 702 | * when we hold the appropriate socket mutex. |
| 703 | */ |
| 704 | int drbd_send_cmd(struct drbd_conf *mdev, int use_data_socket, |
Andreas Gruenbacher | d876302 | 2011-01-26 17:39:41 +0100 | [diff] [blame] | 705 | enum drbd_packet cmd, struct p_header *h, size_t size) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 706 | { |
| 707 | int ok = 0; |
| 708 | struct socket *sock; |
| 709 | |
| 710 | if (use_data_socket) { |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 711 | mutex_lock(&mdev->tconn->data.mutex); |
| 712 | sock = mdev->tconn->data.socket; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 713 | } else { |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 714 | mutex_lock(&mdev->tconn->meta.mutex); |
| 715 | sock = mdev->tconn->meta.socket; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | /* drbd_disconnect() could have called drbd_free_sock() |
| 719 | * while we were waiting in down()... */ |
| 720 | if (likely(sock != NULL)) |
| 721 | ok = _drbd_send_cmd(mdev, sock, cmd, h, size, 0); |
| 722 | |
| 723 | if (use_data_socket) |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 724 | mutex_unlock(&mdev->tconn->data.mutex); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 725 | else |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 726 | mutex_unlock(&mdev->tconn->meta.mutex); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 727 | return ok; |
| 728 | } |
| 729 | |
Philipp Reisner | 6112087 | 2011-02-08 09:50:54 +0100 | [diff] [blame] | 730 | int conn_send_cmd2(struct drbd_tconn *tconn, enum drbd_packet cmd, char *data, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 731 | size_t size) |
| 732 | { |
Philipp Reisner | 6112087 | 2011-02-08 09:50:54 +0100 | [diff] [blame] | 733 | struct p_header80 h; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 734 | int ok; |
| 735 | |
Philipp Reisner | 6112087 | 2011-02-08 09:50:54 +0100 | [diff] [blame] | 736 | prepare_header80(&h, cmd, size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 737 | |
Philipp Reisner | 6112087 | 2011-02-08 09:50:54 +0100 | [diff] [blame] | 738 | if (!drbd_get_data_sock(tconn)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 739 | return 0; |
| 740 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 741 | ok = (sizeof(h) == |
Philipp Reisner | 6112087 | 2011-02-08 09:50:54 +0100 | [diff] [blame] | 742 | drbd_send(tconn, tconn->data.socket, &h, sizeof(h), 0)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 743 | ok = ok && (size == |
Philipp Reisner | 6112087 | 2011-02-08 09:50:54 +0100 | [diff] [blame] | 744 | drbd_send(tconn, tconn->data.socket, data, size, 0)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 745 | |
Philipp Reisner | 6112087 | 2011-02-08 09:50:54 +0100 | [diff] [blame] | 746 | drbd_put_data_sock(tconn); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 747 | |
| 748 | return ok; |
| 749 | } |
| 750 | |
| 751 | int drbd_send_sync_param(struct drbd_conf *mdev, struct syncer_conf *sc) |
| 752 | { |
Philipp Reisner | 8e26f9c | 2010-07-06 17:25:54 +0200 | [diff] [blame] | 753 | struct p_rs_param_95 *p; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 754 | struct socket *sock; |
| 755 | int size, rv; |
Philipp Reisner | 31890f4 | 2011-01-19 14:12:51 +0100 | [diff] [blame] | 756 | const int apv = mdev->tconn->agreed_pro_version; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 757 | |
| 758 | size = apv <= 87 ? sizeof(struct p_rs_param) |
| 759 | : apv == 88 ? sizeof(struct p_rs_param) |
| 760 | + strlen(mdev->sync_conf.verify_alg) + 1 |
Philipp Reisner | 8e26f9c | 2010-07-06 17:25:54 +0200 | [diff] [blame] | 761 | : apv <= 94 ? sizeof(struct p_rs_param_89) |
| 762 | : /* apv >= 95 */ sizeof(struct p_rs_param_95); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 763 | |
| 764 | /* used from admin command context and receiver/worker context. |
| 765 | * to avoid kmalloc, grab the socket right here, |
| 766 | * then use the pre-allocated sbuf there */ |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 767 | mutex_lock(&mdev->tconn->data.mutex); |
| 768 | sock = mdev->tconn->data.socket; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 769 | |
| 770 | if (likely(sock != NULL)) { |
Andreas Gruenbacher | d876302 | 2011-01-26 17:39:41 +0100 | [diff] [blame] | 771 | enum drbd_packet cmd = |
| 772 | apv >= 89 ? P_SYNC_PARAM89 : P_SYNC_PARAM; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 773 | |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 774 | p = &mdev->tconn->data.sbuf.rs_param_95; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 775 | |
| 776 | /* initialize verify_alg and csums_alg */ |
| 777 | memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX); |
| 778 | |
| 779 | p->rate = cpu_to_be32(sc->rate); |
Philipp Reisner | 8e26f9c | 2010-07-06 17:25:54 +0200 | [diff] [blame] | 780 | p->c_plan_ahead = cpu_to_be32(sc->c_plan_ahead); |
| 781 | p->c_delay_target = cpu_to_be32(sc->c_delay_target); |
| 782 | p->c_fill_target = cpu_to_be32(sc->c_fill_target); |
| 783 | p->c_max_rate = cpu_to_be32(sc->c_max_rate); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 784 | |
| 785 | if (apv >= 88) |
| 786 | strcpy(p->verify_alg, mdev->sync_conf.verify_alg); |
| 787 | if (apv >= 89) |
| 788 | strcpy(p->csums_alg, mdev->sync_conf.csums_alg); |
| 789 | |
| 790 | rv = _drbd_send_cmd(mdev, sock, cmd, &p->head, size, 0); |
| 791 | } else |
| 792 | rv = 0; /* not ok */ |
| 793 | |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 794 | mutex_unlock(&mdev->tconn->data.mutex); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 795 | |
| 796 | return rv; |
| 797 | } |
| 798 | |
Philipp Reisner | dc8228d | 2011-02-08 10:13:15 +0100 | [diff] [blame] | 799 | int drbd_send_protocol(struct drbd_tconn *tconn) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 800 | { |
| 801 | struct p_protocol *p; |
Philipp Reisner | cf14c2e | 2010-02-02 21:03:50 +0100 | [diff] [blame] | 802 | int size, cf, rv; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 803 | |
| 804 | size = sizeof(struct p_protocol); |
| 805 | |
Philipp Reisner | dc8228d | 2011-02-08 10:13:15 +0100 | [diff] [blame] | 806 | if (tconn->agreed_pro_version >= 87) |
| 807 | size += strlen(tconn->net_conf->integrity_alg) + 1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 808 | |
| 809 | /* we must not recurse into our own queue, |
| 810 | * as that is blocked during handshake */ |
| 811 | p = kmalloc(size, GFP_NOIO); |
| 812 | if (p == NULL) |
| 813 | return 0; |
| 814 | |
Philipp Reisner | dc8228d | 2011-02-08 10:13:15 +0100 | [diff] [blame] | 815 | p->protocol = cpu_to_be32(tconn->net_conf->wire_protocol); |
| 816 | p->after_sb_0p = cpu_to_be32(tconn->net_conf->after_sb_0p); |
| 817 | p->after_sb_1p = cpu_to_be32(tconn->net_conf->after_sb_1p); |
| 818 | p->after_sb_2p = cpu_to_be32(tconn->net_conf->after_sb_2p); |
| 819 | p->two_primaries = cpu_to_be32(tconn->net_conf->two_primaries); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 820 | |
Philipp Reisner | cf14c2e | 2010-02-02 21:03:50 +0100 | [diff] [blame] | 821 | cf = 0; |
Philipp Reisner | dc8228d | 2011-02-08 10:13:15 +0100 | [diff] [blame] | 822 | if (tconn->net_conf->want_lose) |
Philipp Reisner | cf14c2e | 2010-02-02 21:03:50 +0100 | [diff] [blame] | 823 | cf |= CF_WANT_LOSE; |
Philipp Reisner | dc8228d | 2011-02-08 10:13:15 +0100 | [diff] [blame] | 824 | if (tconn->net_conf->dry_run) { |
| 825 | if (tconn->agreed_pro_version >= 92) |
Philipp Reisner | cf14c2e | 2010-02-02 21:03:50 +0100 | [diff] [blame] | 826 | cf |= CF_DRY_RUN; |
| 827 | else { |
Philipp Reisner | dc8228d | 2011-02-08 10:13:15 +0100 | [diff] [blame] | 828 | conn_err(tconn, "--dry-run is not supported by peer"); |
Dan Carpenter | 7ac314c | 2010-04-22 14:27:23 +0200 | [diff] [blame] | 829 | kfree(p); |
Philipp Reisner | 148efa1 | 2011-01-15 00:21:15 +0100 | [diff] [blame] | 830 | return -1; |
Philipp Reisner | cf14c2e | 2010-02-02 21:03:50 +0100 | [diff] [blame] | 831 | } |
| 832 | } |
| 833 | p->conn_flags = cpu_to_be32(cf); |
| 834 | |
Philipp Reisner | dc8228d | 2011-02-08 10:13:15 +0100 | [diff] [blame] | 835 | if (tconn->agreed_pro_version >= 87) |
| 836 | strcpy(p->integrity_alg, tconn->net_conf->integrity_alg); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 837 | |
Philipp Reisner | dc8228d | 2011-02-08 10:13:15 +0100 | [diff] [blame] | 838 | rv = conn_send_cmd2(tconn, P_PROTOCOL, p->head.payload, size - sizeof(struct p_header)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 839 | kfree(p); |
| 840 | return rv; |
| 841 | } |
| 842 | |
| 843 | int _drbd_send_uuids(struct drbd_conf *mdev, u64 uuid_flags) |
| 844 | { |
| 845 | struct p_uuids p; |
| 846 | int i; |
| 847 | |
| 848 | if (!get_ldev_if_state(mdev, D_NEGOTIATING)) |
| 849 | return 1; |
| 850 | |
| 851 | for (i = UI_CURRENT; i < UI_SIZE; i++) |
| 852 | p.uuid[i] = mdev->ldev ? cpu_to_be64(mdev->ldev->md.uuid[i]) : 0; |
| 853 | |
| 854 | mdev->comm_bm_set = drbd_bm_total_weight(mdev); |
| 855 | p.uuid[UI_SIZE] = cpu_to_be64(mdev->comm_bm_set); |
Philipp Reisner | 89e58e7 | 2011-01-19 13:12:45 +0100 | [diff] [blame] | 856 | uuid_flags |= mdev->tconn->net_conf->want_lose ? 1 : 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 857 | uuid_flags |= test_bit(CRASHED_PRIMARY, &mdev->flags) ? 2 : 0; |
| 858 | uuid_flags |= mdev->new_state_tmp.disk == D_INCONSISTENT ? 4 : 0; |
| 859 | p.uuid[UI_FLAGS] = cpu_to_be64(uuid_flags); |
| 860 | |
| 861 | put_ldev(mdev); |
| 862 | |
Philipp Reisner | c012949 | 2011-01-19 16:58:16 +0100 | [diff] [blame] | 863 | return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_UUIDS, &p.head, sizeof(p)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | int drbd_send_uuids(struct drbd_conf *mdev) |
| 867 | { |
| 868 | return _drbd_send_uuids(mdev, 0); |
| 869 | } |
| 870 | |
| 871 | int drbd_send_uuids_skip_initial_sync(struct drbd_conf *mdev) |
| 872 | { |
| 873 | return _drbd_send_uuids(mdev, 8); |
| 874 | } |
| 875 | |
Lars Ellenberg | 62b0da3 | 2011-01-20 13:25:21 +0100 | [diff] [blame] | 876 | void drbd_print_uuids(struct drbd_conf *mdev, const char *text) |
| 877 | { |
| 878 | if (get_ldev_if_state(mdev, D_NEGOTIATING)) { |
| 879 | u64 *uuid = mdev->ldev->md.uuid; |
| 880 | dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX\n", |
| 881 | text, |
| 882 | (unsigned long long)uuid[UI_CURRENT], |
| 883 | (unsigned long long)uuid[UI_BITMAP], |
| 884 | (unsigned long long)uuid[UI_HISTORY_START], |
| 885 | (unsigned long long)uuid[UI_HISTORY_END]); |
| 886 | put_ldev(mdev); |
| 887 | } else { |
| 888 | dev_info(DEV, "%s effective data uuid: %016llX\n", |
| 889 | text, |
| 890 | (unsigned long long)mdev->ed_uuid); |
| 891 | } |
| 892 | } |
| 893 | |
Lars Ellenberg | 5a22db8 | 2010-12-17 21:14:23 +0100 | [diff] [blame] | 894 | int drbd_gen_and_send_sync_uuid(struct drbd_conf *mdev) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 895 | { |
| 896 | struct p_rs_uuid p; |
Lars Ellenberg | 5a22db8 | 2010-12-17 21:14:23 +0100 | [diff] [blame] | 897 | u64 uuid; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 898 | |
Lars Ellenberg | 5a22db8 | 2010-12-17 21:14:23 +0100 | [diff] [blame] | 899 | D_ASSERT(mdev->state.disk == D_UP_TO_DATE); |
| 900 | |
Philipp Reisner | 4a23f26 | 2011-01-11 17:42:17 +0100 | [diff] [blame] | 901 | uuid = mdev->ldev->md.uuid[UI_BITMAP] + UUID_NEW_BM_OFFSET; |
Lars Ellenberg | 5a22db8 | 2010-12-17 21:14:23 +0100 | [diff] [blame] | 902 | drbd_uuid_set(mdev, UI_BITMAP, uuid); |
Lars Ellenberg | 62b0da3 | 2011-01-20 13:25:21 +0100 | [diff] [blame] | 903 | drbd_print_uuids(mdev, "updated sync UUID"); |
Lars Ellenberg | 5a22db8 | 2010-12-17 21:14:23 +0100 | [diff] [blame] | 904 | drbd_md_sync(mdev); |
| 905 | p.uuid = cpu_to_be64(uuid); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 906 | |
Philipp Reisner | c012949 | 2011-01-19 16:58:16 +0100 | [diff] [blame] | 907 | return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_SYNC_UUID, &p.head, sizeof(p)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 908 | } |
| 909 | |
Philipp Reisner | e89b591 | 2010-03-24 17:11:33 +0100 | [diff] [blame] | 910 | int drbd_send_sizes(struct drbd_conf *mdev, int trigger_reply, enum dds_flags flags) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 911 | { |
| 912 | struct p_sizes p; |
| 913 | sector_t d_size, u_size; |
Philipp Reisner | 99432fc | 2011-05-20 16:39:13 +0200 | [diff] [blame] | 914 | int q_order_type, max_bio_size; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 915 | int ok; |
| 916 | |
| 917 | if (get_ldev_if_state(mdev, D_NEGOTIATING)) { |
| 918 | D_ASSERT(mdev->ldev->backing_bdev); |
| 919 | d_size = drbd_get_max_capacity(mdev->ldev); |
| 920 | u_size = mdev->ldev->dc.disk_size; |
| 921 | q_order_type = drbd_queue_order_type(mdev); |
Philipp Reisner | 99432fc | 2011-05-20 16:39:13 +0200 | [diff] [blame] | 922 | max_bio_size = queue_max_hw_sectors(mdev->ldev->backing_bdev->bd_disk->queue) << 9; |
| 923 | max_bio_size = min_t(int, max_bio_size, DRBD_MAX_BIO_SIZE); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 924 | put_ldev(mdev); |
| 925 | } else { |
| 926 | d_size = 0; |
| 927 | u_size = 0; |
| 928 | q_order_type = QUEUE_ORDERED_NONE; |
Philipp Reisner | 99432fc | 2011-05-20 16:39:13 +0200 | [diff] [blame] | 929 | max_bio_size = DRBD_MAX_BIO_SIZE; /* ... multiple BIOs per peer_request */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | p.d_size = cpu_to_be64(d_size); |
| 933 | p.u_size = cpu_to_be64(u_size); |
| 934 | p.c_size = cpu_to_be64(trigger_reply ? 0 : drbd_get_capacity(mdev->this_bdev)); |
Philipp Reisner | 99432fc | 2011-05-20 16:39:13 +0200 | [diff] [blame] | 935 | p.max_bio_size = cpu_to_be32(max_bio_size); |
Philipp Reisner | e89b591 | 2010-03-24 17:11:33 +0100 | [diff] [blame] | 936 | p.queue_order_type = cpu_to_be16(q_order_type); |
| 937 | p.dds_flags = cpu_to_be16(flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 938 | |
Philipp Reisner | c012949 | 2011-01-19 16:58:16 +0100 | [diff] [blame] | 939 | ok = drbd_send_cmd(mdev, USE_DATA_SOCKET, P_SIZES, &p.head, sizeof(p)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 940 | return ok; |
| 941 | } |
| 942 | |
| 943 | /** |
| 944 | * drbd_send_state() - Sends the drbd state to the peer |
| 945 | * @mdev: DRBD device. |
| 946 | */ |
| 947 | int drbd_send_state(struct drbd_conf *mdev) |
| 948 | { |
| 949 | struct socket *sock; |
| 950 | struct p_state p; |
| 951 | int ok = 0; |
| 952 | |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 953 | mutex_lock(&mdev->tconn->data.mutex); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 954 | |
| 955 | p.state = cpu_to_be32(mdev->state.i); /* Within the send mutex */ |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 956 | sock = mdev->tconn->data.socket; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 957 | |
| 958 | if (likely(sock != NULL)) { |
Philipp Reisner | c012949 | 2011-01-19 16:58:16 +0100 | [diff] [blame] | 959 | ok = _drbd_send_cmd(mdev, sock, P_STATE, &p.head, sizeof(p), 0); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 960 | } |
| 961 | |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 962 | mutex_unlock(&mdev->tconn->data.mutex); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 963 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 964 | return ok; |
| 965 | } |
| 966 | |
| 967 | int drbd_send_state_req(struct drbd_conf *mdev, |
| 968 | union drbd_state mask, union drbd_state val) |
| 969 | { |
| 970 | struct p_req_state p; |
| 971 | |
| 972 | p.mask = cpu_to_be32(mask.i); |
| 973 | p.val = cpu_to_be32(val.i); |
| 974 | |
Philipp Reisner | c012949 | 2011-01-19 16:58:16 +0100 | [diff] [blame] | 975 | return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_STATE_CHG_REQ, &p.head, sizeof(p)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 976 | } |
| 977 | |
Andreas Gruenbacher | bf885f8 | 2010-12-08 00:39:32 +0100 | [diff] [blame] | 978 | int drbd_send_sr_reply(struct drbd_conf *mdev, enum drbd_state_rv retcode) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 979 | { |
| 980 | struct p_req_state_reply p; |
| 981 | |
| 982 | p.retcode = cpu_to_be32(retcode); |
| 983 | |
Philipp Reisner | c012949 | 2011-01-19 16:58:16 +0100 | [diff] [blame] | 984 | return drbd_send_cmd(mdev, USE_META_SOCKET, P_STATE_CHG_REPLY, &p.head, sizeof(p)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | int fill_bitmap_rle_bits(struct drbd_conf *mdev, |
| 988 | struct p_compressed_bm *p, |
| 989 | struct bm_xfer_ctx *c) |
| 990 | { |
| 991 | struct bitstream bs; |
| 992 | unsigned long plain_bits; |
| 993 | unsigned long tmp; |
| 994 | unsigned long rl; |
| 995 | unsigned len; |
| 996 | unsigned toggle; |
| 997 | int bits; |
| 998 | |
| 999 | /* may we use this feature? */ |
| 1000 | if ((mdev->sync_conf.use_rle == 0) || |
Philipp Reisner | 31890f4 | 2011-01-19 14:12:51 +0100 | [diff] [blame] | 1001 | (mdev->tconn->agreed_pro_version < 90)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1002 | return 0; |
| 1003 | |
| 1004 | if (c->bit_offset >= c->bm_bits) |
| 1005 | return 0; /* nothing to do. */ |
| 1006 | |
| 1007 | /* use at most thus many bytes */ |
| 1008 | bitstream_init(&bs, p->code, BM_PACKET_VLI_BYTES_MAX, 0); |
| 1009 | memset(p->code, 0, BM_PACKET_VLI_BYTES_MAX); |
| 1010 | /* plain bits covered in this code string */ |
| 1011 | plain_bits = 0; |
| 1012 | |
| 1013 | /* p->encoding & 0x80 stores whether the first run length is set. |
| 1014 | * bit offset is implicit. |
| 1015 | * start with toggle == 2 to be able to tell the first iteration */ |
| 1016 | toggle = 2; |
| 1017 | |
| 1018 | /* see how much plain bits we can stuff into one packet |
| 1019 | * using RLE and VLI. */ |
| 1020 | do { |
| 1021 | tmp = (toggle == 0) ? _drbd_bm_find_next_zero(mdev, c->bit_offset) |
| 1022 | : _drbd_bm_find_next(mdev, c->bit_offset); |
| 1023 | if (tmp == -1UL) |
| 1024 | tmp = c->bm_bits; |
| 1025 | rl = tmp - c->bit_offset; |
| 1026 | |
| 1027 | if (toggle == 2) { /* first iteration */ |
| 1028 | if (rl == 0) { |
| 1029 | /* the first checked bit was set, |
| 1030 | * store start value, */ |
| 1031 | DCBP_set_start(p, 1); |
| 1032 | /* but skip encoding of zero run length */ |
| 1033 | toggle = !toggle; |
| 1034 | continue; |
| 1035 | } |
| 1036 | DCBP_set_start(p, 0); |
| 1037 | } |
| 1038 | |
| 1039 | /* paranoia: catch zero runlength. |
| 1040 | * can only happen if bitmap is modified while we scan it. */ |
| 1041 | if (rl == 0) { |
| 1042 | dev_err(DEV, "unexpected zero runlength while encoding bitmap " |
| 1043 | "t:%u bo:%lu\n", toggle, c->bit_offset); |
| 1044 | return -1; |
| 1045 | } |
| 1046 | |
| 1047 | bits = vli_encode_bits(&bs, rl); |
| 1048 | if (bits == -ENOBUFS) /* buffer full */ |
| 1049 | break; |
| 1050 | if (bits <= 0) { |
| 1051 | dev_err(DEV, "error while encoding bitmap: %d\n", bits); |
| 1052 | return 0; |
| 1053 | } |
| 1054 | |
| 1055 | toggle = !toggle; |
| 1056 | plain_bits += rl; |
| 1057 | c->bit_offset = tmp; |
| 1058 | } while (c->bit_offset < c->bm_bits); |
| 1059 | |
| 1060 | len = bs.cur.b - p->code + !!bs.cur.bit; |
| 1061 | |
| 1062 | if (plain_bits < (len << 3)) { |
| 1063 | /* incompressible with this method. |
| 1064 | * we need to rewind both word and bit position. */ |
| 1065 | c->bit_offset -= plain_bits; |
| 1066 | bm_xfer_ctx_bit_to_word_offset(c); |
| 1067 | c->bit_offset = c->word_offset * BITS_PER_LONG; |
| 1068 | return 0; |
| 1069 | } |
| 1070 | |
| 1071 | /* RLE + VLI was able to compress it just fine. |
| 1072 | * update c->word_offset. */ |
| 1073 | bm_xfer_ctx_bit_to_word_offset(c); |
| 1074 | |
| 1075 | /* store pad_bits */ |
| 1076 | DCBP_set_pad_bits(p, (8 - bs.cur.bit) & 0x7); |
| 1077 | |
| 1078 | return len; |
| 1079 | } |
| 1080 | |
Andreas Gruenbacher | f70af11 | 2010-12-11 18:51:50 +0100 | [diff] [blame] | 1081 | /** |
| 1082 | * send_bitmap_rle_or_plain |
| 1083 | * |
| 1084 | * Return 0 when done, 1 when another iteration is needed, and a negative error |
| 1085 | * code upon failure. |
| 1086 | */ |
| 1087 | static int |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1088 | send_bitmap_rle_or_plain(struct drbd_conf *mdev, |
Philipp Reisner | c012949 | 2011-01-19 16:58:16 +0100 | [diff] [blame] | 1089 | struct p_header *h, struct bm_xfer_ctx *c) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1090 | { |
| 1091 | struct p_compressed_bm *p = (void*)h; |
| 1092 | unsigned long num_words; |
| 1093 | int len; |
| 1094 | int ok; |
| 1095 | |
| 1096 | len = fill_bitmap_rle_bits(mdev, p, c); |
| 1097 | |
| 1098 | if (len < 0) |
Andreas Gruenbacher | f70af11 | 2010-12-11 18:51:50 +0100 | [diff] [blame] | 1099 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1100 | |
| 1101 | if (len) { |
| 1102 | DCBP_set_code(p, RLE_VLI_Bits); |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 1103 | ok = _drbd_send_cmd(mdev, mdev->tconn->data.socket, P_COMPRESSED_BITMAP, h, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1104 | sizeof(*p) + len, 0); |
| 1105 | |
| 1106 | c->packets[0]++; |
| 1107 | c->bytes[0] += sizeof(*p) + len; |
| 1108 | |
| 1109 | if (c->bit_offset >= c->bm_bits) |
| 1110 | len = 0; /* DONE */ |
| 1111 | } else { |
| 1112 | /* was not compressible. |
| 1113 | * send a buffer full of plain text bits instead. */ |
| 1114 | num_words = min_t(size_t, BM_PACKET_WORDS, c->bm_words - c->word_offset); |
| 1115 | len = num_words * sizeof(long); |
| 1116 | if (len) |
| 1117 | drbd_bm_get_lel(mdev, c->word_offset, num_words, (unsigned long*)h->payload); |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 1118 | ok = _drbd_send_cmd(mdev, mdev->tconn->data.socket, P_BITMAP, |
Philipp Reisner | 0b70a13 | 2010-08-20 13:36:10 +0200 | [diff] [blame] | 1119 | h, sizeof(struct p_header80) + len, 0); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1120 | c->word_offset += num_words; |
| 1121 | c->bit_offset = c->word_offset * BITS_PER_LONG; |
| 1122 | |
| 1123 | c->packets[1]++; |
Philipp Reisner | 0b70a13 | 2010-08-20 13:36:10 +0200 | [diff] [blame] | 1124 | c->bytes[1] += sizeof(struct p_header80) + len; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1125 | |
| 1126 | if (c->bit_offset > c->bm_bits) |
| 1127 | c->bit_offset = c->bm_bits; |
| 1128 | } |
Andreas Gruenbacher | f70af11 | 2010-12-11 18:51:50 +0100 | [diff] [blame] | 1129 | if (ok) { |
| 1130 | if (len == 0) { |
| 1131 | INFO_bm_xfer_stats(mdev, "send", c); |
| 1132 | return 0; |
| 1133 | } else |
| 1134 | return 1; |
| 1135 | } |
| 1136 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | /* See the comment at receive_bitmap() */ |
| 1140 | int _drbd_send_bitmap(struct drbd_conf *mdev) |
| 1141 | { |
| 1142 | struct bm_xfer_ctx c; |
Philipp Reisner | c012949 | 2011-01-19 16:58:16 +0100 | [diff] [blame] | 1143 | struct p_header *p; |
Andreas Gruenbacher | f70af11 | 2010-12-11 18:51:50 +0100 | [diff] [blame] | 1144 | int err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1145 | |
Andreas Gruenbacher | 841ce24 | 2010-12-15 19:31:20 +0100 | [diff] [blame] | 1146 | if (!expect(mdev->bitmap)) |
| 1147 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1148 | |
| 1149 | /* maybe we should use some per thread scratch page, |
| 1150 | * and allocate that during initial device creation? */ |
Philipp Reisner | c012949 | 2011-01-19 16:58:16 +0100 | [diff] [blame] | 1151 | p = (struct p_header *) __get_free_page(GFP_NOIO); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1152 | if (!p) { |
| 1153 | dev_err(DEV, "failed to allocate one page buffer in %s\n", __func__); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 1154 | return false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | if (get_ldev(mdev)) { |
| 1158 | if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC)) { |
| 1159 | dev_info(DEV, "Writing the whole bitmap, MDF_FullSync was set.\n"); |
| 1160 | drbd_bm_set_all(mdev); |
| 1161 | if (drbd_bm_write(mdev)) { |
| 1162 | /* write_bm did fail! Leave full sync flag set in Meta P_DATA |
| 1163 | * but otherwise process as per normal - need to tell other |
| 1164 | * side that a full resync is required! */ |
| 1165 | dev_err(DEV, "Failed to write bitmap to disk!\n"); |
| 1166 | } else { |
| 1167 | drbd_md_clear_flag(mdev, MDF_FULL_SYNC); |
| 1168 | drbd_md_sync(mdev); |
| 1169 | } |
| 1170 | } |
| 1171 | put_ldev(mdev); |
| 1172 | } |
| 1173 | |
| 1174 | c = (struct bm_xfer_ctx) { |
| 1175 | .bm_bits = drbd_bm_bits(mdev), |
| 1176 | .bm_words = drbd_bm_words(mdev), |
| 1177 | }; |
| 1178 | |
| 1179 | do { |
Andreas Gruenbacher | f70af11 | 2010-12-11 18:51:50 +0100 | [diff] [blame] | 1180 | err = send_bitmap_rle_or_plain(mdev, p, &c); |
| 1181 | } while (err > 0); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1182 | |
| 1183 | free_page((unsigned long) p); |
Andreas Gruenbacher | f70af11 | 2010-12-11 18:51:50 +0100 | [diff] [blame] | 1184 | return err == 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | int drbd_send_bitmap(struct drbd_conf *mdev) |
| 1188 | { |
| 1189 | int err; |
| 1190 | |
Philipp Reisner | 6112087 | 2011-02-08 09:50:54 +0100 | [diff] [blame] | 1191 | if (!drbd_get_data_sock(mdev->tconn)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1192 | return -1; |
| 1193 | err = !_drbd_send_bitmap(mdev); |
Philipp Reisner | 6112087 | 2011-02-08 09:50:54 +0100 | [diff] [blame] | 1194 | drbd_put_data_sock(mdev->tconn); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1195 | return err; |
| 1196 | } |
| 1197 | |
| 1198 | int drbd_send_b_ack(struct drbd_conf *mdev, u32 barrier_nr, u32 set_size) |
| 1199 | { |
| 1200 | int ok; |
| 1201 | struct p_barrier_ack p; |
| 1202 | |
| 1203 | p.barrier = barrier_nr; |
| 1204 | p.set_size = cpu_to_be32(set_size); |
| 1205 | |
| 1206 | if (mdev->state.conn < C_CONNECTED) |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 1207 | return false; |
Philipp Reisner | c012949 | 2011-01-19 16:58:16 +0100 | [diff] [blame] | 1208 | ok = drbd_send_cmd(mdev, USE_META_SOCKET, P_BARRIER_ACK, &p.head, sizeof(p)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1209 | return ok; |
| 1210 | } |
| 1211 | |
| 1212 | /** |
| 1213 | * _drbd_send_ack() - Sends an ack packet |
| 1214 | * @mdev: DRBD device. |
| 1215 | * @cmd: Packet command code. |
| 1216 | * @sector: sector, needs to be in big endian byte order |
| 1217 | * @blksize: size in byte, needs to be in big endian byte order |
| 1218 | * @block_id: Id, big endian byte order |
| 1219 | */ |
Andreas Gruenbacher | d876302 | 2011-01-26 17:39:41 +0100 | [diff] [blame] | 1220 | static int _drbd_send_ack(struct drbd_conf *mdev, enum drbd_packet cmd, |
| 1221 | u64 sector, u32 blksize, u64 block_id) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1222 | { |
| 1223 | int ok; |
| 1224 | struct p_block_ack p; |
| 1225 | |
| 1226 | p.sector = sector; |
| 1227 | p.block_id = block_id; |
| 1228 | p.blksize = blksize; |
| 1229 | p.seq_num = cpu_to_be32(atomic_add_return(1, &mdev->packet_seq)); |
| 1230 | |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 1231 | if (!mdev->tconn->meta.socket || mdev->state.conn < C_CONNECTED) |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 1232 | return false; |
Philipp Reisner | c012949 | 2011-01-19 16:58:16 +0100 | [diff] [blame] | 1233 | ok = drbd_send_cmd(mdev, USE_META_SOCKET, cmd, &p.head, sizeof(p)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1234 | return ok; |
| 1235 | } |
| 1236 | |
Lars Ellenberg | 2b2bf21 | 2010-10-06 11:46:55 +0200 | [diff] [blame] | 1237 | /* dp->sector and dp->block_id already/still in network byte order, |
| 1238 | * data_size is payload size according to dp->head, |
| 1239 | * and may need to be corrected for digest size. */ |
Andreas Gruenbacher | d876302 | 2011-01-26 17:39:41 +0100 | [diff] [blame] | 1240 | int drbd_send_ack_dp(struct drbd_conf *mdev, enum drbd_packet cmd, |
Lars Ellenberg | 2b2bf21 | 2010-10-06 11:46:55 +0200 | [diff] [blame] | 1241 | struct p_data *dp, int data_size) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1242 | { |
Philipp Reisner | a063845 | 2011-01-19 14:31:32 +0100 | [diff] [blame] | 1243 | data_size -= (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_r_tfm) ? |
| 1244 | crypto_hash_digestsize(mdev->tconn->integrity_r_tfm) : 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1245 | return _drbd_send_ack(mdev, cmd, dp->sector, cpu_to_be32(data_size), |
| 1246 | dp->block_id); |
| 1247 | } |
| 1248 | |
Andreas Gruenbacher | d876302 | 2011-01-26 17:39:41 +0100 | [diff] [blame] | 1249 | int drbd_send_ack_rp(struct drbd_conf *mdev, enum drbd_packet cmd, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1250 | struct p_block_req *rp) |
| 1251 | { |
| 1252 | return _drbd_send_ack(mdev, cmd, rp->sector, rp->blksize, rp->block_id); |
| 1253 | } |
| 1254 | |
| 1255 | /** |
| 1256 | * drbd_send_ack() - Sends an ack packet |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1257 | * @mdev: DRBD device |
| 1258 | * @cmd: packet command code |
| 1259 | * @peer_req: peer request |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1260 | */ |
Andreas Gruenbacher | d876302 | 2011-01-26 17:39:41 +0100 | [diff] [blame] | 1261 | int drbd_send_ack(struct drbd_conf *mdev, enum drbd_packet cmd, |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1262 | struct drbd_peer_request *peer_req) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1263 | { |
| 1264 | return _drbd_send_ack(mdev, cmd, |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1265 | cpu_to_be64(peer_req->i.sector), |
| 1266 | cpu_to_be32(peer_req->i.size), |
| 1267 | peer_req->block_id); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | /* This function misuses the block_id field to signal if the blocks |
| 1271 | * are is sync or not. */ |
Andreas Gruenbacher | d876302 | 2011-01-26 17:39:41 +0100 | [diff] [blame] | 1272 | int drbd_send_ack_ex(struct drbd_conf *mdev, enum drbd_packet cmd, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1273 | sector_t sector, int blksize, u64 block_id) |
| 1274 | { |
| 1275 | return _drbd_send_ack(mdev, cmd, |
| 1276 | cpu_to_be64(sector), |
| 1277 | cpu_to_be32(blksize), |
| 1278 | cpu_to_be64(block_id)); |
| 1279 | } |
| 1280 | |
| 1281 | int drbd_send_drequest(struct drbd_conf *mdev, int cmd, |
| 1282 | sector_t sector, int size, u64 block_id) |
| 1283 | { |
| 1284 | int ok; |
| 1285 | struct p_block_req p; |
| 1286 | |
| 1287 | p.sector = cpu_to_be64(sector); |
| 1288 | p.block_id = block_id; |
| 1289 | p.blksize = cpu_to_be32(size); |
| 1290 | |
Philipp Reisner | c012949 | 2011-01-19 16:58:16 +0100 | [diff] [blame] | 1291 | ok = drbd_send_cmd(mdev, USE_DATA_SOCKET, cmd, &p.head, sizeof(p)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1292 | return ok; |
| 1293 | } |
| 1294 | |
Andreas Gruenbacher | d876302 | 2011-01-26 17:39:41 +0100 | [diff] [blame] | 1295 | int drbd_send_drequest_csum(struct drbd_conf *mdev, sector_t sector, int size, |
| 1296 | void *digest, int digest_size, enum drbd_packet cmd) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1297 | { |
| 1298 | int ok; |
| 1299 | struct p_block_req p; |
| 1300 | |
Philipp Reisner | fd340c1 | 2011-01-19 16:57:39 +0100 | [diff] [blame] | 1301 | prepare_header(mdev, &p.head, cmd, sizeof(p) - sizeof(struct p_header) + digest_size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1302 | p.sector = cpu_to_be64(sector); |
Andreas Gruenbacher | 9a8e775 | 2011-01-11 14:04:09 +0100 | [diff] [blame] | 1303 | p.block_id = ID_SYNCER /* unused */; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1304 | p.blksize = cpu_to_be32(size); |
| 1305 | |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 1306 | mutex_lock(&mdev->tconn->data.mutex); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1307 | |
Philipp Reisner | bedbd2a | 2011-02-07 15:08:48 +0100 | [diff] [blame] | 1308 | ok = (sizeof(p) == drbd_send(mdev->tconn, mdev->tconn->data.socket, &p, sizeof(p), 0)); |
| 1309 | ok = ok && (digest_size == drbd_send(mdev->tconn, mdev->tconn->data.socket, digest, digest_size, 0)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1310 | |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 1311 | mutex_unlock(&mdev->tconn->data.mutex); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1312 | |
| 1313 | return ok; |
| 1314 | } |
| 1315 | |
| 1316 | int drbd_send_ov_request(struct drbd_conf *mdev, sector_t sector, int size) |
| 1317 | { |
| 1318 | int ok; |
| 1319 | struct p_block_req p; |
| 1320 | |
| 1321 | p.sector = cpu_to_be64(sector); |
Andreas Gruenbacher | 9a8e775 | 2011-01-11 14:04:09 +0100 | [diff] [blame] | 1322 | p.block_id = ID_SYNCER /* unused */; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1323 | p.blksize = cpu_to_be32(size); |
| 1324 | |
Philipp Reisner | c012949 | 2011-01-19 16:58:16 +0100 | [diff] [blame] | 1325 | ok = drbd_send_cmd(mdev, USE_DATA_SOCKET, P_OV_REQUEST, &p.head, sizeof(p)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1326 | return ok; |
| 1327 | } |
| 1328 | |
| 1329 | /* called on sndtimeo |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 1330 | * returns false if we should retry, |
| 1331 | * true if we think connection is dead |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1332 | */ |
Philipp Reisner | 1a7ba64 | 2011-02-07 14:56:02 +0100 | [diff] [blame] | 1333 | static int we_should_drop_the_connection(struct drbd_tconn *tconn, struct socket *sock) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1334 | { |
| 1335 | int drop_it; |
| 1336 | /* long elapsed = (long)(jiffies - mdev->last_received); */ |
| 1337 | |
Philipp Reisner | 1a7ba64 | 2011-02-07 14:56:02 +0100 | [diff] [blame] | 1338 | drop_it = tconn->meta.socket == sock |
| 1339 | || !tconn->asender.task |
| 1340 | || get_t_state(&tconn->asender) != RUNNING |
| 1341 | || tconn->volume0->state.conn < C_CONNECTED; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1342 | |
| 1343 | if (drop_it) |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 1344 | return true; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1345 | |
Philipp Reisner | 1a7ba64 | 2011-02-07 14:56:02 +0100 | [diff] [blame] | 1346 | drop_it = !--tconn->ko_count; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1347 | if (!drop_it) { |
Philipp Reisner | 1a7ba64 | 2011-02-07 14:56:02 +0100 | [diff] [blame] | 1348 | conn_err(tconn, "[%s/%d] sock_sendmsg time expired, ko = %u\n", |
| 1349 | current->comm, current->pid, tconn->ko_count); |
| 1350 | request_ping(tconn); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | return drop_it; /* && (mdev->state == R_PRIMARY) */; |
| 1354 | } |
| 1355 | |
Philipp Reisner | 1a7ba64 | 2011-02-07 14:56:02 +0100 | [diff] [blame] | 1356 | static void drbd_update_congested(struct drbd_tconn *tconn) |
Andreas Gruenbacher | 9e204cd | 2011-01-26 18:45:11 +0100 | [diff] [blame] | 1357 | { |
Philipp Reisner | 1a7ba64 | 2011-02-07 14:56:02 +0100 | [diff] [blame] | 1358 | struct sock *sk = tconn->data.socket->sk; |
Andreas Gruenbacher | 9e204cd | 2011-01-26 18:45:11 +0100 | [diff] [blame] | 1359 | if (sk->sk_wmem_queued > sk->sk_sndbuf * 4 / 5) |
Philipp Reisner | 1a7ba64 | 2011-02-07 14:56:02 +0100 | [diff] [blame] | 1360 | set_bit(NET_CONGESTED, &tconn->flags); |
Andreas Gruenbacher | 9e204cd | 2011-01-26 18:45:11 +0100 | [diff] [blame] | 1361 | } |
| 1362 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1363 | /* The idea of sendpage seems to be to put some kind of reference |
| 1364 | * to the page into the skb, and to hand it over to the NIC. In |
| 1365 | * this process get_page() gets called. |
| 1366 | * |
| 1367 | * As soon as the page was really sent over the network put_page() |
| 1368 | * gets called by some part of the network layer. [ NIC driver? ] |
| 1369 | * |
| 1370 | * [ get_page() / put_page() increment/decrement the count. If count |
| 1371 | * reaches 0 the page will be freed. ] |
| 1372 | * |
| 1373 | * This works nicely with pages from FSs. |
| 1374 | * But this means that in protocol A we might signal IO completion too early! |
| 1375 | * |
| 1376 | * In order not to corrupt data during a resync we must make sure |
| 1377 | * that we do not reuse our own buffer pages (EEs) to early, therefore |
| 1378 | * we have the net_ee list. |
| 1379 | * |
| 1380 | * XFS seems to have problems, still, it submits pages with page_count == 0! |
| 1381 | * As a workaround, we disable sendpage on pages |
| 1382 | * with page_count == 0 or PageSlab. |
| 1383 | */ |
| 1384 | static int _drbd_no_send_page(struct drbd_conf *mdev, struct page *page, |
Lars Ellenberg | ba11ad9 | 2010-05-25 16:26:16 +0200 | [diff] [blame] | 1385 | int offset, size_t size, unsigned msg_flags) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1386 | { |
Philipp Reisner | bedbd2a | 2011-02-07 15:08:48 +0100 | [diff] [blame] | 1387 | int sent = drbd_send(mdev->tconn, mdev->tconn->data.socket, kmap(page) + offset, size, msg_flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1388 | kunmap(page); |
| 1389 | if (sent == size) |
| 1390 | mdev->send_cnt += size>>9; |
| 1391 | return sent == size; |
| 1392 | } |
| 1393 | |
| 1394 | static int _drbd_send_page(struct drbd_conf *mdev, struct page *page, |
Lars Ellenberg | ba11ad9 | 2010-05-25 16:26:16 +0200 | [diff] [blame] | 1395 | int offset, size_t size, unsigned msg_flags) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1396 | { |
| 1397 | mm_segment_t oldfs = get_fs(); |
| 1398 | int sent, ok; |
| 1399 | int len = size; |
| 1400 | |
| 1401 | /* e.g. XFS meta- & log-data is in slab pages, which have a |
| 1402 | * page_count of 0 and/or have PageSlab() set. |
| 1403 | * we cannot use send_page for those, as that does get_page(); |
| 1404 | * put_page(); and would cause either a VM_BUG directly, or |
| 1405 | * __page_cache_release a page that would actually still be referenced |
| 1406 | * by someone, leading to some obscure delayed Oops somewhere else. */ |
| 1407 | if (disable_sendpage || (page_count(page) < 1) || PageSlab(page)) |
Lars Ellenberg | ba11ad9 | 2010-05-25 16:26:16 +0200 | [diff] [blame] | 1408 | return _drbd_no_send_page(mdev, page, offset, size, msg_flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1409 | |
Lars Ellenberg | ba11ad9 | 2010-05-25 16:26:16 +0200 | [diff] [blame] | 1410 | msg_flags |= MSG_NOSIGNAL; |
Philipp Reisner | 1a7ba64 | 2011-02-07 14:56:02 +0100 | [diff] [blame] | 1411 | drbd_update_congested(mdev->tconn); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1412 | set_fs(KERNEL_DS); |
| 1413 | do { |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 1414 | sent = mdev->tconn->data.socket->ops->sendpage(mdev->tconn->data.socket, page, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1415 | offset, len, |
Lars Ellenberg | ba11ad9 | 2010-05-25 16:26:16 +0200 | [diff] [blame] | 1416 | msg_flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1417 | if (sent == -EAGAIN) { |
Philipp Reisner | 1a7ba64 | 2011-02-07 14:56:02 +0100 | [diff] [blame] | 1418 | if (we_should_drop_the_connection(mdev->tconn, |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 1419 | mdev->tconn->data.socket)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1420 | break; |
| 1421 | else |
| 1422 | continue; |
| 1423 | } |
| 1424 | if (sent <= 0) { |
| 1425 | dev_warn(DEV, "%s: size=%d len=%d sent=%d\n", |
| 1426 | __func__, (int)size, len, sent); |
| 1427 | break; |
| 1428 | } |
| 1429 | len -= sent; |
| 1430 | offset += sent; |
| 1431 | } while (len > 0 /* THINK && mdev->cstate >= C_CONNECTED*/); |
| 1432 | set_fs(oldfs); |
Philipp Reisner | 01a311a | 2011-02-07 14:30:33 +0100 | [diff] [blame] | 1433 | clear_bit(NET_CONGESTED, &mdev->tconn->flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1434 | |
| 1435 | ok = (len == 0); |
| 1436 | if (likely(ok)) |
| 1437 | mdev->send_cnt += size>>9; |
| 1438 | return ok; |
| 1439 | } |
| 1440 | |
| 1441 | static int _drbd_send_bio(struct drbd_conf *mdev, struct bio *bio) |
| 1442 | { |
| 1443 | struct bio_vec *bvec; |
| 1444 | int i; |
Lars Ellenberg | ba11ad9 | 2010-05-25 16:26:16 +0200 | [diff] [blame] | 1445 | /* hint all but last page with MSG_MORE */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1446 | __bio_for_each_segment(bvec, bio, i, 0) { |
| 1447 | if (!_drbd_no_send_page(mdev, bvec->bv_page, |
Lars Ellenberg | ba11ad9 | 2010-05-25 16:26:16 +0200 | [diff] [blame] | 1448 | bvec->bv_offset, bvec->bv_len, |
| 1449 | i == bio->bi_vcnt -1 ? 0 : MSG_MORE)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1450 | return 0; |
| 1451 | } |
| 1452 | return 1; |
| 1453 | } |
| 1454 | |
| 1455 | static int _drbd_send_zc_bio(struct drbd_conf *mdev, struct bio *bio) |
| 1456 | { |
| 1457 | struct bio_vec *bvec; |
| 1458 | int i; |
Lars Ellenberg | ba11ad9 | 2010-05-25 16:26:16 +0200 | [diff] [blame] | 1459 | /* hint all but last page with MSG_MORE */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1460 | __bio_for_each_segment(bvec, bio, i, 0) { |
| 1461 | if (!_drbd_send_page(mdev, bvec->bv_page, |
Lars Ellenberg | ba11ad9 | 2010-05-25 16:26:16 +0200 | [diff] [blame] | 1462 | bvec->bv_offset, bvec->bv_len, |
| 1463 | i == bio->bi_vcnt -1 ? 0 : MSG_MORE)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1464 | return 0; |
| 1465 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1466 | return 1; |
| 1467 | } |
| 1468 | |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1469 | static int _drbd_send_zc_ee(struct drbd_conf *mdev, |
| 1470 | struct drbd_peer_request *peer_req) |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1471 | { |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1472 | struct page *page = peer_req->pages; |
| 1473 | unsigned len = peer_req->i.size; |
| 1474 | |
Lars Ellenberg | ba11ad9 | 2010-05-25 16:26:16 +0200 | [diff] [blame] | 1475 | /* hint all but last page with MSG_MORE */ |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1476 | page_chain_for_each(page) { |
| 1477 | unsigned l = min_t(unsigned, len, PAGE_SIZE); |
Lars Ellenberg | ba11ad9 | 2010-05-25 16:26:16 +0200 | [diff] [blame] | 1478 | if (!_drbd_send_page(mdev, page, 0, l, |
| 1479 | page_chain_next(page) ? MSG_MORE : 0)) |
Lars Ellenberg | 45bb912 | 2010-05-14 17:10:48 +0200 | [diff] [blame] | 1480 | return 0; |
| 1481 | len -= l; |
| 1482 | } |
| 1483 | return 1; |
| 1484 | } |
| 1485 | |
Philipp Reisner | 76d2e7e | 2010-08-25 11:58:05 +0200 | [diff] [blame] | 1486 | static u32 bio_flags_to_wire(struct drbd_conf *mdev, unsigned long bi_rw) |
| 1487 | { |
Philipp Reisner | 31890f4 | 2011-01-19 14:12:51 +0100 | [diff] [blame] | 1488 | if (mdev->tconn->agreed_pro_version >= 95) |
Philipp Reisner | 76d2e7e | 2010-08-25 11:58:05 +0200 | [diff] [blame] | 1489 | return (bi_rw & REQ_SYNC ? DP_RW_SYNC : 0) | |
Philipp Reisner | 76d2e7e | 2010-08-25 11:58:05 +0200 | [diff] [blame] | 1490 | (bi_rw & REQ_FUA ? DP_FUA : 0) | |
| 1491 | (bi_rw & REQ_FLUSH ? DP_FLUSH : 0) | |
| 1492 | (bi_rw & REQ_DISCARD ? DP_DISCARD : 0); |
| 1493 | else |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 1494 | return bi_rw & REQ_SYNC ? DP_RW_SYNC : 0; |
Philipp Reisner | 76d2e7e | 2010-08-25 11:58:05 +0200 | [diff] [blame] | 1495 | } |
| 1496 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1497 | /* Used to send write requests |
| 1498 | * R_PRIMARY -> Peer (P_DATA) |
| 1499 | */ |
| 1500 | int drbd_send_dblock(struct drbd_conf *mdev, struct drbd_request *req) |
| 1501 | { |
| 1502 | int ok = 1; |
| 1503 | struct p_data p; |
| 1504 | unsigned int dp_flags = 0; |
| 1505 | void *dgb; |
| 1506 | int dgs; |
| 1507 | |
Philipp Reisner | 6112087 | 2011-02-08 09:50:54 +0100 | [diff] [blame] | 1508 | if (!drbd_get_data_sock(mdev->tconn)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1509 | return 0; |
| 1510 | |
Philipp Reisner | a063845 | 2011-01-19 14:31:32 +0100 | [diff] [blame] | 1511 | dgs = (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_w_tfm) ? |
| 1512 | crypto_hash_digestsize(mdev->tconn->integrity_w_tfm) : 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1513 | |
Philipp Reisner | fd340c1 | 2011-01-19 16:57:39 +0100 | [diff] [blame] | 1514 | prepare_header(mdev, &p.head, P_DATA, sizeof(p) - sizeof(struct p_header) + dgs + req->i.size); |
Andreas Gruenbacher | ace652a | 2011-01-03 17:09:58 +0100 | [diff] [blame] | 1515 | p.sector = cpu_to_be64(req->i.sector); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1516 | p.block_id = (unsigned long)req; |
Philipp Reisner | fd340c1 | 2011-01-19 16:57:39 +0100 | [diff] [blame] | 1517 | p.seq_num = cpu_to_be32(req->seq_num = atomic_add_return(1, &mdev->packet_seq)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1518 | |
Philipp Reisner | 76d2e7e | 2010-08-25 11:58:05 +0200 | [diff] [blame] | 1519 | dp_flags = bio_flags_to_wire(mdev, req->master_bio->bi_rw); |
| 1520 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1521 | if (mdev->state.conn >= C_SYNC_SOURCE && |
| 1522 | mdev->state.conn <= C_PAUSED_SYNC_T) |
| 1523 | dp_flags |= DP_MAY_SET_IN_SYNC; |
| 1524 | |
| 1525 | p.dp_flags = cpu_to_be32(dp_flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1526 | set_bit(UNPLUG_REMOTE, &mdev->flags); |
| 1527 | ok = (sizeof(p) == |
Philipp Reisner | bedbd2a | 2011-02-07 15:08:48 +0100 | [diff] [blame] | 1528 | drbd_send(mdev->tconn, mdev->tconn->data.socket, &p, sizeof(p), dgs ? MSG_MORE : 0)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1529 | if (ok && dgs) { |
Philipp Reisner | a063845 | 2011-01-19 14:31:32 +0100 | [diff] [blame] | 1530 | dgb = mdev->tconn->int_dig_out; |
| 1531 | drbd_csum_bio(mdev, mdev->tconn->integrity_w_tfm, req->master_bio, dgb); |
Philipp Reisner | bedbd2a | 2011-02-07 15:08:48 +0100 | [diff] [blame] | 1532 | ok = dgs == drbd_send(mdev->tconn, mdev->tconn->data.socket, dgb, dgs, 0); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1533 | } |
| 1534 | if (ok) { |
Lars Ellenberg | 470be44 | 2010-11-10 10:36:52 +0100 | [diff] [blame] | 1535 | /* For protocol A, we have to memcpy the payload into |
| 1536 | * socket buffers, as we may complete right away |
| 1537 | * as soon as we handed it over to tcp, at which point the data |
| 1538 | * pages may become invalid. |
| 1539 | * |
| 1540 | * For data-integrity enabled, we copy it as well, so we can be |
| 1541 | * sure that even if the bio pages may still be modified, it |
| 1542 | * won't change the data on the wire, thus if the digest checks |
| 1543 | * out ok after sending on this side, but does not fit on the |
| 1544 | * receiving side, we sure have detected corruption elsewhere. |
| 1545 | */ |
Philipp Reisner | 89e58e7 | 2011-01-19 13:12:45 +0100 | [diff] [blame] | 1546 | if (mdev->tconn->net_conf->wire_protocol == DRBD_PROT_A || dgs) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1547 | ok = _drbd_send_bio(mdev, req->master_bio); |
| 1548 | else |
| 1549 | ok = _drbd_send_zc_bio(mdev, req->master_bio); |
Lars Ellenberg | 470be44 | 2010-11-10 10:36:52 +0100 | [diff] [blame] | 1550 | |
| 1551 | /* double check digest, sometimes buffers have been modified in flight. */ |
| 1552 | if (dgs > 0 && dgs <= 64) { |
Bart Van Assche | 24c4830 | 2011-05-21 18:32:29 +0200 | [diff] [blame] | 1553 | /* 64 byte, 512 bit, is the largest digest size |
Lars Ellenberg | 470be44 | 2010-11-10 10:36:52 +0100 | [diff] [blame] | 1554 | * currently supported in kernel crypto. */ |
| 1555 | unsigned char digest[64]; |
Philipp Reisner | a063845 | 2011-01-19 14:31:32 +0100 | [diff] [blame] | 1556 | drbd_csum_bio(mdev, mdev->tconn->integrity_w_tfm, req->master_bio, digest); |
| 1557 | if (memcmp(mdev->tconn->int_dig_out, digest, dgs)) { |
Lars Ellenberg | 470be44 | 2010-11-10 10:36:52 +0100 | [diff] [blame] | 1558 | dev_warn(DEV, |
| 1559 | "Digest mismatch, buffer modified by upper layers during write: %llus +%u\n", |
Andreas Gruenbacher | ace652a | 2011-01-03 17:09:58 +0100 | [diff] [blame] | 1560 | (unsigned long long)req->i.sector, req->i.size); |
Lars Ellenberg | 470be44 | 2010-11-10 10:36:52 +0100 | [diff] [blame] | 1561 | } |
| 1562 | } /* else if (dgs > 64) { |
| 1563 | ... Be noisy about digest too large ... |
| 1564 | } */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1565 | } |
| 1566 | |
Philipp Reisner | 6112087 | 2011-02-08 09:50:54 +0100 | [diff] [blame] | 1567 | drbd_put_data_sock(mdev->tconn); |
Philipp Reisner | bd26bfc5 | 2010-05-04 12:33:58 +0200 | [diff] [blame] | 1568 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1569 | return ok; |
| 1570 | } |
| 1571 | |
| 1572 | /* answer packet, used to send data back for read requests: |
| 1573 | * Peer -> (diskless) R_PRIMARY (P_DATA_REPLY) |
| 1574 | * C_SYNC_SOURCE -> C_SYNC_TARGET (P_RS_DATA_REPLY) |
| 1575 | */ |
Andreas Gruenbacher | d876302 | 2011-01-26 17:39:41 +0100 | [diff] [blame] | 1576 | int drbd_send_block(struct drbd_conf *mdev, enum drbd_packet cmd, |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1577 | struct drbd_peer_request *peer_req) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1578 | { |
| 1579 | int ok; |
| 1580 | struct p_data p; |
| 1581 | void *dgb; |
| 1582 | int dgs; |
| 1583 | |
Philipp Reisner | a063845 | 2011-01-19 14:31:32 +0100 | [diff] [blame] | 1584 | dgs = (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_w_tfm) ? |
| 1585 | crypto_hash_digestsize(mdev->tconn->integrity_w_tfm) : 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1586 | |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1587 | prepare_header(mdev, &p.head, cmd, sizeof(p) - |
| 1588 | sizeof(struct p_header80) + |
| 1589 | dgs + peer_req->i.size); |
| 1590 | p.sector = cpu_to_be64(peer_req->i.sector); |
| 1591 | p.block_id = peer_req->block_id; |
Andreas Gruenbacher | cc37827 | 2011-01-26 18:01:50 +0100 | [diff] [blame] | 1592 | p.seq_num = 0; /* unused */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1593 | |
| 1594 | /* Only called by our kernel thread. |
| 1595 | * This one may be interrupted by DRBD_SIG and/or DRBD_SIGKILL |
| 1596 | * in response to admin command or module unload. |
| 1597 | */ |
Philipp Reisner | 6112087 | 2011-02-08 09:50:54 +0100 | [diff] [blame] | 1598 | if (!drbd_get_data_sock(mdev->tconn)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1599 | return 0; |
| 1600 | |
Philipp Reisner | bedbd2a | 2011-02-07 15:08:48 +0100 | [diff] [blame] | 1601 | ok = sizeof(p) == drbd_send(mdev->tconn, mdev->tconn->data.socket, &p, sizeof(p), dgs ? MSG_MORE : 0); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1602 | if (ok && dgs) { |
Philipp Reisner | a063845 | 2011-01-19 14:31:32 +0100 | [diff] [blame] | 1603 | dgb = mdev->tconn->int_dig_out; |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1604 | drbd_csum_ee(mdev, mdev->tconn->integrity_w_tfm, peer_req, dgb); |
Philipp Reisner | bedbd2a | 2011-02-07 15:08:48 +0100 | [diff] [blame] | 1605 | ok = dgs == drbd_send(mdev->tconn, mdev->tconn->data.socket, dgb, dgs, 0); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1606 | } |
| 1607 | if (ok) |
Andreas Gruenbacher | db830c4 | 2011-02-04 15:57:48 +0100 | [diff] [blame] | 1608 | ok = _drbd_send_zc_ee(mdev, peer_req); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1609 | |
Philipp Reisner | 6112087 | 2011-02-08 09:50:54 +0100 | [diff] [blame] | 1610 | drbd_put_data_sock(mdev->tconn); |
Philipp Reisner | bd26bfc5 | 2010-05-04 12:33:58 +0200 | [diff] [blame] | 1611 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1612 | return ok; |
| 1613 | } |
| 1614 | |
Philipp Reisner | 73a01a1 | 2010-10-27 14:33:00 +0200 | [diff] [blame] | 1615 | int drbd_send_oos(struct drbd_conf *mdev, struct drbd_request *req) |
| 1616 | { |
| 1617 | struct p_block_desc p; |
| 1618 | |
Andreas Gruenbacher | ace652a | 2011-01-03 17:09:58 +0100 | [diff] [blame] | 1619 | p.sector = cpu_to_be64(req->i.sector); |
| 1620 | p.blksize = cpu_to_be32(req->i.size); |
Philipp Reisner | 73a01a1 | 2010-10-27 14:33:00 +0200 | [diff] [blame] | 1621 | |
| 1622 | return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_OUT_OF_SYNC, &p.head, sizeof(p)); |
| 1623 | } |
| 1624 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1625 | /* |
| 1626 | drbd_send distinguishes two cases: |
| 1627 | |
| 1628 | Packets sent via the data socket "sock" |
| 1629 | and packets sent via the meta data socket "msock" |
| 1630 | |
| 1631 | sock msock |
| 1632 | -----------------+-------------------------+------------------------------ |
| 1633 | timeout conf.timeout / 2 conf.timeout / 2 |
| 1634 | timeout action send a ping via msock Abort communication |
| 1635 | and close all sockets |
| 1636 | */ |
| 1637 | |
| 1638 | /* |
| 1639 | * you must have down()ed the appropriate [m]sock_mutex elsewhere! |
| 1640 | */ |
Philipp Reisner | bedbd2a | 2011-02-07 15:08:48 +0100 | [diff] [blame] | 1641 | int drbd_send(struct drbd_tconn *tconn, struct socket *sock, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1642 | void *buf, size_t size, unsigned msg_flags) |
| 1643 | { |
| 1644 | struct kvec iov; |
| 1645 | struct msghdr msg; |
| 1646 | int rv, sent = 0; |
| 1647 | |
| 1648 | if (!sock) |
| 1649 | return -1000; |
| 1650 | |
| 1651 | /* THINK if (signal_pending) return ... ? */ |
| 1652 | |
| 1653 | iov.iov_base = buf; |
| 1654 | iov.iov_len = size; |
| 1655 | |
| 1656 | msg.msg_name = NULL; |
| 1657 | msg.msg_namelen = 0; |
| 1658 | msg.msg_control = NULL; |
| 1659 | msg.msg_controllen = 0; |
| 1660 | msg.msg_flags = msg_flags | MSG_NOSIGNAL; |
| 1661 | |
Philipp Reisner | bedbd2a | 2011-02-07 15:08:48 +0100 | [diff] [blame] | 1662 | if (sock == tconn->data.socket) { |
| 1663 | tconn->ko_count = tconn->net_conf->ko_count; |
| 1664 | drbd_update_congested(tconn); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1665 | } |
| 1666 | do { |
| 1667 | /* STRANGE |
| 1668 | * tcp_sendmsg does _not_ use its size parameter at all ? |
| 1669 | * |
| 1670 | * -EAGAIN on timeout, -EINTR on signal. |
| 1671 | */ |
| 1672 | /* THINK |
| 1673 | * do we need to block DRBD_SIG if sock == &meta.socket ?? |
| 1674 | * otherwise wake_asender() might interrupt some send_*Ack ! |
| 1675 | */ |
| 1676 | rv = kernel_sendmsg(sock, &msg, &iov, 1, size); |
| 1677 | if (rv == -EAGAIN) { |
Philipp Reisner | bedbd2a | 2011-02-07 15:08:48 +0100 | [diff] [blame] | 1678 | if (we_should_drop_the_connection(tconn, sock)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1679 | break; |
| 1680 | else |
| 1681 | continue; |
| 1682 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1683 | if (rv == -EINTR) { |
| 1684 | flush_signals(current); |
| 1685 | rv = 0; |
| 1686 | } |
| 1687 | if (rv < 0) |
| 1688 | break; |
| 1689 | sent += rv; |
| 1690 | iov.iov_base += rv; |
| 1691 | iov.iov_len -= rv; |
| 1692 | } while (sent < size); |
| 1693 | |
Philipp Reisner | bedbd2a | 2011-02-07 15:08:48 +0100 | [diff] [blame] | 1694 | if (sock == tconn->data.socket) |
| 1695 | clear_bit(NET_CONGESTED, &tconn->flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1696 | |
| 1697 | if (rv <= 0) { |
| 1698 | if (rv != -EAGAIN) { |
Philipp Reisner | bedbd2a | 2011-02-07 15:08:48 +0100 | [diff] [blame] | 1699 | conn_err(tconn, "%s_sendmsg returned %d\n", |
| 1700 | sock == tconn->meta.socket ? "msock" : "sock", |
| 1701 | rv); |
| 1702 | drbd_force_state(tconn->volume0, NS(conn, C_BROKEN_PIPE)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1703 | } else |
Philipp Reisner | bedbd2a | 2011-02-07 15:08:48 +0100 | [diff] [blame] | 1704 | drbd_force_state(tconn->volume0, NS(conn, C_TIMEOUT)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1705 | } |
| 1706 | |
| 1707 | return sent; |
| 1708 | } |
| 1709 | |
| 1710 | static int drbd_open(struct block_device *bdev, fmode_t mode) |
| 1711 | { |
| 1712 | struct drbd_conf *mdev = bdev->bd_disk->private_data; |
| 1713 | unsigned long flags; |
| 1714 | int rv = 0; |
| 1715 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 1716 | mutex_lock(&drbd_main_mutex); |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 1717 | spin_lock_irqsave(&mdev->tconn->req_lock, flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1718 | /* to have a stable mdev->state.role |
| 1719 | * and no race with updating open_cnt */ |
| 1720 | |
| 1721 | if (mdev->state.role != R_PRIMARY) { |
| 1722 | if (mode & FMODE_WRITE) |
| 1723 | rv = -EROFS; |
| 1724 | else if (!allow_oos) |
| 1725 | rv = -EMEDIUMTYPE; |
| 1726 | } |
| 1727 | |
| 1728 | if (!rv) |
| 1729 | mdev->open_cnt++; |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 1730 | spin_unlock_irqrestore(&mdev->tconn->req_lock, flags); |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 1731 | mutex_unlock(&drbd_main_mutex); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1732 | |
| 1733 | return rv; |
| 1734 | } |
| 1735 | |
| 1736 | static int drbd_release(struct gendisk *gd, fmode_t mode) |
| 1737 | { |
| 1738 | struct drbd_conf *mdev = gd->private_data; |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 1739 | mutex_lock(&drbd_main_mutex); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1740 | mdev->open_cnt--; |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 1741 | mutex_unlock(&drbd_main_mutex); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1742 | return 0; |
| 1743 | } |
| 1744 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1745 | static void drbd_set_defaults(struct drbd_conf *mdev) |
| 1746 | { |
Philipp Reisner | 85f4cc1 | 2010-06-29 17:35:34 +0200 | [diff] [blame] | 1747 | /* This way we get a compile error when sync_conf grows, |
| 1748 | and we forgot to initialize it here */ |
| 1749 | mdev->sync_conf = (struct syncer_conf) { |
| 1750 | /* .rate = */ DRBD_RATE_DEF, |
| 1751 | /* .after = */ DRBD_AFTER_DEF, |
| 1752 | /* .al_extents = */ DRBD_AL_EXTENTS_DEF, |
Philipp Reisner | 85f4cc1 | 2010-06-29 17:35:34 +0200 | [diff] [blame] | 1753 | /* .verify_alg = */ {}, 0, |
| 1754 | /* .cpu_mask = */ {}, 0, |
| 1755 | /* .csums_alg = */ {}, 0, |
Philipp Reisner | e756414 | 2010-06-29 17:35:34 +0200 | [diff] [blame] | 1756 | /* .use_rle = */ 0, |
Philipp Reisner | 9a31d71 | 2010-07-05 13:42:03 +0200 | [diff] [blame] | 1757 | /* .on_no_data = */ DRBD_ON_NO_DATA_DEF, |
| 1758 | /* .c_plan_ahead = */ DRBD_C_PLAN_AHEAD_DEF, |
| 1759 | /* .c_delay_target = */ DRBD_C_DELAY_TARGET_DEF, |
| 1760 | /* .c_fill_target = */ DRBD_C_FILL_TARGET_DEF, |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 1761 | /* .c_max_rate = */ DRBD_C_MAX_RATE_DEF, |
| 1762 | /* .c_min_rate = */ DRBD_C_MIN_RATE_DEF |
Philipp Reisner | 85f4cc1 | 2010-06-29 17:35:34 +0200 | [diff] [blame] | 1763 | }; |
| 1764 | |
| 1765 | /* Have to use that way, because the layout differs between |
| 1766 | big endian and little endian */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1767 | mdev->state = (union drbd_state) { |
| 1768 | { .role = R_SECONDARY, |
| 1769 | .peer = R_UNKNOWN, |
| 1770 | .conn = C_STANDALONE, |
| 1771 | .disk = D_DISKLESS, |
| 1772 | .pdsk = D_UNKNOWN, |
Philipp Reisner | fb22c40 | 2010-09-08 23:20:21 +0200 | [diff] [blame] | 1773 | .susp = 0, |
| 1774 | .susp_nod = 0, |
| 1775 | .susp_fen = 0 |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1776 | } }; |
| 1777 | } |
| 1778 | |
| 1779 | void drbd_init_set_defaults(struct drbd_conf *mdev) |
| 1780 | { |
| 1781 | /* the memset(,0,) did most of this. |
| 1782 | * note: only assignments, no allocation in here */ |
| 1783 | |
| 1784 | drbd_set_defaults(mdev); |
| 1785 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1786 | atomic_set(&mdev->ap_bio_cnt, 0); |
| 1787 | atomic_set(&mdev->ap_pending_cnt, 0); |
| 1788 | atomic_set(&mdev->rs_pending_cnt, 0); |
| 1789 | atomic_set(&mdev->unacked_cnt, 0); |
| 1790 | atomic_set(&mdev->local_cnt, 0); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1791 | atomic_set(&mdev->pp_in_use, 0); |
Lars Ellenberg | 435f074 | 2010-09-06 12:30:25 +0200 | [diff] [blame] | 1792 | atomic_set(&mdev->pp_in_use_by_net, 0); |
Philipp Reisner | 778f271 | 2010-07-06 11:14:00 +0200 | [diff] [blame] | 1793 | atomic_set(&mdev->rs_sect_in, 0); |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 1794 | atomic_set(&mdev->rs_sect_ev, 0); |
Philipp Reisner | 759fbdf | 2010-10-26 16:02:27 +0200 | [diff] [blame] | 1795 | atomic_set(&mdev->ap_in_flight, 0); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1796 | |
| 1797 | mutex_init(&mdev->md_io_mutex); |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 1798 | mutex_init(&mdev->tconn->data.mutex); |
| 1799 | mutex_init(&mdev->tconn->meta.mutex); |
| 1800 | sema_init(&mdev->tconn->data.work.s, 0); |
| 1801 | sema_init(&mdev->tconn->meta.work.s, 0); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1802 | mutex_init(&mdev->state_mutex); |
| 1803 | |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 1804 | spin_lock_init(&mdev->tconn->data.work.q_lock); |
| 1805 | spin_lock_init(&mdev->tconn->meta.work.q_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1806 | |
| 1807 | spin_lock_init(&mdev->al_lock); |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 1808 | spin_lock_init(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1809 | spin_lock_init(&mdev->peer_seq_lock); |
| 1810 | spin_lock_init(&mdev->epoch_lock); |
| 1811 | |
| 1812 | INIT_LIST_HEAD(&mdev->active_ee); |
| 1813 | INIT_LIST_HEAD(&mdev->sync_ee); |
| 1814 | INIT_LIST_HEAD(&mdev->done_ee); |
| 1815 | INIT_LIST_HEAD(&mdev->read_ee); |
| 1816 | INIT_LIST_HEAD(&mdev->net_ee); |
| 1817 | INIT_LIST_HEAD(&mdev->resync_reads); |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 1818 | INIT_LIST_HEAD(&mdev->tconn->data.work.q); |
| 1819 | INIT_LIST_HEAD(&mdev->tconn->meta.work.q); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1820 | INIT_LIST_HEAD(&mdev->resync_work.list); |
| 1821 | INIT_LIST_HEAD(&mdev->unplug_work.list); |
Lars Ellenberg | e9e6f3e | 2010-09-14 20:26:27 +0200 | [diff] [blame] | 1822 | INIT_LIST_HEAD(&mdev->go_diskless.list); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1823 | INIT_LIST_HEAD(&mdev->md_sync_work.list); |
Philipp Reisner | c4752ef | 2010-10-27 17:32:36 +0200 | [diff] [blame] | 1824 | INIT_LIST_HEAD(&mdev->start_resync_work.list); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1825 | INIT_LIST_HEAD(&mdev->bm_io_work.w.list); |
Philipp Reisner | 0ced55a | 2010-04-30 15:26:20 +0200 | [diff] [blame] | 1826 | |
Philipp Reisner | 794abb7 | 2010-12-27 11:51:23 +0100 | [diff] [blame] | 1827 | mdev->resync_work.cb = w_resync_timer; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1828 | mdev->unplug_work.cb = w_send_write_hint; |
Lars Ellenberg | e9e6f3e | 2010-09-14 20:26:27 +0200 | [diff] [blame] | 1829 | mdev->go_diskless.cb = w_go_diskless; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1830 | mdev->md_sync_work.cb = w_md_sync; |
| 1831 | mdev->bm_io_work.w.cb = w_bitmap_io; |
Philipp Reisner | 370a43e | 2011-01-14 16:03:11 +0100 | [diff] [blame] | 1832 | mdev->start_resync_work.cb = w_start_resync; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1833 | init_timer(&mdev->resync_timer); |
| 1834 | init_timer(&mdev->md_sync_timer); |
Philipp Reisner | 370a43e | 2011-01-14 16:03:11 +0100 | [diff] [blame] | 1835 | init_timer(&mdev->start_resync_timer); |
Philipp Reisner | 7fde2be | 2011-03-01 11:08:28 +0100 | [diff] [blame] | 1836 | init_timer(&mdev->request_timer); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1837 | mdev->resync_timer.function = resync_timer_fn; |
| 1838 | mdev->resync_timer.data = (unsigned long) mdev; |
| 1839 | mdev->md_sync_timer.function = md_sync_timer_fn; |
| 1840 | mdev->md_sync_timer.data = (unsigned long) mdev; |
Philipp Reisner | 370a43e | 2011-01-14 16:03:11 +0100 | [diff] [blame] | 1841 | mdev->start_resync_timer.function = start_resync_timer_fn; |
| 1842 | mdev->start_resync_timer.data = (unsigned long) mdev; |
Philipp Reisner | 7fde2be | 2011-03-01 11:08:28 +0100 | [diff] [blame] | 1843 | mdev->request_timer.function = request_timer_fn; |
| 1844 | mdev->request_timer.data = (unsigned long) mdev; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1845 | |
| 1846 | init_waitqueue_head(&mdev->misc_wait); |
| 1847 | init_waitqueue_head(&mdev->state_wait); |
| 1848 | init_waitqueue_head(&mdev->ee_wait); |
| 1849 | init_waitqueue_head(&mdev->al_wait); |
| 1850 | init_waitqueue_head(&mdev->seq_wait); |
| 1851 | |
Philipp Reisner | bed879a | 2011-02-04 14:00:37 +0100 | [diff] [blame] | 1852 | drbd_thread_init(mdev, &mdev->tconn->receiver, drbdd_init, "receiver"); |
| 1853 | drbd_thread_init(mdev, &mdev->tconn->worker, drbd_worker, "worker"); |
| 1854 | drbd_thread_init(mdev, &mdev->tconn->asender, drbd_asender, "asender"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1855 | |
Philipp Reisner | fd340c1 | 2011-01-19 16:57:39 +0100 | [diff] [blame] | 1856 | /* mdev->tconn->agreed_pro_version gets initialized in drbd_connect() */ |
Philipp Reisner | 2451fc3 | 2010-08-24 13:43:11 +0200 | [diff] [blame] | 1857 | mdev->write_ordering = WO_bdev_flush; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1858 | mdev->resync_wenr = LC_FREE; |
Philipp Reisner | 99432fc | 2011-05-20 16:39:13 +0200 | [diff] [blame] | 1859 | mdev->peer_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE; |
| 1860 | mdev->local_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1861 | } |
| 1862 | |
| 1863 | void drbd_mdev_cleanup(struct drbd_conf *mdev) |
| 1864 | { |
Lars Ellenberg | 1d7734a | 2010-08-11 21:21:50 +0200 | [diff] [blame] | 1865 | int i; |
Philipp Reisner | e6b3ea8 | 2011-01-19 14:02:01 +0100 | [diff] [blame] | 1866 | if (mdev->tconn->receiver.t_state != NONE) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1867 | dev_err(DEV, "ASSERT FAILED: receiver t_state == %d expected 0.\n", |
Philipp Reisner | e6b3ea8 | 2011-01-19 14:02:01 +0100 | [diff] [blame] | 1868 | mdev->tconn->receiver.t_state); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1869 | |
| 1870 | /* no need to lock it, I'm the only thread alive */ |
| 1871 | if (atomic_read(&mdev->current_epoch->epoch_size) != 0) |
| 1872 | dev_err(DEV, "epoch_size:%d\n", atomic_read(&mdev->current_epoch->epoch_size)); |
| 1873 | mdev->al_writ_cnt = |
| 1874 | mdev->bm_writ_cnt = |
| 1875 | mdev->read_cnt = |
| 1876 | mdev->recv_cnt = |
| 1877 | mdev->send_cnt = |
| 1878 | mdev->writ_cnt = |
| 1879 | mdev->p_size = |
| 1880 | mdev->rs_start = |
| 1881 | mdev->rs_total = |
Lars Ellenberg | 1d7734a | 2010-08-11 21:21:50 +0200 | [diff] [blame] | 1882 | mdev->rs_failed = 0; |
| 1883 | mdev->rs_last_events = 0; |
Lars Ellenberg | 0f0601f | 2010-08-11 23:40:24 +0200 | [diff] [blame] | 1884 | mdev->rs_last_sect_ev = 0; |
Lars Ellenberg | 1d7734a | 2010-08-11 21:21:50 +0200 | [diff] [blame] | 1885 | for (i = 0; i < DRBD_SYNC_MARKS; i++) { |
| 1886 | mdev->rs_mark_left[i] = 0; |
| 1887 | mdev->rs_mark_time[i] = 0; |
| 1888 | } |
Philipp Reisner | 89e58e7 | 2011-01-19 13:12:45 +0100 | [diff] [blame] | 1889 | D_ASSERT(mdev->tconn->net_conf == NULL); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1890 | |
| 1891 | drbd_set_my_capacity(mdev, 0); |
| 1892 | if (mdev->bitmap) { |
| 1893 | /* maybe never allocated. */ |
Philipp Reisner | 02d9a94 | 2010-03-24 16:23:03 +0100 | [diff] [blame] | 1894 | drbd_bm_resize(mdev, 0, 1); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1895 | drbd_bm_cleanup(mdev); |
| 1896 | } |
| 1897 | |
| 1898 | drbd_free_resources(mdev); |
Philipp Reisner | 0778286 | 2010-08-31 12:00:50 +0200 | [diff] [blame] | 1899 | clear_bit(AL_SUSPENDED, &mdev->flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1900 | |
| 1901 | /* |
| 1902 | * currently we drbd_init_ee only on module load, so |
| 1903 | * we may do drbd_release_ee only on module unload! |
| 1904 | */ |
| 1905 | D_ASSERT(list_empty(&mdev->active_ee)); |
| 1906 | D_ASSERT(list_empty(&mdev->sync_ee)); |
| 1907 | D_ASSERT(list_empty(&mdev->done_ee)); |
| 1908 | D_ASSERT(list_empty(&mdev->read_ee)); |
| 1909 | D_ASSERT(list_empty(&mdev->net_ee)); |
| 1910 | D_ASSERT(list_empty(&mdev->resync_reads)); |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 1911 | D_ASSERT(list_empty(&mdev->tconn->data.work.q)); |
| 1912 | D_ASSERT(list_empty(&mdev->tconn->meta.work.q)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1913 | D_ASSERT(list_empty(&mdev->resync_work.list)); |
| 1914 | D_ASSERT(list_empty(&mdev->unplug_work.list)); |
Lars Ellenberg | e9e6f3e | 2010-09-14 20:26:27 +0200 | [diff] [blame] | 1915 | D_ASSERT(list_empty(&mdev->go_diskless.list)); |
Lars Ellenberg | 2265b47 | 2010-12-16 15:41:26 +0100 | [diff] [blame] | 1916 | |
| 1917 | drbd_set_defaults(mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1918 | } |
| 1919 | |
| 1920 | |
| 1921 | static void drbd_destroy_mempools(void) |
| 1922 | { |
| 1923 | struct page *page; |
| 1924 | |
| 1925 | while (drbd_pp_pool) { |
| 1926 | page = drbd_pp_pool; |
| 1927 | drbd_pp_pool = (struct page *)page_private(page); |
| 1928 | __free_page(page); |
| 1929 | drbd_pp_vacant--; |
| 1930 | } |
| 1931 | |
| 1932 | /* D_ASSERT(atomic_read(&drbd_pp_vacant)==0); */ |
| 1933 | |
| 1934 | if (drbd_ee_mempool) |
| 1935 | mempool_destroy(drbd_ee_mempool); |
| 1936 | if (drbd_request_mempool) |
| 1937 | mempool_destroy(drbd_request_mempool); |
| 1938 | if (drbd_ee_cache) |
| 1939 | kmem_cache_destroy(drbd_ee_cache); |
| 1940 | if (drbd_request_cache) |
| 1941 | kmem_cache_destroy(drbd_request_cache); |
| 1942 | if (drbd_bm_ext_cache) |
| 1943 | kmem_cache_destroy(drbd_bm_ext_cache); |
| 1944 | if (drbd_al_ext_cache) |
| 1945 | kmem_cache_destroy(drbd_al_ext_cache); |
| 1946 | |
| 1947 | drbd_ee_mempool = NULL; |
| 1948 | drbd_request_mempool = NULL; |
| 1949 | drbd_ee_cache = NULL; |
| 1950 | drbd_request_cache = NULL; |
| 1951 | drbd_bm_ext_cache = NULL; |
| 1952 | drbd_al_ext_cache = NULL; |
| 1953 | |
| 1954 | return; |
| 1955 | } |
| 1956 | |
| 1957 | static int drbd_create_mempools(void) |
| 1958 | { |
| 1959 | struct page *page; |
Lars Ellenberg | 1816a2b | 2010-11-11 15:19:07 +0100 | [diff] [blame] | 1960 | const int number = (DRBD_MAX_BIO_SIZE/PAGE_SIZE) * minor_count; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1961 | int i; |
| 1962 | |
| 1963 | /* prepare our caches and mempools */ |
| 1964 | drbd_request_mempool = NULL; |
| 1965 | drbd_ee_cache = NULL; |
| 1966 | drbd_request_cache = NULL; |
| 1967 | drbd_bm_ext_cache = NULL; |
| 1968 | drbd_al_ext_cache = NULL; |
| 1969 | drbd_pp_pool = NULL; |
| 1970 | |
| 1971 | /* caches */ |
| 1972 | drbd_request_cache = kmem_cache_create( |
| 1973 | "drbd_req", sizeof(struct drbd_request), 0, 0, NULL); |
| 1974 | if (drbd_request_cache == NULL) |
| 1975 | goto Enomem; |
| 1976 | |
| 1977 | drbd_ee_cache = kmem_cache_create( |
Andreas Gruenbacher | f6ffca9 | 2011-02-04 15:30:34 +0100 | [diff] [blame] | 1978 | "drbd_ee", sizeof(struct drbd_peer_request), 0, 0, NULL); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1979 | if (drbd_ee_cache == NULL) |
| 1980 | goto Enomem; |
| 1981 | |
| 1982 | drbd_bm_ext_cache = kmem_cache_create( |
| 1983 | "drbd_bm", sizeof(struct bm_extent), 0, 0, NULL); |
| 1984 | if (drbd_bm_ext_cache == NULL) |
| 1985 | goto Enomem; |
| 1986 | |
| 1987 | drbd_al_ext_cache = kmem_cache_create( |
| 1988 | "drbd_al", sizeof(struct lc_element), 0, 0, NULL); |
| 1989 | if (drbd_al_ext_cache == NULL) |
| 1990 | goto Enomem; |
| 1991 | |
| 1992 | /* mempools */ |
| 1993 | drbd_request_mempool = mempool_create(number, |
| 1994 | mempool_alloc_slab, mempool_free_slab, drbd_request_cache); |
| 1995 | if (drbd_request_mempool == NULL) |
| 1996 | goto Enomem; |
| 1997 | |
| 1998 | drbd_ee_mempool = mempool_create(number, |
| 1999 | mempool_alloc_slab, mempool_free_slab, drbd_ee_cache); |
Nicolas Kaiser | 2027ae1 | 2010-10-28 06:15:26 -0600 | [diff] [blame] | 2000 | if (drbd_ee_mempool == NULL) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2001 | goto Enomem; |
| 2002 | |
| 2003 | /* drbd's page pool */ |
| 2004 | spin_lock_init(&drbd_pp_lock); |
| 2005 | |
| 2006 | for (i = 0; i < number; i++) { |
| 2007 | page = alloc_page(GFP_HIGHUSER); |
| 2008 | if (!page) |
| 2009 | goto Enomem; |
| 2010 | set_page_private(page, (unsigned long)drbd_pp_pool); |
| 2011 | drbd_pp_pool = page; |
| 2012 | } |
| 2013 | drbd_pp_vacant = number; |
| 2014 | |
| 2015 | return 0; |
| 2016 | |
| 2017 | Enomem: |
| 2018 | drbd_destroy_mempools(); /* in case we allocated some */ |
| 2019 | return -ENOMEM; |
| 2020 | } |
| 2021 | |
| 2022 | static int drbd_notify_sys(struct notifier_block *this, unsigned long code, |
| 2023 | void *unused) |
| 2024 | { |
| 2025 | /* just so we have it. you never know what interesting things we |
| 2026 | * might want to do here some day... |
| 2027 | */ |
| 2028 | |
| 2029 | return NOTIFY_DONE; |
| 2030 | } |
| 2031 | |
| 2032 | static struct notifier_block drbd_notifier = { |
| 2033 | .notifier_call = drbd_notify_sys, |
| 2034 | }; |
| 2035 | |
| 2036 | static void drbd_release_ee_lists(struct drbd_conf *mdev) |
| 2037 | { |
| 2038 | int rr; |
| 2039 | |
| 2040 | rr = drbd_release_ee(mdev, &mdev->active_ee); |
| 2041 | if (rr) |
| 2042 | dev_err(DEV, "%d EEs in active list found!\n", rr); |
| 2043 | |
| 2044 | rr = drbd_release_ee(mdev, &mdev->sync_ee); |
| 2045 | if (rr) |
| 2046 | dev_err(DEV, "%d EEs in sync list found!\n", rr); |
| 2047 | |
| 2048 | rr = drbd_release_ee(mdev, &mdev->read_ee); |
| 2049 | if (rr) |
| 2050 | dev_err(DEV, "%d EEs in read list found!\n", rr); |
| 2051 | |
| 2052 | rr = drbd_release_ee(mdev, &mdev->done_ee); |
| 2053 | if (rr) |
| 2054 | dev_err(DEV, "%d EEs in done list found!\n", rr); |
| 2055 | |
| 2056 | rr = drbd_release_ee(mdev, &mdev->net_ee); |
| 2057 | if (rr) |
| 2058 | dev_err(DEV, "%d EEs in net list found!\n", rr); |
| 2059 | } |
| 2060 | |
| 2061 | /* caution. no locking. |
| 2062 | * currently only used from module cleanup code. */ |
| 2063 | static void drbd_delete_device(unsigned int minor) |
| 2064 | { |
| 2065 | struct drbd_conf *mdev = minor_to_mdev(minor); |
| 2066 | |
| 2067 | if (!mdev) |
| 2068 | return; |
| 2069 | |
| 2070 | /* paranoia asserts */ |
Andreas Gruenbacher | 70dc65e | 2010-12-21 14:46:57 +0100 | [diff] [blame] | 2071 | D_ASSERT(mdev->open_cnt == 0); |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 2072 | D_ASSERT(list_empty(&mdev->tconn->data.work.q)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2073 | /* end paranoia asserts */ |
| 2074 | |
| 2075 | del_gendisk(mdev->vdisk); |
| 2076 | |
| 2077 | /* cleanup stuff that may have been allocated during |
| 2078 | * device (re-)configuration or state changes */ |
| 2079 | |
| 2080 | if (mdev->this_bdev) |
| 2081 | bdput(mdev->this_bdev); |
| 2082 | |
| 2083 | drbd_free_resources(mdev); |
Philipp Reisner | 2111438 | 2011-01-19 12:26:59 +0100 | [diff] [blame] | 2084 | drbd_free_tconn(mdev->tconn); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2085 | |
| 2086 | drbd_release_ee_lists(mdev); |
| 2087 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2088 | lc_destroy(mdev->act_log); |
| 2089 | lc_destroy(mdev->resync); |
| 2090 | |
| 2091 | kfree(mdev->p_uuid); |
| 2092 | /* mdev->p_uuid = NULL; */ |
| 2093 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2094 | /* cleanup the rest that has been |
| 2095 | * allocated from drbd_new_device |
| 2096 | * and actually free the mdev itself */ |
| 2097 | drbd_free_mdev(mdev); |
| 2098 | } |
| 2099 | |
| 2100 | static void drbd_cleanup(void) |
| 2101 | { |
| 2102 | unsigned int i; |
| 2103 | |
| 2104 | unregister_reboot_notifier(&drbd_notifier); |
| 2105 | |
Lars Ellenberg | 17a93f3 | 2010-11-24 10:37:35 +0100 | [diff] [blame] | 2106 | /* first remove proc, |
| 2107 | * drbdsetup uses it's presence to detect |
| 2108 | * whether DRBD is loaded. |
| 2109 | * If we would get stuck in proc removal, |
| 2110 | * but have netlink already deregistered, |
| 2111 | * some drbdsetup commands may wait forever |
| 2112 | * for an answer. |
| 2113 | */ |
| 2114 | if (drbd_proc) |
| 2115 | remove_proc_entry("drbd", NULL); |
| 2116 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2117 | drbd_nl_cleanup(); |
| 2118 | |
| 2119 | if (minor_table) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2120 | i = minor_count; |
| 2121 | while (i--) |
| 2122 | drbd_delete_device(i); |
| 2123 | drbd_destroy_mempools(); |
| 2124 | } |
| 2125 | |
| 2126 | kfree(minor_table); |
| 2127 | |
| 2128 | unregister_blkdev(DRBD_MAJOR, "drbd"); |
| 2129 | |
| 2130 | printk(KERN_INFO "drbd: module cleanup done.\n"); |
| 2131 | } |
| 2132 | |
| 2133 | /** |
| 2134 | * drbd_congested() - Callback for pdflush |
| 2135 | * @congested_data: User data |
| 2136 | * @bdi_bits: Bits pdflush is currently interested in |
| 2137 | * |
| 2138 | * Returns 1<<BDI_async_congested and/or 1<<BDI_sync_congested if we are congested. |
| 2139 | */ |
| 2140 | static int drbd_congested(void *congested_data, int bdi_bits) |
| 2141 | { |
| 2142 | struct drbd_conf *mdev = congested_data; |
| 2143 | struct request_queue *q; |
| 2144 | char reason = '-'; |
| 2145 | int r = 0; |
| 2146 | |
Andreas Gruenbacher | 1b881ef | 2010-12-13 18:03:38 +0100 | [diff] [blame] | 2147 | if (!may_inc_ap_bio(mdev)) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2148 | /* DRBD has frozen IO */ |
| 2149 | r = bdi_bits; |
| 2150 | reason = 'd'; |
| 2151 | goto out; |
| 2152 | } |
| 2153 | |
| 2154 | if (get_ldev(mdev)) { |
| 2155 | q = bdev_get_queue(mdev->ldev->backing_bdev); |
| 2156 | r = bdi_congested(&q->backing_dev_info, bdi_bits); |
| 2157 | put_ldev(mdev); |
| 2158 | if (r) |
| 2159 | reason = 'b'; |
| 2160 | } |
| 2161 | |
Philipp Reisner | 01a311a | 2011-02-07 14:30:33 +0100 | [diff] [blame] | 2162 | if (bdi_bits & (1 << BDI_async_congested) && test_bit(NET_CONGESTED, &mdev->tconn->flags)) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2163 | r |= (1 << BDI_async_congested); |
| 2164 | reason = reason == 'b' ? 'a' : 'n'; |
| 2165 | } |
| 2166 | |
| 2167 | out: |
| 2168 | mdev->congestion_reason = reason; |
| 2169 | return r; |
| 2170 | } |
| 2171 | |
Philipp Reisner | 2111438 | 2011-01-19 12:26:59 +0100 | [diff] [blame] | 2172 | struct drbd_tconn *drbd_new_tconn(char *name) |
| 2173 | { |
| 2174 | struct drbd_tconn *tconn; |
| 2175 | |
| 2176 | tconn = kzalloc(sizeof(struct drbd_tconn), GFP_KERNEL); |
| 2177 | if (!tconn) |
| 2178 | return NULL; |
| 2179 | |
| 2180 | tconn->name = kstrdup(name, GFP_KERNEL); |
| 2181 | if (!tconn->name) |
| 2182 | goto fail; |
| 2183 | |
Philipp Reisner | b2fb6dbe | 2011-01-19 13:48:44 +0100 | [diff] [blame] | 2184 | atomic_set(&tconn->net_cnt, 0); |
| 2185 | init_waitqueue_head(&tconn->net_cnt_wait); |
Philipp Reisner | 062e879 | 2011-02-08 11:09:18 +0100 | [diff] [blame^] | 2186 | idr_init(&tconn->volumes); |
Philipp Reisner | b2fb6dbe | 2011-01-19 13:48:44 +0100 | [diff] [blame] | 2187 | |
Philipp Reisner | 2111438 | 2011-01-19 12:26:59 +0100 | [diff] [blame] | 2188 | write_lock_irq(&global_state_lock); |
| 2189 | list_add(&tconn->all_tconn, &drbd_tconns); |
| 2190 | write_unlock_irq(&global_state_lock); |
| 2191 | |
| 2192 | return tconn; |
| 2193 | |
| 2194 | fail: |
| 2195 | kfree(tconn->name); |
| 2196 | kfree(tconn); |
| 2197 | |
| 2198 | return NULL; |
| 2199 | } |
| 2200 | |
| 2201 | void drbd_free_tconn(struct drbd_tconn *tconn) |
| 2202 | { |
| 2203 | write_lock_irq(&global_state_lock); |
| 2204 | list_del(&tconn->all_tconn); |
| 2205 | write_unlock_irq(&global_state_lock); |
Philipp Reisner | 062e879 | 2011-02-08 11:09:18 +0100 | [diff] [blame^] | 2206 | idr_destroy(&tconn->volumes); |
Philipp Reisner | 2111438 | 2011-01-19 12:26:59 +0100 | [diff] [blame] | 2207 | |
| 2208 | kfree(tconn->name); |
Philipp Reisner | b42a70a | 2011-01-27 10:55:20 +0100 | [diff] [blame] | 2209 | kfree(tconn->int_dig_out); |
| 2210 | kfree(tconn->int_dig_in); |
| 2211 | kfree(tconn->int_dig_vv); |
Philipp Reisner | 2111438 | 2011-01-19 12:26:59 +0100 | [diff] [blame] | 2212 | kfree(tconn); |
| 2213 | } |
| 2214 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2215 | struct drbd_conf *drbd_new_device(unsigned int minor) |
| 2216 | { |
| 2217 | struct drbd_conf *mdev; |
| 2218 | struct gendisk *disk; |
| 2219 | struct request_queue *q; |
Philipp Reisner | 60ae496 | 2011-02-07 14:01:51 +0100 | [diff] [blame] | 2220 | char conn_name[9]; /* drbd1234N */ |
Philipp Reisner | 062e879 | 2011-02-08 11:09:18 +0100 | [diff] [blame^] | 2221 | int vnr; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2222 | |
| 2223 | /* GFP_KERNEL, we are outside of all write-out paths */ |
| 2224 | mdev = kzalloc(sizeof(struct drbd_conf), GFP_KERNEL); |
| 2225 | if (!mdev) |
| 2226 | return NULL; |
Philipp Reisner | 60ae496 | 2011-02-07 14:01:51 +0100 | [diff] [blame] | 2227 | sprintf(conn_name, "drbd%d", minor); |
| 2228 | mdev->tconn = drbd_new_tconn(conn_name); |
Philipp Reisner | 2111438 | 2011-01-19 12:26:59 +0100 | [diff] [blame] | 2229 | if (!mdev->tconn) |
| 2230 | goto out_no_tconn; |
Philipp Reisner | 062e879 | 2011-02-08 11:09:18 +0100 | [diff] [blame^] | 2231 | if (!idr_pre_get(&mdev->tconn->volumes, GFP_KERNEL)) |
| 2232 | goto out_no_cpumask; |
| 2233 | if (idr_get_new(&mdev->tconn->volumes, mdev, &vnr)) |
| 2234 | goto out_no_cpumask; |
| 2235 | if (vnr != 0) { |
| 2236 | dev_err(DEV, "vnr = %d\n", vnr); |
| 2237 | goto out_no_cpumask; |
| 2238 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2239 | if (!zalloc_cpumask_var(&mdev->cpu_mask, GFP_KERNEL)) |
| 2240 | goto out_no_cpumask; |
| 2241 | |
Philipp Reisner | 2111438 | 2011-01-19 12:26:59 +0100 | [diff] [blame] | 2242 | mdev->tconn->volume0 = mdev; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2243 | mdev->minor = minor; |
| 2244 | |
| 2245 | drbd_init_set_defaults(mdev); |
| 2246 | |
| 2247 | q = blk_alloc_queue(GFP_KERNEL); |
| 2248 | if (!q) |
| 2249 | goto out_no_q; |
| 2250 | mdev->rq_queue = q; |
| 2251 | q->queuedata = mdev; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2252 | |
| 2253 | disk = alloc_disk(1); |
| 2254 | if (!disk) |
| 2255 | goto out_no_disk; |
| 2256 | mdev->vdisk = disk; |
| 2257 | |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 2258 | set_disk_ro(disk, true); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2259 | |
| 2260 | disk->queue = q; |
| 2261 | disk->major = DRBD_MAJOR; |
| 2262 | disk->first_minor = minor; |
| 2263 | disk->fops = &drbd_ops; |
| 2264 | sprintf(disk->disk_name, "drbd%d", minor); |
| 2265 | disk->private_data = mdev; |
| 2266 | |
| 2267 | mdev->this_bdev = bdget(MKDEV(DRBD_MAJOR, minor)); |
| 2268 | /* we have no partitions. we contain only ourselves. */ |
| 2269 | mdev->this_bdev->bd_contains = mdev->this_bdev; |
| 2270 | |
| 2271 | q->backing_dev_info.congested_fn = drbd_congested; |
| 2272 | q->backing_dev_info.congested_data = mdev; |
| 2273 | |
Andreas Gruenbacher | 2f58dcf | 2010-12-13 17:48:19 +0100 | [diff] [blame] | 2274 | blk_queue_make_request(q, drbd_make_request); |
Philipp Reisner | 99432fc | 2011-05-20 16:39:13 +0200 | [diff] [blame] | 2275 | /* Setting the max_hw_sectors to an odd value of 8kibyte here |
| 2276 | This triggers a max_bio_size message upon first attach or connect */ |
| 2277 | blk_queue_max_hw_sectors(q, DRBD_MAX_BIO_SIZE_SAFE >> 8); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2278 | blk_queue_bounce_limit(q, BLK_BOUNCE_ANY); |
| 2279 | blk_queue_merge_bvec(q, drbd_merge_bvec); |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 2280 | q->queue_lock = &mdev->tconn->req_lock; /* needed since we use */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2281 | |
| 2282 | mdev->md_io_page = alloc_page(GFP_KERNEL); |
| 2283 | if (!mdev->md_io_page) |
| 2284 | goto out_no_io_page; |
| 2285 | |
| 2286 | if (drbd_bm_init(mdev)) |
| 2287 | goto out_no_bitmap; |
| 2288 | /* no need to lock access, we are still initializing this minor device. */ |
| 2289 | if (!tl_init(mdev)) |
| 2290 | goto out_no_tl; |
Andreas Gruenbacher | dac1389 | 2011-01-21 17:18:39 +0100 | [diff] [blame] | 2291 | mdev->read_requests = RB_ROOT; |
Andreas Gruenbacher | de69671 | 2011-01-20 15:00:24 +0100 | [diff] [blame] | 2292 | mdev->write_requests = RB_ROOT; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2293 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2294 | mdev->current_epoch = kzalloc(sizeof(struct drbd_epoch), GFP_KERNEL); |
| 2295 | if (!mdev->current_epoch) |
| 2296 | goto out_no_epoch; |
| 2297 | |
| 2298 | INIT_LIST_HEAD(&mdev->current_epoch->list); |
| 2299 | mdev->epochs = 1; |
| 2300 | |
| 2301 | return mdev; |
| 2302 | |
| 2303 | /* out_whatever_else: |
| 2304 | kfree(mdev->current_epoch); */ |
| 2305 | out_no_epoch: |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2306 | tl_cleanup(mdev); |
| 2307 | out_no_tl: |
| 2308 | drbd_bm_cleanup(mdev); |
| 2309 | out_no_bitmap: |
| 2310 | __free_page(mdev->md_io_page); |
| 2311 | out_no_io_page: |
| 2312 | put_disk(disk); |
| 2313 | out_no_disk: |
| 2314 | blk_cleanup_queue(q); |
| 2315 | out_no_q: |
| 2316 | free_cpumask_var(mdev->cpu_mask); |
| 2317 | out_no_cpumask: |
Philipp Reisner | 2111438 | 2011-01-19 12:26:59 +0100 | [diff] [blame] | 2318 | drbd_free_tconn(mdev->tconn); |
| 2319 | out_no_tconn: |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2320 | kfree(mdev); |
| 2321 | return NULL; |
| 2322 | } |
| 2323 | |
| 2324 | /* counterpart of drbd_new_device. |
| 2325 | * last part of drbd_delete_device. */ |
| 2326 | void drbd_free_mdev(struct drbd_conf *mdev) |
| 2327 | { |
| 2328 | kfree(mdev->current_epoch); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2329 | tl_cleanup(mdev); |
| 2330 | if (mdev->bitmap) /* should no longer be there. */ |
| 2331 | drbd_bm_cleanup(mdev); |
| 2332 | __free_page(mdev->md_io_page); |
| 2333 | put_disk(mdev->vdisk); |
| 2334 | blk_cleanup_queue(mdev->rq_queue); |
| 2335 | free_cpumask_var(mdev->cpu_mask); |
| 2336 | kfree(mdev); |
| 2337 | } |
| 2338 | |
| 2339 | |
| 2340 | int __init drbd_init(void) |
| 2341 | { |
| 2342 | int err; |
| 2343 | |
Philipp Reisner | fd340c1 | 2011-01-19 16:57:39 +0100 | [diff] [blame] | 2344 | BUILD_BUG_ON(sizeof(struct p_header80) != sizeof(struct p_header95)); |
| 2345 | BUILD_BUG_ON(sizeof(struct p_handshake) != 80); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2346 | |
Philipp Reisner | 2b8a90b | 2011-01-10 11:15:17 +0100 | [diff] [blame] | 2347 | if (minor_count < DRBD_MINOR_COUNT_MIN || minor_count > DRBD_MINOR_COUNT_MAX) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2348 | printk(KERN_ERR |
| 2349 | "drbd: invalid minor_count (%d)\n", minor_count); |
| 2350 | #ifdef MODULE |
| 2351 | return -EINVAL; |
| 2352 | #else |
| 2353 | minor_count = 8; |
| 2354 | #endif |
| 2355 | } |
| 2356 | |
| 2357 | err = drbd_nl_init(); |
| 2358 | if (err) |
| 2359 | return err; |
| 2360 | |
| 2361 | err = register_blkdev(DRBD_MAJOR, "drbd"); |
| 2362 | if (err) { |
| 2363 | printk(KERN_ERR |
| 2364 | "drbd: unable to register block device major %d\n", |
| 2365 | DRBD_MAJOR); |
| 2366 | return err; |
| 2367 | } |
| 2368 | |
| 2369 | register_reboot_notifier(&drbd_notifier); |
| 2370 | |
| 2371 | /* |
| 2372 | * allocate all necessary structs |
| 2373 | */ |
| 2374 | err = -ENOMEM; |
| 2375 | |
| 2376 | init_waitqueue_head(&drbd_pp_wait); |
| 2377 | |
| 2378 | drbd_proc = NULL; /* play safe for drbd_cleanup */ |
| 2379 | minor_table = kzalloc(sizeof(struct drbd_conf *)*minor_count, |
| 2380 | GFP_KERNEL); |
| 2381 | if (!minor_table) |
| 2382 | goto Enomem; |
| 2383 | |
| 2384 | err = drbd_create_mempools(); |
| 2385 | if (err) |
| 2386 | goto Enomem; |
| 2387 | |
Lars Ellenberg | 8c484ee | 2010-03-11 16:47:58 +0100 | [diff] [blame] | 2388 | drbd_proc = proc_create_data("drbd", S_IFREG | S_IRUGO , NULL, &drbd_proc_fops, NULL); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2389 | if (!drbd_proc) { |
| 2390 | printk(KERN_ERR "drbd: unable to register proc file\n"); |
| 2391 | goto Enomem; |
| 2392 | } |
| 2393 | |
| 2394 | rwlock_init(&global_state_lock); |
Philipp Reisner | 2111438 | 2011-01-19 12:26:59 +0100 | [diff] [blame] | 2395 | INIT_LIST_HEAD(&drbd_tconns); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2396 | |
| 2397 | printk(KERN_INFO "drbd: initialized. " |
| 2398 | "Version: " REL_VERSION " (api:%d/proto:%d-%d)\n", |
| 2399 | API_VERSION, PRO_VERSION_MIN, PRO_VERSION_MAX); |
| 2400 | printk(KERN_INFO "drbd: %s\n", drbd_buildtag()); |
| 2401 | printk(KERN_INFO "drbd: registered as block device major %d\n", |
| 2402 | DRBD_MAJOR); |
| 2403 | printk(KERN_INFO "drbd: minor_table @ 0x%p\n", minor_table); |
| 2404 | |
| 2405 | return 0; /* Success! */ |
| 2406 | |
| 2407 | Enomem: |
| 2408 | drbd_cleanup(); |
| 2409 | if (err == -ENOMEM) |
| 2410 | /* currently always the case */ |
| 2411 | printk(KERN_ERR "drbd: ran out of memory\n"); |
| 2412 | else |
| 2413 | printk(KERN_ERR "drbd: initialization failure\n"); |
| 2414 | return err; |
| 2415 | } |
| 2416 | |
| 2417 | void drbd_free_bc(struct drbd_backing_dev *ldev) |
| 2418 | { |
| 2419 | if (ldev == NULL) |
| 2420 | return; |
| 2421 | |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2422 | blkdev_put(ldev->backing_bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); |
| 2423 | blkdev_put(ldev->md_bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2424 | |
| 2425 | kfree(ldev); |
| 2426 | } |
| 2427 | |
| 2428 | void drbd_free_sock(struct drbd_conf *mdev) |
| 2429 | { |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 2430 | if (mdev->tconn->data.socket) { |
| 2431 | mutex_lock(&mdev->tconn->data.mutex); |
| 2432 | kernel_sock_shutdown(mdev->tconn->data.socket, SHUT_RDWR); |
| 2433 | sock_release(mdev->tconn->data.socket); |
| 2434 | mdev->tconn->data.socket = NULL; |
| 2435 | mutex_unlock(&mdev->tconn->data.mutex); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2436 | } |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 2437 | if (mdev->tconn->meta.socket) { |
| 2438 | mutex_lock(&mdev->tconn->meta.mutex); |
| 2439 | kernel_sock_shutdown(mdev->tconn->meta.socket, SHUT_RDWR); |
| 2440 | sock_release(mdev->tconn->meta.socket); |
| 2441 | mdev->tconn->meta.socket = NULL; |
| 2442 | mutex_unlock(&mdev->tconn->meta.mutex); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2443 | } |
| 2444 | } |
| 2445 | |
| 2446 | |
| 2447 | void drbd_free_resources(struct drbd_conf *mdev) |
| 2448 | { |
| 2449 | crypto_free_hash(mdev->csums_tfm); |
| 2450 | mdev->csums_tfm = NULL; |
| 2451 | crypto_free_hash(mdev->verify_tfm); |
| 2452 | mdev->verify_tfm = NULL; |
Philipp Reisner | a063845 | 2011-01-19 14:31:32 +0100 | [diff] [blame] | 2453 | crypto_free_hash(mdev->tconn->cram_hmac_tfm); |
| 2454 | mdev->tconn->cram_hmac_tfm = NULL; |
| 2455 | crypto_free_hash(mdev->tconn->integrity_w_tfm); |
| 2456 | mdev->tconn->integrity_w_tfm = NULL; |
| 2457 | crypto_free_hash(mdev->tconn->integrity_r_tfm); |
| 2458 | mdev->tconn->integrity_r_tfm = NULL; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2459 | |
| 2460 | drbd_free_sock(mdev); |
| 2461 | |
| 2462 | __no_warn(local, |
| 2463 | drbd_free_bc(mdev->ldev); |
| 2464 | mdev->ldev = NULL;); |
| 2465 | } |
| 2466 | |
| 2467 | /* meta data management */ |
| 2468 | |
| 2469 | struct meta_data_on_disk { |
| 2470 | u64 la_size; /* last agreed size. */ |
| 2471 | u64 uuid[UI_SIZE]; /* UUIDs. */ |
| 2472 | u64 device_uuid; |
| 2473 | u64 reserved_u64_1; |
| 2474 | u32 flags; /* MDF */ |
| 2475 | u32 magic; |
| 2476 | u32 md_size_sect; |
| 2477 | u32 al_offset; /* offset to this block */ |
| 2478 | u32 al_nr_extents; /* important for restoring the AL */ |
| 2479 | /* `-- act_log->nr_elements <-- sync_conf.al_extents */ |
| 2480 | u32 bm_offset; /* offset to the bitmap, from here */ |
| 2481 | u32 bm_bytes_per_bit; /* BM_BLOCK_SIZE */ |
Philipp Reisner | 99432fc | 2011-05-20 16:39:13 +0200 | [diff] [blame] | 2482 | u32 la_peer_max_bio_size; /* last peer max_bio_size */ |
| 2483 | u32 reserved_u32[3]; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2484 | |
| 2485 | } __packed; |
| 2486 | |
| 2487 | /** |
| 2488 | * drbd_md_sync() - Writes the meta data super block if the MD_DIRTY flag bit is set |
| 2489 | * @mdev: DRBD device. |
| 2490 | */ |
| 2491 | void drbd_md_sync(struct drbd_conf *mdev) |
| 2492 | { |
| 2493 | struct meta_data_on_disk *buffer; |
| 2494 | sector_t sector; |
| 2495 | int i; |
| 2496 | |
Lars Ellenberg | ee15b03 | 2010-09-03 10:00:09 +0200 | [diff] [blame] | 2497 | del_timer(&mdev->md_sync_timer); |
| 2498 | /* timer may be rearmed by drbd_md_mark_dirty() now. */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2499 | if (!test_and_clear_bit(MD_DIRTY, &mdev->flags)) |
| 2500 | return; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2501 | |
| 2502 | /* We use here D_FAILED and not D_ATTACHING because we try to write |
| 2503 | * metadata even if we detach due to a disk failure! */ |
| 2504 | if (!get_ldev_if_state(mdev, D_FAILED)) |
| 2505 | return; |
| 2506 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2507 | mutex_lock(&mdev->md_io_mutex); |
| 2508 | buffer = (struct meta_data_on_disk *)page_address(mdev->md_io_page); |
| 2509 | memset(buffer, 0, 512); |
| 2510 | |
| 2511 | buffer->la_size = cpu_to_be64(drbd_get_capacity(mdev->this_bdev)); |
| 2512 | for (i = UI_CURRENT; i < UI_SIZE; i++) |
| 2513 | buffer->uuid[i] = cpu_to_be64(mdev->ldev->md.uuid[i]); |
| 2514 | buffer->flags = cpu_to_be32(mdev->ldev->md.flags); |
| 2515 | buffer->magic = cpu_to_be32(DRBD_MD_MAGIC); |
| 2516 | |
| 2517 | buffer->md_size_sect = cpu_to_be32(mdev->ldev->md.md_size_sect); |
| 2518 | buffer->al_offset = cpu_to_be32(mdev->ldev->md.al_offset); |
| 2519 | buffer->al_nr_extents = cpu_to_be32(mdev->act_log->nr_elements); |
| 2520 | buffer->bm_bytes_per_bit = cpu_to_be32(BM_BLOCK_SIZE); |
| 2521 | buffer->device_uuid = cpu_to_be64(mdev->ldev->md.device_uuid); |
| 2522 | |
| 2523 | buffer->bm_offset = cpu_to_be32(mdev->ldev->md.bm_offset); |
Philipp Reisner | 99432fc | 2011-05-20 16:39:13 +0200 | [diff] [blame] | 2524 | buffer->la_peer_max_bio_size = cpu_to_be32(mdev->peer_max_bio_size); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2525 | |
| 2526 | D_ASSERT(drbd_md_ss__(mdev, mdev->ldev) == mdev->ldev->md.md_offset); |
| 2527 | sector = mdev->ldev->md.md_offset; |
| 2528 | |
Lars Ellenberg | 3f3a9b8 | 2010-09-01 15:12:12 +0200 | [diff] [blame] | 2529 | if (!drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE)) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2530 | /* this was a try anyways ... */ |
| 2531 | dev_err(DEV, "meta data update failed!\n"); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 2532 | drbd_chk_io_error(mdev, 1, true); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2533 | } |
| 2534 | |
| 2535 | /* Update mdev->ldev->md.la_size_sect, |
| 2536 | * since we updated it on metadata. */ |
| 2537 | mdev->ldev->md.la_size_sect = drbd_get_capacity(mdev->this_bdev); |
| 2538 | |
| 2539 | mutex_unlock(&mdev->md_io_mutex); |
| 2540 | put_ldev(mdev); |
| 2541 | } |
| 2542 | |
| 2543 | /** |
| 2544 | * drbd_md_read() - Reads in the meta data super block |
| 2545 | * @mdev: DRBD device. |
| 2546 | * @bdev: Device from which the meta data should be read in. |
| 2547 | * |
Andreas Gruenbacher | 116676c | 2010-12-08 13:33:11 +0100 | [diff] [blame] | 2548 | * Return 0 (NO_ERROR) on success, and an enum drbd_ret_code in case |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2549 | * something goes wrong. Currently only: ERR_IO_MD_DISK, ERR_MD_INVALID. |
| 2550 | */ |
| 2551 | int drbd_md_read(struct drbd_conf *mdev, struct drbd_backing_dev *bdev) |
| 2552 | { |
| 2553 | struct meta_data_on_disk *buffer; |
| 2554 | int i, rv = NO_ERROR; |
| 2555 | |
| 2556 | if (!get_ldev_if_state(mdev, D_ATTACHING)) |
| 2557 | return ERR_IO_MD_DISK; |
| 2558 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2559 | mutex_lock(&mdev->md_io_mutex); |
| 2560 | buffer = (struct meta_data_on_disk *)page_address(mdev->md_io_page); |
| 2561 | |
| 2562 | if (!drbd_md_sync_page_io(mdev, bdev, bdev->md.md_offset, READ)) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2563 | /* NOTE: can't do normal error processing here as this is |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2564 | called BEFORE disk is attached */ |
| 2565 | dev_err(DEV, "Error while reading metadata.\n"); |
| 2566 | rv = ERR_IO_MD_DISK; |
| 2567 | goto err; |
| 2568 | } |
| 2569 | |
Andreas Gruenbacher | e7fad8a | 2011-01-11 13:54:02 +0100 | [diff] [blame] | 2570 | if (buffer->magic != cpu_to_be32(DRBD_MD_MAGIC)) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2571 | dev_err(DEV, "Error while reading metadata, magic not found.\n"); |
| 2572 | rv = ERR_MD_INVALID; |
| 2573 | goto err; |
| 2574 | } |
| 2575 | if (be32_to_cpu(buffer->al_offset) != bdev->md.al_offset) { |
| 2576 | dev_err(DEV, "unexpected al_offset: %d (expected %d)\n", |
| 2577 | be32_to_cpu(buffer->al_offset), bdev->md.al_offset); |
| 2578 | rv = ERR_MD_INVALID; |
| 2579 | goto err; |
| 2580 | } |
| 2581 | if (be32_to_cpu(buffer->bm_offset) != bdev->md.bm_offset) { |
| 2582 | dev_err(DEV, "unexpected bm_offset: %d (expected %d)\n", |
| 2583 | be32_to_cpu(buffer->bm_offset), bdev->md.bm_offset); |
| 2584 | rv = ERR_MD_INVALID; |
| 2585 | goto err; |
| 2586 | } |
| 2587 | if (be32_to_cpu(buffer->md_size_sect) != bdev->md.md_size_sect) { |
| 2588 | dev_err(DEV, "unexpected md_size: %u (expected %u)\n", |
| 2589 | be32_to_cpu(buffer->md_size_sect), bdev->md.md_size_sect); |
| 2590 | rv = ERR_MD_INVALID; |
| 2591 | goto err; |
| 2592 | } |
| 2593 | |
| 2594 | if (be32_to_cpu(buffer->bm_bytes_per_bit) != BM_BLOCK_SIZE) { |
| 2595 | dev_err(DEV, "unexpected bm_bytes_per_bit: %u (expected %u)\n", |
| 2596 | be32_to_cpu(buffer->bm_bytes_per_bit), BM_BLOCK_SIZE); |
| 2597 | rv = ERR_MD_INVALID; |
| 2598 | goto err; |
| 2599 | } |
| 2600 | |
| 2601 | bdev->md.la_size_sect = be64_to_cpu(buffer->la_size); |
| 2602 | for (i = UI_CURRENT; i < UI_SIZE; i++) |
| 2603 | bdev->md.uuid[i] = be64_to_cpu(buffer->uuid[i]); |
| 2604 | bdev->md.flags = be32_to_cpu(buffer->flags); |
| 2605 | mdev->sync_conf.al_extents = be32_to_cpu(buffer->al_nr_extents); |
| 2606 | bdev->md.device_uuid = be64_to_cpu(buffer->device_uuid); |
| 2607 | |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 2608 | spin_lock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | 99432fc | 2011-05-20 16:39:13 +0200 | [diff] [blame] | 2609 | if (mdev->state.conn < C_CONNECTED) { |
| 2610 | int peer; |
| 2611 | peer = be32_to_cpu(buffer->la_peer_max_bio_size); |
| 2612 | peer = max_t(int, peer, DRBD_MAX_BIO_SIZE_SAFE); |
| 2613 | mdev->peer_max_bio_size = peer; |
| 2614 | } |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 2615 | spin_unlock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | 99432fc | 2011-05-20 16:39:13 +0200 | [diff] [blame] | 2616 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2617 | if (mdev->sync_conf.al_extents < 7) |
| 2618 | mdev->sync_conf.al_extents = 127; |
| 2619 | |
| 2620 | err: |
| 2621 | mutex_unlock(&mdev->md_io_mutex); |
| 2622 | put_ldev(mdev); |
| 2623 | |
| 2624 | return rv; |
| 2625 | } |
| 2626 | |
| 2627 | /** |
| 2628 | * drbd_md_mark_dirty() - Mark meta data super block as dirty |
| 2629 | * @mdev: DRBD device. |
| 2630 | * |
| 2631 | * Call this function if you change anything that should be written to |
| 2632 | * the meta-data super block. This function sets MD_DIRTY, and starts a |
| 2633 | * timer that ensures that within five seconds you have to call drbd_md_sync(). |
| 2634 | */ |
Lars Ellenberg | ca0e609 | 2010-10-14 15:01:21 +0200 | [diff] [blame] | 2635 | #ifdef DEBUG |
Lars Ellenberg | ee15b03 | 2010-09-03 10:00:09 +0200 | [diff] [blame] | 2636 | void drbd_md_mark_dirty_(struct drbd_conf *mdev, unsigned int line, const char *func) |
| 2637 | { |
| 2638 | if (!test_and_set_bit(MD_DIRTY, &mdev->flags)) { |
| 2639 | mod_timer(&mdev->md_sync_timer, jiffies + HZ); |
| 2640 | mdev->last_md_mark_dirty.line = line; |
| 2641 | mdev->last_md_mark_dirty.func = func; |
| 2642 | } |
| 2643 | } |
| 2644 | #else |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2645 | void drbd_md_mark_dirty(struct drbd_conf *mdev) |
| 2646 | { |
Lars Ellenberg | ee15b03 | 2010-09-03 10:00:09 +0200 | [diff] [blame] | 2647 | if (!test_and_set_bit(MD_DIRTY, &mdev->flags)) |
Lars Ellenberg | ca0e609 | 2010-10-14 15:01:21 +0200 | [diff] [blame] | 2648 | mod_timer(&mdev->md_sync_timer, jiffies + 5*HZ); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2649 | } |
Lars Ellenberg | ee15b03 | 2010-09-03 10:00:09 +0200 | [diff] [blame] | 2650 | #endif |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2651 | |
| 2652 | static void drbd_uuid_move_history(struct drbd_conf *mdev) __must_hold(local) |
| 2653 | { |
| 2654 | int i; |
| 2655 | |
Lars Ellenberg | 62b0da3 | 2011-01-20 13:25:21 +0100 | [diff] [blame] | 2656 | for (i = UI_HISTORY_START; i < UI_HISTORY_END; i++) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2657 | mdev->ldev->md.uuid[i+1] = mdev->ldev->md.uuid[i]; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2658 | } |
| 2659 | |
| 2660 | void _drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local) |
| 2661 | { |
| 2662 | if (idx == UI_CURRENT) { |
| 2663 | if (mdev->state.role == R_PRIMARY) |
| 2664 | val |= 1; |
| 2665 | else |
| 2666 | val &= ~((u64)1); |
| 2667 | |
| 2668 | drbd_set_ed_uuid(mdev, val); |
| 2669 | } |
| 2670 | |
| 2671 | mdev->ldev->md.uuid[idx] = val; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2672 | drbd_md_mark_dirty(mdev); |
| 2673 | } |
| 2674 | |
| 2675 | |
| 2676 | void drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local) |
| 2677 | { |
| 2678 | if (mdev->ldev->md.uuid[idx]) { |
| 2679 | drbd_uuid_move_history(mdev); |
| 2680 | mdev->ldev->md.uuid[UI_HISTORY_START] = mdev->ldev->md.uuid[idx]; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2681 | } |
| 2682 | _drbd_uuid_set(mdev, idx, val); |
| 2683 | } |
| 2684 | |
| 2685 | /** |
| 2686 | * drbd_uuid_new_current() - Creates a new current UUID |
| 2687 | * @mdev: DRBD device. |
| 2688 | * |
| 2689 | * Creates a new current UUID, and rotates the old current UUID into |
| 2690 | * the bitmap slot. Causes an incremental resync upon next connect. |
| 2691 | */ |
| 2692 | void drbd_uuid_new_current(struct drbd_conf *mdev) __must_hold(local) |
| 2693 | { |
| 2694 | u64 val; |
Lars Ellenberg | 62b0da3 | 2011-01-20 13:25:21 +0100 | [diff] [blame] | 2695 | unsigned long long bm_uuid = mdev->ldev->md.uuid[UI_BITMAP]; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2696 | |
Lars Ellenberg | 62b0da3 | 2011-01-20 13:25:21 +0100 | [diff] [blame] | 2697 | if (bm_uuid) |
| 2698 | dev_warn(DEV, "bm UUID was already set: %llX\n", bm_uuid); |
| 2699 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2700 | mdev->ldev->md.uuid[UI_BITMAP] = mdev->ldev->md.uuid[UI_CURRENT]; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2701 | |
| 2702 | get_random_bytes(&val, sizeof(u64)); |
| 2703 | _drbd_uuid_set(mdev, UI_CURRENT, val); |
Lars Ellenberg | 62b0da3 | 2011-01-20 13:25:21 +0100 | [diff] [blame] | 2704 | drbd_print_uuids(mdev, "new current UUID"); |
Lars Ellenberg | aaa8e2b | 2010-10-15 13:16:53 +0200 | [diff] [blame] | 2705 | /* get it to stable storage _now_ */ |
| 2706 | drbd_md_sync(mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2707 | } |
| 2708 | |
| 2709 | void drbd_uuid_set_bm(struct drbd_conf *mdev, u64 val) __must_hold(local) |
| 2710 | { |
| 2711 | if (mdev->ldev->md.uuid[UI_BITMAP] == 0 && val == 0) |
| 2712 | return; |
| 2713 | |
| 2714 | if (val == 0) { |
| 2715 | drbd_uuid_move_history(mdev); |
| 2716 | mdev->ldev->md.uuid[UI_HISTORY_START] = mdev->ldev->md.uuid[UI_BITMAP]; |
| 2717 | mdev->ldev->md.uuid[UI_BITMAP] = 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2718 | } else { |
Lars Ellenberg | 62b0da3 | 2011-01-20 13:25:21 +0100 | [diff] [blame] | 2719 | unsigned long long bm_uuid = mdev->ldev->md.uuid[UI_BITMAP]; |
| 2720 | if (bm_uuid) |
| 2721 | dev_warn(DEV, "bm UUID was already set: %llX\n", bm_uuid); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2722 | |
Lars Ellenberg | 62b0da3 | 2011-01-20 13:25:21 +0100 | [diff] [blame] | 2723 | mdev->ldev->md.uuid[UI_BITMAP] = val & ~((u64)1); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2724 | } |
| 2725 | drbd_md_mark_dirty(mdev); |
| 2726 | } |
| 2727 | |
| 2728 | /** |
| 2729 | * drbd_bmio_set_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io() |
| 2730 | * @mdev: DRBD device. |
| 2731 | * |
| 2732 | * Sets all bits in the bitmap and writes the whole bitmap to stable storage. |
| 2733 | */ |
| 2734 | int drbd_bmio_set_n_write(struct drbd_conf *mdev) |
| 2735 | { |
| 2736 | int rv = -EIO; |
| 2737 | |
| 2738 | if (get_ldev_if_state(mdev, D_ATTACHING)) { |
| 2739 | drbd_md_set_flag(mdev, MDF_FULL_SYNC); |
| 2740 | drbd_md_sync(mdev); |
| 2741 | drbd_bm_set_all(mdev); |
| 2742 | |
| 2743 | rv = drbd_bm_write(mdev); |
| 2744 | |
| 2745 | if (!rv) { |
| 2746 | drbd_md_clear_flag(mdev, MDF_FULL_SYNC); |
| 2747 | drbd_md_sync(mdev); |
| 2748 | } |
| 2749 | |
| 2750 | put_ldev(mdev); |
| 2751 | } |
| 2752 | |
| 2753 | return rv; |
| 2754 | } |
| 2755 | |
| 2756 | /** |
| 2757 | * drbd_bmio_clear_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io() |
| 2758 | * @mdev: DRBD device. |
| 2759 | * |
| 2760 | * Clears all bits in the bitmap and writes the whole bitmap to stable storage. |
| 2761 | */ |
| 2762 | int drbd_bmio_clear_n_write(struct drbd_conf *mdev) |
| 2763 | { |
| 2764 | int rv = -EIO; |
| 2765 | |
Philipp Reisner | 0778286 | 2010-08-31 12:00:50 +0200 | [diff] [blame] | 2766 | drbd_resume_al(mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2767 | if (get_ldev_if_state(mdev, D_ATTACHING)) { |
| 2768 | drbd_bm_clear_all(mdev); |
| 2769 | rv = drbd_bm_write(mdev); |
| 2770 | put_ldev(mdev); |
| 2771 | } |
| 2772 | |
| 2773 | return rv; |
| 2774 | } |
| 2775 | |
| 2776 | static int w_bitmap_io(struct drbd_conf *mdev, struct drbd_work *w, int unused) |
| 2777 | { |
| 2778 | struct bm_io_work *work = container_of(w, struct bm_io_work, w); |
Lars Ellenberg | 02851e9 | 2010-12-16 14:47:39 +0100 | [diff] [blame] | 2779 | int rv = -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2780 | |
| 2781 | D_ASSERT(atomic_read(&mdev->ap_bio_cnt) == 0); |
| 2782 | |
Lars Ellenberg | 02851e9 | 2010-12-16 14:47:39 +0100 | [diff] [blame] | 2783 | if (get_ldev(mdev)) { |
Lars Ellenberg | 20ceb2b | 2011-01-21 10:56:44 +0100 | [diff] [blame] | 2784 | drbd_bm_lock(mdev, work->why, work->flags); |
Lars Ellenberg | 02851e9 | 2010-12-16 14:47:39 +0100 | [diff] [blame] | 2785 | rv = work->io_fn(mdev); |
| 2786 | drbd_bm_unlock(mdev); |
| 2787 | put_ldev(mdev); |
| 2788 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2789 | |
| 2790 | clear_bit(BITMAP_IO, &mdev->flags); |
Philipp Reisner | 127b317 | 2010-11-16 10:07:53 +0100 | [diff] [blame] | 2791 | smp_mb__after_clear_bit(); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2792 | wake_up(&mdev->misc_wait); |
| 2793 | |
| 2794 | if (work->done) |
| 2795 | work->done(mdev, rv); |
| 2796 | |
| 2797 | clear_bit(BITMAP_IO_QUEUED, &mdev->flags); |
| 2798 | work->why = NULL; |
Lars Ellenberg | 20ceb2b | 2011-01-21 10:56:44 +0100 | [diff] [blame] | 2799 | work->flags = 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2800 | |
| 2801 | return 1; |
| 2802 | } |
| 2803 | |
Lars Ellenberg | 82f59cc | 2010-10-16 12:13:47 +0200 | [diff] [blame] | 2804 | void drbd_ldev_destroy(struct drbd_conf *mdev) |
| 2805 | { |
| 2806 | lc_destroy(mdev->resync); |
| 2807 | mdev->resync = NULL; |
| 2808 | lc_destroy(mdev->act_log); |
| 2809 | mdev->act_log = NULL; |
| 2810 | __no_warn(local, |
| 2811 | drbd_free_bc(mdev->ldev); |
| 2812 | mdev->ldev = NULL;); |
| 2813 | |
| 2814 | if (mdev->md_io_tmpp) { |
| 2815 | __free_page(mdev->md_io_tmpp); |
| 2816 | mdev->md_io_tmpp = NULL; |
| 2817 | } |
| 2818 | clear_bit(GO_DISKLESS, &mdev->flags); |
| 2819 | } |
| 2820 | |
Lars Ellenberg | e9e6f3e | 2010-09-14 20:26:27 +0200 | [diff] [blame] | 2821 | static int w_go_diskless(struct drbd_conf *mdev, struct drbd_work *w, int unused) |
| 2822 | { |
| 2823 | D_ASSERT(mdev->state.disk == D_FAILED); |
Lars Ellenberg | 9d28287 | 2010-10-14 13:57:07 +0200 | [diff] [blame] | 2824 | /* we cannot assert local_cnt == 0 here, as get_ldev_if_state will |
| 2825 | * inc/dec it frequently. Once we are D_DISKLESS, no one will touch |
Lars Ellenberg | 82f59cc | 2010-10-16 12:13:47 +0200 | [diff] [blame] | 2826 | * the protected members anymore, though, so once put_ldev reaches zero |
| 2827 | * again, it will be safe to free them. */ |
Lars Ellenberg | e9e6f3e | 2010-09-14 20:26:27 +0200 | [diff] [blame] | 2828 | drbd_force_state(mdev, NS(disk, D_DISKLESS)); |
Lars Ellenberg | e9e6f3e | 2010-09-14 20:26:27 +0200 | [diff] [blame] | 2829 | return 1; |
| 2830 | } |
| 2831 | |
| 2832 | void drbd_go_diskless(struct drbd_conf *mdev) |
| 2833 | { |
| 2834 | D_ASSERT(mdev->state.disk == D_FAILED); |
| 2835 | if (!test_and_set_bit(GO_DISKLESS, &mdev->flags)) |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 2836 | drbd_queue_work(&mdev->tconn->data.work, &mdev->go_diskless); |
Lars Ellenberg | e9e6f3e | 2010-09-14 20:26:27 +0200 | [diff] [blame] | 2837 | } |
| 2838 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2839 | /** |
| 2840 | * drbd_queue_bitmap_io() - Queues an IO operation on the whole bitmap |
| 2841 | * @mdev: DRBD device. |
| 2842 | * @io_fn: IO callback to be called when bitmap IO is possible |
| 2843 | * @done: callback to be called after the bitmap IO was performed |
| 2844 | * @why: Descriptive text of the reason for doing the IO |
| 2845 | * |
| 2846 | * While IO on the bitmap happens we freeze application IO thus we ensure |
| 2847 | * that drbd_set_out_of_sync() can not be called. This function MAY ONLY be |
| 2848 | * called from worker context. It MUST NOT be used while a previous such |
| 2849 | * work is still pending! |
| 2850 | */ |
| 2851 | void drbd_queue_bitmap_io(struct drbd_conf *mdev, |
| 2852 | int (*io_fn)(struct drbd_conf *), |
| 2853 | void (*done)(struct drbd_conf *, int), |
Lars Ellenberg | 20ceb2b | 2011-01-21 10:56:44 +0100 | [diff] [blame] | 2854 | char *why, enum bm_flag flags) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2855 | { |
Philipp Reisner | e6b3ea8 | 2011-01-19 14:02:01 +0100 | [diff] [blame] | 2856 | D_ASSERT(current == mdev->tconn->worker.task); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2857 | |
| 2858 | D_ASSERT(!test_bit(BITMAP_IO_QUEUED, &mdev->flags)); |
| 2859 | D_ASSERT(!test_bit(BITMAP_IO, &mdev->flags)); |
| 2860 | D_ASSERT(list_empty(&mdev->bm_io_work.w.list)); |
| 2861 | if (mdev->bm_io_work.why) |
| 2862 | dev_err(DEV, "FIXME going to queue '%s' but '%s' still pending?\n", |
| 2863 | why, mdev->bm_io_work.why); |
| 2864 | |
| 2865 | mdev->bm_io_work.io_fn = io_fn; |
| 2866 | mdev->bm_io_work.done = done; |
| 2867 | mdev->bm_io_work.why = why; |
Lars Ellenberg | 20ceb2b | 2011-01-21 10:56:44 +0100 | [diff] [blame] | 2868 | mdev->bm_io_work.flags = flags; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2869 | |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 2870 | spin_lock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2871 | set_bit(BITMAP_IO, &mdev->flags); |
| 2872 | if (atomic_read(&mdev->ap_bio_cnt) == 0) { |
Philipp Reisner | 127b317 | 2010-11-16 10:07:53 +0100 | [diff] [blame] | 2873 | if (!test_and_set_bit(BITMAP_IO_QUEUED, &mdev->flags)) |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 2874 | drbd_queue_work(&mdev->tconn->data.work, &mdev->bm_io_work.w); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2875 | } |
Philipp Reisner | 87eeee4 | 2011-01-19 14:16:30 +0100 | [diff] [blame] | 2876 | spin_unlock_irq(&mdev->tconn->req_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2877 | } |
| 2878 | |
| 2879 | /** |
| 2880 | * drbd_bitmap_io() - Does an IO operation on the whole bitmap |
| 2881 | * @mdev: DRBD device. |
| 2882 | * @io_fn: IO callback to be called when bitmap IO is possible |
| 2883 | * @why: Descriptive text of the reason for doing the IO |
| 2884 | * |
| 2885 | * freezes application IO while that the actual IO operations runs. This |
| 2886 | * functions MAY NOT be called from worker context. |
| 2887 | */ |
Lars Ellenberg | 20ceb2b | 2011-01-21 10:56:44 +0100 | [diff] [blame] | 2888 | int drbd_bitmap_io(struct drbd_conf *mdev, int (*io_fn)(struct drbd_conf *), |
| 2889 | char *why, enum bm_flag flags) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2890 | { |
| 2891 | int rv; |
| 2892 | |
Philipp Reisner | e6b3ea8 | 2011-01-19 14:02:01 +0100 | [diff] [blame] | 2893 | D_ASSERT(current != mdev->tconn->worker.task); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2894 | |
Lars Ellenberg | 20ceb2b | 2011-01-21 10:56:44 +0100 | [diff] [blame] | 2895 | if ((flags & BM_LOCKED_SET_ALLOWED) == 0) |
| 2896 | drbd_suspend_io(mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2897 | |
Lars Ellenberg | 20ceb2b | 2011-01-21 10:56:44 +0100 | [diff] [blame] | 2898 | drbd_bm_lock(mdev, why, flags); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2899 | rv = io_fn(mdev); |
| 2900 | drbd_bm_unlock(mdev); |
| 2901 | |
Lars Ellenberg | 20ceb2b | 2011-01-21 10:56:44 +0100 | [diff] [blame] | 2902 | if ((flags & BM_LOCKED_SET_ALLOWED) == 0) |
| 2903 | drbd_resume_io(mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2904 | |
| 2905 | return rv; |
| 2906 | } |
| 2907 | |
| 2908 | void drbd_md_set_flag(struct drbd_conf *mdev, int flag) __must_hold(local) |
| 2909 | { |
| 2910 | if ((mdev->ldev->md.flags & flag) != flag) { |
| 2911 | drbd_md_mark_dirty(mdev); |
| 2912 | mdev->ldev->md.flags |= flag; |
| 2913 | } |
| 2914 | } |
| 2915 | |
| 2916 | void drbd_md_clear_flag(struct drbd_conf *mdev, int flag) __must_hold(local) |
| 2917 | { |
| 2918 | if ((mdev->ldev->md.flags & flag) != 0) { |
| 2919 | drbd_md_mark_dirty(mdev); |
| 2920 | mdev->ldev->md.flags &= ~flag; |
| 2921 | } |
| 2922 | } |
| 2923 | int drbd_md_test_flag(struct drbd_backing_dev *bdev, int flag) |
| 2924 | { |
| 2925 | return (bdev->md.flags & flag) != 0; |
| 2926 | } |
| 2927 | |
| 2928 | static void md_sync_timer_fn(unsigned long data) |
| 2929 | { |
| 2930 | struct drbd_conf *mdev = (struct drbd_conf *) data; |
| 2931 | |
Philipp Reisner | e42325a | 2011-01-19 13:55:45 +0100 | [diff] [blame] | 2932 | drbd_queue_work_front(&mdev->tconn->data.work, &mdev->md_sync_work); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2933 | } |
| 2934 | |
| 2935 | static int w_md_sync(struct drbd_conf *mdev, struct drbd_work *w, int unused) |
| 2936 | { |
| 2937 | dev_warn(DEV, "md_sync_timer expired! Worker calls drbd_md_sync().\n"); |
Lars Ellenberg | ee15b03 | 2010-09-03 10:00:09 +0200 | [diff] [blame] | 2938 | #ifdef DEBUG |
| 2939 | dev_warn(DEV, "last md_mark_dirty: %s:%u\n", |
| 2940 | mdev->last_md_mark_dirty.func, mdev->last_md_mark_dirty.line); |
| 2941 | #endif |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2942 | drbd_md_sync(mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 2943 | return 1; |
| 2944 | } |
| 2945 | |
Andreas Gruenbacher | d876302 | 2011-01-26 17:39:41 +0100 | [diff] [blame] | 2946 | const char *cmdname(enum drbd_packet cmd) |
Andreas Gruenbacher | f2ad906 | 2011-01-26 17:13:25 +0100 | [diff] [blame] | 2947 | { |
| 2948 | /* THINK may need to become several global tables |
| 2949 | * when we want to support more than |
| 2950 | * one PRO_VERSION */ |
| 2951 | static const char *cmdnames[] = { |
| 2952 | [P_DATA] = "Data", |
| 2953 | [P_DATA_REPLY] = "DataReply", |
| 2954 | [P_RS_DATA_REPLY] = "RSDataReply", |
| 2955 | [P_BARRIER] = "Barrier", |
| 2956 | [P_BITMAP] = "ReportBitMap", |
| 2957 | [P_BECOME_SYNC_TARGET] = "BecomeSyncTarget", |
| 2958 | [P_BECOME_SYNC_SOURCE] = "BecomeSyncSource", |
| 2959 | [P_UNPLUG_REMOTE] = "UnplugRemote", |
| 2960 | [P_DATA_REQUEST] = "DataRequest", |
| 2961 | [P_RS_DATA_REQUEST] = "RSDataRequest", |
| 2962 | [P_SYNC_PARAM] = "SyncParam", |
| 2963 | [P_SYNC_PARAM89] = "SyncParam89", |
| 2964 | [P_PROTOCOL] = "ReportProtocol", |
| 2965 | [P_UUIDS] = "ReportUUIDs", |
| 2966 | [P_SIZES] = "ReportSizes", |
| 2967 | [P_STATE] = "ReportState", |
| 2968 | [P_SYNC_UUID] = "ReportSyncUUID", |
| 2969 | [P_AUTH_CHALLENGE] = "AuthChallenge", |
| 2970 | [P_AUTH_RESPONSE] = "AuthResponse", |
| 2971 | [P_PING] = "Ping", |
| 2972 | [P_PING_ACK] = "PingAck", |
| 2973 | [P_RECV_ACK] = "RecvAck", |
| 2974 | [P_WRITE_ACK] = "WriteAck", |
| 2975 | [P_RS_WRITE_ACK] = "RSWriteAck", |
| 2976 | [P_DISCARD_ACK] = "DiscardAck", |
| 2977 | [P_NEG_ACK] = "NegAck", |
| 2978 | [P_NEG_DREPLY] = "NegDReply", |
| 2979 | [P_NEG_RS_DREPLY] = "NegRSDReply", |
| 2980 | [P_BARRIER_ACK] = "BarrierAck", |
| 2981 | [P_STATE_CHG_REQ] = "StateChgRequest", |
| 2982 | [P_STATE_CHG_REPLY] = "StateChgReply", |
| 2983 | [P_OV_REQUEST] = "OVRequest", |
| 2984 | [P_OV_REPLY] = "OVReply", |
| 2985 | [P_OV_RESULT] = "OVResult", |
| 2986 | [P_CSUM_RS_REQUEST] = "CsumRSRequest", |
| 2987 | [P_RS_IS_IN_SYNC] = "CsumRSIsInSync", |
| 2988 | [P_COMPRESSED_BITMAP] = "CBitmap", |
| 2989 | [P_DELAY_PROBE] = "DelayProbe", |
| 2990 | [P_OUT_OF_SYNC] = "OutOfSync", |
| 2991 | [P_MAX_CMD] = NULL, |
| 2992 | }; |
| 2993 | |
| 2994 | if (cmd == P_HAND_SHAKE_M) |
| 2995 | return "HandShakeM"; |
| 2996 | if (cmd == P_HAND_SHAKE_S) |
| 2997 | return "HandShakeS"; |
| 2998 | if (cmd == P_HAND_SHAKE) |
| 2999 | return "HandShake"; |
| 3000 | if (cmd >= P_MAX_CMD) |
| 3001 | return "Unknown"; |
| 3002 | return cmdnames[cmd]; |
| 3003 | } |
| 3004 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3005 | #ifdef CONFIG_DRBD_FAULT_INJECTION |
| 3006 | /* Fault insertion support including random number generator shamelessly |
| 3007 | * stolen from kernel/rcutorture.c */ |
| 3008 | struct fault_random_state { |
| 3009 | unsigned long state; |
| 3010 | unsigned long count; |
| 3011 | }; |
| 3012 | |
| 3013 | #define FAULT_RANDOM_MULT 39916801 /* prime */ |
| 3014 | #define FAULT_RANDOM_ADD 479001701 /* prime */ |
| 3015 | #define FAULT_RANDOM_REFRESH 10000 |
| 3016 | |
| 3017 | /* |
| 3018 | * Crude but fast random-number generator. Uses a linear congruential |
| 3019 | * generator, with occasional help from get_random_bytes(). |
| 3020 | */ |
| 3021 | static unsigned long |
| 3022 | _drbd_fault_random(struct fault_random_state *rsp) |
| 3023 | { |
| 3024 | long refresh; |
| 3025 | |
Roel Kluin | 49829ea | 2009-12-15 22:55:44 +0100 | [diff] [blame] | 3026 | if (!rsp->count--) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3027 | get_random_bytes(&refresh, sizeof(refresh)); |
| 3028 | rsp->state += refresh; |
| 3029 | rsp->count = FAULT_RANDOM_REFRESH; |
| 3030 | } |
| 3031 | rsp->state = rsp->state * FAULT_RANDOM_MULT + FAULT_RANDOM_ADD; |
| 3032 | return swahw32(rsp->state); |
| 3033 | } |
| 3034 | |
| 3035 | static char * |
| 3036 | _drbd_fault_str(unsigned int type) { |
| 3037 | static char *_faults[] = { |
| 3038 | [DRBD_FAULT_MD_WR] = "Meta-data write", |
| 3039 | [DRBD_FAULT_MD_RD] = "Meta-data read", |
| 3040 | [DRBD_FAULT_RS_WR] = "Resync write", |
| 3041 | [DRBD_FAULT_RS_RD] = "Resync read", |
| 3042 | [DRBD_FAULT_DT_WR] = "Data write", |
| 3043 | [DRBD_FAULT_DT_RD] = "Data read", |
| 3044 | [DRBD_FAULT_DT_RA] = "Data read ahead", |
| 3045 | [DRBD_FAULT_BM_ALLOC] = "BM allocation", |
Philipp Reisner | 6b4388a | 2010-04-26 14:11:45 +0200 | [diff] [blame] | 3046 | [DRBD_FAULT_AL_EE] = "EE allocation", |
| 3047 | [DRBD_FAULT_RECEIVE] = "receive data corruption", |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3048 | }; |
| 3049 | |
| 3050 | return (type < DRBD_FAULT_MAX) ? _faults[type] : "**Unknown**"; |
| 3051 | } |
| 3052 | |
| 3053 | unsigned int |
| 3054 | _drbd_insert_fault(struct drbd_conf *mdev, unsigned int type) |
| 3055 | { |
| 3056 | static struct fault_random_state rrs = {0, 0}; |
| 3057 | |
| 3058 | unsigned int ret = ( |
| 3059 | (fault_devs == 0 || |
| 3060 | ((1 << mdev_to_minor(mdev)) & fault_devs) != 0) && |
| 3061 | (((_drbd_fault_random(&rrs) % 100) + 1) <= fault_rate)); |
| 3062 | |
| 3063 | if (ret) { |
| 3064 | fault_count++; |
| 3065 | |
Lars Ellenberg | 7383506 | 2010-05-27 11:51:56 +0200 | [diff] [blame] | 3066 | if (__ratelimit(&drbd_ratelimit_state)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3067 | dev_warn(DEV, "***Simulating %s failure\n", |
| 3068 | _drbd_fault_str(type)); |
| 3069 | } |
| 3070 | |
| 3071 | return ret; |
| 3072 | } |
| 3073 | #endif |
| 3074 | |
| 3075 | const char *drbd_buildtag(void) |
| 3076 | { |
| 3077 | /* DRBD built from external sources has here a reference to the |
| 3078 | git hash of the source code. */ |
| 3079 | |
| 3080 | static char buildtag[38] = "\0uilt-in"; |
| 3081 | |
| 3082 | if (buildtag[0] == 0) { |
| 3083 | #ifdef CONFIG_MODULES |
| 3084 | if (THIS_MODULE != NULL) |
| 3085 | sprintf(buildtag, "srcversion: %-24s", THIS_MODULE->srcversion); |
| 3086 | else |
| 3087 | #endif |
| 3088 | buildtag[0] = 'b'; |
| 3089 | } |
| 3090 | |
| 3091 | return buildtag; |
| 3092 | } |
| 3093 | |
| 3094 | module_init(drbd_init) |
| 3095 | module_exit(drbd_cleanup) |
| 3096 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 3097 | EXPORT_SYMBOL(drbd_conn_str); |
| 3098 | EXPORT_SYMBOL(drbd_role_str); |
| 3099 | EXPORT_SYMBOL(drbd_disk_str); |
| 3100 | EXPORT_SYMBOL(drbd_set_st_err_str); |