From: Kevin Wolf <kwolf@redhat.com>
To: Anthony Liguori <anthony@codemonkey.ws>
Cc: qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH] qcow2: Add full image preallocation option
Date: Fri, 28 Jan 2011 09:46:22 +0100 [thread overview]
Message-ID: <4D42825E.9070101@redhat.com> (raw)
In-Reply-To: <4D41B07E.2030508@codemonkey.ws>
Am 27.01.2011 18:50, schrieb Anthony Liguori:
> On 01/27/2011 09:58 AM, Daniel P. Berrange wrote:
>> On Thu, Jan 27, 2011 at 04:52:14PM +0100, Kevin Wolf wrote:
>>
>>> This adds a preallocation=full mode to qcow2 image creation, which does not
>>> only allocate metadata for the whole image, but also writes zeros to it,
>>> creating a non-sparse image file.
>>>
>>> Signed-off-by: Kevin Wolf<kwolf@redhat.com>
>>> ---
>>> block/qcow2.c | 45 ++++++++++++++++++++++++++++++++++++++++-----
>>> 1 files changed, 40 insertions(+), 5 deletions(-)
>>>
>>> diff --git a/block/qcow2.c b/block/qcow2.c
>>> index a1773e4..90cf2ca 100644
>>> --- a/block/qcow2.c
>>> +++ b/block/qcow2.c
>>> @@ -838,7 +838,15 @@ static int qcow2_change_backing_file(BlockDriverState *bs,
>>> return qcow2_update_ext_header(bs, backing_file, backing_fmt);
>>> }
>>>
>>> -static int preallocate(BlockDriverState *bs)
>>> +enum prealloc_mode {
>>> + PREALLOC_OFF = 0,
>>> + PREALLOC_METADATA,
>>> + PREALLOC_FULL,
>>> +};
>>> +
>>> +#define IO_BUF_SIZE (2 * 1024 * 1024)
>>> +
>>> +static int preallocate(BlockDriverState *bs, enum prealloc_mode mode)
>>> {
>>> uint64_t nb_sectors;
>>> uint64_t offset;
>>> @@ -846,11 +854,14 @@ static int preallocate(BlockDriverState *bs)
>>> int ret;
>>> QCowL2Meta meta;
>>>
>>> + assert(mode != PREALLOC_OFF);
>>> +
>>> nb_sectors = bdrv_getlength(bs)>> 9;
>>> offset = 0;
>>> QLIST_INIT(&meta.dependent_requests);
>>> meta.cluster_offset = 0;
>>>
>>> + /* First allocate metadata in _really_ big chunks */
>>> while (nb_sectors) {
>>> num = MIN(nb_sectors, INT_MAX>> 9);
>>> ret = qcow2_alloc_cluster_offset(bs, offset, 0, num,&num,&meta);
>>> @@ -874,6 +885,28 @@ static int preallocate(BlockDriverState *bs)
>>> offset += num<< 9;
>>> }
>>>
>>> + /* Then write zeros to the cluster data, if requested */
>>> + if (mode == PREALLOC_FULL) {
>>> + void *buf = qemu_mallocz(IO_BUF_SIZE);
>>> +
>>> + nb_sectors = bdrv_getlength(bs)>> BDRV_SECTOR_BITS;
>>> + offset = 0;
>>> +
>>> + while (nb_sectors) {
>>> + num = MIN(nb_sectors, IO_BUF_SIZE / BDRV_SECTOR_SIZE);
>>> + ret = bdrv_write(bs, offset>> BDRV_SECTOR_BITS, buf, num);
>>>
>> Is there a way you can calculate the total size of the qcow2
>> file upfront, and just use a single posix_fallocate() call to
>> do the zero-filled allocation of all the data blocks. It is
>> many orders of magnitude faster than truely writing blocks of
>> zero'd data on modern filesystems. I guess if you're using
>> compression or encryption, we'd really have to go the slow
>> path, but for regular usage it'd be better to take a fast
>> path.
>>
>
> Hrm, so is the intention here to avoid sparse files or to not assume
> zero-fill?
The primary intention (as I understood our feature request ;-)) was to
avoid sparse files. In it's current implementation you could also use it
to overwrite any left-over data. Maybe that's a point for not having a
bdrv_fallocate like Daniel suggested, but rather a bdrv_zero_init, which
could fallocate on files and write zeros on a block device.
Kevin
next prev parent reply other threads:[~2011-01-28 8:44 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-01-27 15:52 [Qemu-devel] [PATCH] qcow2: Add full image preallocation option Kevin Wolf
2011-01-27 15:58 ` Daniel P. Berrange
2011-01-27 17:50 ` Anthony Liguori
2011-01-28 8:46 ` Kevin Wolf [this message]
2011-01-28 8:22 ` Kevin Wolf
2011-01-28 10:49 ` Daniel P. Berrange
2011-01-27 17:45 ` Anthony Liguori
2011-02-04 10:59 ` Stefan Hajnoczi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4D42825E.9070101@redhat.com \
--to=kwolf@redhat.com \
--cc=anthony@codemonkey.ws \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).