From nobody Thu Nov 6 08:30:59 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; dkim=fail spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1497524106588258.9917807610108; Thu, 15 Jun 2017 03:55:06 -0700 (PDT) Received: from localhost ([::1]:53131 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dLSQa-0000xs-CK for importer@patchew.org; Thu, 15 Jun 2017 06:55:04 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40641) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dLSOl-00082o-Ia for qemu-devel@nongnu.org; Thu, 15 Jun 2017 06:53:12 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dLSOj-0007Pk-Me for qemu-devel@nongnu.org; Thu, 15 Jun 2017 06:53:11 -0400 Received: from mail-wr0-x244.google.com ([2a00:1450:400c:c0c::244]:34289) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dLSOj-0007PF-Di for qemu-devel@nongnu.org; Thu, 15 Jun 2017 06:53:09 -0400 Received: by mail-wr0-x244.google.com with SMTP id y25so544152wrd.1 for ; Thu, 15 Jun 2017 03:53:09 -0700 (PDT) Received: from 640k.lan (94-39-191-51.adsl-ull.clienti.tiscali.it. [94.39.191.51]) by smtp.gmail.com with ESMTPSA id f21sm3258597wra.5.2017.06.15.03.53.06 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 15 Jun 2017 03:53:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=mXRXZ6VDxd5rXK3mGfBK8ZozFtEP3pWk7IcPLjdXhBE=; b=RrZ+ZpZidO8OyfuBB67yJG8mnltPeBquOksD2QIIkkLsUK4YIkntLslE5O3KYVZ33n Gqv2vA4pHzStx8UUWWlMwSYmAsi+QutOo771UIN7mhWfGHNIvMtDzqZE/QHNL13DVIwq 1uGX1kyukdm3laKrAHTwnXJpiVvvnAIIAzxxAr19elElPOTQVrFfAevxier2POXQSDCU nipVJ8z6HNN6lzZK78S7Z5J3b3uybXWDTiBPyWQaxsE3P6Qu25d0mt0jpDZERdO3vz6W +3lS4VZKV4XgZN9LaoaBJ6F2zc00b1TWuuPa2QlV2pYFyqJ+vF2WLorUnbDeiFPIOTn2 JM2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=mXRXZ6VDxd5rXK3mGfBK8ZozFtEP3pWk7IcPLjdXhBE=; b=QhyQYn3415MjO4ejZseTii/iGqDQpB6XxkGz4XnchVFIcsvIAdWBkzT4ftkDtCOvKh /8QXTHZkMCnSQgxzr37nEZUJg/Wvny7Q+eoj4d2+TkCd4D+Ov9RQeShKFc5SMBedj6p0 YQb+6VYFZ+dtfZNItybaVv1nQ8HvFkQLWlq6jyLaKQ4rogdxJ+CkCbO6kcavhxTJYLPM j/Kbj73clpzJpSmu8abPODNCUJEc10eC9C5UojYT37EpKCmuzzFlWqHh90+sfgfHFYHA VIAWSLA8oF0zk2m6E6AbopRVVV/8fC31PzXlBZdQpWZyHm5oZeaeyZtlOCtei8qObxDr bRUA== X-Gm-Message-State: AKS2vOyL4+enNoxVAqXVrAnSd87JfoY4m59l1RYf/G17v2WSUPZsrcJQ RC4KlO+TM6MMLxEuEEQ= X-Received: by 10.28.55.201 with SMTP id e192mr2957056wma.75.1497523988037; Thu, 15 Jun 2017 03:53:08 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Thu, 15 Jun 2017 12:52:22 +0200 Message-Id: <1497523981-38449-3-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1497523981-38449-1-git-send-email-pbonzini@redhat.com> References: <1497523981-38449-1-git-send-email-pbonzini@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c0c::244 Subject: [Qemu-devel] [PULL 02/41] exec: split file_ram_alloc() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 From: Marc-Andr=C3=A9 Lureau Move file opening part in a seperate function, file_ram_open(). This allows for reuse of file_ram_alloc() with a given fd. Signed-off-by: Marc-Andr=C3=A9 Lureau Message-Id: <20170602141229.15326-3-marcandre.lureau@redhat.com> Signed-off-by: Paolo Bonzini --- exec.c | 83 +++++++++++++++++++++++++++++++++++++-------------------------= ---- 1 file changed, 46 insertions(+), 37 deletions(-) diff --git a/exec.c b/exec.c index a5be851..42a5a7c 100644 --- a/exec.c +++ b/exec.c @@ -1482,19 +1482,17 @@ static int64_t get_file_size(int fd) return size; } =20 -static void *file_ram_alloc(RAMBlock *block, - ram_addr_t memory, - const char *path, - Error **errp) +static int file_ram_open(const char *path, + const char *region_name, + bool *created, + Error **errp) { - bool unlink_on_error =3D false; char *filename; char *sanitized_name; char *c; - void *area =3D MAP_FAILED; int fd =3D -1; - int64_t file_size; =20 + *created =3D false; for (;;) { fd =3D open(path, O_RDWR); if (fd >=3D 0) { @@ -1505,13 +1503,13 @@ static void *file_ram_alloc(RAMBlock *block, /* @path names a file that doesn't exist, create it */ fd =3D open(path, O_RDWR | O_CREAT | O_EXCL, 0644); if (fd >=3D 0) { - unlink_on_error =3D true; + *created =3D true; break; } } else if (errno =3D=3D EISDIR) { /* @path names a directory, create a file there */ /* Make name safe to use with mkstemp by replacing '/' with '_= '. */ - sanitized_name =3D g_strdup(memory_region_name(block->mr)); + sanitized_name =3D g_strdup(region_name); for (c =3D sanitized_name; *c !=3D '\0'; c++) { if (*c =3D=3D '/') { *c =3D '_'; @@ -1534,7 +1532,7 @@ static void *file_ram_alloc(RAMBlock *block, error_setg_errno(errp, errno, "can't open backing store %s for guest RAM", path); - goto error; + return -1; } /* * Try again on EINTR and EEXIST. The latter happens when @@ -1542,6 +1540,17 @@ static void *file_ram_alloc(RAMBlock *block, */ } =20 + return fd; +} + +static void *file_ram_alloc(RAMBlock *block, + ram_addr_t memory, + int fd, + bool truncate, + Error **errp) +{ + void *area; + block->page_size =3D qemu_fd_getpagesize(fd); block->mr->align =3D block->page_size; #if defined(__s390x__) @@ -1550,20 +1559,11 @@ static void *file_ram_alloc(RAMBlock *block, } #endif =20 - file_size =3D get_file_size(fd); - if (memory < block->page_size) { error_setg(errp, "memory size 0x" RAM_ADDR_FMT " must be equal to " "or larger than page size 0x%zx", memory, block->page_size); - goto error; - } - - if (file_size > 0 && file_size < memory) { - error_setg(errp, "backing store %s size 0x%" PRIx64 - " does not match 'size' option 0x" RAM_ADDR_FMT, - path, file_size, memory); - goto error; + return NULL; } =20 memory =3D ROUND_UP(memory, block->page_size); @@ -1582,7 +1582,7 @@ static void *file_ram_alloc(RAMBlock *block, * those labels. Therefore, extending the non-empty backend file * is disabled as well. */ - if (!file_size && ftruncate(fd, memory)) { + if (truncate && ftruncate(fd, memory)) { perror("ftruncate"); } =20 @@ -1591,30 +1591,19 @@ static void *file_ram_alloc(RAMBlock *block, if (area =3D=3D MAP_FAILED) { error_setg_errno(errp, errno, "unable to map backing store for guest RAM"); - goto error; + return NULL; } =20 if (mem_prealloc) { os_mem_prealloc(fd, area, memory, smp_cpus, errp); if (errp && *errp) { - goto error; + qemu_ram_munmap(area, memory); + return NULL; } } =20 block->fd =3D fd; return area; - -error: - if (area !=3D MAP_FAILED) { - qemu_ram_munmap(area, memory); - } - if (unlink_on_error) { - unlink(path); - } - if (fd !=3D -1) { - close(fd); - } - return NULL; } #endif =20 @@ -1931,6 +1920,9 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, M= emoryRegion *mr, { RAMBlock *new_block; Error *local_err =3D NULL; + int fd; + bool created; + int64_t file_size; =20 if (xen_enabled()) { error_setg(errp, "-mem-path not supported with Xen"); @@ -1954,15 +1946,32 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size,= MemoryRegion *mr, return NULL; } =20 + fd =3D file_ram_open(mem_path, memory_region_name(mr), &created, errp); + if (fd < 0) { + return NULL; + } + size =3D HOST_PAGE_ALIGN(size); + file_size =3D get_file_size(fd); + if (file_size > 0 && file_size < size) { + error_setg(errp, "backing store %s size 0x%" PRIx64 + " does not match 'size' option 0x" RAM_ADDR_FMT, + mem_path, file_size, size); + close(fd); + return NULL; + } + new_block =3D g_malloc0(sizeof(*new_block)); new_block->mr =3D mr; new_block->used_length =3D size; new_block->max_length =3D size; new_block->flags =3D share ? RAM_SHARED : 0; - new_block->host =3D file_ram_alloc(new_block, size, - mem_path, errp); + new_block->host =3D file_ram_alloc(new_block, size, fd, !file_size, er= rp); if (!new_block->host) { + if (created) { + unlink(mem_path); + } + close(fd); g_free(new_block); return NULL; } --=20 1.8.3.1