volumes.c (3c09ec59cdea5b132212d97154d625fd34e436dd) volumes.c (5b316468983dfa9473ff0f1c42e4e30b4c267141)
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
4 */
5
6#include <linux/sched.h>
7#include <linux/sched/mm.h>
8#include <linux/bio.h>

--- 17 unchanged lines hidden (view full) ---

26#include "check-integrity.h"
27#include "rcu-string.h"
28#include "dev-replace.h"
29#include "sysfs.h"
30#include "tree-checker.h"
31#include "space-info.h"
32#include "block-group.h"
33#include "discard.h"
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
4 */
5
6#include <linux/sched.h>
7#include <linux/sched/mm.h>
8#include <linux/bio.h>

--- 17 unchanged lines hidden (view full) ---

26#include "check-integrity.h"
27#include "rcu-string.h"
28#include "dev-replace.h"
29#include "sysfs.h"
30#include "tree-checker.h"
31#include "space-info.h"
32#include "block-group.h"
33#include "discard.h"
34#include "zoned.h"
34
35const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
36 [BTRFS_RAID_RAID10] = {
37 .sub_stripes = 2,
38 .dev_stripes = 1,
39 .devs_max = 0, /* 0 == as many as possible */
40 .devs_min = 4,
41 .tolerated_failures = 1,

--- 327 unchanged lines hidden (view full) ---

369}
370
371void btrfs_free_device(struct btrfs_device *device)
372{
373 WARN_ON(!list_empty(&device->post_commit_list));
374 rcu_string_free(device->name);
375 extent_io_tree_release(&device->alloc_state);
376 bio_put(device->flush_bio);
35
36const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
37 [BTRFS_RAID_RAID10] = {
38 .sub_stripes = 2,
39 .dev_stripes = 1,
40 .devs_max = 0, /* 0 == as many as possible */
41 .devs_min = 4,
42 .tolerated_failures = 1,

--- 327 unchanged lines hidden (view full) ---

370}
371
372void btrfs_free_device(struct btrfs_device *device)
373{
374 WARN_ON(!list_empty(&device->post_commit_list));
375 rcu_string_free(device->name);
376 extent_io_tree_release(&device->alloc_state);
377 bio_put(device->flush_bio);
378 btrfs_destroy_dev_zone_info(device);
377 kfree(device);
378}
379
380static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
381{
382 struct btrfs_device *device;
383 WARN_ON(fs_devices->opened);
384 while (!list_empty(&fs_devices->devices)) {

--- 277 unchanged lines hidden (view full) ---

662 q = bdev_get_queue(bdev);
663 if (!blk_queue_nonrot(q))
664 fs_devices->rotating = true;
665
666 device->bdev = bdev;
667 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
668 device->mode = flags;
669
379 kfree(device);
380}
381
382static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
383{
384 struct btrfs_device *device;
385 WARN_ON(fs_devices->opened);
386 while (!list_empty(&fs_devices->devices)) {

--- 277 unchanged lines hidden (view full) ---

664 q = bdev_get_queue(bdev);
665 if (!blk_queue_nonrot(q))
666 fs_devices->rotating = true;
667
668 device->bdev = bdev;
669 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
670 device->mode = flags;
671
672 ret = btrfs_get_dev_zone_info(device);
673 if (ret != 0)
674 goto error_free_page;
675
670 fs_devices->open_devices++;
671 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
672 device->devid != BTRFS_DEV_REPLACE_DEVID) {
673 fs_devices->rw_devices++;
674 list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list);
675 }
676 btrfs_release_disk_super(disk_super);
677

--- 139 unchanged lines hidden (view full) ---

817
818 mutex_lock(&fs_devices->device_list_mutex);
819 list_add(&fs_devices->fs_list, &fs_uuids);
820
821 device = NULL;
822 } else {
823 mutex_lock(&fs_devices->device_list_mutex);
824 device = btrfs_find_device(fs_devices, devid,
676 fs_devices->open_devices++;
677 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
678 device->devid != BTRFS_DEV_REPLACE_DEVID) {
679 fs_devices->rw_devices++;
680 list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list);
681 }
682 btrfs_release_disk_super(disk_super);
683

--- 139 unchanged lines hidden (view full) ---

823
824 mutex_lock(&fs_devices->device_list_mutex);
825 list_add(&fs_devices->fs_list, &fs_uuids);
826
827 device = NULL;
828 } else {
829 mutex_lock(&fs_devices->device_list_mutex);
830 device = btrfs_find_device(fs_devices, devid,
825 disk_super->dev_item.uuid, NULL, false);
831 disk_super->dev_item.uuid, NULL);
826
827 /*
828 * If this disk has been pulled into an fs devices created by
829 * a device which had the CHANGING_FSID_V2 flag then replace the
830 * metadata_uuid/fsid values of the fs_devices.
831 */
832 if (fs_devices->fsid_change &&
833 found_transid > fs_devices->latest_generation) {

--- 101 unchanged lines hidden (view full) ---

935 if (IS_ERR(path_bdev)) {
936 mutex_unlock(&fs_devices->device_list_mutex);
937 return ERR_CAST(path_bdev);
938 }
939
940 if (device->bdev != path_bdev) {
941 bdput(path_bdev);
942 mutex_unlock(&fs_devices->device_list_mutex);
832
833 /*
834 * If this disk has been pulled into an fs devices created by
835 * a device which had the CHANGING_FSID_V2 flag then replace the
836 * metadata_uuid/fsid values of the fs_devices.
837 */
838 if (fs_devices->fsid_change &&
839 found_transid > fs_devices->latest_generation) {

--- 101 unchanged lines hidden (view full) ---

941 if (IS_ERR(path_bdev)) {
942 mutex_unlock(&fs_devices->device_list_mutex);
943 return ERR_CAST(path_bdev);
944 }
945
946 if (device->bdev != path_bdev) {
947 bdput(path_bdev);
948 mutex_unlock(&fs_devices->device_list_mutex);
943 btrfs_warn_in_rcu(device->fs_info,
949 /*
950 * device->fs_info may not be reliable here, so
951 * pass in a NULL instead. This avoids a
952 * possible use-after-free when the fs_info and
953 * fs_info->sb are already torn down.
954 */
955 btrfs_warn_in_rcu(NULL,
944 "duplicate device %s devid %llu generation %llu scanned by %s (%d)",
945 path, devid, found_transid,
946 current->comm,
947 task_pid_nr(current));
948 return ERR_PTR(-EEXIST);
949 }
950 bdput(path_bdev);
951 btrfs_info_in_rcu(device->fs_info,

--- 81 unchanged lines hidden (view full) ---

1033 return fs_devices;
1034error:
1035 mutex_unlock(&orig->device_list_mutex);
1036 free_fs_devices(fs_devices);
1037 return ERR_PTR(ret);
1038}
1039
1040static void __btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices,
956 "duplicate device %s devid %llu generation %llu scanned by %s (%d)",
957 path, devid, found_transid,
958 current->comm,
959 task_pid_nr(current));
960 return ERR_PTR(-EEXIST);
961 }
962 bdput(path_bdev);
963 btrfs_info_in_rcu(device->fs_info,

--- 81 unchanged lines hidden (view full) ---

1045 return fs_devices;
1046error:
1047 mutex_unlock(&orig->device_list_mutex);
1048 free_fs_devices(fs_devices);
1049 return ERR_PTR(ret);
1050}
1051
1052static void __btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices,
1041 int step, struct btrfs_device **latest_dev)
1053 struct btrfs_device **latest_dev)
1042{
1043 struct btrfs_device *device, *next;
1044
1045 /* This is the initialized path, it is safe to release the devices. */
1046 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
1047 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state)) {
1048 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1049 &device->dev_state) &&
1050 !test_bit(BTRFS_DEV_STATE_MISSING,
1051 &device->dev_state) &&
1052 (!*latest_dev ||
1053 device->generation > (*latest_dev)->generation)) {
1054 *latest_dev = device;
1055 }
1056 continue;
1057 }
1058
1054{
1055 struct btrfs_device *device, *next;
1056
1057 /* This is the initialized path, it is safe to release the devices. */
1058 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
1059 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state)) {
1060 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1061 &device->dev_state) &&
1062 !test_bit(BTRFS_DEV_STATE_MISSING,
1063 &device->dev_state) &&
1064 (!*latest_dev ||
1065 device->generation > (*latest_dev)->generation)) {
1066 *latest_dev = device;
1067 }
1068 continue;
1069 }
1070
1059 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
1060 /*
1061 * In the first step, keep the device which has
1062 * the correct fsid and the devid that is used
1063 * for the dev_replace procedure.
1064 * In the second step, the dev_replace state is
1065 * read from the device tree and it is known
1066 * whether the procedure is really active or
1067 * not, which means whether this device is
1068 * used or whether it should be removed.
1069 */
1070 if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1071 &device->dev_state)) {
1072 continue;
1073 }
1074 }
1071 /*
1072 * We have already validated the presence of BTRFS_DEV_REPLACE_DEVID,
1073 * in btrfs_init_dev_replace() so just continue.
1074 */
1075 if (device->devid == BTRFS_DEV_REPLACE_DEVID)
1076 continue;
1077
1075 if (device->bdev) {
1076 blkdev_put(device->bdev, device->mode);
1077 device->bdev = NULL;
1078 fs_devices->open_devices--;
1079 }
1080 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
1081 list_del_init(&device->dev_alloc_list);
1082 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
1078 if (device->bdev) {
1079 blkdev_put(device->bdev, device->mode);
1080 device->bdev = NULL;
1081 fs_devices->open_devices--;
1082 }
1083 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
1084 list_del_init(&device->dev_alloc_list);
1085 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
1083 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1084 &device->dev_state))
1085 fs_devices->rw_devices--;
1086 }
1087 list_del_init(&device->dev_list);
1088 fs_devices->num_devices--;
1089 btrfs_free_device(device);
1090 }
1091
1092}
1093
1094/*
1095 * After we have read the system tree and know devids belonging to this
1096 * filesystem, remove the device which does not belong there.
1097 */
1086 }
1087 list_del_init(&device->dev_list);
1088 fs_devices->num_devices--;
1089 btrfs_free_device(device);
1090 }
1091
1092}
1093
1094/*
1095 * After we have read the system tree and know devids belonging to this
1096 * filesystem, remove the device which does not belong there.
1097 */
1098void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices, int step)
1098void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices)
1099{
1100 struct btrfs_device *latest_dev = NULL;
1101 struct btrfs_fs_devices *seed_dev;
1102
1103 mutex_lock(&uuid_mutex);
1099{
1100 struct btrfs_device *latest_dev = NULL;
1101 struct btrfs_fs_devices *seed_dev;
1102
1103 mutex_lock(&uuid_mutex);
1104 __btrfs_free_extra_devids(fs_devices, step, &latest_dev);
1104 __btrfs_free_extra_devids(fs_devices, &latest_dev);
1105
1106 list_for_each_entry(seed_dev, &fs_devices->seed_list, seed_list)
1105
1106 list_for_each_entry(seed_dev, &fs_devices->seed_list, seed_list)
1107 __btrfs_free_extra_devids(seed_dev, step, &latest_dev);
1107 __btrfs_free_extra_devids(seed_dev, &latest_dev);
1108
1109 fs_devices->latest_bdev = latest_dev->bdev;
1110
1111 mutex_unlock(&uuid_mutex);
1112}
1113
1114static void btrfs_close_bdev(struct btrfs_device *device)
1115{

--- 22 unchanged lines hidden (view full) ---

1138 fs_devices->missing_devices--;
1139
1140 btrfs_close_bdev(device);
1141 if (device->bdev) {
1142 fs_devices->open_devices--;
1143 device->bdev = NULL;
1144 }
1145 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
1108
1109 fs_devices->latest_bdev = latest_dev->bdev;
1110
1111 mutex_unlock(&uuid_mutex);
1112}
1113
1114static void btrfs_close_bdev(struct btrfs_device *device)
1115{

--- 22 unchanged lines hidden (view full) ---

1138 fs_devices->missing_devices--;
1139
1140 btrfs_close_bdev(device);
1141 if (device->bdev) {
1142 fs_devices->open_devices--;
1143 device->bdev = NULL;
1144 }
1145 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
1146 btrfs_destroy_dev_zone_info(device);
1146
1147 device->fs_info = NULL;
1148 atomic_set(&device->dev_stats_ccnt, 0);
1149 extent_io_tree_release(&device->alloc_state);
1150
1151 /* Verify the device is back in a pristine state */
1152 ASSERT(!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state));
1153 ASSERT(!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));

