HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/vmspawn/vmspawn-qmp.c

87,329 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 0e34b05a54bb5d1403f7e64677a0c022f6739898

Lines 1001–2000 of 2,100

                                return log_error_errno(r, "No PCIe hotplug ports left for SCSI controller");
                        if (r < 0)
                                return log_error_errno(r, "Failed to allocate PCIe hotplug port for SCSI controller: %m");
                }
 
                r = qmp_setup_scsi_controller(bridge, controller_port);
                if (r < 0) {
                        vmspawn_qmp_bridge_release_pcie_port_by_idx(bridge, controller_port_idx);
                        return r;
                }
 
                /* Set before the reply so a second SCSI hotplug queued in the meantime
                 * doesn't re-create the controller; reset in on_scsi_controller_complete on error. */
                bridge->scsi_controller_port_idx = controller_port_idx;
                bridge->scsi_controller_created = true;
        }
 
        slot_ref = drive_info_ref(drive);
        r = qmp_fdset_add(bridge->qmp, TAKE_FD(drive->fd),
                          on_add_observe_stage, slot_ref, &drive->fdset_path, &drive->fdset_id);
        if (r < 0)
                return r;
        TAKE_PTR(slot_ref);
 
        /* Build flags for the file-level node: RO and NO_FLUSH from the drive
         * plus IO_URING from the bridge feature probe. */
        QmpDriveFlags file_flags = drive->flags & (QMP_DRIVE_READ_ONLY|QMP_DRIVE_NO_FLUSH);
        if (FLAGS_SET(bridge->features, VMSPAWN_QMP_FEATURE_IO_URING))
                file_flags |= QMP_DRIVE_IO_URING;
 
        QmpFileNodeParams file_params = {
                .node_name = drive->qmp_file_node_name,
                .filename  = drive->fdset_path,
                .driver    = FLAGS_SET(drive->flags, QMP_DRIVE_BLOCK_DEVICE) ? "host_device" : "file",
                .flags     = file_flags,
        };
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *file_args = NULL;
        r = qmp_build_blockdev_add_file(&file_params, &file_args);
        if (r < 0)
                return r;
 
        slot_ref = drive_info_ref(drive);
        r = qmp_client_invoke(bridge->qmp, /* ret_slot= */ NULL, "blockdev-add", QMP_CLIENT_ARGS(file_args),
                              on_add_file_node_stage, slot_ref);
        if (r < 0)
                return r;
        TAKE_PTR(slot_ref);
 
        QmpFormatNodeParams format_params = {
                .node_name      = drive->qmp_node_name,
                .format         = drive->format,
                .file_node_name = drive->qmp_file_node_name,
                .flags          = drive->flags,
        };
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *format_args = NULL;
        r = qmp_build_blockdev_add_format(&format_params, &format_args);
        if (r < 0)
                return r;
 
        slot_ref = drive_info_ref(drive);
        r = qmp_client_invoke(bridge->qmp, /* ret_slot= */ NULL, "blockdev-add", QMP_CLIENT_ARGS(format_args),
                              on_add_format_node_stage, slot_ref);
        if (r < 0)
                return r;
        TAKE_PTR(slot_ref);
 
        /* Release the monitor's original fd; blockdev-add(file) above took a dup. */
        slot_ref = drive_info_ref(drive);
        r = qmp_fdset_remove(bridge->qmp, drive->fdset_id, on_add_observe_stage, slot_ref);
        if (r < 0)
                return r;
        TAKE_PTR(slot_ref);
 
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *device_args = NULL;
        r = qmp_build_device_add(drive, &device_args);
        if (r < 0)
                return r;
 
        slot_ref = drive_info_ref(drive);
        r = qmp_client_invoke(bridge->qmp, /* ret_slot= */ NULL, "device_add", QMP_CLIENT_ARGS(device_args),
                              on_add_device_add_complete, slot_ref);
        if (r < 0)
                return r;
        TAKE_PTR(slot_ref);
 
        TAKE_PTR(registered);
        return 0;
}
 
static int qmp_setup_regular_drive(VmspawnQmpBridge *bridge, DriveInfo *drive) {
        assert(bridge);
        assert(drive);
        assert(drive->fd >= 0);
 
        return vmspawn_qmp_add_block_device(bridge, drive);
}
 
/* device_del completion is just QEMU acking the request; teardown happens
 * in vmspawn_qmp_dispatch_device_deleted() once the guest acks the eject. */
static int on_remove_device_del_complete(
                QmpClient *client,
                sd_json_variant *result,
                const char *error_desc,
                int error,
                void *userdata) {
 
        _cleanup_(drive_info_unrefp) DriveInfo *drive = ASSERT_PTR(userdata);
        _cleanup_(sd_varlink_unrefp) sd_varlink *link = TAKE_PTR(drive->link);
 
        assert(client);
        assert(link);
 
        if (error < 0) {
                /* device_del rejected: clear the pending bit so the caller can retry. */
                drive->state &= ~BLOCK_DEVICE_STATE_REMOVE_PENDING;
 
                return reply_qmp_error(link, error_desc, error);
        }
 
        return sd_varlink_reply(link, NULL);
}
 
