]>
Commit | Line | Data |
---|---|---|
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" | |
25 | #ifndef QEMU_IMG | |
26 | #include "qemu-timer.h" | |
27 | #include "exec-all.h" | |
28 | #endif | |
29 | #include "block_int.h" | |
30 | #include <assert.h> | |
31 | #include <winioctl.h> | |
32 | ||
33 | #define FTYPE_FILE 0 | |
34 | #define FTYPE_CD 1 | |
35 | #define FTYPE_HARDDISK 2 | |
36 | ||
37 | typedef struct BDRVRawState { | |
38 | HANDLE hfile; | |
39 | int type; | |
40 | char drive_path[16]; /* format: "d:\" */ | |
41 | } BDRVRawState; | |
42 | ||
43 | typedef struct RawAIOCB { | |
44 | BlockDriverAIOCB common; | |
45 | HANDLE hEvent; | |
46 | OVERLAPPED ov; | |
47 | int count; | |
48 | } RawAIOCB; | |
49 | ||
50 | int qemu_ftruncate64(int fd, int64_t length) | |
51 | { | |
52 | LARGE_INTEGER li; | |
53 | LONG high; | |
54 | HANDLE h; | |
55 | BOOL res; | |
56 | ||
57 | if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0) | |
58 | return -1; | |
59 | ||
60 | h = (HANDLE)_get_osfhandle(fd); | |
61 | ||
62 | /* get current position, ftruncate do not change position */ | |
63 | li.HighPart = 0; | |
64 | li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT); | |
65 | if (li.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR) | |
66 | return -1; | |
67 | ||
68 | high = length >> 32; | |
69 | if (!SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN)) | |
70 | return -1; | |
71 | res = SetEndOfFile(h); | |
72 | ||
73 | /* back to old position */ | |
74 | SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN); | |
75 | return res ? 0 : -1; | |
76 | } | |
77 | ||
78 | static int set_sparse(int fd) | |
79 | { | |
80 | DWORD returned; | |
81 | return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE, | |
82 | NULL, 0, NULL, 0, &returned, NULL); | |
83 | } | |
84 | ||
85 | static int raw_open(BlockDriverState *bs, const char *filename, int flags) | |
86 | { | |
87 | BDRVRawState *s = bs->opaque; | |
88 | int access_flags, create_flags; | |
89 | DWORD overlapped; | |
90 | ||
91 | s->type = FTYPE_FILE; | |
92 | ||
93 | if ((flags & BDRV_O_ACCESS) == O_RDWR) { | |
94 | access_flags = GENERIC_READ | GENERIC_WRITE; | |
95 | } else { | |
96 | access_flags = GENERIC_READ; | |
97 | } | |
98 | if (flags & BDRV_O_CREAT) { | |
99 | create_flags = CREATE_ALWAYS; | |
100 | } else { | |
101 | create_flags = OPEN_EXISTING; | |
102 | } | |
103 | #ifdef QEMU_IMG | |
104 | overlapped = FILE_ATTRIBUTE_NORMAL; | |
105 | #else | |
106 | overlapped = FILE_FLAG_OVERLAPPED; | |
107 | #endif | |
108 | s->hfile = CreateFile(filename, access_flags, | |
109 | FILE_SHARE_READ, NULL, | |
110 | create_flags, overlapped, NULL); | |
111 | if (s->hfile == INVALID_HANDLE_VALUE) { | |
112 | int err = GetLastError(); | |
113 | ||
114 | if (err == ERROR_ACCESS_DENIED) | |
115 | return -EACCES; | |
116 | return -1; | |
117 | } | |
118 | return 0; | |
119 | } | |
120 | ||
121 | static int raw_pread(BlockDriverState *bs, int64_t offset, | |
122 | uint8_t *buf, int count) | |
123 | { | |
124 | BDRVRawState *s = bs->opaque; | |
125 | OVERLAPPED ov; | |
126 | DWORD ret_count; | |
127 | int ret; | |
128 | ||
129 | memset(&ov, 0, sizeof(ov)); | |
130 | ov.Offset = offset; | |
131 | ov.OffsetHigh = offset >> 32; | |
132 | ret = ReadFile(s->hfile, buf, count, &ret_count, &ov); | |
133 | if (!ret) { | |
134 | ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE); | |
135 | if (!ret) | |
136 | return -EIO; | |
137 | else | |
138 | return ret_count; | |
139 | } | |
140 | return ret_count; | |
141 | } | |
142 | ||
143 | static int raw_pwrite(BlockDriverState *bs, int64_t offset, | |
144 | const uint8_t *buf, int count) | |
145 | { | |
146 | BDRVRawState *s = bs->opaque; | |
147 | OVERLAPPED ov; | |
148 | DWORD ret_count; | |
149 | int ret; | |
150 | ||
151 | memset(&ov, 0, sizeof(ov)); | |
152 | ov.Offset = offset; | |
153 | ov.OffsetHigh = offset >> 32; | |
154 | ret = WriteFile(s->hfile, buf, count, &ret_count, &ov); | |
155 | if (!ret) { | |
156 | ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE); | |
157 | if (!ret) | |
158 | return -EIO; | |
159 | else | |
160 | return ret_count; | |
161 | } | |
162 | return ret_count; | |
163 | } | |
164 | ||
165 | #if 0 | |
166 | #ifndef QEMU_IMG | |
167 | static void raw_aio_cb(void *opaque) | |
168 | { | |
169 | RawAIOCB *acb = opaque; | |
170 | BlockDriverState *bs = acb->common.bs; | |
171 | BDRVRawState *s = bs->opaque; | |
172 | DWORD ret_count; | |
173 | int ret; | |
174 | ||
175 | ret = GetOverlappedResult(s->hfile, &acb->ov, &ret_count, TRUE); | |
176 | if (!ret || ret_count != acb->count) { | |
177 | acb->common.cb(acb->common.opaque, -EIO); | |
178 | } else { | |
179 | acb->common.cb(acb->common.opaque, 0); | |
180 | } | |
181 | } | |
182 | #endif | |
183 | ||
184 | static RawAIOCB *raw_aio_setup(BlockDriverState *bs, | |
185 | int64_t sector_num, uint8_t *buf, int nb_sectors, | |
186 | BlockDriverCompletionFunc *cb, void *opaque) | |
187 | { | |
188 | RawAIOCB *acb; | |
189 | int64_t offset; | |
190 | ||
191 | acb = qemu_aio_get(bs, cb, opaque); | |
192 | if (acb->hEvent) { | |
193 | acb->hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); | |
194 | if (!acb->hEvent) { | |
195 | qemu_aio_release(acb); | |
196 | return NULL; | |
197 | } | |
198 | } | |
199 | memset(&acb->ov, 0, sizeof(acb->ov)); | |
200 | offset = sector_num * 512; | |
201 | acb->ov.Offset = offset; | |
202 | acb->ov.OffsetHigh = offset >> 32; | |
203 | acb->ov.hEvent = acb->hEvent; | |
204 | acb->count = nb_sectors * 512; | |
205 | #ifndef QEMU_IMG | |
206 | qemu_add_wait_object(acb->ov.hEvent, raw_aio_cb, acb); | |
207 | #endif | |
208 | return acb; | |
209 | } | |
210 | ||
211 | static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs, | |
212 | int64_t sector_num, uint8_t *buf, int nb_sectors, | |
213 | BlockDriverCompletionFunc *cb, void *opaque) | |
214 | { | |
215 | BDRVRawState *s = bs->opaque; | |
216 | RawAIOCB *acb; | |
217 | int ret; | |
218 | ||
219 | acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque); | |
220 | if (!acb) | |
221 | return NULL; | |
222 | ret = ReadFile(s->hfile, buf, acb->count, NULL, &acb->ov); | |
223 | if (!ret) { | |
224 | qemu_aio_release(acb); | |
225 | return NULL; | |
226 | } | |
227 | #ifdef QEMU_IMG | |
228 | qemu_aio_release(acb); | |
229 | #endif | |
230 | return (BlockDriverAIOCB *)acb; | |
231 | } | |
232 | ||
233 | static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs, | |
234 | int64_t sector_num, uint8_t *buf, int nb_sectors, | |
235 | BlockDriverCompletionFunc *cb, void *opaque) | |
236 | { | |
237 | BDRVRawState *s = bs->opaque; | |
238 | RawAIOCB *acb; | |
239 | int ret; | |
240 | ||
241 | acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque); | |
242 | if (!acb) | |
243 | return NULL; | |
244 | ret = WriteFile(s->hfile, buf, acb->count, NULL, &acb->ov); | |
245 | if (!ret) { | |
246 | qemu_aio_release(acb); | |
247 | return NULL; | |
248 | } | |
249 | #ifdef QEMU_IMG | |
250 | qemu_aio_release(acb); | |
251 | #endif | |
252 | return (BlockDriverAIOCB *)acb; | |
253 | } | |
254 | ||
255 | static void raw_aio_cancel(BlockDriverAIOCB *blockacb) | |
256 | { | |
257 | #ifndef QEMU_IMG | |
258 | RawAIOCB *acb = (RawAIOCB *)blockacb; | |
259 | BlockDriverState *bs = acb->common.bs; | |
260 | BDRVRawState *s = bs->opaque; | |
261 | ||
262 | qemu_del_wait_object(acb->ov.hEvent, raw_aio_cb, acb); | |
263 | /* XXX: if more than one async I/O it is not correct */ | |
264 | CancelIo(s->hfile); | |
265 | qemu_aio_release(acb); | |
266 | #endif | |
267 | } | |
268 | #endif /* #if 0 */ | |
269 | ||
270 | static void raw_flush(BlockDriverState *bs) | |
271 | { | |
272 | BDRVRawState *s = bs->opaque; | |
273 | FlushFileBuffers(s->hfile); | |
274 | } | |
275 | ||
276 | static void raw_close(BlockDriverState *bs) | |
277 | { | |
278 | BDRVRawState *s = bs->opaque; | |
279 | CloseHandle(s->hfile); | |
280 | } | |
281 | ||
282 | static int raw_truncate(BlockDriverState *bs, int64_t offset) | |
283 | { | |
284 | BDRVRawState *s = bs->opaque; | |
285 | DWORD low, high; | |
286 | ||
287 | low = offset; | |
288 | high = offset >> 32; | |
289 | if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN)) | |
290 | return -EIO; | |
291 | if (!SetEndOfFile(s->hfile)) | |
292 | return -EIO; | |
293 | return 0; | |
294 | } | |
295 | ||
296 | static int64_t raw_getlength(BlockDriverState *bs) | |
297 | { | |
298 | BDRVRawState *s = bs->opaque; | |
299 | LARGE_INTEGER l; | |
300 | ULARGE_INTEGER available, total, total_free; | |
301 | DISK_GEOMETRY_EX dg; | |
302 | DWORD count; | |
303 | BOOL status; | |
304 | ||
305 | switch(s->type) { | |
306 | case FTYPE_FILE: | |
307 | l.LowPart = GetFileSize(s->hfile, &l.HighPart); | |
308 | if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR) | |
309 | return -EIO; | |
310 | break; | |
311 | case FTYPE_CD: | |
312 | if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free)) | |
313 | return -EIO; | |
314 | l.QuadPart = total.QuadPart; | |
315 | break; | |
316 | case FTYPE_HARDDISK: | |
317 | status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, | |
318 | NULL, 0, &dg, sizeof(dg), &count, NULL); | |
319 | if (status != 0) { | |
320 | l = dg.DiskSize; | |
321 | } | |
322 | break; | |
323 | default: | |
324 | return -EIO; | |
325 | } | |
326 | return l.QuadPart; | |
327 | } | |
328 | ||
329 | static int raw_create(const char *filename, int64_t total_size, | |
330 | const char *backing_file, int flags) | |
331 | { | |
332 | int fd; | |
333 | ||
334 | if (flags || backing_file) | |
335 | return -ENOTSUP; | |
336 | ||
337 | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, | |
338 | 0644); | |
339 | if (fd < 0) | |
340 | return -EIO; | |
341 | set_sparse(fd); | |
342 | ftruncate(fd, total_size * 512); | |
343 | close(fd); | |
344 | return 0; | |
345 | } | |
346 | ||
347 | void qemu_aio_init(void) | |
348 | { | |
349 | } | |
350 | ||
351 | void qemu_aio_poll(void) | |
352 | { | |
353 | } | |
354 | ||
355 | void qemu_aio_flush(void) | |
356 | { | |
357 | } | |
358 | ||
359 | void qemu_aio_wait_start(void) | |
360 | { | |
361 | } | |
362 | ||
363 | void qemu_aio_wait(void) | |
364 | { | |
365 | #ifndef QEMU_IMG | |
366 | qemu_bh_poll(); | |
367 | #endif | |
368 | } | |
369 | ||
370 | void qemu_aio_wait_end(void) | |
371 | { | |
372 | } | |
373 | ||
374 | BlockDriver bdrv_raw = { | |
375 | "raw", | |
376 | sizeof(BDRVRawState), | |
377 | NULL, /* no probe for protocols */ | |
378 | raw_open, | |
379 | NULL, | |
380 | NULL, | |
381 | raw_close, | |
382 | raw_create, | |
383 | raw_flush, | |
384 | ||
385 | #if 0 | |
386 | .bdrv_aio_read = raw_aio_read, | |
387 | .bdrv_aio_write = raw_aio_write, | |
388 | .bdrv_aio_cancel = raw_aio_cancel, | |
389 | .aiocb_size = sizeof(RawAIOCB); | |
390 | #endif | |
391 | .protocol_name = "file", | |
392 | .bdrv_pread = raw_pread, | |
393 | .bdrv_pwrite = raw_pwrite, | |
394 | .bdrv_truncate = raw_truncate, | |
395 | .bdrv_getlength = raw_getlength, | |
396 | }; | |
397 | ||
398 | /***********************************************/ | |
399 | /* host device */ | |
400 | ||
401 | static int find_cdrom(char *cdrom_name, int cdrom_name_size) | |
402 | { | |
403 | char drives[256], *pdrv = drives; | |
404 | UINT type; | |
405 | ||
406 | memset(drives, 0, sizeof(drives)); | |
407 | GetLogicalDriveStrings(sizeof(drives), drives); | |
408 | while(pdrv[0] != '\0') { | |
409 | type = GetDriveType(pdrv); | |
410 | switch(type) { | |
411 | case DRIVE_CDROM: | |
412 | snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]); | |
413 | return 0; | |
414 | break; | |
415 | } | |
416 | pdrv += lstrlen(pdrv) + 1; | |
417 | } | |
418 | return -1; | |
419 | } | |
420 | ||
421 | static int find_device_type(BlockDriverState *bs, const char *filename) | |
422 | { | |
423 | BDRVRawState *s = bs->opaque; | |
424 | UINT type; | |
425 | const char *p; | |
426 | ||
427 | if (strstart(filename, "\\\\.\\", &p) || | |
428 | strstart(filename, "//./", &p)) { | |
429 | if (stristart(p, "PhysicalDrive", NULL)) | |
430 | return FTYPE_HARDDISK; | |
431 | snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]); | |
432 | type = GetDriveType(s->drive_path); | |
433 | if (type == DRIVE_CDROM) | |
434 | return FTYPE_CD; | |
435 | else | |
436 | return FTYPE_FILE; | |
437 | } else { | |
438 | return FTYPE_FILE; | |
439 | } | |
440 | } | |
441 | ||
442 | static int hdev_open(BlockDriverState *bs, const char *filename, int flags) | |
443 | { | |
444 | BDRVRawState *s = bs->opaque; | |
445 | int access_flags, create_flags; | |
446 | DWORD overlapped; | |
447 | char device_name[64]; | |
448 | ||
449 | if (strstart(filename, "/dev/cdrom", NULL)) { | |
450 | if (find_cdrom(device_name, sizeof(device_name)) < 0) | |
451 | return -ENOENT; | |
452 | filename = device_name; | |
453 | } else { | |
454 | /* transform drive letters into device name */ | |
455 | if (((filename[0] >= 'a' && filename[0] <= 'z') || | |
456 | (filename[0] >= 'A' && filename[0] <= 'Z')) && | |
457 | filename[1] == ':' && filename[2] == '\0') { | |
458 | snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]); | |
459 | filename = device_name; | |
460 | } | |
461 | } | |
462 | s->type = find_device_type(bs, filename); | |
463 | ||
464 | if ((flags & BDRV_O_ACCESS) == O_RDWR) { | |
465 | access_flags = GENERIC_READ | GENERIC_WRITE; | |
466 | } else { | |
467 | access_flags = GENERIC_READ; | |
468 | } | |
469 | create_flags = OPEN_EXISTING; | |
470 | ||
471 | #ifdef QEMU_IMG | |
472 | overlapped = FILE_ATTRIBUTE_NORMAL; | |
473 | #else | |
474 | overlapped = FILE_FLAG_OVERLAPPED; | |
475 | #endif | |
476 | s->hfile = CreateFile(filename, access_flags, | |
477 | FILE_SHARE_READ, NULL, | |
478 | create_flags, overlapped, NULL); | |
479 | if (s->hfile == INVALID_HANDLE_VALUE) { | |
480 | int err = GetLastError(); | |
481 | ||
482 | if (err == ERROR_ACCESS_DENIED) | |
483 | return -EACCES; | |
484 | return -1; | |
485 | } | |
486 | return 0; | |
487 | } | |
488 | ||
489 | #if 0 | |
490 | /***********************************************/ | |
491 | /* removable device additional commands */ | |
492 | ||
493 | static int raw_is_inserted(BlockDriverState *bs) | |
494 | { | |
495 | return 1; | |
496 | } | |
497 | ||
498 | static int raw_media_changed(BlockDriverState *bs) | |
499 | { | |
500 | return -ENOTSUP; | |
501 | } | |
502 | ||
503 | static int raw_eject(BlockDriverState *bs, int eject_flag) | |
504 | { | |
505 | DWORD ret_count; | |
506 | ||
507 | if (s->type == FTYPE_FILE) | |
508 | return -ENOTSUP; | |
509 | if (eject_flag) { | |
510 | DeviceIoControl(s->hfile, IOCTL_STORAGE_EJECT_MEDIA, | |
511 | NULL, 0, NULL, 0, &lpBytesReturned, NULL); | |
512 | } else { | |
513 | DeviceIoControl(s->hfile, IOCTL_STORAGE_LOAD_MEDIA, | |
514 | NULL, 0, NULL, 0, &lpBytesReturned, NULL); | |
515 | } | |
516 | } | |
517 | ||
518 | static int raw_set_locked(BlockDriverState *bs, int locked) | |
519 | { | |
520 | return -ENOTSUP; | |
521 | } | |
522 | #endif | |
523 | ||
524 | BlockDriver bdrv_host_device = { | |
525 | "host_device", | |
526 | sizeof(BDRVRawState), | |
527 | NULL, /* no probe for protocols */ | |
528 | hdev_open, | |
529 | NULL, | |
530 | NULL, | |
531 | raw_close, | |
532 | NULL, | |
533 | raw_flush, | |
534 | ||
535 | #if 0 | |
536 | .bdrv_aio_read = raw_aio_read, | |
537 | .bdrv_aio_write = raw_aio_write, | |
538 | .bdrv_aio_cancel = raw_aio_cancel, | |
539 | .aiocb_size = sizeof(RawAIOCB); | |
540 | #endif | |
541 | .bdrv_pread = raw_pread, | |
542 | .bdrv_pwrite = raw_pwrite, | |
543 | .bdrv_getlength = raw_getlength, | |
544 | }; |