Skip to contentSkip to conversation

define BTRFS_DEV_STATE

PATCH Superseded Anand Jain 2 participants 11 messages Nov 29, 2017 Btrfs file system

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
author
#1 lore raw

PATCH 1/4 btrfs: cleanup device states define BTRFS_DEV_STATE_WRITEABLE

In reply to Anand Jain

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.

---
 fs/btrfs/disk-io.c     | 12 ++++++----
 fs/btrfs/extent-tree.c |  2 +-
 fs/btrfs/extent_io.c   |  3 ++-
 fs/btrfs/ioctl.c       |  2 +-
 fs/btrfs/scrub.c       |  3 ++-
 fs/btrfs/volumes.c     | 63 +++++++++++++++++++++++++++++---------------------
 fs/btrfs/volumes.h     |  4 +++-
 7 files changed, 54 insertions(+), 35 deletions(-)
fs/btrfs/disk-io.c +8 -4
index dfdab849037b..0d361b6713e1 100644
@@ -3563,7 +3563,8 @@ static int barrier_all_devices(struct btrfs_fs_info *info)
 			continue;
 		if (!dev->bdev)
 			continue;
-		if (!dev->in_fs_metadata || !dev->writeable)
+		if (!dev->in_fs_metadata ||
+			!test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
 			continue;
 
 		write_dev_flush(dev);
@@ -3578,7 +3579,8 @@ static int barrier_all_devices(struct btrfs_fs_info *info)
 			errors_wait++;
 			continue;
 		}
-		if (!dev->in_fs_metadata || !dev->writeable)
+		if (!dev->in_fs_metadata ||
+			!test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
 			continue;
 
 		ret = wait_dev_flush(dev);
@@ -3675,7 +3677,8 @@ int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors)
 			total_errors++;
 			continue;
 		}
-		if (!dev->in_fs_metadata || !dev->writeable)
+		if (!dev->in_fs_metadata ||
+			!test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
 			continue;
 
 		btrfs_set_stack_device_generation(dev_item, 0);
@@ -3714,7 +3717,8 @@ int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors)
 	list_for_each_entry_rcu(dev, head, dev_list) {
 		if (!dev->bdev)
 			continue;
-		if (!dev->in_fs_metadata || !dev->writeable)
+		if (!dev->in_fs_metadata ||
+			!test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
 			continue;
 
 		ret = wait_dev_supers(dev, max_mirrors);
fs/btrfs/extent-tree.c +1 -1
index e2d7e86b51d1..f81d928754e1 100644
@@ -10935,7 +10935,7 @@ static int btrfs_trim_free_extents(struct btrfs_device *device,
 	*trimmed = 0;
 
 	/* Not writeable = nothing to do. */
-	if (!device->writeable)
+	if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
 		return 0;
 
 	/* No free space = nothing to do. */
fs/btrfs/extent_io.c +2 -1
index 7fa50e12f18e..f51c797847c7 100644
@@ -2028,7 +2028,8 @@ int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
 	bio->bi_iter.bi_sector = sector;
 	dev = bbio->stripes[bbio->mirror_num - 1].dev;
 	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);
 		bio_put(bio);
 		return -EIO;
fs/btrfs/ioctl.c +1 -1
index 6c7a49faf4e0..8a74c83503d6 100644
@@ -1518,7 +1518,7 @@ static noinline int btrfs_ioctl_resize(struct file *file,
 		goto out_free;
 	}
 
-	if (!device->writeable) {
+	if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
 		btrfs_info(fs_info,
 			   "resizer unable to apply on readonly device %llu",
 		       devid);
fs/btrfs/scrub.c +2 -1
index e3f6c49e5c4d..e027e0de66a5 100644
@@ -4117,7 +4117,8 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
 		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);
 		rcu_read_lock();
 		name = rcu_dereference(dev->name);
fs/btrfs/volumes.c +37 -26

199 lines not on this page · raw message

fs/btrfs/volumes.h +3 -1
index ff15208344a7..2e376a422626 100644
@@ -47,6 +47,8 @@ struct btrfs_pending_bios {
 #define btrfs_device_data_ordered_init(device) do { } while (0)
 #endif
 
+#define BTRFS_DEV_STATE_WRITEABLE	(1UL << 1)
+
 struct btrfs_device {
 	struct list_head dev_list;
 	struct list_head dev_alloc_list;
@@ -69,7 +71,7 @@ struct btrfs_device {
 	/* the mode sent to blkdev_get */
 	fmode_t mode;
 
-	int writeable;
+	unsigned long dev_state;
 	int in_fs_metadata;
 	int missing;
 	int can_discard;
Signature · 1 line
2.15.0
author
#4 lore raw

PATCH 2/4 btrfs: cleanup device states define BTRFS_DEV_STATE_IN_FS_METADATA

In reply to Anand Jain

Currently device state is being managed by each individual int variable such as struct btrfs_device::in_fs_metadata. Instead of that declare device state BTRFS_DEV_STATE_IN_FS_METADATA and use the bit operations.

---
 fs/btrfs/disk-io.c | 21 ++++++++++++++-------
 fs/btrfs/scrub.c   |  3 ++-
 fs/btrfs/super.c   |  5 +++--
 fs/btrfs/volumes.c | 29 +++++++++++++++++------------
 fs/btrfs/volumes.h |  2 +-
 5 files changed, 37 insertions(+), 23 deletions(-)
fs/btrfs/disk-io.c +14 -7
index 0d361b6713e1..ab1a514e5c8d 100644
@@ -3563,8 +3563,10 @@ static int barrier_all_devices(struct btrfs_fs_info *info)
 			continue;
 		if (!dev->bdev)
 			continue;
-		if (!dev->in_fs_metadata ||
-			!test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
+		if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
+						&dev->dev_state) ||
+			!test_bit(BTRFS_DEV_STATE_WRITEABLE,
+						&dev->dev_state))
 			continue;
 
 		write_dev_flush(dev);
@@ -3579,8 +3581,10 @@ static int barrier_all_devices(struct btrfs_fs_info *info)
 			errors_wait++;
 			continue;
 		}
-		if (!dev->in_fs_metadata ||
-			!test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
+		if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
+						&dev->dev_state) ||
+			!test_bit(BTRFS_DEV_STATE_WRITEABLE,
+						&dev->dev_state))
 			continue;
 
 		ret = wait_dev_flush(dev);
@@ -3677,7 +3681,8 @@ int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors)
 			total_errors++;
 			continue;
 		}
-		if (!dev->in_fs_metadata ||
+		if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
+						&dev->dev_state) ||
 			!test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
 			continue;
 
@@ -3717,8 +3722,10 @@ int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors)
 	list_for_each_entry_rcu(dev, head, dev_list) {
 		if (!dev->bdev)
 			continue;
-		if (!dev->in_fs_metadata ||
-			!test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
+		if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
+						&dev->dev_state) ||
+			!test_bit(BTRFS_DEV_STATE_WRITEABLE,
+						&dev->dev_state))
 			continue;
 
 		ret = wait_dev_supers(dev, max_mirrors);
fs/btrfs/scrub.c +2 -1
index e027e0de66a5..060fa93731e5 100644
@@ -4129,7 +4129,8 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
 	}
 
 	mutex_lock(&fs_info->scrub_lock);
-	if (!dev->in_fs_metadata || dev->is_tgtdev_for_dev_replace) {
+	if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
+					dev->is_tgtdev_for_dev_replace) {
 		mutex_unlock(&fs_info->scrub_lock);
 		mutex_unlock(&fs_info->fs_devices->device_list_mutex);
 		return -EIO;
fs/btrfs/super.c +3 -2
index 161694b66038..9089aad2f3aa 100644
@@ -1945,8 +1945,9 @@ static int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info,
 
 	rcu_read_lock();
 	list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
-		if (!device->in_fs_metadata || !device->bdev ||
-		    device->is_tgtdev_for_dev_replace)
+		if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
+						&device->dev_state) ||
+			!device->bdev || device->is_tgtdev_for_dev_replace)
 			continue;
 
 		if (i >= nr_devices)
fs/btrfs/volumes.c +17 -12
index 0f5be1808c6e..13a6ae80bee1 100644
@@ -652,7 +652,7 @@ static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
 		fs_devices->rotating = 1;
 
 	device->bdev = bdev;
-	device->in_fs_metadata = 0;
+	clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
 	device->mode = flags;
 
 	fs_devices->open_devices++;
