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