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