xref: /openbmc/qemu/block/qcow2-snapshot.c (revision 1fd6bb44)
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-common.h"
26 #include "block/block_int.h"
27 #include "block/qcow2.h"
28 
29 typedef struct QEMU_PACKED QCowSnapshotHeader {
30     /* header is 8 byte aligned */
31     uint64_t l1_table_offset;
32 
33     uint32_t l1_size;
34     uint16_t id_str_size;
35     uint16_t name_size;
36 
37     uint32_t date_sec;
38     uint32_t date_nsec;
39 
40     uint64_t vm_clock_nsec;
41 
42     uint32_t vm_state_size;
43     uint32_t extra_data_size; /* for extension */
44     /* extra data follows */
45     /* id_str follows */
46     /* name follows  */
47 } QCowSnapshotHeader;
48 
49 typedef struct QEMU_PACKED QCowSnapshotExtraData {
50     uint64_t vm_state_size_large;
51     uint64_t disk_size;
52 } QCowSnapshotExtraData;
53 
54 void qcow2_free_snapshots(BlockDriverState *bs)
55 {
56     BDRVQcowState *s = bs->opaque;
57     int i;
58 
59     for(i = 0; i < s->nb_snapshots; i++) {
60         g_free(s->snapshots[i].name);
61         g_free(s->snapshots[i].id_str);
62     }
63     g_free(s->snapshots);
64     s->snapshots = NULL;
65     s->nb_snapshots = 0;
66 }
67 
68 int qcow2_read_snapshots(BlockDriverState *bs)
69 {
70     BDRVQcowState *s = bs->opaque;
71     QCowSnapshotHeader h;
72     QCowSnapshotExtraData extra;
73     QCowSnapshot *sn;
74     int i, id_str_size, name_size;
75     int64_t offset;
76     uint32_t extra_data_size;
77     int ret;
78 
79     if (!s->nb_snapshots) {
80         s->snapshots = NULL;
81         s->snapshots_size = 0;
82         return 0;
83     }
84 
85     offset = s->snapshots_offset;
86     s->snapshots = g_malloc0(s->nb_snapshots * sizeof(QCowSnapshot));
87 
88     for(i = 0; i < s->nb_snapshots; i++) {
89         /* Read statically sized part of the snapshot header */
90         offset = align_offset(offset, 8);
91         ret = bdrv_pread(bs->file, offset, &h, sizeof(h));
92         if (ret < 0) {
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         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         /* Read extra data */
110         ret = bdrv_pread(bs->file, offset, &extra,
111                          MIN(sizeof(extra), extra_data_size));
112         if (ret < 0) {
113             goto fail;
114         }
115         offset += extra_data_size;
116 
117         if (extra_data_size >= 8) {
118             sn->vm_state_size = be64_to_cpu(extra.vm_state_size_large);
119         }
120 
121         if (extra_data_size >= 16) {
122             sn->disk_size = be64_to_cpu(extra.disk_size);
123         } else {
124             sn->disk_size = bs->total_sectors * BDRV_SECTOR_SIZE;
125         }
126 
127         /* Read snapshot ID */
128         sn->id_str = g_malloc(id_str_size + 1);
129         ret = bdrv_pread(bs->file, offset, sn->id_str, id_str_size);
130         if (ret < 0) {
131             goto fail;
132         }
133         offset += id_str_size;
134         sn->id_str[id_str_size] = '\0';
135 
136         /* Read snapshot name */
137         sn->name = g_malloc(name_size + 1);
138         ret = bdrv_pread(bs->file, offset, sn->name, name_size);
139         if (ret < 0) {
140             goto fail;
141         }
142         offset += name_size;
143         sn->name[name_size] = '\0';
144     }
145 
146     s->snapshots_size = offset - s->snapshots_offset;
147     return 0;
148 
149 fail:
150     qcow2_free_snapshots(bs);
151     return ret;
152 }
153 
154 /* add at the end of the file a new list of snapshots */
155 static int qcow2_write_snapshots(BlockDriverState *bs)
156 {
157     BDRVQcowState *s = bs->opaque;
158     QCowSnapshot *sn;
159     QCowSnapshotHeader h;
160     QCowSnapshotExtraData extra;
161     int i, name_size, id_str_size, snapshots_size;
162     struct {
163         uint32_t nb_snapshots;
164         uint64_t snapshots_offset;
165     } QEMU_PACKED header_data;
166     int64_t offset, snapshots_offset;
167     int ret;
168 
169     /* compute the size of the snapshots */
170     offset = 0;
171     for(i = 0; i < s->nb_snapshots; i++) {
172         sn = s->snapshots + i;
173         offset = align_offset(offset, 8);
174         offset += sizeof(h);
175         offset += sizeof(extra);
176         offset += strlen(sn->id_str);
177         offset += strlen(sn->name);
178     }
179     snapshots_size = offset;
180 
181     /* Allocate space for the new snapshot list */
182     snapshots_offset = qcow2_alloc_clusters(bs, snapshots_size);
183     offset = snapshots_offset;
184     if (offset < 0) {
185         return offset;
186     }
187     ret = bdrv_flush(bs);
188     if (ret < 0) {
189         return ret;
190     }
191 
192     /* Write all snapshots to the new list */
193     for(i = 0; i < s->nb_snapshots; i++) {
194         sn = s->snapshots + i;
195         memset(&h, 0, sizeof(h));
196         h.l1_table_offset = cpu_to_be64(sn->l1_table_offset);
197         h.l1_size = cpu_to_be32(sn->l1_size);
198         /* If it doesn't fit in 32 bit, older implementations should treat it
199          * as a disk-only snapshot rather than truncate the VM state */
200         if (sn->vm_state_size <= 0xffffffff) {
201             h.vm_state_size = cpu_to_be32(sn->vm_state_size);
202         }
203         h.date_sec = cpu_to_be32(sn->date_sec);
204         h.date_nsec = cpu_to_be32(sn->date_nsec);
205         h.vm_clock_nsec = cpu_to_be64(sn->vm_clock_nsec);
206         h.extra_data_size = cpu_to_be32(sizeof(extra));
207 
208         memset(&extra, 0, sizeof(extra));
209         extra.vm_state_size_large = cpu_to_be64(sn->vm_state_size);
210         extra.disk_size = cpu_to_be64(sn->disk_size);
211 
212         id_str_size = strlen(sn->id_str);
213         name_size = strlen(sn->name);
214         h.id_str_size = cpu_to_be16(id_str_size);
215         h.name_size = cpu_to_be16(name_size);
216         offset = align_offset(offset, 8);
217 
218         ret = bdrv_pwrite(bs->file, offset, &h, sizeof(h));
219         if (ret < 0) {
220             goto fail;
221         }
222         offset += sizeof(h);
223 
224         ret = bdrv_pwrite(bs->file, offset, &extra, sizeof(extra));
225         if (ret < 0) {
226             goto fail;
227         }
228         offset += sizeof(extra);
229 
230         ret = bdrv_pwrite(bs->file, offset, sn->id_str, id_str_size);
231         if (ret < 0) {
232             goto fail;
233         }
234         offset += id_str_size;
235 
236         ret = bdrv_pwrite(bs->file, offset, sn->name, name_size);
237         if (ret < 0) {
238             goto fail;
239         }
240         offset += name_size;
241     }
242 
243     /*
244      * Update the header to point to the new snapshot table. This requires the
245      * new table and its refcounts to be stable on disk.
246      */
247     ret = bdrv_flush(bs);
248     if (ret < 0) {
249         goto fail;
250     }
251 
252     QEMU_BUILD_BUG_ON(offsetof(QCowHeader, snapshots_offset) !=
253         offsetof(QCowHeader, nb_snapshots) + sizeof(header_data.nb_snapshots));
254 
255     header_data.nb_snapshots        = cpu_to_be32(s->nb_snapshots);
256     header_data.snapshots_offset    = cpu_to_be64(snapshots_offset);
257 
258     ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, nb_snapshots),
259                            &header_data, sizeof(header_data));
260     if (ret < 0) {
261         goto fail;
262     }
263 
264     /* free the old snapshot table */
265     qcow2_free_clusters(bs, s->snapshots_offset, s->snapshots_size);
266     s->snapshots_offset = snapshots_offset;
267     s->snapshots_size = snapshots_size;
268     return 0;
269 
270 fail:
271     return ret;
272 }
273 
274 static void find_new_snapshot_id(BlockDriverState *bs,
275                                  char *id_str, int id_str_size)
276 {
277     BDRVQcowState *s = bs->opaque;
278     QCowSnapshot *sn;
279     int i, id, id_max = 0;
280 
281     for(i = 0; i < s->nb_snapshots; i++) {
282         sn = s->snapshots + i;
283         id = strtoul(sn->id_str, NULL, 10);
284         if (id > id_max)
285             id_max = id;
286     }
287     snprintf(id_str, id_str_size, "%d", id_max + 1);
288 }
289 
290 static int find_snapshot_by_id(BlockDriverState *bs, const char *id_str)
291 {
292     BDRVQcowState *s = bs->opaque;
293     int i;
294 
295     for(i = 0; i < s->nb_snapshots; i++) {
296         if (!strcmp(s->snapshots[i].id_str, id_str))
297             return i;
298     }
299     return -1;
300 }
301 
302 static int find_snapshot_by_id_or_name(BlockDriverState *bs, const char *name)
303 {
304     BDRVQcowState *s = bs->opaque;
305     int i, ret;
306 
307     ret = find_snapshot_by_id(bs, name);
308     if (ret >= 0)
309         return ret;
310     for(i = 0; i < s->nb_snapshots; i++) {
311         if (!strcmp(s->snapshots[i].name, name))
312             return i;
313     }
314     return -1;
315 }
316 
317 /* if no id is provided, a new one is constructed */
318 int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
319 {
320     BDRVQcowState *s = bs->opaque;
321     QCowSnapshot *new_snapshot_list = NULL;
322     QCowSnapshot *old_snapshot_list = NULL;
323     QCowSnapshot sn1, *sn = &sn1;
324     int i, ret;
325     uint64_t *l1_table = NULL;
326     int64_t l1_table_offset;
327 
328     memset(sn, 0, sizeof(*sn));
329 
330     /* Generate an ID if it wasn't passed */
331     if (sn_info->id_str[0] == '\0') {
332         find_new_snapshot_id(bs, sn_info->id_str, sizeof(sn_info->id_str));
333     }
334 
335     /* Check that the ID is unique */
336     if (find_snapshot_by_id(bs, sn_info->id_str) >= 0) {
337         return -EEXIST;
338     }
339 
340     /* Populate sn with passed data */
341     sn->id_str = g_strdup(sn_info->id_str);
342     sn->name = g_strdup(sn_info->name);
343 
344     sn->disk_size = bs->total_sectors * BDRV_SECTOR_SIZE;
345     sn->vm_state_size = sn_info->vm_state_size;
346     sn->date_sec = sn_info->date_sec;
347     sn->date_nsec = sn_info->date_nsec;
348     sn->vm_clock_nsec = sn_info->vm_clock_nsec;
349 
350     /* Allocate the L1 table of the snapshot and copy the current one there. */
351     l1_table_offset = qcow2_alloc_clusters(bs, s->l1_size * sizeof(uint64_t));
352     if (l1_table_offset < 0) {
353         ret = l1_table_offset;
354         goto fail;
355     }
356 
357     sn->l1_table_offset = l1_table_offset;
358     sn->l1_size = s->l1_size;
359 
360     l1_table = g_malloc(s->l1_size * sizeof(uint64_t));
361     for(i = 0; i < s->l1_size; i++) {
362         l1_table[i] = cpu_to_be64(s->l1_table[i]);
363     }
364 
365     ret = bdrv_pwrite(bs->file, sn->l1_table_offset, l1_table,
366                       s->l1_size * sizeof(uint64_t));
367     if (ret < 0) {
368         goto fail;
369     }
370 
371     g_free(l1_table);
372     l1_table = NULL;
373 
374     /*
375      * Increase the refcounts of all clusters and make sure everything is
376      * stable on disk before updating the snapshot table to contain a pointer
377      * to the new L1 table.
378      */
379     ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1);
380     if (ret < 0) {
381         goto fail;
382     }
383 
384     /* Append the new snapshot to the snapshot list */
385     new_snapshot_list = g_malloc((s->nb_snapshots + 1) * sizeof(QCowSnapshot));
386     if (s->snapshots) {
387         memcpy(new_snapshot_list, s->snapshots,
388                s->nb_snapshots * sizeof(QCowSnapshot));
389         old_snapshot_list = s->snapshots;
390     }
391     s->snapshots = new_snapshot_list;
392     s->snapshots[s->nb_snapshots++] = *sn;
393 
394     ret = qcow2_write_snapshots(bs);
395     if (ret < 0) {
396         g_free(s->snapshots);
397         s->snapshots = old_snapshot_list;
398         goto fail;
399     }
400 
401     g_free(old_snapshot_list);
402 
403 #ifdef DEBUG_ALLOC
404     {
405       BdrvCheckResult result = {0};
406       qcow2_check_refcounts(bs, &result, 0);
407     }
408 #endif
409     return 0;
410 
411 fail:
412     g_free(sn->id_str);
413     g_free(sn->name);
414     g_free(l1_table);
415 
416     return ret;
417 }
418 
419 /* copy the snapshot 'snapshot_name' into the current disk image */
420 int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)
421 {
422     BDRVQcowState *s = bs->opaque;
423     QCowSnapshot *sn;
424     int i, snapshot_index;
425     int cur_l1_bytes, sn_l1_bytes;
426     int ret;
427     uint64_t *sn_l1_table = NULL;
428 
429     /* Search the snapshot */
430     snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id);
431     if (snapshot_index < 0) {
432         return -ENOENT;
433     }
434     sn = &s->snapshots[snapshot_index];
435 
436     if (sn->disk_size != bs->total_sectors * BDRV_SECTOR_SIZE) {
437         error_report("qcow2: Loading snapshots with different disk "
438             "size is not implemented");
439         ret = -ENOTSUP;
440         goto fail;
441     }
442 
443     /*
444      * Make sure that the current L1 table is big enough to contain the whole
445      * L1 table of the snapshot. If the snapshot L1 table is smaller, the
446      * current one must be padded with zeros.
447      */
448     ret = qcow2_grow_l1_table(bs, sn->l1_size, true);
449     if (ret < 0) {
450         goto fail;
451     }
452 
453     cur_l1_bytes = s->l1_size * sizeof(uint64_t);
454     sn_l1_bytes = sn->l1_size * sizeof(uint64_t);
455 
456     /*
457      * Copy the snapshot L1 table to the current L1 table.
458      *
459      * Before overwriting the old current L1 table on disk, make sure to
460      * increase all refcounts for the clusters referenced by the new one.
461      * Decrease the refcount referenced by the old one only when the L1
462      * table is overwritten.
463      */
464     sn_l1_table = g_malloc0(cur_l1_bytes);
465 
466     ret = bdrv_pread(bs->file, sn->l1_table_offset, sn_l1_table, sn_l1_bytes);
467     if (ret < 0) {
468         goto fail;
469     }
470 
471     ret = qcow2_update_snapshot_refcount(bs, sn->l1_table_offset,
472                                          sn->l1_size, 1);
473     if (ret < 0) {
474         goto fail;
475     }
476 
477     ret = bdrv_pwrite_sync(bs->file, s->l1_table_offset, sn_l1_table,
478                            cur_l1_bytes);
479     if (ret < 0) {
480         goto fail;
481     }
482 
483     /*
484      * Decrease refcount of clusters of current L1 table.
485      *
486      * At this point, the in-memory s->l1_table points to the old L1 table,
487      * whereas on disk we already have the new one.
488      *
489      * qcow2_update_snapshot_refcount special cases the current L1 table to use
490      * the in-memory data instead of really using the offset to load a new one,
491      * which is why this works.
492      */
493     ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset,
494                                          s->l1_size, -1);
495 
496     /*
497      * Now update the in-memory L1 table to be in sync with the on-disk one. We
498      * need to do this even if updating refcounts failed.
499      */
500     for(i = 0;i < s->l1_size; i++) {
501         s->l1_table[i] = be64_to_cpu(sn_l1_table[i]);
502     }
503 
504     if (ret < 0) {
505         goto fail;
506     }
507 
508     g_free(sn_l1_table);
509     sn_l1_table = NULL;
510 
511     /*
512      * Update QCOW_OFLAG_COPIED in the active L1 table (it may have changed
513      * when we decreased the refcount of the old snapshot.
514      */
515     ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0);
516     if (ret < 0) {
517         goto fail;
518     }
519 
520 #ifdef DEBUG_ALLOC
521     {
522         BdrvCheckResult result = {0};
523         qcow2_check_refcounts(bs, &result, 0);
524     }
525 #endif
526     return 0;
527 
528 fail:
529     g_free(sn_l1_table);
530     return ret;
531 }
532 
533 int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
534 {
535     BDRVQcowState *s = bs->opaque;
536     QCowSnapshot sn;
537     int snapshot_index, ret;
538 
539     /* Search the snapshot */
540     snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id);
541     if (snapshot_index < 0) {
542         return -ENOENT;
543     }
544     sn = s->snapshots[snapshot_index];
545 
546     /* Remove it from the snapshot list */
547     memmove(s->snapshots + snapshot_index,
548             s->snapshots + snapshot_index + 1,
549             (s->nb_snapshots - snapshot_index - 1) * sizeof(sn));
550     s->nb_snapshots--;
551     ret = qcow2_write_snapshots(bs);
552     if (ret < 0) {
553         return ret;
554     }
555 
556     /*
557      * The snapshot is now unused, clean up. If we fail after this point, we
558      * won't recover but just leak clusters.
559      */
560     g_free(sn.id_str);
561     g_free(sn.name);
562 
563     /*
564      * Now decrease the refcounts of clusters referenced by the snapshot and
565      * free the L1 table.
566      */
567     ret = qcow2_update_snapshot_refcount(bs, sn.l1_table_offset,
568                                          sn.l1_size, -1);
569     if (ret < 0) {
570         return ret;
571     }
572     qcow2_free_clusters(bs, sn.l1_table_offset, sn.l1_size * sizeof(uint64_t));
573 
574     /* must update the copied flag on the current cluster offsets */
575     ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0);
576     if (ret < 0) {
577         return ret;
578     }
579 
580 #ifdef DEBUG_ALLOC
581     {
582         BdrvCheckResult result = {0};
583         qcow2_check_refcounts(bs, &result, 0);
584     }
585 #endif
586     return 0;
587 }
588 
589 int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
590 {
591     BDRVQcowState *s = bs->opaque;
592     QEMUSnapshotInfo *sn_tab, *sn_info;
593     QCowSnapshot *sn;
594     int i;
595 
596     if (!s->nb_snapshots) {
597         *psn_tab = NULL;
598         return s->nb_snapshots;
599     }
600 
601     sn_tab = g_malloc0(s->nb_snapshots * sizeof(QEMUSnapshotInfo));
602     for(i = 0; i < s->nb_snapshots; i++) {
603         sn_info = sn_tab + i;
604         sn = s->snapshots + i;
605         pstrcpy(sn_info->id_str, sizeof(sn_info->id_str),
606                 sn->id_str);
607         pstrcpy(sn_info->name, sizeof(sn_info->name),
608                 sn->name);
609         sn_info->vm_state_size = sn->vm_state_size;
610         sn_info->date_sec = sn->date_sec;
611         sn_info->date_nsec = sn->date_nsec;
612         sn_info->vm_clock_nsec = sn->vm_clock_nsec;
613     }
614     *psn_tab = sn_tab;
615     return s->nb_snapshots;
616 }
617 
618 int qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name)
619 {
620     int i, snapshot_index;
621     BDRVQcowState *s = bs->opaque;
622     QCowSnapshot *sn;
623     uint64_t *new_l1_table;
624     int new_l1_bytes;
625     int ret;
626 
627     assert(bs->read_only);
628 
629     /* Search the snapshot */
630     snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_name);
631     if (snapshot_index < 0) {
632         return -ENOENT;
633     }
634     sn = &s->snapshots[snapshot_index];
635 
636     /* Allocate and read in the snapshot's L1 table */
637     new_l1_bytes = s->l1_size * sizeof(uint64_t);
638     new_l1_table = g_malloc0(align_offset(new_l1_bytes, 512));
639 
640     ret = bdrv_pread(bs->file, sn->l1_table_offset, new_l1_table, new_l1_bytes);
641     if (ret < 0) {
642         g_free(new_l1_table);
643         return ret;
644     }
645 
646     /* Switch the L1 table */
647     g_free(s->l1_table);
648 
649     s->l1_size = sn->l1_size;
650     s->l1_table_offset = sn->l1_table_offset;
651     s->l1_table = new_l1_table;
652 
653     for(i = 0;i < s->l1_size; i++) {
654         be64_to_cpus(&s->l1_table[i]);
655     }
656 
657     return 0;
658 }
659