]> git.proxmox.com Git - mirror_qemu.git/blame - block/raw-win32.c
rbd: fix compile error
[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) {
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 /* 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
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);
c4d9d196 147 ThreadPool *pool;
fc4edb84
PB
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);
c4d9d196
SH
161 pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
162 return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque);
fc4edb84
PB
163}
164
223d4670
TS
165int qemu_ftruncate64(int fd, int64_t length)
166{
167 LARGE_INTEGER li;
2c993ec2 168 DWORD dw;
223d4670
TS
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 /* get current position, ftruncate do not change position */
179 li.HighPart = 0;
180 li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
2c993ec2 181 if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
223d4670 182 return -1;
2c993ec2 183 }
223d4670
TS
184
185 high = length >> 32;
2c993ec2
SW
186 dw = SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN);
187 if (dw == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
223d4670 188 return -1;
2c993ec2 189 }
223d4670
TS
190 res = SetEndOfFile(h);
191
192 /* back to old position */
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
6a8dc042
JC
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;
a2736526
PB
216 if (flags & BDRV_O_NATIVE_AIO) {
217 *overlapped |= FILE_FLAG_OVERLAPPED;
218 }
6a8dc042
JC
219 if (flags & BDRV_O_NOCACHE) {
220 *overlapped |= FILE_FLAG_NO_BUFFERING;
221 }
6a8dc042
JC
222}
223
223d4670
TS
224static int raw_open(BlockDriverState *bs, const char *filename, int flags)
225{
226 BDRVRawState *s = bs->opaque;
9a2d77ad 227 int access_flags;
223d4670
TS
228 DWORD overlapped;
229
230 s->type = FTYPE_FILE;
231
6a8dc042 232 raw_parse_flags(flags, &access_flags, &overlapped);
a2736526
PB
233
234 if ((flags & BDRV_O_NATIVE_AIO) && aio == NULL) {
235 aio = win32_aio_init();
236 if (aio == NULL) {
237 return -EINVAL;
238 }
239 }
9a2d77ad 240
223d4670
TS
241 s->hfile = CreateFile(filename, access_flags,
242 FILE_SHARE_READ, NULL,
9a2d77ad 243 OPEN_EXISTING, overlapped, NULL);
223d4670
TS
244 if (s->hfile == INVALID_HANDLE_VALUE) {
245 int err = GetLastError();
246
247 if (err == ERROR_ACCESS_DENIED)
248 return -EACCES;
a2736526
PB
249 return -EINVAL;
250 }
251
252 if (flags & BDRV_O_NATIVE_AIO) {
253 int ret = win32_aio_attach(aio, s->hfile);
254 if (ret < 0) {
255 CloseHandle(s->hfile);
256 return ret;
257 }
258 s->aio = aio;
223d4670
TS
259 }
260 return 0;
261}
262
fc4edb84
PB
263static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs,
264 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
265 BlockDriverCompletionFunc *cb, void *opaque)
223d4670
TS
266{
267 BDRVRawState *s = bs->opaque;
a2736526
PB
268 if (s->aio) {
269 return win32_aio_submit(bs, s->aio, s->hfile, sector_num, qiov,
270 nb_sectors, cb, opaque, QEMU_AIO_READ);
271 } else {
272 return paio_submit(bs, s->hfile, sector_num, qiov, nb_sectors,
273 cb, opaque, QEMU_AIO_READ);
274 }
223d4670
TS
275}
276
fc4edb84
PB
277static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
278 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
279 BlockDriverCompletionFunc *cb, void *opaque)
223d4670
TS
280{
281 BDRVRawState *s = bs->opaque;
a2736526
PB
282 if (s->aio) {
283 return win32_aio_submit(bs, s->aio, s->hfile, sector_num, qiov,
284 nb_sectors, cb, opaque, QEMU_AIO_WRITE);
285 } else {
286 return paio_submit(bs, s->hfile, sector_num, qiov, nb_sectors,
287 cb, opaque, QEMU_AIO_WRITE);
288 }
223d4670
TS
289}
290
fc4edb84
PB
291static BlockDriverAIOCB *raw_aio_flush(BlockDriverState *bs,
292 BlockDriverCompletionFunc *cb, void *opaque)
223d4670
TS
293{
294 BDRVRawState *s = bs->opaque;
fc4edb84 295 return paio_submit(bs, s->hfile, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
223d4670
TS
296}
297
298static void raw_close(BlockDriverState *bs)
299{
300 BDRVRawState *s = bs->opaque;
301 CloseHandle(s->hfile);
302}
303
304static int raw_truncate(BlockDriverState *bs, int64_t offset)
305{
306 BDRVRawState *s = bs->opaque;
b9e82a59 307 LONG low, high;
fbcad04d 308 DWORD dwPtrLow;
223d4670
TS
309
310 low = offset;
311 high = offset >> 32;
fbcad04d
FC
312
313 /*
314 * An error has occurred if the return value is INVALID_SET_FILE_POINTER
315 * and GetLastError doesn't return NO_ERROR.
316 */
317 dwPtrLow = SetFilePointer(s->hfile, low, &high, FILE_BEGIN);
318 if (dwPtrLow == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
fccedc62 319 fprintf(stderr, "SetFilePointer error: %lu\n", GetLastError());
fbcad04d
FC
320 return -EIO;
321 }
322 if (SetEndOfFile(s->hfile) == 0) {
fccedc62 323 fprintf(stderr, "SetEndOfFile error: %lu\n", GetLastError());
223d4670 324 return -EIO;
fbcad04d 325 }
223d4670
TS
326 return 0;
327}
328
329static int64_t raw_getlength(BlockDriverState *bs)
330{
331 BDRVRawState *s = bs->opaque;
332 LARGE_INTEGER l;
333 ULARGE_INTEGER available, total, total_free;
334 DISK_GEOMETRY_EX dg;
335 DWORD count;
336 BOOL status;
337
338 switch(s->type) {
339 case FTYPE_FILE:
b9e82a59 340 l.LowPart = GetFileSize(s->hfile, (PDWORD)&l.HighPart);
223d4670
TS
341 if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
342 return -EIO;
343 break;
344 case FTYPE_CD:
345 if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
346 return -EIO;
347 l.QuadPart = total.QuadPart;
348 break;
349 case FTYPE_HARDDISK:
350 status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
351 NULL, 0, &dg, sizeof(dg), &count, NULL);
352 if (status != 0) {
353 l = dg.DiskSize;
354 }
355 break;
356 default:
357 return -EIO;
358 }
359 return l.QuadPart;
360}
361
4a1d5e1f
FZ
362static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
363{
364 typedef DWORD (WINAPI * get_compressed_t)(const char *filename,
365 DWORD * high);
366 get_compressed_t get_compressed;
367 struct _stati64 st;
368 const char *filename = bs->filename;
369 /* WinNT support GetCompressedFileSize to determine allocate size */
370 get_compressed =
371 (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"),
372 "GetCompressedFileSizeA");
373 if (get_compressed) {
374 DWORD high, low;
375 low = get_compressed(filename, &high);
376 if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR) {
377 return (((int64_t) high) << 32) + low;
378 }
379 }
380
381 if (_stati64(filename, &st) < 0) {
382 return -1;
383 }
384 return st.st_size;
385}
386
0e7e1989 387static int raw_create(const char *filename, QEMUOptionParameter *options)
223d4670
TS
388{
389 int fd;
0e7e1989 390 int64_t total_size = 0;
223d4670 391
0e7e1989
KW
392 /* Read out options */
393 while (options && options->name) {
394 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
395 total_size = options->value.n / 512;
396 }
397 options++;
398 }
223d4670 399
6165f4d8
CB
400 fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
401 0644);
223d4670
TS
402 if (fd < 0)
403 return -EIO;
404 set_sparse(fd);
405 ftruncate(fd, total_size * 512);
2e1e79da 406 qemu_close(fd);
223d4670
TS
407 return 0;
408}
409
0e7e1989 410static QEMUOptionParameter raw_create_options[] = {
db08adf5
KW
411 {
412 .name = BLOCK_OPT_SIZE,
413 .type = OPT_SIZE,
414 .help = "Virtual disk size"
415 },
0e7e1989
KW
416 { NULL }
417};
418
84a12e66
CH
419static BlockDriver bdrv_file = {
420 .format_name = "file",
421 .protocol_name = "file",
f1b2f712 422 .instance_size = sizeof(BDRVRawState),
66f82cee 423 .bdrv_file_open = raw_open,
f1b2f712
AL
424 .bdrv_close = raw_close,
425 .bdrv_create = raw_create,
c68b89ac 426
fc4edb84
PB
427 .bdrv_aio_readv = raw_aio_readv,
428 .bdrv_aio_writev = raw_aio_writev,
429 .bdrv_aio_flush = raw_aio_flush,
c68b89ac 430
f1b2f712
AL
431 .bdrv_truncate = raw_truncate,
432 .bdrv_getlength = raw_getlength,
4a1d5e1f
FZ
433 .bdrv_get_allocated_file_size
434 = raw_get_allocated_file_size,
0e7e1989
KW
435
436 .create_options = raw_create_options,
223d4670
TS
437};
438
439/***********************************************/
440/* host device */
441
442static int find_cdrom(char *cdrom_name, int cdrom_name_size)
443{
444 char drives[256], *pdrv = drives;
445 UINT type;
446
447 memset(drives, 0, sizeof(drives));
448 GetLogicalDriveStrings(sizeof(drives), drives);
449 while(pdrv[0] != '\0') {
450 type = GetDriveType(pdrv);
451 switch(type) {
452 case DRIVE_CDROM:
453 snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
454 return 0;
455 break;
456 }
457 pdrv += lstrlen(pdrv) + 1;
458 }
459 return -1;
460}
461
462static int find_device_type(BlockDriverState *bs, const char *filename)
463{
464 BDRVRawState *s = bs->opaque;
465 UINT type;
466 const char *p;
467
468 if (strstart(filename, "\\\\.\\", &p) ||
469 strstart(filename, "//./", &p)) {
470 if (stristart(p, "PhysicalDrive", NULL))
471 return FTYPE_HARDDISK;
472 snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
473 type = GetDriveType(s->drive_path);
3060cd14
AL
474 switch (type) {
475 case DRIVE_REMOVABLE:
476 case DRIVE_FIXED:
477 return FTYPE_HARDDISK;
478 case DRIVE_CDROM:
223d4670 479 return FTYPE_CD;
3060cd14 480 default:
223d4670 481 return FTYPE_FILE;
3060cd14 482 }
223d4670
TS
483 } else {
484 return FTYPE_FILE;
485 }
486}
487
508c7cb3
CH
488static int hdev_probe_device(const char *filename)
489{
490 if (strstart(filename, "/dev/cdrom", NULL))
491 return 100;
492 if (is_windows_drive(filename))
493 return 100;
494 return 0;
495}
496
223d4670
TS
497static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
498{
499 BDRVRawState *s = bs->opaque;
500 int access_flags, create_flags;
501 DWORD overlapped;
502 char device_name[64];
503
504 if (strstart(filename, "/dev/cdrom", NULL)) {
505 if (find_cdrom(device_name, sizeof(device_name)) < 0)
506 return -ENOENT;
507 filename = device_name;
508 } else {
509 /* transform drive letters into device name */
510 if (((filename[0] >= 'a' && filename[0] <= 'z') ||
511 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
512 filename[1] == ':' && filename[2] == '\0') {
513 snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
514 filename = device_name;
515 }
516 }
517 s->type = find_device_type(bs, filename);
518
6a8dc042
JC
519 raw_parse_flags(flags, &access_flags, &overlapped);
520
223d4670
TS
521 create_flags = OPEN_EXISTING;
522
223d4670
TS
523 s->hfile = CreateFile(filename, access_flags,
524 FILE_SHARE_READ, NULL,
525 create_flags, overlapped, NULL);
526 if (s->hfile == INVALID_HANDLE_VALUE) {
527 int err = GetLastError();
528
529 if (err == ERROR_ACCESS_DENIED)
530 return -EACCES;
531 return -1;
532 }
533 return 0;
534}
535
336c1c12
KW
536static int hdev_has_zero_init(BlockDriverState *bs)
537{
538 return 0;
539}
540
5efa9d5a 541static BlockDriver bdrv_host_device = {
e60f469c 542 .format_name = "host_device",
84a12e66 543 .protocol_name = "host_device",
e60f469c 544 .instance_size = sizeof(BDRVRawState),
508c7cb3 545 .bdrv_probe_device = hdev_probe_device,
66f82cee 546 .bdrv_file_open = hdev_open,
e60f469c 547 .bdrv_close = raw_close,
336c1c12 548 .bdrv_has_zero_init = hdev_has_zero_init,
223d4670 549
fc4edb84
PB
550 .bdrv_aio_readv = raw_aio_readv,
551 .bdrv_aio_writev = raw_aio_writev,
552 .bdrv_aio_flush = raw_aio_flush,
c68b89ac 553
e60f469c 554 .bdrv_getlength = raw_getlength,
4a1d5e1f
FZ
555 .bdrv_get_allocated_file_size
556 = raw_get_allocated_file_size,
223d4670 557};
5efa9d5a 558
84a12e66 559static void bdrv_file_init(void)
5efa9d5a 560{
84a12e66 561 bdrv_register(&bdrv_file);
5efa9d5a
AL
562 bdrv_register(&bdrv_host_device);
563}
564
84a12e66 565block_init(bdrv_file_init);