From nobody Sat Nov 23 18:11:13 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass(p=reject dis=none) header.from=lists.libvirt.org ARC-Seal: i=1; a=rsa-sha256; t=1723160683; cv=none; d=zohomail.com; s=zohoarc; b=Nv7iUt42ILR+AK6irr+3AIxQJS062+m46PNd+YysHJrZAq0RNu9eGO+4eYCkNsRjlkqnbJyPYKOv+76/CGIEfckYIhb3IWzr37wZvaHgeQzixHdCnb0kcj9APfLLGIzqRiCh0uMrsfWD3DLIOf+I036Em4omJgOlH1n5wQfcSac= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1723160683; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Subject:Subject:To:To:Message-Id; bh=DTEfP8tsJvPX+qcIBcXwIcuXCtfbtE7Jg3m26+f3Uqc=; b=maMX6AcgMq85pwFiZhZR9kpmaDEjCk3oTyepm9j1UKXiaaRdQC62fLFtjs9lqt601AT5GDqeHGKCN2mt2tkkPrcMGcfBX7oaCkuvR/jSqdWuj1lujzwuZ1qZ2P7GVjouD61nnKEo/BfjN2wc5nEmdtu4WF3gIRSUZKJDcfLk6dI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1723160683692697.1644213138644; Thu, 8 Aug 2024 16:44:43 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 9E97413D3; Thu, 8 Aug 2024 19:44:42 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 62FA41527; Thu, 8 Aug 2024 19:39:10 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id E793B1475; Thu, 8 Aug 2024 19:39:06 -0400 (EDT) Received: from mail-wr1-f50.google.com (mail-wr1-f50.google.com [209.85.221.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 274C5147A for ; Thu, 8 Aug 2024 19:38:40 -0400 (EDT) Received: by mail-wr1-f50.google.com with SMTP id ffacd0b85a97d-368313809a4so1510649f8f.0 for ; Thu, 08 Aug 2024 16:38:40 -0700 (PDT) Received: from localhost ([69.51.98.127]) by smtp.gmail.com with ESMTPSA id e9e14a558f8ab-39b20aaaf79sm58802025ab.26.2024.08.08.16.38.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Aug 2024 16:38:38 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1723160319; x=1723765119; darn=lists.libvirt.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=pA2jBZ87jEYD7xXSq+W9qWaRlk4msPPdDhhMbzcmmdU=; b=L3TAlnTnTuOvBYrWwRFovjRv0OJWtd0pom6nFkw/JNizwnSBkFlsXKkZSM3zF7WdsN rYLmx/6LmokBSaKN66txUVJvmCgn2f3r+A51pC3oARlNrUrgHNot55i3TlifFLGC9IwX eZHWU4HgyjXpzCVLyZ04/J+13agcAK4ZBVxBDwO9SotQYAUY2BVdaYnpF1rDxvXzcLJW Lc4dODSGyb3eYEZ2ojuFBk/HSdAtlkXC488htqj4gkogFkeYCo/8AXElxvZ6oFGEE6qU 7R5isH1Zcx28hPYmlKuWpDExIE3/IY+bjaiL24Yvhf69A9DDiIfdp+w6D+9s8KpeAdw2 G9bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723160319; x=1723765119; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=pA2jBZ87jEYD7xXSq+W9qWaRlk4msPPdDhhMbzcmmdU=; b=H7Cq5xmM7gGQ2XRUxc8bA5xNbje8Gn0zI1fbKo6SZ2VAGemr4HgYmnfg65BU5c7Y3m l3jH/o5crDh1bpZTa71yDBAHaoVEAw/JWUN1EQ5msuN9BBcoqOZubExJ80kdy+mZPibi DWTDVzgyEw5NVPWqP9zah7LHYPV/WWJTkMgJMT2ibWUstqAwrxLgyLgrdNgxOoSvHhzC tef2NEarE8qzdLG4htHnfbSVhsC+DfJRmu1YuYsDgNRpQnDpswViCvtbnSe68Xbfr8Q2 2RXDEiQWMLDGhnPGiheNCysY+ICaTyqDKxPcVlpA+tBSSMMh0sG12euhZ/3V5B31RsCU 0dew== X-Gm-Message-State: AOJu0YycMYRGvKCjUwPSDdO6kFW9loVr94crakN4Cq9igIFfUzjHvVmY OdbVHI1AL2Vr5/UFx7lahnwk1BMuT3wYze1gOgSlXv5Gfc5KMPNcsZE5rGuWII3sdsP+F3iHSQH r X-Google-Smtp-Source: AGHT+IEc6wWs05/3/gUFXAIvqilOQKtuxCNd/2srTD9lTzpYVbVeq95awAI2Y2RSdRWwJ6H5qMDizw== X-Received: by 2002:a5d:588a:0:b0:36b:b174:5256 with SMTP id ffacd0b85a97d-36d280bdb20mr3483639f8f.6.1723160318877; Thu, 08 Aug 2024 16:38:38 -0700 (PDT) To: devel@lists.libvirt.org Subject: [PATCH 11/20] qemu: Decompose qemuSaveImageOpen Date: Thu, 8 Aug 2024 17:38:04 -0600 Message-Id: <20240808233813.22905-12-jfehlig@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20240808233813.22905-1-jfehlig@suse.com> References: <20240808233813.22905-1-jfehlig@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Message-ID-Hash: WYZMIVCYKJIOJXOKZSODELIGXIDMJRGH X-Message-ID-Hash: WYZMIVCYKJIOJXOKZSODELIGXIDMJRGH X-MailFrom: jfehlig@suse.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header CC: farosas@suse.de X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: From: Jim Fehlig via Devel Reply-To: Jim Fehlig X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1723160685825116600 Content-Type: text/plain; charset="utf-8" Split the reading of libvirt's save image metadata from the opening of the fd that will be passed to QEMU. This provides flexibility for an upcoming patch adding mapped-ram support for restore. Signed-off-by: Jim Fehlig --- src/qemu/qemu_driver.c | 37 ++++++++-------- src/qemu/qemu_saveimage.c | 89 ++++++++++++++++++++++++--------------- src/qemu/qemu_saveimage.h | 16 ++++--- src/qemu/qemu_snapshot.c | 9 ++-- 4 files changed, 89 insertions(+), 62 deletions(-) diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 977763e5d8..87d75b6baa 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -5783,9 +5783,12 @@ qemuDomainRestoreInternal(virConnectPtr conn, if (flags & VIR_DOMAIN_SAVE_RESET_NVRAM) reset_nvram =3D true; =20 - fd =3D qemuSaveImageOpen(driver, NULL, path, &def, &data, + if (qemuSaveImageGetMetadata(driver, NULL, path, &def, &data, false) <= 0) + goto cleanup; + + fd =3D qemuSaveImageOpen(driver, path, (flags & VIR_DOMAIN_SAVE_BYPASS_CACHE) !=3D 0, - &wrapperFd, false, false); + &wrapperFd, false); if (fd < 0) goto cleanup; =20 @@ -5914,15 +5917,11 @@ qemuDomainSaveImageGetXMLDesc(virConnectPtr conn, c= onst char *path, virQEMUDriver *driver =3D conn->privateData; char *ret =3D NULL; g_autoptr(virDomainDef) def =3D NULL; - int fd =3D -1; virQEMUSaveData *data =3D NULL; =20 virCheckFlags(VIR_DOMAIN_SAVE_IMAGE_XML_SECURE, NULL); =20 - fd =3D qemuSaveImageOpen(driver, NULL, path, &def, &data, - false, NULL, false, false); - - if (fd < 0) + if (qemuSaveImageGetMetadata(driver, NULL, path, &def, &data, false) <= 0) goto cleanup; =20 if (virDomainSaveImageGetXMLDescEnsureACL(conn, def) < 0) @@ -5932,7 +5931,6 @@ qemuDomainSaveImageGetXMLDesc(virConnectPtr conn, con= st char *path, =20 cleanup: virQEMUSaveDataFree(data); - VIR_FORCE_CLOSE(fd); return ret; } =20 @@ -5956,8 +5954,10 @@ qemuDomainSaveImageDefineXML(virConnectPtr conn, con= st char *path, else if (flags & VIR_DOMAIN_SAVE_PAUSED) state =3D 0; =20 - fd =3D qemuSaveImageOpen(driver, NULL, path, &def, &data, - false, NULL, true, false); + if (qemuSaveImageGetMetadata(driver, NULL, path, &def, &data, false) <= 0) + goto cleanup; + + fd =3D qemuSaveImageOpen(driver, path, 0, NULL, false); =20 if (fd < 0) goto cleanup; @@ -6015,7 +6015,6 @@ qemuDomainManagedSaveGetXMLDesc(virDomainPtr dom, uns= igned int flags) g_autofree char *path =3D NULL; char *ret =3D NULL; g_autoptr(virDomainDef) def =3D NULL; - int fd =3D -1; virQEMUSaveData *data =3D NULL; qemuDomainObjPrivate *priv; =20 @@ -6037,15 +6036,14 @@ qemuDomainManagedSaveGetXMLDesc(virDomainPtr dom, u= nsigned int flags) goto cleanup; } =20 - if ((fd =3D qemuSaveImageOpen(driver, priv->qemuCaps, path, &def, &dat= a, - false, NULL, false, false)) < 0) + if (qemuSaveImageGetMetadata(driver, priv->qemuCaps, path, + &def, &data, false) < 0) goto cleanup; =20 ret =3D qemuDomainDefFormatXML(driver, priv->qemuCaps, def, flags); =20 cleanup: virQEMUSaveDataFree(data); - VIR_FORCE_CLOSE(fd); virDomainObjEndAPI(&vm); return ret; } @@ -6101,14 +6099,17 @@ qemuDomainObjRestore(virConnectPtr conn, virQEMUSaveData *data =3D NULL; virFileWrapperFd *wrapperFd =3D NULL; =20 - fd =3D qemuSaveImageOpen(driver, NULL, path, &def, &data, - bypass_cache, &wrapperFd, false, true); - if (fd < 0) { - if (fd =3D=3D -3) + ret =3D qemuSaveImageGetMetadata(driver, NULL, path, &def, &data, true= ); + if (ret < 0) { + if (ret =3D=3D -3) ret =3D 1; goto cleanup; } =20 + fd =3D qemuSaveImageOpen(driver, path, bypass_cache, &wrapperFd, false= ); + if (fd < 0) + goto cleanup; + if (virHookPresent(VIR_HOOK_DRIVER_QEMU)) { int hookret; =20 diff --git a/src/qemu/qemu_saveimage.c b/src/qemu/qemu_saveimage.c index 98a1ad638d..125064ab66 100644 --- a/src/qemu/qemu_saveimage.c +++ b/src/qemu/qemu_saveimage.c @@ -570,58 +570,35 @@ qemuSaveImageGetCompressionProgram(const char *imageF= ormat, =20 =20 /** - * qemuSaveImageOpen: + * qemuSaveImageGetMetadata: * @driver: qemu driver data * @qemuCaps: pointer to qemuCaps if the domain is running or NULL * @path: path of the save image * @ret_def: returns domain definition created from the XML stored in the = image * @ret_data: returns structure filled with data from the image header - * @bypass_cache: bypass cache when opening the file - * @wrapperFd: returns the file wrapper structure - * @open_write: open the file for writing (for updates) * @unlink_corrupt: remove the image file if it is corrupted * - * Returns the opened fd of the save image file and fills the appropriate = fields - * on success. On error returns -1 on most failures, -3 if corrupt image w= as - * unlinked (no error raised). + * Open the save image file, read libvirt's save image metadata, and popul= ate + * the @ret_def and @ret_data structures. Returns 0 on success and -1 on m= ost + * failures. Returns -3 if corrupt image was unlinked (no error raised). */ int -qemuSaveImageOpen(virQEMUDriver *driver, - virQEMUCaps *qemuCaps, - const char *path, - virDomainDef **ret_def, - virQEMUSaveData **ret_data, - bool bypass_cache, - virFileWrapperFd **wrapperFd, - bool open_write, - bool unlink_corrupt) +qemuSaveImageGetMetadata(virQEMUDriver *driver, + virQEMUCaps *qemuCaps, + const char *path, + virDomainDef **ret_def, + virQEMUSaveData **ret_data, + bool unlink_corrupt) { g_autoptr(virQEMUDriverConfig) cfg =3D virQEMUDriverGetConfig(driver); VIR_AUTOCLOSE fd =3D -1; - int ret =3D -1; g_autoptr(virQEMUSaveData) data =3D NULL; virQEMUSaveHeader *header; g_autoptr(virDomainDef) def =3D NULL; - int oflags =3D open_write ? O_RDWR : O_RDONLY; size_t xml_len; size_t cookie_len; =20 - if (bypass_cache) { - int directFlag =3D virFileDirectFdFlag(); - if (directFlag < 0) { - virReportError(VIR_ERR_OPERATION_FAILED, "%s", - _("bypass cache unsupported by this system")); - return -1; - } - oflags |=3D directFlag; - } - - if ((fd =3D qemuDomainOpenFile(cfg, NULL, path, oflags, NULL)) < 0) - return -1; - - if (bypass_cache && - !(*wrapperFd =3D virFileWrapperFdNew(&fd, path, - VIR_FILE_WRAPPER_BYPASS_CACHE))) + if ((fd =3D qemuDomainOpenFile(cfg, NULL, path, O_RDONLY, NULL)) < 0) return -1; =20 data =3D g_new0(virQEMUSaveData, 1); @@ -725,6 +702,50 @@ qemuSaveImageOpen(virQEMUDriver *driver, *ret_def =3D g_steal_pointer(&def); *ret_data =3D g_steal_pointer(&data); =20 + return 0; +} + + +/** + * qemuSaveImageOpen: + * @driver: qemu driver data + * @path: path of the save image + * @bypass_cache: bypass cache when opening the file + * @wrapperFd: returns the file wrapper structure + * @open_write: open the file for writing (for updates) + * + * Returns the opened fd of the save image file on success, -1 on failure. + */ +int +qemuSaveImageOpen(virQEMUDriver *driver, + const char *path, + bool bypass_cache, + virFileWrapperFd **wrapperFd, + bool open_write) +{ + g_autoptr(virQEMUDriverConfig) cfg =3D virQEMUDriverGetConfig(driver); + VIR_AUTOCLOSE fd =3D -1; + int ret =3D -1; + int oflags =3D open_write ? O_RDWR : O_RDONLY; + + if (bypass_cache) { + int directFlag =3D virFileDirectFdFlag(); + if (directFlag < 0) { + virReportError(VIR_ERR_OPERATION_FAILED, "%s", + _("bypass cache unsupported by this system")); + return -1; + } + oflags |=3D directFlag; + } + + if ((fd =3D qemuDomainOpenFile(cfg, NULL, path, oflags, NULL)) < 0) + return -1; + + if (bypass_cache && + !(*wrapperFd =3D virFileWrapperFdNew(&fd, path, + VIR_FILE_WRAPPER_BYPASS_CACHE))) + return -1; + ret =3D fd; fd =3D -1; =20 diff --git a/src/qemu/qemu_saveimage.h b/src/qemu/qemu_saveimage.h index eb2835f11b..e101fdba6e 100644 --- a/src/qemu/qemu_saveimage.h +++ b/src/qemu/qemu_saveimage.h @@ -74,17 +74,21 @@ qemuSaveImageStartVM(virConnectPtr conn, virDomainAsyncJob asyncJob) ATTRIBUTE_NONNULL(4) ATTRIBUTE_NONNULL(5) ATTRIBUTE_NONNULL(6); =20 +int +qemuSaveImageGetMetadata(virQEMUDriver *driver, + virQEMUCaps *qemuCaps, + const char *path, + virDomainDef **ret_def, + virQEMUSaveData **ret_data, + bool unlink_corrupt) + ATTRIBUTE_NONNULL(4) ATTRIBUTE_NONNULL(5); + int qemuSaveImageOpen(virQEMUDriver *driver, - virQEMUCaps *qemuCaps, const char *path, - virDomainDef **ret_def, - virQEMUSaveData **ret_data, bool bypass_cache, virFileWrapperFd **wrapperFd, - bool open_write, - bool unlink_corrupt) - ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4); + bool open_write); =20 int qemuSaveImageGetCompressionProgram(const char *imageFormat, diff --git a/src/qemu/qemu_snapshot.c b/src/qemu/qemu_snapshot.c index 9833fb6206..7d05ce76f4 100644 --- a/src/qemu/qemu_snapshot.c +++ b/src/qemu/qemu_snapshot.c @@ -2118,11 +2118,12 @@ qemuSnapshotRevertExternalPrepare(virDomainObj *vm, g_autoptr(virDomainDef) savedef =3D NULL; =20 memdata->path =3D snapdef->memorysnapshotfile; - memdata->fd =3D qemuSaveImageOpen(driver, NULL, memdata->path, - &savedef, &memdata->data, - false, NULL, - false, false); + if (qemuSaveImageGetMetadata(driver, NULL, memdata->path, &savedef, + &memdata->data, false) < 0) + return -1; =20 + memdata->fd =3D qemuSaveImageOpen(driver, memdata->path, + false, NULL, false); if (memdata->fd < 0) return -1; =20 --=20 2.35.3