1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) STRATO AG 2012. All rights reserved.
6 #include <linux/sched.h>
8 #include <linux/slab.h>
9 #include <linux/blkdev.h>
10 #include <linux/kthread.h>
11 #include <linux/math64.h>
13 #include "extent_map.h"
15 #include "transaction.h"
16 #include "print-tree.h"
18 #include "async-thread.h"
19 #include "check-integrity.h"
20 #include "rcu-string.h"
21 #include "dev-replace.h"
24 static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
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 static int btrfs_dev_replace_kthread(void *data);
32 int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info)
35 struct btrfs_root *dev_root = fs_info->dev_root;
36 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
37 struct extent_buffer *eb;
40 struct btrfs_path *path = NULL;
42 struct btrfs_dev_replace_item *ptr;
45 path = btrfs_alloc_path();
52 key.type = BTRFS_DEV_REPLACE_KEY;
54 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
56 no_valid_dev_replace_entry_found:
58 dev_replace->replace_state =
59 BTRFS_DEV_REPLACE_ITEM_STATE_NEVER_STARTED;
60 dev_replace->cont_reading_from_srcdev_mode =
61 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_ALWAYS;
62 dev_replace->time_started = 0;
63 dev_replace->time_stopped = 0;
64 atomic64_set(&dev_replace->num_write_errors, 0);
65 atomic64_set(&dev_replace->num_uncorrectable_read_errors, 0);
66 dev_replace->cursor_left = 0;
67 dev_replace->committed_cursor_left = 0;
68 dev_replace->cursor_left_last_write_of_item = 0;
69 dev_replace->cursor_right = 0;
70 dev_replace->srcdev = NULL;
71 dev_replace->tgtdev = NULL;
72 dev_replace->is_valid = 0;
73 dev_replace->item_needs_writeback = 0;
76 slot = path->slots[0];
78 item_size = btrfs_item_size_nr(eb, slot);
79 ptr = btrfs_item_ptr(eb, slot, struct btrfs_dev_replace_item);
81 if (item_size != sizeof(struct btrfs_dev_replace_item)) {
83 "dev_replace entry found has unexpected size, ignore entry");
84 goto no_valid_dev_replace_entry_found;
87 src_devid = btrfs_dev_replace_src_devid(eb, ptr);
88 dev_replace->cont_reading_from_srcdev_mode =
89 btrfs_dev_replace_cont_reading_from_srcdev_mode(eb, ptr);
90 dev_replace->replace_state = btrfs_dev_replace_replace_state(eb, ptr);
91 dev_replace->time_started = btrfs_dev_replace_time_started(eb, ptr);
92 dev_replace->time_stopped =
93 btrfs_dev_replace_time_stopped(eb, ptr);
94 atomic64_set(&dev_replace->num_write_errors,
95 btrfs_dev_replace_num_write_errors(eb, ptr));
96 atomic64_set(&dev_replace->num_uncorrectable_read_errors,
97 btrfs_dev_replace_num_uncorrectable_read_errors(eb, ptr));
98 dev_replace->cursor_left = btrfs_dev_replace_cursor_left(eb, ptr);
99 dev_replace->committed_cursor_left = dev_replace->cursor_left;
100 dev_replace->cursor_left_last_write_of_item = dev_replace->cursor_left;
101 dev_replace->cursor_right = btrfs_dev_replace_cursor_right(eb, ptr);
102 dev_replace->is_valid = 1;
104 dev_replace->item_needs_writeback = 0;
105 switch (dev_replace->replace_state) {
106 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
107 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
108 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
109 dev_replace->srcdev = NULL;
110 dev_replace->tgtdev = NULL;
112 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
113 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
114 dev_replace->srcdev = btrfs_find_device(fs_info, src_devid,
116 dev_replace->tgtdev = btrfs_find_device(fs_info,
117 BTRFS_DEV_REPLACE_DEVID,
120 * allow 'btrfs dev replace_cancel' if src/tgt device is
123 if (!dev_replace->srcdev &&
124 !btrfs_test_opt(fs_info, DEGRADED)) {
127 "cannot mount because device replace operation is ongoing and");
129 "srcdev (devid %llu) is missing, need to run 'btrfs dev scan'?",
132 if (!dev_replace->tgtdev &&
133 !btrfs_test_opt(fs_info, DEGRADED)) {
136 "cannot mount because device replace operation is ongoing and");
138 "tgtdev (devid %llu) is missing, need to run 'btrfs dev scan'?",
139 BTRFS_DEV_REPLACE_DEVID);
141 if (dev_replace->tgtdev) {
142 if (dev_replace->srcdev) {
143 dev_replace->tgtdev->total_bytes =
144 dev_replace->srcdev->total_bytes;
145 dev_replace->tgtdev->disk_total_bytes =
146 dev_replace->srcdev->disk_total_bytes;
147 dev_replace->tgtdev->commit_total_bytes =
148 dev_replace->srcdev->commit_total_bytes;
149 dev_replace->tgtdev->bytes_used =
150 dev_replace->srcdev->bytes_used;
151 dev_replace->tgtdev->commit_bytes_used =
152 dev_replace->srcdev->commit_bytes_used;
154 set_bit(BTRFS_DEV_STATE_REPLACE_TGT,
155 &dev_replace->tgtdev->dev_state);
157 WARN_ON(fs_info->fs_devices->rw_devices == 0);
158 dev_replace->tgtdev->io_width = fs_info->sectorsize;
159 dev_replace->tgtdev->io_align = fs_info->sectorsize;
160 dev_replace->tgtdev->sector_size = fs_info->sectorsize;
161 dev_replace->tgtdev->fs_info = fs_info;
162 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
163 &dev_replace->tgtdev->dev_state);
169 btrfs_free_path(path);
174 * Initialize a new device for device replace target from a given source dev
177 * Return 0 and new device in @device_out, otherwise return < 0
179 static int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
180 const char *device_path,
181 struct btrfs_device *srcdev,
182 struct btrfs_device **device_out)
184 struct btrfs_device *device;
185 struct block_device *bdev;
186 struct list_head *devices;
187 struct rcu_string *name;
188 u64 devid = BTRFS_DEV_REPLACE_DEVID;
192 if (fs_info->fs_devices->seeding) {
193 btrfs_err(fs_info, "the filesystem is a seed filesystem!");
197 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
198 fs_info->bdev_holder);
200 btrfs_err(fs_info, "target device %s is invalid!", device_path);
201 return PTR_ERR(bdev);
204 filemap_write_and_wait(bdev->bd_inode->i_mapping);
206 devices = &fs_info->fs_devices->devices;
207 list_for_each_entry(device, devices, dev_list) {
208 if (device->bdev == bdev) {
210 "target device is in the filesystem!");
217 if (i_size_read(bdev->bd_inode) <
218 btrfs_device_get_total_bytes(srcdev)) {
220 "target device is smaller than source device!");
226 device = btrfs_alloc_device(NULL, &devid, NULL);
227 if (IS_ERR(device)) {
228 ret = PTR_ERR(device);
232 name = rcu_string_strdup(device_path, GFP_KERNEL);
234 btrfs_free_device(device);
238 rcu_assign_pointer(device->name, name);
240 mutex_lock(&fs_info->fs_devices->device_list_mutex);
241 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
242 device->generation = 0;
243 device->io_width = fs_info->sectorsize;
244 device->io_align = fs_info->sectorsize;
245 device->sector_size = fs_info->sectorsize;
246 device->total_bytes = btrfs_device_get_total_bytes(srcdev);
247 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
248 device->bytes_used = btrfs_device_get_bytes_used(srcdev);
249 device->commit_total_bytes = srcdev->commit_total_bytes;
250 device->commit_bytes_used = device->bytes_used;
251 device->fs_info = fs_info;
253 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
254 set_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
255 device->mode = FMODE_EXCL;
256 device->dev_stats_valid = 1;
257 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
258 device->fs_devices = fs_info->fs_devices;
259 list_add(&device->dev_list, &fs_info->fs_devices->devices);
260 fs_info->fs_devices->num_devices++;
261 fs_info->fs_devices->open_devices++;
262 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
264 *device_out = device;
268 blkdev_put(bdev, FMODE_EXCL);
273 * called from commit_transaction. Writes changed device replace state to
276 int btrfs_run_dev_replace(struct btrfs_trans_handle *trans,
277 struct btrfs_fs_info *fs_info)
280 struct btrfs_root *dev_root = fs_info->dev_root;
281 struct btrfs_path *path;
282 struct btrfs_key key;
283 struct extent_buffer *eb;
284 struct btrfs_dev_replace_item *ptr;
285 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
287 btrfs_dev_replace_read_lock(dev_replace);
288 if (!dev_replace->is_valid ||
289 !dev_replace->item_needs_writeback) {
290 btrfs_dev_replace_read_unlock(dev_replace);
293 btrfs_dev_replace_read_unlock(dev_replace);
296 key.type = BTRFS_DEV_REPLACE_KEY;
299 path = btrfs_alloc_path();
304 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
307 "error %d while searching for dev_replace item!",
313 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
315 * need to delete old one and insert a new one.
316 * Since no attempt is made to recover any old state, if the
317 * dev_replace state is 'running', the data on the target
319 * It would be possible to recover the state: just make sure
320 * that the beginning of the item is never changed and always
321 * contains all the essential information. Then read this
322 * minimal set of information and use it as a base for the
325 ret = btrfs_del_item(trans, dev_root, path);
328 "delete too small dev_replace item failed %d!",
336 /* need to insert a new item */
337 btrfs_release_path(path);
338 ret = btrfs_insert_empty_item(trans, dev_root, path,
342 "insert dev_replace item failed %d!", ret);
348 ptr = btrfs_item_ptr(eb, path->slots[0],
349 struct btrfs_dev_replace_item);
351 btrfs_dev_replace_write_lock(dev_replace);
352 if (dev_replace->srcdev)
353 btrfs_set_dev_replace_src_devid(eb, ptr,
354 dev_replace->srcdev->devid);
356 btrfs_set_dev_replace_src_devid(eb, ptr, (u64)-1);
357 btrfs_set_dev_replace_cont_reading_from_srcdev_mode(eb, ptr,
358 dev_replace->cont_reading_from_srcdev_mode);
359 btrfs_set_dev_replace_replace_state(eb, ptr,
360 dev_replace->replace_state);
361 btrfs_set_dev_replace_time_started(eb, ptr, dev_replace->time_started);
362 btrfs_set_dev_replace_time_stopped(eb, ptr, dev_replace->time_stopped);
363 btrfs_set_dev_replace_num_write_errors(eb, ptr,
364 atomic64_read(&dev_replace->num_write_errors));
365 btrfs_set_dev_replace_num_uncorrectable_read_errors(eb, ptr,
366 atomic64_read(&dev_replace->num_uncorrectable_read_errors));
367 dev_replace->cursor_left_last_write_of_item =
368 dev_replace->cursor_left;
369 btrfs_set_dev_replace_cursor_left(eb, ptr,
370 dev_replace->cursor_left_last_write_of_item);
371 btrfs_set_dev_replace_cursor_right(eb, ptr,
372 dev_replace->cursor_right);
373 dev_replace->item_needs_writeback = 0;
374 btrfs_dev_replace_write_unlock(dev_replace);
376 btrfs_mark_buffer_dirty(eb);
379 btrfs_free_path(path);
384 static char* btrfs_dev_name(struct btrfs_device *device)
386 if (!device || test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
387 return "<missing disk>";
389 return rcu_str_deref(device->name);
392 static int btrfs_dev_replace_start(struct btrfs_fs_info *fs_info,
393 const char *tgtdev_name, u64 srcdevid, const char *srcdev_name,
396 struct btrfs_root *root = fs_info->dev_root;
397 struct btrfs_trans_handle *trans;
398 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
400 struct btrfs_device *tgt_device = NULL;
401 struct btrfs_device *src_device = NULL;
404 src_device = btrfs_find_device_by_devspec(fs_info, srcdevid,
406 if (IS_ERR(src_device))
407 return PTR_ERR(src_device);
409 if (btrfs_pinned_by_swapfile(fs_info, src_device)) {
410 btrfs_warn_in_rcu(fs_info,
411 "cannot replace device %s (devid %llu) due to active swapfile",
412 btrfs_dev_name(src_device), src_device->devid);
416 ret = btrfs_init_dev_replace_tgtdev(fs_info, tgtdev_name,
417 src_device, &tgt_device);
422 * Here we commit the transaction to make sure commit_total_bytes
423 * of all the devices are updated.
425 trans = btrfs_attach_transaction(root);
426 if (!IS_ERR(trans)) {
427 ret = btrfs_commit_transaction(trans);
430 } else if (PTR_ERR(trans) != -ENOENT) {
431 return PTR_ERR(trans);
435 btrfs_dev_replace_write_lock(dev_replace);
436 switch (dev_replace->replace_state) {
437 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
438 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
439 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
441 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
442 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
444 ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_ALREADY_STARTED;
448 dev_replace->cont_reading_from_srcdev_mode = read_src;
449 WARN_ON(!src_device);
450 dev_replace->srcdev = src_device;
451 dev_replace->tgtdev = tgt_device;
453 btrfs_info_in_rcu(fs_info,
454 "dev_replace from %s (devid %llu) to %s started",
455 btrfs_dev_name(src_device),
457 rcu_str_deref(tgt_device->name));
460 * from now on, the writes to the srcdev are all duplicated to
461 * go to the tgtdev as well (refer to btrfs_map_block()).
463 dev_replace->replace_state = BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED;
464 dev_replace->time_started = ktime_get_real_seconds();
465 dev_replace->cursor_left = 0;
466 dev_replace->committed_cursor_left = 0;
467 dev_replace->cursor_left_last_write_of_item = 0;
468 dev_replace->cursor_right = 0;
469 dev_replace->is_valid = 1;
470 dev_replace->item_needs_writeback = 1;
471 atomic64_set(&dev_replace->num_write_errors, 0);
472 atomic64_set(&dev_replace->num_uncorrectable_read_errors, 0);
473 btrfs_dev_replace_write_unlock(dev_replace);
476 ret = btrfs_sysfs_add_device_link(tgt_device->fs_devices, tgt_device);
478 btrfs_err(fs_info, "kobj add dev failed %d", ret);
480 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
482 /* force writing the updated state information to disk */
483 trans = btrfs_start_transaction(root, 0);
485 ret = PTR_ERR(trans);
487 btrfs_dev_replace_write_lock(dev_replace);
488 dev_replace->replace_state =
489 BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED;
490 dev_replace->srcdev = NULL;
491 dev_replace->tgtdev = NULL;
495 ret = btrfs_commit_transaction(trans);
498 /* the disk copy procedure reuses the scrub code */
499 ret = btrfs_scrub_dev(fs_info, src_device->devid, 0,
500 btrfs_device_get_total_bytes(src_device),
501 &dev_replace->scrub_progress, 0, 1);
503 ret = btrfs_dev_replace_finishing(fs_info, ret);
504 if (ret == -EINPROGRESS) {
505 ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS;
514 btrfs_dev_replace_write_unlock(dev_replace);
515 btrfs_destroy_dev_replace_tgtdev(tgt_device);
519 int btrfs_dev_replace_by_ioctl(struct btrfs_fs_info *fs_info,
520 struct btrfs_ioctl_dev_replace_args *args)
524 switch (args->start.cont_reading_from_srcdev_mode) {
525 case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_ALWAYS:
526 case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_AVOID:
532 if ((args->start.srcdevid == 0 && args->start.srcdev_name[0] == '\0') ||
533 args->start.tgtdev_name[0] == '\0')
536 ret = btrfs_dev_replace_start(fs_info, args->start.tgtdev_name,
537 args->start.srcdevid,
538 args->start.srcdev_name,
539 args->start.cont_reading_from_srcdev_mode);
541 /* don't warn if EINPROGRESS, someone else might be running scrub */
542 if (ret == BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS)
549 * blocked until all in-flight bios operations are finished.
551 static void btrfs_rm_dev_replace_blocked(struct btrfs_fs_info *fs_info)
553 set_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state);
554 wait_event(fs_info->dev_replace.replace_wait, !percpu_counter_sum(
555 &fs_info->dev_replace.bio_counter));
559 * we have removed target device, it is safe to allow new bios request.
561 static void btrfs_rm_dev_replace_unblocked(struct btrfs_fs_info *fs_info)
563 clear_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state);
564 wake_up(&fs_info->dev_replace.replace_wait);
567 static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
570 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
571 struct btrfs_device *tgt_device;
572 struct btrfs_device *src_device;
573 struct btrfs_root *root = fs_info->tree_root;
574 u8 uuid_tmp[BTRFS_UUID_SIZE];
575 struct btrfs_trans_handle *trans;
578 /* don't allow cancel or unmount to disturb the finishing procedure */
579 mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
581 btrfs_dev_replace_read_lock(dev_replace);
582 /* was the operation canceled, or is it finished? */
583 if (dev_replace->replace_state !=
584 BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED) {
585 btrfs_dev_replace_read_unlock(dev_replace);
586 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
590 tgt_device = dev_replace->tgtdev;
591 src_device = dev_replace->srcdev;
592 btrfs_dev_replace_read_unlock(dev_replace);
595 * flush all outstanding I/O and inode extent mappings before the
596 * copy operation is declared as being finished
598 ret = btrfs_start_delalloc_roots(fs_info, -1);
600 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
603 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
605 trans = btrfs_start_transaction(root, 0);
607 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
608 return PTR_ERR(trans);
610 ret = btrfs_commit_transaction(trans);
613 /* keep away write_all_supers() during the finishing procedure */
614 mutex_lock(&fs_info->fs_devices->device_list_mutex);
615 mutex_lock(&fs_info->chunk_mutex);
616 btrfs_dev_replace_write_lock(dev_replace);
617 dev_replace->replace_state =
618 scrub_ret ? BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED
619 : BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED;
620 dev_replace->tgtdev = NULL;
621 dev_replace->srcdev = NULL;
622 dev_replace->time_stopped = ktime_get_real_seconds();
623 dev_replace->item_needs_writeback = 1;
625 /* replace old device with new one in mapping tree */
627 btrfs_dev_replace_update_device_in_mapping_tree(fs_info,
631 btrfs_err_in_rcu(fs_info,
632 "btrfs_scrub_dev(%s, %llu, %s) failed %d",
633 btrfs_dev_name(src_device),
635 rcu_str_deref(tgt_device->name), scrub_ret);
636 btrfs_dev_replace_write_unlock(dev_replace);
637 mutex_unlock(&fs_info->chunk_mutex);
638 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
639 btrfs_rm_dev_replace_blocked(fs_info);
641 btrfs_destroy_dev_replace_tgtdev(tgt_device);
642 btrfs_rm_dev_replace_unblocked(fs_info);
643 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
648 btrfs_info_in_rcu(fs_info,
649 "dev_replace from %s (devid %llu) to %s finished",
650 btrfs_dev_name(src_device),
652 rcu_str_deref(tgt_device->name));
653 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &tgt_device->dev_state);
654 tgt_device->devid = src_device->devid;
655 src_device->devid = BTRFS_DEV_REPLACE_DEVID;
656 memcpy(uuid_tmp, tgt_device->uuid, sizeof(uuid_tmp));
657 memcpy(tgt_device->uuid, src_device->uuid, sizeof(tgt_device->uuid));
658 memcpy(src_device->uuid, uuid_tmp, sizeof(src_device->uuid));
659 btrfs_device_set_total_bytes(tgt_device, src_device->total_bytes);
660 btrfs_device_set_disk_total_bytes(tgt_device,
661 src_device->disk_total_bytes);
662 btrfs_device_set_bytes_used(tgt_device, src_device->bytes_used);
663 ASSERT(list_empty(&src_device->resized_list));
664 tgt_device->commit_total_bytes = src_device->commit_total_bytes;
665 tgt_device->commit_bytes_used = src_device->bytes_used;
667 btrfs_assign_next_active_device(src_device, tgt_device);
669 list_add(&tgt_device->dev_alloc_list, &fs_info->fs_devices->alloc_list);
670 fs_info->fs_devices->rw_devices++;
672 btrfs_dev_replace_write_unlock(dev_replace);
674 btrfs_rm_dev_replace_blocked(fs_info);
676 btrfs_rm_dev_replace_remove_srcdev(src_device);
678 btrfs_rm_dev_replace_unblocked(fs_info);
681 * Increment dev_stats_ccnt so that btrfs_run_dev_stats() will
682 * update on-disk dev stats value during commit transaction
684 atomic_inc(&tgt_device->dev_stats_ccnt);
687 * this is again a consistent state where no dev_replace procedure
688 * is running, the target device is part of the filesystem, the
689 * source device is not part of the filesystem anymore and its 1st
690 * superblock is scratched out so that it is no longer marked to
691 * belong to this filesystem.
693 mutex_unlock(&fs_info->chunk_mutex);
694 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
696 /* replace the sysfs entry */
697 btrfs_sysfs_rm_device_link(fs_info->fs_devices, src_device);
698 btrfs_rm_dev_replace_free_srcdev(fs_info, src_device);
700 /* write back the superblocks */
701 trans = btrfs_start_transaction(root, 0);
703 btrfs_commit_transaction(trans);
705 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
710 static void btrfs_dev_replace_update_device_in_mapping_tree(
711 struct btrfs_fs_info *fs_info,
712 struct btrfs_device *srcdev,
713 struct btrfs_device *tgtdev)
715 struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
716 struct extent_map *em;
717 struct map_lookup *map;
721 write_lock(&em_tree->lock);
723 em = lookup_extent_mapping(em_tree, start, (u64)-1);
726 map = em->map_lookup;
727 for (i = 0; i < map->num_stripes; i++)
728 if (srcdev == map->stripes[i].dev)
729 map->stripes[i].dev = tgtdev;
730 start = em->start + em->len;
733 write_unlock(&em_tree->lock);
737 * Read progress of device replace status according to the state and last
738 * stored position. The value format is the same as for
739 * btrfs_dev_replace::progress_1000
741 static u64 btrfs_dev_replace_progress(struct btrfs_fs_info *fs_info)
743 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
746 switch (dev_replace->replace_state) {
747 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
748 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
751 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
754 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
755 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
756 ret = div64_u64(dev_replace->cursor_left,
757 div_u64(btrfs_device_get_total_bytes(
758 dev_replace->srcdev), 1000));
765 void btrfs_dev_replace_status(struct btrfs_fs_info *fs_info,
766 struct btrfs_ioctl_dev_replace_args *args)
768 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
770 btrfs_dev_replace_read_lock(dev_replace);
771 /* even if !dev_replace_is_valid, the values are good enough for
772 * the replace_status ioctl */
773 args->result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
774 args->status.replace_state = dev_replace->replace_state;
775 args->status.time_started = dev_replace->time_started;
776 args->status.time_stopped = dev_replace->time_stopped;
777 args->status.num_write_errors =
778 atomic64_read(&dev_replace->num_write_errors);
779 args->status.num_uncorrectable_read_errors =
780 atomic64_read(&dev_replace->num_uncorrectable_read_errors);
781 args->status.progress_1000 = btrfs_dev_replace_progress(fs_info);
782 btrfs_dev_replace_read_unlock(dev_replace);
785 int btrfs_dev_replace_cancel(struct btrfs_fs_info *fs_info)
787 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
788 struct btrfs_device *tgt_device = NULL;
789 struct btrfs_device *src_device = NULL;
790 struct btrfs_trans_handle *trans;
791 struct btrfs_root *root = fs_info->tree_root;
795 if (sb_rdonly(fs_info->sb))
798 mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
799 btrfs_dev_replace_write_lock(dev_replace);
800 switch (dev_replace->replace_state) {
801 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
802 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
803 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
804 result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED;
805 btrfs_dev_replace_write_unlock(dev_replace);
807 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
808 tgt_device = dev_replace->tgtdev;
809 src_device = dev_replace->srcdev;
810 btrfs_dev_replace_write_unlock(dev_replace);
811 ret = btrfs_scrub_cancel(fs_info);
813 result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED;
815 result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
817 * btrfs_dev_replace_finishing() will handle the
820 btrfs_info_in_rcu(fs_info,
821 "dev_replace from %s (devid %llu) to %s canceled",
822 btrfs_dev_name(src_device), src_device->devid,
823 btrfs_dev_name(tgt_device));
826 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
828 * Scrub doing the replace isn't running so we need to do the
829 * cleanup step of btrfs_dev_replace_finishing() here
831 result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
832 tgt_device = dev_replace->tgtdev;
833 src_device = dev_replace->srcdev;
834 dev_replace->tgtdev = NULL;
835 dev_replace->srcdev = NULL;
836 dev_replace->replace_state =
837 BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED;
838 dev_replace->time_stopped = ktime_get_real_seconds();
839 dev_replace->item_needs_writeback = 1;
841 btrfs_dev_replace_write_unlock(dev_replace);
843 /* Scrub for replace must not be running in suspended state */
844 ret = btrfs_scrub_cancel(fs_info);
845 ASSERT(ret != -ENOTCONN);
847 trans = btrfs_start_transaction(root, 0);
849 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
850 return PTR_ERR(trans);
852 ret = btrfs_commit_transaction(trans);
855 btrfs_info_in_rcu(fs_info,
856 "suspended dev_replace from %s (devid %llu) to %s canceled",
857 btrfs_dev_name(src_device), src_device->devid,
858 btrfs_dev_name(tgt_device));
861 btrfs_destroy_dev_replace_tgtdev(tgt_device);
867 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
871 void btrfs_dev_replace_suspend_for_unmount(struct btrfs_fs_info *fs_info)
873 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
875 mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
876 btrfs_dev_replace_write_lock(dev_replace);
877 switch (dev_replace->replace_state) {
878 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
879 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
880 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
881 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
883 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
884 dev_replace->replace_state =
885 BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
886 dev_replace->time_stopped = ktime_get_real_seconds();
887 dev_replace->item_needs_writeback = 1;
888 btrfs_info(fs_info, "suspending dev_replace for unmount");
892 btrfs_dev_replace_write_unlock(dev_replace);
893 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
896 /* resume dev_replace procedure that was interrupted by unmount */
897 int btrfs_resume_dev_replace_async(struct btrfs_fs_info *fs_info)
899 struct task_struct *task;
900 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
902 btrfs_dev_replace_write_lock(dev_replace);
903 switch (dev_replace->replace_state) {
904 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
905 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
906 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
907 btrfs_dev_replace_write_unlock(dev_replace);
909 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
911 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
912 dev_replace->replace_state =
913 BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED;
916 if (!dev_replace->tgtdev || !dev_replace->tgtdev->bdev) {
918 "cannot continue dev_replace, tgtdev is missing");
920 "you may cancel the operation after 'mount -o degraded'");
921 dev_replace->replace_state =
922 BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
923 btrfs_dev_replace_write_unlock(dev_replace);
926 btrfs_dev_replace_write_unlock(dev_replace);
929 * This could collide with a paused balance, but the exclusive op logic
930 * should never allow both to start and pause. We don't want to allow
931 * dev-replace to start anyway.
933 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
934 btrfs_dev_replace_write_lock(dev_replace);
935 dev_replace->replace_state =
936 BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
937 btrfs_dev_replace_write_unlock(dev_replace);
939 "cannot resume dev-replace, other exclusive operation running");
943 task = kthread_run(btrfs_dev_replace_kthread, fs_info, "btrfs-devrepl");
944 return PTR_ERR_OR_ZERO(task);
947 static int btrfs_dev_replace_kthread(void *data)
949 struct btrfs_fs_info *fs_info = data;
950 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
954 progress = btrfs_dev_replace_progress(fs_info);
955 progress = div_u64(progress, 10);
956 btrfs_info_in_rcu(fs_info,
957 "continuing dev_replace from %s (devid %llu) to target %s @%u%%",
958 btrfs_dev_name(dev_replace->srcdev),
959 dev_replace->srcdev->devid,
960 btrfs_dev_name(dev_replace->tgtdev),
961 (unsigned int)progress);
963 ret = btrfs_scrub_dev(fs_info, dev_replace->srcdev->devid,
964 dev_replace->committed_cursor_left,
965 btrfs_device_get_total_bytes(dev_replace->srcdev),
966 &dev_replace->scrub_progress, 0, 1);
967 ret = btrfs_dev_replace_finishing(fs_info, ret);
970 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
974 int btrfs_dev_replace_is_ongoing(struct btrfs_dev_replace *dev_replace)
976 if (!dev_replace->is_valid)
979 switch (dev_replace->replace_state) {
980 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
981 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
982 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
984 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
985 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
987 * return true even if tgtdev is missing (this is
988 * something that can happen if the dev_replace
989 * procedure is suspended by an umount and then
990 * the tgtdev is missing (or "btrfs dev scan") was
991 * not called and the the filesystem is remounted
992 * in degraded state. This does not stop the
993 * dev_replace procedure. It needs to be canceled
994 * manually if the cancellation is wanted.
1001 void btrfs_dev_replace_read_lock(struct btrfs_dev_replace *dev_replace)
1003 read_lock(&dev_replace->lock);
1006 void btrfs_dev_replace_read_unlock(struct btrfs_dev_replace *dev_replace)
1008 read_unlock(&dev_replace->lock);
1011 void btrfs_dev_replace_write_lock(struct btrfs_dev_replace *dev_replace)
1014 wait_event(dev_replace->read_lock_wq,
1015 atomic_read(&dev_replace->blocking_readers) == 0);
1016 write_lock(&dev_replace->lock);
1017 if (atomic_read(&dev_replace->blocking_readers)) {
1018 write_unlock(&dev_replace->lock);
1023 void btrfs_dev_replace_write_unlock(struct btrfs_dev_replace *dev_replace)
1025 write_unlock(&dev_replace->lock);
1028 /* inc blocking cnt and release read lock */
1029 void btrfs_dev_replace_set_lock_blocking(
1030 struct btrfs_dev_replace *dev_replace)
1032 /* only set blocking for read lock */
1033 atomic_inc(&dev_replace->blocking_readers);
1034 read_unlock(&dev_replace->lock);
1037 void btrfs_bio_counter_inc_noblocked(struct btrfs_fs_info *fs_info)
1039 percpu_counter_inc(&fs_info->dev_replace.bio_counter);
1042 void btrfs_bio_counter_sub(struct btrfs_fs_info *fs_info, s64 amount)
1044 percpu_counter_sub(&fs_info->dev_replace.bio_counter, amount);
1045 cond_wake_up_nomb(&fs_info->dev_replace.replace_wait);
1048 void btrfs_bio_counter_inc_blocked(struct btrfs_fs_info *fs_info)
1051 percpu_counter_inc(&fs_info->dev_replace.bio_counter);
1052 if (likely(!test_bit(BTRFS_FS_STATE_DEV_REPLACING,
1053 &fs_info->fs_state)))
1056 btrfs_bio_counter_dec(fs_info);
1057 wait_event(fs_info->dev_replace.replace_wait,
1058 !test_bit(BTRFS_FS_STATE_DEV_REPLACING,
1059 &fs_info->fs_state));