.. | .. |
---|
9 | 9 | #include <linux/blkdev.h> |
---|
10 | 10 | #include <linux/kthread.h> |
---|
11 | 11 | #include <linux/math64.h> |
---|
| 12 | +#include "misc.h" |
---|
12 | 13 | #include "ctree.h" |
---|
13 | 14 | #include "extent_map.h" |
---|
14 | 15 | #include "disk-io.h" |
---|
.. | .. |
---|
21 | 22 | #include "dev-replace.h" |
---|
22 | 23 | #include "sysfs.h" |
---|
23 | 24 | |
---|
| 25 | +/* |
---|
| 26 | + * Device replace overview |
---|
| 27 | + * |
---|
| 28 | + * [Objective] |
---|
| 29 | + * To copy all extents (both new and on-disk) from source device to target |
---|
| 30 | + * device, while still keeping the filesystem read-write. |
---|
| 31 | + * |
---|
| 32 | + * [Method] |
---|
| 33 | + * There are two main methods involved: |
---|
| 34 | + * |
---|
| 35 | + * - Write duplication |
---|
| 36 | + * |
---|
| 37 | + * All new writes will be written to both target and source devices, so even |
---|
| 38 | + * if replace gets canceled, sources device still contans up-to-date data. |
---|
| 39 | + * |
---|
| 40 | + * Location: handle_ops_on_dev_replace() from __btrfs_map_block() |
---|
| 41 | + * Start: btrfs_dev_replace_start() |
---|
| 42 | + * End: btrfs_dev_replace_finishing() |
---|
| 43 | + * Content: Latest data/metadata |
---|
| 44 | + * |
---|
| 45 | + * - Copy existing extents |
---|
| 46 | + * |
---|
| 47 | + * This happens by re-using scrub facility, as scrub also iterates through |
---|
| 48 | + * existing extents from commit root. |
---|
| 49 | + * |
---|
| 50 | + * Location: scrub_write_block_to_dev_replace() from |
---|
| 51 | + * scrub_block_complete() |
---|
| 52 | + * Content: Data/meta from commit root. |
---|
| 53 | + * |
---|
| 54 | + * Due to the content difference, we need to avoid nocow write when dev-replace |
---|
| 55 | + * is happening. This is done by marking the block group read-only and waiting |
---|
| 56 | + * for NOCOW writes. |
---|
| 57 | + * |
---|
| 58 | + * After replace is done, the finishing part is done by swapping the target and |
---|
| 59 | + * source devices. |
---|
| 60 | + * |
---|
| 61 | + * Location: btrfs_dev_replace_update_device_in_mapping_tree() from |
---|
| 62 | + * btrfs_dev_replace_finishing() |
---|
| 63 | + */ |
---|
| 64 | + |
---|
24 | 65 | static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info, |
---|
25 | 66 | int scrub_ret); |
---|
26 | | -static void btrfs_dev_replace_update_device_in_mapping_tree( |
---|
27 | | - struct btrfs_fs_info *fs_info, |
---|
28 | | - struct btrfs_device *srcdev, |
---|
29 | | - struct btrfs_device *tgtdev); |
---|
30 | 67 | static int btrfs_dev_replace_kthread(void *data); |
---|
31 | 68 | |
---|
32 | 69 | int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info) |
---|
.. | .. |
---|
67 | 104 | } |
---|
68 | 105 | ret = 0; |
---|
69 | 106 | dev_replace->replace_state = |
---|
70 | | - BTRFS_DEV_REPLACE_ITEM_STATE_NEVER_STARTED; |
---|
| 107 | + BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED; |
---|
71 | 108 | dev_replace->cont_reading_from_srcdev_mode = |
---|
72 | 109 | BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_ALWAYS; |
---|
73 | | - dev_replace->replace_state = 0; |
---|
74 | 110 | dev_replace->time_started = 0; |
---|
75 | 111 | dev_replace->time_stopped = 0; |
---|
76 | 112 | atomic64_set(&dev_replace->num_write_errors, 0); |
---|
.. | .. |
---|
125 | 161 | if (btrfs_find_device(fs_info->fs_devices, |
---|
126 | 162 | BTRFS_DEV_REPLACE_DEVID, NULL, NULL, false)) { |
---|
127 | 163 | btrfs_err(fs_info, |
---|
128 | | - "replace devid present without an active replace item"); |
---|
| 164 | +"replace without active item, run 'device scan --forget' on the target device"); |
---|
129 | 165 | ret = -EUCLEAN; |
---|
130 | 166 | } else { |
---|
131 | 167 | dev_replace->srcdev = NULL; |
---|
.. | .. |
---|
206 | 242 | { |
---|
207 | 243 | struct btrfs_device *device; |
---|
208 | 244 | struct block_device *bdev; |
---|
209 | | - struct list_head *devices; |
---|
210 | 245 | struct rcu_string *name; |
---|
211 | 246 | u64 devid = BTRFS_DEV_REPLACE_DEVID; |
---|
212 | 247 | int ret = 0; |
---|
.. | .. |
---|
224 | 259 | return PTR_ERR(bdev); |
---|
225 | 260 | } |
---|
226 | 261 | |
---|
227 | | - filemap_write_and_wait(bdev->bd_inode->i_mapping); |
---|
| 262 | + sync_blockdev(bdev); |
---|
228 | 263 | |
---|
229 | | - devices = &fs_info->fs_devices->devices; |
---|
230 | | - list_for_each_entry(device, devices, dev_list) { |
---|
| 264 | + list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) { |
---|
231 | 265 | if (device->bdev == bdev) { |
---|
232 | 266 | btrfs_err(fs_info, |
---|
233 | 267 | "target device is in the filesystem!"); |
---|
.. | .. |
---|
260 | 294 | } |
---|
261 | 295 | rcu_assign_pointer(device->name, name); |
---|
262 | 296 | |
---|
263 | | - mutex_lock(&fs_info->fs_devices->device_list_mutex); |
---|
264 | 297 | set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
---|
265 | 298 | device->generation = 0; |
---|
266 | 299 | device->io_width = fs_info->sectorsize; |
---|
.. | .. |
---|
279 | 312 | device->dev_stats_valid = 1; |
---|
280 | 313 | set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE); |
---|
281 | 314 | device->fs_devices = fs_info->fs_devices; |
---|
| 315 | + |
---|
| 316 | + mutex_lock(&fs_info->fs_devices->device_list_mutex); |
---|
282 | 317 | list_add(&device->dev_list, &fs_info->fs_devices->devices); |
---|
283 | 318 | fs_info->fs_devices->num_devices++; |
---|
284 | 319 | fs_info->fs_devices->open_devices++; |
---|
.. | .. |
---|
296 | 331 | * called from commit_transaction. Writes changed device replace state to |
---|
297 | 332 | * disk. |
---|
298 | 333 | */ |
---|
299 | | -int btrfs_run_dev_replace(struct btrfs_trans_handle *trans, |
---|
300 | | - struct btrfs_fs_info *fs_info) |
---|
| 334 | +int btrfs_run_dev_replace(struct btrfs_trans_handle *trans) |
---|
301 | 335 | { |
---|
| 336 | + struct btrfs_fs_info *fs_info = trans->fs_info; |
---|
302 | 337 | int ret; |
---|
303 | 338 | struct btrfs_root *dev_root = fs_info->dev_root; |
---|
304 | 339 | struct btrfs_path *path; |
---|
.. | .. |
---|
307 | 342 | struct btrfs_dev_replace_item *ptr; |
---|
308 | 343 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
---|
309 | 344 | |
---|
310 | | - btrfs_dev_replace_read_lock(dev_replace); |
---|
| 345 | + down_read(&dev_replace->rwsem); |
---|
311 | 346 | if (!dev_replace->is_valid || |
---|
312 | 347 | !dev_replace->item_needs_writeback) { |
---|
313 | | - btrfs_dev_replace_read_unlock(dev_replace); |
---|
| 348 | + up_read(&dev_replace->rwsem); |
---|
314 | 349 | return 0; |
---|
315 | 350 | } |
---|
316 | | - btrfs_dev_replace_read_unlock(dev_replace); |
---|
| 351 | + up_read(&dev_replace->rwsem); |
---|
317 | 352 | |
---|
318 | 353 | key.objectid = 0; |
---|
319 | 354 | key.type = BTRFS_DEV_REPLACE_KEY; |
---|
.. | .. |
---|
371 | 406 | ptr = btrfs_item_ptr(eb, path->slots[0], |
---|
372 | 407 | struct btrfs_dev_replace_item); |
---|
373 | 408 | |
---|
374 | | - btrfs_dev_replace_write_lock(dev_replace); |
---|
| 409 | + down_write(&dev_replace->rwsem); |
---|
375 | 410 | if (dev_replace->srcdev) |
---|
376 | 411 | btrfs_set_dev_replace_src_devid(eb, ptr, |
---|
377 | 412 | dev_replace->srcdev->devid); |
---|
.. | .. |
---|
394 | 429 | btrfs_set_dev_replace_cursor_right(eb, ptr, |
---|
395 | 430 | dev_replace->cursor_right); |
---|
396 | 431 | dev_replace->item_needs_writeback = 0; |
---|
397 | | - btrfs_dev_replace_write_unlock(dev_replace); |
---|
| 432 | + up_write(&dev_replace->rwsem); |
---|
398 | 433 | |
---|
399 | 434 | btrfs_mark_buffer_dirty(eb); |
---|
400 | 435 | |
---|
.. | .. |
---|
402 | 437 | btrfs_free_path(path); |
---|
403 | 438 | |
---|
404 | 439 | return ret; |
---|
405 | | -} |
---|
406 | | - |
---|
407 | | -void btrfs_after_dev_replace_commit(struct btrfs_fs_info *fs_info) |
---|
408 | | -{ |
---|
409 | | - struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
---|
410 | | - |
---|
411 | | - dev_replace->committed_cursor_left = |
---|
412 | | - dev_replace->cursor_left_last_write_of_item; |
---|
413 | 440 | } |
---|
414 | 441 | |
---|
415 | 442 | static char* btrfs_dev_name(struct btrfs_device *device) |
---|
.. | .. |
---|
420 | 447 | return rcu_str_deref(device->name); |
---|
421 | 448 | } |
---|
422 | 449 | |
---|
423 | | -int btrfs_dev_replace_start(struct btrfs_fs_info *fs_info, |
---|
| 450 | +static int btrfs_dev_replace_start(struct btrfs_fs_info *fs_info, |
---|
424 | 451 | const char *tgtdev_name, u64 srcdevid, const char *srcdev_name, |
---|
425 | 452 | int read_src) |
---|
426 | 453 | { |
---|
.. | .. |
---|
431 | 458 | struct btrfs_device *tgt_device = NULL; |
---|
432 | 459 | struct btrfs_device *src_device = NULL; |
---|
433 | 460 | |
---|
434 | | - ret = btrfs_find_device_by_devspec(fs_info, srcdevid, |
---|
435 | | - srcdev_name, &src_device); |
---|
436 | | - if (ret) |
---|
437 | | - return ret; |
---|
| 461 | + src_device = btrfs_find_device_by_devspec(fs_info, srcdevid, |
---|
| 462 | + srcdev_name); |
---|
| 463 | + if (IS_ERR(src_device)) |
---|
| 464 | + return PTR_ERR(src_device); |
---|
438 | 465 | |
---|
439 | | - ret = btrfs_init_dev_replace_tgtdev(fs_info, tgtdev_name, |
---|
440 | | - src_device, &tgt_device); |
---|
441 | | - if (ret) |
---|
442 | | - return ret; |
---|
| 466 | + if (btrfs_pinned_by_swapfile(fs_info, src_device)) { |
---|
| 467 | + btrfs_warn_in_rcu(fs_info, |
---|
| 468 | + "cannot replace device %s (devid %llu) due to active swapfile", |
---|
| 469 | + btrfs_dev_name(src_device), src_device->devid); |
---|
| 470 | + return -ETXTBSY; |
---|
| 471 | + } |
---|
443 | 472 | |
---|
444 | 473 | /* |
---|
445 | 474 | * Here we commit the transaction to make sure commit_total_bytes |
---|
.. | .. |
---|
454 | 483 | return PTR_ERR(trans); |
---|
455 | 484 | } |
---|
456 | 485 | |
---|
457 | | - btrfs_dev_replace_write_lock(dev_replace); |
---|
| 486 | + ret = btrfs_init_dev_replace_tgtdev(fs_info, tgtdev_name, |
---|
| 487 | + src_device, &tgt_device); |
---|
| 488 | + if (ret) |
---|
| 489 | + return ret; |
---|
| 490 | + |
---|
| 491 | + down_write(&dev_replace->rwsem); |
---|
458 | 492 | switch (dev_replace->replace_state) { |
---|
459 | 493 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED: |
---|
460 | 494 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED: |
---|
.. | .. |
---|
464 | 498 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED: |
---|
465 | 499 | ASSERT(0); |
---|
466 | 500 | ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_ALREADY_STARTED; |
---|
| 501 | + up_write(&dev_replace->rwsem); |
---|
467 | 502 | goto leave; |
---|
468 | 503 | } |
---|
469 | 504 | |
---|
470 | 505 | dev_replace->cont_reading_from_srcdev_mode = read_src; |
---|
471 | | - WARN_ON(!src_device); |
---|
472 | 506 | dev_replace->srcdev = src_device; |
---|
473 | 507 | dev_replace->tgtdev = tgt_device; |
---|
474 | 508 | |
---|
.. | .. |
---|
492 | 526 | dev_replace->item_needs_writeback = 1; |
---|
493 | 527 | atomic64_set(&dev_replace->num_write_errors, 0); |
---|
494 | 528 | atomic64_set(&dev_replace->num_uncorrectable_read_errors, 0); |
---|
495 | | - btrfs_dev_replace_write_unlock(dev_replace); |
---|
| 529 | + up_write(&dev_replace->rwsem); |
---|
496 | 530 | |
---|
497 | | - ret = btrfs_sysfs_add_device_link(tgt_device->fs_devices, tgt_device); |
---|
| 531 | + ret = btrfs_sysfs_add_device(tgt_device); |
---|
498 | 532 | if (ret) |
---|
499 | 533 | btrfs_err(fs_info, "kobj add dev failed %d", ret); |
---|
500 | 534 | |
---|
501 | 535 | btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); |
---|
502 | 536 | |
---|
503 | | - /* force writing the updated state information to disk */ |
---|
504 | | - trans = btrfs_start_transaction(root, 0); |
---|
| 537 | + /* Commit dev_replace state and reserve 1 item for it. */ |
---|
| 538 | + trans = btrfs_start_transaction(root, 1); |
---|
505 | 539 | if (IS_ERR(trans)) { |
---|
506 | 540 | ret = PTR_ERR(trans); |
---|
507 | | - btrfs_dev_replace_write_lock(dev_replace); |
---|
| 541 | + down_write(&dev_replace->rwsem); |
---|
508 | 542 | dev_replace->replace_state = |
---|
509 | 543 | BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED; |
---|
510 | 544 | dev_replace->srcdev = NULL; |
---|
511 | 545 | dev_replace->tgtdev = NULL; |
---|
| 546 | + up_write(&dev_replace->rwsem); |
---|
512 | 547 | goto leave; |
---|
513 | 548 | } |
---|
514 | 549 | |
---|
.. | .. |
---|
521 | 556 | &dev_replace->scrub_progress, 0, 1); |
---|
522 | 557 | |
---|
523 | 558 | ret = btrfs_dev_replace_finishing(fs_info, ret); |
---|
524 | | - if (ret == -EINPROGRESS) { |
---|
| 559 | + if (ret == -EINPROGRESS) |
---|
525 | 560 | ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS; |
---|
526 | | - } else { |
---|
527 | | - WARN_ON(ret); |
---|
528 | | - } |
---|
529 | 561 | |
---|
530 | 562 | return ret; |
---|
531 | 563 | |
---|
532 | 564 | leave: |
---|
533 | | - btrfs_dev_replace_write_unlock(dev_replace); |
---|
534 | 565 | btrfs_destroy_dev_replace_tgtdev(tgt_device); |
---|
535 | 566 | return ret; |
---|
536 | 567 | } |
---|
.. | .. |
---|
558 | 589 | args->start.cont_reading_from_srcdev_mode); |
---|
559 | 590 | args->result = ret; |
---|
560 | 591 | /* don't warn if EINPROGRESS, someone else might be running scrub */ |
---|
561 | | - if (ret == BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS) |
---|
562 | | - ret = 0; |
---|
| 592 | + if (ret == BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS || |
---|
| 593 | + ret == BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR) |
---|
| 594 | + return 0; |
---|
563 | 595 | |
---|
564 | 596 | return ret; |
---|
565 | 597 | } |
---|
.. | .. |
---|
570 | 602 | static void btrfs_rm_dev_replace_blocked(struct btrfs_fs_info *fs_info) |
---|
571 | 603 | { |
---|
572 | 604 | set_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state); |
---|
573 | | - wait_event(fs_info->replace_wait, !percpu_counter_sum( |
---|
574 | | - &fs_info->bio_counter)); |
---|
| 605 | + wait_event(fs_info->dev_replace.replace_wait, !percpu_counter_sum( |
---|
| 606 | + &fs_info->dev_replace.bio_counter)); |
---|
575 | 607 | } |
---|
576 | 608 | |
---|
577 | 609 | /* |
---|
.. | .. |
---|
580 | 612 | static void btrfs_rm_dev_replace_unblocked(struct btrfs_fs_info *fs_info) |
---|
581 | 613 | { |
---|
582 | 614 | clear_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state); |
---|
583 | | - wake_up(&fs_info->replace_wait); |
---|
| 615 | + wake_up(&fs_info->dev_replace.replace_wait); |
---|
| 616 | +} |
---|
| 617 | + |
---|
| 618 | +/* |
---|
| 619 | + * When finishing the device replace, before swapping the source device with the |
---|
| 620 | + * target device we must update the chunk allocation state in the target device, |
---|
| 621 | + * as it is empty because replace works by directly copying the chunks and not |
---|
| 622 | + * through the normal chunk allocation path. |
---|
| 623 | + */ |
---|
| 624 | +static int btrfs_set_target_alloc_state(struct btrfs_device *srcdev, |
---|
| 625 | + struct btrfs_device *tgtdev) |
---|
| 626 | +{ |
---|
| 627 | + struct extent_state *cached_state = NULL; |
---|
| 628 | + u64 start = 0; |
---|
| 629 | + u64 found_start; |
---|
| 630 | + u64 found_end; |
---|
| 631 | + int ret = 0; |
---|
| 632 | + |
---|
| 633 | + lockdep_assert_held(&srcdev->fs_info->chunk_mutex); |
---|
| 634 | + |
---|
| 635 | + while (!find_first_extent_bit(&srcdev->alloc_state, start, |
---|
| 636 | + &found_start, &found_end, |
---|
| 637 | + CHUNK_ALLOCATED, &cached_state)) { |
---|
| 638 | + ret = set_extent_bits(&tgtdev->alloc_state, found_start, |
---|
| 639 | + found_end, CHUNK_ALLOCATED); |
---|
| 640 | + if (ret) |
---|
| 641 | + break; |
---|
| 642 | + start = found_end + 1; |
---|
| 643 | + } |
---|
| 644 | + |
---|
| 645 | + free_extent_state(cached_state); |
---|
| 646 | + return ret; |
---|
| 647 | +} |
---|
| 648 | + |
---|
| 649 | +static void btrfs_dev_replace_update_device_in_mapping_tree( |
---|
| 650 | + struct btrfs_fs_info *fs_info, |
---|
| 651 | + struct btrfs_device *srcdev, |
---|
| 652 | + struct btrfs_device *tgtdev) |
---|
| 653 | +{ |
---|
| 654 | + struct extent_map_tree *em_tree = &fs_info->mapping_tree; |
---|
| 655 | + struct extent_map *em; |
---|
| 656 | + struct map_lookup *map; |
---|
| 657 | + u64 start = 0; |
---|
| 658 | + int i; |
---|
| 659 | + |
---|
| 660 | + write_lock(&em_tree->lock); |
---|
| 661 | + do { |
---|
| 662 | + em = lookup_extent_mapping(em_tree, start, (u64)-1); |
---|
| 663 | + if (!em) |
---|
| 664 | + break; |
---|
| 665 | + map = em->map_lookup; |
---|
| 666 | + for (i = 0; i < map->num_stripes; i++) |
---|
| 667 | + if (srcdev == map->stripes[i].dev) |
---|
| 668 | + map->stripes[i].dev = tgtdev; |
---|
| 669 | + start = em->start + em->len; |
---|
| 670 | + free_extent_map(em); |
---|
| 671 | + } while (start); |
---|
| 672 | + write_unlock(&em_tree->lock); |
---|
584 | 673 | } |
---|
585 | 674 | |
---|
586 | 675 | static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info, |
---|
.. | .. |
---|
597 | 686 | /* don't allow cancel or unmount to disturb the finishing procedure */ |
---|
598 | 687 | mutex_lock(&dev_replace->lock_finishing_cancel_unmount); |
---|
599 | 688 | |
---|
600 | | - btrfs_dev_replace_read_lock(dev_replace); |
---|
| 689 | + down_read(&dev_replace->rwsem); |
---|
601 | 690 | /* was the operation canceled, or is it finished? */ |
---|
602 | 691 | if (dev_replace->replace_state != |
---|
603 | 692 | BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED) { |
---|
604 | | - btrfs_dev_replace_read_unlock(dev_replace); |
---|
| 693 | + up_read(&dev_replace->rwsem); |
---|
605 | 694 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); |
---|
606 | 695 | return 0; |
---|
607 | 696 | } |
---|
608 | 697 | |
---|
609 | 698 | tgt_device = dev_replace->tgtdev; |
---|
610 | 699 | src_device = dev_replace->srcdev; |
---|
611 | | - btrfs_dev_replace_read_unlock(dev_replace); |
---|
| 700 | + up_read(&dev_replace->rwsem); |
---|
612 | 701 | |
---|
613 | 702 | /* |
---|
614 | 703 | * flush all outstanding I/O and inode extent mappings before the |
---|
615 | 704 | * copy operation is declared as being finished |
---|
616 | 705 | */ |
---|
617 | | - ret = btrfs_start_delalloc_roots(fs_info, -1); |
---|
| 706 | + ret = btrfs_start_delalloc_roots(fs_info, U64_MAX, false); |
---|
618 | 707 | if (ret) { |
---|
619 | 708 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); |
---|
620 | 709 | return ret; |
---|
621 | 710 | } |
---|
622 | 711 | btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); |
---|
623 | 712 | |
---|
| 713 | + if (!scrub_ret) |
---|
| 714 | + btrfs_reada_remove_dev(src_device); |
---|
| 715 | + |
---|
| 716 | + /* |
---|
| 717 | + * We have to use this loop approach because at this point src_device |
---|
| 718 | + * has to be available for transaction commit to complete, yet new |
---|
| 719 | + * chunks shouldn't be allocated on the device. |
---|
| 720 | + */ |
---|
624 | 721 | while (1) { |
---|
625 | 722 | trans = btrfs_start_transaction(root, 0); |
---|
626 | 723 | if (IS_ERR(trans)) { |
---|
| 724 | + btrfs_reada_undo_remove_dev(src_device); |
---|
627 | 725 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); |
---|
628 | 726 | return PTR_ERR(trans); |
---|
629 | 727 | } |
---|
630 | 728 | ret = btrfs_commit_transaction(trans); |
---|
631 | 729 | WARN_ON(ret); |
---|
632 | | - /* keep away write_all_supers() during the finishing procedure */ |
---|
| 730 | + |
---|
| 731 | + /* Prevent write_all_supers() during the finishing procedure */ |
---|
633 | 732 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
---|
| 733 | + /* Prevent new chunks being allocated on the source device */ |
---|
634 | 734 | mutex_lock(&fs_info->chunk_mutex); |
---|
635 | | - if (src_device->has_pending_chunks) { |
---|
636 | | - mutex_unlock(&root->fs_info->chunk_mutex); |
---|
637 | | - mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
---|
| 735 | + |
---|
| 736 | + if (!list_empty(&src_device->post_commit_list)) { |
---|
| 737 | + mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
---|
| 738 | + mutex_unlock(&fs_info->chunk_mutex); |
---|
638 | 739 | } else { |
---|
639 | 740 | break; |
---|
640 | 741 | } |
---|
641 | 742 | } |
---|
642 | 743 | |
---|
643 | | - btrfs_dev_replace_write_lock(dev_replace); |
---|
| 744 | + down_write(&dev_replace->rwsem); |
---|
644 | 745 | dev_replace->replace_state = |
---|
645 | 746 | scrub_ret ? BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED |
---|
646 | 747 | : BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED; |
---|
.. | .. |
---|
649 | 750 | dev_replace->time_stopped = ktime_get_real_seconds(); |
---|
650 | 751 | dev_replace->item_needs_writeback = 1; |
---|
651 | 752 | |
---|
652 | | - /* replace old device with new one in mapping tree */ |
---|
| 753 | + /* |
---|
| 754 | + * Update allocation state in the new device and replace the old device |
---|
| 755 | + * with the new one in the mapping tree. |
---|
| 756 | + */ |
---|
653 | 757 | if (!scrub_ret) { |
---|
| 758 | + scrub_ret = btrfs_set_target_alloc_state(src_device, tgt_device); |
---|
| 759 | + if (scrub_ret) |
---|
| 760 | + goto error; |
---|
654 | 761 | btrfs_dev_replace_update_device_in_mapping_tree(fs_info, |
---|
655 | 762 | src_device, |
---|
656 | 763 | tgt_device); |
---|
657 | 764 | } else { |
---|
658 | | - btrfs_err_in_rcu(fs_info, |
---|
| 765 | + if (scrub_ret != -ECANCELED) |
---|
| 766 | + btrfs_err_in_rcu(fs_info, |
---|
659 | 767 | "btrfs_scrub_dev(%s, %llu, %s) failed %d", |
---|
660 | 768 | btrfs_dev_name(src_device), |
---|
661 | 769 | src_device->devid, |
---|
662 | 770 | rcu_str_deref(tgt_device->name), scrub_ret); |
---|
663 | | - btrfs_dev_replace_write_unlock(dev_replace); |
---|
| 771 | +error: |
---|
| 772 | + up_write(&dev_replace->rwsem); |
---|
664 | 773 | mutex_unlock(&fs_info->chunk_mutex); |
---|
665 | 774 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
---|
| 775 | + btrfs_reada_undo_remove_dev(src_device); |
---|
666 | 776 | btrfs_rm_dev_replace_blocked(fs_info); |
---|
667 | 777 | if (tgt_device) |
---|
668 | 778 | btrfs_destroy_dev_replace_tgtdev(tgt_device); |
---|
.. | .. |
---|
687 | 797 | btrfs_device_set_disk_total_bytes(tgt_device, |
---|
688 | 798 | src_device->disk_total_bytes); |
---|
689 | 799 | btrfs_device_set_bytes_used(tgt_device, src_device->bytes_used); |
---|
690 | | - ASSERT(list_empty(&src_device->resized_list)); |
---|
691 | | - tgt_device->commit_total_bytes = src_device->commit_total_bytes; |
---|
692 | 800 | tgt_device->commit_bytes_used = src_device->bytes_used; |
---|
693 | 801 | |
---|
694 | 802 | btrfs_assign_next_active_device(src_device, tgt_device); |
---|
.. | .. |
---|
696 | 804 | list_add(&tgt_device->dev_alloc_list, &fs_info->fs_devices->alloc_list); |
---|
697 | 805 | fs_info->fs_devices->rw_devices++; |
---|
698 | 806 | |
---|
699 | | - btrfs_dev_replace_write_unlock(dev_replace); |
---|
700 | | - |
---|
| 807 | + up_write(&dev_replace->rwsem); |
---|
701 | 808 | btrfs_rm_dev_replace_blocked(fs_info); |
---|
702 | 809 | |
---|
703 | 810 | btrfs_rm_dev_replace_remove_srcdev(src_device); |
---|
.. | .. |
---|
721 | 828 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
---|
722 | 829 | |
---|
723 | 830 | /* replace the sysfs entry */ |
---|
724 | | - btrfs_sysfs_rm_device_link(fs_info->fs_devices, src_device); |
---|
725 | | - btrfs_rm_dev_replace_free_srcdev(fs_info, src_device); |
---|
| 831 | + btrfs_sysfs_remove_device(src_device); |
---|
| 832 | + btrfs_sysfs_update_devid(tgt_device); |
---|
| 833 | + if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &src_device->dev_state)) |
---|
| 834 | + btrfs_scratch_superblocks(fs_info, src_device->bdev, |
---|
| 835 | + src_device->name->str); |
---|
726 | 836 | |
---|
727 | 837 | /* write back the superblocks */ |
---|
728 | 838 | trans = btrfs_start_transaction(root, 0); |
---|
.. | .. |
---|
731 | 841 | |
---|
732 | 842 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); |
---|
733 | 843 | |
---|
| 844 | + btrfs_rm_dev_replace_free_srcdev(src_device); |
---|
| 845 | + |
---|
734 | 846 | return 0; |
---|
735 | | -} |
---|
736 | | - |
---|
737 | | -static void btrfs_dev_replace_update_device_in_mapping_tree( |
---|
738 | | - struct btrfs_fs_info *fs_info, |
---|
739 | | - struct btrfs_device *srcdev, |
---|
740 | | - struct btrfs_device *tgtdev) |
---|
741 | | -{ |
---|
742 | | - struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree; |
---|
743 | | - struct extent_map *em; |
---|
744 | | - struct map_lookup *map; |
---|
745 | | - u64 start = 0; |
---|
746 | | - int i; |
---|
747 | | - |
---|
748 | | - write_lock(&em_tree->lock); |
---|
749 | | - do { |
---|
750 | | - em = lookup_extent_mapping(em_tree, start, (u64)-1); |
---|
751 | | - if (!em) |
---|
752 | | - break; |
---|
753 | | - map = em->map_lookup; |
---|
754 | | - for (i = 0; i < map->num_stripes; i++) |
---|
755 | | - if (srcdev == map->stripes[i].dev) |
---|
756 | | - map->stripes[i].dev = tgtdev; |
---|
757 | | - start = em->start + em->len; |
---|
758 | | - free_extent_map(em); |
---|
759 | | - } while (start); |
---|
760 | | - write_unlock(&em_tree->lock); |
---|
761 | 847 | } |
---|
762 | 848 | |
---|
763 | 849 | /* |
---|
.. | .. |
---|
794 | 880 | { |
---|
795 | 881 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
---|
796 | 882 | |
---|
797 | | - btrfs_dev_replace_read_lock(dev_replace); |
---|
| 883 | + down_read(&dev_replace->rwsem); |
---|
798 | 884 | /* even if !dev_replace_is_valid, the values are good enough for |
---|
799 | 885 | * the replace_status ioctl */ |
---|
800 | 886 | args->result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR; |
---|
.. | .. |
---|
806 | 892 | args->status.num_uncorrectable_read_errors = |
---|
807 | 893 | atomic64_read(&dev_replace->num_uncorrectable_read_errors); |
---|
808 | 894 | args->status.progress_1000 = btrfs_dev_replace_progress(fs_info); |
---|
809 | | - btrfs_dev_replace_read_unlock(dev_replace); |
---|
| 895 | + up_read(&dev_replace->rwsem); |
---|
810 | 896 | } |
---|
811 | 897 | |
---|
812 | 898 | int btrfs_dev_replace_cancel(struct btrfs_fs_info *fs_info) |
---|
.. | .. |
---|
823 | 909 | return -EROFS; |
---|
824 | 910 | |
---|
825 | 911 | mutex_lock(&dev_replace->lock_finishing_cancel_unmount); |
---|
826 | | - btrfs_dev_replace_write_lock(dev_replace); |
---|
| 912 | + down_write(&dev_replace->rwsem); |
---|
827 | 913 | switch (dev_replace->replace_state) { |
---|
828 | 914 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED: |
---|
829 | 915 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED: |
---|
830 | 916 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED: |
---|
831 | 917 | result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED; |
---|
832 | | - btrfs_dev_replace_write_unlock(dev_replace); |
---|
| 918 | + up_write(&dev_replace->rwsem); |
---|
833 | 919 | break; |
---|
834 | 920 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED: |
---|
835 | 921 | tgt_device = dev_replace->tgtdev; |
---|
836 | 922 | src_device = dev_replace->srcdev; |
---|
837 | | - btrfs_dev_replace_write_unlock(dev_replace); |
---|
| 923 | + up_write(&dev_replace->rwsem); |
---|
838 | 924 | ret = btrfs_scrub_cancel(fs_info); |
---|
839 | 925 | if (ret < 0) { |
---|
840 | 926 | result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED; |
---|
.. | .. |
---|
865 | 951 | dev_replace->time_stopped = ktime_get_real_seconds(); |
---|
866 | 952 | dev_replace->item_needs_writeback = 1; |
---|
867 | 953 | |
---|
868 | | - btrfs_dev_replace_write_unlock(dev_replace); |
---|
| 954 | + up_write(&dev_replace->rwsem); |
---|
869 | 955 | |
---|
| 956 | + /* Scrub for replace must not be running in suspended state */ |
---|
870 | 957 | btrfs_scrub_cancel(fs_info); |
---|
871 | 958 | |
---|
872 | 959 | trans = btrfs_start_transaction(root, 0); |
---|
.. | .. |
---|
886 | 973 | btrfs_destroy_dev_replace_tgtdev(tgt_device); |
---|
887 | 974 | break; |
---|
888 | 975 | default: |
---|
| 976 | + up_write(&dev_replace->rwsem); |
---|
889 | 977 | result = -EINVAL; |
---|
890 | 978 | } |
---|
891 | 979 | |
---|
.. | .. |
---|
898 | 986 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
---|
899 | 987 | |
---|
900 | 988 | mutex_lock(&dev_replace->lock_finishing_cancel_unmount); |
---|
901 | | - btrfs_dev_replace_write_lock(dev_replace); |
---|
| 989 | + down_write(&dev_replace->rwsem); |
---|
| 990 | + |
---|
902 | 991 | switch (dev_replace->replace_state) { |
---|
903 | 992 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED: |
---|
904 | 993 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED: |
---|
.. | .. |
---|
914 | 1003 | break; |
---|
915 | 1004 | } |
---|
916 | 1005 | |
---|
917 | | - btrfs_dev_replace_write_unlock(dev_replace); |
---|
| 1006 | + up_write(&dev_replace->rwsem); |
---|
918 | 1007 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); |
---|
919 | 1008 | } |
---|
920 | 1009 | |
---|
.. | .. |
---|
924 | 1013 | struct task_struct *task; |
---|
925 | 1014 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
---|
926 | 1015 | |
---|
927 | | - btrfs_dev_replace_write_lock(dev_replace); |
---|
| 1016 | + down_write(&dev_replace->rwsem); |
---|
| 1017 | + |
---|
928 | 1018 | switch (dev_replace->replace_state) { |
---|
929 | 1019 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED: |
---|
930 | 1020 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED: |
---|
931 | 1021 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED: |
---|
932 | | - btrfs_dev_replace_write_unlock(dev_replace); |
---|
| 1022 | + up_write(&dev_replace->rwsem); |
---|
933 | 1023 | return 0; |
---|
934 | 1024 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED: |
---|
935 | 1025 | break; |
---|
.. | .. |
---|
945 | 1035 | "you may cancel the operation after 'mount -o degraded'"); |
---|
946 | 1036 | dev_replace->replace_state = |
---|
947 | 1037 | BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED; |
---|
948 | | - btrfs_dev_replace_write_unlock(dev_replace); |
---|
| 1038 | + up_write(&dev_replace->rwsem); |
---|
949 | 1039 | return 0; |
---|
950 | 1040 | } |
---|
951 | | - btrfs_dev_replace_write_unlock(dev_replace); |
---|
| 1041 | + up_write(&dev_replace->rwsem); |
---|
952 | 1042 | |
---|
953 | 1043 | /* |
---|
954 | 1044 | * This could collide with a paused balance, but the exclusive op logic |
---|
955 | 1045 | * should never allow both to start and pause. We don't want to allow |
---|
956 | 1046 | * dev-replace to start anyway. |
---|
957 | 1047 | */ |
---|
958 | | - if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { |
---|
959 | | - btrfs_dev_replace_write_lock(dev_replace); |
---|
| 1048 | + if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REPLACE)) { |
---|
| 1049 | + down_write(&dev_replace->rwsem); |
---|
960 | 1050 | dev_replace->replace_state = |
---|
961 | 1051 | BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED; |
---|
962 | | - btrfs_dev_replace_write_unlock(dev_replace); |
---|
| 1052 | + up_write(&dev_replace->rwsem); |
---|
963 | 1053 | btrfs_info(fs_info, |
---|
964 | 1054 | "cannot resume dev-replace, other exclusive operation running"); |
---|
965 | 1055 | return 0; |
---|
.. | .. |
---|
990 | 1080 | btrfs_device_get_total_bytes(dev_replace->srcdev), |
---|
991 | 1081 | &dev_replace->scrub_progress, 0, 1); |
---|
992 | 1082 | ret = btrfs_dev_replace_finishing(fs_info, ret); |
---|
993 | | - WARN_ON(ret); |
---|
| 1083 | + WARN_ON(ret && ret != -ECANCELED); |
---|
994 | 1084 | |
---|
995 | | - clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
---|
| 1085 | + btrfs_exclop_finish(fs_info); |
---|
996 | 1086 | return 0; |
---|
997 | 1087 | } |
---|
998 | 1088 | |
---|
999 | | -int btrfs_dev_replace_is_ongoing(struct btrfs_dev_replace *dev_replace) |
---|
| 1089 | +int __pure btrfs_dev_replace_is_ongoing(struct btrfs_dev_replace *dev_replace) |
---|
1000 | 1090 | { |
---|
1001 | 1091 | if (!dev_replace->is_valid) |
---|
1002 | 1092 | return 0; |
---|
.. | .. |
---|
1013 | 1103 | * something that can happen if the dev_replace |
---|
1014 | 1104 | * procedure is suspended by an umount and then |
---|
1015 | 1105 | * the tgtdev is missing (or "btrfs dev scan") was |
---|
1016 | | - * not called and the the filesystem is remounted |
---|
| 1106 | + * not called and the filesystem is remounted |
---|
1017 | 1107 | * in degraded state. This does not stop the |
---|
1018 | 1108 | * dev_replace procedure. It needs to be canceled |
---|
1019 | 1109 | * manually if the cancellation is wanted. |
---|
.. | .. |
---|
1023 | 1113 | return 1; |
---|
1024 | 1114 | } |
---|
1025 | 1115 | |
---|
1026 | | -void btrfs_dev_replace_read_lock(struct btrfs_dev_replace *dev_replace) |
---|
1027 | | -{ |
---|
1028 | | - read_lock(&dev_replace->lock); |
---|
1029 | | - atomic_inc(&dev_replace->read_locks); |
---|
1030 | | -} |
---|
1031 | | - |
---|
1032 | | -void btrfs_dev_replace_read_unlock(struct btrfs_dev_replace *dev_replace) |
---|
1033 | | -{ |
---|
1034 | | - ASSERT(atomic_read(&dev_replace->read_locks) > 0); |
---|
1035 | | - atomic_dec(&dev_replace->read_locks); |
---|
1036 | | - read_unlock(&dev_replace->lock); |
---|
1037 | | -} |
---|
1038 | | - |
---|
1039 | | -void btrfs_dev_replace_write_lock(struct btrfs_dev_replace *dev_replace) |
---|
1040 | | -{ |
---|
1041 | | -again: |
---|
1042 | | - wait_event(dev_replace->read_lock_wq, |
---|
1043 | | - atomic_read(&dev_replace->blocking_readers) == 0); |
---|
1044 | | - write_lock(&dev_replace->lock); |
---|
1045 | | - if (atomic_read(&dev_replace->blocking_readers)) { |
---|
1046 | | - write_unlock(&dev_replace->lock); |
---|
1047 | | - goto again; |
---|
1048 | | - } |
---|
1049 | | -} |
---|
1050 | | - |
---|
1051 | | -void btrfs_dev_replace_write_unlock(struct btrfs_dev_replace *dev_replace) |
---|
1052 | | -{ |
---|
1053 | | - ASSERT(atomic_read(&dev_replace->blocking_readers) == 0); |
---|
1054 | | - write_unlock(&dev_replace->lock); |
---|
1055 | | -} |
---|
1056 | | - |
---|
1057 | | -/* inc blocking cnt and release read lock */ |
---|
1058 | | -void btrfs_dev_replace_set_lock_blocking( |
---|
1059 | | - struct btrfs_dev_replace *dev_replace) |
---|
1060 | | -{ |
---|
1061 | | - /* only set blocking for read lock */ |
---|
1062 | | - ASSERT(atomic_read(&dev_replace->read_locks) > 0); |
---|
1063 | | - atomic_inc(&dev_replace->blocking_readers); |
---|
1064 | | - read_unlock(&dev_replace->lock); |
---|
1065 | | -} |
---|
1066 | | - |
---|
1067 | | -/* acquire read lock and dec blocking cnt */ |
---|
1068 | | -void btrfs_dev_replace_clear_lock_blocking( |
---|
1069 | | - struct btrfs_dev_replace *dev_replace) |
---|
1070 | | -{ |
---|
1071 | | - /* only set blocking for read lock */ |
---|
1072 | | - ASSERT(atomic_read(&dev_replace->read_locks) > 0); |
---|
1073 | | - ASSERT(atomic_read(&dev_replace->blocking_readers) > 0); |
---|
1074 | | - read_lock(&dev_replace->lock); |
---|
1075 | | - /* Barrier implied by atomic_dec_and_test */ |
---|
1076 | | - if (atomic_dec_and_test(&dev_replace->blocking_readers)) |
---|
1077 | | - cond_wake_up_nomb(&dev_replace->read_lock_wq); |
---|
1078 | | -} |
---|
1079 | | - |
---|
1080 | 1116 | void btrfs_bio_counter_inc_noblocked(struct btrfs_fs_info *fs_info) |
---|
1081 | 1117 | { |
---|
1082 | | - percpu_counter_inc(&fs_info->bio_counter); |
---|
| 1118 | + percpu_counter_inc(&fs_info->dev_replace.bio_counter); |
---|
1083 | 1119 | } |
---|
1084 | 1120 | |
---|
1085 | 1121 | void btrfs_bio_counter_sub(struct btrfs_fs_info *fs_info, s64 amount) |
---|
1086 | 1122 | { |
---|
1087 | | - percpu_counter_sub(&fs_info->bio_counter, amount); |
---|
1088 | | - cond_wake_up_nomb(&fs_info->replace_wait); |
---|
| 1123 | + percpu_counter_sub(&fs_info->dev_replace.bio_counter, amount); |
---|
| 1124 | + cond_wake_up_nomb(&fs_info->dev_replace.replace_wait); |
---|
1089 | 1125 | } |
---|
1090 | 1126 | |
---|
1091 | 1127 | void btrfs_bio_counter_inc_blocked(struct btrfs_fs_info *fs_info) |
---|
1092 | 1128 | { |
---|
1093 | 1129 | while (1) { |
---|
1094 | | - percpu_counter_inc(&fs_info->bio_counter); |
---|
| 1130 | + percpu_counter_inc(&fs_info->dev_replace.bio_counter); |
---|
1095 | 1131 | if (likely(!test_bit(BTRFS_FS_STATE_DEV_REPLACING, |
---|
1096 | 1132 | &fs_info->fs_state))) |
---|
1097 | 1133 | break; |
---|
1098 | 1134 | |
---|
1099 | 1135 | btrfs_bio_counter_dec(fs_info); |
---|
1100 | | - wait_event(fs_info->replace_wait, |
---|
| 1136 | + wait_event(fs_info->dev_replace.replace_wait, |
---|
1101 | 1137 | !test_bit(BTRFS_FS_STATE_DEV_REPLACING, |
---|
1102 | 1138 | &fs_info->fs_state)); |
---|
1103 | 1139 | } |
---|