From patchwork Tue Apr 12 11:55:22 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Marc-Andr=C3=A9_Lureau?= X-Patchwork-Id: 8809701 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 951A19F39A for ; Tue, 12 Apr 2016 11:56:07 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id CC0D0201C8 for ; Tue, 12 Apr 2016 11:56:06 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 0205C20148 for ; Tue, 12 Apr 2016 11:56:06 +0000 (UTC) Received: from localhost ([::1]:47876 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1apwvN-0004ah-Df for patchwork-qemu-devel@patchwork.kernel.org; Tue, 12 Apr 2016 07:56:05 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51217) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1apwv0-0004OS-6M for qemu-devel@nongnu.org; Tue, 12 Apr 2016 07:55:46 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1apwuz-0008M6-0U for qemu-devel@nongnu.org; Tue, 12 Apr 2016 07:55:42 -0400 Received: from mail-qg0-x236.google.com ([2607:f8b0:400d:c04::236]:34130) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1apwuy-0008Lw-NQ for qemu-devel@nongnu.org; Tue, 12 Apr 2016 07:55:40 -0400 Received: by mail-qg0-x236.google.com with SMTP id c6so13189957qga.1 for ; Tue, 12 Apr 2016 04:55:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ScQ6bO/+zax729Rh/WN17TFSGlsr4Jw6W019jQfLlCQ=; b=Xyb+dqnlMKJI+fbbxbnDMFBIaHdxYNVM7iwxXg4+pWx5zcjCbIa0ubKqx959+NKKph 017M3NaP+CQLzJ0ARYE8hIEa2NzInWSqmCNpfW9/Q6BAeLWMWegsv2Fx9+3xgHpw+UZJ TNtsj0dme9mgOMoK1YTgTlHkGovzYDKXMyROX7evyy5+d/cCNna7/cvX4O7jeMTSgUsF S6yQiV8WpATIm6k40He7CU3oRcSORYLuq1M2MKexoKMGYqbFH6/78KOMX1drJBgeE6I7 hCe4o1XLZt6CZ0qVVZuxFA9jW3FABUGF9XxwUSARXNMQ2MFpQeb5tic7mdtD544D85CJ hljQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=ScQ6bO/+zax729Rh/WN17TFSGlsr4Jw6W019jQfLlCQ=; b=EDAWJd5FS5LK22F+H4nvLXoZF5voKSeFkRchsl6Ww83bvIq/dIsoxjZNmjmjMLE/RV 8Fo6liuGoFFuaDYXk6W+ZZKCpOgfRQ3VnjSjDzG/TXNrVdyLgrsaQ4IfDShFSYuhf6jG Sjab9HtSvDnJ+WN0/CVYKqHucpHiX26ZgvZvlG0C1z+gjxIaihtz5Dz2LGQic9SKNvTw dpwHMEM561/DnFVU6Op2muUZLGVnuBLUep4Cp5eLSacpfQatQKJpyWvUI7tJyoxdBcdH 7xWQ5L2xrERCkhkAPGhOl4sM9YktvK+ho72lZZxYArnOVj/5x0wV7DIzD7mzVh+f08+o HazA== X-Gm-Message-State: AOPr4FW3f+Uv9IUWJQHWmvybtHaWh4kxxRL6W9FxvQh0fKdDrtUUuswunDd+elFQf8Sulg== X-Received: by 10.140.16.133 with SMTP id 5mr3136965qgb.50.1460462140037; Tue, 12 Apr 2016 04:55:40 -0700 (PDT) Received: from localhost (bne75-h02-31-39-163-232.dsl.sta.abo.bbox.fr. [31.39.163.232]) by smtp.gmail.com with ESMTPSA id z2sm13364190qge.21.2016.04.12.04.55.37 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 12 Apr 2016 04:55:38 -0700 (PDT) From: marcandre.lureau@redhat.com To: qemu-devel@nongnu.org Date: Tue, 12 Apr 2016 13:55:22 +0200 Message-Id: <1460462129-17363-3-git-send-email-marcandre.lureau@redhat.com> X-Mailer: git-send-email 2.5.5 In-Reply-To: <1460462129-17363-1-git-send-email-marcandre.lureau@redhat.com> References: <1460462129-17363-1-git-send-email-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400d:c04::236 Subject: [Qemu-devel] [PATCH 2/9] 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: pbonzini@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Marc-André Lureau Move file opening part in a seperate function file_ram_open(). This allows for reuse of file_ram_alloc() with only a fd. Signed-off-by: Marc-André Lureau --- exec.c | 58 ++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/exec.c b/exec.c index dcc6632..422ea3e 100644 --- a/exec.c +++ b/exec.c @@ -1229,19 +1229,17 @@ void qemu_mutex_unlock_ramlist(void) } #ifdef __linux__ -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 = false; - char *filename; char *sanitized_name; + char *filename; char *c; - void *area; int fd = -1; - int64_t page_size; + *created = false; for (;;) { fd = open(path, O_RDWR); if (fd >= 0) { @@ -1252,13 +1250,13 @@ static void *file_ram_alloc(RAMBlock *block, /* @path names a file that doesn't exist, create it */ fd = open(path, O_RDWR | O_CREAT | O_EXCL, 0644); if (fd >= 0) { - unlink_on_error = true; + *created = true; break; } } else if (errno == EISDIR) { /* @path names a directory, create a file there */ /* Make name safe to use with mkstemp by replacing '/' with '_'. */ - sanitized_name = g_strdup(memory_region_name(block->mr)); + sanitized_name = g_strdup(region_name); for (c = sanitized_name; *c != '\0'; c++) { if (*c == '/') { *c = '_'; @@ -1281,7 +1279,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 @@ -1289,6 +1287,17 @@ static void *file_ram_alloc(RAMBlock *block, */ } + return fd; +} + +static void *file_ram_alloc(RAMBlock *block, + ram_addr_t memory, + int fd, + Error **errp) +{ + void *area; + int64_t page_size; + page_size = qemu_fd_getpagesize(fd); block->mr->align = page_size; @@ -1296,7 +1305,7 @@ static void *file_ram_alloc(RAMBlock *block, error_setg(errp, "memory size 0x" RAM_ADDR_FMT " must be equal to " "or larger than page size 0x%" PRIx64, memory, page_size); - goto error; + return NULL; } memory = ROUND_UP(memory, page_size); @@ -1315,7 +1324,7 @@ static void *file_ram_alloc(RAMBlock *block, if (area == MAP_FAILED) { error_setg_errno(errp, errno, "unable to map backing store for guest RAM"); - goto error; + return NULL; } if (mem_prealloc) { @@ -1324,15 +1333,6 @@ static void *file_ram_alloc(RAMBlock *block, block->fd = fd; return area; - -error: - if (unlink_on_error) { - unlink(path); - } - if (fd != -1) { - close(fd); - } - return NULL; } #endif @@ -1652,6 +1652,8 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr, { RAMBlock *new_block; Error *local_err = NULL; + int fd; + bool created; if (xen_enabled()) { error_setg(errp, "-mem-path not supported with Xen"); @@ -1675,15 +1677,23 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr, return NULL; } + fd = file_ram_open(mem_path, memory_region_name(mr), &created, errp); + if (fd < 0) { + return NULL; + } + size = HOST_PAGE_ALIGN(size); new_block = g_malloc0(sizeof(*new_block)); new_block->mr = mr; new_block->used_length = size; new_block->max_length = size; new_block->flags = share ? RAM_SHARED : 0; - new_block->host = file_ram_alloc(new_block, size, - mem_path, errp); + new_block->host = file_ram_alloc(new_block, size, fd, errp); if (!new_block->host) { + if (created) { + unlink(mem_path); + } + close(fd); g_free(new_block); return NULL; }