blob: 9c43332f4f4f33a134722d39f52858cc0d8239cc [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"
pbrookfaf07962007-11-11 02:51:17 +000025#include "qemu-common.h"
aliguori376253e2009-03-05 23:01:23 +000026#include "monitor.h"
bellardea2384d2004-08-01 21:59:26 +000027#include "block_int.h"
Anthony Liguori5efa9d52009-05-09 17:03:42 -050028#include "module.h"
Luiz Capitulinod15e5462009-12-10 17:16:06 -020029#include "qemu-objects.h"
bellardfc01f7e2003-06-30 10:03:06 +000030
Juan Quintela71e72a12009-07-27 16:12:56 +020031#ifdef CONFIG_BSD
bellard7674e7b2005-04-26 21:59:26 +000032#include <sys/types.h>
33#include <sys/stat.h>
34#include <sys/ioctl.h>
Blue Swirl72cf2d42009-09-12 07:36:22 +000035#include <sys/queue.h>
blueswir1c5e97232009-03-07 20:06:23 +000036#ifndef __DragonFly__
bellard7674e7b2005-04-26 21:59:26 +000037#include <sys/disk.h>
38#endif
blueswir1c5e97232009-03-07 20:06:23 +000039#endif
bellard7674e7b2005-04-26 21:59:26 +000040
aliguori49dc7682009-03-08 16:26:59 +000041#ifdef _WIN32
42#include <windows.h>
43#endif
44
aliguorif141eaf2009-04-07 18:43:24 +000045static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
46 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
aliguoric87c0672009-04-07 18:43:20 +000047 BlockDriverCompletionFunc *cb, void *opaque);
aliguorif141eaf2009-04-07 18:43:24 +000048static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
49 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
pbrookce1a14d2006-08-07 02:38:06 +000050 BlockDriverCompletionFunc *cb, void *opaque);
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +020051static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
52 BlockDriverCompletionFunc *cb, void *opaque);
Alexander Graf016f5cf2010-05-26 17:51:49 +020053static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,
54 BlockDriverCompletionFunc *cb, void *opaque);
ths5fafdf22007-09-16 21:08:06 +000055static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +000056 uint8_t *buf, int nb_sectors);
57static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
58 const uint8_t *buf, int nb_sectors);
bellardec530c82006-04-25 22:36:06 +000059
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +010060static QTAILQ_HEAD(, BlockDriverState) bdrv_states =
61 QTAILQ_HEAD_INITIALIZER(bdrv_states);
blueswir17ee930d2008-09-17 19:04:14 +000062
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +010063static QLIST_HEAD(, BlockDriver) bdrv_drivers =
64 QLIST_HEAD_INITIALIZER(bdrv_drivers);
bellardea2384d2004-08-01 21:59:26 +000065
Markus Armbrustereb852012009-10-27 18:41:44 +010066/* If non-zero, use only whitelisted block drivers */
67static int use_bdrv_whitelist;
68
bellard83f64092006-08-01 16:21:11 +000069int path_is_absolute(const char *path)
70{
71 const char *p;
bellard21664422007-01-07 18:22:37 +000072#ifdef _WIN32
73 /* specific case for names like: "\\.\d:" */
74 if (*path == '/' || *path == '\\')
75 return 1;
76#endif
bellard83f64092006-08-01 16:21:11 +000077 p = strchr(path, ':');
78 if (p)
79 p++;
80 else
81 p = path;
bellard3b9f94e2007-01-07 17:27:07 +000082#ifdef _WIN32
83 return (*p == '/' || *p == '\\');
84#else
85 return (*p == '/');
86#endif
bellard83f64092006-08-01 16:21:11 +000087}
88
89/* if filename is absolute, just copy it to dest. Otherwise, build a
90 path to it by considering it is relative to base_path. URL are
91 supported. */
92void path_combine(char *dest, int dest_size,
93 const char *base_path,
94 const char *filename)
95{
96 const char *p, *p1;
97 int len;
98
99 if (dest_size <= 0)
100 return;
101 if (path_is_absolute(filename)) {
102 pstrcpy(dest, dest_size, filename);
103 } else {
104 p = strchr(base_path, ':');
105 if (p)
106 p++;
107 else
108 p = base_path;
bellard3b9f94e2007-01-07 17:27:07 +0000109 p1 = strrchr(base_path, '/');
110#ifdef _WIN32
111 {
112 const char *p2;
113 p2 = strrchr(base_path, '\\');
114 if (!p1 || p2 > p1)
115 p1 = p2;
116 }
117#endif
bellard83f64092006-08-01 16:21:11 +0000118 if (p1)
119 p1++;
120 else
121 p1 = base_path;
122 if (p1 > p)
123 p = p1;
124 len = p - base_path;
125 if (len > dest_size - 1)
126 len = dest_size - 1;
127 memcpy(dest, base_path, len);
128 dest[len] = '\0';
129 pstrcat(dest, dest_size, filename);
130 }
131}
132
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500133void bdrv_register(BlockDriver *bdrv)
bellardea2384d2004-08-01 21:59:26 +0000134{
aliguorif141eaf2009-04-07 18:43:24 +0000135 if (!bdrv->bdrv_aio_readv) {
bellard83f64092006-08-01 16:21:11 +0000136 /* add AIO emulation layer */
aliguorif141eaf2009-04-07 18:43:24 +0000137 bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
138 bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
aliguorieda578e2009-03-12 19:57:16 +0000139 } else if (!bdrv->bdrv_read) {
bellard83f64092006-08-01 16:21:11 +0000140 /* add synchronous IO emulation layer */
141 bdrv->bdrv_read = bdrv_read_em;
142 bdrv->bdrv_write = bdrv_write_em;
143 }
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +0200144
145 if (!bdrv->bdrv_aio_flush)
146 bdrv->bdrv_aio_flush = bdrv_aio_flush_em;
147
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100148 QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
bellardea2384d2004-08-01 21:59:26 +0000149}
bellardb3380822004-03-14 21:38:54 +0000150
151/* create a new block device (by default it is empty) */
152BlockDriverState *bdrv_new(const char *device_name)
bellardfc01f7e2003-06-30 10:03:06 +0000153{
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +0100154 BlockDriverState *bs;
bellardb3380822004-03-14 21:38:54 +0000155
156 bs = qemu_mallocz(sizeof(BlockDriverState));
bellardb3380822004-03-14 21:38:54 +0000157 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
bellardea2384d2004-08-01 21:59:26 +0000158 if (device_name[0] != '\0') {
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +0100159 QTAILQ_INSERT_TAIL(&bdrv_states, bs, list);
bellardea2384d2004-08-01 21:59:26 +0000160 }
bellardb3380822004-03-14 21:38:54 +0000161 return bs;
162}
163
bellardea2384d2004-08-01 21:59:26 +0000164BlockDriver *bdrv_find_format(const char *format_name)
165{
166 BlockDriver *drv1;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100167 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
168 if (!strcmp(drv1->format_name, format_name)) {
bellardea2384d2004-08-01 21:59:26 +0000169 return drv1;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100170 }
bellardea2384d2004-08-01 21:59:26 +0000171 }
172 return NULL;
173}
174
Markus Armbrustereb852012009-10-27 18:41:44 +0100175static int bdrv_is_whitelisted(BlockDriver *drv)
176{
177 static const char *whitelist[] = {
178 CONFIG_BDRV_WHITELIST
179 };
180 const char **p;
181
182 if (!whitelist[0])
183 return 1; /* no whitelist, anything goes */
184
185 for (p = whitelist; *p; p++) {
186 if (!strcmp(drv->format_name, *p)) {
187 return 1;
188 }
189 }
190 return 0;
191}
192
193BlockDriver *bdrv_find_whitelisted_format(const char *format_name)
194{
195 BlockDriver *drv = bdrv_find_format(format_name);
196 return drv && bdrv_is_whitelisted(drv) ? drv : NULL;
197}
198
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200199int bdrv_create(BlockDriver *drv, const char* filename,
200 QEMUOptionParameter *options)
bellardea2384d2004-08-01 21:59:26 +0000201{
202 if (!drv->bdrv_create)
203 return -ENOTSUP;
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200204
205 return drv->bdrv_create(filename, options);
bellardea2384d2004-08-01 21:59:26 +0000206}
207
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200208int bdrv_create_file(const char* filename, QEMUOptionParameter *options)
209{
210 BlockDriver *drv;
211
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900212 drv = bdrv_find_protocol(filename);
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200213 if (drv == NULL) {
214 drv = bdrv_find_format("file");
215 }
216
217 return bdrv_create(drv, filename, options);
218}
219
bellardd5249392004-08-03 21:14:23 +0000220#ifdef _WIN32
bellard95389c82005-12-18 18:28:15 +0000221void get_tmp_filename(char *filename, int size)
bellardd5249392004-08-03 21:14:23 +0000222{
bellard3b9f94e2007-01-07 17:27:07 +0000223 char temp_dir[MAX_PATH];
ths3b46e622007-09-17 08:09:54 +0000224
bellard3b9f94e2007-01-07 17:27:07 +0000225 GetTempPath(MAX_PATH, temp_dir);
226 GetTempFileName(temp_dir, "qem", 0, filename);
bellardd5249392004-08-03 21:14:23 +0000227}
228#else
bellard95389c82005-12-18 18:28:15 +0000229void get_tmp_filename(char *filename, int size)
bellardea2384d2004-08-01 21:59:26 +0000230{
231 int fd;
blueswir17ccfb2e2008-09-14 06:45:34 +0000232 const char *tmpdir;
bellardd5249392004-08-03 21:14:23 +0000233 /* XXX: race condition possible */
aurel320badc1e2008-03-10 00:05:34 +0000234 tmpdir = getenv("TMPDIR");
235 if (!tmpdir)
236 tmpdir = "/tmp";
237 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
bellardea2384d2004-08-01 21:59:26 +0000238 fd = mkstemp(filename);
239 close(fd);
240}
bellardd5249392004-08-03 21:14:23 +0000241#endif
bellardea2384d2004-08-01 21:59:26 +0000242
bellard19cb3732006-08-19 11:45:59 +0000243#ifdef _WIN32
bellardf45512f2006-08-23 21:40:13 +0000244static int is_windows_drive_prefix(const char *filename)
245{
246 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
247 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
248 filename[1] == ':');
249}
ths3b46e622007-09-17 08:09:54 +0000250
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200251int is_windows_drive(const char *filename)
bellard19cb3732006-08-19 11:45:59 +0000252{
ths5fafdf22007-09-16 21:08:06 +0000253 if (is_windows_drive_prefix(filename) &&
bellardf45512f2006-08-23 21:40:13 +0000254 filename[2] == '\0')
bellard19cb3732006-08-19 11:45:59 +0000255 return 1;
256 if (strstart(filename, "\\\\.\\", NULL) ||
257 strstart(filename, "//./", NULL))
258 return 1;
259 return 0;
260}
261#endif
262
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200263/*
264 * Detect host devices. By convention, /dev/cdrom[N] is always
265 * recognized as a host CDROM.
266 */
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200267static BlockDriver *find_hdev_driver(const char *filename)
268{
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200269 int score_max = 0, score;
270 BlockDriver *drv = NULL, *d;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200271
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100272 QLIST_FOREACH(d, &bdrv_drivers, list) {
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200273 if (d->bdrv_probe_device) {
274 score = d->bdrv_probe_device(filename);
275 if (score > score_max) {
276 score_max = score;
277 drv = d;
278 }
279 }
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200280 }
281
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200282 return drv;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200283}
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200284
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900285BlockDriver *bdrv_find_protocol(const char *filename)
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200286{
287 BlockDriver *drv1;
288 char protocol[128];
289 int len;
290 const char *p;
Kevin Wolf20993082010-05-06 13:04:58 +0200291 int is_drive;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200292
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200293 /* TODO Drivers without bdrv_file_open must be specified explicitly */
294
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200295#ifdef _WIN32
Kevin Wolf20993082010-05-06 13:04:58 +0200296 is_drive = is_windows_drive(filename) ||
297 is_windows_drive_prefix(filename);
298#else
299 is_drive = 0;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200300#endif
301 p = strchr(filename, ':');
Kevin Wolf20993082010-05-06 13:04:58 +0200302 if (!p || is_drive) {
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200303 drv1 = find_hdev_driver(filename);
304 if (!drv1) {
305 drv1 = bdrv_find_format("file");
306 }
307 return drv1;
308 }
309 len = p - filename;
310 if (len > sizeof(protocol) - 1)
311 len = sizeof(protocol) - 1;
312 memcpy(protocol, filename, len);
313 protocol[len] = '\0';
314 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
315 if (drv1->protocol_name &&
316 !strcmp(drv1->protocol_name, protocol)) {
317 return drv1;
318 }
319 }
320 return NULL;
321}
322
bellardea2384d2004-08-01 21:59:26 +0000323static BlockDriver *find_image_format(const char *filename)
324{
bellard83f64092006-08-01 16:21:11 +0000325 int ret, score, score_max;
bellardea2384d2004-08-01 21:59:26 +0000326 BlockDriver *drv1, *drv;
bellard83f64092006-08-01 16:21:11 +0000327 uint8_t buf[2048];
328 BlockDriverState *bs;
ths3b46e622007-09-17 08:09:54 +0000329
Naphtali Spreif5edb012010-01-17 16:48:13 +0200330 ret = bdrv_file_open(&bs, filename, 0);
bellard83f64092006-08-01 16:21:11 +0000331 if (ret < 0)
332 return NULL;
Nicholas Bellingerf8ea0b02010-05-17 09:45:57 -0700333
334 /* Return the raw BlockDriver * to scsi-generic devices */
Nicholas A. Bellinger1a396852010-05-27 08:56:28 -0700335 if (bs->sg) {
336 bdrv_delete(bs);
Nicholas Bellingerf8ea0b02010-05-17 09:45:57 -0700337 return bdrv_find_format("raw");
Nicholas A. Bellinger1a396852010-05-27 08:56:28 -0700338 }
Nicholas Bellingerf8ea0b02010-05-17 09:45:57 -0700339
bellard83f64092006-08-01 16:21:11 +0000340 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
341 bdrv_delete(bs);
342 if (ret < 0) {
343 return NULL;
344 }
345
bellardea2384d2004-08-01 21:59:26 +0000346 score_max = 0;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200347 drv = NULL;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100348 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
bellard83f64092006-08-01 16:21:11 +0000349 if (drv1->bdrv_probe) {
350 score = drv1->bdrv_probe(buf, ret, filename);
351 if (score > score_max) {
352 score_max = score;
353 drv = drv1;
354 }
bellardea2384d2004-08-01 21:59:26 +0000355 }
356 }
357 return drv;
358}
359
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100360/**
361 * Set the current 'total_sectors' value
362 */
363static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
364{
365 BlockDriver *drv = bs->drv;
366
Nicholas Bellinger396759a2010-05-17 09:46:04 -0700367 /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
368 if (bs->sg)
369 return 0;
370
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100371 /* query actual device if possible, otherwise just trust the hint */
372 if (drv->bdrv_getlength) {
373 int64_t length = drv->bdrv_getlength(bs);
374 if (length < 0) {
375 return length;
376 }
377 hint = length >> BDRV_SECTOR_BITS;
378 }
379
380 bs->total_sectors = hint;
381 return 0;
382}
383
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200384/*
Kevin Wolf57915332010-04-14 15:24:50 +0200385 * Common part for opening disk images and files
386 */
387static int bdrv_open_common(BlockDriverState *bs, const char *filename,
388 int flags, BlockDriver *drv)
389{
390 int ret, open_flags;
391
392 assert(drv != NULL);
393
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200394 bs->file = NULL;
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100395 bs->total_sectors = 0;
Kevin Wolf57915332010-04-14 15:24:50 +0200396 bs->is_temporary = 0;
397 bs->encrypted = 0;
398 bs->valid_key = 0;
399 bs->open_flags = flags;
400 /* buffer_alignment defaulted to 512, drivers can change this value */
401 bs->buffer_alignment = 512;
402
403 pstrcpy(bs->filename, sizeof(bs->filename), filename);
404
405 if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {
406 return -ENOTSUP;
407 }
408
409 bs->drv = drv;
410 bs->opaque = qemu_mallocz(drv->instance_size);
411
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
421 /*
422 * Clear flags that are internal to the block layer before opening the
423 * image.
424 */
425 open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
426
427 /*
428 * Snapshots should be writeable.
429 */
430 if (bs->is_temporary) {
431 open_flags |= BDRV_O_RDWR;
432 }
433
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200434 /* Open the image, either directly or using a protocol */
435 if (drv->bdrv_file_open) {
436 ret = drv->bdrv_file_open(bs, filename, open_flags);
437 } else {
438 ret = bdrv_file_open(&bs->file, filename, open_flags);
439 if (ret >= 0) {
440 ret = drv->bdrv_open(bs, open_flags);
441 }
442 }
443
Kevin Wolf57915332010-04-14 15:24:50 +0200444 if (ret < 0) {
445 goto free_and_fail;
446 }
447
448 bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR);
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100449
450 ret = refresh_total_sectors(bs, bs->total_sectors);
451 if (ret < 0) {
452 goto free_and_fail;
Kevin Wolf57915332010-04-14 15:24:50 +0200453 }
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100454
Kevin Wolf57915332010-04-14 15:24:50 +0200455#ifndef _WIN32
456 if (bs->is_temporary) {
457 unlink(filename);
458 }
459#endif
460 return 0;
461
462free_and_fail:
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200463 if (bs->file) {
464 bdrv_delete(bs->file);
465 bs->file = NULL;
466 }
Kevin Wolf57915332010-04-14 15:24:50 +0200467 qemu_free(bs->opaque);
468 bs->opaque = NULL;
469 bs->drv = NULL;
470 return ret;
471}
472
473/*
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200474 * Opens a file using a protocol (file, host_device, nbd, ...)
475 */
bellard83f64092006-08-01 16:21:11 +0000476int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
bellardb3380822004-03-14 21:38:54 +0000477{
bellard83f64092006-08-01 16:21:11 +0000478 BlockDriverState *bs;
Christoph Hellwig6db95602010-04-05 16:53:57 +0200479 BlockDriver *drv;
bellard83f64092006-08-01 16:21:11 +0000480 int ret;
481
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900482 drv = bdrv_find_protocol(filename);
Christoph Hellwig6db95602010-04-05 16:53:57 +0200483 if (!drv) {
484 return -ENOENT;
485 }
486
bellard83f64092006-08-01 16:21:11 +0000487 bs = bdrv_new("");
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200488 ret = bdrv_open_common(bs, filename, flags, drv);
bellard83f64092006-08-01 16:21:11 +0000489 if (ret < 0) {
490 bdrv_delete(bs);
491 return ret;
bellard3b0d4f62005-10-30 18:30:10 +0000492 }
aliguori71d07702009-03-03 17:37:16 +0000493 bs->growable = 1;
bellard83f64092006-08-01 16:21:11 +0000494 *pbs = bs;
495 return 0;
bellardea2384d2004-08-01 21:59:26 +0000496}
bellardfc01f7e2003-06-30 10:03:06 +0000497
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200498/*
499 * Opens a disk image (raw, qcow2, vmdk, ...)
500 */
Kevin Wolfd6e90982010-03-31 14:40:27 +0200501int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
502 BlockDriver *drv)
bellardea2384d2004-08-01 21:59:26 +0000503{
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200504 int ret;
bellard712e7872005-04-28 21:09:32 +0000505
bellard83f64092006-08-01 16:21:11 +0000506 if (flags & BDRV_O_SNAPSHOT) {
bellardea2384d2004-08-01 21:59:26 +0000507 BlockDriverState *bs1;
508 int64_t total_size;
aliguori7c96d462008-09-12 17:54:13 +0000509 int is_protocol = 0;
Kevin Wolf91a073a2009-05-27 14:48:06 +0200510 BlockDriver *bdrv_qcow2;
511 QEMUOptionParameter *options;
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200512 char tmp_filename[PATH_MAX];
513 char backing_filename[PATH_MAX];
ths3b46e622007-09-17 08:09:54 +0000514
bellardea2384d2004-08-01 21:59:26 +0000515 /* if snapshot, we create a temporary backing file and open it
516 instead of opening 'filename' directly */
517
518 /* if there is a backing file, use it */
519 bs1 = bdrv_new("");
Kevin Wolfd6e90982010-03-31 14:40:27 +0200520 ret = bdrv_open(bs1, filename, 0, drv);
aliguori51d7c002009-03-05 23:00:29 +0000521 if (ret < 0) {
bellardea2384d2004-08-01 21:59:26 +0000522 bdrv_delete(bs1);
aliguori51d7c002009-03-05 23:00:29 +0000523 return ret;
bellardea2384d2004-08-01 21:59:26 +0000524 }
Jes Sorensen3e829902010-05-27 16:20:30 +0200525 total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK;
aliguori7c96d462008-09-12 17:54:13 +0000526
527 if (bs1->drv && bs1->drv->protocol_name)
528 is_protocol = 1;
529
bellardea2384d2004-08-01 21:59:26 +0000530 bdrv_delete(bs1);
ths3b46e622007-09-17 08:09:54 +0000531
bellardea2384d2004-08-01 21:59:26 +0000532 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
aliguori7c96d462008-09-12 17:54:13 +0000533
534 /* Real path is meaningless for protocols */
535 if (is_protocol)
536 snprintf(backing_filename, sizeof(backing_filename),
537 "%s", filename);
Kirill A. Shutemov114cdfa2009-12-25 18:19:22 +0000538 else if (!realpath(filename, backing_filename))
539 return -errno;
aliguori7c96d462008-09-12 17:54:13 +0000540
Kevin Wolf91a073a2009-05-27 14:48:06 +0200541 bdrv_qcow2 = bdrv_find_format("qcow2");
542 options = parse_option_parameters("", bdrv_qcow2->create_options, NULL);
543
Jes Sorensen3e829902010-05-27 16:20:30 +0200544 set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size);
Kevin Wolf91a073a2009-05-27 14:48:06 +0200545 set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename);
546 if (drv) {
547 set_option_parameter(options, BLOCK_OPT_BACKING_FMT,
548 drv->format_name);
549 }
550
551 ret = bdrv_create(bdrv_qcow2, tmp_filename, options);
Jan Kiszkad7487682010-04-29 18:24:50 +0200552 free_option_parameters(options);
aliguori51d7c002009-03-05 23:00:29 +0000553 if (ret < 0) {
554 return ret;
bellardea2384d2004-08-01 21:59:26 +0000555 }
Kevin Wolf91a073a2009-05-27 14:48:06 +0200556
bellardea2384d2004-08-01 21:59:26 +0000557 filename = tmp_filename;
Kevin Wolf91a073a2009-05-27 14:48:06 +0200558 drv = bdrv_qcow2;
bellardea2384d2004-08-01 21:59:26 +0000559 bs->is_temporary = 1;
560 }
bellard712e7872005-04-28 21:09:32 +0000561
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200562 /* Find the right image format driver */
Christoph Hellwig6db95602010-04-05 16:53:57 +0200563 if (!drv) {
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200564 drv = find_image_format(filename);
aliguori51d7c002009-03-05 23:00:29 +0000565 }
Christoph Hellwig69873072010-01-20 18:13:25 +0100566
aliguori51d7c002009-03-05 23:00:29 +0000567 if (!drv) {
568 ret = -ENOENT;
569 goto unlink_and_fail;
bellardea2384d2004-08-01 21:59:26 +0000570 }
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200571
572 /* Open the image */
573 ret = bdrv_open_common(bs, filename, flags, drv);
574 if (ret < 0) {
Christoph Hellwig69873072010-01-20 18:13:25 +0100575 goto unlink_and_fail;
576 }
577
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200578 /* If there is a backing file, use it */
579 if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') {
580 char backing_filename[PATH_MAX];
581 int back_flags;
582 BlockDriver *back_drv = NULL;
583
584 bs->backing_hd = bdrv_new("");
585 path_combine(backing_filename, sizeof(backing_filename),
586 filename, bs->backing_file);
587 if (bs->backing_format[0] != '\0')
588 back_drv = bdrv_find_format(bs->backing_format);
589
590 /* backing files always opened read-only */
591 back_flags =
592 flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
593
594 ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv);
595 if (ret < 0) {
596 bdrv_close(bs);
597 return ret;
598 }
599 if (bs->is_temporary) {
600 bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR);
601 } else {
602 /* base image inherits from "parent" */
603 bs->backing_hd->keep_read_only = bs->keep_read_only;
604 }
605 }
606
607 if (!bdrv_key_required(bs)) {
608 /* call the change callback */
609 bs->media_changed = 1;
610 if (bs->change_cb)
611 bs->change_cb(bs->change_opaque);
612 }
613
614 return 0;
615
616unlink_and_fail:
617 if (bs->is_temporary) {
618 unlink(filename);
619 }
620 return ret;
621}
622
bellardfc01f7e2003-06-30 10:03:06 +0000623void bdrv_close(BlockDriverState *bs)
624{
bellard19cb3732006-08-19 11:45:59 +0000625 if (bs->drv) {
Stefan Hajnoczi557df6a2010-04-17 10:49:06 +0100626 if (bs->backing_hd) {
bellardea2384d2004-08-01 21:59:26 +0000627 bdrv_delete(bs->backing_hd);
Stefan Hajnoczi557df6a2010-04-17 10:49:06 +0100628 bs->backing_hd = NULL;
629 }
bellardea2384d2004-08-01 21:59:26 +0000630 bs->drv->bdrv_close(bs);
631 qemu_free(bs->opaque);
632#ifdef _WIN32
633 if (bs->is_temporary) {
634 unlink(bs->filename);
635 }
bellard67b915a2004-03-31 23:37:16 +0000636#endif
bellardea2384d2004-08-01 21:59:26 +0000637 bs->opaque = NULL;
638 bs->drv = NULL;
bellardb3380822004-03-14 21:38:54 +0000639
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200640 if (bs->file != NULL) {
641 bdrv_close(bs->file);
642 }
643
bellardb3380822004-03-14 21:38:54 +0000644 /* call the change callback */
bellard19cb3732006-08-19 11:45:59 +0000645 bs->media_changed = 1;
bellardb3380822004-03-14 21:38:54 +0000646 if (bs->change_cb)
647 bs->change_cb(bs->change_opaque);
648 }
649}
650
651void bdrv_delete(BlockDriverState *bs)
652{
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +0100653 /* remove from list, if necessary */
654 if (bs->device_name[0] != '\0') {
655 QTAILQ_REMOVE(&bdrv_states, bs, list);
656 }
aurel3234c6f052008-04-08 19:51:21 +0000657
bellardb3380822004-03-14 21:38:54 +0000658 bdrv_close(bs);
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200659 if (bs->file != NULL) {
660 bdrv_delete(bs->file);
661 }
662
bellardb3380822004-03-14 21:38:54 +0000663 qemu_free(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000664}
665
aliguorie97fc192009-04-21 23:11:50 +0000666/*
667 * Run consistency checks on an image
668 *
669 * Returns the number of errors or -errno when an internal error occurs
670 */
671int bdrv_check(BlockDriverState *bs)
672{
673 if (bs->drv->bdrv_check == NULL) {
674 return -ENOTSUP;
675 }
676
677 return bs->drv->bdrv_check(bs);
678}
679
bellard33e39632003-07-06 17:15:21 +0000680/* commit COW file into the raw image */
681int bdrv_commit(BlockDriverState *bs)
682{
bellard19cb3732006-08-19 11:45:59 +0000683 BlockDriver *drv = bs->drv;
bellard83f64092006-08-01 16:21:11 +0000684 int64_t i, total_sectors;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200685 int n, j, ro, open_flags;
686 int ret = 0, rw_ret = 0;
Jes Sorenseneb5a3162010-05-27 16:20:31 +0200687 unsigned char sector[BDRV_SECTOR_SIZE];
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200688 char filename[1024];
689 BlockDriverState *bs_rw, *bs_ro;
bellard33e39632003-07-06 17:15:21 +0000690
bellard19cb3732006-08-19 11:45:59 +0000691 if (!drv)
692 return -ENOMEDIUM;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200693
694 if (!bs->backing_hd) {
695 return -ENOTSUP;
bellard33e39632003-07-06 17:15:21 +0000696 }
697
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200698 if (bs->backing_hd->keep_read_only) {
699 return -EACCES;
700 }
701
702 ro = bs->backing_hd->read_only;
703 strncpy(filename, bs->backing_hd->filename, sizeof(filename));
704 open_flags = bs->backing_hd->open_flags;
705
706 if (ro) {
707 /* re-open as RW */
708 bdrv_delete(bs->backing_hd);
709 bs->backing_hd = NULL;
710 bs_rw = bdrv_new("");
Kevin Wolfc3349192010-05-06 16:34:56 +0200711 rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR, drv);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200712 if (rw_ret < 0) {
713 bdrv_delete(bs_rw);
714 /* try to re-open read-only */
715 bs_ro = bdrv_new("");
Kevin Wolfc3349192010-05-06 16:34:56 +0200716 ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, drv);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200717 if (ret < 0) {
718 bdrv_delete(bs_ro);
719 /* drive not functional anymore */
720 bs->drv = NULL;
721 return ret;
722 }
723 bs->backing_hd = bs_ro;
724 return rw_ret;
725 }
726 bs->backing_hd = bs_rw;
bellard33e39632003-07-06 17:15:21 +0000727 }
bellardea2384d2004-08-01 21:59:26 +0000728
Jan Kiszka6ea44302009-11-30 18:21:19 +0100729 total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000730 for (i = 0; i < total_sectors;) {
bellard19cb3732006-08-19 11:45:59 +0000731 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
bellardea2384d2004-08-01 21:59:26 +0000732 for(j = 0; j < n; j++) {
733 if (bdrv_read(bs, i, sector, 1) != 0) {
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200734 ret = -EIO;
735 goto ro_cleanup;
bellardea2384d2004-08-01 21:59:26 +0000736 }
737
738 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200739 ret = -EIO;
740 goto ro_cleanup;
bellardea2384d2004-08-01 21:59:26 +0000741 }
742 i++;
743 }
744 } else {
745 i += n;
746 }
747 }
bellard95389c82005-12-18 18:28:15 +0000748
Christoph Hellwig1d449522010-01-17 12:32:30 +0100749 if (drv->bdrv_make_empty) {
750 ret = drv->bdrv_make_empty(bs);
751 bdrv_flush(bs);
752 }
bellard95389c82005-12-18 18:28:15 +0000753
Christoph Hellwig3f5075a2010-01-12 13:49:23 +0100754 /*
755 * Make sure all data we wrote to the backing device is actually
756 * stable on disk.
757 */
758 if (bs->backing_hd)
759 bdrv_flush(bs->backing_hd);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200760
761ro_cleanup:
762
763 if (ro) {
764 /* re-open as RO */
765 bdrv_delete(bs->backing_hd);
766 bs->backing_hd = NULL;
767 bs_ro = bdrv_new("");
Kevin Wolfc3349192010-05-06 16:34:56 +0200768 ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, drv);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200769 if (ret < 0) {
770 bdrv_delete(bs_ro);
771 /* drive not functional anymore */
772 bs->drv = NULL;
773 return ret;
774 }
775 bs->backing_hd = bs_ro;
776 bs->backing_hd->keep_read_only = 0;
777 }
778
Christoph Hellwig1d449522010-01-17 12:32:30 +0100779 return ret;
bellard33e39632003-07-06 17:15:21 +0000780}
781
Kevin Wolf756e6732010-01-12 12:55:17 +0100782/*
783 * Return values:
784 * 0 - success
785 * -EINVAL - backing format specified, but no file
786 * -ENOSPC - can't update the backing file because no space is left in the
787 * image file header
788 * -ENOTSUP - format driver doesn't support changing the backing file
789 */
790int bdrv_change_backing_file(BlockDriverState *bs,
791 const char *backing_file, const char *backing_fmt)
792{
793 BlockDriver *drv = bs->drv;
794
795 if (drv->bdrv_change_backing_file != NULL) {
796 return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
797 } else {
798 return -ENOTSUP;
799 }
800}
801
aliguori71d07702009-03-03 17:37:16 +0000802static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
803 size_t size)
804{
805 int64_t len;
806
807 if (!bdrv_is_inserted(bs))
808 return -ENOMEDIUM;
809
810 if (bs->growable)
811 return 0;
812
813 len = bdrv_getlength(bs);
814
Kevin Wolffbb7b4e2009-05-08 14:47:24 +0200815 if (offset < 0)
816 return -EIO;
817
818 if ((offset > len) || (len - offset < size))
aliguori71d07702009-03-03 17:37:16 +0000819 return -EIO;
820
821 return 0;
822}
823
824static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
825 int nb_sectors)
826{
Jes Sorenseneb5a3162010-05-27 16:20:31 +0200827 return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE,
828 nb_sectors * BDRV_SECTOR_SIZE);
aliguori71d07702009-03-03 17:37:16 +0000829}
830
bellard19cb3732006-08-19 11:45:59 +0000831/* return < 0 if error. See bdrv_write() for the return codes */
ths5fafdf22007-09-16 21:08:06 +0000832int bdrv_read(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000833 uint8_t *buf, int nb_sectors)
834{
bellardea2384d2004-08-01 21:59:26 +0000835 BlockDriver *drv = bs->drv;
836
bellard19cb3732006-08-19 11:45:59 +0000837 if (!drv)
838 return -ENOMEDIUM;
aliguori71d07702009-03-03 17:37:16 +0000839 if (bdrv_check_request(bs, sector_num, nb_sectors))
840 return -EIO;
bellardb3380822004-03-14 21:38:54 +0000841
aliguorieda578e2009-03-12 19:57:16 +0000842 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
bellardfc01f7e2003-06-30 10:03:06 +0000843}
844
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +0200845static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num,
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100846 int nb_sectors, int dirty)
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +0200847{
848 int64_t start, end;
Jan Kiszkac6d22832009-11-30 18:21:20 +0100849 unsigned long val, idx, bit;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100850
Jan Kiszka6ea44302009-11-30 18:21:19 +0100851 start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkac6d22832009-11-30 18:21:20 +0100852 end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100853
854 for (; start <= end; start++) {
Jan Kiszkac6d22832009-11-30 18:21:20 +0100855 idx = start / (sizeof(unsigned long) * 8);
856 bit = start % (sizeof(unsigned long) * 8);
857 val = bs->dirty_bitmap[idx];
858 if (dirty) {
Liran Schouraaa0eb72010-01-26 10:31:48 +0200859 if (!(val & (1 << bit))) {
860 bs->dirty_count++;
861 val |= 1 << bit;
862 }
Jan Kiszkac6d22832009-11-30 18:21:20 +0100863 } else {
Liran Schouraaa0eb72010-01-26 10:31:48 +0200864 if (val & (1 << bit)) {
865 bs->dirty_count--;
866 val &= ~(1 << bit);
867 }
Jan Kiszkac6d22832009-11-30 18:21:20 +0100868 }
869 bs->dirty_bitmap[idx] = val;
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +0200870 }
871}
872
ths5fafdf22007-09-16 21:08:06 +0000873/* Return < 0 if error. Important errors are:
bellard19cb3732006-08-19 11:45:59 +0000874 -EIO generic I/O error (may happen for all errors)
875 -ENOMEDIUM No media inserted.
876 -EINVAL Invalid sector number or nb_sectors
877 -EACCES Trying to write a read-only device
878*/
ths5fafdf22007-09-16 21:08:06 +0000879int bdrv_write(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000880 const uint8_t *buf, int nb_sectors)
881{
bellard83f64092006-08-01 16:21:11 +0000882 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +0000883 if (!bs->drv)
884 return -ENOMEDIUM;
bellard0849bf02003-06-30 23:17:31 +0000885 if (bs->read_only)
bellard19cb3732006-08-19 11:45:59 +0000886 return -EACCES;
aliguori71d07702009-03-03 17:37:16 +0000887 if (bdrv_check_request(bs, sector_num, nb_sectors))
888 return -EIO;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100889
Jan Kiszkac6d22832009-11-30 18:21:20 +0100890 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +0200891 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
892 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100893
Kevin Wolf294cc352010-04-28 14:34:01 +0200894 if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
895 bs->wr_highest_sector = sector_num + nb_sectors - 1;
896 }
897
aliguori42fb2802009-01-15 20:43:39 +0000898 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
bellard83f64092006-08-01 16:21:11 +0000899}
900
aliguorieda578e2009-03-12 19:57:16 +0000901int bdrv_pread(BlockDriverState *bs, int64_t offset,
902 void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000903{
Jan Kiszka6ea44302009-11-30 18:21:19 +0100904 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
bellard83f64092006-08-01 16:21:11 +0000905 int len, nb_sectors, count;
906 int64_t sector_num;
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100907 int ret;
bellard83f64092006-08-01 16:21:11 +0000908
909 count = count1;
910 /* first read to align to sector start */
Jan Kiszka6ea44302009-11-30 18:21:19 +0100911 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
bellard83f64092006-08-01 16:21:11 +0000912 if (len > count)
913 len = count;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100914 sector_num = offset >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000915 if (len > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100916 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
917 return ret;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100918 memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
bellard83f64092006-08-01 16:21:11 +0000919 count -= len;
920 if (count == 0)
921 return count1;
922 sector_num++;
923 buf += len;
924 }
925
926 /* read the sectors "in place" */
Jan Kiszka6ea44302009-11-30 18:21:19 +0100927 nb_sectors = count >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000928 if (nb_sectors > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100929 if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0)
930 return ret;
bellard83f64092006-08-01 16:21:11 +0000931 sector_num += nb_sectors;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100932 len = nb_sectors << BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000933 buf += len;
934 count -= len;
935 }
936
937 /* add data from the last sector */
938 if (count > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100939 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
940 return ret;
bellard83f64092006-08-01 16:21:11 +0000941 memcpy(buf, tmp_buf, count);
942 }
943 return count1;
944}
945
aliguorieda578e2009-03-12 19:57:16 +0000946int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
947 const void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000948{
Jan Kiszka6ea44302009-11-30 18:21:19 +0100949 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
bellard83f64092006-08-01 16:21:11 +0000950 int len, nb_sectors, count;
951 int64_t sector_num;
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100952 int ret;
bellard83f64092006-08-01 16:21:11 +0000953
954 count = count1;
955 /* first write to align to sector start */
Jan Kiszka6ea44302009-11-30 18:21:19 +0100956 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
bellard83f64092006-08-01 16:21:11 +0000957 if (len > count)
958 len = count;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100959 sector_num = offset >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000960 if (len > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100961 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
962 return ret;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100963 memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len);
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100964 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
965 return ret;
bellard83f64092006-08-01 16:21:11 +0000966 count -= len;
967 if (count == 0)
968 return count1;
969 sector_num++;
970 buf += len;
971 }
972
973 /* write the sectors "in place" */
Jan Kiszka6ea44302009-11-30 18:21:19 +0100974 nb_sectors = count >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000975 if (nb_sectors > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100976 if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0)
977 return ret;
bellard83f64092006-08-01 16:21:11 +0000978 sector_num += nb_sectors;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100979 len = nb_sectors << BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000980 buf += len;
981 count -= len;
982 }
983
984 /* add data from the last sector */
985 if (count > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100986 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
987 return ret;
bellard83f64092006-08-01 16:21:11 +0000988 memcpy(tmp_buf, buf, count);
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100989 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
990 return ret;
bellard83f64092006-08-01 16:21:11 +0000991 }
992 return count1;
993}
bellard83f64092006-08-01 16:21:11 +0000994
995/**
bellard83f64092006-08-01 16:21:11 +0000996 * Truncate file to 'offset' bytes (needed only for file protocols)
997 */
998int bdrv_truncate(BlockDriverState *bs, int64_t offset)
999{
1000 BlockDriver *drv = bs->drv;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001001 int ret;
bellard83f64092006-08-01 16:21:11 +00001002 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001003 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +00001004 if (!drv->bdrv_truncate)
1005 return -ENOTSUP;
Naphtali Sprei59f26892009-10-26 16:25:16 +02001006 if (bs->read_only)
1007 return -EACCES;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001008 ret = drv->bdrv_truncate(bs, offset);
1009 if (ret == 0) {
1010 ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
1011 }
1012 return ret;
bellard83f64092006-08-01 16:21:11 +00001013}
1014
1015/**
1016 * Length of a file in bytes. Return < 0 if error or unknown.
1017 */
1018int64_t bdrv_getlength(BlockDriverState *bs)
1019{
1020 BlockDriver *drv = bs->drv;
1021 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001022 return -ENOMEDIUM;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001023
1024 /* Fixed size devices use the total_sectors value for speed instead of
1025 issuing a length query (like lseek) on each call. Also, legacy block
1026 drivers don't provide a bdrv_getlength function and must use
1027 total_sectors. */
1028 if (!bs->growable || !drv->bdrv_getlength) {
Jan Kiszka6ea44302009-11-30 18:21:19 +01001029 return bs->total_sectors * BDRV_SECTOR_SIZE;
bellard83f64092006-08-01 16:21:11 +00001030 }
1031 return drv->bdrv_getlength(bs);
bellardfc01f7e2003-06-30 10:03:06 +00001032}
1033
bellard19cb3732006-08-19 11:45:59 +00001034/* return 0 as number of sectors if no device present or error */
ths96b8f132007-12-17 01:35:20 +00001035void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
bellardfc01f7e2003-06-30 10:03:06 +00001036{
bellard19cb3732006-08-19 11:45:59 +00001037 int64_t length;
1038 length = bdrv_getlength(bs);
1039 if (length < 0)
1040 length = 0;
1041 else
Jan Kiszka6ea44302009-11-30 18:21:19 +01001042 length = length >> BDRV_SECTOR_BITS;
bellard19cb3732006-08-19 11:45:59 +00001043 *nb_sectors_ptr = length;
bellardfc01f7e2003-06-30 10:03:06 +00001044}
bellardcf989512004-02-16 21:56:36 +00001045
aliguorif3d54fc2008-11-25 21:50:24 +00001046struct partition {
1047 uint8_t boot_ind; /* 0x80 - active */
1048 uint8_t head; /* starting head */
1049 uint8_t sector; /* starting sector */
1050 uint8_t cyl; /* starting cylinder */
1051 uint8_t sys_ind; /* What partition type */
1052 uint8_t end_head; /* end head */
1053 uint8_t end_sector; /* end sector */
1054 uint8_t end_cyl; /* end cylinder */
1055 uint32_t start_sect; /* starting sector counting from 0 */
1056 uint32_t nr_sects; /* nr of sectors in partition */
1057} __attribute__((packed));
1058
1059/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
1060static int guess_disk_lchs(BlockDriverState *bs,
1061 int *pcylinders, int *pheads, int *psectors)
1062{
Jes Sorenseneb5a3162010-05-27 16:20:31 +02001063 uint8_t buf[BDRV_SECTOR_SIZE];
aliguorif3d54fc2008-11-25 21:50:24 +00001064 int ret, i, heads, sectors, cylinders;
1065 struct partition *p;
1066 uint32_t nr_sects;
blueswir1a38131b2008-12-05 17:56:40 +00001067 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +00001068
1069 bdrv_get_geometry(bs, &nb_sectors);
1070
1071 ret = bdrv_read(bs, 0, buf, 1);
1072 if (ret < 0)
1073 return -1;
1074 /* test msdos magic */
1075 if (buf[510] != 0x55 || buf[511] != 0xaa)
1076 return -1;
1077 for(i = 0; i < 4; i++) {
1078 p = ((struct partition *)(buf + 0x1be)) + i;
1079 nr_sects = le32_to_cpu(p->nr_sects);
1080 if (nr_sects && p->end_head) {
1081 /* We make the assumption that the partition terminates on
1082 a cylinder boundary */
1083 heads = p->end_head + 1;
1084 sectors = p->end_sector & 63;
1085 if (sectors == 0)
1086 continue;
1087 cylinders = nb_sectors / (heads * sectors);
1088 if (cylinders < 1 || cylinders > 16383)
1089 continue;
1090 *pheads = heads;
1091 *psectors = sectors;
1092 *pcylinders = cylinders;
1093#if 0
1094 printf("guessed geometry: LCHS=%d %d %d\n",
1095 cylinders, heads, sectors);
1096#endif
1097 return 0;
1098 }
1099 }
1100 return -1;
1101}
1102
1103void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
1104{
1105 int translation, lba_detected = 0;
1106 int cylinders, heads, secs;
blueswir1a38131b2008-12-05 17:56:40 +00001107 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +00001108
1109 /* if a geometry hint is available, use it */
1110 bdrv_get_geometry(bs, &nb_sectors);
1111 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
1112 translation = bdrv_get_translation_hint(bs);
1113 if (cylinders != 0) {
1114 *pcyls = cylinders;
1115 *pheads = heads;
1116 *psecs = secs;
1117 } else {
1118 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
1119 if (heads > 16) {
1120 /* if heads > 16, it means that a BIOS LBA
1121 translation was active, so the default
1122 hardware geometry is OK */
1123 lba_detected = 1;
1124 goto default_geometry;
1125 } else {
1126 *pcyls = cylinders;
1127 *pheads = heads;
1128 *psecs = secs;
1129 /* disable any translation to be in sync with
1130 the logical geometry */
1131 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
1132 bdrv_set_translation_hint(bs,
1133 BIOS_ATA_TRANSLATION_NONE);
1134 }
1135 }
1136 } else {
1137 default_geometry:
1138 /* if no geometry, use a standard physical disk geometry */
1139 cylinders = nb_sectors / (16 * 63);
1140
1141 if (cylinders > 16383)
1142 cylinders = 16383;
1143 else if (cylinders < 2)
1144 cylinders = 2;
1145 *pcyls = cylinders;
1146 *pheads = 16;
1147 *psecs = 63;
1148 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
1149 if ((*pcyls * *pheads) <= 131072) {
1150 bdrv_set_translation_hint(bs,
1151 BIOS_ATA_TRANSLATION_LARGE);
1152 } else {
1153 bdrv_set_translation_hint(bs,
1154 BIOS_ATA_TRANSLATION_LBA);
1155 }
1156 }
1157 }
1158 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
1159 }
1160}
1161
ths5fafdf22007-09-16 21:08:06 +00001162void bdrv_set_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +00001163 int cyls, int heads, int secs)
1164{
1165 bs->cyls = cyls;
1166 bs->heads = heads;
1167 bs->secs = secs;
1168}
1169
1170void bdrv_set_type_hint(BlockDriverState *bs, int type)
1171{
1172 bs->type = type;
1173 bs->removable = ((type == BDRV_TYPE_CDROM ||
1174 type == BDRV_TYPE_FLOPPY));
1175}
1176
bellard46d47672004-11-16 01:45:27 +00001177void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
1178{
1179 bs->translation = translation;
1180}
1181
ths5fafdf22007-09-16 21:08:06 +00001182void bdrv_get_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +00001183 int *pcyls, int *pheads, int *psecs)
1184{
1185 *pcyls = bs->cyls;
1186 *pheads = bs->heads;
1187 *psecs = bs->secs;
1188}
1189
1190int bdrv_get_type_hint(BlockDriverState *bs)
1191{
1192 return bs->type;
1193}
1194
bellard46d47672004-11-16 01:45:27 +00001195int bdrv_get_translation_hint(BlockDriverState *bs)
1196{
1197 return bs->translation;
1198}
1199
bellardb3380822004-03-14 21:38:54 +00001200int bdrv_is_removable(BlockDriverState *bs)
1201{
1202 return bs->removable;
1203}
1204
1205int bdrv_is_read_only(BlockDriverState *bs)
1206{
1207 return bs->read_only;
1208}
1209
ths985a03b2007-12-24 16:10:43 +00001210int bdrv_is_sg(BlockDriverState *bs)
1211{
1212 return bs->sg;
1213}
1214
Christoph Hellwige900a7b2009-09-04 19:01:15 +02001215int bdrv_enable_write_cache(BlockDriverState *bs)
1216{
1217 return bs->enable_write_cache;
1218}
1219
bellard19cb3732006-08-19 11:45:59 +00001220/* XXX: no longer used */
ths5fafdf22007-09-16 21:08:06 +00001221void bdrv_set_change_cb(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +00001222 void (*change_cb)(void *opaque), void *opaque)
1223{
1224 bs->change_cb = change_cb;
1225 bs->change_opaque = opaque;
1226}
1227
bellardea2384d2004-08-01 21:59:26 +00001228int bdrv_is_encrypted(BlockDriverState *bs)
1229{
1230 if (bs->backing_hd && bs->backing_hd->encrypted)
1231 return 1;
1232 return bs->encrypted;
1233}
1234
aliguoric0f4ce72009-03-05 23:01:01 +00001235int bdrv_key_required(BlockDriverState *bs)
1236{
1237 BlockDriverState *backing_hd = bs->backing_hd;
1238
1239 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
1240 return 1;
1241 return (bs->encrypted && !bs->valid_key);
1242}
1243
bellardea2384d2004-08-01 21:59:26 +00001244int bdrv_set_key(BlockDriverState *bs, const char *key)
1245{
1246 int ret;
1247 if (bs->backing_hd && bs->backing_hd->encrypted) {
1248 ret = bdrv_set_key(bs->backing_hd, key);
1249 if (ret < 0)
1250 return ret;
1251 if (!bs->encrypted)
1252 return 0;
1253 }
Shahar Havivifd04a2a2010-03-06 00:26:13 +02001254 if (!bs->encrypted) {
1255 return -EINVAL;
1256 } else if (!bs->drv || !bs->drv->bdrv_set_key) {
1257 return -ENOMEDIUM;
1258 }
aliguoric0f4ce72009-03-05 23:01:01 +00001259 ret = bs->drv->bdrv_set_key(bs, key);
aliguoribb5fc202009-03-05 23:01:15 +00001260 if (ret < 0) {
1261 bs->valid_key = 0;
1262 } else if (!bs->valid_key) {
1263 bs->valid_key = 1;
1264 /* call the change callback now, we skipped it on open */
1265 bs->media_changed = 1;
1266 if (bs->change_cb)
1267 bs->change_cb(bs->change_opaque);
1268 }
aliguoric0f4ce72009-03-05 23:01:01 +00001269 return ret;
bellardea2384d2004-08-01 21:59:26 +00001270}
1271
1272void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
1273{
bellard19cb3732006-08-19 11:45:59 +00001274 if (!bs->drv) {
bellardea2384d2004-08-01 21:59:26 +00001275 buf[0] = '\0';
1276 } else {
1277 pstrcpy(buf, buf_size, bs->drv->format_name);
1278 }
1279}
1280
ths5fafdf22007-09-16 21:08:06 +00001281void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
bellardea2384d2004-08-01 21:59:26 +00001282 void *opaque)
1283{
1284 BlockDriver *drv;
1285
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +01001286 QLIST_FOREACH(drv, &bdrv_drivers, list) {
bellardea2384d2004-08-01 21:59:26 +00001287 it(opaque, drv->format_name);
1288 }
1289}
1290
bellardb3380822004-03-14 21:38:54 +00001291BlockDriverState *bdrv_find(const char *name)
1292{
1293 BlockDriverState *bs;
1294
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001295 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1296 if (!strcmp(name, bs->device_name)) {
bellardb3380822004-03-14 21:38:54 +00001297 return bs;
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001298 }
bellardb3380822004-03-14 21:38:54 +00001299 }
1300 return NULL;
1301}
1302
aliguori51de9762009-03-05 23:00:43 +00001303void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
bellard81d09122004-07-14 17:21:37 +00001304{
1305 BlockDriverState *bs;
1306
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001307 QTAILQ_FOREACH(bs, &bdrv_states, list) {
aliguori51de9762009-03-05 23:00:43 +00001308 it(opaque, bs);
bellard81d09122004-07-14 17:21:37 +00001309 }
1310}
1311
bellardea2384d2004-08-01 21:59:26 +00001312const char *bdrv_get_device_name(BlockDriverState *bs)
1313{
1314 return bs->device_name;
1315}
1316
pbrook7a6cba62006-06-04 11:39:07 +00001317void bdrv_flush(BlockDriverState *bs)
1318{
Alexander Graf016f5cf2010-05-26 17:51:49 +02001319 if (bs->open_flags & BDRV_O_NO_FLUSH) {
1320 return;
1321 }
1322
Christoph Hellwig3f5075a2010-01-12 13:49:23 +01001323 if (bs->drv && bs->drv->bdrv_flush)
pbrook7a6cba62006-06-04 11:39:07 +00001324 bs->drv->bdrv_flush(bs);
pbrook7a6cba62006-06-04 11:39:07 +00001325}
1326
aliguoric6ca28d2008-10-06 13:55:43 +00001327void bdrv_flush_all(void)
1328{
1329 BlockDriverState *bs;
1330
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001331 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1332 if (bs->drv && !bdrv_is_read_only(bs) &&
1333 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) {
aliguoric6ca28d2008-10-06 13:55:43 +00001334 bdrv_flush(bs);
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001335 }
1336 }
aliguoric6ca28d2008-10-06 13:55:43 +00001337}
1338
Kevin Wolff2feebb2010-04-14 17:30:35 +02001339int bdrv_has_zero_init(BlockDriverState *bs)
1340{
1341 assert(bs->drv);
1342
1343 if (bs->drv->no_zero_init) {
1344 return 0;
1345 } else if (bs->file) {
1346 return bdrv_has_zero_init(bs->file);
1347 }
1348
1349 return 1;
1350}
1351
thsf58c7b32008-06-05 21:53:49 +00001352/*
1353 * Returns true iff the specified sector is present in the disk image. Drivers
1354 * not implementing the functionality are assumed to not support backing files,
1355 * hence all their sectors are reported as allocated.
1356 *
1357 * 'pnum' is set to the number of sectors (including and immediately following
1358 * the specified sector) that are known to be in the same
1359 * allocated/unallocated state.
1360 *
1361 * 'nb_sectors' is the max value 'pnum' should be set to.
1362 */
1363int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1364 int *pnum)
1365{
1366 int64_t n;
1367 if (!bs->drv->bdrv_is_allocated) {
1368 if (sector_num >= bs->total_sectors) {
1369 *pnum = 0;
1370 return 0;
1371 }
1372 n = bs->total_sectors - sector_num;
1373 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1374 return 1;
1375 }
1376 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1377}
1378
Luiz Capitulino2582bfe2010-02-03 12:41:01 -02001379void bdrv_mon_event(const BlockDriverState *bdrv,
1380 BlockMonEventAction action, int is_read)
1381{
1382 QObject *data;
1383 const char *action_str;
1384
1385 switch (action) {
1386 case BDRV_ACTION_REPORT:
1387 action_str = "report";
1388 break;
1389 case BDRV_ACTION_IGNORE:
1390 action_str = "ignore";
1391 break;
1392 case BDRV_ACTION_STOP:
1393 action_str = "stop";
1394 break;
1395 default:
1396 abort();
1397 }
1398
1399 data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
1400 bdrv->device_name,
1401 action_str,
1402 is_read ? "read" : "write");
1403 monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data);
1404
1405 qobject_decref(data);
1406}
1407
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001408static void bdrv_print_dict(QObject *obj, void *opaque)
bellardb3380822004-03-14 21:38:54 +00001409{
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001410 QDict *bs_dict;
1411 Monitor *mon = opaque;
1412
1413 bs_dict = qobject_to_qdict(obj);
1414
1415 monitor_printf(mon, "%s: type=%s removable=%d",
1416 qdict_get_str(bs_dict, "device"),
1417 qdict_get_str(bs_dict, "type"),
1418 qdict_get_bool(bs_dict, "removable"));
1419
1420 if (qdict_get_bool(bs_dict, "removable")) {
1421 monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked"));
1422 }
1423
1424 if (qdict_haskey(bs_dict, "inserted")) {
1425 QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted"));
1426
1427 monitor_printf(mon, " file=");
1428 monitor_print_filename(mon, qdict_get_str(qdict, "file"));
1429 if (qdict_haskey(qdict, "backing_file")) {
1430 monitor_printf(mon, " backing_file=");
1431 monitor_print_filename(mon, qdict_get_str(qdict, "backing_file"));
1432 }
1433 monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
1434 qdict_get_bool(qdict, "ro"),
1435 qdict_get_str(qdict, "drv"),
1436 qdict_get_bool(qdict, "encrypted"));
1437 } else {
1438 monitor_printf(mon, " [not inserted]");
1439 }
1440
1441 monitor_printf(mon, "\n");
1442}
1443
1444void bdrv_info_print(Monitor *mon, const QObject *data)
1445{
1446 qlist_iter(qobject_to_qlist(data), bdrv_print_dict, mon);
1447}
1448
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001449void bdrv_info(Monitor *mon, QObject **ret_data)
1450{
1451 QList *bs_list;
bellardb3380822004-03-14 21:38:54 +00001452 BlockDriverState *bs;
1453
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001454 bs_list = qlist_new();
1455
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001456 QTAILQ_FOREACH(bs, &bdrv_states, list) {
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001457 QObject *bs_obj;
1458 const char *type = "unknown";
1459
bellardb3380822004-03-14 21:38:54 +00001460 switch(bs->type) {
1461 case BDRV_TYPE_HD:
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001462 type = "hd";
bellardb3380822004-03-14 21:38:54 +00001463 break;
1464 case BDRV_TYPE_CDROM:
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001465 type = "cdrom";
bellardb3380822004-03-14 21:38:54 +00001466 break;
1467 case BDRV_TYPE_FLOPPY:
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001468 type = "floppy";
bellardb3380822004-03-14 21:38:54 +00001469 break;
1470 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001471
1472 bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': %s, "
1473 "'removable': %i, 'locked': %i }",
1474 bs->device_name, type, bs->removable,
1475 bs->locked);
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001476
bellard19cb3732006-08-19 11:45:59 +00001477 if (bs->drv) {
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001478 QObject *obj;
1479 QDict *bs_dict = qobject_to_qdict(bs_obj);
1480
1481 obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, "
1482 "'encrypted': %i }",
1483 bs->filename, bs->read_only,
1484 bs->drv->format_name,
1485 bdrv_is_encrypted(bs));
thsfef30742006-12-22 14:11:32 +00001486 if (bs->backing_file[0] != '\0') {
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001487 QDict *qdict = qobject_to_qdict(obj);
1488 qdict_put(qdict, "backing_file",
1489 qstring_from_str(bs->backing_file));
aliguori376253e2009-03-05 23:01:23 +00001490 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001491
1492 qdict_put_obj(bs_dict, "inserted", obj);
bellardb3380822004-03-14 21:38:54 +00001493 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001494 qlist_append_obj(bs_list, bs_obj);
bellardb3380822004-03-14 21:38:54 +00001495 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001496
1497 *ret_data = QOBJECT(bs_list);
bellardb3380822004-03-14 21:38:54 +00001498}
thsa36e69d2007-12-02 05:18:19 +00001499
Luiz Capitulino218a5362009-12-10 17:16:07 -02001500static void bdrv_stats_iter(QObject *data, void *opaque)
thsa36e69d2007-12-02 05:18:19 +00001501{
Luiz Capitulino218a5362009-12-10 17:16:07 -02001502 QDict *qdict;
1503 Monitor *mon = opaque;
1504
1505 qdict = qobject_to_qdict(data);
1506 monitor_printf(mon, "%s:", qdict_get_str(qdict, "device"));
1507
1508 qdict = qobject_to_qdict(qdict_get(qdict, "stats"));
1509 monitor_printf(mon, " rd_bytes=%" PRId64
1510 " wr_bytes=%" PRId64
1511 " rd_operations=%" PRId64
1512 " wr_operations=%" PRId64
1513 "\n",
1514 qdict_get_int(qdict, "rd_bytes"),
1515 qdict_get_int(qdict, "wr_bytes"),
1516 qdict_get_int(qdict, "rd_operations"),
1517 qdict_get_int(qdict, "wr_operations"));
1518}
1519
1520void bdrv_stats_print(Monitor *mon, const QObject *data)
1521{
1522 qlist_iter(qobject_to_qlist(data), bdrv_stats_iter, mon);
1523}
1524
Kevin Wolf294cc352010-04-28 14:34:01 +02001525static QObject* bdrv_info_stats_bs(BlockDriverState *bs)
1526{
1527 QObject *res;
1528 QDict *dict;
1529
1530 res = qobject_from_jsonf("{ 'stats': {"
1531 "'rd_bytes': %" PRId64 ","
1532 "'wr_bytes': %" PRId64 ","
1533 "'rd_operations': %" PRId64 ","
1534 "'wr_operations': %" PRId64 ","
1535 "'wr_highest_offset': %" PRId64
1536 "} }",
1537 bs->rd_bytes, bs->wr_bytes,
1538 bs->rd_ops, bs->wr_ops,
Jes Sorenseneb5a3162010-05-27 16:20:31 +02001539 bs->wr_highest_sector * (long)BDRV_SECTOR_SIZE);
Kevin Wolf294cc352010-04-28 14:34:01 +02001540 dict = qobject_to_qdict(res);
1541
1542 if (*bs->device_name) {
1543 qdict_put(dict, "device", qstring_from_str(bs->device_name));
1544 }
1545
1546 if (bs->file) {
1547 QObject *parent = bdrv_info_stats_bs(bs->file);
1548 qdict_put_obj(dict, "parent", parent);
1549 }
1550
1551 return res;
1552}
1553
Luiz Capitulino218a5362009-12-10 17:16:07 -02001554void bdrv_info_stats(Monitor *mon, QObject **ret_data)
1555{
1556 QObject *obj;
1557 QList *devices;
thsa36e69d2007-12-02 05:18:19 +00001558 BlockDriverState *bs;
1559
Luiz Capitulino218a5362009-12-10 17:16:07 -02001560 devices = qlist_new();
1561
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001562 QTAILQ_FOREACH(bs, &bdrv_states, list) {
Kevin Wolf294cc352010-04-28 14:34:01 +02001563 obj = bdrv_info_stats_bs(bs);
Luiz Capitulino218a5362009-12-10 17:16:07 -02001564 qlist_append_obj(devices, obj);
thsa36e69d2007-12-02 05:18:19 +00001565 }
Luiz Capitulino218a5362009-12-10 17:16:07 -02001566
1567 *ret_data = QOBJECT(devices);
thsa36e69d2007-12-02 05:18:19 +00001568}
bellardea2384d2004-08-01 21:59:26 +00001569
aliguori045df332009-03-05 23:00:48 +00001570const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1571{
1572 if (bs->backing_hd && bs->backing_hd->encrypted)
1573 return bs->backing_file;
1574 else if (bs->encrypted)
1575 return bs->filename;
1576 else
1577 return NULL;
1578}
1579
ths5fafdf22007-09-16 21:08:06 +00001580void bdrv_get_backing_filename(BlockDriverState *bs,
bellard83f64092006-08-01 16:21:11 +00001581 char *filename, int filename_size)
bellardea2384d2004-08-01 21:59:26 +00001582{
Kevin Wolfb783e402010-01-12 12:55:16 +01001583 if (!bs->backing_file) {
bellard83f64092006-08-01 16:21:11 +00001584 pstrcpy(filename, filename_size, "");
1585 } else {
1586 pstrcpy(filename, filename_size, bs->backing_file);
1587 }
bellardea2384d2004-08-01 21:59:26 +00001588}
1589
ths5fafdf22007-09-16 21:08:06 +00001590int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
bellardfaea38e2006-08-05 21:31:00 +00001591 const uint8_t *buf, int nb_sectors)
1592{
1593 BlockDriver *drv = bs->drv;
1594 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001595 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001596 if (!drv->bdrv_write_compressed)
1597 return -ENOTSUP;
Kevin Wolffbb7b4e2009-05-08 14:47:24 +02001598 if (bdrv_check_request(bs, sector_num, nb_sectors))
1599 return -EIO;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001600
Jan Kiszkac6d22832009-11-30 18:21:20 +01001601 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001602 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1603 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001604
bellardfaea38e2006-08-05 21:31:00 +00001605 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1606}
ths3b46e622007-09-17 08:09:54 +00001607
bellardfaea38e2006-08-05 21:31:00 +00001608int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1609{
1610 BlockDriver *drv = bs->drv;
1611 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001612 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001613 if (!drv->bdrv_get_info)
1614 return -ENOTSUP;
1615 memset(bdi, 0, sizeof(*bdi));
1616 return drv->bdrv_get_info(bs, bdi);
1617}
1618
Christoph Hellwig45566e92009-07-10 23:11:57 +02001619int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
1620 int64_t pos, int size)
aliguori178e08a2009-04-05 19:10:55 +00001621{
1622 BlockDriver *drv = bs->drv;
1623 if (!drv)
1624 return -ENOMEDIUM;
Christoph Hellwig45566e92009-07-10 23:11:57 +02001625 if (!drv->bdrv_save_vmstate)
aliguori178e08a2009-04-05 19:10:55 +00001626 return -ENOTSUP;
Christoph Hellwig45566e92009-07-10 23:11:57 +02001627 return drv->bdrv_save_vmstate(bs, buf, pos, size);
aliguori178e08a2009-04-05 19:10:55 +00001628}
1629
Christoph Hellwig45566e92009-07-10 23:11:57 +02001630int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1631 int64_t pos, int size)
aliguori178e08a2009-04-05 19:10:55 +00001632{
1633 BlockDriver *drv = bs->drv;
1634 if (!drv)
1635 return -ENOMEDIUM;
Christoph Hellwig45566e92009-07-10 23:11:57 +02001636 if (!drv->bdrv_load_vmstate)
aliguori178e08a2009-04-05 19:10:55 +00001637 return -ENOTSUP;
Christoph Hellwig45566e92009-07-10 23:11:57 +02001638 return drv->bdrv_load_vmstate(bs, buf, pos, size);
aliguori178e08a2009-04-05 19:10:55 +00001639}
1640
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01001641void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
1642{
1643 BlockDriver *drv = bs->drv;
1644
1645 if (!drv || !drv->bdrv_debug_event) {
1646 return;
1647 }
1648
1649 return drv->bdrv_debug_event(bs, event);
1650
1651}
1652
bellardfaea38e2006-08-05 21:31:00 +00001653/**************************************************************/
1654/* handling of snapshots */
1655
ths5fafdf22007-09-16 21:08:06 +00001656int bdrv_snapshot_create(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001657 QEMUSnapshotInfo *sn_info)
1658{
1659 BlockDriver *drv = bs->drv;
1660 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001661 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001662 if (!drv->bdrv_snapshot_create)
1663 return -ENOTSUP;
1664 return drv->bdrv_snapshot_create(bs, sn_info);
1665}
1666
ths5fafdf22007-09-16 21:08:06 +00001667int bdrv_snapshot_goto(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001668 const char *snapshot_id)
1669{
1670 BlockDriver *drv = bs->drv;
1671 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001672 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001673 if (!drv->bdrv_snapshot_goto)
1674 return -ENOTSUP;
1675 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1676}
1677
1678int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1679{
1680 BlockDriver *drv = bs->drv;
1681 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001682 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001683 if (!drv->bdrv_snapshot_delete)
1684 return -ENOTSUP;
1685 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1686}
1687
ths5fafdf22007-09-16 21:08:06 +00001688int bdrv_snapshot_list(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001689 QEMUSnapshotInfo **psn_info)
1690{
1691 BlockDriver *drv = bs->drv;
1692 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001693 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001694 if (!drv->bdrv_snapshot_list)
1695 return -ENOTSUP;
1696 return drv->bdrv_snapshot_list(bs, psn_info);
1697}
1698
1699#define NB_SUFFIXES 4
1700
1701char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1702{
1703 static const char suffixes[NB_SUFFIXES] = "KMGT";
1704 int64_t base;
1705 int i;
1706
1707 if (size <= 999) {
1708 snprintf(buf, buf_size, "%" PRId64, size);
1709 } else {
1710 base = 1024;
1711 for(i = 0; i < NB_SUFFIXES; i++) {
1712 if (size < (10 * base)) {
ths5fafdf22007-09-16 21:08:06 +00001713 snprintf(buf, buf_size, "%0.1f%c",
bellardfaea38e2006-08-05 21:31:00 +00001714 (double)size / base,
1715 suffixes[i]);
1716 break;
1717 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
ths5fafdf22007-09-16 21:08:06 +00001718 snprintf(buf, buf_size, "%" PRId64 "%c",
bellardfaea38e2006-08-05 21:31:00 +00001719 ((size + (base >> 1)) / base),
1720 suffixes[i]);
1721 break;
1722 }
1723 base = base * 1024;
1724 }
1725 }
1726 return buf;
1727}
1728
1729char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1730{
1731 char buf1[128], date_buf[128], clock_buf[128];
bellard3b9f94e2007-01-07 17:27:07 +00001732#ifdef _WIN32
1733 struct tm *ptm;
1734#else
bellardfaea38e2006-08-05 21:31:00 +00001735 struct tm tm;
bellard3b9f94e2007-01-07 17:27:07 +00001736#endif
bellardfaea38e2006-08-05 21:31:00 +00001737 time_t ti;
1738 int64_t secs;
1739
1740 if (!sn) {
ths5fafdf22007-09-16 21:08:06 +00001741 snprintf(buf, buf_size,
1742 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001743 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1744 } else {
1745 ti = sn->date_sec;
bellard3b9f94e2007-01-07 17:27:07 +00001746#ifdef _WIN32
1747 ptm = localtime(&ti);
1748 strftime(date_buf, sizeof(date_buf),
1749 "%Y-%m-%d %H:%M:%S", ptm);
1750#else
bellardfaea38e2006-08-05 21:31:00 +00001751 localtime_r(&ti, &tm);
1752 strftime(date_buf, sizeof(date_buf),
1753 "%Y-%m-%d %H:%M:%S", &tm);
bellard3b9f94e2007-01-07 17:27:07 +00001754#endif
bellardfaea38e2006-08-05 21:31:00 +00001755 secs = sn->vm_clock_nsec / 1000000000;
1756 snprintf(clock_buf, sizeof(clock_buf),
1757 "%02d:%02d:%02d.%03d",
1758 (int)(secs / 3600),
1759 (int)((secs / 60) % 60),
ths5fafdf22007-09-16 21:08:06 +00001760 (int)(secs % 60),
bellardfaea38e2006-08-05 21:31:00 +00001761 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1762 snprintf(buf, buf_size,
ths5fafdf22007-09-16 21:08:06 +00001763 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001764 sn->id_str, sn->name,
1765 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1766 date_buf,
1767 clock_buf);
1768 }
1769 return buf;
1770}
1771
bellardea2384d2004-08-01 21:59:26 +00001772
bellard83f64092006-08-01 16:21:11 +00001773/**************************************************************/
1774/* async I/Os */
1775
aliguori3b69e4b2009-01-22 16:59:24 +00001776BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
aliguorif141eaf2009-04-07 18:43:24 +00001777 QEMUIOVector *qiov, int nb_sectors,
aliguori3b69e4b2009-01-22 16:59:24 +00001778 BlockDriverCompletionFunc *cb, void *opaque)
1779{
bellard83f64092006-08-01 16:21:11 +00001780 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001781 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001782
bellard19cb3732006-08-19 11:45:59 +00001783 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001784 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001785 if (bdrv_check_request(bs, sector_num, nb_sectors))
1786 return NULL;
ths3b46e622007-09-17 08:09:54 +00001787
aliguorif141eaf2009-04-07 18:43:24 +00001788 ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
1789 cb, opaque);
thsa36e69d2007-12-02 05:18:19 +00001790
1791 if (ret) {
1792 /* Update stats even though technically transfer has not happened. */
Jan Kiszka6ea44302009-11-30 18:21:19 +01001793 bs->rd_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
thsa36e69d2007-12-02 05:18:19 +00001794 bs->rd_ops ++;
1795 }
1796
1797 return ret;
bellard83f64092006-08-01 16:21:11 +00001798}
1799
aliguorif141eaf2009-04-07 18:43:24 +00001800BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1801 QEMUIOVector *qiov, int nb_sectors,
1802 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001803{
bellard83f64092006-08-01 16:21:11 +00001804 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001805 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001806
bellard19cb3732006-08-19 11:45:59 +00001807 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001808 return NULL;
bellard83f64092006-08-01 16:21:11 +00001809 if (bs->read_only)
pbrookce1a14d2006-08-07 02:38:06 +00001810 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001811 if (bdrv_check_request(bs, sector_num, nb_sectors))
1812 return NULL;
bellard83f64092006-08-01 16:21:11 +00001813
Jan Kiszkac6d22832009-11-30 18:21:20 +01001814 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001815 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1816 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001817
aliguorif141eaf2009-04-07 18:43:24 +00001818 ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
1819 cb, opaque);
thsa36e69d2007-12-02 05:18:19 +00001820
1821 if (ret) {
Kevin Wolf294cc352010-04-28 14:34:01 +02001822 /* Update stats even though technically transfer has not happened. */
1823 bs->wr_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
1824 bs->wr_ops ++;
1825 if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
1826 bs->wr_highest_sector = sector_num + nb_sectors - 1;
1827 }
thsa36e69d2007-12-02 05:18:19 +00001828 }
1829
1830 return ret;
bellard83f64092006-08-01 16:21:11 +00001831}
1832
Kevin Wolf40b4f532009-09-09 17:53:37 +02001833
1834typedef struct MultiwriteCB {
1835 int error;
1836 int num_requests;
1837 int num_callbacks;
1838 struct {
1839 BlockDriverCompletionFunc *cb;
1840 void *opaque;
1841 QEMUIOVector *free_qiov;
1842 void *free_buf;
1843 } callbacks[];
1844} MultiwriteCB;
1845
1846static void multiwrite_user_cb(MultiwriteCB *mcb)
1847{
1848 int i;
1849
1850 for (i = 0; i < mcb->num_callbacks; i++) {
1851 mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
Stefan Hajnoczi1e1ea482010-04-21 20:35:45 +01001852 if (mcb->callbacks[i].free_qiov) {
1853 qemu_iovec_destroy(mcb->callbacks[i].free_qiov);
1854 }
Kevin Wolf40b4f532009-09-09 17:53:37 +02001855 qemu_free(mcb->callbacks[i].free_qiov);
Herve Poussineauf8a83242010-01-24 21:23:56 +00001856 qemu_vfree(mcb->callbacks[i].free_buf);
Kevin Wolf40b4f532009-09-09 17:53:37 +02001857 }
1858}
1859
1860static void multiwrite_cb(void *opaque, int ret)
1861{
1862 MultiwriteCB *mcb = opaque;
1863
Kevin Wolfcb6d3ca2010-04-01 22:48:44 +02001864 if (ret < 0 && !mcb->error) {
Kevin Wolf40b4f532009-09-09 17:53:37 +02001865 mcb->error = ret;
1866 multiwrite_user_cb(mcb);
1867 }
1868
1869 mcb->num_requests--;
1870 if (mcb->num_requests == 0) {
1871 if (mcb->error == 0) {
1872 multiwrite_user_cb(mcb);
1873 }
1874 qemu_free(mcb);
1875 }
1876}
1877
1878static int multiwrite_req_compare(const void *a, const void *b)
1879{
Christoph Hellwig77be4362010-05-19 20:53:10 +02001880 const BlockRequest *req1 = a, *req2 = b;
1881
1882 /*
1883 * Note that we can't simply subtract req2->sector from req1->sector
1884 * here as that could overflow the return value.
1885 */
1886 if (req1->sector > req2->sector) {
1887 return 1;
1888 } else if (req1->sector < req2->sector) {
1889 return -1;
1890 } else {
1891 return 0;
1892 }
Kevin Wolf40b4f532009-09-09 17:53:37 +02001893}
1894
1895/*
1896 * Takes a bunch of requests and tries to merge them. Returns the number of
1897 * requests that remain after merging.
1898 */
1899static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
1900 int num_reqs, MultiwriteCB *mcb)
1901{
1902 int i, outidx;
1903
1904 // Sort requests by start sector
1905 qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
1906
1907 // Check if adjacent requests touch the same clusters. If so, combine them,
1908 // filling up gaps with zero sectors.
1909 outidx = 0;
1910 for (i = 1; i < num_reqs; i++) {
1911 int merge = 0;
1912 int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
1913
1914 // This handles the cases that are valid for all block drivers, namely
1915 // exactly sequential writes and overlapping writes.
1916 if (reqs[i].sector <= oldreq_last) {
1917 merge = 1;
1918 }
1919
1920 // The block driver may decide that it makes sense to combine requests
1921 // even if there is a gap of some sectors between them. In this case,
1922 // the gap is filled with zeros (therefore only applicable for yet
1923 // unused space in format like qcow2).
1924 if (!merge && bs->drv->bdrv_merge_requests) {
1925 merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]);
1926 }
1927
Christoph Hellwige2a305f2010-01-26 14:49:08 +01001928 if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) {
1929 merge = 0;
1930 }
1931
Kevin Wolf40b4f532009-09-09 17:53:37 +02001932 if (merge) {
1933 size_t size;
1934 QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov));
1935 qemu_iovec_init(qiov,
1936 reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
1937
1938 // Add the first request to the merged one. If the requests are
1939 // overlapping, drop the last sectors of the first request.
1940 size = (reqs[i].sector - reqs[outidx].sector) << 9;
1941 qemu_iovec_concat(qiov, reqs[outidx].qiov, size);
1942
1943 // We might need to add some zeros between the two requests
1944 if (reqs[i].sector > oldreq_last) {
1945 size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
1946 uint8_t *buf = qemu_blockalign(bs, zero_bytes);
1947 memset(buf, 0, zero_bytes);
1948 qemu_iovec_add(qiov, buf, zero_bytes);
1949 mcb->callbacks[i].free_buf = buf;
1950 }
1951
1952 // Add the second request
1953 qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size);
1954
Kevin Wolfcbf1dff2010-05-21 11:09:42 +02001955 reqs[outidx].nb_sectors = qiov->size >> 9;
Kevin Wolf40b4f532009-09-09 17:53:37 +02001956 reqs[outidx].qiov = qiov;
1957
1958 mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
1959 } else {
1960 outidx++;
1961 reqs[outidx].sector = reqs[i].sector;
1962 reqs[outidx].nb_sectors = reqs[i].nb_sectors;
1963 reqs[outidx].qiov = reqs[i].qiov;
1964 }
1965 }
1966
1967 return outidx + 1;
1968}
1969
1970/*
1971 * Submit multiple AIO write requests at once.
1972 *
1973 * On success, the function returns 0 and all requests in the reqs array have
1974 * been submitted. In error case this function returns -1, and any of the
1975 * requests may or may not be submitted yet. In particular, this means that the
1976 * callback will be called for some of the requests, for others it won't. The
1977 * caller must check the error field of the BlockRequest to wait for the right
1978 * callbacks (if error != 0, no callback will be called).
1979 *
1980 * The implementation may modify the contents of the reqs array, e.g. to merge
1981 * requests. However, the fields opaque and error are left unmodified as they
1982 * are used to signal failure for a single request to the caller.
1983 */
1984int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
1985{
1986 BlockDriverAIOCB *acb;
1987 MultiwriteCB *mcb;
1988 int i;
1989
1990 if (num_reqs == 0) {
1991 return 0;
1992 }
1993
1994 // Create MultiwriteCB structure
1995 mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
1996 mcb->num_requests = 0;
1997 mcb->num_callbacks = num_reqs;
1998
1999 for (i = 0; i < num_reqs; i++) {
2000 mcb->callbacks[i].cb = reqs[i].cb;
2001 mcb->callbacks[i].opaque = reqs[i].opaque;
2002 }
2003
2004 // Check for mergable requests
2005 num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
2006
2007 // Run the aio requests
2008 for (i = 0; i < num_reqs; i++) {
2009 acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
2010 reqs[i].nb_sectors, multiwrite_cb, mcb);
2011
2012 if (acb == NULL) {
2013 // We can only fail the whole thing if no request has been
2014 // submitted yet. Otherwise we'll wait for the submitted AIOs to
2015 // complete and report the error in the callback.
2016 if (mcb->num_requests == 0) {
Kevin Wolf0f0b6042010-04-06 18:24:06 +02002017 reqs[i].error = -EIO;
Kevin Wolf40b4f532009-09-09 17:53:37 +02002018 goto fail;
2019 } else {
Kevin Wolf7eb58a62010-04-06 18:24:07 +02002020 mcb->num_requests++;
2021 multiwrite_cb(mcb, -EIO);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002022 break;
2023 }
2024 } else {
2025 mcb->num_requests++;
2026 }
2027 }
2028
2029 return 0;
2030
2031fail:
Bruce Rogersaf474592010-05-13 15:14:33 -06002032 qemu_free(mcb);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002033 return -1;
2034}
2035
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002036BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
2037 BlockDriverCompletionFunc *cb, void *opaque)
2038{
2039 BlockDriver *drv = bs->drv;
2040
Alexander Graf016f5cf2010-05-26 17:51:49 +02002041 if (bs->open_flags & BDRV_O_NO_FLUSH) {
2042 return bdrv_aio_noop_em(bs, cb, opaque);
2043 }
2044
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002045 if (!drv)
2046 return NULL;
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002047 return drv->bdrv_aio_flush(bs, cb, opaque);
2048}
2049
bellard83f64092006-08-01 16:21:11 +00002050void bdrv_aio_cancel(BlockDriverAIOCB *acb)
pbrookce1a14d2006-08-07 02:38:06 +00002051{
aliguori6bbff9a2009-03-20 18:25:59 +00002052 acb->pool->cancel(acb);
bellard83f64092006-08-01 16:21:11 +00002053}
2054
pbrookce1a14d2006-08-07 02:38:06 +00002055
bellard83f64092006-08-01 16:21:11 +00002056/**************************************************************/
2057/* async block device emulation */
2058
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002059typedef struct BlockDriverAIOCBSync {
2060 BlockDriverAIOCB common;
2061 QEMUBH *bh;
2062 int ret;
2063 /* vector translation state */
2064 QEMUIOVector *qiov;
2065 uint8_t *bounce;
2066 int is_write;
2067} BlockDriverAIOCBSync;
2068
2069static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
2070{
Kevin Wolfb666d232010-05-05 11:44:39 +02002071 BlockDriverAIOCBSync *acb =
2072 container_of(blockacb, BlockDriverAIOCBSync, common);
Dor Laor6a7ad292009-06-01 12:07:23 +03002073 qemu_bh_delete(acb->bh);
Avi Kivity36afc452009-06-23 16:20:36 +03002074 acb->bh = NULL;
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002075 qemu_aio_release(acb);
2076}
2077
2078static AIOPool bdrv_em_aio_pool = {
2079 .aiocb_size = sizeof(BlockDriverAIOCBSync),
2080 .cancel = bdrv_aio_cancel_em,
2081};
2082
bellard83f64092006-08-01 16:21:11 +00002083static void bdrv_aio_bh_cb(void *opaque)
bellardbeac80c2006-06-26 20:08:57 +00002084{
pbrookce1a14d2006-08-07 02:38:06 +00002085 BlockDriverAIOCBSync *acb = opaque;
aliguorif141eaf2009-04-07 18:43:24 +00002086
aliguorif141eaf2009-04-07 18:43:24 +00002087 if (!acb->is_write)
2088 qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
aliguoriceb42de2009-04-07 18:43:28 +00002089 qemu_vfree(acb->bounce);
pbrookce1a14d2006-08-07 02:38:06 +00002090 acb->common.cb(acb->common.opaque, acb->ret);
Dor Laor6a7ad292009-06-01 12:07:23 +03002091 qemu_bh_delete(acb->bh);
Avi Kivity36afc452009-06-23 16:20:36 +03002092 acb->bh = NULL;
pbrookce1a14d2006-08-07 02:38:06 +00002093 qemu_aio_release(acb);
bellardbeac80c2006-06-26 20:08:57 +00002094}
bellardbeac80c2006-06-26 20:08:57 +00002095
aliguorif141eaf2009-04-07 18:43:24 +00002096static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
2097 int64_t sector_num,
2098 QEMUIOVector *qiov,
2099 int nb_sectors,
2100 BlockDriverCompletionFunc *cb,
2101 void *opaque,
2102 int is_write)
2103
bellardea2384d2004-08-01 21:59:26 +00002104{
pbrookce1a14d2006-08-07 02:38:06 +00002105 BlockDriverAIOCBSync *acb;
pbrookce1a14d2006-08-07 02:38:06 +00002106
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002107 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
aliguorif141eaf2009-04-07 18:43:24 +00002108 acb->is_write = is_write;
2109 acb->qiov = qiov;
aliguorie268ca52009-04-22 20:20:00 +00002110 acb->bounce = qemu_blockalign(bs, qiov->size);
aliguorif141eaf2009-04-07 18:43:24 +00002111
pbrookce1a14d2006-08-07 02:38:06 +00002112 if (!acb->bh)
2113 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
aliguorif141eaf2009-04-07 18:43:24 +00002114
2115 if (is_write) {
2116 qemu_iovec_to_buffer(acb->qiov, acb->bounce);
2117 acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
2118 } else {
2119 acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
2120 }
2121
pbrookce1a14d2006-08-07 02:38:06 +00002122 qemu_bh_schedule(acb->bh);
aliguorif141eaf2009-04-07 18:43:24 +00002123
pbrookce1a14d2006-08-07 02:38:06 +00002124 return &acb->common;
pbrook7a6cba62006-06-04 11:39:07 +00002125}
2126
aliguorif141eaf2009-04-07 18:43:24 +00002127static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
2128 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
pbrookce1a14d2006-08-07 02:38:06 +00002129 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00002130{
aliguorif141eaf2009-04-07 18:43:24 +00002131 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
bellard83f64092006-08-01 16:21:11 +00002132}
2133
aliguorif141eaf2009-04-07 18:43:24 +00002134static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
2135 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2136 BlockDriverCompletionFunc *cb, void *opaque)
2137{
2138 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
2139}
2140
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002141static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
2142 BlockDriverCompletionFunc *cb, void *opaque)
2143{
2144 BlockDriverAIOCBSync *acb;
2145
2146 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2147 acb->is_write = 1; /* don't bounce in the completion hadler */
2148 acb->qiov = NULL;
2149 acb->bounce = NULL;
2150 acb->ret = 0;
2151
2152 if (!acb->bh)
2153 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2154
2155 bdrv_flush(bs);
2156 qemu_bh_schedule(acb->bh);
2157 return &acb->common;
2158}
2159
Alexander Graf016f5cf2010-05-26 17:51:49 +02002160static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,
2161 BlockDriverCompletionFunc *cb, void *opaque)
2162{
2163 BlockDriverAIOCBSync *acb;
2164
2165 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2166 acb->is_write = 1; /* don't bounce in the completion handler */
2167 acb->qiov = NULL;
2168 acb->bounce = NULL;
2169 acb->ret = 0;
2170
2171 if (!acb->bh) {
2172 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2173 }
2174
2175 qemu_bh_schedule(acb->bh);
2176 return &acb->common;
2177}
2178
bellard83f64092006-08-01 16:21:11 +00002179/**************************************************************/
2180/* sync block device emulation */
2181
2182static void bdrv_rw_em_cb(void *opaque, int ret)
2183{
2184 *(int *)opaque = ret;
2185}
2186
2187#define NOT_DONE 0x7fffffff
2188
ths5fafdf22007-09-16 21:08:06 +00002189static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +00002190 uint8_t *buf, int nb_sectors)
2191{
pbrookce1a14d2006-08-07 02:38:06 +00002192 int async_ret;
2193 BlockDriverAIOCB *acb;
aliguorif141eaf2009-04-07 18:43:24 +00002194 struct iovec iov;
2195 QEMUIOVector qiov;
bellard83f64092006-08-01 16:21:11 +00002196
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002197 async_context_push();
2198
bellard83f64092006-08-01 16:21:11 +00002199 async_ret = NOT_DONE;
blueswir13f4cb3d2009-04-13 16:31:01 +00002200 iov.iov_base = (void *)buf;
Jes Sorenseneb5a3162010-05-27 16:20:31 +02002201 iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
aliguorif141eaf2009-04-07 18:43:24 +00002202 qemu_iovec_init_external(&qiov, &iov, 1);
2203 acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
2204 bdrv_rw_em_cb, &async_ret);
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002205 if (acb == NULL) {
2206 async_ret = -1;
2207 goto fail;
2208 }
aliguoribaf35cb2008-09-10 15:45:19 +00002209
bellard83f64092006-08-01 16:21:11 +00002210 while (async_ret == NOT_DONE) {
2211 qemu_aio_wait();
2212 }
aliguoribaf35cb2008-09-10 15:45:19 +00002213
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002214
2215fail:
2216 async_context_pop();
bellard83f64092006-08-01 16:21:11 +00002217 return async_ret;
2218}
2219
2220static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
2221 const uint8_t *buf, int nb_sectors)
2222{
pbrookce1a14d2006-08-07 02:38:06 +00002223 int async_ret;
2224 BlockDriverAIOCB *acb;
aliguorif141eaf2009-04-07 18:43:24 +00002225 struct iovec iov;
2226 QEMUIOVector qiov;
bellard83f64092006-08-01 16:21:11 +00002227
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002228 async_context_push();
2229
bellard83f64092006-08-01 16:21:11 +00002230 async_ret = NOT_DONE;
aliguorif141eaf2009-04-07 18:43:24 +00002231 iov.iov_base = (void *)buf;
Jes Sorenseneb5a3162010-05-27 16:20:31 +02002232 iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
aliguorif141eaf2009-04-07 18:43:24 +00002233 qemu_iovec_init_external(&qiov, &iov, 1);
2234 acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
2235 bdrv_rw_em_cb, &async_ret);
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002236 if (acb == NULL) {
2237 async_ret = -1;
2238 goto fail;
2239 }
bellard83f64092006-08-01 16:21:11 +00002240 while (async_ret == NOT_DONE) {
2241 qemu_aio_wait();
2242 }
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002243
2244fail:
2245 async_context_pop();
bellard83f64092006-08-01 16:21:11 +00002246 return async_ret;
2247}
bellardea2384d2004-08-01 21:59:26 +00002248
2249void bdrv_init(void)
2250{
Anthony Liguori5efa9d52009-05-09 17:03:42 -05002251 module_call_init(MODULE_INIT_BLOCK);
bellardea2384d2004-08-01 21:59:26 +00002252}
pbrookce1a14d2006-08-07 02:38:06 +00002253
Markus Armbrustereb852012009-10-27 18:41:44 +01002254void bdrv_init_with_whitelist(void)
2255{
2256 use_bdrv_whitelist = 1;
2257 bdrv_init();
2258}
2259
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002260void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
2261 BlockDriverCompletionFunc *cb, void *opaque)
aliguori6bbff9a2009-03-20 18:25:59 +00002262{
pbrookce1a14d2006-08-07 02:38:06 +00002263 BlockDriverAIOCB *acb;
2264
aliguori6bbff9a2009-03-20 18:25:59 +00002265 if (pool->free_aiocb) {
2266 acb = pool->free_aiocb;
2267 pool->free_aiocb = acb->next;
pbrookce1a14d2006-08-07 02:38:06 +00002268 } else {
aliguori6bbff9a2009-03-20 18:25:59 +00002269 acb = qemu_mallocz(pool->aiocb_size);
2270 acb->pool = pool;
pbrookce1a14d2006-08-07 02:38:06 +00002271 }
2272 acb->bs = bs;
2273 acb->cb = cb;
2274 acb->opaque = opaque;
2275 return acb;
2276}
2277
2278void qemu_aio_release(void *p)
2279{
aliguori6bbff9a2009-03-20 18:25:59 +00002280 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
2281 AIOPool *pool = acb->pool;
2282 acb->next = pool->free_aiocb;
2283 pool->free_aiocb = acb;
pbrookce1a14d2006-08-07 02:38:06 +00002284}
bellard19cb3732006-08-19 11:45:59 +00002285
2286/**************************************************************/
2287/* removable device support */
2288
2289/**
2290 * Return TRUE if the media is present
2291 */
2292int bdrv_is_inserted(BlockDriverState *bs)
2293{
2294 BlockDriver *drv = bs->drv;
2295 int ret;
2296 if (!drv)
2297 return 0;
2298 if (!drv->bdrv_is_inserted)
2299 return 1;
2300 ret = drv->bdrv_is_inserted(bs);
2301 return ret;
2302}
2303
2304/**
2305 * Return TRUE if the media changed since the last call to this
ths5fafdf22007-09-16 21:08:06 +00002306 * function. It is currently only used for floppy disks
bellard19cb3732006-08-19 11:45:59 +00002307 */
2308int bdrv_media_changed(BlockDriverState *bs)
2309{
2310 BlockDriver *drv = bs->drv;
2311 int ret;
2312
2313 if (!drv || !drv->bdrv_media_changed)
2314 ret = -ENOTSUP;
2315 else
2316 ret = drv->bdrv_media_changed(bs);
2317 if (ret == -ENOTSUP)
2318 ret = bs->media_changed;
2319 bs->media_changed = 0;
2320 return ret;
2321}
2322
2323/**
2324 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
2325 */
Mark McLoughlinaea2a332009-05-27 10:06:11 +01002326int bdrv_eject(BlockDriverState *bs, int eject_flag)
bellard19cb3732006-08-19 11:45:59 +00002327{
2328 BlockDriver *drv = bs->drv;
2329 int ret;
2330
Mark McLoughlinaea2a332009-05-27 10:06:11 +01002331 if (bs->locked) {
2332 return -EBUSY;
2333 }
2334
bellard19cb3732006-08-19 11:45:59 +00002335 if (!drv || !drv->bdrv_eject) {
2336 ret = -ENOTSUP;
2337 } else {
2338 ret = drv->bdrv_eject(bs, eject_flag);
2339 }
2340 if (ret == -ENOTSUP) {
2341 if (eject_flag)
2342 bdrv_close(bs);
Mark McLoughlinaea2a332009-05-27 10:06:11 +01002343 ret = 0;
bellard19cb3732006-08-19 11:45:59 +00002344 }
Mark McLoughlinaea2a332009-05-27 10:06:11 +01002345
2346 return ret;
bellard19cb3732006-08-19 11:45:59 +00002347}
2348
2349int bdrv_is_locked(BlockDriverState *bs)
2350{
2351 return bs->locked;
2352}
2353
2354/**
2355 * Lock or unlock the media (if it is locked, the user won't be able
2356 * to eject it manually).
2357 */
2358void bdrv_set_locked(BlockDriverState *bs, int locked)
2359{
2360 BlockDriver *drv = bs->drv;
2361
2362 bs->locked = locked;
2363 if (drv && drv->bdrv_set_locked) {
2364 drv->bdrv_set_locked(bs, locked);
2365 }
2366}
ths985a03b2007-12-24 16:10:43 +00002367
2368/* needed for generic scsi interface */
2369
2370int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
2371{
2372 BlockDriver *drv = bs->drv;
2373
2374 if (drv && drv->bdrv_ioctl)
2375 return drv->bdrv_ioctl(bs, req, buf);
2376 return -ENOTSUP;
2377}
aliguori7d780662009-03-12 19:57:08 +00002378
aliguori221f7152009-03-28 17:28:41 +00002379BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
2380 unsigned long int req, void *buf,
2381 BlockDriverCompletionFunc *cb, void *opaque)
aliguori7d780662009-03-12 19:57:08 +00002382{
aliguori221f7152009-03-28 17:28:41 +00002383 BlockDriver *drv = bs->drv;
aliguori7d780662009-03-12 19:57:08 +00002384
aliguori221f7152009-03-28 17:28:41 +00002385 if (drv && drv->bdrv_aio_ioctl)
2386 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
2387 return NULL;
aliguori7d780662009-03-12 19:57:08 +00002388}
aliguorie268ca52009-04-22 20:20:00 +00002389
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002390
2391
aliguorie268ca52009-04-22 20:20:00 +00002392void *qemu_blockalign(BlockDriverState *bs, size_t size)
2393{
2394 return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
2395}
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002396
2397void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable)
2398{
2399 int64_t bitmap_size;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002400
Liran Schouraaa0eb72010-01-26 10:31:48 +02002401 bs->dirty_count = 0;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002402 if (enable) {
Jan Kiszkac6d22832009-11-30 18:21:20 +01002403 if (!bs->dirty_bitmap) {
2404 bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
2405 BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
2406 bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002407
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002408 bs->dirty_bitmap = qemu_mallocz(bitmap_size);
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002409 }
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002410 } else {
Jan Kiszkac6d22832009-11-30 18:21:20 +01002411 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002412 qemu_free(bs->dirty_bitmap);
Jan Kiszkac6d22832009-11-30 18:21:20 +01002413 bs->dirty_bitmap = NULL;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002414 }
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002415 }
2416}
2417
2418int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
2419{
Jan Kiszka6ea44302009-11-30 18:21:19 +01002420 int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002421
Jan Kiszkac6d22832009-11-30 18:21:20 +01002422 if (bs->dirty_bitmap &&
2423 (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) {
2424 return bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] &
2425 (1 << (chunk % (sizeof(unsigned long) * 8)));
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002426 } else {
2427 return 0;
2428 }
2429}
2430
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002431void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
2432 int nr_sectors)
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002433{
2434 set_dirty_bitmap(bs, cur_sector, nr_sectors, 0);
2435}
Liran Schouraaa0eb72010-01-26 10:31:48 +02002436
2437int64_t bdrv_get_dirty_count(BlockDriverState *bs)
2438{
2439 return bs->dirty_count;
2440}