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 __attribute__((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 qemu_free(s
->snapshots
[i
].name
);
56 qemu_free(s
->snapshots
[i
].id_str
);
58 qemu_free(s
->snapshots
);
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
;
72 if (!s
->nb_snapshots
) {
74 s
->snapshots_size
= 0;
78 offset
= s
->snapshots_offset
;
79 s
->snapshots
= qemu_mallocz(s
->nb_snapshots
* sizeof(QCowSnapshot
));
80 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
81 offset
= align_offset(offset
, 8);
82 if (bdrv_pread(bs
->file
, offset
, &h
, sizeof(h
)) != sizeof(h
))
85 sn
= s
->snapshots
+ i
;
86 sn
->l1_table_offset
= be64_to_cpu(h
.l1_table_offset
);
87 sn
->l1_size
= be32_to_cpu(h
.l1_size
);
88 sn
->vm_state_size
= be32_to_cpu(h
.vm_state_size
);
89 sn
->date_sec
= be32_to_cpu(h
.date_sec
);
90 sn
->date_nsec
= be32_to_cpu(h
.date_nsec
);
91 sn
->vm_clock_nsec
= be64_to_cpu(h
.vm_clock_nsec
);
92 extra_data_size
= be32_to_cpu(h
.extra_data_size
);
94 id_str_size
= be16_to_cpu(h
.id_str_size
);
95 name_size
= be16_to_cpu(h
.name_size
);
97 offset
+= extra_data_size
;
99 sn
->id_str
= qemu_malloc(id_str_size
+ 1);
100 if (bdrv_pread(bs
->file
, offset
, sn
->id_str
, id_str_size
) != id_str_size
)
102 offset
+= id_str_size
;
103 sn
->id_str
[id_str_size
] = '\0';
105 sn
->name
= qemu_malloc(name_size
+ 1);
106 if (bdrv_pread(bs
->file
, offset
, sn
->name
, name_size
) != name_size
)
109 sn
->name
[name_size
] = '\0';
111 s
->snapshots_size
= offset
- s
->snapshots_offset
;
114 qcow2_free_snapshots(bs
);
118 /* add at the end of the file a new list of snapshots */
119 static int qcow_write_snapshots(BlockDriverState
*bs
)
121 BDRVQcowState
*s
= bs
->opaque
;
123 QCowSnapshotHeader h
;
124 int i
, name_size
, id_str_size
, snapshots_size
;
127 int64_t offset
, snapshots_offset
;
129 /* compute the size of the snapshots */
131 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
132 sn
= s
->snapshots
+ i
;
133 offset
= align_offset(offset
, 8);
135 offset
+= strlen(sn
->id_str
);
136 offset
+= strlen(sn
->name
);
138 snapshots_size
= offset
;
140 snapshots_offset
= qcow2_alloc_clusters(bs
, snapshots_size
);
141 offset
= snapshots_offset
;
146 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
147 sn
= s
->snapshots
+ i
;
148 memset(&h
, 0, sizeof(h
));
149 h
.l1_table_offset
= cpu_to_be64(sn
->l1_table_offset
);
150 h
.l1_size
= cpu_to_be32(sn
->l1_size
);
151 h
.vm_state_size
= cpu_to_be32(sn
->vm_state_size
);
152 h
.date_sec
= cpu_to_be32(sn
->date_sec
);
153 h
.date_nsec
= cpu_to_be32(sn
->date_nsec
);
154 h
.vm_clock_nsec
= cpu_to_be64(sn
->vm_clock_nsec
);
156 id_str_size
= strlen(sn
->id_str
);
157 name_size
= strlen(sn
->name
);
158 h
.id_str_size
= cpu_to_be16(id_str_size
);
159 h
.name_size
= cpu_to_be16(name_size
);
160 offset
= align_offset(offset
, 8);
161 if (bdrv_pwrite(bs
->file
, offset
, &h
, sizeof(h
)) != sizeof(h
))
164 if (bdrv_pwrite(bs
->file
, offset
, sn
->id_str
, id_str_size
) != id_str_size
)
166 offset
+= id_str_size
;
167 if (bdrv_pwrite(bs
->file
, offset
, sn
->name
, name_size
) != name_size
)
172 /* update the various header fields */
173 data64
= cpu_to_be64(snapshots_offset
);
174 if (bdrv_pwrite(bs
->file
, offsetof(QCowHeader
, snapshots_offset
),
175 &data64
, sizeof(data64
)) != sizeof(data64
))
177 data32
= cpu_to_be32(s
->nb_snapshots
);
178 if (bdrv_pwrite(bs
->file
, offsetof(QCowHeader
, nb_snapshots
),
179 &data32
, sizeof(data32
)) != sizeof(data32
))
182 /* free the old snapshot table */
183 qcow2_free_clusters(bs
, s
->snapshots_offset
, s
->snapshots_size
);
184 s
->snapshots_offset
= snapshots_offset
;
185 s
->snapshots_size
= snapshots_size
;
191 static void find_new_snapshot_id(BlockDriverState
*bs
,
192 char *id_str
, int id_str_size
)
194 BDRVQcowState
*s
= bs
->opaque
;
196 int i
, id
, id_max
= 0;
198 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
199 sn
= s
->snapshots
+ i
;
200 id
= strtoul(sn
->id_str
, NULL
, 10);
204 snprintf(id_str
, id_str_size
, "%d", id_max
+ 1);
207 static int find_snapshot_by_id(BlockDriverState
*bs
, const char *id_str
)
209 BDRVQcowState
*s
= bs
->opaque
;
212 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
213 if (!strcmp(s
->snapshots
[i
].id_str
, id_str
))
219 static int find_snapshot_by_id_or_name(BlockDriverState
*bs
, const char *name
)
221 BDRVQcowState
*s
= bs
->opaque
;
224 ret
= find_snapshot_by_id(bs
, name
);
227 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
228 if (!strcmp(s
->snapshots
[i
].name
, name
))
234 /* if no id is provided, a new one is constructed */
235 int qcow2_snapshot_create(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
)
237 BDRVQcowState
*s
= bs
->opaque
;
238 QCowSnapshot
*snapshots1
, sn1
, *sn
= &sn1
;
240 uint64_t *l1_table
= NULL
;
241 int64_t l1_table_offset
;
243 memset(sn
, 0, sizeof(*sn
));
245 if (sn_info
->id_str
[0] == '\0') {
246 /* compute a new id */
247 find_new_snapshot_id(bs
, sn_info
->id_str
, sizeof(sn_info
->id_str
));
250 /* check that the ID is unique */
251 if (find_snapshot_by_id(bs
, sn_info
->id_str
) >= 0)
254 sn
->id_str
= qemu_strdup(sn_info
->id_str
);
257 sn
->name
= qemu_strdup(sn_info
->name
);
260 sn
->vm_state_size
= sn_info
->vm_state_size
;
261 sn
->date_sec
= sn_info
->date_sec
;
262 sn
->date_nsec
= sn_info
->date_nsec
;
263 sn
->vm_clock_nsec
= sn_info
->vm_clock_nsec
;
265 ret
= qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 1);
269 /* create the L1 table of the snapshot */
270 l1_table_offset
= qcow2_alloc_clusters(bs
, s
->l1_size
* sizeof(uint64_t));
271 if (l1_table_offset
< 0) {
275 sn
->l1_table_offset
= l1_table_offset
;
276 sn
->l1_size
= s
->l1_size
;
278 if (s
->l1_size
!= 0) {
279 l1_table
= qemu_malloc(s
->l1_size
* sizeof(uint64_t));
284 for(i
= 0; i
< s
->l1_size
; i
++) {
285 l1_table
[i
] = cpu_to_be64(s
->l1_table
[i
]);
287 if (bdrv_pwrite(bs
->file
, sn
->l1_table_offset
,
288 l1_table
, s
->l1_size
* sizeof(uint64_t)) !=
289 (s
->l1_size
* sizeof(uint64_t)))
294 snapshots1
= qemu_malloc((s
->nb_snapshots
+ 1) * sizeof(QCowSnapshot
));
296 memcpy(snapshots1
, s
->snapshots
, s
->nb_snapshots
* sizeof(QCowSnapshot
));
297 qemu_free(s
->snapshots
);
299 s
->snapshots
= snapshots1
;
300 s
->snapshots
[s
->nb_snapshots
++] = *sn
;
302 if (qcow_write_snapshots(bs
) < 0)
305 qcow2_check_refcounts(bs
);
314 /* copy the snapshot 'snapshot_name' into the current disk image */
315 int qcow2_snapshot_goto(BlockDriverState
*bs
, const char *snapshot_id
)
317 BDRVQcowState
*s
= bs
->opaque
;
319 int i
, snapshot_index
, l1_size2
;
321 snapshot_index
= find_snapshot_by_id_or_name(bs
, snapshot_id
);
322 if (snapshot_index
< 0)
324 sn
= &s
->snapshots
[snapshot_index
];
326 if (qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, -1) < 0)
329 if (qcow2_grow_l1_table(bs
, sn
->l1_size
) < 0)
332 s
->l1_size
= sn
->l1_size
;
333 l1_size2
= s
->l1_size
* sizeof(uint64_t);
334 /* copy the snapshot l1 table to the current l1 table */
335 if (bdrv_pread(bs
->file
, sn
->l1_table_offset
,
336 s
->l1_table
, l1_size2
) != l1_size2
)
338 if (bdrv_pwrite(bs
->file
, s
->l1_table_offset
,
339 s
->l1_table
, l1_size2
) != l1_size2
)
341 for(i
= 0;i
< s
->l1_size
; i
++) {
342 be64_to_cpus(&s
->l1_table
[i
]);
345 if (qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 1) < 0)
349 qcow2_check_refcounts(bs
);
356 int qcow2_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
358 BDRVQcowState
*s
= bs
->opaque
;
360 int snapshot_index
, ret
;
362 snapshot_index
= find_snapshot_by_id_or_name(bs
, snapshot_id
);
363 if (snapshot_index
< 0)
365 sn
= &s
->snapshots
[snapshot_index
];
367 ret
= qcow2_update_snapshot_refcount(bs
, sn
->l1_table_offset
, sn
->l1_size
, -1);
370 /* must update the copied flag on the current cluster offsets */
371 ret
= qcow2_update_snapshot_refcount(bs
, s
->l1_table_offset
, s
->l1_size
, 0);
374 qcow2_free_clusters(bs
, sn
->l1_table_offset
, sn
->l1_size
* sizeof(uint64_t));
376 qemu_free(sn
->id_str
);
378 memmove(sn
, sn
+ 1, (s
->nb_snapshots
- snapshot_index
- 1) * sizeof(*sn
));
380 ret
= qcow_write_snapshots(bs
);
382 /* XXX: restore snapshot if error ? */
386 qcow2_check_refcounts(bs
);
391 int qcow2_snapshot_list(BlockDriverState
*bs
, QEMUSnapshotInfo
**psn_tab
)
393 BDRVQcowState
*s
= bs
->opaque
;
394 QEMUSnapshotInfo
*sn_tab
, *sn_info
;
398 if (!s
->nb_snapshots
) {
400 return s
->nb_snapshots
;
403 sn_tab
= qemu_mallocz(s
->nb_snapshots
* sizeof(QEMUSnapshotInfo
));
404 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
405 sn_info
= sn_tab
+ i
;
406 sn
= s
->snapshots
+ i
;
407 pstrcpy(sn_info
->id_str
, sizeof(sn_info
->id_str
),
409 pstrcpy(sn_info
->name
, sizeof(sn_info
->name
),
411 sn_info
->vm_state_size
= sn
->vm_state_size
;
412 sn_info
->date_sec
= sn
->date_sec
;
413 sn_info
->date_nsec
= sn
->date_nsec
;
414 sn_info
->vm_clock_nsec
= sn
->vm_clock_nsec
;
417 return s
->nb_snapshots
;