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