From nobody Mon Feb 9 05:01:48 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1496824773060832.5198712414827; Wed, 7 Jun 2017 01:39:33 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 6E55BC0587DD; Wed, 7 Jun 2017 08:39:30 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 31DDF18210; Wed, 7 Jun 2017 08:39:30 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 9A1F81869FF4; Wed, 7 Jun 2017 08:38:36 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v578brVF004885 for ; Wed, 7 Jun 2017 04:37:53 -0400 Received: by smtp.corp.redhat.com (Postfix) id 5530D1837A; Wed, 7 Jun 2017 08:37:53 +0000 (UTC) Received: from virval.usersys.redhat.com (dhcp129-92.brq.redhat.com [10.34.129.92]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 04A7F183E1 for ; Wed, 7 Jun 2017 08:37:51 +0000 (UTC) Received: by virval.usersys.redhat.com (Postfix, from userid 500) id 7CEEB101934; Wed, 7 Jun 2017 10:37:46 +0200 (CEST) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 6E55BC0587DD Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 6E55BC0587DD From: Jiri Denemark To: libvir-list@redhat.com Date: Wed, 7 Jun 2017 10:37:34 +0200 Message-Id: <833dfe9c3e1822a876382f0b4454008b3b8b638a.1496824604.git.jdenemar@redhat.com> In-Reply-To: References: In-Reply-To: References: Mail-Followup-To: libvir-list@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v2 09/20] qemu: Refactor qemuDomainSaveHeader X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Wed, 07 Jun 2017 08:39:31 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" The function is now called virQEMUSaveDataWrite and it is now doing everything it needs to save both the save image header and domain XML to a file. Be it a new file or an existing file in which a user wants to change the domain XML. Signed-off-by: Jiri Denemark --- Notes: Version 2: - new patch (separated from the original 06/15) - VIR_EXPAND_N is no longer used to pad domain XML with zeros src/qemu/qemu_driver.c | 98 ++++++++++++++++++++++++++++++----------------= ---- 1 file changed, 60 insertions(+), 38 deletions(-) diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index b51a41641..5f5215b3d 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -2832,7 +2832,7 @@ virQEMUSaveDataFree(virQEMUSaveHeaderPtr header) =20 =20 static virQEMUSaveHeaderPtr -virQEMUSaveDataNew(char *domXML, +virQEMUSaveDataNew(char *domXML ATTRIBUTE_UNUSED, bool running, int compressed) { @@ -2845,34 +2845,66 @@ virQEMUSaveDataNew(char *domXML, header->version =3D QEMU_SAVE_VERSION; header->was_running =3D running ? 1 : 0; header->compressed =3D compressed; - header->data_len =3D strlen(domXML) + 1; =20 return header; } =20 =20 -/* return -errno on failure, or 0 on success */ +/* virQEMUSaveDataWrite: + * + * Writes libvirt's header (including domain XML) into a saved image of a + * running domain. If @header has data_len filled in (because it was previ= ously + * read from the file), the function will make sure the new data will fit + * within data_len. + * + * Returns -1 on failure, or 0 on success. + */ static int -qemuDomainSaveHeader(int fd, const char *path, const char *xml, - virQEMUSaveHeaderPtr header) +virQEMUSaveDataWrite(virQEMUSaveHeaderPtr header, + const char *xml, + int fd, + const char *path) { - int ret =3D 0; + size_t len; + int ret =3D -1; + size_t zerosLen =3D 0; + char *zeros =3D NULL; + + len =3D strlen(xml) + 1; + + if (header->data_len > 0) { + if (len > header->data_len) { + virReportError(VIR_ERR_OPERATION_FAILED, "%s", + _("new xml too large to fit in file")); + goto cleanup; + } + + zerosLen =3D header->data_len - len; + if (VIR_ALLOC_N(zeros, zerosLen) < 0) + goto cleanup; + } else { + header->data_len =3D len; + } =20 if (safewrite(fd, header, sizeof(*header)) !=3D sizeof(*header)) { - ret =3D -errno; - virReportError(VIR_ERR_OPERATION_FAILED, - _("failed to write header to domain save file '%s'"= ), - path); - goto endjob; + virReportSystemError(errno, + _("failed to write header to domain save file= '%s'"), + path); + goto cleanup; } =20 - if (safewrite(fd, xml, header->data_len) !=3D header->data_len) { - ret =3D -errno; - virReportError(VIR_ERR_OPERATION_FAILED, - _("failed to write xml to '%s'"), path); - goto endjob; + if (safewrite(fd, xml, header->data_len) !=3D header->data_len || + safewrite(fd, zeros, zerosLen) !=3D zerosLen) { + virReportSystemError(errno, + _("failed to write domain xml to '%s'"), + path); + goto cleanup; } - endjob: + + ret =3D 0; + + cleanup: + VIR_FREE(zeros); return ret; } =20 @@ -3146,8 +3178,7 @@ qemuDomainSaveMemory(virQEMUDriverPtr driver, if (!(wrapperFd =3D virFileWrapperFdNew(&fd, path, wrapperFlags))) goto cleanup; =20 - /* Write header to file, followed by XML */ - if (qemuDomainSaveHeader(fd, path, domXML, header) < 0) + if (virQEMUSaveDataWrite(header, domXML, fd, path) < 0) goto cleanup; =20 /* Perform the migration */ @@ -6626,7 +6657,6 @@ qemuDomainSaveImageDefineXML(virConnectPtr conn, cons= t char *path, virDomainDefPtr newdef =3D NULL; int fd =3D -1; char *xml =3D NULL; - size_t len; virQEMUSaveHeaderPtr header =3D NULL; int state =3D -1; =20 @@ -6662,30 +6692,22 @@ qemuDomainSaveImageDefineXML(virConnectPtr conn, co= nst char *path, =20 VIR_FREE(xml); =20 - xml =3D qemuDomainDefFormatXML(driver, newdef, - VIR_DOMAIN_XML_INACTIVE | - VIR_DOMAIN_XML_SECURE | - VIR_DOMAIN_XML_MIGRATABLE); - if (!xml) - goto cleanup; - len =3D strlen(xml) + 1; - - if (len > header->data_len) { - virReportError(VIR_ERR_OPERATION_FAILED, "%s", - _("new xml too large to fit in file")); - goto cleanup; - } - if (VIR_EXPAND_N(xml, len, header->data_len - len) < 0) + if (!(xml =3D qemuDomainDefFormatXML(driver, newdef, + VIR_DOMAIN_XML_INACTIVE | + VIR_DOMAIN_XML_SECURE | + VIR_DOMAIN_XML_MIGRATABLE))) goto cleanup; =20 if (lseek(fd, 0, SEEK_SET) !=3D 0) { virReportSystemError(errno, _("cannot seek in '%s'"), path); goto cleanup; } - if (safewrite(fd, &header, sizeof(header)) !=3D sizeof(header) || - safewrite(fd, xml, len) !=3D len || - VIR_CLOSE(fd) < 0) { - virReportSystemError(errno, _("failed to write xml to '%s'"), path= ); + + if (virQEMUSaveDataWrite(header, xml, fd, path) < 0) + goto cleanup; + + if (VIR_CLOSE(fd) < 0) { + virReportSystemError(errno, _("failed to write header data to '%s'= "), path); goto cleanup; } =20 --=20 2.13.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list