]> git.proxmox.com Git - mirror_qemu.git/blame - block/qcow2-snapshot.c
vhost-user-test: use tmpfs by default
[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"
737e150e 26#include "block/block_int.h"
c142442b 27#include "block/qcow2.h"
d49b6836 28#include "qemu/error-report.h"
c142442b 29
ed6ccf0f 30void qcow2_free_snapshots(BlockDriverState *bs)
c142442b 31{
ff99129a 32 BDRVQcow2State *s = bs->opaque;
c142442b
KW
33 int i;
34
35 for(i = 0; i < s->nb_snapshots; i++) {
7267c094
AL
36 g_free(s->snapshots[i].name);
37 g_free(s->snapshots[i].id_str);
c142442b 38 }
7267c094 39 g_free(s->snapshots);
c142442b
KW
40 s->snapshots = NULL;
41 s->nb_snapshots = 0;
42}
43
ed6ccf0f 44int qcow2_read_snapshots(BlockDriverState *bs)
c142442b 45{
ff99129a 46 BDRVQcow2State *s = bs->opaque;
c142442b 47 QCowSnapshotHeader h;
c2c9a466 48 QCowSnapshotExtraData extra;
c142442b
KW
49 QCowSnapshot *sn;
50 int i, id_str_size, name_size;
51 int64_t offset;
52 uint32_t extra_data_size;
42deb29f 53 int ret;
c142442b
KW
54
55 if (!s->nb_snapshots) {
56 s->snapshots = NULL;
57 s->snapshots_size = 0;
58 return 0;
59 }
60
61 offset = s->snapshots_offset;
5839e53b 62 s->snapshots = g_new0(QCowSnapshot, s->nb_snapshots);
42deb29f 63
c142442b 64 for(i = 0; i < s->nb_snapshots; i++) {
42deb29f 65 /* Read statically sized part of the snapshot header */
c142442b 66 offset = align_offset(offset, 8);
42deb29f
KW
67 ret = bdrv_pread(bs->file, offset, &h, sizeof(h));
68 if (ret < 0) {
c142442b 69 goto fail;
42deb29f
KW
70 }
71
c142442b
KW
72 offset += sizeof(h);
73 sn = s->snapshots + i;
74 sn->l1_table_offset = be64_to_cpu(h.l1_table_offset);
75 sn->l1_size = be32_to_cpu(h.l1_size);
76 sn->vm_state_size = be32_to_cpu(h.vm_state_size);
77 sn->date_sec = be32_to_cpu(h.date_sec);
78 sn->date_nsec = be32_to_cpu(h.date_nsec);
79 sn->vm_clock_nsec = be64_to_cpu(h.vm_clock_nsec);
80 extra_data_size = be32_to_cpu(h.extra_data_size);
81
82 id_str_size = be16_to_cpu(h.id_str_size);
83 name_size = be16_to_cpu(h.name_size);
84
c2c9a466
KW
85 /* Read extra data */
86 ret = bdrv_pread(bs->file, offset, &extra,
87 MIN(sizeof(extra), extra_data_size));
88 if (ret < 0) {
89 goto fail;
90 }
c142442b
KW
91 offset += extra_data_size;
92
c2c9a466
KW
93 if (extra_data_size >= 8) {
94 sn->vm_state_size = be64_to_cpu(extra.vm_state_size_large);
95 }
96
90b27759
KW
97 if (extra_data_size >= 16) {
98 sn->disk_size = be64_to_cpu(extra.disk_size);
99 } else {
100 sn->disk_size = bs->total_sectors * BDRV_SECTOR_SIZE;
101 }
102
42deb29f 103 /* Read snapshot ID */
7267c094 104 sn->id_str = g_malloc(id_str_size + 1);
42deb29f
KW
105 ret = bdrv_pread(bs->file, offset, sn->id_str, id_str_size);
106 if (ret < 0) {
c142442b 107 goto fail;
42deb29f 108 }
c142442b
KW
109 offset += id_str_size;
110 sn->id_str[id_str_size] = '\0';
111
42deb29f 112 /* Read snapshot name */
7267c094 113 sn->name = g_malloc(name_size + 1);
42deb29f
KW
114 ret = bdrv_pread(bs->file, offset, sn->name, name_size);
115 if (ret < 0) {
c142442b 116 goto fail;
42deb29f 117 }
c142442b
KW
118 offset += name_size;
119 sn->name[name_size] = '\0';
5dae6e30
KW
120
121 if (offset - s->snapshots_offset > QCOW_MAX_SNAPSHOTS_SIZE) {
122 ret = -EFBIG;
123 goto fail;
124 }
c142442b 125 }
42deb29f 126
5dae6e30 127 assert(offset - s->snapshots_offset <= INT_MAX);
c142442b
KW
128 s->snapshots_size = offset - s->snapshots_offset;
129 return 0;
42deb29f
KW
130
131fail:
ed6ccf0f 132 qcow2_free_snapshots(bs);
42deb29f 133 return ret;
c142442b
KW
134}
135
136/* add at the end of the file a new list of snapshots */
7c80ab3f 137static int qcow2_write_snapshots(BlockDriverState *bs)
c142442b 138{
ff99129a 139 BDRVQcow2State *s = bs->opaque;
c142442b
KW
140 QCowSnapshot *sn;
141 QCowSnapshotHeader h;
c2c9a466 142 QCowSnapshotExtraData extra;
c142442b 143 int i, name_size, id_str_size, snapshots_size;
d69969c4
KW
144 struct {
145 uint32_t nb_snapshots;
146 uint64_t snapshots_offset;
147 } QEMU_PACKED header_data;
5dae6e30 148 int64_t offset, snapshots_offset = 0;
07fd8779 149 int ret;
c142442b
KW
150
151 /* compute the size of the snapshots */
152 offset = 0;
153 for(i = 0; i < s->nb_snapshots; i++) {
154 sn = s->snapshots + i;
155 offset = align_offset(offset, 8);
156 offset += sizeof(h);
c2c9a466 157 offset += sizeof(extra);
c142442b
KW
158 offset += strlen(sn->id_str);
159 offset += strlen(sn->name);
5dae6e30
KW
160
161 if (offset > QCOW_MAX_SNAPSHOTS_SIZE) {
162 ret = -EFBIG;
163 goto fail;
164 }
c142442b 165 }
5dae6e30
KW
166
167 assert(offset <= INT_MAX);
c142442b
KW
168 snapshots_size = offset;
169
07fd8779 170 /* Allocate space for the new snapshot list */
ed6ccf0f 171 snapshots_offset = qcow2_alloc_clusters(bs, snapshots_size);
c142442b 172 offset = snapshots_offset;
5d757b56 173 if (offset < 0) {
37d41f0a
HR
174 ret = offset;
175 goto fail;
5d757b56 176 }
f6977f15
SH
177 ret = bdrv_flush(bs);
178 if (ret < 0) {
37d41f0a 179 goto fail;
f6977f15 180 }
c142442b 181
cf93980e
HR
182 /* The snapshot list position has not yet been updated, so these clusters
183 * must indeed be completely free */
231bb267 184 ret = qcow2_pre_write_overlap_check(bs, 0, offset, snapshots_size);
cf93980e 185 if (ret < 0) {
37d41f0a 186 goto fail;
cf93980e
HR
187 }
188
189
07fd8779 190 /* Write all snapshots to the new list */
c142442b
KW
191 for(i = 0; i < s->nb_snapshots; i++) {
192 sn = s->snapshots + i;
193 memset(&h, 0, sizeof(h));
194 h.l1_table_offset = cpu_to_be64(sn->l1_table_offset);
195 h.l1_size = cpu_to_be32(sn->l1_size);
c2c9a466
KW
196 /* If it doesn't fit in 32 bit, older implementations should treat it
197 * as a disk-only snapshot rather than truncate the VM state */
198 if (sn->vm_state_size <= 0xffffffff) {
199 h.vm_state_size = cpu_to_be32(sn->vm_state_size);
200 }
c142442b
KW
201 h.date_sec = cpu_to_be32(sn->date_sec);
202 h.date_nsec = cpu_to_be32(sn->date_nsec);
203 h.vm_clock_nsec = cpu_to_be64(sn->vm_clock_nsec);
c2c9a466
KW
204 h.extra_data_size = cpu_to_be32(sizeof(extra));
205
206 memset(&extra, 0, sizeof(extra));
207 extra.vm_state_size_large = cpu_to_be64(sn->vm_state_size);
90b27759 208 extra.disk_size = cpu_to_be64(sn->disk_size);
c142442b
KW
209
210 id_str_size = strlen(sn->id_str);
211 name_size = strlen(sn->name);
88fb1535 212 assert(id_str_size <= UINT16_MAX && name_size <= UINT16_MAX);
c142442b
KW
213 h.id_str_size = cpu_to_be16(id_str_size);
214 h.name_size = cpu_to_be16(name_size);
215 offset = align_offset(offset, 8);
07fd8779
KW
216
217 ret = bdrv_pwrite(bs->file, offset, &h, sizeof(h));
218 if (ret < 0) {
c142442b 219 goto fail;
07fd8779 220 }
c142442b 221 offset += sizeof(h);
07fd8779 222
c2c9a466
KW
223 ret = bdrv_pwrite(bs->file, offset, &extra, sizeof(extra));
224 if (ret < 0) {
225 goto fail;
226 }
227 offset += sizeof(extra);
228
07fd8779
KW
229 ret = bdrv_pwrite(bs->file, offset, sn->id_str, id_str_size);
230 if (ret < 0) {
c142442b 231 goto fail;
07fd8779 232 }
c142442b 233 offset += id_str_size;
07fd8779
KW
234
235 ret = bdrv_pwrite(bs->file, offset, sn->name, name_size);
236 if (ret < 0) {
c142442b 237 goto fail;
07fd8779 238 }
c142442b
KW
239 offset += name_size;
240 }
241
07fd8779
KW
242 /*
243 * Update the header to point to the new snapshot table. This requires the
244 * new table and its refcounts to be stable on disk.
07fd8779
KW
245 */
246 ret = bdrv_flush(bs);
247 if (ret < 0) {
248 goto fail;
249 }
250
d69969c4
KW
251 QEMU_BUILD_BUG_ON(offsetof(QCowHeader, snapshots_offset) !=
252 offsetof(QCowHeader, nb_snapshots) + sizeof(header_data.nb_snapshots));
253
254 header_data.nb_snapshots = cpu_to_be32(s->nb_snapshots);
255 header_data.snapshots_offset = cpu_to_be64(snapshots_offset);
07fd8779 256
07fd8779 257 ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, nb_snapshots),
d69969c4 258 &header_data, sizeof(header_data));
07fd8779 259 if (ret < 0) {
c142442b 260 goto fail;
07fd8779 261 }
c142442b
KW
262
263 /* free the old snapshot table */
6cfcb9b8
KW
264 qcow2_free_clusters(bs, s->snapshots_offset, s->snapshots_size,
265 QCOW2_DISCARD_SNAPSHOT);
c142442b
KW
266 s->snapshots_offset = snapshots_offset;
267 s->snapshots_size = snapshots_size;
268 return 0;
07fd8779
KW
269
270fail:
9186ad96
HR
271 if (snapshots_offset > 0) {
272 qcow2_free_clusters(bs, snapshots_offset, snapshots_size,
273 QCOW2_DISCARD_ALWAYS);
274 }
07fd8779 275 return ret;
c142442b
KW
276}
277
278static void find_new_snapshot_id(BlockDriverState *bs,
279 char *id_str, int id_str_size)
280{
ff99129a 281 BDRVQcow2State *s = bs->opaque;
c142442b 282 QCowSnapshot *sn;
00c49b21
HR
283 int i;
284 unsigned long id, id_max = 0;
c142442b
KW
285
286 for(i = 0; i < s->nb_snapshots; i++) {
287 sn = s->snapshots + i;
288 id = strtoul(sn->id_str, NULL, 10);
289 if (id > id_max)
290 id_max = id;
291 }
00c49b21 292 snprintf(id_str, id_str_size, "%lu", id_max + 1);
c142442b
KW
293}
294
a89d89d3
WX
295static int find_snapshot_by_id_and_name(BlockDriverState *bs,
296 const char *id,
297 const char *name)
c142442b 298{
ff99129a 299 BDRVQcow2State *s = bs->opaque;
c142442b
KW
300 int i;
301
a89d89d3
WX
302 if (id && name) {
303 for (i = 0; i < s->nb_snapshots; i++) {
304 if (!strcmp(s->snapshots[i].id_str, id) &&
305 !strcmp(s->snapshots[i].name, name)) {
306 return i;
307 }
308 }
309 } else if (id) {
310 for (i = 0; i < s->nb_snapshots; i++) {
311 if (!strcmp(s->snapshots[i].id_str, id)) {
312 return i;
313 }
314 }
315 } else if (name) {
316 for (i = 0; i < s->nb_snapshots; i++) {
317 if (!strcmp(s->snapshots[i].name, name)) {
318 return i;
319 }
320 }
c142442b 321 }
a89d89d3 322
c142442b
KW
323 return -1;
324}
325
a89d89d3
WX
326static int find_snapshot_by_id_or_name(BlockDriverState *bs,
327 const char *id_or_name)
c142442b 328{
a89d89d3 329 int ret;
c142442b 330
a89d89d3
WX
331 ret = find_snapshot_by_id_and_name(bs, id_or_name, NULL);
332 if (ret >= 0) {
c142442b 333 return ret;
c142442b 334 }
a89d89d3 335 return find_snapshot_by_id_and_name(bs, NULL, id_or_name);
c142442b
KW
336}
337
338/* if no id is provided, a new one is constructed */
ed6ccf0f 339int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
c142442b 340{
ff99129a 341 BDRVQcow2State *s = bs->opaque;
d1ea98d5
KW
342 QCowSnapshot *new_snapshot_list = NULL;
343 QCowSnapshot *old_snapshot_list = NULL;
344 QCowSnapshot sn1, *sn = &sn1;
c142442b
KW
345 int i, ret;
346 uint64_t *l1_table = NULL;
5d757b56 347 int64_t l1_table_offset;
c142442b 348
ce48f2f4
KW
349 if (s->nb_snapshots >= QCOW_MAX_SNAPSHOTS) {
350 return -EFBIG;
351 }
352
c142442b
KW
353 memset(sn, 0, sizeof(*sn));
354
407bc150
YW
355 /* Generate an ID */
356 find_new_snapshot_id(bs, sn_info->id_str, sizeof(sn_info->id_str));
c142442b 357
03343166 358 /* Check that the ID is unique */
a89d89d3 359 if (find_snapshot_by_id_and_name(bs, sn_info->id_str, NULL) >= 0) {
647cc472 360 return -EEXIST;
03343166 361 }
c142442b 362
03343166 363 /* Populate sn with passed data */
7267c094 364 sn->id_str = g_strdup(sn_info->id_str);
7267c094 365 sn->name = g_strdup(sn_info->name);
03343166 366
90b27759 367 sn->disk_size = bs->total_sectors * BDRV_SECTOR_SIZE;
c142442b
KW
368 sn->vm_state_size = sn_info->vm_state_size;
369 sn->date_sec = sn_info->date_sec;
370 sn->date_nsec = sn_info->date_nsec;
371 sn->vm_clock_nsec = sn_info->vm_clock_nsec;
372
03343166 373 /* Allocate the L1 table of the snapshot and copy the current one there. */
5d757b56
KW
374 l1_table_offset = qcow2_alloc_clusters(bs, s->l1_size * sizeof(uint64_t));
375 if (l1_table_offset < 0) {
d1ea98d5 376 ret = l1_table_offset;
5d757b56
KW
377 goto fail;
378 }
379
380 sn->l1_table_offset = l1_table_offset;
c142442b
KW
381 sn->l1_size = s->l1_size;
382
5839e53b 383 l1_table = g_try_new(uint64_t, s->l1_size);
de82815d
KW
384 if (s->l1_size && l1_table == NULL) {
385 ret = -ENOMEM;
386 goto fail;
387 }
388
c142442b
KW
389 for(i = 0; i < s->l1_size; i++) {
390 l1_table[i] = cpu_to_be64(s->l1_table[i]);
391 }
d1ea98d5 392
231bb267
HR
393 ret = qcow2_pre_write_overlap_check(bs, 0, sn->l1_table_offset,
394 s->l1_size * sizeof(uint64_t));
cf93980e
HR
395 if (ret < 0) {
396 goto fail;
397 }
398
d1ea98d5
KW
399 ret = bdrv_pwrite(bs->file, sn->l1_table_offset, l1_table,
400 s->l1_size * sizeof(uint64_t));
401 if (ret < 0) {
c142442b 402 goto fail;
d1ea98d5
KW
403 }
404
7267c094 405 g_free(l1_table);
c142442b
KW
406 l1_table = NULL;
407
d1ea98d5
KW
408 /*
409 * Increase the refcounts of all clusters and make sure everything is
410 * stable on disk before updating the snapshot table to contain a pointer
411 * to the new L1 table.
412 */
413 ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1);
414 if (ret < 0) {
415 goto fail;
416 }
417
d1ea98d5 418 /* Append the new snapshot to the snapshot list */
5839e53b 419 new_snapshot_list = g_new(QCowSnapshot, s->nb_snapshots + 1);
c142442b 420 if (s->snapshots) {
d1ea98d5
KW
421 memcpy(new_snapshot_list, s->snapshots,
422 s->nb_snapshots * sizeof(QCowSnapshot));
423 old_snapshot_list = s->snapshots;
c142442b 424 }
d1ea98d5 425 s->snapshots = new_snapshot_list;
c142442b
KW
426 s->snapshots[s->nb_snapshots++] = *sn;
427
d1ea98d5
KW
428 ret = qcow2_write_snapshots(bs);
429 if (ret < 0) {
430 g_free(s->snapshots);
431 s->snapshots = old_snapshot_list;
84757f7e 432 s->nb_snapshots--;
c142442b 433 goto fail;
d1ea98d5
KW
434 }
435
436 g_free(old_snapshot_list);
437
1ebf561c
KW
438 /* The VM state isn't needed any more in the active L1 table; in fact, it
439 * hurts by causing expensive COW for the next snapshot. */
440 qcow2_discard_clusters(bs, qcow2_vm_state_offset(s),
441 align_offset(sn->vm_state_size, s->cluster_size)
442 >> BDRV_SECTOR_BITS,
808c4b6f 443 QCOW2_DISCARD_NEVER, false);
1ebf561c 444
c142442b 445#ifdef DEBUG_ALLOC
6cbc3031
PH
446 {
447 BdrvCheckResult result = {0};
b35278f7 448 qcow2_check_refcounts(bs, &result, 0);
6cbc3031 449 }
c142442b
KW
450#endif
451 return 0;
03343166
KW
452
453fail:
454 g_free(sn->id_str);
7267c094
AL
455 g_free(sn->name);
456 g_free(l1_table);
d1ea98d5
KW
457
458 return ret;
c142442b
KW
459}
460
461/* copy the snapshot 'snapshot_name' into the current disk image */
ed6ccf0f 462int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)
c142442b 463{
ff99129a 464 BDRVQcow2State *s = bs->opaque;
c142442b 465 QCowSnapshot *sn;
35d7ace7
KW
466 int i, snapshot_index;
467 int cur_l1_bytes, sn_l1_bytes;
589f284b 468 int ret;
43a0cac4 469 uint64_t *sn_l1_table = NULL;
c142442b 470
589f284b 471 /* Search the snapshot */
c142442b 472 snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id);
589f284b 473 if (snapshot_index < 0) {
c142442b 474 return -ENOENT;
589f284b 475 }
c142442b
KW
476 sn = &s->snapshots[snapshot_index];
477
90b27759
KW
478 if (sn->disk_size != bs->total_sectors * BDRV_SECTOR_SIZE) {
479 error_report("qcow2: Loading snapshots with different disk "
480 "size is not implemented");
481 ret = -ENOTSUP;
482 goto fail;
483 }
484
589f284b
KW
485 /*
486 * Make sure that the current L1 table is big enough to contain the whole
487 * L1 table of the snapshot. If the snapshot L1 table is smaller, the
488 * current one must be padded with zeros.
489 */
490 ret = qcow2_grow_l1_table(bs, sn->l1_size, true);
491 if (ret < 0) {
c142442b 492 goto fail;
589f284b 493 }
c142442b 494
35d7ace7
KW
495 cur_l1_bytes = s->l1_size * sizeof(uint64_t);
496 sn_l1_bytes = sn->l1_size * sizeof(uint64_t);
497
589f284b
KW
498 /*
499 * Copy the snapshot L1 table to the current L1 table.
500 *
501 * Before overwriting the old current L1 table on disk, make sure to
502 * increase all refcounts for the clusters referenced by the new one.
43a0cac4
KW
503 * Decrease the refcount referenced by the old one only when the L1
504 * table is overwritten.
589f284b 505 */
de82815d
KW
506 sn_l1_table = g_try_malloc0(cur_l1_bytes);
507 if (cur_l1_bytes && sn_l1_table == NULL) {
508 ret = -ENOMEM;
509 goto fail;
510 }
43a0cac4
KW
511
512 ret = bdrv_pread(bs->file, sn->l1_table_offset, sn_l1_table, sn_l1_bytes);
513 if (ret < 0) {
514 goto fail;
515 }
516
517 ret = qcow2_update_snapshot_refcount(bs, sn->l1_table_offset,
518 sn->l1_size, 1);
589f284b 519 if (ret < 0) {
c142442b 520 goto fail;
589f284b
KW
521 }
522
231bb267
HR
523 ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L1,
524 s->l1_table_offset, cur_l1_bytes);
cf93980e
HR
525 if (ret < 0) {
526 goto fail;
527 }
528
43a0cac4 529 ret = bdrv_pwrite_sync(bs->file, s->l1_table_offset, sn_l1_table,
589f284b
KW
530 cur_l1_bytes);
531 if (ret < 0) {
c142442b 532 goto fail;
589f284b
KW
533 }
534
43a0cac4
KW
535 /*
536 * Decrease refcount of clusters of current L1 table.
537 *
538 * At this point, the in-memory s->l1_table points to the old L1 table,
539 * whereas on disk we already have the new one.
540 *
541 * qcow2_update_snapshot_refcount special cases the current L1 table to use
542 * the in-memory data instead of really using the offset to load a new one,
543 * which is why this works.
544 */
545 ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset,
546 s->l1_size, -1);
547
548 /*
549 * Now update the in-memory L1 table to be in sync with the on-disk one. We
550 * need to do this even if updating refcounts failed.
551 */
c142442b 552 for(i = 0;i < s->l1_size; i++) {
43a0cac4 553 s->l1_table[i] = be64_to_cpu(sn_l1_table[i]);
c142442b
KW
554 }
555
43a0cac4
KW
556 if (ret < 0) {
557 goto fail;
558 }
559
560 g_free(sn_l1_table);
561 sn_l1_table = NULL;
562
563 /*
564 * Update QCOW_OFLAG_COPIED in the active L1 table (it may have changed
565 * when we decreased the refcount of the old snapshot.
566 */
567 ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0);
589f284b 568 if (ret < 0) {
c142442b 569 goto fail;
589f284b 570 }
c142442b
KW
571
572#ifdef DEBUG_ALLOC
6cbc3031
PH
573 {
574 BdrvCheckResult result = {0};
b35278f7 575 qcow2_check_refcounts(bs, &result, 0);
6cbc3031 576 }
c142442b
KW
577#endif
578 return 0;
589f284b
KW
579
580fail:
43a0cac4 581 g_free(sn_l1_table);
589f284b 582 return ret;
c142442b
KW
583}
584
a89d89d3
WX
585int qcow2_snapshot_delete(BlockDriverState *bs,
586 const char *snapshot_id,
587 const char *name,
588 Error **errp)
c142442b 589{
ff99129a 590 BDRVQcow2State *s = bs->opaque;
9a476780 591 QCowSnapshot sn;
c142442b
KW
592 int snapshot_index, ret;
593
9a476780 594 /* Search the snapshot */
a89d89d3 595 snapshot_index = find_snapshot_by_id_and_name(bs, snapshot_id, name);
9a476780 596 if (snapshot_index < 0) {
a89d89d3 597 error_setg(errp, "Can't find the snapshot");
c142442b 598 return -ENOENT;
9a476780
KW
599 }
600 sn = s->snapshots[snapshot_index];
c142442b 601
9a476780
KW
602 /* Remove it from the snapshot list */
603 memmove(s->snapshots + snapshot_index,
604 s->snapshots + snapshot_index + 1,
605 (s->nb_snapshots - snapshot_index - 1) * sizeof(sn));
606 s->nb_snapshots--;
607 ret = qcow2_write_snapshots(bs);
608 if (ret < 0) {
39a611a3
JC
609 error_setg_errno(errp, -ret,
610 "Failed to remove snapshot from snapshot list");
c142442b 611 return ret;
9a476780
KW
612 }
613
614 /*
615 * The snapshot is now unused, clean up. If we fail after this point, we
616 * won't recover but just leak clusters.
617 */
618 g_free(sn.id_str);
619 g_free(sn.name);
620
621 /*
622 * Now decrease the refcounts of clusters referenced by the snapshot and
623 * free the L1 table.
624 */
625 ret = qcow2_update_snapshot_refcount(bs, sn.l1_table_offset,
626 sn.l1_size, -1);
627 if (ret < 0) {
39a611a3 628 error_setg_errno(errp, -ret, "Failed to free the cluster and L1 table");
c142442b 629 return ret;
9a476780 630 }
6cfcb9b8
KW
631 qcow2_free_clusters(bs, sn.l1_table_offset, sn.l1_size * sizeof(uint64_t),
632 QCOW2_DISCARD_SNAPSHOT);
c142442b 633
9a476780
KW
634 /* must update the copied flag on the current cluster offsets */
635 ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0);
c142442b 636 if (ret < 0) {
39a611a3
JC
637 error_setg_errno(errp, -ret,
638 "Failed to update snapshot status in disk");
c142442b
KW
639 return ret;
640 }
9a476780 641
c142442b 642#ifdef DEBUG_ALLOC
6cbc3031
PH
643 {
644 BdrvCheckResult result = {0};
b35278f7 645 qcow2_check_refcounts(bs, &result, 0);
6cbc3031 646 }
c142442b
KW
647#endif
648 return 0;
649}
650
ed6ccf0f 651int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
c142442b 652{
ff99129a 653 BDRVQcow2State *s = bs->opaque;
c142442b
KW
654 QEMUSnapshotInfo *sn_tab, *sn_info;
655 QCowSnapshot *sn;
656 int i;
657
658 if (!s->nb_snapshots) {
659 *psn_tab = NULL;
660 return s->nb_snapshots;
661 }
662
5839e53b 663 sn_tab = g_new0(QEMUSnapshotInfo, s->nb_snapshots);
c142442b
KW
664 for(i = 0; i < s->nb_snapshots; i++) {
665 sn_info = sn_tab + i;
666 sn = s->snapshots + i;
667 pstrcpy(sn_info->id_str, sizeof(sn_info->id_str),
668 sn->id_str);
669 pstrcpy(sn_info->name, sizeof(sn_info->name),
670 sn->name);
671 sn_info->vm_state_size = sn->vm_state_size;
672 sn_info->date_sec = sn->date_sec;
673 sn_info->date_nsec = sn->date_nsec;
674 sn_info->vm_clock_nsec = sn->vm_clock_nsec;
675 }
676 *psn_tab = sn_tab;
677 return s->nb_snapshots;
678}
679
7b4c4781
WX
680int qcow2_snapshot_load_tmp(BlockDriverState *bs,
681 const char *snapshot_id,
682 const char *name,
683 Error **errp)
51ef6727 684{
e3f652b3 685 int i, snapshot_index;
ff99129a 686 BDRVQcow2State *s = bs->opaque;
51ef6727 687 QCowSnapshot *sn;
e3f652b3
KW
688 uint64_t *new_l1_table;
689 int new_l1_bytes;
690 int ret;
51ef6727 691
e3f652b3
KW
692 assert(bs->read_only);
693
694 /* Search the snapshot */
7b4c4781 695 snapshot_index = find_snapshot_by_id_and_name(bs, snapshot_id, name);
51ef6727 696 if (snapshot_index < 0) {
7b4c4781
WX
697 error_setg(errp,
698 "Can't find snapshot");
51ef6727 699 return -ENOENT;
700 }
51ef6727 701 sn = &s->snapshots[snapshot_index];
51ef6727 702
e3f652b3 703 /* Allocate and read in the snapshot's L1 table */
87b86e7e 704 if (sn->l1_size > QCOW_MAX_L1_SIZE / sizeof(uint64_t)) {
6a83f8b5
KW
705 error_setg(errp, "Snapshot L1 table too large");
706 return -EFBIG;
707 }
c05e4667 708 new_l1_bytes = sn->l1_size * sizeof(uint64_t);
de82815d
KW
709 new_l1_table = qemu_try_blockalign(bs->file,
710 align_offset(new_l1_bytes, 512));
711 if (new_l1_table == NULL) {
712 return -ENOMEM;
713 }
51ef6727 714
e3f652b3
KW
715 ret = bdrv_pread(bs->file, sn->l1_table_offset, new_l1_table, new_l1_bytes);
716 if (ret < 0) {
7b4c4781 717 error_setg(errp, "Failed to read l1 table for snapshot");
de82815d 718 qemu_vfree(new_l1_table);
e3f652b3 719 return ret;
51ef6727 720 }
721
e3f652b3 722 /* Switch the L1 table */
de82815d 723 qemu_vfree(s->l1_table);
e3f652b3
KW
724
725 s->l1_size = sn->l1_size;
726 s->l1_table_offset = sn->l1_table_offset;
727 s->l1_table = new_l1_table;
728
51ef6727 729 for(i = 0;i < s->l1_size; i++) {
730 be64_to_cpus(&s->l1_table[i]);
731 }
e3f652b3 732
51ef6727 733 return 0;
734}