]> git.proxmox.com Git - mirror_qemu.git/blame - block/qcow2-snapshot.c
qcow2: Fix order of refcount updates in qcow2_snapshot_goto
[mirror_qemu.git] / block / qcow2-snapshot.c
CommitLineData
c142442b
KW
1/*
2 * Block driver for the QCOW version 2 format
3 *
4 * Copyright (c) 2004-2006 Fabrice Bellard
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 */
24
25#include "qemu-common.h"
26#include "block_int.h"
27#include "block/qcow2.h"
28
541dc0d4 29typedef struct QEMU_PACKED QCowSnapshotHeader {
c142442b
KW
30 /* header is 8 byte aligned */
31 uint64_t l1_table_offset;
32
33 uint32_t l1_size;
34 uint16_t id_str_size;
35 uint16_t name_size;
36
37 uint32_t date_sec;
38 uint32_t date_nsec;
39
40 uint64_t vm_clock_nsec;
41
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;
48
ed6ccf0f 49void qcow2_free_snapshots(BlockDriverState *bs)
c142442b
KW
50{
51 BDRVQcowState *s = bs->opaque;
52 int i;
53
54 for(i = 0; i < s->nb_snapshots; i++) {
7267c094
AL
55 g_free(s->snapshots[i].name);
56 g_free(s->snapshots[i].id_str);
c142442b 57 }
7267c094 58 g_free(s->snapshots);
c142442b
KW
59 s->snapshots = NULL;
60 s->nb_snapshots = 0;
61}
62
ed6ccf0f 63int qcow2_read_snapshots(BlockDriverState *bs)
c142442b
KW
64{
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;
42deb29f 71 int ret;
c142442b
KW
72
73 if (!s->nb_snapshots) {
74 s->snapshots = NULL;
75 s->snapshots_size = 0;
76 return 0;
77 }
78
79 offset = s->snapshots_offset;
7267c094 80 s->snapshots = g_malloc0(s->nb_snapshots * sizeof(QCowSnapshot));
42deb29f 81
c142442b 82 for(i = 0; i < s->nb_snapshots; i++) {
42deb29f 83 /* Read statically sized part of the snapshot header */
c142442b 84 offset = align_offset(offset, 8);
42deb29f
KW
85 ret = bdrv_pread(bs->file, offset, &h, sizeof(h));
86 if (ret < 0) {
c142442b 87 goto fail;
42deb29f
KW
88 }
89
c142442b
KW
90 offset += 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);
99
100 id_str_size = be16_to_cpu(h.id_str_size);
101 name_size = be16_to_cpu(h.name_size);
102
42deb29f 103 /* Skip extra data */
c142442b
KW
104 offset += extra_data_size;
105
42deb29f 106 /* Read snapshot ID */
7267c094 107 sn->id_str = g_malloc(id_str_size + 1);
42deb29f
KW
108 ret = bdrv_pread(bs->file, offset, sn->id_str, id_str_size);
109 if (ret < 0) {
c142442b 110 goto fail;
42deb29f 111 }
c142442b
KW
112 offset += id_str_size;
113 sn->id_str[id_str_size] = '\0';
114
42deb29f 115 /* Read snapshot name */
7267c094 116 sn->name = g_malloc(name_size + 1);
42deb29f
KW
117 ret = bdrv_pread(bs->file, offset, sn->name, name_size);
118 if (ret < 0) {
c142442b 119 goto fail;
42deb29f 120 }
c142442b
KW
121 offset += name_size;
122 sn->name[name_size] = '\0';
123 }
42deb29f 124
c142442b
KW
125 s->snapshots_size = offset - s->snapshots_offset;
126 return 0;
42deb29f
KW
127
128fail:
ed6ccf0f 129 qcow2_free_snapshots(bs);
42deb29f 130 return ret;
c142442b
KW
131}
132
133/* add at the end of the file a new list of snapshots */
7c80ab3f 134static int qcow2_write_snapshots(BlockDriverState *bs)
c142442b
KW
135{
136 BDRVQcowState *s = bs->opaque;
137 QCowSnapshot *sn;
138 QCowSnapshotHeader h;
139 int i, name_size, id_str_size, snapshots_size;
d69969c4
KW
140 struct {
141 uint32_t nb_snapshots;
142 uint64_t snapshots_offset;
143 } QEMU_PACKED header_data;
c142442b 144 int64_t offset, snapshots_offset;
07fd8779 145 int ret;
c142442b
KW
146
147 /* compute the size of the snapshots */
148 offset = 0;
149 for(i = 0; i < s->nb_snapshots; i++) {
150 sn = s->snapshots + i;
151 offset = align_offset(offset, 8);
152 offset += sizeof(h);
153 offset += strlen(sn->id_str);
154 offset += strlen(sn->name);
155 }
156 snapshots_size = offset;
157
07fd8779 158 /* Allocate space for the new snapshot list */
ed6ccf0f 159 snapshots_offset = qcow2_alloc_clusters(bs, snapshots_size);
29216ed1 160 bdrv_flush(bs->file);
c142442b 161 offset = snapshots_offset;
5d757b56
KW
162 if (offset < 0) {
163 return offset;
164 }
c142442b 165
07fd8779 166 /* Write all snapshots to the new list */
c142442b
KW
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);
176
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);
07fd8779
KW
182
183 ret = bdrv_pwrite(bs->file, offset, &h, sizeof(h));
184 if (ret < 0) {
c142442b 185 goto fail;
07fd8779 186 }
c142442b 187 offset += sizeof(h);
07fd8779
KW
188
189 ret = bdrv_pwrite(bs->file, offset, sn->id_str, id_str_size);
190 if (ret < 0) {
c142442b 191 goto fail;
07fd8779 192 }
c142442b 193 offset += id_str_size;
07fd8779
KW
194
195 ret = bdrv_pwrite(bs->file, offset, sn->name, name_size);
196 if (ret < 0) {
c142442b 197 goto fail;
07fd8779 198 }
c142442b
KW
199 offset += name_size;
200 }
201
07fd8779
KW
202 /*
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.
07fd8779
KW
205 */
206 ret = bdrv_flush(bs);
207 if (ret < 0) {
208 goto fail;
209 }
210
d69969c4
KW
211 QEMU_BUILD_BUG_ON(offsetof(QCowHeader, snapshots_offset) !=
212 offsetof(QCowHeader, nb_snapshots) + sizeof(header_data.nb_snapshots));
213
214 header_data.nb_snapshots = cpu_to_be32(s->nb_snapshots);
215 header_data.snapshots_offset = cpu_to_be64(snapshots_offset);
07fd8779 216
07fd8779 217 ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, nb_snapshots),
d69969c4 218 &header_data, sizeof(header_data));
07fd8779 219 if (ret < 0) {
c142442b 220 goto fail;
07fd8779 221 }
c142442b
KW
222
223 /* free the old snapshot table */
ed6ccf0f 224 qcow2_free_clusters(bs, s->snapshots_offset, s->snapshots_size);
c142442b
KW
225 s->snapshots_offset = snapshots_offset;
226 s->snapshots_size = snapshots_size;
227 return 0;
07fd8779
KW
228
229fail:
230 return ret;
c142442b
KW
231}
232
233static void find_new_snapshot_id(BlockDriverState *bs,
234 char *id_str, int id_str_size)
235{
236 BDRVQcowState *s = bs->opaque;
237 QCowSnapshot *sn;
238 int i, id, id_max = 0;
239
240 for(i = 0; i < s->nb_snapshots; i++) {
241 sn = s->snapshots + i;
242 id = strtoul(sn->id_str, NULL, 10);
243 if (id > id_max)
244 id_max = id;
245 }
246 snprintf(id_str, id_str_size, "%d", id_max + 1);
247}
248
249static int find_snapshot_by_id(BlockDriverState *bs, const char *id_str)
250{
251 BDRVQcowState *s = bs->opaque;
252 int i;
253
254 for(i = 0; i < s->nb_snapshots; i++) {
255 if (!strcmp(s->snapshots[i].id_str, id_str))
256 return i;
257 }
258 return -1;
259}
260
261static int find_snapshot_by_id_or_name(BlockDriverState *bs, const char *name)
262{
263 BDRVQcowState *s = bs->opaque;
264 int i, ret;
265
266 ret = find_snapshot_by_id(bs, name);
267 if (ret >= 0)
268 return ret;
269 for(i = 0; i < s->nb_snapshots; i++) {
270 if (!strcmp(s->snapshots[i].name, name))
271 return i;
272 }
273 return -1;
274}
275
276/* if no id is provided, a new one is constructed */
ed6ccf0f 277int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
c142442b
KW
278{
279 BDRVQcowState *s = bs->opaque;
d1ea98d5
KW
280 QCowSnapshot *new_snapshot_list = NULL;
281 QCowSnapshot *old_snapshot_list = NULL;
282 QCowSnapshot sn1, *sn = &sn1;
c142442b
KW
283 int i, ret;
284 uint64_t *l1_table = NULL;
5d757b56 285 int64_t l1_table_offset;
c142442b
KW
286
287 memset(sn, 0, sizeof(*sn));
288
03343166 289 /* Generate an ID if it wasn't passed */
c142442b 290 if (sn_info->id_str[0] == '\0') {
c142442b
KW
291 find_new_snapshot_id(bs, sn_info->id_str, sizeof(sn_info->id_str));
292 }
293
03343166
KW
294 /* Check that the ID is unique */
295 if (find_snapshot_by_id(bs, sn_info->id_str) >= 0) {
c142442b 296 return -ENOENT;
03343166 297 }
c142442b 298
03343166 299 /* Populate sn with passed data */
7267c094 300 sn->id_str = g_strdup(sn_info->id_str);
7267c094 301 sn->name = g_strdup(sn_info->name);
03343166 302
c142442b
KW
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;
307
03343166 308 /* Allocate the L1 table of the snapshot and copy the current one there. */
5d757b56
KW
309 l1_table_offset = qcow2_alloc_clusters(bs, s->l1_size * sizeof(uint64_t));
310 if (l1_table_offset < 0) {
d1ea98d5 311 ret = l1_table_offset;
5d757b56
KW
312 goto fail;
313 }
314
315 sn->l1_table_offset = l1_table_offset;
c142442b
KW
316 sn->l1_size = s->l1_size;
317
03343166 318 l1_table = g_malloc(s->l1_size * sizeof(uint64_t));
c142442b
KW
319 for(i = 0; i < s->l1_size; i++) {
320 l1_table[i] = cpu_to_be64(s->l1_table[i]);
321 }
d1ea98d5
KW
322
323 ret = bdrv_pwrite(bs->file, sn->l1_table_offset, l1_table,
324 s->l1_size * sizeof(uint64_t));
325 if (ret < 0) {
c142442b 326 goto fail;
d1ea98d5
KW
327 }
328
7267c094 329 g_free(l1_table);
c142442b
KW
330 l1_table = NULL;
331
d1ea98d5
KW
332 /*
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.
336 */
337 ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1);
338 if (ret < 0) {
339 goto fail;
340 }
341
342 ret = bdrv_flush(bs);
343 if (ret < 0) {
344 goto fail;
345 }
346
347 /* Append the new snapshot to the snapshot list */
348 new_snapshot_list = g_malloc((s->nb_snapshots + 1) * sizeof(QCowSnapshot));
c142442b 349 if (s->snapshots) {
d1ea98d5
KW
350 memcpy(new_snapshot_list, s->snapshots,
351 s->nb_snapshots * sizeof(QCowSnapshot));
352 old_snapshot_list = s->snapshots;
c142442b 353 }
d1ea98d5 354 s->snapshots = new_snapshot_list;
c142442b
KW
355 s->snapshots[s->nb_snapshots++] = *sn;
356
d1ea98d5
KW
357 ret = qcow2_write_snapshots(bs);
358 if (ret < 0) {
359 g_free(s->snapshots);
360 s->snapshots = old_snapshot_list;
c142442b 361 goto fail;
d1ea98d5
KW
362 }
363
364 g_free(old_snapshot_list);
365
c142442b 366#ifdef DEBUG_ALLOC
6cbc3031
PH
367 {
368 BdrvCheckResult result = {0};
369 qcow2_check_refcounts(bs, &result);
370 }
c142442b
KW
371#endif
372 return 0;
03343166
KW
373
374fail:
375 g_free(sn->id_str);
7267c094
AL
376 g_free(sn->name);
377 g_free(l1_table);
d1ea98d5
KW
378
379 return ret;
c142442b
KW
380}
381
382/* copy the snapshot 'snapshot_name' into the current disk image */
ed6ccf0f 383int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)
c142442b
KW
384{
385 BDRVQcowState *s = bs->opaque;
386 QCowSnapshot *sn;
35d7ace7
KW
387 int i, snapshot_index;
388 int cur_l1_bytes, sn_l1_bytes;
589f284b 389 int ret;
43a0cac4 390 uint64_t *sn_l1_table = NULL;
c142442b 391
589f284b 392 /* Search the snapshot */
c142442b 393 snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id);
589f284b 394 if (snapshot_index < 0) {
c142442b 395 return -ENOENT;
589f284b 396 }
c142442b
KW
397 sn = &s->snapshots[snapshot_index];
398
589f284b
KW
399 /*
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.
403 */
404 ret = qcow2_grow_l1_table(bs, sn->l1_size, true);
405 if (ret < 0) {
c142442b 406 goto fail;
589f284b 407 }
c142442b 408
35d7ace7
KW
409 cur_l1_bytes = s->l1_size * sizeof(uint64_t);
410 sn_l1_bytes = sn->l1_size * sizeof(uint64_t);
411
589f284b
KW
412 /*
413 * Copy the snapshot L1 table to the current L1 table.
414 *
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.
43a0cac4
KW
417 * Decrease the refcount referenced by the old one only when the L1
418 * table is overwritten.
589f284b 419 */
43a0cac4
KW
420 sn_l1_table = g_malloc0(cur_l1_bytes);
421
422 ret = bdrv_pread(bs->file, sn->l1_table_offset, sn_l1_table, sn_l1_bytes);
423 if (ret < 0) {
424 goto fail;
425 }
426
427 ret = qcow2_update_snapshot_refcount(bs, sn->l1_table_offset,
428 sn->l1_size, 1);
589f284b 429 if (ret < 0) {
c142442b 430 goto fail;
589f284b
KW
431 }
432
43a0cac4 433 ret = bdrv_pwrite_sync(bs->file, s->l1_table_offset, sn_l1_table,
589f284b
KW
434 cur_l1_bytes);
435 if (ret < 0) {
c142442b 436 goto fail;
589f284b
KW
437 }
438
43a0cac4
KW
439 /*
440 * Decrease refcount of clusters of current L1 table.
441 *
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.
444 *
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.
448 */
449 ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset,
450 s->l1_size, -1);
451
452 /*
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.
455 */
c142442b 456 for(i = 0;i < s->l1_size; i++) {
43a0cac4 457 s->l1_table[i] = be64_to_cpu(sn_l1_table[i]);
c142442b
KW
458 }
459
43a0cac4
KW
460 if (ret < 0) {
461 goto fail;
462 }
463
464 g_free(sn_l1_table);
465 sn_l1_table = NULL;
466
467 /*
468 * Update QCOW_OFLAG_COPIED in the active L1 table (it may have changed
469 * when we decreased the refcount of the old snapshot.
470 */
471 ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0);
589f284b 472 if (ret < 0) {
c142442b 473 goto fail;
589f284b 474 }
c142442b
KW
475
476#ifdef DEBUG_ALLOC
6cbc3031
PH
477 {
478 BdrvCheckResult result = {0};
479 qcow2_check_refcounts(bs, &result);
480 }
c142442b
KW
481#endif
482 return 0;
589f284b
KW
483
484fail:
43a0cac4 485 g_free(sn_l1_table);
589f284b 486 return ret;
c142442b
KW
487}
488
ed6ccf0f 489int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
c142442b
KW
490{
491 BDRVQcowState *s = bs->opaque;
492 QCowSnapshot *sn;
493 int snapshot_index, ret;
494
495 snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id);
496 if (snapshot_index < 0)
497 return -ENOENT;
498 sn = &s->snapshots[snapshot_index];
499
ed6ccf0f 500 ret = qcow2_update_snapshot_refcount(bs, sn->l1_table_offset, sn->l1_size, -1);
c142442b
KW
501 if (ret < 0)
502 return ret;
503 /* must update the copied flag on the current cluster offsets */
ed6ccf0f 504 ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0);
c142442b
KW
505 if (ret < 0)
506 return ret;
ed6ccf0f 507 qcow2_free_clusters(bs, sn->l1_table_offset, sn->l1_size * sizeof(uint64_t));
c142442b 508
7267c094
AL
509 g_free(sn->id_str);
510 g_free(sn->name);
c142442b
KW
511 memmove(sn, sn + 1, (s->nb_snapshots - snapshot_index - 1) * sizeof(*sn));
512 s->nb_snapshots--;
7c80ab3f 513 ret = qcow2_write_snapshots(bs);
c142442b
KW
514 if (ret < 0) {
515 /* XXX: restore snapshot if error ? */
516 return ret;
517 }
518#ifdef DEBUG_ALLOC
6cbc3031
PH
519 {
520 BdrvCheckResult result = {0};
521 qcow2_check_refcounts(bs, &result);
522 }
c142442b
KW
523#endif
524 return 0;
525}
526
ed6ccf0f 527int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
c142442b
KW
528{
529 BDRVQcowState *s = bs->opaque;
530 QEMUSnapshotInfo *sn_tab, *sn_info;
531 QCowSnapshot *sn;
532 int i;
533
534 if (!s->nb_snapshots) {
535 *psn_tab = NULL;
536 return s->nb_snapshots;
537 }
538
7267c094 539 sn_tab = g_malloc0(s->nb_snapshots * sizeof(QEMUSnapshotInfo));
c142442b
KW
540 for(i = 0; i < s->nb_snapshots; i++) {
541 sn_info = sn_tab + i;
542 sn = s->snapshots + i;
543 pstrcpy(sn_info->id_str, sizeof(sn_info->id_str),
544 sn->id_str);
545 pstrcpy(sn_info->name, sizeof(sn_info->name),
546 sn->name);
547 sn_info->vm_state_size = sn->vm_state_size;
548 sn_info->date_sec = sn->date_sec;
549 sn_info->date_nsec = sn->date_nsec;
550 sn_info->vm_clock_nsec = sn->vm_clock_nsec;
551 }
552 *psn_tab = sn_tab;
553 return s->nb_snapshots;
554}
555
51ef6727 556int qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name)
557{
558 int i, snapshot_index, l1_size2;
559 BDRVQcowState *s = bs->opaque;
560 QCowSnapshot *sn;
561
562 snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_name);
563 if (snapshot_index < 0) {
564 return -ENOENT;
565 }
566
567 sn = &s->snapshots[snapshot_index];
568 s->l1_size = sn->l1_size;
569 l1_size2 = s->l1_size * sizeof(uint64_t);
570 if (s->l1_table != NULL) {
7267c094 571 g_free(s->l1_table);
51ef6727 572 }
573
574 s->l1_table_offset = sn->l1_table_offset;
7267c094 575 s->l1_table = g_malloc0(align_offset(l1_size2, 512));
51ef6727 576
577 if (bdrv_pread(bs->file, sn->l1_table_offset,
578 s->l1_table, l1_size2) != l1_size2) {
579 return -1;
580 }
581
582 for(i = 0;i < s->l1_size; i++) {
583 be64_to_cpus(&s->l1_table[i]);
584 }
585 return 0;
586}