1b411b363SPhilipp Reisner /* 2b411b363SPhilipp Reisner drbd_actlog.c 3b411b363SPhilipp Reisner 4b411b363SPhilipp Reisner This file is part of DRBD by Philipp Reisner and Lars Ellenberg. 5b411b363SPhilipp Reisner 6b411b363SPhilipp Reisner Copyright (C) 2003-2008, LINBIT Information Technologies GmbH. 7b411b363SPhilipp Reisner Copyright (C) 2003-2008, Philipp Reisner <philipp.reisner@linbit.com>. 8b411b363SPhilipp Reisner Copyright (C) 2003-2008, Lars Ellenberg <lars.ellenberg@linbit.com>. 9b411b363SPhilipp Reisner 10b411b363SPhilipp Reisner drbd is free software; you can redistribute it and/or modify 11b411b363SPhilipp Reisner it under the terms of the GNU General Public License as published by 12b411b363SPhilipp Reisner the Free Software Foundation; either version 2, or (at your option) 13b411b363SPhilipp Reisner any later version. 14b411b363SPhilipp Reisner 15b411b363SPhilipp Reisner drbd is distributed in the hope that it will be useful, 16b411b363SPhilipp Reisner but WITHOUT ANY WARRANTY; without even the implied warranty of 17b411b363SPhilipp Reisner MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18b411b363SPhilipp Reisner GNU General Public License for more details. 19b411b363SPhilipp Reisner 20b411b363SPhilipp Reisner You should have received a copy of the GNU General Public License 21b411b363SPhilipp Reisner along with drbd; see the file COPYING. If not, write to 22b411b363SPhilipp Reisner the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 23b411b363SPhilipp Reisner 24b411b363SPhilipp Reisner */ 25b411b363SPhilipp Reisner 26b411b363SPhilipp Reisner #include <linux/slab.h> 27b411b363SPhilipp Reisner #include <linux/drbd.h> 28b411b363SPhilipp Reisner #include "drbd_int.h" 29b411b363SPhilipp Reisner #include "drbd_wrappers.h" 30b411b363SPhilipp Reisner 31b411b363SPhilipp Reisner /* We maintain a trivial check sum in our on disk activity log. 32b411b363SPhilipp Reisner * With that we can ensure correct operation even when the storage 33b411b363SPhilipp Reisner * device might do a partial (last) sector write while loosing power. 34b411b363SPhilipp Reisner */ 35b411b363SPhilipp Reisner struct __packed al_transaction { 36b411b363SPhilipp Reisner u32 magic; 37b411b363SPhilipp Reisner u32 tr_number; 38b411b363SPhilipp Reisner struct __packed { 39b411b363SPhilipp Reisner u32 pos; 40b411b363SPhilipp Reisner u32 extent; } updates[1 + AL_EXTENTS_PT]; 41b411b363SPhilipp Reisner u32 xor_sum; 42b411b363SPhilipp Reisner }; 43b411b363SPhilipp Reisner 44b411b363SPhilipp Reisner struct update_odbm_work { 45b411b363SPhilipp Reisner struct drbd_work w; 46b411b363SPhilipp Reisner unsigned int enr; 47b411b363SPhilipp Reisner }; 48b411b363SPhilipp Reisner 49b411b363SPhilipp Reisner struct update_al_work { 50b411b363SPhilipp Reisner struct drbd_work w; 51b411b363SPhilipp Reisner struct lc_element *al_ext; 52b411b363SPhilipp Reisner struct completion event; 53b411b363SPhilipp Reisner unsigned int enr; 54b411b363SPhilipp Reisner /* if old_enr != LC_FREE, write corresponding bitmap sector, too */ 55b411b363SPhilipp Reisner unsigned int old_enr; 56b411b363SPhilipp Reisner }; 57b411b363SPhilipp Reisner 58b411b363SPhilipp Reisner struct drbd_atodb_wait { 59b411b363SPhilipp Reisner atomic_t count; 60b411b363SPhilipp Reisner struct completion io_done; 61b411b363SPhilipp Reisner struct drbd_conf *mdev; 62b411b363SPhilipp Reisner int error; 63b411b363SPhilipp Reisner }; 64b411b363SPhilipp Reisner 65b411b363SPhilipp Reisner 66b411b363SPhilipp Reisner int w_al_write_transaction(struct drbd_conf *, struct drbd_work *, int); 67b411b363SPhilipp Reisner 68b411b363SPhilipp Reisner static int _drbd_md_sync_page_io(struct drbd_conf *mdev, 69b411b363SPhilipp Reisner struct drbd_backing_dev *bdev, 70b411b363SPhilipp Reisner struct page *page, sector_t sector, 71b411b363SPhilipp Reisner int rw, int size) 72b411b363SPhilipp Reisner { 73b411b363SPhilipp Reisner struct bio *bio; 74b411b363SPhilipp Reisner struct drbd_md_io md_io; 75b411b363SPhilipp Reisner int ok; 76b411b363SPhilipp Reisner 77b411b363SPhilipp Reisner md_io.mdev = mdev; 78b411b363SPhilipp Reisner init_completion(&md_io.event); 79b411b363SPhilipp Reisner md_io.error = 0; 80b411b363SPhilipp Reisner 81b411b363SPhilipp Reisner if ((rw & WRITE) && !test_bit(MD_NO_BARRIER, &mdev->flags)) 82b411b363SPhilipp Reisner rw |= (1 << BIO_RW_BARRIER); 83b411b363SPhilipp Reisner rw |= ((1<<BIO_RW_UNPLUG) | (1<<BIO_RW_SYNCIO)); 84b411b363SPhilipp Reisner 85b411b363SPhilipp Reisner retry: 86b411b363SPhilipp Reisner bio = bio_alloc(GFP_NOIO, 1); 87b411b363SPhilipp Reisner bio->bi_bdev = bdev->md_bdev; 88b411b363SPhilipp Reisner bio->bi_sector = sector; 89b411b363SPhilipp Reisner ok = (bio_add_page(bio, page, size, 0) == size); 90b411b363SPhilipp Reisner if (!ok) 91b411b363SPhilipp Reisner goto out; 92b411b363SPhilipp Reisner bio->bi_private = &md_io; 93b411b363SPhilipp Reisner bio->bi_end_io = drbd_md_io_complete; 94b411b363SPhilipp Reisner bio->bi_rw = rw; 95b411b363SPhilipp Reisner 96b411b363SPhilipp Reisner if (FAULT_ACTIVE(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD)) 97b411b363SPhilipp Reisner bio_endio(bio, -EIO); 98b411b363SPhilipp Reisner else 99b411b363SPhilipp Reisner submit_bio(rw, bio); 100b411b363SPhilipp Reisner wait_for_completion(&md_io.event); 101b411b363SPhilipp Reisner ok = bio_flagged(bio, BIO_UPTODATE) && md_io.error == 0; 102b411b363SPhilipp Reisner 103b411b363SPhilipp Reisner /* check for unsupported barrier op. 104b411b363SPhilipp Reisner * would rather check on EOPNOTSUPP, but that is not reliable. 105b411b363SPhilipp Reisner * don't try again for ANY return value != 0 */ 106b411b363SPhilipp Reisner if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER) && !ok)) { 107b411b363SPhilipp Reisner /* Try again with no barrier */ 108b411b363SPhilipp Reisner dev_warn(DEV, "Barriers not supported on meta data device - disabling\n"); 109b411b363SPhilipp Reisner set_bit(MD_NO_BARRIER, &mdev->flags); 110b411b363SPhilipp Reisner rw &= ~(1 << BIO_RW_BARRIER); 111b411b363SPhilipp Reisner bio_put(bio); 112b411b363SPhilipp Reisner goto retry; 113b411b363SPhilipp Reisner } 114b411b363SPhilipp Reisner out: 115b411b363SPhilipp Reisner bio_put(bio); 116b411b363SPhilipp Reisner return ok; 117b411b363SPhilipp Reisner } 118b411b363SPhilipp Reisner 119b411b363SPhilipp Reisner int drbd_md_sync_page_io(struct drbd_conf *mdev, struct drbd_backing_dev *bdev, 120b411b363SPhilipp Reisner sector_t sector, int rw) 121b411b363SPhilipp Reisner { 122b411b363SPhilipp Reisner int logical_block_size, mask, ok; 123b411b363SPhilipp Reisner int offset = 0; 124b411b363SPhilipp Reisner struct page *iop = mdev->md_io_page; 125b411b363SPhilipp Reisner 126b411b363SPhilipp Reisner D_ASSERT(mutex_is_locked(&mdev->md_io_mutex)); 127b411b363SPhilipp Reisner 128b411b363SPhilipp Reisner BUG_ON(!bdev->md_bdev); 129b411b363SPhilipp Reisner 130b411b363SPhilipp Reisner logical_block_size = bdev_logical_block_size(bdev->md_bdev); 131b411b363SPhilipp Reisner if (logical_block_size == 0) 132b411b363SPhilipp Reisner logical_block_size = MD_SECTOR_SIZE; 133b411b363SPhilipp Reisner 134b411b363SPhilipp Reisner /* in case logical_block_size != 512 [ s390 only? ] */ 135b411b363SPhilipp Reisner if (logical_block_size != MD_SECTOR_SIZE) { 136b411b363SPhilipp Reisner mask = (logical_block_size / MD_SECTOR_SIZE) - 1; 137b411b363SPhilipp Reisner D_ASSERT(mask == 1 || mask == 3 || mask == 7); 138b411b363SPhilipp Reisner D_ASSERT(logical_block_size == (mask+1) * MD_SECTOR_SIZE); 139b411b363SPhilipp Reisner offset = sector & mask; 140b411b363SPhilipp Reisner sector = sector & ~mask; 141b411b363SPhilipp Reisner iop = mdev->md_io_tmpp; 142b411b363SPhilipp Reisner 143b411b363SPhilipp Reisner if (rw & WRITE) { 144b411b363SPhilipp Reisner /* these are GFP_KERNEL pages, pre-allocated 145b411b363SPhilipp Reisner * on device initialization */ 146b411b363SPhilipp Reisner void *p = page_address(mdev->md_io_page); 147b411b363SPhilipp Reisner void *hp = page_address(mdev->md_io_tmpp); 148b411b363SPhilipp Reisner 149b411b363SPhilipp Reisner ok = _drbd_md_sync_page_io(mdev, bdev, iop, sector, 150b411b363SPhilipp Reisner READ, logical_block_size); 151b411b363SPhilipp Reisner 152b411b363SPhilipp Reisner if (unlikely(!ok)) { 153b411b363SPhilipp Reisner dev_err(DEV, "drbd_md_sync_page_io(,%llus," 154b411b363SPhilipp Reisner "READ [logical_block_size!=512]) failed!\n", 155b411b363SPhilipp Reisner (unsigned long long)sector); 156b411b363SPhilipp Reisner return 0; 157b411b363SPhilipp Reisner } 158b411b363SPhilipp Reisner 159b411b363SPhilipp Reisner memcpy(hp + offset*MD_SECTOR_SIZE, p, MD_SECTOR_SIZE); 160b411b363SPhilipp Reisner } 161b411b363SPhilipp Reisner } 162b411b363SPhilipp Reisner 163b411b363SPhilipp Reisner if (sector < drbd_md_first_sector(bdev) || 164b411b363SPhilipp Reisner sector > drbd_md_last_sector(bdev)) 165b411b363SPhilipp Reisner dev_alert(DEV, "%s [%d]:%s(,%llus,%s) out of range md access!\n", 166b411b363SPhilipp Reisner current->comm, current->pid, __func__, 167b411b363SPhilipp Reisner (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ"); 168b411b363SPhilipp Reisner 169b411b363SPhilipp Reisner ok = _drbd_md_sync_page_io(mdev, bdev, iop, sector, rw, logical_block_size); 170b411b363SPhilipp Reisner if (unlikely(!ok)) { 171b411b363SPhilipp Reisner dev_err(DEV, "drbd_md_sync_page_io(,%llus,%s) failed!\n", 172b411b363SPhilipp Reisner (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ"); 173b411b363SPhilipp Reisner return 0; 174b411b363SPhilipp Reisner } 175b411b363SPhilipp Reisner 176b411b363SPhilipp Reisner if (logical_block_size != MD_SECTOR_SIZE && !(rw & WRITE)) { 177b411b363SPhilipp Reisner void *p = page_address(mdev->md_io_page); 178b411b363SPhilipp Reisner void *hp = page_address(mdev->md_io_tmpp); 179b411b363SPhilipp Reisner 180b411b363SPhilipp Reisner memcpy(p, hp + offset*MD_SECTOR_SIZE, MD_SECTOR_SIZE); 181b411b363SPhilipp Reisner } 182b411b363SPhilipp Reisner 183b411b363SPhilipp Reisner return ok; 184b411b363SPhilipp Reisner } 185b411b363SPhilipp Reisner 186b411b363SPhilipp Reisner static struct lc_element *_al_get(struct drbd_conf *mdev, unsigned int enr) 187b411b363SPhilipp Reisner { 188b411b363SPhilipp Reisner struct lc_element *al_ext; 189b411b363SPhilipp Reisner struct lc_element *tmp; 190b411b363SPhilipp Reisner unsigned long al_flags = 0; 191b411b363SPhilipp Reisner 192b411b363SPhilipp Reisner spin_lock_irq(&mdev->al_lock); 193b411b363SPhilipp Reisner tmp = lc_find(mdev->resync, enr/AL_EXT_PER_BM_SECT); 194b411b363SPhilipp Reisner if (unlikely(tmp != NULL)) { 195b411b363SPhilipp Reisner struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce); 196b411b363SPhilipp Reisner if (test_bit(BME_NO_WRITES, &bm_ext->flags)) { 197b411b363SPhilipp Reisner spin_unlock_irq(&mdev->al_lock); 198b411b363SPhilipp Reisner return NULL; 199b411b363SPhilipp Reisner } 200b411b363SPhilipp Reisner } 201b411b363SPhilipp Reisner al_ext = lc_get(mdev->act_log, enr); 202b411b363SPhilipp Reisner al_flags = mdev->act_log->flags; 203b411b363SPhilipp Reisner spin_unlock_irq(&mdev->al_lock); 204b411b363SPhilipp Reisner 205b411b363SPhilipp Reisner /* 206b411b363SPhilipp Reisner if (!al_ext) { 207b411b363SPhilipp Reisner if (al_flags & LC_STARVING) 208b411b363SPhilipp Reisner dev_warn(DEV, "Have to wait for LRU element (AL too small?)\n"); 209b411b363SPhilipp Reisner if (al_flags & LC_DIRTY) 210b411b363SPhilipp Reisner dev_warn(DEV, "Ongoing AL update (AL device too slow?)\n"); 211b411b363SPhilipp Reisner } 212b411b363SPhilipp Reisner */ 213b411b363SPhilipp Reisner 214b411b363SPhilipp Reisner return al_ext; 215b411b363SPhilipp Reisner } 216b411b363SPhilipp Reisner 217b411b363SPhilipp Reisner void drbd_al_begin_io(struct drbd_conf *mdev, sector_t sector) 218b411b363SPhilipp Reisner { 219b411b363SPhilipp Reisner unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9)); 220b411b363SPhilipp Reisner struct lc_element *al_ext; 221b411b363SPhilipp Reisner struct update_al_work al_work; 222b411b363SPhilipp Reisner 223b411b363SPhilipp Reisner D_ASSERT(atomic_read(&mdev->local_cnt) > 0); 224b411b363SPhilipp Reisner 225b411b363SPhilipp Reisner wait_event(mdev->al_wait, (al_ext = _al_get(mdev, enr))); 226b411b363SPhilipp Reisner 227b411b363SPhilipp Reisner if (al_ext->lc_number != enr) { 228b411b363SPhilipp Reisner /* drbd_al_write_transaction(mdev,al_ext,enr); 229b411b363SPhilipp Reisner * recurses into generic_make_request(), which 230b411b363SPhilipp Reisner * disallows recursion, bios being serialized on the 231b411b363SPhilipp Reisner * current->bio_tail list now. 232b411b363SPhilipp Reisner * we have to delegate updates to the activity log 233b411b363SPhilipp Reisner * to the worker thread. */ 234b411b363SPhilipp Reisner init_completion(&al_work.event); 235b411b363SPhilipp Reisner al_work.al_ext = al_ext; 236b411b363SPhilipp Reisner al_work.enr = enr; 237b411b363SPhilipp Reisner al_work.old_enr = al_ext->lc_number; 238b411b363SPhilipp Reisner al_work.w.cb = w_al_write_transaction; 239b411b363SPhilipp Reisner drbd_queue_work_front(&mdev->data.work, &al_work.w); 240b411b363SPhilipp Reisner wait_for_completion(&al_work.event); 241b411b363SPhilipp Reisner 242b411b363SPhilipp Reisner mdev->al_writ_cnt++; 243b411b363SPhilipp Reisner 244b411b363SPhilipp Reisner spin_lock_irq(&mdev->al_lock); 245b411b363SPhilipp Reisner lc_changed(mdev->act_log, al_ext); 246b411b363SPhilipp Reisner spin_unlock_irq(&mdev->al_lock); 247b411b363SPhilipp Reisner wake_up(&mdev->al_wait); 248b411b363SPhilipp Reisner } 249b411b363SPhilipp Reisner } 250b411b363SPhilipp Reisner 251b411b363SPhilipp Reisner void drbd_al_complete_io(struct drbd_conf *mdev, sector_t sector) 252b411b363SPhilipp Reisner { 253b411b363SPhilipp Reisner unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9)); 254b411b363SPhilipp Reisner struct lc_element *extent; 255b411b363SPhilipp Reisner unsigned long flags; 256b411b363SPhilipp Reisner 257b411b363SPhilipp Reisner spin_lock_irqsave(&mdev->al_lock, flags); 258b411b363SPhilipp Reisner 259b411b363SPhilipp Reisner extent = lc_find(mdev->act_log, enr); 260b411b363SPhilipp Reisner 261b411b363SPhilipp Reisner if (!extent) { 262b411b363SPhilipp Reisner spin_unlock_irqrestore(&mdev->al_lock, flags); 263b411b363SPhilipp Reisner dev_err(DEV, "al_complete_io() called on inactive extent %u\n", enr); 264b411b363SPhilipp Reisner return; 265b411b363SPhilipp Reisner } 266b411b363SPhilipp Reisner 267b411b363SPhilipp Reisner if (lc_put(mdev->act_log, extent) == 0) 268b411b363SPhilipp Reisner wake_up(&mdev->al_wait); 269b411b363SPhilipp Reisner 270b411b363SPhilipp Reisner spin_unlock_irqrestore(&mdev->al_lock, flags); 271b411b363SPhilipp Reisner } 272b411b363SPhilipp Reisner 273b411b363SPhilipp Reisner int 274b411b363SPhilipp Reisner w_al_write_transaction(struct drbd_conf *mdev, struct drbd_work *w, int unused) 275b411b363SPhilipp Reisner { 276b411b363SPhilipp Reisner struct update_al_work *aw = container_of(w, struct update_al_work, w); 277b411b363SPhilipp Reisner struct lc_element *updated = aw->al_ext; 278b411b363SPhilipp Reisner const unsigned int new_enr = aw->enr; 279b411b363SPhilipp Reisner const unsigned int evicted = aw->old_enr; 280b411b363SPhilipp Reisner struct al_transaction *buffer; 281b411b363SPhilipp Reisner sector_t sector; 282b411b363SPhilipp Reisner int i, n, mx; 283b411b363SPhilipp Reisner unsigned int extent_nr; 284b411b363SPhilipp Reisner u32 xor_sum = 0; 285b411b363SPhilipp Reisner 286b411b363SPhilipp Reisner if (!get_ldev(mdev)) { 287b411b363SPhilipp Reisner dev_err(DEV, "get_ldev() failed in w_al_write_transaction\n"); 288b411b363SPhilipp Reisner complete(&((struct update_al_work *)w)->event); 289b411b363SPhilipp Reisner return 1; 290b411b363SPhilipp Reisner } 291b411b363SPhilipp Reisner /* do we have to do a bitmap write, first? 292b411b363SPhilipp Reisner * TODO reduce maximum latency: 293b411b363SPhilipp Reisner * submit both bios, then wait for both, 294b411b363SPhilipp Reisner * instead of doing two synchronous sector writes. */ 295b411b363SPhilipp Reisner if (mdev->state.conn < C_CONNECTED && evicted != LC_FREE) 296b411b363SPhilipp Reisner drbd_bm_write_sect(mdev, evicted/AL_EXT_PER_BM_SECT); 297b411b363SPhilipp Reisner 298b411b363SPhilipp Reisner mutex_lock(&mdev->md_io_mutex); /* protects md_io_page, al_tr_cycle, ... */ 299b411b363SPhilipp Reisner buffer = (struct al_transaction *)page_address(mdev->md_io_page); 300b411b363SPhilipp Reisner 301b411b363SPhilipp Reisner buffer->magic = __constant_cpu_to_be32(DRBD_MAGIC); 302b411b363SPhilipp Reisner buffer->tr_number = cpu_to_be32(mdev->al_tr_number); 303b411b363SPhilipp Reisner 304b411b363SPhilipp Reisner n = lc_index_of(mdev->act_log, updated); 305b411b363SPhilipp Reisner 306b411b363SPhilipp Reisner buffer->updates[0].pos = cpu_to_be32(n); 307b411b363SPhilipp Reisner buffer->updates[0].extent = cpu_to_be32(new_enr); 308b411b363SPhilipp Reisner 309b411b363SPhilipp Reisner xor_sum ^= new_enr; 310b411b363SPhilipp Reisner 311b411b363SPhilipp Reisner mx = min_t(int, AL_EXTENTS_PT, 312b411b363SPhilipp Reisner mdev->act_log->nr_elements - mdev->al_tr_cycle); 313b411b363SPhilipp Reisner for (i = 0; i < mx; i++) { 314b411b363SPhilipp Reisner unsigned idx = mdev->al_tr_cycle + i; 315b411b363SPhilipp Reisner extent_nr = lc_element_by_index(mdev->act_log, idx)->lc_number; 316b411b363SPhilipp Reisner buffer->updates[i+1].pos = cpu_to_be32(idx); 317b411b363SPhilipp Reisner buffer->updates[i+1].extent = cpu_to_be32(extent_nr); 318b411b363SPhilipp Reisner xor_sum ^= extent_nr; 319b411b363SPhilipp Reisner } 320b411b363SPhilipp Reisner for (; i < AL_EXTENTS_PT; i++) { 321b411b363SPhilipp Reisner buffer->updates[i+1].pos = __constant_cpu_to_be32(-1); 322b411b363SPhilipp Reisner buffer->updates[i+1].extent = __constant_cpu_to_be32(LC_FREE); 323b411b363SPhilipp Reisner xor_sum ^= LC_FREE; 324b411b363SPhilipp Reisner } 325b411b363SPhilipp Reisner mdev->al_tr_cycle += AL_EXTENTS_PT; 326b411b363SPhilipp Reisner if (mdev->al_tr_cycle >= mdev->act_log->nr_elements) 327b411b363SPhilipp Reisner mdev->al_tr_cycle = 0; 328b411b363SPhilipp Reisner 329b411b363SPhilipp Reisner buffer->xor_sum = cpu_to_be32(xor_sum); 330b411b363SPhilipp Reisner 331b411b363SPhilipp Reisner sector = mdev->ldev->md.md_offset 332b411b363SPhilipp Reisner + mdev->ldev->md.al_offset + mdev->al_tr_pos; 333b411b363SPhilipp Reisner 334b411b363SPhilipp Reisner if (!drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE)) 335b411b363SPhilipp Reisner drbd_chk_io_error(mdev, 1, TRUE); 336b411b363SPhilipp Reisner 337b411b363SPhilipp Reisner if (++mdev->al_tr_pos > 338b411b363SPhilipp Reisner div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT)) 339b411b363SPhilipp Reisner mdev->al_tr_pos = 0; 340b411b363SPhilipp Reisner 341b411b363SPhilipp Reisner D_ASSERT(mdev->al_tr_pos < MD_AL_MAX_SIZE); 342b411b363SPhilipp Reisner mdev->al_tr_number++; 343b411b363SPhilipp Reisner 344b411b363SPhilipp Reisner mutex_unlock(&mdev->md_io_mutex); 345b411b363SPhilipp Reisner 346b411b363SPhilipp Reisner complete(&((struct update_al_work *)w)->event); 347b411b363SPhilipp Reisner put_ldev(mdev); 348b411b363SPhilipp Reisner 349b411b363SPhilipp Reisner return 1; 350b411b363SPhilipp Reisner } 351b411b363SPhilipp Reisner 352b411b363SPhilipp Reisner /** 353b411b363SPhilipp Reisner * drbd_al_read_tr() - Read a single transaction from the on disk activity log 354b411b363SPhilipp Reisner * @mdev: DRBD device. 355b411b363SPhilipp Reisner * @bdev: Block device to read form. 356b411b363SPhilipp Reisner * @b: pointer to an al_transaction. 357b411b363SPhilipp Reisner * @index: On disk slot of the transaction to read. 358b411b363SPhilipp Reisner * 359b411b363SPhilipp Reisner * Returns -1 on IO error, 0 on checksum error and 1 upon success. 360b411b363SPhilipp Reisner */ 361b411b363SPhilipp Reisner static int drbd_al_read_tr(struct drbd_conf *mdev, 362b411b363SPhilipp Reisner struct drbd_backing_dev *bdev, 363b411b363SPhilipp Reisner struct al_transaction *b, 364b411b363SPhilipp Reisner int index) 365b411b363SPhilipp Reisner { 366b411b363SPhilipp Reisner sector_t sector; 367b411b363SPhilipp Reisner int rv, i; 368b411b363SPhilipp Reisner u32 xor_sum = 0; 369b411b363SPhilipp Reisner 370b411b363SPhilipp Reisner sector = bdev->md.md_offset + bdev->md.al_offset + index; 371b411b363SPhilipp Reisner 372b411b363SPhilipp Reisner /* Dont process error normally, 373b411b363SPhilipp Reisner * as this is done before disk is attached! */ 374b411b363SPhilipp Reisner if (!drbd_md_sync_page_io(mdev, bdev, sector, READ)) 375b411b363SPhilipp Reisner return -1; 376b411b363SPhilipp Reisner 377b411b363SPhilipp Reisner rv = (be32_to_cpu(b->magic) == DRBD_MAGIC); 378b411b363SPhilipp Reisner 379b411b363SPhilipp Reisner for (i = 0; i < AL_EXTENTS_PT + 1; i++) 380b411b363SPhilipp Reisner xor_sum ^= be32_to_cpu(b->updates[i].extent); 381b411b363SPhilipp Reisner rv &= (xor_sum == be32_to_cpu(b->xor_sum)); 382b411b363SPhilipp Reisner 383b411b363SPhilipp Reisner return rv; 384b411b363SPhilipp Reisner } 385b411b363SPhilipp Reisner 386b411b363SPhilipp Reisner /** 387b411b363SPhilipp Reisner * drbd_al_read_log() - Restores the activity log from its on disk representation. 388b411b363SPhilipp Reisner * @mdev: DRBD device. 389b411b363SPhilipp Reisner * @bdev: Block device to read form. 390b411b363SPhilipp Reisner * 391b411b363SPhilipp Reisner * Returns 1 on success, returns 0 when reading the log failed due to IO errors. 392b411b363SPhilipp Reisner */ 393b411b363SPhilipp Reisner int drbd_al_read_log(struct drbd_conf *mdev, struct drbd_backing_dev *bdev) 394b411b363SPhilipp Reisner { 395b411b363SPhilipp Reisner struct al_transaction *buffer; 396b411b363SPhilipp Reisner int i; 397b411b363SPhilipp Reisner int rv; 398b411b363SPhilipp Reisner int mx; 399b411b363SPhilipp Reisner int active_extents = 0; 400b411b363SPhilipp Reisner int transactions = 0; 401b411b363SPhilipp Reisner int found_valid = 0; 402b411b363SPhilipp Reisner int from = 0; 403b411b363SPhilipp Reisner int to = 0; 404b411b363SPhilipp Reisner u32 from_tnr = 0; 405b411b363SPhilipp Reisner u32 to_tnr = 0; 406b411b363SPhilipp Reisner u32 cnr; 407b411b363SPhilipp Reisner 408b411b363SPhilipp Reisner mx = div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT); 409b411b363SPhilipp Reisner 410b411b363SPhilipp Reisner /* lock out all other meta data io for now, 411b411b363SPhilipp Reisner * and make sure the page is mapped. 412b411b363SPhilipp Reisner */ 413b411b363SPhilipp Reisner mutex_lock(&mdev->md_io_mutex); 414b411b363SPhilipp Reisner buffer = page_address(mdev->md_io_page); 415b411b363SPhilipp Reisner 416b411b363SPhilipp Reisner /* Find the valid transaction in the log */ 417b411b363SPhilipp Reisner for (i = 0; i <= mx; i++) { 418b411b363SPhilipp Reisner rv = drbd_al_read_tr(mdev, bdev, buffer, i); 419b411b363SPhilipp Reisner if (rv == 0) 420b411b363SPhilipp Reisner continue; 421b411b363SPhilipp Reisner if (rv == -1) { 422b411b363SPhilipp Reisner mutex_unlock(&mdev->md_io_mutex); 423b411b363SPhilipp Reisner return 0; 424b411b363SPhilipp Reisner } 425b411b363SPhilipp Reisner cnr = be32_to_cpu(buffer->tr_number); 426b411b363SPhilipp Reisner 427b411b363SPhilipp Reisner if (++found_valid == 1) { 428b411b363SPhilipp Reisner from = i; 429b411b363SPhilipp Reisner to = i; 430b411b363SPhilipp Reisner from_tnr = cnr; 431b411b363SPhilipp Reisner to_tnr = cnr; 432b411b363SPhilipp Reisner continue; 433b411b363SPhilipp Reisner } 434b411b363SPhilipp Reisner if ((int)cnr - (int)from_tnr < 0) { 435b411b363SPhilipp Reisner D_ASSERT(from_tnr - cnr + i - from == mx+1); 436b411b363SPhilipp Reisner from = i; 437b411b363SPhilipp Reisner from_tnr = cnr; 438b411b363SPhilipp Reisner } 439b411b363SPhilipp Reisner if ((int)cnr - (int)to_tnr > 0) { 440b411b363SPhilipp Reisner D_ASSERT(cnr - to_tnr == i - to); 441b411b363SPhilipp Reisner to = i; 442b411b363SPhilipp Reisner to_tnr = cnr; 443b411b363SPhilipp Reisner } 444b411b363SPhilipp Reisner } 445b411b363SPhilipp Reisner 446b411b363SPhilipp Reisner if (!found_valid) { 447b411b363SPhilipp Reisner dev_warn(DEV, "No usable activity log found.\n"); 448b411b363SPhilipp Reisner mutex_unlock(&mdev->md_io_mutex); 449b411b363SPhilipp Reisner return 1; 450b411b363SPhilipp Reisner } 451b411b363SPhilipp Reisner 452b411b363SPhilipp Reisner /* Read the valid transactions. 453b411b363SPhilipp Reisner * dev_info(DEV, "Reading from %d to %d.\n",from,to); */ 454b411b363SPhilipp Reisner i = from; 455b411b363SPhilipp Reisner while (1) { 456b411b363SPhilipp Reisner int j, pos; 457b411b363SPhilipp Reisner unsigned int extent_nr; 458b411b363SPhilipp Reisner unsigned int trn; 459b411b363SPhilipp Reisner 460b411b363SPhilipp Reisner rv = drbd_al_read_tr(mdev, bdev, buffer, i); 461b411b363SPhilipp Reisner ERR_IF(rv == 0) goto cancel; 462b411b363SPhilipp Reisner if (rv == -1) { 463b411b363SPhilipp Reisner mutex_unlock(&mdev->md_io_mutex); 464b411b363SPhilipp Reisner return 0; 465b411b363SPhilipp Reisner } 466b411b363SPhilipp Reisner 467b411b363SPhilipp Reisner trn = be32_to_cpu(buffer->tr_number); 468b411b363SPhilipp Reisner 469b411b363SPhilipp Reisner spin_lock_irq(&mdev->al_lock); 470b411b363SPhilipp Reisner 471b411b363SPhilipp Reisner /* This loop runs backwards because in the cyclic 472b411b363SPhilipp Reisner elements there might be an old version of the 473b411b363SPhilipp Reisner updated element (in slot 0). So the element in slot 0 474b411b363SPhilipp Reisner can overwrite old versions. */ 475b411b363SPhilipp Reisner for (j = AL_EXTENTS_PT; j >= 0; j--) { 476b411b363SPhilipp Reisner pos = be32_to_cpu(buffer->updates[j].pos); 477b411b363SPhilipp Reisner extent_nr = be32_to_cpu(buffer->updates[j].extent); 478b411b363SPhilipp Reisner 479b411b363SPhilipp Reisner if (extent_nr == LC_FREE) 480b411b363SPhilipp Reisner continue; 481b411b363SPhilipp Reisner 482b411b363SPhilipp Reisner lc_set(mdev->act_log, extent_nr, pos); 483b411b363SPhilipp Reisner active_extents++; 484b411b363SPhilipp Reisner } 485b411b363SPhilipp Reisner spin_unlock_irq(&mdev->al_lock); 486b411b363SPhilipp Reisner 487b411b363SPhilipp Reisner transactions++; 488b411b363SPhilipp Reisner 489b411b363SPhilipp Reisner cancel: 490b411b363SPhilipp Reisner if (i == to) 491b411b363SPhilipp Reisner break; 492b411b363SPhilipp Reisner i++; 493b411b363SPhilipp Reisner if (i > mx) 494b411b363SPhilipp Reisner i = 0; 495b411b363SPhilipp Reisner } 496b411b363SPhilipp Reisner 497b411b363SPhilipp Reisner mdev->al_tr_number = to_tnr+1; 498b411b363SPhilipp Reisner mdev->al_tr_pos = to; 499b411b363SPhilipp Reisner if (++mdev->al_tr_pos > 500b411b363SPhilipp Reisner div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT)) 501b411b363SPhilipp Reisner mdev->al_tr_pos = 0; 502b411b363SPhilipp Reisner 503b411b363SPhilipp Reisner /* ok, we are done with it */ 504b411b363SPhilipp Reisner mutex_unlock(&mdev->md_io_mutex); 505b411b363SPhilipp Reisner 506b411b363SPhilipp Reisner dev_info(DEV, "Found %d transactions (%d active extents) in activity log.\n", 507b411b363SPhilipp Reisner transactions, active_extents); 508b411b363SPhilipp Reisner 509b411b363SPhilipp Reisner return 1; 510b411b363SPhilipp Reisner } 511b411b363SPhilipp Reisner 512b411b363SPhilipp Reisner static void atodb_endio(struct bio *bio, int error) 513b411b363SPhilipp Reisner { 514b411b363SPhilipp Reisner struct drbd_atodb_wait *wc = bio->bi_private; 515b411b363SPhilipp Reisner struct drbd_conf *mdev = wc->mdev; 516b411b363SPhilipp Reisner struct page *page; 517b411b363SPhilipp Reisner int uptodate = bio_flagged(bio, BIO_UPTODATE); 518b411b363SPhilipp Reisner 519b411b363SPhilipp Reisner /* strange behavior of some lower level drivers... 520b411b363SPhilipp Reisner * fail the request by clearing the uptodate flag, 521b411b363SPhilipp Reisner * but do not return any error?! */ 522b411b363SPhilipp Reisner if (!error && !uptodate) 523b411b363SPhilipp Reisner error = -EIO; 524b411b363SPhilipp Reisner 525b411b363SPhilipp Reisner drbd_chk_io_error(mdev, error, TRUE); 526b411b363SPhilipp Reisner if (error && wc->error == 0) 527b411b363SPhilipp Reisner wc->error = error; 528b411b363SPhilipp Reisner 529b411b363SPhilipp Reisner if (atomic_dec_and_test(&wc->count)) 530b411b363SPhilipp Reisner complete(&wc->io_done); 531b411b363SPhilipp Reisner 532b411b363SPhilipp Reisner page = bio->bi_io_vec[0].bv_page; 533b411b363SPhilipp Reisner put_page(page); 534b411b363SPhilipp Reisner bio_put(bio); 535b411b363SPhilipp Reisner mdev->bm_writ_cnt++; 536b411b363SPhilipp Reisner put_ldev(mdev); 537b411b363SPhilipp Reisner } 538b411b363SPhilipp Reisner 539b411b363SPhilipp Reisner #define S2W(s) ((s)<<(BM_EXT_SHIFT-BM_BLOCK_SHIFT-LN2_BPL)) 540b411b363SPhilipp Reisner /* activity log to on disk bitmap -- prepare bio unless that sector 541b411b363SPhilipp Reisner * is already covered by previously prepared bios */ 542b411b363SPhilipp Reisner static int atodb_prepare_unless_covered(struct drbd_conf *mdev, 543b411b363SPhilipp Reisner struct bio **bios, 544b411b363SPhilipp Reisner unsigned int enr, 545b411b363SPhilipp Reisner struct drbd_atodb_wait *wc) __must_hold(local) 546b411b363SPhilipp Reisner { 547b411b363SPhilipp Reisner struct bio *bio; 548b411b363SPhilipp Reisner struct page *page; 549b411b363SPhilipp Reisner sector_t on_disk_sector = enr + mdev->ldev->md.md_offset 550b411b363SPhilipp Reisner + mdev->ldev->md.bm_offset; 551b411b363SPhilipp Reisner unsigned int page_offset = PAGE_SIZE; 552b411b363SPhilipp Reisner int offset; 553b411b363SPhilipp Reisner int i = 0; 554b411b363SPhilipp Reisner int err = -ENOMEM; 555b411b363SPhilipp Reisner 556b411b363SPhilipp Reisner /* Check if that enr is already covered by an already created bio. 557b411b363SPhilipp Reisner * Caution, bios[] is not NULL terminated, 558b411b363SPhilipp Reisner * but only initialized to all NULL. 559b411b363SPhilipp Reisner * For completely scattered activity log, 560b411b363SPhilipp Reisner * the last invocation iterates over all bios, 561b411b363SPhilipp Reisner * and finds the last NULL entry. 562b411b363SPhilipp Reisner */ 563b411b363SPhilipp Reisner while ((bio = bios[i])) { 564b411b363SPhilipp Reisner if (bio->bi_sector == on_disk_sector) 565b411b363SPhilipp Reisner return 0; 566b411b363SPhilipp Reisner i++; 567b411b363SPhilipp Reisner } 568b411b363SPhilipp Reisner /* bios[i] == NULL, the next not yet used slot */ 569b411b363SPhilipp Reisner 570b411b363SPhilipp Reisner /* GFP_KERNEL, we are not in the write-out path */ 571b411b363SPhilipp Reisner bio = bio_alloc(GFP_KERNEL, 1); 572b411b363SPhilipp Reisner if (bio == NULL) 573b411b363SPhilipp Reisner return -ENOMEM; 574b411b363SPhilipp Reisner 575b411b363SPhilipp Reisner if (i > 0) { 576b411b363SPhilipp Reisner const struct bio_vec *prev_bv = bios[i-1]->bi_io_vec; 577b411b363SPhilipp Reisner page_offset = prev_bv->bv_offset + prev_bv->bv_len; 578b411b363SPhilipp Reisner page = prev_bv->bv_page; 579b411b363SPhilipp Reisner } 580b411b363SPhilipp Reisner if (page_offset == PAGE_SIZE) { 581b411b363SPhilipp Reisner page = alloc_page(__GFP_HIGHMEM); 582b411b363SPhilipp Reisner if (page == NULL) 583b411b363SPhilipp Reisner goto out_bio_put; 584b411b363SPhilipp Reisner page_offset = 0; 585b411b363SPhilipp Reisner } else { 586b411b363SPhilipp Reisner get_page(page); 587b411b363SPhilipp Reisner } 588b411b363SPhilipp Reisner 589b411b363SPhilipp Reisner offset = S2W(enr); 590b411b363SPhilipp Reisner drbd_bm_get_lel(mdev, offset, 591b411b363SPhilipp Reisner min_t(size_t, S2W(1), drbd_bm_words(mdev) - offset), 592b411b363SPhilipp Reisner kmap(page) + page_offset); 593b411b363SPhilipp Reisner kunmap(page); 594b411b363SPhilipp Reisner 595b411b363SPhilipp Reisner bio->bi_private = wc; 596b411b363SPhilipp Reisner bio->bi_end_io = atodb_endio; 597b411b363SPhilipp Reisner bio->bi_bdev = mdev->ldev->md_bdev; 598b411b363SPhilipp Reisner bio->bi_sector = on_disk_sector; 599b411b363SPhilipp Reisner 600b411b363SPhilipp Reisner if (bio_add_page(bio, page, MD_SECTOR_SIZE, page_offset) != MD_SECTOR_SIZE) 601b411b363SPhilipp Reisner goto out_put_page; 602b411b363SPhilipp Reisner 603b411b363SPhilipp Reisner atomic_inc(&wc->count); 604b411b363SPhilipp Reisner /* we already know that we may do this... 605b411b363SPhilipp Reisner * get_ldev_if_state(mdev,D_ATTACHING); 606b411b363SPhilipp Reisner * just get the extra reference, so that the local_cnt reflects 607b411b363SPhilipp Reisner * the number of pending IO requests DRBD at its backing device. 608b411b363SPhilipp Reisner */ 609b411b363SPhilipp Reisner atomic_inc(&mdev->local_cnt); 610b411b363SPhilipp Reisner 611b411b363SPhilipp Reisner bios[i] = bio; 612b411b363SPhilipp Reisner 613b411b363SPhilipp Reisner return 0; 614b411b363SPhilipp Reisner 615b411b363SPhilipp Reisner out_put_page: 616b411b363SPhilipp Reisner err = -EINVAL; 617b411b363SPhilipp Reisner put_page(page); 618b411b363SPhilipp Reisner out_bio_put: 619b411b363SPhilipp Reisner bio_put(bio); 620b411b363SPhilipp Reisner return err; 621b411b363SPhilipp Reisner } 622b411b363SPhilipp Reisner 623b411b363SPhilipp Reisner /** 624b411b363SPhilipp Reisner * drbd_al_to_on_disk_bm() - * Writes bitmap parts covered by active AL extents 625b411b363SPhilipp Reisner * @mdev: DRBD device. 626b411b363SPhilipp Reisner * 627b411b363SPhilipp Reisner * Called when we detach (unconfigure) local storage, 628b411b363SPhilipp Reisner * or when we go from R_PRIMARY to R_SECONDARY role. 629b411b363SPhilipp Reisner */ 630b411b363SPhilipp Reisner void drbd_al_to_on_disk_bm(struct drbd_conf *mdev) 631b411b363SPhilipp Reisner { 632b411b363SPhilipp Reisner int i, nr_elements; 633b411b363SPhilipp Reisner unsigned int enr; 634b411b363SPhilipp Reisner struct bio **bios; 635b411b363SPhilipp Reisner struct drbd_atodb_wait wc; 636b411b363SPhilipp Reisner 637b411b363SPhilipp Reisner ERR_IF (!get_ldev_if_state(mdev, D_ATTACHING)) 638b411b363SPhilipp Reisner return; /* sorry, I don't have any act_log etc... */ 639b411b363SPhilipp Reisner 640b411b363SPhilipp Reisner wait_event(mdev->al_wait, lc_try_lock(mdev->act_log)); 641b411b363SPhilipp Reisner 642b411b363SPhilipp Reisner nr_elements = mdev->act_log->nr_elements; 643b411b363SPhilipp Reisner 644b411b363SPhilipp Reisner /* GFP_KERNEL, we are not in anyone's write-out path */ 645b411b363SPhilipp Reisner bios = kzalloc(sizeof(struct bio *) * nr_elements, GFP_KERNEL); 646b411b363SPhilipp Reisner if (!bios) 647b411b363SPhilipp Reisner goto submit_one_by_one; 648b411b363SPhilipp Reisner 649b411b363SPhilipp Reisner atomic_set(&wc.count, 0); 650b411b363SPhilipp Reisner init_completion(&wc.io_done); 651b411b363SPhilipp Reisner wc.mdev = mdev; 652b411b363SPhilipp Reisner wc.error = 0; 653b411b363SPhilipp Reisner 654b411b363SPhilipp Reisner for (i = 0; i < nr_elements; i++) { 655b411b363SPhilipp Reisner enr = lc_element_by_index(mdev->act_log, i)->lc_number; 656b411b363SPhilipp Reisner if (enr == LC_FREE) 657b411b363SPhilipp Reisner continue; 658b411b363SPhilipp Reisner /* next statement also does atomic_inc wc.count and local_cnt */ 659b411b363SPhilipp Reisner if (atodb_prepare_unless_covered(mdev, bios, 660b411b363SPhilipp Reisner enr/AL_EXT_PER_BM_SECT, 661b411b363SPhilipp Reisner &wc)) 662b411b363SPhilipp Reisner goto free_bios_submit_one_by_one; 663b411b363SPhilipp Reisner } 664b411b363SPhilipp Reisner 665b411b363SPhilipp Reisner /* unnecessary optimization? */ 666b411b363SPhilipp Reisner lc_unlock(mdev->act_log); 667b411b363SPhilipp Reisner wake_up(&mdev->al_wait); 668b411b363SPhilipp Reisner 669b411b363SPhilipp Reisner /* all prepared, submit them */ 670b411b363SPhilipp Reisner for (i = 0; i < nr_elements; i++) { 671b411b363SPhilipp Reisner if (bios[i] == NULL) 672b411b363SPhilipp Reisner break; 673b411b363SPhilipp Reisner if (FAULT_ACTIVE(mdev, DRBD_FAULT_MD_WR)) { 674b411b363SPhilipp Reisner bios[i]->bi_rw = WRITE; 675b411b363SPhilipp Reisner bio_endio(bios[i], -EIO); 676b411b363SPhilipp Reisner } else { 677b411b363SPhilipp Reisner submit_bio(WRITE, bios[i]); 678b411b363SPhilipp Reisner } 679b411b363SPhilipp Reisner } 680b411b363SPhilipp Reisner 681b411b363SPhilipp Reisner drbd_blk_run_queue(bdev_get_queue(mdev->ldev->md_bdev)); 682b411b363SPhilipp Reisner 683b411b363SPhilipp Reisner /* always (try to) flush bitmap to stable storage */ 684b411b363SPhilipp Reisner drbd_md_flush(mdev); 685b411b363SPhilipp Reisner 686b411b363SPhilipp Reisner /* In case we did not submit a single IO do not wait for 687b411b363SPhilipp Reisner * them to complete. ( Because we would wait forever here. ) 688b411b363SPhilipp Reisner * 689b411b363SPhilipp Reisner * In case we had IOs and they are already complete, there 690b411b363SPhilipp Reisner * is not point in waiting anyways. 691b411b363SPhilipp Reisner * Therefore this if () ... */ 692b411b363SPhilipp Reisner if (atomic_read(&wc.count)) 693b411b363SPhilipp Reisner wait_for_completion(&wc.io_done); 694b411b363SPhilipp Reisner 695b411b363SPhilipp Reisner put_ldev(mdev); 696b411b363SPhilipp Reisner 697b411b363SPhilipp Reisner kfree(bios); 698b411b363SPhilipp Reisner return; 699b411b363SPhilipp Reisner 700b411b363SPhilipp Reisner free_bios_submit_one_by_one: 701b411b363SPhilipp Reisner /* free everything by calling the endio callback directly. */ 702b411b363SPhilipp Reisner for (i = 0; i < nr_elements && bios[i]; i++) 703b411b363SPhilipp Reisner bio_endio(bios[i], 0); 704b411b363SPhilipp Reisner 705b411b363SPhilipp Reisner kfree(bios); 706b411b363SPhilipp Reisner 707b411b363SPhilipp Reisner submit_one_by_one: 708b411b363SPhilipp Reisner dev_warn(DEV, "Using the slow drbd_al_to_on_disk_bm()\n"); 709b411b363SPhilipp Reisner 710b411b363SPhilipp Reisner for (i = 0; i < mdev->act_log->nr_elements; i++) { 711b411b363SPhilipp Reisner enr = lc_element_by_index(mdev->act_log, i)->lc_number; 712b411b363SPhilipp Reisner if (enr == LC_FREE) 713b411b363SPhilipp Reisner continue; 714b411b363SPhilipp Reisner /* Really slow: if we have al-extents 16..19 active, 715b411b363SPhilipp Reisner * sector 4 will be written four times! Synchronous! */ 716b411b363SPhilipp Reisner drbd_bm_write_sect(mdev, enr/AL_EXT_PER_BM_SECT); 717b411b363SPhilipp Reisner } 718b411b363SPhilipp Reisner 719b411b363SPhilipp Reisner lc_unlock(mdev->act_log); 720b411b363SPhilipp Reisner wake_up(&mdev->al_wait); 721b411b363SPhilipp Reisner put_ldev(mdev); 722b411b363SPhilipp Reisner } 723b411b363SPhilipp Reisner 724b411b363SPhilipp Reisner /** 725b411b363SPhilipp Reisner * drbd_al_apply_to_bm() - Sets the bitmap to diry(1) where covered ba active AL extents 726b411b363SPhilipp Reisner * @mdev: DRBD device. 727b411b363SPhilipp Reisner */ 728b411b363SPhilipp Reisner void drbd_al_apply_to_bm(struct drbd_conf *mdev) 729b411b363SPhilipp Reisner { 730b411b363SPhilipp Reisner unsigned int enr; 731b411b363SPhilipp Reisner unsigned long add = 0; 732b411b363SPhilipp Reisner char ppb[10]; 733b411b363SPhilipp Reisner int i; 734b411b363SPhilipp Reisner 735b411b363SPhilipp Reisner wait_event(mdev->al_wait, lc_try_lock(mdev->act_log)); 736b411b363SPhilipp Reisner 737b411b363SPhilipp Reisner for (i = 0; i < mdev->act_log->nr_elements; i++) { 738b411b363SPhilipp Reisner enr = lc_element_by_index(mdev->act_log, i)->lc_number; 739b411b363SPhilipp Reisner if (enr == LC_FREE) 740b411b363SPhilipp Reisner continue; 741b411b363SPhilipp Reisner add += drbd_bm_ALe_set_all(mdev, enr); 742b411b363SPhilipp Reisner } 743b411b363SPhilipp Reisner 744b411b363SPhilipp Reisner lc_unlock(mdev->act_log); 745b411b363SPhilipp Reisner wake_up(&mdev->al_wait); 746b411b363SPhilipp Reisner 747b411b363SPhilipp Reisner dev_info(DEV, "Marked additional %s as out-of-sync based on AL.\n", 748b411b363SPhilipp Reisner ppsize(ppb, Bit2KB(add))); 749b411b363SPhilipp Reisner } 750b411b363SPhilipp Reisner 751b411b363SPhilipp Reisner static int _try_lc_del(struct drbd_conf *mdev, struct lc_element *al_ext) 752b411b363SPhilipp Reisner { 753b411b363SPhilipp Reisner int rv; 754b411b363SPhilipp Reisner 755b411b363SPhilipp Reisner spin_lock_irq(&mdev->al_lock); 756b411b363SPhilipp Reisner rv = (al_ext->refcnt == 0); 757b411b363SPhilipp Reisner if (likely(rv)) 758b411b363SPhilipp Reisner lc_del(mdev->act_log, al_ext); 759b411b363SPhilipp Reisner spin_unlock_irq(&mdev->al_lock); 760b411b363SPhilipp Reisner 761b411b363SPhilipp Reisner return rv; 762b411b363SPhilipp Reisner } 763b411b363SPhilipp Reisner 764b411b363SPhilipp Reisner /** 765b411b363SPhilipp Reisner * drbd_al_shrink() - Removes all active extents form the activity log 766b411b363SPhilipp Reisner * @mdev: DRBD device. 767b411b363SPhilipp Reisner * 768b411b363SPhilipp Reisner * Removes all active extents form the activity log, waiting until 769b411b363SPhilipp Reisner * the reference count of each entry dropped to 0 first, of course. 770b411b363SPhilipp Reisner * 771b411b363SPhilipp Reisner * You need to lock mdev->act_log with lc_try_lock() / lc_unlock() 772b411b363SPhilipp Reisner */ 773b411b363SPhilipp Reisner void drbd_al_shrink(struct drbd_conf *mdev) 774b411b363SPhilipp Reisner { 775b411b363SPhilipp Reisner struct lc_element *al_ext; 776b411b363SPhilipp Reisner int i; 777b411b363SPhilipp Reisner 778b411b363SPhilipp Reisner D_ASSERT(test_bit(__LC_DIRTY, &mdev->act_log->flags)); 779b411b363SPhilipp Reisner 780b411b363SPhilipp Reisner for (i = 0; i < mdev->act_log->nr_elements; i++) { 781b411b363SPhilipp Reisner al_ext = lc_element_by_index(mdev->act_log, i); 782b411b363SPhilipp Reisner if (al_ext->lc_number == LC_FREE) 783b411b363SPhilipp Reisner continue; 784b411b363SPhilipp Reisner wait_event(mdev->al_wait, _try_lc_del(mdev, al_ext)); 785b411b363SPhilipp Reisner } 786b411b363SPhilipp Reisner 787b411b363SPhilipp Reisner wake_up(&mdev->al_wait); 788b411b363SPhilipp Reisner } 789b411b363SPhilipp Reisner 790b411b363SPhilipp Reisner static int w_update_odbm(struct drbd_conf *mdev, struct drbd_work *w, int unused) 791b411b363SPhilipp Reisner { 792b411b363SPhilipp Reisner struct update_odbm_work *udw = container_of(w, struct update_odbm_work, w); 793b411b363SPhilipp Reisner 794b411b363SPhilipp Reisner if (!get_ldev(mdev)) { 795b411b363SPhilipp Reisner if (__ratelimit(&drbd_ratelimit_state)) 796b411b363SPhilipp Reisner dev_warn(DEV, "Can not update on disk bitmap, local IO disabled.\n"); 797b411b363SPhilipp Reisner kfree(udw); 798b411b363SPhilipp Reisner return 1; 799b411b363SPhilipp Reisner } 800b411b363SPhilipp Reisner 801b411b363SPhilipp Reisner drbd_bm_write_sect(mdev, udw->enr); 802b411b363SPhilipp Reisner put_ldev(mdev); 803b411b363SPhilipp Reisner 804b411b363SPhilipp Reisner kfree(udw); 805b411b363SPhilipp Reisner 806b411b363SPhilipp Reisner if (drbd_bm_total_weight(mdev) <= mdev->rs_failed) { 807b411b363SPhilipp Reisner switch (mdev->state.conn) { 808b411b363SPhilipp Reisner case C_SYNC_SOURCE: case C_SYNC_TARGET: 809b411b363SPhilipp Reisner case C_PAUSED_SYNC_S: case C_PAUSED_SYNC_T: 810b411b363SPhilipp Reisner drbd_resync_finished(mdev); 811b411b363SPhilipp Reisner default: 812b411b363SPhilipp Reisner /* nothing to do */ 813b411b363SPhilipp Reisner break; 814b411b363SPhilipp Reisner } 815b411b363SPhilipp Reisner } 816b411b363SPhilipp Reisner drbd_bcast_sync_progress(mdev); 817b411b363SPhilipp Reisner 818b411b363SPhilipp Reisner return 1; 819b411b363SPhilipp Reisner } 820b411b363SPhilipp Reisner 821b411b363SPhilipp Reisner 822b411b363SPhilipp Reisner /* ATTENTION. The AL's extents are 4MB each, while the extents in the 823b411b363SPhilipp Reisner * resync LRU-cache are 16MB each. 824b411b363SPhilipp Reisner * The caller of this function has to hold an get_ldev() reference. 825b411b363SPhilipp Reisner * 826b411b363SPhilipp Reisner * TODO will be obsoleted once we have a caching lru of the on disk bitmap 827b411b363SPhilipp Reisner */ 828b411b363SPhilipp Reisner static void drbd_try_clear_on_disk_bm(struct drbd_conf *mdev, sector_t sector, 829b411b363SPhilipp Reisner int count, int success) 830b411b363SPhilipp Reisner { 831b411b363SPhilipp Reisner struct lc_element *e; 832b411b363SPhilipp Reisner struct update_odbm_work *udw; 833b411b363SPhilipp Reisner 834b411b363SPhilipp Reisner unsigned int enr; 835b411b363SPhilipp Reisner 836b411b363SPhilipp Reisner D_ASSERT(atomic_read(&mdev->local_cnt)); 837b411b363SPhilipp Reisner 838b411b363SPhilipp Reisner /* I simply assume that a sector/size pair never crosses 839b411b363SPhilipp Reisner * a 16 MB extent border. (Currently this is true...) */ 840b411b363SPhilipp Reisner enr = BM_SECT_TO_EXT(sector); 841b411b363SPhilipp Reisner 842b411b363SPhilipp Reisner e = lc_get(mdev->resync, enr); 843b411b363SPhilipp Reisner if (e) { 844b411b363SPhilipp Reisner struct bm_extent *ext = lc_entry(e, struct bm_extent, lce); 845b411b363SPhilipp Reisner if (ext->lce.lc_number == enr) { 846b411b363SPhilipp Reisner if (success) 847b411b363SPhilipp Reisner ext->rs_left -= count; 848b411b363SPhilipp Reisner else 849b411b363SPhilipp Reisner ext->rs_failed += count; 850b411b363SPhilipp Reisner if (ext->rs_left < ext->rs_failed) { 851b411b363SPhilipp Reisner dev_err(DEV, "BAD! sector=%llus enr=%u rs_left=%d " 852b411b363SPhilipp Reisner "rs_failed=%d count=%d\n", 853b411b363SPhilipp Reisner (unsigned long long)sector, 854b411b363SPhilipp Reisner ext->lce.lc_number, ext->rs_left, 855b411b363SPhilipp Reisner ext->rs_failed, count); 856b411b363SPhilipp Reisner dump_stack(); 857b411b363SPhilipp Reisner 858b411b363SPhilipp Reisner lc_put(mdev->resync, &ext->lce); 859b411b363SPhilipp Reisner drbd_force_state(mdev, NS(conn, C_DISCONNECTING)); 860b411b363SPhilipp Reisner return; 861b411b363SPhilipp Reisner } 862b411b363SPhilipp Reisner } else { 863b411b363SPhilipp Reisner /* Normally this element should be in the cache, 864b411b363SPhilipp Reisner * since drbd_rs_begin_io() pulled it already in. 865b411b363SPhilipp Reisner * 866b411b363SPhilipp Reisner * But maybe an application write finished, and we set 867b411b363SPhilipp Reisner * something outside the resync lru_cache in sync. 868b411b363SPhilipp Reisner */ 869b411b363SPhilipp Reisner int rs_left = drbd_bm_e_weight(mdev, enr); 870b411b363SPhilipp Reisner if (ext->flags != 0) { 871b411b363SPhilipp Reisner dev_warn(DEV, "changing resync lce: %d[%u;%02lx]" 872b411b363SPhilipp Reisner " -> %d[%u;00]\n", 873b411b363SPhilipp Reisner ext->lce.lc_number, ext->rs_left, 874b411b363SPhilipp Reisner ext->flags, enr, rs_left); 875b411b363SPhilipp Reisner ext->flags = 0; 876b411b363SPhilipp Reisner } 877b411b363SPhilipp Reisner if (ext->rs_failed) { 878b411b363SPhilipp Reisner dev_warn(DEV, "Kicking resync_lru element enr=%u " 879b411b363SPhilipp Reisner "out with rs_failed=%d\n", 880b411b363SPhilipp Reisner ext->lce.lc_number, ext->rs_failed); 881b411b363SPhilipp Reisner set_bit(WRITE_BM_AFTER_RESYNC, &mdev->flags); 882b411b363SPhilipp Reisner } 883b411b363SPhilipp Reisner ext->rs_left = rs_left; 884b411b363SPhilipp Reisner ext->rs_failed = success ? 0 : count; 885b411b363SPhilipp Reisner lc_changed(mdev->resync, &ext->lce); 886b411b363SPhilipp Reisner } 887b411b363SPhilipp Reisner lc_put(mdev->resync, &ext->lce); 888b411b363SPhilipp Reisner /* no race, we are within the al_lock! */ 889b411b363SPhilipp Reisner 890b411b363SPhilipp Reisner if (ext->rs_left == ext->rs_failed) { 891b411b363SPhilipp Reisner ext->rs_failed = 0; 892b411b363SPhilipp Reisner 893b411b363SPhilipp Reisner udw = kmalloc(sizeof(*udw), GFP_ATOMIC); 894b411b363SPhilipp Reisner if (udw) { 895b411b363SPhilipp Reisner udw->enr = ext->lce.lc_number; 896b411b363SPhilipp Reisner udw->w.cb = w_update_odbm; 897b411b363SPhilipp Reisner drbd_queue_work_front(&mdev->data.work, &udw->w); 898b411b363SPhilipp Reisner } else { 899b411b363SPhilipp Reisner dev_warn(DEV, "Could not kmalloc an udw\n"); 900b411b363SPhilipp Reisner set_bit(WRITE_BM_AFTER_RESYNC, &mdev->flags); 901b411b363SPhilipp Reisner } 902b411b363SPhilipp Reisner } 903b411b363SPhilipp Reisner } else { 904b411b363SPhilipp Reisner dev_err(DEV, "lc_get() failed! locked=%d/%d flags=%lu\n", 905b411b363SPhilipp Reisner mdev->resync_locked, 906b411b363SPhilipp Reisner mdev->resync->nr_elements, 907b411b363SPhilipp Reisner mdev->resync->flags); 908b411b363SPhilipp Reisner } 909b411b363SPhilipp Reisner } 910b411b363SPhilipp Reisner 911b411b363SPhilipp Reisner /* clear the bit corresponding to the piece of storage in question: 912b411b363SPhilipp Reisner * size byte of data starting from sector. Only clear a bits of the affected 913b411b363SPhilipp Reisner * one ore more _aligned_ BM_BLOCK_SIZE blocks. 914b411b363SPhilipp Reisner * 915b411b363SPhilipp Reisner * called by worker on C_SYNC_TARGET and receiver on SyncSource. 916b411b363SPhilipp Reisner * 917b411b363SPhilipp Reisner */ 918b411b363SPhilipp Reisner void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector, int size, 919b411b363SPhilipp Reisner const char *file, const unsigned int line) 920b411b363SPhilipp Reisner { 921b411b363SPhilipp Reisner /* Is called from worker and receiver context _only_ */ 922b411b363SPhilipp Reisner unsigned long sbnr, ebnr, lbnr; 923b411b363SPhilipp Reisner unsigned long count = 0; 924b411b363SPhilipp Reisner sector_t esector, nr_sectors; 925b411b363SPhilipp Reisner int wake_up = 0; 926b411b363SPhilipp Reisner unsigned long flags; 927b411b363SPhilipp Reisner 928b411b363SPhilipp Reisner if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_SEGMENT_SIZE) { 929b411b363SPhilipp Reisner dev_err(DEV, "drbd_set_in_sync: sector=%llus size=%d nonsense!\n", 930b411b363SPhilipp Reisner (unsigned long long)sector, size); 931b411b363SPhilipp Reisner return; 932b411b363SPhilipp Reisner } 933b411b363SPhilipp Reisner nr_sectors = drbd_get_capacity(mdev->this_bdev); 934b411b363SPhilipp Reisner esector = sector + (size >> 9) - 1; 935b411b363SPhilipp Reisner 936b411b363SPhilipp Reisner ERR_IF(sector >= nr_sectors) return; 937b411b363SPhilipp Reisner ERR_IF(esector >= nr_sectors) esector = (nr_sectors-1); 938b411b363SPhilipp Reisner 939b411b363SPhilipp Reisner lbnr = BM_SECT_TO_BIT(nr_sectors-1); 940b411b363SPhilipp Reisner 941b411b363SPhilipp Reisner /* we clear it (in sync). 942b411b363SPhilipp Reisner * round up start sector, round down end sector. we make sure we only 943b411b363SPhilipp Reisner * clear full, aligned, BM_BLOCK_SIZE (4K) blocks */ 944b411b363SPhilipp Reisner if (unlikely(esector < BM_SECT_PER_BIT-1)) 945b411b363SPhilipp Reisner return; 946b411b363SPhilipp Reisner if (unlikely(esector == (nr_sectors-1))) 947b411b363SPhilipp Reisner ebnr = lbnr; 948b411b363SPhilipp Reisner else 949b411b363SPhilipp Reisner ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1)); 950b411b363SPhilipp Reisner sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1); 951b411b363SPhilipp Reisner 952b411b363SPhilipp Reisner if (sbnr > ebnr) 953b411b363SPhilipp Reisner return; 954b411b363SPhilipp Reisner 955b411b363SPhilipp Reisner /* 956b411b363SPhilipp Reisner * ok, (capacity & 7) != 0 sometimes, but who cares... 957b411b363SPhilipp Reisner * we count rs_{total,left} in bits, not sectors. 958b411b363SPhilipp Reisner */ 959b411b363SPhilipp Reisner spin_lock_irqsave(&mdev->al_lock, flags); 960b411b363SPhilipp Reisner count = drbd_bm_clear_bits(mdev, sbnr, ebnr); 961b411b363SPhilipp Reisner if (count) { 962b411b363SPhilipp Reisner /* we need the lock for drbd_try_clear_on_disk_bm */ 963b411b363SPhilipp Reisner if (jiffies - mdev->rs_mark_time > HZ*10) { 964b411b363SPhilipp Reisner /* should be rolling marks, 965b411b363SPhilipp Reisner * but we estimate only anyways. */ 966b411b363SPhilipp Reisner if (mdev->rs_mark_left != drbd_bm_total_weight(mdev) && 967b411b363SPhilipp Reisner mdev->state.conn != C_PAUSED_SYNC_T && 968b411b363SPhilipp Reisner mdev->state.conn != C_PAUSED_SYNC_S) { 969b411b363SPhilipp Reisner mdev->rs_mark_time = jiffies; 970b411b363SPhilipp Reisner mdev->rs_mark_left = drbd_bm_total_weight(mdev); 971b411b363SPhilipp Reisner } 972b411b363SPhilipp Reisner } 973b411b363SPhilipp Reisner if (get_ldev(mdev)) { 974b411b363SPhilipp Reisner drbd_try_clear_on_disk_bm(mdev, sector, count, TRUE); 975b411b363SPhilipp Reisner put_ldev(mdev); 976b411b363SPhilipp Reisner } 977b411b363SPhilipp Reisner /* just wake_up unconditional now, various lc_chaged(), 978b411b363SPhilipp Reisner * lc_put() in drbd_try_clear_on_disk_bm(). */ 979b411b363SPhilipp Reisner wake_up = 1; 980b411b363SPhilipp Reisner } 981b411b363SPhilipp Reisner spin_unlock_irqrestore(&mdev->al_lock, flags); 982b411b363SPhilipp Reisner if (wake_up) 983b411b363SPhilipp Reisner wake_up(&mdev->al_wait); 984b411b363SPhilipp Reisner } 985b411b363SPhilipp Reisner 986b411b363SPhilipp Reisner /* 987b411b363SPhilipp Reisner * this is intended to set one request worth of data out of sync. 988b411b363SPhilipp Reisner * affects at least 1 bit, 989b411b363SPhilipp Reisner * and at most 1+DRBD_MAX_SEGMENT_SIZE/BM_BLOCK_SIZE bits. 990b411b363SPhilipp Reisner * 991b411b363SPhilipp Reisner * called by tl_clear and drbd_send_dblock (==drbd_make_request). 992b411b363SPhilipp Reisner * so this can be _any_ process. 993b411b363SPhilipp Reisner */ 994b411b363SPhilipp Reisner void __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector, int size, 995b411b363SPhilipp Reisner const char *file, const unsigned int line) 996b411b363SPhilipp Reisner { 997b411b363SPhilipp Reisner unsigned long sbnr, ebnr, lbnr, flags; 998b411b363SPhilipp Reisner sector_t esector, nr_sectors; 999b411b363SPhilipp Reisner unsigned int enr, count; 1000b411b363SPhilipp Reisner struct lc_element *e; 1001b411b363SPhilipp Reisner 1002b411b363SPhilipp Reisner if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_SEGMENT_SIZE) { 1003b411b363SPhilipp Reisner dev_err(DEV, "sector: %llus, size: %d\n", 1004b411b363SPhilipp Reisner (unsigned long long)sector, size); 1005b411b363SPhilipp Reisner return; 1006b411b363SPhilipp Reisner } 1007b411b363SPhilipp Reisner 1008b411b363SPhilipp Reisner if (!get_ldev(mdev)) 1009b411b363SPhilipp Reisner return; /* no disk, no metadata, no bitmap to set bits in */ 1010b411b363SPhilipp Reisner 1011b411b363SPhilipp Reisner nr_sectors = drbd_get_capacity(mdev->this_bdev); 1012b411b363SPhilipp Reisner esector = sector + (size >> 9) - 1; 1013b411b363SPhilipp Reisner 1014b411b363SPhilipp Reisner ERR_IF(sector >= nr_sectors) 1015b411b363SPhilipp Reisner goto out; 1016b411b363SPhilipp Reisner ERR_IF(esector >= nr_sectors) 1017b411b363SPhilipp Reisner esector = (nr_sectors-1); 1018b411b363SPhilipp Reisner 1019b411b363SPhilipp Reisner lbnr = BM_SECT_TO_BIT(nr_sectors-1); 1020b411b363SPhilipp Reisner 1021b411b363SPhilipp Reisner /* we set it out of sync, 1022b411b363SPhilipp Reisner * we do not need to round anything here */ 1023b411b363SPhilipp Reisner sbnr = BM_SECT_TO_BIT(sector); 1024b411b363SPhilipp Reisner ebnr = BM_SECT_TO_BIT(esector); 1025b411b363SPhilipp Reisner 1026b411b363SPhilipp Reisner /* ok, (capacity & 7) != 0 sometimes, but who cares... 1027b411b363SPhilipp Reisner * we count rs_{total,left} in bits, not sectors. */ 1028b411b363SPhilipp Reisner spin_lock_irqsave(&mdev->al_lock, flags); 1029b411b363SPhilipp Reisner count = drbd_bm_set_bits(mdev, sbnr, ebnr); 1030b411b363SPhilipp Reisner 1031b411b363SPhilipp Reisner enr = BM_SECT_TO_EXT(sector); 1032b411b363SPhilipp Reisner e = lc_find(mdev->resync, enr); 1033b411b363SPhilipp Reisner if (e) 1034b411b363SPhilipp Reisner lc_entry(e, struct bm_extent, lce)->rs_left += count; 1035b411b363SPhilipp Reisner spin_unlock_irqrestore(&mdev->al_lock, flags); 1036b411b363SPhilipp Reisner 1037b411b363SPhilipp Reisner out: 1038b411b363SPhilipp Reisner put_ldev(mdev); 1039b411b363SPhilipp Reisner } 1040b411b363SPhilipp Reisner 1041b411b363SPhilipp Reisner static 1042b411b363SPhilipp Reisner struct bm_extent *_bme_get(struct drbd_conf *mdev, unsigned int enr) 1043b411b363SPhilipp Reisner { 1044b411b363SPhilipp Reisner struct lc_element *e; 1045b411b363SPhilipp Reisner struct bm_extent *bm_ext; 1046b411b363SPhilipp Reisner int wakeup = 0; 1047b411b363SPhilipp Reisner unsigned long rs_flags; 1048b411b363SPhilipp Reisner 1049b411b363SPhilipp Reisner spin_lock_irq(&mdev->al_lock); 1050b411b363SPhilipp Reisner if (mdev->resync_locked > mdev->resync->nr_elements/2) { 1051b411b363SPhilipp Reisner spin_unlock_irq(&mdev->al_lock); 1052b411b363SPhilipp Reisner return NULL; 1053b411b363SPhilipp Reisner } 1054b411b363SPhilipp Reisner e = lc_get(mdev->resync, enr); 1055b411b363SPhilipp Reisner bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; 1056b411b363SPhilipp Reisner if (bm_ext) { 1057b411b363SPhilipp Reisner if (bm_ext->lce.lc_number != enr) { 1058b411b363SPhilipp Reisner bm_ext->rs_left = drbd_bm_e_weight(mdev, enr); 1059b411b363SPhilipp Reisner bm_ext->rs_failed = 0; 1060b411b363SPhilipp Reisner lc_changed(mdev->resync, &bm_ext->lce); 1061b411b363SPhilipp Reisner wakeup = 1; 1062b411b363SPhilipp Reisner } 1063b411b363SPhilipp Reisner if (bm_ext->lce.refcnt == 1) 1064b411b363SPhilipp Reisner mdev->resync_locked++; 1065b411b363SPhilipp Reisner set_bit(BME_NO_WRITES, &bm_ext->flags); 1066b411b363SPhilipp Reisner } 1067b411b363SPhilipp Reisner rs_flags = mdev->resync->flags; 1068b411b363SPhilipp Reisner spin_unlock_irq(&mdev->al_lock); 1069b411b363SPhilipp Reisner if (wakeup) 1070b411b363SPhilipp Reisner wake_up(&mdev->al_wait); 1071b411b363SPhilipp Reisner 1072b411b363SPhilipp Reisner if (!bm_ext) { 1073b411b363SPhilipp Reisner if (rs_flags & LC_STARVING) 1074b411b363SPhilipp Reisner dev_warn(DEV, "Have to wait for element" 1075b411b363SPhilipp Reisner " (resync LRU too small?)\n"); 1076b411b363SPhilipp Reisner BUG_ON(rs_flags & LC_DIRTY); 1077b411b363SPhilipp Reisner } 1078b411b363SPhilipp Reisner 1079b411b363SPhilipp Reisner return bm_ext; 1080b411b363SPhilipp Reisner } 1081b411b363SPhilipp Reisner 1082b411b363SPhilipp Reisner static int _is_in_al(struct drbd_conf *mdev, unsigned int enr) 1083b411b363SPhilipp Reisner { 1084b411b363SPhilipp Reisner struct lc_element *al_ext; 1085b411b363SPhilipp Reisner int rv = 0; 1086b411b363SPhilipp Reisner 1087b411b363SPhilipp Reisner spin_lock_irq(&mdev->al_lock); 1088b411b363SPhilipp Reisner if (unlikely(enr == mdev->act_log->new_number)) 1089b411b363SPhilipp Reisner rv = 1; 1090b411b363SPhilipp Reisner else { 1091b411b363SPhilipp Reisner al_ext = lc_find(mdev->act_log, enr); 1092b411b363SPhilipp Reisner if (al_ext) { 1093b411b363SPhilipp Reisner if (al_ext->refcnt) 1094b411b363SPhilipp Reisner rv = 1; 1095b411b363SPhilipp Reisner } 1096b411b363SPhilipp Reisner } 1097b411b363SPhilipp Reisner spin_unlock_irq(&mdev->al_lock); 1098b411b363SPhilipp Reisner 1099b411b363SPhilipp Reisner /* 1100b411b363SPhilipp Reisner if (unlikely(rv)) { 1101b411b363SPhilipp Reisner dev_info(DEV, "Delaying sync read until app's write is done\n"); 1102b411b363SPhilipp Reisner } 1103b411b363SPhilipp Reisner */ 1104b411b363SPhilipp Reisner return rv; 1105b411b363SPhilipp Reisner } 1106b411b363SPhilipp Reisner 1107b411b363SPhilipp Reisner /** 1108b411b363SPhilipp Reisner * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED 1109b411b363SPhilipp Reisner * @mdev: DRBD device. 1110b411b363SPhilipp Reisner * @sector: The sector number. 1111b411b363SPhilipp Reisner * 1112b411b363SPhilipp Reisner * This functions sleeps on al_wait. Returns 1 on success, 0 if interrupted. 1113b411b363SPhilipp Reisner */ 1114b411b363SPhilipp Reisner int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector) 1115b411b363SPhilipp Reisner { 1116b411b363SPhilipp Reisner unsigned int enr = BM_SECT_TO_EXT(sector); 1117b411b363SPhilipp Reisner struct bm_extent *bm_ext; 1118b411b363SPhilipp Reisner int i, sig; 1119b411b363SPhilipp Reisner 1120b411b363SPhilipp Reisner sig = wait_event_interruptible(mdev->al_wait, 1121b411b363SPhilipp Reisner (bm_ext = _bme_get(mdev, enr))); 1122b411b363SPhilipp Reisner if (sig) 1123b411b363SPhilipp Reisner return 0; 1124b411b363SPhilipp Reisner 1125b411b363SPhilipp Reisner if (test_bit(BME_LOCKED, &bm_ext->flags)) 1126b411b363SPhilipp Reisner return 1; 1127b411b363SPhilipp Reisner 1128b411b363SPhilipp Reisner for (i = 0; i < AL_EXT_PER_BM_SECT; i++) { 1129b411b363SPhilipp Reisner sig = wait_event_interruptible(mdev->al_wait, 1130b411b363SPhilipp Reisner !_is_in_al(mdev, enr * AL_EXT_PER_BM_SECT + i)); 1131b411b363SPhilipp Reisner if (sig) { 1132b411b363SPhilipp Reisner spin_lock_irq(&mdev->al_lock); 1133b411b363SPhilipp Reisner if (lc_put(mdev->resync, &bm_ext->lce) == 0) { 1134b411b363SPhilipp Reisner clear_bit(BME_NO_WRITES, &bm_ext->flags); 1135b411b363SPhilipp Reisner mdev->resync_locked--; 1136b411b363SPhilipp Reisner wake_up(&mdev->al_wait); 1137b411b363SPhilipp Reisner } 1138b411b363SPhilipp Reisner spin_unlock_irq(&mdev->al_lock); 1139b411b363SPhilipp Reisner return 0; 1140b411b363SPhilipp Reisner } 1141b411b363SPhilipp Reisner } 1142b411b363SPhilipp Reisner 1143b411b363SPhilipp Reisner set_bit(BME_LOCKED, &bm_ext->flags); 1144b411b363SPhilipp Reisner 1145b411b363SPhilipp Reisner return 1; 1146b411b363SPhilipp Reisner } 1147b411b363SPhilipp Reisner 1148b411b363SPhilipp Reisner /** 1149b411b363SPhilipp Reisner * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep 1150b411b363SPhilipp Reisner * @mdev: DRBD device. 1151b411b363SPhilipp Reisner * @sector: The sector number. 1152b411b363SPhilipp Reisner * 1153b411b363SPhilipp Reisner * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then 1154b411b363SPhilipp Reisner * tries to set it to BME_LOCKED. Returns 0 upon success, and -EAGAIN 1155b411b363SPhilipp Reisner * if there is still application IO going on in this area. 1156b411b363SPhilipp Reisner */ 1157b411b363SPhilipp Reisner int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector) 1158b411b363SPhilipp Reisner { 1159b411b363SPhilipp Reisner unsigned int enr = BM_SECT_TO_EXT(sector); 1160b411b363SPhilipp Reisner const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT; 1161b411b363SPhilipp Reisner struct lc_element *e; 1162b411b363SPhilipp Reisner struct bm_extent *bm_ext; 1163b411b363SPhilipp Reisner int i; 1164b411b363SPhilipp Reisner 1165b411b363SPhilipp Reisner spin_lock_irq(&mdev->al_lock); 1166b411b363SPhilipp Reisner if (mdev->resync_wenr != LC_FREE && mdev->resync_wenr != enr) { 1167b411b363SPhilipp Reisner /* in case you have very heavy scattered io, it may 1168b411b363SPhilipp Reisner * stall the syncer undefined if we give up the ref count 1169b411b363SPhilipp Reisner * when we try again and requeue. 1170b411b363SPhilipp Reisner * 1171b411b363SPhilipp Reisner * if we don't give up the refcount, but the next time 1172b411b363SPhilipp Reisner * we are scheduled this extent has been "synced" by new 1173b411b363SPhilipp Reisner * application writes, we'd miss the lc_put on the 1174b411b363SPhilipp Reisner * extent we keep the refcount on. 1175b411b363SPhilipp Reisner * so we remembered which extent we had to try again, and 1176b411b363SPhilipp Reisner * if the next requested one is something else, we do 1177b411b363SPhilipp Reisner * the lc_put here... 1178b411b363SPhilipp Reisner * we also have to wake_up 1179b411b363SPhilipp Reisner */ 1180b411b363SPhilipp Reisner e = lc_find(mdev->resync, mdev->resync_wenr); 1181b411b363SPhilipp Reisner bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; 1182b411b363SPhilipp Reisner if (bm_ext) { 1183b411b363SPhilipp Reisner D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags)); 1184b411b363SPhilipp Reisner D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags)); 1185b411b363SPhilipp Reisner clear_bit(BME_NO_WRITES, &bm_ext->flags); 1186b411b363SPhilipp Reisner mdev->resync_wenr = LC_FREE; 1187b411b363SPhilipp Reisner if (lc_put(mdev->resync, &bm_ext->lce) == 0) 1188b411b363SPhilipp Reisner mdev->resync_locked--; 1189b411b363SPhilipp Reisner wake_up(&mdev->al_wait); 1190b411b363SPhilipp Reisner } else { 1191b411b363SPhilipp Reisner dev_alert(DEV, "LOGIC BUG\n"); 1192b411b363SPhilipp Reisner } 1193b411b363SPhilipp Reisner } 1194b411b363SPhilipp Reisner /* TRY. */ 1195b411b363SPhilipp Reisner e = lc_try_get(mdev->resync, enr); 1196b411b363SPhilipp Reisner bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; 1197b411b363SPhilipp Reisner if (bm_ext) { 1198b411b363SPhilipp Reisner if (test_bit(BME_LOCKED, &bm_ext->flags)) 1199b411b363SPhilipp Reisner goto proceed; 1200b411b363SPhilipp Reisner if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) { 1201b411b363SPhilipp Reisner mdev->resync_locked++; 1202b411b363SPhilipp Reisner } else { 1203b411b363SPhilipp Reisner /* we did set the BME_NO_WRITES, 1204b411b363SPhilipp Reisner * but then could not set BME_LOCKED, 1205b411b363SPhilipp Reisner * so we tried again. 1206b411b363SPhilipp Reisner * drop the extra reference. */ 1207b411b363SPhilipp Reisner bm_ext->lce.refcnt--; 1208b411b363SPhilipp Reisner D_ASSERT(bm_ext->lce.refcnt > 0); 1209b411b363SPhilipp Reisner } 1210b411b363SPhilipp Reisner goto check_al; 1211b411b363SPhilipp Reisner } else { 1212b411b363SPhilipp Reisner /* do we rather want to try later? */ 1213*6a0afdf5SJens Axboe if (mdev->resync_locked > mdev->resync->nr_elements-3) 1214b411b363SPhilipp Reisner goto try_again; 1215b411b363SPhilipp Reisner /* Do or do not. There is no try. -- Yoda */ 1216b411b363SPhilipp Reisner e = lc_get(mdev->resync, enr); 1217b411b363SPhilipp Reisner bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; 1218b411b363SPhilipp Reisner if (!bm_ext) { 1219b411b363SPhilipp Reisner const unsigned long rs_flags = mdev->resync->flags; 1220b411b363SPhilipp Reisner if (rs_flags & LC_STARVING) 1221b411b363SPhilipp Reisner dev_warn(DEV, "Have to wait for element" 1222b411b363SPhilipp Reisner " (resync LRU too small?)\n"); 1223b411b363SPhilipp Reisner BUG_ON(rs_flags & LC_DIRTY); 1224b411b363SPhilipp Reisner goto try_again; 1225b411b363SPhilipp Reisner } 1226b411b363SPhilipp Reisner if (bm_ext->lce.lc_number != enr) { 1227b411b363SPhilipp Reisner bm_ext->rs_left = drbd_bm_e_weight(mdev, enr); 1228b411b363SPhilipp Reisner bm_ext->rs_failed = 0; 1229b411b363SPhilipp Reisner lc_changed(mdev->resync, &bm_ext->lce); 1230b411b363SPhilipp Reisner wake_up(&mdev->al_wait); 1231b411b363SPhilipp Reisner D_ASSERT(test_bit(BME_LOCKED, &bm_ext->flags) == 0); 1232b411b363SPhilipp Reisner } 1233b411b363SPhilipp Reisner set_bit(BME_NO_WRITES, &bm_ext->flags); 1234b411b363SPhilipp Reisner D_ASSERT(bm_ext->lce.refcnt == 1); 1235b411b363SPhilipp Reisner mdev->resync_locked++; 1236b411b363SPhilipp Reisner goto check_al; 1237b411b363SPhilipp Reisner } 1238b411b363SPhilipp Reisner check_al: 1239b411b363SPhilipp Reisner for (i = 0; i < AL_EXT_PER_BM_SECT; i++) { 1240b411b363SPhilipp Reisner if (unlikely(al_enr+i == mdev->act_log->new_number)) 1241b411b363SPhilipp Reisner goto try_again; 1242b411b363SPhilipp Reisner if (lc_is_used(mdev->act_log, al_enr+i)) 1243b411b363SPhilipp Reisner goto try_again; 1244b411b363SPhilipp Reisner } 1245b411b363SPhilipp Reisner set_bit(BME_LOCKED, &bm_ext->flags); 1246b411b363SPhilipp Reisner proceed: 1247b411b363SPhilipp Reisner mdev->resync_wenr = LC_FREE; 1248b411b363SPhilipp Reisner spin_unlock_irq(&mdev->al_lock); 1249b411b363SPhilipp Reisner return 0; 1250b411b363SPhilipp Reisner 1251b411b363SPhilipp Reisner try_again: 1252b411b363SPhilipp Reisner if (bm_ext) 1253b411b363SPhilipp Reisner mdev->resync_wenr = enr; 1254b411b363SPhilipp Reisner spin_unlock_irq(&mdev->al_lock); 1255b411b363SPhilipp Reisner return -EAGAIN; 1256b411b363SPhilipp Reisner } 1257b411b363SPhilipp Reisner 1258b411b363SPhilipp Reisner void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector) 1259b411b363SPhilipp Reisner { 1260b411b363SPhilipp Reisner unsigned int enr = BM_SECT_TO_EXT(sector); 1261b411b363SPhilipp Reisner struct lc_element *e; 1262b411b363SPhilipp Reisner struct bm_extent *bm_ext; 1263b411b363SPhilipp Reisner unsigned long flags; 1264b411b363SPhilipp Reisner 1265b411b363SPhilipp Reisner spin_lock_irqsave(&mdev->al_lock, flags); 1266b411b363SPhilipp Reisner e = lc_find(mdev->resync, enr); 1267b411b363SPhilipp Reisner bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; 1268b411b363SPhilipp Reisner if (!bm_ext) { 1269b411b363SPhilipp Reisner spin_unlock_irqrestore(&mdev->al_lock, flags); 1270b411b363SPhilipp Reisner if (__ratelimit(&drbd_ratelimit_state)) 1271b411b363SPhilipp Reisner dev_err(DEV, "drbd_rs_complete_io() called, but extent not found\n"); 1272b411b363SPhilipp Reisner return; 1273b411b363SPhilipp Reisner } 1274b411b363SPhilipp Reisner 1275b411b363SPhilipp Reisner if (bm_ext->lce.refcnt == 0) { 1276b411b363SPhilipp Reisner spin_unlock_irqrestore(&mdev->al_lock, flags); 1277b411b363SPhilipp Reisner dev_err(DEV, "drbd_rs_complete_io(,%llu [=%u]) called, " 1278b411b363SPhilipp Reisner "but refcnt is 0!?\n", 1279b411b363SPhilipp Reisner (unsigned long long)sector, enr); 1280b411b363SPhilipp Reisner return; 1281b411b363SPhilipp Reisner } 1282b411b363SPhilipp Reisner 1283b411b363SPhilipp Reisner if (lc_put(mdev->resync, &bm_ext->lce) == 0) { 1284b411b363SPhilipp Reisner clear_bit(BME_LOCKED, &bm_ext->flags); 1285b411b363SPhilipp Reisner clear_bit(BME_NO_WRITES, &bm_ext->flags); 1286b411b363SPhilipp Reisner mdev->resync_locked--; 1287b411b363SPhilipp Reisner wake_up(&mdev->al_wait); 1288b411b363SPhilipp Reisner } 1289b411b363SPhilipp Reisner 1290b411b363SPhilipp Reisner spin_unlock_irqrestore(&mdev->al_lock, flags); 1291b411b363SPhilipp Reisner } 1292b411b363SPhilipp Reisner 1293b411b363SPhilipp Reisner /** 1294b411b363SPhilipp Reisner * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED) 1295b411b363SPhilipp Reisner * @mdev: DRBD device. 1296b411b363SPhilipp Reisner */ 1297b411b363SPhilipp Reisner void drbd_rs_cancel_all(struct drbd_conf *mdev) 1298b411b363SPhilipp Reisner { 1299b411b363SPhilipp Reisner spin_lock_irq(&mdev->al_lock); 1300b411b363SPhilipp Reisner 1301b411b363SPhilipp Reisner if (get_ldev_if_state(mdev, D_FAILED)) { /* Makes sure ->resync is there. */ 1302b411b363SPhilipp Reisner lc_reset(mdev->resync); 1303b411b363SPhilipp Reisner put_ldev(mdev); 1304b411b363SPhilipp Reisner } 1305b411b363SPhilipp Reisner mdev->resync_locked = 0; 1306b411b363SPhilipp Reisner mdev->resync_wenr = LC_FREE; 1307b411b363SPhilipp Reisner spin_unlock_irq(&mdev->al_lock); 1308b411b363SPhilipp Reisner wake_up(&mdev->al_wait); 1309b411b363SPhilipp Reisner } 1310b411b363SPhilipp Reisner 1311b411b363SPhilipp Reisner /** 1312b411b363SPhilipp Reisner * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU 1313b411b363SPhilipp Reisner * @mdev: DRBD device. 1314b411b363SPhilipp Reisner * 1315b411b363SPhilipp Reisner * Returns 0 upon success, -EAGAIN if at least one reference count was 1316b411b363SPhilipp Reisner * not zero. 1317b411b363SPhilipp Reisner */ 1318b411b363SPhilipp Reisner int drbd_rs_del_all(struct drbd_conf *mdev) 1319b411b363SPhilipp Reisner { 1320b411b363SPhilipp Reisner struct lc_element *e; 1321b411b363SPhilipp Reisner struct bm_extent *bm_ext; 1322b411b363SPhilipp Reisner int i; 1323b411b363SPhilipp Reisner 1324b411b363SPhilipp Reisner spin_lock_irq(&mdev->al_lock); 1325b411b363SPhilipp Reisner 1326b411b363SPhilipp Reisner if (get_ldev_if_state(mdev, D_FAILED)) { 1327b411b363SPhilipp Reisner /* ok, ->resync is there. */ 1328b411b363SPhilipp Reisner for (i = 0; i < mdev->resync->nr_elements; i++) { 1329b411b363SPhilipp Reisner e = lc_element_by_index(mdev->resync, i); 1330b411b363SPhilipp Reisner bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; 1331b411b363SPhilipp Reisner if (bm_ext->lce.lc_number == LC_FREE) 1332b411b363SPhilipp Reisner continue; 1333b411b363SPhilipp Reisner if (bm_ext->lce.lc_number == mdev->resync_wenr) { 1334b411b363SPhilipp Reisner dev_info(DEV, "dropping %u in drbd_rs_del_all, apparently" 1335b411b363SPhilipp Reisner " got 'synced' by application io\n", 1336b411b363SPhilipp Reisner mdev->resync_wenr); 1337b411b363SPhilipp Reisner D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags)); 1338b411b363SPhilipp Reisner D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags)); 1339b411b363SPhilipp Reisner clear_bit(BME_NO_WRITES, &bm_ext->flags); 1340b411b363SPhilipp Reisner mdev->resync_wenr = LC_FREE; 1341b411b363SPhilipp Reisner lc_put(mdev->resync, &bm_ext->lce); 1342b411b363SPhilipp Reisner } 1343b411b363SPhilipp Reisner if (bm_ext->lce.refcnt != 0) { 1344b411b363SPhilipp Reisner dev_info(DEV, "Retrying drbd_rs_del_all() later. " 1345b411b363SPhilipp Reisner "refcnt=%d\n", bm_ext->lce.refcnt); 1346b411b363SPhilipp Reisner put_ldev(mdev); 1347b411b363SPhilipp Reisner spin_unlock_irq(&mdev->al_lock); 1348b411b363SPhilipp Reisner return -EAGAIN; 1349b411b363SPhilipp Reisner } 1350b411b363SPhilipp Reisner D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags)); 1351b411b363SPhilipp Reisner D_ASSERT(!test_bit(BME_NO_WRITES, &bm_ext->flags)); 1352b411b363SPhilipp Reisner lc_del(mdev->resync, &bm_ext->lce); 1353b411b363SPhilipp Reisner } 1354b411b363SPhilipp Reisner D_ASSERT(mdev->resync->used == 0); 1355b411b363SPhilipp Reisner put_ldev(mdev); 1356b411b363SPhilipp Reisner } 1357b411b363SPhilipp Reisner spin_unlock_irq(&mdev->al_lock); 1358b411b363SPhilipp Reisner 1359b411b363SPhilipp Reisner return 0; 1360b411b363SPhilipp Reisner } 1361b411b363SPhilipp Reisner 1362b411b363SPhilipp Reisner /** 1363b411b363SPhilipp Reisner * drbd_rs_failed_io() - Record information on a failure to resync the specified blocks 1364b411b363SPhilipp Reisner * @mdev: DRBD device. 1365b411b363SPhilipp Reisner * @sector: The sector number. 1366b411b363SPhilipp Reisner * @size: Size of failed IO operation, in byte. 1367b411b363SPhilipp Reisner */ 1368b411b363SPhilipp Reisner void drbd_rs_failed_io(struct drbd_conf *mdev, sector_t sector, int size) 1369b411b363SPhilipp Reisner { 1370b411b363SPhilipp Reisner /* Is called from worker and receiver context _only_ */ 1371b411b363SPhilipp Reisner unsigned long sbnr, ebnr, lbnr; 1372b411b363SPhilipp Reisner unsigned long count; 1373b411b363SPhilipp Reisner sector_t esector, nr_sectors; 1374b411b363SPhilipp Reisner int wake_up = 0; 1375b411b363SPhilipp Reisner 1376b411b363SPhilipp Reisner if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_SEGMENT_SIZE) { 1377b411b363SPhilipp Reisner dev_err(DEV, "drbd_rs_failed_io: sector=%llus size=%d nonsense!\n", 1378b411b363SPhilipp Reisner (unsigned long long)sector, size); 1379b411b363SPhilipp Reisner return; 1380b411b363SPhilipp Reisner } 1381b411b363SPhilipp Reisner nr_sectors = drbd_get_capacity(mdev->this_bdev); 1382b411b363SPhilipp Reisner esector = sector + (size >> 9) - 1; 1383b411b363SPhilipp Reisner 1384b411b363SPhilipp Reisner ERR_IF(sector >= nr_sectors) return; 1385b411b363SPhilipp Reisner ERR_IF(esector >= nr_sectors) esector = (nr_sectors-1); 1386b411b363SPhilipp Reisner 1387b411b363SPhilipp Reisner lbnr = BM_SECT_TO_BIT(nr_sectors-1); 1388b411b363SPhilipp Reisner 1389b411b363SPhilipp Reisner /* 1390b411b363SPhilipp Reisner * round up start sector, round down end sector. we make sure we only 1391b411b363SPhilipp Reisner * handle full, aligned, BM_BLOCK_SIZE (4K) blocks */ 1392b411b363SPhilipp Reisner if (unlikely(esector < BM_SECT_PER_BIT-1)) 1393b411b363SPhilipp Reisner return; 1394b411b363SPhilipp Reisner if (unlikely(esector == (nr_sectors-1))) 1395b411b363SPhilipp Reisner ebnr = lbnr; 1396b411b363SPhilipp Reisner else 1397b411b363SPhilipp Reisner ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1)); 1398b411b363SPhilipp Reisner sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1); 1399b411b363SPhilipp Reisner 1400b411b363SPhilipp Reisner if (sbnr > ebnr) 1401b411b363SPhilipp Reisner return; 1402b411b363SPhilipp Reisner 1403b411b363SPhilipp Reisner /* 1404b411b363SPhilipp Reisner * ok, (capacity & 7) != 0 sometimes, but who cares... 1405b411b363SPhilipp Reisner * we count rs_{total,left} in bits, not sectors. 1406b411b363SPhilipp Reisner */ 1407b411b363SPhilipp Reisner spin_lock_irq(&mdev->al_lock); 1408b411b363SPhilipp Reisner count = drbd_bm_count_bits(mdev, sbnr, ebnr); 1409b411b363SPhilipp Reisner if (count) { 1410b411b363SPhilipp Reisner mdev->rs_failed += count; 1411b411b363SPhilipp Reisner 1412b411b363SPhilipp Reisner if (get_ldev(mdev)) { 1413b411b363SPhilipp Reisner drbd_try_clear_on_disk_bm(mdev, sector, count, FALSE); 1414b411b363SPhilipp Reisner put_ldev(mdev); 1415b411b363SPhilipp Reisner } 1416b411b363SPhilipp Reisner 1417b411b363SPhilipp Reisner /* just wake_up unconditional now, various lc_chaged(), 1418b411b363SPhilipp Reisner * lc_put() in drbd_try_clear_on_disk_bm(). */ 1419b411b363SPhilipp Reisner wake_up = 1; 1420b411b363SPhilipp Reisner } 1421b411b363SPhilipp Reisner spin_unlock_irq(&mdev->al_lock); 1422b411b363SPhilipp Reisner if (wake_up) 1423b411b363SPhilipp Reisner wake_up(&mdev->al_wait); 1424b411b363SPhilipp Reisner } 1425