]> git.proxmox.com Git - qemu.git/blob - block/raw-win32.c
Merge branch 'trivial-patches' of git://github.com/stefanha/qemu
[qemu.git] / block / raw-win32.c
1 /*
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 */
24 #include "qemu-common.h"
25 #include "qemu-timer.h"
26 #include "block_int.h"
27 #include "module.h"
28 #include "raw-aio.h"
29 #include "trace.h"
30 #include "thread-pool.h"
31 #include "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
39 static QEMUWin32AIOState *aio;
40
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;
50
51 typedef struct BDRVRawState {
52 HANDLE hfile;
53 int type;
54 char drive_path[16]; /* format: "d:\" */
55 QEMUWin32AIOState *aio;
56 } BDRVRawState;
57
58 /*
59 * Read/writes the data to/from a given linear buffer.
60 *
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.
63 */
64 static 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
96 static 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 /* A short read means that we have reached EOF. Pad the buffer
107 * with zeros for bytes after EOF. */
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
142 static 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
148 acb->bs = bs;
149 acb->hfile = hfile;
150 acb->aio_type = type;
151
152 if (qiov) {
153 acb->aio_iov = qiov->iov;
154 acb->aio_niov = qiov->niov;
155 }
156 acb->aio_nbytes = nb_sectors * 512;
157 acb->aio_offset = sector_num * 512;
158
159 trace_paio_submit(acb, opaque, sector_num, nb_sectors, type);
160 return thread_pool_submit_aio(aio_worker, acb, cb, opaque);
161 }
162
163 int qemu_ftruncate64(int fd, int64_t length)
164 {
165 LARGE_INTEGER li;
166 DWORD dw;
167 LONG high;
168 HANDLE h;
169 BOOL res;
170
171 if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
172 return -1;
173
174 h = (HANDLE)_get_osfhandle(fd);
175
176 /* get current position, ftruncate do not change position */
177 li.HighPart = 0;
178 li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
179 if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
180 return -1;
181 }
182
183 high = length >> 32;
184 dw = SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN);
185 if (dw == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
186 return -1;
187 }
188 res = SetEndOfFile(h);
189
190 /* back to old position */
191 SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
192 return res ? 0 : -1;
193 }
194
195 static int set_sparse(int fd)
196 {
197 DWORD returned;
198 return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
199 NULL, 0, NULL, 0, &returned, NULL);
200 }
201
202 static void raw_parse_flags(int flags, int *access_flags, DWORD *overlapped)
203 {
204 assert(access_flags != NULL);
205 assert(overlapped != NULL);
206
207 if (flags & BDRV_O_RDWR) {
208 *access_flags = GENERIC_READ | GENERIC_WRITE;
209 } else {
210 *access_flags = GENERIC_READ;
211 }
212
213 *overlapped = FILE_ATTRIBUTE_NORMAL;
214 if (flags & BDRV_O_NATIVE_AIO) {
215 *overlapped |= FILE_FLAG_OVERLAPPED;
216 }
217 if (flags & BDRV_O_NOCACHE) {
218 *overlapped |= FILE_FLAG_NO_BUFFERING;
219 }
220 }
221
222 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
223 {
224 BDRVRawState *s = bs->opaque;
225 int access_flags;
226 DWORD overlapped;
227
228 s->type = FTYPE_FILE;
229
230 raw_parse_flags(flags, &access_flags, &overlapped);
231
232 if ((flags & BDRV_O_NATIVE_AIO) && aio == NULL) {
233 aio = win32_aio_init();
234 if (aio == NULL) {
235 return -EINVAL;
236 }
237 }
238
239 s->hfile = CreateFile(filename, access_flags,
240 FILE_SHARE_READ, NULL,
241 OPEN_EXISTING, overlapped, NULL);
242 if (s->hfile == INVALID_HANDLE_VALUE) {
243 int err = GetLastError();
244
245 if (err == ERROR_ACCESS_DENIED)
246 return -EACCES;
247 return -EINVAL;
248 }
249
250 if (flags & BDRV_O_NATIVE_AIO) {
251 int ret = win32_aio_attach(aio, s->hfile);
252 if (ret < 0) {
253 CloseHandle(s->hfile);
254 return ret;
255 }
256 s->aio = aio;
257 }
258 return 0;
259 }
260
261 static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs,
262 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
263 BlockDriverCompletionFunc *cb, void *opaque)
264 {
265 BDRVRawState *s = bs->opaque;
266 if (s->aio) {
267 return win32_aio_submit(bs, s->aio, s->hfile, sector_num, qiov,
268 nb_sectors, cb, opaque, QEMU_AIO_READ);
269 } else {
270 return paio_submit(bs, s->hfile, sector_num, qiov, nb_sectors,
271 cb, opaque, QEMU_AIO_READ);
272 }
273 }
274
275 static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
276 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
277 BlockDriverCompletionFunc *cb, void *opaque)
278 {
279 BDRVRawState *s = bs->opaque;
280 if (s->aio) {
281 return win32_aio_submit(bs, s->aio, s->hfile, sector_num, qiov,
282 nb_sectors, cb, opaque, QEMU_AIO_WRITE);
283 } else {
284 return paio_submit(bs, s->hfile, sector_num, qiov, nb_sectors,
285 cb, opaque, QEMU_AIO_WRITE);
286 }
287 }
288
289 static BlockDriverAIOCB *raw_aio_flush(BlockDriverState *bs,
290 BlockDriverCompletionFunc *cb, void *opaque)
291 {
292 BDRVRawState *s = bs->opaque;
293 return paio_submit(bs, s->hfile, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
294 }
295
296 static void raw_close(BlockDriverState *bs)
297 {
298 BDRVRawState *s = bs->opaque;
299 CloseHandle(s->hfile);
300 }
301
302 static int raw_truncate(BlockDriverState *bs, int64_t offset)
303 {
304 BDRVRawState *s = bs->opaque;
305 LONG low, high;
306
307 low = offset;
308 high = offset >> 32;
309 if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN))
310 return -EIO;
311 if (!SetEndOfFile(s->hfile))
312 return -EIO;
313 return 0;
314 }
315
316 static int64_t raw_getlength(BlockDriverState *bs)
317 {
318 BDRVRawState *s = bs->opaque;
319 LARGE_INTEGER l;
320 ULARGE_INTEGER available, total, total_free;
321 DISK_GEOMETRY_EX dg;
322 DWORD count;
323 BOOL status;
324
325 switch(s->type) {
326 case FTYPE_FILE:
327 l.LowPart = GetFileSize(s->hfile, (PDWORD)&l.HighPart);
328 if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
329 return -EIO;
330 break;
331 case FTYPE_CD:
332 if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
333 return -EIO;
334 l.QuadPart = total.QuadPart;
335 break;
336 case FTYPE_HARDDISK:
337 status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
338 NULL, 0, &dg, sizeof(dg), &count, NULL);
339 if (status != 0) {
340 l = dg.DiskSize;
341 }
342 break;
343 default:
344 return -EIO;
345 }
346 return l.QuadPart;
347 }
348
349 static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
350 {
351 typedef DWORD (WINAPI * get_compressed_t)(const char *filename,
352 DWORD * high);
353 get_compressed_t get_compressed;
354 struct _stati64 st;
355 const char *filename = bs->filename;
356 /* WinNT support GetCompressedFileSize to determine allocate size */
357 get_compressed =
358 (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"),
359 "GetCompressedFileSizeA");
360 if (get_compressed) {
361 DWORD high, low;
362 low = get_compressed(filename, &high);
363 if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR) {
364 return (((int64_t) high) << 32) + low;
365 }
366 }
367
368 if (_stati64(filename, &st) < 0) {
369 return -1;
370 }
371 return st.st_size;
372 }
373
374 static int raw_create(const char *filename, QEMUOptionParameter *options)
375 {
376 int fd;
377 int64_t total_size = 0;
378
379 /* Read out options */
380 while (options && options->name) {
381 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
382 total_size = options->value.n / 512;
383 }
384 options++;
385 }
386
387 fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
388 0644);
389 if (fd < 0)
390 return -EIO;
391 set_sparse(fd);
392 ftruncate(fd, total_size * 512);
393 qemu_close(fd);
394 return 0;
395 }
396
397 static QEMUOptionParameter raw_create_options[] = {
398 {
399 .name = BLOCK_OPT_SIZE,
400 .type = OPT_SIZE,
401 .help = "Virtual disk size"
402 },
403 { NULL }
404 };
405
406 static BlockDriver bdrv_file = {
407 .format_name = "file",
408 .protocol_name = "file",
409 .instance_size = sizeof(BDRVRawState),
410 .bdrv_file_open = raw_open,
411 .bdrv_close = raw_close,
412 .bdrv_create = raw_create,
413
414 .bdrv_aio_readv = raw_aio_readv,
415 .bdrv_aio_writev = raw_aio_writev,
416 .bdrv_aio_flush = raw_aio_flush,
417
418 .bdrv_truncate = raw_truncate,
419 .bdrv_getlength = raw_getlength,
420 .bdrv_get_allocated_file_size
421 = raw_get_allocated_file_size,
422
423 .create_options = raw_create_options,
424 };
425
426 /***********************************************/
427 /* host device */
428
429 static int find_cdrom(char *cdrom_name, int cdrom_name_size)
430 {
431 char drives[256], *pdrv = drives;
432 UINT type;
433
434 memset(drives, 0, sizeof(drives));
435 GetLogicalDriveStrings(sizeof(drives), drives);
436 while(pdrv[0] != '\0') {
437 type = GetDriveType(pdrv);
438 switch(type) {
439 case DRIVE_CDROM:
440 snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
441 return 0;
442 break;
443 }
444 pdrv += lstrlen(pdrv) + 1;
445 }
446 return -1;
447 }
448
449 static int find_device_type(BlockDriverState *bs, const char *filename)
450 {
451 BDRVRawState *s = bs->opaque;
452 UINT type;
453 const char *p;
454
455 if (strstart(filename, "\\\\.\\", &p) ||
456 strstart(filename, "//./", &p)) {
457 if (stristart(p, "PhysicalDrive", NULL))
458 return FTYPE_HARDDISK;
459 snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
460 type = GetDriveType(s->drive_path);
461 switch (type) {
462 case DRIVE_REMOVABLE:
463 case DRIVE_FIXED:
464 return FTYPE_HARDDISK;
465 case DRIVE_CDROM:
466 return FTYPE_CD;
467 default:
468 return FTYPE_FILE;
469 }
470 } else {
471 return FTYPE_FILE;
472 }
473 }
474
475 static int hdev_probe_device(const char *filename)
476 {
477 if (strstart(filename, "/dev/cdrom", NULL))
478 return 100;
479 if (is_windows_drive(filename))
480 return 100;
481 return 0;
482 }
483
484 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
485 {
486 BDRVRawState *s = bs->opaque;
487 int access_flags, create_flags;
488 DWORD overlapped;
489 char device_name[64];
490
491 if (strstart(filename, "/dev/cdrom", NULL)) {
492 if (find_cdrom(device_name, sizeof(device_name)) < 0)
493 return -ENOENT;
494 filename = device_name;
495 } else {
496 /* transform drive letters into device name */
497 if (((filename[0] >= 'a' && filename[0] <= 'z') ||
498 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
499 filename[1] == ':' && filename[2] == '\0') {
500 snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
501 filename = device_name;
502 }
503 }
504 s->type = find_device_type(bs, filename);
505
506 raw_parse_flags(flags, &access_flags, &overlapped);
507
508 create_flags = OPEN_EXISTING;
509
510 s->hfile = CreateFile(filename, access_flags,
511 FILE_SHARE_READ, NULL,
512 create_flags, overlapped, NULL);
513 if (s->hfile == INVALID_HANDLE_VALUE) {
514 int err = GetLastError();
515
516 if (err == ERROR_ACCESS_DENIED)
517 return -EACCES;
518 return -1;
519 }
520 return 0;
521 }
522
523 static int hdev_has_zero_init(BlockDriverState *bs)
524 {
525 return 0;
526 }
527
528 static BlockDriver bdrv_host_device = {
529 .format_name = "host_device",
530 .protocol_name = "host_device",
531 .instance_size = sizeof(BDRVRawState),
532 .bdrv_probe_device = hdev_probe_device,
533 .bdrv_file_open = hdev_open,
534 .bdrv_close = raw_close,
535 .bdrv_has_zero_init = hdev_has_zero_init,
536
537 .bdrv_aio_readv = raw_aio_readv,
538 .bdrv_aio_writev = raw_aio_writev,
539 .bdrv_aio_flush = raw_aio_flush,
540
541 .bdrv_getlength = raw_getlength,
542 .bdrv_get_allocated_file_size
543 = raw_get_allocated_file_size,
544 };
545
546 static void bdrv_file_init(void)
547 {
548 bdrv_register(&bdrv_file);
549 bdrv_register(&bdrv_host_device);
550 }
551
552 block_init(bdrv_file_init);