blob: bbece2d24c014415eaec527da9c31320dd52f1c9 [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
bellard90765422006-08-07 19:10:16 +000038typedef struct BlockDriverAIOCBSync {
39 BlockDriverAIOCB common;
40 QEMUBH *bh;
41 int ret;
42} BlockDriverAIOCBSync;
43
pbrookce1a14d2006-08-07 02:38:06 +000044static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
45 int64_t sector_num, uint8_t *buf, int nb_sectors,
46 BlockDriverCompletionFunc *cb, void *opaque);
47static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
48 int64_t sector_num, const uint8_t *buf, int nb_sectors,
49 BlockDriverCompletionFunc *cb, void *opaque);
bellard83f64092006-08-01 16:21:11 +000050static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
bellard83f64092006-08-01 16:21:11 +000051static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
52 uint8_t *buf, int nb_sectors);
53static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
54 const uint8_t *buf, int nb_sectors);
bellardec530c82006-04-25 22:36:06 +000055
bellardb3380822004-03-14 21:38:54 +000056static BlockDriverState *bdrv_first;
bellardea2384d2004-08-01 21:59:26 +000057static BlockDriver *first_drv;
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;
bellard3b9f94e2007-01-07 17:27:07 +000067#ifdef _WIN32
68 return (*p == '/' || *p == '\\');
69#else
70 return (*p == '/');
71#endif
bellard83f64092006-08-01 16:21:11 +000072}
73
74/* if filename is absolute, just copy it to dest. Otherwise, build a
75 path to it by considering it is relative to base_path. URL are
76 supported. */
77void path_combine(char *dest, int dest_size,
78 const char *base_path,
79 const char *filename)
80{
81 const char *p, *p1;
82 int len;
83
84 if (dest_size <= 0)
85 return;
86 if (path_is_absolute(filename)) {
87 pstrcpy(dest, dest_size, filename);
88 } else {
89 p = strchr(base_path, ':');
90 if (p)
91 p++;
92 else
93 p = base_path;
bellard3b9f94e2007-01-07 17:27:07 +000094 p1 = strrchr(base_path, '/');
95#ifdef _WIN32
96 {
97 const char *p2;
98 p2 = strrchr(base_path, '\\');
99 if (!p1 || p2 > p1)
100 p1 = p2;
101 }
102#endif
bellard83f64092006-08-01 16:21:11 +0000103 if (p1)
104 p1++;
105 else
106 p1 = base_path;
107 if (p1 > p)
108 p = p1;
109 len = p - base_path;
110 if (len > dest_size - 1)
111 len = dest_size - 1;
112 memcpy(dest, base_path, len);
113 dest[len] = '\0';
114 pstrcat(dest, dest_size, filename);
115 }
116}
117
118
bellardea2384d2004-08-01 21:59:26 +0000119void bdrv_register(BlockDriver *bdrv)
120{
pbrookce1a14d2006-08-07 02:38:06 +0000121 if (!bdrv->bdrv_aio_read) {
bellard83f64092006-08-01 16:21:11 +0000122 /* add AIO emulation layer */
bellard83f64092006-08-01 16:21:11 +0000123 bdrv->bdrv_aio_read = bdrv_aio_read_em;
124 bdrv->bdrv_aio_write = bdrv_aio_write_em;
125 bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
bellard90765422006-08-07 19:10:16 +0000126 bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
bellard83f64092006-08-01 16:21:11 +0000127 } else if (!bdrv->bdrv_read && !bdrv->bdrv_pread) {
128 /* add synchronous IO emulation layer */
129 bdrv->bdrv_read = bdrv_read_em;
130 bdrv->bdrv_write = bdrv_write_em;
131 }
bellardea2384d2004-08-01 21:59:26 +0000132 bdrv->next = first_drv;
133 first_drv = bdrv;
134}
bellardb3380822004-03-14 21:38:54 +0000135
136/* create a new block device (by default it is empty) */
137BlockDriverState *bdrv_new(const char *device_name)
bellardfc01f7e2003-06-30 10:03:06 +0000138{
bellardb3380822004-03-14 21:38:54 +0000139 BlockDriverState **pbs, *bs;
140
141 bs = qemu_mallocz(sizeof(BlockDriverState));
142 if(!bs)
143 return NULL;
144 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
bellardea2384d2004-08-01 21:59:26 +0000145 if (device_name[0] != '\0') {
146 /* insert at the end */
147 pbs = &bdrv_first;
148 while (*pbs != NULL)
149 pbs = &(*pbs)->next;
150 *pbs = bs;
151 }
bellardb3380822004-03-14 21:38:54 +0000152 return bs;
153}
154
bellardea2384d2004-08-01 21:59:26 +0000155BlockDriver *bdrv_find_format(const char *format_name)
156{
157 BlockDriver *drv1;
158 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
159 if (!strcmp(drv1->format_name, format_name))
160 return drv1;
161 }
162 return NULL;
163}
164
165int bdrv_create(BlockDriver *drv,
166 const char *filename, int64_t size_in_sectors,
167 const char *backing_file, int flags)
168{
169 if (!drv->bdrv_create)
170 return -ENOTSUP;
171 return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
172}
173
bellardd5249392004-08-03 21:14:23 +0000174#ifdef _WIN32
bellard95389c82005-12-18 18:28:15 +0000175void get_tmp_filename(char *filename, int size)
bellardd5249392004-08-03 21:14:23 +0000176{
bellard3b9f94e2007-01-07 17:27:07 +0000177 char temp_dir[MAX_PATH];
178
179 GetTempPath(MAX_PATH, temp_dir);
180 GetTempFileName(temp_dir, "qem", 0, filename);
bellardd5249392004-08-03 21:14:23 +0000181}
182#else
bellard95389c82005-12-18 18:28:15 +0000183void get_tmp_filename(char *filename, int size)
bellardea2384d2004-08-01 21:59:26 +0000184{
185 int fd;
bellardd5249392004-08-03 21:14:23 +0000186 /* XXX: race condition possible */
bellardea2384d2004-08-01 21:59:26 +0000187 pstrcpy(filename, size, "/tmp/vl.XXXXXX");
188 fd = mkstemp(filename);
189 close(fd);
190}
bellardd5249392004-08-03 21:14:23 +0000191#endif
bellardea2384d2004-08-01 21:59:26 +0000192
bellard19cb3732006-08-19 11:45:59 +0000193#ifdef _WIN32
bellardf45512f2006-08-23 21:40:13 +0000194static int is_windows_drive_prefix(const char *filename)
195{
196 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
197 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
198 filename[1] == ':');
199}
200
bellard19cb3732006-08-19 11:45:59 +0000201static int is_windows_drive(const char *filename)
202{
bellardf45512f2006-08-23 21:40:13 +0000203 if (is_windows_drive_prefix(filename) &&
204 filename[2] == '\0')
bellard19cb3732006-08-19 11:45:59 +0000205 return 1;
206 if (strstart(filename, "\\\\.\\", NULL) ||
207 strstart(filename, "//./", NULL))
208 return 1;
209 return 0;
210}
211#endif
212
bellard83f64092006-08-01 16:21:11 +0000213static BlockDriver *find_protocol(const char *filename)
214{
215 BlockDriver *drv1;
216 char protocol[128];
217 int len;
218 const char *p;
bellard19cb3732006-08-19 11:45:59 +0000219
220#ifdef _WIN32
bellardf45512f2006-08-23 21:40:13 +0000221 if (is_windows_drive(filename) ||
222 is_windows_drive_prefix(filename))
bellard19cb3732006-08-19 11:45:59 +0000223 return &bdrv_raw;
224#endif
bellard83f64092006-08-01 16:21:11 +0000225 p = strchr(filename, ':');
226 if (!p)
227 return &bdrv_raw;
228 len = p - filename;
229 if (len > sizeof(protocol) - 1)
230 len = sizeof(protocol) - 1;
bellard83f64092006-08-01 16:21:11 +0000231 memcpy(protocol, filename, len);
232 protocol[len] = '\0';
233 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
234 if (drv1->protocol_name &&
235 !strcmp(drv1->protocol_name, protocol))
236 return drv1;
237 }
238 return NULL;
239}
240
bellard7674e7b2005-04-26 21:59:26 +0000241/* XXX: force raw format if block or character device ? It would
242 simplify the BSD case */
bellardea2384d2004-08-01 21:59:26 +0000243static BlockDriver *find_image_format(const char *filename)
244{
bellard83f64092006-08-01 16:21:11 +0000245 int ret, score, score_max;
bellardea2384d2004-08-01 21:59:26 +0000246 BlockDriver *drv1, *drv;
bellard83f64092006-08-01 16:21:11 +0000247 uint8_t buf[2048];
248 BlockDriverState *bs;
bellardea2384d2004-08-01 21:59:26 +0000249
bellard19cb3732006-08-19 11:45:59 +0000250 /* detect host devices. By convention, /dev/cdrom[N] is always
251 recognized as a host CDROM */
252 if (strstart(filename, "/dev/cdrom", NULL))
253 return &bdrv_host_device;
254#ifdef _WIN32
255 if (is_windows_drive(filename))
256 return &bdrv_host_device;
257#else
258 {
259 struct stat st;
260 if (stat(filename, &st) >= 0 &&
261 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
262 return &bdrv_host_device;
263 }
264 }
265#endif
266
bellard83f64092006-08-01 16:21:11 +0000267 drv = find_protocol(filename);
bellard19cb3732006-08-19 11:45:59 +0000268 /* no need to test disk image formats for vvfat */
bellard83f64092006-08-01 16:21:11 +0000269 if (drv == &bdrv_vvfat)
270 return drv;
bellard19cb3732006-08-19 11:45:59 +0000271
bellard83f64092006-08-01 16:21:11 +0000272 ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
273 if (ret < 0)
274 return NULL;
275 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
276 bdrv_delete(bs);
277 if (ret < 0) {
278 return NULL;
279 }
280
bellardea2384d2004-08-01 21:59:26 +0000281 score_max = 0;
282 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
bellard83f64092006-08-01 16:21:11 +0000283 if (drv1->bdrv_probe) {
284 score = drv1->bdrv_probe(buf, ret, filename);
285 if (score > score_max) {
286 score_max = score;
287 drv = drv1;
288 }
bellardea2384d2004-08-01 21:59:26 +0000289 }
290 }
291 return drv;
292}
293
bellard83f64092006-08-01 16:21:11 +0000294int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
bellardb3380822004-03-14 21:38:54 +0000295{
bellard83f64092006-08-01 16:21:11 +0000296 BlockDriverState *bs;
297 int ret;
298
299 bs = bdrv_new("");
300 if (!bs)
301 return -ENOMEM;
302 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
303 if (ret < 0) {
304 bdrv_delete(bs);
305 return ret;
bellard3b0d4f62005-10-30 18:30:10 +0000306 }
bellard83f64092006-08-01 16:21:11 +0000307 *pbs = bs;
308 return 0;
bellardea2384d2004-08-01 21:59:26 +0000309}
bellardfc01f7e2003-06-30 10:03:06 +0000310
bellard83f64092006-08-01 16:21:11 +0000311int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
312{
313 return bdrv_open2(bs, filename, flags, NULL);
314}
315
316int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
bellardea2384d2004-08-01 21:59:26 +0000317 BlockDriver *drv)
318{
bellard83f64092006-08-01 16:21:11 +0000319 int ret, open_flags;
bellardea2384d2004-08-01 21:59:26 +0000320 char tmp_filename[1024];
bellard83f64092006-08-01 16:21:11 +0000321 char backing_filename[1024];
bellard33e39632003-07-06 17:15:21 +0000322
bellardea2384d2004-08-01 21:59:26 +0000323 bs->read_only = 0;
324 bs->is_temporary = 0;
325 bs->encrypted = 0;
bellard712e7872005-04-28 21:09:32 +0000326
bellard83f64092006-08-01 16:21:11 +0000327 if (flags & BDRV_O_SNAPSHOT) {
bellardea2384d2004-08-01 21:59:26 +0000328 BlockDriverState *bs1;
329 int64_t total_size;
330
331 /* if snapshot, we create a temporary backing file and open it
332 instead of opening 'filename' directly */
333
334 /* if there is a backing file, use it */
335 bs1 = bdrv_new("");
336 if (!bs1) {
bellard83f64092006-08-01 16:21:11 +0000337 return -ENOMEM;
bellardea2384d2004-08-01 21:59:26 +0000338 }
339 if (bdrv_open(bs1, filename, 0) < 0) {
340 bdrv_delete(bs1);
341 return -1;
342 }
bellard83f64092006-08-01 16:21:11 +0000343 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
bellardea2384d2004-08-01 21:59:26 +0000344 bdrv_delete(bs1);
345
346 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
bellarda817d932006-08-24 19:53:37 +0000347 realpath(filename, backing_filename);
bellardd15a7712006-08-06 13:35:09 +0000348 if (bdrv_create(&bdrv_qcow2, tmp_filename,
bellarda817d932006-08-24 19:53:37 +0000349 total_size, backing_filename, 0) < 0) {
bellardea2384d2004-08-01 21:59:26 +0000350 return -1;
351 }
352 filename = tmp_filename;
353 bs->is_temporary = 1;
354 }
bellard712e7872005-04-28 21:09:32 +0000355
bellardea2384d2004-08-01 21:59:26 +0000356 pstrcpy(bs->filename, sizeof(bs->filename), filename);
bellard83f64092006-08-01 16:21:11 +0000357 if (flags & BDRV_O_FILE) {
358 drv = find_protocol(filename);
bellardea2384d2004-08-01 21:59:26 +0000359 if (!drv)
bellard83f64092006-08-01 16:21:11 +0000360 return -ENOENT;
361 } else {
362 if (!drv) {
363 drv = find_image_format(filename);
364 if (!drv)
365 return -1;
366 }
bellardea2384d2004-08-01 21:59:26 +0000367 }
368 bs->drv = drv;
369 bs->opaque = qemu_mallocz(drv->instance_size);
370 if (bs->opaque == NULL && drv->instance_size > 0)
371 return -1;
bellard83f64092006-08-01 16:21:11 +0000372 /* Note: for compatibility, we open disk image files as RDWR, and
373 RDONLY as fallback */
374 if (!(flags & BDRV_O_FILE))
375 open_flags = BDRV_O_RDWR;
376 else
377 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
378 ret = drv->bdrv_open(bs, filename, open_flags);
379 if (ret == -EACCES && !(flags & BDRV_O_FILE)) {
380 ret = drv->bdrv_open(bs, filename, BDRV_O_RDONLY);
381 bs->read_only = 1;
382 }
bellardea2384d2004-08-01 21:59:26 +0000383 if (ret < 0) {
384 qemu_free(bs->opaque);
bellard6b21b972006-08-23 21:14:37 +0000385 bs->opaque = NULL;
386 bs->drv = NULL;
bellard83f64092006-08-01 16:21:11 +0000387 return ret;
bellardea2384d2004-08-01 21:59:26 +0000388 }
bellardd15a7712006-08-06 13:35:09 +0000389 if (drv->bdrv_getlength) {
390 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
391 }
bellardea2384d2004-08-01 21:59:26 +0000392#ifndef _WIN32
393 if (bs->is_temporary) {
394 unlink(filename);
395 }
396#endif
bellard83f64092006-08-01 16:21:11 +0000397 if (bs->backing_file[0] != '\0') {
bellardea2384d2004-08-01 21:59:26 +0000398 /* if there is a backing file, use it */
399 bs->backing_hd = bdrv_new("");
400 if (!bs->backing_hd) {
401 fail:
402 bdrv_close(bs);
bellard6b21b972006-08-23 21:14:37 +0000403 return -ENOMEM;
bellardea2384d2004-08-01 21:59:26 +0000404 }
bellard83f64092006-08-01 16:21:11 +0000405 path_combine(backing_filename, sizeof(backing_filename),
406 filename, bs->backing_file);
407 if (bdrv_open(bs->backing_hd, backing_filename, 0) < 0)
bellardea2384d2004-08-01 21:59:26 +0000408 goto fail;
409 }
410
bellardb3380822004-03-14 21:38:54 +0000411 /* call the change callback */
bellard19cb3732006-08-19 11:45:59 +0000412 bs->media_changed = 1;
bellardb3380822004-03-14 21:38:54 +0000413 if (bs->change_cb)
414 bs->change_cb(bs->change_opaque);
415
416 return 0;
bellardfc01f7e2003-06-30 10:03:06 +0000417}
418
419void bdrv_close(BlockDriverState *bs)
420{
bellard19cb3732006-08-19 11:45:59 +0000421 if (bs->drv) {
bellardea2384d2004-08-01 21:59:26 +0000422 if (bs->backing_hd)
423 bdrv_delete(bs->backing_hd);
424 bs->drv->bdrv_close(bs);
425 qemu_free(bs->opaque);
426#ifdef _WIN32
427 if (bs->is_temporary) {
428 unlink(bs->filename);
429 }
bellard67b915a2004-03-31 23:37:16 +0000430#endif
bellardea2384d2004-08-01 21:59:26 +0000431 bs->opaque = NULL;
432 bs->drv = NULL;
bellardb3380822004-03-14 21:38:54 +0000433
434 /* call the change callback */
bellard19cb3732006-08-19 11:45:59 +0000435 bs->media_changed = 1;
bellardb3380822004-03-14 21:38:54 +0000436 if (bs->change_cb)
437 bs->change_cb(bs->change_opaque);
438 }
439}
440
441void bdrv_delete(BlockDriverState *bs)
442{
bellardea2384d2004-08-01 21:59:26 +0000443 /* XXX: remove the driver list */
bellardb3380822004-03-14 21:38:54 +0000444 bdrv_close(bs);
445 qemu_free(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000446}
447
bellard33e39632003-07-06 17:15:21 +0000448/* commit COW file into the raw image */
449int bdrv_commit(BlockDriverState *bs)
450{
bellard19cb3732006-08-19 11:45:59 +0000451 BlockDriver *drv = bs->drv;
bellard83f64092006-08-01 16:21:11 +0000452 int64_t i, total_sectors;
bellardea2384d2004-08-01 21:59:26 +0000453 int n, j;
454 unsigned char sector[512];
bellard33e39632003-07-06 17:15:21 +0000455
bellard19cb3732006-08-19 11:45:59 +0000456 if (!drv)
457 return -ENOMEDIUM;
bellard33e39632003-07-06 17:15:21 +0000458
459 if (bs->read_only) {
bellardea2384d2004-08-01 21:59:26 +0000460 return -EACCES;
bellard33e39632003-07-06 17:15:21 +0000461 }
462
bellardea2384d2004-08-01 21:59:26 +0000463 if (!bs->backing_hd) {
464 return -ENOTSUP;
bellard33e39632003-07-06 17:15:21 +0000465 }
bellardea2384d2004-08-01 21:59:26 +0000466
bellard83f64092006-08-01 16:21:11 +0000467 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
468 for (i = 0; i < total_sectors;) {
bellard19cb3732006-08-19 11:45:59 +0000469 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
bellardea2384d2004-08-01 21:59:26 +0000470 for(j = 0; j < n; j++) {
471 if (bdrv_read(bs, i, sector, 1) != 0) {
472 return -EIO;
473 }
474
475 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
476 return -EIO;
477 }
478 i++;
479 }
480 } else {
481 i += n;
482 }
483 }
bellard95389c82005-12-18 18:28:15 +0000484
bellard19cb3732006-08-19 11:45:59 +0000485 if (drv->bdrv_make_empty)
486 return drv->bdrv_make_empty(bs);
bellard95389c82005-12-18 18:28:15 +0000487
bellard33e39632003-07-06 17:15:21 +0000488 return 0;
489}
490
bellard19cb3732006-08-19 11:45:59 +0000491/* return < 0 if error. See bdrv_write() for the return codes */
bellardfc01f7e2003-06-30 10:03:06 +0000492int bdrv_read(BlockDriverState *bs, int64_t sector_num,
493 uint8_t *buf, int nb_sectors)
494{
bellardea2384d2004-08-01 21:59:26 +0000495 BlockDriver *drv = bs->drv;
496
bellard19cb3732006-08-19 11:45:59 +0000497 if (!drv)
498 return -ENOMEDIUM;
bellardb3380822004-03-14 21:38:54 +0000499
bellard83f64092006-08-01 16:21:11 +0000500 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
bellardcf989512004-02-16 21:56:36 +0000501 memcpy(buf, bs->boot_sector_data, 512);
bellard83f64092006-08-01 16:21:11 +0000502 sector_num++;
503 nb_sectors--;
504 buf += 512;
505 if (nb_sectors == 0)
506 return 0;
bellard33e39632003-07-06 17:15:21 +0000507 }
bellard83f64092006-08-01 16:21:11 +0000508 if (drv->bdrv_pread) {
509 int ret, len;
510 len = nb_sectors * 512;
511 ret = drv->bdrv_pread(bs, sector_num * 512, buf, len);
512 if (ret < 0)
513 return ret;
514 else if (ret != len)
bellard19cb3732006-08-19 11:45:59 +0000515 return -EINVAL;
bellard83f64092006-08-01 16:21:11 +0000516 else
517 return 0;
518 } else {
519 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
520 }
bellardfc01f7e2003-06-30 10:03:06 +0000521}
522
bellard19cb3732006-08-19 11:45:59 +0000523/* Return < 0 if error. Important errors are:
524 -EIO generic I/O error (may happen for all errors)
525 -ENOMEDIUM No media inserted.
526 -EINVAL Invalid sector number or nb_sectors
527 -EACCES Trying to write a read-only device
528*/
bellardfc01f7e2003-06-30 10:03:06 +0000529int bdrv_write(BlockDriverState *bs, int64_t sector_num,
530 const uint8_t *buf, int nb_sectors)
531{
bellard83f64092006-08-01 16:21:11 +0000532 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +0000533 if (!bs->drv)
534 return -ENOMEDIUM;
bellard0849bf02003-06-30 23:17:31 +0000535 if (bs->read_only)
bellard19cb3732006-08-19 11:45:59 +0000536 return -EACCES;
bellard79639d42005-11-26 10:58:41 +0000537 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
538 memcpy(bs->boot_sector_data, buf, 512);
539 }
bellard83f64092006-08-01 16:21:11 +0000540 if (drv->bdrv_pwrite) {
541 int ret, len;
542 len = nb_sectors * 512;
543 ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len);
544 if (ret < 0)
545 return ret;
546 else if (ret != len)
547 return -EIO;
548 else
549 return 0;
550 } else {
551 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
552 }
553}
554
bellard83f64092006-08-01 16:21:11 +0000555static int bdrv_pread_em(BlockDriverState *bs, int64_t offset,
bellardfaea38e2006-08-05 21:31:00 +0000556 uint8_t *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000557{
bellard83f64092006-08-01 16:21:11 +0000558 uint8_t tmp_buf[SECTOR_SIZE];
559 int len, nb_sectors, count;
560 int64_t sector_num;
561
562 count = count1;
563 /* first read to align to sector start */
564 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
565 if (len > count)
566 len = count;
567 sector_num = offset >> SECTOR_BITS;
568 if (len > 0) {
569 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
570 return -EIO;
571 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
572 count -= len;
573 if (count == 0)
574 return count1;
575 sector_num++;
576 buf += len;
577 }
578
579 /* read the sectors "in place" */
580 nb_sectors = count >> SECTOR_BITS;
581 if (nb_sectors > 0) {
582 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
583 return -EIO;
584 sector_num += nb_sectors;
585 len = nb_sectors << SECTOR_BITS;
586 buf += len;
587 count -= len;
588 }
589
590 /* add data from the last sector */
591 if (count > 0) {
592 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
593 return -EIO;
594 memcpy(buf, tmp_buf, count);
595 }
596 return count1;
597}
598
599static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset,
bellardfaea38e2006-08-05 21:31:00 +0000600 const uint8_t *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000601{
bellard83f64092006-08-01 16:21:11 +0000602 uint8_t tmp_buf[SECTOR_SIZE];
603 int len, nb_sectors, count;
604 int64_t sector_num;
605
606 count = count1;
607 /* first write to align to sector start */
608 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
609 if (len > count)
610 len = count;
611 sector_num = offset >> SECTOR_BITS;
612 if (len > 0) {
613 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
614 return -EIO;
615 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
616 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
617 return -EIO;
618 count -= len;
619 if (count == 0)
620 return count1;
621 sector_num++;
622 buf += len;
623 }
624
625 /* write the sectors "in place" */
626 nb_sectors = count >> SECTOR_BITS;
627 if (nb_sectors > 0) {
628 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
629 return -EIO;
630 sector_num += nb_sectors;
631 len = nb_sectors << SECTOR_BITS;
632 buf += len;
633 count -= len;
634 }
635
636 /* add data from the last sector */
637 if (count > 0) {
638 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
639 return -EIO;
640 memcpy(tmp_buf, buf, count);
641 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
642 return -EIO;
643 }
644 return count1;
645}
bellard83f64092006-08-01 16:21:11 +0000646
647/**
648 * Read with byte offsets (needed only for file protocols)
649 */
650int bdrv_pread(BlockDriverState *bs, int64_t offset,
651 void *buf1, int count1)
652{
653 BlockDriver *drv = bs->drv;
654
655 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000656 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000657 if (!drv->bdrv_pread)
bellardfaea38e2006-08-05 21:31:00 +0000658 return bdrv_pread_em(bs, offset, buf1, count1);
bellard83f64092006-08-01 16:21:11 +0000659 return drv->bdrv_pread(bs, offset, buf1, count1);
660}
661
662/**
663 * Write with byte offsets (needed only for file protocols)
664 */
665int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
666 const void *buf1, int count1)
667{
668 BlockDriver *drv = bs->drv;
669
670 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000671 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000672 if (!drv->bdrv_pwrite)
bellardfaea38e2006-08-05 21:31:00 +0000673 return bdrv_pwrite_em(bs, offset, buf1, count1);
bellard83f64092006-08-01 16:21:11 +0000674 return drv->bdrv_pwrite(bs, offset, buf1, count1);
675}
676
677/**
678 * Truncate file to 'offset' bytes (needed only for file protocols)
679 */
680int bdrv_truncate(BlockDriverState *bs, int64_t offset)
681{
682 BlockDriver *drv = bs->drv;
683 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000684 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000685 if (!drv->bdrv_truncate)
686 return -ENOTSUP;
687 return drv->bdrv_truncate(bs, offset);
688}
689
690/**
691 * Length of a file in bytes. Return < 0 if error or unknown.
692 */
693int64_t bdrv_getlength(BlockDriverState *bs)
694{
695 BlockDriver *drv = bs->drv;
696 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000697 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000698 if (!drv->bdrv_getlength) {
699 /* legacy mode */
700 return bs->total_sectors * SECTOR_SIZE;
701 }
702 return drv->bdrv_getlength(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000703}
704
bellard19cb3732006-08-19 11:45:59 +0000705/* return 0 as number of sectors if no device present or error */
bellardfc01f7e2003-06-30 10:03:06 +0000706void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr)
707{
bellard19cb3732006-08-19 11:45:59 +0000708 int64_t length;
709 length = bdrv_getlength(bs);
710 if (length < 0)
711 length = 0;
712 else
713 length = length >> SECTOR_BITS;
714 *nb_sectors_ptr = length;
bellardfc01f7e2003-06-30 10:03:06 +0000715}
bellardcf989512004-02-16 21:56:36 +0000716
717/* force a given boot sector. */
718void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size)
719{
720 bs->boot_sector_enabled = 1;
721 if (size > 512)
722 size = 512;
723 memcpy(bs->boot_sector_data, data, size);
724 memset(bs->boot_sector_data + size, 0, 512 - size);
725}
bellardb3380822004-03-14 21:38:54 +0000726
727void bdrv_set_geometry_hint(BlockDriverState *bs,
728 int cyls, int heads, int secs)
729{
730 bs->cyls = cyls;
731 bs->heads = heads;
732 bs->secs = secs;
733}
734
735void bdrv_set_type_hint(BlockDriverState *bs, int type)
736{
737 bs->type = type;
738 bs->removable = ((type == BDRV_TYPE_CDROM ||
739 type == BDRV_TYPE_FLOPPY));
740}
741
bellard46d47672004-11-16 01:45:27 +0000742void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
743{
744 bs->translation = translation;
745}
746
bellardb3380822004-03-14 21:38:54 +0000747void bdrv_get_geometry_hint(BlockDriverState *bs,
748 int *pcyls, int *pheads, int *psecs)
749{
750 *pcyls = bs->cyls;
751 *pheads = bs->heads;
752 *psecs = bs->secs;
753}
754
755int bdrv_get_type_hint(BlockDriverState *bs)
756{
757 return bs->type;
758}
759
bellard46d47672004-11-16 01:45:27 +0000760int bdrv_get_translation_hint(BlockDriverState *bs)
761{
762 return bs->translation;
763}
764
bellardb3380822004-03-14 21:38:54 +0000765int bdrv_is_removable(BlockDriverState *bs)
766{
767 return bs->removable;
768}
769
770int bdrv_is_read_only(BlockDriverState *bs)
771{
772 return bs->read_only;
773}
774
bellard19cb3732006-08-19 11:45:59 +0000775/* XXX: no longer used */
bellardb3380822004-03-14 21:38:54 +0000776void bdrv_set_change_cb(BlockDriverState *bs,
777 void (*change_cb)(void *opaque), void *opaque)
778{
779 bs->change_cb = change_cb;
780 bs->change_opaque = opaque;
781}
782
bellardea2384d2004-08-01 21:59:26 +0000783int bdrv_is_encrypted(BlockDriverState *bs)
784{
785 if (bs->backing_hd && bs->backing_hd->encrypted)
786 return 1;
787 return bs->encrypted;
788}
789
790int bdrv_set_key(BlockDriverState *bs, const char *key)
791{
792 int ret;
793 if (bs->backing_hd && bs->backing_hd->encrypted) {
794 ret = bdrv_set_key(bs->backing_hd, key);
795 if (ret < 0)
796 return ret;
797 if (!bs->encrypted)
798 return 0;
799 }
800 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
801 return -1;
802 return bs->drv->bdrv_set_key(bs, key);
803}
804
805void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
806{
bellard19cb3732006-08-19 11:45:59 +0000807 if (!bs->drv) {
bellardea2384d2004-08-01 21:59:26 +0000808 buf[0] = '\0';
809 } else {
810 pstrcpy(buf, buf_size, bs->drv->format_name);
811 }
812}
813
814void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
815 void *opaque)
816{
817 BlockDriver *drv;
818
819 for (drv = first_drv; drv != NULL; drv = drv->next) {
820 it(opaque, drv->format_name);
821 }
822}
823
bellardb3380822004-03-14 21:38:54 +0000824BlockDriverState *bdrv_find(const char *name)
825{
826 BlockDriverState *bs;
827
828 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
829 if (!strcmp(name, bs->device_name))
830 return bs;
831 }
832 return NULL;
833}
834
bellard81d09122004-07-14 17:21:37 +0000835void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
836{
837 BlockDriverState *bs;
838
839 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
840 it(opaque, bs->device_name);
841 }
842}
843
bellardea2384d2004-08-01 21:59:26 +0000844const char *bdrv_get_device_name(BlockDriverState *bs)
845{
846 return bs->device_name;
847}
848
pbrook7a6cba62006-06-04 11:39:07 +0000849void bdrv_flush(BlockDriverState *bs)
850{
851 if (bs->drv->bdrv_flush)
852 bs->drv->bdrv_flush(bs);
853 if (bs->backing_hd)
854 bdrv_flush(bs->backing_hd);
855}
856
bellardb3380822004-03-14 21:38:54 +0000857void bdrv_info(void)
858{
859 BlockDriverState *bs;
860
861 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
862 term_printf("%s:", bs->device_name);
863 term_printf(" type=");
864 switch(bs->type) {
865 case BDRV_TYPE_HD:
866 term_printf("hd");
867 break;
868 case BDRV_TYPE_CDROM:
869 term_printf("cdrom");
870 break;
871 case BDRV_TYPE_FLOPPY:
872 term_printf("floppy");
873 break;
874 }
875 term_printf(" removable=%d", bs->removable);
876 if (bs->removable) {
877 term_printf(" locked=%d", bs->locked);
878 }
bellard19cb3732006-08-19 11:45:59 +0000879 if (bs->drv) {
thsfef30742006-12-22 14:11:32 +0000880 term_printf(" file=");
881 term_print_filename(bs->filename);
882 if (bs->backing_file[0] != '\0') {
883 term_printf(" backing_file=");
884 term_print_filename(bs->backing_file);
885 }
bellardb3380822004-03-14 21:38:54 +0000886 term_printf(" ro=%d", bs->read_only);
bellardea2384d2004-08-01 21:59:26 +0000887 term_printf(" drv=%s", bs->drv->format_name);
888 if (bs->encrypted)
889 term_printf(" encrypted");
bellardb3380822004-03-14 21:38:54 +0000890 } else {
891 term_printf(" [not inserted]");
892 }
893 term_printf("\n");
894 }
895}
bellardea2384d2004-08-01 21:59:26 +0000896
bellard83f64092006-08-01 16:21:11 +0000897void bdrv_get_backing_filename(BlockDriverState *bs,
898 char *filename, int filename_size)
bellardea2384d2004-08-01 21:59:26 +0000899{
bellard83f64092006-08-01 16:21:11 +0000900 if (!bs->backing_hd) {
901 pstrcpy(filename, filename_size, "");
902 } else {
903 pstrcpy(filename, filename_size, bs->backing_file);
904 }
bellardea2384d2004-08-01 21:59:26 +0000905}
906
bellardfaea38e2006-08-05 21:31:00 +0000907int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
908 const uint8_t *buf, int nb_sectors)
909{
910 BlockDriver *drv = bs->drv;
911 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000912 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +0000913 if (!drv->bdrv_write_compressed)
914 return -ENOTSUP;
915 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
916}
917
918int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
919{
920 BlockDriver *drv = bs->drv;
921 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000922 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +0000923 if (!drv->bdrv_get_info)
924 return -ENOTSUP;
925 memset(bdi, 0, sizeof(*bdi));
926 return drv->bdrv_get_info(bs, bdi);
927}
928
929/**************************************************************/
930/* handling of snapshots */
931
932int bdrv_snapshot_create(BlockDriverState *bs,
933 QEMUSnapshotInfo *sn_info)
934{
935 BlockDriver *drv = bs->drv;
936 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000937 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +0000938 if (!drv->bdrv_snapshot_create)
939 return -ENOTSUP;
940 return drv->bdrv_snapshot_create(bs, sn_info);
941}
942
943int bdrv_snapshot_goto(BlockDriverState *bs,
944 const char *snapshot_id)
945{
946 BlockDriver *drv = bs->drv;
947 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000948 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +0000949 if (!drv->bdrv_snapshot_goto)
950 return -ENOTSUP;
951 return drv->bdrv_snapshot_goto(bs, snapshot_id);
952}
953
954int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
955{
956 BlockDriver *drv = bs->drv;
957 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000958 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +0000959 if (!drv->bdrv_snapshot_delete)
960 return -ENOTSUP;
961 return drv->bdrv_snapshot_delete(bs, snapshot_id);
962}
963
964int bdrv_snapshot_list(BlockDriverState *bs,
965 QEMUSnapshotInfo **psn_info)
966{
967 BlockDriver *drv = bs->drv;
968 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000969 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +0000970 if (!drv->bdrv_snapshot_list)
971 return -ENOTSUP;
972 return drv->bdrv_snapshot_list(bs, psn_info);
973}
974
975#define NB_SUFFIXES 4
976
977char *get_human_readable_size(char *buf, int buf_size, int64_t size)
978{
979 static const char suffixes[NB_SUFFIXES] = "KMGT";
980 int64_t base;
981 int i;
982
983 if (size <= 999) {
984 snprintf(buf, buf_size, "%" PRId64, size);
985 } else {
986 base = 1024;
987 for(i = 0; i < NB_SUFFIXES; i++) {
988 if (size < (10 * base)) {
989 snprintf(buf, buf_size, "%0.1f%c",
990 (double)size / base,
991 suffixes[i]);
992 break;
993 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
994 snprintf(buf, buf_size, "%" PRId64 "%c",
995 ((size + (base >> 1)) / base),
996 suffixes[i]);
997 break;
998 }
999 base = base * 1024;
1000 }
1001 }
1002 return buf;
1003}
1004
1005char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1006{
1007 char buf1[128], date_buf[128], clock_buf[128];
bellard3b9f94e2007-01-07 17:27:07 +00001008#ifdef _WIN32
1009 struct tm *ptm;
1010#else
bellardfaea38e2006-08-05 21:31:00 +00001011 struct tm tm;
bellard3b9f94e2007-01-07 17:27:07 +00001012#endif
bellardfaea38e2006-08-05 21:31:00 +00001013 time_t ti;
1014 int64_t secs;
1015
1016 if (!sn) {
1017 snprintf(buf, buf_size,
1018 "%-10s%-20s%7s%20s%15s",
1019 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1020 } else {
1021 ti = sn->date_sec;
bellard3b9f94e2007-01-07 17:27:07 +00001022#ifdef _WIN32
1023 ptm = localtime(&ti);
1024 strftime(date_buf, sizeof(date_buf),
1025 "%Y-%m-%d %H:%M:%S", ptm);
1026#else
bellardfaea38e2006-08-05 21:31:00 +00001027 localtime_r(&ti, &tm);
1028 strftime(date_buf, sizeof(date_buf),
1029 "%Y-%m-%d %H:%M:%S", &tm);
bellard3b9f94e2007-01-07 17:27:07 +00001030#endif
bellardfaea38e2006-08-05 21:31:00 +00001031 secs = sn->vm_clock_nsec / 1000000000;
1032 snprintf(clock_buf, sizeof(clock_buf),
1033 "%02d:%02d:%02d.%03d",
1034 (int)(secs / 3600),
1035 (int)((secs / 60) % 60),
1036 (int)(secs % 60),
1037 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1038 snprintf(buf, buf_size,
1039 "%-10s%-20s%7s%20s%15s",
1040 sn->id_str, sn->name,
1041 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1042 date_buf,
1043 clock_buf);
1044 }
1045 return buf;
1046}
1047
bellardea2384d2004-08-01 21:59:26 +00001048
bellard83f64092006-08-01 16:21:11 +00001049/**************************************************************/
1050/* async I/Os */
1051
pbrookce1a14d2006-08-07 02:38:06 +00001052BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1053 uint8_t *buf, int nb_sectors,
1054 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001055{
1056 BlockDriver *drv = bs->drv;
bellard83f64092006-08-01 16:21:11 +00001057
bellard19cb3732006-08-19 11:45:59 +00001058 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001059 return NULL;
bellard83f64092006-08-01 16:21:11 +00001060
1061 /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
1062 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1063 memcpy(buf, bs->boot_sector_data, 512);
1064 sector_num++;
1065 nb_sectors--;
1066 buf += 512;
1067 }
1068
pbrookce1a14d2006-08-07 02:38:06 +00001069 return drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
bellard83f64092006-08-01 16:21:11 +00001070}
1071
pbrookce1a14d2006-08-07 02:38:06 +00001072BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1073 const uint8_t *buf, int nb_sectors,
1074 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001075{
bellard83f64092006-08-01 16:21:11 +00001076 BlockDriver *drv = bs->drv;
1077
bellard19cb3732006-08-19 11:45:59 +00001078 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001079 return NULL;
bellard83f64092006-08-01 16:21:11 +00001080 if (bs->read_only)
pbrookce1a14d2006-08-07 02:38:06 +00001081 return NULL;
bellard83f64092006-08-01 16:21:11 +00001082 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1083 memcpy(bs->boot_sector_data, buf, 512);
bellardea2384d2004-08-01 21:59:26 +00001084 }
bellard83f64092006-08-01 16:21:11 +00001085
pbrookce1a14d2006-08-07 02:38:06 +00001086 return drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
bellard83f64092006-08-01 16:21:11 +00001087}
1088
1089void bdrv_aio_cancel(BlockDriverAIOCB *acb)
pbrookce1a14d2006-08-07 02:38:06 +00001090{
1091 BlockDriver *drv = acb->bs->drv;
bellard83f64092006-08-01 16:21:11 +00001092
1093 drv->bdrv_aio_cancel(acb);
bellard83f64092006-08-01 16:21:11 +00001094}
1095
pbrookce1a14d2006-08-07 02:38:06 +00001096
bellard83f64092006-08-01 16:21:11 +00001097/**************************************************************/
1098/* async block device emulation */
1099
1100#ifdef QEMU_TOOL
pbrookce1a14d2006-08-07 02:38:06 +00001101static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1102 int64_t sector_num, uint8_t *buf, int nb_sectors,
1103 BlockDriverCompletionFunc *cb, void *opaque)
bellardea2384d2004-08-01 21:59:26 +00001104{
bellardea2384d2004-08-01 21:59:26 +00001105 int ret;
pbrookce1a14d2006-08-07 02:38:06 +00001106 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1107 cb(opaque, ret);
1108 return NULL;
bellardea2384d2004-08-01 21:59:26 +00001109}
1110
pbrookce1a14d2006-08-07 02:38:06 +00001111static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1112 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1113 BlockDriverCompletionFunc *cb, void *opaque)
bellardea2384d2004-08-01 21:59:26 +00001114{
bellardea2384d2004-08-01 21:59:26 +00001115 int ret;
pbrookce1a14d2006-08-07 02:38:06 +00001116 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1117 cb(opaque, ret);
1118 return NULL;
bellardea2384d2004-08-01 21:59:26 +00001119}
1120
bellard83f64092006-08-01 16:21:11 +00001121static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb)
bellardea2384d2004-08-01 21:59:26 +00001122{
bellardea2384d2004-08-01 21:59:26 +00001123}
bellardbeac80c2006-06-26 20:08:57 +00001124#else
bellard83f64092006-08-01 16:21:11 +00001125static void bdrv_aio_bh_cb(void *opaque)
bellardbeac80c2006-06-26 20:08:57 +00001126{
pbrookce1a14d2006-08-07 02:38:06 +00001127 BlockDriverAIOCBSync *acb = opaque;
1128 acb->common.cb(acb->common.opaque, acb->ret);
1129 qemu_aio_release(acb);
bellardbeac80c2006-06-26 20:08:57 +00001130}
bellardbeac80c2006-06-26 20:08:57 +00001131
pbrookce1a14d2006-08-07 02:38:06 +00001132static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1133 int64_t sector_num, uint8_t *buf, int nb_sectors,
1134 BlockDriverCompletionFunc *cb, void *opaque)
bellardea2384d2004-08-01 21:59:26 +00001135{
pbrookce1a14d2006-08-07 02:38:06 +00001136 BlockDriverAIOCBSync *acb;
bellard83f64092006-08-01 16:21:11 +00001137 int ret;
pbrookce1a14d2006-08-07 02:38:06 +00001138
1139 acb = qemu_aio_get(bs, cb, opaque);
1140 if (!acb->bh)
1141 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1142 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1143 acb->ret = ret;
1144 qemu_bh_schedule(acb->bh);
1145 return &acb->common;
pbrook7a6cba62006-06-04 11:39:07 +00001146}
1147
pbrookce1a14d2006-08-07 02:38:06 +00001148static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1149 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1150 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001151{
pbrookce1a14d2006-08-07 02:38:06 +00001152 BlockDriverAIOCBSync *acb;
bellard83f64092006-08-01 16:21:11 +00001153 int ret;
pbrookce1a14d2006-08-07 02:38:06 +00001154
1155 acb = qemu_aio_get(bs, cb, opaque);
1156 if (!acb->bh)
1157 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1158 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1159 acb->ret = ret;
1160 qemu_bh_schedule(acb->bh);
1161 return &acb->common;
bellard83f64092006-08-01 16:21:11 +00001162}
1163
pbrookce1a14d2006-08-07 02:38:06 +00001164static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
bellard83f64092006-08-01 16:21:11 +00001165{
pbrookce1a14d2006-08-07 02:38:06 +00001166 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1167 qemu_bh_cancel(acb->bh);
1168 qemu_aio_release(acb);
bellard83f64092006-08-01 16:21:11 +00001169}
1170#endif /* !QEMU_TOOL */
1171
1172/**************************************************************/
1173/* sync block device emulation */
1174
1175static void bdrv_rw_em_cb(void *opaque, int ret)
1176{
1177 *(int *)opaque = ret;
1178}
1179
1180#define NOT_DONE 0x7fffffff
1181
1182static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
1183 uint8_t *buf, int nb_sectors)
1184{
pbrookce1a14d2006-08-07 02:38:06 +00001185 int async_ret;
1186 BlockDriverAIOCB *acb;
bellard83f64092006-08-01 16:21:11 +00001187
bellard83f64092006-08-01 16:21:11 +00001188 async_ret = NOT_DONE;
1189 qemu_aio_wait_start();
pbrookce1a14d2006-08-07 02:38:06 +00001190 acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
bellard83f64092006-08-01 16:21:11 +00001191 bdrv_rw_em_cb, &async_ret);
pbrookce1a14d2006-08-07 02:38:06 +00001192 if (acb == NULL) {
bellard83f64092006-08-01 16:21:11 +00001193 qemu_aio_wait_end();
pbrookce1a14d2006-08-07 02:38:06 +00001194 return -1;
bellard83f64092006-08-01 16:21:11 +00001195 }
1196 while (async_ret == NOT_DONE) {
1197 qemu_aio_wait();
1198 }
1199 qemu_aio_wait_end();
1200 return async_ret;
1201}
1202
1203static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1204 const uint8_t *buf, int nb_sectors)
1205{
pbrookce1a14d2006-08-07 02:38:06 +00001206 int async_ret;
1207 BlockDriverAIOCB *acb;
bellard83f64092006-08-01 16:21:11 +00001208
bellard83f64092006-08-01 16:21:11 +00001209 async_ret = NOT_DONE;
1210 qemu_aio_wait_start();
pbrookce1a14d2006-08-07 02:38:06 +00001211 acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
bellard83f64092006-08-01 16:21:11 +00001212 bdrv_rw_em_cb, &async_ret);
pbrookce1a14d2006-08-07 02:38:06 +00001213 if (acb == NULL) {
bellard83f64092006-08-01 16:21:11 +00001214 qemu_aio_wait_end();
pbrookce1a14d2006-08-07 02:38:06 +00001215 return -1;
bellard83f64092006-08-01 16:21:11 +00001216 }
1217 while (async_ret == NOT_DONE) {
1218 qemu_aio_wait();
1219 }
1220 qemu_aio_wait_end();
1221 return async_ret;
1222}
bellardea2384d2004-08-01 21:59:26 +00001223
1224void bdrv_init(void)
1225{
1226 bdrv_register(&bdrv_raw);
bellard19cb3732006-08-19 11:45:59 +00001227 bdrv_register(&bdrv_host_device);
bellardea2384d2004-08-01 21:59:26 +00001228#ifndef _WIN32
1229 bdrv_register(&bdrv_cow);
1230#endif
1231 bdrv_register(&bdrv_qcow);
1232 bdrv_register(&bdrv_vmdk);
bellard3c565212004-09-29 21:29:14 +00001233 bdrv_register(&bdrv_cloop);
bellard585d0ed2004-12-12 11:24:44 +00001234 bdrv_register(&bdrv_dmg);
bellarda8753c32005-04-26 21:34:00 +00001235 bdrv_register(&bdrv_bochs);
bellard6a0f9e82005-04-27 20:17:58 +00001236 bdrv_register(&bdrv_vpc);
bellard712e7872005-04-28 21:09:32 +00001237 bdrv_register(&bdrv_vvfat);
bellardfaea38e2006-08-05 21:31:00 +00001238 bdrv_register(&bdrv_qcow2);
bellardea2384d2004-08-01 21:59:26 +00001239}
pbrookce1a14d2006-08-07 02:38:06 +00001240
1241void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1242 void *opaque)
1243{
1244 BlockDriver *drv;
1245 BlockDriverAIOCB *acb;
1246
1247 drv = bs->drv;
1248 if (drv->free_aiocb) {
1249 acb = drv->free_aiocb;
1250 drv->free_aiocb = acb->next;
1251 } else {
1252 acb = qemu_mallocz(drv->aiocb_size);
1253 if (!acb)
1254 return NULL;
1255 }
1256 acb->bs = bs;
1257 acb->cb = cb;
1258 acb->opaque = opaque;
1259 return acb;
1260}
1261
1262void qemu_aio_release(void *p)
1263{
1264 BlockDriverAIOCB *acb = p;
1265 BlockDriver *drv = acb->bs->drv;
1266 acb->next = drv->free_aiocb;
1267 drv->free_aiocb = acb;
1268}
bellard19cb3732006-08-19 11:45:59 +00001269
1270/**************************************************************/
1271/* removable device support */
1272
1273/**
1274 * Return TRUE if the media is present
1275 */
1276int bdrv_is_inserted(BlockDriverState *bs)
1277{
1278 BlockDriver *drv = bs->drv;
1279 int ret;
1280 if (!drv)
1281 return 0;
1282 if (!drv->bdrv_is_inserted)
1283 return 1;
1284 ret = drv->bdrv_is_inserted(bs);
1285 return ret;
1286}
1287
1288/**
1289 * Return TRUE if the media changed since the last call to this
1290 * function. It is currently only used for floppy disks
1291 */
1292int bdrv_media_changed(BlockDriverState *bs)
1293{
1294 BlockDriver *drv = bs->drv;
1295 int ret;
1296
1297 if (!drv || !drv->bdrv_media_changed)
1298 ret = -ENOTSUP;
1299 else
1300 ret = drv->bdrv_media_changed(bs);
1301 if (ret == -ENOTSUP)
1302 ret = bs->media_changed;
1303 bs->media_changed = 0;
1304 return ret;
1305}
1306
1307/**
1308 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1309 */
1310void bdrv_eject(BlockDriverState *bs, int eject_flag)
1311{
1312 BlockDriver *drv = bs->drv;
1313 int ret;
1314
1315 if (!drv || !drv->bdrv_eject) {
1316 ret = -ENOTSUP;
1317 } else {
1318 ret = drv->bdrv_eject(bs, eject_flag);
1319 }
1320 if (ret == -ENOTSUP) {
1321 if (eject_flag)
1322 bdrv_close(bs);
1323 }
1324}
1325
1326int bdrv_is_locked(BlockDriverState *bs)
1327{
1328 return bs->locked;
1329}
1330
1331/**
1332 * Lock or unlock the media (if it is locked, the user won't be able
1333 * to eject it manually).
1334 */
1335void bdrv_set_locked(BlockDriverState *bs, int locked)
1336{
1337 BlockDriver *drv = bs->drv;
1338
1339 bs->locked = locked;
1340 if (drv && drv->bdrv_set_locked) {
1341 drv->bdrv_set_locked(bs, locked);
1342 }
1343}