]> git.proxmox.com Git - mirror_qemu.git/blame - block/qcow2-snapshot.c
qcow2: Separate qcow2_check_read_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
c142442b
KW
383static void find_new_snapshot_id(BlockDriverState *bs,
384 char *id_str, int id_str_size)
385{
ff99129a 386 BDRVQcow2State *s = bs->opaque;
c142442b 387 QCowSnapshot *sn;
00c49b21
HR
388 int i;
389 unsigned long id, id_max = 0;
c142442b
KW
390
391 for(i = 0; i < s->nb_snapshots; i++) {
392 sn = s->snapshots + i;
393 id = strtoul(sn->id_str, NULL, 10);
394 if (id > id_max)
395 id_max = id;
396 }
00c49b21 397 snprintf(id_str, id_str_size, "%lu", id_max + 1);
c142442b
KW
398}
399
a89d89d3
WX
400static int find_snapshot_by_id_and_name(BlockDriverState *bs,
401 const char *id,
402 const char *name)
c142442b 403{
ff99129a 404 BDRVQcow2State *s = bs->opaque;
c142442b
KW
405 int i;
406
a89d89d3
WX
407 if (id && name) {
408 for (i = 0; i < s->nb_snapshots; i++) {
409 if (!strcmp(s->snapshots[i].id_str, id) &&
410 !strcmp(s->snapshots[i].name, name)) {
411 return i;
412 }
413 }
414 } else if (id) {
415 for (i = 0; i < s->nb_snapshots; i++) {
416 if (!strcmp(s->snapshots[i].id_str, id)) {
417 return i;
418 }
419 }
420 } else if (name) {
421 for (i = 0; i < s->nb_snapshots; i++) {
422 if (!strcmp(s->snapshots[i].name, name)) {
423 return i;
424 }
425 }
c142442b 426 }
a89d89d3 427
c142442b
KW
428 return -1;
429}
430
a89d89d3
WX
431static int find_snapshot_by_id_or_name(BlockDriverState *bs,
432 const char *id_or_name)
c142442b 433{
a89d89d3 434 int ret;
c142442b 435
a89d89d3
WX
436 ret = find_snapshot_by_id_and_name(bs, id_or_name, NULL);
437 if (ret >= 0) {
c142442b 438 return ret;
c142442b 439 }
a89d89d3 440 return find_snapshot_by_id_and_name(bs, NULL, id_or_name);
c142442b
KW
441}
442
443/* if no id is provided, a new one is constructed */
ed6ccf0f 444int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
c142442b 445{
ff99129a 446 BDRVQcow2State *s = bs->opaque;
d1ea98d5
KW
447 QCowSnapshot *new_snapshot_list = NULL;
448 QCowSnapshot *old_snapshot_list = NULL;
449 QCowSnapshot sn1, *sn = &sn1;
c142442b
KW
450 int i, ret;
451 uint64_t *l1_table = NULL;
5d757b56 452 int64_t l1_table_offset;
c142442b 453
ce48f2f4
KW
454 if (s->nb_snapshots >= QCOW_MAX_SNAPSHOTS) {
455 return -EFBIG;
456 }
457
aa8b34c1
KW
458 if (has_data_file(bs)) {
459 return -ENOTSUP;
460 }
461
c142442b
KW
462 memset(sn, 0, sizeof(*sn));
463
407bc150
YW
464 /* Generate an ID */
465 find_new_snapshot_id(bs, sn_info->id_str, sizeof(sn_info->id_str));
c142442b 466
03343166 467 /* Populate sn with passed data */
7267c094 468 sn->id_str = g_strdup(sn_info->id_str);
7267c094 469 sn->name = g_strdup(sn_info->name);
03343166 470
90b27759 471 sn->disk_size = bs->total_sectors * BDRV_SECTOR_SIZE;
c142442b
KW
472 sn->vm_state_size = sn_info->vm_state_size;
473 sn->date_sec = sn_info->date_sec;
474 sn->date_nsec = sn_info->date_nsec;
475 sn->vm_clock_nsec = sn_info->vm_clock_nsec;
fcf9a6b7 476 sn->extra_data_size = sizeof(QCowSnapshotExtraData);
c142442b 477
03343166 478 /* Allocate the L1 table of the snapshot and copy the current one there. */
5d757b56
KW
479 l1_table_offset = qcow2_alloc_clusters(bs, s->l1_size * sizeof(uint64_t));
480 if (l1_table_offset < 0) {
d1ea98d5 481 ret = l1_table_offset;
5d757b56
KW
482 goto fail;
483 }
484
485 sn->l1_table_offset = l1_table_offset;
c142442b
KW
486 sn->l1_size = s->l1_size;
487
5839e53b 488 l1_table = g_try_new(uint64_t, s->l1_size);
de82815d
KW
489 if (s->l1_size && l1_table == NULL) {
490 ret = -ENOMEM;
491 goto fail;
492 }
493
c142442b
KW
494 for(i = 0; i < s->l1_size; i++) {
495 l1_table[i] = cpu_to_be64(s->l1_table[i]);
496 }
d1ea98d5 497
231bb267 498 ret = qcow2_pre_write_overlap_check(bs, 0, sn->l1_table_offset,
966b000f 499 s->l1_size * sizeof(uint64_t), false);
cf93980e
HR
500 if (ret < 0) {
501 goto fail;
502 }
503
d9ca2ea2 504 ret = bdrv_pwrite(bs->file, sn->l1_table_offset, l1_table,
d1ea98d5
KW
505 s->l1_size * sizeof(uint64_t));
506 if (ret < 0) {
c142442b 507 goto fail;
d1ea98d5
KW
508 }
509
7267c094 510 g_free(l1_table);
c142442b
KW
511 l1_table = NULL;
512
d1ea98d5
KW
513 /*
514 * Increase the refcounts of all clusters and make sure everything is
515 * stable on disk before updating the snapshot table to contain a pointer
516 * to the new L1 table.
517 */
518 ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1);
519 if (ret < 0) {
520 goto fail;
521 }
522
d1ea98d5 523 /* Append the new snapshot to the snapshot list */
5839e53b 524 new_snapshot_list = g_new(QCowSnapshot, s->nb_snapshots + 1);
c142442b 525 if (s->snapshots) {
d1ea98d5
KW
526 memcpy(new_snapshot_list, s->snapshots,
527 s->nb_snapshots * sizeof(QCowSnapshot));
528 old_snapshot_list = s->snapshots;
c142442b 529 }
d1ea98d5 530 s->snapshots = new_snapshot_list;
c142442b
KW
531 s->snapshots[s->nb_snapshots++] = *sn;
532
d1ea98d5
KW
533 ret = qcow2_write_snapshots(bs);
534 if (ret < 0) {
535 g_free(s->snapshots);
536 s->snapshots = old_snapshot_list;
84757f7e 537 s->nb_snapshots--;
c142442b 538 goto fail;
d1ea98d5
KW
539 }
540
541 g_free(old_snapshot_list);
542
1ebf561c
KW
543 /* The VM state isn't needed any more in the active L1 table; in fact, it
544 * hurts by causing expensive COW for the next snapshot. */
d2cb36af 545 qcow2_cluster_discard(bs, qcow2_vm_state_offset(s),
9e029689 546 ROUND_UP(sn->vm_state_size, s->cluster_size),
d2cb36af 547 QCOW2_DISCARD_NEVER, false);
1ebf561c 548
c142442b 549#ifdef DEBUG_ALLOC
6cbc3031
PH
550 {
551 BdrvCheckResult result = {0};
b35278f7 552 qcow2_check_refcounts(bs, &result, 0);
6cbc3031 553 }
c142442b
KW
554#endif
555 return 0;
03343166
KW
556
557fail:
558 g_free(sn->id_str);
7267c094
AL
559 g_free(sn->name);
560 g_free(l1_table);
d1ea98d5
KW
561
562 return ret;
c142442b
KW
563}
564
565/* copy the snapshot 'snapshot_name' into the current disk image */
ed6ccf0f 566int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)
c142442b 567{
ff99129a 568 BDRVQcow2State *s = bs->opaque;
c142442b 569 QCowSnapshot *sn;
a8475d75 570 Error *local_err = NULL;
35d7ace7
KW
571 int i, snapshot_index;
572 int cur_l1_bytes, sn_l1_bytes;
589f284b 573 int ret;
43a0cac4 574 uint64_t *sn_l1_table = NULL;
c142442b 575
aa8b34c1
KW
576 if (has_data_file(bs)) {
577 return -ENOTSUP;
578 }
579
589f284b 580 /* Search the snapshot */
c142442b 581 snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id);
589f284b 582 if (snapshot_index < 0) {
c142442b 583 return -ENOENT;
589f284b 584 }
c142442b
KW
585 sn = &s->snapshots[snapshot_index];
586
a8475d75
AG
587 ret = qcow2_validate_table(bs, sn->l1_table_offset, sn->l1_size,
588 sizeof(uint64_t), QCOW_MAX_L1_SIZE,
589 "Snapshot L1 table", &local_err);
590 if (ret < 0) {
591 error_report_err(local_err);
592 goto fail;
593 }
594
90b27759
KW
595 if (sn->disk_size != bs->total_sectors * BDRV_SECTOR_SIZE) {
596 error_report("qcow2: Loading snapshots with different disk "
597 "size is not implemented");
598 ret = -ENOTSUP;
599 goto fail;
600 }
601
589f284b
KW
602 /*
603 * Make sure that the current L1 table is big enough to contain the whole
604 * L1 table of the snapshot. If the snapshot L1 table is smaller, the
605 * current one must be padded with zeros.
606 */
607 ret = qcow2_grow_l1_table(bs, sn->l1_size, true);
608 if (ret < 0) {
c142442b 609 goto fail;
589f284b 610 }
c142442b 611
35d7ace7
KW
612 cur_l1_bytes = s->l1_size * sizeof(uint64_t);
613 sn_l1_bytes = sn->l1_size * sizeof(uint64_t);
614
589f284b
KW
615 /*
616 * Copy the snapshot L1 table to the current L1 table.
617 *
618 * Before overwriting the old current L1 table on disk, make sure to
619 * increase all refcounts for the clusters referenced by the new one.
43a0cac4
KW
620 * Decrease the refcount referenced by the old one only when the L1
621 * table is overwritten.
589f284b 622 */
de82815d
KW
623 sn_l1_table = g_try_malloc0(cur_l1_bytes);
624 if (cur_l1_bytes && sn_l1_table == NULL) {
625 ret = -ENOMEM;
626 goto fail;
627 }
43a0cac4 628
cf2ab8fc 629 ret = bdrv_pread(bs->file, sn->l1_table_offset,
9a4f4c31 630 sn_l1_table, sn_l1_bytes);
43a0cac4
KW
631 if (ret < 0) {
632 goto fail;
633 }
634
635 ret = qcow2_update_snapshot_refcount(bs, sn->l1_table_offset,
636 sn->l1_size, 1);
589f284b 637 if (ret < 0) {
c142442b 638 goto fail;
589f284b
KW
639 }
640
231bb267 641 ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L1,
966b000f
KW
642 s->l1_table_offset, cur_l1_bytes,
643 false);
cf93980e
HR
644 if (ret < 0) {
645 goto fail;
646 }
647
d9ca2ea2 648 ret = bdrv_pwrite_sync(bs->file, s->l1_table_offset, sn_l1_table,
589f284b
KW
649 cur_l1_bytes);
650 if (ret < 0) {
c142442b 651 goto fail;
589f284b
KW
652 }
653
43a0cac4
KW
654 /*
655 * Decrease refcount of clusters of current L1 table.
656 *
657 * At this point, the in-memory s->l1_table points to the old L1 table,
658 * whereas on disk we already have the new one.
659 *
660 * qcow2_update_snapshot_refcount special cases the current L1 table to use
661 * the in-memory data instead of really using the offset to load a new one,
662 * which is why this works.
663 */
664 ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset,
665 s->l1_size, -1);
666
667 /*
668 * Now update the in-memory L1 table to be in sync with the on-disk one. We
669 * need to do this even if updating refcounts failed.
670 */
c142442b 671 for(i = 0;i < s->l1_size; i++) {
43a0cac4 672 s->l1_table[i] = be64_to_cpu(sn_l1_table[i]);
c142442b
KW
673 }
674
43a0cac4
KW
675 if (ret < 0) {
676 goto fail;
677 }
678
679 g_free(sn_l1_table);
680 sn_l1_table = NULL;
681
682 /*
683 * Update QCOW_OFLAG_COPIED in the active L1 table (it may have changed
684 * when we decreased the refcount of the old snapshot.
685 */
686 ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0);
589f284b 687 if (ret < 0) {
c142442b 688 goto fail;
589f284b 689 }
c142442b
KW
690
691#ifdef DEBUG_ALLOC
6cbc3031
PH
692 {
693 BdrvCheckResult result = {0};
b35278f7 694 qcow2_check_refcounts(bs, &result, 0);
6cbc3031 695 }
c142442b
KW
696#endif
697 return 0;
589f284b
KW
698
699fail:
43a0cac4 700 g_free(sn_l1_table);
589f284b 701 return ret;
c142442b
KW
702}
703
a89d89d3
WX
704int qcow2_snapshot_delete(BlockDriverState *bs,
705 const char *snapshot_id,
706 const char *name,
707 Error **errp)
c142442b 708{
ff99129a 709 BDRVQcow2State *s = bs->opaque;
9a476780 710 QCowSnapshot sn;
c142442b
KW
711 int snapshot_index, ret;
712
aa8b34c1
KW
713 if (has_data_file(bs)) {
714 return -ENOTSUP;
715 }
716
9a476780 717 /* Search the snapshot */
a89d89d3 718 snapshot_index = find_snapshot_by_id_and_name(bs, snapshot_id, name);
9a476780 719 if (snapshot_index < 0) {
a89d89d3 720 error_setg(errp, "Can't find the snapshot");
c142442b 721 return -ENOENT;
9a476780
KW
722 }
723 sn = s->snapshots[snapshot_index];
c142442b 724
db5794f1
AG
725 ret = qcow2_validate_table(bs, sn.l1_table_offset, sn.l1_size,
726 sizeof(uint64_t), QCOW_MAX_L1_SIZE,
727 "Snapshot L1 table", errp);
728 if (ret < 0) {
729 return ret;
730 }
731
9a476780
KW
732 /* Remove it from the snapshot list */
733 memmove(s->snapshots + snapshot_index,
734 s->snapshots + snapshot_index + 1,
735 (s->nb_snapshots - snapshot_index - 1) * sizeof(sn));
736 s->nb_snapshots--;
737 ret = qcow2_write_snapshots(bs);
738 if (ret < 0) {
39a611a3
JC
739 error_setg_errno(errp, -ret,
740 "Failed to remove snapshot from snapshot list");
c142442b 741 return ret;
9a476780
KW
742 }
743
744 /*
745 * The snapshot is now unused, clean up. If we fail after this point, we
746 * won't recover but just leak clusters.
747 */
fcf9a6b7 748 g_free(sn.unknown_extra_data);
9a476780
KW
749 g_free(sn.id_str);
750 g_free(sn.name);
751
752 /*
753 * Now decrease the refcounts of clusters referenced by the snapshot and
754 * free the L1 table.
755 */
756 ret = qcow2_update_snapshot_refcount(bs, sn.l1_table_offset,
757 sn.l1_size, -1);
758 if (ret < 0) {
39a611a3 759 error_setg_errno(errp, -ret, "Failed to free the cluster and L1 table");
c142442b 760 return ret;
9a476780 761 }
6cfcb9b8
KW
762 qcow2_free_clusters(bs, sn.l1_table_offset, sn.l1_size * sizeof(uint64_t),
763 QCOW2_DISCARD_SNAPSHOT);
c142442b 764
9a476780
KW
765 /* must update the copied flag on the current cluster offsets */
766 ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0);
c142442b 767 if (ret < 0) {
39a611a3
JC
768 error_setg_errno(errp, -ret,
769 "Failed to update snapshot status in disk");
c142442b
KW
770 return ret;
771 }
9a476780 772
c142442b 773#ifdef DEBUG_ALLOC
6cbc3031
PH
774 {
775 BdrvCheckResult result = {0};
b35278f7 776 qcow2_check_refcounts(bs, &result, 0);
6cbc3031 777 }
c142442b
KW
778#endif
779 return 0;
780}
781
ed6ccf0f 782int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
c142442b 783{
ff99129a 784 BDRVQcow2State *s = bs->opaque;
c142442b
KW
785 QEMUSnapshotInfo *sn_tab, *sn_info;
786 QCowSnapshot *sn;
787 int i;
788
aa8b34c1
KW
789 if (has_data_file(bs)) {
790 return -ENOTSUP;
791 }
c142442b
KW
792 if (!s->nb_snapshots) {
793 *psn_tab = NULL;
794 return s->nb_snapshots;
795 }
796
5839e53b 797 sn_tab = g_new0(QEMUSnapshotInfo, s->nb_snapshots);
c142442b
KW
798 for(i = 0; i < s->nb_snapshots; i++) {
799 sn_info = sn_tab + i;
800 sn = s->snapshots + i;
801 pstrcpy(sn_info->id_str, sizeof(sn_info->id_str),
802 sn->id_str);
803 pstrcpy(sn_info->name, sizeof(sn_info->name),
804 sn->name);
805 sn_info->vm_state_size = sn->vm_state_size;
806 sn_info->date_sec = sn->date_sec;
807 sn_info->date_nsec = sn->date_nsec;
808 sn_info->vm_clock_nsec = sn->vm_clock_nsec;
809 }
810 *psn_tab = sn_tab;
811 return s->nb_snapshots;
812}
813
7b4c4781
WX
814int qcow2_snapshot_load_tmp(BlockDriverState *bs,
815 const char *snapshot_id,
816 const char *name,
817 Error **errp)
51ef6727 818{
e3f652b3 819 int i, snapshot_index;
ff99129a 820 BDRVQcow2State *s = bs->opaque;
51ef6727 821 QCowSnapshot *sn;
e3f652b3
KW
822 uint64_t *new_l1_table;
823 int new_l1_bytes;
824 int ret;
51ef6727 825
e3f652b3
KW
826 assert(bs->read_only);
827
828 /* Search the snapshot */
7b4c4781 829 snapshot_index = find_snapshot_by_id_and_name(bs, snapshot_id, name);
51ef6727 830 if (snapshot_index < 0) {
7b4c4781
WX
831 error_setg(errp,
832 "Can't find snapshot");
51ef6727 833 return -ENOENT;
834 }
51ef6727 835 sn = &s->snapshots[snapshot_index];
51ef6727 836
e3f652b3 837 /* Allocate and read in the snapshot's L1 table */
314e8d39
AG
838 ret = qcow2_validate_table(bs, sn->l1_table_offset, sn->l1_size,
839 sizeof(uint64_t), QCOW_MAX_L1_SIZE,
840 "Snapshot L1 table", errp);
841 if (ret < 0) {
842 return ret;
6a83f8b5 843 }
c05e4667 844 new_l1_bytes = sn->l1_size * sizeof(uint64_t);
9a4f4c31 845 new_l1_table = qemu_try_blockalign(bs->file->bs,
9e029689 846 ROUND_UP(new_l1_bytes, 512));
de82815d
KW
847 if (new_l1_table == NULL) {
848 return -ENOMEM;
849 }
51ef6727 850
cf2ab8fc 851 ret = bdrv_pread(bs->file, sn->l1_table_offset,
9a4f4c31 852 new_l1_table, new_l1_bytes);
e3f652b3 853 if (ret < 0) {
7b4c4781 854 error_setg(errp, "Failed to read l1 table for snapshot");
de82815d 855 qemu_vfree(new_l1_table);
e3f652b3 856 return ret;
51ef6727 857 }
858
e3f652b3 859 /* Switch the L1 table */
de82815d 860 qemu_vfree(s->l1_table);
e3f652b3
KW
861
862 s->l1_size = sn->l1_size;
863 s->l1_table_offset = sn->l1_table_offset;
864 s->l1_table = new_l1_table;
865
51ef6727 866 for(i = 0;i < s->l1_size; i++) {
867 be64_to_cpus(&s->l1_table[i]);
868 }
e3f652b3 869
51ef6727 870 return 0;
871}