]> git.proxmox.com Git - qemu.git/blame - block.c
char: Fix initial reset (Jan Kiszka)
[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 */
3990d09a
BS
24#include "config-host.h"
25#ifdef _BSD
26/* include native header before sys-queue.h */
27#include <sys/queue.h>
28#endif
29
faf07963 30#include "qemu-common.h"
87ecb68b 31#include "console.h"
ea2384d3 32#include "block_int.h"
fc01f7e7 33
7674e7bf
FB
34#ifdef _BSD
35#include <sys/types.h>
36#include <sys/stat.h>
37#include <sys/ioctl.h>
7674e7bf
FB
38#include <sys/disk.h>
39#endif
40
83f64091
FB
41#define SECTOR_BITS 9
42#define SECTOR_SIZE (1 << SECTOR_BITS)
43
90765429
FB
44typedef struct BlockDriverAIOCBSync {
45 BlockDriverAIOCB common;
46 QEMUBH *bh;
47 int ret;
48} BlockDriverAIOCBSync;
49
ce1a14dc
PB
50static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
51 int64_t sector_num, uint8_t *buf, int nb_sectors,
52 BlockDriverCompletionFunc *cb, void *opaque);
53static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
54 int64_t sector_num, const uint8_t *buf, int nb_sectors,
55 BlockDriverCompletionFunc *cb, void *opaque);
83f64091 56static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
5fafdf24 57static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
83f64091
FB
58 uint8_t *buf, int nb_sectors);
59static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
60 const uint8_t *buf, int nb_sectors);
ec530c81 61
7ee930d0
BS
62BlockDriverState *bdrv_first;
63
ea2384d3
FB
64static BlockDriver *first_drv;
65
83f64091 66int path_is_absolute(const char *path)
3b0d4f61 67{
83f64091 68 const char *p;
21664424
FB
69#ifdef _WIN32
70 /* specific case for names like: "\\.\d:" */
71 if (*path == '/' || *path == '\\')
72 return 1;
73#endif
83f64091
FB
74 p = strchr(path, ':');
75 if (p)
76 p++;
77 else
78 p = path;
3b9f94e1
FB
79#ifdef _WIN32
80 return (*p == '/' || *p == '\\');
81#else
82 return (*p == '/');
83#endif
3b0d4f61
FB
84}
85
83f64091
FB
86/* if filename is absolute, just copy it to dest. Otherwise, build a
87 path to it by considering it is relative to base_path. URL are
88 supported. */
89void path_combine(char *dest, int dest_size,
90 const char *base_path,
91 const char *filename)
3b0d4f61 92{
83f64091
FB
93 const char *p, *p1;
94 int len;
95
96 if (dest_size <= 0)
97 return;
98 if (path_is_absolute(filename)) {
99 pstrcpy(dest, dest_size, filename);
100 } else {
101 p = strchr(base_path, ':');
102 if (p)
103 p++;
104 else
105 p = base_path;
3b9f94e1
FB
106 p1 = strrchr(base_path, '/');
107#ifdef _WIN32
108 {
109 const char *p2;
110 p2 = strrchr(base_path, '\\');
111 if (!p1 || p2 > p1)
112 p1 = p2;
113 }
114#endif
83f64091
FB
115 if (p1)
116 p1++;
117 else
118 p1 = base_path;
119 if (p1 > p)
120 p = p1;
121 len = p - base_path;
122 if (len > dest_size - 1)
123 len = dest_size - 1;
124 memcpy(dest, base_path, len);
125 dest[len] = '\0';
126 pstrcat(dest, dest_size, filename);
3b0d4f61 127 }
3b0d4f61
FB
128}
129
3b0d4f61 130
9596ebb7 131static void bdrv_register(BlockDriver *bdrv)
ea2384d3 132{
ce1a14dc 133 if (!bdrv->bdrv_aio_read) {
83f64091 134 /* add AIO emulation layer */
83f64091
FB
135 bdrv->bdrv_aio_read = bdrv_aio_read_em;
136 bdrv->bdrv_aio_write = bdrv_aio_write_em;
137 bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
90765429 138 bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
83f64091
FB
139 } else if (!bdrv->bdrv_read && !bdrv->bdrv_pread) {
140 /* add synchronous IO emulation layer */
141 bdrv->bdrv_read = bdrv_read_em;
142 bdrv->bdrv_write = bdrv_write_em;
143 }
ea2384d3
FB
144 bdrv->next = first_drv;
145 first_drv = bdrv;
146}
b338082b
FB
147
148/* create a new block device (by default it is empty) */
149BlockDriverState *bdrv_new(const char *device_name)
150{
151 BlockDriverState **pbs, *bs;
152
153 bs = qemu_mallocz(sizeof(BlockDriverState));
b338082b 154 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
ea2384d3
FB
155 if (device_name[0] != '\0') {
156 /* insert at the end */
157 pbs = &bdrv_first;
158 while (*pbs != NULL)
159 pbs = &(*pbs)->next;
160 *pbs = bs;
161 }
b338082b
FB
162 return bs;
163}
164
ea2384d3
FB
165BlockDriver *bdrv_find_format(const char *format_name)
166{
167 BlockDriver *drv1;
168 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
169 if (!strcmp(drv1->format_name, format_name))
170 return drv1;
171 }
172 return NULL;
173}
174
5fafdf24 175int bdrv_create(BlockDriver *drv,
ea2384d3
FB
176 const char *filename, int64_t size_in_sectors,
177 const char *backing_file, int flags)
178{
179 if (!drv->bdrv_create)
180 return -ENOTSUP;
181 return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
182}
183
d5249393 184#ifdef _WIN32
95389c86 185void get_tmp_filename(char *filename, int size)
d5249393 186{
3b9f94e1 187 char temp_dir[MAX_PATH];
3b46e624 188
3b9f94e1
FB
189 GetTempPath(MAX_PATH, temp_dir);
190 GetTempFileName(temp_dir, "qem", 0, filename);
d5249393
FB
191}
192#else
95389c86 193void get_tmp_filename(char *filename, int size)
fc01f7e7 194{
67b915a5 195 int fd;
7ccfb2eb 196 const char *tmpdir;
d5249393 197 /* XXX: race condition possible */
0badc1ee
AJ
198 tmpdir = getenv("TMPDIR");
199 if (!tmpdir)
200 tmpdir = "/tmp";
201 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
ea2384d3
FB
202 fd = mkstemp(filename);
203 close(fd);
204}
d5249393 205#endif
fc01f7e7 206
19cb3738 207#ifdef _WIN32
f45512fe
FB
208static int is_windows_drive_prefix(const char *filename)
209{
210 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
211 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
212 filename[1] == ':');
213}
3b46e624 214
19cb3738
FB
215static int is_windows_drive(const char *filename)
216{
5fafdf24 217 if (is_windows_drive_prefix(filename) &&
f45512fe 218 filename[2] == '\0')
19cb3738
FB
219 return 1;
220 if (strstart(filename, "\\\\.\\", NULL) ||
221 strstart(filename, "//./", NULL))
222 return 1;
223 return 0;
224}
225#endif
226
83f64091
FB
227static BlockDriver *find_protocol(const char *filename)
228{
229 BlockDriver *drv1;
230 char protocol[128];
231 int len;
232 const char *p;
19cb3738
FB
233
234#ifdef _WIN32
f45512fe
FB
235 if (is_windows_drive(filename) ||
236 is_windows_drive_prefix(filename))
19cb3738
FB
237 return &bdrv_raw;
238#endif
83f64091
FB
239 p = strchr(filename, ':');
240 if (!p)
241 return &bdrv_raw;
242 len = p - filename;
243 if (len > sizeof(protocol) - 1)
244 len = sizeof(protocol) - 1;
83f64091
FB
245 memcpy(protocol, filename, len);
246 protocol[len] = '\0';
247 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
5fafdf24 248 if (drv1->protocol_name &&
83f64091
FB
249 !strcmp(drv1->protocol_name, protocol))
250 return drv1;
251 }
252 return NULL;
253}
254
7674e7bf
FB
255/* XXX: force raw format if block or character device ? It would
256 simplify the BSD case */
ea2384d3
FB
257static BlockDriver *find_image_format(const char *filename)
258{
83f64091 259 int ret, score, score_max;
ea2384d3 260 BlockDriver *drv1, *drv;
83f64091
FB
261 uint8_t buf[2048];
262 BlockDriverState *bs;
3b46e624 263
19cb3738
FB
264 /* detect host devices. By convention, /dev/cdrom[N] is always
265 recognized as a host CDROM */
266 if (strstart(filename, "/dev/cdrom", NULL))
267 return &bdrv_host_device;
268#ifdef _WIN32
269 if (is_windows_drive(filename))
270 return &bdrv_host_device;
271#else
272 {
273 struct stat st;
5fafdf24 274 if (stat(filename, &st) >= 0 &&
19cb3738
FB
275 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
276 return &bdrv_host_device;
277 }
278 }
279#endif
3b46e624 280
83f64091 281 drv = find_protocol(filename);
19cb3738 282 /* no need to test disk image formats for vvfat */
83f64091
FB
283 if (drv == &bdrv_vvfat)
284 return drv;
19cb3738 285
83f64091
FB
286 ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
287 if (ret < 0)
288 return NULL;
289 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
290 bdrv_delete(bs);
291 if (ret < 0) {
292 return NULL;
293 }
294
ea2384d3
FB
295 score_max = 0;
296 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
83f64091
FB
297 if (drv1->bdrv_probe) {
298 score = drv1->bdrv_probe(buf, ret, filename);
299 if (score > score_max) {
300 score_max = score;
301 drv = drv1;
302 }
0849bf08 303 }
fc01f7e7 304 }
ea2384d3
FB
305 return drv;
306}
307
83f64091 308int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
ea2384d3 309{
83f64091
FB
310 BlockDriverState *bs;
311 int ret;
312
313 bs = bdrv_new("");
314 if (!bs)
315 return -ENOMEM;
316 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
317 if (ret < 0) {
318 bdrv_delete(bs);
319 return ret;
3b0d4f61 320 }
71d0770c 321 bs->growable = 1;
83f64091
FB
322 *pbs = bs;
323 return 0;
324}
325
326int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
327{
328 return bdrv_open2(bs, filename, flags, NULL);
ea2384d3
FB
329}
330
83f64091 331int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
ea2384d3
FB
332 BlockDriver *drv)
333{
83f64091 334 int ret, open_flags;
eb5c851f
TS
335 char tmp_filename[PATH_MAX];
336 char backing_filename[PATH_MAX];
3b46e624 337
ea2384d3
FB
338 bs->read_only = 0;
339 bs->is_temporary = 0;
340 bs->encrypted = 0;
712e7874 341
83f64091 342 if (flags & BDRV_O_SNAPSHOT) {
ea2384d3
FB
343 BlockDriverState *bs1;
344 int64_t total_size;
7c96d46e 345 int is_protocol = 0;
3b46e624 346
ea2384d3
FB
347 /* if snapshot, we create a temporary backing file and open it
348 instead of opening 'filename' directly */
33e3963e 349
ea2384d3
FB
350 /* if there is a backing file, use it */
351 bs1 = bdrv_new("");
352 if (!bs1) {
83f64091 353 return -ENOMEM;
ea2384d3
FB
354 }
355 if (bdrv_open(bs1, filename, 0) < 0) {
356 bdrv_delete(bs1);
357 return -1;
358 }
83f64091 359 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
7c96d46e
AL
360
361 if (bs1->drv && bs1->drv->protocol_name)
362 is_protocol = 1;
363
ea2384d3 364 bdrv_delete(bs1);
3b46e624 365
ea2384d3 366 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
7c96d46e
AL
367
368 /* Real path is meaningless for protocols */
369 if (is_protocol)
370 snprintf(backing_filename, sizeof(backing_filename),
371 "%s", filename);
372 else
373 realpath(filename, backing_filename);
374
5fafdf24 375 if (bdrv_create(&bdrv_qcow2, tmp_filename,
a817d936 376 total_size, backing_filename, 0) < 0) {
ea2384d3
FB
377 return -1;
378 }
379 filename = tmp_filename;
380 bs->is_temporary = 1;
381 }
712e7874 382
ea2384d3 383 pstrcpy(bs->filename, sizeof(bs->filename), filename);
83f64091
FB
384 if (flags & BDRV_O_FILE) {
385 drv = find_protocol(filename);
ea2384d3 386 if (!drv)
83f64091
FB
387 return -ENOENT;
388 } else {
389 if (!drv) {
390 drv = find_image_format(filename);
391 if (!drv)
392 return -1;
393 }
ea2384d3
FB
394 }
395 bs->drv = drv;
396 bs->opaque = qemu_mallocz(drv->instance_size);
83f64091
FB
397 /* Note: for compatibility, we open disk image files as RDWR, and
398 RDONLY as fallback */
399 if (!(flags & BDRV_O_FILE))
9f7965c7 400 open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK);
83f64091
FB
401 else
402 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
403 ret = drv->bdrv_open(bs, filename, open_flags);
a0a83536 404 if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
9f7965c7 405 ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
83f64091
FB
406 bs->read_only = 1;
407 }
ea2384d3
FB
408 if (ret < 0) {
409 qemu_free(bs->opaque);
6b21b973
FB
410 bs->opaque = NULL;
411 bs->drv = NULL;
83f64091 412 return ret;
33e3963e 413 }
d15a771d
FB
414 if (drv->bdrv_getlength) {
415 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
416 }
67b915a5 417#ifndef _WIN32
ea2384d3
FB
418 if (bs->is_temporary) {
419 unlink(filename);
420 }
421#endif
83f64091 422 if (bs->backing_file[0] != '\0') {
ea2384d3
FB
423 /* if there is a backing file, use it */
424 bs->backing_hd = bdrv_new("");
425 if (!bs->backing_hd) {
426 fail:
427 bdrv_close(bs);
6b21b973 428 return -ENOMEM;
33e3963e 429 }
83f64091
FB
430 path_combine(backing_filename, sizeof(backing_filename),
431 filename, bs->backing_file);
9f7965c7 432 if (bdrv_open(bs->backing_hd, backing_filename, open_flags) < 0)
33e3963e 433 goto fail;
33e3963e
FB
434 }
435
b338082b 436 /* call the change callback */
19cb3738 437 bs->media_changed = 1;
b338082b
FB
438 if (bs->change_cb)
439 bs->change_cb(bs->change_opaque);
440
441 return 0;
fc01f7e7
FB
442}
443
444void bdrv_close(BlockDriverState *bs)
445{
19cb3738 446 if (bs->drv) {
ea2384d3
FB
447 if (bs->backing_hd)
448 bdrv_delete(bs->backing_hd);
449 bs->drv->bdrv_close(bs);
450 qemu_free(bs->opaque);
451#ifdef _WIN32
452 if (bs->is_temporary) {
453 unlink(bs->filename);
454 }
67b915a5 455#endif
ea2384d3
FB
456 bs->opaque = NULL;
457 bs->drv = NULL;
b338082b
FB
458
459 /* call the change callback */
19cb3738 460 bs->media_changed = 1;
b338082b
FB
461 if (bs->change_cb)
462 bs->change_cb(bs->change_opaque);
463 }
464}
465
466void bdrv_delete(BlockDriverState *bs)
467{
34c6f050
AJ
468 BlockDriverState **pbs;
469
470 pbs = &bdrv_first;
471 while (*pbs != bs && *pbs != NULL)
472 pbs = &(*pbs)->next;
473 if (*pbs == bs)
474 *pbs = bs->next;
475
b338082b
FB
476 bdrv_close(bs);
477 qemu_free(bs);
fc01f7e7
FB
478}
479
33e3963e
FB
480/* commit COW file into the raw image */
481int bdrv_commit(BlockDriverState *bs)
482{
19cb3738 483 BlockDriver *drv = bs->drv;
83f64091 484 int64_t i, total_sectors;
ea2384d3
FB
485 int n, j;
486 unsigned char sector[512];
33e3963e 487
19cb3738
FB
488 if (!drv)
489 return -ENOMEDIUM;
33e3963e
FB
490
491 if (bs->read_only) {
ea2384d3 492 return -EACCES;
33e3963e
FB
493 }
494
ea2384d3
FB
495 if (!bs->backing_hd) {
496 return -ENOTSUP;
497 }
33e3963e 498
83f64091
FB
499 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
500 for (i = 0; i < total_sectors;) {
19cb3738 501 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
ea2384d3
FB
502 for(j = 0; j < n; j++) {
503 if (bdrv_read(bs, i, sector, 1) != 0) {
504 return -EIO;
505 }
506
507 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
508 return -EIO;
509 }
510 i++;
33e3963e 511 }
ea2384d3
FB
512 } else {
513 i += n;
514 }
33e3963e 515 }
95389c86 516
19cb3738
FB
517 if (drv->bdrv_make_empty)
518 return drv->bdrv_make_empty(bs);
95389c86 519
33e3963e
FB
520 return 0;
521}
522
71d0770c
AL
523static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
524 size_t size)
525{
526 int64_t len;
527
528 if (!bdrv_is_inserted(bs))
529 return -ENOMEDIUM;
530
531 if (bs->growable)
532 return 0;
533
534 len = bdrv_getlength(bs);
535
536 if ((offset + size) > len)
537 return -EIO;
538
539 return 0;
540}
541
542static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
543 int nb_sectors)
544{
545 int64_t offset;
546
547 /* Deal with byte accesses */
548 if (sector_num < 0)
549 offset = -sector_num;
550 else
551 offset = sector_num * 512;
552
553 return bdrv_check_byte_request(bs, offset, nb_sectors * 512);
554}
555
19cb3738 556/* return < 0 if error. See bdrv_write() for the return codes */
5fafdf24 557int bdrv_read(BlockDriverState *bs, int64_t sector_num,
fc01f7e7
FB
558 uint8_t *buf, int nb_sectors)
559{
ea2384d3
FB
560 BlockDriver *drv = bs->drv;
561
19cb3738
FB
562 if (!drv)
563 return -ENOMEDIUM;
71d0770c
AL
564 if (bdrv_check_request(bs, sector_num, nb_sectors))
565 return -EIO;
b338082b 566
83f64091
FB
567 if (drv->bdrv_pread) {
568 int ret, len;
569 len = nb_sectors * 512;
570 ret = drv->bdrv_pread(bs, sector_num * 512, buf, len);
571 if (ret < 0)
572 return ret;
573 else if (ret != len)
19cb3738 574 return -EINVAL;
a36e69dd
TS
575 else {
576 bs->rd_bytes += (unsigned) len;
577 bs->rd_ops ++;
83f64091 578 return 0;
a36e69dd 579 }
83f64091
FB
580 } else {
581 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
33e3963e 582 }
fc01f7e7
FB
583}
584
5fafdf24 585/* Return < 0 if error. Important errors are:
19cb3738
FB
586 -EIO generic I/O error (may happen for all errors)
587 -ENOMEDIUM No media inserted.
588 -EINVAL Invalid sector number or nb_sectors
589 -EACCES Trying to write a read-only device
590*/
5fafdf24 591int bdrv_write(BlockDriverState *bs, int64_t sector_num,
fc01f7e7
FB
592 const uint8_t *buf, int nb_sectors)
593{
83f64091 594 BlockDriver *drv = bs->drv;
19cb3738
FB
595 if (!bs->drv)
596 return -ENOMEDIUM;
0849bf08 597 if (bs->read_only)
19cb3738 598 return -EACCES;
71d0770c
AL
599 if (bdrv_check_request(bs, sector_num, nb_sectors))
600 return -EIO;
601
83f64091 602 if (drv->bdrv_pwrite) {
42fb2807 603 int ret, len, count = 0;
83f64091 604 len = nb_sectors * 512;
42fb2807
AL
605 do {
606 ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len - count);
607 if (ret < 0) {
608 printf("bdrv_write ret=%d\n", ret);
609 return ret;
610 }
611 count += ret;
612 buf += ret;
613 } while (count != len);
614 bs->wr_bytes += (unsigned) len;
615 bs->wr_ops ++;
616 return 0;
83f64091 617 }
42fb2807 618 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
83f64091
FB
619}
620
5fafdf24 621static int bdrv_pread_em(BlockDriverState *bs, int64_t offset,
faea38e7 622 uint8_t *buf, int count1)
83f64091 623{
83f64091
FB
624 uint8_t tmp_buf[SECTOR_SIZE];
625 int len, nb_sectors, count;
626 int64_t sector_num;
627
628 count = count1;
629 /* first read to align to sector start */
630 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
631 if (len > count)
632 len = count;
633 sector_num = offset >> SECTOR_BITS;
634 if (len > 0) {
635 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
636 return -EIO;
637 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
638 count -= len;
639 if (count == 0)
640 return count1;
641 sector_num++;
642 buf += len;
643 }
644
645 /* read the sectors "in place" */
646 nb_sectors = count >> SECTOR_BITS;
647 if (nb_sectors > 0) {
648 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
649 return -EIO;
650 sector_num += nb_sectors;
651 len = nb_sectors << SECTOR_BITS;
652 buf += len;
653 count -= len;
654 }
655
656 /* add data from the last sector */
657 if (count > 0) {
658 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
659 return -EIO;
660 memcpy(buf, tmp_buf, count);
661 }
662 return count1;
663}
664
5fafdf24 665static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset,
faea38e7 666 const uint8_t *buf, int count1)
83f64091 667{
83f64091
FB
668 uint8_t tmp_buf[SECTOR_SIZE];
669 int len, nb_sectors, count;
670 int64_t sector_num;
671
672 count = count1;
673 /* first write to align to sector start */
674 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
675 if (len > count)
676 len = count;
677 sector_num = offset >> SECTOR_BITS;
678 if (len > 0) {
679 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
680 return -EIO;
681 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
682 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
683 return -EIO;
684 count -= len;
685 if (count == 0)
686 return count1;
687 sector_num++;
688 buf += len;
689 }
690
691 /* write the sectors "in place" */
692 nb_sectors = count >> SECTOR_BITS;
693 if (nb_sectors > 0) {
694 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
695 return -EIO;
696 sector_num += nb_sectors;
697 len = nb_sectors << SECTOR_BITS;
698 buf += len;
699 count -= len;
700 }
701
702 /* add data from the last sector */
703 if (count > 0) {
704 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
705 return -EIO;
706 memcpy(tmp_buf, buf, count);
707 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
708 return -EIO;
709 }
710 return count1;
711}
83f64091
FB
712
713/**
5fafdf24 714 * Read with byte offsets (needed only for file protocols)
83f64091 715 */
5fafdf24 716int bdrv_pread(BlockDriverState *bs, int64_t offset,
83f64091
FB
717 void *buf1, int count1)
718{
719 BlockDriver *drv = bs->drv;
720
721 if (!drv)
19cb3738 722 return -ENOMEDIUM;
71d0770c
AL
723 if (bdrv_check_byte_request(bs, offset, count1))
724 return -EIO;
725
83f64091 726 if (!drv->bdrv_pread)
faea38e7 727 return bdrv_pread_em(bs, offset, buf1, count1);
83f64091
FB
728 return drv->bdrv_pread(bs, offset, buf1, count1);
729}
730
5fafdf24
TS
731/**
732 * Write with byte offsets (needed only for file protocols)
83f64091 733 */
5fafdf24 734int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
83f64091
FB
735 const void *buf1, int count1)
736{
737 BlockDriver *drv = bs->drv;
738
739 if (!drv)
19cb3738 740 return -ENOMEDIUM;
71d0770c
AL
741 if (bdrv_check_byte_request(bs, offset, count1))
742 return -EIO;
743
83f64091 744 if (!drv->bdrv_pwrite)
faea38e7 745 return bdrv_pwrite_em(bs, offset, buf1, count1);
83f64091
FB
746 return drv->bdrv_pwrite(bs, offset, buf1, count1);
747}
748
749/**
750 * Truncate file to 'offset' bytes (needed only for file protocols)
751 */
752int bdrv_truncate(BlockDriverState *bs, int64_t offset)
753{
754 BlockDriver *drv = bs->drv;
755 if (!drv)
19cb3738 756 return -ENOMEDIUM;
83f64091
FB
757 if (!drv->bdrv_truncate)
758 return -ENOTSUP;
759 return drv->bdrv_truncate(bs, offset);
760}
761
762/**
763 * Length of a file in bytes. Return < 0 if error or unknown.
764 */
765int64_t bdrv_getlength(BlockDriverState *bs)
766{
767 BlockDriver *drv = bs->drv;
768 if (!drv)
19cb3738 769 return -ENOMEDIUM;
83f64091
FB
770 if (!drv->bdrv_getlength) {
771 /* legacy mode */
772 return bs->total_sectors * SECTOR_SIZE;
773 }
774 return drv->bdrv_getlength(bs);
fc01f7e7
FB
775}
776
19cb3738 777/* return 0 as number of sectors if no device present or error */
96b8f136 778void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
fc01f7e7 779{
19cb3738
FB
780 int64_t length;
781 length = bdrv_getlength(bs);
782 if (length < 0)
783 length = 0;
784 else
785 length = length >> SECTOR_BITS;
786 *nb_sectors_ptr = length;
fc01f7e7 787}
cf98951b 788
f3d54fc4
AL
789struct partition {
790 uint8_t boot_ind; /* 0x80 - active */
791 uint8_t head; /* starting head */
792 uint8_t sector; /* starting sector */
793 uint8_t cyl; /* starting cylinder */
794 uint8_t sys_ind; /* What partition type */
795 uint8_t end_head; /* end head */
796 uint8_t end_sector; /* end sector */
797 uint8_t end_cyl; /* end cylinder */
798 uint32_t start_sect; /* starting sector counting from 0 */
799 uint32_t nr_sects; /* nr of sectors in partition */
800} __attribute__((packed));
801
802/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
803static int guess_disk_lchs(BlockDriverState *bs,
804 int *pcylinders, int *pheads, int *psectors)
805{
806 uint8_t buf[512];
807 int ret, i, heads, sectors, cylinders;
808 struct partition *p;
809 uint32_t nr_sects;
a38131b6 810 uint64_t nb_sectors;
f3d54fc4
AL
811
812 bdrv_get_geometry(bs, &nb_sectors);
813
814 ret = bdrv_read(bs, 0, buf, 1);
815 if (ret < 0)
816 return -1;
817 /* test msdos magic */
818 if (buf[510] != 0x55 || buf[511] != 0xaa)
819 return -1;
820 for(i = 0; i < 4; i++) {
821 p = ((struct partition *)(buf + 0x1be)) + i;
822 nr_sects = le32_to_cpu(p->nr_sects);
823 if (nr_sects && p->end_head) {
824 /* We make the assumption that the partition terminates on
825 a cylinder boundary */
826 heads = p->end_head + 1;
827 sectors = p->end_sector & 63;
828 if (sectors == 0)
829 continue;
830 cylinders = nb_sectors / (heads * sectors);
831 if (cylinders < 1 || cylinders > 16383)
832 continue;
833 *pheads = heads;
834 *psectors = sectors;
835 *pcylinders = cylinders;
836#if 0
837 printf("guessed geometry: LCHS=%d %d %d\n",
838 cylinders, heads, sectors);
839#endif
840 return 0;
841 }
842 }
843 return -1;
844}
845
846void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
847{
848 int translation, lba_detected = 0;
849 int cylinders, heads, secs;
a38131b6 850 uint64_t nb_sectors;
f3d54fc4
AL
851
852 /* if a geometry hint is available, use it */
853 bdrv_get_geometry(bs, &nb_sectors);
854 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
855 translation = bdrv_get_translation_hint(bs);
856 if (cylinders != 0) {
857 *pcyls = cylinders;
858 *pheads = heads;
859 *psecs = secs;
860 } else {
861 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
862 if (heads > 16) {
863 /* if heads > 16, it means that a BIOS LBA
864 translation was active, so the default
865 hardware geometry is OK */
866 lba_detected = 1;
867 goto default_geometry;
868 } else {
869 *pcyls = cylinders;
870 *pheads = heads;
871 *psecs = secs;
872 /* disable any translation to be in sync with
873 the logical geometry */
874 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
875 bdrv_set_translation_hint(bs,
876 BIOS_ATA_TRANSLATION_NONE);
877 }
878 }
879 } else {
880 default_geometry:
881 /* if no geometry, use a standard physical disk geometry */
882 cylinders = nb_sectors / (16 * 63);
883
884 if (cylinders > 16383)
885 cylinders = 16383;
886 else if (cylinders < 2)
887 cylinders = 2;
888 *pcyls = cylinders;
889 *pheads = 16;
890 *psecs = 63;
891 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
892 if ((*pcyls * *pheads) <= 131072) {
893 bdrv_set_translation_hint(bs,
894 BIOS_ATA_TRANSLATION_LARGE);
895 } else {
896 bdrv_set_translation_hint(bs,
897 BIOS_ATA_TRANSLATION_LBA);
898 }
899 }
900 }
901 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
902 }
903}
904
5fafdf24 905void bdrv_set_geometry_hint(BlockDriverState *bs,
b338082b
FB
906 int cyls, int heads, int secs)
907{
908 bs->cyls = cyls;
909 bs->heads = heads;
910 bs->secs = secs;
911}
912
913void bdrv_set_type_hint(BlockDriverState *bs, int type)
914{
915 bs->type = type;
916 bs->removable = ((type == BDRV_TYPE_CDROM ||
917 type == BDRV_TYPE_FLOPPY));
918}
919
46d4767d
FB
920void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
921{
922 bs->translation = translation;
923}
924
5fafdf24 925void bdrv_get_geometry_hint(BlockDriverState *bs,
b338082b
FB
926 int *pcyls, int *pheads, int *psecs)
927{
928 *pcyls = bs->cyls;
929 *pheads = bs->heads;
930 *psecs = bs->secs;
931}
932
933int bdrv_get_type_hint(BlockDriverState *bs)
934{
935 return bs->type;
936}
937
46d4767d
FB
938int bdrv_get_translation_hint(BlockDriverState *bs)
939{
940 return bs->translation;
941}
942
b338082b
FB
943int bdrv_is_removable(BlockDriverState *bs)
944{
945 return bs->removable;
946}
947
948int bdrv_is_read_only(BlockDriverState *bs)
949{
950 return bs->read_only;
951}
952
985a03b0
TS
953int bdrv_is_sg(BlockDriverState *bs)
954{
955 return bs->sg;
956}
957
19cb3738 958/* XXX: no longer used */
5fafdf24 959void bdrv_set_change_cb(BlockDriverState *bs,
b338082b
FB
960 void (*change_cb)(void *opaque), void *opaque)
961{
962 bs->change_cb = change_cb;
963 bs->change_opaque = opaque;
964}
965
ea2384d3
FB
966int bdrv_is_encrypted(BlockDriverState *bs)
967{
968 if (bs->backing_hd && bs->backing_hd->encrypted)
969 return 1;
970 return bs->encrypted;
971}
972
973int bdrv_set_key(BlockDriverState *bs, const char *key)
974{
975 int ret;
976 if (bs->backing_hd && bs->backing_hd->encrypted) {
977 ret = bdrv_set_key(bs->backing_hd, key);
978 if (ret < 0)
979 return ret;
980 if (!bs->encrypted)
981 return 0;
982 }
983 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
984 return -1;
985 return bs->drv->bdrv_set_key(bs, key);
986}
987
988void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
989{
19cb3738 990 if (!bs->drv) {
ea2384d3
FB
991 buf[0] = '\0';
992 } else {
993 pstrcpy(buf, buf_size, bs->drv->format_name);
994 }
995}
996
5fafdf24 997void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
ea2384d3
FB
998 void *opaque)
999{
1000 BlockDriver *drv;
1001
1002 for (drv = first_drv; drv != NULL; drv = drv->next) {
1003 it(opaque, drv->format_name);
1004 }
1005}
1006
b338082b
FB
1007BlockDriverState *bdrv_find(const char *name)
1008{
1009 BlockDriverState *bs;
1010
1011 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1012 if (!strcmp(name, bs->device_name))
1013 return bs;
1014 }
1015 return NULL;
1016}
1017
81d0912d
FB
1018void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
1019{
1020 BlockDriverState *bs;
1021
1022 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1023 it(opaque, bs->device_name);
1024 }
1025}
1026
ea2384d3
FB
1027const char *bdrv_get_device_name(BlockDriverState *bs)
1028{
1029 return bs->device_name;
1030}
1031
7a6cba61
PB
1032void bdrv_flush(BlockDriverState *bs)
1033{
1034 if (bs->drv->bdrv_flush)
1035 bs->drv->bdrv_flush(bs);
1036 if (bs->backing_hd)
1037 bdrv_flush(bs->backing_hd);
1038}
1039
c6ca28d6
AL
1040void bdrv_flush_all(void)
1041{
1042 BlockDriverState *bs;
1043
1044 for (bs = bdrv_first; bs != NULL; bs = bs->next)
1045 if (bs->drv && !bdrv_is_read_only(bs) &&
1046 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1047 bdrv_flush(bs);
1048}
1049
f58c7b35
TS
1050/*
1051 * Returns true iff the specified sector is present in the disk image. Drivers
1052 * not implementing the functionality are assumed to not support backing files,
1053 * hence all their sectors are reported as allocated.
1054 *
1055 * 'pnum' is set to the number of sectors (including and immediately following
1056 * the specified sector) that are known to be in the same
1057 * allocated/unallocated state.
1058 *
1059 * 'nb_sectors' is the max value 'pnum' should be set to.
1060 */
1061int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1062 int *pnum)
1063{
1064 int64_t n;
1065 if (!bs->drv->bdrv_is_allocated) {
1066 if (sector_num >= bs->total_sectors) {
1067 *pnum = 0;
1068 return 0;
1069 }
1070 n = bs->total_sectors - sector_num;
1071 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1072 return 1;
1073 }
1074 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1075}
1076
b338082b
FB
1077void bdrv_info(void)
1078{
1079 BlockDriverState *bs;
1080
1081 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1082 term_printf("%s:", bs->device_name);
1083 term_printf(" type=");
1084 switch(bs->type) {
1085 case BDRV_TYPE_HD:
1086 term_printf("hd");
1087 break;
1088 case BDRV_TYPE_CDROM:
1089 term_printf("cdrom");
1090 break;
1091 case BDRV_TYPE_FLOPPY:
1092 term_printf("floppy");
1093 break;
1094 }
1095 term_printf(" removable=%d", bs->removable);
1096 if (bs->removable) {
1097 term_printf(" locked=%d", bs->locked);
1098 }
19cb3738 1099 if (bs->drv) {
fef30743
TS
1100 term_printf(" file=");
1101 term_print_filename(bs->filename);
1102 if (bs->backing_file[0] != '\0') {
1103 term_printf(" backing_file=");
1104 term_print_filename(bs->backing_file);
1105 }
b338082b 1106 term_printf(" ro=%d", bs->read_only);
ea2384d3
FB
1107 term_printf(" drv=%s", bs->drv->format_name);
1108 if (bs->encrypted)
1109 term_printf(" encrypted");
b338082b
FB
1110 } else {
1111 term_printf(" [not inserted]");
1112 }
1113 term_printf("\n");
1114 }
1115}
a36e69dd
TS
1116
1117/* The "info blockstats" command. */
1118void bdrv_info_stats (void)
1119{
1120 BlockDriverState *bs;
a7cbfae0 1121 BlockDriverInfo bdi;
a36e69dd
TS
1122
1123 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1124 term_printf ("%s:"
1125 " rd_bytes=%" PRIu64
1126 " wr_bytes=%" PRIu64
1127 " rd_operations=%" PRIu64
1128 " wr_operations=%" PRIu64
a7cbfae0 1129 ,
a36e69dd
TS
1130 bs->device_name,
1131 bs->rd_bytes, bs->wr_bytes,
1132 bs->rd_ops, bs->wr_ops);
a7cbfae0 1133 if (bdrv_get_info(bs, &bdi) == 0)
1987530f
AL
1134 term_printf(" high=%" PRId64
1135 " bytes_free=%" PRId64,
1136 bdi.highest_alloc, bdi.num_free_bytes);
a7cbfae0 1137 term_printf("\n");
a36e69dd
TS
1138 }
1139}
ea2384d3 1140
5fafdf24 1141void bdrv_get_backing_filename(BlockDriverState *bs,
83f64091
FB
1142 char *filename, int filename_size)
1143{
1144 if (!bs->backing_hd) {
1145 pstrcpy(filename, filename_size, "");
1146 } else {
1147 pstrcpy(filename, filename_size, bs->backing_file);
1148 }
1149}
1150
5fafdf24 1151int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
faea38e7
FB
1152 const uint8_t *buf, int nb_sectors)
1153{
1154 BlockDriver *drv = bs->drv;
1155 if (!drv)
19cb3738 1156 return -ENOMEDIUM;
faea38e7
FB
1157 if (!drv->bdrv_write_compressed)
1158 return -ENOTSUP;
1159 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1160}
3b46e624 1161
faea38e7
FB
1162int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1163{
1164 BlockDriver *drv = bs->drv;
1165 if (!drv)
19cb3738 1166 return -ENOMEDIUM;
faea38e7
FB
1167 if (!drv->bdrv_get_info)
1168 return -ENOTSUP;
1169 memset(bdi, 0, sizeof(*bdi));
1170 return drv->bdrv_get_info(bs, bdi);
1171}
1172
1173/**************************************************************/
1174/* handling of snapshots */
1175
5fafdf24 1176int bdrv_snapshot_create(BlockDriverState *bs,
faea38e7
FB
1177 QEMUSnapshotInfo *sn_info)
1178{
1179 BlockDriver *drv = bs->drv;
1180 if (!drv)
19cb3738 1181 return -ENOMEDIUM;
faea38e7
FB
1182 if (!drv->bdrv_snapshot_create)
1183 return -ENOTSUP;
1184 return drv->bdrv_snapshot_create(bs, sn_info);
1185}
1186
5fafdf24 1187int bdrv_snapshot_goto(BlockDriverState *bs,
faea38e7
FB
1188 const char *snapshot_id)
1189{
1190 BlockDriver *drv = bs->drv;
1191 if (!drv)
19cb3738 1192 return -ENOMEDIUM;
faea38e7
FB
1193 if (!drv->bdrv_snapshot_goto)
1194 return -ENOTSUP;
1195 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1196}
1197
1198int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1199{
1200 BlockDriver *drv = bs->drv;
1201 if (!drv)
19cb3738 1202 return -ENOMEDIUM;
faea38e7
FB
1203 if (!drv->bdrv_snapshot_delete)
1204 return -ENOTSUP;
1205 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1206}
1207
5fafdf24 1208int bdrv_snapshot_list(BlockDriverState *bs,
faea38e7
FB
1209 QEMUSnapshotInfo **psn_info)
1210{
1211 BlockDriver *drv = bs->drv;
1212 if (!drv)
19cb3738 1213 return -ENOMEDIUM;
faea38e7
FB
1214 if (!drv->bdrv_snapshot_list)
1215 return -ENOTSUP;
1216 return drv->bdrv_snapshot_list(bs, psn_info);
1217}
1218
1219#define NB_SUFFIXES 4
1220
1221char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1222{
1223 static const char suffixes[NB_SUFFIXES] = "KMGT";
1224 int64_t base;
1225 int i;
1226
1227 if (size <= 999) {
1228 snprintf(buf, buf_size, "%" PRId64, size);
1229 } else {
1230 base = 1024;
1231 for(i = 0; i < NB_SUFFIXES; i++) {
1232 if (size < (10 * base)) {
5fafdf24 1233 snprintf(buf, buf_size, "%0.1f%c",
faea38e7
FB
1234 (double)size / base,
1235 suffixes[i]);
1236 break;
1237 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
5fafdf24 1238 snprintf(buf, buf_size, "%" PRId64 "%c",
faea38e7
FB
1239 ((size + (base >> 1)) / base),
1240 suffixes[i]);
1241 break;
1242 }
1243 base = base * 1024;
1244 }
1245 }
1246 return buf;
1247}
1248
1249char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1250{
1251 char buf1[128], date_buf[128], clock_buf[128];
3b9f94e1
FB
1252#ifdef _WIN32
1253 struct tm *ptm;
1254#else
faea38e7 1255 struct tm tm;
3b9f94e1 1256#endif
faea38e7
FB
1257 time_t ti;
1258 int64_t secs;
1259
1260 if (!sn) {
5fafdf24
TS
1261 snprintf(buf, buf_size,
1262 "%-10s%-20s%7s%20s%15s",
faea38e7
FB
1263 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1264 } else {
1265 ti = sn->date_sec;
3b9f94e1
FB
1266#ifdef _WIN32
1267 ptm = localtime(&ti);
1268 strftime(date_buf, sizeof(date_buf),
1269 "%Y-%m-%d %H:%M:%S", ptm);
1270#else
faea38e7
FB
1271 localtime_r(&ti, &tm);
1272 strftime(date_buf, sizeof(date_buf),
1273 "%Y-%m-%d %H:%M:%S", &tm);
3b9f94e1 1274#endif
faea38e7
FB
1275 secs = sn->vm_clock_nsec / 1000000000;
1276 snprintf(clock_buf, sizeof(clock_buf),
1277 "%02d:%02d:%02d.%03d",
1278 (int)(secs / 3600),
1279 (int)((secs / 60) % 60),
5fafdf24 1280 (int)(secs % 60),
faea38e7
FB
1281 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1282 snprintf(buf, buf_size,
5fafdf24 1283 "%-10s%-20s%7s%20s%15s",
faea38e7
FB
1284 sn->id_str, sn->name,
1285 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1286 date_buf,
1287 clock_buf);
1288 }
1289 return buf;
1290}
1291
83f64091 1292
ea2384d3 1293/**************************************************************/
83f64091 1294/* async I/Os */
ea2384d3 1295
3b69e4b9
AL
1296typedef struct VectorTranslationState {
1297 QEMUIOVector *iov;
1298 uint8_t *bounce;
1299 int is_write;
1300 BlockDriverAIOCB *aiocb;
1301 BlockDriverAIOCB *this_aiocb;
1302} VectorTranslationState;
1303
1304static void bdrv_aio_rw_vector_cb(void *opaque, int ret)
1305{
1306 VectorTranslationState *s = opaque;
1307
1308 if (!s->is_write) {
249aa745 1309 qemu_iovec_from_buffer(s->iov, s->bounce, s->iov->size);
3b69e4b9 1310 }
d905dba4 1311 qemu_vfree(s->bounce);
3b69e4b9
AL
1312 s->this_aiocb->cb(s->this_aiocb->opaque, ret);
1313 qemu_aio_release(s->this_aiocb);
1314}
1315
1316static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
1317 int64_t sector_num,
1318 QEMUIOVector *iov,
1319 int nb_sectors,
1320 BlockDriverCompletionFunc *cb,
1321 void *opaque,
1322 int is_write)
1323
1324{
1325 VectorTranslationState *s = qemu_mallocz(sizeof(*s));
1326 BlockDriverAIOCB *aiocb = qemu_aio_get(bs, cb, opaque);
1327
1328 s->this_aiocb = aiocb;
1329 s->iov = iov;
1330 s->bounce = qemu_memalign(512, nb_sectors * 512);
1331 s->is_write = is_write;
1332 if (is_write) {
1333 qemu_iovec_to_buffer(s->iov, s->bounce);
1334 s->aiocb = bdrv_aio_write(bs, sector_num, s->bounce, nb_sectors,
1335 bdrv_aio_rw_vector_cb, s);
1336 } else {
1337 s->aiocb = bdrv_aio_read(bs, sector_num, s->bounce, nb_sectors,
1338 bdrv_aio_rw_vector_cb, s);
1339 }
1340 return aiocb;
1341}
1342
1343BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
1344 QEMUIOVector *iov, int nb_sectors,
1345 BlockDriverCompletionFunc *cb, void *opaque)
1346{
71d0770c
AL
1347 if (bdrv_check_request(bs, sector_num, nb_sectors))
1348 return NULL;
1349
3b69e4b9
AL
1350 return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
1351 cb, opaque, 0);
1352}
1353
1354BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1355 QEMUIOVector *iov, int nb_sectors,
1356 BlockDriverCompletionFunc *cb, void *opaque)
1357{
71d0770c
AL
1358 if (bdrv_check_request(bs, sector_num, nb_sectors))
1359 return NULL;
1360
3b69e4b9
AL
1361 return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
1362 cb, opaque, 1);
1363}
1364
ce1a14dc
PB
1365BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1366 uint8_t *buf, int nb_sectors,
1367 BlockDriverCompletionFunc *cb, void *opaque)
83f64091
FB
1368{
1369 BlockDriver *drv = bs->drv;
a36e69dd 1370 BlockDriverAIOCB *ret;
83f64091 1371
19cb3738 1372 if (!drv)
ce1a14dc 1373 return NULL;
71d0770c
AL
1374 if (bdrv_check_request(bs, sector_num, nb_sectors))
1375 return NULL;
3b46e624 1376
a36e69dd
TS
1377 ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1378
1379 if (ret) {
1380 /* Update stats even though technically transfer has not happened. */
1381 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1382 bs->rd_ops ++;
1383 }
1384
1385 return ret;
ea2384d3
FB
1386}
1387
ce1a14dc
PB
1388BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1389 const uint8_t *buf, int nb_sectors,
1390 BlockDriverCompletionFunc *cb, void *opaque)
ea2384d3 1391{
83f64091 1392 BlockDriver *drv = bs->drv;
a36e69dd 1393 BlockDriverAIOCB *ret;
ea2384d3 1394
19cb3738 1395 if (!drv)
ce1a14dc 1396 return NULL;
83f64091 1397 if (bs->read_only)
ce1a14dc 1398 return NULL;
71d0770c
AL
1399 if (bdrv_check_request(bs, sector_num, nb_sectors))
1400 return NULL;
83f64091 1401
a36e69dd
TS
1402 ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1403
1404 if (ret) {
1405 /* Update stats even though technically transfer has not happened. */
1406 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1407 bs->wr_ops ++;
1408 }
1409
1410 return ret;
83f64091
FB
1411}
1412
1413void bdrv_aio_cancel(BlockDriverAIOCB *acb)
83f64091 1414{
ce1a14dc 1415 BlockDriver *drv = acb->bs->drv;
83f64091 1416
3b69e4b9
AL
1417 if (acb->cb == bdrv_aio_rw_vector_cb) {
1418 VectorTranslationState *s = acb->opaque;
1419 acb = s->aiocb;
1420 }
1421
ce1a14dc 1422 drv->bdrv_aio_cancel(acb);
83f64091
FB
1423}
1424
ce1a14dc 1425
83f64091
FB
1426/**************************************************************/
1427/* async block device emulation */
1428
ce1a14dc 1429static void bdrv_aio_bh_cb(void *opaque)
83f64091 1430{
ce1a14dc
PB
1431 BlockDriverAIOCBSync *acb = opaque;
1432 acb->common.cb(acb->common.opaque, acb->ret);
1433 qemu_aio_release(acb);
83f64091 1434}
beac80cd 1435
ce1a14dc
PB
1436static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1437 int64_t sector_num, uint8_t *buf, int nb_sectors,
1438 BlockDriverCompletionFunc *cb, void *opaque)
83f64091 1439{
ce1a14dc 1440 BlockDriverAIOCBSync *acb;
83f64091 1441 int ret;
ce1a14dc
PB
1442
1443 acb = qemu_aio_get(bs, cb, opaque);
1444 if (!acb->bh)
1445 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1446 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1447 acb->ret = ret;
1448 qemu_bh_schedule(acb->bh);
1449 return &acb->common;
beac80cd
FB
1450}
1451
ce1a14dc
PB
1452static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1453 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1454 BlockDriverCompletionFunc *cb, void *opaque)
beac80cd 1455{
ce1a14dc 1456 BlockDriverAIOCBSync *acb;
83f64091 1457 int ret;
83f64091 1458
ce1a14dc
PB
1459 acb = qemu_aio_get(bs, cb, opaque);
1460 if (!acb->bh)
1461 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1462 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1463 acb->ret = ret;
1464 qemu_bh_schedule(acb->bh);
1465 return &acb->common;
beac80cd 1466}
beac80cd 1467
ce1a14dc 1468static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
ea2384d3 1469{
ce1a14dc
PB
1470 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1471 qemu_bh_cancel(acb->bh);
1472 qemu_aio_release(acb);
83f64091 1473}
ea2384d3 1474
83f64091
FB
1475/**************************************************************/
1476/* sync block device emulation */
ea2384d3 1477
83f64091
FB
1478static void bdrv_rw_em_cb(void *opaque, int ret)
1479{
1480 *(int *)opaque = ret;
ea2384d3
FB
1481}
1482
83f64091
FB
1483#define NOT_DONE 0x7fffffff
1484
5fafdf24 1485static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
83f64091 1486 uint8_t *buf, int nb_sectors)
7a6cba61 1487{
ce1a14dc
PB
1488 int async_ret;
1489 BlockDriverAIOCB *acb;
83f64091 1490
83f64091 1491 async_ret = NOT_DONE;
5fafdf24 1492 acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
83f64091 1493 bdrv_rw_em_cb, &async_ret);
baf35cb9 1494 if (acb == NULL)
ce1a14dc 1495 return -1;
baf35cb9 1496
83f64091
FB
1497 while (async_ret == NOT_DONE) {
1498 qemu_aio_wait();
1499 }
baf35cb9 1500
83f64091 1501 return async_ret;
7a6cba61
PB
1502}
1503
83f64091
FB
1504static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1505 const uint8_t *buf, int nb_sectors)
1506{
ce1a14dc
PB
1507 int async_ret;
1508 BlockDriverAIOCB *acb;
83f64091 1509
83f64091 1510 async_ret = NOT_DONE;
5fafdf24 1511 acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
83f64091 1512 bdrv_rw_em_cb, &async_ret);
baf35cb9 1513 if (acb == NULL)
ce1a14dc 1514 return -1;
83f64091
FB
1515 while (async_ret == NOT_DONE) {
1516 qemu_aio_wait();
1517 }
83f64091
FB
1518 return async_ret;
1519}
ea2384d3
FB
1520
1521void bdrv_init(void)
1522{
1523 bdrv_register(&bdrv_raw);
19cb3738 1524 bdrv_register(&bdrv_host_device);
ea2384d3
FB
1525#ifndef _WIN32
1526 bdrv_register(&bdrv_cow);
1527#endif
1528 bdrv_register(&bdrv_qcow);
1529 bdrv_register(&bdrv_vmdk);
3c56521b 1530 bdrv_register(&bdrv_cloop);
585d0ed9 1531 bdrv_register(&bdrv_dmg);
a8753c34 1532 bdrv_register(&bdrv_bochs);
6a0f9e82 1533 bdrv_register(&bdrv_vpc);
712e7874 1534 bdrv_register(&bdrv_vvfat);
faea38e7 1535 bdrv_register(&bdrv_qcow2);
6ada7453 1536 bdrv_register(&bdrv_parallels);
75818250 1537 bdrv_register(&bdrv_nbd);
ea2384d3 1538}
ce1a14dc
PB
1539
1540void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1541 void *opaque)
1542{
1543 BlockDriver *drv;
1544 BlockDriverAIOCB *acb;
1545
1546 drv = bs->drv;
1547 if (drv->free_aiocb) {
1548 acb = drv->free_aiocb;
1549 drv->free_aiocb = acb->next;
1550 } else {
1551 acb = qemu_mallocz(drv->aiocb_size);
ce1a14dc
PB
1552 }
1553 acb->bs = bs;
1554 acb->cb = cb;
1555 acb->opaque = opaque;
1556 return acb;
1557}
1558
1559void qemu_aio_release(void *p)
1560{
1561 BlockDriverAIOCB *acb = p;
1562 BlockDriver *drv = acb->bs->drv;
1563 acb->next = drv->free_aiocb;
1564 drv->free_aiocb = acb;
1565}
19cb3738
FB
1566
1567/**************************************************************/
1568/* removable device support */
1569
1570/**
1571 * Return TRUE if the media is present
1572 */
1573int bdrv_is_inserted(BlockDriverState *bs)
1574{
1575 BlockDriver *drv = bs->drv;
1576 int ret;
1577 if (!drv)
1578 return 0;
1579 if (!drv->bdrv_is_inserted)
1580 return 1;
1581 ret = drv->bdrv_is_inserted(bs);
1582 return ret;
1583}
1584
1585/**
1586 * Return TRUE if the media changed since the last call to this
5fafdf24 1587 * function. It is currently only used for floppy disks
19cb3738
FB
1588 */
1589int bdrv_media_changed(BlockDriverState *bs)
1590{
1591 BlockDriver *drv = bs->drv;
1592 int ret;
1593
1594 if (!drv || !drv->bdrv_media_changed)
1595 ret = -ENOTSUP;
1596 else
1597 ret = drv->bdrv_media_changed(bs);
1598 if (ret == -ENOTSUP)
1599 ret = bs->media_changed;
1600 bs->media_changed = 0;
1601 return ret;
1602}
1603
1604/**
1605 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1606 */
1607void bdrv_eject(BlockDriverState *bs, int eject_flag)
1608{
1609 BlockDriver *drv = bs->drv;
1610 int ret;
1611
1612 if (!drv || !drv->bdrv_eject) {
1613 ret = -ENOTSUP;
1614 } else {
1615 ret = drv->bdrv_eject(bs, eject_flag);
1616 }
1617 if (ret == -ENOTSUP) {
1618 if (eject_flag)
1619 bdrv_close(bs);
1620 }
1621}
1622
1623int bdrv_is_locked(BlockDriverState *bs)
1624{
1625 return bs->locked;
1626}
1627
1628/**
1629 * Lock or unlock the media (if it is locked, the user won't be able
1630 * to eject it manually).
1631 */
1632void bdrv_set_locked(BlockDriverState *bs, int locked)
1633{
1634 BlockDriver *drv = bs->drv;
1635
1636 bs->locked = locked;
1637 if (drv && drv->bdrv_set_locked) {
1638 drv->bdrv_set_locked(bs, locked);
1639 }
1640}
985a03b0
TS
1641
1642/* needed for generic scsi interface */
1643
1644int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1645{
1646 BlockDriver *drv = bs->drv;
1647
1648 if (drv && drv->bdrv_ioctl)
1649 return drv->bdrv_ioctl(bs, req, buf);
1650 return -ENOTSUP;
1651}