2 * Block driver for the QCOW version 2 format
4 * Copyright (c) 2004-2006 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "qemu/osdep.h"
26 #include "sysemu/block-backend.h"
27 #include "qapi/error.h"
29 #include "qemu/bswap.h"
30 #include "qemu/error-report.h"
31 #include "qemu/cutils.h"
32 #include "qemu/memalign.h"
34 static void qcow2_free_single_snapshot(BlockDriverState
*bs
, int i
)
36 BDRVQcow2State
*s
= bs
->opaque
;
38 assert(i
>= 0 && i
< s
->nb_snapshots
);
39 g_free(s
->snapshots
[i
].name
);
40 g_free(s
->snapshots
[i
].id_str
);
41 g_free(s
->snapshots
[i
].unknown_extra_data
);
42 memset(&s
->snapshots
[i
], 0, sizeof(s
->snapshots
[i
]));
45 void qcow2_free_snapshots(BlockDriverState
*bs
)
47 BDRVQcow2State
*s
= bs
->opaque
;
50 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
51 qcow2_free_single_snapshot(bs
, i
);
59 * If @repair is true, try to repair a broken snapshot table instead
60 * of just returning an error:
62 * - If the snapshot table was too long, set *nb_clusters_reduced to
63 * the number of snapshots removed off the end.
64 * The caller will update the on-disk nb_snapshots accordingly;
65 * this leaks clusters, but is safe.
66 * (The on-disk information must be updated before
67 * qcow2_check_refcounts(), because that function relies on
68 * s->nb_snapshots to reflect the on-disk value.)
70 * - If there were snapshots with too much extra metadata, increment
71 * *extra_data_dropped for each.
72 * This requires the caller to eventually rewrite the whole snapshot
73 * table, which requires cluster allocation. Therefore, this should
74 * be done only after qcow2_check_refcounts() made sure the refcount
75 * structures are valid.
76 * (In the meantime, the image is still valid because
77 * qcow2_check_refcounts() does not do anything with snapshots'
80 static coroutine_fn GRAPH_RDLOCK
81 int qcow2_do_read_snapshots(BlockDriverState
*bs
, bool repair
,
82 int *nb_clusters_reduced
,
83 int *extra_data_dropped
,
86 BDRVQcow2State
*s
= bs
->opaque
;
88 QCowSnapshotExtraData extra
;
90 int i
, id_str_size
, name_size
;
91 int64_t offset
, pre_sn_offset
;
92 uint64_t table_length
= 0;
95 if (!s
->nb_snapshots
) {
97 s
->snapshots_size
= 0;
101 offset
= s
->snapshots_offset
;
102 s
->snapshots
= g_new0(QCowSnapshot
, s
->nb_snapshots
);
104 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
105 bool truncate_unknown_extra_data
= false;
107 pre_sn_offset
= offset
;
108 table_length
= ROUND_UP(table_length
, 8);
110 /* Read statically sized part of the snapshot header */
111 offset
= ROUND_UP(offset
, 8);
112 ret
= bdrv_co_pread(bs
->file
, offset
, sizeof(h
), &h
, 0);
114 error_setg_errno(errp
, -ret
, "Failed to read snapshot table");
119 sn
= s
->snapshots
+ i
;
120 sn
->l1_table_offset
= be64_to_cpu(h
.l1_table_offset
);
121 sn
->l1_size
= be32_to_cpu(h
.l1_size
);
122 sn
->vm_state_size
= be32_to_cpu(h
.vm_state_size
);
123 sn
->date_sec
= be32_to_cpu(h
.date_sec
);
124 sn
->date_nsec
= be32_to_cpu(h
.date_nsec
);
125 sn
->vm_clock_nsec
= be64_to_cpu(h
.vm_clock_nsec
);
126 sn
->extra_data_size
= be32_to_cpu(h
.extra_data_size
);
128 id_str_size
= be16_to_cpu(h
.id_str_size
);
129 name_size
= be16_to_cpu(h
.name_size
);
131 if (sn
->extra_data_size
> QCOW_MAX_SNAPSHOT_EXTRA_DATA
) {
134 error_setg(errp
, "Too much extra metadata in snapshot table "
136 error_append_hint(errp
, "You can force-remove this extra "
137 "metadata with qemu-img check -r all\n");
141 fprintf(stderr
, "Discarding too much extra metadata in snapshot "
142 "table entry %i (%" PRIu32
" > %u)\n",
143 i
, sn
->extra_data_size
, QCOW_MAX_SNAPSHOT_EXTRA_DATA
);
145 (*extra_data_dropped
)++;
146 truncate_unknown_extra_data
= true;
149 /* Read known extra data */
150 ret
= bdrv_co_pread(bs
->file
, offset
,
151 MIN(sizeof(extra
), sn
->extra_data_size
), &extra
, 0);
153 error_setg_errno(errp
, -ret
, "Failed to read snapshot table");
156 offset
+= MIN(sizeof(extra
), sn
->extra_data_size
);
158 if (sn
->extra_data_size
>= endof(QCowSnapshotExtraData
,
159 vm_state_size_large
)) {
160 sn
->vm_state_size
= be64_to_cpu(extra
.vm_state_size_large
);
163 if (sn
->extra_data_size
>= endof(QCowSnapshotExtraData
, disk_size
)) {
164 sn
->disk_size
= be64_to_cpu(extra
.disk_size
);
166 sn
->disk_size
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
169 if (sn
->extra_data_size
>= endof(QCowSnapshotExtraData
, icount
)) {
170 sn
->icount
= be64_to_cpu(extra
.icount
);
175 if (sn
->extra_data_size
> sizeof(extra
)) {
176 uint64_t extra_data_end
;
177 size_t unknown_extra_data_size
;
179 extra_data_end
= offset
+ sn
->extra_data_size
- sizeof(extra
);
181 if (truncate_unknown_extra_data
) {
182 sn
->extra_data_size
= QCOW_MAX_SNAPSHOT_EXTRA_DATA
;
185 /* Store unknown extra data */
186 unknown_extra_data_size
= sn
->extra_data_size
- sizeof(extra
);
187 sn
->unknown_extra_data
= g_malloc(unknown_extra_data_size
);
188 ret
= bdrv_co_pread(bs
->file
, offset
, unknown_extra_data_size
,
189 sn
->unknown_extra_data
, 0);
191 error_setg_errno(errp
, -ret
,
192 "Failed to read snapshot table");
195 offset
= extra_data_end
;
198 /* Read snapshot ID */
199 sn
->id_str
= g_malloc(id_str_size
+ 1);
200 ret
= bdrv_co_pread(bs
->file
, offset
, id_str_size
, sn
->id_str
, 0);
202 error_setg_errno(errp
, -ret
, "Failed to read snapshot table");
205 offset
+= id_str_size
;
206 sn
->id_str
[id_str_size
] = '\0';
208 /* Read snapshot name */
209 sn
->name
= g_malloc(name_size
+ 1);
210 ret
= bdrv_co_pread(bs
->file
, offset
, name_size
, sn
->name
, 0);
212 error_setg_errno(errp
, -ret
, "Failed to read snapshot table");
216 sn
->name
[name_size
] = '\0';
218 /* Note that the extra data may have been truncated */
219 table_length
+= sizeof(h
) + sn
->extra_data_size
+ id_str_size
+
222 assert(table_length
== offset
- s
->snapshots_offset
);
225 if (table_length
> QCOW_MAX_SNAPSHOTS_SIZE
||
226 offset
- s
->snapshots_offset
> INT_MAX
)
230 error_setg(errp
, "Snapshot table is too big");
231 error_append_hint(errp
, "You can force-remove all %u "
232 "overhanging snapshots with qemu-img check "
233 "-r all\n", s
->nb_snapshots
- i
);
237 fprintf(stderr
, "Discarding %u overhanging snapshots (snapshot "
238 "table is too big)\n", s
->nb_snapshots
- i
);
240 *nb_clusters_reduced
+= (s
->nb_snapshots
- i
);
242 /* Discard current snapshot also */
243 qcow2_free_single_snapshot(bs
, i
);
246 * This leaks all the rest of the snapshot table and the
247 * snapshots' clusters, but we run in check -r all mode,
248 * so qcow2_check_refcounts() will take care of it.
251 offset
= pre_sn_offset
;
256 assert(offset
- s
->snapshots_offset
<= INT_MAX
);
257 s
->snapshots_size
= offset
- s
->snapshots_offset
;
261 qcow2_free_snapshots(bs
);
265 int coroutine_fn
qcow2_read_snapshots(BlockDriverState
*bs
, Error
**errp
)
267 return qcow2_do_read_snapshots(bs
, false, NULL
, NULL
, errp
);
270 /* add at the end of the file a new list of snapshots */
271 int qcow2_write_snapshots(BlockDriverState
*bs
)
273 BDRVQcow2State
*s
= bs
->opaque
;
275 QCowSnapshotHeader h
;
276 QCowSnapshotExtraData extra
;
277 int i
, name_size
, id_str_size
, snapshots_size
;
279 uint32_t nb_snapshots
;
280 uint64_t snapshots_offset
;
281 } QEMU_PACKED header_data
;
282 int64_t offset
, snapshots_offset
= 0;
285 /* compute the size of the snapshots */
287 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
288 sn
= s
->snapshots
+ i
;
289 offset
= ROUND_UP(offset
, 8);
291 offset
+= MAX(sizeof(extra
), sn
->extra_data_size
);
292 offset
+= strlen(sn
->id_str
);
293 offset
+= strlen(sn
->name
);
295 if (offset
> QCOW_MAX_SNAPSHOTS_SIZE
) {
301 assert(offset
<= INT_MAX
);
302 snapshots_size
= offset
;
304 /* Allocate space for the new snapshot list */
305 snapshots_offset
= qcow2_alloc_clusters(bs
, snapshots_size
);
306 offset
= snapshots_offset
;
311 ret
= bdrv_flush(bs
);
316 /* The snapshot list position has not yet been updated, so these clusters
317 * must indeed be completely free */
318 ret
= qcow2_pre_write_overlap_check(bs
, 0, offset
, snapshots_size
, false);
324 /* Write all snapshots to the new list */
325 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
326 sn
= s
->snapshots
+ i
;
327 memset(&h
, 0, sizeof(h
));
328 h
.l1_table_offset
= cpu_to_be64(sn
->l1_table_offset
);
329 h
.l1_size
= cpu_to_be32(sn
->l1_size
);
330 /* If it doesn't fit in 32 bit, older implementations should treat it
331 * as a disk-only snapshot rather than truncate the VM state */
332 if (sn
->vm_state_size
<= 0xffffffff) {
333 h
.vm_state_size
= cpu_to_be32(sn
->vm_state_size
);
335 h
.date_sec
= cpu_to_be32(sn
->date_sec
);
336 h
.date_nsec
= cpu_to_be32(sn
->date_nsec
);
337 h
.vm_clock_nsec
= cpu_to_be64(sn
->vm_clock_nsec
);
338 h
.extra_data_size
= cpu_to_be32(MAX(sizeof(extra
),
339 sn
->extra_data_size
));
341 memset(&extra
, 0, sizeof(extra
));
342 extra
.vm_state_size_large
= cpu_to_be64(sn
->vm_state_size
);
343 extra
.disk_size
= cpu_to_be64(sn
->disk_size
);
344 extra
.icount
= cpu_to_be64(sn
->icount
);
346 id_str_size
= strlen(sn
->id_str
);
347 name_size
= strlen(sn
->name
);
348 assert(id_str_size
<= UINT16_MAX
&& name_size
<= UINT16_MAX
);
349 h
.id_str_size
= cpu_to_be16(id_str_size
);
350 h
.name_size
= cpu_to_be16(name_size
);
351 offset
= ROUND_UP(offset
, 8);
353 ret
= bdrv_pwrite(bs
->file
, offset
, sizeof(h
), &h
, 0);
359 ret
= bdrv_pwrite(bs
->file
, offset
, sizeof(extra
), &extra
, 0);
363 offset
+= sizeof(extra
);
365 if (sn
->extra_data_size
> sizeof(extra
)) {
366 size_t unknown_extra_data_size
=
367 sn
->extra_data_size
- sizeof(extra
);
369 /* qcow2_read_snapshots() ensures no unbounded allocation */
370 assert(unknown_extra_data_size
<= BDRV_REQUEST_MAX_BYTES
);
371 assert(sn
->unknown_extra_data
);
373 ret
= bdrv_pwrite(bs
->file
, offset
, unknown_extra_data_size
,
374 sn
->unknown_extra_data
, 0);
378 offset
+= unknown_extra_data_size
;
381 ret
= bdrv_pwrite(bs
->file
, offset
, id_str_size
, sn
->id_str
, 0);
385 offset
+= id_str_size
;
387 ret
= bdrv_pwrite(bs
->file
, offset
, name_size
, sn
->name
, 0);
395 * Update the header to point to the new snapshot table. This requires the
396 * new table and its refcounts to be stable on disk.
398 ret
= bdrv_flush(bs
);
403 QEMU_BUILD_BUG_ON(offsetof(QCowHeader
, snapshots_offset
) !=
404 endof(QCowHeader
, nb_snapshots
));
406 header_data
.nb_snapshots
= cpu_to_be32(s
->nb_snapshots
);
407 header_data
.snapshots_offset
= cpu_to_be64(snapshots_offset
);
409 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, nb_snapshots
),
410 sizeof(header_data
), &header_data
, 0);
415 /* free the old snapshot table */
416 qcow2_free_clusters(bs
, s
->snapshots_offset
, s
->snapshots_size
,
417 QCOW2_DISCARD_SNAPSHOT
);
418 s
->snapshots_offset
= snapshots_offset
;
419 s
->snapshots_size
= snapshots_size
;
423 if (snapshots_offset
> 0) {
424 qcow2_free_clusters(bs
, snapshots_offset
, snapshots_size
,
425 QCOW2_DISCARD_ALWAYS
);
430 int coroutine_fn
qcow2_check_read_snapshot_table(BlockDriverState
*bs
,
431 BdrvCheckResult
*result
,
434 BDRVQcow2State
*s
= bs
->opaque
;
435 Error
*local_err
= NULL
;
436 int nb_clusters_reduced
= 0;
437 int extra_data_dropped
= 0;
440 uint32_t nb_snapshots
;
441 uint64_t snapshots_offset
;
442 } QEMU_PACKED snapshot_table_pointer
;
444 /* qcow2_do_open() discards this information in check mode */
445 ret
= bdrv_co_pread(bs
->file
, offsetof(QCowHeader
, nb_snapshots
),
446 sizeof(snapshot_table_pointer
), &snapshot_table_pointer
,
449 result
->check_errors
++;
450 fprintf(stderr
, "ERROR failed to read the snapshot table pointer from "
451 "the image header: %s\n", strerror(-ret
));
455 s
->snapshots_offset
= be64_to_cpu(snapshot_table_pointer
.snapshots_offset
);
456 s
->nb_snapshots
= be32_to_cpu(snapshot_table_pointer
.nb_snapshots
);
458 if (s
->nb_snapshots
> QCOW_MAX_SNAPSHOTS
&& (fix
& BDRV_FIX_ERRORS
)) {
459 fprintf(stderr
, "Discarding %u overhanging snapshots\n",
460 s
->nb_snapshots
- QCOW_MAX_SNAPSHOTS
);
462 nb_clusters_reduced
+= s
->nb_snapshots
- QCOW_MAX_SNAPSHOTS
;
463 s
->nb_snapshots
= QCOW_MAX_SNAPSHOTS
;
466 ret
= qcow2_validate_table(bs
, s
->snapshots_offset
, s
->nb_snapshots
,
467 sizeof(QCowSnapshotHeader
),
468 sizeof(QCowSnapshotHeader
) * QCOW_MAX_SNAPSHOTS
,
469 "snapshot table", &local_err
);
471 result
->check_errors
++;
472 error_reportf_err(local_err
, "ERROR ");
474 if (s
->nb_snapshots
> QCOW_MAX_SNAPSHOTS
) {
475 fprintf(stderr
, "You can force-remove all %u overhanging snapshots "
476 "with qemu-img check -r all\n",
477 s
->nb_snapshots
- QCOW_MAX_SNAPSHOTS
);
480 /* We did not read the snapshot table, so invalidate this information */
481 s
->snapshots_offset
= 0;
487 qemu_co_mutex_unlock(&s
->lock
);
488 ret
= qcow2_do_read_snapshots(bs
, fix
& BDRV_FIX_ERRORS
,
489 &nb_clusters_reduced
, &extra_data_dropped
,
491 qemu_co_mutex_lock(&s
->lock
);
493 result
->check_errors
++;
494 error_reportf_err(local_err
,
495 "ERROR failed to read the snapshot table: ");
497 /* We did not read the snapshot table, so invalidate this information */
498 s
->snapshots_offset
= 0;
503 result
->corruptions
+= nb_clusters_reduced
+ extra_data_dropped
;
505 if (nb_clusters_reduced
) {
507 * Update image header now, because:
508 * (1) qcow2_check_refcounts() relies on s->nb_snapshots to be
509 * the same as what the image header says,
510 * (2) this leaks clusters, but qcow2_check_refcounts() will
513 assert(fix
& BDRV_FIX_ERRORS
);
515 snapshot_table_pointer
.nb_snapshots
= cpu_to_be32(s
->nb_snapshots
);
516 ret
= bdrv_co_pwrite_sync(bs
->file
, offsetof(QCowHeader
, nb_snapshots
),
517 sizeof(snapshot_table_pointer
.nb_snapshots
),
518 &snapshot_table_pointer
.nb_snapshots
, 0);
520 result
->check_errors
++;
521 fprintf(stderr
, "ERROR failed to update the snapshot count in the "
522 "image header: %s\n", strerror(-ret
));
526 result
->corruptions_fixed
+= nb_clusters_reduced
;
527 result
->corruptions
-= nb_clusters_reduced
;
531 * All of v3 images' snapshot table entries need to have at least
532 * 16 bytes of extra data.
534 if (s
->qcow_version
>= 3) {
536 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
537 if (s
->snapshots
[i
].extra_data_size
<
538 sizeof_field(QCowSnapshotExtraData
, vm_state_size_large
) +
539 sizeof_field(QCowSnapshotExtraData
, disk_size
))
541 result
->corruptions
++;
542 fprintf(stderr
, "%s snapshot table entry %i is incomplete\n",
543 fix
& BDRV_FIX_ERRORS
? "Repairing" : "ERROR", i
);
551 int coroutine_fn
qcow2_check_fix_snapshot_table(BlockDriverState
*bs
,
552 BdrvCheckResult
*result
,
555 BDRVQcow2State
*s
= bs
->opaque
;
558 if (result
->corruptions
&& (fix
& BDRV_FIX_ERRORS
)) {
559 qemu_co_mutex_unlock(&s
->lock
);
560 ret
= qcow2_write_snapshots(bs
);
561 qemu_co_mutex_lock(&s
->lock
);
563 result
->check_errors
++;
564 fprintf(stderr
, "ERROR failed to update snapshot table: %s\n",
569 result
->corruptions_fixed
+= result
->corruptions
;
570 result
->corruptions
= 0;
576 static void find_new_snapshot_id(BlockDriverState
*bs
,
577 char *id_str
, int id_str_size
)
579 BDRVQcow2State
*s
= bs
->opaque
;
582 unsigned long id
, id_max
= 0;
584 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
585 sn
= s
->snapshots
+ i
;
586 id
= strtoul(sn
->id_str
, NULL
, 10);
590 snprintf(id_str
, id_str_size
, "%lu", id_max
+ 1);
593 static int find_snapshot_by_id_and_name(BlockDriverState
*bs
,
597 BDRVQcow2State
*s
= bs
->opaque
;
601 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
602 if (!strcmp(s
->snapshots
[i
].id_str
, id
) &&
603 !strcmp(s
->snapshots
[i
].name
, name
)) {
608 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
609 if (!strcmp(s
->snapshots
[i
].id_str
, id
)) {
614 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
615 if (!strcmp(s
->snapshots
[i
].name
, name
)) {
624 static int find_snapshot_by_id_or_name(BlockDriverState
*bs
,
625 const char *id_or_name
)
629 ret
= find_snapshot_by_id_and_name(bs
, id_or_name
, NULL
);
633 return find_snapshot_by_id_and_name(bs
, NULL
, id_or_name
);
636 /* if no id is provided, a new one is constructed */
637 int qcow2_snapshot_create(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
)
639 BDRVQcow2State
*s
= bs
->opaque
;
640 QCowSnapshot
*new_snapshot_list
= NULL
;
641 QCowSnapshot
*old_snapshot_list
= NULL
;
642 QCowSnapshot sn1
, *sn
= &sn1
;
644 uint64_t *l1_table
= NULL
;
645 int64_t l1_table_offset
;
647 if (s
->nb_snapshots
>= QCOW_MAX_SNAPSHOTS
) {
651 if (has_data_file(bs
)) {
655 memset(sn
, 0, sizeof(*sn
));
658 find_new_snapshot_id(bs
, sn_info
->id_str
, sizeof(sn_info
->id_str
));
660 /* Populate sn with passed data */
661 sn
->id_str
= g_strdup(sn_info
->id_str
);
662 sn
->name
= g_strdup(sn_info
->name
);
664 sn
->disk_size
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
665 sn
->vm_state_size
= sn_info
->vm_state_size
;
666 sn
->date_sec
= sn_info
->date_sec
;
667 sn
->date_nsec
= sn_info
->date_nsec
;
668 sn
->vm_clock_nsec
= sn_info
->vm_clock_nsec
;
669 sn
->icount
= sn_info
->icount
;
670 sn
->extra_data_size
= sizeof(QCowSnapshotExtraData
);
672 /* Allocate the L1 table of the snapshot and copy the current one there. */
673 l1_table_offset
= qcow2_alloc_clusters(bs
, s
->l1_size
* L1E_SIZE
);
674 if (l1_table_offset
< 0) {
675 ret
= l1_table_offset
;
679 sn
->l1_table_offset
= l1_table_offset
;
680 sn
->l1_size
= s
->l1_size
;
682 l1_table
= g_try_new(uint64_t, s
->l1_size
);
683 if (s
->l1_size
&& l1_table
== NULL
) {
688 for(i
= 0; i
< s
->l1_size
; i
++) {
689 l1_table
[i
] = cpu_to_be64(s
->l1_table
[i
]);
692 ret
= qcow2_pre_write_overlap_check(bs
, 0, sn
->l1_table_offset
,
693 s
->l1_size
* L1E_SIZE
, false);
698 ret
= bdrv_pwrite(bs
->file
, sn
->l1_table_offset
, s
->l1_size
* L1E_SIZE
,
708 * Increase the refcounts of all clusters and make sure everything is
709 * stable on disk before updating the snapshot table to contain a pointer
710 * to the new L1 table.
712 ret
= qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 1);
717 /* Append the new snapshot to the snapshot list */
718 new_snapshot_list
= g_new(QCowSnapshot
, s
->nb_snapshots
+ 1);
720 memcpy(new_snapshot_list
, s
->snapshots
,
721 s
->nb_snapshots
* sizeof(QCowSnapshot
));
722 old_snapshot_list
= s
->snapshots
;
724 s
->snapshots
= new_snapshot_list
;
725 s
->snapshots
[s
->nb_snapshots
++] = *sn
;
727 ret
= qcow2_write_snapshots(bs
);
729 g_free(s
->snapshots
);
730 s
->snapshots
= old_snapshot_list
;
735 g_free(old_snapshot_list
);
737 /* The VM state isn't needed any more in the active L1 table; in fact, it
738 * hurts by causing expensive COW for the next snapshot. */
739 qcow2_cluster_discard(bs
, qcow2_vm_state_offset(s
),
740 ROUND_UP(sn
->vm_state_size
, s
->cluster_size
),
741 QCOW2_DISCARD_NEVER
, false);
745 BdrvCheckResult result
= {0};
746 qcow2_check_refcounts(bs
, &result
, 0);
759 /* copy the snapshot 'snapshot_name' into the current disk image */
760 int qcow2_snapshot_goto(BlockDriverState
*bs
, const char *snapshot_id
)
762 BDRVQcow2State
*s
= bs
->opaque
;
764 Error
*local_err
= NULL
;
765 int i
, snapshot_index
;
766 int cur_l1_bytes
, sn_l1_bytes
;
768 uint64_t *sn_l1_table
= NULL
;
770 if (has_data_file(bs
)) {
774 /* Search the snapshot */
775 snapshot_index
= find_snapshot_by_id_or_name(bs
, snapshot_id
);
776 if (snapshot_index
< 0) {
779 sn
= &s
->snapshots
[snapshot_index
];
781 ret
= qcow2_validate_table(bs
, sn
->l1_table_offset
, sn
->l1_size
,
782 L1E_SIZE
, QCOW_MAX_L1_SIZE
,
783 "Snapshot L1 table", &local_err
);
785 error_report_err(local_err
);
789 if (sn
->disk_size
!= bs
->total_sectors
* BDRV_SECTOR_SIZE
) {
790 BlockBackend
*blk
= blk_new_with_bs(bs
, BLK_PERM_RESIZE
, BLK_PERM_ALL
,
793 error_report_err(local_err
);
798 ret
= blk_truncate(blk
, sn
->disk_size
, true, PREALLOC_MODE_OFF
, 0,
802 error_report_err(local_err
);
808 * Make sure that the current L1 table is big enough to contain the whole
809 * L1 table of the snapshot. If the snapshot L1 table is smaller, the
810 * current one must be padded with zeros.
812 ret
= qcow2_grow_l1_table(bs
, sn
->l1_size
, true);
817 cur_l1_bytes
= s
->l1_size
* L1E_SIZE
;
818 sn_l1_bytes
= sn
->l1_size
* L1E_SIZE
;
821 * Copy the snapshot L1 table to the current L1 table.
823 * Before overwriting the old current L1 table on disk, make sure to
824 * increase all refcounts for the clusters referenced by the new one.
825 * Decrease the refcount referenced by the old one only when the L1
826 * table is overwritten.
828 sn_l1_table
= g_try_malloc0(cur_l1_bytes
);
829 if (cur_l1_bytes
&& sn_l1_table
== NULL
) {
834 ret
= bdrv_pread(bs
->file
, sn
->l1_table_offset
, sn_l1_bytes
, sn_l1_table
,
840 ret
= qcow2_update_snapshot_refcount(bs
, sn
->l1_table_offset
,
846 ret
= qcow2_pre_write_overlap_check(bs
, QCOW2_OL_ACTIVE_L1
,
847 s
->l1_table_offset
, cur_l1_bytes
,
853 ret
= bdrv_pwrite_sync(bs
->file
, s
->l1_table_offset
, cur_l1_bytes
,
860 * Decrease refcount of clusters of current L1 table.
862 * At this point, the in-memory s->l1_table points to the old L1 table,
863 * whereas on disk we already have the new one.
865 * qcow2_update_snapshot_refcount special cases the current L1 table to use
866 * the in-memory data instead of really using the offset to load a new one,
867 * which is why this works.
869 ret
= qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
,
873 * Now update the in-memory L1 table to be in sync with the on-disk one. We
874 * need to do this even if updating refcounts failed.
876 for(i
= 0;i
< s
->l1_size
; i
++) {
877 s
->l1_table
[i
] = be64_to_cpu(sn_l1_table
[i
]);
888 * Update QCOW_OFLAG_COPIED in the active L1 table (it may have changed
889 * when we decreased the refcount of the old snapshot.
891 ret
= qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 0);
898 BdrvCheckResult result
= {0};
899 qcow2_check_refcounts(bs
, &result
, 0);
909 int qcow2_snapshot_delete(BlockDriverState
*bs
,
910 const char *snapshot_id
,
914 BDRVQcow2State
*s
= bs
->opaque
;
916 int snapshot_index
, ret
;
918 if (has_data_file(bs
)) {
922 /* Search the snapshot */
923 snapshot_index
= find_snapshot_by_id_and_name(bs
, snapshot_id
, name
);
924 if (snapshot_index
< 0) {
925 error_setg(errp
, "Can't find the snapshot");
928 sn
= s
->snapshots
[snapshot_index
];
930 ret
= qcow2_validate_table(bs
, sn
.l1_table_offset
, sn
.l1_size
,
931 L1E_SIZE
, QCOW_MAX_L1_SIZE
,
932 "Snapshot L1 table", errp
);
937 /* Remove it from the snapshot list */
938 memmove(s
->snapshots
+ snapshot_index
,
939 s
->snapshots
+ snapshot_index
+ 1,
940 (s
->nb_snapshots
- snapshot_index
- 1) * sizeof(sn
));
942 ret
= qcow2_write_snapshots(bs
);
944 error_setg_errno(errp
, -ret
,
945 "Failed to remove snapshot from snapshot list");
950 * The snapshot is now unused, clean up. If we fail after this point, we
951 * won't recover but just leak clusters.
953 g_free(sn
.unknown_extra_data
);
958 * Now decrease the refcounts of clusters referenced by the snapshot and
961 ret
= qcow2_update_snapshot_refcount(bs
, sn
.l1_table_offset
,
964 error_setg_errno(errp
, -ret
, "Failed to free the cluster and L1 table");
967 qcow2_free_clusters(bs
, sn
.l1_table_offset
, sn
.l1_size
* L1E_SIZE
,
968 QCOW2_DISCARD_SNAPSHOT
);
970 /* must update the copied flag on the current cluster offsets */
971 ret
= qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 0);
973 error_setg_errno(errp
, -ret
,
974 "Failed to update snapshot status in disk");
980 BdrvCheckResult result
= {0};
981 qcow2_check_refcounts(bs
, &result
, 0);
987 int qcow2_snapshot_list(BlockDriverState
*bs
, QEMUSnapshotInfo
**psn_tab
)
989 BDRVQcow2State
*s
= bs
->opaque
;
990 QEMUSnapshotInfo
*sn_tab
, *sn_info
;
994 if (has_data_file(bs
)) {
997 if (!s
->nb_snapshots
) {
999 return s
->nb_snapshots
;
1002 sn_tab
= g_new0(QEMUSnapshotInfo
, s
->nb_snapshots
);
1003 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1004 sn_info
= sn_tab
+ i
;
1005 sn
= s
->snapshots
+ i
;
1006 pstrcpy(sn_info
->id_str
, sizeof(sn_info
->id_str
),
1008 pstrcpy(sn_info
->name
, sizeof(sn_info
->name
),
1010 sn_info
->vm_state_size
= sn
->vm_state_size
;
1011 sn_info
->date_sec
= sn
->date_sec
;
1012 sn_info
->date_nsec
= sn
->date_nsec
;
1013 sn_info
->vm_clock_nsec
= sn
->vm_clock_nsec
;
1014 sn_info
->icount
= sn
->icount
;
1017 return s
->nb_snapshots
;
1020 int qcow2_snapshot_load_tmp(BlockDriverState
*bs
,
1021 const char *snapshot_id
,
1025 int i
, snapshot_index
;
1026 BDRVQcow2State
*s
= bs
->opaque
;
1028 uint64_t *new_l1_table
;
1032 assert(bdrv_is_read_only(bs
));
1034 /* Search the snapshot */
1035 snapshot_index
= find_snapshot_by_id_and_name(bs
, snapshot_id
, name
);
1036 if (snapshot_index
< 0) {
1038 "Can't find snapshot");
1041 sn
= &s
->snapshots
[snapshot_index
];
1043 /* Allocate and read in the snapshot's L1 table */
1044 ret
= qcow2_validate_table(bs
, sn
->l1_table_offset
, sn
->l1_size
,
1045 L1E_SIZE
, QCOW_MAX_L1_SIZE
,
1046 "Snapshot L1 table", errp
);
1050 new_l1_bytes
= sn
->l1_size
* L1E_SIZE
;
1051 new_l1_table
= qemu_try_blockalign(bs
->file
->bs
, new_l1_bytes
);
1052 if (new_l1_table
== NULL
) {
1056 ret
= bdrv_pread(bs
->file
, sn
->l1_table_offset
, new_l1_bytes
,
1059 error_setg(errp
, "Failed to read l1 table for snapshot");
1060 qemu_vfree(new_l1_table
);
1064 /* Switch the L1 table */
1065 qemu_vfree(s
->l1_table
);
1067 s
->l1_size
= sn
->l1_size
;
1068 s
->l1_table_offset
= sn
->l1_table_offset
;
1069 s
->l1_table
= new_l1_table
;
1071 for(i
= 0;i
< s
->l1_size
; i
++) {
1072 be64_to_cpus(&s
->l1_table
[i
]);