@@ -857,7 +857,8 @@ void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices, int step)
 again:
 	/* 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 (device->in_fs_metadata) {
+		if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
+							&device->dev_state)) {
 			if (!device->is_tgtdev_for_dev_replace &&
 			    (!latest_dev ||
 			     device->generation > latest_dev->generation)) {
@@ -1600,7 +1601,7 @@ static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
 	struct extent_buffer *leaf;
 	struct btrfs_key key;
 
-	WARN_ON(!device->in_fs_metadata);
+	WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
 	WARN_ON(device->is_tgtdev_for_dev_replace);
 	path = btrfs_alloc_path();
 	if (!path)
@@ -1936,7 +1937,7 @@ int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
 	if (ret)
 		goto error_undo;
 
-	device->in_fs_metadata = 0;
+	clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
 	btrfs_scrub_cancel_dev(fs_info, device);
 
 	/*
@@ -2154,7 +2155,8 @@ int btrfs_find_device_missing_or_by_path(struct btrfs_fs_info *fs_info,
 		 * is held by the caller.
 		 */
 		list_for_each_entry(tmp, devices, dev_list) {
-			if (tmp->in_fs_metadata && !tmp->bdev) {
+			if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
+					&tmp->dev_state) && !tmp->bdev) {
 				*device = tmp;
 				break;
 			}
@@ -2410,7 +2412,7 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
 	device->commit_total_bytes = device->total_bytes;
 	device->fs_info = fs_info;
 	device->bdev = bdev;
-	device->in_fs_metadata = 1;
+	set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
 	device->is_tgtdev_for_dev_replace = 0;
 	device->mode = FMODE_EXCL;
 	device->dev_stats_valid = 1;
@@ -2613,7 +2615,7 @@ int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
 	device->commit_bytes_used = device->bytes_used;
 	device->fs_info = fs_info;
 	device->bdev = bdev;
-	device->in_fs_metadata = 1;
+	set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
 	device->is_tgtdev_for_dev_replace = 1;
 	device->mode = FMODE_EXCL;
 	device->dev_stats_valid = 1;
@@ -2642,7 +2644,7 @@ void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
 	tgtdev->io_align = sectorsize;
 	tgtdev->sector_size = sectorsize;
 	tgtdev->fs_info = fs_info;
-	tgtdev->in_fs_metadata = 1;
+	set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &tgtdev->dev_state);
 }
 
 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
@@ -4694,8 +4696,9 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
 			continue;
 		}
 
-		if (!device->in_fs_metadata ||
-		    device->is_tgtdev_for_dev_replace)
+		if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
+					&device->dev_state) ||
+					device->is_tgtdev_for_dev_replace)
 			continue;
 
 		if (device->total_bytes > device->bytes_used)
@@ -6498,7 +6501,9 @@ static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
 			}
 			btrfs_report_missing_device(fs_info, devid, uuid, false);
 		}
-		map->stripes[i].dev->in_fs_metadata = 1;
+		set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
+				&(map->stripes[i].dev->dev_state));
+
 	}
 
 	write_lock(&map_tree->map_tree.lock);
@@ -6673,7 +6678,7 @@ static int read_one_dev(struct btrfs_fs_info *fs_info,
 	}
 
 	fill_device_from_item(leaf, dev_item, device);
-	device->in_fs_metadata = 1;
+	set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
 	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;
