blob: fd09dffb6ce809f330d380eef5d0a92c918a7503 [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"
blueswir1179a2c12009-03-08 08:23:32 +000025#ifdef HOST_BSD
blueswir13990d092008-12-05 17:53:21 +000026/* 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"
aliguori376253e2009-03-05 23:01:23 +000031#include "monitor.h"
bellardea2384d2004-08-01 21:59:26 +000032#include "block_int.h"
bellardfc01f7e2003-06-30 10:03:06 +000033
blueswir1179a2c12009-03-08 08:23:32 +000034#ifdef HOST_BSD
bellard7674e7b2005-04-26 21:59:26 +000035#include <sys/types.h>
36#include <sys/stat.h>
37#include <sys/ioctl.h>
blueswir1c5e97232009-03-07 20:06:23 +000038#ifndef __DragonFly__
bellard7674e7b2005-04-26 21:59:26 +000039#include <sys/disk.h>
40#endif
blueswir1c5e97232009-03-07 20:06:23 +000041#endif
bellard7674e7b2005-04-26 21:59:26 +000042
aliguori49dc7682009-03-08 16:26:59 +000043#ifdef _WIN32
44#include <windows.h>
45#endif
46
bellard83f64092006-08-01 16:21:11 +000047#define SECTOR_BITS 9
48#define SECTOR_SIZE (1 << SECTOR_BITS)
bellard3b0d4f62005-10-30 18:30:10 +000049
aliguoric07a9002009-03-20 18:26:03 +000050static AIOPool vectored_aio_pool;
51
bellard90765422006-08-07 19:10:16 +000052typedef struct BlockDriverAIOCBSync {
53 BlockDriverAIOCB common;
54 QEMUBH *bh;
55 int ret;
56} BlockDriverAIOCBSync;
57
pbrookce1a14d2006-08-07 02:38:06 +000058static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
59 int64_t sector_num, uint8_t *buf, int nb_sectors,
60 BlockDriverCompletionFunc *cb, void *opaque);
61static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
62 int64_t sector_num, const uint8_t *buf, int nb_sectors,
63 BlockDriverCompletionFunc *cb, void *opaque);
bellard83f64092006-08-01 16:21:11 +000064static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
ths5fafdf22007-09-16 21:08:06 +000065static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +000066 uint8_t *buf, int nb_sectors);
67static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
68 const uint8_t *buf, int nb_sectors);
bellardec530c82006-04-25 22:36:06 +000069
blueswir17ee930d2008-09-17 19:04:14 +000070BlockDriverState *bdrv_first;
71
bellardea2384d2004-08-01 21:59:26 +000072static BlockDriver *first_drv;
73
bellard83f64092006-08-01 16:21:11 +000074int path_is_absolute(const char *path)
75{
76 const char *p;
bellard21664422007-01-07 18:22:37 +000077#ifdef _WIN32
78 /* specific case for names like: "\\.\d:" */
79 if (*path == '/' || *path == '\\')
80 return 1;
81#endif
bellard83f64092006-08-01 16:21:11 +000082 p = strchr(path, ':');
83 if (p)
84 p++;
85 else
86 p = path;
bellard3b9f94e2007-01-07 17:27:07 +000087#ifdef _WIN32
88 return (*p == '/' || *p == '\\');
89#else
90 return (*p == '/');
91#endif
bellard83f64092006-08-01 16:21:11 +000092}
93
94/* if filename is absolute, just copy it to dest. Otherwise, build a
95 path to it by considering it is relative to base_path. URL are
96 supported. */
97void path_combine(char *dest, int dest_size,
98 const char *base_path,
99 const char *filename)
100{
101 const char *p, *p1;
102 int len;
103
104 if (dest_size <= 0)
105 return;
106 if (path_is_absolute(filename)) {
107 pstrcpy(dest, dest_size, filename);
108 } else {
109 p = strchr(base_path, ':');
110 if (p)
111 p++;
112 else
113 p = base_path;
bellard3b9f94e2007-01-07 17:27:07 +0000114 p1 = strrchr(base_path, '/');
115#ifdef _WIN32
116 {
117 const char *p2;
118 p2 = strrchr(base_path, '\\');
119 if (!p1 || p2 > p1)
120 p1 = p2;
121 }
122#endif
bellard83f64092006-08-01 16:21:11 +0000123 if (p1)
124 p1++;
125 else
126 p1 = base_path;
127 if (p1 > p)
128 p = p1;
129 len = p - base_path;
130 if (len > dest_size - 1)
131 len = dest_size - 1;
132 memcpy(dest, base_path, len);
133 dest[len] = '\0';
134 pstrcat(dest, dest_size, filename);
135 }
136}
137
138
pbrook9596ebb2007-11-18 01:44:38 +0000139static void bdrv_register(BlockDriver *bdrv)
bellardea2384d2004-08-01 21:59:26 +0000140{
pbrookce1a14d2006-08-07 02:38:06 +0000141 if (!bdrv->bdrv_aio_read) {
bellard83f64092006-08-01 16:21:11 +0000142 /* add AIO emulation layer */
bellard83f64092006-08-01 16:21:11 +0000143 bdrv->bdrv_aio_read = bdrv_aio_read_em;
144 bdrv->bdrv_aio_write = bdrv_aio_write_em;
145 bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
bellard90765422006-08-07 19:10:16 +0000146 bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
aliguorieda578e2009-03-12 19:57:16 +0000147 } else if (!bdrv->bdrv_read) {
bellard83f64092006-08-01 16:21:11 +0000148 /* add synchronous IO emulation layer */
149 bdrv->bdrv_read = bdrv_read_em;
150 bdrv->bdrv_write = bdrv_write_em;
151 }
aliguori6bbff9a2009-03-20 18:25:59 +0000152 aio_pool_init(&bdrv->aio_pool, bdrv->aiocb_size, bdrv->bdrv_aio_cancel);
bellardea2384d2004-08-01 21:59:26 +0000153 bdrv->next = first_drv;
154 first_drv = bdrv;
155}
bellardb3380822004-03-14 21:38:54 +0000156
157/* create a new block device (by default it is empty) */
158BlockDriverState *bdrv_new(const char *device_name)
bellardfc01f7e2003-06-30 10:03:06 +0000159{
bellardb3380822004-03-14 21:38:54 +0000160 BlockDriverState **pbs, *bs;
161
162 bs = qemu_mallocz(sizeof(BlockDriverState));
bellardb3380822004-03-14 21:38:54 +0000163 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
bellardea2384d2004-08-01 21:59:26 +0000164 if (device_name[0] != '\0') {
165 /* insert at the end */
166 pbs = &bdrv_first;
167 while (*pbs != NULL)
168 pbs = &(*pbs)->next;
169 *pbs = bs;
170 }
bellardb3380822004-03-14 21:38:54 +0000171 return bs;
172}
173
bellardea2384d2004-08-01 21:59:26 +0000174BlockDriver *bdrv_find_format(const char *format_name)
175{
176 BlockDriver *drv1;
177 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
178 if (!strcmp(drv1->format_name, format_name))
179 return drv1;
180 }
181 return NULL;
182}
183
ths5fafdf22007-09-16 21:08:06 +0000184int bdrv_create(BlockDriver *drv,
bellardea2384d2004-08-01 21:59:26 +0000185 const char *filename, int64_t size_in_sectors,
186 const char *backing_file, int flags)
187{
188 if (!drv->bdrv_create)
189 return -ENOTSUP;
190 return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
191}
192
bellardd5249392004-08-03 21:14:23 +0000193#ifdef _WIN32
bellard95389c82005-12-18 18:28:15 +0000194void get_tmp_filename(char *filename, int size)
bellardd5249392004-08-03 21:14:23 +0000195{
bellard3b9f94e2007-01-07 17:27:07 +0000196 char temp_dir[MAX_PATH];
ths3b46e622007-09-17 08:09:54 +0000197
bellard3b9f94e2007-01-07 17:27:07 +0000198 GetTempPath(MAX_PATH, temp_dir);
199 GetTempFileName(temp_dir, "qem", 0, filename);
bellardd5249392004-08-03 21:14:23 +0000200}
201#else
bellard95389c82005-12-18 18:28:15 +0000202void get_tmp_filename(char *filename, int size)
bellardea2384d2004-08-01 21:59:26 +0000203{
204 int fd;
blueswir17ccfb2e2008-09-14 06:45:34 +0000205 const char *tmpdir;
bellardd5249392004-08-03 21:14:23 +0000206 /* XXX: race condition possible */
aurel320badc1e2008-03-10 00:05:34 +0000207 tmpdir = getenv("TMPDIR");
208 if (!tmpdir)
209 tmpdir = "/tmp";
210 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
bellardea2384d2004-08-01 21:59:26 +0000211 fd = mkstemp(filename);
212 close(fd);
213}
bellardd5249392004-08-03 21:14:23 +0000214#endif
bellardea2384d2004-08-01 21:59:26 +0000215
bellard19cb3732006-08-19 11:45:59 +0000216#ifdef _WIN32
bellardf45512f2006-08-23 21:40:13 +0000217static int is_windows_drive_prefix(const char *filename)
218{
219 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
220 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
221 filename[1] == ':');
222}
ths3b46e622007-09-17 08:09:54 +0000223
bellard19cb3732006-08-19 11:45:59 +0000224static int is_windows_drive(const char *filename)
225{
ths5fafdf22007-09-16 21:08:06 +0000226 if (is_windows_drive_prefix(filename) &&
bellardf45512f2006-08-23 21:40:13 +0000227 filename[2] == '\0')
bellard19cb3732006-08-19 11:45:59 +0000228 return 1;
229 if (strstart(filename, "\\\\.\\", NULL) ||
230 strstart(filename, "//./", NULL))
231 return 1;
232 return 0;
233}
234#endif
235
bellard83f64092006-08-01 16:21:11 +0000236static BlockDriver *find_protocol(const char *filename)
237{
238 BlockDriver *drv1;
239 char protocol[128];
240 int len;
241 const char *p;
bellard19cb3732006-08-19 11:45:59 +0000242
243#ifdef _WIN32
bellardf45512f2006-08-23 21:40:13 +0000244 if (is_windows_drive(filename) ||
245 is_windows_drive_prefix(filename))
bellard19cb3732006-08-19 11:45:59 +0000246 return &bdrv_raw;
247#endif
bellard83f64092006-08-01 16:21:11 +0000248 p = strchr(filename, ':');
249 if (!p)
250 return &bdrv_raw;
251 len = p - filename;
252 if (len > sizeof(protocol) - 1)
253 len = sizeof(protocol) - 1;
bellard83f64092006-08-01 16:21:11 +0000254 memcpy(protocol, filename, len);
255 protocol[len] = '\0';
256 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
ths5fafdf22007-09-16 21:08:06 +0000257 if (drv1->protocol_name &&
bellard83f64092006-08-01 16:21:11 +0000258 !strcmp(drv1->protocol_name, protocol))
259 return drv1;
260 }
261 return NULL;
262}
263
bellard7674e7b2005-04-26 21:59:26 +0000264/* XXX: force raw format if block or character device ? It would
265 simplify the BSD case */
bellardea2384d2004-08-01 21:59:26 +0000266static BlockDriver *find_image_format(const char *filename)
267{
bellard83f64092006-08-01 16:21:11 +0000268 int ret, score, score_max;
bellardea2384d2004-08-01 21:59:26 +0000269 BlockDriver *drv1, *drv;
bellard83f64092006-08-01 16:21:11 +0000270 uint8_t buf[2048];
271 BlockDriverState *bs;
ths3b46e622007-09-17 08:09:54 +0000272
bellard19cb3732006-08-19 11:45:59 +0000273 /* detect host devices. By convention, /dev/cdrom[N] is always
274 recognized as a host CDROM */
275 if (strstart(filename, "/dev/cdrom", NULL))
276 return &bdrv_host_device;
277#ifdef _WIN32
278 if (is_windows_drive(filename))
279 return &bdrv_host_device;
280#else
281 {
282 struct stat st;
ths5fafdf22007-09-16 21:08:06 +0000283 if (stat(filename, &st) >= 0 &&
bellard19cb3732006-08-19 11:45:59 +0000284 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
285 return &bdrv_host_device;
286 }
287 }
288#endif
ths3b46e622007-09-17 08:09:54 +0000289
bellard83f64092006-08-01 16:21:11 +0000290 drv = find_protocol(filename);
bellard19cb3732006-08-19 11:45:59 +0000291 /* no need to test disk image formats for vvfat */
bellard83f64092006-08-01 16:21:11 +0000292 if (drv == &bdrv_vvfat)
293 return drv;
bellard19cb3732006-08-19 11:45:59 +0000294
bellard83f64092006-08-01 16:21:11 +0000295 ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
296 if (ret < 0)
297 return NULL;
298 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
299 bdrv_delete(bs);
300 if (ret < 0) {
301 return NULL;
302 }
303
bellardea2384d2004-08-01 21:59:26 +0000304 score_max = 0;
305 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
bellard83f64092006-08-01 16:21:11 +0000306 if (drv1->bdrv_probe) {
307 score = drv1->bdrv_probe(buf, ret, filename);
308 if (score > score_max) {
309 score_max = score;
310 drv = drv1;
311 }
bellardea2384d2004-08-01 21:59:26 +0000312 }
313 }
314 return drv;
315}
316
bellard83f64092006-08-01 16:21:11 +0000317int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
bellardb3380822004-03-14 21:38:54 +0000318{
bellard83f64092006-08-01 16:21:11 +0000319 BlockDriverState *bs;
320 int ret;
321
322 bs = bdrv_new("");
bellard83f64092006-08-01 16:21:11 +0000323 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
324 if (ret < 0) {
325 bdrv_delete(bs);
326 return ret;
bellard3b0d4f62005-10-30 18:30:10 +0000327 }
aliguori71d07702009-03-03 17:37:16 +0000328 bs->growable = 1;
bellard83f64092006-08-01 16:21:11 +0000329 *pbs = bs;
330 return 0;
bellardea2384d2004-08-01 21:59:26 +0000331}
bellardfc01f7e2003-06-30 10:03:06 +0000332
bellard83f64092006-08-01 16:21:11 +0000333int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
334{
335 return bdrv_open2(bs, filename, flags, NULL);
336}
337
338int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
bellardea2384d2004-08-01 21:59:26 +0000339 BlockDriver *drv)
340{
bellard83f64092006-08-01 16:21:11 +0000341 int ret, open_flags;
thseb5c8512007-02-11 15:06:09 +0000342 char tmp_filename[PATH_MAX];
343 char backing_filename[PATH_MAX];
ths3b46e622007-09-17 08:09:54 +0000344
bellardea2384d2004-08-01 21:59:26 +0000345 bs->read_only = 0;
346 bs->is_temporary = 0;
347 bs->encrypted = 0;
aliguoric0f4ce72009-03-05 23:01:01 +0000348 bs->valid_key = 0;
bellard712e7872005-04-28 21:09:32 +0000349
bellard83f64092006-08-01 16:21:11 +0000350 if (flags & BDRV_O_SNAPSHOT) {
bellardea2384d2004-08-01 21:59:26 +0000351 BlockDriverState *bs1;
352 int64_t total_size;
aliguori7c96d462008-09-12 17:54:13 +0000353 int is_protocol = 0;
ths3b46e622007-09-17 08:09:54 +0000354
bellardea2384d2004-08-01 21:59:26 +0000355 /* if snapshot, we create a temporary backing file and open it
356 instead of opening 'filename' directly */
357
358 /* if there is a backing file, use it */
359 bs1 = bdrv_new("");
aliguori51d7c002009-03-05 23:00:29 +0000360 ret = bdrv_open(bs1, filename, 0);
361 if (ret < 0) {
bellardea2384d2004-08-01 21:59:26 +0000362 bdrv_delete(bs1);
aliguori51d7c002009-03-05 23:00:29 +0000363 return ret;
bellardea2384d2004-08-01 21:59:26 +0000364 }
bellard83f64092006-08-01 16:21:11 +0000365 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
aliguori7c96d462008-09-12 17:54:13 +0000366
367 if (bs1->drv && bs1->drv->protocol_name)
368 is_protocol = 1;
369
bellardea2384d2004-08-01 21:59:26 +0000370 bdrv_delete(bs1);
ths3b46e622007-09-17 08:09:54 +0000371
bellardea2384d2004-08-01 21:59:26 +0000372 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
aliguori7c96d462008-09-12 17:54:13 +0000373
374 /* Real path is meaningless for protocols */
375 if (is_protocol)
376 snprintf(backing_filename, sizeof(backing_filename),
377 "%s", filename);
378 else
379 realpath(filename, backing_filename);
380
aliguori51d7c002009-03-05 23:00:29 +0000381 ret = bdrv_create(&bdrv_qcow2, tmp_filename,
382 total_size, backing_filename, 0);
383 if (ret < 0) {
384 return ret;
bellardea2384d2004-08-01 21:59:26 +0000385 }
386 filename = tmp_filename;
387 bs->is_temporary = 1;
388 }
bellard712e7872005-04-28 21:09:32 +0000389
bellardea2384d2004-08-01 21:59:26 +0000390 pstrcpy(bs->filename, sizeof(bs->filename), filename);
bellard83f64092006-08-01 16:21:11 +0000391 if (flags & BDRV_O_FILE) {
392 drv = find_protocol(filename);
aliguori51d7c002009-03-05 23:00:29 +0000393 } else if (!drv) {
394 drv = find_image_format(filename);
395 }
396 if (!drv) {
397 ret = -ENOENT;
398 goto unlink_and_fail;
bellardea2384d2004-08-01 21:59:26 +0000399 }
400 bs->drv = drv;
401 bs->opaque = qemu_mallocz(drv->instance_size);
bellard83f64092006-08-01 16:21:11 +0000402 /* Note: for compatibility, we open disk image files as RDWR, and
403 RDONLY as fallback */
404 if (!(flags & BDRV_O_FILE))
aliguori9f7965c2008-10-14 14:42:54 +0000405 open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK);
bellard83f64092006-08-01 16:21:11 +0000406 else
407 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
408 ret = drv->bdrv_open(bs, filename, open_flags);
aurel32a0a83532008-10-13 21:08:34 +0000409 if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
aliguori9f7965c2008-10-14 14:42:54 +0000410 ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
bellard83f64092006-08-01 16:21:11 +0000411 bs->read_only = 1;
412 }
bellardea2384d2004-08-01 21:59:26 +0000413 if (ret < 0) {
414 qemu_free(bs->opaque);
bellard6b21b972006-08-23 21:14:37 +0000415 bs->opaque = NULL;
416 bs->drv = NULL;
aliguori51d7c002009-03-05 23:00:29 +0000417 unlink_and_fail:
418 if (bs->is_temporary)
419 unlink(filename);
bellard83f64092006-08-01 16:21:11 +0000420 return ret;
bellardea2384d2004-08-01 21:59:26 +0000421 }
bellardd15a7712006-08-06 13:35:09 +0000422 if (drv->bdrv_getlength) {
423 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
424 }
bellardea2384d2004-08-01 21:59:26 +0000425#ifndef _WIN32
426 if (bs->is_temporary) {
427 unlink(filename);
428 }
429#endif
bellard83f64092006-08-01 16:21:11 +0000430 if (bs->backing_file[0] != '\0') {
bellardea2384d2004-08-01 21:59:26 +0000431 /* if there is a backing file, use it */
432 bs->backing_hd = bdrv_new("");
bellard83f64092006-08-01 16:21:11 +0000433 path_combine(backing_filename, sizeof(backing_filename),
434 filename, bs->backing_file);
aliguori51d7c002009-03-05 23:00:29 +0000435 ret = bdrv_open(bs->backing_hd, backing_filename, open_flags);
436 if (ret < 0) {
437 bdrv_close(bs);
438 return ret;
439 }
bellardea2384d2004-08-01 21:59:26 +0000440 }
441
aliguoribb5fc202009-03-05 23:01:15 +0000442 if (!bdrv_key_required(bs)) {
443 /* call the change callback */
444 bs->media_changed = 1;
445 if (bs->change_cb)
446 bs->change_cb(bs->change_opaque);
447 }
bellardb3380822004-03-14 21:38:54 +0000448 return 0;
bellardfc01f7e2003-06-30 10:03:06 +0000449}
450
451void bdrv_close(BlockDriverState *bs)
452{
bellard19cb3732006-08-19 11:45:59 +0000453 if (bs->drv) {
bellardea2384d2004-08-01 21:59:26 +0000454 if (bs->backing_hd)
455 bdrv_delete(bs->backing_hd);
456 bs->drv->bdrv_close(bs);
457 qemu_free(bs->opaque);
458#ifdef _WIN32
459 if (bs->is_temporary) {
460 unlink(bs->filename);
461 }
bellard67b915a2004-03-31 23:37:16 +0000462#endif
bellardea2384d2004-08-01 21:59:26 +0000463 bs->opaque = NULL;
464 bs->drv = NULL;
bellardb3380822004-03-14 21:38:54 +0000465
466 /* call the change callback */
bellard19cb3732006-08-19 11:45:59 +0000467 bs->media_changed = 1;
bellardb3380822004-03-14 21:38:54 +0000468 if (bs->change_cb)
469 bs->change_cb(bs->change_opaque);
470 }
471}
472
473void bdrv_delete(BlockDriverState *bs)
474{
aurel3234c6f052008-04-08 19:51:21 +0000475 BlockDriverState **pbs;
476
477 pbs = &bdrv_first;
478 while (*pbs != bs && *pbs != NULL)
479 pbs = &(*pbs)->next;
480 if (*pbs == bs)
481 *pbs = bs->next;
482
bellardb3380822004-03-14 21:38:54 +0000483 bdrv_close(bs);
484 qemu_free(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000485}
486
bellard33e39632003-07-06 17:15:21 +0000487/* commit COW file into the raw image */
488int bdrv_commit(BlockDriverState *bs)
489{
bellard19cb3732006-08-19 11:45:59 +0000490 BlockDriver *drv = bs->drv;
bellard83f64092006-08-01 16:21:11 +0000491 int64_t i, total_sectors;
bellardea2384d2004-08-01 21:59:26 +0000492 int n, j;
493 unsigned char sector[512];
bellard33e39632003-07-06 17:15:21 +0000494
bellard19cb3732006-08-19 11:45:59 +0000495 if (!drv)
496 return -ENOMEDIUM;
bellard33e39632003-07-06 17:15:21 +0000497
498 if (bs->read_only) {
bellardea2384d2004-08-01 21:59:26 +0000499 return -EACCES;
bellard33e39632003-07-06 17:15:21 +0000500 }
501
bellardea2384d2004-08-01 21:59:26 +0000502 if (!bs->backing_hd) {
503 return -ENOTSUP;
bellard33e39632003-07-06 17:15:21 +0000504 }
bellardea2384d2004-08-01 21:59:26 +0000505
bellard83f64092006-08-01 16:21:11 +0000506 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
507 for (i = 0; i < total_sectors;) {
bellard19cb3732006-08-19 11:45:59 +0000508 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
bellardea2384d2004-08-01 21:59:26 +0000509 for(j = 0; j < n; j++) {
510 if (bdrv_read(bs, i, sector, 1) != 0) {
511 return -EIO;
512 }
513
514 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
515 return -EIO;
516 }
517 i++;
518 }
519 } else {
520 i += n;
521 }
522 }
bellard95389c82005-12-18 18:28:15 +0000523
bellard19cb3732006-08-19 11:45:59 +0000524 if (drv->bdrv_make_empty)
525 return drv->bdrv_make_empty(bs);
bellard95389c82005-12-18 18:28:15 +0000526
bellard33e39632003-07-06 17:15:21 +0000527 return 0;
528}
529
aliguori71d07702009-03-03 17:37:16 +0000530static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
531 size_t size)
532{
533 int64_t len;
534
535 if (!bdrv_is_inserted(bs))
536 return -ENOMEDIUM;
537
538 if (bs->growable)
539 return 0;
540
541 len = bdrv_getlength(bs);
542
543 if ((offset + size) > len)
544 return -EIO;
545
546 return 0;
547}
548
549static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
550 int nb_sectors)
551{
552 int64_t offset;
553
554 /* Deal with byte accesses */
555 if (sector_num < 0)
556 offset = -sector_num;
557 else
558 offset = sector_num * 512;
559
560 return bdrv_check_byte_request(bs, offset, nb_sectors * 512);
561}
562
bellard19cb3732006-08-19 11:45:59 +0000563/* return < 0 if error. See bdrv_write() for the return codes */
ths5fafdf22007-09-16 21:08:06 +0000564int bdrv_read(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000565 uint8_t *buf, int nb_sectors)
566{
bellardea2384d2004-08-01 21:59:26 +0000567 BlockDriver *drv = bs->drv;
568
bellard19cb3732006-08-19 11:45:59 +0000569 if (!drv)
570 return -ENOMEDIUM;
aliguori71d07702009-03-03 17:37:16 +0000571 if (bdrv_check_request(bs, sector_num, nb_sectors))
572 return -EIO;
bellardb3380822004-03-14 21:38:54 +0000573
aliguorieda578e2009-03-12 19:57:16 +0000574 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
bellardfc01f7e2003-06-30 10:03:06 +0000575}
576
ths5fafdf22007-09-16 21:08:06 +0000577/* Return < 0 if error. Important errors are:
bellard19cb3732006-08-19 11:45:59 +0000578 -EIO generic I/O error (may happen for all errors)
579 -ENOMEDIUM No media inserted.
580 -EINVAL Invalid sector number or nb_sectors
581 -EACCES Trying to write a read-only device
582*/
ths5fafdf22007-09-16 21:08:06 +0000583int bdrv_write(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000584 const uint8_t *buf, int nb_sectors)
585{
bellard83f64092006-08-01 16:21:11 +0000586 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +0000587 if (!bs->drv)
588 return -ENOMEDIUM;
bellard0849bf02003-06-30 23:17:31 +0000589 if (bs->read_only)
bellard19cb3732006-08-19 11:45:59 +0000590 return -EACCES;
aliguori71d07702009-03-03 17:37:16 +0000591 if (bdrv_check_request(bs, sector_num, nb_sectors))
592 return -EIO;
593
aliguori42fb2802009-01-15 20:43:39 +0000594 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
bellard83f64092006-08-01 16:21:11 +0000595}
596
aliguorieda578e2009-03-12 19:57:16 +0000597int bdrv_pread(BlockDriverState *bs, int64_t offset,
598 void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000599{
bellard83f64092006-08-01 16:21:11 +0000600 uint8_t tmp_buf[SECTOR_SIZE];
601 int len, nb_sectors, count;
602 int64_t sector_num;
603
604 count = count1;
605 /* first read to align to sector start */
606 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
607 if (len > count)
608 len = count;
609 sector_num = offset >> SECTOR_BITS;
610 if (len > 0) {
611 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
612 return -EIO;
613 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
614 count -= len;
615 if (count == 0)
616 return count1;
617 sector_num++;
618 buf += len;
619 }
620
621 /* read the sectors "in place" */
622 nb_sectors = count >> SECTOR_BITS;
623 if (nb_sectors > 0) {
624 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
625 return -EIO;
626 sector_num += nb_sectors;
627 len = nb_sectors << SECTOR_BITS;
628 buf += len;
629 count -= len;
630 }
631
632 /* add data from the last sector */
633 if (count > 0) {
634 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
635 return -EIO;
636 memcpy(buf, tmp_buf, count);
637 }
638 return count1;
639}
640
aliguorieda578e2009-03-12 19:57:16 +0000641int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
642 const void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000643{
bellard83f64092006-08-01 16:21:11 +0000644 uint8_t tmp_buf[SECTOR_SIZE];
645 int len, nb_sectors, count;
646 int64_t sector_num;
647
648 count = count1;
649 /* first write to align to sector start */
650 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
651 if (len > count)
652 len = count;
653 sector_num = offset >> SECTOR_BITS;
654 if (len > 0) {
655 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
656 return -EIO;
657 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
658 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
659 return -EIO;
660 count -= len;
661 if (count == 0)
662 return count1;
663 sector_num++;
664 buf += len;
665 }
666
667 /* write the sectors "in place" */
668 nb_sectors = count >> SECTOR_BITS;
669 if (nb_sectors > 0) {
670 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
671 return -EIO;
672 sector_num += nb_sectors;
673 len = nb_sectors << SECTOR_BITS;
674 buf += len;
675 count -= len;
676 }
677
678 /* add data from the last sector */
679 if (count > 0) {
680 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
681 return -EIO;
682 memcpy(tmp_buf, buf, count);
683 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
684 return -EIO;
685 }
686 return count1;
687}
bellard83f64092006-08-01 16:21:11 +0000688
689/**
bellard83f64092006-08-01 16:21:11 +0000690 * Truncate file to 'offset' bytes (needed only for file protocols)
691 */
692int bdrv_truncate(BlockDriverState *bs, int64_t offset)
693{
694 BlockDriver *drv = bs->drv;
695 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000696 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000697 if (!drv->bdrv_truncate)
698 return -ENOTSUP;
699 return drv->bdrv_truncate(bs, offset);
700}
701
702/**
703 * Length of a file in bytes. Return < 0 if error or unknown.
704 */
705int64_t bdrv_getlength(BlockDriverState *bs)
706{
707 BlockDriver *drv = bs->drv;
708 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000709 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000710 if (!drv->bdrv_getlength) {
711 /* legacy mode */
712 return bs->total_sectors * SECTOR_SIZE;
713 }
714 return drv->bdrv_getlength(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000715}
716
bellard19cb3732006-08-19 11:45:59 +0000717/* return 0 as number of sectors if no device present or error */
ths96b8f132007-12-17 01:35:20 +0000718void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
bellardfc01f7e2003-06-30 10:03:06 +0000719{
bellard19cb3732006-08-19 11:45:59 +0000720 int64_t length;
721 length = bdrv_getlength(bs);
722 if (length < 0)
723 length = 0;
724 else
725 length = length >> SECTOR_BITS;
726 *nb_sectors_ptr = length;
bellardfc01f7e2003-06-30 10:03:06 +0000727}
bellardcf989512004-02-16 21:56:36 +0000728
aliguorif3d54fc2008-11-25 21:50:24 +0000729struct partition {
730 uint8_t boot_ind; /* 0x80 - active */
731 uint8_t head; /* starting head */
732 uint8_t sector; /* starting sector */
733 uint8_t cyl; /* starting cylinder */
734 uint8_t sys_ind; /* What partition type */
735 uint8_t end_head; /* end head */
736 uint8_t end_sector; /* end sector */
737 uint8_t end_cyl; /* end cylinder */
738 uint32_t start_sect; /* starting sector counting from 0 */
739 uint32_t nr_sects; /* nr of sectors in partition */
740} __attribute__((packed));
741
742/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
743static int guess_disk_lchs(BlockDriverState *bs,
744 int *pcylinders, int *pheads, int *psectors)
745{
746 uint8_t buf[512];
747 int ret, i, heads, sectors, cylinders;
748 struct partition *p;
749 uint32_t nr_sects;
blueswir1a38131b2008-12-05 17:56:40 +0000750 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +0000751
752 bdrv_get_geometry(bs, &nb_sectors);
753
754 ret = bdrv_read(bs, 0, buf, 1);
755 if (ret < 0)
756 return -1;
757 /* test msdos magic */
758 if (buf[510] != 0x55 || buf[511] != 0xaa)
759 return -1;
760 for(i = 0; i < 4; i++) {
761 p = ((struct partition *)(buf + 0x1be)) + i;
762 nr_sects = le32_to_cpu(p->nr_sects);
763 if (nr_sects && p->end_head) {
764 /* We make the assumption that the partition terminates on
765 a cylinder boundary */
766 heads = p->end_head + 1;
767 sectors = p->end_sector & 63;
768 if (sectors == 0)
769 continue;
770 cylinders = nb_sectors / (heads * sectors);
771 if (cylinders < 1 || cylinders > 16383)
772 continue;
773 *pheads = heads;
774 *psectors = sectors;
775 *pcylinders = cylinders;
776#if 0
777 printf("guessed geometry: LCHS=%d %d %d\n",
778 cylinders, heads, sectors);
779#endif
780 return 0;
781 }
782 }
783 return -1;
784}
785
786void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
787{
788 int translation, lba_detected = 0;
789 int cylinders, heads, secs;
blueswir1a38131b2008-12-05 17:56:40 +0000790 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +0000791
792 /* if a geometry hint is available, use it */
793 bdrv_get_geometry(bs, &nb_sectors);
794 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
795 translation = bdrv_get_translation_hint(bs);
796 if (cylinders != 0) {
797 *pcyls = cylinders;
798 *pheads = heads;
799 *psecs = secs;
800 } else {
801 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
802 if (heads > 16) {
803 /* if heads > 16, it means that a BIOS LBA
804 translation was active, so the default
805 hardware geometry is OK */
806 lba_detected = 1;
807 goto default_geometry;
808 } else {
809 *pcyls = cylinders;
810 *pheads = heads;
811 *psecs = secs;
812 /* disable any translation to be in sync with
813 the logical geometry */
814 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
815 bdrv_set_translation_hint(bs,
816 BIOS_ATA_TRANSLATION_NONE);
817 }
818 }
819 } else {
820 default_geometry:
821 /* if no geometry, use a standard physical disk geometry */
822 cylinders = nb_sectors / (16 * 63);
823
824 if (cylinders > 16383)
825 cylinders = 16383;
826 else if (cylinders < 2)
827 cylinders = 2;
828 *pcyls = cylinders;
829 *pheads = 16;
830 *psecs = 63;
831 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
832 if ((*pcyls * *pheads) <= 131072) {
833 bdrv_set_translation_hint(bs,
834 BIOS_ATA_TRANSLATION_LARGE);
835 } else {
836 bdrv_set_translation_hint(bs,
837 BIOS_ATA_TRANSLATION_LBA);
838 }
839 }
840 }
841 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
842 }
843}
844
ths5fafdf22007-09-16 21:08:06 +0000845void bdrv_set_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000846 int cyls, int heads, int secs)
847{
848 bs->cyls = cyls;
849 bs->heads = heads;
850 bs->secs = secs;
851}
852
853void bdrv_set_type_hint(BlockDriverState *bs, int type)
854{
855 bs->type = type;
856 bs->removable = ((type == BDRV_TYPE_CDROM ||
857 type == BDRV_TYPE_FLOPPY));
858}
859
bellard46d47672004-11-16 01:45:27 +0000860void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
861{
862 bs->translation = translation;
863}
864
ths5fafdf22007-09-16 21:08:06 +0000865void bdrv_get_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000866 int *pcyls, int *pheads, int *psecs)
867{
868 *pcyls = bs->cyls;
869 *pheads = bs->heads;
870 *psecs = bs->secs;
871}
872
873int bdrv_get_type_hint(BlockDriverState *bs)
874{
875 return bs->type;
876}
877
bellard46d47672004-11-16 01:45:27 +0000878int bdrv_get_translation_hint(BlockDriverState *bs)
879{
880 return bs->translation;
881}
882
bellardb3380822004-03-14 21:38:54 +0000883int bdrv_is_removable(BlockDriverState *bs)
884{
885 return bs->removable;
886}
887
888int bdrv_is_read_only(BlockDriverState *bs)
889{
890 return bs->read_only;
891}
892
ths985a03b2007-12-24 16:10:43 +0000893int bdrv_is_sg(BlockDriverState *bs)
894{
895 return bs->sg;
896}
897
bellard19cb3732006-08-19 11:45:59 +0000898/* XXX: no longer used */
ths5fafdf22007-09-16 21:08:06 +0000899void bdrv_set_change_cb(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000900 void (*change_cb)(void *opaque), void *opaque)
901{
902 bs->change_cb = change_cb;
903 bs->change_opaque = opaque;
904}
905
bellardea2384d2004-08-01 21:59:26 +0000906int bdrv_is_encrypted(BlockDriverState *bs)
907{
908 if (bs->backing_hd && bs->backing_hd->encrypted)
909 return 1;
910 return bs->encrypted;
911}
912
aliguoric0f4ce72009-03-05 23:01:01 +0000913int bdrv_key_required(BlockDriverState *bs)
914{
915 BlockDriverState *backing_hd = bs->backing_hd;
916
917 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
918 return 1;
919 return (bs->encrypted && !bs->valid_key);
920}
921
bellardea2384d2004-08-01 21:59:26 +0000922int bdrv_set_key(BlockDriverState *bs, const char *key)
923{
924 int ret;
925 if (bs->backing_hd && bs->backing_hd->encrypted) {
926 ret = bdrv_set_key(bs->backing_hd, key);
927 if (ret < 0)
928 return ret;
929 if (!bs->encrypted)
930 return 0;
931 }
932 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
933 return -1;
aliguoric0f4ce72009-03-05 23:01:01 +0000934 ret = bs->drv->bdrv_set_key(bs, key);
aliguoribb5fc202009-03-05 23:01:15 +0000935 if (ret < 0) {
936 bs->valid_key = 0;
937 } else if (!bs->valid_key) {
938 bs->valid_key = 1;
939 /* call the change callback now, we skipped it on open */
940 bs->media_changed = 1;
941 if (bs->change_cb)
942 bs->change_cb(bs->change_opaque);
943 }
aliguoric0f4ce72009-03-05 23:01:01 +0000944 return ret;
bellardea2384d2004-08-01 21:59:26 +0000945}
946
947void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
948{
bellard19cb3732006-08-19 11:45:59 +0000949 if (!bs->drv) {
bellardea2384d2004-08-01 21:59:26 +0000950 buf[0] = '\0';
951 } else {
952 pstrcpy(buf, buf_size, bs->drv->format_name);
953 }
954}
955
ths5fafdf22007-09-16 21:08:06 +0000956void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
bellardea2384d2004-08-01 21:59:26 +0000957 void *opaque)
958{
959 BlockDriver *drv;
960
961 for (drv = first_drv; drv != NULL; drv = drv->next) {
962 it(opaque, drv->format_name);
963 }
964}
965
bellardb3380822004-03-14 21:38:54 +0000966BlockDriverState *bdrv_find(const char *name)
967{
968 BlockDriverState *bs;
969
970 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
971 if (!strcmp(name, bs->device_name))
972 return bs;
973 }
974 return NULL;
975}
976
aliguori51de9762009-03-05 23:00:43 +0000977void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
bellard81d09122004-07-14 17:21:37 +0000978{
979 BlockDriverState *bs;
980
981 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori51de9762009-03-05 23:00:43 +0000982 it(opaque, bs);
bellard81d09122004-07-14 17:21:37 +0000983 }
984}
985
bellardea2384d2004-08-01 21:59:26 +0000986const char *bdrv_get_device_name(BlockDriverState *bs)
987{
988 return bs->device_name;
989}
990
pbrook7a6cba62006-06-04 11:39:07 +0000991void bdrv_flush(BlockDriverState *bs)
992{
993 if (bs->drv->bdrv_flush)
994 bs->drv->bdrv_flush(bs);
995 if (bs->backing_hd)
996 bdrv_flush(bs->backing_hd);
997}
998
aliguoric6ca28d2008-10-06 13:55:43 +0000999void bdrv_flush_all(void)
1000{
1001 BlockDriverState *bs;
1002
1003 for (bs = bdrv_first; bs != NULL; bs = bs->next)
1004 if (bs->drv && !bdrv_is_read_only(bs) &&
1005 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1006 bdrv_flush(bs);
1007}
1008
thsf58c7b32008-06-05 21:53:49 +00001009/*
1010 * Returns true iff the specified sector is present in the disk image. Drivers
1011 * not implementing the functionality are assumed to not support backing files,
1012 * hence all their sectors are reported as allocated.
1013 *
1014 * 'pnum' is set to the number of sectors (including and immediately following
1015 * the specified sector) that are known to be in the same
1016 * allocated/unallocated state.
1017 *
1018 * 'nb_sectors' is the max value 'pnum' should be set to.
1019 */
1020int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1021 int *pnum)
1022{
1023 int64_t n;
1024 if (!bs->drv->bdrv_is_allocated) {
1025 if (sector_num >= bs->total_sectors) {
1026 *pnum = 0;
1027 return 0;
1028 }
1029 n = bs->total_sectors - sector_num;
1030 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1031 return 1;
1032 }
1033 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1034}
1035
aliguori376253e2009-03-05 23:01:23 +00001036void bdrv_info(Monitor *mon)
bellardb3380822004-03-14 21:38:54 +00001037{
1038 BlockDriverState *bs;
1039
1040 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori376253e2009-03-05 23:01:23 +00001041 monitor_printf(mon, "%s:", bs->device_name);
1042 monitor_printf(mon, " type=");
bellardb3380822004-03-14 21:38:54 +00001043 switch(bs->type) {
1044 case BDRV_TYPE_HD:
aliguori376253e2009-03-05 23:01:23 +00001045 monitor_printf(mon, "hd");
bellardb3380822004-03-14 21:38:54 +00001046 break;
1047 case BDRV_TYPE_CDROM:
aliguori376253e2009-03-05 23:01:23 +00001048 monitor_printf(mon, "cdrom");
bellardb3380822004-03-14 21:38:54 +00001049 break;
1050 case BDRV_TYPE_FLOPPY:
aliguori376253e2009-03-05 23:01:23 +00001051 monitor_printf(mon, "floppy");
bellardb3380822004-03-14 21:38:54 +00001052 break;
1053 }
aliguori376253e2009-03-05 23:01:23 +00001054 monitor_printf(mon, " removable=%d", bs->removable);
bellardb3380822004-03-14 21:38:54 +00001055 if (bs->removable) {
aliguori376253e2009-03-05 23:01:23 +00001056 monitor_printf(mon, " locked=%d", bs->locked);
bellardb3380822004-03-14 21:38:54 +00001057 }
bellard19cb3732006-08-19 11:45:59 +00001058 if (bs->drv) {
aliguori376253e2009-03-05 23:01:23 +00001059 monitor_printf(mon, " file=");
1060 monitor_print_filename(mon, bs->filename);
thsfef30742006-12-22 14:11:32 +00001061 if (bs->backing_file[0] != '\0') {
aliguori376253e2009-03-05 23:01:23 +00001062 monitor_printf(mon, " backing_file=");
1063 monitor_print_filename(mon, bs->backing_file);
1064 }
1065 monitor_printf(mon, " ro=%d", bs->read_only);
1066 monitor_printf(mon, " drv=%s", bs->drv->format_name);
1067 monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs));
bellardb3380822004-03-14 21:38:54 +00001068 } else {
aliguori376253e2009-03-05 23:01:23 +00001069 monitor_printf(mon, " [not inserted]");
bellardb3380822004-03-14 21:38:54 +00001070 }
aliguori376253e2009-03-05 23:01:23 +00001071 monitor_printf(mon, "\n");
bellardb3380822004-03-14 21:38:54 +00001072 }
1073}
thsa36e69d2007-12-02 05:18:19 +00001074
1075/* The "info blockstats" command. */
aliguori376253e2009-03-05 23:01:23 +00001076void bdrv_info_stats(Monitor *mon)
thsa36e69d2007-12-02 05:18:19 +00001077{
1078 BlockDriverState *bs;
1079
1080 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori376253e2009-03-05 23:01:23 +00001081 monitor_printf(mon, "%s:"
1082 " rd_bytes=%" PRIu64
1083 " wr_bytes=%" PRIu64
1084 " rd_operations=%" PRIu64
1085 " wr_operations=%" PRIu64
aliguoriebf53fc2009-03-11 20:05:29 +00001086 "\n",
aliguori376253e2009-03-05 23:01:23 +00001087 bs->device_name,
1088 bs->rd_bytes, bs->wr_bytes,
1089 bs->rd_ops, bs->wr_ops);
thsa36e69d2007-12-02 05:18:19 +00001090 }
1091}
bellardea2384d2004-08-01 21:59:26 +00001092
aliguori045df332009-03-05 23:00:48 +00001093const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1094{
1095 if (bs->backing_hd && bs->backing_hd->encrypted)
1096 return bs->backing_file;
1097 else if (bs->encrypted)
1098 return bs->filename;
1099 else
1100 return NULL;
1101}
1102
ths5fafdf22007-09-16 21:08:06 +00001103void bdrv_get_backing_filename(BlockDriverState *bs,
bellard83f64092006-08-01 16:21:11 +00001104 char *filename, int filename_size)
bellardea2384d2004-08-01 21:59:26 +00001105{
bellard83f64092006-08-01 16:21:11 +00001106 if (!bs->backing_hd) {
1107 pstrcpy(filename, filename_size, "");
1108 } else {
1109 pstrcpy(filename, filename_size, bs->backing_file);
1110 }
bellardea2384d2004-08-01 21:59:26 +00001111}
1112
ths5fafdf22007-09-16 21:08:06 +00001113int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
bellardfaea38e2006-08-05 21:31:00 +00001114 const uint8_t *buf, int nb_sectors)
1115{
1116 BlockDriver *drv = bs->drv;
1117 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001118 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001119 if (!drv->bdrv_write_compressed)
1120 return -ENOTSUP;
1121 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1122}
ths3b46e622007-09-17 08:09:54 +00001123
bellardfaea38e2006-08-05 21:31:00 +00001124int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1125{
1126 BlockDriver *drv = bs->drv;
1127 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001128 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001129 if (!drv->bdrv_get_info)
1130 return -ENOTSUP;
1131 memset(bdi, 0, sizeof(*bdi));
1132 return drv->bdrv_get_info(bs, bdi);
1133}
1134
1135/**************************************************************/
1136/* handling of snapshots */
1137
ths5fafdf22007-09-16 21:08:06 +00001138int bdrv_snapshot_create(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001139 QEMUSnapshotInfo *sn_info)
1140{
1141 BlockDriver *drv = bs->drv;
1142 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001143 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001144 if (!drv->bdrv_snapshot_create)
1145 return -ENOTSUP;
1146 return drv->bdrv_snapshot_create(bs, sn_info);
1147}
1148
ths5fafdf22007-09-16 21:08:06 +00001149int bdrv_snapshot_goto(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001150 const char *snapshot_id)
1151{
1152 BlockDriver *drv = bs->drv;
1153 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001154 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001155 if (!drv->bdrv_snapshot_goto)
1156 return -ENOTSUP;
1157 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1158}
1159
1160int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1161{
1162 BlockDriver *drv = bs->drv;
1163 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001164 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001165 if (!drv->bdrv_snapshot_delete)
1166 return -ENOTSUP;
1167 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1168}
1169
ths5fafdf22007-09-16 21:08:06 +00001170int bdrv_snapshot_list(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001171 QEMUSnapshotInfo **psn_info)
1172{
1173 BlockDriver *drv = bs->drv;
1174 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001175 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001176 if (!drv->bdrv_snapshot_list)
1177 return -ENOTSUP;
1178 return drv->bdrv_snapshot_list(bs, psn_info);
1179}
1180
1181#define NB_SUFFIXES 4
1182
1183char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1184{
1185 static const char suffixes[NB_SUFFIXES] = "KMGT";
1186 int64_t base;
1187 int i;
1188
1189 if (size <= 999) {
1190 snprintf(buf, buf_size, "%" PRId64, size);
1191 } else {
1192 base = 1024;
1193 for(i = 0; i < NB_SUFFIXES; i++) {
1194 if (size < (10 * base)) {
ths5fafdf22007-09-16 21:08:06 +00001195 snprintf(buf, buf_size, "%0.1f%c",
bellardfaea38e2006-08-05 21:31:00 +00001196 (double)size / base,
1197 suffixes[i]);
1198 break;
1199 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
ths5fafdf22007-09-16 21:08:06 +00001200 snprintf(buf, buf_size, "%" PRId64 "%c",
bellardfaea38e2006-08-05 21:31:00 +00001201 ((size + (base >> 1)) / base),
1202 suffixes[i]);
1203 break;
1204 }
1205 base = base * 1024;
1206 }
1207 }
1208 return buf;
1209}
1210
1211char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1212{
1213 char buf1[128], date_buf[128], clock_buf[128];
bellard3b9f94e2007-01-07 17:27:07 +00001214#ifdef _WIN32
1215 struct tm *ptm;
1216#else
bellardfaea38e2006-08-05 21:31:00 +00001217 struct tm tm;
bellard3b9f94e2007-01-07 17:27:07 +00001218#endif
bellardfaea38e2006-08-05 21:31:00 +00001219 time_t ti;
1220 int64_t secs;
1221
1222 if (!sn) {
ths5fafdf22007-09-16 21:08:06 +00001223 snprintf(buf, buf_size,
1224 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001225 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1226 } else {
1227 ti = sn->date_sec;
bellard3b9f94e2007-01-07 17:27:07 +00001228#ifdef _WIN32
1229 ptm = localtime(&ti);
1230 strftime(date_buf, sizeof(date_buf),
1231 "%Y-%m-%d %H:%M:%S", ptm);
1232#else
bellardfaea38e2006-08-05 21:31:00 +00001233 localtime_r(&ti, &tm);
1234 strftime(date_buf, sizeof(date_buf),
1235 "%Y-%m-%d %H:%M:%S", &tm);
bellard3b9f94e2007-01-07 17:27:07 +00001236#endif
bellardfaea38e2006-08-05 21:31:00 +00001237 secs = sn->vm_clock_nsec / 1000000000;
1238 snprintf(clock_buf, sizeof(clock_buf),
1239 "%02d:%02d:%02d.%03d",
1240 (int)(secs / 3600),
1241 (int)((secs / 60) % 60),
ths5fafdf22007-09-16 21:08:06 +00001242 (int)(secs % 60),
bellardfaea38e2006-08-05 21:31:00 +00001243 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1244 snprintf(buf, buf_size,
ths5fafdf22007-09-16 21:08:06 +00001245 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001246 sn->id_str, sn->name,
1247 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1248 date_buf,
1249 clock_buf);
1250 }
1251 return buf;
1252}
1253
bellardea2384d2004-08-01 21:59:26 +00001254
bellard83f64092006-08-01 16:21:11 +00001255/**************************************************************/
1256/* async I/Os */
1257
aliguori3fb94d52009-03-20 18:26:12 +00001258typedef struct VectorTranslationAIOCB {
1259 BlockDriverAIOCB common;
aliguori3b69e4b2009-01-22 16:59:24 +00001260 QEMUIOVector *iov;
1261 uint8_t *bounce;
1262 int is_write;
1263 BlockDriverAIOCB *aiocb;
aliguori3fb94d52009-03-20 18:26:12 +00001264} VectorTranslationAIOCB;
aliguori3b69e4b2009-01-22 16:59:24 +00001265
aliguori3fb94d52009-03-20 18:26:12 +00001266static void bdrv_aio_cancel_vector(BlockDriverAIOCB *_acb)
aliguoric07a9002009-03-20 18:26:03 +00001267{
aliguori3fb94d52009-03-20 18:26:12 +00001268 VectorTranslationAIOCB *acb
1269 = container_of(_acb, VectorTranslationAIOCB, common);
aliguoric07a9002009-03-20 18:26:03 +00001270
aliguori3fb94d52009-03-20 18:26:12 +00001271 bdrv_aio_cancel(acb->aiocb);
aliguoric07a9002009-03-20 18:26:03 +00001272}
1273
aliguori3b69e4b2009-01-22 16:59:24 +00001274static void bdrv_aio_rw_vector_cb(void *opaque, int ret)
1275{
aliguori3fb94d52009-03-20 18:26:12 +00001276 VectorTranslationAIOCB *s = (VectorTranslationAIOCB *)opaque;
aliguori3b69e4b2009-01-22 16:59:24 +00001277
1278 if (!s->is_write) {
aliguori249aa742009-01-26 17:17:52 +00001279 qemu_iovec_from_buffer(s->iov, s->bounce, s->iov->size);
aliguori3b69e4b2009-01-22 16:59:24 +00001280 }
aurel32d905dba2009-03-03 06:28:26 +00001281 qemu_vfree(s->bounce);
aliguori3fb94d52009-03-20 18:26:12 +00001282 s->common.cb(s->common.opaque, ret);
1283 qemu_aio_release(s);
aliguori3b69e4b2009-01-22 16:59:24 +00001284}
1285
1286static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
1287 int64_t sector_num,
1288 QEMUIOVector *iov,
1289 int nb_sectors,
1290 BlockDriverCompletionFunc *cb,
1291 void *opaque,
1292 int is_write)
1293
1294{
aliguori3fb94d52009-03-20 18:26:12 +00001295 VectorTranslationAIOCB *s = qemu_aio_get_pool(&vectored_aio_pool, bs,
1296 cb, opaque);
aliguori3b69e4b2009-01-22 16:59:24 +00001297
aliguori3b69e4b2009-01-22 16:59:24 +00001298 s->iov = iov;
1299 s->bounce = qemu_memalign(512, nb_sectors * 512);
1300 s->is_write = is_write;
1301 if (is_write) {
1302 qemu_iovec_to_buffer(s->iov, s->bounce);
1303 s->aiocb = bdrv_aio_write(bs, sector_num, s->bounce, nb_sectors,
1304 bdrv_aio_rw_vector_cb, s);
1305 } else {
1306 s->aiocb = bdrv_aio_read(bs, sector_num, s->bounce, nb_sectors,
1307 bdrv_aio_rw_vector_cb, s);
1308 }
aliguori3fb94d52009-03-20 18:26:12 +00001309 return &s->common;
aliguori3b69e4b2009-01-22 16:59:24 +00001310}
1311
1312BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
1313 QEMUIOVector *iov, int nb_sectors,
1314 BlockDriverCompletionFunc *cb, void *opaque)
1315{
aliguori71d07702009-03-03 17:37:16 +00001316 if (bdrv_check_request(bs, sector_num, nb_sectors))
1317 return NULL;
1318
aliguori3b69e4b2009-01-22 16:59:24 +00001319 return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
1320 cb, opaque, 0);
1321}
1322
1323BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1324 QEMUIOVector *iov, int nb_sectors,
1325 BlockDriverCompletionFunc *cb, void *opaque)
1326{
aliguori71d07702009-03-03 17:37:16 +00001327 if (bdrv_check_request(bs, sector_num, nb_sectors))
1328 return NULL;
1329
aliguori3b69e4b2009-01-22 16:59:24 +00001330 return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
1331 cb, opaque, 1);
1332}
1333
pbrookce1a14d2006-08-07 02:38:06 +00001334BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1335 uint8_t *buf, int nb_sectors,
1336 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001337{
1338 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001339 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001340
bellard19cb3732006-08-19 11:45:59 +00001341 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001342 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001343 if (bdrv_check_request(bs, sector_num, nb_sectors))
1344 return NULL;
ths3b46e622007-09-17 08:09:54 +00001345
thsa36e69d2007-12-02 05:18:19 +00001346 ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1347
1348 if (ret) {
1349 /* Update stats even though technically transfer has not happened. */
1350 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1351 bs->rd_ops ++;
1352 }
1353
1354 return ret;
bellard83f64092006-08-01 16:21:11 +00001355}
1356
pbrookce1a14d2006-08-07 02:38:06 +00001357BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1358 const uint8_t *buf, int nb_sectors,
1359 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001360{
bellard83f64092006-08-01 16:21:11 +00001361 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001362 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001363
bellard19cb3732006-08-19 11:45:59 +00001364 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001365 return NULL;
bellard83f64092006-08-01 16:21:11 +00001366 if (bs->read_only)
pbrookce1a14d2006-08-07 02:38:06 +00001367 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001368 if (bdrv_check_request(bs, sector_num, nb_sectors))
1369 return NULL;
bellard83f64092006-08-01 16:21:11 +00001370
thsa36e69d2007-12-02 05:18:19 +00001371 ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1372
1373 if (ret) {
1374 /* Update stats even though technically transfer has not happened. */
1375 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1376 bs->wr_ops ++;
1377 }
1378
1379 return ret;
bellard83f64092006-08-01 16:21:11 +00001380}
1381
1382void bdrv_aio_cancel(BlockDriverAIOCB *acb)
pbrookce1a14d2006-08-07 02:38:06 +00001383{
aliguori6bbff9a2009-03-20 18:25:59 +00001384 acb->pool->cancel(acb);
bellard83f64092006-08-01 16:21:11 +00001385}
1386
pbrookce1a14d2006-08-07 02:38:06 +00001387
bellard83f64092006-08-01 16:21:11 +00001388/**************************************************************/
1389/* async block device emulation */
1390
bellard83f64092006-08-01 16:21:11 +00001391static void bdrv_aio_bh_cb(void *opaque)
bellardbeac80c2006-06-26 20:08:57 +00001392{
pbrookce1a14d2006-08-07 02:38:06 +00001393 BlockDriverAIOCBSync *acb = opaque;
1394 acb->common.cb(acb->common.opaque, acb->ret);
1395 qemu_aio_release(acb);
bellardbeac80c2006-06-26 20:08:57 +00001396}
bellardbeac80c2006-06-26 20:08:57 +00001397
pbrookce1a14d2006-08-07 02:38:06 +00001398static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1399 int64_t sector_num, uint8_t *buf, int nb_sectors,
1400 BlockDriverCompletionFunc *cb, void *opaque)
bellardea2384d2004-08-01 21:59:26 +00001401{
pbrookce1a14d2006-08-07 02:38:06 +00001402 BlockDriverAIOCBSync *acb;
bellard83f64092006-08-01 16:21:11 +00001403 int ret;
pbrookce1a14d2006-08-07 02:38:06 +00001404
1405 acb = qemu_aio_get(bs, cb, opaque);
1406 if (!acb->bh)
1407 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1408 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1409 acb->ret = ret;
1410 qemu_bh_schedule(acb->bh);
1411 return &acb->common;
pbrook7a6cba62006-06-04 11:39:07 +00001412}
1413
pbrookce1a14d2006-08-07 02:38:06 +00001414static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1415 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1416 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001417{
pbrookce1a14d2006-08-07 02:38:06 +00001418 BlockDriverAIOCBSync *acb;
bellard83f64092006-08-01 16:21:11 +00001419 int ret;
pbrookce1a14d2006-08-07 02:38:06 +00001420
1421 acb = qemu_aio_get(bs, cb, opaque);
1422 if (!acb->bh)
1423 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1424 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1425 acb->ret = ret;
1426 qemu_bh_schedule(acb->bh);
1427 return &acb->common;
bellard83f64092006-08-01 16:21:11 +00001428}
1429
pbrookce1a14d2006-08-07 02:38:06 +00001430static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
bellard83f64092006-08-01 16:21:11 +00001431{
pbrookce1a14d2006-08-07 02:38:06 +00001432 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1433 qemu_bh_cancel(acb->bh);
1434 qemu_aio_release(acb);
bellard83f64092006-08-01 16:21:11 +00001435}
bellard83f64092006-08-01 16:21:11 +00001436
1437/**************************************************************/
1438/* sync block device emulation */
1439
1440static void bdrv_rw_em_cb(void *opaque, int ret)
1441{
1442 *(int *)opaque = ret;
1443}
1444
1445#define NOT_DONE 0x7fffffff
1446
ths5fafdf22007-09-16 21:08:06 +00001447static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +00001448 uint8_t *buf, int nb_sectors)
1449{
pbrookce1a14d2006-08-07 02:38:06 +00001450 int async_ret;
1451 BlockDriverAIOCB *acb;
bellard83f64092006-08-01 16:21:11 +00001452
bellard83f64092006-08-01 16:21:11 +00001453 async_ret = NOT_DONE;
ths5fafdf22007-09-16 21:08:06 +00001454 acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
bellard83f64092006-08-01 16:21:11 +00001455 bdrv_rw_em_cb, &async_ret);
aliguoribaf35cb2008-09-10 15:45:19 +00001456 if (acb == NULL)
pbrookce1a14d2006-08-07 02:38:06 +00001457 return -1;
aliguoribaf35cb2008-09-10 15:45:19 +00001458
bellard83f64092006-08-01 16:21:11 +00001459 while (async_ret == NOT_DONE) {
1460 qemu_aio_wait();
1461 }
aliguoribaf35cb2008-09-10 15:45:19 +00001462
bellard83f64092006-08-01 16:21:11 +00001463 return async_ret;
1464}
1465
1466static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1467 const uint8_t *buf, int nb_sectors)
1468{
pbrookce1a14d2006-08-07 02:38:06 +00001469 int async_ret;
1470 BlockDriverAIOCB *acb;
bellard83f64092006-08-01 16:21:11 +00001471
bellard83f64092006-08-01 16:21:11 +00001472 async_ret = NOT_DONE;
ths5fafdf22007-09-16 21:08:06 +00001473 acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
bellard83f64092006-08-01 16:21:11 +00001474 bdrv_rw_em_cb, &async_ret);
aliguoribaf35cb2008-09-10 15:45:19 +00001475 if (acb == NULL)
pbrookce1a14d2006-08-07 02:38:06 +00001476 return -1;
bellard83f64092006-08-01 16:21:11 +00001477 while (async_ret == NOT_DONE) {
1478 qemu_aio_wait();
1479 }
bellard83f64092006-08-01 16:21:11 +00001480 return async_ret;
1481}
bellardea2384d2004-08-01 21:59:26 +00001482
1483void bdrv_init(void)
1484{
aliguori3fb94d52009-03-20 18:26:12 +00001485 aio_pool_init(&vectored_aio_pool, sizeof(VectorTranslationAIOCB),
aliguoric07a9002009-03-20 18:26:03 +00001486 bdrv_aio_cancel_vector);
1487
bellardea2384d2004-08-01 21:59:26 +00001488 bdrv_register(&bdrv_raw);
bellard19cb3732006-08-19 11:45:59 +00001489 bdrv_register(&bdrv_host_device);
bellardea2384d2004-08-01 21:59:26 +00001490#ifndef _WIN32
1491 bdrv_register(&bdrv_cow);
1492#endif
1493 bdrv_register(&bdrv_qcow);
1494 bdrv_register(&bdrv_vmdk);
bellard3c565212004-09-29 21:29:14 +00001495 bdrv_register(&bdrv_cloop);
bellard585d0ed2004-12-12 11:24:44 +00001496 bdrv_register(&bdrv_dmg);
bellarda8753c32005-04-26 21:34:00 +00001497 bdrv_register(&bdrv_bochs);
bellard6a0f9e82005-04-27 20:17:58 +00001498 bdrv_register(&bdrv_vpc);
bellard712e7872005-04-28 21:09:32 +00001499 bdrv_register(&bdrv_vvfat);
bellardfaea38e2006-08-05 21:31:00 +00001500 bdrv_register(&bdrv_qcow2);
ths6ada7452007-07-31 23:28:53 +00001501 bdrv_register(&bdrv_parallels);
ths75818252008-07-03 13:41:03 +00001502 bdrv_register(&bdrv_nbd);
bellardea2384d2004-08-01 21:59:26 +00001503}
pbrookce1a14d2006-08-07 02:38:06 +00001504
aliguori6bbff9a2009-03-20 18:25:59 +00001505void aio_pool_init(AIOPool *pool, int aiocb_size,
1506 void (*cancel)(BlockDriverAIOCB *acb))
pbrookce1a14d2006-08-07 02:38:06 +00001507{
aliguori6bbff9a2009-03-20 18:25:59 +00001508 pool->aiocb_size = aiocb_size;
1509 pool->cancel = cancel;
1510 pool->free_aiocb = NULL;
1511}
1512
1513void *qemu_aio_get_pool(AIOPool *pool, BlockDriverState *bs,
1514 BlockDriverCompletionFunc *cb, void *opaque)
1515{
pbrookce1a14d2006-08-07 02:38:06 +00001516 BlockDriverAIOCB *acb;
1517
aliguori6bbff9a2009-03-20 18:25:59 +00001518 if (pool->free_aiocb) {
1519 acb = pool->free_aiocb;
1520 pool->free_aiocb = acb->next;
pbrookce1a14d2006-08-07 02:38:06 +00001521 } else {
aliguori6bbff9a2009-03-20 18:25:59 +00001522 acb = qemu_mallocz(pool->aiocb_size);
1523 acb->pool = pool;
pbrookce1a14d2006-08-07 02:38:06 +00001524 }
1525 acb->bs = bs;
1526 acb->cb = cb;
1527 acb->opaque = opaque;
1528 return acb;
1529}
1530
aliguori6bbff9a2009-03-20 18:25:59 +00001531void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1532 void *opaque)
1533{
1534 return qemu_aio_get_pool(&bs->drv->aio_pool, bs, cb, opaque);
1535}
1536
pbrookce1a14d2006-08-07 02:38:06 +00001537void qemu_aio_release(void *p)
1538{
aliguori6bbff9a2009-03-20 18:25:59 +00001539 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
1540 AIOPool *pool = acb->pool;
1541 acb->next = pool->free_aiocb;
1542 pool->free_aiocb = acb;
pbrookce1a14d2006-08-07 02:38:06 +00001543}
bellard19cb3732006-08-19 11:45:59 +00001544
1545/**************************************************************/
1546/* removable device support */
1547
1548/**
1549 * Return TRUE if the media is present
1550 */
1551int bdrv_is_inserted(BlockDriverState *bs)
1552{
1553 BlockDriver *drv = bs->drv;
1554 int ret;
1555 if (!drv)
1556 return 0;
1557 if (!drv->bdrv_is_inserted)
1558 return 1;
1559 ret = drv->bdrv_is_inserted(bs);
1560 return ret;
1561}
1562
1563/**
1564 * Return TRUE if the media changed since the last call to this
ths5fafdf22007-09-16 21:08:06 +00001565 * function. It is currently only used for floppy disks
bellard19cb3732006-08-19 11:45:59 +00001566 */
1567int bdrv_media_changed(BlockDriverState *bs)
1568{
1569 BlockDriver *drv = bs->drv;
1570 int ret;
1571
1572 if (!drv || !drv->bdrv_media_changed)
1573 ret = -ENOTSUP;
1574 else
1575 ret = drv->bdrv_media_changed(bs);
1576 if (ret == -ENOTSUP)
1577 ret = bs->media_changed;
1578 bs->media_changed = 0;
1579 return ret;
1580}
1581
1582/**
1583 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1584 */
1585void bdrv_eject(BlockDriverState *bs, int eject_flag)
1586{
1587 BlockDriver *drv = bs->drv;
1588 int ret;
1589
1590 if (!drv || !drv->bdrv_eject) {
1591 ret = -ENOTSUP;
1592 } else {
1593 ret = drv->bdrv_eject(bs, eject_flag);
1594 }
1595 if (ret == -ENOTSUP) {
1596 if (eject_flag)
1597 bdrv_close(bs);
1598 }
1599}
1600
1601int bdrv_is_locked(BlockDriverState *bs)
1602{
1603 return bs->locked;
1604}
1605
1606/**
1607 * Lock or unlock the media (if it is locked, the user won't be able
1608 * to eject it manually).
1609 */
1610void bdrv_set_locked(BlockDriverState *bs, int locked)
1611{
1612 BlockDriver *drv = bs->drv;
1613
1614 bs->locked = locked;
1615 if (drv && drv->bdrv_set_locked) {
1616 drv->bdrv_set_locked(bs, locked);
1617 }
1618}
ths985a03b2007-12-24 16:10:43 +00001619
1620/* needed for generic scsi interface */
1621
1622int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1623{
1624 BlockDriver *drv = bs->drv;
1625
1626 if (drv && drv->bdrv_ioctl)
1627 return drv->bdrv_ioctl(bs, req, buf);
1628 return -ENOTSUP;
1629}
aliguori7d780662009-03-12 19:57:08 +00001630
1631int bdrv_sg_send_command(BlockDriverState *bs, void *buf, int count)
1632{
aliguori04eeb8b2009-03-12 19:57:12 +00001633 return bs->drv->bdrv_sg_send_command(bs, buf, count);
aliguori7d780662009-03-12 19:57:08 +00001634}
1635
1636int bdrv_sg_recv_response(BlockDriverState *bs, void *buf, int count)
1637{
aliguori04eeb8b2009-03-12 19:57:12 +00001638 return bs->drv->bdrv_sg_recv_response(bs, buf, count);
aliguori7d780662009-03-12 19:57:08 +00001639}
1640
1641BlockDriverAIOCB *bdrv_sg_aio_read(BlockDriverState *bs, void *buf, int count,
1642 BlockDriverCompletionFunc *cb, void *opaque)
1643{
aliguori04eeb8b2009-03-12 19:57:12 +00001644 return bs->drv->bdrv_sg_aio_read(bs, buf, count, cb, opaque);
aliguori7d780662009-03-12 19:57:08 +00001645}
1646
1647BlockDriverAIOCB *bdrv_sg_aio_write(BlockDriverState *bs, void *buf, int count,
1648 BlockDriverCompletionFunc *cb, void *opaque)
1649{
aliguori04eeb8b2009-03-12 19:57:12 +00001650 return bs->drv->bdrv_sg_aio_write(bs, buf, count, cb, opaque);
aliguori7d780662009-03-12 19:57:08 +00001651}