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