From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([140.186.70.92]:38326) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RRT8e-0006Ru-BQ for qemu-devel@nongnu.org; Fri, 18 Nov 2011 13:26:13 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RRT8d-0006eH-4e for qemu-devel@nongnu.org; Fri, 18 Nov 2011 13:26:12 -0500 Received: from mx1.redhat.com ([209.132.183.28]:64656) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RRT8c-0006eA-Qz for qemu-devel@nongnu.org; Fri, 18 Nov 2011 13:26:11 -0500 From: Kevin Wolf Date: Fri, 18 Nov 2011 19:29:04 +0100 Message-Id: <1321640945-9827-9-git-send-email-kwolf@redhat.com> In-Reply-To: <1321640945-9827-1-git-send-email-kwolf@redhat.com> References: <1321640945-9827-1-git-send-email-kwolf@redhat.com> Subject: [Qemu-devel] [PATCH v2 8/9] qcow2: Fix order in qcow2_snapshot_delete List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: kwolf@redhat.com, stefanha@gmail.com First the snapshot must be deleted and only then the refcounts can be decreased. Signed-off-by: Kevin Wolf --- block/qcow2-snapshot.c | 48 +++++++++++++++++++++++++++++++++--------------- 1 files changed, 33 insertions(+), 15 deletions(-) diff --git a/block/qcow2-snapshot.c b/block/qcow2-snapshot.c index 20cb66e..d4fbcb9 100644 --- a/block/qcow2-snapshot.c +++ b/block/qcow2-snapshot.c @@ -489,32 +489,50 @@ fail: int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) { BDRVQcowState *s = bs->opaque; - QCowSnapshot *sn; + QCowSnapshot sn; int snapshot_index, ret; + /* Search the snapshot */ snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id); - if (snapshot_index < 0) + if (snapshot_index < 0) { return -ENOENT; - sn = &s->snapshots[snapshot_index]; + } + sn = s->snapshots[snapshot_index]; - ret = qcow2_update_snapshot_refcount(bs, sn->l1_table_offset, sn->l1_size, -1); - if (ret < 0) + /* Remove it from the snapshot list */ + memmove(s->snapshots + snapshot_index, + s->snapshots + snapshot_index + 1, + (s->nb_snapshots - snapshot_index - 1) * sizeof(sn)); + s->nb_snapshots--; + ret = qcow2_write_snapshots(bs); + if (ret < 0) { return ret; - /* must update the copied flag on the current cluster offsets */ - ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0); - if (ret < 0) + } + + /* + * The snapshot is now unused, clean up. If we fail after this point, we + * won't recover but just leak clusters. + */ + g_free(sn.id_str); + g_free(sn.name); + + /* + * Now decrease the refcounts of clusters referenced by the snapshot and + * free the L1 table. + */ + ret = qcow2_update_snapshot_refcount(bs, sn.l1_table_offset, + sn.l1_size, -1); + if (ret < 0) { return ret; - qcow2_free_clusters(bs, sn->l1_table_offset, sn->l1_size * sizeof(uint64_t)); + } + qcow2_free_clusters(bs, sn.l1_table_offset, sn.l1_size * sizeof(uint64_t)); - g_free(sn->id_str); - g_free(sn->name); - memmove(sn, sn + 1, (s->nb_snapshots - snapshot_index - 1) * sizeof(*sn)); - s->nb_snapshots--; - ret = qcow2_write_snapshots(bs); + /* must update the copied flag on the current cluster offsets */ + ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0); if (ret < 0) { - /* XXX: restore snapshot if error ? */ return ret; } + #ifdef DEBUG_ALLOC { BdrvCheckResult result = {0}; -- 1.7.6.4