blob: 6e4d910a7635d5b13776f8d6bae0b4d934b5a7ca [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"
Anthony Liguori5efa9d52009-05-09 17:03:42 -050033#include "module.h"
bellardfc01f7e2003-06-30 10:03:06 +000034
blueswir1179a2c12009-03-08 08:23:32 +000035#ifdef HOST_BSD
bellard7674e7b2005-04-26 21:59:26 +000036#include <sys/types.h>
37#include <sys/stat.h>
38#include <sys/ioctl.h>
blueswir1c5e97232009-03-07 20:06:23 +000039#ifndef __DragonFly__
bellard7674e7b2005-04-26 21:59:26 +000040#include <sys/disk.h>
41#endif
blueswir1c5e97232009-03-07 20:06:23 +000042#endif
bellard7674e7b2005-04-26 21:59:26 +000043
aliguori49dc7682009-03-08 16:26:59 +000044#ifdef _WIN32
45#include <windows.h>
46#endif
47
bellard83f64092006-08-01 16:21:11 +000048#define SECTOR_BITS 9
49#define SECTOR_SIZE (1 << SECTOR_BITS)
bellard3b0d4f62005-10-30 18:30:10 +000050
bellard90765422006-08-07 19:10:16 +000051typedef struct BlockDriverAIOCBSync {
52 BlockDriverAIOCB common;
53 QEMUBH *bh;
54 int ret;
aliguorif141eaf2009-04-07 18:43:24 +000055 /* vector translation state */
56 QEMUIOVector *qiov;
57 uint8_t *bounce;
58 int is_write;
bellard90765422006-08-07 19:10:16 +000059} BlockDriverAIOCBSync;
60
aliguorif141eaf2009-04-07 18:43:24 +000061static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
62 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
aliguoric87c0672009-04-07 18:43:20 +000063 BlockDriverCompletionFunc *cb, void *opaque);
aliguorif141eaf2009-04-07 18:43:24 +000064static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
65 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
pbrookce1a14d2006-08-07 02:38:06 +000066 BlockDriverCompletionFunc *cb, void *opaque);
bellard83f64092006-08-01 16:21:11 +000067static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
ths5fafdf22007-09-16 21:08:06 +000068static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +000069 uint8_t *buf, int nb_sectors);
70static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
71 const uint8_t *buf, int nb_sectors);
bellardec530c82006-04-25 22:36:06 +000072
blueswir17ee930d2008-09-17 19:04:14 +000073BlockDriverState *bdrv_first;
74
bellardea2384d2004-08-01 21:59:26 +000075static BlockDriver *first_drv;
76
bellard83f64092006-08-01 16:21:11 +000077int path_is_absolute(const char *path)
78{
79 const char *p;
bellard21664422007-01-07 18:22:37 +000080#ifdef _WIN32
81 /* specific case for names like: "\\.\d:" */
82 if (*path == '/' || *path == '\\')
83 return 1;
84#endif
bellard83f64092006-08-01 16:21:11 +000085 p = strchr(path, ':');
86 if (p)
87 p++;
88 else
89 p = path;
bellard3b9f94e2007-01-07 17:27:07 +000090#ifdef _WIN32
91 return (*p == '/' || *p == '\\');
92#else
93 return (*p == '/');
94#endif
bellard83f64092006-08-01 16:21:11 +000095}
96
97/* if filename is absolute, just copy it to dest. Otherwise, build a
98 path to it by considering it is relative to base_path. URL are
99 supported. */
100void path_combine(char *dest, int dest_size,
101 const char *base_path,
102 const char *filename)
103{
104 const char *p, *p1;
105 int len;
106
107 if (dest_size <= 0)
108 return;
109 if (path_is_absolute(filename)) {
110 pstrcpy(dest, dest_size, filename);
111 } else {
112 p = strchr(base_path, ':');
113 if (p)
114 p++;
115 else
116 p = base_path;
bellard3b9f94e2007-01-07 17:27:07 +0000117 p1 = strrchr(base_path, '/');
118#ifdef _WIN32
119 {
120 const char *p2;
121 p2 = strrchr(base_path, '\\');
122 if (!p1 || p2 > p1)
123 p1 = p2;
124 }
125#endif
bellard83f64092006-08-01 16:21:11 +0000126 if (p1)
127 p1++;
128 else
129 p1 = base_path;
130 if (p1 > p)
131 p = p1;
132 len = p - base_path;
133 if (len > dest_size - 1)
134 len = dest_size - 1;
135 memcpy(dest, base_path, len);
136 dest[len] = '\0';
137 pstrcat(dest, dest_size, filename);
138 }
139}
140
141
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500142void bdrv_register(BlockDriver *bdrv)
bellardea2384d2004-08-01 21:59:26 +0000143{
aliguorif141eaf2009-04-07 18:43:24 +0000144 if (!bdrv->bdrv_aio_readv) {
bellard83f64092006-08-01 16:21:11 +0000145 /* add AIO emulation layer */
aliguorif141eaf2009-04-07 18:43:24 +0000146 bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
147 bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
bellard83f64092006-08-01 16:21:11 +0000148 bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
bellard90765422006-08-07 19:10:16 +0000149 bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
aliguorieda578e2009-03-12 19:57:16 +0000150 } else if (!bdrv->bdrv_read) {
bellard83f64092006-08-01 16:21:11 +0000151 /* add synchronous IO emulation layer */
152 bdrv->bdrv_read = bdrv_read_em;
153 bdrv->bdrv_write = bdrv_write_em;
154 }
aliguori6bbff9a2009-03-20 18:25:59 +0000155 aio_pool_init(&bdrv->aio_pool, bdrv->aiocb_size, bdrv->bdrv_aio_cancel);
bellardea2384d2004-08-01 21:59:26 +0000156 bdrv->next = first_drv;
157 first_drv = bdrv;
158}
bellardb3380822004-03-14 21:38:54 +0000159
160/* create a new block device (by default it is empty) */
161BlockDriverState *bdrv_new(const char *device_name)
bellardfc01f7e2003-06-30 10:03:06 +0000162{
bellardb3380822004-03-14 21:38:54 +0000163 BlockDriverState **pbs, *bs;
164
165 bs = qemu_mallocz(sizeof(BlockDriverState));
bellardb3380822004-03-14 21:38:54 +0000166 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
bellardea2384d2004-08-01 21:59:26 +0000167 if (device_name[0] != '\0') {
168 /* insert at the end */
169 pbs = &bdrv_first;
170 while (*pbs != NULL)
171 pbs = &(*pbs)->next;
172 *pbs = bs;
173 }
bellardb3380822004-03-14 21:38:54 +0000174 return bs;
175}
176
bellardea2384d2004-08-01 21:59:26 +0000177BlockDriver *bdrv_find_format(const char *format_name)
178{
179 BlockDriver *drv1;
180 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
181 if (!strcmp(drv1->format_name, format_name))
182 return drv1;
183 }
184 return NULL;
185}
186
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200187int bdrv_create(BlockDriver *drv, const char* filename,
188 QEMUOptionParameter *options)
bellardea2384d2004-08-01 21:59:26 +0000189{
190 if (!drv->bdrv_create)
191 return -ENOTSUP;
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200192
193 return drv->bdrv_create(filename, options);
bellardea2384d2004-08-01 21:59:26 +0000194}
195
bellardd5249392004-08-03 21:14:23 +0000196#ifdef _WIN32
bellard95389c82005-12-18 18:28:15 +0000197void get_tmp_filename(char *filename, int size)
bellardd5249392004-08-03 21:14:23 +0000198{
bellard3b9f94e2007-01-07 17:27:07 +0000199 char temp_dir[MAX_PATH];
ths3b46e622007-09-17 08:09:54 +0000200
bellard3b9f94e2007-01-07 17:27:07 +0000201 GetTempPath(MAX_PATH, temp_dir);
202 GetTempFileName(temp_dir, "qem", 0, filename);
bellardd5249392004-08-03 21:14:23 +0000203}
204#else
bellard95389c82005-12-18 18:28:15 +0000205void get_tmp_filename(char *filename, int size)
bellardea2384d2004-08-01 21:59:26 +0000206{
207 int fd;
blueswir17ccfb2e2008-09-14 06:45:34 +0000208 const char *tmpdir;
bellardd5249392004-08-03 21:14:23 +0000209 /* XXX: race condition possible */
aurel320badc1e2008-03-10 00:05:34 +0000210 tmpdir = getenv("TMPDIR");
211 if (!tmpdir)
212 tmpdir = "/tmp";
213 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
bellardea2384d2004-08-01 21:59:26 +0000214 fd = mkstemp(filename);
215 close(fd);
216}
bellardd5249392004-08-03 21:14:23 +0000217#endif
bellardea2384d2004-08-01 21:59:26 +0000218
bellard19cb3732006-08-19 11:45:59 +0000219#ifdef _WIN32
bellardf45512f2006-08-23 21:40:13 +0000220static int is_windows_drive_prefix(const char *filename)
221{
222 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
223 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
224 filename[1] == ':');
225}
ths3b46e622007-09-17 08:09:54 +0000226
bellard19cb3732006-08-19 11:45:59 +0000227static int is_windows_drive(const char *filename)
228{
ths5fafdf22007-09-16 21:08:06 +0000229 if (is_windows_drive_prefix(filename) &&
bellardf45512f2006-08-23 21:40:13 +0000230 filename[2] == '\0')
bellard19cb3732006-08-19 11:45:59 +0000231 return 1;
232 if (strstart(filename, "\\\\.\\", NULL) ||
233 strstart(filename, "//./", NULL))
234 return 1;
235 return 0;
236}
237#endif
238
bellard83f64092006-08-01 16:21:11 +0000239static BlockDriver *find_protocol(const char *filename)
240{
241 BlockDriver *drv1;
242 char protocol[128];
243 int len;
244 const char *p;
bellard19cb3732006-08-19 11:45:59 +0000245
246#ifdef _WIN32
bellardf45512f2006-08-23 21:40:13 +0000247 if (is_windows_drive(filename) ||
248 is_windows_drive_prefix(filename))
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500249 return bdrv_find_format("raw");
bellard19cb3732006-08-19 11:45:59 +0000250#endif
bellard83f64092006-08-01 16:21:11 +0000251 p = strchr(filename, ':');
252 if (!p)
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500253 return bdrv_find_format("raw");
bellard83f64092006-08-01 16:21:11 +0000254 len = p - filename;
255 if (len > sizeof(protocol) - 1)
256 len = sizeof(protocol) - 1;
bellard83f64092006-08-01 16:21:11 +0000257 memcpy(protocol, filename, len);
258 protocol[len] = '\0';
259 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
ths5fafdf22007-09-16 21:08:06 +0000260 if (drv1->protocol_name &&
bellard83f64092006-08-01 16:21:11 +0000261 !strcmp(drv1->protocol_name, protocol))
262 return drv1;
263 }
264 return NULL;
265}
266
bellard7674e7b2005-04-26 21:59:26 +0000267/* XXX: force raw format if block or character device ? It would
268 simplify the BSD case */
bellardea2384d2004-08-01 21:59:26 +0000269static BlockDriver *find_image_format(const char *filename)
270{
bellard83f64092006-08-01 16:21:11 +0000271 int ret, score, score_max;
bellardea2384d2004-08-01 21:59:26 +0000272 BlockDriver *drv1, *drv;
bellard83f64092006-08-01 16:21:11 +0000273 uint8_t buf[2048];
274 BlockDriverState *bs;
ths3b46e622007-09-17 08:09:54 +0000275
bellard19cb3732006-08-19 11:45:59 +0000276 /* detect host devices. By convention, /dev/cdrom[N] is always
277 recognized as a host CDROM */
278 if (strstart(filename, "/dev/cdrom", NULL))
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500279 return bdrv_find_format("host_device");
bellard19cb3732006-08-19 11:45:59 +0000280#ifdef _WIN32
281 if (is_windows_drive(filename))
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500282 return bdrv_find_format("host_device");
bellard19cb3732006-08-19 11:45:59 +0000283#else
284 {
285 struct stat st;
ths5fafdf22007-09-16 21:08:06 +0000286 if (stat(filename, &st) >= 0 &&
bellard19cb3732006-08-19 11:45:59 +0000287 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500288 return bdrv_find_format("host_device");
bellard19cb3732006-08-19 11:45:59 +0000289 }
290 }
291#endif
ths3b46e622007-09-17 08:09:54 +0000292
bellard83f64092006-08-01 16:21:11 +0000293 drv = find_protocol(filename);
bellard19cb3732006-08-19 11:45:59 +0000294 /* no need to test disk image formats for vvfat */
Anthony Liguoric833ab72009-05-22 08:17:55 -0500295 if (drv && strcmp(drv->format_name, "vvfat") == 0)
bellard83f64092006-08-01 16:21:11 +0000296 return drv;
bellard19cb3732006-08-19 11:45:59 +0000297
bellard83f64092006-08-01 16:21:11 +0000298 ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
299 if (ret < 0)
300 return NULL;
301 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
302 bdrv_delete(bs);
303 if (ret < 0) {
304 return NULL;
305 }
306
bellardea2384d2004-08-01 21:59:26 +0000307 score_max = 0;
308 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
bellard83f64092006-08-01 16:21:11 +0000309 if (drv1->bdrv_probe) {
310 score = drv1->bdrv_probe(buf, ret, filename);
311 if (score > score_max) {
312 score_max = score;
313 drv = drv1;
314 }
bellardea2384d2004-08-01 21:59:26 +0000315 }
316 }
317 return drv;
318}
319
bellard83f64092006-08-01 16:21:11 +0000320int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
bellardb3380822004-03-14 21:38:54 +0000321{
bellard83f64092006-08-01 16:21:11 +0000322 BlockDriverState *bs;
323 int ret;
324
325 bs = bdrv_new("");
bellard83f64092006-08-01 16:21:11 +0000326 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
327 if (ret < 0) {
328 bdrv_delete(bs);
329 return ret;
bellard3b0d4f62005-10-30 18:30:10 +0000330 }
aliguori71d07702009-03-03 17:37:16 +0000331 bs->growable = 1;
bellard83f64092006-08-01 16:21:11 +0000332 *pbs = bs;
333 return 0;
bellardea2384d2004-08-01 21:59:26 +0000334}
bellardfc01f7e2003-06-30 10:03:06 +0000335
bellard83f64092006-08-01 16:21:11 +0000336int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
337{
338 return bdrv_open2(bs, filename, flags, NULL);
339}
340
341int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
bellardea2384d2004-08-01 21:59:26 +0000342 BlockDriver *drv)
343{
bellard83f64092006-08-01 16:21:11 +0000344 int ret, open_flags;
thseb5c8512007-02-11 15:06:09 +0000345 char tmp_filename[PATH_MAX];
346 char backing_filename[PATH_MAX];
ths3b46e622007-09-17 08:09:54 +0000347
bellardea2384d2004-08-01 21:59:26 +0000348 bs->read_only = 0;
349 bs->is_temporary = 0;
350 bs->encrypted = 0;
aliguoric0f4ce72009-03-05 23:01:01 +0000351 bs->valid_key = 0;
aliguorie268ca52009-04-22 20:20:00 +0000352 /* buffer_alignment defaulted to 512, drivers can change this value */
353 bs->buffer_alignment = 512;
bellard712e7872005-04-28 21:09:32 +0000354
bellard83f64092006-08-01 16:21:11 +0000355 if (flags & BDRV_O_SNAPSHOT) {
bellardea2384d2004-08-01 21:59:26 +0000356 BlockDriverState *bs1;
357 int64_t total_size;
aliguori7c96d462008-09-12 17:54:13 +0000358 int is_protocol = 0;
Kevin Wolf91a073a2009-05-27 14:48:06 +0200359 BlockDriver *bdrv_qcow2;
360 QEMUOptionParameter *options;
ths3b46e622007-09-17 08:09:54 +0000361
bellardea2384d2004-08-01 21:59:26 +0000362 /* if snapshot, we create a temporary backing file and open it
363 instead of opening 'filename' directly */
364
365 /* if there is a backing file, use it */
366 bs1 = bdrv_new("");
aliguori5eb45632009-03-28 17:55:10 +0000367 ret = bdrv_open2(bs1, filename, 0, drv);
aliguori51d7c002009-03-05 23:00:29 +0000368 if (ret < 0) {
bellardea2384d2004-08-01 21:59:26 +0000369 bdrv_delete(bs1);
aliguori51d7c002009-03-05 23:00:29 +0000370 return ret;
bellardea2384d2004-08-01 21:59:26 +0000371 }
bellard83f64092006-08-01 16:21:11 +0000372 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
aliguori7c96d462008-09-12 17:54:13 +0000373
374 if (bs1->drv && bs1->drv->protocol_name)
375 is_protocol = 1;
376
bellardea2384d2004-08-01 21:59:26 +0000377 bdrv_delete(bs1);
ths3b46e622007-09-17 08:09:54 +0000378
bellardea2384d2004-08-01 21:59:26 +0000379 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
aliguori7c96d462008-09-12 17:54:13 +0000380
381 /* Real path is meaningless for protocols */
382 if (is_protocol)
383 snprintf(backing_filename, sizeof(backing_filename),
384 "%s", filename);
385 else
386 realpath(filename, backing_filename);
387
Kevin Wolf91a073a2009-05-27 14:48:06 +0200388 bdrv_qcow2 = bdrv_find_format("qcow2");
389 options = parse_option_parameters("", bdrv_qcow2->create_options, NULL);
390
391 set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size * 512);
392 set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename);
393 if (drv) {
394 set_option_parameter(options, BLOCK_OPT_BACKING_FMT,
395 drv->format_name);
396 }
397
398 ret = bdrv_create(bdrv_qcow2, tmp_filename, options);
aliguori51d7c002009-03-05 23:00:29 +0000399 if (ret < 0) {
400 return ret;
bellardea2384d2004-08-01 21:59:26 +0000401 }
Kevin Wolf91a073a2009-05-27 14:48:06 +0200402
bellardea2384d2004-08-01 21:59:26 +0000403 filename = tmp_filename;
Kevin Wolf91a073a2009-05-27 14:48:06 +0200404 drv = bdrv_qcow2;
bellardea2384d2004-08-01 21:59:26 +0000405 bs->is_temporary = 1;
406 }
bellard712e7872005-04-28 21:09:32 +0000407
bellardea2384d2004-08-01 21:59:26 +0000408 pstrcpy(bs->filename, sizeof(bs->filename), filename);
bellard83f64092006-08-01 16:21:11 +0000409 if (flags & BDRV_O_FILE) {
410 drv = find_protocol(filename);
aliguori51d7c002009-03-05 23:00:29 +0000411 } else if (!drv) {
412 drv = find_image_format(filename);
413 }
414 if (!drv) {
415 ret = -ENOENT;
416 goto unlink_and_fail;
bellardea2384d2004-08-01 21:59:26 +0000417 }
418 bs->drv = drv;
419 bs->opaque = qemu_mallocz(drv->instance_size);
bellard83f64092006-08-01 16:21:11 +0000420 /* Note: for compatibility, we open disk image files as RDWR, and
421 RDONLY as fallback */
422 if (!(flags & BDRV_O_FILE))
aliguori9f7965c2008-10-14 14:42:54 +0000423 open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK);
bellard83f64092006-08-01 16:21:11 +0000424 else
425 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
426 ret = drv->bdrv_open(bs, filename, open_flags);
aurel32a0a83532008-10-13 21:08:34 +0000427 if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
aliguori9f7965c2008-10-14 14:42:54 +0000428 ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
bellard83f64092006-08-01 16:21:11 +0000429 bs->read_only = 1;
430 }
bellardea2384d2004-08-01 21:59:26 +0000431 if (ret < 0) {
432 qemu_free(bs->opaque);
bellard6b21b972006-08-23 21:14:37 +0000433 bs->opaque = NULL;
434 bs->drv = NULL;
aliguori51d7c002009-03-05 23:00:29 +0000435 unlink_and_fail:
436 if (bs->is_temporary)
437 unlink(filename);
bellard83f64092006-08-01 16:21:11 +0000438 return ret;
bellardea2384d2004-08-01 21:59:26 +0000439 }
bellardd15a7712006-08-06 13:35:09 +0000440 if (drv->bdrv_getlength) {
441 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
442 }
bellardea2384d2004-08-01 21:59:26 +0000443#ifndef _WIN32
444 if (bs->is_temporary) {
445 unlink(filename);
446 }
447#endif
bellard83f64092006-08-01 16:21:11 +0000448 if (bs->backing_file[0] != '\0') {
bellardea2384d2004-08-01 21:59:26 +0000449 /* if there is a backing file, use it */
aliguori5eb45632009-03-28 17:55:10 +0000450 BlockDriver *back_drv = NULL;
bellardea2384d2004-08-01 21:59:26 +0000451 bs->backing_hd = bdrv_new("");
bellard83f64092006-08-01 16:21:11 +0000452 path_combine(backing_filename, sizeof(backing_filename),
453 filename, bs->backing_file);
aliguori5eb45632009-03-28 17:55:10 +0000454 if (bs->backing_format[0] != '\0')
455 back_drv = bdrv_find_format(bs->backing_format);
456 ret = bdrv_open2(bs->backing_hd, backing_filename, open_flags,
457 back_drv);
aliguori51d7c002009-03-05 23:00:29 +0000458 if (ret < 0) {
459 bdrv_close(bs);
460 return ret;
461 }
bellardea2384d2004-08-01 21:59:26 +0000462 }
463
aliguoribb5fc202009-03-05 23:01:15 +0000464 if (!bdrv_key_required(bs)) {
465 /* call the change callback */
466 bs->media_changed = 1;
467 if (bs->change_cb)
468 bs->change_cb(bs->change_opaque);
469 }
bellardb3380822004-03-14 21:38:54 +0000470 return 0;
bellardfc01f7e2003-06-30 10:03:06 +0000471}
472
473void bdrv_close(BlockDriverState *bs)
474{
bellard19cb3732006-08-19 11:45:59 +0000475 if (bs->drv) {
bellardea2384d2004-08-01 21:59:26 +0000476 if (bs->backing_hd)
477 bdrv_delete(bs->backing_hd);
478 bs->drv->bdrv_close(bs);
479 qemu_free(bs->opaque);
480#ifdef _WIN32
481 if (bs->is_temporary) {
482 unlink(bs->filename);
483 }
bellard67b915a2004-03-31 23:37:16 +0000484#endif
bellardea2384d2004-08-01 21:59:26 +0000485 bs->opaque = NULL;
486 bs->drv = NULL;
bellardb3380822004-03-14 21:38:54 +0000487
488 /* call the change callback */
bellard19cb3732006-08-19 11:45:59 +0000489 bs->media_changed = 1;
bellardb3380822004-03-14 21:38:54 +0000490 if (bs->change_cb)
491 bs->change_cb(bs->change_opaque);
492 }
493}
494
495void bdrv_delete(BlockDriverState *bs)
496{
aurel3234c6f052008-04-08 19:51:21 +0000497 BlockDriverState **pbs;
498
499 pbs = &bdrv_first;
500 while (*pbs != bs && *pbs != NULL)
501 pbs = &(*pbs)->next;
502 if (*pbs == bs)
503 *pbs = bs->next;
504
bellardb3380822004-03-14 21:38:54 +0000505 bdrv_close(bs);
506 qemu_free(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000507}
508
aliguorie97fc192009-04-21 23:11:50 +0000509/*
510 * Run consistency checks on an image
511 *
512 * Returns the number of errors or -errno when an internal error occurs
513 */
514int bdrv_check(BlockDriverState *bs)
515{
516 if (bs->drv->bdrv_check == NULL) {
517 return -ENOTSUP;
518 }
519
520 return bs->drv->bdrv_check(bs);
521}
522
bellard33e39632003-07-06 17:15:21 +0000523/* commit COW file into the raw image */
524int bdrv_commit(BlockDriverState *bs)
525{
bellard19cb3732006-08-19 11:45:59 +0000526 BlockDriver *drv = bs->drv;
bellard83f64092006-08-01 16:21:11 +0000527 int64_t i, total_sectors;
bellardea2384d2004-08-01 21:59:26 +0000528 int n, j;
529 unsigned char sector[512];
bellard33e39632003-07-06 17:15:21 +0000530
bellard19cb3732006-08-19 11:45:59 +0000531 if (!drv)
532 return -ENOMEDIUM;
bellard33e39632003-07-06 17:15:21 +0000533
534 if (bs->read_only) {
bellardea2384d2004-08-01 21:59:26 +0000535 return -EACCES;
bellard33e39632003-07-06 17:15:21 +0000536 }
537
bellardea2384d2004-08-01 21:59:26 +0000538 if (!bs->backing_hd) {
539 return -ENOTSUP;
bellard33e39632003-07-06 17:15:21 +0000540 }
bellardea2384d2004-08-01 21:59:26 +0000541
bellard83f64092006-08-01 16:21:11 +0000542 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
543 for (i = 0; i < total_sectors;) {
bellard19cb3732006-08-19 11:45:59 +0000544 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
bellardea2384d2004-08-01 21:59:26 +0000545 for(j = 0; j < n; j++) {
546 if (bdrv_read(bs, i, sector, 1) != 0) {
547 return -EIO;
548 }
549
550 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
551 return -EIO;
552 }
553 i++;
554 }
555 } else {
556 i += n;
557 }
558 }
bellard95389c82005-12-18 18:28:15 +0000559
bellard19cb3732006-08-19 11:45:59 +0000560 if (drv->bdrv_make_empty)
561 return drv->bdrv_make_empty(bs);
bellard95389c82005-12-18 18:28:15 +0000562
bellard33e39632003-07-06 17:15:21 +0000563 return 0;
564}
565
aliguori71d07702009-03-03 17:37:16 +0000566static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
567 size_t size)
568{
569 int64_t len;
570
571 if (!bdrv_is_inserted(bs))
572 return -ENOMEDIUM;
573
574 if (bs->growable)
575 return 0;
576
577 len = bdrv_getlength(bs);
578
Kevin Wolffbb7b4e2009-05-08 14:47:24 +0200579 if (offset < 0)
580 return -EIO;
581
582 if ((offset > len) || (len - offset < size))
aliguori71d07702009-03-03 17:37:16 +0000583 return -EIO;
584
585 return 0;
586}
587
588static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
589 int nb_sectors)
590{
aliguori999dec52009-03-29 01:31:48 +0000591 return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512);
aliguori71d07702009-03-03 17:37:16 +0000592}
593
bellard19cb3732006-08-19 11:45:59 +0000594/* return < 0 if error. See bdrv_write() for the return codes */
ths5fafdf22007-09-16 21:08:06 +0000595int bdrv_read(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000596 uint8_t *buf, int nb_sectors)
597{
bellardea2384d2004-08-01 21:59:26 +0000598 BlockDriver *drv = bs->drv;
599
bellard19cb3732006-08-19 11:45:59 +0000600 if (!drv)
601 return -ENOMEDIUM;
aliguori71d07702009-03-03 17:37:16 +0000602 if (bdrv_check_request(bs, sector_num, nb_sectors))
603 return -EIO;
bellardb3380822004-03-14 21:38:54 +0000604
aliguorieda578e2009-03-12 19:57:16 +0000605 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
bellardfc01f7e2003-06-30 10:03:06 +0000606}
607
ths5fafdf22007-09-16 21:08:06 +0000608/* Return < 0 if error. Important errors are:
bellard19cb3732006-08-19 11:45:59 +0000609 -EIO generic I/O error (may happen for all errors)
610 -ENOMEDIUM No media inserted.
611 -EINVAL Invalid sector number or nb_sectors
612 -EACCES Trying to write a read-only device
613*/
ths5fafdf22007-09-16 21:08:06 +0000614int bdrv_write(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000615 const uint8_t *buf, int nb_sectors)
616{
bellard83f64092006-08-01 16:21:11 +0000617 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +0000618 if (!bs->drv)
619 return -ENOMEDIUM;
bellard0849bf02003-06-30 23:17:31 +0000620 if (bs->read_only)
bellard19cb3732006-08-19 11:45:59 +0000621 return -EACCES;
aliguori71d07702009-03-03 17:37:16 +0000622 if (bdrv_check_request(bs, sector_num, nb_sectors))
623 return -EIO;
624
aliguori42fb2802009-01-15 20:43:39 +0000625 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
bellard83f64092006-08-01 16:21:11 +0000626}
627
aliguorieda578e2009-03-12 19:57:16 +0000628int bdrv_pread(BlockDriverState *bs, int64_t offset,
629 void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000630{
bellard83f64092006-08-01 16:21:11 +0000631 uint8_t tmp_buf[SECTOR_SIZE];
632 int len, nb_sectors, count;
633 int64_t sector_num;
634
635 count = count1;
636 /* first read to align to sector start */
637 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
638 if (len > count)
639 len = count;
640 sector_num = offset >> SECTOR_BITS;
641 if (len > 0) {
642 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
643 return -EIO;
644 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
645 count -= len;
646 if (count == 0)
647 return count1;
648 sector_num++;
649 buf += len;
650 }
651
652 /* read the sectors "in place" */
653 nb_sectors = count >> SECTOR_BITS;
654 if (nb_sectors > 0) {
655 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
656 return -EIO;
657 sector_num += nb_sectors;
658 len = nb_sectors << SECTOR_BITS;
659 buf += len;
660 count -= len;
661 }
662
663 /* add data from the last sector */
664 if (count > 0) {
665 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
666 return -EIO;
667 memcpy(buf, tmp_buf, count);
668 }
669 return count1;
670}
671
aliguorieda578e2009-03-12 19:57:16 +0000672int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
673 const void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000674{
bellard83f64092006-08-01 16:21:11 +0000675 uint8_t tmp_buf[SECTOR_SIZE];
676 int len, nb_sectors, count;
677 int64_t sector_num;
678
679 count = count1;
680 /* first write to align to sector start */
681 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
682 if (len > count)
683 len = count;
684 sector_num = offset >> SECTOR_BITS;
685 if (len > 0) {
686 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
687 return -EIO;
688 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
689 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
690 return -EIO;
691 count -= len;
692 if (count == 0)
693 return count1;
694 sector_num++;
695 buf += len;
696 }
697
698 /* write the sectors "in place" */
699 nb_sectors = count >> SECTOR_BITS;
700 if (nb_sectors > 0) {
701 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
702 return -EIO;
703 sector_num += nb_sectors;
704 len = nb_sectors << SECTOR_BITS;
705 buf += len;
706 count -= len;
707 }
708
709 /* add data from the last sector */
710 if (count > 0) {
711 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
712 return -EIO;
713 memcpy(tmp_buf, buf, count);
714 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
715 return -EIO;
716 }
717 return count1;
718}
bellard83f64092006-08-01 16:21:11 +0000719
720/**
bellard83f64092006-08-01 16:21:11 +0000721 * Truncate file to 'offset' bytes (needed only for file protocols)
722 */
723int bdrv_truncate(BlockDriverState *bs, int64_t offset)
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_truncate)
729 return -ENOTSUP;
730 return drv->bdrv_truncate(bs, offset);
731}
732
733/**
734 * Length of a file in bytes. Return < 0 if error or unknown.
735 */
736int64_t bdrv_getlength(BlockDriverState *bs)
737{
738 BlockDriver *drv = bs->drv;
739 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000740 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000741 if (!drv->bdrv_getlength) {
742 /* legacy mode */
743 return bs->total_sectors * SECTOR_SIZE;
744 }
745 return drv->bdrv_getlength(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000746}
747
bellard19cb3732006-08-19 11:45:59 +0000748/* return 0 as number of sectors if no device present or error */
ths96b8f132007-12-17 01:35:20 +0000749void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
bellardfc01f7e2003-06-30 10:03:06 +0000750{
bellard19cb3732006-08-19 11:45:59 +0000751 int64_t length;
752 length = bdrv_getlength(bs);
753 if (length < 0)
754 length = 0;
755 else
756 length = length >> SECTOR_BITS;
757 *nb_sectors_ptr = length;
bellardfc01f7e2003-06-30 10:03:06 +0000758}
bellardcf989512004-02-16 21:56:36 +0000759
aliguorif3d54fc2008-11-25 21:50:24 +0000760struct partition {
761 uint8_t boot_ind; /* 0x80 - active */
762 uint8_t head; /* starting head */
763 uint8_t sector; /* starting sector */
764 uint8_t cyl; /* starting cylinder */
765 uint8_t sys_ind; /* What partition type */
766 uint8_t end_head; /* end head */
767 uint8_t end_sector; /* end sector */
768 uint8_t end_cyl; /* end cylinder */
769 uint32_t start_sect; /* starting sector counting from 0 */
770 uint32_t nr_sects; /* nr of sectors in partition */
771} __attribute__((packed));
772
773/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
774static int guess_disk_lchs(BlockDriverState *bs,
775 int *pcylinders, int *pheads, int *psectors)
776{
777 uint8_t buf[512];
778 int ret, i, heads, sectors, cylinders;
779 struct partition *p;
780 uint32_t nr_sects;
blueswir1a38131b2008-12-05 17:56:40 +0000781 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +0000782
783 bdrv_get_geometry(bs, &nb_sectors);
784
785 ret = bdrv_read(bs, 0, buf, 1);
786 if (ret < 0)
787 return -1;
788 /* test msdos magic */
789 if (buf[510] != 0x55 || buf[511] != 0xaa)
790 return -1;
791 for(i = 0; i < 4; i++) {
792 p = ((struct partition *)(buf + 0x1be)) + i;
793 nr_sects = le32_to_cpu(p->nr_sects);
794 if (nr_sects && p->end_head) {
795 /* We make the assumption that the partition terminates on
796 a cylinder boundary */
797 heads = p->end_head + 1;
798 sectors = p->end_sector & 63;
799 if (sectors == 0)
800 continue;
801 cylinders = nb_sectors / (heads * sectors);
802 if (cylinders < 1 || cylinders > 16383)
803 continue;
804 *pheads = heads;
805 *psectors = sectors;
806 *pcylinders = cylinders;
807#if 0
808 printf("guessed geometry: LCHS=%d %d %d\n",
809 cylinders, heads, sectors);
810#endif
811 return 0;
812 }
813 }
814 return -1;
815}
816
817void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
818{
819 int translation, lba_detected = 0;
820 int cylinders, heads, secs;
blueswir1a38131b2008-12-05 17:56:40 +0000821 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +0000822
823 /* if a geometry hint is available, use it */
824 bdrv_get_geometry(bs, &nb_sectors);
825 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
826 translation = bdrv_get_translation_hint(bs);
827 if (cylinders != 0) {
828 *pcyls = cylinders;
829 *pheads = heads;
830 *psecs = secs;
831 } else {
832 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
833 if (heads > 16) {
834 /* if heads > 16, it means that a BIOS LBA
835 translation was active, so the default
836 hardware geometry is OK */
837 lba_detected = 1;
838 goto default_geometry;
839 } else {
840 *pcyls = cylinders;
841 *pheads = heads;
842 *psecs = secs;
843 /* disable any translation to be in sync with
844 the logical geometry */
845 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
846 bdrv_set_translation_hint(bs,
847 BIOS_ATA_TRANSLATION_NONE);
848 }
849 }
850 } else {
851 default_geometry:
852 /* if no geometry, use a standard physical disk geometry */
853 cylinders = nb_sectors / (16 * 63);
854
855 if (cylinders > 16383)
856 cylinders = 16383;
857 else if (cylinders < 2)
858 cylinders = 2;
859 *pcyls = cylinders;
860 *pheads = 16;
861 *psecs = 63;
862 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
863 if ((*pcyls * *pheads) <= 131072) {
864 bdrv_set_translation_hint(bs,
865 BIOS_ATA_TRANSLATION_LARGE);
866 } else {
867 bdrv_set_translation_hint(bs,
868 BIOS_ATA_TRANSLATION_LBA);
869 }
870 }
871 }
872 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
873 }
874}
875
ths5fafdf22007-09-16 21:08:06 +0000876void bdrv_set_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000877 int cyls, int heads, int secs)
878{
879 bs->cyls = cyls;
880 bs->heads = heads;
881 bs->secs = secs;
882}
883
884void bdrv_set_type_hint(BlockDriverState *bs, int type)
885{
886 bs->type = type;
887 bs->removable = ((type == BDRV_TYPE_CDROM ||
888 type == BDRV_TYPE_FLOPPY));
889}
890
bellard46d47672004-11-16 01:45:27 +0000891void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
892{
893 bs->translation = translation;
894}
895
ths5fafdf22007-09-16 21:08:06 +0000896void bdrv_get_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000897 int *pcyls, int *pheads, int *psecs)
898{
899 *pcyls = bs->cyls;
900 *pheads = bs->heads;
901 *psecs = bs->secs;
902}
903
904int bdrv_get_type_hint(BlockDriverState *bs)
905{
906 return bs->type;
907}
908
bellard46d47672004-11-16 01:45:27 +0000909int bdrv_get_translation_hint(BlockDriverState *bs)
910{
911 return bs->translation;
912}
913
bellardb3380822004-03-14 21:38:54 +0000914int bdrv_is_removable(BlockDriverState *bs)
915{
916 return bs->removable;
917}
918
919int bdrv_is_read_only(BlockDriverState *bs)
920{
921 return bs->read_only;
922}
923
ths985a03b2007-12-24 16:10:43 +0000924int bdrv_is_sg(BlockDriverState *bs)
925{
926 return bs->sg;
927}
928
bellard19cb3732006-08-19 11:45:59 +0000929/* XXX: no longer used */
ths5fafdf22007-09-16 21:08:06 +0000930void bdrv_set_change_cb(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000931 void (*change_cb)(void *opaque), void *opaque)
932{
933 bs->change_cb = change_cb;
934 bs->change_opaque = opaque;
935}
936
bellardea2384d2004-08-01 21:59:26 +0000937int bdrv_is_encrypted(BlockDriverState *bs)
938{
939 if (bs->backing_hd && bs->backing_hd->encrypted)
940 return 1;
941 return bs->encrypted;
942}
943
aliguoric0f4ce72009-03-05 23:01:01 +0000944int bdrv_key_required(BlockDriverState *bs)
945{
946 BlockDriverState *backing_hd = bs->backing_hd;
947
948 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
949 return 1;
950 return (bs->encrypted && !bs->valid_key);
951}
952
bellardea2384d2004-08-01 21:59:26 +0000953int bdrv_set_key(BlockDriverState *bs, const char *key)
954{
955 int ret;
956 if (bs->backing_hd && bs->backing_hd->encrypted) {
957 ret = bdrv_set_key(bs->backing_hd, key);
958 if (ret < 0)
959 return ret;
960 if (!bs->encrypted)
961 return 0;
962 }
963 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
964 return -1;
aliguoric0f4ce72009-03-05 23:01:01 +0000965 ret = bs->drv->bdrv_set_key(bs, key);
aliguoribb5fc202009-03-05 23:01:15 +0000966 if (ret < 0) {
967 bs->valid_key = 0;
968 } else if (!bs->valid_key) {
969 bs->valid_key = 1;
970 /* call the change callback now, we skipped it on open */
971 bs->media_changed = 1;
972 if (bs->change_cb)
973 bs->change_cb(bs->change_opaque);
974 }
aliguoric0f4ce72009-03-05 23:01:01 +0000975 return ret;
bellardea2384d2004-08-01 21:59:26 +0000976}
977
978void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
979{
bellard19cb3732006-08-19 11:45:59 +0000980 if (!bs->drv) {
bellardea2384d2004-08-01 21:59:26 +0000981 buf[0] = '\0';
982 } else {
983 pstrcpy(buf, buf_size, bs->drv->format_name);
984 }
985}
986
ths5fafdf22007-09-16 21:08:06 +0000987void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
bellardea2384d2004-08-01 21:59:26 +0000988 void *opaque)
989{
990 BlockDriver *drv;
991
992 for (drv = first_drv; drv != NULL; drv = drv->next) {
993 it(opaque, drv->format_name);
994 }
995}
996
bellardb3380822004-03-14 21:38:54 +0000997BlockDriverState *bdrv_find(const char *name)
998{
999 BlockDriverState *bs;
1000
1001 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1002 if (!strcmp(name, bs->device_name))
1003 return bs;
1004 }
1005 return NULL;
1006}
1007
aliguori51de9762009-03-05 23:00:43 +00001008void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
bellard81d09122004-07-14 17:21:37 +00001009{
1010 BlockDriverState *bs;
1011
1012 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori51de9762009-03-05 23:00:43 +00001013 it(opaque, bs);
bellard81d09122004-07-14 17:21:37 +00001014 }
1015}
1016
bellardea2384d2004-08-01 21:59:26 +00001017const char *bdrv_get_device_name(BlockDriverState *bs)
1018{
1019 return bs->device_name;
1020}
1021
pbrook7a6cba62006-06-04 11:39:07 +00001022void bdrv_flush(BlockDriverState *bs)
1023{
aliguori081501d2009-03-29 01:31:51 +00001024 if (!bs->drv)
1025 return;
pbrook7a6cba62006-06-04 11:39:07 +00001026 if (bs->drv->bdrv_flush)
1027 bs->drv->bdrv_flush(bs);
1028 if (bs->backing_hd)
1029 bdrv_flush(bs->backing_hd);
1030}
1031
aliguoric6ca28d2008-10-06 13:55:43 +00001032void bdrv_flush_all(void)
1033{
1034 BlockDriverState *bs;
1035
1036 for (bs = bdrv_first; bs != NULL; bs = bs->next)
1037 if (bs->drv && !bdrv_is_read_only(bs) &&
1038 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1039 bdrv_flush(bs);
1040}
1041
thsf58c7b32008-06-05 21:53:49 +00001042/*
1043 * Returns true iff the specified sector is present in the disk image. Drivers
1044 * not implementing the functionality are assumed to not support backing files,
1045 * hence all their sectors are reported as allocated.
1046 *
1047 * 'pnum' is set to the number of sectors (including and immediately following
1048 * the specified sector) that are known to be in the same
1049 * allocated/unallocated state.
1050 *
1051 * 'nb_sectors' is the max value 'pnum' should be set to.
1052 */
1053int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1054 int *pnum)
1055{
1056 int64_t n;
1057 if (!bs->drv->bdrv_is_allocated) {
1058 if (sector_num >= bs->total_sectors) {
1059 *pnum = 0;
1060 return 0;
1061 }
1062 n = bs->total_sectors - sector_num;
1063 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1064 return 1;
1065 }
1066 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1067}
1068
aliguori376253e2009-03-05 23:01:23 +00001069void bdrv_info(Monitor *mon)
bellardb3380822004-03-14 21:38:54 +00001070{
1071 BlockDriverState *bs;
1072
1073 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori376253e2009-03-05 23:01:23 +00001074 monitor_printf(mon, "%s:", bs->device_name);
1075 monitor_printf(mon, " type=");
bellardb3380822004-03-14 21:38:54 +00001076 switch(bs->type) {
1077 case BDRV_TYPE_HD:
aliguori376253e2009-03-05 23:01:23 +00001078 monitor_printf(mon, "hd");
bellardb3380822004-03-14 21:38:54 +00001079 break;
1080 case BDRV_TYPE_CDROM:
aliguori376253e2009-03-05 23:01:23 +00001081 monitor_printf(mon, "cdrom");
bellardb3380822004-03-14 21:38:54 +00001082 break;
1083 case BDRV_TYPE_FLOPPY:
aliguori376253e2009-03-05 23:01:23 +00001084 monitor_printf(mon, "floppy");
bellardb3380822004-03-14 21:38:54 +00001085 break;
1086 }
aliguori376253e2009-03-05 23:01:23 +00001087 monitor_printf(mon, " removable=%d", bs->removable);
bellardb3380822004-03-14 21:38:54 +00001088 if (bs->removable) {
aliguori376253e2009-03-05 23:01:23 +00001089 monitor_printf(mon, " locked=%d", bs->locked);
bellardb3380822004-03-14 21:38:54 +00001090 }
bellard19cb3732006-08-19 11:45:59 +00001091 if (bs->drv) {
aliguori376253e2009-03-05 23:01:23 +00001092 monitor_printf(mon, " file=");
1093 monitor_print_filename(mon, bs->filename);
thsfef30742006-12-22 14:11:32 +00001094 if (bs->backing_file[0] != '\0') {
aliguori376253e2009-03-05 23:01:23 +00001095 monitor_printf(mon, " backing_file=");
1096 monitor_print_filename(mon, bs->backing_file);
1097 }
1098 monitor_printf(mon, " ro=%d", bs->read_only);
1099 monitor_printf(mon, " drv=%s", bs->drv->format_name);
1100 monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs));
bellardb3380822004-03-14 21:38:54 +00001101 } else {
aliguori376253e2009-03-05 23:01:23 +00001102 monitor_printf(mon, " [not inserted]");
bellardb3380822004-03-14 21:38:54 +00001103 }
aliguori376253e2009-03-05 23:01:23 +00001104 monitor_printf(mon, "\n");
bellardb3380822004-03-14 21:38:54 +00001105 }
1106}
thsa36e69d2007-12-02 05:18:19 +00001107
1108/* The "info blockstats" command. */
aliguori376253e2009-03-05 23:01:23 +00001109void bdrv_info_stats(Monitor *mon)
thsa36e69d2007-12-02 05:18:19 +00001110{
1111 BlockDriverState *bs;
1112
1113 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori376253e2009-03-05 23:01:23 +00001114 monitor_printf(mon, "%s:"
1115 " rd_bytes=%" PRIu64
1116 " wr_bytes=%" PRIu64
1117 " rd_operations=%" PRIu64
1118 " wr_operations=%" PRIu64
aliguoriebf53fc2009-03-11 20:05:29 +00001119 "\n",
aliguori376253e2009-03-05 23:01:23 +00001120 bs->device_name,
1121 bs->rd_bytes, bs->wr_bytes,
1122 bs->rd_ops, bs->wr_ops);
thsa36e69d2007-12-02 05:18:19 +00001123 }
1124}
bellardea2384d2004-08-01 21:59:26 +00001125
aliguori045df332009-03-05 23:00:48 +00001126const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1127{
1128 if (bs->backing_hd && bs->backing_hd->encrypted)
1129 return bs->backing_file;
1130 else if (bs->encrypted)
1131 return bs->filename;
1132 else
1133 return NULL;
1134}
1135
ths5fafdf22007-09-16 21:08:06 +00001136void bdrv_get_backing_filename(BlockDriverState *bs,
bellard83f64092006-08-01 16:21:11 +00001137 char *filename, int filename_size)
bellardea2384d2004-08-01 21:59:26 +00001138{
bellard83f64092006-08-01 16:21:11 +00001139 if (!bs->backing_hd) {
1140 pstrcpy(filename, filename_size, "");
1141 } else {
1142 pstrcpy(filename, filename_size, bs->backing_file);
1143 }
bellardea2384d2004-08-01 21:59:26 +00001144}
1145
ths5fafdf22007-09-16 21:08:06 +00001146int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
bellardfaea38e2006-08-05 21:31:00 +00001147 const uint8_t *buf, int nb_sectors)
1148{
1149 BlockDriver *drv = bs->drv;
1150 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001151 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001152 if (!drv->bdrv_write_compressed)
1153 return -ENOTSUP;
Kevin Wolffbb7b4e2009-05-08 14:47:24 +02001154 if (bdrv_check_request(bs, sector_num, nb_sectors))
1155 return -EIO;
bellardfaea38e2006-08-05 21:31:00 +00001156 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1157}
ths3b46e622007-09-17 08:09:54 +00001158
bellardfaea38e2006-08-05 21:31:00 +00001159int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1160{
1161 BlockDriver *drv = bs->drv;
1162 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001163 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001164 if (!drv->bdrv_get_info)
1165 return -ENOTSUP;
1166 memset(bdi, 0, sizeof(*bdi));
1167 return drv->bdrv_get_info(bs, bdi);
1168}
1169
aliguori178e08a2009-04-05 19:10:55 +00001170int bdrv_put_buffer(BlockDriverState *bs, const uint8_t *buf, int64_t pos, int size)
1171{
1172 BlockDriver *drv = bs->drv;
1173 if (!drv)
1174 return -ENOMEDIUM;
1175 if (!drv->bdrv_put_buffer)
1176 return -ENOTSUP;
1177 return drv->bdrv_put_buffer(bs, buf, pos, size);
1178}
1179
1180int bdrv_get_buffer(BlockDriverState *bs, uint8_t *buf, int64_t pos, int size)
1181{
1182 BlockDriver *drv = bs->drv;
1183 if (!drv)
1184 return -ENOMEDIUM;
1185 if (!drv->bdrv_get_buffer)
1186 return -ENOTSUP;
1187 return drv->bdrv_get_buffer(bs, buf, pos, size);
1188}
1189
bellardfaea38e2006-08-05 21:31:00 +00001190/**************************************************************/
1191/* handling of snapshots */
1192
ths5fafdf22007-09-16 21:08:06 +00001193int bdrv_snapshot_create(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001194 QEMUSnapshotInfo *sn_info)
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_create)
1200 return -ENOTSUP;
1201 return drv->bdrv_snapshot_create(bs, sn_info);
1202}
1203
ths5fafdf22007-09-16 21:08:06 +00001204int bdrv_snapshot_goto(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001205 const char *snapshot_id)
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_goto)
1211 return -ENOTSUP;
1212 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1213}
1214
1215int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1216{
1217 BlockDriver *drv = bs->drv;
1218 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001219 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001220 if (!drv->bdrv_snapshot_delete)
1221 return -ENOTSUP;
1222 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1223}
1224
ths5fafdf22007-09-16 21:08:06 +00001225int bdrv_snapshot_list(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001226 QEMUSnapshotInfo **psn_info)
1227{
1228 BlockDriver *drv = bs->drv;
1229 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001230 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001231 if (!drv->bdrv_snapshot_list)
1232 return -ENOTSUP;
1233 return drv->bdrv_snapshot_list(bs, psn_info);
1234}
1235
1236#define NB_SUFFIXES 4
1237
1238char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1239{
1240 static const char suffixes[NB_SUFFIXES] = "KMGT";
1241 int64_t base;
1242 int i;
1243
1244 if (size <= 999) {
1245 snprintf(buf, buf_size, "%" PRId64, size);
1246 } else {
1247 base = 1024;
1248 for(i = 0; i < NB_SUFFIXES; i++) {
1249 if (size < (10 * base)) {
ths5fafdf22007-09-16 21:08:06 +00001250 snprintf(buf, buf_size, "%0.1f%c",
bellardfaea38e2006-08-05 21:31:00 +00001251 (double)size / base,
1252 suffixes[i]);
1253 break;
1254 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
ths5fafdf22007-09-16 21:08:06 +00001255 snprintf(buf, buf_size, "%" PRId64 "%c",
bellardfaea38e2006-08-05 21:31:00 +00001256 ((size + (base >> 1)) / base),
1257 suffixes[i]);
1258 break;
1259 }
1260 base = base * 1024;
1261 }
1262 }
1263 return buf;
1264}
1265
1266char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1267{
1268 char buf1[128], date_buf[128], clock_buf[128];
bellard3b9f94e2007-01-07 17:27:07 +00001269#ifdef _WIN32
1270 struct tm *ptm;
1271#else
bellardfaea38e2006-08-05 21:31:00 +00001272 struct tm tm;
bellard3b9f94e2007-01-07 17:27:07 +00001273#endif
bellardfaea38e2006-08-05 21:31:00 +00001274 time_t ti;
1275 int64_t secs;
1276
1277 if (!sn) {
ths5fafdf22007-09-16 21:08:06 +00001278 snprintf(buf, buf_size,
1279 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001280 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1281 } else {
1282 ti = sn->date_sec;
bellard3b9f94e2007-01-07 17:27:07 +00001283#ifdef _WIN32
1284 ptm = localtime(&ti);
1285 strftime(date_buf, sizeof(date_buf),
1286 "%Y-%m-%d %H:%M:%S", ptm);
1287#else
bellardfaea38e2006-08-05 21:31:00 +00001288 localtime_r(&ti, &tm);
1289 strftime(date_buf, sizeof(date_buf),
1290 "%Y-%m-%d %H:%M:%S", &tm);
bellard3b9f94e2007-01-07 17:27:07 +00001291#endif
bellardfaea38e2006-08-05 21:31:00 +00001292 secs = sn->vm_clock_nsec / 1000000000;
1293 snprintf(clock_buf, sizeof(clock_buf),
1294 "%02d:%02d:%02d.%03d",
1295 (int)(secs / 3600),
1296 (int)((secs / 60) % 60),
ths5fafdf22007-09-16 21:08:06 +00001297 (int)(secs % 60),
bellardfaea38e2006-08-05 21:31:00 +00001298 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1299 snprintf(buf, buf_size,
ths5fafdf22007-09-16 21:08:06 +00001300 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001301 sn->id_str, sn->name,
1302 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1303 date_buf,
1304 clock_buf);
1305 }
1306 return buf;
1307}
1308
bellardea2384d2004-08-01 21:59:26 +00001309
bellard83f64092006-08-01 16:21:11 +00001310/**************************************************************/
1311/* async I/Os */
1312
aliguori3b69e4b2009-01-22 16:59:24 +00001313BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
aliguorif141eaf2009-04-07 18:43:24 +00001314 QEMUIOVector *qiov, int nb_sectors,
aliguori3b69e4b2009-01-22 16:59:24 +00001315 BlockDriverCompletionFunc *cb, void *opaque)
1316{
bellard83f64092006-08-01 16:21:11 +00001317 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001318 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001319
bellard19cb3732006-08-19 11:45:59 +00001320 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001321 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001322 if (bdrv_check_request(bs, sector_num, nb_sectors))
1323 return NULL;
ths3b46e622007-09-17 08:09:54 +00001324
aliguorif141eaf2009-04-07 18:43:24 +00001325 ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
1326 cb, opaque);
thsa36e69d2007-12-02 05:18:19 +00001327
1328 if (ret) {
1329 /* Update stats even though technically transfer has not happened. */
1330 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1331 bs->rd_ops ++;
1332 }
1333
1334 return ret;
bellard83f64092006-08-01 16:21:11 +00001335}
1336
aliguorif141eaf2009-04-07 18:43:24 +00001337BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1338 QEMUIOVector *qiov, int nb_sectors,
1339 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001340{
bellard83f64092006-08-01 16:21:11 +00001341 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001342 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001343
bellard19cb3732006-08-19 11:45:59 +00001344 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001345 return NULL;
bellard83f64092006-08-01 16:21:11 +00001346 if (bs->read_only)
pbrookce1a14d2006-08-07 02:38:06 +00001347 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001348 if (bdrv_check_request(bs, sector_num, nb_sectors))
1349 return NULL;
bellard83f64092006-08-01 16:21:11 +00001350
aliguorif141eaf2009-04-07 18:43:24 +00001351 ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
1352 cb, opaque);
thsa36e69d2007-12-02 05:18:19 +00001353
1354 if (ret) {
1355 /* Update stats even though technically transfer has not happened. */
1356 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1357 bs->wr_ops ++;
1358 }
1359
1360 return ret;
bellard83f64092006-08-01 16:21:11 +00001361}
1362
1363void bdrv_aio_cancel(BlockDriverAIOCB *acb)
pbrookce1a14d2006-08-07 02:38:06 +00001364{
aliguori6bbff9a2009-03-20 18:25:59 +00001365 acb->pool->cancel(acb);
bellard83f64092006-08-01 16:21:11 +00001366}
1367
pbrookce1a14d2006-08-07 02:38:06 +00001368
bellard83f64092006-08-01 16:21:11 +00001369/**************************************************************/
1370/* async block device emulation */
1371
bellard83f64092006-08-01 16:21:11 +00001372static void bdrv_aio_bh_cb(void *opaque)
bellardbeac80c2006-06-26 20:08:57 +00001373{
pbrookce1a14d2006-08-07 02:38:06 +00001374 BlockDriverAIOCBSync *acb = opaque;
aliguorif141eaf2009-04-07 18:43:24 +00001375
aliguorif141eaf2009-04-07 18:43:24 +00001376 if (!acb->is_write)
1377 qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
aliguoriceb42de2009-04-07 18:43:28 +00001378 qemu_vfree(acb->bounce);
pbrookce1a14d2006-08-07 02:38:06 +00001379 acb->common.cb(acb->common.opaque, acb->ret);
aliguorif141eaf2009-04-07 18:43:24 +00001380
pbrookce1a14d2006-08-07 02:38:06 +00001381 qemu_aio_release(acb);
bellardbeac80c2006-06-26 20:08:57 +00001382}
bellardbeac80c2006-06-26 20:08:57 +00001383
aliguorif141eaf2009-04-07 18:43:24 +00001384static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
1385 int64_t sector_num,
1386 QEMUIOVector *qiov,
1387 int nb_sectors,
1388 BlockDriverCompletionFunc *cb,
1389 void *opaque,
1390 int is_write)
1391
bellardea2384d2004-08-01 21:59:26 +00001392{
pbrookce1a14d2006-08-07 02:38:06 +00001393 BlockDriverAIOCBSync *acb;
pbrookce1a14d2006-08-07 02:38:06 +00001394
1395 acb = qemu_aio_get(bs, cb, opaque);
aliguorif141eaf2009-04-07 18:43:24 +00001396 acb->is_write = is_write;
1397 acb->qiov = qiov;
aliguorie268ca52009-04-22 20:20:00 +00001398 acb->bounce = qemu_blockalign(bs, qiov->size);
aliguorif141eaf2009-04-07 18:43:24 +00001399
pbrookce1a14d2006-08-07 02:38:06 +00001400 if (!acb->bh)
1401 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
aliguorif141eaf2009-04-07 18:43:24 +00001402
1403 if (is_write) {
1404 qemu_iovec_to_buffer(acb->qiov, acb->bounce);
1405 acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
1406 } else {
1407 acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
1408 }
1409
pbrookce1a14d2006-08-07 02:38:06 +00001410 qemu_bh_schedule(acb->bh);
aliguorif141eaf2009-04-07 18:43:24 +00001411
pbrookce1a14d2006-08-07 02:38:06 +00001412 return &acb->common;
pbrook7a6cba62006-06-04 11:39:07 +00001413}
1414
aliguorif141eaf2009-04-07 18:43:24 +00001415static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
1416 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
pbrookce1a14d2006-08-07 02:38:06 +00001417 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001418{
aliguorif141eaf2009-04-07 18:43:24 +00001419 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
bellard83f64092006-08-01 16:21:11 +00001420}
1421
aliguorif141eaf2009-04-07 18:43:24 +00001422static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
1423 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
1424 BlockDriverCompletionFunc *cb, void *opaque)
1425{
1426 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
1427}
1428
1429
pbrookce1a14d2006-08-07 02:38:06 +00001430static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
bellard83f64092006-08-01 16:21:11 +00001431{
pbrookce1a14d2006-08-07 02:38:06 +00001432 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1433 qemu_bh_cancel(acb->bh);
1434 qemu_aio_release(acb);
bellard83f64092006-08-01 16:21:11 +00001435}
bellard83f64092006-08-01 16:21:11 +00001436
1437/**************************************************************/
1438/* sync block device emulation */
1439
1440static void bdrv_rw_em_cb(void *opaque, int ret)
1441{
1442 *(int *)opaque = ret;
1443}
1444
1445#define NOT_DONE 0x7fffffff
1446
ths5fafdf22007-09-16 21:08:06 +00001447static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +00001448 uint8_t *buf, int nb_sectors)
1449{
pbrookce1a14d2006-08-07 02:38:06 +00001450 int async_ret;
1451 BlockDriverAIOCB *acb;
aliguorif141eaf2009-04-07 18:43:24 +00001452 struct iovec iov;
1453 QEMUIOVector qiov;
bellard83f64092006-08-01 16:21:11 +00001454
bellard83f64092006-08-01 16:21:11 +00001455 async_ret = NOT_DONE;
blueswir13f4cb3d2009-04-13 16:31:01 +00001456 iov.iov_base = (void *)buf;
aliguorif141eaf2009-04-07 18:43:24 +00001457 iov.iov_len = nb_sectors * 512;
1458 qemu_iovec_init_external(&qiov, &iov, 1);
1459 acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
1460 bdrv_rw_em_cb, &async_ret);
aliguoribaf35cb2008-09-10 15:45:19 +00001461 if (acb == NULL)
pbrookce1a14d2006-08-07 02:38:06 +00001462 return -1;
aliguoribaf35cb2008-09-10 15:45:19 +00001463
bellard83f64092006-08-01 16:21:11 +00001464 while (async_ret == NOT_DONE) {
1465 qemu_aio_wait();
1466 }
aliguoribaf35cb2008-09-10 15:45:19 +00001467
bellard83f64092006-08-01 16:21:11 +00001468 return async_ret;
1469}
1470
1471static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1472 const uint8_t *buf, int nb_sectors)
1473{
pbrookce1a14d2006-08-07 02:38:06 +00001474 int async_ret;
1475 BlockDriverAIOCB *acb;
aliguorif141eaf2009-04-07 18:43:24 +00001476 struct iovec iov;
1477 QEMUIOVector qiov;
bellard83f64092006-08-01 16:21:11 +00001478
bellard83f64092006-08-01 16:21:11 +00001479 async_ret = NOT_DONE;
aliguorif141eaf2009-04-07 18:43:24 +00001480 iov.iov_base = (void *)buf;
1481 iov.iov_len = nb_sectors * 512;
1482 qemu_iovec_init_external(&qiov, &iov, 1);
1483 acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
1484 bdrv_rw_em_cb, &async_ret);
aliguoribaf35cb2008-09-10 15:45:19 +00001485 if (acb == NULL)
pbrookce1a14d2006-08-07 02:38:06 +00001486 return -1;
bellard83f64092006-08-01 16:21:11 +00001487 while (async_ret == NOT_DONE) {
1488 qemu_aio_wait();
1489 }
bellard83f64092006-08-01 16:21:11 +00001490 return async_ret;
1491}
bellardea2384d2004-08-01 21:59:26 +00001492
1493void bdrv_init(void)
1494{
Anthony Liguori5efa9d52009-05-09 17:03:42 -05001495 module_call_init(MODULE_INIT_BLOCK);
bellardea2384d2004-08-01 21:59:26 +00001496}
pbrookce1a14d2006-08-07 02:38:06 +00001497
aliguori6bbff9a2009-03-20 18:25:59 +00001498void aio_pool_init(AIOPool *pool, int aiocb_size,
1499 void (*cancel)(BlockDriverAIOCB *acb))
pbrookce1a14d2006-08-07 02:38:06 +00001500{
aliguori6bbff9a2009-03-20 18:25:59 +00001501 pool->aiocb_size = aiocb_size;
1502 pool->cancel = cancel;
1503 pool->free_aiocb = NULL;
1504}
1505
1506void *qemu_aio_get_pool(AIOPool *pool, BlockDriverState *bs,
1507 BlockDriverCompletionFunc *cb, void *opaque)
1508{
pbrookce1a14d2006-08-07 02:38:06 +00001509 BlockDriverAIOCB *acb;
1510
aliguori6bbff9a2009-03-20 18:25:59 +00001511 if (pool->free_aiocb) {
1512 acb = pool->free_aiocb;
1513 pool->free_aiocb = acb->next;
pbrookce1a14d2006-08-07 02:38:06 +00001514 } else {
aliguori6bbff9a2009-03-20 18:25:59 +00001515 acb = qemu_mallocz(pool->aiocb_size);
1516 acb->pool = pool;
pbrookce1a14d2006-08-07 02:38:06 +00001517 }
1518 acb->bs = bs;
1519 acb->cb = cb;
1520 acb->opaque = opaque;
1521 return acb;
1522}
1523
aliguori6bbff9a2009-03-20 18:25:59 +00001524void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1525 void *opaque)
1526{
1527 return qemu_aio_get_pool(&bs->drv->aio_pool, bs, cb, opaque);
1528}
1529
pbrookce1a14d2006-08-07 02:38:06 +00001530void qemu_aio_release(void *p)
1531{
aliguori6bbff9a2009-03-20 18:25:59 +00001532 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
1533 AIOPool *pool = acb->pool;
1534 acb->next = pool->free_aiocb;
1535 pool->free_aiocb = acb;
pbrookce1a14d2006-08-07 02:38:06 +00001536}
bellard19cb3732006-08-19 11:45:59 +00001537
1538/**************************************************************/
1539/* removable device support */
1540
1541/**
1542 * Return TRUE if the media is present
1543 */
1544int bdrv_is_inserted(BlockDriverState *bs)
1545{
1546 BlockDriver *drv = bs->drv;
1547 int ret;
1548 if (!drv)
1549 return 0;
1550 if (!drv->bdrv_is_inserted)
1551 return 1;
1552 ret = drv->bdrv_is_inserted(bs);
1553 return ret;
1554}
1555
1556/**
1557 * Return TRUE if the media changed since the last call to this
ths5fafdf22007-09-16 21:08:06 +00001558 * function. It is currently only used for floppy disks
bellard19cb3732006-08-19 11:45:59 +00001559 */
1560int bdrv_media_changed(BlockDriverState *bs)
1561{
1562 BlockDriver *drv = bs->drv;
1563 int ret;
1564
1565 if (!drv || !drv->bdrv_media_changed)
1566 ret = -ENOTSUP;
1567 else
1568 ret = drv->bdrv_media_changed(bs);
1569 if (ret == -ENOTSUP)
1570 ret = bs->media_changed;
1571 bs->media_changed = 0;
1572 return ret;
1573}
1574
1575/**
1576 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1577 */
1578void bdrv_eject(BlockDriverState *bs, int eject_flag)
1579{
1580 BlockDriver *drv = bs->drv;
1581 int ret;
1582
1583 if (!drv || !drv->bdrv_eject) {
1584 ret = -ENOTSUP;
1585 } else {
1586 ret = drv->bdrv_eject(bs, eject_flag);
1587 }
1588 if (ret == -ENOTSUP) {
1589 if (eject_flag)
1590 bdrv_close(bs);
1591 }
1592}
1593
1594int bdrv_is_locked(BlockDriverState *bs)
1595{
1596 return bs->locked;
1597}
1598
1599/**
1600 * Lock or unlock the media (if it is locked, the user won't be able
1601 * to eject it manually).
1602 */
1603void bdrv_set_locked(BlockDriverState *bs, int locked)
1604{
1605 BlockDriver *drv = bs->drv;
1606
1607 bs->locked = locked;
1608 if (drv && drv->bdrv_set_locked) {
1609 drv->bdrv_set_locked(bs, locked);
1610 }
1611}
ths985a03b2007-12-24 16:10:43 +00001612
1613/* needed for generic scsi interface */
1614
1615int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1616{
1617 BlockDriver *drv = bs->drv;
1618
1619 if (drv && drv->bdrv_ioctl)
1620 return drv->bdrv_ioctl(bs, req, buf);
1621 return -ENOTSUP;
1622}
aliguori7d780662009-03-12 19:57:08 +00001623
aliguori221f7152009-03-28 17:28:41 +00001624BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
1625 unsigned long int req, void *buf,
1626 BlockDriverCompletionFunc *cb, void *opaque)
aliguori7d780662009-03-12 19:57:08 +00001627{
aliguori221f7152009-03-28 17:28:41 +00001628 BlockDriver *drv = bs->drv;
aliguori7d780662009-03-12 19:57:08 +00001629
aliguori221f7152009-03-28 17:28:41 +00001630 if (drv && drv->bdrv_aio_ioctl)
1631 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
1632 return NULL;
aliguori7d780662009-03-12 19:57:08 +00001633}
aliguorie268ca52009-04-22 20:20:00 +00001634
1635void *qemu_blockalign(BlockDriverState *bs, size_t size)
1636{
1637 return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
1638}