int vmspawn_qmp_remove_block_device(VmspawnQmpBridge *bridge, sd_varlink *link, const char *id) {
        int r;
 
        assert(bridge);
        assert(link);
        assert(id);
 
        DriveInfo *drive = hashmap_get(bridge->block_devices, id);
        if (!drive)
                return sd_varlink_error(link, "io.systemd.MachineInstance.NoSuchStorage", NULL);
        if (!FLAGS_SET(drive->flags, QMP_DRIVE_REMOVABLE))
                return sd_varlink_error(link, "io.systemd.MachineInstance.StorageImmutable", NULL);
        if (!FLAGS_SET(drive->state, BLOCK_DEVICE_STATE_BLOCKDEV_ADDED))
                return reply_qmp_error(link, "Block device add pending", -EBUSY);
        if (drive->state & (BLOCK_DEVICE_STATE_REMOVE_PENDING|BLOCK_DEVICE_STATE_REPLACE_PENDING))
                return reply_qmp_error(link, "Block device replace/remove pending", -EBUSY);
 
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *args = NULL;
        r = sd_json_buildo(&args, SD_JSON_BUILD_PAIR_STRING("id", drive->qmp_device_id));
        if (r < 0)
                return sd_varlink_error_errno(link, r);
 
        assert(!drive->link);
        drive->link = sd_varlink_ref(link);
        drive->state |= BLOCK_DEVICE_STATE_REMOVE_PENDING;
 
        r = qmp_client_invoke(bridge->qmp, /* ret_slot= */ NULL, "device_del", QMP_CLIENT_ARGS(args),
                              on_remove_device_del_complete, drive_info_ref(drive));
        if (r < 0) {
                drive->link = sd_varlink_unref(drive->link);
                drive->state &= ~BLOCK_DEVICE_STATE_REMOVE_PENDING;
                drive_info_unref(drive);
                return sd_varlink_error_errno(link, r);
        }
        return 0;
}
 
/* DEVICE_DELETED arrives once the guest has acked the eject; only then is it
 * safe to drop the blockdev node and release the registry slot (and PCIe port). */
static int vmspawn_qmp_dispatch_device_deleted(VmspawnQmpBridge *bridge, sd_json_variant *data) {
        assert(bridge);
 
        if (!data)
                return 0;
 
        const char *qmp_device_id = sd_json_variant_string(sd_json_variant_by_key(data, "device"));
        if (!qmp_device_id)
                return 0;
 
        DriveInfo *drive = hashmap_get(bridge->block_devices_by_qmp_id, qmp_device_id);
        if (!drive)
                return 0;
 
        vmspawn_qmp_block_device_teardown(bridge->qmp, drive->qmp_node_name,
                                          drive->qmp_file_node_name, drive->state);
 
        assert_se(bridge_unregister_drive(bridge, drive) == drive);
        drive_info_unref(drive);
        return 0;
}
 
typedef enum ReplaceCtxStateFlags {
        REPLACE_CTX_FAILED             = 1u << 0,  /* idempotency sentinel */
        REPLACE_CTX_NEW_BLOCKDEV_ADDED = 1u << 1,  /* blockdev-add(new file) ack'd */
        REPLACE_CTX_REOPEN_COMMITTED   = 1u << 2,  /* blockdev-reopen ack'd; new state moved onto DriveInfo */
} ReplaceCtxStateFlags;
 
/* Bits ReplaceStorage may change; others are preserved across replace. */
#define QMP_DRIVE_REPLACE_MUTABLE_MASK \
        (QMP_DRIVE_BLOCK_DEVICE | QMP_DRIVE_READ_ONLY | QMP_DRIVE_IO_URING)
 
/* Per-ReplaceStorage state. One ref per outstanding QMP callback plus one for
 * the entry-point local (until TAKE_PTR'd into the chain). On commit the
 * contents are folded into DriveInfo. */
typedef struct ReplaceCtx {
        unsigned n_ref;
 
        DriveInfo *drive;            /* ref'd */
        char *new_file_node_name;
        char *new_fdset_path;
        uint64_t new_fdset_id;
        QmpDriveFlags new_flags;
 
        char *old_file_node_name;
 
        ReplaceCtxStateFlags state;
} ReplaceCtx;
 
static ReplaceCtx* replace_ctx_free(ReplaceCtx *ctx) {
        if (!ctx)
                return NULL;
 
        drive_info_unref(ctx->drive);
        free(ctx->new_file_node_name);
        free(ctx->new_fdset_path);
        free(ctx->old_file_node_name);
        return mfree(ctx);
}
 
DEFINE_PRIVATE_TRIVIAL_REF_FUNC(ReplaceCtx, replace_ctx);
DEFINE_PRIVATE_TRIVIAL_UNREF_FUNC(ReplaceCtx, replace_ctx, replace_ctx_free);
DEFINE_TRIVIAL_CLEANUP_FUNC(ReplaceCtx*, replace_ctx_unref);
 
/* First-error handler for the replace pipeline. Idempotent. Best-effort tears
 * down whatever new-side state we created on the wire, clears REPLACE_PENDING,
 * and replies on drive->link (if any). */
static int replace_fail(ReplaceCtx *ctx, int error, const char *error_desc) {
        assert(ctx);
 
        if (FLAGS_SET(ctx->state, REPLACE_CTX_FAILED))
                return 0;
        ctx->state |= REPLACE_CTX_FAILED;
 
        DriveInfo *drive = ctx->drive;
        assert(drive);
 
        /* If the new file node was added, del it; that also drops the fdset's
         * dup so the new fdset auto-frees. If add-fd succeeded but blockdev-add
         * never did, an explicit remove-fd is needed instead. */
        if (FLAGS_SET(ctx->state, REPLACE_CTX_NEW_BLOCKDEV_ADDED)) {
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *args = NULL;
                if (sd_json_buildo(&args, SD_JSON_BUILD_PAIR_STRING("node-name", ctx->new_file_node_name)) >= 0)
                        (void) qmp_client_invoke(drive->bridge->qmp, /* ret_slot= */ NULL, "blockdev-del",
                                                 QMP_CLIENT_ARGS(args),
                                                 on_qmp_complete, (void*) "replace rollback blockdev-del");
        } else if (ctx->new_fdset_path) {
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *args = NULL;
                if (sd_json_buildo(&args, SD_JSON_BUILD_PAIR_UNSIGNED("fdset-id", ctx->new_fdset_id)) >= 0)
                        (void) qmp_client_invoke(drive->bridge->qmp, /* ret_slot= */ NULL, "remove-fd",
                                                 QMP_CLIENT_ARGS(args),
                                                 on_qmp_complete, (void*) "replace rollback remove-fd");
        }
 
        drive->state &= ~BLOCK_DEVICE_STATE_REPLACE_PENDING;
        _cleanup_(sd_varlink_unrefp) sd_varlink *link = TAKE_PTR(drive->link);
        if (link)
                return reply_qmp_error(link, error_desc, error);
        return 0;
}
 
