| // SPDX-License-Identifier: GPL-2.0 |
| /* |
| * Copyright (C) 2007 Oracle. All rights reserved. |
| */ |
| |
| #include <linux/sched.h> |
| #include <linux/sched/mm.h> |
| #include <linux/bio.h> |
| #include <linux/slab.h> |
| #include <linux/blkdev.h> |
| #include <linux/ratelimit.h> |
| #include <linux/kthread.h> |
| #include <linux/raid/pq.h> |
| #include <linux/semaphore.h> |
| #include <linux/uuid.h> |
| #include <linux/list_sort.h> |
| #include <linux/namei.h> |
| #include "misc.h" |
| #include "ctree.h" |
| #include "extent_map.h" |
| #include "disk-io.h" |
| #include "transaction.h" |
| #include "print-tree.h" |
| #include "volumes.h" |
| #include "raid56.h" |
| #include "async-thread.h" |
| #include "check-integrity.h" |
| #include "rcu-string.h" |
| #include "dev-replace.h" |
| #include "sysfs.h" |
| #include "tree-checker.h" |
| #include "space-info.h" |
| #include "block-group.h" |
| #include "discard.h" |
| #include "zoned.h" |
| |
| const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = { |
| [BTRFS_RAID_RAID10] = { |
| .sub_stripes = 2, |
| .dev_stripes = 1, |
| .devs_max = 0, /* 0 == as many as possible */ |
| .devs_min = 2, |
| .tolerated_failures = 1, |
| .devs_increment = 2, |
| .ncopies = 2, |
| .nparity = 0, |
| .raid_name = "raid10", |
| .bg_flag = BTRFS_BLOCK_GROUP_RAID10, |
| .mindev_error = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET, |
| }, |
| [BTRFS_RAID_RAID1] = { |
| .sub_stripes = 1, |
| .dev_stripes = 1, |
| .devs_max = 2, |
| .devs_min = 2, |
| .tolerated_failures = 1, |
| .devs_increment = 2, |
| .ncopies = 2, |
| .nparity = 0, |
| .raid_name = "raid1", |
| .bg_flag = BTRFS_BLOCK_GROUP_RAID1, |
| .mindev_error = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET, |
| }, |
| [BTRFS_RAID_RAID1C3] = { |
| .sub_stripes = 1, |
| .dev_stripes = 1, |
| .devs_max = 3, |
| .devs_min = 3, |
| .tolerated_failures = 2, |
| .devs_increment = 3, |
| .ncopies = 3, |
| .nparity = 0, |
| .raid_name = "raid1c3", |
| .bg_flag = BTRFS_BLOCK_GROUP_RAID1C3, |
| .mindev_error = BTRFS_ERROR_DEV_RAID1C3_MIN_NOT_MET, |
| }, |
| [BTRFS_RAID_RAID1C4] = { |
| .sub_stripes = 1, |
| .dev_stripes = 1, |
| .devs_max = 4, |
| .devs_min = 4, |
| .tolerated_failures = 3, |
| .devs_increment = 4, |
| .ncopies = 4, |
| .nparity = 0, |
| .raid_name = "raid1c4", |
| .bg_flag = BTRFS_BLOCK_GROUP_RAID1C4, |
| .mindev_error = BTRFS_ERROR_DEV_RAID1C4_MIN_NOT_MET, |
| }, |
| [BTRFS_RAID_DUP] = { |
| .sub_stripes = 1, |
| .dev_stripes = 2, |
| .devs_max = 1, |
| .devs_min = 1, |
| .tolerated_failures = 0, |
| .devs_increment = 1, |
| .ncopies = 2, |
| .nparity = 0, |
| .raid_name = "dup", |
| .bg_flag = BTRFS_BLOCK_GROUP_DUP, |
| .mindev_error = 0, |
| }, |
| [BTRFS_RAID_RAID0] = { |
| .sub_stripes = 1, |
| .dev_stripes = 1, |
| .devs_max = 0, |
| .devs_min = 1, |
| .tolerated_failures = 0, |
| .devs_increment = 1, |
| .ncopies = 1, |
| .nparity = 0, |
| .raid_name = "raid0", |
| .bg_flag = BTRFS_BLOCK_GROUP_RAID0, |
| .mindev_error = 0, |
| }, |
| [BTRFS_RAID_SINGLE] = { |
| .sub_stripes = 1, |
| .dev_stripes = 1, |
| .devs_max = 1, |
| .devs_min = 1, |
| .tolerated_failures = 0, |
| .devs_increment = 1, |
| .ncopies = 1, |
| .nparity = 0, |
| .raid_name = "single", |
| .bg_flag = 0, |
| .mindev_error = 0, |
| }, |
| [BTRFS_RAID_RAID5] = { |
| .sub_stripes = 1, |
| .dev_stripes = 1, |
| .devs_max = 0, |
| .devs_min = 2, |
| .tolerated_failures = 1, |
| .devs_increment = 1, |
| .ncopies = 1, |
| .nparity = 1, |
| .raid_name = "raid5", |
| .bg_flag = BTRFS_BLOCK_GROUP_RAID5, |
| .mindev_error = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET, |
| }, |
| [BTRFS_RAID_RAID6] = { |
| .sub_stripes = 1, |
| .dev_stripes = 1, |
| .devs_max = 0, |
| .devs_min = 3, |
| .tolerated_failures = 2, |
| .devs_increment = 1, |
| .ncopies = 1, |
| .nparity = 2, |
| .raid_name = "raid6", |
| .bg_flag = BTRFS_BLOCK_GROUP_RAID6, |
| .mindev_error = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET, |
| }, |
| }; |
| |
| /* |
| * Convert block group flags (BTRFS_BLOCK_GROUP_*) to btrfs_raid_types, which |
| * can be used as index to access btrfs_raid_array[]. |
| */ |
| enum btrfs_raid_types __attribute_const__ btrfs_bg_flags_to_raid_index(u64 flags) |
| { |
| if (flags & BTRFS_BLOCK_GROUP_RAID10) |
| return BTRFS_RAID_RAID10; |
| else if (flags & BTRFS_BLOCK_GROUP_RAID1) |
| return BTRFS_RAID_RAID1; |
| else if (flags & BTRFS_BLOCK_GROUP_RAID1C3) |
| return BTRFS_RAID_RAID1C3; |
| else if (flags & BTRFS_BLOCK_GROUP_RAID1C4) |
| return BTRFS_RAID_RAID1C4; |
| else if (flags & BTRFS_BLOCK_GROUP_DUP) |
| return BTRFS_RAID_DUP; |
| else if (flags & BTRFS_BLOCK_GROUP_RAID0) |
| return BTRFS_RAID_RAID0; |
| else if (flags & BTRFS_BLOCK_GROUP_RAID5) |
| return BTRFS_RAID_RAID5; |
| else if (flags & BTRFS_BLOCK_GROUP_RAID6) |
| return BTRFS_RAID_RAID6; |
| |
| return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */ |
| } |
| |
| const char *btrfs_bg_type_to_raid_name(u64 flags) |
| { |
| const int index = btrfs_bg_flags_to_raid_index(flags); |
| |
| if (index >= BTRFS_NR_RAID_TYPES) |
| return NULL; |
| |
| return btrfs_raid_array[index].raid_name; |
| } |
| |
| /* |
| * Fill @buf with textual description of @bg_flags, no more than @size_buf |
| * bytes including terminating null byte. |
| */ |
| void btrfs_describe_block_groups(u64 bg_flags, char *buf, u32 size_buf) |
| { |
| int i; |
| int ret; |
| char *bp = buf; |
| u64 flags = bg_flags; |
| u32 size_bp = size_buf; |
| |
| if (!flags) { |
| strcpy(bp, "NONE"); |
| return; |
| } |
| |
| #define DESCRIBE_FLAG(flag, desc) \ |
| do { \ |
| if (flags & (flag)) { \ |
| ret = snprintf(bp, size_bp, "%s|", (desc)); \ |
| if (ret < 0 || ret >= size_bp) \ |
| goto out_overflow; \ |
| size_bp -= ret; \ |
| bp += ret; \ |
| flags &= ~(flag); \ |
| } \ |
| } while (0) |
| |
| DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_DATA, "data"); |
| DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_SYSTEM, "system"); |
| DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_METADATA, "metadata"); |
| |
| DESCRIBE_FLAG(BTRFS_AVAIL_ALLOC_BIT_SINGLE, "single"); |
| for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
| DESCRIBE_FLAG(btrfs_raid_array[i].bg_flag, |
| btrfs_raid_array[i].raid_name); |
| #undef DESCRIBE_FLAG |
| |
| if (flags) { |
| ret = snprintf(bp, size_bp, "0x%llx|", flags); |
| size_bp -= ret; |
| } |
| |
| if (size_bp < size_buf) |
| buf[size_buf - size_bp - 1] = '\0'; /* remove last | */ |
| |
| /* |
| * The text is trimmed, it's up to the caller to provide sufficiently |
| * large buffer |
| */ |
| out_overflow:; |
| } |
| |
| static int init_first_rw_device(struct btrfs_trans_handle *trans); |
| static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info); |
| static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev); |
| static void btrfs_dev_stat_print_on_load(struct btrfs_device *device); |
| static int __btrfs_map_block(struct btrfs_fs_info *fs_info, |
| enum btrfs_map_op op, |
| u64 logical, u64 *length, |
| struct btrfs_io_context **bioc_ret, |
| int mirror_num, int need_raid_map); |
| |
| /* |
| * Device locking |
| * ============== |
| * |
| * There are several mutexes that protect manipulation of devices and low-level |
| * structures like chunks but not block groups, extents or files |
| * |
| * uuid_mutex (global lock) |
| * ------------------------ |
| * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from |
| * the SCAN_DEV ioctl registration or from mount either implicitly (the first |
| * device) or requested by the device= mount option |
| * |
| * the mutex can be very coarse and can cover long-running operations |
| * |
| * protects: updates to fs_devices counters like missing devices, rw devices, |
| * seeding, structure cloning, opening/closing devices at mount/umount time |
| * |
| * global::fs_devs - add, remove, updates to the global list |
| * |
| * does not protect: manipulation of the fs_devices::devices list in general |
| * but in mount context it could be used to exclude list modifications by eg. |
| * scan ioctl |
| * |
| * btrfs_device::name - renames (write side), read is RCU |
| * |
| * fs_devices::device_list_mutex (per-fs, with RCU) |
| * ------------------------------------------------ |
| * protects updates to fs_devices::devices, ie. adding and deleting |
| * |
| * simple list traversal with read-only actions can be done with RCU protection |
| * |
| * may be used to exclude some operations from running concurrently without any |
| * modifications to the list (see write_all_supers) |
| * |
| * Is not required at mount and close times, because our device list is |
| * protected by the uuid_mutex at that point. |
| * |
| * balance_mutex |
| * ------------- |
| * protects balance structures (status, state) and context accessed from |
| * several places (internally, ioctl) |
| * |
| * chunk_mutex |
| * ----------- |
| * protects chunks, adding or removing during allocation, trim or when a new |
| * device is added/removed. Additionally it also protects post_commit_list of |
| * individual devices, since they can be added to the transaction's |
| * post_commit_list only with chunk_mutex held. |
| * |
| * cleaner_mutex |
| * ------------- |
| * a big lock that is held by the cleaner thread and prevents running subvolume |
| * cleaning together with relocation or delayed iputs |
| * |
| * |
| * Lock nesting |
| * ============ |
| * |
| * uuid_mutex |
| * device_list_mutex |
| * chunk_mutex |
| * balance_mutex |
| * |
| * |
| * Exclusive operations |
| * ==================== |
| * |
| * Maintains the exclusivity of the following operations that apply to the |
| * whole filesystem and cannot run in parallel. |
| * |
| * - Balance (*) |
| * - Device add |
| * - Device remove |
| * - Device replace (*) |
| * - Resize |
| * |
| * The device operations (as above) can be in one of the following states: |
| * |
| * - Running state |
| * - Paused state |
| * - Completed state |
| * |
| * Only device operations marked with (*) can go into the Paused state for the |
| * following reasons: |
| * |
| * - ioctl (only Balance can be Paused through ioctl) |
| * - filesystem remounted as read-only |
| * - filesystem unmounted and mounted as read-only |
| * - system power-cycle and filesystem mounted as read-only |
| * - filesystem or device errors leading to forced read-only |
| * |
| * The status of exclusive operation is set and cleared atomically. |
| * During the course of Paused state, fs_info::exclusive_operation remains set. |
| * A device operation in Paused or Running state can be canceled or resumed |
| * either by ioctl (Balance only) or when remounted as read-write. |
| * The exclusive status is cleared when the device operation is canceled or |
| * completed. |
| */ |
| |
| DEFINE_MUTEX(uuid_mutex); |
| static LIST_HEAD(fs_uuids); |
| struct list_head * __attribute_const__ btrfs_get_fs_uuids(void) |
| { |
| return &fs_uuids; |
| } |
| |
| /* |
| * alloc_fs_devices - allocate struct btrfs_fs_devices |
| * @fsid: if not NULL, copy the UUID to fs_devices::fsid |
| * @metadata_fsid: if not NULL, copy the UUID to fs_devices::metadata_fsid |
| * |
| * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR(). |
| * The returned struct is not linked onto any lists and can be destroyed with |
| * kfree() right away. |
| */ |
| static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid, |
| const u8 *metadata_fsid) |
| { |
| struct btrfs_fs_devices *fs_devs; |
| |
| fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL); |
| if (!fs_devs) |
| return ERR_PTR(-ENOMEM); |
| |
| mutex_init(&fs_devs->device_list_mutex); |
| |
| INIT_LIST_HEAD(&fs_devs->devices); |
| INIT_LIST_HEAD(&fs_devs->alloc_list); |
| INIT_LIST_HEAD(&fs_devs->fs_list); |
| INIT_LIST_HEAD(&fs_devs->seed_list); |
| if (fsid) |
| memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE); |
| |
| if (metadata_fsid) |
| memcpy(fs_devs->metadata_uuid, metadata_fsid, BTRFS_FSID_SIZE); |
| else if (fsid) |
| memcpy(fs_devs->metadata_uuid, fsid, BTRFS_FSID_SIZE); |
| |
| return fs_devs; |
| } |
| |
| void btrfs_free_device(struct btrfs_device *device) |
| { |
| WARN_ON(!list_empty(&device->post_commit_list)); |
| rcu_string_free(device->name); |
| extent_io_tree_release(&device->alloc_state); |
| bio_put(device->flush_bio); |
| btrfs_destroy_dev_zone_info(device); |
| kfree(device); |
| } |
| |
| static void free_fs_devices(struct btrfs_fs_devices *fs_devices) |
| { |
| struct btrfs_device *device; |
| WARN_ON(fs_devices->opened); |
| while (!list_empty(&fs_devices->devices)) { |
| device = list_entry(fs_devices->devices.next, |
| struct btrfs_device, dev_list); |
| list_del(&device->dev_list); |
| btrfs_free_device(device); |
| } |
| kfree(fs_devices); |
| } |
| |
| void __exit btrfs_cleanup_fs_uuids(void) |
| { |
| struct btrfs_fs_devices *fs_devices; |
| |
| while (!list_empty(&fs_uuids)) { |
| fs_devices = list_entry(fs_uuids.next, |
| struct btrfs_fs_devices, fs_list); |
| list_del(&fs_devices->fs_list); |
| free_fs_devices(fs_devices); |
| } |
| } |
| |
| static noinline struct btrfs_fs_devices *find_fsid( |
| const u8 *fsid, const u8 *metadata_fsid) |
| { |
| struct btrfs_fs_devices *fs_devices; |
| |
| ASSERT(fsid); |
| |
| /* Handle non-split brain cases */ |
| list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| if (metadata_fsid) { |
| if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0 |
| && memcmp(metadata_fsid, fs_devices->metadata_uuid, |
| BTRFS_FSID_SIZE) == 0) |
| return fs_devices; |
| } else { |
| if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0) |
| return fs_devices; |
| } |
| } |
| return NULL; |
| } |
| |
| static struct btrfs_fs_devices *find_fsid_with_metadata_uuid( |
| struct btrfs_super_block *disk_super) |
| { |
| |
| struct btrfs_fs_devices *fs_devices; |
| |
| /* |
| * Handle scanned device having completed its fsid change but |
| * belonging to a fs_devices that was created by first scanning |
| * a device which didn't have its fsid/metadata_uuid changed |
| * at all and the CHANGING_FSID_V2 flag set. |
| */ |
| list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| if (fs_devices->fsid_change && |
| memcmp(disk_super->metadata_uuid, fs_devices->fsid, |
| BTRFS_FSID_SIZE) == 0 && |
| memcmp(fs_devices->fsid, fs_devices->metadata_uuid, |
| BTRFS_FSID_SIZE) == 0) { |
| return fs_devices; |
| } |
| } |
| /* |
| * Handle scanned device having completed its fsid change but |
| * belonging to a fs_devices that was created by a device that |
| * has an outdated pair of fsid/metadata_uuid and |
| * CHANGING_FSID_V2 flag set. |
| */ |
| list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| if (fs_devices->fsid_change && |
| memcmp(fs_devices->metadata_uuid, |
| fs_devices->fsid, BTRFS_FSID_SIZE) != 0 && |
| memcmp(disk_super->metadata_uuid, fs_devices->metadata_uuid, |
| BTRFS_FSID_SIZE) == 0) { |
| return fs_devices; |
| } |
| } |
| |
| return find_fsid(disk_super->fsid, disk_super->metadata_uuid); |
| } |
| |
| |
| static int |
| btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder, |
| int flush, struct block_device **bdev, |
| struct btrfs_super_block **disk_super) |
| { |
| int ret; |
| |
| *bdev = blkdev_get_by_path(device_path, flags, holder); |
| |
| if (IS_ERR(*bdev)) { |
| ret = PTR_ERR(*bdev); |
| goto error; |
| } |
| |
| if (flush) |
| sync_blockdev(*bdev); |
| ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE); |
| if (ret) { |
| blkdev_put(*bdev, flags); |
| goto error; |
| } |
| invalidate_bdev(*bdev); |
| *disk_super = btrfs_read_dev_super(*bdev); |
| if (IS_ERR(*disk_super)) { |
| ret = PTR_ERR(*disk_super); |
| blkdev_put(*bdev, flags); |
| goto error; |
| } |
| |
| return 0; |
| |
| error: |
| *bdev = NULL; |
| return ret; |
| } |
| |
| static bool device_path_matched(const char *path, struct btrfs_device *device) |
| { |
| int found; |
| |
| rcu_read_lock(); |
| found = strcmp(rcu_str_deref(device->name), path); |
| rcu_read_unlock(); |
| |
| return found == 0; |
| } |
| |
| /* |
| * Search and remove all stale (devices which are not mounted) devices. |
| * When both inputs are NULL, it will search and release all stale devices. |
| * path: Optional. When provided will it release all unmounted devices |
| * matching this path only. |
| * skip_dev: Optional. Will skip this device when searching for the stale |
| * devices. |
| * Return: 0 for success or if @path is NULL. |
| * -EBUSY if @path is a mounted device. |
| * -ENOENT if @path does not match any device in the list. |
| */ |
| static int btrfs_free_stale_devices(const char *path, |
| struct btrfs_device *skip_device) |
| { |
| struct btrfs_fs_devices *fs_devices, *tmp_fs_devices; |
| struct btrfs_device *device, *tmp_device; |
| int ret = 0; |
| |
| lockdep_assert_held(&uuid_mutex); |
| |
| if (path) |
| ret = -ENOENT; |
| |
| list_for_each_entry_safe(fs_devices, tmp_fs_devices, &fs_uuids, fs_list) { |
| |
| mutex_lock(&fs_devices->device_list_mutex); |
| list_for_each_entry_safe(device, tmp_device, |
| &fs_devices->devices, dev_list) { |
| if (skip_device && skip_device == device) |
| continue; |
| if (path && !device->name) |
| continue; |
| if (path && !device_path_matched(path, device)) |
| continue; |
| if (fs_devices->opened) { |
| /* for an already deleted device return 0 */ |
| if (path && ret != 0) |
| ret = -EBUSY; |
| break; |
| } |
| |
| /* delete the stale device */ |
| fs_devices->num_devices--; |
| list_del(&device->dev_list); |
| btrfs_free_device(device); |
| |
| ret = 0; |
| } |
| mutex_unlock(&fs_devices->device_list_mutex); |
| |
| if (fs_devices->num_devices == 0) { |
| btrfs_sysfs_remove_fsid(fs_devices); |
| list_del(&fs_devices->fs_list); |
| free_fs_devices(fs_devices); |
| } |
| } |
| |
| return ret; |
| } |
| |
| /* |
| * This is only used on mount, and we are protected from competing things |
| * messing with our fs_devices by the uuid_mutex, thus we do not need the |
| * fs_devices->device_list_mutex here. |
| */ |
| static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices, |
| struct btrfs_device *device, fmode_t flags, |
| void *holder) |
| { |
| struct request_queue *q; |
| struct block_device *bdev; |
| struct btrfs_super_block *disk_super; |
| u64 devid; |
| int ret; |
| |
| if (device->bdev) |
| return -EINVAL; |
| if (!device->name) |
| return -EINVAL; |
| |
| ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1, |
| &bdev, &disk_super); |
| if (ret) |
| return ret; |
| |
| devid = btrfs_stack_device_id(&disk_super->dev_item); |
| if (devid != device->devid) |
| goto error_free_page; |
| |
| if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE)) |
| goto error_free_page; |
| |
| device->generation = btrfs_super_generation(disk_super); |
| |
| if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) { |
| if (btrfs_super_incompat_flags(disk_super) & |
| BTRFS_FEATURE_INCOMPAT_METADATA_UUID) { |
| pr_err( |
| "BTRFS: Invalid seeding and uuid-changed device detected\n"); |
| goto error_free_page; |
| } |
| |
| clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
| fs_devices->seeding = true; |
| } else { |
| if (bdev_read_only(bdev)) |
| clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
| else |
| set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
| } |
| |
| q = bdev_get_queue(bdev); |
| if (!blk_queue_nonrot(q)) |
| fs_devices->rotating = true; |
| |
| device->bdev = bdev; |
| clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
| device->mode = flags; |
| |
| fs_devices->open_devices++; |
| if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
| device->devid != BTRFS_DEV_REPLACE_DEVID) { |
| fs_devices->rw_devices++; |
| list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list); |
| } |
| btrfs_release_disk_super(disk_super); |
| |
| return 0; |
| |
| error_free_page: |
| btrfs_release_disk_super(disk_super); |
| blkdev_put(bdev, flags); |
| |
| return -EINVAL; |
| } |
| |
| /* |
| * Handle scanned device having its CHANGING_FSID_V2 flag set and the fs_devices |
| * being created with a disk that has already completed its fsid change. Such |
| * disk can belong to an fs which has its FSID changed or to one which doesn't. |
| * Handle both cases here. |
| */ |
| static struct btrfs_fs_devices *find_fsid_inprogress( |
| struct btrfs_super_block *disk_super) |
| { |
| struct btrfs_fs_devices *fs_devices; |
| |
| list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid, |
| BTRFS_FSID_SIZE) != 0 && |
| memcmp(fs_devices->metadata_uuid, disk_super->fsid, |
| BTRFS_FSID_SIZE) == 0 && !fs_devices->fsid_change) { |
| return fs_devices; |
| } |
| } |
| |
| return find_fsid(disk_super->fsid, NULL); |
| } |
| |
| |
| static struct btrfs_fs_devices *find_fsid_changed( |
| struct btrfs_super_block *disk_super) |
| { |
| struct btrfs_fs_devices *fs_devices; |
| |
| /* |
| * Handles the case where scanned device is part of an fs that had |
| * multiple successful changes of FSID but currently device didn't |
| * observe it. Meaning our fsid will be different than theirs. We need |
| * to handle two subcases : |
| * 1 - The fs still continues to have different METADATA/FSID uuids. |
| * 2 - The fs is switched back to its original FSID (METADATA/FSID |
| * are equal). |
| */ |
| list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| /* Changed UUIDs */ |
| if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid, |
| BTRFS_FSID_SIZE) != 0 && |
| memcmp(fs_devices->metadata_uuid, disk_super->metadata_uuid, |
| BTRFS_FSID_SIZE) == 0 && |
| memcmp(fs_devices->fsid, disk_super->fsid, |
| BTRFS_FSID_SIZE) != 0) |
| return fs_devices; |
| |
| /* Unchanged UUIDs */ |
| if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid, |
| BTRFS_FSID_SIZE) == 0 && |
| memcmp(fs_devices->fsid, disk_super->metadata_uuid, |
| BTRFS_FSID_SIZE) == 0) |
| return fs_devices; |
| } |
| |
| return NULL; |
| } |
| |
| static struct btrfs_fs_devices *find_fsid_reverted_metadata( |
| struct btrfs_super_block *disk_super) |
| { |
| struct btrfs_fs_devices *fs_devices; |
| |
| /* |
| * Handle the case where the scanned device is part of an fs whose last |
| * metadata UUID change reverted it to the original FSID. At the same |
| * time * fs_devices was first created by another constitutent device |
| * which didn't fully observe the operation. This results in an |
| * btrfs_fs_devices created with metadata/fsid different AND |
| * btrfs_fs_devices::fsid_change set AND the metadata_uuid of the |
| * fs_devices equal to the FSID of the disk. |
| */ |
| list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| if (memcmp(fs_devices->fsid, fs_devices->metadata_uuid, |
| BTRFS_FSID_SIZE) != 0 && |
| memcmp(fs_devices->metadata_uuid, disk_super->fsid, |
| BTRFS_FSID_SIZE) == 0 && |
| fs_devices->fsid_change) |
| return fs_devices; |
| } |
| |
| return NULL; |
| } |
| /* |
| * Add new device to list of registered devices |
| * |
| * Returns: |
| * device pointer which was just added or updated when successful |
| * error pointer when failed |
| */ |
| static noinline struct btrfs_device *device_list_add(const char *path, |
| struct btrfs_super_block *disk_super, |
| bool *new_device_added) |
| { |
| struct btrfs_device *device; |
| struct btrfs_fs_devices *fs_devices = NULL; |
| struct rcu_string *name; |
| u64 found_transid = btrfs_super_generation(disk_super); |
| u64 devid = btrfs_stack_device_id(&disk_super->dev_item); |
| bool has_metadata_uuid = (btrfs_super_incompat_flags(disk_super) & |
| BTRFS_FEATURE_INCOMPAT_METADATA_UUID); |
| bool fsid_change_in_progress = (btrfs_super_flags(disk_super) & |
| BTRFS_SUPER_FLAG_CHANGING_FSID_V2); |
| |
| if (fsid_change_in_progress) { |
| if (!has_metadata_uuid) |
| fs_devices = find_fsid_inprogress(disk_super); |
| else |
| fs_devices = find_fsid_changed(disk_super); |
| } else if (has_metadata_uuid) { |
| fs_devices = find_fsid_with_metadata_uuid(disk_super); |
| } else { |
| fs_devices = find_fsid_reverted_metadata(disk_super); |
| if (!fs_devices) |
| fs_devices = find_fsid(disk_super->fsid, NULL); |
| } |
| |
| |
| if (!fs_devices) { |
| if (has_metadata_uuid) |
| fs_devices = alloc_fs_devices(disk_super->fsid, |
| disk_super->metadata_uuid); |
| else |
| fs_devices = alloc_fs_devices(disk_super->fsid, NULL); |
| |
| if (IS_ERR(fs_devices)) |
| return ERR_CAST(fs_devices); |
| |
| fs_devices->fsid_change = fsid_change_in_progress; |
| |
| mutex_lock(&fs_devices->device_list_mutex); |
| list_add(&fs_devices->fs_list, &fs_uuids); |
| |
| device = NULL; |
| } else { |
| struct btrfs_dev_lookup_args args = { |
| .devid = devid, |
| .uuid = disk_super->dev_item.uuid, |
| }; |
| |
| mutex_lock(&fs_devices->device_list_mutex); |
| device = btrfs_find_device(fs_devices, &args); |
| |
| /* |
| * If this disk has been pulled into an fs devices created by |
| * a device which had the CHANGING_FSID_V2 flag then replace the |
| * metadata_uuid/fsid values of the fs_devices. |
| */ |
| if (fs_devices->fsid_change && |
| found_transid > fs_devices->latest_generation) { |
| memcpy(fs_devices->fsid, disk_super->fsid, |
| BTRFS_FSID_SIZE); |
| |
| if (has_metadata_uuid) |
| memcpy(fs_devices->metadata_uuid, |
| disk_super->metadata_uuid, |
| BTRFS_FSID_SIZE); |
| else |
| memcpy(fs_devices->metadata_uuid, |
| disk_super->fsid, BTRFS_FSID_SIZE); |
| |
| fs_devices->fsid_change = false; |
| } |
| } |
| |
| if (!device) { |
| if (fs_devices->opened) { |
| mutex_unlock(&fs_devices->device_list_mutex); |
| return ERR_PTR(-EBUSY); |
| } |
| |
| device = btrfs_alloc_device(NULL, &devid, |
| disk_super->dev_item.uuid); |
| if (IS_ERR(device)) { |
| mutex_unlock(&fs_devices->device_list_mutex); |
| /* we can safely leave the fs_devices entry around */ |
| return device; |
| } |
| |
| name = rcu_string_strdup(path, GFP_NOFS); |
| if (!name) { |
| btrfs_free_device(device); |
| mutex_unlock(&fs_devices->device_list_mutex); |
| return ERR_PTR(-ENOMEM); |
| } |
| rcu_assign_pointer(device->name, name); |
| |
| list_add_rcu(&device->dev_list, &fs_devices->devices); |
| fs_devices->num_devices++; |
| |
| device->fs_devices = fs_devices; |
| *new_device_added = true; |
| |
| if (disk_super->label[0]) |
| pr_info( |
| "BTRFS: device label %s devid %llu transid %llu %s scanned by %s (%d)\n", |
| disk_super->label, devid, found_transid, path, |
| current->comm, task_pid_nr(current)); |
| else |
| pr_info( |
| "BTRFS: device fsid %pU devid %llu transid %llu %s scanned by %s (%d)\n", |
| disk_super->fsid, devid, found_transid, path, |
| current->comm, task_pid_nr(current)); |
| |
| } else if (!device->name || strcmp(device->name->str, path)) { |
| /* |
| * When FS is already mounted. |
| * 1. If you are here and if the device->name is NULL that |
| * means this device was missing at time of FS mount. |
| * 2. If you are here and if the device->name is different |
| * from 'path' that means either |
| * a. The same device disappeared and reappeared with |
| * different name. or |
| * b. The missing-disk-which-was-replaced, has |
| * reappeared now. |
| * |
| * We must allow 1 and 2a above. But 2b would be a spurious |
| * and unintentional. |
| * |
| * Further in case of 1 and 2a above, the disk at 'path' |
| * would have missed some transaction when it was away and |
| * in case of 2a the stale bdev has to be updated as well. |
| * 2b must not be allowed at all time. |
| */ |
| |
| /* |
| * For now, we do allow update to btrfs_fs_device through the |
| * btrfs dev scan cli after FS has been mounted. We're still |
| * tracking a problem where systems fail mount by subvolume id |
| * when we reject replacement on a mounted FS. |
| */ |
| if (!fs_devices->opened && found_transid < device->generation) { |
| /* |
| * That is if the FS is _not_ mounted and if you |
| * are here, that means there is more than one |
| * disk with same uuid and devid.We keep the one |
| * with larger generation number or the last-in if |
| * generation are equal. |
| */ |
| mutex_unlock(&fs_devices->device_list_mutex); |
| return ERR_PTR(-EEXIST); |
| } |
| |
| /* |
| * We are going to replace the device path for a given devid, |
| * make sure it's the same device if the device is mounted |
| */ |
| if (device->bdev) { |
| int error; |
| dev_t path_dev; |
| |
| error = lookup_bdev(path, &path_dev); |
| if (error) { |
| mutex_unlock(&fs_devices->device_list_mutex); |
| return ERR_PTR(error); |
| } |
| |
| if (device->bdev->bd_dev != path_dev) { |
| mutex_unlock(&fs_devices->device_list_mutex); |
| /* |
| * device->fs_info may not be reliable here, so |
| * pass in a NULL instead. This avoids a |
| * possible use-after-free when the fs_info and |
| * fs_info->sb are already torn down. |
| */ |
| btrfs_warn_in_rcu(NULL, |
| "duplicate device %s devid %llu generation %llu scanned by %s (%d)", |
| path, devid, found_transid, |
| current->comm, |
| task_pid_nr(current)); |
| return ERR_PTR(-EEXIST); |
| } |
| btrfs_info_in_rcu(device->fs_info, |
| "devid %llu device path %s changed to %s scanned by %s (%d)", |
| devid, rcu_str_deref(device->name), |
| path, current->comm, |
| task_pid_nr(current)); |
| } |
| |
| name = rcu_string_strdup(path, GFP_NOFS); |
| if (!name) { |
| mutex_unlock(&fs_devices->device_list_mutex); |
| return ERR_PTR(-ENOMEM); |
| } |
| rcu_string_free(device->name); |
| rcu_assign_pointer(device->name, name); |
| if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) { |
| fs_devices->missing_devices--; |
| clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
| } |
| } |
| |
| /* |
| * Unmount does not free the btrfs_device struct but would zero |
| * generation along with most of the other members. So just update |
| * it back. We need it to pick the disk with largest generation |
| * (as above). |
| */ |
| if (!fs_devices->opened) { |
| device->generation = found_transid; |
| fs_devices->latest_generation = max_t(u64, found_transid, |
| fs_devices->latest_generation); |
| } |
| |
| fs_devices->total_devices = btrfs_super_num_devices(disk_super); |
| |
| mutex_unlock(&fs_devices->device_list_mutex); |
| return device; |
| } |
| |
| static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig) |
| { |
| struct btrfs_fs_devices *fs_devices; |
| struct btrfs_device *device; |
| struct btrfs_device *orig_dev; |
| int ret = 0; |
| |
| lockdep_assert_held(&uuid_mutex); |
| |
| fs_devices = alloc_fs_devices(orig->fsid, NULL); |
| if (IS_ERR(fs_devices)) |
| return fs_devices; |
| |
| fs_devices->total_devices = orig->total_devices; |
| |
| list_for_each_entry(orig_dev, &orig->devices, dev_list) { |
| struct rcu_string *name; |
| |
| device = btrfs_alloc_device(NULL, &orig_dev->devid, |
| orig_dev->uuid); |
| if (IS_ERR(device)) { |
| ret = PTR_ERR(device); |
| goto error; |
| } |
| |
| /* |
| * This is ok to do without rcu read locked because we hold the |
| * uuid mutex so nothing we touch in here is going to disappear. |
| */ |
| if (orig_dev->name) { |
| name = rcu_string_strdup(orig_dev->name->str, |
| GFP_KERNEL); |
| if (!name) { |
| btrfs_free_device(device); |
| ret = -ENOMEM; |
| goto error; |
| } |
| rcu_assign_pointer(device->name, name); |
| } |
| |
| list_add(&device->dev_list, &fs_devices->devices); |
| device->fs_devices = fs_devices; |
| fs_devices->num_devices++; |
| } |
| return fs_devices; |
| error: |
| free_fs_devices(fs_devices); |
| return ERR_PTR(ret); |
| } |
| |
| static void __btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices, |
| struct btrfs_device **latest_dev) |
| { |
| struct btrfs_device *device, *next; |
| |
| /* This is the initialized path, it is safe to release the devices. */ |
| list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { |
| if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state)) { |
| if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
| &device->dev_state) && |
| !test_bit(BTRFS_DEV_STATE_MISSING, |
| &device->dev_state) && |
| (!*latest_dev || |
| device->generation > (*latest_dev)->generation)) { |
| *latest_dev = device; |
| } |
| continue; |
| } |
| |
| /* |
| * We have already validated the presence of BTRFS_DEV_REPLACE_DEVID, |
| * in btrfs_init_dev_replace() so just continue. |
| */ |
| if (device->devid == BTRFS_DEV_REPLACE_DEVID) |
| continue; |
| |
| if (device->bdev) { |
| blkdev_put(device->bdev, device->mode); |
| device->bdev = NULL; |
| fs_devices->open_devices--; |
| } |
| if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
| list_del_init(&device->dev_alloc_list); |
| clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
| fs_devices->rw_devices--; |
| } |
| list_del_init(&device->dev_list); |
| fs_devices->num_devices--; |
| btrfs_free_device(device); |
| } |
| |
| } |
| |
| /* |
| * After we have read the system tree and know devids belonging to this |
| * filesystem, remove the device which does not belong there. |
| */ |
| void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices) |
| { |
| struct btrfs_device *latest_dev = NULL; |
| struct btrfs_fs_devices *seed_dev; |
| |
| mutex_lock(&uuid_mutex); |
| __btrfs_free_extra_devids(fs_devices, &latest_dev); |
| |
| list_for_each_entry(seed_dev, &fs_devices->seed_list, seed_list) |
| __btrfs_free_extra_devids(seed_dev, &latest_dev); |
| |
| fs_devices->latest_dev = latest_dev; |
| |
| mutex_unlock(&uuid_mutex); |
| } |
| |
| static void btrfs_close_bdev(struct btrfs_device *device) |
| { |
| if (!device->bdev) |
| return; |
| |
| if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
| sync_blockdev(device->bdev); |
| invalidate_bdev(device->bdev); |
| } |
| |
| blkdev_put(device->bdev, device->mode); |
| } |
| |
| static void btrfs_close_one_device(struct btrfs_device *device) |
| { |
| struct btrfs_fs_devices *fs_devices = device->fs_devices; |
| |
| if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
| device->devid != BTRFS_DEV_REPLACE_DEVID) { |
| list_del_init(&device->dev_alloc_list); |
| fs_devices->rw_devices--; |
| } |
| |
| if (device->devid == BTRFS_DEV_REPLACE_DEVID) |
| clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); |
| |
| if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) { |
| clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
| fs_devices->missing_devices--; |
| } |
| |
| btrfs_close_bdev(device); |
| if (device->bdev) { |
| fs_devices->open_devices--; |
| device->bdev = NULL; |
| } |
| clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
| btrfs_destroy_dev_zone_info(device); |
| |
| device->fs_info = NULL; |
| atomic_set(&device->dev_stats_ccnt, 0); |
| extent_io_tree_release(&device->alloc_state); |
| |
| /* |
| * Reset the flush error record. We might have a transient flush error |
| * in this mount, and if so we aborted the current transaction and set |
| * the fs to an error state, guaranteeing no super blocks can be further |
| * committed. However that error might be transient and if we unmount the |
| * filesystem and mount it again, we should allow the mount to succeed |
| * (btrfs_check_rw_degradable() should not fail) - if after mounting the |
| * filesystem again we still get flush errors, then we will again abort |
| * any transaction and set the error state, guaranteeing no commits of |
| * unsafe super blocks. |
| */ |
| device->last_flush_error = 0; |
| |
| /* Verify the device is back in a pristine state */ |
| ASSERT(!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state)); |
| ASSERT(!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)); |
| ASSERT(list_empty(&device->dev_alloc_list)); |
| ASSERT(list_empty(&device->post_commit_list)); |
| ASSERT(atomic_read(&device->reada_in_flight) == 0); |
| } |
| |
| static void close_fs_devices(struct btrfs_fs_devices *fs_devices) |
| { |
| struct btrfs_device *device, *tmp; |
| |
| lockdep_assert_held(&uuid_mutex); |
| |
| if (--fs_devices->opened > 0) |
| return; |
| |
| list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) |
| btrfs_close_one_device(device); |
| |
| WARN_ON(fs_devices->open_devices); |
| WARN_ON(fs_devices->rw_devices); |
| fs_devices->opened = 0; |
| fs_devices->seeding = false; |
| fs_devices->fs_info = NULL; |
| } |
| |
| void btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
| { |
| LIST_HEAD(list); |
| struct btrfs_fs_devices *tmp; |
| |
| mutex_lock(&uuid_mutex); |
| close_fs_devices(fs_devices); |
| if (!fs_devices->opened) |
| list_splice_init(&fs_devices->seed_list, &list); |
| |
| list_for_each_entry_safe(fs_devices, tmp, &list, seed_list) { |
| close_fs_devices(fs_devices); |
| list_del(&fs_devices->seed_list); |
| free_fs_devices(fs_devices); |
| } |
| mutex_unlock(&uuid_mutex); |
| } |
| |
| static int open_fs_devices(struct btrfs_fs_devices *fs_devices, |
| fmode_t flags, void *holder) |
| { |
| struct btrfs_device *device; |
| struct btrfs_device *latest_dev = NULL; |
| struct btrfs_device *tmp_device; |
| |
| flags |= FMODE_EXCL; |
| |
| list_for_each_entry_safe(device, tmp_device, &fs_devices->devices, |
| dev_list) { |
| int ret; |
| |
| ret = btrfs_open_one_device(fs_devices, device, flags, holder); |
| if (ret == 0 && |
| (!latest_dev || device->generation > latest_dev->generation)) { |
| latest_dev = device; |
| } else if (ret == -ENODATA) { |
| fs_devices->num_devices--; |
| list_del(&device->dev_list); |
| btrfs_free_device(device); |
| } |
| } |
| if (fs_devices->open_devices == 0) |
| return -EINVAL; |
| |
| fs_devices->opened = 1; |
| fs_devices->latest_dev = latest_dev; |
| fs_devices->total_rw_bytes = 0; |
| fs_devices->chunk_alloc_policy = BTRFS_CHUNK_ALLOC_REGULAR; |
| fs_devices->read_policy = BTRFS_READ_POLICY_PID; |
| |
| return 0; |
| } |
| |
| static int devid_cmp(void *priv, const struct list_head *a, |
| const struct list_head *b) |
| { |
| const struct btrfs_device *dev1, *dev2; |
| |
| dev1 = list_entry(a, struct btrfs_device, dev_list); |
| dev2 = list_entry(b, struct btrfs_device, dev_list); |
| |
| if (dev1->devid < dev2->devid) |
| return -1; |
| else if (dev1->devid > dev2->devid) |
| return 1; |
| return 0; |
| } |
| |
| int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
| fmode_t flags, void *holder) |
| { |
| int ret; |
| |
| lockdep_assert_held(&uuid_mutex); |
| /* |
| * The device_list_mutex cannot be taken here in case opening the |
| * underlying device takes further locks like open_mutex. |
| * |
| * We also don't need the lock here as this is called during mount and |
| * exclusion is provided by uuid_mutex |
| */ |
| |
| if (fs_devices->opened) { |
| fs_devices->opened++; |
| ret = 0; |
| } else { |
| list_sort(NULL, &fs_devices->devices, devid_cmp); |
| ret = open_fs_devices(fs_devices, flags, holder); |
| } |
| |
| return ret; |
| } |
| |
| void btrfs_release_disk_super(struct btrfs_super_block *super) |
| { |
| struct page *page = virt_to_page(super); |
| |
| put_page(page); |
| } |
| |
| static struct btrfs_super_block *btrfs_read_disk_super(struct block_device *bdev, |
| u64 bytenr, u64 bytenr_orig) |
| { |
| struct btrfs_super_block *disk_super; |
| struct page *page; |
| void *p; |
| pgoff_t index; |
| |
| /* make sure our super fits in the device */ |
| if (bytenr + PAGE_SIZE >= bdev_nr_bytes(bdev)) |
| return ERR_PTR(-EINVAL); |
| |
| /* make sure our super fits in the page */ |
| if (sizeof(*disk_super) > PAGE_SIZE) |
| return ERR_PTR(-EINVAL); |
| |
| /* make sure our super doesn't straddle pages on disk */ |
| index = bytenr >> PAGE_SHIFT; |
| if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_SHIFT != index) |
| return ERR_PTR(-EINVAL); |
| |
| /* pull in the page with our super */ |
| page = read_cache_page_gfp(bdev->bd_inode->i_mapping, index, GFP_KERNEL); |
| |
| if (IS_ERR(page)) |
| return ERR_CAST(page); |
| |
| p = page_address(page); |
| |
| /* align our pointer to the offset of the super block */ |
| disk_super = p + offset_in_page(bytenr); |
| |
| if (btrfs_super_bytenr(disk_super) != bytenr_orig || |
| btrfs_super_magic(disk_super) != BTRFS_MAGIC) { |
| btrfs_release_disk_super(p); |
| return ERR_PTR(-EINVAL); |
| } |
| |
| if (disk_super->label[0] && disk_super->label[BTRFS_LABEL_SIZE - 1]) |
| disk_super->label[BTRFS_LABEL_SIZE - 1] = 0; |
| |
| return disk_super; |
| } |
| |
| int btrfs_forget_devices(const char *path) |
| { |
| int ret; |
| |
| mutex_lock(&uuid_mutex); |
| ret = btrfs_free_stale_devices(strlen(path) ? path : NULL, NULL); |
| mutex_unlock(&uuid_mutex); |
| |
| return ret; |
| } |
| |
| /* |
| * Look for a btrfs signature on a device. This may be called out of the mount path |
| * and we are not allowed to call set_blocksize during the scan. The superblock |
| * is read via pagecache |
| */ |
| struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags, |
| void *holder) |
| { |
| struct btrfs_super_block *disk_super; |
| bool new_device_added = false; |
| struct btrfs_device *device = NULL; |
| struct block_device *bdev; |
| u64 bytenr, bytenr_orig; |
| int ret; |
| |
| lockdep_assert_held(&uuid_mutex); |
| |
| /* |
| * we would like to check all the supers, but that would make |
| * a btrfs mount succeed after a mkfs from a different FS. |
| * So, we need to add a special mount option to scan for |
| * later supers, using BTRFS_SUPER_MIRROR_MAX instead |
| */ |
| flags |= FMODE_EXCL; |
| |
| bdev = blkdev_get_by_path(path, flags, holder); |
| if (IS_ERR(bdev)) |
| return ERR_CAST(bdev); |
| |
| bytenr_orig = btrfs_sb_offset(0); |
| ret = btrfs_sb_log_location_bdev(bdev, 0, READ, &bytenr); |
| if (ret) |
| return ERR_PTR(ret); |
| |
| disk_super = btrfs_read_disk_super(bdev, bytenr, bytenr_orig); |
| if (IS_ERR(disk_super)) { |
| device = ERR_CAST(disk_super); |
| goto error_bdev_put; |
| } |
| |
| device = device_list_add(path, disk_super, &new_device_added); |
| if (!IS_ERR(device)) { |
| if (new_device_added) |
| btrfs_free_stale_devices(path, device); |
| } |
| |
| btrfs_release_disk_super(disk_super); |
| |
| error_bdev_put: |
| blkdev_put(bdev, flags); |
| |
| return device; |
| } |
| |
| /* |
| * Try to find a chunk that intersects [start, start + len] range and when one |
| * such is found, record the end of it in *start |
| */ |
| static bool contains_pending_extent(struct btrfs_device *device, u64 *start, |
| u64 len) |
| { |
| u64 physical_start, physical_end; |
| |
| lockdep_assert_held(&device->fs_info->chunk_mutex); |
| |
| if (!find_first_extent_bit(&device->alloc_state, *start, |
| &physical_start, &physical_end, |
| CHUNK_ALLOCATED, NULL)) { |
| |
| if (in_range(physical_start, *start, len) || |
| in_range(*start, physical_start, |
| physical_end - physical_start)) { |
| *start = physical_end + 1; |
| return true; |
| } |
| } |
| return false; |
| } |
| |
| static u64 dev_extent_search_start(struct btrfs_device *device, u64 start) |
| { |
| switch (device->fs_devices->chunk_alloc_policy) { |
| case BTRFS_CHUNK_ALLOC_REGULAR: |
| /* |
| * We don't want to overwrite the superblock on the drive nor |
| * any area used by the boot loader (grub for example), so we |
| * make sure to start at an offset of at least 1MB. |
| */ |
| return max_t(u64, start, SZ_1M); |
| case BTRFS_CHUNK_ALLOC_ZONED: |
| /* |
| * We don't care about the starting region like regular |
| * allocator, because we anyway use/reserve the first two zones |
| * for superblock logging. |
| */ |
| return ALIGN(start, device->zone_info->zone_size); |
| default: |
| BUG(); |
| } |
| } |
| |
| static bool dev_extent_hole_check_zoned(struct btrfs_device *device, |
| u64 *hole_start, u64 *hole_size, |
| u64 num_bytes) |
| { |
| u64 zone_size = device->zone_info->zone_size; |
| u64 pos; |
| int ret; |
| bool changed = false; |
| |
| ASSERT(IS_ALIGNED(*hole_start, zone_size)); |
| |
| while (*hole_size > 0) { |
| pos = btrfs_find_allocatable_zones(device, *hole_start, |
| *hole_start + *hole_size, |
| num_bytes); |
| if (pos != *hole_start) { |
| *hole_size = *hole_start + *hole_size - pos; |
| *hole_start = pos; |
| changed = true; |
| if (*hole_size < num_bytes) |
| break; |
| } |
| |
| ret = btrfs_ensure_empty_zones(device, pos, num_bytes); |
| |
| /* Range is ensured to be empty */ |
| if (!ret) |
| return changed; |
| |
| /* Given hole range was invalid (outside of device) */ |
| if (ret == -ERANGE) { |
| *hole_start += *hole_size; |
| *hole_size = 0; |
| return true; |
| } |
| |
| *hole_start += zone_size; |
| *hole_size -= zone_size; |
| changed = true; |
| } |
| |
| return changed; |
| } |
| |
| /** |
| * dev_extent_hole_check - check if specified hole is suitable for allocation |
| * @device: the device which we have the hole |
| * @hole_start: starting position of the hole |
| * @hole_size: the size of the hole |
| * @num_bytes: the size of the free space that we need |
| * |
| * This function may modify @hole_start and @hole_size to reflect the suitable |
| * position for allocation. Returns 1 if hole position is updated, 0 otherwise. |
| */ |
| static bool dev_extent_hole_check(struct btrfs_device *device, u64 *hole_start, |
| u64 *hole_size, u64 num_bytes) |
| { |
| bool changed = false; |
| u64 hole_end = *hole_start + *hole_size; |
| |
| for (;;) { |
| /* |
| * Check before we set max_hole_start, otherwise we could end up |
| * sending back this offset anyway. |
| */ |
| if (contains_pending_extent(device, hole_start, *hole_size)) { |
| if (hole_end >= *hole_start) |
| *hole_size = hole_end - *hole_start; |
| else |
| *hole_size = 0; |
| changed = true; |
| } |
| |
| switch (device->fs_devices->chunk_alloc_policy) { |
| case BTRFS_CHUNK_ALLOC_REGULAR: |
| /* No extra check */ |
| break; |
| case BTRFS_CHUNK_ALLOC_ZONED: |
| if (dev_extent_hole_check_zoned(device, hole_start, |
| hole_size, num_bytes)) { |
| changed = true; |
| /* |
| * The changed hole can contain pending extent. |
| * Loop again to check that. |
| */ |
| continue; |
| } |
| break; |
| default: |
| BUG(); |
| } |
| |
| break; |
| } |
| |
| return changed; |
| } |
| |
| /* |
| * find_free_dev_extent_start - find free space in the specified device |
| * @device: the device which we search the free space in |
| * @num_bytes: the size of the free space that we need |
| * @search_start: the position from which to begin the search |
| * @start: store the start of the free space. |
| * @len: the size of the free space. that we find, or the size |
| * of the max free space if we don't find suitable free space |
| * |
| * this uses a pretty simple search, the expectation is that it is |
| * called very infrequently and that a given device has a small number |
| * of extents |
| * |
| * @start is used to store the start of the free space if we find. But if we |
| * don't find suitable free space, it will be used to store the start position |
| * of the max free space. |
| * |
| * @len is used to store the size of the free space that we find. |
| * But if we don't find suitable free space, it is used to store the size of |
| * the max free space. |
| * |
| * NOTE: This function will search *commit* root of device tree, and does extra |
| * check to ensure dev extents are not double allocated. |
| * This makes the function safe to allocate dev extents but may not report |
| * correct usable device space, as device extent freed in current transaction |
| * is not reported as available. |
| */ |
| static int find_free_dev_extent_start(struct btrfs_device *device, |
| u64 num_bytes, u64 search_start, u64 *start, |
| u64 *len) |
| { |
| struct btrfs_fs_info *fs_info = device->fs_info; |
| struct btrfs_root *root = fs_info->dev_root; |
| struct btrfs_key key; |
| struct btrfs_dev_extent *dev_extent; |
| struct btrfs_path *path; |
| u64 hole_size; |
| u64 max_hole_start; |
| u64 max_hole_size; |
| u64 extent_end; |
| u64 search_end = device->total_bytes; |
| int ret; |
| int slot; |
| struct extent_buffer *l; |
| |
| search_start = dev_extent_search_start(device, search_start); |
| |
| WARN_ON(device->zone_info && |
| !IS_ALIGNED(num_bytes, device->zone_info->zone_size)); |
| |
| path = btrfs_alloc_path(); |
| if (!path) |
| return -ENOMEM; |
| |
| max_hole_start = search_start; |
| max_hole_size = 0; |
| |
| again: |
| if (search_start >= search_end || |
| test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
| ret = -ENOSPC; |
| goto out; |
| } |
| |
| path->reada = READA_FORWARD; |
| path->search_commit_root = 1; |
| path->skip_locking = 1; |
| |
| key.objectid = device->devid; |
| key.offset = search_start; |
| key.type = BTRFS_DEV_EXTENT_KEY; |
| |
| ret = btrfs_search_backwards(root, &key, path); |
| if (ret < 0) |
| goto out; |
| |
| while (1) { |
| l = path->nodes[0]; |
| slot = path->slots[0]; |
| if (slot >= btrfs_header_nritems(l)) { |
| ret = btrfs_next_leaf(root, path); |
| if (ret == 0) |
| continue; |
| if (ret < 0) |
| goto out; |
| |
| break; |
| } |
| btrfs_item_key_to_cpu(l, &key, slot); |
| |
| if (key.objectid < device->devid) |
| goto next; |
| |
| if (key.objectid > device->devid) |
| break; |
| |
| if (key.type != BTRFS_DEV_EXTENT_KEY) |
| goto next; |
| |
| if (key.offset > search_start) { |
| hole_size = key.offset - search_start; |
| dev_extent_hole_check(device, &search_start, &hole_size, |
| num_bytes); |
| |
| if (hole_size > max_hole_size) { |
| max_hole_start = search_start; |
| max_hole_size = hole_size; |
| } |
| |
| /* |
| * If this free space is greater than which we need, |
| * it must be the max free space that we have found |
| * until now, so max_hole_start must point to the start |
| * of this free space and the length of this free space |
| * is stored in max_hole_size. Thus, we return |
| * max_hole_start and max_hole_size and go back to the |
| * caller. |
| */ |
| if (hole_size >= num_bytes) { |
| ret = 0; |
| goto out; |
| } |
| } |
| |
| dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
| extent_end = key.offset + btrfs_dev_extent_length(l, |
| dev_extent); |
| if (extent_end > search_start) |
| search_start = extent_end; |
| next: |
| path->slots[0]++; |
| cond_resched(); |
| } |
| |
| /* |
| * At this point, search_start should be the end of |
| * allocated dev extents, and when shrinking the device, |
| * search_end may be smaller than search_start. |
| */ |
| if (search_end > search_start) { |
| hole_size = search_end - search_start; |
| if (dev_extent_hole_check(device, &search_start, &hole_size, |
| num_bytes)) { |
| btrfs_release_path(path); |
| goto again; |
| } |
| |
| if (hole_size > max_hole_size) { |
| max_hole_start = search_start; |
| max_hole_size = hole_size; |
| } |
| } |
| |
| /* See above. */ |
| if (max_hole_size < num_bytes) |
| ret = -ENOSPC; |
| else |
| ret = 0; |
| |
| out: |
| btrfs_free_path(path); |
| *start = max_hole_start; |
| if (len) |
| *len = max_hole_size; |
| return ret; |
| } |
| |
| int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes, |
| u64 *start, u64 *len) |
| { |
| /* FIXME use last free of some kind */ |
| return find_free_dev_extent_start(device, num_bytes, 0, start, len); |
| } |
| |
| static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, |
| struct btrfs_device *device, |
| u64 start, u64 *dev_extent_len) |
| { |
| struct btrfs_fs_info *fs_info = device->fs_info; |
| struct btrfs_root *root = fs_info->dev_root; |
| int ret; |
| struct btrfs_path *path; |
| struct btrfs_key key; |
| struct btrfs_key found_key; |
| struct extent_buffer *leaf = NULL; |
| struct btrfs_dev_extent *extent = NULL; |
| |
| path = btrfs_alloc_path(); |
| if (!path) |
| return -ENOMEM; |
| |
| key.objectid = device->devid; |
| key.offset = start; |
| key.type = BTRFS_DEV_EXTENT_KEY; |
| again: |
| ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| if (ret > 0) { |
| ret = btrfs_previous_item(root, path, key.objectid, |
| BTRFS_DEV_EXTENT_KEY); |
| if (ret) |
| goto out; |
| leaf = path->nodes[0]; |
| btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| extent = btrfs_item_ptr(leaf, path->slots[0], |
| struct btrfs_dev_extent); |
| BUG_ON(found_key.offset > start || found_key.offset + |
| btrfs_dev_extent_length(leaf, extent) < start); |
| key = found_key; |
| btrfs_release_path(path); |
| goto again; |
| } else if (ret == 0) { |
| leaf = path->nodes[0]; |
| extent = btrfs_item_ptr(leaf, path->slots[0], |
| struct btrfs_dev_extent); |
| } else { |
| goto out; |
| } |
| |
| *dev_extent_len = btrfs_dev_extent_length(leaf, extent); |
| |
| ret = btrfs_del_item(trans, root, path); |
| if (ret == 0) |
| set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags); |
| out: |
| btrfs_free_path(path); |
| return ret; |
| } |
| |
| static u64 find_next_chunk(struct btrfs_fs_info *fs_info) |
| { |
| struct extent_map_tree *em_tree; |
| struct extent_map *em; |
| struct rb_node *n; |
| u64 ret = 0; |
| |
| em_tree = &fs_info->mapping_tree; |
| read_lock(&em_tree->lock); |
| n = rb_last(&em_tree->map.rb_root); |
| if (n) { |
| em = rb_entry(n, struct extent_map, rb_node); |
| ret = em->start + em->len; |
| } |
| read_unlock(&em_tree->lock); |
| |
| return ret; |
| } |
| |
| static noinline int find_next_devid(struct btrfs_fs_info *fs_info, |
| u64 *devid_ret) |
| { |
| int ret; |
| struct btrfs_key key; |
| struct btrfs_key found_key; |
| struct btrfs_path *path; |
| |
| path = btrfs_alloc_path(); |
| if (!path) |
| return -ENOMEM; |
| |
| key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| key.type = BTRFS_DEV_ITEM_KEY; |
| key.offset = (u64)-1; |
| |
| ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0); |
| if (ret < 0) |
| goto error; |
| |
| if (ret == 0) { |
| /* Corruption */ |
| btrfs_err(fs_info, "corrupted chunk tree devid -1 matched"); |
| ret = -EUCLEAN; |
| goto error; |
| } |
| |
| ret = btrfs_previous_item(fs_info->chunk_root, path, |
| BTRFS_DEV_ITEMS_OBJECTID, |
| BTRFS_DEV_ITEM_KEY); |
| if (ret) { |
| *devid_ret = 1; |
| } else { |
| btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| path->slots[0]); |
| *devid_ret = found_key.offset + 1; |
| } |
| ret = 0; |
| error: |
| btrfs_free_path(path); |
| return ret; |
| } |
| |
| /* |
| * the device information is stored in the chunk root |
| * the btrfs_device struct should be fully filled in |
| */ |
| static int btrfs_add_dev_item(struct btrfs_trans_handle *trans, |
| struct btrfs_device *device) |
| { |
| int ret; |
| struct btrfs_path *path; |
| struct btrfs_dev_item *dev_item; |
| struct extent_buffer *leaf; |
| struct btrfs_key key; |
| unsigned long ptr; |
| |
| path = btrfs_alloc_path(); |
| if (!path) |
| return -ENOMEM; |
| |
| key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| key.type = BTRFS_DEV_ITEM_KEY; |
| key.offset = device->devid; |
| |
| btrfs_reserve_chunk_metadata(trans, true); |
| ret = btrfs_insert_empty_item(trans, trans->fs_info->chunk_root, path, |
| &key, sizeof(*dev_item)); |
| btrfs_trans_release_chunk_metadata(trans); |
| if (ret) |
| goto out; |
| |
| leaf = path->nodes[0]; |
| dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| |
| btrfs_set_device_id(leaf, dev_item, device->devid); |
| btrfs_set_device_generation(leaf, dev_item, 0); |
| btrfs_set_device_type(leaf, dev_item, device->type); |
| btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
| btrfs_set_device_total_bytes(leaf, dev_item, |
| btrfs_device_get_disk_total_bytes(device)); |
| btrfs_set_device_bytes_used(leaf, dev_item, |
| btrfs_device_get_bytes_used(device)); |
| btrfs_set_device_group(leaf, dev_item, 0); |
| btrfs_set_device_seek_speed(leaf, dev_item, 0); |
| btrfs_set_device_bandwidth(leaf, dev_item, 0); |
| btrfs_set_device_start_offset(leaf, dev_item, 0); |
| |
| ptr = btrfs_device_uuid(dev_item); |
| write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
| ptr = btrfs_device_fsid(dev_item); |
| write_extent_buffer(leaf, trans->fs_info->fs_devices->metadata_uuid, |
| ptr, BTRFS_FSID_SIZE); |
| btrfs_mark_buffer_dirty(leaf); |
| |
| ret = 0; |
| out: |
| btrfs_free_path(path); |
| return ret; |
| } |
| |
| /* |
| * Function to update ctime/mtime for a given device path. |
| * Mainly used for ctime/mtime based probe like libblkid. |
| * |
| * We don't care about errors here, this is just to be kind to userspace. |
| */ |
| static void update_dev_time(const char *device_path) |
| { |
| struct path path; |
| struct timespec64 now; |
| int ret; |
| |
| ret = kern_path(device_path, LOOKUP_FOLLOW, &path); |
| if (ret) |
| return; |
| |
| now = current_time(d_inode(path.dentry)); |
| inode_update_time(d_inode(path.dentry), &now, S_MTIME | S_CTIME); |
| path_put(&path); |
| } |
| |
| static int btrfs_rm_dev_item(struct btrfs_device *device) |
| { |
| struct btrfs_root *root = device->fs_info->chunk_root; |
| int ret; |
| struct btrfs_path *path; |
| struct btrfs_key key; |
| struct btrfs_trans_handle *trans; |
| |
| path = btrfs_alloc_path(); |
| if (!path) |
| return -ENOMEM; |
| |
| trans = btrfs_start_transaction(root, 0); |
| if (IS_ERR(trans)) { |
| btrfs_free_path(path); |
| return PTR_ERR(trans); |
| } |
| key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| key.type = BTRFS_DEV_ITEM_KEY; |
| key.offset = device->devid; |
| |
| btrfs_reserve_chunk_metadata(trans, false); |
| ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| btrfs_trans_release_chunk_metadata(trans); |
| if (ret) { |
| if (ret > 0) |
| ret = -ENOENT; |
| btrfs_abort_transaction(trans, ret); |
| btrfs_end_transaction(trans); |
| goto out; |
| } |
| |
| ret = btrfs_del_item(trans, root, path); |
| if (ret) { |
| btrfs_abort_transaction(trans, ret); |
| btrfs_end_transaction(trans); |
| } |
| |
| out: |
| btrfs_free_path(path); |
| if (!ret) |
| ret = btrfs_commit_transaction(trans); |
| return ret; |
| } |
| |
| /* |
| * Verify that @num_devices satisfies the RAID profile constraints in the whole |
| * filesystem. It's up to the caller to adjust that number regarding eg. device |
| * replace. |
| */ |
| static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info, |
| u64 num_devices) |
| { |
| u64 all_avail; |
| unsigned seq; |
| int i; |
| |
| do { |
| seq = read_seqbegin(&fs_info->profiles_lock); |
| |
| all_avail = fs_info->avail_data_alloc_bits | |
| fs_info->avail_system_alloc_bits | |
| fs_info->avail_metadata_alloc_bits; |
| } while (read_seqretry(&fs_info->profiles_lock, seq)); |
| |
| for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { |
| if (!(all_avail & btrfs_raid_array[i].bg_flag)) |
| continue; |
| |
| if (num_devices < btrfs_raid_array[i].devs_min) |
| return btrfs_raid_array[i].mindev_error; |
| } |
| |
| return 0; |
| } |
| |
| static struct btrfs_device * btrfs_find_next_active_device( |
| struct btrfs_fs_devices *fs_devs, struct btrfs_device *device) |
| { |
| struct btrfs_device *next_device; |
| |
| list_for_each_entry(next_device, &fs_devs->devices, dev_list) { |
| if (next_device != device && |
| !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state) |
| && next_device->bdev) |
| return next_device; |
| } |
| |
| return NULL; |
| } |
| |
| /* |
| * Helper function to check if the given device is part of s_bdev / latest_dev |
| * and replace it with the provided or the next active device, in the context |
| * where this function called, there should be always be another device (or |
| * this_dev) which is active. |
| */ |
| void __cold btrfs_assign_next_active_device(struct btrfs_device *device, |
| struct btrfs_device *next_device) |
| { |
| struct btrfs_fs_info *fs_info = device->fs_info; |
| |
| if (!next_device) |
| next_device = btrfs_find_next_active_device(fs_info->fs_devices, |
| device); |
| ASSERT(next_device); |
| |
| if (fs_info->sb->s_bdev && |
| (fs_info->sb->s_bdev == device->bdev)) |
| fs_info->sb->s_bdev = next_device->bdev; |
| |
| if (fs_info->fs_devices->latest_dev->bdev == device->bdev) |
| fs_info->fs_devices->latest_dev = next_device; |
| } |
| |
| /* |
| * Return btrfs_fs_devices::num_devices excluding the device that's being |
| * currently replaced. |
| */ |
| static u64 btrfs_num_devices(struct btrfs_fs_info *fs_info) |
| { |
| u64 num_devices = fs_info->fs_devices->num_devices; |
| |
| down_read(&fs_info->dev_replace.rwsem); |
| if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) { |
| ASSERT(num_devices > 1); |
| num_devices--; |
| } |
| up_read(&fs_info->dev_replace.rwsem); |
| |
| return num_devices; |
| } |
| |
| void btrfs_scratch_superblocks(struct btrfs_fs_info *fs_info, |
| struct block_device *bdev, |
| const char *device_path) |
| { |
| struct btrfs_super_block *disk_super; |
| int copy_num; |
| |
| if (!bdev) |
| return; |
| |
| for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX; copy_num++) { |
| struct page *page; |
| int ret; |
| |
| disk_super = btrfs_read_dev_one_super(bdev, copy_num); |
| if (IS_ERR(disk_super)) |
| continue; |
| |
| if (bdev_is_zoned(bdev)) { |
| btrfs_reset_sb_log_zones(bdev, copy_num); |
| continue; |
| } |
| |
| memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| |
| page = virt_to_page(disk_super); |
| set_page_dirty(page); |
| lock_page(page); |
| /* write_on_page() unlocks the page */ |
| ret = write_one_page(page); |
| if (ret) |
| btrfs_warn(fs_info, |
| "error clearing superblock number %d (%d)", |
| copy_num, ret); |
| btrfs_release_disk_super(disk_super); |
| |
| } |
| |
| /* Notify udev that device has changed */ |
| btrfs_kobject_uevent(bdev, KOBJ_CHANGE); |
| |
| /* Update ctime/mtime for device path for libblkid */ |
| update_dev_time(device_path); |
| } |
| |
| int btrfs_rm_device(struct btrfs_fs_info *fs_info, |
| struct btrfs_dev_lookup_args *args, |
| struct block_device **bdev, fmode_t *mode) |
| { |
| struct btrfs_device *device; |
| struct btrfs_fs_devices *cur_devices; |
| struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| u64 num_devices; |
| int ret = 0; |
| |
| /* |
| * The device list in fs_devices is accessed without locks (neither |
| * uuid_mutex nor device_list_mutex) as it won't change on a mounted |
| * filesystem and another device rm cannot run. |
| */ |
| num_devices = btrfs_num_devices(fs_info); |
| |
| ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1); |
| if (ret) |
| goto out; |
| |
| device = btrfs_find_device(fs_info->fs_devices, args); |
| if (!device) { |
| if (args->missing) |
| ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND; |
| else |
| ret = -ENOENT; |
| goto out; |
| } |
| |
| if (btrfs_pinned_by_swapfile(fs_info, device)) { |
| btrfs_warn_in_rcu(fs_info, |
| "cannot remove device %s (devid %llu) due to active swapfile", |
| rcu_str_deref(device->name), device->devid); |
| ret = -ETXTBSY; |
| goto out; |
| } |
| |
| if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
| ret = BTRFS_ERROR_DEV_TGT_REPLACE; |
| goto out; |
| } |
| |
| if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
| fs_info->fs_devices->rw_devices == 1) { |
| ret = BTRFS_ERROR_DEV_ONLY_WRITABLE; |
| goto out; |
| } |
| |
| if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
| mutex_lock(&fs_info->chunk_mutex); |
| list_del_init(&device->dev_alloc_list); |
| device->fs_devices->rw_devices--; |
| mutex_unlock(&fs_info->chunk_mutex); |
| } |
| |
| ret = btrfs_shrink_device(device, 0); |
| if (!ret) |
| btrfs_reada_remove_dev(device); |
| if (ret) |
| goto error_undo; |
| |
| /* |
| * TODO: the superblock still includes this device in its num_devices |
| * counter although write_all_supers() is not locked out. This |
| * could give a filesystem state which requires a degraded mount. |
| */ |
| ret = btrfs_rm_dev_item(device); |
| if (ret) |
| goto error_undo; |
| |
| clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
| btrfs_scrub_cancel_dev(device); |
| |
| /* |
| * the device list mutex makes sure that we don't change |
| * the device list while someone else is writing out all |
| * the device supers. Whoever is writing all supers, should |
| * lock the device list mutex before getting the number of |
| * devices in the super block (super_copy). Conversely, |
| * whoever updates the number of devices in the super block |
| * (super_copy) should hold the device list mutex. |
| */ |
| |
| /* |
| * In normal cases the cur_devices == fs_devices. But in case |
| * of deleting a seed device, the cur_devices should point to |
| * its own fs_devices listed under the fs_devices->seed_list. |
| */ |
| cur_devices = device->fs_devices; |
| mutex_lock(&fs_devices->device_list_mutex); |
| list_del_rcu(&device->dev_list); |
| |
| cur_devices->num_devices--; |
| cur_devices->total_devices--; |
| /* Update total_devices of the parent fs_devices if it's seed */ |
| if (cur_devices != fs_devices) |
| fs_devices->total_devices--; |
| |
| if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) |
| cur_devices->missing_devices--; |
| |
| btrfs_assign_next_active_device(device, NULL); |
| |
| if (device->bdev) { |
| cur_devices->open_devices--; |
| /* remove sysfs entry */ |
| btrfs_sysfs_remove_device(device); |
| } |
| |
| num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1; |
| btrfs_set_super_num_devices(fs_info->super_copy, num_devices); |
| mutex_unlock(&fs_devices->device_list_mutex); |
| |
| /* |
| * At this point, the device is zero sized and detached from the |
| * devices list. All that's left is to zero out the old supers and |
| * free the device. |
| * |
| * We cannot call btrfs_close_bdev() here because we're holding the sb |
| * write lock, and blkdev_put() will pull in the ->open_mutex on the |
| * block device and it's dependencies. Instead just flush the device |
| * and let the caller do the final blkdev_put. |
| */ |
| if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
| btrfs_scratch_superblocks(fs_info, device->bdev, |
| device->name->str); |
| if (device->bdev) { |
| sync_blockdev(device->bdev); |
| invalidate_bdev(device->bdev); |
| } |
| } |
| |
| *bdev = device->bdev; |
| *mode = device->mode; |
| synchronize_rcu(); |
| btrfs_free_device(device); |
| |
| /* |
| * This can happen if cur_devices is the private seed devices list. We |
| * cannot call close_fs_devices() here because it expects the uuid_mutex |
| * to be held, but in fact we don't need that for the private |
| * seed_devices, we can simply decrement cur_devices->opened and then |
| * remove it from our list and free the fs_devices. |
| */ |
| if (cur_devices->num_devices == 0) { |
| list_del_init(&cur_devices->seed_list); |
| ASSERT(cur_devices->opened == 1); |
| cur_devices->opened--; |
| free_fs_devices(cur_devices); |
| } |
| |
| out: |
| return ret; |
| |
| error_undo: |
| btrfs_reada_undo_remove_dev(device); |
| if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
| mutex_lock(&fs_info->chunk_mutex); |
| list_add(&device->dev_alloc_list, |
| &fs_devices->alloc_list); |
| device->fs_devices->rw_devices++; |
| mutex_unlock(&fs_info->chunk_mutex); |
| } |
| goto out; |
| } |
| |
| void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev) |
| { |
| struct btrfs_fs_devices *fs_devices; |
| |
| lockdep_assert_held(&srcdev->fs_info->fs_devices->device_list_mutex); |
| |
| /* |
| * in case of fs with no seed, srcdev->fs_devices will point |
| * to fs_devices of fs_info. However when the dev being replaced is |
| * a seed dev it will point to the seed's local fs_devices. In short |
| * srcdev will have its correct fs_devices in both the cases. |
| */ |
| fs_devices = srcdev->fs_devices; |
| |
| list_del_rcu(&srcdev->dev_list); |
| list_del(&srcdev->dev_alloc_list); |
| fs_devices->num_devices--; |
| if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state)) |
| fs_devices->missing_devices--; |
| |
| if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) |
| fs_devices->rw_devices--; |
| |
| if (srcdev->bdev) |
| fs_devices->open_devices--; |
| } |
| |
| void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device *srcdev) |
| { |
| struct btrfs_fs_devices *fs_devices = srcdev->fs_devices; |
| |
| mutex_lock(&uuid_mutex); |
| |
| btrfs_close_bdev(srcdev); |
| synchronize_rcu(); |
| btrfs_free_device(srcdev); |
| |
| /* if this is no devs we rather delete the fs_devices */ |
| if (!fs_devices->num_devices) { |
| /* |
| * On a mounted FS, num_devices can't be zero unless it's a |
| * seed. In case of a seed device being replaced, the replace |
| * target added to the sprout FS, so there will be no more |
| * device left under the seed FS. |
| */ |
| ASSERT(fs_devices->seeding); |
| |
| list_del_init(&fs_devices->seed_list); |
| close_fs_devices(fs_devices); |
| free_fs_devices(fs_devices); |
| } |
| mutex_unlock(&uuid_mutex); |
| } |
| |
| void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev) |
| { |
| struct btrfs_fs_devices *fs_devices = tgtdev->fs_info->fs_devices; |
| |
| mutex_lock(&fs_devices->device_list_mutex); |
| |
| btrfs_sysfs_remove_device(tgtdev); |
| |
| if (tgtdev->bdev) |
| fs_devices->open_devices--; |
| |
| fs_devices->num_devices--; |
| |
| btrfs_assign_next_active_device(tgtdev, NULL); |
| |
| list_del_rcu(&tgtdev->dev_list); |
| |
| mutex_unlock(&fs_devices->device_list_mutex); |
| |
| btrfs_scratch_superblocks(tgtdev->fs_info, tgtdev->bdev, |
| tgtdev->name->str); |
| |
| btrfs_close_bdev(tgtdev); |
| synchronize_rcu(); |
| btrfs_free_device(tgtdev); |
| } |
| |
| /** |
| * Populate args from device at path |
| * |
| * @fs_info: the filesystem |
| * @args: the args to populate |
| * @path: the path to the device |
| * |
| * This will read the super block of the device at @path and populate @args with |
| * the devid, fsid, and uuid. This is meant to be used for ioctls that need to |
| * lookup a device to operate on, but need to do it before we take any locks. |
| * This properly handles the special case of "missing" that a user may pass in, |
| * and does some basic sanity checks. The caller must make sure that @path is |
| * properly NUL terminated before calling in, and must call |
| * btrfs_put_dev_args_from_path() in order to free up the temporary fsid and |
| * uuid buffers. |
| * |
| * Return: 0 for success, -errno for failure |
| */ |
| int btrfs_get_dev_args_from_path(struct btrfs_fs_info *fs_info, |
| struct btrfs_dev_lookup_args *args, |
| const char *path) |
| { |
| struct btrfs_super_block *disk_super; |
| struct block_device *bdev; |
| int ret; |
| |
| if (!path || !path[0]) |
| return -EINVAL; |
| if (!strcmp(path, "missing")) { |
| args->missing = true; |
| return 0; |
| } |
| |
| args->uuid = kzalloc(BTRFS_UUID_SIZE, GFP_KERNEL); |
| args->fsid = kzalloc(BTRFS_FSID_SIZE, GFP_KERNEL); |
| if (!args->uuid || !args->fsid) { |
| btrfs_put_dev_args_from_path(args); |
| return -ENOMEM; |
| } |
| |
| ret = btrfs_get_bdev_and_sb(path, FMODE_READ, fs_info->bdev_holder, 0, |
| &bdev, &disk_super); |
| if (ret) |
| return ret; |
| args->devid = btrfs_stack_device_id(&disk_super->dev_item); |
| memcpy(args->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE); |
| if (btrfs_fs_incompat(fs_info, METADATA_UUID)) |
| memcpy(args->fsid, disk_super->metadata_uuid, BTRFS_FSID_SIZE); |
| else |
| memcpy(args->fsid, disk_super->fsid, BTRFS_FSID_SIZE); |
| btrfs_release_disk_super(disk_super); |
| blkdev_put(bdev, FMODE_READ); |
| return 0; |
| } |
| |
| /* |
| * Only use this jointly with btrfs_get_dev_args_from_path() because we will |
| * allocate our ->uuid and ->fsid pointers, everybody else uses local variables |
| * that don't need to be freed. |
| */ |
| void btrfs_put_dev_args_from_path(struct btrfs_dev_lookup_args *args) |
| { |
| kfree(args->uuid); |
| kfree(args->fsid); |
| args->uuid = NULL; |
| args->fsid = NULL; |
| } |
| |
| struct btrfs_device *btrfs_find_device_by_devspec( |
| struct btrfs_fs_info *fs_info, u64 devid, |
| const char *device_path) |
| { |
| BTRFS_DEV_LOOKUP_ARGS(args); |
| struct btrfs_device *device; |
| int ret; |
| |
| if (devid) { |
| args.devid = devid; |
| device = btrfs_find_device(fs_info->fs_devices, &args); |
| if (!device) |
| return ERR_PTR(-ENOENT); |
| return device; |
| } |
| |
| ret = btrfs_get_dev_args_from_path(fs_info, &args, device_path); |
| if (ret) |
| return ERR_PTR(ret); |
| device = btrfs_find_device(fs_info->fs_devices, &args); |
| btrfs_put_dev_args_from_path(&args); |
| if (!device) |
| return ERR_PTR(-ENOENT); |
| return device; |
| } |
| |
| /* |
| * does all the dirty work required for changing file system's UUID. |
| */ |
| static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info) |
| { |
| struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| struct btrfs_fs_devices *old_devices; |
| struct btrfs_fs_devices *seed_devices; |
| struct btrfs_super_block *disk_super = fs_info->super_copy; |
| struct btrfs_device *device; |
| u64 super_flags; |
| |
| lockdep_assert_held(&uuid_mutex); |
| if (!fs_devices->seeding) |
| return -EINVAL; |
| |
| /* |
| * Private copy of the seed devices, anchored at |
| * fs_info->fs_devices->seed_list |
| */ |
| seed_devices = alloc_fs_devices(NULL, NULL); |
| if (IS_ERR(seed_devices)) |
| return PTR_ERR(seed_devices); |
| |
| /* |
| * It's necessary to retain a copy of the original seed fs_devices in |
| * fs_uuids so that filesystems which have been seeded can successfully |
| * reference the seed device from open_seed_devices. This also supports |
| * multiple fs seed. |
| */ |
| old_devices = clone_fs_devices(fs_devices); |
| if (IS_ERR(old_devices)) { |
| kfree(seed_devices); |
| return PTR_ERR(old_devices); |
| } |
| |
| list_add(&old_devices->fs_list, &fs_uuids); |
| |
| memcpy(seed_devices, fs_devices, sizeof(*seed_devices)); |
| seed_devices->opened = 1; |
| INIT_LIST_HEAD(&seed_devices->devices); |
| INIT_LIST_HEAD(&seed_devices->alloc_list); |
| mutex_init(&seed_devices->device_list_mutex); |
| |
| mutex_lock(&fs_devices->device_list_mutex); |
| list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices, |
| synchronize_rcu); |
| list_for_each_entry(device, &seed_devices->devices, dev_list) |
| device->fs_devices = seed_devices; |
| |
| fs_devices->seeding = false; |
| fs_devices->num_devices = 0; |
| fs_devices->open_devices = 0; |
| fs_devices->missing_devices = 0; |
| fs_devices->rotating = false; |
| list_add(&seed_devices->seed_list, &fs_devices->seed_list); |
| |
| generate_random_uuid(fs_devices->fsid); |
| memcpy(fs_devices->metadata_uuid, fs_devices->fsid, BTRFS_FSID_SIZE); |
| memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
| mutex_unlock(&fs_devices->device_list_mutex); |
| |
| super_flags = btrfs_super_flags(disk_super) & |
| ~BTRFS_SUPER_FLAG_SEEDING; |
| btrfs_set_super_flags(disk_super, super_flags); |
| |
| return 0; |
| } |
| |
| /* |
| * Store the expected generation for seed devices in device items. |
| */ |
| static int btrfs_finish_sprout(struct btrfs_trans_handle *trans) |
| { |
| BTRFS_DEV_LOOKUP_ARGS(args); |
| struct btrfs_fs_info *fs_info = trans->fs_info; |
| struct btrfs_root *root = fs_info->chunk_root; |
| struct btrfs_path *path; |
| struct extent_buffer *leaf; |
| struct btrfs_dev_item *dev_item; |
| struct btrfs_device *device; |
| struct btrfs_key key; |
| u8 fs_uuid[BTRFS_FSID_SIZE]; |
| u8 dev_uuid[BTRFS_UUID_SIZE]; |
| int ret; |
| |
| path = btrfs_alloc_path(); |
| if (!path) |
| return -ENOMEM; |
| |
| key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| key.offset = 0; |
| key.type = BTRFS_DEV_ITEM_KEY; |
| |
| while (1) { |
| btrfs_reserve_chunk_metadata(trans, false); |
| ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| btrfs_trans_release_chunk_metadata(trans); |
| if (ret < 0) |
| goto error; |
| |
| leaf = path->nodes[0]; |
| next_slot: |
| if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| ret = btrfs_next_leaf(root, path); |
| if (ret > 0) |
| break; |
| if (ret < 0) |
| goto error; |
| leaf = path->nodes[0]; |
| btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| btrfs_release_path(path); |
| continue; |
| } |
| |
| btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID || |
| key.type != BTRFS_DEV_ITEM_KEY) |
| break; |
| |
| dev_item = btrfs_item_ptr(leaf, path->slots[0], |
| struct btrfs_dev_item); |
| args.devid = btrfs_device_id(leaf, dev_item); |
| read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
| BTRFS_UUID_SIZE); |
| read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
| BTRFS_FSID_SIZE); |
| args.uuid = dev_uuid; |
| args.fsid = fs_uuid; |
| device = btrfs_find_device(fs_info->fs_devices, &args); |
| BUG_ON(!device); /* Logic error */ |
| |
| if (device->fs_devices->seeding) { |
| btrfs_set_device_generation(leaf, dev_item, |
| device->generation); |
| btrfs_mark_buffer_dirty(leaf); |
| } |
| |
| path->slots[0]++; |
| goto next_slot; |
| } |
| ret = 0; |
| error: |
| btrfs_free_path(path); |
| return ret; |
| } |
| |
| int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path) |
| { |
| struct btrfs_root *root = fs_info->dev_root; |
| struct request_queue *q; |
| struct btrfs_trans_handle *trans; |
| struct btrfs_device *device; |
| struct block_device *bdev; |
| struct super_block *sb = fs_info->sb; |
| struct rcu_string *name; |
| struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| u64 orig_super_total_bytes; |
| u64 orig_super_num_devices; |
| int seeding_dev = 0; |
| int ret = 0; |
| bool locked = false; |
| |
| if (sb_rdonly(sb) && !fs_devices->seeding) |
| return -EROFS; |
| |
| bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
| fs_info->bdev_holder); |
| if (IS_ERR(bdev)) |
| return PTR_ERR(bdev); |
| |
| if (!btrfs_check_device_zone_type(fs_info, bdev)) { |
| ret = -EINVAL; |
| goto error; |
| } |
| |
| if (fs_devices->seeding) { |
| seeding_dev = 1; |
| down_write(&sb->s_umount); |
| mutex_lock(&uuid_mutex); |
| locked = true; |
| } |
| |
| sync_blockdev(bdev); |
| |
| rcu_read_lock(); |
| list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) { |
| if (device->bdev == bdev) { |
| ret = -EEXIST; |
| rcu_read_unlock(); |
| goto error; |
| } |
| } |
| rcu_read_unlock(); |
| |
| device = btrfs_alloc_device(fs_info, NULL, NULL); |
| if (IS_ERR(device)) { |
| /* we can safely leave the fs_devices entry around */ |
| ret = PTR_ERR(device); |
| goto error; |
| } |
| |
| name = rcu_string_strdup(device_path, GFP_KERNEL); |
| if (!name) { |
| ret = -ENOMEM; |
| goto error_free_device; |
| } |
| rcu_assign_pointer(device->name, name); |
| |
| device->fs_info = fs_info; |
| device->bdev = bdev; |
| |
| ret = btrfs_get_dev_zone_info(device); |
| if (ret) |
| goto error_free_device; |
| |
| trans = btrfs_start_transaction(root, 0); |
| if (IS_ERR(trans)) { |
| ret = PTR_ERR(trans); |
| goto error_free_zone; |
| } |
| |
| q = bdev_get_queue(bdev); |
| set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
| device->generation = trans->transid; |
| device->io_width = fs_info->sectorsize; |
| device->io_align = fs_info->sectorsize; |
| device->sector_size = fs_info->sectorsize; |
| device->total_bytes = |
| round_down(bdev_nr_bytes(bdev), fs_info->sectorsize); |
| device->disk_total_bytes = device->total_bytes; |
| device->commit_total_bytes = device->total_bytes; |
| set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
| clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); |
| device->mode = FMODE_EXCL; |
| device->dev_stats_valid = 1; |
| set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE); |
| |
| if (seeding_dev) { |
| btrfs_clear_sb_rdonly(sb); |
| ret = btrfs_prepare_sprout(fs_info); |
| if (ret) { |
| btrfs_abort_transaction(trans, ret); |
| goto error_trans; |
| } |
| btrfs_assign_next_active_device(fs_info->fs_devices->latest_dev, |
| device); |
| } |
| |
| device->fs_devices = fs_devices; |
| |
| mutex_lock(&fs_devices->device_list_mutex); |
| mutex_lock(&fs_info->chunk_mutex); |
| list_add_rcu(&device->dev_list, &fs_devices->devices); |
| list_add(&device->dev_alloc_list, &fs_devices->alloc_list); |
| fs_devices->num_devices++; |
| fs_devices->open_devices++; |
| fs_devices->rw_devices++; |
| fs_devices->total_devices++; |
| fs_devices->total_rw_bytes += device->total_bytes; |
| |
| atomic64_add(device->total_bytes, &fs_info->free_chunk_space); |
| |
| if (!blk_queue_nonrot(q)) |
| fs_devices->rotating = true; |
| |
| orig_super_total_bytes = btrfs_super_total_bytes(fs_info->super_copy); |
| btrfs_set_super_total_bytes(fs_info->super_copy, |
| round_down(orig_super_total_bytes + device->total_bytes, |
| fs_info->sectorsize)); |
| |
| orig_super_num_devices = btrfs_super_num_devices(fs_info->super_copy); |
| btrfs_set_super_num_devices(fs_info->super_copy, |
| orig_super_num_devices + 1); |
| |
| /* |
| * we've got more storage, clear any full flags on the space |
| * infos |
| */ |
| btrfs_clear_space_info_full(fs_info); |
| |
| mutex_unlock(&fs_info->chunk_mutex); |
| |
| /* Add sysfs device entry */ |
| btrfs_sysfs_add_device(device); |
| |
| mutex_unlock(&fs_devices->device_list_mutex); |
| |
| if (seeding_dev) { |
| mutex_lock(&fs_info->chunk_mutex); |
| ret = init_first_rw_device(trans); |
| mutex_unlock(&fs_info->chunk_mutex); |
| if (ret) { |
| btrfs_abort_transaction(trans, ret); |
| goto error_sysfs; |
| } |
| } |
| |
| ret = btrfs_add_dev_item(trans, device); |
| if (ret) { |
| btrfs_abort_transaction(trans, ret); |
| goto error_sysfs; |
| } |
| |
| if (seeding_dev) { |
| ret = btrfs_finish_sprout(trans); |
| if (ret) { |
| btrfs_abort_transaction(trans, ret); |
| goto error_sysfs; |
| } |
| |
| /* |
| * fs_devices now represents the newly sprouted filesystem and |
| * its fsid has been changed by btrfs_prepare_sprout |
| */ |
| btrfs_sysfs_update_sprout_fsid(fs_devices); |
| } |
| |
| ret = btrfs_commit_transaction(trans); |
| |
| if (seeding_dev) { |
| mutex_unlock(&uuid_mutex); |
| up_write(&sb->s_umount); |
| locked = false; |
| |
| if (ret) /* transaction commit */ |
| return ret; |
| |
| ret = btrfs_relocate_sys_chunks(fs_info); |
| if (ret < 0) |
| btrfs_handle_fs_error(fs_info, ret, |
| "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command."); |
| trans = btrfs_attach_transaction(root); |
| if (IS_ERR(trans)) { |
| if (PTR_ERR(trans) == -ENOENT) |
| return 0; |
| ret = PTR_ERR(trans); |
| trans = NULL; |
| goto error_sysfs; |
| } |
| ret = btrfs_commit_transaction(trans); |
| } |
| |
| /* |
| * Now that we have written a new super block to this device, check all |
| * other fs_devices list if device_path alienates any other scanned |
| * device. |
| * We can ignore the return value as it typically returns -EINVAL and |
| * only succeeds if the device was an alien. |
| */ |
| btrfs_forget_devices(device_path); |
| |
| /* Update ctime/mtime for blkid or udev */ |
| update_dev_time(device_path); |
| |
| return ret; |
| |
| error_sysfs: |
| btrfs_sysfs_remove_device(device); |
| mutex_lock(&fs_info->fs_devices->device_list_mutex); |
| mutex_lock(&fs_info->chunk_mutex); |
| list_del_rcu(&device->dev_list); |
| list_del(&device->dev_alloc_list); |
| fs_info->fs_devices->num_devices--; |
| fs_info->fs_devices->open_devices--; |
| fs_info->fs_devices->rw_devices--; |
| fs_info->fs_devices->total_devices--; |
| fs_info->fs_devices->total_rw_bytes -= device->total_bytes; |
| atomic64_sub(device->total_bytes, &fs_info->free_chunk_space); |
| btrfs_set_super_total_bytes(fs_info->super_copy, |
| orig_super_total_bytes); |
| btrfs_set_super_num_devices(fs_info->super_copy, |
| orig_super_num_devices); |
| mutex_unlock(&fs_info->chunk_mutex); |
| mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
| error_trans: |
| if (seeding_dev) |
| btrfs_set_sb_rdonly(sb); |
| if (trans) |
| btrfs_end_transaction(trans); |
| error_free_zone: |
| btrfs_destroy_dev_zone_info(device); |
| error_free_device: |
| btrfs_free_device(device); |
| error: |
| blkdev_put(bdev, FMODE_EXCL); |
| if (locked) { |
| mutex_unlock(&uuid_mutex); |
| up_write(&sb->s_umount); |
| } |
| return ret; |
| } |
| |
| static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, |
| struct btrfs_device *device) |
| { |
| int ret; |
| struct btrfs_path *path; |
| struct btrfs_root *root = device->fs_info->chunk_root; |
| struct btrfs_dev_item *dev_item; |
| struct extent_buffer *leaf; |
| struct btrfs_key key; |
| |
| path = btrfs_alloc_path(); |
| if (!path) |
| return -ENOMEM; |
| |
| key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| key.type = BTRFS_DEV_ITEM_KEY; |
| key.offset = device->devid; |
| |
| ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| if (ret < 0) |
| goto out; |
| |
| if (ret > 0) { |
| ret = -ENOENT; |
| goto out; |
| } |
| |
| leaf = path->nodes[0]; |
| dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| |
| btrfs_set_device_id(leaf, dev_item, device->devid); |
| btrfs_set_device_type(leaf, dev_item, device->type); |
| btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
| btrfs_set_device_total_bytes(leaf, dev_item, |
| btrfs_device_get_disk_total_bytes(device)); |
| btrfs_set_device_bytes_used(leaf, dev_item, |
| btrfs_device_get_bytes_used(device)); |
| btrfs_mark_buffer_dirty(leaf); |
| |
| out: |
| btrfs_free_path(path); |
| return ret; |
| } |
| |
| int btrfs_grow_device(struct btrfs_trans_handle *trans, |
| struct btrfs_device *device, u64 new_size) |
| { |
| struct btrfs_fs_info *fs_info = device->fs_info; |
| struct btrfs_super_block *super_copy = fs_info->super_copy; |
| u64 old_total; |
| u64 diff; |
| int ret; |
| |
| if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
| return -EACCES; |
| |
| new_size = round_down(new_size, fs_info->sectorsize); |
| |
| mutex_lock(&fs_info->chunk_mutex); |
| old_total = btrfs_super_total_bytes(super_copy); |
| diff = round_down(new_size - device->total_bytes, fs_info->sectorsize); |
| |
| if (new_size <= device->total_bytes || |
| test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
| mutex_unlock(&fs_info->chunk_mutex); |
| return -EINVAL; |
| } |
| |
| btrfs_set_super_total_bytes(super_copy, |
| round_down(old_total + diff, fs_info->sectorsize)); |
| device->fs_devices->total_rw_bytes += diff; |
| |
| btrfs_device_set_total_bytes(device, new_size); |
| btrfs_device_set_disk_total_bytes(device, new_size); |
| btrfs_clear_space_info_full(device->fs_info); |
| if (list_empty(&device->post_commit_list)) |
| list_add_tail(&device->post_commit_list, |
| &trans->transaction->dev_update_list); |
| mutex_unlock(&fs_info->chunk_mutex); |
| |
| btrfs_reserve_chunk_metadata(trans, false); |
| ret = btrfs_update_device(trans, device); |
| btrfs_trans_release_chunk_metadata(trans); |
| |
| return ret; |
| } |
| |
| static int btrfs_free_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset) |
| { |
| struct btrfs_fs_info *fs_info = trans->fs_info; |
| struct btrfs_root *root = fs_info->chunk_root; |
| int ret; |
| struct btrfs_path *path; |
| struct btrfs_key key; |
| |
| path = btrfs_alloc_path(); |
| if (!path) |
| return -ENOMEM; |
| |
| key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| key.offset = chunk_offset; |
| key.type = BTRFS_CHUNK_ITEM_KEY; |
| |
| ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| if (ret < 0) |
| goto out; |
| else if (ret > 0) { /* Logic error or corruption */ |
| btrfs_handle_fs_error(fs_info, -ENOENT, |
| "Failed lookup while freeing chunk."); |
| ret = -ENOENT; |
| goto out; |
| } |
| |
| ret = btrfs_del_item(trans, root, path); |
| if (ret < 0) |
| btrfs_handle_fs_error(fs_info, ret, |
| "Failed to delete chunk item."); |
| out: |
| btrfs_free_path(path); |
| return ret; |
| } |
| |
| static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset) |
| { |
| struct btrfs_super_block *super_copy = fs_info->super_copy; |
| struct btrfs_disk_key *disk_key; |
| struct btrfs_chunk *chunk; |
| u8 *ptr; |
| int ret = 0; |
| u32 num_stripes; |
| u32 array_size; |
| u32 len = 0; |
| u32 cur; |
| struct btrfs_key key; |
| |
| lockdep_assert_held(&fs_info->chunk_mutex); |
| array_size = btrfs_super_sys_array_size(super_copy); |
| |
| ptr = super_copy->sys_chunk_array; |
| cur = 0; |
| |
| while (cur < array_size) { |
| disk_key = (struct btrfs_disk_key *)ptr; |
| btrfs_disk_key_to_cpu(&key, disk_key); |
| |
| len = sizeof(*disk_key); |
| |
| if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
| chunk = (struct btrfs_chunk *)(ptr + len); |
| num_stripes = btrfs_stack_chunk_num_stripes(chunk); |
| len += btrfs_chunk_item_size(num_stripes); |
| } else { |
| ret = -EIO; |
| break; |
| } |
| if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID && |
| key.offset == chunk_offset) { |
| memmove(ptr, ptr + len, array_size - (cur + len)); |
| array_size -= len; |
| btrfs_set_super_sys_array_size(super_copy, array_size); |
| } else { |
| ptr += len; |
| cur += len; |
| } |
| } |
| return ret; |
| } |
| |
| /* |
| * btrfs_get_chunk_map() - Find the mapping containing the given logical extent. |
| * @logical: Logical block offset in bytes. |
| * @length: Length of extent in bytes. |
| * |
| * Return: Chunk mapping or ERR_PTR. |
| */ |
| struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info, |
| u64 logical, u64 length) |
| { |
| struct extent_map_tree *em_tree; |
| struct extent_map *em; |
| |
| em_tree = &fs_info->mapping_tree; |
| read_lock(&em_tree->lock); |
| em = lookup_extent_mapping(em_tree, logical, length); |
| read_unlock(&em_tree->lock); |
| |
| if (!em) { |
| btrfs_crit(fs_info, "unable to find logical %llu length %llu", |
| logical, length); |
| return ERR_PTR(-EINVAL); |
| } |
| |
| if (em->start > logical || em->start + em->len < logical) { |
| btrfs_crit(fs_info, |
| "found a bad mapping, wanted %llu-%llu, found %llu-%llu", |
| logical, length, em->start, em->start + em->len); |
| free_extent_map(em); |
| return ERR_PTR(-EINVAL); |
| } |
| |
| /* callers are responsible for dropping em's ref. */ |
| return em; |
| } |
| |
| static int remove_chunk_item(struct btrfs_trans_handle *trans, |
| struct map_lookup *map, u64 chunk_offset) |
| { |
| int i; |
| |
| /* |
| * Removing chunk items and updating the device items in the chunks btree |
| * requires holding the chunk_mutex. |
| * See the comment at btrfs_chunk_alloc() for the details. |
| */ |
| lockdep_assert_held(&trans->fs_info->chunk_mutex); |
| |
| for (i = 0; i < map->num_stripes; i++) { |
| int ret; |
| |
| ret = btrfs_update_device(trans, map->stripes[i].dev); |
| if (ret) |
| return ret; |
| } |
| |
| return btrfs_free_chunk(trans, chunk_offset); |
| } |
| |
| int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset) |
| { |
| struct btrfs_fs_info *fs_info = trans->fs_info; |
| struct extent_map *em; |
| struct map_lookup *map; |
| u64 dev_extent_len = 0; |
| int i, ret = 0; |
| struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| |
| em = btrfs_get_chunk_map(fs_info, chunk_offset, 1); |
| if (IS_ERR(em)) { |
| /* |
| * This is a logic error, but we don't want to just rely on the |
| * user having built with ASSERT enabled, so if ASSERT doesn't |
| * do anything we still error out. |
| */ |
| ASSERT(0); |
| return PTR_ERR(em); |
| } |
| map = em->map_lookup; |
| |
| /* |
| * First delete the device extent items from the devices btree. |
| * We take the device_list_mutex to avoid racing with the finishing phase |
| * of a device replace operation. See the comment below before acquiring |
| * fs_info->chunk_mutex. Note that here we do not acquire the chunk_mutex |
| * because that can result in a deadlock when deleting the device extent |
| * items from the devices btree - COWing an extent buffer from the btree |
| * may result in allocating a new metadata chunk, which would attempt to |
| * lock again fs_info->chunk_mutex. |
| */ |
| mutex_lock(&fs_devices->device_list_mutex); |
| for (i = 0; i < map->num_stripes; i++) { |
| struct btrfs_device *device = map->stripes[i].dev; |
| ret = btrfs_free_dev_extent(trans, device, |
| map->stripes[i].physical, |
| &dev_extent_len); |
| if (ret) { |
| mutex_unlock(&fs_devices->device_list_mutex); |
| btrfs_abort_transaction(trans, ret); |
| goto out; |
| } |
| |
| if (device->bytes_used > 0) { |
| mutex_lock(&fs_info->chunk_mutex); |
| btrfs_device_set_bytes_used(device, |
| device->bytes_used - dev_extent_len); |
| atomic64_add(dev_extent_len, &fs_info->free_chunk_space); |
| btrfs_clear_space_info_full(fs_info); |
| mutex_unlock(&fs_info->chunk_mutex); |
| } |
| } |
| mutex_unlock(&fs_devices->device_list_mutex); |
| |
| /* |
| * We acquire fs_info->chunk_mutex for 2 reasons: |
| * |
| * 1) Just like with the first phase of the chunk allocation, we must |
| * reserve system space, do all chunk btree updates and deletions, and |
| * update the system chunk array in the superblock while holding this |
| * mutex. This is for similar reasons as explained on the comment at |
| * the top of btrfs_chunk_alloc(); |
| * |
| * 2) Prevent races with the final phase of a device replace operation |
| * that replaces the device object associated with the map's stripes, |
| * because the device object's id can change at any time during that |
| * final phase of the device replace operation |
| * (dev-replace.c:btrfs_dev_replace_finishing()), so we could grab the |
| * replaced device and then see it with an ID of |
| * BTRFS_DEV_REPLACE_DEVID, which would cause a failure when updating |
| * the device item, which does not exists on the chunk btree. |
| * The finishing phase of device replace acquires both the |
| * device_list_mutex and the chunk_mutex, in that order, so we are |
| * safe by just acquiring the chunk_mutex. |
| */ |
| trans->removing_chunk = true; |
| mutex_lock(&fs_info->chunk_mutex); |
| |
| check_system_chunk(trans, map->type); |
| |
| ret = remove_chunk_item(trans, map, chunk_offset); |
| /* |
| * Normally we should not get -ENOSPC since we reserved space before |
| * through the call to check_system_chunk(). |
| * |
| * Despite our system space_info having enough free space, we may not |
| * be able to allocate extents from its block groups, because all have |
| * an incompatible profile, which will force us to allocate a new system |
| * block group with the right profile, or right after we called |
| * check_system_space() above, a scrub turned the only system block group |
| * with enough free space into RO mode. |
| * This is explained with more detail at do_chunk_alloc(). |
| * |
| * So if we get -ENOSPC, allocate a new system chunk and retry once. |
| */ |
| if (ret == -ENOSPC) { |
| const u64 sys_flags = btrfs_system_alloc_profile(fs_info); |
| struct btrfs_block_group *sys_bg; |
| |
| sys_bg = btrfs_create_chunk(trans, sys_flags); |
| if (IS_ERR(sys_bg)) { |
| ret = PTR_ERR(sys_bg); |
| btrfs_abort_transaction(trans, ret); |
| goto out; |
| } |
| |
| ret = btrfs_chunk_alloc_add_chunk_item(trans, sys_bg); |
| if (ret) { |
| btrfs_abort_transaction(trans, ret); |
| goto out; |
| } |
| |
| ret = remove_chunk_item(trans, map, chunk_offset); |
| if (ret) { |
| btrfs_abort_transaction(trans, ret); |
| goto out; |
| } |
| } else if (ret) { |
| btrfs_abort_transaction(trans, ret); |
| goto out; |
| } |
| |
| trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len); |
| |
| if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| ret = btrfs_del_sys_chunk(fs_info, chunk_offset); |
| if (ret) { |
| btrfs_abort_transaction(trans, ret); |
| |