From patchwork Fri Jul 11 06:10:03 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hu Tao X-Patchwork-Id: 368975 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 0E3D31400A0 for ; Fri, 11 Jul 2014 16:16:30 +1000 (EST) Received: from localhost ([::1]:42379 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X5U8C-0000bQ-2C for incoming@patchwork.ozlabs.org; Fri, 11 Jul 2014 02:16:28 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48535) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X5U4R-0003GG-8U for qemu-devel@nongnu.org; Fri, 11 Jul 2014 02:12:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1X5U4M-0007eH-IZ for qemu-devel@nongnu.org; Fri, 11 Jul 2014 02:12:35 -0400 Received: from [59.151.112.132] (port=48618 helo=heian.cn.fujitsu.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X5U4K-0007Cy-6q for qemu-devel@nongnu.org; Fri, 11 Jul 2014 02:12:30 -0400 X-IronPort-AV: E=Sophos;i="5.00,873,1396972800"; d="scan'208";a="33138435" Received: from unknown (HELO edo.cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 11 Jul 2014 14:09:42 +0800 Received: from G08CNEXCHPEKD03.g08.fujitsu.local (localhost.localdomain [127.0.0.1]) by edo.cn.fujitsu.com (8.14.3/8.13.1) with ESMTP id s6B6CPN9031223; Fri, 11 Jul 2014 14:12:25 +0800 Received: from G08FNSTD100614.fnst.cn.fujitsu.com (10.167.226.102) by G08CNEXCHPEKD03.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.181.6; Fri, 11 Jul 2014 14:12:32 +0800 From: Hu Tao To: Date: Fri, 11 Jul 2014 14:10:03 +0800 Message-ID: X-Mailer: git-send-email 1.9.3 In-Reply-To: References: MIME-Version: 1.0 X-Originating-IP: [10.167.226.102] X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 59.151.112.132 Cc: Kevin Wolf , Yasunori Goto , Stefan Hajnoczi , Max Reitz Subject: [Qemu-devel] [PATCH v12 6/6] qcow2: Add falloc and full preallocation option 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 This adds preallocation=falloc and preallocation=full mode to qcow2 image creation. preallocation=full allocates disk space by writing zeros to disk to ensure disk space in any cases. preallocation=falloc likes preallocation=full, but allocates disk space by posix_fallocate(). Signed-off-by: Hu Tao Reviewed-by: Max Reitz --- block/qcow2.c | 31 ++++++++++++++++++++++++-- tests/qemu-iotests/082.out | 54 +++++++++++++++++++++++----------------------- 2 files changed, 56 insertions(+), 29 deletions(-) diff --git a/block/qcow2.c b/block/qcow2.c index cfba93b..f48e915 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -1593,6 +1593,9 @@ static int preallocate(BlockDriverState *bs) return 0; } +static uint64_t minimal_blob_size(uint64_t ts, int cb, int spcb, + uint64_t overhead); + static int qcow2_create2(const char *filename, int64_t total_size, const char *backing_file, const char *backing_format, int flags, size_t cluster_size, PreallocMode prealloc, @@ -1628,6 +1631,29 @@ static int qcow2_create2(const char *filename, int64_t total_size, Error *local_err = NULL; int ret; + if (prealloc == PREALLOC_MODE_FULL || prealloc == PREALLOC_MODE_FALLOC) { + int64_t meta_size = 0; + uint64_t nl2e; + + total_size = align_offset(total_size, cluster_size); + + /* total size of L2 tables */ + nl2e = total_size >> cluster_bits; + nl2e = align_offset(nl2e, cluster_size / sizeof(uint64_t)); + uint64_t l2_clusters = nl2e * sizeof(uint64_t) >> cluster_bits; + + meta_size = + (1 + + minimal_blob_size(total_size >> BDRV_SECTOR_BITS, + cluster_bits, cluster_bits - BDRV_SECTOR_BITS, + 1 + l2_clusters + + (total_size >> cluster_bits)) + + l2_clusters) << cluster_bits; + + qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_size + meta_size); + qemu_opt_set(opts, BLOCK_OPT_PREALLOC, PreallocMode_lookup[prealloc]); + } + ret = bdrv_create_file(filename, opts, &local_err); if (ret < 0) { error_propagate(errp, local_err); @@ -1733,7 +1759,7 @@ static int qcow2_create2(const char *filename, int64_t total_size, } /* And if we're supposed to preallocate metadata, do that now */ - if (prealloc) { + if (prealloc != PREALLOC_MODE_OFF) { BDRVQcowState *s = bs->opaque; qemu_co_mutex_lock(&s->lock); ret = preallocate(bs); @@ -2760,7 +2786,8 @@ static QemuOptsList qcow2_create_opts = { { .name = BLOCK_OPT_PREALLOC, .type = QEMU_OPT_STRING, - .help = "Preallocation mode (allowed values: off, metadata)" + .help = "Preallocation mode (allowed values: off, metadata, " + "falloc, full)" }, { .name = BLOCK_OPT_LAZY_REFCOUNTS, diff --git a/tests/qemu-iotests/082.out b/tests/qemu-iotests/082.out index 413e7ef..90c21c8 100644 --- a/tests/qemu-iotests/082.out +++ b/tests/qemu-iotests/082.out @@ -64,7 +64,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -76,7 +76,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -88,7 +88,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -100,7 +100,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -112,7 +112,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -124,7 +124,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -136,7 +136,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -148,7 +148,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -175,7 +175,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, falloc, full) lazy_refcounts Postpone refcount updates Testing: create -o help @@ -253,7 +253,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -265,7 +265,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -277,7 +277,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -289,7 +289,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -301,7 +301,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -313,7 +313,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -325,7 +325,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -337,7 +337,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -364,7 +364,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, falloc, full) lazy_refcounts Postpone refcount updates Testing: convert -o help @@ -431,7 +431,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -443,7 +443,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -455,7 +455,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -467,7 +467,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -479,7 +479,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -491,7 +491,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -503,7 +503,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -515,7 +515,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, falloc, full) lazy_refcounts Postpone refcount updates nocow Turn off copy-on-write (valid only on btrfs) @@ -544,7 +544,7 @@ backing_file File name of a base image backing_fmt Image format of the base image encryption Encrypt the image cluster_size qcow2 cluster size -preallocation Preallocation mode (allowed values: off, metadata) +preallocation Preallocation mode (allowed values: off, metadata, falloc, full) lazy_refcounts Postpone refcount updates Testing: convert -o help