Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Block driver for the Virtual Disk Image (VDI) format |
| 3 | * |
Stefan Weil | 641543b | 2012-01-21 13:54:24 +0100 | [diff] [blame] | 4 | * Copyright (c) 2009, 2012 Stefan Weil |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 2 of the License, or |
| 9 | * (at your option) version 3 or any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * Reference: |
| 20 | * http://forums.virtualbox.org/viewtopic.php?t=8046 |
| 21 | * |
| 22 | * This driver supports create / read / write operations on VDI images. |
| 23 | * |
| 24 | * Todo (see also TODO in code): |
| 25 | * |
| 26 | * Some features like snapshots are still missing. |
| 27 | * |
| 28 | * Deallocation of zero-filled blocks and shrinking images are missing, too |
| 29 | * (might be added to common block layer). |
| 30 | * |
| 31 | * Allocation of blocks could be optimized (less writes to block map and |
| 32 | * header). |
| 33 | * |
Deepak Kathayat | dc6fb73 | 2014-03-24 16:30:17 +0800 | [diff] [blame] | 34 | * Read and write of adjacent blocks could be done in one operation |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 35 | * (current code uses one operation per block (1 MiB). |
| 36 | * |
| 37 | * The code is not thread safe (missing locks for changes in header and |
| 38 | * block table, no problem with current QEMU). |
| 39 | * |
| 40 | * Hints: |
| 41 | * |
| 42 | * Blocks (VDI documentation) correspond to clusters (QEMU). |
| 43 | * QEMU's backing files could be implemented using VDI snapshot files (TODO). |
| 44 | * VDI snapshot files may also contain the complete machine state. |
| 45 | * Maybe this machine state can be converted to QEMU PC machine snapshot data. |
| 46 | * |
| 47 | * The driver keeps a block cache (little endian entries) in memory. |
| 48 | * For the standard block size (1 MiB), a 1 TiB disk will use 4 MiB RAM, |
| 49 | * so this seems to be reasonable. |
| 50 | */ |
| 51 | |
Peter Maydell | 80c71a2 | 2016-01-18 18:01:42 +0000 | [diff] [blame] | 52 | #include "qemu/osdep.h" |
Philippe Mathieu-Daudé | f043568 | 2018-06-25 09:41:54 -0300 | [diff] [blame] | 53 | #include "qemu/units.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 54 | #include "qapi/error.h" |
Max Reitz | 49858b5 | 2018-03-12 17:55:26 +0100 | [diff] [blame] | 55 | #include "qapi/qobject-input-visitor.h" |
| 56 | #include "qapi/qapi-visit-block-core.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 57 | #include "block/block_int.h" |
Markus Armbruster | f853465 | 2018-06-14 21:14:34 +0200 | [diff] [blame] | 58 | #include "block/qdict.h" |
Kevin Wolf | a08f0c3 | 2016-03-08 15:57:05 +0100 | [diff] [blame] | 59 | #include "sysemu/block-backend.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 60 | #include "qemu/module.h" |
Markus Armbruster | 922a01a | 2018-02-01 12:18:46 +0100 | [diff] [blame] | 61 | #include "qemu/option.h" |
Paolo Bonzini | 58369e2 | 2016-03-15 17:22:36 +0100 | [diff] [blame] | 62 | #include "qemu/bswap.h" |
Juan Quintela | 795c40b | 2017-04-06 12:00:28 +0200 | [diff] [blame] | 63 | #include "migration/blocker.h" |
Daniel P. Berrange | 10817bf | 2015-09-01 14:48:02 +0100 | [diff] [blame] | 64 | #include "qemu/coroutine.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 65 | #include "qemu/cutils.h" |
Fam Zheng | 7c6f55b | 2016-09-21 12:27:17 +0800 | [diff] [blame] | 66 | #include "qemu/uuid.h" |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 67 | |
| 68 | /* Code configuration options. */ |
| 69 | |
| 70 | /* Enable debug messages. */ |
| 71 | //~ #define CONFIG_VDI_DEBUG |
| 72 | |
| 73 | /* Support write operations on VDI images. */ |
| 74 | #define CONFIG_VDI_WRITE |
| 75 | |
| 76 | /* Support non-standard block (cluster) size. This is untested. |
| 77 | * Maybe it will be needed for very large images. |
| 78 | */ |
| 79 | //~ #define CONFIG_VDI_BLOCK_SIZE |
| 80 | |
| 81 | /* Support static (fixed, pre-allocated) images. */ |
| 82 | #define CONFIG_VDI_STATIC_IMAGE |
| 83 | |
| 84 | /* Command line option for static images. */ |
| 85 | #define BLOCK_OPT_STATIC "static" |
| 86 | |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 87 | #define SECTOR_SIZE 512 |
Markus Armbruster | 1463212 | 2019-01-11 20:14:01 +0100 | [diff] [blame] | 88 | #define DEFAULT_CLUSTER_SIZE 1048576 |
| 89 | /* Note: can't use 1 * MiB, because it's passed to stringify() */ |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 90 | |
| 91 | #if defined(CONFIG_VDI_DEBUG) |
Eric Blake | b80666b | 2018-02-13 14:26:56 -0600 | [diff] [blame] | 92 | #define VDI_DEBUG 1 |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 93 | #else |
Eric Blake | b80666b | 2018-02-13 14:26:56 -0600 | [diff] [blame] | 94 | #define VDI_DEBUG 0 |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 95 | #endif |
| 96 | |
Eric Blake | b80666b | 2018-02-13 14:26:56 -0600 | [diff] [blame] | 97 | #define logout(fmt, ...) \ |
| 98 | do { \ |
| 99 | if (VDI_DEBUG) { \ |
| 100 | fprintf(stderr, "vdi\t%-24s" fmt, __func__, ##__VA_ARGS__); \ |
| 101 | } \ |
| 102 | } while (0) |
| 103 | |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 104 | /* Image signature. */ |
| 105 | #define VDI_SIGNATURE 0xbeda107f |
| 106 | |
| 107 | /* Image version. */ |
| 108 | #define VDI_VERSION_1_1 0x00010001 |
| 109 | |
| 110 | /* Image type. */ |
| 111 | #define VDI_TYPE_DYNAMIC 1 |
| 112 | #define VDI_TYPE_STATIC 2 |
| 113 | |
| 114 | /* Innotek / SUN images use these strings in header.text: |
| 115 | * "<<< innotek VirtualBox Disk Image >>>\n" |
| 116 | * "<<< Sun xVM VirtualBox Disk Image >>>\n" |
| 117 | * "<<< Sun VirtualBox Disk Image >>>\n" |
| 118 | * The value does not matter, so QEMU created images use a different text. |
| 119 | */ |
| 120 | #define VDI_TEXT "<<< QEMU VM Virtual Disk Image >>>\n" |
| 121 | |
Eric Sunshine | c794b4e | 2011-10-26 15:51:18 -0400 | [diff] [blame] | 122 | /* A never-allocated block; semantically arbitrary content. */ |
| 123 | #define VDI_UNALLOCATED 0xffffffffU |
| 124 | |
| 125 | /* A discarded (no longer allocated) block; semantically zero-filled. */ |
| 126 | #define VDI_DISCARDED 0xfffffffeU |
| 127 | |
| 128 | #define VDI_IS_ALLOCATED(X) ((X) < VDI_DISCARDED) |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 129 | |
Max Reitz | d20418e | 2014-10-28 11:12:32 +0100 | [diff] [blame] | 130 | /* The bmap will take up VDI_BLOCKS_IN_IMAGE_MAX * sizeof(uint32_t) bytes; since |
| 131 | * the bmap is read and written in a single operation, its size needs to be |
| 132 | * limited to INT_MAX; furthermore, when opening an image, the bmap size is |
| 133 | * rounded up to be aligned on BDRV_SECTOR_SIZE. |
| 134 | * Therefore this should satisfy the following: |
| 135 | * VDI_BLOCKS_IN_IMAGE_MAX * sizeof(uint32_t) + BDRV_SECTOR_SIZE == INT_MAX + 1 |
| 136 | * (INT_MAX + 1 is the first value not representable as an int) |
| 137 | * This guarantees that any value below or equal to the constant will, when |
| 138 | * multiplied by sizeof(uint32_t) and rounded up to a BDRV_SECTOR_SIZE boundary, |
| 139 | * still be below or equal to INT_MAX. */ |
| 140 | #define VDI_BLOCKS_IN_IMAGE_MAX \ |
| 141 | ((unsigned)((INT_MAX + 1u - BDRV_SECTOR_SIZE) / sizeof(uint32_t))) |
Jeff Cody | 63fa06d | 2014-03-28 11:42:24 -0400 | [diff] [blame] | 142 | #define VDI_DISK_SIZE_MAX ((uint64_t)VDI_BLOCKS_IN_IMAGE_MAX * \ |
| 143 | (uint64_t)DEFAULT_CLUSTER_SIZE) |
| 144 | |
Max Reitz | 49858b5 | 2018-03-12 17:55:26 +0100 | [diff] [blame] | 145 | static QemuOptsList vdi_create_opts; |
| 146 | |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 147 | typedef struct { |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 148 | char text[0x40]; |
| 149 | uint32_t signature; |
| 150 | uint32_t version; |
| 151 | uint32_t header_size; |
| 152 | uint32_t image_type; |
| 153 | uint32_t image_flags; |
| 154 | char description[256]; |
| 155 | uint32_t offset_bmap; |
| 156 | uint32_t offset_data; |
| 157 | uint32_t cylinders; /* disk geometry, unused here */ |
| 158 | uint32_t heads; /* disk geometry, unused here */ |
| 159 | uint32_t sectors; /* disk geometry, unused here */ |
| 160 | uint32_t sector_size; |
| 161 | uint32_t unused1; |
| 162 | uint64_t disk_size; |
| 163 | uint32_t block_size; |
| 164 | uint32_t block_extra; /* unused here */ |
| 165 | uint32_t blocks_in_image; |
| 166 | uint32_t blocks_allocated; |
Fam Zheng | 7c6f55b | 2016-09-21 12:27:17 +0800 | [diff] [blame] | 167 | QemuUUID uuid_image; |
| 168 | QemuUUID uuid_last_snap; |
| 169 | QemuUUID uuid_link; |
| 170 | QemuUUID uuid_parent; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 171 | uint64_t unused2[7]; |
Jeff Cody | 8368feb | 2013-09-25 12:08:48 -0400 | [diff] [blame] | 172 | } QEMU_PACKED VdiHeader; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 173 | |
Alberto Garcia | d4f1897 | 2019-05-01 21:13:56 +0300 | [diff] [blame] | 174 | QEMU_BUILD_BUG_ON(sizeof(VdiHeader) != 512); |
| 175 | |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 176 | typedef struct { |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 177 | /* The block map entries are little endian (even in memory). */ |
| 178 | uint32_t *bmap; |
| 179 | /* Size of block (bytes). */ |
| 180 | uint32_t block_size; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 181 | /* First sector of block map. */ |
| 182 | uint32_t bmap_sector; |
Stefan Weil | 4ff9786 | 2011-03-13 15:44:02 +0100 | [diff] [blame] | 183 | /* VDI header (converted to host endianness). */ |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 184 | VdiHeader header; |
Kevin Wolf | fc9d106 | 2011-11-22 16:46:26 +0100 | [diff] [blame] | 185 | |
Paolo Bonzini | 1e88663 | 2017-06-29 15:27:41 +0200 | [diff] [blame] | 186 | CoRwlock bmap_lock; |
Max Reitz | f0ab6f1 | 2015-02-27 14:54:39 -0500 | [diff] [blame] | 187 | |
Kevin Wolf | fc9d106 | 2011-11-22 16:46:26 +0100 | [diff] [blame] | 188 | Error *migration_blocker; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 189 | } BDRVVdiState; |
| 190 | |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 191 | static void vdi_header_to_cpu(VdiHeader *header) |
| 192 | { |
Peter Maydell | 0919018 | 2018-10-16 18:25:03 +0100 | [diff] [blame] | 193 | header->signature = le32_to_cpu(header->signature); |
| 194 | header->version = le32_to_cpu(header->version); |
| 195 | header->header_size = le32_to_cpu(header->header_size); |
| 196 | header->image_type = le32_to_cpu(header->image_type); |
| 197 | header->image_flags = le32_to_cpu(header->image_flags); |
| 198 | header->offset_bmap = le32_to_cpu(header->offset_bmap); |
| 199 | header->offset_data = le32_to_cpu(header->offset_data); |
| 200 | header->cylinders = le32_to_cpu(header->cylinders); |
| 201 | header->heads = le32_to_cpu(header->heads); |
| 202 | header->sectors = le32_to_cpu(header->sectors); |
| 203 | header->sector_size = le32_to_cpu(header->sector_size); |
| 204 | header->disk_size = le64_to_cpu(header->disk_size); |
| 205 | header->block_size = le32_to_cpu(header->block_size); |
| 206 | header->block_extra = le32_to_cpu(header->block_extra); |
| 207 | header->blocks_in_image = le32_to_cpu(header->blocks_in_image); |
| 208 | header->blocks_allocated = le32_to_cpu(header->blocks_allocated); |
Peter Maydell | 1324f06 | 2018-12-10 11:26:49 +0000 | [diff] [blame] | 209 | header->uuid_image = qemu_uuid_bswap(header->uuid_image); |
| 210 | header->uuid_last_snap = qemu_uuid_bswap(header->uuid_last_snap); |
| 211 | header->uuid_link = qemu_uuid_bswap(header->uuid_link); |
| 212 | header->uuid_parent = qemu_uuid_bswap(header->uuid_parent); |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | static void vdi_header_to_le(VdiHeader *header) |
| 216 | { |
Peter Maydell | 0919018 | 2018-10-16 18:25:03 +0100 | [diff] [blame] | 217 | header->signature = cpu_to_le32(header->signature); |
| 218 | header->version = cpu_to_le32(header->version); |
| 219 | header->header_size = cpu_to_le32(header->header_size); |
| 220 | header->image_type = cpu_to_le32(header->image_type); |
| 221 | header->image_flags = cpu_to_le32(header->image_flags); |
| 222 | header->offset_bmap = cpu_to_le32(header->offset_bmap); |
| 223 | header->offset_data = cpu_to_le32(header->offset_data); |
| 224 | header->cylinders = cpu_to_le32(header->cylinders); |
| 225 | header->heads = cpu_to_le32(header->heads); |
| 226 | header->sectors = cpu_to_le32(header->sectors); |
| 227 | header->sector_size = cpu_to_le32(header->sector_size); |
| 228 | header->disk_size = cpu_to_le64(header->disk_size); |
| 229 | header->block_size = cpu_to_le32(header->block_size); |
| 230 | header->block_extra = cpu_to_le32(header->block_extra); |
| 231 | header->blocks_in_image = cpu_to_le32(header->blocks_in_image); |
| 232 | header->blocks_allocated = cpu_to_le32(header->blocks_allocated); |
Peter Maydell | 1324f06 | 2018-12-10 11:26:49 +0000 | [diff] [blame] | 233 | header->uuid_image = qemu_uuid_bswap(header->uuid_image); |
| 234 | header->uuid_last_snap = qemu_uuid_bswap(header->uuid_last_snap); |
| 235 | header->uuid_link = qemu_uuid_bswap(header->uuid_link); |
| 236 | header->uuid_parent = qemu_uuid_bswap(header->uuid_parent); |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 237 | } |
| 238 | |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 239 | static void vdi_header_print(VdiHeader *header) |
| 240 | { |
Peter Maydell | ac928b8 | 2018-12-10 11:26:48 +0000 | [diff] [blame] | 241 | char uuidstr[37]; |
| 242 | QemuUUID uuid; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 243 | logout("text %s", header->text); |
Stefan Weil | 9f0470b | 2013-01-17 21:45:26 +0100 | [diff] [blame] | 244 | logout("signature 0x%08x\n", header->signature); |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 245 | logout("header size 0x%04x\n", header->header_size); |
| 246 | logout("image type 0x%04x\n", header->image_type); |
| 247 | logout("image flags 0x%04x\n", header->image_flags); |
| 248 | logout("description %s\n", header->description); |
| 249 | logout("offset bmap 0x%04x\n", header->offset_bmap); |
| 250 | logout("offset data 0x%04x\n", header->offset_data); |
| 251 | logout("cylinders 0x%04x\n", header->cylinders); |
| 252 | logout("heads 0x%04x\n", header->heads); |
| 253 | logout("sectors 0x%04x\n", header->sectors); |
| 254 | logout("sector size 0x%04x\n", header->sector_size); |
| 255 | logout("image size 0x%" PRIx64 " B (%" PRIu64 " MiB)\n", |
| 256 | header->disk_size, header->disk_size / MiB); |
| 257 | logout("block size 0x%04x\n", header->block_size); |
| 258 | logout("block extra 0x%04x\n", header->block_extra); |
| 259 | logout("blocks tot. 0x%04x\n", header->blocks_in_image); |
| 260 | logout("blocks all. 0x%04x\n", header->blocks_allocated); |
Peter Maydell | ac928b8 | 2018-12-10 11:26:48 +0000 | [diff] [blame] | 261 | uuid = header->uuid_image; |
| 262 | qemu_uuid_unparse(&uuid, uuidstr); |
| 263 | logout("uuid image %s\n", uuidstr); |
| 264 | uuid = header->uuid_last_snap; |
| 265 | qemu_uuid_unparse(&uuid, uuidstr); |
| 266 | logout("uuid snap %s\n", uuidstr); |
| 267 | uuid = header->uuid_link; |
| 268 | qemu_uuid_unparse(&uuid, uuidstr); |
| 269 | logout("uuid link %s\n", uuidstr); |
| 270 | uuid = header->uuid_parent; |
| 271 | qemu_uuid_unparse(&uuid, uuidstr); |
| 272 | logout("uuid parent %s\n", uuidstr); |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 273 | } |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 274 | |
Paolo Bonzini | 2fd6163 | 2018-03-01 17:36:19 +0100 | [diff] [blame] | 275 | static int coroutine_fn vdi_co_check(BlockDriverState *bs, BdrvCheckResult *res, |
| 276 | BdrvCheckMode fix) |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 277 | { |
| 278 | /* TODO: additional checks possible. */ |
| 279 | BDRVVdiState *s = (BDRVVdiState *)bs->opaque; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 280 | uint32_t blocks_allocated = 0; |
| 281 | uint32_t block; |
| 282 | uint32_t *bmap; |
| 283 | logout("\n"); |
| 284 | |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 285 | if (fix) { |
| 286 | return -ENOTSUP; |
| 287 | } |
| 288 | |
Markus Armbruster | 5839e53 | 2014-08-19 10:31:08 +0200 | [diff] [blame] | 289 | bmap = g_try_new(uint32_t, s->header.blocks_in_image); |
Kevin Wolf | 17cce73 | 2014-05-20 13:25:43 +0200 | [diff] [blame] | 290 | if (s->header.blocks_in_image && bmap == NULL) { |
| 291 | res->check_errors++; |
| 292 | return -ENOMEM; |
| 293 | } |
| 294 | |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 295 | memset(bmap, 0xff, s->header.blocks_in_image * sizeof(uint32_t)); |
| 296 | |
| 297 | /* Check block map and value of blocks_allocated. */ |
| 298 | for (block = 0; block < s->header.blocks_in_image; block++) { |
| 299 | uint32_t bmap_entry = le32_to_cpu(s->bmap[block]); |
Eric Sunshine | c794b4e | 2011-10-26 15:51:18 -0400 | [diff] [blame] | 300 | if (VDI_IS_ALLOCATED(bmap_entry)) { |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 301 | if (bmap_entry < s->header.blocks_in_image) { |
| 302 | blocks_allocated++; |
Eric Sunshine | c794b4e | 2011-10-26 15:51:18 -0400 | [diff] [blame] | 303 | if (!VDI_IS_ALLOCATED(bmap[bmap_entry])) { |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 304 | bmap[bmap_entry] = bmap_entry; |
| 305 | } else { |
| 306 | fprintf(stderr, "ERROR: block index %" PRIu32 |
| 307 | " also used by %" PRIu32 "\n", bmap[bmap_entry], bmap_entry); |
Kevin Wolf | 9ac228e | 2010-06-29 12:37:54 +0200 | [diff] [blame] | 308 | res->corruptions++; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 309 | } |
| 310 | } else { |
| 311 | fprintf(stderr, "ERROR: block index %" PRIu32 |
| 312 | " too large, is %" PRIu32 "\n", block, bmap_entry); |
Kevin Wolf | 9ac228e | 2010-06-29 12:37:54 +0200 | [diff] [blame] | 313 | res->corruptions++; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | } |
| 317 | if (blocks_allocated != s->header.blocks_allocated) { |
| 318 | fprintf(stderr, "ERROR: allocated blocks mismatch, is %" PRIu32 |
| 319 | ", should be %" PRIu32 "\n", |
| 320 | blocks_allocated, s->header.blocks_allocated); |
Kevin Wolf | 9ac228e | 2010-06-29 12:37:54 +0200 | [diff] [blame] | 321 | res->corruptions++; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 322 | } |
| 323 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 324 | g_free(bmap); |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 325 | |
Kevin Wolf | 9ac228e | 2010-06-29 12:37:54 +0200 | [diff] [blame] | 326 | return 0; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | static int vdi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
| 330 | { |
| 331 | /* TODO: vdi_get_info would be needed for machine snapshots. |
| 332 | vm_state_offset is still missing. */ |
| 333 | BDRVVdiState *s = (BDRVVdiState *)bs->opaque; |
| 334 | logout("\n"); |
| 335 | bdi->cluster_size = s->block_size; |
| 336 | bdi->vm_state_offset = 0; |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | static int vdi_make_empty(BlockDriverState *bs) |
| 341 | { |
| 342 | /* TODO: missing code. */ |
| 343 | logout("\n"); |
| 344 | /* The return value for missing code must be 0, see block.c. */ |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | static int vdi_probe(const uint8_t *buf, int buf_size, const char *filename) |
| 349 | { |
| 350 | const VdiHeader *header = (const VdiHeader *)buf; |
Jeff Cody | dddc775 | 2014-07-23 17:22:59 -0400 | [diff] [blame] | 351 | int ret = 0; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 352 | |
| 353 | logout("\n"); |
| 354 | |
| 355 | if (buf_size < sizeof(*header)) { |
| 356 | /* Header too small, no VDI. */ |
| 357 | } else if (le32_to_cpu(header->signature) == VDI_SIGNATURE) { |
Jeff Cody | dddc775 | 2014-07-23 17:22:59 -0400 | [diff] [blame] | 358 | ret = 100; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 359 | } |
| 360 | |
Jeff Cody | dddc775 | 2014-07-23 17:22:59 -0400 | [diff] [blame] | 361 | if (ret == 0) { |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 362 | logout("no vdi image\n"); |
| 363 | } else { |
| 364 | logout("%s", header->text); |
| 365 | } |
| 366 | |
Jeff Cody | dddc775 | 2014-07-23 17:22:59 -0400 | [diff] [blame] | 367 | return ret; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 368 | } |
| 369 | |
Max Reitz | 015a103 | 2013-09-05 14:22:29 +0200 | [diff] [blame] | 370 | static int vdi_open(BlockDriverState *bs, QDict *options, int flags, |
| 371 | Error **errp) |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 372 | { |
| 373 | BDRVVdiState *s = bs->opaque; |
| 374 | VdiHeader header; |
| 375 | size_t bmap_size; |
Stefan Weil | 8937f82 | 2013-01-17 21:45:27 +0100 | [diff] [blame] | 376 | int ret; |
Peter Maydell | ac928b8 | 2018-12-10 11:26:48 +0000 | [diff] [blame] | 377 | QemuUUID uuid_link, uuid_parent; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 378 | |
Max Reitz | 8b1869d | 2020-05-13 13:05:35 +0200 | [diff] [blame] | 379 | bs->file = bdrv_open_child(NULL, options, "file", bs, &child_of_bds, |
| 380 | BDRV_CHILD_IMAGE, false, errp); |
Kevin Wolf | 4e4bf5c | 2016-12-16 18:52:37 +0100 | [diff] [blame] | 381 | if (!bs->file) { |
| 382 | return -EINVAL; |
| 383 | } |
| 384 | |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 385 | logout("\n"); |
| 386 | |
Alberto Garcia | d4f1897 | 2019-05-01 21:13:56 +0300 | [diff] [blame] | 387 | ret = bdrv_pread(bs->file, 0, &header, sizeof(header)); |
Stefan Weil | 8937f82 | 2013-01-17 21:45:27 +0100 | [diff] [blame] | 388 | if (ret < 0) { |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 389 | goto fail; |
| 390 | } |
| 391 | |
| 392 | vdi_header_to_cpu(&header); |
Kevin Wolf | 95a14d5 | 2018-03-20 14:41:53 +0100 | [diff] [blame] | 393 | if (VDI_DEBUG) { |
| 394 | vdi_header_print(&header); |
| 395 | } |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 396 | |
Jeff Cody | 63fa06d | 2014-03-28 11:42:24 -0400 | [diff] [blame] | 397 | if (header.disk_size > VDI_DISK_SIZE_MAX) { |
| 398 | error_setg(errp, "Unsupported VDI image size (size is 0x%" PRIx64 |
| 399 | ", max supported is 0x%" PRIx64 ")", |
| 400 | header.disk_size, VDI_DISK_SIZE_MAX); |
| 401 | ret = -ENOTSUP; |
| 402 | goto fail; |
| 403 | } |
| 404 | |
Peter Maydell | ac928b8 | 2018-12-10 11:26:48 +0000 | [diff] [blame] | 405 | uuid_link = header.uuid_link; |
| 406 | uuid_parent = header.uuid_parent; |
| 407 | |
Stefan Weil | f21dc3a | 2010-05-12 20:25:45 +0200 | [diff] [blame] | 408 | if (header.disk_size % SECTOR_SIZE != 0) { |
| 409 | /* 'VBoxManage convertfromraw' can create images with odd disk sizes. |
| 410 | We accept them but round the disk size to the next multiple of |
| 411 | SECTOR_SIZE. */ |
| 412 | logout("odd disk size %" PRIu64 " B, round up\n", header.disk_size); |
Max Reitz | e9082e4 | 2014-10-21 10:51:25 +0200 | [diff] [blame] | 413 | header.disk_size = ROUND_UP(header.disk_size, SECTOR_SIZE); |
Stefan Weil | f21dc3a | 2010-05-12 20:25:45 +0200 | [diff] [blame] | 414 | } |
| 415 | |
Stefan Weil | 0e87ba2 | 2013-01-17 21:45:28 +0100 | [diff] [blame] | 416 | if (header.signature != VDI_SIGNATURE) { |
Max Reitz | 521b2b5 | 2014-04-29 19:03:12 +0200 | [diff] [blame] | 417 | error_setg(errp, "Image not in VDI format (bad signature %08" PRIx32 |
| 418 | ")", header.signature); |
Paolo Bonzini | 76abe40 | 2014-02-17 14:44:06 +0100 | [diff] [blame] | 419 | ret = -EINVAL; |
Stefan Weil | 0e87ba2 | 2013-01-17 21:45:28 +0100 | [diff] [blame] | 420 | goto fail; |
| 421 | } else if (header.version != VDI_VERSION_1_1) { |
Max Reitz | 521b2b5 | 2014-04-29 19:03:12 +0200 | [diff] [blame] | 422 | error_setg(errp, "unsupported VDI image (version %" PRIu32 ".%" PRIu32 |
| 423 | ")", header.version >> 16, header.version & 0xffff); |
Stefan Weil | 8937f82 | 2013-01-17 21:45:27 +0100 | [diff] [blame] | 424 | ret = -ENOTSUP; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 425 | goto fail; |
| 426 | } else if (header.offset_bmap % SECTOR_SIZE != 0) { |
| 427 | /* We only support block maps which start on a sector boundary. */ |
Paolo Bonzini | 5b7aa9b | 2014-02-17 14:44:07 +0100 | [diff] [blame] | 428 | error_setg(errp, "unsupported VDI image (unaligned block map offset " |
Max Reitz | 521b2b5 | 2014-04-29 19:03:12 +0200 | [diff] [blame] | 429 | "0x%" PRIx32 ")", header.offset_bmap); |
Stefan Weil | 8937f82 | 2013-01-17 21:45:27 +0100 | [diff] [blame] | 430 | ret = -ENOTSUP; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 431 | goto fail; |
| 432 | } else if (header.offset_data % SECTOR_SIZE != 0) { |
| 433 | /* We only support data blocks which start on a sector boundary. */ |
Max Reitz | 521b2b5 | 2014-04-29 19:03:12 +0200 | [diff] [blame] | 434 | error_setg(errp, "unsupported VDI image (unaligned data offset 0x%" |
| 435 | PRIx32 ")", header.offset_data); |
Stefan Weil | 8937f82 | 2013-01-17 21:45:27 +0100 | [diff] [blame] | 436 | ret = -ENOTSUP; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 437 | goto fail; |
| 438 | } else if (header.sector_size != SECTOR_SIZE) { |
Max Reitz | 521b2b5 | 2014-04-29 19:03:12 +0200 | [diff] [blame] | 439 | error_setg(errp, "unsupported VDI image (sector size %" PRIu32 |
| 440 | " is not %u)", header.sector_size, SECTOR_SIZE); |
Stefan Weil | 8937f82 | 2013-01-17 21:45:27 +0100 | [diff] [blame] | 441 | ret = -ENOTSUP; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 442 | goto fail; |
Jeff Cody | 63fa06d | 2014-03-28 11:42:24 -0400 | [diff] [blame] | 443 | } else if (header.block_size != DEFAULT_CLUSTER_SIZE) { |
Max Reitz | 521b2b5 | 2014-04-29 19:03:12 +0200 | [diff] [blame] | 444 | error_setg(errp, "unsupported VDI image (block size %" PRIu32 |
Leonid Bloch | 3dd5b8f | 2018-11-04 18:09:28 +0000 | [diff] [blame] | 445 | " is not %" PRIu32 ")", |
Philippe Mathieu-Daudé | f043568 | 2018-06-25 09:41:54 -0300 | [diff] [blame] | 446 | header.block_size, DEFAULT_CLUSTER_SIZE); |
Stefan Weil | 8937f82 | 2013-01-17 21:45:27 +0100 | [diff] [blame] | 447 | ret = -ENOTSUP; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 448 | goto fail; |
Stefan Weil | f21dc3a | 2010-05-12 20:25:45 +0200 | [diff] [blame] | 449 | } else if (header.disk_size > |
| 450 | (uint64_t)header.blocks_in_image * header.block_size) { |
Paolo Bonzini | 5b7aa9b | 2014-02-17 14:44:07 +0100 | [diff] [blame] | 451 | error_setg(errp, "unsupported VDI image (disk size %" PRIu64 ", " |
| 452 | "image bitmap has room for %" PRIu64 ")", |
| 453 | header.disk_size, |
| 454 | (uint64_t)header.blocks_in_image * header.block_size); |
Stefan Weil | 8937f82 | 2013-01-17 21:45:27 +0100 | [diff] [blame] | 455 | ret = -ENOTSUP; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 456 | goto fail; |
Peter Maydell | ac928b8 | 2018-12-10 11:26:48 +0000 | [diff] [blame] | 457 | } else if (!qemu_uuid_is_null(&uuid_link)) { |
Paolo Bonzini | 5b7aa9b | 2014-02-17 14:44:07 +0100 | [diff] [blame] | 458 | error_setg(errp, "unsupported VDI image (non-NULL link UUID)"); |
Stefan Weil | 8937f82 | 2013-01-17 21:45:27 +0100 | [diff] [blame] | 459 | ret = -ENOTSUP; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 460 | goto fail; |
Peter Maydell | ac928b8 | 2018-12-10 11:26:48 +0000 | [diff] [blame] | 461 | } else if (!qemu_uuid_is_null(&uuid_parent)) { |
Paolo Bonzini | 5b7aa9b | 2014-02-17 14:44:07 +0100 | [diff] [blame] | 462 | error_setg(errp, "unsupported VDI image (non-NULL parent UUID)"); |
Stefan Weil | 8937f82 | 2013-01-17 21:45:27 +0100 | [diff] [blame] | 463 | ret = -ENOTSUP; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 464 | goto fail; |
Jeff Cody | 63fa06d | 2014-03-28 11:42:24 -0400 | [diff] [blame] | 465 | } else if (header.blocks_in_image > VDI_BLOCKS_IN_IMAGE_MAX) { |
| 466 | error_setg(errp, "unsupported VDI image " |
| 467 | "(too many blocks %u, max is %u)", |
| 468 | header.blocks_in_image, VDI_BLOCKS_IN_IMAGE_MAX); |
| 469 | ret = -ENOTSUP; |
| 470 | goto fail; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | bs->total_sectors = header.disk_size / SECTOR_SIZE; |
| 474 | |
| 475 | s->block_size = header.block_size; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 476 | s->bmap_sector = header.offset_bmap / SECTOR_SIZE; |
| 477 | s->header = header; |
| 478 | |
| 479 | bmap_size = header.blocks_in_image * sizeof(uint32_t); |
Max Reitz | e9082e4 | 2014-10-21 10:51:25 +0200 | [diff] [blame] | 480 | bmap_size = DIV_ROUND_UP(bmap_size, SECTOR_SIZE); |
Kevin Wolf | 9a4f4c3 | 2015-06-16 14:19:22 +0200 | [diff] [blame] | 481 | s->bmap = qemu_try_blockalign(bs->file->bs, bmap_size * SECTOR_SIZE); |
Kevin Wolf | 17cce73 | 2014-05-20 13:25:43 +0200 | [diff] [blame] | 482 | if (s->bmap == NULL) { |
| 483 | ret = -ENOMEM; |
| 484 | goto fail; |
| 485 | } |
| 486 | |
Alberto Garcia | d4f1897 | 2019-05-01 21:13:56 +0300 | [diff] [blame] | 487 | ret = bdrv_pread(bs->file, header.offset_bmap, s->bmap, |
| 488 | bmap_size * SECTOR_SIZE); |
Stefan Weil | 8937f82 | 2013-01-17 21:45:27 +0100 | [diff] [blame] | 489 | if (ret < 0) { |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 490 | goto fail_free_bmap; |
| 491 | } |
| 492 | |
Kevin Wolf | fc9d106 | 2011-11-22 16:46:26 +0100 | [diff] [blame] | 493 | /* Disable migration when vdi images are used */ |
Alberto Garcia | 81e5f78 | 2015-04-08 12:29:19 +0300 | [diff] [blame] | 494 | error_setg(&s->migration_blocker, "The vdi format used by node '%s' " |
| 495 | "does not support live migration", |
| 496 | bdrv_get_device_or_node_name(bs)); |
Markus Armbruster | 386f6c0 | 2020-07-07 18:06:10 +0200 | [diff] [blame] | 497 | ret = migrate_add_blocker(s->migration_blocker, errp); |
| 498 | if (ret < 0) { |
Ashijeet Acharya | fe44dc9 | 2017-01-16 17:01:53 +0530 | [diff] [blame] | 499 | error_free(s->migration_blocker); |
| 500 | goto fail_free_bmap; |
| 501 | } |
Kevin Wolf | fc9d106 | 2011-11-22 16:46:26 +0100 | [diff] [blame] | 502 | |
Paolo Bonzini | 1e88663 | 2017-06-29 15:27:41 +0200 | [diff] [blame] | 503 | qemu_co_rwlock_init(&s->bmap_lock); |
Max Reitz | f0ab6f1 | 2015-02-27 14:54:39 -0500 | [diff] [blame] | 504 | |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 505 | return 0; |
| 506 | |
| 507 | fail_free_bmap: |
Kevin Wolf | 17cce73 | 2014-05-20 13:25:43 +0200 | [diff] [blame] | 508 | qemu_vfree(s->bmap); |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 509 | |
| 510 | fail: |
Stefan Weil | 8937f82 | 2013-01-17 21:45:27 +0100 | [diff] [blame] | 511 | return ret; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 512 | } |
| 513 | |
Jeff Cody | ecfe2bb | 2012-09-20 15:13:32 -0400 | [diff] [blame] | 514 | static int vdi_reopen_prepare(BDRVReopenState *state, |
| 515 | BlockReopenQueue *queue, Error **errp) |
| 516 | { |
| 517 | return 0; |
| 518 | } |
| 519 | |
Eric Blake | 67635f6 | 2018-02-13 14:26:57 -0600 | [diff] [blame] | 520 | static int coroutine_fn vdi_co_block_status(BlockDriverState *bs, |
| 521 | bool want_zero, |
| 522 | int64_t offset, int64_t bytes, |
| 523 | int64_t *pnum, int64_t *map, |
| 524 | BlockDriverState **file) |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 525 | { |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 526 | BDRVVdiState *s = (BDRVVdiState *)bs->opaque; |
Eric Blake | 67635f6 | 2018-02-13 14:26:57 -0600 | [diff] [blame] | 527 | size_t bmap_index = offset / s->block_size; |
| 528 | size_t index_in_block = offset % s->block_size; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 529 | uint32_t bmap_entry = le32_to_cpu(s->bmap[bmap_index]); |
Paolo Bonzini | 4bc74be | 2013-09-04 19:00:30 +0200 | [diff] [blame] | 530 | int result; |
| 531 | |
Eric Blake | 67635f6 | 2018-02-13 14:26:57 -0600 | [diff] [blame] | 532 | logout("%p, %" PRId64 ", %" PRId64 ", %p\n", bs, offset, bytes, pnum); |
| 533 | *pnum = MIN(s->block_size - index_in_block, bytes); |
Paolo Bonzini | 4bc74be | 2013-09-04 19:00:30 +0200 | [diff] [blame] | 534 | result = VDI_IS_ALLOCATED(bmap_entry); |
| 535 | if (!result) { |
Vladimir Sementsov-Ogievskiy | 2ea0332 | 2020-05-28 12:43:58 +0300 | [diff] [blame] | 536 | return BDRV_BLOCK_ZERO; |
Paolo Bonzini | 4bc74be | 2013-09-04 19:00:30 +0200 | [diff] [blame] | 537 | } |
| 538 | |
Eric Blake | 67635f6 | 2018-02-13 14:26:57 -0600 | [diff] [blame] | 539 | *map = s->header.offset_data + (uint64_t)bmap_entry * s->block_size + |
| 540 | index_in_block; |
Fam Zheng | 8bfb137 | 2016-01-26 11:58:56 +0800 | [diff] [blame] | 541 | *file = bs->file->bs; |
Max Reitz | ad6434d | 2019-07-25 17:55:10 +0200 | [diff] [blame] | 542 | return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | |
| 543 | (s->header.image_type == VDI_TYPE_STATIC ? BDRV_BLOCK_RECURSE : 0); |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 544 | } |
| 545 | |
Kevin Wolf | 0865bb6 | 2016-04-25 16:22:39 +0200 | [diff] [blame] | 546 | static int coroutine_fn |
| 547 | vdi_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes, |
| 548 | QEMUIOVector *qiov, int flags) |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 549 | { |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 550 | BDRVVdiState *s = bs->opaque; |
Kevin Wolf | 0865bb6 | 2016-04-25 16:22:39 +0200 | [diff] [blame] | 551 | QEMUIOVector local_qiov; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 552 | uint32_t bmap_entry; |
| 553 | uint32_t block_index; |
Kevin Wolf | 0865bb6 | 2016-04-25 16:22:39 +0200 | [diff] [blame] | 554 | uint32_t offset_in_block; |
| 555 | uint32_t n_bytes; |
| 556 | uint64_t bytes_done = 0; |
Paolo Bonzini | eb9566d | 2012-03-19 18:07:51 +0100 | [diff] [blame] | 557 | int ret = 0; |
Paolo Bonzini | 4de659e | 2012-03-19 18:07:47 +0100 | [diff] [blame] | 558 | |
| 559 | logout("\n"); |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 560 | |
Kevin Wolf | 0865bb6 | 2016-04-25 16:22:39 +0200 | [diff] [blame] | 561 | qemu_iovec_init(&local_qiov, qiov->niov); |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 562 | |
Kevin Wolf | 0865bb6 | 2016-04-25 16:22:39 +0200 | [diff] [blame] | 563 | while (ret >= 0 && bytes > 0) { |
| 564 | block_index = offset / s->block_size; |
| 565 | offset_in_block = offset % s->block_size; |
| 566 | n_bytes = MIN(bytes, s->block_size - offset_in_block); |
| 567 | |
| 568 | logout("will read %u bytes starting at offset %" PRIu64 "\n", |
| 569 | n_bytes, offset); |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 570 | |
Paolo Bonzini | eb9566d | 2012-03-19 18:07:51 +0100 | [diff] [blame] | 571 | /* prepare next AIO request */ |
Paolo Bonzini | 1e88663 | 2017-06-29 15:27:41 +0200 | [diff] [blame] | 572 | qemu_co_rwlock_rdlock(&s->bmap_lock); |
Paolo Bonzini | eb9566d | 2012-03-19 18:07:51 +0100 | [diff] [blame] | 573 | bmap_entry = le32_to_cpu(s->bmap[block_index]); |
Paolo Bonzini | 1e88663 | 2017-06-29 15:27:41 +0200 | [diff] [blame] | 574 | qemu_co_rwlock_unlock(&s->bmap_lock); |
Paolo Bonzini | eb9566d | 2012-03-19 18:07:51 +0100 | [diff] [blame] | 575 | if (!VDI_IS_ALLOCATED(bmap_entry)) { |
| 576 | /* Block not allocated, return zeros, no need to wait. */ |
Kevin Wolf | 0865bb6 | 2016-04-25 16:22:39 +0200 | [diff] [blame] | 577 | qemu_iovec_memset(qiov, bytes_done, 0, n_bytes); |
Paolo Bonzini | eb9566d | 2012-03-19 18:07:51 +0100 | [diff] [blame] | 578 | ret = 0; |
| 579 | } else { |
Kevin Wolf | 0865bb6 | 2016-04-25 16:22:39 +0200 | [diff] [blame] | 580 | uint64_t data_offset = s->header.offset_data + |
| 581 | (uint64_t)bmap_entry * s->block_size + |
| 582 | offset_in_block; |
Paolo Bonzini | 0c7bfc3 | 2012-03-19 18:07:46 +0100 | [diff] [blame] | 583 | |
Kevin Wolf | 0865bb6 | 2016-04-25 16:22:39 +0200 | [diff] [blame] | 584 | qemu_iovec_reset(&local_qiov); |
| 585 | qemu_iovec_concat(&local_qiov, qiov, bytes_done, n_bytes); |
| 586 | |
Kevin Wolf | a03ef88 | 2016-06-20 21:31:46 +0200 | [diff] [blame] | 587 | ret = bdrv_co_preadv(bs->file, data_offset, n_bytes, |
Kevin Wolf | 0865bb6 | 2016-04-25 16:22:39 +0200 | [diff] [blame] | 588 | &local_qiov, 0); |
| 589 | } |
| 590 | logout("%u bytes read\n", n_bytes); |
| 591 | |
| 592 | bytes -= n_bytes; |
| 593 | offset += n_bytes; |
| 594 | bytes_done += n_bytes; |
Paolo Bonzini | 3d46a75 | 2012-03-19 18:07:45 +0100 | [diff] [blame] | 595 | } |
| 596 | |
Kevin Wolf | 0865bb6 | 2016-04-25 16:22:39 +0200 | [diff] [blame] | 597 | qemu_iovec_destroy(&local_qiov); |
| 598 | |
Paolo Bonzini | 3d46a75 | 2012-03-19 18:07:45 +0100 | [diff] [blame] | 599 | return ret; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 600 | } |
| 601 | |
Kevin Wolf | fde9d56 | 2016-04-25 16:22:39 +0200 | [diff] [blame] | 602 | static int coroutine_fn |
| 603 | vdi_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes, |
| 604 | QEMUIOVector *qiov, int flags) |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 605 | { |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 606 | BDRVVdiState *s = bs->opaque; |
Kevin Wolf | fde9d56 | 2016-04-25 16:22:39 +0200 | [diff] [blame] | 607 | QEMUIOVector local_qiov; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 608 | uint32_t bmap_entry; |
| 609 | uint32_t block_index; |
Kevin Wolf | fde9d56 | 2016-04-25 16:22:39 +0200 | [diff] [blame] | 610 | uint32_t offset_in_block; |
| 611 | uint32_t n_bytes; |
Paolo Bonzini | 1e88663 | 2017-06-29 15:27:41 +0200 | [diff] [blame] | 612 | uint64_t data_offset; |
Paolo Bonzini | bfc45fc | 2012-03-19 18:07:48 +0100 | [diff] [blame] | 613 | uint32_t bmap_first = VDI_UNALLOCATED; |
| 614 | uint32_t bmap_last = VDI_UNALLOCATED; |
Paolo Bonzini | bfc45fc | 2012-03-19 18:07:48 +0100 | [diff] [blame] | 615 | uint8_t *block = NULL; |
Kevin Wolf | fde9d56 | 2016-04-25 16:22:39 +0200 | [diff] [blame] | 616 | uint64_t bytes_done = 0; |
Paolo Bonzini | eb9566d | 2012-03-19 18:07:51 +0100 | [diff] [blame] | 617 | int ret = 0; |
Paolo Bonzini | 4de659e | 2012-03-19 18:07:47 +0100 | [diff] [blame] | 618 | |
| 619 | logout("\n"); |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 620 | |
Kevin Wolf | fde9d56 | 2016-04-25 16:22:39 +0200 | [diff] [blame] | 621 | qemu_iovec_init(&local_qiov, qiov->niov); |
Paolo Bonzini | 0c7bfc3 | 2012-03-19 18:07:46 +0100 | [diff] [blame] | 622 | |
Kevin Wolf | fde9d56 | 2016-04-25 16:22:39 +0200 | [diff] [blame] | 623 | while (ret >= 0 && bytes > 0) { |
| 624 | block_index = offset / s->block_size; |
| 625 | offset_in_block = offset % s->block_size; |
| 626 | n_bytes = MIN(bytes, s->block_size - offset_in_block); |
| 627 | |
| 628 | logout("will write %u bytes starting at offset %" PRIu64 "\n", |
| 629 | n_bytes, offset); |
Paolo Bonzini | 0c7bfc3 | 2012-03-19 18:07:46 +0100 | [diff] [blame] | 630 | |
Paolo Bonzini | eb9566d | 2012-03-19 18:07:51 +0100 | [diff] [blame] | 631 | /* prepare next AIO request */ |
Paolo Bonzini | 1e88663 | 2017-06-29 15:27:41 +0200 | [diff] [blame] | 632 | qemu_co_rwlock_rdlock(&s->bmap_lock); |
Paolo Bonzini | eb9566d | 2012-03-19 18:07:51 +0100 | [diff] [blame] | 633 | bmap_entry = le32_to_cpu(s->bmap[block_index]); |
| 634 | if (!VDI_IS_ALLOCATED(bmap_entry)) { |
| 635 | /* Allocate new block and write to it. */ |
Kevin Wolf | fde9d56 | 2016-04-25 16:22:39 +0200 | [diff] [blame] | 636 | uint64_t data_offset; |
Paolo Bonzini | 1e88663 | 2017-06-29 15:27:41 +0200 | [diff] [blame] | 637 | qemu_co_rwlock_upgrade(&s->bmap_lock); |
| 638 | bmap_entry = le32_to_cpu(s->bmap[block_index]); |
| 639 | if (VDI_IS_ALLOCATED(bmap_entry)) { |
| 640 | /* A concurrent allocation did the work for us. */ |
| 641 | qemu_co_rwlock_downgrade(&s->bmap_lock); |
| 642 | goto nonallocating_write; |
| 643 | } |
| 644 | |
Paolo Bonzini | eb9566d | 2012-03-19 18:07:51 +0100 | [diff] [blame] | 645 | bmap_entry = s->header.blocks_allocated; |
| 646 | s->bmap[block_index] = cpu_to_le32(bmap_entry); |
| 647 | s->header.blocks_allocated++; |
Kevin Wolf | fde9d56 | 2016-04-25 16:22:39 +0200 | [diff] [blame] | 648 | data_offset = s->header.offset_data + |
| 649 | (uint64_t)bmap_entry * s->block_size; |
Paolo Bonzini | eb9566d | 2012-03-19 18:07:51 +0100 | [diff] [blame] | 650 | if (block == NULL) { |
| 651 | block = g_malloc(s->block_size); |
| 652 | bmap_first = block_index; |
| 653 | } |
| 654 | bmap_last = block_index; |
| 655 | /* Copy data to be written to new block and zero unused parts. */ |
Kevin Wolf | fde9d56 | 2016-04-25 16:22:39 +0200 | [diff] [blame] | 656 | memset(block, 0, offset_in_block); |
| 657 | qemu_iovec_to_buf(qiov, bytes_done, block + offset_in_block, |
| 658 | n_bytes); |
| 659 | memset(block + offset_in_block + n_bytes, 0, |
| 660 | s->block_size - n_bytes - offset_in_block); |
Max Reitz | f0ab6f1 | 2015-02-27 14:54:39 -0500 | [diff] [blame] | 661 | |
Paolo Bonzini | 1e88663 | 2017-06-29 15:27:41 +0200 | [diff] [blame] | 662 | /* Write the new block under CoRwLock write-side protection, |
| 663 | * so this full-cluster write does not overlap a partial write |
| 664 | * of the same cluster, issued from the "else" branch. |
| 665 | */ |
Kevin Wolf | d9ca2ea | 2016-06-20 20:09:15 +0200 | [diff] [blame] | 666 | ret = bdrv_pwrite(bs->file, data_offset, block, s->block_size); |
Paolo Bonzini | 1e88663 | 2017-06-29 15:27:41 +0200 | [diff] [blame] | 667 | qemu_co_rwlock_unlock(&s->bmap_lock); |
Paolo Bonzini | eb9566d | 2012-03-19 18:07:51 +0100 | [diff] [blame] | 668 | } else { |
Paolo Bonzini | 1e88663 | 2017-06-29 15:27:41 +0200 | [diff] [blame] | 669 | nonallocating_write: |
| 670 | data_offset = s->header.offset_data + |
| 671 | (uint64_t)bmap_entry * s->block_size + |
| 672 | offset_in_block; |
| 673 | qemu_co_rwlock_unlock(&s->bmap_lock); |
Kevin Wolf | fde9d56 | 2016-04-25 16:22:39 +0200 | [diff] [blame] | 674 | |
| 675 | qemu_iovec_reset(&local_qiov); |
| 676 | qemu_iovec_concat(&local_qiov, qiov, bytes_done, n_bytes); |
| 677 | |
Kevin Wolf | a03ef88 | 2016-06-20 21:31:46 +0200 | [diff] [blame] | 678 | ret = bdrv_co_pwritev(bs->file, data_offset, n_bytes, |
Kevin Wolf | fde9d56 | 2016-04-25 16:22:39 +0200 | [diff] [blame] | 679 | &local_qiov, 0); |
Paolo Bonzini | eb9566d | 2012-03-19 18:07:51 +0100 | [diff] [blame] | 680 | } |
| 681 | |
Kevin Wolf | fde9d56 | 2016-04-25 16:22:39 +0200 | [diff] [blame] | 682 | bytes -= n_bytes; |
| 683 | offset += n_bytes; |
| 684 | bytes_done += n_bytes; |
Paolo Bonzini | eb9566d | 2012-03-19 18:07:51 +0100 | [diff] [blame] | 685 | |
Kevin Wolf | fde9d56 | 2016-04-25 16:22:39 +0200 | [diff] [blame] | 686 | logout("%u bytes written\n", n_bytes); |
Paolo Bonzini | 3d46a75 | 2012-03-19 18:07:45 +0100 | [diff] [blame] | 687 | } |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 688 | |
Kevin Wolf | fde9d56 | 2016-04-25 16:22:39 +0200 | [diff] [blame] | 689 | qemu_iovec_destroy(&local_qiov); |
| 690 | |
Paolo Bonzini | 0c7bfc3 | 2012-03-19 18:07:46 +0100 | [diff] [blame] | 691 | logout("finished data write\n"); |
Paolo Bonzini | 4eea78e | 2012-03-19 18:07:50 +0100 | [diff] [blame] | 692 | if (ret < 0) { |
David Edmondson | 574b830 | 2021-03-25 12:29:36 +0100 | [diff] [blame] | 693 | g_free(block); |
Paolo Bonzini | 4eea78e | 2012-03-19 18:07:50 +0100 | [diff] [blame] | 694 | return ret; |
| 695 | } |
| 696 | |
| 697 | if (block) { |
| 698 | /* One or more new blocks were allocated. */ |
David Edmondson | 07ee2ab | 2021-03-25 12:29:37 +0100 | [diff] [blame] | 699 | VdiHeader *header; |
Paolo Bonzini | 4eea78e | 2012-03-19 18:07:50 +0100 | [diff] [blame] | 700 | uint8_t *base; |
| 701 | uint64_t offset; |
Kevin Wolf | fde9d56 | 2016-04-25 16:22:39 +0200 | [diff] [blame] | 702 | uint32_t n_sectors; |
Paolo Bonzini | 4eea78e | 2012-03-19 18:07:50 +0100 | [diff] [blame] | 703 | |
David Edmondson | 07ee2ab | 2021-03-25 12:29:37 +0100 | [diff] [blame] | 704 | g_free(block); |
| 705 | header = g_malloc(sizeof(*header)); |
| 706 | |
Paolo Bonzini | 4eea78e | 2012-03-19 18:07:50 +0100 | [diff] [blame] | 707 | logout("now writing modified header\n"); |
| 708 | assert(VDI_IS_ALLOCATED(bmap_first)); |
| 709 | *header = s->header; |
| 710 | vdi_header_to_le(header); |
David Edmondson | 07ee2ab | 2021-03-25 12:29:37 +0100 | [diff] [blame] | 711 | ret = bdrv_pwrite(bs->file, 0, header, sizeof(*header)); |
| 712 | g_free(header); |
Paolo Bonzini | 4eea78e | 2012-03-19 18:07:50 +0100 | [diff] [blame] | 713 | |
| 714 | if (ret < 0) { |
| 715 | return ret; |
Paolo Bonzini | 0c7bfc3 | 2012-03-19 18:07:46 +0100 | [diff] [blame] | 716 | } |
Paolo Bonzini | 4eea78e | 2012-03-19 18:07:50 +0100 | [diff] [blame] | 717 | |
| 718 | logout("now writing modified block map entry %u...%u\n", |
| 719 | bmap_first, bmap_last); |
| 720 | /* Write modified sectors from block map. */ |
| 721 | bmap_first /= (SECTOR_SIZE / sizeof(uint32_t)); |
| 722 | bmap_last /= (SECTOR_SIZE / sizeof(uint32_t)); |
| 723 | n_sectors = bmap_last - bmap_first + 1; |
| 724 | offset = s->bmap_sector + bmap_first; |
| 725 | base = ((uint8_t *)&s->bmap[0]) + bmap_first * SECTOR_SIZE; |
| 726 | logout("will write %u block map sectors starting from entry %u\n", |
| 727 | n_sectors, bmap_first); |
Alberto Garcia | d4f1897 | 2019-05-01 21:13:56 +0300 | [diff] [blame] | 728 | ret = bdrv_pwrite(bs->file, offset * SECTOR_SIZE, base, |
| 729 | n_sectors * SECTOR_SIZE); |
Paolo Bonzini | 0c7bfc3 | 2012-03-19 18:07:46 +0100 | [diff] [blame] | 730 | } |
| 731 | |
Alberto Garcia | d4f1897 | 2019-05-01 21:13:56 +0300 | [diff] [blame] | 732 | return ret < 0 ? ret : 0; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 733 | } |
| 734 | |
Max Reitz | e381057 | 2018-03-12 17:55:28 +0100 | [diff] [blame] | 735 | static int coroutine_fn vdi_co_do_create(BlockdevCreateOptions *create_options, |
Max Reitz | 49858b5 | 2018-03-12 17:55:26 +0100 | [diff] [blame] | 736 | size_t block_size, Error **errp) |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 737 | { |
Max Reitz | e381057 | 2018-03-12 17:55:28 +0100 | [diff] [blame] | 738 | BlockdevCreateOptionsVdi *vdi_opts; |
Jeff Cody | dddc775 | 2014-07-23 17:22:59 -0400 | [diff] [blame] | 739 | int ret = 0; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 740 | uint64_t bytes = 0; |
| 741 | uint32_t blocks; |
Kevin Wolf | 61fa648 | 2018-03-20 15:08:00 +0100 | [diff] [blame] | 742 | uint32_t image_type; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 743 | VdiHeader header; |
| 744 | size_t i; |
| 745 | size_t bmap_size; |
Jeff Cody | 7074786 | 2014-07-23 17:22:58 -0400 | [diff] [blame] | 746 | int64_t offset = 0; |
Max Reitz | ec73f06 | 2018-03-12 17:55:27 +0100 | [diff] [blame] | 747 | BlockDriverState *bs_file = NULL; |
Kevin Wolf | a08f0c3 | 2016-03-08 15:57:05 +0100 | [diff] [blame] | 748 | BlockBackend *blk = NULL; |
Jeff Cody | 7074786 | 2014-07-23 17:22:58 -0400 | [diff] [blame] | 749 | uint32_t *bmap = NULL; |
Peter Maydell | ac928b8 | 2018-12-10 11:26:48 +0000 | [diff] [blame] | 750 | QemuUUID uuid; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 751 | |
Max Reitz | e381057 | 2018-03-12 17:55:28 +0100 | [diff] [blame] | 752 | assert(create_options->driver == BLOCKDEV_DRIVER_VDI); |
| 753 | vdi_opts = &create_options->u.vdi; |
| 754 | |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 755 | logout("\n"); |
| 756 | |
Kevin Wolf | da23248 | 2018-03-09 19:53:19 +0100 | [diff] [blame] | 757 | /* Validate options and set default values */ |
Max Reitz | 49858b5 | 2018-03-12 17:55:26 +0100 | [diff] [blame] | 758 | bytes = vdi_opts->size; |
Kevin Wolf | 61fa648 | 2018-03-20 15:08:00 +0100 | [diff] [blame] | 759 | |
| 760 | if (!vdi_opts->has_preallocation) { |
| 761 | vdi_opts->preallocation = PREALLOC_MODE_OFF; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 762 | } |
Kevin Wolf | 61fa648 | 2018-03-20 15:08:00 +0100 | [diff] [blame] | 763 | switch (vdi_opts->preallocation) { |
| 764 | case PREALLOC_MODE_OFF: |
| 765 | image_type = VDI_TYPE_DYNAMIC; |
| 766 | break; |
| 767 | case PREALLOC_MODE_METADATA: |
| 768 | image_type = VDI_TYPE_STATIC; |
| 769 | break; |
| 770 | default: |
| 771 | error_setg(errp, "Preallocation mode not supported for vdi"); |
| 772 | return -EINVAL; |
| 773 | } |
| 774 | |
Max Reitz | 49858b5 | 2018-03-12 17:55:26 +0100 | [diff] [blame] | 775 | #ifndef CONFIG_VDI_STATIC_IMAGE |
| 776 | if (image_type == VDI_TYPE_STATIC) { |
| 777 | ret = -ENOTSUP; |
| 778 | error_setg(errp, "Statically allocated images cannot be created in " |
| 779 | "this build"); |
| 780 | goto exit; |
| 781 | } |
| 782 | #endif |
| 783 | #ifndef CONFIG_VDI_BLOCK_SIZE |
| 784 | if (block_size != DEFAULT_CLUSTER_SIZE) { |
| 785 | ret = -ENOTSUP; |
| 786 | error_setg(errp, |
| 787 | "A non-default cluster size is not supported in this build"); |
| 788 | goto exit; |
| 789 | } |
Chunyan Liu | 004b7f2 | 2014-06-05 17:21:07 +0800 | [diff] [blame] | 790 | #endif |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 791 | |
Jeff Cody | 63fa06d | 2014-03-28 11:42:24 -0400 | [diff] [blame] | 792 | if (bytes > VDI_DISK_SIZE_MAX) { |
Jeff Cody | dddc775 | 2014-07-23 17:22:59 -0400 | [diff] [blame] | 793 | ret = -ENOTSUP; |
Jeff Cody | 63fa06d | 2014-03-28 11:42:24 -0400 | [diff] [blame] | 794 | error_setg(errp, "Unsupported VDI image size (size is 0x%" PRIx64 |
| 795 | ", max supported is 0x%" PRIx64 ")", |
| 796 | bytes, VDI_DISK_SIZE_MAX); |
| 797 | goto exit; |
| 798 | } |
| 799 | |
Kevin Wolf | da23248 | 2018-03-09 19:53:19 +0100 | [diff] [blame] | 800 | /* Create BlockBackend to write to the image */ |
Max Reitz | ec73f06 | 2018-03-12 17:55:27 +0100 | [diff] [blame] | 801 | bs_file = bdrv_open_blockdev_ref(vdi_opts->file, errp); |
| 802 | if (!bs_file) { |
| 803 | ret = -EIO; |
Jeff Cody | 63fa06d | 2014-03-28 11:42:24 -0400 | [diff] [blame] | 804 | goto exit; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 805 | } |
Kevin Wolf | a08f0c3 | 2016-03-08 15:57:05 +0100 | [diff] [blame] | 806 | |
Eric Blake | a3aeeab | 2020-04-28 14:26:46 -0500 | [diff] [blame] | 807 | blk = blk_new_with_bs(bs_file, BLK_PERM_WRITE | BLK_PERM_RESIZE, |
| 808 | BLK_PERM_ALL, errp); |
| 809 | if (!blk) { |
| 810 | ret = -EPERM; |
Jeff Cody | 7074786 | 2014-07-23 17:22:58 -0400 | [diff] [blame] | 811 | goto exit; |
Chunyan Liu | 4ab1559 | 2014-06-30 14:29:58 +0800 | [diff] [blame] | 812 | } |
| 813 | |
Kevin Wolf | a08f0c3 | 2016-03-08 15:57:05 +0100 | [diff] [blame] | 814 | blk_set_allow_write_beyond_eof(blk, true); |
| 815 | |
Stefan Weil | f21dc3a | 2010-05-12 20:25:45 +0200 | [diff] [blame] | 816 | /* We need enough blocks to store the given disk size, |
| 817 | so always round up. */ |
Max Reitz | e9082e4 | 2014-10-21 10:51:25 +0200 | [diff] [blame] | 818 | blocks = DIV_ROUND_UP(bytes, block_size); |
Stefan Weil | f21dc3a | 2010-05-12 20:25:45 +0200 | [diff] [blame] | 819 | |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 820 | bmap_size = blocks * sizeof(uint32_t); |
Max Reitz | e9082e4 | 2014-10-21 10:51:25 +0200 | [diff] [blame] | 821 | bmap_size = ROUND_UP(bmap_size, SECTOR_SIZE); |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 822 | |
| 823 | memset(&header, 0, sizeof(header)); |
Blue Swirl | 1786dc1 | 2009-08-15 11:33:58 +0000 | [diff] [blame] | 824 | pstrcpy(header.text, sizeof(header.text), VDI_TEXT); |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 825 | header.signature = VDI_SIGNATURE; |
| 826 | header.version = VDI_VERSION_1_1; |
| 827 | header.header_size = 0x180; |
| 828 | header.image_type = image_type; |
| 829 | header.offset_bmap = 0x200; |
| 830 | header.offset_data = 0x200 + bmap_size; |
| 831 | header.sector_size = SECTOR_SIZE; |
| 832 | header.disk_size = bytes; |
| 833 | header.block_size = block_size; |
| 834 | header.blocks_in_image = blocks; |
Stefan Weil | 6eea90e | 2009-08-14 21:50:02 +0200 | [diff] [blame] | 835 | if (image_type == VDI_TYPE_STATIC) { |
| 836 | header.blocks_allocated = blocks; |
| 837 | } |
Peter Maydell | ac928b8 | 2018-12-10 11:26:48 +0000 | [diff] [blame] | 838 | qemu_uuid_generate(&uuid); |
| 839 | header.uuid_image = uuid; |
| 840 | qemu_uuid_generate(&uuid); |
| 841 | header.uuid_last_snap = uuid; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 842 | /* There is no need to set header.uuid_link or header.uuid_parent here. */ |
Kevin Wolf | 95a14d5 | 2018-03-20 14:41:53 +0100 | [diff] [blame] | 843 | if (VDI_DEBUG) { |
| 844 | vdi_header_print(&header); |
| 845 | } |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 846 | vdi_header_to_le(&header); |
Eric Blake | 8341f00 | 2016-05-06 10:26:27 -0600 | [diff] [blame] | 847 | ret = blk_pwrite(blk, offset, &header, sizeof(header), 0); |
Jeff Cody | dddc775 | 2014-07-23 17:22:59 -0400 | [diff] [blame] | 848 | if (ret < 0) { |
Max Reitz | ec73f06 | 2018-03-12 17:55:27 +0100 | [diff] [blame] | 849 | error_setg(errp, "Error writing header"); |
Jeff Cody | 7074786 | 2014-07-23 17:22:58 -0400 | [diff] [blame] | 850 | goto exit; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 851 | } |
Jeff Cody | 7074786 | 2014-07-23 17:22:58 -0400 | [diff] [blame] | 852 | offset += sizeof(header); |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 853 | |
Stefan Weil | b76b6e9 | 2010-05-06 20:53:47 +0200 | [diff] [blame] | 854 | if (bmap_size > 0) { |
Kevin Wolf | 17cce73 | 2014-05-20 13:25:43 +0200 | [diff] [blame] | 855 | bmap = g_try_malloc0(bmap_size); |
| 856 | if (bmap == NULL) { |
| 857 | ret = -ENOMEM; |
| 858 | error_setg(errp, "Could not allocate bmap"); |
| 859 | goto exit; |
| 860 | } |
Stefan Weil | 514f21a | 2012-08-17 15:23:24 +0200 | [diff] [blame] | 861 | for (i = 0; i < blocks; i++) { |
| 862 | if (image_type == VDI_TYPE_STATIC) { |
| 863 | bmap[i] = i; |
| 864 | } else { |
| 865 | bmap[i] = VDI_UNALLOCATED; |
| 866 | } |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 867 | } |
Eric Blake | 8341f00 | 2016-05-06 10:26:27 -0600 | [diff] [blame] | 868 | ret = blk_pwrite(blk, offset, bmap, bmap_size, 0); |
Jeff Cody | dddc775 | 2014-07-23 17:22:59 -0400 | [diff] [blame] | 869 | if (ret < 0) { |
Max Reitz | ec73f06 | 2018-03-12 17:55:27 +0100 | [diff] [blame] | 870 | error_setg(errp, "Error writing bmap"); |
Jeff Cody | 7074786 | 2014-07-23 17:22:58 -0400 | [diff] [blame] | 871 | goto exit; |
Stefan Weil | 514f21a | 2012-08-17 15:23:24 +0200 | [diff] [blame] | 872 | } |
Jeff Cody | 7074786 | 2014-07-23 17:22:58 -0400 | [diff] [blame] | 873 | offset += bmap_size; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 874 | } |
Stefan Weil | 514f21a | 2012-08-17 15:23:24 +0200 | [diff] [blame] | 875 | |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 876 | if (image_type == VDI_TYPE_STATIC) { |
Max Reitz | c80d8b0 | 2019-09-18 11:51:40 +0200 | [diff] [blame] | 877 | ret = blk_truncate(blk, offset + blocks * block_size, false, |
Kevin Wolf | 8c6242b | 2020-04-24 14:54:41 +0200 | [diff] [blame] | 878 | PREALLOC_MODE_OFF, 0, errp); |
Jeff Cody | dddc775 | 2014-07-23 17:22:59 -0400 | [diff] [blame] | 879 | if (ret < 0) { |
Max Reitz | ec73f06 | 2018-03-12 17:55:27 +0100 | [diff] [blame] | 880 | error_prepend(errp, "Failed to statically allocate file"); |
Jeff Cody | 7074786 | 2014-07-23 17:22:58 -0400 | [diff] [blame] | 881 | goto exit; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 882 | } |
| 883 | } |
| 884 | |
Kevin Wolf | 53618dd | 2018-05-25 14:48:16 +0200 | [diff] [blame] | 885 | ret = 0; |
Jeff Cody | 63fa06d | 2014-03-28 11:42:24 -0400 | [diff] [blame] | 886 | exit: |
Kevin Wolf | a08f0c3 | 2016-03-08 15:57:05 +0100 | [diff] [blame] | 887 | blk_unref(blk); |
Max Reitz | ec73f06 | 2018-03-12 17:55:27 +0100 | [diff] [blame] | 888 | bdrv_unref(bs_file); |
Jeff Cody | 7074786 | 2014-07-23 17:22:58 -0400 | [diff] [blame] | 889 | g_free(bmap); |
Jeff Cody | dddc775 | 2014-07-23 17:22:59 -0400 | [diff] [blame] | 890 | return ret; |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 891 | } |
| 892 | |
Max Reitz | e381057 | 2018-03-12 17:55:28 +0100 | [diff] [blame] | 893 | static int coroutine_fn vdi_co_create(BlockdevCreateOptions *create_options, |
| 894 | Error **errp) |
| 895 | { |
| 896 | return vdi_co_do_create(create_options, DEFAULT_CLUSTER_SIZE, errp); |
| 897 | } |
| 898 | |
Maxim Levitsky | b92902d | 2020-03-26 03:12:17 +0200 | [diff] [blame] | 899 | static int coroutine_fn vdi_co_create_opts(BlockDriver *drv, |
| 900 | const char *filename, |
| 901 | QemuOpts *opts, |
Max Reitz | 49858b5 | 2018-03-12 17:55:26 +0100 | [diff] [blame] | 902 | Error **errp) |
| 903 | { |
| 904 | QDict *qdict = NULL; |
Max Reitz | e381057 | 2018-03-12 17:55:28 +0100 | [diff] [blame] | 905 | BlockdevCreateOptions *create_options = NULL; |
Max Reitz | ec73f06 | 2018-03-12 17:55:27 +0100 | [diff] [blame] | 906 | BlockDriverState *bs_file = NULL; |
Max Reitz | 49858b5 | 2018-03-12 17:55:26 +0100 | [diff] [blame] | 907 | uint64_t block_size = DEFAULT_CLUSTER_SIZE; |
Kevin Wolf | 61fa648 | 2018-03-20 15:08:00 +0100 | [diff] [blame] | 908 | bool is_static = false; |
Max Reitz | 49858b5 | 2018-03-12 17:55:26 +0100 | [diff] [blame] | 909 | Visitor *v; |
Max Reitz | 49858b5 | 2018-03-12 17:55:26 +0100 | [diff] [blame] | 910 | int ret; |
| 911 | |
Kevin Wolf | da23248 | 2018-03-09 19:53:19 +0100 | [diff] [blame] | 912 | /* Parse options and convert legacy syntax. |
| 913 | * |
| 914 | * Since CONFIG_VDI_BLOCK_SIZE is disabled by default, |
Max Reitz | 49858b5 | 2018-03-12 17:55:26 +0100 | [diff] [blame] | 915 | * cluster-size is not part of the QAPI schema; therefore we have |
| 916 | * to parse it before creating the QAPI object. */ |
| 917 | #if defined(CONFIG_VDI_BLOCK_SIZE) |
| 918 | block_size = qemu_opt_get_size_del(opts, |
| 919 | BLOCK_OPT_CLUSTER_SIZE, |
| 920 | DEFAULT_CLUSTER_SIZE); |
| 921 | if (block_size < BDRV_SECTOR_SIZE || block_size > UINT32_MAX || |
| 922 | !is_power_of_2(block_size)) |
| 923 | { |
| 924 | error_setg(errp, "Invalid cluster size"); |
| 925 | ret = -EINVAL; |
| 926 | goto done; |
| 927 | } |
| 928 | #endif |
Kevin Wolf | 61fa648 | 2018-03-20 15:08:00 +0100 | [diff] [blame] | 929 | if (qemu_opt_get_bool_del(opts, BLOCK_OPT_STATIC, false)) { |
| 930 | is_static = true; |
| 931 | } |
Max Reitz | 49858b5 | 2018-03-12 17:55:26 +0100 | [diff] [blame] | 932 | |
| 933 | qdict = qemu_opts_to_qdict_filtered(opts, NULL, &vdi_create_opts, true); |
| 934 | |
Kevin Wolf | da23248 | 2018-03-09 19:53:19 +0100 | [diff] [blame] | 935 | /* Create and open the file (protocol layer) */ |
Max Reitz | ec73f06 | 2018-03-12 17:55:27 +0100 | [diff] [blame] | 936 | ret = bdrv_create_file(filename, opts, errp); |
| 937 | if (ret < 0) { |
| 938 | goto done; |
| 939 | } |
| 940 | |
| 941 | bs_file = bdrv_open(filename, NULL, NULL, |
| 942 | BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp); |
| 943 | if (!bs_file) { |
| 944 | ret = -EIO; |
| 945 | goto done; |
| 946 | } |
| 947 | |
Max Reitz | e381057 | 2018-03-12 17:55:28 +0100 | [diff] [blame] | 948 | qdict_put_str(qdict, "driver", "vdi"); |
Max Reitz | ec73f06 | 2018-03-12 17:55:27 +0100 | [diff] [blame] | 949 | qdict_put_str(qdict, "file", bs_file->node_name); |
Kevin Wolf | 61fa648 | 2018-03-20 15:08:00 +0100 | [diff] [blame] | 950 | if (is_static) { |
| 951 | qdict_put_str(qdict, "preallocation", "metadata"); |
| 952 | } |
Max Reitz | 49858b5 | 2018-03-12 17:55:26 +0100 | [diff] [blame] | 953 | |
| 954 | /* Get the QAPI object */ |
Markus Armbruster | f853465 | 2018-06-14 21:14:34 +0200 | [diff] [blame] | 955 | v = qobject_input_visitor_new_flat_confused(qdict, errp); |
| 956 | if (!v) { |
| 957 | ret = -EINVAL; |
| 958 | goto done; |
| 959 | } |
Markus Armbruster | b11a093 | 2020-07-07 18:06:07 +0200 | [diff] [blame] | 960 | visit_type_BlockdevCreateOptions(v, NULL, &create_options, errp); |
Max Reitz | 49858b5 | 2018-03-12 17:55:26 +0100 | [diff] [blame] | 961 | visit_free(v); |
Markus Armbruster | b11a093 | 2020-07-07 18:06:07 +0200 | [diff] [blame] | 962 | if (!create_options) { |
Max Reitz | 49858b5 | 2018-03-12 17:55:26 +0100 | [diff] [blame] | 963 | ret = -EINVAL; |
| 964 | goto done; |
| 965 | } |
| 966 | |
Kevin Wolf | da23248 | 2018-03-09 19:53:19 +0100 | [diff] [blame] | 967 | /* Silently round up size */ |
Max Reitz | e381057 | 2018-03-12 17:55:28 +0100 | [diff] [blame] | 968 | assert(create_options->driver == BLOCKDEV_DRIVER_VDI); |
| 969 | create_options->u.vdi.size = ROUND_UP(create_options->u.vdi.size, |
| 970 | BDRV_SECTOR_SIZE); |
Max Reitz | 49858b5 | 2018-03-12 17:55:26 +0100 | [diff] [blame] | 971 | |
Kevin Wolf | da23248 | 2018-03-09 19:53:19 +0100 | [diff] [blame] | 972 | /* Create the vdi image (format layer) */ |
Max Reitz | ec73f06 | 2018-03-12 17:55:27 +0100 | [diff] [blame] | 973 | ret = vdi_co_do_create(create_options, block_size, errp); |
Max Reitz | 49858b5 | 2018-03-12 17:55:26 +0100 | [diff] [blame] | 974 | done: |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 975 | qobject_unref(qdict); |
Max Reitz | e381057 | 2018-03-12 17:55:28 +0100 | [diff] [blame] | 976 | qapi_free_BlockdevCreateOptions(create_options); |
Max Reitz | ec73f06 | 2018-03-12 17:55:27 +0100 | [diff] [blame] | 977 | bdrv_unref(bs_file); |
Max Reitz | 49858b5 | 2018-03-12 17:55:26 +0100 | [diff] [blame] | 978 | return ret; |
| 979 | } |
| 980 | |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 981 | static void vdi_close(BlockDriverState *bs) |
| 982 | { |
Kevin Wolf | fc9d106 | 2011-11-22 16:46:26 +0100 | [diff] [blame] | 983 | BDRVVdiState *s = bs->opaque; |
Kevin Wolf | 6ac5f38 | 2011-11-22 16:57:34 +0100 | [diff] [blame] | 984 | |
Kevin Wolf | 17cce73 | 2014-05-20 13:25:43 +0200 | [diff] [blame] | 985 | qemu_vfree(s->bmap); |
Kevin Wolf | 6ac5f38 | 2011-11-22 16:57:34 +0100 | [diff] [blame] | 986 | |
Kevin Wolf | fc9d106 | 2011-11-22 16:46:26 +0100 | [diff] [blame] | 987 | migrate_del_blocker(s->migration_blocker); |
| 988 | error_free(s->migration_blocker); |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 989 | } |
| 990 | |
Max Reitz | 0a28bf2 | 2019-07-24 19:12:35 +0200 | [diff] [blame] | 991 | static int vdi_has_zero_init(BlockDriverState *bs) |
| 992 | { |
| 993 | BDRVVdiState *s = bs->opaque; |
| 994 | |
| 995 | if (s->header.image_type == VDI_TYPE_STATIC) { |
| 996 | return bdrv_has_zero_init(bs->file->bs); |
| 997 | } else { |
| 998 | return 1; |
| 999 | } |
| 1000 | } |
| 1001 | |
Chunyan Liu | 004b7f2 | 2014-06-05 17:21:07 +0800 | [diff] [blame] | 1002 | static QemuOptsList vdi_create_opts = { |
| 1003 | .name = "vdi-create-opts", |
| 1004 | .head = QTAILQ_HEAD_INITIALIZER(vdi_create_opts.head), |
| 1005 | .desc = { |
| 1006 | { |
| 1007 | .name = BLOCK_OPT_SIZE, |
| 1008 | .type = QEMU_OPT_SIZE, |
| 1009 | .help = "Virtual disk size" |
| 1010 | }, |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 1011 | #if defined(CONFIG_VDI_BLOCK_SIZE) |
Chunyan Liu | 004b7f2 | 2014-06-05 17:21:07 +0800 | [diff] [blame] | 1012 | { |
| 1013 | .name = BLOCK_OPT_CLUSTER_SIZE, |
| 1014 | .type = QEMU_OPT_SIZE, |
| 1015 | .help = "VDI cluster (block) size", |
| 1016 | .def_value_str = stringify(DEFAULT_CLUSTER_SIZE) |
| 1017 | }, |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 1018 | #endif |
| 1019 | #if defined(CONFIG_VDI_STATIC_IMAGE) |
Chunyan Liu | 004b7f2 | 2014-06-05 17:21:07 +0800 | [diff] [blame] | 1020 | { |
| 1021 | .name = BLOCK_OPT_STATIC, |
| 1022 | .type = QEMU_OPT_BOOL, |
| 1023 | .help = "VDI static (pre-allocated) image", |
| 1024 | .def_value_str = "off" |
| 1025 | }, |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 1026 | #endif |
Chunyan Liu | 004b7f2 | 2014-06-05 17:21:07 +0800 | [diff] [blame] | 1027 | /* TODO: An additional option to set UUID values might be useful. */ |
| 1028 | { /* end of list */ } |
| 1029 | } |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 1030 | }; |
| 1031 | |
| 1032 | static BlockDriver bdrv_vdi = { |
| 1033 | .format_name = "vdi", |
| 1034 | .instance_size = sizeof(BDRVVdiState), |
| 1035 | .bdrv_probe = vdi_probe, |
| 1036 | .bdrv_open = vdi_open, |
| 1037 | .bdrv_close = vdi_close, |
Jeff Cody | ecfe2bb | 2012-09-20 15:13:32 -0400 | [diff] [blame] | 1038 | .bdrv_reopen_prepare = vdi_reopen_prepare, |
Max Reitz | 69dca43 | 2020-05-13 13:05:39 +0200 | [diff] [blame] | 1039 | .bdrv_child_perm = bdrv_default_perms, |
Max Reitz | e381057 | 2018-03-12 17:55:28 +0100 | [diff] [blame] | 1040 | .bdrv_co_create = vdi_co_create, |
Kevin Wolf | da23248 | 2018-03-09 19:53:19 +0100 | [diff] [blame] | 1041 | .bdrv_co_create_opts = vdi_co_create_opts, |
Max Reitz | 0a28bf2 | 2019-07-24 19:12:35 +0200 | [diff] [blame] | 1042 | .bdrv_has_zero_init = vdi_has_zero_init, |
Eric Blake | 67635f6 | 2018-02-13 14:26:57 -0600 | [diff] [blame] | 1043 | .bdrv_co_block_status = vdi_co_block_status, |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 1044 | .bdrv_make_empty = vdi_make_empty, |
| 1045 | |
Kevin Wolf | 0865bb6 | 2016-04-25 16:22:39 +0200 | [diff] [blame] | 1046 | .bdrv_co_preadv = vdi_co_preadv, |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 1047 | #if defined(CONFIG_VDI_WRITE) |
Kevin Wolf | fde9d56 | 2016-04-25 16:22:39 +0200 | [diff] [blame] | 1048 | .bdrv_co_pwritev = vdi_co_pwritev, |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 1049 | #endif |
| 1050 | |
| 1051 | .bdrv_get_info = vdi_get_info, |
| 1052 | |
Max Reitz | d67066d | 2020-05-13 13:05:12 +0200 | [diff] [blame] | 1053 | .is_format = true, |
Chunyan Liu | 004b7f2 | 2014-06-05 17:21:07 +0800 | [diff] [blame] | 1054 | .create_opts = &vdi_create_opts, |
Paolo Bonzini | 2fd6163 | 2018-03-01 17:36:19 +0100 | [diff] [blame] | 1055 | .bdrv_co_check = vdi_co_check, |
Stefan Weil | 9aebd98 | 2009-07-31 21:45:56 +0200 | [diff] [blame] | 1056 | }; |
| 1057 | |
| 1058 | static void bdrv_vdi_init(void) |
| 1059 | { |
| 1060 | logout("\n"); |
| 1061 | bdrv_register(&bdrv_vdi); |
| 1062 | } |
| 1063 | |
| 1064 | block_init(bdrv_vdi_init); |