diff options
Diffstat (limited to 'fs/btrfs/volumes.c')
-rw-r--r-- | fs/btrfs/volumes.c | 849 |
1 files changed, 415 insertions, 434 deletions
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c index 71a60cc01451..3c3c69c0eee4 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -134,9 +134,9 @@ const int btrfs_raid_mindev_error[BTRFS_NR_RAID_TYPES] = { }; static int init_first_rw_device(struct btrfs_trans_handle *trans, - struct btrfs_root *root, + struct btrfs_fs_info *fs_info, struct btrfs_device *device); -static int btrfs_relocate_sys_chunks(struct btrfs_root *root); +static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info); static void __btrfs_reset_dev_stats(struct btrfs_device *dev); static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev); static void btrfs_dev_stat_print_on_load(struct btrfs_device *device); @@ -343,9 +343,9 @@ static void requeue_list(struct btrfs_pending_bios *pending_bios, */ static noinline void run_scheduled_bios(struct btrfs_device *device) { + struct btrfs_fs_info *fs_info = device->fs_info; struct bio *pending; struct backing_dev_info *bdi; - struct btrfs_fs_info *fs_info; struct btrfs_pending_bios *pending_bios; struct bio *tail; struct bio *cur; @@ -367,7 +367,6 @@ static noinline void run_scheduled_bios(struct btrfs_device *device) blk_start_plug(&plug); bdi = blk_get_backing_dev_info(device->bdev); - fs_info = device->dev_root->fs_info; limit = btrfs_async_submit_limit(fs_info); limit = limit * 2 / 3; @@ -1179,7 +1178,7 @@ int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start, u64 end, u64 *length) { struct btrfs_key key; - struct btrfs_root *root = device->dev_root; + struct btrfs_root *root = device->fs_info->dev_root; struct btrfs_dev_extent *dev_extent; struct btrfs_path *path; u64 extent_end; @@ -1262,7 +1261,7 @@ static int contains_pending_extent(struct btrfs_transaction *transaction, struct btrfs_device *device, u64 *start, u64 len) { - struct btrfs_fs_info *fs_info = device->dev_root->fs_info; + struct btrfs_fs_info *fs_info = device->fs_info; struct extent_map *em; struct list_head *search_list = &fs_info->pinned_chunks; int ret = 0; @@ -1338,8 +1337,9 @@ int find_free_dev_extent_start(struct btrfs_transaction *transaction, struct btrfs_device *device, u64 num_bytes, u64 search_start, u64 *start, u64 *len) { + struct btrfs_fs_info *fs_info = device->fs_info; + struct btrfs_root *root = fs_info->dev_root; struct btrfs_key key; - struct btrfs_root *root = device->dev_root; struct btrfs_dev_extent *dev_extent; struct btrfs_path *path; u64 hole_size; @@ -1357,7 +1357,7 @@ int find_free_dev_extent_start(struct btrfs_transaction *transaction, * used by the boot loader (grub for example), so we make sure to start * at an offset of at least 1MB. */ - min_search_start = max(root->fs_info->alloc_start, 1024ull * 1024); + min_search_start = max(fs_info->alloc_start, 1024ull * 1024); search_start = max(search_start, min_search_start); path = btrfs_alloc_path(); @@ -1508,9 +1508,10 @@ 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_root *root = device->dev_root; struct btrfs_key key; struct btrfs_key found_key; struct extent_buffer *leaf = NULL; @@ -1544,7 +1545,7 @@ again: extent = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent); } else { - btrfs_handle_fs_error(root->fs_info, ret, "Slot search failed"); + btrfs_handle_fs_error(fs_info, ret, "Slot search failed"); goto out; } @@ -1552,8 +1553,8 @@ again: ret = btrfs_del_item(trans, root, path); if (ret) { - btrfs_handle_fs_error(root->fs_info, ret, - "Failed to remove dev extent item"); + btrfs_handle_fs_error(fs_info, ret, + "Failed to remove dev extent item"); } else { set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags); } @@ -1569,7 +1570,8 @@ static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, { int ret; struct btrfs_path *path; - struct btrfs_root *root = device->dev_root; + struct btrfs_fs_info *fs_info = device->fs_info; + struct btrfs_root *root = fs_info->dev_root; struct btrfs_dev_extent *extent; struct extent_buffer *leaf; struct btrfs_key key; @@ -1595,8 +1597,7 @@ static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid); btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); - write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, - btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE); + write_extent_buffer_chunk_tree_uuid(leaf, fs_info->chunk_tree_uuid); btrfs_set_dev_extent_length(leaf, extent, num_bytes); btrfs_mark_buffer_dirty(leaf); @@ -1667,9 +1668,10 @@ error: * the btrfs_device struct should be fully filled in */ static int btrfs_add_device(struct btrfs_trans_handle *trans, - struct btrfs_root *root, + struct btrfs_fs_info *fs_info, struct btrfs_device *device) { + struct btrfs_root *root = fs_info->chunk_root; int ret; struct btrfs_path *path; struct btrfs_dev_item *dev_item; @@ -1677,8 +1679,6 @@ static int btrfs_add_device(struct btrfs_trans_handle *trans, struct btrfs_key key; unsigned long ptr; - root = root->fs_info->chunk_root; - path = btrfs_alloc_path(); if (!path) return -ENOMEM; @@ -1713,7 +1713,7 @@ static int btrfs_add_device(struct btrfs_trans_handle *trans, 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, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE); + write_extent_buffer(leaf, fs_info->fsid, ptr, BTRFS_UUID_SIZE); btrfs_mark_buffer_dirty(leaf); ret = 0; @@ -1737,16 +1737,15 @@ static void update_dev_time(char *path_name) filp_close(filp, NULL); } -static int btrfs_rm_dev_item(struct btrfs_root *root, +static int btrfs_rm_dev_item(struct btrfs_fs_info *fs_info, struct btrfs_device *device) { + struct btrfs_root *root = fs_info->chunk_root; int ret; struct btrfs_path *path; struct btrfs_key key; struct btrfs_trans_handle *trans; - root = root->fs_info->chunk_root; - path = btrfs_alloc_path(); if (!path) return -ENOMEM; @@ -1774,7 +1773,7 @@ static int btrfs_rm_dev_item(struct btrfs_root *root, goto out; out: btrfs_free_path(path); - btrfs_commit_transaction(trans, root); + btrfs_commit_transaction(trans); return ret; } @@ -1853,7 +1852,7 @@ void btrfs_assign_next_active_device(struct btrfs_fs_info *fs_info, fs_info->fs_devices->latest_bdev = next_device->bdev; } -int btrfs_rm_device(struct btrfs_root *root, char *device_path, u64 devid) +int btrfs_rm_device(struct btrfs_fs_info *fs_info, char *device_path, u64 devid) { struct btrfs_device *device; struct btrfs_fs_devices *cur_devices; @@ -1863,20 +1862,20 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path, u64 devid) mutex_lock(&uuid_mutex); - num_devices = root->fs_info->fs_devices->num_devices; - btrfs_dev_replace_lock(&root->fs_info->dev_replace, 0); - if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) { + num_devices = fs_info->fs_devices->num_devices; + btrfs_dev_replace_lock(&fs_info->dev_replace, 0); + if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) { WARN_ON(num_devices < 1); num_devices--; } - btrfs_dev_replace_unlock(&root->fs_info->dev_replace, 0); + btrfs_dev_replace_unlock(&fs_info->dev_replace, 0); - ret = btrfs_check_raid_min_devices(root->fs_info, num_devices - 1); + ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1); if (ret) goto out; - ret = btrfs_find_device_by_devspec(root, devid, device_path, - &device); + ret = btrfs_find_device_by_devspec(fs_info, devid, device_path, + &device); if (ret) goto out; @@ -1885,16 +1884,16 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path, u64 devid) goto out; } - if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) { + if (device->writeable && fs_info->fs_devices->rw_devices == 1) { ret = BTRFS_ERROR_DEV_ONLY_WRITABLE; goto out; } if (device->writeable) { - lock_chunks(root); + mutex_lock(&fs_info->chunk_mutex); list_del_init(&device->dev_alloc_list); device->fs_devices->rw_devices--; - unlock_chunks(root); + mutex_unlock(&fs_info->chunk_mutex); clear_super = true; } @@ -1909,12 +1908,12 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path, u64 devid) * counter although write_all_supers() is not locked out. This * could give a filesystem state which requires a degraded mount. */ - ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device); + ret = btrfs_rm_dev_item(fs_info, device); if (ret) goto error_undo; device->in_fs_metadata = 0; - btrfs_scrub_cancel_dev(root->fs_info, device); + btrfs_scrub_cancel_dev(fs_info, device); /* * the device list mutex makes sure that we don't change @@ -1927,7 +1926,7 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path, u64 devid) */ cur_devices = device->fs_devices; - mutex_lock(&root->fs_info->fs_devices->device_list_mutex); + mutex_lock(&fs_info->fs_devices->device_list_mutex); list_del_rcu(&device->dev_list); device->fs_devices->num_devices--; @@ -1936,17 +1935,17 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path, u64 devid) if (device->missing) device->fs_devices->missing_devices--; - btrfs_assign_next_active_device(root->fs_info, device, NULL); + btrfs_assign_next_active_device(fs_info, device, NULL); if (device->bdev) { device->fs_devices->open_devices--; /* remove sysfs entry */ - btrfs_sysfs_rm_device_link(root->fs_info->fs_devices, device); + btrfs_sysfs_rm_device_link(fs_info->fs_devices, device); } - num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1; - btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices); - mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); + 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_info->fs_devices->device_list_mutex); /* * at this point, the device is zero sized and detached from @@ -1961,7 +1960,7 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path, u64 devid) if (cur_devices->open_devices == 0) { struct btrfs_fs_devices *fs_devices; - fs_devices = root->fs_info->fs_devices; + fs_devices = fs_info->fs_devices; while (fs_devices) { if (fs_devices->seed == cur_devices) { fs_devices->seed = cur_devices->seed; @@ -1974,8 +1973,8 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path, u64 devid) free_fs_devices(cur_devices); } - root->fs_info->num_tolerated_disk_barrier_failures = - btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info); + fs_info->num_tolerated_disk_barrier_failures = + btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); out: mutex_unlock(&uuid_mutex); @@ -1983,11 +1982,11 @@ out: error_undo: if (device->writeable) { - lock_chunks(root); + mutex_lock(&fs_info->chunk_mutex); list_add(&device->dev_alloc_list, - &root->fs_info->fs_devices->alloc_list); + &fs_info->fs_devices->alloc_list); device->fs_devices->rw_devices++; - unlock_chunks(root); + mutex_unlock(&fs_info->chunk_mutex); } goto out; } @@ -2092,7 +2091,8 @@ void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info, call_rcu(&tgtdev->rcu, free_device); } -static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path, +static int btrfs_find_device_by_path(struct btrfs_fs_info *fs_info, + char *device_path, struct btrfs_device **device) { int ret = 0; @@ -2104,14 +2104,13 @@ static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path, *device = NULL; ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ, - root->fs_info->bdev_holder, 0, &bdev, &bh); + fs_info->bdev_holder, 0, &bdev, &bh); if (ret) return ret; disk_super = (struct btrfs_super_block *)bh->b_data; devid = btrfs_stack_device_id(&disk_super->dev_item); dev_uuid = disk_super->dev_item.uuid; - *device = btrfs_find_device(root->fs_info, devid, dev_uuid, - disk_super->fsid); + *device = btrfs_find_device(fs_info, devid, dev_uuid, disk_super->fsid); brelse(bh); if (!*device) ret = -ENOENT; @@ -2119,7 +2118,7 @@ static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path, return ret; } -int btrfs_find_device_missing_or_by_path(struct btrfs_root *root, +int btrfs_find_device_missing_or_by_path(struct btrfs_fs_info *fs_info, char *device_path, struct btrfs_device **device) { @@ -2128,7 +2127,7 @@ int btrfs_find_device_missing_or_by_path(struct btrfs_root *root, struct list_head *devices; struct btrfs_device *tmp; - devices = &root->fs_info->fs_devices->devices; + devices = &fs_info->fs_devices->devices; /* * It is safe to read the devices since the volume_mutex * is held by the caller. @@ -2145,30 +2144,28 @@ int btrfs_find_device_missing_or_by_path(struct btrfs_root *root, return 0; } else { - return btrfs_find_device_by_path(root, device_path, device); + return btrfs_find_device_by_path(fs_info, device_path, device); } } /* * Lookup a device given by device id, or the path if the id is 0. */ -int btrfs_find_device_by_devspec(struct btrfs_root *root, u64 devid, - char *devpath, - struct btrfs_device **device) +int btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info, u64 devid, + char *devpath, struct btrfs_device **device) { int ret; if (devid) { ret = 0; - *device = btrfs_find_device(root->fs_info, devid, NULL, - NULL); + *device = btrfs_find_device(fs_info, devid, NULL, NULL); if (!*device) ret = -ENOENT; } else { if (!devpath || !devpath[0]) return -EINVAL; - ret = btrfs_find_device_missing_or_by_path(root, devpath, + ret = btrfs_find_device_missing_or_by_path(fs_info, devpath, device); } return ret; @@ -2177,12 +2174,12 @@ int btrfs_find_device_by_devspec(struct btrfs_root *root, u64 devid, /* * does all the dirty work required for changing file system's UUID. */ -static int btrfs_prepare_sprout(struct btrfs_root *root) +static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info) { - struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; + struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; struct btrfs_fs_devices *old_devices; struct btrfs_fs_devices *seed_devices; - struct btrfs_super_block *disk_super = root->fs_info->super_copy; + struct btrfs_super_block *disk_super = fs_info->super_copy; struct btrfs_device *device; u64 super_flags; @@ -2208,15 +2205,15 @@ static int btrfs_prepare_sprout(struct btrfs_root *root) INIT_LIST_HEAD(&seed_devices->alloc_list); mutex_init(&seed_devices->device_list_mutex); - mutex_lock(&root->fs_info->fs_devices->device_list_mutex); + mutex_lock(&fs_info->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; - lock_chunks(root); + mutex_lock(&fs_info->chunk_mutex); list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list); - unlock_chunks(root); + mutex_unlock(&fs_info->chunk_mutex); fs_devices->seeding = 0; fs_devices->num_devices = 0; @@ -2226,9 +2223,9 @@ static int btrfs_prepare_sprout(struct btrfs_root *root) fs_devices->seed = seed_devices; generate_random_uuid(fs_devices->fsid); - memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); + memcpy(fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); - mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); + mutex_unlock(&fs_info->fs_devices->device_list_mutex); super_flags = btrfs_super_flags(disk_super) & ~BTRFS_SUPER_FLAG_SEEDING; @@ -2241,8 +2238,9 @@ static int btrfs_prepare_sprout(struct btrfs_root *root) * Store the expected generation for seed devices in device items. */ static int btrfs_finish_sprout(struct btrfs_trans_handle *trans, - struct btrfs_root *root) + struct btrfs_fs_info *fs_info) { + struct btrfs_root *root = fs_info->chunk_root; struct btrfs_path *path; struct extent_buffer *leaf; struct btrfs_dev_item *dev_item; @@ -2257,7 +2255,6 @@ static int btrfs_finish_sprout(struct btrfs_trans_handle *trans, if (!path) return -ENOMEM; - root = root->fs_info->chunk_root; key.objectid = BTRFS_DEV_ITEMS_OBJECTID; key.offset = 0; key.type = BTRFS_DEV_ITEM_KEY; @@ -2293,8 +2290,7 @@ next_slot: BTRFS_UUID_SIZE); read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), BTRFS_UUID_SIZE); - device = btrfs_find_device(root->fs_info, devid, dev_uuid, - fs_uuid); + device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid); BUG_ON(!device); /* Logic error */ if (device->fs_devices->seeding) { @@ -2312,28 +2308,29 @@ error: return ret; } -int btrfs_init_new_device(struct btrfs_root *root, char *device_path) +int btrfs_init_new_device(struct btrfs_fs_info *fs_info, char *device_path) { + struct btrfs_root *root = fs_info->dev_root; struct request_queue *q; struct btrfs_trans_handle *trans; struct btrfs_device *device; struct block_device *bdev; struct list_head *devices; - struct super_block *sb = root->fs_info->sb; + struct super_block *sb = fs_info->sb; struct rcu_string *name; u64 tmp; int seeding_dev = 0; int ret = 0; - if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding) + if ((sb->s_flags & MS_RDONLY) && !fs_info->fs_devices->seeding) return -EROFS; bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, - root->fs_info->bdev_holder); + fs_info->bdev_holder); if (IS_ERR(bdev)) return PTR_ERR(bdev); - if (root->fs_info->fs_devices->seeding) { + if (fs_info->fs_devices->seeding) { seeding_dev = 1; down_write(&sb->s_umount); mutex_lock(&uuid_mutex); @@ -2341,20 +2338,20 @@ int btrfs_init_new_device(struct btrfs_root *root, char *device_path) filemap_write_and_wait(bdev->bd_inode->i_mapping); - devices = &root->fs_info->fs_devices->devices; + devices = &fs_info->fs_devices->devices; - mutex_lock(&root->fs_info->fs_devices->device_list_mutex); + mutex_lock(&fs_info->fs_devices->device_list_mutex); list_for_each_entry(device, devices, dev_list) { if (device->bdev == bdev) { ret = -EEXIST; mutex_unlock( - &root->fs_info->fs_devices->device_list_mutex); + &fs_info->fs_devices->device_list_mutex); goto error; } } - mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); + mutex_unlock(&fs_info->fs_devices->device_list_mutex); - device = btrfs_alloc_device(root->fs_info, NULL, NULL); + device = btrfs_alloc_device(fs_info, NULL, NULL); if (IS_ERR(device)) { /* we can safely leave the fs_devices entry around */ ret = PTR_ERR(device); @@ -2382,13 +2379,13 @@ int btrfs_init_new_device(struct btrfs_root *root, char *device_path) device->can_discard = 1; device->writeable = 1; device->generation = trans->transid; - device->io_width = root->sectorsize; - device->io_align = root->sectorsize; - device->sector_size = root->sectorsize; + device->io_width = fs_info->sectorsize; + device->io_align = fs_info->sectorsize; + device->sector_size = fs_info->sectorsize; device->total_bytes = i_size_read(bdev->bd_inode); device->disk_total_bytes = device->total_bytes; device->commit_total_bytes = device->total_bytes; - device->dev_root = root->fs_info->dev_root; + device->fs_info = fs_info; device->bdev = bdev; device->in_fs_metadata = 1; device->is_tgtdev_for_dev_replace = 0; @@ -2398,61 +2395,60 @@ int btrfs_init_new_device(struct btrfs_root *root, char *device_path) if (seeding_dev) { sb->s_flags &= ~MS_RDONLY; - ret = btrfs_prepare_sprout(root); + ret = btrfs_prepare_sprout(fs_info); BUG_ON(ret); /* -ENOMEM */ } - device->fs_devices = root->fs_info->fs_devices; + device->fs_devices = fs_info->fs_devices; - mutex_lock(&root->fs_info->fs_devices->device_list_mutex); - lock_chunks(root); - list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices); + mutex_lock(&fs_info->fs_devices->device_list_mutex); + mutex_lock(&fs_info->chunk_mutex); + list_add_rcu(&device->dev_list, &fs_info->fs_devices->devices); list_add(&device->dev_alloc_list, - &root->fs_info->fs_devices->alloc_list); - root->fs_info->fs_devices->num_devices++; - root->fs_info->fs_devices->open_devices++; - root->fs_info->fs_devices->rw_devices++; - root->fs_info->fs_devices->total_devices++; - root->fs_info->fs_devices->total_rw_bytes += device->total_bytes; + &fs_info->fs_devices->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; - spin_lock(&root->fs_info->free_chunk_lock); - root->fs_info->free_chunk_space += device->total_bytes; - spin_unlock(&root->fs_info->free_chunk_lock); + spin_lock(&fs_info->free_chunk_lock); + fs_info->free_chunk_space += device->total_bytes; + spin_unlock(&fs_info->free_chunk_lock); if (!blk_queue_nonrot(bdev_get_queue(bdev))) - root->fs_info->fs_devices->rotating = 1; + fs_info->fs_devices->rotating = 1; - tmp = btrfs_super_total_bytes(root->fs_info->super_copy); - btrfs_set_super_total_bytes(root->fs_info->super_copy, + tmp = btrfs_super_total_bytes(fs_info->super_copy); + btrfs_set_super_total_bytes(fs_info->super_copy, tmp + device->total_bytes); - tmp = btrfs_super_num_devices(root->fs_info->super_copy); - btrfs_set_super_num_devices(root->fs_info->super_copy, - tmp + 1); + tmp = btrfs_super_num_devices(fs_info->super_copy); + btrfs_set_super_num_devices(fs_info->super_copy, tmp + 1); /* add sysfs device entry */ - btrfs_sysfs_add_device_link(root->fs_info->fs_devices, device); + btrfs_sysfs_add_device_link(fs_info->fs_devices, device); /* * we've got more storage, clear any full flags on the space * infos */ - btrfs_clear_space_info_full(root->fs_info); + btrfs_clear_space_info_full(fs_info); - unlock_chunks(root); - mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); + mutex_unlock(&fs_info->chunk_mutex); + mutex_unlock(&fs_info->fs_devices->device_list_mutex); if (seeding_dev) { - lock_chunks(root); - ret = init_first_rw_device(trans, root, device); - unlock_chunks(root); + mutex_lock(&fs_info->chunk_mutex); + ret = init_first_rw_device(trans, fs_info, device); + mutex_unlock(&fs_info->chunk_mutex); if (ret) { btrfs_abort_transaction(trans, ret); goto error_trans; } } - ret = btrfs_add_device(trans, root, device); + ret = btrfs_add_device(trans, fs_info, device); if (ret) { btrfs_abort_transaction(trans, ret); goto error_trans; @@ -2461,7 +2457,7 @@ int btrfs_init_new_device(struct btrfs_root *root, char *device_path) if (seeding_dev) { char fsid_buf[BTRFS_UUID_UNPARSED_SIZE]; - ret = btrfs_finish_sprout(trans, root); + ret = btrfs_finish_sprout(trans, fs_info); if (ret) { btrfs_abort_transaction(trans, ret); goto error_trans; @@ -2471,16 +2467,15 @@ int btrfs_init_new_device(struct btrfs_root *root, char *device_path) * so rename the fsid on the sysfs */ snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", - root->fs_info->fsid); - if (kobject_rename(&root->fs_info->fs_devices->fsid_kobj, - fsid_buf)) - btrfs_warn(root->fs_info, - "sysfs: failed to create fsid for sprout"); + fs_info->fsid); + if (kobject_rename(&fs_info->fs_devices->fsid_kobj, fsid_buf)) + btrfs_warn(fs_info, + "sysfs: failed to create fsid for sprout"); } - root->fs_info->num_tolerated_disk_barrier_failures = - btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info); - ret = btrfs_commit_transaction(trans, root); + fs_info->num_tolerated_disk_barrier_failures = + btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); + ret = btrfs_commit_transaction(trans); if (seeding_dev) { mutex_unlock(&uuid_mutex); @@ -2489,9 +2484,9 @@ int btrfs_init_new_device(struct btrfs_root *root, char *device_path) if (ret) /* transaction commit */ return ret; - ret = btrfs_relocate_sys_chunks(root); + ret = btrfs_relocate_sys_chunks(fs_info); if (ret < 0) - btrfs_handle_fs_error(root->fs_info, ret, + 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)) { @@ -2499,7 +2494,7 @@ int btrfs_init_new_device(struct btrfs_root *root, char *device_path) return 0; return PTR_ERR(trans); } - ret = btrfs_commit_transaction(trans, root); + ret = btrfs_commit_transaction(trans); } /* Update ctime/mtime for libblkid */ @@ -2507,9 +2502,9 @@ int btrfs_init_new_device(struct btrfs_root *root, char *device_path) return ret; error_trans: - btrfs_end_transaction(trans, root); + btrfs_end_transaction(trans); rcu_string_free(device->name); - btrfs_sysfs_rm_device_link(root->fs_info->fs_devices, device); + btrfs_sysfs_rm_device_link(fs_info->fs_devices, device); kfree(device); error: blkdev_put(bdev, FMODE_EXCL); @@ -2520,14 +2515,14 @@ error: return ret; } -int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path, +int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info, + char *device_path, struct btrfs_device *srcdev, struct btrfs_device **device_out) { struct request_queue *q; struct btrfs_device *device; struct block_device *bdev; - struct btrfs_fs_info *fs_info = root->fs_info; struct list_head *devices; struct rcu_string *name; u64 devid = BTRFS_DEV_REPLACE_DEVID; @@ -2585,19 +2580,19 @@ int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path, q = bdev_get_queue(bdev); if (blk_queue_discard(q)) device->can_discard = 1; - mutex_lock(&root->fs_info->fs_devices->device_list_mutex); + mutex_lock(&fs_info->fs_devices->device_list_mutex); device->writeable = 1; device->generation = 0; - device->io_width = root->sectorsize; - device->io_align = root->sectorsize; - device->sector_size = root->sectorsize; + device->io_width = fs_info->sectorsize; + device->io_align = fs_info->sectorsize; + device->sector_size = fs_info->sectorsize; device->total_bytes = btrfs_device_get_total_bytes(srcdev); device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev); device->bytes_used = btrfs_device_get_bytes_used(srcdev); ASSERT(list_empty(&srcdev->resized_list)); device->commit_total_bytes = srcdev->commit_total_bytes; device->commit_bytes_used = device->bytes_used; - device->dev_root = fs_info->dev_root; + device->fs_info = fs_info; device->bdev = bdev; device->in_fs_metadata = 1; device->is_tgtdev_for_dev_replace = 1; @@ -2608,7 +2603,7 @@ int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path, list_add(&device->dev_list, &fs_info->fs_devices->devices); fs_info->fs_devices->num_devices++; fs_info->fs_devices->open_devices++; - mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); + mutex_unlock(&fs_info->fs_devices->device_list_mutex); *device_out = device; return ret; @@ -2621,11 +2616,13 @@ error: void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info, struct btrfs_device *tgtdev) { + u32 sectorsize = fs_info->sectorsize; + WARN_ON(fs_info->fs_devices->rw_devices == 0); - tgtdev->io_width = fs_info->dev_root->sectorsize; - tgtdev->io_align = fs_info->dev_root->sectorsize; - tgtdev->sector_size = fs_info->dev_root->sectorsize; - tgtdev->dev_root = fs_info->dev_root; + tgtdev->io_width = sectorsize; + tgtdev->io_align = sectorsize; + tgtdev->sector_size = sectorsize; + tgtdev->fs_info = fs_info; tgtdev->in_fs_metadata = 1; } @@ -2634,13 +2631,11 @@ static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, { int ret; struct btrfs_path *path; - struct btrfs_root *root; + struct btrfs_root *root = device->fs_info->chunk_root; struct btrfs_dev_item *dev_item; struct extent_buffer *leaf; struct btrfs_key key; - root = device->dev_root->fs_info->chunk_root; - path = btrfs_alloc_path(); if (!path) return -ENOMEM; @@ -2680,8 +2675,8 @@ out: int btrfs_grow_device(struct btrfs_trans_handle *trans, struct btrfs_device *device, u64 new_size) { - struct btrfs_super_block *super_copy = - device->dev_root->fs_info->super_copy; + struct btrfs_fs_info *fs_info = device->fs_info; + struct btrfs_super_block *super_copy = fs_info->super_copy; struct btrfs_fs_devices *fs_devices; u64 old_total; u64 diff; @@ -2689,41 +2684,41 @@ int btrfs_grow_device(struct btrfs_trans_handle *trans, if (!device->writeable) return -EACCES; - lock_chunks(device->dev_root); + mutex_lock(&fs_info->chunk_mutex); old_total = btrfs_super_total_bytes(super_copy); diff = new_size - device->total_bytes; if (new_size <= device->total_bytes || device->is_tgtdev_for_dev_replace) { - unlock_chunks(device->dev_root); + mutex_unlock(&fs_info->chunk_mutex); return -EINVAL; } - fs_devices = device->dev_root->fs_info->fs_devices; + fs_devices = fs_info->fs_devices; btrfs_set_super_total_bytes(super_copy, old_total + diff); device->fs_devices->total_rw_bytes += diff; btrfs_device_set_total_bytes(device, new_size); btrfs_device_set_disk_total_bytes(device, new_size); - btrfs_clear_space_info_full(device->dev_root->fs_info); + btrfs_clear_space_info_full(device->fs_info); if (list_empty(&device->resized_list)) list_add_tail(&device->resized_list, &fs_devices->resized_devices); - unlock_chunks(device->dev_root); + mutex_unlock(&fs_info->chunk_mutex); return btrfs_update_device(trans, device); } static int btrfs_free_chunk(struct btrfs_trans_handle *trans, - struct btrfs_root *root, u64 chunk_objectid, + struct btrfs_fs_info *fs_info, u64 chunk_objectid, u64 chunk_offset) { + struct btrfs_root *root = fs_info->chunk_root; int ret; struct btrfs_path *path; struct btrfs_key key; - root = root->fs_info->chunk_root; path = btrfs_alloc_path(); if (!path) return -ENOMEM; @@ -2736,25 +2731,25 @@ static int btrfs_free_chunk(struct btrfs_trans_handle *trans, if (ret < 0) goto out; else if (ret > 0) { /* Logic error or corruption */ - btrfs_handle_fs_error(root->fs_info, -ENOENT, - "Failed lookup while freeing chunk."); + btrfs_handle_fs_error(fs_info, -ENOENT, + "Failed lookup while freeing chunk."); ret = -ENOENT; goto out; } ret = btrfs_del_item(trans, root, path); if (ret < 0) - btrfs_handle_fs_error(root->fs_info, ret, - "Failed to delete chunk item."); + btrfs_handle_fs_error(fs_info, ret, + "Failed to delete chunk item."); out: btrfs_free_path(path); return ret; } -static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64 - chunk_offset) +static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, + u64 chunk_objectid, u64 chunk_offset) { - struct btrfs_super_block *super_copy = root->fs_info->super_copy; + struct btrfs_super_block *super_copy = fs_info->super_copy; struct btrfs_disk_key *disk_key; struct btrfs_chunk *chunk; u8 *ptr; @@ -2765,7 +2760,7 @@ static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64 u32 cur; struct btrfs_key key; - lock_chunks(root); + mutex_lock(&fs_info->chunk_mutex); array_size = btrfs_super_sys_array_size(super_copy); ptr = super_copy->sys_chunk_array; @@ -2795,25 +2790,22 @@ static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64 cur += len; } } - unlock_chunks(root); + mutex_unlock(&fs_info->chunk_mutex); return ret; } int btrfs_remove_chunk(struct btrfs_trans_handle *trans, - struct btrfs_root *root, u64 chunk_offset) + struct btrfs_fs_info *fs_info, u64 chunk_offset) { struct extent_map_tree *em_tree; struct extent_map *em; - struct btrfs_root *extent_root = root->fs_info->extent_root; struct map_lookup *map; u64 dev_extent_len = 0; u64 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; int i, ret = 0; - struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; + struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; - /* Just in case */ - root = root->fs_info->chunk_root; - em_tree = &root->fs_info->mapping_tree.map_tree; + em_tree = &fs_info->mapping_tree.map_tree; read_lock(&em_tree->lock); em = lookup_extent_mapping(em_tree, chunk_offset, 1); @@ -2832,9 +2824,9 @@ int btrfs_remove_chunk(struct btrfs_trans_handle *trans, return -EINVAL; } map = em->map_lookup; - lock_chunks(root->fs_info->chunk_root); - check_system_chunk(trans, extent_root, map->type); - unlock_chunks(root->fs_info->chunk_root); + mutex_lock(&fs_info->chunk_mutex); + check_system_chunk(trans, fs_info, map->type); + mutex_unlock(&fs_info->chunk_mutex); /* * Take the device list mutex to prevent races with the final phase of @@ -2854,14 +2846,14 @@ int btrfs_remove_chunk(struct btrfs_trans_handle *trans, } if (device->bytes_used > 0) { - lock_chunks(root); + mutex_lock(&fs_info->chunk_mutex); btrfs_device_set_bytes_used(device, device->bytes_used - dev_extent_len); - spin_lock(&root->fs_info->free_chunk_lock); - root->fs_info->free_chunk_space += dev_extent_len; - spin_unlock(&root->fs_info->free_chunk_lock); - btrfs_clear_space_info_full(root->fs_info); - unlock_chunks(root); + spin_lock(&fs_info->free_chunk_lock); + fs_info->free_chunk_space += dev_extent_len; + spin_unlock(&fs_info->free_chunk_lock); + btrfs_clear_space_info_full(fs_info); + mutex_unlock(&fs_info->chunk_mutex); } if (map->stripes[i].dev) { @@ -2875,23 +2867,24 @@ int btrfs_remove_chunk(struct btrfs_trans_handle *trans, } mutex_unlock(&fs_devices->device_list_mutex); - ret = btrfs_free_chunk(trans, root, chunk_objectid, chunk_offset); + ret = btrfs_free_chunk(trans, fs_info, chunk_objectid, chunk_offset); if (ret) { btrfs_abort_transaction(trans, ret); goto out; } - trace_btrfs_chunk_free(root, map, chunk_offset, em->len); + trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len); if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { - ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset); + ret = btrfs_del_sys_chunk(fs_info, chunk_objectid, + chunk_offset); if (ret) { btrfs_abort_transaction(trans, ret); goto out; } } - ret = btrfs_remove_block_group(trans, extent_root, chunk_offset, em); + ret = btrfs_remove_block_group(trans, fs_info, chunk_offset, em); if (ret) { btrfs_abort_transaction(trans, ret); goto out; @@ -2903,15 +2896,12 @@ out: return ret; } -static int btrfs_relocate_chunk(struct btrfs_root *root, u64 chunk_offset) +static |