From patchwork Mon Nov 21 07:40:39 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhi Hui Li X-Patchwork-Id: 126712 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id E1AC0B720F for ; Mon, 21 Nov 2011 18:41:11 +1100 (EST) Received: from localhost ([::1]:48405 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RSOUy-0007ss-K7 for incoming@patchwork.ozlabs.org; Mon, 21 Nov 2011 02:41:04 -0500 Received: from eggs.gnu.org ([140.186.70.92]:37400) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RSOUs-0007sk-NK for qemu-devel@nongnu.org; Mon, 21 Nov 2011 02:40:59 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RSOUr-0004na-Lw for qemu-devel@nongnu.org; Mon, 21 Nov 2011 02:40:58 -0500 Received: from e28smtp01.in.ibm.com ([122.248.162.1]:40842) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RSOUr-0004nH-15 for qemu-devel@nongnu.org; Mon, 21 Nov 2011 02:40:57 -0500 Received: from /spool/local by e28smtp01.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 21 Nov 2011 13:10:51 +0530 Received: from d28relay03.in.ibm.com ([9.184.220.60]) by e28smtp01.in.ibm.com ([192.168.1.131]) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 21 Nov 2011 13:10:45 +0530 Received: from d28av04.in.ibm.com (d28av04.in.ibm.com [9.184.220.66]) by d28relay03.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id pAL7eh244624578 for ; Mon, 21 Nov 2011 13:10:44 +0530 Received: from d28av04.in.ibm.com (loopback [127.0.0.1]) by d28av04.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id pAL7ehn1027195 for ; Mon, 21 Nov 2011 18:40:43 +1100 Received: from mm.cn.ibm.com (mm.cn.ibm.com [9.115.116.119] (may be forged)) by d28av04.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id pAL7ef3s027091; Mon, 21 Nov 2011 18:40:42 +1100 From: Li Zhi Hui To: qemu-devel@nongnu.org Date: Mon, 21 Nov 2011 15:40:39 +0800 Message-Id: <1321861239-15253-1-git-send-email-zhihuili@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.4.1 x-cbid: 11112107-4790-0000-0000-00000011E1F6 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 122.248.162.1 Cc: kwolf@redhat.com, Li Zhi Hui , stefanha@linux.vnet.ibm.com, armbru@redhat.com Subject: [Qemu-devel] [PATCH v2] block: Use bdrv functions to replace file operation in qcow.c X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Since common file operation functions lack of error detection and use much more I/O syscalls, so change them to bdrv series functions and reduce I/O request. v2: avoid malloc a large memory v1: convert file operation functions to bdrv functions and reduce I/O request. Signed-off-by: Li Zhi Hui --- block/qcow.c | 48 +++++++++++++++++++++++++++++++----------------- 1 files changed, 31 insertions(+), 17 deletions(-) diff --git a/block/qcow.c b/block/qcow.c index adecee0..4420158 100644 --- a/block/qcow.c +++ b/block/qcow.c @@ -612,13 +612,14 @@ static void qcow_close(BlockDriverState *bs) static int qcow_create(const char *filename, QEMUOptionParameter *options) { - int fd, header_size, backing_filename_len, l1_size, i, shift; + int header_size, backing_filename_len, l1_size, shift, i; QCowHeader header; - uint64_t tmp; + uint8_t *tmp; int64_t total_size = 0; const char *backing_file = NULL; int flags = 0; int ret; + BlockDriverState *qcow_bs; /* Read out options */ while (options && options->name) { @@ -632,9 +633,21 @@ static int qcow_create(const char *filename, QEMUOptionParameter *options) options++; } - fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644); - if (fd < 0) - return -errno; + ret = bdrv_create_file(filename, options); + if (ret < 0) { + return ret; + } + + ret = bdrv_file_open(&qcow_bs, filename, BDRV_O_RDWR); + if (ret < 0) { + return ret; + } + + ret = bdrv_truncate(qcow_bs, 0); + if (ret < 0) { + goto exit; + } + memset(&header, 0, sizeof(header)); header.magic = cpu_to_be32(QCOW_MAGIC); header.version = cpu_to_be32(QCOW_VERSION); @@ -670,33 +683,34 @@ static int qcow_create(const char *filename, QEMUOptionParameter *options) } /* write all the data */ - ret = qemu_write_full(fd, &header, sizeof(header)); + ret = bdrv_pwrite(qcow_bs, 0, &header, sizeof(header)); if (ret != sizeof(header)) { - ret = -errno; goto exit; } if (backing_file) { - ret = qemu_write_full(fd, backing_file, backing_filename_len); + ret = bdrv_pwrite(qcow_bs, sizeof(header), + backing_file, backing_filename_len); if (ret != backing_filename_len) { - ret = -errno; goto exit; } - } - lseek(fd, header_size, SEEK_SET); - tmp = 0; - for(i = 0;i < l1_size; i++) { - ret = qemu_write_full(fd, &tmp, sizeof(tmp)); - if (ret != sizeof(tmp)) { - ret = -errno; + + tmp = g_malloc0(BDRV_SECTOR_SIZE); + for (i = 0; i < ((sizeof(uint64_t)*l1_size + BDRV_SECTOR_SIZE - 1)/ + BDRV_SECTOR_SIZE); i++) { + ret = bdrv_pwrite(qcow_bs, header_size + + BDRV_SECTOR_SIZE*i, tmp, BDRV_SECTOR_SIZE); + if (ret != BDRV_SECTOR_SIZE) { + g_free(tmp); goto exit; } } + g_free(tmp); ret = 0; exit: - close(fd); + bdrv_delete(qcow_bs); return ret; }