static int on_replace_observe_stage(
                QmpClient *client,
                sd_json_variant *result,
                const char *error_desc,
                int error,
                void *userdata) {
 
        _cleanup_(replace_ctx_unrefp) ReplaceCtx *ctx = ASSERT_PTR(userdata);
        assert(client);
 
        if (error < 0)
                return replace_fail(ctx, error, error_desc);
        return 0;
}
 
static int on_replace_blockdev_add_complete(
                QmpClient *client,
                sd_json_variant *result,
                const char *error_desc,
                int error,
                void *userdata) {
 
        _cleanup_(replace_ctx_unrefp) ReplaceCtx *ctx = ASSERT_PTR(userdata);
        assert(client);
 
        if (error < 0)
                return replace_fail(ctx, error, error_desc);
 
        /* If a sync error elsewhere has already marked the chain failed, the
         * just-added file node is orphaned — tear it down retroactively. */
        if (FLAGS_SET(ctx->state, REPLACE_CTX_FAILED)) {
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *args = NULL;
                if (sd_json_buildo(&args, SD_JSON_BUILD_PAIR_STRING("node-name", ctx->new_file_node_name)) >= 0)
                        (void) qmp_client_invoke(ctx->drive->bridge->qmp, /* ret_slot= */ NULL,
                                                 "blockdev-del", QMP_CLIENT_ARGS(args),
                                                 on_qmp_complete,
                                                 (void*) "replace retroactive blockdev-del");
                return 0;
        }
 
        ctx->state |= REPLACE_CTX_NEW_BLOCKDEV_ADDED;
        return 0;
}
 
static int on_replace_old_blockdev_del_complete(
                QmpClient *client,
                sd_json_variant *result,
                const char *error_desc,
                int error,
                void *userdata) {
 
        _cleanup_(replace_ctx_unrefp) ReplaceCtx *ctx = ASSERT_PTR(userdata);
        assert(client);
 
        DriveInfo *drive = ctx->drive;
 
        /* The swap itself succeeded at reopen-commit time. If del of the old
         * file node failed, the orphan persists until VM exit — log and reply
         * success. The fdset auto-freed when the dup was released regardless. */
        if (error < 0)
                log_warning("Failed to delete orphaned file node '%s' after replace: %s",
                            ctx->old_file_node_name, strna(error_desc));
 
        drive->state &= ~BLOCK_DEVICE_STATE_REPLACE_PENDING;
        _cleanup_(sd_varlink_unrefp) sd_varlink *link = TAKE_PTR(drive->link);
        if (link)
                (void) sd_varlink_reply(link, NULL);
 
        log_info("Block device '%s' backing replaced", drive->id);
        return 0;
}
 
static int on_replace_blockdev_reopen_complete(
                QmpClient *client,
                sd_json_variant *result,
                const char *error_desc,
                int error,
                void *userdata) {
 
        _cleanup_(replace_ctx_unrefp) ReplaceCtx *ctx = ASSERT_PTR(userdata);
        assert(client);
 
        if (error < 0)
                return replace_fail(ctx, error, error_desc);
 
        DriveInfo *drive = ctx->drive;
 
        /* Atomic commit: the format graph now references the new file node.
         * Move the new-side state from ctx onto DriveInfo so subsequent
         * teardowns find the right names. */
        ctx->state |= REPLACE_CTX_REOPEN_COMMITTED;
        free_and_replace(drive->qmp_file_node_name, ctx->new_file_node_name);
        free_and_replace(drive->fdset_path, ctx->new_fdset_path);
        drive->fdset_id = ctx->new_fdset_id;
        /* Only commit the mutable bits so unrelated future flags aren't silently flipped. */
        drive->flags = (drive->flags & ~QMP_DRIVE_REPLACE_MUTABLE_MASK) |
                       (ctx->new_flags & QMP_DRIVE_REPLACE_MUTABLE_MASK);
 
        /* Trailing blockdev-del of the OLD file node. The format no longer
         * references it, so it's an orphan; deleting it also drops the dup
         * that kept the old fdset alive. */
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *del_args = NULL;
        int r = sd_json_buildo(&del_args, SD_JSON_BUILD_PAIR_STRING("node-name", ctx->old_file_node_name));
        if (r >= 0) {
                _cleanup_(replace_ctx_unrefp) ReplaceCtx *slot_ref = replace_ctx_ref(ctx);
                r = qmp_client_invoke(drive->bridge->qmp, /* ret_slot= */ NULL,
                                      "blockdev-del", QMP_CLIENT_ARGS(del_args),
                                      on_replace_old_blockdev_del_complete, slot_ref);
                if (r >= 0) {
                        TAKE_PTR(slot_ref);
                        return 0;
                }
        }
 
        /* Couldn't even queue blockdev-del. The swap succeeded; reply success
         * and leave the orphan to clean up at VM exit. */
        log_warning_errno(r, "Failed to queue blockdev-del for orphaned file node '%s': %m",
                          ctx->old_file_node_name);
 
        drive->state &= ~BLOCK_DEVICE_STATE_REPLACE_PENDING;
        _cleanup_(sd_varlink_unrefp) sd_varlink *link = TAKE_PTR(drive->link);
        if (link)
                (void) sd_varlink_reply(link, NULL);
        return 0;
}
 
