net: drop packet from tap device if all NICs are down
[qemu-kvm/fedora.git] / block.c
blob8e08f323145cfe395906e5fe8342e0292a27ca67
1 /*
2 * QEMU System Emulator block driver
4 * Copyright (c) 2003 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "config-host.h"
25 #ifdef HOST_BSD
26 /* include native header before sys-queue.h */
27 #include <sys/queue.h>
28 #endif
30 #include "qemu-common.h"
31 #include "monitor.h"
32 #include "block_int.h"
33 #include "osdep.h"
35 #ifdef HOST_BSD
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/ioctl.h>
39 #ifndef __DragonFly__
40 #include <sys/disk.h>
41 #endif
42 #endif
44 #ifdef _WIN32
45 #include <windows.h>
46 #endif
48 #define SECTOR_BITS 9
49 #define SECTOR_SIZE (1 << SECTOR_BITS)
51 typedef struct BlockDriverAIOCBSync {
52 BlockDriverAIOCB common;
53 QEMUBH *bh;
54 int ret;
55 /* vector translation state */
56 QEMUIOVector *qiov;
57 uint8_t *bounce;
58 int is_write;
59 } BlockDriverAIOCBSync;
61 static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
62 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
63 BlockDriverCompletionFunc *cb, void *opaque);
64 static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
65 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
66 BlockDriverCompletionFunc *cb, void *opaque);
67 static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
68 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
69 uint8_t *buf, int nb_sectors);
70 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
71 const uint8_t *buf, int nb_sectors);
73 BlockDriverState *bdrv_first;
75 static BlockDriver *first_drv;
77 int path_is_absolute(const char *path)
79 const char *p;
80 #ifdef _WIN32
81 /* specific case for names like: "\\.\d:" */
82 if (*path == '/' || *path == '\\')
83 return 1;
84 #endif
85 p = strchr(path, ':');
86 if (p)
87 p++;
88 else
89 p = path;
90 #ifdef _WIN32
91 return (*p == '/' || *p == '\\');
92 #else
93 return (*p == '/');
94 #endif
97 /* if filename is absolute, just copy it to dest. Otherwise, build a
98 path to it by considering it is relative to base_path. URL are
99 supported. */
100 void path_combine(char *dest, int dest_size,
101 const char *base_path,
102 const char *filename)
104 const char *p, *p1;
105 int len;
107 if (dest_size <= 0)
108 return;
109 if (path_is_absolute(filename)) {
110 pstrcpy(dest, dest_size, filename);
111 } else {
112 p = strchr(base_path, ':');
113 if (p)
114 p++;
115 else
116 p = base_path;
117 p1 = strrchr(base_path, '/');
118 #ifdef _WIN32
120 const char *p2;
121 p2 = strrchr(base_path, '\\');
122 if (!p1 || p2 > p1)
123 p1 = p2;
125 #endif
126 if (p1)
127 p1++;
128 else
129 p1 = base_path;
130 if (p1 > p)
131 p = p1;
132 len = p - base_path;
133 if (len > dest_size - 1)
134 len = dest_size - 1;
135 memcpy(dest, base_path, len);
136 dest[len] = '\0';
137 pstrcat(dest, dest_size, filename);
142 static void bdrv_register(BlockDriver *bdrv)
144 if (!bdrv->bdrv_aio_readv) {
145 /* add AIO emulation layer */
146 bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
147 bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
148 bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
149 bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
150 } else if (!bdrv->bdrv_read) {
151 /* add synchronous IO emulation layer */
152 bdrv->bdrv_read = bdrv_read_em;
153 bdrv->bdrv_write = bdrv_write_em;
155 aio_pool_init(&bdrv->aio_pool, bdrv->aiocb_size, bdrv->bdrv_aio_cancel);
156 bdrv->next = first_drv;
157 first_drv = bdrv;
160 /* create a new block device (by default it is empty) */
161 BlockDriverState *bdrv_new(const char *device_name)
163 BlockDriverState **pbs, *bs;
165 bs = qemu_mallocz(sizeof(BlockDriverState));
166 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
167 if (device_name[0] != '\0') {
168 /* insert at the end */
169 pbs = &bdrv_first;
170 while (*pbs != NULL)
171 pbs = &(*pbs)->next;
172 *pbs = bs;
174 return bs;
177 BlockDriver *bdrv_find_format(const char *format_name)
179 BlockDriver *drv1;
180 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
181 if (!strcmp(drv1->format_name, format_name))
182 return drv1;
184 return NULL;
187 int bdrv_create2(BlockDriver *drv,
188 const char *filename, int64_t size_in_sectors,
189 const char *backing_file, const char *backing_format,
190 int flags)
192 if (drv->bdrv_create2)
193 return drv->bdrv_create2(filename, size_in_sectors, backing_file,
194 backing_format, flags);
195 if (drv->bdrv_create)
196 return drv->bdrv_create(filename, size_in_sectors, backing_file,
197 flags);
198 return -ENOTSUP;
201 int bdrv_create(BlockDriver *drv,
202 const char *filename, int64_t size_in_sectors,
203 const char *backing_file, int flags)
205 if (!drv->bdrv_create)
206 return -ENOTSUP;
207 return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
210 #ifdef _WIN32
211 void get_tmp_filename(char *filename, int size)
213 char temp_dir[MAX_PATH];
215 GetTempPath(MAX_PATH, temp_dir);
216 GetTempFileName(temp_dir, "qem", 0, filename);
218 #else
219 void get_tmp_filename(char *filename, int size)
221 int fd;
222 const char *tmpdir;
223 /* XXX: race condition possible */
224 tmpdir = getenv("TMPDIR");
225 if (!tmpdir)
226 tmpdir = "/tmp";
227 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
228 fd = mkstemp(filename);
229 close(fd);
231 #endif
233 #ifdef _WIN32
234 static int is_windows_drive_prefix(const char *filename)
236 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
237 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
238 filename[1] == ':');
241 static int is_windows_drive(const char *filename)
243 if (is_windows_drive_prefix(filename) &&
244 filename[2] == '\0')
245 return 1;
246 if (strstart(filename, "\\\\.\\", NULL) ||
247 strstart(filename, "//./", NULL))
248 return 1;
249 return 0;
251 #endif
253 static BlockDriver *find_protocol(const char *filename)
255 BlockDriver *drv1;
256 char protocol[128];
257 int len;
258 const char *p;
260 #ifdef _WIN32
261 if (is_windows_drive(filename) ||
262 is_windows_drive_prefix(filename))
263 return &bdrv_raw;
264 #endif
265 p = strchr(filename, ':');
266 if (!p)
267 return &bdrv_raw;
268 len = p - filename;
269 if (len > sizeof(protocol) - 1)
270 len = sizeof(protocol) - 1;
271 memcpy(protocol, filename, len);
272 protocol[len] = '\0';
273 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
274 if (drv1->protocol_name &&
275 !strcmp(drv1->protocol_name, protocol))
276 return drv1;
278 return NULL;
281 /* XXX: force raw format if block or character device ? It would
282 simplify the BSD case */
283 static BlockDriver *find_image_format(const char *filename)
285 int ret, score, score_max;
286 BlockDriver *drv1, *drv;
287 uint8_t buf[2048];
288 BlockDriverState *bs;
290 /* detect host devices. By convention, /dev/cdrom[N] is always
291 recognized as a host CDROM */
292 if (strstart(filename, "/dev/cdrom", NULL))
293 return &bdrv_host_device;
294 #ifdef _WIN32
295 if (is_windows_drive(filename))
296 return &bdrv_host_device;
297 #else
299 struct stat st;
300 if (stat(filename, &st) >= 0 &&
301 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
302 return &bdrv_host_device;
305 #endif
307 drv = find_protocol(filename);
308 /* no need to test disk image formats for vvfat */
309 if (drv == &bdrv_vvfat)
310 return drv;
312 ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
313 if (ret < 0)
314 return NULL;
315 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
316 bdrv_delete(bs);
317 if (ret < 0) {
318 return NULL;
321 score_max = 0;
322 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
323 if (drv1->bdrv_probe) {
324 score = drv1->bdrv_probe(buf, ret, filename);
325 if (score > score_max) {
326 score_max = score;
327 drv = drv1;
331 return drv;
334 int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
336 BlockDriverState *bs;
337 int ret;
339 bs = bdrv_new("");
340 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
341 if (ret < 0) {
342 bdrv_delete(bs);
343 return ret;
345 bs->growable = 1;
346 *pbs = bs;
347 return 0;
350 int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
352 return bdrv_open2(bs, filename, flags, NULL);
355 int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
356 BlockDriver *drv)
358 int ret, open_flags;
359 char tmp_filename[PATH_MAX];
360 char backing_filename[PATH_MAX];
362 bs->read_only = 0;
363 bs->is_temporary = 0;
364 bs->encrypted = 0;
365 bs->valid_key = 0;
367 if (flags & BDRV_O_SNAPSHOT) {
368 BlockDriverState *bs1;
369 int64_t total_size;
370 int is_protocol = 0;
372 /* if snapshot, we create a temporary backing file and open it
373 instead of opening 'filename' directly */
375 /* if there is a backing file, use it */
376 bs1 = bdrv_new("");
377 ret = bdrv_open2(bs1, filename, 0, drv);
378 if (ret < 0) {
379 bdrv_delete(bs1);
380 return ret;
382 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
384 if (bs1->drv && bs1->drv->protocol_name)
385 is_protocol = 1;
387 bdrv_delete(bs1);
389 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
391 /* Real path is meaningless for protocols */
392 if (is_protocol)
393 snprintf(backing_filename, sizeof(backing_filename),
394 "%s", filename);
395 else
396 realpath(filename, backing_filename);
398 ret = bdrv_create2(&bdrv_qcow2, tmp_filename,
399 total_size, backing_filename,
400 (drv ? drv->format_name : NULL), 0);
401 if (ret < 0) {
402 return ret;
404 filename = tmp_filename;
405 drv = &bdrv_qcow2;
406 bs->is_temporary = 1;
409 pstrcpy(bs->filename, sizeof(bs->filename), filename);
410 if (flags & BDRV_O_FILE) {
411 drv = find_protocol(filename);
412 } else if (!drv) {
413 drv = find_image_format(filename);
415 if (!drv) {
416 ret = -ENOENT;
417 goto unlink_and_fail;
419 bs->drv = drv;
420 bs->opaque = qemu_mallocz(drv->instance_size);
421 /* Note: for compatibility, we open disk image files as RDWR, and
422 RDONLY as fallback */
423 if (!(flags & BDRV_O_FILE))
424 open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK);
425 else
426 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
427 ret = drv->bdrv_open(bs, filename, open_flags);
428 if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
429 ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
430 bs->read_only = 1;
432 if (ret < 0) {
433 qemu_free(bs->opaque);
434 bs->opaque = NULL;
435 bs->drv = NULL;
436 unlink_and_fail:
437 if (bs->is_temporary)
438 unlink(filename);
439 return ret;
441 if (drv->bdrv_getlength) {
442 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
444 #ifndef _WIN32
445 if (bs->is_temporary) {
446 unlink(filename);
448 #endif
449 if (bs->backing_file[0] != '\0') {
450 /* if there is a backing file, use it */
451 BlockDriver *back_drv = NULL;
452 bs->backing_hd = bdrv_new("");
453 path_combine(backing_filename, sizeof(backing_filename),
454 filename, bs->backing_file);
455 if (bs->backing_format[0] != '\0')
456 back_drv = bdrv_find_format(bs->backing_format);
457 ret = bdrv_open2(bs->backing_hd, backing_filename, open_flags,
458 back_drv);
459 if (ret < 0) {
460 bdrv_close(bs);
461 return ret;
465 if (!bdrv_key_required(bs)) {
466 /* call the change callback */
467 bs->media_changed = 1;
468 if (bs->change_cb)
469 bs->change_cb(bs->change_opaque);
471 return 0;
474 void bdrv_close(BlockDriverState *bs)
476 if (bs->drv) {
477 if (bs->backing_hd)
478 bdrv_delete(bs->backing_hd);
479 bs->drv->bdrv_close(bs);
480 qemu_free(bs->opaque);
481 #ifdef _WIN32
482 if (bs->is_temporary) {
483 unlink(bs->filename);
485 #endif
486 bs->opaque = NULL;
487 bs->drv = NULL;
489 /* call the change callback */
490 bs->media_changed = 1;
491 if (bs->change_cb)
492 bs->change_cb(bs->change_opaque);
496 void bdrv_delete(BlockDriverState *bs)
498 BlockDriverState **pbs;
500 pbs = &bdrv_first;
501 while (*pbs != bs && *pbs != NULL)
502 pbs = &(*pbs)->next;
503 if (*pbs == bs)
504 *pbs = bs->next;
506 bdrv_close(bs);
507 qemu_free(bs);
511 * Run consistency checks on an image
513 * Returns the number of errors or -errno when an internal error occurs
515 int bdrv_check(BlockDriverState *bs)
517 if (bs->drv->bdrv_check == NULL) {
518 return -ENOTSUP;
521 return bs->drv->bdrv_check(bs);
524 /* commit COW file into the raw image */
525 int bdrv_commit(BlockDriverState *bs)
527 BlockDriver *drv = bs->drv;
528 int64_t i, total_sectors;
529 int n, j;
530 unsigned char sector[512];
532 if (!drv)
533 return -ENOMEDIUM;
535 if (bs->read_only) {
536 return -EACCES;
539 if (!bs->backing_hd) {
540 return -ENOTSUP;
543 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
544 for (i = 0; i < total_sectors;) {
545 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
546 for(j = 0; j < n; j++) {
547 if (bdrv_read(bs, i, sector, 1) != 0) {
548 return -EIO;
551 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
552 return -EIO;
554 i++;
556 } else {
557 i += n;
561 if (drv->bdrv_make_empty)
562 return drv->bdrv_make_empty(bs);
564 return 0;
567 static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
568 size_t size)
570 int64_t len;
572 if (!bdrv_is_inserted(bs))
573 return -ENOMEDIUM;
575 if (bs->growable)
576 return 0;
578 len = bdrv_getlength(bs);
580 if ((offset + size) > len)
581 return -EIO;
583 return 0;
586 static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
587 int nb_sectors)
589 return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512);
592 /* return < 0 if error. See bdrv_write() for the return codes */
593 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
594 uint8_t *buf, int nb_sectors)
596 BlockDriver *drv = bs->drv;
598 if (!drv)
599 return -ENOMEDIUM;
600 if (bdrv_check_request(bs, sector_num, nb_sectors))
601 return -EIO;
603 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
606 /* Return < 0 if error. Important errors are:
607 -EIO generic I/O error (may happen for all errors)
608 -ENOMEDIUM No media inserted.
609 -EINVAL Invalid sector number or nb_sectors
610 -EACCES Trying to write a read-only device
612 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
613 const uint8_t *buf, int nb_sectors)
615 BlockDriver *drv = bs->drv;
616 if (!bs->drv)
617 return -ENOMEDIUM;
618 if (bs->read_only)
619 return -EACCES;
620 if (bdrv_check_request(bs, sector_num, nb_sectors))
621 return -EIO;
623 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
626 int bdrv_pread(BlockDriverState *bs, int64_t offset,
627 void *buf, int count1)
629 uint8_t tmp_buf[SECTOR_SIZE];
630 int len, nb_sectors, count;
631 int64_t sector_num;
633 count = count1;
634 /* first read to align to sector start */
635 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
636 if (len > count)
637 len = count;
638 sector_num = offset >> SECTOR_BITS;
639 if (len > 0) {
640 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
641 return -EIO;
642 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
643 count -= len;
644 if (count == 0)
645 return count1;
646 sector_num++;
647 buf += len;
650 /* read the sectors "in place" */
651 nb_sectors = count >> SECTOR_BITS;
652 if (nb_sectors > 0) {
653 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
654 return -EIO;
655 sector_num += nb_sectors;
656 len = nb_sectors << SECTOR_BITS;
657 buf += len;
658 count -= len;
661 /* add data from the last sector */
662 if (count > 0) {
663 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
664 return -EIO;
665 memcpy(buf, tmp_buf, count);
667 return count1;
670 int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
671 const void *buf, int count1)
673 uint8_t tmp_buf[SECTOR_SIZE];
674 int len, nb_sectors, count;
675 int64_t sector_num;
677 count = count1;
678 /* first write to align to sector start */
679 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
680 if (len > count)
681 len = count;
682 sector_num = offset >> SECTOR_BITS;
683 if (len > 0) {
684 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
685 return -EIO;
686 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
687 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
688 return -EIO;
689 count -= len;
690 if (count == 0)
691 return count1;
692 sector_num++;
693 buf += len;
696 /* write the sectors "in place" */
697 nb_sectors = count >> SECTOR_BITS;
698 if (nb_sectors > 0) {
699 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
700 return -EIO;
701 sector_num += nb_sectors;
702 len = nb_sectors << SECTOR_BITS;
703 buf += len;
704 count -= len;
707 /* add data from the last sector */
708 if (count > 0) {
709 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
710 return -EIO;
711 memcpy(tmp_buf, buf, count);
712 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
713 return -EIO;
715 return count1;
719 * Truncate file to 'offset' bytes (needed only for file protocols)
721 int bdrv_truncate(BlockDriverState *bs, int64_t offset)
723 BlockDriver *drv = bs->drv;
724 if (!drv)
725 return -ENOMEDIUM;
726 if (!drv->bdrv_truncate)
727 return -ENOTSUP;
728 return drv->bdrv_truncate(bs, offset);
732 * Length of a file in bytes. Return < 0 if error or unknown.
734 int64_t bdrv_getlength(BlockDriverState *bs)
736 BlockDriver *drv = bs->drv;
737 if (!drv)
738 return -ENOMEDIUM;
739 if (!drv->bdrv_getlength) {
740 /* legacy mode */
741 return bs->total_sectors * SECTOR_SIZE;
743 return drv->bdrv_getlength(bs);
746 /* return 0 as number of sectors if no device present or error */
747 void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
749 int64_t length;
750 length = bdrv_getlength(bs);
751 if (length < 0)
752 length = 0;
753 else
754 length = length >> SECTOR_BITS;
755 *nb_sectors_ptr = length;
758 struct partition {
759 uint8_t boot_ind; /* 0x80 - active */
760 uint8_t head; /* starting head */
761 uint8_t sector; /* starting sector */
762 uint8_t cyl; /* starting cylinder */
763 uint8_t sys_ind; /* What partition type */
764 uint8_t end_head; /* end head */
765 uint8_t end_sector; /* end sector */
766 uint8_t end_cyl; /* end cylinder */
767 uint32_t start_sect; /* starting sector counting from 0 */
768 uint32_t nr_sects; /* nr of sectors in partition */
769 } __attribute__((packed));
771 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
772 static int guess_disk_lchs(BlockDriverState *bs,
773 int *pcylinders, int *pheads, int *psectors)
775 uint8_t *buf;
776 int ret, i, heads, sectors, cylinders;
777 struct partition *p;
778 uint32_t nr_sects;
779 uint64_t nb_sectors;
781 buf = qemu_memalign(512, 512);
782 if (buf == NULL)
783 return -1;
785 bdrv_get_geometry(bs, &nb_sectors);
787 ret = bdrv_read(bs, 0, buf, 1);
788 if (ret < 0)
789 return -1;
790 /* test msdos magic */
791 if (buf[510] != 0x55 || buf[511] != 0xaa) {
792 qemu_free(buf);
793 return -1;
795 for(i = 0; i < 4; i++) {
796 p = ((struct partition *)(buf + 0x1be)) + i;
797 nr_sects = le32_to_cpu(p->nr_sects);
798 if (nr_sects && p->end_head) {
799 /* We make the assumption that the partition terminates on
800 a cylinder boundary */
801 heads = p->end_head + 1;
802 sectors = p->end_sector & 63;
803 if (sectors == 0)
804 continue;
805 cylinders = nb_sectors / (heads * sectors);
806 if (cylinders < 1 || cylinders > 16383)
807 continue;
808 *pheads = heads;
809 *psectors = sectors;
810 *pcylinders = cylinders;
811 #if 0
812 printf("guessed geometry: LCHS=%d %d %d\n",
813 cylinders, heads, sectors);
814 #endif
815 qemu_free(buf);
816 return 0;
819 qemu_free(buf);
820 return -1;
823 void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
825 int translation, lba_detected = 0;
826 int cylinders, heads, secs;
827 uint64_t nb_sectors;
829 /* if a geometry hint is available, use it */
830 bdrv_get_geometry(bs, &nb_sectors);
831 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
832 translation = bdrv_get_translation_hint(bs);
833 if (cylinders != 0) {
834 *pcyls = cylinders;
835 *pheads = heads;
836 *psecs = secs;
837 } else {
838 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
839 if (heads > 16) {
840 /* if heads > 16, it means that a BIOS LBA
841 translation was active, so the default
842 hardware geometry is OK */
843 lba_detected = 1;
844 goto default_geometry;
845 } else {
846 *pcyls = cylinders;
847 *pheads = heads;
848 *psecs = secs;
849 /* disable any translation to be in sync with
850 the logical geometry */
851 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
852 bdrv_set_translation_hint(bs,
853 BIOS_ATA_TRANSLATION_NONE);
856 } else {
857 default_geometry:
858 /* if no geometry, use a standard physical disk geometry */
859 cylinders = nb_sectors / (16 * 63);
861 if (cylinders > 16383)
862 cylinders = 16383;
863 else if (cylinders < 2)
864 cylinders = 2;
865 *pcyls = cylinders;
866 *pheads = 16;
867 *psecs = 63;
868 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
869 if ((*pcyls * *pheads) <= 131072) {
870 bdrv_set_translation_hint(bs,
871 BIOS_ATA_TRANSLATION_LARGE);
872 } else {
873 bdrv_set_translation_hint(bs,
874 BIOS_ATA_TRANSLATION_LBA);
878 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
882 void bdrv_set_geometry_hint(BlockDriverState *bs,
883 int cyls, int heads, int secs)
885 bs->cyls = cyls;
886 bs->heads = heads;
887 bs->secs = secs;
890 void bdrv_set_type_hint(BlockDriverState *bs, int type)
892 bs->type = type;
893 bs->removable = ((type == BDRV_TYPE_CDROM ||
894 type == BDRV_TYPE_FLOPPY));
897 void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
899 bs->translation = translation;
902 void bdrv_get_geometry_hint(BlockDriverState *bs,
903 int *pcyls, int *pheads, int *psecs)
905 *pcyls = bs->cyls;
906 *pheads = bs->heads;
907 *psecs = bs->secs;
910 int bdrv_get_type_hint(BlockDriverState *bs)
912 return bs->type;
915 int bdrv_get_translation_hint(BlockDriverState *bs)
917 return bs->translation;
920 int bdrv_is_removable(BlockDriverState *bs)
922 return bs->removable;
925 int bdrv_is_read_only(BlockDriverState *bs)
927 return bs->read_only;
930 int bdrv_is_sg(BlockDriverState *bs)
932 return bs->sg;
935 /* XXX: no longer used */
936 void bdrv_set_change_cb(BlockDriverState *bs,
937 void (*change_cb)(void *opaque), void *opaque)
939 bs->change_cb = change_cb;
940 bs->change_opaque = opaque;
943 int bdrv_is_encrypted(BlockDriverState *bs)
945 if (bs->backing_hd && bs->backing_hd->encrypted)
946 return 1;
947 return bs->encrypted;
950 int bdrv_key_required(BlockDriverState *bs)
952 BlockDriverState *backing_hd = bs->backing_hd;
954 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
955 return 1;
956 return (bs->encrypted && !bs->valid_key);
959 int bdrv_set_key(BlockDriverState *bs, const char *key)
961 int ret;
962 if (bs->backing_hd && bs->backing_hd->encrypted) {
963 ret = bdrv_set_key(bs->backing_hd, key);
964 if (ret < 0)
965 return ret;
966 if (!bs->encrypted)
967 return 0;
969 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
970 return -1;
971 ret = bs->drv->bdrv_set_key(bs, key);
972 if (ret < 0) {
973 bs->valid_key = 0;
974 } else if (!bs->valid_key) {
975 bs->valid_key = 1;
976 /* call the change callback now, we skipped it on open */
977 bs->media_changed = 1;
978 if (bs->change_cb)
979 bs->change_cb(bs->change_opaque);
981 return ret;
984 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
986 if (!bs->drv) {
987 buf[0] = '\0';
988 } else {
989 pstrcpy(buf, buf_size, bs->drv->format_name);
993 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
994 void *opaque)
996 BlockDriver *drv;
998 for (drv = first_drv; drv != NULL; drv = drv->next) {
999 it(opaque, drv->format_name);
1003 BlockDriverState *bdrv_find(const char *name)
1005 BlockDriverState *bs;
1007 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1008 if (!strcmp(name, bs->device_name))
1009 return bs;
1011 return NULL;
1014 void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
1016 BlockDriverState *bs;
1018 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1019 it(opaque, bs);
1023 const char *bdrv_get_device_name(BlockDriverState *bs)
1025 return bs->device_name;
1028 void bdrv_flush(BlockDriverState *bs)
1030 if (!bs->drv)
1031 return;
1032 if (bs->drv->bdrv_flush)
1033 bs->drv->bdrv_flush(bs);
1034 if (bs->backing_hd)
1035 bdrv_flush(bs->backing_hd);
1038 void bdrv_flush_all(void)
1040 BlockDriverState *bs;
1042 for (bs = bdrv_first; bs != NULL; bs = bs->next)
1043 if (bs->drv && !bdrv_is_read_only(bs) &&
1044 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1045 bdrv_flush(bs);
1049 * Returns true iff the specified sector is present in the disk image. Drivers
1050 * not implementing the functionality are assumed to not support backing files,
1051 * hence all their sectors are reported as allocated.
1053 * 'pnum' is set to the number of sectors (including and immediately following
1054 * the specified sector) that are known to be in the same
1055 * allocated/unallocated state.
1057 * 'nb_sectors' is the max value 'pnum' should be set to.
1059 int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1060 int *pnum)
1062 int64_t n;
1063 if (!bs->drv->bdrv_is_allocated) {
1064 if (sector_num >= bs->total_sectors) {
1065 *pnum = 0;
1066 return 0;
1068 n = bs->total_sectors - sector_num;
1069 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1070 return 1;
1072 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1075 void bdrv_info(Monitor *mon)
1077 BlockDriverState *bs;
1079 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1080 monitor_printf(mon, "%s:", bs->device_name);
1081 monitor_printf(mon, " type=");
1082 switch(bs->type) {
1083 case BDRV_TYPE_HD:
1084 monitor_printf(mon, "hd");
1085 break;
1086 case BDRV_TYPE_CDROM:
1087 monitor_printf(mon, "cdrom");
1088 break;
1089 case BDRV_TYPE_FLOPPY:
1090 monitor_printf(mon, "floppy");
1091 break;
1093 monitor_printf(mon, " removable=%d", bs->removable);
1094 if (bs->removable) {
1095 monitor_printf(mon, " locked=%d", bs->locked);
1097 if (bs->drv) {
1098 monitor_printf(mon, " file=");
1099 monitor_print_filename(mon, bs->filename);
1100 if (bs->backing_file[0] != '\0') {
1101 monitor_printf(mon, " backing_file=");
1102 monitor_print_filename(mon, bs->backing_file);
1104 monitor_printf(mon, " ro=%d", bs->read_only);
1105 monitor_printf(mon, " drv=%s", bs->drv->format_name);
1106 monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs));
1107 } else {
1108 monitor_printf(mon, " [not inserted]");
1110 monitor_printf(mon, "\n");
1114 /* The "info blockstats" command. */
1115 void bdrv_info_stats(Monitor *mon)
1117 BlockDriverState *bs;
1119 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1120 monitor_printf(mon, "%s:"
1121 " rd_bytes=%" PRIu64
1122 " wr_bytes=%" PRIu64
1123 " rd_operations=%" PRIu64
1124 " wr_operations=%" PRIu64
1125 "\n",
1126 bs->device_name,
1127 bs->rd_bytes, bs->wr_bytes,
1128 bs->rd_ops, bs->wr_ops);
1132 const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1134 if (bs->backing_hd && bs->backing_hd->encrypted)
1135 return bs->backing_file;
1136 else if (bs->encrypted)
1137 return bs->filename;
1138 else
1139 return NULL;
1142 void bdrv_get_backing_filename(BlockDriverState *bs,
1143 char *filename, int filename_size)
1145 if (!bs->backing_hd) {
1146 pstrcpy(filename, filename_size, "");
1147 } else {
1148 pstrcpy(filename, filename_size, bs->backing_file);
1152 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1153 const uint8_t *buf, int nb_sectors)
1155 BlockDriver *drv = bs->drv;
1156 if (!drv)
1157 return -ENOMEDIUM;
1158 if (!drv->bdrv_write_compressed)
1159 return -ENOTSUP;
1160 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1163 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1165 BlockDriver *drv = bs->drv;
1166 if (!drv)
1167 return -ENOMEDIUM;
1168 if (!drv->bdrv_get_info)
1169 return -ENOTSUP;
1170 memset(bdi, 0, sizeof(*bdi));
1171 return drv->bdrv_get_info(bs, bdi);
1174 int bdrv_put_buffer(BlockDriverState *bs, const uint8_t *buf, int64_t pos, int size)
1176 BlockDriver *drv = bs->drv;
1177 if (!drv)
1178 return -ENOMEDIUM;
1179 if (!drv->bdrv_put_buffer)
1180 return -ENOTSUP;
1181 return drv->bdrv_put_buffer(bs, buf, pos, size);
1184 int bdrv_get_buffer(BlockDriverState *bs, uint8_t *buf, int64_t pos, int size)
1186 BlockDriver *drv = bs->drv;
1187 if (!drv)
1188 return -ENOMEDIUM;
1189 if (!drv->bdrv_get_buffer)
1190 return -ENOTSUP;
1191 return drv->bdrv_get_buffer(bs, buf, pos, size);
1194 /**************************************************************/
1195 /* handling of snapshots */
1197 int bdrv_snapshot_create(BlockDriverState *bs,
1198 QEMUSnapshotInfo *sn_info)
1200 BlockDriver *drv = bs->drv;
1201 if (!drv)
1202 return -ENOMEDIUM;
1203 if (!drv->bdrv_snapshot_create)
1204 return -ENOTSUP;
1205 return drv->bdrv_snapshot_create(bs, sn_info);
1208 int bdrv_snapshot_goto(BlockDriverState *bs,
1209 const char *snapshot_id)
1211 BlockDriver *drv = bs->drv;
1212 if (!drv)
1213 return -ENOMEDIUM;
1214 if (!drv->bdrv_snapshot_goto)
1215 return -ENOTSUP;
1216 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1219 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1221 BlockDriver *drv = bs->drv;
1222 if (!drv)
1223 return -ENOMEDIUM;
1224 if (!drv->bdrv_snapshot_delete)
1225 return -ENOTSUP;
1226 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1229 int bdrv_snapshot_list(BlockDriverState *bs,
1230 QEMUSnapshotInfo **psn_info)
1232 BlockDriver *drv = bs->drv;
1233 if (!drv)
1234 return -ENOMEDIUM;
1235 if (!drv->bdrv_snapshot_list)
1236 return -ENOTSUP;
1237 return drv->bdrv_snapshot_list(bs, psn_info);
1240 #define NB_SUFFIXES 4
1242 char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1244 static const char suffixes[NB_SUFFIXES] = "KMGT";
1245 int64_t base;
1246 int i;
1248 if (size <= 999) {
1249 snprintf(buf, buf_size, "%" PRId64, size);
1250 } else {
1251 base = 1024;
1252 for(i = 0; i < NB_SUFFIXES; i++) {
1253 if (size < (10 * base)) {
1254 snprintf(buf, buf_size, "%0.1f%c",
1255 (double)size / base,
1256 suffixes[i]);
1257 break;
1258 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1259 snprintf(buf, buf_size, "%" PRId64 "%c",
1260 ((size + (base >> 1)) / base),
1261 suffixes[i]);
1262 break;
1264 base = base * 1024;
1267 return buf;
1270 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1272 char buf1[128], date_buf[128], clock_buf[128];
1273 #ifdef _WIN32
1274 struct tm *ptm;
1275 #else
1276 struct tm tm;
1277 #endif
1278 time_t ti;
1279 int64_t secs;
1281 if (!sn) {
1282 snprintf(buf, buf_size,
1283 "%-10s%-20s%7s%20s%15s",
1284 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1285 } else {
1286 ti = sn->date_sec;
1287 #ifdef _WIN32
1288 ptm = localtime(&ti);
1289 strftime(date_buf, sizeof(date_buf),
1290 "%Y-%m-%d %H:%M:%S", ptm);
1291 #else
1292 localtime_r(&ti, &tm);
1293 strftime(date_buf, sizeof(date_buf),
1294 "%Y-%m-%d %H:%M:%S", &tm);
1295 #endif
1296 secs = sn->vm_clock_nsec / 1000000000;
1297 snprintf(clock_buf, sizeof(clock_buf),
1298 "%02d:%02d:%02d.%03d",
1299 (int)(secs / 3600),
1300 (int)((secs / 60) % 60),
1301 (int)(secs % 60),
1302 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1303 snprintf(buf, buf_size,
1304 "%-10s%-20s%7s%20s%15s",
1305 sn->id_str, sn->name,
1306 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1307 date_buf,
1308 clock_buf);
1310 return buf;
1314 /**************************************************************/
1315 /* async I/Os */
1317 BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
1318 QEMUIOVector *qiov, int nb_sectors,
1319 BlockDriverCompletionFunc *cb, void *opaque)
1321 BlockDriver *drv = bs->drv;
1322 BlockDriverAIOCB *ret;
1324 if (!drv)
1325 return NULL;
1326 if (bdrv_check_request(bs, sector_num, nb_sectors))
1327 return NULL;
1329 ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
1330 cb, opaque);
1332 if (ret) {
1333 /* Update stats even though technically transfer has not happened. */
1334 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1335 bs->rd_ops ++;
1338 return ret;
1341 BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1342 QEMUIOVector *qiov, int nb_sectors,
1343 BlockDriverCompletionFunc *cb, void *opaque)
1345 BlockDriver *drv = bs->drv;
1346 BlockDriverAIOCB *ret;
1348 if (!drv)
1349 return NULL;
1350 if (bs->read_only)
1351 return NULL;
1352 if (bdrv_check_request(bs, sector_num, nb_sectors))
1353 return NULL;
1355 ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
1356 cb, opaque);
1358 if (ret) {
1359 /* Update stats even though technically transfer has not happened. */
1360 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1361 bs->wr_ops ++;
1364 return ret;
1367 void bdrv_aio_cancel(BlockDriverAIOCB *acb)
1369 acb->pool->cancel(acb);
1373 /**************************************************************/
1374 /* async block device emulation */
1376 static void bdrv_aio_bh_cb(void *opaque)
1378 BlockDriverAIOCBSync *acb = opaque;
1380 if (!acb->is_write)
1381 qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
1382 qemu_vfree(acb->bounce);
1383 acb->common.cb(acb->common.opaque, acb->ret);
1385 qemu_aio_release(acb);
1388 static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
1389 int64_t sector_num,
1390 QEMUIOVector *qiov,
1391 int nb_sectors,
1392 BlockDriverCompletionFunc *cb,
1393 void *opaque,
1394 int is_write)
1397 BlockDriverAIOCBSync *acb;
1399 acb = qemu_aio_get(bs, cb, opaque);
1400 acb->is_write = is_write;
1401 acb->qiov = qiov;
1402 acb->bounce = qemu_memalign(512, qiov->size);
1404 if (!acb->bh)
1405 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1407 if (is_write) {
1408 qemu_iovec_to_buffer(acb->qiov, acb->bounce);
1409 acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
1410 } else {
1411 acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
1414 qemu_bh_schedule(acb->bh);
1416 return &acb->common;
1419 static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
1420 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
1421 BlockDriverCompletionFunc *cb, void *opaque)
1423 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
1426 static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
1427 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
1428 BlockDriverCompletionFunc *cb, void *opaque)
1430 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
1434 static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1436 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1437 qemu_bh_cancel(acb->bh);
1438 qemu_aio_release(acb);
1441 /**************************************************************/
1442 /* sync block device emulation */
1444 static void bdrv_rw_em_cb(void *opaque, int ret)
1446 *(int *)opaque = ret;
1449 #define NOT_DONE 0x7fffffff
1451 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
1452 uint8_t *buf, int nb_sectors)
1454 int async_ret;
1455 BlockDriverAIOCB *acb;
1456 struct iovec iov;
1457 QEMUIOVector qiov;
1459 async_ret = NOT_DONE;
1460 iov.iov_base = (void *)buf;
1461 iov.iov_len = nb_sectors * 512;
1462 qemu_iovec_init_external(&qiov, &iov, 1);
1463 acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
1464 bdrv_rw_em_cb, &async_ret);
1465 if (acb == NULL)
1466 return -1;
1468 while (async_ret == NOT_DONE) {
1469 qemu_aio_wait();
1472 return async_ret;
1475 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1476 const uint8_t *buf, int nb_sectors)
1478 int async_ret;
1479 BlockDriverAIOCB *acb;
1480 struct iovec iov;
1481 QEMUIOVector qiov;
1483 async_ret = NOT_DONE;
1484 iov.iov_base = (void *)buf;
1485 iov.iov_len = nb_sectors * 512;
1486 qemu_iovec_init_external(&qiov, &iov, 1);
1487 acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
1488 bdrv_rw_em_cb, &async_ret);
1489 if (acb == NULL)
1490 return -1;
1491 while (async_ret == NOT_DONE) {
1492 qemu_aio_wait();
1494 return async_ret;
1497 void bdrv_init(void)
1499 bdrv_register(&bdrv_raw);
1500 bdrv_register(&bdrv_host_device);
1501 #ifndef _WIN32
1502 bdrv_register(&bdrv_cow);
1503 #endif
1504 bdrv_register(&bdrv_qcow);
1505 bdrv_register(&bdrv_vmdk);
1506 bdrv_register(&bdrv_cloop);
1507 bdrv_register(&bdrv_dmg);
1508 bdrv_register(&bdrv_bochs);
1509 bdrv_register(&bdrv_vpc);
1510 bdrv_register(&bdrv_vvfat);
1511 bdrv_register(&bdrv_qcow2);
1512 bdrv_register(&bdrv_parallels);
1513 bdrv_register(&bdrv_nbd);
1516 void aio_pool_init(AIOPool *pool, int aiocb_size,
1517 void (*cancel)(BlockDriverAIOCB *acb))
1519 pool->aiocb_size = aiocb_size;
1520 pool->cancel = cancel;
1521 pool->free_aiocb = NULL;
1524 void *qemu_aio_get_pool(AIOPool *pool, BlockDriverState *bs,
1525 BlockDriverCompletionFunc *cb, void *opaque)
1527 BlockDriverAIOCB *acb;
1529 if (pool->free_aiocb) {
1530 acb = pool->free_aiocb;
1531 pool->free_aiocb = acb->next;
1532 } else {
1533 acb = qemu_mallocz(pool->aiocb_size);
1534 acb->pool = pool;
1536 acb->bs = bs;
1537 acb->cb = cb;
1538 acb->opaque = opaque;
1539 return acb;
1542 void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1543 void *opaque)
1545 return qemu_aio_get_pool(&bs->drv->aio_pool, bs, cb, opaque);
1548 void qemu_aio_release(void *p)
1550 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
1551 AIOPool *pool = acb->pool;
1552 acb->next = pool->free_aiocb;
1553 pool->free_aiocb = acb;
1556 /**************************************************************/
1557 /* removable device support */
1560 * Return TRUE if the media is present
1562 int bdrv_is_inserted(BlockDriverState *bs)
1564 BlockDriver *drv = bs->drv;
1565 int ret;
1566 if (!drv)
1567 return 0;
1568 if (!drv->bdrv_is_inserted)
1569 return 1;
1570 ret = drv->bdrv_is_inserted(bs);
1571 return ret;
1575 * Return TRUE if the media changed since the last call to this
1576 * function. It is currently only used for floppy disks
1578 int bdrv_media_changed(BlockDriverState *bs)
1580 BlockDriver *drv = bs->drv;
1581 int ret;
1583 if (!drv || !drv->bdrv_media_changed)
1584 ret = -ENOTSUP;
1585 else
1586 ret = drv->bdrv_media_changed(bs);
1587 if (ret == -ENOTSUP)
1588 ret = bs->media_changed;
1589 bs->media_changed = 0;
1590 return ret;
1594 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1596 void bdrv_eject(BlockDriverState *bs, int eject_flag)
1598 BlockDriver *drv = bs->drv;
1599 int ret;
1601 if (!drv || !drv->bdrv_eject) {
1602 ret = -ENOTSUP;
1603 } else {
1604 ret = drv->bdrv_eject(bs, eject_flag);
1606 if (ret == -ENOTSUP) {
1607 if (eject_flag)
1608 bdrv_close(bs);
1612 int bdrv_is_locked(BlockDriverState *bs)
1614 return bs->locked;
1618 * Lock or unlock the media (if it is locked, the user won't be able
1619 * to eject it manually).
1621 void bdrv_set_locked(BlockDriverState *bs, int locked)
1623 BlockDriver *drv = bs->drv;
1625 bs->locked = locked;
1626 if (drv && drv->bdrv_set_locked) {
1627 drv->bdrv_set_locked(bs, locked);
1631 /* needed for generic scsi interface */
1633 int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1635 BlockDriver *drv = bs->drv;
1637 if (drv && drv->bdrv_ioctl)
1638 return drv->bdrv_ioctl(bs, req, buf);
1639 return -ENOTSUP;
1642 BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
1643 unsigned long int req, void *buf,
1644 BlockDriverCompletionFunc *cb, void *opaque)
1646 BlockDriver *drv = bs->drv;
1648 if (drv && drv->bdrv_aio_ioctl)
1649 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
1650 return NULL;