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