blob: dd40b565c82d8ddd8b134c579afee02ca488816f [file] [log] [blame]
bellardfc01f7e2003-06-30 10:03:06 +00001/*
2 * QEMU System Emulator block driver
ths5fafdf22007-09-16 21:08:06 +00003 *
bellardfc01f7e2003-06-30 10:03:06 +00004 * Copyright (c) 2003 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellardfc01f7e2003-06-30 10:03:06 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
blueswir13990d092008-12-05 17:53:21 +000024#include "config-host.h"
blueswir1179a2c12009-03-08 08:23:32 +000025#ifdef HOST_BSD
blueswir13990d092008-12-05 17:53:21 +000026/* include native header before sys-queue.h */
27#include <sys/queue.h>
28#endif
29
pbrookfaf07962007-11-11 02:51:17 +000030#include "qemu-common.h"
aliguori376253e2009-03-05 23:01:23 +000031#include "monitor.h"
bellardea2384d2004-08-01 21:59:26 +000032#include "block_int.h"
bellardfc01f7e2003-06-30 10:03:06 +000033
blueswir1179a2c12009-03-08 08:23:32 +000034#ifdef HOST_BSD
bellard7674e7b2005-04-26 21:59:26 +000035#include <sys/types.h>
36#include <sys/stat.h>
37#include <sys/ioctl.h>
blueswir1c5e97232009-03-07 20:06:23 +000038#ifndef __DragonFly__
bellard7674e7b2005-04-26 21:59:26 +000039#include <sys/disk.h>
40#endif
blueswir1c5e97232009-03-07 20:06:23 +000041#endif
bellard7674e7b2005-04-26 21:59:26 +000042
aliguori49dc7682009-03-08 16:26:59 +000043#ifdef _WIN32
44#include <windows.h>
45#endif
46
bellard83f64092006-08-01 16:21:11 +000047#define SECTOR_BITS 9
48#define SECTOR_SIZE (1 << SECTOR_BITS)
bellard3b0d4f62005-10-30 18:30:10 +000049
aliguoric07a9002009-03-20 18:26:03 +000050static AIOPool vectored_aio_pool;
51
bellard90765422006-08-07 19:10:16 +000052typedef struct BlockDriverAIOCBSync {
53 BlockDriverAIOCB common;
54 QEMUBH *bh;
55 int ret;
56} BlockDriverAIOCBSync;
57
pbrookce1a14d2006-08-07 02:38:06 +000058static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
59 int64_t sector_num, uint8_t *buf, int nb_sectors,
60 BlockDriverCompletionFunc *cb, void *opaque);
61static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
62 int64_t sector_num, const uint8_t *buf, int nb_sectors,
63 BlockDriverCompletionFunc *cb, void *opaque);
bellard83f64092006-08-01 16:21:11 +000064static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
ths5fafdf22007-09-16 21:08:06 +000065static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +000066 uint8_t *buf, int nb_sectors);
67static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
68 const uint8_t *buf, int nb_sectors);
bellardec530c82006-04-25 22:36:06 +000069
blueswir17ee930d2008-09-17 19:04:14 +000070BlockDriverState *bdrv_first;
71
bellardea2384d2004-08-01 21:59:26 +000072static BlockDriver *first_drv;
73
bellard83f64092006-08-01 16:21:11 +000074int path_is_absolute(const char *path)
75{
76 const char *p;
bellard21664422007-01-07 18:22:37 +000077#ifdef _WIN32
78 /* specific case for names like: "\\.\d:" */
79 if (*path == '/' || *path == '\\')
80 return 1;
81#endif
bellard83f64092006-08-01 16:21:11 +000082 p = strchr(path, ':');
83 if (p)
84 p++;
85 else
86 p = path;
bellard3b9f94e2007-01-07 17:27:07 +000087#ifdef _WIN32
88 return (*p == '/' || *p == '\\');
89#else
90 return (*p == '/');
91#endif
bellard83f64092006-08-01 16:21:11 +000092}
93
94/* if filename is absolute, just copy it to dest. Otherwise, build a
95 path to it by considering it is relative to base_path. URL are
96 supported. */
97void path_combine(char *dest, int dest_size,
98 const char *base_path,
99 const char *filename)
100{
101 const char *p, *p1;
102 int len;
103
104 if (dest_size <= 0)
105 return;
106 if (path_is_absolute(filename)) {
107 pstrcpy(dest, dest_size, filename);
108 } else {
109 p = strchr(base_path, ':');
110 if (p)
111 p++;
112 else
113 p = base_path;
bellard3b9f94e2007-01-07 17:27:07 +0000114 p1 = strrchr(base_path, '/');
115#ifdef _WIN32
116 {
117 const char *p2;
118 p2 = strrchr(base_path, '\\');
119 if (!p1 || p2 > p1)
120 p1 = p2;
121 }
122#endif
bellard83f64092006-08-01 16:21:11 +0000123 if (p1)
124 p1++;
125 else
126 p1 = base_path;
127 if (p1 > p)
128 p = p1;
129 len = p - base_path;
130 if (len > dest_size - 1)
131 len = dest_size - 1;
132 memcpy(dest, base_path, len);
133 dest[len] = '\0';
134 pstrcat(dest, dest_size, filename);
135 }
136}
137
138
pbrook9596ebb2007-11-18 01:44:38 +0000139static void bdrv_register(BlockDriver *bdrv)
bellardea2384d2004-08-01 21:59:26 +0000140{
pbrookce1a14d2006-08-07 02:38:06 +0000141 if (!bdrv->bdrv_aio_read) {
bellard83f64092006-08-01 16:21:11 +0000142 /* add AIO emulation layer */
bellard83f64092006-08-01 16:21:11 +0000143 bdrv->bdrv_aio_read = bdrv_aio_read_em;
144 bdrv->bdrv_aio_write = bdrv_aio_write_em;
145 bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
bellard90765422006-08-07 19:10:16 +0000146 bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
aliguorieda578e2009-03-12 19:57:16 +0000147 } else if (!bdrv->bdrv_read) {
bellard83f64092006-08-01 16:21:11 +0000148 /* add synchronous IO emulation layer */
149 bdrv->bdrv_read = bdrv_read_em;
150 bdrv->bdrv_write = bdrv_write_em;
151 }
aliguori6bbff9a2009-03-20 18:25:59 +0000152 aio_pool_init(&bdrv->aio_pool, bdrv->aiocb_size, bdrv->bdrv_aio_cancel);
bellardea2384d2004-08-01 21:59:26 +0000153 bdrv->next = first_drv;
154 first_drv = bdrv;
155}
bellardb3380822004-03-14 21:38:54 +0000156
157/* create a new block device (by default it is empty) */
158BlockDriverState *bdrv_new(const char *device_name)
bellardfc01f7e2003-06-30 10:03:06 +0000159{
bellardb3380822004-03-14 21:38:54 +0000160 BlockDriverState **pbs, *bs;
161
162 bs = qemu_mallocz(sizeof(BlockDriverState));
bellardb3380822004-03-14 21:38:54 +0000163 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
bellardea2384d2004-08-01 21:59:26 +0000164 if (device_name[0] != '\0') {
165 /* insert at the end */
166 pbs = &bdrv_first;
167 while (*pbs != NULL)
168 pbs = &(*pbs)->next;
169 *pbs = bs;
170 }
bellardb3380822004-03-14 21:38:54 +0000171 return bs;
172}
173
bellardea2384d2004-08-01 21:59:26 +0000174BlockDriver *bdrv_find_format(const char *format_name)
175{
176 BlockDriver *drv1;
177 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
178 if (!strcmp(drv1->format_name, format_name))
179 return drv1;
180 }
181 return NULL;
182}
183
aliguori5eb45632009-03-28 17:55:10 +0000184int bdrv_create2(BlockDriver *drv,
185 const char *filename, int64_t size_in_sectors,
186 const char *backing_file, const char *backing_format,
187 int flags)
188{
189 if (drv->bdrv_create2)
190 return drv->bdrv_create2(filename, size_in_sectors, backing_file,
191 backing_format, flags);
192 if (drv->bdrv_create)
193 return drv->bdrv_create(filename, size_in_sectors, backing_file,
194 flags);
195 return -ENOTSUP;
196}
197
ths5fafdf22007-09-16 21:08:06 +0000198int bdrv_create(BlockDriver *drv,
bellardea2384d2004-08-01 21:59:26 +0000199 const char *filename, int64_t size_in_sectors,
200 const char *backing_file, int flags)
201{
202 if (!drv->bdrv_create)
203 return -ENOTSUP;
204 return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
205}
206
bellardd5249392004-08-03 21:14:23 +0000207#ifdef _WIN32
bellard95389c82005-12-18 18:28:15 +0000208void get_tmp_filename(char *filename, int size)
bellardd5249392004-08-03 21:14:23 +0000209{
bellard3b9f94e2007-01-07 17:27:07 +0000210 char temp_dir[MAX_PATH];
ths3b46e622007-09-17 08:09:54 +0000211
bellard3b9f94e2007-01-07 17:27:07 +0000212 GetTempPath(MAX_PATH, temp_dir);
213 GetTempFileName(temp_dir, "qem", 0, filename);
bellardd5249392004-08-03 21:14:23 +0000214}
215#else
bellard95389c82005-12-18 18:28:15 +0000216void get_tmp_filename(char *filename, int size)
bellardea2384d2004-08-01 21:59:26 +0000217{
218 int fd;
blueswir17ccfb2e2008-09-14 06:45:34 +0000219 const char *tmpdir;
bellardd5249392004-08-03 21:14:23 +0000220 /* XXX: race condition possible */
aurel320badc1e2008-03-10 00:05:34 +0000221 tmpdir = getenv("TMPDIR");
222 if (!tmpdir)
223 tmpdir = "/tmp";
224 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
bellardea2384d2004-08-01 21:59:26 +0000225 fd = mkstemp(filename);
226 close(fd);
227}
bellardd5249392004-08-03 21:14:23 +0000228#endif
bellardea2384d2004-08-01 21:59:26 +0000229
bellard19cb3732006-08-19 11:45:59 +0000230#ifdef _WIN32
bellardf45512f2006-08-23 21:40:13 +0000231static int is_windows_drive_prefix(const char *filename)
232{
233 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
234 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
235 filename[1] == ':');
236}
ths3b46e622007-09-17 08:09:54 +0000237
bellard19cb3732006-08-19 11:45:59 +0000238static int is_windows_drive(const char *filename)
239{
ths5fafdf22007-09-16 21:08:06 +0000240 if (is_windows_drive_prefix(filename) &&
bellardf45512f2006-08-23 21:40:13 +0000241 filename[2] == '\0')
bellard19cb3732006-08-19 11:45:59 +0000242 return 1;
243 if (strstart(filename, "\\\\.\\", NULL) ||
244 strstart(filename, "//./", NULL))
245 return 1;
246 return 0;
247}
248#endif
249
bellard83f64092006-08-01 16:21:11 +0000250static BlockDriver *find_protocol(const char *filename)
251{
252 BlockDriver *drv1;
253 char protocol[128];
254 int len;
255 const char *p;
bellard19cb3732006-08-19 11:45:59 +0000256
257#ifdef _WIN32
bellardf45512f2006-08-23 21:40:13 +0000258 if (is_windows_drive(filename) ||
259 is_windows_drive_prefix(filename))
bellard19cb3732006-08-19 11:45:59 +0000260 return &bdrv_raw;
261#endif
bellard83f64092006-08-01 16:21:11 +0000262 p = strchr(filename, ':');
263 if (!p)
264 return &bdrv_raw;
265 len = p - filename;
266 if (len > sizeof(protocol) - 1)
267 len = sizeof(protocol) - 1;
bellard83f64092006-08-01 16:21:11 +0000268 memcpy(protocol, filename, len);
269 protocol[len] = '\0';
270 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
ths5fafdf22007-09-16 21:08:06 +0000271 if (drv1->protocol_name &&
bellard83f64092006-08-01 16:21:11 +0000272 !strcmp(drv1->protocol_name, protocol))
273 return drv1;
274 }
275 return NULL;
276}
277
bellard7674e7b2005-04-26 21:59:26 +0000278/* XXX: force raw format if block or character device ? It would
279 simplify the BSD case */
bellardea2384d2004-08-01 21:59:26 +0000280static BlockDriver *find_image_format(const char *filename)
281{
bellard83f64092006-08-01 16:21:11 +0000282 int ret, score, score_max;
bellardea2384d2004-08-01 21:59:26 +0000283 BlockDriver *drv1, *drv;
bellard83f64092006-08-01 16:21:11 +0000284 uint8_t buf[2048];
285 BlockDriverState *bs;
ths3b46e622007-09-17 08:09:54 +0000286
bellard19cb3732006-08-19 11:45:59 +0000287 /* detect host devices. By convention, /dev/cdrom[N] is always
288 recognized as a host CDROM */
289 if (strstart(filename, "/dev/cdrom", NULL))
290 return &bdrv_host_device;
291#ifdef _WIN32
292 if (is_windows_drive(filename))
293 return &bdrv_host_device;
294#else
295 {
296 struct stat st;
ths5fafdf22007-09-16 21:08:06 +0000297 if (stat(filename, &st) >= 0 &&
bellard19cb3732006-08-19 11:45:59 +0000298 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
299 return &bdrv_host_device;
300 }
301 }
302#endif
ths3b46e622007-09-17 08:09:54 +0000303
bellard83f64092006-08-01 16:21:11 +0000304 drv = find_protocol(filename);
bellard19cb3732006-08-19 11:45:59 +0000305 /* no need to test disk image formats for vvfat */
bellard83f64092006-08-01 16:21:11 +0000306 if (drv == &bdrv_vvfat)
307 return drv;
bellard19cb3732006-08-19 11:45:59 +0000308
bellard83f64092006-08-01 16:21:11 +0000309 ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
310 if (ret < 0)
311 return NULL;
312 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
313 bdrv_delete(bs);
314 if (ret < 0) {
315 return NULL;
316 }
317
bellardea2384d2004-08-01 21:59:26 +0000318 score_max = 0;
319 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
bellard83f64092006-08-01 16:21:11 +0000320 if (drv1->bdrv_probe) {
321 score = drv1->bdrv_probe(buf, ret, filename);
322 if (score > score_max) {
323 score_max = score;
324 drv = drv1;
325 }
bellardea2384d2004-08-01 21:59:26 +0000326 }
327 }
328 return drv;
329}
330
bellard83f64092006-08-01 16:21:11 +0000331int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
bellardb3380822004-03-14 21:38:54 +0000332{
bellard83f64092006-08-01 16:21:11 +0000333 BlockDriverState *bs;
334 int ret;
335
336 bs = bdrv_new("");
bellard83f64092006-08-01 16:21:11 +0000337 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
338 if (ret < 0) {
339 bdrv_delete(bs);
340 return ret;
bellard3b0d4f62005-10-30 18:30:10 +0000341 }
aliguori71d07702009-03-03 17:37:16 +0000342 bs->growable = 1;
bellard83f64092006-08-01 16:21:11 +0000343 *pbs = bs;
344 return 0;
bellardea2384d2004-08-01 21:59:26 +0000345}
bellardfc01f7e2003-06-30 10:03:06 +0000346
bellard83f64092006-08-01 16:21:11 +0000347int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
348{
349 return bdrv_open2(bs, filename, flags, NULL);
350}
351
352int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
bellardea2384d2004-08-01 21:59:26 +0000353 BlockDriver *drv)
354{
bellard83f64092006-08-01 16:21:11 +0000355 int ret, open_flags;
thseb5c8512007-02-11 15:06:09 +0000356 char tmp_filename[PATH_MAX];
357 char backing_filename[PATH_MAX];
ths3b46e622007-09-17 08:09:54 +0000358
bellardea2384d2004-08-01 21:59:26 +0000359 bs->read_only = 0;
360 bs->is_temporary = 0;
361 bs->encrypted = 0;
aliguoric0f4ce72009-03-05 23:01:01 +0000362 bs->valid_key = 0;
bellard712e7872005-04-28 21:09:32 +0000363
bellard83f64092006-08-01 16:21:11 +0000364 if (flags & BDRV_O_SNAPSHOT) {
bellardea2384d2004-08-01 21:59:26 +0000365 BlockDriverState *bs1;
366 int64_t total_size;
aliguori7c96d462008-09-12 17:54:13 +0000367 int is_protocol = 0;
ths3b46e622007-09-17 08:09:54 +0000368
bellardea2384d2004-08-01 21:59:26 +0000369 /* if snapshot, we create a temporary backing file and open it
370 instead of opening 'filename' directly */
371
372 /* if there is a backing file, use it */
373 bs1 = bdrv_new("");
aliguori5eb45632009-03-28 17:55:10 +0000374 ret = bdrv_open2(bs1, filename, 0, drv);
aliguori51d7c002009-03-05 23:00:29 +0000375 if (ret < 0) {
bellardea2384d2004-08-01 21:59:26 +0000376 bdrv_delete(bs1);
aliguori51d7c002009-03-05 23:00:29 +0000377 return ret;
bellardea2384d2004-08-01 21:59:26 +0000378 }
bellard83f64092006-08-01 16:21:11 +0000379 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
aliguori7c96d462008-09-12 17:54:13 +0000380
381 if (bs1->drv && bs1->drv->protocol_name)
382 is_protocol = 1;
383
bellardea2384d2004-08-01 21:59:26 +0000384 bdrv_delete(bs1);
ths3b46e622007-09-17 08:09:54 +0000385
bellardea2384d2004-08-01 21:59:26 +0000386 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
aliguori7c96d462008-09-12 17:54:13 +0000387
388 /* Real path is meaningless for protocols */
389 if (is_protocol)
390 snprintf(backing_filename, sizeof(backing_filename),
391 "%s", filename);
392 else
393 realpath(filename, backing_filename);
394
aliguori5eb45632009-03-28 17:55:10 +0000395 ret = bdrv_create2(&bdrv_qcow2, tmp_filename,
396 total_size, backing_filename,
397 (drv ? drv->format_name : NULL), 0);
aliguori51d7c002009-03-05 23:00:29 +0000398 if (ret < 0) {
399 return ret;
bellardea2384d2004-08-01 21:59:26 +0000400 }
401 filename = tmp_filename;
aliguori5eb45632009-03-28 17:55:10 +0000402 drv = &bdrv_qcow2;
bellardea2384d2004-08-01 21:59:26 +0000403 bs->is_temporary = 1;
404 }
bellard712e7872005-04-28 21:09:32 +0000405
bellardea2384d2004-08-01 21:59:26 +0000406 pstrcpy(bs->filename, sizeof(bs->filename), filename);
bellard83f64092006-08-01 16:21:11 +0000407 if (flags & BDRV_O_FILE) {
408 drv = find_protocol(filename);
aliguori51d7c002009-03-05 23:00:29 +0000409 } else if (!drv) {
410 drv = find_image_format(filename);
411 }
412 if (!drv) {
413 ret = -ENOENT;
414 goto unlink_and_fail;
bellardea2384d2004-08-01 21:59:26 +0000415 }
416 bs->drv = drv;
417 bs->opaque = qemu_mallocz(drv->instance_size);
bellard83f64092006-08-01 16:21:11 +0000418 /* Note: for compatibility, we open disk image files as RDWR, and
419 RDONLY as fallback */
420 if (!(flags & BDRV_O_FILE))
aliguori9f7965c2008-10-14 14:42:54 +0000421 open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK);
bellard83f64092006-08-01 16:21:11 +0000422 else
423 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
424 ret = drv->bdrv_open(bs, filename, open_flags);
aurel32a0a83532008-10-13 21:08:34 +0000425 if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
aliguori9f7965c2008-10-14 14:42:54 +0000426 ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
bellard83f64092006-08-01 16:21:11 +0000427 bs->read_only = 1;
428 }
bellardea2384d2004-08-01 21:59:26 +0000429 if (ret < 0) {
430 qemu_free(bs->opaque);
bellard6b21b972006-08-23 21:14:37 +0000431 bs->opaque = NULL;
432 bs->drv = NULL;
aliguori51d7c002009-03-05 23:00:29 +0000433 unlink_and_fail:
434 if (bs->is_temporary)
435 unlink(filename);
bellard83f64092006-08-01 16:21:11 +0000436 return ret;
bellardea2384d2004-08-01 21:59:26 +0000437 }
bellardd15a7712006-08-06 13:35:09 +0000438 if (drv->bdrv_getlength) {
439 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
440 }
bellardea2384d2004-08-01 21:59:26 +0000441#ifndef _WIN32
442 if (bs->is_temporary) {
443 unlink(filename);
444 }
445#endif
bellard83f64092006-08-01 16:21:11 +0000446 if (bs->backing_file[0] != '\0') {
bellardea2384d2004-08-01 21:59:26 +0000447 /* if there is a backing file, use it */
aliguori5eb45632009-03-28 17:55:10 +0000448 BlockDriver *back_drv = NULL;
bellardea2384d2004-08-01 21:59:26 +0000449 bs->backing_hd = bdrv_new("");
bellard83f64092006-08-01 16:21:11 +0000450 path_combine(backing_filename, sizeof(backing_filename),
451 filename, bs->backing_file);
aliguori5eb45632009-03-28 17:55:10 +0000452 if (bs->backing_format[0] != '\0')
453 back_drv = bdrv_find_format(bs->backing_format);
454 ret = bdrv_open2(bs->backing_hd, backing_filename, open_flags,
455 back_drv);
aliguori51d7c002009-03-05 23:00:29 +0000456 if (ret < 0) {
457 bdrv_close(bs);
458 return ret;
459 }
bellardea2384d2004-08-01 21:59:26 +0000460 }
461
aliguoribb5fc202009-03-05 23:01:15 +0000462 if (!bdrv_key_required(bs)) {
463 /* call the change callback */
464 bs->media_changed = 1;
465 if (bs->change_cb)
466 bs->change_cb(bs->change_opaque);
467 }
bellardb3380822004-03-14 21:38:54 +0000468 return 0;
bellardfc01f7e2003-06-30 10:03:06 +0000469}
470
471void bdrv_close(BlockDriverState *bs)
472{
bellard19cb3732006-08-19 11:45:59 +0000473 if (bs->drv) {
bellardea2384d2004-08-01 21:59:26 +0000474 if (bs->backing_hd)
475 bdrv_delete(bs->backing_hd);
476 bs->drv->bdrv_close(bs);
477 qemu_free(bs->opaque);
478#ifdef _WIN32
479 if (bs->is_temporary) {
480 unlink(bs->filename);
481 }
bellard67b915a2004-03-31 23:37:16 +0000482#endif
bellardea2384d2004-08-01 21:59:26 +0000483 bs->opaque = NULL;
484 bs->drv = NULL;
bellardb3380822004-03-14 21:38:54 +0000485
486 /* call the change callback */
bellard19cb3732006-08-19 11:45:59 +0000487 bs->media_changed = 1;
bellardb3380822004-03-14 21:38:54 +0000488 if (bs->change_cb)
489 bs->change_cb(bs->change_opaque);
490 }
491}
492
493void bdrv_delete(BlockDriverState *bs)
494{
aurel3234c6f052008-04-08 19:51:21 +0000495 BlockDriverState **pbs;
496
497 pbs = &bdrv_first;
498 while (*pbs != bs && *pbs != NULL)
499 pbs = &(*pbs)->next;
500 if (*pbs == bs)
501 *pbs = bs->next;
502
bellardb3380822004-03-14 21:38:54 +0000503 bdrv_close(bs);
504 qemu_free(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000505}
506
bellard33e39632003-07-06 17:15:21 +0000507/* commit COW file into the raw image */
508int bdrv_commit(BlockDriverState *bs)
509{
bellard19cb3732006-08-19 11:45:59 +0000510 BlockDriver *drv = bs->drv;
bellard83f64092006-08-01 16:21:11 +0000511 int64_t i, total_sectors;
bellardea2384d2004-08-01 21:59:26 +0000512 int n, j;
513 unsigned char sector[512];
bellard33e39632003-07-06 17:15:21 +0000514
bellard19cb3732006-08-19 11:45:59 +0000515 if (!drv)
516 return -ENOMEDIUM;
bellard33e39632003-07-06 17:15:21 +0000517
518 if (bs->read_only) {
bellardea2384d2004-08-01 21:59:26 +0000519 return -EACCES;
bellard33e39632003-07-06 17:15:21 +0000520 }
521
bellardea2384d2004-08-01 21:59:26 +0000522 if (!bs->backing_hd) {
523 return -ENOTSUP;
bellard33e39632003-07-06 17:15:21 +0000524 }
bellardea2384d2004-08-01 21:59:26 +0000525
bellard83f64092006-08-01 16:21:11 +0000526 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
527 for (i = 0; i < total_sectors;) {
bellard19cb3732006-08-19 11:45:59 +0000528 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
bellardea2384d2004-08-01 21:59:26 +0000529 for(j = 0; j < n; j++) {
530 if (bdrv_read(bs, i, sector, 1) != 0) {
531 return -EIO;
532 }
533
534 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
535 return -EIO;
536 }
537 i++;
538 }
539 } else {
540 i += n;
541 }
542 }
bellard95389c82005-12-18 18:28:15 +0000543
bellard19cb3732006-08-19 11:45:59 +0000544 if (drv->bdrv_make_empty)
545 return drv->bdrv_make_empty(bs);
bellard95389c82005-12-18 18:28:15 +0000546
bellard33e39632003-07-06 17:15:21 +0000547 return 0;
548}
549
aliguori71d07702009-03-03 17:37:16 +0000550static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
551 size_t size)
552{
553 int64_t len;
554
555 if (!bdrv_is_inserted(bs))
556 return -ENOMEDIUM;
557
558 if (bs->growable)
559 return 0;
560
561 len = bdrv_getlength(bs);
562
563 if ((offset + size) > len)
564 return -EIO;
565
566 return 0;
567}
568
569static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
570 int nb_sectors)
571{
aliguori999dec52009-03-29 01:31:48 +0000572 return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512);
aliguori71d07702009-03-03 17:37:16 +0000573}
574
bellard19cb3732006-08-19 11:45:59 +0000575/* return < 0 if error. See bdrv_write() for the return codes */
ths5fafdf22007-09-16 21:08:06 +0000576int bdrv_read(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000577 uint8_t *buf, int nb_sectors)
578{
bellardea2384d2004-08-01 21:59:26 +0000579 BlockDriver *drv = bs->drv;
580
bellard19cb3732006-08-19 11:45:59 +0000581 if (!drv)
582 return -ENOMEDIUM;
aliguori71d07702009-03-03 17:37:16 +0000583 if (bdrv_check_request(bs, sector_num, nb_sectors))
584 return -EIO;
bellardb3380822004-03-14 21:38:54 +0000585
aliguorieda578e2009-03-12 19:57:16 +0000586 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
bellardfc01f7e2003-06-30 10:03:06 +0000587}
588
ths5fafdf22007-09-16 21:08:06 +0000589/* Return < 0 if error. Important errors are:
bellard19cb3732006-08-19 11:45:59 +0000590 -EIO generic I/O error (may happen for all errors)
591 -ENOMEDIUM No media inserted.
592 -EINVAL Invalid sector number or nb_sectors
593 -EACCES Trying to write a read-only device
594*/
ths5fafdf22007-09-16 21:08:06 +0000595int bdrv_write(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000596 const uint8_t *buf, int nb_sectors)
597{
bellard83f64092006-08-01 16:21:11 +0000598 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +0000599 if (!bs->drv)
600 return -ENOMEDIUM;
bellard0849bf02003-06-30 23:17:31 +0000601 if (bs->read_only)
bellard19cb3732006-08-19 11:45:59 +0000602 return -EACCES;
aliguori71d07702009-03-03 17:37:16 +0000603 if (bdrv_check_request(bs, sector_num, nb_sectors))
604 return -EIO;
605
aliguori42fb2802009-01-15 20:43:39 +0000606 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
bellard83f64092006-08-01 16:21:11 +0000607}
608
aliguorieda578e2009-03-12 19:57:16 +0000609int bdrv_pread(BlockDriverState *bs, int64_t offset,
610 void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000611{
bellard83f64092006-08-01 16:21:11 +0000612 uint8_t tmp_buf[SECTOR_SIZE];
613 int len, nb_sectors, count;
614 int64_t sector_num;
615
616 count = count1;
617 /* first read to align to sector start */
618 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
619 if (len > count)
620 len = count;
621 sector_num = offset >> SECTOR_BITS;
622 if (len > 0) {
623 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
624 return -EIO;
625 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
626 count -= len;
627 if (count == 0)
628 return count1;
629 sector_num++;
630 buf += len;
631 }
632
633 /* read the sectors "in place" */
634 nb_sectors = count >> SECTOR_BITS;
635 if (nb_sectors > 0) {
636 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
637 return -EIO;
638 sector_num += nb_sectors;
639 len = nb_sectors << SECTOR_BITS;
640 buf += len;
641 count -= len;
642 }
643
644 /* add data from the last sector */
645 if (count > 0) {
646 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
647 return -EIO;
648 memcpy(buf, tmp_buf, count);
649 }
650 return count1;
651}
652
aliguorieda578e2009-03-12 19:57:16 +0000653int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
654 const void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000655{
bellard83f64092006-08-01 16:21:11 +0000656 uint8_t tmp_buf[SECTOR_SIZE];
657 int len, nb_sectors, count;
658 int64_t sector_num;
659
660 count = count1;
661 /* first write to align to sector start */
662 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
663 if (len > count)
664 len = count;
665 sector_num = offset >> SECTOR_BITS;
666 if (len > 0) {
667 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
668 return -EIO;
669 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
670 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
671 return -EIO;
672 count -= len;
673 if (count == 0)
674 return count1;
675 sector_num++;
676 buf += len;
677 }
678
679 /* write the sectors "in place" */
680 nb_sectors = count >> SECTOR_BITS;
681 if (nb_sectors > 0) {
682 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
683 return -EIO;
684 sector_num += nb_sectors;
685 len = nb_sectors << SECTOR_BITS;
686 buf += len;
687 count -= len;
688 }
689
690 /* add data from the last sector */
691 if (count > 0) {
692 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
693 return -EIO;
694 memcpy(tmp_buf, buf, count);
695 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
696 return -EIO;
697 }
698 return count1;
699}
bellard83f64092006-08-01 16:21:11 +0000700
701/**
bellard83f64092006-08-01 16:21:11 +0000702 * Truncate file to 'offset' bytes (needed only for file protocols)
703 */
704int bdrv_truncate(BlockDriverState *bs, int64_t offset)
705{
706 BlockDriver *drv = bs->drv;
707 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000708 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000709 if (!drv->bdrv_truncate)
710 return -ENOTSUP;
711 return drv->bdrv_truncate(bs, offset);
712}
713
714/**
715 * Length of a file in bytes. Return < 0 if error or unknown.
716 */
717int64_t bdrv_getlength(BlockDriverState *bs)
718{
719 BlockDriver *drv = bs->drv;
720 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000721 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000722 if (!drv->bdrv_getlength) {
723 /* legacy mode */
724 return bs->total_sectors * SECTOR_SIZE;
725 }
726 return drv->bdrv_getlength(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000727}
728
bellard19cb3732006-08-19 11:45:59 +0000729/* return 0 as number of sectors if no device present or error */
ths96b8f132007-12-17 01:35:20 +0000730void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
bellardfc01f7e2003-06-30 10:03:06 +0000731{
bellard19cb3732006-08-19 11:45:59 +0000732 int64_t length;
733 length = bdrv_getlength(bs);
734 if (length < 0)
735 length = 0;
736 else
737 length = length >> SECTOR_BITS;
738 *nb_sectors_ptr = length;
bellardfc01f7e2003-06-30 10:03:06 +0000739}
bellardcf989512004-02-16 21:56:36 +0000740
aliguorif3d54fc2008-11-25 21:50:24 +0000741struct partition {
742 uint8_t boot_ind; /* 0x80 - active */
743 uint8_t head; /* starting head */
744 uint8_t sector; /* starting sector */
745 uint8_t cyl; /* starting cylinder */
746 uint8_t sys_ind; /* What partition type */
747 uint8_t end_head; /* end head */
748 uint8_t end_sector; /* end sector */
749 uint8_t end_cyl; /* end cylinder */
750 uint32_t start_sect; /* starting sector counting from 0 */
751 uint32_t nr_sects; /* nr of sectors in partition */
752} __attribute__((packed));
753
754/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
755static int guess_disk_lchs(BlockDriverState *bs,
756 int *pcylinders, int *pheads, int *psectors)
757{
758 uint8_t buf[512];
759 int ret, i, heads, sectors, cylinders;
760 struct partition *p;
761 uint32_t nr_sects;
blueswir1a38131b2008-12-05 17:56:40 +0000762 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +0000763
764 bdrv_get_geometry(bs, &nb_sectors);
765
766 ret = bdrv_read(bs, 0, buf, 1);
767 if (ret < 0)
768 return -1;
769 /* test msdos magic */
770 if (buf[510] != 0x55 || buf[511] != 0xaa)
771 return -1;
772 for(i = 0; i < 4; i++) {
773 p = ((struct partition *)(buf + 0x1be)) + i;
774 nr_sects = le32_to_cpu(p->nr_sects);
775 if (nr_sects && p->end_head) {
776 /* We make the assumption that the partition terminates on
777 a cylinder boundary */
778 heads = p->end_head + 1;
779 sectors = p->end_sector & 63;
780 if (sectors == 0)
781 continue;
782 cylinders = nb_sectors / (heads * sectors);
783 if (cylinders < 1 || cylinders > 16383)
784 continue;
785 *pheads = heads;
786 *psectors = sectors;
787 *pcylinders = cylinders;
788#if 0
789 printf("guessed geometry: LCHS=%d %d %d\n",
790 cylinders, heads, sectors);
791#endif
792 return 0;
793 }
794 }
795 return -1;
796}
797
798void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
799{
800 int translation, lba_detected = 0;
801 int cylinders, heads, secs;
blueswir1a38131b2008-12-05 17:56:40 +0000802 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +0000803
804 /* if a geometry hint is available, use it */
805 bdrv_get_geometry(bs, &nb_sectors);
806 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
807 translation = bdrv_get_translation_hint(bs);
808 if (cylinders != 0) {
809 *pcyls = cylinders;
810 *pheads = heads;
811 *psecs = secs;
812 } else {
813 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
814 if (heads > 16) {
815 /* if heads > 16, it means that a BIOS LBA
816 translation was active, so the default
817 hardware geometry is OK */
818 lba_detected = 1;
819 goto default_geometry;
820 } else {
821 *pcyls = cylinders;
822 *pheads = heads;
823 *psecs = secs;
824 /* disable any translation to be in sync with
825 the logical geometry */
826 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
827 bdrv_set_translation_hint(bs,
828 BIOS_ATA_TRANSLATION_NONE);
829 }
830 }
831 } else {
832 default_geometry:
833 /* if no geometry, use a standard physical disk geometry */
834 cylinders = nb_sectors / (16 * 63);
835
836 if (cylinders > 16383)
837 cylinders = 16383;
838 else if (cylinders < 2)
839 cylinders = 2;
840 *pcyls = cylinders;
841 *pheads = 16;
842 *psecs = 63;
843 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
844 if ((*pcyls * *pheads) <= 131072) {
845 bdrv_set_translation_hint(bs,
846 BIOS_ATA_TRANSLATION_LARGE);
847 } else {
848 bdrv_set_translation_hint(bs,
849 BIOS_ATA_TRANSLATION_LBA);
850 }
851 }
852 }
853 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
854 }
855}
856
ths5fafdf22007-09-16 21:08:06 +0000857void bdrv_set_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000858 int cyls, int heads, int secs)
859{
860 bs->cyls = cyls;
861 bs->heads = heads;
862 bs->secs = secs;
863}
864
865void bdrv_set_type_hint(BlockDriverState *bs, int type)
866{
867 bs->type = type;
868 bs->removable = ((type == BDRV_TYPE_CDROM ||
869 type == BDRV_TYPE_FLOPPY));
870}
871
bellard46d47672004-11-16 01:45:27 +0000872void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
873{
874 bs->translation = translation;
875}
876
ths5fafdf22007-09-16 21:08:06 +0000877void bdrv_get_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000878 int *pcyls, int *pheads, int *psecs)
879{
880 *pcyls = bs->cyls;
881 *pheads = bs->heads;
882 *psecs = bs->secs;
883}
884
885int bdrv_get_type_hint(BlockDriverState *bs)
886{
887 return bs->type;
888}
889
bellard46d47672004-11-16 01:45:27 +0000890int bdrv_get_translation_hint(BlockDriverState *bs)
891{
892 return bs->translation;
893}
894
bellardb3380822004-03-14 21:38:54 +0000895int bdrv_is_removable(BlockDriverState *bs)
896{
897 return bs->removable;
898}
899
900int bdrv_is_read_only(BlockDriverState *bs)
901{
902 return bs->read_only;
903}
904
ths985a03b2007-12-24 16:10:43 +0000905int bdrv_is_sg(BlockDriverState *bs)
906{
907 return bs->sg;
908}
909
bellard19cb3732006-08-19 11:45:59 +0000910/* XXX: no longer used */
ths5fafdf22007-09-16 21:08:06 +0000911void bdrv_set_change_cb(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000912 void (*change_cb)(void *opaque), void *opaque)
913{
914 bs->change_cb = change_cb;
915 bs->change_opaque = opaque;
916}
917
bellardea2384d2004-08-01 21:59:26 +0000918int bdrv_is_encrypted(BlockDriverState *bs)
919{
920 if (bs->backing_hd && bs->backing_hd->encrypted)
921 return 1;
922 return bs->encrypted;
923}
924
aliguoric0f4ce72009-03-05 23:01:01 +0000925int bdrv_key_required(BlockDriverState *bs)
926{
927 BlockDriverState *backing_hd = bs->backing_hd;
928
929 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
930 return 1;
931 return (bs->encrypted && !bs->valid_key);
932}
933
bellardea2384d2004-08-01 21:59:26 +0000934int bdrv_set_key(BlockDriverState *bs, const char *key)
935{
936 int ret;
937 if (bs->backing_hd && bs->backing_hd->encrypted) {
938 ret = bdrv_set_key(bs->backing_hd, key);
939 if (ret < 0)
940 return ret;
941 if (!bs->encrypted)
942 return 0;
943 }
944 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
945 return -1;
aliguoric0f4ce72009-03-05 23:01:01 +0000946 ret = bs->drv->bdrv_set_key(bs, key);
aliguoribb5fc202009-03-05 23:01:15 +0000947 if (ret < 0) {
948 bs->valid_key = 0;
949 } else if (!bs->valid_key) {
950 bs->valid_key = 1;
951 /* call the change callback now, we skipped it on open */
952 bs->media_changed = 1;
953 if (bs->change_cb)
954 bs->change_cb(bs->change_opaque);
955 }
aliguoric0f4ce72009-03-05 23:01:01 +0000956 return ret;
bellardea2384d2004-08-01 21:59:26 +0000957}
958
959void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
960{
bellard19cb3732006-08-19 11:45:59 +0000961 if (!bs->drv) {
bellardea2384d2004-08-01 21:59:26 +0000962 buf[0] = '\0';
963 } else {
964 pstrcpy(buf, buf_size, bs->drv->format_name);
965 }
966}
967
ths5fafdf22007-09-16 21:08:06 +0000968void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
bellardea2384d2004-08-01 21:59:26 +0000969 void *opaque)
970{
971 BlockDriver *drv;
972
973 for (drv = first_drv; drv != NULL; drv = drv->next) {
974 it(opaque, drv->format_name);
975 }
976}
977
bellardb3380822004-03-14 21:38:54 +0000978BlockDriverState *bdrv_find(const char *name)
979{
980 BlockDriverState *bs;
981
982 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
983 if (!strcmp(name, bs->device_name))
984 return bs;
985 }
986 return NULL;
987}
988
aliguori51de9762009-03-05 23:00:43 +0000989void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
bellard81d09122004-07-14 17:21:37 +0000990{
991 BlockDriverState *bs;
992
993 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori51de9762009-03-05 23:00:43 +0000994 it(opaque, bs);
bellard81d09122004-07-14 17:21:37 +0000995 }
996}
997
bellardea2384d2004-08-01 21:59:26 +0000998const char *bdrv_get_device_name(BlockDriverState *bs)
999{
1000 return bs->device_name;
1001}
1002
pbrook7a6cba62006-06-04 11:39:07 +00001003void bdrv_flush(BlockDriverState *bs)
1004{
aliguori081501d2009-03-29 01:31:51 +00001005 if (!bs->drv)
1006 return;
pbrook7a6cba62006-06-04 11:39:07 +00001007 if (bs->drv->bdrv_flush)
1008 bs->drv->bdrv_flush(bs);
1009 if (bs->backing_hd)
1010 bdrv_flush(bs->backing_hd);
1011}
1012
aliguoric6ca28d2008-10-06 13:55:43 +00001013void bdrv_flush_all(void)
1014{
1015 BlockDriverState *bs;
1016
1017 for (bs = bdrv_first; bs != NULL; bs = bs->next)
1018 if (bs->drv && !bdrv_is_read_only(bs) &&
1019 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1020 bdrv_flush(bs);
1021}
1022
thsf58c7b32008-06-05 21:53:49 +00001023/*
1024 * Returns true iff the specified sector is present in the disk image. Drivers
1025 * not implementing the functionality are assumed to not support backing files,
1026 * hence all their sectors are reported as allocated.
1027 *
1028 * 'pnum' is set to the number of sectors (including and immediately following
1029 * the specified sector) that are known to be in the same
1030 * allocated/unallocated state.
1031 *
1032 * 'nb_sectors' is the max value 'pnum' should be set to.
1033 */
1034int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1035 int *pnum)
1036{
1037 int64_t n;
1038 if (!bs->drv->bdrv_is_allocated) {
1039 if (sector_num >= bs->total_sectors) {
1040 *pnum = 0;
1041 return 0;
1042 }
1043 n = bs->total_sectors - sector_num;
1044 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1045 return 1;
1046 }
1047 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1048}
1049
aliguori376253e2009-03-05 23:01:23 +00001050void bdrv_info(Monitor *mon)
bellardb3380822004-03-14 21:38:54 +00001051{
1052 BlockDriverState *bs;
1053
1054 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori376253e2009-03-05 23:01:23 +00001055 monitor_printf(mon, "%s:", bs->device_name);
1056 monitor_printf(mon, " type=");
bellardb3380822004-03-14 21:38:54 +00001057 switch(bs->type) {
1058 case BDRV_TYPE_HD:
aliguori376253e2009-03-05 23:01:23 +00001059 monitor_printf(mon, "hd");
bellardb3380822004-03-14 21:38:54 +00001060 break;
1061 case BDRV_TYPE_CDROM:
aliguori376253e2009-03-05 23:01:23 +00001062 monitor_printf(mon, "cdrom");
bellardb3380822004-03-14 21:38:54 +00001063 break;
1064 case BDRV_TYPE_FLOPPY:
aliguori376253e2009-03-05 23:01:23 +00001065 monitor_printf(mon, "floppy");
bellardb3380822004-03-14 21:38:54 +00001066 break;
1067 }
aliguori376253e2009-03-05 23:01:23 +00001068 monitor_printf(mon, " removable=%d", bs->removable);
bellardb3380822004-03-14 21:38:54 +00001069 if (bs->removable) {
aliguori376253e2009-03-05 23:01:23 +00001070 monitor_printf(mon, " locked=%d", bs->locked);
bellardb3380822004-03-14 21:38:54 +00001071 }
bellard19cb3732006-08-19 11:45:59 +00001072 if (bs->drv) {
aliguori376253e2009-03-05 23:01:23 +00001073 monitor_printf(mon, " file=");
1074 monitor_print_filename(mon, bs->filename);
thsfef30742006-12-22 14:11:32 +00001075 if (bs->backing_file[0] != '\0') {
aliguori376253e2009-03-05 23:01:23 +00001076 monitor_printf(mon, " backing_file=");
1077 monitor_print_filename(mon, bs->backing_file);
1078 }
1079 monitor_printf(mon, " ro=%d", bs->read_only);
1080 monitor_printf(mon, " drv=%s", bs->drv->format_name);
1081 monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs));
bellardb3380822004-03-14 21:38:54 +00001082 } else {
aliguori376253e2009-03-05 23:01:23 +00001083 monitor_printf(mon, " [not inserted]");
bellardb3380822004-03-14 21:38:54 +00001084 }
aliguori376253e2009-03-05 23:01:23 +00001085 monitor_printf(mon, "\n");
bellardb3380822004-03-14 21:38:54 +00001086 }
1087}
thsa36e69d2007-12-02 05:18:19 +00001088
1089/* The "info blockstats" command. */
aliguori376253e2009-03-05 23:01:23 +00001090void bdrv_info_stats(Monitor *mon)
thsa36e69d2007-12-02 05:18:19 +00001091{
1092 BlockDriverState *bs;
1093
1094 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori376253e2009-03-05 23:01:23 +00001095 monitor_printf(mon, "%s:"
1096 " rd_bytes=%" PRIu64
1097 " wr_bytes=%" PRIu64
1098 " rd_operations=%" PRIu64
1099 " wr_operations=%" PRIu64
aliguoriebf53fc2009-03-11 20:05:29 +00001100 "\n",
aliguori376253e2009-03-05 23:01:23 +00001101 bs->device_name,
1102 bs->rd_bytes, bs->wr_bytes,
1103 bs->rd_ops, bs->wr_ops);
thsa36e69d2007-12-02 05:18:19 +00001104 }
1105}
bellardea2384d2004-08-01 21:59:26 +00001106
aliguori045df332009-03-05 23:00:48 +00001107const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1108{
1109 if (bs->backing_hd && bs->backing_hd->encrypted)
1110 return bs->backing_file;
1111 else if (bs->encrypted)
1112 return bs->filename;
1113 else
1114 return NULL;
1115}
1116
ths5fafdf22007-09-16 21:08:06 +00001117void bdrv_get_backing_filename(BlockDriverState *bs,
bellard83f64092006-08-01 16:21:11 +00001118 char *filename, int filename_size)
bellardea2384d2004-08-01 21:59:26 +00001119{
bellard83f64092006-08-01 16:21:11 +00001120 if (!bs->backing_hd) {
1121 pstrcpy(filename, filename_size, "");
1122 } else {
1123 pstrcpy(filename, filename_size, bs->backing_file);
1124 }
bellardea2384d2004-08-01 21:59:26 +00001125}
1126
ths5fafdf22007-09-16 21:08:06 +00001127int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
bellardfaea38e2006-08-05 21:31:00 +00001128 const uint8_t *buf, int nb_sectors)
1129{
1130 BlockDriver *drv = bs->drv;
1131 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001132 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001133 if (!drv->bdrv_write_compressed)
1134 return -ENOTSUP;
1135 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1136}
ths3b46e622007-09-17 08:09:54 +00001137
bellardfaea38e2006-08-05 21:31:00 +00001138int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1139{
1140 BlockDriver *drv = bs->drv;
1141 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001142 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001143 if (!drv->bdrv_get_info)
1144 return -ENOTSUP;
1145 memset(bdi, 0, sizeof(*bdi));
1146 return drv->bdrv_get_info(bs, bdi);
1147}
1148
aliguori178e08a2009-04-05 19:10:55 +00001149int bdrv_put_buffer(BlockDriverState *bs, const uint8_t *buf, int64_t pos, int size)
1150{
1151 BlockDriver *drv = bs->drv;
1152 if (!drv)
1153 return -ENOMEDIUM;
1154 if (!drv->bdrv_put_buffer)
1155 return -ENOTSUP;
1156 return drv->bdrv_put_buffer(bs, buf, pos, size);
1157}
1158
1159int bdrv_get_buffer(BlockDriverState *bs, uint8_t *buf, int64_t pos, int size)
1160{
1161 BlockDriver *drv = bs->drv;
1162 if (!drv)
1163 return -ENOMEDIUM;
1164 if (!drv->bdrv_get_buffer)
1165 return -ENOTSUP;
1166 return drv->bdrv_get_buffer(bs, buf, pos, size);
1167}
1168
bellardfaea38e2006-08-05 21:31:00 +00001169/**************************************************************/
1170/* handling of snapshots */
1171
ths5fafdf22007-09-16 21:08:06 +00001172int bdrv_snapshot_create(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001173 QEMUSnapshotInfo *sn_info)
1174{
1175 BlockDriver *drv = bs->drv;
1176 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001177 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001178 if (!drv->bdrv_snapshot_create)
1179 return -ENOTSUP;
1180 return drv->bdrv_snapshot_create(bs, sn_info);
1181}
1182
ths5fafdf22007-09-16 21:08:06 +00001183int bdrv_snapshot_goto(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001184 const char *snapshot_id)
1185{
1186 BlockDriver *drv = bs->drv;
1187 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001188 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001189 if (!drv->bdrv_snapshot_goto)
1190 return -ENOTSUP;
1191 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1192}
1193
1194int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1195{
1196 BlockDriver *drv = bs->drv;
1197 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001198 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001199 if (!drv->bdrv_snapshot_delete)
1200 return -ENOTSUP;
1201 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1202}
1203
ths5fafdf22007-09-16 21:08:06 +00001204int bdrv_snapshot_list(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001205 QEMUSnapshotInfo **psn_info)
1206{
1207 BlockDriver *drv = bs->drv;
1208 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001209 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001210 if (!drv->bdrv_snapshot_list)
1211 return -ENOTSUP;
1212 return drv->bdrv_snapshot_list(bs, psn_info);
1213}
1214
1215#define NB_SUFFIXES 4
1216
1217char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1218{
1219 static const char suffixes[NB_SUFFIXES] = "KMGT";
1220 int64_t base;
1221 int i;
1222
1223 if (size <= 999) {
1224 snprintf(buf, buf_size, "%" PRId64, size);
1225 } else {
1226 base = 1024;
1227 for(i = 0; i < NB_SUFFIXES; i++) {
1228 if (size < (10 * base)) {
ths5fafdf22007-09-16 21:08:06 +00001229 snprintf(buf, buf_size, "%0.1f%c",
bellardfaea38e2006-08-05 21:31:00 +00001230 (double)size / base,
1231 suffixes[i]);
1232 break;
1233 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
ths5fafdf22007-09-16 21:08:06 +00001234 snprintf(buf, buf_size, "%" PRId64 "%c",
bellardfaea38e2006-08-05 21:31:00 +00001235 ((size + (base >> 1)) / base),
1236 suffixes[i]);
1237 break;
1238 }
1239 base = base * 1024;
1240 }
1241 }
1242 return buf;
1243}
1244
1245char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1246{
1247 char buf1[128], date_buf[128], clock_buf[128];
bellard3b9f94e2007-01-07 17:27:07 +00001248#ifdef _WIN32
1249 struct tm *ptm;
1250#else
bellardfaea38e2006-08-05 21:31:00 +00001251 struct tm tm;
bellard3b9f94e2007-01-07 17:27:07 +00001252#endif
bellardfaea38e2006-08-05 21:31:00 +00001253 time_t ti;
1254 int64_t secs;
1255
1256 if (!sn) {
ths5fafdf22007-09-16 21:08:06 +00001257 snprintf(buf, buf_size,
1258 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001259 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1260 } else {
1261 ti = sn->date_sec;
bellard3b9f94e2007-01-07 17:27:07 +00001262#ifdef _WIN32
1263 ptm = localtime(&ti);
1264 strftime(date_buf, sizeof(date_buf),
1265 "%Y-%m-%d %H:%M:%S", ptm);
1266#else
bellardfaea38e2006-08-05 21:31:00 +00001267 localtime_r(&ti, &tm);
1268 strftime(date_buf, sizeof(date_buf),
1269 "%Y-%m-%d %H:%M:%S", &tm);
bellard3b9f94e2007-01-07 17:27:07 +00001270#endif
bellardfaea38e2006-08-05 21:31:00 +00001271 secs = sn->vm_clock_nsec / 1000000000;
1272 snprintf(clock_buf, sizeof(clock_buf),
1273 "%02d:%02d:%02d.%03d",
1274 (int)(secs / 3600),
1275 (int)((secs / 60) % 60),
ths5fafdf22007-09-16 21:08:06 +00001276 (int)(secs % 60),
bellardfaea38e2006-08-05 21:31:00 +00001277 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1278 snprintf(buf, buf_size,
ths5fafdf22007-09-16 21:08:06 +00001279 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001280 sn->id_str, sn->name,
1281 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1282 date_buf,
1283 clock_buf);
1284 }
1285 return buf;
1286}
1287
bellardea2384d2004-08-01 21:59:26 +00001288
bellard83f64092006-08-01 16:21:11 +00001289/**************************************************************/
1290/* async I/Os */
1291
aliguori3fb94d52009-03-20 18:26:12 +00001292typedef struct VectorTranslationAIOCB {
1293 BlockDriverAIOCB common;
aliguori3b69e4b2009-01-22 16:59:24 +00001294 QEMUIOVector *iov;
1295 uint8_t *bounce;
1296 int is_write;
1297 BlockDriverAIOCB *aiocb;
aliguori3fb94d52009-03-20 18:26:12 +00001298} VectorTranslationAIOCB;
aliguori3b69e4b2009-01-22 16:59:24 +00001299
aliguori3fb94d52009-03-20 18:26:12 +00001300static void bdrv_aio_cancel_vector(BlockDriverAIOCB *_acb)
aliguoric07a9002009-03-20 18:26:03 +00001301{
aliguori3fb94d52009-03-20 18:26:12 +00001302 VectorTranslationAIOCB *acb
1303 = container_of(_acb, VectorTranslationAIOCB, common);
aliguoric07a9002009-03-20 18:26:03 +00001304
aliguori3fb94d52009-03-20 18:26:12 +00001305 bdrv_aio_cancel(acb->aiocb);
aliguoric07a9002009-03-20 18:26:03 +00001306}
1307
aliguori3b69e4b2009-01-22 16:59:24 +00001308static void bdrv_aio_rw_vector_cb(void *opaque, int ret)
1309{
aliguori3fb94d52009-03-20 18:26:12 +00001310 VectorTranslationAIOCB *s = (VectorTranslationAIOCB *)opaque;
aliguori3b69e4b2009-01-22 16:59:24 +00001311
1312 if (!s->is_write) {
aliguori249aa742009-01-26 17:17:52 +00001313 qemu_iovec_from_buffer(s->iov, s->bounce, s->iov->size);
aliguori3b69e4b2009-01-22 16:59:24 +00001314 }
aurel32d905dba2009-03-03 06:28:26 +00001315 qemu_vfree(s->bounce);
aliguori3fb94d52009-03-20 18:26:12 +00001316 s->common.cb(s->common.opaque, ret);
1317 qemu_aio_release(s);
aliguori3b69e4b2009-01-22 16:59:24 +00001318}
1319
1320static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
1321 int64_t sector_num,
1322 QEMUIOVector *iov,
1323 int nb_sectors,
1324 BlockDriverCompletionFunc *cb,
1325 void *opaque,
1326 int is_write)
1327
1328{
aliguori3fb94d52009-03-20 18:26:12 +00001329 VectorTranslationAIOCB *s = qemu_aio_get_pool(&vectored_aio_pool, bs,
1330 cb, opaque);
aliguori3b69e4b2009-01-22 16:59:24 +00001331
aliguori3b69e4b2009-01-22 16:59:24 +00001332 s->iov = iov;
1333 s->bounce = qemu_memalign(512, nb_sectors * 512);
1334 s->is_write = is_write;
1335 if (is_write) {
1336 qemu_iovec_to_buffer(s->iov, s->bounce);
1337 s->aiocb = bdrv_aio_write(bs, sector_num, s->bounce, nb_sectors,
1338 bdrv_aio_rw_vector_cb, s);
1339 } else {
1340 s->aiocb = bdrv_aio_read(bs, sector_num, s->bounce, nb_sectors,
1341 bdrv_aio_rw_vector_cb, s);
1342 }
aliguoric240b9a2009-03-28 16:11:20 +00001343 if (!s->aiocb) {
1344 qemu_vfree(s->bounce);
1345 qemu_aio_release(s);
1346 return NULL;
1347 }
aliguori3fb94d52009-03-20 18:26:12 +00001348 return &s->common;
aliguori3b69e4b2009-01-22 16:59:24 +00001349}
1350
1351BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
1352 QEMUIOVector *iov, int nb_sectors,
1353 BlockDriverCompletionFunc *cb, void *opaque)
1354{
aliguori71d07702009-03-03 17:37:16 +00001355 if (bdrv_check_request(bs, sector_num, nb_sectors))
1356 return NULL;
1357
aliguori3b69e4b2009-01-22 16:59:24 +00001358 return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
1359 cb, opaque, 0);
1360}
1361
1362BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1363 QEMUIOVector *iov, int nb_sectors,
1364 BlockDriverCompletionFunc *cb, void *opaque)
1365{
aliguori71d07702009-03-03 17:37:16 +00001366 if (bdrv_check_request(bs, sector_num, nb_sectors))
1367 return NULL;
1368
aliguori3b69e4b2009-01-22 16:59:24 +00001369 return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
1370 cb, opaque, 1);
1371}
1372
pbrookce1a14d2006-08-07 02:38:06 +00001373BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1374 uint8_t *buf, int nb_sectors,
1375 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001376{
1377 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001378 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001379
bellard19cb3732006-08-19 11:45:59 +00001380 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001381 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001382 if (bdrv_check_request(bs, sector_num, nb_sectors))
1383 return NULL;
ths3b46e622007-09-17 08:09:54 +00001384
thsa36e69d2007-12-02 05:18:19 +00001385 ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1386
1387 if (ret) {
1388 /* Update stats even though technically transfer has not happened. */
1389 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1390 bs->rd_ops ++;
1391 }
1392
1393 return ret;
bellard83f64092006-08-01 16:21:11 +00001394}
1395
pbrookce1a14d2006-08-07 02:38:06 +00001396BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1397 const uint8_t *buf, int nb_sectors,
1398 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001399{
bellard83f64092006-08-01 16:21:11 +00001400 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001401 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001402
bellard19cb3732006-08-19 11:45:59 +00001403 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001404 return NULL;
bellard83f64092006-08-01 16:21:11 +00001405 if (bs->read_only)
pbrookce1a14d2006-08-07 02:38:06 +00001406 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001407 if (bdrv_check_request(bs, sector_num, nb_sectors))
1408 return NULL;
bellard83f64092006-08-01 16:21:11 +00001409
thsa36e69d2007-12-02 05:18:19 +00001410 ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1411
1412 if (ret) {
1413 /* Update stats even though technically transfer has not happened. */
1414 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1415 bs->wr_ops ++;
1416 }
1417
1418 return ret;
bellard83f64092006-08-01 16:21:11 +00001419}
1420
1421void bdrv_aio_cancel(BlockDriverAIOCB *acb)
pbrookce1a14d2006-08-07 02:38:06 +00001422{
aliguori6bbff9a2009-03-20 18:25:59 +00001423 acb->pool->cancel(acb);
bellard83f64092006-08-01 16:21:11 +00001424}
1425
pbrookce1a14d2006-08-07 02:38:06 +00001426
bellard83f64092006-08-01 16:21:11 +00001427/**************************************************************/
1428/* async block device emulation */
1429
bellard83f64092006-08-01 16:21:11 +00001430static void bdrv_aio_bh_cb(void *opaque)
bellardbeac80c2006-06-26 20:08:57 +00001431{
pbrookce1a14d2006-08-07 02:38:06 +00001432 BlockDriverAIOCBSync *acb = opaque;
1433 acb->common.cb(acb->common.opaque, acb->ret);
1434 qemu_aio_release(acb);
bellardbeac80c2006-06-26 20:08:57 +00001435}
bellardbeac80c2006-06-26 20:08:57 +00001436
pbrookce1a14d2006-08-07 02:38:06 +00001437static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1438 int64_t sector_num, uint8_t *buf, int nb_sectors,
1439 BlockDriverCompletionFunc *cb, void *opaque)
bellardea2384d2004-08-01 21:59:26 +00001440{
pbrookce1a14d2006-08-07 02:38:06 +00001441 BlockDriverAIOCBSync *acb;
bellard83f64092006-08-01 16:21:11 +00001442 int ret;
pbrookce1a14d2006-08-07 02:38:06 +00001443
1444 acb = qemu_aio_get(bs, cb, opaque);
1445 if (!acb->bh)
1446 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1447 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1448 acb->ret = ret;
1449 qemu_bh_schedule(acb->bh);
1450 return &acb->common;
pbrook7a6cba62006-06-04 11:39:07 +00001451}
1452
pbrookce1a14d2006-08-07 02:38:06 +00001453static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1454 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1455 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001456{
pbrookce1a14d2006-08-07 02:38:06 +00001457 BlockDriverAIOCBSync *acb;
bellard83f64092006-08-01 16:21:11 +00001458 int ret;
pbrookce1a14d2006-08-07 02:38:06 +00001459
1460 acb = qemu_aio_get(bs, cb, opaque);
1461 if (!acb->bh)
1462 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1463 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1464 acb->ret = ret;
1465 qemu_bh_schedule(acb->bh);
1466 return &acb->common;
bellard83f64092006-08-01 16:21:11 +00001467}
1468
pbrookce1a14d2006-08-07 02:38:06 +00001469static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
bellard83f64092006-08-01 16:21:11 +00001470{
pbrookce1a14d2006-08-07 02:38:06 +00001471 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1472 qemu_bh_cancel(acb->bh);
1473 qemu_aio_release(acb);
bellard83f64092006-08-01 16:21:11 +00001474}
bellard83f64092006-08-01 16:21:11 +00001475
1476/**************************************************************/
1477/* sync block device emulation */
1478
1479static void bdrv_rw_em_cb(void *opaque, int ret)
1480{
1481 *(int *)opaque = ret;
1482}
1483
1484#define NOT_DONE 0x7fffffff
1485
ths5fafdf22007-09-16 21:08:06 +00001486static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +00001487 uint8_t *buf, int nb_sectors)
1488{
pbrookce1a14d2006-08-07 02:38:06 +00001489 int async_ret;
1490 BlockDriverAIOCB *acb;
bellard83f64092006-08-01 16:21:11 +00001491
bellard83f64092006-08-01 16:21:11 +00001492 async_ret = NOT_DONE;
ths5fafdf22007-09-16 21:08:06 +00001493 acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
bellard83f64092006-08-01 16:21:11 +00001494 bdrv_rw_em_cb, &async_ret);
aliguoribaf35cb2008-09-10 15:45:19 +00001495 if (acb == NULL)
pbrookce1a14d2006-08-07 02:38:06 +00001496 return -1;
aliguoribaf35cb2008-09-10 15:45:19 +00001497
bellard83f64092006-08-01 16:21:11 +00001498 while (async_ret == NOT_DONE) {
1499 qemu_aio_wait();
1500 }
aliguoribaf35cb2008-09-10 15:45:19 +00001501
bellard83f64092006-08-01 16:21:11 +00001502 return async_ret;
1503}
1504
1505static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1506 const uint8_t *buf, int nb_sectors)
1507{
pbrookce1a14d2006-08-07 02:38:06 +00001508 int async_ret;
1509 BlockDriverAIOCB *acb;
bellard83f64092006-08-01 16:21:11 +00001510
bellard83f64092006-08-01 16:21:11 +00001511 async_ret = NOT_DONE;
ths5fafdf22007-09-16 21:08:06 +00001512 acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
bellard83f64092006-08-01 16:21:11 +00001513 bdrv_rw_em_cb, &async_ret);
aliguoribaf35cb2008-09-10 15:45:19 +00001514 if (acb == NULL)
pbrookce1a14d2006-08-07 02:38:06 +00001515 return -1;
bellard83f64092006-08-01 16:21:11 +00001516 while (async_ret == NOT_DONE) {
1517 qemu_aio_wait();
1518 }
bellard83f64092006-08-01 16:21:11 +00001519 return async_ret;
1520}
bellardea2384d2004-08-01 21:59:26 +00001521
1522void bdrv_init(void)
1523{
aliguori3fb94d52009-03-20 18:26:12 +00001524 aio_pool_init(&vectored_aio_pool, sizeof(VectorTranslationAIOCB),
aliguoric07a9002009-03-20 18:26:03 +00001525 bdrv_aio_cancel_vector);
1526
bellardea2384d2004-08-01 21:59:26 +00001527 bdrv_register(&bdrv_raw);
bellard19cb3732006-08-19 11:45:59 +00001528 bdrv_register(&bdrv_host_device);
bellardea2384d2004-08-01 21:59:26 +00001529#ifndef _WIN32
1530 bdrv_register(&bdrv_cow);
1531#endif
1532 bdrv_register(&bdrv_qcow);
1533 bdrv_register(&bdrv_vmdk);
bellard3c565212004-09-29 21:29:14 +00001534 bdrv_register(&bdrv_cloop);
bellard585d0ed2004-12-12 11:24:44 +00001535 bdrv_register(&bdrv_dmg);
bellarda8753c32005-04-26 21:34:00 +00001536 bdrv_register(&bdrv_bochs);
bellard6a0f9e82005-04-27 20:17:58 +00001537 bdrv_register(&bdrv_vpc);
bellard712e7872005-04-28 21:09:32 +00001538 bdrv_register(&bdrv_vvfat);
bellardfaea38e2006-08-05 21:31:00 +00001539 bdrv_register(&bdrv_qcow2);
ths6ada7452007-07-31 23:28:53 +00001540 bdrv_register(&bdrv_parallels);
ths75818252008-07-03 13:41:03 +00001541 bdrv_register(&bdrv_nbd);
bellardea2384d2004-08-01 21:59:26 +00001542}
pbrookce1a14d2006-08-07 02:38:06 +00001543
aliguori6bbff9a2009-03-20 18:25:59 +00001544void aio_pool_init(AIOPool *pool, int aiocb_size,
1545 void (*cancel)(BlockDriverAIOCB *acb))
pbrookce1a14d2006-08-07 02:38:06 +00001546{
aliguori6bbff9a2009-03-20 18:25:59 +00001547 pool->aiocb_size = aiocb_size;
1548 pool->cancel = cancel;
1549 pool->free_aiocb = NULL;
1550}
1551
1552void *qemu_aio_get_pool(AIOPool *pool, BlockDriverState *bs,
1553 BlockDriverCompletionFunc *cb, void *opaque)
1554{
pbrookce1a14d2006-08-07 02:38:06 +00001555 BlockDriverAIOCB *acb;
1556
aliguori6bbff9a2009-03-20 18:25:59 +00001557 if (pool->free_aiocb) {
1558 acb = pool->free_aiocb;
1559 pool->free_aiocb = acb->next;
pbrookce1a14d2006-08-07 02:38:06 +00001560 } else {
aliguori6bbff9a2009-03-20 18:25:59 +00001561 acb = qemu_mallocz(pool->aiocb_size);
1562 acb->pool = pool;
pbrookce1a14d2006-08-07 02:38:06 +00001563 }
1564 acb->bs = bs;
1565 acb->cb = cb;
1566 acb->opaque = opaque;
1567 return acb;
1568}
1569
aliguori6bbff9a2009-03-20 18:25:59 +00001570void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1571 void *opaque)
1572{
1573 return qemu_aio_get_pool(&bs->drv->aio_pool, bs, cb, opaque);
1574}
1575
pbrookce1a14d2006-08-07 02:38:06 +00001576void qemu_aio_release(void *p)
1577{
aliguori6bbff9a2009-03-20 18:25:59 +00001578 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
1579 AIOPool *pool = acb->pool;
1580 acb->next = pool->free_aiocb;
1581 pool->free_aiocb = acb;
pbrookce1a14d2006-08-07 02:38:06 +00001582}
bellard19cb3732006-08-19 11:45:59 +00001583
1584/**************************************************************/
1585/* removable device support */
1586
1587/**
1588 * Return TRUE if the media is present
1589 */
1590int bdrv_is_inserted(BlockDriverState *bs)
1591{
1592 BlockDriver *drv = bs->drv;
1593 int ret;
1594 if (!drv)
1595 return 0;
1596 if (!drv->bdrv_is_inserted)
1597 return 1;
1598 ret = drv->bdrv_is_inserted(bs);
1599 return ret;
1600}
1601
1602/**
1603 * Return TRUE if the media changed since the last call to this
ths5fafdf22007-09-16 21:08:06 +00001604 * function. It is currently only used for floppy disks
bellard19cb3732006-08-19 11:45:59 +00001605 */
1606int bdrv_media_changed(BlockDriverState *bs)
1607{
1608 BlockDriver *drv = bs->drv;
1609 int ret;
1610
1611 if (!drv || !drv->bdrv_media_changed)
1612 ret = -ENOTSUP;
1613 else
1614 ret = drv->bdrv_media_changed(bs);
1615 if (ret == -ENOTSUP)
1616 ret = bs->media_changed;
1617 bs->media_changed = 0;
1618 return ret;
1619}
1620
1621/**
1622 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1623 */
1624void bdrv_eject(BlockDriverState *bs, int eject_flag)
1625{
1626 BlockDriver *drv = bs->drv;
1627 int ret;
1628
1629 if (!drv || !drv->bdrv_eject) {
1630 ret = -ENOTSUP;
1631 } else {
1632 ret = drv->bdrv_eject(bs, eject_flag);
1633 }
1634 if (ret == -ENOTSUP) {
1635 if (eject_flag)
1636 bdrv_close(bs);
1637 }
1638}
1639
1640int bdrv_is_locked(BlockDriverState *bs)
1641{
1642 return bs->locked;
1643}
1644
1645/**
1646 * Lock or unlock the media (if it is locked, the user won't be able
1647 * to eject it manually).
1648 */
1649void bdrv_set_locked(BlockDriverState *bs, int locked)
1650{
1651 BlockDriver *drv = bs->drv;
1652
1653 bs->locked = locked;
1654 if (drv && drv->bdrv_set_locked) {
1655 drv->bdrv_set_locked(bs, locked);
1656 }
1657}
ths985a03b2007-12-24 16:10:43 +00001658
1659/* needed for generic scsi interface */
1660
1661int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1662{
1663 BlockDriver *drv = bs->drv;
1664
1665 if (drv && drv->bdrv_ioctl)
1666 return drv->bdrv_ioctl(bs, req, buf);
1667 return -ENOTSUP;
1668}
aliguori7d780662009-03-12 19:57:08 +00001669
aliguori221f7152009-03-28 17:28:41 +00001670BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
1671 unsigned long int req, void *buf,
1672 BlockDriverCompletionFunc *cb, void *opaque)
aliguori7d780662009-03-12 19:57:08 +00001673{
aliguori221f7152009-03-28 17:28:41 +00001674 BlockDriver *drv = bs->drv;
aliguori7d780662009-03-12 19:57:08 +00001675
aliguori221f7152009-03-28 17:28:41 +00001676 if (drv && drv->bdrv_aio_ioctl)
1677 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
1678 return NULL;
aliguori7d780662009-03-12 19:57:08 +00001679}