ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Block driver for RAW files (win32) |
| 3 | * |
| 4 | * Copyright (c) 2006 Fabrice Bellard |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
Peter Maydell | 80c71a2 | 2016-01-18 18:01:42 +0000 | [diff] [blame] | 24 | #include "qemu/osdep.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 25 | #include "qapi/error.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 26 | #include "qemu/cutils.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 27 | #include "block/block_int.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 28 | #include "qemu/module.h" |
Paolo Bonzini | 0187f5c | 2016-07-04 18:33:20 +0200 | [diff] [blame] | 29 | #include "block/raw-aio.h" |
Paolo Bonzini | fc4edb8 | 2012-06-09 04:48:28 +0200 | [diff] [blame] | 30 | #include "trace.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 31 | #include "block/thread-pool.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 32 | #include "qemu/iov.h" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 33 | #include "qapi/qmp/qstring.h" |
Kevin Wolf | 0a4279d | 2016-09-08 15:09:01 +0200 | [diff] [blame] | 34 | #include "qapi/util.h" |
aliguori | 49dc768 | 2009-03-08 16:26:59 +0000 | [diff] [blame] | 35 | #include <windows.h> |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 36 | #include <winioctl.h> |
| 37 | |
| 38 | #define FTYPE_FILE 0 |
| 39 | #define FTYPE_CD 1 |
| 40 | #define FTYPE_HARDDISK 2 |
| 41 | |
Paolo Bonzini | fc4edb8 | 2012-06-09 04:48:28 +0200 | [diff] [blame] | 42 | typedef struct RawWin32AIOData { |
| 43 | BlockDriverState *bs; |
| 44 | HANDLE hfile; |
| 45 | struct iovec *aio_iov; |
| 46 | int aio_niov; |
| 47 | size_t aio_nbytes; |
| 48 | off64_t aio_offset; |
| 49 | int aio_type; |
| 50 | } RawWin32AIOData; |
| 51 | |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 52 | typedef struct BDRVRawState { |
| 53 | HANDLE hfile; |
| 54 | int type; |
| 55 | char drive_path[16]; /* format: "d:\" */ |
Paolo Bonzini | a273652 | 2012-10-26 11:43:58 +0200 | [diff] [blame] | 56 | QEMUWin32AIOState *aio; |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 57 | } BDRVRawState; |
| 58 | |
Paolo Bonzini | fc4edb8 | 2012-06-09 04:48:28 +0200 | [diff] [blame] | 59 | /* |
| 60 | * Read/writes the data to/from a given linear buffer. |
| 61 | * |
| 62 | * Returns the number of bytes handles or -errno in case of an error. Short |
| 63 | * reads are only returned if the end of the file is reached. |
| 64 | */ |
| 65 | static size_t handle_aiocb_rw(RawWin32AIOData *aiocb) |
| 66 | { |
| 67 | size_t offset = 0; |
| 68 | int i; |
| 69 | |
| 70 | for (i = 0; i < aiocb->aio_niov; i++) { |
| 71 | OVERLAPPED ov; |
| 72 | DWORD ret, ret_count, len; |
| 73 | |
| 74 | memset(&ov, 0, sizeof(ov)); |
| 75 | ov.Offset = (aiocb->aio_offset + offset); |
| 76 | ov.OffsetHigh = (aiocb->aio_offset + offset) >> 32; |
| 77 | len = aiocb->aio_iov[i].iov_len; |
| 78 | if (aiocb->aio_type & QEMU_AIO_WRITE) { |
| 79 | ret = WriteFile(aiocb->hfile, aiocb->aio_iov[i].iov_base, |
| 80 | len, &ret_count, &ov); |
| 81 | } else { |
| 82 | ret = ReadFile(aiocb->hfile, aiocb->aio_iov[i].iov_base, |
| 83 | len, &ret_count, &ov); |
| 84 | } |
| 85 | if (!ret) { |
| 86 | ret_count = 0; |
| 87 | } |
| 88 | if (ret_count != len) { |
Tal Kain | 56e023a | 2013-09-09 11:14:55 +0200 | [diff] [blame] | 89 | offset += ret_count; |
Paolo Bonzini | fc4edb8 | 2012-06-09 04:48:28 +0200 | [diff] [blame] | 90 | break; |
| 91 | } |
| 92 | offset += len; |
| 93 | } |
| 94 | |
| 95 | return offset; |
| 96 | } |
| 97 | |
| 98 | static int aio_worker(void *arg) |
| 99 | { |
| 100 | RawWin32AIOData *aiocb = arg; |
| 101 | ssize_t ret = 0; |
| 102 | size_t count; |
| 103 | |
| 104 | switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) { |
| 105 | case QEMU_AIO_READ: |
| 106 | count = handle_aiocb_rw(aiocb); |
Max Reitz | c0191e7 | 2015-02-05 13:58:24 -0500 | [diff] [blame] | 107 | if (count < aiocb->aio_nbytes) { |
Paolo Bonzini | fc4edb8 | 2012-06-09 04:48:28 +0200 | [diff] [blame] | 108 | /* A short read means that we have reached EOF. Pad the buffer |
| 109 | * with zeros for bytes after EOF. */ |
| 110 | iov_memset(aiocb->aio_iov, aiocb->aio_niov, count, |
| 111 | 0, aiocb->aio_nbytes - count); |
| 112 | |
| 113 | count = aiocb->aio_nbytes; |
| 114 | } |
| 115 | if (count == aiocb->aio_nbytes) { |
| 116 | ret = 0; |
| 117 | } else { |
| 118 | ret = -EINVAL; |
| 119 | } |
| 120 | break; |
| 121 | case QEMU_AIO_WRITE: |
| 122 | count = handle_aiocb_rw(aiocb); |
| 123 | if (count == aiocb->aio_nbytes) { |
Kevin Wolf | 5d55503 | 2015-09-23 14:58:21 +0200 | [diff] [blame] | 124 | ret = 0; |
Paolo Bonzini | fc4edb8 | 2012-06-09 04:48:28 +0200 | [diff] [blame] | 125 | } else { |
Kevin Wolf | 5d55503 | 2015-09-23 14:58:21 +0200 | [diff] [blame] | 126 | ret = -EINVAL; |
Paolo Bonzini | fc4edb8 | 2012-06-09 04:48:28 +0200 | [diff] [blame] | 127 | } |
| 128 | break; |
| 129 | case QEMU_AIO_FLUSH: |
| 130 | if (!FlushFileBuffers(aiocb->hfile)) { |
| 131 | return -EIO; |
| 132 | } |
| 133 | break; |
| 134 | default: |
| 135 | fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type); |
| 136 | ret = -EINVAL; |
| 137 | break; |
| 138 | } |
| 139 | |
Paolo Bonzini | c84b319 | 2015-10-01 13:04:39 +0200 | [diff] [blame] | 140 | g_free(aiocb); |
Paolo Bonzini | fc4edb8 | 2012-06-09 04:48:28 +0200 | [diff] [blame] | 141 | return ret; |
| 142 | } |
| 143 | |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 144 | static BlockAIOCB *paio_submit(BlockDriverState *bs, HANDLE hfile, |
Eric Blake | 36e3b2e | 2016-07-15 17:22:55 -0600 | [diff] [blame] | 145 | int64_t offset, QEMUIOVector *qiov, int count, |
Markus Armbruster | 097310b | 2014-10-07 13:59:15 +0200 | [diff] [blame] | 146 | BlockCompletionFunc *cb, void *opaque, int type) |
Paolo Bonzini | fc4edb8 | 2012-06-09 04:48:28 +0200 | [diff] [blame] | 147 | { |
Paolo Bonzini | c84b319 | 2015-10-01 13:04:39 +0200 | [diff] [blame] | 148 | RawWin32AIOData *acb = g_new(RawWin32AIOData, 1); |
Stefan Hajnoczi | c4d9d19 | 2013-03-07 13:41:49 +0100 | [diff] [blame] | 149 | ThreadPool *pool; |
Paolo Bonzini | fc4edb8 | 2012-06-09 04:48:28 +0200 | [diff] [blame] | 150 | |
| 151 | acb->bs = bs; |
| 152 | acb->hfile = hfile; |
| 153 | acb->aio_type = type; |
| 154 | |
| 155 | if (qiov) { |
| 156 | acb->aio_iov = qiov->iov; |
| 157 | acb->aio_niov = qiov->niov; |
Eric Blake | 36e3b2e | 2016-07-15 17:22:55 -0600 | [diff] [blame] | 158 | assert(qiov->size == count); |
Paolo Bonzini | fc4edb8 | 2012-06-09 04:48:28 +0200 | [diff] [blame] | 159 | } |
Eric Blake | 36e3b2e | 2016-07-15 17:22:55 -0600 | [diff] [blame] | 160 | acb->aio_nbytes = count; |
| 161 | acb->aio_offset = offset; |
Paolo Bonzini | fc4edb8 | 2012-06-09 04:48:28 +0200 | [diff] [blame] | 162 | |
Eric Blake | 36e3b2e | 2016-07-15 17:22:55 -0600 | [diff] [blame] | 163 | trace_paio_submit(acb, opaque, offset, count, type); |
Stefan Hajnoczi | c4d9d19 | 2013-03-07 13:41:49 +0100 | [diff] [blame] | 164 | pool = aio_get_thread_pool(bdrv_get_aio_context(bs)); |
| 165 | return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque); |
Paolo Bonzini | fc4edb8 | 2012-06-09 04:48:28 +0200 | [diff] [blame] | 166 | } |
| 167 | |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 168 | int qemu_ftruncate64(int fd, int64_t length) |
| 169 | { |
| 170 | LARGE_INTEGER li; |
Stefan Weil | 2c993ec | 2011-08-20 12:10:05 +0200 | [diff] [blame] | 171 | DWORD dw; |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 172 | LONG high; |
| 173 | HANDLE h; |
| 174 | BOOL res; |
| 175 | |
| 176 | if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0) |
| 177 | return -1; |
| 178 | |
| 179 | h = (HANDLE)_get_osfhandle(fd); |
| 180 | |
| 181 | /* get current position, ftruncate do not change position */ |
| 182 | li.HighPart = 0; |
| 183 | li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT); |
Stefan Weil | 2c993ec | 2011-08-20 12:10:05 +0200 | [diff] [blame] | 184 | if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) { |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 185 | return -1; |
Stefan Weil | 2c993ec | 2011-08-20 12:10:05 +0200 | [diff] [blame] | 186 | } |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 187 | |
| 188 | high = length >> 32; |
Stefan Weil | 2c993ec | 2011-08-20 12:10:05 +0200 | [diff] [blame] | 189 | dw = SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN); |
| 190 | if (dw == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) { |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 191 | return -1; |
Stefan Weil | 2c993ec | 2011-08-20 12:10:05 +0200 | [diff] [blame] | 192 | } |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 193 | res = SetEndOfFile(h); |
| 194 | |
| 195 | /* back to old position */ |
| 196 | SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN); |
| 197 | return res ? 0 : -1; |
| 198 | } |
| 199 | |
| 200 | static int set_sparse(int fd) |
| 201 | { |
| 202 | DWORD returned; |
| 203 | return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE, |
| 204 | NULL, 0, NULL, 0, &returned, NULL); |
| 205 | } |
| 206 | |
Stefan Hajnoczi | 85ebd38 | 2014-05-08 16:34:50 +0200 | [diff] [blame] | 207 | static void raw_detach_aio_context(BlockDriverState *bs) |
| 208 | { |
| 209 | BDRVRawState *s = bs->opaque; |
| 210 | |
| 211 | if (s->aio) { |
| 212 | win32_aio_detach_aio_context(s->aio, bdrv_get_aio_context(bs)); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | static void raw_attach_aio_context(BlockDriverState *bs, |
| 217 | AioContext *new_context) |
| 218 | { |
| 219 | BDRVRawState *s = bs->opaque; |
| 220 | |
| 221 | if (s->aio) { |
| 222 | win32_aio_attach_aio_context(s->aio, new_context); |
| 223 | } |
| 224 | } |
| 225 | |
Eric Blake | 2914a1d | 2016-06-23 16:37:16 -0600 | [diff] [blame] | 226 | static void raw_probe_alignment(BlockDriverState *bs, Error **errp) |
Paolo Bonzini | c25f53b | 2011-11-29 12:42:20 +0100 | [diff] [blame] | 227 | { |
| 228 | BDRVRawState *s = bs->opaque; |
| 229 | DWORD sectorsPerCluster, freeClusters, totalClusters, count; |
| 230 | DISK_GEOMETRY_EX dg; |
| 231 | BOOL status; |
| 232 | |
| 233 | if (s->type == FTYPE_CD) { |
Eric Blake | a5b8dd2 | 2016-06-23 16:37:24 -0600 | [diff] [blame] | 234 | bs->bl.request_alignment = 2048; |
Paolo Bonzini | c25f53b | 2011-11-29 12:42:20 +0100 | [diff] [blame] | 235 | return; |
| 236 | } |
| 237 | if (s->type == FTYPE_HARDDISK) { |
| 238 | status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, |
| 239 | NULL, 0, &dg, sizeof(dg), &count, NULL); |
| 240 | if (status != 0) { |
Eric Blake | a5b8dd2 | 2016-06-23 16:37:24 -0600 | [diff] [blame] | 241 | bs->bl.request_alignment = dg.Geometry.BytesPerSector; |
Paolo Bonzini | c25f53b | 2011-11-29 12:42:20 +0100 | [diff] [blame] | 242 | return; |
| 243 | } |
| 244 | /* try GetDiskFreeSpace too */ |
| 245 | } |
| 246 | |
| 247 | if (s->drive_path[0]) { |
| 248 | GetDiskFreeSpace(s->drive_path, §orsPerCluster, |
| 249 | &dg.Geometry.BytesPerSector, |
| 250 | &freeClusters, &totalClusters); |
Eric Blake | a5b8dd2 | 2016-06-23 16:37:24 -0600 | [diff] [blame] | 251 | bs->bl.request_alignment = dg.Geometry.BytesPerSector; |
Paolo Bonzini | c25f53b | 2011-11-29 12:42:20 +0100 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
Kevin Wolf | 0a4279d | 2016-09-08 15:09:01 +0200 | [diff] [blame] | 255 | static void raw_parse_flags(int flags, bool use_aio, int *access_flags, |
| 256 | DWORD *overlapped) |
Jeff Cody | 6a8dc04 | 2012-09-20 15:13:21 -0400 | [diff] [blame] | 257 | { |
| 258 | assert(access_flags != NULL); |
| 259 | assert(overlapped != NULL); |
| 260 | |
| 261 | if (flags & BDRV_O_RDWR) { |
| 262 | *access_flags = GENERIC_READ | GENERIC_WRITE; |
| 263 | } else { |
| 264 | *access_flags = GENERIC_READ; |
| 265 | } |
| 266 | |
| 267 | *overlapped = FILE_ATTRIBUTE_NORMAL; |
Kevin Wolf | 0a4279d | 2016-09-08 15:09:01 +0200 | [diff] [blame] | 268 | if (use_aio) { |
Paolo Bonzini | a273652 | 2012-10-26 11:43:58 +0200 | [diff] [blame] | 269 | *overlapped |= FILE_FLAG_OVERLAPPED; |
| 270 | } |
Jeff Cody | 6a8dc04 | 2012-09-20 15:13:21 -0400 | [diff] [blame] | 271 | if (flags & BDRV_O_NOCACHE) { |
| 272 | *overlapped |= FILE_FLAG_NO_BUFFERING; |
| 273 | } |
Jeff Cody | 6a8dc04 | 2012-09-20 15:13:21 -0400 | [diff] [blame] | 274 | } |
| 275 | |
Max Reitz | 7dc74db | 2014-03-05 22:41:39 +0100 | [diff] [blame] | 276 | static void raw_parse_filename(const char *filename, QDict *options, |
| 277 | Error **errp) |
| 278 | { |
Max Reitz | 03c320d | 2017-05-22 21:52:16 +0200 | [diff] [blame] | 279 | bdrv_parse_filename_strip_prefix(filename, "file:", options); |
Max Reitz | 7dc74db | 2014-03-05 22:41:39 +0100 | [diff] [blame] | 280 | } |
| 281 | |
Kevin Wolf | 8a79380 | 2013-04-10 11:34:56 +0200 | [diff] [blame] | 282 | static QemuOptsList raw_runtime_opts = { |
| 283 | .name = "raw", |
| 284 | .head = QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head), |
| 285 | .desc = { |
| 286 | { |
| 287 | .name = "filename", |
| 288 | .type = QEMU_OPT_STRING, |
| 289 | .help = "File name of the image", |
| 290 | }, |
Kevin Wolf | 0a4279d | 2016-09-08 15:09:01 +0200 | [diff] [blame] | 291 | { |
| 292 | .name = "aio", |
| 293 | .type = QEMU_OPT_STRING, |
| 294 | .help = "host AIO implementation (threads, native)", |
| 295 | }, |
Kevin Wolf | 8a79380 | 2013-04-10 11:34:56 +0200 | [diff] [blame] | 296 | { /* end of list */ } |
| 297 | }, |
| 298 | }; |
| 299 | |
Kevin Wolf | 0a4279d | 2016-09-08 15:09:01 +0200 | [diff] [blame] | 300 | static bool get_aio_option(QemuOpts *opts, int flags, Error **errp) |
| 301 | { |
| 302 | BlockdevAioOptions aio, aio_default; |
| 303 | |
| 304 | aio_default = (flags & BDRV_O_NATIVE_AIO) ? BLOCKDEV_AIO_OPTIONS_NATIVE |
| 305 | : BLOCKDEV_AIO_OPTIONS_THREADS; |
| 306 | aio = qapi_enum_parse(BlockdevAioOptions_lookup, qemu_opt_get(opts, "aio"), |
| 307 | BLOCKDEV_AIO_OPTIONS__MAX, aio_default, errp); |
| 308 | |
| 309 | switch (aio) { |
| 310 | case BLOCKDEV_AIO_OPTIONS_NATIVE: |
| 311 | return true; |
| 312 | case BLOCKDEV_AIO_OPTIONS_THREADS: |
| 313 | return false; |
| 314 | default: |
| 315 | error_setg(errp, "Invalid AIO option"); |
| 316 | } |
| 317 | return false; |
| 318 | } |
| 319 | |
Max Reitz | 015a103 | 2013-09-05 14:22:29 +0200 | [diff] [blame] | 320 | static int raw_open(BlockDriverState *bs, QDict *options, int flags, |
| 321 | Error **errp) |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 322 | { |
| 323 | BDRVRawState *s = bs->opaque; |
Christoph Hellwig | 9a2d77a | 2010-01-20 18:13:42 +0100 | [diff] [blame] | 324 | int access_flags; |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 325 | DWORD overlapped; |
Kevin Wolf | 8a79380 | 2013-04-10 11:34:56 +0200 | [diff] [blame] | 326 | QemuOpts *opts; |
| 327 | Error *local_err = NULL; |
| 328 | const char *filename; |
Kevin Wolf | 0a4279d | 2016-09-08 15:09:01 +0200 | [diff] [blame] | 329 | bool use_aio; |
Kevin Wolf | 8a79380 | 2013-04-10 11:34:56 +0200 | [diff] [blame] | 330 | int ret; |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 331 | |
| 332 | s->type = FTYPE_FILE; |
| 333 | |
Peter Crosthwaite | 87ea75d | 2014-01-01 18:49:17 -0800 | [diff] [blame] | 334 | opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort); |
Kevin Wolf | 8a79380 | 2013-04-10 11:34:56 +0200 | [diff] [blame] | 335 | qemu_opts_absorb_qdict(opts, options, &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 336 | if (local_err) { |
Max Reitz | c6252b7 | 2013-10-10 15:44:02 +0200 | [diff] [blame] | 337 | error_propagate(errp, local_err); |
Kevin Wolf | 8a79380 | 2013-04-10 11:34:56 +0200 | [diff] [blame] | 338 | ret = -EINVAL; |
| 339 | goto fail; |
| 340 | } |
| 341 | |
Fam Zheng | 1c3a555 | 2017-05-03 00:35:51 +0800 | [diff] [blame] | 342 | if (qdict_get_try_bool(options, "locking", false)) { |
| 343 | error_setg(errp, "locking=on is not supported on Windows"); |
Gerd Hoffmann | cdece04 | 2017-05-16 09:42:55 +0200 | [diff] [blame] | 344 | ret = -EINVAL; |
Fam Zheng | 1c3a555 | 2017-05-03 00:35:51 +0800 | [diff] [blame] | 345 | goto fail; |
| 346 | } |
| 347 | |
Kevin Wolf | 8a79380 | 2013-04-10 11:34:56 +0200 | [diff] [blame] | 348 | filename = qemu_opt_get(opts, "filename"); |
| 349 | |
Kevin Wolf | 0a4279d | 2016-09-08 15:09:01 +0200 | [diff] [blame] | 350 | use_aio = get_aio_option(opts, flags, &local_err); |
| 351 | if (local_err) { |
| 352 | error_propagate(errp, local_err); |
| 353 | ret = -EINVAL; |
| 354 | goto fail; |
| 355 | } |
| 356 | |
| 357 | raw_parse_flags(flags, use_aio, &access_flags, &overlapped); |
Kevin Wolf | 8a79380 | 2013-04-10 11:34:56 +0200 | [diff] [blame] | 358 | |
Paolo Bonzini | c25f53b | 2011-11-29 12:42:20 +0100 | [diff] [blame] | 359 | if (filename[0] && filename[1] == ':') { |
| 360 | snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", filename[0]); |
| 361 | } else if (filename[0] == '\\' && filename[1] == '\\') { |
| 362 | s->drive_path[0] = 0; |
| 363 | } else { |
| 364 | /* Relative path. */ |
| 365 | char buf[MAX_PATH]; |
| 366 | GetCurrentDirectory(MAX_PATH, buf); |
| 367 | snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", buf[0]); |
| 368 | } |
| 369 | |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 370 | s->hfile = CreateFile(filename, access_flags, |
| 371 | FILE_SHARE_READ, NULL, |
Christoph Hellwig | 9a2d77a | 2010-01-20 18:13:42 +0100 | [diff] [blame] | 372 | OPEN_EXISTING, overlapped, NULL); |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 373 | if (s->hfile == INVALID_HANDLE_VALUE) { |
| 374 | int err = GetLastError(); |
| 375 | |
Halil Pasic | 0923775 | 2016-10-11 16:12:35 +0200 | [diff] [blame] | 376 | error_setg_win32(errp, err, "Could not open '%s'", filename); |
Kevin Wolf | 8a79380 | 2013-04-10 11:34:56 +0200 | [diff] [blame] | 377 | if (err == ERROR_ACCESS_DENIED) { |
| 378 | ret = -EACCES; |
| 379 | } else { |
| 380 | ret = -EINVAL; |
| 381 | } |
| 382 | goto fail; |
Paolo Bonzini | a273652 | 2012-10-26 11:43:58 +0200 | [diff] [blame] | 383 | } |
| 384 | |
Kevin Wolf | 0a4279d | 2016-09-08 15:09:01 +0200 | [diff] [blame] | 385 | if (use_aio) { |
Stefan Hajnoczi | 99cc598 | 2014-05-08 16:34:49 +0200 | [diff] [blame] | 386 | s->aio = win32_aio_init(); |
| 387 | if (s->aio == NULL) { |
| 388 | CloseHandle(s->hfile); |
| 389 | error_setg(errp, "Could not initialize AIO"); |
| 390 | ret = -EINVAL; |
| 391 | goto fail; |
| 392 | } |
| 393 | |
| 394 | ret = win32_aio_attach(s->aio, s->hfile); |
Paolo Bonzini | a273652 | 2012-10-26 11:43:58 +0200 | [diff] [blame] | 395 | if (ret < 0) { |
Stefan Hajnoczi | 99cc598 | 2014-05-08 16:34:49 +0200 | [diff] [blame] | 396 | win32_aio_cleanup(s->aio); |
Paolo Bonzini | a273652 | 2012-10-26 11:43:58 +0200 | [diff] [blame] | 397 | CloseHandle(s->hfile); |
Max Reitz | c6252b7 | 2013-10-10 15:44:02 +0200 | [diff] [blame] | 398 | error_setg_errno(errp, -ret, "Could not enable AIO"); |
Kevin Wolf | 8a79380 | 2013-04-10 11:34:56 +0200 | [diff] [blame] | 399 | goto fail; |
Paolo Bonzini | a273652 | 2012-10-26 11:43:58 +0200 | [diff] [blame] | 400 | } |
Stefan Hajnoczi | 85ebd38 | 2014-05-08 16:34:50 +0200 | [diff] [blame] | 401 | |
| 402 | win32_aio_attach_aio_context(s->aio, bdrv_get_aio_context(bs)); |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 403 | } |
Kevin Wolf | 8a79380 | 2013-04-10 11:34:56 +0200 | [diff] [blame] | 404 | |
| 405 | ret = 0; |
| 406 | fail: |
| 407 | qemu_opts_del(opts); |
| 408 | return ret; |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 411 | static BlockAIOCB *raw_aio_readv(BlockDriverState *bs, |
Paolo Bonzini | fc4edb8 | 2012-06-09 04:48:28 +0200 | [diff] [blame] | 412 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
Markus Armbruster | 097310b | 2014-10-07 13:59:15 +0200 | [diff] [blame] | 413 | BlockCompletionFunc *cb, void *opaque) |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 414 | { |
| 415 | BDRVRawState *s = bs->opaque; |
Paolo Bonzini | a273652 | 2012-10-26 11:43:58 +0200 | [diff] [blame] | 416 | if (s->aio) { |
| 417 | return win32_aio_submit(bs, s->aio, s->hfile, sector_num, qiov, |
Eric Blake | 36e3b2e | 2016-07-15 17:22:55 -0600 | [diff] [blame] | 418 | nb_sectors, cb, opaque, QEMU_AIO_READ); |
Paolo Bonzini | a273652 | 2012-10-26 11:43:58 +0200 | [diff] [blame] | 419 | } else { |
Eric Blake | 36e3b2e | 2016-07-15 17:22:55 -0600 | [diff] [blame] | 420 | return paio_submit(bs, s->hfile, sector_num << BDRV_SECTOR_BITS, qiov, |
| 421 | nb_sectors << BDRV_SECTOR_BITS, |
Paolo Bonzini | a273652 | 2012-10-26 11:43:58 +0200 | [diff] [blame] | 422 | cb, opaque, QEMU_AIO_READ); |
| 423 | } |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 426 | static BlockAIOCB *raw_aio_writev(BlockDriverState *bs, |
Paolo Bonzini | fc4edb8 | 2012-06-09 04:48:28 +0200 | [diff] [blame] | 427 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
Markus Armbruster | 097310b | 2014-10-07 13:59:15 +0200 | [diff] [blame] | 428 | BlockCompletionFunc *cb, void *opaque) |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 429 | { |
| 430 | BDRVRawState *s = bs->opaque; |
Paolo Bonzini | a273652 | 2012-10-26 11:43:58 +0200 | [diff] [blame] | 431 | if (s->aio) { |
| 432 | return win32_aio_submit(bs, s->aio, s->hfile, sector_num, qiov, |
Eric Blake | 36e3b2e | 2016-07-15 17:22:55 -0600 | [diff] [blame] | 433 | nb_sectors, cb, opaque, QEMU_AIO_WRITE); |
Paolo Bonzini | a273652 | 2012-10-26 11:43:58 +0200 | [diff] [blame] | 434 | } else { |
Eric Blake | 36e3b2e | 2016-07-15 17:22:55 -0600 | [diff] [blame] | 435 | return paio_submit(bs, s->hfile, sector_num << BDRV_SECTOR_BITS, qiov, |
| 436 | nb_sectors << BDRV_SECTOR_BITS, |
Paolo Bonzini | a273652 | 2012-10-26 11:43:58 +0200 | [diff] [blame] | 437 | cb, opaque, QEMU_AIO_WRITE); |
| 438 | } |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 441 | static BlockAIOCB *raw_aio_flush(BlockDriverState *bs, |
Markus Armbruster | 097310b | 2014-10-07 13:59:15 +0200 | [diff] [blame] | 442 | BlockCompletionFunc *cb, void *opaque) |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 443 | { |
| 444 | BDRVRawState *s = bs->opaque; |
Paolo Bonzini | fc4edb8 | 2012-06-09 04:48:28 +0200 | [diff] [blame] | 445 | return paio_submit(bs, s->hfile, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH); |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | static void raw_close(BlockDriverState *bs) |
| 449 | { |
| 450 | BDRVRawState *s = bs->opaque; |
Stefan Hajnoczi | 99cc598 | 2014-05-08 16:34:49 +0200 | [diff] [blame] | 451 | |
| 452 | if (s->aio) { |
Stefan Hajnoczi | 85ebd38 | 2014-05-08 16:34:50 +0200 | [diff] [blame] | 453 | win32_aio_detach_aio_context(s->aio, bdrv_get_aio_context(bs)); |
Stefan Hajnoczi | 99cc598 | 2014-05-08 16:34:49 +0200 | [diff] [blame] | 454 | win32_aio_cleanup(s->aio); |
| 455 | s->aio = NULL; |
| 456 | } |
| 457 | |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 458 | CloseHandle(s->hfile); |
Kevin Wolf | 8bfea15 | 2014-04-11 19:16:36 +0200 | [diff] [blame] | 459 | if (bs->open_flags & BDRV_O_TEMPORARY) { |
| 460 | unlink(bs->filename); |
| 461 | } |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Max Reitz | 4bff28b | 2017-03-28 22:51:28 +0200 | [diff] [blame] | 464 | static int raw_truncate(BlockDriverState *bs, int64_t offset, Error **errp) |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 465 | { |
| 466 | BDRVRawState *s = bs->opaque; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 467 | LONG low, high; |
Fabien Chouteau | fbcad04 | 2012-12-10 12:56:22 +0100 | [diff] [blame] | 468 | DWORD dwPtrLow; |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 469 | |
| 470 | low = offset; |
| 471 | high = offset >> 32; |
Fabien Chouteau | fbcad04 | 2012-12-10 12:56:22 +0100 | [diff] [blame] | 472 | |
| 473 | /* |
| 474 | * An error has occurred if the return value is INVALID_SET_FILE_POINTER |
| 475 | * and GetLastError doesn't return NO_ERROR. |
| 476 | */ |
| 477 | dwPtrLow = SetFilePointer(s->hfile, low, &high, FILE_BEGIN); |
| 478 | if (dwPtrLow == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) { |
Max Reitz | 4bff28b | 2017-03-28 22:51:28 +0200 | [diff] [blame] | 479 | error_setg_win32(errp, GetLastError(), "SetFilePointer error"); |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 480 | return -EIO; |
Fabien Chouteau | fbcad04 | 2012-12-10 12:56:22 +0100 | [diff] [blame] | 481 | } |
| 482 | if (SetEndOfFile(s->hfile) == 0) { |
Max Reitz | 4bff28b | 2017-03-28 22:51:28 +0200 | [diff] [blame] | 483 | error_setg_win32(errp, GetLastError(), "SetEndOfFile error"); |
Fabien Chouteau | fbcad04 | 2012-12-10 12:56:22 +0100 | [diff] [blame] | 484 | return -EIO; |
| 485 | } |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | static int64_t raw_getlength(BlockDriverState *bs) |
| 490 | { |
| 491 | BDRVRawState *s = bs->opaque; |
| 492 | LARGE_INTEGER l; |
| 493 | ULARGE_INTEGER available, total, total_free; |
| 494 | DISK_GEOMETRY_EX dg; |
| 495 | DWORD count; |
| 496 | BOOL status; |
| 497 | |
| 498 | switch(s->type) { |
| 499 | case FTYPE_FILE: |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 500 | l.LowPart = GetFileSize(s->hfile, (PDWORD)&l.HighPart); |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 501 | if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR) |
| 502 | return -EIO; |
| 503 | break; |
| 504 | case FTYPE_CD: |
| 505 | if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free)) |
| 506 | return -EIO; |
| 507 | l.QuadPart = total.QuadPart; |
| 508 | break; |
| 509 | case FTYPE_HARDDISK: |
| 510 | status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, |
| 511 | NULL, 0, &dg, sizeof(dg), &count, NULL); |
| 512 | if (status != 0) { |
| 513 | l = dg.DiskSize; |
| 514 | } |
| 515 | break; |
| 516 | default: |
| 517 | return -EIO; |
| 518 | } |
| 519 | return l.QuadPart; |
| 520 | } |
| 521 | |
Fam Zheng | 4a1d5e1 | 2011-07-12 19:56:39 +0800 | [diff] [blame] | 522 | static int64_t raw_get_allocated_file_size(BlockDriverState *bs) |
| 523 | { |
| 524 | typedef DWORD (WINAPI * get_compressed_t)(const char *filename, |
| 525 | DWORD * high); |
| 526 | get_compressed_t get_compressed; |
| 527 | struct _stati64 st; |
| 528 | const char *filename = bs->filename; |
| 529 | /* WinNT support GetCompressedFileSize to determine allocate size */ |
| 530 | get_compressed = |
| 531 | (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"), |
| 532 | "GetCompressedFileSizeA"); |
| 533 | if (get_compressed) { |
| 534 | DWORD high, low; |
| 535 | low = get_compressed(filename, &high); |
| 536 | if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR) { |
| 537 | return (((int64_t) high) << 32) + low; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | if (_stati64(filename, &st) < 0) { |
| 542 | return -1; |
| 543 | } |
| 544 | return st.st_size; |
| 545 | } |
| 546 | |
Chunyan Liu | ddef769 | 2014-06-05 17:21:02 +0800 | [diff] [blame] | 547 | static int raw_create(const char *filename, QemuOpts *opts, Error **errp) |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 548 | { |
| 549 | int fd; |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 550 | int64_t total_size = 0; |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 551 | |
Max Reitz | d5546c5 | 2014-03-05 22:41:40 +0100 | [diff] [blame] | 552 | strstart(filename, "file:", &filename); |
| 553 | |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 554 | /* Read out options */ |
Hu Tao | 180e952 | 2014-09-10 17:05:46 +0800 | [diff] [blame] | 555 | total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), |
| 556 | BDRV_SECTOR_SIZE); |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 557 | |
Corey Bryant | 6165f4d | 2012-08-14 16:43:45 -0400 | [diff] [blame] | 558 | fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, |
| 559 | 0644); |
Max Reitz | c6252b7 | 2013-10-10 15:44:02 +0200 | [diff] [blame] | 560 | if (fd < 0) { |
| 561 | error_setg_errno(errp, errno, "Could not create file"); |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 562 | return -EIO; |
Max Reitz | c6252b7 | 2013-10-10 15:44:02 +0200 | [diff] [blame] | 563 | } |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 564 | set_sparse(fd); |
Hu Tao | 180e952 | 2014-09-10 17:05:46 +0800 | [diff] [blame] | 565 | ftruncate(fd, total_size); |
Corey Bryant | 2e1e79d | 2012-08-14 16:43:46 -0400 | [diff] [blame] | 566 | qemu_close(fd); |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 567 | return 0; |
| 568 | } |
| 569 | |
Chunyan Liu | ddef769 | 2014-06-05 17:21:02 +0800 | [diff] [blame] | 570 | |
| 571 | static QemuOptsList raw_create_opts = { |
| 572 | .name = "raw-create-opts", |
| 573 | .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head), |
| 574 | .desc = { |
| 575 | { |
| 576 | .name = BLOCK_OPT_SIZE, |
| 577 | .type = QEMU_OPT_SIZE, |
| 578 | .help = "Virtual disk size" |
| 579 | }, |
| 580 | { /* end of list */ } |
| 581 | } |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 582 | }; |
| 583 | |
Max Reitz | 5f535a9 | 2014-12-02 18:32:41 +0100 | [diff] [blame] | 584 | BlockDriver bdrv_file = { |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 585 | .format_name = "file", |
| 586 | .protocol_name = "file", |
aliguori | f1b2f71 | 2009-04-07 18:23:51 +0000 | [diff] [blame] | 587 | .instance_size = sizeof(BDRVRawState), |
BenoƮt Canet | 030be32 | 2013-09-24 17:07:04 +0200 | [diff] [blame] | 588 | .bdrv_needs_filename = true, |
Max Reitz | 7dc74db | 2014-03-05 22:41:39 +0100 | [diff] [blame] | 589 | .bdrv_parse_filename = raw_parse_filename, |
Chunyan Liu | ddef769 | 2014-06-05 17:21:02 +0800 | [diff] [blame] | 590 | .bdrv_file_open = raw_open, |
Eric Blake | 2914a1d | 2016-06-23 16:37:16 -0600 | [diff] [blame] | 591 | .bdrv_refresh_limits = raw_probe_alignment, |
Chunyan Liu | ddef769 | 2014-06-05 17:21:02 +0800 | [diff] [blame] | 592 | .bdrv_close = raw_close, |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 593 | .bdrv_create = raw_create, |
Peter Lieven | 3ac2162 | 2013-06-28 12:47:42 +0200 | [diff] [blame] | 594 | .bdrv_has_zero_init = bdrv_has_zero_init_1, |
Kevin Wolf | c68b89a | 2011-11-10 17:25:44 +0100 | [diff] [blame] | 595 | |
Paolo Bonzini | fc4edb8 | 2012-06-09 04:48:28 +0200 | [diff] [blame] | 596 | .bdrv_aio_readv = raw_aio_readv, |
| 597 | .bdrv_aio_writev = raw_aio_writev, |
| 598 | .bdrv_aio_flush = raw_aio_flush, |
Kevin Wolf | c68b89a | 2011-11-10 17:25:44 +0100 | [diff] [blame] | 599 | |
aliguori | f1b2f71 | 2009-04-07 18:23:51 +0000 | [diff] [blame] | 600 | .bdrv_truncate = raw_truncate, |
| 601 | .bdrv_getlength = raw_getlength, |
Fam Zheng | 4a1d5e1 | 2011-07-12 19:56:39 +0800 | [diff] [blame] | 602 | .bdrv_get_allocated_file_size |
| 603 | = raw_get_allocated_file_size, |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 604 | |
Chunyan Liu | ddef769 | 2014-06-05 17:21:02 +0800 | [diff] [blame] | 605 | .create_opts = &raw_create_opts, |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 606 | }; |
| 607 | |
| 608 | /***********************************************/ |
| 609 | /* host device */ |
| 610 | |
| 611 | static int find_cdrom(char *cdrom_name, int cdrom_name_size) |
| 612 | { |
| 613 | char drives[256], *pdrv = drives; |
| 614 | UINT type; |
| 615 | |
| 616 | memset(drives, 0, sizeof(drives)); |
| 617 | GetLogicalDriveStrings(sizeof(drives), drives); |
| 618 | while(pdrv[0] != '\0') { |
| 619 | type = GetDriveType(pdrv); |
| 620 | switch(type) { |
| 621 | case DRIVE_CDROM: |
| 622 | snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]); |
| 623 | return 0; |
| 624 | break; |
| 625 | } |
| 626 | pdrv += lstrlen(pdrv) + 1; |
| 627 | } |
| 628 | return -1; |
| 629 | } |
| 630 | |
| 631 | static int find_device_type(BlockDriverState *bs, const char *filename) |
| 632 | { |
| 633 | BDRVRawState *s = bs->opaque; |
| 634 | UINT type; |
| 635 | const char *p; |
| 636 | |
| 637 | if (strstart(filename, "\\\\.\\", &p) || |
| 638 | strstart(filename, "//./", &p)) { |
| 639 | if (stristart(p, "PhysicalDrive", NULL)) |
| 640 | return FTYPE_HARDDISK; |
| 641 | snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]); |
| 642 | type = GetDriveType(s->drive_path); |
aliguori | 3060cd1 | 2009-04-07 01:24:53 +0000 | [diff] [blame] | 643 | switch (type) { |
| 644 | case DRIVE_REMOVABLE: |
| 645 | case DRIVE_FIXED: |
| 646 | return FTYPE_HARDDISK; |
| 647 | case DRIVE_CDROM: |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 648 | return FTYPE_CD; |
aliguori | 3060cd1 | 2009-04-07 01:24:53 +0000 | [diff] [blame] | 649 | default: |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 650 | return FTYPE_FILE; |
aliguori | 3060cd1 | 2009-04-07 01:24:53 +0000 | [diff] [blame] | 651 | } |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 652 | } else { |
| 653 | return FTYPE_FILE; |
| 654 | } |
| 655 | } |
| 656 | |
Christoph Hellwig | 508c7cb | 2009-06-15 14:04:22 +0200 | [diff] [blame] | 657 | static int hdev_probe_device(const char *filename) |
| 658 | { |
| 659 | if (strstart(filename, "/dev/cdrom", NULL)) |
| 660 | return 100; |
| 661 | if (is_windows_drive(filename)) |
| 662 | return 100; |
| 663 | return 0; |
| 664 | } |
| 665 | |
Max Reitz | 57ed25b | 2014-03-08 00:39:45 +0100 | [diff] [blame] | 666 | static void hdev_parse_filename(const char *filename, QDict *options, |
| 667 | Error **errp) |
| 668 | { |
Max Reitz | 03c320d | 2017-05-22 21:52:16 +0200 | [diff] [blame] | 669 | bdrv_parse_filename_strip_prefix(filename, "host_device:", options); |
Max Reitz | 57ed25b | 2014-03-08 00:39:45 +0100 | [diff] [blame] | 670 | } |
| 671 | |
Max Reitz | 015a103 | 2013-09-05 14:22:29 +0200 | [diff] [blame] | 672 | static int hdev_open(BlockDriverState *bs, QDict *options, int flags, |
| 673 | Error **errp) |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 674 | { |
| 675 | BDRVRawState *s = bs->opaque; |
| 676 | int access_flags, create_flags; |
Stefan Weil | 68dc036 | 2013-09-01 22:59:25 +0200 | [diff] [blame] | 677 | int ret = 0; |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 678 | DWORD overlapped; |
| 679 | char device_name[64]; |
Stefan Weil | 68dc036 | 2013-09-01 22:59:25 +0200 | [diff] [blame] | 680 | |
| 681 | Error *local_err = NULL; |
| 682 | const char *filename; |
Kevin Wolf | 0a4279d | 2016-09-08 15:09:01 +0200 | [diff] [blame] | 683 | bool use_aio; |
Stefan Weil | 68dc036 | 2013-09-01 22:59:25 +0200 | [diff] [blame] | 684 | |
Peter Crosthwaite | 87ea75d | 2014-01-01 18:49:17 -0800 | [diff] [blame] | 685 | QemuOpts *opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, |
| 686 | &error_abort); |
Stefan Weil | 68dc036 | 2013-09-01 22:59:25 +0200 | [diff] [blame] | 687 | qemu_opts_absorb_qdict(opts, options, &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 688 | if (local_err) { |
Max Reitz | c6252b7 | 2013-10-10 15:44:02 +0200 | [diff] [blame] | 689 | error_propagate(errp, local_err); |
Stefan Weil | 68dc036 | 2013-09-01 22:59:25 +0200 | [diff] [blame] | 690 | ret = -EINVAL; |
| 691 | goto done; |
| 692 | } |
| 693 | |
| 694 | filename = qemu_opt_get(opts, "filename"); |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 695 | |
Kevin Wolf | 0a4279d | 2016-09-08 15:09:01 +0200 | [diff] [blame] | 696 | use_aio = get_aio_option(opts, flags, &local_err); |
| 697 | if (!local_err && use_aio) { |
| 698 | error_setg(&local_err, "AIO is not supported on Windows host devices"); |
| 699 | } |
| 700 | if (local_err) { |
| 701 | error_propagate(errp, local_err); |
| 702 | ret = -EINVAL; |
| 703 | goto done; |
| 704 | } |
| 705 | |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 706 | if (strstart(filename, "/dev/cdrom", NULL)) { |
Stefan Weil | 68dc036 | 2013-09-01 22:59:25 +0200 | [diff] [blame] | 707 | if (find_cdrom(device_name, sizeof(device_name)) < 0) { |
Max Reitz | c6252b7 | 2013-10-10 15:44:02 +0200 | [diff] [blame] | 708 | error_setg(errp, "Could not open CD-ROM drive"); |
Stefan Weil | 68dc036 | 2013-09-01 22:59:25 +0200 | [diff] [blame] | 709 | ret = -ENOENT; |
| 710 | goto done; |
| 711 | } |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 712 | filename = device_name; |
| 713 | } else { |
| 714 | /* transform drive letters into device name */ |
| 715 | if (((filename[0] >= 'a' && filename[0] <= 'z') || |
| 716 | (filename[0] >= 'A' && filename[0] <= 'Z')) && |
| 717 | filename[1] == ':' && filename[2] == '\0') { |
| 718 | snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]); |
| 719 | filename = device_name; |
| 720 | } |
| 721 | } |
| 722 | s->type = find_device_type(bs, filename); |
| 723 | |
Kevin Wolf | 0a4279d | 2016-09-08 15:09:01 +0200 | [diff] [blame] | 724 | raw_parse_flags(flags, use_aio, &access_flags, &overlapped); |
Jeff Cody | 6a8dc04 | 2012-09-20 15:13:21 -0400 | [diff] [blame] | 725 | |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 726 | create_flags = OPEN_EXISTING; |
| 727 | |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 728 | s->hfile = CreateFile(filename, access_flags, |
| 729 | FILE_SHARE_READ, NULL, |
| 730 | create_flags, overlapped, NULL); |
| 731 | if (s->hfile == INVALID_HANDLE_VALUE) { |
| 732 | int err = GetLastError(); |
| 733 | |
Stefan Weil | 68dc036 | 2013-09-01 22:59:25 +0200 | [diff] [blame] | 734 | if (err == ERROR_ACCESS_DENIED) { |
| 735 | ret = -EACCES; |
| 736 | } else { |
Max Reitz | 45d57f6 | 2013-10-11 14:30:16 +0200 | [diff] [blame] | 737 | ret = -EINVAL; |
Stefan Weil | 68dc036 | 2013-09-01 22:59:25 +0200 | [diff] [blame] | 738 | } |
Max Reitz | 45d57f6 | 2013-10-11 14:30:16 +0200 | [diff] [blame] | 739 | error_setg_errno(errp, -ret, "Could not open device"); |
Stefan Weil | 68dc036 | 2013-09-01 22:59:25 +0200 | [diff] [blame] | 740 | goto done; |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 741 | } |
Stefan Weil | 68dc036 | 2013-09-01 22:59:25 +0200 | [diff] [blame] | 742 | |
| 743 | done: |
| 744 | qemu_opts_del(opts); |
| 745 | return ret; |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 748 | static BlockDriver bdrv_host_device = { |
aurel32 | e60f469 | 2009-03-07 22:00:29 +0000 | [diff] [blame] | 749 | .format_name = "host_device", |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 750 | .protocol_name = "host_device", |
aurel32 | e60f469 | 2009-03-07 22:00:29 +0000 | [diff] [blame] | 751 | .instance_size = sizeof(BDRVRawState), |
BenoƮt Canet | 030be32 | 2013-09-24 17:07:04 +0200 | [diff] [blame] | 752 | .bdrv_needs_filename = true, |
Max Reitz | 57ed25b | 2014-03-08 00:39:45 +0100 | [diff] [blame] | 753 | .bdrv_parse_filename = hdev_parse_filename, |
Christoph Hellwig | 508c7cb | 2009-06-15 14:04:22 +0200 | [diff] [blame] | 754 | .bdrv_probe_device = hdev_probe_device, |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 755 | .bdrv_file_open = hdev_open, |
aurel32 | e60f469 | 2009-03-07 22:00:29 +0000 | [diff] [blame] | 756 | .bdrv_close = raw_close, |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 757 | |
Paolo Bonzini | fc4edb8 | 2012-06-09 04:48:28 +0200 | [diff] [blame] | 758 | .bdrv_aio_readv = raw_aio_readv, |
| 759 | .bdrv_aio_writev = raw_aio_writev, |
| 760 | .bdrv_aio_flush = raw_aio_flush, |
Kevin Wolf | c68b89a | 2011-11-10 17:25:44 +0100 | [diff] [blame] | 761 | |
Stefan Hajnoczi | 85ebd38 | 2014-05-08 16:34:50 +0200 | [diff] [blame] | 762 | .bdrv_detach_aio_context = raw_detach_aio_context, |
| 763 | .bdrv_attach_aio_context = raw_attach_aio_context, |
| 764 | |
Kevin Wolf | b94a261 | 2013-10-29 12:18:58 +0100 | [diff] [blame] | 765 | .bdrv_getlength = raw_getlength, |
| 766 | .has_variable_length = true, |
| 767 | |
Fam Zheng | 4a1d5e1 | 2011-07-12 19:56:39 +0800 | [diff] [blame] | 768 | .bdrv_get_allocated_file_size |
| 769 | = raw_get_allocated_file_size, |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 770 | }; |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 771 | |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 772 | static void bdrv_file_init(void) |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 773 | { |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 774 | bdrv_register(&bdrv_file); |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 775 | bdrv_register(&bdrv_host_device); |
| 776 | } |
| 777 | |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 778 | block_init(bdrv_file_init); |