| // SPDX-License-Identifier: GPL-2.0 |
| /* |
| * Copyright (C) 2007 Oracle. All rights reserved. |
| */ |
| |
| #include <linux/sched.h> |
| #include <linux/sched/mm.h> |
| #include <linux/slab.h> |
| #include <linux/ratelimit.h> |
| #include <linux/kthread.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 "disk-io.h" |
| #include "transaction.h" |
| #include "volumes.h" |
| #include "raid56.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" |
| #include "fs.h" |
| #include "accessors.h" |
| #include "uuid-tree.h" |
| #include "ioctl.h" |
| #include "relocation.h" |
| #include "scrub.h" |
| #include "super.h" |
| #include "raid-stripe-tree.h" |
| |
| #define BTRFS_BLOCK_GROUP_STRIPE_MASK (BTRFS_BLOCK_GROUP_RAID0 | \ |
| BTRFS_BLOCK_GROUP_RAID10 | \ |
| BTRFS_BLOCK_GROUP_RAID56_MASK) |
| |
| struct btrfs_io_geometry { |
| u32 stripe_index; |
| u32 stripe_nr; |
| int mirror_num; |
| int num_stripes; |
| u64 stripe_offset; |
| u64 raid56_full_stripe_start; |
| int max_errors; |
| enum btrfs_map_op op; |
| }; |
| |
| 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) |
| { |
| const u64 profile = (flags & BTRFS_BLOCK_GROUP_PROFILE_MASK); |
| |
| if (!profile) |
| return BTRFS_RAID_SINGLE; |
| |
| return BTRFS_BG_FLAG_TO_INDEX(profile); |
| } |
| |
| 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; |
| } |
| |
| int btrfs_nr_parity_stripes(u64 type) |
| { |
| enum btrfs_raid_types index = btrfs_bg_flags_to_raid_index(type); |
| |
| return btrfs_raid_array[index].nparity; |
| } |
| |
| /* |
| * 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_load(struct btrfs_device *device); |
| |
| /* |
| * 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; |
| } |
| |
| /* |
| * Allocate new btrfs_fs_devices structure identified by a fsid. |
| * |
| * @fsid: if not NULL, copy the UUID to fs_devices::fsid and 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) |
| { |
| 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); |
| memcpy(fs_devs->metadata_uuid, fsid, BTRFS_FSID_SIZE); |
| } |
| |
| return fs_devs; |
| } |
| |
| static 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); |
| 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 bool match_fsid_fs_devices(const struct btrfs_fs_devices *fs_devices, |
| const u8 *fsid, const u8 *metadata_fsid) |
| { |
| if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) != 0) |
| return false; |
| |
| if (!metadata_fsid) |
| return true; |
| |
| if (memcmp(metadata_fsid, fs_devices->metadata_uuid, BTRFS_FSID_SIZE) != 0) |
| return false; |
| |
| return true; |
| } |
| |
| 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 (match_fsid_fs_devices(fs_devices, fsid, metadata_fsid)) |
| return fs_devices; |
| } |
| return NULL; |
| } |
| |
| static int |
| btrfs_get_bdev_and_sb(const char *device_path, blk_mode_t flags, void *holder, |
| int flush, struct file **bdev_file, |
| struct btrfs_super_block **disk_super) |
| { |
| struct block_device *bdev; |
| int ret; |
| |
| *bdev_file = bdev_file_open_by_path(device_path, flags, holder, NULL); |
| |
| if (IS_ERR(*bdev_file)) { |
| ret = PTR_ERR(*bdev_file); |
| goto error; |
| } |
| bdev = file_bdev(*bdev_file); |
| |
| if (flush) |
| sync_blockdev(bdev); |
| if (holder) { |
| ret = set_blocksize(*bdev_file, BTRFS_BDEV_BLOCKSIZE); |
| if (ret) { |
| fput(*bdev_file); |
| goto error; |
| } |
| } |
| invalidate_bdev(bdev); |
| *disk_super = btrfs_read_dev_super(bdev); |
| if (IS_ERR(*disk_super)) { |
| ret = PTR_ERR(*disk_super); |
| fput(*bdev_file); |
| goto error; |
| } |
| |
| return 0; |
| |
| error: |
| *disk_super = NULL; |
| *bdev_file = NULL; |
| return ret; |
| } |
| |
| /* |
| * Search and remove all stale devices (which are not mounted). When both |
| * inputs are NULL, it will search and release all stale devices. |
| * |
| * @devt: Optional. When provided will it release all unmounted devices |
| * matching this devt only. |
| * @skip_device: Optional. Will skip this device when searching for the stale |
| * devices. |
| * |
| * Return: 0 for success or if @devt is 0. |
| * -EBUSY if @devt is a mounted device. |
| * -ENOENT if @devt does not match any device in the list. |
| */ |
| static int btrfs_free_stale_devices(dev_t devt, struct btrfs_device *skip_device) |
| { |
| struct btrfs_fs_devices *fs_devices, *tmp_fs_devices; |
| struct btrfs_device *device, *tmp_device; |
| int ret; |
| bool freed = false; |
| |
| lockdep_assert_held(&uuid_mutex); |
| |
| /* Return good status if there is no instance of devt. */ |
| ret = 0; |
| 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 (devt && devt != device->devt) |
| continue; |
| if (fs_devices->opened) { |
| if (devt) |
| ret = -EBUSY; |
| break; |
| } |
| |
| /* delete the stale device */ |
| fs_devices->num_devices--; |
| list_del(&device->dev_list); |
| btrfs_free_device(device); |
| |
| freed = true; |
| } |
| 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); |
| } |
| } |
| |
| /* If there is at least one freed device return 0. */ |
| if (freed) |
| return 0; |
| |
| return ret; |
| } |
| |
| static struct btrfs_fs_devices *find_fsid_by_device( |
| struct btrfs_super_block *disk_super, |
| dev_t devt, bool *same_fsid_diff_dev) |
| { |
| struct btrfs_fs_devices *fsid_fs_devices; |
| struct btrfs_fs_devices *devt_fs_devices; |
| const bool has_metadata_uuid = (btrfs_super_incompat_flags(disk_super) & |
| BTRFS_FEATURE_INCOMPAT_METADATA_UUID); |
| bool found_by_devt = false; |
| |
| /* Find the fs_device by the usual method, if found use it. */ |
| fsid_fs_devices = find_fsid(disk_super->fsid, |
| has_metadata_uuid ? disk_super->metadata_uuid : NULL); |
| |
| /* The temp_fsid feature is supported only with single device filesystem. */ |
| if (btrfs_super_num_devices(disk_super) != 1) |
| return fsid_fs_devices; |
| |
| /* |
| * A seed device is an integral component of the sprout device, which |
| * functions as a multi-device filesystem. So, temp-fsid feature is |
| * not supported. |
| */ |
| if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) |
| return fsid_fs_devices; |
| |
| /* Try to find a fs_devices by matching devt. */ |
| list_for_each_entry(devt_fs_devices, &fs_uuids, fs_list) { |
| struct btrfs_device *device; |
| |
| list_for_each_entry(device, &devt_fs_devices->devices, dev_list) { |
| if (device->devt == devt) { |
| found_by_devt = true; |
| break; |
| } |
| } |
| if (found_by_devt) |
| break; |
| } |
| |
| if (found_by_devt) { |
| /* Existing device. */ |
| if (fsid_fs_devices == NULL) { |
| if (devt_fs_devices->opened == 0) { |
| /* Stale device. */ |
| return NULL; |
| } else { |
| /* temp_fsid is mounting a subvol. */ |
| return devt_fs_devices; |
| } |
| } else { |
| /* Regular or temp_fsid device mounting a subvol. */ |
| return devt_fs_devices; |
| } |
| } else { |
| /* New device. */ |
| if (fsid_fs_devices == NULL) { |
| return NULL; |
| } else { |
| /* sb::fsid is already used create a new temp_fsid. */ |
| *same_fsid_diff_dev = true; |
| return NULL; |
| } |
| } |
| |
| /* Not reached. */ |
| } |
| |
| /* |
| * 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, blk_mode_t flags, |
| void *holder) |
| { |
| struct file *bdev_file; |
| 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_file, &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(file_bdev(bdev_file))) |
| clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
| else |
| set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
| } |
| |
| if (!bdev_nonrot(file_bdev(bdev_file))) |
| fs_devices->rotating = true; |
| |
| if (bdev_max_discard_sectors(file_bdev(bdev_file))) |
| fs_devices->discardable = true; |
| |
| device->bdev_file = bdev_file; |
| device->bdev = file_bdev(bdev_file); |
| clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
| |
| if (device->devt != device->bdev->bd_dev) { |
| btrfs_warn(NULL, |
| "device %s maj:min changed from %d:%d to %d:%d", |
| device->name->str, MAJOR(device->devt), |
| MINOR(device->devt), MAJOR(device->bdev->bd_dev), |
| MINOR(device->bdev->bd_dev)); |
| |
| device->devt = device->bdev->bd_dev; |
| } |
| |
| 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); |
| fput(bdev_file); |
| |
| return -EINVAL; |
| } |
| |
| const u8 *btrfs_sb_fsid_ptr(const struct btrfs_super_block *sb) |
| { |
| bool has_metadata_uuid = (btrfs_super_incompat_flags(sb) & |
| BTRFS_FEATURE_INCOMPAT_METADATA_UUID); |
| |
| return has_metadata_uuid ? sb->metadata_uuid : sb->fsid; |
| } |
| |
| /* |
| * 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); |
| dev_t path_devt; |
| int error; |
| bool same_fsid_diff_dev = false; |
| bool has_metadata_uuid = (btrfs_super_incompat_flags(disk_super) & |
| BTRFS_FEATURE_INCOMPAT_METADATA_UUID); |
| |
| if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_CHANGING_FSID_V2) { |
| btrfs_err(NULL, |
| "device %s has incomplete metadata_uuid change, please use btrfstune to complete", |
| path); |
| return ERR_PTR(-EAGAIN); |
| } |
| |
| error = lookup_bdev(path, &path_devt); |
| if (error) { |
| btrfs_err(NULL, "failed to lookup block device for path %s: %d", |
| path, error); |
| return ERR_PTR(error); |
| } |
| |
| fs_devices = find_fsid_by_device(disk_super, path_devt, &same_fsid_diff_dev); |
| |
| if (!fs_devices) { |
| fs_devices = alloc_fs_devices(disk_super->fsid); |
| if (IS_ERR(fs_devices)) |
| return ERR_CAST(fs_devices); |
| |
| if (has_metadata_uuid) |
| memcpy(fs_devices->metadata_uuid, |
| disk_super->metadata_uuid, BTRFS_FSID_SIZE); |
| |
| if (same_fsid_diff_dev) { |
| generate_random_uuid(fs_devices->fsid); |
| fs_devices->temp_fsid = true; |
| pr_info("BTRFS: device %s (%d:%d) using temp-fsid %pU\n", |
| path, MAJOR(path_devt), MINOR(path_devt), |
| fs_devices->fsid); |
| } |
| |
| 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 (found_transid > fs_devices->latest_generation) { |
| memcpy(fs_devices->fsid, disk_super->fsid, |
| BTRFS_FSID_SIZE); |
| memcpy(fs_devices->metadata_uuid, |
| btrfs_sb_fsid_ptr(disk_super), BTRFS_FSID_SIZE); |
| } |
| } |
| |
| if (!device) { |
| unsigned int nofs_flag; |
| |
| if (fs_devices->opened) { |
| btrfs_err(NULL, |
| "device %s (%d:%d) belongs to fsid %pU, and the fs is already mounted, scanned by %s (%d)", |
| path, MAJOR(path_devt), MINOR(path_devt), |
| fs_devices->fsid, current->comm, |
| task_pid_nr(current)); |
| mutex_unlock(&fs_devices->device_list_mutex); |
| return ERR_PTR(-EBUSY); |
| } |
| |
| nofs_flag = memalloc_nofs_save(); |
| device = btrfs_alloc_device(NULL, &devid, |
| disk_super->dev_item.uuid, path); |
| memalloc_nofs_restore(nofs_flag); |
| if (IS_ERR(device)) { |
| mutex_unlock(&fs_devices->device_list_mutex); |
| /* we can safely leave the fs_devices entry around */ |
| return device; |
| } |
| |
| device->devt = path_devt; |
| |
| 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 (%d:%d) scanned by %s (%d)\n", |
| disk_super->label, devid, found_transid, path, |
| MAJOR(path_devt), MINOR(path_devt), |
| current->comm, task_pid_nr(current)); |
| else |
| pr_info( |
| "BTRFS: device fsid %pU devid %llu transid %llu %s (%d:%d) scanned by %s (%d)\n", |
| disk_super->fsid, devid, found_transid, path, |
| MAJOR(path_devt), MINOR(path_devt), |
| 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); |
| btrfs_err(NULL, |
| "device %s already registered with a higher generation, found %llu expect %llu", |
| path, found_transid, device->generation); |
| 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 |
| * |
| * NOTE: the device->fs_info may not be reliable here so pass |
| * in a NULL to message helpers instead. This avoids a possible |
| * use-after-free when the fs_info and fs_info->sb are already |
| * torn down. |
| */ |
| if (device->bdev) { |
| if (device->devt != path_devt) { |
| mutex_unlock(&fs_devices->device_list_mutex); |
| 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(NULL, |
| "devid %llu device path %s changed to %s scanned by %s (%d)", |
| devid, btrfs_dev_name(device), |
| 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); |
| } |
| device->devt = path_devt; |
| } |
| |
| /* |
| * 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); |
| 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) { |
| const char *dev_path = NULL; |
| |
| /* |
| * 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) |
| dev_path = orig_dev->name->str; |
| |
| device = btrfs_alloc_device(NULL, &orig_dev->devid, |
| orig_dev->uuid, dev_path); |
| if (IS_ERR(device)) { |
| ret = PTR_ERR(device); |
| goto error; |
| } |
| |
| if (orig_dev->zone_info) { |
| struct btrfs_zoned_device_info *zone_info; |
| |
| zone_info = btrfs_clone_dev_zone_info(orig_dev); |
| if (!zone_info) { |
| btrfs_free_device(device); |
| ret = -ENOMEM; |
| goto error; |
| } |
| device->zone_info = zone_info; |
| } |
| |
| 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_file) { |
| fput(device->bdev_file); |
| device->bdev = NULL; |
| device->bdev_file = 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); |
| } |
| |
| fput(device->bdev_file); |
| } |
| |
| 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 */ |
| WARN_ON(test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state)); |
| WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)); |
| WARN_ON(!list_empty(&device->dev_alloc_list)); |
| WARN_ON(!list_empty(&device->post_commit_list)); |
| } |
| |
| 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); |
| |
| /* |
| * If the struct btrfs_fs_devices is not assembled with any |
| * other device, it can be re-initialized during the next mount |
| * without the needing device-scan step. Therefore, it can be |
| * fully freed. |
| */ |
| if (fs_devices->num_devices == 1) { |
| list_del(&fs_devices->fs_list); |
| free_fs_devices(fs_devices); |
| } |
| } |
| |
| |
| 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, |
| blk_mode_t flags, void *holder) |
| { |
| struct btrfs_device *device; |
| struct btrfs_device *latest_dev = NULL; |
| struct btrfs_device *tmp_device; |
| int ret = 0; |
| |
| list_for_each_entry_safe(device, tmp_device, &fs_devices->devices, |
| dev_list) { |
| int ret2; |
| |
| ret2 = btrfs_open_one_device(fs_devices, device, flags, holder); |
| if (ret2 == 0 && |
| (!latest_dev || device->generation > latest_dev->generation)) { |
| latest_dev = device; |
| } else if (ret2 == -ENODATA) { |
| fs_devices->num_devices--; |
| list_del(&device->dev_list); |
| btrfs_free_device(device); |
| } |
| if (ret == 0 && ret2 != 0) |
| ret = ret2; |
| } |
| |
| if (fs_devices->open_devices == 0) { |
| if (ret) |
| return ret; |
| 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, |
| blk_mode_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_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(dev_t devt) |
| { |
| int ret; |
| |
| mutex_lock(&uuid_mutex); |
| ret = btrfs_free_stale_devices(devt, NULL); |
| mutex_unlock(&uuid_mutex); |
| |
| return ret; |
| } |
| |
| static bool btrfs_skip_registration(struct btrfs_super_block *disk_super, |
| const char *path, dev_t devt, |
| bool mount_arg_dev) |
| { |
| struct btrfs_fs_devices *fs_devices; |
| |
| /* |
| * Do not skip device registration for mounted devices with matching |
| * maj:min but different paths. Booting without initrd relies on |
| * /dev/root initially, later replaced with the actual root device. |
| * A successful scan ensures grub2-probe selects the correct device. |
| */ |
| list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| struct btrfs_device *device; |
| |
| mutex_lock(&fs_devices->device_list_mutex); |
| |
| if (!fs_devices->opened) { |
| mutex_unlock(&fs_devices->device_list_mutex); |
| continue; |
| } |
| |
| list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| if (device->bdev && (device->bdev->bd_dev == devt) && |
| strcmp(device->name->str, path) != 0) { |
| mutex_unlock(&fs_devices->device_list_mutex); |
| |
| /* Do not skip registration. */ |
| return false; |
| } |
| } |
| mutex_unlock(&fs_devices->device_list_mutex); |
| } |
| |
| if (!mount_arg_dev && btrfs_super_num_devices(disk_super) == 1 && |
| !(btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING)) |
| return true; |
| |
| return false; |
| } |
| |
| /* |
| * 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. |
| * |
| * With @mount_arg_dev it's a scan during mount time that will always register |
| * the device or return an error. Multi-device and seeding devices are registered |
| * in both cases. |
| */ |
| struct btrfs_device *btrfs_scan_one_device(const char *path, blk_mode_t flags, |
| bool mount_arg_dev) |
| { |
| struct btrfs_super_block *disk_super; |
| bool new_device_added = false; |
| struct btrfs_device *device = NULL; |
| struct file *bdev_file; |
| u64 bytenr; |
| dev_t devt; |
| int ret; |
| |
| lockdep_assert_held(&uuid_mutex); |
| |
| /* |
| * Avoid an exclusive open here, as the systemd-udev may initiate the |
| * device scan which may race with the user's mount or mkfs command, |
| * resulting in failure. |
| * Since the device scan is solely for reading purposes, there is no |
| * need for an exclusive open. Additionally, the devices are read again |
| * during the mount process. It is ok to get some inconsistent |
| * values temporarily, as the device paths of the fsid are the only |
| * required information for assembling the volume. |
| */ |
| bdev_file = bdev_file_open_by_path(path, flags, NULL, NULL); |
| if (IS_ERR(bdev_file)) |
| return ERR_CAST(bdev_file); |
| |
| /* |
| * We would like to check all the super blocks, but doing so would |
| * allow a mount to succeed after a mkfs from a different filesystem. |
| * Currently, recovery from a bad primary btrfs superblock is done |
| * using the userspace command 'btrfs check --super'. |
| */ |
| ret = btrfs_sb_log_location_bdev(file_bdev(bdev_file), 0, READ, &bytenr); |
| if (ret) { |
| device = ERR_PTR(ret); |
| goto error_bdev_put; |
| } |
| |
| disk_super = btrfs_read_disk_super(file_bdev(bdev_file), bytenr, |
| btrfs_sb_offset(0)); |
| if (IS_ERR(disk_super)) { |
| device = ERR_CAST(disk_super); |
| goto error_bdev_put; |
| } |
| |
| devt = file_bdev(bdev_file)->bd_dev; |
| if (btrfs_skip_registration(disk_super, path, devt, mount_arg_dev)) { |
| pr_debug("BTRFS: skip registering single non-seed device %s (%d:%d)\n", |
| path, MAJOR(devt), MINOR(devt)); |
| |
| btrfs_free_stale_devices(devt, NULL); |
| |
| device = NULL; |
| goto free_disk_super; |
| } |
| |
| device = device_list_add(path, disk_super, &new_device_added); |
| if (!IS_ERR(device) && new_device_added) |
| btrfs_free_stale_devices(device->devt, device); |
| |
| free_disk_super: |
| btrfs_release_disk_super(disk_super); |
| |
| error_bdev_put: |
| fput(bdev_file); |
| |
| 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 + 1 - physical_start)) { |
| *start = physical_end + 1; |
| return true; |
| } |
| } |
| return false; |
| } |
| |
| static u64 dev_extent_search_start(struct btrfs_device *device) |
| { |
| switch (device->fs_devices->chunk_alloc_policy) { |
| case BTRFS_CHUNK_ALLOC_REGULAR: |
| return BTRFS_DEVICE_RANGE_RESERVED; |
| 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 0; |
| 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; |
| } |
| |
| /* |
| * 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 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 does 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(struct btrfs_device *device, u64 num_bytes, |
| 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 search_start; |
| u64 hole_size; |
| u64 max_hole_start; |
| u64 max_hole_size = 0; |
| u64 extent_end; |
| u64 search_end = device->total_bytes; |
| int ret; |
| int slot; |
| struct extent_buffer *l; |
| |
| search_start = dev_extent_search_start(device); |
| max_hole_start = search_start; |
| |
| WARN_ON(device->zone_info && |
| !IS_ALIGNED(num_bytes, device->zone_info->zone_size)); |
| |
| path = btrfs_alloc_path(); |
| if (!path) { |
| ret = -ENOMEM; |
| goto out; |
| } |
| 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 (search_start < search_end) { |
| 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_end) |
| break; |
| |
| 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; |
| |
| ASSERT(max_hole_start + max_hole_size <= search_end); |
| out: |
| btrfs_free_path(path); |
| *start = max_hole_start; |
| if (len) |
| *len = max_hole_size; |
| return ret; |
| } |
| |
| 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 rb_node *n; |
| u64 ret = 0; |
| |
| read_lock(&fs_info->mapping_tree_lock); |
| n = rb_last(&fs_info->mapping_tree.rb_root); |
| if (n) { |
| struct btrfs_chunk_map *map; |
| |
| map = rb_entry(n, struct btrfs_chunk_map, rb_node); |
| ret = map->start + map->chunk_len; |
| } |
| read_unlock(&fs_info->mapping_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(trans, 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; |
| int ret; |
| |
| ret = kern_path(device_path, LOOKUP_FOLLOW, &path); |
| if (ret) |
| return; |
| |
| inode_update_time(d_inode(path.dentry), S_MTIME | S_CTIME | S_VERSION); |
| path_put(&path); |
| } |
| |
| static int btrfs_rm_dev_item(struct btrfs_trans_handle *trans, |
| struct btrfs_device *device) |
| { |
| struct btrfs_root *root = device->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_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; |
| goto out; |
| } |
| |
| ret = btrfs_del_item(trans, root, path); |
| out: |
| btrfs_free_path(path); |
| 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; |
| } |
| |
| static void btrfs_scratch_superblock(struct btrfs_fs_info *fs_info, |
| struct block_device *bdev, int copy_num) |
| { |
| struct btrfs_super_block *disk_super; |
| const size_t len = sizeof(disk_super->magic); |
| const u64 bytenr = btrfs_sb_offset(copy_num); |
| int ret; |
| |
| disk_super = btrfs_read_disk_super(bdev, bytenr, bytenr); |
| if (IS_ERR(disk_super)) |
| return; |
| |
| memset(&disk_super->magic, 0, len); |
| folio_mark_dirty(virt_to_folio(disk_super)); |
| btrfs_release_disk_super(disk_super); |
| |
| ret = sync_blockdev_range(bdev, bytenr, bytenr + len - 1); |
| if (ret) |
| btrfs_warn(fs_info, "error clearing superblock number %d (%d)", |
| copy_num, ret); |
| } |
| |
| void btrfs_scratch_superblocks(struct btrfs_fs_info *fs_info, struct btrfs_device *device) |
| { |
| int copy_num; |
| struct block_device *bdev = device->bdev; |
| |
| if (!bdev) |
| return; |
| |
| for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX; copy_num++) { |
| if (bdev_is_zoned(bdev)) |
| btrfs_reset_sb_log_zones(bdev, copy_num); |
| else |
| btrfs_scratch_superblock(fs_info, bdev, copy_num); |
| } |
| |
| /* Notify udev that device has changed */ |
| btrfs_kobject_uevent(bdev, KOBJ_CHANGE); |
| |
| /* Update ctime/mtime for device path for libblkid */ |
| update_dev_time(device->name->str); |
| } |
| |
| int btrfs_rm_device(struct btrfs_fs_info *fs_info, |
| struct btrfs_dev_lookup_args *args, |
| struct file **bdev_file) |
| { |
| struct btrfs_trans_handle *trans; |
| 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; |
| |
| if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) { |
| btrfs_err(fs_info, "device remove not supported on extent tree v2 yet"); |
| return -EINVAL; |
| } |
| |
| /* |
| * 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) |
| return ret; |
| |
| device = btrfs_find_device(fs_info->fs_devices, args); |
| if (!device) { |
| if (args->missing) |
| ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND; |
| else |
| ret = -ENOENT; |
| return ret; |
| } |
| |
| if (btrfs_pinned_by_swapfile(fs_info, device)) { |
| btrfs_warn_in_rcu(fs_info, |
| "cannot remove device %s (devid %llu) due to active swapfile", |
| btrfs_dev_name(device), device->devid); |
| return -ETXTBSY; |
| } |
| |
| if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) |
| return BTRFS_ERROR_DEV_TGT_REPLACE; |
| |
| if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
| fs_info->fs_devices->rw_devices == 1) |
| return BTRFS_ERROR_DEV_ONLY_WRITABLE; |
| |
| 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) |
| goto error_undo; |
| |
| trans = btrfs_start_transaction(fs_info->chunk_root, 0); |
| if (IS_ERR(trans)) { |
| ret = PTR_ERR(trans); |
| goto error_undo; |
| } |
| |
| ret = btrfs_rm_dev_item(trans, device); |
| if (ret) { |
| /* Any error in dev item removal is critical */ |
| btrfs_crit(fs_info, |
| "failed to remove device item for devid %llu: %d", |
| device->devid, ret); |
| btrfs_abort_transaction(trans, ret); |
| btrfs_end_transaction(trans); |
| return ret; |
| } |
| |
| 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_file) { |
| 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 fput() on the block device 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 bdev_release. |
| */ |
| if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
| btrfs_scratch_superblocks(fs_info, device); |
| if (device->bdev) { |
| sync_blockdev(device->bdev); |
| invalidate_bdev(device->bdev); |
| } |
| } |
| |
| *bdev_file = device->bdev_file; |
| 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); |
| } |
| |
| ret = btrfs_commit_transaction(trans); |
| |
| return ret; |
| |
| error_undo: |
| 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); |
| } |
| return ret; |
| } |
| |
| 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); |
| |
| 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 file *bdev_file; |
| 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, BLK_OPEN_READ, NULL, 0, |
| &bdev_file, &disk_super); |
| if (ret) { |
| btrfs_put_dev_args_from_path(args); |
| 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); |
| fput(bdev_file); |
| 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; |
| } |
| |
| static struct btrfs_fs_devices *btrfs_init_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; |
| |
| lockdep_assert_held(&uuid_mutex); |
| if (!fs_devices->seeding) |
| return ERR_PTR(-EINVAL); |
| |
| /* |
| * Private copy of the seed devices, anchored at |
| * fs_info->fs_devices->seed_list |
| */ |
| seed_devices = alloc_fs_devices(NULL); |
| if (IS_ERR(seed_devices)) |
| return 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 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); |
| |
| return seed_devices; |
| } |
| |
| /* |
| * Splice seed devices into the sprout fs_devices. |
| * Generate a new fsid for the sprouted read-write filesystem. |
| */ |
| static void btrfs_setup_sprout(struct btrfs_fs_info *fs_info, |
| struct btrfs_fs_devices *seed_devices) |
| { |
| struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| struct btrfs_super_block *disk_super = fs_info->super_copy; |
| struct btrfs_device *device; |
| u64 super_flags; |
| |
| /* |
| * We are updating the fsid, the thread leading to device_list_add() |
| * could race, so uuid_mutex is needed. |
| */ |
| lockdep_assert_held(&uuid_mutex); |
| |
| /* |
| * The threads listed below may traverse dev_list but can do that without |
| * device_list_mutex: |
| * - All device ops and balance - as we are in btrfs_exclop_start. |
| * - Various dev_list readers - are using RCU. |
| * - btrfs_ioctl_fitrim() - is using RCU. |
| * |
| * For-read threads as below are using device_list_mutex: |
| * - Readonly scrub btrfs_scrub_dev() |
| * - Readonly scrub btrfs_scrub_progress() |
| * - btrfs_get_dev_stats() |
| */ |
| lockdep_assert_held(&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); |
| |
| super_flags = btrfs_super_flags(disk_super) & |
| ~BTRFS_SUPER_FLAG_SEEDING; |
| btrfs_set_super_flags(disk_super, super_flags); |
| } |
| |
| /* |
| * 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(trans, 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 btrfs_trans_handle *trans; |
| struct btrfs_device *device; |
| struct file *bdev_file; |
| struct super_block *sb = fs_info->sb; |
| struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| struct btrfs_fs_devices *seed_devices = NULL; |
| u64 orig_super_total_bytes; |
| u64 orig_super_num_devices; |
| int ret = 0; |
| bool seeding_dev = false; |
| bool locked = false; |
| |
| if (sb_rdonly(sb) && !fs_devices->seeding) |
| return -EROFS; |
| |
| bdev_file = bdev_file_open_by_path(device_path, BLK_OPEN_WRITE, |
| fs_info->bdev_holder, NULL); |
| if (IS_ERR(bdev_file)) |
| return PTR_ERR(bdev_file); |
| |
| if (!btrfs_check_device_zone_type(fs_info, file_bdev(bdev_file))) { |
| ret = -EINVAL; |
| goto error; |
| } |
| |
| if (fs_devices->seeding) { |
| seeding_dev = true; |
| down_write(&sb->s_umount); |
| mutex_lock(&uuid_mutex); |
| locked = true; |
| } |
| |
| sync_blockdev(file_bdev(bdev_file)); |
| |
| rcu_read_lock(); |
| list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) { |
| if (device->bdev == file_bdev(bdev_file)) { |
| ret = -EEXIST; |
| rcu_read_unlock(); |
| goto error; |
| } |
| } |
| rcu_read_unlock(); |
| |
| device = btrfs_alloc_device(fs_info, NULL, NULL, device_path); |
| if (IS_ERR(device)) { |
| /* we can safely leave the fs_devices entry around */ |
| ret = PTR_ERR(device); |
| goto error; |
| } |
| |
| device->fs_info = fs_info; |
| device->bdev_file = bdev_file; |
| device->bdev = file_bdev(bdev_file); |
| ret = lookup_bdev(device_path, &device->devt); |
| if (ret) |
| goto error_free_device; |
| |
| ret = btrfs_get_dev_zone_info(device, false); |
| if (ret) |
| goto error_free_device; |
| |
| trans = btrfs_start_transaction(root, 0); |
| if (IS_ERR(trans)) { |
| ret = PTR_ERR(trans); |
| goto error_free_zone; |
| } |
| |
| 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(device->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->dev_stats_valid = 1; |
| set_blocksize(device->bdev_file, BTRFS_BDEV_BLOCKSIZE); |
| |
| if (seeding_dev) { |
| btrfs_clear_sb_rdonly(sb); |
| |
| /* GFP_KERNEL allocation must not be under device_list_mutex */ |
| seed_devices = btrfs_init_sprout(fs_info); |
| if (IS_ERR(seed_devices)) { |
| ret = PTR_ERR(seed_devices); |
| btrfs_abort_transaction(trans, ret); |
| goto error_trans; |
| } |
| } |
| |
| mutex_lock(&fs_devices->device_list_mutex); |
| if (seeding_dev) { |
| btrfs_setup_sprout(fs_info, seed_devices); |
| btrfs_assign_next_active_device(fs_info->fs_devices->latest_dev, |
| device); |
| } |
| |
| device->fs_devices = fs_devices; |
| |
| 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 (!bdev_nonrot(device->bdev)) |
| 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_sprout_splice(). |
| */ |
| 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->devt); |
| |
| /* 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: |
| fput(bdev_file); |
| 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(trans, 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; |
| atomic64_add(diff, &fs_info->free_chunk_space); |
| |
| 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_err(fs_info, "failed to lookup chunk %llu when freeing", |
| chunk_offset); |
| btrfs_abort_transaction(trans, -ENOENT); |
| ret = -EUCLEAN; |
| goto out; |
| } |
| |
| ret = btrfs_del_item(trans, root, path); |
| if (ret < 0) { |
| btrfs_err(fs_info, "failed to delete chunk %llu item", chunk_offset); |
| btrfs_abort_transaction(trans, ret); |
| goto out; |
| } |
| 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; |
| } |
| |
| struct btrfs_chunk_map *btrfs_find_chunk_map_nolock(struct btrfs_fs_info *fs_info, |
| u64 logical, u64 length) |
| { |
| struct rb_node *node = fs_info->mapping_tree.rb_root.rb_node; |
| struct rb_node *prev = NULL; |
| struct rb_node *orig_prev; |
| struct btrfs_chunk_map *map; |
| struct btrfs_chunk_map *prev_map = NULL; |
| |
| while (node) { |
| map = rb_entry(node, struct btrfs_chunk_map, rb_node); |
| prev = node; |
| prev_map = map; |
| |
| if (logical < map->start) { |
| node = node->rb_left; |
| } else if (logical >= map->start + map->chunk_len) { |
| node = node->rb_right; |
| } else { |
| refcount_inc(&map->refs); |
| return map; |
| } |
| } |
| |
| if (!prev) |
| return NULL; |
| |
| orig_prev = prev; |
| while (prev && logical >= prev_map->start + prev_map->chunk_len) { |
| prev = rb_next(prev); |
| prev_map = rb_entry(prev, struct btrfs_chunk_map, rb_node); |
| } |
| |
| if (!prev) { |
| prev = orig_prev; |
| prev_map = rb_entry(prev, struct btrfs_chunk_map, rb_node); |
| while (prev && logical < prev_map->start) { |
| prev = rb_prev(prev); |
| prev_map = rb_entry(prev, struct btrfs_chunk_map, rb_node); |
| } |
| } |
| |
| if (prev) { |
| u64 end = logical + length; |
| |
| /* |
| * Caller can pass a U64_MAX length when it wants to get any |
| * chunk starting at an offset of 'logical' or higher, so deal |
| * with underflow by resetting the end offset to U64_MAX. |
| */ |
| if (end < logical) |
| end = U64_MAX; |
| |
| if (end > prev_map->start && |
| logical < prev_map->start + prev_map->chunk_len) { |
| refcount_inc(&prev_map->refs); |
| return prev_map; |
| } |
| } |
| |
| return NULL; |
| } |
| |
| struct btrfs_chunk_map *btrfs_find_chunk_map(struct btrfs_fs_info *fs_info, |
| u64 logical, u64 length) |
| { |
| struct btrfs_chunk_map *map; |
| |
| read_lock(&fs_info->mapping_tree_lock); |
| map = btrfs_find_chunk_map_nolock(fs_info, logical, length); |
| read_unlock(&fs_info->mapping_tree_lock); |
| |
| return 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 btrfs_chunk_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info, |
| u64 logical, u64 length) |
| { |
| struct btrfs_chunk_map *map; |
| |
| map = btrfs_find_chunk_map(fs_info, logical, length); |
| |
| if (unlikely(!map)) { |
| btrfs_crit(fs_info, |
| "unable to find chunk map for logical %llu length %llu", |
| logical, length); |
| return ERR_PTR(-EINVAL); |
| } |
| |
| if (unlikely(map->start > logical || map->start + map->chunk_len <= logical)) { |
| btrfs_crit(fs_info, |
| "found a bad chunk map, wanted %llu-%llu, found %llu-%llu", |
| logical, logical + length, map->start, |
| map->start + map->chunk_len); |
| btrfs_free_chunk_map(map); |
| return ERR_PTR(-EINVAL); |
| } |
| |
| /* Callers are responsible for dropping the reference. */ |
| return map; |
| } |
| |
| static int remove_chunk_item(struct btrfs_trans_handle *trans, |
| struct btrfs_chunk_map *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 |