]> git.proxmox.com Git - qemu.git/blob - block/raw-win32.c
Merge remote-tracking branch 'kwolf/for-anthony' into staging
[qemu.git] / block / raw-win32.c
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"
25 #include "qemu-timer.h"
26 #include "block_int.h"
27 #include "module.h"
28 #include <windows.h>
29 #include <winioctl.h>
30
31 #define FTYPE_FILE 0
32 #define FTYPE_CD 1
33 #define FTYPE_HARDDISK 2
34
35 typedef struct BDRVRawState {
36 HANDLE hfile;
37 int type;
38 char drive_path[16]; /* format: "d:\" */
39 } BDRVRawState;
40
41 int qemu_ftruncate64(int fd, int64_t length)
42 {
43 LARGE_INTEGER li;
44 DWORD dw;
45 LONG high;
46 HANDLE h;
47 BOOL res;
48
49 if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
50 return -1;
51
52 h = (HANDLE)_get_osfhandle(fd);
53
54 /* get current position, ftruncate do not change position */
55 li.HighPart = 0;
56 li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
57 if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
58 return -1;
59 }
60
61 high = length >> 32;
62 dw = SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN);
63 if (dw == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
64 return -1;
65 }
66 res = SetEndOfFile(h);
67
68 /* back to old position */
69 SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
70 return res ? 0 : -1;
71 }
72
73 static int set_sparse(int fd)
74 {
75 DWORD returned;
76 return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
77 NULL, 0, NULL, 0, &returned, NULL);
78 }
79
80 static void raw_parse_flags(int flags, int *access_flags, DWORD *overlapped)
81 {
82 assert(access_flags != NULL);
83 assert(overlapped != NULL);
84
85 if (flags & BDRV_O_RDWR) {
86 *access_flags = GENERIC_READ | GENERIC_WRITE;
87 } else {
88 *access_flags = GENERIC_READ;
89 }
90
91 *overlapped = FILE_ATTRIBUTE_NORMAL;
92 if (flags & BDRV_O_NOCACHE) {
93 *overlapped |= FILE_FLAG_NO_BUFFERING;
94 }
95 }
96
97 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
98 {
99 BDRVRawState *s = bs->opaque;
100 int access_flags;
101 DWORD overlapped;
102
103 s->type = FTYPE_FILE;
104
105 raw_parse_flags(flags, &access_flags, &overlapped);
106
107 s->hfile = CreateFile(filename, access_flags,
108 FILE_SHARE_READ, NULL,
109 OPEN_EXISTING, overlapped, NULL);
110 if (s->hfile == INVALID_HANDLE_VALUE) {
111 int err = GetLastError();
112
113 if (err == ERROR_ACCESS_DENIED)
114 return -EACCES;
115 return -1;
116 }
117 return 0;
118 }
119
120 static int raw_read(BlockDriverState *bs, int64_t sector_num,
121 uint8_t *buf, int nb_sectors)
122 {
123 BDRVRawState *s = bs->opaque;
124 OVERLAPPED ov;
125 DWORD ret_count;
126 int ret;
127 int64_t offset = sector_num * 512;
128 int count = nb_sectors * 512;
129
130 memset(&ov, 0, sizeof(ov));
131 ov.Offset = offset;
132 ov.OffsetHigh = offset >> 32;
133 ret = ReadFile(s->hfile, buf, count, &ret_count, &ov);
134 if (!ret)
135 return ret_count;
136 if (ret_count == count)
137 ret_count = 0;
138 return ret_count;
139 }
140
141 static int raw_write(BlockDriverState *bs, int64_t sector_num,
142 const uint8_t *buf, int nb_sectors)
143 {
144 BDRVRawState *s = bs->opaque;
145 OVERLAPPED ov;
146 DWORD ret_count;
147 int ret;
148 int64_t offset = sector_num * 512;
149 int count = nb_sectors * 512;
150
151 memset(&ov, 0, sizeof(ov));
152 ov.Offset = offset;
153 ov.OffsetHigh = offset >> 32;
154 ret = WriteFile(s->hfile, buf, count, &ret_count, &ov);
155 if (!ret)
156 return ret_count;
157 if (ret_count == count)
158 ret_count = 0;
159 return ret_count;
160 }
161
162 static int raw_flush(BlockDriverState *bs)
163 {
164 BDRVRawState *s = bs->opaque;
165 int ret;
166
167 ret = FlushFileBuffers(s->hfile);
168 if (ret == 0) {
169 return -EIO;
170 }
171
172 return 0;
173 }
174
175 static void raw_close(BlockDriverState *bs)
176 {
177 BDRVRawState *s = bs->opaque;
178 CloseHandle(s->hfile);
179 }
180
181 static int raw_truncate(BlockDriverState *bs, int64_t offset)
182 {
183 BDRVRawState *s = bs->opaque;
184 LONG low, high;
185
186 low = offset;
187 high = offset >> 32;
188 if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN))
189 return -EIO;
190 if (!SetEndOfFile(s->hfile))
191 return -EIO;
192 return 0;
193 }
194
195 static int64_t raw_getlength(BlockDriverState *bs)
196 {
197 BDRVRawState *s = bs->opaque;
198 LARGE_INTEGER l;
199 ULARGE_INTEGER available, total, total_free;
200 DISK_GEOMETRY_EX dg;
201 DWORD count;
202 BOOL status;
203
204 switch(s->type) {
205 case FTYPE_FILE:
206 l.LowPart = GetFileSize(s->hfile, (PDWORD)&l.HighPart);
207 if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
208 return -EIO;
209 break;
210 case FTYPE_CD:
211 if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
212 return -EIO;
213 l.QuadPart = total.QuadPart;
214 break;
215 case FTYPE_HARDDISK:
216 status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
217 NULL, 0, &dg, sizeof(dg), &count, NULL);
218 if (status != 0) {
219 l = dg.DiskSize;
220 }
221 break;
222 default:
223 return -EIO;
224 }
225 return l.QuadPart;
226 }
227
228 static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
229 {
230 typedef DWORD (WINAPI * get_compressed_t)(const char *filename,
231 DWORD * high);
232 get_compressed_t get_compressed;
233 struct _stati64 st;
234 const char *filename = bs->filename;
235 /* WinNT support GetCompressedFileSize to determine allocate size */
236 get_compressed =
237 (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"),
238 "GetCompressedFileSizeA");
239 if (get_compressed) {
240 DWORD high, low;
241 low = get_compressed(filename, &high);
242 if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR) {
243 return (((int64_t) high) << 32) + low;
244 }
245 }
246
247 if (_stati64(filename, &st) < 0) {
248 return -1;
249 }
250 return st.st_size;
251 }
252
253 static int raw_create(const char *filename, QEMUOptionParameter *options)
254 {
255 int fd;
256 int64_t total_size = 0;
257
258 /* Read out options */
259 while (options && options->name) {
260 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
261 total_size = options->value.n / 512;
262 }
263 options++;
264 }
265
266 fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
267 0644);
268 if (fd < 0)
269 return -EIO;
270 set_sparse(fd);
271 ftruncate(fd, total_size * 512);
272 qemu_close(fd);
273 return 0;
274 }
275
276 static QEMUOptionParameter raw_create_options[] = {
277 {
278 .name = BLOCK_OPT_SIZE,
279 .type = OPT_SIZE,
280 .help = "Virtual disk size"
281 },
282 { NULL }
283 };
284
285 static BlockDriver bdrv_file = {
286 .format_name = "file",
287 .protocol_name = "file",
288 .instance_size = sizeof(BDRVRawState),
289 .bdrv_file_open = raw_open,
290 .bdrv_close = raw_close,
291 .bdrv_create = raw_create,
292
293 .bdrv_read = raw_read,
294 .bdrv_write = raw_write,
295 .bdrv_co_flush_to_disk = raw_flush,
296
297 .bdrv_truncate = raw_truncate,
298 .bdrv_getlength = raw_getlength,
299 .bdrv_get_allocated_file_size
300 = raw_get_allocated_file_size,
301
302 .create_options = raw_create_options,
303 };
304
305 /***********************************************/
306 /* host device */
307
308 static int find_cdrom(char *cdrom_name, int cdrom_name_size)
309 {
310 char drives[256], *pdrv = drives;
311 UINT type;
312
313 memset(drives, 0, sizeof(drives));
314 GetLogicalDriveStrings(sizeof(drives), drives);
315 while(pdrv[0] != '\0') {
316 type = GetDriveType(pdrv);
317 switch(type) {
318 case DRIVE_CDROM:
319 snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
320 return 0;
321 break;
322 }
323 pdrv += lstrlen(pdrv) + 1;
324 }
325 return -1;
326 }
327
328 static int find_device_type(BlockDriverState *bs, const char *filename)
329 {
330 BDRVRawState *s = bs->opaque;
331 UINT type;
332 const char *p;
333
334 if (strstart(filename, "\\\\.\\", &p) ||
335 strstart(filename, "//./", &p)) {
336 if (stristart(p, "PhysicalDrive", NULL))
337 return FTYPE_HARDDISK;
338 snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
339 type = GetDriveType(s->drive_path);
340 switch (type) {
341 case DRIVE_REMOVABLE:
342 case DRIVE_FIXED:
343 return FTYPE_HARDDISK;
344 case DRIVE_CDROM:
345 return FTYPE_CD;
346 default:
347 return FTYPE_FILE;
348 }
349 } else {
350 return FTYPE_FILE;
351 }
352 }
353
354 static int hdev_probe_device(const char *filename)
355 {
356 if (strstart(filename, "/dev/cdrom", NULL))
357 return 100;
358 if (is_windows_drive(filename))
359 return 100;
360 return 0;
361 }
362
363 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
364 {
365 BDRVRawState *s = bs->opaque;
366 int access_flags, create_flags;
367 DWORD overlapped;
368 char device_name[64];
369
370 if (strstart(filename, "/dev/cdrom", NULL)) {
371 if (find_cdrom(device_name, sizeof(device_name)) < 0)
372 return -ENOENT;
373 filename = device_name;
374 } else {
375 /* transform drive letters into device name */
376 if (((filename[0] >= 'a' && filename[0] <= 'z') ||
377 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
378 filename[1] == ':' && filename[2] == '\0') {
379 snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
380 filename = device_name;
381 }
382 }
383 s->type = find_device_type(bs, filename);
384
385 raw_parse_flags(flags, &access_flags, &overlapped);
386
387 create_flags = OPEN_EXISTING;
388
389 s->hfile = CreateFile(filename, access_flags,
390 FILE_SHARE_READ, NULL,
391 create_flags, overlapped, NULL);
392 if (s->hfile == INVALID_HANDLE_VALUE) {
393 int err = GetLastError();
394
395 if (err == ERROR_ACCESS_DENIED)
396 return -EACCES;
397 return -1;
398 }
399 return 0;
400 }
401
402 static int hdev_has_zero_init(BlockDriverState *bs)
403 {
404 return 0;
405 }
406
407 static BlockDriver bdrv_host_device = {
408 .format_name = "host_device",
409 .protocol_name = "host_device",
410 .instance_size = sizeof(BDRVRawState),
411 .bdrv_probe_device = hdev_probe_device,
412 .bdrv_file_open = hdev_open,
413 .bdrv_close = raw_close,
414 .bdrv_has_zero_init = hdev_has_zero_init,
415
416 .bdrv_read = raw_read,
417 .bdrv_write = raw_write,
418 .bdrv_co_flush_to_disk = raw_flush,
419
420 .bdrv_getlength = raw_getlength,
421 .bdrv_get_allocated_file_size
422 = raw_get_allocated_file_size,
423 };
424
425 static void bdrv_file_init(void)
426 {
427 bdrv_register(&bdrv_file);
428 bdrv_register(&bdrv_host_device);
429 }
430
431 block_init(bdrv_file_init);