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->replace_state = 0;
63 dev_replace->time_started = 0;
64 dev_replace->time_stopped = 0;
65 atomic64_set(&dev_replace->num_write_errors, 0);
66 atomic64_set(&dev_replace->num_uncorrectable_read_errors, 0);
67 dev_replace->cursor_left = 0;
68 dev_replace->committed_cursor_left = 0;
69 dev_replace->cursor_left_last_write_of_item = 0;
70 dev_replace->cursor_right = 0;
71 dev_replace->srcdev = NULL;
72 dev_replace->tgtdev = NULL;
73 dev_replace->is_valid = 0;
74 dev_replace->item_needs_writeback = 0;
77 slot = path->slots[0];
79 item_size = btrfs_item_size_nr(eb, slot);
80 ptr = btrfs_item_ptr(eb, slot, struct btrfs_dev_replace_item);
82 if (item_size != sizeof(struct btrfs_dev_replace_item)) {
84 "dev_replace entry found has unexpected size, ignore entry");
85 goto no_valid_dev_replace_entry_found;
88 src_devid = btrfs_dev_replace_src_devid(eb, ptr);
89 dev_replace->cont_reading_from_srcdev_mode =
90 btrfs_dev_replace_cont_reading_from_srcdev_mode(eb, ptr);
91 dev_replace->replace_state = btrfs_dev_replace_replace_state(eb, ptr);
92 dev_replace->time_started = btrfs_dev_replace_time_started(eb, ptr);
93 dev_replace->time_stopped =
94 btrfs_dev_replace_time_stopped(eb, ptr);
95 atomic64_set(&dev_replace->num_write_errors,
96 btrfs_dev_replace_num_write_errors(eb, ptr));
97 atomic64_set(&dev_replace->num_uncorrectable_read_errors,
98 btrfs_dev_replace_num_uncorrectable_read_errors(eb, ptr));
99 dev_replace->cursor_left = btrfs_dev_replace_cursor_left(eb, ptr);
100 dev_replace->committed_cursor_left = dev_replace->cursor_left;
101 dev_replace->cursor_left_last_write_of_item = dev_replace->cursor_left;
102 dev_replace->cursor_right = btrfs_dev_replace_cursor_right(eb, ptr);
103 dev_replace->is_valid = 1;
105 dev_replace->item_needs_writeback = 0;
106 switch (dev_replace->replace_state) {
107 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
108 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
109 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
110 dev_replace->srcdev = NULL;
111 dev_replace->tgtdev = NULL;
113 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
114 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
115 dev_replace->srcdev = btrfs_find_device(fs_info, src_devid,
117 dev_replace->tgtdev = btrfs_find_device(fs_info,
118 BTRFS_DEV_REPLACE_DEVID,
121 * allow 'btrfs dev replace_cancel' if src/tgt device is
124 if (!dev_replace->srcdev &&
125 !btrfs_test_opt(fs_info, DEGRADED)) {
128 "cannot mount because device replace operation is ongoing and");
130 "srcdev (devid %llu) is missing, need to run 'btrfs dev scan'?",
133 if (!dev_replace->tgtdev &&
134 !btrfs_test_opt(fs_info, DEGRADED)) {
137 "cannot mount because device replace operation is ongoing and");
139 "tgtdev (devid %llu) is missing, need to run 'btrfs dev scan'?",
140 BTRFS_DEV_REPLACE_DEVID);
142 if (dev_replace->tgtdev) {
143 if (dev_replace->srcdev) {
144 dev_replace->tgtdev->total_bytes =
145 dev_replace->srcdev->total_bytes;
146 dev_replace->tgtdev->disk_total_bytes =
147 dev_replace->srcdev->disk_total_bytes;
148 dev_replace->tgtdev->commit_total_bytes =
149 dev_replace->srcdev->commit_total_bytes;
150 dev_replace->tgtdev->bytes_used =
151 dev_replace->srcdev->bytes_used;
152 dev_replace->tgtdev->commit_bytes_used =
153 dev_replace->srcdev->commit_bytes_used;
155 set_bit(BTRFS_DEV_STATE_REPLACE_TGT,
156 &dev_replace->tgtdev->dev_state);
158 WARN_ON(fs_info->fs_devices->rw_devices == 0);
159 dev_replace->tgtdev->io_width = fs_info->sectorsize;
160 dev_replace->tgtdev->io_align = fs_info->sectorsize;
161 dev_replace->tgtdev->sector_size = fs_info->sectorsize;
162 dev_replace->tgtdev->fs_info = fs_info;
163 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
164 &dev_replace->tgtdev->dev_state);
170 btrfs_free_path(path);
175 * Initialize a new device for device replace target from a given source dev
178 * Return 0 and new device in @device_out, otherwise return < 0
180 static int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
181 const char *device_path,
182 struct btrfs_device *srcdev,
183 struct btrfs_device **device_out)
185 struct btrfs_device *device;
186 struct block_device *bdev;
187 struct list_head *devices;
188 struct rcu_string *name;
189 u64 devid = BTRFS_DEV_REPLACE_DEVID;
193 if (fs_info->fs_devices->seeding) {
194 btrfs_err(fs_info, "the filesystem is a seed filesystem!");
198 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
199 fs_info->bdev_holder);
201 btrfs_err(fs_info, "target device %s is invalid!", device_path);
202 return PTR_ERR(bdev);
205 filemap_write_and_wait(bdev->bd_inode->i_mapping);
207 devices = &fs_info->fs_devices->devices;
208 list_for_each_entry(device, devices, dev_list) {
209 if (device->bdev == bdev) {
211 "target device is in the filesystem!");
218 if (i_size_read(bdev->bd_inode) <
219 btrfs_device_get_total_bytes(srcdev)) {
221 "target device is smaller than source device!");
227 device = btrfs_alloc_device(NULL, &devid, NULL);
228 if (IS_ERR(device)) {
229 ret = PTR_ERR(device);
233 name = rcu_string_strdup(device_path, GFP_KERNEL);
235 btrfs_free_device(device);
239 rcu_assign_pointer(device->name, name);
241 mutex_lock(&fs_info->fs_devices->device_list_mutex);
242 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
243 device->generation = 0;
244 device->io_width = fs_info->sectorsize;
245 device->io_align = fs_info->sectorsize;
246 device->sector_size = fs_info->sectorsize;
247 device->total_bytes = btrfs_device_get_total_bytes(srcdev);
248 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
249 device->bytes_used = btrfs_device_get_bytes_used(srcdev);
250 device->commit_total_bytes = srcdev->commit_total_bytes;
251 device->commit_bytes_used = device->bytes_used;
252 device->fs_info = fs_info;
254 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
255 set_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
256 device->mode = FMODE_EXCL;
257 device->dev_stats_valid = 1;
258 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
259 device->fs_devices = fs_info->fs_devices;
260 list_add(&device->dev_list, &fs_info->fs_devices->devices);
261 fs_info->fs_devices->num_devices++;
262 fs_info->fs_devices->open_devices++;
263 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
265 *device_out = device;
269 blkdev_put(bdev, FMODE_EXCL);
274 * called from commit_transaction. Writes changed device replace state to
277 int btrfs_run_dev_replace(struct btrfs_trans_handle *trans,
278 struct btrfs_fs_info *fs_info)
281 struct btrfs_root *dev_root = fs_info->dev_root;
282 struct btrfs_path *path;
283 struct btrfs_key key;
284 struct extent_buffer *eb;
285 struct btrfs_dev_replace_item *ptr;
286 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
288 btrfs_dev_replace_read_lock(dev_replace);
289 if (!dev_replace->is_valid ||
290 !dev_replace->item_needs_writeback) {
291 btrfs_dev_replace_read_unlock(dev_replace);
294 btrfs_dev_replace_read_unlock(dev_replace);
297 key.type = BTRFS_DEV_REPLACE_KEY;
300 path = btrfs_alloc_path();
305 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
308 "error %d while searching for dev_replace item!",
314 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
316 * need to delete old one and insert a new one.
317 * Since no attempt is made to recover any old state, if the
318 * dev_replace state is 'running', the data on the target
320 * It would be possible to recover the state: just make sure
321 * that the beginning of the item is never changed and always
322 * contains all the essential information. Then read this
323 * minimal set of information and use it as a base for the
326 ret = btrfs_del_item(trans, dev_root, path);
329 "delete too small dev_replace item failed %d!",
337 /* need to insert a new item */
338 btrfs_release_path(path);
339 ret = btrfs_insert_empty_item(trans, dev_root, path,
343 "insert dev_replace item failed %d!", ret);
349 ptr = btrfs_item_ptr(eb, path->slots[0],
350 struct btrfs_dev_replace_item);
352 btrfs_dev_replace_write_lock(dev_replace);
353 if (dev_replace->srcdev)
354 btrfs_set_dev_replace_src_devid(eb, ptr,
355 dev_replace->srcdev->devid);
357 btrfs_set_dev_replace_src_devid(eb, ptr, (u64)-1);
358 btrfs_set_dev_replace_cont_reading_from_srcdev_mode(eb, ptr,
359 dev_replace->cont_reading_from_srcdev_mode);
360 btrfs_set_dev_replace_replace_state(eb, ptr,
361 dev_replace->replace_state);
362 btrfs_set_dev_replace_time_started(eb, ptr, dev_replace->time_started);
363 btrfs_set_dev_replace_time_stopped(eb, ptr, dev_replace->time_stopped);
364 btrfs_set_dev_replace_num_write_errors(eb, ptr,
365 atomic64_read(&dev_replace->num_write_errors));
366 btrfs_set_dev_replace_num_uncorrectable_read_errors(eb, ptr,
367 atomic64_read(&dev_replace->num_uncorrectable_read_errors));
368 dev_replace->cursor_left_last_write_of_item =
369 dev_replace->cursor_left;
370 btrfs_set_dev_replace_cursor_left(eb, ptr,
371 dev_replace->cursor_left_last_write_of_item);
372 btrfs_set_dev_replace_cursor_right(eb, ptr,
373 dev_replace->cursor_right);
374 dev_replace->item_needs_writeback = 0;
375 btrfs_dev_replace_write_unlock(dev_replace);
377 btrfs_mark_buffer_dirty(eb);
380 btrfs_free_path(path);
385 static char* btrfs_dev_name(struct btrfs_device *device)
387 if (!device || test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
388 return "<missing disk>";
390 return rcu_str_deref(device->name);
393 int btrfs_dev_replace_start(struct btrfs_fs_info *fs_info,
394 const char *tgtdev_name, u64 srcdevid, const char *srcdev_name,
397 struct btrfs_root *root = fs_info->dev_root;
398 struct btrfs_trans_handle *trans;
399 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
401 struct btrfs_device *tgt_device = NULL;
402 struct btrfs_device *src_device = NULL;
405 src_device = btrfs_find_device_by_devspec(fs_info, srcdevid,
407 if (IS_ERR(src_device))
408 return PTR_ERR(src_device);
410 if (btrfs_pinned_by_swapfile(fs_info, src_device)) {
411 btrfs_warn_in_rcu(fs_info,
412 "cannot replace device %s (devid %llu) due to active swapfile",
413 btrfs_dev_name(src_device), src_device->devid);
417 ret = btrfs_init_dev_replace_tgtdev(fs_info, tgtdev_name,
418 src_device, &tgt_device);
423 * Here we commit the transaction to make sure commit_total_bytes
424 * of all the devices are updated.
426 trans = btrfs_attach_transaction(root);
427 if (!IS_ERR(trans)) {
428 ret = btrfs_commit_transaction(trans);
431 } else if (PTR_ERR(trans) != -ENOENT) {
432 return PTR_ERR(trans);
436 btrfs_dev_replace_write_lock(dev_replace);
437 switch (dev_replace->replace_state) {
438 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
439 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
440 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
442 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
443 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
445 ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_ALREADY_STARTED;
449 dev_replace->cont_reading_from_srcdev_mode = read_src;
450 WARN_ON(!src_device);
451 dev_replace->srcdev = src_device;
452 dev_replace->tgtdev = tgt_device;
454 btrfs_info_in_rcu(fs_info,
455 "dev_replace from %s (devid %llu) to %s started",
456 btrfs_dev_name(src_device),
458 rcu_str_deref(tgt_device->name));
461 * from now on, the writes to the srcdev are all duplicated to
462 * go to the tgtdev as well (refer to btrfs_map_block()).
464 dev_replace->replace_state = BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED;
465 dev_replace->time_started = ktime_get_real_seconds();
466 dev_replace->cursor_left = 0;
467 dev_replace->committed_cursor_left = 0;
468 dev_replace->cursor_left_last_write_of_item = 0;
469 dev_replace->cursor_right = 0;
470 dev_replace->is_valid = 1;
471 dev_replace->item_needs_writeback = 1;
472 atomic64_set(&dev_replace->num_write_errors, 0);
473 atomic64_set(&dev_replace->num_uncorrectable_read_errors, 0);
474 btrfs_dev_replace_write_unlock(dev_replace);
477 ret = btrfs_sysfs_add_device_link(tgt_device->fs_devices, tgt_device);
479 btrfs_err(fs_info, "kobj add dev failed %d", ret);
481 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
483 /* force writing the updated state information to disk */
484 trans = btrfs_start_transaction(root, 0);
486 ret = PTR_ERR(trans);
488 btrfs_dev_replace_write_lock(dev_replace);
489 dev_replace->replace_state =
490 BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED;
491 dev_replace->srcdev = NULL;
492 dev_replace->tgtdev = NULL;
496 ret = btrfs_commit_transaction(trans);
499 /* the disk copy procedure reuses the scrub code */
500 ret = btrfs_scrub_dev(fs_info, src_device->devid, 0,
501 btrfs_device_get_total_bytes(src_device),
502 &dev_replace->scrub_progress, 0, 1);
504 ret = btrfs_dev_replace_finishing(fs_info, ret);
505 if (ret == -EINPROGRESS) {
506 ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS;
515 btrfs_dev_replace_write_unlock(dev_replace);
516 btrfs_destroy_dev_replace_tgtdev(tgt_device);
520 int btrfs_dev_replace_by_ioctl(struct btrfs_fs_info *fs_info,
521 struct btrfs_ioctl_dev_replace_args *args)
525 switch (args->start.cont_reading_from_srcdev_mode) {
526 case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_ALWAYS:
527 case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_AVOID:
533 if ((args->start.srcdevid == 0 && args->start.srcdev_name[0] == '\0') ||
534 args->start.tgtdev_name[0] == '\0')
537 ret = btrfs_dev_replace_start(fs_info, args->start.tgtdev_name,
538 args->start.srcdevid,
539 args->start.srcdev_name,
540 args->start.cont_reading_from_srcdev_mode);
542 /* don't warn if EINPROGRESS, someone else might be running scrub */
543 if (ret == BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS)
550 * blocked until all in-flight bios operations are finished.
552 static void btrfs_rm_dev_replace_blocked(struct btrfs_fs_info *fs_info)
554 set_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state);
555 wait_event(fs_info->dev_replace.replace_wait, !percpu_counter_sum(
556 &fs_info->dev_replace.bio_counter));
560 * we have removed target device, it is safe to allow new bios request.
562 static void btrfs_rm_dev_replace_unblocked(struct btrfs_fs_info *fs_info)
564 clear_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state);
565 wake_up(&fs_info->dev_replace.replace_wait);
568 static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
571 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
572 struct btrfs_device *tgt_device;
573 struct btrfs_device *src_device;
574 struct btrfs_root *root = fs_info->tree_root;
575 u8 uuid_tmp[BTRFS_UUID_SIZE];
576 struct btrfs_trans_handle *trans;
579 /* don't allow cancel or unmount to disturb the finishing procedure */
580 mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
582 btrfs_dev_replace_read_lock(dev_replace);
583 /* was the operation canceled, or is it finished? */
584 if (dev_replace->replace_state !=
585 BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED) {
586 btrfs_dev_replace_read_unlock(dev_replace);
587 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
591 tgt_device = dev_replace->tgtdev;
592 src_device = dev_replace->srcdev;
593 btrfs_dev_replace_read_unlock(dev_replace);
596 * flush all outstanding I/O and inode extent mappings before the
597 * copy operation is declared as being finished
599 ret = btrfs_start_delalloc_roots(fs_info, -1);
601 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
604 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
606 trans = btrfs_start_transaction(root, 0);
608 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
609 return PTR_ERR(trans);
611 ret = btrfs_commit_transaction(trans);
614 /* keep away write_all_supers() during the finishing procedure */
615 mutex_lock(&fs_info->fs_devices->device_list_mutex);
616 mutex_lock(&fs_info->chunk_mutex);
617 btrfs_dev_replace_write_lock(dev_replace);
618 dev_replace->replace_state =
619 scrub_ret ? BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED
620 : BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED;
621 dev_replace->tgtdev = NULL;
622 dev_replace->srcdev = NULL;
623 dev_replace->time_stopped = ktime_get_real_seconds();
624 dev_replace->item_needs_writeback = 1;
626 /* replace old device with new one in mapping tree */
628 btrfs_dev_replace_update_device_in_mapping_tree(fs_info,
632 btrfs_err_in_rcu(fs_info,
633 "btrfs_scrub_dev(%s, %llu, %s) failed %d",
634 btrfs_dev_name(src_device),
636 rcu_str_deref(tgt_device->name), scrub_ret);
637 btrfs_dev_replace_write_unlock(dev_replace);
638 mutex_unlock(&fs_info->chunk_mutex);
639 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
640 btrfs_rm_dev_replace_blocked(fs_info);
642 btrfs_destroy_dev_replace_tgtdev(tgt_device);
643 btrfs_rm_dev_replace_unblocked(fs_info);
644 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
649 btrfs_info_in_rcu(fs_info,
650 "dev_replace from %s (devid %llu) to %s finished",
651 btrfs_dev_name(src_device),
653 rcu_str_deref(tgt_device->name));
654 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &tgt_device->dev_state);
655 tgt_device->devid = src_device->devid;
656 src_device->devid = BTRFS_DEV_REPLACE_DEVID;
657 memcpy(uuid_tmp, tgt_device->uuid, sizeof(uuid_tmp));
658 memcpy(tgt_device->uuid, src_device->uuid, sizeof(tgt_device->uuid));
659 memcpy(src_device->uuid, uuid_tmp, sizeof(src_device->uuid));
660 btrfs_device_set_total_bytes(tgt_device, src_device->total_bytes);
661 btrfs_device_set_disk_total_bytes(tgt_device,
662 src_device->disk_total_bytes);
663 btrfs_device_set_bytes_used(tgt_device, src_device->bytes_used);
664 ASSERT(list_empty(&src_device->resized_list));
665 tgt_device->commit_total_bytes = src_device->commit_total_bytes;
666 tgt_device->commit_bytes_used = src_device->bytes_used;
668 btrfs_assign_next_active_device(src_device, tgt_device);
670 list_add(&tgt_device->dev_alloc_list, &fs_info->fs_devices->alloc_list);
671 fs_info->fs_devices->rw_devices++;
673 btrfs_dev_replace_write_unlock(dev_replace);
675 btrfs_rm_dev_replace_blocked(fs_info);
677 btrfs_rm_dev_replace_remove_srcdev(src_device);
679 btrfs_rm_dev_replace_unblocked(fs_info);
682 * Increment dev_stats_ccnt so that btrfs_run_dev_stats() will
683 * update on-disk dev stats value during commit transaction
685 atomic_inc(&tgt_device->dev_stats_ccnt);
688 * this is again a consistent state where no dev_replace procedure
689 * is running, the target device is part of the filesystem, the
690 * source device is not part of the filesystem anymore and its 1st
691 * superblock is scratched out so that it is no longer marked to
692 * belong to this filesystem.
694 mutex_unlock(&fs_info->chunk_mutex);
695 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
697 /* replace the sysfs entry */
698 btrfs_sysfs_rm_device_link(fs_info->fs_devices, src_device);
699 btrfs_rm_dev_replace_free_srcdev(fs_info, src_device);
701 /* write back the superblocks */
702 trans = btrfs_start_transaction(root, 0);
704 btrfs_commit_transaction(trans);
706 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
711 static void btrfs_dev_replace_update_device_in_mapping_tree(
712 struct btrfs_fs_info *fs_info,
713 struct btrfs_device *srcdev,
714 struct btrfs_device *tgtdev)
716 struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
717 struct extent_map *em;
718 struct map_lookup *map;
722 write_lock(&em_tree->lock);
724 em = lookup_extent_mapping(em_tree, start, (u64)-1);
727 map = em->map_lookup;
728 for (i = 0; i < map->num_stripes; i++)
729 if (srcdev == map->stripes[i].dev)
730 map->stripes[i].dev = tgtdev;
731 start = em->start + em->len;
734 write_unlock(&em_tree->lock);
738 * Read progress of device replace status according to the state and last
739 * stored position. The value format is the same as for
740 * btrfs_dev_replace::progress_1000
742 static u64 btrfs_dev_replace_progress(struct btrfs_fs_info *fs_info)
744 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
747 switch (dev_replace->replace_state) {
748 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
749 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
752 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
755 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
756 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
757 ret = div64_u64(dev_replace->cursor_left,
758 div_u64(btrfs_device_get_total_bytes(
759 dev_replace->srcdev), 1000));
766 void btrfs_dev_replace_status(struct btrfs_fs_info *fs_info,
767 struct btrfs_ioctl_dev_replace_args *args)
769 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
771 btrfs_dev_replace_read_lock(dev_replace);
772 /* even if !dev_replace_is_valid, the values are good enough for
773 * the replace_status ioctl */
774 args->result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
775 args->status.replace_state = dev_replace->replace_state;
776 args->status.time_started = dev_replace->time_started;
777 args->status.time_stopped = dev_replace->time_stopped;
778 args->status.num_write_errors =
779 atomic64_read(&dev_replace->num_write_errors);
780 args->status.num_uncorrectable_read_errors =
781 atomic64_read(&dev_replace->num_uncorrectable_read_errors);
782 args->status.progress_1000 = btrfs_dev_replace_progress(fs_info);
783 btrfs_dev_replace_read_unlock(dev_replace);
786 int btrfs_dev_replace_cancel(struct btrfs_fs_info *fs_info)
788 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
789 struct btrfs_device *tgt_device = NULL;
790 struct btrfs_device *src_device = NULL;
791 struct btrfs_trans_handle *trans;
792 struct btrfs_root *root = fs_info->tree_root;
796 if (sb_rdonly(fs_info->sb))
799 mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
800 btrfs_dev_replace_write_lock(dev_replace);
801 switch (dev_replace->replace_state) {
802 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
803 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
804 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
805 result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED;
806 btrfs_dev_replace_write_unlock(dev_replace);
808 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
809 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
810 result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
811 tgt_device = dev_replace->tgtdev;
812 src_device = dev_replace->srcdev;
813 dev_replace->tgtdev = NULL;
814 dev_replace->srcdev = NULL;
817 dev_replace->replace_state = BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED;
818 dev_replace->time_stopped = ktime_get_real_seconds();
819 dev_replace->item_needs_writeback = 1;
820 btrfs_dev_replace_write_unlock(dev_replace);
821 btrfs_scrub_cancel(fs_info);
823 trans = btrfs_start_transaction(root, 0);
825 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
826 return PTR_ERR(trans);
828 ret = btrfs_commit_transaction(trans);
831 btrfs_info_in_rcu(fs_info,
832 "dev_replace from %s (devid %llu) to %s canceled",
833 btrfs_dev_name(src_device), src_device->devid,
834 btrfs_dev_name(tgt_device));
837 btrfs_destroy_dev_replace_tgtdev(tgt_device);
840 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
844 void btrfs_dev_replace_suspend_for_unmount(struct btrfs_fs_info *fs_info)
846 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
848 mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
849 btrfs_dev_replace_write_lock(dev_replace);
850 switch (dev_replace->replace_state) {
851 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
852 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
853 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
854 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
856 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
857 dev_replace->replace_state =
858 BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
859 dev_replace->time_stopped = ktime_get_real_seconds();
860 dev_replace->item_needs_writeback = 1;
861 btrfs_info(fs_info, "suspending dev_replace for unmount");
865 btrfs_dev_replace_write_unlock(dev_replace);
866 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
869 /* resume dev_replace procedure that was interrupted by unmount */
870 int btrfs_resume_dev_replace_async(struct btrfs_fs_info *fs_info)
872 struct task_struct *task;
873 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
875 btrfs_dev_replace_write_lock(dev_replace);
876 switch (dev_replace->replace_state) {
877 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
878 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
879 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
880 btrfs_dev_replace_write_unlock(dev_replace);
882 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
884 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
885 dev_replace->replace_state =
886 BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED;
889 if (!dev_replace->tgtdev || !dev_replace->tgtdev->bdev) {
891 "cannot continue dev_replace, tgtdev is missing");
893 "you may cancel the operation after 'mount -o degraded'");
894 btrfs_dev_replace_write_unlock(dev_replace);
897 btrfs_dev_replace_write_unlock(dev_replace);
900 * This could collide with a paused balance, but the exclusive op logic
901 * should never allow both to start and pause. We don't want to allow
902 * dev-replace to start anyway.
904 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
906 "cannot resume dev-replace, other exclusive operation running");
910 task = kthread_run(btrfs_dev_replace_kthread, fs_info, "btrfs-devrepl");
911 return PTR_ERR_OR_ZERO(task);
914 static int btrfs_dev_replace_kthread(void *data)
916 struct btrfs_fs_info *fs_info = data;
917 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
921 progress = btrfs_dev_replace_progress(fs_info);
922 progress = div_u64(progress, 10);
923 btrfs_info_in_rcu(fs_info,
924 "continuing dev_replace from %s (devid %llu) to target %s @%u%%",
925 btrfs_dev_name(dev_replace->srcdev),
926 dev_replace->srcdev->devid,
927 btrfs_dev_name(dev_replace->tgtdev),
928 (unsigned int)progress);
930 ret = btrfs_scrub_dev(fs_info, dev_replace->srcdev->devid,
931 dev_replace->committed_cursor_left,
932 btrfs_device_get_total_bytes(dev_replace->srcdev),
933 &dev_replace->scrub_progress, 0, 1);
934 ret = btrfs_dev_replace_finishing(fs_info, ret);
937 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
941 int btrfs_dev_replace_is_ongoing(struct btrfs_dev_replace *dev_replace)
943 if (!dev_replace->is_valid)
946 switch (dev_replace->replace_state) {
947 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
948 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
949 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
951 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
952 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
954 * return true even if tgtdev is missing (this is
955 * something that can happen if the dev_replace
956 * procedure is suspended by an umount and then
957 * the tgtdev is missing (or "btrfs dev scan") was
958 * not called and the the filesystem is remounted
959 * in degraded state. This does not stop the
960 * dev_replace procedure. It needs to be canceled
961 * manually if the cancellation is wanted.
968 void btrfs_dev_replace_read_lock(struct btrfs_dev_replace *dev_replace)
970 read_lock(&dev_replace->lock);
973 void btrfs_dev_replace_read_unlock(struct btrfs_dev_replace *dev_replace)
975 read_unlock(&dev_replace->lock);
978 void btrfs_dev_replace_write_lock(struct btrfs_dev_replace *dev_replace)
981 wait_event(dev_replace->read_lock_wq,
982 atomic_read(&dev_replace->blocking_readers) == 0);
983 write_lock(&dev_replace->lock);
984 if (atomic_read(&dev_replace->blocking_readers)) {
985 write_unlock(&dev_replace->lock);
990 void btrfs_dev_replace_write_unlock(struct btrfs_dev_replace *dev_replace)
992 write_unlock(&dev_replace->lock);
995 /* inc blocking cnt and release read lock */
996 void btrfs_dev_replace_set_lock_blocking(
997 struct btrfs_dev_replace *dev_replace)
999 /* only set blocking for read lock */
1000 atomic_inc(&dev_replace->blocking_readers);
1001 read_unlock(&dev_replace->lock);
1004 void btrfs_bio_counter_inc_noblocked(struct btrfs_fs_info *fs_info)
1006 percpu_counter_inc(&fs_info->dev_replace.bio_counter);
1009 void btrfs_bio_counter_sub(struct btrfs_fs_info *fs_info, s64 amount)
1011 percpu_counter_sub(&fs_info->dev_replace.bio_counter, amount);
1012 cond_wake_up_nomb(&fs_info->dev_replace.replace_wait);
1015 void btrfs_bio_counter_inc_blocked(struct btrfs_fs_info *fs_info)
1018 percpu_counter_inc(&fs_info->dev_replace.bio_counter);
1019 if (likely(!test_bit(BTRFS_FS_STATE_DEV_REPLACING,
1020 &fs_info->fs_state)))
1023 btrfs_bio_counter_dec(fs_info);
1024 wait_event(fs_info->dev_replace.replace_wait,
1025 !test_bit(BTRFS_FS_STATE_DEV_REPLACING,
1026 &fs_info->fs_state));