From patchwork Tue Apr 16 16:05:20 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Hrdina X-Patchwork-Id: 237026 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 994112C011C for ; Wed, 17 Apr 2013 02:11:20 +1000 (EST) Received: from localhost ([::1]:36016 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1US8TW-00059O-Qu for incoming@patchwork.ozlabs.org; Tue, 16 Apr 2013 12:11:18 -0400 Received: from eggs.gnu.org ([208.118.235.92]:39621) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1US8OB-0005na-ON for qemu-devel@nongnu.org; Tue, 16 Apr 2013 12:05:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1US8O6-0006vd-7S for qemu-devel@nongnu.org; Tue, 16 Apr 2013 12:05:47 -0400 Received: from mx1.redhat.com ([209.132.183.28]:55172) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1US8O5-0006vV-Vr for qemu-devel@nongnu.org; Tue, 16 Apr 2013 12:05:42 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r3GG5fGZ022681 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Tue, 16 Apr 2013 12:05:41 -0400 Received: from localhost.localdomain.com ([10.34.250.241]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r3GG5OMS019439; Tue, 16 Apr 2013 12:05:39 -0400 From: Pavel Hrdina To: qemu-devel@nongnu.org Date: Tue, 16 Apr 2013 18:05:20 +0200 Message-Id: In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: phrdina@redhat.com, armbru@redhat.com, lcapitulino@redhat.com Subject: [Qemu-devel] [PATCH 08/11] block: update error reporting for bdrv_snapshot_create() and related functions 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 Signed-off-by: Pavel Hrdina Reviewed-by: Eric Blake --- block.c | 23 ++++++++++++++--------- block/qcow2-snapshot.c | 18 ++++++++++++------ block/qcow2.h | 4 +++- block/rbd.c | 20 +++++++++++--------- block/sheepdog.c | 22 +++++++++++----------- include/block/block.h | 5 +++-- include/block/block_int.h | 5 +++-- qemu-img.c | 8 +++----- savevm.c | 9 +++++---- 9 files changed, 65 insertions(+), 49 deletions(-) diff --git a/block.c b/block.c index a760a2f..7cf24f5 100644 --- a/block.c +++ b/block.c @@ -3352,17 +3352,22 @@ BlockDriverState *bdrv_snapshots(void) return NULL; } -int bdrv_snapshot_create(BlockDriverState *bs, - QEMUSnapshotInfo *sn_info) +void bdrv_snapshot_create(BlockDriverState *bs, + QEMUSnapshotInfo *sn_info, + Error **errp) { BlockDriver *drv = bs->drv; - if (!drv) - return -ENOMEDIUM; - if (drv->bdrv_snapshot_create) - return drv->bdrv_snapshot_create(bs, sn_info); - if (bs->file) - return bdrv_snapshot_create(bs->file, sn_info); - return -ENOTSUP; + + if (!drv) { + error_setg(errp, "device '%s' has no medium", bdrv_get_device_name(bs)); + } else if (drv->bdrv_snapshot_create) { + drv->bdrv_snapshot_create(bs, sn_info, errp); + } else if (bs->file) { + bdrv_snapshot_create(bs->file, sn_info, errp); + } else { + error_setg(errp, "snapshots are not supported on device '%s'", + bdrv_get_device_name(bs)); + } } void bdrv_snapshot_goto(BlockDriverState *bs, diff --git a/block/qcow2-snapshot.c b/block/qcow2-snapshot.c index 4a69b88..bb6a35d 100644 --- a/block/qcow2-snapshot.c +++ b/block/qcow2-snapshot.c @@ -315,7 +315,9 @@ static int find_snapshot_by_id_or_name(BlockDriverState *bs, const char *name) } /* if no id is provided, a new one is constructed */ -int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info) +void qcow2_snapshot_create(BlockDriverState *bs, + QEMUSnapshotInfo *sn_info, + Error **errp) { BDRVQcowState *s = bs->opaque; QCowSnapshot *new_snapshot_list = NULL; @@ -334,7 +336,8 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info) /* Check that the ID is unique */ if (find_snapshot_by_id(bs, sn_info->id_str) >= 0) { - return -EEXIST; + error_setg(errp, "qcow2: parameter 'id' has to be unique ID"); + return; } /* Populate sn with passed data */ @@ -350,7 +353,8 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info) /* Allocate the L1 table of the snapshot and copy the current one there. */ l1_table_offset = qcow2_alloc_clusters(bs, s->l1_size * sizeof(uint64_t)); if (l1_table_offset < 0) { - ret = l1_table_offset; + error_setg_errno(errp, -l1_table_offset, + "qcow2: failed to allocate L1 table"); goto fail; } @@ -365,6 +369,7 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info) ret = bdrv_pwrite(bs->file, sn->l1_table_offset, l1_table, s->l1_size * sizeof(uint64_t)); if (ret < 0) { + error_setg_errno(errp, -ret, "qcow2: failed to save L1 table"); goto fail; } @@ -378,6 +383,8 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info) */ ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1); if (ret < 0) { + error_setg_errno(errp, -ret, + "qcow2: failed to update snapshot refcount"); goto fail; } @@ -395,6 +402,7 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info) if (ret < 0) { g_free(s->snapshots); s->snapshots = old_snapshot_list; + error_setg_errno(errp, -ret, "qcow2: failed to write new snapshot"); goto fail; } @@ -406,14 +414,12 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info) qcow2_check_refcounts(bs, &result, 0); } #endif - return 0; + return; fail: g_free(sn->id_str); g_free(sn->name); g_free(l1_table); - - return ret; } /* copy the snapshot 'snapshot_name' into the current disk image */ diff --git a/block/qcow2.h b/block/qcow2.h index ae62953..c194cff 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -382,7 +382,9 @@ int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset, int qcow2_zero_clusters(BlockDriverState *bs, uint64_t offset, int nb_sectors); /* qcow2-snapshot.c functions */ -int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info); +void qcow2_snapshot_create(BlockDriverState *bs, + QEMUSnapshotInfo *sn_info, + Error **errp); void qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id, Error **errp); diff --git a/block/rbd.c b/block/rbd.c index 9259621..1f073fe 100644 --- a/block/rbd.c +++ b/block/rbd.c @@ -839,14 +839,16 @@ static int qemu_rbd_truncate(BlockDriverState *bs, int64_t offset) return 0; } -static int qemu_rbd_snap_create(BlockDriverState *bs, - QEMUSnapshotInfo *sn_info) +static void qemu_rbd_snap_create(BlockDriverState *bs, + QEMUSnapshotInfo *sn_info, + Error **errp) { BDRVRBDState *s = bs->opaque; int r; if (sn_info->name[0] == '\0') { - return -EINVAL; /* we need a name for rbd snapshots */ + error_setg(errp, "rbd: parameter 'name' cannot be empty"); + return; } /* @@ -855,20 +857,20 @@ static int qemu_rbd_snap_create(BlockDriverState *bs, */ if (sn_info->id_str[0] != '\0' && strcmp(sn_info->id_str, sn_info->name) != 0) { - return -EINVAL; + error_setg(errp, "rbd: ID and name have to be equal"); + return; } if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) { - return -ERANGE; + error_setg(errp, "rbd: parameter 'name' has to be shorter than %zd " + "chars", sizeof(sn_info->id_str)); + return; } r = rbd_snap_create(s->image, sn_info->name); if (r < 0) { - error_report("failed to create snap: %s", strerror(-r)); - return r; + error_setg_errno(errp, -r, "rbd: failed to create snapshot"); } - - return 0; } static void qemu_rbd_snap_remove(BlockDriverState *bs, diff --git a/block/sheepdog.c b/block/sheepdog.c index 3d44575..996441d 100644 --- a/block/sheepdog.c +++ b/block/sheepdog.c @@ -1767,7 +1767,9 @@ static int coroutine_fn sd_co_flush_to_disk(BlockDriverState *bs) return acb->ret; } -static int sd_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info) +static void sd_snapshot_create(BlockDriverState *bs, + QEMUSnapshotInfo *sn_info, + Error **errp) { BDRVSheepdogState *s = bs->opaque; int ret, fd; @@ -1780,10 +1782,10 @@ static int sd_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info) s->name, sn_info->vm_state_size, s->is_snapshot); if (s->is_snapshot) { - error_report("You can't create a snapshot of a snapshot VDI, " - "%s (%" PRIu32 ").", s->name, s->inode.vdi_id); - - return -EINVAL; + error_setg(errp, "sd: you can't create a snapshot '%s' of a snapshot " + "VDI %s (%" PRIu32 ")", sn_info->name, s->name, + s->inode.vdi_id); + return; } dprintf("%s %s\n", sn_info->name, sn_info->id_str); @@ -1800,22 +1802,21 @@ static int sd_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info) /* refresh inode. */ fd = connect_to_sdog(s); if (fd < 0) { - ret = fd; + error_setg_errno(errp, -fd, "sd: failed to connect to sdog"); goto cleanup; } ret = write_object(fd, (char *)&s->inode, vid_to_vdi_oid(s->inode.vdi_id), s->inode.nr_copies, datalen, 0, false, s->cache_flags); if (ret < 0) { - error_report("failed to write snapshot's inode."); + error_setg_errno(errp, -ret, "sd: failed to write snapshot's inode"); goto cleanup; } ret = do_sd_create(s, s->name, s->inode.vdi_size, s->inode.vdi_id, &new_vid, 1); if (ret < 0) { - error_report("failed to create inode for snapshot. %s", - strerror(errno)); + error_setg_errno(errp, -ret, "sd: failed to create inode for snapshot"); goto cleanup; } @@ -1825,7 +1826,7 @@ static int sd_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info) s->inode.nr_copies, datalen, 0, s->cache_flags); if (ret < 0) { - error_report("failed to read new inode info. %s", strerror(errno)); + error_setg_errno(errp, -ret, "sd: failed to read new inode info"); goto cleanup; } @@ -1835,7 +1836,6 @@ static int sd_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info) cleanup: closesocket(fd); - return ret; } static void sd_snapshot_goto(BlockDriverState *bs, diff --git a/include/block/block.h b/include/block/block.h index 33d498b..fb5a864 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -333,8 +333,9 @@ BlockStats *bdrv_query_stats(const BlockDriverState *bs); int bdrv_can_snapshot(BlockDriverState *bs); int bdrv_is_snapshot(BlockDriverState *bs); BlockDriverState *bdrv_snapshots(void); -int bdrv_snapshot_create(BlockDriverState *bs, - QEMUSnapshotInfo *sn_info); +void bdrv_snapshot_create(BlockDriverState *bs, + QEMUSnapshotInfo *sn_info, + Error **errp); void bdrv_snapshot_goto(BlockDriverState *bs, const char *snapshot_id, Error **errp); diff --git a/include/block/block_int.h b/include/block/block_int.h index c56d923..9903874 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -153,8 +153,9 @@ struct BlockDriver { int (*bdrv_write_compressed)(BlockDriverState *bs, int64_t sector_num, const uint8_t *buf, int nb_sectors); - int (*bdrv_snapshot_create)(BlockDriverState *bs, - QEMUSnapshotInfo *sn_info); + void (*bdrv_snapshot_create)(BlockDriverState *bs, + QEMUSnapshotInfo *sn_info, + Error **errp); void (*bdrv_snapshot_goto)(BlockDriverState *bs, const char *snapshot_id, Error **errp); diff --git a/qemu-img.c b/qemu-img.c index 06cd8af..2e420f3 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -2024,11 +2024,9 @@ static int img_snapshot(int argc, char **argv) sn.date_sec = tv.tv_sec; sn.date_nsec = tv.tv_usec * 1000; - ret = bdrv_snapshot_create(bs, &sn); - if (ret) { - error_report("Could not create snapshot '%s': %d (%s)", - snapshot_name, ret, strerror(-ret)); - } + bdrv_snapshot_create(bs, &sn, &local_err); + ret = qemu_img_handle_error("qemu-img snapshot create failed", + local_err); break; case SNAPSHOT_APPLY: diff --git a/savevm.c b/savevm.c index 1258f5f..a8e7d60 100644 --- a/savevm.c +++ b/savevm.c @@ -2283,6 +2283,7 @@ void do_savevm(Monitor *mon, const QDict *qdict) qemu_timeval tv; struct tm tm; const char *name = qdict_get_try_str(qdict, "name"); + Error *local_err = NULL; /* Verify if there is a device that doesn't support snapshots and is writable */ bs = NULL; @@ -2355,10 +2356,10 @@ void do_savevm(Monitor *mon, const QDict *qdict) if (bdrv_can_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_snapshot_create(bs1, sn, &local_err); + if (error_is_set(&local_err)) { + monitor_printf(mon, "'%s'\n", error_get_pretty(local_err)); + error_free(local_err); } } }