From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47530) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UzSRQ-0002en-5L for qemu-devel@nongnu.org; Wed, 17 Jul 2013 10:10:55 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UzSRH-0004u8-5H for qemu-devel@nongnu.org; Wed, 17 Jul 2013 10:10:52 -0400 Received: from e28smtp07.in.ibm.com ([122.248.162.7]:57747) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UzSRG-0004tI-GL for qemu-devel@nongnu.org; Wed, 17 Jul 2013 10:10:43 -0400 Received: from /spool/local by e28smtp07.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 17 Jul 2013 19:28:32 +0530 Received: from d28relay03.in.ibm.com (d28relay03.in.ibm.com [9.184.220.60]) by d28dlp03.in.ibm.com (Postfix) with ESMTP id B9B971258055 for ; Wed, 17 Jul 2013 19:35:35 +0530 (IST) Received: from d28av05.in.ibm.com (d28av05.in.ibm.com [9.184.220.67]) by d28relay03.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r6HE6w9424641752 for ; Wed, 17 Jul 2013 19:36:58 +0530 Received: from d28av05.in.ibm.com (loopback [127.0.0.1]) by d28av05.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id r6HE6DdN016118 for ; Thu, 18 Jul 2013 00:06:14 +1000 From: Wenchao Xia Date: Wed, 17 Jul 2013 22:03:52 +0800 Message-Id: <1374069835-14287-2-git-send-email-xiawenc@linux.vnet.ibm.com> In-Reply-To: <1374069835-14287-1-git-send-email-xiawenc@linux.vnet.ibm.com> References: <1374069835-14287-1-git-send-email-xiawenc@linux.vnet.ibm.com> Subject: [Qemu-devel] [PATCH 1/4] snapshot: distinguish id and name in load_tmp List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: kwolf@redhat.com, pbonzini@redhat.com, dietmar@proxmox.com, stefanha@redhat.com, Wenchao Xia Since later this function will be used so improve it. The only caller of it now is qemu-img, and it is not impacted by call the function twice to keep old search logic. Signed-off-by: Wenchao Xia --- block/qcow2-snapshot.c | 16 ++++++++++++++-- block/qcow2.h | 5 ++++- block/snapshot.c | 37 +++++++++++++++++++++++++++++++++++-- include/block/block_int.h | 4 +++- include/block/snapshot.h | 4 +++- qemu-img.c | 7 ++++++- 6 files changed, 65 insertions(+), 8 deletions(-) diff --git a/block/qcow2-snapshot.c b/block/qcow2-snapshot.c index 47db6ad..49a02a6 100644 --- a/block/qcow2-snapshot.c +++ b/block/qcow2-snapshot.c @@ -651,7 +651,10 @@ int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab) return s->nb_snapshots; } -int qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name) +int qcow2_snapshot_load_tmp(BlockDriverState *bs, + const char *snapshot_id, + const char *name, + Error **errp) { int i, snapshot_index; BDRVQcowState *s = bs->opaque; @@ -659,12 +662,17 @@ int qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name) uint64_t *new_l1_table; int new_l1_bytes; int ret; + const char *device = bdrv_get_device_name(bs); assert(bs->read_only); /* Search the snapshot */ - snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_name); + snapshot_index = find_snapshot_by_id_and_name(bs, snapshot_id, name); if (snapshot_index < 0) { + error_setg(errp, + "Can't find a snapshot with ID '%s' and name '%s' " + "on device '%s'", + STR_PRINT_CHAR(snapshot_id), STR_PRINT_CHAR(name), device); return -ENOENT; } sn = &s->snapshots[snapshot_index]; @@ -675,6 +683,10 @@ int qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name) ret = bdrv_pread(bs->file, sn->l1_table_offset, new_l1_table, new_l1_bytes); if (ret < 0) { + error_setg(errp, + "Failed to read l1 table for snapshot with ID '%s' and name " + "'%s' on device '%s'", + sn->id_str, sn->name, device); g_free(new_l1_table); return ret; } diff --git a/block/qcow2.h b/block/qcow2.h index 2e6c471..31b9c5c 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -416,7 +416,10 @@ int qcow2_snapshot_delete(BlockDriverState *bs, const char *name, Error **errp); int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab); -int qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name); +int qcow2_snapshot_load_tmp(BlockDriverState *bs, + const char *snapshot_id, + const char *name, + Error **errp); void qcow2_free_snapshots(BlockDriverState *bs); int qcow2_read_snapshots(BlockDriverState *bs); diff --git a/block/snapshot.c b/block/snapshot.c index cf2f4ca..0811f3f 100644 --- a/block/snapshot.c +++ b/block/snapshot.c @@ -243,18 +243,51 @@ int bdrv_snapshot_list(BlockDriverState *bs, return -ENOTSUP; } +/** + * Temporarily load an internal snapshot by @snapshot_id and @name. + * @bs: block device used in the operation + * @snapshot_id: unique snapshot ID, or NULL + * @name: snapshot name, or NULL + * @errp: location to store error + * + * If both @snapshot_id and @name are specified, load the first one with + * id @snapshot_id and name @name. + * If only @snapshot_id is specified, load the first one with id + * @snapshot_id. + * If only @name is specified, load the first one with name @name. + * if none is specified, return -ENINVAL. + * + * Returns: 0 on success, -errno on fail. If @bs is not inserted, return + * -ENOMEDIUM. If @bs is not readonly, return -EINVAL. If @bs did not support + * internal snapshot, return -ENOTSUP. If qemu can't find one matching @id and + * @name, return -ENOENT. If @bs do not support parameter @snapshot_id or + * @name, return -EINVAL. If @errp != NULL, it will always be filled on + * failure. + */ int bdrv_snapshot_load_tmp(BlockDriverState *bs, - const char *snapshot_name) + const char *snapshot_id, + const char *name, + Error **errp) { BlockDriver *drv = bs->drv; + const char *device = bdrv_get_device_name(bs); if (!drv) { + error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); return -ENOMEDIUM; } + if (!snapshot_id && !name) { + error_setg(errp, "snapshot_id and name are both NULL"); + return -EINVAL; + } if (!bs->read_only) { + error_setg(errp, "Device '%s' is not readonly", device); return -EINVAL; } if (drv->bdrv_snapshot_load_tmp) { - return drv->bdrv_snapshot_load_tmp(bs, snapshot_name); + return drv->bdrv_snapshot_load_tmp(bs, snapshot_id, name, errp); } + error_set(errp, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED, + drv->format_name, device, + "temporarily load internal snapshot"); return -ENOTSUP; } diff --git a/include/block/block_int.h b/include/block/block_int.h index 4499b8b..fe44fda 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -173,7 +173,9 @@ struct BlockDriver { int (*bdrv_snapshot_list)(BlockDriverState *bs, QEMUSnapshotInfo **psn_info); int (*bdrv_snapshot_load_tmp)(BlockDriverState *bs, - const char *snapshot_name); + const char *snapshot_id, + const char *name, + Error **errp); int (*bdrv_get_info)(BlockDriverState *bs, BlockDriverInfo *bdi); int (*bdrv_save_vmstate)(BlockDriverState *bs, QEMUIOVector *qiov, diff --git a/include/block/snapshot.h b/include/block/snapshot.h index e47e78d..9903d86 100644 --- a/include/block/snapshot.h +++ b/include/block/snapshot.h @@ -58,5 +58,7 @@ int bdrv_snapshot_delete(BlockDriverState *bs, int bdrv_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_info); int bdrv_snapshot_load_tmp(BlockDriverState *bs, - const char *snapshot_name); + const char *snapshot_id, + const char *name, + Error **errp); #endif diff --git a/qemu-img.c b/qemu-img.c index f4e5818..4f504b0 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -1246,7 +1246,12 @@ static int img_convert(int argc, char **argv) ret = -1; goto out; } - if (bdrv_snapshot_load_tmp(bs[0], snapshot_name) < 0) { + + ret = bdrv_snapshot_load_tmp(bs[0], snapshot_name, NULL, NULL); + if (ret == -ENOENT || ret == -EINVAL) { + ret = bdrv_snapshot_load_tmp(bs[0], NULL, snapshot_name, NULL); + } + if (ret < 0) { error_report("Failed to load snapshot"); ret = -1; goto out; -- 1.7.1