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 int qcow2_do_read_snapshots(BlockDriverState
*bs
, bool repair
,
81 int *nb_clusters_reduced
,
82 int *extra_data_dropped
,
85 BDRVQcow2State
*s
= bs
->opaque
;
87 QCowSnapshotExtraData extra
;
89 int i
, id_str_size
, name_size
;
90 int64_t offset
, pre_sn_offset
;
91 uint64_t table_length
= 0;
94 if (!s
->nb_snapshots
) {
96 s
->snapshots_size
= 0;
100 offset
= s
->snapshots_offset
;
101 s
->snapshots
= g_new0(QCowSnapshot
, s
->nb_snapshots
);
103 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
104 bool truncate_unknown_extra_data
= false;
106 pre_sn_offset
= offset
;
107 table_length
= ROUND_UP(table_length
, 8);
109 /* Read statically sized part of the snapshot header */
110 offset
= ROUND_UP(offset
, 8);
111 ret
= bdrv_pread(bs
->file
, offset
, sizeof(h
), &h
, 0);
113 error_setg_errno(errp
, -ret
, "Failed to read snapshot table");
118 sn
= s
->snapshots
+ i
;
119 sn
->l1_table_offset
= be64_to_cpu(h
.l1_table_offset
);
120 sn
->l1_size
= be32_to_cpu(h
.l1_size
);
121 sn
->vm_state_size
= be32_to_cpu(h
.vm_state_size
);
122 sn
->date_sec
= be32_to_cpu(h
.date_sec
);
123 sn
->date_nsec
= be32_to_cpu(h
.date_nsec
);
124 sn
->vm_clock_nsec
= be64_to_cpu(h
.vm_clock_nsec
);
125 sn
->extra_data_size
= be32_to_cpu(h
.extra_data_size
);
127 id_str_size
= be16_to_cpu(h
.id_str_size
);
128 name_size
= be16_to_cpu(h
.name_size
);
130 if (sn
->extra_data_size
> QCOW_MAX_SNAPSHOT_EXTRA_DATA
) {
133 error_setg(errp
, "Too much extra metadata in snapshot table "
135 error_append_hint(errp
, "You can force-remove this extra "
136 "metadata with qemu-img check -r all\n");
140 fprintf(stderr
, "Discarding too much extra metadata in snapshot "
141 "table entry %i (%" PRIu32
" > %u)\n",
142 i
, sn
->extra_data_size
, QCOW_MAX_SNAPSHOT_EXTRA_DATA
);
144 (*extra_data_dropped
)++;
145 truncate_unknown_extra_data
= true;
148 /* Read known extra data */
149 ret
= bdrv_pread(bs
->file
, offset
,
150 MIN(sizeof(extra
), sn
->extra_data_size
), &extra
, 0);
152 error_setg_errno(errp
, -ret
, "Failed to read snapshot table");
155 offset
+= MIN(sizeof(extra
), sn
->extra_data_size
);
157 if (sn
->extra_data_size
>= endof(QCowSnapshotExtraData
,
158 vm_state_size_large
)) {
159 sn
->vm_state_size
= be64_to_cpu(extra
.vm_state_size_large
);
162 if (sn
->extra_data_size
>= endof(QCowSnapshotExtraData
, disk_size
)) {
163 sn
->disk_size
= be64_to_cpu(extra
.disk_size
);
165 sn
->disk_size
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
168 if (sn
->extra_data_size
>= endof(QCowSnapshotExtraData
, icount
)) {
169 sn
->icount
= be64_to_cpu(extra
.icount
);
174 if (sn
->extra_data_size
> sizeof(extra
)) {
175 uint64_t extra_data_end
;
176 size_t unknown_extra_data_size
;
178 extra_data_end
= offset
+ sn
->extra_data_size
- sizeof(extra
);
180 if (truncate_unknown_extra_data
) {
181 sn
->extra_data_size
= QCOW_MAX_SNAPSHOT_EXTRA_DATA
;
184 /* Store unknown extra data */
185 unknown_extra_data_size
= sn
->extra_data_size
- sizeof(extra
);
186 sn
->unknown_extra_data
= g_malloc(unknown_extra_data_size
);
187 ret
= bdrv_pread(bs
->file
, offset
, unknown_extra_data_size
,
188 sn
->unknown_extra_data
, 0);
190 error_setg_errno(errp
, -ret
,
191 "Failed to read snapshot table");
194 offset
= extra_data_end
;
197 /* Read snapshot ID */
198 sn
->id_str
= g_malloc(id_str_size
+ 1);
199 ret
= bdrv_pread(bs
->file
, offset
, id_str_size
, sn
->id_str
, 0);
201 error_setg_errno(errp
, -ret
, "Failed to read snapshot table");
204 offset
+= id_str_size
;
205 sn
->id_str
[id_str_size
] = '\0';
207 /* Read snapshot name */
208 sn
->name
= g_malloc(name_size
+ 1);
209 ret
= bdrv_pread(bs
->file
, offset
, name_size
, sn
->name
, 0);
211 error_setg_errno(errp
, -ret
, "Failed to read snapshot table");
215 sn
->name
[name_size
] = '\0';
217 /* Note that the extra data may have been truncated */
218 table_length
+= sizeof(h
) + sn
->extra_data_size
+ id_str_size
+
221 assert(table_length
== offset
- s
->snapshots_offset
);
224 if (table_length
> QCOW_MAX_SNAPSHOTS_SIZE
||
225 offset
- s
->snapshots_offset
> INT_MAX
)
229 error_setg(errp
, "Snapshot table is too big");
230 error_append_hint(errp
, "You can force-remove all %u "
231 "overhanging snapshots with qemu-img check "
232 "-r all\n", s
->nb_snapshots
- i
);
236 fprintf(stderr
, "Discarding %u overhanging snapshots (snapshot "
237 "table is too big)\n", s
->nb_snapshots
- i
);
239 *nb_clusters_reduced
+= (s
->nb_snapshots
- i
);
241 /* Discard current snapshot also */
242 qcow2_free_single_snapshot(bs
, i
);
245 * This leaks all the rest of the snapshot table and the
246 * snapshots' clusters, but we run in check -r all mode,
247 * so qcow2_check_refcounts() will take care of it.
250 offset
= pre_sn_offset
;
255 assert(offset
- s
->snapshots_offset
<= INT_MAX
);
256 s
->snapshots_size
= offset
- s
->snapshots_offset
;
260 qcow2_free_snapshots(bs
);
264 int qcow2_read_snapshots(BlockDriverState
*bs
, Error
**errp
)
266 return qcow2_do_read_snapshots(bs
, false, NULL
, NULL
, errp
);
269 /* add at the end of the file a new list of snapshots */
270 int qcow2_write_snapshots(BlockDriverState
*bs
)
272 BDRVQcow2State
*s
= bs
->opaque
;
274 QCowSnapshotHeader h
;
275 QCowSnapshotExtraData extra
;
276 int i
, name_size
, id_str_size
, snapshots_size
;
278 uint32_t nb_snapshots
;
279 uint64_t snapshots_offset
;
280 } QEMU_PACKED header_data
;
281 int64_t offset
, snapshots_offset
= 0;
284 /* compute the size of the snapshots */
286 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
287 sn
= s
->snapshots
+ i
;
288 offset
= ROUND_UP(offset
, 8);
290 offset
+= MAX(sizeof(extra
), sn
->extra_data_size
);
291 offset
+= strlen(sn
->id_str
);
292 offset
+= strlen(sn
->name
);
294 if (offset
> QCOW_MAX_SNAPSHOTS_SIZE
) {
300 assert(offset
<= INT_MAX
);
301 snapshots_size
= offset
;
303 /* Allocate space for the new snapshot list */
304 snapshots_offset
= qcow2_alloc_clusters(bs
, snapshots_size
);
305 offset
= snapshots_offset
;
310 ret
= bdrv_flush(bs
);
315 /* The snapshot list position has not yet been updated, so these clusters
316 * must indeed be completely free */
317 ret
= qcow2_pre_write_overlap_check(bs
, 0, offset
, snapshots_size
, false);
323 /* Write all snapshots to the new list */
324 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
325 sn
= s
->snapshots
+ i
;
326 memset(&h
, 0, sizeof(h
));
327 h
.l1_table_offset
= cpu_to_be64(sn
->l1_table_offset
);
328 h
.l1_size
= cpu_to_be32(sn
->l1_size
);
329 /* If it doesn't fit in 32 bit, older implementations should treat it
330 * as a disk-only snapshot rather than truncate the VM state */
331 if (sn
->vm_state_size
<= 0xffffffff) {
332 h
.vm_state_size
= cpu_to_be32(sn
->vm_state_size
);
334 h
.date_sec
= cpu_to_be32(sn
->date_sec
);
335 h
.date_nsec
= cpu_to_be32(sn
->date_nsec
);
336 h
.vm_clock_nsec
= cpu_to_be64(sn
->vm_clock_nsec
);
337 h
.extra_data_size
= cpu_to_be32(MAX(sizeof(extra
),
338 sn
->extra_data_size
));
340 memset(&extra
, 0, sizeof(extra
));
341 extra
.vm_state_size_large
= cpu_to_be64(sn
->vm_state_size
);
342 extra
.disk_size
= cpu_to_be64(sn
->disk_size
);
343 extra
.icount
= cpu_to_be64(sn
->icount
);
345 id_str_size
= strlen(sn
->id_str
);
346 name_size
= strlen(sn
->name
);
347 assert(id_str_size
<= UINT16_MAX
&& name_size
<= UINT16_MAX
);
348 h
.id_str_size
= cpu_to_be16(id_str_size
);
349 h
.name_size
= cpu_to_be16(name_size
);
350 offset
= ROUND_UP(offset
, 8);
352 ret
= bdrv_pwrite(bs
->file
, offset
, sizeof(h
), &h
, 0);
358 ret
= bdrv_pwrite(bs
->file
, offset
, sizeof(extra
), &extra
, 0);
362 offset
+= sizeof(extra
);
364 if (sn
->extra_data_size
> sizeof(extra
)) {
365 size_t unknown_extra_data_size
=
366 sn
->extra_data_size
- sizeof(extra
);
368 /* qcow2_read_snapshots() ensures no unbounded allocation */
369 assert(unknown_extra_data_size
<= BDRV_REQUEST_MAX_BYTES
);
370 assert(sn
->unknown_extra_data
);
372 ret
= bdrv_pwrite(bs
->file
, offset
, unknown_extra_data_size
,
373 sn
->unknown_extra_data
, 0);
377 offset
+= unknown_extra_data_size
;
380 ret
= bdrv_pwrite(bs
->file
, offset
, id_str_size
, sn
->id_str
, 0);
384 offset
+= id_str_size
;
386 ret
= bdrv_pwrite(bs
->file
, offset
, name_size
, sn
->name
, 0);
394 * Update the header to point to the new snapshot table. This requires the
395 * new table and its refcounts to be stable on disk.
397 ret
= bdrv_flush(bs
);
402 QEMU_BUILD_BUG_ON(offsetof(QCowHeader
, snapshots_offset
) !=
403 endof(QCowHeader
, nb_snapshots
));
405 header_data
.nb_snapshots
= cpu_to_be32(s
->nb_snapshots
);
406 header_data
.snapshots_offset
= cpu_to_be64(snapshots_offset
);
408 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, nb_snapshots
),
409 sizeof(header_data
), &header_data
, 0);
414 /* free the old snapshot table */
415 qcow2_free_clusters(bs
, s
->snapshots_offset
, s
->snapshots_size
,
416 QCOW2_DISCARD_SNAPSHOT
);
417 s
->snapshots_offset
= snapshots_offset
;
418 s
->snapshots_size
= snapshots_size
;
422 if (snapshots_offset
> 0) {
423 qcow2_free_clusters(bs
, snapshots_offset
, snapshots_size
,
424 QCOW2_DISCARD_ALWAYS
);
429 int coroutine_fn
qcow2_check_read_snapshot_table(BlockDriverState
*bs
,
430 BdrvCheckResult
*result
,
433 BDRVQcow2State
*s
= bs
->opaque
;
434 Error
*local_err
= NULL
;
435 int nb_clusters_reduced
= 0;
436 int extra_data_dropped
= 0;
439 uint32_t nb_snapshots
;
440 uint64_t snapshots_offset
;
441 } QEMU_PACKED snapshot_table_pointer
;
443 /* qcow2_do_open() discards this information in check mode */
444 ret
= bdrv_pread(bs
->file
, offsetof(QCowHeader
, nb_snapshots
),
445 sizeof(snapshot_table_pointer
), &snapshot_table_pointer
,
448 result
->check_errors
++;
449 fprintf(stderr
, "ERROR failed to read the snapshot table pointer from "
450 "the image header: %s\n", strerror(-ret
));
454 s
->snapshots_offset
= be64_to_cpu(snapshot_table_pointer
.snapshots_offset
);
455 s
->nb_snapshots
= be32_to_cpu(snapshot_table_pointer
.nb_snapshots
);
457 if (s
->nb_snapshots
> QCOW_MAX_SNAPSHOTS
&& (fix
& BDRV_FIX_ERRORS
)) {
458 fprintf(stderr
, "Discarding %u overhanging snapshots\n",
459 s
->nb_snapshots
- QCOW_MAX_SNAPSHOTS
);
461 nb_clusters_reduced
+= s
->nb_snapshots
- QCOW_MAX_SNAPSHOTS
;
462 s
->nb_snapshots
= QCOW_MAX_SNAPSHOTS
;
465 ret
= qcow2_validate_table(bs
, s
->snapshots_offset
, s
->nb_snapshots
,
466 sizeof(QCowSnapshotHeader
),
467 sizeof(QCowSnapshotHeader
) * QCOW_MAX_SNAPSHOTS
,
468 "snapshot table", &local_err
);
470 result
->check_errors
++;
471 error_reportf_err(local_err
, "ERROR ");
473 if (s
->nb_snapshots
> QCOW_MAX_SNAPSHOTS
) {
474 fprintf(stderr
, "You can force-remove all %u overhanging snapshots "
475 "with qemu-img check -r all\n",
476 s
->nb_snapshots
- QCOW_MAX_SNAPSHOTS
);
479 /* We did not read the snapshot table, so invalidate this information */
480 s
->snapshots_offset
= 0;
486 qemu_co_mutex_unlock(&s
->lock
);
487 ret
= qcow2_do_read_snapshots(bs
, fix
& BDRV_FIX_ERRORS
,
488 &nb_clusters_reduced
, &extra_data_dropped
,
490 qemu_co_mutex_lock(&s
->lock
);
492 result
->check_errors
++;
493 error_reportf_err(local_err
,
494 "ERROR failed to read the snapshot table: ");
496 /* We did not read the snapshot table, so invalidate this information */
497 s
->snapshots_offset
= 0;
502 result
->corruptions
+= nb_clusters_reduced
+ extra_data_dropped
;
504 if (nb_clusters_reduced
) {
506 * Update image header now, because:
507 * (1) qcow2_check_refcounts() relies on s->nb_snapshots to be
508 * the same as what the image header says,
509 * (2) this leaks clusters, but qcow2_check_refcounts() will
512 assert(fix
& BDRV_FIX_ERRORS
);
514 snapshot_table_pointer
.nb_snapshots
= cpu_to_be32(s
->nb_snapshots
);
515 ret
= bdrv_co_pwrite_sync(bs
->file
, offsetof(QCowHeader
, nb_snapshots
),
516 sizeof(snapshot_table_pointer
.nb_snapshots
),
517 &snapshot_table_pointer
.nb_snapshots
, 0);
519 result
->check_errors
++;
520 fprintf(stderr
, "ERROR failed to update the snapshot count in the "
521 "image header: %s\n", strerror(-ret
));
525 result
->corruptions_fixed
+= nb_clusters_reduced
;
526 result
->corruptions
-= nb_clusters_reduced
;
530 * All of v3 images' snapshot table entries need to have at least
531 * 16 bytes of extra data.
533 if (s
->qcow_version
>= 3) {
535 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
536 if (s
->snapshots
[i
].extra_data_size
<
537 sizeof_field(QCowSnapshotExtraData
, vm_state_size_large
) +
538 sizeof_field(QCowSnapshotExtraData
, disk_size
))
540 result
->corruptions
++;
541 fprintf(stderr
, "%s snapshot table entry %i is incomplete\n",
542 fix
& BDRV_FIX_ERRORS
? "Repairing" : "ERROR", i
);
550 int coroutine_fn
qcow2_check_fix_snapshot_table(BlockDriverState
*bs
,
551 BdrvCheckResult
*result
,
554 BDRVQcow2State
*s
= bs
->opaque
;
557 if (result
->corruptions
&& (fix
& BDRV_FIX_ERRORS
)) {
558 qemu_co_mutex_unlock(&s
->lock
);
559 ret
= qcow2_write_snapshots(bs
);
560 qemu_co_mutex_lock(&s
->lock
);
562 result
->check_errors
++;
563 fprintf(stderr
, "ERROR failed to update snapshot table: %s\n",
568 result
->corruptions_fixed
+= result
->corruptions
;
569 result
->corruptions
= 0;
575 static void find_new_snapshot_id(BlockDriverState
*bs
,
576 char *id_str
, int id_str_size
)
578 BDRVQcow2State
*s
= bs
->opaque
;
581 unsigned long id
, id_max
= 0;
583 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
584 sn
= s
->snapshots
+ i
;
585 id
= strtoul(sn
->id_str
, NULL
, 10);
589 snprintf(id_str
, id_str_size
, "%lu", id_max
+ 1);
592 static int find_snapshot_by_id_and_name(BlockDriverState
*bs
,
596 BDRVQcow2State
*s
= bs
->opaque
;
600 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
601 if (!strcmp(s
->snapshots
[i
].id_str
, id
) &&
602 !strcmp(s
->snapshots
[i
].name
, name
)) {
607 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
608 if (!strcmp(s
->snapshots
[i
].id_str
, id
)) {
613 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
614 if (!strcmp(s
->snapshots
[i
].name
, name
)) {
623 static int find_snapshot_by_id_or_name(BlockDriverState
*bs
,
624 const char *id_or_name
)
628 ret
= find_snapshot_by_id_and_name(bs
, id_or_name
, NULL
);
632 return find_snapshot_by_id_and_name(bs
, NULL
, id_or_name
);
635 /* if no id is provided, a new one is constructed */
636 int qcow2_snapshot_create(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
)
638 BDRVQcow2State
*s
= bs
->opaque
;
639 QCowSnapshot
*new_snapshot_list
= NULL
;
640 QCowSnapshot
*old_snapshot_list
= NULL
;
641 QCowSnapshot sn1
, *sn
= &sn1
;
643 uint64_t *l1_table
= NULL
;
644 int64_t l1_table_offset
;
646 if (s
->nb_snapshots
>= QCOW_MAX_SNAPSHOTS
) {
650 if (has_data_file(bs
)) {
654 memset(sn
, 0, sizeof(*sn
));
657 find_new_snapshot_id(bs
, sn_info
->id_str
, sizeof(sn_info
->id_str
));
659 /* Populate sn with passed data */
660 sn
->id_str
= g_strdup(sn_info
->id_str
);
661 sn
->name
= g_strdup(sn_info
->name
);
663 sn
->disk_size
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
664 sn
->vm_state_size
= sn_info
->vm_state_size
;
665 sn
->date_sec
= sn_info
->date_sec
;
666 sn
->date_nsec
= sn_info
->date_nsec
;
667 sn
->vm_clock_nsec
= sn_info
->vm_clock_nsec
;
668 sn
->icount
= sn_info
->icount
;
669 sn
->extra_data_size
= sizeof(QCowSnapshotExtraData
);
671 /* Allocate the L1 table of the snapshot and copy the current one there. */
672 l1_table_offset
= qcow2_alloc_clusters(bs
, s
->l1_size
* L1E_SIZE
);
673 if (l1_table_offset
< 0) {
674 ret
= l1_table_offset
;
678 sn
->l1_table_offset
= l1_table_offset
;
679 sn
->l1_size
= s
->l1_size
;
681 l1_table
= g_try_new(uint64_t, s
->l1_size
);
682 if (s
->l1_size
&& l1_table
== NULL
) {
687 for(i
= 0; i
< s
->l1_size
; i
++) {
688 l1_table
[i
] = cpu_to_be64(s
->l1_table
[i
]);
691 ret
= qcow2_pre_write_overlap_check(bs
, 0, sn
->l1_table_offset
,
692 s
->l1_size
* L1E_SIZE
, false);
697 ret
= bdrv_pwrite(bs
->file
, sn
->l1_table_offset
, s
->l1_size
* L1E_SIZE
,
707 * Increase the refcounts of all clusters and make sure everything is
708 * stable on disk before updating the snapshot table to contain a pointer
709 * to the new L1 table.
711 ret
= qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 1);
716 /* Append the new snapshot to the snapshot list */
717 new_snapshot_list
= g_new(QCowSnapshot
, s
->nb_snapshots
+ 1);
719 memcpy(new_snapshot_list
, s
->snapshots
,
720 s
->nb_snapshots
* sizeof(QCowSnapshot
));
721 old_snapshot_list
= s
->snapshots
;
723 s
->snapshots
= new_snapshot_list
;
724 s
->snapshots
[s
->nb_snapshots
++] = *sn
;
726 ret
= qcow2_write_snapshots(bs
);
728 g_free(s
->snapshots
);
729 s
->snapshots
= old_snapshot_list
;
734 g_free(old_snapshot_list
);
736 /* The VM state isn't needed any more in the active L1 table; in fact, it
737 * hurts by causing expensive COW for the next snapshot. */
738 qcow2_cluster_discard(bs
, qcow2_vm_state_offset(s
),
739 ROUND_UP(sn
->vm_state_size
, s
->cluster_size
),
740 QCOW2_DISCARD_NEVER
, false);
744 BdrvCheckResult result
= {0};
745 qcow2_check_refcounts(bs
, &result
, 0);
758 /* copy the snapshot 'snapshot_name' into the current disk image */
759 int qcow2_snapshot_goto(BlockDriverState
*bs
, const char *snapshot_id
)
761 BDRVQcow2State
*s
= bs
->opaque
;
763 Error
*local_err
= NULL
;
764 int i
, snapshot_index
;
765 int cur_l1_bytes
, sn_l1_bytes
;
767 uint64_t *sn_l1_table
= NULL
;
769 if (has_data_file(bs
)) {
773 /* Search the snapshot */
774 snapshot_index
= find_snapshot_by_id_or_name(bs
, snapshot_id
);
775 if (snapshot_index
< 0) {
778 sn
= &s
->snapshots
[snapshot_index
];
780 ret
= qcow2_validate_table(bs
, sn
->l1_table_offset
, sn
->l1_size
,
781 L1E_SIZE
, QCOW_MAX_L1_SIZE
,
782 "Snapshot L1 table", &local_err
);
784 error_report_err(local_err
);
788 if (sn
->disk_size
!= bs
->total_sectors
* BDRV_SECTOR_SIZE
) {
789 BlockBackend
*blk
= blk_new_with_bs(bs
, BLK_PERM_RESIZE
, BLK_PERM_ALL
,
792 error_report_err(local_err
);
797 ret
= blk_truncate(blk
, sn
->disk_size
, true, PREALLOC_MODE_OFF
, 0,
801 error_report_err(local_err
);
807 * Make sure that the current L1 table is big enough to contain the whole
808 * L1 table of the snapshot. If the snapshot L1 table is smaller, the
809 * current one must be padded with zeros.
811 ret
= qcow2_grow_l1_table(bs
, sn
->l1_size
, true);
816 cur_l1_bytes
= s
->l1_size
* L1E_SIZE
;
817 sn_l1_bytes
= sn
->l1_size
* L1E_SIZE
;
820 * Copy the snapshot L1 table to the current L1 table.
822 * Before overwriting the old current L1 table on disk, make sure to
823 * increase all refcounts for the clusters referenced by the new one.
824 * Decrease the refcount referenced by the old one only when the L1
825 * table is overwritten.
827 sn_l1_table
= g_try_malloc0(cur_l1_bytes
);
828 if (cur_l1_bytes
&& sn_l1_table
== NULL
) {
833 ret
= bdrv_pread(bs
->file
, sn
->l1_table_offset
, sn_l1_bytes
, sn_l1_table
,
839 ret
= qcow2_update_snapshot_refcount(bs
, sn
->l1_table_offset
,
845 ret
= qcow2_pre_write_overlap_check(bs
, QCOW2_OL_ACTIVE_L1
,
846 s
->l1_table_offset
, cur_l1_bytes
,
852 ret
= bdrv_pwrite_sync(bs
->file
, s
->l1_table_offset
, cur_l1_bytes
,
859 * Decrease refcount of clusters of current L1 table.
861 * At this point, the in-memory s->l1_table points to the old L1 table,
862 * whereas on disk we already have the new one.
864 * qcow2_update_snapshot_refcount special cases the current L1 table to use
865 * the in-memory data instead of really using the offset to load a new one,
866 * which is why this works.
868 ret
= qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
,
872 * Now update the in-memory L1 table to be in sync with the on-disk one. We
873 * need to do this even if updating refcounts failed.
875 for(i
= 0;i
< s
->l1_size
; i
++) {
876 s
->l1_table
[i
] = be64_to_cpu(sn_l1_table
[i
]);
887 * Update QCOW_OFLAG_COPIED in the active L1 table (it may have changed
888 * when we decreased the refcount of the old snapshot.
890 ret
= qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 0);
897 BdrvCheckResult result
= {0};
898 qcow2_check_refcounts(bs
, &result
, 0);
908 int qcow2_snapshot_delete(BlockDriverState
*bs
,
909 const char *snapshot_id
,
913 BDRVQcow2State
*s
= bs
->opaque
;
915 int snapshot_index
, ret
;
917 if (has_data_file(bs
)) {
921 /* Search the snapshot */
922 snapshot_index
= find_snapshot_by_id_and_name(bs
, snapshot_id
, name
);
923 if (snapshot_index
< 0) {
924 error_setg(errp
, "Can't find the snapshot");
927 sn
= s
->snapshots
[snapshot_index
];
929 ret
= qcow2_validate_table(bs
, sn
.l1_table_offset
, sn
.l1_size
,
930 L1E_SIZE
, QCOW_MAX_L1_SIZE
,
931 "Snapshot L1 table", errp
);
936 /* Remove it from the snapshot list */
937 memmove(s
->snapshots
+ snapshot_index
,
938 s
->snapshots
+ snapshot_index
+ 1,
939 (s
->nb_snapshots
- snapshot_index
- 1) * sizeof(sn
));
941 ret
= qcow2_write_snapshots(bs
);
943 error_setg_errno(errp
, -ret
,
944 "Failed to remove snapshot from snapshot list");
949 * The snapshot is now unused, clean up. If we fail after this point, we
950 * won't recover but just leak clusters.
952 g_free(sn
.unknown_extra_data
);
957 * Now decrease the refcounts of clusters referenced by the snapshot and
960 ret
= qcow2_update_snapshot_refcount(bs
, sn
.l1_table_offset
,
963 error_setg_errno(errp
, -ret
, "Failed to free the cluster and L1 table");
966 qcow2_free_clusters(bs
, sn
.l1_table_offset
, sn
.l1_size
* L1E_SIZE
,
967 QCOW2_DISCARD_SNAPSHOT
);
969 /* must update the copied flag on the current cluster offsets */
970 ret
= qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 0);
972 error_setg_errno(errp
, -ret
,
973 "Failed to update snapshot status in disk");
979 BdrvCheckResult result
= {0};
980 qcow2_check_refcounts(bs
, &result
, 0);
986 int qcow2_snapshot_list(BlockDriverState
*bs
, QEMUSnapshotInfo
**psn_tab
)
988 BDRVQcow2State
*s
= bs
->opaque
;
989 QEMUSnapshotInfo
*sn_tab
, *sn_info
;
993 if (has_data_file(bs
)) {
996 if (!s
->nb_snapshots
) {
998 return s
->nb_snapshots
;
1001 sn_tab
= g_new0(QEMUSnapshotInfo
, s
->nb_snapshots
);
1002 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1003 sn_info
= sn_tab
+ i
;
1004 sn
= s
->snapshots
+ i
;
1005 pstrcpy(sn_info
->id_str
, sizeof(sn_info
->id_str
),
1007 pstrcpy(sn_info
->name
, sizeof(sn_info
->name
),
1009 sn_info
->vm_state_size
= sn
->vm_state_size
;
1010 sn_info
->date_sec
= sn
->date_sec
;
1011 sn_info
->date_nsec
= sn
->date_nsec
;
1012 sn_info
->vm_clock_nsec
= sn
->vm_clock_nsec
;
1013 sn_info
->icount
= sn
->icount
;
1016 return s
->nb_snapshots
;
1019 int qcow2_snapshot_load_tmp(BlockDriverState
*bs
,
1020 const char *snapshot_id
,
1024 int i
, snapshot_index
;
1025 BDRVQcow2State
*s
= bs
->opaque
;
1027 uint64_t *new_l1_table
;
1031 assert(bdrv_is_read_only(bs
));
1033 /* Search the snapshot */
1034 snapshot_index
= find_snapshot_by_id_and_name(bs
, snapshot_id
, name
);
1035 if (snapshot_index
< 0) {
1037 "Can't find snapshot");
1040 sn
= &s
->snapshots
[snapshot_index
];
1042 /* Allocate and read in the snapshot's L1 table */
1043 ret
= qcow2_validate_table(bs
, sn
->l1_table_offset
, sn
->l1_size
,
1044 L1E_SIZE
, QCOW_MAX_L1_SIZE
,
1045 "Snapshot L1 table", errp
);
1049 new_l1_bytes
= sn
->l1_size
* L1E_SIZE
;
1050 new_l1_table
= qemu_try_blockalign(bs
->file
->bs
, new_l1_bytes
);
1051 if (new_l1_table
== NULL
) {
1055 ret
= bdrv_pread(bs
->file
, sn
->l1_table_offset
, new_l1_bytes
,
1058 error_setg(errp
, "Failed to read l1 table for snapshot");
1059 qemu_vfree(new_l1_table
);
1063 /* Switch the L1 table */
1064 qemu_vfree(s
->l1_table
);
1066 s
->l1_size
= sn
->l1_size
;
1067 s
->l1_table_offset
= sn
->l1_table_offset
;
1068 s
->l1_table
= new_l1_table
;
1070 for(i
= 0;i
< s
->l1_size
; i
++) {
1071 be64_to_cpus(&s
->l1_table
[i
]);