int vmspawn_qmp_replace_block_device(
                VmspawnQmpBridge *bridge,
                sd_varlink *link,
                const char *id,
                int fd,
                QmpDriveFlags fd_flags) {
 
        _cleanup_close_ int owned_fd = fd;
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *file_args = NULL, *reopen_args = NULL;
        _cleanup_(replace_ctx_unrefp) ReplaceCtx *ctx = NULL;
        /* Not _cleanup_'d: aliasing it with ctx tripped a gcc-12
         * -Wuse-after-free false positive on the cleanup chain. Error paths
         * unref it explicitly; success leaks the ref to the callback. */
        ReplaceCtx *slot_ref;
        int r;
 
        assert(bridge);
        assert(link);
        assert(id);
        assert(fd >= 0);
 
        DriveInfo *drive = hashmap_get(bridge->block_devices, id);
        if (!drive)
                return sd_varlink_error(link, "io.systemd.MachineInstance.NoSuchStorage", NULL);
        if (!FLAGS_SET(drive->flags, QMP_DRIVE_REMOVABLE))
                return sd_varlink_error(link, "io.systemd.MachineInstance.StorageImmutable", NULL);
        /* QEMU's blockdev-reopen rejects RW->RO on a node with attached writers
         * (the guest device). For an RW drive the new backing must be writable. */
        if (!FLAGS_SET(drive->flags, QMP_DRIVE_READ_ONLY) && FLAGS_SET(fd_flags, QMP_DRIVE_READ_ONLY))
                return sd_varlink_error_errno(link, -EROFS);
        if (!FLAGS_SET(drive->state, BLOCK_DEVICE_STATE_BLOCKDEV_ADDED))
                return reply_qmp_error(link, "Block device add pending", -EBUSY);
        if (drive->state & (BLOCK_DEVICE_STATE_REMOVE_PENDING|BLOCK_DEVICE_STATE_REPLACE_PENDING))
                return reply_qmp_error(link, "Block device replace/remove pending", -EBUSY);
        assert(!drive->link);
        assert(drive->qmp_file_node_name);
 
        /* Bump generation EARLY so failed attempts don't collide on retry. */
        uint64_t new_gen = ++drive->file_generation;
 
        _cleanup_free_ char *new_file_node_name = NULL;
        if (asprintf(&new_file_node_name, "vmspawn-%" PRIu64 "-file-%" PRIu64,
                     drive->counter, new_gen) < 0)
                return sd_varlink_error_errno(link, -ENOMEM);
 
        /* Compute new flags: keep the existing drive flags, swap in the
         * caller-derived bits (only RO and BLOCK_DEVICE are caller-controlled),
         * fold scsi-cd into RO, and fold in the bridge's io_uring feature. */
        const QmpDriveFlags FD_DERIVED_MASK = QMP_DRIVE_READ_ONLY | QMP_DRIVE_BLOCK_DEVICE;
        QmpDriveFlags new_flags = (drive->flags & ~QMP_DRIVE_REPLACE_MUTABLE_MASK) |
                                  (fd_flags & FD_DERIVED_MASK);
        if (drive->disk_type == DISK_TYPE_VIRTIO_SCSI_CDROM)
                new_flags |= QMP_DRIVE_READ_ONLY;
        if (FLAGS_SET(bridge->features, VMSPAWN_QMP_FEATURE_IO_URING))
                new_flags |= QMP_DRIVE_IO_URING;
 
        ctx = new0(ReplaceCtx, 1);
        if (!ctx)
                return sd_varlink_error_errno(link, -ENOMEM);
        ctx->n_ref = 1;
        ctx->drive = drive_info_ref(drive);
        ctx->new_file_node_name = TAKE_PTR(new_file_node_name);
        ctx->new_flags = new_flags;
        ctx->old_file_node_name = strdup(drive->qmp_file_node_name);
        if (!ctx->old_file_node_name)
                return sd_varlink_error_errno(link, -ENOMEM);
 
        drive->link = sd_varlink_ref(link);
        drive->state |= BLOCK_DEVICE_STATE_REPLACE_PENDING;
 
        /* 1. add-fd → new fdset */
        slot_ref = replace_ctx_ref(ctx);
        r = qmp_fdset_add(bridge->qmp, TAKE_FD(owned_fd),
                          on_replace_observe_stage, slot_ref,
                          &ctx->new_fdset_path, &ctx->new_fdset_id);
        if (r < 0) {
                replace_ctx_unref(slot_ref);
                goto rollback_sync;
        }
 
        /* 2. blockdev-add (new file node, new fdset) */
        QmpFileNodeParams file_params = {
                .node_name = ctx->new_file_node_name,
                .filename  = ctx->new_fdset_path,
                .driver    = FLAGS_SET(new_flags, QMP_DRIVE_BLOCK_DEVICE) ? "host_device" : "file",
                .flags     = new_flags,
        };
        r = qmp_build_blockdev_add_file(&file_params, &file_args);
        if (r < 0)
                goto rollback_sync;
 
        slot_ref = replace_ctx_ref(ctx);
        r = qmp_client_invoke(bridge->qmp, /* ret_slot= */ NULL, "blockdev-add", QMP_CLIENT_ARGS(file_args),
                              on_replace_blockdev_add_complete, slot_ref);
        if (r < 0) {
                replace_ctx_unref(slot_ref);
                goto rollback_sync;
        }
 
        /* 3. remove-fd (new fdset; blockdev-add holds the dup) */
        slot_ref = replace_ctx_ref(ctx);
        r = qmp_fdset_remove(bridge->qmp, ctx->new_fdset_id,
                             on_replace_observe_stage, slot_ref);
        if (r < 0) {
                replace_ctx_unref(slot_ref);
                goto rollback_sync;
        }
 
        /* 4. blockdev-reopen the format node, file → new
         * NB: the option set must be a superset of every field
         * qmp_build_blockdev_add_format() may emit; otherwise reopen rejects
         * "Cannot reset option X to default" or silently flips a flag.
         * No "backing" field: only ephemeral overlays carry backing, and
         * those are never REMOVABLE. */
        r = sd_json_buildo(&reopen_args,
                        SD_JSON_BUILD_PAIR("options", SD_JSON_BUILD_ARRAY(
                                SD_JSON_BUILD_OBJECT(
                                        SD_JSON_BUILD_PAIR_STRING("node-name", drive->qmp_node_name),
                                        SD_JSON_BUILD_PAIR_STRING("driver",    drive->format),
                                        SD_JSON_BUILD_PAIR_STRING("file",      ctx->new_file_node_name),
                                        /* blockdev-reopen resets unspecified options to driver defaults,
                                         * so emit the format-agnostic options unconditionally. */
                                        SD_JSON_BUILD_PAIR_BOOLEAN("read-only", FLAGS_SET(new_flags, QMP_DRIVE_READ_ONLY)),
                                        SD_JSON_BUILD_PAIR_STRING("discard",
                                                                  FLAGS_SET(new_flags, QMP_DRIVE_DISCARD) ? "unmap" : "ignore"),
                                        /* qcow2-only option; raw rejects it as an unknown property. */
                                        SD_JSON_BUILD_PAIR_CONDITION(FLAGS_SET(new_flags, QMP_DRIVE_DISCARD_NO_UNREF),
                                                                     "discard-no-unref", SD_JSON_BUILD_BOOLEAN(true))))));
        if (r < 0)
                goto rollback_sync;
 
        slot_ref = replace_ctx_ref(ctx);
        r = qmp_client_invoke(bridge->qmp, /* ret_slot= */ NULL, "blockdev-reopen", QMP_CLIENT_ARGS(reopen_args),
                              on_replace_blockdev_reopen_complete, slot_ref);
        if (r < 0) {
                replace_ctx_unref(slot_ref);
                goto rollback_sync;
        }
 
        return 0;
 
rollback_sync:
        /* Mark failed so any in-flight callbacks observe the failure and
         * rollback their just-added state retroactively. */
        ctx->state |= REPLACE_CTX_FAILED;
        drive->state &= ~BLOCK_DEVICE_STATE_REPLACE_PENDING;
        drive->link = sd_varlink_unref(drive->link);
        return sd_varlink_error_errno(link, r);
}
 
