blob: 62edb1a6cc78fa3f738790418bab789d41dcf293 [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 */
Peter Maydell80c71a22016-01-18 18:01:42 +000024#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010025#include "qapi/error.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020026#include "qemu/cutils.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010027#include "qemu/timer.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"
Paolo Bonzinifc4edb82012-06-09 04:48:28 +020030#include "raw-aio.h"
31#include "trace.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010032#include "block/thread-pool.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010033#include "qemu/iov.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010034#include "qapi/qmp/qstring.h"
aliguori49dc7682009-03-08 16:26:59 +000035#include <windows.h>
ths223d4672007-12-15 17:28:36 +000036#include <winioctl.h>
37
38#define FTYPE_FILE 0
39#define FTYPE_CD 1
40#define FTYPE_HARDDISK 2
41
Paolo Bonzinifc4edb82012-06-09 04:48:28 +020042typedef 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
ths223d4672007-12-15 17:28:36 +000052typedef struct BDRVRawState {
53 HANDLE hfile;
54 int type;
55 char drive_path[16]; /* format: "d:\" */
Paolo Bonzinia2736522012-10-26 11:43:58 +020056 QEMUWin32AIOState *aio;
ths223d4672007-12-15 17:28:36 +000057} BDRVRawState;
58
Paolo Bonzinifc4edb82012-06-09 04:48:28 +020059/*
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 */
65static 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 Kain56e023a2013-09-09 11:14:55 +020089 offset += ret_count;
Paolo Bonzinifc4edb82012-06-09 04:48:28 +020090 break;
91 }
92 offset += len;
93 }
94
95 return offset;
96}
97
98static 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 Reitzc0191e72015-02-05 13:58:24 -0500107 if (count < aiocb->aio_nbytes) {
Paolo Bonzinifc4edb82012-06-09 04:48:28 +0200108 /* 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 Wolf5d555032015-09-23 14:58:21 +0200124 ret = 0;
Paolo Bonzinifc4edb82012-06-09 04:48:28 +0200125 } else {
Kevin Wolf5d555032015-09-23 14:58:21 +0200126 ret = -EINVAL;
Paolo Bonzinifc4edb82012-06-09 04:48:28 +0200127 }
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 Bonzinic84b3192015-10-01 13:04:39 +0200140 g_free(aiocb);
Paolo Bonzinifc4edb82012-06-09 04:48:28 +0200141 return ret;
142}
143
Markus Armbruster7c84b1b2014-10-07 13:59:14 +0200144static BlockAIOCB *paio_submit(BlockDriverState *bs, HANDLE hfile,
Paolo Bonzinifc4edb82012-06-09 04:48:28 +0200145 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
Markus Armbruster097310b2014-10-07 13:59:15 +0200146 BlockCompletionFunc *cb, void *opaque, int type)
Paolo Bonzinifc4edb82012-06-09 04:48:28 +0200147{
Paolo Bonzinic84b3192015-10-01 13:04:39 +0200148 RawWin32AIOData *acb = g_new(RawWin32AIOData, 1);
Stefan Hajnoczic4d9d192013-03-07 13:41:49 +0100149 ThreadPool *pool;
Paolo Bonzinifc4edb82012-06-09 04:48:28 +0200150
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;
158 }
159 acb->aio_nbytes = nb_sectors * 512;
160 acb->aio_offset = sector_num * 512;
161
162 trace_paio_submit(acb, opaque, sector_num, nb_sectors, type);
Stefan Hajnoczic4d9d192013-03-07 13:41:49 +0100163 pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
164 return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque);
Paolo Bonzinifc4edb82012-06-09 04:48:28 +0200165}
166
ths223d4672007-12-15 17:28:36 +0000167int qemu_ftruncate64(int fd, int64_t length)
168{
169 LARGE_INTEGER li;
Stefan Weil2c993ec2011-08-20 12:10:05 +0200170 DWORD dw;
ths223d4672007-12-15 17:28:36 +0000171 LONG high;
172 HANDLE h;
173 BOOL res;
174
175 if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
176 return -1;
177
178 h = (HANDLE)_get_osfhandle(fd);
179
180 /* get current position, ftruncate do not change position */
181 li.HighPart = 0;
182 li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
Stefan Weil2c993ec2011-08-20 12:10:05 +0200183 if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
ths223d4672007-12-15 17:28:36 +0000184 return -1;
Stefan Weil2c993ec2011-08-20 12:10:05 +0200185 }
ths223d4672007-12-15 17:28:36 +0000186
187 high = length >> 32;
Stefan Weil2c993ec2011-08-20 12:10:05 +0200188 dw = SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN);
189 if (dw == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
ths223d4672007-12-15 17:28:36 +0000190 return -1;
Stefan Weil2c993ec2011-08-20 12:10:05 +0200191 }
ths223d4672007-12-15 17:28:36 +0000192 res = SetEndOfFile(h);
193
194 /* back to old position */
195 SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
196 return res ? 0 : -1;
197}
198
199static int set_sparse(int fd)
200{
201 DWORD returned;
202 return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
203 NULL, 0, NULL, 0, &returned, NULL);
204}
205
Stefan Hajnoczi85ebd382014-05-08 16:34:50 +0200206static void raw_detach_aio_context(BlockDriverState *bs)
207{
208 BDRVRawState *s = bs->opaque;
209
210 if (s->aio) {
211 win32_aio_detach_aio_context(s->aio, bdrv_get_aio_context(bs));
212 }
213}
214
215static void raw_attach_aio_context(BlockDriverState *bs,
216 AioContext *new_context)
217{
218 BDRVRawState *s = bs->opaque;
219
220 if (s->aio) {
221 win32_aio_attach_aio_context(s->aio, new_context);
222 }
223}
224
Eric Blake2914a1d2016-06-23 16:37:16 -0600225static void raw_probe_alignment(BlockDriverState *bs, Error **errp)
Paolo Bonzinic25f53b2011-11-29 12:42:20 +0100226{
227 BDRVRawState *s = bs->opaque;
228 DWORD sectorsPerCluster, freeClusters, totalClusters, count;
229 DISK_GEOMETRY_EX dg;
230 BOOL status;
231
232 if (s->type == FTYPE_CD) {
Eric Blakea5b8dd22016-06-23 16:37:24 -0600233 bs->bl.request_alignment = 2048;
Paolo Bonzinic25f53b2011-11-29 12:42:20 +0100234 return;
235 }
236 if (s->type == FTYPE_HARDDISK) {
237 status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
238 NULL, 0, &dg, sizeof(dg), &count, NULL);
239 if (status != 0) {
Eric Blakea5b8dd22016-06-23 16:37:24 -0600240 bs->bl.request_alignment = dg.Geometry.BytesPerSector;
Paolo Bonzinic25f53b2011-11-29 12:42:20 +0100241 return;
242 }
243 /* try GetDiskFreeSpace too */
244 }
245
246 if (s->drive_path[0]) {
247 GetDiskFreeSpace(s->drive_path, &sectorsPerCluster,
248 &dg.Geometry.BytesPerSector,
249 &freeClusters, &totalClusters);
Eric Blakea5b8dd22016-06-23 16:37:24 -0600250 bs->bl.request_alignment = dg.Geometry.BytesPerSector;
Paolo Bonzinic25f53b2011-11-29 12:42:20 +0100251 }
252}
253
Jeff Cody6a8dc042012-09-20 15:13:21 -0400254static void raw_parse_flags(int flags, int *access_flags, DWORD *overlapped)
255{
256 assert(access_flags != NULL);
257 assert(overlapped != NULL);
258
259 if (flags & BDRV_O_RDWR) {
260 *access_flags = GENERIC_READ | GENERIC_WRITE;
261 } else {
262 *access_flags = GENERIC_READ;
263 }
264
265 *overlapped = FILE_ATTRIBUTE_NORMAL;
Paolo Bonzinia2736522012-10-26 11:43:58 +0200266 if (flags & BDRV_O_NATIVE_AIO) {
267 *overlapped |= FILE_FLAG_OVERLAPPED;
268 }
Jeff Cody6a8dc042012-09-20 15:13:21 -0400269 if (flags & BDRV_O_NOCACHE) {
270 *overlapped |= FILE_FLAG_NO_BUFFERING;
271 }
Jeff Cody6a8dc042012-09-20 15:13:21 -0400272}
273
Max Reitz7dc74db2014-03-05 22:41:39 +0100274static void raw_parse_filename(const char *filename, QDict *options,
275 Error **errp)
276{
277 /* The filename does not have to be prefixed by the protocol name, since
278 * "file" is the default protocol; therefore, the return value of this
279 * function call can be ignored. */
280 strstart(filename, "file:", &filename);
281
282 qdict_put_obj(options, "filename", QOBJECT(qstring_from_str(filename)));
283}
284
Kevin Wolf8a793802013-04-10 11:34:56 +0200285static QemuOptsList raw_runtime_opts = {
286 .name = "raw",
287 .head = QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head),
288 .desc = {
289 {
290 .name = "filename",
291 .type = QEMU_OPT_STRING,
292 .help = "File name of the image",
293 },
294 { /* end of list */ }
295 },
296};
297
Max Reitz015a1032013-09-05 14:22:29 +0200298static int raw_open(BlockDriverState *bs, QDict *options, int flags,
299 Error **errp)
ths223d4672007-12-15 17:28:36 +0000300{
301 BDRVRawState *s = bs->opaque;
Christoph Hellwig9a2d77a2010-01-20 18:13:42 +0100302 int access_flags;
ths223d4672007-12-15 17:28:36 +0000303 DWORD overlapped;
Kevin Wolf8a793802013-04-10 11:34:56 +0200304 QemuOpts *opts;
305 Error *local_err = NULL;
306 const char *filename;
307 int ret;
ths223d4672007-12-15 17:28:36 +0000308
309 s->type = FTYPE_FILE;
310
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800311 opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort);
Kevin Wolf8a793802013-04-10 11:34:56 +0200312 qemu_opts_absorb_qdict(opts, options, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100313 if (local_err) {
Max Reitzc6252b72013-10-10 15:44:02 +0200314 error_propagate(errp, local_err);
Kevin Wolf8a793802013-04-10 11:34:56 +0200315 ret = -EINVAL;
316 goto fail;
317 }
318
319 filename = qemu_opt_get(opts, "filename");
320
Jeff Cody6a8dc042012-09-20 15:13:21 -0400321 raw_parse_flags(flags, &access_flags, &overlapped);
Kevin Wolf8a793802013-04-10 11:34:56 +0200322
Paolo Bonzinic25f53b2011-11-29 12:42:20 +0100323 if (filename[0] && filename[1] == ':') {
324 snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", filename[0]);
325 } else if (filename[0] == '\\' && filename[1] == '\\') {
326 s->drive_path[0] = 0;
327 } else {
328 /* Relative path. */
329 char buf[MAX_PATH];
330 GetCurrentDirectory(MAX_PATH, buf);
331 snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", buf[0]);
332 }
333
ths223d4672007-12-15 17:28:36 +0000334 s->hfile = CreateFile(filename, access_flags,
335 FILE_SHARE_READ, NULL,
Christoph Hellwig9a2d77a2010-01-20 18:13:42 +0100336 OPEN_EXISTING, overlapped, NULL);
ths223d4672007-12-15 17:28:36 +0000337 if (s->hfile == INVALID_HANDLE_VALUE) {
338 int err = GetLastError();
339
Kevin Wolf8a793802013-04-10 11:34:56 +0200340 if (err == ERROR_ACCESS_DENIED) {
341 ret = -EACCES;
342 } else {
343 ret = -EINVAL;
344 }
345 goto fail;
Paolo Bonzinia2736522012-10-26 11:43:58 +0200346 }
347
348 if (flags & BDRV_O_NATIVE_AIO) {
Stefan Hajnoczi99cc5982014-05-08 16:34:49 +0200349 s->aio = win32_aio_init();
350 if (s->aio == NULL) {
351 CloseHandle(s->hfile);
352 error_setg(errp, "Could not initialize AIO");
353 ret = -EINVAL;
354 goto fail;
355 }
356
357 ret = win32_aio_attach(s->aio, s->hfile);
Paolo Bonzinia2736522012-10-26 11:43:58 +0200358 if (ret < 0) {
Stefan Hajnoczi99cc5982014-05-08 16:34:49 +0200359 win32_aio_cleanup(s->aio);
Paolo Bonzinia2736522012-10-26 11:43:58 +0200360 CloseHandle(s->hfile);
Max Reitzc6252b72013-10-10 15:44:02 +0200361 error_setg_errno(errp, -ret, "Could not enable AIO");
Kevin Wolf8a793802013-04-10 11:34:56 +0200362 goto fail;
Paolo Bonzinia2736522012-10-26 11:43:58 +0200363 }
Stefan Hajnoczi85ebd382014-05-08 16:34:50 +0200364
365 win32_aio_attach_aio_context(s->aio, bdrv_get_aio_context(bs));
ths223d4672007-12-15 17:28:36 +0000366 }
Kevin Wolf8a793802013-04-10 11:34:56 +0200367
368 ret = 0;
369fail:
370 qemu_opts_del(opts);
371 return ret;
ths223d4672007-12-15 17:28:36 +0000372}
373
Markus Armbruster7c84b1b2014-10-07 13:59:14 +0200374static BlockAIOCB *raw_aio_readv(BlockDriverState *bs,
Paolo Bonzinifc4edb82012-06-09 04:48:28 +0200375 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
Markus Armbruster097310b2014-10-07 13:59:15 +0200376 BlockCompletionFunc *cb, void *opaque)
ths223d4672007-12-15 17:28:36 +0000377{
378 BDRVRawState *s = bs->opaque;
Paolo Bonzinia2736522012-10-26 11:43:58 +0200379 if (s->aio) {
380 return win32_aio_submit(bs, s->aio, s->hfile, sector_num, qiov,
381 nb_sectors, cb, opaque, QEMU_AIO_READ);
382 } else {
383 return paio_submit(bs, s->hfile, sector_num, qiov, nb_sectors,
384 cb, opaque, QEMU_AIO_READ);
385 }
ths223d4672007-12-15 17:28:36 +0000386}
387
Markus Armbruster7c84b1b2014-10-07 13:59:14 +0200388static BlockAIOCB *raw_aio_writev(BlockDriverState *bs,
Paolo Bonzinifc4edb82012-06-09 04:48:28 +0200389 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
Markus Armbruster097310b2014-10-07 13:59:15 +0200390 BlockCompletionFunc *cb, void *opaque)
ths223d4672007-12-15 17:28:36 +0000391{
392 BDRVRawState *s = bs->opaque;
Paolo Bonzinia2736522012-10-26 11:43:58 +0200393 if (s->aio) {
394 return win32_aio_submit(bs, s->aio, s->hfile, sector_num, qiov,
395 nb_sectors, cb, opaque, QEMU_AIO_WRITE);
396 } else {
397 return paio_submit(bs, s->hfile, sector_num, qiov, nb_sectors,
398 cb, opaque, QEMU_AIO_WRITE);
399 }
ths223d4672007-12-15 17:28:36 +0000400}
401
Markus Armbruster7c84b1b2014-10-07 13:59:14 +0200402static BlockAIOCB *raw_aio_flush(BlockDriverState *bs,
Markus Armbruster097310b2014-10-07 13:59:15 +0200403 BlockCompletionFunc *cb, void *opaque)
ths223d4672007-12-15 17:28:36 +0000404{
405 BDRVRawState *s = bs->opaque;
Paolo Bonzinifc4edb82012-06-09 04:48:28 +0200406 return paio_submit(bs, s->hfile, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
ths223d4672007-12-15 17:28:36 +0000407}
408
409static void raw_close(BlockDriverState *bs)
410{
411 BDRVRawState *s = bs->opaque;
Stefan Hajnoczi99cc5982014-05-08 16:34:49 +0200412
413 if (s->aio) {
Stefan Hajnoczi85ebd382014-05-08 16:34:50 +0200414 win32_aio_detach_aio_context(s->aio, bdrv_get_aio_context(bs));
Stefan Hajnoczi99cc5982014-05-08 16:34:49 +0200415 win32_aio_cleanup(s->aio);
416 s->aio = NULL;
417 }
418
ths223d4672007-12-15 17:28:36 +0000419 CloseHandle(s->hfile);
Kevin Wolf8bfea152014-04-11 19:16:36 +0200420 if (bs->open_flags & BDRV_O_TEMPORARY) {
421 unlink(bs->filename);
422 }
ths223d4672007-12-15 17:28:36 +0000423}
424
425static int raw_truncate(BlockDriverState *bs, int64_t offset)
426{
427 BDRVRawState *s = bs->opaque;
blueswir1b9e82a52009-04-05 18:03:31 +0000428 LONG low, high;
Fabien Chouteaufbcad042012-12-10 12:56:22 +0100429 DWORD dwPtrLow;
ths223d4672007-12-15 17:28:36 +0000430
431 low = offset;
432 high = offset >> 32;
Fabien Chouteaufbcad042012-12-10 12:56:22 +0100433
434 /*
435 * An error has occurred if the return value is INVALID_SET_FILE_POINTER
436 * and GetLastError doesn't return NO_ERROR.
437 */
438 dwPtrLow = SetFilePointer(s->hfile, low, &high, FILE_BEGIN);
439 if (dwPtrLow == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
Stefan Weilfccedc62012-12-17 20:40:01 +0100440 fprintf(stderr, "SetFilePointer error: %lu\n", GetLastError());
ths223d4672007-12-15 17:28:36 +0000441 return -EIO;
Fabien Chouteaufbcad042012-12-10 12:56:22 +0100442 }
443 if (SetEndOfFile(s->hfile) == 0) {
Stefan Weilfccedc62012-12-17 20:40:01 +0100444 fprintf(stderr, "SetEndOfFile error: %lu\n", GetLastError());
Fabien Chouteaufbcad042012-12-10 12:56:22 +0100445 return -EIO;
446 }
ths223d4672007-12-15 17:28:36 +0000447 return 0;
448}
449
450static int64_t raw_getlength(BlockDriverState *bs)
451{
452 BDRVRawState *s = bs->opaque;
453 LARGE_INTEGER l;
454 ULARGE_INTEGER available, total, total_free;
455 DISK_GEOMETRY_EX dg;
456 DWORD count;
457 BOOL status;
458
459 switch(s->type) {
460 case FTYPE_FILE:
blueswir1b9e82a52009-04-05 18:03:31 +0000461 l.LowPart = GetFileSize(s->hfile, (PDWORD)&l.HighPart);
ths223d4672007-12-15 17:28:36 +0000462 if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
463 return -EIO;
464 break;
465 case FTYPE_CD:
466 if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
467 return -EIO;
468 l.QuadPart = total.QuadPart;
469 break;
470 case FTYPE_HARDDISK:
471 status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
472 NULL, 0, &dg, sizeof(dg), &count, NULL);
473 if (status != 0) {
474 l = dg.DiskSize;
475 }
476 break;
477 default:
478 return -EIO;
479 }
480 return l.QuadPart;
481}
482
Fam Zheng4a1d5e12011-07-12 19:56:39 +0800483static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
484{
485 typedef DWORD (WINAPI * get_compressed_t)(const char *filename,
486 DWORD * high);
487 get_compressed_t get_compressed;
488 struct _stati64 st;
489 const char *filename = bs->filename;
490 /* WinNT support GetCompressedFileSize to determine allocate size */
491 get_compressed =
492 (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"),
493 "GetCompressedFileSizeA");
494 if (get_compressed) {
495 DWORD high, low;
496 low = get_compressed(filename, &high);
497 if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR) {
498 return (((int64_t) high) << 32) + low;
499 }
500 }
501
502 if (_stati64(filename, &st) < 0) {
503 return -1;
504 }
505 return st.st_size;
506}
507
Chunyan Liuddef7692014-06-05 17:21:02 +0800508static int raw_create(const char *filename, QemuOpts *opts, Error **errp)
ths223d4672007-12-15 17:28:36 +0000509{
510 int fd;
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200511 int64_t total_size = 0;
ths223d4672007-12-15 17:28:36 +0000512
Max Reitzd5546c52014-03-05 22:41:40 +0100513 strstart(filename, "file:", &filename);
514
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200515 /* Read out options */
Hu Tao180e9522014-09-10 17:05:46 +0800516 total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
517 BDRV_SECTOR_SIZE);
ths223d4672007-12-15 17:28:36 +0000518
Corey Bryant6165f4d2012-08-14 16:43:45 -0400519 fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
520 0644);
Max Reitzc6252b72013-10-10 15:44:02 +0200521 if (fd < 0) {
522 error_setg_errno(errp, errno, "Could not create file");
ths223d4672007-12-15 17:28:36 +0000523 return -EIO;
Max Reitzc6252b72013-10-10 15:44:02 +0200524 }
ths223d4672007-12-15 17:28:36 +0000525 set_sparse(fd);
Hu Tao180e9522014-09-10 17:05:46 +0800526 ftruncate(fd, total_size);
Corey Bryant2e1e79d2012-08-14 16:43:46 -0400527 qemu_close(fd);
ths223d4672007-12-15 17:28:36 +0000528 return 0;
529}
530
Chunyan Liuddef7692014-06-05 17:21:02 +0800531
532static QemuOptsList raw_create_opts = {
533 .name = "raw-create-opts",
534 .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head),
535 .desc = {
536 {
537 .name = BLOCK_OPT_SIZE,
538 .type = QEMU_OPT_SIZE,
539 .help = "Virtual disk size"
540 },
541 { /* end of list */ }
542 }
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200543};
544
Max Reitz5f535a92014-12-02 18:32:41 +0100545BlockDriver bdrv_file = {
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200546 .format_name = "file",
547 .protocol_name = "file",
aliguorif1b2f712009-04-07 18:23:51 +0000548 .instance_size = sizeof(BDRVRawState),
BenoƮt Canet030be322013-09-24 17:07:04 +0200549 .bdrv_needs_filename = true,
Max Reitz7dc74db2014-03-05 22:41:39 +0100550 .bdrv_parse_filename = raw_parse_filename,
Chunyan Liuddef7692014-06-05 17:21:02 +0800551 .bdrv_file_open = raw_open,
Eric Blake2914a1d2016-06-23 16:37:16 -0600552 .bdrv_refresh_limits = raw_probe_alignment,
Chunyan Liuddef7692014-06-05 17:21:02 +0800553 .bdrv_close = raw_close,
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800554 .bdrv_create = raw_create,
Peter Lieven3ac21622013-06-28 12:47:42 +0200555 .bdrv_has_zero_init = bdrv_has_zero_init_1,
Kevin Wolfc68b89a2011-11-10 17:25:44 +0100556
Paolo Bonzinifc4edb82012-06-09 04:48:28 +0200557 .bdrv_aio_readv = raw_aio_readv,
558 .bdrv_aio_writev = raw_aio_writev,
559 .bdrv_aio_flush = raw_aio_flush,
Kevin Wolfc68b89a2011-11-10 17:25:44 +0100560
aliguorif1b2f712009-04-07 18:23:51 +0000561 .bdrv_truncate = raw_truncate,
562 .bdrv_getlength = raw_getlength,
Fam Zheng4a1d5e12011-07-12 19:56:39 +0800563 .bdrv_get_allocated_file_size
564 = raw_get_allocated_file_size,
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200565
Chunyan Liuddef7692014-06-05 17:21:02 +0800566 .create_opts = &raw_create_opts,
ths223d4672007-12-15 17:28:36 +0000567};
568
569/***********************************************/
570/* host device */
571
572static int find_cdrom(char *cdrom_name, int cdrom_name_size)
573{
574 char drives[256], *pdrv = drives;
575 UINT type;
576
577 memset(drives, 0, sizeof(drives));
578 GetLogicalDriveStrings(sizeof(drives), drives);
579 while(pdrv[0] != '\0') {
580 type = GetDriveType(pdrv);
581 switch(type) {
582 case DRIVE_CDROM:
583 snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
584 return 0;
585 break;
586 }
587 pdrv += lstrlen(pdrv) + 1;
588 }
589 return -1;
590}
591
592static int find_device_type(BlockDriverState *bs, const char *filename)
593{
594 BDRVRawState *s = bs->opaque;
595 UINT type;
596 const char *p;
597
598 if (strstart(filename, "\\\\.\\", &p) ||
599 strstart(filename, "//./", &p)) {
600 if (stristart(p, "PhysicalDrive", NULL))
601 return FTYPE_HARDDISK;
602 snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
603 type = GetDriveType(s->drive_path);
aliguori3060cd12009-04-07 01:24:53 +0000604 switch (type) {
605 case DRIVE_REMOVABLE:
606 case DRIVE_FIXED:
607 return FTYPE_HARDDISK;
608 case DRIVE_CDROM:
ths223d4672007-12-15 17:28:36 +0000609 return FTYPE_CD;
aliguori3060cd12009-04-07 01:24:53 +0000610 default:
ths223d4672007-12-15 17:28:36 +0000611 return FTYPE_FILE;
aliguori3060cd12009-04-07 01:24:53 +0000612 }
ths223d4672007-12-15 17:28:36 +0000613 } else {
614 return FTYPE_FILE;
615 }
616}
617
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200618static int hdev_probe_device(const char *filename)
619{
620 if (strstart(filename, "/dev/cdrom", NULL))
621 return 100;
622 if (is_windows_drive(filename))
623 return 100;
624 return 0;
625}
626
Max Reitz57ed25b2014-03-08 00:39:45 +0100627static void hdev_parse_filename(const char *filename, QDict *options,
628 Error **errp)
629{
630 /* The prefix is optional, just as for "file". */
631 strstart(filename, "host_device:", &filename);
632
633 qdict_put_obj(options, "filename", QOBJECT(qstring_from_str(filename)));
634}
635
Max Reitz015a1032013-09-05 14:22:29 +0200636static int hdev_open(BlockDriverState *bs, QDict *options, int flags,
637 Error **errp)
ths223d4672007-12-15 17:28:36 +0000638{
639 BDRVRawState *s = bs->opaque;
640 int access_flags, create_flags;
Stefan Weil68dc0362013-09-01 22:59:25 +0200641 int ret = 0;
ths223d4672007-12-15 17:28:36 +0000642 DWORD overlapped;
643 char device_name[64];
Stefan Weil68dc0362013-09-01 22:59:25 +0200644
645 Error *local_err = NULL;
646 const char *filename;
647
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800648 QemuOpts *opts = qemu_opts_create(&raw_runtime_opts, NULL, 0,
649 &error_abort);
Stefan Weil68dc0362013-09-01 22:59:25 +0200650 qemu_opts_absorb_qdict(opts, options, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100651 if (local_err) {
Max Reitzc6252b72013-10-10 15:44:02 +0200652 error_propagate(errp, local_err);
Stefan Weil68dc0362013-09-01 22:59:25 +0200653 ret = -EINVAL;
654 goto done;
655 }
656
657 filename = qemu_opt_get(opts, "filename");
ths223d4672007-12-15 17:28:36 +0000658
659 if (strstart(filename, "/dev/cdrom", NULL)) {
Stefan Weil68dc0362013-09-01 22:59:25 +0200660 if (find_cdrom(device_name, sizeof(device_name)) < 0) {
Max Reitzc6252b72013-10-10 15:44:02 +0200661 error_setg(errp, "Could not open CD-ROM drive");
Stefan Weil68dc0362013-09-01 22:59:25 +0200662 ret = -ENOENT;
663 goto done;
664 }
ths223d4672007-12-15 17:28:36 +0000665 filename = device_name;
666 } else {
667 /* transform drive letters into device name */
668 if (((filename[0] >= 'a' && filename[0] <= 'z') ||
669 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
670 filename[1] == ':' && filename[2] == '\0') {
671 snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
672 filename = device_name;
673 }
674 }
675 s->type = find_device_type(bs, filename);
676
Jeff Cody6a8dc042012-09-20 15:13:21 -0400677 raw_parse_flags(flags, &access_flags, &overlapped);
678
ths223d4672007-12-15 17:28:36 +0000679 create_flags = OPEN_EXISTING;
680
ths223d4672007-12-15 17:28:36 +0000681 s->hfile = CreateFile(filename, access_flags,
682 FILE_SHARE_READ, NULL,
683 create_flags, overlapped, NULL);
684 if (s->hfile == INVALID_HANDLE_VALUE) {
685 int err = GetLastError();
686
Stefan Weil68dc0362013-09-01 22:59:25 +0200687 if (err == ERROR_ACCESS_DENIED) {
688 ret = -EACCES;
689 } else {
Max Reitz45d57f62013-10-11 14:30:16 +0200690 ret = -EINVAL;
Stefan Weil68dc0362013-09-01 22:59:25 +0200691 }
Max Reitz45d57f62013-10-11 14:30:16 +0200692 error_setg_errno(errp, -ret, "Could not open device");
Stefan Weil68dc0362013-09-01 22:59:25 +0200693 goto done;
ths223d4672007-12-15 17:28:36 +0000694 }
Stefan Weil68dc0362013-09-01 22:59:25 +0200695
696done:
697 qemu_opts_del(opts);
698 return ret;
ths223d4672007-12-15 17:28:36 +0000699}
700
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500701static BlockDriver bdrv_host_device = {
aurel32e60f4692009-03-07 22:00:29 +0000702 .format_name = "host_device",
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200703 .protocol_name = "host_device",
aurel32e60f4692009-03-07 22:00:29 +0000704 .instance_size = sizeof(BDRVRawState),
BenoƮt Canet030be322013-09-24 17:07:04 +0200705 .bdrv_needs_filename = true,
Max Reitz57ed25b2014-03-08 00:39:45 +0100706 .bdrv_parse_filename = hdev_parse_filename,
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200707 .bdrv_probe_device = hdev_probe_device,
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200708 .bdrv_file_open = hdev_open,
aurel32e60f4692009-03-07 22:00:29 +0000709 .bdrv_close = raw_close,
ths223d4672007-12-15 17:28:36 +0000710
Paolo Bonzinifc4edb82012-06-09 04:48:28 +0200711 .bdrv_aio_readv = raw_aio_readv,
712 .bdrv_aio_writev = raw_aio_writev,
713 .bdrv_aio_flush = raw_aio_flush,
Kevin Wolfc68b89a2011-11-10 17:25:44 +0100714
Stefan Hajnoczi85ebd382014-05-08 16:34:50 +0200715 .bdrv_detach_aio_context = raw_detach_aio_context,
716 .bdrv_attach_aio_context = raw_attach_aio_context,
717
Kevin Wolfb94a2612013-10-29 12:18:58 +0100718 .bdrv_getlength = raw_getlength,
719 .has_variable_length = true,
720
Fam Zheng4a1d5e12011-07-12 19:56:39 +0800721 .bdrv_get_allocated_file_size
722 = raw_get_allocated_file_size,
ths223d4672007-12-15 17:28:36 +0000723};
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500724
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200725static void bdrv_file_init(void)
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500726{
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200727 bdrv_register(&bdrv_file);
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500728 bdrv_register(&bdrv_host_device);
729}
730
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200731block_init(bdrv_file_init);