qemu
1/*
2* Block layer snapshot related functions
3*
4* Copyright (c) 2003-2008 Fabrice Bellard
5*
6* Permission is hereby granted, free of charge, to any person obtaining a copy
7* of this software and associated documentation files (the "Software"), to deal
8* in the Software without restriction, including without limitation the rights
9* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10* copies of the Software, and to permit persons to whom the Software is
11* furnished to do so, subject to the following conditions:
12*
13* The above copyright notice and this permission notice shall be included in
14* all copies or substantial portions of the Software.
15*
16* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22* THE SOFTWARE.
23*/
24
25#include "qemu/osdep.h"26#include "block/snapshot.h"27#include "block/block_int.h"28#include "block/qdict.h"29#include "qapi/error.h"30#include "qapi/qmp/qdict.h"31#include "qapi/qmp/qstring.h"32#include "qemu/option.h"33#include "sysemu/block-backend.h"34
35QemuOptsList internal_snapshot_opts = {36.name = "snapshot",37.head = QTAILQ_HEAD_INITIALIZER(internal_snapshot_opts.head),38.desc = {39{40.name = SNAPSHOT_OPT_ID,41.type = QEMU_OPT_STRING,42.help = "snapshot id"43},{44.name = SNAPSHOT_OPT_NAME,45.type = QEMU_OPT_STRING,46.help = "snapshot name"47},{48/* end of list */49}50},51};52
53int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,54const char *name)55{
56QEMUSnapshotInfo *sn_tab, *sn;57int nb_sns, i, ret;58
59GLOBAL_STATE_CODE();60
61ret = -ENOENT;62nb_sns = bdrv_snapshot_list(bs, &sn_tab);63if (nb_sns < 0) {64return ret;65}66for (i = 0; i < nb_sns; i++) {67sn = &sn_tab[i];68if (!strcmp(sn->name, name)) {69*sn_info = *sn;70ret = 0;71break;72}73}74g_free(sn_tab);75return ret;76}
77
78/**
79* Look up an internal snapshot by @id and @name.
80* @bs: block device to search
81* @id: unique snapshot ID, or NULL
82* @name: snapshot name, or NULL
83* @sn_info: location to store information on the snapshot found
84* @errp: location to store error, will be set only for exception
85*
86* This function will traverse snapshot list in @bs to search the matching
87* one, @id and @name are the matching condition:
88* If both @id and @name are specified, find the first one with id @id and
89* name @name.
90* If only @id is specified, find the first one with id @id.
91* If only @name is specified, find the first one with name @name.
92* if none is specified, abort().
93*
94* Returns: true when a snapshot is found and @sn_info will be filled, false
95* when error or not found. If all operation succeed but no matching one is
96* found, @errp will NOT be set.
97*/
98bool bdrv_snapshot_find_by_id_and_name(BlockDriverState *bs,99const char *id,100const char *name,101QEMUSnapshotInfo *sn_info,102Error **errp)103{
104QEMUSnapshotInfo *sn_tab, *sn;105int nb_sns, i;106bool ret = false;107
108assert(id || name);109GLOBAL_STATE_CODE();110
111nb_sns = bdrv_snapshot_list(bs, &sn_tab);112if (nb_sns < 0) {113error_setg_errno(errp, -nb_sns, "Failed to get a snapshot list");114return false;115} else if (nb_sns == 0) {116return false;117}118
119if (id && name) {120for (i = 0; i < nb_sns; i++) {121sn = &sn_tab[i];122if (!strcmp(sn->id_str, id) && !strcmp(sn->name, name)) {123*sn_info = *sn;124ret = true;125break;126}127}128} else if (id) {129for (i = 0; i < nb_sns; i++) {130sn = &sn_tab[i];131if (!strcmp(sn->id_str, id)) {132*sn_info = *sn;133ret = true;134break;135}136}137} else if (name) {138for (i = 0; i < nb_sns; i++) {139sn = &sn_tab[i];140if (!strcmp(sn->name, name)) {141*sn_info = *sn;142ret = true;143break;144}145}146}147
148g_free(sn_tab);149return ret;150}
151
152/**
153* Return a pointer to child of given BDS to which we can fall
154* back if the given BDS does not support snapshots.
155* Return NULL if there is no BDS to (safely) fall back to.
156*/
157static BdrvChild * GRAPH_RDLOCK158bdrv_snapshot_fallback_child(BlockDriverState *bs)159{
160BdrvChild *fallback = bdrv_primary_child(bs);161BdrvChild *child;162
163GLOBAL_STATE_CODE();164assert_bdrv_graph_readable();165
166/* We allow fallback only to primary child */167if (!fallback) {168return NULL;169}170
171/*172* Check that there are no other children that would need to be
173* snapshotted. If there are, it is not safe to fall back to
174* fallback.
175*/
176QLIST_FOREACH(child, &bs->children, next) {177if (child->role & (BDRV_CHILD_DATA | BDRV_CHILD_METADATA |178BDRV_CHILD_FILTERED) &&179child != fallback)180{181return NULL;182}183}184
185return fallback;186}
187
188static BlockDriverState * GRAPH_RDLOCK189bdrv_snapshot_fallback(BlockDriverState *bs)190{
191GLOBAL_STATE_CODE();192return child_bs(bdrv_snapshot_fallback_child(bs));193}
194
195int bdrv_can_snapshot(BlockDriverState *bs)196{
197BlockDriver *drv = bs->drv;198
199GLOBAL_STATE_CODE();200
201if (!drv || !bdrv_is_inserted(bs) || !bdrv_is_writable(bs)) {202return 0;203}204
205if (!drv->bdrv_snapshot_create) {206BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs);207if (fallback_bs) {208return bdrv_can_snapshot(fallback_bs);209}210return 0;211}212
213return 1;214}
215
216int bdrv_snapshot_create(BlockDriverState *bs,217QEMUSnapshotInfo *sn_info)218{
219BlockDriver *drv = bs->drv;220BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs);221
222GLOBAL_STATE_CODE();223
224if (!drv) {225return -ENOMEDIUM;226}227if (drv->bdrv_snapshot_create) {228return drv->bdrv_snapshot_create(bs, sn_info);229}230if (fallback_bs) {231return bdrv_snapshot_create(fallback_bs, sn_info);232}233return -ENOTSUP;234}
235
236int bdrv_snapshot_goto(BlockDriverState *bs,237const char *snapshot_id,238Error **errp)239{
240BlockDriver *drv = bs->drv;241BdrvChild *fallback;242int ret, open_ret;243
244GLOBAL_STATE_CODE();245
246if (!drv) {247error_setg(errp, "Block driver is closed");248return -ENOMEDIUM;249}250
251if (!QLIST_EMPTY(&bs->dirty_bitmaps)) {252error_setg(errp, "Device has active dirty bitmaps");253return -EBUSY;254}255
256if (drv->bdrv_snapshot_goto) {257ret = drv->bdrv_snapshot_goto(bs, snapshot_id);258if (ret < 0) {259error_setg_errno(errp, -ret, "Failed to load snapshot");260}261return ret;262}263
264bdrv_graph_rdlock_main_loop();265fallback = bdrv_snapshot_fallback_child(bs);266bdrv_graph_rdunlock_main_loop();267
268if (fallback) {269QDict *options;270QDict *file_options;271Error *local_err = NULL;272BlockDriverState *fallback_bs = fallback->bs;273char *subqdict_prefix = g_strdup_printf("%s.", fallback->name);274
275options = qdict_clone_shallow(bs->options);276
277/* Prevent it from getting deleted when detached from bs */278bdrv_ref(fallback_bs);279
280qdict_extract_subqdict(options, &file_options, subqdict_prefix);281qobject_unref(file_options);282g_free(subqdict_prefix);283
284/* Force .bdrv_open() below to re-attach fallback_bs on fallback */285qdict_put_str(options, fallback->name,286bdrv_get_node_name(fallback_bs));287
288/* Now close bs, apply the snapshot on fallback_bs, and re-open bs */289if (drv->bdrv_close) {290drv->bdrv_close(bs);291}292
293/* .bdrv_open() will re-attach it */294bdrv_graph_wrlock();295bdrv_unref_child(bs, fallback);296bdrv_graph_wrunlock();297
298ret = bdrv_snapshot_goto(fallback_bs, snapshot_id, errp);299open_ret = drv->bdrv_open(bs, options, bs->open_flags, &local_err);300qobject_unref(options);301if (open_ret < 0) {302bdrv_unref(fallback_bs);303bs->drv = NULL;304/* A bdrv_snapshot_goto() error takes precedence */305error_propagate(errp, local_err);306return ret < 0 ? ret : open_ret;307}308
309/*310* fallback was a primary child. It was closed above and set to NULL,
311* but the .bdrv_open() call has opened it again, because we set the
312* respective option (with the qdict_put_str() call above).
313* Assert that .bdrv_open() has attached the right BDS as primary child.
314*/
315bdrv_graph_rdlock_main_loop();316assert(bdrv_primary_bs(bs) == fallback_bs);317bdrv_graph_rdunlock_main_loop();318
319bdrv_unref(fallback_bs);320return ret;321}322
323error_setg(errp, "Block driver does not support snapshots");324return -ENOTSUP;325}
326
327/**
328* Delete an internal snapshot by @snapshot_id and @name.
329* @bs: block device used in the operation
330* @snapshot_id: unique snapshot ID, or NULL
331* @name: snapshot name, or NULL
332* @errp: location to store error
333*
334* If both @snapshot_id and @name are specified, delete the first one with
335* id @snapshot_id and name @name.
336* If only @snapshot_id is specified, delete the first one with id
337* @snapshot_id.
338* If only @name is specified, delete the first one with name @name.
339* if none is specified, return -EINVAL.
340*
341* Returns: 0 on success, -errno on failure. If @bs is not inserted, return
342* -ENOMEDIUM. If @snapshot_id and @name are both NULL, return -EINVAL. If @bs
343* does not support internal snapshot deletion, return -ENOTSUP. If @bs does
344* not support parameter @snapshot_id or @name, or one of them is not correctly
345* specified, return -EINVAL. If @bs can't find one matching @id and @name,
346* return -ENOENT. If @errp != NULL, it will always be filled with error
347* message on failure.
348*/
349int bdrv_snapshot_delete(BlockDriverState *bs,350const char *snapshot_id,351const char *name,352Error **errp)353{
354BlockDriver *drv = bs->drv;355BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs);356int ret;357
358GLOBAL_STATE_CODE();359
360if (!drv) {361error_setg(errp, "Device '%s' has no medium",362bdrv_get_device_name(bs));363return -ENOMEDIUM;364}365if (!snapshot_id && !name) {366error_setg(errp, "snapshot_id and name are both NULL");367return -EINVAL;368}369
370/* drain all pending i/o before deleting snapshot */371bdrv_drained_begin(bs);372
373if (drv->bdrv_snapshot_delete) {374ret = drv->bdrv_snapshot_delete(bs, snapshot_id, name, errp);375} else if (fallback_bs) {376ret = bdrv_snapshot_delete(fallback_bs, snapshot_id, name, errp);377} else {378error_setg(errp, "Block format '%s' used by device '%s' "379"does not support internal snapshot deletion",380drv->format_name, bdrv_get_device_name(bs));381ret = -ENOTSUP;382}383
384bdrv_drained_end(bs);385return ret;386}
387
388int bdrv_snapshot_list(BlockDriverState *bs,389QEMUSnapshotInfo **psn_info)390{
391GLOBAL_STATE_CODE();392GRAPH_RDLOCK_GUARD_MAINLOOP();393
394BlockDriver *drv = bs->drv;395BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs);396
397if (!drv) {398return -ENOMEDIUM;399}400if (drv->bdrv_snapshot_list) {401return drv->bdrv_snapshot_list(bs, psn_info);402}403if (fallback_bs) {404return bdrv_snapshot_list(fallback_bs, psn_info);405}406return -ENOTSUP;407}
408
409/**
410* Temporarily load an internal snapshot by @snapshot_id and @name.
411* @bs: block device used in the operation
412* @snapshot_id: unique snapshot ID, or NULL
413* @name: snapshot name, or NULL
414* @errp: location to store error
415*
416* If both @snapshot_id and @name are specified, load the first one with
417* id @snapshot_id and name @name.
418* If only @snapshot_id is specified, load the first one with id
419* @snapshot_id.
420* If only @name is specified, load the first one with name @name.
421* if none is specified, return -EINVAL.
422*
423* Returns: 0 on success, -errno on fail. If @bs is not inserted, return
424* -ENOMEDIUM. If @bs is not readonly, return -EINVAL. If @bs did not support
425* internal snapshot, return -ENOTSUP. If qemu can't find a matching @id and
426* @name, return -ENOENT. If @errp != NULL, it will always be filled on
427* failure.
428*/
429int bdrv_snapshot_load_tmp(BlockDriverState *bs,430const char *snapshot_id,431const char *name,432Error **errp)433{
434BlockDriver *drv = bs->drv;435
436GLOBAL_STATE_CODE();437GRAPH_RDLOCK_GUARD_MAINLOOP();438
439if (!drv) {440error_setg(errp, "Device '%s' has no medium",441bdrv_get_device_name(bs));442return -ENOMEDIUM;443}444if (!snapshot_id && !name) {445error_setg(errp, "snapshot_id and name are both NULL");446return -EINVAL;447}448if (!bdrv_is_read_only(bs)) {449error_setg(errp, "Device is not readonly");450return -EINVAL;451}452if (drv->bdrv_snapshot_load_tmp) {453return drv->bdrv_snapshot_load_tmp(bs, snapshot_id, name, errp);454}455error_setg(errp, "Block format '%s' used by device '%s' "456"does not support temporarily loading internal snapshots",457drv->format_name, bdrv_get_device_name(bs));458return -ENOTSUP;459}
460
461int bdrv_snapshot_load_tmp_by_id_or_name(BlockDriverState *bs,462const char *id_or_name,463Error **errp)464{
465int ret;466Error *local_err = NULL;467
468GLOBAL_STATE_CODE();469
470ret = bdrv_snapshot_load_tmp(bs, id_or_name, NULL, &local_err);471if (ret == -ENOENT || ret == -EINVAL) {472error_free(local_err);473local_err = NULL;474ret = bdrv_snapshot_load_tmp(bs, NULL, id_or_name, &local_err);475}476
477error_propagate(errp, local_err);478
479return ret;480}
481
482
483static int GRAPH_RDLOCK484bdrv_all_get_snapshot_devices(bool has_devices, strList *devices,485GList **all_bdrvs, Error **errp)486{
487g_autoptr(GList) bdrvs = NULL;488
489if (has_devices) {490if (!devices) {491error_setg(errp, "At least one device is required for snapshot");492return -1;493}494
495while (devices) {496BlockDriverState *bs = bdrv_find_node(devices->value);497if (!bs) {498error_setg(errp, "No block device node '%s'", devices->value);499return -1;500}501bdrvs = g_list_append(bdrvs, bs);502devices = devices->next;503}504} else {505BlockDriverState *bs;506BdrvNextIterator it;507for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {508bdrvs = g_list_append(bdrvs, bs);509}510}511
512*all_bdrvs = g_steal_pointer(&bdrvs);513return 0;514}
515
516
517static bool GRAPH_RDLOCK bdrv_all_snapshots_includes_bs(BlockDriverState *bs)518{
519GLOBAL_STATE_CODE();520assert_bdrv_graph_readable();521
522if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {523return false;524}525
526/* Include all nodes that are either in use by a BlockBackend, or that527* aren't attached to any node, but owned by the monitor. */
528return bdrv_has_blk(bs) || QLIST_EMPTY(&bs->parents);529}
530
531/* Group operations. All block drivers are involved. */
532
533bool bdrv_all_can_snapshot(bool has_devices, strList *devices,534Error **errp)535{
536g_autoptr(GList) bdrvs = NULL;537GList *iterbdrvs;538
539GLOBAL_STATE_CODE();540GRAPH_RDLOCK_GUARD_MAINLOOP();541
542if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {543return false;544}545
546iterbdrvs = bdrvs;547while (iterbdrvs) {548BlockDriverState *bs = iterbdrvs->data;549bool ok = true;550
551if (devices || bdrv_all_snapshots_includes_bs(bs)) {552ok = bdrv_can_snapshot(bs);553}554if (!ok) {555error_setg(errp, "Device '%s' is writable but does not support "556"snapshots", bdrv_get_device_or_node_name(bs));557return false;558}559
560iterbdrvs = iterbdrvs->next;561}562
563return true;564}
565
566int bdrv_all_delete_snapshot(const char *name,567bool has_devices, strList *devices,568Error **errp)569{
570ERRP_GUARD();571g_autoptr(GList) bdrvs = NULL;572GList *iterbdrvs;573
574GLOBAL_STATE_CODE();575GRAPH_RDLOCK_GUARD_MAINLOOP();576
577if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {578return -1;579}580
581iterbdrvs = bdrvs;582while (iterbdrvs) {583BlockDriverState *bs = iterbdrvs->data;584QEMUSnapshotInfo sn1, *snapshot = &sn1;585int ret = 0;586
587if ((devices || bdrv_all_snapshots_includes_bs(bs)) &&588bdrv_snapshot_find(bs, snapshot, name) >= 0)589{590ret = bdrv_snapshot_delete(bs, snapshot->id_str,591snapshot->name, errp);592}593if (ret < 0) {594error_prepend(errp, "Could not delete snapshot '%s' on '%s': ",595name, bdrv_get_device_or_node_name(bs));596return -1;597}598
599iterbdrvs = iterbdrvs->next;600}601
602return 0;603}
604
605
606int bdrv_all_goto_snapshot(const char *name,607bool has_devices, strList *devices,608Error **errp)609{
610ERRP_GUARD();611g_autoptr(GList) bdrvs = NULL;612GList *iterbdrvs;613int ret;614
615GLOBAL_STATE_CODE();616
617bdrv_graph_rdlock_main_loop();618ret = bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp);619bdrv_graph_rdunlock_main_loop();620
621if (ret < 0) {622return -1;623}624
625iterbdrvs = bdrvs;626while (iterbdrvs) {627BlockDriverState *bs = iterbdrvs->data;628bool all_snapshots_includes_bs;629
630bdrv_graph_rdlock_main_loop();631all_snapshots_includes_bs = bdrv_all_snapshots_includes_bs(bs);632bdrv_graph_rdunlock_main_loop();633
634ret = (devices || all_snapshots_includes_bs) ?635bdrv_snapshot_goto(bs, name, errp) : 0;636if (ret < 0) {637bdrv_graph_rdlock_main_loop();638error_prepend(errp, "Could not load snapshot '%s' on '%s': ",639name, bdrv_get_device_or_node_name(bs));640bdrv_graph_rdunlock_main_loop();641return -1;642}643
644iterbdrvs = iterbdrvs->next;645}646
647return 0;648}
649
650int bdrv_all_has_snapshot(const char *name,651bool has_devices, strList *devices,652Error **errp)653{
654g_autoptr(GList) bdrvs = NULL;655GList *iterbdrvs;656
657GLOBAL_STATE_CODE();658GRAPH_RDLOCK_GUARD_MAINLOOP();659
660if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {661return -1;662}663
664iterbdrvs = bdrvs;665while (iterbdrvs) {666BlockDriverState *bs = iterbdrvs->data;667QEMUSnapshotInfo sn;668int ret = 0;669
670if (devices || bdrv_all_snapshots_includes_bs(bs)) {671ret = bdrv_snapshot_find(bs, &sn, name);672}673if (ret < 0) {674if (ret == -ENOENT) {675return 0;676} else {677error_setg_errno(errp, errno,678"Could not check snapshot '%s' on '%s'",679name, bdrv_get_device_or_node_name(bs));680return -1;681}682}683
684iterbdrvs = iterbdrvs->next;685}686
687return 1;688}
689
690int bdrv_all_create_snapshot(QEMUSnapshotInfo *sn,691BlockDriverState *vm_state_bs,692uint64_t vm_state_size,693bool has_devices, strList *devices,694Error **errp)695{
696g_autoptr(GList) bdrvs = NULL;697GList *iterbdrvs;698
699GLOBAL_STATE_CODE();700GRAPH_RDLOCK_GUARD_MAINLOOP();701
702if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {703return -1;704}705
706iterbdrvs = bdrvs;707while (iterbdrvs) {708BlockDriverState *bs = iterbdrvs->data;709int ret = 0;710
711if (bs == vm_state_bs) {712sn->vm_state_size = vm_state_size;713ret = bdrv_snapshot_create(bs, sn);714} else if (devices || bdrv_all_snapshots_includes_bs(bs)) {715sn->vm_state_size = 0;716ret = bdrv_snapshot_create(bs, sn);717}718if (ret < 0) {719error_setg(errp, "Could not create snapshot '%s' on '%s'",720sn->name, bdrv_get_device_or_node_name(bs));721return -1;722}723
724iterbdrvs = iterbdrvs->next;725}726
727return 0;728}
729
730
731BlockDriverState *bdrv_all_find_vmstate_bs(const char *vmstate_bs,732bool has_devices, strList *devices,733Error **errp)734{
735g_autoptr(GList) bdrvs = NULL;736GList *iterbdrvs;737
738GLOBAL_STATE_CODE();739GRAPH_RDLOCK_GUARD_MAINLOOP();740
741if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {742return NULL;743}744
745iterbdrvs = bdrvs;746while (iterbdrvs) {747BlockDriverState *bs = iterbdrvs->data;748bool found = false;749
750found = (devices || bdrv_all_snapshots_includes_bs(bs)) &&751bdrv_can_snapshot(bs);752
753if (vmstate_bs) {754if (g_str_equal(vmstate_bs,755bdrv_get_node_name(bs))) {756if (found) {757return bs;758} else {759error_setg(errp,760"vmstate block device '%s' does not support snapshots",761vmstate_bs);762return NULL;763}764}765} else if (found) {766return bs;767}768
769iterbdrvs = iterbdrvs->next;770}771
772if (vmstate_bs) {773error_setg(errp,774"vmstate block device '%s' does not exist", vmstate_bs);775} else {776error_setg(errp,777"no block device can store vmstate for snapshot");778}779return NULL;780}
781