int vmspawn_qmp_setup_network(VmspawnQmpBridge *bridge, NetworkInfo *network) {
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *netdev_args = NULL, *device_args = NULL;
        bool tap_by_fd;
        int r;
 
        assert(bridge);
 
        QmpClient *qmp = vmspawn_qmp_bridge_get_qmp(bridge);
        assert(network);
        assert(network->type);
 
        tap_by_fd = streq(network->type, "tap") && network->fd >= 0;
 
        /* For TAP-by-fd: pass the TAP fd to QEMU via getfd + SCM_RIGHTS, then reference it by name
         * in netdev_add. QEMU stores the received fd under the given fdname and closes it on removal. */
        if (tap_by_fd) {
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *getfd_args = NULL;
 
                r = sd_json_buildo(
                                &getfd_args,
                                SD_JSON_BUILD_PAIR_STRING("fdname", "vmspawn_tap"));
                if (r < 0)
                        return log_error_errno(r, "Failed to build getfd JSON: %m");
 
                r = qmp_client_invoke(qmp, /* ret_slot= */ NULL, "getfd", QMP_CLIENT_ARGS_FD(getfd_args, TAKE_FD(network->fd)),
                                      on_qmp_complete, (void*) "getfd");
                if (r < 0)
                        return log_error_errno(r, "Failed to send getfd for TAP fd: %m");
        }
 
        /* netdev_add: create the network backend */
        r = sd_json_buildo(
                        &netdev_args,
                        SD_JSON_BUILD_PAIR_STRING("type", network->type),
                        SD_JSON_BUILD_PAIR_STRING("id", "net0"),
                        SD_JSON_BUILD_PAIR_CONDITION(tap_by_fd,
                                                     "fd", JSON_BUILD_CONST_STRING("vmspawn_tap")),
                        SD_JSON_BUILD_PAIR_CONDITION(!tap_by_fd && !!network->ifname,
                                                     "ifname", SD_JSON_BUILD_STRING(network->ifname)),
                        SD_JSON_BUILD_PAIR_CONDITION(!tap_by_fd && streq(network->type, "tap"),
                                                     "script", JSON_BUILD_CONST_STRING("no")),
                        SD_JSON_BUILD_PAIR_CONDITION(!tap_by_fd && streq(network->type, "tap"),
                                                     "downscript", JSON_BUILD_CONST_STRING("no")));
        if (r < 0)
                return log_error_errno(r, "Failed to build netdev_add JSON: %m");
 
        r = qmp_client_invoke(qmp, /* ret_slot= */ NULL, "netdev_add", QMP_CLIENT_ARGS(netdev_args), on_qmp_complete, (void*) "netdev_add");
        if (r < 0)
                return log_error_errno(r, "Failed to send netdev_add: %m");
 
        /* device_add: attach NIC frontend */
        r = sd_json_buildo(
                        &device_args,
                        SD_JSON_BUILD_PAIR_STRING("driver", "virtio-net-pci"),
                        SD_JSON_BUILD_PAIR_STRING("netdev", "net0"),
                        SD_JSON_BUILD_PAIR_STRING("id", "nic0"),
                        SD_JSON_BUILD_PAIR_CONDITION(network->mac_set,
                                                     "mac", SD_JSON_BUILD_STRING(network->mac_set ? ETHER_ADDR_TO_STR(&network->mac) : NULL)),
                        SD_JSON_BUILD_PAIR_CONDITION(!!network->pcie_port,
                                                     "bus", SD_JSON_BUILD_STRING(network->pcie_port)));
        if (r < 0)
                return log_error_errno(r, "Failed to build NIC device_add JSON: %m");
 
        r = qmp_client_invoke(qmp, /* ret_slot= */ NULL, "device_add", QMP_CLIENT_ARGS(device_args), on_qmp_complete, (void*) "device_add");
        if (r < 0)
                return log_error_errno(r, "Failed to send NIC device_add: %m");
 
        log_debug("Queued %s network setup%s", network->type, tap_by_fd ? " (fd via getfd)" : "");
        return 0;
}
 
