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
);
48 * If @repair is true, try to repair a broken snapshot table instead
49 * of just returning an error:
51 * - If there were snapshots with too much extra metadata, increment
52 * *extra_data_dropped for each.
53 * This requires the caller to eventually rewrite the whole snapshot
54 * table, which requires cluster allocation. Therefore, this should
55 * be done only after qcow2_check_refcounts() made sure the refcount
56 * structures are valid.
57 * (In the meantime, the image is still valid because
58 * qcow2_check_refcounts() does not do anything with snapshots'
61 static int qcow2_do_read_snapshots(BlockDriverState
*bs
, bool repair
,
62 int *extra_data_dropped
,
65 BDRVQcow2State
*s
= bs
->opaque
;
67 QCowSnapshotExtraData extra
;
69 int i
, id_str_size
, name_size
;
71 uint64_t table_length
= 0;
74 if (!s
->nb_snapshots
) {
76 s
->snapshots_size
= 0;
80 offset
= s
->snapshots_offset
;
81 s
->snapshots
= g_new0(QCowSnapshot
, s
->nb_snapshots
);
83 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
84 bool truncate_unknown_extra_data
= false;
86 table_length
= ROUND_UP(table_length
, 8);
88 /* Read statically sized part of the snapshot header */
89 offset
= ROUND_UP(offset
, 8);
90 ret
= bdrv_pread(bs
->file
, offset
, &h
, sizeof(h
));
92 error_setg_errno(errp
, -ret
, "Failed to read snapshot table");
97 sn
= s
->snapshots
+ i
;
98 sn
->l1_table_offset
= be64_to_cpu(h
.l1_table_offset
);
99 sn
->l1_size
= be32_to_cpu(h
.l1_size
);
100 sn
->vm_state_size
= be32_to_cpu(h
.vm_state_size
);
101 sn
->date_sec
= be32_to_cpu(h
.date_sec
);
102 sn
->date_nsec
= be32_to_cpu(h
.date_nsec
);
103 sn
->vm_clock_nsec
= be64_to_cpu(h
.vm_clock_nsec
);
104 sn
->extra_data_size
= be32_to_cpu(h
.extra_data_size
);
106 id_str_size
= be16_to_cpu(h
.id_str_size
);
107 name_size
= be16_to_cpu(h
.name_size
);
109 if (sn
->extra_data_size
> QCOW_MAX_SNAPSHOT_EXTRA_DATA
) {
112 error_setg(errp
, "Too much extra metadata in snapshot table "
114 error_append_hint(errp
, "You can force-remove this extra "
115 "metadata with qemu-img check -r all\n");
119 fprintf(stderr
, "Discarding too much extra metadata in snapshot "
120 "table entry %i (%" PRIu32
" > %u)\n",
121 i
, sn
->extra_data_size
, QCOW_MAX_SNAPSHOT_EXTRA_DATA
);
123 (*extra_data_dropped
)++;
124 truncate_unknown_extra_data
= true;
127 /* Read known extra data */
128 ret
= bdrv_pread(bs
->file
, offset
, &extra
,
129 MIN(sizeof(extra
), sn
->extra_data_size
));
131 error_setg_errno(errp
, -ret
, "Failed to read snapshot table");
134 offset
+= MIN(sizeof(extra
), sn
->extra_data_size
);
136 if (sn
->extra_data_size
>= endof(QCowSnapshotExtraData
,
137 vm_state_size_large
)) {
138 sn
->vm_state_size
= be64_to_cpu(extra
.vm_state_size_large
);
141 if (sn
->extra_data_size
>= endof(QCowSnapshotExtraData
, disk_size
)) {
142 sn
->disk_size
= be64_to_cpu(extra
.disk_size
);
144 sn
->disk_size
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
147 if (sn
->extra_data_size
> sizeof(extra
)) {
148 uint64_t extra_data_end
;
149 size_t unknown_extra_data_size
;
151 extra_data_end
= offset
+ sn
->extra_data_size
- sizeof(extra
);
153 if (truncate_unknown_extra_data
) {
154 sn
->extra_data_size
= QCOW_MAX_SNAPSHOT_EXTRA_DATA
;
157 /* Store unknown extra data */
158 unknown_extra_data_size
= sn
->extra_data_size
- sizeof(extra
);
159 sn
->unknown_extra_data
= g_malloc(unknown_extra_data_size
);
160 ret
= bdrv_pread(bs
->file
, offset
, sn
->unknown_extra_data
,
161 unknown_extra_data_size
);
163 error_setg_errno(errp
, -ret
,
164 "Failed to read snapshot table");
167 offset
= extra_data_end
;
170 /* Read snapshot ID */
171 sn
->id_str
= g_malloc(id_str_size
+ 1);
172 ret
= bdrv_pread(bs
->file
, offset
, sn
->id_str
, id_str_size
);
174 error_setg_errno(errp
, -ret
, "Failed to read snapshot table");
177 offset
+= id_str_size
;
178 sn
->id_str
[id_str_size
] = '\0';
180 /* Read snapshot name */
181 sn
->name
= g_malloc(name_size
+ 1);
182 ret
= bdrv_pread(bs
->file
, offset
, sn
->name
, name_size
);
184 error_setg_errno(errp
, -ret
, "Failed to read snapshot table");
188 sn
->name
[name_size
] = '\0';
190 /* Note that the extra data may have been truncated */
191 table_length
+= sizeof(h
) + sn
->extra_data_size
+ id_str_size
+
194 assert(table_length
== offset
- s
->snapshots_offset
);
197 if (table_length
> QCOW_MAX_SNAPSHOTS_SIZE
||
198 offset
- s
->snapshots_offset
> INT_MAX
)
201 error_setg(errp
, "Snapshot table is too big");
206 assert(offset
- s
->snapshots_offset
<= INT_MAX
);
207 s
->snapshots_size
= offset
- s
->snapshots_offset
;
211 qcow2_free_snapshots(bs
);
215 int qcow2_read_snapshots(BlockDriverState
*bs
, Error
**errp
)
217 return qcow2_do_read_snapshots(bs
, false, NULL
, errp
);
220 /* add at the end of the file a new list of snapshots */
221 int qcow2_write_snapshots(BlockDriverState
*bs
)
223 BDRVQcow2State
*s
= bs
->opaque
;
225 QCowSnapshotHeader h
;
226 QCowSnapshotExtraData extra
;
227 int i
, name_size
, id_str_size
, snapshots_size
;
229 uint32_t nb_snapshots
;
230 uint64_t snapshots_offset
;
231 } QEMU_PACKED header_data
;
232 int64_t offset
, snapshots_offset
= 0;
235 /* compute the size of the snapshots */
237 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
238 sn
= s
->snapshots
+ i
;
239 offset
= ROUND_UP(offset
, 8);
241 offset
+= MAX(sizeof(extra
), sn
->extra_data_size
);
242 offset
+= strlen(sn
->id_str
);
243 offset
+= strlen(sn
->name
);
245 if (offset
> QCOW_MAX_SNAPSHOTS_SIZE
) {
251 assert(offset
<= INT_MAX
);
252 snapshots_size
= offset
;
254 /* Allocate space for the new snapshot list */
255 snapshots_offset
= qcow2_alloc_clusters(bs
, snapshots_size
);
256 offset
= snapshots_offset
;
261 ret
= bdrv_flush(bs
);
266 /* The snapshot list position has not yet been updated, so these clusters
267 * must indeed be completely free */
268 ret
= qcow2_pre_write_overlap_check(bs
, 0, offset
, snapshots_size
, false);
274 /* Write all snapshots to the new list */
275 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
276 sn
= s
->snapshots
+ i
;
277 memset(&h
, 0, sizeof(h
));
278 h
.l1_table_offset
= cpu_to_be64(sn
->l1_table_offset
);
279 h
.l1_size
= cpu_to_be32(sn
->l1_size
);
280 /* If it doesn't fit in 32 bit, older implementations should treat it
281 * as a disk-only snapshot rather than truncate the VM state */
282 if (sn
->vm_state_size
<= 0xffffffff) {
283 h
.vm_state_size
= cpu_to_be32(sn
->vm_state_size
);
285 h
.date_sec
= cpu_to_be32(sn
->date_sec
);
286 h
.date_nsec
= cpu_to_be32(sn
->date_nsec
);
287 h
.vm_clock_nsec
= cpu_to_be64(sn
->vm_clock_nsec
);
288 h
.extra_data_size
= cpu_to_be32(MAX(sizeof(extra
),
289 sn
->extra_data_size
));
291 memset(&extra
, 0, sizeof(extra
));
292 extra
.vm_state_size_large
= cpu_to_be64(sn
->vm_state_size
);
293 extra
.disk_size
= cpu_to_be64(sn
->disk_size
);
295 id_str_size
= strlen(sn
->id_str
);
296 name_size
= strlen(sn
->name
);
297 assert(id_str_size
<= UINT16_MAX
&& name_size
<= UINT16_MAX
);
298 h
.id_str_size
= cpu_to_be16(id_str_size
);
299 h
.name_size
= cpu_to_be16(name_size
);
300 offset
= ROUND_UP(offset
, 8);
302 ret
= bdrv_pwrite(bs
->file
, offset
, &h
, sizeof(h
));
308 ret
= bdrv_pwrite(bs
->file
, offset
, &extra
, sizeof(extra
));
312 offset
+= sizeof(extra
);
314 if (sn
->extra_data_size
> sizeof(extra
)) {
315 size_t unknown_extra_data_size
=
316 sn
->extra_data_size
- sizeof(extra
);
318 /* qcow2_read_snapshots() ensures no unbounded allocation */
319 assert(unknown_extra_data_size
<= BDRV_REQUEST_MAX_BYTES
);
320 assert(sn
->unknown_extra_data
);
322 ret
= bdrv_pwrite(bs
->file
, offset
, sn
->unknown_extra_data
,
323 unknown_extra_data_size
);
327 offset
+= unknown_extra_data_size
;
330 ret
= bdrv_pwrite(bs
->file
, offset
, sn
->id_str
, id_str_size
);
334 offset
+= id_str_size
;
336 ret
= bdrv_pwrite(bs
->file
, offset
, sn
->name
, name_size
);
344 * Update the header to point to the new snapshot table. This requires the
345 * new table and its refcounts to be stable on disk.
347 ret
= bdrv_flush(bs
);
352 QEMU_BUILD_BUG_ON(offsetof(QCowHeader
, snapshots_offset
) !=
353 endof(QCowHeader
, nb_snapshots
));
355 header_data
.nb_snapshots
= cpu_to_be32(s
->nb_snapshots
);
356 header_data
.snapshots_offset
= cpu_to_be64(snapshots_offset
);
358 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, nb_snapshots
),
359 &header_data
, sizeof(header_data
));
364 /* free the old snapshot table */
365 qcow2_free_clusters(bs
, s
->snapshots_offset
, s
->snapshots_size
,
366 QCOW2_DISCARD_SNAPSHOT
);
367 s
->snapshots_offset
= snapshots_offset
;
368 s
->snapshots_size
= snapshots_size
;
372 if (snapshots_offset
> 0) {
373 qcow2_free_clusters(bs
, snapshots_offset
, snapshots_size
,
374 QCOW2_DISCARD_ALWAYS
);
379 int coroutine_fn
qcow2_check_read_snapshot_table(BlockDriverState
*bs
,
380 BdrvCheckResult
*result
,
383 BDRVQcow2State
*s
= bs
->opaque
;
384 Error
*local_err
= NULL
;
385 int extra_data_dropped
= 0;
388 uint32_t nb_snapshots
;
389 uint64_t snapshots_offset
;
390 } QEMU_PACKED snapshot_table_pointer
;
392 /* qcow2_do_open() discards this information in check mode */
393 ret
= bdrv_pread(bs
->file
, offsetof(QCowHeader
, nb_snapshots
),
394 &snapshot_table_pointer
, sizeof(snapshot_table_pointer
));
396 result
->check_errors
++;
397 fprintf(stderr
, "ERROR failed to read the snapshot table pointer from "
398 "the image header: %s\n", strerror(-ret
));
402 s
->snapshots_offset
= be64_to_cpu(snapshot_table_pointer
.snapshots_offset
);
403 s
->nb_snapshots
= be32_to_cpu(snapshot_table_pointer
.nb_snapshots
);
405 ret
= qcow2_validate_table(bs
, s
->snapshots_offset
, s
->nb_snapshots
,
406 sizeof(QCowSnapshotHeader
),
407 sizeof(QCowSnapshotHeader
) * QCOW_MAX_SNAPSHOTS
,
408 "snapshot table", &local_err
);
410 result
->check_errors
++;
411 error_reportf_err(local_err
, "ERROR ");
413 /* We did not read the snapshot table, so invalidate this information */
414 s
->snapshots_offset
= 0;
420 qemu_co_mutex_unlock(&s
->lock
);
421 ret
= qcow2_do_read_snapshots(bs
, fix
& BDRV_FIX_ERRORS
,
422 &extra_data_dropped
, &local_err
);
423 qemu_co_mutex_lock(&s
->lock
);
425 result
->check_errors
++;
426 error_reportf_err(local_err
,
427 "ERROR failed to read the snapshot table: ");
429 /* We did not read the snapshot table, so invalidate this information */
430 s
->snapshots_offset
= 0;
435 result
->corruptions
+= extra_data_dropped
;
440 int coroutine_fn
qcow2_check_fix_snapshot_table(BlockDriverState
*bs
,
441 BdrvCheckResult
*result
,
444 BDRVQcow2State
*s
= bs
->opaque
;
447 if (result
->corruptions
&& (fix
& BDRV_FIX_ERRORS
)) {
448 qemu_co_mutex_unlock(&s
->lock
);
449 ret
= qcow2_write_snapshots(bs
);
450 qemu_co_mutex_lock(&s
->lock
);
452 result
->check_errors
++;
453 fprintf(stderr
, "ERROR failed to update snapshot table: %s\n",
458 result
->corruptions_fixed
+= result
->corruptions
;
459 result
->corruptions
= 0;
465 static void find_new_snapshot_id(BlockDriverState
*bs
,
466 char *id_str
, int id_str_size
)
468 BDRVQcow2State
*s
= bs
->opaque
;
471 unsigned long id
, id_max
= 0;
473 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
474 sn
= s
->snapshots
+ i
;
475 id
= strtoul(sn
->id_str
, NULL
, 10);
479 snprintf(id_str
, id_str_size
, "%lu", id_max
+ 1);
482 static int find_snapshot_by_id_and_name(BlockDriverState
*bs
,
486 BDRVQcow2State
*s
= bs
->opaque
;
490 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
491 if (!strcmp(s
->snapshots
[i
].id_str
, id
) &&
492 !strcmp(s
->snapshots
[i
].name
, name
)) {
497 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
498 if (!strcmp(s
->snapshots
[i
].id_str
, id
)) {
503 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
504 if (!strcmp(s
->snapshots
[i
].name
, name
)) {
513 static int find_snapshot_by_id_or_name(BlockDriverState
*bs
,
514 const char *id_or_name
)
518 ret
= find_snapshot_by_id_and_name(bs
, id_or_name
, NULL
);
522 return find_snapshot_by_id_and_name(bs
, NULL
, id_or_name
);
525 /* if no id is provided, a new one is constructed */
526 int qcow2_snapshot_create(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
)
528 BDRVQcow2State
*s
= bs
->opaque
;
529 QCowSnapshot
*new_snapshot_list
= NULL
;
530 QCowSnapshot
*old_snapshot_list
= NULL
;
531 QCowSnapshot sn1
, *sn
= &sn1
;
533 uint64_t *l1_table
= NULL
;
534 int64_t l1_table_offset
;
536 if (s
->nb_snapshots
>= QCOW_MAX_SNAPSHOTS
) {
540 if (has_data_file(bs
)) {
544 memset(sn
, 0, sizeof(*sn
));
547 find_new_snapshot_id(bs
, sn_info
->id_str
, sizeof(sn_info
->id_str
));
549 /* Populate sn with passed data */
550 sn
->id_str
= g_strdup(sn_info
->id_str
);
551 sn
->name
= g_strdup(sn_info
->name
);
553 sn
->disk_size
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
554 sn
->vm_state_size
= sn_info
->vm_state_size
;
555 sn
->date_sec
= sn_info
->date_sec
;
556 sn
->date_nsec
= sn_info
->date_nsec
;
557 sn
->vm_clock_nsec
= sn_info
->vm_clock_nsec
;
558 sn
->extra_data_size
= sizeof(QCowSnapshotExtraData
);
560 /* Allocate the L1 table of the snapshot and copy the current one there. */
561 l1_table_offset
= qcow2_alloc_clusters(bs
, s
->l1_size
* sizeof(uint64_t));
562 if (l1_table_offset
< 0) {
563 ret
= l1_table_offset
;
567 sn
->l1_table_offset
= l1_table_offset
;
568 sn
->l1_size
= s
->l1_size
;
570 l1_table
= g_try_new(uint64_t, s
->l1_size
);
571 if (s
->l1_size
&& l1_table
== NULL
) {
576 for(i
= 0; i
< s
->l1_size
; i
++) {
577 l1_table
[i
] = cpu_to_be64(s
->l1_table
[i
]);
580 ret
= qcow2_pre_write_overlap_check(bs
, 0, sn
->l1_table_offset
,
581 s
->l1_size
* sizeof(uint64_t), false);
586 ret
= bdrv_pwrite(bs
->file
, sn
->l1_table_offset
, l1_table
,
587 s
->l1_size
* sizeof(uint64_t));
596 * Increase the refcounts of all clusters and make sure everything is
597 * stable on disk before updating the snapshot table to contain a pointer
598 * to the new L1 table.
600 ret
= qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 1);
605 /* Append the new snapshot to the snapshot list */
606 new_snapshot_list
= g_new(QCowSnapshot
, s
->nb_snapshots
+ 1);
608 memcpy(new_snapshot_list
, s
->snapshots
,
609 s
->nb_snapshots
* sizeof(QCowSnapshot
));
610 old_snapshot_list
= s
->snapshots
;
612 s
->snapshots
= new_snapshot_list
;
613 s
->snapshots
[s
->nb_snapshots
++] = *sn
;
615 ret
= qcow2_write_snapshots(bs
);
617 g_free(s
->snapshots
);
618 s
->snapshots
= old_snapshot_list
;
623 g_free(old_snapshot_list
);
625 /* The VM state isn't needed any more in the active L1 table; in fact, it
626 * hurts by causing expensive COW for the next snapshot. */
627 qcow2_cluster_discard(bs
, qcow2_vm_state_offset(s
),
628 ROUND_UP(sn
->vm_state_size
, s
->cluster_size
),
629 QCOW2_DISCARD_NEVER
, false);
633 BdrvCheckResult result
= {0};
634 qcow2_check_refcounts(bs
, &result
, 0);
647 /* copy the snapshot 'snapshot_name' into the current disk image */
648 int qcow2_snapshot_goto(BlockDriverState
*bs
, const char *snapshot_id
)
650 BDRVQcow2State
*s
= bs
->opaque
;
652 Error
*local_err
= NULL
;
653 int i
, snapshot_index
;
654 int cur_l1_bytes
, sn_l1_bytes
;
656 uint64_t *sn_l1_table
= NULL
;
658 if (has_data_file(bs
)) {
662 /* Search the snapshot */
663 snapshot_index
= find_snapshot_by_id_or_name(bs
, snapshot_id
);
664 if (snapshot_index
< 0) {
667 sn
= &s
->snapshots
[snapshot_index
];
669 ret
= qcow2_validate_table(bs
, sn
->l1_table_offset
, sn
->l1_size
,
670 sizeof(uint64_t), QCOW_MAX_L1_SIZE
,
671 "Snapshot L1 table", &local_err
);
673 error_report_err(local_err
);
677 if (sn
->disk_size
!= bs
->total_sectors
* BDRV_SECTOR_SIZE
) {
678 error_report("qcow2: Loading snapshots with different disk "
679 "size is not implemented");
685 * Make sure that the current L1 table is big enough to contain the whole
686 * L1 table of the snapshot. If the snapshot L1 table is smaller, the
687 * current one must be padded with zeros.
689 ret
= qcow2_grow_l1_table(bs
, sn
->l1_size
, true);
694 cur_l1_bytes
= s
->l1_size
* sizeof(uint64_t);
695 sn_l1_bytes
= sn
->l1_size
* sizeof(uint64_t);
698 * Copy the snapshot L1 table to the current L1 table.
700 * Before overwriting the old current L1 table on disk, make sure to
701 * increase all refcounts for the clusters referenced by the new one.
702 * Decrease the refcount referenced by the old one only when the L1
703 * table is overwritten.
705 sn_l1_table
= g_try_malloc0(cur_l1_bytes
);
706 if (cur_l1_bytes
&& sn_l1_table
== NULL
) {
711 ret
= bdrv_pread(bs
->file
, sn
->l1_table_offset
,
712 sn_l1_table
, sn_l1_bytes
);
717 ret
= qcow2_update_snapshot_refcount(bs
, sn
->l1_table_offset
,
723 ret
= qcow2_pre_write_overlap_check(bs
, QCOW2_OL_ACTIVE_L1
,
724 s
->l1_table_offset
, cur_l1_bytes
,
730 ret
= bdrv_pwrite_sync(bs
->file
, s
->l1_table_offset
, sn_l1_table
,
737 * Decrease refcount of clusters of current L1 table.
739 * At this point, the in-memory s->l1_table points to the old L1 table,
740 * whereas on disk we already have the new one.
742 * qcow2_update_snapshot_refcount special cases the current L1 table to use
743 * the in-memory data instead of really using the offset to load a new one,
744 * which is why this works.
746 ret
= qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
,
750 * Now update the in-memory L1 table to be in sync with the on-disk one. We
751 * need to do this even if updating refcounts failed.
753 for(i
= 0;i
< s
->l1_size
; i
++) {
754 s
->l1_table
[i
] = be64_to_cpu(sn_l1_table
[i
]);
765 * Update QCOW_OFLAG_COPIED in the active L1 table (it may have changed
766 * when we decreased the refcount of the old snapshot.
768 ret
= qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 0);
775 BdrvCheckResult result
= {0};
776 qcow2_check_refcounts(bs
, &result
, 0);
786 int qcow2_snapshot_delete(BlockDriverState
*bs
,
787 const char *snapshot_id
,
791 BDRVQcow2State
*s
= bs
->opaque
;
793 int snapshot_index
, ret
;
795 if (has_data_file(bs
)) {
799 /* Search the snapshot */
800 snapshot_index
= find_snapshot_by_id_and_name(bs
, snapshot_id
, name
);
801 if (snapshot_index
< 0) {
802 error_setg(errp
, "Can't find the snapshot");
805 sn
= s
->snapshots
[snapshot_index
];
807 ret
= qcow2_validate_table(bs
, sn
.l1_table_offset
, sn
.l1_size
,
808 sizeof(uint64_t), QCOW_MAX_L1_SIZE
,
809 "Snapshot L1 table", errp
);
814 /* Remove it from the snapshot list */
815 memmove(s
->snapshots
+ snapshot_index
,
816 s
->snapshots
+ snapshot_index
+ 1,
817 (s
->nb_snapshots
- snapshot_index
- 1) * sizeof(sn
));
819 ret
= qcow2_write_snapshots(bs
);
821 error_setg_errno(errp
, -ret
,
822 "Failed to remove snapshot from snapshot list");
827 * The snapshot is now unused, clean up. If we fail after this point, we
828 * won't recover but just leak clusters.
830 g_free(sn
.unknown_extra_data
);
835 * Now decrease the refcounts of clusters referenced by the snapshot and
838 ret
= qcow2_update_snapshot_refcount(bs
, sn
.l1_table_offset
,
841 error_setg_errno(errp
, -ret
, "Failed to free the cluster and L1 table");
844 qcow2_free_clusters(bs
, sn
.l1_table_offset
, sn
.l1_size
* sizeof(uint64_t),
845 QCOW2_DISCARD_SNAPSHOT
);
847 /* must update the copied flag on the current cluster offsets */
848 ret
= qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 0);
850 error_setg_errno(errp
, -ret
,
851 "Failed to update snapshot status in disk");
857 BdrvCheckResult result
= {0};
858 qcow2_check_refcounts(bs
, &result
, 0);
864 int qcow2_snapshot_list(BlockDriverState
*bs
, QEMUSnapshotInfo
**psn_tab
)
866 BDRVQcow2State
*s
= bs
->opaque
;
867 QEMUSnapshotInfo
*sn_tab
, *sn_info
;
871 if (has_data_file(bs
)) {
874 if (!s
->nb_snapshots
) {
876 return s
->nb_snapshots
;
879 sn_tab
= g_new0(QEMUSnapshotInfo
, s
->nb_snapshots
);
880 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
881 sn_info
= sn_tab
+ i
;
882 sn
= s
->snapshots
+ i
;
883 pstrcpy(sn_info
->id_str
, sizeof(sn_info
->id_str
),
885 pstrcpy(sn_info
->name
, sizeof(sn_info
->name
),
887 sn_info
->vm_state_size
= sn
->vm_state_size
;
888 sn_info
->date_sec
= sn
->date_sec
;
889 sn_info
->date_nsec
= sn
->date_nsec
;
890 sn_info
->vm_clock_nsec
= sn
->vm_clock_nsec
;
893 return s
->nb_snapshots
;
896 int qcow2_snapshot_load_tmp(BlockDriverState
*bs
,
897 const char *snapshot_id
,
901 int i
, snapshot_index
;
902 BDRVQcow2State
*s
= bs
->opaque
;
904 uint64_t *new_l1_table
;
908 assert(bs
->read_only
);
910 /* Search the snapshot */
911 snapshot_index
= find_snapshot_by_id_and_name(bs
, snapshot_id
, name
);
912 if (snapshot_index
< 0) {
914 "Can't find snapshot");
917 sn
= &s
->snapshots
[snapshot_index
];
919 /* Allocate and read in the snapshot's L1 table */
920 ret
= qcow2_validate_table(bs
, sn
->l1_table_offset
, sn
->l1_size
,
921 sizeof(uint64_t), QCOW_MAX_L1_SIZE
,
922 "Snapshot L1 table", errp
);
926 new_l1_bytes
= sn
->l1_size
* sizeof(uint64_t);
927 new_l1_table
= qemu_try_blockalign(bs
->file
->bs
,
928 ROUND_UP(new_l1_bytes
, 512));
929 if (new_l1_table
== NULL
) {
933 ret
= bdrv_pread(bs
->file
, sn
->l1_table_offset
,
934 new_l1_table
, new_l1_bytes
);
936 error_setg(errp
, "Failed to read l1 table for snapshot");
937 qemu_vfree(new_l1_table
);
941 /* Switch the L1 table */
942 qemu_vfree(s
->l1_table
);
944 s
->l1_size
= sn
->l1_size
;
945 s
->l1_table_offset
= sn
->l1_table_offset
;
946 s
->l1_table
= new_l1_table
;
948 for(i
= 0;i
< s
->l1_size
; i
++) {
949 be64_to_cpus(&s
->l1_table
[i
]);