ia64/xen-unstable

view tools/ioemu/block.c @ 17042:a905c582a406

Add stubdomain support. See stubdom/README for usage details.

- Move PAGE_SIZE and STACK_SIZE into __PAGE_SIZE and __STACK_SIZE in
arch_limits.h so as to permit getting them from there without
pulling all the internal Mini-OS defines.
- Setup a xen-elf cross-compilation environment in stubdom/cross-root
- Add a POSIX layer on top of Mini-OS by linking against the newlib C
library and lwIP, and implementing the Unixish part in mini-os/lib/sys.c
- Cross-compile zlib and libpci too.
- Add an xs.h-compatible layer on top of Mini-OS' xenbus.
- Cross-compile libxc with an additional xc_minios.c and a few things
disabled.
- Cross-compile ioemu with an additional block-vbd, but without sound,
tpm and other details. A few hacks are needed:
- Align ide and scsi buffers at least on sector size to permit
direct transmission to the block backend. While we are at it, just
page-align it to possibly save a segment. Also, limit the scsi
buffer size because of limitations of the block paravirtualization
protocol.
- Allocate big tables dynamically rather that letting them go to
bss: when Mini-OS gets installed in memory, bss is not lazily
allocated, and doing so during Mini-OS is unnecessarily trick while
we can simply use malloc.
- Had to change the Mini-OS compilation somehow, so as to export
Mini-OS compilation flags to the Makefiles of libxc and ioemu.