--- 64 unchanged lines hidden (view full) ---

1218 }
1219 if (fs_devices->open_devices == 0)
1220 return -EINVAL;
1221
1222 fs_devices->opened = 1;
1223 fs_devices->latest_bdev = latest_dev->bdev;
1224 fs_devices->total_rw_bytes = 0;
1225 fs_devices->chunk_alloc_policy = BTRFS_CHUNK_ALLOC_REGULAR;
1147
1148 device->fs_info = NULL;
1149 atomic_set(&device->dev_stats_ccnt, 0);
1150 extent_io_tree_release(&device->alloc_state);
1151
1152 /* Verify the device is back in a pristine state */
1153 ASSERT(!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state));
1154 ASSERT(!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));

--- 64 unchanged lines hidden (view full) ---

1219 }
1220 if (fs_devices->open_devices == 0)
1221 return -EINVAL;
1222
1223 fs_devices->opened = 1;
1224 fs_devices->latest_bdev = latest_dev->bdev;
1225 fs_devices->total_rw_bytes = 0;
1226 fs_devices->chunk_alloc_policy = BTRFS_CHUNK_ALLOC_REGULAR;
1227 fs_devices->read_policy = BTRFS_READ_POLICY_PID;
1226
1227 return 0;
1228}
1229
1230static int devid_cmp(void *priv, struct list_head *a, struct list_head *b)
1231{
1232 struct btrfs_device *dev1, *dev2;
1233

--- 1060 unchanged lines hidden (view full) ---

2294 fs_info->bdev_holder, 0, &bdev, &disk_super);
2295 if (ret)
2296 return ERR_PTR(ret);
2297
2298 devid = btrfs_stack_device_id(&disk_super->dev_item);
2299 dev_uuid = disk_super->dev_item.uuid;
2300 if (btrfs_fs_incompat(fs_info, METADATA_UUID))
2301 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
1228
1229 return 0;
1230}
1231
1232static int devid_cmp(void *priv, struct list_head *a, struct list_head *b)
1233{
1234 struct btrfs_device *dev1, *dev2;
1235

--- 1060 unchanged lines hidden (view full) ---

2296 fs_info->bdev_holder, 0, &bdev, &disk_super);
2297 if (ret)
2298 return ERR_PTR(ret);
2299
2300 devid = btrfs_stack_device_id(&disk_super->dev_item);
2301 dev_uuid = disk_super->dev_item.uuid;
2302 if (btrfs_fs_incompat(fs_info, METADATA_UUID))
2303 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
2302 disk_super->metadata_uuid, true);
2304 disk_super->metadata_uuid);
2303 else
2304 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
2305 else
2306 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
2305 disk_super->fsid, true);
2307 disk_super->fsid);
2306
2307 btrfs_release_disk_super(disk_super);
2308 if (!device)
2309 device = ERR_PTR(-ENOENT);
2310 blkdev_put(bdev, FMODE_READ);
2311 return device;
2312}
2313
2314/*
2315 * Lookup a device given by device id, or the path if the id is 0.
2316 */
2317struct btrfs_device *btrfs_find_device_by_devspec(
2318 struct btrfs_fs_info *fs_info, u64 devid,
2319 const char *device_path)
2320{
2321 struct btrfs_device *device;
2322
2323 if (devid) {
2324 device = btrfs_find_device(fs_info->fs_devices, devid, NULL,
2308
2309 btrfs_release_disk_super(disk_super);
2310 if (!device)
2311 device = ERR_PTR(-ENOENT);
2312 blkdev_put(bdev, FMODE_READ);
2313 return device;
2314}
2315
2316/*
2317 * Lookup a device given by device id, or the path if the id is 0.
2318 */
2319struct btrfs_device *btrfs_find_device_by_devspec(
2320 struct btrfs_fs_info *fs_info, u64 devid,
2321 const char *device_path)
2322{
2323 struct btrfs_device *device;
2324
2325 if (devid) {
2326 device = btrfs_find_device(fs_info->fs_devices, devid, NULL,
2325 NULL, true);
2327 NULL);
2326 if (!device)
2327 return ERR_PTR(-ENOENT);
2328 return device;
2329 }
2330
2331 if (!device_path || !device_path[0])
2332 return ERR_PTR(-EINVAL);
2333

--- 132 unchanged lines hidden (view full) ---

2466 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2467 struct btrfs_dev_item);
2468 devid = btrfs_device_id(leaf, dev_item);
2469 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
2470 BTRFS_UUID_SIZE);
2471 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
2472 BTRFS_FSID_SIZE);
2473 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
2328 if (!device)
2329 return ERR_PTR(-ENOENT);
2330 return device;
2331 }
2332
2333 if (!device_path || !device_path[0])
2334 return ERR_PTR(-EINVAL);
2335

