All of lore.kernel.org
 help / color / mirror / Atom feed
From: Naohiro Aota <naohiro.aota@wdc.com>
To: linux-btrfs@vger.kernel.org, dsterba@suse.com
Cc: hare@suse.com, linux-fsdevel@vger.kernel.org,
	Jens Axboe <axboe@kernel.dk>,
	Christoph Hellwig <hch@infradead.org>,
	"Darrick J. Wong" <darrick.wong@oracle.com>,
	Johannes Thumshirn <johannes.thumshirn@wdc.com>,
	Naohiro Aota <naohiro.aota@wdc.com>
Subject: [PATCH v11 08/40] btrfs: emulated zoned mode on non-zoned devices
Date: Tue, 22 Dec 2020 12:49:01 +0900	[thread overview]
Message-ID: <e2bcb873196a16b05d5757cd8087900d4f464347.1608608848.git.naohiro.aota@wdc.com> (raw)
In-Reply-To: <06add214bc16ef08214de1594ecdfcc4cdcdbd78.1608608848.git.naohiro.aota@wdc.com>

From: Johannes Thumshirn <johannes.thumshirn@wdc.com>

Emulate zoned btrfs mode on non-zoned devices. This is done by "slicing
up" the block-device into static sized chunks and fake a conventional zone
on each of them. The emulated zone size is determined from the size of
device extent.

This is mainly aimed at testing parts of the zoned mode, i.e. the zoned
chunk allocator, on regular block devices.

Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
---
 fs/btrfs/dev-replace.c |   3 +
 fs/btrfs/volumes.c     |  14 +++++
 fs/btrfs/volumes.h     |   3 +
 fs/btrfs/zoned.c       | 121 +++++++++++++++++++++++++++++++++++++----
 fs/btrfs/zoned.h       |  14 +++--
 5 files changed, 139 insertions(+), 16 deletions(-)

diff --git a/fs/btrfs/dev-replace.c b/fs/btrfs/dev-replace.c
index 324f646d6e5e..e77cb46bf15d 100644
--- a/fs/btrfs/dev-replace.c
+++ b/fs/btrfs/dev-replace.c
@@ -321,6 +321,9 @@ static int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
 	set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
 	device->fs_devices = fs_info->fs_devices;
 
+	if (btrfs_is_zoned(fs_info) && bdev_zoned_model(bdev) == BLK_ZONED_NONE)
+		device->force_zoned = true;
+
 	ret = btrfs_get_dev_zone_info(device);
 	if (ret)
 		goto error;
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 7d92b11ea603..2cdb5fe3e423 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -669,6 +669,15 @@ static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
 	clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
 	device->mode = flags;
 
