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