]> git.proxmox.com Git - qemu.git/blame - block.c
Fix PPC PREP platform, broken by commit 5849
[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 25#include "console.h"
ea2384d3 26#include "block_int.h"
fc01f7e7 27
7674e7bf
FB
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
35
83f64091
FB
36#define SECTOR_BITS 9
37#define SECTOR_SIZE (1 << SECTOR_BITS)
38
90765429
FB
39typedef struct BlockDriverAIOCBSync {
40 BlockDriverAIOCB common;
41 QEMUBH *bh;
42 int ret;
43} BlockDriverAIOCBSync;
44
ce1a14dc
PB
45static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
46 int64_t sector_num, uint8_t *buf, int nb_sectors,
47 BlockDriverCompletionFunc *cb, void *opaque);
48static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
49 int64_t sector_num, const uint8_t *buf, int nb_sectors,
50 BlockDriverCompletionFunc *cb, void *opaque);
83f64091 51static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
5fafdf24 52static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
83f64091
FB
53 uint8_t *buf, int nb_sectors);
54static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
55 const uint8_t *buf, int nb_sectors);
ec530c81 56
7ee930d0
BS
57BlockDriverState *bdrv_first;
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;
7ccfb2eb 193 const 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))
9f7965c7 398 open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK);
83f64091
FB
399 else
400 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
401 ret = drv->bdrv_open(bs, filename, open_flags);
a0a83536 402 if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
9f7965c7 403 ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
83f64091
FB
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);
9f7965c7 430 if (bdrv_open(bs->backing_hd, backing_filename, open_flags) < 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
FB
530 if (drv->bdrv_pread) {
531 int ret, len;
532 len = nb_sectors * 512;
533 ret = drv->bdrv_pread(bs, sector_num * 512, buf, len);
534 if (ret < 0)
535 return ret;
536 else if (ret != len)
19cb3738 537 return -EINVAL;
a36e69dd
TS
538 else {
539 bs->rd_bytes += (unsigned) len;
540 bs->rd_ops ++;
83f64091 541 return 0;
a36e69dd 542 }
83f64091
FB
543 } else {
544 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
33e3963e 545 }
fc01f7e7
FB
546}
547
5fafdf24 548/* Return < 0 if error. Important errors are:
19cb3738
FB
549 -EIO generic I/O error (may happen for all errors)
550 -ENOMEDIUM No media inserted.
551 -EINVAL Invalid sector number or nb_sectors
552 -EACCES Trying to write a read-only device
553*/
5fafdf24 554int bdrv_write(BlockDriverState *bs, int64_t sector_num,
fc01f7e7
FB
555 const uint8_t *buf, int nb_sectors)
556{
83f64091 557 BlockDriver *drv = bs->drv;
19cb3738
FB
558 if (!bs->drv)
559 return -ENOMEDIUM;
0849bf08 560 if (bs->read_only)
19cb3738 561 return -EACCES;
83f64091
FB
562 if (drv->bdrv_pwrite) {
563 int ret, len;
564 len = nb_sectors * 512;
565 ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len);
566 if (ret < 0)
567 return ret;
568 else if (ret != len)
569 return -EIO;
a36e69dd
TS
570 else {
571 bs->wr_bytes += (unsigned) len;
572 bs->wr_ops ++;
83f64091 573 return 0;
a36e69dd 574 }
83f64091
FB
575 } else {
576 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
577 }
578}
579
5fafdf24 580static int bdrv_pread_em(BlockDriverState *bs, int64_t offset,
faea38e7 581 uint8_t *buf, int count1)
83f64091 582{
83f64091
FB
583 uint8_t tmp_buf[SECTOR_SIZE];
584 int len, nb_sectors, count;
585 int64_t sector_num;
586
587 count = count1;
588 /* first read to align to sector start */
589 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
590 if (len > count)
591 len = count;
592 sector_num = offset >> SECTOR_BITS;
593 if (len > 0) {
594 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
595 return -EIO;
596 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
597 count -= len;
598 if (count == 0)
599 return count1;
600 sector_num++;
601 buf += len;
602 }
603
604 /* read the sectors "in place" */
605 nb_sectors = count >> SECTOR_BITS;
606 if (nb_sectors > 0) {
607 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
608 return -EIO;
609 sector_num += nb_sectors;
610 len = nb_sectors << SECTOR_BITS;
611 buf += len;
612 count -= len;
613 }
614
615 /* add data from the last sector */
616 if (count > 0) {
617 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
618 return -EIO;
619 memcpy(buf, tmp_buf, count);
620 }
621 return count1;
622}
623
5fafdf24 624static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset,
faea38e7 625 const uint8_t *buf, int count1)
83f64091 626{
83f64091
FB
627 uint8_t tmp_buf[SECTOR_SIZE];
628 int len, nb_sectors, count;
629 int64_t sector_num;
630
631 count = count1;
632 /* first write to align to sector start */
633 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
634 if (len > count)
635 len = count;
636 sector_num = offset >> SECTOR_BITS;
637 if (len > 0) {
638 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
639 return -EIO;
640 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
641 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
642 return -EIO;
643 count -= len;
644 if (count == 0)
645 return count1;
646 sector_num++;
647 buf += len;
648 }
649
650 /* write the sectors "in place" */
651 nb_sectors = count >> SECTOR_BITS;
652 if (nb_sectors > 0) {
653 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
654 return -EIO;
655 sector_num += nb_sectors;
656 len = nb_sectors << SECTOR_BITS;
657 buf += len;
658 count -= len;
659 }
660
661 /* add data from the last sector */
662 if (count > 0) {
663 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
664 return -EIO;
665 memcpy(tmp_buf, buf, count);
666 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
667 return -EIO;
668 }
669 return count1;
670}
83f64091
FB
671
672/**
5fafdf24 673 * Read with byte offsets (needed only for file protocols)
83f64091 674 */
5fafdf24 675int bdrv_pread(BlockDriverState *bs, int64_t offset,
83f64091
FB
676 void *buf1, int count1)
677{
678 BlockDriver *drv = bs->drv;
679
680 if (!drv)
19cb3738 681 return -ENOMEDIUM;
83f64091 682 if (!drv->bdrv_pread)
faea38e7 683 return bdrv_pread_em(bs, offset, buf1, count1);
83f64091
FB
684 return drv->bdrv_pread(bs, offset, buf1, count1);
685}
686
5fafdf24
TS
687/**
688 * Write with byte offsets (needed only for file protocols)
83f64091 689 */
5fafdf24 690int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
83f64091
FB
691 const void *buf1, int count1)
692{
693 BlockDriver *drv = bs->drv;
694
695 if (!drv)
19cb3738 696 return -ENOMEDIUM;
83f64091 697 if (!drv->bdrv_pwrite)
faea38e7 698 return bdrv_pwrite_em(bs, offset, buf1, count1);
83f64091
FB
699 return drv->bdrv_pwrite(bs, offset, buf1, count1);
700}
701
702/**
703 * Truncate file to 'offset' bytes (needed only for file protocols)
704 */
705int bdrv_truncate(BlockDriverState *bs, int64_t offset)
706{
707 BlockDriver *drv = bs->drv;
708 if (!drv)
19cb3738 709 return -ENOMEDIUM;
83f64091
FB
710 if (!drv->bdrv_truncate)
711 return -ENOTSUP;
712 return drv->bdrv_truncate(bs, offset);
713}
714
715/**
716 * Length of a file in bytes. Return < 0 if error or unknown.
717 */
718int64_t bdrv_getlength(BlockDriverState *bs)
719{
720 BlockDriver *drv = bs->drv;
721 if (!drv)
19cb3738 722 return -ENOMEDIUM;
83f64091
FB
723 if (!drv->bdrv_getlength) {
724 /* legacy mode */
725 return bs->total_sectors * SECTOR_SIZE;
726 }
727 return drv->bdrv_getlength(bs);
fc01f7e7
FB
728}
729
19cb3738 730/* return 0 as number of sectors if no device present or error */
96b8f136 731void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
fc01f7e7 732{
19cb3738
FB
733 int64_t length;
734 length = bdrv_getlength(bs);
735 if (length < 0)
736 length = 0;
737 else
738 length = length >> SECTOR_BITS;
739 *nb_sectors_ptr = length;
fc01f7e7 740}
cf98951b 741
f3d54fc4
AL
742struct partition {
743 uint8_t boot_ind; /* 0x80 - active */
744 uint8_t head; /* starting head */
745 uint8_t sector; /* starting sector */
746 uint8_t cyl; /* starting cylinder */
747 uint8_t sys_ind; /* What partition type */
748 uint8_t end_head; /* end head */
749 uint8_t end_sector; /* end sector */
750 uint8_t end_cyl; /* end cylinder */
751 uint32_t start_sect; /* starting sector counting from 0 */
752 uint32_t nr_sects; /* nr of sectors in partition */
753} __attribute__((packed));
754
755/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
756static int guess_disk_lchs(BlockDriverState *bs,
757 int *pcylinders, int *pheads, int *psectors)
758{
759 uint8_t buf[512];
760 int ret, i, heads, sectors, cylinders;
761 struct partition *p;
762 uint32_t nr_sects;
763 int64_t nb_sectors;
764
765 bdrv_get_geometry(bs, &nb_sectors);
766
767 ret = bdrv_read(bs, 0, buf, 1);
768 if (ret < 0)
769 return -1;
770 /* test msdos magic */
771 if (buf[510] != 0x55 || buf[511] != 0xaa)
772 return -1;
773 for(i = 0; i < 4; i++) {
774 p = ((struct partition *)(buf + 0x1be)) + i;
775 nr_sects = le32_to_cpu(p->nr_sects);
776 if (nr_sects && p->end_head) {
777 /* We make the assumption that the partition terminates on
778 a cylinder boundary */
779 heads = p->end_head + 1;
780 sectors = p->end_sector & 63;
781 if (sectors == 0)
782 continue;
783 cylinders = nb_sectors / (heads * sectors);
784 if (cylinders < 1 || cylinders > 16383)
785 continue;
786 *pheads = heads;
787 *psectors = sectors;
788 *pcylinders = cylinders;
789#if 0
790 printf("guessed geometry: LCHS=%d %d %d\n",
791 cylinders, heads, sectors);
792#endif
793 return 0;
794 }
795 }
796 return -1;
797}
798
799void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
800{
801 int translation, lba_detected = 0;
802 int cylinders, heads, secs;
803 int64_t nb_sectors;
804
805 /* if a geometry hint is available, use it */
806 bdrv_get_geometry(bs, &nb_sectors);
807 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
808 translation = bdrv_get_translation_hint(bs);
809 if (cylinders != 0) {
810 *pcyls = cylinders;
811 *pheads = heads;
812 *psecs = secs;
813 } else {
814 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
815 if (heads > 16) {
816 /* if heads > 16, it means that a BIOS LBA
817 translation was active, so the default
818 hardware geometry is OK */
819 lba_detected = 1;
820 goto default_geometry;
821 } else {
822 *pcyls = cylinders;
823 *pheads = heads;
824 *psecs = secs;
825 /* disable any translation to be in sync with
826 the logical geometry */
827 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
828 bdrv_set_translation_hint(bs,
829 BIOS_ATA_TRANSLATION_NONE);
830 }
831 }
832 } else {
833 default_geometry:
834 /* if no geometry, use a standard physical disk geometry */
835 cylinders = nb_sectors / (16 * 63);
836
837 if (cylinders > 16383)
838 cylinders = 16383;
839 else if (cylinders < 2)
840 cylinders = 2;
841 *pcyls = cylinders;
842 *pheads = 16;
843 *psecs = 63;
844 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
845 if ((*pcyls * *pheads) <= 131072) {
846 bdrv_set_translation_hint(bs,
847 BIOS_ATA_TRANSLATION_LARGE);
848 } else {
849 bdrv_set_translation_hint(bs,
850 BIOS_ATA_TRANSLATION_LBA);
851 }
852 }
853 }
854 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
855 }
856}
857
5fafdf24 858void bdrv_set_geometry_hint(BlockDriverState *bs,
b338082b
FB
859 int cyls, int heads, int secs)
860{
861 bs->cyls = cyls;
862 bs->heads = heads;
863 bs->secs = secs;
864}
865
866void bdrv_set_type_hint(BlockDriverState *bs, int type)
867{
868 bs->type = type;
869 bs->removable = ((type == BDRV_TYPE_CDROM ||
870 type == BDRV_TYPE_FLOPPY));
871}
872
46d4767d
FB
873void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
874{
875 bs->translation = translation;
876}
877
5fafdf24 878void bdrv_get_geometry_hint(BlockDriverState *bs,
b338082b
FB
879 int *pcyls, int *pheads, int *psecs)
880{
881 *pcyls = bs->cyls;
882 *pheads = bs->heads;
883 *psecs = bs->secs;
884}
885
886int bdrv_get_type_hint(BlockDriverState *bs)
887{
888 return bs->type;
889}
890
46d4767d
FB
891int bdrv_get_translation_hint(BlockDriverState *bs)
892{
893 return bs->translation;
894}
895
b338082b
FB
896int bdrv_is_removable(BlockDriverState *bs)
897{
898 return bs->removable;
899}
900
901int bdrv_is_read_only(BlockDriverState *bs)
902{
903 return bs->read_only;
904}
905
985a03b0
TS
906int bdrv_is_sg(BlockDriverState *bs)
907{
908 return bs->sg;
909}
910
19cb3738 911/* XXX: no longer used */
5fafdf24 912void bdrv_set_change_cb(BlockDriverState *bs,
b338082b
FB
913 void (*change_cb)(void *opaque), void *opaque)
914{
915 bs->change_cb = change_cb;
916 bs->change_opaque = opaque;
917}
918
ea2384d3
FB
919int bdrv_is_encrypted(BlockDriverState *bs)
920{
921 if (bs->backing_hd && bs->backing_hd->encrypted)
922 return 1;
923 return bs->encrypted;
924}
925
926int bdrv_set_key(BlockDriverState *bs, const char *key)
927{
928 int ret;
929 if (bs->backing_hd && bs->backing_hd->encrypted) {
930 ret = bdrv_set_key(bs->backing_hd, key);
931 if (ret < 0)
932 return ret;
933 if (!bs->encrypted)
934 return 0;
935 }
936 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
937 return -1;
938 return bs->drv->bdrv_set_key(bs, key);
939}
940
941void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
942{
19cb3738 943 if (!bs->drv) {
ea2384d3
FB
944 buf[0] = '\0';
945 } else {
946 pstrcpy(buf, buf_size, bs->drv->format_name);
947 }
948}
949
5fafdf24 950void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
ea2384d3
FB
951 void *opaque)
952{
953 BlockDriver *drv;
954
955 for (drv = first_drv; drv != NULL; drv = drv->next) {
956 it(opaque, drv->format_name);
957 }
958}
959
b338082b
FB
960BlockDriverState *bdrv_find(const char *name)
961{
962 BlockDriverState *bs;
963
964 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
965 if (!strcmp(name, bs->device_name))
966 return bs;
967 }
968 return NULL;
969}
970
81d0912d
FB
971void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
972{
973 BlockDriverState *bs;
974
975 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
976 it(opaque, bs->device_name);
977 }
978}
979
ea2384d3
FB
980const char *bdrv_get_device_name(BlockDriverState *bs)
981{
982 return bs->device_name;
983}
984
7a6cba61
PB
985void bdrv_flush(BlockDriverState *bs)
986{
987 if (bs->drv->bdrv_flush)
988 bs->drv->bdrv_flush(bs);
989 if (bs->backing_hd)
990 bdrv_flush(bs->backing_hd);
991}
992
c6ca28d6
AL
993void bdrv_flush_all(void)
994{
995 BlockDriverState *bs;
996
997 for (bs = bdrv_first; bs != NULL; bs = bs->next)
998 if (bs->drv && !bdrv_is_read_only(bs) &&
999 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1000 bdrv_flush(bs);
1001}
1002
f58c7b35
TS
1003/*
1004 * Returns true iff the specified sector is present in the disk image. Drivers
1005 * not implementing the functionality are assumed to not support backing files,
1006 * hence all their sectors are reported as allocated.
1007 *
1008 * 'pnum' is set to the number of sectors (including and immediately following
1009 * the specified sector) that are known to be in the same
1010 * allocated/unallocated state.
1011 *
1012 * 'nb_sectors' is the max value 'pnum' should be set to.
1013 */
1014int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1015 int *pnum)
1016{
1017 int64_t n;
1018 if (!bs->drv->bdrv_is_allocated) {
1019 if (sector_num >= bs->total_sectors) {
1020 *pnum = 0;
1021 return 0;
1022 }
1023 n = bs->total_sectors - sector_num;
1024 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1025 return 1;
1026 }
1027 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1028}
1029
b338082b
FB
1030void bdrv_info(void)
1031{
1032 BlockDriverState *bs;
1033
1034 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1035 term_printf("%s:", bs->device_name);
1036 term_printf(" type=");
1037 switch(bs->type) {
1038 case BDRV_TYPE_HD:
1039 term_printf("hd");
1040 break;
1041 case BDRV_TYPE_CDROM:
1042 term_printf("cdrom");
1043 break;
1044 case BDRV_TYPE_FLOPPY:
1045 term_printf("floppy");
1046 break;
1047 }
1048 term_printf(" removable=%d", bs->removable);
1049 if (bs->removable) {
1050 term_printf(" locked=%d", bs->locked);
1051 }
19cb3738 1052 if (bs->drv) {
fef30743
TS
1053 term_printf(" file=");
1054 term_print_filename(bs->filename);
1055 if (bs->backing_file[0] != '\0') {
1056 term_printf(" backing_file=");
1057 term_print_filename(bs->backing_file);
1058 }
b338082b 1059 term_printf(" ro=%d", bs->read_only);
ea2384d3
FB
1060 term_printf(" drv=%s", bs->drv->format_name);
1061 if (bs->encrypted)
1062 term_printf(" encrypted");
b338082b
FB
1063 } else {
1064 term_printf(" [not inserted]");
1065 }
1066 term_printf("\n");
1067 }
1068}
a36e69dd
TS
1069
1070/* The "info blockstats" command. */
1071void bdrv_info_stats (void)
1072{
1073 BlockDriverState *bs;
1074
1075 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1076 term_printf ("%s:"
1077 " rd_bytes=%" PRIu64
1078 " wr_bytes=%" PRIu64
1079 " rd_operations=%" PRIu64
1080 " wr_operations=%" PRIu64
1081 "\n",
1082 bs->device_name,
1083 bs->rd_bytes, bs->wr_bytes,
1084 bs->rd_ops, bs->wr_ops);
1085 }
1086}
ea2384d3 1087
5fafdf24 1088void bdrv_get_backing_filename(BlockDriverState *bs,
83f64091
FB
1089 char *filename, int filename_size)
1090{
1091 if (!bs->backing_hd) {
1092 pstrcpy(filename, filename_size, "");
1093 } else {
1094 pstrcpy(filename, filename_size, bs->backing_file);
1095 }
1096}
1097
5fafdf24 1098int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
faea38e7
FB
1099 const uint8_t *buf, int nb_sectors)
1100{
1101 BlockDriver *drv = bs->drv;
1102 if (!drv)
19cb3738 1103 return -ENOMEDIUM;
faea38e7
FB
1104 if (!drv->bdrv_write_compressed)
1105 return -ENOTSUP;
1106 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1107}
3b46e624 1108
faea38e7
FB
1109int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1110{
1111 BlockDriver *drv = bs->drv;
1112 if (!drv)
19cb3738 1113 return -ENOMEDIUM;
faea38e7
FB
1114 if (!drv->bdrv_get_info)
1115 return -ENOTSUP;
1116 memset(bdi, 0, sizeof(*bdi));
1117 return drv->bdrv_get_info(bs, bdi);
1118}
1119
1120/**************************************************************/
1121/* handling of snapshots */
1122
5fafdf24 1123int bdrv_snapshot_create(BlockDriverState *bs,
faea38e7
FB
1124 QEMUSnapshotInfo *sn_info)
1125{
1126 BlockDriver *drv = bs->drv;
1127 if (!drv)
19cb3738 1128 return -ENOMEDIUM;
faea38e7
FB
1129 if (!drv->bdrv_snapshot_create)
1130 return -ENOTSUP;
1131 return drv->bdrv_snapshot_create(bs, sn_info);
1132}
1133
5fafdf24 1134int bdrv_snapshot_goto(BlockDriverState *bs,
faea38e7
FB
1135 const char *snapshot_id)
1136{
1137 BlockDriver *drv = bs->drv;
1138 if (!drv)
19cb3738 1139 return -ENOMEDIUM;
faea38e7
FB
1140 if (!drv->bdrv_snapshot_goto)
1141 return -ENOTSUP;
1142 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1143}
1144
1145int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1146{
1147 BlockDriver *drv = bs->drv;
1148 if (!drv)
19cb3738 1149 return -ENOMEDIUM;
faea38e7
FB
1150 if (!drv->bdrv_snapshot_delete)
1151 return -ENOTSUP;
1152 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1153}
1154
5fafdf24 1155int bdrv_snapshot_list(BlockDriverState *bs,
faea38e7
FB
1156 QEMUSnapshotInfo **psn_info)
1157{
1158 BlockDriver *drv = bs->drv;
1159 if (!drv)
19cb3738 1160 return -ENOMEDIUM;
faea38e7
FB
1161 if (!drv->bdrv_snapshot_list)
1162 return -ENOTSUP;
1163 return drv->bdrv_snapshot_list(bs, psn_info);
1164}
1165
1166#define NB_SUFFIXES 4
1167
1168char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1169{
1170 static const char suffixes[NB_SUFFIXES] = "KMGT";
1171 int64_t base;
1172 int i;
1173
1174 if (size <= 999) {
1175 snprintf(buf, buf_size, "%" PRId64, size);
1176 } else {
1177 base = 1024;
1178 for(i = 0; i < NB_SUFFIXES; i++) {
1179 if (size < (10 * base)) {
5fafdf24 1180 snprintf(buf, buf_size, "%0.1f%c",
faea38e7
FB
1181 (double)size / base,
1182 suffixes[i]);
1183 break;
1184 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
5fafdf24 1185 snprintf(buf, buf_size, "%" PRId64 "%c",
faea38e7
FB
1186 ((size + (base >> 1)) / base),
1187 suffixes[i]);
1188 break;
1189 }
1190 base = base * 1024;
1191 }
1192 }
1193 return buf;
1194}
1195
1196char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1197{
1198 char buf1[128], date_buf[128], clock_buf[128];
3b9f94e1
FB
1199#ifdef _WIN32
1200 struct tm *ptm;
1201#else
faea38e7 1202 struct tm tm;
3b9f94e1 1203#endif
faea38e7
FB
1204 time_t ti;
1205 int64_t secs;
1206
1207 if (!sn) {
5fafdf24
TS
1208 snprintf(buf, buf_size,
1209 "%-10s%-20s%7s%20s%15s",
faea38e7
FB
1210 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1211 } else {
1212 ti = sn->date_sec;
3b9f94e1
FB
1213#ifdef _WIN32
1214 ptm = localtime(&ti);
1215 strftime(date_buf, sizeof(date_buf),
1216 "%Y-%m-%d %H:%M:%S", ptm);
1217#else
faea38e7
FB
1218 localtime_r(&ti, &tm);
1219 strftime(date_buf, sizeof(date_buf),
1220 "%Y-%m-%d %H:%M:%S", &tm);
3b9f94e1 1221#endif
faea38e7
FB
1222 secs = sn->vm_clock_nsec / 1000000000;
1223 snprintf(clock_buf, sizeof(clock_buf),
1224 "%02d:%02d:%02d.%03d",
1225 (int)(secs / 3600),
1226 (int)((secs / 60) % 60),
5fafdf24 1227 (int)(secs % 60),
faea38e7
FB
1228 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1229 snprintf(buf, buf_size,
5fafdf24 1230 "%-10s%-20s%7s%20s%15s",
faea38e7
FB
1231 sn->id_str, sn->name,
1232 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1233 date_buf,
1234 clock_buf);
1235 }
1236 return buf;
1237}
1238
83f64091 1239
ea2384d3 1240/**************************************************************/
83f64091 1241/* async I/Os */
ea2384d3 1242
ce1a14dc
PB
1243BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1244 uint8_t *buf, int nb_sectors,
1245 BlockDriverCompletionFunc *cb, void *opaque)
83f64091
FB
1246{
1247 BlockDriver *drv = bs->drv;
a36e69dd 1248 BlockDriverAIOCB *ret;
83f64091 1249
19cb3738 1250 if (!drv)
ce1a14dc 1251 return NULL;
3b46e624 1252
a36e69dd
TS
1253 ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1254
1255 if (ret) {
1256 /* Update stats even though technically transfer has not happened. */
1257 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1258 bs->rd_ops ++;
1259 }
1260
1261 return ret;
ea2384d3
FB
1262}
1263
ce1a14dc
PB
1264BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1265 const uint8_t *buf, int nb_sectors,
1266 BlockDriverCompletionFunc *cb, void *opaque)
ea2384d3 1267{
83f64091 1268 BlockDriver *drv = bs->drv;
a36e69dd 1269 BlockDriverAIOCB *ret;
ea2384d3 1270
19cb3738 1271 if (!drv)
ce1a14dc 1272 return NULL;
83f64091 1273 if (bs->read_only)
ce1a14dc 1274 return NULL;
83f64091 1275
a36e69dd
TS
1276 ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1277
1278 if (ret) {
1279 /* Update stats even though technically transfer has not happened. */
1280 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1281 bs->wr_ops ++;
1282 }
1283
1284 return ret;
83f64091
FB
1285}
1286
1287void bdrv_aio_cancel(BlockDriverAIOCB *acb)
83f64091 1288{
ce1a14dc 1289 BlockDriver *drv = acb->bs->drv;
83f64091 1290
ce1a14dc 1291 drv->bdrv_aio_cancel(acb);
83f64091
FB
1292}
1293
ce1a14dc 1294
83f64091
FB
1295/**************************************************************/
1296/* async block device emulation */
1297
ce1a14dc 1298static void bdrv_aio_bh_cb(void *opaque)
83f64091 1299{
ce1a14dc
PB
1300 BlockDriverAIOCBSync *acb = opaque;
1301 acb->common.cb(acb->common.opaque, acb->ret);
1302 qemu_aio_release(acb);
83f64091 1303}
beac80cd 1304
ce1a14dc
PB
1305static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1306 int64_t sector_num, uint8_t *buf, int nb_sectors,
1307 BlockDriverCompletionFunc *cb, void *opaque)
83f64091 1308{
ce1a14dc 1309 BlockDriverAIOCBSync *acb;
83f64091 1310 int ret;
ce1a14dc
PB
1311
1312 acb = qemu_aio_get(bs, cb, opaque);
1313 if (!acb->bh)
1314 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1315 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1316 acb->ret = ret;
1317 qemu_bh_schedule(acb->bh);
1318 return &acb->common;
beac80cd
FB
1319}
1320
ce1a14dc
PB
1321static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1322 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1323 BlockDriverCompletionFunc *cb, void *opaque)
beac80cd 1324{
ce1a14dc 1325 BlockDriverAIOCBSync *acb;
83f64091 1326 int ret;
83f64091 1327
ce1a14dc
PB
1328 acb = qemu_aio_get(bs, cb, opaque);
1329 if (!acb->bh)
1330 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1331 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1332 acb->ret = ret;
1333 qemu_bh_schedule(acb->bh);
1334 return &acb->common;
beac80cd 1335}
beac80cd 1336
ce1a14dc 1337static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
ea2384d3 1338{
ce1a14dc
PB
1339 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1340 qemu_bh_cancel(acb->bh);
1341 qemu_aio_release(acb);
83f64091 1342}
ea2384d3 1343
83f64091
FB
1344/**************************************************************/
1345/* sync block device emulation */
ea2384d3 1346
83f64091
FB
1347static void bdrv_rw_em_cb(void *opaque, int ret)
1348{
1349 *(int *)opaque = ret;
ea2384d3
FB
1350}
1351
83f64091
FB
1352#define NOT_DONE 0x7fffffff
1353
5fafdf24 1354static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
83f64091 1355 uint8_t *buf, int nb_sectors)
7a6cba61 1356{
ce1a14dc
PB
1357 int async_ret;
1358 BlockDriverAIOCB *acb;
83f64091 1359
83f64091 1360 async_ret = NOT_DONE;
5fafdf24 1361 acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
83f64091 1362 bdrv_rw_em_cb, &async_ret);
baf35cb9 1363 if (acb == NULL)
ce1a14dc 1364 return -1;
baf35cb9 1365
83f64091
FB
1366 while (async_ret == NOT_DONE) {
1367 qemu_aio_wait();
1368 }
baf35cb9 1369
83f64091 1370 return async_ret;
7a6cba61
PB
1371}
1372
83f64091
FB
1373static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1374 const uint8_t *buf, int nb_sectors)
1375{
ce1a14dc
PB
1376 int async_ret;
1377 BlockDriverAIOCB *acb;
83f64091 1378
83f64091 1379 async_ret = NOT_DONE;
5fafdf24 1380 acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
83f64091 1381 bdrv_rw_em_cb, &async_ret);
baf35cb9 1382 if (acb == NULL)
ce1a14dc 1383 return -1;
83f64091
FB
1384 while (async_ret == NOT_DONE) {
1385 qemu_aio_wait();
1386 }
83f64091
FB
1387 return async_ret;
1388}
ea2384d3
FB
1389
1390void bdrv_init(void)
1391{
1392 bdrv_register(&bdrv_raw);
19cb3738 1393 bdrv_register(&bdrv_host_device);
ea2384d3
FB
1394#ifndef _WIN32
1395 bdrv_register(&bdrv_cow);
1396#endif
1397 bdrv_register(&bdrv_qcow);
1398 bdrv_register(&bdrv_vmdk);
3c56521b 1399 bdrv_register(&bdrv_cloop);
585d0ed9 1400 bdrv_register(&bdrv_dmg);
a8753c34 1401 bdrv_register(&bdrv_bochs);
6a0f9e82 1402 bdrv_register(&bdrv_vpc);
712e7874 1403 bdrv_register(&bdrv_vvfat);
faea38e7 1404 bdrv_register(&bdrv_qcow2);
6ada7453 1405 bdrv_register(&bdrv_parallels);
75818250 1406 bdrv_register(&bdrv_nbd);
ea2384d3 1407}
ce1a14dc
PB
1408
1409void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1410 void *opaque)
1411{
1412 BlockDriver *drv;
1413 BlockDriverAIOCB *acb;
1414
1415 drv = bs->drv;
1416 if (drv->free_aiocb) {
1417 acb = drv->free_aiocb;
1418 drv->free_aiocb = acb->next;
1419 } else {
1420 acb = qemu_mallocz(drv->aiocb_size);
1421 if (!acb)
1422 return NULL;
1423 }
1424 acb->bs = bs;
1425 acb->cb = cb;
1426 acb->opaque = opaque;
1427 return acb;
1428}
1429
1430void qemu_aio_release(void *p)
1431{
1432 BlockDriverAIOCB *acb = p;
1433 BlockDriver *drv = acb->bs->drv;
1434 acb->next = drv->free_aiocb;
1435 drv->free_aiocb = acb;
1436}
19cb3738
FB
1437
1438/**************************************************************/
1439/* removable device support */
1440
1441/**
1442 * Return TRUE if the media is present
1443 */
1444int bdrv_is_inserted(BlockDriverState *bs)
1445{
1446 BlockDriver *drv = bs->drv;
1447 int ret;
1448 if (!drv)
1449 return 0;
1450 if (!drv->bdrv_is_inserted)
1451 return 1;
1452 ret = drv->bdrv_is_inserted(bs);
1453 return ret;
1454}
1455
1456/**
1457 * Return TRUE if the media changed since the last call to this
5fafdf24 1458 * function. It is currently only used for floppy disks
19cb3738
FB
1459 */
1460int bdrv_media_changed(BlockDriverState *bs)
1461{
1462 BlockDriver *drv = bs->drv;
1463 int ret;
1464
1465 if (!drv || !drv->bdrv_media_changed)
1466 ret = -ENOTSUP;
1467 else
1468 ret = drv->bdrv_media_changed(bs);
1469 if (ret == -ENOTSUP)
1470 ret = bs->media_changed;
1471 bs->media_changed = 0;
1472 return ret;
1473}
1474
1475/**
1476 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1477 */
1478void bdrv_eject(BlockDriverState *bs, int eject_flag)
1479{
1480 BlockDriver *drv = bs->drv;
1481 int ret;
1482
1483 if (!drv || !drv->bdrv_eject) {
1484 ret = -ENOTSUP;
1485 } else {
1486 ret = drv->bdrv_eject(bs, eject_flag);
1487 }
1488 if (ret == -ENOTSUP) {
1489 if (eject_flag)
1490 bdrv_close(bs);
1491 }
1492}
1493
1494int bdrv_is_locked(BlockDriverState *bs)
1495{
1496 return bs->locked;
1497}
1498
1499/**
1500 * Lock or unlock the media (if it is locked, the user won't be able
1501 * to eject it manually).
1502 */
1503void bdrv_set_locked(BlockDriverState *bs, int locked)
1504{
1505 BlockDriver *drv = bs->drv;
1506
1507 bs->locked = locked;
1508 if (drv && drv->bdrv_set_locked) {
1509 drv->bdrv_set_locked(bs, locked);
1510 }
1511}
985a03b0
TS
1512
1513/* needed for generic scsi interface */
1514
1515int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1516{
1517 BlockDriver *drv = bs->drv;
1518
1519 if (drv && drv->bdrv_ioctl)
1520 return drv->bdrv_ioctl(bs, req, buf);
1521 return -ENOTSUP;
1522}