1223d4670Sths /* 2223d4670Sths * Block driver for RAW files (win32) 3223d4670Sths * 4223d4670Sths * Copyright (c) 2006 Fabrice Bellard 5223d4670Sths * 6223d4670Sths * Permission is hereby granted, free of charge, to any person obtaining a copy 7223d4670Sths * of this software and associated documentation files (the "Software"), to deal 8223d4670Sths * in the Software without restriction, including without limitation the rights 9223d4670Sths * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10223d4670Sths * copies of the Software, and to permit persons to whom the Software is 11223d4670Sths * furnished to do so, subject to the following conditions: 12223d4670Sths * 13223d4670Sths * The above copyright notice and this permission notice shall be included in 14223d4670Sths * all copies or substantial portions of the Software. 15223d4670Sths * 16223d4670Sths * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17223d4670Sths * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18223d4670Sths * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19223d4670Sths * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20223d4670Sths * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21223d4670Sths * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22223d4670Sths * THE SOFTWARE. 23223d4670Sths */ 24922a01a0SMarkus Armbruster 2580c71a24SPeter Maydell #include "qemu/osdep.h" 26da34e65cSMarkus Armbruster #include "qapi/error.h" 27f348b6d1SVeronia Bahaa #include "qemu/cutils.h" 28e2c1c34fSMarkus Armbruster #include "block/block-io.h" 29737e150eSPaolo Bonzini #include "block/block_int.h" 301de7afc9SPaolo Bonzini #include "qemu/module.h" 31922a01a0SMarkus Armbruster #include "qemu/option.h" 320187f5c9SPaolo Bonzini #include "block/raw-aio.h" 33fc4edb84SPaolo Bonzini #include "trace.h" 34737e150eSPaolo Bonzini #include "block/thread-pool.h" 351de7afc9SPaolo Bonzini #include "qemu/iov.h" 36*407bc4bfSDaniel P. Berrangé #include "qobject/qdict.h" 37*407bc4bfSDaniel P. Berrangé #include "qobject/qstring.h" 3849dc768dSaliguori #include <windows.h> 39223d4670Sths #include <winioctl.h> 40223d4670Sths 41223d4670Sths #define FTYPE_FILE 0 42223d4670Sths #define FTYPE_CD 1 43223d4670Sths #define FTYPE_HARDDISK 2 44223d4670Sths 45fc4edb84SPaolo Bonzini typedef struct RawWin32AIOData { 46fc4edb84SPaolo Bonzini BlockDriverState *bs; 47fc4edb84SPaolo Bonzini HANDLE hfile; 48fc4edb84SPaolo Bonzini struct iovec *aio_iov; 49fc4edb84SPaolo Bonzini int aio_niov; 50fc4edb84SPaolo Bonzini size_t aio_nbytes; 51fc4edb84SPaolo Bonzini off64_t aio_offset; 52fc4edb84SPaolo Bonzini int aio_type; 53fc4edb84SPaolo Bonzini } RawWin32AIOData; 54fc4edb84SPaolo Bonzini 55223d4670Sths typedef struct BDRVRawState { 56223d4670Sths HANDLE hfile; 57223d4670Sths int type; 58223d4670Sths char drive_path[16]; /* format: "d:\" */ 59a2736526SPaolo Bonzini QEMUWin32AIOState *aio; 60223d4670Sths } BDRVRawState; 61223d4670Sths 62ebd979c7SViktor Prutyanov typedef struct BDRVRawReopenState { 63ebd979c7SViktor Prutyanov HANDLE hfile; 64ebd979c7SViktor Prutyanov } BDRVRawReopenState; 65ebd979c7SViktor Prutyanov 66fc4edb84SPaolo Bonzini /* 67fc4edb84SPaolo Bonzini * Read/writes the data to/from a given linear buffer. 68fc4edb84SPaolo Bonzini * 69fc4edb84SPaolo Bonzini * Returns the number of bytes handles or -errno in case of an error. Short 70fc4edb84SPaolo Bonzini * reads are only returned if the end of the file is reached. 71fc4edb84SPaolo Bonzini */ 72fc4edb84SPaolo Bonzini static size_t handle_aiocb_rw(RawWin32AIOData *aiocb) 73fc4edb84SPaolo Bonzini { 74fc4edb84SPaolo Bonzini size_t offset = 0; 75fc4edb84SPaolo Bonzini int i; 76fc4edb84SPaolo Bonzini 77fc4edb84SPaolo Bonzini for (i = 0; i < aiocb->aio_niov; i++) { 78fc4edb84SPaolo Bonzini OVERLAPPED ov; 79fc4edb84SPaolo Bonzini DWORD ret, ret_count, len; 80fc4edb84SPaolo Bonzini 81fc4edb84SPaolo Bonzini memset(&ov, 0, sizeof(ov)); 82fc4edb84SPaolo Bonzini ov.Offset = (aiocb->aio_offset + offset); 83fc4edb84SPaolo Bonzini ov.OffsetHigh = (aiocb->aio_offset + offset) >> 32; 84fc4edb84SPaolo Bonzini len = aiocb->aio_iov[i].iov_len; 85fc4edb84SPaolo Bonzini if (aiocb->aio_type & QEMU_AIO_WRITE) { 86fc4edb84SPaolo Bonzini ret = WriteFile(aiocb->hfile, aiocb->aio_iov[i].iov_base, 87fc4edb84SPaolo Bonzini len, &ret_count, &ov); 88fc4edb84SPaolo Bonzini } else { 89fc4edb84SPaolo Bonzini ret = ReadFile(aiocb->hfile, aiocb->aio_iov[i].iov_base, 90fc4edb84SPaolo Bonzini len, &ret_count, &ov); 91fc4edb84SPaolo Bonzini } 92fc4edb84SPaolo Bonzini if (!ret) { 93fc4edb84SPaolo Bonzini ret_count = 0; 94fc4edb84SPaolo Bonzini } 95fc4edb84SPaolo Bonzini if (ret_count != len) { 9656e023afSTal Kain offset += ret_count; 97fc4edb84SPaolo Bonzini break; 98fc4edb84SPaolo Bonzini } 99fc4edb84SPaolo Bonzini offset += len; 100fc4edb84SPaolo Bonzini } 101fc4edb84SPaolo Bonzini 102fc4edb84SPaolo Bonzini return offset; 103fc4edb84SPaolo Bonzini } 104fc4edb84SPaolo Bonzini 105fc4edb84SPaolo Bonzini static int aio_worker(void *arg) 106fc4edb84SPaolo Bonzini { 107fc4edb84SPaolo Bonzini RawWin32AIOData *aiocb = arg; 108fc4edb84SPaolo Bonzini ssize_t ret = 0; 109fc4edb84SPaolo Bonzini size_t count; 110fc4edb84SPaolo Bonzini 111fc4edb84SPaolo Bonzini switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) { 112fc4edb84SPaolo Bonzini case QEMU_AIO_READ: 113fc4edb84SPaolo Bonzini count = handle_aiocb_rw(aiocb); 114c0191e76SMax Reitz if (count < aiocb->aio_nbytes) { 115fc4edb84SPaolo Bonzini /* A short read means that we have reached EOF. Pad the buffer 116fc4edb84SPaolo Bonzini * with zeros for bytes after EOF. */ 117fc4edb84SPaolo Bonzini iov_memset(aiocb->aio_iov, aiocb->aio_niov, count, 118fc4edb84SPaolo Bonzini 0, aiocb->aio_nbytes - count); 119fc4edb84SPaolo Bonzini 120fc4edb84SPaolo Bonzini count = aiocb->aio_nbytes; 121fc4edb84SPaolo Bonzini } 122fc4edb84SPaolo Bonzini if (count == aiocb->aio_nbytes) { 123fc4edb84SPaolo Bonzini ret = 0; 124fc4edb84SPaolo Bonzini } else { 125fc4edb84SPaolo Bonzini ret = -EINVAL; 126fc4edb84SPaolo Bonzini } 127fc4edb84SPaolo Bonzini break; 128fc4edb84SPaolo Bonzini case QEMU_AIO_WRITE: 129fc4edb84SPaolo Bonzini count = handle_aiocb_rw(aiocb); 130fc4edb84SPaolo Bonzini if (count == aiocb->aio_nbytes) { 1315d555030SKevin Wolf ret = 0; 132fc4edb84SPaolo Bonzini } else { 1335d555030SKevin Wolf ret = -EINVAL; 134fc4edb84SPaolo Bonzini } 135fc4edb84SPaolo Bonzini break; 136fc4edb84SPaolo Bonzini case QEMU_AIO_FLUSH: 137fc4edb84SPaolo Bonzini if (!FlushFileBuffers(aiocb->hfile)) { 138fc4edb84SPaolo Bonzini return -EIO; 139fc4edb84SPaolo Bonzini } 140fc4edb84SPaolo Bonzini break; 141fc4edb84SPaolo Bonzini default: 142fc4edb84SPaolo Bonzini fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type); 143fc4edb84SPaolo Bonzini ret = -EINVAL; 144fc4edb84SPaolo Bonzini break; 145fc4edb84SPaolo Bonzini } 146fc4edb84SPaolo Bonzini 147c84b3192SPaolo Bonzini g_free(aiocb); 148fc4edb84SPaolo Bonzini return ret; 149fc4edb84SPaolo Bonzini } 150fc4edb84SPaolo Bonzini 1517c84b1b8SMarkus Armbruster static BlockAIOCB *paio_submit(BlockDriverState *bs, HANDLE hfile, 15236e3b2e7SEric Blake int64_t offset, QEMUIOVector *qiov, int count, 153097310b5SMarkus Armbruster BlockCompletionFunc *cb, void *opaque, int type) 154fc4edb84SPaolo Bonzini { 155c84b3192SPaolo Bonzini RawWin32AIOData *acb = g_new(RawWin32AIOData, 1); 156fc4edb84SPaolo Bonzini 157fc4edb84SPaolo Bonzini acb->bs = bs; 158fc4edb84SPaolo Bonzini acb->hfile = hfile; 159fc4edb84SPaolo Bonzini acb->aio_type = type; 160fc4edb84SPaolo Bonzini 161fc4edb84SPaolo Bonzini if (qiov) { 162fc4edb84SPaolo Bonzini acb->aio_iov = qiov->iov; 163fc4edb84SPaolo Bonzini acb->aio_niov = qiov->niov; 16436e3b2e7SEric Blake assert(qiov->size == count); 165fc4edb84SPaolo Bonzini } 16636e3b2e7SEric Blake acb->aio_nbytes = count; 16736e3b2e7SEric Blake acb->aio_offset = offset; 168fc4edb84SPaolo Bonzini 169f8a30874SFam Zheng trace_file_paio_submit(acb, opaque, offset, count, type); 170aef04fc7SEmanuele Giuseppe Esposito return thread_pool_submit_aio(aio_worker, acb, cb, opaque); 171fc4edb84SPaolo Bonzini } 172fc4edb84SPaolo Bonzini 173223d4670Sths int qemu_ftruncate64(int fd, int64_t length) 174223d4670Sths { 175223d4670Sths LARGE_INTEGER li; 1762c993ec2SStefan Weil DWORD dw; 177223d4670Sths LONG high; 178223d4670Sths HANDLE h; 179223d4670Sths BOOL res; 180223d4670Sths 181223d4670Sths if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0) 182223d4670Sths return -1; 183223d4670Sths 184223d4670Sths h = (HANDLE)_get_osfhandle(fd); 185223d4670Sths 186223d4670Sths /* get current position, ftruncate do not change position */ 187223d4670Sths li.HighPart = 0; 188223d4670Sths li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT); 1892c993ec2SStefan Weil if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) { 190223d4670Sths return -1; 1912c993ec2SStefan Weil } 192223d4670Sths 193223d4670Sths high = length >> 32; 1942c993ec2SStefan Weil dw = SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN); 1952c993ec2SStefan Weil if (dw == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) { 196223d4670Sths return -1; 1972c993ec2SStefan Weil } 198223d4670Sths res = SetEndOfFile(h); 199223d4670Sths 200223d4670Sths /* back to old position */ 201223d4670Sths SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN); 202223d4670Sths return res ? 0 : -1; 203223d4670Sths } 204223d4670Sths 205223d4670Sths static int set_sparse(int fd) 206223d4670Sths { 207223d4670Sths DWORD returned; 208223d4670Sths return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE, 209223d4670Sths NULL, 0, NULL, 0, &returned, NULL); 210223d4670Sths } 211223d4670Sths 21285ebd381SStefan Hajnoczi static void raw_detach_aio_context(BlockDriverState *bs) 21385ebd381SStefan Hajnoczi { 21485ebd381SStefan Hajnoczi BDRVRawState *s = bs->opaque; 21585ebd381SStefan Hajnoczi 21685ebd381SStefan Hajnoczi if (s->aio) { 21785ebd381SStefan Hajnoczi win32_aio_detach_aio_context(s->aio, bdrv_get_aio_context(bs)); 21885ebd381SStefan Hajnoczi } 21985ebd381SStefan Hajnoczi } 22085ebd381SStefan Hajnoczi 22185ebd381SStefan Hajnoczi static void raw_attach_aio_context(BlockDriverState *bs, 22285ebd381SStefan Hajnoczi AioContext *new_context) 22385ebd381SStefan Hajnoczi { 22485ebd381SStefan Hajnoczi BDRVRawState *s = bs->opaque; 22585ebd381SStefan Hajnoczi 22685ebd381SStefan Hajnoczi if (s->aio) { 22785ebd381SStefan Hajnoczi win32_aio_attach_aio_context(s->aio, new_context); 22885ebd381SStefan Hajnoczi } 22985ebd381SStefan Hajnoczi } 23085ebd381SStefan Hajnoczi 2312914a1deSEric Blake static void raw_probe_alignment(BlockDriverState *bs, Error **errp) 232c25f53b0SPaolo Bonzini { 233c25f53b0SPaolo Bonzini BDRVRawState *s = bs->opaque; 234c25f53b0SPaolo Bonzini DWORD sectorsPerCluster, freeClusters, totalClusters, count; 235c25f53b0SPaolo Bonzini DISK_GEOMETRY_EX dg; 236c25f53b0SPaolo Bonzini BOOL status; 237c25f53b0SPaolo Bonzini 238c25f53b0SPaolo Bonzini if (s->type == FTYPE_CD) { 239a5b8dd2cSEric Blake bs->bl.request_alignment = 2048; 240c25f53b0SPaolo Bonzini return; 241c25f53b0SPaolo Bonzini } 242c25f53b0SPaolo Bonzini if (s->type == FTYPE_HARDDISK) { 243c25f53b0SPaolo Bonzini status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, 244c25f53b0SPaolo Bonzini NULL, 0, &dg, sizeof(dg), &count, NULL); 245c25f53b0SPaolo Bonzini if (status != 0) { 246a5b8dd2cSEric Blake bs->bl.request_alignment = dg.Geometry.BytesPerSector; 247c25f53b0SPaolo Bonzini return; 248c25f53b0SPaolo Bonzini } 249c25f53b0SPaolo Bonzini /* try GetDiskFreeSpace too */ 250c25f53b0SPaolo Bonzini } 251c25f53b0SPaolo Bonzini 252c25f53b0SPaolo Bonzini if (s->drive_path[0]) { 253c25f53b0SPaolo Bonzini GetDiskFreeSpace(s->drive_path, §orsPerCluster, 254c25f53b0SPaolo Bonzini &dg.Geometry.BytesPerSector, 255c25f53b0SPaolo Bonzini &freeClusters, &totalClusters); 256a5b8dd2cSEric Blake bs->bl.request_alignment = dg.Geometry.BytesPerSector; 257de7056a3SEric Blake return; 258c25f53b0SPaolo Bonzini } 259de7056a3SEric Blake 260de7056a3SEric Blake /* XXX Does Windows support AIO on less than 512-byte alignment? */ 261de7056a3SEric Blake bs->bl.request_alignment = 512; 262c25f53b0SPaolo Bonzini } 263c25f53b0SPaolo Bonzini 2640a4279d9SKevin Wolf static void raw_parse_flags(int flags, bool use_aio, int *access_flags, 2650a4279d9SKevin Wolf DWORD *overlapped) 2666a8dc042SJeff Cody { 2676a8dc042SJeff Cody assert(access_flags != NULL); 2686a8dc042SJeff Cody assert(overlapped != NULL); 2696a8dc042SJeff Cody 2706a8dc042SJeff Cody if (flags & BDRV_O_RDWR) { 2716a8dc042SJeff Cody *access_flags = GENERIC_READ | GENERIC_WRITE; 2726a8dc042SJeff Cody } else { 2736a8dc042SJeff Cody *access_flags = GENERIC_READ; 2746a8dc042SJeff Cody } 2756a8dc042SJeff Cody 2766a8dc042SJeff Cody *overlapped = FILE_ATTRIBUTE_NORMAL; 2770a4279d9SKevin Wolf if (use_aio) { 278a2736526SPaolo Bonzini *overlapped |= FILE_FLAG_OVERLAPPED; 279a2736526SPaolo Bonzini } 2806a8dc042SJeff Cody if (flags & BDRV_O_NOCACHE) { 2816a8dc042SJeff Cody *overlapped |= FILE_FLAG_NO_BUFFERING; 2826a8dc042SJeff Cody } 2836a8dc042SJeff Cody } 2846a8dc042SJeff Cody 2857dc74db8SMax Reitz static void raw_parse_filename(const char *filename, QDict *options, 2867dc74db8SMax Reitz Error **errp) 2877dc74db8SMax Reitz { 28803c320d8SMax Reitz bdrv_parse_filename_strip_prefix(filename, "file:", options); 2897dc74db8SMax Reitz } 2907dc74db8SMax Reitz 2918a79380bSKevin Wolf static QemuOptsList raw_runtime_opts = { 2928a79380bSKevin Wolf .name = "raw", 2938a79380bSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head), 2948a79380bSKevin Wolf .desc = { 2958a79380bSKevin Wolf { 2968a79380bSKevin Wolf .name = "filename", 2978a79380bSKevin Wolf .type = QEMU_OPT_STRING, 2988a79380bSKevin Wolf .help = "File name of the image", 2998a79380bSKevin Wolf }, 3000a4279d9SKevin Wolf { 3010a4279d9SKevin Wolf .name = "aio", 3020a4279d9SKevin Wolf .type = QEMU_OPT_STRING, 3030a4279d9SKevin Wolf .help = "host AIO implementation (threads, native)", 3040a4279d9SKevin Wolf }, 3053b079ac0SKevin Wolf { 3063b079ac0SKevin Wolf .name = "locking", 3073b079ac0SKevin Wolf .type = QEMU_OPT_STRING, 3083b079ac0SKevin Wolf .help = "file locking mode (on/off/auto, default: auto)", 3093b079ac0SKevin Wolf }, 3108a79380bSKevin Wolf { /* end of list */ } 3118a79380bSKevin Wolf }, 3128a79380bSKevin Wolf }; 3138a79380bSKevin Wolf 3140a4279d9SKevin Wolf static bool get_aio_option(QemuOpts *opts, int flags, Error **errp) 3150a4279d9SKevin Wolf { 3160a4279d9SKevin Wolf BlockdevAioOptions aio, aio_default; 3170a4279d9SKevin Wolf 3180a4279d9SKevin Wolf aio_default = (flags & BDRV_O_NATIVE_AIO) ? BLOCKDEV_AIO_OPTIONS_NATIVE 3190a4279d9SKevin Wolf : BLOCKDEV_AIO_OPTIONS_THREADS; 320f7abe0ecSMarc-André Lureau aio = qapi_enum_parse(&BlockdevAioOptions_lookup, qemu_opt_get(opts, "aio"), 32106c60b6cSMarkus Armbruster aio_default, errp); 3220a4279d9SKevin Wolf 3230a4279d9SKevin Wolf switch (aio) { 3240a4279d9SKevin Wolf case BLOCKDEV_AIO_OPTIONS_NATIVE: 3250a4279d9SKevin Wolf return true; 3260a4279d9SKevin Wolf case BLOCKDEV_AIO_OPTIONS_THREADS: 3270a4279d9SKevin Wolf return false; 3280a4279d9SKevin Wolf default: 3290a4279d9SKevin Wolf error_setg(errp, "Invalid AIO option"); 3300a4279d9SKevin Wolf } 3310a4279d9SKevin Wolf return false; 3320a4279d9SKevin Wolf } 3330a4279d9SKevin Wolf 334015a1036SMax Reitz static int raw_open(BlockDriverState *bs, QDict *options, int flags, 335015a1036SMax Reitz Error **errp) 336223d4670Sths { 337223d4670Sths BDRVRawState *s = bs->opaque; 3389a2d77adSChristoph Hellwig int access_flags; 339223d4670Sths DWORD overlapped; 3408a79380bSKevin Wolf QemuOpts *opts; 3418a79380bSKevin Wolf Error *local_err = NULL; 3428a79380bSKevin Wolf const char *filename; 3430a4279d9SKevin Wolf bool use_aio; 3443b079ac0SKevin Wolf OnOffAuto locking; 3458a79380bSKevin Wolf int ret; 346223d4670Sths 347223d4670Sths s->type = FTYPE_FILE; 348223d4670Sths 34987ea75d5SPeter Crosthwaite opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort); 350af175e85SMarkus Armbruster if (!qemu_opts_absorb_qdict(opts, options, errp)) { 3518a79380bSKevin Wolf ret = -EINVAL; 3528a79380bSKevin Wolf goto fail; 3538a79380bSKevin Wolf } 3548a79380bSKevin Wolf 3553b079ac0SKevin Wolf locking = qapi_enum_parse(&OnOffAuto_lookup, 3563b079ac0SKevin Wolf qemu_opt_get(opts, "locking"), 3573b079ac0SKevin Wolf ON_OFF_AUTO_AUTO, &local_err); 3583b079ac0SKevin Wolf if (local_err) { 3593b079ac0SKevin Wolf error_propagate(errp, local_err); 3603b079ac0SKevin Wolf ret = -EINVAL; 3613b079ac0SKevin Wolf goto fail; 3623b079ac0SKevin Wolf } 3633b079ac0SKevin Wolf switch (locking) { 3643b079ac0SKevin Wolf case ON_OFF_AUTO_ON: 3651c3a555cSFam Zheng error_setg(errp, "locking=on is not supported on Windows"); 366cdece046SGerd Hoffmann ret = -EINVAL; 3671c3a555cSFam Zheng goto fail; 3683b079ac0SKevin Wolf case ON_OFF_AUTO_OFF: 3693b079ac0SKevin Wolf case ON_OFF_AUTO_AUTO: 3703b079ac0SKevin Wolf break; 3713b079ac0SKevin Wolf default: 3723b079ac0SKevin Wolf g_assert_not_reached(); 3731c3a555cSFam Zheng } 3741c3a555cSFam Zheng 3758a79380bSKevin Wolf filename = qemu_opt_get(opts, "filename"); 3768a79380bSKevin Wolf 3770a4279d9SKevin Wolf use_aio = get_aio_option(opts, flags, &local_err); 3780a4279d9SKevin Wolf if (local_err) { 3790a4279d9SKevin Wolf error_propagate(errp, local_err); 3800a4279d9SKevin Wolf ret = -EINVAL; 3810a4279d9SKevin Wolf goto fail; 3820a4279d9SKevin Wolf } 3830a4279d9SKevin Wolf 3840a4279d9SKevin Wolf raw_parse_flags(flags, use_aio, &access_flags, &overlapped); 3859a2d77adSChristoph Hellwig 386c25f53b0SPaolo Bonzini if (filename[0] && filename[1] == ':') { 387c25f53b0SPaolo Bonzini snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", filename[0]); 388c25f53b0SPaolo Bonzini } else if (filename[0] == '\\' && filename[1] == '\\') { 389c25f53b0SPaolo Bonzini s->drive_path[0] = 0; 390c25f53b0SPaolo Bonzini } else { 391c25f53b0SPaolo Bonzini /* Relative path. */ 392c25f53b0SPaolo Bonzini char buf[MAX_PATH]; 393c25f53b0SPaolo Bonzini GetCurrentDirectory(MAX_PATH, buf); 394c25f53b0SPaolo Bonzini snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", buf[0]); 395c25f53b0SPaolo Bonzini } 396c25f53b0SPaolo Bonzini 397223d4670Sths s->hfile = CreateFile(filename, access_flags, 398ebd979c7SViktor Prutyanov FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, 3999a2d77adSChristoph Hellwig OPEN_EXISTING, overlapped, NULL); 400223d4670Sths if (s->hfile == INVALID_HANDLE_VALUE) { 401223d4670Sths int err = GetLastError(); 402223d4670Sths 40309237757SHalil Pasic error_setg_win32(errp, err, "Could not open '%s'", filename); 4048a79380bSKevin Wolf if (err == ERROR_ACCESS_DENIED) { 4058a79380bSKevin Wolf ret = -EACCES; 4068a79380bSKevin Wolf } else { 4078a79380bSKevin Wolf ret = -EINVAL; 4088a79380bSKevin Wolf } 4098a79380bSKevin Wolf goto fail; 410a2736526SPaolo Bonzini } 411a2736526SPaolo Bonzini 4120a4279d9SKevin Wolf if (use_aio) { 41399cc5989SStefan Hajnoczi s->aio = win32_aio_init(); 41499cc5989SStefan Hajnoczi if (s->aio == NULL) { 41599cc5989SStefan Hajnoczi CloseHandle(s->hfile); 41699cc5989SStefan Hajnoczi error_setg(errp, "Could not initialize AIO"); 41799cc5989SStefan Hajnoczi ret = -EINVAL; 41899cc5989SStefan Hajnoczi goto fail; 41999cc5989SStefan Hajnoczi } 42099cc5989SStefan Hajnoczi 42199cc5989SStefan Hajnoczi ret = win32_aio_attach(s->aio, s->hfile); 422a2736526SPaolo Bonzini if (ret < 0) { 42399cc5989SStefan Hajnoczi win32_aio_cleanup(s->aio); 424a2736526SPaolo Bonzini CloseHandle(s->hfile); 425c6252b7cSMax Reitz error_setg_errno(errp, -ret, "Could not enable AIO"); 4268a79380bSKevin Wolf goto fail; 427a2736526SPaolo Bonzini } 42885ebd381SStefan Hajnoczi 42985ebd381SStefan Hajnoczi win32_aio_attach_aio_context(s->aio, bdrv_get_aio_context(bs)); 430223d4670Sths } 4318a79380bSKevin Wolf 4328e519795SEric Blake /* When extending regular files, we get zeros from the OS */ 4338e519795SEric Blake bs->supported_truncate_flags = BDRV_REQ_ZERO_WRITE; 4348e519795SEric Blake 4358a79380bSKevin Wolf ret = 0; 4368a79380bSKevin Wolf fail: 4378a79380bSKevin Wolf qemu_opts_del(opts); 4388a79380bSKevin Wolf return ret; 439223d4670Sths } 440223d4670Sths 441de7056a3SEric Blake static BlockAIOCB *raw_aio_preadv(BlockDriverState *bs, 442f7ef38ddSVladimir Sementsov-Ogievskiy int64_t offset, int64_t bytes, 443f7ef38ddSVladimir Sementsov-Ogievskiy QEMUIOVector *qiov, BdrvRequestFlags flags, 444097310b5SMarkus Armbruster BlockCompletionFunc *cb, void *opaque) 445223d4670Sths { 446223d4670Sths BDRVRawState *s = bs->opaque; 447a2736526SPaolo Bonzini if (s->aio) { 448de7056a3SEric Blake return win32_aio_submit(bs, s->aio, s->hfile, offset, bytes, qiov, 449de7056a3SEric Blake cb, opaque, QEMU_AIO_READ); 450a2736526SPaolo Bonzini } else { 451de7056a3SEric Blake return paio_submit(bs, s->hfile, offset, qiov, bytes, 452fc4edb84SPaolo Bonzini cb, opaque, QEMU_AIO_READ); 453223d4670Sths } 454a2736526SPaolo Bonzini } 455223d4670Sths 456de7056a3SEric Blake static BlockAIOCB *raw_aio_pwritev(BlockDriverState *bs, 457e75abedaSVladimir Sementsov-Ogievskiy int64_t offset, int64_t bytes, 458e75abedaSVladimir Sementsov-Ogievskiy QEMUIOVector *qiov, BdrvRequestFlags flags, 459097310b5SMarkus Armbruster BlockCompletionFunc *cb, void *opaque) 460223d4670Sths { 461223d4670Sths BDRVRawState *s = bs->opaque; 462a2736526SPaolo Bonzini if (s->aio) { 463de7056a3SEric Blake return win32_aio_submit(bs, s->aio, s->hfile, offset, bytes, qiov, 464de7056a3SEric Blake cb, opaque, QEMU_AIO_WRITE); 465a2736526SPaolo Bonzini } else { 466de7056a3SEric Blake return paio_submit(bs, s->hfile, offset, qiov, bytes, 467fc4edb84SPaolo Bonzini cb, opaque, QEMU_AIO_WRITE); 468223d4670Sths } 469a2736526SPaolo Bonzini } 470223d4670Sths 4717c84b1b8SMarkus Armbruster static BlockAIOCB *raw_aio_flush(BlockDriverState *bs, 472097310b5SMarkus Armbruster BlockCompletionFunc *cb, void *opaque) 473223d4670Sths { 474223d4670Sths BDRVRawState *s = bs->opaque; 475fc4edb84SPaolo Bonzini return paio_submit(bs, s->hfile, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH); 476223d4670Sths } 477223d4670Sths 478223d4670Sths static void raw_close(BlockDriverState *bs) 479223d4670Sths { 480223d4670Sths BDRVRawState *s = bs->opaque; 48199cc5989SStefan Hajnoczi 48299cc5989SStefan Hajnoczi if (s->aio) { 48385ebd381SStefan Hajnoczi win32_aio_detach_aio_context(s->aio, bdrv_get_aio_context(bs)); 48499cc5989SStefan Hajnoczi win32_aio_cleanup(s->aio); 48599cc5989SStefan Hajnoczi s->aio = NULL; 48699cc5989SStefan Hajnoczi } 48799cc5989SStefan Hajnoczi 488223d4670Sths CloseHandle(s->hfile); 4898bfea15dSKevin Wolf if (bs->open_flags & BDRV_O_TEMPORARY) { 4908bfea15dSKevin Wolf unlink(bs->filename); 4918bfea15dSKevin Wolf } 492223d4670Sths } 493223d4670Sths 494061ca8a3SKevin Wolf static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset, 495c80d8b06SMax Reitz bool exact, PreallocMode prealloc, 49692b92799SKevin Wolf BdrvRequestFlags flags, Error **errp) 497223d4670Sths { 498223d4670Sths BDRVRawState *s = bs->opaque; 499b9e82a59Sblueswir1 LONG low, high; 500fbcad04dSFabien Chouteau DWORD dwPtrLow; 501223d4670Sths 5028243ccb7SMax Reitz if (prealloc != PREALLOC_MODE_OFF) { 5038243ccb7SMax Reitz error_setg(errp, "Unsupported preallocation mode '%s'", 504977c736fSMarkus Armbruster PreallocMode_str(prealloc)); 5058243ccb7SMax Reitz return -ENOTSUP; 5068243ccb7SMax Reitz } 5078243ccb7SMax Reitz 508223d4670Sths low = offset; 509223d4670Sths high = offset >> 32; 510fbcad04dSFabien Chouteau 511fbcad04dSFabien Chouteau /* 512fbcad04dSFabien Chouteau * An error has occurred if the return value is INVALID_SET_FILE_POINTER 513fbcad04dSFabien Chouteau * and GetLastError doesn't return NO_ERROR. 514fbcad04dSFabien Chouteau */ 515fbcad04dSFabien Chouteau dwPtrLow = SetFilePointer(s->hfile, low, &high, FILE_BEGIN); 516fbcad04dSFabien Chouteau if (dwPtrLow == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) { 5174bff28b8SMax Reitz error_setg_win32(errp, GetLastError(), "SetFilePointer error"); 518223d4670Sths return -EIO; 519fbcad04dSFabien Chouteau } 520fbcad04dSFabien Chouteau if (SetEndOfFile(s->hfile) == 0) { 5214bff28b8SMax Reitz error_setg_win32(errp, GetLastError(), "SetEndOfFile error"); 522223d4670Sths return -EIO; 523fbcad04dSFabien Chouteau } 524223d4670Sths return 0; 525223d4670Sths } 526223d4670Sths 527c86422c5SEmanuele Giuseppe Esposito static int64_t coroutine_fn raw_co_getlength(BlockDriverState *bs) 528223d4670Sths { 529223d4670Sths BDRVRawState *s = bs->opaque; 530223d4670Sths LARGE_INTEGER l; 531223d4670Sths ULARGE_INTEGER available, total, total_free; 532223d4670Sths DISK_GEOMETRY_EX dg; 533223d4670Sths DWORD count; 534223d4670Sths BOOL status; 535223d4670Sths 536223d4670Sths switch(s->type) { 537223d4670Sths case FTYPE_FILE: 538b9e82a59Sblueswir1 l.LowPart = GetFileSize(s->hfile, (PDWORD)&l.HighPart); 539223d4670Sths if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR) 540223d4670Sths return -EIO; 541223d4670Sths break; 542223d4670Sths case FTYPE_CD: 543223d4670Sths if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free)) 544223d4670Sths return -EIO; 545223d4670Sths l.QuadPart = total.QuadPart; 546223d4670Sths break; 547223d4670Sths case FTYPE_HARDDISK: 548223d4670Sths status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, 549223d4670Sths NULL, 0, &dg, sizeof(dg), &count, NULL); 550223d4670Sths if (status != 0) { 551223d4670Sths l = dg.DiskSize; 552223d4670Sths } 553223d4670Sths break; 554223d4670Sths default: 555223d4670Sths return -EIO; 556223d4670Sths } 557223d4670Sths return l.QuadPart; 558223d4670Sths } 559223d4670Sths 56082618d7bSEmanuele Giuseppe Esposito static int64_t coroutine_fn raw_co_get_allocated_file_size(BlockDriverState *bs) 5614a1d5e1fSFam Zheng { 5624a1d5e1fSFam Zheng typedef DWORD (WINAPI * get_compressed_t)(const char *filename, 5634a1d5e1fSFam Zheng DWORD * high); 5644a1d5e1fSFam Zheng get_compressed_t get_compressed; 5654a1d5e1fSFam Zheng struct _stati64 st; 5664a1d5e1fSFam Zheng const char *filename = bs->filename; 5674a1d5e1fSFam Zheng /* WinNT support GetCompressedFileSize to determine allocate size */ 5684a1d5e1fSFam Zheng get_compressed = 5694a1d5e1fSFam Zheng (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"), 5704a1d5e1fSFam Zheng "GetCompressedFileSizeA"); 5714a1d5e1fSFam Zheng if (get_compressed) { 5724a1d5e1fSFam Zheng DWORD high, low; 5734a1d5e1fSFam Zheng low = get_compressed(filename, &high); 5744a1d5e1fSFam Zheng if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR) { 5754a1d5e1fSFam Zheng return (((int64_t) high) << 32) + low; 5764a1d5e1fSFam Zheng } 5774a1d5e1fSFam Zheng } 5784a1d5e1fSFam Zheng 5794a1d5e1fSFam Zheng if (_stati64(filename, &st) < 0) { 5804a1d5e1fSFam Zheng return -1; 5814a1d5e1fSFam Zheng } 5824a1d5e1fSFam Zheng return st.st_size; 5834a1d5e1fSFam Zheng } 5844a1d5e1fSFam Zheng 5853766ef57SKevin Wolf static int raw_co_create(BlockdevCreateOptions *options, Error **errp) 5863766ef57SKevin Wolf { 5873766ef57SKevin Wolf BlockdevCreateOptionsFile *file_opts; 5883766ef57SKevin Wolf int fd; 5893766ef57SKevin Wolf 5903766ef57SKevin Wolf assert(options->driver == BLOCKDEV_DRIVER_FILE); 5913766ef57SKevin Wolf file_opts = &options->u.file; 5923766ef57SKevin Wolf 5933766ef57SKevin Wolf if (file_opts->has_preallocation) { 5943766ef57SKevin Wolf error_setg(errp, "Preallocation is not supported on Windows"); 5953766ef57SKevin Wolf return -EINVAL; 5963766ef57SKevin Wolf } 5973766ef57SKevin Wolf if (file_opts->has_nocow) { 5983766ef57SKevin Wolf error_setg(errp, "nocow is not supported on Windows"); 5993766ef57SKevin Wolf return -EINVAL; 6003766ef57SKevin Wolf } 6013766ef57SKevin Wolf 602b18a24a9SDaniel P. Berrangé fd = qemu_create(file_opts->filename, O_WRONLY | O_TRUNC | O_BINARY, 603b18a24a9SDaniel P. Berrangé 0644, errp); 6043766ef57SKevin Wolf if (fd < 0) { 6053766ef57SKevin Wolf return -EIO; 6063766ef57SKevin Wolf } 6073766ef57SKevin Wolf set_sparse(fd); 6083766ef57SKevin Wolf ftruncate(fd, file_opts->size); 6093766ef57SKevin Wolf qemu_close(fd); 6103766ef57SKevin Wolf 6113766ef57SKevin Wolf return 0; 6123766ef57SKevin Wolf } 6133766ef57SKevin Wolf 6144ec8df01SKevin Wolf static int coroutine_fn GRAPH_RDLOCK 6154ec8df01SKevin Wolf raw_co_create_opts(BlockDriver *drv, const char *filename, 6164ec8df01SKevin Wolf QemuOpts *opts, Error **errp) 617223d4670Sths { 6183766ef57SKevin Wolf BlockdevCreateOptions options; 6190e7e1989SKevin Wolf int64_t total_size = 0; 620223d4670Sths 621d5546c5eSMax Reitz strstart(filename, "file:", &filename); 622d5546c5eSMax Reitz 6230e7e1989SKevin Wolf /* Read out options */ 624180e9526SHu Tao total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), 625c2eb918eSHu Tao BDRV_SECTOR_SIZE); 626223d4670Sths 6273766ef57SKevin Wolf options = (BlockdevCreateOptions) { 6283766ef57SKevin Wolf .driver = BLOCKDEV_DRIVER_FILE, 6293766ef57SKevin Wolf .u.file = { 6303766ef57SKevin Wolf .filename = (char *) filename, 6313766ef57SKevin Wolf .size = total_size, 6323766ef57SKevin Wolf .has_preallocation = false, 6333766ef57SKevin Wolf .has_nocow = false, 6343766ef57SKevin Wolf }, 6353766ef57SKevin Wolf }; 6363766ef57SKevin Wolf return raw_co_create(&options, errp); 637c6252b7cSMax Reitz } 638ddef7699SChunyan Liu 639ebd979c7SViktor Prutyanov static int raw_reopen_prepare(BDRVReopenState *state, 640ebd979c7SViktor Prutyanov BlockReopenQueue *queue, Error **errp) 641ebd979c7SViktor Prutyanov { 642ebd979c7SViktor Prutyanov BDRVRawState *s = state->bs->opaque; 643ebd979c7SViktor Prutyanov BDRVRawReopenState *rs; 644ebd979c7SViktor Prutyanov int access_flags; 645ebd979c7SViktor Prutyanov DWORD overlapped; 646ebd979c7SViktor Prutyanov int ret = 0; 647ebd979c7SViktor Prutyanov 648ebd979c7SViktor Prutyanov if (s->type != FTYPE_FILE) { 649ebd979c7SViktor Prutyanov error_setg(errp, "Can only reopen files"); 650ebd979c7SViktor Prutyanov return -EINVAL; 651ebd979c7SViktor Prutyanov } 652ebd979c7SViktor Prutyanov 653ebd979c7SViktor Prutyanov rs = g_new0(BDRVRawReopenState, 1); 654ebd979c7SViktor Prutyanov 655ebd979c7SViktor Prutyanov /* 656ebd979c7SViktor Prutyanov * We do not support changing any options (only flags). By leaving 657ebd979c7SViktor Prutyanov * all options in state->options, we tell the generic reopen code 658ebd979c7SViktor Prutyanov * that we do not support changing any of them, so it will verify 659ebd979c7SViktor Prutyanov * that their values did not change. 660ebd979c7SViktor Prutyanov */ 661ebd979c7SViktor Prutyanov 662ebd979c7SViktor Prutyanov raw_parse_flags(state->flags, s->aio != NULL, &access_flags, &overlapped); 663ebd979c7SViktor Prutyanov rs->hfile = CreateFile(state->bs->filename, access_flags, 664ebd979c7SViktor Prutyanov FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, 665ebd979c7SViktor Prutyanov OPEN_EXISTING, overlapped, NULL); 666ebd979c7SViktor Prutyanov 667ebd979c7SViktor Prutyanov if (rs->hfile == INVALID_HANDLE_VALUE) { 668ebd979c7SViktor Prutyanov int err = GetLastError(); 669ebd979c7SViktor Prutyanov 670ebd979c7SViktor Prutyanov error_setg_win32(errp, err, "Could not reopen '%s'", 671ebd979c7SViktor Prutyanov state->bs->filename); 672ebd979c7SViktor Prutyanov if (err == ERROR_ACCESS_DENIED) { 673ebd979c7SViktor Prutyanov ret = -EACCES; 674ebd979c7SViktor Prutyanov } else { 675ebd979c7SViktor Prutyanov ret = -EINVAL; 676ebd979c7SViktor Prutyanov } 677ebd979c7SViktor Prutyanov goto fail; 678ebd979c7SViktor Prutyanov } 679ebd979c7SViktor Prutyanov 680ebd979c7SViktor Prutyanov if (s->aio) { 681ebd979c7SViktor Prutyanov ret = win32_aio_attach(s->aio, rs->hfile); 682ebd979c7SViktor Prutyanov if (ret < 0) { 683ebd979c7SViktor Prutyanov error_setg_errno(errp, -ret, "Could not enable AIO"); 684ebd979c7SViktor Prutyanov CloseHandle(rs->hfile); 685ebd979c7SViktor Prutyanov goto fail; 686ebd979c7SViktor Prutyanov } 687ebd979c7SViktor Prutyanov } 688ebd979c7SViktor Prutyanov 689ebd979c7SViktor Prutyanov state->opaque = rs; 690ebd979c7SViktor Prutyanov 691ebd979c7SViktor Prutyanov return 0; 692ebd979c7SViktor Prutyanov 693ebd979c7SViktor Prutyanov fail: 694ebd979c7SViktor Prutyanov g_free(rs); 695ebd979c7SViktor Prutyanov state->opaque = NULL; 696ebd979c7SViktor Prutyanov 697ebd979c7SViktor Prutyanov return ret; 698ebd979c7SViktor Prutyanov } 699ebd979c7SViktor Prutyanov 700ebd979c7SViktor Prutyanov static void raw_reopen_commit(BDRVReopenState *state) 701ebd979c7SViktor Prutyanov { 702ebd979c7SViktor Prutyanov BDRVRawState *s = state->bs->opaque; 703ebd979c7SViktor Prutyanov BDRVRawReopenState *rs = state->opaque; 704ebd979c7SViktor Prutyanov 705ebd979c7SViktor Prutyanov assert(rs != NULL); 706ebd979c7SViktor Prutyanov 707ebd979c7SViktor Prutyanov CloseHandle(s->hfile); 708ebd979c7SViktor Prutyanov s->hfile = rs->hfile; 709ebd979c7SViktor Prutyanov 710ebd979c7SViktor Prutyanov g_free(rs); 711ebd979c7SViktor Prutyanov state->opaque = NULL; 712ebd979c7SViktor Prutyanov } 713ebd979c7SViktor Prutyanov 714ebd979c7SViktor Prutyanov static void raw_reopen_abort(BDRVReopenState *state) 715ebd979c7SViktor Prutyanov { 716ebd979c7SViktor Prutyanov BDRVRawReopenState *rs = state->opaque; 717ebd979c7SViktor Prutyanov 718ebd979c7SViktor Prutyanov if (!rs) { 719ebd979c7SViktor Prutyanov return; 720ebd979c7SViktor Prutyanov } 721ebd979c7SViktor Prutyanov 722ebd979c7SViktor Prutyanov if (rs->hfile != INVALID_HANDLE_VALUE) { 723ebd979c7SViktor Prutyanov CloseHandle(rs->hfile); 724ebd979c7SViktor Prutyanov } 725ebd979c7SViktor Prutyanov 726ebd979c7SViktor Prutyanov g_free(rs); 727ebd979c7SViktor Prutyanov state->opaque = NULL; 728ebd979c7SViktor Prutyanov } 729ebd979c7SViktor Prutyanov 730ddef7699SChunyan Liu static QemuOptsList raw_create_opts = { 731ddef7699SChunyan Liu .name = "raw-create-opts", 732ddef7699SChunyan Liu .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head), 733ddef7699SChunyan Liu .desc = { 734db08adf5SKevin Wolf { 735db08adf5SKevin Wolf .name = BLOCK_OPT_SIZE, 736ddef7699SChunyan Liu .type = QEMU_OPT_SIZE, 737db08adf5SKevin Wolf .help = "Virtual disk size" 738db08adf5SKevin Wolf }, 739ddef7699SChunyan Liu { /* end of list */ } 740ddef7699SChunyan Liu } 7410e7e1989SKevin Wolf }; 7420e7e1989SKevin Wolf 7435f535a94SMax Reitz BlockDriver bdrv_file = { 74484a12e66SChristoph Hellwig .format_name = "file", 74584a12e66SChristoph Hellwig .protocol_name = "file", 746f1b2f712Saliguori .instance_size = sizeof(BDRVRawState), 747030be321SBenoît Canet .bdrv_needs_filename = true, 7487dc74db8SMax Reitz .bdrv_parse_filename = raw_parse_filename, 74944b424dcSPaolo Bonzini .bdrv_open = raw_open, 7502914a1deSEric Blake .bdrv_refresh_limits = raw_probe_alignment, 751f1b2f712Saliguori .bdrv_close = raw_close, 752efc75e2aSStefan Hajnoczi .bdrv_co_create_opts = raw_co_create_opts, 7533ac21627SPeter Lieven .bdrv_has_zero_init = bdrv_has_zero_init_1, 754c68b89acSKevin Wolf 755ebd979c7SViktor Prutyanov .bdrv_reopen_prepare = raw_reopen_prepare, 756ebd979c7SViktor Prutyanov .bdrv_reopen_commit = raw_reopen_commit, 757ebd979c7SViktor Prutyanov .bdrv_reopen_abort = raw_reopen_abort, 758ebd979c7SViktor Prutyanov 759de7056a3SEric Blake .bdrv_aio_preadv = raw_aio_preadv, 760de7056a3SEric Blake .bdrv_aio_pwritev = raw_aio_pwritev, 761fc4edb84SPaolo Bonzini .bdrv_aio_flush = raw_aio_flush, 762c68b89acSKevin Wolf 763061ca8a3SKevin Wolf .bdrv_co_truncate = raw_co_truncate, 764c86422c5SEmanuele Giuseppe Esposito .bdrv_co_getlength = raw_co_getlength, 76582618d7bSEmanuele Giuseppe Esposito .bdrv_co_get_allocated_file_size 76682618d7bSEmanuele Giuseppe Esposito = raw_co_get_allocated_file_size, 7670e7e1989SKevin Wolf 768ddef7699SChunyan Liu .create_opts = &raw_create_opts, 769223d4670Sths }; 770223d4670Sths 771223d4670Sths /***********************************************/ 772223d4670Sths /* host device */ 773223d4670Sths 774223d4670Sths static int find_cdrom(char *cdrom_name, int cdrom_name_size) 775223d4670Sths { 776223d4670Sths char drives[256], *pdrv = drives; 777223d4670Sths UINT type; 778223d4670Sths 779223d4670Sths memset(drives, 0, sizeof(drives)); 780223d4670Sths GetLogicalDriveStrings(sizeof(drives), drives); 781223d4670Sths while(pdrv[0] != '\0') { 782223d4670Sths type = GetDriveType(pdrv); 783223d4670Sths switch(type) { 784223d4670Sths case DRIVE_CDROM: 785223d4670Sths snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]); 786223d4670Sths return 0; 787223d4670Sths break; 788223d4670Sths } 789223d4670Sths pdrv += lstrlen(pdrv) + 1; 790223d4670Sths } 791223d4670Sths return -1; 792223d4670Sths } 793223d4670Sths 794223d4670Sths static int find_device_type(BlockDriverState *bs, const char *filename) 795223d4670Sths { 796223d4670Sths BDRVRawState *s = bs->opaque; 797223d4670Sths UINT type; 798223d4670Sths const char *p; 799223d4670Sths 800223d4670Sths if (strstart(filename, "\\\\.\\", &p) || 801223d4670Sths strstart(filename, "//./", &p)) { 802223d4670Sths if (stristart(p, "PhysicalDrive", NULL)) 803223d4670Sths return FTYPE_HARDDISK; 804223d4670Sths snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]); 805223d4670Sths type = GetDriveType(s->drive_path); 8063060cd14Saliguori switch (type) { 8073060cd14Saliguori case DRIVE_REMOVABLE: 8083060cd14Saliguori case DRIVE_FIXED: 8093060cd14Saliguori return FTYPE_HARDDISK; 8103060cd14Saliguori case DRIVE_CDROM: 811223d4670Sths return FTYPE_CD; 8123060cd14Saliguori default: 813223d4670Sths return FTYPE_FILE; 8143060cd14Saliguori } 815223d4670Sths } else { 816223d4670Sths return FTYPE_FILE; 817223d4670Sths } 818223d4670Sths } 819223d4670Sths 820508c7cb3SChristoph Hellwig static int hdev_probe_device(const char *filename) 821508c7cb3SChristoph Hellwig { 822508c7cb3SChristoph Hellwig if (strstart(filename, "/dev/cdrom", NULL)) 823508c7cb3SChristoph Hellwig return 100; 824508c7cb3SChristoph Hellwig if (is_windows_drive(filename)) 825508c7cb3SChristoph Hellwig return 100; 826508c7cb3SChristoph Hellwig return 0; 827508c7cb3SChristoph Hellwig } 828508c7cb3SChristoph Hellwig 82957ed25b1SMax Reitz static void hdev_parse_filename(const char *filename, QDict *options, 83057ed25b1SMax Reitz Error **errp) 83157ed25b1SMax Reitz { 83203c320d8SMax Reitz bdrv_parse_filename_strip_prefix(filename, "host_device:", options); 83357ed25b1SMax Reitz } 83457ed25b1SMax Reitz 835de7056a3SEric Blake static void hdev_refresh_limits(BlockDriverState *bs, Error **errp) 836de7056a3SEric Blake { 837de7056a3SEric Blake /* XXX Does Windows support AIO on less than 512-byte alignment? */ 838de7056a3SEric Blake bs->bl.request_alignment = 512; 8398c6f27e7SPaolo Bonzini bs->bl.has_variable_length = true; 840de7056a3SEric Blake } 841de7056a3SEric Blake 842015a1036SMax Reitz static int hdev_open(BlockDriverState *bs, QDict *options, int flags, 843015a1036SMax Reitz Error **errp) 844223d4670Sths { 845223d4670Sths BDRVRawState *s = bs->opaque; 846223d4670Sths int access_flags, create_flags; 84768dc0364SStefan Weil int ret = 0; 848223d4670Sths DWORD overlapped; 849223d4670Sths char device_name[64]; 85068dc0364SStefan Weil 85168dc0364SStefan Weil Error *local_err = NULL; 85268dc0364SStefan Weil const char *filename; 8530a4279d9SKevin Wolf bool use_aio; 85468dc0364SStefan Weil 85587ea75d5SPeter Crosthwaite QemuOpts *opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, 85687ea75d5SPeter Crosthwaite &error_abort); 857af175e85SMarkus Armbruster if (!qemu_opts_absorb_qdict(opts, options, errp)) { 85868dc0364SStefan Weil ret = -EINVAL; 85968dc0364SStefan Weil goto done; 86068dc0364SStefan Weil } 86168dc0364SStefan Weil 86268dc0364SStefan Weil filename = qemu_opt_get(opts, "filename"); 863223d4670Sths 8640a4279d9SKevin Wolf use_aio = get_aio_option(opts, flags, &local_err); 8650a4279d9SKevin Wolf if (!local_err && use_aio) { 8660a4279d9SKevin Wolf error_setg(&local_err, "AIO is not supported on Windows host devices"); 8670a4279d9SKevin Wolf } 8680a4279d9SKevin Wolf if (local_err) { 8690a4279d9SKevin Wolf error_propagate(errp, local_err); 8700a4279d9SKevin Wolf ret = -EINVAL; 8710a4279d9SKevin Wolf goto done; 8720a4279d9SKevin Wolf } 8730a4279d9SKevin Wolf 874223d4670Sths if (strstart(filename, "/dev/cdrom", NULL)) { 87568dc0364SStefan Weil if (find_cdrom(device_name, sizeof(device_name)) < 0) { 876c6252b7cSMax Reitz error_setg(errp, "Could not open CD-ROM drive"); 87768dc0364SStefan Weil ret = -ENOENT; 87868dc0364SStefan Weil goto done; 87968dc0364SStefan Weil } 880223d4670Sths filename = device_name; 881223d4670Sths } else { 882223d4670Sths /* transform drive letters into device name */ 883223d4670Sths if (((filename[0] >= 'a' && filename[0] <= 'z') || 884223d4670Sths (filename[0] >= 'A' && filename[0] <= 'Z')) && 885223d4670Sths filename[1] == ':' && filename[2] == '\0') { 886223d4670Sths snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]); 887223d4670Sths filename = device_name; 888223d4670Sths } 889223d4670Sths } 890223d4670Sths s->type = find_device_type(bs, filename); 891223d4670Sths 8920a4279d9SKevin Wolf raw_parse_flags(flags, use_aio, &access_flags, &overlapped); 8936a8dc042SJeff Cody 894223d4670Sths create_flags = OPEN_EXISTING; 895223d4670Sths 896223d4670Sths s->hfile = CreateFile(filename, access_flags, 897223d4670Sths FILE_SHARE_READ, NULL, 898223d4670Sths create_flags, overlapped, NULL); 899223d4670Sths if (s->hfile == INVALID_HANDLE_VALUE) { 900223d4670Sths int err = GetLastError(); 901223d4670Sths 90268dc0364SStefan Weil if (err == ERROR_ACCESS_DENIED) { 90368dc0364SStefan Weil ret = -EACCES; 90468dc0364SStefan Weil } else { 90545d57f6eSMax Reitz ret = -EINVAL; 906223d4670Sths } 90745d57f6eSMax Reitz error_setg_errno(errp, -ret, "Could not open device"); 90868dc0364SStefan Weil goto done; 90968dc0364SStefan Weil } 91068dc0364SStefan Weil 91168dc0364SStefan Weil done: 91268dc0364SStefan Weil qemu_opts_del(opts); 91368dc0364SStefan Weil return ret; 914223d4670Sths } 915223d4670Sths 9165efa9d5aSAnthony Liguori static BlockDriver bdrv_host_device = { 917e60f469cSaurel32 .format_name = "host_device", 91884a12e66SChristoph Hellwig .protocol_name = "host_device", 919e60f469cSaurel32 .instance_size = sizeof(BDRVRawState), 920030be321SBenoît Canet .bdrv_needs_filename = true, 92157ed25b1SMax Reitz .bdrv_parse_filename = hdev_parse_filename, 922508c7cb3SChristoph Hellwig .bdrv_probe_device = hdev_probe_device, 92344b424dcSPaolo Bonzini .bdrv_open = hdev_open, 924e60f469cSaurel32 .bdrv_close = raw_close, 925de7056a3SEric Blake .bdrv_refresh_limits = hdev_refresh_limits, 926223d4670Sths 927de7056a3SEric Blake .bdrv_aio_preadv = raw_aio_preadv, 928de7056a3SEric Blake .bdrv_aio_pwritev = raw_aio_pwritev, 929fc4edb84SPaolo Bonzini .bdrv_aio_flush = raw_aio_flush, 930c68b89acSKevin Wolf 93185ebd381SStefan Hajnoczi .bdrv_detach_aio_context = raw_detach_aio_context, 93285ebd381SStefan Hajnoczi .bdrv_attach_aio_context = raw_attach_aio_context, 93385ebd381SStefan Hajnoczi 934c86422c5SEmanuele Giuseppe Esposito .bdrv_co_getlength = raw_co_getlength, 93582618d7bSEmanuele Giuseppe Esposito .bdrv_co_get_allocated_file_size = raw_co_get_allocated_file_size, 936223d4670Sths }; 9375efa9d5aSAnthony Liguori 93884a12e66SChristoph Hellwig static void bdrv_file_init(void) 9395efa9d5aSAnthony Liguori { 94084a12e66SChristoph Hellwig bdrv_register(&bdrv_file); 9415efa9d5aSAnthony Liguori bdrv_register(&bdrv_host_device); 9425efa9d5aSAnthony Liguori } 9435efa9d5aSAnthony Liguori 94484a12e66SChristoph Hellwig block_init(bdrv_file_init); 945