e1000/rtl8139: update HMP NIC when every bit is written
[qemu/ar7.git] / block / raw-win32.c
blob2741e4de10b9989ca916bfe90357680c2f4fbf54
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.
24 #include "qemu-common.h"
25 #include "qemu/timer.h"
26 #include "block/block_int.h"
27 #include "qemu/module.h"
28 #include "raw-aio.h"
29 #include "trace.h"
30 #include "block/thread-pool.h"
31 #include "qemu/iov.h"
32 #include <windows.h>
33 #include <winioctl.h>
35 #define FTYPE_FILE 0
36 #define FTYPE_CD 1
37 #define FTYPE_HARDDISK 2
39 static QEMUWin32AIOState *aio;
41 typedef struct RawWin32AIOData {
42 BlockDriverState *bs;
43 HANDLE hfile;
44 struct iovec *aio_iov;
45 int aio_niov;
46 size_t aio_nbytes;
47 off64_t aio_offset;
48 int aio_type;
49 } RawWin32AIOData;
51 typedef struct BDRVRawState {
52 HANDLE hfile;
53 int type;
54 char drive_path[16]; /* format: "d:\" */
55 QEMUWin32AIOState *aio;
56 } BDRVRawState;
59 * Read/writes the data to/from a given linear buffer.
61 * Returns the number of bytes handles or -errno in case of an error. Short
62 * reads are only returned if the end of the file is reached.
64 static size_t handle_aiocb_rw(RawWin32AIOData *aiocb)
66 size_t offset = 0;
67 int i;
69 for (i = 0; i < aiocb->aio_niov; i++) {
70 OVERLAPPED ov;
71 DWORD ret, ret_count, len;
73 memset(&ov, 0, sizeof(ov));
74 ov.Offset = (aiocb->aio_offset + offset);
75 ov.OffsetHigh = (aiocb->aio_offset + offset) >> 32;
76 len = aiocb->aio_iov[i].iov_len;
77 if (aiocb->aio_type & QEMU_AIO_WRITE) {
78 ret = WriteFile(aiocb->hfile, aiocb->aio_iov[i].iov_base,
79 len, &ret_count, &ov);
80 } else {
81 ret = ReadFile(aiocb->hfile, aiocb->aio_iov[i].iov_base,
82 len, &ret_count, &ov);
84 if (!ret) {
85 ret_count = 0;
87 if (ret_count != len) {
88 offset += ret_count;
89 break;
91 offset += len;
94 return offset;
97 static int aio_worker(void *arg)
99 RawWin32AIOData *aiocb = arg;
100 ssize_t ret = 0;
101 size_t count;
103 switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
104 case QEMU_AIO_READ:
105 count = handle_aiocb_rw(aiocb);
106 if (count < aiocb->aio_nbytes && aiocb->bs->growable) {
107 /* A short read means that we have reached EOF. Pad the buffer
108 * with zeros for bytes after EOF. */
109 iov_memset(aiocb->aio_iov, aiocb->aio_niov, count,
110 0, aiocb->aio_nbytes - count);
112 count = aiocb->aio_nbytes;
114 if (count == aiocb->aio_nbytes) {
115 ret = 0;
116 } else {
117 ret = -EINVAL;
119 break;
120 case QEMU_AIO_WRITE:
121 count = handle_aiocb_rw(aiocb);
122 if (count == aiocb->aio_nbytes) {
123 count = 0;
124 } else {
125 count = -EINVAL;
127 break;
128 case QEMU_AIO_FLUSH:
129 if (!FlushFileBuffers(aiocb->hfile)) {
130 return -EIO;
132 break;
133 default:
134 fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
135 ret = -EINVAL;
136 break;
139 g_slice_free(RawWin32AIOData, aiocb);
140 return ret;
143 static BlockDriverAIOCB *paio_submit(BlockDriverState *bs, HANDLE hfile,
144 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
145 BlockDriverCompletionFunc *cb, void *opaque, int type)
147 RawWin32AIOData *acb = g_slice_new(RawWin32AIOData);
148 ThreadPool *pool;
150 acb->bs = bs;
151 acb->hfile = hfile;
152 acb->aio_type = type;
154 if (qiov) {
155 acb->aio_iov = qiov->iov;
156 acb->aio_niov = qiov->niov;
158 acb->aio_nbytes = nb_sectors * 512;
159 acb->aio_offset = sector_num * 512;
161 trace_paio_submit(acb, opaque, sector_num, nb_sectors, type);
162 pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
163 return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque);
166 int qemu_ftruncate64(int fd, int64_t length)
168 LARGE_INTEGER li;
169 DWORD dw;
170 LONG high;
171 HANDLE h;
172 BOOL res;
174 if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
175 return -1;
177 h = (HANDLE)_get_osfhandle(fd);
179 /* get current position, ftruncate do not change position */
180 li.HighPart = 0;
181 li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
182 if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
183 return -1;
186 high = length >> 32;
187 dw = SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN);
188 if (dw == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
189 return -1;
191 res = SetEndOfFile(h);
193 /* back to old position */
194 SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
195 return res ? 0 : -1;
198 static int set_sparse(int fd)
200 DWORD returned;
201 return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
202 NULL, 0, NULL, 0, &returned, NULL);
205 static void raw_parse_flags(int flags, int *access_flags, DWORD *overlapped)
207 assert(access_flags != NULL);
208 assert(overlapped != NULL);
210 if (flags & BDRV_O_RDWR) {
211 *access_flags = GENERIC_READ | GENERIC_WRITE;
212 } else {
213 *access_flags = GENERIC_READ;
216 *overlapped = FILE_ATTRIBUTE_NORMAL;
217 if (flags & BDRV_O_NATIVE_AIO) {
218 *overlapped |= FILE_FLAG_OVERLAPPED;
220 if (flags & BDRV_O_NOCACHE) {
221 *overlapped |= FILE_FLAG_NO_BUFFERING;
225 static QemuOptsList raw_runtime_opts = {
226 .name = "raw",
227 .head = QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head),
228 .desc = {
230 .name = "filename",
231 .type = QEMU_OPT_STRING,
232 .help = "File name of the image",
234 { /* end of list */ }
238 static int raw_open(BlockDriverState *bs, QDict *options, int flags,
239 Error **errp)
241 BDRVRawState *s = bs->opaque;
242 int access_flags;
243 DWORD overlapped;
244 QemuOpts *opts;
245 Error *local_err = NULL;
246 const char *filename;
247 int ret;
249 s->type = FTYPE_FILE;
251 opts = qemu_opts_create_nofail(&raw_runtime_opts);
252 qemu_opts_absorb_qdict(opts, options, &local_err);
253 if (error_is_set(&local_err)) {
254 error_propagate(errp, local_err);
255 ret = -EINVAL;
256 goto fail;
259 filename = qemu_opt_get(opts, "filename");
261 raw_parse_flags(flags, &access_flags, &overlapped);
263 if ((flags & BDRV_O_NATIVE_AIO) && aio == NULL) {
264 aio = win32_aio_init();
265 if (aio == NULL) {
266 error_setg(errp, "Could not initialize AIO");
267 ret = -EINVAL;
268 goto fail;
272 s->hfile = CreateFile(filename, access_flags,
273 FILE_SHARE_READ, NULL,
274 OPEN_EXISTING, overlapped, NULL);
275 if (s->hfile == INVALID_HANDLE_VALUE) {
276 int err = GetLastError();
278 if (err == ERROR_ACCESS_DENIED) {
279 ret = -EACCES;
280 } else {
281 ret = -EINVAL;
283 error_setg_errno(errp, -ret, "Could not open file");
284 goto fail;
287 if (flags & BDRV_O_NATIVE_AIO) {
288 ret = win32_aio_attach(aio, s->hfile);
289 if (ret < 0) {
290 CloseHandle(s->hfile);
291 error_setg_errno(errp, -ret, "Could not enable AIO");
292 goto fail;
294 s->aio = aio;
297 ret = 0;
298 fail:
299 qemu_opts_del(opts);
300 return ret;
303 static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs,
304 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
305 BlockDriverCompletionFunc *cb, void *opaque)
307 BDRVRawState *s = bs->opaque;
308 if (s->aio) {
309 return win32_aio_submit(bs, s->aio, s->hfile, sector_num, qiov,
310 nb_sectors, cb, opaque, QEMU_AIO_READ);
311 } else {
312 return paio_submit(bs, s->hfile, sector_num, qiov, nb_sectors,
313 cb, opaque, QEMU_AIO_READ);
317 static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
318 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
319 BlockDriverCompletionFunc *cb, void *opaque)
321 BDRVRawState *s = bs->opaque;
322 if (s->aio) {
323 return win32_aio_submit(bs, s->aio, s->hfile, sector_num, qiov,
324 nb_sectors, cb, opaque, QEMU_AIO_WRITE);
325 } else {
326 return paio_submit(bs, s->hfile, sector_num, qiov, nb_sectors,
327 cb, opaque, QEMU_AIO_WRITE);
331 static BlockDriverAIOCB *raw_aio_flush(BlockDriverState *bs,
332 BlockDriverCompletionFunc *cb, void *opaque)
334 BDRVRawState *s = bs->opaque;
335 return paio_submit(bs, s->hfile, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
338 static void raw_close(BlockDriverState *bs)
340 BDRVRawState *s = bs->opaque;
341 CloseHandle(s->hfile);
344 static int raw_truncate(BlockDriverState *bs, int64_t offset)
346 BDRVRawState *s = bs->opaque;
347 LONG low, high;
348 DWORD dwPtrLow;
350 low = offset;
351 high = offset >> 32;
354 * An error has occurred if the return value is INVALID_SET_FILE_POINTER
355 * and GetLastError doesn't return NO_ERROR.
357 dwPtrLow = SetFilePointer(s->hfile, low, &high, FILE_BEGIN);
358 if (dwPtrLow == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
359 fprintf(stderr, "SetFilePointer error: %lu\n", GetLastError());
360 return -EIO;
362 if (SetEndOfFile(s->hfile) == 0) {
363 fprintf(stderr, "SetEndOfFile error: %lu\n", GetLastError());
364 return -EIO;
366 return 0;
369 static int64_t raw_getlength(BlockDriverState *bs)
371 BDRVRawState *s = bs->opaque;
372 LARGE_INTEGER l;
373 ULARGE_INTEGER available, total, total_free;
374 DISK_GEOMETRY_EX dg;
375 DWORD count;
376 BOOL status;
378 switch(s->type) {
379 case FTYPE_FILE:
380 l.LowPart = GetFileSize(s->hfile, (PDWORD)&l.HighPart);
381 if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
382 return -EIO;
383 break;
384 case FTYPE_CD:
385 if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
386 return -EIO;
387 l.QuadPart = total.QuadPart;
388 break;
389 case FTYPE_HARDDISK:
390 status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
391 NULL, 0, &dg, sizeof(dg), &count, NULL);
392 if (status != 0) {
393 l = dg.DiskSize;
395 break;
396 default:
397 return -EIO;
399 return l.QuadPart;
402 static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
404 typedef DWORD (WINAPI * get_compressed_t)(const char *filename,
405 DWORD * high);
406 get_compressed_t get_compressed;
407 struct _stati64 st;
408 const char *filename = bs->filename;
409 /* WinNT support GetCompressedFileSize to determine allocate size */
410 get_compressed =
411 (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"),
412 "GetCompressedFileSizeA");
413 if (get_compressed) {
414 DWORD high, low;
415 low = get_compressed(filename, &high);
416 if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR) {
417 return (((int64_t) high) << 32) + low;
421 if (_stati64(filename, &st) < 0) {
422 return -1;
424 return st.st_size;
427 static int raw_create(const char *filename, QEMUOptionParameter *options,
428 Error **errp)
430 int fd;
431 int64_t total_size = 0;
433 /* Read out options */
434 while (options && options->name) {
435 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
436 total_size = options->value.n / 512;
438 options++;
441 fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
442 0644);
443 if (fd < 0) {
444 error_setg_errno(errp, errno, "Could not create file");
445 return -EIO;
447 set_sparse(fd);
448 ftruncate(fd, total_size * 512);
449 qemu_close(fd);
450 return 0;
453 static QEMUOptionParameter raw_create_options[] = {
455 .name = BLOCK_OPT_SIZE,
456 .type = OPT_SIZE,
457 .help = "Virtual disk size"
459 { NULL }
462 static BlockDriver bdrv_file = {
463 .format_name = "file",
464 .protocol_name = "file",
465 .instance_size = sizeof(BDRVRawState),
466 .bdrv_needs_filename = true,
467 .bdrv_file_open = raw_open,
468 .bdrv_close = raw_close,
469 .bdrv_create = raw_create,
470 .bdrv_has_zero_init = bdrv_has_zero_init_1,
472 .bdrv_aio_readv = raw_aio_readv,
473 .bdrv_aio_writev = raw_aio_writev,
474 .bdrv_aio_flush = raw_aio_flush,
476 .bdrv_truncate = raw_truncate,
477 .bdrv_getlength = raw_getlength,
478 .bdrv_get_allocated_file_size
479 = raw_get_allocated_file_size,
481 .create_options = raw_create_options,
484 /***********************************************/
485 /* host device */
487 static int find_cdrom(char *cdrom_name, int cdrom_name_size)
489 char drives[256], *pdrv = drives;
490 UINT type;
492 memset(drives, 0, sizeof(drives));
493 GetLogicalDriveStrings(sizeof(drives), drives);
494 while(pdrv[0] != '\0') {
495 type = GetDriveType(pdrv);
496 switch(type) {
497 case DRIVE_CDROM:
498 snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
499 return 0;
500 break;
502 pdrv += lstrlen(pdrv) + 1;
504 return -1;
507 static int find_device_type(BlockDriverState *bs, const char *filename)
509 BDRVRawState *s = bs->opaque;
510 UINT type;
511 const char *p;
513 if (strstart(filename, "\\\\.\\", &p) ||
514 strstart(filename, "//./", &p)) {
515 if (stristart(p, "PhysicalDrive", NULL))
516 return FTYPE_HARDDISK;
517 snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
518 type = GetDriveType(s->drive_path);
519 switch (type) {
520 case DRIVE_REMOVABLE:
521 case DRIVE_FIXED:
522 return FTYPE_HARDDISK;
523 case DRIVE_CDROM:
524 return FTYPE_CD;
525 default:
526 return FTYPE_FILE;
528 } else {
529 return FTYPE_FILE;
533 static int hdev_probe_device(const char *filename)
535 if (strstart(filename, "/dev/cdrom", NULL))
536 return 100;
537 if (is_windows_drive(filename))
538 return 100;
539 return 0;
542 static int hdev_open(BlockDriverState *bs, QDict *options, int flags,
543 Error **errp)
545 BDRVRawState *s = bs->opaque;
546 int access_flags, create_flags;
547 int ret = 0;
548 DWORD overlapped;
549 char device_name[64];
551 Error *local_err = NULL;
552 const char *filename;
554 QemuOpts *opts = qemu_opts_create_nofail(&raw_runtime_opts);
555 qemu_opts_absorb_qdict(opts, options, &local_err);
556 if (error_is_set(&local_err)) {
557 error_propagate(errp, local_err);
558 ret = -EINVAL;
559 goto done;
562 filename = qemu_opt_get(opts, "filename");
564 if (strstart(filename, "/dev/cdrom", NULL)) {
565 if (find_cdrom(device_name, sizeof(device_name)) < 0) {
566 error_setg(errp, "Could not open CD-ROM drive");
567 ret = -ENOENT;
568 goto done;
570 filename = device_name;
571 } else {
572 /* transform drive letters into device name */
573 if (((filename[0] >= 'a' && filename[0] <= 'z') ||
574 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
575 filename[1] == ':' && filename[2] == '\0') {
576 snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
577 filename = device_name;
580 s->type = find_device_type(bs, filename);
582 raw_parse_flags(flags, &access_flags, &overlapped);
584 create_flags = OPEN_EXISTING;
586 s->hfile = CreateFile(filename, access_flags,
587 FILE_SHARE_READ, NULL,
588 create_flags, overlapped, NULL);
589 if (s->hfile == INVALID_HANDLE_VALUE) {
590 int err = GetLastError();
592 if (err == ERROR_ACCESS_DENIED) {
593 ret = -EACCES;
594 } else {
595 ret = -EINVAL;
597 error_setg_errno(errp, -ret, "Could not open device");
598 goto done;
601 done:
602 qemu_opts_del(opts);
603 return ret;
606 static BlockDriver bdrv_host_device = {
607 .format_name = "host_device",
608 .protocol_name = "host_device",
609 .instance_size = sizeof(BDRVRawState),
610 .bdrv_needs_filename = true,
611 .bdrv_probe_device = hdev_probe_device,
612 .bdrv_file_open = hdev_open,
613 .bdrv_close = raw_close,
615 .bdrv_aio_readv = raw_aio_readv,
616 .bdrv_aio_writev = raw_aio_writev,
617 .bdrv_aio_flush = raw_aio_flush,
619 .bdrv_getlength = raw_getlength,
620 .has_variable_length = true,
622 .bdrv_get_allocated_file_size
623 = raw_get_allocated_file_size,
626 static void bdrv_file_init(void)
628 bdrv_register(&bdrv_file);
629 bdrv_register(&bdrv_host_device);
632 block_init(bdrv_file_init);