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