Signed-off-by: Samuel Thibault <samuel.thibault@eu.citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Feb 12 14:35:39 2008 +0000 (2008-02-12)
parents 00618037d37d
children e3c722d483f5
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 #define SECTOR_BITS 9
36 #define SECTOR_SIZE (1 << SECTOR_BITS)
38 typedef struct BlockDriverAIOCBSync {
39 BlockDriverAIOCB common;
40 QEMUBH *bh;
41 int ret;
42 } BlockDriverAIOCBSync;
44 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
45 int64_t sector_num, uint8_t *buf, int nb_sectors,
46 BlockDriverCompletionFunc *cb, void *opaque);
47 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
48 int64_t sector_num, const uint8_t *buf, int nb_sectors,
49 BlockDriverCompletionFunc *cb, void *opaque);
50 static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
51 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
52 uint8_t *buf, int nb_sectors);
53 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
54 const uint8_t *buf, int nb_sectors);
56 static BlockDriverState *bdrv_first;
57 static BlockDriver *first_drv;
59 int path_is_absolute(const char *path)
60 {
61 const char *p;
62 #ifdef _WIN32
63 /* specific case for names like: "\\.\d:" */
64 if (*path == '/' || *path == '\\')
65 return 1;
66 #endif
67 p = strchr(path, ':');
68 if (p)
69 p++;
70 else
71 p = path;
72 #ifdef _WIN32
73 return (*p == '/' || *p == '\\');
74 #else
75 return (*p == '/');
76 #endif
77 }
79 /* if filename is absolute, just copy it to dest. Otherwise, build a
80 path to it by considering it is relative to base_path. URL are
81 supported. */
82 void path_combine(char *dest, int dest_size,
83 const char *base_path,
84 const char *filename)
85 {
86 const char *p, *p1;
87 int len;
89 if (dest_size <= 0)
90 return;
91 if (path_is_absolute(filename)) {
92 pstrcpy(dest, dest_size, filename);
93 } else {
94 p = strchr(base_path, ':');
95 if (p)
96 p++;
97 else
98 p = base_path;
99 p1 = strrchr(base_path, '/');
100 #ifdef _WIN32
101 {
102 const char *p2;
103 p2 = strrchr(base_path, '\\');
104 if (!p1 || p2 > p1)
105 p1 = p2;
106 }
107 #endif
108 if (p1)
109 p1++;
110 else
111 p1 = base_path;
112 if (p1 > p)
113 p = p1;
114 len = p - base_path;
115 if (len > dest_size - 1)
116 len = dest_size - 1;
117 memcpy(dest, base_path, len);
118 dest[len] = '\0';
119 pstrcat(dest, dest_size, filename);
120 }
121 }
124 void bdrv_register(BlockDriver *bdrv)
125 {
126 if (!bdrv->bdrv_aio_read) {
127 /* add AIO emulation layer */
128 bdrv->bdrv_aio_read = bdrv_aio_read_em;
129 bdrv->bdrv_aio_write = bdrv_aio_write_em;
130 bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
131 bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
132 } else if (!bdrv->bdrv_read && !bdrv->bdrv_pread) {
133 /* add synchronous IO emulation layer */
134 bdrv->bdrv_read = bdrv_read_em;
135 bdrv->bdrv_write = bdrv_write_em;
136 }
137 bdrv->next = first_drv;
138 first_drv = bdrv;
139 }
141 /* create a new block device (by default it is empty) */
142 BlockDriverState *bdrv_new(const char *device_name)
143 {
144 BlockDriverState **pbs, *bs;
146 bs = qemu_mallocz(sizeof(BlockDriverState));
147 if(!bs)
148 return NULL;
149 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
150 if (device_name[0] != '\0') {
151 /* insert at the end */
152 pbs = &bdrv_first;
153 while (*pbs != NULL)
154 pbs = &(*pbs)->next;
155 *pbs = bs;
156 }
157 return bs;
158 }
160 BlockDriver *bdrv_find_format(const char *format_name)
161 {
162 BlockDriver *drv1;
163 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
164 if (!strcmp(drv1->format_name, format_name))
165 return drv1;
166 }
167 return NULL;
168 }
170 int bdrv_create(BlockDriver *drv,
171 const char *filename, int64_t size_in_sectors,
172 const char *backing_file, int flags)
173 {
174 if (!drv->bdrv_create)
175 return -ENOTSUP;
176 return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
177 }
179 #ifdef _WIN32
180 void get_tmp_filename(char *filename, int size)
181 {
182 char temp_dir[MAX_PATH];
184 GetTempPath(MAX_PATH, temp_dir);
185 GetTempFileName(temp_dir, "qem", 0, filename);
186 }
187 #else
188 void get_tmp_filename(char *filename, int size)
189 {
190 int fd;
191 /* XXX: race condition possible */
192 pstrcpy(filename, size, "/tmp/vl.XXXXXX");
193 fd = mkstemp(filename);
194 close(fd);
195 }
196 #endif
198 #ifdef _WIN32
199 static int is_windows_drive_prefix(const char *filename)
200 {
201 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
202 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
203 filename[1] == ':');
204 }
206 static int is_windows_drive(const char *filename)
207 {
208 if (is_windows_drive_prefix(filename) &&
209 filename[2] == '\0')
210 return 1;
211 if (strstart(filename, "\\\\.\\", NULL) ||
212 strstart(filename, "//./", NULL))
213 return 1;
214 return 0;
215 }
216 #endif
218 static BlockDriver *find_protocol(const char *filename)
219 {
220 BlockDriver *drv1;
221 char protocol[128];
222 int len;
223 const char *p;
225 #ifdef _WIN32
226 if (is_windows_drive(filename) ||
227 is_windows_drive_prefix(filename))
228 return &bdrv_raw;
229 #endif
230 p = strchr(filename, ':');
231 if (!p)
232 return &bdrv_raw;
233 len = p - filename;
234 if (len > sizeof(protocol) - 1)
235 len = sizeof(protocol) - 1;
236 memcpy(protocol, filename, len);
237 protocol[len] = '\0';
238 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
239 if (drv1->protocol_name &&
240 !strcmp(drv1->protocol_name, protocol))
241 return drv1;
242 }
243 return NULL;
244 }
246 /* XXX: force raw format if block or character device ? It would
247 simplify the BSD case */
248 static BlockDriver *find_image_format(const char *filename)
249 {
250 int ret, score, score_max;
251 BlockDriver *drv1, *drv;
252 uint8_t buf[2048];
253 BlockDriverState *bs;
255 /* detect host devices. By convention, /dev/cdrom[N] is always
256 recognized as a host CDROM */
257 if (strstart(filename, "/dev/cdrom", NULL))
258 return &bdrv_host_device;
259 #ifdef _WIN32
260 if (is_windows_drive(filename))
261 return &bdrv_host_device;
262 #else
263 {
264 struct stat st;
265 if (stat(filename, &st) >= 0 &&
266 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
267 return &bdrv_host_device;
268 }
269 }
270 #endif
272 drv = find_protocol(filename);
273 /* no need to test disk image formats for vvfat */
274 if (drv == &bdrv_vvfat)
275 return drv;
277 ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
278 if (ret < 0)
279 return NULL;
280 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
281 bdrv_delete(bs);
282 if (ret < 0) {
283 return NULL;
284 }
286 score_max = 0;
287 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
288 if (drv1->bdrv_probe) {
289 score = drv1->bdrv_probe(buf, ret, filename);
290 if (score > score_max) {
291 score_max = score;
292 drv = drv1;
293 }
294 }
295 }
296 return drv;
297 }
299 int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
300 {
301 BlockDriverState *bs;
302 int ret;
304 bs = bdrv_new("");
305 if (!bs)
306 return -ENOMEM;
307 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
308 if (ret < 0) {
309 bdrv_delete(bs);
310 return ret;
311 }
312 *pbs = bs;
313 return 0;
314 }
316 int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
317 {
318 return bdrv_open2(bs, filename, flags, NULL);
319 }
321 int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
322 BlockDriver *drv)
323 {
324 int ret, open_flags;
325 char tmp_filename[1024];
326 char backing_filename[1024];
328 bs->read_only = 0;
329 bs->is_temporary = 0;
330 bs->encrypted = 0;
332 if (flags & BDRV_O_SNAPSHOT) {
333 BlockDriverState *bs1;
334 int64_t total_size;
336 /* if snapshot, we create a temporary backing file and open it
337 instead of opening 'filename' directly */
339 /* if there is a backing file, use it */
340 bs1 = bdrv_new("");
341 if (!bs1) {
342 return -ENOMEM;
343 }
344 if (bdrv_open(bs1, filename, 0) < 0) {
345 bdrv_delete(bs1);
346 return -1;
347 }
348 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
349 bdrv_delete(bs1);
351 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
352 realpath(filename, backing_filename);
353 if (bdrv_create(&bdrv_qcow2, tmp_filename,
354 total_size, backing_filename, 0) < 0) {
355 return -1;
356 }
357 filename = tmp_filename;
358 bs->is_temporary = 1;
359 }
361 pstrcpy(bs->filename, sizeof(bs->filename), filename);
362 if (flags & BDRV_O_FILE) {
363 drv = find_protocol(filename);
364 if (!drv)
365 return -ENOENT;
366 } else {
367 if (!drv) {
368 drv = find_image_format(filename);
369 if (!drv)
370 return -1;
371 }
372 }
373 bs->drv = drv;
374 bs->opaque = qemu_mallocz(drv->instance_size);
375 if (bs->opaque == NULL && drv->instance_size > 0)
376 return -1;
377 /* Note: for compatibility, we open disk image files as RDWR, and
378 RDONLY as fallback */
379 if (!(flags & BDRV_O_FILE))
380 open_flags = BDRV_O_RDWR;
381 else
382 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
383 ret = drv->bdrv_open(bs, filename, open_flags);
384 if (ret == -EACCES && !(flags & BDRV_O_FILE)) {
385 ret = drv->bdrv_open(bs, filename, BDRV_O_RDONLY);
386 bs->read_only = 1;
387 }
388 if (ret < 0) {
389 qemu_free(bs->opaque);
390 bs->opaque = NULL;
391 bs->drv = NULL;
392 return ret;
393 }
394 if (drv->bdrv_getlength) {
395 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
396 }
397 #ifndef _WIN32
398 if (bs->is_temporary) {
399 unlink(filename);
400 }
401 #endif
402 if (bs->backing_file[0] != '\0') {
403 /* if there is a backing file, use it */
404 bs->backing_hd = bdrv_new("");
405 if (!bs->backing_hd) {
406 fail:
407 bdrv_close(bs);
408 return -ENOMEM;
409 }
410 path_combine(backing_filename, sizeof(backing_filename),
411 filename, bs->backing_file);
412 if (bdrv_open(bs->backing_hd, backing_filename, 0) < 0)
413 goto fail;
414 }
416 /* call the change callback */
417 bs->media_changed = 1;
418 if (bs->change_cb)
419 bs->change_cb(bs->change_opaque);
421 return 0;
422 }
424 void bdrv_close(BlockDriverState *bs)
425 {
426 if (bs->drv) {
427 if (bs->backing_hd)
428 bdrv_delete(bs->backing_hd);
429 bs->drv->bdrv_close(bs);
430 qemu_free(bs->opaque);
431 #ifdef _WIN32
432 if (bs->is_temporary) {
433 unlink(bs->filename);
434 }
435 #endif
436 bs->opaque = NULL;
437 bs->drv = NULL;
439 /* call the change callback */
440 bs->media_changed = 1;
441 if (bs->change_cb)
442 bs->change_cb(bs->change_opaque);
443 }
444 }
446 void bdrv_delete(BlockDriverState *bs)
447 {
448 /* XXX: remove the driver list */
449 bdrv_close(bs);
450 qemu_free(bs);
451 }
453 /* commit COW file into the raw image */
454 int bdrv_commit(BlockDriverState *bs)
455 {
456 BlockDriver *drv = bs->drv;
457 int64_t i, total_sectors;
458 int n, j;
459 unsigned char sector[512];
461 if (!drv)
462 return -ENOMEDIUM;
464 if (bs->read_only) {
465 return -EACCES;
466 }
468 if (!bs->backing_hd) {
469 return -ENOTSUP;
470 }
472 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
473 for (i = 0; i < total_sectors;) {
474 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
475 for(j = 0; j < n; j++) {
476 if (bdrv_read(bs, i, sector, 1) != 0) {
477 return -EIO;
478 }
480 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
481 return -EIO;
482 }
483 i++;
484 }
485 } else {
486 i += n;
487 }
488 }
490 if (drv->bdrv_make_empty)
491 return drv->bdrv_make_empty(bs);
493 return 0;
494 }
496 /* return < 0 if error. See bdrv_write() for the return codes */
497 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
498 uint8_t *buf, int nb_sectors)
499 {
500 BlockDriver *drv = bs->drv;
502 if (!drv)
503 return -ENOMEDIUM;
505 if (sector_num < 0)
506 return -EINVAL;
508 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
509 memcpy(buf, bs->boot_sector_data, 512);
510 sector_num++;
511 nb_sectors--;
512 buf += 512;
513 if (nb_sectors == 0)
514 return 0;
515 }
516 if (drv->bdrv_pread) {
517 int ret, len;
518 len = nb_sectors * 512;
519 ret = drv->bdrv_pread(bs, sector_num * 512, buf, len);
520 if (ret < 0)
521 return ret;
522 else if (ret != len)
523 return -EINVAL;
524 else
525 return 0;
526 } else {
527 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
528 }
529 }
531 /* Return < 0 if error. Important errors are:
532 -EIO generic I/O error (may happen for all errors)
533 -ENOMEDIUM No media inserted.
534 -EINVAL Invalid sector number or nb_sectors
535 -EACCES Trying to write a read-only device
536 */
537 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
538 const uint8_t *buf, int nb_sectors)
539 {
540 BlockDriver *drv = bs->drv;
541 if (!bs->drv)
542 return -ENOMEDIUM;
543 if (bs->read_only)
544 return -EACCES;
545 if (sector_num < 0)
546 return -EINVAL;
547 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
548 memcpy(bs->boot_sector_data, buf, 512);
549 }
550 if (drv->bdrv_pwrite) {
551 int ret, len;
552 len = nb_sectors * 512;
553 ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len);
554 if (ret < 0)
555 return ret;
556 else if (ret != len)
557 return -EIO;
558 else
559 return 0;
560 } else {
561 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
562 }
563 }
565 static int bdrv_pread_em(BlockDriverState *bs, int64_t offset,
566 uint8_t *buf, int count1)
567 {
568 uint8_t tmp_buf[SECTOR_SIZE];
569 int len, nb_sectors, count;
570 int64_t sector_num;
572 count = count1;
573 /* first read to align to sector start */
574 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
575 if (len > count)
576 len = count;
577 sector_num = offset >> SECTOR_BITS;
578 if (len > 0) {
579 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
580 return -EIO;
581 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
582 count -= len;
583 if (count == 0)
584 return count1;
585 sector_num++;
586 buf += len;
587 }
589 /* read the sectors "in place" */
590 nb_sectors = count >> SECTOR_BITS;
591 if (nb_sectors > 0) {
592 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
593 return -EIO;
594 sector_num += nb_sectors;
595 len = nb_sectors << SECTOR_BITS;
596 buf += len;
597 count -= len;
598 }
600 /* add data from the last sector */
601 if (count > 0) {
602 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
603 return -EIO;
604 memcpy(buf, tmp_buf, count);
605 }
606 return count1;
607 }
609 static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset,
610 const uint8_t *buf, int count1)
611 {
612 uint8_t tmp_buf[SECTOR_SIZE];
613 int len, nb_sectors, count;
614 int64_t sector_num;
616 count = count1;
617 /* first write to align to sector start */
618 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
619 if (len > count)
620 len = count;
621 sector_num = offset >> SECTOR_BITS;
622 if (len > 0) {
623 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
624 return -EIO;
625 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
626 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
627 return -EIO;
628 count -= len;
629 if (count == 0)
630 return count1;
631 sector_num++;
632 buf += len;
633 }
635 /* write the sectors "in place" */
636 nb_sectors = count >> SECTOR_BITS;
637 if (nb_sectors > 0) {
638 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
639 return -EIO;
640 sector_num += nb_sectors;
641 len = nb_sectors << SECTOR_BITS;
642 buf += len;
643 count -= len;
644 }
646 /* add data from the last sector */
647 if (count > 0) {
648 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
649 return -EIO;
650 memcpy(tmp_buf, buf, count);
651 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
652 return -EIO;
653 }
654 return count1;
655 }
657 /**
658 * Read with byte offsets (needed only for file protocols)
659 */
660 int bdrv_pread(BlockDriverState *bs, int64_t offset,
661 void *buf1, int count1)
662 {
663 BlockDriver *drv = bs->drv;
665 if (!drv)
666 return -ENOMEDIUM;
667 if (!drv->bdrv_pread)
668 return bdrv_pread_em(bs, offset, buf1, count1);
669 return drv->bdrv_pread(bs, offset, buf1, count1);
670 }
672 /**
673 * Write with byte offsets (needed only for file protocols)
674 */
675 int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
676 const void *buf1, int count1)
677 {
678 BlockDriver *drv = bs->drv;
680 if (!drv)
681 return -ENOMEDIUM;
682 if (!drv->bdrv_pwrite)
683 return bdrv_pwrite_em(bs, offset, buf1, count1);
684 return drv->bdrv_pwrite(bs, offset, buf1, count1);
685 }
687 /**
688 * Truncate file to 'offset' bytes (needed only for file protocols)
689 */
690 int bdrv_truncate(BlockDriverState *bs, int64_t offset)
691 {
692 BlockDriver *drv = bs->drv;
693 if (!drv)
694 return -ENOMEDIUM;
695 if (!drv->bdrv_truncate)
696 return -ENOTSUP;
697 return drv->bdrv_truncate(bs, offset);
698 }
700 /**
701 * Length of a file in bytes. Return < 0 if error or unknown.
702 */
703 int64_t bdrv_getlength(BlockDriverState *bs)
704 {
705 BlockDriver *drv = bs->drv;
706 if (!drv)
707 return -ENOMEDIUM;
708 if (!drv->bdrv_getlength) {
709 /* legacy mode */
710 return bs->total_sectors * SECTOR_SIZE;
711 }
712 return drv->bdrv_getlength(bs);
713 }
715 /* return 0 as number of sectors if no device present or error */
716 void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr)
717 {
718 int64_t length;
719 length = bdrv_getlength(bs);
720 if (length < 0)
721 length = 0;
722 else
723 length = length >> SECTOR_BITS;
724 *nb_sectors_ptr = length;
725 }
727 /* force a given boot sector. */
728 void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size)
729 {
730 bs->boot_sector_enabled = 1;
731 if (size > 512)
732 size = 512;
733 memcpy(bs->boot_sector_data, data, size);
734 memset(bs->boot_sector_data + size, 0, 512 - size);
735 }
737 void bdrv_set_geometry_hint(BlockDriverState *bs,
738 int cyls, int heads, int secs)
739 {
740 bs->cyls = cyls;
741 bs->heads = heads;
742 bs->secs = secs;
743 }
745 void bdrv_set_type_hint(BlockDriverState *bs, int type)
746 {
747 bs->type = type;
748 bs->removable = ((type == BDRV_TYPE_CDROM ||
749 type == BDRV_TYPE_FLOPPY));
750 }
752 void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
753 {
754 bs->translation = translation;
755 }
757 void bdrv_get_geometry_hint(BlockDriverState *bs,
758 int *pcyls, int *pheads, int *psecs)
759 {
760 *pcyls = bs->cyls;
761 *pheads = bs->heads;
762 *psecs = bs->secs;
763 }
765 int bdrv_get_type_hint(BlockDriverState *bs)
766 {
767 return bs->type;
768 }
770 int bdrv_get_translation_hint(BlockDriverState *bs)
771 {
772 return bs->translation;
773 }
775 int bdrv_is_removable(BlockDriverState *bs)
776 {
777 return bs->removable;
778 }
780 int bdrv_is_read_only(BlockDriverState *bs)
781 {
782 return bs->read_only;
783 }
785 /* XXX: no longer used */
786 void bdrv_set_change_cb(BlockDriverState *bs,
787 void (*change_cb)(void *opaque), void *opaque)
788 {
789 bs->change_cb = change_cb;
790 bs->change_opaque = opaque;
791 }
793 int bdrv_is_encrypted(BlockDriverState *bs)
794 {
795 if (bs->backing_hd && bs->backing_hd->encrypted)
796 return 1;
797 return bs->encrypted;
798 }
800 int bdrv_set_key(BlockDriverState *bs, const char *key)
801 {
802 int ret;
803 if (bs->backing_hd && bs->backing_hd->encrypted) {
804 ret = bdrv_set_key(bs->backing_hd, key);
805 if (ret < 0)
806 return ret;
807 if (!bs->encrypted)
808 return 0;
809 }
810 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
811 return -1;
812 return bs->drv->bdrv_set_key(bs, key);
813 }
815 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
816 {
817 if (!bs->drv) {
818 buf[0] = '\0';
819 } else {
820 pstrcpy(buf, buf_size, bs->drv->format_name);
821 }
822 }
824 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
825 void *opaque)
826 {
827 BlockDriver *drv;
829 for (drv = first_drv; drv != NULL; drv = drv->next) {
830 it(opaque, drv->format_name);
831 }
832 }
834 BlockDriverState *bdrv_find(const char *name)
835 {
836 BlockDriverState *bs;
838 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
839 if (!strcmp(name, bs->device_name))
840 return bs;
841 }
842 return NULL;
843 }
845 void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
846 {
847 BlockDriverState *bs;
849 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
850 it(opaque, bs->device_name);
851 }
852 }
854 const char *bdrv_get_device_name(BlockDriverState *bs)
855 {
856 return bs->device_name;
857 }
859 void bdrv_flush(BlockDriverState *bs)
860 {
861 if (bs->drv->bdrv_flush)
862 bs->drv->bdrv_flush(bs);
863 if (bs->backing_hd)
864 bdrv_flush(bs->backing_hd);
865 }
867 void bdrv_info(void)
868 {
869 BlockDriverState *bs;
871 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
872 term_printf("%s:", bs->device_name);
873 term_printf(" type=");
874 switch(bs->type) {
875 case BDRV_TYPE_HD:
876 term_printf("hd");
877 break;
878 case BDRV_TYPE_CDROM:
879 term_printf("cdrom");
880 break;
881 case BDRV_TYPE_FLOPPY:
882 term_printf("floppy");
883 break;
884 }
885 term_printf(" removable=%d", bs->removable);
886 if (bs->removable) {
887 term_printf(" locked=%d", bs->locked);
888 }
889 if (bs->drv) {
890 term_printf(" file=");
891 term_print_filename(bs->filename);
892 if (bs->backing_file[0] != '\0') {
893 term_printf(" backing_file=");
894 term_print_filename(bs->backing_file);
895 }
896 term_printf(" ro=%d", bs->read_only);
897 term_printf(" drv=%s", bs->drv->format_name);
898 if (bs->encrypted)
899 term_printf(" encrypted");
900 } else {
901 term_printf(" [not inserted]");
902 }
903 term_printf("\n");
904 }
905 }
907 void bdrv_get_backing_filename(BlockDriverState *bs,
908 char *filename, int filename_size)
909 {
910 if (!bs->backing_hd) {
911 pstrcpy(filename, filename_size, "");
912 } else {
913 pstrcpy(filename, filename_size, bs->backing_file);
914 }
915 }
917 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
918 const uint8_t *buf, int nb_sectors)
919 {
920 BlockDriver *drv = bs->drv;
921 if (!drv)
922 return -ENOMEDIUM;
923 if (!drv->bdrv_write_compressed)
924 return -ENOTSUP;
925 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
926 }
928 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
929 {
930 BlockDriver *drv = bs->drv;
931 if (!drv)
932 return -ENOMEDIUM;
933 if (!drv->bdrv_get_info)
934 return -ENOTSUP;
935 memset(bdi, 0, sizeof(*bdi));
936 return drv->bdrv_get_info(bs, bdi);
937 }
939 /**************************************************************/
940 /* handling of snapshots */
942 int bdrv_snapshot_create(BlockDriverState *bs,
943 QEMUSnapshotInfo *sn_info)
944 {
945 BlockDriver *drv = bs->drv;
946 if (!drv)
947 return -ENOMEDIUM;
948 if (!drv->bdrv_snapshot_create)
949 return -ENOTSUP;
950 return drv->bdrv_snapshot_create(bs, sn_info);
951 }
953 int bdrv_snapshot_goto(BlockDriverState *bs,
954 const char *snapshot_id)
955 {
956 BlockDriver *drv = bs->drv;
957 if (!drv)
958 return -ENOMEDIUM;
959 if (!drv->bdrv_snapshot_goto)
960 return -ENOTSUP;
961 return drv->bdrv_snapshot_goto(bs, snapshot_id);
962 }
964 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
965 {
966 BlockDriver *drv = bs->drv;
967 if (!drv)
968 return -ENOMEDIUM;
969 if (!drv->bdrv_snapshot_delete)
970 return -ENOTSUP;
971 return drv->bdrv_snapshot_delete(bs, snapshot_id);
972 }
974 int bdrv_snapshot_list(BlockDriverState *bs,
975 QEMUSnapshotInfo **psn_info)
976 {
977 BlockDriver *drv = bs->drv;
978 if (!drv)
979 return -ENOMEDIUM;
980 if (!drv->bdrv_snapshot_list)
981 return -ENOTSUP;
982 return drv->bdrv_snapshot_list(bs, psn_info);
983 }
985 #define NB_SUFFIXES 4
987 char *get_human_readable_size(char *buf, int buf_size, int64_t size)
988 {
989 static const char suffixes[NB_SUFFIXES] = "KMGT";
990 int64_t base;
991 int i;
993 if (size <= 999) {
994 snprintf(buf, buf_size, "%" PRId64, size);
995 } else {
996 base = 1024;
997 for(i = 0; i < NB_SUFFIXES; i++) {
998 if (size < (10 * base)) {
999 snprintf(buf, buf_size, "%0.1f%c",
1000 (double)size / base,
1001 suffixes[i]);
1002 break;
1003 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1004 snprintf(buf, buf_size, "%" PRId64 "%c",
1005 ((size + (base >> 1)) / base),
1006 suffixes[i]);
1007 break;
1009 base = base * 1024;
1012 return buf;
1015 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1017 char buf1[128], date_buf[128], clock_buf[128];
1018 #ifdef _WIN32
1019 struct tm *ptm;
1020 #else
1021 struct tm tm;
1022 #endif
1023 time_t ti;
1024 int64_t secs;
1026 if (!sn) {
1027 snprintf(buf, buf_size,
1028 "%-10s%-20s%7s%20s%15s",
1029 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1030 } else {
1031 ti = sn->date_sec;
1032 #ifdef _WIN32
1033 ptm = localtime(&ti);
1034 strftime(date_buf, sizeof(date_buf),
1035 "%Y-%m-%d %H:%M:%S", ptm);
1036 #else
1037 localtime_r(&ti, &tm);
1038 strftime(date_buf, sizeof(date_buf),
1039 "%Y-%m-%d %H:%M:%S", &tm);
1040 #endif
1041 secs = sn->vm_clock_nsec / 1000000000;
1042 snprintf(clock_buf, sizeof(clock_buf),
1043 "%02d:%02d:%02d.%03d",
1044 (int)(secs / 3600),
1045 (int)((secs / 60) % 60),
1046 (int)(secs % 60),
1047 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1048 snprintf(buf, buf_size,
1049 "%-10s%-20s%7s%20s%15s",
1050 sn->id_str, sn->name,
1051 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1052 date_buf,
1053 clock_buf);
1055 return buf;
1059 /**************************************************************/
1060 /* async I/Os */
1062 BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1063 uint8_t *buf, int nb_sectors,
1064 BlockDriverCompletionFunc *cb, void *opaque)
1066 BlockDriver *drv = bs->drv;
1068 if (!drv)
1069 return NULL;
1071 /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
1072 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1073 memcpy(buf, bs->boot_sector_data, 512);
1074 sector_num++;
1075 nb_sectors--;
1076 buf += 512;
1079 return drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1082 BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1083 const uint8_t *buf, int nb_sectors,
1084 BlockDriverCompletionFunc *cb, void *opaque)
1086 BlockDriver *drv = bs->drv;
1088 if (!drv)
1089 return NULL;
1090 if (bs->read_only)
1091 return NULL;
1092 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1093 memcpy(bs->boot_sector_data, buf, 512);
1096 return drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1099 void bdrv_aio_cancel(BlockDriverAIOCB *acb)
1101 BlockDriver *drv = acb->bs->drv;
1103 drv->bdrv_aio_cancel(acb);
1107 /**************************************************************/
1108 /* async block device emulation */
1110 #ifdef QEMU_TOOL
1111 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1112 int64_t sector_num, uint8_t *buf, int nb_sectors,
1113 BlockDriverCompletionFunc *cb, void *opaque)
1115 int ret;
1116 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1117 cb(opaque, ret);
1118 return NULL;
1121 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1122 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1123 BlockDriverCompletionFunc *cb, void *opaque)
1125 int ret;
1126 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1127 cb(opaque, ret);
1128 return NULL;
1131 static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb)
1134 #else
1135 static void bdrv_aio_bh_cb(void *opaque)
1137 BlockDriverAIOCBSync *acb = opaque;
1138 acb->common.cb(acb->common.opaque, acb->ret);
1139 qemu_aio_release(acb);
1142 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1143 int64_t sector_num, uint8_t *buf, int nb_sectors,
1144 BlockDriverCompletionFunc *cb, void *opaque)
1146 BlockDriverAIOCBSync *acb;
1147 int ret;
1149 acb = qemu_aio_get(bs, cb, opaque);
1150 if (!acb->bh)
1151 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1152 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1153 acb->ret = ret;
1154 qemu_bh_schedule(acb->bh);
1155 return &acb->common;
1158 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1159 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1160 BlockDriverCompletionFunc *cb, void *opaque)
1162 BlockDriverAIOCBSync *acb;
1163 int ret;
1165 acb = qemu_aio_get(bs, cb, opaque);
1166 if (!acb->bh)
1167 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1168 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1169 acb->ret = ret;
1170 qemu_bh_schedule(acb->bh);
1171 return &acb->common;
1174 static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1176 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1177 qemu_bh_cancel(acb->bh);
1178 qemu_aio_release(acb);
1180 #endif /* !QEMU_TOOL */
1182 /**************************************************************/
1183 /* sync block device emulation */
1185 static void bdrv_rw_em_cb(void *opaque, int ret)
1187 *(int *)opaque = ret;
1190 #define NOT_DONE 0x7fffffff
1192 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
1193 uint8_t *buf, int nb_sectors)
1195 int async_ret;
1196 BlockDriverAIOCB *acb;
1198 async_ret = NOT_DONE;
1199 qemu_aio_wait_start();
1200 acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
1201 bdrv_rw_em_cb, &async_ret);
1202 if (acb == NULL) {
1203 qemu_aio_wait_end();
1204 return -1;
1206 while (async_ret == NOT_DONE) {
1207 qemu_aio_wait();
1209 qemu_aio_wait_end();
1210 return async_ret;
1213 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1214 const uint8_t *buf, int nb_sectors)
1216 int async_ret;
1217 BlockDriverAIOCB *acb;
1219 async_ret = NOT_DONE;
1220 qemu_aio_wait_start();
1221 acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
1222 bdrv_rw_em_cb, &async_ret);
1223 if (acb == NULL) {
1224 qemu_aio_wait_end();
1225 return -1;
1227 while (async_ret == NOT_DONE) {
1228 qemu_aio_wait();
1230 qemu_aio_wait_end();
1231 return async_ret;
1234 void bdrv_init(void)
1236 bdrv_register(&bdrv_raw);
1237 bdrv_register(&bdrv_host_device);
1238 #ifdef CONFIG_STUBDOM
1239 bdrv_register(&bdrv_vbd);
1240 #endif
1241 #ifndef _WIN32
1242 bdrv_register(&bdrv_cow);
1243 #endif
1244 bdrv_register(&bdrv_qcow);
1245 bdrv_register(&bdrv_vmdk);
1246 bdrv_register(&bdrv_cloop);
1247 bdrv_register(&bdrv_dmg);
1248 bdrv_register(&bdrv_bochs);
1249 bdrv_register(&bdrv_vpc);
1250 bdrv_register(&bdrv_vvfat);
1251 bdrv_register(&bdrv_qcow2);
1254 void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1255 void *opaque)
1257 BlockDriver *drv;
1258 BlockDriverAIOCB *acb;
1260 drv = bs->drv;
1261 if (drv->free_aiocb) {
1262 acb = drv->free_aiocb;
1263 drv->free_aiocb = acb->next;
1264 } else {
1265 acb = qemu_mallocz(drv->aiocb_size);
1266 if (!acb)
1267 return NULL;
1269 acb->bs = bs;
1270 acb->cb = cb;
1271 acb->opaque = opaque;
1272 return acb;
1275 void qemu_aio_release(void *p)
1277 BlockDriverAIOCB *acb = p;
1278 BlockDriver *drv = acb->bs->drv;
1279 acb->next = drv->free_aiocb;
1280 drv->free_aiocb = acb;
1283 /**************************************************************/
1284 /* removable device support */
1286 /**
1287 * Return TRUE if the media is present
1288 */
1289 int bdrv_is_inserted(BlockDriverState *bs)
1291 BlockDriver *drv = bs->drv;
1292 int ret;
1293 if (!drv)
1294 return 0;
1295 if (!drv->bdrv_is_inserted)
1296 return 1;
1297 ret = drv->bdrv_is_inserted(bs);
1298 return ret;
1301 /**
1302 * Return TRUE if the media changed since the last call to this
1303 * function. It is currently only used for floppy disks
1304 */
1305 int bdrv_media_changed(BlockDriverState *bs)
1307 BlockDriver *drv = bs->drv;
1308 int ret;
1310 if (!drv || !drv->bdrv_media_changed)
1311 ret = -ENOTSUP;
1312 else
1313 ret = drv->bdrv_media_changed(bs);
1314 if (ret == -ENOTSUP)
1315 ret = bs->media_changed;
1316 bs->media_changed = 0;
1317 return ret;
1320 /**
1321 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1322 */
1323 void bdrv_eject(BlockDriverState *bs, int eject_flag)
1325 BlockDriver *drv = bs->drv;
1326 int ret;
1328 if (!drv || !drv->bdrv_eject) {
1329 ret = -ENOTSUP;
1330 } else {
1331 ret = drv->bdrv_eject(bs, eject_flag);
1333 if (ret == -ENOTSUP) {
1334 if (eject_flag)
1335 bdrv_close(bs);
1339 int bdrv_is_locked(BlockDriverState *bs)
1341 return bs->locked;
1344 /**
1345 * Lock or unlock the media (if it is locked, the user won't be able
1346 * to eject it manually).
1347 */
1348 void bdrv_set_locked(BlockDriverState *bs, int locked)
1350 BlockDriver *drv = bs->drv;
1352 bs->locked = locked;
1353 if (drv && drv->bdrv_set_locked) {
1354 drv->bdrv_set_locked(bs, locked);