qemu-iotests: Remove MIG_SOCKET from non-migration tests
[qemu/ar7.git] / block / file-win32.c
blob3c67db43364fdc5a1d66c7c4588bc4e8cd3a06d4
1 /*
2 * Block driver for RAW files (win32)
4 * Copyright (c) 2006 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #include "qemu/osdep.h"
26 #include "qapi/error.h"
27 #include "qemu/cutils.h"
28 #include "block/block_int.h"
29 #include "qemu/module.h"
30 #include "qemu/option.h"
31 #include "block/raw-aio.h"
32 #include "trace.h"
33 #include "block/thread-pool.h"
34 #include "qemu/iov.h"
35 #include "qapi/qmp/qdict.h"
36 #include "qapi/qmp/qstring.h"
37 #include <windows.h>
38 #include <winioctl.h>
40 #define FTYPE_FILE 0
41 #define FTYPE_CD 1
42 #define FTYPE_HARDDISK 2
44 typedef 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;
54 typedef struct BDRVRawState {
55 HANDLE hfile;
56 int type;
57 char drive_path[16]; /* format: "d:\" */
58 QEMUWin32AIOState *aio;
59 } BDRVRawState;
62 * Read/writes the data to/from a given linear buffer.
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.
67 static size_t handle_aiocb_rw(RawWin32AIOData *aiocb)
69 size_t offset = 0;
70 int i;
72 for (i = 0; i < aiocb->aio_niov; i++) {
73 OVERLAPPED ov;
74 DWORD ret, ret_count, len;
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);
87 if (!ret) {
88 ret_count = 0;
90 if (ret_count != len) {
91 offset += ret_count;
92 break;
94 offset += len;
97 return offset;
100 static int aio_worker(void *arg)
102 RawWin32AIOData *aiocb = arg;
103 ssize_t ret = 0;
104 size_t count;
106 switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
107 case QEMU_AIO_READ:
108 count = handle_aiocb_rw(aiocb);
109 if (count < aiocb->aio_nbytes) {
110 /* 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);
115 count = aiocb->aio_nbytes;
117 if (count == aiocb->aio_nbytes) {
118 ret = 0;
119 } else {
120 ret = -EINVAL;
122 break;
123 case QEMU_AIO_WRITE:
124 count = handle_aiocb_rw(aiocb);
125 if (count == aiocb->aio_nbytes) {
126 ret = 0;
127 } else {
128 ret = -EINVAL;
130 break;
131 case QEMU_AIO_FLUSH:
132 if (!FlushFileBuffers(aiocb->hfile)) {
133 return -EIO;
135 break;
136 default:
137 fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
138 ret = -EINVAL;
139 break;
142 g_free(aiocb);
143 return ret;
146 static BlockAIOCB *paio_submit(BlockDriverState *bs, HANDLE hfile,
147 int64_t offset, QEMUIOVector *qiov, int count,
148 BlockCompletionFunc *cb, void *opaque, int type)
150 RawWin32AIOData *acb = g_new(RawWin32AIOData, 1);
151 ThreadPool *pool;
153 acb->bs = bs;
154 acb->hfile = hfile;
155 acb->aio_type = type;
157 if (qiov) {
158 acb->aio_iov = qiov->iov;
159 acb->aio_niov = qiov->niov;
160 assert(qiov->size == count);
162 acb->aio_nbytes = count;
163 acb->aio_offset = offset;
165 trace_paio_submit(acb, opaque, offset, count, type);
166 pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
167 return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque);
170 int qemu_ftruncate64(int fd, int64_t length)
172 LARGE_INTEGER li;
173 DWORD dw;
174 LONG high;
175 HANDLE h;
176 BOOL res;
178 if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
179 return -1;
181 h = (HANDLE)_get_osfhandle(fd);
183 /* get current position, ftruncate do not change position */
184 li.HighPart = 0;
185 li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
186 if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
187 return -1;
190 high = length >> 32;
191 dw = SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN);
192 if (dw == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
193 return -1;
195 res = SetEndOfFile(h);
197 /* back to old position */
198 SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
199 return res ? 0 : -1;
202 static int set_sparse(int fd)
204 DWORD returned;
205 return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
206 NULL, 0, NULL, 0, &returned, NULL);
209 static void raw_detach_aio_context(BlockDriverState *bs)
211 BDRVRawState *s = bs->opaque;
213 if (s->aio) {
214 win32_aio_detach_aio_context(s->aio, bdrv_get_aio_context(bs));
218 static void raw_attach_aio_context(BlockDriverState *bs,
219 AioContext *new_context)
221 BDRVRawState *s = bs->opaque;
223 if (s->aio) {
224 win32_aio_attach_aio_context(s->aio, new_context);
228 static void raw_probe_alignment(BlockDriverState *bs, Error **errp)
230 BDRVRawState *s = bs->opaque;
231 DWORD sectorsPerCluster, freeClusters, totalClusters, count;
232 DISK_GEOMETRY_EX dg;
233 BOOL status;
235 if (s->type == FTYPE_CD) {
236 bs->bl.request_alignment = 2048;
237 return;
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) {
243 bs->bl.request_alignment = dg.Geometry.BytesPerSector;
244 return;
246 /* try GetDiskFreeSpace too */
249 if (s->drive_path[0]) {
250 GetDiskFreeSpace(s->drive_path, &sectorsPerCluster,
251 &dg.Geometry.BytesPerSector,
252 &freeClusters, &totalClusters);
253 bs->bl.request_alignment = dg.Geometry.BytesPerSector;
254 return;
257 /* XXX Does Windows support AIO on less than 512-byte alignment? */
258 bs->bl.request_alignment = 512;
261 static void raw_parse_flags(int flags, bool use_aio, int *access_flags,
262 DWORD *overlapped)
264 assert(access_flags != NULL);
265 assert(overlapped != NULL);
267 if (flags & BDRV_O_RDWR) {
268 *access_flags = GENERIC_READ | GENERIC_WRITE;
269 } else {
270 *access_flags = GENERIC_READ;
273 *overlapped = FILE_ATTRIBUTE_NORMAL;
274 if (use_aio) {
275 *overlapped |= FILE_FLAG_OVERLAPPED;
277 if (flags & BDRV_O_NOCACHE) {
278 *overlapped |= FILE_FLAG_NO_BUFFERING;
282 static void raw_parse_filename(const char *filename, QDict *options,
283 Error **errp)
285 bdrv_parse_filename_strip_prefix(filename, "file:", options);
288 static QemuOptsList raw_runtime_opts = {
289 .name = "raw",
290 .head = QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head),
291 .desc = {
293 .name = "filename",
294 .type = QEMU_OPT_STRING,
295 .help = "File name of the image",
298 .name = "aio",
299 .type = QEMU_OPT_STRING,
300 .help = "host AIO implementation (threads, native)",
302 { /* end of list */ }
306 static bool get_aio_option(QemuOpts *opts, int flags, Error **errp)
308 BlockdevAioOptions aio, aio_default;
310 aio_default = (flags & BDRV_O_NATIVE_AIO) ? BLOCKDEV_AIO_OPTIONS_NATIVE
311 : BLOCKDEV_AIO_OPTIONS_THREADS;
312 aio = qapi_enum_parse(&BlockdevAioOptions_lookup, qemu_opt_get(opts, "aio"),
313 aio_default, errp);
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");
323 return false;
326 static int raw_open(BlockDriverState *bs, QDict *options, int flags,
327 Error **errp)
329 BDRVRawState *s = bs->opaque;
330 int access_flags;
331 DWORD overlapped;
332 QemuOpts *opts;
333 Error *local_err = NULL;
334 const char *filename;
335 bool use_aio;
336 int ret;
338 s->type = FTYPE_FILE;
340 opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort);
341 qemu_opts_absorb_qdict(opts, options, &local_err);
342 if (local_err) {
343 error_propagate(errp, local_err);
344 ret = -EINVAL;
345 goto fail;
348 if (qdict_get_try_bool(options, "locking", false)) {
349 error_setg(errp, "locking=on is not supported on Windows");
350 ret = -EINVAL;
351 goto fail;
354 filename = qemu_opt_get(opts, "filename");
356 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;
363 raw_parse_flags(flags, use_aio, &access_flags, &overlapped);
365 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]);
376 s->hfile = CreateFile(filename, access_flags,
377 FILE_SHARE_READ, NULL,
378 OPEN_EXISTING, overlapped, NULL);
379 if (s->hfile == INVALID_HANDLE_VALUE) {
380 int err = GetLastError();
382 error_setg_win32(errp, err, "Could not open '%s'", filename);
383 if (err == ERROR_ACCESS_DENIED) {
384 ret = -EACCES;
385 } else {
386 ret = -EINVAL;
388 goto fail;
391 if (use_aio) {
392 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;
400 ret = win32_aio_attach(s->aio, s->hfile);
401 if (ret < 0) {
402 win32_aio_cleanup(s->aio);
403 CloseHandle(s->hfile);
404 error_setg_errno(errp, -ret, "Could not enable AIO");
405 goto fail;
408 win32_aio_attach_aio_context(s->aio, bdrv_get_aio_context(bs));
411 ret = 0;
412 fail:
413 qemu_opts_del(opts);
414 return ret;
417 static BlockAIOCB *raw_aio_preadv(BlockDriverState *bs,
418 uint64_t offset, uint64_t bytes,
419 QEMUIOVector *qiov, int flags,
420 BlockCompletionFunc *cb, void *opaque)
422 BDRVRawState *s = bs->opaque;
423 if (s->aio) {
424 return win32_aio_submit(bs, s->aio, s->hfile, offset, bytes, qiov,
425 cb, opaque, QEMU_AIO_READ);
426 } else {
427 return paio_submit(bs, s->hfile, offset, qiov, bytes,
428 cb, opaque, QEMU_AIO_READ);
432 static BlockAIOCB *raw_aio_pwritev(BlockDriverState *bs,
433 uint64_t offset, uint64_t bytes,
434 QEMUIOVector *qiov, int flags,
435 BlockCompletionFunc *cb, void *opaque)
437 BDRVRawState *s = bs->opaque;
438 if (s->aio) {
439 return win32_aio_submit(bs, s->aio, s->hfile, offset, bytes, qiov,
440 cb, opaque, QEMU_AIO_WRITE);
441 } else {
442 return paio_submit(bs, s->hfile, offset, qiov, bytes,
443 cb, opaque, QEMU_AIO_WRITE);
447 static BlockAIOCB *raw_aio_flush(BlockDriverState *bs,
448 BlockCompletionFunc *cb, void *opaque)
450 BDRVRawState *s = bs->opaque;
451 return paio_submit(bs, s->hfile, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
454 static void raw_close(BlockDriverState *bs)
456 BDRVRawState *s = bs->opaque;
458 if (s->aio) {
459 win32_aio_detach_aio_context(s->aio, bdrv_get_aio_context(bs));
460 win32_aio_cleanup(s->aio);
461 s->aio = NULL;
464 CloseHandle(s->hfile);
465 if (bs->open_flags & BDRV_O_TEMPORARY) {
466 unlink(bs->filename);
470 static int raw_truncate(BlockDriverState *bs, int64_t offset,
471 PreallocMode prealloc, Error **errp)
473 BDRVRawState *s = bs->opaque;
474 LONG low, high;
475 DWORD dwPtrLow;
477 if (prealloc != PREALLOC_MODE_OFF) {
478 error_setg(errp, "Unsupported preallocation mode '%s'",
479 PreallocMode_str(prealloc));
480 return -ENOTSUP;
483 low = offset;
484 high = offset >> 32;
487 * An error has occurred if the return value is INVALID_SET_FILE_POINTER
488 * and GetLastError doesn't return NO_ERROR.
490 dwPtrLow = SetFilePointer(s->hfile, low, &high, FILE_BEGIN);
491 if (dwPtrLow == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
492 error_setg_win32(errp, GetLastError(), "SetFilePointer error");
493 return -EIO;
495 if (SetEndOfFile(s->hfile) == 0) {
496 error_setg_win32(errp, GetLastError(), "SetEndOfFile error");
497 return -EIO;
499 return 0;
502 static int64_t raw_getlength(BlockDriverState *bs)
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;
511 switch(s->type) {
512 case FTYPE_FILE:
513 l.LowPart = GetFileSize(s->hfile, (PDWORD)&l.HighPart);
514 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;
528 break;
529 default:
530 return -EIO;
532 return l.QuadPart;
535 static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
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;
554 if (_stati64(filename, &st) < 0) {
555 return -1;
557 return st.st_size;
560 static int raw_co_create(BlockdevCreateOptions *options, Error **errp)
562 BlockdevCreateOptionsFile *file_opts;
563 int fd;
565 assert(options->driver == BLOCKDEV_DRIVER_FILE);
566 file_opts = &options->u.file;
568 if (file_opts->has_preallocation) {
569 error_setg(errp, "Preallocation is not supported on Windows");
570 return -EINVAL;
572 if (file_opts->has_nocow) {
573 error_setg(errp, "nocow is not supported on Windows");
574 return -EINVAL;
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;
583 set_sparse(fd);
584 ftruncate(fd, file_opts->size);
585 qemu_close(fd);
587 return 0;
590 static int coroutine_fn raw_co_create_opts(const char *filename, QemuOpts *opts,
591 Error **errp)
593 BlockdevCreateOptions options;
594 int64_t total_size = 0;
596 strstart(filename, "file:", &filename);
598 /* Read out options */
599 total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
600 BDRV_SECTOR_SIZE);
602 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,
611 return raw_co_create(&options, errp);
614 static QemuOptsList raw_create_opts = {
615 .name = "raw-create-opts",
616 .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head),
617 .desc = {
619 .name = BLOCK_OPT_SIZE,
620 .type = QEMU_OPT_SIZE,
621 .help = "Virtual disk size"
623 { /* end of list */ }
627 BlockDriver bdrv_file = {
628 .format_name = "file",
629 .protocol_name = "file",
630 .instance_size = sizeof(BDRVRawState),
631 .bdrv_needs_filename = true,
632 .bdrv_parse_filename = raw_parse_filename,
633 .bdrv_file_open = raw_open,
634 .bdrv_refresh_limits = raw_probe_alignment,
635 .bdrv_close = raw_close,
636 .bdrv_co_create_opts = raw_co_create_opts,
637 .bdrv_has_zero_init = bdrv_has_zero_init_1,
639 .bdrv_aio_preadv = raw_aio_preadv,
640 .bdrv_aio_pwritev = raw_aio_pwritev,
641 .bdrv_aio_flush = raw_aio_flush,
643 .bdrv_truncate = raw_truncate,
644 .bdrv_getlength = raw_getlength,
645 .bdrv_get_allocated_file_size
646 = raw_get_allocated_file_size,
648 .create_opts = &raw_create_opts,
651 /***********************************************/
652 /* host device */
654 static int find_cdrom(char *cdrom_name, int cdrom_name_size)
656 char drives[256], *pdrv = drives;
657 UINT type;
659 memset(drives, 0, sizeof(drives));
660 GetLogicalDriveStrings(sizeof(drives), drives);
661 while(pdrv[0] != '\0') {
662 type = GetDriveType(pdrv);
663 switch(type) {
664 case DRIVE_CDROM:
665 snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
666 return 0;
667 break;
669 pdrv += lstrlen(pdrv) + 1;
671 return -1;
674 static int find_device_type(BlockDriverState *bs, const char *filename)
676 BDRVRawState *s = bs->opaque;
677 UINT type;
678 const char *p;
680 if (strstart(filename, "\\\\.\\", &p) ||
681 strstart(filename, "//./", &p)) {
682 if (stristart(p, "PhysicalDrive", NULL))
683 return FTYPE_HARDDISK;
684 snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
685 type = GetDriveType(s->drive_path);
686 switch (type) {
687 case DRIVE_REMOVABLE:
688 case DRIVE_FIXED:
689 return FTYPE_HARDDISK;
690 case DRIVE_CDROM:
691 return FTYPE_CD;
692 default:
693 return FTYPE_FILE;
695 } else {
696 return FTYPE_FILE;
700 static int hdev_probe_device(const char *filename)
702 if (strstart(filename, "/dev/cdrom", NULL))
703 return 100;
704 if (is_windows_drive(filename))
705 return 100;
706 return 0;
709 static void hdev_parse_filename(const char *filename, QDict *options,
710 Error **errp)
712 bdrv_parse_filename_strip_prefix(filename, "host_device:", options);
715 static void hdev_refresh_limits(BlockDriverState *bs, Error **errp)
717 /* XXX Does Windows support AIO on less than 512-byte alignment? */
718 bs->bl.request_alignment = 512;
721 static int hdev_open(BlockDriverState *bs, QDict *options, int flags,
722 Error **errp)
724 BDRVRawState *s = bs->opaque;
725 int access_flags, create_flags;
726 int ret = 0;
727 DWORD overlapped;
728 char device_name[64];
730 Error *local_err = NULL;
731 const char *filename;
732 bool use_aio;
734 QemuOpts *opts = qemu_opts_create(&raw_runtime_opts, NULL, 0,
735 &error_abort);
736 qemu_opts_absorb_qdict(opts, options, &local_err);
737 if (local_err) {
738 error_propagate(errp, local_err);
739 ret = -EINVAL;
740 goto done;
743 filename = qemu_opt_get(opts, "filename");
745 use_aio = get_aio_option(opts, flags, &local_err);
746 if (!local_err && use_aio) {
747 error_setg(&local_err, "AIO is not supported on Windows host devices");
749 if (local_err) {
750 error_propagate(errp, local_err);
751 ret = -EINVAL;
752 goto done;
755 if (strstart(filename, "/dev/cdrom", NULL)) {
756 if (find_cdrom(device_name, sizeof(device_name)) < 0) {
757 error_setg(errp, "Could not open CD-ROM drive");
758 ret = -ENOENT;
759 goto done;
761 filename = device_name;
762 } else {
763 /* transform drive letters into device name */
764 if (((filename[0] >= 'a' && filename[0] <= 'z') ||
765 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
766 filename[1] == ':' && filename[2] == '\0') {
767 snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
768 filename = device_name;
771 s->type = find_device_type(bs, filename);
773 raw_parse_flags(flags, use_aio, &access_flags, &overlapped);
775 create_flags = OPEN_EXISTING;
777 s->hfile = CreateFile(filename, access_flags,
778 FILE_SHARE_READ, NULL,
779 create_flags, overlapped, NULL);
780 if (s->hfile == INVALID_HANDLE_VALUE) {
781 int err = GetLastError();
783 if (err == ERROR_ACCESS_DENIED) {
784 ret = -EACCES;
785 } else {
786 ret = -EINVAL;
788 error_setg_errno(errp, -ret, "Could not open device");
789 goto done;
792 done:
793 qemu_opts_del(opts);
794 return ret;
797 static BlockDriver bdrv_host_device = {
798 .format_name = "host_device",
799 .protocol_name = "host_device",
800 .instance_size = sizeof(BDRVRawState),
801 .bdrv_needs_filename = true,
802 .bdrv_parse_filename = hdev_parse_filename,
803 .bdrv_probe_device = hdev_probe_device,
804 .bdrv_file_open = hdev_open,
805 .bdrv_close = raw_close,
806 .bdrv_refresh_limits = hdev_refresh_limits,
808 .bdrv_aio_preadv = raw_aio_preadv,
809 .bdrv_aio_pwritev = raw_aio_pwritev,
810 .bdrv_aio_flush = raw_aio_flush,
812 .bdrv_detach_aio_context = raw_detach_aio_context,
813 .bdrv_attach_aio_context = raw_attach_aio_context,
815 .bdrv_getlength = raw_getlength,
816 .has_variable_length = true,
818 .bdrv_get_allocated_file_size
819 = raw_get_allocated_file_size,
822 static void bdrv_file_init(void)
824 bdrv_register(&bdrv_file);
825 bdrv_register(&bdrv_host_device);
828 block_init(bdrv_file_init);