From patchwork Tue Apr 2 11:47:15 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wayne Xia X-Patchwork-Id: 232971 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 652472C012F for ; Tue, 2 Apr 2013 22:50:39 +1100 (EST) Received: from localhost ([::1]:48193 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UMzjY-0002Xu-P3 for incoming@patchwork.ozlabs.org; Tue, 02 Apr 2013 07:50:36 -0400 Received: from eggs.gnu.org ([208.118.235.92]:49063) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UMzit-0002QQ-SK for qemu-devel@nongnu.org; Tue, 02 Apr 2013 07:49:59 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UMzim-0003Zb-Ms for qemu-devel@nongnu.org; Tue, 02 Apr 2013 07:49:55 -0400 Received: from e28smtp09.in.ibm.com ([122.248.162.9]:52232) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UMzil-0003YC-Jf for qemu-devel@nongnu.org; Tue, 02 Apr 2013 07:49:48 -0400 Received: from /spool/local by e28smtp09.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 2 Apr 2013 17:16:56 +0530 Received: from d28dlp01.in.ibm.com (9.184.220.126) by e28smtp09.in.ibm.com (192.168.1.139) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Tue, 2 Apr 2013 17:16:54 +0530 Received: from d28relay04.in.ibm.com (d28relay04.in.ibm.com [9.184.220.61]) by d28dlp01.in.ibm.com (Postfix) with ESMTP id 1E753E0057 for ; Tue, 2 Apr 2013 17:21:23 +0530 (IST) Received: from d28av01.in.ibm.com (d28av01.in.ibm.com [9.184.220.63]) by d28relay04.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r32BncYT20840500 for ; Tue, 2 Apr 2013 17:19:38 +0530 Received: from d28av01.in.ibm.com (loopback [127.0.0.1]) by d28av01.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id r32Bnf3M001736 for ; Tue, 2 Apr 2013 11:49:41 GMT Received: from RH63Wenchao (wenchaox.cn.ibm.com [9.115.122.29]) by d28av01.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id r32BlY33025943; Tue, 2 Apr 2013 11:49:39 GMT From: Wenchao Xia To: qemu-devel@nongnu.org Date: Tue, 2 Apr 2013 19:47:15 +0800 Message-Id: <1364903250-10429-3-git-send-email-xiawenc@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1364903250-10429-1-git-send-email-xiawenc@linux.vnet.ibm.com> References: <1364903250-10429-1-git-send-email-xiawenc@linux.vnet.ibm.com> X-TM-AS-MML: No X-Content-Scanned: Fidelis XPS MAILER x-cbid: 13040211-2674-0000-0000-000008718E97 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 122.248.162.9 Cc: kwolf@redhat.com, stefanha@gmail.com, armbru@redhat.com, lcapitulino@redhat.com, pbonzini@redhat.com, Wenchao Xia Subject: [Qemu-devel] [PATCH V11 02/17] block: distinguish id and name in bdrv_find_snapshot() 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 To make it clear about id and name in searching, the API is changed a bit to distinguish them, and caller can choose to search by id or name. Searching will be done with higher priority of id. This function also returns negative value from bdrv_snapshot_list() instead of -ENOENT on error now. Signed-off-by: Wenchao Xia Reviewed-by: Eric Blake Reviewed-by: Kevin Wolf --- block/snapshot.c | 46 ++++++++++++++++++++++++++++++++++++++-------- include/block/snapshot.h | 2 +- savevm.c | 10 +++++----- 3 files changed, 44 insertions(+), 14 deletions(-) diff --git a/block/snapshot.c b/block/snapshot.c index c47a899..7b2026c 100644 --- a/block/snapshot.c +++ b/block/snapshot.c @@ -24,8 +24,20 @@ #include "block/snapshot.h" +/* + * Try find an internal snapshot with @id or @name, @id have higher priority + * in searching. + * + * @bs: block device to search on, must not be NULL. + * @sn_info: snapshot information to be filled in, must not be NULL. + * @id: snapshot id to search with, can be NULL. + * @name: snapshot name to search with, can be NULL. + * + * returns 0 and @sn_info is filled with related information if found, + * otherwise it returns negative value. + */ int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info, - const char *name) + const char *id, const char *name) { QEMUSnapshotInfo *sn_tab, *sn; int nb_sns, i, ret; @@ -33,16 +45,34 @@ int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info, ret = -ENOENT; nb_sns = bdrv_snapshot_list(bs, &sn_tab); if (nb_sns < 0) { - return ret; + return nb_sns; } - for (i = 0; i < nb_sns; i++) { - sn = &sn_tab[i]; - if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) { - *sn_info = *sn; - ret = 0; - break; + + /* search by id */ + if (id) { + for (i = 0; i < nb_sns; i++) { + sn = &sn_tab[i]; + if (!strcmp(sn->id_str, id)) { + *sn_info = *sn; + ret = 0; + goto out; + } } } + + /* search by name */ + if (name) { + for (i = 0; i < nb_sns; i++) { + sn = &sn_tab[i]; + if (!strcmp(sn->name, name)) { + *sn_info = *sn; + ret = 0; + goto out; + } + } + } + + out: g_free(sn_tab); return ret; } diff --git a/include/block/snapshot.h b/include/block/snapshot.h index 4ad070c..a047a8e 100644 --- a/include/block/snapshot.h +++ b/include/block/snapshot.h @@ -33,5 +33,5 @@ #include "block.h" int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info, - const char *name); + const char *id, const char *name); #endif diff --git a/savevm.c b/savevm.c index 88acc38..1d2da99 100644 --- a/savevm.c +++ b/savevm.c @@ -2212,7 +2212,7 @@ static int del_existing_snapshots(Monitor *mon, const char *name) bs = NULL; while ((bs = bdrv_next(bs))) { if (bdrv_can_snapshot(bs) && - bdrv_snapshot_find(bs, snapshot, name) >= 0) + bdrv_snapshot_find(bs, snapshot, name, name) >= 0) { ret = bdrv_snapshot_delete(bs, name); if (ret < 0) { @@ -2272,7 +2272,7 @@ void do_savevm(Monitor *mon, const QDict *qdict) sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock); if (name) { - ret = bdrv_snapshot_find(bs, old_sn, name); + ret = bdrv_snapshot_find(bs, old_sn, name, name); if (ret >= 0) { pstrcpy(sn->name, sizeof(sn->name), old_sn->name); pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str); @@ -2363,7 +2363,7 @@ int load_vmstate(const char *name) } /* Don't even try to load empty VM states */ - ret = bdrv_snapshot_find(bs_vm_state, &sn, name); + ret = bdrv_snapshot_find(bs_vm_state, &sn, name, name); if (ret < 0) { return ret; } else if (sn.vm_state_size == 0) { @@ -2387,7 +2387,7 @@ int load_vmstate(const char *name) return -ENOTSUP; } - ret = bdrv_snapshot_find(bs, &sn, name); + ret = bdrv_snapshot_find(bs, &sn, name, name); if (ret < 0) { error_report("Device '%s' does not have the requested snapshot '%s'", bdrv_get_device_name(bs), name); @@ -2493,7 +2493,7 @@ void do_info_snapshots(Monitor *mon, const QDict *qdict) while ((bs1 = bdrv_next(bs1))) { if (bdrv_can_snapshot(bs1) && bs1 != bs) { - ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str); + ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str, NULL); if (ret < 0) { available = 0; break;