Commit ebbede42 authored by Anand Jain's avatar Anand Jain Committed by David Sterba

btrfs: cleanup device states define BTRFS_DEV_STATE_WRITEABLE

Currently device state is being managed by each individual int
variable such as struct btrfs_device::writeable. Instead of that
declare device state BTRFS_DEV_STATE_WRITEABLE and use the
bit operations.
Signed-off-by: default avatarAnand Jain <anand.jain@oracle.com>
[ whitespace adjustments ]
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent 3c958bd2
...@@ -3393,7 +3393,8 @@ static int barrier_all_devices(struct btrfs_fs_info *info) ...@@ -3393,7 +3393,8 @@ static int barrier_all_devices(struct btrfs_fs_info *info)
continue; continue;
if (!dev->bdev) if (!dev->bdev)
continue; continue;
if (!dev->in_fs_metadata || !dev->writeable) if (!dev->in_fs_metadata ||
!test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
continue; continue;
write_dev_flush(dev); write_dev_flush(dev);
...@@ -3408,7 +3409,8 @@ static int barrier_all_devices(struct btrfs_fs_info *info) ...@@ -3408,7 +3409,8 @@ static int barrier_all_devices(struct btrfs_fs_info *info)
errors_wait++; errors_wait++;
continue; continue;
} }
if (!dev->in_fs_metadata || !dev->writeable) if (!dev->in_fs_metadata ||
!test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
continue; continue;
ret = wait_dev_flush(dev); ret = wait_dev_flush(dev);
...@@ -3505,7 +3507,8 @@ int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors) ...@@ -3505,7 +3507,8 @@ int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors)
total_errors++; total_errors++;
continue; continue;
} }
if (!dev->in_fs_metadata || !dev->writeable) if (!dev->in_fs_metadata ||
!test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
continue; continue;
btrfs_set_stack_device_generation(dev_item, 0); btrfs_set_stack_device_generation(dev_item, 0);
...@@ -3544,7 +3547,8 @@ int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors) ...@@ -3544,7 +3547,8 @@ int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors)
list_for_each_entry(dev, head, dev_list) { list_for_each_entry(dev, head, dev_list) {
if (!dev->bdev) if (!dev->bdev)
continue; continue;
if (!dev->in_fs_metadata || !dev->writeable) if (!dev->in_fs_metadata ||
!test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
continue; continue;
ret = wait_dev_supers(dev, max_mirrors); ret = wait_dev_supers(dev, max_mirrors);
......
...@@ -10878,7 +10878,7 @@ static int btrfs_trim_free_extents(struct btrfs_device *device, ...@@ -10878,7 +10878,7 @@ static int btrfs_trim_free_extents(struct btrfs_device *device,
*trimmed = 0; *trimmed = 0;
/* Not writeable = nothing to do. */ /* Not writeable = nothing to do. */
if (!device->writeable) if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
return 0; return 0;
/* No free space = nothing to do. */ /* No free space = nothing to do. */
......
...@@ -2026,7 +2026,8 @@ int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start, ...@@ -2026,7 +2026,8 @@ int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
bio->bi_iter.bi_sector = sector; bio->bi_iter.bi_sector = sector;
dev = bbio->stripes[bbio->mirror_num - 1].dev; dev = bbio->stripes[bbio->mirror_num - 1].dev;
btrfs_put_bbio(bbio); btrfs_put_bbio(bbio);
if (!dev || !dev->bdev || !dev->writeable) { if (!dev || !dev->bdev ||
!test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
btrfs_bio_counter_dec(fs_info); btrfs_bio_counter_dec(fs_info);
bio_put(bio); bio_put(bio);
return -EIO; return -EIO;
......
...@@ -1503,7 +1503,7 @@ static noinline int btrfs_ioctl_resize(struct file *file, ...@@ -1503,7 +1503,7 @@ static noinline int btrfs_ioctl_resize(struct file *file,
goto out_free; goto out_free;
} }
if (!device->writeable) { if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
btrfs_info(fs_info, btrfs_info(fs_info,
"resizer unable to apply on readonly device %llu", "resizer unable to apply on readonly device %llu",
devid); devid);
......
...@@ -4117,7 +4117,8 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start, ...@@ -4117,7 +4117,8 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
return -ENODEV; return -ENODEV;
} }
if (!is_dev_replace && !readonly && !dev->writeable) { if (!is_dev_replace && !readonly &&
!test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
mutex_unlock(&fs_info->fs_devices->device_list_mutex); mutex_unlock(&fs_info->fs_devices->device_list_mutex);
rcu_read_lock(); rcu_read_lock();
name = rcu_dereference(dev->name); name = rcu_dereference(dev->name);
......
...@@ -691,10 +691,13 @@ static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices, ...@@ -691,10 +691,13 @@ static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
device->generation = btrfs_super_generation(disk_super); device->generation = btrfs_super_generation(disk_super);
if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) { if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
device->writeable = 0; clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
fs_devices->seeding = 1; fs_devices->seeding = 1;
} else { } else {
device->writeable = !bdev_read_only(bdev); if (bdev_read_only(bdev))
clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
else
set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
} }
q = bdev_get_queue(bdev); q = bdev_get_queue(bdev);
...@@ -706,7 +709,8 @@ static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices, ...@@ -706,7 +709,8 @@ static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
device->mode = flags; device->mode = flags;
fs_devices->open_devices++; fs_devices->open_devices++;
if (device->writeable && device->devid != BTRFS_DEV_REPLACE_DEVID) { if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
device->devid != BTRFS_DEV_REPLACE_DEVID) {
fs_devices->rw_devices++; fs_devices->rw_devices++;
list_add(&device->dev_alloc_list, &fs_devices->alloc_list); list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
} }
...@@ -934,9 +938,9 @@ void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices, int step) ...@@ -934,9 +938,9 @@ void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices, int step)
device->bdev = NULL; device->bdev = NULL;
fs_devices->open_devices--; fs_devices->open_devices--;
} }
if (device->writeable) { if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
list_del_init(&device->dev_alloc_list); list_del_init(&device->dev_alloc_list);
device->writeable = 0; clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
if (!device->is_tgtdev_for_dev_replace) if (!device->is_tgtdev_for_dev_replace)
fs_devices->rw_devices--; fs_devices->rw_devices--;
} }
...@@ -968,7 +972,7 @@ static void btrfs_close_bdev(struct btrfs_device *device) ...@@ -968,7 +972,7 @@ static void btrfs_close_bdev(struct btrfs_device *device)
if (!device->bdev) if (!device->bdev)
return; return;
if (device->writeable) { if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
sync_blockdev(device->bdev); sync_blockdev(device->bdev);
invalidate_bdev(device->bdev); invalidate_bdev(device->bdev);
} }
...@@ -985,7 +989,7 @@ static void btrfs_prepare_close_one_device(struct btrfs_device *device) ...@@ -985,7 +989,7 @@ static void btrfs_prepare_close_one_device(struct btrfs_device *device)
if (device->bdev) if (device->bdev)
fs_devices->open_devices--; fs_devices->open_devices--;
if (device->writeable && if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
device->devid != BTRFS_DEV_REPLACE_DEVID) { device->devid != BTRFS_DEV_REPLACE_DEVID) {
list_del_init(&device->dev_alloc_list); list_del_init(&device->dev_alloc_list);
fs_devices->rw_devices--; fs_devices->rw_devices--;
...@@ -1943,12 +1947,13 @@ int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path, ...@@ -1943,12 +1947,13 @@ int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
goto out; goto out;
} }
if (device->writeable && fs_info->fs_devices->rw_devices == 1) { if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
fs_info->fs_devices->rw_devices == 1) {
ret = BTRFS_ERROR_DEV_ONLY_WRITABLE; ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
goto out; goto out;
} }
if (device->writeable) { if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
mutex_lock(&fs_info->chunk_mutex); mutex_lock(&fs_info->chunk_mutex);
list_del_init(&device->dev_alloc_list); list_del_init(&device->dev_alloc_list);
device->fs_devices->rw_devices--; device->fs_devices->rw_devices--;
...@@ -2010,7 +2015,7 @@ int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path, ...@@ -2010,7 +2015,7 @@ int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
* the devices list. All that's left is to zero out the old * the devices list. All that's left is to zero out the old
* supers and free the device. * supers and free the device.
*/ */
if (device->writeable) if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
btrfs_scratch_superblocks(device->bdev, device->name->str); btrfs_scratch_superblocks(device->bdev, device->name->str);
btrfs_close_bdev(device); btrfs_close_bdev(device);
...@@ -2037,7 +2042,7 @@ int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path, ...@@ -2037,7 +2042,7 @@ int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
return ret; return ret;
error_undo: error_undo:
if (device->writeable) { if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
mutex_lock(&fs_info->chunk_mutex); mutex_lock(&fs_info->chunk_mutex);
list_add(&device->dev_alloc_list, list_add(&device->dev_alloc_list,
&fs_info->fs_devices->alloc_list); &fs_info->fs_devices->alloc_list);
...@@ -2068,7 +2073,7 @@ void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info, ...@@ -2068,7 +2073,7 @@ void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
if (srcdev->missing) if (srcdev->missing)
fs_devices->missing_devices--; fs_devices->missing_devices--;
if (srcdev->writeable) if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state))
fs_devices->rw_devices--; fs_devices->rw_devices--;
if (srcdev->bdev) if (srcdev->bdev)
...@@ -2080,7 +2085,7 @@ void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info, ...@@ -2080,7 +2085,7 @@ void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
{ {
struct btrfs_fs_devices *fs_devices = srcdev->fs_devices; struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
if (srcdev->writeable) { if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) {
/* zero out the old super if it is writable */ /* zero out the old super if it is writable */
btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str); btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
} }
...@@ -2431,7 +2436,7 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path ...@@ -2431,7 +2436,7 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
} }
q = bdev_get_queue(bdev); q = bdev_get_queue(bdev);
device->writeable = 1; set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
device->generation = trans->transid; device->generation = trans->transid;
device->io_width = fs_info->sectorsize; device->io_width = fs_info->sectorsize;
device->io_align = fs_info->sectorsize; device->io_align = fs_info->sectorsize;
...@@ -2638,7 +2643,7 @@ int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info, ...@@ -2638,7 +2643,7 @@ int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
rcu_assign_pointer(device->name, name); rcu_assign_pointer(device->name, name);
mutex_lock(&fs_info->fs_devices->device_list_mutex); mutex_lock(&fs_info->fs_devices->device_list_mutex);
device->writeable = 1; set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
device->generation = 0; device->generation = 0;
device->io_width = fs_info->sectorsize; device->io_width = fs_info->sectorsize;
device->io_align = fs_info->sectorsize; device->io_align = fs_info->sectorsize;
...@@ -2738,7 +2743,7 @@ int btrfs_grow_device(struct btrfs_trans_handle *trans, ...@@ -2738,7 +2743,7 @@ int btrfs_grow_device(struct btrfs_trans_handle *trans,
u64 old_total; u64 old_total;
u64 diff; u64 diff;
if (!device->writeable) if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
return -EACCES; return -EACCES;
new_size = round_down(new_size, fs_info->sectorsize); new_size = round_down(new_size, fs_info->sectorsize);
...@@ -3558,7 +3563,7 @@ static int __btrfs_balance(struct btrfs_fs_info *fs_info) ...@@ -3558,7 +3563,7 @@ static int __btrfs_balance(struct btrfs_fs_info *fs_info)
old_size = btrfs_device_get_total_bytes(device); old_size = btrfs_device_get_total_bytes(device);
size_to_free = div_factor(old_size, 1); size_to_free = div_factor(old_size, 1);
size_to_free = min_t(u64, size_to_free, SZ_1M); size_to_free = min_t(u64, size_to_free, SZ_1M);
if (!device->writeable || if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) ||
btrfs_device_get_total_bytes(device) - btrfs_device_get_total_bytes(device) -
btrfs_device_get_bytes_used(device) > size_to_free || btrfs_device_get_bytes_used(device) > size_to_free ||
device->is_tgtdev_for_dev_replace) device->is_tgtdev_for_dev_replace)
...@@ -4441,7 +4446,7 @@ int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) ...@@ -4441,7 +4446,7 @@ int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
mutex_lock(&fs_info->chunk_mutex); mutex_lock(&fs_info->chunk_mutex);
btrfs_device_set_total_bytes(device, new_size); btrfs_device_set_total_bytes(device, new_size);
if (device->writeable) { if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
device->fs_devices->total_rw_bytes -= diff; device->fs_devices->total_rw_bytes -= diff;
atomic64_sub(diff, &fs_info->free_chunk_space); atomic64_sub(diff, &fs_info->free_chunk_space);
} }
...@@ -4566,7 +4571,7 @@ int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) ...@@ -4566,7 +4571,7 @@ int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
if (ret) { if (ret) {
mutex_lock(&fs_info->chunk_mutex); mutex_lock(&fs_info->chunk_mutex);
btrfs_device_set_total_bytes(device, old_size); btrfs_device_set_total_bytes(device, old_size);
if (device->writeable) if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
device->fs_devices->total_rw_bytes += diff; device->fs_devices->total_rw_bytes += diff;
atomic64_add(diff, &fs_info->free_chunk_space); atomic64_add(diff, &fs_info->free_chunk_space);
mutex_unlock(&fs_info->chunk_mutex); mutex_unlock(&fs_info->chunk_mutex);
...@@ -4726,7 +4731,7 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, ...@@ -4726,7 +4731,7 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
u64 max_avail; u64 max_avail;
u64 dev_offset; u64 dev_offset;
if (!device->writeable) { if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
WARN(1, KERN_ERR WARN(1, KERN_ERR
"BTRFS: read-only device in alloc_list\n"); "BTRFS: read-only device in alloc_list\n");
continue; continue;
...@@ -5085,8 +5090,8 @@ int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset) ...@@ -5085,8 +5090,8 @@ int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
miss_ndevs++; miss_ndevs++;
continue; continue;
} }
if (!test_bit(BTRFS_DEV_STATE_WRITEABLE,
if (!map->stripes[i].dev->writeable) { &map->stripes[i].dev->dev_state)) {
readonly = 1; readonly = 1;
goto end; goto end;
} }
...@@ -6255,7 +6260,8 @@ blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio, ...@@ -6255,7 +6260,8 @@ blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
for (dev_nr = 0; dev_nr < total_devs; dev_nr++) { for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
dev = bbio->stripes[dev_nr].dev; dev = bbio->stripes[dev_nr].dev;
if (!dev || !dev->bdev || if (!dev || !dev->bdev ||
(bio_op(first_bio) == REQ_OP_WRITE && !dev->writeable)) { (bio_op(first_bio) == REQ_OP_WRITE &&
!test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) {
bbio_error(bbio, first_bio, logical); bbio_error(bbio, first_bio, logical);
continue; continue;
} }
...@@ -6691,7 +6697,7 @@ static int read_one_dev(struct btrfs_fs_info *fs_info, ...@@ -6691,7 +6697,7 @@ static int read_one_dev(struct btrfs_fs_info *fs_info,
} }
if (device->fs_devices != fs_info->fs_devices) { if (device->fs_devices != fs_info->fs_devices) {
BUG_ON(device->writeable); BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state));
if (device->generation != if (device->generation !=
btrfs_device_generation(leaf, dev_item)) btrfs_device_generation(leaf, dev_item))
return -EINVAL; return -EINVAL;
...@@ -6699,7 +6705,8 @@ static int read_one_dev(struct btrfs_fs_info *fs_info, ...@@ -6699,7 +6705,8 @@ static int read_one_dev(struct btrfs_fs_info *fs_info,
fill_device_from_item(leaf, dev_item, device); fill_device_from_item(leaf, dev_item, device);
device->in_fs_metadata = 1; device->in_fs_metadata = 1;
if (device->writeable && !device->is_tgtdev_for_dev_replace) { if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
!device->is_tgtdev_for_dev_replace) {
device->fs_devices->total_rw_bytes += device->total_bytes; device->fs_devices->total_rw_bytes += device->total_bytes;
atomic64_add(device->total_bytes - device->bytes_used, atomic64_add(device->total_bytes - device->bytes_used,
&fs_info->free_chunk_space); &fs_info->free_chunk_space);
......
...@@ -47,6 +47,8 @@ struct btrfs_pending_bios { ...@@ -47,6 +47,8 @@ struct btrfs_pending_bios {
#define btrfs_device_data_ordered_init(device) do { } while (0) #define btrfs_device_data_ordered_init(device) do { } while (0)
#endif #endif
#define BTRFS_DEV_STATE_WRITEABLE (0)
struct btrfs_device { struct btrfs_device {
struct list_head dev_list; struct list_head dev_list;
struct list_head dev_alloc_list; struct list_head dev_alloc_list;
...@@ -69,7 +71,7 @@ struct btrfs_device { ...@@ -69,7 +71,7 @@ struct btrfs_device {
/* the mode sent to blkdev_get */ /* the mode sent to blkdev_get */
fmode_t mode; fmode_t mode;
int writeable; unsigned long dev_state;
int in_fs_metadata; int in_fs_metadata;
int missing; int missing;
int is_tgtdev_for_dev_replace; int is_tgtdev_for_dev_replace;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment