blob: 3d1223d5e094dffdba4d0fd9e79be4af3e882b74 [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;
aliguorie268ca52009-04-22 20:20:00 +0000365 /* buffer_alignment defaulted to 512, drivers can change this value */
366 bs->buffer_alignment = 512;
bellard712e7872005-04-28 21:09:32 +0000367
bellard83f64092006-08-01 16:21:11 +0000368 if (flags & BDRV_O_SNAPSHOT) {
bellardea2384d2004-08-01 21:59:26 +0000369 BlockDriverState *bs1;
370 int64_t total_size;
aliguori7c96d462008-09-12 17:54:13 +0000371 int is_protocol = 0;
ths3b46e622007-09-17 08:09:54 +0000372
bellardea2384d2004-08-01 21:59:26 +0000373 /* if snapshot, we create a temporary backing file and open it
374 instead of opening 'filename' directly */
375
376 /* if there is a backing file, use it */
377 bs1 = bdrv_new("");
aliguori5eb45632009-03-28 17:55:10 +0000378 ret = bdrv_open2(bs1, filename, 0, drv);
aliguori51d7c002009-03-05 23:00:29 +0000379 if (ret < 0) {
bellardea2384d2004-08-01 21:59:26 +0000380 bdrv_delete(bs1);
aliguori51d7c002009-03-05 23:00:29 +0000381 return ret;
bellardea2384d2004-08-01 21:59:26 +0000382 }
bellard83f64092006-08-01 16:21:11 +0000383 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
aliguori7c96d462008-09-12 17:54:13 +0000384
385 if (bs1->drv && bs1->drv->protocol_name)
386 is_protocol = 1;
387
bellardea2384d2004-08-01 21:59:26 +0000388 bdrv_delete(bs1);
ths3b46e622007-09-17 08:09:54 +0000389
bellardea2384d2004-08-01 21:59:26 +0000390 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
aliguori7c96d462008-09-12 17:54:13 +0000391
392 /* Real path is meaningless for protocols */
393 if (is_protocol)
394 snprintf(backing_filename, sizeof(backing_filename),
395 "%s", filename);
396 else
397 realpath(filename, backing_filename);
398
aliguori5eb45632009-03-28 17:55:10 +0000399 ret = bdrv_create2(&bdrv_qcow2, tmp_filename,
400 total_size, backing_filename,
401 (drv ? drv->format_name : NULL), 0);
aliguori51d7c002009-03-05 23:00:29 +0000402 if (ret < 0) {
403 return ret;
bellardea2384d2004-08-01 21:59:26 +0000404 }
405 filename = tmp_filename;
aliguori5eb45632009-03-28 17:55:10 +0000406 drv = &bdrv_qcow2;
bellardea2384d2004-08-01 21:59:26 +0000407 bs->is_temporary = 1;
408 }
bellard712e7872005-04-28 21:09:32 +0000409
bellardea2384d2004-08-01 21:59:26 +0000410 pstrcpy(bs->filename, sizeof(bs->filename), filename);
bellard83f64092006-08-01 16:21:11 +0000411 if (flags & BDRV_O_FILE) {
412 drv = find_protocol(filename);
aliguori51d7c002009-03-05 23:00:29 +0000413 } else if (!drv) {
414 drv = find_image_format(filename);
415 }
416 if (!drv) {
417 ret = -ENOENT;
418 goto unlink_and_fail;
bellardea2384d2004-08-01 21:59:26 +0000419 }
420 bs->drv = drv;
421 bs->opaque = qemu_mallocz(drv->instance_size);
bellard83f64092006-08-01 16:21:11 +0000422 /* Note: for compatibility, we open disk image files as RDWR, and
423 RDONLY as fallback */
424 if (!(flags & BDRV_O_FILE))
aliguori9f7965c2008-10-14 14:42:54 +0000425 open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK);
bellard83f64092006-08-01 16:21:11 +0000426 else
427 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
428 ret = drv->bdrv_open(bs, filename, open_flags);
aurel32a0a83532008-10-13 21:08:34 +0000429 if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
aliguori9f7965c2008-10-14 14:42:54 +0000430 ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
bellard83f64092006-08-01 16:21:11 +0000431 bs->read_only = 1;
432 }
bellardea2384d2004-08-01 21:59:26 +0000433 if (ret < 0) {
434 qemu_free(bs->opaque);
bellard6b21b972006-08-23 21:14:37 +0000435 bs->opaque = NULL;
436 bs->drv = NULL;
aliguori51d7c002009-03-05 23:00:29 +0000437 unlink_and_fail:
438 if (bs->is_temporary)
439 unlink(filename);
bellard83f64092006-08-01 16:21:11 +0000440 return ret;
bellardea2384d2004-08-01 21:59:26 +0000441 }
bellardd15a7712006-08-06 13:35:09 +0000442 if (drv->bdrv_getlength) {
443 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
444 }
bellardea2384d2004-08-01 21:59:26 +0000445#ifndef _WIN32
446 if (bs->is_temporary) {
447 unlink(filename);
448 }
449#endif
bellard83f64092006-08-01 16:21:11 +0000450 if (bs->backing_file[0] != '\0') {
bellardea2384d2004-08-01 21:59:26 +0000451 /* if there is a backing file, use it */
aliguori5eb45632009-03-28 17:55:10 +0000452 BlockDriver *back_drv = NULL;
bellardea2384d2004-08-01 21:59:26 +0000453 bs->backing_hd = bdrv_new("");
bellard83f64092006-08-01 16:21:11 +0000454 path_combine(backing_filename, sizeof(backing_filename),
455 filename, bs->backing_file);
aliguori5eb45632009-03-28 17:55:10 +0000456 if (bs->backing_format[0] != '\0')
457 back_drv = bdrv_find_format(bs->backing_format);
458 ret = bdrv_open2(bs->backing_hd, backing_filename, open_flags,
459 back_drv);
aliguori51d7c002009-03-05 23:00:29 +0000460 if (ret < 0) {
461 bdrv_close(bs);
462 return ret;
463 }
bellardea2384d2004-08-01 21:59:26 +0000464 }
465
aliguoribb5fc202009-03-05 23:01:15 +0000466 if (!bdrv_key_required(bs)) {
467 /* call the change callback */
468 bs->media_changed = 1;
469 if (bs->change_cb)
470 bs->change_cb(bs->change_opaque);
471 }
bellardb3380822004-03-14 21:38:54 +0000472 return 0;
bellardfc01f7e2003-06-30 10:03:06 +0000473}
474
475void bdrv_close(BlockDriverState *bs)
476{
bellard19cb3732006-08-19 11:45:59 +0000477 if (bs->drv) {
bellardea2384d2004-08-01 21:59:26 +0000478 if (bs->backing_hd)
479 bdrv_delete(bs->backing_hd);
480 bs->drv->bdrv_close(bs);
481 qemu_free(bs->opaque);
482#ifdef _WIN32
483 if (bs->is_temporary) {
484 unlink(bs->filename);
485 }
bellard67b915a2004-03-31 23:37:16 +0000486#endif
bellardea2384d2004-08-01 21:59:26 +0000487 bs->opaque = NULL;
488 bs->drv = NULL;
bellardb3380822004-03-14 21:38:54 +0000489
490 /* call the change callback */
bellard19cb3732006-08-19 11:45:59 +0000491 bs->media_changed = 1;
bellardb3380822004-03-14 21:38:54 +0000492 if (bs->change_cb)
493 bs->change_cb(bs->change_opaque);
494 }
495}
496
497void bdrv_delete(BlockDriverState *bs)
498{
aurel3234c6f052008-04-08 19:51:21 +0000499 BlockDriverState **pbs;
500
501 pbs = &bdrv_first;
502 while (*pbs != bs && *pbs != NULL)
503 pbs = &(*pbs)->next;
504 if (*pbs == bs)
505 *pbs = bs->next;
506
bellardb3380822004-03-14 21:38:54 +0000507 bdrv_close(bs);
508 qemu_free(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000509}
510
aliguorie97fc192009-04-21 23:11:50 +0000511/*
512 * Run consistency checks on an image
513 *
514 * Returns the number of errors or -errno when an internal error occurs
515 */
516int bdrv_check(BlockDriverState *bs)
517{
518 if (bs->drv->bdrv_check == NULL) {
519 return -ENOTSUP;
520 }
521
522 return bs->drv->bdrv_check(bs);
523}
524
bellard33e39632003-07-06 17:15:21 +0000525/* commit COW file into the raw image */
526int bdrv_commit(BlockDriverState *bs)
527{
bellard19cb3732006-08-19 11:45:59 +0000528 BlockDriver *drv = bs->drv;
bellard83f64092006-08-01 16:21:11 +0000529 int64_t i, total_sectors;
bellardea2384d2004-08-01 21:59:26 +0000530 int n, j;
531 unsigned char sector[512];
bellard33e39632003-07-06 17:15:21 +0000532
bellard19cb3732006-08-19 11:45:59 +0000533 if (!drv)
534 return -ENOMEDIUM;
bellard33e39632003-07-06 17:15:21 +0000535
536 if (bs->read_only) {
bellardea2384d2004-08-01 21:59:26 +0000537 return -EACCES;
bellard33e39632003-07-06 17:15:21 +0000538 }
539
bellardea2384d2004-08-01 21:59:26 +0000540 if (!bs->backing_hd) {
541 return -ENOTSUP;
bellard33e39632003-07-06 17:15:21 +0000542 }
bellardea2384d2004-08-01 21:59:26 +0000543
bellard83f64092006-08-01 16:21:11 +0000544 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
545 for (i = 0; i < total_sectors;) {
bellard19cb3732006-08-19 11:45:59 +0000546 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
bellardea2384d2004-08-01 21:59:26 +0000547 for(j = 0; j < n; j++) {
548 if (bdrv_read(bs, i, sector, 1) != 0) {
549 return -EIO;
550 }
551
552 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
553 return -EIO;
554 }
555 i++;
556 }
557 } else {
558 i += n;
559 }
560 }
bellard95389c82005-12-18 18:28:15 +0000561
bellard19cb3732006-08-19 11:45:59 +0000562 if (drv->bdrv_make_empty)
563 return drv->bdrv_make_empty(bs);
bellard95389c82005-12-18 18:28:15 +0000564
bellard33e39632003-07-06 17:15:21 +0000565 return 0;
566}
567
aliguori71d07702009-03-03 17:37:16 +0000568static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
569 size_t size)
570{
571 int64_t len;
572
573 if (!bdrv_is_inserted(bs))
574 return -ENOMEDIUM;
575
576 if (bs->growable)
577 return 0;
578
579 len = bdrv_getlength(bs);
580
581 if ((offset + size) > len)
582 return -EIO;
583
584 return 0;
585}
586
587static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
588 int nb_sectors)
589{
aliguori999dec52009-03-29 01:31:48 +0000590 return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512);
aliguori71d07702009-03-03 17:37:16 +0000591}
592
bellard19cb3732006-08-19 11:45:59 +0000593/* return < 0 if error. See bdrv_write() for the return codes */
ths5fafdf22007-09-16 21:08:06 +0000594int bdrv_read(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000595 uint8_t *buf, int nb_sectors)
596{
bellardea2384d2004-08-01 21:59:26 +0000597 BlockDriver *drv = bs->drv;
598
bellard19cb3732006-08-19 11:45:59 +0000599 if (!drv)
600 return -ENOMEDIUM;
aliguori71d07702009-03-03 17:37:16 +0000601 if (bdrv_check_request(bs, sector_num, nb_sectors))
602 return -EIO;
bellardb3380822004-03-14 21:38:54 +0000603
aliguorieda578e2009-03-12 19:57:16 +0000604 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
bellardfc01f7e2003-06-30 10:03:06 +0000605}
606
ths5fafdf22007-09-16 21:08:06 +0000607/* Return < 0 if error. Important errors are:
bellard19cb3732006-08-19 11:45:59 +0000608 -EIO generic I/O error (may happen for all errors)
609 -ENOMEDIUM No media inserted.
610 -EINVAL Invalid sector number or nb_sectors
611 -EACCES Trying to write a read-only device
612*/
ths5fafdf22007-09-16 21:08:06 +0000613int bdrv_write(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000614 const uint8_t *buf, int nb_sectors)
615{
bellard83f64092006-08-01 16:21:11 +0000616 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +0000617 if (!bs->drv)
618 return -ENOMEDIUM;
bellard0849bf02003-06-30 23:17:31 +0000619 if (bs->read_only)
bellard19cb3732006-08-19 11:45:59 +0000620 return -EACCES;
aliguori71d07702009-03-03 17:37:16 +0000621 if (bdrv_check_request(bs, sector_num, nb_sectors))
622 return -EIO;
623
aliguori42fb2802009-01-15 20:43:39 +0000624 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
bellard83f64092006-08-01 16:21:11 +0000625}
626
aliguorieda578e2009-03-12 19:57:16 +0000627int bdrv_pread(BlockDriverState *bs, int64_t offset,
628 void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000629{
bellard83f64092006-08-01 16:21:11 +0000630 uint8_t tmp_buf[SECTOR_SIZE];
631 int len, nb_sectors, count;
632 int64_t sector_num;
633
634 count = count1;
635 /* first read to align to sector start */
636 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
637 if (len > count)
638 len = count;
639 sector_num = offset >> SECTOR_BITS;
640 if (len > 0) {
641 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
642 return -EIO;
643 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
644 count -= len;
645 if (count == 0)
646 return count1;
647 sector_num++;
648 buf += len;
649 }
650
651 /* read the sectors "in place" */
652 nb_sectors = count >> SECTOR_BITS;
653 if (nb_sectors > 0) {
654 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
655 return -EIO;
656 sector_num += nb_sectors;
657 len = nb_sectors << SECTOR_BITS;
658 buf += len;
659 count -= len;
660 }
661
662 /* add data from the last sector */
663 if (count > 0) {
664 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
665 return -EIO;
666 memcpy(buf, tmp_buf, count);
667 }
668 return count1;
669}
670
aliguorieda578e2009-03-12 19:57:16 +0000671int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
672 const void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000673{
bellard83f64092006-08-01 16:21:11 +0000674 uint8_t tmp_buf[SECTOR_SIZE];
675 int len, nb_sectors, count;
676 int64_t sector_num;
677
678 count = count1;
679 /* first write to align to sector start */
680 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
681 if (len > count)
682 len = count;
683 sector_num = offset >> SECTOR_BITS;
684 if (len > 0) {
685 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
686 return -EIO;
687 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
688 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
689 return -EIO;
690 count -= len;
691 if (count == 0)
692 return count1;
693 sector_num++;
694 buf += len;
695 }
696
697 /* write the sectors "in place" */
698 nb_sectors = count >> SECTOR_BITS;
699 if (nb_sectors > 0) {
700 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
701 return -EIO;
702 sector_num += nb_sectors;
703 len = nb_sectors << SECTOR_BITS;
704 buf += len;
705 count -= len;
706 }
707
708 /* add data from the last sector */
709 if (count > 0) {
710 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
711 return -EIO;
712 memcpy(tmp_buf, buf, count);
713 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
714 return -EIO;
715 }
716 return count1;
717}
bellard83f64092006-08-01 16:21:11 +0000718
719/**
bellard83f64092006-08-01 16:21:11 +0000720 * Truncate file to 'offset' bytes (needed only for file protocols)
721 */
722int bdrv_truncate(BlockDriverState *bs, int64_t offset)
723{
724 BlockDriver *drv = bs->drv;
725 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000726 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000727 if (!drv->bdrv_truncate)
728 return -ENOTSUP;
729 return drv->bdrv_truncate(bs, offset);
730}
731
732/**
733 * Length of a file in bytes. Return < 0 if error or unknown.
734 */
735int64_t bdrv_getlength(BlockDriverState *bs)
736{
737 BlockDriver *drv = bs->drv;
738 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000739 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000740 if (!drv->bdrv_getlength) {
741 /* legacy mode */
742 return bs->total_sectors * SECTOR_SIZE;
743 }
744 return drv->bdrv_getlength(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000745}
746
bellard19cb3732006-08-19 11:45:59 +0000747/* return 0 as number of sectors if no device present or error */
ths96b8f132007-12-17 01:35:20 +0000748void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
bellardfc01f7e2003-06-30 10:03:06 +0000749{
bellard19cb3732006-08-19 11:45:59 +0000750 int64_t length;
751 length = bdrv_getlength(bs);
752 if (length < 0)
753 length = 0;
754 else
755 length = length >> SECTOR_BITS;
756 *nb_sectors_ptr = length;
bellardfc01f7e2003-06-30 10:03:06 +0000757}
bellardcf989512004-02-16 21:56:36 +0000758
aliguorif3d54fc2008-11-25 21:50:24 +0000759struct partition {
760 uint8_t boot_ind; /* 0x80 - active */
761 uint8_t head; /* starting head */
762 uint8_t sector; /* starting sector */
763 uint8_t cyl; /* starting cylinder */
764 uint8_t sys_ind; /* What partition type */
765 uint8_t end_head; /* end head */
766 uint8_t end_sector; /* end sector */
767 uint8_t end_cyl; /* end cylinder */
768 uint32_t start_sect; /* starting sector counting from 0 */
769 uint32_t nr_sects; /* nr of sectors in partition */
770} __attribute__((packed));
771
772/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
773static int guess_disk_lchs(BlockDriverState *bs,
774 int *pcylinders, int *pheads, int *psectors)
775{
776 uint8_t buf[512];
777 int ret, i, heads, sectors, cylinders;
778 struct partition *p;
779 uint32_t nr_sects;
blueswir1a38131b2008-12-05 17:56:40 +0000780 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +0000781
782 bdrv_get_geometry(bs, &nb_sectors);
783
784 ret = bdrv_read(bs, 0, buf, 1);
785 if (ret < 0)
786 return -1;
787 /* test msdos magic */
788 if (buf[510] != 0x55 || buf[511] != 0xaa)
789 return -1;
790 for(i = 0; i < 4; i++) {
791 p = ((struct partition *)(buf + 0x1be)) + i;
792 nr_sects = le32_to_cpu(p->nr_sects);
793 if (nr_sects && p->end_head) {
794 /* We make the assumption that the partition terminates on
795 a cylinder boundary */
796 heads = p->end_head + 1;
797 sectors = p->end_sector & 63;
798 if (sectors == 0)
799 continue;
800 cylinders = nb_sectors / (heads * sectors);
801 if (cylinders < 1 || cylinders > 16383)
802 continue;
803 *pheads = heads;
804 *psectors = sectors;
805 *pcylinders = cylinders;
806#if 0
807 printf("guessed geometry: LCHS=%d %d %d\n",
808 cylinders, heads, sectors);
809#endif
810 return 0;
811 }
812 }
813 return -1;
814}
815
816void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
817{
818 int translation, lba_detected = 0;
819 int cylinders, heads, secs;
blueswir1a38131b2008-12-05 17:56:40 +0000820 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +0000821
822 /* if a geometry hint is available, use it */
823 bdrv_get_geometry(bs, &nb_sectors);
824 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
825 translation = bdrv_get_translation_hint(bs);
826 if (cylinders != 0) {
827 *pcyls = cylinders;
828 *pheads = heads;
829 *psecs = secs;
830 } else {
831 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
832 if (heads > 16) {
833 /* if heads > 16, it means that a BIOS LBA
834 translation was active, so the default
835 hardware geometry is OK */
836 lba_detected = 1;
837 goto default_geometry;
838 } else {
839 *pcyls = cylinders;
840 *pheads = heads;
841 *psecs = secs;
842 /* disable any translation to be in sync with
843 the logical geometry */
844 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
845 bdrv_set_translation_hint(bs,
846 BIOS_ATA_TRANSLATION_NONE);
847 }
848 }
849 } else {
850 default_geometry:
851 /* if no geometry, use a standard physical disk geometry */
852 cylinders = nb_sectors / (16 * 63);
853
854 if (cylinders > 16383)
855 cylinders = 16383;
856 else if (cylinders < 2)
857 cylinders = 2;
858 *pcyls = cylinders;
859 *pheads = 16;
860 *psecs = 63;
861 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
862 if ((*pcyls * *pheads) <= 131072) {
863 bdrv_set_translation_hint(bs,
864 BIOS_ATA_TRANSLATION_LARGE);
865 } else {
866 bdrv_set_translation_hint(bs,
867 BIOS_ATA_TRANSLATION_LBA);
868 }
869 }
870 }
871 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
872 }
873}
874
ths5fafdf22007-09-16 21:08:06 +0000875void bdrv_set_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000876 int cyls, int heads, int secs)
877{
878 bs->cyls = cyls;
879 bs->heads = heads;
880 bs->secs = secs;
881}
882
883void bdrv_set_type_hint(BlockDriverState *bs, int type)
884{
885 bs->type = type;
886 bs->removable = ((type == BDRV_TYPE_CDROM ||
887 type == BDRV_TYPE_FLOPPY));
888}
889
bellard46d47672004-11-16 01:45:27 +0000890void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
891{
892 bs->translation = translation;
893}
894
ths5fafdf22007-09-16 21:08:06 +0000895void bdrv_get_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000896 int *pcyls, int *pheads, int *psecs)
897{
898 *pcyls = bs->cyls;
899 *pheads = bs->heads;
900 *psecs = bs->secs;
901}
902
903int bdrv_get_type_hint(BlockDriverState *bs)
904{
905 return bs->type;
906}
907
bellard46d47672004-11-16 01:45:27 +0000908int bdrv_get_translation_hint(BlockDriverState *bs)
909{
910 return bs->translation;
911}
912
bellardb3380822004-03-14 21:38:54 +0000913int bdrv_is_removable(BlockDriverState *bs)
914{
915 return bs->removable;
916}
917
918int bdrv_is_read_only(BlockDriverState *bs)
919{
920 return bs->read_only;
921}
922
ths985a03b2007-12-24 16:10:43 +0000923int bdrv_is_sg(BlockDriverState *bs)
924{
925 return bs->sg;
926}
927
bellard19cb3732006-08-19 11:45:59 +0000928/* XXX: no longer used */
ths5fafdf22007-09-16 21:08:06 +0000929void bdrv_set_change_cb(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000930 void (*change_cb)(void *opaque), void *opaque)
931{
932 bs->change_cb = change_cb;
933 bs->change_opaque = opaque;
934}
935
bellardea2384d2004-08-01 21:59:26 +0000936int bdrv_is_encrypted(BlockDriverState *bs)
937{
938 if (bs->backing_hd && bs->backing_hd->encrypted)
939 return 1;
940 return bs->encrypted;
941}
942
aliguoric0f4ce72009-03-05 23:01:01 +0000943int bdrv_key_required(BlockDriverState *bs)
944{
945 BlockDriverState *backing_hd = bs->backing_hd;
946
947 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
948 return 1;
949 return (bs->encrypted && !bs->valid_key);
950}
951
bellardea2384d2004-08-01 21:59:26 +0000952int bdrv_set_key(BlockDriverState *bs, const char *key)
953{
954 int ret;
955 if (bs->backing_hd && bs->backing_hd->encrypted) {
956 ret = bdrv_set_key(bs->backing_hd, key);
957 if (ret < 0)
958 return ret;
959 if (!bs->encrypted)
960 return 0;
961 }
962 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
963 return -1;
aliguoric0f4ce72009-03-05 23:01:01 +0000964 ret = bs->drv->bdrv_set_key(bs, key);
aliguoribb5fc202009-03-05 23:01:15 +0000965 if (ret < 0) {
966 bs->valid_key = 0;
967 } else if (!bs->valid_key) {
968 bs->valid_key = 1;
969 /* call the change callback now, we skipped it on open */
970 bs->media_changed = 1;
971 if (bs->change_cb)
972 bs->change_cb(bs->change_opaque);
973 }
aliguoric0f4ce72009-03-05 23:01:01 +0000974 return ret;
bellardea2384d2004-08-01 21:59:26 +0000975}
976
977void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
978{
bellard19cb3732006-08-19 11:45:59 +0000979 if (!bs->drv) {
bellardea2384d2004-08-01 21:59:26 +0000980 buf[0] = '\0';
981 } else {
982 pstrcpy(buf, buf_size, bs->drv->format_name);
983 }
984}
985
ths5fafdf22007-09-16 21:08:06 +0000986void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
bellardea2384d2004-08-01 21:59:26 +0000987 void *opaque)
988{
989 BlockDriver *drv;
990
991 for (drv = first_drv; drv != NULL; drv = drv->next) {
992 it(opaque, drv->format_name);
993 }
994}
995
bellardb3380822004-03-14 21:38:54 +0000996BlockDriverState *bdrv_find(const char *name)
997{
998 BlockDriverState *bs;
999
1000 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1001 if (!strcmp(name, bs->device_name))
1002 return bs;
1003 }
1004 return NULL;
1005}
1006
aliguori51de9762009-03-05 23:00:43 +00001007void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
bellard81d09122004-07-14 17:21:37 +00001008{
1009 BlockDriverState *bs;
1010
1011 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori51de9762009-03-05 23:00:43 +00001012 it(opaque, bs);
bellard81d09122004-07-14 17:21:37 +00001013 }
1014}
1015
bellardea2384d2004-08-01 21:59:26 +00001016const char *bdrv_get_device_name(BlockDriverState *bs)
1017{
1018 return bs->device_name;
1019}
1020
pbrook7a6cba62006-06-04 11:39:07 +00001021void bdrv_flush(BlockDriverState *bs)
1022{
aliguori081501d2009-03-29 01:31:51 +00001023 if (!bs->drv)
1024 return;
pbrook7a6cba62006-06-04 11:39:07 +00001025 if (bs->drv->bdrv_flush)
1026 bs->drv->bdrv_flush(bs);
1027 if (bs->backing_hd)
1028 bdrv_flush(bs->backing_hd);
1029}
1030
aliguoric6ca28d2008-10-06 13:55:43 +00001031void bdrv_flush_all(void)
1032{
1033 BlockDriverState *bs;
1034
1035 for (bs = bdrv_first; bs != NULL; bs = bs->next)
1036 if (bs->drv && !bdrv_is_read_only(bs) &&
1037 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1038 bdrv_flush(bs);
1039}
1040
thsf58c7b32008-06-05 21:53:49 +00001041/*
1042 * Returns true iff the specified sector is present in the disk image. Drivers
1043 * not implementing the functionality are assumed to not support backing files,
1044 * hence all their sectors are reported as allocated.
1045 *
1046 * 'pnum' is set to the number of sectors (including and immediately following
1047 * the specified sector) that are known to be in the same
1048 * allocated/unallocated state.
1049 *
1050 * 'nb_sectors' is the max value 'pnum' should be set to.
1051 */
1052int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1053 int *pnum)
1054{
1055 int64_t n;
1056 if (!bs->drv->bdrv_is_allocated) {
1057 if (sector_num >= bs->total_sectors) {
1058 *pnum = 0;
1059 return 0;
1060 }
1061 n = bs->total_sectors - sector_num;
1062 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1063 return 1;
1064 }
1065 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1066}
1067
aliguori376253e2009-03-05 23:01:23 +00001068void bdrv_info(Monitor *mon)
bellardb3380822004-03-14 21:38:54 +00001069{
1070 BlockDriverState *bs;
1071
1072 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori376253e2009-03-05 23:01:23 +00001073 monitor_printf(mon, "%s:", bs->device_name);
1074 monitor_printf(mon, " type=");
bellardb3380822004-03-14 21:38:54 +00001075 switch(bs->type) {
1076 case BDRV_TYPE_HD:
aliguori376253e2009-03-05 23:01:23 +00001077 monitor_printf(mon, "hd");
bellardb3380822004-03-14 21:38:54 +00001078 break;
1079 case BDRV_TYPE_CDROM:
aliguori376253e2009-03-05 23:01:23 +00001080 monitor_printf(mon, "cdrom");
bellardb3380822004-03-14 21:38:54 +00001081 break;
1082 case BDRV_TYPE_FLOPPY:
aliguori376253e2009-03-05 23:01:23 +00001083 monitor_printf(mon, "floppy");
bellardb3380822004-03-14 21:38:54 +00001084 break;
1085 }
aliguori376253e2009-03-05 23:01:23 +00001086 monitor_printf(mon, " removable=%d", bs->removable);
bellardb3380822004-03-14 21:38:54 +00001087 if (bs->removable) {
aliguori376253e2009-03-05 23:01:23 +00001088 monitor_printf(mon, " locked=%d", bs->locked);
bellardb3380822004-03-14 21:38:54 +00001089 }
bellard19cb3732006-08-19 11:45:59 +00001090 if (bs->drv) {
aliguori376253e2009-03-05 23:01:23 +00001091 monitor_printf(mon, " file=");
1092 monitor_print_filename(mon, bs->filename);
thsfef30742006-12-22 14:11:32 +00001093 if (bs->backing_file[0] != '\0') {
aliguori376253e2009-03-05 23:01:23 +00001094 monitor_printf(mon, " backing_file=");
1095 monitor_print_filename(mon, bs->backing_file);
1096 }
1097 monitor_printf(mon, " ro=%d", bs->read_only);
1098 monitor_printf(mon, " drv=%s", bs->drv->format_name);
1099 monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs));
bellardb3380822004-03-14 21:38:54 +00001100 } else {
aliguori376253e2009-03-05 23:01:23 +00001101 monitor_printf(mon, " [not inserted]");
bellardb3380822004-03-14 21:38:54 +00001102 }
aliguori376253e2009-03-05 23:01:23 +00001103 monitor_printf(mon, "\n");
bellardb3380822004-03-14 21:38:54 +00001104 }
1105}
thsa36e69d2007-12-02 05:18:19 +00001106
1107/* The "info blockstats" command. */
aliguori376253e2009-03-05 23:01:23 +00001108void bdrv_info_stats(Monitor *mon)
thsa36e69d2007-12-02 05:18:19 +00001109{
1110 BlockDriverState *bs;
1111
1112 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori376253e2009-03-05 23:01:23 +00001113 monitor_printf(mon, "%s:"
1114 " rd_bytes=%" PRIu64
1115 " wr_bytes=%" PRIu64
1116 " rd_operations=%" PRIu64
1117 " wr_operations=%" PRIu64
aliguoriebf53fc2009-03-11 20:05:29 +00001118 "\n",
aliguori376253e2009-03-05 23:01:23 +00001119 bs->device_name,
1120 bs->rd_bytes, bs->wr_bytes,
1121 bs->rd_ops, bs->wr_ops);
thsa36e69d2007-12-02 05:18:19 +00001122 }
1123}
bellardea2384d2004-08-01 21:59:26 +00001124
aliguori045df332009-03-05 23:00:48 +00001125const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1126{
1127 if (bs->backing_hd && bs->backing_hd->encrypted)
1128 return bs->backing_file;
1129 else if (bs->encrypted)
1130 return bs->filename;
1131 else
1132 return NULL;
1133}
1134
ths5fafdf22007-09-16 21:08:06 +00001135void bdrv_get_backing_filename(BlockDriverState *bs,
bellard83f64092006-08-01 16:21:11 +00001136 char *filename, int filename_size)
bellardea2384d2004-08-01 21:59:26 +00001137{
bellard83f64092006-08-01 16:21:11 +00001138 if (!bs->backing_hd) {
1139 pstrcpy(filename, filename_size, "");
1140 } else {
1141 pstrcpy(filename, filename_size, bs->backing_file);
1142 }
bellardea2384d2004-08-01 21:59:26 +00001143}
1144
ths5fafdf22007-09-16 21:08:06 +00001145int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
bellardfaea38e2006-08-05 21:31:00 +00001146 const uint8_t *buf, int nb_sectors)
1147{
1148 BlockDriver *drv = bs->drv;
1149 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001150 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001151 if (!drv->bdrv_write_compressed)
1152 return -ENOTSUP;
1153 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1154}
ths3b46e622007-09-17 08:09:54 +00001155
bellardfaea38e2006-08-05 21:31:00 +00001156int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1157{
1158 BlockDriver *drv = bs->drv;
1159 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001160 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001161 if (!drv->bdrv_get_info)
1162 return -ENOTSUP;
1163 memset(bdi, 0, sizeof(*bdi));
1164 return drv->bdrv_get_info(bs, bdi);
1165}
1166
aliguori178e08a2009-04-05 19:10:55 +00001167int bdrv_put_buffer(BlockDriverState *bs, const uint8_t *buf, int64_t pos, int size)
1168{
1169 BlockDriver *drv = bs->drv;
1170 if (!drv)
1171 return -ENOMEDIUM;
1172 if (!drv->bdrv_put_buffer)
1173 return -ENOTSUP;
1174 return drv->bdrv_put_buffer(bs, buf, pos, size);
1175}
1176
1177int bdrv_get_buffer(BlockDriverState *bs, uint8_t *buf, int64_t pos, int size)
1178{
1179 BlockDriver *drv = bs->drv;
1180 if (!drv)
1181 return -ENOMEDIUM;
1182 if (!drv->bdrv_get_buffer)
1183 return -ENOTSUP;
1184 return drv->bdrv_get_buffer(bs, buf, pos, size);
1185}
1186
bellardfaea38e2006-08-05 21:31:00 +00001187/**************************************************************/
1188/* handling of snapshots */
1189
ths5fafdf22007-09-16 21:08:06 +00001190int bdrv_snapshot_create(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001191 QEMUSnapshotInfo *sn_info)
1192{
1193 BlockDriver *drv = bs->drv;
1194 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001195 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001196 if (!drv->bdrv_snapshot_create)
1197 return -ENOTSUP;
1198 return drv->bdrv_snapshot_create(bs, sn_info);
1199}
1200
ths5fafdf22007-09-16 21:08:06 +00001201int bdrv_snapshot_goto(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001202 const char *snapshot_id)
1203{
1204 BlockDriver *drv = bs->drv;
1205 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001206 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001207 if (!drv->bdrv_snapshot_goto)
1208 return -ENOTSUP;
1209 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1210}
1211
1212int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1213{
1214 BlockDriver *drv = bs->drv;
1215 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001216 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001217 if (!drv->bdrv_snapshot_delete)
1218 return -ENOTSUP;
1219 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1220}
1221
ths5fafdf22007-09-16 21:08:06 +00001222int bdrv_snapshot_list(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001223 QEMUSnapshotInfo **psn_info)
1224{
1225 BlockDriver *drv = bs->drv;
1226 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001227 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001228 if (!drv->bdrv_snapshot_list)
1229 return -ENOTSUP;
1230 return drv->bdrv_snapshot_list(bs, psn_info);
1231}
1232
1233#define NB_SUFFIXES 4
1234
1235char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1236{
1237 static const char suffixes[NB_SUFFIXES] = "KMGT";
1238 int64_t base;
1239 int i;
1240
1241 if (size <= 999) {
1242 snprintf(buf, buf_size, "%" PRId64, size);
1243 } else {
1244 base = 1024;
1245 for(i = 0; i < NB_SUFFIXES; i++) {
1246 if (size < (10 * base)) {
ths5fafdf22007-09-16 21:08:06 +00001247 snprintf(buf, buf_size, "%0.1f%c",
bellardfaea38e2006-08-05 21:31:00 +00001248 (double)size / base,
1249 suffixes[i]);
1250 break;
1251 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
ths5fafdf22007-09-16 21:08:06 +00001252 snprintf(buf, buf_size, "%" PRId64 "%c",
bellardfaea38e2006-08-05 21:31:00 +00001253 ((size + (base >> 1)) / base),
1254 suffixes[i]);
1255 break;
1256 }
1257 base = base * 1024;
1258 }
1259 }
1260 return buf;
1261}
1262
1263char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1264{
1265 char buf1[128], date_buf[128], clock_buf[128];
bellard3b9f94e2007-01-07 17:27:07 +00001266#ifdef _WIN32
1267 struct tm *ptm;
1268#else
bellardfaea38e2006-08-05 21:31:00 +00001269 struct tm tm;
bellard3b9f94e2007-01-07 17:27:07 +00001270#endif
bellardfaea38e2006-08-05 21:31:00 +00001271 time_t ti;
1272 int64_t secs;
1273
1274 if (!sn) {
ths5fafdf22007-09-16 21:08:06 +00001275 snprintf(buf, buf_size,
1276 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001277 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1278 } else {
1279 ti = sn->date_sec;
bellard3b9f94e2007-01-07 17:27:07 +00001280#ifdef _WIN32
1281 ptm = localtime(&ti);
1282 strftime(date_buf, sizeof(date_buf),
1283 "%Y-%m-%d %H:%M:%S", ptm);
1284#else
bellardfaea38e2006-08-05 21:31:00 +00001285 localtime_r(&ti, &tm);
1286 strftime(date_buf, sizeof(date_buf),
1287 "%Y-%m-%d %H:%M:%S", &tm);
bellard3b9f94e2007-01-07 17:27:07 +00001288#endif
bellardfaea38e2006-08-05 21:31:00 +00001289 secs = sn->vm_clock_nsec / 1000000000;
1290 snprintf(clock_buf, sizeof(clock_buf),
1291 "%02d:%02d:%02d.%03d",
1292 (int)(secs / 3600),
1293 (int)((secs / 60) % 60),
ths5fafdf22007-09-16 21:08:06 +00001294 (int)(secs % 60),
bellardfaea38e2006-08-05 21:31:00 +00001295 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1296 snprintf(buf, buf_size,
ths5fafdf22007-09-16 21:08:06 +00001297 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001298 sn->id_str, sn->name,
1299 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1300 date_buf,
1301 clock_buf);
1302 }
1303 return buf;
1304}
1305
bellardea2384d2004-08-01 21:59:26 +00001306
bellard83f64092006-08-01 16:21:11 +00001307/**************************************************************/
1308/* async I/Os */
1309
aliguori3b69e4b2009-01-22 16:59:24 +00001310BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
aliguorif141eaf2009-04-07 18:43:24 +00001311 QEMUIOVector *qiov, int nb_sectors,
aliguori3b69e4b2009-01-22 16:59:24 +00001312 BlockDriverCompletionFunc *cb, void *opaque)
1313{
bellard83f64092006-08-01 16:21:11 +00001314 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001315 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001316
bellard19cb3732006-08-19 11:45:59 +00001317 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001318 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001319 if (bdrv_check_request(bs, sector_num, nb_sectors))
1320 return NULL;
ths3b46e622007-09-17 08:09:54 +00001321
aliguorif141eaf2009-04-07 18:43:24 +00001322 ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
1323 cb, opaque);
thsa36e69d2007-12-02 05:18:19 +00001324
1325 if (ret) {
1326 /* Update stats even though technically transfer has not happened. */
1327 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1328 bs->rd_ops ++;
1329 }
1330
1331 return ret;
bellard83f64092006-08-01 16:21:11 +00001332}
1333
aliguorif141eaf2009-04-07 18:43:24 +00001334BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1335 QEMUIOVector *qiov, int nb_sectors,
1336 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001337{
bellard83f64092006-08-01 16:21:11 +00001338 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001339 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001340
bellard19cb3732006-08-19 11:45:59 +00001341 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001342 return NULL;
bellard83f64092006-08-01 16:21:11 +00001343 if (bs->read_only)
pbrookce1a14d2006-08-07 02:38:06 +00001344 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001345 if (bdrv_check_request(bs, sector_num, nb_sectors))
1346 return NULL;
bellard83f64092006-08-01 16:21:11 +00001347
aliguorif141eaf2009-04-07 18:43:24 +00001348 ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
1349 cb, opaque);
thsa36e69d2007-12-02 05:18:19 +00001350
1351 if (ret) {
1352 /* Update stats even though technically transfer has not happened. */
1353 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1354 bs->wr_ops ++;
1355 }
1356
1357 return ret;
bellard83f64092006-08-01 16:21:11 +00001358}
1359
1360void bdrv_aio_cancel(BlockDriverAIOCB *acb)
pbrookce1a14d2006-08-07 02:38:06 +00001361{
aliguori6bbff9a2009-03-20 18:25:59 +00001362 acb->pool->cancel(acb);
bellard83f64092006-08-01 16:21:11 +00001363}
1364
pbrookce1a14d2006-08-07 02:38:06 +00001365
bellard83f64092006-08-01 16:21:11 +00001366/**************************************************************/
1367/* async block device emulation */
1368
bellard83f64092006-08-01 16:21:11 +00001369static void bdrv_aio_bh_cb(void *opaque)
bellardbeac80c2006-06-26 20:08:57 +00001370{
pbrookce1a14d2006-08-07 02:38:06 +00001371 BlockDriverAIOCBSync *acb = opaque;
aliguorif141eaf2009-04-07 18:43:24 +00001372
aliguorif141eaf2009-04-07 18:43:24 +00001373 if (!acb->is_write)
1374 qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
aliguoriceb42de2009-04-07 18:43:28 +00001375 qemu_vfree(acb->bounce);
pbrookce1a14d2006-08-07 02:38:06 +00001376 acb->common.cb(acb->common.opaque, acb->ret);
aliguorif141eaf2009-04-07 18:43:24 +00001377
pbrookce1a14d2006-08-07 02:38:06 +00001378 qemu_aio_release(acb);
bellardbeac80c2006-06-26 20:08:57 +00001379}
bellardbeac80c2006-06-26 20:08:57 +00001380
aliguorif141eaf2009-04-07 18:43:24 +00001381static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
1382 int64_t sector_num,
1383 QEMUIOVector *qiov,
1384 int nb_sectors,
1385 BlockDriverCompletionFunc *cb,
1386 void *opaque,
1387 int is_write)
1388
bellardea2384d2004-08-01 21:59:26 +00001389{
pbrookce1a14d2006-08-07 02:38:06 +00001390 BlockDriverAIOCBSync *acb;
pbrookce1a14d2006-08-07 02:38:06 +00001391
1392 acb = qemu_aio_get(bs, cb, opaque);
aliguorif141eaf2009-04-07 18:43:24 +00001393 acb->is_write = is_write;
1394 acb->qiov = qiov;
aliguorie268ca52009-04-22 20:20:00 +00001395 acb->bounce = qemu_blockalign(bs, qiov->size);
aliguorif141eaf2009-04-07 18:43:24 +00001396
pbrookce1a14d2006-08-07 02:38:06 +00001397 if (!acb->bh)
1398 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
aliguorif141eaf2009-04-07 18:43:24 +00001399
1400 if (is_write) {
1401 qemu_iovec_to_buffer(acb->qiov, acb->bounce);
1402 acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
1403 } else {
1404 acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
1405 }
1406
pbrookce1a14d2006-08-07 02:38:06 +00001407 qemu_bh_schedule(acb->bh);
aliguorif141eaf2009-04-07 18:43:24 +00001408
pbrookce1a14d2006-08-07 02:38:06 +00001409 return &acb->common;
pbrook7a6cba62006-06-04 11:39:07 +00001410}
1411
aliguorif141eaf2009-04-07 18:43:24 +00001412static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
1413 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
pbrookce1a14d2006-08-07 02:38:06 +00001414 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001415{
aliguorif141eaf2009-04-07 18:43:24 +00001416 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
bellard83f64092006-08-01 16:21:11 +00001417}
1418
aliguorif141eaf2009-04-07 18:43:24 +00001419static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
1420 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
1421 BlockDriverCompletionFunc *cb, void *opaque)
1422{
1423 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
1424}
1425
1426
pbrookce1a14d2006-08-07 02:38:06 +00001427static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
bellard83f64092006-08-01 16:21:11 +00001428{
pbrookce1a14d2006-08-07 02:38:06 +00001429 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1430 qemu_bh_cancel(acb->bh);
1431 qemu_aio_release(acb);
bellard83f64092006-08-01 16:21:11 +00001432}
bellard83f64092006-08-01 16:21:11 +00001433
1434/**************************************************************/
1435/* sync block device emulation */
1436
1437static void bdrv_rw_em_cb(void *opaque, int ret)
1438{
1439 *(int *)opaque = ret;
1440}
1441
1442#define NOT_DONE 0x7fffffff
1443
ths5fafdf22007-09-16 21:08:06 +00001444static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +00001445 uint8_t *buf, int nb_sectors)
1446{
pbrookce1a14d2006-08-07 02:38:06 +00001447 int async_ret;
1448 BlockDriverAIOCB *acb;
aliguorif141eaf2009-04-07 18:43:24 +00001449 struct iovec iov;
1450 QEMUIOVector qiov;
bellard83f64092006-08-01 16:21:11 +00001451
bellard83f64092006-08-01 16:21:11 +00001452 async_ret = NOT_DONE;
blueswir13f4cb3d2009-04-13 16:31:01 +00001453 iov.iov_base = (void *)buf;
aliguorif141eaf2009-04-07 18:43:24 +00001454 iov.iov_len = nb_sectors * 512;
1455 qemu_iovec_init_external(&qiov, &iov, 1);
1456 acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
1457 bdrv_rw_em_cb, &async_ret);
aliguoribaf35cb2008-09-10 15:45:19 +00001458 if (acb == NULL)
pbrookce1a14d2006-08-07 02:38:06 +00001459 return -1;
aliguoribaf35cb2008-09-10 15:45:19 +00001460
bellard83f64092006-08-01 16:21:11 +00001461 while (async_ret == NOT_DONE) {
1462 qemu_aio_wait();
1463 }
aliguoribaf35cb2008-09-10 15:45:19 +00001464
bellard83f64092006-08-01 16:21:11 +00001465 return async_ret;
1466}
1467
1468static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1469 const uint8_t *buf, int nb_sectors)
1470{
pbrookce1a14d2006-08-07 02:38:06 +00001471 int async_ret;
1472 BlockDriverAIOCB *acb;
aliguorif141eaf2009-04-07 18:43:24 +00001473 struct iovec iov;
1474 QEMUIOVector qiov;
bellard83f64092006-08-01 16:21:11 +00001475
bellard83f64092006-08-01 16:21:11 +00001476 async_ret = NOT_DONE;
aliguorif141eaf2009-04-07 18:43:24 +00001477 iov.iov_base = (void *)buf;
1478 iov.iov_len = nb_sectors * 512;
1479 qemu_iovec_init_external(&qiov, &iov, 1);
1480 acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
1481 bdrv_rw_em_cb, &async_ret);
aliguoribaf35cb2008-09-10 15:45:19 +00001482 if (acb == NULL)
pbrookce1a14d2006-08-07 02:38:06 +00001483 return -1;
bellard83f64092006-08-01 16:21:11 +00001484 while (async_ret == NOT_DONE) {
1485 qemu_aio_wait();
1486 }
bellard83f64092006-08-01 16:21:11 +00001487 return async_ret;
1488}
bellardea2384d2004-08-01 21:59:26 +00001489
1490void bdrv_init(void)
1491{
1492 bdrv_register(&bdrv_raw);
bellard19cb3732006-08-19 11:45:59 +00001493 bdrv_register(&bdrv_host_device);
bellardea2384d2004-08-01 21:59:26 +00001494#ifndef _WIN32
1495 bdrv_register(&bdrv_cow);
1496#endif
1497 bdrv_register(&bdrv_qcow);
1498 bdrv_register(&bdrv_vmdk);
bellard3c565212004-09-29 21:29:14 +00001499 bdrv_register(&bdrv_cloop);
bellard585d0ed2004-12-12 11:24:44 +00001500 bdrv_register(&bdrv_dmg);
bellarda8753c32005-04-26 21:34:00 +00001501 bdrv_register(&bdrv_bochs);
bellard6a0f9e82005-04-27 20:17:58 +00001502 bdrv_register(&bdrv_vpc);
bellard712e7872005-04-28 21:09:32 +00001503 bdrv_register(&bdrv_vvfat);
bellardfaea38e2006-08-05 21:31:00 +00001504 bdrv_register(&bdrv_qcow2);
ths6ada7452007-07-31 23:28:53 +00001505 bdrv_register(&bdrv_parallels);
ths75818252008-07-03 13:41:03 +00001506 bdrv_register(&bdrv_nbd);
bellardea2384d2004-08-01 21:59:26 +00001507}
pbrookce1a14d2006-08-07 02:38:06 +00001508
aliguori6bbff9a2009-03-20 18:25:59 +00001509void aio_pool_init(AIOPool *pool, int aiocb_size,
1510 void (*cancel)(BlockDriverAIOCB *acb))
pbrookce1a14d2006-08-07 02:38:06 +00001511{
aliguori6bbff9a2009-03-20 18:25:59 +00001512 pool->aiocb_size = aiocb_size;
1513 pool->cancel = cancel;
1514 pool->free_aiocb = NULL;
1515}
1516
1517void *qemu_aio_get_pool(AIOPool *pool, BlockDriverState *bs,
1518 BlockDriverCompletionFunc *cb, void *opaque)
1519{
pbrookce1a14d2006-08-07 02:38:06 +00001520 BlockDriverAIOCB *acb;
1521
aliguori6bbff9a2009-03-20 18:25:59 +00001522 if (pool->free_aiocb) {
1523 acb = pool->free_aiocb;
1524 pool->free_aiocb = acb->next;
pbrookce1a14d2006-08-07 02:38:06 +00001525 } else {
aliguori6bbff9a2009-03-20 18:25:59 +00001526 acb = qemu_mallocz(pool->aiocb_size);
1527 acb->pool = pool;
pbrookce1a14d2006-08-07 02:38:06 +00001528 }
1529 acb->bs = bs;
1530 acb->cb = cb;
1531 acb->opaque = opaque;
1532 return acb;
1533}
1534
aliguori6bbff9a2009-03-20 18:25:59 +00001535void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1536 void *opaque)
1537{
1538 return qemu_aio_get_pool(&bs->drv->aio_pool, bs, cb, opaque);
1539}
1540
pbrookce1a14d2006-08-07 02:38:06 +00001541void qemu_aio_release(void *p)
1542{
aliguori6bbff9a2009-03-20 18:25:59 +00001543 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
1544 AIOPool *pool = acb->pool;
1545 acb->next = pool->free_aiocb;
1546 pool->free_aiocb = acb;
pbrookce1a14d2006-08-07 02:38:06 +00001547}
bellard19cb3732006-08-19 11:45:59 +00001548
1549/**************************************************************/
1550/* removable device support */
1551
1552/**
1553 * Return TRUE if the media is present
1554 */
1555int bdrv_is_inserted(BlockDriverState *bs)
1556{
1557 BlockDriver *drv = bs->drv;
1558 int ret;
1559 if (!drv)
1560 return 0;
1561 if (!drv->bdrv_is_inserted)
1562 return 1;
1563 ret = drv->bdrv_is_inserted(bs);
1564 return ret;
1565}
1566
1567/**
1568 * Return TRUE if the media changed since the last call to this
ths5fafdf22007-09-16 21:08:06 +00001569 * function. It is currently only used for floppy disks
bellard19cb3732006-08-19 11:45:59 +00001570 */
1571int bdrv_media_changed(BlockDriverState *bs)
1572{
1573 BlockDriver *drv = bs->drv;
1574 int ret;
1575
1576 if (!drv || !drv->bdrv_media_changed)
1577 ret = -ENOTSUP;
1578 else
1579 ret = drv->bdrv_media_changed(bs);
1580 if (ret == -ENOTSUP)
1581 ret = bs->media_changed;
1582 bs->media_changed = 0;
1583 return ret;
1584}
1585
1586/**
1587 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1588 */
1589void bdrv_eject(BlockDriverState *bs, int eject_flag)
1590{
1591 BlockDriver *drv = bs->drv;
1592 int ret;
1593
1594 if (!drv || !drv->bdrv_eject) {
1595 ret = -ENOTSUP;
1596 } else {
1597 ret = drv->bdrv_eject(bs, eject_flag);
1598 }
1599 if (ret == -ENOTSUP) {
1600 if (eject_flag)
1601 bdrv_close(bs);
1602 }
1603}
1604
1605int bdrv_is_locked(BlockDriverState *bs)
1606{
1607 return bs->locked;
1608}
1609
1610/**
1611 * Lock or unlock the media (if it is locked, the user won't be able
1612 * to eject it manually).
1613 */
1614void bdrv_set_locked(BlockDriverState *bs, int locked)
1615{
1616 BlockDriver *drv = bs->drv;
1617
1618 bs->locked = locked;
1619 if (drv && drv->bdrv_set_locked) {
1620 drv->bdrv_set_locked(bs, locked);
1621 }
1622}
ths985a03b2007-12-24 16:10:43 +00001623
1624/* needed for generic scsi interface */
1625
1626int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1627{
1628 BlockDriver *drv = bs->drv;
1629
1630 if (drv && drv->bdrv_ioctl)
1631 return drv->bdrv_ioctl(bs, req, buf);
1632 return -ENOTSUP;
1633}
aliguori7d780662009-03-12 19:57:08 +00001634
aliguori221f7152009-03-28 17:28:41 +00001635BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
1636 unsigned long int req, void *buf,
1637 BlockDriverCompletionFunc *cb, void *opaque)
aliguori7d780662009-03-12 19:57:08 +00001638{
aliguori221f7152009-03-28 17:28:41 +00001639 BlockDriver *drv = bs->drv;
aliguori7d780662009-03-12 19:57:08 +00001640
aliguori221f7152009-03-28 17:28:41 +00001641 if (drv && drv->bdrv_aio_ioctl)
1642 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
1643 return NULL;
aliguori7d780662009-03-12 19:57:08 +00001644}
aliguorie268ca52009-04-22 20:20:00 +00001645
1646void *qemu_blockalign(BlockDriverState *bs, size_t size)
1647{
1648 return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
1649}