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