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