blob: 916f08e76fdb8c70a21df6ad63b081a2b23fd2a8 [file] [log] [blame]
bellardfc01f7e2003-06-30 10:03:06 +00001/*
2 * QEMU System Emulator block driver
ths5fafdf22007-09-16 21:08:06 +00003 *
bellardfc01f7e2003-06-30 10:03:06 +00004 * Copyright (c) 2003 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellardfc01f7e2003-06-30 10:03:06 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
blueswir13990d092008-12-05 17:53:21 +000024#include "config-host.h"
blueswir1179a2c12009-03-08 08:23:32 +000025#ifdef HOST_BSD
blueswir13990d092008-12-05 17:53:21 +000026/* include native header before sys-queue.h */
27#include <sys/queue.h>
28#endif
29
pbrookfaf07962007-11-11 02:51:17 +000030#include "qemu-common.h"
aliguori376253e2009-03-05 23:01:23 +000031#include "monitor.h"
bellardea2384d2004-08-01 21:59:26 +000032#include "block_int.h"
bellardfc01f7e2003-06-30 10:03:06 +000033
blueswir1179a2c12009-03-08 08:23:32 +000034#ifdef HOST_BSD
bellard7674e7b2005-04-26 21:59:26 +000035#include <sys/types.h>
36#include <sys/stat.h>
37#include <sys/ioctl.h>
blueswir1c5e97232009-03-07 20:06:23 +000038#ifndef __DragonFly__
bellard7674e7b2005-04-26 21:59:26 +000039#include <sys/disk.h>
40#endif
blueswir1c5e97232009-03-07 20:06:23 +000041#endif
bellard7674e7b2005-04-26 21:59:26 +000042
aliguori49dc7682009-03-08 16:26:59 +000043#ifdef _WIN32
44#include <windows.h>
45#endif
46
bellard83f64092006-08-01 16:21:11 +000047#define SECTOR_BITS 9
48#define SECTOR_SIZE (1 << SECTOR_BITS)
bellard3b0d4f62005-10-30 18:30:10 +000049
aliguoric07a9002009-03-20 18:26:03 +000050static AIOPool vectored_aio_pool;
51
bellard90765422006-08-07 19:10:16 +000052typedef struct BlockDriverAIOCBSync {
53 BlockDriverAIOCB common;
54 QEMUBH *bh;
55 int ret;
56} BlockDriverAIOCBSync;
57
aliguoric87c0672009-04-07 18:43:20 +000058static BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs,
59 int64_t sector_num, uint8_t *buf, int nb_sectors,
60 BlockDriverCompletionFunc *cb, void *opaque);
61static BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs,
62 int64_t sector_num, const uint8_t *buf, int nb_sectors,
63 BlockDriverCompletionFunc *cb, void *opaque);
pbrookce1a14d2006-08-07 02:38:06 +000064static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
65 int64_t sector_num, uint8_t *buf, int nb_sectors,
66 BlockDriverCompletionFunc *cb, void *opaque);
67static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
68 int64_t sector_num, const uint8_t *buf, int nb_sectors,
69 BlockDriverCompletionFunc *cb, void *opaque);
bellard83f64092006-08-01 16:21:11 +000070static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
ths5fafdf22007-09-16 21:08:06 +000071static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +000072 uint8_t *buf, int nb_sectors);
73static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
74 const uint8_t *buf, int nb_sectors);
bellardec530c82006-04-25 22:36:06 +000075
blueswir17ee930d2008-09-17 19:04:14 +000076BlockDriverState *bdrv_first;
77
bellardea2384d2004-08-01 21:59:26 +000078static BlockDriver *first_drv;
79
bellard83f64092006-08-01 16:21:11 +000080int path_is_absolute(const char *path)
81{
82 const char *p;
bellard21664422007-01-07 18:22:37 +000083#ifdef _WIN32
84 /* specific case for names like: "\\.\d:" */
85 if (*path == '/' || *path == '\\')
86 return 1;
87#endif
bellard83f64092006-08-01 16:21:11 +000088 p = strchr(path, ':');
89 if (p)
90 p++;
91 else
92 p = path;
bellard3b9f94e2007-01-07 17:27:07 +000093#ifdef _WIN32
94 return (*p == '/' || *p == '\\');
95#else
96 return (*p == '/');
97#endif
bellard83f64092006-08-01 16:21:11 +000098}
99
100/* if filename is absolute, just copy it to dest. Otherwise, build a
101 path to it by considering it is relative to base_path. URL are
102 supported. */
103void path_combine(char *dest, int dest_size,
104 const char *base_path,
105 const char *filename)
106{
107 const char *p, *p1;
108 int len;
109
110 if (dest_size <= 0)
111 return;
112 if (path_is_absolute(filename)) {
113 pstrcpy(dest, dest_size, filename);
114 } else {
115 p = strchr(base_path, ':');
116 if (p)
117 p++;
118 else
119 p = base_path;
bellard3b9f94e2007-01-07 17:27:07 +0000120 p1 = strrchr(base_path, '/');
121#ifdef _WIN32
122 {
123 const char *p2;
124 p2 = strrchr(base_path, '\\');
125 if (!p1 || p2 > p1)
126 p1 = p2;
127 }
128#endif
bellard83f64092006-08-01 16:21:11 +0000129 if (p1)
130 p1++;
131 else
132 p1 = base_path;
133 if (p1 > p)
134 p = p1;
135 len = p - base_path;
136 if (len > dest_size - 1)
137 len = dest_size - 1;
138 memcpy(dest, base_path, len);
139 dest[len] = '\0';
140 pstrcat(dest, dest_size, filename);
141 }
142}
143
144
pbrook9596ebb2007-11-18 01:44:38 +0000145static void bdrv_register(BlockDriver *bdrv)
bellardea2384d2004-08-01 21:59:26 +0000146{
pbrookce1a14d2006-08-07 02:38:06 +0000147 if (!bdrv->bdrv_aio_read) {
bellard83f64092006-08-01 16:21:11 +0000148 /* add AIO emulation layer */
bellard83f64092006-08-01 16:21:11 +0000149 bdrv->bdrv_aio_read = bdrv_aio_read_em;
150 bdrv->bdrv_aio_write = bdrv_aio_write_em;
151 bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
bellard90765422006-08-07 19:10:16 +0000152 bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
aliguorieda578e2009-03-12 19:57:16 +0000153 } else if (!bdrv->bdrv_read) {
bellard83f64092006-08-01 16:21:11 +0000154 /* add synchronous IO emulation layer */
155 bdrv->bdrv_read = bdrv_read_em;
156 bdrv->bdrv_write = bdrv_write_em;
157 }
aliguori6bbff9a2009-03-20 18:25:59 +0000158 aio_pool_init(&bdrv->aio_pool, bdrv->aiocb_size, bdrv->bdrv_aio_cancel);
bellardea2384d2004-08-01 21:59:26 +0000159 bdrv->next = first_drv;
160 first_drv = bdrv;
161}
bellardb3380822004-03-14 21:38:54 +0000162
163/* create a new block device (by default it is empty) */
164BlockDriverState *bdrv_new(const char *device_name)
bellardfc01f7e2003-06-30 10:03:06 +0000165{
bellardb3380822004-03-14 21:38:54 +0000166 BlockDriverState **pbs, *bs;
167
168 bs = qemu_mallocz(sizeof(BlockDriverState));
bellardb3380822004-03-14 21:38:54 +0000169 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
bellardea2384d2004-08-01 21:59:26 +0000170 if (device_name[0] != '\0') {
171 /* insert at the end */
172 pbs = &bdrv_first;
173 while (*pbs != NULL)
174 pbs = &(*pbs)->next;
175 *pbs = bs;
176 }
bellardb3380822004-03-14 21:38:54 +0000177 return bs;
178}
179
bellardea2384d2004-08-01 21:59:26 +0000180BlockDriver *bdrv_find_format(const char *format_name)
181{
182 BlockDriver *drv1;
183 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
184 if (!strcmp(drv1->format_name, format_name))
185 return drv1;
186 }
187 return NULL;
188}
189
aliguori5eb45632009-03-28 17:55:10 +0000190int bdrv_create2(BlockDriver *drv,
191 const char *filename, int64_t size_in_sectors,
192 const char *backing_file, const char *backing_format,
193 int flags)
194{
195 if (drv->bdrv_create2)
196 return drv->bdrv_create2(filename, size_in_sectors, backing_file,
197 backing_format, flags);
198 if (drv->bdrv_create)
199 return drv->bdrv_create(filename, size_in_sectors, backing_file,
200 flags);
201 return -ENOTSUP;
202}
203
ths5fafdf22007-09-16 21:08:06 +0000204int bdrv_create(BlockDriver *drv,
bellardea2384d2004-08-01 21:59:26 +0000205 const char *filename, int64_t size_in_sectors,
206 const char *backing_file, int flags)
207{
208 if (!drv->bdrv_create)
209 return -ENOTSUP;
210 return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
211}
212
bellardd5249392004-08-03 21:14:23 +0000213#ifdef _WIN32
bellard95389c82005-12-18 18:28:15 +0000214void get_tmp_filename(char *filename, int size)
bellardd5249392004-08-03 21:14:23 +0000215{
bellard3b9f94e2007-01-07 17:27:07 +0000216 char temp_dir[MAX_PATH];
ths3b46e622007-09-17 08:09:54 +0000217
bellard3b9f94e2007-01-07 17:27:07 +0000218 GetTempPath(MAX_PATH, temp_dir);
219 GetTempFileName(temp_dir, "qem", 0, filename);
bellardd5249392004-08-03 21:14:23 +0000220}
221#else
bellard95389c82005-12-18 18:28:15 +0000222void get_tmp_filename(char *filename, int size)
bellardea2384d2004-08-01 21:59:26 +0000223{
224 int fd;
blueswir17ccfb2e2008-09-14 06:45:34 +0000225 const char *tmpdir;
bellardd5249392004-08-03 21:14:23 +0000226 /* XXX: race condition possible */
aurel320badc1e2008-03-10 00:05:34 +0000227 tmpdir = getenv("TMPDIR");
228 if (!tmpdir)
229 tmpdir = "/tmp";
230 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
bellardea2384d2004-08-01 21:59:26 +0000231 fd = mkstemp(filename);
232 close(fd);
233}
bellardd5249392004-08-03 21:14:23 +0000234#endif
bellardea2384d2004-08-01 21:59:26 +0000235
bellard19cb3732006-08-19 11:45:59 +0000236#ifdef _WIN32
bellardf45512f2006-08-23 21:40:13 +0000237static int is_windows_drive_prefix(const char *filename)
238{
239 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
240 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
241 filename[1] == ':');
242}
ths3b46e622007-09-17 08:09:54 +0000243
bellard19cb3732006-08-19 11:45:59 +0000244static int is_windows_drive(const char *filename)
245{
ths5fafdf22007-09-16 21:08:06 +0000246 if (is_windows_drive_prefix(filename) &&
bellardf45512f2006-08-23 21:40:13 +0000247 filename[2] == '\0')
bellard19cb3732006-08-19 11:45:59 +0000248 return 1;
249 if (strstart(filename, "\\\\.\\", NULL) ||
250 strstart(filename, "//./", NULL))
251 return 1;
252 return 0;
253}
254#endif
255
bellard83f64092006-08-01 16:21:11 +0000256static BlockDriver *find_protocol(const char *filename)
257{
258 BlockDriver *drv1;
259 char protocol[128];
260 int len;
261 const char *p;
bellard19cb3732006-08-19 11:45:59 +0000262
263#ifdef _WIN32
bellardf45512f2006-08-23 21:40:13 +0000264 if (is_windows_drive(filename) ||
265 is_windows_drive_prefix(filename))
bellard19cb3732006-08-19 11:45:59 +0000266 return &bdrv_raw;
267#endif
bellard83f64092006-08-01 16:21:11 +0000268 p = strchr(filename, ':');
269 if (!p)
270 return &bdrv_raw;
271 len = p - filename;
272 if (len > sizeof(protocol) - 1)
273 len = sizeof(protocol) - 1;
bellard83f64092006-08-01 16:21:11 +0000274 memcpy(protocol, filename, len);
275 protocol[len] = '\0';
276 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
ths5fafdf22007-09-16 21:08:06 +0000277 if (drv1->protocol_name &&
bellard83f64092006-08-01 16:21:11 +0000278 !strcmp(drv1->protocol_name, protocol))
279 return drv1;
280 }
281 return NULL;
282}
283
bellard7674e7b2005-04-26 21:59:26 +0000284/* XXX: force raw format if block or character device ? It would
285 simplify the BSD case */
bellardea2384d2004-08-01 21:59:26 +0000286static BlockDriver *find_image_format(const char *filename)
287{
bellard83f64092006-08-01 16:21:11 +0000288 int ret, score, score_max;
bellardea2384d2004-08-01 21:59:26 +0000289 BlockDriver *drv1, *drv;
bellard83f64092006-08-01 16:21:11 +0000290 uint8_t buf[2048];
291 BlockDriverState *bs;
ths3b46e622007-09-17 08:09:54 +0000292
bellard19cb3732006-08-19 11:45:59 +0000293 /* detect host devices. By convention, /dev/cdrom[N] is always
294 recognized as a host CDROM */
295 if (strstart(filename, "/dev/cdrom", NULL))
296 return &bdrv_host_device;
297#ifdef _WIN32
298 if (is_windows_drive(filename))
299 return &bdrv_host_device;
300#else
301 {
302 struct stat st;
ths5fafdf22007-09-16 21:08:06 +0000303 if (stat(filename, &st) >= 0 &&
bellard19cb3732006-08-19 11:45:59 +0000304 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
305 return &bdrv_host_device;
306 }
307 }
308#endif
ths3b46e622007-09-17 08:09:54 +0000309
bellard83f64092006-08-01 16:21:11 +0000310 drv = find_protocol(filename);
bellard19cb3732006-08-19 11:45:59 +0000311 /* no need to test disk image formats for vvfat */
bellard83f64092006-08-01 16:21:11 +0000312 if (drv == &bdrv_vvfat)
313 return drv;
bellard19cb3732006-08-19 11:45:59 +0000314
bellard83f64092006-08-01 16:21:11 +0000315 ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
316 if (ret < 0)
317 return NULL;
318 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
319 bdrv_delete(bs);
320 if (ret < 0) {
321 return NULL;
322 }
323
bellardea2384d2004-08-01 21:59:26 +0000324 score_max = 0;
325 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
bellard83f64092006-08-01 16:21:11 +0000326 if (drv1->bdrv_probe) {
327 score = drv1->bdrv_probe(buf, ret, filename);
328 if (score > score_max) {
329 score_max = score;
330 drv = drv1;
331 }
bellardea2384d2004-08-01 21:59:26 +0000332 }
333 }
334 return drv;
335}
336
bellard83f64092006-08-01 16:21:11 +0000337int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
bellardb3380822004-03-14 21:38:54 +0000338{
bellard83f64092006-08-01 16:21:11 +0000339 BlockDriverState *bs;
340 int ret;
341
342 bs = bdrv_new("");
bellard83f64092006-08-01 16:21:11 +0000343 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
344 if (ret < 0) {
345 bdrv_delete(bs);
346 return ret;
bellard3b0d4f62005-10-30 18:30:10 +0000347 }
aliguori71d07702009-03-03 17:37:16 +0000348 bs->growable = 1;
bellard83f64092006-08-01 16:21:11 +0000349 *pbs = bs;
350 return 0;
bellardea2384d2004-08-01 21:59:26 +0000351}
bellardfc01f7e2003-06-30 10:03:06 +0000352
bellard83f64092006-08-01 16:21:11 +0000353int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
354{
355 return bdrv_open2(bs, filename, flags, NULL);
356}
357
358int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
bellardea2384d2004-08-01 21:59:26 +0000359 BlockDriver *drv)
360{
bellard83f64092006-08-01 16:21:11 +0000361 int ret, open_flags;
thseb5c8512007-02-11 15:06:09 +0000362 char tmp_filename[PATH_MAX];
363 char backing_filename[PATH_MAX];
ths3b46e622007-09-17 08:09:54 +0000364
bellardea2384d2004-08-01 21:59:26 +0000365 bs->read_only = 0;
366 bs->is_temporary = 0;
367 bs->encrypted = 0;
aliguoric0f4ce72009-03-05 23:01:01 +0000368 bs->valid_key = 0;
bellard712e7872005-04-28 21:09:32 +0000369
bellard83f64092006-08-01 16:21:11 +0000370 if (flags & BDRV_O_SNAPSHOT) {
bellardea2384d2004-08-01 21:59:26 +0000371 BlockDriverState *bs1;
372 int64_t total_size;
aliguori7c96d462008-09-12 17:54:13 +0000373 int is_protocol = 0;
ths3b46e622007-09-17 08:09:54 +0000374
bellardea2384d2004-08-01 21:59:26 +0000375 /* if snapshot, we create a temporary backing file and open it
376 instead of opening 'filename' directly */
377
378 /* if there is a backing file, use it */
379 bs1 = bdrv_new("");
aliguori5eb45632009-03-28 17:55:10 +0000380 ret = bdrv_open2(bs1, filename, 0, drv);
aliguori51d7c002009-03-05 23:00:29 +0000381 if (ret < 0) {
bellardea2384d2004-08-01 21:59:26 +0000382 bdrv_delete(bs1);
aliguori51d7c002009-03-05 23:00:29 +0000383 return ret;
bellardea2384d2004-08-01 21:59:26 +0000384 }
bellard83f64092006-08-01 16:21:11 +0000385 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
aliguori7c96d462008-09-12 17:54:13 +0000386
387 if (bs1->drv && bs1->drv->protocol_name)
388 is_protocol = 1;
389
bellardea2384d2004-08-01 21:59:26 +0000390 bdrv_delete(bs1);
ths3b46e622007-09-17 08:09:54 +0000391
bellardea2384d2004-08-01 21:59:26 +0000392 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
aliguori7c96d462008-09-12 17:54:13 +0000393
394 /* Real path is meaningless for protocols */
395 if (is_protocol)
396 snprintf(backing_filename, sizeof(backing_filename),
397 "%s", filename);
398 else
399 realpath(filename, backing_filename);
400
aliguori5eb45632009-03-28 17:55:10 +0000401 ret = bdrv_create2(&bdrv_qcow2, tmp_filename,
402 total_size, backing_filename,
403 (drv ? drv->format_name : NULL), 0);
aliguori51d7c002009-03-05 23:00:29 +0000404 if (ret < 0) {
405 return ret;
bellardea2384d2004-08-01 21:59:26 +0000406 }
407 filename = tmp_filename;
aliguori5eb45632009-03-28 17:55:10 +0000408 drv = &bdrv_qcow2;
bellardea2384d2004-08-01 21:59:26 +0000409 bs->is_temporary = 1;
410 }
bellard712e7872005-04-28 21:09:32 +0000411
bellardea2384d2004-08-01 21:59:26 +0000412 pstrcpy(bs->filename, sizeof(bs->filename), filename);
bellard83f64092006-08-01 16:21:11 +0000413 if (flags & BDRV_O_FILE) {
414 drv = find_protocol(filename);
aliguori51d7c002009-03-05 23:00:29 +0000415 } else if (!drv) {
416 drv = find_image_format(filename);
417 }
418 if (!drv) {
419 ret = -ENOENT;
420 goto unlink_and_fail;
bellardea2384d2004-08-01 21:59:26 +0000421 }
422 bs->drv = drv;
423 bs->opaque = qemu_mallocz(drv->instance_size);
bellard83f64092006-08-01 16:21:11 +0000424 /* Note: for compatibility, we open disk image files as RDWR, and
425 RDONLY as fallback */
426 if (!(flags & BDRV_O_FILE))
aliguori9f7965c2008-10-14 14:42:54 +0000427 open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK);
bellard83f64092006-08-01 16:21:11 +0000428 else
429 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
430 ret = drv->bdrv_open(bs, filename, open_flags);
aurel32a0a83532008-10-13 21:08:34 +0000431 if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
aliguori9f7965c2008-10-14 14:42:54 +0000432 ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
bellard83f64092006-08-01 16:21:11 +0000433 bs->read_only = 1;
434 }
bellardea2384d2004-08-01 21:59:26 +0000435 if (ret < 0) {
436 qemu_free(bs->opaque);
bellard6b21b972006-08-23 21:14:37 +0000437 bs->opaque = NULL;
438 bs->drv = NULL;
aliguori51d7c002009-03-05 23:00:29 +0000439 unlink_and_fail:
440 if (bs->is_temporary)
441 unlink(filename);
bellard83f64092006-08-01 16:21:11 +0000442 return ret;
bellardea2384d2004-08-01 21:59:26 +0000443 }
bellardd15a7712006-08-06 13:35:09 +0000444 if (drv->bdrv_getlength) {
445 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
446 }
bellardea2384d2004-08-01 21:59:26 +0000447#ifndef _WIN32
448 if (bs->is_temporary) {
449 unlink(filename);
450 }
451#endif
bellard83f64092006-08-01 16:21:11 +0000452 if (bs->backing_file[0] != '\0') {
bellardea2384d2004-08-01 21:59:26 +0000453 /* if there is a backing file, use it */
aliguori5eb45632009-03-28 17:55:10 +0000454 BlockDriver *back_drv = NULL;
bellardea2384d2004-08-01 21:59:26 +0000455 bs->backing_hd = bdrv_new("");
bellard83f64092006-08-01 16:21:11 +0000456 path_combine(backing_filename, sizeof(backing_filename),
457 filename, bs->backing_file);
aliguori5eb45632009-03-28 17:55:10 +0000458 if (bs->backing_format[0] != '\0')
459 back_drv = bdrv_find_format(bs->backing_format);
460 ret = bdrv_open2(bs->backing_hd, backing_filename, open_flags,
461 back_drv);
aliguori51d7c002009-03-05 23:00:29 +0000462 if (ret < 0) {
463 bdrv_close(bs);
464 return ret;
465 }
bellardea2384d2004-08-01 21:59:26 +0000466 }
467
aliguoribb5fc202009-03-05 23:01:15 +0000468 if (!bdrv_key_required(bs)) {
469 /* call the change callback */
470 bs->media_changed = 1;
471 if (bs->change_cb)
472 bs->change_cb(bs->change_opaque);
473 }
bellardb3380822004-03-14 21:38:54 +0000474 return 0;
bellardfc01f7e2003-06-30 10:03:06 +0000475}
476
477void bdrv_close(BlockDriverState *bs)
478{
bellard19cb3732006-08-19 11:45:59 +0000479 if (bs->drv) {
bellardea2384d2004-08-01 21:59:26 +0000480 if (bs->backing_hd)
481 bdrv_delete(bs->backing_hd);
482 bs->drv->bdrv_close(bs);
483 qemu_free(bs->opaque);
484#ifdef _WIN32
485 if (bs->is_temporary) {
486 unlink(bs->filename);
487 }
bellard67b915a2004-03-31 23:37:16 +0000488#endif
bellardea2384d2004-08-01 21:59:26 +0000489 bs->opaque = NULL;
490 bs->drv = NULL;
bellardb3380822004-03-14 21:38:54 +0000491
492 /* call the change callback */
bellard19cb3732006-08-19 11:45:59 +0000493 bs->media_changed = 1;
bellardb3380822004-03-14 21:38:54 +0000494 if (bs->change_cb)
495 bs->change_cb(bs->change_opaque);
496 }
497}
498
499void bdrv_delete(BlockDriverState *bs)
500{
aurel3234c6f052008-04-08 19:51:21 +0000501 BlockDriverState **pbs;
502
503 pbs = &bdrv_first;
504 while (*pbs != bs && *pbs != NULL)
505 pbs = &(*pbs)->next;
506 if (*pbs == bs)
507 *pbs = bs->next;
508
bellardb3380822004-03-14 21:38:54 +0000509 bdrv_close(bs);
510 qemu_free(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000511}
512
bellard33e39632003-07-06 17:15:21 +0000513/* commit COW file into the raw image */
514int bdrv_commit(BlockDriverState *bs)
515{
bellard19cb3732006-08-19 11:45:59 +0000516 BlockDriver *drv = bs->drv;
bellard83f64092006-08-01 16:21:11 +0000517 int64_t i, total_sectors;
bellardea2384d2004-08-01 21:59:26 +0000518 int n, j;
519 unsigned char sector[512];
bellard33e39632003-07-06 17:15:21 +0000520
bellard19cb3732006-08-19 11:45:59 +0000521 if (!drv)
522 return -ENOMEDIUM;
bellard33e39632003-07-06 17:15:21 +0000523
524 if (bs->read_only) {
bellardea2384d2004-08-01 21:59:26 +0000525 return -EACCES;
bellard33e39632003-07-06 17:15:21 +0000526 }
527
bellardea2384d2004-08-01 21:59:26 +0000528 if (!bs->backing_hd) {
529 return -ENOTSUP;
bellard33e39632003-07-06 17:15:21 +0000530 }
bellardea2384d2004-08-01 21:59:26 +0000531
bellard83f64092006-08-01 16:21:11 +0000532 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
533 for (i = 0; i < total_sectors;) {
bellard19cb3732006-08-19 11:45:59 +0000534 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
bellardea2384d2004-08-01 21:59:26 +0000535 for(j = 0; j < n; j++) {
536 if (bdrv_read(bs, i, sector, 1) != 0) {
537 return -EIO;
538 }
539
540 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
541 return -EIO;
542 }
543 i++;
544 }
545 } else {
546 i += n;
547 }
548 }
bellard95389c82005-12-18 18:28:15 +0000549
bellard19cb3732006-08-19 11:45:59 +0000550 if (drv->bdrv_make_empty)
551 return drv->bdrv_make_empty(bs);
bellard95389c82005-12-18 18:28:15 +0000552
bellard33e39632003-07-06 17:15:21 +0000553 return 0;
554}
555
aliguori71d07702009-03-03 17:37:16 +0000556static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
557 size_t size)
558{
559 int64_t len;
560
561 if (!bdrv_is_inserted(bs))
562 return -ENOMEDIUM;
563
564 if (bs->growable)
565 return 0;
566
567 len = bdrv_getlength(bs);
568
569 if ((offset + size) > len)
570 return -EIO;
571
572 return 0;
573}
574
575static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
576 int nb_sectors)
577{
aliguori999dec52009-03-29 01:31:48 +0000578 return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512);
aliguori71d07702009-03-03 17:37:16 +0000579}
580
bellard19cb3732006-08-19 11:45:59 +0000581/* return < 0 if error. See bdrv_write() for the return codes */
ths5fafdf22007-09-16 21:08:06 +0000582int bdrv_read(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000583 uint8_t *buf, int nb_sectors)
584{
bellardea2384d2004-08-01 21:59:26 +0000585 BlockDriver *drv = bs->drv;
586
bellard19cb3732006-08-19 11:45:59 +0000587 if (!drv)
588 return -ENOMEDIUM;
aliguori71d07702009-03-03 17:37:16 +0000589 if (bdrv_check_request(bs, sector_num, nb_sectors))
590 return -EIO;
bellardb3380822004-03-14 21:38:54 +0000591
aliguorieda578e2009-03-12 19:57:16 +0000592 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
bellardfc01f7e2003-06-30 10:03:06 +0000593}
594
ths5fafdf22007-09-16 21:08:06 +0000595/* Return < 0 if error. Important errors are:
bellard19cb3732006-08-19 11:45:59 +0000596 -EIO generic I/O error (may happen for all errors)
597 -ENOMEDIUM No media inserted.
598 -EINVAL Invalid sector number or nb_sectors
599 -EACCES Trying to write a read-only device
600*/
ths5fafdf22007-09-16 21:08:06 +0000601int bdrv_write(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000602 const uint8_t *buf, int nb_sectors)
603{
bellard83f64092006-08-01 16:21:11 +0000604 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +0000605 if (!bs->drv)
606 return -ENOMEDIUM;
bellard0849bf02003-06-30 23:17:31 +0000607 if (bs->read_only)
bellard19cb3732006-08-19 11:45:59 +0000608 return -EACCES;
aliguori71d07702009-03-03 17:37:16 +0000609 if (bdrv_check_request(bs, sector_num, nb_sectors))
610 return -EIO;
611
aliguori42fb2802009-01-15 20:43:39 +0000612 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
bellard83f64092006-08-01 16:21:11 +0000613}
614
aliguorieda578e2009-03-12 19:57:16 +0000615int bdrv_pread(BlockDriverState *bs, int64_t offset,
616 void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000617{
bellard83f64092006-08-01 16:21:11 +0000618 uint8_t tmp_buf[SECTOR_SIZE];
619 int len, nb_sectors, count;
620 int64_t sector_num;
621
622 count = count1;
623 /* first read to align to sector start */
624 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
625 if (len > count)
626 len = count;
627 sector_num = offset >> SECTOR_BITS;
628 if (len > 0) {
629 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
630 return -EIO;
631 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
632 count -= len;
633 if (count == 0)
634 return count1;
635 sector_num++;
636 buf += len;
637 }
638
639 /* read the sectors "in place" */
640 nb_sectors = count >> SECTOR_BITS;
641 if (nb_sectors > 0) {
642 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
643 return -EIO;
644 sector_num += nb_sectors;
645 len = nb_sectors << SECTOR_BITS;
646 buf += len;
647 count -= len;
648 }
649
650 /* add data from the last sector */
651 if (count > 0) {
652 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
653 return -EIO;
654 memcpy(buf, tmp_buf, count);
655 }
656 return count1;
657}
658
aliguorieda578e2009-03-12 19:57:16 +0000659int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
660 const void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000661{
bellard83f64092006-08-01 16:21:11 +0000662 uint8_t tmp_buf[SECTOR_SIZE];
663 int len, nb_sectors, count;
664 int64_t sector_num;
665
666 count = count1;
667 /* first write to align to sector start */
668 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
669 if (len > count)
670 len = count;
671 sector_num = offset >> SECTOR_BITS;
672 if (len > 0) {
673 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
674 return -EIO;
675 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
676 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
677 return -EIO;
678 count -= len;
679 if (count == 0)
680 return count1;
681 sector_num++;
682 buf += len;
683 }
684
685 /* write the sectors "in place" */
686 nb_sectors = count >> SECTOR_BITS;
687 if (nb_sectors > 0) {
688 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
689 return -EIO;
690 sector_num += nb_sectors;
691 len = nb_sectors << SECTOR_BITS;
692 buf += len;
693 count -= len;
694 }
695
696 /* add data from the last sector */
697 if (count > 0) {
698 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
699 return -EIO;
700 memcpy(tmp_buf, buf, count);
701 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
702 return -EIO;
703 }
704 return count1;
705}
bellard83f64092006-08-01 16:21:11 +0000706
707/**
bellard83f64092006-08-01 16:21:11 +0000708 * Truncate file to 'offset' bytes (needed only for file protocols)
709 */
710int bdrv_truncate(BlockDriverState *bs, int64_t offset)
711{
712 BlockDriver *drv = bs->drv;
713 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000714 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000715 if (!drv->bdrv_truncate)
716 return -ENOTSUP;
717 return drv->bdrv_truncate(bs, offset);
718}
719
720/**
721 * Length of a file in bytes. Return < 0 if error or unknown.
722 */
723int64_t bdrv_getlength(BlockDriverState *bs)
724{
725 BlockDriver *drv = bs->drv;
726 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000727 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000728 if (!drv->bdrv_getlength) {
729 /* legacy mode */
730 return bs->total_sectors * SECTOR_SIZE;
731 }
732 return drv->bdrv_getlength(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000733}
734
bellard19cb3732006-08-19 11:45:59 +0000735/* return 0 as number of sectors if no device present or error */
ths96b8f132007-12-17 01:35:20 +0000736void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
bellardfc01f7e2003-06-30 10:03:06 +0000737{
bellard19cb3732006-08-19 11:45:59 +0000738 int64_t length;
739 length = bdrv_getlength(bs);
740 if (length < 0)
741 length = 0;
742 else
743 length = length >> SECTOR_BITS;
744 *nb_sectors_ptr = length;
bellardfc01f7e2003-06-30 10:03:06 +0000745}
bellardcf989512004-02-16 21:56:36 +0000746
aliguorif3d54fc2008-11-25 21:50:24 +0000747struct partition {
748 uint8_t boot_ind; /* 0x80 - active */
749 uint8_t head; /* starting head */
750 uint8_t sector; /* starting sector */
751 uint8_t cyl; /* starting cylinder */
752 uint8_t sys_ind; /* What partition type */
753 uint8_t end_head; /* end head */
754 uint8_t end_sector; /* end sector */
755 uint8_t end_cyl; /* end cylinder */
756 uint32_t start_sect; /* starting sector counting from 0 */
757 uint32_t nr_sects; /* nr of sectors in partition */
758} __attribute__((packed));
759
760/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
761static int guess_disk_lchs(BlockDriverState *bs,
762 int *pcylinders, int *pheads, int *psectors)
763{
764 uint8_t buf[512];
765 int ret, i, heads, sectors, cylinders;
766 struct partition *p;
767 uint32_t nr_sects;
blueswir1a38131b2008-12-05 17:56:40 +0000768 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +0000769
770 bdrv_get_geometry(bs, &nb_sectors);
771
772 ret = bdrv_read(bs, 0, buf, 1);
773 if (ret < 0)
774 return -1;
775 /* test msdos magic */
776 if (buf[510] != 0x55 || buf[511] != 0xaa)
777 return -1;
778 for(i = 0; i < 4; i++) {
779 p = ((struct partition *)(buf + 0x1be)) + i;
780 nr_sects = le32_to_cpu(p->nr_sects);
781 if (nr_sects && p->end_head) {
782 /* We make the assumption that the partition terminates on
783 a cylinder boundary */
784 heads = p->end_head + 1;
785 sectors = p->end_sector & 63;
786 if (sectors == 0)
787 continue;
788 cylinders = nb_sectors / (heads * sectors);
789 if (cylinders < 1 || cylinders > 16383)
790 continue;
791 *pheads = heads;
792 *psectors = sectors;
793 *pcylinders = cylinders;
794#if 0
795 printf("guessed geometry: LCHS=%d %d %d\n",
796 cylinders, heads, sectors);
797#endif
798 return 0;
799 }
800 }
801 return -1;
802}
803
804void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
805{
806 int translation, lba_detected = 0;
807 int cylinders, heads, secs;
blueswir1a38131b2008-12-05 17:56:40 +0000808 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +0000809
810 /* if a geometry hint is available, use it */
811 bdrv_get_geometry(bs, &nb_sectors);
812 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
813 translation = bdrv_get_translation_hint(bs);
814 if (cylinders != 0) {
815 *pcyls = cylinders;
816 *pheads = heads;
817 *psecs = secs;
818 } else {
819 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
820 if (heads > 16) {
821 /* if heads > 16, it means that a BIOS LBA
822 translation was active, so the default
823 hardware geometry is OK */
824 lba_detected = 1;
825 goto default_geometry;
826 } else {
827 *pcyls = cylinders;
828 *pheads = heads;
829 *psecs = secs;
830 /* disable any translation to be in sync with
831 the logical geometry */
832 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
833 bdrv_set_translation_hint(bs,
834 BIOS_ATA_TRANSLATION_NONE);
835 }
836 }
837 } else {
838 default_geometry:
839 /* if no geometry, use a standard physical disk geometry */
840 cylinders = nb_sectors / (16 * 63);
841
842 if (cylinders > 16383)
843 cylinders = 16383;
844 else if (cylinders < 2)
845 cylinders = 2;
846 *pcyls = cylinders;
847 *pheads = 16;
848 *psecs = 63;
849 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
850 if ((*pcyls * *pheads) <= 131072) {
851 bdrv_set_translation_hint(bs,
852 BIOS_ATA_TRANSLATION_LARGE);
853 } else {
854 bdrv_set_translation_hint(bs,
855 BIOS_ATA_TRANSLATION_LBA);
856 }
857 }
858 }
859 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
860 }
861}
862
ths5fafdf22007-09-16 21:08:06 +0000863void bdrv_set_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000864 int cyls, int heads, int secs)
865{
866 bs->cyls = cyls;
867 bs->heads = heads;
868 bs->secs = secs;
869}
870
871void bdrv_set_type_hint(BlockDriverState *bs, int type)
872{
873 bs->type = type;
874 bs->removable = ((type == BDRV_TYPE_CDROM ||
875 type == BDRV_TYPE_FLOPPY));
876}
877
bellard46d47672004-11-16 01:45:27 +0000878void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
879{
880 bs->translation = translation;
881}
882
ths5fafdf22007-09-16 21:08:06 +0000883void bdrv_get_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000884 int *pcyls, int *pheads, int *psecs)
885{
886 *pcyls = bs->cyls;
887 *pheads = bs->heads;
888 *psecs = bs->secs;
889}
890
891int bdrv_get_type_hint(BlockDriverState *bs)
892{
893 return bs->type;
894}
895
bellard46d47672004-11-16 01:45:27 +0000896int bdrv_get_translation_hint(BlockDriverState *bs)
897{
898 return bs->translation;
899}
900
bellardb3380822004-03-14 21:38:54 +0000901int bdrv_is_removable(BlockDriverState *bs)
902{
903 return bs->removable;
904}
905
906int bdrv_is_read_only(BlockDriverState *bs)
907{
908 return bs->read_only;
909}
910
ths985a03b2007-12-24 16:10:43 +0000911int bdrv_is_sg(BlockDriverState *bs)
912{
913 return bs->sg;
914}
915
bellard19cb3732006-08-19 11:45:59 +0000916/* XXX: no longer used */
ths5fafdf22007-09-16 21:08:06 +0000917void bdrv_set_change_cb(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000918 void (*change_cb)(void *opaque), void *opaque)
919{
920 bs->change_cb = change_cb;
921 bs->change_opaque = opaque;
922}
923
bellardea2384d2004-08-01 21:59:26 +0000924int bdrv_is_encrypted(BlockDriverState *bs)
925{
926 if (bs->backing_hd && bs->backing_hd->encrypted)
927 return 1;
928 return bs->encrypted;
929}
930
aliguoric0f4ce72009-03-05 23:01:01 +0000931int bdrv_key_required(BlockDriverState *bs)
932{
933 BlockDriverState *backing_hd = bs->backing_hd;
934
935 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
936 return 1;
937 return (bs->encrypted && !bs->valid_key);
938}
939
bellardea2384d2004-08-01 21:59:26 +0000940int bdrv_set_key(BlockDriverState *bs, const char *key)
941{
942 int ret;
943 if (bs->backing_hd && bs->backing_hd->encrypted) {
944 ret = bdrv_set_key(bs->backing_hd, key);
945 if (ret < 0)
946 return ret;
947 if (!bs->encrypted)
948 return 0;
949 }
950 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
951 return -1;
aliguoric0f4ce72009-03-05 23:01:01 +0000952 ret = bs->drv->bdrv_set_key(bs, key);
aliguoribb5fc202009-03-05 23:01:15 +0000953 if (ret < 0) {
954 bs->valid_key = 0;
955 } else if (!bs->valid_key) {
956 bs->valid_key = 1;
957 /* call the change callback now, we skipped it on open */
958 bs->media_changed = 1;
959 if (bs->change_cb)
960 bs->change_cb(bs->change_opaque);
961 }
aliguoric0f4ce72009-03-05 23:01:01 +0000962 return ret;
bellardea2384d2004-08-01 21:59:26 +0000963}
964
965void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
966{
bellard19cb3732006-08-19 11:45:59 +0000967 if (!bs->drv) {
bellardea2384d2004-08-01 21:59:26 +0000968 buf[0] = '\0';
969 } else {
970 pstrcpy(buf, buf_size, bs->drv->format_name);
971 }
972}
973
ths5fafdf22007-09-16 21:08:06 +0000974void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
bellardea2384d2004-08-01 21:59:26 +0000975 void *opaque)
976{
977 BlockDriver *drv;
978
979 for (drv = first_drv; drv != NULL; drv = drv->next) {
980 it(opaque, drv->format_name);
981 }
982}
983
bellardb3380822004-03-14 21:38:54 +0000984BlockDriverState *bdrv_find(const char *name)
985{
986 BlockDriverState *bs;
987
988 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
989 if (!strcmp(name, bs->device_name))
990 return bs;
991 }
992 return NULL;
993}
994
aliguori51de9762009-03-05 23:00:43 +0000995void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
bellard81d09122004-07-14 17:21:37 +0000996{
997 BlockDriverState *bs;
998
999 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori51de9762009-03-05 23:00:43 +00001000 it(opaque, bs);
bellard81d09122004-07-14 17:21:37 +00001001 }
1002}
1003
bellardea2384d2004-08-01 21:59:26 +00001004const char *bdrv_get_device_name(BlockDriverState *bs)
1005{
1006 return bs->device_name;
1007}
1008
pbrook7a6cba62006-06-04 11:39:07 +00001009void bdrv_flush(BlockDriverState *bs)
1010{
aliguori081501d2009-03-29 01:31:51 +00001011 if (!bs->drv)
1012 return;
pbrook7a6cba62006-06-04 11:39:07 +00001013 if (bs->drv->bdrv_flush)
1014 bs->drv->bdrv_flush(bs);
1015 if (bs->backing_hd)
1016 bdrv_flush(bs->backing_hd);
1017}
1018
aliguoric6ca28d2008-10-06 13:55:43 +00001019void bdrv_flush_all(void)
1020{
1021 BlockDriverState *bs;
1022
1023 for (bs = bdrv_first; bs != NULL; bs = bs->next)
1024 if (bs->drv && !bdrv_is_read_only(bs) &&
1025 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1026 bdrv_flush(bs);
1027}
1028
thsf58c7b32008-06-05 21:53:49 +00001029/*
1030 * Returns true iff the specified sector is present in the disk image. Drivers
1031 * not implementing the functionality are assumed to not support backing files,
1032 * hence all their sectors are reported as allocated.
1033 *
1034 * 'pnum' is set to the number of sectors (including and immediately following
1035 * the specified sector) that are known to be in the same
1036 * allocated/unallocated state.
1037 *
1038 * 'nb_sectors' is the max value 'pnum' should be set to.
1039 */
1040int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1041 int *pnum)
1042{
1043 int64_t n;
1044 if (!bs->drv->bdrv_is_allocated) {
1045 if (sector_num >= bs->total_sectors) {
1046 *pnum = 0;
1047 return 0;
1048 }
1049 n = bs->total_sectors - sector_num;
1050 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1051 return 1;
1052 }
1053 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1054}
1055
aliguori376253e2009-03-05 23:01:23 +00001056void bdrv_info(Monitor *mon)
bellardb3380822004-03-14 21:38:54 +00001057{
1058 BlockDriverState *bs;
1059
1060 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori376253e2009-03-05 23:01:23 +00001061 monitor_printf(mon, "%s:", bs->device_name);
1062 monitor_printf(mon, " type=");
bellardb3380822004-03-14 21:38:54 +00001063 switch(bs->type) {
1064 case BDRV_TYPE_HD:
aliguori376253e2009-03-05 23:01:23 +00001065 monitor_printf(mon, "hd");
bellardb3380822004-03-14 21:38:54 +00001066 break;
1067 case BDRV_TYPE_CDROM:
aliguori376253e2009-03-05 23:01:23 +00001068 monitor_printf(mon, "cdrom");
bellardb3380822004-03-14 21:38:54 +00001069 break;
1070 case BDRV_TYPE_FLOPPY:
aliguori376253e2009-03-05 23:01:23 +00001071 monitor_printf(mon, "floppy");
bellardb3380822004-03-14 21:38:54 +00001072 break;
1073 }
aliguori376253e2009-03-05 23:01:23 +00001074 monitor_printf(mon, " removable=%d", bs->removable);
bellardb3380822004-03-14 21:38:54 +00001075 if (bs->removable) {
aliguori376253e2009-03-05 23:01:23 +00001076 monitor_printf(mon, " locked=%d", bs->locked);
bellardb3380822004-03-14 21:38:54 +00001077 }
bellard19cb3732006-08-19 11:45:59 +00001078 if (bs->drv) {
aliguori376253e2009-03-05 23:01:23 +00001079 monitor_printf(mon, " file=");
1080 monitor_print_filename(mon, bs->filename);
thsfef30742006-12-22 14:11:32 +00001081 if (bs->backing_file[0] != '\0') {
aliguori376253e2009-03-05 23:01:23 +00001082 monitor_printf(mon, " backing_file=");
1083 monitor_print_filename(mon, bs->backing_file);
1084 }
1085 monitor_printf(mon, " ro=%d", bs->read_only);
1086 monitor_printf(mon, " drv=%s", bs->drv->format_name);
1087 monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs));
bellardb3380822004-03-14 21:38:54 +00001088 } else {
aliguori376253e2009-03-05 23:01:23 +00001089 monitor_printf(mon, " [not inserted]");
bellardb3380822004-03-14 21:38:54 +00001090 }
aliguori376253e2009-03-05 23:01:23 +00001091 monitor_printf(mon, "\n");
bellardb3380822004-03-14 21:38:54 +00001092 }
1093}
thsa36e69d2007-12-02 05:18:19 +00001094
1095/* The "info blockstats" command. */
aliguori376253e2009-03-05 23:01:23 +00001096void bdrv_info_stats(Monitor *mon)
thsa36e69d2007-12-02 05:18:19 +00001097{
1098 BlockDriverState *bs;
1099
1100 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori376253e2009-03-05 23:01:23 +00001101 monitor_printf(mon, "%s:"
1102 " rd_bytes=%" PRIu64
1103 " wr_bytes=%" PRIu64
1104 " rd_operations=%" PRIu64
1105 " wr_operations=%" PRIu64
aliguoriebf53fc2009-03-11 20:05:29 +00001106 "\n",
aliguori376253e2009-03-05 23:01:23 +00001107 bs->device_name,
1108 bs->rd_bytes, bs->wr_bytes,
1109 bs->rd_ops, bs->wr_ops);
thsa36e69d2007-12-02 05:18:19 +00001110 }
1111}
bellardea2384d2004-08-01 21:59:26 +00001112
aliguori045df332009-03-05 23:00:48 +00001113const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1114{
1115 if (bs->backing_hd && bs->backing_hd->encrypted)
1116 return bs->backing_file;
1117 else if (bs->encrypted)
1118 return bs->filename;
1119 else
1120 return NULL;
1121}
1122
ths5fafdf22007-09-16 21:08:06 +00001123void bdrv_get_backing_filename(BlockDriverState *bs,
bellard83f64092006-08-01 16:21:11 +00001124 char *filename, int filename_size)
bellardea2384d2004-08-01 21:59:26 +00001125{
bellard83f64092006-08-01 16:21:11 +00001126 if (!bs->backing_hd) {
1127 pstrcpy(filename, filename_size, "");
1128 } else {
1129 pstrcpy(filename, filename_size, bs->backing_file);
1130 }
bellardea2384d2004-08-01 21:59:26 +00001131}
1132
ths5fafdf22007-09-16 21:08:06 +00001133int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
bellardfaea38e2006-08-05 21:31:00 +00001134 const uint8_t *buf, int nb_sectors)
1135{
1136 BlockDriver *drv = bs->drv;
1137 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001138 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001139 if (!drv->bdrv_write_compressed)
1140 return -ENOTSUP;
1141 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1142}
ths3b46e622007-09-17 08:09:54 +00001143
bellardfaea38e2006-08-05 21:31:00 +00001144int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
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_get_info)
1150 return -ENOTSUP;
1151 memset(bdi, 0, sizeof(*bdi));
1152 return drv->bdrv_get_info(bs, bdi);
1153}
1154
aliguori178e08a2009-04-05 19:10:55 +00001155int bdrv_put_buffer(BlockDriverState *bs, const uint8_t *buf, int64_t pos, int size)
1156{
1157 BlockDriver *drv = bs->drv;
1158 if (!drv)
1159 return -ENOMEDIUM;
1160 if (!drv->bdrv_put_buffer)
1161 return -ENOTSUP;
1162 return drv->bdrv_put_buffer(bs, buf, pos, size);
1163}
1164
1165int bdrv_get_buffer(BlockDriverState *bs, uint8_t *buf, int64_t pos, int size)
1166{
1167 BlockDriver *drv = bs->drv;
1168 if (!drv)
1169 return -ENOMEDIUM;
1170 if (!drv->bdrv_get_buffer)
1171 return -ENOTSUP;
1172 return drv->bdrv_get_buffer(bs, buf, pos, size);
1173}
1174
bellardfaea38e2006-08-05 21:31:00 +00001175/**************************************************************/
1176/* handling of snapshots */
1177
ths5fafdf22007-09-16 21:08:06 +00001178int bdrv_snapshot_create(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001179 QEMUSnapshotInfo *sn_info)
1180{
1181 BlockDriver *drv = bs->drv;
1182 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001183 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001184 if (!drv->bdrv_snapshot_create)
1185 return -ENOTSUP;
1186 return drv->bdrv_snapshot_create(bs, sn_info);
1187}
1188
ths5fafdf22007-09-16 21:08:06 +00001189int bdrv_snapshot_goto(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001190 const char *snapshot_id)
1191{
1192 BlockDriver *drv = bs->drv;
1193 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001194 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001195 if (!drv->bdrv_snapshot_goto)
1196 return -ENOTSUP;
1197 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1198}
1199
1200int bdrv_snapshot_delete(BlockDriverState *bs, 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_delete)
1206 return -ENOTSUP;
1207 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1208}
1209
ths5fafdf22007-09-16 21:08:06 +00001210int bdrv_snapshot_list(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001211 QEMUSnapshotInfo **psn_info)
1212{
1213 BlockDriver *drv = bs->drv;
1214 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001215 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001216 if (!drv->bdrv_snapshot_list)
1217 return -ENOTSUP;
1218 return drv->bdrv_snapshot_list(bs, psn_info);
1219}
1220
1221#define NB_SUFFIXES 4
1222
1223char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1224{
1225 static const char suffixes[NB_SUFFIXES] = "KMGT";
1226 int64_t base;
1227 int i;
1228
1229 if (size <= 999) {
1230 snprintf(buf, buf_size, "%" PRId64, size);
1231 } else {
1232 base = 1024;
1233 for(i = 0; i < NB_SUFFIXES; i++) {
1234 if (size < (10 * base)) {
ths5fafdf22007-09-16 21:08:06 +00001235 snprintf(buf, buf_size, "%0.1f%c",
bellardfaea38e2006-08-05 21:31:00 +00001236 (double)size / base,
1237 suffixes[i]);
1238 break;
1239 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
ths5fafdf22007-09-16 21:08:06 +00001240 snprintf(buf, buf_size, "%" PRId64 "%c",
bellardfaea38e2006-08-05 21:31:00 +00001241 ((size + (base >> 1)) / base),
1242 suffixes[i]);
1243 break;
1244 }
1245 base = base * 1024;
1246 }
1247 }
1248 return buf;
1249}
1250
1251char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1252{
1253 char buf1[128], date_buf[128], clock_buf[128];
bellard3b9f94e2007-01-07 17:27:07 +00001254#ifdef _WIN32
1255 struct tm *ptm;
1256#else
bellardfaea38e2006-08-05 21:31:00 +00001257 struct tm tm;
bellard3b9f94e2007-01-07 17:27:07 +00001258#endif
bellardfaea38e2006-08-05 21:31:00 +00001259 time_t ti;
1260 int64_t secs;
1261
1262 if (!sn) {
ths5fafdf22007-09-16 21:08:06 +00001263 snprintf(buf, buf_size,
1264 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001265 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1266 } else {
1267 ti = sn->date_sec;
bellard3b9f94e2007-01-07 17:27:07 +00001268#ifdef _WIN32
1269 ptm = localtime(&ti);
1270 strftime(date_buf, sizeof(date_buf),
1271 "%Y-%m-%d %H:%M:%S", ptm);
1272#else
bellardfaea38e2006-08-05 21:31:00 +00001273 localtime_r(&ti, &tm);
1274 strftime(date_buf, sizeof(date_buf),
1275 "%Y-%m-%d %H:%M:%S", &tm);
bellard3b9f94e2007-01-07 17:27:07 +00001276#endif
bellardfaea38e2006-08-05 21:31:00 +00001277 secs = sn->vm_clock_nsec / 1000000000;
1278 snprintf(clock_buf, sizeof(clock_buf),
1279 "%02d:%02d:%02d.%03d",
1280 (int)(secs / 3600),
1281 (int)((secs / 60) % 60),
ths5fafdf22007-09-16 21:08:06 +00001282 (int)(secs % 60),
bellardfaea38e2006-08-05 21:31:00 +00001283 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1284 snprintf(buf, buf_size,
ths5fafdf22007-09-16 21:08:06 +00001285 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001286 sn->id_str, sn->name,
1287 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1288 date_buf,
1289 clock_buf);
1290 }
1291 return buf;
1292}
1293
bellardea2384d2004-08-01 21:59:26 +00001294
bellard83f64092006-08-01 16:21:11 +00001295/**************************************************************/
1296/* async I/Os */
1297
aliguori3fb94d52009-03-20 18:26:12 +00001298typedef struct VectorTranslationAIOCB {
1299 BlockDriverAIOCB common;
aliguori3b69e4b2009-01-22 16:59:24 +00001300 QEMUIOVector *iov;
1301 uint8_t *bounce;
1302 int is_write;
1303 BlockDriverAIOCB *aiocb;
aliguori3fb94d52009-03-20 18:26:12 +00001304} VectorTranslationAIOCB;
aliguori3b69e4b2009-01-22 16:59:24 +00001305
aliguori3fb94d52009-03-20 18:26:12 +00001306static void bdrv_aio_cancel_vector(BlockDriverAIOCB *_acb)
aliguoric07a9002009-03-20 18:26:03 +00001307{
aliguori3fb94d52009-03-20 18:26:12 +00001308 VectorTranslationAIOCB *acb
1309 = container_of(_acb, VectorTranslationAIOCB, common);
aliguoric07a9002009-03-20 18:26:03 +00001310
aliguori3fb94d52009-03-20 18:26:12 +00001311 bdrv_aio_cancel(acb->aiocb);
aliguoric07a9002009-03-20 18:26:03 +00001312}
1313
aliguori3b69e4b2009-01-22 16:59:24 +00001314static void bdrv_aio_rw_vector_cb(void *opaque, int ret)
1315{
aliguori3fb94d52009-03-20 18:26:12 +00001316 VectorTranslationAIOCB *s = (VectorTranslationAIOCB *)opaque;
aliguori3b69e4b2009-01-22 16:59:24 +00001317
1318 if (!s->is_write) {
aliguori249aa742009-01-26 17:17:52 +00001319 qemu_iovec_from_buffer(s->iov, s->bounce, s->iov->size);
aliguori3b69e4b2009-01-22 16:59:24 +00001320 }
aurel32d905dba2009-03-03 06:28:26 +00001321 qemu_vfree(s->bounce);
aliguori3fb94d52009-03-20 18:26:12 +00001322 s->common.cb(s->common.opaque, ret);
1323 qemu_aio_release(s);
aliguori3b69e4b2009-01-22 16:59:24 +00001324}
1325
1326static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
1327 int64_t sector_num,
1328 QEMUIOVector *iov,
1329 int nb_sectors,
1330 BlockDriverCompletionFunc *cb,
1331 void *opaque,
1332 int is_write)
1333
1334{
aliguori3fb94d52009-03-20 18:26:12 +00001335 VectorTranslationAIOCB *s = qemu_aio_get_pool(&vectored_aio_pool, bs,
1336 cb, opaque);
aliguori3b69e4b2009-01-22 16:59:24 +00001337
aliguori3b69e4b2009-01-22 16:59:24 +00001338 s->iov = iov;
1339 s->bounce = qemu_memalign(512, nb_sectors * 512);
1340 s->is_write = is_write;
1341 if (is_write) {
1342 qemu_iovec_to_buffer(s->iov, s->bounce);
1343 s->aiocb = bdrv_aio_write(bs, sector_num, s->bounce, nb_sectors,
1344 bdrv_aio_rw_vector_cb, s);
1345 } else {
1346 s->aiocb = bdrv_aio_read(bs, sector_num, s->bounce, nb_sectors,
1347 bdrv_aio_rw_vector_cb, s);
1348 }
aliguoric240b9a2009-03-28 16:11:20 +00001349 if (!s->aiocb) {
1350 qemu_vfree(s->bounce);
1351 qemu_aio_release(s);
1352 return NULL;
1353 }
aliguori3fb94d52009-03-20 18:26:12 +00001354 return &s->common;
aliguori3b69e4b2009-01-22 16:59:24 +00001355}
1356
1357BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
1358 QEMUIOVector *iov, int nb_sectors,
1359 BlockDriverCompletionFunc *cb, void *opaque)
1360{
aliguori71d07702009-03-03 17:37:16 +00001361 if (bdrv_check_request(bs, sector_num, nb_sectors))
1362 return NULL;
1363
aliguori3b69e4b2009-01-22 16:59:24 +00001364 return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
1365 cb, opaque, 0);
1366}
1367
1368BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1369 QEMUIOVector *iov, int nb_sectors,
1370 BlockDriverCompletionFunc *cb, void *opaque)
1371{
aliguori71d07702009-03-03 17:37:16 +00001372 if (bdrv_check_request(bs, sector_num, nb_sectors))
1373 return NULL;
1374
aliguori3b69e4b2009-01-22 16:59:24 +00001375 return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
1376 cb, opaque, 1);
1377}
1378
aliguoric87c0672009-04-07 18:43:20 +00001379static BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
pbrookce1a14d2006-08-07 02:38:06 +00001380 uint8_t *buf, int nb_sectors,
1381 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001382{
1383 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001384 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001385
bellard19cb3732006-08-19 11:45:59 +00001386 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001387 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001388 if (bdrv_check_request(bs, sector_num, nb_sectors))
1389 return NULL;
ths3b46e622007-09-17 08:09:54 +00001390
thsa36e69d2007-12-02 05:18:19 +00001391 ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1392
1393 if (ret) {
1394 /* Update stats even though technically transfer has not happened. */
1395 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1396 bs->rd_ops ++;
1397 }
1398
1399 return ret;
bellard83f64092006-08-01 16:21:11 +00001400}
1401
aliguoric87c0672009-04-07 18:43:20 +00001402static BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
pbrookce1a14d2006-08-07 02:38:06 +00001403 const uint8_t *buf, int nb_sectors,
1404 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001405{
bellard83f64092006-08-01 16:21:11 +00001406 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001407 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001408
bellard19cb3732006-08-19 11:45:59 +00001409 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001410 return NULL;
bellard83f64092006-08-01 16:21:11 +00001411 if (bs->read_only)
pbrookce1a14d2006-08-07 02:38:06 +00001412 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001413 if (bdrv_check_request(bs, sector_num, nb_sectors))
1414 return NULL;
bellard83f64092006-08-01 16:21:11 +00001415
thsa36e69d2007-12-02 05:18:19 +00001416 ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1417
1418 if (ret) {
1419 /* Update stats even though technically transfer has not happened. */
1420 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1421 bs->wr_ops ++;
1422 }
1423
1424 return ret;
bellard83f64092006-08-01 16:21:11 +00001425}
1426
1427void bdrv_aio_cancel(BlockDriverAIOCB *acb)
pbrookce1a14d2006-08-07 02:38:06 +00001428{
aliguori6bbff9a2009-03-20 18:25:59 +00001429 acb->pool->cancel(acb);
bellard83f64092006-08-01 16:21:11 +00001430}
1431
pbrookce1a14d2006-08-07 02:38:06 +00001432
bellard83f64092006-08-01 16:21:11 +00001433/**************************************************************/
1434/* async block device emulation */
1435
bellard83f64092006-08-01 16:21:11 +00001436static void bdrv_aio_bh_cb(void *opaque)
bellardbeac80c2006-06-26 20:08:57 +00001437{
pbrookce1a14d2006-08-07 02:38:06 +00001438 BlockDriverAIOCBSync *acb = opaque;
1439 acb->common.cb(acb->common.opaque, acb->ret);
1440 qemu_aio_release(acb);
bellardbeac80c2006-06-26 20:08:57 +00001441}
bellardbeac80c2006-06-26 20:08:57 +00001442
pbrookce1a14d2006-08-07 02:38:06 +00001443static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1444 int64_t sector_num, uint8_t *buf, int nb_sectors,
1445 BlockDriverCompletionFunc *cb, void *opaque)
bellardea2384d2004-08-01 21:59:26 +00001446{
pbrookce1a14d2006-08-07 02:38:06 +00001447 BlockDriverAIOCBSync *acb;
bellard83f64092006-08-01 16:21:11 +00001448 int ret;
pbrookce1a14d2006-08-07 02:38:06 +00001449
1450 acb = qemu_aio_get(bs, cb, opaque);
1451 if (!acb->bh)
1452 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1453 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1454 acb->ret = ret;
1455 qemu_bh_schedule(acb->bh);
1456 return &acb->common;
pbrook7a6cba62006-06-04 11:39:07 +00001457}
1458
pbrookce1a14d2006-08-07 02:38:06 +00001459static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1460 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1461 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001462{
pbrookce1a14d2006-08-07 02:38:06 +00001463 BlockDriverAIOCBSync *acb;
bellard83f64092006-08-01 16:21:11 +00001464 int ret;
pbrookce1a14d2006-08-07 02:38:06 +00001465
1466 acb = qemu_aio_get(bs, cb, opaque);
1467 if (!acb->bh)
1468 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1469 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1470 acb->ret = ret;
1471 qemu_bh_schedule(acb->bh);
1472 return &acb->common;
bellard83f64092006-08-01 16:21:11 +00001473}
1474
pbrookce1a14d2006-08-07 02:38:06 +00001475static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
bellard83f64092006-08-01 16:21:11 +00001476{
pbrookce1a14d2006-08-07 02:38:06 +00001477 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1478 qemu_bh_cancel(acb->bh);
1479 qemu_aio_release(acb);
bellard83f64092006-08-01 16:21:11 +00001480}
bellard83f64092006-08-01 16:21:11 +00001481
1482/**************************************************************/
1483/* sync block device emulation */
1484
1485static void bdrv_rw_em_cb(void *opaque, int ret)
1486{
1487 *(int *)opaque = ret;
1488}
1489
1490#define NOT_DONE 0x7fffffff
1491
ths5fafdf22007-09-16 21:08:06 +00001492static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +00001493 uint8_t *buf, int nb_sectors)
1494{
pbrookce1a14d2006-08-07 02:38:06 +00001495 int async_ret;
1496 BlockDriverAIOCB *acb;
bellard83f64092006-08-01 16:21:11 +00001497
bellard83f64092006-08-01 16:21:11 +00001498 async_ret = NOT_DONE;
ths5fafdf22007-09-16 21:08:06 +00001499 acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
bellard83f64092006-08-01 16:21:11 +00001500 bdrv_rw_em_cb, &async_ret);
aliguoribaf35cb2008-09-10 15:45:19 +00001501 if (acb == NULL)
pbrookce1a14d2006-08-07 02:38:06 +00001502 return -1;
aliguoribaf35cb2008-09-10 15:45:19 +00001503
bellard83f64092006-08-01 16:21:11 +00001504 while (async_ret == NOT_DONE) {
1505 qemu_aio_wait();
1506 }
aliguoribaf35cb2008-09-10 15:45:19 +00001507
bellard83f64092006-08-01 16:21:11 +00001508 return async_ret;
1509}
1510
1511static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1512 const uint8_t *buf, int nb_sectors)
1513{
pbrookce1a14d2006-08-07 02:38:06 +00001514 int async_ret;
1515 BlockDriverAIOCB *acb;
bellard83f64092006-08-01 16:21:11 +00001516
bellard83f64092006-08-01 16:21:11 +00001517 async_ret = NOT_DONE;
ths5fafdf22007-09-16 21:08:06 +00001518 acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
bellard83f64092006-08-01 16:21:11 +00001519 bdrv_rw_em_cb, &async_ret);
aliguoribaf35cb2008-09-10 15:45:19 +00001520 if (acb == NULL)
pbrookce1a14d2006-08-07 02:38:06 +00001521 return -1;
bellard83f64092006-08-01 16:21:11 +00001522 while (async_ret == NOT_DONE) {
1523 qemu_aio_wait();
1524 }
bellard83f64092006-08-01 16:21:11 +00001525 return async_ret;
1526}
bellardea2384d2004-08-01 21:59:26 +00001527
1528void bdrv_init(void)
1529{
aliguori3fb94d52009-03-20 18:26:12 +00001530 aio_pool_init(&vectored_aio_pool, sizeof(VectorTranslationAIOCB),
aliguoric07a9002009-03-20 18:26:03 +00001531 bdrv_aio_cancel_vector);
1532
bellardea2384d2004-08-01 21:59:26 +00001533 bdrv_register(&bdrv_raw);
bellard19cb3732006-08-19 11:45:59 +00001534 bdrv_register(&bdrv_host_device);
bellardea2384d2004-08-01 21:59:26 +00001535#ifndef _WIN32
1536 bdrv_register(&bdrv_cow);
1537#endif
1538 bdrv_register(&bdrv_qcow);
1539 bdrv_register(&bdrv_vmdk);
bellard3c565212004-09-29 21:29:14 +00001540 bdrv_register(&bdrv_cloop);
bellard585d0ed2004-12-12 11:24:44 +00001541 bdrv_register(&bdrv_dmg);
bellarda8753c32005-04-26 21:34:00 +00001542 bdrv_register(&bdrv_bochs);
bellard6a0f9e82005-04-27 20:17:58 +00001543 bdrv_register(&bdrv_vpc);
bellard712e7872005-04-28 21:09:32 +00001544 bdrv_register(&bdrv_vvfat);
bellardfaea38e2006-08-05 21:31:00 +00001545 bdrv_register(&bdrv_qcow2);
ths6ada7452007-07-31 23:28:53 +00001546 bdrv_register(&bdrv_parallels);
ths75818252008-07-03 13:41:03 +00001547 bdrv_register(&bdrv_nbd);
bellardea2384d2004-08-01 21:59:26 +00001548}
pbrookce1a14d2006-08-07 02:38:06 +00001549
aliguori6bbff9a2009-03-20 18:25:59 +00001550void aio_pool_init(AIOPool *pool, int aiocb_size,
1551 void (*cancel)(BlockDriverAIOCB *acb))
pbrookce1a14d2006-08-07 02:38:06 +00001552{
aliguori6bbff9a2009-03-20 18:25:59 +00001553 pool->aiocb_size = aiocb_size;
1554 pool->cancel = cancel;
1555 pool->free_aiocb = NULL;
1556}
1557
1558void *qemu_aio_get_pool(AIOPool *pool, BlockDriverState *bs,
1559 BlockDriverCompletionFunc *cb, void *opaque)
1560{
pbrookce1a14d2006-08-07 02:38:06 +00001561 BlockDriverAIOCB *acb;
1562
aliguori6bbff9a2009-03-20 18:25:59 +00001563 if (pool->free_aiocb) {
1564 acb = pool->free_aiocb;
1565 pool->free_aiocb = acb->next;
pbrookce1a14d2006-08-07 02:38:06 +00001566 } else {
aliguori6bbff9a2009-03-20 18:25:59 +00001567 acb = qemu_mallocz(pool->aiocb_size);
1568 acb->pool = pool;
pbrookce1a14d2006-08-07 02:38:06 +00001569 }
1570 acb->bs = bs;
1571 acb->cb = cb;
1572 acb->opaque = opaque;
1573 return acb;
1574}
1575
aliguori6bbff9a2009-03-20 18:25:59 +00001576void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1577 void *opaque)
1578{
1579 return qemu_aio_get_pool(&bs->drv->aio_pool, bs, cb, opaque);
1580}
1581
pbrookce1a14d2006-08-07 02:38:06 +00001582void qemu_aio_release(void *p)
1583{
aliguori6bbff9a2009-03-20 18:25:59 +00001584 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
1585 AIOPool *pool = acb->pool;
1586 acb->next = pool->free_aiocb;
1587 pool->free_aiocb = acb;
pbrookce1a14d2006-08-07 02:38:06 +00001588}
bellard19cb3732006-08-19 11:45:59 +00001589
1590/**************************************************************/
1591/* removable device support */
1592
1593/**
1594 * Return TRUE if the media is present
1595 */
1596int bdrv_is_inserted(BlockDriverState *bs)
1597{
1598 BlockDriver *drv = bs->drv;
1599 int ret;
1600 if (!drv)
1601 return 0;
1602 if (!drv->bdrv_is_inserted)
1603 return 1;
1604 ret = drv->bdrv_is_inserted(bs);
1605 return ret;
1606}
1607
1608/**
1609 * Return TRUE if the media changed since the last call to this
ths5fafdf22007-09-16 21:08:06 +00001610 * function. It is currently only used for floppy disks
bellard19cb3732006-08-19 11:45:59 +00001611 */
1612int bdrv_media_changed(BlockDriverState *bs)
1613{
1614 BlockDriver *drv = bs->drv;
1615 int ret;
1616
1617 if (!drv || !drv->bdrv_media_changed)
1618 ret = -ENOTSUP;
1619 else
1620 ret = drv->bdrv_media_changed(bs);
1621 if (ret == -ENOTSUP)
1622 ret = bs->media_changed;
1623 bs->media_changed = 0;
1624 return ret;
1625}
1626
1627/**
1628 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1629 */
1630void bdrv_eject(BlockDriverState *bs, int eject_flag)
1631{
1632 BlockDriver *drv = bs->drv;
1633 int ret;
1634
1635 if (!drv || !drv->bdrv_eject) {
1636 ret = -ENOTSUP;
1637 } else {
1638 ret = drv->bdrv_eject(bs, eject_flag);
1639 }
1640 if (ret == -ENOTSUP) {
1641 if (eject_flag)
1642 bdrv_close(bs);
1643 }
1644}
1645
1646int bdrv_is_locked(BlockDriverState *bs)
1647{
1648 return bs->locked;
1649}
1650
1651/**
1652 * Lock or unlock the media (if it is locked, the user won't be able
1653 * to eject it manually).
1654 */
1655void bdrv_set_locked(BlockDriverState *bs, int locked)
1656{
1657 BlockDriver *drv = bs->drv;
1658
1659 bs->locked = locked;
1660 if (drv && drv->bdrv_set_locked) {
1661 drv->bdrv_set_locked(bs, locked);
1662 }
1663}
ths985a03b2007-12-24 16:10:43 +00001664
1665/* needed for generic scsi interface */
1666
1667int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1668{
1669 BlockDriver *drv = bs->drv;
1670
1671 if (drv && drv->bdrv_ioctl)
1672 return drv->bdrv_ioctl(bs, req, buf);
1673 return -ENOTSUP;
1674}
aliguori7d780662009-03-12 19:57:08 +00001675
aliguori221f7152009-03-28 17:28:41 +00001676BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
1677 unsigned long int req, void *buf,
1678 BlockDriverCompletionFunc *cb, void *opaque)
aliguori7d780662009-03-12 19:57:08 +00001679{
aliguori221f7152009-03-28 17:28:41 +00001680 BlockDriver *drv = bs->drv;
aliguori7d780662009-03-12 19:57:08 +00001681
aliguori221f7152009-03-28 17:28:41 +00001682 if (drv && drv->bdrv_aio_ioctl)
1683 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
1684 return NULL;
aliguori7d780662009-03-12 19:57:08 +00001685}