From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:37686) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Svxce-0008DG-9x for qemu-devel@nongnu.org; Mon, 30 Jul 2012 17:35:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Svxcd-0001j5-0R for qemu-devel@nongnu.org; Mon, 30 Jul 2012 17:35:28 -0400 Received: from e32.co.us.ibm.com ([32.97.110.150]:38853) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Svxcc-0001iz-Q9 for qemu-devel@nongnu.org; Mon, 30 Jul 2012 17:35:26 -0400 Received: from /spool/local by e32.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 30 Jul 2012 15:35:26 -0600 Received: from d03relay03.boulder.ibm.com (d03relay03.boulder.ibm.com [9.17.195.228]) by d03dlp02.boulder.ibm.com (Postfix) with ESMTP id F136A3E4005B for ; Mon, 30 Jul 2012 21:35:21 +0000 (WET) Received: from d03av04.boulder.ibm.com (d03av04.boulder.ibm.com [9.17.195.170]) by d03relay03.boulder.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q6ULZFjt097086 for ; Mon, 30 Jul 2012 15:35:16 -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 q6ULZEfm024997 for ; Mon, 30 Jul 2012 15:35:15 -0600 From: Supriya Kannery Date: Tue, 31 Jul 2012 03:05:10 +0530 Message-Id: <20120730213510.21536.12928.sendpatchset@skannery.in.ibm.com> In-Reply-To: <20120730213409.21536.7589.sendpatchset@skannery.in.ibm.com> References: <20120730213409.21536.7589.sendpatchset@skannery.in.ibm.com> Subject: [Qemu-devel] [v2 Patch 4/9]block: 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,