fs/btrfs/volumes.h +1 -1
index 2e376a422626..f73b78f26a61 100644
@@ -48,6 +48,7 @@ struct btrfs_pending_bios {
 #endif
 
 #define BTRFS_DEV_STATE_WRITEABLE	(1UL << 1)
+#define BTRFS_DEV_STATE_IN_FS_METADATA	(1UL << 2)
 
 struct btrfs_device {
 	struct list_head dev_list;
@@ -72,7 +73,6 @@ struct btrfs_device {
 	fmode_t mode;
 
 	unsigned long dev_state;
-	int in_fs_metadata;
 	int missing;
 	int can_discard;
 	int is_tgtdev_for_dev_replace;
Signature · 1 line
2.15.0

Anand Jain

author
#0 lore raw

cover define BTRFS_DEV_STATE

As of now device properties and states are being represented as int variable. Patches in the ML such as device failed state needs this cleanup as well.

Anand Jain (4):
btrfs: cleanup device states define BTRFS_DEV_STATE_WRITEABLE
btrfs: cleanup device states define BTRFS_DEV_STATE_IN_FS_METADATA
btrfs: cleanup device states define BTRFS_DEV_STATE_MISSING
btrfs: cleanup device states define BTRFS_DEV_STATE_CAN_DISCARD

 fs/btrfs/dev-replace.c |   3 +-
 fs/btrfs/disk-io.c     |  23 ++++++---
 fs/btrfs/extent-tree.c |   5 +-
 fs/btrfs/extent_io.c   |   3 +-
 fs/btrfs/ioctl.c       |   2 +-
 fs/btrfs/scrub.c       |  13 +++--
 fs/btrfs/super.c       |   7 +--
 fs/btrfs/volumes.c     | 129 ++++++++++++++++++++++++++++---------------------
 fs/btrfs/volumes.h     |  10 ++--
 9 files changed, 118 insertions(+), 77 deletions(-)
Signature · 1 line
2.15.0
author
#6 lore raw

PATCH 3/4 btrfs: cleanup device states define BTRFS_DEV_STATE_MISSING

Currently device state is being managed by each individual int variable such as struct btrfs_device::missing. Instead of that declare btrfs_device::dev_state BTRFS_DEV_STATE_MISSING and use the bit operations.

---
 fs/btrfs/dev-replace.c |  3 ++-
 fs/btrfs/disk-io.c     |  4 ++--
 fs/btrfs/scrub.c       |  7 ++++---
 fs/btrfs/super.c       |  2 +-
 fs/btrfs/volumes.c     | 33 +++++++++++++++++++--------------
 fs/btrfs/volumes.h     |  2 +-
 6 files changed, 29 insertions(+), 22 deletions(-)
fs/btrfs/dev-replace.c +2 -1
index 4b6ceb38cb5f..559db7667f38 100644
@@ -306,7 +306,8 @@ void btrfs_after_dev_replace_commit(struct btrfs_fs_info *fs_info)
 
 static inline char *dev_missing_or_rcu_str(struct btrfs_device *device)
 {
-	return device->missing ? "<missing disk>" : rcu_str_deref(device->name);
+	return test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state) ?
+				"<missing disk>" : rcu_str_deref(device->name);
 }
 
 int btrfs_dev_replace_start(struct btrfs_fs_info *fs_info,
fs/btrfs/disk-io.c +2 -2
index ab1a514e5c8d..ac1079fc9382 100644
@@ -3559,7 +3559,7 @@ static int barrier_all_devices(struct btrfs_fs_info *info)
 	/* send down all the barriers */
 	head = &info->fs_devices->devices;
 	list_for_each_entry_rcu(dev, head, dev_list) {
-		if (dev->missing)
+		if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
 			continue;
 		if (!dev->bdev)
 			continue;
@@ -3575,7 +3575,7 @@ static int barrier_all_devices(struct btrfs_fs_info *info)
 
 	/* wait for all the barriers */
 	list_for_each_entry_rcu(dev, head, dev_list) {
-		if (dev->missing)
+		if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
 			continue;
 		if (!dev->bdev) {
 			errors_wait++;
fs/btrfs/scrub.c +4 -3
index 060fa93731e5..666c00cbee03 100644
@@ -2535,7 +2535,7 @@ static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
 	}
 
 	WARN_ON(sblock->page_count == 0);
-	if (dev->missing) {
+	if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) {
 		/*
 		 * This case should only be hit for RAID 5/6 device replace. See
 		 * the comment in scrub_missing_raid56_pages() for details.
@@ -2870,7 +2870,7 @@ static int scrub_extent_for_parity(struct scrub_parity *sparity,
 	u8 csum[BTRFS_CSUM_SIZE];
 	u32 blocksize;
 
-	if (dev->missing) {
+	if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) {
 		scrub_parity_mark_sectors_error(sparity, logical, len);
 		return 0;
 	}
@@ -4112,7 +4112,8 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
 
 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
 	dev = btrfs_find_device(fs_info, devid, NULL, NULL);
-	if (!dev || (dev->missing && !is_dev_replace)) {
+	if (!dev || (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) &&
+						!is_dev_replace)) {
 		mutex_unlock(&fs_info->fs_devices->device_list_mutex);
 		return -ENODEV;
 	}
fs/btrfs/super.c +1 -1
index 9089aad2f3aa..4b70ae6f0245 100644
@@ -2243,7 +2243,7 @@ static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
 	while (cur_devices) {
 		head = &cur_devices->devices;
 		list_for_each_entry(dev, head, dev_list) {
-			if (dev->missing)
+			if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
 				continue;
 			if (!dev->name)
 				continue;
fs/btrfs/volumes.c +19 -14
index 13a6ae80bee1..75839e07ce10 100644
@@ -773,9 +773,9 @@ static noinline int device_list_add(const char *path,
 			return -ENOMEM;
 		rcu_string_free(device->name);
 		rcu_assign_pointer(device->name, name);
-		if (device->missing) {
+		if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
 			fs_devices->missing_devices--;
-			device->missing = 0;
+			clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
 		}
 	}
 
@@ -957,7 +957,7 @@ static void btrfs_prepare_close_one_device(struct btrfs_device *device)
 		fs_devices->rw_devices--;
 	}
 
-	if (device->missing)
+	if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
 		fs_devices->missing_devices--;
 
 	new_device = btrfs_alloc_device(NULL, &device->devid,
@@ -1844,7 +1844,8 @@ struct btrfs_device *btrfs_find_next_active_device(struct btrfs_fs_devices *fs_d
 
 	list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
 		if (next_device != device &&
-			!next_device->missing && next_device->bdev)
+			!test_bit(BTRFS_DEV_STATE_MISSING,
+				&next_device->dev_state) && next_device->bdev)
 			return next_device;
 	}
 
@@ -1957,7 +1958,7 @@ int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
 	device->fs_devices->num_devices--;
 	device->fs_devices->total_devices--;
 
-	if (device->missing)
+	if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
 		device->fs_devices->missing_devices--;
 
 	btrfs_assign_next_active_device(fs_info, device, NULL);
@@ -2031,7 +2032,7 @@ void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
 	list_del_rcu(&srcdev->dev_list);
 	list_del_rcu(&srcdev->dev_alloc_list);
 	fs_devices->num_devices--;
-	if (srcdev->missing)
+	if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state))
 		fs_devices->missing_devices--;
 
 	if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state))
@@ -5048,7 +5049,7 @@ int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
 	for (i = 0; i < map->num_stripes; i++) {
 		struct btrfs_device *device = map->stripes[i].dev;
 
-		if (device->missing) {
+		if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
 			miss_ndevs++;
 			continue;
 		}
@@ -6079,7 +6080,8 @@ static noinline void btrfs_schedule_bio(struct btrfs_device *device,
 	int should_queue = 1;
 	struct btrfs_pending_bios *pending_bios;
 
-	if (device->missing || !device->bdev) {
+	if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state) ||
+			!device->bdev) {
 		bio_io_error(bio);
 		return;
 	}
@@ -6282,7 +6284,7 @@ static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices,
 	device->fs_devices = fs_devices;
 	fs_devices->num_devices++;
 
-	device->missing = 1;
+	set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
 	fs_devices->missing_devices++;
 
 	return device;
@@ -6644,7 +6646,8 @@ static int read_one_dev(struct btrfs_fs_info *fs_info,
 							dev_uuid, false);
 		}
 
-		if(!device->bdev && !device->missing) {
+		if (!device->bdev && !test_bit(BTRFS_DEV_STATE_MISSING,
+							&device->dev_state)) {
 			/*
 			 * this happens when a device that was properly setup
 			 * in the device info lists suddenly goes bad.
@@ -6652,12 +6655,13 @@ static int read_one_dev(struct btrfs_fs_info *fs_info,
 			 * device->missing to one here
 			 */
 			device->fs_devices->missing_devices++;
-			device->missing = 1;
+			set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
 		}
 
 		/* Move the device to its own fs_devices */
 		if (device->fs_devices != fs_devices) {
-			ASSERT(device->missing);
+			ASSERT(test_bit(BTRFS_DEV_STATE_MISSING,
+							&device->dev_state));
 
 			list_move(&device->dev_list, &fs_devices->devices);
 			device->fs_devices->num_devices--;
@@ -6843,8 +6847,9 @@ bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info)
 		for (i = 0; i < map->num_stripes; i++) {
 			struct btrfs_device *dev = map->stripes[i].dev;
 
-			if (!dev || !dev->bdev || dev->missing ||
-			    dev->last_flush_error)
+			if (!dev || !dev->bdev ||
+				test_bit(BTRFS_DEV_STATE_MISSING,
+				&dev->dev_state) || dev->last_flush_error)
 				missing++;
 		}
 		if (missing > max_tolerated) {
fs/btrfs/volumes.h +1 -1
index f73b78f26a61..2fbff6902c8d 100644
@@ -49,6 +49,7 @@ struct btrfs_pending_bios {
 
 #define BTRFS_DEV_STATE_WRITEABLE	(1UL << 1)
 #define BTRFS_DEV_STATE_IN_FS_METADATA	(1UL << 2)
+#define BTRFS_DEV_STATE_MISSING		(1UL << 3)
 
 struct btrfs_device {
 	struct list_head dev_list;
@@ -73,7 +74,6 @@ struct btrfs_device {
 	fmode_t mode;
 
 	unsigned long dev_state;
-	int missing;
 	int can_discard;
 	int is_tgtdev_for_dev_replace;
 	blk_status_t last_flush_error;
Signature · 1 line
2.15.0
author
#8 lore raw

PATCH 4/4 btrfs: cleanup device states define BTRFS_DEV_STATE_CAN_DISCARD

In reply to Anand Jain

Currently device state is being managed by each individual int variable such as struct btrfs_device::can_discard. Instead of that declare btrfs_device::dev_state BTRFS_DEV_STATE_CAN_DISCARD and use the bit operations.

---
 fs/btrfs/extent-tree.c | 3 ++-
 fs/btrfs/volumes.c     | 6 +++---
 fs/btrfs/volumes.h     | 2 +-
 3 files changed, 6 insertions(+), 5 deletions(-)
fs/btrfs/extent-tree.c +2 -1
index f81d928754e1..ee79f7cdc543 100644
@@ -2155,7 +2155,8 @@ int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
 
 		for (i = 0; i < bbio->num_stripes; i++, stripe++) {
 			u64 bytes;
-			if (!stripe->dev->can_discard)
+			if (!test_bit(BTRFS_DEV_STATE_CAN_DISCARD,
+						&stripe->dev->dev_state))
 				continue;
 
 			ret = btrfs_issue_discard(stripe->dev->bdev,
fs/btrfs/volumes.c +3 -3
index 75839e07ce10..a9c6486f06f4 100644
@@ -647,7 +647,7 @@ static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
 
 	q = bdev_get_queue(bdev);
 	if (blk_queue_discard(q))
-		device->can_discard = 1;
+		set_bit(BTRFS_DEV_STATE_CAN_DISCARD, &device->dev_state);
 	if (!blk_queue_nonrot(q))
 		fs_devices->rotating = 1;
 
@@ -2401,7 +2401,7 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
 
 	q = bdev_get_queue(bdev);
 	if (blk_queue_discard(q))
-		device->can_discard = 1;
+		set_bit(BTRFS_DEV_STATE_CAN_DISCARD, &device->dev_state);
 	set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
 	device->generation = trans->transid;
 	device->io_width = fs_info->sectorsize;
@@ -2601,7 +2601,7 @@ int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
 
 	q = bdev_get_queue(bdev);
 	if (blk_queue_discard(q))
-		device->can_discard = 1;
+		set_bit(BTRFS_DEV_STATE_CAN_DISCARD, &device->dev_state);
 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
 	set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
 	device->generation = 0;
fs/btrfs/volumes.h +1 -1
index 2fbff6902c8d..85e4b2dcc071 100644
@@ -50,6 +50,7 @@ struct btrfs_pending_bios {
 #define BTRFS_DEV_STATE_WRITEABLE	(1UL << 1)
 #define BTRFS_DEV_STATE_IN_FS_METADATA	(1UL << 2)
 #define BTRFS_DEV_STATE_MISSING		(1UL << 3)
+#define BTRFS_DEV_STATE_CAN_DISCARD	(1UL << 4)
 
 struct btrfs_device {
 	struct list_head dev_list;
@@ -74,7 +75,6 @@ struct btrfs_device {
 	fmode_t mode;
 
 	unsigned long dev_state;
-	int can_discard;
 	int is_tgtdev_for_dev_replace;
 	blk_status_t last_flush_error;
 	int flush_bio_sent;
Signature · 1 line
2.15.0
#2 lore raw

Re: [PATCH 1/4] btrfs: cleanup device states define BTRFS_DEV_STATE_WRITEABLE

In reply to Anand Jain
Quoted 293 lines · Anand Jain

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.

---
 fs/btrfs/disk-io.c     | 12 ++++++----
 fs/btrfs/extent-tree.c |  2 +-
 fs/btrfs/extent_io.c   |  3 ++-
 fs/btrfs/ioctl.c       |  2 +-
 fs/btrfs/scrub.c       |  3 ++-
 fs/btrfs/volumes.c     | 63 +++++++++++++++++++++++++++++---------------------
 fs/btrfs/volumes.h     |  4 +++-
 7 files changed, 54 insertions(+), 35 deletions(-)
fs/btrfs/disk-io.c +8 -4
index dfdab849037b..0d361b6713e1 100644
@@ -3563,7 +3563,8 @@ static int barrier_all_devices(struct btrfs_fs_info *info)
 			continue;
 		if (!dev->bdev)
 			continue;
-		if (!dev->in_fs_metadata || !dev->writeable)
+		if (!dev->in_fs_metadata ||
+			!test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
 			continue;
 
 		write_dev_flush(dev);
@@ -3578,7 +3579,8 @@ static int barrier_all_devices(struct btrfs_fs_info *info)
 			errors_wait++;
 			continue;
 		}
-		if (!dev->in_fs_metadata || !dev->writeable)
+		if (!dev->in_fs_metadata ||
+			!test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
 			continue;
 
 		ret = wait_dev_flush(dev);
@@ -3675,7 +3677,8 @@ int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors)
 			total_errors++;
 			continue;
 		}
-		if (!dev->in_fs_metadata || !dev->writeable)
+		if (!dev->in_fs_metadata ||
+			!test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
 			continue;
 
 		btrfs_set_stack_device_generation(dev_item, 0);
@@ -3714,7 +3717,8 @@ int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors)
 	list_for_each_entry_rcu(dev, head, dev_list) {
 		if (!dev->bdev)
 			continue;
-		if (!dev->in_fs_metadata || !dev->writeable)
+		if (!dev->in_fs_metadata ||
+			!test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
 			continue;
 
 		ret = wait_dev_supers(dev, max_mirrors);
fs/btrfs/extent-tree.c +1 -1
index e2d7e86b51d1..f81d928754e1 100644
@@ -10935,7 +10935,7 @@ static int btrfs_trim_free_extents(struct btrfs_device *device,
 	*trimmed = 0;
 
 	/* Not writeable = nothing to do. */
-	if (!device->writeable)
+	if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
 		return 0;
 
 	/* No free space = nothing to do. */
fs/btrfs/extent_io.c +2 -1
index 7fa50e12f18e..f51c797847c7 100644
@@ -2028,7 +2028,8 @@ int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
 	bio->bi_iter.bi_sector = sector;
 	dev = bbio->stripes[bbio->mirror_num - 1].dev;
 	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);
 		bio_put(bio);
 		return -EIO;
fs/btrfs/ioctl.c +1 -1
index 6c7a49faf4e0..8a74c83503d6 100644
@@ -1518,7 +1518,7 @@ static noinline int btrfs_ioctl_resize(struct file *file,
 		goto out_free;
 	}
 
-	if (!device->writeable) {
+	if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
 		btrfs_info(fs_info,
 			   "resizer unable to apply on readonly device %llu",
 		       devid);
fs/btrfs/scrub.c +2 -1
index e3f6c49e5c4d..e027e0de66a5 100644
@@ -4117,7 +4117,8 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
 		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);
 		rcu_read_lock();
 		name = rcu_dereference(dev->name);
fs/btrfs/volumes.c +28 -20
index 397b335d108c..0f5be1808c6e 100644
@@ -634,10 +634,15 @@ static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
 	device->generation = btrfs_super_generation(disk_super);
 
 	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;
 	} 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);
@@ -651,7 +656,7 @@ static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
 	device->mode = flags;
 
 	fs_devices->open_devices++;
-	if (device->writeable &&
+	if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
 	    device->devid != BTRFS_DEV_REPLACE_DEVID) {
 		fs_devices->rw_devices++;
 		list_add(&device->dev_alloc_list,
@@ -881,9 +886,10 @@ void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices, int step)
 			device->bdev = NULL;
 			fs_devices->open_devices--;
 		}
-		if (device->writeable) {
+		if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
 			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)
 				fs_devices->rw_devices--;
 		}
@@ -925,7 +931,8 @@ static void free_device(struct rcu_head *head)
 
 static void btrfs_close_bdev(struct btrfs_device *device)
 {
-	if (device->bdev && device->writeable) {
+	if (device->bdev &&
+		test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
 		sync_blockdev(device->bdev);
 		invalidate_bdev(device->bdev);
 	}
@@ -943,7 +950,7 @@ static void btrfs_prepare_close_one_device(struct btrfs_device *device)
 	if (device->bdev)
 		fs_devices->open_devices--;
 
-	if (device->writeable &&
+	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--;
@@ -1901,12 +1908,13 @@ int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
 		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;
 		goto out;
 	}
 
-	if (device->writeable) {
+	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--;
@@ -1968,7 +1976,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
 	 * 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_close_bdev(device);
@@ -1994,7 +2002,7 @@ int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
 	return ret;
 
 error_undo:
-	if (device->writeable) {
+	if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
 		mutex_lock(&fs_info->chunk_mutex);
 		list_add(&device->dev_alloc_list,
 			 &fs_info->fs_devices->alloc_list);
@@ -2025,7 +2033,7 @@ void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
 	if (srcdev->missing)
 		fs_devices->missing_devices--;
 
-	if (srcdev->writeable)
+	if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state))
 		fs_devices->rw_devices--;
 
 	if (srcdev->bdev)
@@ -2037,7 +2045,7 @@ void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
 {
 	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 */
 		btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
 	}
@@ -2391,7 +2399,7 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
 	q = bdev_get_queue(bdev);
 	if (blk_queue_discard(q))
 		device->can_discard = 1;
-	device->writeable = 1;
+	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;
@@ -2592,7 +2600,7 @@ int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
 	if (blk_queue_discard(q))
 		device->can_discard = 1;
 	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->io_width = fs_info->sectorsize;
 	device->io_align = fs_info->sectorsize;
@@ -2692,7 +2700,7 @@ int btrfs_grow_device(struct btrfs_trans_handle *trans,
 	u64 old_total;
 	u64 diff;
 
-	if (!device->writeable)
+	if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
 		return -EACCES;
 
 	new_size = round_down(new_size, fs_info->sectorsize);
@@ -3512,7 +3520,7 @@ static int __btrfs_balance(struct btrfs_fs_info *fs_info)
 		old_size = btrfs_device_get_total_bytes(device);
 		size_to_free = div_factor(old_size, 1);
 		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_bytes_used(device) > size_to_free ||
 		    device->is_tgtdev_for_dev_replace)
@@ -4395,7 +4403,7 @@ int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
 	mutex_lock(&fs_info->chunk_mutex);
 
 	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;
 		atomic64_sub(diff, &fs_info->free_chunk_space);
 	}
@@ -4520,7 +4528,7 @@ int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
 	if (ret) {
 		mutex_lock(&fs_info->chunk_mutex);
 		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;
 		atomic64_add(diff, &fs_info->free_chunk_space);
 		mutex_unlock(&fs_info->chunk_mutex);
@@ -4680,7 +4688,7 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
 		u64 max_avail;
 		u64 dev_offset;
 
-		if (!device->writeable) {
+		if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
 			WARN(1, KERN_ERR
 			       "BTRFS: read-only device in alloc_list\n");
 			continue;

fs/btrfs/volumes.c int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset) · line 5048

Quoted 7 lines
fs/btrfs/volumes.c +3 -1
@@ -5035,12 +5043,13 @@ int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
 
 	map = em->map_lookup;
 	for (i = 0; i < map->num_stripes; i++) {
-		if (map->stripes[i].dev->missing) {
+		struct btrfs_device *device = map->stripes[i].dev;
+
+		if (device->missing) {

nit: This is unrelated change.

Quoted 43 lines
fs/btrfs/volumes.c +6 -5
 			miss_ndevs++;
 			continue;
 		}
-
-		if (!map->stripes[i].dev->writeable) {
+		if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
 			readonly = 1;
 			goto end;
 		}
@@ -6220,7 +6229,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++) {
 		dev = bbio->stripes[dev_nr].dev;
 		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);
 			continue;
 		}
@@ -6656,7 +6666,7 @@ static int read_one_dev(struct btrfs_fs_info *fs_info,
 	}
 
 	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 !=
 		    btrfs_device_generation(leaf, dev_item))
 			return -EINVAL;
@@ -6664,7 +6674,8 @@ static int read_one_dev(struct btrfs_fs_info *fs_info,
 
 	fill_device_from_item(leaf, dev_item, device);
 	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;
 		atomic64_add(device->total_bytes - device->bytes_used,
 				&fs_info->free_chunk_space);
fs/btrfs/volumes.h +0 -0
index ff15208344a7..2e376a422626 100644

fs/btrfs/volumes.h struct btrfs_pending_bios { · line 50

Quoted 4 lines
fs/btrfs/volumes.h +1 -0
@@ -47,6 +47,8 @@ struct btrfs_pending_bios {
 #define btrfs_device_data_ordered_init(device) do { } while (0)
 #endif
 
+#define BTRFS_DEV_STATE_WRITEABLE	(1UL << 1)

Any reason why you start at bit position 1 and not 0 ?

Quoted 14 lines
fs/btrfs/volumes.h +2 -1
+
 struct btrfs_device {
 	struct list_head dev_list;
 	struct list_head dev_alloc_list;
@@ -69,7 +71,7 @@ struct btrfs_device {
 	/* the mode sent to blkdev_get */
 	fmode_t mode;
 
-	int writeable;
+	unsigned long dev_state;
 	int in_fs_metadata;
 	int missing;
 	int can_discard;
Reviewed-by on v1 2/4
#5 lore raw

Re: [PATCH 2/4] btrfs: cleanup device states define BTRFS_DEV_STATE_IN_FS_METADATA

In reply to Anand Jain
Quoted 6 lines · Anand Jain

Currently device state is being managed by each individual int variable such as struct btrfs_device::in_fs_metadata. Instead of that declare device state BTRFS_DEV_STATE_IN_FS_METADATA and use the bit operations.

Quoted 222 lines
---
 fs/btrfs/disk-io.c | 21 ++++++++++++++-------
 fs/btrfs/scrub.c   |  3 ++-
 fs/btrfs/super.c   |  5 +++--
 fs/btrfs/volumes.c | 29 +++++++++++++++++------------
 fs/btrfs/volumes.h |  2 +-
 5 files changed, 37 insertions(+), 23 deletions(-)
fs/btrfs/disk-io.c +14 -7
index 0d361b6713e1..ab1a514e5c8d 100644
@@ -3563,8 +3563,10 @@ static int barrier_all_devices(struct btrfs_fs_info *info)
 			continue;
 		if (!dev->bdev)
 			continue;
-		if (!dev->in_fs_metadata ||
-			!test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
+		if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
+						&dev->dev_state) ||
+			!test_bit(BTRFS_DEV_STATE_WRITEABLE,
+						&dev->dev_state))
 			continue;
 
 		write_dev_flush(dev);
@@ -3579,8 +3581,10 @@ static int barrier_all_devices(struct btrfs_fs_info *info)
 			errors_wait++;
 			continue;
 		}
-		if (!dev->in_fs_metadata ||
-			!test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
+		if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
+						&dev->dev_state) ||
+			!test_bit(BTRFS_DEV_STATE_WRITEABLE,
+						&dev->dev_state))
 			continue;
 
 		ret = wait_dev_flush(dev);
@@ -3677,7 +3681,8 @@ int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors)
 			total_errors++;
 			continue;
 		}
-		if (!dev->in_fs_metadata ||
+		if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
+						&dev->dev_state) ||
 			!test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
 			continue;
 
@@ -3717,8 +3722,10 @@ int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors)
 	list_for_each_entry_rcu(dev, head, dev_list) {
 		if (!dev->bdev)
 			continue;
-		if (!dev->in_fs_metadata ||
-			!test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
+		if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
+						&dev->dev_state) ||
+			!test_bit(BTRFS_DEV_STATE_WRITEABLE,
+						&dev->dev_state))
 			continue;
 
 		ret = wait_dev_supers(dev, max_mirrors);
fs/btrfs/scrub.c +2 -1
index e027e0de66a5..060fa93731e5 100644
@@ -4129,7 +4129,8 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
 	}
 
 	mutex_lock(&fs_info->scrub_lock);
-	if (!dev->in_fs_metadata || dev->is_tgtdev_for_dev_replace) {
+	if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
+					dev->is_tgtdev_for_dev_replace) {
 		mutex_unlock(&fs_info->scrub_lock);
 		mutex_unlock(&fs_info->fs_devices->device_list_mutex);
 		return -EIO;
fs/btrfs/super.c +3 -2
index 161694b66038..9089aad2f3aa 100644
@@ -1945,8 +1945,9 @@ static int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info,
 
 	rcu_read_lock();
 	list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
-		if (!device->in_fs_metadata || !device->bdev ||
-		    device->is_tgtdev_for_dev_replace)
+		if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
+						&device->dev_state) ||
+			!device->bdev || device->is_tgtdev_for_dev_replace)
 			continue;
 
 		if (i >= nr_devices)
fs/btrfs/volumes.c +17 -12
index 0f5be1808c6e..13a6ae80bee1 100644
@@ -652,7 +652,7 @@ static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
 		fs_devices->rotating = 1;
 
 	device->bdev = bdev;
-	device->in_fs_metadata = 0;
+	clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
 	device->mode = flags;
 
 	fs_devices->open_devices++;
@@ -857,7 +857,8 @@ void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices, int step)
 again:
 	/* 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 (device->in_fs_metadata) {
+		if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
+							&device->dev_state)) {
 			if (!device->is_tgtdev_for_dev_replace &&
 			    (!latest_dev ||
 			     device->generation > latest_dev->generation)) {
@@ -1600,7 +1601,7 @@ static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
 	struct extent_buffer *leaf;
 	struct btrfs_key key;
 
-	WARN_ON(!device->in_fs_metadata);
+	WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
 	WARN_ON(device->is_tgtdev_for_dev_replace);
 	path = btrfs_alloc_path();
 	if (!path)
@@ -1936,7 +1937,7 @@ int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
 	if (ret)
 		goto error_undo;
 
-	device->in_fs_metadata = 0;
+	clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
 	btrfs_scrub_cancel_dev(fs_info, device);
 
 	/*
@@ -2154,7 +2155,8 @@ int btrfs_find_device_missing_or_by_path(struct btrfs_fs_info *fs_info,
 		 * is held by the caller.
 		 */
 		list_for_each_entry(tmp, devices, dev_list) {
-			if (tmp->in_fs_metadata && !tmp->bdev) {
+			if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
+					&tmp->dev_state) && !tmp->bdev) {
 				*device = tmp;
 				break;
 			}
@@ -2410,7 +2412,7 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
 	device->commit_total_bytes = device->total_bytes;
 	device->fs_info = fs_info;
 	device->bdev = bdev;
-	device->in_fs_metadata = 1;
+	set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
 	device->is_tgtdev_for_dev_replace = 0;
 	device->mode = FMODE_EXCL;
 	device->dev_stats_valid = 1;
@@ -2613,7 +2615,7 @@ int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
 	device->commit_bytes_used = device->bytes_used;
 	device->fs_info = fs_info;
 	device->bdev = bdev;
-	device->in_fs_metadata = 1;
+	set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
 	device->is_tgtdev_for_dev_replace = 1;
 	device->mode = FMODE_EXCL;
 	device->dev_stats_valid = 1;
@@ -2642,7 +2644,7 @@ void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
 	tgtdev->io_align = sectorsize;
 	tgtdev->sector_size = sectorsize;
 	tgtdev->fs_info = fs_info;
-	tgtdev->in_fs_metadata = 1;
+	set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &tgtdev->dev_state);
 }
 
 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
@@ -4694,8 +4696,9 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
 			continue;
 		}
 
-		if (!device->in_fs_metadata ||
-		    device->is_tgtdev_for_dev_replace)
+		if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
+					&device->dev_state) ||
+					device->is_tgtdev_for_dev_replace)
 			continue;
 
 		if (device->total_bytes > device->bytes_used)
@@ -6498,7 +6501,9 @@ static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
 			}
 			btrfs_report_missing_device(fs_info, devid, uuid, false);
 		}
-		map->stripes[i].dev->in_fs_metadata = 1;
+		set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
+				&(map->stripes[i].dev->dev_state));
+
 	}
 
 	write_lock(&map_tree->map_tree.lock);
