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