2013-05-25 05:09:43 +02:00
|
|
|
/*
|
|
|
|
* Block layer snapshot related functions
|
|
|
|
*
|
|
|
|
* Copyright (c) 2003-2008 Fabrice Bellard
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "block/snapshot.h"
|
|
|
|
#include "block/block_int.h"
|
|
|
|
|
|
|
|
int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
QEMUSnapshotInfo *sn_tab, *sn;
|
|
|
|
int nb_sns, i, ret;
|
|
|
|
|
|
|
|
ret = -ENOENT;
|
|
|
|
nb_sns = bdrv_snapshot_list(bs, &sn_tab);
|
|
|
|
if (nb_sns < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_free(sn_tab);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-09-11 08:04:32 +02:00
|
|
|
/**
|
|
|
|
* Look up an internal snapshot by @id and @name.
|
|
|
|
* @bs: block device to search
|
|
|
|
* @id: unique snapshot ID, or NULL
|
|
|
|
* @name: snapshot name, or NULL
|
|
|
|
* @sn_info: location to store information on the snapshot found
|
|
|
|
* @errp: location to store error, will be set only for exception
|
|
|
|
*
|
|
|
|
* This function will traverse snapshot list in @bs to search the matching
|
|
|
|
* one, @id and @name are the matching condition:
|
|
|
|
* If both @id and @name are specified, find the first one with id @id and
|
|
|
|
* name @name.
|
|
|
|
* If only @id is specified, find the first one with id @id.
|
|
|
|
* If only @name is specified, find the first one with name @name.
|
|
|
|
* if none is specified, abort().
|
|
|
|
*
|
|
|
|
* Returns: true when a snapshot is found and @sn_info will be filled, false
|
|
|
|
* when error or not found. If all operation succeed but no matching one is
|
|
|
|
* found, @errp will NOT be set.
|
|
|
|
*/
|
|
|
|
bool bdrv_snapshot_find_by_id_and_name(BlockDriverState *bs,
|
|
|
|
const char *id,
|
|
|
|
const char *name,
|
|
|
|
QEMUSnapshotInfo *sn_info,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
QEMUSnapshotInfo *sn_tab, *sn;
|
|
|
|
int nb_sns, i;
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
assert(id || name);
|
|
|
|
|
|
|
|
nb_sns = bdrv_snapshot_list(bs, &sn_tab);
|
|
|
|
if (nb_sns < 0) {
|
|
|
|
error_setg_errno(errp, -nb_sns, "Failed to get a snapshot list");
|
|
|
|
return false;
|
|
|
|
} else if (nb_sns == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id && name) {
|
|
|
|
for (i = 0; i < nb_sns; i++) {
|
|
|
|
sn = &sn_tab[i];
|
|
|
|
if (!strcmp(sn->id_str, id) && !strcmp(sn->name, name)) {
|
|
|
|
*sn_info = *sn;
|
|
|
|
ret = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (id) {
|
|
|
|
for (i = 0; i < nb_sns; i++) {
|
|
|
|
sn = &sn_tab[i];
|
|
|
|
if (!strcmp(sn->id_str, id)) {
|
|
|
|
*sn_info = *sn;
|
|
|
|
ret = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (name) {
|
|
|
|
for (i = 0; i < nb_sns; i++) {
|
|
|
|
sn = &sn_tab[i];
|
|
|
|
if (!strcmp(sn->name, name)) {
|
|
|
|
*sn_info = *sn;
|
|
|
|
ret = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(sn_tab);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-05-25 05:09:43 +02:00
|
|
|
int bdrv_can_snapshot(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BlockDriver *drv = bs->drv;
|
|
|
|
if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!drv->bdrv_snapshot_create) {
|
|
|
|
if (bs->file != NULL) {
|
|
|
|
return bdrv_can_snapshot(bs->file);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bdrv_snapshot_create(BlockDriverState *bs,
|
|
|
|
QEMUSnapshotInfo *sn_info)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bdrv_snapshot_goto(BlockDriverState *bs,
|
|
|
|
const char *snapshot_id)
|
|
|
|
{
|
|
|
|
BlockDriver *drv = bs->drv;
|
|
|
|
int ret, open_ret;
|
|
|
|
|
|
|
|
if (!drv) {
|
|
|
|
return -ENOMEDIUM;
|
|
|
|
}
|
|
|
|
if (drv->bdrv_snapshot_goto) {
|
|
|
|
return drv->bdrv_snapshot_goto(bs, snapshot_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bs->file) {
|
|
|
|
drv->bdrv_close(bs);
|
|
|
|
ret = bdrv_snapshot_goto(bs->file, snapshot_id);
|
|
|
|
open_ret = drv->bdrv_open(bs, NULL, bs->open_flags);
|
|
|
|
if (open_ret < 0) {
|
2013-08-23 03:14:47 +02:00
|
|
|
bdrv_unref(bs->file);
|
2013-05-25 05:09:43 +02:00
|
|
|
bs->drv = NULL;
|
|
|
|
return open_ret;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
snapshot: distinguish id and name in snapshot delete
Snapshot creation actually already distinguish id and name since it take
a structured parameter *sn, but delete can't. Later an accurate delete
is needed in qmp_transaction abort and blockdev-snapshot-delete-sync,
so change its prototype. Also *errp is added to tip error, but return
value is kepted to let caller check what kind of error happens. Existing
caller for it are savevm, delvm and qemu-img, they are not impacted by
introducing a new function bdrv_snapshot_delete_by_id_or_name(), which
check the return value and do the operation again.
Before this patch:
For qcow2, it search id first then name to find the one to delete.
For rbd, it search name.
For sheepdog, it does nothing.
After this patch:
For qcow2, logic is the same by call it twice in caller.
For rbd, it always fails in delete with id, but still search for name
in second try, no change to user.
Some code for *errp is based on Pavel's patch.
Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2013-09-11 08:04:33 +02:00
|
|
|
/**
|
|
|
|
* Delete 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, delete the first one with
|
|
|
|
* id @snapshot_id and name @name.
|
|
|
|
* If only @snapshot_id is specified, delete the first one with id
|
|
|
|
* @snapshot_id.
|
|
|
|
* If only @name is specified, delete the first one with name @name.
|
|
|
|
* if none is specified, return -ENINVAL.
|
|
|
|
*
|
|
|
|
* Returns: 0 on success, -errno on failure. If @bs is not inserted, return
|
|
|
|
* -ENOMEDIUM. If @snapshot_id and @name are both NULL, return -EINVAL. If @bs
|
|
|
|
* does not support internal snapshot deletion, return -ENOTSUP. If @bs does
|
|
|
|
* not support parameter @snapshot_id or @name, or one of them is not correctly
|
|
|
|
* specified, return -EINVAL. If @bs can't find one matching @id and @name,
|
|
|
|
* return -ENOENT. If @errp != NULL, it will always be filled with error
|
|
|
|
* message on failure.
|
|
|
|
*/
|
|
|
|
int bdrv_snapshot_delete(BlockDriverState *bs,
|
|
|
|
const char *snapshot_id,
|
|
|
|
const char *name,
|
|
|
|
Error **errp)
|
2013-05-25 05:09:43 +02:00
|
|
|
{
|
|
|
|
BlockDriver *drv = bs->drv;
|
|
|
|
if (!drv) {
|
snapshot: distinguish id and name in snapshot delete
Snapshot creation actually already distinguish id and name since it take
a structured parameter *sn, but delete can't. Later an accurate delete
is needed in qmp_transaction abort and blockdev-snapshot-delete-sync,
so change its prototype. Also *errp is added to tip error, but return
value is kepted to let caller check what kind of error happens. Existing
caller for it are savevm, delvm and qemu-img, they are not impacted by
introducing a new function bdrv_snapshot_delete_by_id_or_name(), which
check the return value and do the operation again.
Before this patch:
For qcow2, it search id first then name to find the one to delete.
For rbd, it search name.
For sheepdog, it does nothing.
After this patch:
For qcow2, logic is the same by call it twice in caller.
For rbd, it always fails in delete with id, but still search for name
in second try, no change to user.
Some code for *errp is based on Pavel's patch.
Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2013-09-11 08:04:33 +02:00
|
|
|
error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, bdrv_get_device_name(bs));
|
2013-05-25 05:09:43 +02:00
|
|
|
return -ENOMEDIUM;
|
|
|
|
}
|
snapshot: distinguish id and name in snapshot delete
Snapshot creation actually already distinguish id and name since it take
a structured parameter *sn, but delete can't. Later an accurate delete
is needed in qmp_transaction abort and blockdev-snapshot-delete-sync,
so change its prototype. Also *errp is added to tip error, but return
value is kepted to let caller check what kind of error happens. Existing
caller for it are savevm, delvm and qemu-img, they are not impacted by
introducing a new function bdrv_snapshot_delete_by_id_or_name(), which
check the return value and do the operation again.
Before this patch:
For qcow2, it search id first then name to find the one to delete.
For rbd, it search name.
For sheepdog, it does nothing.
After this patch:
For qcow2, logic is the same by call it twice in caller.
For rbd, it always fails in delete with id, but still search for name
in second try, no change to user.
Some code for *errp is based on Pavel's patch.
Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2013-09-11 08:04:33 +02:00
|
|
|
if (!snapshot_id && !name) {
|
|
|
|
error_setg(errp, "snapshot_id and name are both NULL");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2013-05-25 05:09:43 +02:00
|
|
|
if (drv->bdrv_snapshot_delete) {
|
snapshot: distinguish id and name in snapshot delete
Snapshot creation actually already distinguish id and name since it take
a structured parameter *sn, but delete can't. Later an accurate delete
is needed in qmp_transaction abort and blockdev-snapshot-delete-sync,
so change its prototype. Also *errp is added to tip error, but return
value is kepted to let caller check what kind of error happens. Existing
caller for it are savevm, delvm and qemu-img, they are not impacted by
introducing a new function bdrv_snapshot_delete_by_id_or_name(), which
check the return value and do the operation again.
Before this patch:
For qcow2, it search id first then name to find the one to delete.
For rbd, it search name.
For sheepdog, it does nothing.
After this patch:
For qcow2, logic is the same by call it twice in caller.
For rbd, it always fails in delete with id, but still search for name
in second try, no change to user.
Some code for *errp is based on Pavel's patch.
Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2013-09-11 08:04:33 +02:00
|
|
|
return drv->bdrv_snapshot_delete(bs, snapshot_id, name, errp);
|
2013-05-25 05:09:43 +02:00
|
|
|
}
|
|
|
|
if (bs->file) {
|
snapshot: distinguish id and name in snapshot delete
Snapshot creation actually already distinguish id and name since it take
a structured parameter *sn, but delete can't. Later an accurate delete
is needed in qmp_transaction abort and blockdev-snapshot-delete-sync,
so change its prototype. Also *errp is added to tip error, but return
value is kepted to let caller check what kind of error happens. Existing
caller for it are savevm, delvm and qemu-img, they are not impacted by
introducing a new function bdrv_snapshot_delete_by_id_or_name(), which
check the return value and do the operation again.
Before this patch:
For qcow2, it search id first then name to find the one to delete.
For rbd, it search name.
For sheepdog, it does nothing.
After this patch:
For qcow2, logic is the same by call it twice in caller.
For rbd, it always fails in delete with id, but still search for name
in second try, no change to user.
Some code for *errp is based on Pavel's patch.
Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2013-09-11 08:04:33 +02:00
|
|
|
return bdrv_snapshot_delete(bs->file, snapshot_id, name, errp);
|
2013-05-25 05:09:43 +02:00
|
|
|
}
|
snapshot: distinguish id and name in snapshot delete
Snapshot creation actually already distinguish id and name since it take
a structured parameter *sn, but delete can't. Later an accurate delete
is needed in qmp_transaction abort and blockdev-snapshot-delete-sync,
so change its prototype. Also *errp is added to tip error, but return
value is kepted to let caller check what kind of error happens. Existing
caller for it are savevm, delvm and qemu-img, they are not impacted by
introducing a new function bdrv_snapshot_delete_by_id_or_name(), which
check the return value and do the operation again.
Before this patch:
For qcow2, it search id first then name to find the one to delete.
For rbd, it search name.
For sheepdog, it does nothing.
After this patch:
For qcow2, logic is the same by call it twice in caller.
For rbd, it always fails in delete with id, but still search for name
in second try, no change to user.
Some code for *errp is based on Pavel's patch.
Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2013-09-11 08:04:33 +02:00
|
|
|
error_set(errp, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
|
|
|
|
drv->format_name, bdrv_get_device_name(bs),
|
|
|
|
"internal snapshot deletion");
|
2013-05-25 05:09:43 +02:00
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
snapshot: distinguish id and name in snapshot delete
Snapshot creation actually already distinguish id and name since it take
a structured parameter *sn, but delete can't. Later an accurate delete
is needed in qmp_transaction abort and blockdev-snapshot-delete-sync,
so change its prototype. Also *errp is added to tip error, but return
value is kepted to let caller check what kind of error happens. Existing
caller for it are savevm, delvm and qemu-img, they are not impacted by
introducing a new function bdrv_snapshot_delete_by_id_or_name(), which
check the return value and do the operation again.
Before this patch:
For qcow2, it search id first then name to find the one to delete.
For rbd, it search name.
For sheepdog, it does nothing.
After this patch:
For qcow2, logic is the same by call it twice in caller.
For rbd, it always fails in delete with id, but still search for name
in second try, no change to user.
Some code for *errp is based on Pavel's patch.
Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2013-09-11 08:04:33 +02:00
|
|
|
void bdrv_snapshot_delete_by_id_or_name(BlockDriverState *bs,
|
|
|
|
const char *id_or_name,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
Error *local_err = NULL;
|
|
|
|
|
|
|
|
ret = bdrv_snapshot_delete(bs, id_or_name, NULL, &local_err);
|
|
|
|
if (ret == -ENOENT || ret == -EINVAL) {
|
|
|
|
error_free(local_err);
|
|
|
|
local_err = NULL;
|
|
|
|
ret = bdrv_snapshot_delete(bs, NULL, id_or_name, &local_err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-25 05:09:43 +02:00
|
|
|
int bdrv_snapshot_list(BlockDriverState *bs,
|
|
|
|
QEMUSnapshotInfo **psn_info)
|
|
|
|
{
|
|
|
|
BlockDriver *drv = bs->drv;
|
|
|
|
if (!drv) {
|
|
|
|
return -ENOMEDIUM;
|
|
|
|
}
|
|
|
|
if (drv->bdrv_snapshot_list) {
|
|
|
|
return drv->bdrv_snapshot_list(bs, psn_info);
|
|
|
|
}
|
|
|
|
if (bs->file) {
|
|
|
|
return bdrv_snapshot_list(bs->file, psn_info);
|
|
|
|
}
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bdrv_snapshot_load_tmp(BlockDriverState *bs,
|
|
|
|
const char *snapshot_name)
|
|
|
|
{
|
|
|
|
BlockDriver *drv = bs->drv;
|
|
|
|
if (!drv) {
|
|
|
|
return -ENOMEDIUM;
|
|
|
|
}
|
|
|
|
if (!bs->read_only) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (drv->bdrv_snapshot_load_tmp) {
|
|
|
|
return drv->bdrv_snapshot_load_tmp(bs, snapshot_name);
|
|
|
|
}
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|