static int vmspawn_qmp_setup_one_virtiofs(QmpClient *qmp, const VirtiofsInfo *vfs) {
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *chardev_args = NULL, *device_args = NULL;
        int r;
 
        assert(qmp);
        assert(vfs);
        assert(vfs->id);
        assert(vfs->socket_path);
        assert(vfs->tag);
 
        /* chardev-add: connect to virtiofsd socket.
         * ChardevBackend and SocketAddressLegacy are QAPI legacy unions with explicit "data"
         * wrapper objects at each level — the nesting is mandatory on the wire. */
        r = sd_json_buildo(
                        &chardev_args,
                        SD_JSON_BUILD_PAIR_STRING("id", vfs->id),
                        SD_JSON_BUILD_PAIR("backend", SD_JSON_BUILD_OBJECT(
                                        SD_JSON_BUILD_PAIR_STRING("type", "socket"),
                                        SD_JSON_BUILD_PAIR("data", SD_JSON_BUILD_OBJECT(
                                                        SD_JSON_BUILD_PAIR("addr", SD_JSON_BUILD_OBJECT(
                                                                        SD_JSON_BUILD_PAIR_STRING("type", "unix"),
                                                                        SD_JSON_BUILD_PAIR("data", SD_JSON_BUILD_OBJECT(
                                                                                        SD_JSON_BUILD_PAIR_STRING("path", vfs->socket_path))))),
                                                        SD_JSON_BUILD_PAIR_BOOLEAN("server", false))))));
        if (r < 0)
                return log_error_errno(r, "Failed to build chardev-add JSON for '%s': %m", vfs->id);
 
        r = qmp_client_invoke(qmp, /* ret_slot= */ NULL, "chardev-add", QMP_CLIENT_ARGS(chardev_args), on_qmp_complete, (void*) "chardev-add");
        if (r < 0)
                return log_error_errno(r, "Failed to send chardev-add '%s': %m", vfs->id);
 
        /* device_add: create vhost-user-fs-pci device */
        r = sd_json_buildo(
                        &device_args,
                        SD_JSON_BUILD_PAIR_STRING("driver", "vhost-user-fs-pci"),
                        SD_JSON_BUILD_PAIR_STRING("id", vfs->id),
                        SD_JSON_BUILD_PAIR_STRING("chardev", vfs->id),
                        SD_JSON_BUILD_PAIR_STRING("tag", vfs->tag),
                        SD_JSON_BUILD_PAIR_UNSIGNED("queue-size", 1024),
                        SD_JSON_BUILD_PAIR_CONDITION(!!vfs->pcie_port,
                                                     "bus", SD_JSON_BUILD_STRING(vfs->pcie_port)));
        if (r < 0)
                return log_error_errno(r, "Failed to build virtiofs device_add JSON for '%s': %m", vfs->id);
 
        r = qmp_client_invoke(qmp, /* ret_slot= */ NULL, "device_add", QMP_CLIENT_ARGS(device_args), on_qmp_complete, (void*) "device_add");
        if (r < 0)
                return log_error_errno(r, "Failed to send virtiofs device_add '%s': %m", vfs->id);
 
        log_debug("Queued virtiofs device '%s' (tag=%s)", vfs->id, vfs->tag);
        return 0;
}
 
int vmspawn_qmp_setup_virtiofs(VmspawnQmpBridge *bridge, const VirtiofsInfos *virtiofs) {
        int r;
 
        assert(bridge);
 
        QmpClient *qmp = vmspawn_qmp_bridge_get_qmp(bridge);
        assert(virtiofs);
 
        FOREACH_ARRAY(e, virtiofs->entries, virtiofs->n_entries) {
                r = vmspawn_qmp_setup_one_virtiofs(qmp, e);
                if (r < 0)
                        return r;
        }
 
        return 0;
}
 