--- 132 unchanged lines hidden (view full) ---

2468 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2469 struct btrfs_dev_item);
2470 devid = btrfs_device_id(leaf, dev_item);
2471 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
2472 BTRFS_UUID_SIZE);
2473 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
2474 BTRFS_FSID_SIZE);
2475 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
2474 fs_uuid, true);
2476 fs_uuid);
2475 BUG_ON(!device); /* Logic error */
2476
2477 if (device->fs_devices->seeding) {
2478 btrfs_set_device_generation(leaf, dev_item,
2479 device->generation);
2480 btrfs_mark_buffer_dirty(leaf);
2481 }
2482

--- 58 unchanged lines hidden (view full) ---

2541
2542 name = rcu_string_strdup(device_path, GFP_KERNEL);
2543 if (!name) {
2544 ret = -ENOMEM;
2545 goto error_free_device;
2546 }
2547 rcu_assign_pointer(device->name, name);
2548
2477 BUG_ON(!device); /* Logic error */
2478
2479 if (device->fs_devices->seeding) {
2480 btrfs_set_device_generation(leaf, dev_item,
2481 device->generation);
2482 btrfs_mark_buffer_dirty(leaf);
2483 }
2484

--- 58 unchanged lines hidden (view full) ---

2543
2544 name = rcu_string_strdup(device_path, GFP_KERNEL);
2545 if (!name) {
2546 ret = -ENOMEM;
2547 goto error_free_device;
2548 }
2549 rcu_assign_pointer(device->name, name);
2550
2551 device->fs_info = fs_info;
2552 device->bdev = bdev;
2553
2554 ret = btrfs_get_dev_zone_info(device);
2555 if (ret)
2556 goto error_free_device;
2557
2549 trans = btrfs_start_transaction(root, 0);
2550 if (IS_ERR(trans)) {
2551 ret = PTR_ERR(trans);
2558 trans = btrfs_start_transaction(root, 0);
2559 if (IS_ERR(trans)) {
2560 ret = PTR_ERR(trans);
2552 goto error_free_device;
2561 goto error_free_zone;
2553 }
2554
2555 q = bdev_get_queue(bdev);
2556 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
2557 device->generation = trans->transid;
2558 device->io_width = fs_info->sectorsize;
2559 device->io_align = fs_info->sectorsize;
2560 device->sector_size = fs_info->sectorsize;
2561 device->total_bytes = round_down(i_size_read(bdev->bd_inode),
2562 fs_info->sectorsize);
2563 device->disk_total_bytes = device->total_bytes;
2564 device->commit_total_bytes = device->total_bytes;
2562 }
2563
2564 q = bdev_get_queue(bdev);
2565 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
2566 device->generation = trans->transid;
2567 device->io_width = fs_info->sectorsize;
2568 device->io_align = fs_info->sectorsize;
2569 device->sector_size = fs_info->sectorsize;
2570 device->total_bytes = round_down(i_size_read(bdev->bd_inode),
2571 fs_info->sectorsize);
2572 device->disk_total_bytes = device->total_bytes;
2573 device->commit_total_bytes = device->total_bytes;
2565 device->fs_info = fs_info;
2566 device->bdev = bdev;
2567 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
2568 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
2569 device->mode = FMODE_EXCL;
2570 device->dev_stats_valid = 1;
2571 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
2572
2573 if (seeding_dev) {
2574 sb->s_flags &= ~SB_RDONLY;

--- 130 unchanged lines hidden (view full) ---

2705 orig_super_num_devices);
2706 mutex_unlock(&fs_info->chunk_mutex);
2707 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2708error_trans:
2709 if (seeding_dev)
2710 sb->s_flags |= SB_RDONLY;
2711 if (trans)
2712 btrfs_end_transaction(trans);
2574 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
2575 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
2576 device->mode = FMODE_EXCL;
2577 device->dev_stats_valid = 1;
2578 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
2579
2580 if (seeding_dev) {
2581 sb->s_flags &= ~SB_RDONLY;

--- 130 unchanged lines hidden (view full) ---

2712 orig_super_num_devices);
2713 mutex_unlock(&fs_info->chunk_mutex);
2714 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2715error_trans:
2716 if (seeding_dev)
2717 sb->s_flags |= SB_RDONLY;
2718 if (trans)
2719 btrfs_end_transaction(trans);
2720error_free_zone:
2721 btrfs_destroy_dev_zone_info(device);
2713error_free_device:
2714 btrfs_free_device(device);
2715error:
2716 blkdev_put(bdev, FMODE_EXCL);
2717 if (locked) {
2718 mutex_unlock(&uuid_mutex);
2719 up_write(&sb->s_umount);
2720 }

