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