]> git.proxmox.com Git - qemu.git/blame - block.c
usb: Support for removing device by host addr, improved auto filter syntax (Max Krasn...
[qemu.git] / block.c
CommitLineData
fc01f7e7
FB
1/*
2 * QEMU System Emulator block driver
5fafdf24 3 *
fc01f7e7 4 * Copyright (c) 2003 Fabrice Bellard
5fafdf24 5 *
fc01f7e7
FB
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 */
faf07963 24#include "qemu-common.h"
87ecb68b
PB
25#ifndef QEMU_IMG
26#include "console.h"
faf07963 27#endif
ea2384d3 28#include "block_int.h"
fc01f7e7 29
7674e7bf
FB
30#ifdef _BSD
31#include <sys/types.h>
32#include <sys/stat.h>
33#include <sys/ioctl.h>
34#include <sys/queue.h>
35#include <sys/disk.h>
36#endif
37
83f64091
FB
38#define SECTOR_BITS 9
39#define SECTOR_SIZE (1 << SECTOR_BITS)
40
90765429
FB
41typedef struct BlockDriverAIOCBSync {
42 BlockDriverAIOCB common;
43 QEMUBH *bh;
44 int ret;
45} BlockDriverAIOCBSync;
46
ce1a14dc
PB
47static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
48 int64_t sector_num, uint8_t *buf, int nb_sectors,
49 BlockDriverCompletionFunc *cb, void *opaque);
50static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
51 int64_t sector_num, const uint8_t *buf, int nb_sectors,
52 BlockDriverCompletionFunc *cb, void *opaque);
83f64091 53static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
5fafdf24 54static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
83f64091
FB
55 uint8_t *buf, int nb_sectors);
56static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
57 const uint8_t *buf, int nb_sectors);
ec530c81 58
ea2384d3
FB
59static BlockDriver *first_drv;
60
83f64091 61int path_is_absolute(const char *path)
3b0d4f61 62{
83f64091 63 const char *p;
21664424
FB
64#ifdef _WIN32
65 /* specific case for names like: "\\.\d:" */
66 if (*path == '/' || *path == '\\')
67 return 1;
68#endif
83f64091
FB
69 p = strchr(path, ':');
70 if (p)
71 p++;
72 else
73 p = path;
3b9f94e1
FB
74#ifdef _WIN32
75 return (*p == '/' || *p == '\\');
76#else
77 return (*p == '/');
78#endif
3b0d4f61
FB
79}
80
83f64091
FB
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. */
84void path_combine(char *dest, int dest_size,
85 const char *base_path,
86 const char *filename)
3b0d4f61 87{
83f64091
FB
88 const char *p, *p1;
89 int len;
90
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;
3b9f94e1
FB
101 p1 = strrchr(base_path, '/');
102#ifdef _WIN32
103 {
104 const char *p2;
105 p2 = strrchr(base_path, '\\');
106 if (!p1 || p2 > p1)
107 p1 = p2;
108 }
109#endif
83f64091
FB
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);
3b0d4f61 122 }
3b0d4f61
FB
123}
124
3b0d4f61 125
9596ebb7 126static void bdrv_register(BlockDriver *bdrv)
ea2384d3 127{
ce1a14dc 128 if (!bdrv->bdrv_aio_read) {
83f64091 129 /* add AIO emulation layer */
83f64091
FB
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;
90765429 133 bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
83f64091
FB
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;
138 }
ea2384d3
FB
139 bdrv->next = first_drv;
140 first_drv = bdrv;
141}
b338082b
FB
142
143/* create a new block device (by default it is empty) */
144BlockDriverState *bdrv_new(const char *device_name)
145{
146 BlockDriverState **pbs, *bs;
147
148 bs = qemu_mallocz(sizeof(BlockDriverState));
149 if(!bs)
150 return NULL;
151 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
ea2384d3
FB
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;
158 }
b338082b
FB
159 return bs;
160}
161
ea2384d3
FB
162BlockDriver *bdrv_find_format(const char *format_name)
163{
164 BlockDriver *drv1;
165 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
166 if (!strcmp(drv1->format_name, format_name))
167 return drv1;
168 }
169 return NULL;
170}
171
5fafdf24 172int bdrv_create(BlockDriver *drv,
ea2384d3
FB
173 const char *filename, int64_t size_in_sectors,
174 const char *backing_file, int flags)
175{
176 if (!drv->bdrv_create)
177 return -ENOTSUP;
178 return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
179}
180
d5249393 181#ifdef _WIN32
95389c86 182void get_tmp_filename(char *filename, int size)
d5249393 183{
3b9f94e1 184 char temp_dir[MAX_PATH];
3b46e624 185
3b9f94e1
FB
186 GetTempPath(MAX_PATH, temp_dir);
187 GetTempFileName(temp_dir, "qem", 0, filename);
d5249393
FB
188}
189#else
95389c86 190void get_tmp_filename(char *filename, int size)
fc01f7e7 191{
67b915a5 192 int fd;
0badc1ee 193 char *tmpdir;
d5249393 194 /* XXX: race condition possible */
0badc1ee
AJ
195 tmpdir = getenv("TMPDIR");
196 if (!tmpdir)
197 tmpdir = "/tmp";
198 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
ea2384d3
FB
199 fd = mkstemp(filename);
200 close(fd);
201}
d5249393 202#endif
fc01f7e7 203
19cb3738 204#ifdef _WIN32
f45512fe
FB
205static int is_windows_drive_prefix(const char *filename)
206{
207 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
208 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
209 filename[1] == ':');
210}
3b46e624 211
19cb3738
FB
212static int is_windows_drive(const char *filename)
213{
5fafdf24 214 if (is_windows_drive_prefix(filename) &&
f45512fe 215 filename[2] == '\0')
19cb3738
FB
216 return 1;
217 if (strstart(filename, "\\\\.\\", NULL) ||
218 strstart(filename, "//./", NULL))
219 return 1;
220 return 0;
221}
222#endif
223
83f64091
FB
224static BlockDriver *find_protocol(const char *filename)
225{
226 BlockDriver *drv1;
227 char protocol[128];
228 int len;
229 const char *p;
19cb3738
FB
230
231#ifdef _WIN32
f45512fe
FB
232 if (is_windows_drive(filename) ||
233 is_windows_drive_prefix(filename))
19cb3738
FB
234 return &bdrv_raw;
235#endif
83f64091
FB
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;
83f64091
FB
242 memcpy(protocol, filename, len);
243 protocol[len] = '\0';
244 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
5fafdf24 245 if (drv1->protocol_name &&
83f64091
FB
246 !strcmp(drv1->protocol_name, protocol))
247 return drv1;
248 }
249 return NULL;
250}
251
7674e7bf
FB
252/* XXX: force raw format if block or character device ? It would
253 simplify the BSD case */
ea2384d3
FB
254static BlockDriver *find_image_format(const char *filename)
255{
83f64091 256 int ret, score, score_max;
ea2384d3 257 BlockDriver *drv1, *drv;
83f64091
FB
258 uint8_t buf[2048];
259 BlockDriverState *bs;
3b46e624 260
19cb3738
FB
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
269 {
270 struct stat st;
5fafdf24 271 if (stat(filename, &st) >= 0 &&
19cb3738
FB
272 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
273 return &bdrv_host_device;
274 }
275 }
276#endif
3b46e624 277
83f64091 278 drv = find_protocol(filename);
19cb3738 279 /* no need to test disk image formats for vvfat */
83f64091
FB
280 if (drv == &bdrv_vvfat)
281 return drv;
19cb3738 282
83f64091
FB
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;
290 }
291
ea2384d3
FB
292 score_max = 0;
293 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
83f64091
FB
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;
299 }
0849bf08 300 }
fc01f7e7 301 }
ea2384d3
FB
302 return drv;
303}
304
83f64091 305int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
ea2384d3 306{
83f64091
FB
307 BlockDriverState *bs;
308 int ret;
309
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;
3b0d4f61 317 }
83f64091
FB
318 *pbs = bs;
319 return 0;
320}
321
322int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
323{
324 return bdrv_open2(bs, filename, flags, NULL);
ea2384d3
FB
325}
326
83f64091 327int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
ea2384d3
FB
328 BlockDriver *drv)
329{
83f64091 330 int ret, open_flags;
eb5c851f
TS
331 char tmp_filename[PATH_MAX];
332 char backing_filename[PATH_MAX];
3b46e624 333
ea2384d3
FB
334 bs->read_only = 0;
335 bs->is_temporary = 0;
336 bs->encrypted = 0;
712e7874 337
83f64091 338 if (flags & BDRV_O_SNAPSHOT) {
ea2384d3
FB
339 BlockDriverState *bs1;
340 int64_t total_size;
7c96d46e 341 int is_protocol = 0;
3b46e624 342
ea2384d3
FB
343 /* if snapshot, we create a temporary backing file and open it
344 instead of opening 'filename' directly */
33e3963e 345
ea2384d3
FB
346 /* if there is a backing file, use it */
347 bs1 = bdrv_new("");
348 if (!bs1) {
83f64091 349 return -ENOMEM;
ea2384d3
FB
350 }
351 if (bdrv_open(bs1, filename, 0) < 0) {
352 bdrv_delete(bs1);
353 return -1;
354 }
83f64091 355 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
7c96d46e
AL
356
357 if (bs1->drv && bs1->drv->protocol_name)
358 is_protocol = 1;
359
ea2384d3 360 bdrv_delete(bs1);
3b46e624 361
ea2384d3 362 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
7c96d46e
AL
363
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);
370
5fafdf24 371 if (bdrv_create(&bdrv_qcow2, tmp_filename,
a817d936 372 total_size, backing_filename, 0) < 0) {
ea2384d3
FB
373 return -1;
374 }
375 filename = tmp_filename;
376 bs->is_temporary = 1;
377 }
712e7874 378
ea2384d3 379 pstrcpy(bs->filename, sizeof(bs->filename), filename);
83f64091
FB
380 if (flags & BDRV_O_FILE) {
381 drv = find_protocol(filename);
ea2384d3 382 if (!drv)
83f64091
FB
383 return -ENOENT;
384 } else {
385 if (!drv) {
386 drv = find_image_format(filename);
387 if (!drv)
388 return -1;
389 }
ea2384d3
FB
390 }
391 bs->drv = drv;
392 bs->opaque = qemu_mallocz(drv->instance_size);
393 if (bs->opaque == NULL && drv->instance_size > 0)
394 return -1;
83f64091
FB
395 /* Note: for compatibility, we open disk image files as RDWR, and
396 RDONLY as fallback */
397 if (!(flags & BDRV_O_FILE))
33f00271 398 open_flags = BDRV_O_RDWR | (flags & BDRV_O_DIRECT);
83f64091
FB
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 && !(flags & BDRV_O_FILE)) {
403 ret = drv->bdrv_open(bs, filename, BDRV_O_RDONLY);
404 bs->read_only = 1;
405 }
ea2384d3
FB
406 if (ret < 0) {
407 qemu_free(bs->opaque);
6b21b973
FB
408 bs->opaque = NULL;
409 bs->drv = NULL;
83f64091 410 return ret;
33e3963e 411 }
d15a771d
FB
412 if (drv->bdrv_getlength) {
413 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
414 }
67b915a5 415#ifndef _WIN32
ea2384d3
FB
416 if (bs->is_temporary) {
417 unlink(filename);
418 }
419#endif
83f64091 420 if (bs->backing_file[0] != '\0') {
ea2384d3
FB
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);
6b21b973 426 return -ENOMEM;
33e3963e 427 }
83f64091
FB
428 path_combine(backing_filename, sizeof(backing_filename),
429 filename, bs->backing_file);
430 if (bdrv_open(bs->backing_hd, backing_filename, 0) < 0)
33e3963e 431 goto fail;
33e3963e
FB
432 }
433
b338082b 434 /* call the change callback */
19cb3738 435 bs->media_changed = 1;
b338082b
FB
436 if (bs->change_cb)
437 bs->change_cb(bs->change_opaque);
438
439 return 0;
fc01f7e7
FB
440}
441
442void bdrv_close(BlockDriverState *bs)
443{
19cb3738 444 if (bs->drv) {
ea2384d3
FB
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);
452 }
67b915a5 453#endif
ea2384d3
FB
454 bs->opaque = NULL;
455 bs->drv = NULL;
b338082b
FB
456
457 /* call the change callback */
19cb3738 458 bs->media_changed = 1;
b338082b
FB
459 if (bs->change_cb)
460 bs->change_cb(bs->change_opaque);
461 }
462}
463
464void bdrv_delete(BlockDriverState *bs)
465{
34c6f050
AJ
466 BlockDriverState **pbs;
467
468 pbs = &bdrv_first;
469 while (*pbs != bs && *pbs != NULL)
470 pbs = &(*pbs)->next;
471 if (*pbs == bs)
472 *pbs = bs->next;
473
b338082b
FB
474 bdrv_close(bs);
475 qemu_free(bs);
fc01f7e7
FB
476}
477
33e3963e
FB
478/* commit COW file into the raw image */
479int bdrv_commit(BlockDriverState *bs)
480{
19cb3738 481 BlockDriver *drv = bs->drv;
83f64091 482 int64_t i, total_sectors;
ea2384d3
FB
483 int n, j;
484 unsigned char sector[512];
33e3963e 485
19cb3738
FB
486 if (!drv)
487 return -ENOMEDIUM;
33e3963e
FB
488
489 if (bs->read_only) {
ea2384d3 490 return -EACCES;
33e3963e
FB
491 }
492
ea2384d3
FB
493 if (!bs->backing_hd) {
494 return -ENOTSUP;
495 }
33e3963e 496
83f64091
FB
497 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
498 for (i = 0; i < total_sectors;) {
19cb3738 499 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
ea2384d3
FB
500 for(j = 0; j < n; j++) {
501 if (bdrv_read(bs, i, sector, 1) != 0) {
502 return -EIO;
503 }
504
505 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
506 return -EIO;
507 }
508 i++;
33e3963e 509 }
ea2384d3
FB
510 } else {
511 i += n;
512 }
33e3963e 513 }
95389c86 514
19cb3738
FB
515 if (drv->bdrv_make_empty)
516 return drv->bdrv_make_empty(bs);
95389c86 517
33e3963e
FB
518 return 0;
519}
520
19cb3738 521/* return < 0 if error. See bdrv_write() for the return codes */
5fafdf24 522int bdrv_read(BlockDriverState *bs, int64_t sector_num,
fc01f7e7
FB
523 uint8_t *buf, int nb_sectors)
524{
ea2384d3
FB
525 BlockDriver *drv = bs->drv;
526
19cb3738
FB
527 if (!drv)
528 return -ENOMEDIUM;
b338082b 529
83f64091 530 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
cf98951b 531 memcpy(buf, bs->boot_sector_data, 512);
83f64091
FB
532 sector_num++;
533 nb_sectors--;
534 buf += 512;
535 if (nb_sectors == 0)
536 return 0;
537 }
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)
19cb3738 545 return -EINVAL;
a36e69dd
TS
546 else {
547 bs->rd_bytes += (unsigned) len;
548 bs->rd_ops ++;
83f64091 549 return 0;
a36e69dd 550 }
83f64091
FB
551 } else {
552 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
33e3963e 553 }
fc01f7e7
FB
554}
555
5fafdf24 556/* Return < 0 if error. Important errors are:
19cb3738
FB
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
561*/
5fafdf24 562int bdrv_write(BlockDriverState *bs, int64_t sector_num,
fc01f7e7
FB
563 const uint8_t *buf, int nb_sectors)
564{
83f64091 565 BlockDriver *drv = bs->drv;
19cb3738
FB
566 if (!bs->drv)
567 return -ENOMEDIUM;
0849bf08 568 if (bs->read_only)
19cb3738 569 return -EACCES;
79639d42 570 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
3b46e624 571 memcpy(bs->boot_sector_data, buf, 512);
79639d42 572 }
83f64091
FB
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;
a36e69dd
TS
581 else {
582 bs->wr_bytes += (unsigned) len;
583 bs->wr_ops ++;
83f64091 584 return 0;
a36e69dd 585 }
83f64091
FB
586 } else {
587 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
588 }
589}
590
5fafdf24 591static int bdrv_pread_em(BlockDriverState *bs, int64_t offset,
faea38e7 592 uint8_t *buf, int count1)
83f64091 593{
83f64091
FB
594 uint8_t tmp_buf[SECTOR_SIZE];
595 int len, nb_sectors, count;
596 int64_t sector_num;
597
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;
613 }
614
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;
624 }
625
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);
631 }
632 return count1;
633}
634
5fafdf24 635static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset,
faea38e7 636 const uint8_t *buf, int count1)
83f64091 637{
83f64091
FB
638 uint8_t tmp_buf[SECTOR_SIZE];
639 int len, nb_sectors, count;
640 int64_t sector_num;
641
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;
659 }
660
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;
670 }
671
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;
679 }
680 return count1;
681}
83f64091
FB
682
683/**
5fafdf24 684 * Read with byte offsets (needed only for file protocols)
83f64091 685 */
5fafdf24 686int bdrv_pread(BlockDriverState *bs, int64_t offset,
83f64091
FB
687 void *buf1, int count1)
688{
689 BlockDriver *drv = bs->drv;
690
691 if (!drv)
19cb3738 692 return -ENOMEDIUM;
83f64091 693 if (!drv->bdrv_pread)
faea38e7 694 return bdrv_pread_em(bs, offset, buf1, count1);
83f64091
FB
695 return drv->bdrv_pread(bs, offset, buf1, count1);
696}
697
5fafdf24
TS
698/**
699 * Write with byte offsets (needed only for file protocols)
83f64091 700 */
5fafdf24 701int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
83f64091
FB
702 const void *buf1, int count1)
703{
704 BlockDriver *drv = bs->drv;
705
706 if (!drv)
19cb3738 707 return -ENOMEDIUM;
83f64091 708 if (!drv->bdrv_pwrite)
faea38e7 709 return bdrv_pwrite_em(bs, offset, buf1, count1);
83f64091
FB
710 return drv->bdrv_pwrite(bs, offset, buf1, count1);
711}
712
713/**
714 * Truncate file to 'offset' bytes (needed only for file protocols)
715 */
716int bdrv_truncate(BlockDriverState *bs, int64_t offset)
717{
718 BlockDriver *drv = bs->drv;
719 if (!drv)
19cb3738 720 return -ENOMEDIUM;
83f64091
FB
721 if (!drv->bdrv_truncate)
722 return -ENOTSUP;
723 return drv->bdrv_truncate(bs, offset);
724}
725
726/**
727 * Length of a file in bytes. Return < 0 if error or unknown.
728 */
729int64_t bdrv_getlength(BlockDriverState *bs)
730{
731 BlockDriver *drv = bs->drv;
732 if (!drv)
19cb3738 733 return -ENOMEDIUM;
83f64091
FB
734 if (!drv->bdrv_getlength) {
735 /* legacy mode */
736 return bs->total_sectors * SECTOR_SIZE;
737 }
738 return drv->bdrv_getlength(bs);
fc01f7e7
FB
739}
740
19cb3738 741/* return 0 as number of sectors if no device present or error */
96b8f136 742void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
fc01f7e7 743{
19cb3738
FB
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;
fc01f7e7 751}
cf98951b
FB
752
753/* force a given boot sector. */
754void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size)
755{
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);
761}
b338082b 762
5fafdf24 763void bdrv_set_geometry_hint(BlockDriverState *bs,
b338082b
FB
764 int cyls, int heads, int secs)
765{
766 bs->cyls = cyls;
767 bs->heads = heads;
768 bs->secs = secs;
769}
770
771void bdrv_set_type_hint(BlockDriverState *bs, int type)
772{
773 bs->type = type;
774 bs->removable = ((type == BDRV_TYPE_CDROM ||
775 type == BDRV_TYPE_FLOPPY));
776}
777
46d4767d
FB
778void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
779{
780 bs->translation = translation;
781}
782
5fafdf24 783void bdrv_get_geometry_hint(BlockDriverState *bs,
b338082b
FB
784 int *pcyls, int *pheads, int *psecs)
785{
786 *pcyls = bs->cyls;
787 *pheads = bs->heads;
788 *psecs = bs->secs;
789}
790
791int bdrv_get_type_hint(BlockDriverState *bs)
792{
793 return bs->type;
794}
795
46d4767d
FB
796int bdrv_get_translation_hint(BlockDriverState *bs)
797{
798 return bs->translation;
799}
800
b338082b
FB
801int bdrv_is_removable(BlockDriverState *bs)
802{
803 return bs->removable;
804}
805
806int bdrv_is_read_only(BlockDriverState *bs)
807{
808 return bs->read_only;
809}
810
985a03b0
TS
811int bdrv_is_sg(BlockDriverState *bs)
812{
813 return bs->sg;
814}
815
19cb3738 816/* XXX: no longer used */
5fafdf24 817void bdrv_set_change_cb(BlockDriverState *bs,
b338082b
FB
818 void (*change_cb)(void *opaque), void *opaque)
819{
820 bs->change_cb = change_cb;
821 bs->change_opaque = opaque;
822}
823
ea2384d3
FB
824int bdrv_is_encrypted(BlockDriverState *bs)
825{
826 if (bs->backing_hd && bs->backing_hd->encrypted)
827 return 1;
828 return bs->encrypted;
829}
830
831int bdrv_set_key(BlockDriverState *bs, const char *key)
832{
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;
840 }
841 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
842 return -1;
843 return bs->drv->bdrv_set_key(bs, key);
844}
845
846void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
847{
19cb3738 848 if (!bs->drv) {
ea2384d3
FB
849 buf[0] = '\0';
850 } else {
851 pstrcpy(buf, buf_size, bs->drv->format_name);
852 }
853}
854
5fafdf24 855void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
ea2384d3
FB
856 void *opaque)
857{
858 BlockDriver *drv;
859
860 for (drv = first_drv; drv != NULL; drv = drv->next) {
861 it(opaque, drv->format_name);
862 }
863}
864
b338082b
FB
865BlockDriverState *bdrv_find(const char *name)
866{
867 BlockDriverState *bs;
868
869 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
870 if (!strcmp(name, bs->device_name))
871 return bs;
872 }
873 return NULL;
874}
875
81d0912d
FB
876void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
877{
878 BlockDriverState *bs;
879
880 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
881 it(opaque, bs->device_name);
882 }
883}
884
ea2384d3
FB
885const char *bdrv_get_device_name(BlockDriverState *bs)
886{
887 return bs->device_name;
888}
889
7a6cba61
PB
890void bdrv_flush(BlockDriverState *bs)
891{
892 if (bs->drv->bdrv_flush)
893 bs->drv->bdrv_flush(bs);
894 if (bs->backing_hd)
895 bdrv_flush(bs->backing_hd);
896}
897
f58c7b35
TS
898/*
899 * Returns true iff the specified sector is present in the disk image. Drivers
900 * not implementing the functionality are assumed to not support backing files,
901 * hence all their sectors are reported as allocated.
902 *
903 * 'pnum' is set to the number of sectors (including and immediately following
904 * the specified sector) that are known to be in the same
905 * allocated/unallocated state.
906 *
907 * 'nb_sectors' is the max value 'pnum' should be set to.
908 */
909int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
910 int *pnum)
911{
912 int64_t n;
913 if (!bs->drv->bdrv_is_allocated) {
914 if (sector_num >= bs->total_sectors) {
915 *pnum = 0;
916 return 0;
917 }
918 n = bs->total_sectors - sector_num;
919 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
920 return 1;
921 }
922 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
923}
924
faf07963 925#ifndef QEMU_IMG
b338082b
FB
926void bdrv_info(void)
927{
928 BlockDriverState *bs;
929
930 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
931 term_printf("%s:", bs->device_name);
932 term_printf(" type=");
933 switch(bs->type) {
934 case BDRV_TYPE_HD:
935 term_printf("hd");
936 break;
937 case BDRV_TYPE_CDROM:
938 term_printf("cdrom");
939 break;
940 case BDRV_TYPE_FLOPPY:
941 term_printf("floppy");
942 break;
943 }
944 term_printf(" removable=%d", bs->removable);
945 if (bs->removable) {
946 term_printf(" locked=%d", bs->locked);
947 }
19cb3738 948 if (bs->drv) {
fef30743
TS
949 term_printf(" file=");
950 term_print_filename(bs->filename);
951 if (bs->backing_file[0] != '\0') {
952 term_printf(" backing_file=");
953 term_print_filename(bs->backing_file);
954 }
b338082b 955 term_printf(" ro=%d", bs->read_only);
ea2384d3
FB
956 term_printf(" drv=%s", bs->drv->format_name);
957 if (bs->encrypted)
958 term_printf(" encrypted");
b338082b
FB
959 } else {
960 term_printf(" [not inserted]");
961 }
962 term_printf("\n");
963 }
964}
a36e69dd
TS
965
966/* The "info blockstats" command. */
967void bdrv_info_stats (void)
968{
969 BlockDriverState *bs;
970
971 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
972 term_printf ("%s:"
973 " rd_bytes=%" PRIu64
974 " wr_bytes=%" PRIu64
975 " rd_operations=%" PRIu64
976 " wr_operations=%" PRIu64
977 "\n",
978 bs->device_name,
979 bs->rd_bytes, bs->wr_bytes,
980 bs->rd_ops, bs->wr_ops);
981 }
982}
faf07963 983#endif
ea2384d3 984
5fafdf24 985void bdrv_get_backing_filename(BlockDriverState *bs,
83f64091
FB
986 char *filename, int filename_size)
987{
988 if (!bs->backing_hd) {
989 pstrcpy(filename, filename_size, "");
990 } else {
991 pstrcpy(filename, filename_size, bs->backing_file);
992 }
993}
994
5fafdf24 995int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
faea38e7
FB
996 const uint8_t *buf, int nb_sectors)
997{
998 BlockDriver *drv = bs->drv;
999 if (!drv)
19cb3738 1000 return -ENOMEDIUM;
faea38e7
FB
1001 if (!drv->bdrv_write_compressed)
1002 return -ENOTSUP;
1003 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1004}
3b46e624 1005
faea38e7
FB
1006int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1007{
1008 BlockDriver *drv = bs->drv;
1009 if (!drv)
19cb3738 1010 return -ENOMEDIUM;
faea38e7
FB
1011 if (!drv->bdrv_get_info)
1012 return -ENOTSUP;
1013 memset(bdi, 0, sizeof(*bdi));
1014 return drv->bdrv_get_info(bs, bdi);
1015}
1016
1017/**************************************************************/
1018/* handling of snapshots */
1019
5fafdf24 1020int bdrv_snapshot_create(BlockDriverState *bs,
faea38e7
FB
1021 QEMUSnapshotInfo *sn_info)
1022{
1023 BlockDriver *drv = bs->drv;
1024 if (!drv)
19cb3738 1025 return -ENOMEDIUM;
faea38e7
FB
1026 if (!drv->bdrv_snapshot_create)
1027 return -ENOTSUP;
1028 return drv->bdrv_snapshot_create(bs, sn_info);
1029}
1030
5fafdf24 1031int bdrv_snapshot_goto(BlockDriverState *bs,
faea38e7
FB
1032 const char *snapshot_id)
1033{
1034 BlockDriver *drv = bs->drv;
1035 if (!drv)
19cb3738 1036 return -ENOMEDIUM;
faea38e7
FB
1037 if (!drv->bdrv_snapshot_goto)
1038 return -ENOTSUP;
1039 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1040}
1041
1042int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1043{
1044 BlockDriver *drv = bs->drv;
1045 if (!drv)
19cb3738 1046 return -ENOMEDIUM;
faea38e7
FB
1047 if (!drv->bdrv_snapshot_delete)
1048 return -ENOTSUP;
1049 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1050}
1051
5fafdf24 1052int bdrv_snapshot_list(BlockDriverState *bs,
faea38e7
FB
1053 QEMUSnapshotInfo **psn_info)
1054{
1055 BlockDriver *drv = bs->drv;
1056 if (!drv)
19cb3738 1057 return -ENOMEDIUM;
faea38e7
FB
1058 if (!drv->bdrv_snapshot_list)
1059 return -ENOTSUP;
1060 return drv->bdrv_snapshot_list(bs, psn_info);
1061}
1062
1063#define NB_SUFFIXES 4
1064
1065char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1066{
1067 static const char suffixes[NB_SUFFIXES] = "KMGT";
1068 int64_t base;
1069 int i;
1070
1071 if (size <= 999) {
1072 snprintf(buf, buf_size, "%" PRId64, size);
1073 } else {
1074 base = 1024;
1075 for(i = 0; i < NB_SUFFIXES; i++) {
1076 if (size < (10 * base)) {
5fafdf24 1077 snprintf(buf, buf_size, "%0.1f%c",
faea38e7
FB
1078 (double)size / base,
1079 suffixes[i]);
1080 break;
1081 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
5fafdf24 1082 snprintf(buf, buf_size, "%" PRId64 "%c",
faea38e7
FB
1083 ((size + (base >> 1)) / base),
1084 suffixes[i]);
1085 break;
1086 }
1087 base = base * 1024;
1088 }
1089 }
1090 return buf;
1091}
1092
1093char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1094{
1095 char buf1[128], date_buf[128], clock_buf[128];
3b9f94e1
FB
1096#ifdef _WIN32
1097 struct tm *ptm;
1098#else
faea38e7 1099 struct tm tm;
3b9f94e1 1100#endif
faea38e7
FB
1101 time_t ti;
1102 int64_t secs;
1103
1104 if (!sn) {
5fafdf24
TS
1105 snprintf(buf, buf_size,
1106 "%-10s%-20s%7s%20s%15s",
faea38e7
FB
1107 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1108 } else {
1109 ti = sn->date_sec;
3b9f94e1
FB
1110#ifdef _WIN32
1111 ptm = localtime(&ti);
1112 strftime(date_buf, sizeof(date_buf),
1113 "%Y-%m-%d %H:%M:%S", ptm);
1114#else
faea38e7
FB
1115 localtime_r(&ti, &tm);
1116 strftime(date_buf, sizeof(date_buf),
1117 "%Y-%m-%d %H:%M:%S", &tm);
3b9f94e1 1118#endif
faea38e7
FB
1119 secs = sn->vm_clock_nsec / 1000000000;
1120 snprintf(clock_buf, sizeof(clock_buf),
1121 "%02d:%02d:%02d.%03d",
1122 (int)(secs / 3600),
1123 (int)((secs / 60) % 60),
5fafdf24 1124 (int)(secs % 60),
faea38e7
FB
1125 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1126 snprintf(buf, buf_size,
5fafdf24 1127 "%-10s%-20s%7s%20s%15s",
faea38e7
FB
1128 sn->id_str, sn->name,
1129 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1130 date_buf,
1131 clock_buf);
1132 }
1133 return buf;
1134}
1135
83f64091 1136
ea2384d3 1137/**************************************************************/
83f64091 1138/* async I/Os */
ea2384d3 1139
ce1a14dc
PB
1140BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1141 uint8_t *buf, int nb_sectors,
1142 BlockDriverCompletionFunc *cb, void *opaque)
83f64091
FB
1143{
1144 BlockDriver *drv = bs->drv;
a36e69dd 1145 BlockDriverAIOCB *ret;
83f64091 1146
19cb3738 1147 if (!drv)
ce1a14dc 1148 return NULL;
3b46e624 1149
83f64091
FB
1150 /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
1151 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1152 memcpy(buf, bs->boot_sector_data, 512);
1153 sector_num++;
1154 nb_sectors--;
1155 buf += 512;
1156 }
1157
a36e69dd
TS
1158 ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1159
1160 if (ret) {
1161 /* Update stats even though technically transfer has not happened. */
1162 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1163 bs->rd_ops ++;
1164 }
1165
1166 return ret;
ea2384d3
FB
1167}
1168
ce1a14dc
PB
1169BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1170 const uint8_t *buf, int nb_sectors,
1171 BlockDriverCompletionFunc *cb, void *opaque)
ea2384d3 1172{
83f64091 1173 BlockDriver *drv = bs->drv;
a36e69dd 1174 BlockDriverAIOCB *ret;
ea2384d3 1175
19cb3738 1176 if (!drv)
ce1a14dc 1177 return NULL;
83f64091 1178 if (bs->read_only)
ce1a14dc 1179 return NULL;
83f64091 1180 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
3b46e624 1181 memcpy(bs->boot_sector_data, buf, 512);
ea2384d3 1182 }
83f64091 1183
a36e69dd
TS
1184 ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1185
1186 if (ret) {
1187 /* Update stats even though technically transfer has not happened. */
1188 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1189 bs->wr_ops ++;
1190 }
1191
1192 return ret;
83f64091
FB
1193}
1194
1195void bdrv_aio_cancel(BlockDriverAIOCB *acb)
83f64091 1196{
ce1a14dc 1197 BlockDriver *drv = acb->bs->drv;
83f64091 1198
ce1a14dc 1199 drv->bdrv_aio_cancel(acb);
83f64091
FB
1200}
1201
ce1a14dc 1202
83f64091
FB
1203/**************************************************************/
1204/* async block device emulation */
1205
faf07963 1206#ifdef QEMU_IMG
ce1a14dc
PB
1207static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1208 int64_t sector_num, uint8_t *buf, int nb_sectors,
1209 BlockDriverCompletionFunc *cb, void *opaque)
ea2384d3 1210{
ea2384d3 1211 int ret;
ce1a14dc
PB
1212 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1213 cb(opaque, ret);
1214 return NULL;
ea2384d3
FB
1215}
1216
ce1a14dc
PB
1217static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1218 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1219 BlockDriverCompletionFunc *cb, void *opaque)
ea2384d3 1220{
ea2384d3 1221 int ret;
ce1a14dc
PB
1222 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1223 cb(opaque, ret);
1224 return NULL;
ea2384d3
FB
1225}
1226
83f64091 1227static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb)
ea2384d3 1228{
ea2384d3 1229}
83f64091 1230#else
ce1a14dc 1231static void bdrv_aio_bh_cb(void *opaque)
83f64091 1232{
ce1a14dc
PB
1233 BlockDriverAIOCBSync *acb = opaque;
1234 acb->common.cb(acb->common.opaque, acb->ret);
1235 qemu_aio_release(acb);
83f64091 1236}
beac80cd 1237
ce1a14dc
PB
1238static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1239 int64_t sector_num, uint8_t *buf, int nb_sectors,
1240 BlockDriverCompletionFunc *cb, void *opaque)
83f64091 1241{
ce1a14dc 1242 BlockDriverAIOCBSync *acb;
83f64091 1243 int ret;
ce1a14dc
PB
1244
1245 acb = qemu_aio_get(bs, cb, opaque);
1246 if (!acb->bh)
1247 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1248 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1249 acb->ret = ret;
1250 qemu_bh_schedule(acb->bh);
1251 return &acb->common;
beac80cd
FB
1252}
1253
ce1a14dc
PB
1254static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1255 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1256 BlockDriverCompletionFunc *cb, void *opaque)
beac80cd 1257{
ce1a14dc 1258 BlockDriverAIOCBSync *acb;
83f64091 1259 int ret;
83f64091 1260
ce1a14dc
PB
1261 acb = qemu_aio_get(bs, cb, opaque);
1262 if (!acb->bh)
1263 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1264 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1265 acb->ret = ret;
1266 qemu_bh_schedule(acb->bh);
1267 return &acb->common;
beac80cd 1268}
beac80cd 1269
ce1a14dc 1270static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
ea2384d3 1271{
ce1a14dc
PB
1272 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1273 qemu_bh_cancel(acb->bh);
1274 qemu_aio_release(acb);
83f64091 1275}
faf07963 1276#endif /* !QEMU_IMG */
ea2384d3 1277
83f64091
FB
1278/**************************************************************/
1279/* sync block device emulation */
ea2384d3 1280
83f64091
FB
1281static void bdrv_rw_em_cb(void *opaque, int ret)
1282{
1283 *(int *)opaque = ret;
ea2384d3
FB
1284}
1285
83f64091
FB
1286#define NOT_DONE 0x7fffffff
1287
5fafdf24 1288static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
83f64091 1289 uint8_t *buf, int nb_sectors)
7a6cba61 1290{
ce1a14dc
PB
1291 int async_ret;
1292 BlockDriverAIOCB *acb;
83f64091 1293
83f64091 1294 async_ret = NOT_DONE;
5fafdf24 1295 acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
83f64091 1296 bdrv_rw_em_cb, &async_ret);
baf35cb9 1297 if (acb == NULL)
ce1a14dc 1298 return -1;
baf35cb9 1299
83f64091
FB
1300 while (async_ret == NOT_DONE) {
1301 qemu_aio_wait();
1302 }
baf35cb9 1303
83f64091 1304 return async_ret;
7a6cba61
PB
1305}
1306
83f64091
FB
1307static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1308 const uint8_t *buf, int nb_sectors)
1309{
ce1a14dc
PB
1310 int async_ret;
1311 BlockDriverAIOCB *acb;
83f64091 1312
83f64091 1313 async_ret = NOT_DONE;
5fafdf24 1314 acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
83f64091 1315 bdrv_rw_em_cb, &async_ret);
baf35cb9 1316 if (acb == NULL)
ce1a14dc 1317 return -1;
83f64091
FB
1318 while (async_ret == NOT_DONE) {
1319 qemu_aio_wait();
1320 }
83f64091
FB
1321 return async_ret;
1322}
ea2384d3
FB
1323
1324void bdrv_init(void)
1325{
1326 bdrv_register(&bdrv_raw);
19cb3738 1327 bdrv_register(&bdrv_host_device);
ea2384d3
FB
1328#ifndef _WIN32
1329 bdrv_register(&bdrv_cow);
1330#endif
1331 bdrv_register(&bdrv_qcow);
1332 bdrv_register(&bdrv_vmdk);
3c56521b 1333 bdrv_register(&bdrv_cloop);
585d0ed9 1334 bdrv_register(&bdrv_dmg);
a8753c34 1335 bdrv_register(&bdrv_bochs);
6a0f9e82 1336 bdrv_register(&bdrv_vpc);
712e7874 1337 bdrv_register(&bdrv_vvfat);
faea38e7 1338 bdrv_register(&bdrv_qcow2);
6ada7453 1339 bdrv_register(&bdrv_parallels);
cd01b4a3 1340#ifndef _WIN32
75818250 1341 bdrv_register(&bdrv_nbd);
cd01b4a3 1342#endif
a3392f9b
AL
1343
1344 qemu_aio_init();
ea2384d3 1345}
ce1a14dc
PB
1346
1347void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1348 void *opaque)
1349{
1350 BlockDriver *drv;
1351 BlockDriverAIOCB *acb;
1352
1353 drv = bs->drv;
1354 if (drv->free_aiocb) {
1355 acb = drv->free_aiocb;
1356 drv->free_aiocb = acb->next;
1357 } else {
1358 acb = qemu_mallocz(drv->aiocb_size);
1359 if (!acb)
1360 return NULL;
1361 }
1362 acb->bs = bs;
1363 acb->cb = cb;
1364 acb->opaque = opaque;
1365 return acb;
1366}
1367
1368void qemu_aio_release(void *p)
1369{
1370 BlockDriverAIOCB *acb = p;
1371 BlockDriver *drv = acb->bs->drv;
1372 acb->next = drv->free_aiocb;
1373 drv->free_aiocb = acb;
1374}
19cb3738
FB
1375
1376/**************************************************************/
1377/* removable device support */
1378
1379/**
1380 * Return TRUE if the media is present
1381 */
1382int bdrv_is_inserted(BlockDriverState *bs)
1383{
1384 BlockDriver *drv = bs->drv;
1385 int ret;
1386 if (!drv)
1387 return 0;
1388 if (!drv->bdrv_is_inserted)
1389 return 1;
1390 ret = drv->bdrv_is_inserted(bs);
1391 return ret;
1392}
1393
1394/**
1395 * Return TRUE if the media changed since the last call to this
5fafdf24 1396 * function. It is currently only used for floppy disks
19cb3738
FB
1397 */
1398int bdrv_media_changed(BlockDriverState *bs)
1399{
1400 BlockDriver *drv = bs->drv;
1401 int ret;
1402
1403 if (!drv || !drv->bdrv_media_changed)
1404 ret = -ENOTSUP;
1405 else
1406 ret = drv->bdrv_media_changed(bs);
1407 if (ret == -ENOTSUP)
1408 ret = bs->media_changed;
1409 bs->media_changed = 0;
1410 return ret;
1411}
1412
1413/**
1414 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1415 */
1416void bdrv_eject(BlockDriverState *bs, int eject_flag)
1417{
1418 BlockDriver *drv = bs->drv;
1419 int ret;
1420
1421 if (!drv || !drv->bdrv_eject) {
1422 ret = -ENOTSUP;
1423 } else {
1424 ret = drv->bdrv_eject(bs, eject_flag);
1425 }
1426 if (ret == -ENOTSUP) {
1427 if (eject_flag)
1428 bdrv_close(bs);
1429 }
1430}
1431
1432int bdrv_is_locked(BlockDriverState *bs)
1433{
1434 return bs->locked;
1435}
1436
1437/**
1438 * Lock or unlock the media (if it is locked, the user won't be able
1439 * to eject it manually).
1440 */
1441void bdrv_set_locked(BlockDriverState *bs, int locked)
1442{
1443 BlockDriver *drv = bs->drv;
1444
1445 bs->locked = locked;
1446 if (drv && drv->bdrv_set_locked) {
1447 drv->bdrv_set_locked(bs, locked);
1448 }
1449}
985a03b0
TS
1450
1451/* needed for generic scsi interface */
1452
1453int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1454{
1455 BlockDriver *drv = bs->drv;
1456
1457 if (drv && drv->bdrv_ioctl)
1458 return drv->bdrv_ioctl(bs, req, buf);
1459 return -ENOTSUP;
1460}