diff options
author | Max Reitz <mreitz@redhat.com> | 2014-10-24 15:57:31 +0200 |
---|---|---|
committer | Stefan Hajnoczi <stefanha@redhat.com> | 2014-11-03 11:41:48 +0000 |
commit | 491d27e2af4f6e157c4b29d43269c5cb0d191171 (patch) | |
tree | 3a08c2abc1325963d13083b87eae904a9948f7bb /block/qcow2.c | |
parent | 808c4b6f30d77295292cef8ee38c462957a6b9ca (diff) |
qcow2: Implement bdrv_make_empty()
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 <mreitz@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
Message-id: 1414159063-25977-3-git-send-email-mreitz@redhat.com
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
Diffstat (limited to 'block/qcow2.c')
-rw-r--r-- | block/qcow2.c | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/block/qcow2.c b/block/qcow2.c index d64a4bab3d..bf871d58f4 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, |