Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | drbd_actlog.c |
| 3 | |
| 4 | This file is part of DRBD by Philipp Reisner and Lars Ellenberg. |
| 5 | |
| 6 | Copyright (C) 2003-2008, LINBIT Information Technologies GmbH. |
| 7 | Copyright (C) 2003-2008, Philipp Reisner <philipp.reisner@linbit.com>. |
| 8 | Copyright (C) 2003-2008, Lars Ellenberg <lars.ellenberg@linbit.com>. |
| 9 | |
| 10 | drbd is free software; you can redistribute it and/or modify |
| 11 | it under the terms of the GNU General Public License as published by |
| 12 | the Free Software Foundation; either version 2, or (at your option) |
| 13 | any later version. |
| 14 | |
| 15 | drbd is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with drbd; see the file COPYING. If not, write to |
| 22 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
| 23 | |
| 24 | */ |
| 25 | |
| 26 | #include <linux/slab.h> |
Lars Ellenberg | 7ad651b | 2011-02-21 13:21:03 +0100 | [diff] [blame] | 27 | #include <linux/crc32c.h> |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 28 | #include <linux/drbd.h> |
Lars Ellenberg | 7ad651b | 2011-02-21 13:21:03 +0100 | [diff] [blame] | 29 | #include <linux/drbd_limits.h> |
| 30 | #include <linux/dynamic_debug.h> |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 31 | #include "drbd_int.h" |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 32 | #include "drbd_wrappers.h" |
| 33 | |
Lars Ellenberg | 85f103d | 2011-03-31 12:06:48 +0200 | [diff] [blame] | 34 | |
| 35 | enum al_transaction_types { |
| 36 | AL_TR_UPDATE = 0, |
| 37 | AL_TR_INITIALIZED = 0xffff |
| 38 | }; |
Lars Ellenberg | 7ad651b | 2011-02-21 13:21:03 +0100 | [diff] [blame] | 39 | /* all fields on disc in big endian */ |
| 40 | struct __packed al_transaction_on_disk { |
| 41 | /* don't we all like magic */ |
| 42 | __be32 magic; |
| 43 | |
| 44 | /* to identify the most recent transaction block |
| 45 | * in the on disk ring buffer */ |
| 46 | __be32 tr_number; |
| 47 | |
| 48 | /* checksum on the full 4k block, with this field set to 0. */ |
| 49 | __be32 crc32c; |
| 50 | |
| 51 | /* type of transaction, special transaction types like: |
Lars Ellenberg | 85f103d | 2011-03-31 12:06:48 +0200 | [diff] [blame] | 52 | * purge-all, set-all-idle, set-all-active, ... to-be-defined |
| 53 | * see also enum al_transaction_types */ |
Lars Ellenberg | 7ad651b | 2011-02-21 13:21:03 +0100 | [diff] [blame] | 54 | __be16 transaction_type; |
| 55 | |
| 56 | /* we currently allow only a few thousand extents, |
| 57 | * so 16bit will be enough for the slot number. */ |
| 58 | |
| 59 | /* how many updates in this transaction */ |
| 60 | __be16 n_updates; |
| 61 | |
| 62 | /* maximum slot number, "al-extents" in drbd.conf speak. |
| 63 | * Having this in each transaction should make reconfiguration |
| 64 | * of that parameter easier. */ |
| 65 | __be16 context_size; |
| 66 | |
| 67 | /* slot number the context starts with */ |
| 68 | __be16 context_start_slot_nr; |
| 69 | |
| 70 | /* Some reserved bytes. Expected usage is a 64bit counter of |
| 71 | * sectors-written since device creation, and other data generation tag |
| 72 | * supporting usage */ |
| 73 | __be32 __reserved[4]; |
| 74 | |
| 75 | /* --- 36 byte used --- */ |
| 76 | |
| 77 | /* Reserve space for up to AL_UPDATES_PER_TRANSACTION changes |
| 78 | * in one transaction, then use the remaining byte in the 4k block for |
| 79 | * context information. "Flexible" number of updates per transaction |
| 80 | * does not help, as we have to account for the case when all update |
| 81 | * slots are used anyways, so it would only complicate code without |
| 82 | * additional benefit. |
| 83 | */ |
| 84 | __be16 update_slot_nr[AL_UPDATES_PER_TRANSACTION]; |
| 85 | |
| 86 | /* but the extent number is 32bit, which at an extent size of 4 MiB |
| 87 | * allows to cover device sizes of up to 2**54 Byte (16 PiB) */ |
| 88 | __be32 update_extent_nr[AL_UPDATES_PER_TRANSACTION]; |
| 89 | |
| 90 | /* --- 420 bytes used (36 + 64*6) --- */ |
| 91 | |
| 92 | /* 4096 - 420 = 3676 = 919 * 4 */ |
| 93 | __be32 context[AL_CONTEXT_PER_TRANSACTION]; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | struct update_odbm_work { |
| 97 | struct drbd_work w; |
| 98 | unsigned int enr; |
| 99 | }; |
| 100 | |
| 101 | struct update_al_work { |
| 102 | struct drbd_work w; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 103 | struct completion event; |
Lars Ellenberg | 7ad651b | 2011-02-21 13:21:03 +0100 | [diff] [blame] | 104 | int err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 107 | |
Philipp Reisner | cdfda63 | 2011-07-05 15:38:59 +0200 | [diff] [blame] | 108 | void *drbd_md_get_buffer(struct drbd_conf *mdev) |
| 109 | { |
| 110 | int r; |
| 111 | |
| 112 | wait_event(mdev->misc_wait, |
| 113 | (r = atomic_cmpxchg(&mdev->md_io_in_use, 0, 1)) == 0 || |
| 114 | mdev->state.disk <= D_FAILED); |
| 115 | |
| 116 | return r ? NULL : page_address(mdev->md_io_page); |
| 117 | } |
| 118 | |
| 119 | void drbd_md_put_buffer(struct drbd_conf *mdev) |
| 120 | { |
| 121 | if (atomic_dec_and_test(&mdev->md_io_in_use)) |
| 122 | wake_up(&mdev->misc_wait); |
| 123 | } |
| 124 | |
Lars Ellenberg | e34b677 | 2012-09-27 15:07:11 +0200 | [diff] [blame] | 125 | void wait_until_done_or_force_detached(struct drbd_conf *mdev, struct drbd_backing_dev *bdev, |
Philipp Reisner | 32db80f | 2012-02-22 11:51:57 +0100 | [diff] [blame] | 126 | unsigned int *done) |
Philipp Reisner | cdfda63 | 2011-07-05 15:38:59 +0200 | [diff] [blame] | 127 | { |
Philipp Reisner | 32db80f | 2012-02-22 11:51:57 +0100 | [diff] [blame] | 128 | long dt; |
| 129 | |
| 130 | rcu_read_lock(); |
| 131 | dt = rcu_dereference(bdev->disk_conf)->disk_timeout; |
| 132 | rcu_read_unlock(); |
| 133 | dt = dt * HZ / 10; |
| 134 | if (dt == 0) |
| 135 | dt = MAX_SCHEDULE_TIMEOUT; |
| 136 | |
Lars Ellenberg | e34b677 | 2012-09-27 15:07:11 +0200 | [diff] [blame] | 137 | dt = wait_event_timeout(mdev->misc_wait, |
| 138 | *done || test_bit(FORCE_DETACH, &mdev->flags), dt); |
| 139 | if (dt == 0) { |
Philipp Reisner | 32db80f | 2012-02-22 11:51:57 +0100 | [diff] [blame] | 140 | dev_err(DEV, "meta-data IO operation timed out\n"); |
Lars Ellenberg | e34b677 | 2012-09-27 15:07:11 +0200 | [diff] [blame] | 141 | drbd_chk_io_error(mdev, 1, DRBD_FORCE_DETACH); |
| 142 | } |
Philipp Reisner | cdfda63 | 2011-07-05 15:38:59 +0200 | [diff] [blame] | 143 | } |
| 144 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 145 | static int _drbd_md_sync_page_io(struct drbd_conf *mdev, |
| 146 | struct drbd_backing_dev *bdev, |
| 147 | struct page *page, sector_t sector, |
| 148 | int rw, int size) |
| 149 | { |
| 150 | struct bio *bio; |
Andreas Gruenbacher | ac29f40 | 2010-12-13 02:20:47 +0100 | [diff] [blame] | 151 | int err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 152 | |
Philipp Reisner | cdfda63 | 2011-07-05 15:38:59 +0200 | [diff] [blame] | 153 | mdev->md_io.done = 0; |
| 154 | mdev->md_io.error = -ENODEV; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 155 | |
Philipp Reisner | a8a4e51 | 2010-08-25 10:21:04 +0200 | [diff] [blame] | 156 | if ((rw & WRITE) && !test_bit(MD_NO_FUA, &mdev->flags)) |
Lars Ellenberg | 86e1e98 | 2011-06-28 13:22:48 +0200 | [diff] [blame] | 157 | rw |= REQ_FUA | REQ_FLUSH; |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 158 | rw |= REQ_SYNC; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 159 | |
Lars Ellenberg | da4a75d | 2011-02-23 17:02:01 +0100 | [diff] [blame] | 160 | bio = bio_alloc_drbd(GFP_NOIO); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 161 | bio->bi_bdev = bdev->md_bdev; |
| 162 | bio->bi_sector = sector; |
Andreas Gruenbacher | ac29f40 | 2010-12-13 02:20:47 +0100 | [diff] [blame] | 163 | err = -EIO; |
| 164 | if (bio_add_page(bio, page, size, 0) != size) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 165 | goto out; |
Philipp Reisner | cdfda63 | 2011-07-05 15:38:59 +0200 | [diff] [blame] | 166 | bio->bi_private = &mdev->md_io; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 167 | bio->bi_end_io = drbd_md_io_complete; |
| 168 | bio->bi_rw = rw; |
| 169 | |
Lars Ellenberg | c04ccaa | 2013-03-19 18:16:47 +0100 | [diff] [blame] | 170 | if (!(rw & WRITE) && mdev->state.disk == D_DISKLESS && mdev->ldev == NULL) |
| 171 | /* special case, drbd_md_read() during drbd_adm_attach(): no get_ldev */ |
| 172 | ; |
| 173 | else if (!get_ldev_if_state(mdev, D_ATTACHING)) { |
| 174 | /* Corresponding put_ldev in drbd_md_io_complete() */ |
Philipp Reisner | cdfda63 | 2011-07-05 15:38:59 +0200 | [diff] [blame] | 175 | dev_err(DEV, "ASSERT FAILED: get_ldev_if_state() == 1 in _drbd_md_sync_page_io()\n"); |
| 176 | err = -ENODEV; |
| 177 | goto out; |
| 178 | } |
| 179 | |
| 180 | bio_get(bio); /* one bio_put() is in the completion handler */ |
| 181 | atomic_inc(&mdev->md_io_in_use); /* drbd_md_put_buffer() is in the completion handler */ |
Andreas Gruenbacher | 0cf9d27 | 2010-12-07 10:43:29 +0100 | [diff] [blame] | 182 | if (drbd_insert_fault(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 183 | bio_endio(bio, -EIO); |
| 184 | else |
| 185 | submit_bio(rw, bio); |
Lars Ellenberg | e34b677 | 2012-09-27 15:07:11 +0200 | [diff] [blame] | 186 | wait_until_done_or_force_detached(mdev, bdev, &mdev->md_io.done); |
Andreas Gruenbacher | ac29f40 | 2010-12-13 02:20:47 +0100 | [diff] [blame] | 187 | if (bio_flagged(bio, BIO_UPTODATE)) |
Philipp Reisner | cdfda63 | 2011-07-05 15:38:59 +0200 | [diff] [blame] | 188 | err = mdev->md_io.error; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 189 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 190 | out: |
| 191 | bio_put(bio); |
Andreas Gruenbacher | ac29f40 | 2010-12-13 02:20:47 +0100 | [diff] [blame] | 192 | return err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | int drbd_md_sync_page_io(struct drbd_conf *mdev, struct drbd_backing_dev *bdev, |
| 196 | sector_t sector, int rw) |
| 197 | { |
Andreas Gruenbacher | 3fbf4d2 | 2010-12-13 02:25:41 +0100 | [diff] [blame] | 198 | int err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 199 | struct page *iop = mdev->md_io_page; |
| 200 | |
Philipp Reisner | cdfda63 | 2011-07-05 15:38:59 +0200 | [diff] [blame] | 201 | D_ASSERT(atomic_read(&mdev->md_io_in_use) == 1); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 202 | |
| 203 | BUG_ON(!bdev->md_bdev); |
| 204 | |
Lars Ellenberg | c04ccaa | 2013-03-19 18:16:47 +0100 | [diff] [blame] | 205 | dev_dbg(DEV, "meta_data io: %s [%d]:%s(,%llus,%s) %pS\n", |
Lars Ellenberg | 7ad651b | 2011-02-21 13:21:03 +0100 | [diff] [blame] | 206 | current->comm, current->pid, __func__, |
Lars Ellenberg | c04ccaa | 2013-03-19 18:16:47 +0100 | [diff] [blame] | 207 | (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ", |
| 208 | (void*)_RET_IP_ ); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 209 | |
| 210 | if (sector < drbd_md_first_sector(bdev) || |
Lars Ellenberg | 7ad651b | 2011-02-21 13:21:03 +0100 | [diff] [blame] | 211 | sector + 7 > drbd_md_last_sector(bdev)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 212 | dev_alert(DEV, "%s [%d]:%s(,%llus,%s) out of range md access!\n", |
| 213 | current->comm, current->pid, __func__, |
| 214 | (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ"); |
| 215 | |
Lars Ellenberg | ae8bf31 | 2013-03-19 18:16:43 +0100 | [diff] [blame] | 216 | /* we do all our meta data IO in aligned 4k blocks. */ |
| 217 | err = _drbd_md_sync_page_io(mdev, bdev, iop, sector, rw, 4096); |
Andreas Gruenbacher | 3fbf4d2 | 2010-12-13 02:25:41 +0100 | [diff] [blame] | 218 | if (err) { |
Andreas Gruenbacher | 935be26 | 2011-08-19 13:47:31 +0200 | [diff] [blame] | 219 | dev_err(DEV, "drbd_md_sync_page_io(,%llus,%s) failed with error %d\n", |
| 220 | (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ", err); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 221 | } |
Andreas Gruenbacher | 3fbf4d2 | 2010-12-13 02:25:41 +0100 | [diff] [blame] | 222 | return err; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Lars Ellenberg | 6c3c4355 | 2013-03-19 18:16:53 +0100 | [diff] [blame^] | 225 | static struct bm_extent *find_active_resync_extent(struct drbd_conf *mdev, unsigned int enr) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 226 | { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 227 | struct lc_element *tmp; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 228 | tmp = lc_find(mdev->resync, enr/AL_EXT_PER_BM_SECT); |
| 229 | if (unlikely(tmp != NULL)) { |
| 230 | struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce); |
Lars Ellenberg | 6c3c4355 | 2013-03-19 18:16:53 +0100 | [diff] [blame^] | 231 | if (test_bit(BME_NO_WRITES, &bm_ext->flags)) |
| 232 | return bm_ext; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 233 | } |
Lars Ellenberg | 6c3c4355 | 2013-03-19 18:16:53 +0100 | [diff] [blame^] | 234 | return NULL; |
| 235 | } |
| 236 | |
| 237 | static struct lc_element *_al_get(struct drbd_conf *mdev, unsigned int enr, bool nonblock) |
| 238 | { |
| 239 | struct lc_element *al_ext; |
| 240 | struct bm_extent *bm_ext; |
| 241 | int wake; |
| 242 | |
| 243 | spin_lock_irq(&mdev->al_lock); |
| 244 | bm_ext = find_active_resync_extent(mdev, enr); |
| 245 | if (bm_ext) { |
| 246 | wake = !test_and_set_bit(BME_PRIORITY, &bm_ext->flags); |
| 247 | spin_unlock_irq(&mdev->al_lock); |
| 248 | if (wake) |
| 249 | wake_up(&mdev->al_wait); |
| 250 | return NULL; |
| 251 | } |
| 252 | if (nonblock) |
| 253 | al_ext = lc_try_get(mdev->act_log, enr); |
| 254 | else |
| 255 | al_ext = lc_get(mdev->act_log, enr); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 256 | spin_unlock_irq(&mdev->al_lock); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 257 | return al_ext; |
| 258 | } |
| 259 | |
Lars Ellenberg | b5bc8e0 | 2013-03-19 18:16:52 +0100 | [diff] [blame] | 260 | bool drbd_al_begin_io_fastpath(struct drbd_conf *mdev, struct drbd_interval *i) |
| 261 | { |
| 262 | /* for bios crossing activity log extent boundaries, |
| 263 | * we may need to activate two extents in one go */ |
| 264 | unsigned first = i->sector >> (AL_EXTENT_SHIFT-9); |
| 265 | unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9); |
Lars Ellenberg | b5bc8e0 | 2013-03-19 18:16:52 +0100 | [diff] [blame] | 266 | |
| 267 | D_ASSERT((unsigned)(last - first) <= 1); |
| 268 | D_ASSERT(atomic_read(&mdev->local_cnt) > 0); |
| 269 | |
| 270 | /* FIXME figure out a fast path for bios crossing AL extent boundaries */ |
| 271 | if (first != last) |
| 272 | return false; |
| 273 | |
Lars Ellenberg | 6c3c4355 | 2013-03-19 18:16:53 +0100 | [diff] [blame^] | 274 | return _al_get(mdev, first, true); |
Lars Ellenberg | b5bc8e0 | 2013-03-19 18:16:52 +0100 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | bool drbd_al_begin_io_prepare(struct drbd_conf *mdev, struct drbd_interval *i) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 278 | { |
Lars Ellenberg | 7726547 | 2011-03-31 16:00:51 +0200 | [diff] [blame] | 279 | /* for bios crossing activity log extent boundaries, |
| 280 | * we may need to activate two extents in one go */ |
Lars Ellenberg | e15766e | 2011-04-01 10:38:30 +0200 | [diff] [blame] | 281 | unsigned first = i->sector >> (AL_EXTENT_SHIFT-9); |
Lars Ellenberg | 81a3537 | 2012-07-30 09:00:54 +0200 | [diff] [blame] | 282 | unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9); |
Lars Ellenberg | e15766e | 2011-04-01 10:38:30 +0200 | [diff] [blame] | 283 | unsigned enr; |
Lars Ellenberg | ebfd5d8 | 2013-03-19 18:16:49 +0100 | [diff] [blame] | 284 | bool need_transaction = false; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 285 | |
Lars Ellenberg | 81a3537 | 2012-07-30 09:00:54 +0200 | [diff] [blame] | 286 | D_ASSERT(first <= last); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 287 | D_ASSERT(atomic_read(&mdev->local_cnt) > 0); |
| 288 | |
Lars Ellenberg | ebfd5d8 | 2013-03-19 18:16:49 +0100 | [diff] [blame] | 289 | for (enr = first; enr <= last; enr++) { |
| 290 | struct lc_element *al_ext; |
Lars Ellenberg | 6c3c4355 | 2013-03-19 18:16:53 +0100 | [diff] [blame^] | 291 | wait_event(mdev->al_wait, |
| 292 | (al_ext = _al_get(mdev, enr, false)) != NULL); |
Lars Ellenberg | ebfd5d8 | 2013-03-19 18:16:49 +0100 | [diff] [blame] | 293 | if (al_ext->lc_number != enr) |
| 294 | need_transaction = true; |
| 295 | } |
Lars Ellenberg | b5bc8e0 | 2013-03-19 18:16:52 +0100 | [diff] [blame] | 296 | return need_transaction; |
| 297 | } |
Lars Ellenberg | ebfd5d8 | 2013-03-19 18:16:49 +0100 | [diff] [blame] | 298 | |
Lars Ellenberg | b5bc8e0 | 2013-03-19 18:16:52 +0100 | [diff] [blame] | 299 | static int al_write_transaction(struct drbd_conf *mdev, bool delegate); |
| 300 | |
| 301 | /* When called through generic_make_request(), we must delegate |
| 302 | * activity log I/O to the worker thread: a further request |
| 303 | * submitted via generic_make_request() within the same task |
| 304 | * would be queued on current->bio_list, and would only start |
| 305 | * after this function returns (see generic_make_request()). |
| 306 | * |
| 307 | * However, if we *are* the worker, we must not delegate to ourselves. |
| 308 | */ |
| 309 | |
| 310 | /* |
| 311 | * @delegate: delegate activity log I/O to the worker thread |
| 312 | */ |
| 313 | void drbd_al_begin_io_commit(struct drbd_conf *mdev, bool delegate) |
| 314 | { |
| 315 | bool locked = false; |
| 316 | |
| 317 | BUG_ON(delegate && current == mdev->tconn->worker.task); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 318 | |
Lars Ellenberg | 7dc1d67 | 2011-05-03 16:49:20 +0200 | [diff] [blame] | 319 | /* Serialize multiple transactions. |
| 320 | * This uses test_and_set_bit, memory barrier is implicit. |
| 321 | */ |
| 322 | wait_event(mdev->al_wait, |
| 323 | mdev->act_log->pending_changes == 0 || |
| 324 | (locked = lc_try_lock_for_transaction(mdev->act_log))); |
| 325 | |
| 326 | if (locked) { |
Lars Ellenberg | 7ad651b | 2011-02-21 13:21:03 +0100 | [diff] [blame] | 327 | /* Double check: it may have been committed by someone else, |
| 328 | * while we have been waiting for the lock. */ |
Lars Ellenberg | e15766e | 2011-04-01 10:38:30 +0200 | [diff] [blame] | 329 | if (mdev->act_log->pending_changes) { |
Philipp Reisner | 9a51ab1 | 2012-02-20 21:53:28 +0100 | [diff] [blame] | 330 | bool write_al_updates; |
| 331 | |
| 332 | rcu_read_lock(); |
| 333 | write_al_updates = rcu_dereference(mdev->ldev->disk_conf)->al_updates; |
| 334 | rcu_read_unlock(); |
| 335 | |
Lars Ellenberg | b5bc8e0 | 2013-03-19 18:16:52 +0100 | [diff] [blame] | 336 | if (write_al_updates) |
Lars Ellenberg | 56392d2 | 2013-03-19 18:16:48 +0100 | [diff] [blame] | 337 | al_write_transaction(mdev, delegate); |
Lars Ellenberg | 7ad651b | 2011-02-21 13:21:03 +0100 | [diff] [blame] | 338 | spin_lock_irq(&mdev->al_lock); |
| 339 | /* FIXME |
Philipp Reisner | 1b7ab15 | 2011-07-15 17:19:02 +0200 | [diff] [blame] | 340 | if (err) |
Lars Ellenberg | 7ad651b | 2011-02-21 13:21:03 +0100 | [diff] [blame] | 341 | we need an "lc_cancel" here; |
| 342 | */ |
| 343 | lc_committed(mdev->act_log); |
| 344 | spin_unlock_irq(&mdev->al_lock); |
| 345 | } |
| 346 | lc_unlock(mdev->act_log); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 347 | wake_up(&mdev->al_wait); |
| 348 | } |
| 349 | } |
| 350 | |
Lars Ellenberg | b5bc8e0 | 2013-03-19 18:16:52 +0100 | [diff] [blame] | 351 | /* |
| 352 | * @delegate: delegate activity log I/O to the worker thread |
| 353 | */ |
| 354 | void drbd_al_begin_io(struct drbd_conf *mdev, struct drbd_interval *i, bool delegate) |
| 355 | { |
| 356 | BUG_ON(delegate && current == mdev->tconn->worker.task); |
| 357 | |
| 358 | if (drbd_al_begin_io_prepare(mdev, i)) |
| 359 | drbd_al_begin_io_commit(mdev, delegate); |
| 360 | } |
| 361 | |
Lars Ellenberg | 181286a | 2011-03-31 15:18:56 +0200 | [diff] [blame] | 362 | void drbd_al_complete_io(struct drbd_conf *mdev, struct drbd_interval *i) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 363 | { |
Lars Ellenberg | e15766e | 2011-04-01 10:38:30 +0200 | [diff] [blame] | 364 | /* for bios crossing activity log extent boundaries, |
| 365 | * we may need to activate two extents in one go */ |
| 366 | unsigned first = i->sector >> (AL_EXTENT_SHIFT-9); |
Lars Ellenberg | 81a3537 | 2012-07-30 09:00:54 +0200 | [diff] [blame] | 367 | unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9); |
Lars Ellenberg | e15766e | 2011-04-01 10:38:30 +0200 | [diff] [blame] | 368 | unsigned enr; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 369 | struct lc_element *extent; |
| 370 | unsigned long flags; |
| 371 | |
Lars Ellenberg | 81a3537 | 2012-07-30 09:00:54 +0200 | [diff] [blame] | 372 | D_ASSERT(first <= last); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 373 | spin_lock_irqsave(&mdev->al_lock, flags); |
| 374 | |
Lars Ellenberg | e15766e | 2011-04-01 10:38:30 +0200 | [diff] [blame] | 375 | for (enr = first; enr <= last; enr++) { |
| 376 | extent = lc_find(mdev->act_log, enr); |
| 377 | if (!extent) { |
| 378 | dev_err(DEV, "al_complete_io() called on inactive extent %u\n", enr); |
| 379 | continue; |
| 380 | } |
Philipp Reisner | 376694a | 2011-11-07 10:54:28 +0100 | [diff] [blame] | 381 | lc_put(mdev->act_log, extent); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 382 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 383 | spin_unlock_irqrestore(&mdev->al_lock, flags); |
Lars Ellenberg | e15766e | 2011-04-01 10:38:30 +0200 | [diff] [blame] | 384 | wake_up(&mdev->al_wait); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Lars Ellenberg | 19f843a | 2010-12-15 08:59:11 +0100 | [diff] [blame] | 387 | #if (PAGE_SHIFT + 3) < (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT) |
| 388 | /* Currently BM_BLOCK_SHIFT, BM_EXT_SHIFT and AL_EXTENT_SHIFT |
| 389 | * are still coupled, or assume too much about their relation. |
| 390 | * Code below will not work if this is violated. |
| 391 | * Will be cleaned up with some followup patch. |
| 392 | */ |
| 393 | # error FIXME |
| 394 | #endif |
| 395 | |
| 396 | static unsigned int al_extent_to_bm_page(unsigned int al_enr) |
| 397 | { |
| 398 | return al_enr >> |
| 399 | /* bit to page */ |
| 400 | ((PAGE_SHIFT + 3) - |
| 401 | /* al extent number to bit */ |
| 402 | (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT)); |
| 403 | } |
| 404 | |
| 405 | static unsigned int rs_extent_to_bm_page(unsigned int rs_enr) |
| 406 | { |
| 407 | return rs_enr >> |
| 408 | /* bit to page */ |
| 409 | ((PAGE_SHIFT + 3) - |
Lars Ellenberg | acb104c3 | 2011-04-28 07:58:24 +0200 | [diff] [blame] | 410 | /* resync extent number to bit */ |
Lars Ellenberg | 19f843a | 2010-12-15 08:59:11 +0100 | [diff] [blame] | 411 | (BM_EXT_SHIFT - BM_BLOCK_SHIFT)); |
| 412 | } |
| 413 | |
Lars Ellenberg | ae8bf31 | 2013-03-19 18:16:43 +0100 | [diff] [blame] | 414 | static sector_t al_tr_number_to_on_disk_sector(struct drbd_conf *mdev) |
| 415 | { |
Lars Ellenberg | 3a4d4eb | 2013-03-19 18:16:44 +0100 | [diff] [blame] | 416 | const unsigned int stripes = mdev->ldev->md.al_stripes; |
| 417 | const unsigned int stripe_size_4kB = mdev->ldev->md.al_stripe_size_4k; |
Lars Ellenberg | ae8bf31 | 2013-03-19 18:16:43 +0100 | [diff] [blame] | 418 | |
| 419 | /* transaction number, modulo on-disk ring buffer wrap around */ |
Lars Ellenberg | 3a4d4eb | 2013-03-19 18:16:44 +0100 | [diff] [blame] | 420 | unsigned int t = mdev->al_tr_number % (mdev->ldev->md.al_size_4k); |
Lars Ellenberg | ae8bf31 | 2013-03-19 18:16:43 +0100 | [diff] [blame] | 421 | |
| 422 | /* ... to aligned 4k on disk block */ |
| 423 | t = ((t % stripes) * stripe_size_4kB) + t/stripes; |
| 424 | |
| 425 | /* ... to 512 byte sector in activity log */ |
| 426 | t *= 8; |
| 427 | |
| 428 | /* ... plus offset to the on disk position */ |
| 429 | return mdev->ldev->md.md_offset + mdev->ldev->md.al_offset + t; |
| 430 | } |
| 431 | |
Andreas Gruenbacher | 99920dc | 2011-03-16 15:31:39 +0100 | [diff] [blame] | 432 | static int |
Philipp Reisner | 1b7ab15 | 2011-07-15 17:19:02 +0200 | [diff] [blame] | 433 | _al_write_transaction(struct drbd_conf *mdev) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 434 | { |
Lars Ellenberg | 7ad651b | 2011-02-21 13:21:03 +0100 | [diff] [blame] | 435 | struct al_transaction_on_disk *buffer; |
| 436 | struct lc_element *e; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 437 | sector_t sector; |
Lars Ellenberg | 7ad651b | 2011-02-21 13:21:03 +0100 | [diff] [blame] | 438 | int i, mx; |
| 439 | unsigned extent_nr; |
| 440 | unsigned crc = 0; |
Philipp Reisner | 1b7ab15 | 2011-07-15 17:19:02 +0200 | [diff] [blame] | 441 | int err = 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 442 | |
| 443 | if (!get_ldev(mdev)) { |
Lars Ellenberg | 7ad651b | 2011-02-21 13:21:03 +0100 | [diff] [blame] | 444 | dev_err(DEV, "disk is %s, cannot start al transaction\n", |
| 445 | drbd_disk_str(mdev->state.disk)); |
Philipp Reisner | 1b7ab15 | 2011-07-15 17:19:02 +0200 | [diff] [blame] | 446 | return -EIO; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 447 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 448 | |
Lars Ellenberg | 6719fb0 | 2010-10-18 23:04:07 +0200 | [diff] [blame] | 449 | /* The bitmap write may have failed, causing a state change. */ |
| 450 | if (mdev->state.disk < D_INCONSISTENT) { |
| 451 | dev_err(DEV, |
Lars Ellenberg | 7ad651b | 2011-02-21 13:21:03 +0100 | [diff] [blame] | 452 | "disk is %s, cannot write al transaction\n", |
| 453 | drbd_disk_str(mdev->state.disk)); |
Lars Ellenberg | 6719fb0 | 2010-10-18 23:04:07 +0200 | [diff] [blame] | 454 | put_ldev(mdev); |
Philipp Reisner | 1b7ab15 | 2011-07-15 17:19:02 +0200 | [diff] [blame] | 455 | return -EIO; |
Lars Ellenberg | 6719fb0 | 2010-10-18 23:04:07 +0200 | [diff] [blame] | 456 | } |
| 457 | |
Philipp Reisner | cdfda63 | 2011-07-05 15:38:59 +0200 | [diff] [blame] | 458 | buffer = drbd_md_get_buffer(mdev); /* protects md_io_buffer, al_tr_cycle, ... */ |
| 459 | if (!buffer) { |
| 460 | dev_err(DEV, "disk failed while waiting for md_io buffer\n"); |
Philipp Reisner | cdfda63 | 2011-07-05 15:38:59 +0200 | [diff] [blame] | 461 | put_ldev(mdev); |
Philipp Reisner | 1b7ab15 | 2011-07-15 17:19:02 +0200 | [diff] [blame] | 462 | return -ENODEV; |
Philipp Reisner | cdfda63 | 2011-07-05 15:38:59 +0200 | [diff] [blame] | 463 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 464 | |
Lars Ellenberg | 7ad651b | 2011-02-21 13:21:03 +0100 | [diff] [blame] | 465 | memset(buffer, 0, sizeof(*buffer)); |
| 466 | buffer->magic = cpu_to_be32(DRBD_AL_MAGIC); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 467 | buffer->tr_number = cpu_to_be32(mdev->al_tr_number); |
| 468 | |
Lars Ellenberg | 7ad651b | 2011-02-21 13:21:03 +0100 | [diff] [blame] | 469 | i = 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 470 | |
Lars Ellenberg | 7ad651b | 2011-02-21 13:21:03 +0100 | [diff] [blame] | 471 | /* Even though no one can start to change this list |
| 472 | * once we set the LC_LOCKED -- from drbd_al_begin_io(), |
| 473 | * lc_try_lock_for_transaction() --, someone may still |
| 474 | * be in the process of changing it. */ |
| 475 | spin_lock_irq(&mdev->al_lock); |
| 476 | list_for_each_entry(e, &mdev->act_log->to_be_changed, list) { |
| 477 | if (i == AL_UPDATES_PER_TRANSACTION) { |
| 478 | i++; |
| 479 | break; |
| 480 | } |
| 481 | buffer->update_slot_nr[i] = cpu_to_be16(e->lc_index); |
| 482 | buffer->update_extent_nr[i] = cpu_to_be32(e->lc_new_number); |
| 483 | if (e->lc_number != LC_FREE) |
| 484 | drbd_bm_mark_for_writeout(mdev, |
| 485 | al_extent_to_bm_page(e->lc_number)); |
| 486 | i++; |
| 487 | } |
| 488 | spin_unlock_irq(&mdev->al_lock); |
| 489 | BUG_ON(i > AL_UPDATES_PER_TRANSACTION); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 490 | |
Lars Ellenberg | 7ad651b | 2011-02-21 13:21:03 +0100 | [diff] [blame] | 491 | buffer->n_updates = cpu_to_be16(i); |
| 492 | for ( ; i < AL_UPDATES_PER_TRANSACTION; i++) { |
| 493 | buffer->update_slot_nr[i] = cpu_to_be16(-1); |
| 494 | buffer->update_extent_nr[i] = cpu_to_be32(LC_FREE); |
| 495 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 496 | |
Lars Ellenberg | 7ad651b | 2011-02-21 13:21:03 +0100 | [diff] [blame] | 497 | buffer->context_size = cpu_to_be16(mdev->act_log->nr_elements); |
| 498 | buffer->context_start_slot_nr = cpu_to_be16(mdev->al_tr_cycle); |
| 499 | |
| 500 | mx = min_t(int, AL_CONTEXT_PER_TRANSACTION, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 501 | mdev->act_log->nr_elements - mdev->al_tr_cycle); |
| 502 | for (i = 0; i < mx; i++) { |
| 503 | unsigned idx = mdev->al_tr_cycle + i; |
| 504 | extent_nr = lc_element_by_index(mdev->act_log, idx)->lc_number; |
Lars Ellenberg | 7ad651b | 2011-02-21 13:21:03 +0100 | [diff] [blame] | 505 | buffer->context[i] = cpu_to_be32(extent_nr); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 506 | } |
Lars Ellenberg | 7ad651b | 2011-02-21 13:21:03 +0100 | [diff] [blame] | 507 | for (; i < AL_CONTEXT_PER_TRANSACTION; i++) |
| 508 | buffer->context[i] = cpu_to_be32(LC_FREE); |
| 509 | |
| 510 | mdev->al_tr_cycle += AL_CONTEXT_PER_TRANSACTION; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 511 | if (mdev->al_tr_cycle >= mdev->act_log->nr_elements) |
| 512 | mdev->al_tr_cycle = 0; |
| 513 | |
Lars Ellenberg | ae8bf31 | 2013-03-19 18:16:43 +0100 | [diff] [blame] | 514 | sector = al_tr_number_to_on_disk_sector(mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 515 | |
Lars Ellenberg | 7ad651b | 2011-02-21 13:21:03 +0100 | [diff] [blame] | 516 | crc = crc32c(0, buffer, 4096); |
| 517 | buffer->crc32c = cpu_to_be32(crc); |
| 518 | |
| 519 | if (drbd_bm_write_hinted(mdev)) |
Philipp Reisner | 1b7ab15 | 2011-07-15 17:19:02 +0200 | [diff] [blame] | 520 | err = -EIO; |
Lars Ellenberg | b5bc8e0 | 2013-03-19 18:16:52 +0100 | [diff] [blame] | 521 | else { |
| 522 | bool write_al_updates; |
| 523 | rcu_read_lock(); |
| 524 | write_al_updates = rcu_dereference(mdev->ldev->disk_conf)->al_updates; |
| 525 | rcu_read_unlock(); |
| 526 | if (write_al_updates) { |
| 527 | if (drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE)) { |
| 528 | err = -EIO; |
| 529 | drbd_chk_io_error(mdev, 1, DRBD_META_IO_ERROR); |
| 530 | } else { |
| 531 | mdev->al_tr_number++; |
| 532 | mdev->al_writ_cnt++; |
| 533 | } |
| 534 | } |
Lars Ellenberg | 7ad651b | 2011-02-21 13:21:03 +0100 | [diff] [blame] | 535 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 536 | |
Philipp Reisner | cdfda63 | 2011-07-05 15:38:59 +0200 | [diff] [blame] | 537 | drbd_md_put_buffer(mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 538 | put_ldev(mdev); |
| 539 | |
Philipp Reisner | 1b7ab15 | 2011-07-15 17:19:02 +0200 | [diff] [blame] | 540 | return err; |
| 541 | } |
| 542 | |
| 543 | |
| 544 | static int w_al_write_transaction(struct drbd_work *w, int unused) |
| 545 | { |
| 546 | struct update_al_work *aw = container_of(w, struct update_al_work, w); |
| 547 | struct drbd_conf *mdev = w->mdev; |
| 548 | int err; |
| 549 | |
| 550 | err = _al_write_transaction(mdev); |
| 551 | aw->err = err; |
| 552 | complete(&aw->event); |
| 553 | |
| 554 | return err != -EIO ? err : 0; |
| 555 | } |
| 556 | |
| 557 | /* Calls from worker context (see w_restart_disk_io()) need to write the |
| 558 | transaction directly. Others came through generic_make_request(), |
| 559 | those need to delegate it to the worker. */ |
Lars Ellenberg | 56392d2 | 2013-03-19 18:16:48 +0100 | [diff] [blame] | 560 | static int al_write_transaction(struct drbd_conf *mdev, bool delegate) |
Philipp Reisner | 1b7ab15 | 2011-07-15 17:19:02 +0200 | [diff] [blame] | 561 | { |
Lars Ellenberg | 56392d2 | 2013-03-19 18:16:48 +0100 | [diff] [blame] | 562 | if (delegate) { |
| 563 | struct update_al_work al_work; |
| 564 | init_completion(&al_work.event); |
| 565 | al_work.w.cb = w_al_write_transaction; |
| 566 | al_work.w.mdev = mdev; |
| 567 | drbd_queue_work_front(&mdev->tconn->sender_work, &al_work.w); |
| 568 | wait_for_completion(&al_work.event); |
| 569 | return al_work.err; |
| 570 | } else |
Philipp Reisner | 1b7ab15 | 2011-07-15 17:19:02 +0200 | [diff] [blame] | 571 | return _al_write_transaction(mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 574 | static int _try_lc_del(struct drbd_conf *mdev, struct lc_element *al_ext) |
| 575 | { |
| 576 | int rv; |
| 577 | |
| 578 | spin_lock_irq(&mdev->al_lock); |
| 579 | rv = (al_ext->refcnt == 0); |
| 580 | if (likely(rv)) |
| 581 | lc_del(mdev->act_log, al_ext); |
| 582 | spin_unlock_irq(&mdev->al_lock); |
| 583 | |
| 584 | return rv; |
| 585 | } |
| 586 | |
| 587 | /** |
| 588 | * drbd_al_shrink() - Removes all active extents form the activity log |
| 589 | * @mdev: DRBD device. |
| 590 | * |
| 591 | * Removes all active extents form the activity log, waiting until |
| 592 | * the reference count of each entry dropped to 0 first, of course. |
| 593 | * |
| 594 | * You need to lock mdev->act_log with lc_try_lock() / lc_unlock() |
| 595 | */ |
| 596 | void drbd_al_shrink(struct drbd_conf *mdev) |
| 597 | { |
| 598 | struct lc_element *al_ext; |
| 599 | int i; |
| 600 | |
Lars Ellenberg | 46a15bc | 2011-02-21 13:21:01 +0100 | [diff] [blame] | 601 | D_ASSERT(test_bit(__LC_LOCKED, &mdev->act_log->flags)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 602 | |
| 603 | for (i = 0; i < mdev->act_log->nr_elements; i++) { |
| 604 | al_ext = lc_element_by_index(mdev->act_log, i); |
| 605 | if (al_ext->lc_number == LC_FREE) |
| 606 | continue; |
| 607 | wait_event(mdev->al_wait, _try_lc_del(mdev, al_ext)); |
| 608 | } |
| 609 | |
| 610 | wake_up(&mdev->al_wait); |
| 611 | } |
| 612 | |
Andreas Gruenbacher | 99920dc | 2011-03-16 15:31:39 +0100 | [diff] [blame] | 613 | static int w_update_odbm(struct drbd_work *w, int unused) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 614 | { |
| 615 | struct update_odbm_work *udw = container_of(w, struct update_odbm_work, w); |
Philipp Reisner | 00d5694 | 2011-02-09 18:09:48 +0100 | [diff] [blame] | 616 | struct drbd_conf *mdev = w->mdev; |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 617 | struct sib_info sib = { .sib_reason = SIB_SYNC_PROGRESS, }; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 618 | |
| 619 | if (!get_ldev(mdev)) { |
| 620 | if (__ratelimit(&drbd_ratelimit_state)) |
| 621 | dev_warn(DEV, "Can not update on disk bitmap, local IO disabled.\n"); |
| 622 | kfree(udw); |
Andreas Gruenbacher | 99920dc | 2011-03-16 15:31:39 +0100 | [diff] [blame] | 623 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 624 | } |
| 625 | |
Lars Ellenberg | 19f843a | 2010-12-15 08:59:11 +0100 | [diff] [blame] | 626 | drbd_bm_write_page(mdev, rs_extent_to_bm_page(udw->enr)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 627 | put_ldev(mdev); |
| 628 | |
| 629 | kfree(udw); |
| 630 | |
| 631 | if (drbd_bm_total_weight(mdev) <= mdev->rs_failed) { |
| 632 | switch (mdev->state.conn) { |
| 633 | case C_SYNC_SOURCE: case C_SYNC_TARGET: |
| 634 | case C_PAUSED_SYNC_S: case C_PAUSED_SYNC_T: |
| 635 | drbd_resync_finished(mdev); |
| 636 | default: |
| 637 | /* nothing to do */ |
| 638 | break; |
| 639 | } |
| 640 | } |
Lars Ellenberg | 3b98c0c | 2011-03-07 12:49:34 +0100 | [diff] [blame] | 641 | drbd_bcast_event(mdev, &sib); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 642 | |
Andreas Gruenbacher | 99920dc | 2011-03-16 15:31:39 +0100 | [diff] [blame] | 643 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | |
| 647 | /* ATTENTION. The AL's extents are 4MB each, while the extents in the |
| 648 | * resync LRU-cache are 16MB each. |
| 649 | * The caller of this function has to hold an get_ldev() reference. |
| 650 | * |
| 651 | * TODO will be obsoleted once we have a caching lru of the on disk bitmap |
| 652 | */ |
| 653 | static void drbd_try_clear_on_disk_bm(struct drbd_conf *mdev, sector_t sector, |
| 654 | int count, int success) |
| 655 | { |
| 656 | struct lc_element *e; |
| 657 | struct update_odbm_work *udw; |
| 658 | |
| 659 | unsigned int enr; |
| 660 | |
| 661 | D_ASSERT(atomic_read(&mdev->local_cnt)); |
| 662 | |
| 663 | /* I simply assume that a sector/size pair never crosses |
| 664 | * a 16 MB extent border. (Currently this is true...) */ |
| 665 | enr = BM_SECT_TO_EXT(sector); |
| 666 | |
| 667 | e = lc_get(mdev->resync, enr); |
| 668 | if (e) { |
| 669 | struct bm_extent *ext = lc_entry(e, struct bm_extent, lce); |
| 670 | if (ext->lce.lc_number == enr) { |
| 671 | if (success) |
| 672 | ext->rs_left -= count; |
| 673 | else |
| 674 | ext->rs_failed += count; |
| 675 | if (ext->rs_left < ext->rs_failed) { |
Philipp Reisner | 975b297 | 2011-11-17 10:11:47 +0100 | [diff] [blame] | 676 | dev_warn(DEV, "BAD! sector=%llus enr=%u rs_left=%d " |
| 677 | "rs_failed=%d count=%d cstate=%s\n", |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 678 | (unsigned long long)sector, |
| 679 | ext->lce.lc_number, ext->rs_left, |
Philipp Reisner | 975b297 | 2011-11-17 10:11:47 +0100 | [diff] [blame] | 680 | ext->rs_failed, count, |
| 681 | drbd_conn_str(mdev->state.conn)); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 682 | |
Philipp Reisner | 975b297 | 2011-11-17 10:11:47 +0100 | [diff] [blame] | 683 | /* We don't expect to be able to clear more bits |
| 684 | * than have been set when we originally counted |
| 685 | * the set bits to cache that value in ext->rs_left. |
| 686 | * Whatever the reason (disconnect during resync, |
| 687 | * delayed local completion of an application write), |
| 688 | * try to fix it up by recounting here. */ |
| 689 | ext->rs_left = drbd_bm_e_weight(mdev, enr); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 690 | } |
| 691 | } else { |
| 692 | /* Normally this element should be in the cache, |
| 693 | * since drbd_rs_begin_io() pulled it already in. |
| 694 | * |
| 695 | * But maybe an application write finished, and we set |
| 696 | * something outside the resync lru_cache in sync. |
| 697 | */ |
| 698 | int rs_left = drbd_bm_e_weight(mdev, enr); |
| 699 | if (ext->flags != 0) { |
| 700 | dev_warn(DEV, "changing resync lce: %d[%u;%02lx]" |
| 701 | " -> %d[%u;00]\n", |
| 702 | ext->lce.lc_number, ext->rs_left, |
| 703 | ext->flags, enr, rs_left); |
| 704 | ext->flags = 0; |
| 705 | } |
| 706 | if (ext->rs_failed) { |
| 707 | dev_warn(DEV, "Kicking resync_lru element enr=%u " |
| 708 | "out with rs_failed=%d\n", |
| 709 | ext->lce.lc_number, ext->rs_failed); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 710 | } |
| 711 | ext->rs_left = rs_left; |
| 712 | ext->rs_failed = success ? 0 : count; |
Lars Ellenberg | 46a15bc | 2011-02-21 13:21:01 +0100 | [diff] [blame] | 713 | /* we don't keep a persistent log of the resync lru, |
| 714 | * we can commit any change right away. */ |
| 715 | lc_committed(mdev->resync); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 716 | } |
| 717 | lc_put(mdev->resync, &ext->lce); |
| 718 | /* no race, we are within the al_lock! */ |
| 719 | |
| 720 | if (ext->rs_left == ext->rs_failed) { |
| 721 | ext->rs_failed = 0; |
| 722 | |
| 723 | udw = kmalloc(sizeof(*udw), GFP_ATOMIC); |
| 724 | if (udw) { |
| 725 | udw->enr = ext->lce.lc_number; |
| 726 | udw->w.cb = w_update_odbm; |
Philipp Reisner | a21e929 | 2011-02-08 15:08:49 +0100 | [diff] [blame] | 727 | udw->w.mdev = mdev; |
Lars Ellenberg | d5b27b0 | 2011-11-14 15:42:37 +0100 | [diff] [blame] | 728 | drbd_queue_work_front(&mdev->tconn->sender_work, &udw->w); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 729 | } else { |
| 730 | dev_warn(DEV, "Could not kmalloc an udw\n"); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | } else { |
| 734 | dev_err(DEV, "lc_get() failed! locked=%d/%d flags=%lu\n", |
| 735 | mdev->resync_locked, |
| 736 | mdev->resync->nr_elements, |
| 737 | mdev->resync->flags); |
| 738 | } |
| 739 | } |
| 740 | |
Lars Ellenberg | c6ea14d | 2010-11-05 09:23:37 +0100 | [diff] [blame] | 741 | void drbd_advance_rs_marks(struct drbd_conf *mdev, unsigned long still_to_go) |
| 742 | { |
| 743 | unsigned long now = jiffies; |
| 744 | unsigned long last = mdev->rs_mark_time[mdev->rs_last_mark]; |
| 745 | int next = (mdev->rs_last_mark + 1) % DRBD_SYNC_MARKS; |
| 746 | if (time_after_eq(now, last + DRBD_SYNC_MARK_STEP)) { |
| 747 | if (mdev->rs_mark_left[mdev->rs_last_mark] != still_to_go && |
| 748 | mdev->state.conn != C_PAUSED_SYNC_T && |
| 749 | mdev->state.conn != C_PAUSED_SYNC_S) { |
| 750 | mdev->rs_mark_time[next] = now; |
| 751 | mdev->rs_mark_left[next] = still_to_go; |
| 752 | mdev->rs_last_mark = next; |
| 753 | } |
| 754 | } |
| 755 | } |
| 756 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 757 | /* clear the bit corresponding to the piece of storage in question: |
| 758 | * size byte of data starting from sector. Only clear a bits of the affected |
| 759 | * one ore more _aligned_ BM_BLOCK_SIZE blocks. |
| 760 | * |
| 761 | * called by worker on C_SYNC_TARGET and receiver on SyncSource. |
| 762 | * |
| 763 | */ |
| 764 | void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector, int size, |
| 765 | const char *file, const unsigned int line) |
| 766 | { |
| 767 | /* Is called from worker and receiver context _only_ */ |
| 768 | unsigned long sbnr, ebnr, lbnr; |
| 769 | unsigned long count = 0; |
| 770 | sector_t esector, nr_sectors; |
| 771 | int wake_up = 0; |
| 772 | unsigned long flags; |
| 773 | |
Andreas Gruenbacher | c670a39 | 2011-02-21 12:41:39 +0100 | [diff] [blame] | 774 | if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 775 | dev_err(DEV, "drbd_set_in_sync: sector=%llus size=%d nonsense!\n", |
| 776 | (unsigned long long)sector, size); |
| 777 | return; |
| 778 | } |
Philipp Reisner | 518a4d5 | 2012-10-19 14:21:22 +0200 | [diff] [blame] | 779 | |
| 780 | if (!get_ldev(mdev)) |
| 781 | return; /* no disk, no metadata, no bitmap to clear bits in */ |
| 782 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 783 | nr_sectors = drbd_get_capacity(mdev->this_bdev); |
| 784 | esector = sector + (size >> 9) - 1; |
| 785 | |
Andreas Gruenbacher | 841ce24 | 2010-12-15 19:31:20 +0100 | [diff] [blame] | 786 | if (!expect(sector < nr_sectors)) |
Philipp Reisner | 518a4d5 | 2012-10-19 14:21:22 +0200 | [diff] [blame] | 787 | goto out; |
Andreas Gruenbacher | 841ce24 | 2010-12-15 19:31:20 +0100 | [diff] [blame] | 788 | if (!expect(esector < nr_sectors)) |
| 789 | esector = nr_sectors - 1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 790 | |
| 791 | lbnr = BM_SECT_TO_BIT(nr_sectors-1); |
| 792 | |
| 793 | /* we clear it (in sync). |
| 794 | * round up start sector, round down end sector. we make sure we only |
| 795 | * clear full, aligned, BM_BLOCK_SIZE (4K) blocks */ |
| 796 | if (unlikely(esector < BM_SECT_PER_BIT-1)) |
Philipp Reisner | 518a4d5 | 2012-10-19 14:21:22 +0200 | [diff] [blame] | 797 | goto out; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 798 | if (unlikely(esector == (nr_sectors-1))) |
| 799 | ebnr = lbnr; |
| 800 | else |
| 801 | ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1)); |
| 802 | sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1); |
| 803 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 804 | if (sbnr > ebnr) |
Philipp Reisner | 518a4d5 | 2012-10-19 14:21:22 +0200 | [diff] [blame] | 805 | goto out; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 806 | |
| 807 | /* |
| 808 | * ok, (capacity & 7) != 0 sometimes, but who cares... |
| 809 | * we count rs_{total,left} in bits, not sectors. |
| 810 | */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 811 | count = drbd_bm_clear_bits(mdev, sbnr, ebnr); |
Philipp Reisner | 518a4d5 | 2012-10-19 14:21:22 +0200 | [diff] [blame] | 812 | if (count) { |
Lars Ellenberg | c6ea14d | 2010-11-05 09:23:37 +0100 | [diff] [blame] | 813 | drbd_advance_rs_marks(mdev, drbd_bm_total_weight(mdev)); |
Lars Ellenberg | 1d7734a | 2010-08-11 21:21:50 +0200 | [diff] [blame] | 814 | spin_lock_irqsave(&mdev->al_lock, flags); |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 815 | drbd_try_clear_on_disk_bm(mdev, sector, count, true); |
Lars Ellenberg | 1d7734a | 2010-08-11 21:21:50 +0200 | [diff] [blame] | 816 | spin_unlock_irqrestore(&mdev->al_lock, flags); |
| 817 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 818 | /* just wake_up unconditional now, various lc_chaged(), |
| 819 | * lc_put() in drbd_try_clear_on_disk_bm(). */ |
| 820 | wake_up = 1; |
| 821 | } |
Philipp Reisner | 518a4d5 | 2012-10-19 14:21:22 +0200 | [diff] [blame] | 822 | out: |
| 823 | put_ldev(mdev); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 824 | if (wake_up) |
| 825 | wake_up(&mdev->al_wait); |
| 826 | } |
| 827 | |
| 828 | /* |
| 829 | * this is intended to set one request worth of data out of sync. |
| 830 | * affects at least 1 bit, |
Lars Ellenberg | 1816a2b | 2010-11-11 15:19:07 +0100 | [diff] [blame] | 831 | * and at most 1+DRBD_MAX_BIO_SIZE/BM_BLOCK_SIZE bits. |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 832 | * |
| 833 | * called by tl_clear and drbd_send_dblock (==drbd_make_request). |
| 834 | * so this can be _any_ process. |
| 835 | */ |
Philipp Reisner | 73a01a1 | 2010-10-27 14:33:00 +0200 | [diff] [blame] | 836 | int __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector, int size, |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 837 | const char *file, const unsigned int line) |
| 838 | { |
Philipp Reisner | 376694a | 2011-11-07 10:54:28 +0100 | [diff] [blame] | 839 | unsigned long sbnr, ebnr, flags; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 840 | sector_t esector, nr_sectors; |
Philipp Reisner | 73a01a1 | 2010-10-27 14:33:00 +0200 | [diff] [blame] | 841 | unsigned int enr, count = 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 842 | struct lc_element *e; |
| 843 | |
Lars Ellenberg | 81a3537 | 2012-07-30 09:00:54 +0200 | [diff] [blame] | 844 | /* this should be an empty REQ_FLUSH */ |
| 845 | if (size == 0) |
| 846 | return 0; |
| 847 | |
| 848 | if (size < 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 849 | dev_err(DEV, "sector: %llus, size: %d\n", |
| 850 | (unsigned long long)sector, size); |
Philipp Reisner | 73a01a1 | 2010-10-27 14:33:00 +0200 | [diff] [blame] | 851 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | if (!get_ldev(mdev)) |
Philipp Reisner | 73a01a1 | 2010-10-27 14:33:00 +0200 | [diff] [blame] | 855 | return 0; /* no disk, no metadata, no bitmap to set bits in */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 856 | |
| 857 | nr_sectors = drbd_get_capacity(mdev->this_bdev); |
| 858 | esector = sector + (size >> 9) - 1; |
| 859 | |
Andreas Gruenbacher | 841ce24 | 2010-12-15 19:31:20 +0100 | [diff] [blame] | 860 | if (!expect(sector < nr_sectors)) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 861 | goto out; |
Andreas Gruenbacher | 841ce24 | 2010-12-15 19:31:20 +0100 | [diff] [blame] | 862 | if (!expect(esector < nr_sectors)) |
| 863 | esector = nr_sectors - 1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 864 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 865 | /* we set it out of sync, |
| 866 | * we do not need to round anything here */ |
| 867 | sbnr = BM_SECT_TO_BIT(sector); |
| 868 | ebnr = BM_SECT_TO_BIT(esector); |
| 869 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 870 | /* ok, (capacity & 7) != 0 sometimes, but who cares... |
| 871 | * we count rs_{total,left} in bits, not sectors. */ |
| 872 | spin_lock_irqsave(&mdev->al_lock, flags); |
| 873 | count = drbd_bm_set_bits(mdev, sbnr, ebnr); |
| 874 | |
| 875 | enr = BM_SECT_TO_EXT(sector); |
| 876 | e = lc_find(mdev->resync, enr); |
| 877 | if (e) |
| 878 | lc_entry(e, struct bm_extent, lce)->rs_left += count; |
| 879 | spin_unlock_irqrestore(&mdev->al_lock, flags); |
| 880 | |
| 881 | out: |
| 882 | put_ldev(mdev); |
Philipp Reisner | 73a01a1 | 2010-10-27 14:33:00 +0200 | [diff] [blame] | 883 | |
| 884 | return count; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | static |
| 888 | struct bm_extent *_bme_get(struct drbd_conf *mdev, unsigned int enr) |
| 889 | { |
| 890 | struct lc_element *e; |
| 891 | struct bm_extent *bm_ext; |
| 892 | int wakeup = 0; |
| 893 | unsigned long rs_flags; |
| 894 | |
| 895 | spin_lock_irq(&mdev->al_lock); |
| 896 | if (mdev->resync_locked > mdev->resync->nr_elements/2) { |
| 897 | spin_unlock_irq(&mdev->al_lock); |
| 898 | return NULL; |
| 899 | } |
| 900 | e = lc_get(mdev->resync, enr); |
| 901 | bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; |
| 902 | if (bm_ext) { |
| 903 | if (bm_ext->lce.lc_number != enr) { |
| 904 | bm_ext->rs_left = drbd_bm_e_weight(mdev, enr); |
| 905 | bm_ext->rs_failed = 0; |
Lars Ellenberg | 46a15bc | 2011-02-21 13:21:01 +0100 | [diff] [blame] | 906 | lc_committed(mdev->resync); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 907 | wakeup = 1; |
| 908 | } |
| 909 | if (bm_ext->lce.refcnt == 1) |
| 910 | mdev->resync_locked++; |
| 911 | set_bit(BME_NO_WRITES, &bm_ext->flags); |
| 912 | } |
| 913 | rs_flags = mdev->resync->flags; |
| 914 | spin_unlock_irq(&mdev->al_lock); |
| 915 | if (wakeup) |
| 916 | wake_up(&mdev->al_wait); |
| 917 | |
| 918 | if (!bm_ext) { |
| 919 | if (rs_flags & LC_STARVING) |
| 920 | dev_warn(DEV, "Have to wait for element" |
| 921 | " (resync LRU too small?)\n"); |
Lars Ellenberg | 46a15bc | 2011-02-21 13:21:01 +0100 | [diff] [blame] | 922 | BUG_ON(rs_flags & LC_LOCKED); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | return bm_ext; |
| 926 | } |
| 927 | |
| 928 | static int _is_in_al(struct drbd_conf *mdev, unsigned int enr) |
| 929 | { |
Lars Ellenberg | 46a15bc | 2011-02-21 13:21:01 +0100 | [diff] [blame] | 930 | int rv; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 931 | |
| 932 | spin_lock_irq(&mdev->al_lock); |
Lars Ellenberg | 46a15bc | 2011-02-21 13:21:01 +0100 | [diff] [blame] | 933 | rv = lc_is_used(mdev->act_log, enr); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 934 | spin_unlock_irq(&mdev->al_lock); |
| 935 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 936 | return rv; |
| 937 | } |
| 938 | |
| 939 | /** |
| 940 | * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED |
| 941 | * @mdev: DRBD device. |
| 942 | * @sector: The sector number. |
| 943 | * |
Lars Ellenberg | 80a40e4 | 2010-08-11 23:28:00 +0200 | [diff] [blame] | 944 | * This functions sleeps on al_wait. Returns 0 on success, -EINTR if interrupted. |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 945 | */ |
| 946 | int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector) |
| 947 | { |
| 948 | unsigned int enr = BM_SECT_TO_EXT(sector); |
| 949 | struct bm_extent *bm_ext; |
| 950 | int i, sig; |
Philipp Reisner | f91ab62 | 2010-11-09 13:59:41 +0100 | [diff] [blame] | 951 | int sa = 200; /* Step aside 200 times, then grab the extent and let app-IO wait. |
| 952 | 200 times -> 20 seconds. */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 953 | |
Philipp Reisner | f91ab62 | 2010-11-09 13:59:41 +0100 | [diff] [blame] | 954 | retry: |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 955 | sig = wait_event_interruptible(mdev->al_wait, |
| 956 | (bm_ext = _bme_get(mdev, enr))); |
| 957 | if (sig) |
Lars Ellenberg | 80a40e4 | 2010-08-11 23:28:00 +0200 | [diff] [blame] | 958 | return -EINTR; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 959 | |
| 960 | if (test_bit(BME_LOCKED, &bm_ext->flags)) |
Lars Ellenberg | 80a40e4 | 2010-08-11 23:28:00 +0200 | [diff] [blame] | 961 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 962 | |
| 963 | for (i = 0; i < AL_EXT_PER_BM_SECT; i++) { |
| 964 | sig = wait_event_interruptible(mdev->al_wait, |
Philipp Reisner | f91ab62 | 2010-11-09 13:59:41 +0100 | [diff] [blame] | 965 | !_is_in_al(mdev, enr * AL_EXT_PER_BM_SECT + i) || |
Philipp Reisner | c507f46 | 2010-11-22 15:49:17 +0100 | [diff] [blame] | 966 | test_bit(BME_PRIORITY, &bm_ext->flags)); |
Philipp Reisner | f91ab62 | 2010-11-09 13:59:41 +0100 | [diff] [blame] | 967 | |
| 968 | if (sig || (test_bit(BME_PRIORITY, &bm_ext->flags) && sa)) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 969 | spin_lock_irq(&mdev->al_lock); |
| 970 | if (lc_put(mdev->resync, &bm_ext->lce) == 0) { |
Philipp Reisner | f91ab62 | 2010-11-09 13:59:41 +0100 | [diff] [blame] | 971 | bm_ext->flags = 0; /* clears BME_NO_WRITES and eventually BME_PRIORITY */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 972 | mdev->resync_locked--; |
| 973 | wake_up(&mdev->al_wait); |
| 974 | } |
| 975 | spin_unlock_irq(&mdev->al_lock); |
Philipp Reisner | f91ab62 | 2010-11-09 13:59:41 +0100 | [diff] [blame] | 976 | if (sig) |
| 977 | return -EINTR; |
| 978 | if (schedule_timeout_interruptible(HZ/10)) |
| 979 | return -EINTR; |
Philipp Reisner | c507f46 | 2010-11-22 15:49:17 +0100 | [diff] [blame] | 980 | if (sa && --sa == 0) |
| 981 | dev_warn(DEV,"drbd_rs_begin_io() stepped aside for 20sec." |
| 982 | "Resync stalled?\n"); |
Philipp Reisner | f91ab62 | 2010-11-09 13:59:41 +0100 | [diff] [blame] | 983 | goto retry; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 984 | } |
| 985 | } |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 986 | set_bit(BME_LOCKED, &bm_ext->flags); |
Lars Ellenberg | 80a40e4 | 2010-08-11 23:28:00 +0200 | [diff] [blame] | 987 | return 0; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | /** |
| 991 | * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep |
| 992 | * @mdev: DRBD device. |
| 993 | * @sector: The sector number. |
| 994 | * |
| 995 | * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then |
| 996 | * tries to set it to BME_LOCKED. Returns 0 upon success, and -EAGAIN |
| 997 | * if there is still application IO going on in this area. |
| 998 | */ |
| 999 | int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector) |
| 1000 | { |
| 1001 | unsigned int enr = BM_SECT_TO_EXT(sector); |
| 1002 | const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT; |
| 1003 | struct lc_element *e; |
| 1004 | struct bm_extent *bm_ext; |
| 1005 | int i; |
| 1006 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1007 | spin_lock_irq(&mdev->al_lock); |
| 1008 | if (mdev->resync_wenr != LC_FREE && mdev->resync_wenr != enr) { |
| 1009 | /* in case you have very heavy scattered io, it may |
| 1010 | * stall the syncer undefined if we give up the ref count |
| 1011 | * when we try again and requeue. |
| 1012 | * |
| 1013 | * if we don't give up the refcount, but the next time |
| 1014 | * we are scheduled this extent has been "synced" by new |
| 1015 | * application writes, we'd miss the lc_put on the |
| 1016 | * extent we keep the refcount on. |
| 1017 | * so we remembered which extent we had to try again, and |
| 1018 | * if the next requested one is something else, we do |
| 1019 | * the lc_put here... |
| 1020 | * we also have to wake_up |
| 1021 | */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1022 | e = lc_find(mdev->resync, mdev->resync_wenr); |
| 1023 | bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; |
| 1024 | if (bm_ext) { |
| 1025 | D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags)); |
| 1026 | D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags)); |
| 1027 | clear_bit(BME_NO_WRITES, &bm_ext->flags); |
| 1028 | mdev->resync_wenr = LC_FREE; |
| 1029 | if (lc_put(mdev->resync, &bm_ext->lce) == 0) |
| 1030 | mdev->resync_locked--; |
| 1031 | wake_up(&mdev->al_wait); |
| 1032 | } else { |
| 1033 | dev_alert(DEV, "LOGIC BUG\n"); |
| 1034 | } |
| 1035 | } |
| 1036 | /* TRY. */ |
| 1037 | e = lc_try_get(mdev->resync, enr); |
| 1038 | bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; |
| 1039 | if (bm_ext) { |
| 1040 | if (test_bit(BME_LOCKED, &bm_ext->flags)) |
| 1041 | goto proceed; |
| 1042 | if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) { |
| 1043 | mdev->resync_locked++; |
| 1044 | } else { |
| 1045 | /* we did set the BME_NO_WRITES, |
| 1046 | * but then could not set BME_LOCKED, |
| 1047 | * so we tried again. |
| 1048 | * drop the extra reference. */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1049 | bm_ext->lce.refcnt--; |
| 1050 | D_ASSERT(bm_ext->lce.refcnt > 0); |
| 1051 | } |
| 1052 | goto check_al; |
| 1053 | } else { |
| 1054 | /* do we rather want to try later? */ |
Jens Axboe | 6a0afdf | 2009-10-01 09:04:14 +0200 | [diff] [blame] | 1055 | if (mdev->resync_locked > mdev->resync->nr_elements-3) |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1056 | goto try_again; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1057 | /* Do or do not. There is no try. -- Yoda */ |
| 1058 | e = lc_get(mdev->resync, enr); |
| 1059 | bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; |
| 1060 | if (!bm_ext) { |
| 1061 | const unsigned long rs_flags = mdev->resync->flags; |
| 1062 | if (rs_flags & LC_STARVING) |
| 1063 | dev_warn(DEV, "Have to wait for element" |
| 1064 | " (resync LRU too small?)\n"); |
Lars Ellenberg | 46a15bc | 2011-02-21 13:21:01 +0100 | [diff] [blame] | 1065 | BUG_ON(rs_flags & LC_LOCKED); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1066 | goto try_again; |
| 1067 | } |
| 1068 | if (bm_ext->lce.lc_number != enr) { |
| 1069 | bm_ext->rs_left = drbd_bm_e_weight(mdev, enr); |
| 1070 | bm_ext->rs_failed = 0; |
Lars Ellenberg | 46a15bc | 2011-02-21 13:21:01 +0100 | [diff] [blame] | 1071 | lc_committed(mdev->resync); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1072 | wake_up(&mdev->al_wait); |
| 1073 | D_ASSERT(test_bit(BME_LOCKED, &bm_ext->flags) == 0); |
| 1074 | } |
| 1075 | set_bit(BME_NO_WRITES, &bm_ext->flags); |
| 1076 | D_ASSERT(bm_ext->lce.refcnt == 1); |
| 1077 | mdev->resync_locked++; |
| 1078 | goto check_al; |
| 1079 | } |
| 1080 | check_al: |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1081 | for (i = 0; i < AL_EXT_PER_BM_SECT; i++) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1082 | if (lc_is_used(mdev->act_log, al_enr+i)) |
| 1083 | goto try_again; |
| 1084 | } |
| 1085 | set_bit(BME_LOCKED, &bm_ext->flags); |
| 1086 | proceed: |
| 1087 | mdev->resync_wenr = LC_FREE; |
| 1088 | spin_unlock_irq(&mdev->al_lock); |
| 1089 | return 0; |
| 1090 | |
| 1091 | try_again: |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1092 | if (bm_ext) |
| 1093 | mdev->resync_wenr = enr; |
| 1094 | spin_unlock_irq(&mdev->al_lock); |
| 1095 | return -EAGAIN; |
| 1096 | } |
| 1097 | |
| 1098 | void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector) |
| 1099 | { |
| 1100 | unsigned int enr = BM_SECT_TO_EXT(sector); |
| 1101 | struct lc_element *e; |
| 1102 | struct bm_extent *bm_ext; |
| 1103 | unsigned long flags; |
| 1104 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1105 | spin_lock_irqsave(&mdev->al_lock, flags); |
| 1106 | e = lc_find(mdev->resync, enr); |
| 1107 | bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; |
| 1108 | if (!bm_ext) { |
| 1109 | spin_unlock_irqrestore(&mdev->al_lock, flags); |
| 1110 | if (__ratelimit(&drbd_ratelimit_state)) |
| 1111 | dev_err(DEV, "drbd_rs_complete_io() called, but extent not found\n"); |
| 1112 | return; |
| 1113 | } |
| 1114 | |
| 1115 | if (bm_ext->lce.refcnt == 0) { |
| 1116 | spin_unlock_irqrestore(&mdev->al_lock, flags); |
| 1117 | dev_err(DEV, "drbd_rs_complete_io(,%llu [=%u]) called, " |
| 1118 | "but refcnt is 0!?\n", |
| 1119 | (unsigned long long)sector, enr); |
| 1120 | return; |
| 1121 | } |
| 1122 | |
| 1123 | if (lc_put(mdev->resync, &bm_ext->lce) == 0) { |
Philipp Reisner | e3555d8 | 2010-11-07 15:56:29 +0100 | [diff] [blame] | 1124 | bm_ext->flags = 0; /* clear BME_LOCKED, BME_NO_WRITES and BME_PRIORITY */ |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1125 | mdev->resync_locked--; |
| 1126 | wake_up(&mdev->al_wait); |
| 1127 | } |
| 1128 | |
| 1129 | spin_unlock_irqrestore(&mdev->al_lock, flags); |
| 1130 | } |
| 1131 | |
| 1132 | /** |
| 1133 | * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED) |
| 1134 | * @mdev: DRBD device. |
| 1135 | */ |
| 1136 | void drbd_rs_cancel_all(struct drbd_conf *mdev) |
| 1137 | { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1138 | spin_lock_irq(&mdev->al_lock); |
| 1139 | |
| 1140 | if (get_ldev_if_state(mdev, D_FAILED)) { /* Makes sure ->resync is there. */ |
| 1141 | lc_reset(mdev->resync); |
| 1142 | put_ldev(mdev); |
| 1143 | } |
| 1144 | mdev->resync_locked = 0; |
| 1145 | mdev->resync_wenr = LC_FREE; |
| 1146 | spin_unlock_irq(&mdev->al_lock); |
| 1147 | wake_up(&mdev->al_wait); |
| 1148 | } |
| 1149 | |
| 1150 | /** |
| 1151 | * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU |
| 1152 | * @mdev: DRBD device. |
| 1153 | * |
| 1154 | * Returns 0 upon success, -EAGAIN if at least one reference count was |
| 1155 | * not zero. |
| 1156 | */ |
| 1157 | int drbd_rs_del_all(struct drbd_conf *mdev) |
| 1158 | { |
| 1159 | struct lc_element *e; |
| 1160 | struct bm_extent *bm_ext; |
| 1161 | int i; |
| 1162 | |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1163 | spin_lock_irq(&mdev->al_lock); |
| 1164 | |
| 1165 | if (get_ldev_if_state(mdev, D_FAILED)) { |
| 1166 | /* ok, ->resync is there. */ |
| 1167 | for (i = 0; i < mdev->resync->nr_elements; i++) { |
| 1168 | e = lc_element_by_index(mdev->resync, i); |
Philipp Reisner | b2b163d | 2010-04-02 08:40:33 +0200 | [diff] [blame] | 1169 | bm_ext = lc_entry(e, struct bm_extent, lce); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1170 | if (bm_ext->lce.lc_number == LC_FREE) |
| 1171 | continue; |
| 1172 | if (bm_ext->lce.lc_number == mdev->resync_wenr) { |
| 1173 | dev_info(DEV, "dropping %u in drbd_rs_del_all, apparently" |
| 1174 | " got 'synced' by application io\n", |
| 1175 | mdev->resync_wenr); |
| 1176 | D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags)); |
| 1177 | D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags)); |
| 1178 | clear_bit(BME_NO_WRITES, &bm_ext->flags); |
| 1179 | mdev->resync_wenr = LC_FREE; |
| 1180 | lc_put(mdev->resync, &bm_ext->lce); |
| 1181 | } |
| 1182 | if (bm_ext->lce.refcnt != 0) { |
| 1183 | dev_info(DEV, "Retrying drbd_rs_del_all() later. " |
| 1184 | "refcnt=%d\n", bm_ext->lce.refcnt); |
| 1185 | put_ldev(mdev); |
| 1186 | spin_unlock_irq(&mdev->al_lock); |
| 1187 | return -EAGAIN; |
| 1188 | } |
| 1189 | D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags)); |
| 1190 | D_ASSERT(!test_bit(BME_NO_WRITES, &bm_ext->flags)); |
| 1191 | lc_del(mdev->resync, &bm_ext->lce); |
| 1192 | } |
| 1193 | D_ASSERT(mdev->resync->used == 0); |
| 1194 | put_ldev(mdev); |
| 1195 | } |
| 1196 | spin_unlock_irq(&mdev->al_lock); |
Lars Ellenberg | a6a7d4f | 2012-03-26 16:21:37 +0200 | [diff] [blame] | 1197 | wake_up(&mdev->al_wait); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1198 | |
| 1199 | return 0; |
| 1200 | } |
| 1201 | |
| 1202 | /** |
| 1203 | * drbd_rs_failed_io() - Record information on a failure to resync the specified blocks |
| 1204 | * @mdev: DRBD device. |
| 1205 | * @sector: The sector number. |
| 1206 | * @size: Size of failed IO operation, in byte. |
| 1207 | */ |
| 1208 | void drbd_rs_failed_io(struct drbd_conf *mdev, sector_t sector, int size) |
| 1209 | { |
| 1210 | /* Is called from worker and receiver context _only_ */ |
| 1211 | unsigned long sbnr, ebnr, lbnr; |
| 1212 | unsigned long count; |
| 1213 | sector_t esector, nr_sectors; |
| 1214 | int wake_up = 0; |
| 1215 | |
Andreas Gruenbacher | c670a39 | 2011-02-21 12:41:39 +0100 | [diff] [blame] | 1216 | if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) { |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1217 | dev_err(DEV, "drbd_rs_failed_io: sector=%llus size=%d nonsense!\n", |
| 1218 | (unsigned long long)sector, size); |
| 1219 | return; |
| 1220 | } |
| 1221 | nr_sectors = drbd_get_capacity(mdev->this_bdev); |
| 1222 | esector = sector + (size >> 9) - 1; |
| 1223 | |
Andreas Gruenbacher | 841ce24 | 2010-12-15 19:31:20 +0100 | [diff] [blame] | 1224 | if (!expect(sector < nr_sectors)) |
| 1225 | return; |
| 1226 | if (!expect(esector < nr_sectors)) |
| 1227 | esector = nr_sectors - 1; |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1228 | |
| 1229 | lbnr = BM_SECT_TO_BIT(nr_sectors-1); |
| 1230 | |
| 1231 | /* |
| 1232 | * round up start sector, round down end sector. we make sure we only |
| 1233 | * handle full, aligned, BM_BLOCK_SIZE (4K) blocks */ |
| 1234 | if (unlikely(esector < BM_SECT_PER_BIT-1)) |
| 1235 | return; |
| 1236 | if (unlikely(esector == (nr_sectors-1))) |
| 1237 | ebnr = lbnr; |
| 1238 | else |
| 1239 | ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1)); |
| 1240 | sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1); |
| 1241 | |
| 1242 | if (sbnr > ebnr) |
| 1243 | return; |
| 1244 | |
| 1245 | /* |
| 1246 | * ok, (capacity & 7) != 0 sometimes, but who cares... |
| 1247 | * we count rs_{total,left} in bits, not sectors. |
| 1248 | */ |
| 1249 | spin_lock_irq(&mdev->al_lock); |
| 1250 | count = drbd_bm_count_bits(mdev, sbnr, ebnr); |
| 1251 | if (count) { |
| 1252 | mdev->rs_failed += count; |
| 1253 | |
| 1254 | if (get_ldev(mdev)) { |
Andreas Gruenbacher | 81e8465 | 2010-12-09 15:03:57 +0100 | [diff] [blame] | 1255 | drbd_try_clear_on_disk_bm(mdev, sector, count, false); |
Philipp Reisner | b411b36 | 2009-09-25 16:07:19 -0700 | [diff] [blame] | 1256 | put_ldev(mdev); |
| 1257 | } |
| 1258 | |
| 1259 | /* just wake_up unconditional now, various lc_chaged(), |
| 1260 | * lc_put() in drbd_try_clear_on_disk_bm(). */ |
| 1261 | wake_up = 1; |
| 1262 | } |
| 1263 | spin_unlock_irq(&mdev->al_lock); |
| 1264 | if (wake_up) |
| 1265 | wake_up(&mdev->al_wait); |
| 1266 | } |