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