]> git.proxmox.com Git - mirror_qemu.git/blame - block/snapshot.c
Merge remote-tracking branch 'remotes/kevin/tags/for-upstream' into staging
[mirror_qemu.git] / block / snapshot.c
CommitLineData
de08c606
WX
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
80c71a24 25#include "qemu/osdep.h"
de08c606
WX
26#include "block/snapshot.h"
27#include "block/block_int.h"
609f45ea 28#include "block/qdict.h"
da34e65c 29#include "qapi/error.h"
452fcdbc 30#include "qapi/qmp/qdict.h"
cc7a8ea7 31#include "qapi/qmp/qerror.h"
7a9e5119 32#include "qapi/qmp/qstring.h"
922a01a0 33#include "qemu/option.h"
05f4aced 34#include "sysemu/block-backend.h"
de08c606 35
8c116b0e
WX
36QemuOptsList internal_snapshot_opts = {
37 .name = "snapshot",
38 .head = QTAILQ_HEAD_INITIALIZER(internal_snapshot_opts.head),
39 .desc = {
40 {
41 .name = SNAPSHOT_OPT_ID,
42 .type = QEMU_OPT_STRING,
43 .help = "snapshot id"
44 },{
45 .name = SNAPSHOT_OPT_NAME,
46 .type = QEMU_OPT_STRING,
47 .help = "snapshot name"
48 },{
49 /* end of list */
50 }
51 },
52};
53
de08c606
WX
54int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
55 const char *name)
56{
57 QEMUSnapshotInfo *sn_tab, *sn;
58 int nb_sns, i, ret;
59
60 ret = -ENOENT;
61 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
62 if (nb_sns < 0) {
63 return ret;
64 }
65 for (i = 0; i < nb_sns; i++) {
66 sn = &sn_tab[i];
6ca08045 67 if (!strcmp(sn->name, name)) {
de08c606
WX
68 *sn_info = *sn;
69 ret = 0;
70 break;
71 }
72 }
73 g_free(sn_tab);
74 return ret;
75}
76
2ea1dd75
WX
77/**
78 * Look up an internal snapshot by @id and @name.
79 * @bs: block device to search
80 * @id: unique snapshot ID, or NULL
81 * @name: snapshot name, or NULL
82 * @sn_info: location to store information on the snapshot found
83 * @errp: location to store error, will be set only for exception
84 *
85 * This function will traverse snapshot list in @bs to search the matching
86 * one, @id and @name are the matching condition:
87 * If both @id and @name are specified, find the first one with id @id and
88 * name @name.
89 * If only @id is specified, find the first one with id @id.
90 * If only @name is specified, find the first one with name @name.
91 * if none is specified, abort().
92 *
93 * Returns: true when a snapshot is found and @sn_info will be filled, false
94 * when error or not found. If all operation succeed but no matching one is
95 * found, @errp will NOT be set.
96 */
97bool bdrv_snapshot_find_by_id_and_name(BlockDriverState *bs,
98 const char *id,
99 const char *name,
100 QEMUSnapshotInfo *sn_info,
101 Error **errp)
102{
103 QEMUSnapshotInfo *sn_tab, *sn;
104 int nb_sns, i;
105 bool ret = false;
106
107 assert(id || name);
108
109 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
110 if (nb_sns < 0) {
111 error_setg_errno(errp, -nb_sns, "Failed to get a snapshot list");
112 return false;
113 } else if (nb_sns == 0) {
114 return false;
115 }
116
117 if (id && name) {
118 for (i = 0; i < nb_sns; i++) {
119 sn = &sn_tab[i];
120 if (!strcmp(sn->id_str, id) && !strcmp(sn->name, name)) {
121 *sn_info = *sn;
122 ret = true;
123 break;
124 }
125 }
126 } else if (id) {
127 for (i = 0; i < nb_sns; i++) {
128 sn = &sn_tab[i];
129 if (!strcmp(sn->id_str, id)) {
130 *sn_info = *sn;
131 ret = true;
132 break;
133 }
134 }
135 } else if (name) {
136 for (i = 0; i < nb_sns; i++) {
137 sn = &sn_tab[i];
138 if (!strcmp(sn->name, name)) {
139 *sn_info = *sn;
140 ret = true;
141 break;
142 }
143 }
144 }
145
146 g_free(sn_tab);
147 return ret;
148}
149
de08c606
WX
150int bdrv_can_snapshot(BlockDriverState *bs)
151{
152 BlockDriver *drv = bs->drv;
153 if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
154 return 0;
155 }
156
157 if (!drv->bdrv_snapshot_create) {
158 if (bs->file != NULL) {
9a4f4c31 159 return bdrv_can_snapshot(bs->file->bs);
de08c606
WX
160 }
161 return 0;
162 }
163
164 return 1;
165}
166
167int bdrv_snapshot_create(BlockDriverState *bs,
168 QEMUSnapshotInfo *sn_info)
169{
170 BlockDriver *drv = bs->drv;
171 if (!drv) {
172 return -ENOMEDIUM;
173 }
174 if (drv->bdrv_snapshot_create) {
175 return drv->bdrv_snapshot_create(bs, sn_info);
176 }
177 if (bs->file) {
9a4f4c31 178 return bdrv_snapshot_create(bs->file->bs, sn_info);
de08c606
WX
179 }
180 return -ENOTSUP;
181}
182
183int bdrv_snapshot_goto(BlockDriverState *bs,
0b62bcbc
KW
184 const char *snapshot_id,
185 Error **errp)
de08c606
WX
186{
187 BlockDriver *drv = bs->drv;
188 int ret, open_ret;
189
190 if (!drv) {
0b62bcbc 191 error_setg(errp, "Block driver is closed");
de08c606
WX
192 return -ENOMEDIUM;
193 }
04dec3c3 194
70a5afed
KW
195 if (!QLIST_EMPTY(&bs->dirty_bitmaps)) {
196 error_setg(errp, "Device has active dirty bitmaps");
197 return -EBUSY;
04dec3c3 198 }
04dec3c3 199
de08c606 200 if (drv->bdrv_snapshot_goto) {
0b62bcbc
KW
201 ret = drv->bdrv_snapshot_goto(bs, snapshot_id);
202 if (ret < 0) {
203 error_setg_errno(errp, -ret, "Failed to load snapshot");
204 }
205 return ret;
de08c606
WX
206 }
207
208 if (bs->file) {
7a9e5119
DJS
209 BlockDriverState *file;
210 QDict *options = qdict_clone_shallow(bs->options);
211 QDict *file_options;
0b62bcbc 212 Error *local_err = NULL;
7a9e5119
DJS
213
214 file = bs->file->bs;
215 /* Prevent it from getting deleted when detached from bs */
216 bdrv_ref(file);
217
218 qdict_extract_subqdict(options, &file_options, "file.");
cb3e7f08 219 qobject_unref(file_options);
46f5ac20 220 qdict_put_str(options, "file", bdrv_get_node_name(file));
7a9e5119 221
3c005293
VSO
222 if (drv->bdrv_close) {
223 drv->bdrv_close(bs);
224 }
7a9e5119
DJS
225 bdrv_unref_child(bs, bs->file);
226 bs->file = NULL;
227
0b62bcbc
KW
228 ret = bdrv_snapshot_goto(file, snapshot_id, errp);
229 open_ret = drv->bdrv_open(bs, options, bs->open_flags, &local_err);
cb3e7f08 230 qobject_unref(options);
de08c606 231 if (open_ret < 0) {
7a9e5119 232 bdrv_unref(file);
de08c606 233 bs->drv = NULL;
0b62bcbc
KW
234 /* A bdrv_snapshot_goto() error takes precedence */
235 error_propagate(errp, local_err);
236 return ret < 0 ? ret : open_ret;
de08c606 237 }
7a9e5119
DJS
238
239 assert(bs->file->bs == file);
240 bdrv_unref(file);
de08c606
WX
241 return ret;
242 }
243
0b62bcbc 244 error_setg(errp, "Block driver does not support snapshots");
de08c606
WX
245 return -ENOTSUP;
246}
247
a89d89d3
WX
248/**
249 * Delete an internal snapshot by @snapshot_id and @name.
250 * @bs: block device used in the operation
251 * @snapshot_id: unique snapshot ID, or NULL
252 * @name: snapshot name, or NULL
253 * @errp: location to store error
254 *
255 * If both @snapshot_id and @name are specified, delete the first one with
256 * id @snapshot_id and name @name.
257 * If only @snapshot_id is specified, delete the first one with id
258 * @snapshot_id.
259 * If only @name is specified, delete the first one with name @name.
7b4c4781 260 * if none is specified, return -EINVAL.
a89d89d3
WX
261 *
262 * Returns: 0 on success, -errno on failure. If @bs is not inserted, return
263 * -ENOMEDIUM. If @snapshot_id and @name are both NULL, return -EINVAL. If @bs
264 * does not support internal snapshot deletion, return -ENOTSUP. If @bs does
265 * not support parameter @snapshot_id or @name, or one of them is not correctly
266 * specified, return -EINVAL. If @bs can't find one matching @id and @name,
267 * return -ENOENT. If @errp != NULL, it will always be filled with error
268 * message on failure.
269 */
270int bdrv_snapshot_delete(BlockDriverState *bs,
271 const char *snapshot_id,
272 const char *name,
273 Error **errp)
de08c606
WX
274{
275 BlockDriver *drv = bs->drv;
27a7649a
PB
276 int ret;
277
de08c606 278 if (!drv) {
c6bd8c70 279 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, bdrv_get_device_name(bs));
de08c606
WX
280 return -ENOMEDIUM;
281 }
a89d89d3
WX
282 if (!snapshot_id && !name) {
283 error_setg(errp, "snapshot_id and name are both NULL");
284 return -EINVAL;
285 }
3432a192
ZH
286
287 /* drain all pending i/o before deleting snapshot */
27a7649a 288 bdrv_drained_begin(bs);
3432a192 289
de08c606 290 if (drv->bdrv_snapshot_delete) {
27a7649a
PB
291 ret = drv->bdrv_snapshot_delete(bs, snapshot_id, name, errp);
292 } else if (bs->file) {
293 ret = bdrv_snapshot_delete(bs->file->bs, snapshot_id, name, errp);
294 } else {
295 error_setg(errp, "Block format '%s' used by device '%s' "
296 "does not support internal snapshot deletion",
297 drv->format_name, bdrv_get_device_name(bs));
298 ret = -ENOTSUP;
de08c606 299 }
27a7649a
PB
300
301 bdrv_drained_end(bs);
302 return ret;
de08c606
WX
303}
304
305int bdrv_snapshot_list(BlockDriverState *bs,
306 QEMUSnapshotInfo **psn_info)
307{
308 BlockDriver *drv = bs->drv;
309 if (!drv) {
310 return -ENOMEDIUM;
311 }
312 if (drv->bdrv_snapshot_list) {
313 return drv->bdrv_snapshot_list(bs, psn_info);
314 }
315 if (bs->file) {
9a4f4c31 316 return bdrv_snapshot_list(bs->file->bs, psn_info);
de08c606
WX
317 }
318 return -ENOTSUP;
319}
320
7b4c4781
WX
321/**
322 * Temporarily load an internal snapshot by @snapshot_id and @name.
323 * @bs: block device used in the operation
324 * @snapshot_id: unique snapshot ID, or NULL
325 * @name: snapshot name, or NULL
326 * @errp: location to store error
327 *
328 * If both @snapshot_id and @name are specified, load the first one with
329 * id @snapshot_id and name @name.
330 * If only @snapshot_id is specified, load the first one with id
331 * @snapshot_id.
332 * If only @name is specified, load the first one with name @name.
333 * if none is specified, return -EINVAL.
334 *
335 * Returns: 0 on success, -errno on fail. If @bs is not inserted, return
336 * -ENOMEDIUM. If @bs is not readonly, return -EINVAL. If @bs did not support
337 * internal snapshot, return -ENOTSUP. If qemu can't find a matching @id and
338 * @name, return -ENOENT. If @errp != NULL, it will always be filled on
339 * failure.
340 */
de08c606 341int bdrv_snapshot_load_tmp(BlockDriverState *bs,
7b4c4781
WX
342 const char *snapshot_id,
343 const char *name,
344 Error **errp)
de08c606
WX
345{
346 BlockDriver *drv = bs->drv;
7b4c4781 347
de08c606 348 if (!drv) {
c6bd8c70 349 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, bdrv_get_device_name(bs));
de08c606
WX
350 return -ENOMEDIUM;
351 }
7b4c4781
WX
352 if (!snapshot_id && !name) {
353 error_setg(errp, "snapshot_id and name are both NULL");
354 return -EINVAL;
355 }
de08c606 356 if (!bs->read_only) {
7b4c4781 357 error_setg(errp, "Device is not readonly");
de08c606
WX
358 return -EINVAL;
359 }
360 if (drv->bdrv_snapshot_load_tmp) {
7b4c4781 361 return drv->bdrv_snapshot_load_tmp(bs, snapshot_id, name, errp);
de08c606 362 }
81e5f78a
AG
363 error_setg(errp, "Block format '%s' used by device '%s' "
364 "does not support temporarily loading internal snapshots",
365 drv->format_name, bdrv_get_device_name(bs));
de08c606
WX
366 return -ENOTSUP;
367}
7b4c4781
WX
368
369int bdrv_snapshot_load_tmp_by_id_or_name(BlockDriverState *bs,
370 const char *id_or_name,
371 Error **errp)
372{
373 int ret;
374 Error *local_err = NULL;
375
376 ret = bdrv_snapshot_load_tmp(bs, id_or_name, NULL, &local_err);
377 if (ret == -ENOENT || ret == -EINVAL) {
378 error_free(local_err);
379 local_err = NULL;
380 ret = bdrv_snapshot_load_tmp(bs, NULL, id_or_name, &local_err);
381 }
382
621ff94d 383 error_propagate(errp, local_err);
7b4c4781
WX
384
385 return ret;
386}
e9ff957a 387
05f4aced
KW
388static bool bdrv_all_snapshots_includes_bs(BlockDriverState *bs)
389{
390 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
391 return false;
392 }
393
394 /* Include all nodes that are either in use by a BlockBackend, or that
395 * aren't attached to any node, but owned by the monitor. */
396 return bdrv_has_blk(bs) || QLIST_EMPTY(&bs->parents);
397}
e9ff957a
DL
398
399/* Group operations. All block drivers are involved.
400 * These functions will properly handle dataplane (take aio_context_acquire
401 * when appropriate for appropriate block drivers) */
402
403bool bdrv_all_can_snapshot(BlockDriverState **first_bad_bs)
404{
405 bool ok = true;
7c8eece4 406 BlockDriverState *bs;
88be7b4b 407 BdrvNextIterator it;
e9ff957a 408
88be7b4b 409 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
e9ff957a
DL
410 AioContext *ctx = bdrv_get_aio_context(bs);
411
412 aio_context_acquire(ctx);
05f4aced 413 if (bdrv_all_snapshots_includes_bs(bs)) {
e9ff957a
DL
414 ok = bdrv_can_snapshot(bs);
415 }
416 aio_context_release(ctx);
88be7b4b 417 if (!ok) {
5e003f17 418 bdrv_next_cleanup(&it);
88be7b4b
KW
419 goto fail;
420 }
e9ff957a
DL
421 }
422
88be7b4b 423fail:
e9ff957a
DL
424 *first_bad_bs = bs;
425 return ok;
426}
9b00ea37
DL
427
428int bdrv_all_delete_snapshot(const char *name, BlockDriverState **first_bad_bs,
e53a578a 429 Error **errp)
9b00ea37
DL
430{
431 int ret = 0;
7c8eece4 432 BlockDriverState *bs;
88be7b4b 433 BdrvNextIterator it;
9b00ea37
DL
434 QEMUSnapshotInfo sn1, *snapshot = &sn1;
435
88be7b4b 436 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
9b00ea37
DL
437 AioContext *ctx = bdrv_get_aio_context(bs);
438
439 aio_context_acquire(ctx);
05f4aced
KW
440 if (bdrv_all_snapshots_includes_bs(bs) &&
441 bdrv_snapshot_find(bs, snapshot, name) >= 0)
442 {
6ca08045 443 ret = bdrv_snapshot_delete(bs, snapshot->id_str,
e53a578a 444 snapshot->name, errp);
9b00ea37
DL
445 }
446 aio_context_release(ctx);
88be7b4b 447 if (ret < 0) {
5e003f17 448 bdrv_next_cleanup(&it);
88be7b4b
KW
449 goto fail;
450 }
9b00ea37
DL
451 }
452
88be7b4b 453fail:
9b00ea37
DL
454 *first_bad_bs = bs;
455 return ret;
456}
4c1cdbaa
DL
457
458
2b624fe0
KW
459int bdrv_all_goto_snapshot(const char *name, BlockDriverState **first_bad_bs,
460 Error **errp)
4c1cdbaa 461{
2b624fe0 462 int ret = 0;
7c8eece4 463 BlockDriverState *bs;
88be7b4b 464 BdrvNextIterator it;
4c1cdbaa 465
88be7b4b 466 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
4c1cdbaa
DL
467 AioContext *ctx = bdrv_get_aio_context(bs);
468
469 aio_context_acquire(ctx);
05f4aced 470 if (bdrv_all_snapshots_includes_bs(bs)) {
2b624fe0 471 ret = bdrv_snapshot_goto(bs, name, errp);
4c1cdbaa
DL
472 }
473 aio_context_release(ctx);
2b624fe0 474 if (ret < 0) {
5e003f17 475 bdrv_next_cleanup(&it);
88be7b4b
KW
476 goto fail;
477 }
4c1cdbaa
DL
478 }
479
88be7b4b 480fail:
4c1cdbaa 481 *first_bad_bs = bs;
2b624fe0 482 return ret;
4c1cdbaa 483}
723ccda1
DL
484
485int bdrv_all_find_snapshot(const char *name, BlockDriverState **first_bad_bs)
486{
487 QEMUSnapshotInfo sn;
488 int err = 0;
7c8eece4 489 BlockDriverState *bs;
88be7b4b 490 BdrvNextIterator it;
723ccda1 491
88be7b4b 492 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
723ccda1
DL
493 AioContext *ctx = bdrv_get_aio_context(bs);
494
495 aio_context_acquire(ctx);
05f4aced 496 if (bdrv_all_snapshots_includes_bs(bs)) {
723ccda1
DL
497 err = bdrv_snapshot_find(bs, &sn, name);
498 }
499 aio_context_release(ctx);
88be7b4b 500 if (err < 0) {
5e003f17 501 bdrv_next_cleanup(&it);
88be7b4b
KW
502 goto fail;
503 }
723ccda1
DL
504 }
505
88be7b4b 506fail:
723ccda1
DL
507 *first_bad_bs = bs;
508 return err;
509}
a9085f9b
DL
510
511int bdrv_all_create_snapshot(QEMUSnapshotInfo *sn,
512 BlockDriverState *vm_state_bs,
513 uint64_t vm_state_size,
514 BlockDriverState **first_bad_bs)
515{
516 int err = 0;
7c8eece4 517 BlockDriverState *bs;
88be7b4b 518 BdrvNextIterator it;
a9085f9b 519
88be7b4b 520 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
a9085f9b
DL
521 AioContext *ctx = bdrv_get_aio_context(bs);
522
523 aio_context_acquire(ctx);
524 if (bs == vm_state_bs) {
525 sn->vm_state_size = vm_state_size;
526 err = bdrv_snapshot_create(bs, sn);
05f4aced 527 } else if (bdrv_all_snapshots_includes_bs(bs)) {
a9085f9b
DL
528 sn->vm_state_size = 0;
529 err = bdrv_snapshot_create(bs, sn);
530 }
531 aio_context_release(ctx);
88be7b4b 532 if (err < 0) {
5e003f17 533 bdrv_next_cleanup(&it);
88be7b4b
KW
534 goto fail;
535 }
a9085f9b
DL
536 }
537
88be7b4b 538fail:
a9085f9b
DL
539 *first_bad_bs = bs;
540 return err;
541}
7cb14481
DL
542
543BlockDriverState *bdrv_all_find_vmstate_bs(void)
544{
7c8eece4 545 BlockDriverState *bs;
88be7b4b 546 BdrvNextIterator it;
7cb14481 547
88be7b4b 548 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
7cb14481 549 AioContext *ctx = bdrv_get_aio_context(bs);
88be7b4b 550 bool found;
7cb14481
DL
551
552 aio_context_acquire(ctx);
05f4aced 553 found = bdrv_all_snapshots_includes_bs(bs) && bdrv_can_snapshot(bs);
7cb14481 554 aio_context_release(ctx);
88be7b4b
KW
555
556 if (found) {
5e003f17 557 bdrv_next_cleanup(&it);
88be7b4b
KW
558 break;
559 }
7cb14481
DL
560 }
561 return bs;
562}