]> git.proxmox.com Git - mirror_qemu.git/blame - block/qcow2-snapshot.c
net: Forbid dealing with packets when VM is not running
[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
KW
27#include "block/qcow2.h"
28
ed6ccf0f 29void qcow2_free_snapshots(BlockDriverState *bs)
c142442b
KW
30{
31 BDRVQcowState *s = bs->opaque;
32 int i;
33
34 for(i = 0; i < s->nb_snapshots; i++) {
7267c094
AL
35 g_free(s->snapshots[i].name);
36 g_free(s->snapshots[i].id_str);
c142442b 37 }
7267c094 38 g_free(s->snapshots);
c142442b
KW
39 s->snapshots = NULL;
40 s->nb_snapshots = 0;
41}
42
ed6ccf0f 43int qcow2_read_snapshots(BlockDriverState *bs)
c142442b
KW
44{
45 BDRVQcowState *s = bs->opaque;
46 QCowSnapshotHeader h;
c2c9a466 47 QCowSnapshotExtraData extra;
c142442b
KW
48 QCowSnapshot *sn;
49 int i, id_str_size, name_size;
50 int64_t offset;
51 uint32_t extra_data_size;
42deb29f 52 int ret;
c142442b
KW
53
54 if (!s->nb_snapshots) {
55 s->snapshots = NULL;
56 s->snapshots_size = 0;
57 return 0;
58 }
59
60 offset = s->snapshots_offset;
5839e53b 61 s->snapshots = g_new0(QCowSnapshot, s->nb_snapshots);
42deb29f 62
c142442b 63 for(i = 0; i < s->nb_snapshots; i++) {
42deb29f 64 /* Read statically sized part of the snapshot header */
c142442b 65 offset = align_offset(offset, 8);
42deb29f
KW
66 ret = bdrv_pread(bs->file, offset, &h, sizeof(h));
67 if (ret < 0) {
c142442b 68 goto fail;
42deb29f
KW
69 }
70
c142442b
KW
71 offset += sizeof(h);
72 sn = s->snapshots + i;
73 sn->l1_table_offset = be64_to_cpu(h.l1_table_offset);
74 sn->l1_size = be32_to_cpu(h.l1_size);
75 sn->vm_state_size = be32_to_cpu(h.vm_state_size);
76 sn->date_sec = be32_to_cpu(h.date_sec);
77 sn->date_nsec = be32_to_cpu(h.date_nsec);
78 sn->vm_clock_nsec = be64_to_cpu(h.vm_clock_nsec);
79 extra_data_size = be32_to_cpu(h.extra_data_size);
80
81 id_str_size = be16_to_cpu(h.id_str_size);
82 name_size = be16_to_cpu(h.name_size);
83
c2c9a466
KW
84 /* Read extra data */
85 ret = bdrv_pread(bs->file, offset, &extra,
86 MIN(sizeof(extra), extra_data_size));
87 if (ret < 0) {
88 goto fail;
89 }
c142442b
KW
90 offset += extra_data_size;
91
c2c9a466
KW
92 if (extra_data_size >= 8) {
93 sn->vm_state_size = be64_to_cpu(extra.vm_state_size_large);
94 }
95
90b27759
KW
96 if (extra_data_size >= 16) {
97 sn->disk_size = be64_to_cpu(extra.disk_size);
98 } else {
99 sn->disk_size = bs->total_sectors * BDRV_SECTOR_SIZE;
100 }
101
42deb29f 102 /* Read snapshot ID */
7267c094 103 sn->id_str = g_malloc(id_str_size + 1);
42deb29f
KW
104 ret = bdrv_pread(bs->file, offset, sn->id_str, id_str_size);
105 if (ret < 0) {
c142442b 106 goto fail;
42deb29f 107 }
c142442b
KW
108 offset += id_str_size;
109 sn->id_str[id_str_size] = '\0';
110
42deb29f 111 /* Read snapshot name */
7267c094 112 sn->name = g_malloc(name_size + 1);
42deb29f
KW
113 ret = bdrv_pread(bs->file, offset, sn->name, name_size);
114 if (ret < 0) {
c142442b 115 goto fail;
42deb29f 116 }
c142442b
KW
117 offset += name_size;
118 sn->name[name_size] = '\0';
5dae6e30
KW
119
120 if (offset - s->snapshots_offset > QCOW_MAX_SNAPSHOTS_SIZE) {
121 ret = -EFBIG;
122 goto fail;
123 }
c142442b 124 }
42deb29f 125
5dae6e30 126 assert(offset - s->snapshots_offset <= INT_MAX);
c142442b
KW
127 s->snapshots_size = offset - s->snapshots_offset;
128 return 0;
42deb29f
KW
129
130fail:
ed6ccf0f 131 qcow2_free_snapshots(bs);
42deb29f 132 return ret;
c142442b
KW
133}
134
135/* add at the end of the file a new list of snapshots */
7c80ab3f 136static int qcow2_write_snapshots(BlockDriverState *bs)
c142442b
KW
137{
138 BDRVQcowState *s = bs->opaque;
139 QCowSnapshot *sn;
140 QCowSnapshotHeader h;
c2c9a466 141 QCowSnapshotExtraData extra;
c142442b 142 int i, name_size, id_str_size, snapshots_size;
d69969c4
KW
143 struct {
144 uint32_t nb_snapshots;
145 uint64_t snapshots_offset;
146 } QEMU_PACKED header_data;
5dae6e30 147 int64_t offset, snapshots_offset = 0;
07fd8779 148 int ret;
c142442b
KW
149
150 /* compute the size of the snapshots */
151 offset = 0;
152 for(i = 0; i < s->nb_snapshots; i++) {
153 sn = s->snapshots + i;
154 offset = align_offset(offset, 8);
155 offset += sizeof(h);
c2c9a466 156 offset += sizeof(extra);
c142442b
KW
157 offset += strlen(sn->id_str);
158 offset += strlen(sn->name);
5dae6e30
KW
159
160 if (offset > QCOW_MAX_SNAPSHOTS_SIZE) {
161 ret = -EFBIG;
162 goto fail;
163 }
c142442b 164 }
5dae6e30
KW
165
166 assert(offset <= INT_MAX);
c142442b
KW
167 snapshots_size = offset;
168
07fd8779 169 /* Allocate space for the new snapshot list */
ed6ccf0f 170 snapshots_offset = qcow2_alloc_clusters(bs, snapshots_size);
c142442b 171 offset = snapshots_offset;
5d757b56 172 if (offset < 0) {
37d41f0a
HR
173 ret = offset;
174 goto fail;
5d757b56 175 }
f6977f15
SH
176 ret = bdrv_flush(bs);
177 if (ret < 0) {
37d41f0a 178 goto fail;
f6977f15 179 }
c142442b 180
cf93980e
HR
181 /* The snapshot list position has not yet been updated, so these clusters
182 * must indeed be completely free */
231bb267 183 ret = qcow2_pre_write_overlap_check(bs, 0, offset, snapshots_size);
cf93980e 184 if (ret < 0) {
37d41f0a 185 goto fail;
cf93980e
HR
186 }
187
188
07fd8779 189 /* Write all snapshots to the new list */
c142442b
KW
190 for(i = 0; i < s->nb_snapshots; i++) {
191 sn = s->snapshots + i;
192 memset(&h, 0, sizeof(h));
193 h.l1_table_offset = cpu_to_be64(sn->l1_table_offset);
194 h.l1_size = cpu_to_be32(sn->l1_size);
c2c9a466
KW
195 /* If it doesn't fit in 32 bit, older implementations should treat it
196 * as a disk-only snapshot rather than truncate the VM state */
197 if (sn->vm_state_size <= 0xffffffff) {
198 h.vm_state_size = cpu_to_be32(sn->vm_state_size);
199 }
c142442b
KW
200 h.date_sec = cpu_to_be32(sn->date_sec);
201 h.date_nsec = cpu_to_be32(sn->date_nsec);
202 h.vm_clock_nsec = cpu_to_be64(sn->vm_clock_nsec);
c2c9a466
KW
203 h.extra_data_size = cpu_to_be32(sizeof(extra));
204
205 memset(&extra, 0, sizeof(extra));
206 extra.vm_state_size_large = cpu_to_be64(sn->vm_state_size);
90b27759 207 extra.disk_size = cpu_to_be64(sn->disk_size);
c142442b
KW
208
209 id_str_size = strlen(sn->id_str);
210 name_size = strlen(sn->name);
88fb1535 211 assert(id_str_size <= UINT16_MAX && name_size <= UINT16_MAX);
c142442b
KW
212 h.id_str_size = cpu_to_be16(id_str_size);
213 h.name_size = cpu_to_be16(name_size);
214 offset = align_offset(offset, 8);
07fd8779
KW
215
216 ret = bdrv_pwrite(bs->file, offset, &h, sizeof(h));
217 if (ret < 0) {
c142442b 218 goto fail;
07fd8779 219 }
c142442b 220 offset += sizeof(h);
07fd8779 221
c2c9a466
KW
222 ret = bdrv_pwrite(bs->file, offset, &extra, sizeof(extra));
223 if (ret < 0) {
224 goto fail;
225 }
226 offset += sizeof(extra);
227
07fd8779
KW
228 ret = bdrv_pwrite(bs->file, offset, sn->id_str, id_str_size);
229 if (ret < 0) {
c142442b 230 goto fail;
07fd8779 231 }
c142442b 232 offset += id_str_size;
07fd8779
KW
233
234 ret = bdrv_pwrite(bs->file, offset, sn->name, name_size);
235 if (ret < 0) {
c142442b 236 goto fail;
07fd8779 237 }
c142442b
KW
238 offset += name_size;
239 }
240
07fd8779
KW
241 /*
242 * Update the header to point to the new snapshot table. This requires the
243 * new table and its refcounts to be stable on disk.
07fd8779
KW
244 */
245 ret = bdrv_flush(bs);
246 if (ret < 0) {
247 goto fail;
248 }
249
d69969c4
KW
250 QEMU_BUILD_BUG_ON(offsetof(QCowHeader, snapshots_offset) !=
251 offsetof(QCowHeader, nb_snapshots) + sizeof(header_data.nb_snapshots));
252
253 header_data.nb_snapshots = cpu_to_be32(s->nb_snapshots);
254 header_data.snapshots_offset = cpu_to_be64(snapshots_offset);
07fd8779 255
07fd8779 256 ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, nb_snapshots),
d69969c4 257 &header_data, sizeof(header_data));
07fd8779 258 if (ret < 0) {
c142442b 259 goto fail;
07fd8779 260 }
c142442b
KW
261
262 /* free the old snapshot table */
6cfcb9b8
KW
263 qcow2_free_clusters(bs, s->snapshots_offset, s->snapshots_size,
264 QCOW2_DISCARD_SNAPSHOT);
c142442b
KW
265 s->snapshots_offset = snapshots_offset;
266 s->snapshots_size = snapshots_size;
267 return 0;
07fd8779
KW
268
269fail:
9186ad96
HR
270 if (snapshots_offset > 0) {
271 qcow2_free_clusters(bs, snapshots_offset, snapshots_size,
272 QCOW2_DISCARD_ALWAYS);
273 }
07fd8779 274 return ret;
c142442b
KW
275}
276
277static void find_new_snapshot_id(BlockDriverState *bs,
278 char *id_str, int id_str_size)
279{
280 BDRVQcowState *s = bs->opaque;
281 QCowSnapshot *sn;
00c49b21
HR
282 int i;
283 unsigned long id, id_max = 0;
c142442b
KW
284
285 for(i = 0; i < s->nb_snapshots; i++) {
286 sn = s->snapshots + i;
287 id = strtoul(sn->id_str, NULL, 10);
288 if (id > id_max)
289 id_max = id;
290 }
00c49b21 291 snprintf(id_str, id_str_size, "%lu", id_max + 1);
c142442b
KW
292}
293
a89d89d3
WX
294static int find_snapshot_by_id_and_name(BlockDriverState *bs,
295 const char *id,
296 const char *name)
c142442b
KW
297{
298 BDRVQcowState *s = bs->opaque;
299 int i;
300
a89d89d3
WX
301 if (id && name) {
302 for (i = 0; i < s->nb_snapshots; i++) {
303 if (!strcmp(s->snapshots[i].id_str, id) &&
304 !strcmp(s->snapshots[i].name, name)) {
305 return i;
306 }
307 }
308 } else if (id) {
309 for (i = 0; i < s->nb_snapshots; i++) {
310 if (!strcmp(s->snapshots[i].id_str, id)) {
311 return i;
312 }
313 }
314 } else if (name) {
315 for (i = 0; i < s->nb_snapshots; i++) {
316 if (!strcmp(s->snapshots[i].name, name)) {
317 return i;
318 }
319 }
c142442b 320 }
a89d89d3 321
c142442b
KW
322 return -1;
323}
324
a89d89d3
WX
325static int find_snapshot_by_id_or_name(BlockDriverState *bs,
326 const char *id_or_name)
c142442b 327{
a89d89d3 328 int ret;
c142442b 329
a89d89d3
WX
330 ret = find_snapshot_by_id_and_name(bs, id_or_name, NULL);
331 if (ret >= 0) {
c142442b 332 return ret;
c142442b 333 }
a89d89d3 334 return find_snapshot_by_id_and_name(bs, NULL, id_or_name);
c142442b
KW
335}
336
337/* if no id is provided, a new one is constructed */
ed6ccf0f 338int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
c142442b
KW
339{
340 BDRVQcowState *s = bs->opaque;
d1ea98d5
KW
341 QCowSnapshot *new_snapshot_list = NULL;
342 QCowSnapshot *old_snapshot_list = NULL;
343 QCowSnapshot sn1, *sn = &sn1;
c142442b
KW
344 int i, ret;
345 uint64_t *l1_table = NULL;
5d757b56 346 int64_t l1_table_offset;
c142442b 347
ce48f2f4
KW
348 if (s->nb_snapshots >= QCOW_MAX_SNAPSHOTS) {
349 return -EFBIG;
350 }
351
c142442b
KW
352 memset(sn, 0, sizeof(*sn));
353
03343166 354 /* Generate an ID if it wasn't passed */
c142442b 355 if (sn_info->id_str[0] == '\0') {
c142442b
KW
356 find_new_snapshot_id(bs, sn_info->id_str, sizeof(sn_info->id_str));
357 }
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
d1ea98d5
KW
400 ret = bdrv_pwrite(bs->file, sn->l1_table_offset, l1_table,
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,
444 QCOW2_DISCARD_NEVER);
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
KW
464{
465 BDRVQcowState *s = bs->opaque;
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
KW
512
513 ret = bdrv_pread(bs->file, sn->l1_table_offset, sn_l1_table, sn_l1_bytes);
514 if (ret < 0) {
515 goto fail;
516 }
517
518 ret = qcow2_update_snapshot_refcount(bs, sn->l1_table_offset,
519 sn->l1_size, 1);
589f284b 520 if (ret < 0) {
c142442b 521 goto fail;
589f284b
KW
522 }
523
231bb267
HR
524 ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L1,
525 s->l1_table_offset, cur_l1_bytes);
cf93980e
HR
526 if (ret < 0) {
527 goto fail;
528 }
529
43a0cac4 530 ret = bdrv_pwrite_sync(bs->file, s->l1_table_offset, sn_l1_table,
589f284b
KW
531 cur_l1_bytes);
532 if (ret < 0) {
c142442b 533 goto fail;
589f284b
KW
534 }
535
43a0cac4
KW
536 /*
537 * Decrease refcount of clusters of current L1 table.
538 *
539 * At this point, the in-memory s->l1_table points to the old L1 table,
540 * whereas on disk we already have the new one.
541 *
542 * qcow2_update_snapshot_refcount special cases the current L1 table to use
543 * the in-memory data instead of really using the offset to load a new one,
544 * which is why this works.
545 */
546 ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset,
547 s->l1_size, -1);
548
549 /*
550 * Now update the in-memory L1 table to be in sync with the on-disk one. We
551 * need to do this even if updating refcounts failed.
552 */
c142442b 553 for(i = 0;i < s->l1_size; i++) {
43a0cac4 554 s->l1_table[i] = be64_to_cpu(sn_l1_table[i]);
c142442b
KW
555 }
556
43a0cac4
KW
557 if (ret < 0) {
558 goto fail;
559 }
560
561 g_free(sn_l1_table);
562 sn_l1_table = NULL;
563
564 /*
565 * Update QCOW_OFLAG_COPIED in the active L1 table (it may have changed
566 * when we decreased the refcount of the old snapshot.
567 */
568 ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0);
589f284b 569 if (ret < 0) {
c142442b 570 goto fail;
589f284b 571 }
c142442b
KW
572
573#ifdef DEBUG_ALLOC
6cbc3031
PH
574 {
575 BdrvCheckResult result = {0};
b35278f7 576 qcow2_check_refcounts(bs, &result, 0);
6cbc3031 577 }
c142442b
KW
578#endif
579 return 0;
589f284b
KW
580
581fail:
43a0cac4 582 g_free(sn_l1_table);
589f284b 583 return ret;
c142442b
KW
584}
585
a89d89d3
WX
586int qcow2_snapshot_delete(BlockDriverState *bs,
587 const char *snapshot_id,
588 const char *name,
589 Error **errp)
c142442b
KW
590{
591 BDRVQcowState *s = bs->opaque;
9a476780 592 QCowSnapshot sn;
c142442b
KW
593 int snapshot_index, ret;
594
9a476780 595 /* Search the snapshot */
a89d89d3 596 snapshot_index = find_snapshot_by_id_and_name(bs, snapshot_id, name);
9a476780 597 if (snapshot_index < 0) {
a89d89d3 598 error_setg(errp, "Can't find the snapshot");
c142442b 599 return -ENOENT;
9a476780
KW
600 }
601 sn = s->snapshots[snapshot_index];
c142442b 602
9a476780
KW
603 /* Remove it from the snapshot list */
604 memmove(s->snapshots + snapshot_index,
605 s->snapshots + snapshot_index + 1,
606 (s->nb_snapshots - snapshot_index - 1) * sizeof(sn));
607 s->nb_snapshots--;
608 ret = qcow2_write_snapshots(bs);
609 if (ret < 0) {
39a611a3
JC
610 error_setg_errno(errp, -ret,
611 "Failed to remove snapshot from snapshot list");
c142442b 612 return ret;
9a476780
KW
613 }
614
615 /*
616 * The snapshot is now unused, clean up. If we fail after this point, we
617 * won't recover but just leak clusters.
618 */
619 g_free(sn.id_str);
620 g_free(sn.name);
621
622 /*
623 * Now decrease the refcounts of clusters referenced by the snapshot and
624 * free the L1 table.
625 */
626 ret = qcow2_update_snapshot_refcount(bs, sn.l1_table_offset,
627 sn.l1_size, -1);
628 if (ret < 0) {
39a611a3 629 error_setg_errno(errp, -ret, "Failed to free the cluster and L1 table");
c142442b 630 return ret;
9a476780 631 }
6cfcb9b8
KW
632 qcow2_free_clusters(bs, sn.l1_table_offset, sn.l1_size * sizeof(uint64_t),
633 QCOW2_DISCARD_SNAPSHOT);
c142442b 634
9a476780
KW
635 /* must update the copied flag on the current cluster offsets */
636 ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0);
c142442b 637 if (ret < 0) {
39a611a3
JC
638 error_setg_errno(errp, -ret,
639 "Failed to update snapshot status in disk");
c142442b
KW
640 return ret;
641 }
9a476780 642
c142442b 643#ifdef DEBUG_ALLOC
6cbc3031
PH
644 {
645 BdrvCheckResult result = {0};
b35278f7 646 qcow2_check_refcounts(bs, &result, 0);
6cbc3031 647 }
c142442b
KW
648#endif
649 return 0;
650}
651
ed6ccf0f 652int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
c142442b
KW
653{
654 BDRVQcowState *s = bs->opaque;
655 QEMUSnapshotInfo *sn_tab, *sn_info;
656 QCowSnapshot *sn;
657 int i;
658
659 if (!s->nb_snapshots) {
660 *psn_tab = NULL;
661 return s->nb_snapshots;
662 }
663
5839e53b 664 sn_tab = g_new0(QEMUSnapshotInfo, s->nb_snapshots);
c142442b
KW
665 for(i = 0; i < s->nb_snapshots; i++) {
666 sn_info = sn_tab + i;
667 sn = s->snapshots + i;
668 pstrcpy(sn_info->id_str, sizeof(sn_info->id_str),
669 sn->id_str);
670 pstrcpy(sn_info->name, sizeof(sn_info->name),
671 sn->name);
672 sn_info->vm_state_size = sn->vm_state_size;
673 sn_info->date_sec = sn->date_sec;
674 sn_info->date_nsec = sn->date_nsec;
675 sn_info->vm_clock_nsec = sn->vm_clock_nsec;
676 }
677 *psn_tab = sn_tab;
678 return s->nb_snapshots;
679}
680
7b4c4781
WX
681int qcow2_snapshot_load_tmp(BlockDriverState *bs,
682 const char *snapshot_id,
683 const char *name,
684 Error **errp)
51ef6727 685{
e3f652b3 686 int i, snapshot_index;
51ef6727 687 BDRVQcowState *s = bs->opaque;
688 QCowSnapshot *sn;
e3f652b3
KW
689 uint64_t *new_l1_table;
690 int new_l1_bytes;
691 int ret;
51ef6727 692
e3f652b3
KW
693 assert(bs->read_only);
694
695 /* Search the snapshot */
7b4c4781 696 snapshot_index = find_snapshot_by_id_and_name(bs, snapshot_id, name);
51ef6727 697 if (snapshot_index < 0) {
7b4c4781
WX
698 error_setg(errp,
699 "Can't find snapshot");
51ef6727 700 return -ENOENT;
701 }
51ef6727 702 sn = &s->snapshots[snapshot_index];
51ef6727 703
e3f652b3 704 /* Allocate and read in the snapshot's L1 table */
6a83f8b5
KW
705 if (sn->l1_size > QCOW_MAX_L1_SIZE) {
706 error_setg(errp, "Snapshot L1 table too large");
707 return -EFBIG;
708 }
c05e4667 709 new_l1_bytes = sn->l1_size * sizeof(uint64_t);
de82815d
KW
710 new_l1_table = qemu_try_blockalign(bs->file,
711 align_offset(new_l1_bytes, 512));
712 if (new_l1_table == NULL) {
713 return -ENOMEM;
714 }
51ef6727 715
e3f652b3
KW
716 ret = bdrv_pread(bs->file, sn->l1_table_offset, new_l1_table, new_l1_bytes);
717 if (ret < 0) {
7b4c4781 718 error_setg(errp, "Failed to read l1 table for snapshot");
de82815d 719 qemu_vfree(new_l1_table);
e3f652b3 720 return ret;
51ef6727 721 }
722
e3f652b3 723 /* Switch the L1 table */
de82815d 724 qemu_vfree(s->l1_table);
e3f652b3
KW
725
726 s->l1_size = sn->l1_size;
727 s->l1_table_offset = sn->l1_table_offset;
728 s->l1_table = new_l1_table;
729
51ef6727 730 for(i = 0;i < s->l1_size; i++) {
731 be64_to_cpus(&s->l1_table[i]);
732 }
e3f652b3 733
51ef6727 734 return 0;
735}