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