Add full zoned storage emulation to the qcow2 driver
v11 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 \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 (5):
docs/qcow2: add the zoned format feature
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/file-posix.c | 2 +- block/qcow2-cache.c | 8 + block/qcow2-refcount.c | 7 + block/qcow2.c | 1464 +++++++++++++++++++++- block/qcow2.h | 83 +- block/trace-events | 2 + docs/interop/qcow2.rst | 117 +- docs/system/qemu-block-drivers.rst.inc | 47 + hw/block/virtio-blk.c | 22 +- include/block/block_int-common.h | 15 +- qapi/block-core.json | 91 +- tests/qemu-iotests/tests/zoned-qcow2 | 209 +++ tests/qemu-iotests/tests/zoned-qcow2.out | 191 +++ 13 files changed, 2244 insertions(+), 14 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 v11 1/5 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 | 47 ++++++++++++++++++++++++++ 1 file changed, 47 insertions(+)
docs/system/qemu-block-drivers.rst.inc +47 -0
@@ -172,6 +172,53 @@ 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 to 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 limit of the zones with 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 maximal allowed open zones. 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 number of bytes in a zone append request that can be issued to the + device. It must be 512-byte aligned and less than the zone capacity. + Defaults to ``64 KiB``. + .. program:: image-formats .. option:: qed
Signature · 1 line
2.53.0
Re: [PATCH v11 1/5] docs/qcow2: add the zoned format feature
Quoted 36 lines · Sam Li
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.
Signed-off-by Sam Li <[email protected]>Reviewed-by Stefan Hajnoczi <[email protected]>--- docs/system/qemu-block-drivers.rst.inc | 47 ++++++++++++++++++++++++++ 1 file changed, 47 insertions(+)docs/system/qemu-block-drivers.rst.inc +17
-0@@ -172,6 +172,53 @@ 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 to 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
docs/system/qemu-block-drivers.rst.inc This section describes each format and the options that are supported for it. · line 201
Quoted 10 lines
docs/system/qemu-block-drivers.rst.inc +10
-0+ 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 limit of the zones with implicit open, explicit open or closed state.
To make the text slightly less ambiguous, perhaps write this as:
The maximum allowed active zones (zones in the implicit open, explicit open, or closed state).
docs/system/qemu-block-drivers.rst.inc This section describes each format and the options that are supported for it. · line 209
Quoted 8 lines
docs/system/qemu-block-drivers.rst.inc +8
-0+ + 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 maximal allowed open zones. The max open zones must not be larger than
The specifications usually use the word maximum instead of maximal.
docs/system/qemu-block-drivers.rst.inc This section describes each format and the options that are supported for it. · line 220
Quoted 11 lines
docs/system/qemu-block-drivers.rst.inc +11
-0+ 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 number of bytes in a zone append request that can be issued to the + device. It must be 512-byte aligned and less than the zone capacity. + Defaults to ``64 KiB``.
Perhaps mention what 0 means here.
In the VIRTIO specification, 0 means that zone append requests are not supported, while in NVMe ZNS, a value of 0 means follow MDTS, and a MDTS value of 0 means no limit.
Kind regards,
Niklas
PATCH v11 2/5 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/file-posix.c | 2 +- block/qcow2.c | 329 ++++++++++++++++++++++++++++++- block/qcow2.h | 83 +++++++- docs/interop/qcow2.rst | 117 ++++++++++- include/block/block_int-common.h | 15 +- qapi/block-core.json | 91 ++++++++- 6 files changed, 628 insertions(+), 9 deletions(-)
block/file-posix.c +1 -1
@@ -3607,7 +3607,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 ; }
block/qcow2.c +326 -3
431 lines not on this page · raw message
block/qcow2.h +82 -1
117 lines not on this page · raw message
docs/interop/qcow2.rst +116 -1
135 lines not on this page · raw message
include/block/block_int-common.h +14 -1
@@ -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
@@ -901,7 +908,13 @@ typedef struct BlockLimits {BlockZoneModel zoned;/* zone size expressed in bytes */ -uint32_t zone_size; +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
Signature · 1 line
2.53.0
Re: [PATCH v11 2/5] qcow2: add configurations for zoned format extension
Quoted 48 lines · Sam Li
To 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 \(quoted diff) +0
-3-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-managedSigned-off-by Sam Li <[email protected]>--- block/file-posix.c | 2 +- block/qcow2.c | 329 ++++++++++++++++++++++++++++++- block/qcow2.h | 83 +++++++- docs/interop/qcow2.rst | 117 ++++++++++- include/block/block_int-common.h | 15 +- qapi/block-core.json | 91 ++++++++- 6 files changed, 628 insertions(+), 9 deletions(-)block/file-posix.c +1
-1@@ -3607,7 +3607,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 ; }block/qcow2.c +1
-0@@ -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,
block/qcow2.c qcow2_extract_crypto_opts(QemuOpts *opts, const char *fmt, Error **errp) · line 202
Quoted 8 lines
block/qcow2.c +5
-0@@ -194,6 +195,93 @@ qcow2_extract_crypto_opts(QemuOpts *opts, const char *fmt, Error **errp)return cryptoopts_qdict; } +/* +* Passing by the zoned device configurations by a zoned_header struct, check +* if the zone device options are under constraints. Return false when some +* option is invalid +*/
I have trouble parsing the first sentence. Maybe replace this doc comment with "Returns true if zone_opt is valid, false otherwise"?
Quoted 51 lines
block/qcow2.c +51
-0+static bool +qcow2_check_zone_options (Qcow2ZonedHeaderExtension *zone_opt,Error **errp) +{ +uint32_t sequential_zones; + +assert (zone_opt !=NULL ); + +if (zone_opt->zoned !=QCOW2_Z_NONE && zone_opt->zoned !=QCOW2_Z_HM ) { +error_setg (errp,"Zoned extension header zoned field has unknown " +"value %" PRIu8, zone_opt->zoned); +return false; + } + +if (!is_power_of_2 (zone_opt->zone_size)) { +error_setg (errp,"Zoned extension header zone_size %" PRIu64 +"B is not a power of 2" , zone_opt->zone_size); +return false; + } + +if (zone_opt->nr_zones >UINT32_MAX /8 ) { +error_setg (errp,"Zoned extension header nr_zones %" PRIu32 +" exceeds maximum %u" , + zone_opt->nr_zones,UINT32_MAX /8 ); +return false; + } + +if (zone_opt->zone_capacity > zone_opt->zone_size) { +error_setg (errp,"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 )) { +error_setg (errp,"max append bytes %" PRIu32"B is not aligned " +"to %" PRIu64, zone_opt->max_append_bytes, + (uint64_t )BDRV_SECTOR_SIZE ); +return false; + } + +if (zone_opt->max_append_bytes +BDRV_SECTOR_SIZE >= + zone_opt->zone_capacity) { +error_setg (errp,"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) { + error_setg(errp, "Conventional_zones %" PRIu32 " exceeds " + "nr_zones %" PRIu32 ".",
block/qcow2.c qcow2_extract_crypto_opts(QemuOpts *opts, const char *fmt, Error **errp) · line 263
Quoted 10 lines
block/qcow2.c +10
-0+ zone_opt->conventional_zones, zone_opt->nr_zones); +return false; + } + +if (zone_opt->max_active_zones > zone_opt->nr_zones) { +error_setg (errp,"Max_active_zones %" PRIu32" exceeds " +"nr_zones %" PRIu32". Set it to nr_zones." , + zone_opt->max_active_zones, zone_opt->nr_zones); + zone_opt->max_active_zones = zone_opt->nr_zones; + }
errp is an error that must be handled by the caller, but the function will return true. The caller may not notice the Error object and it may be leaked.
Another issue with this approach is that error_setg(errp, "a"); error_setg(errp, "b"); is not allowed (see assert(*errp == NULL) in error_setv()). So if two of these conditionals are taken then QEMU will abort.
I think the intention is to print a warning rather than to set an error here. Use warn_report() without touching errp to avoid these issues.
Quoted 55 lines
block/qcow2.c +41
-0+ + sequential_zones = zone_opt->nr_zones - zone_opt->conventional_zones; +if (zone_opt->max_open_zones > sequential_zones) { +error_setg (errp,"Max_open_zones field can not be larger than" +"the number of SWR zones. Set it to number of SWR" +"zones %" PRIu32"." , 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) { +error_setg (errp,"Max_open_zones %" PRIu32" exceeds " +"max_active_zones %" PRIu32". Set it 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 +299,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 +521,50 @@ 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);
block/qcow2.c qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset, · line 552
Quoted 10 lines
block/qcow2.c +10
-0+ 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; + +/* refuse to open broken images */ +if (zoned_ext.nr_zones !=DIV_ROUND_UP (bs->total_sectors * +BDRV_SECTOR_SIZE , zoned_ext.zone_size)) {
This is not safe when validating untrusted qcow2 files because zoned_ext.zone_size could be 0 (division-by-zero kills the process) and bs->total_sectors * BDRV_SECTOR_SIZE can integer overflow (unlikely because the file would have to be extremely large).
Can you express this in a way that is safe? If necessary, split it into multiple checks.
Quoted 98 lines
block/qcow2.c +66
-0+error_setg (errp,"Zoned extension header nr_zones field " +"is wrong" ); +return -EINVAL ; + } +if (!qcow2_check_zone_options (&zoned_ext, errp)) { +return -EINVAL ; + } + +#ifdef DEBUG_EXT +printf ("Qcow2: Got zoned format extension: " +"offset=%" PRIu32"\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 +2201,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 +3322,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 +3372,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 +3771,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.
block/qcow2.c qcow2_co_create(BlockdevCreateOptions *create_options, Error **errp) · line 3802
Quoted 4 lines
block/qcow2.c +1
-0@@ -3615,6 +3799,8 @@ qcow2_co_create(BlockdevCreateOptions *create_options, Error **errp)assert (create_options->driver ==BLOCKDEV_DRIVER_QCOW2 ); qcow2_opts = &create_options->u.qcow2; + zone_struct = create_options->u.qcow2.zone;
A slightly more concise way of writing this:
zone_struct = qcow2_opts->zone;
block/qcow2.c qcow2_co_create(BlockdevCreateOptions *create_options, Error **errp) · line 3803
Quoted 1 line
block/qcow2.c +1
-0+ zone_host_managed = &create_options->u.qcow2.zone->u.host_managed;
This must be moved down to avoid deferencing a NULL create_options->u.qcow2.zone pointer:
if (zone_struct && zone_struct->mode == QCOW2_ZONE_MODEL_HOST_MANAGED) {
Qcow2ZoneHostManaged *zone_host_managed = &zone_struct->u.host_managed;Re: [PATCH v11 2/5] qcow2: add configurations for zoned format extension
In reply to Sam LiQuoted 12 lines · Sam Li
To 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 \(quoted diff) +0
-3-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
Quoted 1 line
Signed-off-by Sam Li <[email protected]>
QAPI schema
PATCH v11 3/5 virtio-blk: do not merge writes across a zone boundary
In reply to Sam Livirtio_blk_submit_multireq() fuses adjacent in-zone writes 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.
--- hw/block/virtio-blk.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-)
hw/block/virtio-blk.c +21 -1
@@ -288,6 +288,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 ; +BlockDriverState *bs =blk_bs (s->blk); +bool zone_cross; +int64_t zone_sector, end_sector;if (mrb->num_reqs ==1 ) {submit_requests (s, mrb,0 ,1 ,-1 );
@@ -303,17 +306,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 must not span a zone +* boundary. Bail out of merging if combining req into the current +* batch would straddle a zone. +*/ +if (bs && bs->bl.zone_size >0 ) { + zone_sector = bs->bl.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 ; }
Signature · 1 line
2.53.0
Re: [PATCH v11 3/5] virtio-blk: do not merge writes across a zone boundary
hw/block/virtio-blk.c static void virtio_blk_submit_multireq(VirtIOBlock *s, MultiReqBuffer *mrb) · line 291
Quoted 9 lines · Sam Li
hw/block/virtio-blk.c +1
-0@@ -288,6 +288,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 ; +BlockDriverState *bs =blk_bs (s->blk);
Please add a blk_get_zone_size() API in block-backend.c along the lines of the other accessor APIs (blk_get_request_alignment(), etc) instead of using bs directly.
hw/block/virtio-blk.c static void virtio_blk_submit_multireq(VirtIOBlock *s, MultiReqBuffer *mrb) · line 312
Quoted 13 lines
hw/block/virtio-blk.c +6
-0+bool zone_cross; +int64_t zone_sector, end_sector;if (mrb->num_reqs ==1 ) {submit_requests (s, mrb,0 ,1 ,-1 );@@ -303,17 +306,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 must not span a zone
This code handles both reads and writes but the comment only mentions writes. I think reads are allowed to span zones, so should there be a check that mrb->is_write?
Re: [PATCH v11 3/5] virtio-blk: do not merge writes across a zone boundary
Hello Stefan,
Quoted 17 lines · Stefan Hajnoczi
hw/block/virtio-blk.c static void virtio_blk_submit_multireq(VirtIOBlock *s, MultiReqBuffer *mrb) · line 312
Quoted 12 lines · Sam Li
hw/block/virtio-blk.c +4
-0@@ -303,17 +306,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 must not span a zone This code handles both reads and writes but the comment only mentions writes. I think reads are allowed to span zones, so should there be a check that
mrb->is_write?
From the Virtual I/O Device (VIRTIO) Version 1.4 specification:
"If a VIRTIO_BLK_T_ZONE_APPEND request, a VIRTIO_BLK_T_IN request or a VIRTIO_BLK_T_OUT request issued to a SWR zone has the range that has sectors in more than one zone, then the request SHALL be completed with VIRTIO_BLK_S_ZONE_INVALID_CMD value in the field status."
Thus, while this is allowed to read past the zone boundary in many zoned specifications, e.g. in both ZAC and ZBC, when the "Read Across Zone Boundaries bit" is set, is seems like it is never allowed to do so in VIRTIO.
Kind regards,
Niklas
Re: [PATCH v11 3/5] virtio-blk: do not merge writes across a zone boundary
Quoted 29 lines · Niklas Cassel
Hello Stefan,
Quoted 17 lines · Stefan Hajnoczi
hw/block/virtio-blk.c static void virtio_blk_submit_multireq(VirtIOBlock *s, MultiReqBuffer *mrb) · line 312
Quoted 12 lines · Sam Li
hw/block/virtio-blk.c +4
-0@@ -303,17 +306,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 must not span a zone This code handles both reads and writes but the comment only mentions writes. I think reads are allowed to span zones, so should there be a check that
mrb->is_write?From the Virtual I/O Device (VIRTIO) Version 1.4 specification:
"If a
VIRTIO_BLK_T_ZONE_APPENDrequest, aVIRTIO_BLK_T_INrequest or aVIRTIO_BLK_T_OUTrequest issued to a SWR zone has the range that has sectors in more than one zone, then the request SHALL be completed withVIRTIO_BLK_S_ZONE_INVALID_CMDvalue in the field status."
Quoted 4 lines
Thus, while this is allowed to read past the zone boundary in many zoned specifications, e.g. in both ZAC and ZBC, when the "Read Across Zone Boundaries bit" is set, is seems like it is never allowed to do so in VIRTIO.
Hi Niklas,
Thanks for pointing this out. So no change is needed here.
I noticed that NVMe has a Read Across Zone Boundaries bit to indicate whether or not this is allowed. virtio-blk could add a feature bit to offer the same functionality in the future. This would come in handy if existing applications depend on reading across zones.
Stefan
Re: [PATCH v11 3/5] virtio-blk: do not merge writes across a zone boundary
Quoted 36 lines · Stefan Hajnoczi
Quoted 29 lines · Niklas Cassel
Hello Stefan,
Quoted 17 lines · Stefan Hajnoczi
hw/block/virtio-blk.c static void virtio_blk_submit_multireq(VirtIOBlock *s, MultiReqBuffer *mrb) · line 312
Quoted 12 lines · Sam Li
hw/block/virtio-blk.c +4
-0@@ -303,17 +306,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 must not span a zone This code handles both reads and writes but the comment only mentions writes. I think reads are allowed to span zones, so should there be a check that
mrb->is_write?From the Virtual I/O Device (VIRTIO) Version 1.4 specification:
"If a
VIRTIO_BLK_T_ZONE_APPENDrequest, aVIRTIO_BLK_T_INrequest or aVIRTIO_BLK_T_OUTrequest issued to a SWR zone has the range that has sectors in more than one zone, then the request SHALL be completed withVIRTIO_BLK_S_ZONE_INVALID_CMDvalue in the field status."Quoted 4 lines
Thus, while this is allowed to read past the zone boundary in many zoned specifications, e.g. in both ZAC and ZBC, when the "Read Across Zone Boundaries bit" is set, is seems like it is never allowed to do so in VIRTIO.
Quoted 7 lines
Hi Niklas,
Thanks for pointing this out. So no change is needed here.I noticed that NVMe has a Read Across Zone Boundaries bit to indicate whether or not this is allowed. virtio-blk could add a feature bit to offer the same functionality in the future. This would come in handy if existing applications depend on reading across zones.
Sounds like a good addition to the persistent metadata for ZNS emulation series?
Sam
PATCH v11 4/5 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 | 8 + block/qcow2-refcount.c | 7 + block/qcow2.c | 1137 +++++++++++++++++++++++++++++++++++++++- block/trace-events | 2 + 4 files changed, 1149 insertions(+), 5 deletions(-)
block/qcow2-cache.c +8 -0
@@ -275,6 +275,14 @@ 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) { ret =qcow2_cache_flush_dependency (bs, dependency);if (ret <0 ) {
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 +1132 -5
1,233 lines not on this page · raw message
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
Re: [PATCH v11 4/5] qcow2: add zoned emulation capability
Quoted 37 lines · Sam Li
By 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.
Signed-off-by Sam Li <[email protected]>--- block/qcow2-cache.c | 8 + block/qcow2-refcount.c | 7 + block/qcow2.c | 1137 +++++++++++++++++++++++++++++++++++++++- block/trace-events | 2 + 4 files changed, 1149 insertions(+), 5 deletions(-)block/qcow2-cache.c +0
-0
block/qcow2-cache.c int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c, · line 284
Quoted 10 lines
block/qcow2-cache.c +7
-0@@ -275,6 +275,14 @@ 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 ; + }
This makes part of the expression below tautologous:
if (c->depends && (c->depends != dependency)) {
^^^^^^^^^^^^^^^^^^^^^^^^^^
ret = qcow2_cache_flush_dependency(bs, c);
if (ret < 0) {
return ret;
}
}That sub-expression could be dropped, but it makes me worry that the earlier if (dependency->depends) statement is needed even when c->depends == dependency.
Kevin: Any thoughts on this?
Quoted 31 lines
block/qcow2-cache.c +1
-0+if (dependency->depends) { ret =qcow2_cache_flush_dependency (bs, dependency);if (ret <0 ) {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 +1
-0@@ -195,6 +195,300 @@ qcow2_extract_crypto_opts(QemuOpts *opts, const char *fmt, Error **errp)return cryptoopts_qdict; } +#define QCOW2_ZT_IS_CONV (wp) (wp & 1ULL << 59)
block/qcow2.c qcow2_extract_crypto_opts(QemuOpts *opts, const char *fmt, Error **errp) · line 208
Quoted 10 lines
block/qcow2.c +10
-0+#define QCOW2_GET_WP (wp) ((wp << 5) >> 5) + +/* +* To emulate a real zoned device, closed, empty and full states are +* preserved after a power cycle. The open states are in-memory and will +* be lost after closing the device. Read-only and offline states are +* device-internal events, which are not considered for simplicity. +*/ +static inline BlockZoneState qcow2_get_zone_state (BlockDriverState *bs, +uint32_t index)
I guess this function requires bs->wps->colock or s->lock, otherwise the TAILQ accesses could race? Please check and document the locking requirements.
I/O requests may be processed in multiple threads simultaneously. s->lock protects qcow2 state.
Quoted 8 lines
block/qcow2.c +8
-0+{ +BDRVQcow2State *s = bs->opaque; +Qcow2ZoneListEntry *zone_entry = &s->zone_list_entries[index]; +uint64_t zone_wp = bs->wps->wp[index]; +uint64_t zone_start; + +if (QCOW2_ZT_IS_CONV (zone_wp)) { +return BLK_ZS_NOT_WP ;
block/qcow2.c qcow2_extract_crypto_opts(QemuOpts *opts, const char *fmt, Error **errp) · line 226
Quoted 10 lines
block/qcow2.c +10
-0+ } + +if (QTAILQ_IN_USE (zone_entry, exp_open_zone_entry)) { +return BLK_ZS_EOPEN ; + } +if (QTAILQ_IN_USE (zone_entry, imp_open_zone_entry)) { +return BLK_ZS_IOPEN ; + } + + zone_start = index * bs->bl.zone_size;
This is a uint32_t * uint32_t multiplication that can overflow. Avoid that with:
zone_start = (uint64_t)index * bs->bl.zone_size;
Quoted 14 lines
block/qcow2.c +14
-0+if (zone_wp == zone_start) { +return BLK_ZS_EMPTY ; + } +if (zone_wp >= zone_start + bs->bl.zone_capacity) { +return BLK_ZS_FULL ; + } +if (zone_wp > zone_start) { +if (!QTAILQ_IN_USE (zone_entry, closed_zone_entry)) { +/* +* The number of closed zones is not always updated in time when +* the device is closed. However, it only matters when doing +* zone report. Refresh the count and list of closed zones to +* provide correct zone states for zone report. +*/
block/qcow2.c qcow2_extract_crypto_opts(QemuOpts *opts, const char *fmt, Error **errp) · line 250
Quoted 10 lines
block/qcow2.c +10
-0+QTAILQ_INSERT_HEAD (&s->closed_zones, zone_entry, closed_zone_entry); + s->nr_zones_closed++; + } +return BLK_ZS_CLOSED ; + } +return BLK_ZS_NOT_WP ; +} + +static void qcow2_rm_exp_open_zone (BDRVQcow2State *s, +uint32_t index)
Locking requirements here and in the functions that follow?
Quoted 94 lines
block/qcow2.c +94
-0+{ +Qcow2ZoneListEntry *zone_entry = &s->zone_list_entries[index]; + +QTAILQ_REMOVE (&s->exp_open_zones, zone_entry, exp_open_zone_entry); + s->nr_zones_exp_open--; +} + +static void qcow2_rm_imp_open_zone (BDRVQcow2State *s, +int32_t index) +{ +Qcow2ZoneListEntry *zone_entry; +if (index <0 ) { +/* Apply LRU when the index is not specified. */ + zone_entry =QTAILQ_LAST (&s->imp_open_zones); + }else { + zone_entry = &s->zone_list_entries[index]; + } + +QTAILQ_REMOVE (&s->imp_open_zones, zone_entry, imp_open_zone_entry); + s->nr_zones_imp_open--; +} + +static void qcow2_rm_open_zone (BDRVQcow2State *s, +uint32_t index) +{ +Qcow2ZoneListEntry *zone_entry = &s->zone_list_entries[index]; + +if (QTAILQ_IN_USE (zone_entry, exp_open_zone_entry)) { +qcow2_rm_exp_open_zone (s, index); + }else if (QTAILQ_IN_USE (zone_entry, imp_open_zone_entry)) { +qcow2_rm_imp_open_zone (s, index); + } +} + +static void qcow2_rm_closed_zone (BDRVQcow2State *s, +uint32_t index) +{ +Qcow2ZoneListEntry *zone_entry = &s->zone_list_entries[index]; + +QTAILQ_REMOVE (&s->closed_zones, zone_entry, closed_zone_entry); + s->nr_zones_closed--; +} + +static void qcow2_do_imp_open_zone (BDRVQcow2State *s, +uint32_t index, +BlockZoneState zs) +{ +Qcow2ZoneListEntry *zone_entry = &s->zone_list_entries[index]; + +switch (zs) { +case BLK_ZS_EMPTY : +break ; +case BLK_ZS_CLOSED : +qcow2_rm_closed_zone (s, index); +break ; +case BLK_ZS_IOPEN : +/* +* The LRU policy: update the zone that is most recently +* used to the head of the zone list +*/ +if (zone_entry ==QTAILQ_FIRST (&s->imp_open_zones)) { +return ; + } +QTAILQ_REMOVE (&s->imp_open_zones, zone_entry, imp_open_zone_entry); + s->nr_zones_imp_open--; +break ; +default : +return ; + } + +QTAILQ_INSERT_HEAD (&s->imp_open_zones, zone_entry, imp_open_zone_entry); + s->nr_zones_imp_open++; +} + +static void qcow2_do_exp_open_zone (BDRVQcow2State *s, +uint32_t index) +{ +Qcow2ZoneListEntry *zone_entry = &s->zone_list_entries[index]; + +QTAILQ_INSERT_HEAD (&s->exp_open_zones, zone_entry, exp_open_zone_entry); + s->nr_zones_exp_open++; +} + +/* +* The list of zones is managed using an LRU policy: the last +* zone of the list is always the one that was least recently used +* for writing and is chosen as the zone to close to be able to +* implicitly open another zone. +* +* We can only close the open zones. The index is not specified +* when it is less than 0. +*/ +static void qcow2_do_close_zone(BlockDriverState *bs, + int32_t index,
block/qcow2.c qcow2_extract_crypto_opts(QemuOpts *opts, const char *fmt, Error **errp) · line 354
Quoted 10 lines
block/qcow2.c +10
-0+ BlockZoneState zs) +{ +BDRVQcow2State *s = bs->opaque; +Qcow2ZoneListEntry *zone_entry; + +if (index >=0 ) { + zone_entry = &s->zone_list_entries[index]; + }else { +/* before removal of the last implicitly open zone */ + zone_entry =QTAILQ_LAST (&s->imp_open_zones);
There is an assumption that zone_entry is no NULL when zs == BLK_ZS_IOPEN? I think that make sense and it means NULL dereferences cannot happen, but I wanted to check. You could add an assert(zone_entry != NULL) here to make that explicit.
Quoted 11 lines
block/qcow2.c +11
-0+ } + +if (zs ==BLK_ZS_IOPEN ) { +qcow2_rm_imp_open_zone (s, index); + goto close_zone; + } + +if (index >=0 && zs ==BLK_ZS_EOPEN ) { +qcow2_rm_exp_open_zone (s, index); +/* +* The zone state changes when the zone is removed from the list of
block/qcow2.c qcow2_extract_crypto_opts(QemuOpts *opts, const char *fmt, Error **errp) · line 375
Quoted 10 lines
block/qcow2.c +10
-0+* open zones (explicitly open -> empty). The closed zone list is +* refreshed during get_zone_state(). +*/ +qcow2_get_zone_state (bs, index); + } +return ; + +close_zone: +QTAILQ_INSERT_HEAD (&s->closed_zones, zone_entry, closed_zone_entry); + s->nr_zones_closed++;
Is the goto and label necessary? Maybe move this inside the if statement instead to simplify the function.
block/qcow2.c qcow2_extract_crypto_opts(QemuOpts *opts, const char *fmt, Error **errp) · line 376
Quoted 1 line
block/qcow2.c +1
-0+}
I've reviewed up to here for now.
PATCH v11 5/5 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 36 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.
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-managed
Quoted 4 lines
Then 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 \
Please run make check-block-qcow and ensure the tests pass. In some cases the test reference output may be outdated and you can simply copy the <test>.out.bad file over the <test>.out reference file.
Thanks,
Stefan
Quoted 37 lines · Stefan Hajnoczi
Quoted 36 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.
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-managed
Quoted 6 lines
Quoted 4 lines
Then 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 \Please run
make check-block-qcowand ensure the tests pass. In some
That should be make check-block-qcow2.
Quoted 38 lines · Stefan Hajnoczi
Quoted 36 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.
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-managed
Quoted 8 lines
Quoted 4 lines
Then 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 \Please run
make check-block-qcowand ensure the tests pass. In some cases the test reference output may be outdated and you can simply copy the <test>.out.bad file over the <test>.out reference file.
Thanks, I will check this.
Sam