blob: 0c2fc6ea842e49acdd2e39959ea78ac52ba06b5f [file] [log] [blame]
bellardfc01f7e2003-06-30 10:03:06 +00001/*
2 * QEMU System Emulator block driver
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
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 */
bellardfc01f7e2003-06-30 10:03:06 +000024#include "vl.h"
bellardea2384d2004-08-01 21:59:26 +000025#include "block_int.h"
bellardfc01f7e2003-06-30 10:03:06 +000026
bellard7674e7b2005-04-26 21:59:26 +000027#ifdef _BSD
28#include <sys/types.h>
29#include <sys/stat.h>
30#include <sys/ioctl.h>
31#include <sys/queue.h>
32#include <sys/disk.h>
33#endif
34
bellard83f64092006-08-01 16:21:11 +000035#define SECTOR_BITS 9
36#define SECTOR_SIZE (1 << SECTOR_BITS)
bellard3b0d4f62005-10-30 18:30:10 +000037
pbrookce1a14d2006-08-07 02:38:06 +000038static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
39 int64_t sector_num, uint8_t *buf, int nb_sectors,
40 BlockDriverCompletionFunc *cb, void *opaque);
41static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
42 int64_t sector_num, const uint8_t *buf, int nb_sectors,
43 BlockDriverCompletionFunc *cb, void *opaque);
bellard83f64092006-08-01 16:21:11 +000044static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
bellard83f64092006-08-01 16:21:11 +000045static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
46 uint8_t *buf, int nb_sectors);
47static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
48 const uint8_t *buf, int nb_sectors);
bellardec530c82006-04-25 22:36:06 +000049
bellardb3380822004-03-14 21:38:54 +000050static BlockDriverState *bdrv_first;
bellardea2384d2004-08-01 21:59:26 +000051static BlockDriver *first_drv;
52
bellard83f64092006-08-01 16:21:11 +000053#ifdef _WIN32
54#define PATH_SEP '\\'
55#else
56#define PATH_SEP '/'
bellard3b0d4f62005-10-30 18:30:10 +000057#endif
58
bellard83f64092006-08-01 16:21:11 +000059int path_is_absolute(const char *path)
60{
61 const char *p;
62 p = strchr(path, ':');
63 if (p)
64 p++;
65 else
66 p = path;
67 return (*p == PATH_SEP);
68}
69
70/* if filename is absolute, just copy it to dest. Otherwise, build a
71 path to it by considering it is relative to base_path. URL are
72 supported. */
73void path_combine(char *dest, int dest_size,
74 const char *base_path,
75 const char *filename)
76{
77 const char *p, *p1;
78 int len;
79
80 if (dest_size <= 0)
81 return;
82 if (path_is_absolute(filename)) {
83 pstrcpy(dest, dest_size, filename);
84 } else {
85 p = strchr(base_path, ':');
86 if (p)
87 p++;
88 else
89 p = base_path;
90 p1 = strrchr(base_path, PATH_SEP);
91 if (p1)
92 p1++;
93 else
94 p1 = base_path;
95 if (p1 > p)
96 p = p1;
97 len = p - base_path;
98 if (len > dest_size - 1)
99 len = dest_size - 1;
100 memcpy(dest, base_path, len);
101 dest[len] = '\0';
102 pstrcat(dest, dest_size, filename);
103 }
104}
105
106
bellardea2384d2004-08-01 21:59:26 +0000107void bdrv_register(BlockDriver *bdrv)
108{
pbrookce1a14d2006-08-07 02:38:06 +0000109 if (!bdrv->bdrv_aio_read) {
bellard83f64092006-08-01 16:21:11 +0000110 /* add AIO emulation layer */
bellard83f64092006-08-01 16:21:11 +0000111 bdrv->bdrv_aio_read = bdrv_aio_read_em;
112 bdrv->bdrv_aio_write = bdrv_aio_write_em;
113 bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
bellard83f64092006-08-01 16:21:11 +0000114 } else if (!bdrv->bdrv_read && !bdrv->bdrv_pread) {
115 /* add synchronous IO emulation layer */
116 bdrv->bdrv_read = bdrv_read_em;
117 bdrv->bdrv_write = bdrv_write_em;
118 }
bellardea2384d2004-08-01 21:59:26 +0000119 bdrv->next = first_drv;
120 first_drv = bdrv;
121}
bellardb3380822004-03-14 21:38:54 +0000122
123/* create a new block device (by default it is empty) */
124BlockDriverState *bdrv_new(const char *device_name)
bellardfc01f7e2003-06-30 10:03:06 +0000125{
bellardb3380822004-03-14 21:38:54 +0000126 BlockDriverState **pbs, *bs;
127
128 bs = qemu_mallocz(sizeof(BlockDriverState));
129 if(!bs)
130 return NULL;
131 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
bellardea2384d2004-08-01 21:59:26 +0000132 if (device_name[0] != '\0') {
133 /* insert at the end */
134 pbs = &bdrv_first;
135 while (*pbs != NULL)
136 pbs = &(*pbs)->next;
137 *pbs = bs;
138 }
bellardb3380822004-03-14 21:38:54 +0000139 return bs;
140}
141
bellardea2384d2004-08-01 21:59:26 +0000142BlockDriver *bdrv_find_format(const char *format_name)
143{
144 BlockDriver *drv1;
145 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
146 if (!strcmp(drv1->format_name, format_name))
147 return drv1;
148 }
149 return NULL;
150}
151
152int bdrv_create(BlockDriver *drv,
153 const char *filename, int64_t size_in_sectors,
154 const char *backing_file, int flags)
155{
156 if (!drv->bdrv_create)
157 return -ENOTSUP;
158 return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
159}
160
bellardd5249392004-08-03 21:14:23 +0000161#ifdef _WIN32
bellard95389c82005-12-18 18:28:15 +0000162void get_tmp_filename(char *filename, int size)
bellardd5249392004-08-03 21:14:23 +0000163{
bellard83f64092006-08-01 16:21:11 +0000164 tmpnam(filename);
bellardd5249392004-08-03 21:14:23 +0000165}
166#else
bellard95389c82005-12-18 18:28:15 +0000167void get_tmp_filename(char *filename, int size)
bellardea2384d2004-08-01 21:59:26 +0000168{
169 int fd;
bellardd5249392004-08-03 21:14:23 +0000170 /* XXX: race condition possible */
bellardea2384d2004-08-01 21:59:26 +0000171 pstrcpy(filename, size, "/tmp/vl.XXXXXX");
172 fd = mkstemp(filename);
173 close(fd);
174}
bellardd5249392004-08-03 21:14:23 +0000175#endif
bellardea2384d2004-08-01 21:59:26 +0000176
bellard83f64092006-08-01 16:21:11 +0000177static BlockDriver *find_protocol(const char *filename)
178{
179 BlockDriver *drv1;
180 char protocol[128];
181 int len;
182 const char *p;
183 p = strchr(filename, ':');
184 if (!p)
185 return &bdrv_raw;
186 len = p - filename;
187 if (len > sizeof(protocol) - 1)
188 len = sizeof(protocol) - 1;
189#ifdef _WIN32
190 if (len == 1) {
191 /* specific win32 case for driver letters */
192 return &bdrv_raw;
193 }
194#endif
195 memcpy(protocol, filename, len);
196 protocol[len] = '\0';
197 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
198 if (drv1->protocol_name &&
199 !strcmp(drv1->protocol_name, protocol))
200 return drv1;
201 }
202 return NULL;
203}
204
bellard7674e7b2005-04-26 21:59:26 +0000205/* XXX: force raw format if block or character device ? It would
206 simplify the BSD case */
bellardea2384d2004-08-01 21:59:26 +0000207static BlockDriver *find_image_format(const char *filename)
208{
bellard83f64092006-08-01 16:21:11 +0000209 int ret, score, score_max;
bellardea2384d2004-08-01 21:59:26 +0000210 BlockDriver *drv1, *drv;
bellard83f64092006-08-01 16:21:11 +0000211 uint8_t buf[2048];
212 BlockDriverState *bs;
bellardea2384d2004-08-01 21:59:26 +0000213
bellard83f64092006-08-01 16:21:11 +0000214 drv = find_protocol(filename);
215 /* no need to test disk image formats for vvfat or host specific
216 devices */
217 if (drv == &bdrv_vvfat)
218 return drv;
219 if (strstart(filename, "/dev/", NULL))
220 return &bdrv_raw;
221
222 ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
223 if (ret < 0)
224 return NULL;
225 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
226 bdrv_delete(bs);
227 if (ret < 0) {
228 return NULL;
229 }
230
bellardea2384d2004-08-01 21:59:26 +0000231 score_max = 0;
232 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
bellard83f64092006-08-01 16:21:11 +0000233 if (drv1->bdrv_probe) {
234 score = drv1->bdrv_probe(buf, ret, filename);
235 if (score > score_max) {
236 score_max = score;
237 drv = drv1;
238 }
bellardea2384d2004-08-01 21:59:26 +0000239 }
240 }
241 return drv;
242}
243
bellard83f64092006-08-01 16:21:11 +0000244int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
bellardb3380822004-03-14 21:38:54 +0000245{
bellard83f64092006-08-01 16:21:11 +0000246 BlockDriverState *bs;
247 int ret;
248
249 bs = bdrv_new("");
250 if (!bs)
251 return -ENOMEM;
252 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
253 if (ret < 0) {
254 bdrv_delete(bs);
255 return ret;
bellard3b0d4f62005-10-30 18:30:10 +0000256 }
bellard83f64092006-08-01 16:21:11 +0000257 *pbs = bs;
258 return 0;
bellardea2384d2004-08-01 21:59:26 +0000259}
bellardfc01f7e2003-06-30 10:03:06 +0000260
bellard83f64092006-08-01 16:21:11 +0000261int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
262{
263 return bdrv_open2(bs, filename, flags, NULL);
264}
265
266int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
bellardea2384d2004-08-01 21:59:26 +0000267 BlockDriver *drv)
268{
bellard83f64092006-08-01 16:21:11 +0000269 int ret, open_flags;
bellardea2384d2004-08-01 21:59:26 +0000270 char tmp_filename[1024];
bellard83f64092006-08-01 16:21:11 +0000271 char backing_filename[1024];
bellard33e39632003-07-06 17:15:21 +0000272
bellardea2384d2004-08-01 21:59:26 +0000273 bs->read_only = 0;
274 bs->is_temporary = 0;
275 bs->encrypted = 0;
bellard712e7872005-04-28 21:09:32 +0000276
bellard83f64092006-08-01 16:21:11 +0000277 if (flags & BDRV_O_SNAPSHOT) {
bellardea2384d2004-08-01 21:59:26 +0000278 BlockDriverState *bs1;
279 int64_t total_size;
280
281 /* if snapshot, we create a temporary backing file and open it
282 instead of opening 'filename' directly */
283
284 /* if there is a backing file, use it */
285 bs1 = bdrv_new("");
286 if (!bs1) {
bellard83f64092006-08-01 16:21:11 +0000287 return -ENOMEM;
bellardea2384d2004-08-01 21:59:26 +0000288 }
289 if (bdrv_open(bs1, filename, 0) < 0) {
290 bdrv_delete(bs1);
291 return -1;
292 }
bellard83f64092006-08-01 16:21:11 +0000293 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
bellardea2384d2004-08-01 21:59:26 +0000294 bdrv_delete(bs1);
295
296 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
bellardd15a7712006-08-06 13:35:09 +0000297 if (bdrv_create(&bdrv_qcow2, tmp_filename,
bellardea2384d2004-08-01 21:59:26 +0000298 total_size, filename, 0) < 0) {
299 return -1;
300 }
301 filename = tmp_filename;
302 bs->is_temporary = 1;
303 }
bellard712e7872005-04-28 21:09:32 +0000304
bellardea2384d2004-08-01 21:59:26 +0000305 pstrcpy(bs->filename, sizeof(bs->filename), filename);
bellard83f64092006-08-01 16:21:11 +0000306 if (flags & BDRV_O_FILE) {
307 drv = find_protocol(filename);
bellardea2384d2004-08-01 21:59:26 +0000308 if (!drv)
bellard83f64092006-08-01 16:21:11 +0000309 return -ENOENT;
310 } else {
311 if (!drv) {
312 drv = find_image_format(filename);
313 if (!drv)
314 return -1;
315 }
bellardea2384d2004-08-01 21:59:26 +0000316 }
317 bs->drv = drv;
318 bs->opaque = qemu_mallocz(drv->instance_size);
319 if (bs->opaque == NULL && drv->instance_size > 0)
320 return -1;
bellard83f64092006-08-01 16:21:11 +0000321 /* Note: for compatibility, we open disk image files as RDWR, and
322 RDONLY as fallback */
323 if (!(flags & BDRV_O_FILE))
324 open_flags = BDRV_O_RDWR;
325 else
326 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
327 ret = drv->bdrv_open(bs, filename, open_flags);
328 if (ret == -EACCES && !(flags & BDRV_O_FILE)) {
329 ret = drv->bdrv_open(bs, filename, BDRV_O_RDONLY);
330 bs->read_only = 1;
331 }
bellardea2384d2004-08-01 21:59:26 +0000332 if (ret < 0) {
333 qemu_free(bs->opaque);
bellard83f64092006-08-01 16:21:11 +0000334 return ret;
bellardea2384d2004-08-01 21:59:26 +0000335 }
bellardd15a7712006-08-06 13:35:09 +0000336 if (drv->bdrv_getlength) {
337 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
338 }
bellardea2384d2004-08-01 21:59:26 +0000339#ifndef _WIN32
340 if (bs->is_temporary) {
341 unlink(filename);
342 }
343#endif
bellard83f64092006-08-01 16:21:11 +0000344 if (bs->backing_file[0] != '\0') {
bellardea2384d2004-08-01 21:59:26 +0000345 /* if there is a backing file, use it */
346 bs->backing_hd = bdrv_new("");
347 if (!bs->backing_hd) {
348 fail:
349 bdrv_close(bs);
350 return -1;
351 }
bellard83f64092006-08-01 16:21:11 +0000352 path_combine(backing_filename, sizeof(backing_filename),
353 filename, bs->backing_file);
354 if (bdrv_open(bs->backing_hd, backing_filename, 0) < 0)
bellardea2384d2004-08-01 21:59:26 +0000355 goto fail;
356 }
357
bellardb3380822004-03-14 21:38:54 +0000358 bs->inserted = 1;
359
360 /* call the change callback */
361 if (bs->change_cb)
362 bs->change_cb(bs->change_opaque);
363
364 return 0;
bellardfc01f7e2003-06-30 10:03:06 +0000365}
366
367void bdrv_close(BlockDriverState *bs)
368{
bellardb3380822004-03-14 21:38:54 +0000369 if (bs->inserted) {
bellardea2384d2004-08-01 21:59:26 +0000370 if (bs->backing_hd)
371 bdrv_delete(bs->backing_hd);
372 bs->drv->bdrv_close(bs);
373 qemu_free(bs->opaque);
374#ifdef _WIN32
375 if (bs->is_temporary) {
376 unlink(bs->filename);
377 }
bellard67b915a2004-03-31 23:37:16 +0000378#endif
bellardea2384d2004-08-01 21:59:26 +0000379 bs->opaque = NULL;
380 bs->drv = NULL;
bellardb3380822004-03-14 21:38:54 +0000381 bs->inserted = 0;
382
383 /* call the change callback */
384 if (bs->change_cb)
385 bs->change_cb(bs->change_opaque);
386 }
387}
388
389void bdrv_delete(BlockDriverState *bs)
390{
bellardea2384d2004-08-01 21:59:26 +0000391 /* XXX: remove the driver list */
bellardb3380822004-03-14 21:38:54 +0000392 bdrv_close(bs);
393 qemu_free(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000394}
395
bellard33e39632003-07-06 17:15:21 +0000396/* commit COW file into the raw image */
397int bdrv_commit(BlockDriverState *bs)
398{
bellard83f64092006-08-01 16:21:11 +0000399 int64_t i, total_sectors;
bellardea2384d2004-08-01 21:59:26 +0000400 int n, j;
401 unsigned char sector[512];
bellard33e39632003-07-06 17:15:21 +0000402
bellardb3380822004-03-14 21:38:54 +0000403 if (!bs->inserted)
bellardea2384d2004-08-01 21:59:26 +0000404 return -ENOENT;
bellard33e39632003-07-06 17:15:21 +0000405
406 if (bs->read_only) {
bellardea2384d2004-08-01 21:59:26 +0000407 return -EACCES;
bellard33e39632003-07-06 17:15:21 +0000408 }
409
bellardea2384d2004-08-01 21:59:26 +0000410 if (!bs->backing_hd) {
411 return -ENOTSUP;
bellard33e39632003-07-06 17:15:21 +0000412 }
bellardea2384d2004-08-01 21:59:26 +0000413
bellard83f64092006-08-01 16:21:11 +0000414 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
415 for (i = 0; i < total_sectors;) {
bellardea2384d2004-08-01 21:59:26 +0000416 if (bs->drv->bdrv_is_allocated(bs, i, 65536, &n)) {
417 for(j = 0; j < n; j++) {
418 if (bdrv_read(bs, i, sector, 1) != 0) {
419 return -EIO;
420 }
421
422 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
423 return -EIO;
424 }
425 i++;
426 }
427 } else {
428 i += n;
429 }
430 }
bellard95389c82005-12-18 18:28:15 +0000431
432 if (bs->drv->bdrv_make_empty)
433 return bs->drv->bdrv_make_empty(bs);
434
bellard33e39632003-07-06 17:15:21 +0000435 return 0;
436}
437
bellard83f64092006-08-01 16:21:11 +0000438/* return < 0 if error */
bellardfc01f7e2003-06-30 10:03:06 +0000439int bdrv_read(BlockDriverState *bs, int64_t sector_num,
440 uint8_t *buf, int nb_sectors)
441{
bellardea2384d2004-08-01 21:59:26 +0000442 BlockDriver *drv = bs->drv;
443
bellardb3380822004-03-14 21:38:54 +0000444 if (!bs->inserted)
445 return -1;
446
bellard83f64092006-08-01 16:21:11 +0000447 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
bellardcf989512004-02-16 21:56:36 +0000448 memcpy(buf, bs->boot_sector_data, 512);
bellard83f64092006-08-01 16:21:11 +0000449 sector_num++;
450 nb_sectors--;
451 buf += 512;
452 if (nb_sectors == 0)
453 return 0;
bellard33e39632003-07-06 17:15:21 +0000454 }
bellard83f64092006-08-01 16:21:11 +0000455 if (drv->bdrv_pread) {
456 int ret, len;
457 len = nb_sectors * 512;
458 ret = drv->bdrv_pread(bs, sector_num * 512, buf, len);
459 if (ret < 0)
460 return ret;
461 else if (ret != len)
462 return -EIO;
463 else
464 return 0;
465 } else {
466 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
467 }
bellardfc01f7e2003-06-30 10:03:06 +0000468}
469
bellard83f64092006-08-01 16:21:11 +0000470/* return < 0 if error */
bellardfc01f7e2003-06-30 10:03:06 +0000471int bdrv_write(BlockDriverState *bs, int64_t sector_num,
472 const uint8_t *buf, int nb_sectors)
473{
bellard83f64092006-08-01 16:21:11 +0000474 BlockDriver *drv = bs->drv;
bellardb3380822004-03-14 21:38:54 +0000475 if (!bs->inserted)
476 return -1;
bellard0849bf02003-06-30 23:17:31 +0000477 if (bs->read_only)
478 return -1;
bellard79639d42005-11-26 10:58:41 +0000479 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
480 memcpy(bs->boot_sector_data, buf, 512);
481 }
bellard83f64092006-08-01 16:21:11 +0000482 if (drv->bdrv_pwrite) {
483 int ret, len;
484 len = nb_sectors * 512;
485 ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len);
486 if (ret < 0)
487 return ret;
488 else if (ret != len)
489 return -EIO;
490 else
491 return 0;
492 } else {
493 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
494 }
495}
496
bellard83f64092006-08-01 16:21:11 +0000497/* not necessary now */
498static int bdrv_pread_em(BlockDriverState *bs, int64_t offset,
bellardfaea38e2006-08-05 21:31:00 +0000499 uint8_t *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000500{
bellard83f64092006-08-01 16:21:11 +0000501 uint8_t tmp_buf[SECTOR_SIZE];
502 int len, nb_sectors, count;
503 int64_t sector_num;
504
505 count = count1;
506 /* first read to align to sector start */
507 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
508 if (len > count)
509 len = count;
510 sector_num = offset >> SECTOR_BITS;
511 if (len > 0) {
512 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
513 return -EIO;
514 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
515 count -= len;
516 if (count == 0)
517 return count1;
518 sector_num++;
519 buf += len;
520 }
521
522 /* read the sectors "in place" */
523 nb_sectors = count >> SECTOR_BITS;
524 if (nb_sectors > 0) {
525 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
526 return -EIO;
527 sector_num += nb_sectors;
528 len = nb_sectors << SECTOR_BITS;
529 buf += len;
530 count -= len;
531 }
532
533 /* add data from the last sector */
534 if (count > 0) {
535 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
536 return -EIO;
537 memcpy(buf, tmp_buf, count);
538 }
539 return count1;
540}
541
542static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset,
bellardfaea38e2006-08-05 21:31:00 +0000543 const uint8_t *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000544{
bellard83f64092006-08-01 16:21:11 +0000545 uint8_t tmp_buf[SECTOR_SIZE];
546 int len, nb_sectors, count;
547 int64_t sector_num;
548
549 count = count1;
550 /* first write to align to sector start */
551 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
552 if (len > count)
553 len = count;
554 sector_num = offset >> SECTOR_BITS;
555 if (len > 0) {
556 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
557 return -EIO;
558 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
559 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
560 return -EIO;
561 count -= len;
562 if (count == 0)
563 return count1;
564 sector_num++;
565 buf += len;
566 }
567
568 /* write the sectors "in place" */
569 nb_sectors = count >> SECTOR_BITS;
570 if (nb_sectors > 0) {
571 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
572 return -EIO;
573 sector_num += nb_sectors;
574 len = nb_sectors << SECTOR_BITS;
575 buf += len;
576 count -= len;
577 }
578
579 /* add data from the last sector */
580 if (count > 0) {
581 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
582 return -EIO;
583 memcpy(tmp_buf, buf, count);
584 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
585 return -EIO;
586 }
587 return count1;
588}
bellard83f64092006-08-01 16:21:11 +0000589
590/**
591 * Read with byte offsets (needed only for file protocols)
592 */
593int bdrv_pread(BlockDriverState *bs, int64_t offset,
594 void *buf1, int count1)
595{
596 BlockDriver *drv = bs->drv;
597
598 if (!drv)
599 return -ENOENT;
600 if (!drv->bdrv_pread)
bellardfaea38e2006-08-05 21:31:00 +0000601 return bdrv_pread_em(bs, offset, buf1, count1);
bellard83f64092006-08-01 16:21:11 +0000602 return drv->bdrv_pread(bs, offset, buf1, count1);
603}
604
605/**
606 * Write with byte offsets (needed only for file protocols)
607 */
608int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
609 const void *buf1, int count1)
610{
611 BlockDriver *drv = bs->drv;
612
613 if (!drv)
614 return -ENOENT;
615 if (!drv->bdrv_pwrite)
bellardfaea38e2006-08-05 21:31:00 +0000616 return bdrv_pwrite_em(bs, offset, buf1, count1);
bellard83f64092006-08-01 16:21:11 +0000617 return drv->bdrv_pwrite(bs, offset, buf1, count1);
618}
619
620/**
621 * Truncate file to 'offset' bytes (needed only for file protocols)
622 */
623int bdrv_truncate(BlockDriverState *bs, int64_t offset)
624{
625 BlockDriver *drv = bs->drv;
626 if (!drv)
627 return -ENOENT;
628 if (!drv->bdrv_truncate)
629 return -ENOTSUP;
630 return drv->bdrv_truncate(bs, offset);
631}
632
633/**
634 * Length of a file in bytes. Return < 0 if error or unknown.
635 */
636int64_t bdrv_getlength(BlockDriverState *bs)
637{
638 BlockDriver *drv = bs->drv;
639 if (!drv)
640 return -ENOENT;
641 if (!drv->bdrv_getlength) {
642 /* legacy mode */
643 return bs->total_sectors * SECTOR_SIZE;
644 }
645 return drv->bdrv_getlength(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000646}
647
648void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr)
649{
bellardd15a7712006-08-06 13:35:09 +0000650 *nb_sectors_ptr = bs->total_sectors;
bellardfc01f7e2003-06-30 10:03:06 +0000651}
bellardcf989512004-02-16 21:56:36 +0000652
653/* force a given boot sector. */
654void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size)
655{
656 bs->boot_sector_enabled = 1;
657 if (size > 512)
658 size = 512;
659 memcpy(bs->boot_sector_data, data, size);
660 memset(bs->boot_sector_data + size, 0, 512 - size);
661}
bellardb3380822004-03-14 21:38:54 +0000662
663void bdrv_set_geometry_hint(BlockDriverState *bs,
664 int cyls, int heads, int secs)
665{
666 bs->cyls = cyls;
667 bs->heads = heads;
668 bs->secs = secs;
669}
670
671void bdrv_set_type_hint(BlockDriverState *bs, int type)
672{
673 bs->type = type;
674 bs->removable = ((type == BDRV_TYPE_CDROM ||
675 type == BDRV_TYPE_FLOPPY));
676}
677
bellard46d47672004-11-16 01:45:27 +0000678void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
679{
680 bs->translation = translation;
681}
682
bellardb3380822004-03-14 21:38:54 +0000683void bdrv_get_geometry_hint(BlockDriverState *bs,
684 int *pcyls, int *pheads, int *psecs)
685{
686 *pcyls = bs->cyls;
687 *pheads = bs->heads;
688 *psecs = bs->secs;
689}
690
691int bdrv_get_type_hint(BlockDriverState *bs)
692{
693 return bs->type;
694}
695
bellard46d47672004-11-16 01:45:27 +0000696int bdrv_get_translation_hint(BlockDriverState *bs)
697{
698 return bs->translation;
699}
700
bellardb3380822004-03-14 21:38:54 +0000701int bdrv_is_removable(BlockDriverState *bs)
702{
703 return bs->removable;
704}
705
706int bdrv_is_read_only(BlockDriverState *bs)
707{
708 return bs->read_only;
709}
710
711int bdrv_is_inserted(BlockDriverState *bs)
712{
713 return bs->inserted;
714}
715
716int bdrv_is_locked(BlockDriverState *bs)
717{
718 return bs->locked;
719}
720
721void bdrv_set_locked(BlockDriverState *bs, int locked)
722{
723 bs->locked = locked;
724}
725
726void bdrv_set_change_cb(BlockDriverState *bs,
727 void (*change_cb)(void *opaque), void *opaque)
728{
729 bs->change_cb = change_cb;
730 bs->change_opaque = opaque;
731}
732
bellardea2384d2004-08-01 21:59:26 +0000733int bdrv_is_encrypted(BlockDriverState *bs)
734{
735 if (bs->backing_hd && bs->backing_hd->encrypted)
736 return 1;
737 return bs->encrypted;
738}
739
740int bdrv_set_key(BlockDriverState *bs, const char *key)
741{
742 int ret;
743 if (bs->backing_hd && bs->backing_hd->encrypted) {
744 ret = bdrv_set_key(bs->backing_hd, key);
745 if (ret < 0)
746 return ret;
747 if (!bs->encrypted)
748 return 0;
749 }
750 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
751 return -1;
752 return bs->drv->bdrv_set_key(bs, key);
753}
754
755void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
756{
757 if (!bs->inserted || !bs->drv) {
758 buf[0] = '\0';
759 } else {
760 pstrcpy(buf, buf_size, bs->drv->format_name);
761 }
762}
763
764void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
765 void *opaque)
766{
767 BlockDriver *drv;
768
769 for (drv = first_drv; drv != NULL; drv = drv->next) {
770 it(opaque, drv->format_name);
771 }
772}
773
bellardb3380822004-03-14 21:38:54 +0000774BlockDriverState *bdrv_find(const char *name)
775{
776 BlockDriverState *bs;
777
778 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
779 if (!strcmp(name, bs->device_name))
780 return bs;
781 }
782 return NULL;
783}
784
bellard81d09122004-07-14 17:21:37 +0000785void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
786{
787 BlockDriverState *bs;
788
789 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
790 it(opaque, bs->device_name);
791 }
792}
793
bellardea2384d2004-08-01 21:59:26 +0000794const char *bdrv_get_device_name(BlockDriverState *bs)
795{
796 return bs->device_name;
797}
798
pbrook7a6cba62006-06-04 11:39:07 +0000799void bdrv_flush(BlockDriverState *bs)
800{
801 if (bs->drv->bdrv_flush)
802 bs->drv->bdrv_flush(bs);
803 if (bs->backing_hd)
804 bdrv_flush(bs->backing_hd);
805}
806
bellardb3380822004-03-14 21:38:54 +0000807void bdrv_info(void)
808{
809 BlockDriverState *bs;
810
811 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
812 term_printf("%s:", bs->device_name);
813 term_printf(" type=");
814 switch(bs->type) {
815 case BDRV_TYPE_HD:
816 term_printf("hd");
817 break;
818 case BDRV_TYPE_CDROM:
819 term_printf("cdrom");
820 break;
821 case BDRV_TYPE_FLOPPY:
822 term_printf("floppy");
823 break;
824 }
825 term_printf(" removable=%d", bs->removable);
826 if (bs->removable) {
827 term_printf(" locked=%d", bs->locked);
828 }
829 if (bs->inserted) {
830 term_printf(" file=%s", bs->filename);
bellardea2384d2004-08-01 21:59:26 +0000831 if (bs->backing_file[0] != '\0')
832 term_printf(" backing_file=%s", bs->backing_file);
bellardb3380822004-03-14 21:38:54 +0000833 term_printf(" ro=%d", bs->read_only);
bellardea2384d2004-08-01 21:59:26 +0000834 term_printf(" drv=%s", bs->drv->format_name);
835 if (bs->encrypted)
836 term_printf(" encrypted");
bellardb3380822004-03-14 21:38:54 +0000837 } else {
838 term_printf(" [not inserted]");
839 }
840 term_printf("\n");
841 }
842}
bellardea2384d2004-08-01 21:59:26 +0000843
bellard83f64092006-08-01 16:21:11 +0000844void bdrv_get_backing_filename(BlockDriverState *bs,
845 char *filename, int filename_size)
bellardea2384d2004-08-01 21:59:26 +0000846{
bellard83f64092006-08-01 16:21:11 +0000847 if (!bs->backing_hd) {
848 pstrcpy(filename, filename_size, "");
849 } else {
850 pstrcpy(filename, filename_size, bs->backing_file);
851 }
bellardea2384d2004-08-01 21:59:26 +0000852}
853
bellardfaea38e2006-08-05 21:31:00 +0000854int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
855 const uint8_t *buf, int nb_sectors)
856{
857 BlockDriver *drv = bs->drv;
858 if (!drv)
859 return -ENOENT;
860 if (!drv->bdrv_write_compressed)
861 return -ENOTSUP;
862 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
863}
864
865int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
866{
867 BlockDriver *drv = bs->drv;
868 if (!drv)
869 return -ENOENT;
870 if (!drv->bdrv_get_info)
871 return -ENOTSUP;
872 memset(bdi, 0, sizeof(*bdi));
873 return drv->bdrv_get_info(bs, bdi);
874}
875
876/**************************************************************/
877/* handling of snapshots */
878
879int bdrv_snapshot_create(BlockDriverState *bs,
880 QEMUSnapshotInfo *sn_info)
881{
882 BlockDriver *drv = bs->drv;
883 if (!drv)
884 return -ENOENT;
885 if (!drv->bdrv_snapshot_create)
886 return -ENOTSUP;
887 return drv->bdrv_snapshot_create(bs, sn_info);
888}
889
890int bdrv_snapshot_goto(BlockDriverState *bs,
891 const char *snapshot_id)
892{
893 BlockDriver *drv = bs->drv;
894 if (!drv)
895 return -ENOENT;
896 if (!drv->bdrv_snapshot_goto)
897 return -ENOTSUP;
898 return drv->bdrv_snapshot_goto(bs, snapshot_id);
899}
900
901int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
902{
903 BlockDriver *drv = bs->drv;
904 if (!drv)
905 return -ENOENT;
906 if (!drv->bdrv_snapshot_delete)
907 return -ENOTSUP;
908 return drv->bdrv_snapshot_delete(bs, snapshot_id);
909}
910
911int bdrv_snapshot_list(BlockDriverState *bs,
912 QEMUSnapshotInfo **psn_info)
913{
914 BlockDriver *drv = bs->drv;
915 if (!drv)
916 return -ENOENT;
917 if (!drv->bdrv_snapshot_list)
918 return -ENOTSUP;
919 return drv->bdrv_snapshot_list(bs, psn_info);
920}
921
922#define NB_SUFFIXES 4
923
924char *get_human_readable_size(char *buf, int buf_size, int64_t size)
925{
926 static const char suffixes[NB_SUFFIXES] = "KMGT";
927 int64_t base;
928 int i;
929
930 if (size <= 999) {
931 snprintf(buf, buf_size, "%" PRId64, size);
932 } else {
933 base = 1024;
934 for(i = 0; i < NB_SUFFIXES; i++) {
935 if (size < (10 * base)) {
936 snprintf(buf, buf_size, "%0.1f%c",
937 (double)size / base,
938 suffixes[i]);
939 break;
940 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
941 snprintf(buf, buf_size, "%" PRId64 "%c",
942 ((size + (base >> 1)) / base),
943 suffixes[i]);
944 break;
945 }
946 base = base * 1024;
947 }
948 }
949 return buf;
950}
951
952char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
953{
954 char buf1[128], date_buf[128], clock_buf[128];
955 struct tm tm;
956 time_t ti;
957 int64_t secs;
958
959 if (!sn) {
960 snprintf(buf, buf_size,
961 "%-10s%-20s%7s%20s%15s",
962 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
963 } else {
964 ti = sn->date_sec;
pbrookce1a14d2006-08-07 02:38:06 +0000965#ifndef _WIN32
bellardfaea38e2006-08-05 21:31:00 +0000966 localtime_r(&ti, &tm);
pbrookce1a14d2006-08-07 02:38:06 +0000967#endif
bellardfaea38e2006-08-05 21:31:00 +0000968 strftime(date_buf, sizeof(date_buf),
969 "%Y-%m-%d %H:%M:%S", &tm);
970 secs = sn->vm_clock_nsec / 1000000000;
971 snprintf(clock_buf, sizeof(clock_buf),
972 "%02d:%02d:%02d.%03d",
973 (int)(secs / 3600),
974 (int)((secs / 60) % 60),
975 (int)(secs % 60),
976 (int)((sn->vm_clock_nsec / 1000000) % 1000));
977 snprintf(buf, buf_size,
978 "%-10s%-20s%7s%20s%15s",
979 sn->id_str, sn->name,
980 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
981 date_buf,
982 clock_buf);
983 }
984 return buf;
985}
986
bellardea2384d2004-08-01 21:59:26 +0000987
bellard83f64092006-08-01 16:21:11 +0000988/**************************************************************/
989/* async I/Os */
990
pbrookce1a14d2006-08-07 02:38:06 +0000991BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
992 uint8_t *buf, int nb_sectors,
993 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +0000994{
995 BlockDriver *drv = bs->drv;
bellard83f64092006-08-01 16:21:11 +0000996
997 if (!bs->inserted)
pbrookce1a14d2006-08-07 02:38:06 +0000998 return NULL;
bellard83f64092006-08-01 16:21:11 +0000999
1000 /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
1001 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1002 memcpy(buf, bs->boot_sector_data, 512);
1003 sector_num++;
1004 nb_sectors--;
1005 buf += 512;
1006 }
1007
pbrookce1a14d2006-08-07 02:38:06 +00001008 return drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
bellard83f64092006-08-01 16:21:11 +00001009}
1010
pbrookce1a14d2006-08-07 02:38:06 +00001011BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1012 const uint8_t *buf, int nb_sectors,
1013 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001014{
bellard83f64092006-08-01 16:21:11 +00001015 BlockDriver *drv = bs->drv;
1016
1017 if (!bs->inserted)
pbrookce1a14d2006-08-07 02:38:06 +00001018 return NULL;
bellard83f64092006-08-01 16:21:11 +00001019 if (bs->read_only)
pbrookce1a14d2006-08-07 02:38:06 +00001020 return NULL;
bellard83f64092006-08-01 16:21:11 +00001021 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1022 memcpy(bs->boot_sector_data, buf, 512);
bellardea2384d2004-08-01 21:59:26 +00001023 }
bellard83f64092006-08-01 16:21:11 +00001024
pbrookce1a14d2006-08-07 02:38:06 +00001025 return drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
bellard83f64092006-08-01 16:21:11 +00001026}
1027
1028void bdrv_aio_cancel(BlockDriverAIOCB *acb)
pbrookce1a14d2006-08-07 02:38:06 +00001029{
1030 BlockDriver *drv = acb->bs->drv;
bellard83f64092006-08-01 16:21:11 +00001031
1032 drv->bdrv_aio_cancel(acb);
bellard83f64092006-08-01 16:21:11 +00001033}
1034
pbrookce1a14d2006-08-07 02:38:06 +00001035
bellard83f64092006-08-01 16:21:11 +00001036/**************************************************************/
1037/* async block device emulation */
1038
1039#ifdef QEMU_TOOL
pbrookce1a14d2006-08-07 02:38:06 +00001040static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1041 int64_t sector_num, uint8_t *buf, int nb_sectors,
1042 BlockDriverCompletionFunc *cb, void *opaque)
bellardea2384d2004-08-01 21:59:26 +00001043{
bellardea2384d2004-08-01 21:59:26 +00001044 int ret;
pbrookce1a14d2006-08-07 02:38:06 +00001045 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1046 cb(opaque, ret);
1047 return NULL;
bellardea2384d2004-08-01 21:59:26 +00001048}
1049
pbrookce1a14d2006-08-07 02:38:06 +00001050static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1051 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1052 BlockDriverCompletionFunc *cb, void *opaque)
bellardea2384d2004-08-01 21:59:26 +00001053{
bellardea2384d2004-08-01 21:59:26 +00001054 int ret;
pbrookce1a14d2006-08-07 02:38:06 +00001055 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1056 cb(opaque, ret);
1057 return NULL;
bellardea2384d2004-08-01 21:59:26 +00001058}
1059
bellard83f64092006-08-01 16:21:11 +00001060static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb)
bellardea2384d2004-08-01 21:59:26 +00001061{
bellardea2384d2004-08-01 21:59:26 +00001062}
bellardbeac80c2006-06-26 20:08:57 +00001063#else
bellard83f64092006-08-01 16:21:11 +00001064typedef struct BlockDriverAIOCBSync {
pbrookce1a14d2006-08-07 02:38:06 +00001065 BlockDriverAIOCB common;
bellard83f64092006-08-01 16:21:11 +00001066 QEMUBH *bh;
1067 int ret;
1068} BlockDriverAIOCBSync;
1069
pbrookce1a14d2006-08-07 02:38:06 +00001070static BlockDriverAIOCBSync *free_acb = NULL;
1071
bellard83f64092006-08-01 16:21:11 +00001072static void bdrv_aio_bh_cb(void *opaque)
bellardbeac80c2006-06-26 20:08:57 +00001073{
pbrookce1a14d2006-08-07 02:38:06 +00001074 BlockDriverAIOCBSync *acb = opaque;
1075 acb->common.cb(acb->common.opaque, acb->ret);
1076 qemu_aio_release(acb);
bellardbeac80c2006-06-26 20:08:57 +00001077}
bellardbeac80c2006-06-26 20:08:57 +00001078
pbrookce1a14d2006-08-07 02:38:06 +00001079static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1080 int64_t sector_num, uint8_t *buf, int nb_sectors,
1081 BlockDriverCompletionFunc *cb, void *opaque)
bellardea2384d2004-08-01 21:59:26 +00001082{
pbrookce1a14d2006-08-07 02:38:06 +00001083 BlockDriverAIOCBSync *acb;
bellard83f64092006-08-01 16:21:11 +00001084 int ret;
pbrookce1a14d2006-08-07 02:38:06 +00001085
1086 acb = qemu_aio_get(bs, cb, opaque);
1087 if (!acb->bh)
1088 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1089 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1090 acb->ret = ret;
1091 qemu_bh_schedule(acb->bh);
1092 return &acb->common;
pbrook7a6cba62006-06-04 11:39:07 +00001093}
1094
pbrookce1a14d2006-08-07 02:38:06 +00001095static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1096 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1097 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001098{
pbrookce1a14d2006-08-07 02:38:06 +00001099 BlockDriverAIOCBSync *acb;
bellard83f64092006-08-01 16:21:11 +00001100 int ret;
pbrookce1a14d2006-08-07 02:38:06 +00001101
1102 acb = qemu_aio_get(bs, cb, opaque);
1103 if (!acb->bh)
1104 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1105 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1106 acb->ret = ret;
1107 qemu_bh_schedule(acb->bh);
1108 return &acb->common;
bellard83f64092006-08-01 16:21:11 +00001109}
1110
pbrookce1a14d2006-08-07 02:38:06 +00001111static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
bellard83f64092006-08-01 16:21:11 +00001112{
pbrookce1a14d2006-08-07 02:38:06 +00001113 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1114 qemu_bh_cancel(acb->bh);
1115 qemu_aio_release(acb);
bellard83f64092006-08-01 16:21:11 +00001116}
1117#endif /* !QEMU_TOOL */
1118
1119/**************************************************************/
1120/* sync block device emulation */
1121
1122static void bdrv_rw_em_cb(void *opaque, int ret)
1123{
1124 *(int *)opaque = ret;
1125}
1126
1127#define NOT_DONE 0x7fffffff
1128
1129static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
1130 uint8_t *buf, int nb_sectors)
1131{
pbrookce1a14d2006-08-07 02:38:06 +00001132 int async_ret;
1133 BlockDriverAIOCB *acb;
bellard83f64092006-08-01 16:21:11 +00001134
bellard83f64092006-08-01 16:21:11 +00001135 async_ret = NOT_DONE;
1136 qemu_aio_wait_start();
pbrookce1a14d2006-08-07 02:38:06 +00001137 acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
bellard83f64092006-08-01 16:21:11 +00001138 bdrv_rw_em_cb, &async_ret);
pbrookce1a14d2006-08-07 02:38:06 +00001139 if (acb == NULL) {
bellard83f64092006-08-01 16:21:11 +00001140 qemu_aio_wait_end();
pbrookce1a14d2006-08-07 02:38:06 +00001141 return -1;
bellard83f64092006-08-01 16:21:11 +00001142 }
1143 while (async_ret == NOT_DONE) {
1144 qemu_aio_wait();
1145 }
1146 qemu_aio_wait_end();
1147 return async_ret;
1148}
1149
1150static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1151 const uint8_t *buf, int nb_sectors)
1152{
pbrookce1a14d2006-08-07 02:38:06 +00001153 int async_ret;
1154 BlockDriverAIOCB *acb;
bellard83f64092006-08-01 16:21:11 +00001155
bellard83f64092006-08-01 16:21:11 +00001156 async_ret = NOT_DONE;
1157 qemu_aio_wait_start();
pbrookce1a14d2006-08-07 02:38:06 +00001158 acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
bellard83f64092006-08-01 16:21:11 +00001159 bdrv_rw_em_cb, &async_ret);
pbrookce1a14d2006-08-07 02:38:06 +00001160 if (acb == NULL) {
bellard83f64092006-08-01 16:21:11 +00001161 qemu_aio_wait_end();
pbrookce1a14d2006-08-07 02:38:06 +00001162 return -1;
bellard83f64092006-08-01 16:21:11 +00001163 }
1164 while (async_ret == NOT_DONE) {
1165 qemu_aio_wait();
1166 }
1167 qemu_aio_wait_end();
1168 return async_ret;
1169}
bellardea2384d2004-08-01 21:59:26 +00001170
1171void bdrv_init(void)
1172{
1173 bdrv_register(&bdrv_raw);
1174#ifndef _WIN32
1175 bdrv_register(&bdrv_cow);
1176#endif
1177 bdrv_register(&bdrv_qcow);
1178 bdrv_register(&bdrv_vmdk);
bellard3c565212004-09-29 21:29:14 +00001179 bdrv_register(&bdrv_cloop);
bellard585d0ed2004-12-12 11:24:44 +00001180 bdrv_register(&bdrv_dmg);
bellarda8753c32005-04-26 21:34:00 +00001181 bdrv_register(&bdrv_bochs);
bellard6a0f9e82005-04-27 20:17:58 +00001182 bdrv_register(&bdrv_vpc);
bellard712e7872005-04-28 21:09:32 +00001183 bdrv_register(&bdrv_vvfat);
bellardfaea38e2006-08-05 21:31:00 +00001184 bdrv_register(&bdrv_qcow2);
bellardea2384d2004-08-01 21:59:26 +00001185}
pbrookce1a14d2006-08-07 02:38:06 +00001186
1187void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1188 void *opaque)
1189{
1190 BlockDriver *drv;
1191 BlockDriverAIOCB *acb;
1192
1193 drv = bs->drv;
1194 if (drv->free_aiocb) {
1195 acb = drv->free_aiocb;
1196 drv->free_aiocb = acb->next;
1197 } else {
1198 acb = qemu_mallocz(drv->aiocb_size);
1199 if (!acb)
1200 return NULL;
1201 }
1202 acb->bs = bs;
1203 acb->cb = cb;
1204 acb->opaque = opaque;
1205 return acb;
1206}
1207
1208void qemu_aio_release(void *p)
1209{
1210 BlockDriverAIOCB *acb = p;
1211 BlockDriver *drv = acb->bs->drv;
1212 acb->next = drv->free_aiocb;
1213 drv->free_aiocb = acb;
1214}