Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 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 | |
Peter Maydell | 80c71a2 | 2016-01-18 18:01:42 +0000 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 26 | #include "qapi/error.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 27 | #include "block/block_int.h" |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 28 | #include "block/qcow2.h" |
Paolo Bonzini | 58369e2 | 2016-03-15 17:22:36 +0100 | [diff] [blame] | 29 | #include "qemu/bswap.h" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 30 | #include "qemu/error-report.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 31 | #include "qemu/cutils.h" |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 32 | |
Kevin Wolf | ed6ccf0 | 2009-05-28 16:07:07 +0200 | [diff] [blame] | 33 | void qcow2_free_snapshots(BlockDriverState *bs) |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 34 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 35 | BDRVQcow2State *s = bs->opaque; |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 36 | int i; |
| 37 | |
| 38 | for(i = 0; i < s->nb_snapshots; i++) { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 39 | g_free(s->snapshots[i].name); |
| 40 | g_free(s->snapshots[i].id_str); |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 41 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 42 | g_free(s->snapshots); |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 43 | s->snapshots = NULL; |
| 44 | s->nb_snapshots = 0; |
| 45 | } |
| 46 | |
Kevin Wolf | ed6ccf0 | 2009-05-28 16:07:07 +0200 | [diff] [blame] | 47 | int qcow2_read_snapshots(BlockDriverState *bs) |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 48 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 49 | BDRVQcow2State *s = bs->opaque; |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 50 | QCowSnapshotHeader h; |
Kevin Wolf | c2c9a46 | 2011-11-16 11:35:54 +0100 | [diff] [blame] | 51 | QCowSnapshotExtraData extra; |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 52 | QCowSnapshot *sn; |
| 53 | int i, id_str_size, name_size; |
| 54 | int64_t offset; |
| 55 | uint32_t extra_data_size; |
Kevin Wolf | 42deb29 | 2011-11-16 11:43:28 +0100 | [diff] [blame] | 56 | int ret; |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 57 | |
| 58 | if (!s->nb_snapshots) { |
| 59 | s->snapshots = NULL; |
| 60 | s->snapshots_size = 0; |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | offset = s->snapshots_offset; |
Markus Armbruster | 5839e53 | 2014-08-19 10:31:08 +0200 | [diff] [blame] | 65 | s->snapshots = g_new0(QCowSnapshot, s->nb_snapshots); |
Kevin Wolf | 42deb29 | 2011-11-16 11:43:28 +0100 | [diff] [blame] | 66 | |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 67 | for(i = 0; i < s->nb_snapshots; i++) { |
Kevin Wolf | 42deb29 | 2011-11-16 11:43:28 +0100 | [diff] [blame] | 68 | /* Read statically sized part of the snapshot header */ |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 69 | offset = align_offset(offset, 8); |
Kevin Wolf | cf2ab8f | 2016-06-20 18:24:02 +0200 | [diff] [blame] | 70 | ret = bdrv_pread(bs->file, offset, &h, sizeof(h)); |
Kevin Wolf | 42deb29 | 2011-11-16 11:43:28 +0100 | [diff] [blame] | 71 | if (ret < 0) { |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 72 | goto fail; |
Kevin Wolf | 42deb29 | 2011-11-16 11:43:28 +0100 | [diff] [blame] | 73 | } |
| 74 | |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 75 | offset += sizeof(h); |
| 76 | sn = s->snapshots + i; |
| 77 | sn->l1_table_offset = be64_to_cpu(h.l1_table_offset); |
| 78 | sn->l1_size = be32_to_cpu(h.l1_size); |
| 79 | sn->vm_state_size = be32_to_cpu(h.vm_state_size); |
| 80 | sn->date_sec = be32_to_cpu(h.date_sec); |
| 81 | sn->date_nsec = be32_to_cpu(h.date_nsec); |
| 82 | sn->vm_clock_nsec = be64_to_cpu(h.vm_clock_nsec); |
| 83 | extra_data_size = be32_to_cpu(h.extra_data_size); |
| 84 | |
| 85 | id_str_size = be16_to_cpu(h.id_str_size); |
| 86 | name_size = be16_to_cpu(h.name_size); |
| 87 | |
Kevin Wolf | c2c9a46 | 2011-11-16 11:35:54 +0100 | [diff] [blame] | 88 | /* Read extra data */ |
Kevin Wolf | cf2ab8f | 2016-06-20 18:24:02 +0200 | [diff] [blame] | 89 | ret = bdrv_pread(bs->file, offset, &extra, |
Kevin Wolf | c2c9a46 | 2011-11-16 11:35:54 +0100 | [diff] [blame] | 90 | MIN(sizeof(extra), extra_data_size)); |
| 91 | if (ret < 0) { |
| 92 | goto fail; |
| 93 | } |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 94 | offset += extra_data_size; |
| 95 | |
Kevin Wolf | c2c9a46 | 2011-11-16 11:35:54 +0100 | [diff] [blame] | 96 | if (extra_data_size >= 8) { |
| 97 | sn->vm_state_size = be64_to_cpu(extra.vm_state_size_large); |
| 98 | } |
| 99 | |
Kevin Wolf | 90b2775 | 2012-04-11 16:33:50 +0200 | [diff] [blame] | 100 | if (extra_data_size >= 16) { |
| 101 | sn->disk_size = be64_to_cpu(extra.disk_size); |
| 102 | } else { |
| 103 | sn->disk_size = bs->total_sectors * BDRV_SECTOR_SIZE; |
| 104 | } |
| 105 | |
Kevin Wolf | 42deb29 | 2011-11-16 11:43:28 +0100 | [diff] [blame] | 106 | /* Read snapshot ID */ |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 107 | sn->id_str = g_malloc(id_str_size + 1); |
Kevin Wolf | cf2ab8f | 2016-06-20 18:24:02 +0200 | [diff] [blame] | 108 | ret = bdrv_pread(bs->file, offset, sn->id_str, id_str_size); |
Kevin Wolf | 42deb29 | 2011-11-16 11:43:28 +0100 | [diff] [blame] | 109 | if (ret < 0) { |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 110 | goto fail; |
Kevin Wolf | 42deb29 | 2011-11-16 11:43:28 +0100 | [diff] [blame] | 111 | } |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 112 | offset += id_str_size; |
| 113 | sn->id_str[id_str_size] = '\0'; |
| 114 | |
Kevin Wolf | 42deb29 | 2011-11-16 11:43:28 +0100 | [diff] [blame] | 115 | /* Read snapshot name */ |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 116 | sn->name = g_malloc(name_size + 1); |
Kevin Wolf | cf2ab8f | 2016-06-20 18:24:02 +0200 | [diff] [blame] | 117 | ret = bdrv_pread(bs->file, offset, sn->name, name_size); |
Kevin Wolf | 42deb29 | 2011-11-16 11:43:28 +0100 | [diff] [blame] | 118 | if (ret < 0) { |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 119 | goto fail; |
Kevin Wolf | 42deb29 | 2011-11-16 11:43:28 +0100 | [diff] [blame] | 120 | } |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 121 | offset += name_size; |
| 122 | sn->name[name_size] = '\0'; |
Kevin Wolf | 5dae6e3 | 2014-03-26 13:06:07 +0100 | [diff] [blame] | 123 | |
| 124 | if (offset - s->snapshots_offset > QCOW_MAX_SNAPSHOTS_SIZE) { |
| 125 | ret = -EFBIG; |
| 126 | goto fail; |
| 127 | } |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 128 | } |
Kevin Wolf | 42deb29 | 2011-11-16 11:43:28 +0100 | [diff] [blame] | 129 | |
Kevin Wolf | 5dae6e3 | 2014-03-26 13:06:07 +0100 | [diff] [blame] | 130 | assert(offset - s->snapshots_offset <= INT_MAX); |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 131 | s->snapshots_size = offset - s->snapshots_offset; |
| 132 | return 0; |
Kevin Wolf | 42deb29 | 2011-11-16 11:43:28 +0100 | [diff] [blame] | 133 | |
| 134 | fail: |
Kevin Wolf | ed6ccf0 | 2009-05-28 16:07:07 +0200 | [diff] [blame] | 135 | qcow2_free_snapshots(bs); |
Kevin Wolf | 42deb29 | 2011-11-16 11:43:28 +0100 | [diff] [blame] | 136 | return ret; |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /* add at the end of the file a new list of snapshots */ |
Jes Sorensen | 7c80ab3 | 2010-12-17 16:02:39 +0100 | [diff] [blame] | 140 | static int qcow2_write_snapshots(BlockDriverState *bs) |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 141 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 142 | BDRVQcow2State *s = bs->opaque; |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 143 | QCowSnapshot *sn; |
| 144 | QCowSnapshotHeader h; |
Kevin Wolf | c2c9a46 | 2011-11-16 11:35:54 +0100 | [diff] [blame] | 145 | QCowSnapshotExtraData extra; |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 146 | int i, name_size, id_str_size, snapshots_size; |
Kevin Wolf | d69969c | 2011-11-18 18:27:00 +0100 | [diff] [blame] | 147 | struct { |
| 148 | uint32_t nb_snapshots; |
| 149 | uint64_t snapshots_offset; |
| 150 | } QEMU_PACKED header_data; |
Kevin Wolf | 5dae6e3 | 2014-03-26 13:06:07 +0100 | [diff] [blame] | 151 | int64_t offset, snapshots_offset = 0; |
Kevin Wolf | 07fd877 | 2011-11-16 12:00:59 +0100 | [diff] [blame] | 152 | int ret; |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 153 | |
| 154 | /* compute the size of the snapshots */ |
| 155 | offset = 0; |
| 156 | for(i = 0; i < s->nb_snapshots; i++) { |
| 157 | sn = s->snapshots + i; |
| 158 | offset = align_offset(offset, 8); |
| 159 | offset += sizeof(h); |
Kevin Wolf | c2c9a46 | 2011-11-16 11:35:54 +0100 | [diff] [blame] | 160 | offset += sizeof(extra); |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 161 | offset += strlen(sn->id_str); |
| 162 | offset += strlen(sn->name); |
Kevin Wolf | 5dae6e3 | 2014-03-26 13:06:07 +0100 | [diff] [blame] | 163 | |
| 164 | if (offset > QCOW_MAX_SNAPSHOTS_SIZE) { |
| 165 | ret = -EFBIG; |
| 166 | goto fail; |
| 167 | } |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 168 | } |
Kevin Wolf | 5dae6e3 | 2014-03-26 13:06:07 +0100 | [diff] [blame] | 169 | |
| 170 | assert(offset <= INT_MAX); |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 171 | snapshots_size = offset; |
| 172 | |
Kevin Wolf | 07fd877 | 2011-11-16 12:00:59 +0100 | [diff] [blame] | 173 | /* Allocate space for the new snapshot list */ |
Kevin Wolf | ed6ccf0 | 2009-05-28 16:07:07 +0200 | [diff] [blame] | 174 | snapshots_offset = qcow2_alloc_clusters(bs, snapshots_size); |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 175 | offset = snapshots_offset; |
Kevin Wolf | 5d757b5 | 2010-01-20 15:04:01 +0100 | [diff] [blame] | 176 | if (offset < 0) { |
Max Reitz | 37d41f0 | 2013-10-09 10:51:04 +0200 | [diff] [blame] | 177 | ret = offset; |
| 178 | goto fail; |
Kevin Wolf | 5d757b5 | 2010-01-20 15:04:01 +0100 | [diff] [blame] | 179 | } |
Stefan Hajnoczi | f6977f1 | 2013-03-04 15:02:31 +0100 | [diff] [blame] | 180 | ret = bdrv_flush(bs); |
| 181 | if (ret < 0) { |
Max Reitz | 37d41f0 | 2013-10-09 10:51:04 +0200 | [diff] [blame] | 182 | goto fail; |
Stefan Hajnoczi | f6977f1 | 2013-03-04 15:02:31 +0100 | [diff] [blame] | 183 | } |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 184 | |
Max Reitz | cf93980 | 2013-08-30 14:34:26 +0200 | [diff] [blame] | 185 | /* The snapshot list position has not yet been updated, so these clusters |
| 186 | * must indeed be completely free */ |
Max Reitz | 231bb26 | 2013-10-10 11:09:23 +0200 | [diff] [blame] | 187 | ret = qcow2_pre_write_overlap_check(bs, 0, offset, snapshots_size); |
Max Reitz | cf93980 | 2013-08-30 14:34:26 +0200 | [diff] [blame] | 188 | if (ret < 0) { |
Max Reitz | 37d41f0 | 2013-10-09 10:51:04 +0200 | [diff] [blame] | 189 | goto fail; |
Max Reitz | cf93980 | 2013-08-30 14:34:26 +0200 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | |
Kevin Wolf | 07fd877 | 2011-11-16 12:00:59 +0100 | [diff] [blame] | 193 | /* Write all snapshots to the new list */ |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 194 | for(i = 0; i < s->nb_snapshots; i++) { |
| 195 | sn = s->snapshots + i; |
| 196 | memset(&h, 0, sizeof(h)); |
| 197 | h.l1_table_offset = cpu_to_be64(sn->l1_table_offset); |
| 198 | h.l1_size = cpu_to_be32(sn->l1_size); |
Kevin Wolf | c2c9a46 | 2011-11-16 11:35:54 +0100 | [diff] [blame] | 199 | /* If it doesn't fit in 32 bit, older implementations should treat it |
| 200 | * as a disk-only snapshot rather than truncate the VM state */ |
| 201 | if (sn->vm_state_size <= 0xffffffff) { |
| 202 | h.vm_state_size = cpu_to_be32(sn->vm_state_size); |
| 203 | } |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 204 | h.date_sec = cpu_to_be32(sn->date_sec); |
| 205 | h.date_nsec = cpu_to_be32(sn->date_nsec); |
| 206 | h.vm_clock_nsec = cpu_to_be64(sn->vm_clock_nsec); |
Kevin Wolf | c2c9a46 | 2011-11-16 11:35:54 +0100 | [diff] [blame] | 207 | h.extra_data_size = cpu_to_be32(sizeof(extra)); |
| 208 | |
| 209 | memset(&extra, 0, sizeof(extra)); |
| 210 | extra.vm_state_size_large = cpu_to_be64(sn->vm_state_size); |
Kevin Wolf | 90b2775 | 2012-04-11 16:33:50 +0200 | [diff] [blame] | 211 | extra.disk_size = cpu_to_be64(sn->disk_size); |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 212 | |
| 213 | id_str_size = strlen(sn->id_str); |
| 214 | name_size = strlen(sn->name); |
Max Reitz | 88fb153 | 2013-10-09 10:51:06 +0200 | [diff] [blame] | 215 | assert(id_str_size <= UINT16_MAX && name_size <= UINT16_MAX); |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 216 | h.id_str_size = cpu_to_be16(id_str_size); |
| 217 | h.name_size = cpu_to_be16(name_size); |
| 218 | offset = align_offset(offset, 8); |
Kevin Wolf | 07fd877 | 2011-11-16 12:00:59 +0100 | [diff] [blame] | 219 | |
Kevin Wolf | d9ca2ea | 2016-06-20 20:09:15 +0200 | [diff] [blame] | 220 | ret = bdrv_pwrite(bs->file, offset, &h, sizeof(h)); |
Kevin Wolf | 07fd877 | 2011-11-16 12:00:59 +0100 | [diff] [blame] | 221 | if (ret < 0) { |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 222 | goto fail; |
Kevin Wolf | 07fd877 | 2011-11-16 12:00:59 +0100 | [diff] [blame] | 223 | } |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 224 | offset += sizeof(h); |
Kevin Wolf | 07fd877 | 2011-11-16 12:00:59 +0100 | [diff] [blame] | 225 | |
Kevin Wolf | d9ca2ea | 2016-06-20 20:09:15 +0200 | [diff] [blame] | 226 | ret = bdrv_pwrite(bs->file, offset, &extra, sizeof(extra)); |
Kevin Wolf | c2c9a46 | 2011-11-16 11:35:54 +0100 | [diff] [blame] | 227 | if (ret < 0) { |
| 228 | goto fail; |
| 229 | } |
| 230 | offset += sizeof(extra); |
| 231 | |
Kevin Wolf | d9ca2ea | 2016-06-20 20:09:15 +0200 | [diff] [blame] | 232 | ret = bdrv_pwrite(bs->file, offset, sn->id_str, id_str_size); |
Kevin Wolf | 07fd877 | 2011-11-16 12:00:59 +0100 | [diff] [blame] | 233 | if (ret < 0) { |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 234 | goto fail; |
Kevin Wolf | 07fd877 | 2011-11-16 12:00:59 +0100 | [diff] [blame] | 235 | } |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 236 | offset += id_str_size; |
Kevin Wolf | 07fd877 | 2011-11-16 12:00:59 +0100 | [diff] [blame] | 237 | |
Kevin Wolf | d9ca2ea | 2016-06-20 20:09:15 +0200 | [diff] [blame] | 238 | ret = bdrv_pwrite(bs->file, offset, sn->name, name_size); |
Kevin Wolf | 07fd877 | 2011-11-16 12:00:59 +0100 | [diff] [blame] | 239 | if (ret < 0) { |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 240 | goto fail; |
Kevin Wolf | 07fd877 | 2011-11-16 12:00:59 +0100 | [diff] [blame] | 241 | } |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 242 | offset += name_size; |
| 243 | } |
| 244 | |
Kevin Wolf | 07fd877 | 2011-11-16 12:00:59 +0100 | [diff] [blame] | 245 | /* |
| 246 | * Update the header to point to the new snapshot table. This requires the |
| 247 | * new table and its refcounts to be stable on disk. |
Kevin Wolf | 07fd877 | 2011-11-16 12:00:59 +0100 | [diff] [blame] | 248 | */ |
| 249 | ret = bdrv_flush(bs); |
| 250 | if (ret < 0) { |
| 251 | goto fail; |
| 252 | } |
| 253 | |
Kevin Wolf | d69969c | 2011-11-18 18:27:00 +0100 | [diff] [blame] | 254 | QEMU_BUILD_BUG_ON(offsetof(QCowHeader, snapshots_offset) != |
| 255 | offsetof(QCowHeader, nb_snapshots) + sizeof(header_data.nb_snapshots)); |
Kevin Wolf | 07fd877 | 2011-11-16 12:00:59 +0100 | [diff] [blame] | 256 | |
Kevin Wolf | d69969c | 2011-11-18 18:27:00 +0100 | [diff] [blame] | 257 | header_data.nb_snapshots = cpu_to_be32(s->nb_snapshots); |
| 258 | header_data.snapshots_offset = cpu_to_be64(snapshots_offset); |
| 259 | |
Kevin Wolf | d9ca2ea | 2016-06-20 20:09:15 +0200 | [diff] [blame] | 260 | ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, nb_snapshots), |
Kevin Wolf | d69969c | 2011-11-18 18:27:00 +0100 | [diff] [blame] | 261 | &header_data, sizeof(header_data)); |
Kevin Wolf | 07fd877 | 2011-11-16 12:00:59 +0100 | [diff] [blame] | 262 | if (ret < 0) { |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 263 | goto fail; |
Kevin Wolf | 07fd877 | 2011-11-16 12:00:59 +0100 | [diff] [blame] | 264 | } |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 265 | |
| 266 | /* free the old snapshot table */ |
Kevin Wolf | 6cfcb9b | 2013-06-19 13:44:18 +0200 | [diff] [blame] | 267 | qcow2_free_clusters(bs, s->snapshots_offset, s->snapshots_size, |
| 268 | QCOW2_DISCARD_SNAPSHOT); |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 269 | s->snapshots_offset = snapshots_offset; |
| 270 | s->snapshots_size = snapshots_size; |
| 271 | return 0; |
Kevin Wolf | 07fd877 | 2011-11-16 12:00:59 +0100 | [diff] [blame] | 272 | |
| 273 | fail: |
Max Reitz | 9186ad9 | 2013-10-09 10:51:05 +0200 | [diff] [blame] | 274 | if (snapshots_offset > 0) { |
| 275 | qcow2_free_clusters(bs, snapshots_offset, snapshots_size, |
| 276 | QCOW2_DISCARD_ALWAYS); |
| 277 | } |
Kevin Wolf | 07fd877 | 2011-11-16 12:00:59 +0100 | [diff] [blame] | 278 | return ret; |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | static void find_new_snapshot_id(BlockDriverState *bs, |
| 282 | char *id_str, int id_str_size) |
| 283 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 284 | BDRVQcow2State *s = bs->opaque; |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 285 | QCowSnapshot *sn; |
Max Reitz | 00c49b2 | 2013-10-09 14:42:47 +0200 | [diff] [blame] | 286 | int i; |
| 287 | unsigned long id, id_max = 0; |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 288 | |
| 289 | for(i = 0; i < s->nb_snapshots; i++) { |
| 290 | sn = s->snapshots + i; |
| 291 | id = strtoul(sn->id_str, NULL, 10); |
| 292 | if (id > id_max) |
| 293 | id_max = id; |
| 294 | } |
Max Reitz | 00c49b2 | 2013-10-09 14:42:47 +0200 | [diff] [blame] | 295 | snprintf(id_str, id_str_size, "%lu", id_max + 1); |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 296 | } |
| 297 | |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 298 | static int find_snapshot_by_id_and_name(BlockDriverState *bs, |
| 299 | const char *id, |
| 300 | const char *name) |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 301 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 302 | BDRVQcow2State *s = bs->opaque; |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 303 | int i; |
| 304 | |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 305 | if (id && name) { |
| 306 | for (i = 0; i < s->nb_snapshots; i++) { |
| 307 | if (!strcmp(s->snapshots[i].id_str, id) && |
| 308 | !strcmp(s->snapshots[i].name, name)) { |
| 309 | return i; |
| 310 | } |
| 311 | } |
| 312 | } else if (id) { |
| 313 | for (i = 0; i < s->nb_snapshots; i++) { |
| 314 | if (!strcmp(s->snapshots[i].id_str, id)) { |
| 315 | return i; |
| 316 | } |
| 317 | } |
| 318 | } else if (name) { |
| 319 | for (i = 0; i < s->nb_snapshots; i++) { |
| 320 | if (!strcmp(s->snapshots[i].name, name)) { |
| 321 | return i; |
| 322 | } |
| 323 | } |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 324 | } |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 325 | |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 326 | return -1; |
| 327 | } |
| 328 | |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 329 | static int find_snapshot_by_id_or_name(BlockDriverState *bs, |
| 330 | const char *id_or_name) |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 331 | { |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 332 | int ret; |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 333 | |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 334 | ret = find_snapshot_by_id_and_name(bs, id_or_name, NULL); |
| 335 | if (ret >= 0) { |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 336 | return ret; |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 337 | } |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 338 | return find_snapshot_by_id_and_name(bs, NULL, id_or_name); |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | /* if no id is provided, a new one is constructed */ |
Kevin Wolf | ed6ccf0 | 2009-05-28 16:07:07 +0200 | [diff] [blame] | 342 | int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info) |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 343 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 344 | BDRVQcow2State *s = bs->opaque; |
Kevin Wolf | d1ea98d | 2011-11-16 12:43:59 +0100 | [diff] [blame] | 345 | QCowSnapshot *new_snapshot_list = NULL; |
| 346 | QCowSnapshot *old_snapshot_list = NULL; |
| 347 | QCowSnapshot sn1, *sn = &sn1; |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 348 | int i, ret; |
| 349 | uint64_t *l1_table = NULL; |
Kevin Wolf | 5d757b5 | 2010-01-20 15:04:01 +0100 | [diff] [blame] | 350 | int64_t l1_table_offset; |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 351 | |
Kevin Wolf | ce48f2f4 | 2014-03-26 13:05:45 +0100 | [diff] [blame] | 352 | if (s->nb_snapshots >= QCOW_MAX_SNAPSHOTS) { |
| 353 | return -EFBIG; |
| 354 | } |
| 355 | |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 356 | memset(sn, 0, sizeof(*sn)); |
| 357 | |
Yi Wang | 407bc15 | 2015-03-12 22:54:42 +0800 | [diff] [blame] | 358 | /* Generate an ID */ |
| 359 | find_new_snapshot_id(bs, sn_info->id_str, sizeof(sn_info->id_str)); |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 360 | |
Kevin Wolf | 0334316 | 2011-11-16 17:46:29 +0100 | [diff] [blame] | 361 | /* Check that the ID is unique */ |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 362 | if (find_snapshot_by_id_and_name(bs, sn_info->id_str, NULL) >= 0) { |
Zhi Yong Wu | 647cc47 | 2012-04-26 16:11:37 +0800 | [diff] [blame] | 363 | return -EEXIST; |
Kevin Wolf | 0334316 | 2011-11-16 17:46:29 +0100 | [diff] [blame] | 364 | } |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 365 | |
Kevin Wolf | 0334316 | 2011-11-16 17:46:29 +0100 | [diff] [blame] | 366 | /* Populate sn with passed data */ |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 367 | sn->id_str = g_strdup(sn_info->id_str); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 368 | sn->name = g_strdup(sn_info->name); |
Kevin Wolf | 0334316 | 2011-11-16 17:46:29 +0100 | [diff] [blame] | 369 | |
Kevin Wolf | 90b2775 | 2012-04-11 16:33:50 +0200 | [diff] [blame] | 370 | sn->disk_size = bs->total_sectors * BDRV_SECTOR_SIZE; |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 371 | sn->vm_state_size = sn_info->vm_state_size; |
| 372 | sn->date_sec = sn_info->date_sec; |
| 373 | sn->date_nsec = sn_info->date_nsec; |
| 374 | sn->vm_clock_nsec = sn_info->vm_clock_nsec; |
| 375 | |
Kevin Wolf | 0334316 | 2011-11-16 17:46:29 +0100 | [diff] [blame] | 376 | /* Allocate the L1 table of the snapshot and copy the current one there. */ |
Kevin Wolf | 5d757b5 | 2010-01-20 15:04:01 +0100 | [diff] [blame] | 377 | l1_table_offset = qcow2_alloc_clusters(bs, s->l1_size * sizeof(uint64_t)); |
| 378 | if (l1_table_offset < 0) { |
Kevin Wolf | d1ea98d | 2011-11-16 12:43:59 +0100 | [diff] [blame] | 379 | ret = l1_table_offset; |
Kevin Wolf | 5d757b5 | 2010-01-20 15:04:01 +0100 | [diff] [blame] | 380 | goto fail; |
| 381 | } |
| 382 | |
| 383 | sn->l1_table_offset = l1_table_offset; |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 384 | sn->l1_size = s->l1_size; |
| 385 | |
Markus Armbruster | 5839e53 | 2014-08-19 10:31:08 +0200 | [diff] [blame] | 386 | l1_table = g_try_new(uint64_t, s->l1_size); |
Kevin Wolf | de82815 | 2014-05-20 17:12:47 +0200 | [diff] [blame] | 387 | if (s->l1_size && l1_table == NULL) { |
| 388 | ret = -ENOMEM; |
| 389 | goto fail; |
| 390 | } |
| 391 | |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 392 | for(i = 0; i < s->l1_size; i++) { |
| 393 | l1_table[i] = cpu_to_be64(s->l1_table[i]); |
| 394 | } |
Kevin Wolf | d1ea98d | 2011-11-16 12:43:59 +0100 | [diff] [blame] | 395 | |
Max Reitz | 231bb26 | 2013-10-10 11:09:23 +0200 | [diff] [blame] | 396 | ret = qcow2_pre_write_overlap_check(bs, 0, sn->l1_table_offset, |
| 397 | s->l1_size * sizeof(uint64_t)); |
Max Reitz | cf93980 | 2013-08-30 14:34:26 +0200 | [diff] [blame] | 398 | if (ret < 0) { |
| 399 | goto fail; |
| 400 | } |
| 401 | |
Kevin Wolf | d9ca2ea | 2016-06-20 20:09:15 +0200 | [diff] [blame] | 402 | ret = bdrv_pwrite(bs->file, sn->l1_table_offset, l1_table, |
Kevin Wolf | d1ea98d | 2011-11-16 12:43:59 +0100 | [diff] [blame] | 403 | s->l1_size * sizeof(uint64_t)); |
| 404 | if (ret < 0) { |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 405 | goto fail; |
Kevin Wolf | d1ea98d | 2011-11-16 12:43:59 +0100 | [diff] [blame] | 406 | } |
| 407 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 408 | g_free(l1_table); |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 409 | l1_table = NULL; |
| 410 | |
Kevin Wolf | d1ea98d | 2011-11-16 12:43:59 +0100 | [diff] [blame] | 411 | /* |
| 412 | * Increase the refcounts of all clusters and make sure everything is |
| 413 | * stable on disk before updating the snapshot table to contain a pointer |
| 414 | * to the new L1 table. |
| 415 | */ |
| 416 | ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1); |
| 417 | if (ret < 0) { |
| 418 | goto fail; |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 419 | } |
Kevin Wolf | d1ea98d | 2011-11-16 12:43:59 +0100 | [diff] [blame] | 420 | |
Kevin Wolf | d1ea98d | 2011-11-16 12:43:59 +0100 | [diff] [blame] | 421 | /* Append the new snapshot to the snapshot list */ |
Markus Armbruster | 5839e53 | 2014-08-19 10:31:08 +0200 | [diff] [blame] | 422 | new_snapshot_list = g_new(QCowSnapshot, s->nb_snapshots + 1); |
Kevin Wolf | d1ea98d | 2011-11-16 12:43:59 +0100 | [diff] [blame] | 423 | if (s->snapshots) { |
| 424 | memcpy(new_snapshot_list, s->snapshots, |
| 425 | s->nb_snapshots * sizeof(QCowSnapshot)); |
| 426 | old_snapshot_list = s->snapshots; |
| 427 | } |
| 428 | s->snapshots = new_snapshot_list; |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 429 | s->snapshots[s->nb_snapshots++] = *sn; |
| 430 | |
Kevin Wolf | d1ea98d | 2011-11-16 12:43:59 +0100 | [diff] [blame] | 431 | ret = qcow2_write_snapshots(bs); |
| 432 | if (ret < 0) { |
| 433 | g_free(s->snapshots); |
| 434 | s->snapshots = old_snapshot_list; |
Max Reitz | 84757f7 | 2013-10-09 14:42:00 +0200 | [diff] [blame] | 435 | s->nb_snapshots--; |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 436 | goto fail; |
Kevin Wolf | d1ea98d | 2011-11-16 12:43:59 +0100 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | g_free(old_snapshot_list); |
| 440 | |
Kevin Wolf | 1ebf561 | 2013-09-06 12:20:08 +0200 | [diff] [blame] | 441 | /* The VM state isn't needed any more in the active L1 table; in fact, it |
| 442 | * hurts by causing expensive COW for the next snapshot. */ |
Eric Blake | d2cb36a | 2017-05-06 19:05:52 -0500 | [diff] [blame] | 443 | qcow2_cluster_discard(bs, qcow2_vm_state_offset(s), |
| 444 | align_offset(sn->vm_state_size, s->cluster_size), |
| 445 | QCOW2_DISCARD_NEVER, false); |
Kevin Wolf | 1ebf561 | 2013-09-06 12:20:08 +0200 | [diff] [blame] | 446 | |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 447 | #ifdef DEBUG_ALLOC |
Philipp Hahn | 6cbc303 | 2011-08-04 19:22:10 +0200 | [diff] [blame] | 448 | { |
| 449 | BdrvCheckResult result = {0}; |
Stefan Hajnoczi | b35278f | 2012-06-15 16:41:07 +0100 | [diff] [blame] | 450 | qcow2_check_refcounts(bs, &result, 0); |
Philipp Hahn | 6cbc303 | 2011-08-04 19:22:10 +0200 | [diff] [blame] | 451 | } |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 452 | #endif |
| 453 | return 0; |
Kevin Wolf | 0334316 | 2011-11-16 17:46:29 +0100 | [diff] [blame] | 454 | |
| 455 | fail: |
| 456 | g_free(sn->id_str); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 457 | g_free(sn->name); |
| 458 | g_free(l1_table); |
Kevin Wolf | d1ea98d | 2011-11-16 12:43:59 +0100 | [diff] [blame] | 459 | |
| 460 | return ret; |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | /* copy the snapshot 'snapshot_name' into the current disk image */ |
Kevin Wolf | ed6ccf0 | 2009-05-28 16:07:07 +0200 | [diff] [blame] | 464 | int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id) |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 465 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 466 | BDRVQcow2State *s = bs->opaque; |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 467 | QCowSnapshot *sn; |
Kevin Wolf | 35d7ace | 2011-08-05 12:06:11 +0200 | [diff] [blame] | 468 | int i, snapshot_index; |
| 469 | int cur_l1_bytes, sn_l1_bytes; |
Kevin Wolf | 589f284 | 2011-11-16 15:04:11 +0100 | [diff] [blame] | 470 | int ret; |
Kevin Wolf | 43a0cac | 2011-11-16 15:20:45 +0100 | [diff] [blame] | 471 | uint64_t *sn_l1_table = NULL; |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 472 | |
Kevin Wolf | 589f284 | 2011-11-16 15:04:11 +0100 | [diff] [blame] | 473 | /* Search the snapshot */ |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 474 | snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id); |
Kevin Wolf | 589f284 | 2011-11-16 15:04:11 +0100 | [diff] [blame] | 475 | if (snapshot_index < 0) { |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 476 | return -ENOENT; |
Kevin Wolf | 589f284 | 2011-11-16 15:04:11 +0100 | [diff] [blame] | 477 | } |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 478 | sn = &s->snapshots[snapshot_index]; |
| 479 | |
Kevin Wolf | 90b2775 | 2012-04-11 16:33:50 +0200 | [diff] [blame] | 480 | if (sn->disk_size != bs->total_sectors * BDRV_SECTOR_SIZE) { |
| 481 | error_report("qcow2: Loading snapshots with different disk " |
| 482 | "size is not implemented"); |
| 483 | ret = -ENOTSUP; |
| 484 | goto fail; |
| 485 | } |
| 486 | |
Kevin Wolf | 589f284 | 2011-11-16 15:04:11 +0100 | [diff] [blame] | 487 | /* |
| 488 | * Make sure that the current L1 table is big enough to contain the whole |
| 489 | * L1 table of the snapshot. If the snapshot L1 table is smaller, the |
| 490 | * current one must be padded with zeros. |
| 491 | */ |
| 492 | ret = qcow2_grow_l1_table(bs, sn->l1_size, true); |
| 493 | if (ret < 0) { |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 494 | goto fail; |
Kevin Wolf | 589f284 | 2011-11-16 15:04:11 +0100 | [diff] [blame] | 495 | } |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 496 | |
Kevin Wolf | 35d7ace | 2011-08-05 12:06:11 +0200 | [diff] [blame] | 497 | cur_l1_bytes = s->l1_size * sizeof(uint64_t); |
| 498 | sn_l1_bytes = sn->l1_size * sizeof(uint64_t); |
| 499 | |
Kevin Wolf | 589f284 | 2011-11-16 15:04:11 +0100 | [diff] [blame] | 500 | /* |
| 501 | * Copy the snapshot L1 table to the current L1 table. |
| 502 | * |
| 503 | * Before overwriting the old current L1 table on disk, make sure to |
| 504 | * increase all refcounts for the clusters referenced by the new one. |
Kevin Wolf | 43a0cac | 2011-11-16 15:20:45 +0100 | [diff] [blame] | 505 | * Decrease the refcount referenced by the old one only when the L1 |
| 506 | * table is overwritten. |
Kevin Wolf | 589f284 | 2011-11-16 15:04:11 +0100 | [diff] [blame] | 507 | */ |
Kevin Wolf | de82815 | 2014-05-20 17:12:47 +0200 | [diff] [blame] | 508 | sn_l1_table = g_try_malloc0(cur_l1_bytes); |
| 509 | if (cur_l1_bytes && sn_l1_table == NULL) { |
| 510 | ret = -ENOMEM; |
| 511 | goto fail; |
| 512 | } |
Kevin Wolf | 43a0cac | 2011-11-16 15:20:45 +0100 | [diff] [blame] | 513 | |
Kevin Wolf | cf2ab8f | 2016-06-20 18:24:02 +0200 | [diff] [blame] | 514 | ret = bdrv_pread(bs->file, sn->l1_table_offset, |
Kevin Wolf | 9a4f4c3 | 2015-06-16 14:19:22 +0200 | [diff] [blame] | 515 | sn_l1_table, sn_l1_bytes); |
Kevin Wolf | 589f284 | 2011-11-16 15:04:11 +0100 | [diff] [blame] | 516 | if (ret < 0) { |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 517 | goto fail; |
Kevin Wolf | 589f284 | 2011-11-16 15:04:11 +0100 | [diff] [blame] | 518 | } |
| 519 | |
Kevin Wolf | 43a0cac | 2011-11-16 15:20:45 +0100 | [diff] [blame] | 520 | ret = qcow2_update_snapshot_refcount(bs, sn->l1_table_offset, |
| 521 | sn->l1_size, 1); |
| 522 | if (ret < 0) { |
| 523 | goto fail; |
| 524 | } |
| 525 | |
Max Reitz | 231bb26 | 2013-10-10 11:09:23 +0200 | [diff] [blame] | 526 | ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L1, |
| 527 | s->l1_table_offset, cur_l1_bytes); |
Max Reitz | cf93980 | 2013-08-30 14:34:26 +0200 | [diff] [blame] | 528 | if (ret < 0) { |
| 529 | goto fail; |
| 530 | } |
| 531 | |
Kevin Wolf | d9ca2ea | 2016-06-20 20:09:15 +0200 | [diff] [blame] | 532 | ret = bdrv_pwrite_sync(bs->file, s->l1_table_offset, sn_l1_table, |
Kevin Wolf | 589f284 | 2011-11-16 15:04:11 +0100 | [diff] [blame] | 533 | cur_l1_bytes); |
| 534 | if (ret < 0) { |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 535 | goto fail; |
Kevin Wolf | 589f284 | 2011-11-16 15:04:11 +0100 | [diff] [blame] | 536 | } |
| 537 | |
Kevin Wolf | 43a0cac | 2011-11-16 15:20:45 +0100 | [diff] [blame] | 538 | /* |
| 539 | * Decrease refcount of clusters of current L1 table. |
| 540 | * |
| 541 | * At this point, the in-memory s->l1_table points to the old L1 table, |
| 542 | * whereas on disk we already have the new one. |
| 543 | * |
| 544 | * qcow2_update_snapshot_refcount special cases the current L1 table to use |
| 545 | * the in-memory data instead of really using the offset to load a new one, |
| 546 | * which is why this works. |
| 547 | */ |
| 548 | ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, |
| 549 | s->l1_size, -1); |
| 550 | |
| 551 | /* |
| 552 | * Now update the in-memory L1 table to be in sync with the on-disk one. We |
| 553 | * need to do this even if updating refcounts failed. |
| 554 | */ |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 555 | for(i = 0;i < s->l1_size; i++) { |
Kevin Wolf | 43a0cac | 2011-11-16 15:20:45 +0100 | [diff] [blame] | 556 | s->l1_table[i] = be64_to_cpu(sn_l1_table[i]); |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 557 | } |
| 558 | |
Kevin Wolf | 43a0cac | 2011-11-16 15:20:45 +0100 | [diff] [blame] | 559 | if (ret < 0) { |
| 560 | goto fail; |
| 561 | } |
| 562 | |
| 563 | g_free(sn_l1_table); |
| 564 | sn_l1_table = NULL; |
| 565 | |
| 566 | /* |
| 567 | * Update QCOW_OFLAG_COPIED in the active L1 table (it may have changed |
| 568 | * when we decreased the refcount of the old snapshot. |
| 569 | */ |
| 570 | ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0); |
Kevin Wolf | 589f284 | 2011-11-16 15:04:11 +0100 | [diff] [blame] | 571 | if (ret < 0) { |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 572 | goto fail; |
Kevin Wolf | 589f284 | 2011-11-16 15:04:11 +0100 | [diff] [blame] | 573 | } |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 574 | |
| 575 | #ifdef DEBUG_ALLOC |
Philipp Hahn | 6cbc303 | 2011-08-04 19:22:10 +0200 | [diff] [blame] | 576 | { |
| 577 | BdrvCheckResult result = {0}; |
Stefan Hajnoczi | b35278f | 2012-06-15 16:41:07 +0100 | [diff] [blame] | 578 | qcow2_check_refcounts(bs, &result, 0); |
Philipp Hahn | 6cbc303 | 2011-08-04 19:22:10 +0200 | [diff] [blame] | 579 | } |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 580 | #endif |
| 581 | return 0; |
Kevin Wolf | 589f284 | 2011-11-16 15:04:11 +0100 | [diff] [blame] | 582 | |
| 583 | fail: |
Kevin Wolf | 43a0cac | 2011-11-16 15:20:45 +0100 | [diff] [blame] | 584 | g_free(sn_l1_table); |
Kevin Wolf | 589f284 | 2011-11-16 15:04:11 +0100 | [diff] [blame] | 585 | return ret; |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 586 | } |
| 587 | |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 588 | int qcow2_snapshot_delete(BlockDriverState *bs, |
| 589 | const char *snapshot_id, |
| 590 | const char *name, |
| 591 | Error **errp) |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 592 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 593 | BDRVQcow2State *s = bs->opaque; |
Kevin Wolf | 9a47678 | 2011-11-16 17:22:10 +0100 | [diff] [blame] | 594 | QCowSnapshot sn; |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 595 | int snapshot_index, ret; |
| 596 | |
Kevin Wolf | 9a47678 | 2011-11-16 17:22:10 +0100 | [diff] [blame] | 597 | /* Search the snapshot */ |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 598 | snapshot_index = find_snapshot_by_id_and_name(bs, snapshot_id, name); |
Kevin Wolf | 9a47678 | 2011-11-16 17:22:10 +0100 | [diff] [blame] | 599 | if (snapshot_index < 0) { |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 600 | error_setg(errp, "Can't find the snapshot"); |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 601 | return -ENOENT; |
Kevin Wolf | 9a47678 | 2011-11-16 17:22:10 +0100 | [diff] [blame] | 602 | } |
| 603 | sn = s->snapshots[snapshot_index]; |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 604 | |
Kevin Wolf | 9a47678 | 2011-11-16 17:22:10 +0100 | [diff] [blame] | 605 | /* Remove it from the snapshot list */ |
| 606 | memmove(s->snapshots + snapshot_index, |
| 607 | s->snapshots + snapshot_index + 1, |
| 608 | (s->nb_snapshots - snapshot_index - 1) * sizeof(sn)); |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 609 | s->nb_snapshots--; |
Jes Sorensen | 7c80ab3 | 2010-12-17 16:02:39 +0100 | [diff] [blame] | 610 | ret = qcow2_write_snapshots(bs); |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 611 | if (ret < 0) { |
Jeff Cody | 39a611a | 2014-02-12 14:46:24 -0500 | [diff] [blame] | 612 | error_setg_errno(errp, -ret, |
| 613 | "Failed to remove snapshot from snapshot list"); |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 614 | return ret; |
| 615 | } |
Kevin Wolf | 9a47678 | 2011-11-16 17:22:10 +0100 | [diff] [blame] | 616 | |
| 617 | /* |
| 618 | * The snapshot is now unused, clean up. If we fail after this point, we |
| 619 | * won't recover but just leak clusters. |
| 620 | */ |
| 621 | g_free(sn.id_str); |
| 622 | g_free(sn.name); |
| 623 | |
| 624 | /* |
| 625 | * Now decrease the refcounts of clusters referenced by the snapshot and |
| 626 | * free the L1 table. |
| 627 | */ |
| 628 | ret = qcow2_update_snapshot_refcount(bs, sn.l1_table_offset, |
| 629 | sn.l1_size, -1); |
| 630 | if (ret < 0) { |
Jeff Cody | 39a611a | 2014-02-12 14:46:24 -0500 | [diff] [blame] | 631 | error_setg_errno(errp, -ret, "Failed to free the cluster and L1 table"); |
Kevin Wolf | 9a47678 | 2011-11-16 17:22:10 +0100 | [diff] [blame] | 632 | return ret; |
| 633 | } |
Kevin Wolf | 6cfcb9b | 2013-06-19 13:44:18 +0200 | [diff] [blame] | 634 | qcow2_free_clusters(bs, sn.l1_table_offset, sn.l1_size * sizeof(uint64_t), |
| 635 | QCOW2_DISCARD_SNAPSHOT); |
Kevin Wolf | 9a47678 | 2011-11-16 17:22:10 +0100 | [diff] [blame] | 636 | |
| 637 | /* must update the copied flag on the current cluster offsets */ |
| 638 | ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0); |
| 639 | if (ret < 0) { |
Jeff Cody | 39a611a | 2014-02-12 14:46:24 -0500 | [diff] [blame] | 640 | error_setg_errno(errp, -ret, |
| 641 | "Failed to update snapshot status in disk"); |
Kevin Wolf | 9a47678 | 2011-11-16 17:22:10 +0100 | [diff] [blame] | 642 | return ret; |
| 643 | } |
| 644 | |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 645 | #ifdef DEBUG_ALLOC |
Philipp Hahn | 6cbc303 | 2011-08-04 19:22:10 +0200 | [diff] [blame] | 646 | { |
| 647 | BdrvCheckResult result = {0}; |
Stefan Hajnoczi | b35278f | 2012-06-15 16:41:07 +0100 | [diff] [blame] | 648 | qcow2_check_refcounts(bs, &result, 0); |
Philipp Hahn | 6cbc303 | 2011-08-04 19:22:10 +0200 | [diff] [blame] | 649 | } |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 650 | #endif |
| 651 | return 0; |
| 652 | } |
| 653 | |
Kevin Wolf | ed6ccf0 | 2009-05-28 16:07:07 +0200 | [diff] [blame] | 654 | int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab) |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 655 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 656 | BDRVQcow2State *s = bs->opaque; |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 657 | QEMUSnapshotInfo *sn_tab, *sn_info; |
| 658 | QCowSnapshot *sn; |
| 659 | int i; |
| 660 | |
| 661 | if (!s->nb_snapshots) { |
| 662 | *psn_tab = NULL; |
| 663 | return s->nb_snapshots; |
| 664 | } |
| 665 | |
Markus Armbruster | 5839e53 | 2014-08-19 10:31:08 +0200 | [diff] [blame] | 666 | sn_tab = g_new0(QEMUSnapshotInfo, s->nb_snapshots); |
Kevin Wolf | c142442 | 2009-05-28 16:07:06 +0200 | [diff] [blame] | 667 | for(i = 0; i < s->nb_snapshots; i++) { |
| 668 | sn_info = sn_tab + i; |
| 669 | sn = s->snapshots + i; |
| 670 | pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), |
| 671 | sn->id_str); |
| 672 | pstrcpy(sn_info->name, sizeof(sn_info->name), |
| 673 | sn->name); |
| 674 | sn_info->vm_state_size = sn->vm_state_size; |
| 675 | sn_info->date_sec = sn->date_sec; |
| 676 | sn_info->date_nsec = sn->date_nsec; |
| 677 | sn_info->vm_clock_nsec = sn->vm_clock_nsec; |
| 678 | } |
| 679 | *psn_tab = sn_tab; |
| 680 | return s->nb_snapshots; |
| 681 | } |
| 682 | |
Wenchao Xia | 7b4c478 | 2013-12-04 17:10:54 +0800 | [diff] [blame] | 683 | int qcow2_snapshot_load_tmp(BlockDriverState *bs, |
| 684 | const char *snapshot_id, |
| 685 | const char *name, |
| 686 | Error **errp) |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 687 | { |
Kevin Wolf | e3f652b | 2011-11-16 17:30:33 +0100 | [diff] [blame] | 688 | int i, snapshot_index; |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 689 | BDRVQcow2State *s = bs->opaque; |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 690 | QCowSnapshot *sn; |
Kevin Wolf | e3f652b | 2011-11-16 17:30:33 +0100 | [diff] [blame] | 691 | uint64_t *new_l1_table; |
| 692 | int new_l1_bytes; |
| 693 | int ret; |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 694 | |
Kevin Wolf | e3f652b | 2011-11-16 17:30:33 +0100 | [diff] [blame] | 695 | assert(bs->read_only); |
| 696 | |
| 697 | /* Search the snapshot */ |
Wenchao Xia | 7b4c478 | 2013-12-04 17:10:54 +0800 | [diff] [blame] | 698 | snapshot_index = find_snapshot_by_id_and_name(bs, snapshot_id, name); |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 699 | if (snapshot_index < 0) { |
Wenchao Xia | 7b4c478 | 2013-12-04 17:10:54 +0800 | [diff] [blame] | 700 | error_setg(errp, |
| 701 | "Can't find snapshot"); |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 702 | return -ENOENT; |
| 703 | } |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 704 | sn = &s->snapshots[snapshot_index]; |
Kevin Wolf | e3f652b | 2011-11-16 17:30:33 +0100 | [diff] [blame] | 705 | |
| 706 | /* Allocate and read in the snapshot's L1 table */ |
Wen Congyang | 87b86e7 | 2015-03-11 11:05:21 +0800 | [diff] [blame] | 707 | if (sn->l1_size > QCOW_MAX_L1_SIZE / sizeof(uint64_t)) { |
Kevin Wolf | 6a83f8b | 2014-03-26 13:06:06 +0100 | [diff] [blame] | 708 | error_setg(errp, "Snapshot L1 table too large"); |
| 709 | return -EFBIG; |
| 710 | } |
Kevin Wolf | c05e466 | 2014-03-26 13:06:05 +0100 | [diff] [blame] | 711 | new_l1_bytes = sn->l1_size * sizeof(uint64_t); |
Kevin Wolf | 9a4f4c3 | 2015-06-16 14:19:22 +0200 | [diff] [blame] | 712 | new_l1_table = qemu_try_blockalign(bs->file->bs, |
Kevin Wolf | de82815 | 2014-05-20 17:12:47 +0200 | [diff] [blame] | 713 | align_offset(new_l1_bytes, 512)); |
| 714 | if (new_l1_table == NULL) { |
| 715 | return -ENOMEM; |
| 716 | } |
Kevin Wolf | e3f652b | 2011-11-16 17:30:33 +0100 | [diff] [blame] | 717 | |
Kevin Wolf | cf2ab8f | 2016-06-20 18:24:02 +0200 | [diff] [blame] | 718 | ret = bdrv_pread(bs->file, sn->l1_table_offset, |
Kevin Wolf | 9a4f4c3 | 2015-06-16 14:19:22 +0200 | [diff] [blame] | 719 | new_l1_table, new_l1_bytes); |
Kevin Wolf | e3f652b | 2011-11-16 17:30:33 +0100 | [diff] [blame] | 720 | if (ret < 0) { |
Wenchao Xia | 7b4c478 | 2013-12-04 17:10:54 +0800 | [diff] [blame] | 721 | error_setg(errp, "Failed to read l1 table for snapshot"); |
Kevin Wolf | de82815 | 2014-05-20 17:12:47 +0200 | [diff] [blame] | 722 | qemu_vfree(new_l1_table); |
Kevin Wolf | e3f652b | 2011-11-16 17:30:33 +0100 | [diff] [blame] | 723 | return ret; |
| 724 | } |
| 725 | |
| 726 | /* Switch the L1 table */ |
Kevin Wolf | de82815 | 2014-05-20 17:12:47 +0200 | [diff] [blame] | 727 | qemu_vfree(s->l1_table); |
Kevin Wolf | e3f652b | 2011-11-16 17:30:33 +0100 | [diff] [blame] | 728 | |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 729 | s->l1_size = sn->l1_size; |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 730 | s->l1_table_offset = sn->l1_table_offset; |
Kevin Wolf | e3f652b | 2011-11-16 17:30:33 +0100 | [diff] [blame] | 731 | s->l1_table = new_l1_table; |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 732 | |
| 733 | for(i = 0;i < s->l1_size; i++) { |
| 734 | be64_to_cpus(&s->l1_table[i]); |
| 735 | } |
Kevin Wolf | e3f652b | 2011-11-16 17:30:33 +0100 | [diff] [blame] | 736 | |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 737 | return 0; |
| 738 | } |