Make sure to enable dirty tracking of VBE vram mapping
[qemu/aliguori-queue.git] / block / qcow2-snapshot.c
blobd63c7e17d80ebbda29e53587a03d679c45b40ff7
1 /*
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
22 * THE SOFTWARE.
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;
33 uint32_t l1_size;
34 uint16_t id_str_size;
35 uint16_t name_size;
37 uint32_t date_sec;
38 uint32_t date_nsec;
40 uint64_t vm_clock_nsec;
42 uint32_t vm_state_size;
43 uint32_t extra_data_size; /* for extension */
44 /* extra data follows */
45 /* id_str follows */
46 /* name follows */
47 } QCowSnapshotHeader;
49 void qcow2_free_snapshots(BlockDriverState *bs)
51 BDRVQcowState *s = bs->opaque;
52 int i;
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);
59 s->snapshots = NULL;
60 s->nb_snapshots = 0;
63 int qcow2_read_snapshots(BlockDriverState *bs)
65 BDRVQcowState *s = bs->opaque;
66 QCowSnapshotHeader h;
67 QCowSnapshot *sn;
68 int i, id_str_size, name_size;
69 int64_t offset;
70 uint32_t extra_data_size;
72 if (!s->nb_snapshots) {
73 s->snapshots = NULL;
74 s->snapshots_size = 0;
75 return 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(s->hd, offset, &h, sizeof(h)) != sizeof(h))
83 goto fail;
84 offset += 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(s->hd, offset, sn->id_str, id_str_size) != id_str_size)
101 goto fail;
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(s->hd, offset, sn->name, name_size) != name_size)
107 goto fail;
108 offset += name_size;
109 sn->name[name_size] = '\0';
111 s->snapshots_size = offset - s->snapshots_offset;
112 return 0;
113 fail:
114 qcow2_free_snapshots(bs);
115 return -1;
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;
122 QCowSnapshot *sn;
123 QCowSnapshotHeader h;
124 int i, name_size, id_str_size, snapshots_size;
125 uint64_t data64;
126 uint32_t data32;
127 int64_t offset, snapshots_offset;
129 /* compute the size of the snapshots */
130 offset = 0;
131 for(i = 0; i < s->nb_snapshots; i++) {
132 sn = s->snapshots + i;
133 offset = align_offset(offset, 8);
134 offset += sizeof(h);
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;
143 for(i = 0; i < s->nb_snapshots; i++) {
144 sn = s->snapshots + i;
145 memset(&h, 0, sizeof(h));
146 h.l1_table_offset = cpu_to_be64(sn->l1_table_offset);
147 h.l1_size = cpu_to_be32(sn->l1_size);
148 h.vm_state_size = cpu_to_be32(sn->vm_state_size);
149 h.date_sec = cpu_to_be32(sn->date_sec);
150 h.date_nsec = cpu_to_be32(sn->date_nsec);
151 h.vm_clock_nsec = cpu_to_be64(sn->vm_clock_nsec);
153 id_str_size = strlen(sn->id_str);
154 name_size = strlen(sn->name);
155 h.id_str_size = cpu_to_be16(id_str_size);
156 h.name_size = cpu_to_be16(name_size);
157 offset = align_offset(offset, 8);
158 if (bdrv_pwrite(s->hd, offset, &h, sizeof(h)) != sizeof(h))
159 goto fail;
160 offset += sizeof(h);
161 if (bdrv_pwrite(s->hd, offset, sn->id_str, id_str_size) != id_str_size)
162 goto fail;
163 offset += id_str_size;
164 if (bdrv_pwrite(s->hd, offset, sn->name, name_size) != name_size)
165 goto fail;
166 offset += name_size;
169 /* update the various header fields */
170 data64 = cpu_to_be64(snapshots_offset);
171 if (bdrv_pwrite(s->hd, offsetof(QCowHeader, snapshots_offset),
172 &data64, sizeof(data64)) != sizeof(data64))
173 goto fail;
174 data32 = cpu_to_be32(s->nb_snapshots);
175 if (bdrv_pwrite(s->hd, offsetof(QCowHeader, nb_snapshots),
176 &data32, sizeof(data32)) != sizeof(data32))
177 goto fail;
179 /* free the old snapshot table */
180 qcow2_free_clusters(bs, s->snapshots_offset, s->snapshots_size);
181 s->snapshots_offset = snapshots_offset;
182 s->snapshots_size = snapshots_size;
183 return 0;
184 fail:
185 return -1;
188 static void find_new_snapshot_id(BlockDriverState *bs,
189 char *id_str, int id_str_size)
191 BDRVQcowState *s = bs->opaque;
192 QCowSnapshot *sn;
193 int i, id, id_max = 0;
195 for(i = 0; i < s->nb_snapshots; i++) {
196 sn = s->snapshots + i;
197 id = strtoul(sn->id_str, NULL, 10);
198 if (id > id_max)
199 id_max = id;
201 snprintf(id_str, id_str_size, "%d", id_max + 1);
204 static int find_snapshot_by_id(BlockDriverState *bs, const char *id_str)
206 BDRVQcowState *s = bs->opaque;
207 int i;
209 for(i = 0; i < s->nb_snapshots; i++) {
210 if (!strcmp(s->snapshots[i].id_str, id_str))
211 return i;
213 return -1;
216 static int find_snapshot_by_id_or_name(BlockDriverState *bs, const char *name)
218 BDRVQcowState *s = bs->opaque;
219 int i, ret;
221 ret = find_snapshot_by_id(bs, name);
222 if (ret >= 0)
223 return ret;
224 for(i = 0; i < s->nb_snapshots; i++) {
225 if (!strcmp(s->snapshots[i].name, name))
226 return i;
228 return -1;
231 /* if no id is provided, a new one is constructed */
232 int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
234 BDRVQcowState *s = bs->opaque;
235 QCowSnapshot *snapshots1, sn1, *sn = &sn1;
236 int i, ret;
237 uint64_t *l1_table = NULL;
239 memset(sn, 0, sizeof(*sn));
241 if (sn_info->id_str[0] == '\0') {
242 /* compute a new id */
243 find_new_snapshot_id(bs, sn_info->id_str, sizeof(sn_info->id_str));
246 /* check that the ID is unique */
247 if (find_snapshot_by_id(bs, sn_info->id_str) >= 0)
248 return -ENOENT;
250 sn->id_str = qemu_strdup(sn_info->id_str);
251 if (!sn->id_str)
252 goto fail;
253 sn->name = qemu_strdup(sn_info->name);
254 if (!sn->name)
255 goto fail;
256 sn->vm_state_size = sn_info->vm_state_size;
257 sn->date_sec = sn_info->date_sec;
258 sn->date_nsec = sn_info->date_nsec;
259 sn->vm_clock_nsec = sn_info->vm_clock_nsec;
261 ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1);
262 if (ret < 0)
263 goto fail;
265 /* create the L1 table of the snapshot */
266 sn->l1_table_offset = qcow2_alloc_clusters(bs, s->l1_size * sizeof(uint64_t));
267 sn->l1_size = s->l1_size;
269 if (s->l1_size != 0) {
270 l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t));
271 } else {
272 l1_table = NULL;
275 for(i = 0; i < s->l1_size; i++) {
276 l1_table[i] = cpu_to_be64(s->l1_table[i]);
278 if (bdrv_pwrite(s->hd, sn->l1_table_offset,
279 l1_table, s->l1_size * sizeof(uint64_t)) !=
280 (s->l1_size * sizeof(uint64_t)))
281 goto fail;
282 qemu_free(l1_table);
283 l1_table = NULL;
285 snapshots1 = qemu_malloc((s->nb_snapshots + 1) * sizeof(QCowSnapshot));
286 if (s->snapshots) {
287 memcpy(snapshots1, s->snapshots, s->nb_snapshots * sizeof(QCowSnapshot));
288 qemu_free(s->snapshots);
290 s->snapshots = snapshots1;
291 s->snapshots[s->nb_snapshots++] = *sn;
293 if (qcow_write_snapshots(bs) < 0)
294 goto fail;
295 #ifdef DEBUG_ALLOC
296 qcow2_check_refcounts(bs);
297 #endif
298 return 0;
299 fail:
300 qemu_free(sn->name);
301 qemu_free(l1_table);
302 return -1;
305 /* copy the snapshot 'snapshot_name' into the current disk image */
306 int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)
308 BDRVQcowState *s = bs->opaque;
309 QCowSnapshot *sn;
310 int i, snapshot_index, l1_size2;
312 snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id);
313 if (snapshot_index < 0)
314 return -ENOENT;
315 sn = &s->snapshots[snapshot_index];
317 if (qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, -1) < 0)
318 goto fail;
320 if (qcow2_grow_l1_table(bs, sn->l1_size) < 0)
321 goto fail;
323 s->l1_size = sn->l1_size;
324 l1_size2 = s->l1_size * sizeof(uint64_t);
325 /* copy the snapshot l1 table to the current l1 table */
326 if (bdrv_pread(s->hd, sn->l1_table_offset,
327 s->l1_table, l1_size2) != l1_size2)
328 goto fail;
329 if (bdrv_pwrite(s->hd, s->l1_table_offset,
330 s->l1_table, l1_size2) != l1_size2)
331 goto fail;
332 for(i = 0;i < s->l1_size; i++) {
333 be64_to_cpus(&s->l1_table[i]);
336 if (qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1) < 0)
337 goto fail;
339 #ifdef DEBUG_ALLOC
340 qcow2_check_refcounts(bs);
341 #endif
342 return 0;
343 fail:
344 return -EIO;
347 int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
349 BDRVQcowState *s = bs->opaque;
350 QCowSnapshot *sn;
351 int snapshot_index, ret;
353 snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id);
354 if (snapshot_index < 0)
355 return -ENOENT;
356 sn = &s->snapshots[snapshot_index];
358 ret = qcow2_update_snapshot_refcount(bs, sn->l1_table_offset, sn->l1_size, -1);
359 if (ret < 0)
360 return ret;
361 /* must update the copied flag on the current cluster offsets */
362 ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0);
363 if (ret < 0)
364 return ret;
365 qcow2_free_clusters(bs, sn->l1_table_offset, sn->l1_size * sizeof(uint64_t));
367 qemu_free(sn->id_str);
368 qemu_free(sn->name);
369 memmove(sn, sn + 1, (s->nb_snapshots - snapshot_index - 1) * sizeof(*sn));
370 s->nb_snapshots--;
371 ret = qcow_write_snapshots(bs);
372 if (ret < 0) {
373 /* XXX: restore snapshot if error ? */
374 return ret;
376 #ifdef DEBUG_ALLOC
377 qcow2_check_refcounts(bs);
378 #endif
379 return 0;
382 int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
384 BDRVQcowState *s = bs->opaque;
385 QEMUSnapshotInfo *sn_tab, *sn_info;
386 QCowSnapshot *sn;
387 int i;
389 if (!s->nb_snapshots) {
390 *psn_tab = NULL;
391 return s->nb_snapshots;
394 sn_tab = qemu_mallocz(s->nb_snapshots * sizeof(QEMUSnapshotInfo));
395 for(i = 0; i < s->nb_snapshots; i++) {
396 sn_info = sn_tab + i;
397 sn = s->snapshots + i;
398 pstrcpy(sn_info->id_str, sizeof(sn_info->id_str),
399 sn->id_str);
400 pstrcpy(sn_info->name, sizeof(sn_info->name),
401 sn->name);
402 sn_info->vm_state_size = sn->vm_state_size;
403 sn_info->date_sec = sn->date_sec;
404 sn_info->date_nsec = sn->date_nsec;
405 sn_info->vm_clock_nsec = sn->vm_clock_nsec;
407 *psn_tab = sn_tab;
408 return s->nb_snapshots;