]>
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" | |
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 |
39 | static QEMUWin32AIOState *aio; |
40 | ||
fc4edb84 PB |
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 | ||
223d4670 TS |
51 | typedef 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 | */ | |
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) { | |
56e023af | 88 | offset += ret_count; |
fc4edb84 PB |
89 | break; |
90 | } | |
91 | offset += len; | |
92 | } | |
93 | ||
94 | return offset; | |
95 | } | |
96 | ||
97 | static 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 | ||
143 | static 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 |
166 | int 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 | ||
198 | static 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 | ||
c25f53b0 PB |
205 | static void raw_probe_alignment(BlockDriverState *bs) |
206 | { | |
207 | BDRVRawState *s = bs->opaque; | |
208 | DWORD sectorsPerCluster, freeClusters, totalClusters, count; | |
209 | DISK_GEOMETRY_EX dg; | |
210 | BOOL status; | |
211 | ||
212 | if (s->type == FTYPE_CD) { | |
213 | bs->request_alignment = 2048; | |
214 | return; | |
215 | } | |
216 | if (s->type == FTYPE_HARDDISK) { | |
217 | status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, | |
218 | NULL, 0, &dg, sizeof(dg), &count, NULL); | |
219 | if (status != 0) { | |
220 | bs->request_alignment = dg.Geometry.BytesPerSector; | |
221 | return; | |
222 | } | |
223 | /* try GetDiskFreeSpace too */ | |
224 | } | |
225 | ||
226 | if (s->drive_path[0]) { | |
227 | GetDiskFreeSpace(s->drive_path, §orsPerCluster, | |
228 | &dg.Geometry.BytesPerSector, | |
229 | &freeClusters, &totalClusters); | |
230 | bs->request_alignment = dg.Geometry.BytesPerSector; | |
231 | } | |
232 | } | |
233 | ||
6a8dc042 JC |
234 | static void raw_parse_flags(int flags, int *access_flags, DWORD *overlapped) |
235 | { | |
236 | assert(access_flags != NULL); | |
237 | assert(overlapped != NULL); | |
238 | ||
239 | if (flags & BDRV_O_RDWR) { | |
240 | *access_flags = GENERIC_READ | GENERIC_WRITE; | |
241 | } else { | |
242 | *access_flags = GENERIC_READ; | |
243 | } | |
244 | ||
245 | *overlapped = FILE_ATTRIBUTE_NORMAL; | |
a2736526 PB |
246 | if (flags & BDRV_O_NATIVE_AIO) { |
247 | *overlapped |= FILE_FLAG_OVERLAPPED; | |
248 | } | |
6a8dc042 JC |
249 | if (flags & BDRV_O_NOCACHE) { |
250 | *overlapped |= FILE_FLAG_NO_BUFFERING; | |
251 | } | |
6a8dc042 JC |
252 | } |
253 | ||
8a79380b KW |
254 | static QemuOptsList raw_runtime_opts = { |
255 | .name = "raw", | |
256 | .head = QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head), | |
257 | .desc = { | |
258 | { | |
259 | .name = "filename", | |
260 | .type = QEMU_OPT_STRING, | |
261 | .help = "File name of the image", | |
262 | }, | |
263 | { /* end of list */ } | |
264 | }, | |
265 | }; | |
266 | ||
015a1036 HR |
267 | static int raw_open(BlockDriverState *bs, QDict *options, int flags, |
268 | Error **errp) | |
223d4670 TS |
269 | { |
270 | BDRVRawState *s = bs->opaque; | |
9a2d77ad | 271 | int access_flags; |
223d4670 | 272 | DWORD overlapped; |
8a79380b KW |
273 | QemuOpts *opts; |
274 | Error *local_err = NULL; | |
275 | const char *filename; | |
276 | int ret; | |
223d4670 TS |
277 | |
278 | s->type = FTYPE_FILE; | |
279 | ||
87ea75d5 | 280 | opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort); |
8a79380b KW |
281 | qemu_opts_absorb_qdict(opts, options, &local_err); |
282 | if (error_is_set(&local_err)) { | |
c6252b7c | 283 | error_propagate(errp, local_err); |
8a79380b KW |
284 | ret = -EINVAL; |
285 | goto fail; | |
286 | } | |
287 | ||
288 | filename = qemu_opt_get(opts, "filename"); | |
289 | ||
6a8dc042 | 290 | raw_parse_flags(flags, &access_flags, &overlapped); |
8a79380b | 291 | |
a2736526 PB |
292 | if ((flags & BDRV_O_NATIVE_AIO) && aio == NULL) { |
293 | aio = win32_aio_init(); | |
294 | if (aio == NULL) { | |
c6252b7c | 295 | error_setg(errp, "Could not initialize AIO"); |
8a79380b KW |
296 | ret = -EINVAL; |
297 | goto fail; | |
a2736526 PB |
298 | } |
299 | } | |
9a2d77ad | 300 | |
c25f53b0 PB |
301 | if (filename[0] && filename[1] == ':') { |
302 | snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", filename[0]); | |
303 | } else if (filename[0] == '\\' && filename[1] == '\\') { | |
304 | s->drive_path[0] = 0; | |
305 | } else { | |
306 | /* Relative path. */ | |
307 | char buf[MAX_PATH]; | |
308 | GetCurrentDirectory(MAX_PATH, buf); | |
309 | snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", buf[0]); | |
310 | } | |
311 | ||
223d4670 TS |
312 | s->hfile = CreateFile(filename, access_flags, |
313 | FILE_SHARE_READ, NULL, | |
9a2d77ad | 314 | OPEN_EXISTING, overlapped, NULL); |
223d4670 TS |
315 | if (s->hfile == INVALID_HANDLE_VALUE) { |
316 | int err = GetLastError(); | |
317 | ||
8a79380b KW |
318 | if (err == ERROR_ACCESS_DENIED) { |
319 | ret = -EACCES; | |
320 | } else { | |
321 | ret = -EINVAL; | |
322 | } | |
323 | goto fail; | |
a2736526 PB |
324 | } |
325 | ||
326 | if (flags & BDRV_O_NATIVE_AIO) { | |
8a79380b | 327 | ret = win32_aio_attach(aio, s->hfile); |
a2736526 PB |
328 | if (ret < 0) { |
329 | CloseHandle(s->hfile); | |
c6252b7c | 330 | error_setg_errno(errp, -ret, "Could not enable AIO"); |
8a79380b | 331 | goto fail; |
a2736526 PB |
332 | } |
333 | s->aio = aio; | |
223d4670 | 334 | } |
8a79380b | 335 | |
c25f53b0 | 336 | raw_probe_alignment(bs); |
8a79380b KW |
337 | ret = 0; |
338 | fail: | |
339 | qemu_opts_del(opts); | |
340 | return ret; | |
223d4670 TS |
341 | } |
342 | ||
fc4edb84 PB |
343 | static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs, |
344 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
345 | BlockDriverCompletionFunc *cb, void *opaque) | |
223d4670 TS |
346 | { |
347 | BDRVRawState *s = bs->opaque; | |
a2736526 PB |
348 | if (s->aio) { |
349 | return win32_aio_submit(bs, s->aio, s->hfile, sector_num, qiov, | |
350 | nb_sectors, cb, opaque, QEMU_AIO_READ); | |
351 | } else { | |
352 | return paio_submit(bs, s->hfile, sector_num, qiov, nb_sectors, | |
353 | cb, opaque, QEMU_AIO_READ); | |
354 | } | |
223d4670 TS |
355 | } |
356 | ||
fc4edb84 PB |
357 | static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs, |
358 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
359 | BlockDriverCompletionFunc *cb, void *opaque) | |
223d4670 TS |
360 | { |
361 | BDRVRawState *s = bs->opaque; | |
a2736526 PB |
362 | if (s->aio) { |
363 | return win32_aio_submit(bs, s->aio, s->hfile, sector_num, qiov, | |
364 | nb_sectors, cb, opaque, QEMU_AIO_WRITE); | |
365 | } else { | |
366 | return paio_submit(bs, s->hfile, sector_num, qiov, nb_sectors, | |
367 | cb, opaque, QEMU_AIO_WRITE); | |
368 | } | |
223d4670 TS |
369 | } |
370 | ||
fc4edb84 PB |
371 | static BlockDriverAIOCB *raw_aio_flush(BlockDriverState *bs, |
372 | BlockDriverCompletionFunc *cb, void *opaque) | |
223d4670 TS |
373 | { |
374 | BDRVRawState *s = bs->opaque; | |
fc4edb84 | 375 | return paio_submit(bs, s->hfile, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH); |
223d4670 TS |
376 | } |
377 | ||
378 | static void raw_close(BlockDriverState *bs) | |
379 | { | |
380 | BDRVRawState *s = bs->opaque; | |
381 | CloseHandle(s->hfile); | |
382 | } | |
383 | ||
384 | static int raw_truncate(BlockDriverState *bs, int64_t offset) | |
385 | { | |
386 | BDRVRawState *s = bs->opaque; | |
b9e82a59 | 387 | LONG low, high; |
fbcad04d | 388 | DWORD dwPtrLow; |
223d4670 TS |
389 | |
390 | low = offset; | |
391 | high = offset >> 32; | |
fbcad04d FC |
392 | |
393 | /* | |
394 | * An error has occurred if the return value is INVALID_SET_FILE_POINTER | |
395 | * and GetLastError doesn't return NO_ERROR. | |
396 | */ | |
397 | dwPtrLow = SetFilePointer(s->hfile, low, &high, FILE_BEGIN); | |
398 | if (dwPtrLow == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) { | |
fccedc62 | 399 | fprintf(stderr, "SetFilePointer error: %lu\n", GetLastError()); |
fbcad04d FC |
400 | return -EIO; |
401 | } | |
402 | if (SetEndOfFile(s->hfile) == 0) { | |
fccedc62 | 403 | fprintf(stderr, "SetEndOfFile error: %lu\n", GetLastError()); |
223d4670 | 404 | return -EIO; |
fbcad04d | 405 | } |
223d4670 TS |
406 | return 0; |
407 | } | |
408 | ||
409 | static int64_t raw_getlength(BlockDriverState *bs) | |
410 | { | |
411 | BDRVRawState *s = bs->opaque; | |
412 | LARGE_INTEGER l; | |
413 | ULARGE_INTEGER available, total, total_free; | |
414 | DISK_GEOMETRY_EX dg; | |
415 | DWORD count; | |
416 | BOOL status; | |
417 | ||
418 | switch(s->type) { | |
419 | case FTYPE_FILE: | |
b9e82a59 | 420 | l.LowPart = GetFileSize(s->hfile, (PDWORD)&l.HighPart); |
223d4670 TS |
421 | if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR) |
422 | return -EIO; | |
423 | break; | |
424 | case FTYPE_CD: | |
425 | if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free)) | |
426 | return -EIO; | |
427 | l.QuadPart = total.QuadPart; | |
428 | break; | |
429 | case FTYPE_HARDDISK: | |
430 | status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, | |
431 | NULL, 0, &dg, sizeof(dg), &count, NULL); | |
432 | if (status != 0) { | |
433 | l = dg.DiskSize; | |
434 | } | |
435 | break; | |
436 | default: | |
437 | return -EIO; | |
438 | } | |
439 | return l.QuadPart; | |
440 | } | |
441 | ||
4a1d5e1f FZ |
442 | static int64_t raw_get_allocated_file_size(BlockDriverState *bs) |
443 | { | |
444 | typedef DWORD (WINAPI * get_compressed_t)(const char *filename, | |
445 | DWORD * high); | |
446 | get_compressed_t get_compressed; | |
447 | struct _stati64 st; | |
448 | const char *filename = bs->filename; | |
449 | /* WinNT support GetCompressedFileSize to determine allocate size */ | |
450 | get_compressed = | |
451 | (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"), | |
452 | "GetCompressedFileSizeA"); | |
453 | if (get_compressed) { | |
454 | DWORD high, low; | |
455 | low = get_compressed(filename, &high); | |
456 | if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR) { | |
457 | return (((int64_t) high) << 32) + low; | |
458 | } | |
459 | } | |
460 | ||
461 | if (_stati64(filename, &st) < 0) { | |
462 | return -1; | |
463 | } | |
464 | return st.st_size; | |
465 | } | |
466 | ||
d5124c00 HR |
467 | static int raw_create(const char *filename, QEMUOptionParameter *options, |
468 | Error **errp) | |
223d4670 TS |
469 | { |
470 | int fd; | |
0e7e1989 | 471 | int64_t total_size = 0; |
223d4670 | 472 | |
0e7e1989 KW |
473 | /* Read out options */ |
474 | while (options && options->name) { | |
475 | if (!strcmp(options->name, BLOCK_OPT_SIZE)) { | |
476 | total_size = options->value.n / 512; | |
477 | } | |
478 | options++; | |
479 | } | |
223d4670 | 480 | |
6165f4d8 CB |
481 | fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, |
482 | 0644); | |
c6252b7c HR |
483 | if (fd < 0) { |
484 | error_setg_errno(errp, errno, "Could not create file"); | |
223d4670 | 485 | return -EIO; |
c6252b7c | 486 | } |
223d4670 TS |
487 | set_sparse(fd); |
488 | ftruncate(fd, total_size * 512); | |
2e1e79da | 489 | qemu_close(fd); |
223d4670 TS |
490 | return 0; |
491 | } | |
492 | ||
0e7e1989 | 493 | static QEMUOptionParameter raw_create_options[] = { |
db08adf5 KW |
494 | { |
495 | .name = BLOCK_OPT_SIZE, | |
496 | .type = OPT_SIZE, | |
497 | .help = "Virtual disk size" | |
498 | }, | |
0e7e1989 KW |
499 | { NULL } |
500 | }; | |
501 | ||
84a12e66 CH |
502 | static BlockDriver bdrv_file = { |
503 | .format_name = "file", | |
504 | .protocol_name = "file", | |
f1b2f712 | 505 | .instance_size = sizeof(BDRVRawState), |
030be321 | 506 | .bdrv_needs_filename = true, |
66f82cee | 507 | .bdrv_file_open = raw_open, |
f1b2f712 AL |
508 | .bdrv_close = raw_close, |
509 | .bdrv_create = raw_create, | |
3ac21627 | 510 | .bdrv_has_zero_init = bdrv_has_zero_init_1, |
c68b89ac | 511 | |
fc4edb84 PB |
512 | .bdrv_aio_readv = raw_aio_readv, |
513 | .bdrv_aio_writev = raw_aio_writev, | |
514 | .bdrv_aio_flush = raw_aio_flush, | |
c68b89ac | 515 | |
f1b2f712 AL |
516 | .bdrv_truncate = raw_truncate, |
517 | .bdrv_getlength = raw_getlength, | |
4a1d5e1f FZ |
518 | .bdrv_get_allocated_file_size |
519 | = raw_get_allocated_file_size, | |
0e7e1989 KW |
520 | |
521 | .create_options = raw_create_options, | |
223d4670 TS |
522 | }; |
523 | ||
524 | /***********************************************/ | |
525 | /* host device */ | |
526 | ||
527 | static int find_cdrom(char *cdrom_name, int cdrom_name_size) | |
528 | { | |
529 | char drives[256], *pdrv = drives; | |
530 | UINT type; | |
531 | ||
532 | memset(drives, 0, sizeof(drives)); | |
533 | GetLogicalDriveStrings(sizeof(drives), drives); | |
534 | while(pdrv[0] != '\0') { | |
535 | type = GetDriveType(pdrv); | |
536 | switch(type) { | |
537 | case DRIVE_CDROM: | |
538 | snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]); | |
539 | return 0; | |
540 | break; | |
541 | } | |
542 | pdrv += lstrlen(pdrv) + 1; | |
543 | } | |
544 | return -1; | |
545 | } | |
546 | ||
547 | static int find_device_type(BlockDriverState *bs, const char *filename) | |
548 | { | |
549 | BDRVRawState *s = bs->opaque; | |
550 | UINT type; | |
551 | const char *p; | |
552 | ||
553 | if (strstart(filename, "\\\\.\\", &p) || | |
554 | strstart(filename, "//./", &p)) { | |
555 | if (stristart(p, "PhysicalDrive", NULL)) | |
556 | return FTYPE_HARDDISK; | |
557 | snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]); | |
558 | type = GetDriveType(s->drive_path); | |
3060cd14 AL |
559 | switch (type) { |
560 | case DRIVE_REMOVABLE: | |
561 | case DRIVE_FIXED: | |
562 | return FTYPE_HARDDISK; | |
563 | case DRIVE_CDROM: | |
223d4670 | 564 | return FTYPE_CD; |
3060cd14 | 565 | default: |
223d4670 | 566 | return FTYPE_FILE; |
3060cd14 | 567 | } |
223d4670 TS |
568 | } else { |
569 | return FTYPE_FILE; | |
570 | } | |
571 | } | |
572 | ||
508c7cb3 CH |
573 | static int hdev_probe_device(const char *filename) |
574 | { | |
575 | if (strstart(filename, "/dev/cdrom", NULL)) | |
576 | return 100; | |
577 | if (is_windows_drive(filename)) | |
578 | return 100; | |
579 | return 0; | |
580 | } | |
581 | ||
015a1036 HR |
582 | static int hdev_open(BlockDriverState *bs, QDict *options, int flags, |
583 | Error **errp) | |
223d4670 TS |
584 | { |
585 | BDRVRawState *s = bs->opaque; | |
586 | int access_flags, create_flags; | |
68dc0364 | 587 | int ret = 0; |
223d4670 TS |
588 | DWORD overlapped; |
589 | char device_name[64]; | |
68dc0364 SW |
590 | |
591 | Error *local_err = NULL; | |
592 | const char *filename; | |
593 | ||
87ea75d5 PC |
594 | QemuOpts *opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, |
595 | &error_abort); | |
68dc0364 SW |
596 | qemu_opts_absorb_qdict(opts, options, &local_err); |
597 | if (error_is_set(&local_err)) { | |
c6252b7c | 598 | error_propagate(errp, local_err); |
68dc0364 SW |
599 | ret = -EINVAL; |
600 | goto done; | |
601 | } | |
602 | ||
603 | filename = qemu_opt_get(opts, "filename"); | |
223d4670 TS |
604 | |
605 | if (strstart(filename, "/dev/cdrom", NULL)) { | |
68dc0364 | 606 | if (find_cdrom(device_name, sizeof(device_name)) < 0) { |
c6252b7c | 607 | error_setg(errp, "Could not open CD-ROM drive"); |
68dc0364 SW |
608 | ret = -ENOENT; |
609 | goto done; | |
610 | } | |
223d4670 TS |
611 | filename = device_name; |
612 | } else { | |
613 | /* transform drive letters into device name */ | |
614 | if (((filename[0] >= 'a' && filename[0] <= 'z') || | |
615 | (filename[0] >= 'A' && filename[0] <= 'Z')) && | |
616 | filename[1] == ':' && filename[2] == '\0') { | |
617 | snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]); | |
618 | filename = device_name; | |
619 | } | |
620 | } | |
621 | s->type = find_device_type(bs, filename); | |
622 | ||
6a8dc042 JC |
623 | raw_parse_flags(flags, &access_flags, &overlapped); |
624 | ||
223d4670 TS |
625 | create_flags = OPEN_EXISTING; |
626 | ||
223d4670 TS |
627 | s->hfile = CreateFile(filename, access_flags, |
628 | FILE_SHARE_READ, NULL, | |
629 | create_flags, overlapped, NULL); | |
630 | if (s->hfile == INVALID_HANDLE_VALUE) { | |
631 | int err = GetLastError(); | |
632 | ||
68dc0364 SW |
633 | if (err == ERROR_ACCESS_DENIED) { |
634 | ret = -EACCES; | |
635 | } else { | |
45d57f6e | 636 | ret = -EINVAL; |
68dc0364 | 637 | } |
45d57f6e | 638 | error_setg_errno(errp, -ret, "Could not open device"); |
68dc0364 | 639 | goto done; |
223d4670 | 640 | } |
68dc0364 SW |
641 | |
642 | done: | |
643 | qemu_opts_del(opts); | |
644 | return ret; | |
223d4670 TS |
645 | } |
646 | ||
5efa9d5a | 647 | static BlockDriver bdrv_host_device = { |
e60f469c | 648 | .format_name = "host_device", |
84a12e66 | 649 | .protocol_name = "host_device", |
e60f469c | 650 | .instance_size = sizeof(BDRVRawState), |
030be321 | 651 | .bdrv_needs_filename = true, |
508c7cb3 | 652 | .bdrv_probe_device = hdev_probe_device, |
66f82cee | 653 | .bdrv_file_open = hdev_open, |
e60f469c | 654 | .bdrv_close = raw_close, |
223d4670 | 655 | |
fc4edb84 PB |
656 | .bdrv_aio_readv = raw_aio_readv, |
657 | .bdrv_aio_writev = raw_aio_writev, | |
658 | .bdrv_aio_flush = raw_aio_flush, | |
c68b89ac | 659 | |
b94a2610 KW |
660 | .bdrv_getlength = raw_getlength, |
661 | .has_variable_length = true, | |
662 | ||
4a1d5e1f FZ |
663 | .bdrv_get_allocated_file_size |
664 | = raw_get_allocated_file_size, | |
223d4670 | 665 | }; |
5efa9d5a | 666 | |
84a12e66 | 667 | static void bdrv_file_init(void) |
5efa9d5a | 668 | { |
84a12e66 | 669 | bdrv_register(&bdrv_file); |
5efa9d5a AL |
670 | bdrv_register(&bdrv_host_device); |
671 | } | |
672 | ||
84a12e66 | 673 | block_init(bdrv_file_init); |