+	/* Emulate zoned mode on regular device? */
+	if ((btrfs_super_incompat_flags(disk_super) &
+	     BTRFS_FEATURE_INCOMPAT_ZONED) &&
+	    bdev_zoned_model(device->bdev) == BLK_ZONED_NONE) {
+		btrfs_info(NULL,
+"zoned: incompat zoned flag detected on regular device, forcing zoned mode emulation");
+		device->force_zoned = true;
+	}
+
 	fs_devices->open_devices++;
 	if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
 	    device->devid != BTRFS_DEV_REPLACE_DEVID) {
@@ -2562,6 +2571,11 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
 	device->fs_info = fs_info;
 	device->bdev = bdev;
 
+	/* Zoned mode is enabled. Emulate zoned device on a regular device. */
+	if (btrfs_is_zoned(fs_info) &&
+	    bdev_zoned_model(device->bdev) == BLK_ZONED_NONE)
+		device->force_zoned = true;
+
 	ret = btrfs_get_dev_zone_info(device);
 	if (ret)
 		goto error_free_device;
diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
index 1997a4649a66..59d9d47f173d 100644
--- a/fs/btrfs/volumes.h
+++ b/fs/btrfs/volumes.h
@@ -144,6 +144,9 @@ struct btrfs_device {
 	struct completion kobj_unregister;
 	/* For sysfs/FSID/devinfo/devid/ */
 	struct kobject devid_kobj;
+
+	/* Force zoned mode */
+	bool force_zoned;
 };
 
 /*
diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index ae566a7da088..fc43a650cd79 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -119,6 +119,32 @@ static inline u32 sb_zone_number(int shift, int mirror)
 	return 0;
 }
 
+static int emulate_report_zones(struct btrfs_device *device, u64 pos,
+				struct blk_zone *zones, unsigned int nr_zones)
+{
+	const sector_t zone_sectors =
+		device->fs_info->zone_size >> SECTOR_SHIFT;
+	sector_t bdev_size = device->bdev->bd_part->nr_sects;
+	unsigned int i;
+
+	pos >>= SECTOR_SHIFT;
+	for (i = 0; i < nr_zones; i++) {
+		zones[i].start = i * zone_sectors + pos;
+		zones[i].len = zone_sectors;
+		zones[i].capacity = zone_sectors;
+		zones[i].wp = zones[i].start + zone_sectors;
+		zones[i].type = BLK_ZONE_TYPE_CONVENTIONAL;
+		zones[i].cond = BLK_ZONE_COND_NOT_WP;
+
+		if (zones[i].wp >= bdev_size) {
+			i++;
+			break;
+		}
+	}
+
+	return i;
+}
+
 static int btrfs_get_dev_zones(struct btrfs_device *device, u64 pos,
 			       struct blk_zone *zones, unsigned int *nr_zones)
 {
@@ -127,6 +153,12 @@ static int btrfs_get_dev_zones(struct btrfs_device *device, u64 pos,
 	if (!*nr_zones)
 		return 0;
 
+	if (device->force_zoned) {
+		ret = emulate_report_zones(device, pos, zones, *nr_zones);
+		*nr_zones = ret;
+		return 0;
+	}
+
 	ret = blkdev_report_zones(device->bdev, pos >> SECTOR_SHIFT, *nr_zones,
 				  copy_zone_info_cb, zones);
 	if (ret < 0) {
@@ -143,6 +175,49 @@ static int btrfs_get_dev_zones(struct btrfs_device *device, u64 pos,
 	return 0;
 }
 
+static int calculate_emulated_zone_size(struct btrfs_fs_info *fs_info)
+{
+	struct btrfs_path *path;
+	struct btrfs_root *root = fs_info->dev_root;
+	struct btrfs_key key;
+	struct extent_buffer *leaf;
+	struct btrfs_dev_extent *dext;
+	int ret = 0;
+
+	key.objectid = 1;
+	key.type = BTRFS_DEV_EXTENT_KEY;
+	key.offset = 0;
+
+	path = btrfs_alloc_path();
+	if (!path)
+		return -ENOMEM;
+
+	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
+	if (ret < 0)
+		goto out;
+
+	if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
+		ret = btrfs_next_item(root, path);
+		if (ret < 0)
+			goto out;
+		/* No dev extents at all? Not good */
+		if (ret > 0) {
+			ret = -EUCLEAN;
+			goto out;
+		}
+	}
+
+	leaf = path->nodes[0];
+	dext = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent);
+	fs_info->zone_size = btrfs_dev_extent_length(leaf, dext);
+	ret = 0;
+
+out:
+	btrfs_free_path(path);
+
+	return ret;
+}
+
 int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info)
 {
 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
@@ -158,6 +233,12 @@ int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info)
 		if (!device->bdev)
 			continue;
 
+		if (device->force_zoned && !fs_info->zone_size) {
+			ret = calculate_emulated_zone_size(fs_info);
+			if (ret)
+				break;
+		}
+
 		ret = btrfs_get_dev_zone_info(device);
 		if (ret)
 			break;
@@ -177,9 +258,11 @@ int btrfs_get_dev_zone_info(struct btrfs_device *device)
 	struct blk_zone *zones = NULL;
 	unsigned int i, nreported = 0, nr_zones;
 	unsigned int zone_sectors;
