]>
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" |
d49b6836 | 32 | #include "qapi/qmp/qstring.h" |
49dc768d | 33 | #include <windows.h> |
223d4670 TS |
34 | #include <winioctl.h> |
35 | ||
36 | #define FTYPE_FILE 0 | |
37 | #define FTYPE_CD 1 | |
38 | #define FTYPE_HARDDISK 2 | |
39 | ||
fc4edb84 PB |
40 | typedef struct RawWin32AIOData { |
41 | BlockDriverState *bs; | |
42 | HANDLE hfile; | |
43 | struct iovec *aio_iov; | |
44 | int aio_niov; | |
45 | size_t aio_nbytes; | |
46 | off64_t aio_offset; | |
47 | int aio_type; | |
48 | } RawWin32AIOData; | |
49 | ||
223d4670 TS |
50 | typedef struct BDRVRawState { |
51 | HANDLE hfile; | |
52 | int type; | |
53 | char drive_path[16]; /* format: "d:\" */ | |
a2736526 | 54 | QEMUWin32AIOState *aio; |
223d4670 TS |
55 | } BDRVRawState; |
56 | ||
fc4edb84 PB |
57 | /* |
58 | * Read/writes the data to/from a given linear buffer. | |
59 | * | |
60 | * Returns the number of bytes handles or -errno in case of an error. Short | |
61 | * reads are only returned if the end of the file is reached. | |
62 | */ | |
63 | static size_t handle_aiocb_rw(RawWin32AIOData *aiocb) | |
64 | { | |
65 | size_t offset = 0; | |
66 | int i; | |
67 | ||
68 | for (i = 0; i < aiocb->aio_niov; i++) { | |
69 | OVERLAPPED ov; | |
70 | DWORD ret, ret_count, len; | |
71 | ||
72 | memset(&ov, 0, sizeof(ov)); | |
73 | ov.Offset = (aiocb->aio_offset + offset); | |
74 | ov.OffsetHigh = (aiocb->aio_offset + offset) >> 32; | |
75 | len = aiocb->aio_iov[i].iov_len; | |
76 | if (aiocb->aio_type & QEMU_AIO_WRITE) { | |
77 | ret = WriteFile(aiocb->hfile, aiocb->aio_iov[i].iov_base, | |
78 | len, &ret_count, &ov); | |
79 | } else { | |
80 | ret = ReadFile(aiocb->hfile, aiocb->aio_iov[i].iov_base, | |
81 | len, &ret_count, &ov); | |
82 | } | |
83 | if (!ret) { | |
84 | ret_count = 0; | |
85 | } | |
86 | if (ret_count != len) { | |
56e023af | 87 | offset += ret_count; |
fc4edb84 PB |
88 | break; |
89 | } | |
90 | offset += len; | |
91 | } | |
92 | ||
93 | return offset; | |
94 | } | |
95 | ||
96 | static 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); | |
c0191e76 | 105 | if (count < aiocb->aio_nbytes) { |
fc4edb84 PB |
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) { | |
5d555030 | 122 | ret = 0; |
fc4edb84 | 123 | } else { |
5d555030 | 124 | ret = -EINVAL; |
fc4edb84 PB |
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 | ||
c84b3192 | 138 | g_free(aiocb); |
fc4edb84 PB |
139 | return ret; |
140 | } | |
141 | ||
7c84b1b8 | 142 | static BlockAIOCB *paio_submit(BlockDriverState *bs, HANDLE hfile, |
fc4edb84 | 143 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
097310b5 | 144 | BlockCompletionFunc *cb, void *opaque, int type) |
fc4edb84 | 145 | { |
c84b3192 | 146 | RawWin32AIOData *acb = g_new(RawWin32AIOData, 1); |
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 |
165 | int 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 | ||
197 | static 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 | ||
85ebd381 SH |
204 | static void raw_detach_aio_context(BlockDriverState *bs) |
205 | { | |
206 | BDRVRawState *s = bs->opaque; | |
207 | ||
208 | if (s->aio) { | |
209 | win32_aio_detach_aio_context(s->aio, bdrv_get_aio_context(bs)); | |
210 | } | |
211 | } | |
212 | ||
213 | static void raw_attach_aio_context(BlockDriverState *bs, | |
214 | AioContext *new_context) | |
215 | { | |
216 | BDRVRawState *s = bs->opaque; | |
217 | ||
218 | if (s->aio) { | |
219 | win32_aio_attach_aio_context(s->aio, new_context); | |
220 | } | |
221 | } | |
222 | ||
c25f53b0 PB |
223 | static void raw_probe_alignment(BlockDriverState *bs) |
224 | { | |
225 | BDRVRawState *s = bs->opaque; | |
226 | DWORD sectorsPerCluster, freeClusters, totalClusters, count; | |
227 | DISK_GEOMETRY_EX dg; | |
228 | BOOL status; | |
229 | ||
230 | if (s->type == FTYPE_CD) { | |
231 | bs->request_alignment = 2048; | |
232 | return; | |
233 | } | |
234 | if (s->type == FTYPE_HARDDISK) { | |
235 | status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, | |
236 | NULL, 0, &dg, sizeof(dg), &count, NULL); | |
237 | if (status != 0) { | |
238 | bs->request_alignment = dg.Geometry.BytesPerSector; | |
239 | return; | |
240 | } | |
241 | /* try GetDiskFreeSpace too */ | |
242 | } | |
243 | ||
244 | if (s->drive_path[0]) { | |
245 | GetDiskFreeSpace(s->drive_path, §orsPerCluster, | |
246 | &dg.Geometry.BytesPerSector, | |
247 | &freeClusters, &totalClusters); | |
248 | bs->request_alignment = dg.Geometry.BytesPerSector; | |
249 | } | |
250 | } | |
251 | ||
6a8dc042 JC |
252 | static void raw_parse_flags(int flags, int *access_flags, DWORD *overlapped) |
253 | { | |
254 | assert(access_flags != NULL); | |
255 | assert(overlapped != NULL); | |
256 | ||
257 | if (flags & BDRV_O_RDWR) { | |
258 | *access_flags = GENERIC_READ | GENERIC_WRITE; | |
259 | } else { | |
260 | *access_flags = GENERIC_READ; | |
261 | } | |
262 | ||
263 | *overlapped = FILE_ATTRIBUTE_NORMAL; | |
a2736526 PB |
264 | if (flags & BDRV_O_NATIVE_AIO) { |
265 | *overlapped |= FILE_FLAG_OVERLAPPED; | |
266 | } | |
6a8dc042 JC |
267 | if (flags & BDRV_O_NOCACHE) { |
268 | *overlapped |= FILE_FLAG_NO_BUFFERING; | |
269 | } | |
6a8dc042 JC |
270 | } |
271 | ||
7dc74db8 HR |
272 | static void raw_parse_filename(const char *filename, QDict *options, |
273 | Error **errp) | |
274 | { | |
275 | /* The filename does not have to be prefixed by the protocol name, since | |
276 | * "file" is the default protocol; therefore, the return value of this | |
277 | * function call can be ignored. */ | |
278 | strstart(filename, "file:", &filename); | |
279 | ||
280 | qdict_put_obj(options, "filename", QOBJECT(qstring_from_str(filename))); | |
281 | } | |
282 | ||
8a79380b KW |
283 | static QemuOptsList raw_runtime_opts = { |
284 | .name = "raw", | |
285 | .head = QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head), | |
286 | .desc = { | |
287 | { | |
288 | .name = "filename", | |
289 | .type = QEMU_OPT_STRING, | |
290 | .help = "File name of the image", | |
291 | }, | |
292 | { /* end of list */ } | |
293 | }, | |
294 | }; | |
295 | ||
015a1036 HR |
296 | static int raw_open(BlockDriverState *bs, QDict *options, int flags, |
297 | Error **errp) | |
223d4670 TS |
298 | { |
299 | BDRVRawState *s = bs->opaque; | |
9a2d77ad | 300 | int access_flags; |
223d4670 | 301 | DWORD overlapped; |
8a79380b KW |
302 | QemuOpts *opts; |
303 | Error *local_err = NULL; | |
304 | const char *filename; | |
305 | int ret; | |
223d4670 TS |
306 | |
307 | s->type = FTYPE_FILE; | |
308 | ||
87ea75d5 | 309 | opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort); |
8a79380b | 310 | qemu_opts_absorb_qdict(opts, options, &local_err); |
84d18f06 | 311 | if (local_err) { |
c6252b7c | 312 | error_propagate(errp, local_err); |
8a79380b KW |
313 | ret = -EINVAL; |
314 | goto fail; | |
315 | } | |
316 | ||
317 | filename = qemu_opt_get(opts, "filename"); | |
318 | ||
6a8dc042 | 319 | raw_parse_flags(flags, &access_flags, &overlapped); |
8a79380b | 320 | |
c25f53b0 PB |
321 | if (filename[0] && filename[1] == ':') { |
322 | snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", filename[0]); | |
323 | } else if (filename[0] == '\\' && filename[1] == '\\') { | |
324 | s->drive_path[0] = 0; | |
325 | } else { | |
326 | /* Relative path. */ | |
327 | char buf[MAX_PATH]; | |
328 | GetCurrentDirectory(MAX_PATH, buf); | |
329 | snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", buf[0]); | |
330 | } | |
331 | ||
223d4670 TS |
332 | s->hfile = CreateFile(filename, access_flags, |
333 | FILE_SHARE_READ, NULL, | |
9a2d77ad | 334 | OPEN_EXISTING, overlapped, NULL); |
223d4670 TS |
335 | if (s->hfile == INVALID_HANDLE_VALUE) { |
336 | int err = GetLastError(); | |
337 | ||
8a79380b KW |
338 | if (err == ERROR_ACCESS_DENIED) { |
339 | ret = -EACCES; | |
340 | } else { | |
341 | ret = -EINVAL; | |
342 | } | |
343 | goto fail; | |
a2736526 PB |
344 | } |
345 | ||
346 | if (flags & BDRV_O_NATIVE_AIO) { | |
99cc5989 SH |
347 | s->aio = win32_aio_init(); |
348 | if (s->aio == NULL) { | |
349 | CloseHandle(s->hfile); | |
350 | error_setg(errp, "Could not initialize AIO"); | |
351 | ret = -EINVAL; | |
352 | goto fail; | |
353 | } | |
354 | ||
355 | ret = win32_aio_attach(s->aio, s->hfile); | |
a2736526 | 356 | if (ret < 0) { |
99cc5989 | 357 | win32_aio_cleanup(s->aio); |
a2736526 | 358 | CloseHandle(s->hfile); |
c6252b7c | 359 | error_setg_errno(errp, -ret, "Could not enable AIO"); |
8a79380b | 360 | goto fail; |
a2736526 | 361 | } |
85ebd381 SH |
362 | |
363 | win32_aio_attach_aio_context(s->aio, bdrv_get_aio_context(bs)); | |
223d4670 | 364 | } |
8a79380b | 365 | |
c25f53b0 | 366 | raw_probe_alignment(bs); |
8a79380b KW |
367 | ret = 0; |
368 | fail: | |
369 | qemu_opts_del(opts); | |
370 | return ret; | |
223d4670 TS |
371 | } |
372 | ||
7c84b1b8 | 373 | static BlockAIOCB *raw_aio_readv(BlockDriverState *bs, |
fc4edb84 | 374 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
097310b5 | 375 | BlockCompletionFunc *cb, void *opaque) |
223d4670 TS |
376 | { |
377 | BDRVRawState *s = bs->opaque; | |
a2736526 PB |
378 | if (s->aio) { |
379 | return win32_aio_submit(bs, s->aio, s->hfile, sector_num, qiov, | |
380 | nb_sectors, cb, opaque, QEMU_AIO_READ); | |
381 | } else { | |
382 | return paio_submit(bs, s->hfile, sector_num, qiov, nb_sectors, | |
383 | cb, opaque, QEMU_AIO_READ); | |
384 | } | |
223d4670 TS |
385 | } |
386 | ||
7c84b1b8 | 387 | static BlockAIOCB *raw_aio_writev(BlockDriverState *bs, |
fc4edb84 | 388 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
097310b5 | 389 | BlockCompletionFunc *cb, void *opaque) |
223d4670 TS |
390 | { |
391 | BDRVRawState *s = bs->opaque; | |
a2736526 PB |
392 | if (s->aio) { |
393 | return win32_aio_submit(bs, s->aio, s->hfile, sector_num, qiov, | |
394 | nb_sectors, cb, opaque, QEMU_AIO_WRITE); | |
395 | } else { | |
396 | return paio_submit(bs, s->hfile, sector_num, qiov, nb_sectors, | |
397 | cb, opaque, QEMU_AIO_WRITE); | |
398 | } | |
223d4670 TS |
399 | } |
400 | ||
7c84b1b8 | 401 | static BlockAIOCB *raw_aio_flush(BlockDriverState *bs, |
097310b5 | 402 | BlockCompletionFunc *cb, void *opaque) |
223d4670 TS |
403 | { |
404 | BDRVRawState *s = bs->opaque; | |
fc4edb84 | 405 | return paio_submit(bs, s->hfile, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH); |
223d4670 TS |
406 | } |
407 | ||
408 | static void raw_close(BlockDriverState *bs) | |
409 | { | |
410 | BDRVRawState *s = bs->opaque; | |
99cc5989 SH |
411 | |
412 | if (s->aio) { | |
85ebd381 | 413 | win32_aio_detach_aio_context(s->aio, bdrv_get_aio_context(bs)); |
99cc5989 SH |
414 | win32_aio_cleanup(s->aio); |
415 | s->aio = NULL; | |
416 | } | |
417 | ||
223d4670 | 418 | CloseHandle(s->hfile); |
8bfea15d KW |
419 | if (bs->open_flags & BDRV_O_TEMPORARY) { |
420 | unlink(bs->filename); | |
421 | } | |
223d4670 TS |
422 | } |
423 | ||
424 | static int raw_truncate(BlockDriverState *bs, int64_t offset) | |
425 | { | |
426 | BDRVRawState *s = bs->opaque; | |
b9e82a59 | 427 | LONG low, high; |
fbcad04d | 428 | DWORD dwPtrLow; |
223d4670 TS |
429 | |
430 | low = offset; | |
431 | high = offset >> 32; | |
fbcad04d FC |
432 | |
433 | /* | |
434 | * An error has occurred if the return value is INVALID_SET_FILE_POINTER | |
435 | * and GetLastError doesn't return NO_ERROR. | |
436 | */ | |
437 | dwPtrLow = SetFilePointer(s->hfile, low, &high, FILE_BEGIN); | |
438 | if (dwPtrLow == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) { | |
fccedc62 | 439 | fprintf(stderr, "SetFilePointer error: %lu\n", GetLastError()); |
fbcad04d FC |
440 | return -EIO; |
441 | } | |
442 | if (SetEndOfFile(s->hfile) == 0) { | |
fccedc62 | 443 | fprintf(stderr, "SetEndOfFile error: %lu\n", GetLastError()); |
223d4670 | 444 | return -EIO; |
fbcad04d | 445 | } |
223d4670 TS |
446 | return 0; |
447 | } | |
448 | ||
449 | static int64_t raw_getlength(BlockDriverState *bs) | |
450 | { | |
451 | BDRVRawState *s = bs->opaque; | |
452 | LARGE_INTEGER l; | |
453 | ULARGE_INTEGER available, total, total_free; | |
454 | DISK_GEOMETRY_EX dg; | |
455 | DWORD count; | |
456 | BOOL status; | |
457 | ||
458 | switch(s->type) { | |
459 | case FTYPE_FILE: | |
b9e82a59 | 460 | l.LowPart = GetFileSize(s->hfile, (PDWORD)&l.HighPart); |
223d4670 TS |
461 | if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR) |
462 | return -EIO; | |
463 | break; | |
464 | case FTYPE_CD: | |
465 | if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free)) | |
466 | return -EIO; | |
467 | l.QuadPart = total.QuadPart; | |
468 | break; | |
469 | case FTYPE_HARDDISK: | |
470 | status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, | |
471 | NULL, 0, &dg, sizeof(dg), &count, NULL); | |
472 | if (status != 0) { | |
473 | l = dg.DiskSize; | |
474 | } | |
475 | break; | |
476 | default: | |
477 | return -EIO; | |
478 | } | |
479 | return l.QuadPart; | |
480 | } | |
481 | ||
4a1d5e1f FZ |
482 | static int64_t raw_get_allocated_file_size(BlockDriverState *bs) |
483 | { | |
484 | typedef DWORD (WINAPI * get_compressed_t)(const char *filename, | |
485 | DWORD * high); | |
486 | get_compressed_t get_compressed; | |
487 | struct _stati64 st; | |
488 | const char *filename = bs->filename; | |
489 | /* WinNT support GetCompressedFileSize to determine allocate size */ | |
490 | get_compressed = | |
491 | (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"), | |
492 | "GetCompressedFileSizeA"); | |
493 | if (get_compressed) { | |
494 | DWORD high, low; | |
495 | low = get_compressed(filename, &high); | |
496 | if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR) { | |
497 | return (((int64_t) high) << 32) + low; | |
498 | } | |
499 | } | |
500 | ||
501 | if (_stati64(filename, &st) < 0) { | |
502 | return -1; | |
503 | } | |
504 | return st.st_size; | |
505 | } | |
506 | ||
ddef7699 | 507 | static int raw_create(const char *filename, QemuOpts *opts, Error **errp) |
223d4670 TS |
508 | { |
509 | int fd; | |
0e7e1989 | 510 | int64_t total_size = 0; |
223d4670 | 511 | |
d5546c5e HR |
512 | strstart(filename, "file:", &filename); |
513 | ||
0e7e1989 | 514 | /* Read out options */ |
180e9526 HT |
515 | total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), |
516 | BDRV_SECTOR_SIZE); | |
223d4670 | 517 | |
6165f4d8 CB |
518 | fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, |
519 | 0644); | |
c6252b7c HR |
520 | if (fd < 0) { |
521 | error_setg_errno(errp, errno, "Could not create file"); | |
223d4670 | 522 | return -EIO; |
c6252b7c | 523 | } |
223d4670 | 524 | set_sparse(fd); |
180e9526 | 525 | ftruncate(fd, total_size); |
2e1e79da | 526 | qemu_close(fd); |
223d4670 TS |
527 | return 0; |
528 | } | |
529 | ||
ddef7699 CL |
530 | |
531 | static QemuOptsList raw_create_opts = { | |
532 | .name = "raw-create-opts", | |
533 | .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head), | |
534 | .desc = { | |
535 | { | |
536 | .name = BLOCK_OPT_SIZE, | |
537 | .type = QEMU_OPT_SIZE, | |
538 | .help = "Virtual disk size" | |
539 | }, | |
540 | { /* end of list */ } | |
541 | } | |
0e7e1989 KW |
542 | }; |
543 | ||
5f535a94 | 544 | BlockDriver bdrv_file = { |
84a12e66 CH |
545 | .format_name = "file", |
546 | .protocol_name = "file", | |
f1b2f712 | 547 | .instance_size = sizeof(BDRVRawState), |
030be321 | 548 | .bdrv_needs_filename = true, |
7dc74db8 | 549 | .bdrv_parse_filename = raw_parse_filename, |
ddef7699 CL |
550 | .bdrv_file_open = raw_open, |
551 | .bdrv_close = raw_close, | |
c282e1fd | 552 | .bdrv_create = raw_create, |
3ac21627 | 553 | .bdrv_has_zero_init = bdrv_has_zero_init_1, |
c68b89ac | 554 | |
fc4edb84 PB |
555 | .bdrv_aio_readv = raw_aio_readv, |
556 | .bdrv_aio_writev = raw_aio_writev, | |
557 | .bdrv_aio_flush = raw_aio_flush, | |
c68b89ac | 558 | |
f1b2f712 AL |
559 | .bdrv_truncate = raw_truncate, |
560 | .bdrv_getlength = raw_getlength, | |
4a1d5e1f FZ |
561 | .bdrv_get_allocated_file_size |
562 | = raw_get_allocated_file_size, | |
0e7e1989 | 563 | |
ddef7699 | 564 | .create_opts = &raw_create_opts, |
223d4670 TS |
565 | }; |
566 | ||
567 | /***********************************************/ | |
568 | /* host device */ | |
569 | ||
570 | static int find_cdrom(char *cdrom_name, int cdrom_name_size) | |
571 | { | |
572 | char drives[256], *pdrv = drives; | |
573 | UINT type; | |
574 | ||
575 | memset(drives, 0, sizeof(drives)); | |
576 | GetLogicalDriveStrings(sizeof(drives), drives); | |
577 | while(pdrv[0] != '\0') { | |
578 | type = GetDriveType(pdrv); | |
579 | switch(type) { | |
580 | case DRIVE_CDROM: | |
581 | snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]); | |
582 | return 0; | |
583 | break; | |
584 | } | |
585 | pdrv += lstrlen(pdrv) + 1; | |
586 | } | |
587 | return -1; | |
588 | } | |
589 | ||
590 | static int find_device_type(BlockDriverState *bs, const char *filename) | |
591 | { | |
592 | BDRVRawState *s = bs->opaque; | |
593 | UINT type; | |
594 | const char *p; | |
595 | ||
596 | if (strstart(filename, "\\\\.\\", &p) || | |
597 | strstart(filename, "//./", &p)) { | |
598 | if (stristart(p, "PhysicalDrive", NULL)) | |
599 | return FTYPE_HARDDISK; | |
600 | snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]); | |
601 | type = GetDriveType(s->drive_path); | |
3060cd14 AL |
602 | switch (type) { |
603 | case DRIVE_REMOVABLE: | |
604 | case DRIVE_FIXED: | |
605 | return FTYPE_HARDDISK; | |
606 | case DRIVE_CDROM: | |
223d4670 | 607 | return FTYPE_CD; |
3060cd14 | 608 | default: |
223d4670 | 609 | return FTYPE_FILE; |
3060cd14 | 610 | } |
223d4670 TS |
611 | } else { |
612 | return FTYPE_FILE; | |
613 | } | |
614 | } | |
615 | ||
508c7cb3 CH |
616 | static int hdev_probe_device(const char *filename) |
617 | { | |
618 | if (strstart(filename, "/dev/cdrom", NULL)) | |
619 | return 100; | |
620 | if (is_windows_drive(filename)) | |
621 | return 100; | |
622 | return 0; | |
623 | } | |
624 | ||
57ed25b1 HR |
625 | static void hdev_parse_filename(const char *filename, QDict *options, |
626 | Error **errp) | |
627 | { | |
628 | /* The prefix is optional, just as for "file". */ | |
629 | strstart(filename, "host_device:", &filename); | |
630 | ||
631 | qdict_put_obj(options, "filename", QOBJECT(qstring_from_str(filename))); | |
632 | } | |
633 | ||
015a1036 HR |
634 | static int hdev_open(BlockDriverState *bs, QDict *options, int flags, |
635 | Error **errp) | |
223d4670 TS |
636 | { |
637 | BDRVRawState *s = bs->opaque; | |
638 | int access_flags, create_flags; | |
68dc0364 | 639 | int ret = 0; |
223d4670 TS |
640 | DWORD overlapped; |
641 | char device_name[64]; | |
68dc0364 SW |
642 | |
643 | Error *local_err = NULL; | |
644 | const char *filename; | |
645 | ||
87ea75d5 PC |
646 | QemuOpts *opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, |
647 | &error_abort); | |
68dc0364 | 648 | qemu_opts_absorb_qdict(opts, options, &local_err); |
84d18f06 | 649 | if (local_err) { |
c6252b7c | 650 | error_propagate(errp, local_err); |
68dc0364 SW |
651 | ret = -EINVAL; |
652 | goto done; | |
653 | } | |
654 | ||
655 | filename = qemu_opt_get(opts, "filename"); | |
223d4670 TS |
656 | |
657 | if (strstart(filename, "/dev/cdrom", NULL)) { | |
68dc0364 | 658 | if (find_cdrom(device_name, sizeof(device_name)) < 0) { |
c6252b7c | 659 | error_setg(errp, "Could not open CD-ROM drive"); |
68dc0364 SW |
660 | ret = -ENOENT; |
661 | goto done; | |
662 | } | |
223d4670 TS |
663 | filename = device_name; |
664 | } else { | |
665 | /* transform drive letters into device name */ | |
666 | if (((filename[0] >= 'a' && filename[0] <= 'z') || | |
667 | (filename[0] >= 'A' && filename[0] <= 'Z')) && | |
668 | filename[1] == ':' && filename[2] == '\0') { | |
669 | snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]); | |
670 | filename = device_name; | |
671 | } | |
672 | } | |
673 | s->type = find_device_type(bs, filename); | |
674 | ||
6a8dc042 JC |
675 | raw_parse_flags(flags, &access_flags, &overlapped); |
676 | ||
223d4670 TS |
677 | create_flags = OPEN_EXISTING; |
678 | ||
223d4670 TS |
679 | s->hfile = CreateFile(filename, access_flags, |
680 | FILE_SHARE_READ, NULL, | |
681 | create_flags, overlapped, NULL); | |
682 | if (s->hfile == INVALID_HANDLE_VALUE) { | |
683 | int err = GetLastError(); | |
684 | ||
68dc0364 SW |
685 | if (err == ERROR_ACCESS_DENIED) { |
686 | ret = -EACCES; | |
687 | } else { | |
45d57f6e | 688 | ret = -EINVAL; |
68dc0364 | 689 | } |
45d57f6e | 690 | error_setg_errno(errp, -ret, "Could not open device"); |
68dc0364 | 691 | goto done; |
223d4670 | 692 | } |
68dc0364 SW |
693 | |
694 | done: | |
695 | qemu_opts_del(opts); | |
696 | return ret; | |
223d4670 TS |
697 | } |
698 | ||
5efa9d5a | 699 | static BlockDriver bdrv_host_device = { |
e60f469c | 700 | .format_name = "host_device", |
84a12e66 | 701 | .protocol_name = "host_device", |
e60f469c | 702 | .instance_size = sizeof(BDRVRawState), |
030be321 | 703 | .bdrv_needs_filename = true, |
57ed25b1 | 704 | .bdrv_parse_filename = hdev_parse_filename, |
508c7cb3 | 705 | .bdrv_probe_device = hdev_probe_device, |
66f82cee | 706 | .bdrv_file_open = hdev_open, |
e60f469c | 707 | .bdrv_close = raw_close, |
223d4670 | 708 | |
fc4edb84 PB |
709 | .bdrv_aio_readv = raw_aio_readv, |
710 | .bdrv_aio_writev = raw_aio_writev, | |
711 | .bdrv_aio_flush = raw_aio_flush, | |
c68b89ac | 712 | |
85ebd381 SH |
713 | .bdrv_detach_aio_context = raw_detach_aio_context, |
714 | .bdrv_attach_aio_context = raw_attach_aio_context, | |
715 | ||
b94a2610 KW |
716 | .bdrv_getlength = raw_getlength, |
717 | .has_variable_length = true, | |
718 | ||
4a1d5e1f FZ |
719 | .bdrv_get_allocated_file_size |
720 | = raw_get_allocated_file_size, | |
223d4670 | 721 | }; |
5efa9d5a | 722 | |
84a12e66 | 723 | static void bdrv_file_init(void) |
5efa9d5a | 724 | { |
84a12e66 | 725 | bdrv_register(&bdrv_file); |
5efa9d5a AL |
726 | bdrv_register(&bdrv_host_device); |
727 | } | |
728 | ||
84a12e66 | 729 | block_init(bdrv_file_init); |