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"
33 static void qcow2_free_single_snapshot(BlockDriverState
*bs
, int i
)
35 BDRVQcow2State
*s
= bs
->opaque
;
37 assert(i
>= 0 && i
< s
->nb_snapshots
);
38 g_free(s
->snapshots
[i
].name
);
39 g_free(s
->snapshots
[i
].id_str
);
40 g_free(s
->snapshots
[i
].unknown_extra_data
);
41 memset(&s
->snapshots
[i
], 0, sizeof(s
->snapshots
[i
]));
44 void qcow2_free_snapshots(BlockDriverState
*bs
)
46 BDRVQcow2State
*s
= bs
->opaque
;
49 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
50 qcow2_free_single_snapshot(bs
, i
);
58 * If @repair is true, try to repair a broken snapshot table instead
59 * of just returning an error:
61 * - If the snapshot table was too long, set *nb_clusters_reduced to
62 * the number of snapshots removed off the end.
63 * The caller will update the on-disk nb_snapshots accordingly;
64 * this leaks clusters, but is safe.
65 * (The on-disk information must be updated before
66 * qcow2_check_refcounts(), because that function relies on
67 * s->nb_snapshots to reflect the on-disk value.)
69 * - If there were snapshots with too much extra metadata, increment
70 * *extra_data_dropped for each.
71 * This requires the caller to eventually rewrite the whole snapshot
72 * table, which requires cluster allocation. Therefore, this should
73 * be done only after qcow2_check_refcounts() made sure the refcount
74 * structures are valid.
75 * (In the meantime, the image is still valid because
76 * qcow2_check_refcounts() does not do anything with snapshots'
79 static int qcow2_do_read_snapshots(BlockDriverState
*bs
, bool repair
,
80 int *nb_clusters_reduced
,
81 int *extra_data_dropped
,
84 BDRVQcow2State
*s
= bs
->opaque
;
86 QCowSnapshotExtraData extra
;
88 int i
, id_str_size
, name_size
;
89 int64_t offset
, pre_sn_offset
;
90 uint64_t table_length
= 0;
93 if (!s
->nb_snapshots
) {
95 s
->snapshots_size
= 0;
99 offset
= s
->snapshots_offset
;
100 s
->snapshots
= g_new0(QCowSnapshot
, s
->nb_snapshots
);
102 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
103 bool truncate_unknown_extra_data
= false;
105 pre_sn_offset
= offset
;
106 table_length
= ROUND_UP(table_length
, 8);
108 /* Read statically sized part of the snapshot header */
109 offset
= ROUND_UP(offset
, 8);
110 ret
= bdrv_pread(bs
->file
, offset
, &h
, sizeof(h
));
112 error_setg_errno(errp
, -ret
, "Failed to read snapshot table");
117 sn
= s
->snapshots
+ i
;
118 sn
->l1_table_offset
= be64_to_cpu(h
.l1_table_offset
);
119 sn
->l1_size
= be32_to_cpu(h
.l1_size
);
120 sn
->vm_state_size
= be32_to_cpu(h
.vm_state_size
);
121 sn
->date_sec
= be32_to_cpu(h
.date_sec
);
122 sn
->date_nsec
= be32_to_cpu(h
.date_nsec
);
123 sn
->vm_clock_nsec
= be64_to_cpu(h
.vm_clock_nsec
);
124 sn
->extra_data_size
= be32_to_cpu(h
.extra_data_size
);
126 id_str_size
= be16_to_cpu(h
.id_str_size
);
127 name_size
= be16_to_cpu(h
.name_size
);
129 if (sn
->extra_data_size
> QCOW_MAX_SNAPSHOT_EXTRA_DATA
) {
132 error_setg(errp
, "Too much extra metadata in snapshot table "
134 error_append_hint(errp
, "You can force-remove this extra "
135 "metadata with qemu-img check -r all\n");
139 fprintf(stderr
, "Discarding too much extra metadata in snapshot "
140 "table entry %i (%" PRIu32
" > %u)\n",
141 i
, sn
->extra_data_size
, QCOW_MAX_SNAPSHOT_EXTRA_DATA
);
143 (*extra_data_dropped
)++;
144 truncate_unknown_extra_data
= true;
147 /* Read known extra data */
148 ret
= bdrv_pread(bs
->file
, offset
, &extra
,
149 MIN(sizeof(extra
), sn
->extra_data_size
));
151 error_setg_errno(errp
, -ret
, "Failed to read snapshot table");
154 offset
+= MIN(sizeof(extra
), sn
->extra_data_size
);
156 if (sn
->extra_data_size
>= endof(QCowSnapshotExtraData
,
157 vm_state_size_large
)) {
158 sn
->vm_state_size
= be64_to_cpu(extra
.vm_state_size_large
);
161 if (sn
->extra_data_size
>= endof(QCowSnapshotExtraData
, disk_size
)) {
162 sn
->disk_size
= be64_to_cpu(extra
.disk_size
);
164 sn
->disk_size
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
167 if (sn
->extra_data_size
> sizeof(extra
)) {
168 uint64_t extra_data_end
;
169 size_t unknown_extra_data_size
;
171 extra_data_end
= offset
+ sn
->extra_data_size
- sizeof(extra
);
173 if (truncate_unknown_extra_data
) {
174 sn
->extra_data_size
= QCOW_MAX_SNAPSHOT_EXTRA_DATA
;
177 /* Store unknown extra data */
178 unknown_extra_data_size
= sn
->extra_data_size
- sizeof(extra
);
179 sn
->unknown_extra_data
= g_malloc(unknown_extra_data_size
);
180 ret
= bdrv_pread(bs
->file
, offset
, sn
->unknown_extra_data
,
181 unknown_extra_data_size
);
183 error_setg_errno(errp
, -ret
,
184 "Failed to read snapshot table");
187 offset
= extra_data_end
;
190 /* Read snapshot ID */
191 sn
->id_str
= g_malloc(id_str_size
+ 1);
192 ret
= bdrv_pread(bs
->file
, offset
, sn
->id_str
, id_str_size
);
194 error_setg_errno(errp
, -ret
, "Failed to read snapshot table");
197 offset
+= id_str_size
;
198 sn
->id_str
[id_str_size
] = '\0';
200 /* Read snapshot name */
201 sn
->name
= g_malloc(name_size
+ 1);
202 ret
= bdrv_pread(bs
->file
, offset
, sn
->name
, name_size
);
204 error_setg_errno(errp
, -ret
, "Failed to read snapshot table");
208 sn
->name
[name_size
] = '\0';
210 /* Note that the extra data may have been truncated */
211 table_length
+= sizeof(h
) + sn
->extra_data_size
+ id_str_size
+
214 assert(table_length
== offset
- s
->snapshots_offset
);
217 if (table_length
> QCOW_MAX_SNAPSHOTS_SIZE
||
218 offset
- s
->snapshots_offset
> INT_MAX
)
222 error_setg(errp
, "Snapshot table is too big");
223 error_append_hint(errp
, "You can force-remove all %u "
224 "overhanging snapshots with qemu-img check "
225 "-r all\n", s
->nb_snapshots
- i
);
229 fprintf(stderr
, "Discarding %u overhanging snapshots (snapshot "
230 "table is too big)\n", s
->nb_snapshots
- i
);
232 *nb_clusters_reduced
+= (s
->nb_snapshots
- i
);
234 /* Discard current snapshot also */
235 qcow2_free_single_snapshot(bs
, i
);
238 * This leaks all the rest of the snapshot table and the
239 * snapshots' clusters, but we run in check -r all mode,
240 * so qcow2_check_refcounts() will take care of it.
243 offset
= pre_sn_offset
;
248 assert(offset
- s
->snapshots_offset
<= INT_MAX
);
249 s
->snapshots_size
= offset
- s
->snapshots_offset
;
253 qcow2_free_snapshots(bs
);
257 int qcow2_read_snapshots(BlockDriverState
*bs
, Error
**errp
)
259 return qcow2_do_read_snapshots(bs
, false, NULL
, NULL
, errp
);
262 /* add at the end of the file a new list of snapshots */
263 int qcow2_write_snapshots(BlockDriverState
*bs
)
265 BDRVQcow2State
*s
= bs
->opaque
;
267 QCowSnapshotHeader h
;
268 QCowSnapshotExtraData extra
;
269 int i
, name_size
, id_str_size
, snapshots_size
;
271 uint32_t nb_snapshots
;
272 uint64_t snapshots_offset
;
273 } QEMU_PACKED header_data
;
274 int64_t offset
, snapshots_offset
= 0;
277 /* compute the size of the snapshots */
279 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
280 sn
= s
->snapshots
+ i
;
281 offset
= ROUND_UP(offset
, 8);
283 offset
+= MAX(sizeof(extra
), sn
->extra_data_size
);
284 offset
+= strlen(sn
->id_str
);
285 offset
+= strlen(sn
->name
);
287 if (offset
> QCOW_MAX_SNAPSHOTS_SIZE
) {
293 assert(offset
<= INT_MAX
);
294 snapshots_size
= offset
;
296 /* Allocate space for the new snapshot list */
297 snapshots_offset
= qcow2_alloc_clusters(bs
, snapshots_size
);
298 offset
= snapshots_offset
;
303 ret
= bdrv_flush(bs
);
308 /* The snapshot list position has not yet been updated, so these clusters
309 * must indeed be completely free */
310 ret
= qcow2_pre_write_overlap_check(bs
, 0, offset
, snapshots_size
, false);
316 /* Write all snapshots to the new list */
317 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
318 sn
= s
->snapshots
+ i
;
319 memset(&h
, 0, sizeof(h
));
320 h
.l1_table_offset
= cpu_to_be64(sn
->l1_table_offset
);
321 h
.l1_size
= cpu_to_be32(sn
->l1_size
);
322 /* If it doesn't fit in 32 bit, older implementations should treat it
323 * as a disk-only snapshot rather than truncate the VM state */
324 if (sn
->vm_state_size
<= 0xffffffff) {
325 h
.vm_state_size
= cpu_to_be32(sn
->vm_state_size
);
327 h
.date_sec
= cpu_to_be32(sn
->date_sec
);
328 h
.date_nsec
= cpu_to_be32(sn
->date_nsec
);
329 h
.vm_clock_nsec
= cpu_to_be64(sn
->vm_clock_nsec
);
330 h
.extra_data_size
= cpu_to_be32(MAX(sizeof(extra
),
331 sn
->extra_data_size
));
333 memset(&extra
, 0, sizeof(extra
));
334 extra
.vm_state_size_large
= cpu_to_be64(sn
->vm_state_size
);
335 extra
.disk_size
= cpu_to_be64(sn
->disk_size
);
337 id_str_size
= strlen(sn
->id_str
);
338 name_size
= strlen(sn
->name
);
339 assert(id_str_size
<= UINT16_MAX
&& name_size
<= UINT16_MAX
);
340 h
.id_str_size
= cpu_to_be16(id_str_size
);
341 h
.name_size
= cpu_to_be16(name_size
);
342 offset
= ROUND_UP(offset
, 8);
344 ret
= bdrv_pwrite(bs
->file
, offset
, &h
, sizeof(h
));
350 ret
= bdrv_pwrite(bs
->file
, offset
, &extra
, sizeof(extra
));
354 offset
+= sizeof(extra
);
356 if (sn
->extra_data_size
> sizeof(extra
)) {
357 size_t unknown_extra_data_size
=
358 sn
->extra_data_size
- sizeof(extra
);
360 /* qcow2_read_snapshots() ensures no unbounded allocation */
361 assert(unknown_extra_data_size
<= BDRV_REQUEST_MAX_BYTES
);
362 assert(sn
->unknown_extra_data
);
364 ret
= bdrv_pwrite(bs
->file
, offset
, sn
->unknown_extra_data
,
365 unknown_extra_data_size
);
369 offset
+= unknown_extra_data_size
;
372 ret
= bdrv_pwrite(bs
->file
, offset
, sn
->id_str
, id_str_size
);
376 offset
+= id_str_size
;
378 ret
= bdrv_pwrite(bs
->file
, offset
, sn
->name
, name_size
);
386 * Update the header to point to the new snapshot table. This requires the
387 * new table and its refcounts to be stable on disk.
389 ret
= bdrv_flush(bs
);
394 QEMU_BUILD_BUG_ON(offsetof(QCowHeader
, snapshots_offset
) !=
395 endof(QCowHeader
, nb_snapshots
));
397 header_data
.nb_snapshots
= cpu_to_be32(s
->nb_snapshots
);
398 header_data
.snapshots_offset
= cpu_to_be64(snapshots_offset
);
400 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, nb_snapshots
),
401 &header_data
, sizeof(header_data
));
406 /* free the old snapshot table */
407 qcow2_free_clusters(bs
, s
->snapshots_offset
, s
->snapshots_size
,
408 QCOW2_DISCARD_SNAPSHOT
);
409 s
->snapshots_offset
= snapshots_offset
;
410 s
->snapshots_size
= snapshots_size
;
414 if (snapshots_offset
> 0) {
415 qcow2_free_clusters(bs
, snapshots_offset
, snapshots_size
,
416 QCOW2_DISCARD_ALWAYS
);
421 int coroutine_fn
qcow2_check_read_snapshot_table(BlockDriverState
*bs
,
422 BdrvCheckResult
*result
,
425 BDRVQcow2State
*s
= bs
->opaque
;
426 Error
*local_err
= NULL
;
427 int nb_clusters_reduced
= 0;
428 int extra_data_dropped
= 0;
431 uint32_t nb_snapshots
;
432 uint64_t snapshots_offset
;
433 } QEMU_PACKED snapshot_table_pointer
;
435 /* qcow2_do_open() discards this information in check mode */
436 ret
= bdrv_pread(bs
->file
, offsetof(QCowHeader
, nb_snapshots
),
437 &snapshot_table_pointer
, sizeof(snapshot_table_pointer
));
439 result
->check_errors
++;
440 fprintf(stderr
, "ERROR failed to read the snapshot table pointer from "
441 "the image header: %s\n", strerror(-ret
));
445 s
->snapshots_offset
= be64_to_cpu(snapshot_table_pointer
.snapshots_offset
);
446 s
->nb_snapshots
= be32_to_cpu(snapshot_table_pointer
.nb_snapshots
);
448 if (s
->nb_snapshots
> QCOW_MAX_SNAPSHOTS
&& (fix
& BDRV_FIX_ERRORS
)) {
449 fprintf(stderr
, "Discarding %u overhanging snapshots\n",
450 s
->nb_snapshots
- QCOW_MAX_SNAPSHOTS
);
452 nb_clusters_reduced
+= s
->nb_snapshots
- QCOW_MAX_SNAPSHOTS
;
453 s
->nb_snapshots
= QCOW_MAX_SNAPSHOTS
;
456 ret
= qcow2_validate_table(bs
, s
->snapshots_offset
, s
->nb_snapshots
,
457 sizeof(QCowSnapshotHeader
),
458 sizeof(QCowSnapshotHeader
) * QCOW_MAX_SNAPSHOTS
,
459 "snapshot table", &local_err
);
461 result
->check_errors
++;
462 error_reportf_err(local_err
, "ERROR ");
464 if (s
->nb_snapshots
> QCOW_MAX_SNAPSHOTS
) {
465 fprintf(stderr
, "You can force-remove all %u overhanging snapshots "
466 "with qemu-img check -r all\n",
467 s
->nb_snapshots
- QCOW_MAX_SNAPSHOTS
);
470 /* We did not read the snapshot table, so invalidate this information */
471 s
->snapshots_offset
= 0;
477 qemu_co_mutex_unlock(&s
->lock
);
478 ret
= qcow2_do_read_snapshots(bs
, fix
& BDRV_FIX_ERRORS
,
479 &nb_clusters_reduced
, &extra_data_dropped
,
481 qemu_co_mutex_lock(&s
->lock
);
483 result
->check_errors
++;
484 error_reportf_err(local_err
,
485 "ERROR failed to read the snapshot table: ");
487 /* We did not read the snapshot table, so invalidate this information */
488 s
->snapshots_offset
= 0;
493 result
->corruptions
+= nb_clusters_reduced
+ extra_data_dropped
;
495 if (nb_clusters_reduced
) {
497 * Update image header now, because:
498 * (1) qcow2_check_refcounts() relies on s->nb_snapshots to be
499 * the same as what the image header says,
500 * (2) this leaks clusters, but qcow2_check_refcounts() will
503 assert(fix
& BDRV_FIX_ERRORS
);
505 snapshot_table_pointer
.nb_snapshots
= cpu_to_be32(s
->nb_snapshots
);
506 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, nb_snapshots
),
507 &snapshot_table_pointer
.nb_snapshots
,
508 sizeof(snapshot_table_pointer
.nb_snapshots
));
510 result
->check_errors
++;
511 fprintf(stderr
, "ERROR failed to update the snapshot count in the "
512 "image header: %s\n", strerror(-ret
));
516 result
->corruptions_fixed
+= nb_clusters_reduced
;
517 result
->corruptions
-= nb_clusters_reduced
;
521 * All of v3 images' snapshot table entries need to have at least
522 * 16 bytes of extra data.
524 if (s
->qcow_version
>= 3) {
526 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
527 if (s
->snapshots
[i
].extra_data_size
<
528 sizeof_field(QCowSnapshotExtraData
, vm_state_size_large
) +
529 sizeof_field(QCowSnapshotExtraData
, disk_size
))
531 result
->corruptions
++;
532 fprintf(stderr
, "%s snapshot table entry %i is incomplete\n",
533 fix
& BDRV_FIX_ERRORS
? "Repairing" : "ERROR", i
);
541 int coroutine_fn
qcow2_check_fix_snapshot_table(BlockDriverState
*bs
,
542 BdrvCheckResult
*result
,
545 BDRVQcow2State
*s
= bs
->opaque
;
548 if (result
->corruptions
&& (fix
& BDRV_FIX_ERRORS
)) {
549 qemu_co_mutex_unlock(&s
->lock
);
550 ret
= qcow2_write_snapshots(bs
);
551 qemu_co_mutex_lock(&s
->lock
);
553 result
->check_errors
++;
554 fprintf(stderr
, "ERROR failed to update snapshot table: %s\n",
559 result
->corruptions_fixed
+= result
->corruptions
;
560 result
->corruptions
= 0;
566 static void find_new_snapshot_id(BlockDriverState
*bs
,
567 char *id_str
, int id_str_size
)
569 BDRVQcow2State
*s
= bs
->opaque
;
572 unsigned long id
, id_max
= 0;
574 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
575 sn
= s
->snapshots
+ i
;
576 id
= strtoul(sn
->id_str
, NULL
, 10);
580 snprintf(id_str
, id_str_size
, "%lu", id_max
+ 1);
583 static int find_snapshot_by_id_and_name(BlockDriverState
*bs
,
587 BDRVQcow2State
*s
= bs
->opaque
;
591 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
592 if (!strcmp(s
->snapshots
[i
].id_str
, id
) &&
593 !strcmp(s
->snapshots
[i
].name
, name
)) {
598 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
599 if (!strcmp(s
->snapshots
[i
].id_str
, id
)) {
604 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
605 if (!strcmp(s
->snapshots
[i
].name
, name
)) {
614 static int find_snapshot_by_id_or_name(BlockDriverState
*bs
,
615 const char *id_or_name
)
619 ret
= find_snapshot_by_id_and_name(bs
, id_or_name
, NULL
);
623 return find_snapshot_by_id_and_name(bs
, NULL
, id_or_name
);
626 /* if no id is provided, a new one is constructed */
627 int qcow2_snapshot_create(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
)
629 BDRVQcow2State
*s
= bs
->opaque
;
630 QCowSnapshot
*new_snapshot_list
= NULL
;
631 QCowSnapshot
*old_snapshot_list
= NULL
;
632 QCowSnapshot sn1
, *sn
= &sn1
;
634 uint64_t *l1_table
= NULL
;
635 int64_t l1_table_offset
;
637 if (s
->nb_snapshots
>= QCOW_MAX_SNAPSHOTS
) {
641 if (has_data_file(bs
)) {
645 memset(sn
, 0, sizeof(*sn
));
648 find_new_snapshot_id(bs
, sn_info
->id_str
, sizeof(sn_info
->id_str
));
650 /* Populate sn with passed data */
651 sn
->id_str
= g_strdup(sn_info
->id_str
);
652 sn
->name
= g_strdup(sn_info
->name
);
654 sn
->disk_size
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
655 sn
->vm_state_size
= sn_info
->vm_state_size
;
656 sn
->date_sec
= sn_info
->date_sec
;
657 sn
->date_nsec
= sn_info
->date_nsec
;
658 sn
->vm_clock_nsec
= sn_info
->vm_clock_nsec
;
659 sn
->extra_data_size
= sizeof(QCowSnapshotExtraData
);
661 /* Allocate the L1 table of the snapshot and copy the current one there. */
662 l1_table_offset
= qcow2_alloc_clusters(bs
, s
->l1_size
* sizeof(uint64_t));
663 if (l1_table_offset
< 0) {
664 ret
= l1_table_offset
;
668 sn
->l1_table_offset
= l1_table_offset
;
669 sn
->l1_size
= s
->l1_size
;
671 l1_table
= g_try_new(uint64_t, s
->l1_size
);
672 if (s
->l1_size
&& l1_table
== NULL
) {
677 for(i
= 0; i
< s
->l1_size
; i
++) {
678 l1_table
[i
] = cpu_to_be64(s
->l1_table
[i
]);
681 ret
= qcow2_pre_write_overlap_check(bs
, 0, sn
->l1_table_offset
,
682 s
->l1_size
* sizeof(uint64_t), false);
687 ret
= bdrv_pwrite(bs
->file
, sn
->l1_table_offset
, l1_table
,
688 s
->l1_size
* sizeof(uint64_t));
697 * Increase the refcounts of all clusters and make sure everything is
698 * stable on disk before updating the snapshot table to contain a pointer
699 * to the new L1 table.
701 ret
= qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 1);
706 /* Append the new snapshot to the snapshot list */
707 new_snapshot_list
= g_new(QCowSnapshot
, s
->nb_snapshots
+ 1);
709 memcpy(new_snapshot_list
, s
->snapshots
,
710 s
->nb_snapshots
* sizeof(QCowSnapshot
));
711 old_snapshot_list
= s
->snapshots
;
713 s
->snapshots
= new_snapshot_list
;
714 s
->snapshots
[s
->nb_snapshots
++] = *sn
;
716 ret
= qcow2_write_snapshots(bs
);
718 g_free(s
->snapshots
);
719 s
->snapshots
= old_snapshot_list
;
724 g_free(old_snapshot_list
);
726 /* The VM state isn't needed any more in the active L1 table; in fact, it
727 * hurts by causing expensive COW for the next snapshot. */
728 qcow2_cluster_discard(bs
, qcow2_vm_state_offset(s
),
729 ROUND_UP(sn
->vm_state_size
, s
->cluster_size
),
730 QCOW2_DISCARD_NEVER
, false);
734 BdrvCheckResult result
= {0};
735 qcow2_check_refcounts(bs
, &result
, 0);
748 /* copy the snapshot 'snapshot_name' into the current disk image */
749 int qcow2_snapshot_goto(BlockDriverState
*bs
, const char *snapshot_id
)
751 BDRVQcow2State
*s
= bs
->opaque
;
753 Error
*local_err
= NULL
;
754 int i
, snapshot_index
;
755 int cur_l1_bytes
, sn_l1_bytes
;
757 uint64_t *sn_l1_table
= NULL
;
759 if (has_data_file(bs
)) {
763 /* Search the snapshot */
764 snapshot_index
= find_snapshot_by_id_or_name(bs
, snapshot_id
);
765 if (snapshot_index
< 0) {
768 sn
= &s
->snapshots
[snapshot_index
];
770 ret
= qcow2_validate_table(bs
, sn
->l1_table_offset
, sn
->l1_size
,
771 sizeof(uint64_t), QCOW_MAX_L1_SIZE
,
772 "Snapshot L1 table", &local_err
);
774 error_report_err(local_err
);
778 if (sn
->disk_size
!= bs
->total_sectors
* BDRV_SECTOR_SIZE
) {
779 BlockBackend
*blk
= blk_new_with_bs(bs
, BLK_PERM_RESIZE
, BLK_PERM_ALL
,
782 error_report_err(local_err
);
787 ret
= blk_truncate(blk
, sn
->disk_size
, true, PREALLOC_MODE_OFF
, 0,
791 error_report_err(local_err
);
797 * Make sure that the current L1 table is big enough to contain the whole
798 * L1 table of the snapshot. If the snapshot L1 table is smaller, the
799 * current one must be padded with zeros.
801 ret
= qcow2_grow_l1_table(bs
, sn
->l1_size
, true);
806 cur_l1_bytes
= s
->l1_size
* sizeof(uint64_t);
807 sn_l1_bytes
= sn
->l1_size
* sizeof(uint64_t);
810 * Copy the snapshot L1 table to the current L1 table.
812 * Before overwriting the old current L1 table on disk, make sure to
813 * increase all refcounts for the clusters referenced by the new one.
814 * Decrease the refcount referenced by the old one only when the L1
815 * table is overwritten.
817 sn_l1_table
= g_try_malloc0(cur_l1_bytes
);
818 if (cur_l1_bytes
&& sn_l1_table
== NULL
) {
823 ret
= bdrv_pread(bs
->file
, sn
->l1_table_offset
,
824 sn_l1_table
, sn_l1_bytes
);
829 ret
= qcow2_update_snapshot_refcount(bs
, sn
->l1_table_offset
,
835 ret
= qcow2_pre_write_overlap_check(bs
, QCOW2_OL_ACTIVE_L1
,
836 s
->l1_table_offset
, cur_l1_bytes
,
842 ret
= bdrv_pwrite_sync(bs
->file
, s
->l1_table_offset
, sn_l1_table
,
849 * Decrease refcount of clusters of current L1 table.
851 * At this point, the in-memory s->l1_table points to the old L1 table,
852 * whereas on disk we already have the new one.
854 * qcow2_update_snapshot_refcount special cases the current L1 table to use
855 * the in-memory data instead of really using the offset to load a new one,
856 * which is why this works.
858 ret
= qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
,
862 * Now update the in-memory L1 table to be in sync with the on-disk one. We
863 * need to do this even if updating refcounts failed.
865 for(i
= 0;i
< s
->l1_size
; i
++) {
866 s
->l1_table
[i
] = be64_to_cpu(sn_l1_table
[i
]);
877 * Update QCOW_OFLAG_COPIED in the active L1 table (it may have changed
878 * when we decreased the refcount of the old snapshot.
880 ret
= qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 0);
887 BdrvCheckResult result
= {0};
888 qcow2_check_refcounts(bs
, &result
, 0);
898 int qcow2_snapshot_delete(BlockDriverState
*bs
,
899 const char *snapshot_id
,
903 BDRVQcow2State
*s
= bs
->opaque
;
905 int snapshot_index
, ret
;
907 if (has_data_file(bs
)) {
911 /* Search the snapshot */
912 snapshot_index
= find_snapshot_by_id_and_name(bs
, snapshot_id
, name
);
913 if (snapshot_index
< 0) {
914 error_setg(errp
, "Can't find the snapshot");
917 sn
= s
->snapshots
[snapshot_index
];
919 ret
= qcow2_validate_table(bs
, sn
.l1_table_offset
, sn
.l1_size
,
920 sizeof(uint64_t), QCOW_MAX_L1_SIZE
,
921 "Snapshot L1 table", errp
);
926 /* Remove it from the snapshot list */
927 memmove(s
->snapshots
+ snapshot_index
,
928 s
->snapshots
+ snapshot_index
+ 1,
929 (s
->nb_snapshots
- snapshot_index
- 1) * sizeof(sn
));
931 ret
= qcow2_write_snapshots(bs
);
933 error_setg_errno(errp
, -ret
,
934 "Failed to remove snapshot from snapshot list");
939 * The snapshot is now unused, clean up. If we fail after this point, we
940 * won't recover but just leak clusters.
942 g_free(sn
.unknown_extra_data
);
947 * Now decrease the refcounts of clusters referenced by the snapshot and
950 ret
= qcow2_update_snapshot_refcount(bs
, sn
.l1_table_offset
,
953 error_setg_errno(errp
, -ret
, "Failed to free the cluster and L1 table");
956 qcow2_free_clusters(bs
, sn
.l1_table_offset
, sn
.l1_size
* sizeof(uint64_t),
957 QCOW2_DISCARD_SNAPSHOT
);
959 /* must update the copied flag on the current cluster offsets */
960 ret
= qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 0);
962 error_setg_errno(errp
, -ret
,
963 "Failed to update snapshot status in disk");
969 BdrvCheckResult result
= {0};
970 qcow2_check_refcounts(bs
, &result
, 0);
976 int qcow2_snapshot_list(BlockDriverState
*bs
, QEMUSnapshotInfo
**psn_tab
)
978 BDRVQcow2State
*s
= bs
->opaque
;
979 QEMUSnapshotInfo
*sn_tab
, *sn_info
;
983 if (has_data_file(bs
)) {
986 if (!s
->nb_snapshots
) {
988 return s
->nb_snapshots
;
991 sn_tab
= g_new0(QEMUSnapshotInfo
, s
->nb_snapshots
);
992 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
993 sn_info
= sn_tab
+ i
;
994 sn
= s
->snapshots
+ i
;
995 pstrcpy(sn_info
->id_str
, sizeof(sn_info
->id_str
),
997 pstrcpy(sn_info
->name
, sizeof(sn_info
->name
),
999 sn_info
->vm_state_size
= sn
->vm_state_size
;
1000 sn_info
->date_sec
= sn
->date_sec
;
1001 sn_info
->date_nsec
= sn
->date_nsec
;
1002 sn_info
->vm_clock_nsec
= sn
->vm_clock_nsec
;
1005 return s
->nb_snapshots
;
1008 int qcow2_snapshot_load_tmp(BlockDriverState
*bs
,
1009 const char *snapshot_id
,
1013 int i
, snapshot_index
;
1014 BDRVQcow2State
*s
= bs
->opaque
;
1016 uint64_t *new_l1_table
;
1020 assert(bs
->read_only
);
1022 /* Search the snapshot */
1023 snapshot_index
= find_snapshot_by_id_and_name(bs
, snapshot_id
, name
);
1024 if (snapshot_index
< 0) {
1026 "Can't find snapshot");
1029 sn
= &s
->snapshots
[snapshot_index
];
1031 /* Allocate and read in the snapshot's L1 table */
1032 ret
= qcow2_validate_table(bs
, sn
->l1_table_offset
, sn
->l1_size
,
1033 sizeof(uint64_t), QCOW_MAX_L1_SIZE
,
1034 "Snapshot L1 table", errp
);
1038 new_l1_bytes
= sn
->l1_size
* sizeof(uint64_t);
1039 new_l1_table
= qemu_try_blockalign(bs
->file
->bs
, new_l1_bytes
);
1040 if (new_l1_table
== NULL
) {
1044 ret
= bdrv_pread(bs
->file
, sn
->l1_table_offset
,
1045 new_l1_table
, new_l1_bytes
);
1047 error_setg(errp
, "Failed to read l1 table for snapshot");
1048 qemu_vfree(new_l1_table
);
1052 /* Switch the L1 table */
1053 qemu_vfree(s
->l1_table
);
1055 s
->l1_size
= sn
->l1_size
;
1056 s
->l1_table_offset
= sn
->l1_table_offset
;
1057 s
->l1_table
= new_l1_table
;
1059 for(i
= 0;i
< s
->l1_size
; i
++) {
1060 be64_to_cpus(&s
->l1_table
[i
]);