int vmspawn_qmp_setup_vsock(VmspawnQmpBridge *bridge, VsockInfo *vsock) {
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *getfd_args = NULL, *device_args = NULL;
        int r;
 
        assert(bridge);
        assert(vsock);
 
        if (vsock->fd < 0)
                return 0;
 
        QmpClient *qmp = vmspawn_qmp_bridge_get_qmp(bridge);
 
        /* getfd: pass the vhost-vsock fd to QEMU via SCM_RIGHTS */
        r = sd_json_buildo(
                        &getfd_args,
                        SD_JSON_BUILD_PAIR_STRING("fdname", "vmspawn_vsock"));
        if (r < 0)
                return log_error_errno(r, "Failed to build getfd JSON for VSOCK: %m");
 
        r = qmp_client_invoke(qmp, /* ret_slot= */ NULL, "getfd", QMP_CLIENT_ARGS_FD(getfd_args, TAKE_FD(vsock->fd)),
                              on_qmp_complete, (void*) "getfd");
        if (r < 0)
                return log_error_errno(r, "Failed to send getfd for VSOCK fd: %m");
 
        /* device_add: create vhost-vsock-pci device referencing the named fd */
        r = sd_json_buildo(
                        &device_args,
                        SD_JSON_BUILD_PAIR_STRING("driver", "vhost-vsock-pci"),
                        SD_JSON_BUILD_PAIR_STRING("id", "vsock0"),
                        SD_JSON_BUILD_PAIR_UNSIGNED("guest-cid", vsock->cid),
                        SD_JSON_BUILD_PAIR_STRING("vhostfd", "vmspawn_vsock"),
                        SD_JSON_BUILD_PAIR_CONDITION(!!vsock->pcie_port,
                                                     "bus", SD_JSON_BUILD_STRING(vsock->pcie_port)));
        if (r < 0)
                return log_error_errno(r, "Failed to build VSOCK device_add JSON: %m");
 
        r = qmp_client_invoke(qmp, /* ret_slot= */ NULL, "device_add", QMP_CLIENT_ARGS(device_args), on_qmp_complete, (void*) "device_add");
        if (r < 0)
                return log_error_errno(r, "Failed to send VSOCK device_add: %m");
 
        log_debug("Queued vhost-vsock-pci device setup (cid=%u)", vsock->cid);
        return 0;
}
 
int vmspawn_qmp_setup_drives(VmspawnQmpBridge *bridge, DriveInfos *drives) {
        int r;
 
        assert(bridge);
        assert(drives);
 
        QmpClient *qmp = vmspawn_qmp_bridge_get_qmp(bridge);
 
        /* io_uring support was probed during vmspawn_qmp_init(). The cached result in
         * bridge->features is passed to each file node setup call. SCSI controller
         * creation is handled on-demand by vmspawn_qmp_add_block_device() for the first
         * SCSI drive, using the hotplug-spares pool. */
 
        FOREACH_ARRAY(d, drives->drives, drives->n_drives) {
                if ((*d)->overlay_fd >= 0)
                        r = qmp_setup_ephemeral_drive(bridge, qmp, *d);
                else
                        r = qmp_setup_regular_drive(bridge, TAKE_PTR(*d));
                if (r < 0)
                        return r;
        }
 
        return 0;
}
 
VmspawnQmpBridge* vmspawn_qmp_bridge_free(VmspawnQmpBridge *b) {
        if (!b)
                return NULL;
 
        /* Unref first: pending QMP callbacks may release hotplug ports through the bridge. */
        qmp_client_unref(b->qmp);
 
        hashmap_free(b->block_devices_by_qmp_id);
        hashmap_free(b->block_devices);
        hashmap_free(b->pending_jobs);
 
        FOREACH_ELEMENT(owner, b->hotplug_port_owner)
                free(*owner);
 
        return mfree(b);
}
 
QmpClient* vmspawn_qmp_bridge_get_qmp(VmspawnQmpBridge *b) {
        assert(b);
        return b->qmp;
}
 
/* Probe-reply convention: ignore -EIO (QMP rejection = "feature absent", log at debug
 * and leave the feature flag clear) and transport errors (caught by the post-loop
 * qmp_client_is_disconnected() check in vmspawn_qmp_probe_features()). Cleanup calls
 * are best-effort — failing to delete a private probe node leaves a harmless /dev/null
 * blockdev in QEMU until it exits. */
 
/* QEMU node names must start with a letter. */
#define IO_URING_PROBE_NODE_NAME "vmspawn-io-uring-probe"
 
static int on_io_uring_probe_del_reply(
                QmpClient *c,
                sd_json_variant *result,
                const char *error_desc,
                int error,
                void *userdata) {
 
        assert(c);
 
        if (error_desc)
                log_debug("Failed to remove io_uring probe node: %s", error_desc);
        return 0;
}
 
static int on_io_uring_probe_add_reply(
                QmpClient *c,
                sd_json_variant *result,
                const char *error_desc,
                int error,
                void *userdata) {
 
        VmspawnQmpBridge *bridge = ASSERT_PTR(userdata);
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *del_args = NULL;
        int r;
 
        assert(c);
 
        if (error < 0 && !error_desc)
                return log_debug_errno(error, "io_uring probe did not execute: %m");
        if (error_desc) {
                log_debug("QEMU does not support aio=io_uring: %s", error_desc);
                return 0;
        }
 
        bridge->features |= VMSPAWN_QMP_FEATURE_IO_URING;
        log_debug("QEMU supports aio=io_uring");
 
        /* Best-effort cleanup; the chained reply keeps the pump busy via the slots set. */
        r = sd_json_buildo(&del_args,
                        SD_JSON_BUILD_PAIR_STRING("node-name", IO_URING_PROBE_NODE_NAME));
        if (r < 0)
                return r;
 
        return qmp_client_invoke(c, /* ret_slot= */ NULL, "blockdev-del", QMP_CLIENT_ARGS(del_args),
                        on_io_uring_probe_del_reply, bridge);
}
 
static int probe_io_uring(QmpClient *c, VmspawnQmpBridge *bridge) {
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *args = NULL;
        int r;
 
        assert(c);
        assert(bridge);
 
        r = sd_json_buildo(
                        &args,
                        SD_JSON_BUILD_PAIR_STRING("node-name", IO_URING_PROBE_NODE_NAME),
                        SD_JSON_BUILD_PAIR_STRING("driver", "file"),
                        SD_JSON_BUILD_PAIR_STRING("filename", "/dev/null"),
                        SD_JSON_BUILD_PAIR_BOOLEAN("read-only", true),
                        SD_JSON_BUILD_PAIR_STRING("aio", "io_uring"));
        if (r < 0)
                return r;
 
        return qmp_client_invoke(c, /* ret_slot= */ NULL, "blockdev-add", QMP_CLIENT_ARGS(args),
                        on_io_uring_probe_add_reply, bridge);
}
 
