hc
2024-05-14 bedbef8ad3e75a304af6361af235302bcc61d06b
kernel/fs/btrfs/dev-replace.c
....@@ -9,6 +9,7 @@
99 #include <linux/blkdev.h>
1010 #include <linux/kthread.h>
1111 #include <linux/math64.h>
12
+#include "misc.h"
1213 #include "ctree.h"
1314 #include "extent_map.h"
1415 #include "disk-io.h"
....@@ -21,12 +22,48 @@
2122 #include "dev-replace.h"
2223 #include "sysfs.h"
2324
25
+/*
26
+ * Device replace overview
27
+ *
28
+ * [Objective]
29
+ * To copy all extents (both new and on-disk) from source device to target
30
+ * device, while still keeping the filesystem read-write.
31
+ *
32
+ * [Method]
33
+ * There are two main methods involved:
34
+ *
35
+ * - Write duplication
36
+ *
37
+ * All new writes will be written to both target and source devices, so even
38
+ * if replace gets canceled, sources device still contans up-to-date data.
39
+ *
40
+ * Location: handle_ops_on_dev_replace() from __btrfs_map_block()
41
+ * Start: btrfs_dev_replace_start()
42
+ * End: btrfs_dev_replace_finishing()
43
+ * Content: Latest data/metadata
44
+ *
45
+ * - Copy existing extents
46
+ *
47
+ * This happens by re-using scrub facility, as scrub also iterates through
48
+ * existing extents from commit root.
49
+ *
50
+ * Location: scrub_write_block_to_dev_replace() from
51
+ * scrub_block_complete()
52
+ * Content: Data/meta from commit root.
53
+ *
54
+ * Due to the content difference, we need to avoid nocow write when dev-replace
55
+ * is happening. This is done by marking the block group read-only and waiting
56
+ * for NOCOW writes.
57
+ *
58
+ * After replace is done, the finishing part is done by swapping the target and
59
+ * source devices.
60
+ *
61
+ * Location: btrfs_dev_replace_update_device_in_mapping_tree() from
62
+ * btrfs_dev_replace_finishing()
63
+ */
64
+
2465 static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
2566 int scrub_ret);
26
-static void btrfs_dev_replace_update_device_in_mapping_tree(
27
- struct btrfs_fs_info *fs_info,
28
- struct btrfs_device *srcdev,
29
- struct btrfs_device *tgtdev);
3067 static int btrfs_dev_replace_kthread(void *data);
3168
3269 int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info)
....@@ -67,10 +104,9 @@
67104 }
68105 ret = 0;
69106 dev_replace->replace_state =
70
- BTRFS_DEV_REPLACE_ITEM_STATE_NEVER_STARTED;
107
+ BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED;
71108 dev_replace->cont_reading_from_srcdev_mode =
72109 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_ALWAYS;
73
- dev_replace->replace_state = 0;
74110 dev_replace->time_started = 0;
75111 dev_replace->time_stopped = 0;
76112 atomic64_set(&dev_replace->num_write_errors, 0);
....@@ -125,7 +161,7 @@
125161 if (btrfs_find_device(fs_info->fs_devices,
126162 BTRFS_DEV_REPLACE_DEVID, NULL, NULL, false)) {
127163 btrfs_err(fs_info,
128
- "replace devid present without an active replace item");
164
+"replace without active item, run 'device scan --forget' on the target device");
129165 ret = -EUCLEAN;
130166 } else {
131167 dev_replace->srcdev = NULL;
....@@ -206,7 +242,6 @@
206242 {
207243 struct btrfs_device *device;
208244 struct block_device *bdev;
209
- struct list_head *devices;
210245 struct rcu_string *name;
211246 u64 devid = BTRFS_DEV_REPLACE_DEVID;
212247 int ret = 0;
....@@ -224,10 +259,9 @@
224259 return PTR_ERR(bdev);
225260 }
226261
227
- filemap_write_and_wait(bdev->bd_inode->i_mapping);
262
+ sync_blockdev(bdev);
228263
229
- devices = &fs_info->fs_devices->devices;
230
- list_for_each_entry(device, devices, dev_list) {
264
+ list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
231265 if (device->bdev == bdev) {
232266 btrfs_err(fs_info,
233267 "target device is in the filesystem!");
....@@ -260,7 +294,6 @@
260294 }
261295 rcu_assign_pointer(device->name, name);
262296
263
- mutex_lock(&fs_info->fs_devices->device_list_mutex);
264297 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
265298 device->generation = 0;
266299 device->io_width = fs_info->sectorsize;
....@@ -279,6 +312,8 @@
279312 device->dev_stats_valid = 1;
280313 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
281314 device->fs_devices = fs_info->fs_devices;
315
+
316
+ mutex_lock(&fs_info->fs_devices->device_list_mutex);
282317 list_add(&device->dev_list, &fs_info->fs_devices->devices);
283318 fs_info->fs_devices->num_devices++;
284319 fs_info->fs_devices->open_devices++;
....@@ -296,9 +331,9 @@
296331 * called from commit_transaction. Writes changed device replace state to
297332 * disk.
298333 */
299
-int btrfs_run_dev_replace(struct btrfs_trans_handle *trans,
300
- struct btrfs_fs_info *fs_info)
334
+int btrfs_run_dev_replace(struct btrfs_trans_handle *trans)
301335 {
336
+ struct btrfs_fs_info *fs_info = trans->fs_info;
302337 int ret;
303338 struct btrfs_root *dev_root = fs_info->dev_root;
304339 struct btrfs_path *path;
....@@ -307,13 +342,13 @@
307342 struct btrfs_dev_replace_item *ptr;
308343 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
309344
310
- btrfs_dev_replace_read_lock(dev_replace);
345
+ down_read(&dev_replace->rwsem);
311346 if (!dev_replace->is_valid ||
312347 !dev_replace->item_needs_writeback) {
313
- btrfs_dev_replace_read_unlock(dev_replace);
348
+ up_read(&dev_replace->rwsem);
314349 return 0;
315350 }
316
- btrfs_dev_replace_read_unlock(dev_replace);
351
+ up_read(&dev_replace->rwsem);
317352
318353 key.objectid = 0;
319354 key.type = BTRFS_DEV_REPLACE_KEY;
....@@ -371,7 +406,7 @@
371406 ptr = btrfs_item_ptr(eb, path->slots[0],
372407 struct btrfs_dev_replace_item);
373408
374
- btrfs_dev_replace_write_lock(dev_replace);
409
+ down_write(&dev_replace->rwsem);
375410 if (dev_replace->srcdev)
376411 btrfs_set_dev_replace_src_devid(eb, ptr,
377412 dev_replace->srcdev->devid);
....@@ -394,7 +429,7 @@
394429 btrfs_set_dev_replace_cursor_right(eb, ptr,
395430 dev_replace->cursor_right);
396431 dev_replace->item_needs_writeback = 0;
397
- btrfs_dev_replace_write_unlock(dev_replace);
432
+ up_write(&dev_replace->rwsem);
398433
399434 btrfs_mark_buffer_dirty(eb);
400435
....@@ -402,14 +437,6 @@
402437 btrfs_free_path(path);
403438
404439 return ret;
405
-}
406
-
407
-void btrfs_after_dev_replace_commit(struct btrfs_fs_info *fs_info)
408
-{
409
- struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
410
-
411
- dev_replace->committed_cursor_left =
412
- dev_replace->cursor_left_last_write_of_item;
413440 }
414441
415442 static char* btrfs_dev_name(struct btrfs_device *device)
....@@ -420,7 +447,7 @@
420447 return rcu_str_deref(device->name);
421448 }
422449
423
-int btrfs_dev_replace_start(struct btrfs_fs_info *fs_info,
450
+static int btrfs_dev_replace_start(struct btrfs_fs_info *fs_info,
424451 const char *tgtdev_name, u64 srcdevid, const char *srcdev_name,
425452 int read_src)
426453 {
....@@ -431,15 +458,17 @@
431458 struct btrfs_device *tgt_device = NULL;
432459 struct btrfs_device *src_device = NULL;
433460
434
- ret = btrfs_find_device_by_devspec(fs_info, srcdevid,
435
- srcdev_name, &src_device);
436
- if (ret)
437
- return ret;
461
+ src_device = btrfs_find_device_by_devspec(fs_info, srcdevid,
462
+ srcdev_name);
463
+ if (IS_ERR(src_device))
464
+ return PTR_ERR(src_device);
438465
439
- ret = btrfs_init_dev_replace_tgtdev(fs_info, tgtdev_name,
440
- src_device, &tgt_device);
441
- if (ret)
442
- return ret;
466
+ if (btrfs_pinned_by_swapfile(fs_info, src_device)) {
467
+ btrfs_warn_in_rcu(fs_info,
468
+ "cannot replace device %s (devid %llu) due to active swapfile",
469
+ btrfs_dev_name(src_device), src_device->devid);
470
+ return -ETXTBSY;
471
+ }
443472
444473 /*
445474 * Here we commit the transaction to make sure commit_total_bytes
....@@ -454,7 +483,12 @@
454483 return PTR_ERR(trans);
455484 }
456485
457
- btrfs_dev_replace_write_lock(dev_replace);
486
+ ret = btrfs_init_dev_replace_tgtdev(fs_info, tgtdev_name,
487
+ src_device, &tgt_device);
488
+ if (ret)
489
+ return ret;
490
+
491
+ down_write(&dev_replace->rwsem);
458492 switch (dev_replace->replace_state) {
459493 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
460494 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
....@@ -464,11 +498,11 @@
464498 case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
465499 ASSERT(0);
466500 ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_ALREADY_STARTED;
501
+ up_write(&dev_replace->rwsem);
467502 goto leave;
468503 }
469504
470505 dev_replace->cont_reading_from_srcdev_mode = read_src;
471
- WARN_ON(!src_device);
472506 dev_replace->srcdev = src_device;
473507 dev_replace->tgtdev = tgt_device;
474508
....@@ -492,23 +526,24 @@
492526 dev_replace->item_needs_writeback = 1;
493527 atomic64_set(&dev_replace->num_write_errors, 0);
494528 atomic64_set(&dev_replace->num_uncorrectable_read_errors, 0);
495
- btrfs_dev_replace_write_unlock(dev_replace);
529
+ up_write(&dev_replace->rwsem);
496530
497
- ret = btrfs_sysfs_add_device_link(tgt_device->fs_devices, tgt_device);
531
+ ret = btrfs_sysfs_add_device(tgt_device);
498532 if (ret)
499533 btrfs_err(fs_info, "kobj add dev failed %d", ret);
500534
501535 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
502536
503
- /* force writing the updated state information to disk */
504
- trans = btrfs_start_transaction(root, 0);
537
+ /* Commit dev_replace state and reserve 1 item for it. */
538
+ trans = btrfs_start_transaction(root, 1);
505539 if (IS_ERR(trans)) {
506540 ret = PTR_ERR(trans);
507
- btrfs_dev_replace_write_lock(dev_replace);
541
+ down_write(&dev_replace->rwsem);
508542 dev_replace->replace_state =
509543 BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED;
510544 dev_replace->srcdev = NULL;
511545 dev_replace->tgtdev = NULL;
546
+ up_write(&dev_replace->rwsem);
512547 goto leave;
513548 }
514549
....@@ -521,16 +556,12 @@
521556 &dev_replace->scrub_progress, 0, 1);
522557
523558 ret = btrfs_dev_replace_finishing(fs_info, ret);
524
- if (ret == -EINPROGRESS) {
559
+ if (ret == -EINPROGRESS)
525560 ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS;
526
- } else {
527
- WARN_ON(ret);
528
- }
529561
530562 return ret;
531563
532564 leave:
533
- btrfs_dev_replace_write_unlock(dev_replace);
534565 btrfs_destroy_dev_replace_tgtdev(tgt_device);
535566 return ret;
536567 }
....@@ -558,8 +589,9 @@
558589 args->start.cont_reading_from_srcdev_mode);
559590 args->result = ret;
560591 /* don't warn if EINPROGRESS, someone else might be running scrub */
561
- if (ret == BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS)
562
- ret = 0;
592
+ if (ret == BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS ||
593
+ ret == BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR)
594
+ return 0;
563595
564596 return ret;
565597 }
....@@ -570,8 +602,8 @@
570602 static void btrfs_rm_dev_replace_blocked(struct btrfs_fs_info *fs_info)
571603 {
572604 set_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state);
573
- wait_event(fs_info->replace_wait, !percpu_counter_sum(
574
- &fs_info->bio_counter));
605
+ wait_event(fs_info->dev_replace.replace_wait, !percpu_counter_sum(
606
+ &fs_info->dev_replace.bio_counter));
575607 }
576608
577609 /*
....@@ -580,7 +612,64 @@
580612 static void btrfs_rm_dev_replace_unblocked(struct btrfs_fs_info *fs_info)
581613 {
582614 clear_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state);
583
- wake_up(&fs_info->replace_wait);
615
+ wake_up(&fs_info->dev_replace.replace_wait);
616
+}
617
+
618
+/*
619
+ * When finishing the device replace, before swapping the source device with the
620
+ * target device we must update the chunk allocation state in the target device,
621
+ * as it is empty because replace works by directly copying the chunks and not
622
+ * through the normal chunk allocation path.
623
+ */
624
+static int btrfs_set_target_alloc_state(struct btrfs_device *srcdev,
625
+ struct btrfs_device *tgtdev)
626
+{
627
+ struct extent_state *cached_state = NULL;
628
+ u64 start = 0;
629
+ u64 found_start;
630
+ u64 found_end;
631
+ int ret = 0;
632
+
633
+ lockdep_assert_held(&srcdev->fs_info->chunk_mutex);
634
+
635
+ while (!find_first_extent_bit(&srcdev->alloc_state, start,
636
+ &found_start, &found_end,
637
+ CHUNK_ALLOCATED, &cached_state)) {
638
+ ret = set_extent_bits(&tgtdev->alloc_state, found_start,
639
+ found_end, CHUNK_ALLOCATED);
640
+ if (ret)
641
+ break;
642
+ start = found_end + 1;
643
+ }
644
+
645
+ free_extent_state(cached_state);
646
+ return ret;
647
+}
648
+
649
+static void btrfs_dev_replace_update_device_in_mapping_tree(
650
+ struct btrfs_fs_info *fs_info,
651
+ struct btrfs_device *srcdev,
652
+ struct btrfs_device *tgtdev)
653
+{
654
+ struct extent_map_tree *em_tree = &fs_info->mapping_tree;
655
+ struct extent_map *em;
656
+ struct map_lookup *map;
657
+ u64 start = 0;
658
+ int i;
659
+
660
+ write_lock(&em_tree->lock);
661
+ do {
662
+ em = lookup_extent_mapping(em_tree, start, (u64)-1);
663
+ if (!em)
664
+ break;
665
+ map = em->map_lookup;
666
+ for (i = 0; i < map->num_stripes; i++)
667
+ if (srcdev == map->stripes[i].dev)
668
+ map->stripes[i].dev = tgtdev;
669
+ start = em->start + em->len;
670
+ free_extent_map(em);
671
+ } while (start);
672
+ write_unlock(&em_tree->lock);
584673 }
585674
586675 static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
....@@ -597,50 +686,62 @@
597686 /* don't allow cancel or unmount to disturb the finishing procedure */
598687 mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
599688
600
- btrfs_dev_replace_read_lock(dev_replace);
689
+ down_read(&dev_replace->rwsem);
601690 /* was the operation canceled, or is it finished? */
602691 if (dev_replace->replace_state !=
603692 BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED) {
604
- btrfs_dev_replace_read_unlock(dev_replace);
693
+ up_read(&dev_replace->rwsem);
605694 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
606695 return 0;
607696 }
608697
609698 tgt_device = dev_replace->tgtdev;
610699 src_device = dev_replace->srcdev;
611
- btrfs_dev_replace_read_unlock(dev_replace);
700
+ up_read(&dev_replace->rwsem);
612701
613702 /*
614703 * flush all outstanding I/O and inode extent mappings before the
615704 * copy operation is declared as being finished
616705 */
617
- ret = btrfs_start_delalloc_roots(fs_info, -1);
706
+ ret = btrfs_start_delalloc_roots(fs_info, U64_MAX, false);
618707 if (ret) {
619708 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
620709 return ret;
621710 }
622711 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
623712
713
+ if (!scrub_ret)
714
+ btrfs_reada_remove_dev(src_device);
715
+
716
+ /*
717
+ * We have to use this loop approach because at this point src_device
718
+ * has to be available for transaction commit to complete, yet new
719
+ * chunks shouldn't be allocated on the device.
720
+ */
624721 while (1) {
625722 trans = btrfs_start_transaction(root, 0);
626723 if (IS_ERR(trans)) {
724
+ btrfs_reada_undo_remove_dev(src_device);
627725 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
628726 return PTR_ERR(trans);
629727 }
630728 ret = btrfs_commit_transaction(trans);
631729 WARN_ON(ret);
632
- /* keep away write_all_supers() during the finishing procedure */
730
+
731
+ /* Prevent write_all_supers() during the finishing procedure */
633732 mutex_lock(&fs_info->fs_devices->device_list_mutex);
733
+ /* Prevent new chunks being allocated on the source device */
634734 mutex_lock(&fs_info->chunk_mutex);
635
- if (src_device->has_pending_chunks) {
636
- mutex_unlock(&root->fs_info->chunk_mutex);
637
- mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
735
+
736
+ if (!list_empty(&src_device->post_commit_list)) {
737
+ mutex_unlock(&fs_info->fs_devices->device_list_mutex);
738
+ mutex_unlock(&fs_info->chunk_mutex);
638739 } else {
639740 break;
640741 }
641742 }
642743
643
- btrfs_dev_replace_write_lock(dev_replace);
744
+ down_write(&dev_replace->rwsem);
644745 dev_replace->replace_state =
645746 scrub_ret ? BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED
646747 : BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED;
....@@ -649,20 +750,29 @@
649750 dev_replace->time_stopped = ktime_get_real_seconds();
650751 dev_replace->item_needs_writeback = 1;
651752
652
- /* replace old device with new one in mapping tree */
753
+ /*
754
+ * Update allocation state in the new device and replace the old device
755
+ * with the new one in the mapping tree.
756
+ */
653757 if (!scrub_ret) {
758
+ scrub_ret = btrfs_set_target_alloc_state(src_device, tgt_device);
759
+ if (scrub_ret)
760
+ goto error;
654761 btrfs_dev_replace_update_device_in_mapping_tree(fs_info,
655762 src_device,
656763 tgt_device);
657764 } else {
658
- btrfs_err_in_rcu(fs_info,
765
+ if (scrub_ret != -ECANCELED)
766
+ btrfs_err_in_rcu(fs_info,
659767 "btrfs_scrub_dev(%s, %llu, %s) failed %d",
660768 btrfs_dev_name(src_device),
661769 src_device->devid,
662770 rcu_str_deref(tgt_device->name), scrub_ret);
663
- btrfs_dev_replace_write_unlock(dev_replace);
771
+error:
772
+ up_write(&dev_replace->rwsem);
664773 mutex_unlock(&fs_info->chunk_mutex);
665774 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
775
+ btrfs_reada_undo_remove_dev(src_device);
666776 btrfs_rm_dev_replace_blocked(fs_info);
667777 if (tgt_device)
668778 btrfs_destroy_dev_replace_tgtdev(tgt_device);
....@@ -687,8 +797,6 @@
687797 btrfs_device_set_disk_total_bytes(tgt_device,
688798 src_device->disk_total_bytes);
689799 btrfs_device_set_bytes_used(tgt_device, src_device->bytes_used);
690
- ASSERT(list_empty(&src_device->resized_list));
691
- tgt_device->commit_total_bytes = src_device->commit_total_bytes;
692800 tgt_device->commit_bytes_used = src_device->bytes_used;
693801
694802 btrfs_assign_next_active_device(src_device, tgt_device);
....@@ -696,8 +804,7 @@
696804 list_add(&tgt_device->dev_alloc_list, &fs_info->fs_devices->alloc_list);
697805 fs_info->fs_devices->rw_devices++;
698806
699
- btrfs_dev_replace_write_unlock(dev_replace);
700
-
807
+ up_write(&dev_replace->rwsem);
701808 btrfs_rm_dev_replace_blocked(fs_info);
702809
703810 btrfs_rm_dev_replace_remove_srcdev(src_device);
....@@ -721,8 +828,11 @@
721828 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
722829
723830 /* replace the sysfs entry */
724
- btrfs_sysfs_rm_device_link(fs_info->fs_devices, src_device);
725
- btrfs_rm_dev_replace_free_srcdev(fs_info, src_device);
831
+ btrfs_sysfs_remove_device(src_device);
832
+ btrfs_sysfs_update_devid(tgt_device);
833
+ if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &src_device->dev_state))
834
+ btrfs_scratch_superblocks(fs_info, src_device->bdev,
835
+ src_device->name->str);
726836
727837 /* write back the superblocks */
728838 trans = btrfs_start_transaction(root, 0);
....@@ -731,33 +841,9 @@
731841
732842 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
733843
844
+ btrfs_rm_dev_replace_free_srcdev(src_device);
845
+
734846 return 0;
735
-}
736
-
737
-static void btrfs_dev_replace_update_device_in_mapping_tree(
738
- struct btrfs_fs_info *fs_info,
739
- struct btrfs_device *srcdev,
740
- struct btrfs_device *tgtdev)
741
-{
742
- struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
743
- struct extent_map *em;
744
- struct map_lookup *map;
745
- u64 start = 0;
746
- int i;
747
-
748
- write_lock(&em_tree->lock);
749
- do {
750
- em = lookup_extent_mapping(em_tree, start, (u64)-1);
751
- if (!em)
752
- break;
753
- map = em->map_lookup;
754
- for (i = 0; i < map->num_stripes; i++)
755
- if (srcdev == map->stripes[i].dev)
756
- map->stripes[i].dev = tgtdev;
757
- start = em->start + em->len;
758
- free_extent_map(em);
759
- } while (start);
760
- write_unlock(&em_tree->lock);
761847 }
762848
763849 /*
....@@ -794,7 +880,7 @@
794880 {
795881 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
796882
797
- btrfs_dev_replace_read_lock(dev_replace);
883
+ down_read(&dev_replace->rwsem);
798884 /* even if !dev_replace_is_valid, the values are good enough for
799885 * the replace_status ioctl */
800886 args->result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
....@@ -806,7 +892,7 @@
806892 args->status.num_uncorrectable_read_errors =
807893 atomic64_read(&dev_replace->num_uncorrectable_read_errors);
808894 args->status.progress_1000 = btrfs_dev_replace_progress(fs_info);
809
- btrfs_dev_replace_read_unlock(dev_replace);
895
+ up_read(&dev_replace->rwsem);
810896 }
811897
812898 int btrfs_dev_replace_cancel(struct btrfs_fs_info *fs_info)
....@@ -823,18 +909,18 @@
823909 return -EROFS;
824910
825911 mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
826
- btrfs_dev_replace_write_lock(dev_replace);
912
+ down_write(&dev_replace->rwsem);
827913 switch (dev_replace->replace_state) {
828914 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
829915 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
830916 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
831917 result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED;
832
- btrfs_dev_replace_write_unlock(dev_replace);
918
+ up_write(&dev_replace->rwsem);
833919 break;
834920 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
835921 tgt_device = dev_replace->tgtdev;
836922 src_device = dev_replace->srcdev;
837
- btrfs_dev_replace_write_unlock(dev_replace);
923
+ up_write(&dev_replace->rwsem);
838924 ret = btrfs_scrub_cancel(fs_info);
839925 if (ret < 0) {
840926 result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED;
....@@ -865,8 +951,9 @@
865951 dev_replace->time_stopped = ktime_get_real_seconds();
866952 dev_replace->item_needs_writeback = 1;
867953
868
- btrfs_dev_replace_write_unlock(dev_replace);
954
+ up_write(&dev_replace->rwsem);
869955
956
+ /* Scrub for replace must not be running in suspended state */
870957 btrfs_scrub_cancel(fs_info);
871958
872959 trans = btrfs_start_transaction(root, 0);
....@@ -886,6 +973,7 @@
886973 btrfs_destroy_dev_replace_tgtdev(tgt_device);
887974 break;
888975 default:
976
+ up_write(&dev_replace->rwsem);
889977 result = -EINVAL;
890978 }
891979
....@@ -898,7 +986,8 @@
898986 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
899987
900988 mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
901
- btrfs_dev_replace_write_lock(dev_replace);
989
+ down_write(&dev_replace->rwsem);
990
+
902991 switch (dev_replace->replace_state) {
903992 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
904993 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
....@@ -914,7 +1003,7 @@
9141003 break;
9151004 }
9161005
917
- btrfs_dev_replace_write_unlock(dev_replace);
1006
+ up_write(&dev_replace->rwsem);
9181007 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
9191008 }
9201009
....@@ -924,12 +1013,13 @@
9241013 struct task_struct *task;
9251014 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
9261015
927
- btrfs_dev_replace_write_lock(dev_replace);
1016
+ down_write(&dev_replace->rwsem);
1017
+
9281018 switch (dev_replace->replace_state) {
9291019 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
9301020 case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
9311021 case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
932
- btrfs_dev_replace_write_unlock(dev_replace);
1022
+ up_write(&dev_replace->rwsem);
9331023 return 0;
9341024 case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
9351025 break;
....@@ -945,21 +1035,21 @@
9451035 "you may cancel the operation after 'mount -o degraded'");
9461036 dev_replace->replace_state =
9471037 BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
948
- btrfs_dev_replace_write_unlock(dev_replace);
1038
+ up_write(&dev_replace->rwsem);
9491039 return 0;
9501040 }
951
- btrfs_dev_replace_write_unlock(dev_replace);
1041
+ up_write(&dev_replace->rwsem);
9521042
9531043 /*
9541044 * This could collide with a paused balance, but the exclusive op logic
9551045 * should never allow both to start and pause. We don't want to allow
9561046 * dev-replace to start anyway.
9571047 */
958
- if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
959
- btrfs_dev_replace_write_lock(dev_replace);
1048
+ if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REPLACE)) {
1049
+ down_write(&dev_replace->rwsem);
9601050 dev_replace->replace_state =
9611051 BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
962
- btrfs_dev_replace_write_unlock(dev_replace);
1052
+ up_write(&dev_replace->rwsem);
9631053 btrfs_info(fs_info,
9641054 "cannot resume dev-replace, other exclusive operation running");
9651055 return 0;
....@@ -990,13 +1080,13 @@
9901080 btrfs_device_get_total_bytes(dev_replace->srcdev),
9911081 &dev_replace->scrub_progress, 0, 1);
9921082 ret = btrfs_dev_replace_finishing(fs_info, ret);
993
- WARN_ON(ret);
1083
+ WARN_ON(ret && ret != -ECANCELED);
9941084
995
- clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
1085
+ btrfs_exclop_finish(fs_info);
9961086 return 0;
9971087 }
9981088
999
-int btrfs_dev_replace_is_ongoing(struct btrfs_dev_replace *dev_replace)
1089
+int __pure btrfs_dev_replace_is_ongoing(struct btrfs_dev_replace *dev_replace)
10001090 {
10011091 if (!dev_replace->is_valid)
10021092 return 0;
....@@ -1013,7 +1103,7 @@
10131103 * something that can happen if the dev_replace
10141104 * procedure is suspended by an umount and then
10151105 * the tgtdev is missing (or "btrfs dev scan") was
1016
- * not called and the the filesystem is remounted
1106
+ * not called and the filesystem is remounted
10171107 * in degraded state. This does not stop the
10181108 * dev_replace procedure. It needs to be canceled
10191109 * manually if the cancellation is wanted.
....@@ -1023,81 +1113,27 @@
10231113 return 1;
10241114 }
10251115
1026
-void btrfs_dev_replace_read_lock(struct btrfs_dev_replace *dev_replace)
1027
-{
1028
- read_lock(&dev_replace->lock);
1029
- atomic_inc(&dev_replace->read_locks);
1030
-}
1031
-
1032
-void btrfs_dev_replace_read_unlock(struct btrfs_dev_replace *dev_replace)
1033
-{
1034
- ASSERT(atomic_read(&dev_replace->read_locks) > 0);
1035
- atomic_dec(&dev_replace->read_locks);
1036
- read_unlock(&dev_replace->lock);
1037
-}
1038
-
1039
-void btrfs_dev_replace_write_lock(struct btrfs_dev_replace *dev_replace)
1040
-{
1041
-again:
1042
- wait_event(dev_replace->read_lock_wq,
1043
- atomic_read(&dev_replace->blocking_readers) == 0);
1044
- write_lock(&dev_replace->lock);
1045
- if (atomic_read(&dev_replace->blocking_readers)) {
1046
- write_unlock(&dev_replace->lock);
1047
- goto again;
1048
- }
1049
-}
1050
-
1051
-void btrfs_dev_replace_write_unlock(struct btrfs_dev_replace *dev_replace)
1052
-{
1053
- ASSERT(atomic_read(&dev_replace->blocking_readers) == 0);
1054
- write_unlock(&dev_replace->lock);
1055
-}
1056
-
1057
-/* inc blocking cnt and release read lock */
1058
-void btrfs_dev_replace_set_lock_blocking(
1059
- struct btrfs_dev_replace *dev_replace)
1060
-{
1061
- /* only set blocking for read lock */
1062
- ASSERT(atomic_read(&dev_replace->read_locks) > 0);
1063
- atomic_inc(&dev_replace->blocking_readers);
1064
- read_unlock(&dev_replace->lock);
1065
-}
1066
-
1067
-/* acquire read lock and dec blocking cnt */
1068
-void btrfs_dev_replace_clear_lock_blocking(
1069
- struct btrfs_dev_replace *dev_replace)
1070
-{
1071
- /* only set blocking for read lock */
1072
- ASSERT(atomic_read(&dev_replace->read_locks) > 0);
1073
- ASSERT(atomic_read(&dev_replace->blocking_readers) > 0);
1074
- read_lock(&dev_replace->lock);
1075
- /* Barrier implied by atomic_dec_and_test */
1076
- if (atomic_dec_and_test(&dev_replace->blocking_readers))
1077
- cond_wake_up_nomb(&dev_replace->read_lock_wq);
1078
-}
1079
-
10801116 void btrfs_bio_counter_inc_noblocked(struct btrfs_fs_info *fs_info)
10811117 {
1082
- percpu_counter_inc(&fs_info->bio_counter);
1118
+ percpu_counter_inc(&fs_info->dev_replace.bio_counter);
10831119 }
10841120
10851121 void btrfs_bio_counter_sub(struct btrfs_fs_info *fs_info, s64 amount)
10861122 {
1087
- percpu_counter_sub(&fs_info->bio_counter, amount);
1088
- cond_wake_up_nomb(&fs_info->replace_wait);
1123
+ percpu_counter_sub(&fs_info->dev_replace.bio_counter, amount);
1124
+ cond_wake_up_nomb(&fs_info->dev_replace.replace_wait);
10891125 }
10901126
10911127 void btrfs_bio_counter_inc_blocked(struct btrfs_fs_info *fs_info)
10921128 {
10931129 while (1) {
1094
- percpu_counter_inc(&fs_info->bio_counter);
1130
+ percpu_counter_inc(&fs_info->dev_replace.bio_counter);
10951131 if (likely(!test_bit(BTRFS_FS_STATE_DEV_REPLACING,
10961132 &fs_info->fs_state)))
10971133 break;
10981134
10991135 btrfs_bio_counter_dec(fs_info);
1100
- wait_event(fs_info->replace_wait,
1136
+ wait_event(fs_info->dev_replace.replace_wait,
11011137 !test_bit(BTRFS_FS_STATE_DEV_REPLACING,
11021138 &fs_info->fs_state));
11031139 }