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