static int on_probe_schema_reply(
                QmpClient *c,
                sd_json_variant *result,
                const char *error_desc,
                int error,
                void *userdata) {
 
        VmspawnQmpBridge *bridge = ASSERT_PTR(userdata);
 
        assert(c);
 
        if (error < 0 && !error_desc)
                return log_debug_errno(error, "query-qmp-schema probe did not execute: %m");
        if (error_desc) {
                log_debug("query-qmp-schema rejected: %s", error_desc);
                return 0;
        }
 
        if (qmp_schema_has_member(result, "discard-no-unref")) {
                bridge->features |= VMSPAWN_QMP_FEATURE_DISCARD_NO_UNREF;
                log_debug("QEMU supports qcow2 discard-no-unref");
        } else
                log_debug("QEMU does not support qcow2 discard-no-unref");
 
        return 0;
}
 
static int probe_schema(QmpClient *c, VmspawnQmpBridge *bridge) {
        assert(c);
        assert(bridge);
 
        return qmp_client_invoke(c, /* ret_slot= */ NULL, "query-qmp-schema", QMP_CLIENT_ARGS(NULL),
                        on_probe_schema_reply, bridge);
}
 
static int on_job_dismiss_complete(
                QmpClient *client,
                sd_json_variant *result,
                const char *error_desc,
                int error,
                void *userdata) {
 
        if (error < 0)
                log_debug_errno(error, "job-dismiss failed: %s", strna(error_desc));
 
        return 0;
}
 
static int dispatch_pending_job(VmspawnQmpBridge *bridge, sd_json_variant *data) {
        const char *job_id, *status;
        int r;
 
        assert(bridge);
 
        if (!data)
                return 0;
 
        job_id = sd_json_variant_string(sd_json_variant_by_key(data, "id"));
        status = sd_json_variant_string(sd_json_variant_by_key(data, "status"));
 
        if (!job_id || !streq_ptr(status, "concluded"))
                return 0;
 
        _cleanup_free_ char *key = NULL;
        _cleanup_(pending_job_freep) PendingJob *job = hashmap_remove2(bridge->pending_jobs, job_id, (void**) &key);
        if (!job)
                return 0;
 
        log_debug("QMP job '%s' concluded, firing continuation", job_id);
 
        /* Dismiss the concluded job before running the continuation */
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *dismiss_args = NULL;
        r = sd_json_buildo(&dismiss_args, SD_JSON_BUILD_PAIR_STRING("id", job_id));
        if (r < 0) {
                log_error_errno(r, "Failed to build job-dismiss arguments for '%s': %m", job_id);
                return sd_event_exit(qmp_client_get_event(bridge->qmp), r);
        }
 
        r = qmp_client_invoke(bridge->qmp, /* ret_slot= */ NULL, "job-dismiss", QMP_CLIENT_ARGS(dismiss_args),
                              on_job_dismiss_complete, /* userdata= */ NULL);
        if (r < 0) {
                log_error_errno(r, "Failed to send job-dismiss for '%s': %m", job_id);
                return sd_event_exit(qmp_client_get_event(bridge->qmp), r);
        }
 
        if (!job->on_concluded)
                return 1;
 
        r = job->on_concluded(bridge->qmp, TAKE_PTR(job->userdata));
        if (r < 0) {
                log_error_errno(r, "Job continuation failed: %m");
                return sd_event_exit(qmp_client_get_event(bridge->qmp), r);
        }
 
        return 1;
}
 
static int on_qmp_event(
                QmpClient *client,
                const char *event,
                sd_json_variant *data,
                void *userdata) {
 
        VmspawnQmpBridge *bridge = ASSERT_PTR(userdata);
 
        assert(client);
        assert(event);
 
        /* Dispatch job status changes to pending continuations (e.g. blockdev-create) */
        if (streq(event, "JOB_STATUS_CHANGE"))
                return dispatch_pending_job(bridge, data);
 
        /* Notification still fans out below. */
        if (streq(event, "DEVICE_DELETED"))
                (void) vmspawn_qmp_dispatch_device_deleted(bridge, data);
 
        if (!bridge->event_callback)
                return 0;
 
        return bridge->event_callback(client, event, data, bridge->event_userdata);
}
 
void vmspawn_qmp_bridge_bind_event(VmspawnQmpBridge *b, qmp_event_callback_t callback, void *userdata) {
        assert(b);
 
        b->event_callback = callback;
        b->event_userdata = userdata;
}
 
int vmspawn_qmp_init(VmspawnQmpBridge **ret, int fd, sd_event *event) {
        _cleanup_(vmspawn_qmp_bridge_freep) VmspawnQmpBridge *bridge = NULL;
        _cleanup_close_ int fd_close = ASSERT_FD(TAKE_FD(fd));
        int r;
 
        assert(ret);
        assert(event);
 
        /* This function always takes ownership of the passed fd. */
 
        bridge = new0(VmspawnQmpBridge, 1);
        if (!bridge)
                return log_oom();
 
        bridge->scsi_controller_port_idx = -1;
 
        r = qmp_client_connect_fd(&bridge->qmp, TAKE_FD(fd_close));
        if (r < 0)
                return log_error_errno(r, "Failed to create QMP client: %m");
 
        r = qmp_client_set_description(bridge->qmp, "vmspawn-qmp-client");
        if (r < 0)
                return log_error_errno(r, "Failed to set QMP client description: %m");
 
        /* Command callbacks find the bridge through the client's userdata. */