hw/nvme: refactor zone append writes using block layer APIs
RFC 4/5 hw/nvme: refactor zone append writes using block layer APIs
Signed-off-by Sam Li <[email protected]>
--- block/block-backend.c | 8 ++ block/qcow2.c | 7 +- hw/nvme/ctrl.c | 195 ++++++++++++++++++++++-------- include/sysemu/block-backend-io.h | 1 + include/sysemu/dma.h | 3 + softmmu/dma-helpers.c | 17 +++ 6 files changed, 181 insertions(+), 50 deletions(-)
block/block-backend.c +8 -0
@@ -2426,6 +2426,14 @@ uint32_t blk_get_nr_zones(BlockBackend *blk)return bs ? bs->bl.nr_zones :0 ; } +uint32_t blk_get_write_granularity (BlockBackend *blk) +{ +BlockDriverState *bs =blk_bs (blk); +IO_CODE (); + +return bs ? bs->bl.write_granularity :0 ; +} +uint8_t *blk_get_zone_extension (BlockBackend *blk) {BlockDriverState * bs =blk_bs (blk);IO_CODE ();
block/qcow2.c +6 -1
@@ -2198,7 +2198,7 @@ static void qcow2_refresh_limits(BlockDriverState *bs, Error **errp) 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.write_granularity =BDRV_SECTOR_SIZE ; + bs->bl.write_granularity =4096 ;/* physical block size */ } static int qcow2_reopen_prepare(BDRVReopenState *state,
@@ -4915,6 +4915,11 @@ qcow2_co_zone_append(BlockDriverState *bs, int64_t *offset, QEMUIOVector *qiov,qemu_co_mutex_lock (&s->wps->colock);uint64_t wp = s->wps->wp[index];uint64_t wp_i =qcow2_get_wp (wp); +printf ("qcow2 offset 0x%lx\n" , *offset); +printf ("checking wp[%ld]: 0b%lb\n" , *offset / bs->bl.zone_size, wp); +for (int i =0 ; i < bs->bl.nr_zones; i++) { +printf ("Listing wp[%d]: 0b%lb\n" , i, s->wps->wp[i]); + } ret =qcow2_co_pwritev_part (bs, wp_i, len, qiov,0 ,0 );if (ret ==0 ) { *offset = wp_i;
hw/nvme/ctrl.c +146 -49
239 lines not on this page · raw message
include/sysemu/block-backend-io.h +1 -0
@@ -109,6 +109,7 @@ 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); +uint32_t blk_get_write_granularity (BlockBackend *blk);BlockZoneWps *blk_get_zone_wps (BlockBackend *blk);void blk_io_plug (void );
include/sysemu/dma.h +3 -0
@@ -301,6 +301,9 @@ BlockAIOCB *dma_blk_read(BlockBackend *blk,BlockAIOCB *dma_blk_write (BlockBackend *blk,QEMUSGList *sg,uint64_t offset,uint32_t align,BlockCompletionFunc *cb,void *opaque); +BlockAIOCB *dma_blk_zone_append (BlockBackend *blk, +QEMUSGList *sg,int64_t offset,uint32_t align, +void (*cb)(void *opaque,int ret),void *opaque);MemTxResult dma_buf_read (void *ptr,dma_addr_t len,dma_addr_t *residual,QEMUSGList *sg,MemTxAttrs attrs); MemTxResult dma_buf_write(void *ptr, dma_addr_t len, dma_addr_t *residual,
softmmu/dma-helpers.c +17 -0
@@ -282,6 +282,23 @@ BlockAIOCB *dma_blk_write(BlockBackend *blk, DMA_DIRECTION_TO_DEVICE); } +static +BlockAIOCB *dma_blk_zone_append_io_func (int64_t offset,QEMUIOVector *iov, +BlockCompletionFunc *cb,void *cb_opaque, +void *opaque) +{ +BlockBackend *blk = opaque; +return blk_aio_zone_append (blk, (int64_t *)offset, iov,0 , cb, cb_opaque); +} + +BlockAIOCB *dma_blk_zone_append (BlockBackend *blk, +QEMUSGList *sg,int64_t offset,uint32_t align, +void (*cb)(void *opaque,int ret),void *opaque) +{ +return dma_blk_io (blk_get_aio_context (blk), sg, offset, align, + dma_blk_zone_append_io_func, blk, cb, opaque, +DMA_DIRECTION_TO_DEVICE ); +} static MemTxResult dma_buf_rw(void *buf, dma_addr_t len, dma_addr_t *residual, QEMUSGList *sg, DMADirection dir,
Signature · 1 line
2.40.1
RFC 5/5 hw/nvme: make ZDED persistent
Zone descriptor extension data (ZDED) is not persistent across QEMU restarts. The zone descriptor extension valid bit (ZDEV) is part of zone attributes, which sets to one when the ZDED is associated with the zone.
With the qcow2-ZNS file as the backing file, the NVMe ZNS device stores the zone attributes at the following eight bit of zoned bit of write pointers for each zone. The ZDED is stored as part of zoned metadata as write pointers.
Signed-off-by Sam Li <[email protected]>
--- block/qcow2.c | 44 +++++++++++++++++++++++++++++++++++- hw/nvme/ctrl.c | 6 +---- include/block/block-common.h | 1 + 3 files changed, 45 insertions(+), 6 deletions(-)
block/qcow2.c +43 -1
@@ -25,6 +25,7 @@#include "qemu/osdep.h" #include "block/qdict.h" +#include "block/nvme.h" #include "sysemu/block-backend.h" #include "qemu/main-loop.h" #include "qemu/module.h"
@@ -214,6 +215,17 @@ static inline void qcow2_set_wp(uint64_t *wp, BlockZoneState zs) *wp = addr; } +static inline void qcow2_set_za (uint64_t *wp,uint8_t za) +{ +/* +* The zone attribute takes up one byte. Store it after the zoned +* bit. +*/ +uint64_t addr = *wp; + addr |= ((uint64_t )za <<51 ); + *wp = addr; +} +/* * File wp tracking: reset zone, finish zone and append zone can * change the value of write pointer. All zone operations will change
@@ -308,7 +320,7 @@ static int qcow2_check_open(BlockDriverState *bs)/* * The zoned device has limited zone resources of open, closed, active -* zones. +* zones. Check if we can manage a zone without exceeding those limits. */ static int qcow2_check_zone_resources (BlockDriverState *bs,BlockZoneState zs)
@@ -4801,6 +4813,33 @@ unlock:return ret; } +static int qcow2_zns_set_zded (BlockDriverState *bs,uint32_t index) +{ +BDRVQcow2State *s = bs->opaque; +int ret; + +qemu_co_mutex_lock (&s->wps->colock); +uint64_t *wp = &s->wps->wp[index]; +BlockZoneState zs =qcow2_get_zs (*wp); +if (zs ==BLK_ZS_EMPTY ) { + ret =qcow2_check_zone_resources (bs, zs); +if (ret <0 ) { +return ret; + } + +qcow2_set_za (wp,NVME_ZA_ZD_EXT_VALID ); + ret =qcow2_write_wp_at (bs, wp, index,BLK_ZO_CLOSE ); +if (ret <0 ) { +error_report ("Failed to set zone extension at 0x%" PRIx64"" , *wp); +return ret; + } + s->nr_zones_closed++; +return ret; + } + +return NVME_ZONE_INVAL_TRANSITION ; +} + static int coroutine_fn qcow2_co_zone_mgmt(BlockDriverState *bs, BlockZoneOp op,int64_t offset,int64_t len) {
@@ -4857,6 +4896,9 @@ static int coroutine_fn qcow2_co_zone_mgmt(BlockDriverState *bs, BlockZoneOp op,case BLK_ZO_OFFLINE : ret =qcow2_write_wp_at (bs, &wps->wp[index], index,BLK_ZO_OFFLINE );break ; +case BLK_ZO_SET_ZDED : + ret =qcow2_zns_set_zded (bs, index); +break ;default :error_report ("Unsupported zone op: 0x%x" , op); ret = -ENOTSUP ;
hw/nvme/ctrl.c +1 -5
@@ -3425,11 +3425,6 @@ static uint16_t nvme_zone_mgmt_send(NvmeCtrl *n, NvmeRequest *req)NvmeNamespace *ns = req->ns;NvmeZoneMgmtAIOCB *iocb;uint64_t slba =0 ; -uint64_t offset; -BlockBackend *blk = ns->blkconf.blk; -uint32_t zone_size =blk_get_zone_size (blk); -uint64_t size = zone_size *blk_get_nr_zones (blk); -int64_t len;uint32_t zone_idx =0 ;uint16_t status;uint8_t action = cmd->zsa;
@@ -3485,6 +3480,7 @@ static uint16_t nvme_zone_mgmt_send(NvmeCtrl *n, NvmeRequest *req)break ;case NVME_ZONE_ACTION_SET_ZD_EXT : + op =BLK_ZO_SET_ZDED ;int zd_ext_size =blk_get_zd_ext_size (blk);trace_pci_nvme_set_descriptor_extension (slba, zone_idx);if (all || !zd_ext_size) {
include/block/block-common.h +1 -0
@@ -84,6 +84,7 @@ typedef enum BlockZoneOp {BLK_ZO_FINISH ,BLK_ZO_RESET ,BLK_ZO_OFFLINE , +BLK_ZO_SET_ZDED , } BlockZoneOp; typedef enum BlockZoneModel {
Signature · 1 line
2.40.1