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
->fs_devices
,
115 src_devid
, NULL
, NULL
, true);
116 dev_replace
->tgtdev
= btrfs_find_device(fs_info
->fs_devices
,
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
);
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 set_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
);
241 device
->generation
= 0;
242 device
->io_width
= fs_info
->sectorsize
;
243 device
->io_align
= fs_info
->sectorsize
;
244 device
->sector_size
= fs_info
->sectorsize
;
245 device
->total_bytes
= btrfs_device_get_total_bytes(srcdev
);
246 device
->disk_total_bytes
= btrfs_device_get_disk_total_bytes(srcdev
);
247 device
->bytes_used
= btrfs_device_get_bytes_used(srcdev
);
248 device
->commit_total_bytes
= srcdev
->commit_total_bytes
;
249 device
->commit_bytes_used
= device
->bytes_used
;
250 device
->fs_info
= fs_info
;
252 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA
, &device
->dev_state
);
253 set_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
);
254 device
->mode
= FMODE_EXCL
;
255 device
->dev_stats_valid
= 1;
256 set_blocksize(device
->bdev
, BTRFS_BDEV_BLOCKSIZE
);
257 device
->fs_devices
= fs_info
->fs_devices
;
259 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
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
)
279 struct btrfs_fs_info
*fs_info
= trans
->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 down_read(&dev_replace
->rwsem
);
289 if (!dev_replace
->is_valid
||
290 !dev_replace
->item_needs_writeback
) {
291 up_read(&dev_replace
->rwsem
);
294 up_read(&dev_replace
->rwsem
);
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 down_write(&dev_replace
->rwsem
);
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 up_write(&dev_replace
->rwsem
);
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 static 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
;
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
);
417 * Here we commit the transaction to make sure commit_total_bytes
418 * of all the devices are updated.
420 trans
= btrfs_attach_transaction(root
);
421 if (!IS_ERR(trans
)) {
422 ret
= btrfs_commit_transaction(trans
);
425 } else if (PTR_ERR(trans
) != -ENOENT
) {
426 return PTR_ERR(trans
);
429 ret
= btrfs_init_dev_replace_tgtdev(fs_info
, tgtdev_name
,
430 src_device
, &tgt_device
);
434 down_write(&dev_replace
->rwsem
);
435 switch (dev_replace
->replace_state
) {
436 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED
:
437 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED
:
438 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED
:
440 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED
:
441 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED
:
443 ret
= BTRFS_IOCTL_DEV_REPLACE_RESULT_ALREADY_STARTED
;
444 up_write(&dev_replace
->rwsem
);
448 dev_replace
->cont_reading_from_srcdev_mode
= read_src
;
449 dev_replace
->srcdev
= src_device
;
450 dev_replace
->tgtdev
= tgt_device
;
452 btrfs_info_in_rcu(fs_info
,
453 "dev_replace from %s (devid %llu) to %s started",
454 btrfs_dev_name(src_device
),
456 rcu_str_deref(tgt_device
->name
));
459 * from now on, the writes to the srcdev are all duplicated to
460 * go to the tgtdev as well (refer to btrfs_map_block()).
462 dev_replace
->replace_state
= BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED
;
463 dev_replace
->time_started
= ktime_get_real_seconds();
464 dev_replace
->cursor_left
= 0;
465 dev_replace
->committed_cursor_left
= 0;
466 dev_replace
->cursor_left_last_write_of_item
= 0;
467 dev_replace
->cursor_right
= 0;
468 dev_replace
->is_valid
= 1;
469 dev_replace
->item_needs_writeback
= 1;
470 atomic64_set(&dev_replace
->num_write_errors
, 0);
471 atomic64_set(&dev_replace
->num_uncorrectable_read_errors
, 0);
472 up_write(&dev_replace
->rwsem
);
474 ret
= btrfs_sysfs_add_device_link(tgt_device
->fs_devices
, tgt_device
);
476 btrfs_err(fs_info
, "kobj add dev failed %d", ret
);
478 btrfs_wait_ordered_roots(fs_info
, U64_MAX
, 0, (u64
)-1);
480 /* Commit dev_replace state and reserve 1 item for it. */
481 trans
= btrfs_start_transaction(root
, 1);
483 ret
= PTR_ERR(trans
);
484 down_write(&dev_replace
->rwsem
);
485 dev_replace
->replace_state
=
486 BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED
;
487 dev_replace
->srcdev
= NULL
;
488 dev_replace
->tgtdev
= NULL
;
489 up_write(&dev_replace
->rwsem
);
493 ret
= btrfs_commit_transaction(trans
);
496 /* the disk copy procedure reuses the scrub code */
497 ret
= btrfs_scrub_dev(fs_info
, src_device
->devid
, 0,
498 btrfs_device_get_total_bytes(src_device
),
499 &dev_replace
->scrub_progress
, 0, 1);
501 ret
= btrfs_dev_replace_finishing(fs_info
, ret
);
502 if (ret
== -EINPROGRESS
) {
503 ret
= BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS
;
504 } else if (ret
!= -ECANCELED
) {
511 btrfs_destroy_dev_replace_tgtdev(tgt_device
);
515 int btrfs_dev_replace_by_ioctl(struct btrfs_fs_info
*fs_info
,
516 struct btrfs_ioctl_dev_replace_args
*args
)
520 switch (args
->start
.cont_reading_from_srcdev_mode
) {
521 case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_ALWAYS
:
522 case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_AVOID
:
528 if ((args
->start
.srcdevid
== 0 && args
->start
.srcdev_name
[0] == '\0') ||
529 args
->start
.tgtdev_name
[0] == '\0')
532 ret
= btrfs_dev_replace_start(fs_info
, args
->start
.tgtdev_name
,
533 args
->start
.srcdevid
,
534 args
->start
.srcdev_name
,
535 args
->start
.cont_reading_from_srcdev_mode
);
537 /* don't warn if EINPROGRESS, someone else might be running scrub */
538 if (ret
== BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS
||
539 ret
== BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR
)
546 * blocked until all in-flight bios operations are finished.
548 static void btrfs_rm_dev_replace_blocked(struct btrfs_fs_info
*fs_info
)
550 set_bit(BTRFS_FS_STATE_DEV_REPLACING
, &fs_info
->fs_state
);
551 wait_event(fs_info
->dev_replace
.replace_wait
, !percpu_counter_sum(
552 &fs_info
->dev_replace
.bio_counter
));
556 * we have removed target device, it is safe to allow new bios request.
558 static void btrfs_rm_dev_replace_unblocked(struct btrfs_fs_info
*fs_info
)
560 clear_bit(BTRFS_FS_STATE_DEV_REPLACING
, &fs_info
->fs_state
);
561 wake_up(&fs_info
->dev_replace
.replace_wait
);
564 static int btrfs_dev_replace_finishing(struct btrfs_fs_info
*fs_info
,
567 struct btrfs_dev_replace
*dev_replace
= &fs_info
->dev_replace
;
568 struct btrfs_device
*tgt_device
;
569 struct btrfs_device
*src_device
;
570 struct btrfs_root
*root
= fs_info
->tree_root
;
571 u8 uuid_tmp
[BTRFS_UUID_SIZE
];
572 struct btrfs_trans_handle
*trans
;
575 /* don't allow cancel or unmount to disturb the finishing procedure */
576 mutex_lock(&dev_replace
->lock_finishing_cancel_unmount
);
578 down_read(&dev_replace
->rwsem
);
579 /* was the operation canceled, or is it finished? */
580 if (dev_replace
->replace_state
!=
581 BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED
) {
582 up_read(&dev_replace
->rwsem
);
583 mutex_unlock(&dev_replace
->lock_finishing_cancel_unmount
);
587 tgt_device
= dev_replace
->tgtdev
;
588 src_device
= dev_replace
->srcdev
;
589 up_read(&dev_replace
->rwsem
);
592 * flush all outstanding I/O and inode extent mappings before the
593 * copy operation is declared as being finished
595 ret
= btrfs_start_delalloc_roots(fs_info
, -1);
597 mutex_unlock(&dev_replace
->lock_finishing_cancel_unmount
);
600 btrfs_wait_ordered_roots(fs_info
, U64_MAX
, 0, (u64
)-1);
603 * We have to use this loop approach because at this point src_device
604 * has to be available for transaction commit to complete, yet new
605 * chunks shouldn't be allocated on the device.
608 trans
= btrfs_start_transaction(root
, 0);
610 mutex_unlock(&dev_replace
->lock_finishing_cancel_unmount
);
611 return PTR_ERR(trans
);
613 ret
= btrfs_commit_transaction(trans
);
616 /* Prevent write_all_supers() during the finishing procedure */
617 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
618 /* Prevent new chunks being allocated on the source device */
619 mutex_lock(&fs_info
->chunk_mutex
);
621 if (!list_empty(&src_device
->post_commit_list
)) {
622 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
623 mutex_unlock(&fs_info
->chunk_mutex
);
629 down_write(&dev_replace
->rwsem
);
630 dev_replace
->replace_state
=
631 scrub_ret
? BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED
632 : BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED
;
633 dev_replace
->tgtdev
= NULL
;
634 dev_replace
->srcdev
= NULL
;
635 dev_replace
->time_stopped
= ktime_get_real_seconds();
636 dev_replace
->item_needs_writeback
= 1;
638 /* replace old device with new one in mapping tree */
640 btrfs_dev_replace_update_device_in_mapping_tree(fs_info
,
644 if (scrub_ret
!= -ECANCELED
)
645 btrfs_err_in_rcu(fs_info
,
646 "btrfs_scrub_dev(%s, %llu, %s) failed %d",
647 btrfs_dev_name(src_device
),
649 rcu_str_deref(tgt_device
->name
), scrub_ret
);
650 up_write(&dev_replace
->rwsem
);
651 mutex_unlock(&fs_info
->chunk_mutex
);
652 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
653 btrfs_rm_dev_replace_blocked(fs_info
);
655 btrfs_destroy_dev_replace_tgtdev(tgt_device
);
656 btrfs_rm_dev_replace_unblocked(fs_info
);
657 mutex_unlock(&dev_replace
->lock_finishing_cancel_unmount
);
662 btrfs_info_in_rcu(fs_info
,
663 "dev_replace from %s (devid %llu) to %s finished",
664 btrfs_dev_name(src_device
),
666 rcu_str_deref(tgt_device
->name
));
667 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &tgt_device
->dev_state
);
668 tgt_device
->devid
= src_device
->devid
;
669 src_device
->devid
= BTRFS_DEV_REPLACE_DEVID
;
670 memcpy(uuid_tmp
, tgt_device
->uuid
, sizeof(uuid_tmp
));
671 memcpy(tgt_device
->uuid
, src_device
->uuid
, sizeof(tgt_device
->uuid
));
672 memcpy(src_device
->uuid
, uuid_tmp
, sizeof(src_device
->uuid
));
673 btrfs_device_set_total_bytes(tgt_device
, src_device
->total_bytes
);
674 btrfs_device_set_disk_total_bytes(tgt_device
,
675 src_device
->disk_total_bytes
);
676 btrfs_device_set_bytes_used(tgt_device
, src_device
->bytes_used
);
677 tgt_device
->commit_bytes_used
= src_device
->bytes_used
;
679 btrfs_assign_next_active_device(src_device
, tgt_device
);
681 list_add(&tgt_device
->dev_alloc_list
, &fs_info
->fs_devices
->alloc_list
);
682 fs_info
->fs_devices
->rw_devices
++;
684 up_write(&dev_replace
->rwsem
);
685 btrfs_rm_dev_replace_blocked(fs_info
);
687 btrfs_rm_dev_replace_remove_srcdev(src_device
);
689 btrfs_rm_dev_replace_unblocked(fs_info
);
692 * Increment dev_stats_ccnt so that btrfs_run_dev_stats() will
693 * update on-disk dev stats value during commit transaction
695 atomic_inc(&tgt_device
->dev_stats_ccnt
);
698 * this is again a consistent state where no dev_replace procedure
699 * is running, the target device is part of the filesystem, the
700 * source device is not part of the filesystem anymore and its 1st
701 * superblock is scratched out so that it is no longer marked to
702 * belong to this filesystem.
704 mutex_unlock(&fs_info
->chunk_mutex
);
705 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
707 /* replace the sysfs entry */
708 btrfs_sysfs_rm_device_link(fs_info
->fs_devices
, src_device
);
709 btrfs_rm_dev_replace_free_srcdev(src_device
);
711 /* write back the superblocks */
712 trans
= btrfs_start_transaction(root
, 0);
714 btrfs_commit_transaction(trans
);
716 mutex_unlock(&dev_replace
->lock_finishing_cancel_unmount
);
721 static void btrfs_dev_replace_update_device_in_mapping_tree(
722 struct btrfs_fs_info
*fs_info
,
723 struct btrfs_device
*srcdev
,
724 struct btrfs_device
*tgtdev
)
726 struct extent_map_tree
*em_tree
= &fs_info
->mapping_tree
;
727 struct extent_map
*em
;
728 struct map_lookup
*map
;
732 write_lock(&em_tree
->lock
);
734 em
= lookup_extent_mapping(em_tree
, start
, (u64
)-1);
737 map
= em
->map_lookup
;
738 for (i
= 0; i
< map
->num_stripes
; i
++)
739 if (srcdev
== map
->stripes
[i
].dev
)
740 map
->stripes
[i
].dev
= tgtdev
;
741 start
= em
->start
+ em
->len
;
744 write_unlock(&em_tree
->lock
);
748 * Read progress of device replace status according to the state and last
749 * stored position. The value format is the same as for
750 * btrfs_dev_replace::progress_1000
752 static u64
btrfs_dev_replace_progress(struct btrfs_fs_info
*fs_info
)
754 struct btrfs_dev_replace
*dev_replace
= &fs_info
->dev_replace
;
757 switch (dev_replace
->replace_state
) {
758 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED
:
759 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED
:
762 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED
:
765 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED
:
766 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED
:
767 ret
= div64_u64(dev_replace
->cursor_left
,
768 div_u64(btrfs_device_get_total_bytes(
769 dev_replace
->srcdev
), 1000));
776 void btrfs_dev_replace_status(struct btrfs_fs_info
*fs_info
,
777 struct btrfs_ioctl_dev_replace_args
*args
)
779 struct btrfs_dev_replace
*dev_replace
= &fs_info
->dev_replace
;
781 down_read(&dev_replace
->rwsem
);
782 /* even if !dev_replace_is_valid, the values are good enough for
783 * the replace_status ioctl */
784 args
->result
= BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR
;
785 args
->status
.replace_state
= dev_replace
->replace_state
;
786 args
->status
.time_started
= dev_replace
->time_started
;
787 args
->status
.time_stopped
= dev_replace
->time_stopped
;
788 args
->status
.num_write_errors
=
789 atomic64_read(&dev_replace
->num_write_errors
);
790 args
->status
.num_uncorrectable_read_errors
=
791 atomic64_read(&dev_replace
->num_uncorrectable_read_errors
);
792 args
->status
.progress_1000
= btrfs_dev_replace_progress(fs_info
);
793 up_read(&dev_replace
->rwsem
);
796 int btrfs_dev_replace_cancel(struct btrfs_fs_info
*fs_info
)
798 struct btrfs_dev_replace
*dev_replace
= &fs_info
->dev_replace
;
799 struct btrfs_device
*tgt_device
= NULL
;
800 struct btrfs_device
*src_device
= NULL
;
801 struct btrfs_trans_handle
*trans
;
802 struct btrfs_root
*root
= fs_info
->tree_root
;
806 if (sb_rdonly(fs_info
->sb
))
809 mutex_lock(&dev_replace
->lock_finishing_cancel_unmount
);
810 down_write(&dev_replace
->rwsem
);
811 switch (dev_replace
->replace_state
) {
812 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED
:
813 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED
:
814 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED
:
815 result
= BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED
;
816 up_write(&dev_replace
->rwsem
);
818 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED
:
819 tgt_device
= dev_replace
->tgtdev
;
820 src_device
= dev_replace
->srcdev
;
821 up_write(&dev_replace
->rwsem
);
822 ret
= btrfs_scrub_cancel(fs_info
);
824 result
= BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED
;
826 result
= BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR
;
828 * btrfs_dev_replace_finishing() will handle the
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 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED
:
839 * Scrub doing the replace isn't running so we need to do the
840 * cleanup step of btrfs_dev_replace_finishing() here
842 result
= BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR
;
843 tgt_device
= dev_replace
->tgtdev
;
844 src_device
= dev_replace
->srcdev
;
845 dev_replace
->tgtdev
= NULL
;
846 dev_replace
->srcdev
= NULL
;
847 dev_replace
->replace_state
=
848 BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED
;
849 dev_replace
->time_stopped
= ktime_get_real_seconds();
850 dev_replace
->item_needs_writeback
= 1;
852 up_write(&dev_replace
->rwsem
);
854 /* Scrub for replace must not be running in suspended state */
855 ret
= btrfs_scrub_cancel(fs_info
);
856 ASSERT(ret
!= -ENOTCONN
);
858 trans
= btrfs_start_transaction(root
, 0);
860 mutex_unlock(&dev_replace
->lock_finishing_cancel_unmount
);
861 return PTR_ERR(trans
);
863 ret
= btrfs_commit_transaction(trans
);
866 btrfs_info_in_rcu(fs_info
,
867 "suspended dev_replace from %s (devid %llu) to %s canceled",
868 btrfs_dev_name(src_device
), src_device
->devid
,
869 btrfs_dev_name(tgt_device
));
872 btrfs_destroy_dev_replace_tgtdev(tgt_device
);
875 up_write(&dev_replace
->rwsem
);
879 mutex_unlock(&dev_replace
->lock_finishing_cancel_unmount
);
883 void btrfs_dev_replace_suspend_for_unmount(struct btrfs_fs_info
*fs_info
)
885 struct btrfs_dev_replace
*dev_replace
= &fs_info
->dev_replace
;
887 mutex_lock(&dev_replace
->lock_finishing_cancel_unmount
);
888 down_write(&dev_replace
->rwsem
);
890 switch (dev_replace
->replace_state
) {
891 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED
:
892 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED
:
893 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED
:
894 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED
:
896 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED
:
897 dev_replace
->replace_state
=
898 BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED
;
899 dev_replace
->time_stopped
= ktime_get_real_seconds();
900 dev_replace
->item_needs_writeback
= 1;
901 btrfs_info(fs_info
, "suspending dev_replace for unmount");
905 up_write(&dev_replace
->rwsem
);
906 mutex_unlock(&dev_replace
->lock_finishing_cancel_unmount
);
909 /* resume dev_replace procedure that was interrupted by unmount */
910 int btrfs_resume_dev_replace_async(struct btrfs_fs_info
*fs_info
)
912 struct task_struct
*task
;
913 struct btrfs_dev_replace
*dev_replace
= &fs_info
->dev_replace
;
915 down_write(&dev_replace
->rwsem
);
917 switch (dev_replace
->replace_state
) {
918 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED
:
919 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED
:
920 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED
:
921 up_write(&dev_replace
->rwsem
);
923 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED
:
925 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED
:
926 dev_replace
->replace_state
=
927 BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED
;
930 if (!dev_replace
->tgtdev
|| !dev_replace
->tgtdev
->bdev
) {
932 "cannot continue dev_replace, tgtdev is missing");
934 "you may cancel the operation after 'mount -o degraded'");
935 dev_replace
->replace_state
=
936 BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED
;
937 up_write(&dev_replace
->rwsem
);
940 up_write(&dev_replace
->rwsem
);
943 * This could collide with a paused balance, but the exclusive op logic
944 * should never allow both to start and pause. We don't want to allow
945 * dev-replace to start anyway.
947 if (test_and_set_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
)) {
948 down_write(&dev_replace
->rwsem
);
949 dev_replace
->replace_state
=
950 BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED
;
951 up_write(&dev_replace
->rwsem
);
953 "cannot resume dev-replace, other exclusive operation running");
957 task
= kthread_run(btrfs_dev_replace_kthread
, fs_info
, "btrfs-devrepl");
958 return PTR_ERR_OR_ZERO(task
);
961 static int btrfs_dev_replace_kthread(void *data
)
963 struct btrfs_fs_info
*fs_info
= data
;
964 struct btrfs_dev_replace
*dev_replace
= &fs_info
->dev_replace
;
968 progress
= btrfs_dev_replace_progress(fs_info
);
969 progress
= div_u64(progress
, 10);
970 btrfs_info_in_rcu(fs_info
,
971 "continuing dev_replace from %s (devid %llu) to target %s @%u%%",
972 btrfs_dev_name(dev_replace
->srcdev
),
973 dev_replace
->srcdev
->devid
,
974 btrfs_dev_name(dev_replace
->tgtdev
),
975 (unsigned int)progress
);
977 ret
= btrfs_scrub_dev(fs_info
, dev_replace
->srcdev
->devid
,
978 dev_replace
->committed_cursor_left
,
979 btrfs_device_get_total_bytes(dev_replace
->srcdev
),
980 &dev_replace
->scrub_progress
, 0, 1);
981 ret
= btrfs_dev_replace_finishing(fs_info
, ret
);
982 WARN_ON(ret
&& ret
!= -ECANCELED
);
984 clear_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
);
988 int btrfs_dev_replace_is_ongoing(struct btrfs_dev_replace
*dev_replace
)
990 if (!dev_replace
->is_valid
)
993 switch (dev_replace
->replace_state
) {
994 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED
:
995 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED
:
996 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED
:
998 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED
:
999 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED
:
1001 * return true even if tgtdev is missing (this is
1002 * something that can happen if the dev_replace
1003 * procedure is suspended by an umount and then
1004 * the tgtdev is missing (or "btrfs dev scan") was
1005 * not called and the filesystem is remounted
1006 * in degraded state. This does not stop the
1007 * dev_replace procedure. It needs to be canceled
1008 * manually if the cancellation is wanted.
1015 void btrfs_bio_counter_inc_noblocked(struct btrfs_fs_info
*fs_info
)
1017 percpu_counter_inc(&fs_info
->dev_replace
.bio_counter
);
1020 void btrfs_bio_counter_sub(struct btrfs_fs_info
*fs_info
, s64 amount
)
1022 percpu_counter_sub(&fs_info
->dev_replace
.bio_counter
, amount
);
1023 cond_wake_up_nomb(&fs_info
->dev_replace
.replace_wait
);
1026 void btrfs_bio_counter_inc_blocked(struct btrfs_fs_info
*fs_info
)
1029 percpu_counter_inc(&fs_info
->dev_replace
.bio_counter
);
1030 if (likely(!test_bit(BTRFS_FS_STATE_DEV_REPLACING
,
1031 &fs_info
->fs_state
)))
1034 btrfs_bio_counter_dec(fs_info
);
1035 wait_event(fs_info
->dev_replace
.replace_wait
,
1036 !test_bit(BTRFS_FS_STATE_DEV_REPLACING
,
1037 &fs_info
->fs_state
));