blob: da0930cbbabdf97d0e8f8746f8a3fb1963fc4274 [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"
Juan Quintela71e72a12009-07-27 16:12:56 +020025#ifdef CONFIG_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
Juan Quintela71e72a12009-07-27 16:12:56 +020035#ifdef CONFIG_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);
Christoph Hellwige900a7b2009-09-04 19:01:15 +0200411
412 /*
413 * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a
414 * write cache to the guest. We do need the fdatasync to flush
415 * out transactions for block allocations, and we maybe have a
416 * volatile write cache in our backing device to deal with.
417 */
418 if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE))
419 bs->enable_write_cache = 1;
420
bellard83f64092006-08-01 16:21:11 +0000421 /* Note: for compatibility, we open disk image files as RDWR, and
422 RDONLY as fallback */
423 if (!(flags & BDRV_O_FILE))
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200424 open_flags = BDRV_O_RDWR |
425 (flags & (BDRV_O_CACHE_MASK|BDRV_O_NATIVE_AIO));
bellard83f64092006-08-01 16:21:11 +0000426 else
427 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
Anthony Liguori1cec71e2009-07-02 08:12:26 -0500428 ret = drv->bdrv_open(bs, filename, open_flags);
aurel32a0a83532008-10-13 21:08:34 +0000429 if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
Anthony Liguori1cec71e2009-07-02 08:12:26 -0500430 ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
bellard83f64092006-08-01 16:21:11 +0000431 bs->read_only = 1;
432 }
bellardea2384d2004-08-01 21:59:26 +0000433 if (ret < 0) {
434 qemu_free(bs->opaque);
bellard6b21b972006-08-23 21:14:37 +0000435 bs->opaque = NULL;
436 bs->drv = NULL;
aliguori51d7c002009-03-05 23:00:29 +0000437 unlink_and_fail:
438 if (bs->is_temporary)
439 unlink(filename);
bellard83f64092006-08-01 16:21:11 +0000440 return ret;
bellardea2384d2004-08-01 21:59:26 +0000441 }
bellardd15a7712006-08-06 13:35:09 +0000442 if (drv->bdrv_getlength) {
443 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
444 }
bellardea2384d2004-08-01 21:59:26 +0000445#ifndef _WIN32
446 if (bs->is_temporary) {
447 unlink(filename);
448 }
449#endif
bellard83f64092006-08-01 16:21:11 +0000450 if (bs->backing_file[0] != '\0') {
bellardea2384d2004-08-01 21:59:26 +0000451 /* if there is a backing file, use it */
aliguori5eb45632009-03-28 17:55:10 +0000452 BlockDriver *back_drv = NULL;
bellardea2384d2004-08-01 21:59:26 +0000453 bs->backing_hd = bdrv_new("");
bellard83f64092006-08-01 16:21:11 +0000454 path_combine(backing_filename, sizeof(backing_filename),
455 filename, bs->backing_file);
aliguori5eb45632009-03-28 17:55:10 +0000456 if (bs->backing_format[0] != '\0')
457 back_drv = bdrv_find_format(bs->backing_format);
458 ret = bdrv_open2(bs->backing_hd, backing_filename, open_flags,
459 back_drv);
aliguori51d7c002009-03-05 23:00:29 +0000460 if (ret < 0) {
461 bdrv_close(bs);
462 return ret;
463 }
bellardea2384d2004-08-01 21:59:26 +0000464 }
465
aliguoribb5fc202009-03-05 23:01:15 +0000466 if (!bdrv_key_required(bs)) {
467 /* call the change callback */
468 bs->media_changed = 1;
469 if (bs->change_cb)
470 bs->change_cb(bs->change_opaque);
471 }
bellardb3380822004-03-14 21:38:54 +0000472 return 0;
bellardfc01f7e2003-06-30 10:03:06 +0000473}
474
475void bdrv_close(BlockDriverState *bs)
476{
bellard19cb3732006-08-19 11:45:59 +0000477 if (bs->drv) {
bellardea2384d2004-08-01 21:59:26 +0000478 if (bs->backing_hd)
479 bdrv_delete(bs->backing_hd);
480 bs->drv->bdrv_close(bs);
481 qemu_free(bs->opaque);
482#ifdef _WIN32
483 if (bs->is_temporary) {
484 unlink(bs->filename);
485 }
bellard67b915a2004-03-31 23:37:16 +0000486#endif
bellardea2384d2004-08-01 21:59:26 +0000487 bs->opaque = NULL;
488 bs->drv = NULL;
bellardb3380822004-03-14 21:38:54 +0000489
490 /* call the change callback */
bellard19cb3732006-08-19 11:45:59 +0000491 bs->media_changed = 1;
bellardb3380822004-03-14 21:38:54 +0000492 if (bs->change_cb)
493 bs->change_cb(bs->change_opaque);
494 }
495}
496
497void bdrv_delete(BlockDriverState *bs)
498{
aurel3234c6f052008-04-08 19:51:21 +0000499 BlockDriverState **pbs;
500
501 pbs = &bdrv_first;
502 while (*pbs != bs && *pbs != NULL)
503 pbs = &(*pbs)->next;
504 if (*pbs == bs)
505 *pbs = bs->next;
506
bellardb3380822004-03-14 21:38:54 +0000507 bdrv_close(bs);
508 qemu_free(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000509}
510
aliguorie97fc192009-04-21 23:11:50 +0000511/*
512 * Run consistency checks on an image
513 *
514 * Returns the number of errors or -errno when an internal error occurs
515 */
516int bdrv_check(BlockDriverState *bs)
517{
518 if (bs->drv->bdrv_check == NULL) {
519 return -ENOTSUP;
520 }
521
522 return bs->drv->bdrv_check(bs);
523}
524
bellard33e39632003-07-06 17:15:21 +0000525/* commit COW file into the raw image */
526int bdrv_commit(BlockDriverState *bs)
527{
bellard19cb3732006-08-19 11:45:59 +0000528 BlockDriver *drv = bs->drv;
bellard83f64092006-08-01 16:21:11 +0000529 int64_t i, total_sectors;
bellardea2384d2004-08-01 21:59:26 +0000530 int n, j;
531 unsigned char sector[512];
bellard33e39632003-07-06 17:15:21 +0000532
bellard19cb3732006-08-19 11:45:59 +0000533 if (!drv)
534 return -ENOMEDIUM;
bellard33e39632003-07-06 17:15:21 +0000535
536 if (bs->read_only) {
bellardea2384d2004-08-01 21:59:26 +0000537 return -EACCES;
bellard33e39632003-07-06 17:15:21 +0000538 }
539
bellardea2384d2004-08-01 21:59:26 +0000540 if (!bs->backing_hd) {
541 return -ENOTSUP;
bellard33e39632003-07-06 17:15:21 +0000542 }
bellardea2384d2004-08-01 21:59:26 +0000543
bellard83f64092006-08-01 16:21:11 +0000544 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
545 for (i = 0; i < total_sectors;) {
bellard19cb3732006-08-19 11:45:59 +0000546 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
bellardea2384d2004-08-01 21:59:26 +0000547 for(j = 0; j < n; j++) {
548 if (bdrv_read(bs, i, sector, 1) != 0) {
549 return -EIO;
550 }
551
552 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
553 return -EIO;
554 }
555 i++;
556 }
557 } else {
558 i += n;
559 }
560 }
bellard95389c82005-12-18 18:28:15 +0000561
bellard19cb3732006-08-19 11:45:59 +0000562 if (drv->bdrv_make_empty)
563 return drv->bdrv_make_empty(bs);
bellard95389c82005-12-18 18:28:15 +0000564
bellard33e39632003-07-06 17:15:21 +0000565 return 0;
566}
567
aliguori71d07702009-03-03 17:37:16 +0000568static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
569 size_t size)
570{
571 int64_t len;
572
573 if (!bdrv_is_inserted(bs))
574 return -ENOMEDIUM;
575
576 if (bs->growable)
577 return 0;
578
579 len = bdrv_getlength(bs);
580
Kevin Wolffbb7b4e2009-05-08 14:47:24 +0200581 if (offset < 0)
582 return -EIO;
583
584 if ((offset > len) || (len - offset < size))
aliguori71d07702009-03-03 17:37:16 +0000585 return -EIO;
586
587 return 0;
588}
589
590static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
591 int nb_sectors)
592{
aliguori999dec52009-03-29 01:31:48 +0000593 return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512);
aliguori71d07702009-03-03 17:37:16 +0000594}
595
bellard19cb3732006-08-19 11:45:59 +0000596/* return < 0 if error. See bdrv_write() for the return codes */
ths5fafdf22007-09-16 21:08:06 +0000597int bdrv_read(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000598 uint8_t *buf, int nb_sectors)
599{
bellardea2384d2004-08-01 21:59:26 +0000600 BlockDriver *drv = bs->drv;
601
bellard19cb3732006-08-19 11:45:59 +0000602 if (!drv)
603 return -ENOMEDIUM;
aliguori71d07702009-03-03 17:37:16 +0000604 if (bdrv_check_request(bs, sector_num, nb_sectors))
605 return -EIO;
bellardb3380822004-03-14 21:38:54 +0000606
aliguorieda578e2009-03-12 19:57:16 +0000607 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
bellardfc01f7e2003-06-30 10:03:06 +0000608}
609
ths5fafdf22007-09-16 21:08:06 +0000610/* Return < 0 if error. Important errors are:
bellard19cb3732006-08-19 11:45:59 +0000611 -EIO generic I/O error (may happen for all errors)
612 -ENOMEDIUM No media inserted.
613 -EINVAL Invalid sector number or nb_sectors
614 -EACCES Trying to write a read-only device
615*/
ths5fafdf22007-09-16 21:08:06 +0000616int bdrv_write(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000617 const uint8_t *buf, int nb_sectors)
618{
bellard83f64092006-08-01 16:21:11 +0000619 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +0000620 if (!bs->drv)
621 return -ENOMEDIUM;
bellard0849bf02003-06-30 23:17:31 +0000622 if (bs->read_only)
bellard19cb3732006-08-19 11:45:59 +0000623 return -EACCES;
aliguori71d07702009-03-03 17:37:16 +0000624 if (bdrv_check_request(bs, sector_num, nb_sectors))
625 return -EIO;
626
aliguori42fb2802009-01-15 20:43:39 +0000627 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
bellard83f64092006-08-01 16:21:11 +0000628}
629
aliguorieda578e2009-03-12 19:57:16 +0000630int bdrv_pread(BlockDriverState *bs, int64_t offset,
631 void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000632{
bellard83f64092006-08-01 16:21:11 +0000633 uint8_t tmp_buf[SECTOR_SIZE];
634 int len, nb_sectors, count;
635 int64_t sector_num;
636
637 count = count1;
638 /* first read to align to sector start */
639 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
640 if (len > count)
641 len = count;
642 sector_num = offset >> SECTOR_BITS;
643 if (len > 0) {
644 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
645 return -EIO;
646 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
647 count -= len;
648 if (count == 0)
649 return count1;
650 sector_num++;
651 buf += len;
652 }
653
654 /* read the sectors "in place" */
655 nb_sectors = count >> SECTOR_BITS;
656 if (nb_sectors > 0) {
657 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
658 return -EIO;
659 sector_num += nb_sectors;
660 len = nb_sectors << SECTOR_BITS;
661 buf += len;
662 count -= len;
663 }
664
665 /* add data from the last sector */
666 if (count > 0) {
667 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
668 return -EIO;
669 memcpy(buf, tmp_buf, count);
670 }
671 return count1;
672}
673
aliguorieda578e2009-03-12 19:57:16 +0000674int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
675 const void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000676{
bellard83f64092006-08-01 16:21:11 +0000677 uint8_t tmp_buf[SECTOR_SIZE];
678 int len, nb_sectors, count;
679 int64_t sector_num;
680
681 count = count1;
682 /* first write to align to sector start */
683 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
684 if (len > count)
685 len = count;
686 sector_num = offset >> SECTOR_BITS;
687 if (len > 0) {
688 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
689 return -EIO;
690 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
691 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
692 return -EIO;
693 count -= len;
694 if (count == 0)
695 return count1;
696 sector_num++;
697 buf += len;
698 }
699
700 /* write the sectors "in place" */
701 nb_sectors = count >> SECTOR_BITS;
702 if (nb_sectors > 0) {
703 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
704 return -EIO;
705 sector_num += nb_sectors;
706 len = nb_sectors << SECTOR_BITS;
707 buf += len;
708 count -= len;
709 }
710
711 /* add data from the last sector */
712 if (count > 0) {
713 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
714 return -EIO;
715 memcpy(tmp_buf, buf, count);
716 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
717 return -EIO;
718 }
719 return count1;
720}
bellard83f64092006-08-01 16:21:11 +0000721
722/**
bellard83f64092006-08-01 16:21:11 +0000723 * Truncate file to 'offset' bytes (needed only for file protocols)
724 */
725int bdrv_truncate(BlockDriverState *bs, int64_t offset)
726{
727 BlockDriver *drv = bs->drv;
728 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000729 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000730 if (!drv->bdrv_truncate)
731 return -ENOTSUP;
732 return drv->bdrv_truncate(bs, offset);
733}
734
735/**
736 * Length of a file in bytes. Return < 0 if error or unknown.
737 */
738int64_t bdrv_getlength(BlockDriverState *bs)
739{
740 BlockDriver *drv = bs->drv;
741 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000742 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000743 if (!drv->bdrv_getlength) {
744 /* legacy mode */
745 return bs->total_sectors * SECTOR_SIZE;
746 }
747 return drv->bdrv_getlength(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000748}
749
bellard19cb3732006-08-19 11:45:59 +0000750/* return 0 as number of sectors if no device present or error */
ths96b8f132007-12-17 01:35:20 +0000751void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
bellardfc01f7e2003-06-30 10:03:06 +0000752{
bellard19cb3732006-08-19 11:45:59 +0000753 int64_t length;
754 length = bdrv_getlength(bs);
755 if (length < 0)
756 length = 0;
757 else
758 length = length >> SECTOR_BITS;
759 *nb_sectors_ptr = length;
bellardfc01f7e2003-06-30 10:03:06 +0000760}
bellardcf989512004-02-16 21:56:36 +0000761
aliguorif3d54fc2008-11-25 21:50:24 +0000762struct partition {
763 uint8_t boot_ind; /* 0x80 - active */
764 uint8_t head; /* starting head */
765 uint8_t sector; /* starting sector */
766 uint8_t cyl; /* starting cylinder */
767 uint8_t sys_ind; /* What partition type */
768 uint8_t end_head; /* end head */
769 uint8_t end_sector; /* end sector */
770 uint8_t end_cyl; /* end cylinder */
771 uint32_t start_sect; /* starting sector counting from 0 */
772 uint32_t nr_sects; /* nr of sectors in partition */
773} __attribute__((packed));
774
775/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
776static int guess_disk_lchs(BlockDriverState *bs,
777 int *pcylinders, int *pheads, int *psectors)
778{
779 uint8_t buf[512];
780 int ret, i, heads, sectors, cylinders;
781 struct partition *p;
782 uint32_t nr_sects;
blueswir1a38131b2008-12-05 17:56:40 +0000783 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +0000784
785 bdrv_get_geometry(bs, &nb_sectors);
786
787 ret = bdrv_read(bs, 0, buf, 1);
788 if (ret < 0)
789 return -1;
790 /* test msdos magic */
791 if (buf[510] != 0x55 || buf[511] != 0xaa)
792 return -1;
793 for(i = 0; i < 4; i++) {
794 p = ((struct partition *)(buf + 0x1be)) + i;
795 nr_sects = le32_to_cpu(p->nr_sects);
796 if (nr_sects && p->end_head) {
797 /* We make the assumption that the partition terminates on
798 a cylinder boundary */
799 heads = p->end_head + 1;
800 sectors = p->end_sector & 63;
801 if (sectors == 0)
802 continue;
803 cylinders = nb_sectors / (heads * sectors);
804 if (cylinders < 1 || cylinders > 16383)
805 continue;
806 *pheads = heads;
807 *psectors = sectors;
808 *pcylinders = cylinders;
809#if 0
810 printf("guessed geometry: LCHS=%d %d %d\n",
811 cylinders, heads, sectors);
812#endif
813 return 0;
814 }
815 }
816 return -1;
817}
818
819void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
820{
821 int translation, lba_detected = 0;
822 int cylinders, heads, secs;
blueswir1a38131b2008-12-05 17:56:40 +0000823 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +0000824
825 /* if a geometry hint is available, use it */
826 bdrv_get_geometry(bs, &nb_sectors);
827 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
828 translation = bdrv_get_translation_hint(bs);
829 if (cylinders != 0) {
830 *pcyls = cylinders;
831 *pheads = heads;
832 *psecs = secs;
833 } else {
834 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
835 if (heads > 16) {
836 /* if heads > 16, it means that a BIOS LBA
837 translation was active, so the default
838 hardware geometry is OK */
839 lba_detected = 1;
840 goto default_geometry;
841 } else {
842 *pcyls = cylinders;
843 *pheads = heads;
844 *psecs = secs;
845 /* disable any translation to be in sync with
846 the logical geometry */
847 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
848 bdrv_set_translation_hint(bs,
849 BIOS_ATA_TRANSLATION_NONE);
850 }
851 }
852 } else {
853 default_geometry:
854 /* if no geometry, use a standard physical disk geometry */
855 cylinders = nb_sectors / (16 * 63);
856
857 if (cylinders > 16383)
858 cylinders = 16383;
859 else if (cylinders < 2)
860 cylinders = 2;
861 *pcyls = cylinders;
862 *pheads = 16;
863 *psecs = 63;
864 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
865 if ((*pcyls * *pheads) <= 131072) {
866 bdrv_set_translation_hint(bs,
867 BIOS_ATA_TRANSLATION_LARGE);
868 } else {
869 bdrv_set_translation_hint(bs,
870 BIOS_ATA_TRANSLATION_LBA);
871 }
872 }
873 }
874 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
875 }
876}
877
ths5fafdf22007-09-16 21:08:06 +0000878void bdrv_set_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000879 int cyls, int heads, int secs)
880{
881 bs->cyls = cyls;
882 bs->heads = heads;
883 bs->secs = secs;
884}
885
886void bdrv_set_type_hint(BlockDriverState *bs, int type)
887{
888 bs->type = type;
889 bs->removable = ((type == BDRV_TYPE_CDROM ||
890 type == BDRV_TYPE_FLOPPY));
891}
892
bellard46d47672004-11-16 01:45:27 +0000893void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
894{
895 bs->translation = translation;
896}
897
ths5fafdf22007-09-16 21:08:06 +0000898void bdrv_get_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000899 int *pcyls, int *pheads, int *psecs)
900{
901 *pcyls = bs->cyls;
902 *pheads = bs->heads;
903 *psecs = bs->secs;
904}
905
906int bdrv_get_type_hint(BlockDriverState *bs)
907{
908 return bs->type;
909}
910
bellard46d47672004-11-16 01:45:27 +0000911int bdrv_get_translation_hint(BlockDriverState *bs)
912{
913 return bs->translation;
914}
915
bellardb3380822004-03-14 21:38:54 +0000916int bdrv_is_removable(BlockDriverState *bs)
917{
918 return bs->removable;
919}
920
921int bdrv_is_read_only(BlockDriverState *bs)
922{
923 return bs->read_only;
924}
925
ths985a03b2007-12-24 16:10:43 +0000926int bdrv_is_sg(BlockDriverState *bs)
927{
928 return bs->sg;
929}
930
Christoph Hellwige900a7b2009-09-04 19:01:15 +0200931int bdrv_enable_write_cache(BlockDriverState *bs)
932{
933 return bs->enable_write_cache;
934}
935
bellard19cb3732006-08-19 11:45:59 +0000936/* XXX: no longer used */
ths5fafdf22007-09-16 21:08:06 +0000937void bdrv_set_change_cb(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000938 void (*change_cb)(void *opaque), void *opaque)
939{
940 bs->change_cb = change_cb;
941 bs->change_opaque = opaque;
942}
943
bellardea2384d2004-08-01 21:59:26 +0000944int bdrv_is_encrypted(BlockDriverState *bs)
945{
946 if (bs->backing_hd && bs->backing_hd->encrypted)
947 return 1;
948 return bs->encrypted;
949}
950
aliguoric0f4ce72009-03-05 23:01:01 +0000951int bdrv_key_required(BlockDriverState *bs)
952{
953 BlockDriverState *backing_hd = bs->backing_hd;
954
955 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
956 return 1;
957 return (bs->encrypted && !bs->valid_key);
958}
959
bellardea2384d2004-08-01 21:59:26 +0000960int bdrv_set_key(BlockDriverState *bs, const char *key)
961{
962 int ret;
963 if (bs->backing_hd && bs->backing_hd->encrypted) {
964 ret = bdrv_set_key(bs->backing_hd, key);
965 if (ret < 0)
966 return ret;
967 if (!bs->encrypted)
968 return 0;
969 }
970 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
971 return -1;
aliguoric0f4ce72009-03-05 23:01:01 +0000972 ret = bs->drv->bdrv_set_key(bs, key);
aliguoribb5fc202009-03-05 23:01:15 +0000973 if (ret < 0) {
974 bs->valid_key = 0;
975 } else if (!bs->valid_key) {
976 bs->valid_key = 1;
977 /* call the change callback now, we skipped it on open */
978 bs->media_changed = 1;
979 if (bs->change_cb)
980 bs->change_cb(bs->change_opaque);
981 }
aliguoric0f4ce72009-03-05 23:01:01 +0000982 return ret;
bellardea2384d2004-08-01 21:59:26 +0000983}
984
985void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
986{
bellard19cb3732006-08-19 11:45:59 +0000987 if (!bs->drv) {
bellardea2384d2004-08-01 21:59:26 +0000988 buf[0] = '\0';
989 } else {
990 pstrcpy(buf, buf_size, bs->drv->format_name);
991 }
992}
993
ths5fafdf22007-09-16 21:08:06 +0000994void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
bellardea2384d2004-08-01 21:59:26 +0000995 void *opaque)
996{
997 BlockDriver *drv;
998
999 for (drv = first_drv; drv != NULL; drv = drv->next) {
1000 it(opaque, drv->format_name);
1001 }
1002}
1003
bellardb3380822004-03-14 21:38:54 +00001004BlockDriverState *bdrv_find(const char *name)
1005{
1006 BlockDriverState *bs;
1007
1008 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1009 if (!strcmp(name, bs->device_name))
1010 return bs;
1011 }
1012 return NULL;
1013}
1014
aliguori51de9762009-03-05 23:00:43 +00001015void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
bellard81d09122004-07-14 17:21:37 +00001016{
1017 BlockDriverState *bs;
1018
1019 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori51de9762009-03-05 23:00:43 +00001020 it(opaque, bs);
bellard81d09122004-07-14 17:21:37 +00001021 }
1022}
1023
bellardea2384d2004-08-01 21:59:26 +00001024const char *bdrv_get_device_name(BlockDriverState *bs)
1025{
1026 return bs->device_name;
1027}
1028
pbrook7a6cba62006-06-04 11:39:07 +00001029void bdrv_flush(BlockDriverState *bs)
1030{
aliguori081501d2009-03-29 01:31:51 +00001031 if (!bs->drv)
1032 return;
pbrook7a6cba62006-06-04 11:39:07 +00001033 if (bs->drv->bdrv_flush)
1034 bs->drv->bdrv_flush(bs);
1035 if (bs->backing_hd)
1036 bdrv_flush(bs->backing_hd);
1037}
1038
aliguoric6ca28d2008-10-06 13:55:43 +00001039void bdrv_flush_all(void)
1040{
1041 BlockDriverState *bs;
1042
1043 for (bs = bdrv_first; bs != NULL; bs = bs->next)
1044 if (bs->drv && !bdrv_is_read_only(bs) &&
1045 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1046 bdrv_flush(bs);
1047}
1048
thsf58c7b32008-06-05 21:53:49 +00001049/*
1050 * Returns true iff the specified sector is present in the disk image. Drivers
1051 * not implementing the functionality are assumed to not support backing files,
1052 * hence all their sectors are reported as allocated.
1053 *
1054 * 'pnum' is set to the number of sectors (including and immediately following
1055 * the specified sector) that are known to be in the same
1056 * allocated/unallocated state.
1057 *
1058 * 'nb_sectors' is the max value 'pnum' should be set to.
1059 */
1060int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1061 int *pnum)
1062{
1063 int64_t n;
1064 if (!bs->drv->bdrv_is_allocated) {
1065 if (sector_num >= bs->total_sectors) {
1066 *pnum = 0;
1067 return 0;
1068 }
1069 n = bs->total_sectors - sector_num;
1070 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1071 return 1;
1072 }
1073 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1074}
1075
aliguori376253e2009-03-05 23:01:23 +00001076void bdrv_info(Monitor *mon)
bellardb3380822004-03-14 21:38:54 +00001077{
1078 BlockDriverState *bs;
1079
1080 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori376253e2009-03-05 23:01:23 +00001081 monitor_printf(mon, "%s:", bs->device_name);
1082 monitor_printf(mon, " type=");
bellardb3380822004-03-14 21:38:54 +00001083 switch(bs->type) {
1084 case BDRV_TYPE_HD:
aliguori376253e2009-03-05 23:01:23 +00001085 monitor_printf(mon, "hd");
bellardb3380822004-03-14 21:38:54 +00001086 break;
1087 case BDRV_TYPE_CDROM:
aliguori376253e2009-03-05 23:01:23 +00001088 monitor_printf(mon, "cdrom");
bellardb3380822004-03-14 21:38:54 +00001089 break;
1090 case BDRV_TYPE_FLOPPY:
aliguori376253e2009-03-05 23:01:23 +00001091 monitor_printf(mon, "floppy");
bellardb3380822004-03-14 21:38:54 +00001092 break;
1093 }
aliguori376253e2009-03-05 23:01:23 +00001094 monitor_printf(mon, " removable=%d", bs->removable);
bellardb3380822004-03-14 21:38:54 +00001095 if (bs->removable) {
aliguori376253e2009-03-05 23:01:23 +00001096 monitor_printf(mon, " locked=%d", bs->locked);
bellardb3380822004-03-14 21:38:54 +00001097 }
bellard19cb3732006-08-19 11:45:59 +00001098 if (bs->drv) {
aliguori376253e2009-03-05 23:01:23 +00001099 monitor_printf(mon, " file=");
1100 monitor_print_filename(mon, bs->filename);
thsfef30742006-12-22 14:11:32 +00001101 if (bs->backing_file[0] != '\0') {
aliguori376253e2009-03-05 23:01:23 +00001102 monitor_printf(mon, " backing_file=");
1103 monitor_print_filename(mon, bs->backing_file);
1104 }
1105 monitor_printf(mon, " ro=%d", bs->read_only);
1106 monitor_printf(mon, " drv=%s", bs->drv->format_name);
1107 monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs));
bellardb3380822004-03-14 21:38:54 +00001108 } else {
aliguori376253e2009-03-05 23:01:23 +00001109 monitor_printf(mon, " [not inserted]");
bellardb3380822004-03-14 21:38:54 +00001110 }
aliguori376253e2009-03-05 23:01:23 +00001111 monitor_printf(mon, "\n");
bellardb3380822004-03-14 21:38:54 +00001112 }
1113}
thsa36e69d2007-12-02 05:18:19 +00001114
1115/* The "info blockstats" command. */
aliguori376253e2009-03-05 23:01:23 +00001116void bdrv_info_stats(Monitor *mon)
thsa36e69d2007-12-02 05:18:19 +00001117{
1118 BlockDriverState *bs;
1119
1120 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori376253e2009-03-05 23:01:23 +00001121 monitor_printf(mon, "%s:"
1122 " rd_bytes=%" PRIu64
1123 " wr_bytes=%" PRIu64
1124 " rd_operations=%" PRIu64
1125 " wr_operations=%" PRIu64
aliguoriebf53fc2009-03-11 20:05:29 +00001126 "\n",
aliguori376253e2009-03-05 23:01:23 +00001127 bs->device_name,
1128 bs->rd_bytes, bs->wr_bytes,
1129 bs->rd_ops, bs->wr_ops);
thsa36e69d2007-12-02 05:18:19 +00001130 }
1131}
bellardea2384d2004-08-01 21:59:26 +00001132
aliguori045df332009-03-05 23:00:48 +00001133const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1134{
1135 if (bs->backing_hd && bs->backing_hd->encrypted)
1136 return bs->backing_file;
1137 else if (bs->encrypted)
1138 return bs->filename;
1139 else
1140 return NULL;
1141}
1142
ths5fafdf22007-09-16 21:08:06 +00001143void bdrv_get_backing_filename(BlockDriverState *bs,
bellard83f64092006-08-01 16:21:11 +00001144 char *filename, int filename_size)
bellardea2384d2004-08-01 21:59:26 +00001145{
bellard83f64092006-08-01 16:21:11 +00001146 if (!bs->backing_hd) {
1147 pstrcpy(filename, filename_size, "");
1148 } else {
1149 pstrcpy(filename, filename_size, bs->backing_file);
1150 }
bellardea2384d2004-08-01 21:59:26 +00001151}
1152
ths5fafdf22007-09-16 21:08:06 +00001153int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
bellardfaea38e2006-08-05 21:31:00 +00001154 const uint8_t *buf, int nb_sectors)
1155{
1156 BlockDriver *drv = bs->drv;
1157 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001158 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001159 if (!drv->bdrv_write_compressed)
1160 return -ENOTSUP;
Kevin Wolffbb7b4e2009-05-08 14:47:24 +02001161 if (bdrv_check_request(bs, sector_num, nb_sectors))
1162 return -EIO;
bellardfaea38e2006-08-05 21:31:00 +00001163 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1164}
ths3b46e622007-09-17 08:09:54 +00001165
bellardfaea38e2006-08-05 21:31:00 +00001166int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1167{
1168 BlockDriver *drv = bs->drv;
1169 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001170 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001171 if (!drv->bdrv_get_info)
1172 return -ENOTSUP;
1173 memset(bdi, 0, sizeof(*bdi));
1174 return drv->bdrv_get_info(bs, bdi);
1175}
1176
Christoph Hellwig45566e92009-07-10 23:11:57 +02001177int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
1178 int64_t pos, int size)
aliguori178e08a2009-04-05 19:10:55 +00001179{
1180 BlockDriver *drv = bs->drv;
1181 if (!drv)
1182 return -ENOMEDIUM;
Christoph Hellwig45566e92009-07-10 23:11:57 +02001183 if (!drv->bdrv_save_vmstate)
aliguori178e08a2009-04-05 19:10:55 +00001184 return -ENOTSUP;
Christoph Hellwig45566e92009-07-10 23:11:57 +02001185 return drv->bdrv_save_vmstate(bs, buf, pos, size);
aliguori178e08a2009-04-05 19:10:55 +00001186}
1187
Christoph Hellwig45566e92009-07-10 23:11:57 +02001188int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1189 int64_t pos, int size)
aliguori178e08a2009-04-05 19:10:55 +00001190{
1191 BlockDriver *drv = bs->drv;
1192 if (!drv)
1193 return -ENOMEDIUM;
Christoph Hellwig45566e92009-07-10 23:11:57 +02001194 if (!drv->bdrv_load_vmstate)
aliguori178e08a2009-04-05 19:10:55 +00001195 return -ENOTSUP;
Christoph Hellwig45566e92009-07-10 23:11:57 +02001196 return drv->bdrv_load_vmstate(bs, buf, pos, size);
aliguori178e08a2009-04-05 19:10:55 +00001197}
1198
bellardfaea38e2006-08-05 21:31:00 +00001199/**************************************************************/
1200/* handling of snapshots */
1201
ths5fafdf22007-09-16 21:08:06 +00001202int bdrv_snapshot_create(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001203 QEMUSnapshotInfo *sn_info)
1204{
1205 BlockDriver *drv = bs->drv;
1206 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001207 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001208 if (!drv->bdrv_snapshot_create)
1209 return -ENOTSUP;
1210 return drv->bdrv_snapshot_create(bs, sn_info);
1211}
1212
ths5fafdf22007-09-16 21:08:06 +00001213int bdrv_snapshot_goto(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001214 const char *snapshot_id)
1215{
1216 BlockDriver *drv = bs->drv;
1217 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001218 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001219 if (!drv->bdrv_snapshot_goto)
1220 return -ENOTSUP;
1221 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1222}
1223
1224int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1225{
1226 BlockDriver *drv = bs->drv;
1227 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001228 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001229 if (!drv->bdrv_snapshot_delete)
1230 return -ENOTSUP;
1231 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1232}
1233
ths5fafdf22007-09-16 21:08:06 +00001234int bdrv_snapshot_list(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001235 QEMUSnapshotInfo **psn_info)
1236{
1237 BlockDriver *drv = bs->drv;
1238 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001239 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001240 if (!drv->bdrv_snapshot_list)
1241 return -ENOTSUP;
1242 return drv->bdrv_snapshot_list(bs, psn_info);
1243}
1244
1245#define NB_SUFFIXES 4
1246
1247char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1248{
1249 static const char suffixes[NB_SUFFIXES] = "KMGT";
1250 int64_t base;
1251 int i;
1252
1253 if (size <= 999) {
1254 snprintf(buf, buf_size, "%" PRId64, size);
1255 } else {
1256 base = 1024;
1257 for(i = 0; i < NB_SUFFIXES; i++) {
1258 if (size < (10 * base)) {
ths5fafdf22007-09-16 21:08:06 +00001259 snprintf(buf, buf_size, "%0.1f%c",
bellardfaea38e2006-08-05 21:31:00 +00001260 (double)size / base,
1261 suffixes[i]);
1262 break;
1263 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
ths5fafdf22007-09-16 21:08:06 +00001264 snprintf(buf, buf_size, "%" PRId64 "%c",
bellardfaea38e2006-08-05 21:31:00 +00001265 ((size + (base >> 1)) / base),
1266 suffixes[i]);
1267 break;
1268 }
1269 base = base * 1024;
1270 }
1271 }
1272 return buf;
1273}
1274
1275char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1276{
1277 char buf1[128], date_buf[128], clock_buf[128];
bellard3b9f94e2007-01-07 17:27:07 +00001278#ifdef _WIN32
1279 struct tm *ptm;
1280#else
bellardfaea38e2006-08-05 21:31:00 +00001281 struct tm tm;
bellard3b9f94e2007-01-07 17:27:07 +00001282#endif
bellardfaea38e2006-08-05 21:31:00 +00001283 time_t ti;
1284 int64_t secs;
1285
1286 if (!sn) {
ths5fafdf22007-09-16 21:08:06 +00001287 snprintf(buf, buf_size,
1288 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001289 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1290 } else {
1291 ti = sn->date_sec;
bellard3b9f94e2007-01-07 17:27:07 +00001292#ifdef _WIN32
1293 ptm = localtime(&ti);
1294 strftime(date_buf, sizeof(date_buf),
1295 "%Y-%m-%d %H:%M:%S", ptm);
1296#else
bellardfaea38e2006-08-05 21:31:00 +00001297 localtime_r(&ti, &tm);
1298 strftime(date_buf, sizeof(date_buf),
1299 "%Y-%m-%d %H:%M:%S", &tm);
bellard3b9f94e2007-01-07 17:27:07 +00001300#endif
bellardfaea38e2006-08-05 21:31:00 +00001301 secs = sn->vm_clock_nsec / 1000000000;
1302 snprintf(clock_buf, sizeof(clock_buf),
1303 "%02d:%02d:%02d.%03d",
1304 (int)(secs / 3600),
1305 (int)((secs / 60) % 60),
ths5fafdf22007-09-16 21:08:06 +00001306 (int)(secs % 60),
bellardfaea38e2006-08-05 21:31:00 +00001307 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1308 snprintf(buf, buf_size,
ths5fafdf22007-09-16 21:08:06 +00001309 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001310 sn->id_str, sn->name,
1311 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1312 date_buf,
1313 clock_buf);
1314 }
1315 return buf;
1316}
1317
bellardea2384d2004-08-01 21:59:26 +00001318
bellard83f64092006-08-01 16:21:11 +00001319/**************************************************************/
1320/* async I/Os */
1321
aliguori3b69e4b2009-01-22 16:59:24 +00001322BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
aliguorif141eaf2009-04-07 18:43:24 +00001323 QEMUIOVector *qiov, int nb_sectors,
aliguori3b69e4b2009-01-22 16:59:24 +00001324 BlockDriverCompletionFunc *cb, void *opaque)
1325{
bellard83f64092006-08-01 16:21:11 +00001326 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001327 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001328
bellard19cb3732006-08-19 11:45:59 +00001329 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001330 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001331 if (bdrv_check_request(bs, sector_num, nb_sectors))
1332 return NULL;
ths3b46e622007-09-17 08:09:54 +00001333
aliguorif141eaf2009-04-07 18:43:24 +00001334 ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
1335 cb, opaque);
thsa36e69d2007-12-02 05:18:19 +00001336
1337 if (ret) {
1338 /* Update stats even though technically transfer has not happened. */
1339 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1340 bs->rd_ops ++;
1341 }
1342
1343 return ret;
bellard83f64092006-08-01 16:21:11 +00001344}
1345
aliguorif141eaf2009-04-07 18:43:24 +00001346BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1347 QEMUIOVector *qiov, int nb_sectors,
1348 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001349{
bellard83f64092006-08-01 16:21:11 +00001350 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001351 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001352
bellard19cb3732006-08-19 11:45:59 +00001353 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001354 return NULL;
bellard83f64092006-08-01 16:21:11 +00001355 if (bs->read_only)
pbrookce1a14d2006-08-07 02:38:06 +00001356 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001357 if (bdrv_check_request(bs, sector_num, nb_sectors))
1358 return NULL;
bellard83f64092006-08-01 16:21:11 +00001359
aliguorif141eaf2009-04-07 18:43:24 +00001360 ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
1361 cb, opaque);
thsa36e69d2007-12-02 05:18:19 +00001362
1363 if (ret) {
1364 /* Update stats even though technically transfer has not happened. */
1365 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1366 bs->wr_ops ++;
1367 }
1368
1369 return ret;
bellard83f64092006-08-01 16:21:11 +00001370}
1371
Kevin Wolf40b4f532009-09-09 17:53:37 +02001372
1373typedef struct MultiwriteCB {
1374 int error;
1375 int num_requests;
1376 int num_callbacks;
1377 struct {
1378 BlockDriverCompletionFunc *cb;
1379 void *opaque;
1380 QEMUIOVector *free_qiov;
1381 void *free_buf;
1382 } callbacks[];
1383} MultiwriteCB;
1384
1385static void multiwrite_user_cb(MultiwriteCB *mcb)
1386{
1387 int i;
1388
1389 for (i = 0; i < mcb->num_callbacks; i++) {
1390 mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
1391 qemu_free(mcb->callbacks[i].free_qiov);
1392 qemu_free(mcb->callbacks[i].free_buf);
1393 }
1394}
1395
1396static void multiwrite_cb(void *opaque, int ret)
1397{
1398 MultiwriteCB *mcb = opaque;
1399
1400 if (ret < 0) {
1401 mcb->error = ret;
1402 multiwrite_user_cb(mcb);
1403 }
1404
1405 mcb->num_requests--;
1406 if (mcb->num_requests == 0) {
1407 if (mcb->error == 0) {
1408 multiwrite_user_cb(mcb);
1409 }
1410 qemu_free(mcb);
1411 }
1412}
1413
1414static int multiwrite_req_compare(const void *a, const void *b)
1415{
1416 return (((BlockRequest*) a)->sector - ((BlockRequest*) b)->sector);
1417}
1418
1419/*
1420 * Takes a bunch of requests and tries to merge them. Returns the number of
1421 * requests that remain after merging.
1422 */
1423static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
1424 int num_reqs, MultiwriteCB *mcb)
1425{
1426 int i, outidx;
1427
1428 // Sort requests by start sector
1429 qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
1430
1431 // Check if adjacent requests touch the same clusters. If so, combine them,
1432 // filling up gaps with zero sectors.
1433 outidx = 0;
1434 for (i = 1; i < num_reqs; i++) {
1435 int merge = 0;
1436 int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
1437
1438 // This handles the cases that are valid for all block drivers, namely
1439 // exactly sequential writes and overlapping writes.
1440 if (reqs[i].sector <= oldreq_last) {
1441 merge = 1;
1442 }
1443
1444 // The block driver may decide that it makes sense to combine requests
1445 // even if there is a gap of some sectors between them. In this case,
1446 // the gap is filled with zeros (therefore only applicable for yet
1447 // unused space in format like qcow2).
1448 if (!merge && bs->drv->bdrv_merge_requests) {
1449 merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]);
1450 }
1451
1452 if (merge) {
1453 size_t size;
1454 QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov));
1455 qemu_iovec_init(qiov,
1456 reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
1457
1458 // Add the first request to the merged one. If the requests are
1459 // overlapping, drop the last sectors of the first request.
1460 size = (reqs[i].sector - reqs[outidx].sector) << 9;
1461 qemu_iovec_concat(qiov, reqs[outidx].qiov, size);
1462
1463 // We might need to add some zeros between the two requests
1464 if (reqs[i].sector > oldreq_last) {
1465 size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
1466 uint8_t *buf = qemu_blockalign(bs, zero_bytes);
1467 memset(buf, 0, zero_bytes);
1468 qemu_iovec_add(qiov, buf, zero_bytes);
1469 mcb->callbacks[i].free_buf = buf;
1470 }
1471
1472 // Add the second request
1473 qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size);
1474
1475 reqs[outidx].nb_sectors += reqs[i].nb_sectors;
1476 reqs[outidx].qiov = qiov;
1477
1478 mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
1479 } else {
1480 outidx++;
1481 reqs[outidx].sector = reqs[i].sector;
1482 reqs[outidx].nb_sectors = reqs[i].nb_sectors;
1483 reqs[outidx].qiov = reqs[i].qiov;
1484 }
1485 }
1486
1487 return outidx + 1;
1488}
1489
1490/*
1491 * Submit multiple AIO write requests at once.
1492 *
1493 * On success, the function returns 0 and all requests in the reqs array have
1494 * been submitted. In error case this function returns -1, and any of the
1495 * requests may or may not be submitted yet. In particular, this means that the
1496 * callback will be called for some of the requests, for others it won't. The
1497 * caller must check the error field of the BlockRequest to wait for the right
1498 * callbacks (if error != 0, no callback will be called).
1499 *
1500 * The implementation may modify the contents of the reqs array, e.g. to merge
1501 * requests. However, the fields opaque and error are left unmodified as they
1502 * are used to signal failure for a single request to the caller.
1503 */
1504int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
1505{
1506 BlockDriverAIOCB *acb;
1507 MultiwriteCB *mcb;
1508 int i;
1509
1510 if (num_reqs == 0) {
1511 return 0;
1512 }
1513
1514 // Create MultiwriteCB structure
1515 mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
1516 mcb->num_requests = 0;
1517 mcb->num_callbacks = num_reqs;
1518
1519 for (i = 0; i < num_reqs; i++) {
1520 mcb->callbacks[i].cb = reqs[i].cb;
1521 mcb->callbacks[i].opaque = reqs[i].opaque;
1522 }
1523
1524 // Check for mergable requests
1525 num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
1526
1527 // Run the aio requests
1528 for (i = 0; i < num_reqs; i++) {
1529 acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
1530 reqs[i].nb_sectors, multiwrite_cb, mcb);
1531
1532 if (acb == NULL) {
1533 // We can only fail the whole thing if no request has been
1534 // submitted yet. Otherwise we'll wait for the submitted AIOs to
1535 // complete and report the error in the callback.
1536 if (mcb->num_requests == 0) {
1537 reqs[i].error = EIO;
1538 goto fail;
1539 } else {
1540 mcb->error = EIO;
1541 break;
1542 }
1543 } else {
1544 mcb->num_requests++;
1545 }
1546 }
1547
1548 return 0;
1549
1550fail:
1551 free(mcb);
1552 return -1;
1553}
1554
bellard83f64092006-08-01 16:21:11 +00001555void bdrv_aio_cancel(BlockDriverAIOCB *acb)
pbrookce1a14d2006-08-07 02:38:06 +00001556{
aliguori6bbff9a2009-03-20 18:25:59 +00001557 acb->pool->cancel(acb);
bellard83f64092006-08-01 16:21:11 +00001558}
1559
pbrookce1a14d2006-08-07 02:38:06 +00001560
bellard83f64092006-08-01 16:21:11 +00001561/**************************************************************/
1562/* async block device emulation */
1563
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02001564typedef struct BlockDriverAIOCBSync {
1565 BlockDriverAIOCB common;
1566 QEMUBH *bh;
1567 int ret;
1568 /* vector translation state */
1569 QEMUIOVector *qiov;
1570 uint8_t *bounce;
1571 int is_write;
1572} BlockDriverAIOCBSync;
1573
1574static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1575{
1576 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
Dor Laor6a7ad292009-06-01 12:07:23 +03001577 qemu_bh_delete(acb->bh);
Avi Kivity36afc452009-06-23 16:20:36 +03001578 acb->bh = NULL;
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02001579 qemu_aio_release(acb);
1580}
1581
1582static AIOPool bdrv_em_aio_pool = {
1583 .aiocb_size = sizeof(BlockDriverAIOCBSync),
1584 .cancel = bdrv_aio_cancel_em,
1585};
1586
bellard83f64092006-08-01 16:21:11 +00001587static void bdrv_aio_bh_cb(void *opaque)
bellardbeac80c2006-06-26 20:08:57 +00001588{
pbrookce1a14d2006-08-07 02:38:06 +00001589 BlockDriverAIOCBSync *acb = opaque;
aliguorif141eaf2009-04-07 18:43:24 +00001590
aliguorif141eaf2009-04-07 18:43:24 +00001591 if (!acb->is_write)
1592 qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
aliguoriceb42de2009-04-07 18:43:28 +00001593 qemu_vfree(acb->bounce);
pbrookce1a14d2006-08-07 02:38:06 +00001594 acb->common.cb(acb->common.opaque, acb->ret);
Dor Laor6a7ad292009-06-01 12:07:23 +03001595 qemu_bh_delete(acb->bh);
Avi Kivity36afc452009-06-23 16:20:36 +03001596 acb->bh = NULL;
pbrookce1a14d2006-08-07 02:38:06 +00001597 qemu_aio_release(acb);
bellardbeac80c2006-06-26 20:08:57 +00001598}
bellardbeac80c2006-06-26 20:08:57 +00001599
aliguorif141eaf2009-04-07 18:43:24 +00001600static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
1601 int64_t sector_num,
1602 QEMUIOVector *qiov,
1603 int nb_sectors,
1604 BlockDriverCompletionFunc *cb,
1605 void *opaque,
1606 int is_write)
1607
bellardea2384d2004-08-01 21:59:26 +00001608{
pbrookce1a14d2006-08-07 02:38:06 +00001609 BlockDriverAIOCBSync *acb;
pbrookce1a14d2006-08-07 02:38:06 +00001610
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02001611 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
aliguorif141eaf2009-04-07 18:43:24 +00001612 acb->is_write = is_write;
1613 acb->qiov = qiov;
aliguorie268ca52009-04-22 20:20:00 +00001614 acb->bounce = qemu_blockalign(bs, qiov->size);
aliguorif141eaf2009-04-07 18:43:24 +00001615
pbrookce1a14d2006-08-07 02:38:06 +00001616 if (!acb->bh)
1617 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
aliguorif141eaf2009-04-07 18:43:24 +00001618
1619 if (is_write) {
1620 qemu_iovec_to_buffer(acb->qiov, acb->bounce);
1621 acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
1622 } else {
1623 acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
1624 }
1625
pbrookce1a14d2006-08-07 02:38:06 +00001626 qemu_bh_schedule(acb->bh);
aliguorif141eaf2009-04-07 18:43:24 +00001627
pbrookce1a14d2006-08-07 02:38:06 +00001628 return &acb->common;
pbrook7a6cba62006-06-04 11:39:07 +00001629}
1630
aliguorif141eaf2009-04-07 18:43:24 +00001631static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
1632 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
pbrookce1a14d2006-08-07 02:38:06 +00001633 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001634{
aliguorif141eaf2009-04-07 18:43:24 +00001635 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
bellard83f64092006-08-01 16:21:11 +00001636}
1637
aliguorif141eaf2009-04-07 18:43:24 +00001638static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
1639 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
1640 BlockDriverCompletionFunc *cb, void *opaque)
1641{
1642 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
1643}
1644
bellard83f64092006-08-01 16:21:11 +00001645/**************************************************************/
1646/* sync block device emulation */
1647
1648static void bdrv_rw_em_cb(void *opaque, int ret)
1649{
1650 *(int *)opaque = ret;
1651}
1652
1653#define NOT_DONE 0x7fffffff
1654
ths5fafdf22007-09-16 21:08:06 +00001655static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +00001656 uint8_t *buf, int nb_sectors)
1657{
pbrookce1a14d2006-08-07 02:38:06 +00001658 int async_ret;
1659 BlockDriverAIOCB *acb;
aliguorif141eaf2009-04-07 18:43:24 +00001660 struct iovec iov;
1661 QEMUIOVector qiov;
bellard83f64092006-08-01 16:21:11 +00001662
bellard83f64092006-08-01 16:21:11 +00001663 async_ret = NOT_DONE;
blueswir13f4cb3d2009-04-13 16:31:01 +00001664 iov.iov_base = (void *)buf;
aliguorif141eaf2009-04-07 18:43:24 +00001665 iov.iov_len = nb_sectors * 512;
1666 qemu_iovec_init_external(&qiov, &iov, 1);
1667 acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
1668 bdrv_rw_em_cb, &async_ret);
aliguoribaf35cb2008-09-10 15:45:19 +00001669 if (acb == NULL)
pbrookce1a14d2006-08-07 02:38:06 +00001670 return -1;
aliguoribaf35cb2008-09-10 15:45:19 +00001671
bellard83f64092006-08-01 16:21:11 +00001672 while (async_ret == NOT_DONE) {
1673 qemu_aio_wait();
1674 }
aliguoribaf35cb2008-09-10 15:45:19 +00001675
bellard83f64092006-08-01 16:21:11 +00001676 return async_ret;
1677}
1678
1679static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1680 const uint8_t *buf, int nb_sectors)
1681{
pbrookce1a14d2006-08-07 02:38:06 +00001682 int async_ret;
1683 BlockDriverAIOCB *acb;
aliguorif141eaf2009-04-07 18:43:24 +00001684 struct iovec iov;
1685 QEMUIOVector qiov;
bellard83f64092006-08-01 16:21:11 +00001686
bellard83f64092006-08-01 16:21:11 +00001687 async_ret = NOT_DONE;
aliguorif141eaf2009-04-07 18:43:24 +00001688 iov.iov_base = (void *)buf;
1689 iov.iov_len = nb_sectors * 512;
1690 qemu_iovec_init_external(&qiov, &iov, 1);
1691 acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
1692 bdrv_rw_em_cb, &async_ret);
aliguoribaf35cb2008-09-10 15:45:19 +00001693 if (acb == NULL)
pbrookce1a14d2006-08-07 02:38:06 +00001694 return -1;
bellard83f64092006-08-01 16:21:11 +00001695 while (async_ret == NOT_DONE) {
1696 qemu_aio_wait();
1697 }
bellard83f64092006-08-01 16:21:11 +00001698 return async_ret;
1699}
bellardea2384d2004-08-01 21:59:26 +00001700
1701void bdrv_init(void)
1702{
Anthony Liguori5efa9d52009-05-09 17:03:42 -05001703 module_call_init(MODULE_INIT_BLOCK);
bellardea2384d2004-08-01 21:59:26 +00001704}
pbrookce1a14d2006-08-07 02:38:06 +00001705
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02001706void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
1707 BlockDriverCompletionFunc *cb, void *opaque)
aliguori6bbff9a2009-03-20 18:25:59 +00001708{
pbrookce1a14d2006-08-07 02:38:06 +00001709 BlockDriverAIOCB *acb;
1710
aliguori6bbff9a2009-03-20 18:25:59 +00001711 if (pool->free_aiocb) {
1712 acb = pool->free_aiocb;
1713 pool->free_aiocb = acb->next;
pbrookce1a14d2006-08-07 02:38:06 +00001714 } else {
aliguori6bbff9a2009-03-20 18:25:59 +00001715 acb = qemu_mallocz(pool->aiocb_size);
1716 acb->pool = pool;
pbrookce1a14d2006-08-07 02:38:06 +00001717 }
1718 acb->bs = bs;
1719 acb->cb = cb;
1720 acb->opaque = opaque;
1721 return acb;
1722}
1723
1724void qemu_aio_release(void *p)
1725{
aliguori6bbff9a2009-03-20 18:25:59 +00001726 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
1727 AIOPool *pool = acb->pool;
1728 acb->next = pool->free_aiocb;
1729 pool->free_aiocb = acb;
pbrookce1a14d2006-08-07 02:38:06 +00001730}
bellard19cb3732006-08-19 11:45:59 +00001731
1732/**************************************************************/
1733/* removable device support */
1734
1735/**
1736 * Return TRUE if the media is present
1737 */
1738int bdrv_is_inserted(BlockDriverState *bs)
1739{
1740 BlockDriver *drv = bs->drv;
1741 int ret;
1742 if (!drv)
1743 return 0;
1744 if (!drv->bdrv_is_inserted)
1745 return 1;
1746 ret = drv->bdrv_is_inserted(bs);
1747 return ret;
1748}
1749
1750/**
1751 * Return TRUE if the media changed since the last call to this
ths5fafdf22007-09-16 21:08:06 +00001752 * function. It is currently only used for floppy disks
bellard19cb3732006-08-19 11:45:59 +00001753 */
1754int bdrv_media_changed(BlockDriverState *bs)
1755{
1756 BlockDriver *drv = bs->drv;
1757 int ret;
1758
1759 if (!drv || !drv->bdrv_media_changed)
1760 ret = -ENOTSUP;
1761 else
1762 ret = drv->bdrv_media_changed(bs);
1763 if (ret == -ENOTSUP)
1764 ret = bs->media_changed;
1765 bs->media_changed = 0;
1766 return ret;
1767}
1768
1769/**
1770 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1771 */
Mark McLoughlinaea2a332009-05-27 10:06:11 +01001772int bdrv_eject(BlockDriverState *bs, int eject_flag)
bellard19cb3732006-08-19 11:45:59 +00001773{
1774 BlockDriver *drv = bs->drv;
1775 int ret;
1776
Mark McLoughlinaea2a332009-05-27 10:06:11 +01001777 if (bs->locked) {
1778 return -EBUSY;
1779 }
1780
bellard19cb3732006-08-19 11:45:59 +00001781 if (!drv || !drv->bdrv_eject) {
1782 ret = -ENOTSUP;
1783 } else {
1784 ret = drv->bdrv_eject(bs, eject_flag);
1785 }
1786 if (ret == -ENOTSUP) {
1787 if (eject_flag)
1788 bdrv_close(bs);
Mark McLoughlinaea2a332009-05-27 10:06:11 +01001789 ret = 0;
bellard19cb3732006-08-19 11:45:59 +00001790 }
Mark McLoughlinaea2a332009-05-27 10:06:11 +01001791
1792 return ret;
bellard19cb3732006-08-19 11:45:59 +00001793}
1794
1795int bdrv_is_locked(BlockDriverState *bs)
1796{
1797 return bs->locked;
1798}
1799
1800/**
1801 * Lock or unlock the media (if it is locked, the user won't be able
1802 * to eject it manually).
1803 */
1804void bdrv_set_locked(BlockDriverState *bs, int locked)
1805{
1806 BlockDriver *drv = bs->drv;
1807
1808 bs->locked = locked;
1809 if (drv && drv->bdrv_set_locked) {
1810 drv->bdrv_set_locked(bs, locked);
1811 }
1812}
ths985a03b2007-12-24 16:10:43 +00001813
1814/* needed for generic scsi interface */
1815
1816int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1817{
1818 BlockDriver *drv = bs->drv;
1819
1820 if (drv && drv->bdrv_ioctl)
1821 return drv->bdrv_ioctl(bs, req, buf);
1822 return -ENOTSUP;
1823}
aliguori7d780662009-03-12 19:57:08 +00001824
aliguori221f7152009-03-28 17:28:41 +00001825BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
1826 unsigned long int req, void *buf,
1827 BlockDriverCompletionFunc *cb, void *opaque)
aliguori7d780662009-03-12 19:57:08 +00001828{
aliguori221f7152009-03-28 17:28:41 +00001829 BlockDriver *drv = bs->drv;
aliguori7d780662009-03-12 19:57:08 +00001830
aliguori221f7152009-03-28 17:28:41 +00001831 if (drv && drv->bdrv_aio_ioctl)
1832 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
1833 return NULL;
aliguori7d780662009-03-12 19:57:08 +00001834}
aliguorie268ca52009-04-22 20:20:00 +00001835
1836void *qemu_blockalign(BlockDriverState *bs, size_t size)
1837{
1838 return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
1839}