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
24 #include "qemu/osdep.h"
25 #include "qapi/error.h"
26 #include "qemu/cutils.h"
27 #include "qemu/timer.h"
28 #include "block/block_int.h"
29 #include "qemu/module.h"
30 #include "block/raw-aio.h"
32 #include "block/thread-pool.h"
34 #include "qapi/qmp/qstring.h"
40 #define FTYPE_HARDDISK 2
42 typedef struct RawWin32AIOData {
45 struct iovec *aio_iov;
52 typedef struct BDRVRawState {
55 char drive_path[16]; /* format: "d:\" */
56 QEMUWin32AIOState *aio;
60 * Read/writes the data to/from a given linear buffer.
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.
65 static size_t handle_aiocb_rw(RawWin32AIOData *aiocb)
70 for (i = 0; i < aiocb->aio_niov; i++) {
72 DWORD ret, ret_count, len;
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);
82 ret = ReadFile(aiocb->hfile, aiocb->aio_iov[i].iov_base,
83 len, &ret_count, &ov);
88 if (ret_count != len) {
98 static int aio_worker(void *arg)
100 RawWin32AIOData *aiocb = arg;
104 switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
106 count = handle_aiocb_rw(aiocb);
107 if (count < aiocb->aio_nbytes) {
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);
113 count = aiocb->aio_nbytes;
115 if (count == aiocb->aio_nbytes) {
122 count = handle_aiocb_rw(aiocb);
123 if (count == aiocb->aio_nbytes) {
130 if (!FlushFileBuffers(aiocb->hfile)) {
135 fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
144 static BlockAIOCB *paio_submit(BlockDriverState *bs, HANDLE hfile,
145 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
146 BlockCompletionFunc *cb, void *opaque, int type)
148 RawWin32AIOData *acb = g_new(RawWin32AIOData, 1);
153 acb->aio_type = type;
156 acb->aio_iov = qiov->iov;
157 acb->aio_niov = qiov->niov;
159 acb->aio_nbytes = nb_sectors * 512;
160 acb->aio_offset = sector_num * 512;
162 trace_paio_submit(acb, opaque, sector_num, nb_sectors, type);
163 pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
164 return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque);
167 int qemu_ftruncate64(int fd, int64_t length)
175 if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
178 h = (HANDLE)_get_osfhandle(fd);
180 /* get current position, ftruncate do not change position */
182 li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
183 if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
188 dw = SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN);
189 if (dw == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
192 res = SetEndOfFile(h);
194 /* back to old position */
195 SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
199 static int set_sparse(int fd)
202 return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
203 NULL, 0, NULL, 0, &returned, NULL);
206 static void raw_detach_aio_context(BlockDriverState *bs)
208 BDRVRawState *s = bs->opaque;
211 win32_aio_detach_aio_context(s->aio, bdrv_get_aio_context(bs));
215 static void raw_attach_aio_context(BlockDriverState *bs,
216 AioContext *new_context)
218 BDRVRawState *s = bs->opaque;
221 win32_aio_attach_aio_context(s->aio, new_context);
225 static void raw_probe_alignment(BlockDriverState *bs, Error **errp)
227 BDRVRawState *s = bs->opaque;
228 DWORD sectorsPerCluster, freeClusters, totalClusters, count;
232 if (s->type == FTYPE_CD) {
233 bs->bl.request_alignment = 2048;
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);
240 bs->bl.request_alignment = dg.Geometry.BytesPerSector;
243 /* try GetDiskFreeSpace too */
246 if (s->drive_path[0]) {
247 GetDiskFreeSpace(s->drive_path, §orsPerCluster,
248 &dg.Geometry.BytesPerSector,
249 &freeClusters, &totalClusters);
250 bs->bl.request_alignment = dg.Geometry.BytesPerSector;
254 static void raw_parse_flags(int flags, int *access_flags, DWORD *overlapped)
256 assert(access_flags != NULL);
257 assert(overlapped != NULL);
259 if (flags & BDRV_O_RDWR) {
260 *access_flags = GENERIC_READ | GENERIC_WRITE;
262 *access_flags = GENERIC_READ;
265 *overlapped = FILE_ATTRIBUTE_NORMAL;
266 if (flags & BDRV_O_NATIVE_AIO) {
267 *overlapped |= FILE_FLAG_OVERLAPPED;
269 if (flags & BDRV_O_NOCACHE) {
270 *overlapped |= FILE_FLAG_NO_BUFFERING;
274 static void raw_parse_filename(const char *filename, QDict *options,
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);
282 qdict_put_obj(options, "filename", QOBJECT(qstring_from_str(filename)));
285 static QemuOptsList raw_runtime_opts = {
287 .head = QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head),
291 .type = QEMU_OPT_STRING,
292 .help = "File name of the image",
294 { /* end of list */ }
298 static int raw_open(BlockDriverState *bs, QDict *options, int flags,
301 BDRVRawState *s = bs->opaque;
305 Error *local_err = NULL;
306 const char *filename;
309 s->type = FTYPE_FILE;
311 opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort);
312 qemu_opts_absorb_qdict(opts, options, &local_err);
314 error_propagate(errp, local_err);
319 filename = qemu_opt_get(opts, "filename");
321 raw_parse_flags(flags, &access_flags, &overlapped);
323 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;
330 GetCurrentDirectory(MAX_PATH, buf);
331 snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", buf[0]);
334 s->hfile = CreateFile(filename, access_flags,
335 FILE_SHARE_READ, NULL,
336 OPEN_EXISTING, overlapped, NULL);
337 if (s->hfile == INVALID_HANDLE_VALUE) {
338 int err = GetLastError();
340 if (err == ERROR_ACCESS_DENIED) {
348 if (flags & BDRV_O_NATIVE_AIO) {
349 s->aio = win32_aio_init();
350 if (s->aio == NULL) {
351 CloseHandle(s->hfile);
352 error_setg(errp, "Could not initialize AIO");
357 ret = win32_aio_attach(s->aio, s->hfile);
359 win32_aio_cleanup(s->aio);
360 CloseHandle(s->hfile);
361 error_setg_errno(errp, -ret, "Could not enable AIO");
365 win32_aio_attach_aio_context(s->aio, bdrv_get_aio_context(bs));
374 static BlockAIOCB *raw_aio_readv(BlockDriverState *bs,
375 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
376 BlockCompletionFunc *cb, void *opaque)
378 BDRVRawState *s = bs->opaque;
380 return win32_aio_submit(bs, s->aio, s->hfile, sector_num, qiov,
381 nb_sectors, cb, opaque, QEMU_AIO_READ);
383 return paio_submit(bs, s->hfile, sector_num, qiov, nb_sectors,
384 cb, opaque, QEMU_AIO_READ);
388 static BlockAIOCB *raw_aio_writev(BlockDriverState *bs,
389 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
390 BlockCompletionFunc *cb, void *opaque)
392 BDRVRawState *s = bs->opaque;
394 return win32_aio_submit(bs, s->aio, s->hfile, sector_num, qiov,
395 nb_sectors, cb, opaque, QEMU_AIO_WRITE);
397 return paio_submit(bs, s->hfile, sector_num, qiov, nb_sectors,
398 cb, opaque, QEMU_AIO_WRITE);
402 static BlockAIOCB *raw_aio_flush(BlockDriverState *bs,
403 BlockCompletionFunc *cb, void *opaque)
405 BDRVRawState *s = bs->opaque;
406 return paio_submit(bs, s->hfile, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
409 static void raw_close(BlockDriverState *bs)
411 BDRVRawState *s = bs->opaque;
414 win32_aio_detach_aio_context(s->aio, bdrv_get_aio_context(bs));
415 win32_aio_cleanup(s->aio);
419 CloseHandle(s->hfile);
420 if (bs->open_flags & BDRV_O_TEMPORARY) {
421 unlink(bs->filename);
425 static int raw_truncate(BlockDriverState *bs, int64_t offset)
427 BDRVRawState *s = bs->opaque;
435 * An error has occurred if the return value is INVALID_SET_FILE_POINTER
436 * and GetLastError doesn't return NO_ERROR.
438 dwPtrLow = SetFilePointer(s->hfile, low, &high, FILE_BEGIN);
439 if (dwPtrLow == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
440 fprintf(stderr, "SetFilePointer error: %lu\n", GetLastError());
443 if (SetEndOfFile(s->hfile) == 0) {
444 fprintf(stderr, "SetEndOfFile error: %lu\n", GetLastError());
450 static int64_t raw_getlength(BlockDriverState *bs)
452 BDRVRawState *s = bs->opaque;
454 ULARGE_INTEGER available, total, total_free;
461 l.LowPart = GetFileSize(s->hfile, (PDWORD)&l.HighPart);
462 if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
466 if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
468 l.QuadPart = total.QuadPart;
471 status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
472 NULL, 0, &dg, sizeof(dg), &count, NULL);
483 static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
485 typedef DWORD (WINAPI * get_compressed_t)(const char *filename,
487 get_compressed_t get_compressed;
489 const char *filename = bs->filename;
490 /* WinNT support GetCompressedFileSize to determine allocate size */
492 (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"),
493 "GetCompressedFileSizeA");
494 if (get_compressed) {
496 low = get_compressed(filename, &high);
497 if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR) {
498 return (((int64_t) high) << 32) + low;
502 if (_stati64(filename, &st) < 0) {
508 static int raw_create(const char *filename, QemuOpts *opts, Error **errp)
511 int64_t total_size = 0;
513 strstart(filename, "file:", &filename);
515 /* Read out options */
516 total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
519 fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
522 error_setg_errno(errp, errno, "Could not create file");
526 ftruncate(fd, total_size);
532 static QemuOptsList raw_create_opts = {
533 .name = "raw-create-opts",
534 .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head),
537 .name = BLOCK_OPT_SIZE,
538 .type = QEMU_OPT_SIZE,
539 .help = "Virtual disk size"
541 { /* end of list */ }
545 BlockDriver bdrv_file = {
546 .format_name = "file",
547 .protocol_name = "file",
548 .instance_size = sizeof(BDRVRawState),
549 .bdrv_needs_filename = true,
550 .bdrv_parse_filename = raw_parse_filename,
551 .bdrv_file_open = raw_open,
552 .bdrv_refresh_limits = raw_probe_alignment,
553 .bdrv_close = raw_close,
554 .bdrv_create = raw_create,
555 .bdrv_has_zero_init = bdrv_has_zero_init_1,
557 .bdrv_aio_readv = raw_aio_readv,
558 .bdrv_aio_writev = raw_aio_writev,
559 .bdrv_aio_flush = raw_aio_flush,
561 .bdrv_truncate = raw_truncate,
562 .bdrv_getlength = raw_getlength,
563 .bdrv_get_allocated_file_size
564 = raw_get_allocated_file_size,
566 .create_opts = &raw_create_opts,
569 /***********************************************/
572 static int find_cdrom(char *cdrom_name, int cdrom_name_size)
574 char drives[256], *pdrv = drives;
577 memset(drives, 0, sizeof(drives));
578 GetLogicalDriveStrings(sizeof(drives), drives);
579 while(pdrv[0] != '\0') {
580 type = GetDriveType(pdrv);
583 snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
587 pdrv += lstrlen(pdrv) + 1;
592 static int find_device_type(BlockDriverState *bs, const char *filename)
594 BDRVRawState *s = bs->opaque;
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);
605 case DRIVE_REMOVABLE:
607 return FTYPE_HARDDISK;
618 static int hdev_probe_device(const char *filename)
620 if (strstart(filename, "/dev/cdrom", NULL))
622 if (is_windows_drive(filename))
627 static void hdev_parse_filename(const char *filename, QDict *options,
630 /* The prefix is optional, just as for "file". */
631 strstart(filename, "host_device:", &filename);
633 qdict_put_obj(options, "filename", QOBJECT(qstring_from_str(filename)));
636 static int hdev_open(BlockDriverState *bs, QDict *options, int flags,
639 BDRVRawState *s = bs->opaque;
640 int access_flags, create_flags;
643 char device_name[64];
645 Error *local_err = NULL;
646 const char *filename;
648 QemuOpts *opts = qemu_opts_create(&raw_runtime_opts, NULL, 0,
650 qemu_opts_absorb_qdict(opts, options, &local_err);
652 error_propagate(errp, local_err);
657 filename = qemu_opt_get(opts, "filename");
659 if (strstart(filename, "/dev/cdrom", NULL)) {
660 if (find_cdrom(device_name, sizeof(device_name)) < 0) {
661 error_setg(errp, "Could not open CD-ROM drive");
665 filename = device_name;
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;
675 s->type = find_device_type(bs, filename);
677 raw_parse_flags(flags, &access_flags, &overlapped);
679 create_flags = OPEN_EXISTING;
681 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();
687 if (err == ERROR_ACCESS_DENIED) {
692 error_setg_errno(errp, -ret, "Could not open device");
701 static BlockDriver bdrv_host_device = {
702 .format_name = "host_device",
703 .protocol_name = "host_device",
704 .instance_size = sizeof(BDRVRawState),
705 .bdrv_needs_filename = true,
706 .bdrv_parse_filename = hdev_parse_filename,
707 .bdrv_probe_device = hdev_probe_device,
708 .bdrv_file_open = hdev_open,
709 .bdrv_close = raw_close,
711 .bdrv_aio_readv = raw_aio_readv,
712 .bdrv_aio_writev = raw_aio_writev,
713 .bdrv_aio_flush = raw_aio_flush,
715 .bdrv_detach_aio_context = raw_detach_aio_context,
716 .bdrv_attach_aio_context = raw_attach_aio_context,
718 .bdrv_getlength = raw_getlength,
719 .has_variable_length = true,
721 .bdrv_get_allocated_file_size
722 = raw_get_allocated_file_size,
725 static void bdrv_file_init(void)
727 bdrv_register(&bdrv_file);
728 bdrv_register(&bdrv_host_device);
731 block_init(bdrv_file_init);