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-common.h"
26 #include "block_int.h"
27 #include "block/qcow2.h"
29 typedef struct QEMU_PACKED QCowSnapshotHeader
{
30 /* header is 8 byte aligned */
31 uint64_t l1_table_offset
;
40 uint64_t vm_clock_nsec
;
42 uint32_t vm_state_size
;
43 uint32_t extra_data_size
; /* for extension */
44 /* extra data follows */
49 void qcow2_free_snapshots(BlockDriverState
*bs
)
51 BDRVQcowState
*s
= bs
->opaque
;
54 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
55 g_free(s
->snapshots
[i
].name
);
56 g_free(s
->snapshots
[i
].id_str
);
63 int qcow2_read_snapshots(BlockDriverState
*bs
)
65 BDRVQcowState
*s
= bs
->opaque
;
68 int i
, id_str_size
, name_size
;
70 uint32_t extra_data_size
;
73 if (!s
->nb_snapshots
) {
75 s
->snapshots_size
= 0;
79 offset
= s
->snapshots_offset
;
80 s
->snapshots
= g_malloc0(s
->nb_snapshots
* sizeof(QCowSnapshot
));
82 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
83 /* Read statically sized part of the snapshot header */
84 offset
= align_offset(offset
, 8);
85 ret
= bdrv_pread(bs
->file
, offset
, &h
, sizeof(h
));
91 sn
= s
->snapshots
+ i
;
92 sn
->l1_table_offset
= be64_to_cpu(h
.l1_table_offset
);
93 sn
->l1_size
= be32_to_cpu(h
.l1_size
);
94 sn
->vm_state_size
= be32_to_cpu(h
.vm_state_size
);
95 sn
->date_sec
= be32_to_cpu(h
.date_sec
);
96 sn
->date_nsec
= be32_to_cpu(h
.date_nsec
);
97 sn
->vm_clock_nsec
= be64_to_cpu(h
.vm_clock_nsec
);
98 extra_data_size
= be32_to_cpu(h
.extra_data_size
);
100 id_str_size
= be16_to_cpu(h
.id_str_size
);
101 name_size
= be16_to_cpu(h
.name_size
);
103 /* Skip extra data */
104 offset
+= extra_data_size
;
106 /* Read snapshot ID */
107 sn
->id_str
= g_malloc(id_str_size
+ 1);
108 ret
= bdrv_pread(bs
->file
, offset
, sn
->id_str
, id_str_size
);
112 offset
+= id_str_size
;
113 sn
->id_str
[id_str_size
] = '\0';
115 /* Read snapshot name */
116 sn
->name
= g_malloc(name_size
+ 1);
117 ret
= bdrv_pread(bs
->file
, offset
, sn
->name
, name_size
);
122 sn
->name
[name_size
] = '\0';
125 s
->snapshots_size
= offset
- s
->snapshots_offset
;
129 qcow2_free_snapshots(bs
);
133 /* add at the end of the file a new list of snapshots */
134 static int qcow2_write_snapshots(BlockDriverState
*bs
)
136 BDRVQcowState
*s
= bs
->opaque
;
138 QCowSnapshotHeader h
;
139 int i
, name_size
, id_str_size
, snapshots_size
;
141 uint32_t nb_snapshots
;
142 uint64_t snapshots_offset
;
143 } QEMU_PACKED header_data
;
144 int64_t offset
, snapshots_offset
;
147 /* compute the size of the snapshots */
149 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
150 sn
= s
->snapshots
+ i
;
151 offset
= align_offset(offset
, 8);
153 offset
+= strlen(sn
->id_str
);
154 offset
+= strlen(sn
->name
);
156 snapshots_size
= offset
;
158 /* Allocate space for the new snapshot list */
159 snapshots_offset
= qcow2_alloc_clusters(bs
, snapshots_size
);
160 bdrv_flush(bs
->file
);
161 offset
= snapshots_offset
;
166 /* Write all snapshots to the new list */
167 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
168 sn
= s
->snapshots
+ i
;
169 memset(&h
, 0, sizeof(h
));
170 h
.l1_table_offset
= cpu_to_be64(sn
->l1_table_offset
);
171 h
.l1_size
= cpu_to_be32(sn
->l1_size
);
172 h
.vm_state_size
= cpu_to_be32(sn
->vm_state_size
);
173 h
.date_sec
= cpu_to_be32(sn
->date_sec
);
174 h
.date_nsec
= cpu_to_be32(sn
->date_nsec
);
175 h
.vm_clock_nsec
= cpu_to_be64(sn
->vm_clock_nsec
);
177 id_str_size
= strlen(sn
->id_str
);
178 name_size
= strlen(sn
->name
);
179 h
.id_str_size
= cpu_to_be16(id_str_size
);
180 h
.name_size
= cpu_to_be16(name_size
);
181 offset
= align_offset(offset
, 8);
183 ret
= bdrv_pwrite(bs
->file
, offset
, &h
, sizeof(h
));
189 ret
= bdrv_pwrite(bs
->file
, offset
, sn
->id_str
, id_str_size
);
193 offset
+= id_str_size
;
195 ret
= bdrv_pwrite(bs
->file
, offset
, sn
->name
, name_size
);
203 * Update the header to point to the new snapshot table. This requires the
204 * new table and its refcounts to be stable on disk.
206 ret
= bdrv_flush(bs
);
211 QEMU_BUILD_BUG_ON(offsetof(QCowHeader
, snapshots_offset
) !=
212 offsetof(QCowHeader
, nb_snapshots
) + sizeof(header_data
.nb_snapshots
));
214 header_data
.nb_snapshots
= cpu_to_be32(s
->nb_snapshots
);
215 header_data
.snapshots_offset
= cpu_to_be64(snapshots_offset
);
217 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, nb_snapshots
),
218 &header_data
, sizeof(header_data
));
223 /* free the old snapshot table */
224 qcow2_free_clusters(bs
, s
->snapshots_offset
, s
->snapshots_size
);
225 s
->snapshots_offset
= snapshots_offset
;
226 s
->snapshots_size
= snapshots_size
;
233 static void find_new_snapshot_id(BlockDriverState
*bs
,
234 char *id_str
, int id_str_size
)
236 BDRVQcowState
*s
= bs
->opaque
;
238 int i
, id
, id_max
= 0;
240 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
241 sn
= s
->snapshots
+ i
;
242 id
= strtoul(sn
->id_str
, NULL
, 10);
246 snprintf(id_str
, id_str_size
, "%d", id_max
+ 1);
249 static int find_snapshot_by_id(BlockDriverState
*bs
, const char *id_str
)
251 BDRVQcowState
*s
= bs
->opaque
;
254 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
255 if (!strcmp(s
->snapshots
[i
].id_str
, id_str
))
261 static int find_snapshot_by_id_or_name(BlockDriverState
*bs
, const char *name
)
263 BDRVQcowState
*s
= bs
->opaque
;
266 ret
= find_snapshot_by_id(bs
, name
);
269 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
270 if (!strcmp(s
->snapshots
[i
].name
, name
))
276 /* if no id is provided, a new one is constructed */
277 int qcow2_snapshot_create(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
)
279 BDRVQcowState
*s
= bs
->opaque
;
280 QCowSnapshot
*new_snapshot_list
= NULL
;
281 QCowSnapshot
*old_snapshot_list
= NULL
;
282 QCowSnapshot sn1
, *sn
= &sn1
;
284 uint64_t *l1_table
= NULL
;
285 int64_t l1_table_offset
;
287 memset(sn
, 0, sizeof(*sn
));
289 /* Generate an ID if it wasn't passed */
290 if (sn_info
->id_str
[0] == '\0') {
291 find_new_snapshot_id(bs
, sn_info
->id_str
, sizeof(sn_info
->id_str
));
294 /* Check that the ID is unique */
295 if (find_snapshot_by_id(bs
, sn_info
->id_str
) >= 0) {
299 /* Populate sn with passed data */
300 sn
->id_str
= g_strdup(sn_info
->id_str
);
301 sn
->name
= g_strdup(sn_info
->name
);
303 sn
->vm_state_size
= sn_info
->vm_state_size
;
304 sn
->date_sec
= sn_info
->date_sec
;
305 sn
->date_nsec
= sn_info
->date_nsec
;
306 sn
->vm_clock_nsec
= sn_info
->vm_clock_nsec
;
308 /* Allocate the L1 table of the snapshot and copy the current one there. */
309 l1_table_offset
= qcow2_alloc_clusters(bs
, s
->l1_size
* sizeof(uint64_t));
310 if (l1_table_offset
< 0) {
311 ret
= l1_table_offset
;
315 sn
->l1_table_offset
= l1_table_offset
;
316 sn
->l1_size
= s
->l1_size
;
318 l1_table
= g_malloc(s
->l1_size
* sizeof(uint64_t));
319 for(i
= 0; i
< s
->l1_size
; i
++) {
320 l1_table
[i
] = cpu_to_be64(s
->l1_table
[i
]);
323 ret
= bdrv_pwrite(bs
->file
, sn
->l1_table_offset
, l1_table
,
324 s
->l1_size
* sizeof(uint64_t));
333 * Increase the refcounts of all clusters and make sure everything is
334 * stable on disk before updating the snapshot table to contain a pointer
335 * to the new L1 table.
337 ret
= qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 1);
342 ret
= bdrv_flush(bs
);
347 /* Append the new snapshot to the snapshot list */
348 new_snapshot_list
= g_malloc((s
->nb_snapshots
+ 1) * sizeof(QCowSnapshot
));
350 memcpy(new_snapshot_list
, s
->snapshots
,
351 s
->nb_snapshots
* sizeof(QCowSnapshot
));
352 old_snapshot_list
= s
->snapshots
;
354 s
->snapshots
= new_snapshot_list
;
355 s
->snapshots
[s
->nb_snapshots
++] = *sn
;
357 ret
= qcow2_write_snapshots(bs
);
359 g_free(s
->snapshots
);
360 s
->snapshots
= old_snapshot_list
;
364 g_free(old_snapshot_list
);
368 BdrvCheckResult result
= {0};
369 qcow2_check_refcounts(bs
, &result
);
382 /* copy the snapshot 'snapshot_name' into the current disk image */
383 int qcow2_snapshot_goto(BlockDriverState
*bs
, const char *snapshot_id
)
385 BDRVQcowState
*s
= bs
->opaque
;
387 int i
, snapshot_index
;
388 int cur_l1_bytes
, sn_l1_bytes
;
390 uint64_t *sn_l1_table
= NULL
;
392 /* Search the snapshot */
393 snapshot_index
= find_snapshot_by_id_or_name(bs
, snapshot_id
);
394 if (snapshot_index
< 0) {
397 sn
= &s
->snapshots
[snapshot_index
];
400 * Make sure that the current L1 table is big enough to contain the whole
401 * L1 table of the snapshot. If the snapshot L1 table is smaller, the
402 * current one must be padded with zeros.
404 ret
= qcow2_grow_l1_table(bs
, sn
->l1_size
, true);
409 cur_l1_bytes
= s
->l1_size
* sizeof(uint64_t);
410 sn_l1_bytes
= sn
->l1_size
* sizeof(uint64_t);
413 * Copy the snapshot L1 table to the current L1 table.
415 * Before overwriting the old current L1 table on disk, make sure to
416 * increase all refcounts for the clusters referenced by the new one.
417 * Decrease the refcount referenced by the old one only when the L1
418 * table is overwritten.
420 sn_l1_table
= g_malloc0(cur_l1_bytes
);
422 ret
= bdrv_pread(bs
->file
, sn
->l1_table_offset
, sn_l1_table
, sn_l1_bytes
);
427 ret
= qcow2_update_snapshot_refcount(bs
, sn
->l1_table_offset
,
433 ret
= bdrv_pwrite_sync(bs
->file
, s
->l1_table_offset
, sn_l1_table
,
440 * Decrease refcount of clusters of current L1 table.
442 * At this point, the in-memory s->l1_table points to the old L1 table,
443 * whereas on disk we already have the new one.
445 * qcow2_update_snapshot_refcount special cases the current L1 table to use
446 * the in-memory data instead of really using the offset to load a new one,
447 * which is why this works.
449 ret
= qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
,
453 * Now update the in-memory L1 table to be in sync with the on-disk one. We
454 * need to do this even if updating refcounts failed.
456 for(i
= 0;i
< s
->l1_size
; i
++) {
457 s
->l1_table
[i
] = be64_to_cpu(sn_l1_table
[i
]);
468 * Update QCOW_OFLAG_COPIED in the active L1 table (it may have changed
469 * when we decreased the refcount of the old snapshot.
471 ret
= qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 0);
478 BdrvCheckResult result
= {0};
479 qcow2_check_refcounts(bs
, &result
);
489 int qcow2_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
491 BDRVQcowState
*s
= bs
->opaque
;
493 int snapshot_index
, ret
;
495 /* Search the snapshot */
496 snapshot_index
= find_snapshot_by_id_or_name(bs
, snapshot_id
);
497 if (snapshot_index
< 0) {
500 sn
= s
->snapshots
[snapshot_index
];
502 /* Remove it from the snapshot list */
503 memmove(s
->snapshots
+ snapshot_index
,
504 s
->snapshots
+ snapshot_index
+ 1,
505 (s
->nb_snapshots
- snapshot_index
- 1) * sizeof(sn
));
507 ret
= qcow2_write_snapshots(bs
);
513 * The snapshot is now unused, clean up. If we fail after this point, we
514 * won't recover but just leak clusters.
520 * Now decrease the refcounts of clusters referenced by the snapshot and
523 ret
= qcow2_update_snapshot_refcount(bs
, sn
.l1_table_offset
,
528 qcow2_free_clusters(bs
, sn
.l1_table_offset
, sn
.l1_size
* sizeof(uint64_t));
530 /* must update the copied flag on the current cluster offsets */
531 ret
= qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 0);
538 BdrvCheckResult result
= {0};
539 qcow2_check_refcounts(bs
, &result
);
545 int qcow2_snapshot_list(BlockDriverState
*bs
, QEMUSnapshotInfo
**psn_tab
)
547 BDRVQcowState
*s
= bs
->opaque
;
548 QEMUSnapshotInfo
*sn_tab
, *sn_info
;
552 if (!s
->nb_snapshots
) {
554 return s
->nb_snapshots
;
557 sn_tab
= g_malloc0(s
->nb_snapshots
* sizeof(QEMUSnapshotInfo
));
558 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
559 sn_info
= sn_tab
+ i
;
560 sn
= s
->snapshots
+ i
;
561 pstrcpy(sn_info
->id_str
, sizeof(sn_info
->id_str
),
563 pstrcpy(sn_info
->name
, sizeof(sn_info
->name
),
565 sn_info
->vm_state_size
= sn
->vm_state_size
;
566 sn_info
->date_sec
= sn
->date_sec
;
567 sn_info
->date_nsec
= sn
->date_nsec
;
568 sn_info
->vm_clock_nsec
= sn
->vm_clock_nsec
;
571 return s
->nb_snapshots
;
574 int qcow2_snapshot_load_tmp(BlockDriverState
*bs
, const char *snapshot_name
)
576 int i
, snapshot_index
;
577 BDRVQcowState
*s
= bs
->opaque
;
579 uint64_t *new_l1_table
;
583 assert(bs
->read_only
);
585 /* Search the snapshot */
586 snapshot_index
= find_snapshot_by_id_or_name(bs
, snapshot_name
);
587 if (snapshot_index
< 0) {
590 sn
= &s
->snapshots
[snapshot_index
];
592 /* Allocate and read in the snapshot's L1 table */
593 new_l1_bytes
= s
->l1_size
* sizeof(uint64_t);
594 new_l1_table
= g_malloc0(align_offset(new_l1_bytes
, 512));
596 ret
= bdrv_pread(bs
->file
, sn
->l1_table_offset
, new_l1_table
, new_l1_bytes
);
598 g_free(new_l1_table
);
602 /* Switch the L1 table */
605 s
->l1_size
= sn
->l1_size
;
606 s
->l1_table_offset
= sn
->l1_table_offset
;
607 s
->l1_table
= new_l1_table
;
609 for(i
= 0;i
< s
->l1_size
; i
++) {
610 be64_to_cpus(&s
->l1_table
[i
]);