]>
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" |
737e150e | 27 | #include "block/block_int.h" |
1de7afc9 | 28 | #include "qemu/module.h" |
0187f5c9 | 29 | #include "block/raw-aio.h" |
fc4edb84 | 30 | #include "trace.h" |
737e150e | 31 | #include "block/thread-pool.h" |
1de7afc9 | 32 | #include "qemu/iov.h" |
d49b6836 | 33 | #include "qapi/qmp/qstring.h" |
0a4279d9 | 34 | #include "qapi/util.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 | ||
0a4279d9 KW |
255 | static void raw_parse_flags(int flags, bool use_aio, int *access_flags, |
256 | DWORD *overlapped) | |
6a8dc042 JC |
257 | { |
258 | assert(access_flags != NULL); | |
259 | assert(overlapped != NULL); | |
260 | ||
261 | if (flags & BDRV_O_RDWR) { | |
262 | *access_flags = GENERIC_READ | GENERIC_WRITE; | |
263 | } else { | |
264 | *access_flags = GENERIC_READ; | |
265 | } | |
266 | ||
267 | *overlapped = FILE_ATTRIBUTE_NORMAL; | |
0a4279d9 | 268 | if (use_aio) { |
a2736526 PB |
269 | *overlapped |= FILE_FLAG_OVERLAPPED; |
270 | } | |
6a8dc042 JC |
271 | if (flags & BDRV_O_NOCACHE) { |
272 | *overlapped |= FILE_FLAG_NO_BUFFERING; | |
273 | } | |
6a8dc042 JC |
274 | } |
275 | ||
7dc74db8 HR |
276 | static void raw_parse_filename(const char *filename, QDict *options, |
277 | Error **errp) | |
278 | { | |
279 | /* The filename does not have to be prefixed by the protocol name, since | |
280 | * "file" is the default protocol; therefore, the return value of this | |
281 | * function call can be ignored. */ | |
282 | strstart(filename, "file:", &filename); | |
283 | ||
46f5ac20 | 284 | qdict_put_str(options, "filename", filename); |
7dc74db8 HR |
285 | } |
286 | ||
8a79380b KW |
287 | static QemuOptsList raw_runtime_opts = { |
288 | .name = "raw", | |
289 | .head = QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head), | |
290 | .desc = { | |
291 | { | |
292 | .name = "filename", | |
293 | .type = QEMU_OPT_STRING, | |
294 | .help = "File name of the image", | |
295 | }, | |
0a4279d9 KW |
296 | { |
297 | .name = "aio", | |
298 | .type = QEMU_OPT_STRING, | |
299 | .help = "host AIO implementation (threads, native)", | |
300 | }, | |
8a79380b KW |
301 | { /* end of list */ } |
302 | }, | |
303 | }; | |
304 | ||
0a4279d9 KW |
305 | static bool get_aio_option(QemuOpts *opts, int flags, Error **errp) |
306 | { | |
307 | BlockdevAioOptions aio, aio_default; | |
308 | ||
309 | aio_default = (flags & BDRV_O_NATIVE_AIO) ? BLOCKDEV_AIO_OPTIONS_NATIVE | |
310 | : BLOCKDEV_AIO_OPTIONS_THREADS; | |
311 | aio = qapi_enum_parse(BlockdevAioOptions_lookup, qemu_opt_get(opts, "aio"), | |
312 | BLOCKDEV_AIO_OPTIONS__MAX, aio_default, errp); | |
313 | ||
314 | switch (aio) { | |
315 | case BLOCKDEV_AIO_OPTIONS_NATIVE: | |
316 | return true; | |
317 | case BLOCKDEV_AIO_OPTIONS_THREADS: | |
318 | return false; | |
319 | default: | |
320 | error_setg(errp, "Invalid AIO option"); | |
321 | } | |
322 | return false; | |
323 | } | |
324 | ||
015a1036 HR |
325 | static int raw_open(BlockDriverState *bs, QDict *options, int flags, |
326 | Error **errp) | |
223d4670 TS |
327 | { |
328 | BDRVRawState *s = bs->opaque; | |
9a2d77ad | 329 | int access_flags; |
223d4670 | 330 | DWORD overlapped; |
8a79380b KW |
331 | QemuOpts *opts; |
332 | Error *local_err = NULL; | |
333 | const char *filename; | |
0a4279d9 | 334 | bool use_aio; |
8a79380b | 335 | int ret; |
223d4670 TS |
336 | |
337 | s->type = FTYPE_FILE; | |
338 | ||
87ea75d5 | 339 | opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort); |
8a79380b | 340 | qemu_opts_absorb_qdict(opts, options, &local_err); |
84d18f06 | 341 | if (local_err) { |
c6252b7c | 342 | error_propagate(errp, local_err); |
8a79380b KW |
343 | ret = -EINVAL; |
344 | goto fail; | |
345 | } | |
346 | ||
1c3a555c FZ |
347 | if (qdict_get_try_bool(options, "locking", false)) { |
348 | error_setg(errp, "locking=on is not supported on Windows"); | |
349 | goto fail; | |
350 | } | |
351 | ||
8a79380b KW |
352 | filename = qemu_opt_get(opts, "filename"); |
353 | ||
0a4279d9 KW |
354 | use_aio = get_aio_option(opts, flags, &local_err); |
355 | if (local_err) { | |
356 | error_propagate(errp, local_err); | |
357 | ret = -EINVAL; | |
358 | goto fail; | |
359 | } | |
360 | ||
361 | raw_parse_flags(flags, use_aio, &access_flags, &overlapped); | |
8a79380b | 362 | |
c25f53b0 PB |
363 | if (filename[0] && filename[1] == ':') { |
364 | snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", filename[0]); | |
365 | } else if (filename[0] == '\\' && filename[1] == '\\') { | |
366 | s->drive_path[0] = 0; | |
367 | } else { | |
368 | /* Relative path. */ | |
369 | char buf[MAX_PATH]; | |
370 | GetCurrentDirectory(MAX_PATH, buf); | |
371 | snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", buf[0]); | |
372 | } | |
373 | ||
223d4670 TS |
374 | s->hfile = CreateFile(filename, access_flags, |
375 | FILE_SHARE_READ, NULL, | |
9a2d77ad | 376 | OPEN_EXISTING, overlapped, NULL); |
223d4670 TS |
377 | if (s->hfile == INVALID_HANDLE_VALUE) { |
378 | int err = GetLastError(); | |
379 | ||
09237757 | 380 | error_setg_win32(errp, err, "Could not open '%s'", filename); |
8a79380b KW |
381 | if (err == ERROR_ACCESS_DENIED) { |
382 | ret = -EACCES; | |
383 | } else { | |
384 | ret = -EINVAL; | |
385 | } | |
386 | goto fail; | |
a2736526 PB |
387 | } |
388 | ||
0a4279d9 | 389 | if (use_aio) { |
99cc5989 SH |
390 | s->aio = win32_aio_init(); |
391 | if (s->aio == NULL) { | |
392 | CloseHandle(s->hfile); | |
393 | error_setg(errp, "Could not initialize AIO"); | |
394 | ret = -EINVAL; | |
395 | goto fail; | |
396 | } | |
397 | ||
398 | ret = win32_aio_attach(s->aio, s->hfile); | |
a2736526 | 399 | if (ret < 0) { |
99cc5989 | 400 | win32_aio_cleanup(s->aio); |
a2736526 | 401 | CloseHandle(s->hfile); |
c6252b7c | 402 | error_setg_errno(errp, -ret, "Could not enable AIO"); |
8a79380b | 403 | goto fail; |
a2736526 | 404 | } |
85ebd381 SH |
405 | |
406 | win32_aio_attach_aio_context(s->aio, bdrv_get_aio_context(bs)); | |
223d4670 | 407 | } |
8a79380b KW |
408 | |
409 | ret = 0; | |
410 | fail: | |
411 | qemu_opts_del(opts); | |
412 | return ret; | |
223d4670 TS |
413 | } |
414 | ||
7c84b1b8 | 415 | static BlockAIOCB *raw_aio_readv(BlockDriverState *bs, |
fc4edb84 | 416 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
097310b5 | 417 | BlockCompletionFunc *cb, void *opaque) |
223d4670 TS |
418 | { |
419 | BDRVRawState *s = bs->opaque; | |
a2736526 PB |
420 | if (s->aio) { |
421 | return win32_aio_submit(bs, s->aio, s->hfile, sector_num, qiov, | |
36e3b2e7 | 422 | nb_sectors, cb, opaque, QEMU_AIO_READ); |
a2736526 | 423 | } else { |
36e3b2e7 EB |
424 | return paio_submit(bs, s->hfile, sector_num << BDRV_SECTOR_BITS, qiov, |
425 | nb_sectors << BDRV_SECTOR_BITS, | |
a2736526 PB |
426 | cb, opaque, QEMU_AIO_READ); |
427 | } | |
223d4670 TS |
428 | } |
429 | ||
7c84b1b8 | 430 | static BlockAIOCB *raw_aio_writev(BlockDriverState *bs, |
fc4edb84 | 431 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
097310b5 | 432 | BlockCompletionFunc *cb, void *opaque) |
223d4670 TS |
433 | { |
434 | BDRVRawState *s = bs->opaque; | |
a2736526 PB |
435 | if (s->aio) { |
436 | return win32_aio_submit(bs, s->aio, s->hfile, sector_num, qiov, | |
36e3b2e7 | 437 | nb_sectors, cb, opaque, QEMU_AIO_WRITE); |
a2736526 | 438 | } else { |
36e3b2e7 EB |
439 | return paio_submit(bs, s->hfile, sector_num << BDRV_SECTOR_BITS, qiov, |
440 | nb_sectors << BDRV_SECTOR_BITS, | |
a2736526 PB |
441 | cb, opaque, QEMU_AIO_WRITE); |
442 | } | |
223d4670 TS |
443 | } |
444 | ||
7c84b1b8 | 445 | static BlockAIOCB *raw_aio_flush(BlockDriverState *bs, |
097310b5 | 446 | BlockCompletionFunc *cb, void *opaque) |
223d4670 TS |
447 | { |
448 | BDRVRawState *s = bs->opaque; | |
fc4edb84 | 449 | return paio_submit(bs, s->hfile, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH); |
223d4670 TS |
450 | } |
451 | ||
452 | static void raw_close(BlockDriverState *bs) | |
453 | { | |
454 | BDRVRawState *s = bs->opaque; | |
99cc5989 SH |
455 | |
456 | if (s->aio) { | |
85ebd381 | 457 | win32_aio_detach_aio_context(s->aio, bdrv_get_aio_context(bs)); |
99cc5989 SH |
458 | win32_aio_cleanup(s->aio); |
459 | s->aio = NULL; | |
460 | } | |
461 | ||
223d4670 | 462 | CloseHandle(s->hfile); |
8bfea15d KW |
463 | if (bs->open_flags & BDRV_O_TEMPORARY) { |
464 | unlink(bs->filename); | |
465 | } | |
223d4670 TS |
466 | } |
467 | ||
4bff28b8 | 468 | static int raw_truncate(BlockDriverState *bs, int64_t offset, Error **errp) |
223d4670 TS |
469 | { |
470 | BDRVRawState *s = bs->opaque; | |
b9e82a59 | 471 | LONG low, high; |
fbcad04d | 472 | DWORD dwPtrLow; |
223d4670 TS |
473 | |
474 | low = offset; | |
475 | high = offset >> 32; | |
fbcad04d FC |
476 | |
477 | /* | |
478 | * An error has occurred if the return value is INVALID_SET_FILE_POINTER | |
479 | * and GetLastError doesn't return NO_ERROR. | |
480 | */ | |
481 | dwPtrLow = SetFilePointer(s->hfile, low, &high, FILE_BEGIN); | |
482 | if (dwPtrLow == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) { | |
4bff28b8 | 483 | error_setg_win32(errp, GetLastError(), "SetFilePointer error"); |
fbcad04d FC |
484 | return -EIO; |
485 | } | |
486 | if (SetEndOfFile(s->hfile) == 0) { | |
4bff28b8 | 487 | error_setg_win32(errp, GetLastError(), "SetEndOfFile error"); |
223d4670 | 488 | return -EIO; |
fbcad04d | 489 | } |
223d4670 TS |
490 | return 0; |
491 | } | |
492 | ||
493 | static int64_t raw_getlength(BlockDriverState *bs) | |
494 | { | |
495 | BDRVRawState *s = bs->opaque; | |
496 | LARGE_INTEGER l; | |
497 | ULARGE_INTEGER available, total, total_free; | |
498 | DISK_GEOMETRY_EX dg; | |
499 | DWORD count; | |
500 | BOOL status; | |
501 | ||
502 | switch(s->type) { | |
503 | case FTYPE_FILE: | |
b9e82a59 | 504 | l.LowPart = GetFileSize(s->hfile, (PDWORD)&l.HighPart); |
223d4670 TS |
505 | if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR) |
506 | return -EIO; | |
507 | break; | |
508 | case FTYPE_CD: | |
509 | if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free)) | |
510 | return -EIO; | |
511 | l.QuadPart = total.QuadPart; | |
512 | break; | |
513 | case FTYPE_HARDDISK: | |
514 | status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, | |
515 | NULL, 0, &dg, sizeof(dg), &count, NULL); | |
516 | if (status != 0) { | |
517 | l = dg.DiskSize; | |
518 | } | |
519 | break; | |
520 | default: | |
521 | return -EIO; | |
522 | } | |
523 | return l.QuadPart; | |
524 | } | |
525 | ||
4a1d5e1f FZ |
526 | static int64_t raw_get_allocated_file_size(BlockDriverState *bs) |
527 | { | |
528 | typedef DWORD (WINAPI * get_compressed_t)(const char *filename, | |
529 | DWORD * high); | |
530 | get_compressed_t get_compressed; | |
531 | struct _stati64 st; | |
532 | const char *filename = bs->filename; | |
533 | /* WinNT support GetCompressedFileSize to determine allocate size */ | |
534 | get_compressed = | |
535 | (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"), | |
536 | "GetCompressedFileSizeA"); | |
537 | if (get_compressed) { | |
538 | DWORD high, low; | |
539 | low = get_compressed(filename, &high); | |
540 | if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR) { | |
541 | return (((int64_t) high) << 32) + low; | |
542 | } | |
543 | } | |
544 | ||
545 | if (_stati64(filename, &st) < 0) { | |
546 | return -1; | |
547 | } | |
548 | return st.st_size; | |
549 | } | |
550 | ||
ddef7699 | 551 | static int raw_create(const char *filename, QemuOpts *opts, Error **errp) |
223d4670 TS |
552 | { |
553 | int fd; | |
0e7e1989 | 554 | int64_t total_size = 0; |
223d4670 | 555 | |
d5546c5e HR |
556 | strstart(filename, "file:", &filename); |
557 | ||
0e7e1989 | 558 | /* Read out options */ |
180e9526 HT |
559 | total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), |
560 | BDRV_SECTOR_SIZE); | |
223d4670 | 561 | |
6165f4d8 CB |
562 | fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, |
563 | 0644); | |
c6252b7c HR |
564 | if (fd < 0) { |
565 | error_setg_errno(errp, errno, "Could not create file"); | |
223d4670 | 566 | return -EIO; |
c6252b7c | 567 | } |
223d4670 | 568 | set_sparse(fd); |
180e9526 | 569 | ftruncate(fd, total_size); |
2e1e79da | 570 | qemu_close(fd); |
223d4670 TS |
571 | return 0; |
572 | } | |
573 | ||
ddef7699 CL |
574 | |
575 | static QemuOptsList raw_create_opts = { | |
576 | .name = "raw-create-opts", | |
577 | .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head), | |
578 | .desc = { | |
579 | { | |
580 | .name = BLOCK_OPT_SIZE, | |
581 | .type = QEMU_OPT_SIZE, | |
582 | .help = "Virtual disk size" | |
583 | }, | |
584 | { /* end of list */ } | |
585 | } | |
0e7e1989 KW |
586 | }; |
587 | ||
5f535a94 | 588 | BlockDriver bdrv_file = { |
84a12e66 CH |
589 | .format_name = "file", |
590 | .protocol_name = "file", | |
f1b2f712 | 591 | .instance_size = sizeof(BDRVRawState), |
030be321 | 592 | .bdrv_needs_filename = true, |
7dc74db8 | 593 | .bdrv_parse_filename = raw_parse_filename, |
ddef7699 | 594 | .bdrv_file_open = raw_open, |
2914a1de | 595 | .bdrv_refresh_limits = raw_probe_alignment, |
ddef7699 | 596 | .bdrv_close = raw_close, |
c282e1fd | 597 | .bdrv_create = raw_create, |
3ac21627 | 598 | .bdrv_has_zero_init = bdrv_has_zero_init_1, |
c68b89ac | 599 | |
fc4edb84 PB |
600 | .bdrv_aio_readv = raw_aio_readv, |
601 | .bdrv_aio_writev = raw_aio_writev, | |
602 | .bdrv_aio_flush = raw_aio_flush, | |
c68b89ac | 603 | |
f1b2f712 AL |
604 | .bdrv_truncate = raw_truncate, |
605 | .bdrv_getlength = raw_getlength, | |
4a1d5e1f FZ |
606 | .bdrv_get_allocated_file_size |
607 | = raw_get_allocated_file_size, | |
0e7e1989 | 608 | |
ddef7699 | 609 | .create_opts = &raw_create_opts, |
223d4670 TS |
610 | }; |
611 | ||
612 | /***********************************************/ | |
613 | /* host device */ | |
614 | ||
615 | static int find_cdrom(char *cdrom_name, int cdrom_name_size) | |
616 | { | |
617 | char drives[256], *pdrv = drives; | |
618 | UINT type; | |
619 | ||
620 | memset(drives, 0, sizeof(drives)); | |
621 | GetLogicalDriveStrings(sizeof(drives), drives); | |
622 | while(pdrv[0] != '\0') { | |
623 | type = GetDriveType(pdrv); | |
624 | switch(type) { | |
625 | case DRIVE_CDROM: | |
626 | snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]); | |
627 | return 0; | |
628 | break; | |
629 | } | |
630 | pdrv += lstrlen(pdrv) + 1; | |
631 | } | |
632 | return -1; | |
633 | } | |
634 | ||
635 | static int find_device_type(BlockDriverState *bs, const char *filename) | |
636 | { | |
637 | BDRVRawState *s = bs->opaque; | |
638 | UINT type; | |
639 | const char *p; | |
640 | ||
641 | if (strstart(filename, "\\\\.\\", &p) || | |
642 | strstart(filename, "//./", &p)) { | |
643 | if (stristart(p, "PhysicalDrive", NULL)) | |
644 | return FTYPE_HARDDISK; | |
645 | snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]); | |
646 | type = GetDriveType(s->drive_path); | |
3060cd14 AL |
647 | switch (type) { |
648 | case DRIVE_REMOVABLE: | |
649 | case DRIVE_FIXED: | |
650 | return FTYPE_HARDDISK; | |
651 | case DRIVE_CDROM: | |
223d4670 | 652 | return FTYPE_CD; |
3060cd14 | 653 | default: |
223d4670 | 654 | return FTYPE_FILE; |
3060cd14 | 655 | } |
223d4670 TS |
656 | } else { |
657 | return FTYPE_FILE; | |
658 | } | |
659 | } | |
660 | ||
508c7cb3 CH |
661 | static int hdev_probe_device(const char *filename) |
662 | { | |
663 | if (strstart(filename, "/dev/cdrom", NULL)) | |
664 | return 100; | |
665 | if (is_windows_drive(filename)) | |
666 | return 100; | |
667 | return 0; | |
668 | } | |
669 | ||
57ed25b1 HR |
670 | static void hdev_parse_filename(const char *filename, QDict *options, |
671 | Error **errp) | |
672 | { | |
673 | /* The prefix is optional, just as for "file". */ | |
674 | strstart(filename, "host_device:", &filename); | |
675 | ||
46f5ac20 | 676 | qdict_put_str(options, "filename", filename); |
57ed25b1 HR |
677 | } |
678 | ||
015a1036 HR |
679 | static int hdev_open(BlockDriverState *bs, QDict *options, int flags, |
680 | Error **errp) | |
223d4670 TS |
681 | { |
682 | BDRVRawState *s = bs->opaque; | |
683 | int access_flags, create_flags; | |
68dc0364 | 684 | int ret = 0; |
223d4670 TS |
685 | DWORD overlapped; |
686 | char device_name[64]; | |
68dc0364 SW |
687 | |
688 | Error *local_err = NULL; | |
689 | const char *filename; | |
0a4279d9 | 690 | bool use_aio; |
68dc0364 | 691 | |
87ea75d5 PC |
692 | QemuOpts *opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, |
693 | &error_abort); | |
68dc0364 | 694 | qemu_opts_absorb_qdict(opts, options, &local_err); |
84d18f06 | 695 | if (local_err) { |
c6252b7c | 696 | error_propagate(errp, local_err); |
68dc0364 SW |
697 | ret = -EINVAL; |
698 | goto done; | |
699 | } | |
700 | ||
701 | filename = qemu_opt_get(opts, "filename"); | |
223d4670 | 702 | |
0a4279d9 KW |
703 | use_aio = get_aio_option(opts, flags, &local_err); |
704 | if (!local_err && use_aio) { | |
705 | error_setg(&local_err, "AIO is not supported on Windows host devices"); | |
706 | } | |
707 | if (local_err) { | |
708 | error_propagate(errp, local_err); | |
709 | ret = -EINVAL; | |
710 | goto done; | |
711 | } | |
712 | ||
223d4670 | 713 | if (strstart(filename, "/dev/cdrom", NULL)) { |
68dc0364 | 714 | if (find_cdrom(device_name, sizeof(device_name)) < 0) { |
c6252b7c | 715 | error_setg(errp, "Could not open CD-ROM drive"); |
68dc0364 SW |
716 | ret = -ENOENT; |
717 | goto done; | |
718 | } | |
223d4670 TS |
719 | filename = device_name; |
720 | } else { | |
721 | /* transform drive letters into device name */ | |
722 | if (((filename[0] >= 'a' && filename[0] <= 'z') || | |
723 | (filename[0] >= 'A' && filename[0] <= 'Z')) && | |
724 | filename[1] == ':' && filename[2] == '\0') { | |
725 | snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]); | |
726 | filename = device_name; | |
727 | } | |
728 | } | |
729 | s->type = find_device_type(bs, filename); | |
730 | ||
0a4279d9 | 731 | raw_parse_flags(flags, use_aio, &access_flags, &overlapped); |
6a8dc042 | 732 | |
223d4670 TS |
733 | create_flags = OPEN_EXISTING; |
734 | ||
223d4670 TS |
735 | s->hfile = CreateFile(filename, access_flags, |
736 | FILE_SHARE_READ, NULL, | |
737 | create_flags, overlapped, NULL); | |
738 | if (s->hfile == INVALID_HANDLE_VALUE) { | |
739 | int err = GetLastError(); | |
740 | ||
68dc0364 SW |
741 | if (err == ERROR_ACCESS_DENIED) { |
742 | ret = -EACCES; | |
743 | } else { | |
45d57f6e | 744 | ret = -EINVAL; |
68dc0364 | 745 | } |
45d57f6e | 746 | error_setg_errno(errp, -ret, "Could not open device"); |
68dc0364 | 747 | goto done; |
223d4670 | 748 | } |
68dc0364 SW |
749 | |
750 | done: | |
751 | qemu_opts_del(opts); | |
752 | return ret; | |
223d4670 TS |
753 | } |
754 | ||
5efa9d5a | 755 | static BlockDriver bdrv_host_device = { |
e60f469c | 756 | .format_name = "host_device", |
84a12e66 | 757 | .protocol_name = "host_device", |
e60f469c | 758 | .instance_size = sizeof(BDRVRawState), |
030be321 | 759 | .bdrv_needs_filename = true, |
57ed25b1 | 760 | .bdrv_parse_filename = hdev_parse_filename, |
508c7cb3 | 761 | .bdrv_probe_device = hdev_probe_device, |
66f82cee | 762 | .bdrv_file_open = hdev_open, |
e60f469c | 763 | .bdrv_close = raw_close, |
223d4670 | 764 | |
fc4edb84 PB |
765 | .bdrv_aio_readv = raw_aio_readv, |
766 | .bdrv_aio_writev = raw_aio_writev, | |
767 | .bdrv_aio_flush = raw_aio_flush, | |
c68b89ac | 768 | |
85ebd381 SH |
769 | .bdrv_detach_aio_context = raw_detach_aio_context, |
770 | .bdrv_attach_aio_context = raw_attach_aio_context, | |
771 | ||
b94a2610 KW |
772 | .bdrv_getlength = raw_getlength, |
773 | .has_variable_length = true, | |
774 | ||
4a1d5e1f FZ |
775 | .bdrv_get_allocated_file_size |
776 | = raw_get_allocated_file_size, | |
223d4670 | 777 | }; |
5efa9d5a | 778 | |
84a12e66 | 779 | static void bdrv_file_init(void) |
5efa9d5a | 780 | { |
84a12e66 | 781 | bdrv_register(&bdrv_file); |
5efa9d5a AL |
782 | bdrv_register(&bdrv_host_device); |
783 | } | |
784 | ||
84a12e66 | 785 | block_init(bdrv_file_init); |