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