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> 277ad651b5SLars Ellenberg #include <linux/crc32c.h> 28b411b363SPhilipp Reisner #include <linux/drbd.h> 297ad651b5SLars Ellenberg #include <linux/drbd_limits.h> 307ad651b5SLars Ellenberg #include <linux/dynamic_debug.h> 31b411b363SPhilipp Reisner #include "drbd_int.h" 32b411b363SPhilipp Reisner #include "drbd_wrappers.h" 33b411b363SPhilipp Reisner 3485f103d8SLars Ellenberg 3585f103d8SLars Ellenberg enum al_transaction_types { 3685f103d8SLars Ellenberg AL_TR_UPDATE = 0, 3785f103d8SLars Ellenberg AL_TR_INITIALIZED = 0xffff 3885f103d8SLars Ellenberg }; 397ad651b5SLars Ellenberg /* all fields on disc in big endian */ 407ad651b5SLars Ellenberg struct __packed al_transaction_on_disk { 417ad651b5SLars Ellenberg /* don't we all like magic */ 427ad651b5SLars Ellenberg __be32 magic; 437ad651b5SLars Ellenberg 447ad651b5SLars Ellenberg /* to identify the most recent transaction block 457ad651b5SLars Ellenberg * in the on disk ring buffer */ 467ad651b5SLars Ellenberg __be32 tr_number; 477ad651b5SLars Ellenberg 487ad651b5SLars Ellenberg /* checksum on the full 4k block, with this field set to 0. */ 497ad651b5SLars Ellenberg __be32 crc32c; 507ad651b5SLars Ellenberg 517ad651b5SLars Ellenberg /* type of transaction, special transaction types like: 5285f103d8SLars Ellenberg * purge-all, set-all-idle, set-all-active, ... to-be-defined 5385f103d8SLars Ellenberg * see also enum al_transaction_types */ 547ad651b5SLars Ellenberg __be16 transaction_type; 557ad651b5SLars Ellenberg 567ad651b5SLars Ellenberg /* we currently allow only a few thousand extents, 577ad651b5SLars Ellenberg * so 16bit will be enough for the slot number. */ 587ad651b5SLars Ellenberg 597ad651b5SLars Ellenberg /* how many updates in this transaction */ 607ad651b5SLars Ellenberg __be16 n_updates; 617ad651b5SLars Ellenberg 627ad651b5SLars Ellenberg /* maximum slot number, "al-extents" in drbd.conf speak. 637ad651b5SLars Ellenberg * Having this in each transaction should make reconfiguration 647ad651b5SLars Ellenberg * of that parameter easier. */ 657ad651b5SLars Ellenberg __be16 context_size; 667ad651b5SLars Ellenberg 677ad651b5SLars Ellenberg /* slot number the context starts with */ 687ad651b5SLars Ellenberg __be16 context_start_slot_nr; 697ad651b5SLars Ellenberg 707ad651b5SLars Ellenberg /* Some reserved bytes. Expected usage is a 64bit counter of 717ad651b5SLars Ellenberg * sectors-written since device creation, and other data generation tag 727ad651b5SLars Ellenberg * supporting usage */ 737ad651b5SLars Ellenberg __be32 __reserved[4]; 747ad651b5SLars Ellenberg 757ad651b5SLars Ellenberg /* --- 36 byte used --- */ 767ad651b5SLars Ellenberg 777ad651b5SLars Ellenberg /* Reserve space for up to AL_UPDATES_PER_TRANSACTION changes 787ad651b5SLars Ellenberg * in one transaction, then use the remaining byte in the 4k block for 797ad651b5SLars Ellenberg * context information. "Flexible" number of updates per transaction 807ad651b5SLars Ellenberg * does not help, as we have to account for the case when all update 817ad651b5SLars Ellenberg * slots are used anyways, so it would only complicate code without 827ad651b5SLars Ellenberg * additional benefit. 83b411b363SPhilipp Reisner */ 847ad651b5SLars Ellenberg __be16 update_slot_nr[AL_UPDATES_PER_TRANSACTION]; 857ad651b5SLars Ellenberg 867ad651b5SLars Ellenberg /* but the extent number is 32bit, which at an extent size of 4 MiB 877ad651b5SLars Ellenberg * allows to cover device sizes of up to 2**54 Byte (16 PiB) */ 887ad651b5SLars Ellenberg __be32 update_extent_nr[AL_UPDATES_PER_TRANSACTION]; 897ad651b5SLars Ellenberg 907ad651b5SLars Ellenberg /* --- 420 bytes used (36 + 64*6) --- */ 917ad651b5SLars Ellenberg 927ad651b5SLars Ellenberg /* 4096 - 420 = 3676 = 919 * 4 */ 937ad651b5SLars Ellenberg __be32 context[AL_CONTEXT_PER_TRANSACTION]; 94b411b363SPhilipp Reisner }; 95b411b363SPhilipp Reisner 96b411b363SPhilipp Reisner struct update_odbm_work { 97b411b363SPhilipp Reisner struct drbd_work w; 98b411b363SPhilipp Reisner unsigned int enr; 99b411b363SPhilipp Reisner }; 100b411b363SPhilipp Reisner 101b411b363SPhilipp Reisner struct update_al_work { 102b411b363SPhilipp Reisner struct drbd_work w; 103b411b363SPhilipp Reisner struct completion event; 1047ad651b5SLars Ellenberg int err; 105b411b363SPhilipp Reisner }; 106b411b363SPhilipp Reisner 1071b7ab15bSPhilipp Reisner static int al_write_transaction(struct drbd_conf *mdev); 108b411b363SPhilipp Reisner 109cdfda633SPhilipp Reisner void *drbd_md_get_buffer(struct drbd_conf *mdev) 110cdfda633SPhilipp Reisner { 111cdfda633SPhilipp Reisner int r; 112cdfda633SPhilipp Reisner 113cdfda633SPhilipp Reisner wait_event(mdev->misc_wait, 114cdfda633SPhilipp Reisner (r = atomic_cmpxchg(&mdev->md_io_in_use, 0, 1)) == 0 || 115cdfda633SPhilipp Reisner mdev->state.disk <= D_FAILED); 116cdfda633SPhilipp Reisner 117cdfda633SPhilipp Reisner return r ? NULL : page_address(mdev->md_io_page); 118cdfda633SPhilipp Reisner } 119cdfda633SPhilipp Reisner 120cdfda633SPhilipp Reisner void drbd_md_put_buffer(struct drbd_conf *mdev) 121cdfda633SPhilipp Reisner { 122cdfda633SPhilipp Reisner if (atomic_dec_and_test(&mdev->md_io_in_use)) 123cdfda633SPhilipp Reisner wake_up(&mdev->misc_wait); 124cdfda633SPhilipp Reisner } 125cdfda633SPhilipp Reisner 126cdfda633SPhilipp Reisner static bool md_io_allowed(struct drbd_conf *mdev) 127cdfda633SPhilipp Reisner { 128cdfda633SPhilipp Reisner enum drbd_disk_state ds = mdev->state.disk; 129cdfda633SPhilipp Reisner return ds >= D_NEGOTIATING || ds == D_ATTACHING; 130cdfda633SPhilipp Reisner } 131cdfda633SPhilipp Reisner 132*32db80f6SPhilipp Reisner void wait_until_done_or_disk_failure(struct drbd_conf *mdev, struct drbd_backing_dev *bdev, 133*32db80f6SPhilipp Reisner unsigned int *done) 134cdfda633SPhilipp Reisner { 135*32db80f6SPhilipp Reisner long dt; 136*32db80f6SPhilipp Reisner 137*32db80f6SPhilipp Reisner rcu_read_lock(); 138*32db80f6SPhilipp Reisner dt = rcu_dereference(bdev->disk_conf)->disk_timeout; 139*32db80f6SPhilipp Reisner rcu_read_unlock(); 140*32db80f6SPhilipp Reisner dt = dt * HZ / 10; 141*32db80f6SPhilipp Reisner if (dt == 0) 142*32db80f6SPhilipp Reisner dt = MAX_SCHEDULE_TIMEOUT; 143*32db80f6SPhilipp Reisner 144*32db80f6SPhilipp Reisner dt = wait_event_timeout(mdev->misc_wait, *done || !md_io_allowed(mdev), dt); 145*32db80f6SPhilipp Reisner if (dt == 0) 146*32db80f6SPhilipp Reisner dev_err(DEV, "meta-data IO operation timed out\n"); 147cdfda633SPhilipp Reisner } 148cdfda633SPhilipp Reisner 149b411b363SPhilipp Reisner static int _drbd_md_sync_page_io(struct drbd_conf *mdev, 150b411b363SPhilipp Reisner struct drbd_backing_dev *bdev, 151b411b363SPhilipp Reisner struct page *page, sector_t sector, 152b411b363SPhilipp Reisner int rw, int size) 153b411b363SPhilipp Reisner { 154b411b363SPhilipp Reisner struct bio *bio; 155ac29f403SAndreas Gruenbacher int err; 156b411b363SPhilipp Reisner 157cdfda633SPhilipp Reisner mdev->md_io.done = 0; 158cdfda633SPhilipp Reisner mdev->md_io.error = -ENODEV; 159b411b363SPhilipp Reisner 160a8a4e51eSPhilipp Reisner if ((rw & WRITE) && !test_bit(MD_NO_FUA, &mdev->flags)) 16186e1e98eSLars Ellenberg rw |= REQ_FUA | REQ_FLUSH; 162721a9602SJens Axboe rw |= REQ_SYNC; 163b411b363SPhilipp Reisner 164da4a75d2SLars Ellenberg bio = bio_alloc_drbd(GFP_NOIO); 165b411b363SPhilipp Reisner bio->bi_bdev = bdev->md_bdev; 166b411b363SPhilipp Reisner bio->bi_sector = sector; 167ac29f403SAndreas Gruenbacher err = -EIO; 168ac29f403SAndreas Gruenbacher if (bio_add_page(bio, page, size, 0) != size) 169b411b363SPhilipp Reisner goto out; 170cdfda633SPhilipp Reisner bio->bi_private = &mdev->md_io; 171b411b363SPhilipp Reisner bio->bi_end_io = drbd_md_io_complete; 172b411b363SPhilipp Reisner bio->bi_rw = rw; 173b411b363SPhilipp Reisner 174cdfda633SPhilipp Reisner if (!get_ldev_if_state(mdev, D_ATTACHING)) { /* Corresponding put_ldev in drbd_md_io_complete() */ 175cdfda633SPhilipp Reisner dev_err(DEV, "ASSERT FAILED: get_ldev_if_state() == 1 in _drbd_md_sync_page_io()\n"); 176cdfda633SPhilipp Reisner err = -ENODEV; 177cdfda633SPhilipp Reisner goto out; 178cdfda633SPhilipp Reisner } 179cdfda633SPhilipp Reisner 180cdfda633SPhilipp Reisner bio_get(bio); /* one bio_put() is in the completion handler */ 181cdfda633SPhilipp Reisner atomic_inc(&mdev->md_io_in_use); /* drbd_md_put_buffer() is in the completion handler */ 1820cf9d27eSAndreas Gruenbacher if (drbd_insert_fault(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD)) 183b411b363SPhilipp Reisner bio_endio(bio, -EIO); 184b411b363SPhilipp Reisner else 185b411b363SPhilipp Reisner submit_bio(rw, bio); 186*32db80f6SPhilipp Reisner wait_until_done_or_disk_failure(mdev, bdev, &mdev->md_io.done); 187ac29f403SAndreas Gruenbacher if (bio_flagged(bio, BIO_UPTODATE)) 188cdfda633SPhilipp Reisner err = mdev->md_io.error; 189b411b363SPhilipp Reisner 190b411b363SPhilipp Reisner out: 191b411b363SPhilipp Reisner bio_put(bio); 192ac29f403SAndreas Gruenbacher return err; 193b411b363SPhilipp Reisner } 194b411b363SPhilipp Reisner 195b411b363SPhilipp Reisner int drbd_md_sync_page_io(struct drbd_conf *mdev, struct drbd_backing_dev *bdev, 196b411b363SPhilipp Reisner sector_t sector, int rw) 197b411b363SPhilipp Reisner { 1983fbf4d21SAndreas Gruenbacher int err; 199b411b363SPhilipp Reisner struct page *iop = mdev->md_io_page; 200b411b363SPhilipp Reisner 201cdfda633SPhilipp Reisner D_ASSERT(atomic_read(&mdev->md_io_in_use) == 1); 202b411b363SPhilipp Reisner 203b411b363SPhilipp Reisner BUG_ON(!bdev->md_bdev); 204b411b363SPhilipp Reisner 2057ad651b5SLars Ellenberg dev_dbg(DEV, "meta_data io: %s [%d]:%s(,%llus,%s)\n", 2067ad651b5SLars Ellenberg current->comm, current->pid, __func__, 2077ad651b5SLars Ellenberg (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ"); 208b411b363SPhilipp Reisner 209b411b363SPhilipp Reisner if (sector < drbd_md_first_sector(bdev) || 2107ad651b5SLars Ellenberg sector + 7 > drbd_md_last_sector(bdev)) 211b411b363SPhilipp Reisner dev_alert(DEV, "%s [%d]:%s(,%llus,%s) out of range md access!\n", 212b411b363SPhilipp Reisner current->comm, current->pid, __func__, 213b411b363SPhilipp Reisner (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ"); 214b411b363SPhilipp Reisner 2153fbf4d21SAndreas Gruenbacher err = _drbd_md_sync_page_io(mdev, bdev, iop, sector, rw, MD_BLOCK_SIZE); 2163fbf4d21SAndreas Gruenbacher if (err) { 217935be260SAndreas Gruenbacher dev_err(DEV, "drbd_md_sync_page_io(,%llus,%s) failed with error %d\n", 218935be260SAndreas Gruenbacher (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ", err); 219b411b363SPhilipp Reisner } 2203fbf4d21SAndreas Gruenbacher return err; 221b411b363SPhilipp Reisner } 222b411b363SPhilipp Reisner 223b411b363SPhilipp Reisner static struct lc_element *_al_get(struct drbd_conf *mdev, unsigned int enr) 224b411b363SPhilipp Reisner { 225b411b363SPhilipp Reisner struct lc_element *al_ext; 226b411b363SPhilipp Reisner struct lc_element *tmp; 227f91ab628SPhilipp Reisner int wake; 228b411b363SPhilipp Reisner 229b411b363SPhilipp Reisner spin_lock_irq(&mdev->al_lock); 230b411b363SPhilipp Reisner tmp = lc_find(mdev->resync, enr/AL_EXT_PER_BM_SECT); 231b411b363SPhilipp Reisner if (unlikely(tmp != NULL)) { 232b411b363SPhilipp Reisner struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce); 233b411b363SPhilipp Reisner if (test_bit(BME_NO_WRITES, &bm_ext->flags)) { 234f91ab628SPhilipp Reisner wake = !test_and_set_bit(BME_PRIORITY, &bm_ext->flags); 235b411b363SPhilipp Reisner spin_unlock_irq(&mdev->al_lock); 236f91ab628SPhilipp Reisner if (wake) 237f91ab628SPhilipp Reisner wake_up(&mdev->al_wait); 238b411b363SPhilipp Reisner return NULL; 239b411b363SPhilipp Reisner } 240b411b363SPhilipp Reisner } 241b411b363SPhilipp Reisner al_ext = lc_get(mdev->act_log, enr); 242b411b363SPhilipp Reisner spin_unlock_irq(&mdev->al_lock); 243b411b363SPhilipp Reisner return al_ext; 244b411b363SPhilipp Reisner } 245b411b363SPhilipp Reisner 246181286adSLars Ellenberg void drbd_al_begin_io(struct drbd_conf *mdev, struct drbd_interval *i) 247b411b363SPhilipp Reisner { 2487726547eSLars Ellenberg /* for bios crossing activity log extent boundaries, 2497726547eSLars Ellenberg * we may need to activate two extents in one go */ 250e15766e9SLars Ellenberg unsigned first = i->sector >> (AL_EXTENT_SHIFT-9); 251e15766e9SLars Ellenberg unsigned last = (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9); 252e15766e9SLars Ellenberg unsigned enr; 2537dc1d67fSLars Ellenberg bool locked = false; 2547dc1d67fSLars Ellenberg 255b411b363SPhilipp Reisner 256b411b363SPhilipp Reisner D_ASSERT(atomic_read(&mdev->local_cnt) > 0); 257b411b363SPhilipp Reisner 258e15766e9SLars Ellenberg for (enr = first; enr <= last; enr++) 259e15766e9SLars Ellenberg wait_event(mdev->al_wait, _al_get(mdev, enr) != NULL); 260b411b363SPhilipp Reisner 2617dc1d67fSLars Ellenberg /* Serialize multiple transactions. 2627dc1d67fSLars Ellenberg * This uses test_and_set_bit, memory barrier is implicit. 2637dc1d67fSLars Ellenberg */ 2647dc1d67fSLars Ellenberg wait_event(mdev->al_wait, 2657dc1d67fSLars Ellenberg mdev->act_log->pending_changes == 0 || 2667dc1d67fSLars Ellenberg (locked = lc_try_lock_for_transaction(mdev->act_log))); 2677dc1d67fSLars Ellenberg 2687dc1d67fSLars Ellenberg if (locked) { 269b411b363SPhilipp Reisner /* drbd_al_write_transaction(mdev,al_ext,enr); 270b411b363SPhilipp Reisner * recurses into generic_make_request(), which 271b411b363SPhilipp Reisner * disallows recursion, bios being serialized on the 272b411b363SPhilipp Reisner * current->bio_tail list now. 273b411b363SPhilipp Reisner * we have to delegate updates to the activity log 274b411b363SPhilipp Reisner * to the worker thread. */ 2757ad651b5SLars Ellenberg 2767ad651b5SLars Ellenberg /* Double check: it may have been committed by someone else, 2777ad651b5SLars Ellenberg * while we have been waiting for the lock. */ 278e15766e9SLars Ellenberg if (mdev->act_log->pending_changes) { 279376694a0SPhilipp Reisner al_write_transaction(mdev); 280b411b363SPhilipp Reisner mdev->al_writ_cnt++; 281b411b363SPhilipp Reisner 282b411b363SPhilipp Reisner spin_lock_irq(&mdev->al_lock); 2837ad651b5SLars Ellenberg /* FIXME 2841b7ab15bSPhilipp Reisner if (err) 2857ad651b5SLars Ellenberg we need an "lc_cancel" here; 2867ad651b5SLars Ellenberg */ 28746a15bc3SLars Ellenberg lc_committed(mdev->act_log); 288b411b363SPhilipp Reisner spin_unlock_irq(&mdev->al_lock); 2897ad651b5SLars Ellenberg } 2907ad651b5SLars Ellenberg lc_unlock(mdev->act_log); 291b411b363SPhilipp Reisner wake_up(&mdev->al_wait); 292b411b363SPhilipp Reisner } 293b411b363SPhilipp Reisner } 294b411b363SPhilipp Reisner 295181286adSLars Ellenberg void drbd_al_complete_io(struct drbd_conf *mdev, struct drbd_interval *i) 296b411b363SPhilipp Reisner { 297e15766e9SLars Ellenberg /* for bios crossing activity log extent boundaries, 298e15766e9SLars Ellenberg * we may need to activate two extents in one go */ 299e15766e9SLars Ellenberg unsigned first = i->sector >> (AL_EXTENT_SHIFT-9); 300e15766e9SLars Ellenberg unsigned last = (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9); 301e15766e9SLars Ellenberg unsigned enr; 302b411b363SPhilipp Reisner struct lc_element *extent; 303b411b363SPhilipp Reisner unsigned long flags; 304b411b363SPhilipp Reisner 305b411b363SPhilipp Reisner spin_lock_irqsave(&mdev->al_lock, flags); 306b411b363SPhilipp Reisner 307e15766e9SLars Ellenberg for (enr = first; enr <= last; enr++) { 308b411b363SPhilipp Reisner extent = lc_find(mdev->act_log, enr); 309b411b363SPhilipp Reisner if (!extent) { 310b411b363SPhilipp Reisner dev_err(DEV, "al_complete_io() called on inactive extent %u\n", enr); 311e15766e9SLars Ellenberg continue; 312b411b363SPhilipp Reisner } 313376694a0SPhilipp Reisner lc_put(mdev->act_log, extent); 314e15766e9SLars Ellenberg } 315b411b363SPhilipp Reisner spin_unlock_irqrestore(&mdev->al_lock, flags); 316e15766e9SLars Ellenberg wake_up(&mdev->al_wait); 317b411b363SPhilipp Reisner } 318b411b363SPhilipp Reisner 31919f843aaSLars Ellenberg #if (PAGE_SHIFT + 3) < (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT) 32019f843aaSLars Ellenberg /* Currently BM_BLOCK_SHIFT, BM_EXT_SHIFT and AL_EXTENT_SHIFT 32119f843aaSLars Ellenberg * are still coupled, or assume too much about their relation. 32219f843aaSLars Ellenberg * Code below will not work if this is violated. 32319f843aaSLars Ellenberg * Will be cleaned up with some followup patch. 32419f843aaSLars Ellenberg */ 32519f843aaSLars Ellenberg # error FIXME 32619f843aaSLars Ellenberg #endif 32719f843aaSLars Ellenberg 32819f843aaSLars Ellenberg static unsigned int al_extent_to_bm_page(unsigned int al_enr) 32919f843aaSLars Ellenberg { 33019f843aaSLars Ellenberg return al_enr >> 33119f843aaSLars Ellenberg /* bit to page */ 33219f843aaSLars Ellenberg ((PAGE_SHIFT + 3) - 33319f843aaSLars Ellenberg /* al extent number to bit */ 33419f843aaSLars Ellenberg (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT)); 33519f843aaSLars Ellenberg } 33619f843aaSLars Ellenberg 33719f843aaSLars Ellenberg static unsigned int rs_extent_to_bm_page(unsigned int rs_enr) 33819f843aaSLars Ellenberg { 33919f843aaSLars Ellenberg return rs_enr >> 34019f843aaSLars Ellenberg /* bit to page */ 34119f843aaSLars Ellenberg ((PAGE_SHIFT + 3) - 342acb104c3SLars Ellenberg /* resync extent number to bit */ 34319f843aaSLars Ellenberg (BM_EXT_SHIFT - BM_BLOCK_SHIFT)); 34419f843aaSLars Ellenberg } 34519f843aaSLars Ellenberg 34699920dc5SAndreas Gruenbacher static int 3471b7ab15bSPhilipp Reisner _al_write_transaction(struct drbd_conf *mdev) 348b411b363SPhilipp Reisner { 3497ad651b5SLars Ellenberg struct al_transaction_on_disk *buffer; 3507ad651b5SLars Ellenberg struct lc_element *e; 351b411b363SPhilipp Reisner sector_t sector; 3527ad651b5SLars Ellenberg int i, mx; 3537ad651b5SLars Ellenberg unsigned extent_nr; 3547ad651b5SLars Ellenberg unsigned crc = 0; 3551b7ab15bSPhilipp Reisner int err = 0; 356b411b363SPhilipp Reisner 357b411b363SPhilipp Reisner if (!get_ldev(mdev)) { 3587ad651b5SLars Ellenberg dev_err(DEV, "disk is %s, cannot start al transaction\n", 3597ad651b5SLars Ellenberg drbd_disk_str(mdev->state.disk)); 3601b7ab15bSPhilipp Reisner return -EIO; 361b411b363SPhilipp Reisner } 362b411b363SPhilipp Reisner 3636719fb03SLars Ellenberg /* The bitmap write may have failed, causing a state change. */ 3646719fb03SLars Ellenberg if (mdev->state.disk < D_INCONSISTENT) { 3656719fb03SLars Ellenberg dev_err(DEV, 3667ad651b5SLars Ellenberg "disk is %s, cannot write al transaction\n", 3677ad651b5SLars Ellenberg drbd_disk_str(mdev->state.disk)); 3686719fb03SLars Ellenberg put_ldev(mdev); 3691b7ab15bSPhilipp Reisner return -EIO; 3706719fb03SLars Ellenberg } 3716719fb03SLars Ellenberg 372cdfda633SPhilipp Reisner buffer = drbd_md_get_buffer(mdev); /* protects md_io_buffer, al_tr_cycle, ... */ 373cdfda633SPhilipp Reisner if (!buffer) { 374cdfda633SPhilipp Reisner dev_err(DEV, "disk failed while waiting for md_io buffer\n"); 375cdfda633SPhilipp Reisner put_ldev(mdev); 3761b7ab15bSPhilipp Reisner return -ENODEV; 377cdfda633SPhilipp Reisner } 378b411b363SPhilipp Reisner 3797ad651b5SLars Ellenberg memset(buffer, 0, sizeof(*buffer)); 3807ad651b5SLars Ellenberg buffer->magic = cpu_to_be32(DRBD_AL_MAGIC); 381b411b363SPhilipp Reisner buffer->tr_number = cpu_to_be32(mdev->al_tr_number); 382b411b363SPhilipp Reisner 3837ad651b5SLars Ellenberg i = 0; 384b411b363SPhilipp Reisner 3857ad651b5SLars Ellenberg /* Even though no one can start to change this list 3867ad651b5SLars Ellenberg * once we set the LC_LOCKED -- from drbd_al_begin_io(), 3877ad651b5SLars Ellenberg * lc_try_lock_for_transaction() --, someone may still 3887ad651b5SLars Ellenberg * be in the process of changing it. */ 3897ad651b5SLars Ellenberg spin_lock_irq(&mdev->al_lock); 3907ad651b5SLars Ellenberg list_for_each_entry(e, &mdev->act_log->to_be_changed, list) { 3917ad651b5SLars Ellenberg if (i == AL_UPDATES_PER_TRANSACTION) { 3927ad651b5SLars Ellenberg i++; 3937ad651b5SLars Ellenberg break; 3947ad651b5SLars Ellenberg } 3957ad651b5SLars Ellenberg buffer->update_slot_nr[i] = cpu_to_be16(e->lc_index); 3967ad651b5SLars Ellenberg buffer->update_extent_nr[i] = cpu_to_be32(e->lc_new_number); 3977ad651b5SLars Ellenberg if (e->lc_number != LC_FREE) 3987ad651b5SLars Ellenberg drbd_bm_mark_for_writeout(mdev, 3997ad651b5SLars Ellenberg al_extent_to_bm_page(e->lc_number)); 4007ad651b5SLars Ellenberg i++; 4017ad651b5SLars Ellenberg } 4027ad651b5SLars Ellenberg spin_unlock_irq(&mdev->al_lock); 4037ad651b5SLars Ellenberg BUG_ON(i > AL_UPDATES_PER_TRANSACTION); 404b411b363SPhilipp Reisner 4057ad651b5SLars Ellenberg buffer->n_updates = cpu_to_be16(i); 4067ad651b5SLars Ellenberg for ( ; i < AL_UPDATES_PER_TRANSACTION; i++) { 4077ad651b5SLars Ellenberg buffer->update_slot_nr[i] = cpu_to_be16(-1); 4087ad651b5SLars Ellenberg buffer->update_extent_nr[i] = cpu_to_be32(LC_FREE); 4097ad651b5SLars Ellenberg } 410b411b363SPhilipp Reisner 4117ad651b5SLars Ellenberg buffer->context_size = cpu_to_be16(mdev->act_log->nr_elements); 4127ad651b5SLars Ellenberg buffer->context_start_slot_nr = cpu_to_be16(mdev->al_tr_cycle); 4137ad651b5SLars Ellenberg 4147ad651b5SLars Ellenberg mx = min_t(int, AL_CONTEXT_PER_TRANSACTION, 415b411b363SPhilipp Reisner mdev->act_log->nr_elements - mdev->al_tr_cycle); 416b411b363SPhilipp Reisner for (i = 0; i < mx; i++) { 417b411b363SPhilipp Reisner unsigned idx = mdev->al_tr_cycle + i; 418b411b363SPhilipp Reisner extent_nr = lc_element_by_index(mdev->act_log, idx)->lc_number; 4197ad651b5SLars Ellenberg buffer->context[i] = cpu_to_be32(extent_nr); 420b411b363SPhilipp Reisner } 4217ad651b5SLars Ellenberg for (; i < AL_CONTEXT_PER_TRANSACTION; i++) 4227ad651b5SLars Ellenberg buffer->context[i] = cpu_to_be32(LC_FREE); 4237ad651b5SLars Ellenberg 4247ad651b5SLars Ellenberg mdev->al_tr_cycle += AL_CONTEXT_PER_TRANSACTION; 425b411b363SPhilipp Reisner if (mdev->al_tr_cycle >= mdev->act_log->nr_elements) 426b411b363SPhilipp Reisner mdev->al_tr_cycle = 0; 427b411b363SPhilipp Reisner 428b411b363SPhilipp Reisner sector = mdev->ldev->md.md_offset 4297ad651b5SLars Ellenberg + mdev->ldev->md.al_offset 4307ad651b5SLars Ellenberg + mdev->al_tr_pos * (MD_BLOCK_SIZE>>9); 431b411b363SPhilipp Reisner 4327ad651b5SLars Ellenberg crc = crc32c(0, buffer, 4096); 4337ad651b5SLars Ellenberg buffer->crc32c = cpu_to_be32(crc); 4347ad651b5SLars Ellenberg 4357ad651b5SLars Ellenberg if (drbd_bm_write_hinted(mdev)) 4361b7ab15bSPhilipp Reisner err = -EIO; 4377ad651b5SLars Ellenberg /* drbd_chk_io_error done already */ 4383fbf4d21SAndreas Gruenbacher else if (drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE)) { 4391b7ab15bSPhilipp Reisner err = -EIO; 44081e84650SAndreas Gruenbacher drbd_chk_io_error(mdev, 1, true); 4417ad651b5SLars Ellenberg } else { 4427ad651b5SLars Ellenberg /* advance ringbuffer position and transaction counter */ 4437ad651b5SLars Ellenberg mdev->al_tr_pos = (mdev->al_tr_pos + 1) % (MD_AL_SECTORS*512/MD_BLOCK_SIZE); 444b411b363SPhilipp Reisner mdev->al_tr_number++; 4457ad651b5SLars Ellenberg } 446b411b363SPhilipp Reisner 447cdfda633SPhilipp Reisner drbd_md_put_buffer(mdev); 448b411b363SPhilipp Reisner put_ldev(mdev); 449b411b363SPhilipp Reisner 4501b7ab15bSPhilipp Reisner return err; 4511b7ab15bSPhilipp Reisner } 4521b7ab15bSPhilipp Reisner 4531b7ab15bSPhilipp Reisner 4541b7ab15bSPhilipp Reisner static int w_al_write_transaction(struct drbd_work *w, int unused) 4551b7ab15bSPhilipp Reisner { 4561b7ab15bSPhilipp Reisner struct update_al_work *aw = container_of(w, struct update_al_work, w); 4571b7ab15bSPhilipp Reisner struct drbd_conf *mdev = w->mdev; 4581b7ab15bSPhilipp Reisner int err; 4591b7ab15bSPhilipp Reisner 4601b7ab15bSPhilipp Reisner err = _al_write_transaction(mdev); 4611b7ab15bSPhilipp Reisner aw->err = err; 4621b7ab15bSPhilipp Reisner complete(&aw->event); 4631b7ab15bSPhilipp Reisner 4641b7ab15bSPhilipp Reisner return err != -EIO ? err : 0; 4651b7ab15bSPhilipp Reisner } 4661b7ab15bSPhilipp Reisner 4671b7ab15bSPhilipp Reisner /* Calls from worker context (see w_restart_disk_io()) need to write the 4681b7ab15bSPhilipp Reisner transaction directly. Others came through generic_make_request(), 4691b7ab15bSPhilipp Reisner those need to delegate it to the worker. */ 4701b7ab15bSPhilipp Reisner static int al_write_transaction(struct drbd_conf *mdev) 4711b7ab15bSPhilipp Reisner { 4721b7ab15bSPhilipp Reisner struct update_al_work al_work; 4731b7ab15bSPhilipp Reisner 4741b7ab15bSPhilipp Reisner if (current == mdev->tconn->worker.task) 4751b7ab15bSPhilipp Reisner return _al_write_transaction(mdev); 4761b7ab15bSPhilipp Reisner 4771b7ab15bSPhilipp Reisner init_completion(&al_work.event); 4781b7ab15bSPhilipp Reisner al_work.w.cb = w_al_write_transaction; 4791b7ab15bSPhilipp Reisner al_work.w.mdev = mdev; 4801b7ab15bSPhilipp Reisner drbd_queue_work_front(&mdev->tconn->data.work, &al_work.w); 4811b7ab15bSPhilipp Reisner wait_for_completion(&al_work.event); 4821b7ab15bSPhilipp Reisner 4831b7ab15bSPhilipp Reisner return al_work.err; 484b411b363SPhilipp Reisner } 485b411b363SPhilipp Reisner 486b411b363SPhilipp Reisner static int _try_lc_del(struct drbd_conf *mdev, struct lc_element *al_ext) 487b411b363SPhilipp Reisner { 488b411b363SPhilipp Reisner int rv; 489b411b363SPhilipp Reisner 490b411b363SPhilipp Reisner spin_lock_irq(&mdev->al_lock); 491b411b363SPhilipp Reisner rv = (al_ext->refcnt == 0); 492b411b363SPhilipp Reisner if (likely(rv)) 493b411b363SPhilipp Reisner lc_del(mdev->act_log, al_ext); 494b411b363SPhilipp Reisner spin_unlock_irq(&mdev->al_lock); 495b411b363SPhilipp Reisner 496b411b363SPhilipp Reisner return rv; 497b411b363SPhilipp Reisner } 498b411b363SPhilipp Reisner 499b411b363SPhilipp Reisner /** 500b411b363SPhilipp Reisner * drbd_al_shrink() - Removes all active extents form the activity log 501b411b363SPhilipp Reisner * @mdev: DRBD device. 502b411b363SPhilipp Reisner * 503b411b363SPhilipp Reisner * Removes all active extents form the activity log, waiting until 504b411b363SPhilipp Reisner * the reference count of each entry dropped to 0 first, of course. 505b411b363SPhilipp Reisner * 506b411b363SPhilipp Reisner * You need to lock mdev->act_log with lc_try_lock() / lc_unlock() 507b411b363SPhilipp Reisner */ 508b411b363SPhilipp Reisner void drbd_al_shrink(struct drbd_conf *mdev) 509b411b363SPhilipp Reisner { 510b411b363SPhilipp Reisner struct lc_element *al_ext; 511b411b363SPhilipp Reisner int i; 512b411b363SPhilipp Reisner 51346a15bc3SLars Ellenberg D_ASSERT(test_bit(__LC_LOCKED, &mdev->act_log->flags)); 514b411b363SPhilipp Reisner 515b411b363SPhilipp Reisner for (i = 0; i < mdev->act_log->nr_elements; i++) { 516b411b363SPhilipp Reisner al_ext = lc_element_by_index(mdev->act_log, i); 517b411b363SPhilipp Reisner if (al_ext->lc_number == LC_FREE) 518b411b363SPhilipp Reisner continue; 519b411b363SPhilipp Reisner wait_event(mdev->al_wait, _try_lc_del(mdev, al_ext)); 520b411b363SPhilipp Reisner } 521b411b363SPhilipp Reisner 522b411b363SPhilipp Reisner wake_up(&mdev->al_wait); 523b411b363SPhilipp Reisner } 524b411b363SPhilipp Reisner 52599920dc5SAndreas Gruenbacher static int w_update_odbm(struct drbd_work *w, int unused) 526b411b363SPhilipp Reisner { 527b411b363SPhilipp Reisner struct update_odbm_work *udw = container_of(w, struct update_odbm_work, w); 52800d56944SPhilipp Reisner struct drbd_conf *mdev = w->mdev; 5293b98c0c2SLars Ellenberg struct sib_info sib = { .sib_reason = SIB_SYNC_PROGRESS, }; 530b411b363SPhilipp Reisner 531b411b363SPhilipp Reisner if (!get_ldev(mdev)) { 532b411b363SPhilipp Reisner if (__ratelimit(&drbd_ratelimit_state)) 533b411b363SPhilipp Reisner dev_warn(DEV, "Can not update on disk bitmap, local IO disabled.\n"); 534b411b363SPhilipp Reisner kfree(udw); 53599920dc5SAndreas Gruenbacher return 0; 536b411b363SPhilipp Reisner } 537b411b363SPhilipp Reisner 53819f843aaSLars Ellenberg drbd_bm_write_page(mdev, rs_extent_to_bm_page(udw->enr)); 539b411b363SPhilipp Reisner put_ldev(mdev); 540b411b363SPhilipp Reisner 541b411b363SPhilipp Reisner kfree(udw); 542b411b363SPhilipp Reisner 543b411b363SPhilipp Reisner if (drbd_bm_total_weight(mdev) <= mdev->rs_failed) { 544b411b363SPhilipp Reisner switch (mdev->state.conn) { 545b411b363SPhilipp Reisner case C_SYNC_SOURCE: case C_SYNC_TARGET: 546b411b363SPhilipp Reisner case C_PAUSED_SYNC_S: case C_PAUSED_SYNC_T: 547b411b363SPhilipp Reisner drbd_resync_finished(mdev); 548b411b363SPhilipp Reisner default: 549b411b363SPhilipp Reisner /* nothing to do */ 550b411b363SPhilipp Reisner break; 551b411b363SPhilipp Reisner } 552b411b363SPhilipp Reisner } 5533b98c0c2SLars Ellenberg drbd_bcast_event(mdev, &sib); 554b411b363SPhilipp Reisner 55599920dc5SAndreas Gruenbacher return 0; 556b411b363SPhilipp Reisner } 557b411b363SPhilipp Reisner 558b411b363SPhilipp Reisner 559b411b363SPhilipp Reisner /* ATTENTION. The AL's extents are 4MB each, while the extents in the 560b411b363SPhilipp Reisner * resync LRU-cache are 16MB each. 561b411b363SPhilipp Reisner * The caller of this function has to hold an get_ldev() reference. 562b411b363SPhilipp Reisner * 563b411b363SPhilipp Reisner * TODO will be obsoleted once we have a caching lru of the on disk bitmap 564b411b363SPhilipp Reisner */ 565b411b363SPhilipp Reisner static void drbd_try_clear_on_disk_bm(struct drbd_conf *mdev, sector_t sector, 566b411b363SPhilipp Reisner int count, int success) 567b411b363SPhilipp Reisner { 568b411b363SPhilipp Reisner struct lc_element *e; 569b411b363SPhilipp Reisner struct update_odbm_work *udw; 570b411b363SPhilipp Reisner 571b411b363SPhilipp Reisner unsigned int enr; 572b411b363SPhilipp Reisner 573b411b363SPhilipp Reisner D_ASSERT(atomic_read(&mdev->local_cnt)); 574b411b363SPhilipp Reisner 575b411b363SPhilipp Reisner /* I simply assume that a sector/size pair never crosses 576b411b363SPhilipp Reisner * a 16 MB extent border. (Currently this is true...) */ 577b411b363SPhilipp Reisner enr = BM_SECT_TO_EXT(sector); 578b411b363SPhilipp Reisner 579b411b363SPhilipp Reisner e = lc_get(mdev->resync, enr); 580b411b363SPhilipp Reisner if (e) { 581b411b363SPhilipp Reisner struct bm_extent *ext = lc_entry(e, struct bm_extent, lce); 582b411b363SPhilipp Reisner if (ext->lce.lc_number == enr) { 583b411b363SPhilipp Reisner if (success) 584b411b363SPhilipp Reisner ext->rs_left -= count; 585b411b363SPhilipp Reisner else 586b411b363SPhilipp Reisner ext->rs_failed += count; 587b411b363SPhilipp Reisner if (ext->rs_left < ext->rs_failed) { 588975b2979SPhilipp Reisner dev_warn(DEV, "BAD! sector=%llus enr=%u rs_left=%d " 589975b2979SPhilipp Reisner "rs_failed=%d count=%d cstate=%s\n", 590b411b363SPhilipp Reisner (unsigned long long)sector, 591b411b363SPhilipp Reisner ext->lce.lc_number, ext->rs_left, 592975b2979SPhilipp Reisner ext->rs_failed, count, 593975b2979SPhilipp Reisner drbd_conn_str(mdev->state.conn)); 594b411b363SPhilipp Reisner 595975b2979SPhilipp Reisner /* We don't expect to be able to clear more bits 596975b2979SPhilipp Reisner * than have been set when we originally counted 597975b2979SPhilipp Reisner * the set bits to cache that value in ext->rs_left. 598975b2979SPhilipp Reisner * Whatever the reason (disconnect during resync, 599975b2979SPhilipp Reisner * delayed local completion of an application write), 600975b2979SPhilipp Reisner * try to fix it up by recounting here. */ 601975b2979SPhilipp Reisner ext->rs_left = drbd_bm_e_weight(mdev, enr); 602b411b363SPhilipp Reisner } 603b411b363SPhilipp Reisner } else { 604b411b363SPhilipp Reisner /* Normally this element should be in the cache, 605b411b363SPhilipp Reisner * since drbd_rs_begin_io() pulled it already in. 606b411b363SPhilipp Reisner * 607b411b363SPhilipp Reisner * But maybe an application write finished, and we set 608b411b363SPhilipp Reisner * something outside the resync lru_cache in sync. 609b411b363SPhilipp Reisner */ 610b411b363SPhilipp Reisner int rs_left = drbd_bm_e_weight(mdev, enr); 611b411b363SPhilipp Reisner if (ext->flags != 0) { 612b411b363SPhilipp Reisner dev_warn(DEV, "changing resync lce: %d[%u;%02lx]" 613b411b363SPhilipp Reisner " -> %d[%u;00]\n", 614b411b363SPhilipp Reisner ext->lce.lc_number, ext->rs_left, 615b411b363SPhilipp Reisner ext->flags, enr, rs_left); 616b411b363SPhilipp Reisner ext->flags = 0; 617b411b363SPhilipp Reisner } 618b411b363SPhilipp Reisner if (ext->rs_failed) { 619b411b363SPhilipp Reisner dev_warn(DEV, "Kicking resync_lru element enr=%u " 620b411b363SPhilipp Reisner "out with rs_failed=%d\n", 621b411b363SPhilipp Reisner ext->lce.lc_number, ext->rs_failed); 622b411b363SPhilipp Reisner } 623b411b363SPhilipp Reisner ext->rs_left = rs_left; 624b411b363SPhilipp Reisner ext->rs_failed = success ? 0 : count; 62546a15bc3SLars Ellenberg /* we don't keep a persistent log of the resync lru, 62646a15bc3SLars Ellenberg * we can commit any change right away. */ 62746a15bc3SLars Ellenberg lc_committed(mdev->resync); 628b411b363SPhilipp Reisner } 629b411b363SPhilipp Reisner lc_put(mdev->resync, &ext->lce); 630b411b363SPhilipp Reisner /* no race, we are within the al_lock! */ 631b411b363SPhilipp Reisner 632b411b363SPhilipp Reisner if (ext->rs_left == ext->rs_failed) { 633b411b363SPhilipp Reisner ext->rs_failed = 0; 634b411b363SPhilipp Reisner 635b411b363SPhilipp Reisner udw = kmalloc(sizeof(*udw), GFP_ATOMIC); 636b411b363SPhilipp Reisner if (udw) { 637b411b363SPhilipp Reisner udw->enr = ext->lce.lc_number; 638b411b363SPhilipp Reisner udw->w.cb = w_update_odbm; 639a21e9298SPhilipp Reisner udw->w.mdev = mdev; 640e42325a5SPhilipp Reisner drbd_queue_work_front(&mdev->tconn->data.work, &udw->w); 641b411b363SPhilipp Reisner } else { 642b411b363SPhilipp Reisner dev_warn(DEV, "Could not kmalloc an udw\n"); 643b411b363SPhilipp Reisner } 644b411b363SPhilipp Reisner } 645b411b363SPhilipp Reisner } else { 646b411b363SPhilipp Reisner dev_err(DEV, "lc_get() failed! locked=%d/%d flags=%lu\n", 647b411b363SPhilipp Reisner mdev->resync_locked, 648b411b363SPhilipp Reisner mdev->resync->nr_elements, 649b411b363SPhilipp Reisner mdev->resync->flags); 650b411b363SPhilipp Reisner } 651b411b363SPhilipp Reisner } 652b411b363SPhilipp Reisner 653c6ea14dfSLars Ellenberg void drbd_advance_rs_marks(struct drbd_conf *mdev, unsigned long still_to_go) 654c6ea14dfSLars Ellenberg { 655c6ea14dfSLars Ellenberg unsigned long now = jiffies; 656c6ea14dfSLars Ellenberg unsigned long last = mdev->rs_mark_time[mdev->rs_last_mark]; 657c6ea14dfSLars Ellenberg int next = (mdev->rs_last_mark + 1) % DRBD_SYNC_MARKS; 658c6ea14dfSLars Ellenberg if (time_after_eq(now, last + DRBD_SYNC_MARK_STEP)) { 659c6ea14dfSLars Ellenberg if (mdev->rs_mark_left[mdev->rs_last_mark] != still_to_go && 660c6ea14dfSLars Ellenberg mdev->state.conn != C_PAUSED_SYNC_T && 661c6ea14dfSLars Ellenberg mdev->state.conn != C_PAUSED_SYNC_S) { 662c6ea14dfSLars Ellenberg mdev->rs_mark_time[next] = now; 663c6ea14dfSLars Ellenberg mdev->rs_mark_left[next] = still_to_go; 664c6ea14dfSLars Ellenberg mdev->rs_last_mark = next; 665c6ea14dfSLars Ellenberg } 666c6ea14dfSLars Ellenberg } 667c6ea14dfSLars Ellenberg } 668c6ea14dfSLars Ellenberg 669b411b363SPhilipp Reisner /* clear the bit corresponding to the piece of storage in question: 670b411b363SPhilipp Reisner * size byte of data starting from sector. Only clear a bits of the affected 671b411b363SPhilipp Reisner * one ore more _aligned_ BM_BLOCK_SIZE blocks. 672b411b363SPhilipp Reisner * 673b411b363SPhilipp Reisner * called by worker on C_SYNC_TARGET and receiver on SyncSource. 674b411b363SPhilipp Reisner * 675b411b363SPhilipp Reisner */ 676b411b363SPhilipp Reisner void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector, int size, 677b411b363SPhilipp Reisner const char *file, const unsigned int line) 678b411b363SPhilipp Reisner { 679b411b363SPhilipp Reisner /* Is called from worker and receiver context _only_ */ 680b411b363SPhilipp Reisner unsigned long sbnr, ebnr, lbnr; 681b411b363SPhilipp Reisner unsigned long count = 0; 682b411b363SPhilipp Reisner sector_t esector, nr_sectors; 683b411b363SPhilipp Reisner int wake_up = 0; 684b411b363SPhilipp Reisner unsigned long flags; 685b411b363SPhilipp Reisner 686c670a398SAndreas Gruenbacher if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) { 687b411b363SPhilipp Reisner dev_err(DEV, "drbd_set_in_sync: sector=%llus size=%d nonsense!\n", 688b411b363SPhilipp Reisner (unsigned long long)sector, size); 689b411b363SPhilipp Reisner return; 690b411b363SPhilipp Reisner } 691b411b363SPhilipp Reisner nr_sectors = drbd_get_capacity(mdev->this_bdev); 692b411b363SPhilipp Reisner esector = sector + (size >> 9) - 1; 693b411b363SPhilipp Reisner 694841ce241SAndreas Gruenbacher if (!expect(sector < nr_sectors)) 695841ce241SAndreas Gruenbacher return; 696841ce241SAndreas Gruenbacher if (!expect(esector < nr_sectors)) 697841ce241SAndreas Gruenbacher esector = nr_sectors - 1; 698b411b363SPhilipp Reisner 699b411b363SPhilipp Reisner lbnr = BM_SECT_TO_BIT(nr_sectors-1); 700b411b363SPhilipp Reisner 701b411b363SPhilipp Reisner /* we clear it (in sync). 702b411b363SPhilipp Reisner * round up start sector, round down end sector. we make sure we only 703b411b363SPhilipp Reisner * clear full, aligned, BM_BLOCK_SIZE (4K) blocks */ 704b411b363SPhilipp Reisner if (unlikely(esector < BM_SECT_PER_BIT-1)) 705b411b363SPhilipp Reisner return; 706b411b363SPhilipp Reisner if (unlikely(esector == (nr_sectors-1))) 707b411b363SPhilipp Reisner ebnr = lbnr; 708b411b363SPhilipp Reisner else 709b411b363SPhilipp Reisner ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1)); 710b411b363SPhilipp Reisner sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1); 711b411b363SPhilipp Reisner 712b411b363SPhilipp Reisner if (sbnr > ebnr) 713b411b363SPhilipp Reisner return; 714b411b363SPhilipp Reisner 715b411b363SPhilipp Reisner /* 716b411b363SPhilipp Reisner * ok, (capacity & 7) != 0 sometimes, but who cares... 717b411b363SPhilipp Reisner * we count rs_{total,left} in bits, not sectors. 718b411b363SPhilipp Reisner */ 719b411b363SPhilipp Reisner count = drbd_bm_clear_bits(mdev, sbnr, ebnr); 7201d7734a0SLars Ellenberg if (count && get_ldev(mdev)) { 721c6ea14dfSLars Ellenberg drbd_advance_rs_marks(mdev, drbd_bm_total_weight(mdev)); 7221d7734a0SLars Ellenberg spin_lock_irqsave(&mdev->al_lock, flags); 72381e84650SAndreas Gruenbacher drbd_try_clear_on_disk_bm(mdev, sector, count, true); 7241d7734a0SLars Ellenberg spin_unlock_irqrestore(&mdev->al_lock, flags); 7251d7734a0SLars Ellenberg 726b411b363SPhilipp Reisner /* just wake_up unconditional now, various lc_chaged(), 727b411b363SPhilipp Reisner * lc_put() in drbd_try_clear_on_disk_bm(). */ 728b411b363SPhilipp Reisner wake_up = 1; 7291d7734a0SLars Ellenberg put_ldev(mdev); 730b411b363SPhilipp Reisner } 731b411b363SPhilipp Reisner if (wake_up) 732b411b363SPhilipp Reisner wake_up(&mdev->al_wait); 733b411b363SPhilipp Reisner } 734b411b363SPhilipp Reisner 735b411b363SPhilipp Reisner /* 736b411b363SPhilipp Reisner * this is intended to set one request worth of data out of sync. 737b411b363SPhilipp Reisner * affects at least 1 bit, 7381816a2b4SLars Ellenberg * and at most 1+DRBD_MAX_BIO_SIZE/BM_BLOCK_SIZE bits. 739b411b363SPhilipp Reisner * 740b411b363SPhilipp Reisner * called by tl_clear and drbd_send_dblock (==drbd_make_request). 741b411b363SPhilipp Reisner * so this can be _any_ process. 742b411b363SPhilipp Reisner */ 74373a01a18SPhilipp Reisner int __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector, int size, 744b411b363SPhilipp Reisner const char *file, const unsigned int line) 745b411b363SPhilipp Reisner { 746376694a0SPhilipp Reisner unsigned long sbnr, ebnr, flags; 747b411b363SPhilipp Reisner sector_t esector, nr_sectors; 74873a01a18SPhilipp Reisner unsigned int enr, count = 0; 749b411b363SPhilipp Reisner struct lc_element *e; 750b411b363SPhilipp Reisner 751c670a398SAndreas Gruenbacher if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) { 752b411b363SPhilipp Reisner dev_err(DEV, "sector: %llus, size: %d\n", 753b411b363SPhilipp Reisner (unsigned long long)sector, size); 75473a01a18SPhilipp Reisner return 0; 755b411b363SPhilipp Reisner } 756b411b363SPhilipp Reisner 757b411b363SPhilipp Reisner if (!get_ldev(mdev)) 75873a01a18SPhilipp Reisner return 0; /* no disk, no metadata, no bitmap to set bits in */ 759b411b363SPhilipp Reisner 760b411b363SPhilipp Reisner nr_sectors = drbd_get_capacity(mdev->this_bdev); 761b411b363SPhilipp Reisner esector = sector + (size >> 9) - 1; 762b411b363SPhilipp Reisner 763841ce241SAndreas Gruenbacher if (!expect(sector < nr_sectors)) 764b411b363SPhilipp Reisner goto out; 765841ce241SAndreas Gruenbacher if (!expect(esector < nr_sectors)) 766841ce241SAndreas Gruenbacher esector = nr_sectors - 1; 767b411b363SPhilipp Reisner 768b411b363SPhilipp Reisner /* we set it out of sync, 769b411b363SPhilipp Reisner * we do not need to round anything here */ 770b411b363SPhilipp Reisner sbnr = BM_SECT_TO_BIT(sector); 771b411b363SPhilipp Reisner ebnr = BM_SECT_TO_BIT(esector); 772b411b363SPhilipp Reisner 773b411b363SPhilipp Reisner /* ok, (capacity & 7) != 0 sometimes, but who cares... 774b411b363SPhilipp Reisner * we count rs_{total,left} in bits, not sectors. */ 775b411b363SPhilipp Reisner spin_lock_irqsave(&mdev->al_lock, flags); 776b411b363SPhilipp Reisner count = drbd_bm_set_bits(mdev, sbnr, ebnr); 777b411b363SPhilipp Reisner 778b411b363SPhilipp Reisner enr = BM_SECT_TO_EXT(sector); 779b411b363SPhilipp Reisner e = lc_find(mdev->resync, enr); 780b411b363SPhilipp Reisner if (e) 781b411b363SPhilipp Reisner lc_entry(e, struct bm_extent, lce)->rs_left += count; 782b411b363SPhilipp Reisner spin_unlock_irqrestore(&mdev->al_lock, flags); 783b411b363SPhilipp Reisner 784b411b363SPhilipp Reisner out: 785b411b363SPhilipp Reisner put_ldev(mdev); 78673a01a18SPhilipp Reisner 78773a01a18SPhilipp Reisner return count; 788b411b363SPhilipp Reisner } 789b411b363SPhilipp Reisner 790b411b363SPhilipp Reisner static 791b411b363SPhilipp Reisner struct bm_extent *_bme_get(struct drbd_conf *mdev, unsigned int enr) 792b411b363SPhilipp Reisner { 793b411b363SPhilipp Reisner struct lc_element *e; 794b411b363SPhilipp Reisner struct bm_extent *bm_ext; 795b411b363SPhilipp Reisner int wakeup = 0; 796b411b363SPhilipp Reisner unsigned long rs_flags; 797b411b363SPhilipp Reisner 798b411b363SPhilipp Reisner spin_lock_irq(&mdev->al_lock); 799b411b363SPhilipp Reisner if (mdev->resync_locked > mdev->resync->nr_elements/2) { 800b411b363SPhilipp Reisner spin_unlock_irq(&mdev->al_lock); 801b411b363SPhilipp Reisner return NULL; 802b411b363SPhilipp Reisner } 803b411b363SPhilipp Reisner e = lc_get(mdev->resync, enr); 804b411b363SPhilipp Reisner bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; 805b411b363SPhilipp Reisner if (bm_ext) { 806b411b363SPhilipp Reisner if (bm_ext->lce.lc_number != enr) { 807b411b363SPhilipp Reisner bm_ext->rs_left = drbd_bm_e_weight(mdev, enr); 808b411b363SPhilipp Reisner bm_ext->rs_failed = 0; 80946a15bc3SLars Ellenberg lc_committed(mdev->resync); 810b411b363SPhilipp Reisner wakeup = 1; 811b411b363SPhilipp Reisner } 812b411b363SPhilipp Reisner if (bm_ext->lce.refcnt == 1) 813b411b363SPhilipp Reisner mdev->resync_locked++; 814b411b363SPhilipp Reisner set_bit(BME_NO_WRITES, &bm_ext->flags); 815b411b363SPhilipp Reisner } 816b411b363SPhilipp Reisner rs_flags = mdev->resync->flags; 817b411b363SPhilipp Reisner spin_unlock_irq(&mdev->al_lock); 818b411b363SPhilipp Reisner if (wakeup) 819b411b363SPhilipp Reisner wake_up(&mdev->al_wait); 820b411b363SPhilipp Reisner 821b411b363SPhilipp Reisner if (!bm_ext) { 822b411b363SPhilipp Reisner if (rs_flags & LC_STARVING) 823b411b363SPhilipp Reisner dev_warn(DEV, "Have to wait for element" 824b411b363SPhilipp Reisner " (resync LRU too small?)\n"); 82546a15bc3SLars Ellenberg BUG_ON(rs_flags & LC_LOCKED); 826b411b363SPhilipp Reisner } 827b411b363SPhilipp Reisner 828b411b363SPhilipp Reisner return bm_ext; 829b411b363SPhilipp Reisner } 830b411b363SPhilipp Reisner 831b411b363SPhilipp Reisner static int _is_in_al(struct drbd_conf *mdev, unsigned int enr) 832b411b363SPhilipp Reisner { 83346a15bc3SLars Ellenberg int rv; 834b411b363SPhilipp Reisner 835b411b363SPhilipp Reisner spin_lock_irq(&mdev->al_lock); 83646a15bc3SLars Ellenberg rv = lc_is_used(mdev->act_log, enr); 837b411b363SPhilipp Reisner spin_unlock_irq(&mdev->al_lock); 838b411b363SPhilipp Reisner 839b411b363SPhilipp Reisner return rv; 840b411b363SPhilipp Reisner } 841b411b363SPhilipp Reisner 842b411b363SPhilipp Reisner /** 843b411b363SPhilipp Reisner * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED 844b411b363SPhilipp Reisner * @mdev: DRBD device. 845b411b363SPhilipp Reisner * @sector: The sector number. 846b411b363SPhilipp Reisner * 84780a40e43SLars Ellenberg * This functions sleeps on al_wait. Returns 0 on success, -EINTR if interrupted. 848b411b363SPhilipp Reisner */ 849b411b363SPhilipp Reisner int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector) 850b411b363SPhilipp Reisner { 851b411b363SPhilipp Reisner unsigned int enr = BM_SECT_TO_EXT(sector); 852b411b363SPhilipp Reisner struct bm_extent *bm_ext; 853b411b363SPhilipp Reisner int i, sig; 854f91ab628SPhilipp Reisner int sa = 200; /* Step aside 200 times, then grab the extent and let app-IO wait. 855f91ab628SPhilipp Reisner 200 times -> 20 seconds. */ 856b411b363SPhilipp Reisner 857f91ab628SPhilipp Reisner retry: 858b411b363SPhilipp Reisner sig = wait_event_interruptible(mdev->al_wait, 859b411b363SPhilipp Reisner (bm_ext = _bme_get(mdev, enr))); 860b411b363SPhilipp Reisner if (sig) 86180a40e43SLars Ellenberg return -EINTR; 862b411b363SPhilipp Reisner 863b411b363SPhilipp Reisner if (test_bit(BME_LOCKED, &bm_ext->flags)) 86480a40e43SLars Ellenberg return 0; 865b411b363SPhilipp Reisner 866b411b363SPhilipp Reisner for (i = 0; i < AL_EXT_PER_BM_SECT; i++) { 867b411b363SPhilipp Reisner sig = wait_event_interruptible(mdev->al_wait, 868f91ab628SPhilipp Reisner !_is_in_al(mdev, enr * AL_EXT_PER_BM_SECT + i) || 869c507f46fSPhilipp Reisner test_bit(BME_PRIORITY, &bm_ext->flags)); 870f91ab628SPhilipp Reisner 871f91ab628SPhilipp Reisner if (sig || (test_bit(BME_PRIORITY, &bm_ext->flags) && sa)) { 872b411b363SPhilipp Reisner spin_lock_irq(&mdev->al_lock); 873b411b363SPhilipp Reisner if (lc_put(mdev->resync, &bm_ext->lce) == 0) { 874f91ab628SPhilipp Reisner bm_ext->flags = 0; /* clears BME_NO_WRITES and eventually BME_PRIORITY */ 875b411b363SPhilipp Reisner mdev->resync_locked--; 876b411b363SPhilipp Reisner wake_up(&mdev->al_wait); 877b411b363SPhilipp Reisner } 878b411b363SPhilipp Reisner spin_unlock_irq(&mdev->al_lock); 879f91ab628SPhilipp Reisner if (sig) 88080a40e43SLars Ellenberg return -EINTR; 881f91ab628SPhilipp Reisner if (schedule_timeout_interruptible(HZ/10)) 882f91ab628SPhilipp Reisner return -EINTR; 883c507f46fSPhilipp Reisner if (sa && --sa == 0) 884c507f46fSPhilipp Reisner dev_warn(DEV,"drbd_rs_begin_io() stepped aside for 20sec." 885c507f46fSPhilipp Reisner "Resync stalled?\n"); 886f91ab628SPhilipp Reisner goto retry; 887b411b363SPhilipp Reisner } 888b411b363SPhilipp Reisner } 889b411b363SPhilipp Reisner set_bit(BME_LOCKED, &bm_ext->flags); 89080a40e43SLars Ellenberg return 0; 891b411b363SPhilipp Reisner } 892b411b363SPhilipp Reisner 893b411b363SPhilipp Reisner /** 894b411b363SPhilipp Reisner * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep 895b411b363SPhilipp Reisner * @mdev: DRBD device. 896b411b363SPhilipp Reisner * @sector: The sector number. 897b411b363SPhilipp Reisner * 898b411b363SPhilipp Reisner * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then 899b411b363SPhilipp Reisner * tries to set it to BME_LOCKED. Returns 0 upon success, and -EAGAIN 900b411b363SPhilipp Reisner * if there is still application IO going on in this area. 901b411b363SPhilipp Reisner */ 902b411b363SPhilipp Reisner int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector) 903b411b363SPhilipp Reisner { 904b411b363SPhilipp Reisner unsigned int enr = BM_SECT_TO_EXT(sector); 905b411b363SPhilipp Reisner const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT; 906b411b363SPhilipp Reisner struct lc_element *e; 907b411b363SPhilipp Reisner struct bm_extent *bm_ext; 908b411b363SPhilipp Reisner int i; 909b411b363SPhilipp Reisner 910b411b363SPhilipp Reisner spin_lock_irq(&mdev->al_lock); 911b411b363SPhilipp Reisner if (mdev->resync_wenr != LC_FREE && mdev->resync_wenr != enr) { 912b411b363SPhilipp Reisner /* in case you have very heavy scattered io, it may 913b411b363SPhilipp Reisner * stall the syncer undefined if we give up the ref count 914b411b363SPhilipp Reisner * when we try again and requeue. 915b411b363SPhilipp Reisner * 916b411b363SPhilipp Reisner * if we don't give up the refcount, but the next time 917b411b363SPhilipp Reisner * we are scheduled this extent has been "synced" by new 918b411b363SPhilipp Reisner * application writes, we'd miss the lc_put on the 919b411b363SPhilipp Reisner * extent we keep the refcount on. 920b411b363SPhilipp Reisner * so we remembered which extent we had to try again, and 921b411b363SPhilipp Reisner * if the next requested one is something else, we do 922b411b363SPhilipp Reisner * the lc_put here... 923b411b363SPhilipp Reisner * we also have to wake_up 924b411b363SPhilipp Reisner */ 925b411b363SPhilipp Reisner e = lc_find(mdev->resync, mdev->resync_wenr); 926b411b363SPhilipp Reisner bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; 927b411b363SPhilipp Reisner if (bm_ext) { 928b411b363SPhilipp Reisner D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags)); 929b411b363SPhilipp Reisner D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags)); 930b411b363SPhilipp Reisner clear_bit(BME_NO_WRITES, &bm_ext->flags); 931b411b363SPhilipp Reisner mdev->resync_wenr = LC_FREE; 932b411b363SPhilipp Reisner if (lc_put(mdev->resync, &bm_ext->lce) == 0) 933b411b363SPhilipp Reisner mdev->resync_locked--; 934b411b363SPhilipp Reisner wake_up(&mdev->al_wait); 935b411b363SPhilipp Reisner } else { 936b411b363SPhilipp Reisner dev_alert(DEV, "LOGIC BUG\n"); 937b411b363SPhilipp Reisner } 938b411b363SPhilipp Reisner } 939b411b363SPhilipp Reisner /* TRY. */ 940b411b363SPhilipp Reisner e = lc_try_get(mdev->resync, enr); 941b411b363SPhilipp Reisner bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; 942b411b363SPhilipp Reisner if (bm_ext) { 943b411b363SPhilipp Reisner if (test_bit(BME_LOCKED, &bm_ext->flags)) 944b411b363SPhilipp Reisner goto proceed; 945b411b363SPhilipp Reisner if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) { 946b411b363SPhilipp Reisner mdev->resync_locked++; 947b411b363SPhilipp Reisner } else { 948b411b363SPhilipp Reisner /* we did set the BME_NO_WRITES, 949b411b363SPhilipp Reisner * but then could not set BME_LOCKED, 950b411b363SPhilipp Reisner * so we tried again. 951b411b363SPhilipp Reisner * drop the extra reference. */ 952b411b363SPhilipp Reisner bm_ext->lce.refcnt--; 953b411b363SPhilipp Reisner D_ASSERT(bm_ext->lce.refcnt > 0); 954b411b363SPhilipp Reisner } 955b411b363SPhilipp Reisner goto check_al; 956b411b363SPhilipp Reisner } else { 957b411b363SPhilipp Reisner /* do we rather want to try later? */ 9586a0afdf5SJens Axboe if (mdev->resync_locked > mdev->resync->nr_elements-3) 959b411b363SPhilipp Reisner goto try_again; 960b411b363SPhilipp Reisner /* Do or do not. There is no try. -- Yoda */ 961b411b363SPhilipp Reisner e = lc_get(mdev->resync, enr); 962b411b363SPhilipp Reisner bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; 963b411b363SPhilipp Reisner if (!bm_ext) { 964b411b363SPhilipp Reisner const unsigned long rs_flags = mdev->resync->flags; 965b411b363SPhilipp Reisner if (rs_flags & LC_STARVING) 966b411b363SPhilipp Reisner dev_warn(DEV, "Have to wait for element" 967b411b363SPhilipp Reisner " (resync LRU too small?)\n"); 96846a15bc3SLars Ellenberg BUG_ON(rs_flags & LC_LOCKED); 969b411b363SPhilipp Reisner goto try_again; 970b411b363SPhilipp Reisner } 971b411b363SPhilipp Reisner if (bm_ext->lce.lc_number != enr) { 972b411b363SPhilipp Reisner bm_ext->rs_left = drbd_bm_e_weight(mdev, enr); 973b411b363SPhilipp Reisner bm_ext->rs_failed = 0; 97446a15bc3SLars Ellenberg lc_committed(mdev->resync); 975b411b363SPhilipp Reisner wake_up(&mdev->al_wait); 976b411b363SPhilipp Reisner D_ASSERT(test_bit(BME_LOCKED, &bm_ext->flags) == 0); 977b411b363SPhilipp Reisner } 978b411b363SPhilipp Reisner set_bit(BME_NO_WRITES, &bm_ext->flags); 979b411b363SPhilipp Reisner D_ASSERT(bm_ext->lce.refcnt == 1); 980b411b363SPhilipp Reisner mdev->resync_locked++; 981b411b363SPhilipp Reisner goto check_al; 982b411b363SPhilipp Reisner } 983b411b363SPhilipp Reisner check_al: 984b411b363SPhilipp Reisner for (i = 0; i < AL_EXT_PER_BM_SECT; i++) { 985b411b363SPhilipp Reisner if (lc_is_used(mdev->act_log, al_enr+i)) 986b411b363SPhilipp Reisner goto try_again; 987b411b363SPhilipp Reisner } 988b411b363SPhilipp Reisner set_bit(BME_LOCKED, &bm_ext->flags); 989b411b363SPhilipp Reisner proceed: 990b411b363SPhilipp Reisner mdev->resync_wenr = LC_FREE; 991b411b363SPhilipp Reisner spin_unlock_irq(&mdev->al_lock); 992b411b363SPhilipp Reisner return 0; 993b411b363SPhilipp Reisner 994b411b363SPhilipp Reisner try_again: 995b411b363SPhilipp Reisner if (bm_ext) 996b411b363SPhilipp Reisner mdev->resync_wenr = enr; 997b411b363SPhilipp Reisner spin_unlock_irq(&mdev->al_lock); 998b411b363SPhilipp Reisner return -EAGAIN; 999b411b363SPhilipp Reisner } 1000b411b363SPhilipp Reisner 1001b411b363SPhilipp Reisner void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector) 1002b411b363SPhilipp Reisner { 1003b411b363SPhilipp Reisner unsigned int enr = BM_SECT_TO_EXT(sector); 1004b411b363SPhilipp Reisner struct lc_element *e; 1005b411b363SPhilipp Reisner struct bm_extent *bm_ext; 1006b411b363SPhilipp Reisner unsigned long flags; 1007b411b363SPhilipp Reisner 1008b411b363SPhilipp Reisner spin_lock_irqsave(&mdev->al_lock, flags); 1009b411b363SPhilipp Reisner e = lc_find(mdev->resync, enr); 1010b411b363SPhilipp Reisner bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; 1011b411b363SPhilipp Reisner if (!bm_ext) { 1012b411b363SPhilipp Reisner spin_unlock_irqrestore(&mdev->al_lock, flags); 1013b411b363SPhilipp Reisner if (__ratelimit(&drbd_ratelimit_state)) 1014b411b363SPhilipp Reisner dev_err(DEV, "drbd_rs_complete_io() called, but extent not found\n"); 1015b411b363SPhilipp Reisner return; 1016b411b363SPhilipp Reisner } 1017b411b363SPhilipp Reisner 1018b411b363SPhilipp Reisner if (bm_ext->lce.refcnt == 0) { 1019b411b363SPhilipp Reisner spin_unlock_irqrestore(&mdev->al_lock, flags); 1020b411b363SPhilipp Reisner dev_err(DEV, "drbd_rs_complete_io(,%llu [=%u]) called, " 1021b411b363SPhilipp Reisner "but refcnt is 0!?\n", 1022b411b363SPhilipp Reisner (unsigned long long)sector, enr); 1023b411b363SPhilipp Reisner return; 1024b411b363SPhilipp Reisner } 1025b411b363SPhilipp Reisner 1026b411b363SPhilipp Reisner if (lc_put(mdev->resync, &bm_ext->lce) == 0) { 1027e3555d85SPhilipp Reisner bm_ext->flags = 0; /* clear BME_LOCKED, BME_NO_WRITES and BME_PRIORITY */ 1028b411b363SPhilipp Reisner mdev->resync_locked--; 1029b411b363SPhilipp Reisner wake_up(&mdev->al_wait); 1030b411b363SPhilipp Reisner } 1031b411b363SPhilipp Reisner 1032b411b363SPhilipp Reisner spin_unlock_irqrestore(&mdev->al_lock, flags); 1033b411b363SPhilipp Reisner } 1034b411b363SPhilipp Reisner 1035b411b363SPhilipp Reisner /** 1036b411b363SPhilipp Reisner * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED) 1037b411b363SPhilipp Reisner * @mdev: DRBD device. 1038b411b363SPhilipp Reisner */ 1039b411b363SPhilipp Reisner void drbd_rs_cancel_all(struct drbd_conf *mdev) 1040b411b363SPhilipp Reisner { 1041b411b363SPhilipp Reisner spin_lock_irq(&mdev->al_lock); 1042b411b363SPhilipp Reisner 1043b411b363SPhilipp Reisner if (get_ldev_if_state(mdev, D_FAILED)) { /* Makes sure ->resync is there. */ 1044b411b363SPhilipp Reisner lc_reset(mdev->resync); 1045b411b363SPhilipp Reisner put_ldev(mdev); 1046b411b363SPhilipp Reisner } 1047b411b363SPhilipp Reisner mdev->resync_locked = 0; 1048b411b363SPhilipp Reisner mdev->resync_wenr = LC_FREE; 1049b411b363SPhilipp Reisner spin_unlock_irq(&mdev->al_lock); 1050b411b363SPhilipp Reisner wake_up(&mdev->al_wait); 1051b411b363SPhilipp Reisner } 1052b411b363SPhilipp Reisner 1053b411b363SPhilipp Reisner /** 1054b411b363SPhilipp Reisner * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU 1055b411b363SPhilipp Reisner * @mdev: DRBD device. 1056b411b363SPhilipp Reisner * 1057b411b363SPhilipp Reisner * Returns 0 upon success, -EAGAIN if at least one reference count was 1058b411b363SPhilipp Reisner * not zero. 1059b411b363SPhilipp Reisner */ 1060b411b363SPhilipp Reisner int drbd_rs_del_all(struct drbd_conf *mdev) 1061b411b363SPhilipp Reisner { 1062b411b363SPhilipp Reisner struct lc_element *e; 1063b411b363SPhilipp Reisner struct bm_extent *bm_ext; 1064b411b363SPhilipp Reisner int i; 1065b411b363SPhilipp Reisner 1066b411b363SPhilipp Reisner spin_lock_irq(&mdev->al_lock); 1067b411b363SPhilipp Reisner 1068b411b363SPhilipp Reisner if (get_ldev_if_state(mdev, D_FAILED)) { 1069b411b363SPhilipp Reisner /* ok, ->resync is there. */ 1070b411b363SPhilipp Reisner for (i = 0; i < mdev->resync->nr_elements; i++) { 1071b411b363SPhilipp Reisner e = lc_element_by_index(mdev->resync, i); 1072b2b163ddSPhilipp Reisner bm_ext = lc_entry(e, struct bm_extent, lce); 1073b411b363SPhilipp Reisner if (bm_ext->lce.lc_number == LC_FREE) 1074b411b363SPhilipp Reisner continue; 1075b411b363SPhilipp Reisner if (bm_ext->lce.lc_number == mdev->resync_wenr) { 1076b411b363SPhilipp Reisner dev_info(DEV, "dropping %u in drbd_rs_del_all, apparently" 1077b411b363SPhilipp Reisner " got 'synced' by application io\n", 1078b411b363SPhilipp Reisner mdev->resync_wenr); 1079b411b363SPhilipp Reisner D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags)); 1080b411b363SPhilipp Reisner D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags)); 1081b411b363SPhilipp Reisner clear_bit(BME_NO_WRITES, &bm_ext->flags); 1082b411b363SPhilipp Reisner mdev->resync_wenr = LC_FREE; 1083b411b363SPhilipp Reisner lc_put(mdev->resync, &bm_ext->lce); 1084b411b363SPhilipp Reisner } 1085b411b363SPhilipp Reisner if (bm_ext->lce.refcnt != 0) { 1086b411b363SPhilipp Reisner dev_info(DEV, "Retrying drbd_rs_del_all() later. " 1087b411b363SPhilipp Reisner "refcnt=%d\n", bm_ext->lce.refcnt); 1088b411b363SPhilipp Reisner put_ldev(mdev); 1089b411b363SPhilipp Reisner spin_unlock_irq(&mdev->al_lock); 1090b411b363SPhilipp Reisner return -EAGAIN; 1091b411b363SPhilipp Reisner } 1092b411b363SPhilipp Reisner D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags)); 1093b411b363SPhilipp Reisner D_ASSERT(!test_bit(BME_NO_WRITES, &bm_ext->flags)); 1094b411b363SPhilipp Reisner lc_del(mdev->resync, &bm_ext->lce); 1095b411b363SPhilipp Reisner } 1096b411b363SPhilipp Reisner D_ASSERT(mdev->resync->used == 0); 1097b411b363SPhilipp Reisner put_ldev(mdev); 1098b411b363SPhilipp Reisner } 1099b411b363SPhilipp Reisner spin_unlock_irq(&mdev->al_lock); 1100b411b363SPhilipp Reisner 1101b411b363SPhilipp Reisner return 0; 1102b411b363SPhilipp Reisner } 1103b411b363SPhilipp Reisner 1104b411b363SPhilipp Reisner /** 1105b411b363SPhilipp Reisner * drbd_rs_failed_io() - Record information on a failure to resync the specified blocks 1106b411b363SPhilipp Reisner * @mdev: DRBD device. 1107b411b363SPhilipp Reisner * @sector: The sector number. 1108b411b363SPhilipp Reisner * @size: Size of failed IO operation, in byte. 1109b411b363SPhilipp Reisner */ 1110b411b363SPhilipp Reisner void drbd_rs_failed_io(struct drbd_conf *mdev, sector_t sector, int size) 1111b411b363SPhilipp Reisner { 1112b411b363SPhilipp Reisner /* Is called from worker and receiver context _only_ */ 1113b411b363SPhilipp Reisner unsigned long sbnr, ebnr, lbnr; 1114b411b363SPhilipp Reisner unsigned long count; 1115b411b363SPhilipp Reisner sector_t esector, nr_sectors; 1116b411b363SPhilipp Reisner int wake_up = 0; 1117b411b363SPhilipp Reisner 1118c670a398SAndreas Gruenbacher if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) { 1119b411b363SPhilipp Reisner dev_err(DEV, "drbd_rs_failed_io: sector=%llus size=%d nonsense!\n", 1120b411b363SPhilipp Reisner (unsigned long long)sector, size); 1121b411b363SPhilipp Reisner return; 1122b411b363SPhilipp Reisner } 1123b411b363SPhilipp Reisner nr_sectors = drbd_get_capacity(mdev->this_bdev); 1124b411b363SPhilipp Reisner esector = sector + (size >> 9) - 1; 1125b411b363SPhilipp Reisner 1126841ce241SAndreas Gruenbacher if (!expect(sector < nr_sectors)) 1127841ce241SAndreas Gruenbacher return; 1128841ce241SAndreas Gruenbacher if (!expect(esector < nr_sectors)) 1129841ce241SAndreas Gruenbacher esector = nr_sectors - 1; 1130b411b363SPhilipp Reisner 1131b411b363SPhilipp Reisner lbnr = BM_SECT_TO_BIT(nr_sectors-1); 1132b411b363SPhilipp Reisner 1133b411b363SPhilipp Reisner /* 1134b411b363SPhilipp Reisner * round up start sector, round down end sector. we make sure we only 1135b411b363SPhilipp Reisner * handle full, aligned, BM_BLOCK_SIZE (4K) blocks */ 1136b411b363SPhilipp Reisner if (unlikely(esector < BM_SECT_PER_BIT-1)) 1137b411b363SPhilipp Reisner return; 1138b411b363SPhilipp Reisner if (unlikely(esector == (nr_sectors-1))) 1139b411b363SPhilipp Reisner ebnr = lbnr; 1140b411b363SPhilipp Reisner else 1141b411b363SPhilipp Reisner ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1)); 1142b411b363SPhilipp Reisner sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1); 1143b411b363SPhilipp Reisner 1144b411b363SPhilipp Reisner if (sbnr > ebnr) 1145b411b363SPhilipp Reisner return; 1146b411b363SPhilipp Reisner 1147b411b363SPhilipp Reisner /* 1148b411b363SPhilipp Reisner * ok, (capacity & 7) != 0 sometimes, but who cares... 1149b411b363SPhilipp Reisner * we count rs_{total,left} in bits, not sectors. 1150b411b363SPhilipp Reisner */ 1151b411b363SPhilipp Reisner spin_lock_irq(&mdev->al_lock); 1152b411b363SPhilipp Reisner count = drbd_bm_count_bits(mdev, sbnr, ebnr); 1153b411b363SPhilipp Reisner if (count) { 1154b411b363SPhilipp Reisner mdev->rs_failed += count; 1155b411b363SPhilipp Reisner 1156b411b363SPhilipp Reisner if (get_ldev(mdev)) { 115781e84650SAndreas Gruenbacher drbd_try_clear_on_disk_bm(mdev, sector, count, false); 1158b411b363SPhilipp Reisner put_ldev(mdev); 1159b411b363SPhilipp Reisner } 1160b411b363SPhilipp Reisner 1161b411b363SPhilipp Reisner /* just wake_up unconditional now, various lc_chaged(), 1162b411b363SPhilipp Reisner * lc_put() in drbd_try_clear_on_disk_bm(). */ 1163b411b363SPhilipp Reisner wake_up = 1; 1164b411b363SPhilipp Reisner } 1165b411b363SPhilipp Reisner spin_unlock_irq(&mdev->al_lock); 1166b411b363SPhilipp Reisner if (wake_up) 1167b411b363SPhilipp Reisner wake_up(&mdev->al_wait); 1168b411b363SPhilipp Reisner } 1169