Skip to contentSkip to conversation

Add persistence to NVMe ZNS emulation

RFC Superseded Sam Li 2 participants 5 messages Aug 16, 2023 – Aug 21, 2023 Block layer

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Sam Li

author
#0 lore raw

cover Add persistence to NVMe ZNS emulation

ZNS emulation follows NVMe ZNS spec but the state of namespace zones does not persist accross restarts of QEMU. This patch makes the metadata of ZNS emulation persistent by using new block layer APIs and the qcow2 img as backing file. It is the second part after the patches
- adding full zoned storage emulation to qcow2 driver.

The metadata of ZNS emulation divides into two parts, zone metadata and zone descriptor extension data. The zone metadata is composed of zone states, zone type, wp and zone attributes. The zone information can be stored at an uint64_t wp to save space and easy access. The structure of wp of each zone is as follows:
|| zone state (4)| zone type (1)| zone attr (8)| wp (51) ||

The zone descriptor extension data is relatively small comparing to the overall size therefore we adopt the option that store zded of all zones in an array regardless of the valid bit set.

To create a zns format qcow2 image file, use:

$ ./build/qemu-img create -f qcow2 zns.qcow2 -o size=768M
-o zone_size=64M -o zone_capacity=64M -o zone_nr_conv=0
-o max_append_sectors=512 -o max_open_zones=0 -o
max_active_zones=0 -o zoned_profile=zns

To attach this file as emulated zns drive in the command line of QEMU, use:
-drive file=${znsimg},id=nvmezns0,format=qcow2,if=none \
-device nvme-ns,drive=nvmezns0,bus=nvme0,nsid=1,uuid=xxx \

Sam Li (5):
hw/nvme: use blk_get_*() to access zone info in the block layer
qcow2: add zone device metadata with zd_extension hw/nvme: make the metadata of ZNS emulation persistent hw/nvme: refactor zone append writes using block layer APIs hw/nvme: make ZDED persistent

 block/block-backend.c             |   94 +++
 block/qcow2.c                     |  160 +++-
 block/qcow2.h                     |    3 +
 docs/interop/qcow2.txt            |    2 +
 hw/nvme/ctrl.c                    | 1256 ++++++++---------------------
 hw/nvme/ns.c                      |  163 +---
 hw/nvme/nvme.h                    |   95 +--
 include/block/block-common.h      |    9 +
 include/block/block_int-common.h  |    8 +
 include/sysemu/block-backend-io.h |   12 +
 include/sysemu/dma.h              |    3 +
 qapi/block-core.json              |    3 +
 softmmu/dma-helpers.c             |   17 +
 13 files changed, 686 insertions(+), 1139 deletions(-)
Signature · 1 line
2.40.1
author
#1 lore raw

RFC 1/5 hw/nvme: use blk_get_*() to access zone info in the block layer

The zone information is contained in the BlockLimits fileds. Add blk_get_*() functions to access the block layer and update zone info accessing in the NVMe device emulation.

---
 block/block-backend.c             | 56 ++++++++++++++++++++++++++++
 block/qcow2.c                     | 20 +++++++++-
 hw/nvme/ctrl.c                    | 34 ++++++-----------
 hw/nvme/ns.c                      | 62 ++++++++++---------------------
 hw/nvme/nvme.h                    |  3 --
 include/sysemu/block-backend-io.h |  7 ++++
 6 files changed, 112 insertions(+), 70 deletions(-)
block/block-backend.c +56 -0
index 4009ed5fed..ad410286a0 100644
@@ -2362,6 +2362,62 @@ int blk_get_max_iov(BlockBackend *blk)
     return blk->root->bs->bl.max_iov;
 }
 
