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>
14 #include "extent_map.h"
16 #include "transaction.h"
17 #include "print-tree.h"
19 #include "async-thread.h"
20 #include "check-integrity.h"
21 #include "rcu-string.h"
22 #include "dev-replace.h"
26 * Device replace overview
29 * To copy all extents (both new and on-disk) from source device to target
30 * device, while still keeping the filesystem read-write.
33 * There are two main methods involved:
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.
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
45 * - Copy existing extents
47 * This happens by re-using scrub facility, as scrub also iterates through
48 * existing extents from commit root.
50 * Location: scrub_write_block_to_dev_replace() from
51 * scrub_block_complete()
52 * Content: Data/meta from commit root.
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
58 * After replace is done, the finishing part is done by swapping the target and
61 * Location: btrfs_dev_replace_update_device_in_mapping_tree() from
62 * btrfs_dev_replace_finishing()
65 static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
67 static int btrfs_dev_replace_kthread(void *data);
69 int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info)
72 struct btrfs_root *dev_root = fs_info->dev_root;
73 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
74 struct extent_buffer *eb;
77 struct btrfs_path *path = NULL;
79 struct btrfs_dev_replace_item *ptr;
82 path = btrfs_alloc_path();
89 key.type = BTRFS_DEV_REPLACE_KEY;
91 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
93 no_valid_dev_replace_entry_found:
95 * We don't have a replace item or it's corrupted. If there is
96 * a replace target, fail the mount.
98 if (btrfs_find_device(fs_info->fs_devices,
99 BTRFS_DEV_REPLACE_DEVID, NULL, NULL, false)) {
101 "found replace target device without a valid replace item");
106 dev_replace->replace_state =
107 BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED;
108 dev_replace->cont_reading_from_srcdev_mode =
109 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_ALWAYS;
110 dev_replace->time_started = 0;
111 dev_replace->time_stopped = 0;
112 atomic64_set(&dev_replace->num_write_errors, 0);
113 atomic64_set(&dev_replace->num_uncorrectable_read_errors, 0);
114 dev_replace->cursor_left = 0;
115 dev_replace->committed_cursor_left = 0;
116 dev_replace->cursor_left_last_write_of_item = 0;
117 dev_replace->cursor_right = 0;
118 dev_replace->srcdev = NULL;
119 dev_replace->tgtdev = NULL;
120 dev_replace->is_valid = 0;
121 dev_replace->item_needs_writeback = 0;
124 slot = path->slots[0];
126 item_size = btrfs_item_size_nr(eb, slot);
127 ptr = btrfs_item_ptr(eb, slot, struct btrfs_dev_replace_item);
129 if (item_size != sizeof(struct btrfs_dev_replace_item)) {
131 "dev_replace entry found has unexpected size, ignore entry");
132 goto no_valid_dev_replace_entry_found;
135 src_devid = btrfs_dev_replace_src_devid(eb, ptr);
136 dev_replace->cont_reading_from_srcdev_mode =
137 btrfs_dev_replace_cont_reading_from_srcdev_mode(eb, ptr);
138 dev_replace->replace_state = btrfs_dev_replace_replace_state(eb, ptr);
139 dev_replace->time_started = btrfs_dev_replace_time_started(eb, ptr);
140 dev_replace->time_stopped =
141 btrfs_dev_replace_time_stopped(eb, ptr);
142 atomic64_set(&dev_replace->num_write_errors,
143 btrfs_dev_replace_num_write_errors(eb, ptr));
144 atomic64_set(&dev_replace->num_uncorrectable_read_errors,
145 btrfs_dev_replace_num_uncorrectable_read_errors(eb, ptr));
146 dev_replace->cursor_left = btrfs_dev_replace_cursor_left(eb, ptr);
147 dev_replace->committed_cursor_left = dev_replace->cursor_left;
148 dev_replace->cursor_left_last_write_of_item = dev_replace->cursor_left;
149 dev_replace->cursor_right = btrfs_dev_replace_cursor_right(eb, ptr);
150 dev_replace->is_valid = 1;
152 dev_replace->item_needs_writeback = 0;
153 switch (dev_replace->replace_state) {
154 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
155 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
156 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
158 * We don't have an active replace item but if there is a
159 * replace target, fail the mount.
161 if (btrfs_find_device(fs_info->fs_devices,
162 BTRFS_DEV_REPLACE_DEVID, NULL, NULL, false)) {
164 "replace devid present without an active replace item");
167 dev_replace->srcdev = NULL;
168 dev_replace->tgtdev = NULL;
171 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
172 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
173 dev_replace->srcdev = btrfs_find_device(fs_info->fs_devices,
174 src_devid, NULL, NULL, true);
175 dev_replace->tgtdev = btrfs_find_device(fs_info->fs_devices,
176 BTRFS_DEV_REPLACE_DEVID,
179 * allow 'btrfs dev replace_cancel' if src/tgt device is
182 if (!dev_replace->srcdev &&
183 !btrfs_test_opt(fs_info, DEGRADED)) {
186 "cannot mount because device replace operation is ongoing and");
188 "srcdev (devid %llu) is missing, need to run 'btrfs dev scan'?",
191 if (!dev_replace->tgtdev &&
192 !btrfs_test_opt(fs_info, DEGRADED)) {
195 "cannot mount because device replace operation is ongoing and");
197 "tgtdev (devid %llu) is missing, need to run 'btrfs dev scan'?",
198 BTRFS_DEV_REPLACE_DEVID);
200 if (dev_replace->tgtdev) {
201 if (dev_replace->srcdev) {
202 dev_replace->tgtdev->total_bytes =
203 dev_replace->srcdev->total_bytes;
204 dev_replace->tgtdev->disk_total_bytes =
205 dev_replace->srcdev->disk_total_bytes;
206 dev_replace->tgtdev->commit_total_bytes =
207 dev_replace->srcdev->commit_total_bytes;
208 dev_replace->tgtdev->bytes_used =
209 dev_replace->srcdev->bytes_used;
210 dev_replace->tgtdev->commit_bytes_used =
211 dev_replace->srcdev->commit_bytes_used;
213 set_bit(BTRFS_DEV_STATE_REPLACE_TGT,
214 &dev_replace->tgtdev->dev_state);
216 WARN_ON(fs_info->fs_devices->rw_devices == 0);
217 dev_replace->tgtdev->io_width = fs_info->sectorsize;
218 dev_replace->tgtdev->io_align = fs_info->sectorsize;
219 dev_replace->tgtdev->sector_size = fs_info->sectorsize;
220 dev_replace->tgtdev->fs_info = fs_info;
221 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
222 &dev_replace->tgtdev->dev_state);
228 btrfs_free_path(path);
233 * Initialize a new device for device replace target from a given source dev
236 * Return 0 and new device in @device_out, otherwise return < 0
238 static int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
239 const char *device_path,
240 struct btrfs_device *srcdev,
241 struct btrfs_device **device_out)
243 struct btrfs_device *device;
244 struct block_device *bdev;
245 struct rcu_string *name;
246 u64 devid = BTRFS_DEV_REPLACE_DEVID;
250 if (srcdev->fs_devices->seeding) {
251 btrfs_err(fs_info, "the filesystem is a seed filesystem!");
255 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
256 fs_info->bdev_holder);
258 btrfs_err(fs_info, "target device %s is invalid!", device_path);
259 return PTR_ERR(bdev);
264 list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
265 if (device->bdev == bdev) {
267 "target device is in the filesystem!");
274 if (i_size_read(bdev->bd_inode) <
275 btrfs_device_get_total_bytes(srcdev)) {
277 "target device is smaller than source device!");
283 device = btrfs_alloc_device(NULL, &devid, NULL);
284 if (IS_ERR(device)) {
285 ret = PTR_ERR(device);
289 name = rcu_string_strdup(device_path, GFP_KERNEL);
291 btrfs_free_device(device);
295 rcu_assign_pointer(device->name, name);
297 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
298 device->generation = 0;
299 device->io_width = fs_info->sectorsize;
300 device->io_align = fs_info->sectorsize;
301 device->sector_size = fs_info->sectorsize;
302 device->total_bytes = btrfs_device_get_total_bytes(srcdev);
303 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
304 device->bytes_used = btrfs_device_get_bytes_used(srcdev);
305 device->commit_total_bytes = srcdev->commit_total_bytes;
306 device->commit_bytes_used = device->bytes_used;
307 device->fs_info = fs_info;
309 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
310 set_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
311 device->mode = FMODE_EXCL;
312 device->dev_stats_valid = 1;
313 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
314 device->fs_devices = fs_info->fs_devices;
316 mutex_lock(&fs_info->fs_devices->device_list_mutex);
317 list_add(&device->dev_list, &fs_info->fs_devices->devices);
318 fs_info->fs_devices->num_devices++;
319 fs_info->fs_devices->open_devices++;
320 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
322 *device_out = device;
326 blkdev_put(bdev, FMODE_EXCL);
331 * called from commit_transaction. Writes changed device replace state to
334 int btrfs_run_dev_replace(struct btrfs_trans_handle *trans)
336 struct btrfs_fs_info *fs_info = trans->fs_info;
338 struct btrfs_root *dev_root = fs_info->dev_root;
339 struct btrfs_path *path;
340 struct btrfs_key key;
341 struct extent_buffer *eb;
342 struct btrfs_dev_replace_item *ptr;
343 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
345 down_read(&dev_replace->rwsem);
346 if (!dev_replace->is_valid ||
347 !dev_replace->item_needs_writeback) {
348 up_read(&dev_replace->rwsem);
351 up_read(&dev_replace->rwsem);
354 key.type = BTRFS_DEV_REPLACE_KEY;
357 path = btrfs_alloc_path();
362 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
365 "error %d while searching for dev_replace item!",
371 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
373 * need to delete old one and insert a new one.
374 * Since no attempt is made to recover any old state, if the
375 * dev_replace state is 'running', the data on the target
377 * It would be possible to recover the state: just make sure
378 * that the beginning of the item is never changed and always
379 * contains all the essential information. Then read this
380 * minimal set of information and use it as a base for the
383 ret = btrfs_del_item(trans, dev_root, path);
386 "delete too small dev_replace item failed %d!",
394 /* need to insert a new item */
395 btrfs_release_path(path);
396 ret = btrfs_insert_empty_item(trans, dev_root, path,
400 "insert dev_replace item failed %d!", ret);
406 ptr = btrfs_item_ptr(eb, path->slots[0],
407 struct btrfs_dev_replace_item);
409 down_write(&dev_replace->rwsem);
410 if (dev_replace->srcdev)
411 btrfs_set_dev_replace_src_devid(eb, ptr,
412 dev_replace->srcdev->devid);
414 btrfs_set_dev_replace_src_devid(eb, ptr, (u64)-1);
415 btrfs_set_dev_replace_cont_reading_from_srcdev_mode(eb, ptr,
416 dev_replace->cont_reading_from_srcdev_mode);
417 btrfs_set_dev_replace_replace_state(eb, ptr,
418 dev_replace->replace_state);
419 btrfs_set_dev_replace_time_started(eb, ptr, dev_replace->time_started);
420 btrfs_set_dev_replace_time_stopped(eb, ptr, dev_replace->time_stopped);
421 btrfs_set_dev_replace_num_write_errors(eb, ptr,
422 atomic64_read(&dev_replace->num_write_errors));
423 btrfs_set_dev_replace_num_uncorrectable_read_errors(eb, ptr,
424 atomic64_read(&dev_replace->num_uncorrectable_read_errors));
425 dev_replace->cursor_left_last_write_of_item =
426 dev_replace->cursor_left;
427 btrfs_set_dev_replace_cursor_left(eb, ptr,
428 dev_replace->cursor_left_last_write_of_item);
429 btrfs_set_dev_replace_cursor_right(eb, ptr,
430 dev_replace->cursor_right);
431 dev_replace->item_needs_writeback = 0;
432 up_write(&dev_replace->rwsem);
434 btrfs_mark_buffer_dirty(eb);
437 btrfs_free_path(path);
442 static char* btrfs_dev_name(struct btrfs_device *device)
444 if (!device || test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
445 return "<missing disk>";
447 return rcu_str_deref(device->name);
450 static int btrfs_dev_replace_start(struct btrfs_fs_info *fs_info,
451 const char *tgtdev_name, u64 srcdevid, const char *srcdev_name,
454 struct btrfs_root *root = fs_info->dev_root;
455 struct btrfs_trans_handle *trans;
456 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
458 struct btrfs_device *tgt_device = NULL;
459 struct btrfs_device *src_device = NULL;
461 src_device = btrfs_find_device_by_devspec(fs_info, srcdevid,
463 if (IS_ERR(src_device))
464 return PTR_ERR(src_device);
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);
474 * Here we commit the transaction to make sure commit_total_bytes
475 * of all the devices are updated.
477 trans = btrfs_attach_transaction(root);
478 if (!IS_ERR(trans)) {
479 ret = btrfs_commit_transaction(trans);
482 } else if (PTR_ERR(trans) != -ENOENT) {
483 return PTR_ERR(trans);
486 ret = btrfs_init_dev_replace_tgtdev(fs_info, tgtdev_name,
487 src_device, &tgt_device);
491 down_write(&dev_replace->rwsem);
492 switch (dev_replace->replace_state) {
493 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
494 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
495 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
497 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
498 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
500 ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_ALREADY_STARTED;
501 up_write(&dev_replace->rwsem);
505 dev_replace->cont_reading_from_srcdev_mode = read_src;
506 dev_replace->srcdev = src_device;
507 dev_replace->tgtdev = tgt_device;
509 btrfs_info_in_rcu(fs_info,
510 "dev_replace from %s (devid %llu) to %s started",
511 btrfs_dev_name(src_device),
513 rcu_str_deref(tgt_device->name));
516 * from now on, the writes to the srcdev are all duplicated to
517 * go to the tgtdev as well (refer to btrfs_map_block()).
519 dev_replace->replace_state = BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED;
520 dev_replace->time_started = ktime_get_real_seconds();
521 dev_replace->cursor_left = 0;
522 dev_replace->committed_cursor_left = 0;
523 dev_replace->cursor_left_last_write_of_item = 0;
524 dev_replace->cursor_right = 0;
525 dev_replace->is_valid = 1;
526 dev_replace->item_needs_writeback = 1;
527 atomic64_set(&dev_replace->num_write_errors, 0);
528 atomic64_set(&dev_replace->num_uncorrectable_read_errors, 0);
529 up_write(&dev_replace->rwsem);
531 ret = btrfs_sysfs_add_device(tgt_device);
533 btrfs_err(fs_info, "kobj add dev failed %d", ret);
535 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
537 /* Commit dev_replace state and reserve 1 item for it. */
538 trans = btrfs_start_transaction(root, 1);
540 ret = PTR_ERR(trans);
541 down_write(&dev_replace->rwsem);
542 dev_replace->replace_state =
543 BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED;
544 dev_replace->srcdev = NULL;
545 dev_replace->tgtdev = NULL;
546 up_write(&dev_replace->rwsem);
550 ret = btrfs_commit_transaction(trans);
553 /* the disk copy procedure reuses the scrub code */
554 ret = btrfs_scrub_dev(fs_info, src_device->devid, 0,
555 btrfs_device_get_total_bytes(src_device),
556 &dev_replace->scrub_progress, 0, 1);
558 ret = btrfs_dev_replace_finishing(fs_info, ret);
559 if (ret == -EINPROGRESS)
560 ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS;
565 btrfs_destroy_dev_replace_tgtdev(tgt_device);
569 int btrfs_dev_replace_by_ioctl(struct btrfs_fs_info *fs_info,
570 struct btrfs_ioctl_dev_replace_args *args)
574 switch (args->start.cont_reading_from_srcdev_mode) {
575 case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_ALWAYS:
576 case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_AVOID:
582 if ((args->start.srcdevid == 0 && args->start.srcdev_name[0] == '\0') ||
583 args->start.tgtdev_name[0] == '\0')
586 ret = btrfs_dev_replace_start(fs_info, args->start.tgtdev_name,
587 args->start.srcdevid,
588 args->start.srcdev_name,
589 args->start.cont_reading_from_srcdev_mode);
591 /* don't warn if EINPROGRESS, someone else might be running scrub */
592 if (ret == BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS ||
593 ret == BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR)
600 * blocked until all in-flight bios operations are finished.
602 static void btrfs_rm_dev_replace_blocked(struct btrfs_fs_info *fs_info)
604 set_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state);
605 wait_event(fs_info->dev_replace.replace_wait, !percpu_counter_sum(
606 &fs_info->dev_replace.bio_counter));
610 * we have removed target device, it is safe to allow new bios request.
612 static void btrfs_rm_dev_replace_unblocked(struct btrfs_fs_info *fs_info)
614 clear_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state);
615 wake_up(&fs_info->dev_replace.replace_wait);
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.
624 static int btrfs_set_target_alloc_state(struct btrfs_device *srcdev,
625 struct btrfs_device *tgtdev)
627 struct extent_state *cached_state = NULL;
633 lockdep_assert_held(&srcdev->fs_info->chunk_mutex);
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);
642 start = found_end + 1;
645 free_extent_state(cached_state);
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)
654 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
655 struct extent_map *em;
656 struct map_lookup *map;
660 write_lock(&em_tree->lock);
662 em = lookup_extent_mapping(em_tree, start, (u64)-1);
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;
672 write_unlock(&em_tree->lock);
675 static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
678 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
679 struct btrfs_device *tgt_device;
680 struct btrfs_device *src_device;
681 struct btrfs_root *root = fs_info->tree_root;
682 u8 uuid_tmp[BTRFS_UUID_SIZE];
683 struct btrfs_trans_handle *trans;
686 /* don't allow cancel or unmount to disturb the finishing procedure */
687 mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
689 down_read(&dev_replace->rwsem);
690 /* was the operation canceled, or is it finished? */
691 if (dev_replace->replace_state !=
692 BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED) {
693 up_read(&dev_replace->rwsem);
694 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
698 tgt_device = dev_replace->tgtdev;
699 src_device = dev_replace->srcdev;
700 up_read(&dev_replace->rwsem);
703 * flush all outstanding I/O and inode extent mappings before the
704 * copy operation is declared as being finished
706 ret = btrfs_start_delalloc_roots(fs_info, U64_MAX);
708 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
711 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
714 btrfs_reada_remove_dev(src_device);
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.
722 trans = btrfs_start_transaction(root, 0);
724 btrfs_reada_undo_remove_dev(src_device);
725 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
726 return PTR_ERR(trans);
728 ret = btrfs_commit_transaction(trans);
731 /* Prevent write_all_supers() during the finishing procedure */
732 mutex_lock(&fs_info->fs_devices->device_list_mutex);
733 /* Prevent new chunks being allocated on the source device */
734 mutex_lock(&fs_info->chunk_mutex);
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);
744 down_write(&dev_replace->rwsem);
745 dev_replace->replace_state =
746 scrub_ret ? BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED
747 : BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED;
748 dev_replace->tgtdev = NULL;
749 dev_replace->srcdev = NULL;
750 dev_replace->time_stopped = ktime_get_real_seconds();
751 dev_replace->item_needs_writeback = 1;
754 * Update allocation state in the new device and replace the old device
755 * with the new one in the mapping tree.
758 scrub_ret = btrfs_set_target_alloc_state(src_device, tgt_device);
761 btrfs_dev_replace_update_device_in_mapping_tree(fs_info,
765 if (scrub_ret != -ECANCELED)
766 btrfs_err_in_rcu(fs_info,
767 "btrfs_scrub_dev(%s, %llu, %s) failed %d",
768 btrfs_dev_name(src_device),
770 rcu_str_deref(tgt_device->name), scrub_ret);
772 up_write(&dev_replace->rwsem);
773 mutex_unlock(&fs_info->chunk_mutex);
774 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
775 btrfs_reada_undo_remove_dev(src_device);
776 btrfs_rm_dev_replace_blocked(fs_info);
778 btrfs_destroy_dev_replace_tgtdev(tgt_device);
779 btrfs_rm_dev_replace_unblocked(fs_info);
780 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
785 btrfs_info_in_rcu(fs_info,
786 "dev_replace from %s (devid %llu) to %s finished",
787 btrfs_dev_name(src_device),
789 rcu_str_deref(tgt_device->name));
790 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &tgt_device->dev_state);
791 tgt_device->devid = src_device->devid;
792 src_device->devid = BTRFS_DEV_REPLACE_DEVID;
793 memcpy(uuid_tmp, tgt_device->uuid, sizeof(uuid_tmp));
794 memcpy(tgt_device->uuid, src_device->uuid, sizeof(tgt_device->uuid));
795 memcpy(src_device->uuid, uuid_tmp, sizeof(src_device->uuid));
796 btrfs_device_set_total_bytes(tgt_device, src_device->total_bytes);
797 btrfs_device_set_disk_total_bytes(tgt_device,
798 src_device->disk_total_bytes);
799 btrfs_device_set_bytes_used(tgt_device, src_device->bytes_used);
800 tgt_device->commit_bytes_used = src_device->bytes_used;
802 btrfs_assign_next_active_device(src_device, tgt_device);
804 list_add(&tgt_device->dev_alloc_list, &fs_info->fs_devices->alloc_list);
805 fs_info->fs_devices->rw_devices++;
807 up_write(&dev_replace->rwsem);
808 btrfs_rm_dev_replace_blocked(fs_info);
810 btrfs_rm_dev_replace_remove_srcdev(src_device);
812 btrfs_rm_dev_replace_unblocked(fs_info);
815 * Increment dev_stats_ccnt so that btrfs_run_dev_stats() will
816 * update on-disk dev stats value during commit transaction
818 atomic_inc(&tgt_device->dev_stats_ccnt);
821 * this is again a consistent state where no dev_replace procedure
822 * is running, the target device is part of the filesystem, the
823 * source device is not part of the filesystem anymore and its 1st
824 * superblock is scratched out so that it is no longer marked to
825 * belong to this filesystem.
827 mutex_unlock(&fs_info->chunk_mutex);
828 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
830 /* replace the sysfs entry */
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);
837 /* write back the superblocks */
838 trans = btrfs_start_transaction(root, 0);
840 btrfs_commit_transaction(trans);
842 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
844 btrfs_rm_dev_replace_free_srcdev(src_device);
850 * Read progress of device replace status according to the state and last
851 * stored position. The value format is the same as for
852 * btrfs_dev_replace::progress_1000
854 static u64 btrfs_dev_replace_progress(struct btrfs_fs_info *fs_info)
856 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
859 switch (dev_replace->replace_state) {
860 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
861 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
864 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
867 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
868 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
869 ret = div64_u64(dev_replace->cursor_left,
870 div_u64(btrfs_device_get_total_bytes(
871 dev_replace->srcdev), 1000));
878 void btrfs_dev_replace_status(struct btrfs_fs_info *fs_info,
879 struct btrfs_ioctl_dev_replace_args *args)
881 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
883 down_read(&dev_replace->rwsem);
884 /* even if !dev_replace_is_valid, the values are good enough for
885 * the replace_status ioctl */
886 args->result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
887 args->status.replace_state = dev_replace->replace_state;
888 args->status.time_started = dev_replace->time_started;
889 args->status.time_stopped = dev_replace->time_stopped;
890 args->status.num_write_errors =
891 atomic64_read(&dev_replace->num_write_errors);
892 args->status.num_uncorrectable_read_errors =
893 atomic64_read(&dev_replace->num_uncorrectable_read_errors);
894 args->status.progress_1000 = btrfs_dev_replace_progress(fs_info);
895 up_read(&dev_replace->rwsem);
898 int btrfs_dev_replace_cancel(struct btrfs_fs_info *fs_info)
900 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
901 struct btrfs_device *tgt_device = NULL;
902 struct btrfs_device *src_device = NULL;
903 struct btrfs_trans_handle *trans;
904 struct btrfs_root *root = fs_info->tree_root;
908 if (sb_rdonly(fs_info->sb))
911 mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
912 down_write(&dev_replace->rwsem);
913 switch (dev_replace->replace_state) {
914 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
915 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
916 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
917 result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED;
918 up_write(&dev_replace->rwsem);
920 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
921 tgt_device = dev_replace->tgtdev;
922 src_device = dev_replace->srcdev;
923 up_write(&dev_replace->rwsem);
924 ret = btrfs_scrub_cancel(fs_info);
926 result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED;
928 result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
930 * btrfs_dev_replace_finishing() will handle the
933 btrfs_info_in_rcu(fs_info,
934 "dev_replace from %s (devid %llu) to %s canceled",
935 btrfs_dev_name(src_device), src_device->devid,
936 btrfs_dev_name(tgt_device));
939 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
941 * Scrub doing the replace isn't running so we need to do the
942 * cleanup step of btrfs_dev_replace_finishing() here
944 result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
945 tgt_device = dev_replace->tgtdev;
946 src_device = dev_replace->srcdev;
947 dev_replace->tgtdev = NULL;
948 dev_replace->srcdev = NULL;
949 dev_replace->replace_state =
950 BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED;
951 dev_replace->time_stopped = ktime_get_real_seconds();
952 dev_replace->item_needs_writeback = 1;
954 up_write(&dev_replace->rwsem);
956 /* Scrub for replace must not be running in suspended state */
957 ret = btrfs_scrub_cancel(fs_info);
958 ASSERT(ret != -ENOTCONN);
960 trans = btrfs_start_transaction(root, 0);
962 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
963 return PTR_ERR(trans);
965 ret = btrfs_commit_transaction(trans);
968 btrfs_info_in_rcu(fs_info,
969 "suspended dev_replace from %s (devid %llu) to %s canceled",
970 btrfs_dev_name(src_device), src_device->devid,
971 btrfs_dev_name(tgt_device));
974 btrfs_destroy_dev_replace_tgtdev(tgt_device);
977 up_write(&dev_replace->rwsem);
981 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
985 void btrfs_dev_replace_suspend_for_unmount(struct btrfs_fs_info *fs_info)
987 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
989 mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
990 down_write(&dev_replace->rwsem);
992 switch (dev_replace->replace_state) {
993 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
994 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
995 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
996 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
998 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
999 dev_replace->replace_state =
1000 BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
1001 dev_replace->time_stopped = ktime_get_real_seconds();
1002 dev_replace->item_needs_writeback = 1;
1003 btrfs_info(fs_info, "suspending dev_replace for unmount");
1007 up_write(&dev_replace->rwsem);
1008 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
1011 /* resume dev_replace procedure that was interrupted by unmount */
1012 int btrfs_resume_dev_replace_async(struct btrfs_fs_info *fs_info)
1014 struct task_struct *task;
1015 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
1017 down_write(&dev_replace->rwsem);
1019 switch (dev_replace->replace_state) {
1020 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
1021 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
1022 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
1023 up_write(&dev_replace->rwsem);
1025 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
1027 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
1028 dev_replace->replace_state =
1029 BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED;
1032 if (!dev_replace->tgtdev || !dev_replace->tgtdev->bdev) {
1034 "cannot continue dev_replace, tgtdev is missing");
1036 "you may cancel the operation after 'mount -o degraded'");
1037 dev_replace->replace_state =
1038 BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
1039 up_write(&dev_replace->rwsem);
1042 up_write(&dev_replace->rwsem);
1045 * This could collide with a paused balance, but the exclusive op logic
1046 * should never allow both to start and pause. We don't want to allow
1047 * dev-replace to start anyway.
1049 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REPLACE)) {
1050 down_write(&dev_replace->rwsem);
1051 dev_replace->replace_state =
1052 BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
1053 up_write(&dev_replace->rwsem);
1055 "cannot resume dev-replace, other exclusive operation running");
1059 task = kthread_run(btrfs_dev_replace_kthread, fs_info, "btrfs-devrepl");
1060 return PTR_ERR_OR_ZERO(task);
1063 static int btrfs_dev_replace_kthread(void *data)
1065 struct btrfs_fs_info *fs_info = data;
1066 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
1070 progress = btrfs_dev_replace_progress(fs_info);
1071 progress = div_u64(progress, 10);
1072 btrfs_info_in_rcu(fs_info,
1073 "continuing dev_replace from %s (devid %llu) to target %s @%u%%",
1074 btrfs_dev_name(dev_replace->srcdev),
1075 dev_replace->srcdev->devid,
1076 btrfs_dev_name(dev_replace->tgtdev),
1077 (unsigned int)progress);
1079 ret = btrfs_scrub_dev(fs_info, dev_replace->srcdev->devid,
1080 dev_replace->committed_cursor_left,
1081 btrfs_device_get_total_bytes(dev_replace->srcdev),
1082 &dev_replace->scrub_progress, 0, 1);
1083 ret = btrfs_dev_replace_finishing(fs_info, ret);
1084 WARN_ON(ret && ret != -ECANCELED);
1086 btrfs_exclop_finish(fs_info);
1090 int __pure btrfs_dev_replace_is_ongoing(struct btrfs_dev_replace *dev_replace)
1092 if (!dev_replace->is_valid)
1095 switch (dev_replace->replace_state) {
1096 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
1097 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
1098 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
1100 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
1101 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
1103 * return true even if tgtdev is missing (this is
1104 * something that can happen if the dev_replace
1105 * procedure is suspended by an umount and then
1106 * the tgtdev is missing (or "btrfs dev scan") was
1107 * not called and the filesystem is remounted
1108 * in degraded state. This does not stop the
1109 * dev_replace procedure. It needs to be canceled
1110 * manually if the cancellation is wanted.
1117 void btrfs_bio_counter_inc_noblocked(struct btrfs_fs_info *fs_info)
1119 percpu_counter_inc(&fs_info->dev_replace.bio_counter);
1122 void btrfs_bio_counter_sub(struct btrfs_fs_info *fs_info, s64 amount)
1124 percpu_counter_sub(&fs_info->dev_replace.bio_counter, amount);
1125 cond_wake_up_nomb(&fs_info->dev_replace.replace_wait);
1128 void btrfs_bio_counter_inc_blocked(struct btrfs_fs_info *fs_info)
1131 percpu_counter_inc(&fs_info->dev_replace.bio_counter);
1132 if (likely(!test_bit(BTRFS_FS_STATE_DEV_REPLACING,
1133 &fs_info->fs_state)))
1136 btrfs_bio_counter_dec(fs_info);
1137 wait_event(fs_info->dev_replace.replace_wait,
1138 !test_bit(BTRFS_FS_STATE_DEV_REPLACING,
1139 &fs_info->fs_state));