1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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>
34
35#define FTYPE_FILE 0
36#define FTYPE_CD 1
37#define FTYPE_HARDDISK 2
38
39static QEMUWin32AIOState *aio;
40
41typedef 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;
50
51typedef struct BDRVRawState {
52 HANDLE hfile;
53 int type;
54 char drive_path[16];
55 QEMUWin32AIOState *aio;
56} BDRVRawState;
57
58
59
60
61
62
63
64static size_t handle_aiocb_rw(RawWin32AIOData *aiocb)
65{
66 size_t offset = 0;
67 int i;
68
69 for (i = 0; i < aiocb->aio_niov; i++) {
70 OVERLAPPED ov;
71 DWORD ret, ret_count, len;
72
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);
83 }
84 if (!ret) {
85 ret_count = 0;
86 }
87 if (ret_count != len) {
88 break;
89 }
90 offset += len;
91 }
92
93 return offset;
94}
95
96static int aio_worker(void *arg)
97{
98 RawWin32AIOData *aiocb = arg;
99 ssize_t ret = 0;
100 size_t count;
101
102 switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
103 case QEMU_AIO_READ:
104 count = handle_aiocb_rw(aiocb);
105 if (count < aiocb->aio_nbytes && aiocb->bs->growable) {
106
107
108 iov_memset(aiocb->aio_iov, aiocb->aio_niov, count,
109 0, aiocb->aio_nbytes - count);
110
111 count = aiocb->aio_nbytes;
112 }
113 if (count == aiocb->aio_nbytes) {
114 ret = 0;
115 } else {
116 ret = -EINVAL;
117 }
118 break;
119 case QEMU_AIO_WRITE:
120 count = handle_aiocb_rw(aiocb);
121 if (count == aiocb->aio_nbytes) {
122 count = 0;
123 } else {
124 count = -EINVAL;
125 }
126 break;
127 case QEMU_AIO_FLUSH:
128 if (!FlushFileBuffers(aiocb->hfile)) {
129 return -EIO;
130 }
131 break;
132 default:
133 fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
134 ret = -EINVAL;
135 break;
136 }
137
138 g_slice_free(RawWin32AIOData, aiocb);
139 return ret;
140}
141
142static BlockDriverAIOCB *paio_submit(BlockDriverState *bs, HANDLE hfile,
143 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
144 BlockDriverCompletionFunc *cb, void *opaque, int type)
145{
146 RawWin32AIOData *acb = g_slice_new(RawWin32AIOData);
147 ThreadPool *pool;
148
149 acb->bs = bs;
150 acb->hfile = hfile;
151 acb->aio_type = type;
152
153 if (qiov) {
154 acb->aio_iov = qiov->iov;
155 acb->aio_niov = qiov->niov;
156 }
157 acb->aio_nbytes = nb_sectors * 512;
158 acb->aio_offset = sector_num * 512;
159
160 trace_paio_submit(acb, opaque, sector_num, nb_sectors, type);
161 pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
162 return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque);
163}
164
165int qemu_ftruncate64(int fd, int64_t length)
166{
167 LARGE_INTEGER li;
168 DWORD dw;
169 LONG high;
170 HANDLE h;
171 BOOL res;
172
173 if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
174 return -1;
175
176 h = (HANDLE)_get_osfhandle(fd);
177
178
179 li.HighPart = 0;
180 li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
181 if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
182 return -1;
183 }
184
185 high = length >> 32;
186 dw = SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN);
187 if (dw == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
188 return -1;
189 }
190 res = SetEndOfFile(h);
191
192
193 SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
194 return res ? 0 : -1;
195}
196
197static int set_sparse(int fd)
198{
199 DWORD returned;
200 return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
201 NULL, 0, NULL, 0, &returned, NULL);
202}
203
204static void raw_parse_flags(int flags, int *access_flags, DWORD *overlapped)
205{
206 assert(access_flags != NULL);
207 assert(overlapped != NULL);
208
209 if (flags & BDRV_O_RDWR) {
210 *access_flags = GENERIC_READ | GENERIC_WRITE;
211 } else {
212 *access_flags = GENERIC_READ;
213 }
214
215 *overlapped = FILE_ATTRIBUTE_NORMAL;
216 if (flags & BDRV_O_NATIVE_AIO) {
217 *overlapped |= FILE_FLAG_OVERLAPPED;
218 }
219 if (flags & BDRV_O_NOCACHE) {
220 *overlapped |= FILE_FLAG_NO_BUFFERING;
221 }
222}
223
224static QemuOptsList raw_runtime_opts = {
225 .name = "raw",
226 .head = QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head),
227 .desc = {
228 {
229 .name = "filename",
230 .type = QEMU_OPT_STRING,
231 .help = "File name of the image",
232 },
233 { }
234 },
235};
236
237static int raw_open(BlockDriverState *bs, QDict *options, int flags)
238{
239 BDRVRawState *s = bs->opaque;
240 int access_flags;
241 DWORD overlapped;
242 QemuOpts *opts;
243 Error *local_err = NULL;
244 const char *filename;
245 int ret;
246
247 s->type = FTYPE_FILE;
248
249 opts = qemu_opts_create_nofail(&raw_runtime_opts);
250 qemu_opts_absorb_qdict(opts, options, &local_err);
251 if (error_is_set(&local_err)) {
252 qerror_report_err(local_err);
253 error_free(local_err);
254 ret = -EINVAL;
255 goto fail;
256 }
257
258 filename = qemu_opt_get(opts, "filename");
259
260 raw_parse_flags(flags, &access_flags, &overlapped);
261
262 if ((flags & BDRV_O_NATIVE_AIO) && aio == NULL) {
263 aio = win32_aio_init();
264 if (aio == NULL) {
265 ret = -EINVAL;
266 goto fail;
267 }
268 }
269
270 s->hfile = CreateFile(filename, access_flags,
271 FILE_SHARE_READ, NULL,
272 OPEN_EXISTING, overlapped, NULL);
273 if (s->hfile == INVALID_HANDLE_VALUE) {
274 int err = GetLastError();
275
276 if (err == ERROR_ACCESS_DENIED) {
277 ret = -EACCES;
278 } else {
279 ret = -EINVAL;
280 }
281 goto fail;
282 }
283
284 if (flags & BDRV_O_NATIVE_AIO) {
285 ret = win32_aio_attach(aio, s->hfile);
286 if (ret < 0) {
287 CloseHandle(s->hfile);
288 goto fail;
289 }
290 s->aio = aio;
291 }
292
293 ret = 0;
294fail:
295 qemu_opts_del(opts);
296 return ret;
297}
298
299static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs,
300 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
301 BlockDriverCompletionFunc *cb, void *opaque)
302{
303 BDRVRawState *s = bs->opaque;
304 if (s->aio) {
305 return win32_aio_submit(bs, s->aio, s->hfile, sector_num, qiov,
306 nb_sectors, cb, opaque, QEMU_AIO_READ);
307 } else {
308 return paio_submit(bs, s->hfile, sector_num, qiov, nb_sectors,
309 cb, opaque, QEMU_AIO_READ);
310 }
311}
312
313static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
314 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
315 BlockDriverCompletionFunc *cb, void *opaque)
316{
317 BDRVRawState *s = bs->opaque;
318 if (s->aio) {
319 return win32_aio_submit(bs, s->aio, s->hfile, sector_num, qiov,
320 nb_sectors, cb, opaque, QEMU_AIO_WRITE);
321 } else {
322 return paio_submit(bs, s->hfile, sector_num, qiov, nb_sectors,
323 cb, opaque, QEMU_AIO_WRITE);
324 }
325}
326
327static BlockDriverAIOCB *raw_aio_flush(BlockDriverState *bs,
328 BlockDriverCompletionFunc *cb, void *opaque)
329{
330 BDRVRawState *s = bs->opaque;
331 return paio_submit(bs, s->hfile, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
332}
333
334static void raw_close(BlockDriverState *bs)
335{
336 BDRVRawState *s = bs->opaque;
337 CloseHandle(s->hfile);
338}
339
340static int raw_truncate(BlockDriverState *bs, int64_t offset)
341{
342 BDRVRawState *s = bs->opaque;
343 LONG low, high;
344 DWORD dwPtrLow;
345
346 low = offset;
347 high = offset >> 32;
348
349
350
351
352
353 dwPtrLow = SetFilePointer(s->hfile, low, &high, FILE_BEGIN);
354 if (dwPtrLow == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
355 fprintf(stderr, "SetFilePointer error: %lu\n", GetLastError());
356 return -EIO;
357 }
358 if (SetEndOfFile(s->hfile) == 0) {
359 fprintf(stderr, "SetEndOfFile error: %lu\n", GetLastError());
360 return -EIO;
361 }
362 return 0;
363}
364
365static int64_t raw_getlength(BlockDriverState *bs)
366{
367 BDRVRawState *s = bs->opaque;
368 LARGE_INTEGER l;
369 ULARGE_INTEGER available, total, total_free;
370 DISK_GEOMETRY_EX dg;
371 DWORD count;
372 BOOL status;
373
374 switch(s->type) {
375 case FTYPE_FILE:
376 l.LowPart = GetFileSize(s->hfile, (PDWORD)&l.HighPart);
377 if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
378 return -EIO;
379 break;
380 case FTYPE_CD:
381 if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
382 return -EIO;
383 l.QuadPart = total.QuadPart;
384 break;
385 case FTYPE_HARDDISK:
386 status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
387 NULL, 0, &dg, sizeof(dg), &count, NULL);
388 if (status != 0) {
389 l = dg.DiskSize;
390 }
391 break;
392 default:
393 return -EIO;
394 }
395 return l.QuadPart;
396}
397
398static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
399{
400 typedef DWORD (WINAPI * get_compressed_t)(const char *filename,
401 DWORD * high);
402 get_compressed_t get_compressed;
403 struct _stati64 st;
404 const char *filename = bs->filename;
405
406 get_compressed =
407 (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"),
408 "GetCompressedFileSizeA");
409 if (get_compressed) {
410 DWORD high, low;
411 low = get_compressed(filename, &high);
412 if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR) {
413 return (((int64_t) high) << 32) + low;
414 }
415 }
416
417 if (_stati64(filename, &st) < 0) {
418 return -1;
419 }
420 return st.st_size;
421}
422
423static int raw_create(const char *filename, QEMUOptionParameter *options)
424{
425 int fd;
426 int64_t total_size = 0;
427
428
429 while (options && options->name) {
430 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
431 total_size = options->value.n / 512;
432 }
433 options++;
434 }
435
436 fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
437 0644);
438 if (fd < 0)
439 return -EIO;
440 set_sparse(fd);
441 ftruncate(fd, total_size * 512);
442 qemu_close(fd);
443 return 0;
444}
445
446static QEMUOptionParameter raw_create_options[] = {
447 {
448 .name = BLOCK_OPT_SIZE,
449 .type = OPT_SIZE,
450 .help = "Virtual disk size"
451 },
452 { NULL }
453};
454
455static BlockDriver bdrv_file = {
456 .format_name = "file",
457 .protocol_name = "file",
458 .instance_size = sizeof(BDRVRawState),
459 .bdrv_file_open = raw_open,
460 .bdrv_close = raw_close,
461 .bdrv_create = raw_create,
462
463 .bdrv_aio_readv = raw_aio_readv,
464 .bdrv_aio_writev = raw_aio_writev,
465 .bdrv_aio_flush = raw_aio_flush,
466
467 .bdrv_truncate = raw_truncate,
468 .bdrv_getlength = raw_getlength,
469 .bdrv_get_allocated_file_size
470 = raw_get_allocated_file_size,
471
472 .create_options = raw_create_options,
473};
474
475
476
477
478static int find_cdrom(char *cdrom_name, int cdrom_name_size)
479{
480 char drives[256], *pdrv = drives;
481 UINT type;
482
483 memset(drives, 0, sizeof(drives));
484 GetLogicalDriveStrings(sizeof(drives), drives);
485 while(pdrv[0] != '\0') {
486 type = GetDriveType(pdrv);
487 switch(type) {
488 case DRIVE_CDROM:
489 snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
490 return 0;
491 break;
492 }
493 pdrv += lstrlen(pdrv) + 1;
494 }
495 return -1;
496}
497
498static int find_device_type(BlockDriverState *bs, const char *filename)
499{
500 BDRVRawState *s = bs->opaque;
501 UINT type;
502 const char *p;
503
504 if (strstart(filename, "\\\\.\\", &p) ||
505 strstart(filename, "//./", &p)) {
506 if (stristart(p, "PhysicalDrive", NULL))
507 return FTYPE_HARDDISK;
508 snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
509 type = GetDriveType(s->drive_path);
510 switch (type) {
511 case DRIVE_REMOVABLE:
512 case DRIVE_FIXED:
513 return FTYPE_HARDDISK;
514 case DRIVE_CDROM:
515 return FTYPE_CD;
516 default:
517 return FTYPE_FILE;
518 }
519 } else {
520 return FTYPE_FILE;
521 }
522}
523
524static int hdev_probe_device(const char *filename)
525{
526 if (strstart(filename, "/dev/cdrom", NULL))
527 return 100;
528 if (is_windows_drive(filename))
529 return 100;
530 return 0;
531}
532
533static int hdev_open(BlockDriverState *bs, QDict *options, int flags)
534{
535 BDRVRawState *s = bs->opaque;
536 int access_flags, create_flags;
537 DWORD overlapped;
538 char device_name[64];
539 const char *filename = qdict_get_str(options, "filename");
540
541 if (strstart(filename, "/dev/cdrom", NULL)) {
542 if (find_cdrom(device_name, sizeof(device_name)) < 0)
543 return -ENOENT;
544 filename = device_name;
545 } else {
546
547 if (((filename[0] >= 'a' && filename[0] <= 'z') ||
548 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
549 filename[1] == ':' && filename[2] == '\0') {
550 snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
551 filename = device_name;
552 }
553 }
554 s->type = find_device_type(bs, filename);
555
556 raw_parse_flags(flags, &access_flags, &overlapped);
557
558 create_flags = OPEN_EXISTING;
559
560 s->hfile = CreateFile(filename, access_flags,
561 FILE_SHARE_READ, NULL,
562 create_flags, overlapped, NULL);
563 if (s->hfile == INVALID_HANDLE_VALUE) {
564 int err = GetLastError();
565
566 if (err == ERROR_ACCESS_DENIED)
567 return -EACCES;
568 return -1;
569 }
570 return 0;
571}
572
573static int hdev_has_zero_init(BlockDriverState *bs)
574{
575 return 0;
576}
577
578static BlockDriver bdrv_host_device = {
579 .format_name = "host_device",
580 .protocol_name = "host_device",
581 .instance_size = sizeof(BDRVRawState),
582 .bdrv_probe_device = hdev_probe_device,
583 .bdrv_file_open = hdev_open,
584 .bdrv_close = raw_close,
585 .bdrv_has_zero_init = hdev_has_zero_init,
586
587 .bdrv_aio_readv = raw_aio_readv,
588 .bdrv_aio_writev = raw_aio_writev,
589 .bdrv_aio_flush = raw_aio_flush,
590
591 .bdrv_getlength = raw_getlength,
592 .bdrv_get_allocated_file_size
593 = raw_get_allocated_file_size,
594};
595
596static void bdrv_file_init(void)
597{
598 bdrv_register(&bdrv_file);
599 bdrv_register(&bdrv_host_device);
600}
601
602block_init(bdrv_file_init);
603