+uint8_t blk_get_zone_model(BlockBackend *blk)
+{
+    BlockDriverState *bs = blk_bs(blk);
+    IO_CODE();
+    return bs ? bs->bl.zoned: 0;
+
+}
+
+uint8_t blk_get_zone_profile(BlockBackend *blk)
+{
+    BlockDriverState *bs = blk_bs(blk);
+    IO_CODE();
+    return bs ? bs->bl.zoned_profile: 0;
+
+}
+
+uint32_t blk_get_zone_size(BlockBackend *blk)
+{
+    BlockDriverState *bs = blk_bs(blk);
+    IO_CODE();
+
+    return bs ? bs->bl.zone_size : 0;
+}
+
+uint32_t blk_get_zone_capacity(BlockBackend *blk)
+{
+    BlockDriverState *bs = blk_bs(blk);
+    IO_CODE();
+
+    return bs ? bs->bl.zone_capacity : 0;
+}
+
+uint32_t blk_get_max_open_zones(BlockBackend *blk)
+{
+    BlockDriverState *bs = blk_bs(blk);
+    IO_CODE();
+
+    return bs ? bs->bl.max_open_zones : 0;
+}
+
+uint32_t blk_get_max_active_zones(BlockBackend *blk)
+{
+    BlockDriverState *bs = blk_bs(blk);
+    IO_CODE();
+
+    return bs ? bs->bl.max_active_zones : 0;
+}
+
+uint32_t blk_get_max_append_sectors(BlockBackend *blk)
+{
+    BlockDriverState *bs = blk_bs(blk);
+    IO_CODE();
+
+    return bs ? bs->bl.max_append_sectors : 0;
+}
+
 void *blk_try_blockalign(BlockBackend *blk, size_t size)
 {
     IO_CODE();
block/qcow2.c +18 -2
index 5ccf79cbe7..9de90ccc9f 100644
@@ -2172,6 +2172,8 @@ static void qcow2_refresh_limits(BlockDriverState *bs, Error **errp)
     bs->bl.pwrite_zeroes_alignment = s->subcluster_size;
     bs->bl.pdiscard_alignment = s->cluster_size;
     bs->bl.zoned = s->zoned_header.zoned;
+    bs->bl.zoned_profile = s->zoned_header.zoned_profile;
+    bs->bl.zone_capacity = s->zoned_header.zone_capacity;
     bs->bl.nr_zones = s->zoned_header.nr_zones;
     bs->wps = s->wps;
     bs->bl.max_append_sectors = s->zoned_header.max_append_sectors;
@@ -4083,8 +4085,22 @@ qcow2_co_create(BlockdevCreateOptions *create_options, Error **errp)
         s->zoned_header.zoned = BLK_Z_HM;
         s->zoned_header.zone_size = qcow2_opts->zone_size;
         s->zoned_header.zone_nr_conv = qcow2_opts->zone_nr_conv;
-        s->zoned_header.max_open_zones = qcow2_opts->max_open_zones;
-        s->zoned_header.max_active_zones = qcow2_opts->max_active_zones;
+
+        if (!qcow2_opts->max_active_zones) {
+            if (qcow2_opts->max_open_zones > qcow2_opts->max_active_zones) {
+                error_setg(errp, "max_open_zones (%u) exceeds "
+                           "max_active_zones (%u)", qcow2_opts->max_open_zones,
+                           qcow2_opts->max_active_zones);
+                return -1;
+            }
+
+            if (!qcow2_opts->max_open_zones) {
+                s->zoned_header.max_open_zones = qcow2_opts->max_active_zones;
+            }
+            s->zoned_header.max_open_zones = qcow2_opts->max_open_zones;
+            s->zoned_header.max_active_zones = qcow2_opts->max_active_zones;
+        }
+
         s->zoned_header.max_append_sectors = qcow2_opts->max_append_sectors;
         s->zoned_header.nr_zones = qcow2_opts->size / qcow2_opts->zone_size;
 
hw/nvme/ctrl.c +11 -23
index 539d273553..4e1608f0c1 100644
@@ -417,18 +417,6 @@ static void nvme_assign_zone_state(NvmeNamespace *ns, NvmeZone *zone,
 static uint16_t nvme_zns_check_resources(NvmeNamespace *ns, uint32_t act,
                                          uint32_t opn, uint32_t zrwa)
 {
-    if (ns->params.max_active_zones != 0 &&
-        ns->nr_active_zones + act > ns->params.max_active_zones) {
-        trace_pci_nvme_err_insuff_active_res(ns->params.max_active_zones);
-        return NVME_ZONE_TOO_MANY_ACTIVE | NVME_DNR;
-    }
-
-    if (ns->params.max_open_zones != 0 &&
-        ns->nr_open_zones + opn > ns->params.max_open_zones) {
-        trace_pci_nvme_err_insuff_open_res(ns->params.max_open_zones);
-        return NVME_ZONE_TOO_MANY_OPEN | NVME_DNR;
-    }
-
     if (zrwa > ns->zns.numzrwa) {
         return NVME_NOZRWA | NVME_DNR;
     }
@@ -1988,9 +1976,9 @@ static uint16_t nvme_zrm_reset(NvmeNamespace *ns, NvmeZone *zone)
 static void nvme_zrm_auto_transition_zone(NvmeNamespace *ns)
 {
     NvmeZone *zone;
+    int moz = blk_get_max_open_zones(ns->blkconf.blk);
 
-    if (ns->params.max_open_zones &&
-        ns->nr_open_zones == ns->params.max_open_zones) {
+    if (moz && ns->nr_open_zones == moz) {
         zone = QTAILQ_FIRST(&ns->imp_open_zones);
         if (zone) {
             /*
@@ -2165,7 +2153,7 @@ void nvme_rw_complete_cb(void *opaque, int ret)
         block_acct_done(stats, acct);
     }
 
-    if (ns->params.zoned && nvme_is_write(req)) {
+    if (blk_get_zone_model(blk) && nvme_is_write(req)) {
         nvme_finalize_zoned_write(ns, req);
     }
 
@@ -2887,7 +2875,7 @@ static void nvme_copy_out_completed_cb(void *opaque, int ret)
         goto out;
     }
 
-    if (ns->params.zoned) {
+    if (blk_get_zone_model(ns->blkconf.blk)) {
         nvme_advance_zone_wp(ns, iocb->zone, nlb);
     }
 
@@ -2999,7 +2987,7 @@ static void nvme_copy_in_completed_cb(void *opaque, int ret)
         goto invalid;
     }
 
-    if (ns->params.zoned) {
+    if (blk_get_zone_model(ns->blkconf.blk)) {
         status = nvme_check_zone_write(ns, iocb->zone, iocb->slba, nlb);
         if (status) {
             goto invalid;
@@ -3093,7 +3081,7 @@ static void nvme_do_copy(NvmeCopyAIOCB *iocb)
         }
     }
 
-    if (ns->params.zoned) {
+    if (blk_get_zone_model(ns->blkconf.blk)) {
         status = nvme_check_zone_read(ns, slba, nlb);
         if (status) {
             goto invalid;
@@ -3169,7 +3157,7 @@ static uint16_t nvme_copy(NvmeCtrl *n, NvmeRequest *req)
 
     iocb->slba = le64_to_cpu(copy->sdlba);
 
-    if (ns->params.zoned) {
+    if (blk_get_zone_model(ns->blkconf.blk)) {
         iocb->zone = nvme_get_zone_by_slba(ns, iocb->slba);
         if (!iocb->zone) {
             status = NVME_LBA_RANGE | NVME_DNR;
@@ -3440,7 +3428,7 @@ static uint16_t nvme_read(NvmeCtrl *n, NvmeRequest *req)
         goto invalid;
     }
 
-    if (ns->params.zoned) {
+    if (blk_get_zone_model(blk)) {
         status = nvme_check_zone_read(ns, slba, nlb);
         if (status) {
             trace_pci_nvme_err_zone_read_not_ok(slba, nlb, status);
@@ -3555,7 +3543,7 @@ static uint16_t nvme_do_write(NvmeCtrl *n, NvmeRequest *req, bool append,
         goto invalid;
     }
 
-    if (ns->params.zoned) {
+    if (blk_get_zone_model(blk)) {
         zone = nvme_get_zone_by_slba(ns, slba);
         assert(zone);
 
@@ -3673,7 +3661,7 @@ static uint16_t nvme_get_mgmt_zone_slba_idx(NvmeNamespace *ns, NvmeCmd *c,
     uint32_t dw10 = le32_to_cpu(c->cdw10);
     uint32_t dw11 = le32_to_cpu(c->cdw11);
 
-    if (!ns->params.zoned) {
+    if (blk_get_zone_model(ns->blkconf.blk)) {
         trace_pci_nvme_err_invalid_opc(c->opcode);
         return NVME_INVALID_OPCODE | NVME_DNR;
     }
@@ -6534,7 +6522,7 @@ done:
 
 static uint16_t nvme_format_check(NvmeNamespace *ns, uint8_t lbaf, uint8_t pi)
 {
-    if (ns->params.zoned) {
+    if (blk_get_zone_model(ns->blkconf.blk)) {
         return NVME_INVALID_FORMAT | NVME_DNR;
     }
 
hw/nvme/ns.c +20 -42

138 lines not on this page · raw message

hw/nvme/nvme.h +0 -3
index 5f2ae7b28b..76677a86e9 100644
@@ -189,10 +189,7 @@ typedef struct NvmeNamespaceParams {
     uint32_t mcl;
     uint8_t  msrc;
 
-    bool     zoned;
     bool     cross_zone_read;
-    uint64_t zone_size_bs;
-    uint64_t zone_cap_bs;
     uint32_t max_active_zones;
     uint32_t max_open_zones;
     uint32_t zd_extension_size;
include/sysemu/block-backend-io.h +7 -0
index be4dcef59d..3be221e752 100644
@@ -99,6 +99,13 @@ void blk_error_action(BlockBackend *blk, BlockErrorAction action,
 void blk_iostatus_set_err(BlockBackend *blk, int error);
 int blk_get_max_iov(BlockBackend *blk);
 int blk_get_max_hw_iov(BlockBackend *blk);
+uint8_t blk_get_zone_model(BlockBackend *blk);
+uint8_t blk_get_zone_profile(BlockBackend *blk);
+uint32_t blk_get_zone_size(BlockBackend *blk);
+uint32_t blk_get_zone_capacity(BlockBackend *blk);
+uint32_t blk_get_max_open_zones(BlockBackend *blk);
+uint32_t blk_get_max_active_zones(BlockBackend *blk);
+uint32_t blk_get_max_append_sectors(BlockBackend *blk);
 
 void blk_io_plug(void);
 void blk_io_unplug(void);
Signature · 1 line
2.40.1
author
#2 lore raw

RFC 2/5 qcow2: add zone device metadata with zd_extension

In reply to Sam Li

Zone descriptor data is host definied data that is associated with each zone. Add zone descriptor extensions to zonedmeta and blk_get_zone_extension to access zd_extensions.

---
 block/block-backend.c             | 15 ++++++
 block/qcow2.c                     | 86 ++++++++++++++++++++++++++-----
 block/qcow2.h                     |  3 ++
 docs/interop/qcow2.txt            |  2 +
 hw/nvme/ctrl.c                    | 19 ++++---
 hw/nvme/ns.c                      | 24 ++-------
 hw/nvme/nvme.h                    |  7 ---
 include/block/block_int-common.h  |  6 +++
 include/sysemu/block-backend-io.h |  2 +
 qapi/block-core.json              |  3 ++
 10 files changed, 121 insertions(+), 46 deletions(-)
block/block-backend.c +15 -0
index ad410286a0..f68c5263f3 100644
@@ -2418,6 +2418,21 @@ uint32_t blk_get_max_append_sectors(BlockBackend *blk)
     return bs ? bs->bl.max_append_sectors : 0;
 }
 
+uint8_t *blk_get_zone_extension(BlockBackend *blk) {
+    BlockDriverState * bs = blk_bs(blk);
+    IO_CODE();
+
+    return bs ? bs->zd_extensions : NULL;
+}
+
+uint32_t blk_get_zd_ext_size(BlockBackend *blk)
+{
+    BlockDriverState *bs = blk_bs(blk);
+    IO_CODE();
+
+    return bs ? bs->bl.zd_extension_size : 0;
+}
+
 void *blk_try_blockalign(BlockBackend *blk, size_t size)
 {
     IO_CODE();
block/qcow2.c +73 -13

157 lines not on this page · raw message

block/qcow2.h +3 -0
index 38b779ae32..254295cfce 100644
@@ -250,6 +250,8 @@ typedef struct Qcow2ZonedHeaderExtension {
     uint32_t max_append_sectors;
     uint64_t zonedmeta_offset;
     uint64_t zonedmeta_size;
+    uint32_t zd_extension_size; /* must be multiple of 64 B */
+    uint32_t reserved32;
 } QEMU_PACKED Qcow2ZonedHeaderExtension;
 
 typedef struct Qcow2UnknownHeaderExtension {
@@ -445,6 +447,7 @@ typedef struct BDRVQcow2State {
     uint32_t nr_zones_imp_open;
     uint32_t nr_zones_closed;
     BlockZoneWps *wps;
+    uint64_t zded_size;
 } BDRVQcow2State;
 
 typedef struct Qcow2COWRegion {
docs/interop/qcow2.txt +2 -0
index 739e2c62c6..beb4ead094 100644
@@ -356,6 +356,8 @@ The fields of the zoned extension are:
           28 - 31:  max_append_sectors
           32 - 39:  zonedmeta_offset
           40 - 47:  zonedmeta_size
+          48 - 51:  zd_extension_size
+          52 - 55:  Reserved, must be zero.
 
 == Full disk encryption header pointer ==
 
hw/nvme/ctrl.c +13 -6
index 4e1608f0c1..4320f3a15c 100644
@@ -4010,6 +4010,12 @@ static uint16_t nvme_zone_mgmt_send_zrwa_flush(NvmeCtrl *n, NvmeZone *zone,
     return NVME_SUCCESS;
 }
 
+static inline uint8_t *nvme_get_zd_extension(NvmeNamespace *ns,
+                                        uint32_t zone_idx)
+{
+    return &ns->zd_extensions[zone_idx * blk_get_zd_ext_size(ns->blkconf.blk)];
+}
+
 static uint16_t nvme_zone_mgmt_send(NvmeCtrl *n, NvmeRequest *req)
 {
     NvmeZoneSendCmd *cmd = (NvmeZoneSendCmd *)&req->cmd;
@@ -4094,11 +4100,11 @@ static uint16_t nvme_zone_mgmt_send(NvmeCtrl *n, NvmeRequest *req)
 
     case NVME_ZONE_ACTION_SET_ZD_EXT:
         trace_pci_nvme_set_descriptor_extension(slba, zone_idx);
-        if (all || !ns->params.zd_extension_size) {
+        if (all || !blk_get_zd_ext_size(ns->blkconf.blk)) {
             return NVME_INVALID_FIELD | NVME_DNR;
         }
         zd_ext = nvme_get_zd_extension(ns, zone_idx);
-        status = nvme_h2c(n, zd_ext, ns->params.zd_extension_size, req);
+        status = nvme_h2c(n, zd_ext, blk_get_zd_ext_size(ns->blkconf.blk), req);
         if (status) {
             trace_pci_nvme_err_zd_extension_map_error(zone_idx);
             return status;
@@ -4189,7 +4195,7 @@ static uint16_t nvme_zone_mgmt_recv(NvmeCtrl *n, NvmeRequest *req)
     if (zra != NVME_ZONE_REPORT && zra != NVME_ZONE_REPORT_EXTENDED) {
         return NVME_INVALID_FIELD | NVME_DNR;
     }
-    if (zra == NVME_ZONE_REPORT_EXTENDED && !ns->params.zd_extension_size) {
+    if (zra == NVME_ZONE_REPORT_EXTENDED && !blk_get_zd_ext_size(ns->blkconf.blk)){
         return NVME_INVALID_FIELD | NVME_DNR;
     }
 
@@ -4211,7 +4217,7 @@ static uint16_t nvme_zone_mgmt_recv(NvmeCtrl *n, NvmeRequest *req)
 
     zone_entry_sz = sizeof(NvmeZoneDescr);
     if (zra == NVME_ZONE_REPORT_EXTENDED) {
-        zone_entry_sz += ns->params.zd_extension_size;
+        zone_entry_sz += blk_get_zd_ext_size(ns->blkconf.blk) ;
     }
 
     max_zones = (data_size - sizeof(NvmeZoneReportHeader)) / zone_entry_sz;
@@ -4249,11 +4255,12 @@ static uint16_t nvme_zone_mgmt_recv(NvmeCtrl *n, NvmeRequest *req)
             }
 
             if (zra == NVME_ZONE_REPORT_EXTENDED) {
+                int zd_ext_size = blk_get_zd_ext_size(ns->blkconf.blk);
                 if (zone->d.za & NVME_ZA_ZD_EXT_VALID) {
                     memcpy(buf_p, nvme_get_zd_extension(ns, zone_idx),
-                           ns->params.zd_extension_size);
+                           zd_ext_size);
                 }
-                buf_p += ns->params.zd_extension_size;
+                buf_p += zd_ext_size;
             }
 
             max_zones--;
hw/nvme/ns.c +4 -20
index f076593ada..c9c3a54d36 100644
@@ -218,15 +218,15 @@ static int nvme_ns_zoned_check_calc_geometry(NvmeNamespace *ns, Error **errp)
 
 static void nvme_ns_zoned_init_state(NvmeNamespace *ns)
 {
+    BlockBackend *blk = ns->blkconf.blk;
     uint64_t start = 0, zone_size = ns->zone_size;
     uint64_t capacity = ns->num_zones * zone_size;
     NvmeZone *zone;
     int i;
 
     ns->zone_array = g_new0(NvmeZone, ns->num_zones);
-    if (ns->params.zd_extension_size) {
-        ns->zd_extensions = g_malloc0(ns->params.zd_extension_size *
-                                      ns->num_zones);
+    if (blk_get_zone_extension(blk)) {
+        ns->zd_extensions = blk_get_zone_extension(blk);
     }
 
     QTAILQ_INIT(&ns->exp_open_zones);
@@ -275,7 +275,7 @@ static void nvme_ns_init_zoned(NvmeNamespace *ns)
     for (i = 0; i <= ns->id_ns.nlbaf; i++) {
         id_ns_z->lbafe[i].zsze = cpu_to_le64(ns->zone_size);
         id_ns_z->lbafe[i].zdes =
-            ns->params.zd_extension_size >> 6; /* Units of 64B */
+            blk_get_zd_ext_size(blk) >> 6; /* Units of 64B */
     }
 
     if (ns->params.zrwas) {
@@ -577,19 +577,6 @@ static int nvme_ns_check_constraints(NvmeNamespace *ns, Error **errp)
     }
 
     if (blk_get_zone_model(blk)) {
-        if (ns->params.zd_extension_size) {
-            if (ns->params.zd_extension_size & 0x3f) {
-                error_setg(errp, "zone descriptor extension size must be a "
-                           "multiple of 64B");
-                return -1;
-            }
-            if ((ns->params.zd_extension_size >> 6) > 0xff) {
-                error_setg(errp,
-                           "zone descriptor extension size is too large");
-                return -1;
-            }
-        }
-
         if (ns->params.zrwas) {
             if (ns->params.zrwas % ns->blkconf.logical_block_size) {
                 error_setg(errp, "zone random write area size (zoned.zrwas "
@@ -677,7 +664,6 @@ void nvme_ns_cleanup(NvmeNamespace *ns)
     if (blk_get_zone_model(ns->blkconf.blk)) {
         g_free(ns->id_ns_zoned);
         g_free(ns->zone_array);
-        g_free(ns->zd_extensions);
     }
 
     if (ns->endgrp && ns->endgrp->fdp.enabled) {
@@ -795,8 +781,6 @@ static Property nvme_ns_props[] = {
                        params.max_active_zones, 0),
     DEFINE_PROP_UINT32("zoned.max_open", NvmeNamespace,
                        params.max_open_zones, 0),
-    DEFINE_PROP_UINT32("zoned.descr_ext_size", NvmeNamespace,
-                       params.zd_extension_size, 0),
     DEFINE_PROP_UINT32("zoned.numzrwa", NvmeNamespace, params.numzrwa, 0),
     DEFINE_PROP_SIZE("zoned.zrwas", NvmeNamespace, params.zrwas, 0),
     DEFINE_PROP_SIZE("zoned.zrwafg", NvmeNamespace, params.zrwafg, -1),
hw/nvme/nvme.h +0 -7
index 76677a86e9..37007952fc 100644
@@ -192,7 +192,6 @@ typedef struct NvmeNamespaceParams {
     bool     cross_zone_read;
     uint32_t max_active_zones;
     uint32_t max_open_zones;
-    uint32_t zd_extension_size;
 
     uint32_t numzrwa;
     uint64_t zrwas;
@@ -315,12 +314,6 @@ static inline bool nvme_wp_is_valid(NvmeZone *zone)
            st != NVME_ZONE_STATE_OFFLINE;
 }
 
-static inline uint8_t *nvme_get_zd_extension(NvmeNamespace *ns,
-                                             uint32_t zone_idx)
-{
-    return &ns->zd_extensions[zone_idx * ns->params.zd_extension_size];
-}
-
 static inline void nvme_aor_inc_open(NvmeNamespace *ns)
 {
     assert(ns->nr_open_zones >= 0);
include/block/block_int-common.h +6 -0
index 1dbe820a9b..e16dfe8581 100644
@@ -61,6 +61,7 @@
 #define BLOCK_OPT_Z_MODEL           "zoned"
 #define BLOCK_OPT_Z_SIZE            "zone_size"
 #define BLOCK_OPT_Z_CAP             "zone_capacity"
+#define BLOCK_OPT_Z_DEXTSIZE        "zd_extension_size"
 #define BLOCK_OPT_Z_NR_COV          "zone_nr_conv"
 #define BLOCK_OPT_Z_MAS             "max_append_sectors"
 #define BLOCK_OPT_Z_MAZ             "max_active_zones"
@@ -907,6 +908,9 @@ typedef struct BlockLimits {
     uint32_t max_active_zones;
 
     uint32_t write_granularity;
+
+    /* size of data that is associated with a zone in bytes */
+    uint32_t zd_extension_size;
 } BlockLimits;
 
 typedef struct BdrvOpBlocker BdrvOpBlocker;
@@ -1265,6 +1269,8 @@ struct BlockDriverState {
 
     /* array of write pointers' location of each zone in the zoned device. */
     BlockZoneWps *wps;
+
+    uint8_t *zd_extensions;
 };
 
 struct BlockBackendRootState {
include/sysemu/block-backend-io.h +2 -0
index 3be221e752..c56ed29c8f 100644
@@ -106,6 +106,8 @@ uint32_t blk_get_zone_capacity(BlockBackend *blk);
 uint32_t blk_get_max_open_zones(BlockBackend *blk);
 uint32_t blk_get_max_active_zones(BlockBackend *blk);
 uint32_t blk_get_max_append_sectors(BlockBackend *blk);
+uint8_t *blk_get_zone_extension(BlockBackend *blk);
+uint32_t blk_get_zd_ext_size(BlockBackend *blk);
 
 void blk_io_plug(void);
 void blk_io_unplug(void);
qapi/block-core.json +3 -0
index 0c97ae678b..f71dd18fc3 100644
@@ -5024,6 +5024,8 @@
 #                 (default: off, since 8.0)
 # @zone-size: The size of a zone of the zoned device (since 8.0)
 # @zone-capacity: The capacity of a zone of the zoned device (since 8.0)
+# @zd-extension-size: Zone descriptor extension size. Must be a multiple of
+#                     64 bytes (since 8.0)
 # @zone-nr-conv: The number of conventional zones of the zoned device
 #                (since 8.0)
 # @max-open-zones: The maximal allowed open zones (since 8.0)
@@ -5052,6 +5054,7 @@
             '*zoned-profile':      'str',
             '*zone-size':          'size',
             '*zone-capacity':      'size',
+            '*zd-extension-size':  'size',
             '*zone-nr-conv':       'uint32',
             '*max-open-zones':     'uint32',
             '*max-active-zones':   'uint32',
Signature · 1 line
2.40.1
#3 lore raw

Re: [RFC 2/5] qcow2: add zone device metadata with zd_extension

Quoted 5 lines · Sam Li

Zone descriptor data is host definied data that is associated with each zone. Add zone descriptor extensions to zonedmeta and blk_get_zone_extension to access zd_extensions.

[...]

qapi/block-core.json line 5031

Quoted 13 lines
qapi/block-core.json +2 -0
index 0c97ae678b..f71dd18fc3 100644
@@ -5024,6 +5024,8 @@
 #                 (default: off, since 8.0)
 # @zone-size: The size of a zone of the zoned device (since 8.0)
 # @zone-capacity: The capacity of a zone of the zoned device (since 8.0)
+# @zd-extension-size: Zone descriptor extension size. Must be a multiple of
+#                     64 bytes (since 8.0)
 # @zone-nr-conv: The number of conventional zones of the zoned device
 #                (since 8.0)
 # @max-open-zones: The maximal allowed open zones (since 8.0)

For better or worse, we tend to avoid abbreviations in QAPI schema names. Would @zone-descriptor-extension-size be too unwieldy?

Quoted 8 lines
qapi/block-core.json +1 -0
@@ -5052,6 +5054,7 @@
             '*zoned-profile':      'str',
             '*zone-size':          'size',
             '*zone-capacity':      'size',
+            '*zd-extension-size':  'size',
             '*zone-nr-conv':       'uint32',
             '*max-open-zones':     'uint32',
             '*max-active-zones':   'uint32',
author
#4 lore raw

RFC 3/5 hw/nvme: make the metadata of ZNS emulation persistent

In reply to Sam Li

The NVMe ZNS devices follow NVMe ZNS spec but the state of namespace zones does not persist accross restarts of QEMU. This patch makes the metadata of ZNS emulation persistent by using new block layer APIs. The ZNS device calls zone report and zone mgmt APIs from the block layer which will handle zone state transition and manage zone resources.

---
 block/block-backend.c             |   15 +
 block/qcow2.c                     |    3 +
 hw/nvme/ctrl.c                    | 1114 ++++++-----------------------
 hw/nvme/ns.c                      |   77 +-
 hw/nvme/nvme.h                    |   85 +--
 include/block/block-common.h      |    8 +
 include/block/block_int-common.h  |    2 +
 include/sysemu/block-backend-io.h |    2 +
 8 files changed, 283 insertions(+), 1023 deletions(-)
block/block-backend.c +15 -0
index f68c5263f3..9c95ae0267 100644
@@ -2418,6 +2418,14 @@ uint32_t blk_get_max_append_sectors(BlockBackend *blk)
     return bs ? bs->bl.max_append_sectors : 0;
 }
 
+uint32_t blk_get_nr_zones(BlockBackend *blk)
+{
+    BlockDriverState *bs = blk_bs(blk);
+    IO_CODE();
+
+    return bs ? bs->bl.nr_zones : 0;
+}
+
 uint8_t *blk_get_zone_extension(BlockBackend *blk) {
     BlockDriverState * bs = blk_bs(blk);
     IO_CODE();
@@ -2433,6 +2441,13 @@ uint32_t blk_get_zd_ext_size(BlockBackend *blk)
     return bs ? bs->bl.zd_extension_size : 0;
 }
 
+BlockZoneWps *blk_get_zone_wps(BlockBackend *blk) {
+    BlockDriverState * bs = blk_bs(blk);
+    IO_CODE();
+
+    return bs ? bs->wps : NULL;
+}
+
 void *blk_try_blockalign(BlockBackend *blk, size_t size)
 {
     IO_CODE();
block/qcow2.c +3 -0
index fce1fe83a7..41549dd68b 100644
@@ -4854,6 +4854,9 @@ static int coroutine_fn qcow2_co_zone_mgmt(BlockDriverState *bs, BlockZoneOp op,
     case BLK_ZO_RESET:
         ret = qcow2_reset_zone(bs, index, len);
         break;
+    case BLK_ZO_OFFLINE:
+        ret = qcow2_write_wp_at(bs, &wps->wp[index], index, BLK_ZO_OFFLINE);
+        break;
     default:
         error_report("Unsupported zone op: 0x%x", op);
         ret = -ENOTSUP;
hw/nvme/ctrl.c +237 -877

1,396 lines not on this page · raw message

hw/nvme/ns.c +1 -76

110 lines not on this page · raw message

hw/nvme/nvme.h +15 -70

116 lines not on this page · raw message

include/block/block-common.h +8 -0
index 9f04a772f6..0cbed607a8 100644
@@ -83,6 +83,7 @@ typedef enum BlockZoneOp {
     BLK_ZO_CLOSE,
     BLK_ZO_FINISH,
     BLK_ZO_RESET,
+    BLK_ZO_OFFLINE,
 } BlockZoneOp;
 
 typedef enum BlockZoneModel {
@@ -262,6 +263,13 @@ typedef enum {
  */
 #define BDRV_ZT_IS_CONV(wp)    (wp & (1ULL << 63))
 
+/*
+ * Clear the zone state, type and attribute information in the wp.
+ */
+#define BDRV_ZP_GET_WP(wp)     ((wp << 6) >> 6)
+#define BDRV_ZP_GET_ZS(wp)     (wp >> 60)
+#define BDRV_ZP_GET_ZA(wp)      (wp & ((1ULL << 8) - 1ULL) << 51)
+
 #define BDRV_REQUEST_MAX_SECTORS MIN_CONST(SIZE_MAX >> BDRV_SECTOR_BITS, \
                                            INT_MAX >> BDRV_SECTOR_BITS)
 #define BDRV_REQUEST_MAX_BYTES (BDRV_REQUEST_MAX_SECTORS << BDRV_SECTOR_BITS)
include/block/block_int-common.h +2 -0
index e16dfe8581..79a62b5271 100644
@@ -911,6 +911,8 @@ typedef struct BlockLimits {
 
     /* size of data that is associated with a zone in bytes */
     uint32_t zd_extension_size;
+
+    uint8_t zone_attribute;
 } BlockLimits;
 
 typedef struct BdrvOpBlocker BdrvOpBlocker;
include/sysemu/block-backend-io.h +2 -0
index c56ed29c8f..f69aa1094a 100644
@@ -106,8 +106,10 @@ uint32_t blk_get_zone_capacity(BlockBackend *blk);
 uint32_t blk_get_max_open_zones(BlockBackend *blk);
 uint32_t blk_get_max_active_zones(BlockBackend *blk);
 uint32_t blk_get_max_append_sectors(BlockBackend *blk);
+uint32_t blk_get_nr_zones(BlockBackend *blk);
 uint8_t *blk_get_zone_extension(BlockBackend *blk);
 uint32_t blk_get_zd_ext_size(BlockBackend *blk);
+BlockZoneWps *blk_get_zone_wps(BlockBackend *blk);
 
 void blk_io_plug(void);
 void blk_io_unplug(void);
Signature · 1 line
2.40.1