blob: 7c744c7409775ccb77d2f833d2dd48b1acfee47c [file] [log] [blame]
bellardfc01f7e2003-06-30 10:03:06 +00001/*
2 * QEMU System Emulator block driver
ths5fafdf22007-09-16 21:08:06 +00003 *
bellardfc01f7e2003-06-30 10:03:06 +00004 * Copyright (c) 2003 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellardfc01f7e2003-06-30 10:03:06 +00006 * 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 */
blueswir13990d092008-12-05 17:53:21 +000024#include "config-host.h"
25#ifdef _BSD
26/* include native header before sys-queue.h */
27#include <sys/queue.h>
28#endif
29
pbrookfaf07962007-11-11 02:51:17 +000030#include "qemu-common.h"
pbrook87ecb682007-11-17 17:14:51 +000031#include "console.h"
bellardea2384d2004-08-01 21:59:26 +000032#include "block_int.h"
bellardfc01f7e2003-06-30 10:03:06 +000033
bellard7674e7b2005-04-26 21:59:26 +000034#ifdef _BSD
35#include <sys/types.h>
36#include <sys/stat.h>
37#include <sys/ioctl.h>
bellard7674e7b2005-04-26 21:59:26 +000038#include <sys/disk.h>
39#endif
40
bellard83f64092006-08-01 16:21:11 +000041#define SECTOR_BITS 9
42#define SECTOR_SIZE (1 << SECTOR_BITS)
bellard3b0d4f62005-10-30 18:30:10 +000043
bellard90765422006-08-07 19:10:16 +000044typedef struct BlockDriverAIOCBSync {
45 BlockDriverAIOCB common;
46 QEMUBH *bh;
47 int ret;
48} BlockDriverAIOCBSync;
49
pbrookce1a14d2006-08-07 02:38:06 +000050static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
51 int64_t sector_num, uint8_t *buf, int nb_sectors,
52 BlockDriverCompletionFunc *cb, void *opaque);
53static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
54 int64_t sector_num, const uint8_t *buf, int nb_sectors,
55 BlockDriverCompletionFunc *cb, void *opaque);
bellard83f64092006-08-01 16:21:11 +000056static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
ths5fafdf22007-09-16 21:08:06 +000057static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +000058 uint8_t *buf, int nb_sectors);
59static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
60 const uint8_t *buf, int nb_sectors);
bellardec530c82006-04-25 22:36:06 +000061
blueswir17ee930d2008-09-17 19:04:14 +000062BlockDriverState *bdrv_first;
63
bellardea2384d2004-08-01 21:59:26 +000064static BlockDriver *first_drv;
65
bellard83f64092006-08-01 16:21:11 +000066int path_is_absolute(const char *path)
67{
68 const char *p;
bellard21664422007-01-07 18:22:37 +000069#ifdef _WIN32
70 /* specific case for names like: "\\.\d:" */
71 if (*path == '/' || *path == '\\')
72 return 1;
73#endif
bellard83f64092006-08-01 16:21:11 +000074 p = strchr(path, ':');
75 if (p)
76 p++;
77 else
78 p = path;
bellard3b9f94e2007-01-07 17:27:07 +000079#ifdef _WIN32
80 return (*p == '/' || *p == '\\');
81#else
82 return (*p == '/');
83#endif
bellard83f64092006-08-01 16:21:11 +000084}
85
86/* if filename is absolute, just copy it to dest. Otherwise, build a
87 path to it by considering it is relative to base_path. URL are
88 supported. */
89void path_combine(char *dest, int dest_size,
90 const char *base_path,
91 const char *filename)
92{
93 const char *p, *p1;
94 int len;
95
96 if (dest_size <= 0)
97 return;
98 if (path_is_absolute(filename)) {
99 pstrcpy(dest, dest_size, filename);
100 } else {
101 p = strchr(base_path, ':');
102 if (p)
103 p++;
104 else
105 p = base_path;
bellard3b9f94e2007-01-07 17:27:07 +0000106 p1 = strrchr(base_path, '/');
107#ifdef _WIN32
108 {
109 const char *p2;
110 p2 = strrchr(base_path, '\\');
111 if (!p1 || p2 > p1)
112 p1 = p2;
113 }
114#endif
bellard83f64092006-08-01 16:21:11 +0000115 if (p1)
116 p1++;
117 else
118 p1 = base_path;
119 if (p1 > p)
120 p = p1;
121 len = p - base_path;
122 if (len > dest_size - 1)
123 len = dest_size - 1;
124 memcpy(dest, base_path, len);
125 dest[len] = '\0';
126 pstrcat(dest, dest_size, filename);
127 }
128}
129
130
pbrook9596ebb2007-11-18 01:44:38 +0000131static void bdrv_register(BlockDriver *bdrv)
bellardea2384d2004-08-01 21:59:26 +0000132{
pbrookce1a14d2006-08-07 02:38:06 +0000133 if (!bdrv->bdrv_aio_read) {
bellard83f64092006-08-01 16:21:11 +0000134 /* add AIO emulation layer */
bellard83f64092006-08-01 16:21:11 +0000135 bdrv->bdrv_aio_read = bdrv_aio_read_em;
136 bdrv->bdrv_aio_write = bdrv_aio_write_em;
137 bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
bellard90765422006-08-07 19:10:16 +0000138 bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
bellard83f64092006-08-01 16:21:11 +0000139 } else if (!bdrv->bdrv_read && !bdrv->bdrv_pread) {
140 /* add synchronous IO emulation layer */
141 bdrv->bdrv_read = bdrv_read_em;
142 bdrv->bdrv_write = bdrv_write_em;
143 }
bellardea2384d2004-08-01 21:59:26 +0000144 bdrv->next = first_drv;
145 first_drv = bdrv;
146}
bellardb3380822004-03-14 21:38:54 +0000147
148/* create a new block device (by default it is empty) */
149BlockDriverState *bdrv_new(const char *device_name)
bellardfc01f7e2003-06-30 10:03:06 +0000150{
bellardb3380822004-03-14 21:38:54 +0000151 BlockDriverState **pbs, *bs;
152
153 bs = qemu_mallocz(sizeof(BlockDriverState));
bellardb3380822004-03-14 21:38:54 +0000154 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
bellardea2384d2004-08-01 21:59:26 +0000155 if (device_name[0] != '\0') {
156 /* insert at the end */
157 pbs = &bdrv_first;
158 while (*pbs != NULL)
159 pbs = &(*pbs)->next;
160 *pbs = bs;
161 }
bellardb3380822004-03-14 21:38:54 +0000162 return bs;
163}
164
bellardea2384d2004-08-01 21:59:26 +0000165BlockDriver *bdrv_find_format(const char *format_name)
166{
167 BlockDriver *drv1;
168 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
169 if (!strcmp(drv1->format_name, format_name))
170 return drv1;
171 }
172 return NULL;
173}
174
ths5fafdf22007-09-16 21:08:06 +0000175int bdrv_create(BlockDriver *drv,
bellardea2384d2004-08-01 21:59:26 +0000176 const char *filename, int64_t size_in_sectors,
177 const char *backing_file, int flags)
178{
179 if (!drv->bdrv_create)
180 return -ENOTSUP;
181 return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
182}
183
bellardd5249392004-08-03 21:14:23 +0000184#ifdef _WIN32
bellard95389c82005-12-18 18:28:15 +0000185void get_tmp_filename(char *filename, int size)
bellardd5249392004-08-03 21:14:23 +0000186{
bellard3b9f94e2007-01-07 17:27:07 +0000187 char temp_dir[MAX_PATH];
ths3b46e622007-09-17 08:09:54 +0000188
bellard3b9f94e2007-01-07 17:27:07 +0000189 GetTempPath(MAX_PATH, temp_dir);
190 GetTempFileName(temp_dir, "qem", 0, filename);
bellardd5249392004-08-03 21:14:23 +0000191}
192#else
bellard95389c82005-12-18 18:28:15 +0000193void get_tmp_filename(char *filename, int size)
bellardea2384d2004-08-01 21:59:26 +0000194{
195 int fd;
blueswir17ccfb2e2008-09-14 06:45:34 +0000196 const char *tmpdir;
bellardd5249392004-08-03 21:14:23 +0000197 /* XXX: race condition possible */
aurel320badc1e2008-03-10 00:05:34 +0000198 tmpdir = getenv("TMPDIR");
199 if (!tmpdir)
200 tmpdir = "/tmp";
201 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
bellardea2384d2004-08-01 21:59:26 +0000202 fd = mkstemp(filename);
203 close(fd);
204}
bellardd5249392004-08-03 21:14:23 +0000205#endif
bellardea2384d2004-08-01 21:59:26 +0000206
bellard19cb3732006-08-19 11:45:59 +0000207#ifdef _WIN32
bellardf45512f2006-08-23 21:40:13 +0000208static int is_windows_drive_prefix(const char *filename)
209{
210 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
211 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
212 filename[1] == ':');
213}
ths3b46e622007-09-17 08:09:54 +0000214
bellard19cb3732006-08-19 11:45:59 +0000215static int is_windows_drive(const char *filename)
216{
ths5fafdf22007-09-16 21:08:06 +0000217 if (is_windows_drive_prefix(filename) &&
bellardf45512f2006-08-23 21:40:13 +0000218 filename[2] == '\0')
bellard19cb3732006-08-19 11:45:59 +0000219 return 1;
220 if (strstart(filename, "\\\\.\\", NULL) ||
221 strstart(filename, "//./", NULL))
222 return 1;
223 return 0;
224}
225#endif
226
bellard83f64092006-08-01 16:21:11 +0000227static BlockDriver *find_protocol(const char *filename)
228{
229 BlockDriver *drv1;
230 char protocol[128];
231 int len;
232 const char *p;
bellard19cb3732006-08-19 11:45:59 +0000233
234#ifdef _WIN32
bellardf45512f2006-08-23 21:40:13 +0000235 if (is_windows_drive(filename) ||
236 is_windows_drive_prefix(filename))
bellard19cb3732006-08-19 11:45:59 +0000237 return &bdrv_raw;
238#endif
bellard83f64092006-08-01 16:21:11 +0000239 p = strchr(filename, ':');
240 if (!p)
241 return &bdrv_raw;
242 len = p - filename;
243 if (len > sizeof(protocol) - 1)
244 len = sizeof(protocol) - 1;
bellard83f64092006-08-01 16:21:11 +0000245 memcpy(protocol, filename, len);
246 protocol[len] = '\0';
247 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
ths5fafdf22007-09-16 21:08:06 +0000248 if (drv1->protocol_name &&
bellard83f64092006-08-01 16:21:11 +0000249 !strcmp(drv1->protocol_name, protocol))
250 return drv1;
251 }
252 return NULL;
253}
254
bellard7674e7b2005-04-26 21:59:26 +0000255/* XXX: force raw format if block or character device ? It would
256 simplify the BSD case */
bellardea2384d2004-08-01 21:59:26 +0000257static BlockDriver *find_image_format(const char *filename)
258{
bellard83f64092006-08-01 16:21:11 +0000259 int ret, score, score_max;
bellardea2384d2004-08-01 21:59:26 +0000260 BlockDriver *drv1, *drv;
bellard83f64092006-08-01 16:21:11 +0000261 uint8_t buf[2048];
262 BlockDriverState *bs;
ths3b46e622007-09-17 08:09:54 +0000263
bellard19cb3732006-08-19 11:45:59 +0000264 /* detect host devices. By convention, /dev/cdrom[N] is always
265 recognized as a host CDROM */
266 if (strstart(filename, "/dev/cdrom", NULL))
267 return &bdrv_host_device;
268#ifdef _WIN32
269 if (is_windows_drive(filename))
270 return &bdrv_host_device;
271#else
272 {
273 struct stat st;
ths5fafdf22007-09-16 21:08:06 +0000274 if (stat(filename, &st) >= 0 &&
bellard19cb3732006-08-19 11:45:59 +0000275 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
276 return &bdrv_host_device;
277 }
278 }
279#endif
ths3b46e622007-09-17 08:09:54 +0000280
bellard83f64092006-08-01 16:21:11 +0000281 drv = find_protocol(filename);
bellard19cb3732006-08-19 11:45:59 +0000282 /* no need to test disk image formats for vvfat */
bellard83f64092006-08-01 16:21:11 +0000283 if (drv == &bdrv_vvfat)
284 return drv;
bellard19cb3732006-08-19 11:45:59 +0000285
bellard83f64092006-08-01 16:21:11 +0000286 ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
287 if (ret < 0)
288 return NULL;
289 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
290 bdrv_delete(bs);
291 if (ret < 0) {
292 return NULL;
293 }
294
bellardea2384d2004-08-01 21:59:26 +0000295 score_max = 0;
296 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
bellard83f64092006-08-01 16:21:11 +0000297 if (drv1->bdrv_probe) {
298 score = drv1->bdrv_probe(buf, ret, filename);
299 if (score > score_max) {
300 score_max = score;
301 drv = drv1;
302 }
bellardea2384d2004-08-01 21:59:26 +0000303 }
304 }
305 return drv;
306}
307
bellard83f64092006-08-01 16:21:11 +0000308int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
bellardb3380822004-03-14 21:38:54 +0000309{
bellard83f64092006-08-01 16:21:11 +0000310 BlockDriverState *bs;
311 int ret;
312
313 bs = bdrv_new("");
314 if (!bs)
315 return -ENOMEM;
316 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
317 if (ret < 0) {
318 bdrv_delete(bs);
319 return ret;
bellard3b0d4f62005-10-30 18:30:10 +0000320 }
aliguori71d07702009-03-03 17:37:16 +0000321 bs->growable = 1;
bellard83f64092006-08-01 16:21:11 +0000322 *pbs = bs;
323 return 0;
bellardea2384d2004-08-01 21:59:26 +0000324}
bellardfc01f7e2003-06-30 10:03:06 +0000325
bellard83f64092006-08-01 16:21:11 +0000326int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
327{
328 return bdrv_open2(bs, filename, flags, NULL);
329}
330
331int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
bellardea2384d2004-08-01 21:59:26 +0000332 BlockDriver *drv)
333{
bellard83f64092006-08-01 16:21:11 +0000334 int ret, open_flags;
thseb5c8512007-02-11 15:06:09 +0000335 char tmp_filename[PATH_MAX];
336 char backing_filename[PATH_MAX];
ths3b46e622007-09-17 08:09:54 +0000337
bellardea2384d2004-08-01 21:59:26 +0000338 bs->read_only = 0;
339 bs->is_temporary = 0;
340 bs->encrypted = 0;
bellard712e7872005-04-28 21:09:32 +0000341
bellard83f64092006-08-01 16:21:11 +0000342 if (flags & BDRV_O_SNAPSHOT) {
bellardea2384d2004-08-01 21:59:26 +0000343 BlockDriverState *bs1;
344 int64_t total_size;
aliguori7c96d462008-09-12 17:54:13 +0000345 int is_protocol = 0;
ths3b46e622007-09-17 08:09:54 +0000346
bellardea2384d2004-08-01 21:59:26 +0000347 /* if snapshot, we create a temporary backing file and open it
348 instead of opening 'filename' directly */
349
350 /* if there is a backing file, use it */
351 bs1 = bdrv_new("");
352 if (!bs1) {
bellard83f64092006-08-01 16:21:11 +0000353 return -ENOMEM;
bellardea2384d2004-08-01 21:59:26 +0000354 }
355 if (bdrv_open(bs1, filename, 0) < 0) {
356 bdrv_delete(bs1);
357 return -1;
358 }
bellard83f64092006-08-01 16:21:11 +0000359 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
aliguori7c96d462008-09-12 17:54:13 +0000360
361 if (bs1->drv && bs1->drv->protocol_name)
362 is_protocol = 1;
363
bellardea2384d2004-08-01 21:59:26 +0000364 bdrv_delete(bs1);
ths3b46e622007-09-17 08:09:54 +0000365
bellardea2384d2004-08-01 21:59:26 +0000366 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
aliguori7c96d462008-09-12 17:54:13 +0000367
368 /* Real path is meaningless for protocols */
369 if (is_protocol)
370 snprintf(backing_filename, sizeof(backing_filename),
371 "%s", filename);
372 else
373 realpath(filename, backing_filename);
374
ths5fafdf22007-09-16 21:08:06 +0000375 if (bdrv_create(&bdrv_qcow2, tmp_filename,
bellarda817d932006-08-24 19:53:37 +0000376 total_size, backing_filename, 0) < 0) {
bellardea2384d2004-08-01 21:59:26 +0000377 return -1;
378 }
379 filename = tmp_filename;
380 bs->is_temporary = 1;
381 }
bellard712e7872005-04-28 21:09:32 +0000382
bellardea2384d2004-08-01 21:59:26 +0000383 pstrcpy(bs->filename, sizeof(bs->filename), filename);
bellard83f64092006-08-01 16:21:11 +0000384 if (flags & BDRV_O_FILE) {
385 drv = find_protocol(filename);
bellardea2384d2004-08-01 21:59:26 +0000386 if (!drv)
bellard83f64092006-08-01 16:21:11 +0000387 return -ENOENT;
388 } else {
389 if (!drv) {
390 drv = find_image_format(filename);
391 if (!drv)
392 return -1;
393 }
bellardea2384d2004-08-01 21:59:26 +0000394 }
395 bs->drv = drv;
396 bs->opaque = qemu_mallocz(drv->instance_size);
bellard83f64092006-08-01 16:21:11 +0000397 /* Note: for compatibility, we open disk image files as RDWR, and
398 RDONLY as fallback */
399 if (!(flags & BDRV_O_FILE))
aliguori9f7965c2008-10-14 14:42:54 +0000400 open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK);
bellard83f64092006-08-01 16:21:11 +0000401 else
402 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
403 ret = drv->bdrv_open(bs, filename, open_flags);
aurel32a0a83532008-10-13 21:08:34 +0000404 if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
aliguori9f7965c2008-10-14 14:42:54 +0000405 ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
bellard83f64092006-08-01 16:21:11 +0000406 bs->read_only = 1;
407 }
bellardea2384d2004-08-01 21:59:26 +0000408 if (ret < 0) {
409 qemu_free(bs->opaque);
bellard6b21b972006-08-23 21:14:37 +0000410 bs->opaque = NULL;
411 bs->drv = NULL;
bellard83f64092006-08-01 16:21:11 +0000412 return ret;
bellardea2384d2004-08-01 21:59:26 +0000413 }
bellardd15a7712006-08-06 13:35:09 +0000414 if (drv->bdrv_getlength) {
415 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
416 }
bellardea2384d2004-08-01 21:59:26 +0000417#ifndef _WIN32
418 if (bs->is_temporary) {
419 unlink(filename);
420 }
421#endif
bellard83f64092006-08-01 16:21:11 +0000422 if (bs->backing_file[0] != '\0') {
bellardea2384d2004-08-01 21:59:26 +0000423 /* if there is a backing file, use it */
424 bs->backing_hd = bdrv_new("");
425 if (!bs->backing_hd) {
426 fail:
427 bdrv_close(bs);
bellard6b21b972006-08-23 21:14:37 +0000428 return -ENOMEM;
bellardea2384d2004-08-01 21:59:26 +0000429 }
bellard83f64092006-08-01 16:21:11 +0000430 path_combine(backing_filename, sizeof(backing_filename),
431 filename, bs->backing_file);
aliguori9f7965c2008-10-14 14:42:54 +0000432 if (bdrv_open(bs->backing_hd, backing_filename, open_flags) < 0)
bellardea2384d2004-08-01 21:59:26 +0000433 goto fail;
434 }
435
bellardb3380822004-03-14 21:38:54 +0000436 /* call the change callback */
bellard19cb3732006-08-19 11:45:59 +0000437 bs->media_changed = 1;
bellardb3380822004-03-14 21:38:54 +0000438 if (bs->change_cb)
439 bs->change_cb(bs->change_opaque);
440
441 return 0;
bellardfc01f7e2003-06-30 10:03:06 +0000442}
443
444void bdrv_close(BlockDriverState *bs)
445{
bellard19cb3732006-08-19 11:45:59 +0000446 if (bs->drv) {
bellardea2384d2004-08-01 21:59:26 +0000447 if (bs->backing_hd)
448 bdrv_delete(bs->backing_hd);
449 bs->drv->bdrv_close(bs);
450 qemu_free(bs->opaque);
451#ifdef _WIN32
452 if (bs->is_temporary) {
453 unlink(bs->filename);
454 }
bellard67b915a2004-03-31 23:37:16 +0000455#endif
bellardea2384d2004-08-01 21:59:26 +0000456 bs->opaque = NULL;
457 bs->drv = NULL;
bellardb3380822004-03-14 21:38:54 +0000458
459 /* call the change callback */
bellard19cb3732006-08-19 11:45:59 +0000460 bs->media_changed = 1;
bellardb3380822004-03-14 21:38:54 +0000461 if (bs->change_cb)
462 bs->change_cb(bs->change_opaque);
463 }
464}
465
466void bdrv_delete(BlockDriverState *bs)
467{
aurel3234c6f052008-04-08 19:51:21 +0000468 BlockDriverState **pbs;
469
470 pbs = &bdrv_first;
471 while (*pbs != bs && *pbs != NULL)
472 pbs = &(*pbs)->next;
473 if (*pbs == bs)
474 *pbs = bs->next;
475
bellardb3380822004-03-14 21:38:54 +0000476 bdrv_close(bs);
477 qemu_free(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000478}
479
bellard33e39632003-07-06 17:15:21 +0000480/* commit COW file into the raw image */
481int bdrv_commit(BlockDriverState *bs)
482{
bellard19cb3732006-08-19 11:45:59 +0000483 BlockDriver *drv = bs->drv;
bellard83f64092006-08-01 16:21:11 +0000484 int64_t i, total_sectors;
bellardea2384d2004-08-01 21:59:26 +0000485 int n, j;
486 unsigned char sector[512];
bellard33e39632003-07-06 17:15:21 +0000487
bellard19cb3732006-08-19 11:45:59 +0000488 if (!drv)
489 return -ENOMEDIUM;
bellard33e39632003-07-06 17:15:21 +0000490
491 if (bs->read_only) {
bellardea2384d2004-08-01 21:59:26 +0000492 return -EACCES;
bellard33e39632003-07-06 17:15:21 +0000493 }
494
bellardea2384d2004-08-01 21:59:26 +0000495 if (!bs->backing_hd) {
496 return -ENOTSUP;
bellard33e39632003-07-06 17:15:21 +0000497 }
bellardea2384d2004-08-01 21:59:26 +0000498
bellard83f64092006-08-01 16:21:11 +0000499 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
500 for (i = 0; i < total_sectors;) {
bellard19cb3732006-08-19 11:45:59 +0000501 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
bellardea2384d2004-08-01 21:59:26 +0000502 for(j = 0; j < n; j++) {
503 if (bdrv_read(bs, i, sector, 1) != 0) {
504 return -EIO;
505 }
506
507 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
508 return -EIO;
509 }
510 i++;
511 }
512 } else {
513 i += n;
514 }
515 }
bellard95389c82005-12-18 18:28:15 +0000516
bellard19cb3732006-08-19 11:45:59 +0000517 if (drv->bdrv_make_empty)
518 return drv->bdrv_make_empty(bs);
bellard95389c82005-12-18 18:28:15 +0000519
bellard33e39632003-07-06 17:15:21 +0000520 return 0;
521}
522
aliguori71d07702009-03-03 17:37:16 +0000523static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
524 size_t size)
525{
526 int64_t len;
527
528 if (!bdrv_is_inserted(bs))
529 return -ENOMEDIUM;
530
531 if (bs->growable)
532 return 0;
533
534 len = bdrv_getlength(bs);
535
536 if ((offset + size) > len)
537 return -EIO;
538
539 return 0;
540}
541
542static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
543 int nb_sectors)
544{
545 int64_t offset;
546
547 /* Deal with byte accesses */
548 if (sector_num < 0)
549 offset = -sector_num;
550 else
551 offset = sector_num * 512;
552
553 return bdrv_check_byte_request(bs, offset, nb_sectors * 512);
554}
555
bellard19cb3732006-08-19 11:45:59 +0000556/* return < 0 if error. See bdrv_write() for the return codes */
ths5fafdf22007-09-16 21:08:06 +0000557int bdrv_read(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000558 uint8_t *buf, int nb_sectors)
559{
bellardea2384d2004-08-01 21:59:26 +0000560 BlockDriver *drv = bs->drv;
561
bellard19cb3732006-08-19 11:45:59 +0000562 if (!drv)
563 return -ENOMEDIUM;
aliguori71d07702009-03-03 17:37:16 +0000564 if (bdrv_check_request(bs, sector_num, nb_sectors))
565 return -EIO;
bellardb3380822004-03-14 21:38:54 +0000566
bellard83f64092006-08-01 16:21:11 +0000567 if (drv->bdrv_pread) {
568 int ret, len;
569 len = nb_sectors * 512;
570 ret = drv->bdrv_pread(bs, sector_num * 512, buf, len);
571 if (ret < 0)
572 return ret;
573 else if (ret != len)
bellard19cb3732006-08-19 11:45:59 +0000574 return -EINVAL;
thsa36e69d2007-12-02 05:18:19 +0000575 else {
576 bs->rd_bytes += (unsigned) len;
577 bs->rd_ops ++;
bellard83f64092006-08-01 16:21:11 +0000578 return 0;
thsa36e69d2007-12-02 05:18:19 +0000579 }
bellard83f64092006-08-01 16:21:11 +0000580 } else {
581 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
582 }
bellardfc01f7e2003-06-30 10:03:06 +0000583}
584
ths5fafdf22007-09-16 21:08:06 +0000585/* Return < 0 if error. Important errors are:
bellard19cb3732006-08-19 11:45:59 +0000586 -EIO generic I/O error (may happen for all errors)
587 -ENOMEDIUM No media inserted.
588 -EINVAL Invalid sector number or nb_sectors
589 -EACCES Trying to write a read-only device
590*/
ths5fafdf22007-09-16 21:08:06 +0000591int bdrv_write(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000592 const uint8_t *buf, int nb_sectors)
593{
bellard83f64092006-08-01 16:21:11 +0000594 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +0000595 if (!bs->drv)
596 return -ENOMEDIUM;
bellard0849bf02003-06-30 23:17:31 +0000597 if (bs->read_only)
bellard19cb3732006-08-19 11:45:59 +0000598 return -EACCES;
aliguori71d07702009-03-03 17:37:16 +0000599 if (bdrv_check_request(bs, sector_num, nb_sectors))
600 return -EIO;
601
bellard83f64092006-08-01 16:21:11 +0000602 if (drv->bdrv_pwrite) {
aliguori42fb2802009-01-15 20:43:39 +0000603 int ret, len, count = 0;
bellard83f64092006-08-01 16:21:11 +0000604 len = nb_sectors * 512;
aliguori42fb2802009-01-15 20:43:39 +0000605 do {
606 ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len - count);
607 if (ret < 0) {
608 printf("bdrv_write ret=%d\n", ret);
609 return ret;
610 }
611 count += ret;
612 buf += ret;
613 } while (count != len);
614 bs->wr_bytes += (unsigned) len;
615 bs->wr_ops ++;
616 return 0;
bellard83f64092006-08-01 16:21:11 +0000617 }
aliguori42fb2802009-01-15 20:43:39 +0000618 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
bellard83f64092006-08-01 16:21:11 +0000619}
620
ths5fafdf22007-09-16 21:08:06 +0000621static int bdrv_pread_em(BlockDriverState *bs, int64_t offset,
bellardfaea38e2006-08-05 21:31:00 +0000622 uint8_t *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000623{
bellard83f64092006-08-01 16:21:11 +0000624 uint8_t tmp_buf[SECTOR_SIZE];
625 int len, nb_sectors, count;
626 int64_t sector_num;
627
628 count = count1;
629 /* first read to align to sector start */
630 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
631 if (len > count)
632 len = count;
633 sector_num = offset >> SECTOR_BITS;
634 if (len > 0) {
635 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
636 return -EIO;
637 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
638 count -= len;
639 if (count == 0)
640 return count1;
641 sector_num++;
642 buf += len;
643 }
644
645 /* read the sectors "in place" */
646 nb_sectors = count >> SECTOR_BITS;
647 if (nb_sectors > 0) {
648 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
649 return -EIO;
650 sector_num += nb_sectors;
651 len = nb_sectors << SECTOR_BITS;
652 buf += len;
653 count -= len;
654 }
655
656 /* add data from the last sector */
657 if (count > 0) {
658 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
659 return -EIO;
660 memcpy(buf, tmp_buf, count);
661 }
662 return count1;
663}
664
ths5fafdf22007-09-16 21:08:06 +0000665static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset,
bellardfaea38e2006-08-05 21:31:00 +0000666 const uint8_t *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000667{
bellard83f64092006-08-01 16:21:11 +0000668 uint8_t tmp_buf[SECTOR_SIZE];
669 int len, nb_sectors, count;
670 int64_t sector_num;
671
672 count = count1;
673 /* first write to align to sector start */
674 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
675 if (len > count)
676 len = count;
677 sector_num = offset >> SECTOR_BITS;
678 if (len > 0) {
679 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
680 return -EIO;
681 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
682 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
683 return -EIO;
684 count -= len;
685 if (count == 0)
686 return count1;
687 sector_num++;
688 buf += len;
689 }
690
691 /* write the sectors "in place" */
692 nb_sectors = count >> SECTOR_BITS;
693 if (nb_sectors > 0) {
694 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
695 return -EIO;
696 sector_num += nb_sectors;
697 len = nb_sectors << SECTOR_BITS;
698 buf += len;
699 count -= len;
700 }
701
702 /* add data from the last sector */
703 if (count > 0) {
704 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
705 return -EIO;
706 memcpy(tmp_buf, buf, count);
707 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
708 return -EIO;
709 }
710 return count1;
711}
bellard83f64092006-08-01 16:21:11 +0000712
713/**
ths5fafdf22007-09-16 21:08:06 +0000714 * Read with byte offsets (needed only for file protocols)
bellard83f64092006-08-01 16:21:11 +0000715 */
ths5fafdf22007-09-16 21:08:06 +0000716int bdrv_pread(BlockDriverState *bs, int64_t offset,
bellard83f64092006-08-01 16:21:11 +0000717 void *buf1, int count1)
718{
719 BlockDriver *drv = bs->drv;
720
721 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000722 return -ENOMEDIUM;
aliguori71d07702009-03-03 17:37:16 +0000723 if (bdrv_check_byte_request(bs, offset, count1))
724 return -EIO;
725
bellard83f64092006-08-01 16:21:11 +0000726 if (!drv->bdrv_pread)
bellardfaea38e2006-08-05 21:31:00 +0000727 return bdrv_pread_em(bs, offset, buf1, count1);
bellard83f64092006-08-01 16:21:11 +0000728 return drv->bdrv_pread(bs, offset, buf1, count1);
729}
730
ths5fafdf22007-09-16 21:08:06 +0000731/**
732 * Write with byte offsets (needed only for file protocols)
bellard83f64092006-08-01 16:21:11 +0000733 */
ths5fafdf22007-09-16 21:08:06 +0000734int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
bellard83f64092006-08-01 16:21:11 +0000735 const void *buf1, int count1)
736{
737 BlockDriver *drv = bs->drv;
738
739 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000740 return -ENOMEDIUM;
aliguori71d07702009-03-03 17:37:16 +0000741 if (bdrv_check_byte_request(bs, offset, count1))
742 return -EIO;
743
bellard83f64092006-08-01 16:21:11 +0000744 if (!drv->bdrv_pwrite)
bellardfaea38e2006-08-05 21:31:00 +0000745 return bdrv_pwrite_em(bs, offset, buf1, count1);
bellard83f64092006-08-01 16:21:11 +0000746 return drv->bdrv_pwrite(bs, offset, buf1, count1);
747}
748
749/**
750 * Truncate file to 'offset' bytes (needed only for file protocols)
751 */
752int bdrv_truncate(BlockDriverState *bs, int64_t offset)
753{
754 BlockDriver *drv = bs->drv;
755 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000756 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000757 if (!drv->bdrv_truncate)
758 return -ENOTSUP;
759 return drv->bdrv_truncate(bs, offset);
760}
761
762/**
763 * Length of a file in bytes. Return < 0 if error or unknown.
764 */
765int64_t bdrv_getlength(BlockDriverState *bs)
766{
767 BlockDriver *drv = bs->drv;
768 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000769 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000770 if (!drv->bdrv_getlength) {
771 /* legacy mode */
772 return bs->total_sectors * SECTOR_SIZE;
773 }
774 return drv->bdrv_getlength(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000775}
776
bellard19cb3732006-08-19 11:45:59 +0000777/* return 0 as number of sectors if no device present or error */
ths96b8f132007-12-17 01:35:20 +0000778void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
bellardfc01f7e2003-06-30 10:03:06 +0000779{
bellard19cb3732006-08-19 11:45:59 +0000780 int64_t length;
781 length = bdrv_getlength(bs);
782 if (length < 0)
783 length = 0;
784 else
785 length = length >> SECTOR_BITS;
786 *nb_sectors_ptr = length;
bellardfc01f7e2003-06-30 10:03:06 +0000787}
bellardcf989512004-02-16 21:56:36 +0000788
aliguorif3d54fc2008-11-25 21:50:24 +0000789struct partition {
790 uint8_t boot_ind; /* 0x80 - active */
791 uint8_t head; /* starting head */
792 uint8_t sector; /* starting sector */
793 uint8_t cyl; /* starting cylinder */
794 uint8_t sys_ind; /* What partition type */
795 uint8_t end_head; /* end head */
796 uint8_t end_sector; /* end sector */
797 uint8_t end_cyl; /* end cylinder */
798 uint32_t start_sect; /* starting sector counting from 0 */
799 uint32_t nr_sects; /* nr of sectors in partition */
800} __attribute__((packed));
801
802/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
803static int guess_disk_lchs(BlockDriverState *bs,
804 int *pcylinders, int *pheads, int *psectors)
805{
806 uint8_t buf[512];
807 int ret, i, heads, sectors, cylinders;
808 struct partition *p;
809 uint32_t nr_sects;
blueswir1a38131b2008-12-05 17:56:40 +0000810 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +0000811
812 bdrv_get_geometry(bs, &nb_sectors);
813
814 ret = bdrv_read(bs, 0, buf, 1);
815 if (ret < 0)
816 return -1;
817 /* test msdos magic */
818 if (buf[510] != 0x55 || buf[511] != 0xaa)
819 return -1;
820 for(i = 0; i < 4; i++) {
821 p = ((struct partition *)(buf + 0x1be)) + i;
822 nr_sects = le32_to_cpu(p->nr_sects);
823 if (nr_sects && p->end_head) {
824 /* We make the assumption that the partition terminates on
825 a cylinder boundary */
826 heads = p->end_head + 1;
827 sectors = p->end_sector & 63;
828 if (sectors == 0)
829 continue;
830 cylinders = nb_sectors / (heads * sectors);
831 if (cylinders < 1 || cylinders > 16383)
832 continue;
833 *pheads = heads;
834 *psectors = sectors;
835 *pcylinders = cylinders;
836#if 0
837 printf("guessed geometry: LCHS=%d %d %d\n",
838 cylinders, heads, sectors);
839#endif
840 return 0;
841 }
842 }
843 return -1;
844}
845
846void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
847{
848 int translation, lba_detected = 0;
849 int cylinders, heads, secs;
blueswir1a38131b2008-12-05 17:56:40 +0000850 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +0000851
852 /* if a geometry hint is available, use it */
853 bdrv_get_geometry(bs, &nb_sectors);
854 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
855 translation = bdrv_get_translation_hint(bs);
856 if (cylinders != 0) {
857 *pcyls = cylinders;
858 *pheads = heads;
859 *psecs = secs;
860 } else {
861 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
862 if (heads > 16) {
863 /* if heads > 16, it means that a BIOS LBA
864 translation was active, so the default
865 hardware geometry is OK */
866 lba_detected = 1;
867 goto default_geometry;
868 } else {
869 *pcyls = cylinders;
870 *pheads = heads;
871 *psecs = secs;
872 /* disable any translation to be in sync with
873 the logical geometry */
874 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
875 bdrv_set_translation_hint(bs,
876 BIOS_ATA_TRANSLATION_NONE);
877 }
878 }
879 } else {
880 default_geometry:
881 /* if no geometry, use a standard physical disk geometry */
882 cylinders = nb_sectors / (16 * 63);
883
884 if (cylinders > 16383)
885 cylinders = 16383;
886 else if (cylinders < 2)
887 cylinders = 2;
888 *pcyls = cylinders;
889 *pheads = 16;
890 *psecs = 63;
891 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
892 if ((*pcyls * *pheads) <= 131072) {
893 bdrv_set_translation_hint(bs,
894 BIOS_ATA_TRANSLATION_LARGE);
895 } else {
896 bdrv_set_translation_hint(bs,
897 BIOS_ATA_TRANSLATION_LBA);
898 }
899 }
900 }
901 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
902 }
903}
904
ths5fafdf22007-09-16 21:08:06 +0000905void bdrv_set_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000906 int cyls, int heads, int secs)
907{
908 bs->cyls = cyls;
909 bs->heads = heads;
910 bs->secs = secs;
911}
912
913void bdrv_set_type_hint(BlockDriverState *bs, int type)
914{
915 bs->type = type;
916 bs->removable = ((type == BDRV_TYPE_CDROM ||
917 type == BDRV_TYPE_FLOPPY));
918}
919
bellard46d47672004-11-16 01:45:27 +0000920void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
921{
922 bs->translation = translation;
923}
924
ths5fafdf22007-09-16 21:08:06 +0000925void bdrv_get_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000926 int *pcyls, int *pheads, int *psecs)
927{
928 *pcyls = bs->cyls;
929 *pheads = bs->heads;
930 *psecs = bs->secs;
931}
932
933int bdrv_get_type_hint(BlockDriverState *bs)
934{
935 return bs->type;
936}
937
bellard46d47672004-11-16 01:45:27 +0000938int bdrv_get_translation_hint(BlockDriverState *bs)
939{
940 return bs->translation;
941}
942
bellardb3380822004-03-14 21:38:54 +0000943int bdrv_is_removable(BlockDriverState *bs)
944{
945 return bs->removable;
946}
947
948int bdrv_is_read_only(BlockDriverState *bs)
949{
950 return bs->read_only;
951}
952
ths985a03b2007-12-24 16:10:43 +0000953int bdrv_is_sg(BlockDriverState *bs)
954{
955 return bs->sg;
956}
957
bellard19cb3732006-08-19 11:45:59 +0000958/* XXX: no longer used */
ths5fafdf22007-09-16 21:08:06 +0000959void bdrv_set_change_cb(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000960 void (*change_cb)(void *opaque), void *opaque)
961{
962 bs->change_cb = change_cb;
963 bs->change_opaque = opaque;
964}
965
bellardea2384d2004-08-01 21:59:26 +0000966int bdrv_is_encrypted(BlockDriverState *bs)
967{
968 if (bs->backing_hd && bs->backing_hd->encrypted)
969 return 1;
970 return bs->encrypted;
971}
972
973int bdrv_set_key(BlockDriverState *bs, const char *key)
974{
975 int ret;
976 if (bs->backing_hd && bs->backing_hd->encrypted) {
977 ret = bdrv_set_key(bs->backing_hd, key);
978 if (ret < 0)
979 return ret;
980 if (!bs->encrypted)
981 return 0;
982 }
983 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
984 return -1;
985 return bs->drv->bdrv_set_key(bs, key);
986}
987
988void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
989{
bellard19cb3732006-08-19 11:45:59 +0000990 if (!bs->drv) {
bellardea2384d2004-08-01 21:59:26 +0000991 buf[0] = '\0';
992 } else {
993 pstrcpy(buf, buf_size, bs->drv->format_name);
994 }
995}
996
ths5fafdf22007-09-16 21:08:06 +0000997void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
bellardea2384d2004-08-01 21:59:26 +0000998 void *opaque)
999{
1000 BlockDriver *drv;
1001
1002 for (drv = first_drv; drv != NULL; drv = drv->next) {
1003 it(opaque, drv->format_name);
1004 }
1005}
1006
bellardb3380822004-03-14 21:38:54 +00001007BlockDriverState *bdrv_find(const char *name)
1008{
1009 BlockDriverState *bs;
1010
1011 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1012 if (!strcmp(name, bs->device_name))
1013 return bs;
1014 }
1015 return NULL;
1016}
1017
bellard81d09122004-07-14 17:21:37 +00001018void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
1019{
1020 BlockDriverState *bs;
1021
1022 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1023 it(opaque, bs->device_name);
1024 }
1025}
1026
bellardea2384d2004-08-01 21:59:26 +00001027const char *bdrv_get_device_name(BlockDriverState *bs)
1028{
1029 return bs->device_name;
1030}
1031
pbrook7a6cba62006-06-04 11:39:07 +00001032void bdrv_flush(BlockDriverState *bs)
1033{
1034 if (bs->drv->bdrv_flush)
1035 bs->drv->bdrv_flush(bs);
1036 if (bs->backing_hd)
1037 bdrv_flush(bs->backing_hd);
1038}
1039
aliguoric6ca28d2008-10-06 13:55:43 +00001040void bdrv_flush_all(void)
1041{
1042 BlockDriverState *bs;
1043
1044 for (bs = bdrv_first; bs != NULL; bs = bs->next)
1045 if (bs->drv && !bdrv_is_read_only(bs) &&
1046 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1047 bdrv_flush(bs);
1048}
1049
thsf58c7b32008-06-05 21:53:49 +00001050/*
1051 * Returns true iff the specified sector is present in the disk image. Drivers
1052 * not implementing the functionality are assumed to not support backing files,
1053 * hence all their sectors are reported as allocated.
1054 *
1055 * 'pnum' is set to the number of sectors (including and immediately following
1056 * the specified sector) that are known to be in the same
1057 * allocated/unallocated state.
1058 *
1059 * 'nb_sectors' is the max value 'pnum' should be set to.
1060 */
1061int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1062 int *pnum)
1063{
1064 int64_t n;
1065 if (!bs->drv->bdrv_is_allocated) {
1066 if (sector_num >= bs->total_sectors) {
1067 *pnum = 0;
1068 return 0;
1069 }
1070 n = bs->total_sectors - sector_num;
1071 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1072 return 1;
1073 }
1074 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1075}
1076
bellardb3380822004-03-14 21:38:54 +00001077void bdrv_info(void)
1078{
1079 BlockDriverState *bs;
1080
1081 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1082 term_printf("%s:", bs->device_name);
1083 term_printf(" type=");
1084 switch(bs->type) {
1085 case BDRV_TYPE_HD:
1086 term_printf("hd");
1087 break;
1088 case BDRV_TYPE_CDROM:
1089 term_printf("cdrom");
1090 break;
1091 case BDRV_TYPE_FLOPPY:
1092 term_printf("floppy");
1093 break;
1094 }
1095 term_printf(" removable=%d", bs->removable);
1096 if (bs->removable) {
1097 term_printf(" locked=%d", bs->locked);
1098 }
bellard19cb3732006-08-19 11:45:59 +00001099 if (bs->drv) {
thsfef30742006-12-22 14:11:32 +00001100 term_printf(" file=");
1101 term_print_filename(bs->filename);
1102 if (bs->backing_file[0] != '\0') {
1103 term_printf(" backing_file=");
1104 term_print_filename(bs->backing_file);
1105 }
bellardb3380822004-03-14 21:38:54 +00001106 term_printf(" ro=%d", bs->read_only);
bellardea2384d2004-08-01 21:59:26 +00001107 term_printf(" drv=%s", bs->drv->format_name);
1108 if (bs->encrypted)
1109 term_printf(" encrypted");
bellardb3380822004-03-14 21:38:54 +00001110 } else {
1111 term_printf(" [not inserted]");
1112 }
1113 term_printf("\n");
1114 }
1115}
thsa36e69d2007-12-02 05:18:19 +00001116
1117/* The "info blockstats" command. */
1118void bdrv_info_stats (void)
1119{
1120 BlockDriverState *bs;
aliguoria7cbfae2009-01-22 18:57:30 +00001121 BlockDriverInfo bdi;
thsa36e69d2007-12-02 05:18:19 +00001122
1123 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1124 term_printf ("%s:"
1125 " rd_bytes=%" PRIu64
1126 " wr_bytes=%" PRIu64
1127 " rd_operations=%" PRIu64
1128 " wr_operations=%" PRIu64
aliguoria7cbfae2009-01-22 18:57:30 +00001129 ,
thsa36e69d2007-12-02 05:18:19 +00001130 bs->device_name,
1131 bs->rd_bytes, bs->wr_bytes,
1132 bs->rd_ops, bs->wr_ops);
aliguoria7cbfae2009-01-22 18:57:30 +00001133 if (bdrv_get_info(bs, &bdi) == 0)
aliguori19875302009-01-22 18:57:34 +00001134 term_printf(" high=%" PRId64
1135 " bytes_free=%" PRId64,
1136 bdi.highest_alloc, bdi.num_free_bytes);
aliguoria7cbfae2009-01-22 18:57:30 +00001137 term_printf("\n");
thsa36e69d2007-12-02 05:18:19 +00001138 }
1139}
bellardea2384d2004-08-01 21:59:26 +00001140
ths5fafdf22007-09-16 21:08:06 +00001141void bdrv_get_backing_filename(BlockDriverState *bs,
bellard83f64092006-08-01 16:21:11 +00001142 char *filename, int filename_size)
bellardea2384d2004-08-01 21:59:26 +00001143{
bellard83f64092006-08-01 16:21:11 +00001144 if (!bs->backing_hd) {
1145 pstrcpy(filename, filename_size, "");
1146 } else {
1147 pstrcpy(filename, filename_size, bs->backing_file);
1148 }
bellardea2384d2004-08-01 21:59:26 +00001149}
1150
ths5fafdf22007-09-16 21:08:06 +00001151int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
bellardfaea38e2006-08-05 21:31:00 +00001152 const uint8_t *buf, int nb_sectors)
1153{
1154 BlockDriver *drv = bs->drv;
1155 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001156 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001157 if (!drv->bdrv_write_compressed)
1158 return -ENOTSUP;
1159 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1160}
ths3b46e622007-09-17 08:09:54 +00001161
bellardfaea38e2006-08-05 21:31:00 +00001162int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1163{
1164 BlockDriver *drv = bs->drv;
1165 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001166 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001167 if (!drv->bdrv_get_info)
1168 return -ENOTSUP;
1169 memset(bdi, 0, sizeof(*bdi));
1170 return drv->bdrv_get_info(bs, bdi);
1171}
1172
1173/**************************************************************/
1174/* handling of snapshots */
1175
ths5fafdf22007-09-16 21:08:06 +00001176int bdrv_snapshot_create(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001177 QEMUSnapshotInfo *sn_info)
1178{
1179 BlockDriver *drv = bs->drv;
1180 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001181 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001182 if (!drv->bdrv_snapshot_create)
1183 return -ENOTSUP;
1184 return drv->bdrv_snapshot_create(bs, sn_info);
1185}
1186
ths5fafdf22007-09-16 21:08:06 +00001187int bdrv_snapshot_goto(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001188 const char *snapshot_id)
1189{
1190 BlockDriver *drv = bs->drv;
1191 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001192 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001193 if (!drv->bdrv_snapshot_goto)
1194 return -ENOTSUP;
1195 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1196}
1197
1198int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1199{
1200 BlockDriver *drv = bs->drv;
1201 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001202 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001203 if (!drv->bdrv_snapshot_delete)
1204 return -ENOTSUP;
1205 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1206}
1207
ths5fafdf22007-09-16 21:08:06 +00001208int bdrv_snapshot_list(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001209 QEMUSnapshotInfo **psn_info)
1210{
1211 BlockDriver *drv = bs->drv;
1212 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001213 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001214 if (!drv->bdrv_snapshot_list)
1215 return -ENOTSUP;
1216 return drv->bdrv_snapshot_list(bs, psn_info);
1217}
1218
1219#define NB_SUFFIXES 4
1220
1221char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1222{
1223 static const char suffixes[NB_SUFFIXES] = "KMGT";
1224 int64_t base;
1225 int i;
1226
1227 if (size <= 999) {
1228 snprintf(buf, buf_size, "%" PRId64, size);
1229 } else {
1230 base = 1024;
1231 for(i = 0; i < NB_SUFFIXES; i++) {
1232 if (size < (10 * base)) {
ths5fafdf22007-09-16 21:08:06 +00001233 snprintf(buf, buf_size, "%0.1f%c",
bellardfaea38e2006-08-05 21:31:00 +00001234 (double)size / base,
1235 suffixes[i]);
1236 break;
1237 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
ths5fafdf22007-09-16 21:08:06 +00001238 snprintf(buf, buf_size, "%" PRId64 "%c",
bellardfaea38e2006-08-05 21:31:00 +00001239 ((size + (base >> 1)) / base),
1240 suffixes[i]);
1241 break;
1242 }
1243 base = base * 1024;
1244 }
1245 }
1246 return buf;
1247}
1248
1249char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1250{
1251 char buf1[128], date_buf[128], clock_buf[128];
bellard3b9f94e2007-01-07 17:27:07 +00001252#ifdef _WIN32
1253 struct tm *ptm;
1254#else
bellardfaea38e2006-08-05 21:31:00 +00001255 struct tm tm;
bellard3b9f94e2007-01-07 17:27:07 +00001256#endif
bellardfaea38e2006-08-05 21:31:00 +00001257 time_t ti;
1258 int64_t secs;
1259
1260 if (!sn) {
ths5fafdf22007-09-16 21:08:06 +00001261 snprintf(buf, buf_size,
1262 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001263 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1264 } else {
1265 ti = sn->date_sec;
bellard3b9f94e2007-01-07 17:27:07 +00001266#ifdef _WIN32
1267 ptm = localtime(&ti);
1268 strftime(date_buf, sizeof(date_buf),
1269 "%Y-%m-%d %H:%M:%S", ptm);
1270#else
bellardfaea38e2006-08-05 21:31:00 +00001271 localtime_r(&ti, &tm);
1272 strftime(date_buf, sizeof(date_buf),
1273 "%Y-%m-%d %H:%M:%S", &tm);
bellard3b9f94e2007-01-07 17:27:07 +00001274#endif
bellardfaea38e2006-08-05 21:31:00 +00001275 secs = sn->vm_clock_nsec / 1000000000;
1276 snprintf(clock_buf, sizeof(clock_buf),
1277 "%02d:%02d:%02d.%03d",
1278 (int)(secs / 3600),
1279 (int)((secs / 60) % 60),
ths5fafdf22007-09-16 21:08:06 +00001280 (int)(secs % 60),
bellardfaea38e2006-08-05 21:31:00 +00001281 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1282 snprintf(buf, buf_size,
ths5fafdf22007-09-16 21:08:06 +00001283 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001284 sn->id_str, sn->name,
1285 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1286 date_buf,
1287 clock_buf);
1288 }
1289 return buf;
1290}
1291
bellardea2384d2004-08-01 21:59:26 +00001292
bellard83f64092006-08-01 16:21:11 +00001293/**************************************************************/
1294/* async I/Os */
1295
aliguori3b69e4b2009-01-22 16:59:24 +00001296typedef struct VectorTranslationState {
1297 QEMUIOVector *iov;
1298 uint8_t *bounce;
1299 int is_write;
1300 BlockDriverAIOCB *aiocb;
1301 BlockDriverAIOCB *this_aiocb;
1302} VectorTranslationState;
1303
1304static void bdrv_aio_rw_vector_cb(void *opaque, int ret)
1305{
1306 VectorTranslationState *s = opaque;
1307
1308 if (!s->is_write) {
aliguori249aa742009-01-26 17:17:52 +00001309 qemu_iovec_from_buffer(s->iov, s->bounce, s->iov->size);
aliguori3b69e4b2009-01-22 16:59:24 +00001310 }
aurel32d905dba2009-03-03 06:28:26 +00001311 qemu_vfree(s->bounce);
aliguori3b69e4b2009-01-22 16:59:24 +00001312 s->this_aiocb->cb(s->this_aiocb->opaque, ret);
1313 qemu_aio_release(s->this_aiocb);
1314}
1315
1316static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
1317 int64_t sector_num,
1318 QEMUIOVector *iov,
1319 int nb_sectors,
1320 BlockDriverCompletionFunc *cb,
1321 void *opaque,
1322 int is_write)
1323
1324{
1325 VectorTranslationState *s = qemu_mallocz(sizeof(*s));
1326 BlockDriverAIOCB *aiocb = qemu_aio_get(bs, cb, opaque);
1327
1328 s->this_aiocb = aiocb;
1329 s->iov = iov;
1330 s->bounce = qemu_memalign(512, nb_sectors * 512);
1331 s->is_write = is_write;
1332 if (is_write) {
1333 qemu_iovec_to_buffer(s->iov, s->bounce);
1334 s->aiocb = bdrv_aio_write(bs, sector_num, s->bounce, nb_sectors,
1335 bdrv_aio_rw_vector_cb, s);
1336 } else {
1337 s->aiocb = bdrv_aio_read(bs, sector_num, s->bounce, nb_sectors,
1338 bdrv_aio_rw_vector_cb, s);
1339 }
1340 return aiocb;
1341}
1342
1343BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
1344 QEMUIOVector *iov, int nb_sectors,
1345 BlockDriverCompletionFunc *cb, void *opaque)
1346{
aliguori71d07702009-03-03 17:37:16 +00001347 if (bdrv_check_request(bs, sector_num, nb_sectors))
1348 return NULL;
1349
aliguori3b69e4b2009-01-22 16:59:24 +00001350 return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
1351 cb, opaque, 0);
1352}
1353
1354BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1355 QEMUIOVector *iov, int nb_sectors,
1356 BlockDriverCompletionFunc *cb, void *opaque)
1357{
aliguori71d07702009-03-03 17:37:16 +00001358 if (bdrv_check_request(bs, sector_num, nb_sectors))
1359 return NULL;
1360
aliguori3b69e4b2009-01-22 16:59:24 +00001361 return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
1362 cb, opaque, 1);
1363}
1364
pbrookce1a14d2006-08-07 02:38:06 +00001365BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1366 uint8_t *buf, int nb_sectors,
1367 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001368{
1369 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001370 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001371
bellard19cb3732006-08-19 11:45:59 +00001372 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001373 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001374 if (bdrv_check_request(bs, sector_num, nb_sectors))
1375 return NULL;
ths3b46e622007-09-17 08:09:54 +00001376
thsa36e69d2007-12-02 05:18:19 +00001377 ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1378
1379 if (ret) {
1380 /* Update stats even though technically transfer has not happened. */
1381 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1382 bs->rd_ops ++;
1383 }
1384
1385 return ret;
bellard83f64092006-08-01 16:21:11 +00001386}
1387
pbrookce1a14d2006-08-07 02:38:06 +00001388BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1389 const uint8_t *buf, int nb_sectors,
1390 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001391{
bellard83f64092006-08-01 16:21:11 +00001392 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001393 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001394
bellard19cb3732006-08-19 11:45:59 +00001395 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001396 return NULL;
bellard83f64092006-08-01 16:21:11 +00001397 if (bs->read_only)
pbrookce1a14d2006-08-07 02:38:06 +00001398 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001399 if (bdrv_check_request(bs, sector_num, nb_sectors))
1400 return NULL;
bellard83f64092006-08-01 16:21:11 +00001401
thsa36e69d2007-12-02 05:18:19 +00001402 ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1403
1404 if (ret) {
1405 /* Update stats even though technically transfer has not happened. */
1406 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1407 bs->wr_ops ++;
1408 }
1409
1410 return ret;
bellard83f64092006-08-01 16:21:11 +00001411}
1412
1413void bdrv_aio_cancel(BlockDriverAIOCB *acb)
pbrookce1a14d2006-08-07 02:38:06 +00001414{
1415 BlockDriver *drv = acb->bs->drv;
bellard83f64092006-08-01 16:21:11 +00001416
aliguori3b69e4b2009-01-22 16:59:24 +00001417 if (acb->cb == bdrv_aio_rw_vector_cb) {
1418 VectorTranslationState *s = acb->opaque;
1419 acb = s->aiocb;
1420 }
1421
bellard83f64092006-08-01 16:21:11 +00001422 drv->bdrv_aio_cancel(acb);
bellard83f64092006-08-01 16:21:11 +00001423}
1424
pbrookce1a14d2006-08-07 02:38:06 +00001425
bellard83f64092006-08-01 16:21:11 +00001426/**************************************************************/
1427/* async block device emulation */
1428
bellard83f64092006-08-01 16:21:11 +00001429static void bdrv_aio_bh_cb(void *opaque)
bellardbeac80c2006-06-26 20:08:57 +00001430{
pbrookce1a14d2006-08-07 02:38:06 +00001431 BlockDriverAIOCBSync *acb = opaque;
1432 acb->common.cb(acb->common.opaque, acb->ret);
1433 qemu_aio_release(acb);
bellardbeac80c2006-06-26 20:08:57 +00001434}
bellardbeac80c2006-06-26 20:08:57 +00001435
pbrookce1a14d2006-08-07 02:38:06 +00001436static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1437 int64_t sector_num, uint8_t *buf, int nb_sectors,
1438 BlockDriverCompletionFunc *cb, void *opaque)
bellardea2384d2004-08-01 21:59:26 +00001439{
pbrookce1a14d2006-08-07 02:38:06 +00001440 BlockDriverAIOCBSync *acb;
bellard83f64092006-08-01 16:21:11 +00001441 int ret;
pbrookce1a14d2006-08-07 02:38:06 +00001442
1443 acb = qemu_aio_get(bs, cb, opaque);
1444 if (!acb->bh)
1445 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1446 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1447 acb->ret = ret;
1448 qemu_bh_schedule(acb->bh);
1449 return &acb->common;
pbrook7a6cba62006-06-04 11:39:07 +00001450}
1451
pbrookce1a14d2006-08-07 02:38:06 +00001452static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1453 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1454 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001455{
pbrookce1a14d2006-08-07 02:38:06 +00001456 BlockDriverAIOCBSync *acb;
bellard83f64092006-08-01 16:21:11 +00001457 int ret;
pbrookce1a14d2006-08-07 02:38:06 +00001458
1459 acb = qemu_aio_get(bs, cb, opaque);
1460 if (!acb->bh)
1461 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1462 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1463 acb->ret = ret;
1464 qemu_bh_schedule(acb->bh);
1465 return &acb->common;
bellard83f64092006-08-01 16:21:11 +00001466}
1467
pbrookce1a14d2006-08-07 02:38:06 +00001468static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
bellard83f64092006-08-01 16:21:11 +00001469{
pbrookce1a14d2006-08-07 02:38:06 +00001470 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1471 qemu_bh_cancel(acb->bh);
1472 qemu_aio_release(acb);
bellard83f64092006-08-01 16:21:11 +00001473}
bellard83f64092006-08-01 16:21:11 +00001474
1475/**************************************************************/
1476/* sync block device emulation */
1477
1478static void bdrv_rw_em_cb(void *opaque, int ret)
1479{
1480 *(int *)opaque = ret;
1481}
1482
1483#define NOT_DONE 0x7fffffff
1484
ths5fafdf22007-09-16 21:08:06 +00001485static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +00001486 uint8_t *buf, int nb_sectors)
1487{
pbrookce1a14d2006-08-07 02:38:06 +00001488 int async_ret;
1489 BlockDriverAIOCB *acb;
bellard83f64092006-08-01 16:21:11 +00001490
bellard83f64092006-08-01 16:21:11 +00001491 async_ret = NOT_DONE;
ths5fafdf22007-09-16 21:08:06 +00001492 acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
bellard83f64092006-08-01 16:21:11 +00001493 bdrv_rw_em_cb, &async_ret);
aliguoribaf35cb2008-09-10 15:45:19 +00001494 if (acb == NULL)
pbrookce1a14d2006-08-07 02:38:06 +00001495 return -1;
aliguoribaf35cb2008-09-10 15:45:19 +00001496
bellard83f64092006-08-01 16:21:11 +00001497 while (async_ret == NOT_DONE) {
1498 qemu_aio_wait();
1499 }
aliguoribaf35cb2008-09-10 15:45:19 +00001500
bellard83f64092006-08-01 16:21:11 +00001501 return async_ret;
1502}
1503
1504static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1505 const uint8_t *buf, int nb_sectors)
1506{
pbrookce1a14d2006-08-07 02:38:06 +00001507 int async_ret;
1508 BlockDriverAIOCB *acb;
bellard83f64092006-08-01 16:21:11 +00001509
bellard83f64092006-08-01 16:21:11 +00001510 async_ret = NOT_DONE;
ths5fafdf22007-09-16 21:08:06 +00001511 acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
bellard83f64092006-08-01 16:21:11 +00001512 bdrv_rw_em_cb, &async_ret);
aliguoribaf35cb2008-09-10 15:45:19 +00001513 if (acb == NULL)
pbrookce1a14d2006-08-07 02:38:06 +00001514 return -1;
bellard83f64092006-08-01 16:21:11 +00001515 while (async_ret == NOT_DONE) {
1516 qemu_aio_wait();
1517 }
bellard83f64092006-08-01 16:21:11 +00001518 return async_ret;
1519}
bellardea2384d2004-08-01 21:59:26 +00001520
1521void bdrv_init(void)
1522{
1523 bdrv_register(&bdrv_raw);
bellard19cb3732006-08-19 11:45:59 +00001524 bdrv_register(&bdrv_host_device);
bellardea2384d2004-08-01 21:59:26 +00001525#ifndef _WIN32
1526 bdrv_register(&bdrv_cow);
1527#endif
1528 bdrv_register(&bdrv_qcow);
1529 bdrv_register(&bdrv_vmdk);
bellard3c565212004-09-29 21:29:14 +00001530 bdrv_register(&bdrv_cloop);
bellard585d0ed2004-12-12 11:24:44 +00001531 bdrv_register(&bdrv_dmg);
bellarda8753c32005-04-26 21:34:00 +00001532 bdrv_register(&bdrv_bochs);
bellard6a0f9e82005-04-27 20:17:58 +00001533 bdrv_register(&bdrv_vpc);
bellard712e7872005-04-28 21:09:32 +00001534 bdrv_register(&bdrv_vvfat);
bellardfaea38e2006-08-05 21:31:00 +00001535 bdrv_register(&bdrv_qcow2);
ths6ada7452007-07-31 23:28:53 +00001536 bdrv_register(&bdrv_parallels);
ths75818252008-07-03 13:41:03 +00001537 bdrv_register(&bdrv_nbd);
bellardea2384d2004-08-01 21:59:26 +00001538}
pbrookce1a14d2006-08-07 02:38:06 +00001539
1540void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1541 void *opaque)
1542{
1543 BlockDriver *drv;
1544 BlockDriverAIOCB *acb;
1545
1546 drv = bs->drv;
1547 if (drv->free_aiocb) {
1548 acb = drv->free_aiocb;
1549 drv->free_aiocb = acb->next;
1550 } else {
1551 acb = qemu_mallocz(drv->aiocb_size);
pbrookce1a14d2006-08-07 02:38:06 +00001552 }
1553 acb->bs = bs;
1554 acb->cb = cb;
1555 acb->opaque = opaque;
1556 return acb;
1557}
1558
1559void qemu_aio_release(void *p)
1560{
1561 BlockDriverAIOCB *acb = p;
1562 BlockDriver *drv = acb->bs->drv;
1563 acb->next = drv->free_aiocb;
1564 drv->free_aiocb = acb;
1565}
bellard19cb3732006-08-19 11:45:59 +00001566
1567/**************************************************************/
1568/* removable device support */
1569
1570/**
1571 * Return TRUE if the media is present
1572 */
1573int bdrv_is_inserted(BlockDriverState *bs)
1574{
1575 BlockDriver *drv = bs->drv;
1576 int ret;
1577 if (!drv)
1578 return 0;
1579 if (!drv->bdrv_is_inserted)
1580 return 1;
1581 ret = drv->bdrv_is_inserted(bs);
1582 return ret;
1583}
1584
1585/**
1586 * Return TRUE if the media changed since the last call to this
ths5fafdf22007-09-16 21:08:06 +00001587 * function. It is currently only used for floppy disks
bellard19cb3732006-08-19 11:45:59 +00001588 */
1589int bdrv_media_changed(BlockDriverState *bs)
1590{
1591 BlockDriver *drv = bs->drv;
1592 int ret;
1593
1594 if (!drv || !drv->bdrv_media_changed)
1595 ret = -ENOTSUP;
1596 else
1597 ret = drv->bdrv_media_changed(bs);
1598 if (ret == -ENOTSUP)
1599 ret = bs->media_changed;
1600 bs->media_changed = 0;
1601 return ret;
1602}
1603
1604/**
1605 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1606 */
1607void bdrv_eject(BlockDriverState *bs, int eject_flag)
1608{
1609 BlockDriver *drv = bs->drv;
1610 int ret;
1611
1612 if (!drv || !drv->bdrv_eject) {
1613 ret = -ENOTSUP;
1614 } else {
1615 ret = drv->bdrv_eject(bs, eject_flag);
1616 }
1617 if (ret == -ENOTSUP) {
1618 if (eject_flag)
1619 bdrv_close(bs);
1620 }
1621}
1622
1623int bdrv_is_locked(BlockDriverState *bs)
1624{
1625 return bs->locked;
1626}
1627
1628/**
1629 * Lock or unlock the media (if it is locked, the user won't be able
1630 * to eject it manually).
1631 */
1632void bdrv_set_locked(BlockDriverState *bs, int locked)
1633{
1634 BlockDriver *drv = bs->drv;
1635
1636 bs->locked = locked;
1637 if (drv && drv->bdrv_set_locked) {
1638 drv->bdrv_set_locked(bs, locked);
1639 }
1640}
ths985a03b2007-12-24 16:10:43 +00001641
1642/* needed for generic scsi interface */
1643
1644int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1645{
1646 BlockDriver *drv = bs->drv;
1647
1648 if (drv && drv->bdrv_ioctl)
1649 return drv->bdrv_ioctl(bs, req, buf);
1650 return -ENOTSUP;
1651}