blob: 41f55dfece6e21e08c351286752e98e565f1bbf3 [file] [log] [blame]
ths223d4672007-12-15 17:28:36 +00001/*
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 */
Markus Armbruster922a01a2018-02-01 12:18:46 +010024
Peter Maydell80c71a22016-01-18 18:01:42 +000025#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010026#include "qapi/error.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020027#include "qemu/cutils.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010028#include "block/block_int.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010029#include "qemu/module.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010030#include "qemu/option.h"
Paolo Bonzini0187f5c2016-07-04 18:33:20 +020031#include "block/raw-aio.h"
Paolo Bonzinifc4edb82012-06-09 04:48:28 +020032#include "trace.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010033#include "block/thread-pool.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010034#include "qemu/iov.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010035#include "qapi/qmp/qdict.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010036#include "qapi/qmp/qstring.h"
aliguori49dc7682009-03-08 16:26:59 +000037#include <windows.h>
ths223d4672007-12-15 17:28:36 +000038#include <winioctl.h>
39
40#define FTYPE_FILE 0
41#define FTYPE_CD 1
42#define FTYPE_HARDDISK 2
43
Paolo Bonzinifc4edb82012-06-09 04:48:28 +020044typedef struct RawWin32AIOData {
45 BlockDriverState *bs;
46 HANDLE hfile;
47 struct iovec *aio_iov;
48 int aio_niov;
49 size_t aio_nbytes;
50 off64_t aio_offset;
51 int aio_type;
52} RawWin32AIOData;
53
ths223d4672007-12-15 17:28:36 +000054typedef struct BDRVRawState {
55 HANDLE hfile;
56 int type;
57 char drive_path[16]; /* format: "d:\" */
Paolo Bonzinia2736522012-10-26 11:43:58 +020058 QEMUWin32AIOState *aio;
ths223d4672007-12-15 17:28:36 +000059} BDRVRawState;
60
Paolo Bonzinifc4edb82012-06-09 04:48:28 +020061/*
62 * Read/writes the data to/from a given linear buffer.
63 *
64 * Returns the number of bytes handles or -errno in case of an error. Short
65 * reads are only returned if the end of the file is reached.
66 */
67static size_t handle_aiocb_rw(RawWin32AIOData *aiocb)
68{
69 size_t offset = 0;
70 int i;
71
72 for (i = 0; i < aiocb->aio_niov; i++) {
73 OVERLAPPED ov;
74 DWORD ret, ret_count, len;
75
76 memset(&ov, 0, sizeof(ov));
77 ov.Offset = (aiocb->aio_offset + offset);
78 ov.OffsetHigh = (aiocb->aio_offset + offset) >> 32;
79 len = aiocb->aio_iov[i].iov_len;
80 if (aiocb->aio_type & QEMU_AIO_WRITE) {
81 ret = WriteFile(aiocb->hfile, aiocb->aio_iov[i].iov_base,
82 len, &ret_count, &ov);
83 } else {
84 ret = ReadFile(aiocb->hfile, aiocb->aio_iov[i].iov_base,
85 len, &ret_count, &ov);
86 }
87 if (!ret) {
88 ret_count = 0;
89 }
90 if (ret_count != len) {
Tal Kain56e023a2013-09-09 11:14:55 +020091 offset += ret_count;
Paolo Bonzinifc4edb82012-06-09 04:48:28 +020092 break;
93 }
94 offset += len;
95 }
96
97 return offset;
98}
99
100static int aio_worker(void *arg)
101{
102 RawWin32AIOData *aiocb = arg;
103 ssize_t ret = 0;
104 size_t count;
105
106 switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
107 case QEMU_AIO_READ:
108 count = handle_aiocb_rw(aiocb);
Max Reitzc0191e72015-02-05 13:58:24 -0500109 if (count < aiocb->aio_nbytes) {
Paolo Bonzinifc4edb82012-06-09 04:48:28 +0200110 /* A short read means that we have reached EOF. Pad the buffer
111 * with zeros for bytes after EOF. */
112 iov_memset(aiocb->aio_iov, aiocb->aio_niov, count,
113 0, aiocb->aio_nbytes - count);
114
115 count = aiocb->aio_nbytes;
116 }
117 if (count == aiocb->aio_nbytes) {
118 ret = 0;
119 } else {
120 ret = -EINVAL;
121 }
122 break;
123 case QEMU_AIO_WRITE:
124 count = handle_aiocb_rw(aiocb);
125 if (count == aiocb->aio_nbytes) {
Kevin Wolf5d555032015-09-23 14:58:21 +0200126 ret = 0;
Paolo Bonzinifc4edb82012-06-09 04:48:28 +0200127 } else {
Kevin Wolf5d555032015-09-23 14:58:21 +0200128 ret = -EINVAL;
Paolo Bonzinifc4edb82012-06-09 04:48:28 +0200129 }
130 break;
131 case QEMU_AIO_FLUSH:
132 if (!FlushFileBuffers(aiocb->hfile)) {
133 return -EIO;
134 }
135 break;
136 default:
137 fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
138 ret = -EINVAL;
139 break;
140 }
141
Paolo Bonzinic84b3192015-10-01 13:04:39 +0200142 g_free(aiocb);
Paolo Bonzinifc4edb82012-06-09 04:48:28 +0200143 return ret;
144}
145
Markus Armbruster7c84b1b2014-10-07 13:59:14 +0200146static BlockAIOCB *paio_submit(BlockDriverState *bs, HANDLE hfile,
Eric Blake36e3b2e2016-07-15 17:22:55 -0600147 int64_t offset, QEMUIOVector *qiov, int count,
Markus Armbruster097310b2014-10-07 13:59:15 +0200148 BlockCompletionFunc *cb, void *opaque, int type)
Paolo Bonzinifc4edb82012-06-09 04:48:28 +0200149{
Paolo Bonzinic84b3192015-10-01 13:04:39 +0200150 RawWin32AIOData *acb = g_new(RawWin32AIOData, 1);
Stefan Hajnoczic4d9d192013-03-07 13:41:49 +0100151 ThreadPool *pool;
Paolo Bonzinifc4edb82012-06-09 04:48:28 +0200152
153 acb->bs = bs;
154 acb->hfile = hfile;
155 acb->aio_type = type;
156
157 if (qiov) {
158 acb->aio_iov = qiov->iov;
159 acb->aio_niov = qiov->niov;
Eric Blake36e3b2e2016-07-15 17:22:55 -0600160 assert(qiov->size == count);
Paolo Bonzinifc4edb82012-06-09 04:48:28 +0200161 }
Eric Blake36e3b2e2016-07-15 17:22:55 -0600162 acb->aio_nbytes = count;
163 acb->aio_offset = offset;
Paolo Bonzinifc4edb82012-06-09 04:48:28 +0200164
Fam Zhengf8a30872018-07-10 14:31:15 +0800165 trace_file_paio_submit(acb, opaque, offset, count, type);
Stefan Hajnoczic4d9d192013-03-07 13:41:49 +0100166 pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
167 return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque);
Paolo Bonzinifc4edb82012-06-09 04:48:28 +0200168}
169
ths223d4672007-12-15 17:28:36 +0000170int qemu_ftruncate64(int fd, int64_t length)
171{
172 LARGE_INTEGER li;
Stefan Weil2c993ec2011-08-20 12:10:05 +0200173 DWORD dw;
ths223d4672007-12-15 17:28:36 +0000174 LONG high;
175 HANDLE h;
176 BOOL res;
177
178 if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
Paolo Bonzini7d374352018-12-13 23:37:37 +0100179 return -1;
ths223d4672007-12-15 17:28:36 +0000180
181 h = (HANDLE)_get_osfhandle(fd);
182
183 /* get current position, ftruncate do not change position */
184 li.HighPart = 0;
185 li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
Stefan Weil2c993ec2011-08-20 12:10:05 +0200186 if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
Paolo Bonzini7d374352018-12-13 23:37:37 +0100187 return -1;
Stefan Weil2c993ec2011-08-20 12:10:05 +0200188 }
ths223d4672007-12-15 17:28:36 +0000189
190 high = length >> 32;
Stefan Weil2c993ec2011-08-20 12:10:05 +0200191 dw = SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN);
192 if (dw == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
Paolo Bonzini7d374352018-12-13 23:37:37 +0100193 return -1;
Stefan Weil2c993ec2011-08-20 12:10:05 +0200194 }
ths223d4672007-12-15 17:28:36 +0000195 res = SetEndOfFile(h);
196
197 /* back to old position */
198 SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
199 return res ? 0 : -1;
200}
201
202static int set_sparse(int fd)
203{
204 DWORD returned;
205 return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
Paolo Bonzini7d374352018-12-13 23:37:37 +0100206 NULL, 0, NULL, 0, &returned, NULL);
ths223d4672007-12-15 17:28:36 +0000207}
208
Stefan Hajnoczi85ebd382014-05-08 16:34:50 +0200209static void raw_detach_aio_context(BlockDriverState *bs)
210{
211 BDRVRawState *s = bs->opaque;
212
213 if (s->aio) {
214 win32_aio_detach_aio_context(s->aio, bdrv_get_aio_context(bs));
215 }
216}
217
218static void raw_attach_aio_context(BlockDriverState *bs,
219 AioContext *new_context)
220{
221 BDRVRawState *s = bs->opaque;
222
223 if (s->aio) {
224 win32_aio_attach_aio_context(s->aio, new_context);
225 }
226}
227
Eric Blake2914a1d2016-06-23 16:37:16 -0600228static void raw_probe_alignment(BlockDriverState *bs, Error **errp)
Paolo Bonzinic25f53b2011-11-29 12:42:20 +0100229{
230 BDRVRawState *s = bs->opaque;
231 DWORD sectorsPerCluster, freeClusters, totalClusters, count;
232 DISK_GEOMETRY_EX dg;
233 BOOL status;
234
235 if (s->type == FTYPE_CD) {
Eric Blakea5b8dd22016-06-23 16:37:24 -0600236 bs->bl.request_alignment = 2048;
Paolo Bonzinic25f53b2011-11-29 12:42:20 +0100237 return;
238 }
239 if (s->type == FTYPE_HARDDISK) {
240 status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
241 NULL, 0, &dg, sizeof(dg), &count, NULL);
242 if (status != 0) {
Eric Blakea5b8dd22016-06-23 16:37:24 -0600243 bs->bl.request_alignment = dg.Geometry.BytesPerSector;
Paolo Bonzinic25f53b2011-11-29 12:42:20 +0100244 return;
245 }
246 /* try GetDiskFreeSpace too */
247 }
248
249 if (s->drive_path[0]) {
250 GetDiskFreeSpace(s->drive_path, &sectorsPerCluster,
251 &dg.Geometry.BytesPerSector,
252 &freeClusters, &totalClusters);
Eric Blakea5b8dd22016-06-23 16:37:24 -0600253 bs->bl.request_alignment = dg.Geometry.BytesPerSector;
Eric Blakede7056a2018-04-24 14:25:02 -0500254 return;
Paolo Bonzinic25f53b2011-11-29 12:42:20 +0100255 }
Eric Blakede7056a2018-04-24 14:25:02 -0500256
257 /* XXX Does Windows support AIO on less than 512-byte alignment? */
258 bs->bl.request_alignment = 512;
Paolo Bonzinic25f53b2011-11-29 12:42:20 +0100259}
260
Kevin Wolf0a4279d2016-09-08 15:09:01 +0200261static void raw_parse_flags(int flags, bool use_aio, int *access_flags,
262 DWORD *overlapped)
Jeff Cody6a8dc042012-09-20 15:13:21 -0400263{
264 assert(access_flags != NULL);
265 assert(overlapped != NULL);
266
267 if (flags & BDRV_O_RDWR) {
268 *access_flags = GENERIC_READ | GENERIC_WRITE;
269 } else {
270 *access_flags = GENERIC_READ;
271 }
272
273 *overlapped = FILE_ATTRIBUTE_NORMAL;
Kevin Wolf0a4279d2016-09-08 15:09:01 +0200274 if (use_aio) {
Paolo Bonzinia2736522012-10-26 11:43:58 +0200275 *overlapped |= FILE_FLAG_OVERLAPPED;
276 }
Jeff Cody6a8dc042012-09-20 15:13:21 -0400277 if (flags & BDRV_O_NOCACHE) {
278 *overlapped |= FILE_FLAG_NO_BUFFERING;
279 }
Jeff Cody6a8dc042012-09-20 15:13:21 -0400280}
281
Max Reitz7dc74db2014-03-05 22:41:39 +0100282static void raw_parse_filename(const char *filename, QDict *options,
283 Error **errp)
284{
Max Reitz03c320d2017-05-22 21:52:16 +0200285 bdrv_parse_filename_strip_prefix(filename, "file:", options);
Max Reitz7dc74db2014-03-05 22:41:39 +0100286}
287
Kevin Wolf8a793802013-04-10 11:34:56 +0200288static QemuOptsList raw_runtime_opts = {
289 .name = "raw",
290 .head = QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head),
291 .desc = {
292 {
293 .name = "filename",
294 .type = QEMU_OPT_STRING,
295 .help = "File name of the image",
296 },
Kevin Wolf0a4279d2016-09-08 15:09:01 +0200297 {
298 .name = "aio",
299 .type = QEMU_OPT_STRING,
300 .help = "host AIO implementation (threads, native)",
301 },
Kevin Wolf8a793802013-04-10 11:34:56 +0200302 { /* end of list */ }
303 },
304};
305
Kevin Wolf0a4279d2016-09-08 15:09:01 +0200306static bool get_aio_option(QemuOpts *opts, int flags, Error **errp)
307{
308 BlockdevAioOptions aio, aio_default;
309
310 aio_default = (flags & BDRV_O_NATIVE_AIO) ? BLOCKDEV_AIO_OPTIONS_NATIVE
311 : BLOCKDEV_AIO_OPTIONS_THREADS;
Marc-AndrƩ Lureauf7abe0e2017-08-24 10:46:10 +0200312 aio = qapi_enum_parse(&BlockdevAioOptions_lookup, qemu_opt_get(opts, "aio"),
Markus Armbruster06c60b62017-08-24 10:45:57 +0200313 aio_default, errp);
Kevin Wolf0a4279d2016-09-08 15:09:01 +0200314
315 switch (aio) {
316 case BLOCKDEV_AIO_OPTIONS_NATIVE:
317 return true;
318 case BLOCKDEV_AIO_OPTIONS_THREADS:
319 return false;
320 default:
321 error_setg(errp, "Invalid AIO option");
322 }
323 return false;
324}
325
Max Reitz015a1032013-09-05 14:22:29 +0200326static int raw_open(BlockDriverState *bs, QDict *options, int flags,
327 Error **errp)
ths223d4672007-12-15 17:28:36 +0000328{
329 BDRVRawState *s = bs->opaque;
Christoph Hellwig9a2d77a2010-01-20 18:13:42 +0100330 int access_flags;
ths223d4672007-12-15 17:28:36 +0000331 DWORD overlapped;
Kevin Wolf8a793802013-04-10 11:34:56 +0200332 QemuOpts *opts;
333 Error *local_err = NULL;
334 const char *filename;
Kevin Wolf0a4279d2016-09-08 15:09:01 +0200335 bool use_aio;
Kevin Wolf8a793802013-04-10 11:34:56 +0200336 int ret;
ths223d4672007-12-15 17:28:36 +0000337
338 s->type = FTYPE_FILE;
339
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800340 opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort);
Kevin Wolf8a793802013-04-10 11:34:56 +0200341 qemu_opts_absorb_qdict(opts, options, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100342 if (local_err) {
Max Reitzc6252b72013-10-10 15:44:02 +0200343 error_propagate(errp, local_err);
Kevin Wolf8a793802013-04-10 11:34:56 +0200344 ret = -EINVAL;
345 goto fail;
346 }
347
Fam Zheng1c3a5552017-05-03 00:35:51 +0800348 if (qdict_get_try_bool(options, "locking", false)) {
349 error_setg(errp, "locking=on is not supported on Windows");
Gerd Hoffmanncdece042017-05-16 09:42:55 +0200350 ret = -EINVAL;
Fam Zheng1c3a5552017-05-03 00:35:51 +0800351 goto fail;
352 }
353
Kevin Wolf8a793802013-04-10 11:34:56 +0200354 filename = qemu_opt_get(opts, "filename");
355
Kevin Wolf0a4279d2016-09-08 15:09:01 +0200356 use_aio = get_aio_option(opts, flags, &local_err);
357 if (local_err) {
358 error_propagate(errp, local_err);
359 ret = -EINVAL;
360 goto fail;
361 }
362
363 raw_parse_flags(flags, use_aio, &access_flags, &overlapped);
Kevin Wolf8a793802013-04-10 11:34:56 +0200364
Paolo Bonzinic25f53b2011-11-29 12:42:20 +0100365 if (filename[0] && filename[1] == ':') {
366 snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", filename[0]);
367 } else if (filename[0] == '\\' && filename[1] == '\\') {
368 s->drive_path[0] = 0;
369 } else {
370 /* Relative path. */
371 char buf[MAX_PATH];
372 GetCurrentDirectory(MAX_PATH, buf);
373 snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", buf[0]);
374 }
375
ths223d4672007-12-15 17:28:36 +0000376 s->hfile = CreateFile(filename, access_flags,
377 FILE_SHARE_READ, NULL,
Christoph Hellwig9a2d77a2010-01-20 18:13:42 +0100378 OPEN_EXISTING, overlapped, NULL);
ths223d4672007-12-15 17:28:36 +0000379 if (s->hfile == INVALID_HANDLE_VALUE) {
380 int err = GetLastError();
381
Halil Pasic09237752016-10-11 16:12:35 +0200382 error_setg_win32(errp, err, "Could not open '%s'", filename);
Kevin Wolf8a793802013-04-10 11:34:56 +0200383 if (err == ERROR_ACCESS_DENIED) {
384 ret = -EACCES;
385 } else {
386 ret = -EINVAL;
387 }
388 goto fail;
Paolo Bonzinia2736522012-10-26 11:43:58 +0200389 }
390
Kevin Wolf0a4279d2016-09-08 15:09:01 +0200391 if (use_aio) {
Stefan Hajnoczi99cc5982014-05-08 16:34:49 +0200392 s->aio = win32_aio_init();
393 if (s->aio == NULL) {
394 CloseHandle(s->hfile);
395 error_setg(errp, "Could not initialize AIO");
396 ret = -EINVAL;
397 goto fail;
398 }
399
400 ret = win32_aio_attach(s->aio, s->hfile);
Paolo Bonzinia2736522012-10-26 11:43:58 +0200401 if (ret < 0) {
Stefan Hajnoczi99cc5982014-05-08 16:34:49 +0200402 win32_aio_cleanup(s->aio);
Paolo Bonzinia2736522012-10-26 11:43:58 +0200403 CloseHandle(s->hfile);
Max Reitzc6252b72013-10-10 15:44:02 +0200404 error_setg_errno(errp, -ret, "Could not enable AIO");
Kevin Wolf8a793802013-04-10 11:34:56 +0200405 goto fail;
Paolo Bonzinia2736522012-10-26 11:43:58 +0200406 }
Stefan Hajnoczi85ebd382014-05-08 16:34:50 +0200407
408 win32_aio_attach_aio_context(s->aio, bdrv_get_aio_context(bs));
ths223d4672007-12-15 17:28:36 +0000409 }
Kevin Wolf8a793802013-04-10 11:34:56 +0200410
411 ret = 0;
412fail:
413 qemu_opts_del(opts);
414 return ret;
ths223d4672007-12-15 17:28:36 +0000415}
416
Eric Blakede7056a2018-04-24 14:25:02 -0500417static BlockAIOCB *raw_aio_preadv(BlockDriverState *bs,
418 uint64_t offset, uint64_t bytes,
419 QEMUIOVector *qiov, int flags,
420 BlockCompletionFunc *cb, void *opaque)
ths223d4672007-12-15 17:28:36 +0000421{
422 BDRVRawState *s = bs->opaque;
Paolo Bonzinia2736522012-10-26 11:43:58 +0200423 if (s->aio) {
Eric Blakede7056a2018-04-24 14:25:02 -0500424 return win32_aio_submit(bs, s->aio, s->hfile, offset, bytes, qiov,
425 cb, opaque, QEMU_AIO_READ);
Paolo Bonzinia2736522012-10-26 11:43:58 +0200426 } else {
Eric Blakede7056a2018-04-24 14:25:02 -0500427 return paio_submit(bs, s->hfile, offset, qiov, bytes,
Paolo Bonzinia2736522012-10-26 11:43:58 +0200428 cb, opaque, QEMU_AIO_READ);
429 }
ths223d4672007-12-15 17:28:36 +0000430}
431
Eric Blakede7056a2018-04-24 14:25:02 -0500432static BlockAIOCB *raw_aio_pwritev(BlockDriverState *bs,
433 uint64_t offset, uint64_t bytes,
434 QEMUIOVector *qiov, int flags,
435 BlockCompletionFunc *cb, void *opaque)
ths223d4672007-12-15 17:28:36 +0000436{
437 BDRVRawState *s = bs->opaque;
Paolo Bonzinia2736522012-10-26 11:43:58 +0200438 if (s->aio) {
Eric Blakede7056a2018-04-24 14:25:02 -0500439 return win32_aio_submit(bs, s->aio, s->hfile, offset, bytes, qiov,
440 cb, opaque, QEMU_AIO_WRITE);
Paolo Bonzinia2736522012-10-26 11:43:58 +0200441 } else {
Eric Blakede7056a2018-04-24 14:25:02 -0500442 return paio_submit(bs, s->hfile, offset, qiov, bytes,
Paolo Bonzinia2736522012-10-26 11:43:58 +0200443 cb, opaque, QEMU_AIO_WRITE);
444 }
ths223d4672007-12-15 17:28:36 +0000445}
446
Markus Armbruster7c84b1b2014-10-07 13:59:14 +0200447static BlockAIOCB *raw_aio_flush(BlockDriverState *bs,
Markus Armbruster097310b2014-10-07 13:59:15 +0200448 BlockCompletionFunc *cb, void *opaque)
ths223d4672007-12-15 17:28:36 +0000449{
450 BDRVRawState *s = bs->opaque;
Paolo Bonzinifc4edb82012-06-09 04:48:28 +0200451 return paio_submit(bs, s->hfile, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
ths223d4672007-12-15 17:28:36 +0000452}
453
454static void raw_close(BlockDriverState *bs)
455{
456 BDRVRawState *s = bs->opaque;
Stefan Hajnoczi99cc5982014-05-08 16:34:49 +0200457
458 if (s->aio) {
Stefan Hajnoczi85ebd382014-05-08 16:34:50 +0200459 win32_aio_detach_aio_context(s->aio, bdrv_get_aio_context(bs));
Stefan Hajnoczi99cc5982014-05-08 16:34:49 +0200460 win32_aio_cleanup(s->aio);
461 s->aio = NULL;
462 }
463
ths223d4672007-12-15 17:28:36 +0000464 CloseHandle(s->hfile);
Kevin Wolf8bfea152014-04-11 19:16:36 +0200465 if (bs->open_flags & BDRV_O_TEMPORARY) {
466 unlink(bs->filename);
467 }
ths223d4672007-12-15 17:28:36 +0000468}
469
Kevin Wolf061ca8a2018-06-21 17:54:35 +0200470static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset,
471 PreallocMode prealloc, Error **errp)
ths223d4672007-12-15 17:28:36 +0000472{
473 BDRVRawState *s = bs->opaque;
blueswir1b9e82a52009-04-05 18:03:31 +0000474 LONG low, high;
Fabien Chouteaufbcad042012-12-10 12:56:22 +0100475 DWORD dwPtrLow;
ths223d4672007-12-15 17:28:36 +0000476
Max Reitz8243ccb2017-06-13 22:20:52 +0200477 if (prealloc != PREALLOC_MODE_OFF) {
478 error_setg(errp, "Unsupported preallocation mode '%s'",
Markus Armbruster977c7362017-08-24 10:46:08 +0200479 PreallocMode_str(prealloc));
Max Reitz8243ccb2017-06-13 22:20:52 +0200480 return -ENOTSUP;
481 }
482
ths223d4672007-12-15 17:28:36 +0000483 low = offset;
484 high = offset >> 32;
Fabien Chouteaufbcad042012-12-10 12:56:22 +0100485
486 /*
487 * An error has occurred if the return value is INVALID_SET_FILE_POINTER
488 * and GetLastError doesn't return NO_ERROR.
489 */
490 dwPtrLow = SetFilePointer(s->hfile, low, &high, FILE_BEGIN);
491 if (dwPtrLow == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
Max Reitz4bff28b2017-03-28 22:51:28 +0200492 error_setg_win32(errp, GetLastError(), "SetFilePointer error");
ths223d4672007-12-15 17:28:36 +0000493 return -EIO;
Fabien Chouteaufbcad042012-12-10 12:56:22 +0100494 }
495 if (SetEndOfFile(s->hfile) == 0) {
Max Reitz4bff28b2017-03-28 22:51:28 +0200496 error_setg_win32(errp, GetLastError(), "SetEndOfFile error");
Fabien Chouteaufbcad042012-12-10 12:56:22 +0100497 return -EIO;
498 }
ths223d4672007-12-15 17:28:36 +0000499 return 0;
500}
501
502static int64_t raw_getlength(BlockDriverState *bs)
503{
504 BDRVRawState *s = bs->opaque;
505 LARGE_INTEGER l;
506 ULARGE_INTEGER available, total, total_free;
507 DISK_GEOMETRY_EX dg;
508 DWORD count;
509 BOOL status;
510
511 switch(s->type) {
512 case FTYPE_FILE:
blueswir1b9e82a52009-04-05 18:03:31 +0000513 l.LowPart = GetFileSize(s->hfile, (PDWORD)&l.HighPart);
ths223d4672007-12-15 17:28:36 +0000514 if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
515 return -EIO;
516 break;
517 case FTYPE_CD:
518 if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
519 return -EIO;
520 l.QuadPart = total.QuadPart;
521 break;
522 case FTYPE_HARDDISK:
523 status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
524 NULL, 0, &dg, sizeof(dg), &count, NULL);
525 if (status != 0) {
526 l = dg.DiskSize;
527 }
528 break;
529 default:
530 return -EIO;
531 }
532 return l.QuadPart;
533}
534
Fam Zheng4a1d5e12011-07-12 19:56:39 +0800535static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
536{
537 typedef DWORD (WINAPI * get_compressed_t)(const char *filename,
538 DWORD * high);
539 get_compressed_t get_compressed;
540 struct _stati64 st;
541 const char *filename = bs->filename;
542 /* WinNT support GetCompressedFileSize to determine allocate size */
543 get_compressed =
544 (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"),
545 "GetCompressedFileSizeA");
546 if (get_compressed) {
547 DWORD high, low;
548 low = get_compressed(filename, &high);
549 if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR) {
550 return (((int64_t) high) << 32) + low;
551 }
552 }
553
554 if (_stati64(filename, &st) < 0) {
555 return -1;
556 }
557 return st.st_size;
558}
559
Kevin Wolf3766ef52018-02-05 16:24:32 +0100560static int raw_co_create(BlockdevCreateOptions *options, Error **errp)
561{
562 BlockdevCreateOptionsFile *file_opts;
563 int fd;
564
565 assert(options->driver == BLOCKDEV_DRIVER_FILE);
566 file_opts = &options->u.file;
567
568 if (file_opts->has_preallocation) {
569 error_setg(errp, "Preallocation is not supported on Windows");
570 return -EINVAL;
571 }
572 if (file_opts->has_nocow) {
573 error_setg(errp, "nocow is not supported on Windows");
574 return -EINVAL;
575 }
576
577 fd = qemu_open(file_opts->filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
578 0644);
579 if (fd < 0) {
580 error_setg_errno(errp, errno, "Could not create file");
581 return -EIO;
582 }
583 set_sparse(fd);
584 ftruncate(fd, file_opts->size);
585 qemu_close(fd);
586
587 return 0;
588}
589
Stefan Hajnocziefc75e22018-01-18 13:43:45 +0100590static int coroutine_fn raw_co_create_opts(const char *filename, QemuOpts *opts,
591 Error **errp)
ths223d4672007-12-15 17:28:36 +0000592{
Kevin Wolf3766ef52018-02-05 16:24:32 +0100593 BlockdevCreateOptions options;
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200594 int64_t total_size = 0;
ths223d4672007-12-15 17:28:36 +0000595
Max Reitzd5546c52014-03-05 22:41:40 +0100596 strstart(filename, "file:", &filename);
597
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200598 /* Read out options */
Hu Tao180e9522014-09-10 17:05:46 +0800599 total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
600 BDRV_SECTOR_SIZE);
ths223d4672007-12-15 17:28:36 +0000601
Kevin Wolf3766ef52018-02-05 16:24:32 +0100602 options = (BlockdevCreateOptions) {
603 .driver = BLOCKDEV_DRIVER_FILE,
604 .u.file = {
605 .filename = (char *) filename,
606 .size = total_size,
607 .has_preallocation = false,
608 .has_nocow = false,
609 },
610 };
611 return raw_co_create(&options, errp);
ths223d4672007-12-15 17:28:36 +0000612}
613
Chunyan Liuddef7692014-06-05 17:21:02 +0800614static QemuOptsList raw_create_opts = {
615 .name = "raw-create-opts",
616 .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head),
617 .desc = {
618 {
619 .name = BLOCK_OPT_SIZE,
620 .type = QEMU_OPT_SIZE,
621 .help = "Virtual disk size"
622 },
623 { /* end of list */ }
624 }
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200625};
626
Max Reitz5f535a92014-12-02 18:32:41 +0100627BlockDriver bdrv_file = {
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200628 .format_name = "file",
629 .protocol_name = "file",
aliguorif1b2f712009-04-07 18:23:51 +0000630 .instance_size = sizeof(BDRVRawState),
BenoƮt Canet030be322013-09-24 17:07:04 +0200631 .bdrv_needs_filename = true,
Max Reitz7dc74db2014-03-05 22:41:39 +0100632 .bdrv_parse_filename = raw_parse_filename,
Chunyan Liuddef7692014-06-05 17:21:02 +0800633 .bdrv_file_open = raw_open,
Eric Blake2914a1d2016-06-23 16:37:16 -0600634 .bdrv_refresh_limits = raw_probe_alignment,
Chunyan Liuddef7692014-06-05 17:21:02 +0800635 .bdrv_close = raw_close,
Stefan Hajnocziefc75e22018-01-18 13:43:45 +0100636 .bdrv_co_create_opts = raw_co_create_opts,
Peter Lieven3ac21622013-06-28 12:47:42 +0200637 .bdrv_has_zero_init = bdrv_has_zero_init_1,
Max Reitz1dcaf522019-07-24 19:12:32 +0200638 .bdrv_has_zero_init_truncate = bdrv_has_zero_init_1,
Kevin Wolfc68b89a2011-11-10 17:25:44 +0100639
Eric Blakede7056a2018-04-24 14:25:02 -0500640 .bdrv_aio_preadv = raw_aio_preadv,
641 .bdrv_aio_pwritev = raw_aio_pwritev,
Paolo Bonzinifc4edb82012-06-09 04:48:28 +0200642 .bdrv_aio_flush = raw_aio_flush,
Kevin Wolfc68b89a2011-11-10 17:25:44 +0100643
Kevin Wolf061ca8a2018-06-21 17:54:35 +0200644 .bdrv_co_truncate = raw_co_truncate,
aliguorif1b2f712009-04-07 18:23:51 +0000645 .bdrv_getlength = raw_getlength,
Fam Zheng4a1d5e12011-07-12 19:56:39 +0800646 .bdrv_get_allocated_file_size
647 = raw_get_allocated_file_size,
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200648
Chunyan Liuddef7692014-06-05 17:21:02 +0800649 .create_opts = &raw_create_opts,
ths223d4672007-12-15 17:28:36 +0000650};
651
652/***********************************************/
653/* host device */
654
655static int find_cdrom(char *cdrom_name, int cdrom_name_size)
656{
657 char drives[256], *pdrv = drives;
658 UINT type;
659
660 memset(drives, 0, sizeof(drives));
661 GetLogicalDriveStrings(sizeof(drives), drives);
662 while(pdrv[0] != '\0') {
663 type = GetDriveType(pdrv);
664 switch(type) {
665 case DRIVE_CDROM:
666 snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
667 return 0;
668 break;
669 }
670 pdrv += lstrlen(pdrv) + 1;
671 }
672 return -1;
673}
674
675static int find_device_type(BlockDriverState *bs, const char *filename)
676{
677 BDRVRawState *s = bs->opaque;
678 UINT type;
679 const char *p;
680
681 if (strstart(filename, "\\\\.\\", &p) ||
682 strstart(filename, "//./", &p)) {
683 if (stristart(p, "PhysicalDrive", NULL))
684 return FTYPE_HARDDISK;
685 snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
686 type = GetDriveType(s->drive_path);
aliguori3060cd12009-04-07 01:24:53 +0000687 switch (type) {
688 case DRIVE_REMOVABLE:
689 case DRIVE_FIXED:
690 return FTYPE_HARDDISK;
691 case DRIVE_CDROM:
ths223d4672007-12-15 17:28:36 +0000692 return FTYPE_CD;
aliguori3060cd12009-04-07 01:24:53 +0000693 default:
ths223d4672007-12-15 17:28:36 +0000694 return FTYPE_FILE;
aliguori3060cd12009-04-07 01:24:53 +0000695 }
ths223d4672007-12-15 17:28:36 +0000696 } else {
697 return FTYPE_FILE;
698 }
699}
700
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200701static int hdev_probe_device(const char *filename)
702{
703 if (strstart(filename, "/dev/cdrom", NULL))
704 return 100;
705 if (is_windows_drive(filename))
706 return 100;
707 return 0;
708}
709
Max Reitz57ed25b2014-03-08 00:39:45 +0100710static void hdev_parse_filename(const char *filename, QDict *options,
711 Error **errp)
712{
Max Reitz03c320d2017-05-22 21:52:16 +0200713 bdrv_parse_filename_strip_prefix(filename, "host_device:", options);
Max Reitz57ed25b2014-03-08 00:39:45 +0100714}
715
Eric Blakede7056a2018-04-24 14:25:02 -0500716static void hdev_refresh_limits(BlockDriverState *bs, Error **errp)
717{
718 /* XXX Does Windows support AIO on less than 512-byte alignment? */
719 bs->bl.request_alignment = 512;
720}
721
Max Reitz015a1032013-09-05 14:22:29 +0200722static int hdev_open(BlockDriverState *bs, QDict *options, int flags,
723 Error **errp)
ths223d4672007-12-15 17:28:36 +0000724{
725 BDRVRawState *s = bs->opaque;
726 int access_flags, create_flags;
Stefan Weil68dc0362013-09-01 22:59:25 +0200727 int ret = 0;
ths223d4672007-12-15 17:28:36 +0000728 DWORD overlapped;
729 char device_name[64];
Stefan Weil68dc0362013-09-01 22:59:25 +0200730
731 Error *local_err = NULL;
732 const char *filename;
Kevin Wolf0a4279d2016-09-08 15:09:01 +0200733 bool use_aio;
Stefan Weil68dc0362013-09-01 22:59:25 +0200734
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800735 QemuOpts *opts = qemu_opts_create(&raw_runtime_opts, NULL, 0,
736 &error_abort);
Stefan Weil68dc0362013-09-01 22:59:25 +0200737 qemu_opts_absorb_qdict(opts, options, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100738 if (local_err) {
Max Reitzc6252b72013-10-10 15:44:02 +0200739 error_propagate(errp, local_err);
Stefan Weil68dc0362013-09-01 22:59:25 +0200740 ret = -EINVAL;
741 goto done;
742 }
743
744 filename = qemu_opt_get(opts, "filename");
ths223d4672007-12-15 17:28:36 +0000745
Kevin Wolf0a4279d2016-09-08 15:09:01 +0200746 use_aio = get_aio_option(opts, flags, &local_err);
747 if (!local_err && use_aio) {
748 error_setg(&local_err, "AIO is not supported on Windows host devices");
749 }
750 if (local_err) {
751 error_propagate(errp, local_err);
752 ret = -EINVAL;
753 goto done;
754 }
755
ths223d4672007-12-15 17:28:36 +0000756 if (strstart(filename, "/dev/cdrom", NULL)) {
Stefan Weil68dc0362013-09-01 22:59:25 +0200757 if (find_cdrom(device_name, sizeof(device_name)) < 0) {
Max Reitzc6252b72013-10-10 15:44:02 +0200758 error_setg(errp, "Could not open CD-ROM drive");
Stefan Weil68dc0362013-09-01 22:59:25 +0200759 ret = -ENOENT;
760 goto done;
761 }
ths223d4672007-12-15 17:28:36 +0000762 filename = device_name;
763 } else {
764 /* transform drive letters into device name */
765 if (((filename[0] >= 'a' && filename[0] <= 'z') ||
766 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
767 filename[1] == ':' && filename[2] == '\0') {
768 snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
769 filename = device_name;
770 }
771 }
772 s->type = find_device_type(bs, filename);
773
Kevin Wolf0a4279d2016-09-08 15:09:01 +0200774 raw_parse_flags(flags, use_aio, &access_flags, &overlapped);
Jeff Cody6a8dc042012-09-20 15:13:21 -0400775
ths223d4672007-12-15 17:28:36 +0000776 create_flags = OPEN_EXISTING;
777
ths223d4672007-12-15 17:28:36 +0000778 s->hfile = CreateFile(filename, access_flags,
779 FILE_SHARE_READ, NULL,
780 create_flags, overlapped, NULL);
781 if (s->hfile == INVALID_HANDLE_VALUE) {
782 int err = GetLastError();
783
Stefan Weil68dc0362013-09-01 22:59:25 +0200784 if (err == ERROR_ACCESS_DENIED) {
785 ret = -EACCES;
786 } else {
Max Reitz45d57f62013-10-11 14:30:16 +0200787 ret = -EINVAL;
Stefan Weil68dc0362013-09-01 22:59:25 +0200788 }
Max Reitz45d57f62013-10-11 14:30:16 +0200789 error_setg_errno(errp, -ret, "Could not open device");
Stefan Weil68dc0362013-09-01 22:59:25 +0200790 goto done;
ths223d4672007-12-15 17:28:36 +0000791 }
Stefan Weil68dc0362013-09-01 22:59:25 +0200792
793done:
794 qemu_opts_del(opts);
795 return ret;
ths223d4672007-12-15 17:28:36 +0000796}
797
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500798static BlockDriver bdrv_host_device = {
aurel32e60f4692009-03-07 22:00:29 +0000799 .format_name = "host_device",
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200800 .protocol_name = "host_device",
aurel32e60f4692009-03-07 22:00:29 +0000801 .instance_size = sizeof(BDRVRawState),
BenoƮt Canet030be322013-09-24 17:07:04 +0200802 .bdrv_needs_filename = true,
Max Reitz57ed25b2014-03-08 00:39:45 +0100803 .bdrv_parse_filename = hdev_parse_filename,
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200804 .bdrv_probe_device = hdev_probe_device,
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200805 .bdrv_file_open = hdev_open,
aurel32e60f4692009-03-07 22:00:29 +0000806 .bdrv_close = raw_close,
Eric Blakede7056a2018-04-24 14:25:02 -0500807 .bdrv_refresh_limits = hdev_refresh_limits,
ths223d4672007-12-15 17:28:36 +0000808
Eric Blakede7056a2018-04-24 14:25:02 -0500809 .bdrv_aio_preadv = raw_aio_preadv,
810 .bdrv_aio_pwritev = raw_aio_pwritev,
Paolo Bonzinifc4edb82012-06-09 04:48:28 +0200811 .bdrv_aio_flush = raw_aio_flush,
Kevin Wolfc68b89a2011-11-10 17:25:44 +0100812
Stefan Hajnoczi85ebd382014-05-08 16:34:50 +0200813 .bdrv_detach_aio_context = raw_detach_aio_context,
814 .bdrv_attach_aio_context = raw_attach_aio_context,
815
Kevin Wolfb94a2612013-10-29 12:18:58 +0100816 .bdrv_getlength = raw_getlength,
817 .has_variable_length = true,
818
Fam Zheng4a1d5e12011-07-12 19:56:39 +0800819 .bdrv_get_allocated_file_size
820 = raw_get_allocated_file_size,
ths223d4672007-12-15 17:28:36 +0000821};
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500822
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200823static void bdrv_file_init(void)
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500824{
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200825 bdrv_register(&bdrv_file);
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500826 bdrv_register(&bdrv_host_device);
827}
828
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200829block_init(bdrv_file_init);