From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:40032) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SCsN0-0005Uq-HO for qemu-devel@nongnu.org; Wed, 28 Mar 2012 08:53:09 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SCsMr-0007sr-OK for qemu-devel@nongnu.org; Wed, 28 Mar 2012 08:52:58 -0400 From: =?UTF-8?q?Andreas=20F=C3=A4rber?= Date: Wed, 28 Mar 2012 14:52:24 +0200 Message-Id: <1332939159-16434-22-git-send-email-afaerber@suse.de> In-Reply-To: <1332939159-16434-1-git-send-email-afaerber@suse.de> References: <1332939159-16434-1-git-send-email-afaerber@suse.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH stable-0.15 21/36] Teach block/vdi about "discarded" (no longer allocated) blocks List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Kevin Wolf , kvm@suse.de, qemu-stable@nongnu.org, Eric Sunshine , Bruce Rogers , =?UTF-8?q?Andreas=20F=C3=A4rber?= From: Eric Sunshine An entry in the VDI block map will hold an offset to the actual block if the block is allocated, or one of two specially-interpreted values if not allocated. Using VirtualBox terminology, value VDI_IMAGE_BLOCK_FREE (0xffffffff) represents a never-allocated block (semantically arbitrary content). VDI_IMAGE_BLOCK_ZERO (0xfffffffe) represents a "discarded" block (semantically zero-filled). block/vdi knows only about VDI_IMAGE_BLOCK_FREE. Teach it about VDI_IMAGE_BLOCK_ZERO. Signed-off-by: Eric Sunshine Signed-off-by: Kevin Wolf (cherry picked from commit c794b4e0fd9ef8d72b068614dcdb2418c105d5cc) Signed-off-by: Bruce Rogers Signed-off-by: Andreas F=C3=A4rber --- block/vdi.c | 23 ++++++++++++++--------- 1 files changed, 14 insertions(+), 9 deletions(-) diff --git a/block/vdi.c b/block/vdi.c index 261cf9b..1be0cdc 100644 --- a/block/vdi.c +++ b/block/vdi.c @@ -114,8 +114,13 @@ void uuid_unparse(const uuid_t uu, char *out); */ #define VDI_TEXT "<<< QEMU VM Virtual Disk Image >>>\n" =20 -/* Unallocated blocks use this index (no need to convert endianness). */ -#define VDI_UNALLOCATED UINT32_MAX +/* A never-allocated block; semantically arbitrary content. */ +#define VDI_UNALLOCATED 0xffffffffU + +/* A discarded (no longer allocated) block; semantically zero-filled. */ +#define VDI_DISCARDED 0xfffffffeU + +#define VDI_IS_ALLOCATED(X) ((X) < VDI_DISCARDED) =20 #if !defined(CONFIG_UUID) void uuid_generate(uuid_t out) @@ -307,10 +312,10 @@ static int vdi_check(BlockDriverState *bs, BdrvChec= kResult *res) /* Check block map and value of blocks_allocated. */ for (block =3D 0; block < s->header.blocks_in_image; block++) { uint32_t bmap_entry =3D le32_to_cpu(s->bmap[block]); - if (bmap_entry !=3D VDI_UNALLOCATED) { + if (VDI_IS_ALLOCATED(bmap_entry)) { if (bmap_entry < s->header.blocks_in_image) { blocks_allocated++; - if (bmap[bmap_entry] =3D=3D VDI_UNALLOCATED) { + if (!VDI_IS_ALLOCATED(bmap[bmap_entry])) { bmap[bmap_entry] =3D bmap_entry; } else { fprintf(stderr, "ERROR: block index %" PRIu32 @@ -472,7 +477,7 @@ static int vdi_is_allocated(BlockDriverState *bs, int= 64_t sector_num, n_sectors =3D nb_sectors; } *pnum =3D n_sectors; - return bmap_entry !=3D VDI_UNALLOCATED; + return VDI_IS_ALLOCATED(bmap_entry); } =20 static void vdi_aio_cancel(BlockDriverAIOCB *blockacb) @@ -603,7 +608,7 @@ static void vdi_aio_read_cb(void *opaque, int ret) /* prepare next AIO request */ acb->n_sectors =3D n_sectors; bmap_entry =3D le32_to_cpu(s->bmap[block_index]); - if (bmap_entry =3D=3D VDI_UNALLOCATED) { + if (!VDI_IS_ALLOCATED(bmap_entry)) { /* Block not allocated, return zeros, no need to wait. */ memset(acb->buf, 0, n_sectors * SECTOR_SIZE); ret =3D vdi_schedule_bh(vdi_aio_rw_bh, acb); @@ -685,7 +690,7 @@ static void vdi_aio_write_cb(void *opaque, int ret) if (acb->header_modified) { VdiHeader *header =3D acb->block_buffer; logout("now writing modified header\n"); - assert(acb->bmap_first !=3D VDI_UNALLOCATED); + assert(VDI_IS_ALLOCATED(acb->bmap_first)); *header =3D s->header; vdi_header_to_le(header); acb->header_modified =3D 0; @@ -699,7 +704,7 @@ static void vdi_aio_write_cb(void *opaque, int ret) goto done; } return; - } else if (acb->bmap_first !=3D VDI_UNALLOCATED) { + } else if (VDI_IS_ALLOCATED(acb->bmap_first)) { /* One or more new blocks were allocated. */ uint64_t offset; uint32_t bmap_first; @@ -749,7 +754,7 @@ static void vdi_aio_write_cb(void *opaque, int ret) /* prepare next AIO request */ acb->n_sectors =3D n_sectors; bmap_entry =3D le32_to_cpu(s->bmap[block_index]); - if (bmap_entry =3D=3D VDI_UNALLOCATED) { + if (!VDI_IS_ALLOCATED(bmap_entry)) { /* Allocate new block and write to it. */ uint64_t offset; uint8_t *block; --=20 1.7.7