blob: 8348cf211e970f1af04f4d8c8ca0955c9615aac0 [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;
aliguorif141eaf2009-04-07 18:43:24 +000054 /* vector translation state */
55 QEMUIOVector *qiov;
56 uint8_t *bounce;
57 int is_write;
bellard90765422006-08-07 19:10:16 +000058} BlockDriverAIOCBSync;
59
aliguorif141eaf2009-04-07 18:43:24 +000060static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
61 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
aliguoric87c0672009-04-07 18:43:20 +000062 BlockDriverCompletionFunc *cb, void *opaque);
aliguorif141eaf2009-04-07 18:43:24 +000063static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
64 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
pbrookce1a14d2006-08-07 02:38:06 +000065 BlockDriverCompletionFunc *cb, void *opaque);
bellard83f64092006-08-01 16:21:11 +000066static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
ths5fafdf22007-09-16 21:08:06 +000067static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +000068 uint8_t *buf, int nb_sectors);
69static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
70 const uint8_t *buf, int nb_sectors);
bellardec530c82006-04-25 22:36:06 +000071
blueswir17ee930d2008-09-17 19:04:14 +000072BlockDriverState *bdrv_first;
73
bellardea2384d2004-08-01 21:59:26 +000074static BlockDriver *first_drv;
75
bellard83f64092006-08-01 16:21:11 +000076int path_is_absolute(const char *path)
77{
78 const char *p;
bellard21664422007-01-07 18:22:37 +000079#ifdef _WIN32
80 /* specific case for names like: "\\.\d:" */
81 if (*path == '/' || *path == '\\')
82 return 1;
83#endif
bellard83f64092006-08-01 16:21:11 +000084 p = strchr(path, ':');
85 if (p)
86 p++;
87 else
88 p = path;
bellard3b9f94e2007-01-07 17:27:07 +000089#ifdef _WIN32
90 return (*p == '/' || *p == '\\');
91#else
92 return (*p == '/');
93#endif
bellard83f64092006-08-01 16:21:11 +000094}
95
96/* if filename is absolute, just copy it to dest. Otherwise, build a
97 path to it by considering it is relative to base_path. URL are
98 supported. */
99void path_combine(char *dest, int dest_size,
100 const char *base_path,
101 const char *filename)
102{
103 const char *p, *p1;
104 int len;
105
106 if (dest_size <= 0)
107 return;
108 if (path_is_absolute(filename)) {
109 pstrcpy(dest, dest_size, filename);
110 } else {
111 p = strchr(base_path, ':');
112 if (p)
113 p++;
114 else
115 p = base_path;
bellard3b9f94e2007-01-07 17:27:07 +0000116 p1 = strrchr(base_path, '/');
117#ifdef _WIN32
118 {
119 const char *p2;
120 p2 = strrchr(base_path, '\\');
121 if (!p1 || p2 > p1)
122 p1 = p2;
123 }
124#endif
bellard83f64092006-08-01 16:21:11 +0000125 if (p1)
126 p1++;
127 else
128 p1 = base_path;
129 if (p1 > p)
130 p = p1;
131 len = p - base_path;
132 if (len > dest_size - 1)
133 len = dest_size - 1;
134 memcpy(dest, base_path, len);
135 dest[len] = '\0';
136 pstrcat(dest, dest_size, filename);
137 }
138}
139
140
pbrook9596ebb2007-11-18 01:44:38 +0000141static void bdrv_register(BlockDriver *bdrv)
bellardea2384d2004-08-01 21:59:26 +0000142{
aliguorif141eaf2009-04-07 18:43:24 +0000143 if (!bdrv->bdrv_aio_readv) {
bellard83f64092006-08-01 16:21:11 +0000144 /* add AIO emulation layer */
aliguorif141eaf2009-04-07 18:43:24 +0000145 bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
146 bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
bellard83f64092006-08-01 16:21:11 +0000147 bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
bellard90765422006-08-07 19:10:16 +0000148 bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
aliguorieda578e2009-03-12 19:57:16 +0000149 } else if (!bdrv->bdrv_read) {
bellard83f64092006-08-01 16:21:11 +0000150 /* add synchronous IO emulation layer */
151 bdrv->bdrv_read = bdrv_read_em;
152 bdrv->bdrv_write = bdrv_write_em;
153 }
aliguori6bbff9a2009-03-20 18:25:59 +0000154 aio_pool_init(&bdrv->aio_pool, bdrv->aiocb_size, bdrv->bdrv_aio_cancel);
bellardea2384d2004-08-01 21:59:26 +0000155 bdrv->next = first_drv;
156 first_drv = bdrv;
157}
bellardb3380822004-03-14 21:38:54 +0000158
159/* create a new block device (by default it is empty) */
160BlockDriverState *bdrv_new(const char *device_name)
bellardfc01f7e2003-06-30 10:03:06 +0000161{
bellardb3380822004-03-14 21:38:54 +0000162 BlockDriverState **pbs, *bs;
163
164 bs = qemu_mallocz(sizeof(BlockDriverState));
bellardb3380822004-03-14 21:38:54 +0000165 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
bellardea2384d2004-08-01 21:59:26 +0000166 if (device_name[0] != '\0') {
167 /* insert at the end */
168 pbs = &bdrv_first;
169 while (*pbs != NULL)
170 pbs = &(*pbs)->next;
171 *pbs = bs;
172 }
bellardb3380822004-03-14 21:38:54 +0000173 return bs;
174}
175
bellardea2384d2004-08-01 21:59:26 +0000176BlockDriver *bdrv_find_format(const char *format_name)
177{
178 BlockDriver *drv1;
179 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
180 if (!strcmp(drv1->format_name, format_name))
181 return drv1;
182 }
183 return NULL;
184}
185
aliguori5eb45632009-03-28 17:55:10 +0000186int bdrv_create2(BlockDriver *drv,
187 const char *filename, int64_t size_in_sectors,
188 const char *backing_file, const char *backing_format,
189 int flags)
190{
191 if (drv->bdrv_create2)
192 return drv->bdrv_create2(filename, size_in_sectors, backing_file,
193 backing_format, flags);
194 if (drv->bdrv_create)
195 return drv->bdrv_create(filename, size_in_sectors, backing_file,
196 flags);
197 return -ENOTSUP;
198}
199
ths5fafdf22007-09-16 21:08:06 +0000200int bdrv_create(BlockDriver *drv,
bellardea2384d2004-08-01 21:59:26 +0000201 const char *filename, int64_t size_in_sectors,
202 const char *backing_file, int flags)
203{
204 if (!drv->bdrv_create)
205 return -ENOTSUP;
206 return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
207}
208
bellardd5249392004-08-03 21:14:23 +0000209#ifdef _WIN32
bellard95389c82005-12-18 18:28:15 +0000210void get_tmp_filename(char *filename, int size)
bellardd5249392004-08-03 21:14:23 +0000211{
bellard3b9f94e2007-01-07 17:27:07 +0000212 char temp_dir[MAX_PATH];
ths3b46e622007-09-17 08:09:54 +0000213
bellard3b9f94e2007-01-07 17:27:07 +0000214 GetTempPath(MAX_PATH, temp_dir);
215 GetTempFileName(temp_dir, "qem", 0, filename);
bellardd5249392004-08-03 21:14:23 +0000216}
217#else
bellard95389c82005-12-18 18:28:15 +0000218void get_tmp_filename(char *filename, int size)
bellardea2384d2004-08-01 21:59:26 +0000219{
220 int fd;
blueswir17ccfb2e2008-09-14 06:45:34 +0000221 const char *tmpdir;
bellardd5249392004-08-03 21:14:23 +0000222 /* XXX: race condition possible */
aurel320badc1e2008-03-10 00:05:34 +0000223 tmpdir = getenv("TMPDIR");
224 if (!tmpdir)
225 tmpdir = "/tmp";
226 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
bellardea2384d2004-08-01 21:59:26 +0000227 fd = mkstemp(filename);
228 close(fd);
229}
bellardd5249392004-08-03 21:14:23 +0000230#endif
bellardea2384d2004-08-01 21:59:26 +0000231
bellard19cb3732006-08-19 11:45:59 +0000232#ifdef _WIN32
bellardf45512f2006-08-23 21:40:13 +0000233static int is_windows_drive_prefix(const char *filename)
234{
235 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
236 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
237 filename[1] == ':');
238}
ths3b46e622007-09-17 08:09:54 +0000239
bellard19cb3732006-08-19 11:45:59 +0000240static int is_windows_drive(const char *filename)
241{
ths5fafdf22007-09-16 21:08:06 +0000242 if (is_windows_drive_prefix(filename) &&
bellardf45512f2006-08-23 21:40:13 +0000243 filename[2] == '\0')
bellard19cb3732006-08-19 11:45:59 +0000244 return 1;
245 if (strstart(filename, "\\\\.\\", NULL) ||
246 strstart(filename, "//./", NULL))
247 return 1;
248 return 0;
249}
250#endif
251
bellard83f64092006-08-01 16:21:11 +0000252static BlockDriver *find_protocol(const char *filename)
253{
254 BlockDriver *drv1;
255 char protocol[128];
256 int len;
257 const char *p;
bellard19cb3732006-08-19 11:45:59 +0000258
259#ifdef _WIN32
bellardf45512f2006-08-23 21:40:13 +0000260 if (is_windows_drive(filename) ||
261 is_windows_drive_prefix(filename))
bellard19cb3732006-08-19 11:45:59 +0000262 return &bdrv_raw;
263#endif
bellard83f64092006-08-01 16:21:11 +0000264 p = strchr(filename, ':');
265 if (!p)
266 return &bdrv_raw;
267 len = p - filename;
268 if (len > sizeof(protocol) - 1)
269 len = sizeof(protocol) - 1;
bellard83f64092006-08-01 16:21:11 +0000270 memcpy(protocol, filename, len);
271 protocol[len] = '\0';
272 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
ths5fafdf22007-09-16 21:08:06 +0000273 if (drv1->protocol_name &&
bellard83f64092006-08-01 16:21:11 +0000274 !strcmp(drv1->protocol_name, protocol))
275 return drv1;
276 }
277 return NULL;
278}
279
bellard7674e7b2005-04-26 21:59:26 +0000280/* XXX: force raw format if block or character device ? It would
281 simplify the BSD case */
bellardea2384d2004-08-01 21:59:26 +0000282static BlockDriver *find_image_format(const char *filename)
283{
bellard83f64092006-08-01 16:21:11 +0000284 int ret, score, score_max;
bellardea2384d2004-08-01 21:59:26 +0000285 BlockDriver *drv1, *drv;
bellard83f64092006-08-01 16:21:11 +0000286 uint8_t buf[2048];
287 BlockDriverState *bs;
ths3b46e622007-09-17 08:09:54 +0000288
bellard19cb3732006-08-19 11:45:59 +0000289 /* detect host devices. By convention, /dev/cdrom[N] is always
290 recognized as a host CDROM */
291 if (strstart(filename, "/dev/cdrom", NULL))
292 return &bdrv_host_device;
293#ifdef _WIN32
294 if (is_windows_drive(filename))
295 return &bdrv_host_device;
296#else
297 {
298 struct stat st;
ths5fafdf22007-09-16 21:08:06 +0000299 if (stat(filename, &st) >= 0 &&
bellard19cb3732006-08-19 11:45:59 +0000300 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
301 return &bdrv_host_device;
302 }
303 }
304#endif
ths3b46e622007-09-17 08:09:54 +0000305
bellard83f64092006-08-01 16:21:11 +0000306 drv = find_protocol(filename);
bellard19cb3732006-08-19 11:45:59 +0000307 /* no need to test disk image formats for vvfat */
bellard83f64092006-08-01 16:21:11 +0000308 if (drv == &bdrv_vvfat)
309 return drv;
bellard19cb3732006-08-19 11:45:59 +0000310
bellard83f64092006-08-01 16:21:11 +0000311 ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
312 if (ret < 0)
313 return NULL;
314 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
315 bdrv_delete(bs);
316 if (ret < 0) {
317 return NULL;
318 }
319
bellardea2384d2004-08-01 21:59:26 +0000320 score_max = 0;
321 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
bellard83f64092006-08-01 16:21:11 +0000322 if (drv1->bdrv_probe) {
323 score = drv1->bdrv_probe(buf, ret, filename);
324 if (score > score_max) {
325 score_max = score;
326 drv = drv1;
327 }
bellardea2384d2004-08-01 21:59:26 +0000328 }
329 }
330 return drv;
331}
332
bellard83f64092006-08-01 16:21:11 +0000333int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
bellardb3380822004-03-14 21:38:54 +0000334{
bellard83f64092006-08-01 16:21:11 +0000335 BlockDriverState *bs;
336 int ret;
337
338 bs = bdrv_new("");
bellard83f64092006-08-01 16:21:11 +0000339 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
340 if (ret < 0) {
341 bdrv_delete(bs);
342 return ret;
bellard3b0d4f62005-10-30 18:30:10 +0000343 }
aliguori71d07702009-03-03 17:37:16 +0000344 bs->growable = 1;
bellard83f64092006-08-01 16:21:11 +0000345 *pbs = bs;
346 return 0;
bellardea2384d2004-08-01 21:59:26 +0000347}
bellardfc01f7e2003-06-30 10:03:06 +0000348
bellard83f64092006-08-01 16:21:11 +0000349int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
350{
351 return bdrv_open2(bs, filename, flags, NULL);
352}
353
354int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
bellardea2384d2004-08-01 21:59:26 +0000355 BlockDriver *drv)
356{
bellard83f64092006-08-01 16:21:11 +0000357 int ret, open_flags;
thseb5c8512007-02-11 15:06:09 +0000358 char tmp_filename[PATH_MAX];
359 char backing_filename[PATH_MAX];
ths3b46e622007-09-17 08:09:54 +0000360
bellardea2384d2004-08-01 21:59:26 +0000361 bs->read_only = 0;
362 bs->is_temporary = 0;
363 bs->encrypted = 0;
aliguoric0f4ce72009-03-05 23:01:01 +0000364 bs->valid_key = 0;
bellard712e7872005-04-28 21:09:32 +0000365
bellard83f64092006-08-01 16:21:11 +0000366 if (flags & BDRV_O_SNAPSHOT) {
bellardea2384d2004-08-01 21:59:26 +0000367 BlockDriverState *bs1;
368 int64_t total_size;
aliguori7c96d462008-09-12 17:54:13 +0000369 int is_protocol = 0;
ths3b46e622007-09-17 08:09:54 +0000370
bellardea2384d2004-08-01 21:59:26 +0000371 /* if snapshot, we create a temporary backing file and open it
372 instead of opening 'filename' directly */
373
374 /* if there is a backing file, use it */
375 bs1 = bdrv_new("");
aliguori5eb45632009-03-28 17:55:10 +0000376 ret = bdrv_open2(bs1, filename, 0, drv);
aliguori51d7c002009-03-05 23:00:29 +0000377 if (ret < 0) {
bellardea2384d2004-08-01 21:59:26 +0000378 bdrv_delete(bs1);
aliguori51d7c002009-03-05 23:00:29 +0000379 return ret;
bellardea2384d2004-08-01 21:59:26 +0000380 }
bellard83f64092006-08-01 16:21:11 +0000381 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
aliguori7c96d462008-09-12 17:54:13 +0000382
383 if (bs1->drv && bs1->drv->protocol_name)
384 is_protocol = 1;
385
bellardea2384d2004-08-01 21:59:26 +0000386 bdrv_delete(bs1);
ths3b46e622007-09-17 08:09:54 +0000387
bellardea2384d2004-08-01 21:59:26 +0000388 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
aliguori7c96d462008-09-12 17:54:13 +0000389
390 /* Real path is meaningless for protocols */
391 if (is_protocol)
392 snprintf(backing_filename, sizeof(backing_filename),
393 "%s", filename);
394 else
395 realpath(filename, backing_filename);
396
aliguori5eb45632009-03-28 17:55:10 +0000397 ret = bdrv_create2(&bdrv_qcow2, tmp_filename,
398 total_size, backing_filename,
399 (drv ? drv->format_name : NULL), 0);
aliguori51d7c002009-03-05 23:00:29 +0000400 if (ret < 0) {
401 return ret;
bellardea2384d2004-08-01 21:59:26 +0000402 }
403 filename = tmp_filename;
aliguori5eb45632009-03-28 17:55:10 +0000404 drv = &bdrv_qcow2;
bellardea2384d2004-08-01 21:59:26 +0000405 bs->is_temporary = 1;
406 }
bellard712e7872005-04-28 21:09:32 +0000407
bellardea2384d2004-08-01 21:59:26 +0000408 pstrcpy(bs->filename, sizeof(bs->filename), filename);
bellard83f64092006-08-01 16:21:11 +0000409 if (flags & BDRV_O_FILE) {
410 drv = find_protocol(filename);
aliguori51d7c002009-03-05 23:00:29 +0000411 } else if (!drv) {
412 drv = find_image_format(filename);
413 }
414 if (!drv) {
415 ret = -ENOENT;
416 goto unlink_and_fail;
bellardea2384d2004-08-01 21:59:26 +0000417 }
418 bs->drv = drv;
419 bs->opaque = qemu_mallocz(drv->instance_size);
bellard83f64092006-08-01 16:21:11 +0000420 /* Note: for compatibility, we open disk image files as RDWR, and
421 RDONLY as fallback */
422 if (!(flags & BDRV_O_FILE))
aliguori9f7965c2008-10-14 14:42:54 +0000423 open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK);
bellard83f64092006-08-01 16:21:11 +0000424 else
425 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
426 ret = drv->bdrv_open(bs, filename, open_flags);
aurel32a0a83532008-10-13 21:08:34 +0000427 if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
aliguori9f7965c2008-10-14 14:42:54 +0000428 ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
bellard83f64092006-08-01 16:21:11 +0000429 bs->read_only = 1;
430 }
bellardea2384d2004-08-01 21:59:26 +0000431 if (ret < 0) {
432 qemu_free(bs->opaque);
bellard6b21b972006-08-23 21:14:37 +0000433 bs->opaque = NULL;
434 bs->drv = NULL;
aliguori51d7c002009-03-05 23:00:29 +0000435 unlink_and_fail:
436 if (bs->is_temporary)
437 unlink(filename);
bellard83f64092006-08-01 16:21:11 +0000438 return ret;
bellardea2384d2004-08-01 21:59:26 +0000439 }
bellardd15a7712006-08-06 13:35:09 +0000440 if (drv->bdrv_getlength) {
441 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
442 }
bellardea2384d2004-08-01 21:59:26 +0000443#ifndef _WIN32
444 if (bs->is_temporary) {
445 unlink(filename);
446 }
447#endif
bellard83f64092006-08-01 16:21:11 +0000448 if (bs->backing_file[0] != '\0') {
bellardea2384d2004-08-01 21:59:26 +0000449 /* if there is a backing file, use it */
aliguori5eb45632009-03-28 17:55:10 +0000450 BlockDriver *back_drv = NULL;
bellardea2384d2004-08-01 21:59:26 +0000451 bs->backing_hd = bdrv_new("");
bellard83f64092006-08-01 16:21:11 +0000452 path_combine(backing_filename, sizeof(backing_filename),
453 filename, bs->backing_file);
aliguori5eb45632009-03-28 17:55:10 +0000454 if (bs->backing_format[0] != '\0')
455 back_drv = bdrv_find_format(bs->backing_format);
456 ret = bdrv_open2(bs->backing_hd, backing_filename, open_flags,
457 back_drv);
aliguori51d7c002009-03-05 23:00:29 +0000458 if (ret < 0) {
459 bdrv_close(bs);
460 return ret;
461 }
bellardea2384d2004-08-01 21:59:26 +0000462 }
463
aliguoribb5fc202009-03-05 23:01:15 +0000464 if (!bdrv_key_required(bs)) {
465 /* call the change callback */
466 bs->media_changed = 1;
467 if (bs->change_cb)
468 bs->change_cb(bs->change_opaque);
469 }
bellardb3380822004-03-14 21:38:54 +0000470 return 0;
bellardfc01f7e2003-06-30 10:03:06 +0000471}
472
473void bdrv_close(BlockDriverState *bs)
474{
bellard19cb3732006-08-19 11:45:59 +0000475 if (bs->drv) {
bellardea2384d2004-08-01 21:59:26 +0000476 if (bs->backing_hd)
477 bdrv_delete(bs->backing_hd);
478 bs->drv->bdrv_close(bs);
479 qemu_free(bs->opaque);
480#ifdef _WIN32
481 if (bs->is_temporary) {
482 unlink(bs->filename);
483 }
bellard67b915a2004-03-31 23:37:16 +0000484#endif
bellardea2384d2004-08-01 21:59:26 +0000485 bs->opaque = NULL;
486 bs->drv = NULL;
bellardb3380822004-03-14 21:38:54 +0000487
488 /* call the change callback */
bellard19cb3732006-08-19 11:45:59 +0000489 bs->media_changed = 1;
bellardb3380822004-03-14 21:38:54 +0000490 if (bs->change_cb)
491 bs->change_cb(bs->change_opaque);
492 }
493}
494
495void bdrv_delete(BlockDriverState *bs)
496{
aurel3234c6f052008-04-08 19:51:21 +0000497 BlockDriverState **pbs;
498
499 pbs = &bdrv_first;
500 while (*pbs != bs && *pbs != NULL)
501 pbs = &(*pbs)->next;
502 if (*pbs == bs)
503 *pbs = bs->next;
504
bellardb3380822004-03-14 21:38:54 +0000505 bdrv_close(bs);
506 qemu_free(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000507}
508
aliguorie97fc192009-04-21 23:11:50 +0000509/*
510 * Run consistency checks on an image
511 *
512 * Returns the number of errors or -errno when an internal error occurs
513 */
514int bdrv_check(BlockDriverState *bs)
515{
516 if (bs->drv->bdrv_check == NULL) {
517 return -ENOTSUP;
518 }
519
520 return bs->drv->bdrv_check(bs);
521}
522
bellard33e39632003-07-06 17:15:21 +0000523/* commit COW file into the raw image */
524int bdrv_commit(BlockDriverState *bs)
525{
bellard19cb3732006-08-19 11:45:59 +0000526 BlockDriver *drv = bs->drv;
bellard83f64092006-08-01 16:21:11 +0000527 int64_t i, total_sectors;
bellardea2384d2004-08-01 21:59:26 +0000528 int n, j;
529 unsigned char sector[512];
bellard33e39632003-07-06 17:15:21 +0000530
bellard19cb3732006-08-19 11:45:59 +0000531 if (!drv)
532 return -ENOMEDIUM;
bellard33e39632003-07-06 17:15:21 +0000533
534 if (bs->read_only) {
bellardea2384d2004-08-01 21:59:26 +0000535 return -EACCES;
bellard33e39632003-07-06 17:15:21 +0000536 }
537
bellardea2384d2004-08-01 21:59:26 +0000538 if (!bs->backing_hd) {
539 return -ENOTSUP;
bellard33e39632003-07-06 17:15:21 +0000540 }
bellardea2384d2004-08-01 21:59:26 +0000541
bellard83f64092006-08-01 16:21:11 +0000542 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
543 for (i = 0; i < total_sectors;) {
bellard19cb3732006-08-19 11:45:59 +0000544 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
bellardea2384d2004-08-01 21:59:26 +0000545 for(j = 0; j < n; j++) {
546 if (bdrv_read(bs, i, sector, 1) != 0) {
547 return -EIO;
548 }
549
550 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
551 return -EIO;
552 }
553 i++;
554 }
555 } else {
556 i += n;
557 }
558 }
bellard95389c82005-12-18 18:28:15 +0000559
bellard19cb3732006-08-19 11:45:59 +0000560 if (drv->bdrv_make_empty)
561 return drv->bdrv_make_empty(bs);
bellard95389c82005-12-18 18:28:15 +0000562
bellard33e39632003-07-06 17:15:21 +0000563 return 0;
564}
565
aliguori71d07702009-03-03 17:37:16 +0000566static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
567 size_t size)
568{
569 int64_t len;
570
571 if (!bdrv_is_inserted(bs))
572 return -ENOMEDIUM;
573
574 if (bs->growable)
575 return 0;
576
577 len = bdrv_getlength(bs);
578
579 if ((offset + size) > len)
580 return -EIO;
581
582 return 0;
583}
584
585static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
586 int nb_sectors)
587{
aliguori999dec52009-03-29 01:31:48 +0000588 return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512);
aliguori71d07702009-03-03 17:37:16 +0000589}
590
bellard19cb3732006-08-19 11:45:59 +0000591/* return < 0 if error. See bdrv_write() for the return codes */
ths5fafdf22007-09-16 21:08:06 +0000592int bdrv_read(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000593 uint8_t *buf, int nb_sectors)
594{
bellardea2384d2004-08-01 21:59:26 +0000595 BlockDriver *drv = bs->drv;
596
bellard19cb3732006-08-19 11:45:59 +0000597 if (!drv)
598 return -ENOMEDIUM;
aliguori71d07702009-03-03 17:37:16 +0000599 if (bdrv_check_request(bs, sector_num, nb_sectors))
600 return -EIO;
bellardb3380822004-03-14 21:38:54 +0000601
aliguorieda578e2009-03-12 19:57:16 +0000602 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
bellardfc01f7e2003-06-30 10:03:06 +0000603}
604
ths5fafdf22007-09-16 21:08:06 +0000605/* Return < 0 if error. Important errors are:
bellard19cb3732006-08-19 11:45:59 +0000606 -EIO generic I/O error (may happen for all errors)
607 -ENOMEDIUM No media inserted.
608 -EINVAL Invalid sector number or nb_sectors
609 -EACCES Trying to write a read-only device
610*/
ths5fafdf22007-09-16 21:08:06 +0000611int bdrv_write(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000612 const uint8_t *buf, int nb_sectors)
613{
bellard83f64092006-08-01 16:21:11 +0000614 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +0000615 if (!bs->drv)
616 return -ENOMEDIUM;
bellard0849bf02003-06-30 23:17:31 +0000617 if (bs->read_only)
bellard19cb3732006-08-19 11:45:59 +0000618 return -EACCES;
aliguori71d07702009-03-03 17:37:16 +0000619 if (bdrv_check_request(bs, sector_num, nb_sectors))
620 return -EIO;
621
aliguori42fb2802009-01-15 20:43:39 +0000622 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
bellard83f64092006-08-01 16:21:11 +0000623}
624
aliguorieda578e2009-03-12 19:57:16 +0000625int bdrv_pread(BlockDriverState *bs, int64_t offset,
626 void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000627{
bellard83f64092006-08-01 16:21:11 +0000628 uint8_t tmp_buf[SECTOR_SIZE];
629 int len, nb_sectors, count;
630 int64_t sector_num;
631
632 count = count1;
633 /* first read to align to sector start */
634 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
635 if (len > count)
636 len = count;
637 sector_num = offset >> SECTOR_BITS;
638 if (len > 0) {
639 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
640 return -EIO;
641 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
642 count -= len;
643 if (count == 0)
644 return count1;
645 sector_num++;
646 buf += len;
647 }
648
649 /* read the sectors "in place" */
650 nb_sectors = count >> SECTOR_BITS;
651 if (nb_sectors > 0) {
652 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
653 return -EIO;
654 sector_num += nb_sectors;
655 len = nb_sectors << SECTOR_BITS;
656 buf += len;
657 count -= len;
658 }
659
660 /* add data from the last sector */
661 if (count > 0) {
662 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
663 return -EIO;
664 memcpy(buf, tmp_buf, count);
665 }
666 return count1;
667}
668
aliguorieda578e2009-03-12 19:57:16 +0000669int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
670 const void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000671{
bellard83f64092006-08-01 16:21:11 +0000672 uint8_t tmp_buf[SECTOR_SIZE];
673 int len, nb_sectors, count;
674 int64_t sector_num;
675
676 count = count1;
677 /* first write to align to sector start */
678 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
679 if (len > count)
680 len = count;
681 sector_num = offset >> SECTOR_BITS;
682 if (len > 0) {
683 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
684 return -EIO;
685 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
686 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
687 return -EIO;
688 count -= len;
689 if (count == 0)
690 return count1;
691 sector_num++;
692 buf += len;
693 }
694
695 /* write the sectors "in place" */
696 nb_sectors = count >> SECTOR_BITS;
697 if (nb_sectors > 0) {
698 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
699 return -EIO;
700 sector_num += nb_sectors;
701 len = nb_sectors << SECTOR_BITS;
702 buf += len;
703 count -= len;
704 }
705
706 /* add data from the last sector */
707 if (count > 0) {
708 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
709 return -EIO;
710 memcpy(tmp_buf, buf, count);
711 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
712 return -EIO;
713 }
714 return count1;
715}
bellard83f64092006-08-01 16:21:11 +0000716
717/**
bellard83f64092006-08-01 16:21:11 +0000718 * Truncate file to 'offset' bytes (needed only for file protocols)
719 */
720int bdrv_truncate(BlockDriverState *bs, int64_t offset)
721{
722 BlockDriver *drv = bs->drv;
723 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000724 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000725 if (!drv->bdrv_truncate)
726 return -ENOTSUP;
727 return drv->bdrv_truncate(bs, offset);
728}
729
730/**
731 * Length of a file in bytes. Return < 0 if error or unknown.
732 */
733int64_t bdrv_getlength(BlockDriverState *bs)
734{
735 BlockDriver *drv = bs->drv;
736 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000737 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000738 if (!drv->bdrv_getlength) {
739 /* legacy mode */
740 return bs->total_sectors * SECTOR_SIZE;
741 }
742 return drv->bdrv_getlength(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000743}
744
bellard19cb3732006-08-19 11:45:59 +0000745/* return 0 as number of sectors if no device present or error */
ths96b8f132007-12-17 01:35:20 +0000746void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
bellardfc01f7e2003-06-30 10:03:06 +0000747{
bellard19cb3732006-08-19 11:45:59 +0000748 int64_t length;
749 length = bdrv_getlength(bs);
750 if (length < 0)
751 length = 0;
752 else
753 length = length >> SECTOR_BITS;
754 *nb_sectors_ptr = length;
bellardfc01f7e2003-06-30 10:03:06 +0000755}
bellardcf989512004-02-16 21:56:36 +0000756
aliguorif3d54fc2008-11-25 21:50:24 +0000757struct partition {
758 uint8_t boot_ind; /* 0x80 - active */
759 uint8_t head; /* starting head */
760 uint8_t sector; /* starting sector */
761 uint8_t cyl; /* starting cylinder */
762 uint8_t sys_ind; /* What partition type */
763 uint8_t end_head; /* end head */
764 uint8_t end_sector; /* end sector */
765 uint8_t end_cyl; /* end cylinder */
766 uint32_t start_sect; /* starting sector counting from 0 */
767 uint32_t nr_sects; /* nr of sectors in partition */
768} __attribute__((packed));
769
770/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
771static int guess_disk_lchs(BlockDriverState *bs,
772 int *pcylinders, int *pheads, int *psectors)
773{
774 uint8_t buf[512];
775 int ret, i, heads, sectors, cylinders;
776 struct partition *p;
777 uint32_t nr_sects;
blueswir1a38131b2008-12-05 17:56:40 +0000778 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +0000779
780 bdrv_get_geometry(bs, &nb_sectors);
781
782 ret = bdrv_read(bs, 0, buf, 1);
783 if (ret < 0)
784 return -1;
785 /* test msdos magic */
786 if (buf[510] != 0x55 || buf[511] != 0xaa)
787 return -1;
788 for(i = 0; i < 4; i++) {
789 p = ((struct partition *)(buf + 0x1be)) + i;
790 nr_sects = le32_to_cpu(p->nr_sects);
791 if (nr_sects && p->end_head) {
792 /* We make the assumption that the partition terminates on
793 a cylinder boundary */
794 heads = p->end_head + 1;
795 sectors = p->end_sector & 63;
796 if (sectors == 0)
797 continue;
798 cylinders = nb_sectors / (heads * sectors);
799 if (cylinders < 1 || cylinders > 16383)
800 continue;
801 *pheads = heads;
802 *psectors = sectors;
803 *pcylinders = cylinders;
804#if 0
805 printf("guessed geometry: LCHS=%d %d %d\n",
806 cylinders, heads, sectors);
807#endif
808 return 0;
809 }
810 }
811 return -1;
812}
813
814void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
815{
816 int translation, lba_detected = 0;
817 int cylinders, heads, secs;
blueswir1a38131b2008-12-05 17:56:40 +0000818 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +0000819
820 /* if a geometry hint is available, use it */
821 bdrv_get_geometry(bs, &nb_sectors);
822 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
823 translation = bdrv_get_translation_hint(bs);
824 if (cylinders != 0) {
825 *pcyls = cylinders;
826 *pheads = heads;
827 *psecs = secs;
828 } else {
829 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
830 if (heads > 16) {
831 /* if heads > 16, it means that a BIOS LBA
832 translation was active, so the default
833 hardware geometry is OK */
834 lba_detected = 1;
835 goto default_geometry;
836 } else {
837 *pcyls = cylinders;
838 *pheads = heads;
839 *psecs = secs;
840 /* disable any translation to be in sync with
841 the logical geometry */
842 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
843 bdrv_set_translation_hint(bs,
844 BIOS_ATA_TRANSLATION_NONE);
845 }
846 }
847 } else {
848 default_geometry:
849 /* if no geometry, use a standard physical disk geometry */
850 cylinders = nb_sectors / (16 * 63);
851
852 if (cylinders > 16383)
853 cylinders = 16383;
854 else if (cylinders < 2)
855 cylinders = 2;
856 *pcyls = cylinders;
857 *pheads = 16;
858 *psecs = 63;
859 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
860 if ((*pcyls * *pheads) <= 131072) {
861 bdrv_set_translation_hint(bs,
862 BIOS_ATA_TRANSLATION_LARGE);
863 } else {
864 bdrv_set_translation_hint(bs,
865 BIOS_ATA_TRANSLATION_LBA);
866 }
867 }
868 }
869 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
870 }
871}
872
ths5fafdf22007-09-16 21:08:06 +0000873void bdrv_set_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000874 int cyls, int heads, int secs)
875{
876 bs->cyls = cyls;
877 bs->heads = heads;
878 bs->secs = secs;
879}
880
881void bdrv_set_type_hint(BlockDriverState *bs, int type)
882{
883 bs->type = type;
884 bs->removable = ((type == BDRV_TYPE_CDROM ||
885 type == BDRV_TYPE_FLOPPY));
886}
887
bellard46d47672004-11-16 01:45:27 +0000888void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
889{
890 bs->translation = translation;
891}
892
ths5fafdf22007-09-16 21:08:06 +0000893void bdrv_get_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000894 int *pcyls, int *pheads, int *psecs)
895{
896 *pcyls = bs->cyls;
897 *pheads = bs->heads;
898 *psecs = bs->secs;
899}
900
901int bdrv_get_type_hint(BlockDriverState *bs)
902{
903 return bs->type;
904}
905
bellard46d47672004-11-16 01:45:27 +0000906int bdrv_get_translation_hint(BlockDriverState *bs)
907{
908 return bs->translation;
909}
910
bellardb3380822004-03-14 21:38:54 +0000911int bdrv_is_removable(BlockDriverState *bs)
912{
913 return bs->removable;
914}
915
916int bdrv_is_read_only(BlockDriverState *bs)
917{
918 return bs->read_only;
919}
920
ths985a03b2007-12-24 16:10:43 +0000921int bdrv_is_sg(BlockDriverState *bs)
922{
923 return bs->sg;
924}
925
bellard19cb3732006-08-19 11:45:59 +0000926/* XXX: no longer used */
ths5fafdf22007-09-16 21:08:06 +0000927void bdrv_set_change_cb(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000928 void (*change_cb)(void *opaque), void *opaque)
929{
930 bs->change_cb = change_cb;
931 bs->change_opaque = opaque;
932}
933
bellardea2384d2004-08-01 21:59:26 +0000934int bdrv_is_encrypted(BlockDriverState *bs)
935{
936 if (bs->backing_hd && bs->backing_hd->encrypted)
937 return 1;
938 return bs->encrypted;
939}
940
aliguoric0f4ce72009-03-05 23:01:01 +0000941int bdrv_key_required(BlockDriverState *bs)
942{
943 BlockDriverState *backing_hd = bs->backing_hd;
944
945 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
946 return 1;
947 return (bs->encrypted && !bs->valid_key);
948}
949
bellardea2384d2004-08-01 21:59:26 +0000950int bdrv_set_key(BlockDriverState *bs, const char *key)
951{
952 int ret;
953 if (bs->backing_hd && bs->backing_hd->encrypted) {
954 ret = bdrv_set_key(bs->backing_hd, key);
955 if (ret < 0)
956 return ret;
957 if (!bs->encrypted)
958 return 0;
959 }
960 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
961 return -1;
aliguoric0f4ce72009-03-05 23:01:01 +0000962 ret = bs->drv->bdrv_set_key(bs, key);
aliguoribb5fc202009-03-05 23:01:15 +0000963 if (ret < 0) {
964 bs->valid_key = 0;
965 } else if (!bs->valid_key) {
966 bs->valid_key = 1;
967 /* call the change callback now, we skipped it on open */
968 bs->media_changed = 1;
969 if (bs->change_cb)
970 bs->change_cb(bs->change_opaque);
971 }
aliguoric0f4ce72009-03-05 23:01:01 +0000972 return ret;
bellardea2384d2004-08-01 21:59:26 +0000973}
974
975void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
976{
bellard19cb3732006-08-19 11:45:59 +0000977 if (!bs->drv) {
bellardea2384d2004-08-01 21:59:26 +0000978 buf[0] = '\0';
979 } else {
980 pstrcpy(buf, buf_size, bs->drv->format_name);
981 }
982}
983
ths5fafdf22007-09-16 21:08:06 +0000984void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
bellardea2384d2004-08-01 21:59:26 +0000985 void *opaque)
986{
987 BlockDriver *drv;
988
989 for (drv = first_drv; drv != NULL; drv = drv->next) {
990 it(opaque, drv->format_name);
991 }
992}
993
bellardb3380822004-03-14 21:38:54 +0000994BlockDriverState *bdrv_find(const char *name)
995{
996 BlockDriverState *bs;
997
998 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
999 if (!strcmp(name, bs->device_name))
1000 return bs;
1001 }
1002 return NULL;
1003}
1004
aliguori51de9762009-03-05 23:00:43 +00001005void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
bellard81d09122004-07-14 17:21:37 +00001006{
1007 BlockDriverState *bs;
1008
1009 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori51de9762009-03-05 23:00:43 +00001010 it(opaque, bs);
bellard81d09122004-07-14 17:21:37 +00001011 }
1012}
1013
bellardea2384d2004-08-01 21:59:26 +00001014const char *bdrv_get_device_name(BlockDriverState *bs)
1015{
1016 return bs->device_name;
1017}
1018
pbrook7a6cba62006-06-04 11:39:07 +00001019void bdrv_flush(BlockDriverState *bs)
1020{
aliguori081501d2009-03-29 01:31:51 +00001021 if (!bs->drv)
1022 return;
pbrook7a6cba62006-06-04 11:39:07 +00001023 if (bs->drv->bdrv_flush)
1024 bs->drv->bdrv_flush(bs);
1025 if (bs->backing_hd)
1026 bdrv_flush(bs->backing_hd);
1027}
1028
aliguoric6ca28d2008-10-06 13:55:43 +00001029void bdrv_flush_all(void)
1030{
1031 BlockDriverState *bs;
1032
1033 for (bs = bdrv_first; bs != NULL; bs = bs->next)
1034 if (bs->drv && !bdrv_is_read_only(bs) &&
1035 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1036 bdrv_flush(bs);
1037}
1038
thsf58c7b32008-06-05 21:53:49 +00001039/*
1040 * Returns true iff the specified sector is present in the disk image. Drivers
1041 * not implementing the functionality are assumed to not support backing files,
1042 * hence all their sectors are reported as allocated.
1043 *
1044 * 'pnum' is set to the number of sectors (including and immediately following
1045 * the specified sector) that are known to be in the same
1046 * allocated/unallocated state.
1047 *
1048 * 'nb_sectors' is the max value 'pnum' should be set to.
1049 */
1050int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1051 int *pnum)
1052{
1053 int64_t n;
1054 if (!bs->drv->bdrv_is_allocated) {
1055 if (sector_num >= bs->total_sectors) {
1056 *pnum = 0;
1057 return 0;
1058 }
1059 n = bs->total_sectors - sector_num;
1060 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1061 return 1;
1062 }
1063 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1064}
1065
aliguori376253e2009-03-05 23:01:23 +00001066void bdrv_info(Monitor *mon)
bellardb3380822004-03-14 21:38:54 +00001067{
1068 BlockDriverState *bs;
1069
1070 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori376253e2009-03-05 23:01:23 +00001071 monitor_printf(mon, "%s:", bs->device_name);
1072 monitor_printf(mon, " type=");
bellardb3380822004-03-14 21:38:54 +00001073 switch(bs->type) {
1074 case BDRV_TYPE_HD:
aliguori376253e2009-03-05 23:01:23 +00001075 monitor_printf(mon, "hd");
bellardb3380822004-03-14 21:38:54 +00001076 break;
1077 case BDRV_TYPE_CDROM:
aliguori376253e2009-03-05 23:01:23 +00001078 monitor_printf(mon, "cdrom");
bellardb3380822004-03-14 21:38:54 +00001079 break;
1080 case BDRV_TYPE_FLOPPY:
aliguori376253e2009-03-05 23:01:23 +00001081 monitor_printf(mon, "floppy");
bellardb3380822004-03-14 21:38:54 +00001082 break;
1083 }
aliguori376253e2009-03-05 23:01:23 +00001084 monitor_printf(mon, " removable=%d", bs->removable);
bellardb3380822004-03-14 21:38:54 +00001085 if (bs->removable) {
aliguori376253e2009-03-05 23:01:23 +00001086 monitor_printf(mon, " locked=%d", bs->locked);
bellardb3380822004-03-14 21:38:54 +00001087 }
bellard19cb3732006-08-19 11:45:59 +00001088 if (bs->drv) {
aliguori376253e2009-03-05 23:01:23 +00001089 monitor_printf(mon, " file=");
1090 monitor_print_filename(mon, bs->filename);
thsfef30742006-12-22 14:11:32 +00001091 if (bs->backing_file[0] != '\0') {
aliguori376253e2009-03-05 23:01:23 +00001092 monitor_printf(mon, " backing_file=");
1093 monitor_print_filename(mon, bs->backing_file);
1094 }
1095 monitor_printf(mon, " ro=%d", bs->read_only);
1096 monitor_printf(mon, " drv=%s", bs->drv->format_name);
1097 monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs));
bellardb3380822004-03-14 21:38:54 +00001098 } else {
aliguori376253e2009-03-05 23:01:23 +00001099 monitor_printf(mon, " [not inserted]");
bellardb3380822004-03-14 21:38:54 +00001100 }
aliguori376253e2009-03-05 23:01:23 +00001101 monitor_printf(mon, "\n");
bellardb3380822004-03-14 21:38:54 +00001102 }
1103}
thsa36e69d2007-12-02 05:18:19 +00001104
1105/* The "info blockstats" command. */
aliguori376253e2009-03-05 23:01:23 +00001106void bdrv_info_stats(Monitor *mon)
thsa36e69d2007-12-02 05:18:19 +00001107{
1108 BlockDriverState *bs;
1109
1110 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori376253e2009-03-05 23:01:23 +00001111 monitor_printf(mon, "%s:"
1112 " rd_bytes=%" PRIu64
1113 " wr_bytes=%" PRIu64
1114 " rd_operations=%" PRIu64
1115 " wr_operations=%" PRIu64
aliguoriebf53fc2009-03-11 20:05:29 +00001116 "\n",
aliguori376253e2009-03-05 23:01:23 +00001117 bs->device_name,
1118 bs->rd_bytes, bs->wr_bytes,
1119 bs->rd_ops, bs->wr_ops);
thsa36e69d2007-12-02 05:18:19 +00001120 }
1121}
bellardea2384d2004-08-01 21:59:26 +00001122
aliguori045df332009-03-05 23:00:48 +00001123const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1124{
1125 if (bs->backing_hd && bs->backing_hd->encrypted)
1126 return bs->backing_file;
1127 else if (bs->encrypted)
1128 return bs->filename;
1129 else
1130 return NULL;
1131}
1132
ths5fafdf22007-09-16 21:08:06 +00001133void bdrv_get_backing_filename(BlockDriverState *bs,
bellard83f64092006-08-01 16:21:11 +00001134 char *filename, int filename_size)
bellardea2384d2004-08-01 21:59:26 +00001135{
bellard83f64092006-08-01 16:21:11 +00001136 if (!bs->backing_hd) {
1137 pstrcpy(filename, filename_size, "");
1138 } else {
1139 pstrcpy(filename, filename_size, bs->backing_file);
1140 }
bellardea2384d2004-08-01 21:59:26 +00001141}
1142
ths5fafdf22007-09-16 21:08:06 +00001143int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
bellardfaea38e2006-08-05 21:31:00 +00001144 const uint8_t *buf, int nb_sectors)
1145{
1146 BlockDriver *drv = bs->drv;
1147 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001148 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001149 if (!drv->bdrv_write_compressed)
1150 return -ENOTSUP;
1151 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1152}
ths3b46e622007-09-17 08:09:54 +00001153
bellardfaea38e2006-08-05 21:31:00 +00001154int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1155{
1156 BlockDriver *drv = bs->drv;
1157 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001158 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001159 if (!drv->bdrv_get_info)
1160 return -ENOTSUP;
1161 memset(bdi, 0, sizeof(*bdi));
1162 return drv->bdrv_get_info(bs, bdi);
1163}
1164
aliguori178e08a2009-04-05 19:10:55 +00001165int bdrv_put_buffer(BlockDriverState *bs, const uint8_t *buf, int64_t pos, int size)
1166{
1167 BlockDriver *drv = bs->drv;
1168 if (!drv)
1169 return -ENOMEDIUM;
1170 if (!drv->bdrv_put_buffer)
1171 return -ENOTSUP;
1172 return drv->bdrv_put_buffer(bs, buf, pos, size);
1173}
1174
1175int bdrv_get_buffer(BlockDriverState *bs, uint8_t *buf, int64_t pos, int size)
1176{
1177 BlockDriver *drv = bs->drv;
1178 if (!drv)
1179 return -ENOMEDIUM;
1180 if (!drv->bdrv_get_buffer)
1181 return -ENOTSUP;
1182 return drv->bdrv_get_buffer(bs, buf, pos, size);
1183}
1184
bellardfaea38e2006-08-05 21:31:00 +00001185/**************************************************************/
1186/* handling of snapshots */
1187
ths5fafdf22007-09-16 21:08:06 +00001188int bdrv_snapshot_create(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001189 QEMUSnapshotInfo *sn_info)
1190{
1191 BlockDriver *drv = bs->drv;
1192 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001193 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001194 if (!drv->bdrv_snapshot_create)
1195 return -ENOTSUP;
1196 return drv->bdrv_snapshot_create(bs, sn_info);
1197}
1198
ths5fafdf22007-09-16 21:08:06 +00001199int bdrv_snapshot_goto(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001200 const char *snapshot_id)
1201{
1202 BlockDriver *drv = bs->drv;
1203 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001204 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001205 if (!drv->bdrv_snapshot_goto)
1206 return -ENOTSUP;
1207 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1208}
1209
1210int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1211{
1212 BlockDriver *drv = bs->drv;
1213 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001214 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001215 if (!drv->bdrv_snapshot_delete)
1216 return -ENOTSUP;
1217 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1218}
1219
ths5fafdf22007-09-16 21:08:06 +00001220int bdrv_snapshot_list(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001221 QEMUSnapshotInfo **psn_info)
1222{
1223 BlockDriver *drv = bs->drv;
1224 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001225 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001226 if (!drv->bdrv_snapshot_list)
1227 return -ENOTSUP;
1228 return drv->bdrv_snapshot_list(bs, psn_info);
1229}
1230
1231#define NB_SUFFIXES 4
1232
1233char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1234{
1235 static const char suffixes[NB_SUFFIXES] = "KMGT";
1236 int64_t base;
1237 int i;
1238
1239 if (size <= 999) {
1240 snprintf(buf, buf_size, "%" PRId64, size);
1241 } else {
1242 base = 1024;
1243 for(i = 0; i < NB_SUFFIXES; i++) {
1244 if (size < (10 * base)) {
ths5fafdf22007-09-16 21:08:06 +00001245 snprintf(buf, buf_size, "%0.1f%c",
bellardfaea38e2006-08-05 21:31:00 +00001246 (double)size / base,
1247 suffixes[i]);
1248 break;
1249 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
ths5fafdf22007-09-16 21:08:06 +00001250 snprintf(buf, buf_size, "%" PRId64 "%c",
bellardfaea38e2006-08-05 21:31:00 +00001251 ((size + (base >> 1)) / base),
1252 suffixes[i]);
1253 break;
1254 }
1255 base = base * 1024;
1256 }
1257 }
1258 return buf;
1259}
1260
1261char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1262{
1263 char buf1[128], date_buf[128], clock_buf[128];
bellard3b9f94e2007-01-07 17:27:07 +00001264#ifdef _WIN32
1265 struct tm *ptm;
1266#else
bellardfaea38e2006-08-05 21:31:00 +00001267 struct tm tm;
bellard3b9f94e2007-01-07 17:27:07 +00001268#endif
bellardfaea38e2006-08-05 21:31:00 +00001269 time_t ti;
1270 int64_t secs;
1271
1272 if (!sn) {
ths5fafdf22007-09-16 21:08:06 +00001273 snprintf(buf, buf_size,
1274 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001275 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1276 } else {
1277 ti = sn->date_sec;
bellard3b9f94e2007-01-07 17:27:07 +00001278#ifdef _WIN32
1279 ptm = localtime(&ti);
1280 strftime(date_buf, sizeof(date_buf),
1281 "%Y-%m-%d %H:%M:%S", ptm);
1282#else
bellardfaea38e2006-08-05 21:31:00 +00001283 localtime_r(&ti, &tm);
1284 strftime(date_buf, sizeof(date_buf),
1285 "%Y-%m-%d %H:%M:%S", &tm);
bellard3b9f94e2007-01-07 17:27:07 +00001286#endif
bellardfaea38e2006-08-05 21:31:00 +00001287 secs = sn->vm_clock_nsec / 1000000000;
1288 snprintf(clock_buf, sizeof(clock_buf),
1289 "%02d:%02d:%02d.%03d",
1290 (int)(secs / 3600),
1291 (int)((secs / 60) % 60),
ths5fafdf22007-09-16 21:08:06 +00001292 (int)(secs % 60),
bellardfaea38e2006-08-05 21:31:00 +00001293 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1294 snprintf(buf, buf_size,
ths5fafdf22007-09-16 21:08:06 +00001295 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001296 sn->id_str, sn->name,
1297 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1298 date_buf,
1299 clock_buf);
1300 }
1301 return buf;
1302}
1303
bellardea2384d2004-08-01 21:59:26 +00001304
bellard83f64092006-08-01 16:21:11 +00001305/**************************************************************/
1306/* async I/Os */
1307
aliguori3b69e4b2009-01-22 16:59:24 +00001308BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
aliguorif141eaf2009-04-07 18:43:24 +00001309 QEMUIOVector *qiov, int nb_sectors,
aliguori3b69e4b2009-01-22 16:59:24 +00001310 BlockDriverCompletionFunc *cb, void *opaque)
1311{
bellard83f64092006-08-01 16:21:11 +00001312 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001313 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001314
bellard19cb3732006-08-19 11:45:59 +00001315 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001316 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001317 if (bdrv_check_request(bs, sector_num, nb_sectors))
1318 return NULL;
ths3b46e622007-09-17 08:09:54 +00001319
aliguorif141eaf2009-04-07 18:43:24 +00001320 ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
1321 cb, opaque);
thsa36e69d2007-12-02 05:18:19 +00001322
1323 if (ret) {
1324 /* Update stats even though technically transfer has not happened. */
1325 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1326 bs->rd_ops ++;
1327 }
1328
1329 return ret;
bellard83f64092006-08-01 16:21:11 +00001330}
1331
aliguorif141eaf2009-04-07 18:43:24 +00001332BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1333 QEMUIOVector *qiov, int nb_sectors,
1334 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001335{
bellard83f64092006-08-01 16:21:11 +00001336 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001337 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001338
bellard19cb3732006-08-19 11:45:59 +00001339 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001340 return NULL;
bellard83f64092006-08-01 16:21:11 +00001341 if (bs->read_only)
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;
bellard83f64092006-08-01 16:21:11 +00001345
aliguorif141eaf2009-04-07 18:43:24 +00001346 ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
1347 cb, opaque);
thsa36e69d2007-12-02 05:18:19 +00001348
1349 if (ret) {
1350 /* Update stats even though technically transfer has not happened. */
1351 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1352 bs->wr_ops ++;
1353 }
1354
1355 return ret;
bellard83f64092006-08-01 16:21:11 +00001356}
1357
1358void bdrv_aio_cancel(BlockDriverAIOCB *acb)
pbrookce1a14d2006-08-07 02:38:06 +00001359{
aliguori6bbff9a2009-03-20 18:25:59 +00001360 acb->pool->cancel(acb);
bellard83f64092006-08-01 16:21:11 +00001361}
1362
pbrookce1a14d2006-08-07 02:38:06 +00001363
bellard83f64092006-08-01 16:21:11 +00001364/**************************************************************/
1365/* async block device emulation */
1366
bellard83f64092006-08-01 16:21:11 +00001367static void bdrv_aio_bh_cb(void *opaque)
bellardbeac80c2006-06-26 20:08:57 +00001368{
pbrookce1a14d2006-08-07 02:38:06 +00001369 BlockDriverAIOCBSync *acb = opaque;
aliguorif141eaf2009-04-07 18:43:24 +00001370
aliguorif141eaf2009-04-07 18:43:24 +00001371 if (!acb->is_write)
1372 qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
aliguoriceb42de2009-04-07 18:43:28 +00001373 qemu_vfree(acb->bounce);
pbrookce1a14d2006-08-07 02:38:06 +00001374 acb->common.cb(acb->common.opaque, acb->ret);
aliguorif141eaf2009-04-07 18:43:24 +00001375
pbrookce1a14d2006-08-07 02:38:06 +00001376 qemu_aio_release(acb);
bellardbeac80c2006-06-26 20:08:57 +00001377}
bellardbeac80c2006-06-26 20:08:57 +00001378
aliguorif141eaf2009-04-07 18:43:24 +00001379static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
1380 int64_t sector_num,
1381 QEMUIOVector *qiov,
1382 int nb_sectors,
1383 BlockDriverCompletionFunc *cb,
1384 void *opaque,
1385 int is_write)
1386
bellardea2384d2004-08-01 21:59:26 +00001387{
pbrookce1a14d2006-08-07 02:38:06 +00001388 BlockDriverAIOCBSync *acb;
pbrookce1a14d2006-08-07 02:38:06 +00001389
1390 acb = qemu_aio_get(bs, cb, opaque);
aliguorif141eaf2009-04-07 18:43:24 +00001391 acb->is_write = is_write;
1392 acb->qiov = qiov;
1393 acb->bounce = qemu_memalign(512, qiov->size);
1394
pbrookce1a14d2006-08-07 02:38:06 +00001395 if (!acb->bh)
1396 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
aliguorif141eaf2009-04-07 18:43:24 +00001397
1398 if (is_write) {
1399 qemu_iovec_to_buffer(acb->qiov, acb->bounce);
1400 acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
1401 } else {
1402 acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
1403 }
1404
pbrookce1a14d2006-08-07 02:38:06 +00001405 qemu_bh_schedule(acb->bh);
aliguorif141eaf2009-04-07 18:43:24 +00001406
pbrookce1a14d2006-08-07 02:38:06 +00001407 return &acb->common;
pbrook7a6cba62006-06-04 11:39:07 +00001408}
1409
aliguorif141eaf2009-04-07 18:43:24 +00001410static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
1411 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
pbrookce1a14d2006-08-07 02:38:06 +00001412 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001413{
aliguorif141eaf2009-04-07 18:43:24 +00001414 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
bellard83f64092006-08-01 16:21:11 +00001415}
1416
aliguorif141eaf2009-04-07 18:43:24 +00001417static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
1418 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
1419 BlockDriverCompletionFunc *cb, void *opaque)
1420{
1421 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
1422}
1423
1424
pbrookce1a14d2006-08-07 02:38:06 +00001425static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
bellard83f64092006-08-01 16:21:11 +00001426{
pbrookce1a14d2006-08-07 02:38:06 +00001427 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1428 qemu_bh_cancel(acb->bh);
1429 qemu_aio_release(acb);
bellard83f64092006-08-01 16:21:11 +00001430}
bellard83f64092006-08-01 16:21:11 +00001431
1432/**************************************************************/
1433/* sync block device emulation */
1434
1435static void bdrv_rw_em_cb(void *opaque, int ret)
1436{
1437 *(int *)opaque = ret;
1438}
1439
1440#define NOT_DONE 0x7fffffff
1441
ths5fafdf22007-09-16 21:08:06 +00001442static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +00001443 uint8_t *buf, int nb_sectors)
1444{
pbrookce1a14d2006-08-07 02:38:06 +00001445 int async_ret;
1446 BlockDriverAIOCB *acb;
aliguorif141eaf2009-04-07 18:43:24 +00001447 struct iovec iov;
1448 QEMUIOVector qiov;
bellard83f64092006-08-01 16:21:11 +00001449
bellard83f64092006-08-01 16:21:11 +00001450 async_ret = NOT_DONE;
blueswir13f4cb3d2009-04-13 16:31:01 +00001451 iov.iov_base = (void *)buf;
aliguorif141eaf2009-04-07 18:43:24 +00001452 iov.iov_len = nb_sectors * 512;
1453 qemu_iovec_init_external(&qiov, &iov, 1);
1454 acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
1455 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;
aliguorif141eaf2009-04-07 18:43:24 +00001471 struct iovec iov;
1472 QEMUIOVector qiov;
bellard83f64092006-08-01 16:21:11 +00001473
bellard83f64092006-08-01 16:21:11 +00001474 async_ret = NOT_DONE;
aliguorif141eaf2009-04-07 18:43:24 +00001475 iov.iov_base = (void *)buf;
1476 iov.iov_len = nb_sectors * 512;
1477 qemu_iovec_init_external(&qiov, &iov, 1);
1478 acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
1479 bdrv_rw_em_cb, &async_ret);
aliguoribaf35cb2008-09-10 15:45:19 +00001480 if (acb == NULL)
pbrookce1a14d2006-08-07 02:38:06 +00001481 return -1;
bellard83f64092006-08-01 16:21:11 +00001482 while (async_ret == NOT_DONE) {
1483 qemu_aio_wait();
1484 }
bellard83f64092006-08-01 16:21:11 +00001485 return async_ret;
1486}
bellardea2384d2004-08-01 21:59:26 +00001487
1488void bdrv_init(void)
1489{
1490 bdrv_register(&bdrv_raw);
bellard19cb3732006-08-19 11:45:59 +00001491 bdrv_register(&bdrv_host_device);
bellardea2384d2004-08-01 21:59:26 +00001492#ifndef _WIN32
1493 bdrv_register(&bdrv_cow);
1494#endif
1495 bdrv_register(&bdrv_qcow);
1496 bdrv_register(&bdrv_vmdk);
bellard3c565212004-09-29 21:29:14 +00001497 bdrv_register(&bdrv_cloop);
bellard585d0ed2004-12-12 11:24:44 +00001498 bdrv_register(&bdrv_dmg);
bellarda8753c32005-04-26 21:34:00 +00001499 bdrv_register(&bdrv_bochs);
bellard6a0f9e82005-04-27 20:17:58 +00001500 bdrv_register(&bdrv_vpc);
bellard712e7872005-04-28 21:09:32 +00001501 bdrv_register(&bdrv_vvfat);
bellardfaea38e2006-08-05 21:31:00 +00001502 bdrv_register(&bdrv_qcow2);
ths6ada7452007-07-31 23:28:53 +00001503 bdrv_register(&bdrv_parallels);
ths75818252008-07-03 13:41:03 +00001504 bdrv_register(&bdrv_nbd);
bellardea2384d2004-08-01 21:59:26 +00001505}
pbrookce1a14d2006-08-07 02:38:06 +00001506
aliguori6bbff9a2009-03-20 18:25:59 +00001507void aio_pool_init(AIOPool *pool, int aiocb_size,
1508 void (*cancel)(BlockDriverAIOCB *acb))
pbrookce1a14d2006-08-07 02:38:06 +00001509{
aliguori6bbff9a2009-03-20 18:25:59 +00001510 pool->aiocb_size = aiocb_size;
1511 pool->cancel = cancel;
1512 pool->free_aiocb = NULL;
1513}
1514
1515void *qemu_aio_get_pool(AIOPool *pool, BlockDriverState *bs,
1516 BlockDriverCompletionFunc *cb, void *opaque)
1517{
pbrookce1a14d2006-08-07 02:38:06 +00001518 BlockDriverAIOCB *acb;
1519
aliguori6bbff9a2009-03-20 18:25:59 +00001520 if (pool->free_aiocb) {
1521 acb = pool->free_aiocb;
1522 pool->free_aiocb = acb->next;
pbrookce1a14d2006-08-07 02:38:06 +00001523 } else {
aliguori6bbff9a2009-03-20 18:25:59 +00001524 acb = qemu_mallocz(pool->aiocb_size);
1525 acb->pool = pool;
pbrookce1a14d2006-08-07 02:38:06 +00001526 }
1527 acb->bs = bs;
1528 acb->cb = cb;
1529 acb->opaque = opaque;
1530 return acb;
1531}
1532
aliguori6bbff9a2009-03-20 18:25:59 +00001533void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1534 void *opaque)
1535{
1536 return qemu_aio_get_pool(&bs->drv->aio_pool, bs, cb, opaque);
1537}
1538
pbrookce1a14d2006-08-07 02:38:06 +00001539void qemu_aio_release(void *p)
1540{
aliguori6bbff9a2009-03-20 18:25:59 +00001541 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
1542 AIOPool *pool = acb->pool;
1543 acb->next = pool->free_aiocb;
1544 pool->free_aiocb = acb;
pbrookce1a14d2006-08-07 02:38:06 +00001545}
bellard19cb3732006-08-19 11:45:59 +00001546
1547/**************************************************************/
1548/* removable device support */
1549
1550/**
1551 * Return TRUE if the media is present
1552 */
1553int bdrv_is_inserted(BlockDriverState *bs)
1554{
1555 BlockDriver *drv = bs->drv;
1556 int ret;
1557 if (!drv)
1558 return 0;
1559 if (!drv->bdrv_is_inserted)
1560 return 1;
1561 ret = drv->bdrv_is_inserted(bs);
1562 return ret;
1563}
1564
1565/**
1566 * Return TRUE if the media changed since the last call to this
ths5fafdf22007-09-16 21:08:06 +00001567 * function. It is currently only used for floppy disks
bellard19cb3732006-08-19 11:45:59 +00001568 */
1569int bdrv_media_changed(BlockDriverState *bs)
1570{
1571 BlockDriver *drv = bs->drv;
1572 int ret;
1573
1574 if (!drv || !drv->bdrv_media_changed)
1575 ret = -ENOTSUP;
1576 else
1577 ret = drv->bdrv_media_changed(bs);
1578 if (ret == -ENOTSUP)
1579 ret = bs->media_changed;
1580 bs->media_changed = 0;
1581 return ret;
1582}
1583
1584/**
1585 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1586 */
1587void bdrv_eject(BlockDriverState *bs, int eject_flag)
1588{
1589 BlockDriver *drv = bs->drv;
1590 int ret;
1591
1592 if (!drv || !drv->bdrv_eject) {
1593 ret = -ENOTSUP;
1594 } else {
1595 ret = drv->bdrv_eject(bs, eject_flag);
1596 }
1597 if (ret == -ENOTSUP) {
1598 if (eject_flag)
1599 bdrv_close(bs);
1600 }
1601}
1602
1603int bdrv_is_locked(BlockDriverState *bs)
1604{
1605 return bs->locked;
1606}
1607
1608/**
1609 * Lock or unlock the media (if it is locked, the user won't be able
1610 * to eject it manually).
1611 */
1612void bdrv_set_locked(BlockDriverState *bs, int locked)
1613{
1614 BlockDriver *drv = bs->drv;
1615
1616 bs->locked = locked;
1617 if (drv && drv->bdrv_set_locked) {
1618 drv->bdrv_set_locked(bs, locked);
1619 }
1620}
ths985a03b2007-12-24 16:10:43 +00001621
1622/* needed for generic scsi interface */
1623
1624int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1625{
1626 BlockDriver *drv = bs->drv;
1627
1628 if (drv && drv->bdrv_ioctl)
1629 return drv->bdrv_ioctl(bs, req, buf);
1630 return -ENOTSUP;
1631}
aliguori7d780662009-03-12 19:57:08 +00001632
aliguori221f7152009-03-28 17:28:41 +00001633BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
1634 unsigned long int req, void *buf,
1635 BlockDriverCompletionFunc *cb, void *opaque)
aliguori7d780662009-03-12 19:57:08 +00001636{
aliguori221f7152009-03-28 17:28:41 +00001637 BlockDriver *drv = bs->drv;
aliguori7d780662009-03-12 19:57:08 +00001638
aliguori221f7152009-03-28 17:28:41 +00001639 if (drv && drv->bdrv_aio_ioctl)
1640 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
1641 return NULL;
aliguori7d780662009-03-12 19:57:08 +00001642}