ia64/xen-unstable

view tools/ioemu/block.c @ 14913:837d12d4d2d1

[qemu-dm] Add bounds checks to block device reads/writes.

Signed-off-by: Christian Limpach <Christian.Limpach@xensource.com>
author Christian Limpach <Christian.Limpach@xensource.com>
date Tue Apr 24 17:02:09 2007 +0100 (2007-04-24)
parents 9bb6c1c1890a
children 00618037d37d
line source
1 /*
2 * QEMU System Emulator block driver
3 *
4 * Copyright (c) 2003 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 "vl.h"
25 #include "block_int.h"
27 #ifdef _BSD
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/ioctl.h>
31 #include <sys/queue.h>
32 #include <sys/disk.h>
33 #endif
35 #ifdef CONFIG_COCOA
36 #include <paths.h>
37 #include <sys/param.h>
38 #include <IOKit/IOKitLib.h>
39 #include <IOKit/IOBSD.h>
40 #include <IOKit/storage/IOMediaBSDClient.h>
41 #include <IOKit/storage/IOMedia.h>
42 #include <IOKit/storage/IOCDMedia.h>
43 //#include <IOKit/storage/IOCDTypes.h>
44 #include <CoreFoundation/CoreFoundation.h>
45 #endif
47 #ifdef __sun__
48 #include <sys/dkio.h>
49 #endif
51 static BlockDriverState *bdrv_first;
52 static BlockDriver *first_drv;
54 #ifdef CONFIG_COCOA
55 static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
56 static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
58 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
59 {
60 kern_return_t kernResult;
61 mach_port_t masterPort;
62 CFMutableDictionaryRef classesToMatch;
64 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
65 if ( KERN_SUCCESS != kernResult ) {
66 printf( "IOMasterPort returned %d\n", kernResult );
67 }
69 classesToMatch = IOServiceMatching( kIOCDMediaClass );
70 if ( classesToMatch == NULL ) {
71 printf( "IOServiceMatching returned a NULL dictionary.\n" );
72 } else {
73 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
74 }
75 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
76 if ( KERN_SUCCESS != kernResult )
77 {
78 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
79 }
81 return kernResult;
82 }
84 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
85 {
86 io_object_t nextMedia;
87 kern_return_t kernResult = KERN_FAILURE;
88 *bsdPath = '\0';
89 nextMedia = IOIteratorNext( mediaIterator );
90 if ( nextMedia )
91 {
92 CFTypeRef bsdPathAsCFString;
93 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
94 if ( bsdPathAsCFString ) {
95 size_t devPathLength;
96 strcpy( bsdPath, _PATH_DEV );
97 strcat( bsdPath, "r" );
98 devPathLength = strlen( bsdPath );
99 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
100 kernResult = KERN_SUCCESS;
101 }
102 CFRelease( bsdPathAsCFString );
103 }
104 IOObjectRelease( nextMedia );
105 }
107 return kernResult;
108 }
110 #endif
112 void bdrv_register(BlockDriver *bdrv)
113 {
114 bdrv->next = first_drv;
115 first_drv = bdrv;
116 }
118 /* create a new block device (by default it is empty) */
119 BlockDriverState *bdrv_new(const char *device_name)
120 {
121 BlockDriverState **pbs, *bs;
123 bs = qemu_mallocz(sizeof(BlockDriverState));
124 if(!bs)
125 return NULL;
126 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
127 if (device_name[0] != '\0') {
128 /* insert at the end */
129 pbs = &bdrv_first;
130 while (*pbs != NULL)
131 pbs = &(*pbs)->next;
132 *pbs = bs;
133 }
134 return bs;
135 }
137 BlockDriver *bdrv_find_format(const char *format_name)
138 {
139 BlockDriver *drv1;
140 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
141 if (!strcmp(drv1->format_name, format_name))
142 return drv1;
143 }
144 return NULL;
145 }
147 int bdrv_create(BlockDriver *drv,
148 const char *filename, int64_t size_in_sectors,
149 const char *backing_file, int flags)
150 {
151 if (!drv->bdrv_create)
152 return -ENOTSUP;
153 return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
154 }
156 #ifdef _WIN32
157 void get_tmp_filename(char *filename, int size)
158 {
159 char* p = strrchr(filename, '/');
161 if (p == NULL)
162 return;
164 /* XXX: find a better function */
165 tmpnam(p);
166 *p = '/';
167 }
168 #else
169 void get_tmp_filename(char *filename, int size)
170 {
171 int fd;
172 /* XXX: race condition possible */
173 pstrcpy(filename, size, "/tmp/vl.XXXXXX");
174 fd = mkstemp(filename);
175 close(fd);
176 }
177 #endif
179 /* XXX: force raw format if block or character device ? It would
180 simplify the BSD case */
181 static BlockDriver *find_image_format(const char *filename)
182 {
183 int fd, ret, score, score_max;
184 BlockDriver *drv1, *drv;
185 uint8_t *buf;
186 size_t bufsize = 1024;
188 fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
189 if (fd < 0) {
190 buf = NULL;
191 ret = 0;
192 } else {
193 #ifdef DIOCGSECTORSIZE
194 {
195 unsigned int sectorsize = 512;
196 if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
197 sectorsize > bufsize)
198 bufsize = sectorsize;
199 }
200 #endif
201 #ifdef CONFIG_COCOA
202 u_int32_t blockSize = 512;
203 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
204 bufsize = blockSize;
205 }
206 #endif
207 buf = qemu_malloc(bufsize);
208 if (!buf)
209 return NULL;
210 ret = read(fd, buf, bufsize);
211 if (ret < 0) {
212 close(fd);
213 qemu_free(buf);
214 return NULL;
215 }
216 close(fd);
217 }
219 drv = NULL;
220 score_max = 0;
221 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
222 score = drv1->bdrv_probe(buf, ret, filename);
223 if (score > score_max) {
224 score_max = score;
225 drv = drv1;
226 }
227 }
228 qemu_free(buf);
229 return drv;
230 }
232 int bdrv_open(BlockDriverState *bs, const char *filename, int snapshot)
233 {
234 #ifdef CONFIG_COCOA
235 if ( strncmp( filename, "/dev/cdrom", 10 ) == 0 ) {
236 kern_return_t kernResult;
237 io_iterator_t mediaIterator;
238 char bsdPath[ MAXPATHLEN ];
239 int fd;
241 kernResult = FindEjectableCDMedia( &mediaIterator );
242 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
244 if ( bsdPath[ 0 ] != '\0' ) {
245 strcat(bsdPath,"s0");
246 /* some CDs don't have a partition 0 */
247 fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
248 if (fd < 0) {
249 bsdPath[strlen(bsdPath)-1] = '1';
250 } else {
251 close(fd);
252 }
253 filename = bsdPath;
254 }
256 if ( mediaIterator )
257 IOObjectRelease( mediaIterator );
258 }
259 #endif
260 return bdrv_open2(bs, filename, snapshot, NULL);
261 }
263 int bdrv_open2(BlockDriverState *bs, const char *filename, int snapshot,
264 BlockDriver *drv)
265 {
266 int ret;
267 char tmp_filename[1024];
269 bs->read_only = 0;
270 bs->is_temporary = 0;
271 bs->encrypted = 0;
273 if (snapshot) {
274 BlockDriverState *bs1;
275 int64_t total_size;
277 /* if snapshot, we create a temporary backing file and open it
278 instead of opening 'filename' directly */
280 /* if there is a backing file, use it */
281 bs1 = bdrv_new("");
282 if (!bs1) {
283 return -1;
284 }
285 if (bdrv_open(bs1, filename, 0) < 0) {
286 bdrv_delete(bs1);
287 return -1;
288 }
289 total_size = bs1->total_sectors;
290 bdrv_delete(bs1);
292 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
293 /* XXX: use cow for linux as it is more efficient ? */
294 if (bdrv_create(&bdrv_qcow, tmp_filename,
295 total_size, filename, 0) < 0) {
296 return -1;
297 }
298 filename = tmp_filename;
299 bs->is_temporary = 1;
300 }
302 pstrcpy(bs->filename, sizeof(bs->filename), filename);
303 if (!drv) {
304 drv = find_image_format(filename);
305 if (!drv)
306 return -1;
307 }
308 bs->drv = drv;
309 bs->opaque = qemu_mallocz(drv->instance_size);
310 if (bs->opaque == NULL && drv->instance_size > 0)
311 return -1;
313 ret = drv->bdrv_open(bs, filename);
314 if (ret < 0) {
315 qemu_free(bs->opaque);
316 return -1;
317 }
318 #ifndef _WIN32
319 if (bs->is_temporary) {
320 unlink(filename);
321 }
322 #endif
323 if (bs->backing_file[0] != '\0' && drv->bdrv_is_allocated) {
324 /* if there is a backing file, use it */
325 bs->backing_hd = bdrv_new("");
326 if (!bs->backing_hd) {
327 fail:
328 bdrv_close(bs);
329 return -1;
330 }
331 if (bdrv_open(bs->backing_hd, bs->backing_file, 0) < 0)
332 goto fail;
333 }
335 bs->inserted = 1;
337 /* call the change callback */
338 if (bs->change_cb)
339 bs->change_cb(bs->change_opaque);
341 return 0;
342 }
344 void bdrv_close(BlockDriverState *bs)
345 {
346 if (bs->inserted) {
347 if (bs->backing_hd)
348 bdrv_delete(bs->backing_hd);
349 bs->drv->bdrv_close(bs);
350 qemu_free(bs->opaque);
351 #ifdef _WIN32
352 if (bs->is_temporary) {
353 unlink(bs->filename);
354 }
355 #endif
356 bs->opaque = NULL;
357 bs->drv = NULL;
358 bs->inserted = 0;
360 /* call the change callback */
361 if (bs->change_cb)
362 bs->change_cb(bs->change_opaque);
363 }
364 }
366 void bdrv_delete(BlockDriverState *bs)
367 {
368 /* XXX: remove the driver list */
369 bdrv_close(bs);
370 qemu_free(bs);
371 }
373 /* commit COW file into the raw image */
374 int bdrv_commit(BlockDriverState *bs)
375 {
376 int64_t i;
377 int n, j;
378 unsigned char sector[512];
380 if (!bs->inserted)
381 return -ENOENT;
383 if (bs->read_only) {
384 return -EACCES;
385 }
387 if (!bs->backing_hd) {
388 return -ENOTSUP;
389 }
391 for (i = 0; i < bs->total_sectors;) {
392 if (bs->drv->bdrv_is_allocated(bs, i, 65536, &n)) {
393 for(j = 0; j < n; j++) {
394 if (bdrv_read(bs, i, sector, 1) != 0) {
395 return -EIO;
396 }
398 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
399 return -EIO;
400 }
401 i++;
402 }
403 } else {
404 i += n;
405 }
406 }
408 if (bs->drv->bdrv_make_empty)
409 return bs->drv->bdrv_make_empty(bs);
411 return 0;
412 }
414 /* return -1 if error */
415 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
416 uint8_t *buf, int nb_sectors)
417 {
418 int ret, n;
419 BlockDriver *drv = bs->drv;
421 if (!bs->inserted)
422 return -1;
423 if (sector_num < 0)
424 return -1;
426 while (nb_sectors > 0) {
427 if (sector_num == 0 && bs->boot_sector_enabled) {
428 memcpy(buf, bs->boot_sector_data, 512);
429 n = 1;
430 } else if (bs->backing_hd) {
431 if (drv->bdrv_is_allocated(bs, sector_num, nb_sectors, &n)) {
432 ret = drv->bdrv_read(bs, sector_num, buf, n);
433 if (ret < 0)
434 return -1;
435 } else {
436 /* read from the base image */
437 ret = bdrv_read(bs->backing_hd, sector_num, buf, n);
438 if (ret < 0)
439 return -1;
440 }
441 } else {
442 ret = drv->bdrv_read(bs, sector_num, buf, nb_sectors);
443 if (ret < 0)
444 return -1;
445 /* no need to loop */
446 break;
447 }
448 nb_sectors -= n;
449 sector_num += n;
450 buf += n * 512;
451 }
452 return 0;
453 }
455 /* return -1 if error */
456 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
457 const uint8_t *buf, int nb_sectors)
458 {
459 if (!bs->inserted)
460 return -1;
461 if (bs->read_only)
462 return -1;
463 if (sector_num < 0)
464 return -1;
465 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
466 memcpy(bs->boot_sector_data, buf, 512);
467 }
468 return bs->drv->bdrv_write(bs, sector_num, buf, nb_sectors);
469 }
471 void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr)
472 {
473 *nb_sectors_ptr = bs->total_sectors;
474 }
476 /* force a given boot sector. */
477 void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size)
478 {
479 bs->boot_sector_enabled = 1;
480 if (size > 512)
481 size = 512;
482 memcpy(bs->boot_sector_data, data, size);
483 memset(bs->boot_sector_data + size, 0, 512 - size);
484 }
486 void bdrv_set_geometry_hint(BlockDriverState *bs,
487 int cyls, int heads, int secs)
488 {
489 bs->cyls = cyls;
490 bs->heads = heads;
491 bs->secs = secs;
492 }
494 void bdrv_set_type_hint(BlockDriverState *bs, int type)
495 {
496 bs->type = type;
497 bs->removable = ((type == BDRV_TYPE_CDROM ||
498 type == BDRV_TYPE_FLOPPY));
499 }
501 void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
502 {
503 bs->translation = translation;
504 }
506 void bdrv_get_geometry_hint(BlockDriverState *bs,
507 int *pcyls, int *pheads, int *psecs)
508 {
509 *pcyls = bs->cyls;
510 *pheads = bs->heads;
511 *psecs = bs->secs;
512 }
514 int bdrv_get_type_hint(BlockDriverState *bs)
515 {
516 return bs->type;
517 }
519 int bdrv_get_translation_hint(BlockDriverState *bs)
520 {
521 return bs->translation;
522 }
524 int bdrv_is_removable(BlockDriverState *bs)
525 {
526 return bs->removable;
527 }
529 int bdrv_is_read_only(BlockDriverState *bs)
530 {
531 return bs->read_only;
532 }
534 int bdrv_is_inserted(BlockDriverState *bs)
535 {
536 return bs->inserted;
537 }
539 int bdrv_is_locked(BlockDriverState *bs)
540 {
541 return bs->locked;
542 }
544 void bdrv_set_locked(BlockDriverState *bs, int locked)
545 {
546 bs->locked = locked;
547 }
549 void bdrv_set_change_cb(BlockDriverState *bs,
550 void (*change_cb)(void *opaque), void *opaque)
551 {
552 bs->change_cb = change_cb;
553 bs->change_opaque = opaque;
554 }
556 int bdrv_is_encrypted(BlockDriverState *bs)
557 {
558 if (bs->backing_hd && bs->backing_hd->encrypted)
559 return 1;
560 return bs->encrypted;
561 }
563 int bdrv_set_key(BlockDriverState *bs, const char *key)
564 {
565 int ret;
566 if (bs->backing_hd && bs->backing_hd->encrypted) {
567 ret = bdrv_set_key(bs->backing_hd, key);
568 if (ret < 0)
569 return ret;
570 if (!bs->encrypted)
571 return 0;
572 }
573 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
574 return -1;
575 return bs->drv->bdrv_set_key(bs, key);
576 }
578 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
579 {
580 if (!bs->inserted || !bs->drv) {
581 buf[0] = '\0';
582 } else {
583 pstrcpy(buf, buf_size, bs->drv->format_name);
584 }
585 }
587 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
588 void *opaque)
589 {
590 BlockDriver *drv;
592 for (drv = first_drv; drv != NULL; drv = drv->next) {
593 it(opaque, drv->format_name);
594 }
595 }
597 BlockDriverState *bdrv_find(const char *name)
598 {
599 BlockDriverState *bs;
601 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
602 if (!strcmp(name, bs->device_name))
603 return bs;
604 }
605 return NULL;
606 }
608 void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
609 {
610 BlockDriverState *bs;
612 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
613 it(opaque, bs->device_name);
614 }
615 }
617 const char *bdrv_get_device_name(BlockDriverState *bs)
618 {
619 return bs->device_name;
620 }
622 void bdrv_flush(BlockDriverState *bs)
623 {
624 if (bs->drv->bdrv_flush)
625 bs->drv->bdrv_flush(bs);
626 if (bs->backing_hd)
627 bdrv_flush(bs->backing_hd);
628 }
630 void bdrv_info(void)
631 {
632 BlockDriverState *bs;
634 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
635 term_printf("%s:", bs->device_name);
636 term_printf(" type=");
637 switch(bs->type) {
638 case BDRV_TYPE_HD:
639 term_printf("hd");
640 break;
641 case BDRV_TYPE_CDROM:
642 term_printf("cdrom");
643 break;
644 case BDRV_TYPE_FLOPPY:
645 term_printf("floppy");
646 break;
647 }
648 term_printf(" removable=%d", bs->removable);
649 if (bs->removable) {
650 term_printf(" locked=%d", bs->locked);
651 }
652 if (bs->inserted) {
653 term_printf(" file=%s", bs->filename);
654 if (bs->backing_file[0] != '\0')
655 term_printf(" backing_file=%s", bs->backing_file);
656 term_printf(" ro=%d", bs->read_only);
657 term_printf(" drv=%s", bs->drv->format_name);
658 if (bs->encrypted)
659 term_printf(" encrypted");
660 } else {
661 term_printf(" [not inserted]");
662 }
663 term_printf("\n");
664 }
665 }
667 /**************************************************************/
668 /* RAW block driver */
670 typedef struct BDRVRawState {
671 int fd;
672 } BDRVRawState;
674 static int raw_probe(const uint8_t *buf, int buf_size, const char *filename)
675 {
676 return 1; /* maybe */
677 }
679 static int raw_open(BlockDriverState *bs, const char *filename)
680 {
681 BDRVRawState *s = bs->opaque;
682 int fd;
683 int64_t size;
684 #ifdef _BSD
685 struct stat sb;
686 #endif
687 #ifdef __sun__
688 struct dk_minfo minfo;
689 int rv;
690 #endif
692 fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE);
693 if (fd < 0) {
694 fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
695 if (fd < 0)
696 return -1;
697 bs->read_only = 1;
698 }
699 #ifdef _BSD
700 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
701 #ifdef DIOCGMEDIASIZE
702 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
703 #endif
704 #ifdef CONFIG_COCOA
705 size = LONG_LONG_MAX;
706 #else
707 size = lseek(fd, 0LL, SEEK_END);
708 #endif
709 } else
710 #endif
711 #ifdef __sun__
712 /*
713 * use the DKIOCGMEDIAINFO ioctl to read the size.
714 */
715 rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
716 if ( rv != -1 ) {
717 size = minfo.dki_lbsize * minfo.dki_capacity;
718 } else /* there are reports that lseek on some devices
719 fails, but irc discussion said that contingency
720 on contingency was overkill */
721 #endif
722 {
723 size = lseek(fd, 0, SEEK_END);
724 }
725 #ifdef _WIN32
726 /* On Windows hosts it can happen that we're unable to get file size
727 for CD-ROM raw device (it's inherent limitation of the CDFS driver). */
728 if (size == -1)
729 size = LONG_LONG_MAX;
730 #endif
731 bs->total_sectors = size / 512;
732 s->fd = fd;
733 return 0;
734 }
736 static int raw_read(BlockDriverState *bs, int64_t sector_num,
737 uint8_t *buf, int nb_sectors)
738 {
739 BDRVRawState *s = bs->opaque;
740 int ret;
742 lseek(s->fd, sector_num * 512, SEEK_SET);
743 ret = read(s->fd, buf, nb_sectors * 512);
744 if (ret != nb_sectors * 512)
745 return -1;
746 return 0;
747 }
749 static int raw_write(BlockDriverState *bs, int64_t sector_num,
750 const uint8_t *buf, int nb_sectors)
751 {
752 BDRVRawState *s = bs->opaque;
753 int ret;
755 lseek(s->fd, sector_num * 512, SEEK_SET);
756 ret = write(s->fd, buf, nb_sectors * 512);
757 if (ret != nb_sectors * 512)
758 return -1;
759 return 0;
760 }
762 static void raw_close(BlockDriverState *bs)
763 {
764 BDRVRawState *s = bs->opaque;
765 bs->total_sectors = 0;
766 close(s->fd);
767 }
769 #ifdef _WIN32
770 #include <windows.h>
771 #include <winioctl.h>
773 int qemu_ftruncate64(int fd, int64_t length)
774 {
775 LARGE_INTEGER li;
776 LONG high;
777 HANDLE h;
778 BOOL res;
780 if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
781 return -1;
783 h = (HANDLE)_get_osfhandle(fd);
785 /* get current position, ftruncate do not change position */
786 li.HighPart = 0;
787 li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
788 if (li.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
789 return -1;
791 high = length >> 32;
792 if (!SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN))
793 return -1;
794 res = SetEndOfFile(h);
796 /* back to old position */
797 SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
798 return res ? 0 : -1;
799 }
801 static int set_sparse(int fd)
802 {
803 DWORD returned;
804 return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
805 NULL, 0, NULL, 0, &returned, NULL);
806 }
807 #else
808 static inline int set_sparse(int fd)
809 {
810 return 1;
811 }
812 #endif
814 static int raw_create(const char *filename, int64_t total_size,
815 const char *backing_file, int flags)
816 {
817 int fd;
819 if (flags || backing_file)
820 return -ENOTSUP;
822 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
823 0644);
824 if (fd < 0)
825 return -EIO;
826 set_sparse(fd);
827 ftruncate(fd, total_size * 512);
828 close(fd);
829 return 0;
830 }
832 static void raw_flush(BlockDriverState *bs)
833 {
834 BDRVRawState *s = bs->opaque;
835 fsync(s->fd);
836 }
838 BlockDriver bdrv_raw = {
839 "raw",
840 sizeof(BDRVRawState),
841 raw_probe,
842 raw_open,
843 raw_read,
844 raw_write,
845 raw_close,
846 raw_create,
847 raw_flush,
848 };
850 void bdrv_init(void)
851 {
852 bdrv_register(&bdrv_raw);
853 #ifndef _WIN32
854 bdrv_register(&bdrv_cow);
855 #endif
856 bdrv_register(&bdrv_qcow);
857 bdrv_register(&bdrv_vmdk);
858 bdrv_register(&bdrv_cloop);
859 bdrv_register(&bdrv_dmg);
860 bdrv_register(&bdrv_bochs);
861 bdrv_register(&bdrv_vpc);
862 bdrv_register(&bdrv_vvfat);
863 }