Add full zoned storage emulation to the qcow2 driver
v14 cover Add full zoned storage emulation to the qcow2 driver
This patch series add a new extension - zoned format - to the qcow2 driver, allowing full zoned storage emulation on a qcow2 image file. A user can attach such an image to a guest and have it appear as a host-managed zoned block device.
The zoned format is opt-in through a new qcow2 header extension that pins the zone geometry. Behind the extension is a dedicated zoned metadata region that stores one 8-byte write pointer (WP) per zone. The extension is gated by an incompatible bit, so an older qcow2 implementation cannot accidentally open the image.
Each write pointer is routed through the write pointer cache, a Qcow2Cache object. The write pointer cache is written to disk after the qcow2 metadata is written, thus guaranteeing that the write pointer is updated after the corresponding data is written.
Zone states are in memory. Read-only and offline states are device-internal events, which are not modelled in qcow2 emulation for simplicity. The other zone states (closed, empty, full) can be inferred from write poiner values, presistent across QEMU reboots. The open states are kept in memory using open zone lists.
To create a qcow2 file with the zoned format:
qemu-img create -f qcow2 zbc.qcow2 \
-o size=768M \
-o zone.size=64M \
-o zone.capacity=64M \
-o zone.conventional_zones=0 \
-o zone.max_append_bytes=4096 \
-o zone.max_open_zones=6 \
-o zone.max_active_zones=8 \
-o zone.mode=host-managedThen attach it to a guest via the QEMU command line:
-blockdev node-name=drive1,driver=qcow2,\
file.driver=file,file.filename=zbc.qcow2 \
-device virtio-blk-pci,drive=drive1 \v13->v14:
- fix review comments [Stefan]
* store the on-disk write pointers big-endian
* move the conventional-zone flag to bit 63 to follow block layer's wp encoding
* add QEMU_BUILD_BUG_ON check on conv bit
* make zone_append beyond the device capacity return -ENOSPC
* cap nr_zones to avoid DoS
* zone reset and finish yield until in-flight writes to the target zone drain
* fix wps memory leak on zonedmeta refresh failure
* fix overflow on condition checks
* improve err handling for write pointers
- refresh qcow2 checks output
- add reviewed-by tags [Niklas]
v12->v13:
- split the zone_size widening to a separate commit [Niklas]
- relocate the qcow2_check_zone_options reorder and max_active/open/append-zones wording [Niklas]
- modify the comment for request merge constraints [Niklas]
v11->v12:
- validate the zoned header extension before allocating any zoned metadata
- fix the zoned locking discipline: hold the qcow2 state lock (s->lock) across all zone-state list updates [Stefan]
- reject, instead of aborting on an assertion, a write that reaches the open-zone limit while every open zone is explicitly open
- fix reduced-capacity (zone_capacity < zone_size) handling:
* transition a zone to FULL when the write pointer reaches the zone capacity
* report the zone capacity, not the zone size, in zone_report
- return -EIO rather than -EINVAL for zone write-rule violations
- use int64_t for the zoned-metadata cluster offset and zone_size to avoid truncation on large images
- simplify qcow2_cache_set_dependency() [Stefan]
- virtio-blk: read the zone size through a new blk_get_zone_size() [Stefan]
- docs/QAPI: clarify the max_open_zones / max_active_zones / max_append_bytes descriptions; use warn_report() for recoverable zoned configuration values in qcow2_check_zone_options() [Niklas, Stefan]
- iotests: add qcow2 check updates (303, qcow2-encryption) [Stefan]
- coding style: function opening braces, DEBUG_EXT format specifier
v10->v11:
- add write ordering through write pointer cache [Stefan]
- add new test cases to iotests for zone append write
- add zoned image info to qemu-img --info
- move virtio-blk cross-zone-boundary merge fix into its own patch [Stefan]
- fix specs and docs:
* document bit 59 conventional-zone marker in qcow2.rst [Stefan]
* spell out write ordering for crash consistency [Stefan]
* QAPI: rephrase comments, bump version [Markus]
- fix review comments [Stefan]
v9->v10:
- add cross-boundary constraint for merging writes on zoned devices
- extract call from assert() to fix image creation crash
v8->v9:
- fix compilation err after modifying types
v7->v8:
- sets default values for zoned img confurations [Markus, Damien]
- changes the type of zone_size, zone_capacity from uint32_t to uint64_t [Stefan]
- fix docs [Stefan]
- modify append writes to increase concurrency [Damien, Stefan]
- use tailqueue to track active zones [Stefan]
- fix undefined behavior of ranges_overlap()
- fix the iotest of case 2(1)
v6->v7:
- modify zone resource managemant (style) [Damien]
- fix accessing list with negative index err
- add some tests for zrm in iotests
- address review comments [Markus]
v5->v6:
- fix docs and specs [Eric, Markus, Stefan]
- add general sanity checks for zoned device configurations while creation and opening [Eric]
- fix LRU when implicitly open a zone for a long time [Stefan]
v4->v5:
- add incompatible bit for zoned format [Eric]
- fix and manage zone resources via LRU [Damien]
- renaming functions and fields, spec changes [Markus, Damien]
- add closed zone list
- make qemu iotests for zoned device consecutive [Stefan]
v3->v4:
- use QLIST for implicit, explicit open zones management [Stefan]
- keep zone states in memory and drop state bits in wp metadata structure [Damien, Stefan]
- change zone resource management and iotests accordingly
- add tracing for number of implicit zones
- address review comments [Stefan, Markus]:
* documentation, config, style
v2->v3:
- drop zoned_profile option [Klaus]
- reformat doc comments of qcow2 [Markus]
- add input validation and checks for zoned information [Stefan]
- code style: format, comments, documentation, naming [Stefan]
- add tracing function for wp tracking [Stefan]
- reconstruct io path in check_zone_resources [Stefan]
v1->v2:
- add more tests to qemu-io zoned commands
- make zone append change state to full when wp reaches end
- add documentation to qcow2 zoned extension header
- address review comments (Stefan):
* fix zoned_mata allocation size
* use bitwise or than addition
* fix wp index overflow and locking
* cleanups: comments, naming
Sam Li (6):
docs/qcow2: add the zoned format feature
block: widen BlockLimits.zone_size to uint64_t
qcow2: add configurations for zoned format extension
virtio-blk: do not merge writes across a zone boundary
qcow2: add zoned emulation capability
iotests: test the zoned format feature for qcow2 file
block/block-backend.c | 11 + block/file-posix.c | 2 +- block/qcow2-cache.c | 11 +- block/qcow2-refcount.c | 7 + block/qcow2.c | 1617 ++++++++++++++++- block/qcow2.h | 88 +- block/trace-events | 2 + docs/interop/qcow2.rst | 118 +- docs/system/qemu-block-drivers.rst.inc | 51 + hw/block/virtio-blk.c | 22 +- include/block/block_int-common.h | 15 +- include/system/block-backend-io.h | 1 + qapi/block-core.json | 91 +- tests/qemu-iotests/031.out | 8 +- tests/qemu-iotests/036.out | 4 +- tests/qemu-iotests/082.out | 126 ++ tests/qemu-iotests/303.out | 4 +- tests/qemu-iotests/tests/qcow2-encryption.out | 4 +- tests/qemu-iotests/tests/zoned-qcow2 | 209 +++ tests/qemu-iotests/tests/zoned-qcow2.out | 191 ++ 20 files changed, 2539 insertions(+), 43 deletions(-) create mode 100755 tests/qemu-iotests/tests/zoned-qcow2 create mode 100644 tests/qemu-iotests/tests/zoned-qcow2.out
Signature · 1 line
2.53.0
PATCH v14 1/6 docs/qcow2: add the zoned format feature
Add the specs for the zoned format feature of the qcow2 driver. The qcow2 file then can emulate real zoned devices, either passed through by virtio-blk device or NVMe ZNS drive to the guest given zoned information.
--- docs/system/qemu-block-drivers.rst.inc | 51 ++++++++++++++++++++++++++ 1 file changed, 51 insertions(+)
docs/system/qemu-block-drivers.rst.inc +51 -0
@@ -172,6 +172,57 @@ This section describes each format and the options that are supported for it. filename`` to check if the NOCOW flag is set or not (Capital 'C' is NOCOW flag). + .. option:: zone.mode + + If this is set to ``host-managed``, the image is an emulated zoned + block device. This option is only valid for emulated zoned device files. + + .. option:: zone.size + + The size of a zone in bytes. The device is divided into zones of this + size with the exception of the last zone, which may be smaller. + Defaults to ``256 MiB``. + + .. option:: zone.capacity + + The initial capacity value, in bytes, for all zones. The capacity must + be less than or equal to zone size. If the last zone is smaller, then + its capacity is capped. Defaults to ``zone.size``. + + The zone capacity is per zone and may be different between zones in real + devices. QCow2 sets all zones to the same capacity. + + .. option:: zone.conventional_zones + + The number of conventional zones of the zoned device. Defaults to ``0`` + (all zones are sequential-write-required). + + .. option:: zone.max_active_zones + + The maximum allowed active zones (zones in the implicit open, explicit + open, or closed state). + + The max active zones must be less or equal to the number of SWR + (sequential write required) zones of the device. Defaults to ``0``, + meaning no limit. + + .. option:: zone.max_open_zones + + The maximum allowed open zones (zones in the implicit open or explicit + open state). The max open zones must not be larger than the max active + zones. Defaults to ``zone.max_active_zones`` if that is set, otherwise + ``0`` (no limit). + + If the limits of open zones or active zones are equal to the number of + SWR zones, then it is the same as having no limits. + + .. option:: zone.max_append_bytes + + The maximum number of bytes of a zone append request that can be issued + to the device. It must be 512-byte aligned and less than the zone + capacity. A value of ``0`` means that zone append requests are not + supported. Defaults to ``64 KiB``. + .. program:: image-formats .. option:: qed
Signature · 1 line
2.53.0
PATCH v14 2/6 block: widen BlockLimits.zone_size to uint64_t
In reply to Sam LiThe zone-size field in BlockLimits is currently uint32_t, capping expressible zone sizes at 4 GiB. Real zoned-device protocols like NVMe ZNS allow larger zones. Widen BlockLimits.zone_size to uint64_t to match.
--- block/file-posix.c | 2 +- include/block/block_int-common.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
block/file-posix.c +1 -1
@@ -3600,7 +3600,7 @@ raw_co_zone_append(BlockDriverState *bs,if (*offset & zone_size_mask) {error_report ("sector offset %" PRId64" is not aligned to zone size " -"%" PRId32"" , *offset /512 , bs->bl.zone_size /512 ); +"%" PRId64"" , *offset /512 , bs->bl.zone_size /512 );return -EINVAL ; }
include/block/block_int-common.h +1 -1
@@ -901,7 +901,7 @@ typedef struct BlockLimits {BlockZoneModel zoned;/* zone size expressed in bytes */ -uint32_t zone_size; +uint64_t zone_size;/* total number of zones */ uint32_t nr_zones;
Signature · 1 line
2.53.0
Re: [PATCH v14 2/6] block: widen BlockLimits.zone_size to uint64_t
Quoted 11 lines · Sam Li
The zone-size field in BlockLimits is currently
uint32_t, capping expressible zone sizes at 4 GiB. Real zoned-device protocols like NVMe ZNS allow larger zones. Widen BlockLimits.zone_size touint64_tto match.Signed-off-by Sam Li <[email protected]>Reviewed-by Niklas Cassel <[email protected]>--- block/file-posix.c | 2 +- include/block/block_int-common.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
PATCH v14 3/6 qcow2: add configurations for zoned format extension
In reply to Sam LiTo configure the zoned format feature on the qcow2 driver, it requires settings as: the device size, zone model, zone size, zone capacity, number of conventional zones, limits on zone resources (max append bytes, max open zones, and max_active_zones).
To create a qcow2 image with zoned format feature, use command like this:
qemu-img create -f qcow2 zbc.qcow2 -o size=768M \
-o zone.size=64M -o zone.capacity=64M -o zone.conventional_zones=0 \
-o zone.max_append_bytes=4096 -o zone.max_open_zones=6 \
-o zone.max_active_zones=8 -o zone.mode=host-managed
--- block/qcow2.c | 366 +++++++++++++++++- block/qcow2.h | 86 +++- docs/interop/qcow2.rst | 118 +++++- include/block/block_int-common.h | 13 + qapi/block-core.json | 91 ++++- tests/qemu-iotests/031.out | 8 +- tests/qemu-iotests/036.out | 4 +- tests/qemu-iotests/082.out | 126 ++++++ tests/qemu-iotests/303.out | 4 +- tests/qemu-iotests/tests/qcow2-encryption.out | 4 +- 10 files changed, 803 insertions(+), 17 deletions(-)
block/qcow2.c +363 -3
@@ -73,6 +73,7 @@ typedef struct {#define QCOW2_EXT_MAGIC_CRYPTO_HEADER 0x0537be77#define QCOW2_EXT_MAGIC_BITMAPS 0x23852875#define QCOW2_EXT_MAGIC_DATA_FILE 0x44415441 +#define QCOW2_EXT_MAGIC_ZONED_FORMAT 0x007a6264 static int coroutine_fn qcow2_co_preadv_compressed(BlockDriverState *bs,
@@ -194,6 +195,95 @@ qcow2_extract_crypto_opts(QemuOpts *opts, const char *fmt, Error **errp)return cryptoopts_qdict; } +/* +* Returns true if zone_opt is valid, false otherwise. +*/ +static bool +qcow2_check_zone_options (Qcow2ZonedHeaderExtension *zone_opt) +{ +uint32_t sequential_zones; + +assert (zone_opt !=NULL ); + +if (zone_opt->zoned !=QCOW2_Z_NONE && zone_opt->zoned !=QCOW2_Z_HM ) { +warn_report ("Zoned extension header zoned field has unknown " +"value %" PRIu8, zone_opt->zoned); +return false; + } + +if (zone_opt->zone_size ==0 ) { +warn_report ("Zoned extension header zone_size field must not be 0" ); +return false; + } + +if (!is_power_of_2 (zone_opt->zone_size)) { +warn_report ("Zoned extension header zone_size %" PRIu64 +"B is not a power of 2" , zone_opt->zone_size); +return false; + } + +if (zone_opt->nr_zones >QCOW2_MAX_NR_ZONES ) { +warn_report ("Zoned extension header nr_zones %" PRIu32 +" exceeds maximum %u" , + zone_opt->nr_zones,QCOW2_MAX_NR_ZONES ); +return false; + } + +if (zone_opt->zone_capacity > zone_opt->zone_size) { +warn_report ("zone capacity %" PRIu64"B exceeds zone size " +"%" PRIu64"B" , zone_opt->zone_capacity, + zone_opt->zone_size); +return false; + } + +if (!QEMU_IS_ALIGNED (zone_opt->max_append_bytes,BDRV_SECTOR_SIZE )) { +warn_report ("max append bytes %" PRIu32"B is not aligned " +"to %" PRIu64, zone_opt->max_append_bytes, + (uint64_t )BDRV_SECTOR_SIZE ); +return false; + } + +if ((uint64_t )zone_opt->max_append_bytes +BDRV_SECTOR_SIZE >= + zone_opt->zone_capacity) { +warn_report ("max append bytes %" PRIu32"B exceeds zone " +"capacity %" PRIu64"B by more than block size" , + zone_opt->max_append_bytes, zone_opt->zone_capacity); +return false; + } + +if (zone_opt->conventional_zones >= zone_opt->nr_zones) { +warn_report ("Conventional_zones %" PRIu32" exceeds " +"nr_zones %" PRIu32"." , + zone_opt->conventional_zones, zone_opt->nr_zones); +return false; + } + +if (zone_opt->max_active_zones > zone_opt->nr_zones) { +warn_report ("max_active_zones %" PRIu32" exceeds nr_zones %" PRIu32 +", clamping to nr_zones" , + zone_opt->max_active_zones, zone_opt->nr_zones); + zone_opt->max_active_zones = zone_opt->nr_zones; + } + + sequential_zones = zone_opt->nr_zones - zone_opt->conventional_zones; +if (zone_opt->max_open_zones > sequential_zones) { +warn_report ("max_open_zones %" PRIu32" exceeds the number of SWR " +"zones, clamping to %" PRIu32, + zone_opt->max_open_zones, sequential_zones); + zone_opt->max_open_zones = sequential_zones; + } +if (zone_opt->max_active_zones !=0 && + zone_opt->max_open_zones > zone_opt->max_active_zones) { +warn_report ("max_open_zones %" PRIu32" exceeds max_active_zones " +"%" PRIu32", clamping to max_active_zones" , + zone_opt->max_open_zones, + zone_opt->max_active_zones); + zone_opt->max_open_zones = zone_opt->max_active_zones; + } + +return true; +} +/* * read qcow2 extension and fill bs * start reading from start_offset
@@ -211,6 +301,7 @@ qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset,uint64_t offset;int ret;Qcow2BitmapHeaderExt bitmaps_ext; +Qcow2ZonedHeaderExtension zoned_ext;if (need_update_header !=NULL ) { *need_update_header = false;
@@ -432,6 +523,83 @@ qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset,break ; } +case QCOW2_EXT_MAGIC_ZONED_FORMAT : + { +if (ext.len !=sizeof (zoned_ext)) { +error_setg (errp,"zoned_ext: unexpected len=%" PRIu32" " +"(expected %zu)" , ext.len,sizeof (zoned_ext)); +return -EINVAL ; + } + ret =bdrv_pread (bs->file, offset, ext.len, &zoned_ext,0 ); +if (ret <0 ) { +error_setg_errno (errp, -ret,"zoned_ext: " +"Could not read ext header" ); +return ret; + } + + zoned_ext.zone_size =be64_to_cpu (zoned_ext.zone_size); + zoned_ext.zone_capacity =be64_to_cpu (zoned_ext.zone_capacity); + zoned_ext.conventional_zones = +be32_to_cpu (zoned_ext.conventional_zones); + zoned_ext.nr_zones =be32_to_cpu (zoned_ext.nr_zones); + zoned_ext.max_open_zones =be32_to_cpu (zoned_ext.max_open_zones); + zoned_ext.max_active_zones = +be32_to_cpu (zoned_ext.max_active_zones); + zoned_ext.max_append_bytes = +be32_to_cpu (zoned_ext.max_append_bytes); + s->zoned_header = zoned_ext; + +/* validate the header first */ +if (!qcow2_check_zone_options (&zoned_ext)) { +error_setg (errp,"Invalid zoned extension header" ); +return -EINVAL ; + } +/* +* refuse to open broken images: reject untrusted total_sectors +* that would overflow when converted to bytes, then verify +* nr_zones matches the device size. +*/ +if (bs->total_sectors <0 || + (uint64_t )bs->total_sectors >UINT64_MAX /BDRV_SECTOR_SIZE ) { +error_setg (errp,"Image size overflows when converted " +"to bytes" ); +return -EINVAL ; + } +if (zoned_ext.nr_zones !=DIV_ROUND_UP ( + (uint64_t )bs->total_sectors *BDRV_SECTOR_SIZE , + zoned_ext.zone_size)) { +error_setg (errp,"Zoned extension header nr_zones field " +"is wrong" ); +return -EINVAL ; + } + +/* +* Reject a header that claims more zones than the file can hold. +*/ + { +int64_t file_length =bdrv_co_getlength (bs->file->bs); +uint64_t wp_table_size = + (uint64_t )zoned_ext.nr_zones *sizeof (uint64_t ); + +if (file_length <0 ) { +return file_length; + } +if (zoned_ext.zonedmeta_offset > (uint64_t )file_length || + (uint64_t )file_length - zoned_ext.zonedmeta_offset < + wp_table_size) { +error_setg (errp,"Zoned metadata region exceeds the " +"image file size" ); +return -EINVAL ; + } + } + +#ifdef DEBUG_EXT +printf ("Qcow2: Got zoned format extension: " +"offset=%" PRIu64"\n" , offset); +#endif +break ; + } +default :/* unknown magic - save it in case we need to rewrite the header */ /* If you add a new feature, make sure to also update the fast
@@ -2068,6 +2236,25 @@ static void qcow2_refresh_limits(BlockDriverState *bs, Error **errp) } bs->bl.pwrite_zeroes_alignment = s->subcluster_size; bs->bl.pdiscard_alignment = s->cluster_size; + +switch (s->zoned_header.zoned) { +case QCOW2_Z_HM : + bs->bl.zoned =BLK_Z_HM ; +break ; +case QCOW2_Z_NONE : +default : + bs->bl.zoned =BLK_Z_NONE ; +break ; + } + + bs->bl.nr_zones = s->zoned_header.nr_zones; + bs->bl.max_append_sectors = s->zoned_header.max_append_bytes + >>BDRV_SECTOR_BITS ; + bs->bl.max_active_zones = s->zoned_header.max_active_zones; + bs->bl.max_open_zones = s->zoned_header.max_open_zones; + bs->bl.zone_size = s->zoned_header.zone_size; + bs->bl.zone_capacity = s->zoned_header.zone_capacity; + bs->bl.write_granularity =BDRV_SECTOR_SIZE ; }static int GRAPH_UNLOCKED
@@ -3170,6 +3357,11 @@ int qcow2_update_header(BlockDriverState *bs) .bit = QCOW2_INCOMPAT_EXTL2_BITNR, .name = "extended L2 entries", }, + { + .type = QCOW2_FEAT_TYPE_INCOMPATIBLE, + .bit = QCOW2_INCOMPAT_ZONED_FORMAT_BITNR, + .name = "zoned format", + }, { .type = QCOW2_FEAT_TYPE_COMPATIBLE, .bit = QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR,
@@ -3215,6 +3407,31 @@ int qcow2_update_header(BlockDriverState *bs) buflen -= ret; } +/* Zoned devices header extension */ +if (s->zoned_header.zoned ==QCOW2_Z_HM ) { +Qcow2ZonedHeaderExtension zoned_header = { + .zoned = s->zoned_header.zoned, + .zone_size =cpu_to_be64 (s->zoned_header.zone_size), + .zone_capacity =cpu_to_be64 (s->zoned_header.zone_capacity), + .conventional_zones = +cpu_to_be32 (s->zoned_header.conventional_zones), + .nr_zones =cpu_to_be32 (s->zoned_header.nr_zones), + .max_open_zones =cpu_to_be32 (s->zoned_header.max_open_zones), + .max_active_zones = +cpu_to_be32 (s->zoned_header.max_active_zones), + .max_append_bytes = +cpu_to_be32 (s->zoned_header.max_append_bytes) + }; + ret =header_ext_add (buf,QCOW2_EXT_MAGIC_ZONED_FORMAT , + &zoned_header,sizeof (zoned_header), + buflen); +if (ret <0 ) { + goto fail; + } + buf += ret; + buflen -= ret; + } +/* Keep unknown header extensions */ QLIST_FOREACH(uext, &s->unknown_header_ext, next) { ret =header_ext_add (buf, uext->magic, uext->data, uext->len, buflen);
@@ -3589,6 +3806,8 @@ qcow2_co_create(BlockdevCreateOptions *create_options, Error **errp)ERRP_GUARD ();BlockdevCreateOptionsQcow2 *qcow2_opts;QDict *options; +Qcow2ZoneCreateOptions *zone_struct; +Qcow2ZoneHostManaged *zone_host_managed;/* * Open the image file and write a minimal qcow2 header.
@@ -3615,6 +3834,8 @@ qcow2_co_create(BlockdevCreateOptions *create_options, Error **errp)assert (create_options->driver ==BLOCKDEV_DRIVER_QCOW2 ); qcow2_opts = &create_options->u.qcow2; + zone_struct = qcow2_opts->zone; + zone_host_managed =NULL ; bs =bdrv_co_open_blockdev_ref (qcow2_opts->file, errp);if (bs ==NULL ) {
@@ -3828,6 +4049,14 @@ qcow2_co_create(BlockdevCreateOptions *create_options, Error **errp) header->incompatible_features |=cpu_to_be64 (QCOW2_INCOMPAT_DATA_FILE ); } +if (zone_struct && zone_struct->mode ==QCOW2_ZONE_MODEL_HOST_MANAGED ) { +/* +* The incompatible bit must be set when the zone model is +* host-managed +*/ + header->incompatible_features |= +cpu_to_be64 (QCOW2_INCOMPAT_ZONED_FORMAT ); + }if (qcow2_opts->data_file_raw) { header->autoclear_features |=cpu_to_be64 (QCOW2_AUTOCLEAR_DATA_FILE_RAW );
@@ -3885,10 +4114,9 @@ qcow2_co_create(BlockdevCreateOptions *create_options, Error **errp)bdrv_graph_co_rdlock (); ret =qcow2_alloc_clusters (blk_bs (blk),3 * cluster_size);if (ret <0 ) { -bdrv_graph_co_rdunlock ();error_setg_errno (errp, -ret,"Could not allocate clusters for qcow2 " "header and refcount table" ); - goto out; + goto unlock; }else if (ret !=0 ) {error_report ("Huh, first cluster in empty image is already in use?" );
@@ -3896,11 +4124,78 @@ qcow2_co_create(BlockdevCreateOptions *create_options, Error **errp) }/* Set the external data file if necessary */ +BDRVQcow2State *s =blk_bs (blk)->opaque;if (data_bs) { -BDRVQcow2State *s =blk_bs (blk)->opaque; s->image_data_file =g_strdup (data_bs->filename); } +if (zone_struct && zone_struct->mode ==QCOW2_ZONE_MODEL_HOST_MANAGED ) { + s->zoned_header.zoned =QCOW2_Z_HM ; + zone_host_managed = &zone_struct->u.host_managed; + +if (zone_host_managed->has_size) { + s->zoned_header.zone_size = zone_host_managed->size; + }else { + s->zoned_header.zone_size =DEFAULT_ZONE_SIZE ; + } + +if (s->zoned_header.zone_size ==0 ) { +error_setg (errp,"Zoned extension header zone_size field " +"can not be 0" ); + s->zoned_header.zoned =QCOW2_Z_NONE ; + ret = -EINVAL ; + goto unlock; + } + s->zoned_header.nr_zones =DIV_ROUND_UP (qcow2_opts->size, + s->zoned_header.zone_size); + +if (zone_host_managed->has_capacity) { + s->zoned_header.zone_capacity = zone_host_managed->capacity; + }else { + s->zoned_header.zone_capacity = s->zoned_header.zone_size; + } + +if (zone_host_managed->has_conventional_zones) { + s->zoned_header.conventional_zones = + zone_host_managed->conventional_zones; + }else { + s->zoned_header.conventional_zones =0 ; + } + +if (zone_host_managed->has_max_active_zones) { + s->zoned_header.max_active_zones = + zone_host_managed->max_active_zones; + }else { + s->zoned_header.max_active_zones =0 ; + } + +if (zone_host_managed->has_max_open_zones) { + s->zoned_header.max_open_zones = + zone_host_managed->max_open_zones; + }else if (zone_host_managed->has_max_active_zones) { + s->zoned_header.max_open_zones = + zone_host_managed->max_active_zones; + }else { + s->zoned_header.max_open_zones =0 ; + } + +if (zone_host_managed->has_max_append_bytes) { + s->zoned_header.max_append_bytes = + zone_host_managed->max_append_bytes; + }else { + s->zoned_header.max_append_bytes =DEFAULT_ZONE_MAX_APPEND_BYTES ; + } + +if (!qcow2_check_zone_options (&s->zoned_header)) { +error_setg (errp,"Invalid zoned device options" ); + s->zoned_header.zoned =QCOW2_Z_NONE ; + ret = -EINVAL ; + goto unlock; + } + }else { + s->zoned_header.zoned =QCOW2_Z_NONE ; + } +/* Create a full header (including things like feature table) */ ret =qcow2_update_header (blk_bs (blk));bdrv_graph_co_rdunlock ();
@@ -3974,6 +4269,9 @@ qcow2_co_create(BlockdevCreateOptions *create_options, Error **errp) } ret =0 ; + goto out; +unlock: +bdrv_graph_co_rdunlock (); out:blk_co_unref (blk);bdrv_co_unref (bs);
@@ -4052,6 +4350,10 @@ qcow2_co_create_opts(BlockDriver *drv, const char *filename, QemuOpts *opts, { BLOCK_OPT_COMPAT_LEVEL, "version" }, { BLOCK_OPT_DATA_FILE_RAW, "data-file-raw" }, { BLOCK_OPT_COMPRESSION_TYPE, "compression-type" }, + { BLOCK_OPT_CONVENTIONAL_ZONES, "zone.conventional-zones" }, + { BLOCK_OPT_MAX_OPEN_ZONES, "zone.max-open-zones" }, + { BLOCK_OPT_MAX_ACTIVE_ZONES, "zone.max-active-zones" }, + { BLOCK_OPT_MAX_APPEND_BYTES, "zone.max-append-bytes" }, { NULL, NULL }, };
@@ -5448,6 +5750,29 @@ qcow2_get_specific_info(BlockDriverState *bs, Error **errp) .data_file_raw = data_file_is_raw(bs), .compression_type = s->compression_type, }; +if (s->zoned_header.zoned ==QCOW2_Z_HM ) { +ImageInfoSpecificQCow2Zoned *z = +g_new0 (ImageInfoSpecificQCow2Zoned,1 ); + *z = (ImageInfoSpecificQCow2Zoned ){ + .mode =QCOW2_ZONE_MODEL_HOST_MANAGED , + .nr_zones = s->zoned_header.nr_zones, + .u.host_managed = { + .has_size = true, + .size = s->zoned_header.zone_size, + .has_capacity = true, + .capacity = s->zoned_header.zone_capacity, + .has_conventional_zones = true, + .conventional_zones = s->zoned_header.conventional_zones, + .has_max_open_zones = true, + .max_open_zones = s->zoned_header.max_open_zones, + .has_max_active_zones = true, + .max_active_zones = s->zoned_header.max_active_zones, + .has_max_append_bytes = true, + .max_append_bytes = s->zoned_header.max_append_bytes, + }, + }; + spec_info->u.qcow2.data->zone = z; + } }else {/* if this assertion fails, this probably means a new version was * added without having it covered here */
@@ -6271,6 +6596,41 @@ static QemuOptsList qcow2_create_opts = { .type = QEMU_OPT_BOOL, \ .help ="Assume the external data file already exists and " \"do not overwrite it" \ + }, \ + { \ + .name = BLOCK_OPT_ZONE_MODEL, \ + .type = QEMU_OPT_STRING, \ + .help = "zone model modes, mode choice: host-managed", \ + }, \ + { \ + .name = BLOCK_OPT_ZONE_SIZE, \ + .type = QEMU_OPT_SIZE, \ + .help = "zone size", \ + }, \ + { \ + .name = BLOCK_OPT_ZONE_CAPACITY, \ + .type = QEMU_OPT_SIZE, \ + .help = "zone capacity", \ + }, \ + { \ + .name = BLOCK_OPT_CONVENTIONAL_ZONES, \ + .type = QEMU_OPT_NUMBER, \ + .help = "numbers of conventional zones", \ + }, \ + { \ + .name = BLOCK_OPT_MAX_APPEND_BYTES, \ + .type = QEMU_OPT_SIZE, \ + .help = "max append bytes", \ + }, \ + { \ + .name = BLOCK_OPT_MAX_ACTIVE_ZONES, \ + .type = QEMU_OPT_NUMBER, \ + .help = "max active zones", \ + }, \ + { \ + .name = BLOCK_OPT_MAX_OPEN_ZONES, \ + .type = QEMU_OPT_NUMBER, \ + .help ="max open zones" , \ }, QCOW_COMMON_OPTIONS, { /* end of list */ }
block/qcow2.h +85 -1
120 lines not on this page · raw message
docs/interop/qcow2.rst +117 -1
136 lines not on this page · raw message
include/block/block_int-common.h +13 -0
@@ -57,6 +57,13 @@#define BLOCK_OPT_COMPRESSION_TYPE "compression_type"#define BLOCK_OPT_EXTL2 "extended_l2"#define BLOCK_OPT_KEEP_DATA_FILE "keep_data_file" +#define BLOCK_OPT_ZONE_MODEL "zone.mode" +#define BLOCK_OPT_ZONE_SIZE "zone.size" +#define BLOCK_OPT_ZONE_CAPACITY "zone.capacity" +#define BLOCK_OPT_CONVENTIONAL_ZONES "zone.conventional_zones" +#define BLOCK_OPT_MAX_APPEND_BYTES "zone.max_append_bytes" +#define BLOCK_OPT_MAX_ACTIVE_ZONES "zone.max_active_zones" +#define BLOCK_OPT_MAX_OPEN_ZONES "zone.max_open_zones"#define BLOCK_PROBE_BUF_SIZE 512
@@ -903,6 +910,12 @@ typedef struct BlockLimits {/* zone size expressed in bytes */ uint64_t zone_size; +/* +* The number of usable logical blocks within the zone, expressed +* in bytes. A zone capacity is smaller or equal to the zone size. +*/ +uint64_t zone_capacity; +/* total number of zones */ uint32_t nr_zones;
qapi/block-core.json +89 -2
121 lines not on this page · raw message
tests/qemu-iotests/031.out +4 -4
@@ -117,7 +117,7 @@ header_length 112 Header extension: magic 0x6803f857 (Feature table) -length 384 +length 432 data <binary> Header extension:
@@ -150,7 +150,7 @@ header_length 112 Header extension: magic 0x6803f857 (Feature table) -length 384 +length 432 data <binary> Header extension:
@@ -164,7 +164,7 @@ No errors were found on the image. magic 0x514649fb version 3 -backing_file_offset 0x240 +backing_file_offset 0x270 backing_file_size 0x17 cluster_bits 16 size 67108864
@@ -188,7 +188,7 @@ data 'host_device' Header extension: magic 0x6803f857 (Feature table) -length 384 +length 432 data <binary> Header extension:
tests/qemu-iotests/036.out +2 -2
@@ -26,7 +26,7 @@ compatible_features [] autoclear_features [63] Header extension: magic 0x6803f857 (Feature table) -length 384 +length 432 data <binary>
@@ -38,7 +38,7 @@ compatible_features [] autoclear_features [] Header extension: magic 0x6803f857 (Feature table) -length 384 +length 432 data <binary> *** done
tests/qemu-iotests/082.out +126 -0
234 lines not on this page · raw message
tests/qemu-iotests/303.out +2 -2
@@ -47,7 +47,7 @@ header_length 112 Header extension: magic 0x6803f857 (Feature table) -length 384 +length 432 data <binary> Header extension:
@@ -133,7 +133,7 @@ wrote 1048576/1048576 bytes at offset 7340032 { "name": "Feature table", "magic": 1745090647, - "length": 384, + "length": 432, "data_str": "<binary>" }, {
tests/qemu-iotests/tests/qcow2-encryption.out +2 -2
@@ -10,7 +10,7 @@ data <binary> Header extension: magic 0x6803f857 (Feature table) -length 384 +length 432 data <binary> image: TEST_DIR/t.IMGFMT
@@ -24,7 +24,7 @@ No errors were found on the image. Header extension: magic 0x6803f857 (Feature table) -length 384 +length 432 data <binary> qemu-img: Could not open 'TEST_DIR/t.IMGFMT': Missing CRYPTO header for crypt method 2
Signature · 1 line
2.53.0
Re: [PATCH v14 3/6] qcow2: add configurations for zoned format extension
Hello Sam,
Quoted 13 lines · Sam Li
block/qcow2.c +9
-0@@ -2068,6 +2236,25 @@ static void qcow2_refresh_limits(BlockDriverState *bs, Error **errp) } bs->bl.pwrite_zeroes_alignment = s->subcluster_size; bs->bl.pdiscard_alignment = s->cluster_size; + +switch (s->zoned_header.zoned) { +case QCOW2_Z_HM : + bs->bl.zoned =BLK_Z_HM ; +break ; +case QCOW2_Z_NONE : +default : + bs->bl.zoned =BLK_Z_NONE ; +break ;
block/qcow2.c static void qcow2_refresh_limits(BlockDriverState *bs, Error **errp) · line 2257
Quoted 10 lines
block/qcow2.c +10
-0+ } + + bs->bl.nr_zones = s->zoned_header.nr_zones; + bs->bl.max_append_sectors = s->zoned_header.max_append_bytes + >>BDRV_SECTOR_BITS ; + bs->bl.max_active_zones = s->zoned_header.max_active_zones; + bs->bl.max_open_zones = s->zoned_header.max_open_zones; + bs->bl.zone_size = s->zoned_header.zone_size; + bs->bl.zone_capacity = s->zoned_header.zone_capacity; + bs->bl.write_granularity =BDRV_SECTOR_SIZE ;
Since we are already defining max_append_sectors in the qcow2 extension, is there a reason not to allow the user to define the write_granularity?
From the virtio spec:
"The size of the data that is appended MUST be a multiple of write_granularity bytes and MUST NOT exceed the max_append_sectors value provided by the device in virtio_blk_zoned_characteristics configuration space structure."
At least for virtio, it seems like they go together.
And if you consider that virtio is a specification that can be implemented by hardware, somehow I do feel like we should (somehow) be able to make the backing device (qcow2 in this case) represent the hardware.
And I don't really see the logic to define all the struct members in from virtio
struct virtio_blk_zoned_characteristics {
le32 zone_sectors;
le32 max_open_zones;
le32 max_active_zones;
le32 max_append_sectors;
le32 write_granularity;
u8 model;
u8 unused2[3];
} zoned;except for write_granularity. (I guess we could still default write_granularity to BDRV_SECTOR_SIZE.)
If we define max_append_sectors in qcow2, I think we should define write_granularity in qcow2 as well.
Looking at the write_granularity in virtio-blk + file-posix:
for blkcfg.zoned.write_granularity, which is what virtio-blk exposes to the guest, it is initialized using:
https://github.com/qemu/qemu/blob/v11.1.0-rc3/hw/block/virtio-blk.c#L1257conf->logical_block_size:
https://github.com/qemu/qemu/blob/v11.1.0-rc3/hw/block/virtio-blk.c#L1183
and the value we set to the qemu block layer - bs->bl.write_granularity, which file-posix initilizes using:
https://github.com/qemu/qemu/blob/v11.1.0-rc3/block/file-posix.c#L1498-L1501
the sysfs value for physical_block_size.
Sam, you seem to be the author of the code for both assignments. It seems inconsistent that one uses the logical block size and one uses the physical block size. I could send a patch for this if we know which one to use. I assume that using the logical block size in both places makes most sense for writes (even if it would be a read-modify-write), but the physical block size makes most sense for zone appends... Damien, thoughts?
There are some ZAC drives that support 512e, but they obviously don't support zone append.
Trying to understand how qemu deals with other hardware limits:
logical_block_size and physical_block_size are parameters for the virtio-blk-pci device, rather than the qcow2 blockdev.
E.g.
-drive file=vm-disk.qcow2,format=qcow2,if=none,id=hd0 \
-device virtio-blk-pci,drive=hd0,logical_block_size=4096,physical_block_size=4096
for a 4K native device.
Looking at the qcow2 driver, we can see that it does not set bs->bl.request_alignment in the normal case (only if using crypto).
Looking at the file-posix driver, it does probe the logical block size of the backing device (using ioctls()) and stores it in bl.request_alignment.
The virtio-blk driver does have a check:virtio_blk_sect_range_ok() where it will fail any request where the size is not aligned to the logical block size, so I guess it is fine that the qcow2 driver does not initialize bs->bl.request_alignment.
Perhaps we should modify check_zoned_request() to also check if the len is a multiple of the logical block size?
Without such a check, for a 4Kn device (physical block size == 4K, logical block size == 4K), with write_granularity unconditionally set to BDRV_SECTOR_SIZE (512) (like the qcow2 driver seems to do), I think we would be able to append less than a logical block, which seems wrong.
(This is not a problem right now since file-posix.c does initialize bs->bl.write_granularity using the sysfs value for physical_block_size and not a hardcoded 512 value.)
Kind regards,
Niklas
Re: [PATCH v14 3/6] qcow2: add configurations for zoned format extension
Quoted 11 lines · Niklas Cassel
Looking at the
write_granularityin virtio-blk + file-posix:
for blkcfg.zoned.write_granularity, which is what virtio-blk exposes to the guest, it is initialized using:
https://github.com/qemu/qemu/blob/v11.1.0-rc3/hw/block/virtio-blk.c#L1257conf->logical_block_size:
https://github.com/qemu/qemu/blob/v11.1.0-rc3/hw/block/virtio-blk.c#L1183and the value we set to the qemu block layer -
bs->bl.write_granularity, which file-posix initilizes using:
https://github.com/qemu/qemu/blob/v11.1.0-rc3/block/file-posix.c#L1498-L1501
the sysfs value forphysical_block_size.
If we are using sysfs, I wonder why this code does not simply read:
/sys/block/<disk>/queue/zone_write_granularity
instead.
But that would still be inconsistent with blkcfg.zoned.write_granularity which is initialized using conf->logical_block_size.
Kind regards,
Niklas
Re: [PATCH v14 3/6] qcow2: add configurations for zoned format extension
Quoted 13 lines · Niklas Cassel
Quoted 11 lines · Niklas Cassel
Looking at the
write_granularityin virtio-blk + file-posix:
for blkcfg.zoned.write_granularity, which is what virtio-blk exposes to the guest, it is initialized using:
https://github.com/qemu/qemu/blob/v11.1.0-rc3/hw/block/virtio-blk.c#L1257conf->logical_block_size:
https://github.com/qemu/qemu/blob/v11.1.0-rc3/hw/block/virtio-blk.c#L1183and the value we set to the qemu block layer -
bs->bl.write_granularity, which file-posix initilizes using:
https://github.com/qemu/qemu/blob/v11.1.0-rc3/block/file-posix.c#L1498-L1501
the sysfs value forphysical_block_size.
Quoted 7 lines
If we are using sysfs, I wonder why this code does not simply read:
/sys/block/<disk>/queue/zone_write_granularity
instead.But that would still be inconsistent with blkcfg.zoned.write_granularity which is initialized using
conf->logical_block_size.
At least QEMU is currently bug compatible with Linux:
linux $ git grep "zone_write_granularity ="
block/blk-settings.c: lim->zone_write_granularity = lim->logical_block_size; drivers/scsi/sd_zbc.c: lim->zone_write_granularity = sdkp->physical_block_size;
Where blk-settings is using the logical block size, but SCSI is using the physical block size :)
Kind regards,
Niklas
PATCH v14 4/6 virtio-blk: do not merge writes across a zone boundary
In reply to Sam Livirtio_blk_submit_multireq() fuses adjacent in-zone requests into a single request. On a zoned backend, a merged zone append request that straddles a zone boundary is rejected by the device because each write must stay within a single zone.
Add a bail condition to the merge coalescer: if combining the candidate request into the current batch would cross a zone boundary, flush the current batch and start a new one.
--- block/block-backend.c | 11 +++++++++++ hw/block/virtio-blk.c | 22 +++++++++++++++++++++- include/system/block-backend-io.h | 1 + 3 files changed, 33 insertions(+), 1 deletion(-)
block/block-backend.c +11 -0
@@ -2326,6 +2326,17 @@ uint32_t blk_get_request_alignment(BlockBackend *blk)return bs ? bs->bl.request_alignment :BDRV_SECTOR_SIZE ; } +/* +* Returns the zone size in bytes for a zoned backend, or 0 if @blk does +* not present zoned geometry. +*/ +uint64_t blk_get_zone_size (BlockBackend *blk) +{ +BlockDriverState *bs =blk_bs (blk); +IO_CODE (); +return bs ? bs->bl.zone_size :0 ; +} +/* Returns the optimal write zeroes alignment, in bytes; guaranteed nonzero */ uint32_t blk_get_pwrite_zeroes_alignment (BlockBackend *blk) {
hw/block/virtio-blk.c +21 -1
@@ -294,6 +294,9 @@ static void virtio_blk_submit_multireq(VirtIOBlock *s, MultiReqBuffer *mrb)int i =0 , start =0 , num_reqs =0 , niov =0 , nb_sectors =0 ;uint32_t max_transfer;int64_t sector_num =0 ; +uint64_t zone_size =blk_get_zone_size (s->blk); +bool zone_cross; +int64_t zone_sector, end_sector;if (mrb->num_reqs ==1 ) {submit_requests (s, mrb,0 ,1 ,-1 );
@@ -309,17 +312,34 @@ static void virtio_blk_submit_multireq(VirtIOBlock *s, MultiReqBuffer *mrb)for (i =0 ; i < mrb->num_reqs; i++) {VirtIOBlockReq *req = mrb->reqs[i];if (num_reqs >0 ) { + zone_cross = false; + +/* +* On zoned backends, a single backend write/read must not span +* a zone boundary. Bail out of merging if combining req into +* the current batch would straddle a zone. +*/ +if (zone_size >0 ) { + zone_sector = zone_size /BDRV_SECTOR_SIZE ; + end_sector = req->sector_num + + req->qiov.size /BDRV_SECTOR_SIZE -1 ; + zone_cross = (sector_num / zone_sector) != + (end_sector / zone_sector); + } +/* * NOTE: We cannot merge the requests in below situations: * 1. requests are not sequential * 2. merge would exceed maximum number of IOVs * 3. merge would exceed maximum transfer length of backend device +* 4. merge would cross a zone boundary on a zoned backend */ if (sector_num + nb_sectors != req->sector_num || niov >blk_get_max_iov (s->blk) - req->qiov.niov || req->qiov.size > max_transfer || nb_sectors > (max_transfer - - req->qiov.size) /BDRV_SECTOR_SIZE ) { + req->qiov.size) /BDRV_SECTOR_SIZE || + zone_cross) {submit_requests (s, mrb, start, num_reqs, niov); num_reqs =0 ; }
include/system/block-backend-io.h +1 -0
@@ -121,6 +121,7 @@ uint32_t blk_get_request_alignment(BlockBackend *blk);uint32_t blk_get_pwrite_zeroes_alignment (BlockBackend *blk);uint32_t blk_get_max_transfer (BlockBackend *blk);uint64_t blk_get_max_hw_transfer (BlockBackend *blk); +uint64_t blk_get_zone_size (BlockBackend *blk); int coroutine_fn blk_co_copy_range(BlockBackend *blk_in, int64_t off_in, BlockBackend *blk_out, int64_t off_out,
Signature · 1 line
2.53.0
PATCH v14 5/6 qcow2: add zoned emulation capability
In reply to Sam LiBy adding zone operations and zoned metadata, the zoned emulation capability enables full emulation support of zoned device using a qcow2 file. The zoned device metadata includes zone type, zoned device state and write pointer (WP) of each zone, which is stored to an array of unsigned integers.
WP accessor (qcow2_rw_wp_at) routes reads and writes of an 8-byte WP slot through the write pointer cache. The write pointer cache is written to disk after the qcow2 metadata is written, thus guaranteeing that the write pointer is updated after the corresponding data is written. Per-completion cache flush is deferred. The WP cluster reaches disk on the next flush.
Each zone of a zoned device makes state transitions following the zone state machine. The zone state machine mainly describes five states, IMPLICIT OPEN, EXPLICIT OPEN, FULL, EMPTY and CLOSED. READ ONLY and OFFLINE states will generally be affected by device internal events. The operations on zones cause corresponding state changing.
Zoned devices have limits on zone resources, which put constraints on write operations on zones. It is managed by active zone queues following LRU policy.
--- block/qcow2-cache.c | 11 +- block/qcow2-refcount.c | 7 + block/qcow2.c | 1219 +++++++++++++++++++++++++++++++++++++++- block/qcow2.h | 2 + block/trace-events | 2 + 5 files changed, 1234 insertions(+), 7 deletions(-)
block/qcow2-cache.c +10 -1
@@ -275,14 +275,23 @@ int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c, {int ret; +/* +* If the dependency graph is unchanged, nothing to do. This avoids +* a synchronous flush on every call below. +*/ +if (c->depends == dependency) { +return 0 ; + } +if (dependency->depends) { +/* Flatten any chain on the dependency side as a cycle-breaker. */ ret =qcow2_cache_flush_dependency (bs, dependency);if (ret <0 ) {return ret; } } -if (c->depends && (c->depends != dependency)) { +if (c->depends) { ret =qcow2_cache_flush_dependency (bs, c);if (ret <0 ) {return ret;
block/qcow2-refcount.c +7 -0
@@ -1239,6 +1239,13 @@ int qcow2_write_caches(BlockDriverState *bs) } } +if (s->wp_cache) { + ret =qcow2_cache_write (bs, s->wp_cache); +if (ret <0 ) { +return ret; + } + } +return 0 ; }
block/qcow2.c +1213 -6
1,322 lines not on this page · raw message
block/qcow2.h +2 -0
@@ -298,6 +298,8 @@ typedef struct Qcow2ZoneWPState { QTAILQ_HEAD(, Qcow2WPReq) in_flight;/* PENDING reqs, sorted by lba */ QTAILQ_HEAD(, Qcow2WPReq) completed_pending; +/* zone mgmt ops wait here until in_flight has drained */ +CoQueue drain; } Qcow2ZoneWPState; typedef struct Qcow2UnknownHeaderExtension {
block/trace-events +2 -0
@@ -76,6 +76,8 @@ qcow2_writev_data(void *co, uint64_t offset) "co %p offset 0x%" PRIx64 qcow2_pwrite_zeroes_start_req(void *co, int64_t offset, int64_t bytes) "co %p offset 0x%" PRIx64 " bytes %" PRId64 qcow2_pwrite_zeroes(void *co, int64_t offset, int64_t bytes) "co %p offset 0x%" PRIx64 " bytes %" PRId64 qcow2_skip_cow(void *co, uint64_t offset, int nb_clusters) "co %p offset 0x%" PRIx64 " nb_clusters %d" +qcow2_wp_tracking(int index, uint64_t wp) "wps[%d]: 0x%" PRIx64 +qcow2_imp_open_zones(uint8_t op, int nrz) "nr_imp_open_zones after op 0x%x: %d" # qcow2-cluster.c qcow2_alloc_clusters_offset(void *co, uint64_t offset, int bytes) "co %p offset 0x%" PRIx64 " bytes %d"
Signature · 1 line
2.53.0
PATCH v14 6/6 iotests: test the zoned format feature for qcow2 file
In reply to Sam LiThe zoned format feature can be tested by:
$ tests/qemu-iotests/check -qcow2 zoned-qcow2
--- tests/qemu-iotests/tests/zoned-qcow2 | 209 +++++++++++++++++++++++ tests/qemu-iotests/tests/zoned-qcow2.out | 191 +++++++++++++++++++++ 2 files changed, 400 insertions(+) create mode 100755 tests/qemu-iotests/tests/zoned-qcow2 create mode 100644 tests/qemu-iotests/tests/zoned-qcow2.out
tests/qemu-iotests/tests/zoned-qcow2 new file +209 -0
@@ -0,0 +1,209 @@ +#!/usr/bin/env bash +# +# SPDX-License-Identifier: GPL-2.0-or-later +# +# Test zone management operations for qcow2 file. +# + +seq="$(basename $0)" +echo "QA output created by $seq" +status=1 # failure is the default! + +file_name="zbc.qcow2" +_cleanup() +{ + _cleanup_test_img + _rm_test_img "$file_name" +} +trap "_cleanup; exit \$status" 0 1 2 3 15 + +# get standard environment, filters and checks +. ../common.rc +. ../common.filter +. ../common.qemu + +# This test only runs on Linux hosts with qcow2 image files. +_supported_fmt qcow2 +_supported_proto file +_supported_os Linux + +echo +echo "=== Initial image setup ===" +echo + +$QEMU_IMG create -f qcow2 $file_name -o size=768M -o zone.size=64M -o \ +zone.capacity=64M -o zone.conventional_zones=0 -o zone.max_append_bytes=32M \ +-o zone.max_open_zones=6 -o zone.max_active_zones=8 -o zone.mode=host-managed + +IMG="--image-opts -n driver=qcow2,file.driver=file,file.filename=$file_name" +QEMU_IO_OPTIONS=$QEMU_IO_OPTIONS_NO_FMT + +echo +echo "=== Testing a qcow2 img with zoned format ===" +echo +echo "case 1: test zone operations one by one" + +echo "(1) report zones[0]:" +$QEMU_IO $IMG -c "zrp 0 1" +echo +echo "report zones[0~9]:" +$QEMU_IO $IMG -c "zrp 0 10" +echo +echo "report zones[-1]:" # zones[-1] dictates the last zone +$QEMU_IO $IMG -c "zrp 0x2C000000 2" # 0x2C000000 / 512 = 0x160000 +echo +echo +echo "(2) open zones[0], zones[1], zones[-1] then close, finish, reset:" +$QEMU_IO $IMG << EOF +zo 0 0x4000000 +zrp 0 1 +zo 0x4000000 0x4000000 +zrp 0x4000000 1 +zo 0x2C000000 0x4000000 +zrp 0x2C000000 2 +zc 0 0x4000000 +zrp 0 1 +zc 0x4000000 0x4000000 +zrp 0x4000000 1 +zc 0x2C000000 0x4000000 +zrp 0x2C000000 2 +zf 0 0x4000000 +zrp 0 1 +zf 64M 64M +zrp 0x4000000 2 +zf 0x2C000000 0x4000000 +zrp 0x2C000000 2 +zrs 0 0x4000000 +zrp 0 1 +zrs 0x4000000 0x4000000 +zrp 0x4000000 1 +zrs 0x2C000000 0x4000000 +zrp 0x2C000000 2 +EOF + +echo +echo "(3) append write with (4k, 8k) data" +$QEMU_IO $IMG -c "zrp 0 12" # the physical block size of the device is 4096 +echo "Append write zones[0], zones[1] twice" +$QEMU_IO $IMG << EOF +zap -p 0 0x1000 0x2000 +zrp 0 1 +zap -p 0 0x1000 0x2000 +zrp 0 1 +zap -p 0x4000000 0x1000 0x2000 +zrp 0x4000000 1 +zap -p 0x4000000 0x1000 0x2000 +zrp 0x4000000 1 +EOF + +echo +echo "Reset all:" +$QEMU_IO $IMG -c "zrp 0 12" -c "zrs 0 768M" -c "zrp 0 12" +echo +echo + +echo "case 2: test a sets of ops that works or not" +echo "(1) append write (4k, 4k) and then write to full" +$QEMU_IO $IMG << EOF +zap -p 0 0x1000 0x1000 +zrp 0 1 +zap -p 0 0x1000 0x1ffd000 +zap -p 0 0x1000000 0x1000000 +zrp 0 1 +EOF + +echo "Reset zones[0]:" +$QEMU_IO $IMG -c "zrs 0 64M" -c "zrp 0 1" + +echo "(2) write in zones[0], zones[3], zones[8], and then reset all" +$QEMU_IO $IMG << EOF +zap -p 0 0x1000 0x1000 +zap -p 0xc000000 0x1000 0x1000 +zap -p 0x20000000 0x1000 0x1000 +zrp 0 12 +zrs 0 768M +zrp 0 12 +EOF + +echo "case 3: test zone resource management" +echo "(1) write in zones[0], zones[1], zones[2] and then close it" +$QEMU_IO $IMG << EOF +zap -p 0 0x1000 0x1000 +zap -p 0x4000000 0x1000 0x1000 +zap -p 0x8000000 0x1000 0x1000 +zrp 0 12 +zc 0 64M +zc 0x4000000 64M +zc 0x8000000 64M +zrp 0 12 +EOF + +echo "(2) reset all after 3(1)" +$QEMU_IO $IMG << EOF +zrs 0 768M +zrp 0 12 +EOF + +echo +echo "case 4: WP cache crash consistency under concurrent appends" +echo "(1) concurrent writes to the same sequential zone (zone 5 @ 320M)" +# Three concurrent aio_writes at WP, WP+4K, WP+8K. Data writes are +# permitted to complete out of order +$QEMU_IO $IMG <<EOF +aio_write -q -P 0xa1 0x14000000 4k +aio_write -q -P 0xa2 0x14001000 4k +aio_write -q -P 0xa3 0x14002000 4k +aio_flush +zrp 0x14000000 1 +read -q -P 0xa1 0x14000000 4k +read -q -P 0xa2 0x14001000 4k +read -q -P 0xa3 0x14002000 4k +EOF + +echo "(2) concurrent writes to different sequential zones (zones 6, 7, 9)" +# Spread across three zones. No per-zone interaction; each zone's WP +# must advance independently. +$QEMU_IO $IMG <<EOF +aio_write -q -P 0xb1 0x18000000 4k +aio_write -q -P 0xb2 0x1C000000 4k +aio_write -q -P 0xb3 0x24000000 4k +aio_flush +zrp 0x18000000 1 +zrp 0x1C000000 1 +zrp 0x24000000 1 +read -q -P 0xb1 0x18000000 4k +read -q -P 0xb2 0x1C000000 4k +read -q -P 0xb3 0x24000000 4k +EOF + +echo "(3) reset zones with no in-flight writes (zones 5, 6)" +# Reset zones 5 and 6 (which we wrote in (1) and (2)). WP should drop +# back to zone_start; data reads must return zero (unallocated +# post-reset). +$QEMU_IO $IMG -c "zrs 0x14000000 64M" -c "zrs 0x18000000 64M" \ + -c "zrp 0x14000000 1" -c "zrp 0x18000000 1" \ + -c "read -q -P 0 0x14000000 4k" \ + -c "read -q -P 0 0x18000000 4k" + +echo "(4) BLKRESETALL after concurrent writes" +# Reset everything first, refill three zones with concurrent writes, +# then RESETALL. Each zone's WP must return to its start. +$QEMU_IO $IMG <<EOF +zrs 0 768M +aio_write -q -P 0xc1 0x14000000 4k +aio_write -q -P 0xc2 0x18000000 4k +aio_write -q -P 0xc3 0x1C000000 4k +aio_flush +zrp 0x14000000 1 +zrp 0x18000000 1 +zrp 0x1C000000 1 +zrs 0 768M +zrp 0x14000000 1 +zrp 0x18000000 1 +zrp 0x1C000000 1 +EOF + +# success, all done +echo "*** done" +rm -f $seq.full +status=0
tests/qemu-iotests/tests/zoned-qcow2.out new file +191 -0
191 lines not on this page · raw message
Signature · 1 line
2.53.0
Quoted 17 lines · Sam Li
This patch series add a new extension - zoned format - to the qcow2 driver, allowing full zoned storage emulation on a qcow2 image file. A user can attach such an image to a guest and have it appear as a host-managed zoned block device.
The zoned format is opt-in through a new qcow2 header extension that pins the zone geometry. Behind the extension is a dedicated zoned metadata region that stores one 8-byte write pointer (WP) per zone. The extension is gated by an incompatible bit, so an older qcow2 implementation cannot accidentally open the image.
Each write pointer is routed through the write pointer cache, a Qcow2Cache object. The write pointer cache is written to disk after the qcow2 metadata is written, thus guaranteeing that the write pointer is updated after the corresponding data is written.
Quoted 7 lines
Zone states are in memory. Read-only and offline states are device-internal events, which are not modelled in qcow2 emulation for simplicity. The other zone states (closed, empty, full) can be inferred from write poiner values, presistent across QEMU reboots. The open states are kept in memory using open zone lists.
Hello Sam,
I ran: fio test suite, zonefs test suite, blktests, and zbd-state-test (WD internal test suite), on the following backends:
1) QEMU qcow2 (this series):
$ lsblk -z NAME ZONED ZONE-SZ ZONE-NR ZONE-AMAX ZONE-OMAX ZONE-APP ZONE-WGRAN vda host-managed 256M 140 0 16 4K 512B
2) zloop:
https://docs.kernel.org/admin-guide/blockdev/zoned_loop.html
$ lsblk -z NAME ZONED ZONE-SZ ZONE-NR ZONE-AMAX ZONE-OMAX ZONE-APP ZONE-WGRAN vda host-managed 256M 140 0 16 1M 4K
3) A real HM-SMR device:
$ lsblk -z NAME ZONED ZONE-SZ ZONE-NR ZONE-AMAX ZONE-OMAX ZONE-APP ZONE-WGRAN sda host-managed 256M 111760 0 128 32M 4K
fio test suite: looks good.
zonefs test suite: looks good.
blktests: same amount of test cases pass.
Only major difference I can see is for the following test cases:
QEMU qcow2:
zbd/004 => vda (write split across sequential zones) [passed]
runtime ... 148.769s
zloop:
zbd/004 => vda (write split across sequential zones) [passed]
runtime ... 47.446s
real dev:
zbd/004 => sda (write split across sequential zones) [passed]
runtime ... 27.616s
QEMU qcow2 seems significantly slower than zloop for this test case.
Perhaps not something to worry about, but the difference compared to zloop is big enough to raise my eyebrows :)
zbd-state-test:
Show a test case for QEMU qcow2 that fails, but passes on zloop and a real device.
You can use e.g. blkzone
or libzbd:
https://github.com/westerndigitalcorporation/libzbd
to perform zone operations.
I guess you could also use iotests for this test case.
021: OPEN_ZONE full to full... [Failed]
=> Expected zone condition fu
Got oe
Test case seem to do:
1) Finds a zone in zone state empty.
2) Fills the zone (using dd), zone is now in zone state full.
3) Tries to explicitly open the zone (this zone transition is not legal), and the open command should fail.
4) Checks the zone state of the zone, should still be zone state full.
I guess the problem with the QEMU qcow2 code is that it actually changes the zone state to explicit open in step 3), while it should fail the open command and keep the zone in zone state full.
If you look at e.g. null_blk: null_open_zone(), it also fails to open a full zone.
Kind regards,
Niklas
Quoted 71 lines · Niklas Cassel
Quoted 17 lines · Sam Li
This patch series add a new extension - zoned format - to the qcow2 driver, allowing full zoned storage emulation on a qcow2 image file. A user can attach such an image to a guest and have it appear as a host-managed zoned block device.
The zoned format is opt-in through a new qcow2 header extension that pins the zone geometry. Behind the extension is a dedicated zoned metadata region that stores one 8-byte write pointer (WP) per zone. The extension is gated by an incompatible bit, so an older qcow2 implementation cannot accidentally open the image.
Each write pointer is routed through the write pointer cache, a Qcow2Cache object. The write pointer cache is written to disk after the qcow2 metadata is written, thus guaranteeing that the write pointer is updated after the corresponding data is written.
Quoted 7 lines
Zone states are in memory. Read-only and offline states are device-internal events, which are not modelled in qcow2 emulation for simplicity. The other zone states (closed, empty, full) can be inferred from write poiner values, presistent across QEMU reboots. The open states are kept in memory using open zone lists.
Hello Sam,
I ran: fio test suite, zonefs test suite, blktests, and zbd-state-test (WD internal test suite), on the following backends:
1) QEMU qcow2 (this series):
$ lsblk -z NAME ZONED ZONE-SZ ZONE-NR ZONE-AMAX ZONE-OMAX ZONE-APP ZONE-WGRAN vda host-managed 256M 140 0 16 4K 512B2) zloop:
https://docs.kernel.org/admin-guide/blockdev/zoned_loop.html$ lsblk -z NAME ZONED ZONE-SZ ZONE-NR ZONE-AMAX ZONE-OMAX ZONE-APP ZONE-WGRAN vda host-managed 256M 140 0 16 1M 4K3) A real HM-SMR device:
$ lsblk -z NAME ZONED ZONE-SZ ZONE-NR ZONE-AMAX ZONE-OMAX ZONE-APP ZONE-WGRAN sda host-managed 256M 111760 0 128 32M 4Kfio test suite: looks good.
zonefs test suite: looks good.
blktests: same amount of test cases pass.
Only major difference I can see is for the following test cases:QEMU qcow2:
zbd/004 => vda (write split across sequential zones) [passed]runtime ... 148.769szloop:
zbd/004 => vda (write split across sequential zones) [passed]runtime ... 47.446sreal dev:
zbd/004 => sda (write split across sequential zones) [passed]
Quoted 7 lines
runtime ... 27.616sQEMU qcow2 seems significantly slower than zloop for this test case.
Perhaps not something to worry about, but the difference compared to zloop is big enough to raise my eyebrows :)
Hi Niklas,
Thanks for running the tests and sharing your thoughts. A possible reason for QEMU qcow being way slower than zloop might be the write dependency of writes (qcow2 metadata, zoned metadata, data) for crash consistency. I'll look into it to see the real problem behind this when I have some free time.
Quoted 24 lines
zbd-state-test:
Show a test case for QEMU qcow2 that fails, but passes on zloop and a real device.You can use e.g. blkzone
or libzbd:
https://github.com/westerndigitalcorporation/libzbd
to perform zone operations.I guess you could also use iotests for this test case.
021:
OPEN_ZONEfull to full... [Failed]
=> Expected zone condition fu
Got oeTest case seem to do:
1) Finds a zone in zone state empty.
2) Fills the zone (using dd), zone is now in zone state full.
3) Tries to explicitly open the zone (this zone transition is not legal), and the open command should fail.
4) Checks the zone state of the zone, should still be zone state full.
Quoted 6 lines
I guess the problem with the QEMU qcow2 code is that it actually changes the zone state to explicit open in step 3), while it should fail the open command and keep the zone in zone state full.
If you look at e.g.
null_blk:null_open_zone(), it also fails to open a full zone.
Ah, right. That’s a state transition I missed. Thanks for catching that.
Best regards,
Sam