From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60660) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XhfNQ-0001U7-Vd for qemu-devel@nongnu.org; Fri, 24 Oct 2014 09:58:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XhfNJ-0006w0-5I for qemu-devel@nongnu.org; Fri, 24 Oct 2014 09:58:00 -0400 Received: from mx1.redhat.com ([209.132.183.28]:20203) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XhfNI-0006vn-Rc for qemu-devel@nongnu.org; Fri, 24 Oct 2014 09:57:53 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s9ODvo0u020064 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL) for ; Fri, 24 Oct 2014 09:57:51 -0400 From: Max Reitz Date: Fri, 24 Oct 2014 15:57:31 +0200 Message-Id: <1414159063-25977-3-git-send-email-mreitz@redhat.com> In-Reply-To: <1414159063-25977-1-git-send-email-mreitz@redhat.com> References: <1414159063-25977-1-git-send-email-mreitz@redhat.com> Subject: [Qemu-devel] [PATCH v14 02/14] qcow2: Implement bdrv_make_empty() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Kevin Wolf , Stefan Hajnoczi , Max Reitz Implement this function by making all clusters in the image file fall through to the backing file (by using the recently extended discard). Signed-off-by: Max Reitz Reviewed-by: Eric Blake Reviewed-by: Kevin Wolf --- block/qcow2.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/block/qcow2.c b/block/qcow2.c index d64a4ba..bf871d5 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -2230,6 +2230,32 @@ fail: return ret; } +static int qcow2_make_empty(BlockDriverState *bs) +{ + int ret = 0; + uint64_t start_sector; + int sector_step = INT_MAX / BDRV_SECTOR_SIZE; + + for (start_sector = 0; start_sector < bs->total_sectors; + start_sector += sector_step) + { + /* As this function is generally used after committing an external + * snapshot, QCOW2_DISCARD_SNAPSHOT seems appropriate. Also, the + * default action for this kind of discard is to pass the discard, + * which will ideally result in an actually smaller image file, as + * is probably desired. */ + ret = qcow2_discard_clusters(bs, start_sector * BDRV_SECTOR_SIZE, + MIN(sector_step, + bs->total_sectors - start_sector), + QCOW2_DISCARD_SNAPSHOT, true); + if (ret < 0) { + break; + } + } + + return ret; +} + static coroutine_fn int qcow2_co_flush_to_os(BlockDriverState *bs) { BDRVQcowState *s = bs->opaque; @@ -2676,6 +2702,7 @@ static BlockDriver bdrv_qcow2 = { .bdrv_co_discard = qcow2_co_discard, .bdrv_truncate = qcow2_truncate, .bdrv_write_compressed = qcow2_write_compressed, + .bdrv_make_empty = qcow2_make_empty, .bdrv_snapshot_create = qcow2_snapshot_create, .bdrv_snapshot_goto = qcow2_snapshot_goto, -- 1.9.3