From patchwork Fri Nov 18 18:29:05 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Wolf X-Patchwork-Id: 126467 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 7CDD5B7232 for ; Sat, 19 Nov 2011 05:27:31 +1100 (EST) Received: from localhost ([::1]:54304 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RRT9i-0000uH-M2 for incoming@patchwork.ozlabs.org; Fri, 18 Nov 2011 13:27:18 -0500 Received: from eggs.gnu.org ([140.186.70.92]:38366) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RRT8h-0006cd-AM for qemu-devel@nongnu.org; Fri, 18 Nov 2011 13:26:16 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RRT8e-0006en-Fj for qemu-devel@nongnu.org; Fri, 18 Nov 2011 13:26:15 -0500 Received: from mx1.redhat.com ([209.132.183.28]:8711) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RRT8e-0006eS-6V for qemu-devel@nongnu.org; Fri, 18 Nov 2011 13:26:12 -0500 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id pAIIQAbB018066 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 18 Nov 2011 13:26:10 -0500 Received: from dhcp-5-188.str.redhat.com (vpn1-6-61.ams2.redhat.com [10.36.6.61]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id pAIIPuJf031078; Fri, 18 Nov 2011 13:26:09 -0500 From: Kevin Wolf To: qemu-devel@nongnu.org Date: Fri, 18 Nov 2011 19:29:05 +0100 Message-Id: <1321640945-9827-10-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> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-Received-From: 209.132.183.28 Cc: kwolf@redhat.com, stefanha@gmail.com Subject: [Qemu-devel] [PATCH v2 9/9] qcow2: Fix error path in qcow2_snapshot_load_tmp X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org If the bdrv_read() of the snapshot's L1 table fails, return the right error code and make sure that the old L1 table is still loaded and we don't break the BlockDriverState completely. Signed-off-by: Kevin Wolf --- block/qcow2-snapshot.c | 34 ++++++++++++++++++++++------------ 1 files changed, 22 insertions(+), 12 deletions(-) diff --git a/block/qcow2-snapshot.c b/block/qcow2-snapshot.c index d4fbcb9..b2d1a56 100644 --- a/block/qcow2-snapshot.c +++ b/block/qcow2-snapshot.c @@ -573,32 +573,42 @@ int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab) int qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name) { - int i, snapshot_index, l1_size2; + int i, snapshot_index; BDRVQcowState *s = bs->opaque; QCowSnapshot *sn; + uint64_t *new_l1_table; + int new_l1_bytes; + int ret; + assert(bs->read_only); + + /* Search the snapshot */ snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_name); if (snapshot_index < 0) { return -ENOENT; } - sn = &s->snapshots[snapshot_index]; - s->l1_size = sn->l1_size; - l1_size2 = s->l1_size * sizeof(uint64_t); - if (s->l1_table != NULL) { - g_free(s->l1_table); - } - s->l1_table_offset = sn->l1_table_offset; - s->l1_table = g_malloc0(align_offset(l1_size2, 512)); + /* Allocate and read in the snapshot's L1 table */ + new_l1_bytes = s->l1_size * sizeof(uint64_t); + new_l1_table = g_malloc0(align_offset(new_l1_bytes, 512)); - if (bdrv_pread(bs->file, sn->l1_table_offset, - s->l1_table, l1_size2) != l1_size2) { - return -1; + ret = bdrv_pread(bs->file, sn->l1_table_offset, new_l1_table, new_l1_bytes); + if (ret < 0) { + g_free(new_l1_table); + return ret; } + /* Switch the L1 table */ + g_free(s->l1_table); + + s->l1_size = sn->l1_size; + s->l1_table_offset = sn->l1_table_offset; + s->l1_table = new_l1_table; + for(i = 0;i < s->l1_size; i++) { be64_to_cpus(&s->l1_table[i]); } + return 0; }