From patchwork Fri May 28 18:18:46 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miguel Di Ciurcio Filho X-Patchwork-Id: 53949 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 7DAA1B6F11 for ; Sat, 29 May 2010 04:21:48 +1000 (EST) Received: from localhost ([127.0.0.1]:32783 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OI4BQ-00073u-Lj for incoming@patchwork.ozlabs.org; Fri, 28 May 2010 14:21:24 -0400 Received: from [140.186.70.92] (port=36905 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OI49s-00070X-Bf for qemu-devel@nongnu.org; Fri, 28 May 2010 14:19:51 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OI49n-0005sN-Gh for qemu-devel@nongnu.org; Fri, 28 May 2010 14:19:45 -0400 Received: from mail-yx0-f173.google.com ([209.85.213.173]:49585) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OI49n-0005rx-DJ for qemu-devel@nongnu.org; Fri, 28 May 2010 14:19:43 -0400 Received: by yxn35 with SMTP id 35so134809yxn.4 for ; Fri, 28 May 2010 11:19:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:from:to:cc:subject:date :message-id:x-mailer; bh=STOpMNbGQpjvh/8nyllDVwS3abRHG+HiP8183SUF/4Y=; b=vLHtFioPQVrRgmohTI44FPMD598a4qRR0VT0jVppGwPLRjZQnps7oqltJTfgsrVaZ8 r2PyYWp6KIQ4cOf+to1sgKC13ZlxDN74/RI9Rtm8moIqqfjT/P/qfmG+CGZEti0o1mKb i+ADYzdj8tKbYXYj2BH/AXbuc+aNcz9TqbjqE= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer; b=Gt7Ntx0KPCWdLRSH6ivHaTNd9nAip7H6X72bpSI+Wac4pT4J7JtRUjI2VrUurydWCi 4KbPywYrUplr/xzmBPhvJJ32kXvTt8F850FgRZuMEO5BD0OjMv5o50Zh1h897R/fYAwD dQYEapnXx29O+lxu3WkE3Xo+rfRmQSdc2pTag= Received: by 10.150.244.1 with SMTP id r1mr1879949ybh.374.1275070780918; Fri, 28 May 2010 11:19:40 -0700 (PDT) Received: from localhost.localdomain (quake.ic.unicamp.br [143.106.7.51]) by mx.google.com with ESMTPS id p5sm21491409ybe.0.2010.05.28.11.19.38 (version=TLSv1/SSLv3 cipher=RC4-MD5); Fri, 28 May 2010 11:19:39 -0700 (PDT) From: Miguel Di Ciurcio Filho To: qemu-devel@nongnu.org Date: Fri, 28 May 2010 15:18:46 -0300 Message-Id: <1275070726-18531-1-git-send-email-miguel.filho@gmail.com> X-Mailer: git-send-email 1.7.1 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) Cc: kwolf@redhat.com, Miguel Di Ciurcio Filho Subject: [Qemu-devel] [PATCH] savevm: Really verify if a drive supports snapshots X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Both bdrv_can_snapshot() and bdrv_has_snapshot() does not work as advertized. First issue: Their names implies different porpouses, but they do the same thing and have exactly the same code. Maybe copied and pasted and forgotten? bdrv_has_snapshot() is called in various places for actually checking if there is snapshots or not. Second issue: the way bdrv_can_snapshot() verifies if a block driver supports or not snapshots does not catch all cases. E.g.: a raw image. So when do_savevm() is called, first thing it does is to set a global BlockDriverState to save the VM memory state calling get_bs_snapshots(). static BlockDriverState *get_bs_snapshots(void) { BlockDriverState *bs; DriveInfo *dinfo; if (bs_snapshots) return bs_snapshots; QTAILQ_FOREACH(dinfo, &drives, next) { bs = dinfo->bdrv; if (bdrv_can_snapshot(bs)) goto ok; } return NULL; ok: bs_snapshots = bs; return bs; } bdrv_can_snapshot() may return a BlockDriverState that does not support snapshots and do_savevm() goes on. Later on in do_savevm(), we find: QTAILQ_FOREACH(dinfo, &drives, next) { bs1 = dinfo->bdrv; if (bdrv_has_snapshot(bs1)) { /* Write VM state size only to the image that contains the state */ sn->vm_state_size = (bs == bs1 ? vm_state_size : 0); ret = bdrv_snapshot_create(bs1, sn); if (ret < 0) { monitor_printf(mon, "Error while creating snapshot on '%s'\n", bdrv_get_device_name(bs1)); } } } bdrv_has_snapshot(bs1) is not checking if the device does support or has snapshots as explained above. Only in bdrv_snapshot_create() the device is actually checked for snapshot support. So, in cases where the first device supports snapshots, and the second does not, the snapshot on the first will happen anyways. I believe this is not a good behavior. It should be an all or nothing process. This patch addresses these issues by making bdrv_can_snapshot() and bdrv_has_snapshot() actually do what they must do and enforces better tests to avoid errors in the middle of do_savevm(). The functions were moved from savevm.c to block.c. It makes more sense to me. The bdrv_has_snapshot() is not beaultiful, but it does the job. I think having this function avaible in the BlockDriver would be the best option. The loadvm_state() function was updated too to enforce that when loading a VM at least all writable devices must support snapshots too. Signed-off-by: Miguel Di Ciurcio Filho --- block.c | 47 ++++++++++++++++++++++++++++++++++++++++++----- block.h | 2 ++ savevm.c | 48 +++++++++++++++++++++++++----------------------- 3 files changed, 69 insertions(+), 28 deletions(-) diff --git a/block.c b/block.c index cd70730..7eddc15 100644 --- a/block.c +++ b/block.c @@ -1720,15 +1720,52 @@ void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event) /**************************************************************/ /* handling of snapshots */ -int bdrv_snapshot_create(BlockDriverState *bs, - QEMUSnapshotInfo *sn_info) +int bdrv_can_snapshot(BlockDriverState *bs) { BlockDriver *drv = bs->drv; - if (!drv) + if (!drv) { + return -ENOMEDIUM; + } + + if (!drv->bdrv_snapshot_create || bdrv_is_removable(bs) || + bdrv_is_read_only(bs)) { + return -ENOTSUP; + } + + return 1; +} + +int bdrv_has_snapshot(BlockDriverState *bs) +{ + int ret; + QEMUSnapshotInfo *sn_tab; + BlockDriver *drv = bs->drv; + if (!drv) { return -ENOMEDIUM; - if (!drv->bdrv_snapshot_create) + } + + if (!drv->bdrv_snapshot_list) { return -ENOTSUP; - return drv->bdrv_snapshot_create(bs, sn_info); + } + + ret = drv->bdrv_snapshot_list(bs, &sn_tab); + + if (sn_tab) { + qemu_free(sn_tab); + } + + return ret; +} + +int bdrv_snapshot_create(BlockDriverState *bs, + QEMUSnapshotInfo *sn_info) +{ + BlockDriver *drv = bs->drv; + if (bdrv_can_snapshot(bs) > 0) { + return drv->bdrv_snapshot_create(bs, sn_info); + } + + return -1; } int bdrv_snapshot_goto(BlockDriverState *bs, diff --git a/block.h b/block.h index 24efeb6..c536f1c 100644 --- a/block.h +++ b/block.h @@ -173,6 +173,8 @@ int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi); const char *bdrv_get_encrypted_filename(BlockDriverState *bs); void bdrv_get_backing_filename(BlockDriverState *bs, char *filename, int filename_size); +int bdrv_can_snapshot(BlockDriverState *bs); +int bdrv_has_snapshot(BlockDriverState *bs); int bdrv_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info); int bdrv_snapshot_goto(BlockDriverState *bs, diff --git a/savevm.c b/savevm.c index dc20390..9bc232f 100644 --- a/savevm.c +++ b/savevm.c @@ -1574,22 +1574,6 @@ out: return ret; } -/* device can contain snapshots */ -static int bdrv_can_snapshot(BlockDriverState *bs) -{ - return (bs && - !bdrv_is_removable(bs) && - !bdrv_is_read_only(bs)); -} - -/* device must be snapshots in order to have a reliable snapshot */ -static int bdrv_has_snapshot(BlockDriverState *bs) -{ - return (bs && - !bdrv_is_removable(bs) && - !bdrv_is_read_only(bs)); -} - static BlockDriverState *get_bs_snapshots(void) { BlockDriverState *bs; @@ -1599,7 +1583,7 @@ static BlockDriverState *get_bs_snapshots(void) return bs_snapshots; QTAILQ_FOREACH(dinfo, &drives, next) { bs = dinfo->bdrv; - if (bdrv_can_snapshot(bs)) + if (bdrv_can_snapshot(bs) > 0) goto ok; } return NULL; @@ -1642,7 +1626,7 @@ static int del_existing_snapshots(Monitor *mon, const char *name) QTAILQ_FOREACH(dinfo, &drives, next) { bs = dinfo->bdrv; - if (bdrv_can_snapshot(bs) && + if ((bdrv_can_snapshot(bs) > 0) && bdrv_snapshot_find(bs, snapshot, name) >= 0) { ret = bdrv_snapshot_delete(bs, name); @@ -1674,12 +1658,30 @@ void do_savevm(Monitor *mon, const QDict *qdict) #endif const char *name = qdict_get_try_str(qdict, "name"); + /* Verify if there is have a device that doesn't support snapshots and is writable*/ + ret = 0; + QTAILQ_FOREACH(dinfo, &drives, next) { + bs = dinfo->bdrv; + + if (bdrv_is_removable(bs) || bdrv_is_read_only(bs)) { + continue; + } + + if ((ret = bdrv_can_snapshot(bs)) < 0) { + monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n", + bdrv_get_device_name(bs)); + } + } + + if (ret < 0) { + goto the_end; + } + bs = get_bs_snapshots(); if (!bs) { monitor_printf(mon, "No block device can accept snapshots\n"); return; } - /* ??? Should this occur after vm_stop? */ qemu_aio_flush(); @@ -1732,7 +1734,7 @@ void do_savevm(Monitor *mon, const QDict *qdict) QTAILQ_FOREACH(dinfo, &drives, next) { bs1 = dinfo->bdrv; - if (bdrv_has_snapshot(bs1)) { + if (bdrv_can_snapshot(bs1) > 0) { /* Write VM state size only to the image that contains the state */ sn->vm_state_size = (bs == bs1 ? vm_state_size : 0); ret = bdrv_snapshot_create(bs1, sn); @@ -1767,7 +1769,7 @@ int load_vmstate(const char *name) QTAILQ_FOREACH(dinfo, &drives, next) { bs1 = dinfo->bdrv; - if (bdrv_has_snapshot(bs1)) { + if (bdrv_has_snapshot(bs1) > 0) { ret = bdrv_snapshot_goto(bs1, name); if (ret < 0) { switch(ret) { @@ -1829,7 +1831,7 @@ void do_delvm(Monitor *mon, const QDict *qdict) QTAILQ_FOREACH(dinfo, &drives, next) { bs1 = dinfo->bdrv; - if (bdrv_has_snapshot(bs1)) { + if (bdrv_has_snapshot(bs1) > 0) { ret = bdrv_snapshot_delete(bs1, name); if (ret < 0) { if (ret == -ENOTSUP) @@ -1860,7 +1862,7 @@ void do_info_snapshots(Monitor *mon) monitor_printf(mon, "Snapshot devices:"); QTAILQ_FOREACH(dinfo, &drives, next) { bs1 = dinfo->bdrv; - if (bdrv_has_snapshot(bs1)) { + if (bdrv_can_snapshot(bs1) > 0) { if (bs == bs1) monitor_printf(mon, " %s", bdrv_get_device_name(bs1)); }