From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:37772) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SfdRj-0005wf-2d for qemu-devel@nongnu.org; Fri, 15 Jun 2012 16:48:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SfdRh-0001CX-2n for qemu-devel@nongnu.org; Fri, 15 Jun 2012 16:48:42 -0400 Received: from e39.co.us.ibm.com ([32.97.110.160]:45997) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SfdRg-00019a-SK for qemu-devel@nongnu.org; Fri, 15 Jun 2012 16:48:41 -0400 Received: from /spool/local by e39.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Fri, 15 Jun 2012 14:48:37 -0600 Received: from d03relay02.boulder.ibm.com (d03relay02.boulder.ibm.com [9.17.195.227]) by d03dlp01.boulder.ibm.com (Postfix) with ESMTP id 4CDA11FF001F for ; Fri, 15 Jun 2012 20:48:34 +0000 (WET) Received: from d03av04.boulder.ibm.com (d03av04.boulder.ibm.com [9.17.195.170]) by d03relay02.boulder.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q5FKmYSP177542 for ; Fri, 15 Jun 2012 14:48:34 -0600 Received: from d03av04.boulder.ibm.com (loopback [127.0.0.1]) by d03av04.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id q5FKmXUf013419 for ; Fri, 15 Jun 2012 14:48:33 -0600 From: Supriya Kannery Date: Sat, 16 Jun 2012 02:18:29 +0530 Message-Id: <20120615204829.9853.11764.sendpatchset@skannery.in.ibm.com> In-Reply-To: <20120615204648.9853.1225.sendpatchset@skannery.in.ibm.com> References: <20120615204648.9853.1225.sendpatchset@skannery.in.ibm.com> Subject: [Qemu-devel] [v1 Patch 7/10]Qemu: vmdk image file reopen List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Kevin Wolf , Shrinidhi Joshi , Stefan Hajnoczi , Jeff Cody , Luiz Capitulino , Christoph Hellwig vmdk driver changes for bdrv_reopen_xx functions to safely reopen image files. Reopening of image files while changing hostcache dynamically is handled here. Signed-off-by: Supriya Kannery Index: qemu/block/vmdk.c =================================================================== --- qemu.orig/block/vmdk.c +++ qemu/block/vmdk.c @@ -115,6 +115,14 @@ typedef struct VmdkGrainMarker { uint8_t data[0]; } VmdkGrainMarker; +typedef struct BDRVVmdkReopenState { + BDRVReopenState reopen_state; + BDRVVmdkState *stash_s; +} BDRVVmdkReopenState; + +static void vmdk_stash_state(BDRVVmdkState *stashed_state, BDRVVmdkState *s); +static void vmdk_revert_state(BDRVVmdkState *s, BDRVVmdkState *stashed_state); + static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename) { uint32_t magic; @@ -588,7 +596,6 @@ static int vmdk_parse_extents(const char if (!strcmp(type, "FLAT")) { /* FLAT extent */ VmdkExtent *extent; - extent = vmdk_add_extent(bs, extent_file, true, sectors, 0, 0, 0, 0, sectors); extent->flat_start_offset = flat_offset << 9; @@ -675,6 +682,94 @@ fail: return ret; } +static int vmdk_reopen_prepare(BlockDriverState *bs, BDRVReopenState **prs, + int flags) +{ + BDRVVmdkReopenState *vmdk_rs = g_malloc0(sizeof(BDRVVmdkReopenState)); + int ret = 0; + BDRVVmdkState *s = bs->opaque; + + vmdk_rs->reopen_state.bs = bs; + + /* save state before reopen */ + vmdk_rs->stash_s = g_malloc0(sizeof(BDRVVmdkState)); + vmdk_stash_state(vmdk_rs->stash_s, s); + s->num_extents = 0; + s->extents = NULL; + s->migration_blocker = NULL; + *prs = &(vmdk_rs->reopen_state); + + /* create extents afresh with new flags */ + ret = vmdk_open(bs, flags); + return ret; +} + +static void vmdk_reopen_commit(BlockDriverState *bs, BDRVReopenState *rs) +{ + BDRVVmdkReopenState *vmdk_rs; + BDRVVmdkState *stashed_s; + VmdkExtent *e; + int i; + + vmdk_rs = container_of(rs, BDRVVmdkReopenState, reopen_state); + stashed_s = vmdk_rs->stash_s; + + /* clean up stashed state */ + for (i = 0; i < stashed_s->num_extents; i++) { + e = &stashed_s->extents[i]; + g_free(e->l1_table); + g_free(e->l2_cache); + g_free(e->l1_backup_table); + } + g_free(stashed_s->extents); + g_free(vmdk_rs->stash_s); + g_free(vmdk_rs); +} + +static void vmdk_reopen_abort(BlockDriverState *bs, BDRVReopenState *rs) +{ + BDRVVmdkReopenState *vmdk_rs; + BDRVVmdkState *s = bs->opaque; + VmdkExtent *e; + int i; + + vmdk_rs = container_of(rs, BDRVVmdkReopenState, reopen_state); + + /* revert to stashed state */ + for (i = 0; i < s->num_extents; i++) { + e = &s->extents[i]; + g_free(e->l1_table); + g_free(e->l2_cache); + g_free(e->l1_backup_table); + } + g_free(s->extents); + vmdk_revert_state(s, vmdk_rs->stash_s); + g_free(vmdk_rs->stash_s); + g_free(vmdk_rs); +} + +static void vmdk_stash_state(BDRVVmdkState *stashed_state, BDRVVmdkState *s) +{ + stashed_state->lock = s->lock; + stashed_state->desc_offset = s->desc_offset; + stashed_state->cid_updated = s->cid_updated; + stashed_state->parent_cid = s->parent_cid; + stashed_state->num_extents = s->num_extents; + stashed_state->extents = s->extents; + stashed_state->migration_blocker = s->migration_blocker; +} + +static void vmdk_revert_state(BDRVVmdkState *s, BDRVVmdkState *stashed_state) +{ + s->lock = stashed_state->lock; + s->desc_offset = stashed_state->desc_offset; + s->cid_updated = stashed_state->cid_updated; + s->parent_cid = stashed_state->parent_cid; + s->num_extents = stashed_state->num_extents; + s->extents = stashed_state->extents; + s->migration_blocker = stashed_state->migration_blocker; +} + static int get_whole_cluster(BlockDriverState *bs, VmdkExtent *extent, uint64_t cluster_offset, @@ -1593,6 +1688,12 @@ static BlockDriver bdrv_vmdk = { .instance_size = sizeof(BDRVVmdkState), .bdrv_probe = vmdk_probe, .bdrv_open = vmdk_open, + .bdrv_reopen_prepare + = vmdk_reopen_prepare, + .bdrv_reopen_commit + = vmdk_reopen_commit, + .bdrv_reopen_abort + = vmdk_reopen_abort, .bdrv_read = vmdk_co_read, .bdrv_write = vmdk_co_write, .bdrv_close = vmdk_close,