aes: make Td[0-5] and Te[0-5] tables non static
[qemu/ar7.git] / block / raw-win32.c
blobece2f1a8093fb8761a7784639214a5b74d4e101c
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 break;
90 offset += len;
93 return offset;
96 static int aio_worker(void *arg)
98 RawWin32AIOData *aiocb = arg;
99 ssize_t ret = 0;
100 size_t count;
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);
111 count = aiocb->aio_nbytes;
113 if (count == aiocb->aio_nbytes) {
114 ret = 0;
115 } else {
116 ret = -EINVAL;
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;
126 break;
127 case QEMU_AIO_FLUSH:
128 if (!FlushFileBuffers(aiocb->hfile)) {
129 return -EIO;
131 break;
132 default:
133 fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
134 ret = -EINVAL;
135 break;
138 g_slice_free(RawWin32AIOData, aiocb);
139 return ret;
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)
146 RawWin32AIOData *acb = g_slice_new(RawWin32AIOData);
147 ThreadPool *pool;
149 acb->bs = bs;
150 acb->hfile = hfile;
151 acb->aio_type = type;
153 if (qiov) {
154 acb->aio_iov = qiov->iov;
155 acb->aio_niov = qiov->niov;
157 acb->aio_nbytes = nb_sectors * 512;
158 acb->aio_offset = sector_num * 512;
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);
165 int qemu_ftruncate64(int fd, int64_t length)
167 LARGE_INTEGER li;
168 DWORD dw;
169 LONG high;
170 HANDLE h;
171 BOOL res;
173 if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
174 return -1;
176 h = (HANDLE)_get_osfhandle(fd);
178 /* get current position, ftruncate do not change position */
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;
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;
190 res = SetEndOfFile(h);
192 /* back to old position */
193 SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
194 return res ? 0 : -1;
197 static int set_sparse(int fd)
199 DWORD returned;
200 return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
201 NULL, 0, NULL, 0, &returned, NULL);
204 static void raw_parse_flags(int flags, int *access_flags, DWORD *overlapped)
206 assert(access_flags != NULL);
207 assert(overlapped != NULL);
209 if (flags & BDRV_O_RDWR) {
210 *access_flags = GENERIC_READ | GENERIC_WRITE;
211 } else {
212 *access_flags = GENERIC_READ;
215 *overlapped = FILE_ATTRIBUTE_NORMAL;
216 if (flags & BDRV_O_NATIVE_AIO) {
217 *overlapped |= FILE_FLAG_OVERLAPPED;
219 if (flags & BDRV_O_NOCACHE) {
220 *overlapped |= FILE_FLAG_NO_BUFFERING;
224 static int raw_open(BlockDriverState *bs, const char *filename,
225 QDict *options, int flags)
227 BDRVRawState *s = bs->opaque;
228 int access_flags;
229 DWORD overlapped;
231 s->type = FTYPE_FILE;
233 raw_parse_flags(flags, &access_flags, &overlapped);
235 if ((flags & BDRV_O_NATIVE_AIO) && aio == NULL) {
236 aio = win32_aio_init();
237 if (aio == NULL) {
238 return -EINVAL;
242 s->hfile = CreateFile(filename, access_flags,
243 FILE_SHARE_READ, NULL,
244 OPEN_EXISTING, overlapped, NULL);
245 if (s->hfile == INVALID_HANDLE_VALUE) {
246 int err = GetLastError();
248 if (err == ERROR_ACCESS_DENIED)
249 return -EACCES;
250 return -EINVAL;
253 if (flags & BDRV_O_NATIVE_AIO) {
254 int ret = win32_aio_attach(aio, s->hfile);
255 if (ret < 0) {
256 CloseHandle(s->hfile);
257 return ret;
259 s->aio = aio;
261 return 0;
264 static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs,
265 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
266 BlockDriverCompletionFunc *cb, void *opaque)
268 BDRVRawState *s = bs->opaque;
269 if (s->aio) {
270 return win32_aio_submit(bs, s->aio, s->hfile, sector_num, qiov,
271 nb_sectors, cb, opaque, QEMU_AIO_READ);
272 } else {
273 return paio_submit(bs, s->hfile, sector_num, qiov, nb_sectors,
274 cb, opaque, QEMU_AIO_READ);
278 static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
279 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
280 BlockDriverCompletionFunc *cb, void *opaque)
282 BDRVRawState *s = bs->opaque;
283 if (s->aio) {
284 return win32_aio_submit(bs, s->aio, s->hfile, sector_num, qiov,
285 nb_sectors, cb, opaque, QEMU_AIO_WRITE);
286 } else {
287 return paio_submit(bs, s->hfile, sector_num, qiov, nb_sectors,
288 cb, opaque, QEMU_AIO_WRITE);
292 static BlockDriverAIOCB *raw_aio_flush(BlockDriverState *bs,
293 BlockDriverCompletionFunc *cb, void *opaque)
295 BDRVRawState *s = bs->opaque;
296 return paio_submit(bs, s->hfile, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
299 static void raw_close(BlockDriverState *bs)
301 BDRVRawState *s = bs->opaque;
302 CloseHandle(s->hfile);
305 static int raw_truncate(BlockDriverState *bs, int64_t offset)
307 BDRVRawState *s = bs->opaque;
308 LONG low, high;
309 DWORD dwPtrLow;
311 low = offset;
312 high = offset >> 32;
315 * An error has occurred if the return value is INVALID_SET_FILE_POINTER
316 * and GetLastError doesn't return NO_ERROR.
318 dwPtrLow = SetFilePointer(s->hfile, low, &high, FILE_BEGIN);
319 if (dwPtrLow == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
320 fprintf(stderr, "SetFilePointer error: %lu\n", GetLastError());
321 return -EIO;
323 if (SetEndOfFile(s->hfile) == 0) {
324 fprintf(stderr, "SetEndOfFile error: %lu\n", GetLastError());
325 return -EIO;
327 return 0;
330 static int64_t raw_getlength(BlockDriverState *bs)
332 BDRVRawState *s = bs->opaque;
333 LARGE_INTEGER l;
334 ULARGE_INTEGER available, total, total_free;
335 DISK_GEOMETRY_EX dg;
336 DWORD count;
337 BOOL status;
339 switch(s->type) {
340 case FTYPE_FILE:
341 l.LowPart = GetFileSize(s->hfile, (PDWORD)&l.HighPart);
342 if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
343 return -EIO;
344 break;
345 case FTYPE_CD:
346 if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
347 return -EIO;
348 l.QuadPart = total.QuadPart;
349 break;
350 case FTYPE_HARDDISK:
351 status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
352 NULL, 0, &dg, sizeof(dg), &count, NULL);
353 if (status != 0) {
354 l = dg.DiskSize;
356 break;
357 default:
358 return -EIO;
360 return l.QuadPart;
363 static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
365 typedef DWORD (WINAPI * get_compressed_t)(const char *filename,
366 DWORD * high);
367 get_compressed_t get_compressed;
368 struct _stati64 st;
369 const char *filename = bs->filename;
370 /* WinNT support GetCompressedFileSize to determine allocate size */
371 get_compressed =
372 (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"),
373 "GetCompressedFileSizeA");
374 if (get_compressed) {
375 DWORD high, low;
376 low = get_compressed(filename, &high);
377 if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR) {
378 return (((int64_t) high) << 32) + low;
382 if (_stati64(filename, &st) < 0) {
383 return -1;
385 return st.st_size;
388 static int raw_create(const char *filename, QEMUOptionParameter *options)
390 int fd;
391 int64_t total_size = 0;
393 /* Read out options */
394 while (options && options->name) {
395 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
396 total_size = options->value.n / 512;
398 options++;
401 fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
402 0644);
403 if (fd < 0)
404 return -EIO;
405 set_sparse(fd);
406 ftruncate(fd, total_size * 512);
407 qemu_close(fd);
408 return 0;
411 static QEMUOptionParameter raw_create_options[] = {
413 .name = BLOCK_OPT_SIZE,
414 .type = OPT_SIZE,
415 .help = "Virtual disk size"
417 { NULL }
420 static BlockDriver bdrv_file = {
421 .format_name = "file",
422 .protocol_name = "file",
423 .instance_size = sizeof(BDRVRawState),
424 .bdrv_file_open = raw_open,
425 .bdrv_close = raw_close,
426 .bdrv_create = raw_create,
428 .bdrv_aio_readv = raw_aio_readv,
429 .bdrv_aio_writev = raw_aio_writev,
430 .bdrv_aio_flush = raw_aio_flush,
432 .bdrv_truncate = raw_truncate,
433 .bdrv_getlength = raw_getlength,
434 .bdrv_get_allocated_file_size
435 = raw_get_allocated_file_size,
437 .create_options = raw_create_options,
440 /***********************************************/
441 /* host device */
443 static int find_cdrom(char *cdrom_name, int cdrom_name_size)
445 char drives[256], *pdrv = drives;
446 UINT type;
448 memset(drives, 0, sizeof(drives));
449 GetLogicalDriveStrings(sizeof(drives), drives);
450 while(pdrv[0] != '\0') {
451 type = GetDriveType(pdrv);
452 switch(type) {
453 case DRIVE_CDROM:
454 snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
455 return 0;
456 break;
458 pdrv += lstrlen(pdrv) + 1;
460 return -1;
463 static int find_device_type(BlockDriverState *bs, const char *filename)
465 BDRVRawState *s = bs->opaque;
466 UINT type;
467 const char *p;
469 if (strstart(filename, "\\\\.\\", &p) ||
470 strstart(filename, "//./", &p)) {
471 if (stristart(p, "PhysicalDrive", NULL))
472 return FTYPE_HARDDISK;
473 snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
474 type = GetDriveType(s->drive_path);
475 switch (type) {
476 case DRIVE_REMOVABLE:
477 case DRIVE_FIXED:
478 return FTYPE_HARDDISK;
479 case DRIVE_CDROM:
480 return FTYPE_CD;
481 default:
482 return FTYPE_FILE;
484 } else {
485 return FTYPE_FILE;
489 static int hdev_probe_device(const char *filename)
491 if (strstart(filename, "/dev/cdrom", NULL))
492 return 100;
493 if (is_windows_drive(filename))
494 return 100;
495 return 0;
498 static int hdev_open(BlockDriverState *bs, const char *filename,
499 QDict *options, int flags)
501 BDRVRawState *s = bs->opaque;
502 int access_flags, create_flags;
503 DWORD overlapped;
504 char device_name[64];
506 if (strstart(filename, "/dev/cdrom", NULL)) {
507 if (find_cdrom(device_name, sizeof(device_name)) < 0)
508 return -ENOENT;
509 filename = device_name;
510 } else {
511 /* transform drive letters into device name */
512 if (((filename[0] >= 'a' && filename[0] <= 'z') ||
513 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
514 filename[1] == ':' && filename[2] == '\0') {
515 snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
516 filename = device_name;
519 s->type = find_device_type(bs, filename);
521 raw_parse_flags(flags, &access_flags, &overlapped);
523 create_flags = OPEN_EXISTING;
525 s->hfile = CreateFile(filename, access_flags,
526 FILE_SHARE_READ, NULL,
527 create_flags, overlapped, NULL);
528 if (s->hfile == INVALID_HANDLE_VALUE) {
529 int err = GetLastError();
531 if (err == ERROR_ACCESS_DENIED)
532 return -EACCES;
533 return -1;
535 return 0;
538 static int hdev_has_zero_init(BlockDriverState *bs)
540 return 0;
543 static BlockDriver bdrv_host_device = {
544 .format_name = "host_device",
545 .protocol_name = "host_device",
546 .instance_size = sizeof(BDRVRawState),
547 .bdrv_probe_device = hdev_probe_device,
548 .bdrv_file_open = hdev_open,
549 .bdrv_close = raw_close,
550 .bdrv_has_zero_init = hdev_has_zero_init,
552 .bdrv_aio_readv = raw_aio_readv,
553 .bdrv_aio_writev = raw_aio_writev,
554 .bdrv_aio_flush = raw_aio_flush,
556 .bdrv_getlength = raw_getlength,
557 .bdrv_get_allocated_file_size
558 = raw_get_allocated_file_size,
561 static void bdrv_file_init(void)
563 bdrv_register(&bdrv_file);
564 bdrv_register(&bdrv_host_device);
567 block_init(bdrv_file_init);