From patchwork Thu Apr 28 02:06:48 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brad Campbell X-Patchwork-Id: 93165 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 1B484B6F1B for ; Thu, 28 Apr 2011 12:07:11 +1000 (EST) Received: from localhost ([::1]:59049 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QFGdH-00060E-3G for incoming@patchwork.ozlabs.org; Wed, 27 Apr 2011 22:07:07 -0400 Received: from eggs.gnu.org ([140.186.70.92]:53025) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QFGd9-000609-IZ for qemu-devel@nongnu.org; Wed, 27 Apr 2011 22:07:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QFGd8-0006V9-BX for qemu-devel@nongnu.org; Wed, 27 Apr 2011 22:06:59 -0400 Received: from fnarfbargle.com ([93.93.131.67]:34674) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QFGd8-0006Ux-4b for qemu-devel@nongnu.org; Wed, 27 Apr 2011 22:06:58 -0400 Received: from srv.homea ([10.8.0.1] helo=localhost ident=heh2328) by fnarfbargle.com with esmtp (Exim 4.69) (envelope-from ) id 1QFGd3-0008BA-1M; Thu, 28 Apr 2011 03:06:53 +0100 Received: from bkmac.home ([192.168.2.87]) by localhost with esmtp (Exim 4.72) (envelope-from ) id 1QFGcy-0000bM-Oz; Thu, 28 Apr 2011 10:06:48 +0800 Message-ID: <4DB8CBB8.9050002@fnarfbargle.com> Date: Thu, 28 Apr 2011 10:06:48 +0800 From: Brad Campbell User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.14) Gecko/20110223 Lightning/1.0b2 Thunderbird/3.1.8 MIME-Version: 1.0 To: Brad Campbell References: <4DB7880C.4090302@fnarfbargle.com> <4DB7DA44.4030008@fnarfbargle.com> <4DB7EAAC.3080402@redhat.com> <4DB81E16.5010408@fnarfbargle.com> <4DB8209A.7070400@redhat.com> <4DB821EE.8010706@fnarfbargle.com> In-Reply-To: <4DB821EE.8010706@fnarfbargle.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) X-Received-From: 93.93.131.67 Cc: Kevin Wolf , Stefan Hajnoczi , qemu-devel@nongnu.org Subject: Re: [Qemu-devel] Qemu-img convert with -B 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 On 27/04/11 22:02, Brad Campbell wrote: > On 27/04/11 21:56, Kevin Wolf wrote: > >> When you don't have a backing file, leaving an cluster unallocated means >> that it's zero. When you have a backing file, it could be anything. So >> if qemu-img convert wanted to save this space, it would have to read >> from the backing file and leave the cluster unallocated if it reads as >> zero. >> >> This is something that qemu-img doesn't do today. > This passes cursory testing, but I'm just wondering if this is along the right lines? /* Check if compression is supported */ @@ -767,6 +774,9 @@ static int img_convert(int argc, char **argv) bs_offset = 0; bdrv_get_geometry(bs[0], &bs_sectors); buf = qemu_malloc(IO_BUF_SIZE); + if (out_baseimg) { + buf3 = qemu_malloc(IO_BUF_SIZE); + } if (compress) { ret = bdrv_get_info(out_bs, &bdi); @@ -889,9 +899,17 @@ static int img_convert(int argc, char **argv) are present in both the output's and input's base images (no need to copy them). */ if (out_baseimg) { - if (!bdrv_is_allocated(bs[bs_i], sector_num - bs_offset, - n, &n1)) { + if (bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n) < 0) { + error_report("error while reading input file"); + goto out; + } + if (bdrv_read(out_bf, sector_num - bs_offset, buf3, n) < 0) { + error_report("error while reading backing file"); + goto out; + } + if (!compare_sectors(buf, buf3, n, &n1)) { sector_num += n1; + // printf("Skipping %u sectors\n",n1); continue; } /* The next 'n1' sectors are allocated in the input image. Copy @@ -939,9 +957,16 @@ out: free_option_parameters(create_options); free_option_parameters(param); qemu_free(buf); + if (buf3) { + qemu_free(buf3); + } if (out_bs) { bdrv_delete(out_bs); } + if (out_bf) { + bdrv_delete(out_bf); + } + if (bs) { for (bs_i = 0; bs_i < bs_n; bs_i++) { if (bs[bs_i]) { diff --git a/qemu-img.c b/qemu-img.c index d9c2c12..ab4c70c 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -1,4 +1,4 @@ -/* + /* * QEMU disk image utility * * Copyright (c) 2003-2008 Fabrice Bellard @@ -571,11 +571,12 @@ static int img_convert(int argc, char **argv) int progress = 0; const char *fmt, *out_fmt, *out_baseimg, *out_filename; BlockDriver *drv, *proto_drv; - BlockDriverState **bs = NULL, *out_bs = NULL; + BlockDriverState **bs = NULL, *out_bs = NULL, *out_bf = NULL; int64_t total_sectors, nb_sectors, sector_num, bs_offset; uint64_t bs_sectors; uint8_t * buf = NULL; const uint8_t *buf1; + uint8_t * buf3 = NULL; BlockDriverInfo bdi; QEMUOptionParameter *param = NULL, *create_options = NULL; QEMUOptionParameter *out_baseimg_param; @@ -719,6 +720,12 @@ static int img_convert(int argc, char **argv) out_baseimg_param = get_option_parameter(param, BLOCK_OPT_BACKING_FILE); if (out_baseimg_param) { out_baseimg = out_baseimg_param->value.s; + out_bf = bdrv_new_open(out_baseimg, NULL, BDRV_O_FLAGS); + if (!out_bf) { + error_report("Could not open backing file '%s'", out_baseimg); + ret = -1; + goto out; + } }