blob: 39f726cd6607b1705ba52dd00c6b7ce131456453 [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
aliguorif141eaf2009-04-07 18:43:24 +000051static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
52 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
aliguoric87c0672009-04-07 18:43:20 +000053 BlockDriverCompletionFunc *cb, void *opaque);
aliguorif141eaf2009-04-07 18:43:24 +000054static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
55 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
pbrookce1a14d2006-08-07 02:38:06 +000056 BlockDriverCompletionFunc *cb, void *opaque);
ths5fafdf22007-09-16 21:08:06 +000057static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +000058 uint8_t *buf, int nb_sectors);
59static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
60 const uint8_t *buf, int nb_sectors);
bellardec530c82006-04-25 22:36:06 +000061
blueswir17ee930d2008-09-17 19:04:14 +000062BlockDriverState *bdrv_first;
63
bellardea2384d2004-08-01 21:59:26 +000064static BlockDriver *first_drv;
65
bellard83f64092006-08-01 16:21:11 +000066int path_is_absolute(const char *path)
67{
68 const char *p;
bellard21664422007-01-07 18:22:37 +000069#ifdef _WIN32
70 /* specific case for names like: "\\.\d:" */
71 if (*path == '/' || *path == '\\')
72 return 1;
73#endif
bellard83f64092006-08-01 16:21:11 +000074 p = strchr(path, ':');
75 if (p)
76 p++;
77 else
78 p = path;
bellard3b9f94e2007-01-07 17:27:07 +000079#ifdef _WIN32
80 return (*p == '/' || *p == '\\');
81#else
82 return (*p == '/');
83#endif
bellard83f64092006-08-01 16:21:11 +000084}
85
86/* if filename is absolute, just copy it to dest. Otherwise, build a
87 path to it by considering it is relative to base_path. URL are
88 supported. */
89void path_combine(char *dest, int dest_size,
90 const char *base_path,
91 const char *filename)
92{
93 const char *p, *p1;
94 int len;
95
96 if (dest_size <= 0)
97 return;
98 if (path_is_absolute(filename)) {
99 pstrcpy(dest, dest_size, filename);
100 } else {
101 p = strchr(base_path, ':');
102 if (p)
103 p++;
104 else
105 p = base_path;
bellard3b9f94e2007-01-07 17:27:07 +0000106 p1 = strrchr(base_path, '/');
107#ifdef _WIN32
108 {
109 const char *p2;
110 p2 = strrchr(base_path, '\\');
111 if (!p1 || p2 > p1)
112 p1 = p2;
113 }
114#endif
bellard83f64092006-08-01 16:21:11 +0000115 if (p1)
116 p1++;
117 else
118 p1 = base_path;
119 if (p1 > p)
120 p = p1;
121 len = p - base_path;
122 if (len > dest_size - 1)
123 len = dest_size - 1;
124 memcpy(dest, base_path, len);
125 dest[len] = '\0';
126 pstrcat(dest, dest_size, filename);
127 }
128}
129
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500130void bdrv_register(BlockDriver *bdrv)
bellardea2384d2004-08-01 21:59:26 +0000131{
aliguorif141eaf2009-04-07 18:43:24 +0000132 if (!bdrv->bdrv_aio_readv) {
bellard83f64092006-08-01 16:21:11 +0000133 /* add AIO emulation layer */
aliguorif141eaf2009-04-07 18:43:24 +0000134 bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
135 bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
aliguorieda578e2009-03-12 19:57:16 +0000136 } else if (!bdrv->bdrv_read) {
bellard83f64092006-08-01 16:21:11 +0000137 /* add synchronous IO emulation layer */
138 bdrv->bdrv_read = bdrv_read_em;
139 bdrv->bdrv_write = bdrv_write_em;
140 }
bellardea2384d2004-08-01 21:59:26 +0000141 bdrv->next = first_drv;
142 first_drv = bdrv;
143}
bellardb3380822004-03-14 21:38:54 +0000144
145/* create a new block device (by default it is empty) */
146BlockDriverState *bdrv_new(const char *device_name)
bellardfc01f7e2003-06-30 10:03:06 +0000147{
bellardb3380822004-03-14 21:38:54 +0000148 BlockDriverState **pbs, *bs;
149
150 bs = qemu_mallocz(sizeof(BlockDriverState));
bellardb3380822004-03-14 21:38:54 +0000151 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
bellardea2384d2004-08-01 21:59:26 +0000152 if (device_name[0] != '\0') {
153 /* insert at the end */
154 pbs = &bdrv_first;
155 while (*pbs != NULL)
156 pbs = &(*pbs)->next;
157 *pbs = bs;
158 }
bellardb3380822004-03-14 21:38:54 +0000159 return bs;
160}
161
bellardea2384d2004-08-01 21:59:26 +0000162BlockDriver *bdrv_find_format(const char *format_name)
163{
164 BlockDriver *drv1;
165 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
166 if (!strcmp(drv1->format_name, format_name))
167 return drv1;
168 }
169 return NULL;
170}
171
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200172int bdrv_create(BlockDriver *drv, const char* filename,
173 QEMUOptionParameter *options)
bellardea2384d2004-08-01 21:59:26 +0000174{
175 if (!drv->bdrv_create)
176 return -ENOTSUP;
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200177
178 return drv->bdrv_create(filename, options);
bellardea2384d2004-08-01 21:59:26 +0000179}
180
bellardd5249392004-08-03 21:14:23 +0000181#ifdef _WIN32
bellard95389c82005-12-18 18:28:15 +0000182void get_tmp_filename(char *filename, int size)
bellardd5249392004-08-03 21:14:23 +0000183{
bellard3b9f94e2007-01-07 17:27:07 +0000184 char temp_dir[MAX_PATH];
ths3b46e622007-09-17 08:09:54 +0000185
bellard3b9f94e2007-01-07 17:27:07 +0000186 GetTempPath(MAX_PATH, temp_dir);
187 GetTempFileName(temp_dir, "qem", 0, filename);
bellardd5249392004-08-03 21:14:23 +0000188}
189#else
bellard95389c82005-12-18 18:28:15 +0000190void get_tmp_filename(char *filename, int size)
bellardea2384d2004-08-01 21:59:26 +0000191{
192 int fd;
blueswir17ccfb2e2008-09-14 06:45:34 +0000193 const char *tmpdir;
bellardd5249392004-08-03 21:14:23 +0000194 /* XXX: race condition possible */
aurel320badc1e2008-03-10 00:05:34 +0000195 tmpdir = getenv("TMPDIR");
196 if (!tmpdir)
197 tmpdir = "/tmp";
198 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
bellardea2384d2004-08-01 21:59:26 +0000199 fd = mkstemp(filename);
200 close(fd);
201}
bellardd5249392004-08-03 21:14:23 +0000202#endif
bellardea2384d2004-08-01 21:59:26 +0000203
bellard19cb3732006-08-19 11:45:59 +0000204#ifdef _WIN32
bellardf45512f2006-08-23 21:40:13 +0000205static int is_windows_drive_prefix(const char *filename)
206{
207 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
208 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
209 filename[1] == ':');
210}
ths3b46e622007-09-17 08:09:54 +0000211
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200212int is_windows_drive(const char *filename)
bellard19cb3732006-08-19 11:45:59 +0000213{
ths5fafdf22007-09-16 21:08:06 +0000214 if (is_windows_drive_prefix(filename) &&
bellardf45512f2006-08-23 21:40:13 +0000215 filename[2] == '\0')
bellard19cb3732006-08-19 11:45:59 +0000216 return 1;
217 if (strstart(filename, "\\\\.\\", NULL) ||
218 strstart(filename, "//./", NULL))
219 return 1;
220 return 0;
221}
222#endif
223
bellard83f64092006-08-01 16:21:11 +0000224static BlockDriver *find_protocol(const char *filename)
225{
226 BlockDriver *drv1;
227 char protocol[128];
Anthony Liguori1cec71e2009-07-02 08:12:26 -0500228 int len;
bellard83f64092006-08-01 16:21:11 +0000229 const char *p;
bellard19cb3732006-08-19 11:45:59 +0000230
231#ifdef _WIN32
bellardf45512f2006-08-23 21:40:13 +0000232 if (is_windows_drive(filename) ||
233 is_windows_drive_prefix(filename))
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500234 return bdrv_find_format("raw");
bellard19cb3732006-08-19 11:45:59 +0000235#endif
Anthony Liguori1cec71e2009-07-02 08:12:26 -0500236 p = strchr(filename, ':');
237 if (!p)
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500238 return bdrv_find_format("raw");
Anthony Liguori1cec71e2009-07-02 08:12:26 -0500239 len = p - filename;
240 if (len > sizeof(protocol) - 1)
241 len = sizeof(protocol) - 1;
242 memcpy(protocol, filename, len);
243 protocol[len] = '\0';
bellard83f64092006-08-01 16:21:11 +0000244 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
ths5fafdf22007-09-16 21:08:06 +0000245 if (drv1->protocol_name &&
bellard83f64092006-08-01 16:21:11 +0000246 !strcmp(drv1->protocol_name, protocol))
247 return drv1;
248 }
249 return NULL;
250}
251
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200252/*
253 * Detect host devices. By convention, /dev/cdrom[N] is always
254 * recognized as a host CDROM.
255 */
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200256static BlockDriver *find_hdev_driver(const char *filename)
257{
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200258 int score_max = 0, score;
259 BlockDriver *drv = NULL, *d;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200260
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200261 for (d = first_drv; d; d = d->next) {
262 if (d->bdrv_probe_device) {
263 score = d->bdrv_probe_device(filename);
264 if (score > score_max) {
265 score_max = score;
266 drv = d;
267 }
268 }
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200269 }
270
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200271 return drv;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200272}
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200273
bellardea2384d2004-08-01 21:59:26 +0000274static BlockDriver *find_image_format(const char *filename)
275{
bellard83f64092006-08-01 16:21:11 +0000276 int ret, score, score_max;
bellardea2384d2004-08-01 21:59:26 +0000277 BlockDriver *drv1, *drv;
bellard83f64092006-08-01 16:21:11 +0000278 uint8_t buf[2048];
279 BlockDriverState *bs;
ths3b46e622007-09-17 08:09:54 +0000280
bellard83f64092006-08-01 16:21:11 +0000281 drv = find_protocol(filename);
bellard19cb3732006-08-19 11:45:59 +0000282 /* no need to test disk image formats for vvfat */
Anthony Liguoric833ab72009-05-22 08:17:55 -0500283 if (drv && strcmp(drv->format_name, "vvfat") == 0)
bellard83f64092006-08-01 16:21:11 +0000284 return drv;
bellard19cb3732006-08-19 11:45:59 +0000285
bellard83f64092006-08-01 16:21:11 +0000286 ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
287 if (ret < 0)
288 return NULL;
289 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
290 bdrv_delete(bs);
291 if (ret < 0) {
292 return NULL;
293 }
294
bellardea2384d2004-08-01 21:59:26 +0000295 score_max = 0;
296 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
bellard83f64092006-08-01 16:21:11 +0000297 if (drv1->bdrv_probe) {
298 score = drv1->bdrv_probe(buf, ret, filename);
299 if (score > score_max) {
300 score_max = score;
301 drv = drv1;
302 }
bellardea2384d2004-08-01 21:59:26 +0000303 }
304 }
305 return drv;
306}
307
bellard83f64092006-08-01 16:21:11 +0000308int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
bellardb3380822004-03-14 21:38:54 +0000309{
bellard83f64092006-08-01 16:21:11 +0000310 BlockDriverState *bs;
311 int ret;
312
313 bs = bdrv_new("");
bellard83f64092006-08-01 16:21:11 +0000314 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
315 if (ret < 0) {
316 bdrv_delete(bs);
317 return ret;
bellard3b0d4f62005-10-30 18:30:10 +0000318 }
aliguori71d07702009-03-03 17:37:16 +0000319 bs->growable = 1;
bellard83f64092006-08-01 16:21:11 +0000320 *pbs = bs;
321 return 0;
bellardea2384d2004-08-01 21:59:26 +0000322}
bellardfc01f7e2003-06-30 10:03:06 +0000323
bellard83f64092006-08-01 16:21:11 +0000324int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
325{
326 return bdrv_open2(bs, filename, flags, NULL);
327}
328
329int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
bellardea2384d2004-08-01 21:59:26 +0000330 BlockDriver *drv)
331{
bellard83f64092006-08-01 16:21:11 +0000332 int ret, open_flags;
thseb5c8512007-02-11 15:06:09 +0000333 char tmp_filename[PATH_MAX];
334 char backing_filename[PATH_MAX];
ths3b46e622007-09-17 08:09:54 +0000335
bellardea2384d2004-08-01 21:59:26 +0000336 bs->read_only = 0;
337 bs->is_temporary = 0;
338 bs->encrypted = 0;
aliguoric0f4ce72009-03-05 23:01:01 +0000339 bs->valid_key = 0;
aliguorie268ca52009-04-22 20:20:00 +0000340 /* buffer_alignment defaulted to 512, drivers can change this value */
341 bs->buffer_alignment = 512;
bellard712e7872005-04-28 21:09:32 +0000342
bellard83f64092006-08-01 16:21:11 +0000343 if (flags & BDRV_O_SNAPSHOT) {
bellardea2384d2004-08-01 21:59:26 +0000344 BlockDriverState *bs1;
345 int64_t total_size;
aliguori7c96d462008-09-12 17:54:13 +0000346 int is_protocol = 0;
Kevin Wolf91a073a2009-05-27 14:48:06 +0200347 BlockDriver *bdrv_qcow2;
348 QEMUOptionParameter *options;
ths3b46e622007-09-17 08:09:54 +0000349
bellardea2384d2004-08-01 21:59:26 +0000350 /* if snapshot, we create a temporary backing file and open it
351 instead of opening 'filename' directly */
352
353 /* if there is a backing file, use it */
354 bs1 = bdrv_new("");
aliguori5eb45632009-03-28 17:55:10 +0000355 ret = bdrv_open2(bs1, filename, 0, drv);
aliguori51d7c002009-03-05 23:00:29 +0000356 if (ret < 0) {
bellardea2384d2004-08-01 21:59:26 +0000357 bdrv_delete(bs1);
aliguori51d7c002009-03-05 23:00:29 +0000358 return ret;
bellardea2384d2004-08-01 21:59:26 +0000359 }
bellard83f64092006-08-01 16:21:11 +0000360 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
aliguori7c96d462008-09-12 17:54:13 +0000361
362 if (bs1->drv && bs1->drv->protocol_name)
363 is_protocol = 1;
364
bellardea2384d2004-08-01 21:59:26 +0000365 bdrv_delete(bs1);
ths3b46e622007-09-17 08:09:54 +0000366
bellardea2384d2004-08-01 21:59:26 +0000367 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
aliguori7c96d462008-09-12 17:54:13 +0000368
369 /* Real path is meaningless for protocols */
370 if (is_protocol)
371 snprintf(backing_filename, sizeof(backing_filename),
372 "%s", filename);
373 else
374 realpath(filename, backing_filename);
375
Kevin Wolf91a073a2009-05-27 14:48:06 +0200376 bdrv_qcow2 = bdrv_find_format("qcow2");
377 options = parse_option_parameters("", bdrv_qcow2->create_options, NULL);
378
379 set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size * 512);
380 set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename);
381 if (drv) {
382 set_option_parameter(options, BLOCK_OPT_BACKING_FMT,
383 drv->format_name);
384 }
385
386 ret = bdrv_create(bdrv_qcow2, tmp_filename, options);
aliguori51d7c002009-03-05 23:00:29 +0000387 if (ret < 0) {
388 return ret;
bellardea2384d2004-08-01 21:59:26 +0000389 }
Kevin Wolf91a073a2009-05-27 14:48:06 +0200390
bellardea2384d2004-08-01 21:59:26 +0000391 filename = tmp_filename;
Kevin Wolf91a073a2009-05-27 14:48:06 +0200392 drv = bdrv_qcow2;
bellardea2384d2004-08-01 21:59:26 +0000393 bs->is_temporary = 1;
394 }
bellard712e7872005-04-28 21:09:32 +0000395
bellardea2384d2004-08-01 21:59:26 +0000396 pstrcpy(bs->filename, sizeof(bs->filename), filename);
bellard83f64092006-08-01 16:21:11 +0000397 if (flags & BDRV_O_FILE) {
398 drv = find_protocol(filename);
aliguori51d7c002009-03-05 23:00:29 +0000399 } else if (!drv) {
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200400 drv = find_hdev_driver(filename);
401 if (!drv) {
402 drv = find_image_format(filename);
403 }
aliguori51d7c002009-03-05 23:00:29 +0000404 }
405 if (!drv) {
406 ret = -ENOENT;
407 goto unlink_and_fail;
bellardea2384d2004-08-01 21:59:26 +0000408 }
409 bs->drv = drv;
410 bs->opaque = qemu_mallocz(drv->instance_size);
bellard83f64092006-08-01 16:21:11 +0000411 /* Note: for compatibility, we open disk image files as RDWR, and
412 RDONLY as fallback */
413 if (!(flags & BDRV_O_FILE))
aliguori9f7965c2008-10-14 14:42:54 +0000414 open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK);
bellard83f64092006-08-01 16:21:11 +0000415 else
416 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
Anthony Liguori1cec71e2009-07-02 08:12:26 -0500417 ret = drv->bdrv_open(bs, filename, open_flags);
aurel32a0a83532008-10-13 21:08:34 +0000418 if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
Anthony Liguori1cec71e2009-07-02 08:12:26 -0500419 ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
bellard83f64092006-08-01 16:21:11 +0000420 bs->read_only = 1;
421 }
bellardea2384d2004-08-01 21:59:26 +0000422 if (ret < 0) {
423 qemu_free(bs->opaque);
bellard6b21b972006-08-23 21:14:37 +0000424 bs->opaque = NULL;
425 bs->drv = NULL;
aliguori51d7c002009-03-05 23:00:29 +0000426 unlink_and_fail:
427 if (bs->is_temporary)
428 unlink(filename);
bellard83f64092006-08-01 16:21:11 +0000429 return ret;
bellardea2384d2004-08-01 21:59:26 +0000430 }
bellardd15a7712006-08-06 13:35:09 +0000431 if (drv->bdrv_getlength) {
432 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
433 }
bellardea2384d2004-08-01 21:59:26 +0000434#ifndef _WIN32
435 if (bs->is_temporary) {
436 unlink(filename);
437 }
438#endif
bellard83f64092006-08-01 16:21:11 +0000439 if (bs->backing_file[0] != '\0') {
bellardea2384d2004-08-01 21:59:26 +0000440 /* if there is a backing file, use it */
aliguori5eb45632009-03-28 17:55:10 +0000441 BlockDriver *back_drv = NULL;
bellardea2384d2004-08-01 21:59:26 +0000442 bs->backing_hd = bdrv_new("");
bellard83f64092006-08-01 16:21:11 +0000443 path_combine(backing_filename, sizeof(backing_filename),
444 filename, bs->backing_file);
aliguori5eb45632009-03-28 17:55:10 +0000445 if (bs->backing_format[0] != '\0')
446 back_drv = bdrv_find_format(bs->backing_format);
447 ret = bdrv_open2(bs->backing_hd, backing_filename, open_flags,
448 back_drv);
aliguori51d7c002009-03-05 23:00:29 +0000449 if (ret < 0) {
450 bdrv_close(bs);
451 return ret;
452 }
bellardea2384d2004-08-01 21:59:26 +0000453 }
454
aliguoribb5fc202009-03-05 23:01:15 +0000455 if (!bdrv_key_required(bs)) {
456 /* call the change callback */
457 bs->media_changed = 1;
458 if (bs->change_cb)
459 bs->change_cb(bs->change_opaque);
460 }
bellardb3380822004-03-14 21:38:54 +0000461 return 0;
bellardfc01f7e2003-06-30 10:03:06 +0000462}
463
464void bdrv_close(BlockDriverState *bs)
465{
bellard19cb3732006-08-19 11:45:59 +0000466 if (bs->drv) {
bellardea2384d2004-08-01 21:59:26 +0000467 if (bs->backing_hd)
468 bdrv_delete(bs->backing_hd);
469 bs->drv->bdrv_close(bs);
470 qemu_free(bs->opaque);
471#ifdef _WIN32
472 if (bs->is_temporary) {
473 unlink(bs->filename);
474 }
bellard67b915a2004-03-31 23:37:16 +0000475#endif
bellardea2384d2004-08-01 21:59:26 +0000476 bs->opaque = NULL;
477 bs->drv = NULL;
bellardb3380822004-03-14 21:38:54 +0000478
479 /* call the change callback */
bellard19cb3732006-08-19 11:45:59 +0000480 bs->media_changed = 1;
bellardb3380822004-03-14 21:38:54 +0000481 if (bs->change_cb)
482 bs->change_cb(bs->change_opaque);
483 }
484}
485
486void bdrv_delete(BlockDriverState *bs)
487{
aurel3234c6f052008-04-08 19:51:21 +0000488 BlockDriverState **pbs;
489
490 pbs = &bdrv_first;
491 while (*pbs != bs && *pbs != NULL)
492 pbs = &(*pbs)->next;
493 if (*pbs == bs)
494 *pbs = bs->next;
495
bellardb3380822004-03-14 21:38:54 +0000496 bdrv_close(bs);
497 qemu_free(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000498}
499
aliguorie97fc192009-04-21 23:11:50 +0000500/*
501 * Run consistency checks on an image
502 *
503 * Returns the number of errors or -errno when an internal error occurs
504 */
505int bdrv_check(BlockDriverState *bs)
506{
507 if (bs->drv->bdrv_check == NULL) {
508 return -ENOTSUP;
509 }
510
511 return bs->drv->bdrv_check(bs);
512}
513
bellard33e39632003-07-06 17:15:21 +0000514/* commit COW file into the raw image */
515int bdrv_commit(BlockDriverState *bs)
516{
bellard19cb3732006-08-19 11:45:59 +0000517 BlockDriver *drv = bs->drv;
bellard83f64092006-08-01 16:21:11 +0000518 int64_t i, total_sectors;
bellardea2384d2004-08-01 21:59:26 +0000519 int n, j;
520 unsigned char sector[512];
bellard33e39632003-07-06 17:15:21 +0000521
bellard19cb3732006-08-19 11:45:59 +0000522 if (!drv)
523 return -ENOMEDIUM;
bellard33e39632003-07-06 17:15:21 +0000524
525 if (bs->read_only) {
bellardea2384d2004-08-01 21:59:26 +0000526 return -EACCES;
bellard33e39632003-07-06 17:15:21 +0000527 }
528
bellardea2384d2004-08-01 21:59:26 +0000529 if (!bs->backing_hd) {
530 return -ENOTSUP;
bellard33e39632003-07-06 17:15:21 +0000531 }
bellardea2384d2004-08-01 21:59:26 +0000532
bellard83f64092006-08-01 16:21:11 +0000533 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
534 for (i = 0; i < total_sectors;) {
bellard19cb3732006-08-19 11:45:59 +0000535 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
bellardea2384d2004-08-01 21:59:26 +0000536 for(j = 0; j < n; j++) {
537 if (bdrv_read(bs, i, sector, 1) != 0) {
538 return -EIO;
539 }
540
541 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
542 return -EIO;
543 }
544 i++;
545 }
546 } else {
547 i += n;
548 }
549 }
bellard95389c82005-12-18 18:28:15 +0000550
bellard19cb3732006-08-19 11:45:59 +0000551 if (drv->bdrv_make_empty)
552 return drv->bdrv_make_empty(bs);
bellard95389c82005-12-18 18:28:15 +0000553
bellard33e39632003-07-06 17:15:21 +0000554 return 0;
555}
556
aliguori71d07702009-03-03 17:37:16 +0000557static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
558 size_t size)
559{
560 int64_t len;
561
562 if (!bdrv_is_inserted(bs))
563 return -ENOMEDIUM;
564
565 if (bs->growable)
566 return 0;
567
568 len = bdrv_getlength(bs);
569
Kevin Wolffbb7b4e2009-05-08 14:47:24 +0200570 if (offset < 0)
571 return -EIO;
572
573 if ((offset > len) || (len - offset < size))
aliguori71d07702009-03-03 17:37:16 +0000574 return -EIO;
575
576 return 0;
577}
578
579static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
580 int nb_sectors)
581{
aliguori999dec52009-03-29 01:31:48 +0000582 return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512);
aliguori71d07702009-03-03 17:37:16 +0000583}
584
bellard19cb3732006-08-19 11:45:59 +0000585/* return < 0 if error. See bdrv_write() for the return codes */
ths5fafdf22007-09-16 21:08:06 +0000586int bdrv_read(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000587 uint8_t *buf, int nb_sectors)
588{
bellardea2384d2004-08-01 21:59:26 +0000589 BlockDriver *drv = bs->drv;
590
bellard19cb3732006-08-19 11:45:59 +0000591 if (!drv)
592 return -ENOMEDIUM;
aliguori71d07702009-03-03 17:37:16 +0000593 if (bdrv_check_request(bs, sector_num, nb_sectors))
594 return -EIO;
bellardb3380822004-03-14 21:38:54 +0000595
aliguorieda578e2009-03-12 19:57:16 +0000596 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
bellardfc01f7e2003-06-30 10:03:06 +0000597}
598
ths5fafdf22007-09-16 21:08:06 +0000599/* Return < 0 if error. Important errors are:
bellard19cb3732006-08-19 11:45:59 +0000600 -EIO generic I/O error (may happen for all errors)
601 -ENOMEDIUM No media inserted.
602 -EINVAL Invalid sector number or nb_sectors
603 -EACCES Trying to write a read-only device
604*/
ths5fafdf22007-09-16 21:08:06 +0000605int bdrv_write(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000606 const uint8_t *buf, int nb_sectors)
607{
bellard83f64092006-08-01 16:21:11 +0000608 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +0000609 if (!bs->drv)
610 return -ENOMEDIUM;
bellard0849bf02003-06-30 23:17:31 +0000611 if (bs->read_only)
bellard19cb3732006-08-19 11:45:59 +0000612 return -EACCES;
aliguori71d07702009-03-03 17:37:16 +0000613 if (bdrv_check_request(bs, sector_num, nb_sectors))
614 return -EIO;
615
aliguori42fb2802009-01-15 20:43:39 +0000616 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
bellard83f64092006-08-01 16:21:11 +0000617}
618
aliguorieda578e2009-03-12 19:57:16 +0000619int bdrv_pread(BlockDriverState *bs, int64_t offset,
620 void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000621{
bellard83f64092006-08-01 16:21:11 +0000622 uint8_t tmp_buf[SECTOR_SIZE];
623 int len, nb_sectors, count;
624 int64_t sector_num;
625
626 count = count1;
627 /* first read to align to sector start */
628 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
629 if (len > count)
630 len = count;
631 sector_num = offset >> SECTOR_BITS;
632 if (len > 0) {
633 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
634 return -EIO;
635 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
636 count -= len;
637 if (count == 0)
638 return count1;
639 sector_num++;
640 buf += len;
641 }
642
643 /* read the sectors "in place" */
644 nb_sectors = count >> SECTOR_BITS;
645 if (nb_sectors > 0) {
646 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
647 return -EIO;
648 sector_num += nb_sectors;
649 len = nb_sectors << SECTOR_BITS;
650 buf += len;
651 count -= len;
652 }
653
654 /* add data from the last sector */
655 if (count > 0) {
656 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
657 return -EIO;
658 memcpy(buf, tmp_buf, count);
659 }
660 return count1;
661}
662
aliguorieda578e2009-03-12 19:57:16 +0000663int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
664 const void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000665{
bellard83f64092006-08-01 16:21:11 +0000666 uint8_t tmp_buf[SECTOR_SIZE];
667 int len, nb_sectors, count;
668 int64_t sector_num;
669
670 count = count1;
671 /* first write to align to sector start */
672 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
673 if (len > count)
674 len = count;
675 sector_num = offset >> SECTOR_BITS;
676 if (len > 0) {
677 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
678 return -EIO;
679 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
680 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
681 return -EIO;
682 count -= len;
683 if (count == 0)
684 return count1;
685 sector_num++;
686 buf += len;
687 }
688
689 /* write the sectors "in place" */
690 nb_sectors = count >> SECTOR_BITS;
691 if (nb_sectors > 0) {
692 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
693 return -EIO;
694 sector_num += nb_sectors;
695 len = nb_sectors << SECTOR_BITS;
696 buf += len;
697 count -= len;
698 }
699
700 /* add data from the last sector */
701 if (count > 0) {
702 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
703 return -EIO;
704 memcpy(tmp_buf, buf, count);
705 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
706 return -EIO;
707 }
708 return count1;
709}
bellard83f64092006-08-01 16:21:11 +0000710
711/**
bellard83f64092006-08-01 16:21:11 +0000712 * Truncate file to 'offset' bytes (needed only for file protocols)
713 */
714int bdrv_truncate(BlockDriverState *bs, int64_t offset)
715{
716 BlockDriver *drv = bs->drv;
717 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000718 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000719 if (!drv->bdrv_truncate)
720 return -ENOTSUP;
721 return drv->bdrv_truncate(bs, offset);
722}
723
724/**
725 * Length of a file in bytes. Return < 0 if error or unknown.
726 */
727int64_t bdrv_getlength(BlockDriverState *bs)
728{
729 BlockDriver *drv = bs->drv;
730 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000731 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000732 if (!drv->bdrv_getlength) {
733 /* legacy mode */
734 return bs->total_sectors * SECTOR_SIZE;
735 }
736 return drv->bdrv_getlength(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000737}
738
bellard19cb3732006-08-19 11:45:59 +0000739/* return 0 as number of sectors if no device present or error */
ths96b8f132007-12-17 01:35:20 +0000740void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
bellardfc01f7e2003-06-30 10:03:06 +0000741{
bellard19cb3732006-08-19 11:45:59 +0000742 int64_t length;
743 length = bdrv_getlength(bs);
744 if (length < 0)
745 length = 0;
746 else
747 length = length >> SECTOR_BITS;
748 *nb_sectors_ptr = length;
bellardfc01f7e2003-06-30 10:03:06 +0000749}
bellardcf989512004-02-16 21:56:36 +0000750
aliguorif3d54fc2008-11-25 21:50:24 +0000751struct partition {
752 uint8_t boot_ind; /* 0x80 - active */
753 uint8_t head; /* starting head */
754 uint8_t sector; /* starting sector */
755 uint8_t cyl; /* starting cylinder */
756 uint8_t sys_ind; /* What partition type */
757 uint8_t end_head; /* end head */
758 uint8_t end_sector; /* end sector */
759 uint8_t end_cyl; /* end cylinder */
760 uint32_t start_sect; /* starting sector counting from 0 */
761 uint32_t nr_sects; /* nr of sectors in partition */
762} __attribute__((packed));
763
764/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
765static int guess_disk_lchs(BlockDriverState *bs,
766 int *pcylinders, int *pheads, int *psectors)
767{
768 uint8_t buf[512];
769 int ret, i, heads, sectors, cylinders;
770 struct partition *p;
771 uint32_t nr_sects;
blueswir1a38131b2008-12-05 17:56:40 +0000772 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +0000773
774 bdrv_get_geometry(bs, &nb_sectors);
775
776 ret = bdrv_read(bs, 0, buf, 1);
777 if (ret < 0)
778 return -1;
779 /* test msdos magic */
780 if (buf[510] != 0x55 || buf[511] != 0xaa)
781 return -1;
782 for(i = 0; i < 4; i++) {
783 p = ((struct partition *)(buf + 0x1be)) + i;
784 nr_sects = le32_to_cpu(p->nr_sects);
785 if (nr_sects && p->end_head) {
786 /* We make the assumption that the partition terminates on
787 a cylinder boundary */
788 heads = p->end_head + 1;
789 sectors = p->end_sector & 63;
790 if (sectors == 0)
791 continue;
792 cylinders = nb_sectors / (heads * sectors);
793 if (cylinders < 1 || cylinders > 16383)
794 continue;
795 *pheads = heads;
796 *psectors = sectors;
797 *pcylinders = cylinders;
798#if 0
799 printf("guessed geometry: LCHS=%d %d %d\n",
800 cylinders, heads, sectors);
801#endif
802 return 0;
803 }
804 }
805 return -1;
806}
807
808void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
809{
810 int translation, lba_detected = 0;
811 int cylinders, heads, secs;
blueswir1a38131b2008-12-05 17:56:40 +0000812 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +0000813
814 /* if a geometry hint is available, use it */
815 bdrv_get_geometry(bs, &nb_sectors);
816 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
817 translation = bdrv_get_translation_hint(bs);
818 if (cylinders != 0) {
819 *pcyls = cylinders;
820 *pheads = heads;
821 *psecs = secs;
822 } else {
823 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
824 if (heads > 16) {
825 /* if heads > 16, it means that a BIOS LBA
826 translation was active, so the default
827 hardware geometry is OK */
828 lba_detected = 1;
829 goto default_geometry;
830 } else {
831 *pcyls = cylinders;
832 *pheads = heads;
833 *psecs = secs;
834 /* disable any translation to be in sync with
835 the logical geometry */
836 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
837 bdrv_set_translation_hint(bs,
838 BIOS_ATA_TRANSLATION_NONE);
839 }
840 }
841 } else {
842 default_geometry:
843 /* if no geometry, use a standard physical disk geometry */
844 cylinders = nb_sectors / (16 * 63);
845
846 if (cylinders > 16383)
847 cylinders = 16383;
848 else if (cylinders < 2)
849 cylinders = 2;
850 *pcyls = cylinders;
851 *pheads = 16;
852 *psecs = 63;
853 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
854 if ((*pcyls * *pheads) <= 131072) {
855 bdrv_set_translation_hint(bs,
856 BIOS_ATA_TRANSLATION_LARGE);
857 } else {
858 bdrv_set_translation_hint(bs,
859 BIOS_ATA_TRANSLATION_LBA);
860 }
861 }
862 }
863 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
864 }
865}
866
ths5fafdf22007-09-16 21:08:06 +0000867void bdrv_set_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000868 int cyls, int heads, int secs)
869{
870 bs->cyls = cyls;
871 bs->heads = heads;
872 bs->secs = secs;
873}
874
875void bdrv_set_type_hint(BlockDriverState *bs, int type)
876{
877 bs->type = type;
878 bs->removable = ((type == BDRV_TYPE_CDROM ||
879 type == BDRV_TYPE_FLOPPY));
880}
881
bellard46d47672004-11-16 01:45:27 +0000882void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
883{
884 bs->translation = translation;
885}
886
ths5fafdf22007-09-16 21:08:06 +0000887void bdrv_get_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000888 int *pcyls, int *pheads, int *psecs)
889{
890 *pcyls = bs->cyls;
891 *pheads = bs->heads;
892 *psecs = bs->secs;
893}
894
895int bdrv_get_type_hint(BlockDriverState *bs)
896{
897 return bs->type;
898}
899
bellard46d47672004-11-16 01:45:27 +0000900int bdrv_get_translation_hint(BlockDriverState *bs)
901{
902 return bs->translation;
903}
904
bellardb3380822004-03-14 21:38:54 +0000905int bdrv_is_removable(BlockDriverState *bs)
906{
907 return bs->removable;
908}
909
910int bdrv_is_read_only(BlockDriverState *bs)
911{
912 return bs->read_only;
913}
914
ths985a03b2007-12-24 16:10:43 +0000915int bdrv_is_sg(BlockDriverState *bs)
916{
917 return bs->sg;
918}
919
bellard19cb3732006-08-19 11:45:59 +0000920/* XXX: no longer used */
ths5fafdf22007-09-16 21:08:06 +0000921void bdrv_set_change_cb(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000922 void (*change_cb)(void *opaque), void *opaque)
923{
924 bs->change_cb = change_cb;
925 bs->change_opaque = opaque;
926}
927
bellardea2384d2004-08-01 21:59:26 +0000928int bdrv_is_encrypted(BlockDriverState *bs)
929{
930 if (bs->backing_hd && bs->backing_hd->encrypted)
931 return 1;
932 return bs->encrypted;
933}
934
aliguoric0f4ce72009-03-05 23:01:01 +0000935int bdrv_key_required(BlockDriverState *bs)
936{
937 BlockDriverState *backing_hd = bs->backing_hd;
938
939 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
940 return 1;
941 return (bs->encrypted && !bs->valid_key);
942}
943
bellardea2384d2004-08-01 21:59:26 +0000944int bdrv_set_key(BlockDriverState *bs, const char *key)
945{
946 int ret;
947 if (bs->backing_hd && bs->backing_hd->encrypted) {
948 ret = bdrv_set_key(bs->backing_hd, key);
949 if (ret < 0)
950 return ret;
951 if (!bs->encrypted)
952 return 0;
953 }
954 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
955 return -1;
aliguoric0f4ce72009-03-05 23:01:01 +0000956 ret = bs->drv->bdrv_set_key(bs, key);
aliguoribb5fc202009-03-05 23:01:15 +0000957 if (ret < 0) {
958 bs->valid_key = 0;
959 } else if (!bs->valid_key) {
960 bs->valid_key = 1;
961 /* call the change callback now, we skipped it on open */
962 bs->media_changed = 1;
963 if (bs->change_cb)
964 bs->change_cb(bs->change_opaque);
965 }
aliguoric0f4ce72009-03-05 23:01:01 +0000966 return ret;
bellardea2384d2004-08-01 21:59:26 +0000967}
968
969void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
970{
bellard19cb3732006-08-19 11:45:59 +0000971 if (!bs->drv) {
bellardea2384d2004-08-01 21:59:26 +0000972 buf[0] = '\0';
973 } else {
974 pstrcpy(buf, buf_size, bs->drv->format_name);
975 }
976}
977
ths5fafdf22007-09-16 21:08:06 +0000978void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
bellardea2384d2004-08-01 21:59:26 +0000979 void *opaque)
980{
981 BlockDriver *drv;
982
983 for (drv = first_drv; drv != NULL; drv = drv->next) {
984 it(opaque, drv->format_name);
985 }
986}
987
bellardb3380822004-03-14 21:38:54 +0000988BlockDriverState *bdrv_find(const char *name)
989{
990 BlockDriverState *bs;
991
992 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
993 if (!strcmp(name, bs->device_name))
994 return bs;
995 }
996 return NULL;
997}
998
aliguori51de9762009-03-05 23:00:43 +0000999void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
bellard81d09122004-07-14 17:21:37 +00001000{
1001 BlockDriverState *bs;
1002
1003 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori51de9762009-03-05 23:00:43 +00001004 it(opaque, bs);
bellard81d09122004-07-14 17:21:37 +00001005 }
1006}
1007
bellardea2384d2004-08-01 21:59:26 +00001008const char *bdrv_get_device_name(BlockDriverState *bs)
1009{
1010 return bs->device_name;
1011}
1012
pbrook7a6cba62006-06-04 11:39:07 +00001013void bdrv_flush(BlockDriverState *bs)
1014{
aliguori081501d2009-03-29 01:31:51 +00001015 if (!bs->drv)
1016 return;
pbrook7a6cba62006-06-04 11:39:07 +00001017 if (bs->drv->bdrv_flush)
1018 bs->drv->bdrv_flush(bs);
1019 if (bs->backing_hd)
1020 bdrv_flush(bs->backing_hd);
1021}
1022
aliguoric6ca28d2008-10-06 13:55:43 +00001023void bdrv_flush_all(void)
1024{
1025 BlockDriverState *bs;
1026
1027 for (bs = bdrv_first; bs != NULL; bs = bs->next)
1028 if (bs->drv && !bdrv_is_read_only(bs) &&
1029 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1030 bdrv_flush(bs);
1031}
1032
thsf58c7b32008-06-05 21:53:49 +00001033/*
1034 * Returns true iff the specified sector is present in the disk image. Drivers
1035 * not implementing the functionality are assumed to not support backing files,
1036 * hence all their sectors are reported as allocated.
1037 *
1038 * 'pnum' is set to the number of sectors (including and immediately following
1039 * the specified sector) that are known to be in the same
1040 * allocated/unallocated state.
1041 *
1042 * 'nb_sectors' is the max value 'pnum' should be set to.
1043 */
1044int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1045 int *pnum)
1046{
1047 int64_t n;
1048 if (!bs->drv->bdrv_is_allocated) {
1049 if (sector_num >= bs->total_sectors) {
1050 *pnum = 0;
1051 return 0;
1052 }
1053 n = bs->total_sectors - sector_num;
1054 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1055 return 1;
1056 }
1057 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1058}
1059
aliguori376253e2009-03-05 23:01:23 +00001060void bdrv_info(Monitor *mon)
bellardb3380822004-03-14 21:38:54 +00001061{
1062 BlockDriverState *bs;
1063
1064 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori376253e2009-03-05 23:01:23 +00001065 monitor_printf(mon, "%s:", bs->device_name);
1066 monitor_printf(mon, " type=");
bellardb3380822004-03-14 21:38:54 +00001067 switch(bs->type) {
1068 case BDRV_TYPE_HD:
aliguori376253e2009-03-05 23:01:23 +00001069 monitor_printf(mon, "hd");
bellardb3380822004-03-14 21:38:54 +00001070 break;
1071 case BDRV_TYPE_CDROM:
aliguori376253e2009-03-05 23:01:23 +00001072 monitor_printf(mon, "cdrom");
bellardb3380822004-03-14 21:38:54 +00001073 break;
1074 case BDRV_TYPE_FLOPPY:
aliguori376253e2009-03-05 23:01:23 +00001075 monitor_printf(mon, "floppy");
bellardb3380822004-03-14 21:38:54 +00001076 break;
1077 }
aliguori376253e2009-03-05 23:01:23 +00001078 monitor_printf(mon, " removable=%d", bs->removable);
bellardb3380822004-03-14 21:38:54 +00001079 if (bs->removable) {
aliguori376253e2009-03-05 23:01:23 +00001080 monitor_printf(mon, " locked=%d", bs->locked);
bellardb3380822004-03-14 21:38:54 +00001081 }
bellard19cb3732006-08-19 11:45:59 +00001082 if (bs->drv) {
aliguori376253e2009-03-05 23:01:23 +00001083 monitor_printf(mon, " file=");
1084 monitor_print_filename(mon, bs->filename);
thsfef30742006-12-22 14:11:32 +00001085 if (bs->backing_file[0] != '\0') {
aliguori376253e2009-03-05 23:01:23 +00001086 monitor_printf(mon, " backing_file=");
1087 monitor_print_filename(mon, bs->backing_file);
1088 }
1089 monitor_printf(mon, " ro=%d", bs->read_only);
1090 monitor_printf(mon, " drv=%s", bs->drv->format_name);
1091 monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs));
bellardb3380822004-03-14 21:38:54 +00001092 } else {
aliguori376253e2009-03-05 23:01:23 +00001093 monitor_printf(mon, " [not inserted]");
bellardb3380822004-03-14 21:38:54 +00001094 }
aliguori376253e2009-03-05 23:01:23 +00001095 monitor_printf(mon, "\n");
bellardb3380822004-03-14 21:38:54 +00001096 }
1097}
thsa36e69d2007-12-02 05:18:19 +00001098
1099/* The "info blockstats" command. */
aliguori376253e2009-03-05 23:01:23 +00001100void bdrv_info_stats(Monitor *mon)
thsa36e69d2007-12-02 05:18:19 +00001101{
1102 BlockDriverState *bs;
1103
1104 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori376253e2009-03-05 23:01:23 +00001105 monitor_printf(mon, "%s:"
1106 " rd_bytes=%" PRIu64
1107 " wr_bytes=%" PRIu64
1108 " rd_operations=%" PRIu64
1109 " wr_operations=%" PRIu64
aliguoriebf53fc2009-03-11 20:05:29 +00001110 "\n",
aliguori376253e2009-03-05 23:01:23 +00001111 bs->device_name,
1112 bs->rd_bytes, bs->wr_bytes,
1113 bs->rd_ops, bs->wr_ops);
thsa36e69d2007-12-02 05:18:19 +00001114 }
1115}
bellardea2384d2004-08-01 21:59:26 +00001116
aliguori045df332009-03-05 23:00:48 +00001117const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1118{
1119 if (bs->backing_hd && bs->backing_hd->encrypted)
1120 return bs->backing_file;
1121 else if (bs->encrypted)
1122 return bs->filename;
1123 else
1124 return NULL;
1125}
1126
ths5fafdf22007-09-16 21:08:06 +00001127void bdrv_get_backing_filename(BlockDriverState *bs,
bellard83f64092006-08-01 16:21:11 +00001128 char *filename, int filename_size)
bellardea2384d2004-08-01 21:59:26 +00001129{
bellard83f64092006-08-01 16:21:11 +00001130 if (!bs->backing_hd) {
1131 pstrcpy(filename, filename_size, "");
1132 } else {
1133 pstrcpy(filename, filename_size, bs->backing_file);
1134 }
bellardea2384d2004-08-01 21:59:26 +00001135}
1136
ths5fafdf22007-09-16 21:08:06 +00001137int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
bellardfaea38e2006-08-05 21:31:00 +00001138 const uint8_t *buf, int nb_sectors)
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_write_compressed)
1144 return -ENOTSUP;
Kevin Wolffbb7b4e2009-05-08 14:47:24 +02001145 if (bdrv_check_request(bs, sector_num, nb_sectors))
1146 return -EIO;
bellardfaea38e2006-08-05 21:31:00 +00001147 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1148}
ths3b46e622007-09-17 08:09:54 +00001149
bellardfaea38e2006-08-05 21:31:00 +00001150int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1151{
1152 BlockDriver *drv = bs->drv;
1153 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001154 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001155 if (!drv->bdrv_get_info)
1156 return -ENOTSUP;
1157 memset(bdi, 0, sizeof(*bdi));
1158 return drv->bdrv_get_info(bs, bdi);
1159}
1160
Christoph Hellwig45566e92009-07-10 23:11:57 +02001161int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
1162 int64_t pos, int size)
aliguori178e08a2009-04-05 19:10:55 +00001163{
1164 BlockDriver *drv = bs->drv;
1165 if (!drv)
1166 return -ENOMEDIUM;
Christoph Hellwig45566e92009-07-10 23:11:57 +02001167 if (!drv->bdrv_save_vmstate)
aliguori178e08a2009-04-05 19:10:55 +00001168 return -ENOTSUP;
Christoph Hellwig45566e92009-07-10 23:11:57 +02001169 return drv->bdrv_save_vmstate(bs, buf, pos, size);
aliguori178e08a2009-04-05 19:10:55 +00001170}
1171
Christoph Hellwig45566e92009-07-10 23:11:57 +02001172int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1173 int64_t pos, int size)
aliguori178e08a2009-04-05 19:10:55 +00001174{
1175 BlockDriver *drv = bs->drv;
1176 if (!drv)
1177 return -ENOMEDIUM;
Christoph Hellwig45566e92009-07-10 23:11:57 +02001178 if (!drv->bdrv_load_vmstate)
aliguori178e08a2009-04-05 19:10:55 +00001179 return -ENOTSUP;
Christoph Hellwig45566e92009-07-10 23:11:57 +02001180 return drv->bdrv_load_vmstate(bs, buf, pos, size);
aliguori178e08a2009-04-05 19:10:55 +00001181}
1182
bellardfaea38e2006-08-05 21:31:00 +00001183/**************************************************************/
1184/* handling of snapshots */
1185
ths5fafdf22007-09-16 21:08:06 +00001186int bdrv_snapshot_create(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001187 QEMUSnapshotInfo *sn_info)
1188{
1189 BlockDriver *drv = bs->drv;
1190 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001191 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001192 if (!drv->bdrv_snapshot_create)
1193 return -ENOTSUP;
1194 return drv->bdrv_snapshot_create(bs, sn_info);
1195}
1196
ths5fafdf22007-09-16 21:08:06 +00001197int bdrv_snapshot_goto(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001198 const char *snapshot_id)
1199{
1200 BlockDriver *drv = bs->drv;
1201 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001202 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001203 if (!drv->bdrv_snapshot_goto)
1204 return -ENOTSUP;
1205 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1206}
1207
1208int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1209{
1210 BlockDriver *drv = bs->drv;
1211 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001212 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001213 if (!drv->bdrv_snapshot_delete)
1214 return -ENOTSUP;
1215 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1216}
1217
ths5fafdf22007-09-16 21:08:06 +00001218int bdrv_snapshot_list(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001219 QEMUSnapshotInfo **psn_info)
1220{
1221 BlockDriver *drv = bs->drv;
1222 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001223 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001224 if (!drv->bdrv_snapshot_list)
1225 return -ENOTSUP;
1226 return drv->bdrv_snapshot_list(bs, psn_info);
1227}
1228
1229#define NB_SUFFIXES 4
1230
1231char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1232{
1233 static const char suffixes[NB_SUFFIXES] = "KMGT";
1234 int64_t base;
1235 int i;
1236
1237 if (size <= 999) {
1238 snprintf(buf, buf_size, "%" PRId64, size);
1239 } else {
1240 base = 1024;
1241 for(i = 0; i < NB_SUFFIXES; i++) {
1242 if (size < (10 * base)) {
ths5fafdf22007-09-16 21:08:06 +00001243 snprintf(buf, buf_size, "%0.1f%c",
bellardfaea38e2006-08-05 21:31:00 +00001244 (double)size / base,
1245 suffixes[i]);
1246 break;
1247 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
ths5fafdf22007-09-16 21:08:06 +00001248 snprintf(buf, buf_size, "%" PRId64 "%c",
bellardfaea38e2006-08-05 21:31:00 +00001249 ((size + (base >> 1)) / base),
1250 suffixes[i]);
1251 break;
1252 }
1253 base = base * 1024;
1254 }
1255 }
1256 return buf;
1257}
1258
1259char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1260{
1261 char buf1[128], date_buf[128], clock_buf[128];
bellard3b9f94e2007-01-07 17:27:07 +00001262#ifdef _WIN32
1263 struct tm *ptm;
1264#else
bellardfaea38e2006-08-05 21:31:00 +00001265 struct tm tm;
bellard3b9f94e2007-01-07 17:27:07 +00001266#endif
bellardfaea38e2006-08-05 21:31:00 +00001267 time_t ti;
1268 int64_t secs;
1269
1270 if (!sn) {
ths5fafdf22007-09-16 21:08:06 +00001271 snprintf(buf, buf_size,
1272 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001273 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1274 } else {
1275 ti = sn->date_sec;
bellard3b9f94e2007-01-07 17:27:07 +00001276#ifdef _WIN32
1277 ptm = localtime(&ti);
1278 strftime(date_buf, sizeof(date_buf),
1279 "%Y-%m-%d %H:%M:%S", ptm);
1280#else
bellardfaea38e2006-08-05 21:31:00 +00001281 localtime_r(&ti, &tm);
1282 strftime(date_buf, sizeof(date_buf),
1283 "%Y-%m-%d %H:%M:%S", &tm);
bellard3b9f94e2007-01-07 17:27:07 +00001284#endif
bellardfaea38e2006-08-05 21:31:00 +00001285 secs = sn->vm_clock_nsec / 1000000000;
1286 snprintf(clock_buf, sizeof(clock_buf),
1287 "%02d:%02d:%02d.%03d",
1288 (int)(secs / 3600),
1289 (int)((secs / 60) % 60),
ths5fafdf22007-09-16 21:08:06 +00001290 (int)(secs % 60),
bellardfaea38e2006-08-05 21:31:00 +00001291 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1292 snprintf(buf, buf_size,
ths5fafdf22007-09-16 21:08:06 +00001293 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001294 sn->id_str, sn->name,
1295 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1296 date_buf,
1297 clock_buf);
1298 }
1299 return buf;
1300}
1301
bellardea2384d2004-08-01 21:59:26 +00001302
bellard83f64092006-08-01 16:21:11 +00001303/**************************************************************/
1304/* async I/Os */
1305
aliguori3b69e4b2009-01-22 16:59:24 +00001306BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
aliguorif141eaf2009-04-07 18:43:24 +00001307 QEMUIOVector *qiov, int nb_sectors,
aliguori3b69e4b2009-01-22 16:59:24 +00001308 BlockDriverCompletionFunc *cb, void *opaque)
1309{
bellard83f64092006-08-01 16:21:11 +00001310 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001311 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001312
bellard19cb3732006-08-19 11:45:59 +00001313 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001314 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001315 if (bdrv_check_request(bs, sector_num, nb_sectors))
1316 return NULL;
ths3b46e622007-09-17 08:09:54 +00001317
aliguorif141eaf2009-04-07 18:43:24 +00001318 ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
1319 cb, opaque);
thsa36e69d2007-12-02 05:18:19 +00001320
1321 if (ret) {
1322 /* Update stats even though technically transfer has not happened. */
1323 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1324 bs->rd_ops ++;
1325 }
1326
1327 return ret;
bellard83f64092006-08-01 16:21:11 +00001328}
1329
aliguorif141eaf2009-04-07 18:43:24 +00001330BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1331 QEMUIOVector *qiov, int nb_sectors,
1332 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001333{
bellard83f64092006-08-01 16:21:11 +00001334 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001335 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001336
bellard19cb3732006-08-19 11:45:59 +00001337 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001338 return NULL;
bellard83f64092006-08-01 16:21:11 +00001339 if (bs->read_only)
pbrookce1a14d2006-08-07 02:38:06 +00001340 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001341 if (bdrv_check_request(bs, sector_num, nb_sectors))
1342 return NULL;
bellard83f64092006-08-01 16:21:11 +00001343
aliguorif141eaf2009-04-07 18:43:24 +00001344 ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
1345 cb, opaque);
thsa36e69d2007-12-02 05:18:19 +00001346
1347 if (ret) {
1348 /* Update stats even though technically transfer has not happened. */
1349 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1350 bs->wr_ops ++;
1351 }
1352
1353 return ret;
bellard83f64092006-08-01 16:21:11 +00001354}
1355
1356void bdrv_aio_cancel(BlockDriverAIOCB *acb)
pbrookce1a14d2006-08-07 02:38:06 +00001357{
aliguori6bbff9a2009-03-20 18:25:59 +00001358 acb->pool->cancel(acb);
bellard83f64092006-08-01 16:21:11 +00001359}
1360
pbrookce1a14d2006-08-07 02:38:06 +00001361
bellard83f64092006-08-01 16:21:11 +00001362/**************************************************************/
1363/* async block device emulation */
1364
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02001365typedef struct BlockDriverAIOCBSync {
1366 BlockDriverAIOCB common;
1367 QEMUBH *bh;
1368 int ret;
1369 /* vector translation state */
1370 QEMUIOVector *qiov;
1371 uint8_t *bounce;
1372 int is_write;
1373} BlockDriverAIOCBSync;
1374
1375static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1376{
1377 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
Dor Laor6a7ad292009-06-01 12:07:23 +03001378 qemu_bh_delete(acb->bh);
Avi Kivity36afc452009-06-23 16:20:36 +03001379 acb->bh = NULL;
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02001380 qemu_aio_release(acb);
1381}
1382
1383static AIOPool bdrv_em_aio_pool = {
1384 .aiocb_size = sizeof(BlockDriverAIOCBSync),
1385 .cancel = bdrv_aio_cancel_em,
1386};
1387
bellard83f64092006-08-01 16:21:11 +00001388static void bdrv_aio_bh_cb(void *opaque)
bellardbeac80c2006-06-26 20:08:57 +00001389{
pbrookce1a14d2006-08-07 02:38:06 +00001390 BlockDriverAIOCBSync *acb = opaque;
aliguorif141eaf2009-04-07 18:43:24 +00001391
aliguorif141eaf2009-04-07 18:43:24 +00001392 if (!acb->is_write)
1393 qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
aliguoriceb42de2009-04-07 18:43:28 +00001394 qemu_vfree(acb->bounce);
pbrookce1a14d2006-08-07 02:38:06 +00001395 acb->common.cb(acb->common.opaque, acb->ret);
Dor Laor6a7ad292009-06-01 12:07:23 +03001396 qemu_bh_delete(acb->bh);
Avi Kivity36afc452009-06-23 16:20:36 +03001397 acb->bh = NULL;
pbrookce1a14d2006-08-07 02:38:06 +00001398 qemu_aio_release(acb);
bellardbeac80c2006-06-26 20:08:57 +00001399}
bellardbeac80c2006-06-26 20:08:57 +00001400
aliguorif141eaf2009-04-07 18:43:24 +00001401static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
1402 int64_t sector_num,
1403 QEMUIOVector *qiov,
1404 int nb_sectors,
1405 BlockDriverCompletionFunc *cb,
1406 void *opaque,
1407 int is_write)
1408
bellardea2384d2004-08-01 21:59:26 +00001409{
pbrookce1a14d2006-08-07 02:38:06 +00001410 BlockDriverAIOCBSync *acb;
pbrookce1a14d2006-08-07 02:38:06 +00001411
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02001412 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
aliguorif141eaf2009-04-07 18:43:24 +00001413 acb->is_write = is_write;
1414 acb->qiov = qiov;
aliguorie268ca52009-04-22 20:20:00 +00001415 acb->bounce = qemu_blockalign(bs, qiov->size);
aliguorif141eaf2009-04-07 18:43:24 +00001416
pbrookce1a14d2006-08-07 02:38:06 +00001417 if (!acb->bh)
1418 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
aliguorif141eaf2009-04-07 18:43:24 +00001419
1420 if (is_write) {
1421 qemu_iovec_to_buffer(acb->qiov, acb->bounce);
1422 acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
1423 } else {
1424 acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
1425 }
1426
pbrookce1a14d2006-08-07 02:38:06 +00001427 qemu_bh_schedule(acb->bh);
aliguorif141eaf2009-04-07 18:43:24 +00001428
pbrookce1a14d2006-08-07 02:38:06 +00001429 return &acb->common;
pbrook7a6cba62006-06-04 11:39:07 +00001430}
1431
aliguorif141eaf2009-04-07 18:43:24 +00001432static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
1433 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
pbrookce1a14d2006-08-07 02:38:06 +00001434 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001435{
aliguorif141eaf2009-04-07 18:43:24 +00001436 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
bellard83f64092006-08-01 16:21:11 +00001437}
1438
aliguorif141eaf2009-04-07 18:43:24 +00001439static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
1440 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
1441 BlockDriverCompletionFunc *cb, void *opaque)
1442{
1443 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
1444}
1445
bellard83f64092006-08-01 16:21:11 +00001446/**************************************************************/
1447/* sync block device emulation */
1448
1449static void bdrv_rw_em_cb(void *opaque, int ret)
1450{
1451 *(int *)opaque = ret;
1452}
1453
1454#define NOT_DONE 0x7fffffff
1455
ths5fafdf22007-09-16 21:08:06 +00001456static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +00001457 uint8_t *buf, int nb_sectors)
1458{
pbrookce1a14d2006-08-07 02:38:06 +00001459 int async_ret;
1460 BlockDriverAIOCB *acb;
aliguorif141eaf2009-04-07 18:43:24 +00001461 struct iovec iov;
1462 QEMUIOVector qiov;
bellard83f64092006-08-01 16:21:11 +00001463
bellard83f64092006-08-01 16:21:11 +00001464 async_ret = NOT_DONE;
blueswir13f4cb3d2009-04-13 16:31:01 +00001465 iov.iov_base = (void *)buf;
aliguorif141eaf2009-04-07 18:43:24 +00001466 iov.iov_len = nb_sectors * 512;
1467 qemu_iovec_init_external(&qiov, &iov, 1);
1468 acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
1469 bdrv_rw_em_cb, &async_ret);
aliguoribaf35cb2008-09-10 15:45:19 +00001470 if (acb == NULL)
pbrookce1a14d2006-08-07 02:38:06 +00001471 return -1;
aliguoribaf35cb2008-09-10 15:45:19 +00001472
bellard83f64092006-08-01 16:21:11 +00001473 while (async_ret == NOT_DONE) {
1474 qemu_aio_wait();
1475 }
aliguoribaf35cb2008-09-10 15:45:19 +00001476
bellard83f64092006-08-01 16:21:11 +00001477 return async_ret;
1478}
1479
1480static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1481 const uint8_t *buf, int nb_sectors)
1482{
pbrookce1a14d2006-08-07 02:38:06 +00001483 int async_ret;
1484 BlockDriverAIOCB *acb;
aliguorif141eaf2009-04-07 18:43:24 +00001485 struct iovec iov;
1486 QEMUIOVector qiov;
bellard83f64092006-08-01 16:21:11 +00001487
bellard83f64092006-08-01 16:21:11 +00001488 async_ret = NOT_DONE;
aliguorif141eaf2009-04-07 18:43:24 +00001489 iov.iov_base = (void *)buf;
1490 iov.iov_len = nb_sectors * 512;
1491 qemu_iovec_init_external(&qiov, &iov, 1);
1492 acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
1493 bdrv_rw_em_cb, &async_ret);
aliguoribaf35cb2008-09-10 15:45:19 +00001494 if (acb == NULL)
pbrookce1a14d2006-08-07 02:38:06 +00001495 return -1;
bellard83f64092006-08-01 16:21:11 +00001496 while (async_ret == NOT_DONE) {
1497 qemu_aio_wait();
1498 }
bellard83f64092006-08-01 16:21:11 +00001499 return async_ret;
1500}
bellardea2384d2004-08-01 21:59:26 +00001501
1502void bdrv_init(void)
1503{
Anthony Liguori5efa9d52009-05-09 17:03:42 -05001504 module_call_init(MODULE_INIT_BLOCK);
bellardea2384d2004-08-01 21:59:26 +00001505}
pbrookce1a14d2006-08-07 02:38:06 +00001506
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02001507void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
1508 BlockDriverCompletionFunc *cb, void *opaque)
aliguori6bbff9a2009-03-20 18:25:59 +00001509{
pbrookce1a14d2006-08-07 02:38:06 +00001510 BlockDriverAIOCB *acb;
1511
aliguori6bbff9a2009-03-20 18:25:59 +00001512 if (pool->free_aiocb) {
1513 acb = pool->free_aiocb;
1514 pool->free_aiocb = acb->next;
pbrookce1a14d2006-08-07 02:38:06 +00001515 } else {
aliguori6bbff9a2009-03-20 18:25:59 +00001516 acb = qemu_mallocz(pool->aiocb_size);
1517 acb->pool = pool;
pbrookce1a14d2006-08-07 02:38:06 +00001518 }
1519 acb->bs = bs;
1520 acb->cb = cb;
1521 acb->opaque = opaque;
1522 return acb;
1523}
1524
1525void qemu_aio_release(void *p)
1526{
aliguori6bbff9a2009-03-20 18:25:59 +00001527 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
1528 AIOPool *pool = acb->pool;
1529 acb->next = pool->free_aiocb;
1530 pool->free_aiocb = acb;
pbrookce1a14d2006-08-07 02:38:06 +00001531}
bellard19cb3732006-08-19 11:45:59 +00001532
1533/**************************************************************/
1534/* removable device support */
1535
1536/**
1537 * Return TRUE if the media is present
1538 */
1539int bdrv_is_inserted(BlockDriverState *bs)
1540{
1541 BlockDriver *drv = bs->drv;
1542 int ret;
1543 if (!drv)
1544 return 0;
1545 if (!drv->bdrv_is_inserted)
1546 return 1;
1547 ret = drv->bdrv_is_inserted(bs);
1548 return ret;
1549}
1550
1551/**
1552 * Return TRUE if the media changed since the last call to this
ths5fafdf22007-09-16 21:08:06 +00001553 * function. It is currently only used for floppy disks
bellard19cb3732006-08-19 11:45:59 +00001554 */
1555int bdrv_media_changed(BlockDriverState *bs)
1556{
1557 BlockDriver *drv = bs->drv;
1558 int ret;
1559
1560 if (!drv || !drv->bdrv_media_changed)
1561 ret = -ENOTSUP;
1562 else
1563 ret = drv->bdrv_media_changed(bs);
1564 if (ret == -ENOTSUP)
1565 ret = bs->media_changed;
1566 bs->media_changed = 0;
1567 return ret;
1568}
1569
1570/**
1571 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1572 */
Mark McLoughlinaea2a332009-05-27 10:06:11 +01001573int bdrv_eject(BlockDriverState *bs, int eject_flag)
bellard19cb3732006-08-19 11:45:59 +00001574{
1575 BlockDriver *drv = bs->drv;
1576 int ret;
1577
Mark McLoughlinaea2a332009-05-27 10:06:11 +01001578 if (bs->locked) {
1579 return -EBUSY;
1580 }
1581
bellard19cb3732006-08-19 11:45:59 +00001582 if (!drv || !drv->bdrv_eject) {
1583 ret = -ENOTSUP;
1584 } else {
1585 ret = drv->bdrv_eject(bs, eject_flag);
1586 }
1587 if (ret == -ENOTSUP) {
1588 if (eject_flag)
1589 bdrv_close(bs);
Mark McLoughlinaea2a332009-05-27 10:06:11 +01001590 ret = 0;
bellard19cb3732006-08-19 11:45:59 +00001591 }
Mark McLoughlinaea2a332009-05-27 10:06:11 +01001592
1593 return ret;
bellard19cb3732006-08-19 11:45:59 +00001594}
1595
1596int bdrv_is_locked(BlockDriverState *bs)
1597{
1598 return bs->locked;
1599}
1600
1601/**
1602 * Lock or unlock the media (if it is locked, the user won't be able
1603 * to eject it manually).
1604 */
1605void bdrv_set_locked(BlockDriverState *bs, int locked)
1606{
1607 BlockDriver *drv = bs->drv;
1608
1609 bs->locked = locked;
1610 if (drv && drv->bdrv_set_locked) {
1611 drv->bdrv_set_locked(bs, locked);
1612 }
1613}
ths985a03b2007-12-24 16:10:43 +00001614
1615/* needed for generic scsi interface */
1616
1617int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1618{
1619 BlockDriver *drv = bs->drv;
1620
1621 if (drv && drv->bdrv_ioctl)
1622 return drv->bdrv_ioctl(bs, req, buf);
1623 return -ENOTSUP;
1624}
aliguori7d780662009-03-12 19:57:08 +00001625
aliguori221f7152009-03-28 17:28:41 +00001626BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
1627 unsigned long int req, void *buf,
1628 BlockDriverCompletionFunc *cb, void *opaque)
aliguori7d780662009-03-12 19:57:08 +00001629{
aliguori221f7152009-03-28 17:28:41 +00001630 BlockDriver *drv = bs->drv;
aliguori7d780662009-03-12 19:57:08 +00001631
aliguori221f7152009-03-28 17:28:41 +00001632 if (drv && drv->bdrv_aio_ioctl)
1633 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
1634 return NULL;
aliguori7d780662009-03-12 19:57:08 +00001635}
aliguorie268ca52009-04-22 20:20:00 +00001636
1637void *qemu_blockalign(BlockDriverState *bs, size_t size)
1638{
1639 return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
1640}