1 /* 2 drbd_actlog.c 3 4 This file is part of DRBD by Philipp Reisner and Lars Ellenberg. 5 6 Copyright (C) 2003-2008, LINBIT Information Technologies GmbH. 7 Copyright (C) 2003-2008, Philipp Reisner <philipp.reisner@linbit.com>. 8 Copyright (C) 2003-2008, Lars Ellenberg <lars.ellenberg@linbit.com>. 9 10 drbd is free software; you can redistribute it and/or modify 11 it under the terms of the GNU General Public License as published by 12 the Free Software Foundation; either version 2, or (at your option) 13 any later version. 14 15 drbd is distributed in the hope that it will be useful, 16 but WITHOUT ANY WARRANTY; without even the implied warranty of 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 GNU General Public License for more details. 19 20 You should have received a copy of the GNU General Public License 21 along with drbd; see the file COPYING. If not, write to 22 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 23 24 */ 25 26 #include <linux/slab.h> 27 #include <linux/crc32c.h> 28 #include <linux/drbd.h> 29 #include <linux/drbd_limits.h> 30 #include <linux/dynamic_debug.h> 31 #include "drbd_int.h" 32 33 34 enum al_transaction_types { 35 AL_TR_UPDATE = 0, 36 AL_TR_INITIALIZED = 0xffff 37 }; 38 /* all fields on disc in big endian */ 39 struct __packed al_transaction_on_disk { 40 /* don't we all like magic */ 41 __be32 magic; 42 43 /* to identify the most recent transaction block 44 * in the on disk ring buffer */ 45 __be32 tr_number; 46 47 /* checksum on the full 4k block, with this field set to 0. */ 48 __be32 crc32c; 49 50 /* type of transaction, special transaction types like: 51 * purge-all, set-all-idle, set-all-active, ... to-be-defined 52 * see also enum al_transaction_types */ 53 __be16 transaction_type; 54 55 /* we currently allow only a few thousand extents, 56 * so 16bit will be enough for the slot number. */ 57 58 /* how many updates in this transaction */ 59 __be16 n_updates; 60 61 /* maximum slot number, "al-extents" in drbd.conf speak. 62 * Having this in each transaction should make reconfiguration 63 * of that parameter easier. */ 64 __be16 context_size; 65 66 /* slot number the context starts with */ 67 __be16 context_start_slot_nr; 68 69 /* Some reserved bytes. Expected usage is a 64bit counter of 70 * sectors-written since device creation, and other data generation tag 71 * supporting usage */ 72 __be32 __reserved[4]; 73 74 /* --- 36 byte used --- */ 75 76 /* Reserve space for up to AL_UPDATES_PER_TRANSACTION changes 77 * in one transaction, then use the remaining byte in the 4k block for 78 * context information. "Flexible" number of updates per transaction 79 * does not help, as we have to account for the case when all update 80 * slots are used anyways, so it would only complicate code without 81 * additional benefit. 82 */ 83 __be16 update_slot_nr[AL_UPDATES_PER_TRANSACTION]; 84 85 /* but the extent number is 32bit, which at an extent size of 4 MiB 86 * allows to cover device sizes of up to 2**54 Byte (16 PiB) */ 87 __be32 update_extent_nr[AL_UPDATES_PER_TRANSACTION]; 88 89 /* --- 420 bytes used (36 + 64*6) --- */ 90 91 /* 4096 - 420 = 3676 = 919 * 4 */ 92 __be32 context[AL_CONTEXT_PER_TRANSACTION]; 93 }; 94 95 void *drbd_md_get_buffer(struct drbd_device *device) 96 { 97 int r; 98 99 wait_event(device->misc_wait, 100 (r = atomic_cmpxchg(&device->md_io_in_use, 0, 1)) == 0 || 101 device->state.disk <= D_FAILED); 102 103 return r ? NULL : page_address(device->md_io_page); 104 } 105 106 void drbd_md_put_buffer(struct drbd_device *device) 107 { 108 if (atomic_dec_and_test(&device->md_io_in_use)) 109 wake_up(&device->misc_wait); 110 } 111 112 void wait_until_done_or_force_detached(struct drbd_device *device, struct drbd_backing_dev *bdev, 113 unsigned int *done) 114 { 115 long dt; 116 117 rcu_read_lock(); 118 dt = rcu_dereference(bdev->disk_conf)->disk_timeout; 119 rcu_read_unlock(); 120 dt = dt * HZ / 10; 121 if (dt == 0) 122 dt = MAX_SCHEDULE_TIMEOUT; 123 124 dt = wait_event_timeout(device->misc_wait, 125 *done || test_bit(FORCE_DETACH, &device->flags), dt); 126 if (dt == 0) { 127 drbd_err(device, "meta-data IO operation timed out\n"); 128 drbd_chk_io_error(device, 1, DRBD_FORCE_DETACH); 129 } 130 } 131 132 static int _drbd_md_sync_page_io(struct drbd_device *device, 133 struct drbd_backing_dev *bdev, 134 struct page *page, sector_t sector, 135 int rw, int size) 136 { 137 struct bio *bio; 138 int err; 139 140 device->md_io.done = 0; 141 device->md_io.error = -ENODEV; 142 143 if ((rw & WRITE) && !test_bit(MD_NO_FUA, &device->flags)) 144 rw |= REQ_FUA | REQ_FLUSH; 145 rw |= REQ_SYNC | REQ_NOIDLE; 146 147 bio = bio_alloc_drbd(GFP_NOIO); 148 bio->bi_bdev = bdev->md_bdev; 149 bio->bi_iter.bi_sector = sector; 150 err = -EIO; 151 if (bio_add_page(bio, page, size, 0) != size) 152 goto out; 153 bio->bi_private = &device->md_io; 154 bio->bi_end_io = drbd_md_io_complete; 155 bio->bi_rw = rw; 156 157 if (!(rw & WRITE) && device->state.disk == D_DISKLESS && device->ldev == NULL) 158 /* special case, drbd_md_read() during drbd_adm_attach(): no get_ldev */ 159 ; 160 else if (!get_ldev_if_state(device, D_ATTACHING)) { 161 /* Corresponding put_ldev in drbd_md_io_complete() */ 162 drbd_err(device, "ASSERT FAILED: get_ldev_if_state() == 1 in _drbd_md_sync_page_io()\n"); 163 err = -ENODEV; 164 goto out; 165 } 166 167 bio_get(bio); /* one bio_put() is in the completion handler */ 168 atomic_inc(&device->md_io_in_use); /* drbd_md_put_buffer() is in the completion handler */ 169 if (drbd_insert_fault(device, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD)) 170 bio_endio(bio, -EIO); 171 else 172 submit_bio(rw, bio); 173 wait_until_done_or_force_detached(device, bdev, &device->md_io.done); 174 if (bio_flagged(bio, BIO_UPTODATE)) 175 err = device->md_io.error; 176 177 out: 178 bio_put(bio); 179 return err; 180 } 181 182 int drbd_md_sync_page_io(struct drbd_device *device, struct drbd_backing_dev *bdev, 183 sector_t sector, int rw) 184 { 185 int err; 186 struct page *iop = device->md_io_page; 187 188 D_ASSERT(device, atomic_read(&device->md_io_in_use) == 1); 189 190 BUG_ON(!bdev->md_bdev); 191 192 dynamic_drbd_dbg(device, "meta_data io: %s [%d]:%s(,%llus,%s) %pS\n", 193 current->comm, current->pid, __func__, 194 (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ", 195 (void*)_RET_IP_ ); 196 197 if (sector < drbd_md_first_sector(bdev) || 198 sector + 7 > drbd_md_last_sector(bdev)) 199 drbd_alert(device, "%s [%d]:%s(,%llus,%s) out of range md access!\n", 200 current->comm, current->pid, __func__, 201 (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ"); 202 203 /* we do all our meta data IO in aligned 4k blocks. */ 204 err = _drbd_md_sync_page_io(device, bdev, iop, sector, rw, 4096); 205 if (err) { 206 drbd_err(device, "drbd_md_sync_page_io(,%llus,%s) failed with error %d\n", 207 (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ", err); 208 } 209 return err; 210 } 211 212 static struct bm_extent *find_active_resync_extent(struct drbd_device *device, unsigned int enr) 213 { 214 struct lc_element *tmp; 215 tmp = lc_find(device->resync, enr/AL_EXT_PER_BM_SECT); 216 if (unlikely(tmp != NULL)) { 217 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce); 218 if (test_bit(BME_NO_WRITES, &bm_ext->flags)) 219 return bm_ext; 220 } 221 return NULL; 222 } 223 224 static struct lc_element *_al_get(struct drbd_device *device, unsigned int enr, bool nonblock) 225 { 226 struct lc_element *al_ext; 227 struct bm_extent *bm_ext; 228 int wake; 229 230 spin_lock_irq(&device->al_lock); 231 bm_ext = find_active_resync_extent(device, enr); 232 if (bm_ext) { 233 wake = !test_and_set_bit(BME_PRIORITY, &bm_ext->flags); 234 spin_unlock_irq(&device->al_lock); 235 if (wake) 236 wake_up(&device->al_wait); 237 return NULL; 238 } 239 if (nonblock) 240 al_ext = lc_try_get(device->act_log, enr); 241 else 242 al_ext = lc_get(device->act_log, enr); 243 spin_unlock_irq(&device->al_lock); 244 return al_ext; 245 } 246 247 bool drbd_al_begin_io_fastpath(struct drbd_device *device, struct drbd_interval *i) 248 { 249 /* for bios crossing activity log extent boundaries, 250 * we may need to activate two extents in one go */ 251 unsigned first = i->sector >> (AL_EXTENT_SHIFT-9); 252 unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9); 253 254 D_ASSERT(device, (unsigned)(last - first) <= 1); 255 D_ASSERT(device, atomic_read(&device->local_cnt) > 0); 256 257 /* FIXME figure out a fast path for bios crossing AL extent boundaries */ 258 if (first != last) 259 return false; 260 261 return _al_get(device, first, true); 262 } 263 264 bool drbd_al_begin_io_prepare(struct drbd_device *device, struct drbd_interval *i) 265 { 266 /* for bios crossing activity log extent boundaries, 267 * we may need to activate two extents in one go */ 268 unsigned first = i->sector >> (AL_EXTENT_SHIFT-9); 269 unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9); 270 unsigned enr; 271 bool need_transaction = false; 272 273 D_ASSERT(device, first <= last); 274 D_ASSERT(device, atomic_read(&device->local_cnt) > 0); 275 276 for (enr = first; enr <= last; enr++) { 277 struct lc_element *al_ext; 278 wait_event(device->al_wait, 279 (al_ext = _al_get(device, enr, false)) != NULL); 280 if (al_ext->lc_number != enr) 281 need_transaction = true; 282 } 283 return need_transaction; 284 } 285 286 static int al_write_transaction(struct drbd_device *device); 287 288 void drbd_al_begin_io_commit(struct drbd_device *device) 289 { 290 bool locked = false; 291 292 /* Serialize multiple transactions. 293 * This uses test_and_set_bit, memory barrier is implicit. 294 */ 295 wait_event(device->al_wait, 296 device->act_log->pending_changes == 0 || 297 (locked = lc_try_lock_for_transaction(device->act_log))); 298 299 if (locked) { 300 /* Double check: it may have been committed by someone else, 301 * while we have been waiting for the lock. */ 302 if (device->act_log->pending_changes) { 303 bool write_al_updates; 304 305 rcu_read_lock(); 306 write_al_updates = rcu_dereference(device->ldev->disk_conf)->al_updates; 307 rcu_read_unlock(); 308 309 if (write_al_updates) 310 al_write_transaction(device); 311 spin_lock_irq(&device->al_lock); 312 /* FIXME 313 if (err) 314 we need an "lc_cancel" here; 315 */ 316 lc_committed(device->act_log); 317 spin_unlock_irq(&device->al_lock); 318 } 319 lc_unlock(device->act_log); 320 wake_up(&device->al_wait); 321 } 322 } 323 324 /* 325 * @delegate: delegate activity log I/O to the worker thread 326 */ 327 void drbd_al_begin_io(struct drbd_device *device, struct drbd_interval *i) 328 { 329 if (drbd_al_begin_io_prepare(device, i)) 330 drbd_al_begin_io_commit(device); 331 } 332 333 int drbd_al_begin_io_nonblock(struct drbd_device *device, struct drbd_interval *i) 334 { 335 struct lru_cache *al = device->act_log; 336 /* for bios crossing activity log extent boundaries, 337 * we may need to activate two extents in one go */ 338 unsigned first = i->sector >> (AL_EXTENT_SHIFT-9); 339 unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9); 340 unsigned nr_al_extents; 341 unsigned available_update_slots; 342 unsigned enr; 343 344 D_ASSERT(device, first <= last); 345 346 nr_al_extents = 1 + last - first; /* worst case: all touched extends are cold. */ 347 available_update_slots = min(al->nr_elements - al->used, 348 al->max_pending_changes - al->pending_changes); 349 350 /* We want all necessary updates for a given request within the same transaction 351 * We could first check how many updates are *actually* needed, 352 * and use that instead of the worst-case nr_al_extents */ 353 if (available_update_slots < nr_al_extents) 354 return -EWOULDBLOCK; 355 356 /* Is resync active in this area? */ 357 for (enr = first; enr <= last; enr++) { 358 struct lc_element *tmp; 359 tmp = lc_find(device->resync, enr/AL_EXT_PER_BM_SECT); 360 if (unlikely(tmp != NULL)) { 361 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce); 362 if (test_bit(BME_NO_WRITES, &bm_ext->flags)) { 363 if (!test_and_set_bit(BME_PRIORITY, &bm_ext->flags)) 364 return -EBUSY; 365 return -EWOULDBLOCK; 366 } 367 } 368 } 369 370 /* Checkout the refcounts. 371 * Given that we checked for available elements and update slots above, 372 * this has to be successful. */ 373 for (enr = first; enr <= last; enr++) { 374 struct lc_element *al_ext; 375 al_ext = lc_get_cumulative(device->act_log, enr); 376 if (!al_ext) 377 drbd_info(device, "LOGIC BUG for enr=%u\n", enr); 378 } 379 return 0; 380 } 381 382 void drbd_al_complete_io(struct drbd_device *device, struct drbd_interval *i) 383 { 384 /* for bios crossing activity log extent boundaries, 385 * we may need to activate two extents in one go */ 386 unsigned first = i->sector >> (AL_EXTENT_SHIFT-9); 387 unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9); 388 unsigned enr; 389 struct lc_element *extent; 390 unsigned long flags; 391 392 D_ASSERT(device, first <= last); 393 spin_lock_irqsave(&device->al_lock, flags); 394 395 for (enr = first; enr <= last; enr++) { 396 extent = lc_find(device->act_log, enr); 397 if (!extent) { 398 drbd_err(device, "al_complete_io() called on inactive extent %u\n", enr); 399 continue; 400 } 401 lc_put(device->act_log, extent); 402 } 403 spin_unlock_irqrestore(&device->al_lock, flags); 404 wake_up(&device->al_wait); 405 } 406 407 #if (PAGE_SHIFT + 3) < (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT) 408 /* Currently BM_BLOCK_SHIFT, BM_EXT_SHIFT and AL_EXTENT_SHIFT 409 * are still coupled, or assume too much about their relation. 410 * Code below will not work if this is violated. 411 * Will be cleaned up with some followup patch. 412 */ 413 # error FIXME 414 #endif 415 416 static unsigned int al_extent_to_bm_page(unsigned int al_enr) 417 { 418 return al_enr >> 419 /* bit to page */ 420 ((PAGE_SHIFT + 3) - 421 /* al extent number to bit */ 422 (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT)); 423 } 424 425 static sector_t al_tr_number_to_on_disk_sector(struct drbd_device *device) 426 { 427 const unsigned int stripes = device->ldev->md.al_stripes; 428 const unsigned int stripe_size_4kB = device->ldev->md.al_stripe_size_4k; 429 430 /* transaction number, modulo on-disk ring buffer wrap around */ 431 unsigned int t = device->al_tr_number % (device->ldev->md.al_size_4k); 432 433 /* ... to aligned 4k on disk block */ 434 t = ((t % stripes) * stripe_size_4kB) + t/stripes; 435 436 /* ... to 512 byte sector in activity log */ 437 t *= 8; 438 439 /* ... plus offset to the on disk position */ 440 return device->ldev->md.md_offset + device->ldev->md.al_offset + t; 441 } 442 443 int al_write_transaction(struct drbd_device *device) 444 { 445 struct al_transaction_on_disk *buffer; 446 struct lc_element *e; 447 sector_t sector; 448 int i, mx; 449 unsigned extent_nr; 450 unsigned crc = 0; 451 int err = 0; 452 453 if (!get_ldev(device)) { 454 drbd_err(device, "disk is %s, cannot start al transaction\n", 455 drbd_disk_str(device->state.disk)); 456 return -EIO; 457 } 458 459 /* The bitmap write may have failed, causing a state change. */ 460 if (device->state.disk < D_INCONSISTENT) { 461 drbd_err(device, 462 "disk is %s, cannot write al transaction\n", 463 drbd_disk_str(device->state.disk)); 464 put_ldev(device); 465 return -EIO; 466 } 467 468 buffer = drbd_md_get_buffer(device); /* protects md_io_buffer, al_tr_cycle, ... */ 469 if (!buffer) { 470 drbd_err(device, "disk failed while waiting for md_io buffer\n"); 471 put_ldev(device); 472 return -ENODEV; 473 } 474 475 memset(buffer, 0, sizeof(*buffer)); 476 buffer->magic = cpu_to_be32(DRBD_AL_MAGIC); 477 buffer->tr_number = cpu_to_be32(device->al_tr_number); 478 479 i = 0; 480 481 /* Even though no one can start to change this list 482 * once we set the LC_LOCKED -- from drbd_al_begin_io(), 483 * lc_try_lock_for_transaction() --, someone may still 484 * be in the process of changing it. */ 485 spin_lock_irq(&device->al_lock); 486 list_for_each_entry(e, &device->act_log->to_be_changed, list) { 487 if (i == AL_UPDATES_PER_TRANSACTION) { 488 i++; 489 break; 490 } 491 buffer->update_slot_nr[i] = cpu_to_be16(e->lc_index); 492 buffer->update_extent_nr[i] = cpu_to_be32(e->lc_new_number); 493 if (e->lc_number != LC_FREE) 494 drbd_bm_mark_for_writeout(device, 495 al_extent_to_bm_page(e->lc_number)); 496 i++; 497 } 498 spin_unlock_irq(&device->al_lock); 499 BUG_ON(i > AL_UPDATES_PER_TRANSACTION); 500 501 buffer->n_updates = cpu_to_be16(i); 502 for ( ; i < AL_UPDATES_PER_TRANSACTION; i++) { 503 buffer->update_slot_nr[i] = cpu_to_be16(-1); 504 buffer->update_extent_nr[i] = cpu_to_be32(LC_FREE); 505 } 506 507 buffer->context_size = cpu_to_be16(device->act_log->nr_elements); 508 buffer->context_start_slot_nr = cpu_to_be16(device->al_tr_cycle); 509 510 mx = min_t(int, AL_CONTEXT_PER_TRANSACTION, 511 device->act_log->nr_elements - device->al_tr_cycle); 512 for (i = 0; i < mx; i++) { 513 unsigned idx = device->al_tr_cycle + i; 514 extent_nr = lc_element_by_index(device->act_log, idx)->lc_number; 515 buffer->context[i] = cpu_to_be32(extent_nr); 516 } 517 for (; i < AL_CONTEXT_PER_TRANSACTION; i++) 518 buffer->context[i] = cpu_to_be32(LC_FREE); 519 520 device->al_tr_cycle += AL_CONTEXT_PER_TRANSACTION; 521 if (device->al_tr_cycle >= device->act_log->nr_elements) 522 device->al_tr_cycle = 0; 523 524 sector = al_tr_number_to_on_disk_sector(device); 525 526 crc = crc32c(0, buffer, 4096); 527 buffer->crc32c = cpu_to_be32(crc); 528 529 if (drbd_bm_write_hinted(device)) 530 err = -EIO; 531 else { 532 bool write_al_updates; 533 rcu_read_lock(); 534 write_al_updates = rcu_dereference(device->ldev->disk_conf)->al_updates; 535 rcu_read_unlock(); 536 if (write_al_updates) { 537 if (drbd_md_sync_page_io(device, device->ldev, sector, WRITE)) { 538 err = -EIO; 539 drbd_chk_io_error(device, 1, DRBD_META_IO_ERROR); 540 } else { 541 device->al_tr_number++; 542 device->al_writ_cnt++; 543 } 544 } 545 } 546 547 drbd_md_put_buffer(device); 548 put_ldev(device); 549 550 return err; 551 } 552 553 static int _try_lc_del(struct drbd_device *device, struct lc_element *al_ext) 554 { 555 int rv; 556 557 spin_lock_irq(&device->al_lock); 558 rv = (al_ext->refcnt == 0); 559 if (likely(rv)) 560 lc_del(device->act_log, al_ext); 561 spin_unlock_irq(&device->al_lock); 562 563 return rv; 564 } 565 566 /** 567 * drbd_al_shrink() - Removes all active extents form the activity log 568 * @device: DRBD device. 569 * 570 * Removes all active extents form the activity log, waiting until 571 * the reference count of each entry dropped to 0 first, of course. 572 * 573 * You need to lock device->act_log with lc_try_lock() / lc_unlock() 574 */ 575 void drbd_al_shrink(struct drbd_device *device) 576 { 577 struct lc_element *al_ext; 578 int i; 579 580 D_ASSERT(device, test_bit(__LC_LOCKED, &device->act_log->flags)); 581 582 for (i = 0; i < device->act_log->nr_elements; i++) { 583 al_ext = lc_element_by_index(device->act_log, i); 584 if (al_ext->lc_number == LC_FREE) 585 continue; 586 wait_event(device->al_wait, _try_lc_del(device, al_ext)); 587 } 588 589 wake_up(&device->al_wait); 590 } 591 592 int drbd_initialize_al(struct drbd_device *device, void *buffer) 593 { 594 struct al_transaction_on_disk *al = buffer; 595 struct drbd_md *md = &device->ldev->md; 596 sector_t al_base = md->md_offset + md->al_offset; 597 int al_size_4k = md->al_stripes * md->al_stripe_size_4k; 598 int i; 599 600 memset(al, 0, 4096); 601 al->magic = cpu_to_be32(DRBD_AL_MAGIC); 602 al->transaction_type = cpu_to_be16(AL_TR_INITIALIZED); 603 al->crc32c = cpu_to_be32(crc32c(0, al, 4096)); 604 605 for (i = 0; i < al_size_4k; i++) { 606 int err = drbd_md_sync_page_io(device, device->ldev, al_base + i * 8, WRITE); 607 if (err) 608 return err; 609 } 610 return 0; 611 } 612 613 static const char *drbd_change_sync_fname[] = { 614 [RECORD_RS_FAILED] = "drbd_rs_failed_io", 615 [SET_IN_SYNC] = "drbd_set_in_sync", 616 [SET_OUT_OF_SYNC] = "drbd_set_out_of_sync" 617 }; 618 619 /* ATTENTION. The AL's extents are 4MB each, while the extents in the 620 * resync LRU-cache are 16MB each. 621 * The caller of this function has to hold an get_ldev() reference. 622 * 623 * Adjusts the caching members ->rs_left (success) or ->rs_failed (!success), 624 * potentially pulling in (and recounting the corresponding bits) 625 * this resync extent into the resync extent lru cache. 626 * 627 * Returns whether all bits have been cleared for this resync extent, 628 * precisely: (rs_left <= rs_failed) 629 * 630 * TODO will be obsoleted once we have a caching lru of the on disk bitmap 631 */ 632 static bool update_rs_extent(struct drbd_device *device, 633 unsigned int enr, int count, 634 enum update_sync_bits_mode mode) 635 { 636 struct lc_element *e; 637 638 D_ASSERT(device, atomic_read(&device->local_cnt)); 639 640 /* When setting out-of-sync bits, 641 * we don't need it cached (lc_find). 642 * But if it is present in the cache, 643 * we should update the cached bit count. 644 * Otherwise, that extent should be in the resync extent lru cache 645 * already -- or we want to pull it in if necessary -- (lc_get), 646 * then update and check rs_left and rs_failed. */ 647 if (mode == SET_OUT_OF_SYNC) 648 e = lc_find(device->resync, enr); 649 else 650 e = lc_get(device->resync, enr); 651 if (e) { 652 struct bm_extent *ext = lc_entry(e, struct bm_extent, lce); 653 if (ext->lce.lc_number == enr) { 654 if (mode == SET_IN_SYNC) 655 ext->rs_left -= count; 656 else if (mode == SET_OUT_OF_SYNC) 657 ext->rs_left += count; 658 else 659 ext->rs_failed += count; 660 if (ext->rs_left < ext->rs_failed) { 661 drbd_warn(device, "BAD! enr=%u rs_left=%d " 662 "rs_failed=%d count=%d cstate=%s\n", 663 ext->lce.lc_number, ext->rs_left, 664 ext->rs_failed, count, 665 drbd_conn_str(device->state.conn)); 666 667 /* We don't expect to be able to clear more bits 668 * than have been set when we originally counted 669 * the set bits to cache that value in ext->rs_left. 670 * Whatever the reason (disconnect during resync, 671 * delayed local completion of an application write), 672 * try to fix it up by recounting here. */ 673 ext->rs_left = drbd_bm_e_weight(device, enr); 674 } 675 } else { 676 /* Normally this element should be in the cache, 677 * since drbd_rs_begin_io() pulled it already in. 678 * 679 * But maybe an application write finished, and we set 680 * something outside the resync lru_cache in sync. 681 */ 682 int rs_left = drbd_bm_e_weight(device, enr); 683 if (ext->flags != 0) { 684 drbd_warn(device, "changing resync lce: %d[%u;%02lx]" 685 " -> %d[%u;00]\n", 686 ext->lce.lc_number, ext->rs_left, 687 ext->flags, enr, rs_left); 688 ext->flags = 0; 689 } 690 if (ext->rs_failed) { 691 drbd_warn(device, "Kicking resync_lru element enr=%u " 692 "out with rs_failed=%d\n", 693 ext->lce.lc_number, ext->rs_failed); 694 } 695 ext->rs_left = rs_left; 696 ext->rs_failed = (mode == RECORD_RS_FAILED) ? count : 0; 697 /* we don't keep a persistent log of the resync lru, 698 * we can commit any change right away. */ 699 lc_committed(device->resync); 700 } 701 if (mode != SET_OUT_OF_SYNC) 702 lc_put(device->resync, &ext->lce); 703 /* no race, we are within the al_lock! */ 704 705 if (ext->rs_left <= ext->rs_failed) { 706 ext->rs_failed = 0; 707 return true; 708 } 709 } else if (mode != SET_OUT_OF_SYNC) { 710 /* be quiet if lc_find() did not find it. */ 711 drbd_err(device, "lc_get() failed! locked=%d/%d flags=%lu\n", 712 device->resync_locked, 713 device->resync->nr_elements, 714 device->resync->flags); 715 } 716 return false; 717 } 718 719 void drbd_advance_rs_marks(struct drbd_device *device, unsigned long still_to_go) 720 { 721 unsigned long now = jiffies; 722 unsigned long last = device->rs_mark_time[device->rs_last_mark]; 723 int next = (device->rs_last_mark + 1) % DRBD_SYNC_MARKS; 724 if (time_after_eq(now, last + DRBD_SYNC_MARK_STEP)) { 725 if (device->rs_mark_left[device->rs_last_mark] != still_to_go && 726 device->state.conn != C_PAUSED_SYNC_T && 727 device->state.conn != C_PAUSED_SYNC_S) { 728 device->rs_mark_time[next] = now; 729 device->rs_mark_left[next] = still_to_go; 730 device->rs_last_mark = next; 731 } 732 } 733 } 734 735 /* It is called lazy update, so don't do write-out too often. */ 736 static bool lazy_bitmap_update_due(struct drbd_device *device) 737 { 738 return time_after(jiffies, device->rs_last_bcast + 2*HZ); 739 } 740 741 static void maybe_schedule_on_disk_bitmap_update(struct drbd_device *device, bool rs_done) 742 { 743 if (rs_done) 744 set_bit(RS_DONE, &device->flags); 745 /* and also set RS_PROGRESS below */ 746 else if (!lazy_bitmap_update_due(device)) 747 return; 748 749 drbd_device_post_work(device, RS_PROGRESS); 750 } 751 752 static int update_sync_bits(struct drbd_device *device, 753 unsigned long sbnr, unsigned long ebnr, 754 enum update_sync_bits_mode mode) 755 { 756 /* 757 * We keep a count of set bits per resync-extent in the ->rs_left 758 * caching member, so we need to loop and work within the resync extent 759 * alignment. Typically this loop will execute exactly once. 760 */ 761 unsigned long flags; 762 unsigned long count = 0; 763 unsigned int cleared = 0; 764 while (sbnr <= ebnr) { 765 /* set temporary boundary bit number to last bit number within 766 * the resync extent of the current start bit number, 767 * but cap at provided end bit number */ 768 unsigned long tbnr = min(ebnr, sbnr | BM_BLOCKS_PER_BM_EXT_MASK); 769 unsigned long c; 770 771 if (mode == RECORD_RS_FAILED) 772 /* Only called from drbd_rs_failed_io(), bits 773 * supposedly still set. Recount, maybe some 774 * of the bits have been successfully cleared 775 * by application IO meanwhile. 776 */ 777 c = drbd_bm_count_bits(device, sbnr, tbnr); 778 else if (mode == SET_IN_SYNC) 779 c = drbd_bm_clear_bits(device, sbnr, tbnr); 780 else /* if (mode == SET_OUT_OF_SYNC) */ 781 c = drbd_bm_set_bits(device, sbnr, tbnr); 782 783 if (c) { 784 spin_lock_irqsave(&device->al_lock, flags); 785 cleared += update_rs_extent(device, BM_BIT_TO_EXT(sbnr), c, mode); 786 spin_unlock_irqrestore(&device->al_lock, flags); 787 count += c; 788 } 789 sbnr = tbnr + 1; 790 } 791 if (count) { 792 if (mode == SET_IN_SYNC) { 793 unsigned long still_to_go = drbd_bm_total_weight(device); 794 bool rs_is_done = (still_to_go <= device->rs_failed); 795 drbd_advance_rs_marks(device, still_to_go); 796 if (cleared || rs_is_done) 797 maybe_schedule_on_disk_bitmap_update(device, rs_is_done); 798 } else if (mode == RECORD_RS_FAILED) 799 device->rs_failed += count; 800 wake_up(&device->al_wait); 801 } 802 return count; 803 } 804 805 /* clear the bit corresponding to the piece of storage in question: 806 * size byte of data starting from sector. Only clear a bits of the affected 807 * one ore more _aligned_ BM_BLOCK_SIZE blocks. 808 * 809 * called by worker on C_SYNC_TARGET and receiver on SyncSource. 810 * 811 */ 812 int __drbd_change_sync(struct drbd_device *device, sector_t sector, int size, 813 enum update_sync_bits_mode mode, 814 const char *file, const unsigned int line) 815 { 816 /* Is called from worker and receiver context _only_ */ 817 unsigned long sbnr, ebnr, lbnr; 818 unsigned long count = 0; 819 sector_t esector, nr_sectors; 820 821 /* This would be an empty REQ_FLUSH, be silent. */ 822 if ((mode == SET_OUT_OF_SYNC) && size == 0) 823 return 0; 824 825 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_DISCARD_SIZE) { 826 drbd_err(device, "%s: sector=%llus size=%d nonsense!\n", 827 drbd_change_sync_fname[mode], 828 (unsigned long long)sector, size); 829 return 0; 830 } 831 832 if (!get_ldev(device)) 833 return 0; /* no disk, no metadata, no bitmap to manipulate bits in */ 834 835 nr_sectors = drbd_get_capacity(device->this_bdev); 836 esector = sector + (size >> 9) - 1; 837 838 if (!expect(sector < nr_sectors)) 839 goto out; 840 if (!expect(esector < nr_sectors)) 841 esector = nr_sectors - 1; 842 843 lbnr = BM_SECT_TO_BIT(nr_sectors-1); 844 845 if (mode == SET_IN_SYNC) { 846 /* Round up start sector, round down end sector. We make sure 847 * we only clear full, aligned, BM_BLOCK_SIZE blocks. */ 848 if (unlikely(esector < BM_SECT_PER_BIT-1)) 849 goto out; 850 if (unlikely(esector == (nr_sectors-1))) 851 ebnr = lbnr; 852 else 853 ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1)); 854 sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1); 855 } else { 856 /* We set it out of sync, or record resync failure. 857 * Should not round anything here. */ 858 sbnr = BM_SECT_TO_BIT(sector); 859 ebnr = BM_SECT_TO_BIT(esector); 860 } 861 862 count = update_sync_bits(device, sbnr, ebnr, mode); 863 out: 864 put_ldev(device); 865 return count; 866 } 867 868 static 869 struct bm_extent *_bme_get(struct drbd_device *device, unsigned int enr) 870 { 871 struct lc_element *e; 872 struct bm_extent *bm_ext; 873 int wakeup = 0; 874 unsigned long rs_flags; 875 876 spin_lock_irq(&device->al_lock); 877 if (device->resync_locked > device->resync->nr_elements/2) { 878 spin_unlock_irq(&device->al_lock); 879 return NULL; 880 } 881 e = lc_get(device->resync, enr); 882 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; 883 if (bm_ext) { 884 if (bm_ext->lce.lc_number != enr) { 885 bm_ext->rs_left = drbd_bm_e_weight(device, enr); 886 bm_ext->rs_failed = 0; 887 lc_committed(device->resync); 888 wakeup = 1; 889 } 890 if (bm_ext->lce.refcnt == 1) 891 device->resync_locked++; 892 set_bit(BME_NO_WRITES, &bm_ext->flags); 893 } 894 rs_flags = device->resync->flags; 895 spin_unlock_irq(&device->al_lock); 896 if (wakeup) 897 wake_up(&device->al_wait); 898 899 if (!bm_ext) { 900 if (rs_flags & LC_STARVING) 901 drbd_warn(device, "Have to wait for element" 902 " (resync LRU too small?)\n"); 903 BUG_ON(rs_flags & LC_LOCKED); 904 } 905 906 return bm_ext; 907 } 908 909 static int _is_in_al(struct drbd_device *device, unsigned int enr) 910 { 911 int rv; 912 913 spin_lock_irq(&device->al_lock); 914 rv = lc_is_used(device->act_log, enr); 915 spin_unlock_irq(&device->al_lock); 916 917 return rv; 918 } 919 920 /** 921 * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED 922 * @device: DRBD device. 923 * @sector: The sector number. 924 * 925 * This functions sleeps on al_wait. Returns 0 on success, -EINTR if interrupted. 926 */ 927 int drbd_rs_begin_io(struct drbd_device *device, sector_t sector) 928 { 929 unsigned int enr = BM_SECT_TO_EXT(sector); 930 struct bm_extent *bm_ext; 931 int i, sig; 932 bool sa; 933 934 retry: 935 sig = wait_event_interruptible(device->al_wait, 936 (bm_ext = _bme_get(device, enr))); 937 if (sig) 938 return -EINTR; 939 940 if (test_bit(BME_LOCKED, &bm_ext->flags)) 941 return 0; 942 943 /* step aside only while we are above c-min-rate; unless disabled. */ 944 sa = drbd_rs_c_min_rate_throttle(device); 945 946 for (i = 0; i < AL_EXT_PER_BM_SECT; i++) { 947 sig = wait_event_interruptible(device->al_wait, 948 !_is_in_al(device, enr * AL_EXT_PER_BM_SECT + i) || 949 (sa && test_bit(BME_PRIORITY, &bm_ext->flags))); 950 951 if (sig || (sa && test_bit(BME_PRIORITY, &bm_ext->flags))) { 952 spin_lock_irq(&device->al_lock); 953 if (lc_put(device->resync, &bm_ext->lce) == 0) { 954 bm_ext->flags = 0; /* clears BME_NO_WRITES and eventually BME_PRIORITY */ 955 device->resync_locked--; 956 wake_up(&device->al_wait); 957 } 958 spin_unlock_irq(&device->al_lock); 959 if (sig) 960 return -EINTR; 961 if (schedule_timeout_interruptible(HZ/10)) 962 return -EINTR; 963 goto retry; 964 } 965 } 966 set_bit(BME_LOCKED, &bm_ext->flags); 967 return 0; 968 } 969 970 /** 971 * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep 972 * @device: DRBD device. 973 * @sector: The sector number. 974 * 975 * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then 976 * tries to set it to BME_LOCKED. Returns 0 upon success, and -EAGAIN 977 * if there is still application IO going on in this area. 978 */ 979 int drbd_try_rs_begin_io(struct drbd_device *device, sector_t sector) 980 { 981 unsigned int enr = BM_SECT_TO_EXT(sector); 982 const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT; 983 struct lc_element *e; 984 struct bm_extent *bm_ext; 985 int i; 986 987 spin_lock_irq(&device->al_lock); 988 if (device->resync_wenr != LC_FREE && device->resync_wenr != enr) { 989 /* in case you have very heavy scattered io, it may 990 * stall the syncer undefined if we give up the ref count 991 * when we try again and requeue. 992 * 993 * if we don't give up the refcount, but the next time 994 * we are scheduled this extent has been "synced" by new 995 * application writes, we'd miss the lc_put on the 996 * extent we keep the refcount on. 997 * so we remembered which extent we had to try again, and 998 * if the next requested one is something else, we do 999 * the lc_put here... 1000 * we also have to wake_up 1001 */ 1002 e = lc_find(device->resync, device->resync_wenr); 1003 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; 1004 if (bm_ext) { 1005 D_ASSERT(device, !test_bit(BME_LOCKED, &bm_ext->flags)); 1006 D_ASSERT(device, test_bit(BME_NO_WRITES, &bm_ext->flags)); 1007 clear_bit(BME_NO_WRITES, &bm_ext->flags); 1008 device->resync_wenr = LC_FREE; 1009 if (lc_put(device->resync, &bm_ext->lce) == 0) 1010 device->resync_locked--; 1011 wake_up(&device->al_wait); 1012 } else { 1013 drbd_alert(device, "LOGIC BUG\n"); 1014 } 1015 } 1016 /* TRY. */ 1017 e = lc_try_get(device->resync, enr); 1018 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; 1019 if (bm_ext) { 1020 if (test_bit(BME_LOCKED, &bm_ext->flags)) 1021 goto proceed; 1022 if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) { 1023 device->resync_locked++; 1024 } else { 1025 /* we did set the BME_NO_WRITES, 1026 * but then could not set BME_LOCKED, 1027 * so we tried again. 1028 * drop the extra reference. */ 1029 bm_ext->lce.refcnt--; 1030 D_ASSERT(device, bm_ext->lce.refcnt > 0); 1031 } 1032 goto check_al; 1033 } else { 1034 /* do we rather want to try later? */ 1035 if (device->resync_locked > device->resync->nr_elements-3) 1036 goto try_again; 1037 /* Do or do not. There is no try. -- Yoda */ 1038 e = lc_get(device->resync, enr); 1039 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; 1040 if (!bm_ext) { 1041 const unsigned long rs_flags = device->resync->flags; 1042 if (rs_flags & LC_STARVING) 1043 drbd_warn(device, "Have to wait for element" 1044 " (resync LRU too small?)\n"); 1045 BUG_ON(rs_flags & LC_LOCKED); 1046 goto try_again; 1047 } 1048 if (bm_ext->lce.lc_number != enr) { 1049 bm_ext->rs_left = drbd_bm_e_weight(device, enr); 1050 bm_ext->rs_failed = 0; 1051 lc_committed(device->resync); 1052 wake_up(&device->al_wait); 1053 D_ASSERT(device, test_bit(BME_LOCKED, &bm_ext->flags) == 0); 1054 } 1055 set_bit(BME_NO_WRITES, &bm_ext->flags); 1056 D_ASSERT(device, bm_ext->lce.refcnt == 1); 1057 device->resync_locked++; 1058 goto check_al; 1059 } 1060 check_al: 1061 for (i = 0; i < AL_EXT_PER_BM_SECT; i++) { 1062 if (lc_is_used(device->act_log, al_enr+i)) 1063 goto try_again; 1064 } 1065 set_bit(BME_LOCKED, &bm_ext->flags); 1066 proceed: 1067 device->resync_wenr = LC_FREE; 1068 spin_unlock_irq(&device->al_lock); 1069 return 0; 1070 1071 try_again: 1072 if (bm_ext) 1073 device->resync_wenr = enr; 1074 spin_unlock_irq(&device->al_lock); 1075 return -EAGAIN; 1076 } 1077 1078 void drbd_rs_complete_io(struct drbd_device *device, sector_t sector) 1079 { 1080 unsigned int enr = BM_SECT_TO_EXT(sector); 1081 struct lc_element *e; 1082 struct bm_extent *bm_ext; 1083 unsigned long flags; 1084 1085 spin_lock_irqsave(&device->al_lock, flags); 1086 e = lc_find(device->resync, enr); 1087 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; 1088 if (!bm_ext) { 1089 spin_unlock_irqrestore(&device->al_lock, flags); 1090 if (__ratelimit(&drbd_ratelimit_state)) 1091 drbd_err(device, "drbd_rs_complete_io() called, but extent not found\n"); 1092 return; 1093 } 1094 1095 if (bm_ext->lce.refcnt == 0) { 1096 spin_unlock_irqrestore(&device->al_lock, flags); 1097 drbd_err(device, "drbd_rs_complete_io(,%llu [=%u]) called, " 1098 "but refcnt is 0!?\n", 1099 (unsigned long long)sector, enr); 1100 return; 1101 } 1102 1103 if (lc_put(device->resync, &bm_ext->lce) == 0) { 1104 bm_ext->flags = 0; /* clear BME_LOCKED, BME_NO_WRITES and BME_PRIORITY */ 1105 device->resync_locked--; 1106 wake_up(&device->al_wait); 1107 } 1108 1109 spin_unlock_irqrestore(&device->al_lock, flags); 1110 } 1111 1112 /** 1113 * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED) 1114 * @device: DRBD device. 1115 */ 1116 void drbd_rs_cancel_all(struct drbd_device *device) 1117 { 1118 spin_lock_irq(&device->al_lock); 1119 1120 if (get_ldev_if_state(device, D_FAILED)) { /* Makes sure ->resync is there. */ 1121 lc_reset(device->resync); 1122 put_ldev(device); 1123 } 1124 device->resync_locked = 0; 1125 device->resync_wenr = LC_FREE; 1126 spin_unlock_irq(&device->al_lock); 1127 wake_up(&device->al_wait); 1128 } 1129 1130 /** 1131 * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU 1132 * @device: DRBD device. 1133 * 1134 * Returns 0 upon success, -EAGAIN if at least one reference count was 1135 * not zero. 1136 */ 1137 int drbd_rs_del_all(struct drbd_device *device) 1138 { 1139 struct lc_element *e; 1140 struct bm_extent *bm_ext; 1141 int i; 1142 1143 spin_lock_irq(&device->al_lock); 1144 1145 if (get_ldev_if_state(device, D_FAILED)) { 1146 /* ok, ->resync is there. */ 1147 for (i = 0; i < device->resync->nr_elements; i++) { 1148 e = lc_element_by_index(device->resync, i); 1149 bm_ext = lc_entry(e, struct bm_extent, lce); 1150 if (bm_ext->lce.lc_number == LC_FREE) 1151 continue; 1152 if (bm_ext->lce.lc_number == device->resync_wenr) { 1153 drbd_info(device, "dropping %u in drbd_rs_del_all, apparently" 1154 " got 'synced' by application io\n", 1155 device->resync_wenr); 1156 D_ASSERT(device, !test_bit(BME_LOCKED, &bm_ext->flags)); 1157 D_ASSERT(device, test_bit(BME_NO_WRITES, &bm_ext->flags)); 1158 clear_bit(BME_NO_WRITES, &bm_ext->flags); 1159 device->resync_wenr = LC_FREE; 1160 lc_put(device->resync, &bm_ext->lce); 1161 } 1162 if (bm_ext->lce.refcnt != 0) { 1163 drbd_info(device, "Retrying drbd_rs_del_all() later. " 1164 "refcnt=%d\n", bm_ext->lce.refcnt); 1165 put_ldev(device); 1166 spin_unlock_irq(&device->al_lock); 1167 return -EAGAIN; 1168 } 1169 D_ASSERT(device, !test_bit(BME_LOCKED, &bm_ext->flags)); 1170 D_ASSERT(device, !test_bit(BME_NO_WRITES, &bm_ext->flags)); 1171 lc_del(device->resync, &bm_ext->lce); 1172 } 1173 D_ASSERT(device, device->resync->used == 0); 1174 put_ldev(device); 1175 } 1176 spin_unlock_irq(&device->al_lock); 1177 wake_up(&device->al_wait); 1178 1179 return 0; 1180 } 1181