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 107b411b363SPhilipp Reisner 108*b30ab791SAndreas Gruenbacher void *drbd_md_get_buffer(struct drbd_device *device) 109cdfda633SPhilipp Reisner { 110cdfda633SPhilipp Reisner int r; 111cdfda633SPhilipp Reisner 112*b30ab791SAndreas Gruenbacher wait_event(device->misc_wait, 113*b30ab791SAndreas Gruenbacher (r = atomic_cmpxchg(&device->md_io_in_use, 0, 1)) == 0 || 114*b30ab791SAndreas Gruenbacher device->state.disk <= D_FAILED); 115cdfda633SPhilipp Reisner 116*b30ab791SAndreas Gruenbacher return r ? NULL : page_address(device->md_io_page); 117cdfda633SPhilipp Reisner } 118cdfda633SPhilipp Reisner 119*b30ab791SAndreas Gruenbacher void drbd_md_put_buffer(struct drbd_device *device) 120cdfda633SPhilipp Reisner { 121*b30ab791SAndreas Gruenbacher if (atomic_dec_and_test(&device->md_io_in_use)) 122*b30ab791SAndreas Gruenbacher wake_up(&device->misc_wait); 123cdfda633SPhilipp Reisner } 124cdfda633SPhilipp Reisner 125*b30ab791SAndreas Gruenbacher void wait_until_done_or_force_detached(struct drbd_device *device, struct drbd_backing_dev *bdev, 12632db80f6SPhilipp Reisner unsigned int *done) 127cdfda633SPhilipp Reisner { 12832db80f6SPhilipp Reisner long dt; 12932db80f6SPhilipp Reisner 13032db80f6SPhilipp Reisner rcu_read_lock(); 13132db80f6SPhilipp Reisner dt = rcu_dereference(bdev->disk_conf)->disk_timeout; 13232db80f6SPhilipp Reisner rcu_read_unlock(); 13332db80f6SPhilipp Reisner dt = dt * HZ / 10; 13432db80f6SPhilipp Reisner if (dt == 0) 13532db80f6SPhilipp Reisner dt = MAX_SCHEDULE_TIMEOUT; 13632db80f6SPhilipp Reisner 137*b30ab791SAndreas Gruenbacher dt = wait_event_timeout(device->misc_wait, 138*b30ab791SAndreas Gruenbacher *done || test_bit(FORCE_DETACH, &device->flags), dt); 139e34b677dSLars Ellenberg if (dt == 0) { 14032db80f6SPhilipp Reisner dev_err(DEV, "meta-data IO operation timed out\n"); 141*b30ab791SAndreas Gruenbacher drbd_chk_io_error(device, 1, DRBD_FORCE_DETACH); 142e34b677dSLars Ellenberg } 143cdfda633SPhilipp Reisner } 144cdfda633SPhilipp Reisner 145*b30ab791SAndreas Gruenbacher static int _drbd_md_sync_page_io(struct drbd_device *device, 146b411b363SPhilipp Reisner struct drbd_backing_dev *bdev, 147b411b363SPhilipp Reisner struct page *page, sector_t sector, 148b411b363SPhilipp Reisner int rw, int size) 149b411b363SPhilipp Reisner { 150b411b363SPhilipp Reisner struct bio *bio; 151ac29f403SAndreas Gruenbacher int err; 152b411b363SPhilipp Reisner 153*b30ab791SAndreas Gruenbacher device->md_io.done = 0; 154*b30ab791SAndreas Gruenbacher device->md_io.error = -ENODEV; 155b411b363SPhilipp Reisner 156*b30ab791SAndreas Gruenbacher if ((rw & WRITE) && !test_bit(MD_NO_FUA, &device->flags)) 15786e1e98eSLars Ellenberg rw |= REQ_FUA | REQ_FLUSH; 158721a9602SJens Axboe rw |= REQ_SYNC; 159b411b363SPhilipp Reisner 160da4a75d2SLars Ellenberg bio = bio_alloc_drbd(GFP_NOIO); 161b411b363SPhilipp Reisner bio->bi_bdev = bdev->md_bdev; 1624f024f37SKent Overstreet bio->bi_iter.bi_sector = sector; 163ac29f403SAndreas Gruenbacher err = -EIO; 164ac29f403SAndreas Gruenbacher if (bio_add_page(bio, page, size, 0) != size) 165b411b363SPhilipp Reisner goto out; 166*b30ab791SAndreas Gruenbacher bio->bi_private = &device->md_io; 167b411b363SPhilipp Reisner bio->bi_end_io = drbd_md_io_complete; 168b411b363SPhilipp Reisner bio->bi_rw = rw; 169b411b363SPhilipp Reisner 170*b30ab791SAndreas Gruenbacher if (!(rw & WRITE) && device->state.disk == D_DISKLESS && device->ldev == NULL) 171c04ccaa6SLars Ellenberg /* special case, drbd_md_read() during drbd_adm_attach(): no get_ldev */ 172c04ccaa6SLars Ellenberg ; 173*b30ab791SAndreas Gruenbacher else if (!get_ldev_if_state(device, D_ATTACHING)) { 174c04ccaa6SLars Ellenberg /* 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 */ 181*b30ab791SAndreas Gruenbacher atomic_inc(&device->md_io_in_use); /* drbd_md_put_buffer() is in the completion handler */ 182*b30ab791SAndreas Gruenbacher if (drbd_insert_fault(device, (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*b30ab791SAndreas Gruenbacher wait_until_done_or_force_detached(device, bdev, &device->md_io.done); 187ac29f403SAndreas Gruenbacher if (bio_flagged(bio, BIO_UPTODATE)) 188*b30ab791SAndreas Gruenbacher err = device->md_io.error; 189b411b363SPhilipp Reisner 190b411b363SPhilipp Reisner out: 191b411b363SPhilipp Reisner bio_put(bio); 192ac29f403SAndreas Gruenbacher return err; 193b411b363SPhilipp Reisner } 194b411b363SPhilipp Reisner 195*b30ab791SAndreas Gruenbacher int drbd_md_sync_page_io(struct drbd_device *device, struct drbd_backing_dev *bdev, 196b411b363SPhilipp Reisner sector_t sector, int rw) 197b411b363SPhilipp Reisner { 1983fbf4d21SAndreas Gruenbacher int err; 199*b30ab791SAndreas Gruenbacher struct page *iop = device->md_io_page; 200b411b363SPhilipp Reisner 201*b30ab791SAndreas Gruenbacher D_ASSERT(atomic_read(&device->md_io_in_use) == 1); 202b411b363SPhilipp Reisner 203b411b363SPhilipp Reisner BUG_ON(!bdev->md_bdev); 204b411b363SPhilipp Reisner 205c04ccaa6SLars Ellenberg dev_dbg(DEV, "meta_data io: %s [%d]:%s(,%llus,%s) %pS\n", 2067ad651b5SLars Ellenberg current->comm, current->pid, __func__, 207c04ccaa6SLars Ellenberg (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ", 208c04ccaa6SLars Ellenberg (void*)_RET_IP_ ); 209b411b363SPhilipp Reisner 210b411b363SPhilipp Reisner if (sector < drbd_md_first_sector(bdev) || 2117ad651b5SLars Ellenberg sector + 7 > drbd_md_last_sector(bdev)) 212b411b363SPhilipp Reisner dev_alert(DEV, "%s [%d]:%s(,%llus,%s) out of range md access!\n", 213b411b363SPhilipp Reisner current->comm, current->pid, __func__, 214b411b363SPhilipp Reisner (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ"); 215b411b363SPhilipp Reisner 216ae8bf312SLars Ellenberg /* we do all our meta data IO in aligned 4k blocks. */ 217*b30ab791SAndreas Gruenbacher err = _drbd_md_sync_page_io(device, bdev, iop, sector, rw, 4096); 2183fbf4d21SAndreas Gruenbacher if (err) { 219935be260SAndreas Gruenbacher dev_err(DEV, "drbd_md_sync_page_io(,%llus,%s) failed with error %d\n", 220935be260SAndreas Gruenbacher (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ", err); 221b411b363SPhilipp Reisner } 2223fbf4d21SAndreas Gruenbacher return err; 223b411b363SPhilipp Reisner } 224b411b363SPhilipp Reisner 225*b30ab791SAndreas Gruenbacher static struct bm_extent *find_active_resync_extent(struct drbd_device *device, unsigned int enr) 226b411b363SPhilipp Reisner { 227b411b363SPhilipp Reisner struct lc_element *tmp; 228*b30ab791SAndreas Gruenbacher tmp = lc_find(device->resync, enr/AL_EXT_PER_BM_SECT); 229b411b363SPhilipp Reisner if (unlikely(tmp != NULL)) { 230b411b363SPhilipp Reisner struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce); 2316c3c4355SLars Ellenberg if (test_bit(BME_NO_WRITES, &bm_ext->flags)) 2326c3c4355SLars Ellenberg return bm_ext; 2336c3c4355SLars Ellenberg } 2346c3c4355SLars Ellenberg return NULL; 2356c3c4355SLars Ellenberg } 2366c3c4355SLars Ellenberg 237*b30ab791SAndreas Gruenbacher static struct lc_element *_al_get(struct drbd_device *device, unsigned int enr, bool nonblock) 2386c3c4355SLars Ellenberg { 2396c3c4355SLars Ellenberg struct lc_element *al_ext; 2406c3c4355SLars Ellenberg struct bm_extent *bm_ext; 2416c3c4355SLars Ellenberg int wake; 2426c3c4355SLars Ellenberg 243*b30ab791SAndreas Gruenbacher spin_lock_irq(&device->al_lock); 244*b30ab791SAndreas Gruenbacher bm_ext = find_active_resync_extent(device, enr); 2456c3c4355SLars Ellenberg if (bm_ext) { 246f91ab628SPhilipp Reisner wake = !test_and_set_bit(BME_PRIORITY, &bm_ext->flags); 247*b30ab791SAndreas Gruenbacher spin_unlock_irq(&device->al_lock); 248f91ab628SPhilipp Reisner if (wake) 249*b30ab791SAndreas Gruenbacher wake_up(&device->al_wait); 250b411b363SPhilipp Reisner return NULL; 251b411b363SPhilipp Reisner } 2526c3c4355SLars Ellenberg if (nonblock) 253*b30ab791SAndreas Gruenbacher al_ext = lc_try_get(device->act_log, enr); 2546c3c4355SLars Ellenberg else 255*b30ab791SAndreas Gruenbacher al_ext = lc_get(device->act_log, enr); 256*b30ab791SAndreas Gruenbacher spin_unlock_irq(&device->al_lock); 257b411b363SPhilipp Reisner return al_ext; 258b411b363SPhilipp Reisner } 259b411b363SPhilipp Reisner 260*b30ab791SAndreas Gruenbacher bool drbd_al_begin_io_fastpath(struct drbd_device *device, struct drbd_interval *i) 261b5bc8e08SLars Ellenberg { 262b5bc8e08SLars Ellenberg /* for bios crossing activity log extent boundaries, 263b5bc8e08SLars Ellenberg * we may need to activate two extents in one go */ 264b5bc8e08SLars Ellenberg unsigned first = i->sector >> (AL_EXTENT_SHIFT-9); 265b5bc8e08SLars Ellenberg unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9); 266b5bc8e08SLars Ellenberg 267b5bc8e08SLars Ellenberg D_ASSERT((unsigned)(last - first) <= 1); 268*b30ab791SAndreas Gruenbacher D_ASSERT(atomic_read(&device->local_cnt) > 0); 269b5bc8e08SLars Ellenberg 270b5bc8e08SLars Ellenberg /* FIXME figure out a fast path for bios crossing AL extent boundaries */ 271b5bc8e08SLars Ellenberg if (first != last) 272b5bc8e08SLars Ellenberg return false; 273b5bc8e08SLars Ellenberg 274*b30ab791SAndreas Gruenbacher return _al_get(device, first, true); 275b5bc8e08SLars Ellenberg } 276b5bc8e08SLars Ellenberg 277a99efafcSRashika Kheria static 278*b30ab791SAndreas Gruenbacher bool drbd_al_begin_io_prepare(struct drbd_device *device, struct drbd_interval *i) 279b411b363SPhilipp Reisner { 2807726547eSLars Ellenberg /* for bios crossing activity log extent boundaries, 2817726547eSLars Ellenberg * we may need to activate two extents in one go */ 282e15766e9SLars Ellenberg unsigned first = i->sector >> (AL_EXTENT_SHIFT-9); 28381a3537aSLars Ellenberg unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9); 284e15766e9SLars Ellenberg unsigned enr; 285ebfd5d8fSLars Ellenberg bool need_transaction = false; 286b411b363SPhilipp Reisner 28781a3537aSLars Ellenberg D_ASSERT(first <= last); 288*b30ab791SAndreas Gruenbacher D_ASSERT(atomic_read(&device->local_cnt) > 0); 289b411b363SPhilipp Reisner 290ebfd5d8fSLars Ellenberg for (enr = first; enr <= last; enr++) { 291ebfd5d8fSLars Ellenberg struct lc_element *al_ext; 292*b30ab791SAndreas Gruenbacher wait_event(device->al_wait, 293*b30ab791SAndreas Gruenbacher (al_ext = _al_get(device, enr, false)) != NULL); 294ebfd5d8fSLars Ellenberg if (al_ext->lc_number != enr) 295ebfd5d8fSLars Ellenberg need_transaction = true; 296ebfd5d8fSLars Ellenberg } 297b5bc8e08SLars Ellenberg return need_transaction; 298b5bc8e08SLars Ellenberg } 299ebfd5d8fSLars Ellenberg 300*b30ab791SAndreas Gruenbacher static int al_write_transaction(struct drbd_device *device, bool delegate); 301b5bc8e08SLars Ellenberg 302b5bc8e08SLars Ellenberg /* When called through generic_make_request(), we must delegate 303b5bc8e08SLars Ellenberg * activity log I/O to the worker thread: a further request 304b5bc8e08SLars Ellenberg * submitted via generic_make_request() within the same task 305b5bc8e08SLars Ellenberg * would be queued on current->bio_list, and would only start 306b5bc8e08SLars Ellenberg * after this function returns (see generic_make_request()). 307b5bc8e08SLars Ellenberg * 308b5bc8e08SLars Ellenberg * However, if we *are* the worker, we must not delegate to ourselves. 309b5bc8e08SLars Ellenberg */ 310b5bc8e08SLars Ellenberg 311b5bc8e08SLars Ellenberg /* 312b5bc8e08SLars Ellenberg * @delegate: delegate activity log I/O to the worker thread 313b5bc8e08SLars Ellenberg */ 314*b30ab791SAndreas Gruenbacher void drbd_al_begin_io_commit(struct drbd_device *device, bool delegate) 315b5bc8e08SLars Ellenberg { 316b5bc8e08SLars Ellenberg bool locked = false; 317b5bc8e08SLars Ellenberg 318*b30ab791SAndreas Gruenbacher BUG_ON(delegate && current == device->tconn->worker.task); 319b411b363SPhilipp Reisner 3207dc1d67fSLars Ellenberg /* Serialize multiple transactions. 3217dc1d67fSLars Ellenberg * This uses test_and_set_bit, memory barrier is implicit. 3227dc1d67fSLars Ellenberg */ 323*b30ab791SAndreas Gruenbacher wait_event(device->al_wait, 324*b30ab791SAndreas Gruenbacher device->act_log->pending_changes == 0 || 325*b30ab791SAndreas Gruenbacher (locked = lc_try_lock_for_transaction(device->act_log))); 3267dc1d67fSLars Ellenberg 3277dc1d67fSLars Ellenberg if (locked) { 3287ad651b5SLars Ellenberg /* Double check: it may have been committed by someone else, 3297ad651b5SLars Ellenberg * while we have been waiting for the lock. */ 330*b30ab791SAndreas Gruenbacher if (device->act_log->pending_changes) { 3319a51ab1cSPhilipp Reisner bool write_al_updates; 3329a51ab1cSPhilipp Reisner 3339a51ab1cSPhilipp Reisner rcu_read_lock(); 334*b30ab791SAndreas Gruenbacher write_al_updates = rcu_dereference(device->ldev->disk_conf)->al_updates; 3359a51ab1cSPhilipp Reisner rcu_read_unlock(); 3369a51ab1cSPhilipp Reisner 337b5bc8e08SLars Ellenberg if (write_al_updates) 338*b30ab791SAndreas Gruenbacher al_write_transaction(device, delegate); 339*b30ab791SAndreas Gruenbacher spin_lock_irq(&device->al_lock); 3407ad651b5SLars Ellenberg /* FIXME 3411b7ab15bSPhilipp Reisner if (err) 3427ad651b5SLars Ellenberg we need an "lc_cancel" here; 3437ad651b5SLars Ellenberg */ 344*b30ab791SAndreas Gruenbacher lc_committed(device->act_log); 345*b30ab791SAndreas Gruenbacher spin_unlock_irq(&device->al_lock); 3467ad651b5SLars Ellenberg } 347*b30ab791SAndreas Gruenbacher lc_unlock(device->act_log); 348*b30ab791SAndreas Gruenbacher wake_up(&device->al_wait); 349b411b363SPhilipp Reisner } 350b411b363SPhilipp Reisner } 351b411b363SPhilipp Reisner 352b5bc8e08SLars Ellenberg /* 353b5bc8e08SLars Ellenberg * @delegate: delegate activity log I/O to the worker thread 354b5bc8e08SLars Ellenberg */ 355*b30ab791SAndreas Gruenbacher void drbd_al_begin_io(struct drbd_device *device, struct drbd_interval *i, bool delegate) 356b5bc8e08SLars Ellenberg { 357*b30ab791SAndreas Gruenbacher BUG_ON(delegate && current == device->tconn->worker.task); 358b5bc8e08SLars Ellenberg 359*b30ab791SAndreas Gruenbacher if (drbd_al_begin_io_prepare(device, i)) 360*b30ab791SAndreas Gruenbacher drbd_al_begin_io_commit(device, delegate); 361b5bc8e08SLars Ellenberg } 362b5bc8e08SLars Ellenberg 363*b30ab791SAndreas Gruenbacher int drbd_al_begin_io_nonblock(struct drbd_device *device, struct drbd_interval *i) 36408a1ddabSLars Ellenberg { 365*b30ab791SAndreas Gruenbacher struct lru_cache *al = device->act_log; 36608a1ddabSLars Ellenberg /* for bios crossing activity log extent boundaries, 36708a1ddabSLars Ellenberg * we may need to activate two extents in one go */ 36808a1ddabSLars Ellenberg unsigned first = i->sector >> (AL_EXTENT_SHIFT-9); 36908a1ddabSLars Ellenberg unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9); 37008a1ddabSLars Ellenberg unsigned nr_al_extents; 37108a1ddabSLars Ellenberg unsigned available_update_slots; 37208a1ddabSLars Ellenberg unsigned enr; 37308a1ddabSLars Ellenberg 37408a1ddabSLars Ellenberg D_ASSERT(first <= last); 37508a1ddabSLars Ellenberg 37608a1ddabSLars Ellenberg nr_al_extents = 1 + last - first; /* worst case: all touched extends are cold. */ 37708a1ddabSLars Ellenberg available_update_slots = min(al->nr_elements - al->used, 37808a1ddabSLars Ellenberg al->max_pending_changes - al->pending_changes); 37908a1ddabSLars Ellenberg 38008a1ddabSLars Ellenberg /* We want all necessary updates for a given request within the same transaction 38108a1ddabSLars Ellenberg * We could first check how many updates are *actually* needed, 38208a1ddabSLars Ellenberg * and use that instead of the worst-case nr_al_extents */ 38308a1ddabSLars Ellenberg if (available_update_slots < nr_al_extents) 38408a1ddabSLars Ellenberg return -EWOULDBLOCK; 38508a1ddabSLars Ellenberg 38608a1ddabSLars Ellenberg /* Is resync active in this area? */ 38708a1ddabSLars Ellenberg for (enr = first; enr <= last; enr++) { 38808a1ddabSLars Ellenberg struct lc_element *tmp; 389*b30ab791SAndreas Gruenbacher tmp = lc_find(device->resync, enr/AL_EXT_PER_BM_SECT); 39008a1ddabSLars Ellenberg if (unlikely(tmp != NULL)) { 39108a1ddabSLars Ellenberg struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce); 39208a1ddabSLars Ellenberg if (test_bit(BME_NO_WRITES, &bm_ext->flags)) { 3930b6ef416SLars Ellenberg if (!test_and_set_bit(BME_PRIORITY, &bm_ext->flags)) 39408a1ddabSLars Ellenberg return -EBUSY; 39508a1ddabSLars Ellenberg return -EWOULDBLOCK; 39608a1ddabSLars Ellenberg } 39708a1ddabSLars Ellenberg } 39808a1ddabSLars Ellenberg } 39908a1ddabSLars Ellenberg 40008a1ddabSLars Ellenberg /* Checkout the refcounts. 40108a1ddabSLars Ellenberg * Given that we checked for available elements and update slots above, 40208a1ddabSLars Ellenberg * this has to be successful. */ 40308a1ddabSLars Ellenberg for (enr = first; enr <= last; enr++) { 40408a1ddabSLars Ellenberg struct lc_element *al_ext; 405*b30ab791SAndreas Gruenbacher al_ext = lc_get_cumulative(device->act_log, enr); 40608a1ddabSLars Ellenberg if (!al_ext) 40708a1ddabSLars Ellenberg dev_info(DEV, "LOGIC BUG for enr=%u\n", enr); 40808a1ddabSLars Ellenberg } 40908a1ddabSLars Ellenberg return 0; 41008a1ddabSLars Ellenberg } 41108a1ddabSLars Ellenberg 412*b30ab791SAndreas Gruenbacher void drbd_al_complete_io(struct drbd_device *device, struct drbd_interval *i) 413b411b363SPhilipp Reisner { 414e15766e9SLars Ellenberg /* for bios crossing activity log extent boundaries, 415e15766e9SLars Ellenberg * we may need to activate two extents in one go */ 416e15766e9SLars Ellenberg unsigned first = i->sector >> (AL_EXTENT_SHIFT-9); 41781a3537aSLars Ellenberg unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9); 418e15766e9SLars Ellenberg unsigned enr; 419b411b363SPhilipp Reisner struct lc_element *extent; 420b411b363SPhilipp Reisner unsigned long flags; 421b411b363SPhilipp Reisner 42281a3537aSLars Ellenberg D_ASSERT(first <= last); 423*b30ab791SAndreas Gruenbacher spin_lock_irqsave(&device->al_lock, flags); 424b411b363SPhilipp Reisner 425e15766e9SLars Ellenberg for (enr = first; enr <= last; enr++) { 426*b30ab791SAndreas Gruenbacher extent = lc_find(device->act_log, enr); 427b411b363SPhilipp Reisner if (!extent) { 428b411b363SPhilipp Reisner dev_err(DEV, "al_complete_io() called on inactive extent %u\n", enr); 429e15766e9SLars Ellenberg continue; 430b411b363SPhilipp Reisner } 431*b30ab791SAndreas Gruenbacher lc_put(device->act_log, extent); 432e15766e9SLars Ellenberg } 433*b30ab791SAndreas Gruenbacher spin_unlock_irqrestore(&device->al_lock, flags); 434*b30ab791SAndreas Gruenbacher wake_up(&device->al_wait); 435b411b363SPhilipp Reisner } 436b411b363SPhilipp Reisner 43719f843aaSLars Ellenberg #if (PAGE_SHIFT + 3) < (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT) 43819f843aaSLars Ellenberg /* Currently BM_BLOCK_SHIFT, BM_EXT_SHIFT and AL_EXTENT_SHIFT 43919f843aaSLars Ellenberg * are still coupled, or assume too much about their relation. 44019f843aaSLars Ellenberg * Code below will not work if this is violated. 44119f843aaSLars Ellenberg * Will be cleaned up with some followup patch. 44219f843aaSLars Ellenberg */ 44319f843aaSLars Ellenberg # error FIXME 44419f843aaSLars Ellenberg #endif 44519f843aaSLars Ellenberg 44619f843aaSLars Ellenberg static unsigned int al_extent_to_bm_page(unsigned int al_enr) 44719f843aaSLars Ellenberg { 44819f843aaSLars Ellenberg return al_enr >> 44919f843aaSLars Ellenberg /* bit to page */ 45019f843aaSLars Ellenberg ((PAGE_SHIFT + 3) - 45119f843aaSLars Ellenberg /* al extent number to bit */ 45219f843aaSLars Ellenberg (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT)); 45319f843aaSLars Ellenberg } 45419f843aaSLars Ellenberg 45519f843aaSLars Ellenberg static unsigned int rs_extent_to_bm_page(unsigned int rs_enr) 45619f843aaSLars Ellenberg { 45719f843aaSLars Ellenberg return rs_enr >> 45819f843aaSLars Ellenberg /* bit to page */ 45919f843aaSLars Ellenberg ((PAGE_SHIFT + 3) - 460acb104c3SLars Ellenberg /* resync extent number to bit */ 46119f843aaSLars Ellenberg (BM_EXT_SHIFT - BM_BLOCK_SHIFT)); 46219f843aaSLars Ellenberg } 46319f843aaSLars Ellenberg 464*b30ab791SAndreas Gruenbacher static sector_t al_tr_number_to_on_disk_sector(struct drbd_device *device) 465ae8bf312SLars Ellenberg { 466*b30ab791SAndreas Gruenbacher const unsigned int stripes = device->ldev->md.al_stripes; 467*b30ab791SAndreas Gruenbacher const unsigned int stripe_size_4kB = device->ldev->md.al_stripe_size_4k; 468ae8bf312SLars Ellenberg 469ae8bf312SLars Ellenberg /* transaction number, modulo on-disk ring buffer wrap around */ 470*b30ab791SAndreas Gruenbacher unsigned int t = device->al_tr_number % (device->ldev->md.al_size_4k); 471ae8bf312SLars Ellenberg 472ae8bf312SLars Ellenberg /* ... to aligned 4k on disk block */ 473ae8bf312SLars Ellenberg t = ((t % stripes) * stripe_size_4kB) + t/stripes; 474ae8bf312SLars Ellenberg 475ae8bf312SLars Ellenberg /* ... to 512 byte sector in activity log */ 476ae8bf312SLars Ellenberg t *= 8; 477ae8bf312SLars Ellenberg 478ae8bf312SLars Ellenberg /* ... plus offset to the on disk position */ 479*b30ab791SAndreas Gruenbacher return device->ldev->md.md_offset + device->ldev->md.al_offset + t; 480ae8bf312SLars Ellenberg } 481ae8bf312SLars Ellenberg 48299920dc5SAndreas Gruenbacher static int 483*b30ab791SAndreas Gruenbacher _al_write_transaction(struct drbd_device *device) 484b411b363SPhilipp Reisner { 4857ad651b5SLars Ellenberg struct al_transaction_on_disk *buffer; 4867ad651b5SLars Ellenberg struct lc_element *e; 487b411b363SPhilipp Reisner sector_t sector; 4887ad651b5SLars Ellenberg int i, mx; 4897ad651b5SLars Ellenberg unsigned extent_nr; 4907ad651b5SLars Ellenberg unsigned crc = 0; 4911b7ab15bSPhilipp Reisner int err = 0; 492b411b363SPhilipp Reisner 493*b30ab791SAndreas Gruenbacher if (!get_ldev(device)) { 4947ad651b5SLars Ellenberg dev_err(DEV, "disk is %s, cannot start al transaction\n", 495*b30ab791SAndreas Gruenbacher drbd_disk_str(device->state.disk)); 4961b7ab15bSPhilipp Reisner return -EIO; 497b411b363SPhilipp Reisner } 498b411b363SPhilipp Reisner 4996719fb03SLars Ellenberg /* The bitmap write may have failed, causing a state change. */ 500*b30ab791SAndreas Gruenbacher if (device->state.disk < D_INCONSISTENT) { 5016719fb03SLars Ellenberg dev_err(DEV, 5027ad651b5SLars Ellenberg "disk is %s, cannot write al transaction\n", 503*b30ab791SAndreas Gruenbacher drbd_disk_str(device->state.disk)); 504*b30ab791SAndreas Gruenbacher put_ldev(device); 5051b7ab15bSPhilipp Reisner return -EIO; 5066719fb03SLars Ellenberg } 5076719fb03SLars Ellenberg 508*b30ab791SAndreas Gruenbacher buffer = drbd_md_get_buffer(device); /* protects md_io_buffer, al_tr_cycle, ... */ 509cdfda633SPhilipp Reisner if (!buffer) { 510cdfda633SPhilipp Reisner dev_err(DEV, "disk failed while waiting for md_io buffer\n"); 511*b30ab791SAndreas Gruenbacher put_ldev(device); 5121b7ab15bSPhilipp Reisner return -ENODEV; 513cdfda633SPhilipp Reisner } 514b411b363SPhilipp Reisner 5157ad651b5SLars Ellenberg memset(buffer, 0, sizeof(*buffer)); 5167ad651b5SLars Ellenberg buffer->magic = cpu_to_be32(DRBD_AL_MAGIC); 517*b30ab791SAndreas Gruenbacher buffer->tr_number = cpu_to_be32(device->al_tr_number); 518b411b363SPhilipp Reisner 5197ad651b5SLars Ellenberg i = 0; 520b411b363SPhilipp Reisner 5217ad651b5SLars Ellenberg /* Even though no one can start to change this list 5227ad651b5SLars Ellenberg * once we set the LC_LOCKED -- from drbd_al_begin_io(), 5237ad651b5SLars Ellenberg * lc_try_lock_for_transaction() --, someone may still 5247ad651b5SLars Ellenberg * be in the process of changing it. */ 525*b30ab791SAndreas Gruenbacher spin_lock_irq(&device->al_lock); 526*b30ab791SAndreas Gruenbacher list_for_each_entry(e, &device->act_log->to_be_changed, list) { 5277ad651b5SLars Ellenberg if (i == AL_UPDATES_PER_TRANSACTION) { 5287ad651b5SLars Ellenberg i++; 5297ad651b5SLars Ellenberg break; 5307ad651b5SLars Ellenberg } 5317ad651b5SLars Ellenberg buffer->update_slot_nr[i] = cpu_to_be16(e->lc_index); 5327ad651b5SLars Ellenberg buffer->update_extent_nr[i] = cpu_to_be32(e->lc_new_number); 5337ad651b5SLars Ellenberg if (e->lc_number != LC_FREE) 534*b30ab791SAndreas Gruenbacher drbd_bm_mark_for_writeout(device, 5357ad651b5SLars Ellenberg al_extent_to_bm_page(e->lc_number)); 5367ad651b5SLars Ellenberg i++; 5377ad651b5SLars Ellenberg } 538*b30ab791SAndreas Gruenbacher spin_unlock_irq(&device->al_lock); 5397ad651b5SLars Ellenberg BUG_ON(i > AL_UPDATES_PER_TRANSACTION); 540b411b363SPhilipp Reisner 5417ad651b5SLars Ellenberg buffer->n_updates = cpu_to_be16(i); 5427ad651b5SLars Ellenberg for ( ; i < AL_UPDATES_PER_TRANSACTION; i++) { 5437ad651b5SLars Ellenberg buffer->update_slot_nr[i] = cpu_to_be16(-1); 5447ad651b5SLars Ellenberg buffer->update_extent_nr[i] = cpu_to_be32(LC_FREE); 5457ad651b5SLars Ellenberg } 546b411b363SPhilipp Reisner 547*b30ab791SAndreas Gruenbacher buffer->context_size = cpu_to_be16(device->act_log->nr_elements); 548*b30ab791SAndreas Gruenbacher buffer->context_start_slot_nr = cpu_to_be16(device->al_tr_cycle); 5497ad651b5SLars Ellenberg 5507ad651b5SLars Ellenberg mx = min_t(int, AL_CONTEXT_PER_TRANSACTION, 551*b30ab791SAndreas Gruenbacher device->act_log->nr_elements - device->al_tr_cycle); 552b411b363SPhilipp Reisner for (i = 0; i < mx; i++) { 553*b30ab791SAndreas Gruenbacher unsigned idx = device->al_tr_cycle + i; 554*b30ab791SAndreas Gruenbacher extent_nr = lc_element_by_index(device->act_log, idx)->lc_number; 5557ad651b5SLars Ellenberg buffer->context[i] = cpu_to_be32(extent_nr); 556b411b363SPhilipp Reisner } 5577ad651b5SLars Ellenberg for (; i < AL_CONTEXT_PER_TRANSACTION; i++) 5587ad651b5SLars Ellenberg buffer->context[i] = cpu_to_be32(LC_FREE); 5597ad651b5SLars Ellenberg 560*b30ab791SAndreas Gruenbacher device->al_tr_cycle += AL_CONTEXT_PER_TRANSACTION; 561*b30ab791SAndreas Gruenbacher if (device->al_tr_cycle >= device->act_log->nr_elements) 562*b30ab791SAndreas Gruenbacher device->al_tr_cycle = 0; 563b411b363SPhilipp Reisner 564*b30ab791SAndreas Gruenbacher sector = al_tr_number_to_on_disk_sector(device); 565b411b363SPhilipp Reisner 5667ad651b5SLars Ellenberg crc = crc32c(0, buffer, 4096); 5677ad651b5SLars Ellenberg buffer->crc32c = cpu_to_be32(crc); 5687ad651b5SLars Ellenberg 569*b30ab791SAndreas Gruenbacher if (drbd_bm_write_hinted(device)) 5701b7ab15bSPhilipp Reisner err = -EIO; 571b5bc8e08SLars Ellenberg else { 572b5bc8e08SLars Ellenberg bool write_al_updates; 573b5bc8e08SLars Ellenberg rcu_read_lock(); 574*b30ab791SAndreas Gruenbacher write_al_updates = rcu_dereference(device->ldev->disk_conf)->al_updates; 575b5bc8e08SLars Ellenberg rcu_read_unlock(); 576b5bc8e08SLars Ellenberg if (write_al_updates) { 577*b30ab791SAndreas Gruenbacher if (drbd_md_sync_page_io(device, device->ldev, sector, WRITE)) { 5781b7ab15bSPhilipp Reisner err = -EIO; 579*b30ab791SAndreas Gruenbacher drbd_chk_io_error(device, 1, DRBD_META_IO_ERROR); 5807ad651b5SLars Ellenberg } else { 581*b30ab791SAndreas Gruenbacher device->al_tr_number++; 582*b30ab791SAndreas Gruenbacher device->al_writ_cnt++; 583b5bc8e08SLars Ellenberg } 584b5bc8e08SLars Ellenberg } 5857ad651b5SLars Ellenberg } 586b411b363SPhilipp Reisner 587*b30ab791SAndreas Gruenbacher drbd_md_put_buffer(device); 588*b30ab791SAndreas Gruenbacher put_ldev(device); 589b411b363SPhilipp Reisner 5901b7ab15bSPhilipp Reisner return err; 5911b7ab15bSPhilipp Reisner } 5921b7ab15bSPhilipp Reisner 5931b7ab15bSPhilipp Reisner 5941b7ab15bSPhilipp Reisner static int w_al_write_transaction(struct drbd_work *w, int unused) 5951b7ab15bSPhilipp Reisner { 5961b7ab15bSPhilipp Reisner struct update_al_work *aw = container_of(w, struct update_al_work, w); 597*b30ab791SAndreas Gruenbacher struct drbd_device *device = w->device; 5981b7ab15bSPhilipp Reisner int err; 5991b7ab15bSPhilipp Reisner 600*b30ab791SAndreas Gruenbacher err = _al_write_transaction(device); 6011b7ab15bSPhilipp Reisner aw->err = err; 6021b7ab15bSPhilipp Reisner complete(&aw->event); 6031b7ab15bSPhilipp Reisner 6041b7ab15bSPhilipp Reisner return err != -EIO ? err : 0; 6051b7ab15bSPhilipp Reisner } 6061b7ab15bSPhilipp Reisner 6071b7ab15bSPhilipp Reisner /* Calls from worker context (see w_restart_disk_io()) need to write the 6081b7ab15bSPhilipp Reisner transaction directly. Others came through generic_make_request(), 6091b7ab15bSPhilipp Reisner those need to delegate it to the worker. */ 610*b30ab791SAndreas Gruenbacher static int al_write_transaction(struct drbd_device *device, bool delegate) 6111b7ab15bSPhilipp Reisner { 61256392d2fSLars Ellenberg if (delegate) { 6131b7ab15bSPhilipp Reisner struct update_al_work al_work; 6141b7ab15bSPhilipp Reisner init_completion(&al_work.event); 6151b7ab15bSPhilipp Reisner al_work.w.cb = w_al_write_transaction; 616*b30ab791SAndreas Gruenbacher al_work.w.device = device; 617*b30ab791SAndreas Gruenbacher drbd_queue_work_front(&device->tconn->sender_work, &al_work.w); 6181b7ab15bSPhilipp Reisner wait_for_completion(&al_work.event); 6191b7ab15bSPhilipp Reisner return al_work.err; 62056392d2fSLars Ellenberg } else 621*b30ab791SAndreas Gruenbacher return _al_write_transaction(device); 622b411b363SPhilipp Reisner } 623b411b363SPhilipp Reisner 624*b30ab791SAndreas Gruenbacher static int _try_lc_del(struct drbd_device *device, struct lc_element *al_ext) 625b411b363SPhilipp Reisner { 626b411b363SPhilipp Reisner int rv; 627b411b363SPhilipp Reisner 628*b30ab791SAndreas Gruenbacher spin_lock_irq(&device->al_lock); 629b411b363SPhilipp Reisner rv = (al_ext->refcnt == 0); 630b411b363SPhilipp Reisner if (likely(rv)) 631*b30ab791SAndreas Gruenbacher lc_del(device->act_log, al_ext); 632*b30ab791SAndreas Gruenbacher spin_unlock_irq(&device->al_lock); 633b411b363SPhilipp Reisner 634b411b363SPhilipp Reisner return rv; 635b411b363SPhilipp Reisner } 636b411b363SPhilipp Reisner 637b411b363SPhilipp Reisner /** 638b411b363SPhilipp Reisner * drbd_al_shrink() - Removes all active extents form the activity log 639*b30ab791SAndreas Gruenbacher * @device: DRBD device. 640b411b363SPhilipp Reisner * 641b411b363SPhilipp Reisner * Removes all active extents form the activity log, waiting until 642b411b363SPhilipp Reisner * the reference count of each entry dropped to 0 first, of course. 643b411b363SPhilipp Reisner * 644*b30ab791SAndreas Gruenbacher * You need to lock device->act_log with lc_try_lock() / lc_unlock() 645b411b363SPhilipp Reisner */ 646*b30ab791SAndreas Gruenbacher void drbd_al_shrink(struct drbd_device *device) 647b411b363SPhilipp Reisner { 648b411b363SPhilipp Reisner struct lc_element *al_ext; 649b411b363SPhilipp Reisner int i; 650b411b363SPhilipp Reisner 651*b30ab791SAndreas Gruenbacher D_ASSERT(test_bit(__LC_LOCKED, &device->act_log->flags)); 652b411b363SPhilipp Reisner 653*b30ab791SAndreas Gruenbacher for (i = 0; i < device->act_log->nr_elements; i++) { 654*b30ab791SAndreas Gruenbacher al_ext = lc_element_by_index(device->act_log, i); 655b411b363SPhilipp Reisner if (al_ext->lc_number == LC_FREE) 656b411b363SPhilipp Reisner continue; 657*b30ab791SAndreas Gruenbacher wait_event(device->al_wait, _try_lc_del(device, al_ext)); 658b411b363SPhilipp Reisner } 659b411b363SPhilipp Reisner 660*b30ab791SAndreas Gruenbacher wake_up(&device->al_wait); 661b411b363SPhilipp Reisner } 662b411b363SPhilipp Reisner 663*b30ab791SAndreas Gruenbacher int drbd_initialize_al(struct drbd_device *device, void *buffer) 664d752b269SPhilipp Reisner { 665d752b269SPhilipp Reisner struct al_transaction_on_disk *al = buffer; 666*b30ab791SAndreas Gruenbacher struct drbd_md *md = &device->ldev->md; 667d752b269SPhilipp Reisner sector_t al_base = md->md_offset + md->al_offset; 668d752b269SPhilipp Reisner int al_size_4k = md->al_stripes * md->al_stripe_size_4k; 669d752b269SPhilipp Reisner int i; 670d752b269SPhilipp Reisner 671d752b269SPhilipp Reisner memset(al, 0, 4096); 672d752b269SPhilipp Reisner al->magic = cpu_to_be32(DRBD_AL_MAGIC); 673d752b269SPhilipp Reisner al->transaction_type = cpu_to_be16(AL_TR_INITIALIZED); 674d752b269SPhilipp Reisner al->crc32c = cpu_to_be32(crc32c(0, al, 4096)); 675d752b269SPhilipp Reisner 676d752b269SPhilipp Reisner for (i = 0; i < al_size_4k; i++) { 677*b30ab791SAndreas Gruenbacher int err = drbd_md_sync_page_io(device, device->ldev, al_base + i * 8, WRITE); 678d752b269SPhilipp Reisner if (err) 679d752b269SPhilipp Reisner return err; 680d752b269SPhilipp Reisner } 681d752b269SPhilipp Reisner return 0; 682d752b269SPhilipp Reisner } 683d752b269SPhilipp Reisner 68499920dc5SAndreas Gruenbacher static int w_update_odbm(struct drbd_work *w, int unused) 685b411b363SPhilipp Reisner { 686b411b363SPhilipp Reisner struct update_odbm_work *udw = container_of(w, struct update_odbm_work, w); 687*b30ab791SAndreas Gruenbacher struct drbd_device *device = w->device; 6883b98c0c2SLars Ellenberg struct sib_info sib = { .sib_reason = SIB_SYNC_PROGRESS, }; 689b411b363SPhilipp Reisner 690*b30ab791SAndreas Gruenbacher if (!get_ldev(device)) { 691b411b363SPhilipp Reisner if (__ratelimit(&drbd_ratelimit_state)) 692b411b363SPhilipp Reisner dev_warn(DEV, "Can not update on disk bitmap, local IO disabled.\n"); 693b411b363SPhilipp Reisner kfree(udw); 69499920dc5SAndreas Gruenbacher return 0; 695b411b363SPhilipp Reisner } 696b411b363SPhilipp Reisner 697*b30ab791SAndreas Gruenbacher drbd_bm_write_page(device, rs_extent_to_bm_page(udw->enr)); 698*b30ab791SAndreas Gruenbacher put_ldev(device); 699b411b363SPhilipp Reisner 700b411b363SPhilipp Reisner kfree(udw); 701b411b363SPhilipp Reisner 702*b30ab791SAndreas Gruenbacher if (drbd_bm_total_weight(device) <= device->rs_failed) { 703*b30ab791SAndreas Gruenbacher switch (device->state.conn) { 704b411b363SPhilipp Reisner case C_SYNC_SOURCE: case C_SYNC_TARGET: 705b411b363SPhilipp Reisner case C_PAUSED_SYNC_S: case C_PAUSED_SYNC_T: 706*b30ab791SAndreas Gruenbacher drbd_resync_finished(device); 707b411b363SPhilipp Reisner default: 708b411b363SPhilipp Reisner /* nothing to do */ 709b411b363SPhilipp Reisner break; 710b411b363SPhilipp Reisner } 711b411b363SPhilipp Reisner } 712*b30ab791SAndreas Gruenbacher drbd_bcast_event(device, &sib); 713b411b363SPhilipp Reisner 71499920dc5SAndreas Gruenbacher return 0; 715b411b363SPhilipp Reisner } 716b411b363SPhilipp Reisner 717b411b363SPhilipp Reisner 718b411b363SPhilipp Reisner /* ATTENTION. The AL's extents are 4MB each, while the extents in the 719b411b363SPhilipp Reisner * resync LRU-cache are 16MB each. 720b411b363SPhilipp Reisner * The caller of this function has to hold an get_ldev() reference. 721b411b363SPhilipp Reisner * 722b411b363SPhilipp Reisner * TODO will be obsoleted once we have a caching lru of the on disk bitmap 723b411b363SPhilipp Reisner */ 724*b30ab791SAndreas Gruenbacher static void drbd_try_clear_on_disk_bm(struct drbd_device *device, sector_t sector, 725b411b363SPhilipp Reisner int count, int success) 726b411b363SPhilipp Reisner { 727b411b363SPhilipp Reisner struct lc_element *e; 728b411b363SPhilipp Reisner struct update_odbm_work *udw; 729b411b363SPhilipp Reisner 730b411b363SPhilipp Reisner unsigned int enr; 731b411b363SPhilipp Reisner 732*b30ab791SAndreas Gruenbacher D_ASSERT(atomic_read(&device->local_cnt)); 733b411b363SPhilipp Reisner 734b411b363SPhilipp Reisner /* I simply assume that a sector/size pair never crosses 735b411b363SPhilipp Reisner * a 16 MB extent border. (Currently this is true...) */ 736b411b363SPhilipp Reisner enr = BM_SECT_TO_EXT(sector); 737b411b363SPhilipp Reisner 738*b30ab791SAndreas Gruenbacher e = lc_get(device->resync, enr); 739b411b363SPhilipp Reisner if (e) { 740b411b363SPhilipp Reisner struct bm_extent *ext = lc_entry(e, struct bm_extent, lce); 741b411b363SPhilipp Reisner if (ext->lce.lc_number == enr) { 742b411b363SPhilipp Reisner if (success) 743b411b363SPhilipp Reisner ext->rs_left -= count; 744b411b363SPhilipp Reisner else 745b411b363SPhilipp Reisner ext->rs_failed += count; 746b411b363SPhilipp Reisner if (ext->rs_left < ext->rs_failed) { 747975b2979SPhilipp Reisner dev_warn(DEV, "BAD! sector=%llus enr=%u rs_left=%d " 748975b2979SPhilipp Reisner "rs_failed=%d count=%d cstate=%s\n", 749b411b363SPhilipp Reisner (unsigned long long)sector, 750b411b363SPhilipp Reisner ext->lce.lc_number, ext->rs_left, 751975b2979SPhilipp Reisner ext->rs_failed, count, 752*b30ab791SAndreas Gruenbacher drbd_conn_str(device->state.conn)); 753b411b363SPhilipp Reisner 754975b2979SPhilipp Reisner /* We don't expect to be able to clear more bits 755975b2979SPhilipp Reisner * than have been set when we originally counted 756975b2979SPhilipp Reisner * the set bits to cache that value in ext->rs_left. 757975b2979SPhilipp Reisner * Whatever the reason (disconnect during resync, 758975b2979SPhilipp Reisner * delayed local completion of an application write), 759975b2979SPhilipp Reisner * try to fix it up by recounting here. */ 760*b30ab791SAndreas Gruenbacher ext->rs_left = drbd_bm_e_weight(device, enr); 761b411b363SPhilipp Reisner } 762b411b363SPhilipp Reisner } else { 763b411b363SPhilipp Reisner /* Normally this element should be in the cache, 764b411b363SPhilipp Reisner * since drbd_rs_begin_io() pulled it already in. 765b411b363SPhilipp Reisner * 766b411b363SPhilipp Reisner * But maybe an application write finished, and we set 767b411b363SPhilipp Reisner * something outside the resync lru_cache in sync. 768b411b363SPhilipp Reisner */ 769*b30ab791SAndreas Gruenbacher int rs_left = drbd_bm_e_weight(device, enr); 770b411b363SPhilipp Reisner if (ext->flags != 0) { 771b411b363SPhilipp Reisner dev_warn(DEV, "changing resync lce: %d[%u;%02lx]" 772b411b363SPhilipp Reisner " -> %d[%u;00]\n", 773b411b363SPhilipp Reisner ext->lce.lc_number, ext->rs_left, 774b411b363SPhilipp Reisner ext->flags, enr, rs_left); 775b411b363SPhilipp Reisner ext->flags = 0; 776b411b363SPhilipp Reisner } 777b411b363SPhilipp Reisner if (ext->rs_failed) { 778b411b363SPhilipp Reisner dev_warn(DEV, "Kicking resync_lru element enr=%u " 779b411b363SPhilipp Reisner "out with rs_failed=%d\n", 780b411b363SPhilipp Reisner ext->lce.lc_number, ext->rs_failed); 781b411b363SPhilipp Reisner } 782b411b363SPhilipp Reisner ext->rs_left = rs_left; 783b411b363SPhilipp Reisner ext->rs_failed = success ? 0 : count; 78446a15bc3SLars Ellenberg /* we don't keep a persistent log of the resync lru, 78546a15bc3SLars Ellenberg * we can commit any change right away. */ 786*b30ab791SAndreas Gruenbacher lc_committed(device->resync); 787b411b363SPhilipp Reisner } 788*b30ab791SAndreas Gruenbacher lc_put(device->resync, &ext->lce); 789b411b363SPhilipp Reisner /* no race, we are within the al_lock! */ 790b411b363SPhilipp Reisner 791b411b363SPhilipp Reisner if (ext->rs_left == ext->rs_failed) { 792b411b363SPhilipp Reisner ext->rs_failed = 0; 793b411b363SPhilipp Reisner 794b411b363SPhilipp Reisner udw = kmalloc(sizeof(*udw), GFP_ATOMIC); 795b411b363SPhilipp Reisner if (udw) { 796b411b363SPhilipp Reisner udw->enr = ext->lce.lc_number; 797b411b363SPhilipp Reisner udw->w.cb = w_update_odbm; 798*b30ab791SAndreas Gruenbacher udw->w.device = device; 799*b30ab791SAndreas Gruenbacher drbd_queue_work_front(&device->tconn->sender_work, &udw->w); 800b411b363SPhilipp Reisner } else { 801b411b363SPhilipp Reisner dev_warn(DEV, "Could not kmalloc an udw\n"); 802b411b363SPhilipp Reisner } 803b411b363SPhilipp Reisner } 804b411b363SPhilipp Reisner } else { 805b411b363SPhilipp Reisner dev_err(DEV, "lc_get() failed! locked=%d/%d flags=%lu\n", 806*b30ab791SAndreas Gruenbacher device->resync_locked, 807*b30ab791SAndreas Gruenbacher device->resync->nr_elements, 808*b30ab791SAndreas Gruenbacher device->resync->flags); 809b411b363SPhilipp Reisner } 810b411b363SPhilipp Reisner } 811b411b363SPhilipp Reisner 812*b30ab791SAndreas Gruenbacher void drbd_advance_rs_marks(struct drbd_device *device, unsigned long still_to_go) 813c6ea14dfSLars Ellenberg { 814c6ea14dfSLars Ellenberg unsigned long now = jiffies; 815*b30ab791SAndreas Gruenbacher unsigned long last = device->rs_mark_time[device->rs_last_mark]; 816*b30ab791SAndreas Gruenbacher int next = (device->rs_last_mark + 1) % DRBD_SYNC_MARKS; 817c6ea14dfSLars Ellenberg if (time_after_eq(now, last + DRBD_SYNC_MARK_STEP)) { 818*b30ab791SAndreas Gruenbacher if (device->rs_mark_left[device->rs_last_mark] != still_to_go && 819*b30ab791SAndreas Gruenbacher device->state.conn != C_PAUSED_SYNC_T && 820*b30ab791SAndreas Gruenbacher device->state.conn != C_PAUSED_SYNC_S) { 821*b30ab791SAndreas Gruenbacher device->rs_mark_time[next] = now; 822*b30ab791SAndreas Gruenbacher device->rs_mark_left[next] = still_to_go; 823*b30ab791SAndreas Gruenbacher device->rs_last_mark = next; 824c6ea14dfSLars Ellenberg } 825c6ea14dfSLars Ellenberg } 826c6ea14dfSLars Ellenberg } 827c6ea14dfSLars Ellenberg 828b411b363SPhilipp Reisner /* clear the bit corresponding to the piece of storage in question: 829b411b363SPhilipp Reisner * size byte of data starting from sector. Only clear a bits of the affected 830b411b363SPhilipp Reisner * one ore more _aligned_ BM_BLOCK_SIZE blocks. 831b411b363SPhilipp Reisner * 832b411b363SPhilipp Reisner * called by worker on C_SYNC_TARGET and receiver on SyncSource. 833b411b363SPhilipp Reisner * 834b411b363SPhilipp Reisner */ 835*b30ab791SAndreas Gruenbacher void __drbd_set_in_sync(struct drbd_device *device, sector_t sector, int size, 836b411b363SPhilipp Reisner const char *file, const unsigned int line) 837b411b363SPhilipp Reisner { 838b411b363SPhilipp Reisner /* Is called from worker and receiver context _only_ */ 839b411b363SPhilipp Reisner unsigned long sbnr, ebnr, lbnr; 840b411b363SPhilipp Reisner unsigned long count = 0; 841b411b363SPhilipp Reisner sector_t esector, nr_sectors; 842b411b363SPhilipp Reisner int wake_up = 0; 843b411b363SPhilipp Reisner unsigned long flags; 844b411b363SPhilipp Reisner 845c670a398SAndreas Gruenbacher if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) { 846b411b363SPhilipp Reisner dev_err(DEV, "drbd_set_in_sync: sector=%llus size=%d nonsense!\n", 847b411b363SPhilipp Reisner (unsigned long long)sector, size); 848b411b363SPhilipp Reisner return; 849b411b363SPhilipp Reisner } 850518a4d53SPhilipp Reisner 851*b30ab791SAndreas Gruenbacher if (!get_ldev(device)) 852518a4d53SPhilipp Reisner return; /* no disk, no metadata, no bitmap to clear bits in */ 853518a4d53SPhilipp Reisner 854*b30ab791SAndreas Gruenbacher nr_sectors = drbd_get_capacity(device->this_bdev); 855b411b363SPhilipp Reisner esector = sector + (size >> 9) - 1; 856b411b363SPhilipp Reisner 857841ce241SAndreas Gruenbacher if (!expect(sector < nr_sectors)) 858518a4d53SPhilipp Reisner goto out; 859841ce241SAndreas Gruenbacher if (!expect(esector < nr_sectors)) 860841ce241SAndreas Gruenbacher esector = nr_sectors - 1; 861b411b363SPhilipp Reisner 862b411b363SPhilipp Reisner lbnr = BM_SECT_TO_BIT(nr_sectors-1); 863b411b363SPhilipp Reisner 864b411b363SPhilipp Reisner /* we clear it (in sync). 865b411b363SPhilipp Reisner * round up start sector, round down end sector. we make sure we only 866b411b363SPhilipp Reisner * clear full, aligned, BM_BLOCK_SIZE (4K) blocks */ 867b411b363SPhilipp Reisner if (unlikely(esector < BM_SECT_PER_BIT-1)) 868518a4d53SPhilipp Reisner goto out; 869b411b363SPhilipp Reisner if (unlikely(esector == (nr_sectors-1))) 870b411b363SPhilipp Reisner ebnr = lbnr; 871b411b363SPhilipp Reisner else 872b411b363SPhilipp Reisner ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1)); 873b411b363SPhilipp Reisner sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1); 874b411b363SPhilipp Reisner 875b411b363SPhilipp Reisner if (sbnr > ebnr) 876518a4d53SPhilipp Reisner goto out; 877b411b363SPhilipp Reisner 878b411b363SPhilipp Reisner /* 879b411b363SPhilipp Reisner * ok, (capacity & 7) != 0 sometimes, but who cares... 880b411b363SPhilipp Reisner * we count rs_{total,left} in bits, not sectors. 881b411b363SPhilipp Reisner */ 882*b30ab791SAndreas Gruenbacher count = drbd_bm_clear_bits(device, sbnr, ebnr); 883518a4d53SPhilipp Reisner if (count) { 884*b30ab791SAndreas Gruenbacher drbd_advance_rs_marks(device, drbd_bm_total_weight(device)); 885*b30ab791SAndreas Gruenbacher spin_lock_irqsave(&device->al_lock, flags); 886*b30ab791SAndreas Gruenbacher drbd_try_clear_on_disk_bm(device, sector, count, true); 887*b30ab791SAndreas Gruenbacher spin_unlock_irqrestore(&device->al_lock, flags); 8881d7734a0SLars Ellenberg 889b411b363SPhilipp Reisner /* just wake_up unconditional now, various lc_chaged(), 890b411b363SPhilipp Reisner * lc_put() in drbd_try_clear_on_disk_bm(). */ 891b411b363SPhilipp Reisner wake_up = 1; 892b411b363SPhilipp Reisner } 893518a4d53SPhilipp Reisner out: 894*b30ab791SAndreas Gruenbacher put_ldev(device); 895b411b363SPhilipp Reisner if (wake_up) 896*b30ab791SAndreas Gruenbacher wake_up(&device->al_wait); 897b411b363SPhilipp Reisner } 898b411b363SPhilipp Reisner 899b411b363SPhilipp Reisner /* 900b411b363SPhilipp Reisner * this is intended to set one request worth of data out of sync. 901b411b363SPhilipp Reisner * affects at least 1 bit, 9021816a2b4SLars Ellenberg * and at most 1+DRBD_MAX_BIO_SIZE/BM_BLOCK_SIZE bits. 903b411b363SPhilipp Reisner * 904b411b363SPhilipp Reisner * called by tl_clear and drbd_send_dblock (==drbd_make_request). 905b411b363SPhilipp Reisner * so this can be _any_ process. 906b411b363SPhilipp Reisner */ 907*b30ab791SAndreas Gruenbacher int __drbd_set_out_of_sync(struct drbd_device *device, sector_t sector, int size, 908b411b363SPhilipp Reisner const char *file, const unsigned int line) 909b411b363SPhilipp Reisner { 910376694a0SPhilipp Reisner unsigned long sbnr, ebnr, flags; 911b411b363SPhilipp Reisner sector_t esector, nr_sectors; 91273a01a18SPhilipp Reisner unsigned int enr, count = 0; 913b411b363SPhilipp Reisner struct lc_element *e; 914b411b363SPhilipp Reisner 91581a3537aSLars Ellenberg /* this should be an empty REQ_FLUSH */ 91681a3537aSLars Ellenberg if (size == 0) 91781a3537aSLars Ellenberg return 0; 91881a3537aSLars Ellenberg 91981a3537aSLars Ellenberg if (size < 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) { 920b411b363SPhilipp Reisner dev_err(DEV, "sector: %llus, size: %d\n", 921b411b363SPhilipp Reisner (unsigned long long)sector, size); 92273a01a18SPhilipp Reisner return 0; 923b411b363SPhilipp Reisner } 924b411b363SPhilipp Reisner 925*b30ab791SAndreas Gruenbacher if (!get_ldev(device)) 92673a01a18SPhilipp Reisner return 0; /* no disk, no metadata, no bitmap to set bits in */ 927b411b363SPhilipp Reisner 928*b30ab791SAndreas Gruenbacher nr_sectors = drbd_get_capacity(device->this_bdev); 929b411b363SPhilipp Reisner esector = sector + (size >> 9) - 1; 930b411b363SPhilipp Reisner 931841ce241SAndreas Gruenbacher if (!expect(sector < nr_sectors)) 932b411b363SPhilipp Reisner goto out; 933841ce241SAndreas Gruenbacher if (!expect(esector < nr_sectors)) 934841ce241SAndreas Gruenbacher esector = nr_sectors - 1; 935b411b363SPhilipp Reisner 936b411b363SPhilipp Reisner /* we set it out of sync, 937b411b363SPhilipp Reisner * we do not need to round anything here */ 938b411b363SPhilipp Reisner sbnr = BM_SECT_TO_BIT(sector); 939b411b363SPhilipp Reisner ebnr = BM_SECT_TO_BIT(esector); 940b411b363SPhilipp Reisner 941b411b363SPhilipp Reisner /* ok, (capacity & 7) != 0 sometimes, but who cares... 942b411b363SPhilipp Reisner * we count rs_{total,left} in bits, not sectors. */ 943*b30ab791SAndreas Gruenbacher spin_lock_irqsave(&device->al_lock, flags); 944*b30ab791SAndreas Gruenbacher count = drbd_bm_set_bits(device, sbnr, ebnr); 945b411b363SPhilipp Reisner 946b411b363SPhilipp Reisner enr = BM_SECT_TO_EXT(sector); 947*b30ab791SAndreas Gruenbacher e = lc_find(device->resync, enr); 948b411b363SPhilipp Reisner if (e) 949b411b363SPhilipp Reisner lc_entry(e, struct bm_extent, lce)->rs_left += count; 950*b30ab791SAndreas Gruenbacher spin_unlock_irqrestore(&device->al_lock, flags); 951b411b363SPhilipp Reisner 952b411b363SPhilipp Reisner out: 953*b30ab791SAndreas Gruenbacher put_ldev(device); 95473a01a18SPhilipp Reisner 95573a01a18SPhilipp Reisner return count; 956b411b363SPhilipp Reisner } 957b411b363SPhilipp Reisner 958b411b363SPhilipp Reisner static 959*b30ab791SAndreas Gruenbacher struct bm_extent *_bme_get(struct drbd_device *device, unsigned int enr) 960b411b363SPhilipp Reisner { 961b411b363SPhilipp Reisner struct lc_element *e; 962b411b363SPhilipp Reisner struct bm_extent *bm_ext; 963b411b363SPhilipp Reisner int wakeup = 0; 964b411b363SPhilipp Reisner unsigned long rs_flags; 965b411b363SPhilipp Reisner 966*b30ab791SAndreas Gruenbacher spin_lock_irq(&device->al_lock); 967*b30ab791SAndreas Gruenbacher if (device->resync_locked > device->resync->nr_elements/2) { 968*b30ab791SAndreas Gruenbacher spin_unlock_irq(&device->al_lock); 969b411b363SPhilipp Reisner return NULL; 970b411b363SPhilipp Reisner } 971*b30ab791SAndreas Gruenbacher e = lc_get(device->resync, enr); 972b411b363SPhilipp Reisner bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; 973b411b363SPhilipp Reisner if (bm_ext) { 974b411b363SPhilipp Reisner if (bm_ext->lce.lc_number != enr) { 975*b30ab791SAndreas Gruenbacher bm_ext->rs_left = drbd_bm_e_weight(device, enr); 976b411b363SPhilipp Reisner bm_ext->rs_failed = 0; 977*b30ab791SAndreas Gruenbacher lc_committed(device->resync); 978b411b363SPhilipp Reisner wakeup = 1; 979b411b363SPhilipp Reisner } 980b411b363SPhilipp Reisner if (bm_ext->lce.refcnt == 1) 981*b30ab791SAndreas Gruenbacher device->resync_locked++; 982b411b363SPhilipp Reisner set_bit(BME_NO_WRITES, &bm_ext->flags); 983b411b363SPhilipp Reisner } 984*b30ab791SAndreas Gruenbacher rs_flags = device->resync->flags; 985*b30ab791SAndreas Gruenbacher spin_unlock_irq(&device->al_lock); 986b411b363SPhilipp Reisner if (wakeup) 987*b30ab791SAndreas Gruenbacher wake_up(&device->al_wait); 988b411b363SPhilipp Reisner 989b411b363SPhilipp Reisner if (!bm_ext) { 990b411b363SPhilipp Reisner if (rs_flags & LC_STARVING) 991b411b363SPhilipp Reisner dev_warn(DEV, "Have to wait for element" 992b411b363SPhilipp Reisner " (resync LRU too small?)\n"); 99346a15bc3SLars Ellenberg BUG_ON(rs_flags & LC_LOCKED); 994b411b363SPhilipp Reisner } 995b411b363SPhilipp Reisner 996b411b363SPhilipp Reisner return bm_ext; 997b411b363SPhilipp Reisner } 998b411b363SPhilipp Reisner 999*b30ab791SAndreas Gruenbacher static int _is_in_al(struct drbd_device *device, unsigned int enr) 1000b411b363SPhilipp Reisner { 100146a15bc3SLars Ellenberg int rv; 1002b411b363SPhilipp Reisner 1003*b30ab791SAndreas Gruenbacher spin_lock_irq(&device->al_lock); 1004*b30ab791SAndreas Gruenbacher rv = lc_is_used(device->act_log, enr); 1005*b30ab791SAndreas Gruenbacher spin_unlock_irq(&device->al_lock); 1006b411b363SPhilipp Reisner 1007b411b363SPhilipp Reisner return rv; 1008b411b363SPhilipp Reisner } 1009b411b363SPhilipp Reisner 1010b411b363SPhilipp Reisner /** 1011b411b363SPhilipp Reisner * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED 1012*b30ab791SAndreas Gruenbacher * @device: DRBD device. 1013b411b363SPhilipp Reisner * @sector: The sector number. 1014b411b363SPhilipp Reisner * 101580a40e43SLars Ellenberg * This functions sleeps on al_wait. Returns 0 on success, -EINTR if interrupted. 1016b411b363SPhilipp Reisner */ 1017*b30ab791SAndreas Gruenbacher int drbd_rs_begin_io(struct drbd_device *device, sector_t sector) 1018b411b363SPhilipp Reisner { 1019b411b363SPhilipp Reisner unsigned int enr = BM_SECT_TO_EXT(sector); 1020b411b363SPhilipp Reisner struct bm_extent *bm_ext; 1021b411b363SPhilipp Reisner int i, sig; 1022f91ab628SPhilipp Reisner int sa = 200; /* Step aside 200 times, then grab the extent and let app-IO wait. 1023f91ab628SPhilipp Reisner 200 times -> 20 seconds. */ 1024b411b363SPhilipp Reisner 1025f91ab628SPhilipp Reisner retry: 1026*b30ab791SAndreas Gruenbacher sig = wait_event_interruptible(device->al_wait, 1027*b30ab791SAndreas Gruenbacher (bm_ext = _bme_get(device, enr))); 1028b411b363SPhilipp Reisner if (sig) 102980a40e43SLars Ellenberg return -EINTR; 1030b411b363SPhilipp Reisner 1031b411b363SPhilipp Reisner if (test_bit(BME_LOCKED, &bm_ext->flags)) 103280a40e43SLars Ellenberg return 0; 1033b411b363SPhilipp Reisner 1034b411b363SPhilipp Reisner for (i = 0; i < AL_EXT_PER_BM_SECT; i++) { 1035*b30ab791SAndreas Gruenbacher sig = wait_event_interruptible(device->al_wait, 1036*b30ab791SAndreas Gruenbacher !_is_in_al(device, enr * AL_EXT_PER_BM_SECT + i) || 1037c507f46fSPhilipp Reisner test_bit(BME_PRIORITY, &bm_ext->flags)); 1038f91ab628SPhilipp Reisner 1039f91ab628SPhilipp Reisner if (sig || (test_bit(BME_PRIORITY, &bm_ext->flags) && sa)) { 1040*b30ab791SAndreas Gruenbacher spin_lock_irq(&device->al_lock); 1041*b30ab791SAndreas Gruenbacher if (lc_put(device->resync, &bm_ext->lce) == 0) { 1042f91ab628SPhilipp Reisner bm_ext->flags = 0; /* clears BME_NO_WRITES and eventually BME_PRIORITY */ 1043*b30ab791SAndreas Gruenbacher device->resync_locked--; 1044*b30ab791SAndreas Gruenbacher wake_up(&device->al_wait); 1045b411b363SPhilipp Reisner } 1046*b30ab791SAndreas Gruenbacher spin_unlock_irq(&device->al_lock); 1047f91ab628SPhilipp Reisner if (sig) 104880a40e43SLars Ellenberg return -EINTR; 1049f91ab628SPhilipp Reisner if (schedule_timeout_interruptible(HZ/10)) 1050f91ab628SPhilipp Reisner return -EINTR; 1051c507f46fSPhilipp Reisner if (sa && --sa == 0) 1052c507f46fSPhilipp Reisner dev_warn(DEV,"drbd_rs_begin_io() stepped aside for 20sec." 1053c507f46fSPhilipp Reisner "Resync stalled?\n"); 1054f91ab628SPhilipp Reisner goto retry; 1055b411b363SPhilipp Reisner } 1056b411b363SPhilipp Reisner } 1057b411b363SPhilipp Reisner set_bit(BME_LOCKED, &bm_ext->flags); 105880a40e43SLars Ellenberg return 0; 1059b411b363SPhilipp Reisner } 1060b411b363SPhilipp Reisner 1061b411b363SPhilipp Reisner /** 1062b411b363SPhilipp Reisner * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep 1063*b30ab791SAndreas Gruenbacher * @device: DRBD device. 1064b411b363SPhilipp Reisner * @sector: The sector number. 1065b411b363SPhilipp Reisner * 1066b411b363SPhilipp Reisner * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then 1067b411b363SPhilipp Reisner * tries to set it to BME_LOCKED. Returns 0 upon success, and -EAGAIN 1068b411b363SPhilipp Reisner * if there is still application IO going on in this area. 1069b411b363SPhilipp Reisner */ 1070*b30ab791SAndreas Gruenbacher int drbd_try_rs_begin_io(struct drbd_device *device, sector_t sector) 1071b411b363SPhilipp Reisner { 1072b411b363SPhilipp Reisner unsigned int enr = BM_SECT_TO_EXT(sector); 1073b411b363SPhilipp Reisner const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT; 1074b411b363SPhilipp Reisner struct lc_element *e; 1075b411b363SPhilipp Reisner struct bm_extent *bm_ext; 1076b411b363SPhilipp Reisner int i; 1077b411b363SPhilipp Reisner 1078*b30ab791SAndreas Gruenbacher spin_lock_irq(&device->al_lock); 1079*b30ab791SAndreas Gruenbacher if (device->resync_wenr != LC_FREE && device->resync_wenr != enr) { 1080b411b363SPhilipp Reisner /* in case you have very heavy scattered io, it may 1081b411b363SPhilipp Reisner * stall the syncer undefined if we give up the ref count 1082b411b363SPhilipp Reisner * when we try again and requeue. 1083b411b363SPhilipp Reisner * 1084b411b363SPhilipp Reisner * if we don't give up the refcount, but the next time 1085b411b363SPhilipp Reisner * we are scheduled this extent has been "synced" by new 1086b411b363SPhilipp Reisner * application writes, we'd miss the lc_put on the 1087b411b363SPhilipp Reisner * extent we keep the refcount on. 1088b411b363SPhilipp Reisner * so we remembered which extent we had to try again, and 1089b411b363SPhilipp Reisner * if the next requested one is something else, we do 1090b411b363SPhilipp Reisner * the lc_put here... 1091b411b363SPhilipp Reisner * we also have to wake_up 1092b411b363SPhilipp Reisner */ 1093*b30ab791SAndreas Gruenbacher e = lc_find(device->resync, device->resync_wenr); 1094b411b363SPhilipp Reisner bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; 1095b411b363SPhilipp Reisner if (bm_ext) { 1096b411b363SPhilipp Reisner D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags)); 1097b411b363SPhilipp Reisner D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags)); 1098b411b363SPhilipp Reisner clear_bit(BME_NO_WRITES, &bm_ext->flags); 1099*b30ab791SAndreas Gruenbacher device->resync_wenr = LC_FREE; 1100*b30ab791SAndreas Gruenbacher if (lc_put(device->resync, &bm_ext->lce) == 0) 1101*b30ab791SAndreas Gruenbacher device->resync_locked--; 1102*b30ab791SAndreas Gruenbacher wake_up(&device->al_wait); 1103b411b363SPhilipp Reisner } else { 1104b411b363SPhilipp Reisner dev_alert(DEV, "LOGIC BUG\n"); 1105b411b363SPhilipp Reisner } 1106b411b363SPhilipp Reisner } 1107b411b363SPhilipp Reisner /* TRY. */ 1108*b30ab791SAndreas Gruenbacher e = lc_try_get(device->resync, enr); 1109b411b363SPhilipp Reisner bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; 1110b411b363SPhilipp Reisner if (bm_ext) { 1111b411b363SPhilipp Reisner if (test_bit(BME_LOCKED, &bm_ext->flags)) 1112b411b363SPhilipp Reisner goto proceed; 1113b411b363SPhilipp Reisner if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) { 1114*b30ab791SAndreas Gruenbacher device->resync_locked++; 1115b411b363SPhilipp Reisner } else { 1116b411b363SPhilipp Reisner /* we did set the BME_NO_WRITES, 1117b411b363SPhilipp Reisner * but then could not set BME_LOCKED, 1118b411b363SPhilipp Reisner * so we tried again. 1119b411b363SPhilipp Reisner * drop the extra reference. */ 1120b411b363SPhilipp Reisner bm_ext->lce.refcnt--; 1121b411b363SPhilipp Reisner D_ASSERT(bm_ext->lce.refcnt > 0); 1122b411b363SPhilipp Reisner } 1123b411b363SPhilipp Reisner goto check_al; 1124b411b363SPhilipp Reisner } else { 1125b411b363SPhilipp Reisner /* do we rather want to try later? */ 1126*b30ab791SAndreas Gruenbacher if (device->resync_locked > device->resync->nr_elements-3) 1127b411b363SPhilipp Reisner goto try_again; 1128b411b363SPhilipp Reisner /* Do or do not. There is no try. -- Yoda */ 1129*b30ab791SAndreas Gruenbacher e = lc_get(device->resync, enr); 1130b411b363SPhilipp Reisner bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; 1131b411b363SPhilipp Reisner if (!bm_ext) { 1132*b30ab791SAndreas Gruenbacher const unsigned long rs_flags = device->resync->flags; 1133b411b363SPhilipp Reisner if (rs_flags & LC_STARVING) 1134b411b363SPhilipp Reisner dev_warn(DEV, "Have to wait for element" 1135b411b363SPhilipp Reisner " (resync LRU too small?)\n"); 113646a15bc3SLars Ellenberg BUG_ON(rs_flags & LC_LOCKED); 1137b411b363SPhilipp Reisner goto try_again; 1138b411b363SPhilipp Reisner } 1139b411b363SPhilipp Reisner if (bm_ext->lce.lc_number != enr) { 1140*b30ab791SAndreas Gruenbacher bm_ext->rs_left = drbd_bm_e_weight(device, enr); 1141b411b363SPhilipp Reisner bm_ext->rs_failed = 0; 1142*b30ab791SAndreas Gruenbacher lc_committed(device->resync); 1143*b30ab791SAndreas Gruenbacher wake_up(&device->al_wait); 1144b411b363SPhilipp Reisner D_ASSERT(test_bit(BME_LOCKED, &bm_ext->flags) == 0); 1145b411b363SPhilipp Reisner } 1146b411b363SPhilipp Reisner set_bit(BME_NO_WRITES, &bm_ext->flags); 1147b411b363SPhilipp Reisner D_ASSERT(bm_ext->lce.refcnt == 1); 1148*b30ab791SAndreas Gruenbacher device->resync_locked++; 1149b411b363SPhilipp Reisner goto check_al; 1150b411b363SPhilipp Reisner } 1151b411b363SPhilipp Reisner check_al: 1152b411b363SPhilipp Reisner for (i = 0; i < AL_EXT_PER_BM_SECT; i++) { 1153*b30ab791SAndreas Gruenbacher if (lc_is_used(device->act_log, al_enr+i)) 1154b411b363SPhilipp Reisner goto try_again; 1155b411b363SPhilipp Reisner } 1156b411b363SPhilipp Reisner set_bit(BME_LOCKED, &bm_ext->flags); 1157b411b363SPhilipp Reisner proceed: 1158*b30ab791SAndreas Gruenbacher device->resync_wenr = LC_FREE; 1159*b30ab791SAndreas Gruenbacher spin_unlock_irq(&device->al_lock); 1160b411b363SPhilipp Reisner return 0; 1161b411b363SPhilipp Reisner 1162b411b363SPhilipp Reisner try_again: 1163b411b363SPhilipp Reisner if (bm_ext) 1164*b30ab791SAndreas Gruenbacher device->resync_wenr = enr; 1165*b30ab791SAndreas Gruenbacher spin_unlock_irq(&device->al_lock); 1166b411b363SPhilipp Reisner return -EAGAIN; 1167b411b363SPhilipp Reisner } 1168b411b363SPhilipp Reisner 1169*b30ab791SAndreas Gruenbacher void drbd_rs_complete_io(struct drbd_device *device, sector_t sector) 1170b411b363SPhilipp Reisner { 1171b411b363SPhilipp Reisner unsigned int enr = BM_SECT_TO_EXT(sector); 1172b411b363SPhilipp Reisner struct lc_element *e; 1173b411b363SPhilipp Reisner struct bm_extent *bm_ext; 1174b411b363SPhilipp Reisner unsigned long flags; 1175b411b363SPhilipp Reisner 1176*b30ab791SAndreas Gruenbacher spin_lock_irqsave(&device->al_lock, flags); 1177*b30ab791SAndreas Gruenbacher e = lc_find(device->resync, enr); 1178b411b363SPhilipp Reisner bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; 1179b411b363SPhilipp Reisner if (!bm_ext) { 1180*b30ab791SAndreas Gruenbacher spin_unlock_irqrestore(&device->al_lock, flags); 1181b411b363SPhilipp Reisner if (__ratelimit(&drbd_ratelimit_state)) 1182b411b363SPhilipp Reisner dev_err(DEV, "drbd_rs_complete_io() called, but extent not found\n"); 1183b411b363SPhilipp Reisner return; 1184b411b363SPhilipp Reisner } 1185b411b363SPhilipp Reisner 1186b411b363SPhilipp Reisner if (bm_ext->lce.refcnt == 0) { 1187*b30ab791SAndreas Gruenbacher spin_unlock_irqrestore(&device->al_lock, flags); 1188b411b363SPhilipp Reisner dev_err(DEV, "drbd_rs_complete_io(,%llu [=%u]) called, " 1189b411b363SPhilipp Reisner "but refcnt is 0!?\n", 1190b411b363SPhilipp Reisner (unsigned long long)sector, enr); 1191b411b363SPhilipp Reisner return; 1192b411b363SPhilipp Reisner } 1193b411b363SPhilipp Reisner 1194*b30ab791SAndreas Gruenbacher if (lc_put(device->resync, &bm_ext->lce) == 0) { 1195e3555d85SPhilipp Reisner bm_ext->flags = 0; /* clear BME_LOCKED, BME_NO_WRITES and BME_PRIORITY */ 1196*b30ab791SAndreas Gruenbacher device->resync_locked--; 1197*b30ab791SAndreas Gruenbacher wake_up(&device->al_wait); 1198b411b363SPhilipp Reisner } 1199b411b363SPhilipp Reisner 1200*b30ab791SAndreas Gruenbacher spin_unlock_irqrestore(&device->al_lock, flags); 1201b411b363SPhilipp Reisner } 1202b411b363SPhilipp Reisner 1203b411b363SPhilipp Reisner /** 1204b411b363SPhilipp Reisner * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED) 1205*b30ab791SAndreas Gruenbacher * @device: DRBD device. 1206b411b363SPhilipp Reisner */ 1207*b30ab791SAndreas Gruenbacher void drbd_rs_cancel_all(struct drbd_device *device) 1208b411b363SPhilipp Reisner { 1209*b30ab791SAndreas Gruenbacher spin_lock_irq(&device->al_lock); 1210b411b363SPhilipp Reisner 1211*b30ab791SAndreas Gruenbacher if (get_ldev_if_state(device, D_FAILED)) { /* Makes sure ->resync is there. */ 1212*b30ab791SAndreas Gruenbacher lc_reset(device->resync); 1213*b30ab791SAndreas Gruenbacher put_ldev(device); 1214b411b363SPhilipp Reisner } 1215*b30ab791SAndreas Gruenbacher device->resync_locked = 0; 1216*b30ab791SAndreas Gruenbacher device->resync_wenr = LC_FREE; 1217*b30ab791SAndreas Gruenbacher spin_unlock_irq(&device->al_lock); 1218*b30ab791SAndreas Gruenbacher wake_up(&device->al_wait); 1219b411b363SPhilipp Reisner } 1220b411b363SPhilipp Reisner 1221b411b363SPhilipp Reisner /** 1222b411b363SPhilipp Reisner * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU 1223*b30ab791SAndreas Gruenbacher * @device: DRBD device. 1224b411b363SPhilipp Reisner * 1225b411b363SPhilipp Reisner * Returns 0 upon success, -EAGAIN if at least one reference count was 1226b411b363SPhilipp Reisner * not zero. 1227b411b363SPhilipp Reisner */ 1228*b30ab791SAndreas Gruenbacher int drbd_rs_del_all(struct drbd_device *device) 1229b411b363SPhilipp Reisner { 1230b411b363SPhilipp Reisner struct lc_element *e; 1231b411b363SPhilipp Reisner struct bm_extent *bm_ext; 1232b411b363SPhilipp Reisner int i; 1233b411b363SPhilipp Reisner 1234*b30ab791SAndreas Gruenbacher spin_lock_irq(&device->al_lock); 1235b411b363SPhilipp Reisner 1236*b30ab791SAndreas Gruenbacher if (get_ldev_if_state(device, D_FAILED)) { 1237b411b363SPhilipp Reisner /* ok, ->resync is there. */ 1238*b30ab791SAndreas Gruenbacher for (i = 0; i < device->resync->nr_elements; i++) { 1239*b30ab791SAndreas Gruenbacher e = lc_element_by_index(device->resync, i); 1240b2b163ddSPhilipp Reisner bm_ext = lc_entry(e, struct bm_extent, lce); 1241b411b363SPhilipp Reisner if (bm_ext->lce.lc_number == LC_FREE) 1242b411b363SPhilipp Reisner continue; 1243*b30ab791SAndreas Gruenbacher if (bm_ext->lce.lc_number == device->resync_wenr) { 1244b411b363SPhilipp Reisner dev_info(DEV, "dropping %u in drbd_rs_del_all, apparently" 1245b411b363SPhilipp Reisner " got 'synced' by application io\n", 1246*b30ab791SAndreas Gruenbacher device->resync_wenr); 1247b411b363SPhilipp Reisner D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags)); 1248b411b363SPhilipp Reisner D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags)); 1249b411b363SPhilipp Reisner clear_bit(BME_NO_WRITES, &bm_ext->flags); 1250*b30ab791SAndreas Gruenbacher device->resync_wenr = LC_FREE; 1251*b30ab791SAndreas Gruenbacher lc_put(device->resync, &bm_ext->lce); 1252b411b363SPhilipp Reisner } 1253b411b363SPhilipp Reisner if (bm_ext->lce.refcnt != 0) { 1254b411b363SPhilipp Reisner dev_info(DEV, "Retrying drbd_rs_del_all() later. " 1255b411b363SPhilipp Reisner "refcnt=%d\n", bm_ext->lce.refcnt); 1256*b30ab791SAndreas Gruenbacher put_ldev(device); 1257*b30ab791SAndreas Gruenbacher spin_unlock_irq(&device->al_lock); 1258b411b363SPhilipp Reisner return -EAGAIN; 1259b411b363SPhilipp Reisner } 1260b411b363SPhilipp Reisner D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags)); 1261b411b363SPhilipp Reisner D_ASSERT(!test_bit(BME_NO_WRITES, &bm_ext->flags)); 1262*b30ab791SAndreas Gruenbacher lc_del(device->resync, &bm_ext->lce); 1263b411b363SPhilipp Reisner } 1264*b30ab791SAndreas Gruenbacher D_ASSERT(device->resync->used == 0); 1265*b30ab791SAndreas Gruenbacher put_ldev(device); 1266b411b363SPhilipp Reisner } 1267*b30ab791SAndreas Gruenbacher spin_unlock_irq(&device->al_lock); 1268*b30ab791SAndreas Gruenbacher wake_up(&device->al_wait); 1269b411b363SPhilipp Reisner 1270b411b363SPhilipp Reisner return 0; 1271b411b363SPhilipp Reisner } 1272b411b363SPhilipp Reisner 1273b411b363SPhilipp Reisner /** 1274b411b363SPhilipp Reisner * drbd_rs_failed_io() - Record information on a failure to resync the specified blocks 1275*b30ab791SAndreas Gruenbacher * @device: DRBD device. 1276b411b363SPhilipp Reisner * @sector: The sector number. 1277b411b363SPhilipp Reisner * @size: Size of failed IO operation, in byte. 1278b411b363SPhilipp Reisner */ 1279*b30ab791SAndreas Gruenbacher void drbd_rs_failed_io(struct drbd_device *device, sector_t sector, int size) 1280b411b363SPhilipp Reisner { 1281b411b363SPhilipp Reisner /* Is called from worker and receiver context _only_ */ 1282b411b363SPhilipp Reisner unsigned long sbnr, ebnr, lbnr; 1283b411b363SPhilipp Reisner unsigned long count; 1284b411b363SPhilipp Reisner sector_t esector, nr_sectors; 1285b411b363SPhilipp Reisner int wake_up = 0; 1286b411b363SPhilipp Reisner 1287c670a398SAndreas Gruenbacher if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) { 1288b411b363SPhilipp Reisner dev_err(DEV, "drbd_rs_failed_io: sector=%llus size=%d nonsense!\n", 1289b411b363SPhilipp Reisner (unsigned long long)sector, size); 1290b411b363SPhilipp Reisner return; 1291b411b363SPhilipp Reisner } 1292*b30ab791SAndreas Gruenbacher nr_sectors = drbd_get_capacity(device->this_bdev); 1293b411b363SPhilipp Reisner esector = sector + (size >> 9) - 1; 1294b411b363SPhilipp Reisner 1295841ce241SAndreas Gruenbacher if (!expect(sector < nr_sectors)) 1296841ce241SAndreas Gruenbacher return; 1297841ce241SAndreas Gruenbacher if (!expect(esector < nr_sectors)) 1298841ce241SAndreas Gruenbacher esector = nr_sectors - 1; 1299b411b363SPhilipp Reisner 1300b411b363SPhilipp Reisner lbnr = BM_SECT_TO_BIT(nr_sectors-1); 1301b411b363SPhilipp Reisner 1302b411b363SPhilipp Reisner /* 1303b411b363SPhilipp Reisner * round up start sector, round down end sector. we make sure we only 1304b411b363SPhilipp Reisner * handle full, aligned, BM_BLOCK_SIZE (4K) blocks */ 1305b411b363SPhilipp Reisner if (unlikely(esector < BM_SECT_PER_BIT-1)) 1306b411b363SPhilipp Reisner return; 1307b411b363SPhilipp Reisner if (unlikely(esector == (nr_sectors-1))) 1308b411b363SPhilipp Reisner ebnr = lbnr; 1309b411b363SPhilipp Reisner else 1310b411b363SPhilipp Reisner ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1)); 1311b411b363SPhilipp Reisner sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1); 1312b411b363SPhilipp Reisner 1313b411b363SPhilipp Reisner if (sbnr > ebnr) 1314b411b363SPhilipp Reisner return; 1315b411b363SPhilipp Reisner 1316b411b363SPhilipp Reisner /* 1317b411b363SPhilipp Reisner * ok, (capacity & 7) != 0 sometimes, but who cares... 1318b411b363SPhilipp Reisner * we count rs_{total,left} in bits, not sectors. 1319b411b363SPhilipp Reisner */ 1320*b30ab791SAndreas Gruenbacher spin_lock_irq(&device->al_lock); 1321*b30ab791SAndreas Gruenbacher count = drbd_bm_count_bits(device, sbnr, ebnr); 1322b411b363SPhilipp Reisner if (count) { 1323*b30ab791SAndreas Gruenbacher device->rs_failed += count; 1324b411b363SPhilipp Reisner 1325*b30ab791SAndreas Gruenbacher if (get_ldev(device)) { 1326*b30ab791SAndreas Gruenbacher drbd_try_clear_on_disk_bm(device, sector, count, false); 1327*b30ab791SAndreas Gruenbacher put_ldev(device); 1328b411b363SPhilipp Reisner } 1329b411b363SPhilipp Reisner 1330b411b363SPhilipp Reisner /* just wake_up unconditional now, various lc_chaged(), 1331b411b363SPhilipp Reisner * lc_put() in drbd_try_clear_on_disk_bm(). */ 1332b411b363SPhilipp Reisner wake_up = 1; 1333b411b363SPhilipp Reisner } 1334*b30ab791SAndreas Gruenbacher spin_unlock_irq(&device->al_lock); 1335b411b363SPhilipp Reisner if (wake_up) 1336*b30ab791SAndreas Gruenbacher wake_up(&device->al_wait); 1337b411b363SPhilipp Reisner } 1338