1e8d4e5ffSJeff Cody /* 2e8d4e5ffSJeff Cody * Block driver for Hyper-V VHDX Images 3e8d4e5ffSJeff Cody * 4e8d4e5ffSJeff Cody * Copyright (c) 2013 Red Hat, Inc., 5e8d4e5ffSJeff Cody * 6e8d4e5ffSJeff Cody * Authors: 7e8d4e5ffSJeff Cody * Jeff Cody <jcody@redhat.com> 8e8d4e5ffSJeff Cody * 96e9d290bSJeff Cody * This is based on the "VHDX Format Specification v1.00", published 8/25/2012 10e8d4e5ffSJeff Cody * by Microsoft: 116e9d290bSJeff Cody * https://www.microsoft.com/en-us/download/details.aspx?id=34750 12e8d4e5ffSJeff Cody * 13e8d4e5ffSJeff Cody * This work is licensed under the terms of the GNU LGPL, version 2 or later. 14e8d4e5ffSJeff Cody * See the COPYING.LIB file in the top-level directory. 15e8d4e5ffSJeff Cody * 16e8d4e5ffSJeff Cody */ 17e8d4e5ffSJeff Cody 1880c71a24SPeter Maydell #include "qemu/osdep.h" 19da34e65cSMarkus Armbruster #include "qapi/error.h" 20e8d4e5ffSJeff Cody #include "block/block_int.h" 2110bf03afSKevin Wolf #include "sysemu/block-backend.h" 22e8d4e5ffSJeff Cody #include "qemu/module.h" 23922a01a0SMarkus Armbruster #include "qemu/option.h" 24e8d4e5ffSJeff Cody #include "qemu/crc32c.h" 2558369e22SPaolo Bonzini #include "qemu/bswap.h" 26e8d4e5ffSJeff Cody #include "block/vhdx.h" 27795c40b8SJuan Quintela #include "migration/blocker.h" 28cb6414dfSFam Zheng #include "qemu/uuid.h" 2909b68dabSKevin Wolf #include "qapi/qmp/qdict.h" 3009b68dabSKevin Wolf #include "qapi/qobject-input-visitor.h" 3109b68dabSKevin Wolf #include "qapi/qapi-visit-block-core.h" 323412f7b1SJeff Cody 333412f7b1SJeff Cody /* Options for VHDX creation */ 343412f7b1SJeff Cody 353412f7b1SJeff Cody #define VHDX_BLOCK_OPT_LOG_SIZE "log_size" 363412f7b1SJeff Cody #define VHDX_BLOCK_OPT_BLOCK_SIZE "block_size" 373412f7b1SJeff Cody #define VHDX_BLOCK_OPT_ZERO "block_state_zero" 383412f7b1SJeff Cody 393412f7b1SJeff Cody typedef enum VHDXImageType { 403412f7b1SJeff Cody VHDX_TYPE_DYNAMIC = 0, 413412f7b1SJeff Cody VHDX_TYPE_FIXED, 423412f7b1SJeff Cody VHDX_TYPE_DIFFERENCING, /* Currently unsupported */ 433412f7b1SJeff Cody } VHDXImageType; 44e8d4e5ffSJeff Cody 4509b68dabSKevin Wolf static QemuOptsList vhdx_create_opts; 4609b68dabSKevin Wolf 47e8d4e5ffSJeff Cody /* Several metadata and region table data entries are identified by 48e8d4e5ffSJeff Cody * guids in a MS-specific GUID format. */ 49e8d4e5ffSJeff Cody 50e8d4e5ffSJeff Cody 51e8d4e5ffSJeff Cody /* ------- Known Region Table GUIDs ---------------------- */ 52e8d4e5ffSJeff Cody static const MSGUID bat_guid = { .data1 = 0x2dc27766, 53e8d4e5ffSJeff Cody .data2 = 0xf623, 54e8d4e5ffSJeff Cody .data3 = 0x4200, 55e8d4e5ffSJeff Cody .data4 = { 0x9d, 0x64, 0x11, 0x5e, 56e8d4e5ffSJeff Cody 0x9b, 0xfd, 0x4a, 0x08} }; 57e8d4e5ffSJeff Cody 58e8d4e5ffSJeff Cody static const MSGUID metadata_guid = { .data1 = 0x8b7ca206, 59e8d4e5ffSJeff Cody .data2 = 0x4790, 60e8d4e5ffSJeff Cody .data3 = 0x4b9a, 61e8d4e5ffSJeff Cody .data4 = { 0xb8, 0xfe, 0x57, 0x5f, 62e8d4e5ffSJeff Cody 0x05, 0x0f, 0x88, 0x6e} }; 63e8d4e5ffSJeff Cody 64e8d4e5ffSJeff Cody 65e8d4e5ffSJeff Cody 66e8d4e5ffSJeff Cody /* ------- Known Metadata Entry GUIDs ---------------------- */ 67e8d4e5ffSJeff Cody static const MSGUID file_param_guid = { .data1 = 0xcaa16737, 68e8d4e5ffSJeff Cody .data2 = 0xfa36, 69e8d4e5ffSJeff Cody .data3 = 0x4d43, 70e8d4e5ffSJeff Cody .data4 = { 0xb3, 0xb6, 0x33, 0xf0, 71e8d4e5ffSJeff Cody 0xaa, 0x44, 0xe7, 0x6b} }; 72e8d4e5ffSJeff Cody 73e8d4e5ffSJeff Cody static const MSGUID virtual_size_guid = { .data1 = 0x2FA54224, 74e8d4e5ffSJeff Cody .data2 = 0xcd1b, 75e8d4e5ffSJeff Cody .data3 = 0x4876, 76e8d4e5ffSJeff Cody .data4 = { 0xb2, 0x11, 0x5d, 0xbe, 77e8d4e5ffSJeff Cody 0xd8, 0x3b, 0xf4, 0xb8} }; 78e8d4e5ffSJeff Cody 79e8d4e5ffSJeff Cody static const MSGUID page83_guid = { .data1 = 0xbeca12ab, 80e8d4e5ffSJeff Cody .data2 = 0xb2e6, 81e8d4e5ffSJeff Cody .data3 = 0x4523, 82e8d4e5ffSJeff Cody .data4 = { 0x93, 0xef, 0xc3, 0x09, 83e8d4e5ffSJeff Cody 0xe0, 0x00, 0xc7, 0x46} }; 84e8d4e5ffSJeff Cody 85e8d4e5ffSJeff Cody 86e8d4e5ffSJeff Cody static const MSGUID phys_sector_guid = { .data1 = 0xcda348c7, 87e8d4e5ffSJeff Cody .data2 = 0x445d, 88e8d4e5ffSJeff Cody .data3 = 0x4471, 89e8d4e5ffSJeff Cody .data4 = { 0x9c, 0xc9, 0xe9, 0x88, 90e8d4e5ffSJeff Cody 0x52, 0x51, 0xc5, 0x56} }; 91e8d4e5ffSJeff Cody 92e8d4e5ffSJeff Cody static const MSGUID parent_locator_guid = { .data1 = 0xa8d35f2d, 93e8d4e5ffSJeff Cody .data2 = 0xb30b, 94e8d4e5ffSJeff Cody .data3 = 0x454d, 95e8d4e5ffSJeff Cody .data4 = { 0xab, 0xf7, 0xd3, 96e8d4e5ffSJeff Cody 0xd8, 0x48, 0x34, 97e8d4e5ffSJeff Cody 0xab, 0x0c} }; 98e8d4e5ffSJeff Cody 99e8d4e5ffSJeff Cody static const MSGUID logical_sector_guid = { .data1 = 0x8141bf1d, 100e8d4e5ffSJeff Cody .data2 = 0xa96f, 101e8d4e5ffSJeff Cody .data3 = 0x4709, 102e8d4e5ffSJeff Cody .data4 = { 0xba, 0x47, 0xf2, 103e8d4e5ffSJeff Cody 0x33, 0xa8, 0xfa, 104e8d4e5ffSJeff Cody 0xab, 0x5f} }; 105e8d4e5ffSJeff Cody 106e8d4e5ffSJeff Cody /* Each parent type must have a valid GUID; this is for parent images 107e8d4e5ffSJeff Cody * of type 'VHDX'. If we were to allow e.g. a QCOW2 parent, we would 108e8d4e5ffSJeff Cody * need to make up our own QCOW2 GUID type */ 109c2ebb05eSPeter Maydell static const MSGUID parent_vhdx_guid __attribute__((unused)) 110c2ebb05eSPeter Maydell = { .data1 = 0xb04aefb7, 111e8d4e5ffSJeff Cody .data2 = 0xd19e, 112e8d4e5ffSJeff Cody .data3 = 0x4a81, 113e8d4e5ffSJeff Cody .data4 = { 0xb7, 0x89, 0x25, 0xb8, 114e8d4e5ffSJeff Cody 0xe9, 0x44, 0x59, 0x13} }; 115e8d4e5ffSJeff Cody 116e8d4e5ffSJeff Cody 117e8d4e5ffSJeff Cody #define META_FILE_PARAMETER_PRESENT 0x01 118e8d4e5ffSJeff Cody #define META_VIRTUAL_DISK_SIZE_PRESENT 0x02 119e8d4e5ffSJeff Cody #define META_PAGE_83_PRESENT 0x04 120e8d4e5ffSJeff Cody #define META_LOGICAL_SECTOR_SIZE_PRESENT 0x08 121e8d4e5ffSJeff Cody #define META_PHYS_SECTOR_SIZE_PRESENT 0x10 122e8d4e5ffSJeff Cody #define META_PARENT_LOCATOR_PRESENT 0x20 123e8d4e5ffSJeff Cody 124e8d4e5ffSJeff Cody #define META_ALL_PRESENT \ 125e8d4e5ffSJeff Cody (META_FILE_PARAMETER_PRESENT | META_VIRTUAL_DISK_SIZE_PRESENT | \ 126e8d4e5ffSJeff Cody META_PAGE_83_PRESENT | META_LOGICAL_SECTOR_SIZE_PRESENT | \ 127e8d4e5ffSJeff Cody META_PHYS_SECTOR_SIZE_PRESENT) 128e8d4e5ffSJeff Cody 129e8d4e5ffSJeff Cody 130059e2fbbSJeff Cody typedef struct VHDXSectorInfo { 131059e2fbbSJeff Cody uint32_t bat_idx; /* BAT entry index */ 132059e2fbbSJeff Cody uint32_t sectors_avail; /* sectors available in payload block */ 133059e2fbbSJeff Cody uint32_t bytes_left; /* bytes left in the block after data to r/w */ 134059e2fbbSJeff Cody uint32_t bytes_avail; /* bytes available in payload block */ 135059e2fbbSJeff Cody uint64_t file_offset; /* absolute offset in bytes, in file */ 136059e2fbbSJeff Cody uint64_t block_offset; /* block offset, in bytes */ 137059e2fbbSJeff Cody } VHDXSectorInfo; 138059e2fbbSJeff Cody 1394f18b782SJeff Cody /* Calculates new checksum. 1404f18b782SJeff Cody * 1414f18b782SJeff Cody * Zero is substituted during crc calculation for the original crc field 1424f18b782SJeff Cody * crc_offset: byte offset in buf of the buffer crc 1434f18b782SJeff Cody * buf: buffer pointer 1444f18b782SJeff Cody * size: size of buffer (must be > crc_offset+4) 1454f18b782SJeff Cody * 1464f75b52aSJeff Cody * Note: The buffer should have all multi-byte data in little-endian format, 1474f75b52aSJeff Cody * and the resulting checksum is in little endian format. 1484f18b782SJeff Cody */ 1494f18b782SJeff Cody uint32_t vhdx_update_checksum(uint8_t *buf, size_t size, int crc_offset) 1504f18b782SJeff Cody { 1514f18b782SJeff Cody uint32_t crc; 1524f18b782SJeff Cody 1534f18b782SJeff Cody assert(buf != NULL); 1544f18b782SJeff Cody assert(size > (crc_offset + sizeof(crc))); 1554f18b782SJeff Cody 1564f18b782SJeff Cody memset(buf + crc_offset, 0, sizeof(crc)); 1574f18b782SJeff Cody crc = crc32c(0xffffffff, buf, size); 1584f75b52aSJeff Cody cpu_to_le32s(&crc); 1594f18b782SJeff Cody memcpy(buf + crc_offset, &crc, sizeof(crc)); 1604f18b782SJeff Cody 1614f18b782SJeff Cody return crc; 1624f18b782SJeff Cody } 1634f18b782SJeff Cody 164e8d4e5ffSJeff Cody uint32_t vhdx_checksum_calc(uint32_t crc, uint8_t *buf, size_t size, 165e8d4e5ffSJeff Cody int crc_offset) 166e8d4e5ffSJeff Cody { 167e8d4e5ffSJeff Cody uint32_t crc_new; 168e8d4e5ffSJeff Cody uint32_t crc_orig; 169e8d4e5ffSJeff Cody assert(buf != NULL); 170e8d4e5ffSJeff Cody 171e8d4e5ffSJeff Cody if (crc_offset > 0) { 172e8d4e5ffSJeff Cody memcpy(&crc_orig, buf + crc_offset, sizeof(crc_orig)); 173e8d4e5ffSJeff Cody memset(buf + crc_offset, 0, sizeof(crc_orig)); 174e8d4e5ffSJeff Cody } 175e8d4e5ffSJeff Cody 176e8d4e5ffSJeff Cody crc_new = crc32c(crc, buf, size); 177e8d4e5ffSJeff Cody if (crc_offset > 0) { 178e8d4e5ffSJeff Cody memcpy(buf + crc_offset, &crc_orig, sizeof(crc_orig)); 179e8d4e5ffSJeff Cody } 180e8d4e5ffSJeff Cody 181e8d4e5ffSJeff Cody return crc_new; 182e8d4e5ffSJeff Cody } 183e8d4e5ffSJeff Cody 184e8d4e5ffSJeff Cody /* Validates the checksum of the buffer, with an in-place CRC. 185e8d4e5ffSJeff Cody * 186e8d4e5ffSJeff Cody * Zero is substituted during crc calculation for the original crc field, 187e8d4e5ffSJeff Cody * and the crc field is restored afterwards. But the buffer will be modifed 188e8d4e5ffSJeff Cody * during the calculation, so this may not be not suitable for multi-threaded 189e8d4e5ffSJeff Cody * use. 190e8d4e5ffSJeff Cody * 191e8d4e5ffSJeff Cody * crc_offset: byte offset in buf of the buffer crc 192e8d4e5ffSJeff Cody * buf: buffer pointer 193e8d4e5ffSJeff Cody * size: size of buffer (must be > crc_offset+4) 194e8d4e5ffSJeff Cody * 195e8d4e5ffSJeff Cody * returns true if checksum is valid, false otherwise 196e8d4e5ffSJeff Cody */ 197e8d4e5ffSJeff Cody bool vhdx_checksum_is_valid(uint8_t *buf, size_t size, int crc_offset) 198e8d4e5ffSJeff Cody { 199e8d4e5ffSJeff Cody uint32_t crc_orig; 200e8d4e5ffSJeff Cody uint32_t crc; 201e8d4e5ffSJeff Cody 202e8d4e5ffSJeff Cody assert(buf != NULL); 203e8d4e5ffSJeff Cody assert(size > (crc_offset + 4)); 204e8d4e5ffSJeff Cody 205e8d4e5ffSJeff Cody memcpy(&crc_orig, buf + crc_offset, sizeof(crc_orig)); 206e8d4e5ffSJeff Cody crc_orig = le32_to_cpu(crc_orig); 207e8d4e5ffSJeff Cody 208e8d4e5ffSJeff Cody crc = vhdx_checksum_calc(0xffffffff, buf, size, crc_offset); 209e8d4e5ffSJeff Cody 210e8d4e5ffSJeff Cody return crc == crc_orig; 211e8d4e5ffSJeff Cody } 212e8d4e5ffSJeff Cody 213e8d4e5ffSJeff Cody 214e8d4e5ffSJeff Cody /* 2154f18b782SJeff Cody * This generates a UUID that is compliant with the MS GUIDs used 2164f18b782SJeff Cody * in the VHDX spec (and elsewhere). 2174f18b782SJeff Cody */ 2184f18b782SJeff Cody void vhdx_guid_generate(MSGUID *guid) 2194f18b782SJeff Cody { 220cb6414dfSFam Zheng QemuUUID uuid; 2214f18b782SJeff Cody assert(guid != NULL); 2224f18b782SJeff Cody 223cb6414dfSFam Zheng qemu_uuid_generate(&uuid); 224cb6414dfSFam Zheng memcpy(guid, &uuid, sizeof(MSGUID)); 2254f18b782SJeff Cody } 2264f18b782SJeff Cody 2271a848fd4SJeff Cody /* Check for region overlaps inside the VHDX image */ 2281a848fd4SJeff Cody static int vhdx_region_check(BDRVVHDXState *s, uint64_t start, uint64_t length) 2291a848fd4SJeff Cody { 2301a848fd4SJeff Cody int ret = 0; 2311a848fd4SJeff Cody uint64_t end; 2321a848fd4SJeff Cody VHDXRegionEntry *r; 2331a848fd4SJeff Cody 2341a848fd4SJeff Cody end = start + length; 2351a848fd4SJeff Cody QLIST_FOREACH(r, &s->regions, entries) { 2361a848fd4SJeff Cody if (!((start >= r->end) || (end <= r->start))) { 2371a848fd4SJeff Cody ret = -EINVAL; 2381a848fd4SJeff Cody goto exit; 2391a848fd4SJeff Cody } 2401a848fd4SJeff Cody } 2411a848fd4SJeff Cody 2421a848fd4SJeff Cody exit: 2431a848fd4SJeff Cody return ret; 2441a848fd4SJeff Cody } 2451a848fd4SJeff Cody 2461a848fd4SJeff Cody /* Register a region for future checks */ 2471a848fd4SJeff Cody static void vhdx_region_register(BDRVVHDXState *s, 2481a848fd4SJeff Cody uint64_t start, uint64_t length) 2491a848fd4SJeff Cody { 2501a848fd4SJeff Cody VHDXRegionEntry *r; 2511a848fd4SJeff Cody 2521a848fd4SJeff Cody r = g_malloc0(sizeof(*r)); 2531a848fd4SJeff Cody 2541a848fd4SJeff Cody r->start = start; 2551a848fd4SJeff Cody r->end = start + length; 2561a848fd4SJeff Cody 2571a848fd4SJeff Cody QLIST_INSERT_HEAD(&s->regions, r, entries); 2581a848fd4SJeff Cody } 2591a848fd4SJeff Cody 2601a848fd4SJeff Cody /* Free all registered regions */ 2611a848fd4SJeff Cody static void vhdx_region_unregister_all(BDRVVHDXState *s) 2621a848fd4SJeff Cody { 2631a848fd4SJeff Cody VHDXRegionEntry *r, *r_next; 2641a848fd4SJeff Cody 2651a848fd4SJeff Cody QLIST_FOREACH_SAFE(r, &s->regions, entries, r_next) { 2661a848fd4SJeff Cody QLIST_REMOVE(r, entries); 2671a848fd4SJeff Cody g_free(r); 2681a848fd4SJeff Cody } 2691a848fd4SJeff Cody } 2701a848fd4SJeff Cody 2711e74a971SJeff Cody static void vhdx_set_shift_bits(BDRVVHDXState *s) 2721e74a971SJeff Cody { 27304a36158SMax Reitz s->logical_sector_size_bits = ctz32(s->logical_sector_size); 27404a36158SMax Reitz s->sectors_per_block_bits = ctz32(s->sectors_per_block); 27504a36158SMax Reitz s->chunk_ratio_bits = ctz64(s->chunk_ratio); 27604a36158SMax Reitz s->block_size_bits = ctz32(s->block_size); 2771e74a971SJeff Cody } 2781e74a971SJeff Cody 2794f18b782SJeff Cody /* 280e8d4e5ffSJeff Cody * Per the MS VHDX Specification, for every VHDX file: 281e8d4e5ffSJeff Cody * - The header section is fixed size - 1 MB 282e8d4e5ffSJeff Cody * - The header section is always the first "object" 283e8d4e5ffSJeff Cody * - The first 64KB of the header is the File Identifier 284e8d4e5ffSJeff Cody * - The first uint64 (8 bytes) is the VHDX Signature ("vhdxfile") 285e8d4e5ffSJeff Cody * - The following 512 bytes constitute a UTF-16 string identifiying the 286e8d4e5ffSJeff Cody * software that created the file, and is optional and diagnostic only. 287e8d4e5ffSJeff Cody * 288e8d4e5ffSJeff Cody * Therefore, we probe by looking for the vhdxfile signature "vhdxfile" 289e8d4e5ffSJeff Cody */ 290e8d4e5ffSJeff Cody static int vhdx_probe(const uint8_t *buf, int buf_size, const char *filename) 291e8d4e5ffSJeff Cody { 292e8d4e5ffSJeff Cody if (buf_size >= 8 && !memcmp(buf, "vhdxfile", 8)) { 293e8d4e5ffSJeff Cody return 100; 294e8d4e5ffSJeff Cody } 295e8d4e5ffSJeff Cody return 0; 296e8d4e5ffSJeff Cody } 297e8d4e5ffSJeff Cody 2981e74a971SJeff Cody /* 2991e74a971SJeff Cody * Writes the header to the specified offset. 3001e74a971SJeff Cody * 3011e74a971SJeff Cody * This will optionally read in buffer data from disk (otherwise zero-fill), 3021e74a971SJeff Cody * and then update the header checksum. Header is converted to proper 3031e74a971SJeff Cody * endianness before being written to the specified file offset 3041e74a971SJeff Cody */ 305cf2ab8fcSKevin Wolf static int vhdx_write_header(BdrvChild *file, VHDXHeader *hdr, 3061e74a971SJeff Cody uint64_t offset, bool read) 3071e74a971SJeff Cody { 308cf2ab8fcSKevin Wolf BlockDriverState *bs_file = file->bs; 3091e74a971SJeff Cody uint8_t *buffer = NULL; 3101e74a971SJeff Cody int ret; 3114f75b52aSJeff Cody VHDXHeader *header_le; 3121e74a971SJeff Cody 3131e74a971SJeff Cody assert(bs_file != NULL); 3141e74a971SJeff Cody assert(hdr != NULL); 3151e74a971SJeff Cody 3161e74a971SJeff Cody /* the header checksum is not over just the packed size of VHDXHeader, 3171e74a971SJeff Cody * but rather over the entire 'reserved' range for the header, which is 3181e74a971SJeff Cody * 4KB (VHDX_HEADER_SIZE). */ 3191e74a971SJeff Cody 3201e74a971SJeff Cody buffer = qemu_blockalign(bs_file, VHDX_HEADER_SIZE); 3211e74a971SJeff Cody if (read) { 3221e74a971SJeff Cody /* if true, we can't assume the extra reserved bytes are 0 */ 323cf2ab8fcSKevin Wolf ret = bdrv_pread(file, offset, buffer, VHDX_HEADER_SIZE); 3241e74a971SJeff Cody if (ret < 0) { 3251e74a971SJeff Cody goto exit; 3261e74a971SJeff Cody } 3271e74a971SJeff Cody } else { 3281e74a971SJeff Cody memset(buffer, 0, VHDX_HEADER_SIZE); 3291e74a971SJeff Cody } 3301e74a971SJeff Cody 3311e74a971SJeff Cody /* overwrite the actual VHDXHeader portion */ 3324f75b52aSJeff Cody header_le = (VHDXHeader *)buffer; 3334f75b52aSJeff Cody memcpy(header_le, hdr, sizeof(VHDXHeader)); 3344f75b52aSJeff Cody vhdx_header_le_export(hdr, header_le); 3354f75b52aSJeff Cody vhdx_update_checksum(buffer, VHDX_HEADER_SIZE, 3361e74a971SJeff Cody offsetof(VHDXHeader, checksum)); 337d9ca2ea2SKevin Wolf ret = bdrv_pwrite_sync(file, offset, header_le, sizeof(VHDXHeader)); 3381e74a971SJeff Cody 3391e74a971SJeff Cody exit: 3401e74a971SJeff Cody qemu_vfree(buffer); 3411e74a971SJeff Cody return ret; 3421e74a971SJeff Cody } 3431e74a971SJeff Cody 3444f18b782SJeff Cody /* Update the VHDX headers 3454f18b782SJeff Cody * 3464f18b782SJeff Cody * This follows the VHDX spec procedures for header updates. 3474f18b782SJeff Cody * 3484f18b782SJeff Cody * - non-current header is updated with largest sequence number 3494f18b782SJeff Cody */ 3504f18b782SJeff Cody static int vhdx_update_header(BlockDriverState *bs, BDRVVHDXState *s, 351c3906c5eSJeff Cody bool generate_data_write_guid, MSGUID *log_guid) 3524f18b782SJeff Cody { 3534f18b782SJeff Cody int ret = 0; 3544f18b782SJeff Cody int hdr_idx = 0; 3554f18b782SJeff Cody uint64_t header_offset = VHDX_HEADER1_OFFSET; 3564f18b782SJeff Cody 3574f18b782SJeff Cody VHDXHeader *active_header; 3584f18b782SJeff Cody VHDXHeader *inactive_header; 3594f18b782SJeff Cody 3604f18b782SJeff Cody /* operate on the non-current header */ 3614f18b782SJeff Cody if (s->curr_header == 0) { 3624f18b782SJeff Cody hdr_idx = 1; 3634f18b782SJeff Cody header_offset = VHDX_HEADER2_OFFSET; 3644f18b782SJeff Cody } 3654f18b782SJeff Cody 3664f18b782SJeff Cody active_header = s->headers[s->curr_header]; 3674f18b782SJeff Cody inactive_header = s->headers[hdr_idx]; 3684f18b782SJeff Cody 3694f18b782SJeff Cody inactive_header->sequence_number = active_header->sequence_number + 1; 3704f18b782SJeff Cody 3714f18b782SJeff Cody /* a new file guid must be generated before any file write, including 3724f18b782SJeff Cody * headers */ 3734f18b782SJeff Cody inactive_header->file_write_guid = s->session_guid; 3744f18b782SJeff Cody 3754f18b782SJeff Cody /* a new data guid only needs to be generated before any guest-visible 3764f18b782SJeff Cody * writes (i.e. something observable via virtual disk read) */ 3774f18b782SJeff Cody if (generate_data_write_guid) { 3784f18b782SJeff Cody vhdx_guid_generate(&inactive_header->data_write_guid); 3794f18b782SJeff Cody } 3804f18b782SJeff Cody 381c3906c5eSJeff Cody /* update the log guid if present */ 382c3906c5eSJeff Cody if (log_guid) { 383c3906c5eSJeff Cody inactive_header->log_guid = *log_guid; 384c3906c5eSJeff Cody } 385c3906c5eSJeff Cody 386cf2ab8fcSKevin Wolf ret = vhdx_write_header(bs->file, inactive_header, header_offset, true); 3874f18b782SJeff Cody if (ret < 0) { 3884f18b782SJeff Cody goto exit; 3894f18b782SJeff Cody } 3904f18b782SJeff Cody s->curr_header = hdr_idx; 3914f18b782SJeff Cody 3924f18b782SJeff Cody exit: 3934f18b782SJeff Cody return ret; 3944f18b782SJeff Cody } 3954f18b782SJeff Cody 3964f18b782SJeff Cody /* 3974f18b782SJeff Cody * The VHDX spec calls for header updates to be performed twice, so that both 3984f18b782SJeff Cody * the current and non-current header have valid info 3994f18b782SJeff Cody */ 400c3906c5eSJeff Cody int vhdx_update_headers(BlockDriverState *bs, BDRVVHDXState *s, 401c3906c5eSJeff Cody bool generate_data_write_guid, MSGUID *log_guid) 4024f18b782SJeff Cody { 4034f18b782SJeff Cody int ret; 4044f18b782SJeff Cody 405c3906c5eSJeff Cody ret = vhdx_update_header(bs, s, generate_data_write_guid, log_guid); 4064f18b782SJeff Cody if (ret < 0) { 4074f18b782SJeff Cody return ret; 4084f18b782SJeff Cody } 409c3906c5eSJeff Cody ret = vhdx_update_header(bs, s, generate_data_write_guid, log_guid); 4104f18b782SJeff Cody return ret; 4114f18b782SJeff Cody } 412e8d4e5ffSJeff Cody 413e8d4e5ffSJeff Cody /* opens the specified header block from the VHDX file header section */ 4146890aad4SPaolo Bonzini static void vhdx_parse_header(BlockDriverState *bs, BDRVVHDXState *s, 4156890aad4SPaolo Bonzini Error **errp) 416e8d4e5ffSJeff Cody { 4176890aad4SPaolo Bonzini int ret; 418e8d4e5ffSJeff Cody VHDXHeader *header1; 419e8d4e5ffSJeff Cody VHDXHeader *header2; 420e8d4e5ffSJeff Cody bool h1_valid = false; 421e8d4e5ffSJeff Cody bool h2_valid = false; 422e8d4e5ffSJeff Cody uint64_t h1_seq = 0; 423e8d4e5ffSJeff Cody uint64_t h2_seq = 0; 424e8d4e5ffSJeff Cody uint8_t *buffer; 425e8d4e5ffSJeff Cody 4266e9d290bSJeff Cody /* header1 & header2 are freed in vhdx_close() */ 427e8d4e5ffSJeff Cody header1 = qemu_blockalign(bs, sizeof(VHDXHeader)); 428e8d4e5ffSJeff Cody header2 = qemu_blockalign(bs, sizeof(VHDXHeader)); 429e8d4e5ffSJeff Cody 430e8d4e5ffSJeff Cody buffer = qemu_blockalign(bs, VHDX_HEADER_SIZE); 431e8d4e5ffSJeff Cody 432e8d4e5ffSJeff Cody s->headers[0] = header1; 433e8d4e5ffSJeff Cody s->headers[1] = header2; 434e8d4e5ffSJeff Cody 435e8d4e5ffSJeff Cody /* We have to read the whole VHDX_HEADER_SIZE instead of 436e8d4e5ffSJeff Cody * sizeof(VHDXHeader), because the checksum is over the whole 437e8d4e5ffSJeff Cody * region */ 438cf2ab8fcSKevin Wolf ret = bdrv_pread(bs->file, VHDX_HEADER1_OFFSET, buffer, 4399a4f4c31SKevin Wolf VHDX_HEADER_SIZE); 440e8d4e5ffSJeff Cody if (ret < 0) { 441e8d4e5ffSJeff Cody goto fail; 442e8d4e5ffSJeff Cody } 443e8d4e5ffSJeff Cody /* copy over just the relevant portion that we need */ 444e8d4e5ffSJeff Cody memcpy(header1, buffer, sizeof(VHDXHeader)); 445e8d4e5ffSJeff Cody 4464f75b52aSJeff Cody if (vhdx_checksum_is_valid(buffer, VHDX_HEADER_SIZE, 4)) { 4474f75b52aSJeff Cody vhdx_header_le_import(header1); 4484f75b52aSJeff Cody if (header1->signature == VHDX_HEADER_SIGNATURE && 449e8d4e5ffSJeff Cody header1->version == 1) { 450e8d4e5ffSJeff Cody h1_seq = header1->sequence_number; 451e8d4e5ffSJeff Cody h1_valid = true; 452e8d4e5ffSJeff Cody } 4534f75b52aSJeff Cody } 454e8d4e5ffSJeff Cody 455cf2ab8fcSKevin Wolf ret = bdrv_pread(bs->file, VHDX_HEADER2_OFFSET, buffer, 4569a4f4c31SKevin Wolf VHDX_HEADER_SIZE); 457e8d4e5ffSJeff Cody if (ret < 0) { 458e8d4e5ffSJeff Cody goto fail; 459e8d4e5ffSJeff Cody } 460e8d4e5ffSJeff Cody /* copy over just the relevant portion that we need */ 461e8d4e5ffSJeff Cody memcpy(header2, buffer, sizeof(VHDXHeader)); 462e8d4e5ffSJeff Cody 4634f75b52aSJeff Cody if (vhdx_checksum_is_valid(buffer, VHDX_HEADER_SIZE, 4)) { 4644f75b52aSJeff Cody vhdx_header_le_import(header2); 4654f75b52aSJeff Cody if (header2->signature == VHDX_HEADER_SIGNATURE && 466e8d4e5ffSJeff Cody header2->version == 1) { 467e8d4e5ffSJeff Cody h2_seq = header2->sequence_number; 468e8d4e5ffSJeff Cody h2_valid = true; 469e8d4e5ffSJeff Cody } 4704f75b52aSJeff Cody } 471e8d4e5ffSJeff Cody 472e8d4e5ffSJeff Cody /* If there is only 1 valid header (or no valid headers), we 473e8d4e5ffSJeff Cody * don't care what the sequence numbers are */ 474e8d4e5ffSJeff Cody if (h1_valid && !h2_valid) { 475e8d4e5ffSJeff Cody s->curr_header = 0; 476e8d4e5ffSJeff Cody } else if (!h1_valid && h2_valid) { 477e8d4e5ffSJeff Cody s->curr_header = 1; 478e8d4e5ffSJeff Cody } else if (!h1_valid && !h2_valid) { 479e8d4e5ffSJeff Cody goto fail; 480e8d4e5ffSJeff Cody } else { 481e8d4e5ffSJeff Cody /* If both headers are valid, then we choose the active one by the 482e8d4e5ffSJeff Cody * highest sequence number. If the sequence numbers are equal, that is 483e8d4e5ffSJeff Cody * invalid */ 484e8d4e5ffSJeff Cody if (h1_seq > h2_seq) { 485e8d4e5ffSJeff Cody s->curr_header = 0; 486e8d4e5ffSJeff Cody } else if (h2_seq > h1_seq) { 487e8d4e5ffSJeff Cody s->curr_header = 1; 488e8d4e5ffSJeff Cody } else { 48969060461SJeff Cody /* The Microsoft Disk2VHD tool will create 2 identical 49069060461SJeff Cody * headers, with identical sequence numbers. If the headers are 49169060461SJeff Cody * identical, don't consider the file corrupt */ 49269060461SJeff Cody if (!memcmp(header1, header2, sizeof(VHDXHeader))) { 49369060461SJeff Cody s->curr_header = 0; 49469060461SJeff Cody } else { 495e8d4e5ffSJeff Cody goto fail; 496e8d4e5ffSJeff Cody } 497e8d4e5ffSJeff Cody } 49869060461SJeff Cody } 499e8d4e5ffSJeff Cody 5001a848fd4SJeff Cody vhdx_region_register(s, s->headers[s->curr_header]->log_offset, 5011a848fd4SJeff Cody s->headers[s->curr_header]->log_length); 502e8d4e5ffSJeff Cody goto exit; 503e8d4e5ffSJeff Cody 504e8d4e5ffSJeff Cody fail: 5056890aad4SPaolo Bonzini error_setg_errno(errp, -ret, "No valid VHDX header found"); 506e8d4e5ffSJeff Cody qemu_vfree(header1); 507e8d4e5ffSJeff Cody qemu_vfree(header2); 508e8d4e5ffSJeff Cody s->headers[0] = NULL; 509e8d4e5ffSJeff Cody s->headers[1] = NULL; 510e8d4e5ffSJeff Cody exit: 511e8d4e5ffSJeff Cody qemu_vfree(buffer); 512e8d4e5ffSJeff Cody } 513e8d4e5ffSJeff Cody 514e8d4e5ffSJeff Cody 515e8d4e5ffSJeff Cody static int vhdx_open_region_tables(BlockDriverState *bs, BDRVVHDXState *s) 516e8d4e5ffSJeff Cody { 517e8d4e5ffSJeff Cody int ret = 0; 518e8d4e5ffSJeff Cody uint8_t *buffer; 519e8d4e5ffSJeff Cody int offset = 0; 520e8d4e5ffSJeff Cody VHDXRegionTableEntry rt_entry; 521e8d4e5ffSJeff Cody uint32_t i; 522e8d4e5ffSJeff Cody bool bat_rt_found = false; 523e8d4e5ffSJeff Cody bool metadata_rt_found = false; 524e8d4e5ffSJeff Cody 525e8d4e5ffSJeff Cody /* We have to read the whole 64KB block, because the crc32 is over the 526e8d4e5ffSJeff Cody * whole block */ 527e8d4e5ffSJeff Cody buffer = qemu_blockalign(bs, VHDX_HEADER_BLOCK_SIZE); 528e8d4e5ffSJeff Cody 529cf2ab8fcSKevin Wolf ret = bdrv_pread(bs->file, VHDX_REGION_TABLE_OFFSET, buffer, 530e8d4e5ffSJeff Cody VHDX_HEADER_BLOCK_SIZE); 531e8d4e5ffSJeff Cody if (ret < 0) { 532e8d4e5ffSJeff Cody goto fail; 533e8d4e5ffSJeff Cody } 534e8d4e5ffSJeff Cody memcpy(&s->rt, buffer, sizeof(s->rt)); 535e8d4e5ffSJeff Cody offset += sizeof(s->rt); 536e8d4e5ffSJeff Cody 5374f75b52aSJeff Cody if (!vhdx_checksum_is_valid(buffer, VHDX_HEADER_BLOCK_SIZE, 4)) { 538e8d4e5ffSJeff Cody ret = -EINVAL; 539e8d4e5ffSJeff Cody goto fail; 540e8d4e5ffSJeff Cody } 541e8d4e5ffSJeff Cody 5424f75b52aSJeff Cody vhdx_region_header_le_import(&s->rt); 5434f75b52aSJeff Cody 5444f75b52aSJeff Cody if (s->rt.signature != VHDX_REGION_SIGNATURE) { 5454f75b52aSJeff Cody ret = -EINVAL; 5464f75b52aSJeff Cody goto fail; 5474f75b52aSJeff Cody } 5484f75b52aSJeff Cody 5494f75b52aSJeff Cody 550e8d4e5ffSJeff Cody /* Per spec, maximum region table entry count is 2047 */ 551e8d4e5ffSJeff Cody if (s->rt.entry_count > 2047) { 552e8d4e5ffSJeff Cody ret = -EINVAL; 553e8d4e5ffSJeff Cody goto fail; 554e8d4e5ffSJeff Cody } 555e8d4e5ffSJeff Cody 556e8d4e5ffSJeff Cody for (i = 0; i < s->rt.entry_count; i++) { 557e8d4e5ffSJeff Cody memcpy(&rt_entry, buffer + offset, sizeof(rt_entry)); 558e8d4e5ffSJeff Cody offset += sizeof(rt_entry); 559e8d4e5ffSJeff Cody 560c325ee1dSJeff Cody vhdx_region_entry_le_import(&rt_entry); 561e8d4e5ffSJeff Cody 5621a848fd4SJeff Cody /* check for region overlap between these entries, and any 5631a848fd4SJeff Cody * other memory regions in the file */ 5641a848fd4SJeff Cody ret = vhdx_region_check(s, rt_entry.file_offset, rt_entry.length); 5651a848fd4SJeff Cody if (ret < 0) { 5661a848fd4SJeff Cody goto fail; 5671a848fd4SJeff Cody } 5681a848fd4SJeff Cody 5691a848fd4SJeff Cody vhdx_region_register(s, rt_entry.file_offset, rt_entry.length); 5701a848fd4SJeff Cody 571e8d4e5ffSJeff Cody /* see if we recognize the entry */ 572e8d4e5ffSJeff Cody if (guid_eq(rt_entry.guid, bat_guid)) { 573e8d4e5ffSJeff Cody /* must be unique; if we have already found it this is invalid */ 574e8d4e5ffSJeff Cody if (bat_rt_found) { 575e8d4e5ffSJeff Cody ret = -EINVAL; 576e8d4e5ffSJeff Cody goto fail; 577e8d4e5ffSJeff Cody } 578e8d4e5ffSJeff Cody bat_rt_found = true; 579e8d4e5ffSJeff Cody s->bat_rt = rt_entry; 580e8d4e5ffSJeff Cody continue; 581e8d4e5ffSJeff Cody } 582e8d4e5ffSJeff Cody 583e8d4e5ffSJeff Cody if (guid_eq(rt_entry.guid, metadata_guid)) { 584e8d4e5ffSJeff Cody /* must be unique; if we have already found it this is invalid */ 585e8d4e5ffSJeff Cody if (metadata_rt_found) { 586e8d4e5ffSJeff Cody ret = -EINVAL; 587e8d4e5ffSJeff Cody goto fail; 588e8d4e5ffSJeff Cody } 589e8d4e5ffSJeff Cody metadata_rt_found = true; 590e8d4e5ffSJeff Cody s->metadata_rt = rt_entry; 591e8d4e5ffSJeff Cody continue; 592e8d4e5ffSJeff Cody } 593e8d4e5ffSJeff Cody 594e8d4e5ffSJeff Cody if (rt_entry.data_bits & VHDX_REGION_ENTRY_REQUIRED) { 595e8d4e5ffSJeff Cody /* cannot read vhdx file - required region table entry that 596e8d4e5ffSJeff Cody * we do not understand. per spec, we must fail to open */ 597e8d4e5ffSJeff Cody ret = -ENOTSUP; 598e8d4e5ffSJeff Cody goto fail; 599e8d4e5ffSJeff Cody } 600e8d4e5ffSJeff Cody } 6011a848fd4SJeff Cody 6021a848fd4SJeff Cody if (!bat_rt_found || !metadata_rt_found) { 6031a848fd4SJeff Cody ret = -EINVAL; 6041a848fd4SJeff Cody goto fail; 6051a848fd4SJeff Cody } 6061a848fd4SJeff Cody 607e8d4e5ffSJeff Cody ret = 0; 608e8d4e5ffSJeff Cody 609e8d4e5ffSJeff Cody fail: 610e8d4e5ffSJeff Cody qemu_vfree(buffer); 611e8d4e5ffSJeff Cody return ret; 612e8d4e5ffSJeff Cody } 613e8d4e5ffSJeff Cody 614e8d4e5ffSJeff Cody 615e8d4e5ffSJeff Cody 616e8d4e5ffSJeff Cody /* Metadata initial parser 617e8d4e5ffSJeff Cody * 618e8d4e5ffSJeff Cody * This loads all the metadata entry fields. This may cause additional 619e8d4e5ffSJeff Cody * fields to be processed (e.g. parent locator, etc..). 620e8d4e5ffSJeff Cody * 621e8d4e5ffSJeff Cody * There are 5 Metadata items that are always required: 622e8d4e5ffSJeff Cody * - File Parameters (block size, has a parent) 623e8d4e5ffSJeff Cody * - Virtual Disk Size (size, in bytes, of the virtual drive) 624e8d4e5ffSJeff Cody * - Page 83 Data (scsi page 83 guid) 625e8d4e5ffSJeff Cody * - Logical Sector Size (logical sector size in bytes, either 512 or 626e8d4e5ffSJeff Cody * 4096. We only support 512 currently) 627e8d4e5ffSJeff Cody * - Physical Sector Size (512 or 4096) 628e8d4e5ffSJeff Cody * 629e8d4e5ffSJeff Cody * Also, if the File Parameters indicate this is a differencing file, 630e8d4e5ffSJeff Cody * we must also look for the Parent Locator metadata item. 631e8d4e5ffSJeff Cody */ 632e8d4e5ffSJeff Cody static int vhdx_parse_metadata(BlockDriverState *bs, BDRVVHDXState *s) 633e8d4e5ffSJeff Cody { 634e8d4e5ffSJeff Cody int ret = 0; 635e8d4e5ffSJeff Cody uint8_t *buffer; 636e8d4e5ffSJeff Cody int offset = 0; 637e8d4e5ffSJeff Cody uint32_t i = 0; 638e8d4e5ffSJeff Cody VHDXMetadataTableEntry md_entry; 639e8d4e5ffSJeff Cody 640e8d4e5ffSJeff Cody buffer = qemu_blockalign(bs, VHDX_METADATA_TABLE_MAX_SIZE); 641e8d4e5ffSJeff Cody 642cf2ab8fcSKevin Wolf ret = bdrv_pread(bs->file, s->metadata_rt.file_offset, buffer, 643e8d4e5ffSJeff Cody VHDX_METADATA_TABLE_MAX_SIZE); 644e8d4e5ffSJeff Cody if (ret < 0) { 645e8d4e5ffSJeff Cody goto exit; 646e8d4e5ffSJeff Cody } 647e8d4e5ffSJeff Cody memcpy(&s->metadata_hdr, buffer, sizeof(s->metadata_hdr)); 648e8d4e5ffSJeff Cody offset += sizeof(s->metadata_hdr); 649e8d4e5ffSJeff Cody 650c325ee1dSJeff Cody vhdx_metadata_header_le_import(&s->metadata_hdr); 651e8d4e5ffSJeff Cody 6524f75b52aSJeff Cody if (s->metadata_hdr.signature != VHDX_METADATA_SIGNATURE) { 653e8d4e5ffSJeff Cody ret = -EINVAL; 654e8d4e5ffSJeff Cody goto exit; 655e8d4e5ffSJeff Cody } 656e8d4e5ffSJeff Cody 657e8d4e5ffSJeff Cody s->metadata_entries.present = 0; 658e8d4e5ffSJeff Cody 659e8d4e5ffSJeff Cody if ((s->metadata_hdr.entry_count * sizeof(md_entry)) > 660e8d4e5ffSJeff Cody (VHDX_METADATA_TABLE_MAX_SIZE - offset)) { 661e8d4e5ffSJeff Cody ret = -EINVAL; 662e8d4e5ffSJeff Cody goto exit; 663e8d4e5ffSJeff Cody } 664e8d4e5ffSJeff Cody 665e8d4e5ffSJeff Cody for (i = 0; i < s->metadata_hdr.entry_count; i++) { 666e8d4e5ffSJeff Cody memcpy(&md_entry, buffer + offset, sizeof(md_entry)); 667e8d4e5ffSJeff Cody offset += sizeof(md_entry); 668e8d4e5ffSJeff Cody 669c325ee1dSJeff Cody vhdx_metadata_entry_le_import(&md_entry); 670e8d4e5ffSJeff Cody 671e8d4e5ffSJeff Cody if (guid_eq(md_entry.item_id, file_param_guid)) { 672e8d4e5ffSJeff Cody if (s->metadata_entries.present & META_FILE_PARAMETER_PRESENT) { 673e8d4e5ffSJeff Cody ret = -EINVAL; 674e8d4e5ffSJeff Cody goto exit; 675e8d4e5ffSJeff Cody } 676e8d4e5ffSJeff Cody s->metadata_entries.file_parameters_entry = md_entry; 677e8d4e5ffSJeff Cody s->metadata_entries.present |= META_FILE_PARAMETER_PRESENT; 678e8d4e5ffSJeff Cody continue; 679e8d4e5ffSJeff Cody } 680e8d4e5ffSJeff Cody 681e8d4e5ffSJeff Cody if (guid_eq(md_entry.item_id, virtual_size_guid)) { 682e8d4e5ffSJeff Cody if (s->metadata_entries.present & META_VIRTUAL_DISK_SIZE_PRESENT) { 683e8d4e5ffSJeff Cody ret = -EINVAL; 684e8d4e5ffSJeff Cody goto exit; 685e8d4e5ffSJeff Cody } 686e8d4e5ffSJeff Cody s->metadata_entries.virtual_disk_size_entry = md_entry; 687e8d4e5ffSJeff Cody s->metadata_entries.present |= META_VIRTUAL_DISK_SIZE_PRESENT; 688e8d4e5ffSJeff Cody continue; 689e8d4e5ffSJeff Cody } 690e8d4e5ffSJeff Cody 691e8d4e5ffSJeff Cody if (guid_eq(md_entry.item_id, page83_guid)) { 692e8d4e5ffSJeff Cody if (s->metadata_entries.present & META_PAGE_83_PRESENT) { 693e8d4e5ffSJeff Cody ret = -EINVAL; 694e8d4e5ffSJeff Cody goto exit; 695e8d4e5ffSJeff Cody } 696e8d4e5ffSJeff Cody s->metadata_entries.page83_data_entry = md_entry; 697e8d4e5ffSJeff Cody s->metadata_entries.present |= META_PAGE_83_PRESENT; 698e8d4e5ffSJeff Cody continue; 699e8d4e5ffSJeff Cody } 700e8d4e5ffSJeff Cody 701e8d4e5ffSJeff Cody if (guid_eq(md_entry.item_id, logical_sector_guid)) { 702e8d4e5ffSJeff Cody if (s->metadata_entries.present & 703e8d4e5ffSJeff Cody META_LOGICAL_SECTOR_SIZE_PRESENT) { 704e8d4e5ffSJeff Cody ret = -EINVAL; 705e8d4e5ffSJeff Cody goto exit; 706e8d4e5ffSJeff Cody } 707e8d4e5ffSJeff Cody s->metadata_entries.logical_sector_size_entry = md_entry; 708e8d4e5ffSJeff Cody s->metadata_entries.present |= META_LOGICAL_SECTOR_SIZE_PRESENT; 709e8d4e5ffSJeff Cody continue; 710e8d4e5ffSJeff Cody } 711e8d4e5ffSJeff Cody 712e8d4e5ffSJeff Cody if (guid_eq(md_entry.item_id, phys_sector_guid)) { 713e8d4e5ffSJeff Cody if (s->metadata_entries.present & META_PHYS_SECTOR_SIZE_PRESENT) { 714e8d4e5ffSJeff Cody ret = -EINVAL; 715e8d4e5ffSJeff Cody goto exit; 716e8d4e5ffSJeff Cody } 717e8d4e5ffSJeff Cody s->metadata_entries.phys_sector_size_entry = md_entry; 718e8d4e5ffSJeff Cody s->metadata_entries.present |= META_PHYS_SECTOR_SIZE_PRESENT; 719e8d4e5ffSJeff Cody continue; 720e8d4e5ffSJeff Cody } 721e8d4e5ffSJeff Cody 722e8d4e5ffSJeff Cody if (guid_eq(md_entry.item_id, parent_locator_guid)) { 723e8d4e5ffSJeff Cody if (s->metadata_entries.present & META_PARENT_LOCATOR_PRESENT) { 724e8d4e5ffSJeff Cody ret = -EINVAL; 725e8d4e5ffSJeff Cody goto exit; 726e8d4e5ffSJeff Cody } 727e8d4e5ffSJeff Cody s->metadata_entries.parent_locator_entry = md_entry; 728e8d4e5ffSJeff Cody s->metadata_entries.present |= META_PARENT_LOCATOR_PRESENT; 729e8d4e5ffSJeff Cody continue; 730e8d4e5ffSJeff Cody } 731e8d4e5ffSJeff Cody 732e8d4e5ffSJeff Cody if (md_entry.data_bits & VHDX_META_FLAGS_IS_REQUIRED) { 733e8d4e5ffSJeff Cody /* cannot read vhdx file - required region table entry that 734e8d4e5ffSJeff Cody * we do not understand. per spec, we must fail to open */ 735e8d4e5ffSJeff Cody ret = -ENOTSUP; 736e8d4e5ffSJeff Cody goto exit; 737e8d4e5ffSJeff Cody } 738e8d4e5ffSJeff Cody } 739e8d4e5ffSJeff Cody 740e8d4e5ffSJeff Cody if (s->metadata_entries.present != META_ALL_PRESENT) { 741e8d4e5ffSJeff Cody ret = -ENOTSUP; 742e8d4e5ffSJeff Cody goto exit; 743e8d4e5ffSJeff Cody } 744e8d4e5ffSJeff Cody 745cf2ab8fcSKevin Wolf ret = bdrv_pread(bs->file, 746e8d4e5ffSJeff Cody s->metadata_entries.file_parameters_entry.offset 747e8d4e5ffSJeff Cody + s->metadata_rt.file_offset, 748e8d4e5ffSJeff Cody &s->params, 749e8d4e5ffSJeff Cody sizeof(s->params)); 750e8d4e5ffSJeff Cody 751e8d4e5ffSJeff Cody if (ret < 0) { 752e8d4e5ffSJeff Cody goto exit; 753e8d4e5ffSJeff Cody } 754e8d4e5ffSJeff Cody 755e8d4e5ffSJeff Cody le32_to_cpus(&s->params.block_size); 756e8d4e5ffSJeff Cody le32_to_cpus(&s->params.data_bits); 757e8d4e5ffSJeff Cody 758e8d4e5ffSJeff Cody 759e8d4e5ffSJeff Cody /* We now have the file parameters, so we can tell if this is a 760e8d4e5ffSJeff Cody * differencing file (i.e.. has_parent), is dynamic or fixed 761e8d4e5ffSJeff Cody * sized (leave_blocks_allocated), and the block size */ 762e8d4e5ffSJeff Cody 763e8d4e5ffSJeff Cody /* The parent locator required iff the file parameters has_parent set */ 764e8d4e5ffSJeff Cody if (s->params.data_bits & VHDX_PARAMS_HAS_PARENT) { 765e8d4e5ffSJeff Cody if (s->metadata_entries.present & META_PARENT_LOCATOR_PRESENT) { 766e8d4e5ffSJeff Cody /* TODO: parse parent locator fields */ 767e8d4e5ffSJeff Cody ret = -ENOTSUP; /* temp, until differencing files are supported */ 768e8d4e5ffSJeff Cody goto exit; 769e8d4e5ffSJeff Cody } else { 770e8d4e5ffSJeff Cody /* if has_parent is set, but there is not parent locator present, 771e8d4e5ffSJeff Cody * then that is an invalid combination */ 772e8d4e5ffSJeff Cody ret = -EINVAL; 773e8d4e5ffSJeff Cody goto exit; 774e8d4e5ffSJeff Cody } 775e8d4e5ffSJeff Cody } 776e8d4e5ffSJeff Cody 777e8d4e5ffSJeff Cody /* determine virtual disk size, logical sector size, 778e8d4e5ffSJeff Cody * and phys sector size */ 779e8d4e5ffSJeff Cody 780cf2ab8fcSKevin Wolf ret = bdrv_pread(bs->file, 781e8d4e5ffSJeff Cody s->metadata_entries.virtual_disk_size_entry.offset 782e8d4e5ffSJeff Cody + s->metadata_rt.file_offset, 783e8d4e5ffSJeff Cody &s->virtual_disk_size, 784e8d4e5ffSJeff Cody sizeof(uint64_t)); 785e8d4e5ffSJeff Cody if (ret < 0) { 786e8d4e5ffSJeff Cody goto exit; 787e8d4e5ffSJeff Cody } 788cf2ab8fcSKevin Wolf ret = bdrv_pread(bs->file, 789e8d4e5ffSJeff Cody s->metadata_entries.logical_sector_size_entry.offset 790e8d4e5ffSJeff Cody + s->metadata_rt.file_offset, 791e8d4e5ffSJeff Cody &s->logical_sector_size, 792e8d4e5ffSJeff Cody sizeof(uint32_t)); 793e8d4e5ffSJeff Cody if (ret < 0) { 794e8d4e5ffSJeff Cody goto exit; 795e8d4e5ffSJeff Cody } 796cf2ab8fcSKevin Wolf ret = bdrv_pread(bs->file, 797e8d4e5ffSJeff Cody s->metadata_entries.phys_sector_size_entry.offset 798e8d4e5ffSJeff Cody + s->metadata_rt.file_offset, 799e8d4e5ffSJeff Cody &s->physical_sector_size, 800e8d4e5ffSJeff Cody sizeof(uint32_t)); 801e8d4e5ffSJeff Cody if (ret < 0) { 802e8d4e5ffSJeff Cody goto exit; 803e8d4e5ffSJeff Cody } 804e8d4e5ffSJeff Cody 805e8d4e5ffSJeff Cody le64_to_cpus(&s->virtual_disk_size); 806e8d4e5ffSJeff Cody le32_to_cpus(&s->logical_sector_size); 807e8d4e5ffSJeff Cody le32_to_cpus(&s->physical_sector_size); 808e8d4e5ffSJeff Cody 8091d7678deSJeff Cody if (s->params.block_size < VHDX_BLOCK_SIZE_MIN || 8101d7678deSJeff Cody s->params.block_size > VHDX_BLOCK_SIZE_MAX) { 811e8d4e5ffSJeff Cody ret = -EINVAL; 812e8d4e5ffSJeff Cody goto exit; 813e8d4e5ffSJeff Cody } 814e8d4e5ffSJeff Cody 8151d7678deSJeff Cody /* only 2 supported sector sizes */ 8161d7678deSJeff Cody if (s->logical_sector_size != 512 && s->logical_sector_size != 4096) { 8171d7678deSJeff Cody ret = -EINVAL; 8181d7678deSJeff Cody goto exit; 8191d7678deSJeff Cody } 8201d7678deSJeff Cody 8211d7678deSJeff Cody /* Both block_size and sector_size are guaranteed powers of 2, below. 8221d7678deSJeff Cody Due to range checks above, s->sectors_per_block can never be < 256 */ 823e8d4e5ffSJeff Cody s->sectors_per_block = s->params.block_size / s->logical_sector_size; 824e8d4e5ffSJeff Cody s->chunk_ratio = (VHDX_MAX_SECTORS_PER_BLOCK) * 825e8d4e5ffSJeff Cody (uint64_t)s->logical_sector_size / 826e8d4e5ffSJeff Cody (uint64_t)s->params.block_size; 827e8d4e5ffSJeff Cody 828e8d4e5ffSJeff Cody /* These values are ones we will want to use for division / multiplication 829e8d4e5ffSJeff Cody * later on, and they are all guaranteed (per the spec) to be powers of 2, 830e8d4e5ffSJeff Cody * so we can take advantage of that for shift operations during 831e8d4e5ffSJeff Cody * reads/writes */ 832e8d4e5ffSJeff Cody if (s->logical_sector_size & (s->logical_sector_size - 1)) { 833e8d4e5ffSJeff Cody ret = -EINVAL; 834e8d4e5ffSJeff Cody goto exit; 835e8d4e5ffSJeff Cody } 836e8d4e5ffSJeff Cody if (s->sectors_per_block & (s->sectors_per_block - 1)) { 837e8d4e5ffSJeff Cody ret = -EINVAL; 838e8d4e5ffSJeff Cody goto exit; 839e8d4e5ffSJeff Cody } 840e8d4e5ffSJeff Cody if (s->chunk_ratio & (s->chunk_ratio - 1)) { 841e8d4e5ffSJeff Cody ret = -EINVAL; 842e8d4e5ffSJeff Cody goto exit; 843e8d4e5ffSJeff Cody } 844e8d4e5ffSJeff Cody s->block_size = s->params.block_size; 845e8d4e5ffSJeff Cody if (s->block_size & (s->block_size - 1)) { 846e8d4e5ffSJeff Cody ret = -EINVAL; 847e8d4e5ffSJeff Cody goto exit; 848e8d4e5ffSJeff Cody } 849e8d4e5ffSJeff Cody 8501e74a971SJeff Cody vhdx_set_shift_bits(s); 851e8d4e5ffSJeff Cody 852e8d4e5ffSJeff Cody ret = 0; 853e8d4e5ffSJeff Cody 854e8d4e5ffSJeff Cody exit: 855e8d4e5ffSJeff Cody qemu_vfree(buffer); 856e8d4e5ffSJeff Cody return ret; 857e8d4e5ffSJeff Cody } 858e8d4e5ffSJeff Cody 8591e74a971SJeff Cody /* 8601e74a971SJeff Cody * Calculate the number of BAT entries, including sector 8611e74a971SJeff Cody * bitmap entries. 8621e74a971SJeff Cody */ 8631e74a971SJeff Cody static void vhdx_calc_bat_entries(BDRVVHDXState *s) 8641e74a971SJeff Cody { 8651e74a971SJeff Cody uint32_t data_blocks_cnt, bitmap_blocks_cnt; 8661e74a971SJeff Cody 867939901dcSMax Reitz data_blocks_cnt = DIV_ROUND_UP(s->virtual_disk_size, s->block_size); 868939901dcSMax Reitz bitmap_blocks_cnt = DIV_ROUND_UP(data_blocks_cnt, s->chunk_ratio); 8691e74a971SJeff Cody 8701e74a971SJeff Cody if (s->parent_entries) { 8711e74a971SJeff Cody s->bat_entries = bitmap_blocks_cnt * (s->chunk_ratio + 1); 8721e74a971SJeff Cody } else { 8731e74a971SJeff Cody s->bat_entries = data_blocks_cnt + 8741e74a971SJeff Cody ((data_blocks_cnt - 1) >> s->chunk_ratio_bits); 8751e74a971SJeff Cody } 8761e74a971SJeff Cody 8771e74a971SJeff Cody } 878e8d4e5ffSJeff Cody 879c46415afSJeff Cody static void vhdx_close(BlockDriverState *bs) 880c46415afSJeff Cody { 881c46415afSJeff Cody BDRVVHDXState *s = bs->opaque; 882c46415afSJeff Cody qemu_vfree(s->headers[0]); 8830a43a1b5SJeff Cody s->headers[0] = NULL; 884c46415afSJeff Cody qemu_vfree(s->headers[1]); 8850a43a1b5SJeff Cody s->headers[1] = NULL; 886c46415afSJeff Cody qemu_vfree(s->bat); 8870a43a1b5SJeff Cody s->bat = NULL; 888c46415afSJeff Cody qemu_vfree(s->parent_entries); 8890a43a1b5SJeff Cody s->parent_entries = NULL; 890c46415afSJeff Cody migrate_del_blocker(s->migration_blocker); 891c46415afSJeff Cody error_free(s->migration_blocker); 8920a43a1b5SJeff Cody qemu_vfree(s->log.hdr); 8930a43a1b5SJeff Cody s->log.hdr = NULL; 8941a848fd4SJeff Cody vhdx_region_unregister_all(s); 895c46415afSJeff Cody } 896c46415afSJeff Cody 897015a1036SMax Reitz static int vhdx_open(BlockDriverState *bs, QDict *options, int flags, 898015a1036SMax Reitz Error **errp) 899e8d4e5ffSJeff Cody { 900e8d4e5ffSJeff Cody BDRVVHDXState *s = bs->opaque; 901e8d4e5ffSJeff Cody int ret = 0; 902e8d4e5ffSJeff Cody uint32_t i; 903e8d4e5ffSJeff Cody uint64_t signature; 9046890aad4SPaolo Bonzini Error *local_err = NULL; 905e8d4e5ffSJeff Cody 9064e4bf5c4SKevin Wolf bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file, 9074e4bf5c4SKevin Wolf false, errp); 9084e4bf5c4SKevin Wolf if (!bs->file) { 9094e4bf5c4SKevin Wolf return -EINVAL; 9104e4bf5c4SKevin Wolf } 9114e4bf5c4SKevin Wolf 912e8d4e5ffSJeff Cody s->bat = NULL; 913c3906c5eSJeff Cody s->first_visible_write = true; 914e8d4e5ffSJeff Cody 915e8d4e5ffSJeff Cody qemu_co_mutex_init(&s->lock); 9161a848fd4SJeff Cody QLIST_INIT(&s->regions); 917e8d4e5ffSJeff Cody 918e8d4e5ffSJeff Cody /* validate the file signature */ 919cf2ab8fcSKevin Wolf ret = bdrv_pread(bs->file, 0, &signature, sizeof(uint64_t)); 920e8d4e5ffSJeff Cody if (ret < 0) { 921e8d4e5ffSJeff Cody goto fail; 922e8d4e5ffSJeff Cody } 923e8d4e5ffSJeff Cody if (memcmp(&signature, "vhdxfile", 8)) { 924e8d4e5ffSJeff Cody ret = -EINVAL; 925e8d4e5ffSJeff Cody goto fail; 926e8d4e5ffSJeff Cody } 927e8d4e5ffSJeff Cody 9284f18b782SJeff Cody /* This is used for any header updates, for the file_write_guid. 9294f18b782SJeff Cody * The spec dictates that a new value should be used for the first 9304f18b782SJeff Cody * header update */ 9314f18b782SJeff Cody vhdx_guid_generate(&s->session_guid); 9324f18b782SJeff Cody 9336890aad4SPaolo Bonzini vhdx_parse_header(bs, s, &local_err); 9346890aad4SPaolo Bonzini if (local_err != NULL) { 9356890aad4SPaolo Bonzini error_propagate(errp, local_err); 9366890aad4SPaolo Bonzini ret = -EINVAL; 937e8d4e5ffSJeff Cody goto fail; 938e8d4e5ffSJeff Cody } 939e8d4e5ffSJeff Cody 9407e30e6a6SJeff Cody ret = vhdx_parse_log(bs, s, &s->log_replayed_on_open, errp); 9410a43a1b5SJeff Cody if (ret < 0) { 942e8d4e5ffSJeff Cody goto fail; 943e8d4e5ffSJeff Cody } 944e8d4e5ffSJeff Cody 945e8d4e5ffSJeff Cody ret = vhdx_open_region_tables(bs, s); 9460a43a1b5SJeff Cody if (ret < 0) { 947e8d4e5ffSJeff Cody goto fail; 948e8d4e5ffSJeff Cody } 949e8d4e5ffSJeff Cody 950e8d4e5ffSJeff Cody ret = vhdx_parse_metadata(bs, s); 9510a43a1b5SJeff Cody if (ret < 0) { 952e8d4e5ffSJeff Cody goto fail; 953e8d4e5ffSJeff Cody } 9540a43a1b5SJeff Cody 955e8d4e5ffSJeff Cody s->block_size = s->params.block_size; 956e8d4e5ffSJeff Cody 957e8d4e5ffSJeff Cody /* the VHDX spec dictates that virtual_disk_size is always a multiple of 958e8d4e5ffSJeff Cody * logical_sector_size */ 959e8d4e5ffSJeff Cody bs->total_sectors = s->virtual_disk_size >> s->logical_sector_size_bits; 960e8d4e5ffSJeff Cody 9611e74a971SJeff Cody vhdx_calc_bat_entries(s); 962e8d4e5ffSJeff Cody 963e8d4e5ffSJeff Cody s->bat_offset = s->bat_rt.file_offset; 964e8d4e5ffSJeff Cody 965e8d4e5ffSJeff Cody if (s->bat_entries > s->bat_rt.length / sizeof(VHDXBatEntry)) { 966e8d4e5ffSJeff Cody /* BAT allocation is not large enough for all entries */ 967e8d4e5ffSJeff Cody ret = -EINVAL; 968e8d4e5ffSJeff Cody goto fail; 969e8d4e5ffSJeff Cody } 970e8d4e5ffSJeff Cody 9716e9d290bSJeff Cody /* s->bat is freed in vhdx_close() */ 9729a4f4c31SKevin Wolf s->bat = qemu_try_blockalign(bs->file->bs, s->bat_rt.length); 973a67e128aSKevin Wolf if (s->bat == NULL) { 974a67e128aSKevin Wolf ret = -ENOMEM; 975a67e128aSKevin Wolf goto fail; 976a67e128aSKevin Wolf } 977a67e128aSKevin Wolf 978cf2ab8fcSKevin Wolf ret = bdrv_pread(bs->file, s->bat_offset, s->bat, s->bat_rt.length); 979e8d4e5ffSJeff Cody if (ret < 0) { 980e8d4e5ffSJeff Cody goto fail; 981e8d4e5ffSJeff Cody } 982e8d4e5ffSJeff Cody 9831a848fd4SJeff Cody uint64_t payblocks = s->chunk_ratio; 9841a848fd4SJeff Cody /* endian convert, and verify populated BAT field file offsets against 9851a848fd4SJeff Cody * region table and log entries */ 986e8d4e5ffSJeff Cody for (i = 0; i < s->bat_entries; i++) { 987e8d4e5ffSJeff Cody le64_to_cpus(&s->bat[i]); 9881a848fd4SJeff Cody if (payblocks--) { 9891a848fd4SJeff Cody /* payload bat entries */ 9901a848fd4SJeff Cody if ((s->bat[i] & VHDX_BAT_STATE_BIT_MASK) == 991d92aa883SJeff Cody PAYLOAD_BLOCK_FULLY_PRESENT) { 9921a848fd4SJeff Cody ret = vhdx_region_check(s, s->bat[i] & VHDX_BAT_FILE_OFF_MASK, 9931a848fd4SJeff Cody s->block_size); 9941a848fd4SJeff Cody if (ret < 0) { 9951a848fd4SJeff Cody goto fail; 9961a848fd4SJeff Cody } 9971a848fd4SJeff Cody } 9981a848fd4SJeff Cody } else { 9991a848fd4SJeff Cody payblocks = s->chunk_ratio; 10001a848fd4SJeff Cody /* Once differencing files are supported, verify sector bitmap 10011a848fd4SJeff Cody * blocks here */ 10021a848fd4SJeff Cody } 1003e8d4e5ffSJeff Cody } 1004e8d4e5ffSJeff Cody 1005fe44dc91SAshijeet Acharya /* Disable migration when VHDX images are used */ 1006fe44dc91SAshijeet Acharya error_setg(&s->migration_blocker, "The vhdx format used by node '%s' " 1007fe44dc91SAshijeet Acharya "does not support live migration", 1008fe44dc91SAshijeet Acharya bdrv_get_device_or_node_name(bs)); 1009fe44dc91SAshijeet Acharya ret = migrate_add_blocker(s->migration_blocker, &local_err); 1010fe44dc91SAshijeet Acharya if (local_err) { 1011fe44dc91SAshijeet Acharya error_propagate(errp, local_err); 1012fe44dc91SAshijeet Acharya error_free(s->migration_blocker); 1013fe44dc91SAshijeet Acharya goto fail; 1014fe44dc91SAshijeet Acharya } 1015fe44dc91SAshijeet Acharya 1016d92aa883SJeff Cody /* TODO: differencing files */ 1017e8d4e5ffSJeff Cody 1018e8d4e5ffSJeff Cody return 0; 1019e8d4e5ffSJeff Cody fail: 10200a43a1b5SJeff Cody vhdx_close(bs); 1021e8d4e5ffSJeff Cody return ret; 1022e8d4e5ffSJeff Cody } 1023e8d4e5ffSJeff Cody 1024e8d4e5ffSJeff Cody static int vhdx_reopen_prepare(BDRVReopenState *state, 1025e8d4e5ffSJeff Cody BlockReopenQueue *queue, Error **errp) 1026e8d4e5ffSJeff Cody { 1027e8d4e5ffSJeff Cody return 0; 1028e8d4e5ffSJeff Cody } 1029e8d4e5ffSJeff Cody 1030e8d4e5ffSJeff Cody 1031059e2fbbSJeff Cody /* 1032059e2fbbSJeff Cody * Perform sector to block offset translations, to get various 1033059e2fbbSJeff Cody * sector and file offsets into the image. See VHDXSectorInfo 1034059e2fbbSJeff Cody */ 1035059e2fbbSJeff Cody static void vhdx_block_translate(BDRVVHDXState *s, int64_t sector_num, 1036059e2fbbSJeff Cody int nb_sectors, VHDXSectorInfo *sinfo) 1037059e2fbbSJeff Cody { 1038059e2fbbSJeff Cody uint32_t block_offset; 1039059e2fbbSJeff Cody 1040059e2fbbSJeff Cody sinfo->bat_idx = sector_num >> s->sectors_per_block_bits; 1041059e2fbbSJeff Cody /* effectively a modulo - this gives us the offset into the block 1042059e2fbbSJeff Cody * (in sector sizes) for our sector number */ 1043059e2fbbSJeff Cody block_offset = sector_num - (sinfo->bat_idx << s->sectors_per_block_bits); 1044059e2fbbSJeff Cody /* the chunk ratio gives us the interleaving of the sector 1045059e2fbbSJeff Cody * bitmaps, so we need to advance our page block index by the 1046059e2fbbSJeff Cody * sector bitmaps entry number */ 1047059e2fbbSJeff Cody sinfo->bat_idx += sinfo->bat_idx >> s->chunk_ratio_bits; 1048059e2fbbSJeff Cody 1049059e2fbbSJeff Cody /* the number of sectors we can read/write in this cycle */ 1050059e2fbbSJeff Cody sinfo->sectors_avail = s->sectors_per_block - block_offset; 1051059e2fbbSJeff Cody 1052059e2fbbSJeff Cody sinfo->bytes_left = sinfo->sectors_avail << s->logical_sector_size_bits; 1053059e2fbbSJeff Cody 1054059e2fbbSJeff Cody if (sinfo->sectors_avail > nb_sectors) { 1055059e2fbbSJeff Cody sinfo->sectors_avail = nb_sectors; 1056059e2fbbSJeff Cody } 1057059e2fbbSJeff Cody 1058059e2fbbSJeff Cody sinfo->bytes_avail = sinfo->sectors_avail << s->logical_sector_size_bits; 1059059e2fbbSJeff Cody 10600b7da092SJeff Cody sinfo->file_offset = s->bat[sinfo->bat_idx] & VHDX_BAT_FILE_OFF_MASK; 1061059e2fbbSJeff Cody 1062059e2fbbSJeff Cody sinfo->block_offset = block_offset << s->logical_sector_size_bits; 1063059e2fbbSJeff Cody 1064059e2fbbSJeff Cody /* The file offset must be past the header section, so must be > 0 */ 1065059e2fbbSJeff Cody if (sinfo->file_offset == 0) { 1066059e2fbbSJeff Cody return; 1067059e2fbbSJeff Cody } 1068059e2fbbSJeff Cody 1069059e2fbbSJeff Cody /* block offset is the offset in vhdx logical sectors, in 1070059e2fbbSJeff Cody * the payload data block. Convert that to a byte offset 1071059e2fbbSJeff Cody * in the block, and add in the payload data block offset 1072059e2fbbSJeff Cody * in the file, in bytes, to get the final read address */ 1073059e2fbbSJeff Cody 1074059e2fbbSJeff Cody sinfo->file_offset += sinfo->block_offset; 1075059e2fbbSJeff Cody } 1076059e2fbbSJeff Cody 1077059e2fbbSJeff Cody 107897b00e28SPaolo Bonzini static int vhdx_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 107997b00e28SPaolo Bonzini { 108097b00e28SPaolo Bonzini BDRVVHDXState *s = bs->opaque; 108197b00e28SPaolo Bonzini 108297b00e28SPaolo Bonzini bdi->cluster_size = s->block_size; 108397b00e28SPaolo Bonzini 108495de6d70SPaolo Bonzini bdi->unallocated_blocks_are_zero = 108595de6d70SPaolo Bonzini (s->params.data_bits & VHDX_PARAMS_HAS_PARENT) == 0; 108695de6d70SPaolo Bonzini 108797b00e28SPaolo Bonzini return 0; 108897b00e28SPaolo Bonzini } 108997b00e28SPaolo Bonzini 1090059e2fbbSJeff Cody 1091e8d4e5ffSJeff Cody static coroutine_fn int vhdx_co_readv(BlockDriverState *bs, int64_t sector_num, 1092e8d4e5ffSJeff Cody int nb_sectors, QEMUIOVector *qiov) 1093e8d4e5ffSJeff Cody { 1094059e2fbbSJeff Cody BDRVVHDXState *s = bs->opaque; 1095059e2fbbSJeff Cody int ret = 0; 1096059e2fbbSJeff Cody VHDXSectorInfo sinfo; 1097059e2fbbSJeff Cody uint64_t bytes_done = 0; 1098059e2fbbSJeff Cody QEMUIOVector hd_qiov; 1099059e2fbbSJeff Cody 1100059e2fbbSJeff Cody qemu_iovec_init(&hd_qiov, qiov->niov); 1101059e2fbbSJeff Cody 1102059e2fbbSJeff Cody qemu_co_mutex_lock(&s->lock); 1103059e2fbbSJeff Cody 1104059e2fbbSJeff Cody while (nb_sectors > 0) { 1105059e2fbbSJeff Cody /* We are a differencing file, so we need to inspect the sector bitmap 1106059e2fbbSJeff Cody * to see if we have the data or not */ 1107059e2fbbSJeff Cody if (s->params.data_bits & VHDX_PARAMS_HAS_PARENT) { 1108059e2fbbSJeff Cody /* not supported yet */ 1109059e2fbbSJeff Cody ret = -ENOTSUP; 1110059e2fbbSJeff Cody goto exit; 1111059e2fbbSJeff Cody } else { 1112059e2fbbSJeff Cody vhdx_block_translate(s, sector_num, nb_sectors, &sinfo); 1113059e2fbbSJeff Cody 1114059e2fbbSJeff Cody qemu_iovec_reset(&hd_qiov); 1115059e2fbbSJeff Cody qemu_iovec_concat(&hd_qiov, qiov, bytes_done, sinfo.bytes_avail); 1116059e2fbbSJeff Cody 1117059e2fbbSJeff Cody /* check the payload block state */ 1118059e2fbbSJeff Cody switch (s->bat[sinfo.bat_idx] & VHDX_BAT_STATE_BIT_MASK) { 1119059e2fbbSJeff Cody case PAYLOAD_BLOCK_NOT_PRESENT: /* fall through */ 11200571df44SJeff Cody case PAYLOAD_BLOCK_UNDEFINED: 11210571df44SJeff Cody case PAYLOAD_BLOCK_UNMAPPED: 1122a9d1e9daSJeff Cody case PAYLOAD_BLOCK_UNMAPPED_v095: 1123059e2fbbSJeff Cody case PAYLOAD_BLOCK_ZERO: 1124059e2fbbSJeff Cody /* return zero */ 1125059e2fbbSJeff Cody qemu_iovec_memset(&hd_qiov, 0, 0, sinfo.bytes_avail); 1126059e2fbbSJeff Cody break; 1127d92aa883SJeff Cody case PAYLOAD_BLOCK_FULLY_PRESENT: 1128059e2fbbSJeff Cody qemu_co_mutex_unlock(&s->lock); 112928b04a8fSKevin Wolf ret = bdrv_co_readv(bs->file, 1130059e2fbbSJeff Cody sinfo.file_offset >> BDRV_SECTOR_BITS, 1131059e2fbbSJeff Cody sinfo.sectors_avail, &hd_qiov); 1132059e2fbbSJeff Cody qemu_co_mutex_lock(&s->lock); 1133059e2fbbSJeff Cody if (ret < 0) { 1134059e2fbbSJeff Cody goto exit; 1135059e2fbbSJeff Cody } 1136059e2fbbSJeff Cody break; 1137059e2fbbSJeff Cody case PAYLOAD_BLOCK_PARTIALLY_PRESENT: 1138059e2fbbSJeff Cody /* we don't yet support difference files, fall through 1139059e2fbbSJeff Cody * to error */ 1140059e2fbbSJeff Cody default: 1141059e2fbbSJeff Cody ret = -EIO; 1142059e2fbbSJeff Cody goto exit; 1143059e2fbbSJeff Cody break; 1144059e2fbbSJeff Cody } 1145059e2fbbSJeff Cody nb_sectors -= sinfo.sectors_avail; 1146059e2fbbSJeff Cody sector_num += sinfo.sectors_avail; 1147059e2fbbSJeff Cody bytes_done += sinfo.bytes_avail; 1148059e2fbbSJeff Cody } 1149059e2fbbSJeff Cody } 1150059e2fbbSJeff Cody ret = 0; 1151059e2fbbSJeff Cody exit: 1152059e2fbbSJeff Cody qemu_co_mutex_unlock(&s->lock); 1153059e2fbbSJeff Cody qemu_iovec_destroy(&hd_qiov); 1154059e2fbbSJeff Cody return ret; 1155e8d4e5ffSJeff Cody } 1156e8d4e5ffSJeff Cody 1157d92aa883SJeff Cody /* 1158d92aa883SJeff Cody * Allocate a new payload block at the end of the file. 1159d92aa883SJeff Cody * 1160d92aa883SJeff Cody * Allocation will happen at 1MB alignment inside the file 1161d92aa883SJeff Cody * 1162d92aa883SJeff Cody * Returns the file offset start of the new payload block 1163d92aa883SJeff Cody */ 1164d92aa883SJeff Cody static int vhdx_allocate_block(BlockDriverState *bs, BDRVVHDXState *s, 1165d92aa883SJeff Cody uint64_t *new_offset) 1166d92aa883SJeff Cody { 11673f910692SJeff Cody int64_t current_len; 11683f910692SJeff Cody 11693f910692SJeff Cody current_len = bdrv_getlength(bs->file->bs); 11703f910692SJeff Cody if (current_len < 0) { 11713f910692SJeff Cody return current_len; 11723f910692SJeff Cody } 11733f910692SJeff Cody 11743f910692SJeff Cody *new_offset = current_len; 1175e8d4e5ffSJeff Cody 1176d92aa883SJeff Cody /* per the spec, the address for a block is in units of 1MB */ 1177d92aa883SJeff Cody *new_offset = ROUND_UP(*new_offset, 1024 * 1024); 117827539ac5SJeff Cody if (*new_offset > INT64_MAX) { 117927539ac5SJeff Cody return -EINVAL; 118027539ac5SJeff Cody } 1181d92aa883SJeff Cody 11827ea37c30SMax Reitz return bdrv_truncate(bs->file, *new_offset + s->block_size, 11837ea37c30SMax Reitz PREALLOC_MODE_OFF, NULL); 1184d92aa883SJeff Cody } 1185d92aa883SJeff Cody 1186d92aa883SJeff Cody /* 1187d92aa883SJeff Cody * Update the BAT table entry with the new file offset, and the new entry 1188d92aa883SJeff Cody * state */ 1189d92aa883SJeff Cody static void vhdx_update_bat_table_entry(BlockDriverState *bs, BDRVVHDXState *s, 1190d92aa883SJeff Cody VHDXSectorInfo *sinfo, 1191d92aa883SJeff Cody uint64_t *bat_entry_le, 1192d92aa883SJeff Cody uint64_t *bat_offset, int state) 1193d92aa883SJeff Cody { 1194d92aa883SJeff Cody /* The BAT entry is a uint64, with 44 bits for the file offset in units of 1195d92aa883SJeff Cody * 1MB, and 3 bits for the block state. */ 1196cdf9634bSJeff Cody if ((state == PAYLOAD_BLOCK_ZERO) || 1197cdf9634bSJeff Cody (state == PAYLOAD_BLOCK_UNDEFINED) || 1198cdf9634bSJeff Cody (state == PAYLOAD_BLOCK_NOT_PRESENT) || 1199cdf9634bSJeff Cody (state == PAYLOAD_BLOCK_UNMAPPED)) { 1200cdf9634bSJeff Cody s->bat[sinfo->bat_idx] = 0; /* For PAYLOAD_BLOCK_ZERO, the 1201cdf9634bSJeff Cody FileOffsetMB field is denoted as 1202cdf9634bSJeff Cody 'reserved' in the v1.0 spec. If it is 1203cdf9634bSJeff Cody non-zero, MS Hyper-V will fail to read 1204cdf9634bSJeff Cody the disk image */ 1205cdf9634bSJeff Cody } else { 12060b7da092SJeff Cody s->bat[sinfo->bat_idx] = sinfo->file_offset; 1207cdf9634bSJeff Cody } 1208d92aa883SJeff Cody 1209d92aa883SJeff Cody s->bat[sinfo->bat_idx] |= state & VHDX_BAT_STATE_BIT_MASK; 1210d92aa883SJeff Cody 1211d92aa883SJeff Cody *bat_entry_le = cpu_to_le64(s->bat[sinfo->bat_idx]); 1212d92aa883SJeff Cody *bat_offset = s->bat_offset + sinfo->bat_idx * sizeof(VHDXBatEntry); 1213d92aa883SJeff Cody 1214d92aa883SJeff Cody } 1215e8d4e5ffSJeff Cody 1216c3906c5eSJeff Cody /* Per the spec, on the first write of guest-visible data to the file the 1217c3906c5eSJeff Cody * data write guid must be updated in the header */ 1218c3906c5eSJeff Cody int vhdx_user_visible_write(BlockDriverState *bs, BDRVVHDXState *s) 1219c3906c5eSJeff Cody { 1220c3906c5eSJeff Cody int ret = 0; 1221c3906c5eSJeff Cody if (s->first_visible_write) { 1222c3906c5eSJeff Cody s->first_visible_write = false; 1223c3906c5eSJeff Cody ret = vhdx_update_headers(bs, s, true, NULL); 1224c3906c5eSJeff Cody } 1225c3906c5eSJeff Cody return ret; 1226c3906c5eSJeff Cody } 1227c3906c5eSJeff Cody 1228e8d4e5ffSJeff Cody static coroutine_fn int vhdx_co_writev(BlockDriverState *bs, int64_t sector_num, 1229*e18a58b4SEric Blake int nb_sectors, QEMUIOVector *qiov, 1230*e18a58b4SEric Blake int flags) 1231e8d4e5ffSJeff Cody { 1232d92aa883SJeff Cody int ret = -ENOTSUP; 1233d92aa883SJeff Cody BDRVVHDXState *s = bs->opaque; 1234d92aa883SJeff Cody VHDXSectorInfo sinfo; 1235d92aa883SJeff Cody uint64_t bytes_done = 0; 1236d92aa883SJeff Cody uint64_t bat_entry = 0; 1237d92aa883SJeff Cody uint64_t bat_entry_offset = 0; 1238d92aa883SJeff Cody QEMUIOVector hd_qiov; 1239d92aa883SJeff Cody struct iovec iov1 = { 0 }; 1240d92aa883SJeff Cody struct iovec iov2 = { 0 }; 1241d92aa883SJeff Cody int sectors_to_write; 1242d92aa883SJeff Cody int bat_state; 1243d92aa883SJeff Cody uint64_t bat_prior_offset = 0; 1244d92aa883SJeff Cody bool bat_update = false; 1245d92aa883SJeff Cody 1246*e18a58b4SEric Blake assert(!flags); 1247d92aa883SJeff Cody qemu_iovec_init(&hd_qiov, qiov->niov); 1248d92aa883SJeff Cody 1249d92aa883SJeff Cody qemu_co_mutex_lock(&s->lock); 1250d92aa883SJeff Cody 1251d92aa883SJeff Cody ret = vhdx_user_visible_write(bs, s); 1252d92aa883SJeff Cody if (ret < 0) { 1253d92aa883SJeff Cody goto exit; 1254d92aa883SJeff Cody } 1255d92aa883SJeff Cody 1256d92aa883SJeff Cody while (nb_sectors > 0) { 1257d92aa883SJeff Cody bool use_zero_buffers = false; 1258d92aa883SJeff Cody bat_update = false; 1259d92aa883SJeff Cody if (s->params.data_bits & VHDX_PARAMS_HAS_PARENT) { 1260d92aa883SJeff Cody /* not supported yet */ 1261d92aa883SJeff Cody ret = -ENOTSUP; 1262d92aa883SJeff Cody goto exit; 1263d92aa883SJeff Cody } else { 1264d92aa883SJeff Cody vhdx_block_translate(s, sector_num, nb_sectors, &sinfo); 1265d92aa883SJeff Cody sectors_to_write = sinfo.sectors_avail; 1266d92aa883SJeff Cody 1267d92aa883SJeff Cody qemu_iovec_reset(&hd_qiov); 1268d92aa883SJeff Cody /* check the payload block state */ 1269d92aa883SJeff Cody bat_state = s->bat[sinfo.bat_idx] & VHDX_BAT_STATE_BIT_MASK; 1270d92aa883SJeff Cody switch (bat_state) { 1271d92aa883SJeff Cody case PAYLOAD_BLOCK_ZERO: 1272d92aa883SJeff Cody /* in this case, we need to preserve zero writes for 1273d92aa883SJeff Cody * data that is not part of this write, so we must pad 1274d92aa883SJeff Cody * the rest of the buffer to zeroes */ 1275d92aa883SJeff Cody 1276d92aa883SJeff Cody /* if we are on a posix system with ftruncate() that extends 1277d92aa883SJeff Cody * a file, then it is zero-filled for us. On Win32, the raw 1278d92aa883SJeff Cody * layer uses SetFilePointer and SetFileEnd, which does not 1279d92aa883SJeff Cody * zero fill AFAIK */ 1280d92aa883SJeff Cody 1281d92aa883SJeff Cody /* Queue another write of zero buffers if the underlying file 1282d92aa883SJeff Cody * does not zero-fill on file extension */ 1283d92aa883SJeff Cody 12849a4f4c31SKevin Wolf if (bdrv_has_zero_init(bs->file->bs) == 0) { 1285d92aa883SJeff Cody use_zero_buffers = true; 1286d92aa883SJeff Cody 1287d92aa883SJeff Cody /* zero fill the front, if any */ 1288d92aa883SJeff Cody if (sinfo.block_offset) { 1289d92aa883SJeff Cody iov1.iov_len = sinfo.block_offset; 1290d92aa883SJeff Cody iov1.iov_base = qemu_blockalign(bs, iov1.iov_len); 1291d92aa883SJeff Cody memset(iov1.iov_base, 0, iov1.iov_len); 1292d92aa883SJeff Cody qemu_iovec_concat_iov(&hd_qiov, &iov1, 1, 0, 1293d1a126c5SKevin Wolf iov1.iov_len); 1294d92aa883SJeff Cody sectors_to_write += iov1.iov_len >> BDRV_SECTOR_BITS; 1295d92aa883SJeff Cody } 1296d92aa883SJeff Cody 1297d92aa883SJeff Cody /* our actual data */ 1298d92aa883SJeff Cody qemu_iovec_concat(&hd_qiov, qiov, bytes_done, 1299d92aa883SJeff Cody sinfo.bytes_avail); 1300d92aa883SJeff Cody 1301d92aa883SJeff Cody /* zero fill the back, if any */ 1302d92aa883SJeff Cody if ((sinfo.bytes_avail - sinfo.block_offset) < 1303d92aa883SJeff Cody s->block_size) { 1304d92aa883SJeff Cody iov2.iov_len = s->block_size - 1305d92aa883SJeff Cody (sinfo.bytes_avail + sinfo.block_offset); 1306d92aa883SJeff Cody iov2.iov_base = qemu_blockalign(bs, iov2.iov_len); 1307d92aa883SJeff Cody memset(iov2.iov_base, 0, iov2.iov_len); 1308d92aa883SJeff Cody qemu_iovec_concat_iov(&hd_qiov, &iov2, 1, 0, 1309d1a126c5SKevin Wolf iov2.iov_len); 1310d92aa883SJeff Cody sectors_to_write += iov2.iov_len >> BDRV_SECTOR_BITS; 1311d92aa883SJeff Cody } 1312d92aa883SJeff Cody } 1313d92aa883SJeff Cody /* fall through */ 1314d92aa883SJeff Cody case PAYLOAD_BLOCK_NOT_PRESENT: /* fall through */ 13150571df44SJeff Cody case PAYLOAD_BLOCK_UNMAPPED: 1316a9d1e9daSJeff Cody case PAYLOAD_BLOCK_UNMAPPED_v095: 13170571df44SJeff Cody case PAYLOAD_BLOCK_UNDEFINED: 1318d92aa883SJeff Cody bat_prior_offset = sinfo.file_offset; 1319d92aa883SJeff Cody ret = vhdx_allocate_block(bs, s, &sinfo.file_offset); 1320d92aa883SJeff Cody if (ret < 0) { 1321d92aa883SJeff Cody goto exit; 1322d92aa883SJeff Cody } 1323d92aa883SJeff Cody /* once we support differencing files, this may also be 1324d92aa883SJeff Cody * partially present */ 1325d92aa883SJeff Cody /* update block state to the newly specified state */ 1326d92aa883SJeff Cody vhdx_update_bat_table_entry(bs, s, &sinfo, &bat_entry, 1327d92aa883SJeff Cody &bat_entry_offset, 1328d92aa883SJeff Cody PAYLOAD_BLOCK_FULLY_PRESENT); 1329d92aa883SJeff Cody bat_update = true; 1330d92aa883SJeff Cody /* since we just allocated a block, file_offset is the 1331d92aa883SJeff Cody * beginning of the payload block. It needs to be the 1332d92aa883SJeff Cody * write address, which includes the offset into the block */ 1333d92aa883SJeff Cody if (!use_zero_buffers) { 1334d92aa883SJeff Cody sinfo.file_offset += sinfo.block_offset; 1335d92aa883SJeff Cody } 1336d92aa883SJeff Cody /* fall through */ 1337d92aa883SJeff Cody case PAYLOAD_BLOCK_FULLY_PRESENT: 1338d92aa883SJeff Cody /* if the file offset address is in the header zone, 1339d92aa883SJeff Cody * there is a problem */ 1340d92aa883SJeff Cody if (sinfo.file_offset < (1024 * 1024)) { 1341d92aa883SJeff Cody ret = -EFAULT; 1342d92aa883SJeff Cody goto error_bat_restore; 1343d92aa883SJeff Cody } 1344d92aa883SJeff Cody 1345d92aa883SJeff Cody if (!use_zero_buffers) { 1346d92aa883SJeff Cody qemu_iovec_concat(&hd_qiov, qiov, bytes_done, 1347d92aa883SJeff Cody sinfo.bytes_avail); 1348d92aa883SJeff Cody } 1349d92aa883SJeff Cody /* block exists, so we can just overwrite it */ 1350d92aa883SJeff Cody qemu_co_mutex_unlock(&s->lock); 135125ec177dSKevin Wolf ret = bdrv_co_writev(bs->file, 1352d92aa883SJeff Cody sinfo.file_offset >> BDRV_SECTOR_BITS, 1353d92aa883SJeff Cody sectors_to_write, &hd_qiov); 1354d92aa883SJeff Cody qemu_co_mutex_lock(&s->lock); 1355d92aa883SJeff Cody if (ret < 0) { 1356d92aa883SJeff Cody goto error_bat_restore; 1357d92aa883SJeff Cody } 1358d92aa883SJeff Cody break; 1359d92aa883SJeff Cody case PAYLOAD_BLOCK_PARTIALLY_PRESENT: 1360d92aa883SJeff Cody /* we don't yet support difference files, fall through 1361d92aa883SJeff Cody * to error */ 1362d92aa883SJeff Cody default: 1363d92aa883SJeff Cody ret = -EIO; 1364d92aa883SJeff Cody goto exit; 1365d92aa883SJeff Cody break; 1366d92aa883SJeff Cody } 1367d92aa883SJeff Cody 1368d92aa883SJeff Cody if (bat_update) { 1369d92aa883SJeff Cody /* this will update the BAT entry into the log journal, and 1370d92aa883SJeff Cody * then flush the log journal out to disk */ 1371d92aa883SJeff Cody ret = vhdx_log_write_and_flush(bs, s, &bat_entry, 1372d92aa883SJeff Cody sizeof(VHDXBatEntry), 1373d92aa883SJeff Cody bat_entry_offset); 1374d92aa883SJeff Cody if (ret < 0) { 1375d92aa883SJeff Cody goto exit; 1376d92aa883SJeff Cody } 1377d92aa883SJeff Cody } 1378d92aa883SJeff Cody 1379d92aa883SJeff Cody nb_sectors -= sinfo.sectors_avail; 1380d92aa883SJeff Cody sector_num += sinfo.sectors_avail; 1381d92aa883SJeff Cody bytes_done += sinfo.bytes_avail; 1382d92aa883SJeff Cody 1383d92aa883SJeff Cody } 1384d92aa883SJeff Cody } 1385d92aa883SJeff Cody 1386d92aa883SJeff Cody goto exit; 1387d92aa883SJeff Cody 1388d92aa883SJeff Cody error_bat_restore: 1389d92aa883SJeff Cody if (bat_update) { 1390d92aa883SJeff Cody /* keep metadata in sync, and restore the bat entry state 1391d92aa883SJeff Cody * if error. */ 1392d92aa883SJeff Cody sinfo.file_offset = bat_prior_offset; 1393d92aa883SJeff Cody vhdx_update_bat_table_entry(bs, s, &sinfo, &bat_entry, 1394d92aa883SJeff Cody &bat_entry_offset, bat_state); 1395d92aa883SJeff Cody } 1396d92aa883SJeff Cody exit: 1397d92aa883SJeff Cody qemu_vfree(iov1.iov_base); 1398d92aa883SJeff Cody qemu_vfree(iov2.iov_base); 1399d92aa883SJeff Cody qemu_co_mutex_unlock(&s->lock); 1400d92aa883SJeff Cody qemu_iovec_destroy(&hd_qiov); 1401d92aa883SJeff Cody return ret; 1402e8d4e5ffSJeff Cody } 1403e8d4e5ffSJeff Cody 1404e8d4e5ffSJeff Cody 14053412f7b1SJeff Cody 14063412f7b1SJeff Cody /* 14073412f7b1SJeff Cody * Create VHDX Headers 14083412f7b1SJeff Cody * 14093412f7b1SJeff Cody * There are 2 headers, and the highest sequence number will represent 14103412f7b1SJeff Cody * the active header 14113412f7b1SJeff Cody */ 1412db1e80eeSKevin Wolf static int vhdx_create_new_headers(BlockBackend *blk, uint64_t image_size, 14133412f7b1SJeff Cody uint32_t log_size) 14143412f7b1SJeff Cody { 1415db1e80eeSKevin Wolf BlockDriverState *bs = blk_bs(blk); 1416cf2ab8fcSKevin Wolf BdrvChild *child; 14173412f7b1SJeff Cody int ret = 0; 14183412f7b1SJeff Cody VHDXHeader *hdr = NULL; 14193412f7b1SJeff Cody 14205839e53bSMarkus Armbruster hdr = g_new0(VHDXHeader, 1); 14213412f7b1SJeff Cody 14223412f7b1SJeff Cody hdr->signature = VHDX_HEADER_SIGNATURE; 14233412f7b1SJeff Cody hdr->sequence_number = g_random_int(); 14243412f7b1SJeff Cody hdr->log_version = 0; 14253412f7b1SJeff Cody hdr->version = 1; 14263412f7b1SJeff Cody hdr->log_length = log_size; 14273412f7b1SJeff Cody hdr->log_offset = VHDX_HEADER_SECTION_END; 14283412f7b1SJeff Cody vhdx_guid_generate(&hdr->file_write_guid); 14293412f7b1SJeff Cody vhdx_guid_generate(&hdr->data_write_guid); 14303412f7b1SJeff Cody 1431cf2ab8fcSKevin Wolf /* XXX Ugly way to get blk->root, but that's a feature, not a bug. This 1432cf2ab8fcSKevin Wolf * hack makes it obvious that vhdx_write_header() bypasses the BlockBackend 1433cf2ab8fcSKevin Wolf * here, which it really shouldn't be doing. */ 1434cf2ab8fcSKevin Wolf child = QLIST_FIRST(&bs->parents); 1435cf2ab8fcSKevin Wolf assert(!QLIST_NEXT(child, next_parent)); 1436cf2ab8fcSKevin Wolf 1437cf2ab8fcSKevin Wolf ret = vhdx_write_header(child, hdr, VHDX_HEADER1_OFFSET, false); 14383412f7b1SJeff Cody if (ret < 0) { 14393412f7b1SJeff Cody goto exit; 14403412f7b1SJeff Cody } 14413412f7b1SJeff Cody hdr->sequence_number++; 1442cf2ab8fcSKevin Wolf ret = vhdx_write_header(child, hdr, VHDX_HEADER2_OFFSET, false); 14433412f7b1SJeff Cody if (ret < 0) { 14443412f7b1SJeff Cody goto exit; 14453412f7b1SJeff Cody } 14463412f7b1SJeff Cody 14473412f7b1SJeff Cody exit: 14483412f7b1SJeff Cody g_free(hdr); 14493412f7b1SJeff Cody return ret; 14503412f7b1SJeff Cody } 14513412f7b1SJeff Cody 1452e91a8b2fSJeff Cody #define VHDX_METADATA_ENTRY_BUFFER_SIZE \ 1453e91a8b2fSJeff Cody (sizeof(VHDXFileParameters) +\ 1454e91a8b2fSJeff Cody sizeof(VHDXVirtualDiskSize) +\ 1455e91a8b2fSJeff Cody sizeof(VHDXPage83Data) +\ 1456e91a8b2fSJeff Cody sizeof(VHDXVirtualDiskLogicalSectorSize) +\ 1457e91a8b2fSJeff Cody sizeof(VHDXVirtualDiskPhysicalSectorSize)) 14583412f7b1SJeff Cody 14593412f7b1SJeff Cody /* 14603412f7b1SJeff Cody * Create the Metadata entries. 14613412f7b1SJeff Cody * 14623412f7b1SJeff Cody * For more details on the entries, see section 3.5 (pg 29) in the 14633412f7b1SJeff Cody * VHDX 1.00 specification. 14643412f7b1SJeff Cody * 14653412f7b1SJeff Cody * We support 5 metadata entries (all required by spec): 14663412f7b1SJeff Cody * File Parameters, 14673412f7b1SJeff Cody * Virtual Disk Size, 14683412f7b1SJeff Cody * Page 83 Data, 14693412f7b1SJeff Cody * Logical Sector Size, 14703412f7b1SJeff Cody * Physical Sector Size 14713412f7b1SJeff Cody * 14723412f7b1SJeff Cody * The first 64KB of the Metadata section is reserved for the metadata 14733412f7b1SJeff Cody * header and entries; beyond that, the metadata items themselves reside. 14743412f7b1SJeff Cody */ 1475db1e80eeSKevin Wolf static int vhdx_create_new_metadata(BlockBackend *blk, 14763412f7b1SJeff Cody uint64_t image_size, 14773412f7b1SJeff Cody uint32_t block_size, 14783412f7b1SJeff Cody uint32_t sector_size, 14793412f7b1SJeff Cody uint64_t metadata_offset, 14803412f7b1SJeff Cody VHDXImageType type) 14813412f7b1SJeff Cody { 14823412f7b1SJeff Cody int ret = 0; 14833412f7b1SJeff Cody uint32_t offset = 0; 14843412f7b1SJeff Cody void *buffer = NULL; 14853412f7b1SJeff Cody void *entry_buffer; 1486a8f15a27SDaniel P. Berrange VHDXMetadataTableHeader *md_table; 14873412f7b1SJeff Cody VHDXMetadataTableEntry *md_table_entry; 14883412f7b1SJeff Cody 14893412f7b1SJeff Cody /* Metadata entries */ 14903412f7b1SJeff Cody VHDXFileParameters *mt_file_params; 14913412f7b1SJeff Cody VHDXVirtualDiskSize *mt_virtual_size; 14923412f7b1SJeff Cody VHDXPage83Data *mt_page83; 14933412f7b1SJeff Cody VHDXVirtualDiskLogicalSectorSize *mt_log_sector_size; 14943412f7b1SJeff Cody VHDXVirtualDiskPhysicalSectorSize *mt_phys_sector_size; 14953412f7b1SJeff Cody 1496e91a8b2fSJeff Cody entry_buffer = g_malloc0(VHDX_METADATA_ENTRY_BUFFER_SIZE); 14973412f7b1SJeff Cody 14983412f7b1SJeff Cody mt_file_params = entry_buffer; 14993412f7b1SJeff Cody offset += sizeof(VHDXFileParameters); 15003412f7b1SJeff Cody mt_virtual_size = entry_buffer + offset; 15013412f7b1SJeff Cody offset += sizeof(VHDXVirtualDiskSize); 15023412f7b1SJeff Cody mt_page83 = entry_buffer + offset; 15033412f7b1SJeff Cody offset += sizeof(VHDXPage83Data); 15043412f7b1SJeff Cody mt_log_sector_size = entry_buffer + offset; 15053412f7b1SJeff Cody offset += sizeof(VHDXVirtualDiskLogicalSectorSize); 15063412f7b1SJeff Cody mt_phys_sector_size = entry_buffer + offset; 15073412f7b1SJeff Cody 15083412f7b1SJeff Cody mt_file_params->block_size = cpu_to_le32(block_size); 15093412f7b1SJeff Cody if (type == VHDX_TYPE_FIXED) { 15103412f7b1SJeff Cody mt_file_params->data_bits |= VHDX_PARAMS_LEAVE_BLOCKS_ALLOCED; 15113412f7b1SJeff Cody cpu_to_le32s(&mt_file_params->data_bits); 15123412f7b1SJeff Cody } 15133412f7b1SJeff Cody 15143412f7b1SJeff Cody vhdx_guid_generate(&mt_page83->page_83_data); 15153412f7b1SJeff Cody cpu_to_leguids(&mt_page83->page_83_data); 15163412f7b1SJeff Cody mt_virtual_size->virtual_disk_size = cpu_to_le64(image_size); 15173412f7b1SJeff Cody mt_log_sector_size->logical_sector_size = cpu_to_le32(sector_size); 15183412f7b1SJeff Cody mt_phys_sector_size->physical_sector_size = cpu_to_le32(sector_size); 15193412f7b1SJeff Cody 15203412f7b1SJeff Cody buffer = g_malloc0(VHDX_HEADER_BLOCK_SIZE); 15213412f7b1SJeff Cody md_table = buffer; 15223412f7b1SJeff Cody 15233412f7b1SJeff Cody md_table->signature = VHDX_METADATA_SIGNATURE; 15243412f7b1SJeff Cody md_table->entry_count = 5; 15253412f7b1SJeff Cody vhdx_metadata_header_le_export(md_table); 15263412f7b1SJeff Cody 15273412f7b1SJeff Cody 15283412f7b1SJeff Cody /* This will reference beyond the reserved table portion */ 15293412f7b1SJeff Cody offset = 64 * KiB; 15303412f7b1SJeff Cody 15313412f7b1SJeff Cody md_table_entry = buffer + sizeof(VHDXMetadataTableHeader); 15323412f7b1SJeff Cody 15333412f7b1SJeff Cody md_table_entry[0].item_id = file_param_guid; 15343412f7b1SJeff Cody md_table_entry[0].offset = offset; 15353412f7b1SJeff Cody md_table_entry[0].length = sizeof(VHDXFileParameters); 15363412f7b1SJeff Cody md_table_entry[0].data_bits |= VHDX_META_FLAGS_IS_REQUIRED; 15373412f7b1SJeff Cody offset += md_table_entry[0].length; 15383412f7b1SJeff Cody vhdx_metadata_entry_le_export(&md_table_entry[0]); 15393412f7b1SJeff Cody 15403412f7b1SJeff Cody md_table_entry[1].item_id = virtual_size_guid; 15413412f7b1SJeff Cody md_table_entry[1].offset = offset; 15423412f7b1SJeff Cody md_table_entry[1].length = sizeof(VHDXVirtualDiskSize); 15433412f7b1SJeff Cody md_table_entry[1].data_bits |= VHDX_META_FLAGS_IS_REQUIRED | 15443412f7b1SJeff Cody VHDX_META_FLAGS_IS_VIRTUAL_DISK; 15453412f7b1SJeff Cody offset += md_table_entry[1].length; 15463412f7b1SJeff Cody vhdx_metadata_entry_le_export(&md_table_entry[1]); 15473412f7b1SJeff Cody 15483412f7b1SJeff Cody md_table_entry[2].item_id = page83_guid; 15493412f7b1SJeff Cody md_table_entry[2].offset = offset; 15503412f7b1SJeff Cody md_table_entry[2].length = sizeof(VHDXPage83Data); 15513412f7b1SJeff Cody md_table_entry[2].data_bits |= VHDX_META_FLAGS_IS_REQUIRED | 15523412f7b1SJeff Cody VHDX_META_FLAGS_IS_VIRTUAL_DISK; 15533412f7b1SJeff Cody offset += md_table_entry[2].length; 15543412f7b1SJeff Cody vhdx_metadata_entry_le_export(&md_table_entry[2]); 15553412f7b1SJeff Cody 15563412f7b1SJeff Cody md_table_entry[3].item_id = logical_sector_guid; 15573412f7b1SJeff Cody md_table_entry[3].offset = offset; 15583412f7b1SJeff Cody md_table_entry[3].length = sizeof(VHDXVirtualDiskLogicalSectorSize); 15593412f7b1SJeff Cody md_table_entry[3].data_bits |= VHDX_META_FLAGS_IS_REQUIRED | 15603412f7b1SJeff Cody VHDX_META_FLAGS_IS_VIRTUAL_DISK; 15613412f7b1SJeff Cody offset += md_table_entry[3].length; 15623412f7b1SJeff Cody vhdx_metadata_entry_le_export(&md_table_entry[3]); 15633412f7b1SJeff Cody 15643412f7b1SJeff Cody md_table_entry[4].item_id = phys_sector_guid; 15653412f7b1SJeff Cody md_table_entry[4].offset = offset; 15663412f7b1SJeff Cody md_table_entry[4].length = sizeof(VHDXVirtualDiskPhysicalSectorSize); 15673412f7b1SJeff Cody md_table_entry[4].data_bits |= VHDX_META_FLAGS_IS_REQUIRED | 15683412f7b1SJeff Cody VHDX_META_FLAGS_IS_VIRTUAL_DISK; 15693412f7b1SJeff Cody vhdx_metadata_entry_le_export(&md_table_entry[4]); 15703412f7b1SJeff Cody 1571db1e80eeSKevin Wolf ret = blk_pwrite(blk, metadata_offset, buffer, VHDX_HEADER_BLOCK_SIZE, 0); 15723412f7b1SJeff Cody if (ret < 0) { 15733412f7b1SJeff Cody goto exit; 15743412f7b1SJeff Cody } 15753412f7b1SJeff Cody 1576db1e80eeSKevin Wolf ret = blk_pwrite(blk, metadata_offset + (64 * KiB), entry_buffer, 1577db1e80eeSKevin Wolf VHDX_METADATA_ENTRY_BUFFER_SIZE, 0); 15783412f7b1SJeff Cody if (ret < 0) { 15793412f7b1SJeff Cody goto exit; 15803412f7b1SJeff Cody } 15813412f7b1SJeff Cody 15823412f7b1SJeff Cody 15833412f7b1SJeff Cody exit: 15843412f7b1SJeff Cody g_free(buffer); 15853412f7b1SJeff Cody g_free(entry_buffer); 15863412f7b1SJeff Cody return ret; 15873412f7b1SJeff Cody } 15883412f7b1SJeff Cody 15893412f7b1SJeff Cody /* This create the actual BAT itself. We currently only support 15903412f7b1SJeff Cody * 'Dynamic' and 'Fixed' image types. 15913412f7b1SJeff Cody * 15923412f7b1SJeff Cody * Dynamic images: default state of the BAT is all zeroes. 15933412f7b1SJeff Cody * 15943412f7b1SJeff Cody * Fixed images: default state of the BAT is fully populated, with 15953412f7b1SJeff Cody * file offsets and state PAYLOAD_BLOCK_FULLY_PRESENT. 15963412f7b1SJeff Cody */ 1597db1e80eeSKevin Wolf static int vhdx_create_bat(BlockBackend *blk, BDRVVHDXState *s, 15983412f7b1SJeff Cody uint64_t image_size, VHDXImageType type, 15994f75b52aSJeff Cody bool use_zero_blocks, uint64_t file_offset, 160055b9392bSMax Reitz uint32_t length, Error **errp) 16013412f7b1SJeff Cody { 16023412f7b1SJeff Cody int ret = 0; 16033412f7b1SJeff Cody uint64_t data_file_offset; 16043412f7b1SJeff Cody uint64_t total_sectors = 0; 16053412f7b1SJeff Cody uint64_t sector_num = 0; 16063412f7b1SJeff Cody uint64_t unused; 16073412f7b1SJeff Cody int block_state; 16083412f7b1SJeff Cody VHDXSectorInfo sinfo; 16093412f7b1SJeff Cody 16103412f7b1SJeff Cody assert(s->bat == NULL); 16113412f7b1SJeff Cody 16123412f7b1SJeff Cody /* this gives a data start after BAT/bitmap entries, and well 16133412f7b1SJeff Cody * past any metadata entries (with a 4 MB buffer for future 16143412f7b1SJeff Cody * expansion */ 16154f75b52aSJeff Cody data_file_offset = file_offset + length + 5 * MiB; 16163412f7b1SJeff Cody total_sectors = image_size >> s->logical_sector_size_bits; 16173412f7b1SJeff Cody 16183412f7b1SJeff Cody if (type == VHDX_TYPE_DYNAMIC) { 16193412f7b1SJeff Cody /* All zeroes, so we can just extend the file - the end of the BAT 16203412f7b1SJeff Cody * is the furthest thing we have written yet */ 16213a691c50SMax Reitz ret = blk_truncate(blk, data_file_offset, PREALLOC_MODE_OFF, errp); 16223412f7b1SJeff Cody if (ret < 0) { 16233412f7b1SJeff Cody goto exit; 16243412f7b1SJeff Cody } 16253412f7b1SJeff Cody } else if (type == VHDX_TYPE_FIXED) { 16263a691c50SMax Reitz ret = blk_truncate(blk, data_file_offset + image_size, 16273a691c50SMax Reitz PREALLOC_MODE_OFF, errp); 16283412f7b1SJeff Cody if (ret < 0) { 16293412f7b1SJeff Cody goto exit; 16303412f7b1SJeff Cody } 16313412f7b1SJeff Cody } else { 163255b9392bSMax Reitz error_setg(errp, "Unsupported image type"); 16333412f7b1SJeff Cody ret = -ENOTSUP; 16343412f7b1SJeff Cody goto exit; 16353412f7b1SJeff Cody } 16363412f7b1SJeff Cody 16373412f7b1SJeff Cody if (type == VHDX_TYPE_FIXED || 16383412f7b1SJeff Cody use_zero_blocks || 1639db1e80eeSKevin Wolf bdrv_has_zero_init(blk_bs(blk)) == 0) { 16403412f7b1SJeff Cody /* for a fixed file, the default BAT entry is not zero */ 1641a67e128aSKevin Wolf s->bat = g_try_malloc0(length); 1642a011898dSAdelina Tuvenie if (length && s->bat == NULL) { 164355b9392bSMax Reitz error_setg(errp, "Failed to allocate memory for the BAT"); 1644a67e128aSKevin Wolf ret = -ENOMEM; 1645a67e128aSKevin Wolf goto exit; 1646a67e128aSKevin Wolf } 16473412f7b1SJeff Cody block_state = type == VHDX_TYPE_FIXED ? PAYLOAD_BLOCK_FULLY_PRESENT : 16483412f7b1SJeff Cody PAYLOAD_BLOCK_NOT_PRESENT; 16493412f7b1SJeff Cody block_state = use_zero_blocks ? PAYLOAD_BLOCK_ZERO : block_state; 16503412f7b1SJeff Cody /* fill the BAT by emulating sector writes of sectors_per_block size */ 16513412f7b1SJeff Cody while (sector_num < total_sectors) { 16523412f7b1SJeff Cody vhdx_block_translate(s, sector_num, s->sectors_per_block, &sinfo); 16533412f7b1SJeff Cody sinfo.file_offset = data_file_offset + 16543412f7b1SJeff Cody (sector_num << s->logical_sector_size_bits); 16553412f7b1SJeff Cody sinfo.file_offset = ROUND_UP(sinfo.file_offset, MiB); 1656db1e80eeSKevin Wolf vhdx_update_bat_table_entry(blk_bs(blk), s, &sinfo, &unused, &unused, 16573412f7b1SJeff Cody block_state); 16583412f7b1SJeff Cody cpu_to_le64s(&s->bat[sinfo.bat_idx]); 16593412f7b1SJeff Cody sector_num += s->sectors_per_block; 16603412f7b1SJeff Cody } 1661db1e80eeSKevin Wolf ret = blk_pwrite(blk, file_offset, s->bat, length, 0); 16623412f7b1SJeff Cody if (ret < 0) { 166355b9392bSMax Reitz error_setg_errno(errp, -ret, "Failed to write the BAT"); 16643412f7b1SJeff Cody goto exit; 16653412f7b1SJeff Cody } 16663412f7b1SJeff Cody } 16673412f7b1SJeff Cody 16683412f7b1SJeff Cody 16693412f7b1SJeff Cody 16703412f7b1SJeff Cody exit: 16713412f7b1SJeff Cody g_free(s->bat); 16723412f7b1SJeff Cody return ret; 16733412f7b1SJeff Cody } 16743412f7b1SJeff Cody 16753412f7b1SJeff Cody /* Creates the region table header, and region table entries. 16763412f7b1SJeff Cody * There are 2 supported region table entries: BAT, and Metadata/ 16773412f7b1SJeff Cody * 16783412f7b1SJeff Cody * As the calculations for the BAT region table are also needed 16793412f7b1SJeff Cody * to create the BAT itself, we will also cause the BAT to be 16803412f7b1SJeff Cody * created. 16813412f7b1SJeff Cody */ 1682db1e80eeSKevin Wolf static int vhdx_create_new_region_table(BlockBackend *blk, 16833412f7b1SJeff Cody uint64_t image_size, 16843412f7b1SJeff Cody uint32_t block_size, 16853412f7b1SJeff Cody uint32_t sector_size, 16863412f7b1SJeff Cody uint32_t log_size, 16873412f7b1SJeff Cody bool use_zero_blocks, 16883412f7b1SJeff Cody VHDXImageType type, 168955b9392bSMax Reitz uint64_t *metadata_offset, 169055b9392bSMax Reitz Error **errp) 16913412f7b1SJeff Cody { 16923412f7b1SJeff Cody int ret = 0; 16933412f7b1SJeff Cody uint32_t offset = 0; 16943412f7b1SJeff Cody void *buffer = NULL; 16954f75b52aSJeff Cody uint64_t bat_file_offset; 16964f75b52aSJeff Cody uint32_t bat_length; 16973412f7b1SJeff Cody BDRVVHDXState *s = NULL; 16983412f7b1SJeff Cody VHDXRegionTableHeader *region_table; 16993412f7b1SJeff Cody VHDXRegionTableEntry *rt_bat; 17003412f7b1SJeff Cody VHDXRegionTableEntry *rt_metadata; 17013412f7b1SJeff Cody 17023412f7b1SJeff Cody assert(metadata_offset != NULL); 17033412f7b1SJeff Cody 17043412f7b1SJeff Cody /* Populate enough of the BDRVVHDXState to be able to use the 17053412f7b1SJeff Cody * pre-existing BAT calculation, translation, and update functions */ 17065839e53bSMarkus Armbruster s = g_new0(BDRVVHDXState, 1); 17073412f7b1SJeff Cody 17083412f7b1SJeff Cody s->chunk_ratio = (VHDX_MAX_SECTORS_PER_BLOCK) * 17093412f7b1SJeff Cody (uint64_t) sector_size / (uint64_t) block_size; 17103412f7b1SJeff Cody 17113412f7b1SJeff Cody s->sectors_per_block = block_size / sector_size; 17123412f7b1SJeff Cody s->virtual_disk_size = image_size; 17133412f7b1SJeff Cody s->block_size = block_size; 17143412f7b1SJeff Cody s->logical_sector_size = sector_size; 17153412f7b1SJeff Cody 17163412f7b1SJeff Cody vhdx_set_shift_bits(s); 17173412f7b1SJeff Cody 17183412f7b1SJeff Cody vhdx_calc_bat_entries(s); 17193412f7b1SJeff Cody 17203412f7b1SJeff Cody /* At this point the VHDX state is populated enough for creation */ 17213412f7b1SJeff Cody 17223412f7b1SJeff Cody /* a single buffer is used so we can calculate the checksum over the 17233412f7b1SJeff Cody * entire 64KB block */ 17243412f7b1SJeff Cody buffer = g_malloc0(VHDX_HEADER_BLOCK_SIZE); 17253412f7b1SJeff Cody region_table = buffer; 17263412f7b1SJeff Cody offset += sizeof(VHDXRegionTableHeader); 17273412f7b1SJeff Cody rt_bat = buffer + offset; 17283412f7b1SJeff Cody offset += sizeof(VHDXRegionTableEntry); 17293412f7b1SJeff Cody rt_metadata = buffer + offset; 17303412f7b1SJeff Cody 17313412f7b1SJeff Cody region_table->signature = VHDX_REGION_SIGNATURE; 17323412f7b1SJeff Cody region_table->entry_count = 2; /* BAT and Metadata */ 17333412f7b1SJeff Cody 17343412f7b1SJeff Cody rt_bat->guid = bat_guid; 17353412f7b1SJeff Cody rt_bat->length = ROUND_UP(s->bat_entries * sizeof(VHDXBatEntry), MiB); 17363412f7b1SJeff Cody rt_bat->file_offset = ROUND_UP(VHDX_HEADER_SECTION_END + log_size, MiB); 17373412f7b1SJeff Cody s->bat_offset = rt_bat->file_offset; 17383412f7b1SJeff Cody 17393412f7b1SJeff Cody rt_metadata->guid = metadata_guid; 17403412f7b1SJeff Cody rt_metadata->file_offset = ROUND_UP(rt_bat->file_offset + rt_bat->length, 17413412f7b1SJeff Cody MiB); 17423412f7b1SJeff Cody rt_metadata->length = 1 * MiB; /* min size, and more than enough */ 17433412f7b1SJeff Cody *metadata_offset = rt_metadata->file_offset; 17443412f7b1SJeff Cody 17454f75b52aSJeff Cody bat_file_offset = rt_bat->file_offset; 17464f75b52aSJeff Cody bat_length = rt_bat->length; 17474f75b52aSJeff Cody 17484f75b52aSJeff Cody vhdx_region_header_le_export(region_table); 17494f75b52aSJeff Cody vhdx_region_entry_le_export(rt_bat); 17504f75b52aSJeff Cody vhdx_region_entry_le_export(rt_metadata); 17514f75b52aSJeff Cody 17523412f7b1SJeff Cody vhdx_update_checksum(buffer, VHDX_HEADER_BLOCK_SIZE, 17533412f7b1SJeff Cody offsetof(VHDXRegionTableHeader, checksum)); 17543412f7b1SJeff Cody 17553412f7b1SJeff Cody 17563412f7b1SJeff Cody /* The region table gives us the data we need to create the BAT, 17573412f7b1SJeff Cody * so do that now */ 1758db1e80eeSKevin Wolf ret = vhdx_create_bat(blk, s, image_size, type, use_zero_blocks, 175955b9392bSMax Reitz bat_file_offset, bat_length, errp); 17604f75b52aSJeff Cody if (ret < 0) { 17614f75b52aSJeff Cody goto exit; 17624f75b52aSJeff Cody } 17633412f7b1SJeff Cody 17643412f7b1SJeff Cody /* Now write out the region headers to disk */ 1765db1e80eeSKevin Wolf ret = blk_pwrite(blk, VHDX_REGION_TABLE_OFFSET, buffer, 1766db1e80eeSKevin Wolf VHDX_HEADER_BLOCK_SIZE, 0); 17673412f7b1SJeff Cody if (ret < 0) { 176855b9392bSMax Reitz error_setg_errno(errp, -ret, "Failed to write first region table"); 17693412f7b1SJeff Cody goto exit; 17703412f7b1SJeff Cody } 17713412f7b1SJeff Cody 1772db1e80eeSKevin Wolf ret = blk_pwrite(blk, VHDX_REGION_TABLE2_OFFSET, buffer, 1773db1e80eeSKevin Wolf VHDX_HEADER_BLOCK_SIZE, 0); 17743412f7b1SJeff Cody if (ret < 0) { 177555b9392bSMax Reitz error_setg_errno(errp, -ret, "Failed to write second region table"); 17763412f7b1SJeff Cody goto exit; 17773412f7b1SJeff Cody } 17783412f7b1SJeff Cody 17793412f7b1SJeff Cody exit: 17803412f7b1SJeff Cody g_free(s); 17813412f7b1SJeff Cody g_free(buffer); 17823412f7b1SJeff Cody return ret; 17833412f7b1SJeff Cody } 17843412f7b1SJeff Cody 17853412f7b1SJeff Cody /* We need to create the following elements: 17863412f7b1SJeff Cody * 17873412f7b1SJeff Cody * .-----------------------------------------------------------------. 17883412f7b1SJeff Cody * | (A) | (B) | (C) | (D) | (E) | 17893412f7b1SJeff Cody * | File ID | Header1 | Header 2 | Region Tbl 1 | Region Tbl 2 | 17903412f7b1SJeff Cody * | | | | | | 17913412f7b1SJeff Cody * .-----------------------------------------------------------------. 17923412f7b1SJeff Cody * 0 64KB 128KB 192KB 256KB 320KB 17933412f7b1SJeff Cody * 17943412f7b1SJeff Cody * 17953412f7b1SJeff Cody * .---- ~ ----------- ~ ------------ ~ ---------------- ~ -----------. 17963412f7b1SJeff Cody * | (F) | (G) | (H) | | 17973412f7b1SJeff Cody * | Journal Log | BAT / Bitmap | Metadata | .... data ...... | 17983412f7b1SJeff Cody * | | | | | 17993412f7b1SJeff Cody * .---- ~ ----------- ~ ------------ ~ ---------------- ~ -----------. 18003412f7b1SJeff Cody * 1MB 18013412f7b1SJeff Cody */ 180209b68dabSKevin Wolf static int coroutine_fn vhdx_co_create(BlockdevCreateOptions *opts, 1803efc75e2aSStefan Hajnoczi Error **errp) 18043412f7b1SJeff Cody { 180509b68dabSKevin Wolf BlockdevCreateOptionsVhdx *vhdx_opts; 180609b68dabSKevin Wolf BlockBackend *blk = NULL; 180709b68dabSKevin Wolf BlockDriverState *bs = NULL; 180809b68dabSKevin Wolf 18093412f7b1SJeff Cody int ret = 0; 181009b68dabSKevin Wolf uint64_t image_size; 181109b68dabSKevin Wolf uint32_t log_size; 181209b68dabSKevin Wolf uint32_t block_size; 18133412f7b1SJeff Cody uint64_t signature; 18143412f7b1SJeff Cody uint64_t metadata_offset; 18153412f7b1SJeff Cody bool use_zero_blocks = false; 18163412f7b1SJeff Cody 18173412f7b1SJeff Cody gunichar2 *creator = NULL; 18183412f7b1SJeff Cody glong creator_items; 18193412f7b1SJeff Cody VHDXImageType image_type; 18203412f7b1SJeff Cody 182109b68dabSKevin Wolf assert(opts->driver == BLOCKDEV_DRIVER_VHDX); 182209b68dabSKevin Wolf vhdx_opts = &opts->u.vhdx; 18233412f7b1SJeff Cody 182409b68dabSKevin Wolf /* Validate options and set default values */ 182509b68dabSKevin Wolf image_size = vhdx_opts->size; 18263412f7b1SJeff Cody if (image_size > VHDX_MAX_IMAGE_SIZE) { 18270fcc38e7SKevin Wolf error_setg(errp, "Image size too large; max of 64TB"); 182809b68dabSKevin Wolf return -EINVAL; 18293412f7b1SJeff Cody } 18303412f7b1SJeff Cody 183109b68dabSKevin Wolf if (!vhdx_opts->has_log_size) { 183209b68dabSKevin Wolf log_size = DEFAULT_LOG_SIZE; 18333412f7b1SJeff Cody } else { 18346f16f7c5SKevin Wolf if (vhdx_opts->log_size > UINT32_MAX) { 18356f16f7c5SKevin Wolf error_setg(errp, "Log size must be smaller than 4 GB"); 18366f16f7c5SKevin Wolf return -EINVAL; 18376f16f7c5SKevin Wolf } 183809b68dabSKevin Wolf log_size = vhdx_opts->log_size; 183909b68dabSKevin Wolf } 184009b68dabSKevin Wolf if (log_size < MiB || (log_size % MiB) != 0) { 18410fcc38e7SKevin Wolf error_setg(errp, "Log size must be a multiple of 1 MB"); 184209b68dabSKevin Wolf return -EINVAL; 184309b68dabSKevin Wolf } 184409b68dabSKevin Wolf 184509b68dabSKevin Wolf if (!vhdx_opts->has_block_state_zero) { 184609b68dabSKevin Wolf use_zero_blocks = true; 184709b68dabSKevin Wolf } else { 184809b68dabSKevin Wolf use_zero_blocks = vhdx_opts->block_state_zero; 184909b68dabSKevin Wolf } 185009b68dabSKevin Wolf 185109b68dabSKevin Wolf if (!vhdx_opts->has_subformat) { 185209b68dabSKevin Wolf vhdx_opts->subformat = BLOCKDEV_VHDX_SUBFORMAT_DYNAMIC; 185309b68dabSKevin Wolf } 185409b68dabSKevin Wolf 185509b68dabSKevin Wolf switch (vhdx_opts->subformat) { 185609b68dabSKevin Wolf case BLOCKDEV_VHDX_SUBFORMAT_DYNAMIC: 185709b68dabSKevin Wolf image_type = VHDX_TYPE_DYNAMIC; 185809b68dabSKevin Wolf break; 185909b68dabSKevin Wolf case BLOCKDEV_VHDX_SUBFORMAT_FIXED: 186009b68dabSKevin Wolf image_type = VHDX_TYPE_FIXED; 186109b68dabSKevin Wolf break; 186209b68dabSKevin Wolf default: 186309b68dabSKevin Wolf g_assert_not_reached(); 18643412f7b1SJeff Cody } 18653412f7b1SJeff Cody 18663412f7b1SJeff Cody /* These are pretty arbitrary, and mainly designed to keep the BAT 18673412f7b1SJeff Cody * size reasonable to load into RAM */ 186809b68dabSKevin Wolf if (vhdx_opts->has_block_size) { 186909b68dabSKevin Wolf block_size = vhdx_opts->block_size; 187009b68dabSKevin Wolf } else { 18713412f7b1SJeff Cody if (image_size > 32 * TiB) { 18723412f7b1SJeff Cody block_size = 64 * MiB; 18733412f7b1SJeff Cody } else if (image_size > (uint64_t) 100 * GiB) { 18743412f7b1SJeff Cody block_size = 32 * MiB; 18753412f7b1SJeff Cody } else if (image_size > 1 * GiB) { 18763412f7b1SJeff Cody block_size = 16 * MiB; 18773412f7b1SJeff Cody } else { 18783412f7b1SJeff Cody block_size = 8 * MiB; 18793412f7b1SJeff Cody } 18803412f7b1SJeff Cody } 18813412f7b1SJeff Cody 188209b68dabSKevin Wolf if (block_size < MiB || (block_size % MiB) != 0) { 18830fcc38e7SKevin Wolf error_setg(errp, "Block size must be a multiple of 1 MB"); 188409b68dabSKevin Wolf return -EINVAL; 188509b68dabSKevin Wolf } 1886b412f494SKevin Wolf if (!is_power_of_2(block_size)) { 1887b412f494SKevin Wolf error_setg(errp, "Block size must be a power of two"); 1888b412f494SKevin Wolf return -EINVAL; 1889b412f494SKevin Wolf } 189009b68dabSKevin Wolf if (block_size > VHDX_BLOCK_SIZE_MAX) { 18910fcc38e7SKevin Wolf error_setg(errp, "Block size must not exceed %d", VHDX_BLOCK_SIZE_MAX); 189209b68dabSKevin Wolf return -EINVAL; 189309b68dabSKevin Wolf } 18943412f7b1SJeff Cody 189509b68dabSKevin Wolf /* Create BlockBackend to write to the image */ 189609b68dabSKevin Wolf bs = bdrv_open_blockdev_ref(vhdx_opts->file, errp); 189709b68dabSKevin Wolf if (bs == NULL) { 189809b68dabSKevin Wolf return -EIO; 189909b68dabSKevin Wolf } 19003412f7b1SJeff Cody 190109b68dabSKevin Wolf blk = blk_new(BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL); 190209b68dabSKevin Wolf ret = blk_insert_bs(blk, bs, errp); 19033412f7b1SJeff Cody if (ret < 0) { 190409b68dabSKevin Wolf goto delete_and_exit; 19053412f7b1SJeff Cody } 190610bf03afSKevin Wolf blk_set_allow_write_beyond_eof(blk, true); 190710bf03afSKevin Wolf 19083412f7b1SJeff Cody /* Create (A) */ 19093412f7b1SJeff Cody 19103412f7b1SJeff Cody /* The creator field is optional, but may be useful for 19113412f7b1SJeff Cody * debugging / diagnostics */ 19123412f7b1SJeff Cody creator = g_utf8_to_utf16("QEMU v" QEMU_VERSION, -1, NULL, 19133412f7b1SJeff Cody &creator_items, NULL); 19143412f7b1SJeff Cody signature = cpu_to_le64(VHDX_FILE_SIGNATURE); 19158341f00dSEric Blake ret = blk_pwrite(blk, VHDX_FILE_ID_OFFSET, &signature, sizeof(signature), 19168341f00dSEric Blake 0); 19173412f7b1SJeff Cody if (ret < 0) { 191855b9392bSMax Reitz error_setg_errno(errp, -ret, "Failed to write file signature"); 19193412f7b1SJeff Cody goto delete_and_exit; 19203412f7b1SJeff Cody } 19213412f7b1SJeff Cody if (creator) { 192210bf03afSKevin Wolf ret = blk_pwrite(blk, VHDX_FILE_ID_OFFSET + sizeof(signature), 19238341f00dSEric Blake creator, creator_items * sizeof(gunichar2), 0); 19243412f7b1SJeff Cody if (ret < 0) { 192555b9392bSMax Reitz error_setg_errno(errp, -ret, "Failed to write creator field"); 19263412f7b1SJeff Cody goto delete_and_exit; 19273412f7b1SJeff Cody } 19283412f7b1SJeff Cody } 19293412f7b1SJeff Cody 19303412f7b1SJeff Cody 19313412f7b1SJeff Cody /* Creates (B),(C) */ 1932db1e80eeSKevin Wolf ret = vhdx_create_new_headers(blk, image_size, log_size); 19333412f7b1SJeff Cody if (ret < 0) { 193455b9392bSMax Reitz error_setg_errno(errp, -ret, "Failed to write image headers"); 19353412f7b1SJeff Cody goto delete_and_exit; 19363412f7b1SJeff Cody } 19373412f7b1SJeff Cody 19383412f7b1SJeff Cody /* Creates (D),(E),(G) explicitly. (F) created as by-product */ 1939db1e80eeSKevin Wolf ret = vhdx_create_new_region_table(blk, image_size, block_size, 512, 19403412f7b1SJeff Cody log_size, use_zero_blocks, image_type, 194155b9392bSMax Reitz &metadata_offset, errp); 19423412f7b1SJeff Cody if (ret < 0) { 19433412f7b1SJeff Cody goto delete_and_exit; 19443412f7b1SJeff Cody } 19453412f7b1SJeff Cody 19463412f7b1SJeff Cody /* Creates (H) */ 1947db1e80eeSKevin Wolf ret = vhdx_create_new_metadata(blk, image_size, block_size, 512, 19483412f7b1SJeff Cody metadata_offset, image_type); 19493412f7b1SJeff Cody if (ret < 0) { 195055b9392bSMax Reitz error_setg_errno(errp, -ret, "Failed to initialize metadata"); 19513412f7b1SJeff Cody goto delete_and_exit; 19523412f7b1SJeff Cody } 19533412f7b1SJeff Cody 19543412f7b1SJeff Cody 19553412f7b1SJeff Cody delete_and_exit: 195610bf03afSKevin Wolf blk_unref(blk); 195709b68dabSKevin Wolf bdrv_unref(bs); 19583412f7b1SJeff Cody g_free(creator); 19593412f7b1SJeff Cody return ret; 19603412f7b1SJeff Cody } 19613412f7b1SJeff Cody 196209b68dabSKevin Wolf static int coroutine_fn vhdx_co_create_opts(const char *filename, 196309b68dabSKevin Wolf QemuOpts *opts, 196409b68dabSKevin Wolf Error **errp) 196509b68dabSKevin Wolf { 196609b68dabSKevin Wolf BlockdevCreateOptions *create_options = NULL; 196709b68dabSKevin Wolf QDict *qdict = NULL; 196809b68dabSKevin Wolf QObject *qobj; 196909b68dabSKevin Wolf Visitor *v; 197009b68dabSKevin Wolf BlockDriverState *bs = NULL; 197109b68dabSKevin Wolf Error *local_err = NULL; 197209b68dabSKevin Wolf int ret; 197309b68dabSKevin Wolf 197409b68dabSKevin Wolf static const QDictRenames opt_renames[] = { 197509b68dabSKevin Wolf { VHDX_BLOCK_OPT_LOG_SIZE, "log-size" }, 197609b68dabSKevin Wolf { VHDX_BLOCK_OPT_BLOCK_SIZE, "block-size" }, 197709b68dabSKevin Wolf { VHDX_BLOCK_OPT_ZERO, "block-state-zero" }, 197809b68dabSKevin Wolf { NULL, NULL }, 197909b68dabSKevin Wolf }; 198009b68dabSKevin Wolf 198109b68dabSKevin Wolf /* Parse options and convert legacy syntax */ 198209b68dabSKevin Wolf qdict = qemu_opts_to_qdict_filtered(opts, NULL, &vhdx_create_opts, true); 198309b68dabSKevin Wolf 198409b68dabSKevin Wolf if (!qdict_rename_keys(qdict, opt_renames, errp)) { 198509b68dabSKevin Wolf ret = -EINVAL; 198609b68dabSKevin Wolf goto fail; 198709b68dabSKevin Wolf } 198809b68dabSKevin Wolf 198909b68dabSKevin Wolf /* Create and open the file (protocol layer) */ 199009b68dabSKevin Wolf ret = bdrv_create_file(filename, opts, &local_err); 199109b68dabSKevin Wolf if (ret < 0) { 199209b68dabSKevin Wolf error_propagate(errp, local_err); 199309b68dabSKevin Wolf goto fail; 199409b68dabSKevin Wolf } 199509b68dabSKevin Wolf 199609b68dabSKevin Wolf bs = bdrv_open(filename, NULL, NULL, 199709b68dabSKevin Wolf BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp); 199809b68dabSKevin Wolf if (bs == NULL) { 199909b68dabSKevin Wolf ret = -EIO; 200009b68dabSKevin Wolf goto fail; 200109b68dabSKevin Wolf } 200209b68dabSKevin Wolf 200309b68dabSKevin Wolf /* Now get the QAPI type BlockdevCreateOptions */ 200409b68dabSKevin Wolf qdict_put_str(qdict, "driver", "vhdx"); 200509b68dabSKevin Wolf qdict_put_str(qdict, "file", bs->node_name); 200609b68dabSKevin Wolf 200709b68dabSKevin Wolf qobj = qdict_crumple(qdict, errp); 2008cb3e7f08SMarc-André Lureau qobject_unref(qdict); 20097dc847ebSMax Reitz qdict = qobject_to(QDict, qobj); 201009b68dabSKevin Wolf if (qdict == NULL) { 201109b68dabSKevin Wolf ret = -EINVAL; 201209b68dabSKevin Wolf goto fail; 201309b68dabSKevin Wolf } 201409b68dabSKevin Wolf 201509b68dabSKevin Wolf v = qobject_input_visitor_new_keyval(QOBJECT(qdict)); 201609b68dabSKevin Wolf visit_type_BlockdevCreateOptions(v, NULL, &create_options, &local_err); 201709b68dabSKevin Wolf visit_free(v); 201809b68dabSKevin Wolf 201909b68dabSKevin Wolf if (local_err) { 202009b68dabSKevin Wolf error_propagate(errp, local_err); 202109b68dabSKevin Wolf ret = -EINVAL; 202209b68dabSKevin Wolf goto fail; 202309b68dabSKevin Wolf } 202409b68dabSKevin Wolf 202509b68dabSKevin Wolf /* Silently round up sizes: 202609b68dabSKevin Wolf * The image size is rounded to 512 bytes. Make the block and log size 202709b68dabSKevin Wolf * close to what was specified, but must be at least 1MB, and a multiple of 202809b68dabSKevin Wolf * 1 MB. Also respect VHDX_BLOCK_SIZE_MAX for block sizes. block_size = 0 202909b68dabSKevin Wolf * means auto, which is represented by a missing key in QAPI. */ 203009b68dabSKevin Wolf assert(create_options->driver == BLOCKDEV_DRIVER_VHDX); 203109b68dabSKevin Wolf create_options->u.vhdx.size = 203209b68dabSKevin Wolf ROUND_UP(create_options->u.vhdx.size, BDRV_SECTOR_SIZE); 203309b68dabSKevin Wolf 203409b68dabSKevin Wolf if (create_options->u.vhdx.has_log_size) { 203509b68dabSKevin Wolf create_options->u.vhdx.log_size = 203609b68dabSKevin Wolf ROUND_UP(create_options->u.vhdx.log_size, MiB); 203709b68dabSKevin Wolf } 203809b68dabSKevin Wolf if (create_options->u.vhdx.has_block_size) { 203909b68dabSKevin Wolf create_options->u.vhdx.block_size = 204009b68dabSKevin Wolf ROUND_UP(create_options->u.vhdx.block_size, MiB); 204109b68dabSKevin Wolf 204209b68dabSKevin Wolf if (create_options->u.vhdx.block_size == 0) { 204309b68dabSKevin Wolf create_options->u.vhdx.has_block_size = false; 204409b68dabSKevin Wolf } 204509b68dabSKevin Wolf if (create_options->u.vhdx.block_size > VHDX_BLOCK_SIZE_MAX) { 204609b68dabSKevin Wolf create_options->u.vhdx.block_size = VHDX_BLOCK_SIZE_MAX; 204709b68dabSKevin Wolf } 204809b68dabSKevin Wolf } 204909b68dabSKevin Wolf 205009b68dabSKevin Wolf /* Create the vhdx image (format layer) */ 205109b68dabSKevin Wolf ret = vhdx_co_create(create_options, errp); 205209b68dabSKevin Wolf 205309b68dabSKevin Wolf fail: 2054cb3e7f08SMarc-André Lureau qobject_unref(qdict); 205509b68dabSKevin Wolf bdrv_unref(bs); 205609b68dabSKevin Wolf qapi_free_BlockdevCreateOptions(create_options); 205709b68dabSKevin Wolf return ret; 205809b68dabSKevin Wolf } 205909b68dabSKevin Wolf 20607e30e6a6SJeff Cody /* If opened r/w, the VHDX driver will automatically replay the log, 20617e30e6a6SJeff Cody * if one is present, inside the vhdx_open() call. 20627e30e6a6SJeff Cody * 20637e30e6a6SJeff Cody * If qemu-img check -r all is called, the image is automatically opened 20647e30e6a6SJeff Cody * r/w and any log has already been replayed, so there is nothing (currently) 20657e30e6a6SJeff Cody * for us to do here 20667e30e6a6SJeff Cody */ 20672fd61638SPaolo Bonzini static int coroutine_fn vhdx_co_check(BlockDriverState *bs, 20682fd61638SPaolo Bonzini BdrvCheckResult *result, 20697e30e6a6SJeff Cody BdrvCheckMode fix) 20707e30e6a6SJeff Cody { 20717e30e6a6SJeff Cody BDRVVHDXState *s = bs->opaque; 20727e30e6a6SJeff Cody 20737e30e6a6SJeff Cody if (s->log_replayed_on_open) { 20747e30e6a6SJeff Cody result->corruptions_fixed++; 20757e30e6a6SJeff Cody } 20767e30e6a6SJeff Cody return 0; 20777e30e6a6SJeff Cody } 20787e30e6a6SJeff Cody 20795366092cSChunyan Liu static QemuOptsList vhdx_create_opts = { 20805366092cSChunyan Liu .name = "vhdx-create-opts", 20815366092cSChunyan Liu .head = QTAILQ_HEAD_INITIALIZER(vhdx_create_opts.head), 20825366092cSChunyan Liu .desc = { 20833412f7b1SJeff Cody { 20843412f7b1SJeff Cody .name = BLOCK_OPT_SIZE, 20855366092cSChunyan Liu .type = QEMU_OPT_SIZE, 20863412f7b1SJeff Cody .help = "Virtual disk size; max of 64TB." 20873412f7b1SJeff Cody }, 20883412f7b1SJeff Cody { 20893412f7b1SJeff Cody .name = VHDX_BLOCK_OPT_LOG_SIZE, 20905366092cSChunyan Liu .type = QEMU_OPT_SIZE, 20915366092cSChunyan Liu .def_value_str = stringify(DEFAULT_LOG_SIZE), 20923412f7b1SJeff Cody .help = "Log size; min 1MB." 20933412f7b1SJeff Cody }, 20943412f7b1SJeff Cody { 20953412f7b1SJeff Cody .name = VHDX_BLOCK_OPT_BLOCK_SIZE, 20965366092cSChunyan Liu .type = QEMU_OPT_SIZE, 20975366092cSChunyan Liu .def_value_str = stringify(0), 20983412f7b1SJeff Cody .help = "Block Size; min 1MB, max 256MB. " \ 20993412f7b1SJeff Cody "0 means auto-calculate based on image size." 21003412f7b1SJeff Cody }, 21013412f7b1SJeff Cody { 21023412f7b1SJeff Cody .name = BLOCK_OPT_SUBFMT, 21035366092cSChunyan Liu .type = QEMU_OPT_STRING, 21043412f7b1SJeff Cody .help = "VHDX format type, can be either 'dynamic' or 'fixed'. "\ 21053412f7b1SJeff Cody "Default is 'dynamic'." 21063412f7b1SJeff Cody }, 21073412f7b1SJeff Cody { 21083412f7b1SJeff Cody .name = VHDX_BLOCK_OPT_ZERO, 21095366092cSChunyan Liu .type = QEMU_OPT_BOOL, 211030af51ceSJeff Cody .help = "Force use of payload blocks of type 'ZERO'. "\ 211130af51ceSJeff Cody "Non-standard, but default. Do not set to 'off' when "\ 211230af51ceSJeff Cody "using 'qemu-img convert' with subformat=dynamic." 21133412f7b1SJeff Cody }, 21143412f7b1SJeff Cody { NULL } 21155366092cSChunyan Liu } 21163412f7b1SJeff Cody }; 21173412f7b1SJeff Cody 2118e8d4e5ffSJeff Cody static BlockDriver bdrv_vhdx = { 2119e8d4e5ffSJeff Cody .format_name = "vhdx", 2120e8d4e5ffSJeff Cody .instance_size = sizeof(BDRVVHDXState), 2121e8d4e5ffSJeff Cody .bdrv_probe = vhdx_probe, 2122e8d4e5ffSJeff Cody .bdrv_open = vhdx_open, 2123e8d4e5ffSJeff Cody .bdrv_close = vhdx_close, 2124e8d4e5ffSJeff Cody .bdrv_reopen_prepare = vhdx_reopen_prepare, 2125862f215fSKevin Wolf .bdrv_child_perm = bdrv_format_default_perms, 2126e8d4e5ffSJeff Cody .bdrv_co_readv = vhdx_co_readv, 2127e8d4e5ffSJeff Cody .bdrv_co_writev = vhdx_co_writev, 212809b68dabSKevin Wolf .bdrv_co_create = vhdx_co_create, 2129efc75e2aSStefan Hajnoczi .bdrv_co_create_opts = vhdx_co_create_opts, 213097b00e28SPaolo Bonzini .bdrv_get_info = vhdx_get_info, 21312fd61638SPaolo Bonzini .bdrv_co_check = vhdx_co_check, 213285b712c9SJeff Cody .bdrv_has_zero_init = bdrv_has_zero_init_1, 21333412f7b1SJeff Cody 21345366092cSChunyan Liu .create_opts = &vhdx_create_opts, 2135e8d4e5ffSJeff Cody }; 2136e8d4e5ffSJeff Cody 2137e8d4e5ffSJeff Cody static void bdrv_vhdx_init(void) 2138e8d4e5ffSJeff Cody { 2139e8d4e5ffSJeff Cody bdrv_register(&bdrv_vhdx); 2140e8d4e5ffSJeff Cody } 2141e8d4e5ffSJeff Cody 2142e8d4e5ffSJeff Cody block_init(bdrv_vhdx_init); 2143