@@ -6673,7 +6678,7 @@ static int read_one_dev(struct btrfs_fs_info *fs_info,
 	}
 
 	fill_device_from_item(leaf, dev_item, device);
-	device->in_fs_metadata = 1;
+	set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
 	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;
fs/btrfs/volumes.h +1 -1
index 2e376a422626..f73b78f26a61 100644
@@ -48,6 +48,7 @@ struct btrfs_pending_bios {
 #endif
 
 #define BTRFS_DEV_STATE_WRITEABLE	(1UL << 1)
+#define BTRFS_DEV_STATE_IN_FS_METADATA	(1UL << 2)
 
 struct btrfs_device {
 	struct list_head dev_list;
@@ -72,7 +73,6 @@ struct btrfs_device {
 	fmode_t mode;
 
 	unsigned long dev_state;
-	int in_fs_metadata;
 	int missing;
 	int can_discard;
 	int is_tgtdev_for_dev_replace;
Reviewed-by on v1 3/4
#7 lore raw

Re: [PATCH 3/4] btrfs: cleanup device states define BTRFS_DEV_STATE_MISSING

In reply to Anand Jain
Quoted 6 lines · Anand Jain

Currently device state is being managed by each individual int variable such as struct btrfs_device::missing. Instead of that declare btrfs_device::dev_state BTRFS_DEV_STATE_MISSING and use the bit operations.

Quoted 230 lines
---
 fs/btrfs/dev-replace.c |  3 ++-
 fs/btrfs/disk-io.c     |  4 ++--
 fs/btrfs/scrub.c       |  7 ++++---
 fs/btrfs/super.c       |  2 +-
 fs/btrfs/volumes.c     | 33 +++++++++++++++++++--------------
 fs/btrfs/volumes.h     |  2 +-
 6 files changed, 29 insertions(+), 22 deletions(-)
fs/btrfs/dev-replace.c +2 -1
index 4b6ceb38cb5f..559db7667f38 100644
@@ -306,7 +306,8 @@ void btrfs_after_dev_replace_commit(struct btrfs_fs_info *fs_info)
 
 static inline char *dev_missing_or_rcu_str(struct btrfs_device *device)
 {
-	return device->missing ? "<missing disk>" : rcu_str_deref(device->name);
+	return test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state) ?
+				"<missing disk>" : rcu_str_deref(device->name);
 }
 
 int btrfs_dev_replace_start(struct btrfs_fs_info *fs_info,
fs/btrfs/disk-io.c +2 -2
index ab1a514e5c8d..ac1079fc9382 100644
@@ -3559,7 +3559,7 @@ static int barrier_all_devices(struct btrfs_fs_info *info)
 	/* send down all the barriers */
 	head = &info->fs_devices->devices;
 	list_for_each_entry_rcu(dev, head, dev_list) {
-		if (dev->missing)
+		if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
 			continue;
 		if (!dev->bdev)
 			continue;
@@ -3575,7 +3575,7 @@ static int barrier_all_devices(struct btrfs_fs_info *info)
 
 	/* wait for all the barriers */
 	list_for_each_entry_rcu(dev, head, dev_list) {
-		if (dev->missing)
+		if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
 			continue;
 		if (!dev->bdev) {
 			errors_wait++;
fs/btrfs/scrub.c +4 -3
index 060fa93731e5..666c00cbee03 100644
@@ -2535,7 +2535,7 @@ static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
 	}
 
 	WARN_ON(sblock->page_count == 0);
-	if (dev->missing) {
+	if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) {
 		/*
 		 * This case should only be hit for RAID 5/6 device replace. See
 		 * the comment in scrub_missing_raid56_pages() for details.
@@ -2870,7 +2870,7 @@ static int scrub_extent_for_parity(struct scrub_parity *sparity,
 	u8 csum[BTRFS_CSUM_SIZE];
 	u32 blocksize;
 
-	if (dev->missing) {
+	if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) {
 		scrub_parity_mark_sectors_error(sparity, logical, len);
 		return 0;
 	}
@@ -4112,7 +4112,8 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
 
 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
 	dev = btrfs_find_device(fs_info, devid, NULL, NULL);
-	if (!dev || (dev->missing && !is_dev_replace)) {
+	if (!dev || (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) &&
+						!is_dev_replace)) {
 		mutex_unlock(&fs_info->fs_devices->device_list_mutex);
 		return -ENODEV;
 	}
fs/btrfs/super.c +1 -1
index 9089aad2f3aa..4b70ae6f0245 100644
@@ -2243,7 +2243,7 @@ static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
 	while (cur_devices) {
 		head = &cur_devices->devices;
 		list_for_each_entry(dev, head, dev_list) {
-			if (dev->missing)
+			if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
 				continue;
 			if (!dev->name)
 				continue;
fs/btrfs/volumes.c +19 -14
index 13a6ae80bee1..75839e07ce10 100644
@@ -773,9 +773,9 @@ static noinline int device_list_add(const char *path,
 			return -ENOMEM;
 		rcu_string_free(device->name);
 		rcu_assign_pointer(device->name, name);
-		if (device->missing) {
+		if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
 			fs_devices->missing_devices--;
-			device->missing = 0;
+			clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
 		}
 	}
 
@@ -957,7 +957,7 @@ static void btrfs_prepare_close_one_device(struct btrfs_device *device)
 		fs_devices->rw_devices--;
 	}
 
-	if (device->missing)
+	if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
 		fs_devices->missing_devices--;
 
 	new_device = btrfs_alloc_device(NULL, &device->devid,
@@ -1844,7 +1844,8 @@ struct btrfs_device *btrfs_find_next_active_device(struct btrfs_fs_devices *fs_d
 
 	list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
 		if (next_device != device &&
-			!next_device->missing && next_device->bdev)
+			!test_bit(BTRFS_DEV_STATE_MISSING,
+				&next_device->dev_state) && next_device->bdev)
 			return next_device;
 	}
 
@@ -1957,7 +1958,7 @@ int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
 	device->fs_devices->num_devices--;
 	device->fs_devices->total_devices--;
 
-	if (device->missing)
+	if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
 		device->fs_devices->missing_devices--;
 
 	btrfs_assign_next_active_device(fs_info, device, NULL);
@@ -2031,7 +2032,7 @@ void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
 	list_del_rcu(&srcdev->dev_list);
 	list_del_rcu(&srcdev->dev_alloc_list);
 	fs_devices->num_devices--;
-	if (srcdev->missing)
+	if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state))
 		fs_devices->missing_devices--;
 
 	if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state))
@@ -5048,7 +5049,7 @@ int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
 	for (i = 0; i < map->num_stripes; i++) {
 		struct btrfs_device *device = map->stripes[i].dev;
 
-		if (device->missing) {
+		if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
 			miss_ndevs++;
 			continue;
 		}
@@ -6079,7 +6080,8 @@ static noinline void btrfs_schedule_bio(struct btrfs_device *device,
 	int should_queue = 1;
 	struct btrfs_pending_bios *pending_bios;
 
-	if (device->missing || !device->bdev) {
+	if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state) ||
+			!device->bdev) {
 		bio_io_error(bio);
 		return;
 	}
@@ -6282,7 +6284,7 @@ static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices,
 	device->fs_devices = fs_devices;
 	fs_devices->num_devices++;
 
-	device->missing = 1;
+	set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
 	fs_devices->missing_devices++;
 
 	return device;
@@ -6644,7 +6646,8 @@ static int read_one_dev(struct btrfs_fs_info *fs_info,
 							dev_uuid, false);
 		}
 
-		if(!device->bdev && !device->missing) {
+		if (!device->bdev && !test_bit(BTRFS_DEV_STATE_MISSING,
+							&device->dev_state)) {
 			/*
 			 * this happens when a device that was properly setup
 			 * in the device info lists suddenly goes bad.
@@ -6652,12 +6655,13 @@ static int read_one_dev(struct btrfs_fs_info *fs_info,
 			 * device->missing to one here
 			 */
 			device->fs_devices->missing_devices++;
-			device->missing = 1;
+			set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
 		}
 
 		/* Move the device to its own fs_devices */
 		if (device->fs_devices != fs_devices) {
-			ASSERT(device->missing);
+			ASSERT(test_bit(BTRFS_DEV_STATE_MISSING,
+							&device->dev_state));
 
 			list_move(&device->dev_list, &fs_devices->devices);
 			device->fs_devices->num_devices--;
@@ -6843,8 +6847,9 @@ bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info)
 		for (i = 0; i < map->num_stripes; i++) {
 			struct btrfs_device *dev = map->stripes[i].dev;
 
-			if (!dev || !dev->bdev || dev->missing ||
-			    dev->last_flush_error)
+			if (!dev || !dev->bdev ||
+				test_bit(BTRFS_DEV_STATE_MISSING,
+				&dev->dev_state) || dev->last_flush_error)
 				missing++;
 		}
 		if (missing > max_tolerated) {
fs/btrfs/volumes.h +1 -1
index f73b78f26a61..2fbff6902c8d 100644
@@ -49,6 +49,7 @@ struct btrfs_pending_bios {
 
 #define BTRFS_DEV_STATE_WRITEABLE	(1UL << 1)
 #define BTRFS_DEV_STATE_IN_FS_METADATA	(1UL << 2)
+#define BTRFS_DEV_STATE_MISSING		(1UL << 3)
 
 struct btrfs_device {
 	struct list_head dev_list;
@@ -73,7 +74,6 @@ struct btrfs_device {
 	fmode_t mode;
 
 	unsigned long dev_state;
-	int missing;
 	int can_discard;
 	int is_tgtdev_for_dev_replace;
 	blk_status_t last_flush_error;
#9 lore raw

Re: [PATCH 4/4] btrfs: cleanup device states define BTRFS_DEV_STATE_CAN_DISCARD

In reply to Anand Jain
Quoted 17 lines · Anand Jain

Currently device state is being managed by each individual int variable such as struct btrfs_device::can_discard. Instead of that declare btrfs_device::dev_state BTRFS_DEV_STATE_CAN_DISCARD and use the bit operations.

---
 fs/btrfs/extent-tree.c | 3 ++-
 fs/btrfs/volumes.c     | 6 +++---
 fs/btrfs/volumes.h     | 2 +-
 3 files changed, 6 insertions(+), 5 deletions(-)
fs/btrfs/extent-tree.c +0 -0
index f81d928754e1..ee79f7cdc543 100644

fs/btrfs/extent-tree.c int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr, · line 2160

Quoted 7 lines
fs/btrfs/extent-tree.c +2 -1
@@ -2155,7 +2155,8 @@ int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
 
 		for (i = 0; i < bbio->num_stripes; i++, stripe++) {
 			u64 bytes;
-			if (!stripe->dev->can_discard)
+			if (!test_bit(BTRFS_DEV_STATE_CAN_DISCARD,
+						&stripe->dev->dev_state))
 				continue;

Given that we only check for discard support here I can't help but think do we really need to duplicate the information. We already have struct block_device in struct btrfs_device, why don't we query for discard support directly the underlying device, rather than duplicating information?

Quoted 54 lines
fs/btrfs/extent-tree.c +0 -0
 			ret = btrfs_issue_discard(stripe->dev->bdev,
fs/btrfs/volumes.c +3 -3
index 75839e07ce10..a9c6486f06f4 100644
@@ -647,7 +647,7 @@ static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
 
 	q = bdev_get_queue(bdev);
 	if (blk_queue_discard(q))
-		device->can_discard = 1;
+		set_bit(BTRFS_DEV_STATE_CAN_DISCARD, &device->dev_state);
 	if (!blk_queue_nonrot(q))
 		fs_devices->rotating = 1;
 
@@ -2401,7 +2401,7 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
 
 	q = bdev_get_queue(bdev);
 	if (blk_queue_discard(q))
-		device->can_discard = 1;
+		set_bit(BTRFS_DEV_STATE_CAN_DISCARD, &device->dev_state);
 	set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
 	device->generation = trans->transid;
 	device->io_width = fs_info->sectorsize;
@@ -2601,7 +2601,7 @@ int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
 
 	q = bdev_get_queue(bdev);
 	if (blk_queue_discard(q))
-		device->can_discard = 1;
+		set_bit(BTRFS_DEV_STATE_CAN_DISCARD, &device->dev_state);
 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
 	set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
 	device->generation = 0;
fs/btrfs/volumes.h +1 -1
index 2fbff6902c8d..85e4b2dcc071 100644
@@ -50,6 +50,7 @@ struct btrfs_pending_bios {
 #define BTRFS_DEV_STATE_WRITEABLE	(1UL << 1)
 #define BTRFS_DEV_STATE_IN_FS_METADATA	(1UL << 2)
 #define BTRFS_DEV_STATE_MISSING		(1UL << 3)
+#define BTRFS_DEV_STATE_CAN_DISCARD	(1UL << 4)
 
 struct btrfs_device {
 	struct list_head dev_list;
@@ -74,7 +75,6 @@ struct btrfs_device {
 	fmode_t mode;
 
 	unsigned long dev_state;
-	int can_discard;
 	int is_tgtdev_for_dev_replace;
 	blk_status_t last_flush_error;
 	int flush_bio_sent;
author
#3 lore raw

Re: [PATCH 1/4] btrfs: cleanup device states define BTRFS_DEV_STATE_WRITEABLE

In reply to Nikolay Borisov
Quoted 297 lines · Nikolay Borisov
Quoted 294 lines · Anand Jain

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.

---
  fs/btrfs/disk-io.c     | 12 ++++++----
  fs/btrfs/extent-tree.c |  2 +-
  fs/btrfs/extent_io.c   |  3 ++-
  fs/btrfs/ioctl.c       |  2 +-
  fs/btrfs/scrub.c       |  3 ++-
  fs/btrfs/volumes.c     | 63 +++++++++++++++++++++++++++++---------------------
  fs/btrfs/volumes.h     |  4 +++-
  7 files changed, 54 insertions(+), 35 deletions(-)
fs/btrfs/disk-io.c +8 -4
index dfdab849037b..0d361b6713e1 100644
@@ -3563,7 +3563,8 @@ static int barrier_all_devices(struct btrfs_fs_info *info)
  			continue;
  		if (!dev->bdev)
  			continue;
-		if (!dev->in_fs_metadata || !dev->writeable)
+		if (!dev->in_fs_metadata ||
+			!test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
  			continue;
 
  		write_dev_flush(dev);
@@ -3578,7 +3579,8 @@ static int barrier_all_devices(struct btrfs_fs_info *info)
  			errors_wait++;
  			continue;
  		}
-		if (!dev->in_fs_metadata || !dev->writeable)
+		if (!dev->in_fs_metadata ||
+			!test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
  			continue;
 
  		ret = wait_dev_flush(dev);
@@ -3675,7 +3677,8 @@ int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors)
  			total_errors++;
  			continue;
  		}
-		if (!dev->in_fs_metadata || !dev->writeable)
+		if (!dev->in_fs_metadata ||
+			!test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
  			continue;
 
  		btrfs_set_stack_device_generation(dev_item, 0);
@@ -3714,7 +3717,8 @@ int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors)
  	list_for_each_entry_rcu(dev, head, dev_list) {
  		if (!dev->bdev)
  			continue;
-		if (!dev->in_fs_metadata || !dev->writeable)
+		if (!dev->in_fs_metadata ||
+			!test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
  			continue;
 
  		ret = wait_dev_supers(dev, max_mirrors);
fs/btrfs/extent-tree.c +1 -1
index e2d7e86b51d1..f81d928754e1 100644
@@ -10935,7 +10935,7 @@ static int btrfs_trim_free_extents(struct btrfs_device *device,
  	*trimmed = 0;
 
  	/* Not writeable = nothing to do. */
-	if (!device->writeable)
+	if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
  		return 0;
 
  	/* No free space = nothing to do. */
fs/btrfs/extent_io.c +2 -1
index 7fa50e12f18e..f51c797847c7 100644
@@ -2028,7 +2028,8 @@ int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
  	bio->bi_iter.bi_sector = sector;
  	dev = bbio->stripes[bbio->mirror_num - 1].dev;
  	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);
  		bio_put(bio);
  		return -EIO;
fs/btrfs/ioctl.c +1 -1
index 6c7a49faf4e0..8a74c83503d6 100644
@@ -1518,7 +1518,7 @@ static noinline int btrfs_ioctl_resize(struct file *file,
  		goto out_free;
  	}
 
-	if (!device->writeable) {
+	if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
  		btrfs_info(fs_info,
  			   "resizer unable to apply on readonly device %llu",
  		       devid);
fs/btrfs/scrub.c +2 -1
index e3f6c49e5c4d..e027e0de66a5 100644
@@ -4117,7 +4117,8 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
  		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);
  		rcu_read_lock();
  		name = rcu_dereference(dev->name);
fs/btrfs/volumes.c +28 -20
index 397b335d108c..0f5be1808c6e 100644
@@ -634,10 +634,15 @@ static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
  	device->generation = btrfs_super_generation(disk_super);
 
  	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;
  	} 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);
@@ -651,7 +656,7 @@ static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
  	device->mode = flags;
 
  	fs_devices->open_devices++;
-	if (device->writeable &&
+	if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
  	    device->devid != BTRFS_DEV_REPLACE_DEVID) {
  		fs_devices->rw_devices++;
  		list_add(&device->dev_alloc_list,
@@ -881,9 +886,10 @@ void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices, int step)
  			device->bdev = NULL;
  			fs_devices->open_devices--;
  		}
-		if (device->writeable) {
+		if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
  			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)
  				fs_devices->rw_devices--;
  		}
@@ -925,7 +931,8 @@ static void free_device(struct rcu_head *head)
 
  static void btrfs_close_bdev(struct btrfs_device *device)
  {
-	if (device->bdev && device->writeable) {
+	if (device->bdev &&
+		test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
  		sync_blockdev(device->bdev);
  		invalidate_bdev(device->bdev);
  	}
@@ -943,7 +950,7 @@ static void btrfs_prepare_close_one_device(struct btrfs_device *device)
  	if (device->bdev)
  		fs_devices->open_devices--;
 
-	if (device->writeable &&
+	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--;
@@ -1901,12 +1908,13 @@ int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
  		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;
  		goto out;
  	}
 
-	if (device->writeable) {
+	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--;
@@ -1968,7 +1976,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
  	 * 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_close_bdev(device);
@@ -1994,7 +2002,7 @@ int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
  	return ret;
 
  error_undo:
-	if (device->writeable) {
+	if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
  		mutex_lock(&fs_info->chunk_mutex);
  		list_add(&device->dev_alloc_list,
  			 &fs_info->fs_devices->alloc_list);
@@ -2025,7 +2033,7 @@ void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
  	if (srcdev->missing)
  		fs_devices->missing_devices--;
 
-	if (srcdev->writeable)
+	if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state))
  		fs_devices->rw_devices--;
 
  	if (srcdev->bdev)
@@ -2037,7 +2045,7 @@ void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
  {
  	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 */
  		btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
  	}
@@ -2391,7 +2399,7 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
  	q = bdev_get_queue(bdev);
  	if (blk_queue_discard(q))
  		device->can_discard = 1;
-	device->writeable = 1;
+	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;
@@ -2592,7 +2600,7 @@ int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
  	if (blk_queue_discard(q))
  		device->can_discard = 1;
  	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->io_width = fs_info->sectorsize;
  	device->io_align = fs_info->sectorsize;
@@ -2692,7 +2700,7 @@ int btrfs_grow_device(struct btrfs_trans_handle *trans,
  	u64 old_total;
  	u64 diff;
 
-	if (!device->writeable)
+	if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
  		return -EACCES;
 
  	new_size = round_down(new_size, fs_info->sectorsize);
@@ -3512,7 +3520,7 @@ static int __btrfs_balance(struct btrfs_fs_info *fs_info)
  		old_size = btrfs_device_get_total_bytes(device);
  		size_to_free = div_factor(old_size, 1);
  		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_bytes_used(device) > size_to_free ||
  		    device->is_tgtdev_for_dev_replace)
@@ -4395,7 +4403,7 @@ int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
  	mutex_lock(&fs_info->chunk_mutex);
 
  	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;
  		atomic64_sub(diff, &fs_info->free_chunk_space);
  	}
@@ -4520,7 +4528,7 @@ int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
  	if (ret) {
  		mutex_lock(&fs_info->chunk_mutex);
  		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;
  		atomic64_add(diff, &fs_info->free_chunk_space);
  		mutex_unlock(&fs_info->chunk_mutex);
@@ -4680,7 +4688,7 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  		u64 max_avail;
  		u64 dev_offset;
 
-		if (!device->writeable) {
+		if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
  			WARN(1, KERN_ERR
  			       "BTRFS: read-only device in alloc_list\n");
  			continue;
@@ -5035,12 +5043,13 @@ int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
 
Quoted 8 lines

fs/btrfs/volumes.c int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset) · line 5048

Quoted 6 lines
fs/btrfs/volumes.c +3 -1
  	map = em->map_lookup;
  	for (i = 0; i < map->num_stripes; i++) {
-		if (map->stripes[i].dev->missing) {
+		struct btrfs_device *device = map->stripes[i].dev;
+
+		if (device->missing) {

nit: This is unrelated change.

Actually this helped to avoid the super long code. I could revert it though, I am ok either ways.

Quoted 46 lines
Quoted 46 lines
fs/btrfs/volumes.c +6 -5
  			miss_ndevs++;
  			continue;
  		}
-
-		if (!map->stripes[i].dev->writeable) {
+		if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
  			readonly = 1;
  			goto end;
  		}
@@ -6220,7 +6229,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++) {
  		dev = bbio->stripes[dev_nr].dev;
  		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);
  			continue;
  		}
@@ -6656,7 +6666,7 @@ static int read_one_dev(struct btrfs_fs_info *fs_info,
  	}
 
  	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 !=
  		    btrfs_device_generation(leaf, dev_item))
  			return -EINVAL;
@@ -6664,7 +6674,8 @@ static int read_one_dev(struct btrfs_fs_info *fs_info,
 
  	fill_device_from_item(leaf, dev_item, device);
  	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;
  		atomic64_add(device->total_bytes - device->bytes_used,
  				&fs_info->free_chunk_space);
fs/btrfs/volumes.h +0 -0
index ff15208344a7..2e376a422626 100644
@@ -47,6 +47,8 @@ struct btrfs_pending_bios {
  #define btrfs_device_data_ordered_init(device) do { } while (0)
  #endif
 
Quoted 3 lines

fs/btrfs/volumes.h struct btrfs_pending_bios { · line 50

Quoted 1 line
fs/btrfs/volumes.h +1 -0
+#define BTRFS_DEV_STATE_WRITEABLE	(1UL << 1)

Any reason why you start at bit position 1 and not 0 ?

Right. I was wondering if we need BTRFS_DEV_STATE_ONLINE at some point. So thought it will be nice to reserve 0 for that, not a strong reason though. I am ok to start from 0 if there is any concern.

Thanks, Anand

Quoted 14 lines
Quoted 14 lines
fs/btrfs/volumes.h +2 -1
+
  struct btrfs_device {
  	struct list_head dev_list;
  	struct list_head dev_alloc_list;
@@ -69,7 +71,7 @@ struct btrfs_device {
  	/* the mode sent to blkdev_get */
  	fmode_t mode;
 
-	int writeable;
+	unsigned long dev_state;
  	int in_fs_metadata;
  	int missing;
  	int can_discard;
author
#10 lore raw

Re: [PATCH 4/4] btrfs: cleanup device states define BTRFS_DEV_STATE_CAN_DISCARD

In reply to Nikolay Borisov
Quoted 21 lines · Nikolay Borisov
Quoted 18 lines · Anand Jain

Currently device state is being managed by each individual int variable such as struct btrfs_device::can_discard. Instead of that declare btrfs_device::dev_state BTRFS_DEV_STATE_CAN_DISCARD and use the bit operations.

---
  fs/btrfs/extent-tree.c | 3 ++-
  fs/btrfs/volumes.c     | 6 +++---
  fs/btrfs/volumes.h     | 2 +-
  3 files changed, 6 insertions(+), 5 deletions(-)
fs/btrfs/extent-tree.c +0 -0
index f81d928754e1..ee79f7cdc543 100644
@@ -2155,7 +2155,8 @@ int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
 
Quoted 12 lines

fs/btrfs/extent-tree.c int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr, · line 2160

Quoted 6 lines
fs/btrfs/extent-tree.c +2 -1
  		for (i = 0; i < bbio->num_stripes; i++, stripe++) {
  			u64 bytes;
-			if (!stripe->dev->can_discard)
+			if (!test_bit(BTRFS_DEV_STATE_CAN_DISCARD,
+						&stripe->dev->dev_state))
  				continue;

Given that we only check for discard support here I can't help but think do we really need to duplicate the information. We already have struct block_device in struct btrfs_device, why don't we query for discard support directly the underlying device, rather than duplicating information?

I agree. bdev_get_queue(bdev) and blk_queue_discard(q) are light weight too.
So I shall withdraw this patch and sent the following patch to remove can_discard altogether.
[PATCH] btrfs: drop btrfs_device::can_discard to query directly

Thanks, Anand

Quoted 54 lines
Quoted 54 lines
fs/btrfs/extent-tree.c +0 -0
  			ret = btrfs_issue_discard(stripe->dev->bdev,
fs/btrfs/volumes.c +3 -3
index 75839e07ce10..a9c6486f06f4 100644
@@ -647,7 +647,7 @@ static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
 
  	q = bdev_get_queue(bdev);
  	if (blk_queue_discard(q))
-		device->can_discard = 1;
+		set_bit(BTRFS_DEV_STATE_CAN_DISCARD, &device->dev_state);
  	if (!blk_queue_nonrot(q))
  		fs_devices->rotating = 1;
 
@@ -2401,7 +2401,7 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
 
  	q = bdev_get_queue(bdev);
  	if (blk_queue_discard(q))
-		device->can_discard = 1;
+		set_bit(BTRFS_DEV_STATE_CAN_DISCARD, &device->dev_state);
  	set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
  	device->generation = trans->transid;
  	device->io_width = fs_info->sectorsize;
@@ -2601,7 +2601,7 @@ int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
 
  	q = bdev_get_queue(bdev);
  	if (blk_queue_discard(q))
-		device->can_discard = 1;
+		set_bit(BTRFS_DEV_STATE_CAN_DISCARD, &device->dev_state);
  	mutex_lock(&fs_info->fs_devices->device_list_mutex);
  	set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
  	device->generation = 0;
fs/btrfs/volumes.h +1 -1
index 2fbff6902c8d..85e4b2dcc071 100644
@@ -50,6 +50,7 @@ struct btrfs_pending_bios {
  #define BTRFS_DEV_STATE_WRITEABLE	(1UL << 1)
  #define BTRFS_DEV_STATE_IN_FS_METADATA	(1UL << 2)
  #define BTRFS_DEV_STATE_MISSING		(1UL << 3)
+#define BTRFS_DEV_STATE_CAN_DISCARD	(1UL << 4)
 
  struct btrfs_device {
  	struct list_head dev_list;
@@ -74,7 +75,6 @@ struct btrfs_device {
  	fmode_t mode;
 
  	unsigned long dev_state;
-	int can_discard;
  	int is_tgtdev_for_dev_replace;
  	blk_status_t last_flush_error;
  	int flush_bio_sent;