--- 2759 unchanged lines hidden (view full) ---

5480 ASSERT((map->type &
5481 (BTRFS_BLOCK_GROUP_RAID1_MASK | BTRFS_BLOCK_GROUP_RAID10)));
5482
5483 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5484 num_stripes = map->sub_stripes;
5485 else
5486 num_stripes = map->num_stripes;
5487
2722error_free_device:
2723 btrfs_free_device(device);
2724error:
2725 blkdev_put(bdev, FMODE_EXCL);
2726 if (locked) {
2727 mutex_unlock(&uuid_mutex);
2728 up_write(&sb->s_umount);
2729 }

--- 2759 unchanged lines hidden (view full) ---

5489 ASSERT((map->type &
5490 (BTRFS_BLOCK_GROUP_RAID1_MASK | BTRFS_BLOCK_GROUP_RAID10)));
5491
5492 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5493 num_stripes = map->sub_stripes;
5494 else
5495 num_stripes = map->num_stripes;
5496
5488 preferred_mirror = first + current->pid % num_stripes;
5497 switch (fs_info->fs_devices->read_policy) {
5498 default:
5499 /* Shouldn't happen, just warn and use pid instead of failing */
5500 btrfs_warn_rl(fs_info,
5501 "unknown read_policy type %u, reset to pid",
5502 fs_info->fs_devices->read_policy);
5503 fs_info->fs_devices->read_policy = BTRFS_READ_POLICY_PID;
5504 fallthrough;
5505 case BTRFS_READ_POLICY_PID:
5506 preferred_mirror = first + (current->pid % num_stripes);
5507 break;
5508 }
5489
5490 if (dev_replace_is_ongoing &&
5491 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5492 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5493 srcdev = fs_info->dev_replace.srcdev;
5494 else
5495 srcdev = NULL;
5496

--- 951 unchanged lines hidden (view full) ---

6448 * return NULL.
6449 *
6450 * If devid and uuid are both specified, the match must be exact, otherwise
6451 * only devid is used.
6452 *
6453 * If @seed is true, traverse through the seed devices.
6454 */
6455struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices,
5509
5510 if (dev_replace_is_ongoing &&
5511 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5512 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5513 srcdev = fs_info->dev_replace.srcdev;
5514 else
5515 srcdev = NULL;
5516

--- 951 unchanged lines hidden (view full) ---

6468 * return NULL.
6469 *
6470 * If devid and uuid are both specified, the match must be exact, otherwise
6471 * only devid is used.
6472 *
6473 * If @seed is true, traverse through the seed devices.
6474 */
6475struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices,
6456 u64 devid, u8 *uuid, u8 *fsid,
6457 bool seed)
6476 u64 devid, u8 *uuid, u8 *fsid)
6458{
6459 struct btrfs_device *device;
6460 struct btrfs_fs_devices *seed_devs;
6461
6462 if (!fsid || !memcmp(fs_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) {
6463 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6464 if (device->devid == devid &&
6465 (!uuid || memcmp(device->uuid, uuid,

--- 190 unchanged lines hidden (view full) ---

6656 for (i = 0; i < num_stripes; i++) {
6657 map->stripes[i].physical =
6658 btrfs_stripe_offset_nr(leaf, chunk, i);
6659 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
6660 read_extent_buffer(leaf, uuid, (unsigned long)
6661 btrfs_stripe_dev_uuid_nr(chunk, i),
6662 BTRFS_UUID_SIZE);
6663 map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices,
6477{
6478 struct btrfs_device *device;
6479 struct btrfs_fs_devices *seed_devs;
6480
6481 if (!fsid || !memcmp(fs_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) {
6482 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6483 if (device->devid == devid &&
6484 (!uuid || memcmp(device->uuid, uuid,

--- 190 unchanged lines hidden (view full) ---

6675 for (i = 0; i < num_stripes; i++) {
6676 map->stripes[i].physical =
6677 btrfs_stripe_offset_nr(leaf, chunk, i);
6678 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
6679 read_extent_buffer(leaf, uuid, (unsigned long)
6680 btrfs_stripe_dev_uuid_nr(chunk, i),
6681 BTRFS_UUID_SIZE);
6682 map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices,
6664 devid, uuid, NULL, true);
6683 devid, uuid, NULL);
6665 if (!map->stripes[i].dev &&
6666 !btrfs_test_opt(fs_info, DEGRADED)) {
6667 free_extent_map(em);
6668 btrfs_report_missing_device(fs_info, devid, uuid, true);
6669 return -ENOENT;
6670 }
6671 if (!map->stripes[i].dev) {
6672 map->stripes[i].dev =

--- 122 unchanged lines hidden (view full) ---

6795
6796 if (memcmp(fs_uuid, fs_devices->metadata_uuid, BTRFS_FSID_SIZE)) {
6797 fs_devices = open_seed_devices(fs_info, fs_uuid);
6798 if (IS_ERR(fs_devices))
6799 return PTR_ERR(fs_devices);
6800 }
6801
6802 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
6684 if (!map->stripes[i].dev &&
6685 !btrfs_test_opt(fs_info, DEGRADED)) {
6686 free_extent_map(em);
6687 btrfs_report_missing_device(fs_info, devid, uuid, true);
6688 return -ENOENT;
6689 }
6690 if (!map->stripes[i].dev) {
6691 map->stripes[i].dev =

--- 122 unchanged lines hidden (view full) ---

6814
6815 if (memcmp(fs_uuid, fs_devices->metadata_uuid, BTRFS_FSID_SIZE)) {
6816 fs_devices = open_seed_devices(fs_info, fs_uuid);
6817 if (IS_ERR(fs_devices))
6818 return PTR_ERR(fs_devices);
6819 }
6820
6821 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
6803 fs_uuid, true);
6822 fs_uuid);
6804 if (!device) {
6805 if (!btrfs_test_opt(fs_info, DEGRADED)) {
6806 btrfs_report_missing_device(fs_info, devid,
6807 dev_uuid, true);
6808 return -ENOENT;
6809 }
6810
6811 device = add_missing_dev(fs_devices, devid, dev_uuid);

--- 46 unchanged lines hidden (view full) ---

6858 if (device->fs_devices != fs_info->fs_devices) {
6859 BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state));
6860 if (device->generation !=
6861 btrfs_device_generation(leaf, dev_item))
6862 return -EINVAL;
6863 }
6864
6865 fill_device_from_item(leaf, dev_item, device);
6823 if (!device) {
6824 if (!btrfs_test_opt(fs_info, DEGRADED)) {
6825 btrfs_report_missing_device(fs_info, devid,
6826 dev_uuid, true);
6827 return -ENOENT;
6828 }
6829
6830 device = add_missing_dev(fs_devices, devid, dev_uuid);

--- 46 unchanged lines hidden (view full) ---

6877 if (device->fs_devices != fs_info->fs_devices) {
6878 BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state));
6879 if (device->generation !=
6880 btrfs_device_generation(leaf, dev_item))
6881 return -EINVAL;
6882 }
6883
6884 fill_device_from_item(leaf, dev_item, device);
6885 if (device->bdev) {
6886 u64 max_total_bytes = i_size_read(device->bdev->bd_inode);
6887
6888 if (device->total_bytes > max_total_bytes) {
6889 btrfs_err(fs_info,
6890 "device total_bytes should be at most %llu but found %llu",
6891 max_total_bytes, device->total_bytes);
6892 return -EINVAL;
6893 }
6894 }
6866 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
6867 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
6868 !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
6869 device->fs_devices->total_rw_bytes += device->total_bytes;
6870 atomic64_add(device->total_bytes - device->bytes_used,
6871 &fs_info->free_chunk_space);
6872 }
6873 ret = 0;

--- 18 unchanged lines hidden (view full) ---

6892 struct btrfs_key key;
6893
6894 ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize);
6895 /*
6896 * This will create extent buffer of nodesize, superblock size is
6897 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6898 * overallocate but we can keep it as-is, only the first page is used.
6899 */
6895 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
6896 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
6897 !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
6898 device->fs_devices->total_rw_bytes += device->total_bytes;
6899 atomic64_add(device->total_bytes - device->bytes_used,
6900 &fs_info->free_chunk_space);
6901 }
6902 ret = 0;

--- 18 unchanged lines hidden (view full) ---

6921 struct btrfs_key key;
6922
6923 ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize);
6924 /*
6925 * This will create extent buffer of nodesize, superblock size is
6926 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6927 * overallocate but we can keep it as-is, only the first page is used.
6928 */
6900 sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET);
6929 sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET,
6930 root->root_key.objectid, 0);
6901 if (IS_ERR(sb))
6902 return PTR_ERR(sb);
6903 set_extent_buffer_uptodate(sb);
6931 if (IS_ERR(sb))
6932 return PTR_ERR(sb);
6933 set_extent_buffer_uptodate(sb);
6904 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
6905 /*
6906 * The sb extent buffer is artificial and just used to read the system array.
6907 * set_extent_buffer_uptodate() call does not properly mark all it's
6908 * pages up-to-date when the page is larger: extent does not cover the
6909 * whole page and consequently check_page_uptodate does not find all
6910 * the page's extents up-to-date (the hole beyond sb),
6911 * write_extent_buffer then triggers a WARN_ON.
6912 *

--- 147 unchanged lines hidden (view full) ---

7060 return ret;
7061}
7062
7063static void readahead_tree_node_children(struct extent_buffer *node)
7064{
7065 int i;
7066 const int nr_items = btrfs_header_nritems(node);
7067
6934 /*
6935 * The sb extent buffer is artificial and just used to read the system array.
6936 * set_extent_buffer_uptodate() call does not properly mark all it's
6937 * pages up-to-date when the page is larger: extent does not cover the
6938 * whole page and consequently check_page_uptodate does not find all
6939 * the page's extents up-to-date (the hole beyond sb),
6940 * write_extent_buffer then triggers a WARN_ON.
6941 *

--- 147 unchanged lines hidden (view full) ---

7089 return ret;
7090}
7091
7092static void readahead_tree_node_children(struct extent_buffer *node)
7093{
7094 int i;
7095 const int nr_items = btrfs_header_nritems(node);
7096
7068 for (i = 0; i < nr_items; i++) {
7069 u64 start;
7070
7071 start = btrfs_node_blockptr(node, i);
7072 readahead_tree_block(node->fs_info, start);
7073 }
7097 for (i = 0; i < nr_items; i++)
7098 btrfs_readahead_node_child(node, i);
7074}
7075
7076int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
7077{
7078 struct btrfs_root *root = fs_info->chunk_root;
7079 struct btrfs_path *path;
7080 struct extent_buffer *leaf;
7081 struct btrfs_key key;

--- 370 unchanged lines hidden (view full) ---

7452int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
7453 struct btrfs_ioctl_get_dev_stats *stats)
7454{
7455 struct btrfs_device *dev;
7456 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7457 int i;
7458
7459 mutex_lock(&fs_devices->device_list_mutex);
7099}
7100
7101int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
7102{
7103 struct btrfs_root *root = fs_info->chunk_root;
7104 struct btrfs_path *path;
7105 struct extent_buffer *leaf;
7106 struct btrfs_key key;

--- 370 unchanged lines hidden (view full) ---

7477int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
7478 struct btrfs_ioctl_get_dev_stats *stats)
7479{
7480 struct btrfs_device *dev;
7481 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7482 int i;
7483
7484 mutex_lock(&fs_devices->device_list_mutex);
7460 dev = btrfs_find_device(fs_info->fs_devices, stats->devid, NULL, NULL,
7461 true);
7485 dev = btrfs_find_device(fs_info->fs_devices, stats->devid, NULL, NULL);
7462 mutex_unlock(&fs_devices->device_list_mutex);
7463
7464 if (!dev) {
7465 btrfs_warn(fs_info, "get dev_stats failed, device not found");
7466 return -ENODEV;
7467 } else if (!dev->dev_stats_valid) {
7468 btrfs_warn(fs_info, "get dev_stats failed, not yet valid");
7469 return -ENODEV;

--- 114 unchanged lines hidden (view full) ---

7584 if (!found) {
7585 btrfs_err(fs_info,
7586 "dev extent physical offset %llu devid %llu has no corresponding chunk",
7587 physical_offset, devid);
7588 ret = -EUCLEAN;
7589 }
7590
7591 /* Make sure no dev extent is beyond device bondary */
7486 mutex_unlock(&fs_devices->device_list_mutex);
7487
7488 if (!dev) {
7489 btrfs_warn(fs_info, "get dev_stats failed, device not found");
7490 return -ENODEV;
7491 } else if (!dev->dev_stats_valid) {
7492 btrfs_warn(fs_info, "get dev_stats failed, not yet valid");
7493 return -ENODEV;

--- 114 unchanged lines hidden (view full) ---

7608 if (!found) {
7609 btrfs_err(fs_info,
7610 "dev extent physical offset %llu devid %llu has no corresponding chunk",
7611 physical_offset, devid);
7612 ret = -EUCLEAN;
7613 }
7614
7615 /* Make sure no dev extent is beyond device bondary */
7592 dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
7616 dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL);
7593 if (!dev) {
7594 btrfs_err(fs_info, "failed to find devid %llu", devid);
7595 ret = -EUCLEAN;
7596 goto out;
7597 }
7598
7617 if (!dev) {
7618 btrfs_err(fs_info, "failed to find devid %llu", devid);
7619 ret = -EUCLEAN;
7620 goto out;
7621 }
7622
7599 /* It's possible this device is a dummy for seed device */
7600 if (dev->disk_total_bytes == 0) {
7601 struct btrfs_fs_devices *devs;
7602
7603 devs = list_first_entry(&fs_info->fs_devices->seed_list,
7604 struct btrfs_fs_devices, seed_list);
7605 dev = btrfs_find_device(devs, devid, NULL, NULL, false);
7606 if (!dev) {
7607 btrfs_err(fs_info, "failed to find seed devid %llu",
7608 devid);
7609 ret = -EUCLEAN;
7610 goto out;
7611 }
7612 }
7613
7614 if (physical_offset + physical_len > dev->disk_total_bytes) {
7615 btrfs_err(fs_info,
7616"dev extent devid %llu physical offset %llu len %llu is beyond device boundary %llu",
7617 devid, physical_offset, physical_len,
7618 dev->disk_total_bytes);
7619 ret = -EUCLEAN;
7620 goto out;
7621 }

--- 38 unchanged lines hidden (view full) ---

7660{
7661 struct btrfs_path *path;
7662 struct btrfs_root *root = fs_info->dev_root;
7663 struct btrfs_key key;
7664 u64 prev_devid = 0;
7665 u64 prev_dev_ext_end = 0;
7666 int ret = 0;
7667
7623 if (physical_offset + physical_len > dev->disk_total_bytes) {
7624 btrfs_err(fs_info,
7625"dev extent devid %llu physical offset %llu len %llu is beyond device boundary %llu",
7626 devid, physical_offset, physical_len,
7627 dev->disk_total_bytes);
7628 ret = -EUCLEAN;
7629 goto out;
7630 }

--- 38 unchanged lines hidden (view full) ---

7669{
7670 struct btrfs_path *path;
7671 struct btrfs_root *root = fs_info->dev_root;
7672 struct btrfs_key key;
7673 u64 prev_devid = 0;
7674 u64 prev_dev_ext_end = 0;
7675 int ret = 0;
7676
7677 /*
7678 * We don't have a dev_root because we mounted with ignorebadroots and
7679 * failed to load the root, so we want to skip the verification in this
7680 * case for sure.
7681 *
7682 * However if the dev root is fine, but the tree itself is corrupted
7683 * we'd still fail to mount. This verification is only to make sure
7684 * writes can happen safely, so instead just bypass this check
7685 * completely in the case of IGNOREBADROOTS.
7686 */
7687 if (btrfs_test_opt(fs_info, IGNOREBADROOTS))
7688 return 0;
7689
7668 key.objectid = 1;
7669 key.type = BTRFS_DEV_EXTENT_KEY;
7670 key.offset = 0;
7671
7672 path = btrfs_alloc_path();
7673 if (!path)
7674 return -ENOMEM;
7675

--- 89 unchanged lines hidden ---
7690 key.objectid = 1;
7691 key.type = BTRFS_DEV_EXTENT_KEY;
7692 key.offset = 0;
7693
7694 path = btrfs_alloc_path();
7695 if (!path)
7696 return -ENOMEM;
7697

--- 89 unchanged lines hidden ---