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 "qapi/error.h"
28 #include "qemu/bswap.h"
29 #include "qemu/error-report.h"
30 #include "qemu/cutils.h"
32 void qcow2_free_snapshots(BlockDriverState
*bs
)
34 BDRVQcow2State
*s
= bs
->opaque
;
37 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
38 g_free(s
->snapshots
[i
].name
);
39 g_free(s
->snapshots
[i
].id_str
);
40 g_free(s
->snapshots
[i
].unknown_extra_data
);
47 int qcow2_read_snapshots(BlockDriverState
*bs
, Error
**errp
)
49 BDRVQcow2State
*s
= bs
->opaque
;
51 QCowSnapshotExtraData extra
;
53 int i
, id_str_size
, name_size
;
57 if (!s
->nb_snapshots
) {
59 s
->snapshots_size
= 0;
63 offset
= s
->snapshots_offset
;
64 s
->snapshots
= g_new0(QCowSnapshot
, s
->nb_snapshots
);
66 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
67 /* Read statically sized part of the snapshot header */
68 offset
= ROUND_UP(offset
, 8);
69 ret
= bdrv_pread(bs
->file
, offset
, &h
, sizeof(h
));
71 error_setg_errno(errp
, -ret
, "Failed to read snapshot table");
76 sn
= s
->snapshots
+ i
;
77 sn
->l1_table_offset
= be64_to_cpu(h
.l1_table_offset
);
78 sn
->l1_size
= be32_to_cpu(h
.l1_size
);
79 sn
->vm_state_size
= be32_to_cpu(h
.vm_state_size
);
80 sn
->date_sec
= be32_to_cpu(h
.date_sec
);
81 sn
->date_nsec
= be32_to_cpu(h
.date_nsec
);
82 sn
->vm_clock_nsec
= be64_to_cpu(h
.vm_clock_nsec
);
83 sn
->extra_data_size
= be32_to_cpu(h
.extra_data_size
);
85 id_str_size
= be16_to_cpu(h
.id_str_size
);
86 name_size
= be16_to_cpu(h
.name_size
);
88 if (sn
->extra_data_size
> QCOW_MAX_SNAPSHOT_EXTRA_DATA
) {
90 error_setg(errp
, "Too much extra metadata in snapshot table "
95 /* Read known extra data */
96 ret
= bdrv_pread(bs
->file
, offset
, &extra
,
97 MIN(sizeof(extra
), sn
->extra_data_size
));
99 error_setg_errno(errp
, -ret
, "Failed to read snapshot table");
102 offset
+= MIN(sizeof(extra
), sn
->extra_data_size
);
104 if (sn
->extra_data_size
>= endof(QCowSnapshotExtraData
,
105 vm_state_size_large
)) {
106 sn
->vm_state_size
= be64_to_cpu(extra
.vm_state_size_large
);
109 if (sn
->extra_data_size
>= endof(QCowSnapshotExtraData
, disk_size
)) {
110 sn
->disk_size
= be64_to_cpu(extra
.disk_size
);
112 sn
->disk_size
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
115 if (sn
->extra_data_size
> sizeof(extra
)) {
116 /* Store unknown extra data */
117 size_t unknown_extra_data_size
=
118 sn
->extra_data_size
- sizeof(extra
);
120 sn
->unknown_extra_data
= g_malloc(unknown_extra_data_size
);
121 ret
= bdrv_pread(bs
->file
, offset
, sn
->unknown_extra_data
,
122 unknown_extra_data_size
);
124 error_setg_errno(errp
, -ret
, "Failed to read snapshot table");
127 offset
+= unknown_extra_data_size
;
130 /* Read snapshot ID */
131 sn
->id_str
= g_malloc(id_str_size
+ 1);
132 ret
= bdrv_pread(bs
->file
, offset
, sn
->id_str
, id_str_size
);
134 error_setg_errno(errp
, -ret
, "Failed to read snapshot table");
137 offset
+= id_str_size
;
138 sn
->id_str
[id_str_size
] = '\0';
140 /* Read snapshot name */
141 sn
->name
= g_malloc(name_size
+ 1);
142 ret
= bdrv_pread(bs
->file
, offset
, sn
->name
, name_size
);
144 error_setg_errno(errp
, -ret
, "Failed to read snapshot table");
148 sn
->name
[name_size
] = '\0';
150 if (offset
- s
->snapshots_offset
> QCOW_MAX_SNAPSHOTS_SIZE
) {
152 error_setg(errp
, "Snapshot table is too big");
157 assert(offset
- s
->snapshots_offset
<= INT_MAX
);
158 s
->snapshots_size
= offset
- s
->snapshots_offset
;
162 qcow2_free_snapshots(bs
);
166 /* add at the end of the file a new list of snapshots */
167 int qcow2_write_snapshots(BlockDriverState
*bs
)
169 BDRVQcow2State
*s
= bs
->opaque
;
171 QCowSnapshotHeader h
;
172 QCowSnapshotExtraData extra
;
173 int i
, name_size
, id_str_size
, snapshots_size
;
175 uint32_t nb_snapshots
;
176 uint64_t snapshots_offset
;
177 } QEMU_PACKED header_data
;
178 int64_t offset
, snapshots_offset
= 0;
181 /* compute the size of the snapshots */
183 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
184 sn
= s
->snapshots
+ i
;
185 offset
= ROUND_UP(offset
, 8);
187 offset
+= MAX(sizeof(extra
), sn
->extra_data_size
);
188 offset
+= strlen(sn
->id_str
);
189 offset
+= strlen(sn
->name
);
191 if (offset
> QCOW_MAX_SNAPSHOTS_SIZE
) {
197 assert(offset
<= INT_MAX
);
198 snapshots_size
= offset
;
200 /* Allocate space for the new snapshot list */
201 snapshots_offset
= qcow2_alloc_clusters(bs
, snapshots_size
);
202 offset
= snapshots_offset
;
207 ret
= bdrv_flush(bs
);
212 /* The snapshot list position has not yet been updated, so these clusters
213 * must indeed be completely free */
214 ret
= qcow2_pre_write_overlap_check(bs
, 0, offset
, snapshots_size
, false);
220 /* Write all snapshots to the new list */
221 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
222 sn
= s
->snapshots
+ i
;
223 memset(&h
, 0, sizeof(h
));
224 h
.l1_table_offset
= cpu_to_be64(sn
->l1_table_offset
);
225 h
.l1_size
= cpu_to_be32(sn
->l1_size
);
226 /* If it doesn't fit in 32 bit, older implementations should treat it
227 * as a disk-only snapshot rather than truncate the VM state */
228 if (sn
->vm_state_size
<= 0xffffffff) {
229 h
.vm_state_size
= cpu_to_be32(sn
->vm_state_size
);
231 h
.date_sec
= cpu_to_be32(sn
->date_sec
);
232 h
.date_nsec
= cpu_to_be32(sn
->date_nsec
);
233 h
.vm_clock_nsec
= cpu_to_be64(sn
->vm_clock_nsec
);
234 h
.extra_data_size
= cpu_to_be32(MAX(sizeof(extra
),
235 sn
->extra_data_size
));
237 memset(&extra
, 0, sizeof(extra
));
238 extra
.vm_state_size_large
= cpu_to_be64(sn
->vm_state_size
);
239 extra
.disk_size
= cpu_to_be64(sn
->disk_size
);
241 id_str_size
= strlen(sn
->id_str
);
242 name_size
= strlen(sn
->name
);
243 assert(id_str_size
<= UINT16_MAX
&& name_size
<= UINT16_MAX
);
244 h
.id_str_size
= cpu_to_be16(id_str_size
);
245 h
.name_size
= cpu_to_be16(name_size
);
246 offset
= ROUND_UP(offset
, 8);
248 ret
= bdrv_pwrite(bs
->file
, offset
, &h
, sizeof(h
));
254 ret
= bdrv_pwrite(bs
->file
, offset
, &extra
, sizeof(extra
));
258 offset
+= sizeof(extra
);
260 if (sn
->extra_data_size
> sizeof(extra
)) {
261 size_t unknown_extra_data_size
=
262 sn
->extra_data_size
- sizeof(extra
);
264 /* qcow2_read_snapshots() ensures no unbounded allocation */
265 assert(unknown_extra_data_size
<= BDRV_REQUEST_MAX_BYTES
);
266 assert(sn
->unknown_extra_data
);
268 ret
= bdrv_pwrite(bs
->file
, offset
, sn
->unknown_extra_data
,
269 unknown_extra_data_size
);
273 offset
+= unknown_extra_data_size
;
276 ret
= bdrv_pwrite(bs
->file
, offset
, sn
->id_str
, id_str_size
);
280 offset
+= id_str_size
;
282 ret
= bdrv_pwrite(bs
->file
, offset
, sn
->name
, name_size
);
290 * Update the header to point to the new snapshot table. This requires the
291 * new table and its refcounts to be stable on disk.
293 ret
= bdrv_flush(bs
);
298 QEMU_BUILD_BUG_ON(offsetof(QCowHeader
, snapshots_offset
) !=
299 endof(QCowHeader
, nb_snapshots
));
301 header_data
.nb_snapshots
= cpu_to_be32(s
->nb_snapshots
);
302 header_data
.snapshots_offset
= cpu_to_be64(snapshots_offset
);
304 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, nb_snapshots
),
305 &header_data
, sizeof(header_data
));
310 /* free the old snapshot table */
311 qcow2_free_clusters(bs
, s
->snapshots_offset
, s
->snapshots_size
,
312 QCOW2_DISCARD_SNAPSHOT
);
313 s
->snapshots_offset
= snapshots_offset
;
314 s
->snapshots_size
= snapshots_size
;
318 if (snapshots_offset
> 0) {
319 qcow2_free_clusters(bs
, snapshots_offset
, snapshots_size
,
320 QCOW2_DISCARD_ALWAYS
);
325 int coroutine_fn
qcow2_check_read_snapshot_table(BlockDriverState
*bs
,
326 BdrvCheckResult
*result
,
329 BDRVQcow2State
*s
= bs
->opaque
;
330 Error
*local_err
= NULL
;
333 uint32_t nb_snapshots
;
334 uint64_t snapshots_offset
;
335 } QEMU_PACKED snapshot_table_pointer
;
337 /* qcow2_do_open() discards this information in check mode */
338 ret
= bdrv_pread(bs
->file
, offsetof(QCowHeader
, nb_snapshots
),
339 &snapshot_table_pointer
, sizeof(snapshot_table_pointer
));
341 result
->check_errors
++;
342 fprintf(stderr
, "ERROR failed to read the snapshot table pointer from "
343 "the image header: %s\n", strerror(-ret
));
347 s
->snapshots_offset
= be64_to_cpu(snapshot_table_pointer
.snapshots_offset
);
348 s
->nb_snapshots
= be32_to_cpu(snapshot_table_pointer
.nb_snapshots
);
350 ret
= qcow2_validate_table(bs
, s
->snapshots_offset
, s
->nb_snapshots
,
351 sizeof(QCowSnapshotHeader
),
352 sizeof(QCowSnapshotHeader
) * QCOW_MAX_SNAPSHOTS
,
353 "snapshot table", &local_err
);
355 result
->check_errors
++;
356 error_reportf_err(local_err
, "ERROR ");
358 /* We did not read the snapshot table, so invalidate this information */
359 s
->snapshots_offset
= 0;
365 qemu_co_mutex_unlock(&s
->lock
);
366 ret
= qcow2_read_snapshots(bs
, &local_err
);
367 qemu_co_mutex_lock(&s
->lock
);
369 result
->check_errors
++;
370 error_reportf_err(local_err
,
371 "ERROR failed to read the snapshot table: ");
373 /* We did not read the snapshot table, so invalidate this information */
374 s
->snapshots_offset
= 0;
383 static void find_new_snapshot_id(BlockDriverState
*bs
,
384 char *id_str
, int id_str_size
)
386 BDRVQcow2State
*s
= bs
->opaque
;
389 unsigned long id
, id_max
= 0;
391 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
392 sn
= s
->snapshots
+ i
;
393 id
= strtoul(sn
->id_str
, NULL
, 10);
397 snprintf(id_str
, id_str_size
, "%lu", id_max
+ 1);
400 static int find_snapshot_by_id_and_name(BlockDriverState
*bs
,
404 BDRVQcow2State
*s
= bs
->opaque
;
408 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
409 if (!strcmp(s
->snapshots
[i
].id_str
, id
) &&
410 !strcmp(s
->snapshots
[i
].name
, name
)) {
415 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
416 if (!strcmp(s
->snapshots
[i
].id_str
, id
)) {
421 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
422 if (!strcmp(s
->snapshots
[i
].name
, name
)) {
431 static int find_snapshot_by_id_or_name(BlockDriverState
*bs
,
432 const char *id_or_name
)
436 ret
= find_snapshot_by_id_and_name(bs
, id_or_name
, NULL
);
440 return find_snapshot_by_id_and_name(bs
, NULL
, id_or_name
);
443 /* if no id is provided, a new one is constructed */
444 int qcow2_snapshot_create(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
)
446 BDRVQcow2State
*s
= bs
->opaque
;
447 QCowSnapshot
*new_snapshot_list
= NULL
;
448 QCowSnapshot
*old_snapshot_list
= NULL
;
449 QCowSnapshot sn1
, *sn
= &sn1
;
451 uint64_t *l1_table
= NULL
;
452 int64_t l1_table_offset
;
454 if (s
->nb_snapshots
>= QCOW_MAX_SNAPSHOTS
) {
458 if (has_data_file(bs
)) {
462 memset(sn
, 0, sizeof(*sn
));
465 find_new_snapshot_id(bs
, sn_info
->id_str
, sizeof(sn_info
->id_str
));
467 /* Populate sn with passed data */
468 sn
->id_str
= g_strdup(sn_info
->id_str
);
469 sn
->name
= g_strdup(sn_info
->name
);
471 sn
->disk_size
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
472 sn
->vm_state_size
= sn_info
->vm_state_size
;
473 sn
->date_sec
= sn_info
->date_sec
;
474 sn
->date_nsec
= sn_info
->date_nsec
;
475 sn
->vm_clock_nsec
= sn_info
->vm_clock_nsec
;
476 sn
->extra_data_size
= sizeof(QCowSnapshotExtraData
);
478 /* Allocate the L1 table of the snapshot and copy the current one there. */
479 l1_table_offset
= qcow2_alloc_clusters(bs
, s
->l1_size
* sizeof(uint64_t));
480 if (l1_table_offset
< 0) {
481 ret
= l1_table_offset
;
485 sn
->l1_table_offset
= l1_table_offset
;
486 sn
->l1_size
= s
->l1_size
;
488 l1_table
= g_try_new(uint64_t, s
->l1_size
);
489 if (s
->l1_size
&& l1_table
== NULL
) {
494 for(i
= 0; i
< s
->l1_size
; i
++) {
495 l1_table
[i
] = cpu_to_be64(s
->l1_table
[i
]);
498 ret
= qcow2_pre_write_overlap_check(bs
, 0, sn
->l1_table_offset
,
499 s
->l1_size
* sizeof(uint64_t), false);
504 ret
= bdrv_pwrite(bs
->file
, sn
->l1_table_offset
, l1_table
,
505 s
->l1_size
* sizeof(uint64_t));
514 * Increase the refcounts of all clusters and make sure everything is
515 * stable on disk before updating the snapshot table to contain a pointer
516 * to the new L1 table.
518 ret
= qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 1);
523 /* Append the new snapshot to the snapshot list */
524 new_snapshot_list
= g_new(QCowSnapshot
, s
->nb_snapshots
+ 1);
526 memcpy(new_snapshot_list
, s
->snapshots
,
527 s
->nb_snapshots
* sizeof(QCowSnapshot
));
528 old_snapshot_list
= s
->snapshots
;
530 s
->snapshots
= new_snapshot_list
;
531 s
->snapshots
[s
->nb_snapshots
++] = *sn
;
533 ret
= qcow2_write_snapshots(bs
);
535 g_free(s
->snapshots
);
536 s
->snapshots
= old_snapshot_list
;
541 g_free(old_snapshot_list
);
543 /* The VM state isn't needed any more in the active L1 table; in fact, it
544 * hurts by causing expensive COW for the next snapshot. */
545 qcow2_cluster_discard(bs
, qcow2_vm_state_offset(s
),
546 ROUND_UP(sn
->vm_state_size
, s
->cluster_size
),
547 QCOW2_DISCARD_NEVER
, false);
551 BdrvCheckResult result
= {0};
552 qcow2_check_refcounts(bs
, &result
, 0);
565 /* copy the snapshot 'snapshot_name' into the current disk image */
566 int qcow2_snapshot_goto(BlockDriverState
*bs
, const char *snapshot_id
)
568 BDRVQcow2State
*s
= bs
->opaque
;
570 Error
*local_err
= NULL
;
571 int i
, snapshot_index
;
572 int cur_l1_bytes
, sn_l1_bytes
;
574 uint64_t *sn_l1_table
= NULL
;
576 if (has_data_file(bs
)) {
580 /* Search the snapshot */
581 snapshot_index
= find_snapshot_by_id_or_name(bs
, snapshot_id
);
582 if (snapshot_index
< 0) {
585 sn
= &s
->snapshots
[snapshot_index
];
587 ret
= qcow2_validate_table(bs
, sn
->l1_table_offset
, sn
->l1_size
,
588 sizeof(uint64_t), QCOW_MAX_L1_SIZE
,
589 "Snapshot L1 table", &local_err
);
591 error_report_err(local_err
);
595 if (sn
->disk_size
!= bs
->total_sectors
* BDRV_SECTOR_SIZE
) {
596 error_report("qcow2: Loading snapshots with different disk "
597 "size is not implemented");
603 * Make sure that the current L1 table is big enough to contain the whole
604 * L1 table of the snapshot. If the snapshot L1 table is smaller, the
605 * current one must be padded with zeros.
607 ret
= qcow2_grow_l1_table(bs
, sn
->l1_size
, true);
612 cur_l1_bytes
= s
->l1_size
* sizeof(uint64_t);
613 sn_l1_bytes
= sn
->l1_size
* sizeof(uint64_t);
616 * Copy the snapshot L1 table to the current L1 table.
618 * Before overwriting the old current L1 table on disk, make sure to
619 * increase all refcounts for the clusters referenced by the new one.
620 * Decrease the refcount referenced by the old one only when the L1
621 * table is overwritten.
623 sn_l1_table
= g_try_malloc0(cur_l1_bytes
);
624 if (cur_l1_bytes
&& sn_l1_table
== NULL
) {
629 ret
= bdrv_pread(bs
->file
, sn
->l1_table_offset
,
630 sn_l1_table
, sn_l1_bytes
);
635 ret
= qcow2_update_snapshot_refcount(bs
, sn
->l1_table_offset
,
641 ret
= qcow2_pre_write_overlap_check(bs
, QCOW2_OL_ACTIVE_L1
,
642 s
->l1_table_offset
, cur_l1_bytes
,
648 ret
= bdrv_pwrite_sync(bs
->file
, s
->l1_table_offset
, sn_l1_table
,
655 * Decrease refcount of clusters of current L1 table.
657 * At this point, the in-memory s->l1_table points to the old L1 table,
658 * whereas on disk we already have the new one.
660 * qcow2_update_snapshot_refcount special cases the current L1 table to use
661 * the in-memory data instead of really using the offset to load a new one,
662 * which is why this works.
664 ret
= qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
,
668 * Now update the in-memory L1 table to be in sync with the on-disk one. We
669 * need to do this even if updating refcounts failed.
671 for(i
= 0;i
< s
->l1_size
; i
++) {
672 s
->l1_table
[i
] = be64_to_cpu(sn_l1_table
[i
]);
683 * Update QCOW_OFLAG_COPIED in the active L1 table (it may have changed
684 * when we decreased the refcount of the old snapshot.
686 ret
= qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 0);
693 BdrvCheckResult result
= {0};
694 qcow2_check_refcounts(bs
, &result
, 0);
704 int qcow2_snapshot_delete(BlockDriverState
*bs
,
705 const char *snapshot_id
,
709 BDRVQcow2State
*s
= bs
->opaque
;
711 int snapshot_index
, ret
;
713 if (has_data_file(bs
)) {
717 /* Search the snapshot */
718 snapshot_index
= find_snapshot_by_id_and_name(bs
, snapshot_id
, name
);
719 if (snapshot_index
< 0) {
720 error_setg(errp
, "Can't find the snapshot");
723 sn
= s
->snapshots
[snapshot_index
];
725 ret
= qcow2_validate_table(bs
, sn
.l1_table_offset
, sn
.l1_size
,
726 sizeof(uint64_t), QCOW_MAX_L1_SIZE
,
727 "Snapshot L1 table", errp
);
732 /* Remove it from the snapshot list */
733 memmove(s
->snapshots
+ snapshot_index
,
734 s
->snapshots
+ snapshot_index
+ 1,
735 (s
->nb_snapshots
- snapshot_index
- 1) * sizeof(sn
));
737 ret
= qcow2_write_snapshots(bs
);
739 error_setg_errno(errp
, -ret
,
740 "Failed to remove snapshot from snapshot list");
745 * The snapshot is now unused, clean up. If we fail after this point, we
746 * won't recover but just leak clusters.
748 g_free(sn
.unknown_extra_data
);
753 * Now decrease the refcounts of clusters referenced by the snapshot and
756 ret
= qcow2_update_snapshot_refcount(bs
, sn
.l1_table_offset
,
759 error_setg_errno(errp
, -ret
, "Failed to free the cluster and L1 table");
762 qcow2_free_clusters(bs
, sn
.l1_table_offset
, sn
.l1_size
* sizeof(uint64_t),
763 QCOW2_DISCARD_SNAPSHOT
);
765 /* must update the copied flag on the current cluster offsets */
766 ret
= qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 0);
768 error_setg_errno(errp
, -ret
,
769 "Failed to update snapshot status in disk");
775 BdrvCheckResult result
= {0};
776 qcow2_check_refcounts(bs
, &result
, 0);
782 int qcow2_snapshot_list(BlockDriverState
*bs
, QEMUSnapshotInfo
**psn_tab
)
784 BDRVQcow2State
*s
= bs
->opaque
;
785 QEMUSnapshotInfo
*sn_tab
, *sn_info
;
789 if (has_data_file(bs
)) {
792 if (!s
->nb_snapshots
) {
794 return s
->nb_snapshots
;
797 sn_tab
= g_new0(QEMUSnapshotInfo
, s
->nb_snapshots
);
798 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
799 sn_info
= sn_tab
+ i
;
800 sn
= s
->snapshots
+ i
;
801 pstrcpy(sn_info
->id_str
, sizeof(sn_info
->id_str
),
803 pstrcpy(sn_info
->name
, sizeof(sn_info
->name
),
805 sn_info
->vm_state_size
= sn
->vm_state_size
;
806 sn_info
->date_sec
= sn
->date_sec
;
807 sn_info
->date_nsec
= sn
->date_nsec
;
808 sn_info
->vm_clock_nsec
= sn
->vm_clock_nsec
;
811 return s
->nb_snapshots
;
814 int qcow2_snapshot_load_tmp(BlockDriverState
*bs
,
815 const char *snapshot_id
,
819 int i
, snapshot_index
;
820 BDRVQcow2State
*s
= bs
->opaque
;
822 uint64_t *new_l1_table
;
826 assert(bs
->read_only
);
828 /* Search the snapshot */
829 snapshot_index
= find_snapshot_by_id_and_name(bs
, snapshot_id
, name
);
830 if (snapshot_index
< 0) {
832 "Can't find snapshot");
835 sn
= &s
->snapshots
[snapshot_index
];
837 /* Allocate and read in the snapshot's L1 table */
838 ret
= qcow2_validate_table(bs
, sn
->l1_table_offset
, sn
->l1_size
,
839 sizeof(uint64_t), QCOW_MAX_L1_SIZE
,
840 "Snapshot L1 table", errp
);
844 new_l1_bytes
= sn
->l1_size
* sizeof(uint64_t);
845 new_l1_table
= qemu_try_blockalign(bs
->file
->bs
,
846 ROUND_UP(new_l1_bytes
, 512));
847 if (new_l1_table
== NULL
) {
851 ret
= bdrv_pread(bs
->file
, sn
->l1_table_offset
,
852 new_l1_table
, new_l1_bytes
);
854 error_setg(errp
, "Failed to read l1 table for snapshot");
855 qemu_vfree(new_l1_table
);
859 /* Switch the L1 table */
860 qemu_vfree(s
->l1_table
);
862 s
->l1_size
= sn
->l1_size
;
863 s
->l1_table_offset
= sn
->l1_table_offset
;
864 s
->l1_table
= new_l1_table
;
866 for(i
= 0;i
< s
->l1_size
; i
++) {
867 be64_to_cpus(&s
->l1_table
[i
]);