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 3385f103d8SLars Ellenberg 3485f103d8SLars Ellenberg enum al_transaction_types { 3585f103d8SLars Ellenberg AL_TR_UPDATE = 0, 3685f103d8SLars Ellenberg AL_TR_INITIALIZED = 0xffff 3785f103d8SLars Ellenberg }; 387ad651b5SLars Ellenberg /* all fields on disc in big endian */ 397ad651b5SLars Ellenberg struct __packed al_transaction_on_disk { 407ad651b5SLars Ellenberg /* don't we all like magic */ 417ad651b5SLars Ellenberg __be32 magic; 427ad651b5SLars Ellenberg 437ad651b5SLars Ellenberg /* to identify the most recent transaction block 447ad651b5SLars Ellenberg * in the on disk ring buffer */ 457ad651b5SLars Ellenberg __be32 tr_number; 467ad651b5SLars Ellenberg 477ad651b5SLars Ellenberg /* checksum on the full 4k block, with this field set to 0. */ 487ad651b5SLars Ellenberg __be32 crc32c; 497ad651b5SLars Ellenberg 507ad651b5SLars Ellenberg /* type of transaction, special transaction types like: 5185f103d8SLars Ellenberg * purge-all, set-all-idle, set-all-active, ... to-be-defined 5285f103d8SLars Ellenberg * see also enum al_transaction_types */ 537ad651b5SLars Ellenberg __be16 transaction_type; 547ad651b5SLars Ellenberg 557ad651b5SLars Ellenberg /* we currently allow only a few thousand extents, 567ad651b5SLars Ellenberg * so 16bit will be enough for the slot number. */ 577ad651b5SLars Ellenberg 587ad651b5SLars Ellenberg /* how many updates in this transaction */ 597ad651b5SLars Ellenberg __be16 n_updates; 607ad651b5SLars Ellenberg 617ad651b5SLars Ellenberg /* maximum slot number, "al-extents" in drbd.conf speak. 627ad651b5SLars Ellenberg * Having this in each transaction should make reconfiguration 637ad651b5SLars Ellenberg * of that parameter easier. */ 647ad651b5SLars Ellenberg __be16 context_size; 657ad651b5SLars Ellenberg 667ad651b5SLars Ellenberg /* slot number the context starts with */ 677ad651b5SLars Ellenberg __be16 context_start_slot_nr; 687ad651b5SLars Ellenberg 697ad651b5SLars Ellenberg /* Some reserved bytes. Expected usage is a 64bit counter of 707ad651b5SLars Ellenberg * sectors-written since device creation, and other data generation tag 717ad651b5SLars Ellenberg * supporting usage */ 727ad651b5SLars Ellenberg __be32 __reserved[4]; 737ad651b5SLars Ellenberg 747ad651b5SLars Ellenberg /* --- 36 byte used --- */ 757ad651b5SLars Ellenberg 767ad651b5SLars Ellenberg /* Reserve space for up to AL_UPDATES_PER_TRANSACTION changes 777ad651b5SLars Ellenberg * in one transaction, then use the remaining byte in the 4k block for 787ad651b5SLars Ellenberg * context information. "Flexible" number of updates per transaction 797ad651b5SLars Ellenberg * does not help, as we have to account for the case when all update 807ad651b5SLars Ellenberg * slots are used anyways, so it would only complicate code without 817ad651b5SLars Ellenberg * additional benefit. 82b411b363SPhilipp Reisner */ 837ad651b5SLars Ellenberg __be16 update_slot_nr[AL_UPDATES_PER_TRANSACTION]; 847ad651b5SLars Ellenberg 857ad651b5SLars Ellenberg /* but the extent number is 32bit, which at an extent size of 4 MiB 867ad651b5SLars Ellenberg * allows to cover device sizes of up to 2**54 Byte (16 PiB) */ 877ad651b5SLars Ellenberg __be32 update_extent_nr[AL_UPDATES_PER_TRANSACTION]; 887ad651b5SLars Ellenberg 897ad651b5SLars Ellenberg /* --- 420 bytes used (36 + 64*6) --- */ 907ad651b5SLars Ellenberg 917ad651b5SLars Ellenberg /* 4096 - 420 = 3676 = 919 * 4 */ 927ad651b5SLars Ellenberg __be32 context[AL_CONTEXT_PER_TRANSACTION]; 93b411b363SPhilipp Reisner }; 94b411b363SPhilipp Reisner 95b30ab791SAndreas Gruenbacher void *drbd_md_get_buffer(struct drbd_device *device) 96cdfda633SPhilipp Reisner { 97cdfda633SPhilipp Reisner int r; 98cdfda633SPhilipp Reisner 99b30ab791SAndreas Gruenbacher wait_event(device->misc_wait, 100b30ab791SAndreas Gruenbacher (r = atomic_cmpxchg(&device->md_io_in_use, 0, 1)) == 0 || 101b30ab791SAndreas Gruenbacher device->state.disk <= D_FAILED); 102cdfda633SPhilipp Reisner 103b30ab791SAndreas Gruenbacher return r ? NULL : page_address(device->md_io_page); 104cdfda633SPhilipp Reisner } 105cdfda633SPhilipp Reisner 106b30ab791SAndreas Gruenbacher void drbd_md_put_buffer(struct drbd_device *device) 107cdfda633SPhilipp Reisner { 108b30ab791SAndreas Gruenbacher if (atomic_dec_and_test(&device->md_io_in_use)) 109b30ab791SAndreas Gruenbacher wake_up(&device->misc_wait); 110cdfda633SPhilipp Reisner } 111cdfda633SPhilipp Reisner 112b30ab791SAndreas Gruenbacher void wait_until_done_or_force_detached(struct drbd_device *device, struct drbd_backing_dev *bdev, 11332db80f6SPhilipp Reisner unsigned int *done) 114cdfda633SPhilipp Reisner { 11532db80f6SPhilipp Reisner long dt; 11632db80f6SPhilipp Reisner 11732db80f6SPhilipp Reisner rcu_read_lock(); 11832db80f6SPhilipp Reisner dt = rcu_dereference(bdev->disk_conf)->disk_timeout; 11932db80f6SPhilipp Reisner rcu_read_unlock(); 12032db80f6SPhilipp Reisner dt = dt * HZ / 10; 12132db80f6SPhilipp Reisner if (dt == 0) 12232db80f6SPhilipp Reisner dt = MAX_SCHEDULE_TIMEOUT; 12332db80f6SPhilipp Reisner 124b30ab791SAndreas Gruenbacher dt = wait_event_timeout(device->misc_wait, 125b30ab791SAndreas Gruenbacher *done || test_bit(FORCE_DETACH, &device->flags), dt); 126e34b677dSLars Ellenberg if (dt == 0) { 127d0180171SAndreas Gruenbacher drbd_err(device, "meta-data IO operation timed out\n"); 128b30ab791SAndreas Gruenbacher drbd_chk_io_error(device, 1, DRBD_FORCE_DETACH); 129e34b677dSLars Ellenberg } 130cdfda633SPhilipp Reisner } 131cdfda633SPhilipp Reisner 132b30ab791SAndreas Gruenbacher static int _drbd_md_sync_page_io(struct drbd_device *device, 133b411b363SPhilipp Reisner struct drbd_backing_dev *bdev, 134b411b363SPhilipp Reisner struct page *page, sector_t sector, 135b411b363SPhilipp Reisner int rw, int size) 136b411b363SPhilipp Reisner { 137b411b363SPhilipp Reisner struct bio *bio; 138ac29f403SAndreas Gruenbacher int err; 139b411b363SPhilipp Reisner 140b30ab791SAndreas Gruenbacher device->md_io.done = 0; 141b30ab791SAndreas Gruenbacher device->md_io.error = -ENODEV; 142b411b363SPhilipp Reisner 143b30ab791SAndreas Gruenbacher if ((rw & WRITE) && !test_bit(MD_NO_FUA, &device->flags)) 14486e1e98eSLars Ellenberg rw |= REQ_FUA | REQ_FLUSH; 1452ed912e9SLars Ellenberg rw |= REQ_SYNC | REQ_NOIDLE; 146b411b363SPhilipp Reisner 147da4a75d2SLars Ellenberg bio = bio_alloc_drbd(GFP_NOIO); 148b411b363SPhilipp Reisner bio->bi_bdev = bdev->md_bdev; 1494f024f37SKent Overstreet bio->bi_iter.bi_sector = sector; 150ac29f403SAndreas Gruenbacher err = -EIO; 151ac29f403SAndreas Gruenbacher if (bio_add_page(bio, page, size, 0) != size) 152b411b363SPhilipp Reisner goto out; 153b30ab791SAndreas Gruenbacher bio->bi_private = &device->md_io; 154b411b363SPhilipp Reisner bio->bi_end_io = drbd_md_io_complete; 155b411b363SPhilipp Reisner bio->bi_rw = rw; 156b411b363SPhilipp Reisner 157b30ab791SAndreas Gruenbacher if (!(rw & WRITE) && device->state.disk == D_DISKLESS && device->ldev == NULL) 158c04ccaa6SLars Ellenberg /* special case, drbd_md_read() during drbd_adm_attach(): no get_ldev */ 159c04ccaa6SLars Ellenberg ; 160b30ab791SAndreas Gruenbacher else if (!get_ldev_if_state(device, D_ATTACHING)) { 161c04ccaa6SLars Ellenberg /* Corresponding put_ldev in drbd_md_io_complete() */ 162d0180171SAndreas Gruenbacher drbd_err(device, "ASSERT FAILED: get_ldev_if_state() == 1 in _drbd_md_sync_page_io()\n"); 163cdfda633SPhilipp Reisner err = -ENODEV; 164cdfda633SPhilipp Reisner goto out; 165cdfda633SPhilipp Reisner } 166cdfda633SPhilipp Reisner 167cdfda633SPhilipp Reisner bio_get(bio); /* one bio_put() is in the completion handler */ 168b30ab791SAndreas Gruenbacher atomic_inc(&device->md_io_in_use); /* drbd_md_put_buffer() is in the completion handler */ 169b30ab791SAndreas Gruenbacher if (drbd_insert_fault(device, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD)) 170b411b363SPhilipp Reisner bio_endio(bio, -EIO); 171b411b363SPhilipp Reisner else 172b411b363SPhilipp Reisner submit_bio(rw, bio); 173b30ab791SAndreas Gruenbacher wait_until_done_or_force_detached(device, bdev, &device->md_io.done); 174ac29f403SAndreas Gruenbacher if (bio_flagged(bio, BIO_UPTODATE)) 175b30ab791SAndreas Gruenbacher err = device->md_io.error; 176b411b363SPhilipp Reisner 177b411b363SPhilipp Reisner out: 178b411b363SPhilipp Reisner bio_put(bio); 179ac29f403SAndreas Gruenbacher return err; 180b411b363SPhilipp Reisner } 181b411b363SPhilipp Reisner 182b30ab791SAndreas Gruenbacher int drbd_md_sync_page_io(struct drbd_device *device, struct drbd_backing_dev *bdev, 183b411b363SPhilipp Reisner sector_t sector, int rw) 184b411b363SPhilipp Reisner { 1853fbf4d21SAndreas Gruenbacher int err; 186b30ab791SAndreas Gruenbacher struct page *iop = device->md_io_page; 187b411b363SPhilipp Reisner 1880b0ba1efSAndreas Gruenbacher D_ASSERT(device, atomic_read(&device->md_io_in_use) == 1); 189b411b363SPhilipp Reisner 190b411b363SPhilipp Reisner BUG_ON(!bdev->md_bdev); 191b411b363SPhilipp Reisner 192e4d7d6f4SLars Ellenberg dynamic_drbd_dbg(device, "meta_data io: %s [%d]:%s(,%llus,%s) %pS\n", 1937ad651b5SLars Ellenberg current->comm, current->pid, __func__, 194c04ccaa6SLars Ellenberg (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ", 195c04ccaa6SLars Ellenberg (void*)_RET_IP_ ); 196b411b363SPhilipp Reisner 197b411b363SPhilipp Reisner if (sector < drbd_md_first_sector(bdev) || 1987ad651b5SLars Ellenberg sector + 7 > drbd_md_last_sector(bdev)) 199d0180171SAndreas Gruenbacher drbd_alert(device, "%s [%d]:%s(,%llus,%s) out of range md access!\n", 200b411b363SPhilipp Reisner current->comm, current->pid, __func__, 201b411b363SPhilipp Reisner (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ"); 202b411b363SPhilipp Reisner 203ae8bf312SLars Ellenberg /* we do all our meta data IO in aligned 4k blocks. */ 204b30ab791SAndreas Gruenbacher err = _drbd_md_sync_page_io(device, bdev, iop, sector, rw, 4096); 2053fbf4d21SAndreas Gruenbacher if (err) { 206d0180171SAndreas Gruenbacher drbd_err(device, "drbd_md_sync_page_io(,%llus,%s) failed with error %d\n", 207935be260SAndreas Gruenbacher (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ", err); 208b411b363SPhilipp Reisner } 2093fbf4d21SAndreas Gruenbacher return err; 210b411b363SPhilipp Reisner } 211b411b363SPhilipp Reisner 212b30ab791SAndreas Gruenbacher static struct bm_extent *find_active_resync_extent(struct drbd_device *device, unsigned int enr) 213b411b363SPhilipp Reisner { 214b411b363SPhilipp Reisner struct lc_element *tmp; 215b30ab791SAndreas Gruenbacher tmp = lc_find(device->resync, enr/AL_EXT_PER_BM_SECT); 216b411b363SPhilipp Reisner if (unlikely(tmp != NULL)) { 217b411b363SPhilipp Reisner struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce); 2186c3c4355SLars Ellenberg if (test_bit(BME_NO_WRITES, &bm_ext->flags)) 2196c3c4355SLars Ellenberg return bm_ext; 2206c3c4355SLars Ellenberg } 2216c3c4355SLars Ellenberg return NULL; 2226c3c4355SLars Ellenberg } 2236c3c4355SLars Ellenberg 224b30ab791SAndreas Gruenbacher static struct lc_element *_al_get(struct drbd_device *device, unsigned int enr, bool nonblock) 2256c3c4355SLars Ellenberg { 2266c3c4355SLars Ellenberg struct lc_element *al_ext; 2276c3c4355SLars Ellenberg struct bm_extent *bm_ext; 2286c3c4355SLars Ellenberg int wake; 2296c3c4355SLars Ellenberg 230b30ab791SAndreas Gruenbacher spin_lock_irq(&device->al_lock); 231b30ab791SAndreas Gruenbacher bm_ext = find_active_resync_extent(device, enr); 2326c3c4355SLars Ellenberg if (bm_ext) { 233f91ab628SPhilipp Reisner wake = !test_and_set_bit(BME_PRIORITY, &bm_ext->flags); 234b30ab791SAndreas Gruenbacher spin_unlock_irq(&device->al_lock); 235f91ab628SPhilipp Reisner if (wake) 236b30ab791SAndreas Gruenbacher wake_up(&device->al_wait); 237b411b363SPhilipp Reisner return NULL; 238b411b363SPhilipp Reisner } 2396c3c4355SLars Ellenberg if (nonblock) 240b30ab791SAndreas Gruenbacher al_ext = lc_try_get(device->act_log, enr); 2416c3c4355SLars Ellenberg else 242b30ab791SAndreas Gruenbacher al_ext = lc_get(device->act_log, enr); 243b30ab791SAndreas Gruenbacher spin_unlock_irq(&device->al_lock); 244b411b363SPhilipp Reisner return al_ext; 245b411b363SPhilipp Reisner } 246b411b363SPhilipp Reisner 247b30ab791SAndreas Gruenbacher bool drbd_al_begin_io_fastpath(struct drbd_device *device, struct drbd_interval *i) 248b5bc8e08SLars Ellenberg { 249b5bc8e08SLars Ellenberg /* for bios crossing activity log extent boundaries, 250b5bc8e08SLars Ellenberg * we may need to activate two extents in one go */ 251b5bc8e08SLars Ellenberg unsigned first = i->sector >> (AL_EXTENT_SHIFT-9); 252b5bc8e08SLars Ellenberg unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9); 253b5bc8e08SLars Ellenberg 2540b0ba1efSAndreas Gruenbacher D_ASSERT(device, (unsigned)(last - first) <= 1); 2550b0ba1efSAndreas Gruenbacher D_ASSERT(device, atomic_read(&device->local_cnt) > 0); 256b5bc8e08SLars Ellenberg 257b5bc8e08SLars Ellenberg /* FIXME figure out a fast path for bios crossing AL extent boundaries */ 258b5bc8e08SLars Ellenberg if (first != last) 259b5bc8e08SLars Ellenberg return false; 260b5bc8e08SLars Ellenberg 261b30ab791SAndreas Gruenbacher return _al_get(device, first, true); 262b5bc8e08SLars Ellenberg } 263b5bc8e08SLars Ellenberg 264b30ab791SAndreas Gruenbacher bool drbd_al_begin_io_prepare(struct drbd_device *device, struct drbd_interval *i) 265b411b363SPhilipp Reisner { 2667726547eSLars Ellenberg /* for bios crossing activity log extent boundaries, 2677726547eSLars Ellenberg * we may need to activate two extents in one go */ 268e15766e9SLars Ellenberg unsigned first = i->sector >> (AL_EXTENT_SHIFT-9); 26981a3537aSLars Ellenberg unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9); 270e15766e9SLars Ellenberg unsigned enr; 271ebfd5d8fSLars Ellenberg bool need_transaction = false; 272b411b363SPhilipp Reisner 2730b0ba1efSAndreas Gruenbacher D_ASSERT(device, first <= last); 2740b0ba1efSAndreas Gruenbacher D_ASSERT(device, atomic_read(&device->local_cnt) > 0); 275b411b363SPhilipp Reisner 276ebfd5d8fSLars Ellenberg for (enr = first; enr <= last; enr++) { 277ebfd5d8fSLars Ellenberg struct lc_element *al_ext; 278b30ab791SAndreas Gruenbacher wait_event(device->al_wait, 279b30ab791SAndreas Gruenbacher (al_ext = _al_get(device, enr, false)) != NULL); 280ebfd5d8fSLars Ellenberg if (al_ext->lc_number != enr) 281ebfd5d8fSLars Ellenberg need_transaction = true; 282ebfd5d8fSLars Ellenberg } 283b5bc8e08SLars Ellenberg return need_transaction; 284b5bc8e08SLars Ellenberg } 285ebfd5d8fSLars Ellenberg 286*4dd726f0SLars Ellenberg static int al_write_transaction(struct drbd_device *device); 287b5bc8e08SLars Ellenberg 288*4dd726f0SLars Ellenberg void drbd_al_begin_io_commit(struct drbd_device *device) 289b5bc8e08SLars Ellenberg { 290b5bc8e08SLars Ellenberg bool locked = false; 291b5bc8e08SLars Ellenberg 2927dc1d67fSLars Ellenberg /* Serialize multiple transactions. 2937dc1d67fSLars Ellenberg * This uses test_and_set_bit, memory barrier is implicit. 2947dc1d67fSLars Ellenberg */ 295b30ab791SAndreas Gruenbacher wait_event(device->al_wait, 296b30ab791SAndreas Gruenbacher device->act_log->pending_changes == 0 || 297b30ab791SAndreas Gruenbacher (locked = lc_try_lock_for_transaction(device->act_log))); 2987dc1d67fSLars Ellenberg 2997dc1d67fSLars Ellenberg if (locked) { 3007ad651b5SLars Ellenberg /* Double check: it may have been committed by someone else, 3017ad651b5SLars Ellenberg * while we have been waiting for the lock. */ 302b30ab791SAndreas Gruenbacher if (device->act_log->pending_changes) { 3039a51ab1cSPhilipp Reisner bool write_al_updates; 3049a51ab1cSPhilipp Reisner 3059a51ab1cSPhilipp Reisner rcu_read_lock(); 306b30ab791SAndreas Gruenbacher write_al_updates = rcu_dereference(device->ldev->disk_conf)->al_updates; 3079a51ab1cSPhilipp Reisner rcu_read_unlock(); 3089a51ab1cSPhilipp Reisner 309b5bc8e08SLars Ellenberg if (write_al_updates) 310*4dd726f0SLars Ellenberg al_write_transaction(device); 311b30ab791SAndreas Gruenbacher spin_lock_irq(&device->al_lock); 3127ad651b5SLars Ellenberg /* FIXME 3131b7ab15bSPhilipp Reisner if (err) 3147ad651b5SLars Ellenberg we need an "lc_cancel" here; 3157ad651b5SLars Ellenberg */ 316b30ab791SAndreas Gruenbacher lc_committed(device->act_log); 317b30ab791SAndreas Gruenbacher spin_unlock_irq(&device->al_lock); 3187ad651b5SLars Ellenberg } 319b30ab791SAndreas Gruenbacher lc_unlock(device->act_log); 320b30ab791SAndreas Gruenbacher wake_up(&device->al_wait); 321b411b363SPhilipp Reisner } 322b411b363SPhilipp Reisner } 323b411b363SPhilipp Reisner 324b5bc8e08SLars Ellenberg /* 325b5bc8e08SLars Ellenberg * @delegate: delegate activity log I/O to the worker thread 326b5bc8e08SLars Ellenberg */ 327*4dd726f0SLars Ellenberg void drbd_al_begin_io(struct drbd_device *device, struct drbd_interval *i) 328b5bc8e08SLars Ellenberg { 329b30ab791SAndreas Gruenbacher if (drbd_al_begin_io_prepare(device, i)) 330*4dd726f0SLars Ellenberg drbd_al_begin_io_commit(device); 331b5bc8e08SLars Ellenberg } 332b5bc8e08SLars Ellenberg 333b30ab791SAndreas Gruenbacher int drbd_al_begin_io_nonblock(struct drbd_device *device, struct drbd_interval *i) 33408a1ddabSLars Ellenberg { 335b30ab791SAndreas Gruenbacher struct lru_cache *al = device->act_log; 33608a1ddabSLars Ellenberg /* for bios crossing activity log extent boundaries, 33708a1ddabSLars Ellenberg * we may need to activate two extents in one go */ 33808a1ddabSLars Ellenberg unsigned first = i->sector >> (AL_EXTENT_SHIFT-9); 33908a1ddabSLars Ellenberg unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9); 34008a1ddabSLars Ellenberg unsigned nr_al_extents; 34108a1ddabSLars Ellenberg unsigned available_update_slots; 34208a1ddabSLars Ellenberg unsigned enr; 34308a1ddabSLars Ellenberg 3440b0ba1efSAndreas Gruenbacher D_ASSERT(device, first <= last); 34508a1ddabSLars Ellenberg 34608a1ddabSLars Ellenberg nr_al_extents = 1 + last - first; /* worst case: all touched extends are cold. */ 34708a1ddabSLars Ellenberg available_update_slots = min(al->nr_elements - al->used, 34808a1ddabSLars Ellenberg al->max_pending_changes - al->pending_changes); 34908a1ddabSLars Ellenberg 35008a1ddabSLars Ellenberg /* We want all necessary updates for a given request within the same transaction 35108a1ddabSLars Ellenberg * We could first check how many updates are *actually* needed, 35208a1ddabSLars Ellenberg * and use that instead of the worst-case nr_al_extents */ 35308a1ddabSLars Ellenberg if (available_update_slots < nr_al_extents) 35408a1ddabSLars Ellenberg return -EWOULDBLOCK; 35508a1ddabSLars Ellenberg 35608a1ddabSLars Ellenberg /* Is resync active in this area? */ 35708a1ddabSLars Ellenberg for (enr = first; enr <= last; enr++) { 35808a1ddabSLars Ellenberg struct lc_element *tmp; 359b30ab791SAndreas Gruenbacher tmp = lc_find(device->resync, enr/AL_EXT_PER_BM_SECT); 36008a1ddabSLars Ellenberg if (unlikely(tmp != NULL)) { 36108a1ddabSLars Ellenberg struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce); 36208a1ddabSLars Ellenberg if (test_bit(BME_NO_WRITES, &bm_ext->flags)) { 3630b6ef416SLars Ellenberg if (!test_and_set_bit(BME_PRIORITY, &bm_ext->flags)) 36408a1ddabSLars Ellenberg return -EBUSY; 36508a1ddabSLars Ellenberg return -EWOULDBLOCK; 36608a1ddabSLars Ellenberg } 36708a1ddabSLars Ellenberg } 36808a1ddabSLars Ellenberg } 36908a1ddabSLars Ellenberg 37008a1ddabSLars Ellenberg /* Checkout the refcounts. 37108a1ddabSLars Ellenberg * Given that we checked for available elements and update slots above, 37208a1ddabSLars Ellenberg * this has to be successful. */ 37308a1ddabSLars Ellenberg for (enr = first; enr <= last; enr++) { 37408a1ddabSLars Ellenberg struct lc_element *al_ext; 375b30ab791SAndreas Gruenbacher al_ext = lc_get_cumulative(device->act_log, enr); 37608a1ddabSLars Ellenberg if (!al_ext) 377d0180171SAndreas Gruenbacher drbd_info(device, "LOGIC BUG for enr=%u\n", enr); 37808a1ddabSLars Ellenberg } 37908a1ddabSLars Ellenberg return 0; 38008a1ddabSLars Ellenberg } 38108a1ddabSLars Ellenberg 382b30ab791SAndreas Gruenbacher void drbd_al_complete_io(struct drbd_device *device, struct drbd_interval *i) 383b411b363SPhilipp Reisner { 384e15766e9SLars Ellenberg /* for bios crossing activity log extent boundaries, 385e15766e9SLars Ellenberg * we may need to activate two extents in one go */ 386e15766e9SLars Ellenberg unsigned first = i->sector >> (AL_EXTENT_SHIFT-9); 38781a3537aSLars Ellenberg unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9); 388e15766e9SLars Ellenberg unsigned enr; 389b411b363SPhilipp Reisner struct lc_element *extent; 390b411b363SPhilipp Reisner unsigned long flags; 391b411b363SPhilipp Reisner 3920b0ba1efSAndreas Gruenbacher D_ASSERT(device, first <= last); 393b30ab791SAndreas Gruenbacher spin_lock_irqsave(&device->al_lock, flags); 394b411b363SPhilipp Reisner 395e15766e9SLars Ellenberg for (enr = first; enr <= last; enr++) { 396b30ab791SAndreas Gruenbacher extent = lc_find(device->act_log, enr); 397b411b363SPhilipp Reisner if (!extent) { 398d0180171SAndreas Gruenbacher drbd_err(device, "al_complete_io() called on inactive extent %u\n", enr); 399e15766e9SLars Ellenberg continue; 400b411b363SPhilipp Reisner } 401b30ab791SAndreas Gruenbacher lc_put(device->act_log, extent); 402e15766e9SLars Ellenberg } 403b30ab791SAndreas Gruenbacher spin_unlock_irqrestore(&device->al_lock, flags); 404b30ab791SAndreas Gruenbacher wake_up(&device->al_wait); 405b411b363SPhilipp Reisner } 406b411b363SPhilipp Reisner 40719f843aaSLars Ellenberg #if (PAGE_SHIFT + 3) < (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT) 40819f843aaSLars Ellenberg /* Currently BM_BLOCK_SHIFT, BM_EXT_SHIFT and AL_EXTENT_SHIFT 40919f843aaSLars Ellenberg * are still coupled, or assume too much about their relation. 41019f843aaSLars Ellenberg * Code below will not work if this is violated. 41119f843aaSLars Ellenberg * Will be cleaned up with some followup patch. 41219f843aaSLars Ellenberg */ 41319f843aaSLars Ellenberg # error FIXME 41419f843aaSLars Ellenberg #endif 41519f843aaSLars Ellenberg 41619f843aaSLars Ellenberg static unsigned int al_extent_to_bm_page(unsigned int al_enr) 41719f843aaSLars Ellenberg { 41819f843aaSLars Ellenberg return al_enr >> 41919f843aaSLars Ellenberg /* bit to page */ 42019f843aaSLars Ellenberg ((PAGE_SHIFT + 3) - 42119f843aaSLars Ellenberg /* al extent number to bit */ 42219f843aaSLars Ellenberg (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT)); 42319f843aaSLars Ellenberg } 42419f843aaSLars Ellenberg 425b30ab791SAndreas Gruenbacher static sector_t al_tr_number_to_on_disk_sector(struct drbd_device *device) 426ae8bf312SLars Ellenberg { 427b30ab791SAndreas Gruenbacher const unsigned int stripes = device->ldev->md.al_stripes; 428b30ab791SAndreas Gruenbacher const unsigned int stripe_size_4kB = device->ldev->md.al_stripe_size_4k; 429ae8bf312SLars Ellenberg 430ae8bf312SLars Ellenberg /* transaction number, modulo on-disk ring buffer wrap around */ 431b30ab791SAndreas Gruenbacher unsigned int t = device->al_tr_number % (device->ldev->md.al_size_4k); 432ae8bf312SLars Ellenberg 433ae8bf312SLars Ellenberg /* ... to aligned 4k on disk block */ 434ae8bf312SLars Ellenberg t = ((t % stripes) * stripe_size_4kB) + t/stripes; 435ae8bf312SLars Ellenberg 436ae8bf312SLars Ellenberg /* ... to 512 byte sector in activity log */ 437ae8bf312SLars Ellenberg t *= 8; 438ae8bf312SLars Ellenberg 439ae8bf312SLars Ellenberg /* ... plus offset to the on disk position */ 440b30ab791SAndreas Gruenbacher return device->ldev->md.md_offset + device->ldev->md.al_offset + t; 441ae8bf312SLars Ellenberg } 442ae8bf312SLars Ellenberg 443*4dd726f0SLars Ellenberg int al_write_transaction(struct drbd_device *device) 444b411b363SPhilipp Reisner { 4457ad651b5SLars Ellenberg struct al_transaction_on_disk *buffer; 4467ad651b5SLars Ellenberg struct lc_element *e; 447b411b363SPhilipp Reisner sector_t sector; 4487ad651b5SLars Ellenberg int i, mx; 4497ad651b5SLars Ellenberg unsigned extent_nr; 4507ad651b5SLars Ellenberg unsigned crc = 0; 4511b7ab15bSPhilipp Reisner int err = 0; 452b411b363SPhilipp Reisner 453b30ab791SAndreas Gruenbacher if (!get_ldev(device)) { 454d0180171SAndreas Gruenbacher drbd_err(device, "disk is %s, cannot start al transaction\n", 455b30ab791SAndreas Gruenbacher drbd_disk_str(device->state.disk)); 4561b7ab15bSPhilipp Reisner return -EIO; 457b411b363SPhilipp Reisner } 458b411b363SPhilipp Reisner 4596719fb03SLars Ellenberg /* The bitmap write may have failed, causing a state change. */ 460b30ab791SAndreas Gruenbacher if (device->state.disk < D_INCONSISTENT) { 461d0180171SAndreas Gruenbacher drbd_err(device, 4627ad651b5SLars Ellenberg "disk is %s, cannot write al transaction\n", 463b30ab791SAndreas Gruenbacher drbd_disk_str(device->state.disk)); 464b30ab791SAndreas Gruenbacher put_ldev(device); 4651b7ab15bSPhilipp Reisner return -EIO; 4666719fb03SLars Ellenberg } 4676719fb03SLars Ellenberg 468b30ab791SAndreas Gruenbacher buffer = drbd_md_get_buffer(device); /* protects md_io_buffer, al_tr_cycle, ... */ 469cdfda633SPhilipp Reisner if (!buffer) { 470d0180171SAndreas Gruenbacher drbd_err(device, "disk failed while waiting for md_io buffer\n"); 471b30ab791SAndreas Gruenbacher put_ldev(device); 4721b7ab15bSPhilipp Reisner return -ENODEV; 473cdfda633SPhilipp Reisner } 474b411b363SPhilipp Reisner 4757ad651b5SLars Ellenberg memset(buffer, 0, sizeof(*buffer)); 4767ad651b5SLars Ellenberg buffer->magic = cpu_to_be32(DRBD_AL_MAGIC); 477b30ab791SAndreas Gruenbacher buffer->tr_number = cpu_to_be32(device->al_tr_number); 478b411b363SPhilipp Reisner 4797ad651b5SLars Ellenberg i = 0; 480b411b363SPhilipp Reisner 4817ad651b5SLars Ellenberg /* Even though no one can start to change this list 4827ad651b5SLars Ellenberg * once we set the LC_LOCKED -- from drbd_al_begin_io(), 4837ad651b5SLars Ellenberg * lc_try_lock_for_transaction() --, someone may still 4847ad651b5SLars Ellenberg * be in the process of changing it. */ 485b30ab791SAndreas Gruenbacher spin_lock_irq(&device->al_lock); 486b30ab791SAndreas Gruenbacher list_for_each_entry(e, &device->act_log->to_be_changed, list) { 4877ad651b5SLars Ellenberg if (i == AL_UPDATES_PER_TRANSACTION) { 4887ad651b5SLars Ellenberg i++; 4897ad651b5SLars Ellenberg break; 4907ad651b5SLars Ellenberg } 4917ad651b5SLars Ellenberg buffer->update_slot_nr[i] = cpu_to_be16(e->lc_index); 4927ad651b5SLars Ellenberg buffer->update_extent_nr[i] = cpu_to_be32(e->lc_new_number); 4937ad651b5SLars Ellenberg if (e->lc_number != LC_FREE) 494b30ab791SAndreas Gruenbacher drbd_bm_mark_for_writeout(device, 4957ad651b5SLars Ellenberg al_extent_to_bm_page(e->lc_number)); 4967ad651b5SLars Ellenberg i++; 4977ad651b5SLars Ellenberg } 498b30ab791SAndreas Gruenbacher spin_unlock_irq(&device->al_lock); 4997ad651b5SLars Ellenberg BUG_ON(i > AL_UPDATES_PER_TRANSACTION); 500b411b363SPhilipp Reisner 5017ad651b5SLars Ellenberg buffer->n_updates = cpu_to_be16(i); 5027ad651b5SLars Ellenberg for ( ; i < AL_UPDATES_PER_TRANSACTION; i++) { 5037ad651b5SLars Ellenberg buffer->update_slot_nr[i] = cpu_to_be16(-1); 5047ad651b5SLars Ellenberg buffer->update_extent_nr[i] = cpu_to_be32(LC_FREE); 5057ad651b5SLars Ellenberg } 506b411b363SPhilipp Reisner 507b30ab791SAndreas Gruenbacher buffer->context_size = cpu_to_be16(device->act_log->nr_elements); 508b30ab791SAndreas Gruenbacher buffer->context_start_slot_nr = cpu_to_be16(device->al_tr_cycle); 5097ad651b5SLars Ellenberg 5107ad651b5SLars Ellenberg mx = min_t(int, AL_CONTEXT_PER_TRANSACTION, 511b30ab791SAndreas Gruenbacher device->act_log->nr_elements - device->al_tr_cycle); 512b411b363SPhilipp Reisner for (i = 0; i < mx; i++) { 513b30ab791SAndreas Gruenbacher unsigned idx = device->al_tr_cycle + i; 514b30ab791SAndreas Gruenbacher extent_nr = lc_element_by_index(device->act_log, idx)->lc_number; 5157ad651b5SLars Ellenberg buffer->context[i] = cpu_to_be32(extent_nr); 516b411b363SPhilipp Reisner } 5177ad651b5SLars Ellenberg for (; i < AL_CONTEXT_PER_TRANSACTION; i++) 5187ad651b5SLars Ellenberg buffer->context[i] = cpu_to_be32(LC_FREE); 5197ad651b5SLars Ellenberg 520b30ab791SAndreas Gruenbacher device->al_tr_cycle += AL_CONTEXT_PER_TRANSACTION; 521b30ab791SAndreas Gruenbacher if (device->al_tr_cycle >= device->act_log->nr_elements) 522b30ab791SAndreas Gruenbacher device->al_tr_cycle = 0; 523b411b363SPhilipp Reisner 524b30ab791SAndreas Gruenbacher sector = al_tr_number_to_on_disk_sector(device); 525b411b363SPhilipp Reisner 5267ad651b5SLars Ellenberg crc = crc32c(0, buffer, 4096); 5277ad651b5SLars Ellenberg buffer->crc32c = cpu_to_be32(crc); 5287ad651b5SLars Ellenberg 529b30ab791SAndreas Gruenbacher if (drbd_bm_write_hinted(device)) 5301b7ab15bSPhilipp Reisner err = -EIO; 531b5bc8e08SLars Ellenberg else { 532b5bc8e08SLars Ellenberg bool write_al_updates; 533b5bc8e08SLars Ellenberg rcu_read_lock(); 534b30ab791SAndreas Gruenbacher write_al_updates = rcu_dereference(device->ldev->disk_conf)->al_updates; 535b5bc8e08SLars Ellenberg rcu_read_unlock(); 536b5bc8e08SLars Ellenberg if (write_al_updates) { 537b30ab791SAndreas Gruenbacher if (drbd_md_sync_page_io(device, device->ldev, sector, WRITE)) { 5381b7ab15bSPhilipp Reisner err = -EIO; 539b30ab791SAndreas Gruenbacher drbd_chk_io_error(device, 1, DRBD_META_IO_ERROR); 5407ad651b5SLars Ellenberg } else { 541b30ab791SAndreas Gruenbacher device->al_tr_number++; 542b30ab791SAndreas Gruenbacher device->al_writ_cnt++; 543b5bc8e08SLars Ellenberg } 544b5bc8e08SLars Ellenberg } 5457ad651b5SLars Ellenberg } 546b411b363SPhilipp Reisner 547b30ab791SAndreas Gruenbacher drbd_md_put_buffer(device); 548b30ab791SAndreas Gruenbacher put_ldev(device); 549b411b363SPhilipp Reisner 5501b7ab15bSPhilipp Reisner return err; 5511b7ab15bSPhilipp Reisner } 5521b7ab15bSPhilipp Reisner 553b30ab791SAndreas Gruenbacher static int _try_lc_del(struct drbd_device *device, struct lc_element *al_ext) 554b411b363SPhilipp Reisner { 555b411b363SPhilipp Reisner int rv; 556b411b363SPhilipp Reisner 557b30ab791SAndreas Gruenbacher spin_lock_irq(&device->al_lock); 558b411b363SPhilipp Reisner rv = (al_ext->refcnt == 0); 559b411b363SPhilipp Reisner if (likely(rv)) 560b30ab791SAndreas Gruenbacher lc_del(device->act_log, al_ext); 561b30ab791SAndreas Gruenbacher spin_unlock_irq(&device->al_lock); 562b411b363SPhilipp Reisner 563b411b363SPhilipp Reisner return rv; 564b411b363SPhilipp Reisner } 565b411b363SPhilipp Reisner 566b411b363SPhilipp Reisner /** 567b411b363SPhilipp Reisner * drbd_al_shrink() - Removes all active extents form the activity log 568b30ab791SAndreas Gruenbacher * @device: DRBD device. 569b411b363SPhilipp Reisner * 570b411b363SPhilipp Reisner * Removes all active extents form the activity log, waiting until 571b411b363SPhilipp Reisner * the reference count of each entry dropped to 0 first, of course. 572b411b363SPhilipp Reisner * 573b30ab791SAndreas Gruenbacher * You need to lock device->act_log with lc_try_lock() / lc_unlock() 574b411b363SPhilipp Reisner */ 575b30ab791SAndreas Gruenbacher void drbd_al_shrink(struct drbd_device *device) 576b411b363SPhilipp Reisner { 577b411b363SPhilipp Reisner struct lc_element *al_ext; 578b411b363SPhilipp Reisner int i; 579b411b363SPhilipp Reisner 5800b0ba1efSAndreas Gruenbacher D_ASSERT(device, test_bit(__LC_LOCKED, &device->act_log->flags)); 581b411b363SPhilipp Reisner 582b30ab791SAndreas Gruenbacher for (i = 0; i < device->act_log->nr_elements; i++) { 583b30ab791SAndreas Gruenbacher al_ext = lc_element_by_index(device->act_log, i); 584b411b363SPhilipp Reisner if (al_ext->lc_number == LC_FREE) 585b411b363SPhilipp Reisner continue; 586b30ab791SAndreas Gruenbacher wait_event(device->al_wait, _try_lc_del(device, al_ext)); 587b411b363SPhilipp Reisner } 588b411b363SPhilipp Reisner 589b30ab791SAndreas Gruenbacher wake_up(&device->al_wait); 590b411b363SPhilipp Reisner } 591b411b363SPhilipp Reisner 592b30ab791SAndreas Gruenbacher int drbd_initialize_al(struct drbd_device *device, void *buffer) 593d752b269SPhilipp Reisner { 594d752b269SPhilipp Reisner struct al_transaction_on_disk *al = buffer; 595b30ab791SAndreas Gruenbacher struct drbd_md *md = &device->ldev->md; 596d752b269SPhilipp Reisner sector_t al_base = md->md_offset + md->al_offset; 597d752b269SPhilipp Reisner int al_size_4k = md->al_stripes * md->al_stripe_size_4k; 598d752b269SPhilipp Reisner int i; 599d752b269SPhilipp Reisner 600d752b269SPhilipp Reisner memset(al, 0, 4096); 601d752b269SPhilipp Reisner al->magic = cpu_to_be32(DRBD_AL_MAGIC); 602d752b269SPhilipp Reisner al->transaction_type = cpu_to_be16(AL_TR_INITIALIZED); 603d752b269SPhilipp Reisner al->crc32c = cpu_to_be32(crc32c(0, al, 4096)); 604d752b269SPhilipp Reisner 605d752b269SPhilipp Reisner for (i = 0; i < al_size_4k; i++) { 606b30ab791SAndreas Gruenbacher int err = drbd_md_sync_page_io(device, device->ldev, al_base + i * 8, WRITE); 607d752b269SPhilipp Reisner if (err) 608d752b269SPhilipp Reisner return err; 609d752b269SPhilipp Reisner } 610d752b269SPhilipp Reisner return 0; 611d752b269SPhilipp Reisner } 612d752b269SPhilipp Reisner 6135ab7d2c0SLars Ellenberg static const char *drbd_change_sync_fname[] = { 6145ab7d2c0SLars Ellenberg [RECORD_RS_FAILED] = "drbd_rs_failed_io", 6155ab7d2c0SLars Ellenberg [SET_IN_SYNC] = "drbd_set_in_sync", 6165ab7d2c0SLars Ellenberg [SET_OUT_OF_SYNC] = "drbd_set_out_of_sync" 6175ab7d2c0SLars Ellenberg }; 6185ab7d2c0SLars Ellenberg 619b411b363SPhilipp Reisner /* ATTENTION. The AL's extents are 4MB each, while the extents in the 620b411b363SPhilipp Reisner * resync LRU-cache are 16MB each. 621b411b363SPhilipp Reisner * The caller of this function has to hold an get_ldev() reference. 622b411b363SPhilipp Reisner * 6235ab7d2c0SLars Ellenberg * Adjusts the caching members ->rs_left (success) or ->rs_failed (!success), 6245ab7d2c0SLars Ellenberg * potentially pulling in (and recounting the corresponding bits) 6255ab7d2c0SLars Ellenberg * this resync extent into the resync extent lru cache. 6265ab7d2c0SLars Ellenberg * 6275ab7d2c0SLars Ellenberg * Returns whether all bits have been cleared for this resync extent, 6285ab7d2c0SLars Ellenberg * precisely: (rs_left <= rs_failed) 6295ab7d2c0SLars Ellenberg * 630b411b363SPhilipp Reisner * TODO will be obsoleted once we have a caching lru of the on disk bitmap 631b411b363SPhilipp Reisner */ 6325ab7d2c0SLars Ellenberg static bool update_rs_extent(struct drbd_device *device, 6335ab7d2c0SLars Ellenberg unsigned int enr, int count, 6345ab7d2c0SLars Ellenberg enum update_sync_bits_mode mode) 635b411b363SPhilipp Reisner { 636b411b363SPhilipp Reisner struct lc_element *e; 637b411b363SPhilipp Reisner 6380b0ba1efSAndreas Gruenbacher D_ASSERT(device, atomic_read(&device->local_cnt)); 639b411b363SPhilipp Reisner 6405ab7d2c0SLars Ellenberg /* When setting out-of-sync bits, 6415ab7d2c0SLars Ellenberg * we don't need it cached (lc_find). 6425ab7d2c0SLars Ellenberg * But if it is present in the cache, 6435ab7d2c0SLars Ellenberg * we should update the cached bit count. 6445ab7d2c0SLars Ellenberg * Otherwise, that extent should be in the resync extent lru cache 6455ab7d2c0SLars Ellenberg * already -- or we want to pull it in if necessary -- (lc_get), 6465ab7d2c0SLars Ellenberg * then update and check rs_left and rs_failed. */ 6475ab7d2c0SLars Ellenberg if (mode == SET_OUT_OF_SYNC) 6485ab7d2c0SLars Ellenberg e = lc_find(device->resync, enr); 6495ab7d2c0SLars Ellenberg else 650b30ab791SAndreas Gruenbacher e = lc_get(device->resync, enr); 651b411b363SPhilipp Reisner if (e) { 652b411b363SPhilipp Reisner struct bm_extent *ext = lc_entry(e, struct bm_extent, lce); 653b411b363SPhilipp Reisner if (ext->lce.lc_number == enr) { 6545ab7d2c0SLars Ellenberg if (mode == SET_IN_SYNC) 655b411b363SPhilipp Reisner ext->rs_left -= count; 6565ab7d2c0SLars Ellenberg else if (mode == SET_OUT_OF_SYNC) 6575ab7d2c0SLars Ellenberg ext->rs_left += count; 658b411b363SPhilipp Reisner else 659b411b363SPhilipp Reisner ext->rs_failed += count; 660b411b363SPhilipp Reisner if (ext->rs_left < ext->rs_failed) { 6615ab7d2c0SLars Ellenberg drbd_warn(device, "BAD! enr=%u rs_left=%d " 662975b2979SPhilipp Reisner "rs_failed=%d count=%d cstate=%s\n", 663b411b363SPhilipp Reisner ext->lce.lc_number, ext->rs_left, 664975b2979SPhilipp Reisner ext->rs_failed, count, 665b30ab791SAndreas Gruenbacher drbd_conn_str(device->state.conn)); 666b411b363SPhilipp Reisner 667975b2979SPhilipp Reisner /* We don't expect to be able to clear more bits 668975b2979SPhilipp Reisner * than have been set when we originally counted 669975b2979SPhilipp Reisner * the set bits to cache that value in ext->rs_left. 670975b2979SPhilipp Reisner * Whatever the reason (disconnect during resync, 671975b2979SPhilipp Reisner * delayed local completion of an application write), 672975b2979SPhilipp Reisner * try to fix it up by recounting here. */ 673b30ab791SAndreas Gruenbacher ext->rs_left = drbd_bm_e_weight(device, enr); 674b411b363SPhilipp Reisner } 675b411b363SPhilipp Reisner } else { 676b411b363SPhilipp Reisner /* Normally this element should be in the cache, 677b411b363SPhilipp Reisner * since drbd_rs_begin_io() pulled it already in. 678b411b363SPhilipp Reisner * 679b411b363SPhilipp Reisner * But maybe an application write finished, and we set 680b411b363SPhilipp Reisner * something outside the resync lru_cache in sync. 681b411b363SPhilipp Reisner */ 682b30ab791SAndreas Gruenbacher int rs_left = drbd_bm_e_weight(device, enr); 683b411b363SPhilipp Reisner if (ext->flags != 0) { 684d0180171SAndreas Gruenbacher drbd_warn(device, "changing resync lce: %d[%u;%02lx]" 685b411b363SPhilipp Reisner " -> %d[%u;00]\n", 686b411b363SPhilipp Reisner ext->lce.lc_number, ext->rs_left, 687b411b363SPhilipp Reisner ext->flags, enr, rs_left); 688b411b363SPhilipp Reisner ext->flags = 0; 689b411b363SPhilipp Reisner } 690b411b363SPhilipp Reisner if (ext->rs_failed) { 691d0180171SAndreas Gruenbacher drbd_warn(device, "Kicking resync_lru element enr=%u " 692b411b363SPhilipp Reisner "out with rs_failed=%d\n", 693b411b363SPhilipp Reisner ext->lce.lc_number, ext->rs_failed); 694b411b363SPhilipp Reisner } 695b411b363SPhilipp Reisner ext->rs_left = rs_left; 6965ab7d2c0SLars Ellenberg ext->rs_failed = (mode == RECORD_RS_FAILED) ? count : 0; 69746a15bc3SLars Ellenberg /* we don't keep a persistent log of the resync lru, 69846a15bc3SLars Ellenberg * we can commit any change right away. */ 699b30ab791SAndreas Gruenbacher lc_committed(device->resync); 700b411b363SPhilipp Reisner } 7015ab7d2c0SLars Ellenberg if (mode != SET_OUT_OF_SYNC) 702b30ab791SAndreas Gruenbacher lc_put(device->resync, &ext->lce); 703b411b363SPhilipp Reisner /* no race, we are within the al_lock! */ 704b411b363SPhilipp Reisner 7055ab7d2c0SLars Ellenberg if (ext->rs_left <= ext->rs_failed) { 706b411b363SPhilipp Reisner ext->rs_failed = 0; 7075ab7d2c0SLars Ellenberg return true; 708b411b363SPhilipp Reisner } 7095ab7d2c0SLars Ellenberg } else if (mode != SET_OUT_OF_SYNC) { 7105ab7d2c0SLars Ellenberg /* be quiet if lc_find() did not find it. */ 711d0180171SAndreas Gruenbacher drbd_err(device, "lc_get() failed! locked=%d/%d flags=%lu\n", 712b30ab791SAndreas Gruenbacher device->resync_locked, 713b30ab791SAndreas Gruenbacher device->resync->nr_elements, 714b30ab791SAndreas Gruenbacher device->resync->flags); 715b411b363SPhilipp Reisner } 7165ab7d2c0SLars Ellenberg return false; 717b411b363SPhilipp Reisner } 718b411b363SPhilipp Reisner 719b30ab791SAndreas Gruenbacher void drbd_advance_rs_marks(struct drbd_device *device, unsigned long still_to_go) 720c6ea14dfSLars Ellenberg { 721c6ea14dfSLars Ellenberg unsigned long now = jiffies; 722b30ab791SAndreas Gruenbacher unsigned long last = device->rs_mark_time[device->rs_last_mark]; 723b30ab791SAndreas Gruenbacher int next = (device->rs_last_mark + 1) % DRBD_SYNC_MARKS; 724c6ea14dfSLars Ellenberg if (time_after_eq(now, last + DRBD_SYNC_MARK_STEP)) { 725b30ab791SAndreas Gruenbacher if (device->rs_mark_left[device->rs_last_mark] != still_to_go && 726b30ab791SAndreas Gruenbacher device->state.conn != C_PAUSED_SYNC_T && 727b30ab791SAndreas Gruenbacher device->state.conn != C_PAUSED_SYNC_S) { 728b30ab791SAndreas Gruenbacher device->rs_mark_time[next] = now; 729b30ab791SAndreas Gruenbacher device->rs_mark_left[next] = still_to_go; 730b30ab791SAndreas Gruenbacher device->rs_last_mark = next; 731c6ea14dfSLars Ellenberg } 732c6ea14dfSLars Ellenberg } 733c6ea14dfSLars Ellenberg } 734c6ea14dfSLars Ellenberg 7355ab7d2c0SLars Ellenberg /* It is called lazy update, so don't do write-out too often. */ 7365ab7d2c0SLars Ellenberg static bool lazy_bitmap_update_due(struct drbd_device *device) 7375ab7d2c0SLars Ellenberg { 7385ab7d2c0SLars Ellenberg return time_after(jiffies, device->rs_last_bcast + 2*HZ); 7395ab7d2c0SLars Ellenberg } 7405ab7d2c0SLars Ellenberg 7415ab7d2c0SLars Ellenberg static void maybe_schedule_on_disk_bitmap_update(struct drbd_device *device, bool rs_done) 7425ab7d2c0SLars Ellenberg { 7435ab7d2c0SLars Ellenberg if (rs_done) 7445ab7d2c0SLars Ellenberg set_bit(RS_DONE, &device->flags); 7455ab7d2c0SLars Ellenberg /* and also set RS_PROGRESS below */ 7465ab7d2c0SLars Ellenberg else if (!lazy_bitmap_update_due(device)) 7475ab7d2c0SLars Ellenberg return; 7485ab7d2c0SLars Ellenberg 749e334f550SLars Ellenberg drbd_device_post_work(device, RS_PROGRESS); 7505ab7d2c0SLars Ellenberg } 7515ab7d2c0SLars Ellenberg 7525ab7d2c0SLars Ellenberg static int update_sync_bits(struct drbd_device *device, 7535ab7d2c0SLars Ellenberg unsigned long sbnr, unsigned long ebnr, 7545ab7d2c0SLars Ellenberg enum update_sync_bits_mode mode) 7555ab7d2c0SLars Ellenberg { 7565ab7d2c0SLars Ellenberg /* 7575ab7d2c0SLars Ellenberg * We keep a count of set bits per resync-extent in the ->rs_left 7585ab7d2c0SLars Ellenberg * caching member, so we need to loop and work within the resync extent 7595ab7d2c0SLars Ellenberg * alignment. Typically this loop will execute exactly once. 7605ab7d2c0SLars Ellenberg */ 7615ab7d2c0SLars Ellenberg unsigned long flags; 7625ab7d2c0SLars Ellenberg unsigned long count = 0; 7635ab7d2c0SLars Ellenberg unsigned int cleared = 0; 7645ab7d2c0SLars Ellenberg while (sbnr <= ebnr) { 7655ab7d2c0SLars Ellenberg /* set temporary boundary bit number to last bit number within 7665ab7d2c0SLars Ellenberg * the resync extent of the current start bit number, 7675ab7d2c0SLars Ellenberg * but cap at provided end bit number */ 7685ab7d2c0SLars Ellenberg unsigned long tbnr = min(ebnr, sbnr | BM_BLOCKS_PER_BM_EXT_MASK); 7695ab7d2c0SLars Ellenberg unsigned long c; 7705ab7d2c0SLars Ellenberg 7715ab7d2c0SLars Ellenberg if (mode == RECORD_RS_FAILED) 7725ab7d2c0SLars Ellenberg /* Only called from drbd_rs_failed_io(), bits 7735ab7d2c0SLars Ellenberg * supposedly still set. Recount, maybe some 7745ab7d2c0SLars Ellenberg * of the bits have been successfully cleared 7755ab7d2c0SLars Ellenberg * by application IO meanwhile. 7765ab7d2c0SLars Ellenberg */ 7775ab7d2c0SLars Ellenberg c = drbd_bm_count_bits(device, sbnr, tbnr); 7785ab7d2c0SLars Ellenberg else if (mode == SET_IN_SYNC) 7795ab7d2c0SLars Ellenberg c = drbd_bm_clear_bits(device, sbnr, tbnr); 7805ab7d2c0SLars Ellenberg else /* if (mode == SET_OUT_OF_SYNC) */ 7815ab7d2c0SLars Ellenberg c = drbd_bm_set_bits(device, sbnr, tbnr); 7825ab7d2c0SLars Ellenberg 7835ab7d2c0SLars Ellenberg if (c) { 7845ab7d2c0SLars Ellenberg spin_lock_irqsave(&device->al_lock, flags); 7855ab7d2c0SLars Ellenberg cleared += update_rs_extent(device, BM_BIT_TO_EXT(sbnr), c, mode); 7865ab7d2c0SLars Ellenberg spin_unlock_irqrestore(&device->al_lock, flags); 7875ab7d2c0SLars Ellenberg count += c; 7885ab7d2c0SLars Ellenberg } 7895ab7d2c0SLars Ellenberg sbnr = tbnr + 1; 7905ab7d2c0SLars Ellenberg } 7915ab7d2c0SLars Ellenberg if (count) { 7925ab7d2c0SLars Ellenberg if (mode == SET_IN_SYNC) { 7935ab7d2c0SLars Ellenberg unsigned long still_to_go = drbd_bm_total_weight(device); 7945ab7d2c0SLars Ellenberg bool rs_is_done = (still_to_go <= device->rs_failed); 7955ab7d2c0SLars Ellenberg drbd_advance_rs_marks(device, still_to_go); 7965ab7d2c0SLars Ellenberg if (cleared || rs_is_done) 7975ab7d2c0SLars Ellenberg maybe_schedule_on_disk_bitmap_update(device, rs_is_done); 7985ab7d2c0SLars Ellenberg } else if (mode == RECORD_RS_FAILED) 7995ab7d2c0SLars Ellenberg device->rs_failed += count; 8005ab7d2c0SLars Ellenberg wake_up(&device->al_wait); 8015ab7d2c0SLars Ellenberg } 8025ab7d2c0SLars Ellenberg return count; 8035ab7d2c0SLars Ellenberg } 8045ab7d2c0SLars Ellenberg 805b411b363SPhilipp Reisner /* clear the bit corresponding to the piece of storage in question: 806b411b363SPhilipp Reisner * size byte of data starting from sector. Only clear a bits of the affected 807b411b363SPhilipp Reisner * one ore more _aligned_ BM_BLOCK_SIZE blocks. 808b411b363SPhilipp Reisner * 809b411b363SPhilipp Reisner * called by worker on C_SYNC_TARGET and receiver on SyncSource. 810b411b363SPhilipp Reisner * 811b411b363SPhilipp Reisner */ 8125ab7d2c0SLars Ellenberg int __drbd_change_sync(struct drbd_device *device, sector_t sector, int size, 8135ab7d2c0SLars Ellenberg enum update_sync_bits_mode mode, 814b411b363SPhilipp Reisner const char *file, const unsigned int line) 815b411b363SPhilipp Reisner { 816b411b363SPhilipp Reisner /* Is called from worker and receiver context _only_ */ 817b411b363SPhilipp Reisner unsigned long sbnr, ebnr, lbnr; 818b411b363SPhilipp Reisner unsigned long count = 0; 819b411b363SPhilipp Reisner sector_t esector, nr_sectors; 8205ab7d2c0SLars Ellenberg 8215ab7d2c0SLars Ellenberg /* This would be an empty REQ_FLUSH, be silent. */ 8225ab7d2c0SLars Ellenberg if ((mode == SET_OUT_OF_SYNC) && size == 0) 8235ab7d2c0SLars Ellenberg return 0; 824b411b363SPhilipp Reisner 825a0fb3c47SLars Ellenberg if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_DISCARD_SIZE) { 8265ab7d2c0SLars Ellenberg drbd_err(device, "%s: sector=%llus size=%d nonsense!\n", 8275ab7d2c0SLars Ellenberg drbd_change_sync_fname[mode], 828b411b363SPhilipp Reisner (unsigned long long)sector, size); 8295ab7d2c0SLars Ellenberg return 0; 830b411b363SPhilipp Reisner } 831518a4d53SPhilipp Reisner 832b30ab791SAndreas Gruenbacher if (!get_ldev(device)) 8335ab7d2c0SLars Ellenberg return 0; /* no disk, no metadata, no bitmap to manipulate bits in */ 834518a4d53SPhilipp Reisner 835b30ab791SAndreas Gruenbacher nr_sectors = drbd_get_capacity(device->this_bdev); 836b411b363SPhilipp Reisner esector = sector + (size >> 9) - 1; 837b411b363SPhilipp Reisner 838841ce241SAndreas Gruenbacher if (!expect(sector < nr_sectors)) 839518a4d53SPhilipp Reisner goto out; 840841ce241SAndreas Gruenbacher if (!expect(esector < nr_sectors)) 841841ce241SAndreas Gruenbacher esector = nr_sectors - 1; 842b411b363SPhilipp Reisner 843b411b363SPhilipp Reisner lbnr = BM_SECT_TO_BIT(nr_sectors-1); 844b411b363SPhilipp Reisner 8455ab7d2c0SLars Ellenberg if (mode == SET_IN_SYNC) { 8465ab7d2c0SLars Ellenberg /* Round up start sector, round down end sector. We make sure 8475ab7d2c0SLars Ellenberg * we only clear full, aligned, BM_BLOCK_SIZE blocks. */ 848b411b363SPhilipp Reisner if (unlikely(esector < BM_SECT_PER_BIT-1)) 849518a4d53SPhilipp Reisner goto out; 850b411b363SPhilipp Reisner if (unlikely(esector == (nr_sectors-1))) 851b411b363SPhilipp Reisner ebnr = lbnr; 852b411b363SPhilipp Reisner else 853b411b363SPhilipp Reisner ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1)); 854b411b363SPhilipp Reisner sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1); 8555ab7d2c0SLars Ellenberg } else { 8565ab7d2c0SLars Ellenberg /* We set it out of sync, or record resync failure. 8575ab7d2c0SLars Ellenberg * Should not round anything here. */ 858b411b363SPhilipp Reisner sbnr = BM_SECT_TO_BIT(sector); 859b411b363SPhilipp Reisner ebnr = BM_SECT_TO_BIT(esector); 8605ab7d2c0SLars Ellenberg } 861b411b363SPhilipp Reisner 8625ab7d2c0SLars Ellenberg count = update_sync_bits(device, sbnr, ebnr, mode); 863b411b363SPhilipp Reisner out: 864b30ab791SAndreas Gruenbacher put_ldev(device); 86573a01a18SPhilipp Reisner return count; 866b411b363SPhilipp Reisner } 867b411b363SPhilipp Reisner 868b411b363SPhilipp Reisner static 869b30ab791SAndreas Gruenbacher struct bm_extent *_bme_get(struct drbd_device *device, unsigned int enr) 870b411b363SPhilipp Reisner { 871b411b363SPhilipp Reisner struct lc_element *e; 872b411b363SPhilipp Reisner struct bm_extent *bm_ext; 873b411b363SPhilipp Reisner int wakeup = 0; 874b411b363SPhilipp Reisner unsigned long rs_flags; 875b411b363SPhilipp Reisner 876b30ab791SAndreas Gruenbacher spin_lock_irq(&device->al_lock); 877b30ab791SAndreas Gruenbacher if (device->resync_locked > device->resync->nr_elements/2) { 878b30ab791SAndreas Gruenbacher spin_unlock_irq(&device->al_lock); 879b411b363SPhilipp Reisner return NULL; 880b411b363SPhilipp Reisner } 881b30ab791SAndreas Gruenbacher e = lc_get(device->resync, enr); 882b411b363SPhilipp Reisner bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; 883b411b363SPhilipp Reisner if (bm_ext) { 884b411b363SPhilipp Reisner if (bm_ext->lce.lc_number != enr) { 885b30ab791SAndreas Gruenbacher bm_ext->rs_left = drbd_bm_e_weight(device, enr); 886b411b363SPhilipp Reisner bm_ext->rs_failed = 0; 887b30ab791SAndreas Gruenbacher lc_committed(device->resync); 888b411b363SPhilipp Reisner wakeup = 1; 889b411b363SPhilipp Reisner } 890b411b363SPhilipp Reisner if (bm_ext->lce.refcnt == 1) 891b30ab791SAndreas Gruenbacher device->resync_locked++; 892b411b363SPhilipp Reisner set_bit(BME_NO_WRITES, &bm_ext->flags); 893b411b363SPhilipp Reisner } 894b30ab791SAndreas Gruenbacher rs_flags = device->resync->flags; 895b30ab791SAndreas Gruenbacher spin_unlock_irq(&device->al_lock); 896b411b363SPhilipp Reisner if (wakeup) 897b30ab791SAndreas Gruenbacher wake_up(&device->al_wait); 898b411b363SPhilipp Reisner 899b411b363SPhilipp Reisner if (!bm_ext) { 900b411b363SPhilipp Reisner if (rs_flags & LC_STARVING) 901d0180171SAndreas Gruenbacher drbd_warn(device, "Have to wait for element" 902b411b363SPhilipp Reisner " (resync LRU too small?)\n"); 90346a15bc3SLars Ellenberg BUG_ON(rs_flags & LC_LOCKED); 904b411b363SPhilipp Reisner } 905b411b363SPhilipp Reisner 906b411b363SPhilipp Reisner return bm_ext; 907b411b363SPhilipp Reisner } 908b411b363SPhilipp Reisner 909b30ab791SAndreas Gruenbacher static int _is_in_al(struct drbd_device *device, unsigned int enr) 910b411b363SPhilipp Reisner { 91146a15bc3SLars Ellenberg int rv; 912b411b363SPhilipp Reisner 913b30ab791SAndreas Gruenbacher spin_lock_irq(&device->al_lock); 914b30ab791SAndreas Gruenbacher rv = lc_is_used(device->act_log, enr); 915b30ab791SAndreas Gruenbacher spin_unlock_irq(&device->al_lock); 916b411b363SPhilipp Reisner 917b411b363SPhilipp Reisner return rv; 918b411b363SPhilipp Reisner } 919b411b363SPhilipp Reisner 920b411b363SPhilipp Reisner /** 921b411b363SPhilipp Reisner * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED 922b30ab791SAndreas Gruenbacher * @device: DRBD device. 923b411b363SPhilipp Reisner * @sector: The sector number. 924b411b363SPhilipp Reisner * 92580a40e43SLars Ellenberg * This functions sleeps on al_wait. Returns 0 on success, -EINTR if interrupted. 926b411b363SPhilipp Reisner */ 927b30ab791SAndreas Gruenbacher int drbd_rs_begin_io(struct drbd_device *device, sector_t sector) 928b411b363SPhilipp Reisner { 929b411b363SPhilipp Reisner unsigned int enr = BM_SECT_TO_EXT(sector); 930b411b363SPhilipp Reisner struct bm_extent *bm_ext; 931b411b363SPhilipp Reisner int i, sig; 932e8299874SLars Ellenberg bool sa; 933b411b363SPhilipp Reisner 934f91ab628SPhilipp Reisner retry: 935b30ab791SAndreas Gruenbacher sig = wait_event_interruptible(device->al_wait, 936b30ab791SAndreas Gruenbacher (bm_ext = _bme_get(device, enr))); 937b411b363SPhilipp Reisner if (sig) 93880a40e43SLars Ellenberg return -EINTR; 939b411b363SPhilipp Reisner 940b411b363SPhilipp Reisner if (test_bit(BME_LOCKED, &bm_ext->flags)) 94180a40e43SLars Ellenberg return 0; 942b411b363SPhilipp Reisner 943e8299874SLars Ellenberg /* step aside only while we are above c-min-rate; unless disabled. */ 944e8299874SLars Ellenberg sa = drbd_rs_c_min_rate_throttle(device); 945e8299874SLars Ellenberg 946b411b363SPhilipp Reisner for (i = 0; i < AL_EXT_PER_BM_SECT; i++) { 947b30ab791SAndreas Gruenbacher sig = wait_event_interruptible(device->al_wait, 948b30ab791SAndreas Gruenbacher !_is_in_al(device, enr * AL_EXT_PER_BM_SECT + i) || 949e8299874SLars Ellenberg (sa && test_bit(BME_PRIORITY, &bm_ext->flags))); 950f91ab628SPhilipp Reisner 951e8299874SLars Ellenberg if (sig || (sa && test_bit(BME_PRIORITY, &bm_ext->flags))) { 952b30ab791SAndreas Gruenbacher spin_lock_irq(&device->al_lock); 953b30ab791SAndreas Gruenbacher if (lc_put(device->resync, &bm_ext->lce) == 0) { 954f91ab628SPhilipp Reisner bm_ext->flags = 0; /* clears BME_NO_WRITES and eventually BME_PRIORITY */ 955b30ab791SAndreas Gruenbacher device->resync_locked--; 956b30ab791SAndreas Gruenbacher wake_up(&device->al_wait); 957b411b363SPhilipp Reisner } 958b30ab791SAndreas Gruenbacher spin_unlock_irq(&device->al_lock); 959f91ab628SPhilipp Reisner if (sig) 96080a40e43SLars Ellenberg return -EINTR; 961f91ab628SPhilipp Reisner if (schedule_timeout_interruptible(HZ/10)) 962f91ab628SPhilipp Reisner return -EINTR; 963f91ab628SPhilipp Reisner goto retry; 964b411b363SPhilipp Reisner } 965b411b363SPhilipp Reisner } 966b411b363SPhilipp Reisner set_bit(BME_LOCKED, &bm_ext->flags); 96780a40e43SLars Ellenberg return 0; 968b411b363SPhilipp Reisner } 969b411b363SPhilipp Reisner 970b411b363SPhilipp Reisner /** 971b411b363SPhilipp Reisner * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep 972b30ab791SAndreas Gruenbacher * @device: DRBD device. 973b411b363SPhilipp Reisner * @sector: The sector number. 974b411b363SPhilipp Reisner * 975b411b363SPhilipp Reisner * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then 976b411b363SPhilipp Reisner * tries to set it to BME_LOCKED. Returns 0 upon success, and -EAGAIN 977b411b363SPhilipp Reisner * if there is still application IO going on in this area. 978b411b363SPhilipp Reisner */ 979b30ab791SAndreas Gruenbacher int drbd_try_rs_begin_io(struct drbd_device *device, sector_t sector) 980b411b363SPhilipp Reisner { 981b411b363SPhilipp Reisner unsigned int enr = BM_SECT_TO_EXT(sector); 982b411b363SPhilipp Reisner const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT; 983b411b363SPhilipp Reisner struct lc_element *e; 984b411b363SPhilipp Reisner struct bm_extent *bm_ext; 985b411b363SPhilipp Reisner int i; 986b411b363SPhilipp Reisner 987b30ab791SAndreas Gruenbacher spin_lock_irq(&device->al_lock); 988b30ab791SAndreas Gruenbacher if (device->resync_wenr != LC_FREE && device->resync_wenr != enr) { 989b411b363SPhilipp Reisner /* in case you have very heavy scattered io, it may 990b411b363SPhilipp Reisner * stall the syncer undefined if we give up the ref count 991b411b363SPhilipp Reisner * when we try again and requeue. 992b411b363SPhilipp Reisner * 993b411b363SPhilipp Reisner * if we don't give up the refcount, but the next time 994b411b363SPhilipp Reisner * we are scheduled this extent has been "synced" by new 995b411b363SPhilipp Reisner * application writes, we'd miss the lc_put on the 996b411b363SPhilipp Reisner * extent we keep the refcount on. 997b411b363SPhilipp Reisner * so we remembered which extent we had to try again, and 998b411b363SPhilipp Reisner * if the next requested one is something else, we do 999b411b363SPhilipp Reisner * the lc_put here... 1000b411b363SPhilipp Reisner * we also have to wake_up 1001b411b363SPhilipp Reisner */ 1002b30ab791SAndreas Gruenbacher e = lc_find(device->resync, device->resync_wenr); 1003b411b363SPhilipp Reisner bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; 1004b411b363SPhilipp Reisner if (bm_ext) { 10050b0ba1efSAndreas Gruenbacher D_ASSERT(device, !test_bit(BME_LOCKED, &bm_ext->flags)); 10060b0ba1efSAndreas Gruenbacher D_ASSERT(device, test_bit(BME_NO_WRITES, &bm_ext->flags)); 1007b411b363SPhilipp Reisner clear_bit(BME_NO_WRITES, &bm_ext->flags); 1008b30ab791SAndreas Gruenbacher device->resync_wenr = LC_FREE; 1009b30ab791SAndreas Gruenbacher if (lc_put(device->resync, &bm_ext->lce) == 0) 1010b30ab791SAndreas Gruenbacher device->resync_locked--; 1011b30ab791SAndreas Gruenbacher wake_up(&device->al_wait); 1012b411b363SPhilipp Reisner } else { 1013d0180171SAndreas Gruenbacher drbd_alert(device, "LOGIC BUG\n"); 1014b411b363SPhilipp Reisner } 1015b411b363SPhilipp Reisner } 1016b411b363SPhilipp Reisner /* TRY. */ 1017b30ab791SAndreas Gruenbacher e = lc_try_get(device->resync, enr); 1018b411b363SPhilipp Reisner bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; 1019b411b363SPhilipp Reisner if (bm_ext) { 1020b411b363SPhilipp Reisner if (test_bit(BME_LOCKED, &bm_ext->flags)) 1021b411b363SPhilipp Reisner goto proceed; 1022b411b363SPhilipp Reisner if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) { 1023b30ab791SAndreas Gruenbacher device->resync_locked++; 1024b411b363SPhilipp Reisner } else { 1025b411b363SPhilipp Reisner /* we did set the BME_NO_WRITES, 1026b411b363SPhilipp Reisner * but then could not set BME_LOCKED, 1027b411b363SPhilipp Reisner * so we tried again. 1028b411b363SPhilipp Reisner * drop the extra reference. */ 1029b411b363SPhilipp Reisner bm_ext->lce.refcnt--; 10300b0ba1efSAndreas Gruenbacher D_ASSERT(device, bm_ext->lce.refcnt > 0); 1031b411b363SPhilipp Reisner } 1032b411b363SPhilipp Reisner goto check_al; 1033b411b363SPhilipp Reisner } else { 1034b411b363SPhilipp Reisner /* do we rather want to try later? */ 1035b30ab791SAndreas Gruenbacher if (device->resync_locked > device->resync->nr_elements-3) 1036b411b363SPhilipp Reisner goto try_again; 1037b411b363SPhilipp Reisner /* Do or do not. There is no try. -- Yoda */ 1038b30ab791SAndreas Gruenbacher e = lc_get(device->resync, enr); 1039b411b363SPhilipp Reisner bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; 1040b411b363SPhilipp Reisner if (!bm_ext) { 1041b30ab791SAndreas Gruenbacher const unsigned long rs_flags = device->resync->flags; 1042b411b363SPhilipp Reisner if (rs_flags & LC_STARVING) 1043d0180171SAndreas Gruenbacher drbd_warn(device, "Have to wait for element" 1044b411b363SPhilipp Reisner " (resync LRU too small?)\n"); 104546a15bc3SLars Ellenberg BUG_ON(rs_flags & LC_LOCKED); 1046b411b363SPhilipp Reisner goto try_again; 1047b411b363SPhilipp Reisner } 1048b411b363SPhilipp Reisner if (bm_ext->lce.lc_number != enr) { 1049b30ab791SAndreas Gruenbacher bm_ext->rs_left = drbd_bm_e_weight(device, enr); 1050b411b363SPhilipp Reisner bm_ext->rs_failed = 0; 1051b30ab791SAndreas Gruenbacher lc_committed(device->resync); 1052b30ab791SAndreas Gruenbacher wake_up(&device->al_wait); 10530b0ba1efSAndreas Gruenbacher D_ASSERT(device, test_bit(BME_LOCKED, &bm_ext->flags) == 0); 1054b411b363SPhilipp Reisner } 1055b411b363SPhilipp Reisner set_bit(BME_NO_WRITES, &bm_ext->flags); 10560b0ba1efSAndreas Gruenbacher D_ASSERT(device, bm_ext->lce.refcnt == 1); 1057b30ab791SAndreas Gruenbacher device->resync_locked++; 1058b411b363SPhilipp Reisner goto check_al; 1059b411b363SPhilipp Reisner } 1060b411b363SPhilipp Reisner check_al: 1061b411b363SPhilipp Reisner for (i = 0; i < AL_EXT_PER_BM_SECT; i++) { 1062b30ab791SAndreas Gruenbacher if (lc_is_used(device->act_log, al_enr+i)) 1063b411b363SPhilipp Reisner goto try_again; 1064b411b363SPhilipp Reisner } 1065b411b363SPhilipp Reisner set_bit(BME_LOCKED, &bm_ext->flags); 1066b411b363SPhilipp Reisner proceed: 1067b30ab791SAndreas Gruenbacher device->resync_wenr = LC_FREE; 1068b30ab791SAndreas Gruenbacher spin_unlock_irq(&device->al_lock); 1069b411b363SPhilipp Reisner return 0; 1070b411b363SPhilipp Reisner 1071b411b363SPhilipp Reisner try_again: 1072b411b363SPhilipp Reisner if (bm_ext) 1073b30ab791SAndreas Gruenbacher device->resync_wenr = enr; 1074b30ab791SAndreas Gruenbacher spin_unlock_irq(&device->al_lock); 1075b411b363SPhilipp Reisner return -EAGAIN; 1076b411b363SPhilipp Reisner } 1077b411b363SPhilipp Reisner 1078b30ab791SAndreas Gruenbacher void drbd_rs_complete_io(struct drbd_device *device, sector_t sector) 1079b411b363SPhilipp Reisner { 1080b411b363SPhilipp Reisner unsigned int enr = BM_SECT_TO_EXT(sector); 1081b411b363SPhilipp Reisner struct lc_element *e; 1082b411b363SPhilipp Reisner struct bm_extent *bm_ext; 1083b411b363SPhilipp Reisner unsigned long flags; 1084b411b363SPhilipp Reisner 1085b30ab791SAndreas Gruenbacher spin_lock_irqsave(&device->al_lock, flags); 1086b30ab791SAndreas Gruenbacher e = lc_find(device->resync, enr); 1087b411b363SPhilipp Reisner bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; 1088b411b363SPhilipp Reisner if (!bm_ext) { 1089b30ab791SAndreas Gruenbacher spin_unlock_irqrestore(&device->al_lock, flags); 1090b411b363SPhilipp Reisner if (__ratelimit(&drbd_ratelimit_state)) 1091d0180171SAndreas Gruenbacher drbd_err(device, "drbd_rs_complete_io() called, but extent not found\n"); 1092b411b363SPhilipp Reisner return; 1093b411b363SPhilipp Reisner } 1094b411b363SPhilipp Reisner 1095b411b363SPhilipp Reisner if (bm_ext->lce.refcnt == 0) { 1096b30ab791SAndreas Gruenbacher spin_unlock_irqrestore(&device->al_lock, flags); 1097d0180171SAndreas Gruenbacher drbd_err(device, "drbd_rs_complete_io(,%llu [=%u]) called, " 1098b411b363SPhilipp Reisner "but refcnt is 0!?\n", 1099b411b363SPhilipp Reisner (unsigned long long)sector, enr); 1100b411b363SPhilipp Reisner return; 1101b411b363SPhilipp Reisner } 1102b411b363SPhilipp Reisner 1103b30ab791SAndreas Gruenbacher if (lc_put(device->resync, &bm_ext->lce) == 0) { 1104e3555d85SPhilipp Reisner bm_ext->flags = 0; /* clear BME_LOCKED, BME_NO_WRITES and BME_PRIORITY */ 1105b30ab791SAndreas Gruenbacher device->resync_locked--; 1106b30ab791SAndreas Gruenbacher wake_up(&device->al_wait); 1107b411b363SPhilipp Reisner } 1108b411b363SPhilipp Reisner 1109b30ab791SAndreas Gruenbacher spin_unlock_irqrestore(&device->al_lock, flags); 1110b411b363SPhilipp Reisner } 1111b411b363SPhilipp Reisner 1112b411b363SPhilipp Reisner /** 1113b411b363SPhilipp Reisner * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED) 1114b30ab791SAndreas Gruenbacher * @device: DRBD device. 1115b411b363SPhilipp Reisner */ 1116b30ab791SAndreas Gruenbacher void drbd_rs_cancel_all(struct drbd_device *device) 1117b411b363SPhilipp Reisner { 1118b30ab791SAndreas Gruenbacher spin_lock_irq(&device->al_lock); 1119b411b363SPhilipp Reisner 1120b30ab791SAndreas Gruenbacher if (get_ldev_if_state(device, D_FAILED)) { /* Makes sure ->resync is there. */ 1121b30ab791SAndreas Gruenbacher lc_reset(device->resync); 1122b30ab791SAndreas Gruenbacher put_ldev(device); 1123b411b363SPhilipp Reisner } 1124b30ab791SAndreas Gruenbacher device->resync_locked = 0; 1125b30ab791SAndreas Gruenbacher device->resync_wenr = LC_FREE; 1126b30ab791SAndreas Gruenbacher spin_unlock_irq(&device->al_lock); 1127b30ab791SAndreas Gruenbacher wake_up(&device->al_wait); 1128b411b363SPhilipp Reisner } 1129b411b363SPhilipp Reisner 1130b411b363SPhilipp Reisner /** 1131b411b363SPhilipp Reisner * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU 1132b30ab791SAndreas Gruenbacher * @device: DRBD device. 1133b411b363SPhilipp Reisner * 1134b411b363SPhilipp Reisner * Returns 0 upon success, -EAGAIN if at least one reference count was 1135b411b363SPhilipp Reisner * not zero. 1136b411b363SPhilipp Reisner */ 1137b30ab791SAndreas Gruenbacher int drbd_rs_del_all(struct drbd_device *device) 1138b411b363SPhilipp Reisner { 1139b411b363SPhilipp Reisner struct lc_element *e; 1140b411b363SPhilipp Reisner struct bm_extent *bm_ext; 1141b411b363SPhilipp Reisner int i; 1142b411b363SPhilipp Reisner 1143b30ab791SAndreas Gruenbacher spin_lock_irq(&device->al_lock); 1144b411b363SPhilipp Reisner 1145b30ab791SAndreas Gruenbacher if (get_ldev_if_state(device, D_FAILED)) { 1146b411b363SPhilipp Reisner /* ok, ->resync is there. */ 1147b30ab791SAndreas Gruenbacher for (i = 0; i < device->resync->nr_elements; i++) { 1148b30ab791SAndreas Gruenbacher e = lc_element_by_index(device->resync, i); 1149b2b163ddSPhilipp Reisner bm_ext = lc_entry(e, struct bm_extent, lce); 1150b411b363SPhilipp Reisner if (bm_ext->lce.lc_number == LC_FREE) 1151b411b363SPhilipp Reisner continue; 1152b30ab791SAndreas Gruenbacher if (bm_ext->lce.lc_number == device->resync_wenr) { 1153d0180171SAndreas Gruenbacher drbd_info(device, "dropping %u in drbd_rs_del_all, apparently" 1154b411b363SPhilipp Reisner " got 'synced' by application io\n", 1155b30ab791SAndreas Gruenbacher device->resync_wenr); 11560b0ba1efSAndreas Gruenbacher D_ASSERT(device, !test_bit(BME_LOCKED, &bm_ext->flags)); 11570b0ba1efSAndreas Gruenbacher D_ASSERT(device, test_bit(BME_NO_WRITES, &bm_ext->flags)); 1158b411b363SPhilipp Reisner clear_bit(BME_NO_WRITES, &bm_ext->flags); 1159b30ab791SAndreas Gruenbacher device->resync_wenr = LC_FREE; 1160b30ab791SAndreas Gruenbacher lc_put(device->resync, &bm_ext->lce); 1161b411b363SPhilipp Reisner } 1162b411b363SPhilipp Reisner if (bm_ext->lce.refcnt != 0) { 1163d0180171SAndreas Gruenbacher drbd_info(device, "Retrying drbd_rs_del_all() later. " 1164b411b363SPhilipp Reisner "refcnt=%d\n", bm_ext->lce.refcnt); 1165b30ab791SAndreas Gruenbacher put_ldev(device); 1166b30ab791SAndreas Gruenbacher spin_unlock_irq(&device->al_lock); 1167b411b363SPhilipp Reisner return -EAGAIN; 1168b411b363SPhilipp Reisner } 11690b0ba1efSAndreas Gruenbacher D_ASSERT(device, !test_bit(BME_LOCKED, &bm_ext->flags)); 11700b0ba1efSAndreas Gruenbacher D_ASSERT(device, !test_bit(BME_NO_WRITES, &bm_ext->flags)); 1171b30ab791SAndreas Gruenbacher lc_del(device->resync, &bm_ext->lce); 1172b411b363SPhilipp Reisner } 11730b0ba1efSAndreas Gruenbacher D_ASSERT(device, device->resync->used == 0); 1174b30ab791SAndreas Gruenbacher put_ldev(device); 1175b411b363SPhilipp Reisner } 1176b30ab791SAndreas Gruenbacher spin_unlock_irq(&device->al_lock); 1177b30ab791SAndreas Gruenbacher wake_up(&device->al_wait); 1178b411b363SPhilipp Reisner 1179b411b363SPhilipp Reisner return 0; 1180b411b363SPhilipp Reisner } 1181