• Home
  • Raw
  • Download

Lines Matching +full:path +full:- +full:map

1 // SPDX-License-Identifier: GPL-2.0
18 #include "disk-io.h"
20 #include "print-tree.h"
23 #include "rcu-string.h"
24 #include "dev-replace.h"
26 #include "tree-checker.h"
27 #include "space-info.h"
28 #include "block-group.h"
33 #include "uuid-tree.h"
217 size_bp -= ret; \ in btrfs_describe_block_groups()
235 size_bp -= ret; in btrfs_describe_block_groups()
239 buf[size_buf - size_bp - 1] = '\0'; /* remove last | */ in btrfs_describe_block_groups()
256 * There are several mutexes that protect manipulation of devices and low-level
260 * ------------------------
261 * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from
265 * the mutex can be very coarse and can cover long-running operations
270 * global::fs_devs - add, remove, updates to the global list
276 * btrfs_device::name - renames (write side), read is RCU
278 * fs_devices::device_list_mutex (per-fs, with RCU)
279 * ------------------------------------------------
282 * simple list traversal with read-only actions can be done with RCU protection
291 * -------------
296 * -----------
303 * -------------
323 * - Balance (*)
324 * - Device add
325 * - Device remove
326 * - Device replace (*)
327 * - Resize
331 * - Running state
332 * - Paused state
333 * - Completed state
338 * - ioctl (only Balance can be Paused through ioctl)
339 * - filesystem remounted as read-only
340 * - filesystem unmounted and mounted as read-only
341 * - system power-cycle and filesystem mounted as read-only
342 * - filesystem or device errors leading to forced read-only
347 * either by ioctl (Balance only) or when remounted as read-write.
360 * alloc_fs_devices - allocate struct btrfs_fs_devices
377 return ERR_PTR(-ENOMEM); in alloc_fs_devices()
379 mutex_init(&fs_devs->device_list_mutex); in alloc_fs_devices()
381 INIT_LIST_HEAD(&fs_devs->devices); in alloc_fs_devices()
382 INIT_LIST_HEAD(&fs_devs->alloc_list); in alloc_fs_devices()
383 INIT_LIST_HEAD(&fs_devs->fs_list); in alloc_fs_devices()
384 INIT_LIST_HEAD(&fs_devs->seed_list); in alloc_fs_devices()
387 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE); in alloc_fs_devices()
388 memcpy(fs_devs->metadata_uuid, in alloc_fs_devices()
397 WARN_ON(!list_empty(&device->post_commit_list)); in btrfs_free_device()
398 rcu_string_free(device->name); in btrfs_free_device()
399 extent_io_tree_release(&device->alloc_state); in btrfs_free_device()
408 WARN_ON(fs_devices->opened); in free_fs_devices()
409 while (!list_empty(&fs_devices->devices)) { in free_fs_devices()
410 device = list_entry(fs_devices->devices.next, in free_fs_devices()
412 list_del(&device->dev_list); in free_fs_devices()
425 list_del(&fs_devices->fs_list); in btrfs_cleanup_fs_uuids()
433 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) != 0) in match_fsid_fs_devices()
439 if (memcmp(metadata_fsid, fs_devices->metadata_uuid, BTRFS_FSID_SIZE) != 0) in match_fsid_fs_devices()
452 /* Handle non-split brain cases */ in find_fsid()
468 return memcmp(fs_devices->fsid, fs_devices->metadata_uuid, in check_fsid_changed()
470 memcmp(fs_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE) == 0; in check_fsid_changed()
486 if (!fs_devices->fsid_change) in find_fsid_with_metadata_uuid()
489 if (match_fsid_fs_devices(fs_devices, disk_super->metadata_uuid, in find_fsid_with_metadata_uuid()
490 fs_devices->fsid)) in find_fsid_with_metadata_uuid()
501 if (!fs_devices->fsid_change) in find_fsid_with_metadata_uuid()
504 if (check_fsid_changed(fs_devices, disk_super->metadata_uuid)) in find_fsid_with_metadata_uuid()
508 return find_fsid(disk_super->fsid, disk_super->metadata_uuid); in find_fsid_with_metadata_uuid()
558 * -EBUSY if @devt is a mounted device.
559 * -ENOENT if @devt does not match any device in the list.
570 ret = -ENOENT; in btrfs_free_stale_devices()
574 mutex_lock(&fs_devices->device_list_mutex); in btrfs_free_stale_devices()
576 &fs_devices->devices, dev_list) { in btrfs_free_stale_devices()
579 if (devt && devt != device->devt) in btrfs_free_stale_devices()
581 if (fs_devices->opened) { in btrfs_free_stale_devices()
584 ret = -EBUSY; in btrfs_free_stale_devices()
589 fs_devices->num_devices--; in btrfs_free_stale_devices()
590 list_del(&device->dev_list); in btrfs_free_stale_devices()
595 mutex_unlock(&fs_devices->device_list_mutex); in btrfs_free_stale_devices()
597 if (fs_devices->num_devices == 0) { in btrfs_free_stale_devices()
599 list_del(&fs_devices->fs_list); in btrfs_free_stale_devices()
610 * fs_devices->device_list_mutex here.
621 if (device->bdev) in btrfs_open_one_device()
622 return -EINVAL; in btrfs_open_one_device()
623 if (!device->name) in btrfs_open_one_device()
624 return -EINVAL; in btrfs_open_one_device()
626 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1, in btrfs_open_one_device()
631 devid = btrfs_stack_device_id(&disk_super->dev_item); in btrfs_open_one_device()
632 if (devid != device->devid) in btrfs_open_one_device()
635 if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE)) in btrfs_open_one_device()
638 device->generation = btrfs_super_generation(disk_super); in btrfs_open_one_device()
644 "BTRFS: Invalid seeding and uuid-changed device detected\n"); in btrfs_open_one_device()
648 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); in btrfs_open_one_device()
649 fs_devices->seeding = true; in btrfs_open_one_device()
652 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); in btrfs_open_one_device()
654 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); in btrfs_open_one_device()
658 fs_devices->rotating = true; in btrfs_open_one_device()
661 fs_devices->discardable = true; in btrfs_open_one_device()
663 device->bdev = bdev; in btrfs_open_one_device()
664 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); in btrfs_open_one_device()
665 device->holder = holder; in btrfs_open_one_device()
667 fs_devices->open_devices++; in btrfs_open_one_device()
668 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && in btrfs_open_one_device()
669 device->devid != BTRFS_DEV_REPLACE_DEVID) { in btrfs_open_one_device()
670 fs_devices->rw_devices++; in btrfs_open_one_device()
671 list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list); in btrfs_open_one_device()
681 return -EINVAL; in btrfs_open_one_device()
689 return has_metadata_uuid ? sb->metadata_uuid : sb->fsid; in btrfs_sb_fsid_ptr()
694 struct path old = { .mnt = NULL, .dentry = NULL }; in is_same_device()
695 struct path new = { .mnt = NULL, .dentry = NULL }; in is_same_device()
700 if (!device->name) in is_same_device()
708 ret = strscpy(old_path, rcu_str_deref(device->name), PATH_MAX); in is_same_device()
740 if (fs_devices->fsid_change) in find_fsid_inprogress()
743 if (check_fsid_changed(fs_devices, disk_super->fsid)) in find_fsid_inprogress()
747 return find_fsid(disk_super->fsid, NULL); in find_fsid_inprogress()
760 * 1 - The fs still continues to have different METADATA/FSID uuids. in find_fsid_changed()
761 * 2 - The fs is switched back to its original FSID (METADATA/FSID in find_fsid_changed()
766 if (check_fsid_changed(fs_devices, disk_super->metadata_uuid) && in find_fsid_changed()
767 memcmp(fs_devices->fsid, disk_super->fsid, in find_fsid_changed()
772 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid, in find_fsid_changed()
774 memcmp(fs_devices->fsid, disk_super->metadata_uuid, in find_fsid_changed()
797 if (!fs_devices->fsid_change) in find_fsid_reverted_metadata()
800 if (check_fsid_changed(fs_devices, disk_super->fsid)) in find_fsid_reverted_metadata()
813 static noinline struct btrfs_device *device_list_add(const char *path, in device_list_add() argument
821 u64 devid = btrfs_stack_device_id(&disk_super->dev_item); in device_list_add()
829 error = lookup_bdev(path, &path_devt); in device_list_add()
831 btrfs_err(NULL, "failed to lookup block device for path %s: %d", in device_list_add()
832 path, error); in device_list_add()
846 fs_devices = find_fsid(disk_super->fsid, NULL); in device_list_add()
851 fs_devices = alloc_fs_devices(disk_super->fsid, in device_list_add()
852 has_metadata_uuid ? disk_super->metadata_uuid : NULL); in device_list_add()
856 fs_devices->fsid_change = fsid_change_in_progress; in device_list_add()
858 mutex_lock(&fs_devices->device_list_mutex); in device_list_add()
859 list_add(&fs_devices->fs_list, &fs_uuids); in device_list_add()
865 .uuid = disk_super->dev_item.uuid, in device_list_add()
868 mutex_lock(&fs_devices->device_list_mutex); in device_list_add()
876 if (fs_devices->fsid_change && in device_list_add()
877 found_transid > fs_devices->latest_generation) { in device_list_add()
878 memcpy(fs_devices->fsid, disk_super->fsid, in device_list_add()
880 memcpy(fs_devices->metadata_uuid, in device_list_add()
882 fs_devices->fsid_change = false; in device_list_add()
889 if (fs_devices->opened) { in device_list_add()
892 path, fs_devices->fsid, current->comm, in device_list_add()
894 mutex_unlock(&fs_devices->device_list_mutex); in device_list_add()
895 return ERR_PTR(-EBUSY); in device_list_add()
900 disk_super->dev_item.uuid, path); in device_list_add()
903 mutex_unlock(&fs_devices->device_list_mutex); in device_list_add()
908 device->devt = path_devt; in device_list_add()
910 list_add_rcu(&device->dev_list, &fs_devices->devices); in device_list_add()
911 fs_devices->num_devices++; in device_list_add()
913 device->fs_devices = fs_devices; in device_list_add()
916 if (disk_super->label[0]) in device_list_add()
919 disk_super->label, devid, found_transid, path, in device_list_add()
920 current->comm, task_pid_nr(current)); in device_list_add()
924 disk_super->fsid, devid, found_transid, path, in device_list_add()
925 current->comm, task_pid_nr(current)); in device_list_add()
927 } else if (!device->name || !is_same_device(device, path)) { in device_list_add()
930 * 1. If you are here and if the device->name is NULL that in device_list_add()
932 * 2. If you are here and if the device->name is different in device_list_add()
933 * from 'path' that means either in device_list_add()
936 * b. The missing-disk-which-was-replaced, has in device_list_add()
942 * Further in case of 1 and 2a above, the disk at 'path' in device_list_add()
954 if (!fs_devices->opened && found_transid < device->generation) { in device_list_add()
959 * with larger generation number or the last-in if in device_list_add()
962 mutex_unlock(&fs_devices->device_list_mutex); in device_list_add()
965 path, found_transid, device->generation); in device_list_add()
966 return ERR_PTR(-EEXIST); in device_list_add()
970 * We are going to replace the device path for a given devid, in device_list_add()
973 * NOTE: the device->fs_info may not be reliable here so pass in device_list_add()
975 * use-after-free when the fs_info and fs_info->sb are already in device_list_add()
978 if (device->bdev) { in device_list_add()
979 if (device->devt != path_devt) { in device_list_add()
980 mutex_unlock(&fs_devices->device_list_mutex); in device_list_add()
983 path, devid, found_transid, in device_list_add()
984 current->comm, in device_list_add()
986 return ERR_PTR(-EEXIST); in device_list_add()
989 "devid %llu device path %s changed to %s scanned by %s (%d)", in device_list_add()
991 path, current->comm, in device_list_add()
995 name = rcu_string_strdup(path, GFP_NOFS); in device_list_add()
997 mutex_unlock(&fs_devices->device_list_mutex); in device_list_add()
998 return ERR_PTR(-ENOMEM); in device_list_add()
1000 rcu_string_free(device->name); in device_list_add()
1001 rcu_assign_pointer(device->name, name); in device_list_add()
1002 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) { in device_list_add()
1003 fs_devices->missing_devices--; in device_list_add()
1004 clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); in device_list_add()
1006 device->devt = path_devt; in device_list_add()
1015 if (!fs_devices->opened) { in device_list_add()
1016 device->generation = found_transid; in device_list_add()
1017 fs_devices->latest_generation = max_t(u64, found_transid, in device_list_add()
1018 fs_devices->latest_generation); in device_list_add()
1021 fs_devices->total_devices = btrfs_super_num_devices(disk_super); in device_list_add()
1023 mutex_unlock(&fs_devices->device_list_mutex); in device_list_add()
1036 fs_devices = alloc_fs_devices(orig->fsid, NULL); in clone_fs_devices()
1040 fs_devices->total_devices = orig->total_devices; in clone_fs_devices()
1042 list_for_each_entry(orig_dev, &orig->devices, dev_list) { in clone_fs_devices()
1049 if (orig_dev->name) in clone_fs_devices()
1050 dev_path = orig_dev->name->str; in clone_fs_devices()
1052 device = btrfs_alloc_device(NULL, &orig_dev->devid, in clone_fs_devices()
1053 orig_dev->uuid, dev_path); in clone_fs_devices()
1059 if (orig_dev->zone_info) { in clone_fs_devices()
1065 ret = -ENOMEM; in clone_fs_devices()
1068 device->zone_info = zone_info; in clone_fs_devices()
1071 list_add(&device->dev_list, &fs_devices->devices); in clone_fs_devices()
1072 device->fs_devices = fs_devices; in clone_fs_devices()
1073 fs_devices->num_devices++; in clone_fs_devices()
1086 /* This is the initialized path, it is safe to release the devices. */ in __btrfs_free_extra_devids()
1087 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { in __btrfs_free_extra_devids()
1088 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state)) { in __btrfs_free_extra_devids()
1090 &device->dev_state) && in __btrfs_free_extra_devids()
1092 &device->dev_state) && in __btrfs_free_extra_devids()
1094 device->generation > (*latest_dev)->generation)) { in __btrfs_free_extra_devids()
1104 if (device->devid == BTRFS_DEV_REPLACE_DEVID) in __btrfs_free_extra_devids()
1107 if (device->bdev) { in __btrfs_free_extra_devids()
1108 blkdev_put(device->bdev, device->holder); in __btrfs_free_extra_devids()
1109 device->bdev = NULL; in __btrfs_free_extra_devids()
1110 fs_devices->open_devices--; in __btrfs_free_extra_devids()
1112 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { in __btrfs_free_extra_devids()
1113 list_del_init(&device->dev_alloc_list); in __btrfs_free_extra_devids()
1114 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); in __btrfs_free_extra_devids()
1115 fs_devices->rw_devices--; in __btrfs_free_extra_devids()
1117 list_del_init(&device->dev_list); in __btrfs_free_extra_devids()
1118 fs_devices->num_devices--; in __btrfs_free_extra_devids()
1136 list_for_each_entry(seed_dev, &fs_devices->seed_list, seed_list) in btrfs_free_extra_devids()
1139 fs_devices->latest_dev = latest_dev; in btrfs_free_extra_devids()
1146 if (!device->bdev) in btrfs_close_bdev()
1149 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { in btrfs_close_bdev()
1150 sync_blockdev(device->bdev); in btrfs_close_bdev()
1151 invalidate_bdev(device->bdev); in btrfs_close_bdev()
1154 blkdev_put(device->bdev, device->holder); in btrfs_close_bdev()
1159 struct btrfs_fs_devices *fs_devices = device->fs_devices; in btrfs_close_one_device()
1161 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && in btrfs_close_one_device()
1162 device->devid != BTRFS_DEV_REPLACE_DEVID) { in btrfs_close_one_device()
1163 list_del_init(&device->dev_alloc_list); in btrfs_close_one_device()
1164 fs_devices->rw_devices--; in btrfs_close_one_device()
1167 if (device->devid == BTRFS_DEV_REPLACE_DEVID) in btrfs_close_one_device()
1168 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); in btrfs_close_one_device()
1170 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) { in btrfs_close_one_device()
1171 clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); in btrfs_close_one_device()
1172 fs_devices->missing_devices--; in btrfs_close_one_device()
1176 if (device->bdev) { in btrfs_close_one_device()
1177 fs_devices->open_devices--; in btrfs_close_one_device()
1178 device->bdev = NULL; in btrfs_close_one_device()
1180 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); in btrfs_close_one_device()
1183 device->fs_info = NULL; in btrfs_close_one_device()
1184 atomic_set(&device->dev_stats_ccnt, 0); in btrfs_close_one_device()
1185 extent_io_tree_release(&device->alloc_state); in btrfs_close_one_device()
1193 * (btrfs_check_rw_degradable() should not fail) - if after mounting the in btrfs_close_one_device()
1198 device->last_flush_error = 0; in btrfs_close_one_device()
1201 WARN_ON(test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state)); in btrfs_close_one_device()
1202 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)); in btrfs_close_one_device()
1203 WARN_ON(!list_empty(&device->dev_alloc_list)); in btrfs_close_one_device()
1204 WARN_ON(!list_empty(&device->post_commit_list)); in btrfs_close_one_device()
1213 if (--fs_devices->opened > 0) in close_fs_devices()
1216 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) in close_fs_devices()
1219 WARN_ON(fs_devices->open_devices); in close_fs_devices()
1220 WARN_ON(fs_devices->rw_devices); in close_fs_devices()
1221 fs_devices->opened = 0; in close_fs_devices()
1222 fs_devices->seeding = false; in close_fs_devices()
1223 fs_devices->fs_info = NULL; in close_fs_devices()
1233 if (!fs_devices->opened) { in btrfs_close_devices()
1234 list_splice_init(&fs_devices->seed_list, &list); in btrfs_close_devices()
1238 * other device, it can be re-initialized during the next mount in btrfs_close_devices()
1239 * without the needing device-scan step. Therefore, it can be in btrfs_close_devices()
1242 if (fs_devices->num_devices == 1) { in btrfs_close_devices()
1243 list_del(&fs_devices->fs_list); in btrfs_close_devices()
1251 list_del(&fs_devices->seed_list); in btrfs_close_devices()
1265 list_for_each_entry_safe(device, tmp_device, &fs_devices->devices, in open_fs_devices()
1271 (!latest_dev || device->generation > latest_dev->generation)) { in open_fs_devices()
1273 } else if (ret2 == -ENODATA) { in open_fs_devices()
1274 fs_devices->num_devices--; in open_fs_devices()
1275 list_del(&device->dev_list); in open_fs_devices()
1282 if (fs_devices->open_devices == 0) { in open_fs_devices()
1285 return -EINVAL; in open_fs_devices()
1288 fs_devices->opened = 1; in open_fs_devices()
1289 fs_devices->latest_dev = latest_dev; in open_fs_devices()
1290 fs_devices->total_rw_bytes = 0; in open_fs_devices()
1291 fs_devices->chunk_alloc_policy = BTRFS_CHUNK_ALLOC_REGULAR; in open_fs_devices()
1292 fs_devices->read_policy = BTRFS_READ_POLICY_PID; in open_fs_devices()
1305 if (dev1->devid < dev2->devid) in devid_cmp()
1306 return -1; in devid_cmp()
1307 else if (dev1->devid > dev2->devid) in devid_cmp()
1326 if (fs_devices->opened) { in btrfs_open_devices()
1327 fs_devices->opened++; in btrfs_open_devices()
1330 list_sort(NULL, &fs_devices->devices, devid_cmp); in btrfs_open_devices()
1354 return ERR_PTR(-EINVAL); in btrfs_read_disk_super()
1358 return ERR_PTR(-EINVAL); in btrfs_read_disk_super()
1362 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_SHIFT != index) in btrfs_read_disk_super()
1363 return ERR_PTR(-EINVAL); in btrfs_read_disk_super()
1366 page = read_cache_page_gfp(bdev->bd_inode->i_mapping, index, GFP_KERNEL); in btrfs_read_disk_super()
1379 return ERR_PTR(-EINVAL); in btrfs_read_disk_super()
1382 if (disk_super->label[0] && disk_super->label[BTRFS_LABEL_SIZE - 1]) in btrfs_read_disk_super()
1383 disk_super->label[BTRFS_LABEL_SIZE - 1] = 0; in btrfs_read_disk_super()
1400 * Look for a btrfs signature on a device. This may be called out of the mount path
1404 struct btrfs_device *btrfs_scan_one_device(const char *path, blk_mode_t flags) in btrfs_scan_one_device() argument
1423 * Avoid an exclusive open here, as the systemd-udev may initiate the in btrfs_scan_one_device()
1432 bdev = blkdev_get_by_path(path, flags, NULL, NULL); in btrfs_scan_one_device()
1449 device = device_list_add(path, disk_super, &new_device_added); in btrfs_scan_one_device()
1451 btrfs_free_stale_devices(device->devt, device); in btrfs_scan_one_device()
1470 lockdep_assert_held(&device->fs_info->chunk_mutex); in contains_pending_extent()
1472 if (find_first_extent_bit(&device->alloc_state, *start, in contains_pending_extent()
1478 physical_end + 1 - physical_start)) { in contains_pending_extent()
1488 switch (device->fs_devices->chunk_alloc_policy) { in dev_extent_search_start()
1507 u64 zone_size = device->zone_info->zone_size; in dev_extent_hole_check_zoned()
1519 *hole_size = *hole_start + *hole_size - pos; in dev_extent_hole_check_zoned()
1533 if (ret == -ERANGE) { in dev_extent_hole_check_zoned()
1540 *hole_size -= zone_size; in dev_extent_hole_check_zoned()
1571 *hole_size = hole_end - *hole_start; in dev_extent_hole_check()
1577 switch (device->fs_devices->chunk_alloc_policy) { in dev_extent_hole_check()
1632 struct btrfs_fs_info *fs_info = device->fs_info; in find_free_dev_extent()
1633 struct btrfs_root *root = fs_info->dev_root; in find_free_dev_extent()
1636 struct btrfs_path *path; in find_free_dev_extent() local
1642 u64 search_end = device->total_bytes; in find_free_dev_extent()
1650 WARN_ON(device->zone_info && in find_free_dev_extent()
1651 !IS_ALIGNED(num_bytes, device->zone_info->zone_size)); in find_free_dev_extent()
1653 path = btrfs_alloc_path(); in find_free_dev_extent()
1654 if (!path) { in find_free_dev_extent()
1655 ret = -ENOMEM; in find_free_dev_extent()
1660 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { in find_free_dev_extent()
1661 ret = -ENOSPC; in find_free_dev_extent()
1665 path->reada = READA_FORWARD; in find_free_dev_extent()
1666 path->search_commit_root = 1; in find_free_dev_extent()
1667 path->skip_locking = 1; in find_free_dev_extent()
1669 key.objectid = device->devid; in find_free_dev_extent()
1673 ret = btrfs_search_backwards(root, &key, path); in find_free_dev_extent()
1678 l = path->nodes[0]; in find_free_dev_extent()
1679 slot = path->slots[0]; in find_free_dev_extent()
1681 ret = btrfs_next_leaf(root, path); in find_free_dev_extent()
1691 if (key.objectid < device->devid) in find_free_dev_extent()
1694 if (key.objectid > device->devid) in find_free_dev_extent()
1704 hole_size = key.offset - search_start; in find_free_dev_extent()
1734 path->slots[0]++; in find_free_dev_extent()
1744 hole_size = search_end - search_start; in find_free_dev_extent()
1747 btrfs_release_path(path); in find_free_dev_extent()
1759 ret = -ENOSPC; in find_free_dev_extent()
1765 btrfs_free_path(path); in find_free_dev_extent()
1776 struct btrfs_fs_info *fs_info = device->fs_info; in btrfs_free_dev_extent()
1777 struct btrfs_root *root = fs_info->dev_root; in btrfs_free_dev_extent()
1779 struct btrfs_path *path; in btrfs_free_dev_extent() local
1785 path = btrfs_alloc_path(); in btrfs_free_dev_extent()
1786 if (!path) in btrfs_free_dev_extent()
1787 return -ENOMEM; in btrfs_free_dev_extent()
1789 key.objectid = device->devid; in btrfs_free_dev_extent()
1793 ret = btrfs_search_slot(trans, root, &key, path, -1, 1); in btrfs_free_dev_extent()
1795 ret = btrfs_previous_item(root, path, key.objectid, in btrfs_free_dev_extent()
1799 leaf = path->nodes[0]; in btrfs_free_dev_extent()
1800 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); in btrfs_free_dev_extent()
1801 extent = btrfs_item_ptr(leaf, path->slots[0], in btrfs_free_dev_extent()
1806 btrfs_release_path(path); in btrfs_free_dev_extent()
1809 leaf = path->nodes[0]; in btrfs_free_dev_extent()
1810 extent = btrfs_item_ptr(leaf, path->slots[0], in btrfs_free_dev_extent()
1818 ret = btrfs_del_item(trans, root, path); in btrfs_free_dev_extent()
1820 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags); in btrfs_free_dev_extent()
1822 btrfs_free_path(path); in btrfs_free_dev_extent()
1833 em_tree = &fs_info->mapping_tree; in find_next_chunk()
1834 read_lock(&em_tree->lock); in find_next_chunk()
1835 n = rb_last(&em_tree->map.rb_root); in find_next_chunk()
1838 ret = em->start + em->len; in find_next_chunk()
1840 read_unlock(&em_tree->lock); in find_next_chunk()
1851 struct btrfs_path *path; in find_next_devid() local
1853 path = btrfs_alloc_path(); in find_next_devid()
1854 if (!path) in find_next_devid()
1855 return -ENOMEM; in find_next_devid()
1859 key.offset = (u64)-1; in find_next_devid()
1861 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0); in find_next_devid()
1867 btrfs_err(fs_info, "corrupted chunk tree devid -1 matched"); in find_next_devid()
1868 ret = -EUCLEAN; in find_next_devid()
1872 ret = btrfs_previous_item(fs_info->chunk_root, path, in find_next_devid()
1878 btrfs_item_key_to_cpu(path->nodes[0], &found_key, in find_next_devid()
1879 path->slots[0]); in find_next_devid()
1884 btrfs_free_path(path); in find_next_devid()
1896 struct btrfs_path *path; in btrfs_add_dev_item() local
1902 path = btrfs_alloc_path(); in btrfs_add_dev_item()
1903 if (!path) in btrfs_add_dev_item()
1904 return -ENOMEM; in btrfs_add_dev_item()
1908 key.offset = device->devid; in btrfs_add_dev_item()
1911 ret = btrfs_insert_empty_item(trans, trans->fs_info->chunk_root, path, in btrfs_add_dev_item()
1917 leaf = path->nodes[0]; in btrfs_add_dev_item()
1918 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); in btrfs_add_dev_item()
1920 btrfs_set_device_id(leaf, dev_item, device->devid); in btrfs_add_dev_item()
1922 btrfs_set_device_type(leaf, dev_item, device->type); in btrfs_add_dev_item()
1923 btrfs_set_device_io_align(leaf, dev_item, device->io_align); in btrfs_add_dev_item()
1924 btrfs_set_device_io_width(leaf, dev_item, device->io_width); in btrfs_add_dev_item()
1925 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); in btrfs_add_dev_item()
1936 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); in btrfs_add_dev_item()
1938 write_extent_buffer(leaf, trans->fs_info->fs_devices->metadata_uuid, in btrfs_add_dev_item()
1944 btrfs_free_path(path); in btrfs_add_dev_item()
1949 * Function to update ctime/mtime for a given device path.
1956 struct path path; in update_dev_time() local
1959 ret = kern_path(device_path, LOOKUP_FOLLOW, &path); in update_dev_time()
1963 inode_update_time(d_inode(path.dentry), S_MTIME | S_CTIME | S_VERSION); in update_dev_time()
1964 path_put(&path); in update_dev_time()
1970 struct btrfs_root *root = device->fs_info->chunk_root; in btrfs_rm_dev_item()
1972 struct btrfs_path *path; in btrfs_rm_dev_item() local
1975 path = btrfs_alloc_path(); in btrfs_rm_dev_item()
1976 if (!path) in btrfs_rm_dev_item()
1977 return -ENOMEM; in btrfs_rm_dev_item()
1981 key.offset = device->devid; in btrfs_rm_dev_item()
1984 ret = btrfs_search_slot(trans, root, &key, path, -1, 1); in btrfs_rm_dev_item()
1988 ret = -ENOENT; in btrfs_rm_dev_item()
1992 ret = btrfs_del_item(trans, root, path); in btrfs_rm_dev_item()
1994 btrfs_free_path(path); in btrfs_rm_dev_item()
2011 seq = read_seqbegin(&fs_info->profiles_lock); in btrfs_check_raid_min_devices()
2013 all_avail = fs_info->avail_data_alloc_bits | in btrfs_check_raid_min_devices()
2014 fs_info->avail_system_alloc_bits | in btrfs_check_raid_min_devices()
2015 fs_info->avail_metadata_alloc_bits; in btrfs_check_raid_min_devices()
2016 } while (read_seqretry(&fs_info->profiles_lock, seq)); in btrfs_check_raid_min_devices()
2034 list_for_each_entry(next_device, &fs_devs->devices, dev_list) { in btrfs_find_next_active_device()
2036 !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state) in btrfs_find_next_active_device()
2037 && next_device->bdev) in btrfs_find_next_active_device()
2053 struct btrfs_fs_info *fs_info = device->fs_info; in btrfs_assign_next_active_device()
2056 next_device = btrfs_find_next_active_device(fs_info->fs_devices, in btrfs_assign_next_active_device()
2060 if (fs_info->sb->s_bdev && in btrfs_assign_next_active_device()
2061 (fs_info->sb->s_bdev == device->bdev)) in btrfs_assign_next_active_device()
2062 fs_info->sb->s_bdev = next_device->bdev; in btrfs_assign_next_active_device()
2064 if (fs_info->fs_devices->latest_dev->bdev == device->bdev) in btrfs_assign_next_active_device()
2065 fs_info->fs_devices->latest_dev = next_device; in btrfs_assign_next_active_device()
2074 u64 num_devices = fs_info->fs_devices->num_devices; in btrfs_num_devices()
2076 down_read(&fs_info->dev_replace.rwsem); in btrfs_num_devices()
2077 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) { in btrfs_num_devices()
2079 num_devices--; in btrfs_num_devices()
2081 up_read(&fs_info->dev_replace.rwsem); in btrfs_num_devices()
2090 const size_t len = sizeof(disk_super->magic); in btrfs_scratch_superblock()
2098 memset(&disk_super->magic, 0, len); in btrfs_scratch_superblock()
2102 ret = sync_blockdev_range(bdev, bytenr, bytenr + len - 1); in btrfs_scratch_superblock()
2127 /* Update ctime/mtime for device path for libblkid */ in btrfs_scratch_superblocks()
2138 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; in btrfs_rm_device()
2144 return -EINVAL; in btrfs_rm_device()
2154 ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1); in btrfs_rm_device()
2158 device = btrfs_find_device(fs_info->fs_devices, args); in btrfs_rm_device()
2160 if (args->missing) in btrfs_rm_device()
2163 ret = -ENOENT; in btrfs_rm_device()
2170 btrfs_dev_name(device), device->devid); in btrfs_rm_device()
2171 return -ETXTBSY; in btrfs_rm_device()
2174 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) in btrfs_rm_device()
2177 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && in btrfs_rm_device()
2178 fs_info->fs_devices->rw_devices == 1) in btrfs_rm_device()
2181 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { in btrfs_rm_device()
2182 mutex_lock(&fs_info->chunk_mutex); in btrfs_rm_device()
2183 list_del_init(&device->dev_alloc_list); in btrfs_rm_device()
2184 device->fs_devices->rw_devices--; in btrfs_rm_device()
2185 mutex_unlock(&fs_info->chunk_mutex); in btrfs_rm_device()
2192 trans = btrfs_start_transaction(fs_info->chunk_root, 0); in btrfs_rm_device()
2203 device->devid, ret); in btrfs_rm_device()
2209 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); in btrfs_rm_device()
2225 * its own fs_devices listed under the fs_devices->seed_list. in btrfs_rm_device()
2227 cur_devices = device->fs_devices; in btrfs_rm_device()
2228 mutex_lock(&fs_devices->device_list_mutex); in btrfs_rm_device()
2229 list_del_rcu(&device->dev_list); in btrfs_rm_device()
2231 cur_devices->num_devices--; in btrfs_rm_device()
2232 cur_devices->total_devices--; in btrfs_rm_device()
2235 fs_devices->total_devices--; in btrfs_rm_device()
2237 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) in btrfs_rm_device()
2238 cur_devices->missing_devices--; in btrfs_rm_device()
2242 if (device->bdev) { in btrfs_rm_device()
2243 cur_devices->open_devices--; in btrfs_rm_device()
2248 num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1; in btrfs_rm_device()
2249 btrfs_set_super_num_devices(fs_info->super_copy, num_devices); in btrfs_rm_device()
2250 mutex_unlock(&fs_devices->device_list_mutex); in btrfs_rm_device()
2258 * write lock, and blkdev_put() will pull in the ->open_mutex on the in btrfs_rm_device()
2262 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { in btrfs_rm_device()
2263 btrfs_scratch_superblocks(fs_info, device->bdev, in btrfs_rm_device()
2264 device->name->str); in btrfs_rm_device()
2265 if (device->bdev) { in btrfs_rm_device()
2266 sync_blockdev(device->bdev); in btrfs_rm_device()
2267 invalidate_bdev(device->bdev); in btrfs_rm_device()
2271 *bdev = device->bdev; in btrfs_rm_device()
2272 *holder = device->holder; in btrfs_rm_device()
2280 * seed_devices, we can simply decrement cur_devices->opened and then in btrfs_rm_device()
2283 if (cur_devices->num_devices == 0) { in btrfs_rm_device()
2284 list_del_init(&cur_devices->seed_list); in btrfs_rm_device()
2285 ASSERT(cur_devices->opened == 1); in btrfs_rm_device()
2286 cur_devices->opened--; in btrfs_rm_device()
2295 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { in btrfs_rm_device()
2296 mutex_lock(&fs_info->chunk_mutex); in btrfs_rm_device()
2297 list_add(&device->dev_alloc_list, in btrfs_rm_device()
2298 &fs_devices->alloc_list); in btrfs_rm_device()
2299 device->fs_devices->rw_devices++; in btrfs_rm_device()
2300 mutex_unlock(&fs_info->chunk_mutex); in btrfs_rm_device()
2309 lockdep_assert_held(&srcdev->fs_info->fs_devices->device_list_mutex); in btrfs_rm_dev_replace_remove_srcdev()
2312 * in case of fs with no seed, srcdev->fs_devices will point in btrfs_rm_dev_replace_remove_srcdev()
2317 fs_devices = srcdev->fs_devices; in btrfs_rm_dev_replace_remove_srcdev()
2319 list_del_rcu(&srcdev->dev_list); in btrfs_rm_dev_replace_remove_srcdev()
2320 list_del(&srcdev->dev_alloc_list); in btrfs_rm_dev_replace_remove_srcdev()
2321 fs_devices->num_devices--; in btrfs_rm_dev_replace_remove_srcdev()
2322 if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state)) in btrfs_rm_dev_replace_remove_srcdev()
2323 fs_devices->missing_devices--; in btrfs_rm_dev_replace_remove_srcdev()
2325 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) in btrfs_rm_dev_replace_remove_srcdev()
2326 fs_devices->rw_devices--; in btrfs_rm_dev_replace_remove_srcdev()
2328 if (srcdev->bdev) in btrfs_rm_dev_replace_remove_srcdev()
2329 fs_devices->open_devices--; in btrfs_rm_dev_replace_remove_srcdev()
2334 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices; in btrfs_rm_dev_replace_free_srcdev()
2343 if (!fs_devices->num_devices) { in btrfs_rm_dev_replace_free_srcdev()
2350 ASSERT(fs_devices->seeding); in btrfs_rm_dev_replace_free_srcdev()
2352 list_del_init(&fs_devices->seed_list); in btrfs_rm_dev_replace_free_srcdev()
2361 struct btrfs_fs_devices *fs_devices = tgtdev->fs_info->fs_devices; in btrfs_destroy_dev_replace_tgtdev()
2363 mutex_lock(&fs_devices->device_list_mutex); in btrfs_destroy_dev_replace_tgtdev()
2367 if (tgtdev->bdev) in btrfs_destroy_dev_replace_tgtdev()
2368 fs_devices->open_devices--; in btrfs_destroy_dev_replace_tgtdev()
2370 fs_devices->num_devices--; in btrfs_destroy_dev_replace_tgtdev()
2374 list_del_rcu(&tgtdev->dev_list); in btrfs_destroy_dev_replace_tgtdev()
2376 mutex_unlock(&fs_devices->device_list_mutex); in btrfs_destroy_dev_replace_tgtdev()
2378 btrfs_scratch_superblocks(tgtdev->fs_info, tgtdev->bdev, in btrfs_destroy_dev_replace_tgtdev()
2379 tgtdev->name->str); in btrfs_destroy_dev_replace_tgtdev()
2387 * Populate args from device at path.
2391 * @path: the path to the device
2393 * This will read the super block of the device at @path and populate @args with
2397 * and does some basic sanity checks. The caller must make sure that @path is
2402 * Return: 0 for success, -errno for failure
2406 const char *path) in btrfs_get_dev_args_from_path() argument
2412 if (!path || !path[0]) in btrfs_get_dev_args_from_path()
2413 return -EINVAL; in btrfs_get_dev_args_from_path()
2414 if (!strcmp(path, "missing")) { in btrfs_get_dev_args_from_path()
2415 args->missing = true; in btrfs_get_dev_args_from_path()
2419 args->uuid = kzalloc(BTRFS_UUID_SIZE, GFP_KERNEL); in btrfs_get_dev_args_from_path()
2420 args->fsid = kzalloc(BTRFS_FSID_SIZE, GFP_KERNEL); in btrfs_get_dev_args_from_path()
2421 if (!args->uuid || !args->fsid) { in btrfs_get_dev_args_from_path()
2423 return -ENOMEM; in btrfs_get_dev_args_from_path()
2426 ret = btrfs_get_bdev_and_sb(path, BLK_OPEN_READ, NULL, 0, in btrfs_get_dev_args_from_path()
2433 args->devid = btrfs_stack_device_id(&disk_super->dev_item); in btrfs_get_dev_args_from_path()
2434 memcpy(args->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE); in btrfs_get_dev_args_from_path()
2436 memcpy(args->fsid, disk_super->metadata_uuid, BTRFS_FSID_SIZE); in btrfs_get_dev_args_from_path()
2438 memcpy(args->fsid, disk_super->fsid, BTRFS_FSID_SIZE); in btrfs_get_dev_args_from_path()
2446 * allocate our ->uuid and ->fsid pointers, everybody else uses local variables
2451 kfree(args->uuid); in btrfs_put_dev_args_from_path()
2452 kfree(args->fsid); in btrfs_put_dev_args_from_path()
2453 args->uuid = NULL; in btrfs_put_dev_args_from_path()
2454 args->fsid = NULL; in btrfs_put_dev_args_from_path()
2467 device = btrfs_find_device(fs_info->fs_devices, &args); in btrfs_find_device_by_devspec()
2469 return ERR_PTR(-ENOENT); in btrfs_find_device_by_devspec()
2476 device = btrfs_find_device(fs_info->fs_devices, &args); in btrfs_find_device_by_devspec()
2479 return ERR_PTR(-ENOENT); in btrfs_find_device_by_devspec()
2485 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; in btrfs_init_sprout()
2490 if (!fs_devices->seeding) in btrfs_init_sprout()
2491 return ERR_PTR(-EINVAL); in btrfs_init_sprout()
2495 * fs_info->fs_devices->seed_list in btrfs_init_sprout()
2513 list_add(&old_devices->fs_list, &fs_uuids); in btrfs_init_sprout()
2516 seed_devices->opened = 1; in btrfs_init_sprout()
2517 INIT_LIST_HEAD(&seed_devices->devices); in btrfs_init_sprout()
2518 INIT_LIST_HEAD(&seed_devices->alloc_list); in btrfs_init_sprout()
2519 mutex_init(&seed_devices->device_list_mutex); in btrfs_init_sprout()
2526 * Generate a new fsid for the sprouted read-write filesystem.
2531 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; in btrfs_setup_sprout()
2532 struct btrfs_super_block *disk_super = fs_info->super_copy; in btrfs_setup_sprout()
2545 * - All device ops and balance - as we are in btrfs_exclop_start. in btrfs_setup_sprout()
2546 * - Various dev_list readers - are using RCU. in btrfs_setup_sprout()
2547 * - btrfs_ioctl_fitrim() - is using RCU. in btrfs_setup_sprout()
2549 * For-read threads as below are using device_list_mutex: in btrfs_setup_sprout()
2550 * - Readonly scrub btrfs_scrub_dev() in btrfs_setup_sprout()
2551 * - Readonly scrub btrfs_scrub_progress() in btrfs_setup_sprout()
2552 * - btrfs_get_dev_stats() in btrfs_setup_sprout()
2554 lockdep_assert_held(&fs_devices->device_list_mutex); in btrfs_setup_sprout()
2556 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices, in btrfs_setup_sprout()
2558 list_for_each_entry(device, &seed_devices->devices, dev_list) in btrfs_setup_sprout()
2559 device->fs_devices = seed_devices; in btrfs_setup_sprout()
2561 fs_devices->seeding = false; in btrfs_setup_sprout()
2562 fs_devices->num_devices = 0; in btrfs_setup_sprout()
2563 fs_devices->open_devices = 0; in btrfs_setup_sprout()
2564 fs_devices->missing_devices = 0; in btrfs_setup_sprout()
2565 fs_devices->rotating = false; in btrfs_setup_sprout()
2566 list_add(&seed_devices->seed_list, &fs_devices->seed_list); in btrfs_setup_sprout()
2568 generate_random_uuid(fs_devices->fsid); in btrfs_setup_sprout()
2569 memcpy(fs_devices->metadata_uuid, fs_devices->fsid, BTRFS_FSID_SIZE); in btrfs_setup_sprout()
2570 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); in btrfs_setup_sprout()
2583 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_finish_sprout()
2584 struct btrfs_root *root = fs_info->chunk_root; in btrfs_finish_sprout()
2585 struct btrfs_path *path; in btrfs_finish_sprout() local
2594 path = btrfs_alloc_path(); in btrfs_finish_sprout()
2595 if (!path) in btrfs_finish_sprout()
2596 return -ENOMEM; in btrfs_finish_sprout()
2604 ret = btrfs_search_slot(trans, root, &key, path, 0, 1); in btrfs_finish_sprout()
2609 leaf = path->nodes[0]; in btrfs_finish_sprout()
2611 if (path->slots[0] >= btrfs_header_nritems(leaf)) { in btrfs_finish_sprout()
2612 ret = btrfs_next_leaf(root, path); in btrfs_finish_sprout()
2617 leaf = path->nodes[0]; in btrfs_finish_sprout()
2618 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); in btrfs_finish_sprout()
2619 btrfs_release_path(path); in btrfs_finish_sprout()
2623 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); in btrfs_finish_sprout()
2628 dev_item = btrfs_item_ptr(leaf, path->slots[0], in btrfs_finish_sprout()
2637 device = btrfs_find_device(fs_info->fs_devices, &args); in btrfs_finish_sprout()
2640 if (device->fs_devices->seeding) { in btrfs_finish_sprout()
2642 device->generation); in btrfs_finish_sprout()
2646 path->slots[0]++; in btrfs_finish_sprout()
2651 btrfs_free_path(path); in btrfs_finish_sprout()
2657 struct btrfs_root *root = fs_info->dev_root; in btrfs_init_new_device()
2661 struct super_block *sb = fs_info->sb; in btrfs_init_new_device()
2662 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; in btrfs_init_new_device()
2670 if (sb_rdonly(sb) && !fs_devices->seeding) in btrfs_init_new_device()
2671 return -EROFS; in btrfs_init_new_device()
2674 fs_info->bdev_holder, NULL); in btrfs_init_new_device()
2679 ret = -EINVAL; in btrfs_init_new_device()
2683 if (fs_devices->seeding) { in btrfs_init_new_device()
2685 down_write(&sb->s_umount); in btrfs_init_new_device()
2693 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) { in btrfs_init_new_device()
2694 if (device->bdev == bdev) { in btrfs_init_new_device()
2695 ret = -EEXIST; in btrfs_init_new_device()
2709 device->fs_info = fs_info; in btrfs_init_new_device()
2710 device->bdev = bdev; in btrfs_init_new_device()
2711 ret = lookup_bdev(device_path, &device->devt); in btrfs_init_new_device()
2725 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); in btrfs_init_new_device()
2726 device->generation = trans->transid; in btrfs_init_new_device()
2727 device->io_width = fs_info->sectorsize; in btrfs_init_new_device()
2728 device->io_align = fs_info->sectorsize; in btrfs_init_new_device()
2729 device->sector_size = fs_info->sectorsize; in btrfs_init_new_device()
2730 device->total_bytes = in btrfs_init_new_device()
2731 round_down(bdev_nr_bytes(bdev), fs_info->sectorsize); in btrfs_init_new_device()
2732 device->disk_total_bytes = device->total_bytes; in btrfs_init_new_device()
2733 device->commit_total_bytes = device->total_bytes; in btrfs_init_new_device()
2734 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); in btrfs_init_new_device()
2735 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); in btrfs_init_new_device()
2736 device->holder = fs_info->bdev_holder; in btrfs_init_new_device()
2737 device->dev_stats_valid = 1; in btrfs_init_new_device()
2738 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE); in btrfs_init_new_device()
2750 mutex_lock(&fs_devices->device_list_mutex); in btrfs_init_new_device()
2753 btrfs_assign_next_active_device(fs_info->fs_devices->latest_dev, in btrfs_init_new_device()
2757 device->fs_devices = fs_devices; in btrfs_init_new_device()
2759 mutex_lock(&fs_info->chunk_mutex); in btrfs_init_new_device()
2760 list_add_rcu(&device->dev_list, &fs_devices->devices); in btrfs_init_new_device()
2761 list_add(&device->dev_alloc_list, &fs_devices->alloc_list); in btrfs_init_new_device()
2762 fs_devices->num_devices++; in btrfs_init_new_device()
2763 fs_devices->open_devices++; in btrfs_init_new_device()
2764 fs_devices->rw_devices++; in btrfs_init_new_device()
2765 fs_devices->total_devices++; in btrfs_init_new_device()
2766 fs_devices->total_rw_bytes += device->total_bytes; in btrfs_init_new_device()
2768 atomic64_add(device->total_bytes, &fs_info->free_chunk_space); in btrfs_init_new_device()
2771 fs_devices->rotating = true; in btrfs_init_new_device()
2773 orig_super_total_bytes = btrfs_super_total_bytes(fs_info->super_copy); in btrfs_init_new_device()
2774 btrfs_set_super_total_bytes(fs_info->super_copy, in btrfs_init_new_device()
2775 round_down(orig_super_total_bytes + device->total_bytes, in btrfs_init_new_device()
2776 fs_info->sectorsize)); in btrfs_init_new_device()
2778 orig_super_num_devices = btrfs_super_num_devices(fs_info->super_copy); in btrfs_init_new_device()
2779 btrfs_set_super_num_devices(fs_info->super_copy, in btrfs_init_new_device()
2788 mutex_unlock(&fs_info->chunk_mutex); in btrfs_init_new_device()
2793 mutex_unlock(&fs_devices->device_list_mutex); in btrfs_init_new_device()
2796 mutex_lock(&fs_info->chunk_mutex); in btrfs_init_new_device()
2798 mutex_unlock(&fs_info->chunk_mutex); in btrfs_init_new_device()
2829 up_write(&sb->s_umount); in btrfs_init_new_device()
2841 if (PTR_ERR(trans) == -ENOENT) in btrfs_init_new_device()
2854 * We can ignore the return value as it typically returns -EINVAL and in btrfs_init_new_device()
2857 btrfs_forget_devices(device->devt); in btrfs_init_new_device()
2866 mutex_lock(&fs_info->fs_devices->device_list_mutex); in btrfs_init_new_device()
2867 mutex_lock(&fs_info->chunk_mutex); in btrfs_init_new_device()
2868 list_del_rcu(&device->dev_list); in btrfs_init_new_device()
2869 list_del(&device->dev_alloc_list); in btrfs_init_new_device()
2870 fs_info->fs_devices->num_devices--; in btrfs_init_new_device()
2871 fs_info->fs_devices->open_devices--; in btrfs_init_new_device()
2872 fs_info->fs_devices->rw_devices--; in btrfs_init_new_device()
2873 fs_info->fs_devices->total_devices--; in btrfs_init_new_device()
2874 fs_info->fs_devices->total_rw_bytes -= device->total_bytes; in btrfs_init_new_device()
2875 atomic64_sub(device->total_bytes, &fs_info->free_chunk_space); in btrfs_init_new_device()
2876 btrfs_set_super_total_bytes(fs_info->super_copy, in btrfs_init_new_device()
2878 btrfs_set_super_num_devices(fs_info->super_copy, in btrfs_init_new_device()
2880 mutex_unlock(&fs_info->chunk_mutex); in btrfs_init_new_device()
2881 mutex_unlock(&fs_info->fs_devices->device_list_mutex); in btrfs_init_new_device()
2890 blkdev_put(bdev, fs_info->bdev_holder); in btrfs_init_new_device()
2893 up_write(&sb->s_umount); in btrfs_init_new_device()
2902 struct btrfs_path *path; in btrfs_update_device() local
2903 struct btrfs_root *root = device->fs_info->chunk_root; in btrfs_update_device()
2908 path = btrfs_alloc_path(); in btrfs_update_device()
2909 if (!path) in btrfs_update_device()
2910 return -ENOMEM; in btrfs_update_device()
2914 key.offset = device->devid; in btrfs_update_device()
2916 ret = btrfs_search_slot(trans, root, &key, path, 0, 1); in btrfs_update_device()
2921 ret = -ENOENT; in btrfs_update_device()
2925 leaf = path->nodes[0]; in btrfs_update_device()
2926 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); in btrfs_update_device()
2928 btrfs_set_device_id(leaf, dev_item, device->devid); in btrfs_update_device()
2929 btrfs_set_device_type(leaf, dev_item, device->type); in btrfs_update_device()
2930 btrfs_set_device_io_align(leaf, dev_item, device->io_align); in btrfs_update_device()
2931 btrfs_set_device_io_width(leaf, dev_item, device->io_width); in btrfs_update_device()
2932 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); in btrfs_update_device()
2940 btrfs_free_path(path); in btrfs_update_device()
2947 struct btrfs_fs_info *fs_info = device->fs_info; in btrfs_grow_device()
2948 struct btrfs_super_block *super_copy = fs_info->super_copy; in btrfs_grow_device()
2953 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) in btrfs_grow_device()
2954 return -EACCES; in btrfs_grow_device()
2956 new_size = round_down(new_size, fs_info->sectorsize); in btrfs_grow_device()
2958 mutex_lock(&fs_info->chunk_mutex); in btrfs_grow_device()
2960 diff = round_down(new_size - device->total_bytes, fs_info->sectorsize); in btrfs_grow_device()
2962 if (new_size <= device->total_bytes || in btrfs_grow_device()
2963 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { in btrfs_grow_device()
2964 mutex_unlock(&fs_info->chunk_mutex); in btrfs_grow_device()
2965 return -EINVAL; in btrfs_grow_device()
2969 round_down(old_total + diff, fs_info->sectorsize)); in btrfs_grow_device()
2970 device->fs_devices->total_rw_bytes += diff; in btrfs_grow_device()
2974 btrfs_clear_space_info_full(device->fs_info); in btrfs_grow_device()
2975 if (list_empty(&device->post_commit_list)) in btrfs_grow_device()
2976 list_add_tail(&device->post_commit_list, in btrfs_grow_device()
2977 &trans->transaction->dev_update_list); in btrfs_grow_device()
2978 mutex_unlock(&fs_info->chunk_mutex); in btrfs_grow_device()
2989 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_free_chunk()
2990 struct btrfs_root *root = fs_info->chunk_root; in btrfs_free_chunk()
2992 struct btrfs_path *path; in btrfs_free_chunk() local
2995 path = btrfs_alloc_path(); in btrfs_free_chunk()
2996 if (!path) in btrfs_free_chunk()
2997 return -ENOMEM; in btrfs_free_chunk()
3003 ret = btrfs_search_slot(trans, root, &key, path, -1, 1); in btrfs_free_chunk()
3007 btrfs_handle_fs_error(fs_info, -ENOENT, in btrfs_free_chunk()
3009 ret = -ENOENT; in btrfs_free_chunk()
3013 ret = btrfs_del_item(trans, root, path); in btrfs_free_chunk()
3018 btrfs_free_path(path); in btrfs_free_chunk()
3024 struct btrfs_super_block *super_copy = fs_info->super_copy; in btrfs_del_sys_chunk()
3035 lockdep_assert_held(&fs_info->chunk_mutex); in btrfs_del_sys_chunk()
3038 ptr = super_copy->sys_chunk_array; in btrfs_del_sys_chunk()
3052 ret = -EIO; in btrfs_del_sys_chunk()
3057 memmove(ptr, ptr + len, array_size - (cur + len)); in btrfs_del_sys_chunk()
3058 array_size -= len; in btrfs_del_sys_chunk()
3069 * btrfs_get_chunk_map() - Find the mapping containing the given logical extent.
3081 em_tree = &fs_info->mapping_tree; in btrfs_get_chunk_map()
3082 read_lock(&em_tree->lock); in btrfs_get_chunk_map()
3084 read_unlock(&em_tree->lock); in btrfs_get_chunk_map()
3088 "unable to find chunk map for logical %llu length %llu", in btrfs_get_chunk_map()
3090 return ERR_PTR(-EINVAL); in btrfs_get_chunk_map()
3093 if (em->start > logical || em->start + em->len <= logical) { in btrfs_get_chunk_map()
3095 "found a bad chunk map, wanted %llu-%llu, found %llu-%llu", in btrfs_get_chunk_map()
3096 logical, logical + length, em->start, em->start + em->len); in btrfs_get_chunk_map()
3098 return ERR_PTR(-EINVAL); in btrfs_get_chunk_map()
3106 struct map_lookup *map, u64 chunk_offset) in remove_chunk_item() argument
3115 lockdep_assert_held(&trans->fs_info->chunk_mutex); in remove_chunk_item()
3117 for (i = 0; i < map->num_stripes; i++) { in remove_chunk_item()
3120 ret = btrfs_update_device(trans, map->stripes[i].dev); in remove_chunk_item()
3130 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_remove_chunk()
3132 struct map_lookup *map; in btrfs_remove_chunk() local
3135 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; in btrfs_remove_chunk()
3147 map = em->map_lookup; in btrfs_remove_chunk()
3153 * fs_info->chunk_mutex. Note that here we do not acquire the chunk_mutex in btrfs_remove_chunk()
3155 * items from the devices btree - COWing an extent buffer from the btree in btrfs_remove_chunk()
3157 * lock again fs_info->chunk_mutex. in btrfs_remove_chunk()
3159 mutex_lock(&fs_devices->device_list_mutex); in btrfs_remove_chunk()
3160 for (i = 0; i < map->num_stripes; i++) { in btrfs_remove_chunk()
3161 struct btrfs_device *device = map->stripes[i].dev; in btrfs_remove_chunk()
3163 map->stripes[i].physical, in btrfs_remove_chunk()
3166 mutex_unlock(&fs_devices->device_list_mutex); in btrfs_remove_chunk()
3171 if (device->bytes_used > 0) { in btrfs_remove_chunk()
3172 mutex_lock(&fs_info->chunk_mutex); in btrfs_remove_chunk()
3174 device->bytes_used - dev_extent_len); in btrfs_remove_chunk()
3175 atomic64_add(dev_extent_len, &fs_info->free_chunk_space); in btrfs_remove_chunk()
3178 if (list_empty(&device->post_commit_list)) { in btrfs_remove_chunk()
3179 list_add_tail(&device->post_commit_list, in btrfs_remove_chunk()
3180 &trans->transaction->dev_update_list); in btrfs_remove_chunk()
3183 mutex_unlock(&fs_info->chunk_mutex); in btrfs_remove_chunk()
3186 mutex_unlock(&fs_devices->device_list_mutex); in btrfs_remove_chunk()
3189 * We acquire fs_info->chunk_mutex for 2 reasons: in btrfs_remove_chunk()
3198 * that replaces the device object associated with the map's stripes, in btrfs_remove_chunk()
3201 * (dev-replace.c:btrfs_dev_replace_finishing()), so we could grab the in btrfs_remove_chunk()
3209 trans->removing_chunk = true; in btrfs_remove_chunk()
3210 mutex_lock(&fs_info->chunk_mutex); in btrfs_remove_chunk()
3212 check_system_chunk(trans, map->type); in btrfs_remove_chunk()
3214 ret = remove_chunk_item(trans, map, chunk_offset); in btrfs_remove_chunk()
3216 * Normally we should not get -ENOSPC since we reserved space before in btrfs_remove_chunk()
3227 * So if we get -ENOSPC, allocate a new system chunk and retry once. in btrfs_remove_chunk()
3229 if (ret == -ENOSPC) { in btrfs_remove_chunk()
3246 ret = remove_chunk_item(trans, map, chunk_offset); in btrfs_remove_chunk()
3256 trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len); in btrfs_remove_chunk()
3258 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { in btrfs_remove_chunk()
3266 mutex_unlock(&fs_info->chunk_mutex); in btrfs_remove_chunk()
3267 trans->removing_chunk = false; in btrfs_remove_chunk()
3282 if (trans->removing_chunk) { in btrfs_remove_chunk()
3283 mutex_unlock(&fs_info->chunk_mutex); in btrfs_remove_chunk()
3284 trans->removing_chunk = false; in btrfs_remove_chunk()
3293 struct btrfs_root *root = fs_info->chunk_root; in btrfs_relocate_chunk()
3302 return -EINVAL; in btrfs_relocate_chunk()
3314 * we release the path used to search the chunk/dev tree and before in btrfs_relocate_chunk()
3317 lockdep_assert_held(&fs_info->reclaim_bgs_lock); in btrfs_relocate_chunk()
3335 return -ENOENT; in btrfs_relocate_chunk()
3336 btrfs_discard_cancel_work(&fs_info->discard_ctl, block_group); in btrfs_relocate_chunk()
3337 length = block_group->length; in btrfs_relocate_chunk()
3354 trans = btrfs_start_trans_remove_block_group(root->fs_info, in btrfs_relocate_chunk()
3358 btrfs_handle_fs_error(root->fs_info, ret, NULL); in btrfs_relocate_chunk()
3373 struct btrfs_root *chunk_root = fs_info->chunk_root; in btrfs_relocate_sys_chunks()
3374 struct btrfs_path *path; in btrfs_relocate_sys_chunks() local
3384 path = btrfs_alloc_path(); in btrfs_relocate_sys_chunks()
3385 if (!path) in btrfs_relocate_sys_chunks()
3386 return -ENOMEM; in btrfs_relocate_sys_chunks()
3390 key.offset = (u64)-1; in btrfs_relocate_sys_chunks()
3394 mutex_lock(&fs_info->reclaim_bgs_lock); in btrfs_relocate_sys_chunks()
3395 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); in btrfs_relocate_sys_chunks()
3397 mutex_unlock(&fs_info->reclaim_bgs_lock); in btrfs_relocate_sys_chunks()
3403 * offset -1, which is not possible. On subsequent in btrfs_relocate_sys_chunks()
3408 ret = -EUCLEAN; in btrfs_relocate_sys_chunks()
3409 mutex_unlock(&fs_info->reclaim_bgs_lock); in btrfs_relocate_sys_chunks()
3413 ret = btrfs_previous_item(chunk_root, path, key.objectid, in btrfs_relocate_sys_chunks()
3416 mutex_unlock(&fs_info->reclaim_bgs_lock); in btrfs_relocate_sys_chunks()
3422 leaf = path->nodes[0]; in btrfs_relocate_sys_chunks()
3423 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); in btrfs_relocate_sys_chunks()
3425 chunk = btrfs_item_ptr(leaf, path->slots[0], in btrfs_relocate_sys_chunks()
3428 btrfs_release_path(path); in btrfs_relocate_sys_chunks()
3432 if (ret == -ENOSPC) in btrfs_relocate_sys_chunks()
3437 mutex_unlock(&fs_info->reclaim_bgs_lock); in btrfs_relocate_sys_chunks()
3441 key.offset = found_key.offset - 1; in btrfs_relocate_sys_chunks()
3449 ret = -ENOSPC; in btrfs_relocate_sys_chunks()
3452 btrfs_free_path(path); in btrfs_relocate_sys_chunks()
3470 chunk_type = cache->flags; in btrfs_may_alloc_data_chunk()
3476 spin_lock(&fs_info->data_sinfo->lock); in btrfs_may_alloc_data_chunk()
3477 bytes_used = fs_info->data_sinfo->bytes_used; in btrfs_may_alloc_data_chunk()
3478 spin_unlock(&fs_info->data_sinfo->lock); in btrfs_may_alloc_data_chunk()
3484 trans = btrfs_join_transaction(fs_info->tree_root); in btrfs_may_alloc_data_chunk()
3501 struct btrfs_root *root = fs_info->tree_root; in insert_balance_item()
3505 struct btrfs_path *path; in insert_balance_item() local
3510 path = btrfs_alloc_path(); in insert_balance_item()
3511 if (!path) in insert_balance_item()
3512 return -ENOMEM; in insert_balance_item()
3516 btrfs_free_path(path); in insert_balance_item()
3524 ret = btrfs_insert_empty_item(trans, root, path, &key, in insert_balance_item()
3529 leaf = path->nodes[0]; in insert_balance_item()
3530 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); in insert_balance_item()
3534 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data); in insert_balance_item()
3536 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta); in insert_balance_item()
3538 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys); in insert_balance_item()
3541 btrfs_set_balance_flags(leaf, item, bctl->flags); in insert_balance_item()
3545 btrfs_free_path(path); in insert_balance_item()
3554 struct btrfs_root *root = fs_info->tree_root; in del_balance_item()
3556 struct btrfs_path *path; in del_balance_item() local
3560 path = btrfs_alloc_path(); in del_balance_item()
3561 if (!path) in del_balance_item()
3562 return -ENOMEM; in del_balance_item()
3566 btrfs_free_path(path); in del_balance_item()
3574 ret = btrfs_search_slot(trans, root, &key, path, -1, 1); in del_balance_item()
3578 ret = -ENOENT; in del_balance_item()
3582 ret = btrfs_del_item(trans, root, path); in del_balance_item()
3584 btrfs_free_path(path); in del_balance_item()
3600 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) in update_balance_args()
3601 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT; in update_balance_args()
3602 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) in update_balance_args()
3603 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT; in update_balance_args()
3604 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) in update_balance_args()
3605 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT; in update_balance_args()
3611 * converted - that will keep us from relocating unconverted in update_balance_args()
3614 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) && in update_balance_args()
3615 !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && in update_balance_args()
3616 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) { in update_balance_args()
3617 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE; in update_balance_args()
3618 bctl->data.usage = 90; in update_balance_args()
3620 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) && in update_balance_args()
3621 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && in update_balance_args()
3622 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) { in update_balance_args()
3623 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE; in update_balance_args()
3624 bctl->sys.usage = 90; in update_balance_args()
3626 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) && in update_balance_args()
3627 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && in update_balance_args()
3628 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) { in update_balance_args()
3629 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE; in update_balance_args()
3630 bctl->meta.usage = 90; in update_balance_args()
3639 struct btrfs_balance_control *bctl = fs_info->balance_ctl; in reset_balance_state()
3642 BUG_ON(!fs_info->balance_ctl); in reset_balance_state()
3644 spin_lock(&fs_info->balance_lock); in reset_balance_state()
3645 fs_info->balance_ctl = NULL; in reset_balance_state()
3646 spin_unlock(&fs_info->balance_lock); in reset_balance_state()
3664 if (bargs->profiles & chunk_type) in chunk_profiles_filter()
3680 chunk_used = cache->used; in chunk_usage_range_filter()
3682 if (bargs->usage_min == 0) in chunk_usage_range_filter()
3685 user_thresh_min = mult_perc(cache->length, bargs->usage_min); in chunk_usage_range_filter()
3687 if (bargs->usage_max == 0) in chunk_usage_range_filter()
3689 else if (bargs->usage_max > 100) in chunk_usage_range_filter()
3690 user_thresh_max = cache->length; in chunk_usage_range_filter()
3692 user_thresh_max = mult_perc(cache->length, bargs->usage_max); in chunk_usage_range_filter()
3709 chunk_used = cache->used; in chunk_usage_filter()
3711 if (bargs->usage_min == 0) in chunk_usage_filter()
3713 else if (bargs->usage > 100) in chunk_usage_filter()
3714 user_thresh = cache->length; in chunk_usage_filter()
3716 user_thresh = mult_perc(cache->length, bargs->usage); in chunk_usage_filter()
3735 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid) in chunk_devid_filter()
3748 return (num_stripes - nparity) / ncopies; in calc_data_stripes()
3764 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID)) in chunk_drange_filter()
3772 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid) in chunk_drange_filter()
3779 if (stripe_offset < bargs->pend && in chunk_drange_filter()
3780 stripe_offset + stripe_length > bargs->pstart) in chunk_drange_filter()
3793 if (chunk_offset < bargs->vend && in chunk_vrange_filter()
3794 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart) in chunk_vrange_filter()
3807 if (bargs->stripes_min <= num_stripes in chunk_stripes_range_filter()
3808 && num_stripes <= bargs->stripes_max) in chunk_stripes_range_filter()
3817 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT)) in chunk_soft_convert_filter()
3823 if (bargs->target == chunk_type) in chunk_soft_convert_filter()
3832 struct btrfs_fs_info *fs_info = leaf->fs_info; in should_balance_chunk()
3833 struct btrfs_balance_control *bctl = fs_info->balance_ctl; in should_balance_chunk()
3839 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) { in should_balance_chunk()
3844 bargs = &bctl->data; in should_balance_chunk()
3846 bargs = &bctl->sys; in should_balance_chunk()
3848 bargs = &bctl->meta; in should_balance_chunk()
3851 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) && in should_balance_chunk()
3857 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) && in should_balance_chunk()
3860 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && in should_balance_chunk()
3866 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) && in should_balance_chunk()
3872 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) && in should_balance_chunk()
3878 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) && in should_balance_chunk()
3884 if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) && in should_balance_chunk()
3890 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) && in should_balance_chunk()
3898 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) { in should_balance_chunk()
3899 if (bargs->limit == 0) in should_balance_chunk()
3902 bargs->limit--; in should_balance_chunk()
3903 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) { in should_balance_chunk()
3909 if (bargs->limit_max == 0) in should_balance_chunk()
3912 bargs->limit_max--; in should_balance_chunk()
3920 struct btrfs_balance_control *bctl = fs_info->balance_ctl; in __btrfs_balance()
3921 struct btrfs_root *chunk_root = fs_info->chunk_root; in __btrfs_balance()
3924 struct btrfs_path *path = NULL; in __btrfs_balance() local
3933 u64 limit_data = bctl->data.limit; in __btrfs_balance()
3934 u64 limit_meta = bctl->meta.limit; in __btrfs_balance()
3935 u64 limit_sys = bctl->sys.limit; in __btrfs_balance()
3941 path = btrfs_alloc_path(); in __btrfs_balance()
3942 if (!path) { in __btrfs_balance()
3943 ret = -ENOMEM; in __btrfs_balance()
3948 spin_lock(&fs_info->balance_lock); in __btrfs_balance()
3949 memset(&bctl->stat, 0, sizeof(bctl->stat)); in __btrfs_balance()
3950 spin_unlock(&fs_info->balance_lock); in __btrfs_balance()
3957 bctl->data.limit = limit_data; in __btrfs_balance()
3958 bctl->meta.limit = limit_meta; in __btrfs_balance()
3959 bctl->sys.limit = limit_sys; in __btrfs_balance()
3962 key.offset = (u64)-1; in __btrfs_balance()
3966 if ((!counting && atomic_read(&fs_info->balance_pause_req)) || in __btrfs_balance()
3967 atomic_read(&fs_info->balance_cancel_req)) { in __btrfs_balance()
3968 ret = -ECANCELED; in __btrfs_balance()
3972 mutex_lock(&fs_info->reclaim_bgs_lock); in __btrfs_balance()
3973 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); in __btrfs_balance()
3975 mutex_unlock(&fs_info->reclaim_bgs_lock); in __btrfs_balance()
3986 ret = btrfs_previous_item(chunk_root, path, 0, in __btrfs_balance()
3989 mutex_unlock(&fs_info->reclaim_bgs_lock); in __btrfs_balance()
3994 leaf = path->nodes[0]; in __btrfs_balance()
3995 slot = path->slots[0]; in __btrfs_balance()
3999 mutex_unlock(&fs_info->reclaim_bgs_lock); in __btrfs_balance()
4007 spin_lock(&fs_info->balance_lock); in __btrfs_balance()
4008 bctl->stat.considered++; in __btrfs_balance()
4009 spin_unlock(&fs_info->balance_lock); in __btrfs_balance()
4014 btrfs_release_path(path); in __btrfs_balance()
4016 mutex_unlock(&fs_info->reclaim_bgs_lock); in __btrfs_balance()
4021 mutex_unlock(&fs_info->reclaim_bgs_lock); in __btrfs_balance()
4022 spin_lock(&fs_info->balance_lock); in __btrfs_balance()
4023 bctl->stat.expected++; in __btrfs_balance()
4024 spin_unlock(&fs_info->balance_lock); in __btrfs_balance()
4041 count_data < bctl->data.limit_min) in __btrfs_balance()
4043 count_meta < bctl->meta.limit_min) in __btrfs_balance()
4045 count_sys < bctl->sys.limit_min)) { in __btrfs_balance()
4046 mutex_unlock(&fs_info->reclaim_bgs_lock); in __btrfs_balance()
4060 mutex_unlock(&fs_info->reclaim_bgs_lock); in __btrfs_balance()
4068 mutex_unlock(&fs_info->reclaim_bgs_lock); in __btrfs_balance()
4069 if (ret == -ENOSPC) { in __btrfs_balance()
4071 } else if (ret == -ETXTBSY) { in __btrfs_balance()
4079 spin_lock(&fs_info->balance_lock); in __btrfs_balance()
4080 bctl->stat.completed++; in __btrfs_balance()
4081 spin_unlock(&fs_info->balance_lock); in __btrfs_balance()
4086 key.offset = found_key.offset - 1; in __btrfs_balance()
4090 btrfs_release_path(path); in __btrfs_balance()
4095 btrfs_free_path(path); in __btrfs_balance()
4100 ret = -ENOSPC; in __btrfs_balance()
4138 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT)) in validate_convert_profile()
4142 if (alloc_profile_is_valid(bargs->target, 1) && in validate_convert_profile()
4143 (bargs->target & ~allowed) == 0) in validate_convert_profile()
4147 type, btrfs_bg_type_to_raid_name(bargs->target)); in validate_convert_profile()
4162 u64 flags = bargs->flags; in describe_balance_args()
4173 size_bp -= ret; \ in describe_balance_args()
4182 size_bp -= ret; \ in describe_balance_args()
4191 size_bp -= ret; \ in describe_balance_args()
4197 btrfs_bg_type_to_raid_name(bargs->target)); in describe_balance_args()
4203 btrfs_describe_block_groups(bargs->profiles, tmp_buf, in describe_balance_args()
4209 CHECK_APPEND_1ARG("usage=%llu,", bargs->usage); in describe_balance_args()
4213 bargs->usage_min, bargs->usage_max); in describe_balance_args()
4216 CHECK_APPEND_1ARG("devid=%llu,", bargs->devid); in describe_balance_args()
4220 bargs->pstart, bargs->pend); in describe_balance_args()
4224 bargs->vstart, bargs->vend); in describe_balance_args()
4227 CHECK_APPEND_1ARG("limit=%llu,", bargs->limit); in describe_balance_args()
4231 bargs->limit_min, bargs->limit_max); in describe_balance_args()
4235 bargs->stripes_min, bargs->stripes_max); in describe_balance_args()
4244 buf[size_buf - size_bp - 1] = '\0'; /* remove last , */ in describe_balance_args()
4257 struct btrfs_balance_control *bctl = fs_info->balance_ctl; in describe_balance_start_or_resume()
4270 size_bp -= ret; \ in describe_balance_start_or_resume()
4274 if (bctl->flags & BTRFS_BALANCE_FORCE) in describe_balance_start_or_resume()
4275 CHECK_APPEND_1ARG("%s", "-f "); in describe_balance_start_or_resume()
4277 if (bctl->flags & BTRFS_BALANCE_DATA) { in describe_balance_start_or_resume()
4278 describe_balance_args(&bctl->data, tmp_buf, sizeof(tmp_buf)); in describe_balance_start_or_resume()
4279 CHECK_APPEND_1ARG("-d%s ", tmp_buf); in describe_balance_start_or_resume()
4282 if (bctl->flags & BTRFS_BALANCE_METADATA) { in describe_balance_start_or_resume()
4283 describe_balance_args(&bctl->meta, tmp_buf, sizeof(tmp_buf)); in describe_balance_start_or_resume()
4284 CHECK_APPEND_1ARG("-m%s ", tmp_buf); in describe_balance_start_or_resume()
4287 if (bctl->flags & BTRFS_BALANCE_SYSTEM) { in describe_balance_start_or_resume()
4288 describe_balance_args(&bctl->sys, tmp_buf, sizeof(tmp_buf)); in describe_balance_start_or_resume()
4289 CHECK_APPEND_1ARG("-s%s ", tmp_buf); in describe_balance_start_or_resume()
4297 buf[size_buf - size_bp - 1] = '\0'; /* remove last " " */ in describe_balance_start_or_resume()
4299 (bctl->flags & BTRFS_BALANCE_RESUME) ? in describe_balance_start_or_resume()
4323 atomic_read(&fs_info->balance_pause_req) || in btrfs_balance()
4325 ret = -EINVAL; in btrfs_balance()
4329 allowed = btrfs_super_incompat_flags(fs_info->super_copy); in btrfs_balance()
4338 if (mixed && (bctl->flags & allowed)) { in btrfs_balance()
4339 if (!(bctl->flags & BTRFS_BALANCE_DATA) || in btrfs_balance()
4340 !(bctl->flags & BTRFS_BALANCE_METADATA) || in btrfs_balance()
4341 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) { in btrfs_balance()
4344 ret = -EINVAL; in btrfs_balance()
4353 num_devices = fs_info->fs_devices->rw_devices; in btrfs_balance()
4356 * SINGLE profile on-disk has no profile bit, but in-memory we have a in btrfs_balance()
4365 if (!validate_convert_profile(fs_info, &bctl->data, allowed, "data") || in btrfs_balance()
4366 !validate_convert_profile(fs_info, &bctl->meta, allowed, "metadata") || in btrfs_balance()
4367 !validate_convert_profile(fs_info, &bctl->sys, allowed, "system")) { in btrfs_balance()
4368 ret = -EINVAL; in btrfs_balance()
4383 seq = read_seqbegin(&fs_info->profiles_lock); in btrfs_balance()
4385 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && in btrfs_balance()
4386 (fs_info->avail_system_alloc_bits & allowed) && in btrfs_balance()
4387 !(bctl->sys.target & allowed)) || in btrfs_balance()
4388 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && in btrfs_balance()
4389 (fs_info->avail_metadata_alloc_bits & allowed) && in btrfs_balance()
4390 !(bctl->meta.target & allowed))) in btrfs_balance()
4396 meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ? in btrfs_balance()
4397 bctl->meta.target : fs_info->avail_metadata_alloc_bits; in btrfs_balance()
4398 data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ? in btrfs_balance()
4399 bctl->data.target : fs_info->avail_data_alloc_bits; in btrfs_balance()
4400 } while (read_seqretry(&fs_info->profiles_lock, seq)); in btrfs_balance()
4403 if (bctl->flags & BTRFS_BALANCE_FORCE) { in btrfs_balance()
4408 "balance: reduces metadata redundancy, use --force if you want this"); in btrfs_balance()
4409 ret = -EINVAL; in btrfs_balance()
4423 if (ret && ret != -EEXIST) in btrfs_balance()
4426 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) { in btrfs_balance()
4427 BUG_ON(ret == -EEXIST); in btrfs_balance()
4428 BUG_ON(fs_info->balance_ctl); in btrfs_balance()
4429 spin_lock(&fs_info->balance_lock); in btrfs_balance()
4430 fs_info->balance_ctl = bctl; in btrfs_balance()
4431 spin_unlock(&fs_info->balance_lock); in btrfs_balance()
4433 BUG_ON(ret != -EEXIST); in btrfs_balance()
4434 spin_lock(&fs_info->balance_lock); in btrfs_balance()
4436 spin_unlock(&fs_info->balance_lock); in btrfs_balance()
4439 ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); in btrfs_balance()
4440 set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags); in btrfs_balance()
4442 mutex_unlock(&fs_info->balance_mutex); in btrfs_balance()
4446 mutex_lock(&fs_info->balance_mutex); in btrfs_balance()
4447 if (ret == -ECANCELED && atomic_read(&fs_info->balance_pause_req)) { in btrfs_balance()
4455 * - Regular cancel request in btrfs_balance()
4456 * Then ret == -ECANCELED and balance_cancel_req > 0 in btrfs_balance()
4458 * - Fatal signal to "btrfs" process in btrfs_balance()
4460 * got -EINTR, or caught by btrfs_should_cancel_balance() and in btrfs_balance()
4461 * got -ECANCELED. in btrfs_balance()
4463 * ret == -EINTR or ret == -ECANCELED. in btrfs_balance()
4467 else if (ret == -ECANCELED || ret == -EINTR) in btrfs_balance()
4472 clear_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags); in btrfs_balance()
4485 wake_up(&fs_info->balance_wait_q); in btrfs_balance()
4489 if (bctl->flags & BTRFS_BALANCE_RESUME) in btrfs_balance()
4503 sb_start_write(fs_info->sb); in balance_kthread()
4504 mutex_lock(&fs_info->balance_mutex); in balance_kthread()
4505 if (fs_info->balance_ctl) in balance_kthread()
4506 ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL); in balance_kthread()
4507 mutex_unlock(&fs_info->balance_mutex); in balance_kthread()
4508 sb_end_write(fs_info->sb); in balance_kthread()
4517 mutex_lock(&fs_info->balance_mutex); in btrfs_resume_balance_async()
4518 if (!fs_info->balance_ctl) { in btrfs_resume_balance_async()
4519 mutex_unlock(&fs_info->balance_mutex); in btrfs_resume_balance_async()
4522 mutex_unlock(&fs_info->balance_mutex); in btrfs_resume_balance_async()
4529 spin_lock(&fs_info->super_lock); in btrfs_resume_balance_async()
4530 ASSERT(fs_info->exclusive_operation == BTRFS_EXCLOP_BALANCE_PAUSED); in btrfs_resume_balance_async()
4531 fs_info->exclusive_operation = BTRFS_EXCLOP_BALANCE; in btrfs_resume_balance_async()
4532 spin_unlock(&fs_info->super_lock); in btrfs_resume_balance_async()
4534 * A ro->rw remount sequence should continue with the paused balance in btrfs_resume_balance_async()
4538 spin_lock(&fs_info->balance_lock); in btrfs_resume_balance_async()
4539 fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME; in btrfs_resume_balance_async()
4540 spin_unlock(&fs_info->balance_lock); in btrfs_resume_balance_async()
4542 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance"); in btrfs_resume_balance_async()
4551 struct btrfs_path *path; in btrfs_recover_balance() local
4556 path = btrfs_alloc_path(); in btrfs_recover_balance()
4557 if (!path) in btrfs_recover_balance()
4558 return -ENOMEM; in btrfs_recover_balance()
4564 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); in btrfs_recover_balance()
4567 if (ret > 0) { /* ret = -ENOENT; */ in btrfs_recover_balance()
4574 ret = -ENOMEM; in btrfs_recover_balance()
4578 leaf = path->nodes[0]; in btrfs_recover_balance()
4579 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); in btrfs_recover_balance()
4581 bctl->flags = btrfs_balance_flags(leaf, item); in btrfs_recover_balance()
4582 bctl->flags |= BTRFS_BALANCE_RESUME; in btrfs_recover_balance()
4585 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs); in btrfs_recover_balance()
4587 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs); in btrfs_recover_balance()
4589 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs); in btrfs_recover_balance()
4605 btrfs_release_path(path); in btrfs_recover_balance()
4607 mutex_lock(&fs_info->balance_mutex); in btrfs_recover_balance()
4608 BUG_ON(fs_info->balance_ctl); in btrfs_recover_balance()
4609 spin_lock(&fs_info->balance_lock); in btrfs_recover_balance()
4610 fs_info->balance_ctl = bctl; in btrfs_recover_balance()
4611 spin_unlock(&fs_info->balance_lock); in btrfs_recover_balance()
4612 mutex_unlock(&fs_info->balance_mutex); in btrfs_recover_balance()
4614 btrfs_free_path(path); in btrfs_recover_balance()
4622 mutex_lock(&fs_info->balance_mutex); in btrfs_pause_balance()
4623 if (!fs_info->balance_ctl) { in btrfs_pause_balance()
4624 mutex_unlock(&fs_info->balance_mutex); in btrfs_pause_balance()
4625 return -ENOTCONN; in btrfs_pause_balance()
4628 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { in btrfs_pause_balance()
4629 atomic_inc(&fs_info->balance_pause_req); in btrfs_pause_balance()
4630 mutex_unlock(&fs_info->balance_mutex); in btrfs_pause_balance()
4632 wait_event(fs_info->balance_wait_q, in btrfs_pause_balance()
4633 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); in btrfs_pause_balance()
4635 mutex_lock(&fs_info->balance_mutex); in btrfs_pause_balance()
4637 BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); in btrfs_pause_balance()
4638 atomic_dec(&fs_info->balance_pause_req); in btrfs_pause_balance()
4640 ret = -ENOTCONN; in btrfs_pause_balance()
4643 mutex_unlock(&fs_info->balance_mutex); in btrfs_pause_balance()
4649 mutex_lock(&fs_info->balance_mutex); in btrfs_cancel_balance()
4650 if (!fs_info->balance_ctl) { in btrfs_cancel_balance()
4651 mutex_unlock(&fs_info->balance_mutex); in btrfs_cancel_balance()
4652 return -ENOTCONN; in btrfs_cancel_balance()
4657 * mount time if the mount is read-write. Otherwise it's still paused in btrfs_cancel_balance()
4660 if (sb_rdonly(fs_info->sb)) { in btrfs_cancel_balance()
4661 mutex_unlock(&fs_info->balance_mutex); in btrfs_cancel_balance()
4662 return -EROFS; in btrfs_cancel_balance()
4665 atomic_inc(&fs_info->balance_cancel_req); in btrfs_cancel_balance()
4670 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { in btrfs_cancel_balance()
4671 mutex_unlock(&fs_info->balance_mutex); in btrfs_cancel_balance()
4672 wait_event(fs_info->balance_wait_q, in btrfs_cancel_balance()
4673 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); in btrfs_cancel_balance()
4674 mutex_lock(&fs_info->balance_mutex); in btrfs_cancel_balance()
4676 mutex_unlock(&fs_info->balance_mutex); in btrfs_cancel_balance()
4681 mutex_lock(&fs_info->balance_mutex); in btrfs_cancel_balance()
4683 if (fs_info->balance_ctl) { in btrfs_cancel_balance()
4690 ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); in btrfs_cancel_balance()
4691 atomic_dec(&fs_info->balance_cancel_req); in btrfs_cancel_balance()
4692 mutex_unlock(&fs_info->balance_mutex); in btrfs_cancel_balance()
4699 struct btrfs_root *root = fs_info->tree_root; in btrfs_uuid_scan_kthread()
4701 struct btrfs_path *path = NULL; in btrfs_uuid_scan_kthread() local
4710 path = btrfs_alloc_path(); in btrfs_uuid_scan_kthread()
4711 if (!path) { in btrfs_uuid_scan_kthread()
4712 ret = -ENOMEM; in btrfs_uuid_scan_kthread()
4725 ret = btrfs_search_forward(root, &key, path, in btrfs_uuid_scan_kthread()
4739 eb = path->nodes[0]; in btrfs_uuid_scan_kthread()
4740 slot = path->slots[0]; in btrfs_uuid_scan_kthread()
4756 btrfs_release_path(path); in btrfs_uuid_scan_kthread()
4758 * 1 - subvol uuid item in btrfs_uuid_scan_kthread()
4759 * 1 - received_subvol uuid item in btrfs_uuid_scan_kthread()
4761 trans = btrfs_start_transaction(fs_info->uuid_root, 2); in btrfs_uuid_scan_kthread()
4771 btrfs_release_path(path); in btrfs_uuid_scan_kthread()
4796 btrfs_release_path(path); in btrfs_uuid_scan_kthread()
4804 if (key.offset < (u64)-1) { in btrfs_uuid_scan_kthread()
4809 } else if (key.objectid < (u64)-1) { in btrfs_uuid_scan_kthread()
4820 btrfs_free_path(path); in btrfs_uuid_scan_kthread()
4826 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); in btrfs_uuid_scan_kthread()
4827 up(&fs_info->uuid_tree_rescan_sem); in btrfs_uuid_scan_kthread()
4834 struct btrfs_root *tree_root = fs_info->tree_root; in btrfs_create_uuid_tree()
4840 * 1 - root node in btrfs_create_uuid_tree()
4841 * 1 - root item in btrfs_create_uuid_tree()
4855 fs_info->uuid_root = uuid_root; in btrfs_create_uuid_tree()
4861 down(&fs_info->uuid_tree_rescan_sem); in btrfs_create_uuid_tree()
4862 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid"); in btrfs_create_uuid_tree()
4864 /* fs_info->update_uuid_tree_gen remains 0 in all error case */ in btrfs_create_uuid_tree()
4866 up(&fs_info->uuid_tree_rescan_sem); in btrfs_create_uuid_tree()
4880 struct btrfs_fs_info *fs_info = device->fs_info; in btrfs_shrink_device()
4881 struct btrfs_root *root = fs_info->dev_root; in btrfs_shrink_device()
4884 struct btrfs_path *path; in btrfs_shrink_device() local
4893 struct btrfs_super_block *super_copy = fs_info->super_copy; in btrfs_shrink_device()
4899 new_size = round_down(new_size, fs_info->sectorsize); in btrfs_shrink_device()
4901 diff = round_down(old_size - new_size, fs_info->sectorsize); in btrfs_shrink_device()
4903 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) in btrfs_shrink_device()
4904 return -EINVAL; in btrfs_shrink_device()
4906 path = btrfs_alloc_path(); in btrfs_shrink_device()
4907 if (!path) in btrfs_shrink_device()
4908 return -ENOMEM; in btrfs_shrink_device()
4910 path->reada = READA_BACK; in btrfs_shrink_device()
4914 btrfs_free_path(path); in btrfs_shrink_device()
4918 mutex_lock(&fs_info->chunk_mutex); in btrfs_shrink_device()
4921 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { in btrfs_shrink_device()
4922 device->fs_devices->total_rw_bytes -= diff; in btrfs_shrink_device()
4923 atomic64_sub(diff, &fs_info->free_chunk_space); in btrfs_shrink_device()
4928 * in-memory chunks are synced to disk so that the loop below sees them in btrfs_shrink_device()
4932 mutex_unlock(&fs_info->chunk_mutex); in btrfs_shrink_device()
4937 mutex_unlock(&fs_info->chunk_mutex); in btrfs_shrink_device()
4942 key.objectid = device->devid; in btrfs_shrink_device()
4943 key.offset = (u64)-1; in btrfs_shrink_device()
4947 mutex_lock(&fs_info->reclaim_bgs_lock); in btrfs_shrink_device()
4948 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); in btrfs_shrink_device()
4950 mutex_unlock(&fs_info->reclaim_bgs_lock); in btrfs_shrink_device()
4954 ret = btrfs_previous_item(root, path, 0, key.type); in btrfs_shrink_device()
4956 mutex_unlock(&fs_info->reclaim_bgs_lock); in btrfs_shrink_device()
4960 btrfs_release_path(path); in btrfs_shrink_device()
4964 l = path->nodes[0]; in btrfs_shrink_device()
4965 slot = path->slots[0]; in btrfs_shrink_device()
4966 btrfs_item_key_to_cpu(l, &key, path->slots[0]); in btrfs_shrink_device()
4968 if (key.objectid != device->devid) { in btrfs_shrink_device()
4969 mutex_unlock(&fs_info->reclaim_bgs_lock); in btrfs_shrink_device()
4970 btrfs_release_path(path); in btrfs_shrink_device()
4978 mutex_unlock(&fs_info->reclaim_bgs_lock); in btrfs_shrink_device()
4979 btrfs_release_path(path); in btrfs_shrink_device()
4984 btrfs_release_path(path); in btrfs_shrink_device()
4994 mutex_unlock(&fs_info->reclaim_bgs_lock); in btrfs_shrink_device()
4999 mutex_unlock(&fs_info->reclaim_bgs_lock); in btrfs_shrink_device()
5000 if (ret == -ENOSPC) { in btrfs_shrink_device()
5003 if (ret == -ETXTBSY) { in btrfs_shrink_device()
5010 } while (key.offset-- > 0); in btrfs_shrink_device()
5017 ret = -ENOSPC; in btrfs_shrink_device()
5028 mutex_lock(&fs_info->chunk_mutex); in btrfs_shrink_device()
5030 clear_extent_bits(&device->alloc_state, new_size, (u64)-1, in btrfs_shrink_device()
5034 if (list_empty(&device->post_commit_list)) in btrfs_shrink_device()
5035 list_add_tail(&device->post_commit_list, in btrfs_shrink_device()
5036 &trans->transaction->dev_update_list); in btrfs_shrink_device()
5040 round_down(old_total - diff, fs_info->sectorsize)); in btrfs_shrink_device()
5041 mutex_unlock(&fs_info->chunk_mutex); in btrfs_shrink_device()
5044 /* Now btrfs_update_device() will change the on-disk size. */ in btrfs_shrink_device()
5054 btrfs_free_path(path); in btrfs_shrink_device()
5056 mutex_lock(&fs_info->chunk_mutex); in btrfs_shrink_device()
5058 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) in btrfs_shrink_device()
5059 device->fs_devices->total_rw_bytes += diff; in btrfs_shrink_device()
5060 atomic64_add(diff, &fs_info->free_chunk_space); in btrfs_shrink_device()
5061 mutex_unlock(&fs_info->chunk_mutex); in btrfs_shrink_device()
5070 struct btrfs_super_block *super_copy = fs_info->super_copy; in btrfs_add_system_chunk()
5075 lockdep_assert_held(&fs_info->chunk_mutex); in btrfs_add_system_chunk()
5080 return -EFBIG; in btrfs_add_system_chunk()
5082 ptr = super_copy->sys_chunk_array + array_size; in btrfs_add_system_chunk()
5101 if (di_a->max_avail > di_b->max_avail) in btrfs_cmp_device_info()
5102 return -1; in btrfs_cmp_device_info()
5103 if (di_a->max_avail < di_b->max_avail) in btrfs_cmp_device_info()
5105 if (di_a->total_avail > di_b->total_avail) in btrfs_cmp_device_info()
5106 return -1; in btrfs_cmp_device_info()
5107 if (di_a->total_avail < di_b->total_avail) in btrfs_cmp_device_info()
5137 /* sub_stripes info for map */
5165 space_info = btrfs_find_space_info(fs_devices->fs_info, ctl->type); in init_alloc_chunk_ctl_policy_regular()
5168 ctl->max_chunk_size = READ_ONCE(space_info->chunk_size); in init_alloc_chunk_ctl_policy_regular()
5169 ctl->max_stripe_size = min_t(u64, ctl->max_chunk_size, SZ_1G); in init_alloc_chunk_ctl_policy_regular()
5171 if (ctl->type & BTRFS_BLOCK_GROUP_SYSTEM) in init_alloc_chunk_ctl_policy_regular()
5172 ctl->devs_max = min_t(int, ctl->devs_max, BTRFS_MAX_DEVS_SYS_CHUNK); in init_alloc_chunk_ctl_policy_regular()
5175 ctl->max_chunk_size = min(mult_perc(fs_devices->total_rw_bytes, 10), in init_alloc_chunk_ctl_policy_regular()
5176 ctl->max_chunk_size); in init_alloc_chunk_ctl_policy_regular()
5177 ctl->dev_extent_min = btrfs_stripe_nr_to_offset(ctl->dev_stripes); in init_alloc_chunk_ctl_policy_regular()
5184 u64 zone_size = fs_devices->fs_info->zone_size; in init_alloc_chunk_ctl_policy_zoned()
5186 int min_num_stripes = ctl->devs_min * ctl->dev_stripes; in init_alloc_chunk_ctl_policy_zoned()
5187 int min_data_stripes = (min_num_stripes - ctl->nparity) / ctl->ncopies; in init_alloc_chunk_ctl_policy_zoned()
5189 u64 type = ctl->type; in init_alloc_chunk_ctl_policy_zoned()
5191 ctl->max_stripe_size = zone_size; in init_alloc_chunk_ctl_policy_zoned()
5193 ctl->max_chunk_size = round_down(BTRFS_MAX_DATA_CHUNK_SIZE, in init_alloc_chunk_ctl_policy_zoned()
5196 ctl->max_chunk_size = ctl->max_stripe_size; in init_alloc_chunk_ctl_policy_zoned()
5198 ctl->max_chunk_size = 2 * ctl->max_stripe_size; in init_alloc_chunk_ctl_policy_zoned()
5199 ctl->devs_max = min_t(int, ctl->devs_max, in init_alloc_chunk_ctl_policy_zoned()
5206 limit = max(round_down(mult_perc(fs_devices->total_rw_bytes, 10), in init_alloc_chunk_ctl_policy_zoned()
5209 ctl->max_chunk_size = min(limit, ctl->max_chunk_size); in init_alloc_chunk_ctl_policy_zoned()
5210 ctl->dev_extent_min = zone_size * ctl->dev_stripes; in init_alloc_chunk_ctl_policy_zoned()
5216 int index = btrfs_bg_flags_to_raid_index(ctl->type); in init_alloc_chunk_ctl()
5218 ctl->sub_stripes = btrfs_raid_array[index].sub_stripes; in init_alloc_chunk_ctl()
5219 ctl->dev_stripes = btrfs_raid_array[index].dev_stripes; in init_alloc_chunk_ctl()
5220 ctl->devs_max = btrfs_raid_array[index].devs_max; in init_alloc_chunk_ctl()
5221 if (!ctl->devs_max) in init_alloc_chunk_ctl()
5222 ctl->devs_max = BTRFS_MAX_DEVS(fs_devices->fs_info); in init_alloc_chunk_ctl()
5223 ctl->devs_min = btrfs_raid_array[index].devs_min; in init_alloc_chunk_ctl()
5224 ctl->devs_increment = btrfs_raid_array[index].devs_increment; in init_alloc_chunk_ctl()
5225 ctl->ncopies = btrfs_raid_array[index].ncopies; in init_alloc_chunk_ctl()
5226 ctl->nparity = btrfs_raid_array[index].nparity; in init_alloc_chunk_ctl()
5227 ctl->ndevs = 0; in init_alloc_chunk_ctl()
5229 switch (fs_devices->chunk_alloc_policy) { in init_alloc_chunk_ctl()
5245 struct btrfs_fs_info *info = fs_devices->fs_info; in gather_device_info()
5248 u64 dev_extent_want = ctl->max_stripe_size * ctl->dev_stripes; in gather_device_info()
5258 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { in gather_device_info()
5259 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { in gather_device_info()
5261 "BTRFS: read-only device in alloc_list\n"); in gather_device_info()
5266 &device->dev_state) || in gather_device_info()
5267 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) in gather_device_info()
5270 if (device->total_bytes > device->bytes_used) in gather_device_info()
5271 total_avail = device->total_bytes - device->bytes_used; in gather_device_info()
5276 if (total_avail < ctl->dev_extent_min) in gather_device_info()
5281 if (ret && ret != -ENOSPC) in gather_device_info()
5287 if (max_avail < ctl->dev_extent_min) { in gather_device_info()
5291 __func__, device->devid, max_avail, in gather_device_info()
5292 ctl->dev_extent_min); in gather_device_info()
5296 if (ndevs == fs_devices->rw_devices) { in gather_device_info()
5298 __func__, fs_devices->rw_devices); in gather_device_info()
5307 ctl->ndevs = ndevs; in gather_device_info()
5331 ctl->stripe_size = div_u64(devices_info[ctl->ndevs - 1].max_avail, in decide_stripe_size_regular()
5332 ctl->dev_stripes); in decide_stripe_size_regular()
5333 ctl->num_stripes = ctl->ndevs * ctl->dev_stripes; in decide_stripe_size_regular()
5336 data_stripes = (ctl->num_stripes - ctl->nparity) / ctl->ncopies; in decide_stripe_size_regular()
5344 if (ctl->stripe_size * data_stripes > ctl->max_chunk_size) { in decide_stripe_size_regular()
5350 ctl->stripe_size = min(round_up(div_u64(ctl->max_chunk_size, in decide_stripe_size_regular()
5352 ctl->stripe_size); in decide_stripe_size_regular()
5356 ctl->stripe_size = min_t(u64, ctl->stripe_size, SZ_1G); in decide_stripe_size_regular()
5359 ctl->stripe_size = round_down(ctl->stripe_size, BTRFS_STRIPE_LEN); in decide_stripe_size_regular()
5360 ctl->chunk_size = ctl->stripe_size * data_stripes; in decide_stripe_size_regular()
5368 u64 zone_size = devices_info[0].dev->zone_info->zone_size; in decide_stripe_size_zoned()
5376 ASSERT(devices_info[ctl->ndevs - 1].max_avail == ctl->dev_extent_min); in decide_stripe_size_zoned()
5378 ctl->stripe_size = zone_size; in decide_stripe_size_zoned()
5379 ctl->num_stripes = ctl->ndevs * ctl->dev_stripes; in decide_stripe_size_zoned()
5380 data_stripes = (ctl->num_stripes - ctl->nparity) / ctl->ncopies; in decide_stripe_size_zoned()
5383 if (ctl->stripe_size * data_stripes > ctl->max_chunk_size) { in decide_stripe_size_zoned()
5384 ctl->ndevs = div_u64(div_u64(ctl->max_chunk_size * ctl->ncopies, in decide_stripe_size_zoned()
5385 ctl->stripe_size) + ctl->nparity, in decide_stripe_size_zoned()
5386 ctl->dev_stripes); in decide_stripe_size_zoned()
5387 ctl->num_stripes = ctl->ndevs * ctl->dev_stripes; in decide_stripe_size_zoned()
5388 data_stripes = (ctl->num_stripes - ctl->nparity) / ctl->ncopies; in decide_stripe_size_zoned()
5389 ASSERT(ctl->stripe_size * data_stripes <= ctl->max_chunk_size); in decide_stripe_size_zoned()
5392 ctl->chunk_size = ctl->stripe_size * data_stripes; in decide_stripe_size_zoned()
5401 struct btrfs_fs_info *info = fs_devices->fs_info; in decide_stripe_size()
5408 ctl->ndevs = rounddown(ctl->ndevs, ctl->devs_increment); in decide_stripe_size()
5410 if (ctl->ndevs < ctl->devs_min) { in decide_stripe_size()
5414 __func__, ctl->ndevs, ctl->devs_min); in decide_stripe_size()
5416 return -ENOSPC; in decide_stripe_size()
5419 ctl->ndevs = min(ctl->ndevs, ctl->devs_max); in decide_stripe_size()
5421 switch (fs_devices->chunk_alloc_policy) { in decide_stripe_size()
5435 struct btrfs_fs_info *info = trans->fs_info; in create_chunk()
5436 struct map_lookup *map = NULL; in create_chunk() local
5440 u64 start = ctl->start; in create_chunk()
5441 u64 type = ctl->type; in create_chunk()
5446 map = kmalloc(map_lookup_size(ctl->num_stripes), GFP_NOFS); in create_chunk()
5447 if (!map) in create_chunk()
5448 return ERR_PTR(-ENOMEM); in create_chunk()
5449 map->num_stripes = ctl->num_stripes; in create_chunk()
5451 for (i = 0; i < ctl->ndevs; ++i) { in create_chunk()
5452 for (j = 0; j < ctl->dev_stripes; ++j) { in create_chunk()
5453 int s = i * ctl->dev_stripes + j; in create_chunk()
5454 map->stripes[s].dev = devices_info[i].dev; in create_chunk()
5455 map->stripes[s].physical = devices_info[i].dev_offset + in create_chunk()
5456 j * ctl->stripe_size; in create_chunk()
5459 map->io_align = BTRFS_STRIPE_LEN; in create_chunk()
5460 map->io_width = BTRFS_STRIPE_LEN; in create_chunk()
5461 map->type = type; in create_chunk()
5462 map->sub_stripes = ctl->sub_stripes; in create_chunk()
5464 trace_btrfs_chunk_alloc(info, map, start, ctl->chunk_size); in create_chunk()
5468 kfree(map); in create_chunk()
5469 return ERR_PTR(-ENOMEM); in create_chunk()
5471 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); in create_chunk()
5472 em->map_lookup = map; in create_chunk()
5473 em->start = start; in create_chunk()
5474 em->len = ctl->chunk_size; in create_chunk()
5475 em->block_start = 0; in create_chunk()
5476 em->block_len = em->len; in create_chunk()
5477 em->orig_block_len = ctl->stripe_size; in create_chunk()
5479 em_tree = &info->mapping_tree; in create_chunk()
5480 write_lock(&em_tree->lock); in create_chunk()
5483 write_unlock(&em_tree->lock); in create_chunk()
5487 write_unlock(&em_tree->lock); in create_chunk()
5489 block_group = btrfs_make_block_group(trans, type, start, ctl->chunk_size); in create_chunk()
5493 for (i = 0; i < map->num_stripes; i++) { in create_chunk()
5494 struct btrfs_device *dev = map->stripes[i].dev; in create_chunk()
5497 dev->bytes_used + ctl->stripe_size); in create_chunk()
5498 if (list_empty(&dev->post_commit_list)) in create_chunk()
5499 list_add_tail(&dev->post_commit_list, in create_chunk()
5500 &trans->transaction->dev_update_list); in create_chunk()
5503 atomic64_sub(ctl->stripe_size * map->num_stripes, in create_chunk()
5504 &info->free_chunk_space); in create_chunk()
5513 write_lock(&em_tree->lock); in create_chunk()
5515 write_unlock(&em_tree->lock); in create_chunk()
5528 struct btrfs_fs_info *info = trans->fs_info; in btrfs_create_chunk()
5529 struct btrfs_fs_devices *fs_devices = info->fs_devices; in btrfs_create_chunk()
5535 lockdep_assert_held(&info->chunk_mutex); in btrfs_create_chunk()
5539 return ERR_PTR(-EINVAL); in btrfs_create_chunk()
5542 if (list_empty(&fs_devices->alloc_list)) { in btrfs_create_chunk()
5545 return ERR_PTR(-ENOSPC); in btrfs_create_chunk()
5551 return ERR_PTR(-EINVAL); in btrfs_create_chunk()
5558 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info), in btrfs_create_chunk()
5561 return ERR_PTR(-ENOMEM); in btrfs_create_chunk()
5593 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_chunk_alloc_add_chunk_item()
5594 struct btrfs_root *chunk_root = fs_info->chunk_root; in btrfs_chunk_alloc_add_chunk_item()
5599 struct map_lookup *map; in btrfs_chunk_alloc_add_chunk_item() local
5613 * that replaces the device object associated with the map's stripes, in btrfs_chunk_alloc_add_chunk_item()
5616 * (dev-replace.c:btrfs_dev_replace_finishing()), so we could grab the in btrfs_chunk_alloc_add_chunk_item()
5622 * acquires both mutexes - first the device_list_mutex and then the in btrfs_chunk_alloc_add_chunk_item()
5626 lockdep_assert_held(&fs_info->chunk_mutex); in btrfs_chunk_alloc_add_chunk_item()
5628 em = btrfs_get_chunk_map(fs_info, bg->start, bg->length); in btrfs_chunk_alloc_add_chunk_item()
5635 map = em->map_lookup; in btrfs_chunk_alloc_add_chunk_item()
5636 item_size = btrfs_chunk_item_size(map->num_stripes); in btrfs_chunk_alloc_add_chunk_item()
5640 ret = -ENOMEM; in btrfs_chunk_alloc_add_chunk_item()
5645 for (i = 0; i < map->num_stripes; i++) { in btrfs_chunk_alloc_add_chunk_item()
5646 struct btrfs_device *device = map->stripes[i].dev; in btrfs_chunk_alloc_add_chunk_item()
5653 stripe = &chunk->stripe; in btrfs_chunk_alloc_add_chunk_item()
5654 for (i = 0; i < map->num_stripes; i++) { in btrfs_chunk_alloc_add_chunk_item()
5655 struct btrfs_device *device = map->stripes[i].dev; in btrfs_chunk_alloc_add_chunk_item()
5656 const u64 dev_offset = map->stripes[i].physical; in btrfs_chunk_alloc_add_chunk_item()
5658 btrfs_set_stack_stripe_devid(stripe, device->devid); in btrfs_chunk_alloc_add_chunk_item()
5660 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); in btrfs_chunk_alloc_add_chunk_item()
5664 btrfs_set_stack_chunk_length(chunk, bg->length); in btrfs_chunk_alloc_add_chunk_item()
5667 btrfs_set_stack_chunk_type(chunk, map->type); in btrfs_chunk_alloc_add_chunk_item()
5668 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes); in btrfs_chunk_alloc_add_chunk_item()
5671 btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize); in btrfs_chunk_alloc_add_chunk_item()
5672 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes); in btrfs_chunk_alloc_add_chunk_item()
5676 key.offset = bg->start; in btrfs_chunk_alloc_add_chunk_item()
5682 set_bit(BLOCK_GROUP_FLAG_CHUNK_ITEM_INSERTED, &bg->runtime_flags); in btrfs_chunk_alloc_add_chunk_item()
5684 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { in btrfs_chunk_alloc_add_chunk_item()
5698 struct btrfs_fs_info *fs_info = trans->fs_info; in init_first_rw_device()
5704 * When adding a new device for sprouting, the seed device is read-only in init_first_rw_device()
5710 * otherwise we would get -ENOSPC since the block groups from the in init_first_rw_device()
5711 * seed device are read-only; in init_first_rw_device()
5713 * 2) Add the device item for the new sprout device - finishing the setup in init_first_rw_device()
5716 * ensures this does not fail with -ENOSPC. in init_first_rw_device()
5737 static inline int btrfs_chunk_max_errors(struct map_lookup *map) in btrfs_chunk_max_errors() argument
5739 const int index = btrfs_bg_flags_to_raid_index(map->type); in btrfs_chunk_max_errors()
5747 struct map_lookup *map; in btrfs_chunk_writeable() local
5756 map = em->map_lookup; in btrfs_chunk_writeable()
5757 for (i = 0; i < map->num_stripes; i++) { in btrfs_chunk_writeable()
5759 &map->stripes[i].dev->dev_state)) { in btrfs_chunk_writeable()
5764 &map->stripes[i].dev->dev_state)) { in btrfs_chunk_writeable()
5774 if (miss_ndevs > btrfs_chunk_max_errors(map)) in btrfs_chunk_writeable()
5786 write_lock(&tree->lock); in btrfs_mapping_tree_free()
5787 em = lookup_extent_mapping(tree, 0, (u64)-1); in btrfs_mapping_tree_free()
5790 write_unlock(&tree->lock); in btrfs_mapping_tree_free()
5803 struct map_lookup *map; in btrfs_num_copies() local
5817 map = em->map_lookup; in btrfs_num_copies()
5818 index = btrfs_bg_flags_to_raid_index(map->type); in btrfs_num_copies()
5820 /* Non-RAID56, use their ncopies from btrfs_raid_array. */ in btrfs_num_copies()
5821 if (!(map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)) in btrfs_num_copies()
5823 else if (map->type & BTRFS_BLOCK_GROUP_RAID5) in btrfs_num_copies()
5825 else if (map->type & BTRFS_BLOCK_GROUP_RAID6) in btrfs_num_copies()
5833 ret = map->num_stripes; in btrfs_num_copies()
5842 struct map_lookup *map; in btrfs_full_stripe_len() local
5843 unsigned long len = fs_info->sectorsize; in btrfs_full_stripe_len()
5851 map = em->map_lookup; in btrfs_full_stripe_len()
5852 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) in btrfs_full_stripe_len()
5853 len = btrfs_stripe_nr_to_offset(nr_data_stripes(map)); in btrfs_full_stripe_len()
5862 struct map_lookup *map; in btrfs_is_parity_mirror() local
5871 map = em->map_lookup; in btrfs_is_parity_mirror()
5872 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) in btrfs_is_parity_mirror()
5880 struct map_lookup *map, int first, in find_live_mirror() argument
5889 ASSERT((map->type & in find_live_mirror()
5892 if (map->type & BTRFS_BLOCK_GROUP_RAID10) in find_live_mirror()
5893 num_stripes = map->sub_stripes; in find_live_mirror()
5895 num_stripes = map->num_stripes; in find_live_mirror()
5897 switch (fs_info->fs_devices->read_policy) { in find_live_mirror()
5902 fs_info->fs_devices->read_policy); in find_live_mirror()
5903 fs_info->fs_devices->read_policy = BTRFS_READ_POLICY_PID; in find_live_mirror()
5906 preferred_mirror = first + (current->pid % num_stripes); in find_live_mirror()
5911 fs_info->dev_replace.cont_reading_from_srcdev_mode == in find_live_mirror()
5913 srcdev = fs_info->dev_replace.srcdev; in find_live_mirror()
5919 * dev-replace procedure, only choose it if no other non-missing in find_live_mirror()
5923 if (map->stripes[preferred_mirror].dev->bdev && in find_live_mirror()
5924 (tolerance || map->stripes[preferred_mirror].dev != srcdev)) in find_live_mirror()
5927 if (map->stripes[i].dev->bdev && in find_live_mirror()
5928 (tolerance || map->stripes[i].dev != srcdev)) in find_live_mirror()
5954 refcount_set(&bioc->refs, 1); in alloc_btrfs_io_context()
5956 bioc->fs_info = fs_info; in alloc_btrfs_io_context()
5957 bioc->replace_stripe_src = -1; in alloc_btrfs_io_context()
5958 bioc->full_stripe_logical = (u64)-1; in alloc_btrfs_io_context()
5965 WARN_ON(!refcount_read(&bioc->refs)); in btrfs_get_bioc()
5966 refcount_inc(&bioc->refs); in btrfs_get_bioc()
5973 if (refcount_dec_and_test(&bioc->refs)) in btrfs_put_bioc()
5986 struct map_lookup *map; in btrfs_map_discard() local
6008 map = em->map_lookup; in btrfs_map_discard()
6011 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { in btrfs_map_discard()
6012 ret = -EOPNOTSUPP; in btrfs_map_discard()
6016 offset = logical - em->start; in btrfs_map_discard()
6017 length = min_t(u64, em->start + em->len - logical, length); in btrfs_map_discard()
6027 stripe_offset = offset - btrfs_stripe_nr_to_offset(stripe_nr); in btrfs_map_discard()
6031 stripe_cnt = stripe_nr_end - stripe_nr; in btrfs_map_discard()
6032 stripe_end_offset = btrfs_stripe_nr_to_offset(stripe_nr_end) - in btrfs_map_discard()
6041 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | in btrfs_map_discard()
6043 if (map->type & BTRFS_BLOCK_GROUP_RAID0) in btrfs_map_discard()
6046 sub_stripes = map->sub_stripes; in btrfs_map_discard()
6048 factor = map->num_stripes / sub_stripes; in btrfs_map_discard()
6049 *num_stripes = min_t(u64, map->num_stripes, in btrfs_map_discard()
6057 last_stripe = ((stripe_nr_end - 1) % factor) * sub_stripes; in btrfs_map_discard()
6058 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1_MASK | in btrfs_map_discard()
6060 *num_stripes = map->num_stripes; in btrfs_map_discard()
6062 stripe_index = stripe_nr % map->num_stripes; in btrfs_map_discard()
6063 stripe_nr /= map->num_stripes; in btrfs_map_discard()
6068 ret = -ENOMEM; in btrfs_map_discard()
6074 map->stripes[stripe_index].physical + in btrfs_map_discard()
6076 stripes[i].dev = map->stripes[stripe_index].dev; in btrfs_map_discard()
6078 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | in btrfs_map_discard()
6089 * |-------|...|-------| in btrfs_map_discard()
6090 * |----------| in btrfs_map_discard()
6094 stripes[i].length -= stripe_offset; in btrfs_map_discard()
6098 sub_stripes - 1)) in btrfs_map_discard()
6099 stripes[i].length -= stripe_end_offset; in btrfs_map_discard()
6101 if (i == sub_stripes - 1) in btrfs_map_discard()
6108 if (stripe_index == map->num_stripes) { in btrfs_map_discard()
6132 ret = test_bit(BLOCK_GROUP_FLAG_TO_COPY, &cache->runtime_flags); in is_block_group_to_copy()
6144 u64 srcdev_devid = dev_replace->srcdev->devid; in handle_ops_on_dev_replace()
6156 * the dev-replace process. We can avoid cloning IO here. in handle_ops_on_dev_replace()
6158 if (is_block_group_to_copy(dev_replace->srcdev->fs_info, logical)) in handle_ops_on_dev_replace()
6162 * Duplicate the write operations while the dev-replace procedure is in handle_ops_on_dev_replace()
6168 * Note that device->missing is handled by the caller, and that the in handle_ops_on_dev_replace()
6172 struct btrfs_io_stripe *old = &bioc->stripes[i]; in handle_ops_on_dev_replace()
6173 struct btrfs_io_stripe *new = &bioc->stripes[num_stripes + nr_extra_stripes]; in handle_ops_on_dev_replace()
6175 if (old->dev->devid != srcdev_devid) in handle_ops_on_dev_replace()
6178 new->physical = old->physical; in handle_ops_on_dev_replace()
6179 new->dev = dev_replace->tgtdev; in handle_ops_on_dev_replace()
6180 if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) in handle_ops_on_dev_replace()
6181 bioc->replace_stripe_src = i; in handle_ops_on_dev_replace()
6193 struct btrfs_io_stripe *first = &bioc->stripes[num_stripes]; in handle_ops_on_dev_replace()
6194 struct btrfs_io_stripe *second = &bioc->stripes[num_stripes + 1]; in handle_ops_on_dev_replace()
6197 ASSERT(bioc->map_type & BTRFS_BLOCK_GROUP_DUP); in handle_ops_on_dev_replace()
6203 if (first->physical > second->physical) { in handle_ops_on_dev_replace()
6204 swap(second->physical, first->physical); in handle_ops_on_dev_replace()
6205 swap(second->dev, first->dev); in handle_ops_on_dev_replace()
6206 nr_extra_stripes--; in handle_ops_on_dev_replace()
6212 bioc->replace_nr_stripes = nr_extra_stripes; in handle_ops_on_dev_replace()
6215 static u64 btrfs_max_io_len(struct map_lookup *map, enum btrfs_map_op op, in btrfs_max_io_len() argument
6227 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { in btrfs_max_io_len()
6229 btrfs_stripe_nr_to_offset(nr_data_stripes(map)); in btrfs_max_io_len()
6242 rounddown(*stripe_nr, nr_data_stripes(map))); in btrfs_max_io_len()
6251 return full_stripe_len - (offset - *full_stripe_start); in btrfs_max_io_len()
6258 if (map->type & BTRFS_BLOCK_GROUP_STRIPE_MASK) in btrfs_max_io_len()
6259 return BTRFS_STRIPE_LEN - *stripe_offset; in btrfs_max_io_len()
6263 static void set_io_stripe(struct btrfs_io_stripe *dst, const struct map_lookup *map, in set_io_stripe() argument
6266 dst->dev = map->stripes[stripe_index].dev; in set_io_stripe()
6267 dst->physical = map->stripes[stripe_index].physical + in set_io_stripe()
6272 * Map one logical range to one or more physical ranges.
6285 * If the map request can be fulfilled by one single
6295 * For non-RAID56 profiles, non-zero mirror_num means
6306 * @need_raid_map: (Used only for integrity checker) whether the map wants
6307 * a full stripe map (including all data and P/Q stripes)
6317 struct map_lookup *map; in btrfs_map_block() local
6330 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; in btrfs_map_block()
6333 u64 raid56_full_stripe_start = (u64)-1; in btrfs_map_block()
6338 num_copies = btrfs_num_copies(fs_info, logical, fs_info->sectorsize); in btrfs_map_block()
6340 return -EINVAL; in btrfs_map_block()
6346 map = em->map_lookup; in btrfs_map_block()
6347 data_stripes = nr_data_stripes(map); in btrfs_map_block()
6349 map_offset = logical - em->start; in btrfs_map_block()
6350 max_len = btrfs_max_io_len(map, op, map_offset, &stripe_nr, in btrfs_map_block()
6352 *length = min_t(u64, em->len - map_offset, max_len); in btrfs_map_block()
6354 if (dev_replace->replace_task != current) in btrfs_map_block()
6355 down_read(&dev_replace->rwsem); in btrfs_map_block()
6362 if (!dev_replace_is_ongoing && dev_replace->replace_task != current) in btrfs_map_block()
6363 up_read(&dev_replace->rwsem); in btrfs_map_block()
6367 if (map->type & BTRFS_BLOCK_GROUP_RAID0) { in btrfs_map_block()
6368 stripe_index = stripe_nr % map->num_stripes; in btrfs_map_block()
6369 stripe_nr /= map->num_stripes; in btrfs_map_block()
6372 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1_MASK) { in btrfs_map_block()
6374 num_stripes = map->num_stripes; in btrfs_map_block()
6376 stripe_index = mirror_num - 1; in btrfs_map_block()
6378 stripe_index = find_live_mirror(fs_info, map, 0, in btrfs_map_block()
6383 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { in btrfs_map_block()
6385 num_stripes = map->num_stripes; in btrfs_map_block()
6387 stripe_index = mirror_num - 1; in btrfs_map_block()
6392 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { in btrfs_map_block()
6393 u32 factor = map->num_stripes / map->sub_stripes; in btrfs_map_block()
6395 stripe_index = (stripe_nr % factor) * map->sub_stripes; in btrfs_map_block()
6399 num_stripes = map->sub_stripes; in btrfs_map_block()
6401 stripe_index += mirror_num - 1; in btrfs_map_block()
6404 stripe_index = find_live_mirror(fs_info, map, in btrfs_map_block()
6407 mirror_num = stripe_index - old_stripe_index + 1; in btrfs_map_block()
6410 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { in btrfs_map_block()
6424 num_stripes = map->num_stripes; in btrfs_map_block()
6425 max_errors = btrfs_chunk_max_errors(map); in btrfs_map_block()
6429 raid56_full_stripe_start + em->start + in btrfs_map_block()
6430 btrfs_stripe_nr_to_offset(data_stripes)) - in btrfs_map_block()
6443 stripe_index = data_stripes + mirror_num - 2; in btrfs_map_block()
6446 stripe_index = (stripe_nr + stripe_index) % map->num_stripes; in btrfs_map_block()
6456 stripe_index = stripe_nr % map->num_stripes; in btrfs_map_block()
6457 stripe_nr /= map->num_stripes; in btrfs_map_block()
6460 if (stripe_index >= map->num_stripes) { in btrfs_map_block()
6463 stripe_index, map->num_stripes); in btrfs_map_block()
6464 ret = -EINVAL; in btrfs_map_block()
6469 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL && in btrfs_map_block()
6486 !((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) && mirror_num > 1)) { in btrfs_map_block()
6487 set_io_stripe(smap, map, stripe_index, stripe_offset, stripe_nr); in btrfs_map_block()
6497 ret = -ENOMEM; in btrfs_map_block()
6500 bioc->map_type = map->type; in btrfs_map_block()
6503 * For RAID56 full map, we need to make sure the stripes[] follows the in btrfs_map_block()
6509 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map && in btrfs_map_block()
6519 bioc->full_stripe_logical = em->start + in btrfs_map_block()
6522 set_io_stripe(&bioc->stripes[i], map, in btrfs_map_block()
6527 * For all other non-RAID56 profiles, just copy the target in btrfs_map_block()
6531 set_io_stripe(&bioc->stripes[i], map, stripe_index, in btrfs_map_block()
6538 max_errors = btrfs_chunk_max_errors(map); in btrfs_map_block()
6540 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL && in btrfs_map_block()
6547 bioc->num_stripes = num_stripes; in btrfs_map_block()
6548 bioc->max_errors = max_errors; in btrfs_map_block()
6549 bioc->mirror_num = mirror_num; in btrfs_map_block()
6552 if (dev_replace_is_ongoing && dev_replace->replace_task != current) { in btrfs_map_block()
6553 lockdep_assert_held(&dev_replace->rwsem); in btrfs_map_block()
6555 up_read(&dev_replace->rwsem); in btrfs_map_block()
6564 if (args->fsid == NULL) in dev_args_match_fs_devices()
6566 if (memcmp(fs_devices->metadata_uuid, args->fsid, BTRFS_FSID_SIZE) == 0) in dev_args_match_fs_devices()
6574 if (args->missing) { in dev_args_match_device()
6575 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state) && in dev_args_match_device()
6576 !device->bdev) in dev_args_match_device()
6581 if (device->devid != args->devid) in dev_args_match_device()
6583 if (args->uuid && memcmp(device->uuid, args->uuid, BTRFS_UUID_SIZE) != 0) in dev_args_match_device()
6602 list_for_each_entry(device, &fs_devices->devices, dev_list) { in btrfs_find_device()
6608 list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list) { in btrfs_find_device()
6611 list_for_each_entry(device, &seed_devs->devices, dev_list) { in btrfs_find_device()
6639 list_add(&device->dev_list, &fs_devices->devices); in add_missing_dev()
6640 device->fs_devices = fs_devices; in add_missing_dev()
6641 fs_devices->num_devices++; in add_missing_dev()
6643 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); in add_missing_dev()
6644 fs_devices->missing_devices++; in add_missing_dev()
6658 * @path: a pointer to device path if available, NULL otherwise.
6666 const char *path) in btrfs_alloc_device() argument
6672 return ERR_PTR(-EINVAL); in btrfs_alloc_device()
6676 return ERR_PTR(-ENOMEM); in btrfs_alloc_device()
6678 INIT_LIST_HEAD(&dev->dev_list); in btrfs_alloc_device()
6679 INIT_LIST_HEAD(&dev->dev_alloc_list); in btrfs_alloc_device()
6680 INIT_LIST_HEAD(&dev->post_commit_list); in btrfs_alloc_device()
6682 atomic_set(&dev->dev_stats_ccnt, 0); in btrfs_alloc_device()
6684 extent_io_tree_init(fs_info, &dev->alloc_state, IO_TREE_DEVICE_ALLOC_STATE); in btrfs_alloc_device()
6697 dev->devid = tmp; in btrfs_alloc_device()
6700 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE); in btrfs_alloc_device()
6702 generate_random_uuid(dev->uuid); in btrfs_alloc_device()
6704 if (path) { in btrfs_alloc_device()
6707 name = rcu_string_strdup(path, GFP_KERNEL); in btrfs_alloc_device()
6710 return ERR_PTR(-ENOMEM); in btrfs_alloc_device()
6712 rcu_assign_pointer(dev->name, name); in btrfs_alloc_device()
6731 const struct map_lookup *map = em->map_lookup; in btrfs_calc_stripe_length() local
6732 const int data_stripes = calc_data_stripes(map->type, map->num_stripes); in btrfs_calc_stripe_length()
6734 return div_u64(em->len, data_stripes); in btrfs_calc_stripe_length()
6755 return -EOVERFLOW; in check_32bit_meta_chunk()
6784 return ERR_PTR(-ENOENT); in handle_missing_device()
6787 dev = add_missing_dev(fs_info->fs_devices, devid, uuid); in handle_missing_device()
6802 struct btrfs_fs_info *fs_info = leaf->fs_info; in read_one_chunk()
6803 struct extent_map_tree *map_tree = &fs_info->mapping_tree; in read_one_chunk()
6804 struct map_lookup *map; in read_one_chunk() local
6816 logical = key->offset; in read_one_chunk()
6831 * as chunk item in tree block is already verified by tree-checker. in read_one_chunk()
6833 if (leaf->start == BTRFS_SUPER_INFO_OFFSET) { in read_one_chunk()
6839 read_lock(&map_tree->lock); in read_one_chunk()
6841 read_unlock(&map_tree->lock); in read_one_chunk()
6844 if (em && em->start <= logical && em->start + em->len > logical) { in read_one_chunk()
6853 return -ENOMEM; in read_one_chunk()
6854 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); in read_one_chunk()
6855 if (!map) { in read_one_chunk()
6857 return -ENOMEM; in read_one_chunk()
6860 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); in read_one_chunk()
6861 em->map_lookup = map; in read_one_chunk()
6862 em->start = logical; in read_one_chunk()
6863 em->len = length; in read_one_chunk()
6864 em->orig_start = 0; in read_one_chunk()
6865 em->block_start = 0; in read_one_chunk()
6866 em->block_len = em->len; in read_one_chunk()
6868 map->num_stripes = num_stripes; in read_one_chunk()
6869 map->io_width = btrfs_chunk_io_width(leaf, chunk); in read_one_chunk()
6870 map->io_align = btrfs_chunk_io_align(leaf, chunk); in read_one_chunk()
6871 map->type = type; in read_one_chunk()
6876 * In that case, it can cause divide-by-zero errors later. in read_one_chunk()
6880 map->sub_stripes = btrfs_raid_array[index].sub_stripes; in read_one_chunk()
6881 map->verified_stripes = 0; in read_one_chunk()
6882 em->orig_block_len = btrfs_calc_stripe_length(em); in read_one_chunk()
6884 map->stripes[i].physical = in read_one_chunk()
6892 map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices, &args); in read_one_chunk()
6893 if (!map->stripes[i].dev) { in read_one_chunk()
6894 map->stripes[i].dev = handle_missing_device(fs_info, in read_one_chunk()
6896 if (IS_ERR(map->stripes[i].dev)) { in read_one_chunk()
6897 ret = PTR_ERR(map->stripes[i].dev); in read_one_chunk()
6904 &(map->stripes[i].dev->dev_state)); in read_one_chunk()
6907 write_lock(&map_tree->lock); in read_one_chunk()
6909 write_unlock(&map_tree->lock); in read_one_chunk()
6912 "failed to add chunk map, start=%llu len=%llu: %d", in read_one_chunk()
6913 em->start, em->len, ret); in read_one_chunk()
6926 device->devid = btrfs_device_id(leaf, dev_item); in fill_device_from_item()
6927 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item); in fill_device_from_item()
6928 device->total_bytes = device->disk_total_bytes; in fill_device_from_item()
6929 device->commit_total_bytes = device->disk_total_bytes; in fill_device_from_item()
6930 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); in fill_device_from_item()
6931 device->commit_bytes_used = device->bytes_used; in fill_device_from_item()
6932 device->type = btrfs_device_type(leaf, dev_item); in fill_device_from_item()
6933 device->io_align = btrfs_device_io_align(leaf, dev_item); in fill_device_from_item()
6934 device->io_width = btrfs_device_io_width(leaf, dev_item); in fill_device_from_item()
6935 device->sector_size = btrfs_device_sector_size(leaf, dev_item); in fill_device_from_item()
6936 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID); in fill_device_from_item()
6937 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); in fill_device_from_item()
6940 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); in fill_device_from_item()
6952 /* This will match only for multi-device seed fs */ in open_seed_devices()
6953 list_for_each_entry(fs_devices, &fs_info->fs_devices->seed_list, seed_list) in open_seed_devices()
6954 if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE)) in open_seed_devices()
6961 return ERR_PTR(-ENOENT); in open_seed_devices()
6967 fs_devices->seeding = true; in open_seed_devices()
6968 fs_devices->opened = 1; in open_seed_devices()
6974 * respective fs_devices and anchor it at fs_info->fs_devices->seed_list in open_seed_devices()
6980 ret = open_fs_devices(fs_devices, BLK_OPEN_READ, fs_info->bdev_holder); in open_seed_devices()
6986 if (!fs_devices->seeding) { in open_seed_devices()
6989 return ERR_PTR(-EINVAL); in open_seed_devices()
6992 list_add(&fs_devices->seed_list, &fs_info->fs_devices->seed_list); in open_seed_devices()
7001 struct btrfs_fs_info *fs_info = leaf->fs_info; in read_one_dev()
7002 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; in read_one_dev()
7018 if (memcmp(fs_uuid, fs_devices->metadata_uuid, BTRFS_FSID_SIZE)) { in read_one_dev()
7024 device = btrfs_find_device(fs_info->fs_devices, &args); in read_one_dev()
7029 return -ENOENT; in read_one_dev()
7041 if (!device->bdev) { in read_one_dev()
7045 return -ENOENT; in read_one_dev()
7051 if (!device->bdev && in read_one_dev()
7052 !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) { in read_one_dev()
7056 * device->bdev is NULL, and so we have to set in read_one_dev()
7057 * device->missing to one here in read_one_dev()
7059 device->fs_devices->missing_devices++; in read_one_dev()
7060 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); in read_one_dev()
7064 if (device->fs_devices != fs_devices) { in read_one_dev()
7066 &device->dev_state)); in read_one_dev()
7068 list_move(&device->dev_list, &fs_devices->devices); in read_one_dev()
7069 device->fs_devices->num_devices--; in read_one_dev()
7070 fs_devices->num_devices++; in read_one_dev()
7072 device->fs_devices->missing_devices--; in read_one_dev()
7073 fs_devices->missing_devices++; in read_one_dev()
7075 device->fs_devices = fs_devices; in read_one_dev()
7079 if (device->fs_devices != fs_info->fs_devices) { in read_one_dev()
7080 BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)); in read_one_dev()
7081 if (device->generation != in read_one_dev()
7083 return -EINVAL; in read_one_dev()
7087 if (device->bdev) { in read_one_dev()
7088 u64 max_total_bytes = bdev_nr_bytes(device->bdev); in read_one_dev()
7090 if (device->total_bytes > max_total_bytes) { in read_one_dev()
7093 max_total_bytes, device->total_bytes); in read_one_dev()
7094 return -EINVAL; in read_one_dev()
7097 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); in read_one_dev()
7098 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && in read_one_dev()
7099 !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { in read_one_dev()
7100 device->fs_devices->total_rw_bytes += device->total_bytes; in read_one_dev()
7101 atomic64_add(device->total_bytes - device->bytes_used, in read_one_dev()
7102 &fs_info->free_chunk_space); in read_one_dev()
7110 struct btrfs_super_block *super_copy = fs_info->super_copy; in btrfs_read_sys_array()
7124 ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize); in btrfs_read_sys_array()
7133 return -ENOMEM; in btrfs_read_sys_array()
7139 array_ptr = super_copy->sys_chunk_array; in btrfs_read_sys_array()
7159 ret = -EIO; in btrfs_read_sys_array()
7177 ret = -EIO; in btrfs_read_sys_array()
7186 ret = -EIO; in btrfs_read_sys_array()
7211 return -EIO; in btrfs_read_sys_array()
7215 * Check if all chunks in the fs are OK for read-write degraded mount
7225 struct extent_map_tree *map_tree = &fs_info->mapping_tree; in btrfs_check_rw_degradable()
7230 read_lock(&map_tree->lock); in btrfs_check_rw_degradable()
7231 em = lookup_extent_mapping(map_tree, 0, (u64)-1); in btrfs_check_rw_degradable()
7232 read_unlock(&map_tree->lock); in btrfs_check_rw_degradable()
7239 struct map_lookup *map; in btrfs_check_rw_degradable() local
7244 map = em->map_lookup; in btrfs_check_rw_degradable()
7247 map->type); in btrfs_check_rw_degradable()
7248 for (i = 0; i < map->num_stripes; i++) { in btrfs_check_rw_degradable()
7249 struct btrfs_device *dev = map->stripes[i].dev; in btrfs_check_rw_degradable()
7251 if (!dev || !dev->bdev || in btrfs_check_rw_degradable()
7252 test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) || in btrfs_check_rw_degradable()
7253 dev->last_flush_error) in btrfs_check_rw_degradable()
7262 em->start, missing, max_tolerated); in btrfs_check_rw_degradable()
7270 read_lock(&map_tree->lock); in btrfs_check_rw_degradable()
7272 (u64)(-1) - next_start); in btrfs_check_rw_degradable()
7273 read_unlock(&map_tree->lock); in btrfs_check_rw_degradable()
7290 struct btrfs_root *root = fs_info->chunk_root; in btrfs_read_chunk_tree()
7291 struct btrfs_path *path; in btrfs_read_chunk_tree() local
7301 path = btrfs_alloc_path(); in btrfs_read_chunk_tree()
7302 if (!path) in btrfs_read_chunk_tree()
7303 return -ENOMEM; in btrfs_read_chunk_tree()
7313 * we execute this code path, but open_fs_devices failed to clear in btrfs_read_chunk_tree()
7317 fs_info->fs_devices->total_rw_bytes = 0; in btrfs_read_chunk_tree()
7329 ASSERT(!test_bit(BTRFS_FS_OPEN, &fs_info->flags)); in btrfs_read_chunk_tree()
7330 path->skip_locking = 1; in btrfs_read_chunk_tree()
7336 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID). in btrfs_read_chunk_tree()
7341 btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) { in btrfs_read_chunk_tree()
7342 struct extent_buffer *node = path->nodes[1]; in btrfs_read_chunk_tree()
7344 leaf = path->nodes[0]; in btrfs_read_chunk_tree()
7345 slot = path->slots[0]; in btrfs_read_chunk_tree()
7348 if (last_ra_node != node->start) { in btrfs_read_chunk_tree()
7350 last_ra_node = node->start; in btrfs_read_chunk_tree()
7366 * fs_info->chunk_mutex. Plus, to avoid lockdep warnings, in btrfs_read_chunk_tree()
7367 * we always lock first fs_info->chunk_mutex before in btrfs_read_chunk_tree()
7388 if (total_dev != fs_info->fs_devices->total_devices) { in btrfs_read_chunk_tree()
7391 btrfs_super_num_devices(fs_info->super_copy), in btrfs_read_chunk_tree()
7393 fs_info->fs_devices->total_devices = total_dev; in btrfs_read_chunk_tree()
7394 btrfs_set_super_num_devices(fs_info->super_copy, total_dev); in btrfs_read_chunk_tree()
7396 if (btrfs_super_total_bytes(fs_info->super_copy) < in btrfs_read_chunk_tree()
7397 fs_info->fs_devices->total_rw_bytes) { in btrfs_read_chunk_tree()
7400 btrfs_super_total_bytes(fs_info->super_copy), in btrfs_read_chunk_tree()
7401 fs_info->fs_devices->total_rw_bytes); in btrfs_read_chunk_tree()
7402 ret = -EINVAL; in btrfs_read_chunk_tree()
7409 btrfs_free_path(path); in btrfs_read_chunk_tree()
7415 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices, *seed_devs; in btrfs_init_devices_late()
7419 fs_devices->fs_info = fs_info; in btrfs_init_devices_late()
7421 mutex_lock(&fs_devices->device_list_mutex); in btrfs_init_devices_late()
7422 list_for_each_entry(device, &fs_devices->devices, dev_list) in btrfs_init_devices_late()
7423 device->fs_info = fs_info; in btrfs_init_devices_late()
7425 list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list) { in btrfs_init_devices_late()
7426 list_for_each_entry(device, &seed_devs->devices, dev_list) { in btrfs_init_devices_late()
7427 device->fs_info = fs_info; in btrfs_init_devices_late()
7433 seed_devs->fs_info = fs_info; in btrfs_init_devices_late()
7435 mutex_unlock(&fs_devices->device_list_mutex); in btrfs_init_devices_late()
7464 struct btrfs_path *path) in btrfs_device_init_dev_stats() argument
7472 if (!device->fs_info->dev_root) in btrfs_device_init_dev_stats()
7477 key.offset = device->devid; in btrfs_device_init_dev_stats()
7478 ret = btrfs_search_slot(NULL, device->fs_info->dev_root, &key, path, 0, 0); in btrfs_device_init_dev_stats()
7482 device->dev_stats_valid = 1; in btrfs_device_init_dev_stats()
7483 btrfs_release_path(path); in btrfs_device_init_dev_stats()
7486 slot = path->slots[0]; in btrfs_device_init_dev_stats()
7487 eb = path->nodes[0]; in btrfs_device_init_dev_stats()
7500 device->dev_stats_valid = 1; in btrfs_device_init_dev_stats()
7502 btrfs_release_path(path); in btrfs_device_init_dev_stats()
7509 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices, *seed_devs; in btrfs_init_dev_stats()
7511 struct btrfs_path *path = NULL; in btrfs_init_dev_stats() local
7514 path = btrfs_alloc_path(); in btrfs_init_dev_stats()
7515 if (!path) in btrfs_init_dev_stats()
7516 return -ENOMEM; in btrfs_init_dev_stats()
7518 mutex_lock(&fs_devices->device_list_mutex); in btrfs_init_dev_stats()
7519 list_for_each_entry(device, &fs_devices->devices, dev_list) { in btrfs_init_dev_stats()
7520 ret = btrfs_device_init_dev_stats(device, path); in btrfs_init_dev_stats()
7524 list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list) { in btrfs_init_dev_stats()
7525 list_for_each_entry(device, &seed_devs->devices, dev_list) { in btrfs_init_dev_stats()
7526 ret = btrfs_device_init_dev_stats(device, path); in btrfs_init_dev_stats()
7532 mutex_unlock(&fs_devices->device_list_mutex); in btrfs_init_dev_stats()
7534 btrfs_free_path(path); in btrfs_init_dev_stats()
7541 struct btrfs_fs_info *fs_info = trans->fs_info; in update_dev_stat_item()
7542 struct btrfs_root *dev_root = fs_info->dev_root; in update_dev_stat_item()
7543 struct btrfs_path *path; in update_dev_stat_item() local
7552 key.offset = device->devid; in update_dev_stat_item()
7554 path = btrfs_alloc_path(); in update_dev_stat_item()
7555 if (!path) in update_dev_stat_item()
7556 return -ENOMEM; in update_dev_stat_item()
7557 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1); in update_dev_stat_item()
7566 btrfs_item_size(path->nodes[0], path->slots[0]) < sizeof(*ptr)) { in update_dev_stat_item()
7568 ret = btrfs_del_item(trans, dev_root, path); in update_dev_stat_item()
7580 btrfs_release_path(path); in update_dev_stat_item()
7581 ret = btrfs_insert_empty_item(trans, dev_root, path, in update_dev_stat_item()
7591 eb = path->nodes[0]; in update_dev_stat_item()
7592 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item); in update_dev_stat_item()
7599 btrfs_free_path(path); in update_dev_stat_item()
7608 struct btrfs_fs_info *fs_info = trans->fs_info; in btrfs_run_dev_stats()
7609 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; in btrfs_run_dev_stats()
7614 mutex_lock(&fs_devices->device_list_mutex); in btrfs_run_dev_stats()
7615 list_for_each_entry(device, &fs_devices->devices, dev_list) { in btrfs_run_dev_stats()
7616 stats_cnt = atomic_read(&device->dev_stats_ccnt); in btrfs_run_dev_stats()
7617 if (!device->dev_stats_valid || stats_cnt == 0) in btrfs_run_dev_stats()
7622 * There is a LOAD-LOAD control dependency between the value of in btrfs_run_dev_stats()
7623 * dev_stats_ccnt and updating the on-disk values which requires in btrfs_run_dev_stats()
7624 * reading the in-memory counters. Such control dependencies in btrfs_run_dev_stats()
7636 atomic_sub(stats_cnt, &device->dev_stats_ccnt); in btrfs_run_dev_stats()
7638 mutex_unlock(&fs_devices->device_list_mutex); in btrfs_run_dev_stats()
7647 if (!dev->dev_stats_valid) in btrfs_dev_stat_inc_and_print()
7649 btrfs_err_rl_in_rcu(dev->fs_info, in btrfs_dev_stat_inc_and_print()
7669 btrfs_info_in_rcu(dev->fs_info, in btrfs_dev_stat_print_on_load()
7684 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; in btrfs_get_dev_stats()
7687 mutex_lock(&fs_devices->device_list_mutex); in btrfs_get_dev_stats()
7688 args.devid = stats->devid; in btrfs_get_dev_stats()
7689 dev = btrfs_find_device(fs_info->fs_devices, &args); in btrfs_get_dev_stats()
7690 mutex_unlock(&fs_devices->device_list_mutex); in btrfs_get_dev_stats()
7694 return -ENODEV; in btrfs_get_dev_stats()
7695 } else if (!dev->dev_stats_valid) { in btrfs_get_dev_stats()
7697 return -ENODEV; in btrfs_get_dev_stats()
7698 } else if (stats->flags & BTRFS_DEV_STATS_RESET) { in btrfs_get_dev_stats()
7700 if (stats->nr_items > i) in btrfs_get_dev_stats()
7701 stats->values[i] = in btrfs_get_dev_stats()
7707 current->comm, task_pid_nr(current)); in btrfs_get_dev_stats()
7710 if (stats->nr_items > i) in btrfs_get_dev_stats()
7711 stats->values[i] = btrfs_dev_stat_read(dev, i); in btrfs_get_dev_stats()
7713 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX) in btrfs_get_dev_stats()
7714 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX; in btrfs_get_dev_stats()
7729 ASSERT(trans->state == TRANS_STATE_COMMIT_DOING); in btrfs_commit_device_sizes()
7731 if (list_empty(&trans->dev_update_list)) in btrfs_commit_device_sizes()
7739 mutex_lock(&trans->fs_info->chunk_mutex); in btrfs_commit_device_sizes()
7740 list_for_each_entry_safe(curr, next, &trans->dev_update_list, in btrfs_commit_device_sizes()
7742 list_del_init(&curr->post_commit_list); in btrfs_commit_device_sizes()
7743 curr->commit_total_bytes = curr->disk_total_bytes; in btrfs_commit_device_sizes()
7744 curr->commit_bytes_used = curr->bytes_used; in btrfs_commit_device_sizes()
7746 mutex_unlock(&trans->fs_info->chunk_mutex); in btrfs_commit_device_sizes()
7750 * Multiplicity factor for simple profiles: DUP, RAID1-like and RAID10.
7766 struct extent_map_tree *em_tree = &fs_info->mapping_tree; in verify_one_dev_extent()
7768 struct map_lookup *map; in verify_one_dev_extent() local
7775 read_lock(&em_tree->lock); in verify_one_dev_extent()
7777 read_unlock(&em_tree->lock); in verify_one_dev_extent()
7783 ret = -EUCLEAN; in verify_one_dev_extent()
7787 map = em->map_lookup; in verify_one_dev_extent()
7792 physical_offset, devid, em->start, physical_len, in verify_one_dev_extent()
7794 ret = -EUCLEAN; in verify_one_dev_extent()
7808 for (i = 0; i < map->num_stripes; i++) { in verify_one_dev_extent()
7809 if (map->stripes[i].dev->devid == devid && in verify_one_dev_extent()
7810 map->stripes[i].physical == physical_offset) { in verify_one_dev_extent()
7812 if (map->verified_stripes >= map->num_stripes) { in verify_one_dev_extent()
7815 em->start); in verify_one_dev_extent()
7816 ret = -EUCLEAN; in verify_one_dev_extent()
7819 map->verified_stripes++; in verify_one_dev_extent()
7827 ret = -EUCLEAN; in verify_one_dev_extent()
7831 dev = btrfs_find_device(fs_info->fs_devices, &args); in verify_one_dev_extent()
7834 ret = -EUCLEAN; in verify_one_dev_extent()
7838 if (physical_offset + physical_len > dev->disk_total_bytes) { in verify_one_dev_extent()
7842 dev->disk_total_bytes); in verify_one_dev_extent()
7843 ret = -EUCLEAN; in verify_one_dev_extent()
7847 if (dev->zone_info) { in verify_one_dev_extent()
7848 u64 zone_size = dev->zone_info->zone_size; in verify_one_dev_extent()
7855 ret = -EUCLEAN; in verify_one_dev_extent()
7867 struct extent_map_tree *em_tree = &fs_info->mapping_tree; in verify_chunk_dev_extent_mapping()
7872 read_lock(&em_tree->lock); in verify_chunk_dev_extent_mapping()
7873 for (node = rb_first_cached(&em_tree->map); node; node = rb_next(node)) { in verify_chunk_dev_extent_mapping()
7875 if (em->map_lookup->num_stripes != in verify_chunk_dev_extent_mapping()
7876 em->map_lookup->verified_stripes) { in verify_chunk_dev_extent_mapping()
7879 em->start, em->map_lookup->verified_stripes, in verify_chunk_dev_extent_mapping()
7880 em->map_lookup->num_stripes); in verify_chunk_dev_extent_mapping()
7881 ret = -EUCLEAN; in verify_chunk_dev_extent_mapping()
7886 read_unlock(&em_tree->lock); in verify_chunk_dev_extent_mapping()
7899 struct btrfs_path *path; in btrfs_verify_dev_extents() local
7900 struct btrfs_root *root = fs_info->dev_root; in btrfs_verify_dev_extents()
7923 path = btrfs_alloc_path(); in btrfs_verify_dev_extents()
7924 if (!path) in btrfs_verify_dev_extents()
7925 return -ENOMEM; in btrfs_verify_dev_extents()
7927 path->reada = READA_FORWARD; in btrfs_verify_dev_extents()
7928 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); in btrfs_verify_dev_extents()
7932 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { in btrfs_verify_dev_extents()
7933 ret = btrfs_next_leaf(root, path); in btrfs_verify_dev_extents()
7938 ret = -EUCLEAN; in btrfs_verify_dev_extents()
7943 struct extent_buffer *leaf = path->nodes[0]; in btrfs_verify_dev_extents()
7945 int slot = path->slots[0]; in btrfs_verify_dev_extents()
7966 ret = -EUCLEAN; in btrfs_verify_dev_extents()
7977 ret = btrfs_next_item(root, path); in btrfs_verify_dev_extents()
7989 btrfs_free_path(path); in btrfs_verify_dev_extents()
8002 spin_lock(&fs_info->swapfile_pins_lock); in btrfs_pinned_by_swapfile()
8003 node = fs_info->swapfile_pins.rb_node; in btrfs_pinned_by_swapfile()
8006 if (ptr < sp->ptr) in btrfs_pinned_by_swapfile()
8007 node = node->rb_left; in btrfs_pinned_by_swapfile()
8008 else if (ptr > sp->ptr) in btrfs_pinned_by_swapfile()
8009 node = node->rb_right; in btrfs_pinned_by_swapfile()
8013 spin_unlock(&fs_info->swapfile_pins_lock); in btrfs_pinned_by_swapfile()
8020 struct btrfs_fs_info *fs_info = cache->fs_info; in relocating_repair_kthread()
8024 target = cache->start; in relocating_repair_kthread()
8027 sb_start_write(fs_info->sb); in relocating_repair_kthread()
8032 sb_end_write(fs_info->sb); in relocating_repair_kthread()
8033 return -EBUSY; in relocating_repair_kthread()
8036 mutex_lock(&fs_info->reclaim_bgs_lock); in relocating_repair_kthread()
8043 if (!test_bit(BLOCK_GROUP_FLAG_RELOCATING_REPAIR, &cache->runtime_flags)) in relocating_repair_kthread()
8058 mutex_unlock(&fs_info->reclaim_bgs_lock); in relocating_repair_kthread()
8060 sb_end_write(fs_info->sb); in relocating_repair_kthread()
8080 if (test_and_set_bit(BLOCK_GROUP_FLAG_RELOCATING_REPAIR, &cache->runtime_flags)) { in btrfs_repair_one_zone()
8086 "btrfs-relocating-repair"); in btrfs_repair_one_zone()
8099 u64 stripe_start = bioc->full_stripe_logical + in map_raid56_repair_block()
8107 smap->dev = bioc->stripes[i].dev; in map_raid56_repair_block()
8108 smap->physical = bioc->stripes[i].physical + in map_raid56_repair_block()
8109 ((logical - bioc->full_stripe_logical) & in map_raid56_repair_block()
8114 * Map a repair write into a single device.
8118 * Not update any other mirrors nor go through RMW path.
8122 * - Call btrfs_bio_counter_inc_blocked() first
8123 * - The range does not cross stripe boundary
8124 * - Has a valid @mirror_num passed in.
8142 /* The map range should not cross stripe boundary. */ in btrfs_map_repair_block()
8149 /* Map the RAID56 multi-stripe writes to a single one. */ in btrfs_map_repair_block()
8150 if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) { in btrfs_map_repair_block()
8155 ASSERT(mirror_num <= bioc->num_stripes); in btrfs_map_repair_block()
8156 smap->dev = bioc->stripes[mirror_num - 1].dev; in btrfs_map_repair_block()
8157 smap->physical = bioc->stripes[mirror_num - 1].physical; in btrfs_map_repair_block()
8160 ASSERT(smap->dev); in btrfs_map_repair_block()