+	const bool force_zoned = device->force_zoned;
+	char *model, *emulated;
 	int ret;
 
-	if (!bdev_is_zoned(bdev))
+	if (!bdev_is_zoned(bdev) && !force_zoned)
 		return 0;
 
 	if (device->zone_info)
@@ -189,8 +272,12 @@ int btrfs_get_dev_zone_info(struct btrfs_device *device)
 	if (!zone_info)
 		return -ENOMEM;
 
+	if (force_zoned)
+		zone_sectors = device->fs_info->zone_size >> SECTOR_SHIFT;
+	else
+		zone_sectors = bdev_zone_sectors(bdev);
+
 	nr_sectors = bdev->bd_part->nr_sects;
-	zone_sectors = bdev_zone_sectors(bdev);
 	/* Check if it's power of 2 (see is_power_of_2) */
 	ASSERT(zone_sectors != 0 && (zone_sectors & (zone_sectors - 1)) == 0);
 	zone_info->zone_size = zone_sectors << SECTOR_SHIFT;
@@ -296,12 +383,22 @@ int btrfs_get_dev_zone_info(struct btrfs_device *device)
 
 	device->zone_info = zone_info;
 
-	/* device->fs_info is not safe to use for printing messages */
-	btrfs_info_in_rcu(NULL,
-			"host-%s zoned block device %s, %u zones of %llu bytes",
-			bdev_zoned_model(bdev) == BLK_ZONED_HM ? "managed" : "aware",
-			rcu_str_deref(device->name), zone_info->nr_zones,
-			zone_info->zone_size);
+	if (bdev_zoned_model(bdev) == BLK_ZONED_HM) {
+		model = "host-managed zoned";
+		emulated = "";
+	} else if (bdev_zoned_model(bdev) == BLK_ZONED_HA) {
+		model = "host-aware zoned";
+		emulated = "";
+	} else if (bdev_zoned_model(bdev) == BLK_ZONED_NONE &&
+		 device->force_zoned) {
+		model = "regular";
+		emulated = "emulated ";
+	}
+
+	btrfs_info_in_rcu(device->fs_info,
+		"%s block device %s, %u %szones of %llu bytes",
+		model, rcu_str_deref(device->name), zone_info->nr_zones,
+		emulated, zone_info->zone_size);
 
 	return 0;
 
@@ -348,7 +445,7 @@ int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info)
 	u64 nr_devices = 0;
 	u64 zone_size = 0;
 	u64 max_zone_append_size = 0;
-	const bool incompat_zoned = btrfs_is_zoned(fs_info);
+	const bool incompat_zoned = btrfs_fs_incompat(fs_info, ZONED);
 	int ret = 0;
 
 	/* Count zoned devices */
