blob: ad16ee49356400c7e2c563dc70b24101fe8d6e3a [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
bellard90765422006-08-07 19:10:16 +000050typedef struct BlockDriverAIOCBSync {
51 BlockDriverAIOCB common;
52 QEMUBH *bh;
53 int ret;
54} BlockDriverAIOCBSync;
55
pbrookce1a14d2006-08-07 02:38:06 +000056static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
57 int64_t sector_num, uint8_t *buf, int nb_sectors,
58 BlockDriverCompletionFunc *cb, void *opaque);
59static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
60 int64_t sector_num, const uint8_t *buf, int nb_sectors,
61 BlockDriverCompletionFunc *cb, void *opaque);
bellard83f64092006-08-01 16:21:11 +000062static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
ths5fafdf22007-09-16 21:08:06 +000063static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +000064 uint8_t *buf, int nb_sectors);
65static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
66 const uint8_t *buf, int nb_sectors);
bellardec530c82006-04-25 22:36:06 +000067
blueswir17ee930d2008-09-17 19:04:14 +000068BlockDriverState *bdrv_first;
69
bellardea2384d2004-08-01 21:59:26 +000070static BlockDriver *first_drv;
71
bellard83f64092006-08-01 16:21:11 +000072int path_is_absolute(const char *path)
73{
74 const char *p;
bellard21664422007-01-07 18:22:37 +000075#ifdef _WIN32
76 /* specific case for names like: "\\.\d:" */
77 if (*path == '/' || *path == '\\')
78 return 1;
79#endif
bellard83f64092006-08-01 16:21:11 +000080 p = strchr(path, ':');
81 if (p)
82 p++;
83 else
84 p = path;
bellard3b9f94e2007-01-07 17:27:07 +000085#ifdef _WIN32
86 return (*p == '/' || *p == '\\');
87#else
88 return (*p == '/');
89#endif
bellard83f64092006-08-01 16:21:11 +000090}
91
92/* if filename is absolute, just copy it to dest. Otherwise, build a
93 path to it by considering it is relative to base_path. URL are
94 supported. */
95void path_combine(char *dest, int dest_size,
96 const char *base_path,
97 const char *filename)
98{
99 const char *p, *p1;
100 int len;
101
102 if (dest_size <= 0)
103 return;
104 if (path_is_absolute(filename)) {
105 pstrcpy(dest, dest_size, filename);
106 } else {
107 p = strchr(base_path, ':');
108 if (p)
109 p++;
110 else
111 p = base_path;
bellard3b9f94e2007-01-07 17:27:07 +0000112 p1 = strrchr(base_path, '/');
113#ifdef _WIN32
114 {
115 const char *p2;
116 p2 = strrchr(base_path, '\\');
117 if (!p1 || p2 > p1)
118 p1 = p2;
119 }
120#endif
bellard83f64092006-08-01 16:21:11 +0000121 if (p1)
122 p1++;
123 else
124 p1 = base_path;
125 if (p1 > p)
126 p = p1;
127 len = p - base_path;
128 if (len > dest_size - 1)
129 len = dest_size - 1;
130 memcpy(dest, base_path, len);
131 dest[len] = '\0';
132 pstrcat(dest, dest_size, filename);
133 }
134}
135
136
pbrook9596ebb2007-11-18 01:44:38 +0000137static void bdrv_register(BlockDriver *bdrv)
bellardea2384d2004-08-01 21:59:26 +0000138{
pbrookce1a14d2006-08-07 02:38:06 +0000139 if (!bdrv->bdrv_aio_read) {
bellard83f64092006-08-01 16:21:11 +0000140 /* add AIO emulation layer */
bellard83f64092006-08-01 16:21:11 +0000141 bdrv->bdrv_aio_read = bdrv_aio_read_em;
142 bdrv->bdrv_aio_write = bdrv_aio_write_em;
143 bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
bellard90765422006-08-07 19:10:16 +0000144 bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
aliguorieda578e2009-03-12 19:57:16 +0000145 } else if (!bdrv->bdrv_read) {
bellard83f64092006-08-01 16:21:11 +0000146 /* add synchronous IO emulation layer */
147 bdrv->bdrv_read = bdrv_read_em;
148 bdrv->bdrv_write = bdrv_write_em;
149 }
aliguori6bbff9a2009-03-20 18:25:59 +0000150 aio_pool_init(&bdrv->aio_pool, bdrv->aiocb_size, bdrv->bdrv_aio_cancel);
bellardea2384d2004-08-01 21:59:26 +0000151 bdrv->next = first_drv;
152 first_drv = bdrv;
153}
bellardb3380822004-03-14 21:38:54 +0000154
155/* create a new block device (by default it is empty) */
156BlockDriverState *bdrv_new(const char *device_name)
bellardfc01f7e2003-06-30 10:03:06 +0000157{
bellardb3380822004-03-14 21:38:54 +0000158 BlockDriverState **pbs, *bs;
159
160 bs = qemu_mallocz(sizeof(BlockDriverState));
bellardb3380822004-03-14 21:38:54 +0000161 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
bellardea2384d2004-08-01 21:59:26 +0000162 if (device_name[0] != '\0') {
163 /* insert at the end */
164 pbs = &bdrv_first;
165 while (*pbs != NULL)
166 pbs = &(*pbs)->next;
167 *pbs = bs;
168 }
bellardb3380822004-03-14 21:38:54 +0000169 return bs;
170}
171
bellardea2384d2004-08-01 21:59:26 +0000172BlockDriver *bdrv_find_format(const char *format_name)
173{
174 BlockDriver *drv1;
175 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
176 if (!strcmp(drv1->format_name, format_name))
177 return drv1;
178 }
179 return NULL;
180}
181
ths5fafdf22007-09-16 21:08:06 +0000182int bdrv_create(BlockDriver *drv,
bellardea2384d2004-08-01 21:59:26 +0000183 const char *filename, int64_t size_in_sectors,
184 const char *backing_file, int flags)
185{
186 if (!drv->bdrv_create)
187 return -ENOTSUP;
188 return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
189}
190
bellardd5249392004-08-03 21:14:23 +0000191#ifdef _WIN32
bellard95389c82005-12-18 18:28:15 +0000192void get_tmp_filename(char *filename, int size)
bellardd5249392004-08-03 21:14:23 +0000193{
bellard3b9f94e2007-01-07 17:27:07 +0000194 char temp_dir[MAX_PATH];
ths3b46e622007-09-17 08:09:54 +0000195
bellard3b9f94e2007-01-07 17:27:07 +0000196 GetTempPath(MAX_PATH, temp_dir);
197 GetTempFileName(temp_dir, "qem", 0, filename);
bellardd5249392004-08-03 21:14:23 +0000198}
199#else
bellard95389c82005-12-18 18:28:15 +0000200void get_tmp_filename(char *filename, int size)
bellardea2384d2004-08-01 21:59:26 +0000201{
202 int fd;
blueswir17ccfb2e2008-09-14 06:45:34 +0000203 const char *tmpdir;
bellardd5249392004-08-03 21:14:23 +0000204 /* XXX: race condition possible */
aurel320badc1e2008-03-10 00:05:34 +0000205 tmpdir = getenv("TMPDIR");
206 if (!tmpdir)
207 tmpdir = "/tmp";
208 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
bellardea2384d2004-08-01 21:59:26 +0000209 fd = mkstemp(filename);
210 close(fd);
211}
bellardd5249392004-08-03 21:14:23 +0000212#endif
bellardea2384d2004-08-01 21:59:26 +0000213
bellard19cb3732006-08-19 11:45:59 +0000214#ifdef _WIN32
bellardf45512f2006-08-23 21:40:13 +0000215static int is_windows_drive_prefix(const char *filename)
216{
217 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
218 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
219 filename[1] == ':');
220}
ths3b46e622007-09-17 08:09:54 +0000221
bellard19cb3732006-08-19 11:45:59 +0000222static int is_windows_drive(const char *filename)
223{
ths5fafdf22007-09-16 21:08:06 +0000224 if (is_windows_drive_prefix(filename) &&
bellardf45512f2006-08-23 21:40:13 +0000225 filename[2] == '\0')
bellard19cb3732006-08-19 11:45:59 +0000226 return 1;
227 if (strstart(filename, "\\\\.\\", NULL) ||
228 strstart(filename, "//./", NULL))
229 return 1;
230 return 0;
231}
232#endif
233
bellard83f64092006-08-01 16:21:11 +0000234static BlockDriver *find_protocol(const char *filename)
235{
236 BlockDriver *drv1;
237 char protocol[128];
238 int len;
239 const char *p;
bellard19cb3732006-08-19 11:45:59 +0000240
241#ifdef _WIN32
bellardf45512f2006-08-23 21:40:13 +0000242 if (is_windows_drive(filename) ||
243 is_windows_drive_prefix(filename))
bellard19cb3732006-08-19 11:45:59 +0000244 return &bdrv_raw;
245#endif
bellard83f64092006-08-01 16:21:11 +0000246 p = strchr(filename, ':');
247 if (!p)
248 return &bdrv_raw;
249 len = p - filename;
250 if (len > sizeof(protocol) - 1)
251 len = sizeof(protocol) - 1;
bellard83f64092006-08-01 16:21:11 +0000252 memcpy(protocol, filename, len);
253 protocol[len] = '\0';
254 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
ths5fafdf22007-09-16 21:08:06 +0000255 if (drv1->protocol_name &&
bellard83f64092006-08-01 16:21:11 +0000256 !strcmp(drv1->protocol_name, protocol))
257 return drv1;
258 }
259 return NULL;
260}
261
bellard7674e7b2005-04-26 21:59:26 +0000262/* XXX: force raw format if block or character device ? It would
263 simplify the BSD case */
bellardea2384d2004-08-01 21:59:26 +0000264static BlockDriver *find_image_format(const char *filename)
265{
bellard83f64092006-08-01 16:21:11 +0000266 int ret, score, score_max;
bellardea2384d2004-08-01 21:59:26 +0000267 BlockDriver *drv1, *drv;
bellard83f64092006-08-01 16:21:11 +0000268 uint8_t buf[2048];
269 BlockDriverState *bs;
ths3b46e622007-09-17 08:09:54 +0000270
bellard19cb3732006-08-19 11:45:59 +0000271 /* detect host devices. By convention, /dev/cdrom[N] is always
272 recognized as a host CDROM */
273 if (strstart(filename, "/dev/cdrom", NULL))
274 return &bdrv_host_device;
275#ifdef _WIN32
276 if (is_windows_drive(filename))
277 return &bdrv_host_device;
278#else
279 {
280 struct stat st;
ths5fafdf22007-09-16 21:08:06 +0000281 if (stat(filename, &st) >= 0 &&
bellard19cb3732006-08-19 11:45:59 +0000282 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
283 return &bdrv_host_device;
284 }
285 }
286#endif
ths3b46e622007-09-17 08:09:54 +0000287
bellard83f64092006-08-01 16:21:11 +0000288 drv = find_protocol(filename);
bellard19cb3732006-08-19 11:45:59 +0000289 /* no need to test disk image formats for vvfat */
bellard83f64092006-08-01 16:21:11 +0000290 if (drv == &bdrv_vvfat)
291 return drv;
bellard19cb3732006-08-19 11:45:59 +0000292
bellard83f64092006-08-01 16:21:11 +0000293 ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
294 if (ret < 0)
295 return NULL;
296 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
297 bdrv_delete(bs);
298 if (ret < 0) {
299 return NULL;
300 }
301
bellardea2384d2004-08-01 21:59:26 +0000302 score_max = 0;
303 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
bellard83f64092006-08-01 16:21:11 +0000304 if (drv1->bdrv_probe) {
305 score = drv1->bdrv_probe(buf, ret, filename);
306 if (score > score_max) {
307 score_max = score;
308 drv = drv1;
309 }
bellardea2384d2004-08-01 21:59:26 +0000310 }
311 }
312 return drv;
313}
314
bellard83f64092006-08-01 16:21:11 +0000315int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
bellardb3380822004-03-14 21:38:54 +0000316{
bellard83f64092006-08-01 16:21:11 +0000317 BlockDriverState *bs;
318 int ret;
319
320 bs = bdrv_new("");
bellard83f64092006-08-01 16:21:11 +0000321 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
322 if (ret < 0) {
323 bdrv_delete(bs);
324 return ret;
bellard3b0d4f62005-10-30 18:30:10 +0000325 }
aliguori71d07702009-03-03 17:37:16 +0000326 bs->growable = 1;
bellard83f64092006-08-01 16:21:11 +0000327 *pbs = bs;
328 return 0;
bellardea2384d2004-08-01 21:59:26 +0000329}
bellardfc01f7e2003-06-30 10:03:06 +0000330
bellard83f64092006-08-01 16:21:11 +0000331int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
332{
333 return bdrv_open2(bs, filename, flags, NULL);
334}
335
336int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
bellardea2384d2004-08-01 21:59:26 +0000337 BlockDriver *drv)
338{
bellard83f64092006-08-01 16:21:11 +0000339 int ret, open_flags;
thseb5c8512007-02-11 15:06:09 +0000340 char tmp_filename[PATH_MAX];
341 char backing_filename[PATH_MAX];
ths3b46e622007-09-17 08:09:54 +0000342
bellardea2384d2004-08-01 21:59:26 +0000343 bs->read_only = 0;
344 bs->is_temporary = 0;
345 bs->encrypted = 0;
aliguoric0f4ce72009-03-05 23:01:01 +0000346 bs->valid_key = 0;
bellard712e7872005-04-28 21:09:32 +0000347
bellard83f64092006-08-01 16:21:11 +0000348 if (flags & BDRV_O_SNAPSHOT) {
bellardea2384d2004-08-01 21:59:26 +0000349 BlockDriverState *bs1;
350 int64_t total_size;
aliguori7c96d462008-09-12 17:54:13 +0000351 int is_protocol = 0;
ths3b46e622007-09-17 08:09:54 +0000352
bellardea2384d2004-08-01 21:59:26 +0000353 /* if snapshot, we create a temporary backing file and open it
354 instead of opening 'filename' directly */
355
356 /* if there is a backing file, use it */
357 bs1 = bdrv_new("");
aliguori51d7c002009-03-05 23:00:29 +0000358 ret = bdrv_open(bs1, filename, 0);
359 if (ret < 0) {
bellardea2384d2004-08-01 21:59:26 +0000360 bdrv_delete(bs1);
aliguori51d7c002009-03-05 23:00:29 +0000361 return ret;
bellardea2384d2004-08-01 21:59:26 +0000362 }
bellard83f64092006-08-01 16:21:11 +0000363 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
aliguori7c96d462008-09-12 17:54:13 +0000364
365 if (bs1->drv && bs1->drv->protocol_name)
366 is_protocol = 1;
367
bellardea2384d2004-08-01 21:59:26 +0000368 bdrv_delete(bs1);
ths3b46e622007-09-17 08:09:54 +0000369
bellardea2384d2004-08-01 21:59:26 +0000370 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
aliguori7c96d462008-09-12 17:54:13 +0000371
372 /* Real path is meaningless for protocols */
373 if (is_protocol)
374 snprintf(backing_filename, sizeof(backing_filename),
375 "%s", filename);
376 else
377 realpath(filename, backing_filename);
378
aliguori51d7c002009-03-05 23:00:29 +0000379 ret = bdrv_create(&bdrv_qcow2, tmp_filename,
380 total_size, backing_filename, 0);
381 if (ret < 0) {
382 return ret;
bellardea2384d2004-08-01 21:59:26 +0000383 }
384 filename = tmp_filename;
385 bs->is_temporary = 1;
386 }
bellard712e7872005-04-28 21:09:32 +0000387
bellardea2384d2004-08-01 21:59:26 +0000388 pstrcpy(bs->filename, sizeof(bs->filename), filename);
bellard83f64092006-08-01 16:21:11 +0000389 if (flags & BDRV_O_FILE) {
390 drv = find_protocol(filename);
aliguori51d7c002009-03-05 23:00:29 +0000391 } else if (!drv) {
392 drv = find_image_format(filename);
393 }
394 if (!drv) {
395 ret = -ENOENT;
396 goto unlink_and_fail;
bellardea2384d2004-08-01 21:59:26 +0000397 }
398 bs->drv = drv;
399 bs->opaque = qemu_mallocz(drv->instance_size);
bellard83f64092006-08-01 16:21:11 +0000400 /* Note: for compatibility, we open disk image files as RDWR, and
401 RDONLY as fallback */
402 if (!(flags & BDRV_O_FILE))
aliguori9f7965c2008-10-14 14:42:54 +0000403 open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK);
bellard83f64092006-08-01 16:21:11 +0000404 else
405 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
406 ret = drv->bdrv_open(bs, filename, open_flags);
aurel32a0a83532008-10-13 21:08:34 +0000407 if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
aliguori9f7965c2008-10-14 14:42:54 +0000408 ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
bellard83f64092006-08-01 16:21:11 +0000409 bs->read_only = 1;
410 }
bellardea2384d2004-08-01 21:59:26 +0000411 if (ret < 0) {
412 qemu_free(bs->opaque);
bellard6b21b972006-08-23 21:14:37 +0000413 bs->opaque = NULL;
414 bs->drv = NULL;
aliguori51d7c002009-03-05 23:00:29 +0000415 unlink_and_fail:
416 if (bs->is_temporary)
417 unlink(filename);
bellard83f64092006-08-01 16:21:11 +0000418 return ret;
bellardea2384d2004-08-01 21:59:26 +0000419 }
bellardd15a7712006-08-06 13:35:09 +0000420 if (drv->bdrv_getlength) {
421 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
422 }
bellardea2384d2004-08-01 21:59:26 +0000423#ifndef _WIN32
424 if (bs->is_temporary) {
425 unlink(filename);
426 }
427#endif
bellard83f64092006-08-01 16:21:11 +0000428 if (bs->backing_file[0] != '\0') {
bellardea2384d2004-08-01 21:59:26 +0000429 /* if there is a backing file, use it */
430 bs->backing_hd = bdrv_new("");
bellard83f64092006-08-01 16:21:11 +0000431 path_combine(backing_filename, sizeof(backing_filename),
432 filename, bs->backing_file);
aliguori51d7c002009-03-05 23:00:29 +0000433 ret = bdrv_open(bs->backing_hd, backing_filename, open_flags);
434 if (ret < 0) {
435 bdrv_close(bs);
436 return ret;
437 }
bellardea2384d2004-08-01 21:59:26 +0000438 }
439
aliguoribb5fc202009-03-05 23:01:15 +0000440 if (!bdrv_key_required(bs)) {
441 /* call the change callback */
442 bs->media_changed = 1;
443 if (bs->change_cb)
444 bs->change_cb(bs->change_opaque);
445 }
bellardb3380822004-03-14 21:38:54 +0000446 return 0;
bellardfc01f7e2003-06-30 10:03:06 +0000447}
448
449void bdrv_close(BlockDriverState *bs)
450{
bellard19cb3732006-08-19 11:45:59 +0000451 if (bs->drv) {
bellardea2384d2004-08-01 21:59:26 +0000452 if (bs->backing_hd)
453 bdrv_delete(bs->backing_hd);
454 bs->drv->bdrv_close(bs);
455 qemu_free(bs->opaque);
456#ifdef _WIN32
457 if (bs->is_temporary) {
458 unlink(bs->filename);
459 }
bellard67b915a2004-03-31 23:37:16 +0000460#endif
bellardea2384d2004-08-01 21:59:26 +0000461 bs->opaque = NULL;
462 bs->drv = NULL;
bellardb3380822004-03-14 21:38:54 +0000463
464 /* call the change callback */
bellard19cb3732006-08-19 11:45:59 +0000465 bs->media_changed = 1;
bellardb3380822004-03-14 21:38:54 +0000466 if (bs->change_cb)
467 bs->change_cb(bs->change_opaque);
468 }
469}
470
471void bdrv_delete(BlockDriverState *bs)
472{
aurel3234c6f052008-04-08 19:51:21 +0000473 BlockDriverState **pbs;
474
475 pbs = &bdrv_first;
476 while (*pbs != bs && *pbs != NULL)
477 pbs = &(*pbs)->next;
478 if (*pbs == bs)
479 *pbs = bs->next;
480
bellardb3380822004-03-14 21:38:54 +0000481 bdrv_close(bs);
482 qemu_free(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000483}
484
bellard33e39632003-07-06 17:15:21 +0000485/* commit COW file into the raw image */
486int bdrv_commit(BlockDriverState *bs)
487{
bellard19cb3732006-08-19 11:45:59 +0000488 BlockDriver *drv = bs->drv;
bellard83f64092006-08-01 16:21:11 +0000489 int64_t i, total_sectors;
bellardea2384d2004-08-01 21:59:26 +0000490 int n, j;
491 unsigned char sector[512];
bellard33e39632003-07-06 17:15:21 +0000492
bellard19cb3732006-08-19 11:45:59 +0000493 if (!drv)
494 return -ENOMEDIUM;
bellard33e39632003-07-06 17:15:21 +0000495
496 if (bs->read_only) {
bellardea2384d2004-08-01 21:59:26 +0000497 return -EACCES;
bellard33e39632003-07-06 17:15:21 +0000498 }
499
bellardea2384d2004-08-01 21:59:26 +0000500 if (!bs->backing_hd) {
501 return -ENOTSUP;
bellard33e39632003-07-06 17:15:21 +0000502 }
bellardea2384d2004-08-01 21:59:26 +0000503
bellard83f64092006-08-01 16:21:11 +0000504 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
505 for (i = 0; i < total_sectors;) {
bellard19cb3732006-08-19 11:45:59 +0000506 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
bellardea2384d2004-08-01 21:59:26 +0000507 for(j = 0; j < n; j++) {
508 if (bdrv_read(bs, i, sector, 1) != 0) {
509 return -EIO;
510 }
511
512 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
513 return -EIO;
514 }
515 i++;
516 }
517 } else {
518 i += n;
519 }
520 }
bellard95389c82005-12-18 18:28:15 +0000521
bellard19cb3732006-08-19 11:45:59 +0000522 if (drv->bdrv_make_empty)
523 return drv->bdrv_make_empty(bs);
bellard95389c82005-12-18 18:28:15 +0000524
bellard33e39632003-07-06 17:15:21 +0000525 return 0;
526}
527
aliguori71d07702009-03-03 17:37:16 +0000528static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
529 size_t size)
530{
531 int64_t len;
532
533 if (!bdrv_is_inserted(bs))
534 return -ENOMEDIUM;
535
536 if (bs->growable)
537 return 0;
538
539 len = bdrv_getlength(bs);
540
541 if ((offset + size) > len)
542 return -EIO;
543
544 return 0;
545}
546
547static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
548 int nb_sectors)
549{
550 int64_t offset;
551
552 /* Deal with byte accesses */
553 if (sector_num < 0)
554 offset = -sector_num;
555 else
556 offset = sector_num * 512;
557
558 return bdrv_check_byte_request(bs, offset, nb_sectors * 512);
559}
560
bellard19cb3732006-08-19 11:45:59 +0000561/* return < 0 if error. See bdrv_write() for the return codes */
ths5fafdf22007-09-16 21:08:06 +0000562int bdrv_read(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000563 uint8_t *buf, int nb_sectors)
564{
bellardea2384d2004-08-01 21:59:26 +0000565 BlockDriver *drv = bs->drv;
566
bellard19cb3732006-08-19 11:45:59 +0000567 if (!drv)
568 return -ENOMEDIUM;
aliguori71d07702009-03-03 17:37:16 +0000569 if (bdrv_check_request(bs, sector_num, nb_sectors))
570 return -EIO;
bellardb3380822004-03-14 21:38:54 +0000571
aliguorieda578e2009-03-12 19:57:16 +0000572 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
bellardfc01f7e2003-06-30 10:03:06 +0000573}
574
ths5fafdf22007-09-16 21:08:06 +0000575/* Return < 0 if error. Important errors are:
bellard19cb3732006-08-19 11:45:59 +0000576 -EIO generic I/O error (may happen for all errors)
577 -ENOMEDIUM No media inserted.
578 -EINVAL Invalid sector number or nb_sectors
579 -EACCES Trying to write a read-only device
580*/
ths5fafdf22007-09-16 21:08:06 +0000581int bdrv_write(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000582 const uint8_t *buf, int nb_sectors)
583{
bellard83f64092006-08-01 16:21:11 +0000584 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +0000585 if (!bs->drv)
586 return -ENOMEDIUM;
bellard0849bf02003-06-30 23:17:31 +0000587 if (bs->read_only)
bellard19cb3732006-08-19 11:45:59 +0000588 return -EACCES;
aliguori71d07702009-03-03 17:37:16 +0000589 if (bdrv_check_request(bs, sector_num, nb_sectors))
590 return -EIO;
591
aliguori42fb2802009-01-15 20:43:39 +0000592 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
bellard83f64092006-08-01 16:21:11 +0000593}
594
aliguorieda578e2009-03-12 19:57:16 +0000595int bdrv_pread(BlockDriverState *bs, int64_t offset,
596 void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000597{
bellard83f64092006-08-01 16:21:11 +0000598 uint8_t tmp_buf[SECTOR_SIZE];
599 int len, nb_sectors, count;
600 int64_t sector_num;
601
602 count = count1;
603 /* first read to align to sector start */
604 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
605 if (len > count)
606 len = count;
607 sector_num = offset >> SECTOR_BITS;
608 if (len > 0) {
609 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
610 return -EIO;
611 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
612 count -= len;
613 if (count == 0)
614 return count1;
615 sector_num++;
616 buf += len;
617 }
618
619 /* read the sectors "in place" */
620 nb_sectors = count >> SECTOR_BITS;
621 if (nb_sectors > 0) {
622 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
623 return -EIO;
624 sector_num += nb_sectors;
625 len = nb_sectors << SECTOR_BITS;
626 buf += len;
627 count -= len;
628 }
629
630 /* add data from the last sector */
631 if (count > 0) {
632 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
633 return -EIO;
634 memcpy(buf, tmp_buf, count);
635 }
636 return count1;
637}
638
aliguorieda578e2009-03-12 19:57:16 +0000639int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
640 const void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000641{
bellard83f64092006-08-01 16:21:11 +0000642 uint8_t tmp_buf[SECTOR_SIZE];
643 int len, nb_sectors, count;
644 int64_t sector_num;
645
646 count = count1;
647 /* first write to align to sector start */
648 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
649 if (len > count)
650 len = count;
651 sector_num = offset >> SECTOR_BITS;
652 if (len > 0) {
653 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
654 return -EIO;
655 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
656 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
657 return -EIO;
658 count -= len;
659 if (count == 0)
660 return count1;
661 sector_num++;
662 buf += len;
663 }
664
665 /* write the sectors "in place" */
666 nb_sectors = count >> SECTOR_BITS;
667 if (nb_sectors > 0) {
668 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
669 return -EIO;
670 sector_num += nb_sectors;
671 len = nb_sectors << SECTOR_BITS;
672 buf += len;
673 count -= len;
674 }
675
676 /* add data from the last sector */
677 if (count > 0) {
678 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
679 return -EIO;
680 memcpy(tmp_buf, buf, count);
681 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
682 return -EIO;
683 }
684 return count1;
685}
bellard83f64092006-08-01 16:21:11 +0000686
687/**
bellard83f64092006-08-01 16:21:11 +0000688 * Truncate file to 'offset' bytes (needed only for file protocols)
689 */
690int bdrv_truncate(BlockDriverState *bs, int64_t offset)
691{
692 BlockDriver *drv = bs->drv;
693 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000694 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000695 if (!drv->bdrv_truncate)
696 return -ENOTSUP;
697 return drv->bdrv_truncate(bs, offset);
698}
699
700/**
701 * Length of a file in bytes. Return < 0 if error or unknown.
702 */
703int64_t bdrv_getlength(BlockDriverState *bs)
704{
705 BlockDriver *drv = bs->drv;
706 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000707 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000708 if (!drv->bdrv_getlength) {
709 /* legacy mode */
710 return bs->total_sectors * SECTOR_SIZE;
711 }
712 return drv->bdrv_getlength(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000713}
714
bellard19cb3732006-08-19 11:45:59 +0000715/* return 0 as number of sectors if no device present or error */
ths96b8f132007-12-17 01:35:20 +0000716void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
bellardfc01f7e2003-06-30 10:03:06 +0000717{
bellard19cb3732006-08-19 11:45:59 +0000718 int64_t length;
719 length = bdrv_getlength(bs);
720 if (length < 0)
721 length = 0;
722 else
723 length = length >> SECTOR_BITS;
724 *nb_sectors_ptr = length;
bellardfc01f7e2003-06-30 10:03:06 +0000725}
bellardcf989512004-02-16 21:56:36 +0000726
aliguorif3d54fc2008-11-25 21:50:24 +0000727struct partition {
728 uint8_t boot_ind; /* 0x80 - active */
729 uint8_t head; /* starting head */
730 uint8_t sector; /* starting sector */
731 uint8_t cyl; /* starting cylinder */
732 uint8_t sys_ind; /* What partition type */
733 uint8_t end_head; /* end head */
734 uint8_t end_sector; /* end sector */
735 uint8_t end_cyl; /* end cylinder */
736 uint32_t start_sect; /* starting sector counting from 0 */
737 uint32_t nr_sects; /* nr of sectors in partition */
738} __attribute__((packed));
739
740/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
741static int guess_disk_lchs(BlockDriverState *bs,
742 int *pcylinders, int *pheads, int *psectors)
743{
744 uint8_t buf[512];
745 int ret, i, heads, sectors, cylinders;
746 struct partition *p;
747 uint32_t nr_sects;
blueswir1a38131b2008-12-05 17:56:40 +0000748 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +0000749
750 bdrv_get_geometry(bs, &nb_sectors);
751
752 ret = bdrv_read(bs, 0, buf, 1);
753 if (ret < 0)
754 return -1;
755 /* test msdos magic */
756 if (buf[510] != 0x55 || buf[511] != 0xaa)
757 return -1;
758 for(i = 0; i < 4; i++) {
759 p = ((struct partition *)(buf + 0x1be)) + i;
760 nr_sects = le32_to_cpu(p->nr_sects);
761 if (nr_sects && p->end_head) {
762 /* We make the assumption that the partition terminates on
763 a cylinder boundary */
764 heads = p->end_head + 1;
765 sectors = p->end_sector & 63;
766 if (sectors == 0)
767 continue;
768 cylinders = nb_sectors / (heads * sectors);
769 if (cylinders < 1 || cylinders > 16383)
770 continue;
771 *pheads = heads;
772 *psectors = sectors;
773 *pcylinders = cylinders;
774#if 0
775 printf("guessed geometry: LCHS=%d %d %d\n",
776 cylinders, heads, sectors);
777#endif
778 return 0;
779 }
780 }
781 return -1;
782}
783
784void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
785{
786 int translation, lba_detected = 0;
787 int cylinders, heads, secs;
blueswir1a38131b2008-12-05 17:56:40 +0000788 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +0000789
790 /* if a geometry hint is available, use it */
791 bdrv_get_geometry(bs, &nb_sectors);
792 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
793 translation = bdrv_get_translation_hint(bs);
794 if (cylinders != 0) {
795 *pcyls = cylinders;
796 *pheads = heads;
797 *psecs = secs;
798 } else {
799 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
800 if (heads > 16) {
801 /* if heads > 16, it means that a BIOS LBA
802 translation was active, so the default
803 hardware geometry is OK */
804 lba_detected = 1;
805 goto default_geometry;
806 } else {
807 *pcyls = cylinders;
808 *pheads = heads;
809 *psecs = secs;
810 /* disable any translation to be in sync with
811 the logical geometry */
812 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
813 bdrv_set_translation_hint(bs,
814 BIOS_ATA_TRANSLATION_NONE);
815 }
816 }
817 } else {
818 default_geometry:
819 /* if no geometry, use a standard physical disk geometry */
820 cylinders = nb_sectors / (16 * 63);
821
822 if (cylinders > 16383)
823 cylinders = 16383;
824 else if (cylinders < 2)
825 cylinders = 2;
826 *pcyls = cylinders;
827 *pheads = 16;
828 *psecs = 63;
829 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
830 if ((*pcyls * *pheads) <= 131072) {
831 bdrv_set_translation_hint(bs,
832 BIOS_ATA_TRANSLATION_LARGE);
833 } else {
834 bdrv_set_translation_hint(bs,
835 BIOS_ATA_TRANSLATION_LBA);
836 }
837 }
838 }
839 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
840 }
841}
842
ths5fafdf22007-09-16 21:08:06 +0000843void bdrv_set_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000844 int cyls, int heads, int secs)
845{
846 bs->cyls = cyls;
847 bs->heads = heads;
848 bs->secs = secs;
849}
850
851void bdrv_set_type_hint(BlockDriverState *bs, int type)
852{
853 bs->type = type;
854 bs->removable = ((type == BDRV_TYPE_CDROM ||
855 type == BDRV_TYPE_FLOPPY));
856}
857
bellard46d47672004-11-16 01:45:27 +0000858void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
859{
860 bs->translation = translation;
861}
862
ths5fafdf22007-09-16 21:08:06 +0000863void bdrv_get_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000864 int *pcyls, int *pheads, int *psecs)
865{
866 *pcyls = bs->cyls;
867 *pheads = bs->heads;
868 *psecs = bs->secs;
869}
870
871int bdrv_get_type_hint(BlockDriverState *bs)
872{
873 return bs->type;
874}
875
bellard46d47672004-11-16 01:45:27 +0000876int bdrv_get_translation_hint(BlockDriverState *bs)
877{
878 return bs->translation;
879}
880
bellardb3380822004-03-14 21:38:54 +0000881int bdrv_is_removable(BlockDriverState *bs)
882{
883 return bs->removable;
884}
885
886int bdrv_is_read_only(BlockDriverState *bs)
887{
888 return bs->read_only;
889}
890
ths985a03b2007-12-24 16:10:43 +0000891int bdrv_is_sg(BlockDriverState *bs)
892{
893 return bs->sg;
894}
895
bellard19cb3732006-08-19 11:45:59 +0000896/* XXX: no longer used */
ths5fafdf22007-09-16 21:08:06 +0000897void bdrv_set_change_cb(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000898 void (*change_cb)(void *opaque), void *opaque)
899{
900 bs->change_cb = change_cb;
901 bs->change_opaque = opaque;
902}
903
bellardea2384d2004-08-01 21:59:26 +0000904int bdrv_is_encrypted(BlockDriverState *bs)
905{
906 if (bs->backing_hd && bs->backing_hd->encrypted)
907 return 1;
908 return bs->encrypted;
909}
910
aliguoric0f4ce72009-03-05 23:01:01 +0000911int bdrv_key_required(BlockDriverState *bs)
912{
913 BlockDriverState *backing_hd = bs->backing_hd;
914
915 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
916 return 1;
917 return (bs->encrypted && !bs->valid_key);
918}
919
bellardea2384d2004-08-01 21:59:26 +0000920int bdrv_set_key(BlockDriverState *bs, const char *key)
921{
922 int ret;
923 if (bs->backing_hd && bs->backing_hd->encrypted) {
924 ret = bdrv_set_key(bs->backing_hd, key);
925 if (ret < 0)
926 return ret;
927 if (!bs->encrypted)
928 return 0;
929 }
930 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
931 return -1;
aliguoric0f4ce72009-03-05 23:01:01 +0000932 ret = bs->drv->bdrv_set_key(bs, key);
aliguoribb5fc202009-03-05 23:01:15 +0000933 if (ret < 0) {
934 bs->valid_key = 0;
935 } else if (!bs->valid_key) {
936 bs->valid_key = 1;
937 /* call the change callback now, we skipped it on open */
938 bs->media_changed = 1;
939 if (bs->change_cb)
940 bs->change_cb(bs->change_opaque);
941 }
aliguoric0f4ce72009-03-05 23:01:01 +0000942 return ret;
bellardea2384d2004-08-01 21:59:26 +0000943}
944
945void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
946{
bellard19cb3732006-08-19 11:45:59 +0000947 if (!bs->drv) {
bellardea2384d2004-08-01 21:59:26 +0000948 buf[0] = '\0';
949 } else {
950 pstrcpy(buf, buf_size, bs->drv->format_name);
951 }
952}
953
ths5fafdf22007-09-16 21:08:06 +0000954void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
bellardea2384d2004-08-01 21:59:26 +0000955 void *opaque)
956{
957 BlockDriver *drv;
958
959 for (drv = first_drv; drv != NULL; drv = drv->next) {
960 it(opaque, drv->format_name);
961 }
962}
963
bellardb3380822004-03-14 21:38:54 +0000964BlockDriverState *bdrv_find(const char *name)
965{
966 BlockDriverState *bs;
967
968 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
969 if (!strcmp(name, bs->device_name))
970 return bs;
971 }
972 return NULL;
973}
974
aliguori51de9762009-03-05 23:00:43 +0000975void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
bellard81d09122004-07-14 17:21:37 +0000976{
977 BlockDriverState *bs;
978
979 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori51de9762009-03-05 23:00:43 +0000980 it(opaque, bs);
bellard81d09122004-07-14 17:21:37 +0000981 }
982}
983
bellardea2384d2004-08-01 21:59:26 +0000984const char *bdrv_get_device_name(BlockDriverState *bs)
985{
986 return bs->device_name;
987}
988
pbrook7a6cba62006-06-04 11:39:07 +0000989void bdrv_flush(BlockDriverState *bs)
990{
991 if (bs->drv->bdrv_flush)
992 bs->drv->bdrv_flush(bs);
993 if (bs->backing_hd)
994 bdrv_flush(bs->backing_hd);
995}
996
aliguoric6ca28d2008-10-06 13:55:43 +0000997void bdrv_flush_all(void)
998{
999 BlockDriverState *bs;
1000
1001 for (bs = bdrv_first; bs != NULL; bs = bs->next)
1002 if (bs->drv && !bdrv_is_read_only(bs) &&
1003 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1004 bdrv_flush(bs);
1005}
1006
thsf58c7b32008-06-05 21:53:49 +00001007/*
1008 * Returns true iff the specified sector is present in the disk image. Drivers
1009 * not implementing the functionality are assumed to not support backing files,
1010 * hence all their sectors are reported as allocated.
1011 *
1012 * 'pnum' is set to the number of sectors (including and immediately following
1013 * the specified sector) that are known to be in the same
1014 * allocated/unallocated state.
1015 *
1016 * 'nb_sectors' is the max value 'pnum' should be set to.
1017 */
1018int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1019 int *pnum)
1020{
1021 int64_t n;
1022 if (!bs->drv->bdrv_is_allocated) {
1023 if (sector_num >= bs->total_sectors) {
1024 *pnum = 0;
1025 return 0;
1026 }
1027 n = bs->total_sectors - sector_num;
1028 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1029 return 1;
1030 }
1031 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1032}
1033
aliguori376253e2009-03-05 23:01:23 +00001034void bdrv_info(Monitor *mon)
bellardb3380822004-03-14 21:38:54 +00001035{
1036 BlockDriverState *bs;
1037
1038 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori376253e2009-03-05 23:01:23 +00001039 monitor_printf(mon, "%s:", bs->device_name);
1040 monitor_printf(mon, " type=");
bellardb3380822004-03-14 21:38:54 +00001041 switch(bs->type) {
1042 case BDRV_TYPE_HD:
aliguori376253e2009-03-05 23:01:23 +00001043 monitor_printf(mon, "hd");
bellardb3380822004-03-14 21:38:54 +00001044 break;
1045 case BDRV_TYPE_CDROM:
aliguori376253e2009-03-05 23:01:23 +00001046 monitor_printf(mon, "cdrom");
bellardb3380822004-03-14 21:38:54 +00001047 break;
1048 case BDRV_TYPE_FLOPPY:
aliguori376253e2009-03-05 23:01:23 +00001049 monitor_printf(mon, "floppy");
bellardb3380822004-03-14 21:38:54 +00001050 break;
1051 }
aliguori376253e2009-03-05 23:01:23 +00001052 monitor_printf(mon, " removable=%d", bs->removable);
bellardb3380822004-03-14 21:38:54 +00001053 if (bs->removable) {
aliguori376253e2009-03-05 23:01:23 +00001054 monitor_printf(mon, " locked=%d", bs->locked);
bellardb3380822004-03-14 21:38:54 +00001055 }
bellard19cb3732006-08-19 11:45:59 +00001056 if (bs->drv) {
aliguori376253e2009-03-05 23:01:23 +00001057 monitor_printf(mon, " file=");
1058 monitor_print_filename(mon, bs->filename);
thsfef30742006-12-22 14:11:32 +00001059 if (bs->backing_file[0] != '\0') {
aliguori376253e2009-03-05 23:01:23 +00001060 monitor_printf(mon, " backing_file=");
1061 monitor_print_filename(mon, bs->backing_file);
1062 }
1063 monitor_printf(mon, " ro=%d", bs->read_only);
1064 monitor_printf(mon, " drv=%s", bs->drv->format_name);
1065 monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs));
bellardb3380822004-03-14 21:38:54 +00001066 } else {
aliguori376253e2009-03-05 23:01:23 +00001067 monitor_printf(mon, " [not inserted]");
bellardb3380822004-03-14 21:38:54 +00001068 }
aliguori376253e2009-03-05 23:01:23 +00001069 monitor_printf(mon, "\n");
bellardb3380822004-03-14 21:38:54 +00001070 }
1071}
thsa36e69d2007-12-02 05:18:19 +00001072
1073/* The "info blockstats" command. */
aliguori376253e2009-03-05 23:01:23 +00001074void bdrv_info_stats(Monitor *mon)
thsa36e69d2007-12-02 05:18:19 +00001075{
1076 BlockDriverState *bs;
1077
1078 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori376253e2009-03-05 23:01:23 +00001079 monitor_printf(mon, "%s:"
1080 " rd_bytes=%" PRIu64
1081 " wr_bytes=%" PRIu64
1082 " rd_operations=%" PRIu64
1083 " wr_operations=%" PRIu64
aliguoriebf53fc2009-03-11 20:05:29 +00001084 "\n",
aliguori376253e2009-03-05 23:01:23 +00001085 bs->device_name,
1086 bs->rd_bytes, bs->wr_bytes,
1087 bs->rd_ops, bs->wr_ops);
thsa36e69d2007-12-02 05:18:19 +00001088 }
1089}
bellardea2384d2004-08-01 21:59:26 +00001090
aliguori045df332009-03-05 23:00:48 +00001091const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1092{
1093 if (bs->backing_hd && bs->backing_hd->encrypted)
1094 return bs->backing_file;
1095 else if (bs->encrypted)
1096 return bs->filename;
1097 else
1098 return NULL;
1099}
1100
ths5fafdf22007-09-16 21:08:06 +00001101void bdrv_get_backing_filename(BlockDriverState *bs,
bellard83f64092006-08-01 16:21:11 +00001102 char *filename, int filename_size)
bellardea2384d2004-08-01 21:59:26 +00001103{
bellard83f64092006-08-01 16:21:11 +00001104 if (!bs->backing_hd) {
1105 pstrcpy(filename, filename_size, "");
1106 } else {
1107 pstrcpy(filename, filename_size, bs->backing_file);
1108 }
bellardea2384d2004-08-01 21:59:26 +00001109}
1110
ths5fafdf22007-09-16 21:08:06 +00001111int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
bellardfaea38e2006-08-05 21:31:00 +00001112 const uint8_t *buf, int nb_sectors)
1113{
1114 BlockDriver *drv = bs->drv;
1115 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001116 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001117 if (!drv->bdrv_write_compressed)
1118 return -ENOTSUP;
1119 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1120}
ths3b46e622007-09-17 08:09:54 +00001121
bellardfaea38e2006-08-05 21:31:00 +00001122int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1123{
1124 BlockDriver *drv = bs->drv;
1125 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001126 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001127 if (!drv->bdrv_get_info)
1128 return -ENOTSUP;
1129 memset(bdi, 0, sizeof(*bdi));
1130 return drv->bdrv_get_info(bs, bdi);
1131}
1132
1133/**************************************************************/
1134/* handling of snapshots */
1135
ths5fafdf22007-09-16 21:08:06 +00001136int bdrv_snapshot_create(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001137 QEMUSnapshotInfo *sn_info)
1138{
1139 BlockDriver *drv = bs->drv;
1140 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001141 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001142 if (!drv->bdrv_snapshot_create)
1143 return -ENOTSUP;
1144 return drv->bdrv_snapshot_create(bs, sn_info);
1145}
1146
ths5fafdf22007-09-16 21:08:06 +00001147int bdrv_snapshot_goto(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001148 const char *snapshot_id)
1149{
1150 BlockDriver *drv = bs->drv;
1151 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001152 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001153 if (!drv->bdrv_snapshot_goto)
1154 return -ENOTSUP;
1155 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1156}
1157
1158int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1159{
1160 BlockDriver *drv = bs->drv;
1161 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001162 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001163 if (!drv->bdrv_snapshot_delete)
1164 return -ENOTSUP;
1165 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1166}
1167
ths5fafdf22007-09-16 21:08:06 +00001168int bdrv_snapshot_list(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001169 QEMUSnapshotInfo **psn_info)
1170{
1171 BlockDriver *drv = bs->drv;
1172 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001173 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001174 if (!drv->bdrv_snapshot_list)
1175 return -ENOTSUP;
1176 return drv->bdrv_snapshot_list(bs, psn_info);
1177}
1178
1179#define NB_SUFFIXES 4
1180
1181char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1182{
1183 static const char suffixes[NB_SUFFIXES] = "KMGT";
1184 int64_t base;
1185 int i;
1186
1187 if (size <= 999) {
1188 snprintf(buf, buf_size, "%" PRId64, size);
1189 } else {
1190 base = 1024;
1191 for(i = 0; i < NB_SUFFIXES; i++) {
1192 if (size < (10 * base)) {
ths5fafdf22007-09-16 21:08:06 +00001193 snprintf(buf, buf_size, "%0.1f%c",
bellardfaea38e2006-08-05 21:31:00 +00001194 (double)size / base,
1195 suffixes[i]);
1196 break;
1197 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
ths5fafdf22007-09-16 21:08:06 +00001198 snprintf(buf, buf_size, "%" PRId64 "%c",
bellardfaea38e2006-08-05 21:31:00 +00001199 ((size + (base >> 1)) / base),
1200 suffixes[i]);
1201 break;
1202 }
1203 base = base * 1024;
1204 }
1205 }
1206 return buf;
1207}
1208
1209char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1210{
1211 char buf1[128], date_buf[128], clock_buf[128];
bellard3b9f94e2007-01-07 17:27:07 +00001212#ifdef _WIN32
1213 struct tm *ptm;
1214#else
bellardfaea38e2006-08-05 21:31:00 +00001215 struct tm tm;
bellard3b9f94e2007-01-07 17:27:07 +00001216#endif
bellardfaea38e2006-08-05 21:31:00 +00001217 time_t ti;
1218 int64_t secs;
1219
1220 if (!sn) {
ths5fafdf22007-09-16 21:08:06 +00001221 snprintf(buf, buf_size,
1222 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001223 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1224 } else {
1225 ti = sn->date_sec;
bellard3b9f94e2007-01-07 17:27:07 +00001226#ifdef _WIN32
1227 ptm = localtime(&ti);
1228 strftime(date_buf, sizeof(date_buf),
1229 "%Y-%m-%d %H:%M:%S", ptm);
1230#else
bellardfaea38e2006-08-05 21:31:00 +00001231 localtime_r(&ti, &tm);
1232 strftime(date_buf, sizeof(date_buf),
1233 "%Y-%m-%d %H:%M:%S", &tm);
bellard3b9f94e2007-01-07 17:27:07 +00001234#endif
bellardfaea38e2006-08-05 21:31:00 +00001235 secs = sn->vm_clock_nsec / 1000000000;
1236 snprintf(clock_buf, sizeof(clock_buf),
1237 "%02d:%02d:%02d.%03d",
1238 (int)(secs / 3600),
1239 (int)((secs / 60) % 60),
ths5fafdf22007-09-16 21:08:06 +00001240 (int)(secs % 60),
bellardfaea38e2006-08-05 21:31:00 +00001241 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1242 snprintf(buf, buf_size,
ths5fafdf22007-09-16 21:08:06 +00001243 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001244 sn->id_str, sn->name,
1245 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1246 date_buf,
1247 clock_buf);
1248 }
1249 return buf;
1250}
1251
bellardea2384d2004-08-01 21:59:26 +00001252
bellard83f64092006-08-01 16:21:11 +00001253/**************************************************************/
1254/* async I/Os */
1255
aliguori3b69e4b2009-01-22 16:59:24 +00001256typedef struct VectorTranslationState {
1257 QEMUIOVector *iov;
1258 uint8_t *bounce;
1259 int is_write;
1260 BlockDriverAIOCB *aiocb;
1261 BlockDriverAIOCB *this_aiocb;
1262} VectorTranslationState;
1263
1264static void bdrv_aio_rw_vector_cb(void *opaque, int ret)
1265{
1266 VectorTranslationState *s = opaque;
1267
1268 if (!s->is_write) {
aliguori249aa742009-01-26 17:17:52 +00001269 qemu_iovec_from_buffer(s->iov, s->bounce, s->iov->size);
aliguori3b69e4b2009-01-22 16:59:24 +00001270 }
aurel32d905dba2009-03-03 06:28:26 +00001271 qemu_vfree(s->bounce);
aliguori3b69e4b2009-01-22 16:59:24 +00001272 s->this_aiocb->cb(s->this_aiocb->opaque, ret);
1273 qemu_aio_release(s->this_aiocb);
1274}
1275
1276static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
1277 int64_t sector_num,
1278 QEMUIOVector *iov,
1279 int nb_sectors,
1280 BlockDriverCompletionFunc *cb,
1281 void *opaque,
1282 int is_write)
1283
1284{
1285 VectorTranslationState *s = qemu_mallocz(sizeof(*s));
1286 BlockDriverAIOCB *aiocb = qemu_aio_get(bs, cb, opaque);
1287
1288 s->this_aiocb = aiocb;
1289 s->iov = iov;
1290 s->bounce = qemu_memalign(512, nb_sectors * 512);
1291 s->is_write = is_write;
1292 if (is_write) {
1293 qemu_iovec_to_buffer(s->iov, s->bounce);
1294 s->aiocb = bdrv_aio_write(bs, sector_num, s->bounce, nb_sectors,
1295 bdrv_aio_rw_vector_cb, s);
1296 } else {
1297 s->aiocb = bdrv_aio_read(bs, sector_num, s->bounce, nb_sectors,
1298 bdrv_aio_rw_vector_cb, s);
1299 }
1300 return aiocb;
1301}
1302
1303BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
1304 QEMUIOVector *iov, int nb_sectors,
1305 BlockDriverCompletionFunc *cb, void *opaque)
1306{
aliguori71d07702009-03-03 17:37:16 +00001307 if (bdrv_check_request(bs, sector_num, nb_sectors))
1308 return NULL;
1309
aliguori3b69e4b2009-01-22 16:59:24 +00001310 return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
1311 cb, opaque, 0);
1312}
1313
1314BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1315 QEMUIOVector *iov, int nb_sectors,
1316 BlockDriverCompletionFunc *cb, void *opaque)
1317{
aliguori71d07702009-03-03 17:37:16 +00001318 if (bdrv_check_request(bs, sector_num, nb_sectors))
1319 return NULL;
1320
aliguori3b69e4b2009-01-22 16:59:24 +00001321 return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
1322 cb, opaque, 1);
1323}
1324
pbrookce1a14d2006-08-07 02:38:06 +00001325BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1326 uint8_t *buf, int nb_sectors,
1327 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001328{
1329 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001330 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001331
bellard19cb3732006-08-19 11:45:59 +00001332 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001333 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001334 if (bdrv_check_request(bs, sector_num, nb_sectors))
1335 return NULL;
ths3b46e622007-09-17 08:09:54 +00001336
thsa36e69d2007-12-02 05:18:19 +00001337 ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1338
1339 if (ret) {
1340 /* Update stats even though technically transfer has not happened. */
1341 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1342 bs->rd_ops ++;
1343 }
1344
1345 return ret;
bellard83f64092006-08-01 16:21:11 +00001346}
1347
pbrookce1a14d2006-08-07 02:38:06 +00001348BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1349 const uint8_t *buf, int nb_sectors,
1350 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001351{
bellard83f64092006-08-01 16:21:11 +00001352 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001353 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001354
bellard19cb3732006-08-19 11:45:59 +00001355 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001356 return NULL;
bellard83f64092006-08-01 16:21:11 +00001357 if (bs->read_only)
pbrookce1a14d2006-08-07 02:38:06 +00001358 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001359 if (bdrv_check_request(bs, sector_num, nb_sectors))
1360 return NULL;
bellard83f64092006-08-01 16:21:11 +00001361
thsa36e69d2007-12-02 05:18:19 +00001362 ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1363
1364 if (ret) {
1365 /* Update stats even though technically transfer has not happened. */
1366 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1367 bs->wr_ops ++;
1368 }
1369
1370 return ret;
bellard83f64092006-08-01 16:21:11 +00001371}
1372
1373void bdrv_aio_cancel(BlockDriverAIOCB *acb)
pbrookce1a14d2006-08-07 02:38:06 +00001374{
aliguori3b69e4b2009-01-22 16:59:24 +00001375 if (acb->cb == bdrv_aio_rw_vector_cb) {
1376 VectorTranslationState *s = acb->opaque;
1377 acb = s->aiocb;
1378 }
1379
aliguori6bbff9a2009-03-20 18:25:59 +00001380 acb->pool->cancel(acb);
bellard83f64092006-08-01 16:21:11 +00001381}
1382
pbrookce1a14d2006-08-07 02:38:06 +00001383
bellard83f64092006-08-01 16:21:11 +00001384/**************************************************************/
1385/* async block device emulation */
1386
bellard83f64092006-08-01 16:21:11 +00001387static void bdrv_aio_bh_cb(void *opaque)
bellardbeac80c2006-06-26 20:08:57 +00001388{
pbrookce1a14d2006-08-07 02:38:06 +00001389 BlockDriverAIOCBSync *acb = opaque;
1390 acb->common.cb(acb->common.opaque, acb->ret);
1391 qemu_aio_release(acb);
bellardbeac80c2006-06-26 20:08:57 +00001392}
bellardbeac80c2006-06-26 20:08:57 +00001393
pbrookce1a14d2006-08-07 02:38:06 +00001394static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1395 int64_t sector_num, uint8_t *buf, int nb_sectors,
1396 BlockDriverCompletionFunc *cb, void *opaque)
bellardea2384d2004-08-01 21:59:26 +00001397{
pbrookce1a14d2006-08-07 02:38:06 +00001398 BlockDriverAIOCBSync *acb;
bellard83f64092006-08-01 16:21:11 +00001399 int ret;
pbrookce1a14d2006-08-07 02:38:06 +00001400
1401 acb = qemu_aio_get(bs, cb, opaque);
1402 if (!acb->bh)
1403 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1404 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1405 acb->ret = ret;
1406 qemu_bh_schedule(acb->bh);
1407 return &acb->common;
pbrook7a6cba62006-06-04 11:39:07 +00001408}
1409
pbrookce1a14d2006-08-07 02:38:06 +00001410static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1411 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1412 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001413{
pbrookce1a14d2006-08-07 02:38:06 +00001414 BlockDriverAIOCBSync *acb;
bellard83f64092006-08-01 16:21:11 +00001415 int ret;
pbrookce1a14d2006-08-07 02:38:06 +00001416
1417 acb = qemu_aio_get(bs, cb, opaque);
1418 if (!acb->bh)
1419 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1420 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1421 acb->ret = ret;
1422 qemu_bh_schedule(acb->bh);
1423 return &acb->common;
bellard83f64092006-08-01 16:21:11 +00001424}
1425
pbrookce1a14d2006-08-07 02:38:06 +00001426static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
bellard83f64092006-08-01 16:21:11 +00001427{
pbrookce1a14d2006-08-07 02:38:06 +00001428 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1429 qemu_bh_cancel(acb->bh);
1430 qemu_aio_release(acb);
bellard83f64092006-08-01 16:21:11 +00001431}
bellard83f64092006-08-01 16:21:11 +00001432
1433/**************************************************************/
1434/* sync block device emulation */
1435
1436static void bdrv_rw_em_cb(void *opaque, int ret)
1437{
1438 *(int *)opaque = ret;
1439}
1440
1441#define NOT_DONE 0x7fffffff
1442
ths5fafdf22007-09-16 21:08:06 +00001443static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +00001444 uint8_t *buf, int nb_sectors)
1445{
pbrookce1a14d2006-08-07 02:38:06 +00001446 int async_ret;
1447 BlockDriverAIOCB *acb;
bellard83f64092006-08-01 16:21:11 +00001448
bellard83f64092006-08-01 16:21:11 +00001449 async_ret = NOT_DONE;
ths5fafdf22007-09-16 21:08:06 +00001450 acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
bellard83f64092006-08-01 16:21:11 +00001451 bdrv_rw_em_cb, &async_ret);
aliguoribaf35cb2008-09-10 15:45:19 +00001452 if (acb == NULL)
pbrookce1a14d2006-08-07 02:38:06 +00001453 return -1;
aliguoribaf35cb2008-09-10 15:45:19 +00001454
bellard83f64092006-08-01 16:21:11 +00001455 while (async_ret == NOT_DONE) {
1456 qemu_aio_wait();
1457 }
aliguoribaf35cb2008-09-10 15:45:19 +00001458
bellard83f64092006-08-01 16:21:11 +00001459 return async_ret;
1460}
1461
1462static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1463 const uint8_t *buf, int nb_sectors)
1464{
pbrookce1a14d2006-08-07 02:38:06 +00001465 int async_ret;
1466 BlockDriverAIOCB *acb;
bellard83f64092006-08-01 16:21:11 +00001467
bellard83f64092006-08-01 16:21:11 +00001468 async_ret = NOT_DONE;
ths5fafdf22007-09-16 21:08:06 +00001469 acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
bellard83f64092006-08-01 16:21:11 +00001470 bdrv_rw_em_cb, &async_ret);
aliguoribaf35cb2008-09-10 15:45:19 +00001471 if (acb == NULL)
pbrookce1a14d2006-08-07 02:38:06 +00001472 return -1;
bellard83f64092006-08-01 16:21:11 +00001473 while (async_ret == NOT_DONE) {
1474 qemu_aio_wait();
1475 }
bellard83f64092006-08-01 16:21:11 +00001476 return async_ret;
1477}
bellardea2384d2004-08-01 21:59:26 +00001478
1479void bdrv_init(void)
1480{
1481 bdrv_register(&bdrv_raw);
bellard19cb3732006-08-19 11:45:59 +00001482 bdrv_register(&bdrv_host_device);
bellardea2384d2004-08-01 21:59:26 +00001483#ifndef _WIN32
1484 bdrv_register(&bdrv_cow);
1485#endif
1486 bdrv_register(&bdrv_qcow);
1487 bdrv_register(&bdrv_vmdk);
bellard3c565212004-09-29 21:29:14 +00001488 bdrv_register(&bdrv_cloop);
bellard585d0ed2004-12-12 11:24:44 +00001489 bdrv_register(&bdrv_dmg);
bellarda8753c32005-04-26 21:34:00 +00001490 bdrv_register(&bdrv_bochs);
bellard6a0f9e82005-04-27 20:17:58 +00001491 bdrv_register(&bdrv_vpc);
bellard712e7872005-04-28 21:09:32 +00001492 bdrv_register(&bdrv_vvfat);
bellardfaea38e2006-08-05 21:31:00 +00001493 bdrv_register(&bdrv_qcow2);
ths6ada7452007-07-31 23:28:53 +00001494 bdrv_register(&bdrv_parallels);
ths75818252008-07-03 13:41:03 +00001495 bdrv_register(&bdrv_nbd);
bellardea2384d2004-08-01 21:59:26 +00001496}
pbrookce1a14d2006-08-07 02:38:06 +00001497
aliguori6bbff9a2009-03-20 18:25:59 +00001498void aio_pool_init(AIOPool *pool, int aiocb_size,
1499 void (*cancel)(BlockDriverAIOCB *acb))
pbrookce1a14d2006-08-07 02:38:06 +00001500{
aliguori6bbff9a2009-03-20 18:25:59 +00001501 pool->aiocb_size = aiocb_size;
1502 pool->cancel = cancel;
1503 pool->free_aiocb = NULL;
1504}
1505
1506void *qemu_aio_get_pool(AIOPool *pool, BlockDriverState *bs,
1507 BlockDriverCompletionFunc *cb, void *opaque)
1508{
pbrookce1a14d2006-08-07 02:38:06 +00001509 BlockDriverAIOCB *acb;
1510
aliguori6bbff9a2009-03-20 18:25:59 +00001511 if (pool->free_aiocb) {
1512 acb = pool->free_aiocb;
1513 pool->free_aiocb = acb->next;
pbrookce1a14d2006-08-07 02:38:06 +00001514 } else {
aliguori6bbff9a2009-03-20 18:25:59 +00001515 acb = qemu_mallocz(pool->aiocb_size);
1516 acb->pool = pool;
pbrookce1a14d2006-08-07 02:38:06 +00001517 }
1518 acb->bs = bs;
1519 acb->cb = cb;
1520 acb->opaque = opaque;
1521 return acb;
1522}
1523
aliguori6bbff9a2009-03-20 18:25:59 +00001524void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1525 void *opaque)
1526{
1527 return qemu_aio_get_pool(&bs->drv->aio_pool, bs, cb, opaque);
1528}
1529
pbrookce1a14d2006-08-07 02:38:06 +00001530void qemu_aio_release(void *p)
1531{
aliguori6bbff9a2009-03-20 18:25:59 +00001532 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
1533 AIOPool *pool = acb->pool;
1534 acb->next = pool->free_aiocb;
1535 pool->free_aiocb = acb;
pbrookce1a14d2006-08-07 02:38:06 +00001536}
bellard19cb3732006-08-19 11:45:59 +00001537
1538/**************************************************************/
1539/* removable device support */
1540
1541/**
1542 * Return TRUE if the media is present
1543 */
1544int bdrv_is_inserted(BlockDriverState *bs)
1545{
1546 BlockDriver *drv = bs->drv;
1547 int ret;
1548 if (!drv)
1549 return 0;
1550 if (!drv->bdrv_is_inserted)
1551 return 1;
1552 ret = drv->bdrv_is_inserted(bs);
1553 return ret;
1554}
1555
1556/**
1557 * Return TRUE if the media changed since the last call to this
ths5fafdf22007-09-16 21:08:06 +00001558 * function. It is currently only used for floppy disks
bellard19cb3732006-08-19 11:45:59 +00001559 */
1560int bdrv_media_changed(BlockDriverState *bs)
1561{
1562 BlockDriver *drv = bs->drv;
1563 int ret;
1564
1565 if (!drv || !drv->bdrv_media_changed)
1566 ret = -ENOTSUP;
1567 else
1568 ret = drv->bdrv_media_changed(bs);
1569 if (ret == -ENOTSUP)
1570 ret = bs->media_changed;
1571 bs->media_changed = 0;
1572 return ret;
1573}
1574
1575/**
1576 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1577 */
1578void bdrv_eject(BlockDriverState *bs, int eject_flag)
1579{
1580 BlockDriver *drv = bs->drv;
1581 int ret;
1582
1583 if (!drv || !drv->bdrv_eject) {
1584 ret = -ENOTSUP;
1585 } else {
1586 ret = drv->bdrv_eject(bs, eject_flag);
1587 }
1588 if (ret == -ENOTSUP) {
1589 if (eject_flag)
1590 bdrv_close(bs);
1591 }
1592}
1593
1594int bdrv_is_locked(BlockDriverState *bs)
1595{
1596 return bs->locked;
1597}
1598
1599/**
1600 * Lock or unlock the media (if it is locked, the user won't be able
1601 * to eject it manually).
1602 */
1603void bdrv_set_locked(BlockDriverState *bs, int locked)
1604{
1605 BlockDriver *drv = bs->drv;
1606
1607 bs->locked = locked;
1608 if (drv && drv->bdrv_set_locked) {
1609 drv->bdrv_set_locked(bs, locked);
1610 }
1611}
ths985a03b2007-12-24 16:10:43 +00001612
1613/* needed for generic scsi interface */
1614
1615int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1616{
1617 BlockDriver *drv = bs->drv;
1618
1619 if (drv && drv->bdrv_ioctl)
1620 return drv->bdrv_ioctl(bs, req, buf);
1621 return -ENOTSUP;
1622}
aliguori7d780662009-03-12 19:57:08 +00001623
1624int bdrv_sg_send_command(BlockDriverState *bs, void *buf, int count)
1625{
aliguori04eeb8b2009-03-12 19:57:12 +00001626 return bs->drv->bdrv_sg_send_command(bs, buf, count);
aliguori7d780662009-03-12 19:57:08 +00001627}
1628
1629int bdrv_sg_recv_response(BlockDriverState *bs, void *buf, int count)
1630{
aliguori04eeb8b2009-03-12 19:57:12 +00001631 return bs->drv->bdrv_sg_recv_response(bs, buf, count);
aliguori7d780662009-03-12 19:57:08 +00001632}
1633
1634BlockDriverAIOCB *bdrv_sg_aio_read(BlockDriverState *bs, void *buf, int count,
1635 BlockDriverCompletionFunc *cb, void *opaque)
1636{
aliguori04eeb8b2009-03-12 19:57:12 +00001637 return bs->drv->bdrv_sg_aio_read(bs, buf, count, cb, opaque);
aliguori7d780662009-03-12 19:57:08 +00001638}
1639
1640BlockDriverAIOCB *bdrv_sg_aio_write(BlockDriverState *bs, void *buf, int count,
1641 BlockDriverCompletionFunc *cb, void *opaque)
1642{
aliguori04eeb8b2009-03-12 19:57:12 +00001643 return bs->drv->bdrv_sg_aio_write(bs, buf, count, cb, opaque);
aliguori7d780662009-03-12 19:57:08 +00001644}