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 "qemu-common.h"
27 #include "block/block_int.h"
28 #include "block/qcow2.h"
29 #include "qemu/error-report.h"
31 void qcow2_free_snapshots(BlockDriverState
*bs
)
33 BDRVQcow2State
*s
= bs
->opaque
;
36 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
37 g_free(s
->snapshots
[i
].name
);
38 g_free(s
->snapshots
[i
].id_str
);
45 int qcow2_read_snapshots(BlockDriverState
*bs
)
47 BDRVQcow2State
*s
= bs
->opaque
;
49 QCowSnapshotExtraData extra
;
51 int i
, id_str_size
, name_size
;
53 uint32_t extra_data_size
;
56 if (!s
->nb_snapshots
) {
58 s
->snapshots_size
= 0;
62 offset
= s
->snapshots_offset
;
63 s
->snapshots
= g_new0(QCowSnapshot
, s
->nb_snapshots
);
65 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
66 /* Read statically sized part of the snapshot header */
67 offset
= align_offset(offset
, 8);
68 ret
= bdrv_pread(bs
->file
->bs
, offset
, &h
, sizeof(h
));
74 sn
= s
->snapshots
+ i
;
75 sn
->l1_table_offset
= be64_to_cpu(h
.l1_table_offset
);
76 sn
->l1_size
= be32_to_cpu(h
.l1_size
);
77 sn
->vm_state_size
= be32_to_cpu(h
.vm_state_size
);
78 sn
->date_sec
= be32_to_cpu(h
.date_sec
);
79 sn
->date_nsec
= be32_to_cpu(h
.date_nsec
);
80 sn
->vm_clock_nsec
= be64_to_cpu(h
.vm_clock_nsec
);
81 extra_data_size
= be32_to_cpu(h
.extra_data_size
);
83 id_str_size
= be16_to_cpu(h
.id_str_size
);
84 name_size
= be16_to_cpu(h
.name_size
);
87 ret
= bdrv_pread(bs
->file
->bs
, offset
, &extra
,
88 MIN(sizeof(extra
), extra_data_size
));
92 offset
+= extra_data_size
;
94 if (extra_data_size
>= 8) {
95 sn
->vm_state_size
= be64_to_cpu(extra
.vm_state_size_large
);
98 if (extra_data_size
>= 16) {
99 sn
->disk_size
= be64_to_cpu(extra
.disk_size
);
101 sn
->disk_size
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
104 /* Read snapshot ID */
105 sn
->id_str
= g_malloc(id_str_size
+ 1);
106 ret
= bdrv_pread(bs
->file
->bs
, offset
, sn
->id_str
, id_str_size
);
110 offset
+= id_str_size
;
111 sn
->id_str
[id_str_size
] = '\0';
113 /* Read snapshot name */
114 sn
->name
= g_malloc(name_size
+ 1);
115 ret
= bdrv_pread(bs
->file
->bs
, offset
, sn
->name
, name_size
);
120 sn
->name
[name_size
] = '\0';
122 if (offset
- s
->snapshots_offset
> QCOW_MAX_SNAPSHOTS_SIZE
) {
128 assert(offset
- s
->snapshots_offset
<= INT_MAX
);
129 s
->snapshots_size
= offset
- s
->snapshots_offset
;
133 qcow2_free_snapshots(bs
);
137 /* add at the end of the file a new list of snapshots */
138 static int qcow2_write_snapshots(BlockDriverState
*bs
)
140 BDRVQcow2State
*s
= bs
->opaque
;
142 QCowSnapshotHeader h
;
143 QCowSnapshotExtraData extra
;
144 int i
, name_size
, id_str_size
, snapshots_size
;
146 uint32_t nb_snapshots
;
147 uint64_t snapshots_offset
;
148 } QEMU_PACKED header_data
;
149 int64_t offset
, snapshots_offset
= 0;
152 /* compute the size of the snapshots */
154 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
155 sn
= s
->snapshots
+ i
;
156 offset
= align_offset(offset
, 8);
158 offset
+= sizeof(extra
);
159 offset
+= strlen(sn
->id_str
);
160 offset
+= strlen(sn
->name
);
162 if (offset
> QCOW_MAX_SNAPSHOTS_SIZE
) {
168 assert(offset
<= INT_MAX
);
169 snapshots_size
= offset
;
171 /* Allocate space for the new snapshot list */
172 snapshots_offset
= qcow2_alloc_clusters(bs
, snapshots_size
);
173 offset
= snapshots_offset
;
178 ret
= bdrv_flush(bs
);
183 /* The snapshot list position has not yet been updated, so these clusters
184 * must indeed be completely free */
185 ret
= qcow2_pre_write_overlap_check(bs
, 0, offset
, snapshots_size
);
191 /* Write all snapshots to the new list */
192 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
193 sn
= s
->snapshots
+ i
;
194 memset(&h
, 0, sizeof(h
));
195 h
.l1_table_offset
= cpu_to_be64(sn
->l1_table_offset
);
196 h
.l1_size
= cpu_to_be32(sn
->l1_size
);
197 /* If it doesn't fit in 32 bit, older implementations should treat it
198 * as a disk-only snapshot rather than truncate the VM state */
199 if (sn
->vm_state_size
<= 0xffffffff) {
200 h
.vm_state_size
= cpu_to_be32(sn
->vm_state_size
);
202 h
.date_sec
= cpu_to_be32(sn
->date_sec
);
203 h
.date_nsec
= cpu_to_be32(sn
->date_nsec
);
204 h
.vm_clock_nsec
= cpu_to_be64(sn
->vm_clock_nsec
);
205 h
.extra_data_size
= cpu_to_be32(sizeof(extra
));
207 memset(&extra
, 0, sizeof(extra
));
208 extra
.vm_state_size_large
= cpu_to_be64(sn
->vm_state_size
);
209 extra
.disk_size
= cpu_to_be64(sn
->disk_size
);
211 id_str_size
= strlen(sn
->id_str
);
212 name_size
= strlen(sn
->name
);
213 assert(id_str_size
<= UINT16_MAX
&& name_size
<= UINT16_MAX
);
214 h
.id_str_size
= cpu_to_be16(id_str_size
);
215 h
.name_size
= cpu_to_be16(name_size
);
216 offset
= align_offset(offset
, 8);
218 ret
= bdrv_pwrite(bs
->file
->bs
, offset
, &h
, sizeof(h
));
224 ret
= bdrv_pwrite(bs
->file
->bs
, offset
, &extra
, sizeof(extra
));
228 offset
+= sizeof(extra
);
230 ret
= bdrv_pwrite(bs
->file
->bs
, offset
, sn
->id_str
, id_str_size
);
234 offset
+= id_str_size
;
236 ret
= bdrv_pwrite(bs
->file
->bs
, offset
, sn
->name
, name_size
);
244 * Update the header to point to the new snapshot table. This requires the
245 * new table and its refcounts to be stable on disk.
247 ret
= bdrv_flush(bs
);
252 QEMU_BUILD_BUG_ON(offsetof(QCowHeader
, snapshots_offset
) !=
253 offsetof(QCowHeader
, nb_snapshots
) + sizeof(header_data
.nb_snapshots
));
255 header_data
.nb_snapshots
= cpu_to_be32(s
->nb_snapshots
);
256 header_data
.snapshots_offset
= cpu_to_be64(snapshots_offset
);
258 ret
= bdrv_pwrite_sync(bs
->file
->bs
, offsetof(QCowHeader
, nb_snapshots
),
259 &header_data
, sizeof(header_data
));
264 /* free the old snapshot table */
265 qcow2_free_clusters(bs
, s
->snapshots_offset
, s
->snapshots_size
,
266 QCOW2_DISCARD_SNAPSHOT
);
267 s
->snapshots_offset
= snapshots_offset
;
268 s
->snapshots_size
= snapshots_size
;
272 if (snapshots_offset
> 0) {
273 qcow2_free_clusters(bs
, snapshots_offset
, snapshots_size
,
274 QCOW2_DISCARD_ALWAYS
);
279 static void find_new_snapshot_id(BlockDriverState
*bs
,
280 char *id_str
, int id_str_size
)
282 BDRVQcow2State
*s
= bs
->opaque
;
285 unsigned long id
, id_max
= 0;
287 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
288 sn
= s
->snapshots
+ i
;
289 id
= strtoul(sn
->id_str
, NULL
, 10);
293 snprintf(id_str
, id_str_size
, "%lu", id_max
+ 1);
296 static int find_snapshot_by_id_and_name(BlockDriverState
*bs
,
300 BDRVQcow2State
*s
= bs
->opaque
;
304 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
305 if (!strcmp(s
->snapshots
[i
].id_str
, id
) &&
306 !strcmp(s
->snapshots
[i
].name
, name
)) {
311 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
312 if (!strcmp(s
->snapshots
[i
].id_str
, id
)) {
317 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
318 if (!strcmp(s
->snapshots
[i
].name
, name
)) {
327 static int find_snapshot_by_id_or_name(BlockDriverState
*bs
,
328 const char *id_or_name
)
332 ret
= find_snapshot_by_id_and_name(bs
, id_or_name
, NULL
);
336 return find_snapshot_by_id_and_name(bs
, NULL
, id_or_name
);
339 /* if no id is provided, a new one is constructed */
340 int qcow2_snapshot_create(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
)
342 BDRVQcow2State
*s
= bs
->opaque
;
343 QCowSnapshot
*new_snapshot_list
= NULL
;
344 QCowSnapshot
*old_snapshot_list
= NULL
;
345 QCowSnapshot sn1
, *sn
= &sn1
;
347 uint64_t *l1_table
= NULL
;
348 int64_t l1_table_offset
;
350 if (s
->nb_snapshots
>= QCOW_MAX_SNAPSHOTS
) {
354 memset(sn
, 0, sizeof(*sn
));
357 find_new_snapshot_id(bs
, sn_info
->id_str
, sizeof(sn_info
->id_str
));
359 /* Check that the ID is unique */
360 if (find_snapshot_by_id_and_name(bs
, sn_info
->id_str
, NULL
) >= 0) {
364 /* Populate sn with passed data */
365 sn
->id_str
= g_strdup(sn_info
->id_str
);
366 sn
->name
= g_strdup(sn_info
->name
);
368 sn
->disk_size
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
369 sn
->vm_state_size
= sn_info
->vm_state_size
;
370 sn
->date_sec
= sn_info
->date_sec
;
371 sn
->date_nsec
= sn_info
->date_nsec
;
372 sn
->vm_clock_nsec
= sn_info
->vm_clock_nsec
;
374 /* Allocate the L1 table of the snapshot and copy the current one there. */
375 l1_table_offset
= qcow2_alloc_clusters(bs
, s
->l1_size
* sizeof(uint64_t));
376 if (l1_table_offset
< 0) {
377 ret
= l1_table_offset
;
381 sn
->l1_table_offset
= l1_table_offset
;
382 sn
->l1_size
= s
->l1_size
;
384 l1_table
= g_try_new(uint64_t, s
->l1_size
);
385 if (s
->l1_size
&& l1_table
== NULL
) {
390 for(i
= 0; i
< s
->l1_size
; i
++) {
391 l1_table
[i
] = cpu_to_be64(s
->l1_table
[i
]);
394 ret
= qcow2_pre_write_overlap_check(bs
, 0, sn
->l1_table_offset
,
395 s
->l1_size
* sizeof(uint64_t));
400 ret
= bdrv_pwrite(bs
->file
->bs
, sn
->l1_table_offset
, l1_table
,
401 s
->l1_size
* sizeof(uint64_t));
410 * Increase the refcounts of all clusters and make sure everything is
411 * stable on disk before updating the snapshot table to contain a pointer
412 * to the new L1 table.
414 ret
= qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 1);
419 /* Append the new snapshot to the snapshot list */
420 new_snapshot_list
= g_new(QCowSnapshot
, s
->nb_snapshots
+ 1);
422 memcpy(new_snapshot_list
, s
->snapshots
,
423 s
->nb_snapshots
* sizeof(QCowSnapshot
));
424 old_snapshot_list
= s
->snapshots
;
426 s
->snapshots
= new_snapshot_list
;
427 s
->snapshots
[s
->nb_snapshots
++] = *sn
;
429 ret
= qcow2_write_snapshots(bs
);
431 g_free(s
->snapshots
);
432 s
->snapshots
= old_snapshot_list
;
437 g_free(old_snapshot_list
);
439 /* The VM state isn't needed any more in the active L1 table; in fact, it
440 * hurts by causing expensive COW for the next snapshot. */
441 qcow2_discard_clusters(bs
, qcow2_vm_state_offset(s
),
442 align_offset(sn
->vm_state_size
, s
->cluster_size
)
444 QCOW2_DISCARD_NEVER
, false);
448 BdrvCheckResult result
= {0};
449 qcow2_check_refcounts(bs
, &result
, 0);
462 /* copy the snapshot 'snapshot_name' into the current disk image */
463 int qcow2_snapshot_goto(BlockDriverState
*bs
, const char *snapshot_id
)
465 BDRVQcow2State
*s
= bs
->opaque
;
467 int i
, snapshot_index
;
468 int cur_l1_bytes
, sn_l1_bytes
;
470 uint64_t *sn_l1_table
= NULL
;
472 /* Search the snapshot */
473 snapshot_index
= find_snapshot_by_id_or_name(bs
, snapshot_id
);
474 if (snapshot_index
< 0) {
477 sn
= &s
->snapshots
[snapshot_index
];
479 if (sn
->disk_size
!= bs
->total_sectors
* BDRV_SECTOR_SIZE
) {
480 error_report("qcow2: Loading snapshots with different disk "
481 "size is not implemented");
487 * Make sure that the current L1 table is big enough to contain the whole
488 * L1 table of the snapshot. If the snapshot L1 table is smaller, the
489 * current one must be padded with zeros.
491 ret
= qcow2_grow_l1_table(bs
, sn
->l1_size
, true);
496 cur_l1_bytes
= s
->l1_size
* sizeof(uint64_t);
497 sn_l1_bytes
= sn
->l1_size
* sizeof(uint64_t);
500 * Copy the snapshot L1 table to the current L1 table.
502 * Before overwriting the old current L1 table on disk, make sure to
503 * increase all refcounts for the clusters referenced by the new one.
504 * Decrease the refcount referenced by the old one only when the L1
505 * table is overwritten.
507 sn_l1_table
= g_try_malloc0(cur_l1_bytes
);
508 if (cur_l1_bytes
&& sn_l1_table
== NULL
) {
513 ret
= bdrv_pread(bs
->file
->bs
, sn
->l1_table_offset
,
514 sn_l1_table
, sn_l1_bytes
);
519 ret
= qcow2_update_snapshot_refcount(bs
, sn
->l1_table_offset
,
525 ret
= qcow2_pre_write_overlap_check(bs
, QCOW2_OL_ACTIVE_L1
,
526 s
->l1_table_offset
, cur_l1_bytes
);
531 ret
= bdrv_pwrite_sync(bs
->file
->bs
, s
->l1_table_offset
, sn_l1_table
,
538 * Decrease refcount of clusters of current L1 table.
540 * At this point, the in-memory s->l1_table points to the old L1 table,
541 * whereas on disk we already have the new one.
543 * qcow2_update_snapshot_refcount special cases the current L1 table to use
544 * the in-memory data instead of really using the offset to load a new one,
545 * which is why this works.
547 ret
= qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
,
551 * Now update the in-memory L1 table to be in sync with the on-disk one. We
552 * need to do this even if updating refcounts failed.
554 for(i
= 0;i
< s
->l1_size
; i
++) {
555 s
->l1_table
[i
] = be64_to_cpu(sn_l1_table
[i
]);
566 * Update QCOW_OFLAG_COPIED in the active L1 table (it may have changed
567 * when we decreased the refcount of the old snapshot.
569 ret
= qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 0);
576 BdrvCheckResult result
= {0};
577 qcow2_check_refcounts(bs
, &result
, 0);
587 int qcow2_snapshot_delete(BlockDriverState
*bs
,
588 const char *snapshot_id
,
592 BDRVQcow2State
*s
= bs
->opaque
;
594 int snapshot_index
, ret
;
596 /* Search the snapshot */
597 snapshot_index
= find_snapshot_by_id_and_name(bs
, snapshot_id
, name
);
598 if (snapshot_index
< 0) {
599 error_setg(errp
, "Can't find the snapshot");
602 sn
= s
->snapshots
[snapshot_index
];
604 /* Remove it from the snapshot list */
605 memmove(s
->snapshots
+ snapshot_index
,
606 s
->snapshots
+ snapshot_index
+ 1,
607 (s
->nb_snapshots
- snapshot_index
- 1) * sizeof(sn
));
609 ret
= qcow2_write_snapshots(bs
);
611 error_setg_errno(errp
, -ret
,
612 "Failed to remove snapshot from snapshot list");
617 * The snapshot is now unused, clean up. If we fail after this point, we
618 * won't recover but just leak clusters.
624 * Now decrease the refcounts of clusters referenced by the snapshot and
627 ret
= qcow2_update_snapshot_refcount(bs
, sn
.l1_table_offset
,
630 error_setg_errno(errp
, -ret
, "Failed to free the cluster and L1 table");
633 qcow2_free_clusters(bs
, sn
.l1_table_offset
, sn
.l1_size
* sizeof(uint64_t),
634 QCOW2_DISCARD_SNAPSHOT
);
636 /* must update the copied flag on the current cluster offsets */
637 ret
= qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 0);
639 error_setg_errno(errp
, -ret
,
640 "Failed to update snapshot status in disk");
646 BdrvCheckResult result
= {0};
647 qcow2_check_refcounts(bs
, &result
, 0);
653 int qcow2_snapshot_list(BlockDriverState
*bs
, QEMUSnapshotInfo
**psn_tab
)
655 BDRVQcow2State
*s
= bs
->opaque
;
656 QEMUSnapshotInfo
*sn_tab
, *sn_info
;
660 if (!s
->nb_snapshots
) {
662 return s
->nb_snapshots
;
665 sn_tab
= g_new0(QEMUSnapshotInfo
, s
->nb_snapshots
);
666 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
667 sn_info
= sn_tab
+ i
;
668 sn
= s
->snapshots
+ i
;
669 pstrcpy(sn_info
->id_str
, sizeof(sn_info
->id_str
),
671 pstrcpy(sn_info
->name
, sizeof(sn_info
->name
),
673 sn_info
->vm_state_size
= sn
->vm_state_size
;
674 sn_info
->date_sec
= sn
->date_sec
;
675 sn_info
->date_nsec
= sn
->date_nsec
;
676 sn_info
->vm_clock_nsec
= sn
->vm_clock_nsec
;
679 return s
->nb_snapshots
;
682 int qcow2_snapshot_load_tmp(BlockDriverState
*bs
,
683 const char *snapshot_id
,
687 int i
, snapshot_index
;
688 BDRVQcow2State
*s
= bs
->opaque
;
690 uint64_t *new_l1_table
;
694 assert(bs
->read_only
);
696 /* Search the snapshot */
697 snapshot_index
= find_snapshot_by_id_and_name(bs
, snapshot_id
, name
);
698 if (snapshot_index
< 0) {
700 "Can't find snapshot");
703 sn
= &s
->snapshots
[snapshot_index
];
705 /* Allocate and read in the snapshot's L1 table */
706 if (sn
->l1_size
> QCOW_MAX_L1_SIZE
/ sizeof(uint64_t)) {
707 error_setg(errp
, "Snapshot L1 table too large");
710 new_l1_bytes
= sn
->l1_size
* sizeof(uint64_t);
711 new_l1_table
= qemu_try_blockalign(bs
->file
->bs
,
712 align_offset(new_l1_bytes
, 512));
713 if (new_l1_table
== NULL
) {
717 ret
= bdrv_pread(bs
->file
->bs
, sn
->l1_table_offset
,
718 new_l1_table
, new_l1_bytes
);
720 error_setg(errp
, "Failed to read l1 table for snapshot");
721 qemu_vfree(new_l1_table
);
725 /* Switch the L1 table */
726 qemu_vfree(s
->l1_table
);
728 s
->l1_size
= sn
->l1_size
;
729 s
->l1_table_offset
= sn
->l1_table_offset
;
730 s
->l1_table
= new_l1_table
;
732 for(i
= 0;i
< s
->l1_size
; i
++) {
733 be64_to_cpus(&s
->l1_table
[i
]);