@@ -360,8 +457,10 @@ int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info)
 
 		model = bdev_zoned_model(device->bdev);
 		if (model == BLK_ZONED_HM ||
-		    (model == BLK_ZONED_HA && incompat_zoned)) {
-			struct btrfs_zoned_device_info *zone_info;
+		    (model == BLK_ZONED_HA && incompat_zoned) ||
+		    device->force_zoned) {
+			struct btrfs_zoned_device_info *zone_info =
+				device->zone_info;
 
 			zone_info = device->zone_info;
 			zoned_devices++;
diff --git a/fs/btrfs/zoned.h b/fs/btrfs/zoned.h
index 5e0e7de84a82..058a57317c05 100644
--- a/fs/btrfs/zoned.h
+++ b/fs/btrfs/zoned.h
@@ -143,12 +143,16 @@ static inline void btrfs_dev_clear_zone_empty(struct btrfs_device *device, u64 p
 static inline bool btrfs_check_device_zone_type(const struct btrfs_fs_info *fs_info,
 						struct block_device *bdev)
 {
-	u64 zone_size;
-
 	if (btrfs_is_zoned(fs_info)) {
-		zone_size = bdev_zone_sectors(bdev) << SECTOR_SHIFT;
-		/* Do not allow non-zoned device */
-		return bdev_is_zoned(bdev) && fs_info->zone_size == zone_size;
+		/*
+		 * We can allow a regular device on a zoned btrfs, because
+		 * we will emulate zoned device on the regular device.
+		 */
+		if (!bdev_is_zoned(bdev))
+			return true;
+
+		return fs_info->zone_size ==
+			(bdev_zone_sectors(bdev) << SECTOR_SHIFT);
 	}
 
 	/* Do not allow Host Manged zoned device */
-- 
2.27.0


  parent reply	other threads:[~2020-12-22  3:52 UTC|newest]

Thread overview: 86+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-22  3:48 [PATCH v11 00/40] btrfs: zoned block device support Naohiro Aota
2020-12-22  3:48 ` [PATCH v11 01/40] block: add bio_add_zone_append_page Naohiro Aota
2020-12-22  3:48   ` [PATCH v11 02/40] iomap: support REQ_OP_ZONE_APPEND Naohiro Aota
2021-01-04 22:30     ` Darrick J. Wong
2021-01-13  9:34       ` Johannes Thumshirn
2020-12-22  3:48   ` [PATCH v11 03/40] btrfs: defer loading zone info after opening trees Naohiro Aota
2021-01-11 19:16     ` Josef Bacik
2020-12-22  3:48   ` [PATCH v11 04/40] btrfs: change superblock location on conventional zone Naohiro Aota
2021-01-11 19:47     ` Josef Bacik
2021-01-14 15:10       ` Naohiro Aota
2020-12-22  3:48   ` [PATCH v11 05/40] btrfs: release path before calling into btrfs_load_block_group_zone_info Naohiro Aota
2021-01-11 20:01     ` Josef Bacik
2021-01-12  8:05       ` Johannes Thumshirn
2020-12-22  3:48   ` [PATCH v11 06/40] btrfs: do not load fs_info->zoned from incompat flag Naohiro Aota
2021-01-11 20:08     ` Josef Bacik
2021-01-12  8:00       ` Johannes Thumshirn
2020-12-22  3:49   ` [PATCH v11 07/40] btrfs: disallow fitrim in ZONED mode Naohiro Aota
2021-01-11 20:12     ` Josef Bacik
2021-01-12 10:19       ` Johannes Thumshirn
2020-12-22  3:49   ` Naohiro Aota [this message]
2021-01-11 20:54     ` [PATCH v11 08/40] btrfs: emulated zoned mode on non-zoned devices Josef Bacik
2021-01-13 17:58     ` David Sterba
2021-01-13 18:23       ` Johannes Thumshirn
2020-12-22  3:49   ` [PATCH v11 09/40] btrfs: implement zoned chunk allocator Naohiro Aota
2021-01-11 21:24     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 10/40] btrfs: verify device extent is aligned to zone Naohiro Aota
2020-12-22  3:49   ` [PATCH v11 11/40] btrfs: load zone's allocation offset Naohiro Aota
2020-12-22  3:49   ` [PATCH v11 12/40] btrfs: calculate allocation offset for conventional zones Naohiro Aota
2021-01-12 15:12     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 13/40] btrfs: track unusable bytes for zones Naohiro Aota
2021-01-12 15:45     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 14/40] btrfs: do sequential extent allocation in ZONED mode Naohiro Aota
2020-12-22  3:49   ` [PATCH v11 15/40] btrfs: redirty released extent buffers " Naohiro Aota
2021-01-12 15:51     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 16/40] btrfs: advance allocation pointer after tree log node Naohiro Aota
2021-01-12 15:52     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 17/40] btrfs: enable to mount ZONED incompat flag Naohiro Aota
2020-12-22  3:49   ` [PATCH v11 18/40] btrfs: reset zones of unused block groups Naohiro Aota
2021-01-12 15:54     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 19/40] btrfs: extract page adding function Naohiro Aota
2021-01-11 16:22     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 20/40] btrfs: use bio_add_zone_append_page for zoned btrfs Naohiro Aota
2021-01-12 15:55     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 21/40] btrfs: handle REQ_OP_ZONE_APPEND as writing Naohiro Aota
2020-12-22  3:49   ` [PATCH v11 22/40] btrfs: split ordered extent when bio is sent Naohiro Aota
2021-01-12 15:59     ` Josef Bacik
2021-01-13 10:05       ` Johannes Thumshirn
2021-01-15  7:08     ` Su Yue
2020-12-22  3:49   ` [PATCH v11 23/40] btrfs: extend btrfs_rmap_block for specifying a device Naohiro Aota
2021-01-12 16:00     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 24/40] btrfs: cache if block-group is on a sequential zone Naohiro Aota
2021-01-12 16:01     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 25/40] btrfs: use ZONE_APPEND write for ZONED btrfs Naohiro Aota
2020-12-22  3:49   ` [PATCH v11 26/40] btrfs: enable zone append writing for direct IO Naohiro Aota
2020-12-22  3:49   ` [PATCH v11 27/40] btrfs: introduce dedicated data write path for ZONED mode Naohiro Aota
2021-01-12 19:24     ` Josef Bacik
2021-01-13 10:41       ` Naohiro Aota
2021-01-12 19:28     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 28/40] btrfs: serialize meta IOs on " Naohiro Aota
2020-12-22  3:49   ` [PATCH v11 29/40] btrfs: wait existing extents before truncating Naohiro Aota
2020-12-22  3:49   ` [PATCH v11 30/40] btrfs: avoid async metadata checksum on ZONED mode Naohiro Aota
2020-12-22  3:49   ` [PATCH v11 31/40] btrfs: mark block groups to copy for device-replace Naohiro Aota
2021-01-12 19:30     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 32/40] btrfs: implement cloning for ZONED device-replace Naohiro Aota
2021-01-12 19:36     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 33/40] btrfs: implement copying " Naohiro Aota
2021-01-12 19:37     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 34/40] btrfs: support dev-replace in ZONED mode Naohiro Aota
2021-01-12 19:37     ` Josef Bacik
2021-01-12 19:40     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 35/40] btrfs: enable relocation " Naohiro Aota
2021-01-12 19:43     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 36/40] btrfs: relocate block group to repair IO failure in ZONED Naohiro Aota
2020-12-22  3:49   ` [PATCH v11 37/40] btrfs: split alloc_log_tree() Naohiro Aota
2021-01-12 19:44     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 38/40] btrfs: extend zoned allocator to use dedicated tree-log block group Naohiro Aota
2021-01-12 19:48     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 39/40] btrfs: serialize log transaction on ZONED mode Naohiro Aota
2021-01-12 19:50     ` Josef Bacik
2020-12-22  3:49   ` [PATCH v11 40/40] btrfs: reorder log node allocation Naohiro Aota
2020-12-22 13:35   ` [PATCH v11 01/40] block: add bio_add_zone_append_page Christoph Hellwig
2021-01-12 13:48     ` Johannes Thumshirn
2020-12-22 13:38 ` [PATCH v11 00/40] btrfs: zoned block device support Christoph Hellwig
2021-01-11 10:17   ` Johannes Thumshirn
2021-01-12 10:23     ` hch
2021-01-12 10:26       ` Johannes Thumshirn

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e2bcb873196a16b05d5757cd8087900d4f464347.1608608848.git.naohiro.aota@wdc.com \
    --to=naohiro.aota@wdc.com \
    --cc=axboe@kernel.dk \
    --cc=darrick.wong@oracle.com \
    --cc=dsterba@suse.com \
    --cc=hare@suse.com \
    --cc=hch@infradead.org \
    --cc=johannes.thumshirn@wdc.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.