Silence warnings about unused variables
[qemu/mini2440.git] / block.c
blob48229cde7b456ea71e960ab917657e7f58a8aeef
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 "qemu-common.h"
25 #include "console.h"
26 #include "block_int.h"
28 #ifdef _BSD
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/ioctl.h>
32 #include <sys/queue.h>
33 #include <sys/disk.h>
34 #endif
36 #define SECTOR_BITS 9
37 #define SECTOR_SIZE (1 << SECTOR_BITS)
39 typedef struct BlockDriverAIOCBSync {
40 BlockDriverAIOCB common;
41 QEMUBH *bh;
42 int ret;
43 } BlockDriverAIOCBSync;
45 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
46 int64_t sector_num, uint8_t *buf, int nb_sectors,
47 BlockDriverCompletionFunc *cb, void *opaque);
48 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
49 int64_t sector_num, const uint8_t *buf, int nb_sectors,
50 BlockDriverCompletionFunc *cb, void *opaque);
51 static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
52 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
53 uint8_t *buf, int nb_sectors);
54 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
55 const uint8_t *buf, int nb_sectors);
57 BlockDriverState *bdrv_first;
59 static BlockDriver *first_drv;
61 int path_is_absolute(const char *path)
63 const char *p;
64 #ifdef _WIN32
65 /* specific case for names like: "\\.\d:" */
66 if (*path == '/' || *path == '\\')
67 return 1;
68 #endif
69 p = strchr(path, ':');
70 if (p)
71 p++;
72 else
73 p = path;
74 #ifdef _WIN32
75 return (*p == '/' || *p == '\\');
76 #else
77 return (*p == '/');
78 #endif
81 /* if filename is absolute, just copy it to dest. Otherwise, build a
82 path to it by considering it is relative to base_path. URL are
83 supported. */
84 void path_combine(char *dest, int dest_size,
85 const char *base_path,
86 const char *filename)
88 const char *p, *p1;
89 int len;
91 if (dest_size <= 0)
92 return;
93 if (path_is_absolute(filename)) {
94 pstrcpy(dest, dest_size, filename);
95 } else {
96 p = strchr(base_path, ':');
97 if (p)
98 p++;
99 else
100 p = base_path;
101 p1 = strrchr(base_path, '/');
102 #ifdef _WIN32
104 const char *p2;
105 p2 = strrchr(base_path, '\\');
106 if (!p1 || p2 > p1)
107 p1 = p2;
109 #endif
110 if (p1)
111 p1++;
112 else
113 p1 = base_path;
114 if (p1 > p)
115 p = p1;
116 len = p - base_path;
117 if (len > dest_size - 1)
118 len = dest_size - 1;
119 memcpy(dest, base_path, len);
120 dest[len] = '\0';
121 pstrcat(dest, dest_size, filename);
126 static void bdrv_register(BlockDriver *bdrv)
128 if (!bdrv->bdrv_aio_read) {
129 /* add AIO emulation layer */
130 bdrv->bdrv_aio_read = bdrv_aio_read_em;
131 bdrv->bdrv_aio_write = bdrv_aio_write_em;
132 bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
133 bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
134 } else if (!bdrv->bdrv_read && !bdrv->bdrv_pread) {
135 /* add synchronous IO emulation layer */
136 bdrv->bdrv_read = bdrv_read_em;
137 bdrv->bdrv_write = bdrv_write_em;
139 bdrv->next = first_drv;
140 first_drv = bdrv;
143 /* create a new block device (by default it is empty) */
144 BlockDriverState *bdrv_new(const char *device_name)
146 BlockDriverState **pbs, *bs;
148 bs = qemu_mallocz(sizeof(BlockDriverState));
149 if(!bs)
150 return NULL;
151 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
152 if (device_name[0] != '\0') {
153 /* insert at the end */
154 pbs = &bdrv_first;
155 while (*pbs != NULL)
156 pbs = &(*pbs)->next;
157 *pbs = bs;
159 return bs;
162 BlockDriver *bdrv_find_format(const char *format_name)
164 BlockDriver *drv1;
165 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
166 if (!strcmp(drv1->format_name, format_name))
167 return drv1;
169 return NULL;
172 int bdrv_create(BlockDriver *drv,
173 const char *filename, int64_t size_in_sectors,
174 const char *backing_file, int flags)
176 if (!drv->bdrv_create)
177 return -ENOTSUP;
178 return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
181 #ifdef _WIN32
182 void get_tmp_filename(char *filename, int size)
184 char temp_dir[MAX_PATH];
186 GetTempPath(MAX_PATH, temp_dir);
187 GetTempFileName(temp_dir, "qem", 0, filename);
189 #else
190 void get_tmp_filename(char *filename, int size)
192 int fd;
193 const char *tmpdir;
194 /* XXX: race condition possible */
195 tmpdir = getenv("TMPDIR");
196 if (!tmpdir)
197 tmpdir = "/tmp";
198 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
199 fd = mkstemp(filename);
200 close(fd);
202 #endif
204 #ifdef _WIN32
205 static int is_windows_drive_prefix(const char *filename)
207 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
208 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
209 filename[1] == ':');
212 static int is_windows_drive(const char *filename)
214 if (is_windows_drive_prefix(filename) &&
215 filename[2] == '\0')
216 return 1;
217 if (strstart(filename, "\\\\.\\", NULL) ||
218 strstart(filename, "//./", NULL))
219 return 1;
220 return 0;
222 #endif
224 static BlockDriver *find_protocol(const char *filename)
226 BlockDriver *drv1;
227 char protocol[128];
228 int len;
229 const char *p;
231 #ifdef _WIN32
232 if (is_windows_drive(filename) ||
233 is_windows_drive_prefix(filename))
234 return &bdrv_raw;
235 #endif
236 p = strchr(filename, ':');
237 if (!p)
238 return &bdrv_raw;
239 len = p - filename;
240 if (len > sizeof(protocol) - 1)
241 len = sizeof(protocol) - 1;
242 memcpy(protocol, filename, len);
243 protocol[len] = '\0';
244 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
245 if (drv1->protocol_name &&
246 !strcmp(drv1->protocol_name, protocol))
247 return drv1;
249 return NULL;
252 /* XXX: force raw format if block or character device ? It would
253 simplify the BSD case */
254 static BlockDriver *find_image_format(const char *filename)
256 int ret, score, score_max;
257 BlockDriver *drv1, *drv;
258 uint8_t buf[2048];
259 BlockDriverState *bs;
261 /* detect host devices. By convention, /dev/cdrom[N] is always
262 recognized as a host CDROM */
263 if (strstart(filename, "/dev/cdrom", NULL))
264 return &bdrv_host_device;
265 #ifdef _WIN32
266 if (is_windows_drive(filename))
267 return &bdrv_host_device;
268 #else
270 struct stat st;
271 if (stat(filename, &st) >= 0 &&
272 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
273 return &bdrv_host_device;
276 #endif
278 drv = find_protocol(filename);
279 /* no need to test disk image formats for vvfat */
280 if (drv == &bdrv_vvfat)
281 return drv;
283 ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
284 if (ret < 0)
285 return NULL;
286 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
287 bdrv_delete(bs);
288 if (ret < 0) {
289 return NULL;
292 score_max = 0;
293 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
294 if (drv1->bdrv_probe) {
295 score = drv1->bdrv_probe(buf, ret, filename);
296 if (score > score_max) {
297 score_max = score;
298 drv = drv1;
302 return drv;
305 int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
307 BlockDriverState *bs;
308 int ret;
310 bs = bdrv_new("");
311 if (!bs)
312 return -ENOMEM;
313 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
314 if (ret < 0) {
315 bdrv_delete(bs);
316 return ret;
318 *pbs = bs;
319 return 0;
322 int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
324 return bdrv_open2(bs, filename, flags, NULL);
327 int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
328 BlockDriver *drv)
330 int ret, open_flags;
331 char tmp_filename[PATH_MAX];
332 char backing_filename[PATH_MAX];
334 bs->read_only = 0;
335 bs->is_temporary = 0;
336 bs->encrypted = 0;
338 if (flags & BDRV_O_SNAPSHOT) {
339 BlockDriverState *bs1;
340 int64_t total_size;
341 int is_protocol = 0;
343 /* if snapshot, we create a temporary backing file and open it
344 instead of opening 'filename' directly */
346 /* if there is a backing file, use it */
347 bs1 = bdrv_new("");
348 if (!bs1) {
349 return -ENOMEM;
351 if (bdrv_open(bs1, filename, 0) < 0) {
352 bdrv_delete(bs1);
353 return -1;
355 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
357 if (bs1->drv && bs1->drv->protocol_name)
358 is_protocol = 1;
360 bdrv_delete(bs1);
362 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
364 /* Real path is meaningless for protocols */
365 if (is_protocol)
366 snprintf(backing_filename, sizeof(backing_filename),
367 "%s", filename);
368 else
369 realpath(filename, backing_filename);
371 if (bdrv_create(&bdrv_qcow2, tmp_filename,
372 total_size, backing_filename, 0) < 0) {
373 return -1;
375 filename = tmp_filename;
376 bs->is_temporary = 1;
379 pstrcpy(bs->filename, sizeof(bs->filename), filename);
380 if (flags & BDRV_O_FILE) {
381 drv = find_protocol(filename);
382 if (!drv)
383 return -ENOENT;
384 } else {
385 if (!drv) {
386 drv = find_image_format(filename);
387 if (!drv)
388 return -1;
391 bs->drv = drv;
392 bs->opaque = qemu_mallocz(drv->instance_size);
393 if (bs->opaque == NULL && drv->instance_size > 0)
394 return -1;
395 /* Note: for compatibility, we open disk image files as RDWR, and
396 RDONLY as fallback */
397 if (!(flags & BDRV_O_FILE))
398 open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK);
399 else
400 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
401 ret = drv->bdrv_open(bs, filename, open_flags);
402 if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
403 ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
404 bs->read_only = 1;
406 if (ret < 0) {
407 qemu_free(bs->opaque);
408 bs->opaque = NULL;
409 bs->drv = NULL;
410 return ret;
412 if (drv->bdrv_getlength) {
413 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
415 #ifndef _WIN32
416 if (bs->is_temporary) {
417 unlink(filename);
419 #endif
420 if (bs->backing_file[0] != '\0') {
421 /* if there is a backing file, use it */
422 bs->backing_hd = bdrv_new("");
423 if (!bs->backing_hd) {
424 fail:
425 bdrv_close(bs);
426 return -ENOMEM;
428 path_combine(backing_filename, sizeof(backing_filename),
429 filename, bs->backing_file);
430 if (bdrv_open(bs->backing_hd, backing_filename, open_flags) < 0)
431 goto fail;
434 /* call the change callback */
435 bs->media_changed = 1;
436 if (bs->change_cb)
437 bs->change_cb(bs->change_opaque);
439 return 0;
442 void bdrv_close(BlockDriverState *bs)
444 if (bs->drv) {
445 if (bs->backing_hd)
446 bdrv_delete(bs->backing_hd);
447 bs->drv->bdrv_close(bs);
448 qemu_free(bs->opaque);
449 #ifdef _WIN32
450 if (bs->is_temporary) {
451 unlink(bs->filename);
453 #endif
454 bs->opaque = NULL;
455 bs->drv = NULL;
457 /* call the change callback */
458 bs->media_changed = 1;
459 if (bs->change_cb)
460 bs->change_cb(bs->change_opaque);
464 void bdrv_delete(BlockDriverState *bs)
466 BlockDriverState **pbs;
468 pbs = &bdrv_first;
469 while (*pbs != bs && *pbs != NULL)
470 pbs = &(*pbs)->next;
471 if (*pbs == bs)
472 *pbs = bs->next;
474 bdrv_close(bs);
475 qemu_free(bs);
478 /* commit COW file into the raw image */
479 int bdrv_commit(BlockDriverState *bs)
481 BlockDriver *drv = bs->drv;
482 int64_t i, total_sectors;
483 int n, j;
484 unsigned char sector[512];
486 if (!drv)
487 return -ENOMEDIUM;
489 if (bs->read_only) {
490 return -EACCES;
493 if (!bs->backing_hd) {
494 return -ENOTSUP;
497 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
498 for (i = 0; i < total_sectors;) {
499 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
500 for(j = 0; j < n; j++) {
501 if (bdrv_read(bs, i, sector, 1) != 0) {
502 return -EIO;
505 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
506 return -EIO;
508 i++;
510 } else {
511 i += n;
515 if (drv->bdrv_make_empty)
516 return drv->bdrv_make_empty(bs);
518 return 0;
521 /* return < 0 if error. See bdrv_write() for the return codes */
522 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
523 uint8_t *buf, int nb_sectors)
525 BlockDriver *drv = bs->drv;
527 if (!drv)
528 return -ENOMEDIUM;
530 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
531 memcpy(buf, bs->boot_sector_data, 512);
532 sector_num++;
533 nb_sectors--;
534 buf += 512;
535 if (nb_sectors == 0)
536 return 0;
538 if (drv->bdrv_pread) {
539 int ret, len;
540 len = nb_sectors * 512;
541 ret = drv->bdrv_pread(bs, sector_num * 512, buf, len);
542 if (ret < 0)
543 return ret;
544 else if (ret != len)
545 return -EINVAL;
546 else {
547 bs->rd_bytes += (unsigned) len;
548 bs->rd_ops ++;
549 return 0;
551 } else {
552 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
556 /* Return < 0 if error. Important errors are:
557 -EIO generic I/O error (may happen for all errors)
558 -ENOMEDIUM No media inserted.
559 -EINVAL Invalid sector number or nb_sectors
560 -EACCES Trying to write a read-only device
562 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
563 const uint8_t *buf, int nb_sectors)
565 BlockDriver *drv = bs->drv;
566 if (!bs->drv)
567 return -ENOMEDIUM;
568 if (bs->read_only)
569 return -EACCES;
570 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
571 memcpy(bs->boot_sector_data, buf, 512);
573 if (drv->bdrv_pwrite) {
574 int ret, len;
575 len = nb_sectors * 512;
576 ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len);
577 if (ret < 0)
578 return ret;
579 else if (ret != len)
580 return -EIO;
581 else {
582 bs->wr_bytes += (unsigned) len;
583 bs->wr_ops ++;
584 return 0;
586 } else {
587 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
591 static int bdrv_pread_em(BlockDriverState *bs, int64_t offset,
592 uint8_t *buf, int count1)
594 uint8_t tmp_buf[SECTOR_SIZE];
595 int len, nb_sectors, count;
596 int64_t sector_num;
598 count = count1;
599 /* first read to align to sector start */
600 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
601 if (len > count)
602 len = count;
603 sector_num = offset >> SECTOR_BITS;
604 if (len > 0) {
605 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
606 return -EIO;
607 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
608 count -= len;
609 if (count == 0)
610 return count1;
611 sector_num++;
612 buf += len;
615 /* read the sectors "in place" */
616 nb_sectors = count >> SECTOR_BITS;
617 if (nb_sectors > 0) {
618 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
619 return -EIO;
620 sector_num += nb_sectors;
621 len = nb_sectors << SECTOR_BITS;
622 buf += len;
623 count -= len;
626 /* add data from the last sector */
627 if (count > 0) {
628 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
629 return -EIO;
630 memcpy(buf, tmp_buf, count);
632 return count1;
635 static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset,
636 const uint8_t *buf, int count1)
638 uint8_t tmp_buf[SECTOR_SIZE];
639 int len, nb_sectors, count;
640 int64_t sector_num;
642 count = count1;
643 /* first write to align to sector start */
644 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
645 if (len > count)
646 len = count;
647 sector_num = offset >> SECTOR_BITS;
648 if (len > 0) {
649 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
650 return -EIO;
651 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
652 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
653 return -EIO;
654 count -= len;
655 if (count == 0)
656 return count1;
657 sector_num++;
658 buf += len;
661 /* write the sectors "in place" */
662 nb_sectors = count >> SECTOR_BITS;
663 if (nb_sectors > 0) {
664 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
665 return -EIO;
666 sector_num += nb_sectors;
667 len = nb_sectors << SECTOR_BITS;
668 buf += len;
669 count -= len;
672 /* add data from the last sector */
673 if (count > 0) {
674 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
675 return -EIO;
676 memcpy(tmp_buf, buf, count);
677 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
678 return -EIO;
680 return count1;
684 * Read with byte offsets (needed only for file protocols)
686 int bdrv_pread(BlockDriverState *bs, int64_t offset,
687 void *buf1, int count1)
689 BlockDriver *drv = bs->drv;
691 if (!drv)
692 return -ENOMEDIUM;
693 if (!drv->bdrv_pread)
694 return bdrv_pread_em(bs, offset, buf1, count1);
695 return drv->bdrv_pread(bs, offset, buf1, count1);
699 * Write with byte offsets (needed only for file protocols)
701 int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
702 const void *buf1, int count1)
704 BlockDriver *drv = bs->drv;
706 if (!drv)
707 return -ENOMEDIUM;
708 if (!drv->bdrv_pwrite)
709 return bdrv_pwrite_em(bs, offset, buf1, count1);
710 return drv->bdrv_pwrite(bs, offset, buf1, count1);
714 * Truncate file to 'offset' bytes (needed only for file protocols)
716 int bdrv_truncate(BlockDriverState *bs, int64_t offset)
718 BlockDriver *drv = bs->drv;
719 if (!drv)
720 return -ENOMEDIUM;
721 if (!drv->bdrv_truncate)
722 return -ENOTSUP;
723 return drv->bdrv_truncate(bs, offset);
727 * Length of a file in bytes. Return < 0 if error or unknown.
729 int64_t bdrv_getlength(BlockDriverState *bs)
731 BlockDriver *drv = bs->drv;
732 if (!drv)
733 return -ENOMEDIUM;
734 if (!drv->bdrv_getlength) {
735 /* legacy mode */
736 return bs->total_sectors * SECTOR_SIZE;
738 return drv->bdrv_getlength(bs);
741 /* return 0 as number of sectors if no device present or error */
742 void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
744 int64_t length;
745 length = bdrv_getlength(bs);
746 if (length < 0)
747 length = 0;
748 else
749 length = length >> SECTOR_BITS;
750 *nb_sectors_ptr = length;
753 /* force a given boot sector. */
754 void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size)
756 bs->boot_sector_enabled = 1;
757 if (size > 512)
758 size = 512;
759 memcpy(bs->boot_sector_data, data, size);
760 memset(bs->boot_sector_data + size, 0, 512 - size);
763 void bdrv_set_geometry_hint(BlockDriverState *bs,
764 int cyls, int heads, int secs)
766 bs->cyls = cyls;
767 bs->heads = heads;
768 bs->secs = secs;
771 void bdrv_set_type_hint(BlockDriverState *bs, int type)
773 bs->type = type;
774 bs->removable = ((type == BDRV_TYPE_CDROM ||
775 type == BDRV_TYPE_FLOPPY));
778 void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
780 bs->translation = translation;
783 void bdrv_get_geometry_hint(BlockDriverState *bs,
784 int *pcyls, int *pheads, int *psecs)
786 *pcyls = bs->cyls;
787 *pheads = bs->heads;
788 *psecs = bs->secs;
791 int bdrv_get_type_hint(BlockDriverState *bs)
793 return bs->type;
796 int bdrv_get_translation_hint(BlockDriverState *bs)
798 return bs->translation;
801 int bdrv_is_removable(BlockDriverState *bs)
803 return bs->removable;
806 int bdrv_is_read_only(BlockDriverState *bs)
808 return bs->read_only;
811 int bdrv_is_sg(BlockDriverState *bs)
813 return bs->sg;
816 /* XXX: no longer used */
817 void bdrv_set_change_cb(BlockDriverState *bs,
818 void (*change_cb)(void *opaque), void *opaque)
820 bs->change_cb = change_cb;
821 bs->change_opaque = opaque;
824 int bdrv_is_encrypted(BlockDriverState *bs)
826 if (bs->backing_hd && bs->backing_hd->encrypted)
827 return 1;
828 return bs->encrypted;
831 int bdrv_set_key(BlockDriverState *bs, const char *key)
833 int ret;
834 if (bs->backing_hd && bs->backing_hd->encrypted) {
835 ret = bdrv_set_key(bs->backing_hd, key);
836 if (ret < 0)
837 return ret;
838 if (!bs->encrypted)
839 return 0;
841 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
842 return -1;
843 return bs->drv->bdrv_set_key(bs, key);
846 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
848 if (!bs->drv) {
849 buf[0] = '\0';
850 } else {
851 pstrcpy(buf, buf_size, bs->drv->format_name);
855 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
856 void *opaque)
858 BlockDriver *drv;
860 for (drv = first_drv; drv != NULL; drv = drv->next) {
861 it(opaque, drv->format_name);
865 BlockDriverState *bdrv_find(const char *name)
867 BlockDriverState *bs;
869 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
870 if (!strcmp(name, bs->device_name))
871 return bs;
873 return NULL;
876 void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
878 BlockDriverState *bs;
880 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
881 it(opaque, bs->device_name);
885 const char *bdrv_get_device_name(BlockDriverState *bs)
887 return bs->device_name;
890 void bdrv_flush(BlockDriverState *bs)
892 if (bs->drv->bdrv_flush)
893 bs->drv->bdrv_flush(bs);
894 if (bs->backing_hd)
895 bdrv_flush(bs->backing_hd);
898 void bdrv_flush_all(void)
900 BlockDriverState *bs;
902 for (bs = bdrv_first; bs != NULL; bs = bs->next)
903 if (bs->drv && !bdrv_is_read_only(bs) &&
904 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
905 bdrv_flush(bs);
909 * Returns true iff the specified sector is present in the disk image. Drivers
910 * not implementing the functionality are assumed to not support backing files,
911 * hence all their sectors are reported as allocated.
913 * 'pnum' is set to the number of sectors (including and immediately following
914 * the specified sector) that are known to be in the same
915 * allocated/unallocated state.
917 * 'nb_sectors' is the max value 'pnum' should be set to.
919 int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
920 int *pnum)
922 int64_t n;
923 if (!bs->drv->bdrv_is_allocated) {
924 if (sector_num >= bs->total_sectors) {
925 *pnum = 0;
926 return 0;
928 n = bs->total_sectors - sector_num;
929 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
930 return 1;
932 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
935 void bdrv_info(void)
937 BlockDriverState *bs;
939 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
940 term_printf("%s:", bs->device_name);
941 term_printf(" type=");
942 switch(bs->type) {
943 case BDRV_TYPE_HD:
944 term_printf("hd");
945 break;
946 case BDRV_TYPE_CDROM:
947 term_printf("cdrom");
948 break;
949 case BDRV_TYPE_FLOPPY:
950 term_printf("floppy");
951 break;
953 term_printf(" removable=%d", bs->removable);
954 if (bs->removable) {
955 term_printf(" locked=%d", bs->locked);
957 if (bs->drv) {
958 term_printf(" file=");
959 term_print_filename(bs->filename);
960 if (bs->backing_file[0] != '\0') {
961 term_printf(" backing_file=");
962 term_print_filename(bs->backing_file);
964 term_printf(" ro=%d", bs->read_only);
965 term_printf(" drv=%s", bs->drv->format_name);
966 if (bs->encrypted)
967 term_printf(" encrypted");
968 } else {
969 term_printf(" [not inserted]");
971 term_printf("\n");
975 /* The "info blockstats" command. */
976 void bdrv_info_stats (void)
978 BlockDriverState *bs;
980 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
981 term_printf ("%s:"
982 " rd_bytes=%" PRIu64
983 " wr_bytes=%" PRIu64
984 " rd_operations=%" PRIu64
985 " wr_operations=%" PRIu64
986 "\n",
987 bs->device_name,
988 bs->rd_bytes, bs->wr_bytes,
989 bs->rd_ops, bs->wr_ops);
993 void bdrv_get_backing_filename(BlockDriverState *bs,
994 char *filename, int filename_size)
996 if (!bs->backing_hd) {
997 pstrcpy(filename, filename_size, "");
998 } else {
999 pstrcpy(filename, filename_size, bs->backing_file);
1003 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1004 const uint8_t *buf, int nb_sectors)
1006 BlockDriver *drv = bs->drv;
1007 if (!drv)
1008 return -ENOMEDIUM;
1009 if (!drv->bdrv_write_compressed)
1010 return -ENOTSUP;
1011 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1014 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1016 BlockDriver *drv = bs->drv;
1017 if (!drv)
1018 return -ENOMEDIUM;
1019 if (!drv->bdrv_get_info)
1020 return -ENOTSUP;
1021 memset(bdi, 0, sizeof(*bdi));
1022 return drv->bdrv_get_info(bs, bdi);
1025 /**************************************************************/
1026 /* handling of snapshots */
1028 int bdrv_snapshot_create(BlockDriverState *bs,
1029 QEMUSnapshotInfo *sn_info)
1031 BlockDriver *drv = bs->drv;
1032 if (!drv)
1033 return -ENOMEDIUM;
1034 if (!drv->bdrv_snapshot_create)
1035 return -ENOTSUP;
1036 return drv->bdrv_snapshot_create(bs, sn_info);
1039 int bdrv_snapshot_goto(BlockDriverState *bs,
1040 const char *snapshot_id)
1042 BlockDriver *drv = bs->drv;
1043 if (!drv)
1044 return -ENOMEDIUM;
1045 if (!drv->bdrv_snapshot_goto)
1046 return -ENOTSUP;
1047 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1050 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1052 BlockDriver *drv = bs->drv;
1053 if (!drv)
1054 return -ENOMEDIUM;
1055 if (!drv->bdrv_snapshot_delete)
1056 return -ENOTSUP;
1057 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1060 int bdrv_snapshot_list(BlockDriverState *bs,
1061 QEMUSnapshotInfo **psn_info)
1063 BlockDriver *drv = bs->drv;
1064 if (!drv)
1065 return -ENOMEDIUM;
1066 if (!drv->bdrv_snapshot_list)
1067 return -ENOTSUP;
1068 return drv->bdrv_snapshot_list(bs, psn_info);
1071 #define NB_SUFFIXES 4
1073 char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1075 static const char suffixes[NB_SUFFIXES] = "KMGT";
1076 int64_t base;
1077 int i;
1079 if (size <= 999) {
1080 snprintf(buf, buf_size, "%" PRId64, size);
1081 } else {
1082 base = 1024;
1083 for(i = 0; i < NB_SUFFIXES; i++) {
1084 if (size < (10 * base)) {
1085 snprintf(buf, buf_size, "%0.1f%c",
1086 (double)size / base,
1087 suffixes[i]);
1088 break;
1089 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1090 snprintf(buf, buf_size, "%" PRId64 "%c",
1091 ((size + (base >> 1)) / base),
1092 suffixes[i]);
1093 break;
1095 base = base * 1024;
1098 return buf;
1101 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1103 char buf1[128], date_buf[128], clock_buf[128];
1104 #ifdef _WIN32
1105 struct tm *ptm;
1106 #else
1107 struct tm tm;
1108 #endif
1109 time_t ti;
1110 int64_t secs;
1112 if (!sn) {
1113 snprintf(buf, buf_size,
1114 "%-10s%-20s%7s%20s%15s",
1115 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1116 } else {
1117 ti = sn->date_sec;
1118 #ifdef _WIN32
1119 ptm = localtime(&ti);
1120 strftime(date_buf, sizeof(date_buf),
1121 "%Y-%m-%d %H:%M:%S", ptm);
1122 #else
1123 localtime_r(&ti, &tm);
1124 strftime(date_buf, sizeof(date_buf),
1125 "%Y-%m-%d %H:%M:%S", &tm);
1126 #endif
1127 secs = sn->vm_clock_nsec / 1000000000;
1128 snprintf(clock_buf, sizeof(clock_buf),
1129 "%02d:%02d:%02d.%03d",
1130 (int)(secs / 3600),
1131 (int)((secs / 60) % 60),
1132 (int)(secs % 60),
1133 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1134 snprintf(buf, buf_size,
1135 "%-10s%-20s%7s%20s%15s",
1136 sn->id_str, sn->name,
1137 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1138 date_buf,
1139 clock_buf);
1141 return buf;
1145 /**************************************************************/
1146 /* async I/Os */
1148 BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1149 uint8_t *buf, int nb_sectors,
1150 BlockDriverCompletionFunc *cb, void *opaque)
1152 BlockDriver *drv = bs->drv;
1153 BlockDriverAIOCB *ret;
1155 if (!drv)
1156 return NULL;
1158 /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
1159 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1160 memcpy(buf, bs->boot_sector_data, 512);
1161 sector_num++;
1162 nb_sectors--;
1163 buf += 512;
1166 ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1168 if (ret) {
1169 /* Update stats even though technically transfer has not happened. */
1170 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1171 bs->rd_ops ++;
1174 return ret;
1177 BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1178 const uint8_t *buf, int nb_sectors,
1179 BlockDriverCompletionFunc *cb, void *opaque)
1181 BlockDriver *drv = bs->drv;
1182 BlockDriverAIOCB *ret;
1184 if (!drv)
1185 return NULL;
1186 if (bs->read_only)
1187 return NULL;
1188 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1189 memcpy(bs->boot_sector_data, buf, 512);
1192 ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1194 if (ret) {
1195 /* Update stats even though technically transfer has not happened. */
1196 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1197 bs->wr_ops ++;
1200 return ret;
1203 void bdrv_aio_cancel(BlockDriverAIOCB *acb)
1205 BlockDriver *drv = acb->bs->drv;
1207 drv->bdrv_aio_cancel(acb);
1211 /**************************************************************/
1212 /* async block device emulation */
1214 static void bdrv_aio_bh_cb(void *opaque)
1216 BlockDriverAIOCBSync *acb = opaque;
1217 acb->common.cb(acb->common.opaque, acb->ret);
1218 qemu_aio_release(acb);
1221 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1222 int64_t sector_num, uint8_t *buf, int nb_sectors,
1223 BlockDriverCompletionFunc *cb, void *opaque)
1225 BlockDriverAIOCBSync *acb;
1226 int ret;
1228 acb = qemu_aio_get(bs, cb, opaque);
1229 if (!acb->bh)
1230 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1231 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1232 acb->ret = ret;
1233 qemu_bh_schedule(acb->bh);
1234 return &acb->common;
1237 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1238 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1239 BlockDriverCompletionFunc *cb, void *opaque)
1241 BlockDriverAIOCBSync *acb;
1242 int ret;
1244 acb = qemu_aio_get(bs, cb, opaque);
1245 if (!acb->bh)
1246 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1247 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1248 acb->ret = ret;
1249 qemu_bh_schedule(acb->bh);
1250 return &acb->common;
1253 static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1255 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1256 qemu_bh_cancel(acb->bh);
1257 qemu_aio_release(acb);
1260 /**************************************************************/
1261 /* sync block device emulation */
1263 static void bdrv_rw_em_cb(void *opaque, int ret)
1265 *(int *)opaque = ret;
1268 #define NOT_DONE 0x7fffffff
1270 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
1271 uint8_t *buf, int nb_sectors)
1273 int async_ret;
1274 BlockDriverAIOCB *acb;
1276 async_ret = NOT_DONE;
1277 acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
1278 bdrv_rw_em_cb, &async_ret);
1279 if (acb == NULL)
1280 return -1;
1282 while (async_ret == NOT_DONE) {
1283 qemu_aio_wait();
1286 return async_ret;
1289 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1290 const uint8_t *buf, int nb_sectors)
1292 int async_ret;
1293 BlockDriverAIOCB *acb;
1295 async_ret = NOT_DONE;
1296 acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
1297 bdrv_rw_em_cb, &async_ret);
1298 if (acb == NULL)
1299 return -1;
1300 while (async_ret == NOT_DONE) {
1301 qemu_aio_wait();
1303 return async_ret;
1306 void bdrv_init(void)
1308 bdrv_register(&bdrv_raw);
1309 bdrv_register(&bdrv_host_device);
1310 #ifndef _WIN32
1311 bdrv_register(&bdrv_cow);
1312 #endif
1313 bdrv_register(&bdrv_qcow);
1314 bdrv_register(&bdrv_vmdk);
1315 bdrv_register(&bdrv_cloop);
1316 bdrv_register(&bdrv_dmg);
1317 bdrv_register(&bdrv_bochs);
1318 bdrv_register(&bdrv_vpc);
1319 bdrv_register(&bdrv_vvfat);
1320 bdrv_register(&bdrv_qcow2);
1321 bdrv_register(&bdrv_parallels);
1322 bdrv_register(&bdrv_nbd);
1325 void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1326 void *opaque)
1328 BlockDriver *drv;
1329 BlockDriverAIOCB *acb;
1331 drv = bs->drv;
1332 if (drv->free_aiocb) {
1333 acb = drv->free_aiocb;
1334 drv->free_aiocb = acb->next;
1335 } else {
1336 acb = qemu_mallocz(drv->aiocb_size);
1337 if (!acb)
1338 return NULL;
1340 acb->bs = bs;
1341 acb->cb = cb;
1342 acb->opaque = opaque;
1343 return acb;
1346 void qemu_aio_release(void *p)
1348 BlockDriverAIOCB *acb = p;
1349 BlockDriver *drv = acb->bs->drv;
1350 acb->next = drv->free_aiocb;
1351 drv->free_aiocb = acb;
1354 /**************************************************************/
1355 /* removable device support */
1358 * Return TRUE if the media is present
1360 int bdrv_is_inserted(BlockDriverState *bs)
1362 BlockDriver *drv = bs->drv;
1363 int ret;
1364 if (!drv)
1365 return 0;
1366 if (!drv->bdrv_is_inserted)
1367 return 1;
1368 ret = drv->bdrv_is_inserted(bs);
1369 return ret;
1373 * Return TRUE if the media changed since the last call to this
1374 * function. It is currently only used for floppy disks
1376 int bdrv_media_changed(BlockDriverState *bs)
1378 BlockDriver *drv = bs->drv;
1379 int ret;
1381 if (!drv || !drv->bdrv_media_changed)
1382 ret = -ENOTSUP;
1383 else
1384 ret = drv->bdrv_media_changed(bs);
1385 if (ret == -ENOTSUP)
1386 ret = bs->media_changed;
1387 bs->media_changed = 0;
1388 return ret;
1392 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1394 void bdrv_eject(BlockDriverState *bs, int eject_flag)
1396 BlockDriver *drv = bs->drv;
1397 int ret;
1399 if (!drv || !drv->bdrv_eject) {
1400 ret = -ENOTSUP;
1401 } else {
1402 ret = drv->bdrv_eject(bs, eject_flag);
1404 if (ret == -ENOTSUP) {
1405 if (eject_flag)
1406 bdrv_close(bs);
1410 int bdrv_is_locked(BlockDriverState *bs)
1412 return bs->locked;
1416 * Lock or unlock the media (if it is locked, the user won't be able
1417 * to eject it manually).
1419 void bdrv_set_locked(BlockDriverState *bs, int locked)
1421 BlockDriver *drv = bs->drv;
1423 bs->locked = locked;
1424 if (drv && drv->bdrv_set_locked) {
1425 drv->bdrv_set_locked(bs, locked);
1429 /* needed for generic scsi interface */
1431 int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1433 BlockDriver *drv = bs->drv;
1435 if (drv && drv->bdrv_ioctl)
1436 return drv->bdrv_ioctl(bs, req, buf);
1437 return -ENOTSUP;