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 static void qcow2_free_single_snapshot(BlockDriverState
*bs
, int i
)
34 BDRVQcow2State
*s
= bs
->opaque
;
36 assert(i
>= 0 && i
< s
->nb_snapshots
);
37 g_free(s
->snapshots
[i
].name
);
38 g_free(s
->snapshots
[i
].id_str
);
39 g_free(s
->snapshots
[i
].unknown_extra_data
);
40 memset(&s
->snapshots
[i
], 0, sizeof(s
->snapshots
[i
]));
43 void qcow2_free_snapshots(BlockDriverState
*bs
)
45 BDRVQcow2State
*s
= bs
->opaque
;
48 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
49 qcow2_free_single_snapshot(bs
, i
);
57 * If @repair is true, try to repair a broken snapshot table instead
58 * of just returning an error:
60 * - If the snapshot table was too long, set *nb_clusters_reduced to
61 * the number of snapshots removed off the end.
62 * The caller will update the on-disk nb_snapshots accordingly;
63 * this leaks clusters, but is safe.
64 * (The on-disk information must be updated before
65 * qcow2_check_refcounts(), because that function relies on
66 * s->nb_snapshots to reflect the on-disk value.)
68 * - If there were snapshots with too much extra metadata, increment
69 * *extra_data_dropped for each.
70 * This requires the caller to eventually rewrite the whole snapshot
71 * table, which requires cluster allocation. Therefore, this should
72 * be done only after qcow2_check_refcounts() made sure the refcount
73 * structures are valid.
74 * (In the meantime, the image is still valid because
75 * qcow2_check_refcounts() does not do anything with snapshots'
78 static int qcow2_do_read_snapshots(BlockDriverState
*bs
, bool repair
,
79 int *nb_clusters_reduced
,
80 int *extra_data_dropped
,
83 BDRVQcow2State
*s
= bs
->opaque
;
85 QCowSnapshotExtraData extra
;
87 int i
, id_str_size
, name_size
;
88 int64_t offset
, pre_sn_offset
;
89 uint64_t table_length
= 0;
92 if (!s
->nb_snapshots
) {
94 s
->snapshots_size
= 0;
98 offset
= s
->snapshots_offset
;
99 s
->snapshots
= g_new0(QCowSnapshot
, s
->nb_snapshots
);
101 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
102 bool truncate_unknown_extra_data
= false;
104 pre_sn_offset
= offset
;
105 table_length
= ROUND_UP(table_length
, 8);
107 /* Read statically sized part of the snapshot header */
108 offset
= ROUND_UP(offset
, 8);
109 ret
= bdrv_pread(bs
->file
, offset
, &h
, sizeof(h
));
111 error_setg_errno(errp
, -ret
, "Failed to read snapshot table");
116 sn
= s
->snapshots
+ i
;
117 sn
->l1_table_offset
= be64_to_cpu(h
.l1_table_offset
);
118 sn
->l1_size
= be32_to_cpu(h
.l1_size
);
119 sn
->vm_state_size
= be32_to_cpu(h
.vm_state_size
);
120 sn
->date_sec
= be32_to_cpu(h
.date_sec
);
121 sn
->date_nsec
= be32_to_cpu(h
.date_nsec
);
122 sn
->vm_clock_nsec
= be64_to_cpu(h
.vm_clock_nsec
);
123 sn
->extra_data_size
= be32_to_cpu(h
.extra_data_size
);
125 id_str_size
= be16_to_cpu(h
.id_str_size
);
126 name_size
= be16_to_cpu(h
.name_size
);
128 if (sn
->extra_data_size
> QCOW_MAX_SNAPSHOT_EXTRA_DATA
) {
131 error_setg(errp
, "Too much extra metadata in snapshot table "
133 error_append_hint(errp
, "You can force-remove this extra "
134 "metadata with qemu-img check -r all\n");
138 fprintf(stderr
, "Discarding too much extra metadata in snapshot "
139 "table entry %i (%" PRIu32
" > %u)\n",
140 i
, sn
->extra_data_size
, QCOW_MAX_SNAPSHOT_EXTRA_DATA
);
142 (*extra_data_dropped
)++;
143 truncate_unknown_extra_data
= true;
146 /* Read known extra data */
147 ret
= bdrv_pread(bs
->file
, offset
, &extra
,
148 MIN(sizeof(extra
), sn
->extra_data_size
));
150 error_setg_errno(errp
, -ret
, "Failed to read snapshot table");
153 offset
+= MIN(sizeof(extra
), sn
->extra_data_size
);
155 if (sn
->extra_data_size
>= endof(QCowSnapshotExtraData
,
156 vm_state_size_large
)) {
157 sn
->vm_state_size
= be64_to_cpu(extra
.vm_state_size_large
);
160 if (sn
->extra_data_size
>= endof(QCowSnapshotExtraData
, disk_size
)) {
161 sn
->disk_size
= be64_to_cpu(extra
.disk_size
);
163 sn
->disk_size
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
166 if (sn
->extra_data_size
> sizeof(extra
)) {
167 uint64_t extra_data_end
;
168 size_t unknown_extra_data_size
;
170 extra_data_end
= offset
+ sn
->extra_data_size
- sizeof(extra
);
172 if (truncate_unknown_extra_data
) {
173 sn
->extra_data_size
= QCOW_MAX_SNAPSHOT_EXTRA_DATA
;
176 /* Store unknown extra data */
177 unknown_extra_data_size
= sn
->extra_data_size
- sizeof(extra
);
178 sn
->unknown_extra_data
= g_malloc(unknown_extra_data_size
);
179 ret
= bdrv_pread(bs
->file
, offset
, sn
->unknown_extra_data
,
180 unknown_extra_data_size
);
182 error_setg_errno(errp
, -ret
,
183 "Failed to read snapshot table");
186 offset
= extra_data_end
;
189 /* Read snapshot ID */
190 sn
->id_str
= g_malloc(id_str_size
+ 1);
191 ret
= bdrv_pread(bs
->file
, offset
, sn
->id_str
, id_str_size
);
193 error_setg_errno(errp
, -ret
, "Failed to read snapshot table");
196 offset
+= id_str_size
;
197 sn
->id_str
[id_str_size
] = '\0';
199 /* Read snapshot name */
200 sn
->name
= g_malloc(name_size
+ 1);
201 ret
= bdrv_pread(bs
->file
, offset
, sn
->name
, name_size
);
203 error_setg_errno(errp
, -ret
, "Failed to read snapshot table");
207 sn
->name
[name_size
] = '\0';
209 /* Note that the extra data may have been truncated */
210 table_length
+= sizeof(h
) + sn
->extra_data_size
+ id_str_size
+
213 assert(table_length
== offset
- s
->snapshots_offset
);
216 if (table_length
> QCOW_MAX_SNAPSHOTS_SIZE
||
217 offset
- s
->snapshots_offset
> INT_MAX
)
221 error_setg(errp
, "Snapshot table is too big");
222 error_append_hint(errp
, "You can force-remove all %u "
223 "overhanging snapshots with qemu-img check "
224 "-r all\n", s
->nb_snapshots
- i
);
228 fprintf(stderr
, "Discarding %u overhanging snapshots (snapshot "
229 "table is too big)\n", s
->nb_snapshots
- i
);
231 *nb_clusters_reduced
+= (s
->nb_snapshots
- i
);
233 /* Discard current snapshot also */
234 qcow2_free_single_snapshot(bs
, i
);
237 * This leaks all the rest of the snapshot table and the
238 * snapshots' clusters, but we run in check -r all mode,
239 * so qcow2_check_refcounts() will take care of it.
242 offset
= pre_sn_offset
;
247 assert(offset
- s
->snapshots_offset
<= INT_MAX
);
248 s
->snapshots_size
= offset
- s
->snapshots_offset
;
252 qcow2_free_snapshots(bs
);
256 int qcow2_read_snapshots(BlockDriverState
*bs
, Error
**errp
)
258 return qcow2_do_read_snapshots(bs
, false, NULL
, NULL
, errp
);
261 /* add at the end of the file a new list of snapshots */
262 int qcow2_write_snapshots(BlockDriverState
*bs
)
264 BDRVQcow2State
*s
= bs
->opaque
;
266 QCowSnapshotHeader h
;
267 QCowSnapshotExtraData extra
;
268 int i
, name_size
, id_str_size
, snapshots_size
;
270 uint32_t nb_snapshots
;
271 uint64_t snapshots_offset
;
272 } QEMU_PACKED header_data
;
273 int64_t offset
, snapshots_offset
= 0;
276 /* compute the size of the snapshots */
278 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
279 sn
= s
->snapshots
+ i
;
280 offset
= ROUND_UP(offset
, 8);
282 offset
+= MAX(sizeof(extra
), sn
->extra_data_size
);
283 offset
+= strlen(sn
->id_str
);
284 offset
+= strlen(sn
->name
);
286 if (offset
> QCOW_MAX_SNAPSHOTS_SIZE
) {
292 assert(offset
<= INT_MAX
);
293 snapshots_size
= offset
;
295 /* Allocate space for the new snapshot list */
296 snapshots_offset
= qcow2_alloc_clusters(bs
, snapshots_size
);
297 offset
= snapshots_offset
;
302 ret
= bdrv_flush(bs
);
307 /* The snapshot list position has not yet been updated, so these clusters
308 * must indeed be completely free */
309 ret
= qcow2_pre_write_overlap_check(bs
, 0, offset
, snapshots_size
, false);
315 /* Write all snapshots to the new list */
316 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
317 sn
= s
->snapshots
+ i
;
318 memset(&h
, 0, sizeof(h
));
319 h
.l1_table_offset
= cpu_to_be64(sn
->l1_table_offset
);
320 h
.l1_size
= cpu_to_be32(sn
->l1_size
);
321 /* If it doesn't fit in 32 bit, older implementations should treat it
322 * as a disk-only snapshot rather than truncate the VM state */
323 if (sn
->vm_state_size
<= 0xffffffff) {
324 h
.vm_state_size
= cpu_to_be32(sn
->vm_state_size
);
326 h
.date_sec
= cpu_to_be32(sn
->date_sec
);
327 h
.date_nsec
= cpu_to_be32(sn
->date_nsec
);
328 h
.vm_clock_nsec
= cpu_to_be64(sn
->vm_clock_nsec
);
329 h
.extra_data_size
= cpu_to_be32(MAX(sizeof(extra
),
330 sn
->extra_data_size
));
332 memset(&extra
, 0, sizeof(extra
));
333 extra
.vm_state_size_large
= cpu_to_be64(sn
->vm_state_size
);
334 extra
.disk_size
= cpu_to_be64(sn
->disk_size
);
336 id_str_size
= strlen(sn
->id_str
);
337 name_size
= strlen(sn
->name
);
338 assert(id_str_size
<= UINT16_MAX
&& name_size
<= UINT16_MAX
);
339 h
.id_str_size
= cpu_to_be16(id_str_size
);
340 h
.name_size
= cpu_to_be16(name_size
);
341 offset
= ROUND_UP(offset
, 8);
343 ret
= bdrv_pwrite(bs
->file
, offset
, &h
, sizeof(h
));
349 ret
= bdrv_pwrite(bs
->file
, offset
, &extra
, sizeof(extra
));
353 offset
+= sizeof(extra
);
355 if (sn
->extra_data_size
> sizeof(extra
)) {
356 size_t unknown_extra_data_size
=
357 sn
->extra_data_size
- sizeof(extra
);
359 /* qcow2_read_snapshots() ensures no unbounded allocation */
360 assert(unknown_extra_data_size
<= BDRV_REQUEST_MAX_BYTES
);
361 assert(sn
->unknown_extra_data
);
363 ret
= bdrv_pwrite(bs
->file
, offset
, sn
->unknown_extra_data
,
364 unknown_extra_data_size
);
368 offset
+= unknown_extra_data_size
;
371 ret
= bdrv_pwrite(bs
->file
, offset
, sn
->id_str
, id_str_size
);
375 offset
+= id_str_size
;
377 ret
= bdrv_pwrite(bs
->file
, offset
, sn
->name
, name_size
);
385 * Update the header to point to the new snapshot table. This requires the
386 * new table and its refcounts to be stable on disk.
388 ret
= bdrv_flush(bs
);
393 QEMU_BUILD_BUG_ON(offsetof(QCowHeader
, snapshots_offset
) !=
394 endof(QCowHeader
, nb_snapshots
));
396 header_data
.nb_snapshots
= cpu_to_be32(s
->nb_snapshots
);
397 header_data
.snapshots_offset
= cpu_to_be64(snapshots_offset
);
399 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, nb_snapshots
),
400 &header_data
, sizeof(header_data
));
405 /* free the old snapshot table */
406 qcow2_free_clusters(bs
, s
->snapshots_offset
, s
->snapshots_size
,
407 QCOW2_DISCARD_SNAPSHOT
);
408 s
->snapshots_offset
= snapshots_offset
;
409 s
->snapshots_size
= snapshots_size
;
413 if (snapshots_offset
> 0) {
414 qcow2_free_clusters(bs
, snapshots_offset
, snapshots_size
,
415 QCOW2_DISCARD_ALWAYS
);
420 int coroutine_fn
qcow2_check_read_snapshot_table(BlockDriverState
*bs
,
421 BdrvCheckResult
*result
,
424 BDRVQcow2State
*s
= bs
->opaque
;
425 Error
*local_err
= NULL
;
426 int nb_clusters_reduced
= 0;
427 int extra_data_dropped
= 0;
430 uint32_t nb_snapshots
;
431 uint64_t snapshots_offset
;
432 } QEMU_PACKED snapshot_table_pointer
;
434 /* qcow2_do_open() discards this information in check mode */
435 ret
= bdrv_pread(bs
->file
, offsetof(QCowHeader
, nb_snapshots
),
436 &snapshot_table_pointer
, sizeof(snapshot_table_pointer
));
438 result
->check_errors
++;
439 fprintf(stderr
, "ERROR failed to read the snapshot table pointer from "
440 "the image header: %s\n", strerror(-ret
));
444 s
->snapshots_offset
= be64_to_cpu(snapshot_table_pointer
.snapshots_offset
);
445 s
->nb_snapshots
= be32_to_cpu(snapshot_table_pointer
.nb_snapshots
);
447 ret
= qcow2_validate_table(bs
, s
->snapshots_offset
, s
->nb_snapshots
,
448 sizeof(QCowSnapshotHeader
),
449 sizeof(QCowSnapshotHeader
) * QCOW_MAX_SNAPSHOTS
,
450 "snapshot table", &local_err
);
452 result
->check_errors
++;
453 error_reportf_err(local_err
, "ERROR ");
455 /* We did not read the snapshot table, so invalidate this information */
456 s
->snapshots_offset
= 0;
462 qemu_co_mutex_unlock(&s
->lock
);
463 ret
= qcow2_do_read_snapshots(bs
, fix
& BDRV_FIX_ERRORS
,
464 &nb_clusters_reduced
, &extra_data_dropped
,
466 qemu_co_mutex_lock(&s
->lock
);
468 result
->check_errors
++;
469 error_reportf_err(local_err
,
470 "ERROR failed to read the snapshot table: ");
472 /* We did not read the snapshot table, so invalidate this information */
473 s
->snapshots_offset
= 0;
478 result
->corruptions
+= nb_clusters_reduced
+ extra_data_dropped
;
480 if (nb_clusters_reduced
) {
482 * Update image header now, because:
483 * (1) qcow2_check_refcounts() relies on s->nb_snapshots to be
484 * the same as what the image header says,
485 * (2) this leaks clusters, but qcow2_check_refcounts() will
488 assert(fix
& BDRV_FIX_ERRORS
);
490 snapshot_table_pointer
.nb_snapshots
= cpu_to_be32(s
->nb_snapshots
);
491 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, nb_snapshots
),
492 &snapshot_table_pointer
.nb_snapshots
,
493 sizeof(snapshot_table_pointer
.nb_snapshots
));
495 result
->check_errors
++;
496 fprintf(stderr
, "ERROR failed to update the snapshot count in the "
497 "image header: %s\n", strerror(-ret
));
501 result
->corruptions_fixed
+= nb_clusters_reduced
;
502 result
->corruptions
-= nb_clusters_reduced
;
508 int coroutine_fn
qcow2_check_fix_snapshot_table(BlockDriverState
*bs
,
509 BdrvCheckResult
*result
,
512 BDRVQcow2State
*s
= bs
->opaque
;
515 if (result
->corruptions
&& (fix
& BDRV_FIX_ERRORS
)) {
516 qemu_co_mutex_unlock(&s
->lock
);
517 ret
= qcow2_write_snapshots(bs
);
518 qemu_co_mutex_lock(&s
->lock
);
520 result
->check_errors
++;
521 fprintf(stderr
, "ERROR failed to update snapshot table: %s\n",
526 result
->corruptions_fixed
+= result
->corruptions
;
527 result
->corruptions
= 0;
533 static void find_new_snapshot_id(BlockDriverState
*bs
,
534 char *id_str
, int id_str_size
)
536 BDRVQcow2State
*s
= bs
->opaque
;
539 unsigned long id
, id_max
= 0;
541 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
542 sn
= s
->snapshots
+ i
;
543 id
= strtoul(sn
->id_str
, NULL
, 10);
547 snprintf(id_str
, id_str_size
, "%lu", id_max
+ 1);
550 static int find_snapshot_by_id_and_name(BlockDriverState
*bs
,
554 BDRVQcow2State
*s
= bs
->opaque
;
558 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
559 if (!strcmp(s
->snapshots
[i
].id_str
, id
) &&
560 !strcmp(s
->snapshots
[i
].name
, name
)) {
565 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
566 if (!strcmp(s
->snapshots
[i
].id_str
, id
)) {
571 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
572 if (!strcmp(s
->snapshots
[i
].name
, name
)) {
581 static int find_snapshot_by_id_or_name(BlockDriverState
*bs
,
582 const char *id_or_name
)
586 ret
= find_snapshot_by_id_and_name(bs
, id_or_name
, NULL
);
590 return find_snapshot_by_id_and_name(bs
, NULL
, id_or_name
);
593 /* if no id is provided, a new one is constructed */
594 int qcow2_snapshot_create(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
)
596 BDRVQcow2State
*s
= bs
->opaque
;
597 QCowSnapshot
*new_snapshot_list
= NULL
;
598 QCowSnapshot
*old_snapshot_list
= NULL
;
599 QCowSnapshot sn1
, *sn
= &sn1
;
601 uint64_t *l1_table
= NULL
;
602 int64_t l1_table_offset
;
604 if (s
->nb_snapshots
>= QCOW_MAX_SNAPSHOTS
) {
608 if (has_data_file(bs
)) {
612 memset(sn
, 0, sizeof(*sn
));
615 find_new_snapshot_id(bs
, sn_info
->id_str
, sizeof(sn_info
->id_str
));
617 /* Populate sn with passed data */
618 sn
->id_str
= g_strdup(sn_info
->id_str
);
619 sn
->name
= g_strdup(sn_info
->name
);
621 sn
->disk_size
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
622 sn
->vm_state_size
= sn_info
->vm_state_size
;
623 sn
->date_sec
= sn_info
->date_sec
;
624 sn
->date_nsec
= sn_info
->date_nsec
;
625 sn
->vm_clock_nsec
= sn_info
->vm_clock_nsec
;
626 sn
->extra_data_size
= sizeof(QCowSnapshotExtraData
);
628 /* Allocate the L1 table of the snapshot and copy the current one there. */
629 l1_table_offset
= qcow2_alloc_clusters(bs
, s
->l1_size
* sizeof(uint64_t));
630 if (l1_table_offset
< 0) {
631 ret
= l1_table_offset
;
635 sn
->l1_table_offset
= l1_table_offset
;
636 sn
->l1_size
= s
->l1_size
;
638 l1_table
= g_try_new(uint64_t, s
->l1_size
);
639 if (s
->l1_size
&& l1_table
== NULL
) {
644 for(i
= 0; i
< s
->l1_size
; i
++) {
645 l1_table
[i
] = cpu_to_be64(s
->l1_table
[i
]);
648 ret
= qcow2_pre_write_overlap_check(bs
, 0, sn
->l1_table_offset
,
649 s
->l1_size
* sizeof(uint64_t), false);
654 ret
= bdrv_pwrite(bs
->file
, sn
->l1_table_offset
, l1_table
,
655 s
->l1_size
* sizeof(uint64_t));
664 * Increase the refcounts of all clusters and make sure everything is
665 * stable on disk before updating the snapshot table to contain a pointer
666 * to the new L1 table.
668 ret
= qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 1);
673 /* Append the new snapshot to the snapshot list */
674 new_snapshot_list
= g_new(QCowSnapshot
, s
->nb_snapshots
+ 1);
676 memcpy(new_snapshot_list
, s
->snapshots
,
677 s
->nb_snapshots
* sizeof(QCowSnapshot
));
678 old_snapshot_list
= s
->snapshots
;
680 s
->snapshots
= new_snapshot_list
;
681 s
->snapshots
[s
->nb_snapshots
++] = *sn
;
683 ret
= qcow2_write_snapshots(bs
);
685 g_free(s
->snapshots
);
686 s
->snapshots
= old_snapshot_list
;
691 g_free(old_snapshot_list
);
693 /* The VM state isn't needed any more in the active L1 table; in fact, it
694 * hurts by causing expensive COW for the next snapshot. */
695 qcow2_cluster_discard(bs
, qcow2_vm_state_offset(s
),
696 ROUND_UP(sn
->vm_state_size
, s
->cluster_size
),
697 QCOW2_DISCARD_NEVER
, false);
701 BdrvCheckResult result
= {0};
702 qcow2_check_refcounts(bs
, &result
, 0);
715 /* copy the snapshot 'snapshot_name' into the current disk image */
716 int qcow2_snapshot_goto(BlockDriverState
*bs
, const char *snapshot_id
)
718 BDRVQcow2State
*s
= bs
->opaque
;
720 Error
*local_err
= NULL
;
721 int i
, snapshot_index
;
722 int cur_l1_bytes
, sn_l1_bytes
;
724 uint64_t *sn_l1_table
= NULL
;
726 if (has_data_file(bs
)) {
730 /* Search the snapshot */
731 snapshot_index
= find_snapshot_by_id_or_name(bs
, snapshot_id
);
732 if (snapshot_index
< 0) {
735 sn
= &s
->snapshots
[snapshot_index
];
737 ret
= qcow2_validate_table(bs
, sn
->l1_table_offset
, sn
->l1_size
,
738 sizeof(uint64_t), QCOW_MAX_L1_SIZE
,
739 "Snapshot L1 table", &local_err
);
741 error_report_err(local_err
);
745 if (sn
->disk_size
!= bs
->total_sectors
* BDRV_SECTOR_SIZE
) {
746 error_report("qcow2: Loading snapshots with different disk "
747 "size is not implemented");
753 * Make sure that the current L1 table is big enough to contain the whole
754 * L1 table of the snapshot. If the snapshot L1 table is smaller, the
755 * current one must be padded with zeros.
757 ret
= qcow2_grow_l1_table(bs
, sn
->l1_size
, true);
762 cur_l1_bytes
= s
->l1_size
* sizeof(uint64_t);
763 sn_l1_bytes
= sn
->l1_size
* sizeof(uint64_t);
766 * Copy the snapshot L1 table to the current L1 table.
768 * Before overwriting the old current L1 table on disk, make sure to
769 * increase all refcounts for the clusters referenced by the new one.
770 * Decrease the refcount referenced by the old one only when the L1
771 * table is overwritten.
773 sn_l1_table
= g_try_malloc0(cur_l1_bytes
);
774 if (cur_l1_bytes
&& sn_l1_table
== NULL
) {
779 ret
= bdrv_pread(bs
->file
, sn
->l1_table_offset
,
780 sn_l1_table
, sn_l1_bytes
);
785 ret
= qcow2_update_snapshot_refcount(bs
, sn
->l1_table_offset
,
791 ret
= qcow2_pre_write_overlap_check(bs
, QCOW2_OL_ACTIVE_L1
,
792 s
->l1_table_offset
, cur_l1_bytes
,
798 ret
= bdrv_pwrite_sync(bs
->file
, s
->l1_table_offset
, sn_l1_table
,
805 * Decrease refcount of clusters of current L1 table.
807 * At this point, the in-memory s->l1_table points to the old L1 table,
808 * whereas on disk we already have the new one.
810 * qcow2_update_snapshot_refcount special cases the current L1 table to use
811 * the in-memory data instead of really using the offset to load a new one,
812 * which is why this works.
814 ret
= qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
,
818 * Now update the in-memory L1 table to be in sync with the on-disk one. We
819 * need to do this even if updating refcounts failed.
821 for(i
= 0;i
< s
->l1_size
; i
++) {
822 s
->l1_table
[i
] = be64_to_cpu(sn_l1_table
[i
]);
833 * Update QCOW_OFLAG_COPIED in the active L1 table (it may have changed
834 * when we decreased the refcount of the old snapshot.
836 ret
= qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 0);
843 BdrvCheckResult result
= {0};
844 qcow2_check_refcounts(bs
, &result
, 0);
854 int qcow2_snapshot_delete(BlockDriverState
*bs
,
855 const char *snapshot_id
,
859 BDRVQcow2State
*s
= bs
->opaque
;
861 int snapshot_index
, ret
;
863 if (has_data_file(bs
)) {
867 /* Search the snapshot */
868 snapshot_index
= find_snapshot_by_id_and_name(bs
, snapshot_id
, name
);
869 if (snapshot_index
< 0) {
870 error_setg(errp
, "Can't find the snapshot");
873 sn
= s
->snapshots
[snapshot_index
];
875 ret
= qcow2_validate_table(bs
, sn
.l1_table_offset
, sn
.l1_size
,
876 sizeof(uint64_t), QCOW_MAX_L1_SIZE
,
877 "Snapshot L1 table", errp
);
882 /* Remove it from the snapshot list */
883 memmove(s
->snapshots
+ snapshot_index
,
884 s
->snapshots
+ snapshot_index
+ 1,
885 (s
->nb_snapshots
- snapshot_index
- 1) * sizeof(sn
));
887 ret
= qcow2_write_snapshots(bs
);
889 error_setg_errno(errp
, -ret
,
890 "Failed to remove snapshot from snapshot list");
895 * The snapshot is now unused, clean up. If we fail after this point, we
896 * won't recover but just leak clusters.
898 g_free(sn
.unknown_extra_data
);
903 * Now decrease the refcounts of clusters referenced by the snapshot and
906 ret
= qcow2_update_snapshot_refcount(bs
, sn
.l1_table_offset
,
909 error_setg_errno(errp
, -ret
, "Failed to free the cluster and L1 table");
912 qcow2_free_clusters(bs
, sn
.l1_table_offset
, sn
.l1_size
* sizeof(uint64_t),
913 QCOW2_DISCARD_SNAPSHOT
);
915 /* must update the copied flag on the current cluster offsets */
916 ret
= qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 0);
918 error_setg_errno(errp
, -ret
,
919 "Failed to update snapshot status in disk");
925 BdrvCheckResult result
= {0};
926 qcow2_check_refcounts(bs
, &result
, 0);
932 int qcow2_snapshot_list(BlockDriverState
*bs
, QEMUSnapshotInfo
**psn_tab
)
934 BDRVQcow2State
*s
= bs
->opaque
;
935 QEMUSnapshotInfo
*sn_tab
, *sn_info
;
939 if (has_data_file(bs
)) {
942 if (!s
->nb_snapshots
) {
944 return s
->nb_snapshots
;
947 sn_tab
= g_new0(QEMUSnapshotInfo
, s
->nb_snapshots
);
948 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
949 sn_info
= sn_tab
+ i
;
950 sn
= s
->snapshots
+ i
;
951 pstrcpy(sn_info
->id_str
, sizeof(sn_info
->id_str
),
953 pstrcpy(sn_info
->name
, sizeof(sn_info
->name
),
955 sn_info
->vm_state_size
= sn
->vm_state_size
;
956 sn_info
->date_sec
= sn
->date_sec
;
957 sn_info
->date_nsec
= sn
->date_nsec
;
958 sn_info
->vm_clock_nsec
= sn
->vm_clock_nsec
;
961 return s
->nb_snapshots
;
964 int qcow2_snapshot_load_tmp(BlockDriverState
*bs
,
965 const char *snapshot_id
,
969 int i
, snapshot_index
;
970 BDRVQcow2State
*s
= bs
->opaque
;
972 uint64_t *new_l1_table
;
976 assert(bs
->read_only
);
978 /* Search the snapshot */
979 snapshot_index
= find_snapshot_by_id_and_name(bs
, snapshot_id
, name
);
980 if (snapshot_index
< 0) {
982 "Can't find snapshot");
985 sn
= &s
->snapshots
[snapshot_index
];
987 /* Allocate and read in the snapshot's L1 table */
988 ret
= qcow2_validate_table(bs
, sn
->l1_table_offset
, sn
->l1_size
,
989 sizeof(uint64_t), QCOW_MAX_L1_SIZE
,
990 "Snapshot L1 table", errp
);
994 new_l1_bytes
= sn
->l1_size
* sizeof(uint64_t);
995 new_l1_table
= qemu_try_blockalign(bs
->file
->bs
,
996 ROUND_UP(new_l1_bytes
, 512));
997 if (new_l1_table
== NULL
) {
1001 ret
= bdrv_pread(bs
->file
, sn
->l1_table_offset
,
1002 new_l1_table
, new_l1_bytes
);
1004 error_setg(errp
, "Failed to read l1 table for snapshot");
1005 qemu_vfree(new_l1_table
);
1009 /* Switch the L1 table */
1010 qemu_vfree(s
->l1_table
);
1012 s
->l1_size
= sn
->l1_size
;
1013 s
->l1_table_offset
= sn
->l1_table_offset
;
1014 s
->l1_table
= new_l1_table
;
1016 for(i
= 0;i
< s
->l1_size
; i
++) {
1017 be64_to_cpus(&s
->l1_table
[i
]);