Commit 86d136c484 for qemu.org

commit 86d136c48476738ab6831940ee24c1482f8a403d
Author: Klaus Jensen <k.jensen@samsung.com>
Date:   Wed Nov 18 13:45:34 2026 +0100

    hw/nvme: fix open zone zrwa allocation

    nvme_zrm_open_flags incorrectly allocates a ZRWA resource regardless of
    the zone state. A ZRWA resource shall only be allocated if

        a) there are available ZRWA resources; and
        b) the zone is empty.

    Fix the state machine and error out early.

    Cc: qemu-stable@nongnu.org
    Link: https://gitlab.com/qemu-project/qemu/-/work_items/4520
    Fixes: e321b4cdc2dd ("hw/nvme: add support for zoned random write area")
    Reviewed-by: Jesper Wendel Devantier <foss@defmacro.it>
    Signed-off-by: Klaus Jensen <k.jensen@samsung.com>

diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c
index 5e8788a257..16860ccd95 100644
--- a/hw/nvme/ctrl.c
+++ b/hw/nvme/ctrl.c
@@ -2038,10 +2038,19 @@ enum {
 static uint16_t nvme_zrm_open_flags(NvmeCtrl *n, NvmeNamespace *ns,
                                     NvmeZone *zone, int flags)
 {
+    NvmeZoneState state = nvme_get_zone_state(zone);
+
+    bool zrwaa =
+        (flags & NVME_ZRM_ZRWA) && !(zone->d.za & NVME_ZA_ZRWA_VALID);
+
     int act = 0;
     uint16_t status;

-    switch (nvme_get_zone_state(zone)) {
+    if (zrwaa && state != NVME_ZONE_STATE_EMPTY) {
+        return NVME_INVALID_ZONE_OP | NVME_DNR;
+    }
+
+    switch (state) {
     case NVME_ZONE_STATE_EMPTY:
         act = 1;

@@ -2051,8 +2060,7 @@ static uint16_t nvme_zrm_open_flags(NvmeCtrl *n, NvmeNamespace *ns,
         if (n->params.auto_transition_zones) {
             nvme_zrm_auto_transition_zone(ns);
         }
-        status = nvme_zns_check_resources(ns, act, 1,
-                                          (flags & NVME_ZRM_ZRWA) ? 1 : 0);
+        status = nvme_zns_check_resources(ns, act, 1, zrwaa);
         if (status) {
             return status;
         }
@@ -2080,7 +2088,7 @@ static uint16_t nvme_zrm_open_flags(NvmeCtrl *n, NvmeNamespace *ns,
         /* fallthrough */

     case NVME_ZONE_STATE_EXPLICITLY_OPEN:
-        if (flags & NVME_ZRM_ZRWA) {
+        if (zrwaa) {
             ns->zns.numzrwa--;

             zone->d.za |= NVME_ZA_ZRWA_VALID;