blob: df2d3a6e8cd2f77c10305fce8f12b92122dab069 [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
Kevin Wolf08a00552010-06-01 18:37:31 +0200334 /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
335 if (bs->sg || !bdrv_is_inserted(bs)) {
Nicholas A. Bellinger1a396852010-05-27 08:56:28 -0700336 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
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +0900651void bdrv_close_all(void)
652{
653 BlockDriverState *bs;
654
655 QTAILQ_FOREACH(bs, &bdrv_states, list) {
656 bdrv_close(bs);
657 }
658}
659
bellardb3380822004-03-14 21:38:54 +0000660void bdrv_delete(BlockDriverState *bs)
661{
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +0100662 /* remove from list, if necessary */
663 if (bs->device_name[0] != '\0') {
664 QTAILQ_REMOVE(&bdrv_states, bs, list);
665 }
aurel3234c6f052008-04-08 19:51:21 +0000666
bellardb3380822004-03-14 21:38:54 +0000667 bdrv_close(bs);
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200668 if (bs->file != NULL) {
669 bdrv_delete(bs->file);
670 }
671
bellardb3380822004-03-14 21:38:54 +0000672 qemu_free(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000673}
674
aliguorie97fc192009-04-21 23:11:50 +0000675/*
676 * Run consistency checks on an image
677 *
678 * Returns the number of errors or -errno when an internal error occurs
679 */
680int bdrv_check(BlockDriverState *bs)
681{
682 if (bs->drv->bdrv_check == NULL) {
683 return -ENOTSUP;
684 }
685
686 return bs->drv->bdrv_check(bs);
687}
688
bellard33e39632003-07-06 17:15:21 +0000689/* commit COW file into the raw image */
690int bdrv_commit(BlockDriverState *bs)
691{
bellard19cb3732006-08-19 11:45:59 +0000692 BlockDriver *drv = bs->drv;
bellard83f64092006-08-01 16:21:11 +0000693 int64_t i, total_sectors;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200694 int n, j, ro, open_flags;
695 int ret = 0, rw_ret = 0;
Jes Sorenseneb5a3162010-05-27 16:20:31 +0200696 unsigned char sector[BDRV_SECTOR_SIZE];
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200697 char filename[1024];
698 BlockDriverState *bs_rw, *bs_ro;
bellard33e39632003-07-06 17:15:21 +0000699
bellard19cb3732006-08-19 11:45:59 +0000700 if (!drv)
701 return -ENOMEDIUM;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200702
703 if (!bs->backing_hd) {
704 return -ENOTSUP;
bellard33e39632003-07-06 17:15:21 +0000705 }
706
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200707 if (bs->backing_hd->keep_read_only) {
708 return -EACCES;
709 }
710
711 ro = bs->backing_hd->read_only;
712 strncpy(filename, bs->backing_hd->filename, sizeof(filename));
713 open_flags = bs->backing_hd->open_flags;
714
715 if (ro) {
716 /* re-open as RW */
717 bdrv_delete(bs->backing_hd);
718 bs->backing_hd = NULL;
719 bs_rw = bdrv_new("");
Kevin Wolfc3349192010-05-06 16:34:56 +0200720 rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR, drv);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200721 if (rw_ret < 0) {
722 bdrv_delete(bs_rw);
723 /* try to re-open read-only */
724 bs_ro = bdrv_new("");
Kevin Wolfc3349192010-05-06 16:34:56 +0200725 ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, drv);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200726 if (ret < 0) {
727 bdrv_delete(bs_ro);
728 /* drive not functional anymore */
729 bs->drv = NULL;
730 return ret;
731 }
732 bs->backing_hd = bs_ro;
733 return rw_ret;
734 }
735 bs->backing_hd = bs_rw;
bellard33e39632003-07-06 17:15:21 +0000736 }
bellardea2384d2004-08-01 21:59:26 +0000737
Jan Kiszka6ea44302009-11-30 18:21:19 +0100738 total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000739 for (i = 0; i < total_sectors;) {
bellard19cb3732006-08-19 11:45:59 +0000740 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
bellardea2384d2004-08-01 21:59:26 +0000741 for(j = 0; j < n; j++) {
742 if (bdrv_read(bs, i, sector, 1) != 0) {
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200743 ret = -EIO;
744 goto ro_cleanup;
bellardea2384d2004-08-01 21:59:26 +0000745 }
746
747 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200748 ret = -EIO;
749 goto ro_cleanup;
bellardea2384d2004-08-01 21:59:26 +0000750 }
751 i++;
752 }
753 } else {
754 i += n;
755 }
756 }
bellard95389c82005-12-18 18:28:15 +0000757
Christoph Hellwig1d449522010-01-17 12:32:30 +0100758 if (drv->bdrv_make_empty) {
759 ret = drv->bdrv_make_empty(bs);
760 bdrv_flush(bs);
761 }
bellard95389c82005-12-18 18:28:15 +0000762
Christoph Hellwig3f5075a2010-01-12 13:49:23 +0100763 /*
764 * Make sure all data we wrote to the backing device is actually
765 * stable on disk.
766 */
767 if (bs->backing_hd)
768 bdrv_flush(bs->backing_hd);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200769
770ro_cleanup:
771
772 if (ro) {
773 /* re-open as RO */
774 bdrv_delete(bs->backing_hd);
775 bs->backing_hd = NULL;
776 bs_ro = bdrv_new("");
Kevin Wolfc3349192010-05-06 16:34:56 +0200777 ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, drv);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200778 if (ret < 0) {
779 bdrv_delete(bs_ro);
780 /* drive not functional anymore */
781 bs->drv = NULL;
782 return ret;
783 }
784 bs->backing_hd = bs_ro;
785 bs->backing_hd->keep_read_only = 0;
786 }
787
Christoph Hellwig1d449522010-01-17 12:32:30 +0100788 return ret;
bellard33e39632003-07-06 17:15:21 +0000789}
790
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200791void bdrv_commit_all(void)
792{
793 BlockDriverState *bs;
794
795 QTAILQ_FOREACH(bs, &bdrv_states, list) {
796 bdrv_commit(bs);
797 }
798}
799
Kevin Wolf756e6732010-01-12 12:55:17 +0100800/*
801 * Return values:
802 * 0 - success
803 * -EINVAL - backing format specified, but no file
804 * -ENOSPC - can't update the backing file because no space is left in the
805 * image file header
806 * -ENOTSUP - format driver doesn't support changing the backing file
807 */
808int bdrv_change_backing_file(BlockDriverState *bs,
809 const char *backing_file, const char *backing_fmt)
810{
811 BlockDriver *drv = bs->drv;
812
813 if (drv->bdrv_change_backing_file != NULL) {
814 return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
815 } else {
816 return -ENOTSUP;
817 }
818}
819
aliguori71d07702009-03-03 17:37:16 +0000820static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
821 size_t size)
822{
823 int64_t len;
824
825 if (!bdrv_is_inserted(bs))
826 return -ENOMEDIUM;
827
828 if (bs->growable)
829 return 0;
830
831 len = bdrv_getlength(bs);
832
Kevin Wolffbb7b4e2009-05-08 14:47:24 +0200833 if (offset < 0)
834 return -EIO;
835
836 if ((offset > len) || (len - offset < size))
aliguori71d07702009-03-03 17:37:16 +0000837 return -EIO;
838
839 return 0;
840}
841
842static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
843 int nb_sectors)
844{
Jes Sorenseneb5a3162010-05-27 16:20:31 +0200845 return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE,
846 nb_sectors * BDRV_SECTOR_SIZE);
aliguori71d07702009-03-03 17:37:16 +0000847}
848
bellard19cb3732006-08-19 11:45:59 +0000849/* return < 0 if error. See bdrv_write() for the return codes */
ths5fafdf22007-09-16 21:08:06 +0000850int bdrv_read(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000851 uint8_t *buf, int nb_sectors)
852{
bellardea2384d2004-08-01 21:59:26 +0000853 BlockDriver *drv = bs->drv;
854
bellard19cb3732006-08-19 11:45:59 +0000855 if (!drv)
856 return -ENOMEDIUM;
aliguori71d07702009-03-03 17:37:16 +0000857 if (bdrv_check_request(bs, sector_num, nb_sectors))
858 return -EIO;
bellardb3380822004-03-14 21:38:54 +0000859
aliguorieda578e2009-03-12 19:57:16 +0000860 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
bellardfc01f7e2003-06-30 10:03:06 +0000861}
862
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +0200863static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num,
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100864 int nb_sectors, int dirty)
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +0200865{
866 int64_t start, end;
Jan Kiszkac6d22832009-11-30 18:21:20 +0100867 unsigned long val, idx, bit;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100868
Jan Kiszka6ea44302009-11-30 18:21:19 +0100869 start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkac6d22832009-11-30 18:21:20 +0100870 end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100871
872 for (; start <= end; start++) {
Jan Kiszkac6d22832009-11-30 18:21:20 +0100873 idx = start / (sizeof(unsigned long) * 8);
874 bit = start % (sizeof(unsigned long) * 8);
875 val = bs->dirty_bitmap[idx];
876 if (dirty) {
Liran Schouraaa0eb72010-01-26 10:31:48 +0200877 if (!(val & (1 << bit))) {
878 bs->dirty_count++;
879 val |= 1 << bit;
880 }
Jan Kiszkac6d22832009-11-30 18:21:20 +0100881 } else {
Liran Schouraaa0eb72010-01-26 10:31:48 +0200882 if (val & (1 << bit)) {
883 bs->dirty_count--;
884 val &= ~(1 << bit);
885 }
Jan Kiszkac6d22832009-11-30 18:21:20 +0100886 }
887 bs->dirty_bitmap[idx] = val;
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +0200888 }
889}
890
ths5fafdf22007-09-16 21:08:06 +0000891/* Return < 0 if error. Important errors are:
bellard19cb3732006-08-19 11:45:59 +0000892 -EIO generic I/O error (may happen for all errors)
893 -ENOMEDIUM No media inserted.
894 -EINVAL Invalid sector number or nb_sectors
895 -EACCES Trying to write a read-only device
896*/
ths5fafdf22007-09-16 21:08:06 +0000897int bdrv_write(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000898 const uint8_t *buf, int nb_sectors)
899{
bellard83f64092006-08-01 16:21:11 +0000900 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +0000901 if (!bs->drv)
902 return -ENOMEDIUM;
bellard0849bf02003-06-30 23:17:31 +0000903 if (bs->read_only)
bellard19cb3732006-08-19 11:45:59 +0000904 return -EACCES;
aliguori71d07702009-03-03 17:37:16 +0000905 if (bdrv_check_request(bs, sector_num, nb_sectors))
906 return -EIO;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100907
Jan Kiszkac6d22832009-11-30 18:21:20 +0100908 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +0200909 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
910 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100911
Kevin Wolf294cc352010-04-28 14:34:01 +0200912 if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
913 bs->wr_highest_sector = sector_num + nb_sectors - 1;
914 }
915
aliguori42fb2802009-01-15 20:43:39 +0000916 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
bellard83f64092006-08-01 16:21:11 +0000917}
918
aliguorieda578e2009-03-12 19:57:16 +0000919int bdrv_pread(BlockDriverState *bs, int64_t offset,
920 void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000921{
Jan Kiszka6ea44302009-11-30 18:21:19 +0100922 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
bellard83f64092006-08-01 16:21:11 +0000923 int len, nb_sectors, count;
924 int64_t sector_num;
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100925 int ret;
bellard83f64092006-08-01 16:21:11 +0000926
927 count = count1;
928 /* first read to align to sector start */
Jan Kiszka6ea44302009-11-30 18:21:19 +0100929 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
bellard83f64092006-08-01 16:21:11 +0000930 if (len > count)
931 len = count;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100932 sector_num = offset >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000933 if (len > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100934 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
935 return ret;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100936 memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
bellard83f64092006-08-01 16:21:11 +0000937 count -= len;
938 if (count == 0)
939 return count1;
940 sector_num++;
941 buf += len;
942 }
943
944 /* read the sectors "in place" */
Jan Kiszka6ea44302009-11-30 18:21:19 +0100945 nb_sectors = count >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000946 if (nb_sectors > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100947 if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0)
948 return ret;
bellard83f64092006-08-01 16:21:11 +0000949 sector_num += nb_sectors;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100950 len = nb_sectors << BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000951 buf += len;
952 count -= len;
953 }
954
955 /* add data from the last sector */
956 if (count > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100957 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
958 return ret;
bellard83f64092006-08-01 16:21:11 +0000959 memcpy(buf, tmp_buf, count);
960 }
961 return count1;
962}
963
aliguorieda578e2009-03-12 19:57:16 +0000964int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
965 const void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000966{
Jan Kiszka6ea44302009-11-30 18:21:19 +0100967 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
bellard83f64092006-08-01 16:21:11 +0000968 int len, nb_sectors, count;
969 int64_t sector_num;
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100970 int ret;
bellard83f64092006-08-01 16:21:11 +0000971
972 count = count1;
973 /* first write to align to sector start */
Jan Kiszka6ea44302009-11-30 18:21:19 +0100974 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
bellard83f64092006-08-01 16:21:11 +0000975 if (len > count)
976 len = count;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100977 sector_num = offset >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000978 if (len > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100979 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
980 return ret;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100981 memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len);
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100982 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
983 return ret;
bellard83f64092006-08-01 16:21:11 +0000984 count -= len;
985 if (count == 0)
986 return count1;
987 sector_num++;
988 buf += len;
989 }
990
991 /* write the sectors "in place" */
Jan Kiszka6ea44302009-11-30 18:21:19 +0100992 nb_sectors = count >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000993 if (nb_sectors > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100994 if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0)
995 return ret;
bellard83f64092006-08-01 16:21:11 +0000996 sector_num += nb_sectors;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100997 len = nb_sectors << BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000998 buf += len;
999 count -= len;
1000 }
1001
1002 /* add data from the last sector */
1003 if (count > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001004 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1005 return ret;
bellard83f64092006-08-01 16:21:11 +00001006 memcpy(tmp_buf, buf, count);
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001007 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
1008 return ret;
bellard83f64092006-08-01 16:21:11 +00001009 }
1010 return count1;
1011}
bellard83f64092006-08-01 16:21:11 +00001012
1013/**
bellard83f64092006-08-01 16:21:11 +00001014 * Truncate file to 'offset' bytes (needed only for file protocols)
1015 */
1016int bdrv_truncate(BlockDriverState *bs, int64_t offset)
1017{
1018 BlockDriver *drv = bs->drv;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001019 int ret;
bellard83f64092006-08-01 16:21:11 +00001020 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001021 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +00001022 if (!drv->bdrv_truncate)
1023 return -ENOTSUP;
Naphtali Sprei59f26892009-10-26 16:25:16 +02001024 if (bs->read_only)
1025 return -EACCES;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001026 ret = drv->bdrv_truncate(bs, offset);
1027 if (ret == 0) {
1028 ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
1029 }
1030 return ret;
bellard83f64092006-08-01 16:21:11 +00001031}
1032
1033/**
1034 * Length of a file in bytes. Return < 0 if error or unknown.
1035 */
1036int64_t bdrv_getlength(BlockDriverState *bs)
1037{
1038 BlockDriver *drv = bs->drv;
1039 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001040 return -ENOMEDIUM;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001041
1042 /* Fixed size devices use the total_sectors value for speed instead of
1043 issuing a length query (like lseek) on each call. Also, legacy block
1044 drivers don't provide a bdrv_getlength function and must use
1045 total_sectors. */
1046 if (!bs->growable || !drv->bdrv_getlength) {
Jan Kiszka6ea44302009-11-30 18:21:19 +01001047 return bs->total_sectors * BDRV_SECTOR_SIZE;
bellard83f64092006-08-01 16:21:11 +00001048 }
1049 return drv->bdrv_getlength(bs);
bellardfc01f7e2003-06-30 10:03:06 +00001050}
1051
bellard19cb3732006-08-19 11:45:59 +00001052/* return 0 as number of sectors if no device present or error */
ths96b8f132007-12-17 01:35:20 +00001053void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
bellardfc01f7e2003-06-30 10:03:06 +00001054{
bellard19cb3732006-08-19 11:45:59 +00001055 int64_t length;
1056 length = bdrv_getlength(bs);
1057 if (length < 0)
1058 length = 0;
1059 else
Jan Kiszka6ea44302009-11-30 18:21:19 +01001060 length = length >> BDRV_SECTOR_BITS;
bellard19cb3732006-08-19 11:45:59 +00001061 *nb_sectors_ptr = length;
bellardfc01f7e2003-06-30 10:03:06 +00001062}
bellardcf989512004-02-16 21:56:36 +00001063
aliguorif3d54fc2008-11-25 21:50:24 +00001064struct partition {
1065 uint8_t boot_ind; /* 0x80 - active */
1066 uint8_t head; /* starting head */
1067 uint8_t sector; /* starting sector */
1068 uint8_t cyl; /* starting cylinder */
1069 uint8_t sys_ind; /* What partition type */
1070 uint8_t end_head; /* end head */
1071 uint8_t end_sector; /* end sector */
1072 uint8_t end_cyl; /* end cylinder */
1073 uint32_t start_sect; /* starting sector counting from 0 */
1074 uint32_t nr_sects; /* nr of sectors in partition */
1075} __attribute__((packed));
1076
1077/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
1078static int guess_disk_lchs(BlockDriverState *bs,
1079 int *pcylinders, int *pheads, int *psectors)
1080{
Jes Sorenseneb5a3162010-05-27 16:20:31 +02001081 uint8_t buf[BDRV_SECTOR_SIZE];
aliguorif3d54fc2008-11-25 21:50:24 +00001082 int ret, i, heads, sectors, cylinders;
1083 struct partition *p;
1084 uint32_t nr_sects;
blueswir1a38131b2008-12-05 17:56:40 +00001085 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +00001086
1087 bdrv_get_geometry(bs, &nb_sectors);
1088
1089 ret = bdrv_read(bs, 0, buf, 1);
1090 if (ret < 0)
1091 return -1;
1092 /* test msdos magic */
1093 if (buf[510] != 0x55 || buf[511] != 0xaa)
1094 return -1;
1095 for(i = 0; i < 4; i++) {
1096 p = ((struct partition *)(buf + 0x1be)) + i;
1097 nr_sects = le32_to_cpu(p->nr_sects);
1098 if (nr_sects && p->end_head) {
1099 /* We make the assumption that the partition terminates on
1100 a cylinder boundary */
1101 heads = p->end_head + 1;
1102 sectors = p->end_sector & 63;
1103 if (sectors == 0)
1104 continue;
1105 cylinders = nb_sectors / (heads * sectors);
1106 if (cylinders < 1 || cylinders > 16383)
1107 continue;
1108 *pheads = heads;
1109 *psectors = sectors;
1110 *pcylinders = cylinders;
1111#if 0
1112 printf("guessed geometry: LCHS=%d %d %d\n",
1113 cylinders, heads, sectors);
1114#endif
1115 return 0;
1116 }
1117 }
1118 return -1;
1119}
1120
1121void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
1122{
1123 int translation, lba_detected = 0;
1124 int cylinders, heads, secs;
blueswir1a38131b2008-12-05 17:56:40 +00001125 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +00001126
1127 /* if a geometry hint is available, use it */
1128 bdrv_get_geometry(bs, &nb_sectors);
1129 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
1130 translation = bdrv_get_translation_hint(bs);
1131 if (cylinders != 0) {
1132 *pcyls = cylinders;
1133 *pheads = heads;
1134 *psecs = secs;
1135 } else {
1136 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
1137 if (heads > 16) {
1138 /* if heads > 16, it means that a BIOS LBA
1139 translation was active, so the default
1140 hardware geometry is OK */
1141 lba_detected = 1;
1142 goto default_geometry;
1143 } else {
1144 *pcyls = cylinders;
1145 *pheads = heads;
1146 *psecs = secs;
1147 /* disable any translation to be in sync with
1148 the logical geometry */
1149 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
1150 bdrv_set_translation_hint(bs,
1151 BIOS_ATA_TRANSLATION_NONE);
1152 }
1153 }
1154 } else {
1155 default_geometry:
1156 /* if no geometry, use a standard physical disk geometry */
1157 cylinders = nb_sectors / (16 * 63);
1158
1159 if (cylinders > 16383)
1160 cylinders = 16383;
1161 else if (cylinders < 2)
1162 cylinders = 2;
1163 *pcyls = cylinders;
1164 *pheads = 16;
1165 *psecs = 63;
1166 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
1167 if ((*pcyls * *pheads) <= 131072) {
1168 bdrv_set_translation_hint(bs,
1169 BIOS_ATA_TRANSLATION_LARGE);
1170 } else {
1171 bdrv_set_translation_hint(bs,
1172 BIOS_ATA_TRANSLATION_LBA);
1173 }
1174 }
1175 }
1176 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
1177 }
1178}
1179
ths5fafdf22007-09-16 21:08:06 +00001180void bdrv_set_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +00001181 int cyls, int heads, int secs)
1182{
1183 bs->cyls = cyls;
1184 bs->heads = heads;
1185 bs->secs = secs;
1186}
1187
1188void bdrv_set_type_hint(BlockDriverState *bs, int type)
1189{
1190 bs->type = type;
1191 bs->removable = ((type == BDRV_TYPE_CDROM ||
1192 type == BDRV_TYPE_FLOPPY));
1193}
1194
bellard46d47672004-11-16 01:45:27 +00001195void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
1196{
1197 bs->translation = translation;
1198}
1199
ths5fafdf22007-09-16 21:08:06 +00001200void bdrv_get_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +00001201 int *pcyls, int *pheads, int *psecs)
1202{
1203 *pcyls = bs->cyls;
1204 *pheads = bs->heads;
1205 *psecs = bs->secs;
1206}
1207
1208int bdrv_get_type_hint(BlockDriverState *bs)
1209{
1210 return bs->type;
1211}
1212
bellard46d47672004-11-16 01:45:27 +00001213int bdrv_get_translation_hint(BlockDriverState *bs)
1214{
1215 return bs->translation;
1216}
1217
Markus Armbrusterabd7f682010-06-02 18:55:17 +02001218void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error,
1219 BlockErrorAction on_write_error)
1220{
1221 bs->on_read_error = on_read_error;
1222 bs->on_write_error = on_write_error;
1223}
1224
1225BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read)
1226{
1227 return is_read ? bs->on_read_error : bs->on_write_error;
1228}
1229
bellardb3380822004-03-14 21:38:54 +00001230int bdrv_is_removable(BlockDriverState *bs)
1231{
1232 return bs->removable;
1233}
1234
1235int bdrv_is_read_only(BlockDriverState *bs)
1236{
1237 return bs->read_only;
1238}
1239
ths985a03b2007-12-24 16:10:43 +00001240int bdrv_is_sg(BlockDriverState *bs)
1241{
1242 return bs->sg;
1243}
1244
Christoph Hellwige900a7b2009-09-04 19:01:15 +02001245int bdrv_enable_write_cache(BlockDriverState *bs)
1246{
1247 return bs->enable_write_cache;
1248}
1249
bellard19cb3732006-08-19 11:45:59 +00001250/* XXX: no longer used */
ths5fafdf22007-09-16 21:08:06 +00001251void bdrv_set_change_cb(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +00001252 void (*change_cb)(void *opaque), void *opaque)
1253{
1254 bs->change_cb = change_cb;
1255 bs->change_opaque = opaque;
1256}
1257
bellardea2384d2004-08-01 21:59:26 +00001258int bdrv_is_encrypted(BlockDriverState *bs)
1259{
1260 if (bs->backing_hd && bs->backing_hd->encrypted)
1261 return 1;
1262 return bs->encrypted;
1263}
1264
aliguoric0f4ce72009-03-05 23:01:01 +00001265int bdrv_key_required(BlockDriverState *bs)
1266{
1267 BlockDriverState *backing_hd = bs->backing_hd;
1268
1269 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
1270 return 1;
1271 return (bs->encrypted && !bs->valid_key);
1272}
1273
bellardea2384d2004-08-01 21:59:26 +00001274int bdrv_set_key(BlockDriverState *bs, const char *key)
1275{
1276 int ret;
1277 if (bs->backing_hd && bs->backing_hd->encrypted) {
1278 ret = bdrv_set_key(bs->backing_hd, key);
1279 if (ret < 0)
1280 return ret;
1281 if (!bs->encrypted)
1282 return 0;
1283 }
Shahar Havivifd04a2a2010-03-06 00:26:13 +02001284 if (!bs->encrypted) {
1285 return -EINVAL;
1286 } else if (!bs->drv || !bs->drv->bdrv_set_key) {
1287 return -ENOMEDIUM;
1288 }
aliguoric0f4ce72009-03-05 23:01:01 +00001289 ret = bs->drv->bdrv_set_key(bs, key);
aliguoribb5fc202009-03-05 23:01:15 +00001290 if (ret < 0) {
1291 bs->valid_key = 0;
1292 } else if (!bs->valid_key) {
1293 bs->valid_key = 1;
1294 /* call the change callback now, we skipped it on open */
1295 bs->media_changed = 1;
1296 if (bs->change_cb)
1297 bs->change_cb(bs->change_opaque);
1298 }
aliguoric0f4ce72009-03-05 23:01:01 +00001299 return ret;
bellardea2384d2004-08-01 21:59:26 +00001300}
1301
1302void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
1303{
bellard19cb3732006-08-19 11:45:59 +00001304 if (!bs->drv) {
bellardea2384d2004-08-01 21:59:26 +00001305 buf[0] = '\0';
1306 } else {
1307 pstrcpy(buf, buf_size, bs->drv->format_name);
1308 }
1309}
1310
ths5fafdf22007-09-16 21:08:06 +00001311void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
bellardea2384d2004-08-01 21:59:26 +00001312 void *opaque)
1313{
1314 BlockDriver *drv;
1315
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +01001316 QLIST_FOREACH(drv, &bdrv_drivers, list) {
bellardea2384d2004-08-01 21:59:26 +00001317 it(opaque, drv->format_name);
1318 }
1319}
1320
bellardb3380822004-03-14 21:38:54 +00001321BlockDriverState *bdrv_find(const char *name)
1322{
1323 BlockDriverState *bs;
1324
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001325 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1326 if (!strcmp(name, bs->device_name)) {
bellardb3380822004-03-14 21:38:54 +00001327 return bs;
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001328 }
bellardb3380822004-03-14 21:38:54 +00001329 }
1330 return NULL;
1331}
1332
aliguori51de9762009-03-05 23:00:43 +00001333void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
bellard81d09122004-07-14 17:21:37 +00001334{
1335 BlockDriverState *bs;
1336
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001337 QTAILQ_FOREACH(bs, &bdrv_states, list) {
aliguori51de9762009-03-05 23:00:43 +00001338 it(opaque, bs);
bellard81d09122004-07-14 17:21:37 +00001339 }
1340}
1341
bellardea2384d2004-08-01 21:59:26 +00001342const char *bdrv_get_device_name(BlockDriverState *bs)
1343{
1344 return bs->device_name;
1345}
1346
pbrook7a6cba62006-06-04 11:39:07 +00001347void bdrv_flush(BlockDriverState *bs)
1348{
Alexander Graf016f5cf2010-05-26 17:51:49 +02001349 if (bs->open_flags & BDRV_O_NO_FLUSH) {
1350 return;
1351 }
1352
Christoph Hellwig3f5075a2010-01-12 13:49:23 +01001353 if (bs->drv && bs->drv->bdrv_flush)
pbrook7a6cba62006-06-04 11:39:07 +00001354 bs->drv->bdrv_flush(bs);
pbrook7a6cba62006-06-04 11:39:07 +00001355}
1356
aliguoric6ca28d2008-10-06 13:55:43 +00001357void bdrv_flush_all(void)
1358{
1359 BlockDriverState *bs;
1360
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001361 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1362 if (bs->drv && !bdrv_is_read_only(bs) &&
1363 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) {
aliguoric6ca28d2008-10-06 13:55:43 +00001364 bdrv_flush(bs);
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001365 }
1366 }
aliguoric6ca28d2008-10-06 13:55:43 +00001367}
1368
Kevin Wolff2feebb2010-04-14 17:30:35 +02001369int bdrv_has_zero_init(BlockDriverState *bs)
1370{
1371 assert(bs->drv);
1372
1373 if (bs->drv->no_zero_init) {
1374 return 0;
1375 } else if (bs->file) {
1376 return bdrv_has_zero_init(bs->file);
1377 }
1378
1379 return 1;
1380}
1381
thsf58c7b32008-06-05 21:53:49 +00001382/*
1383 * Returns true iff the specified sector is present in the disk image. Drivers
1384 * not implementing the functionality are assumed to not support backing files,
1385 * hence all their sectors are reported as allocated.
1386 *
1387 * 'pnum' is set to the number of sectors (including and immediately following
1388 * the specified sector) that are known to be in the same
1389 * allocated/unallocated state.
1390 *
1391 * 'nb_sectors' is the max value 'pnum' should be set to.
1392 */
1393int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1394 int *pnum)
1395{
1396 int64_t n;
1397 if (!bs->drv->bdrv_is_allocated) {
1398 if (sector_num >= bs->total_sectors) {
1399 *pnum = 0;
1400 return 0;
1401 }
1402 n = bs->total_sectors - sector_num;
1403 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1404 return 1;
1405 }
1406 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1407}
1408
Luiz Capitulino2582bfe2010-02-03 12:41:01 -02001409void bdrv_mon_event(const BlockDriverState *bdrv,
1410 BlockMonEventAction action, int is_read)
1411{
1412 QObject *data;
1413 const char *action_str;
1414
1415 switch (action) {
1416 case BDRV_ACTION_REPORT:
1417 action_str = "report";
1418 break;
1419 case BDRV_ACTION_IGNORE:
1420 action_str = "ignore";
1421 break;
1422 case BDRV_ACTION_STOP:
1423 action_str = "stop";
1424 break;
1425 default:
1426 abort();
1427 }
1428
1429 data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
1430 bdrv->device_name,
1431 action_str,
1432 is_read ? "read" : "write");
1433 monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data);
1434
1435 qobject_decref(data);
1436}
1437
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001438static void bdrv_print_dict(QObject *obj, void *opaque)
bellardb3380822004-03-14 21:38:54 +00001439{
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001440 QDict *bs_dict;
1441 Monitor *mon = opaque;
1442
1443 bs_dict = qobject_to_qdict(obj);
1444
1445 monitor_printf(mon, "%s: type=%s removable=%d",
1446 qdict_get_str(bs_dict, "device"),
1447 qdict_get_str(bs_dict, "type"),
1448 qdict_get_bool(bs_dict, "removable"));
1449
1450 if (qdict_get_bool(bs_dict, "removable")) {
1451 monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked"));
1452 }
1453
1454 if (qdict_haskey(bs_dict, "inserted")) {
1455 QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted"));
1456
1457 monitor_printf(mon, " file=");
1458 monitor_print_filename(mon, qdict_get_str(qdict, "file"));
1459 if (qdict_haskey(qdict, "backing_file")) {
1460 monitor_printf(mon, " backing_file=");
1461 monitor_print_filename(mon, qdict_get_str(qdict, "backing_file"));
1462 }
1463 monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
1464 qdict_get_bool(qdict, "ro"),
1465 qdict_get_str(qdict, "drv"),
1466 qdict_get_bool(qdict, "encrypted"));
1467 } else {
1468 monitor_printf(mon, " [not inserted]");
1469 }
1470
1471 monitor_printf(mon, "\n");
1472}
1473
1474void bdrv_info_print(Monitor *mon, const QObject *data)
1475{
1476 qlist_iter(qobject_to_qlist(data), bdrv_print_dict, mon);
1477}
1478
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001479void bdrv_info(Monitor *mon, QObject **ret_data)
1480{
1481 QList *bs_list;
bellardb3380822004-03-14 21:38:54 +00001482 BlockDriverState *bs;
1483
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001484 bs_list = qlist_new();
1485
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001486 QTAILQ_FOREACH(bs, &bdrv_states, list) {
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001487 QObject *bs_obj;
1488 const char *type = "unknown";
1489
bellardb3380822004-03-14 21:38:54 +00001490 switch(bs->type) {
1491 case BDRV_TYPE_HD:
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001492 type = "hd";
bellardb3380822004-03-14 21:38:54 +00001493 break;
1494 case BDRV_TYPE_CDROM:
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001495 type = "cdrom";
bellardb3380822004-03-14 21:38:54 +00001496 break;
1497 case BDRV_TYPE_FLOPPY:
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001498 type = "floppy";
bellardb3380822004-03-14 21:38:54 +00001499 break;
1500 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001501
1502 bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': %s, "
1503 "'removable': %i, 'locked': %i }",
1504 bs->device_name, type, bs->removable,
1505 bs->locked);
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001506
bellard19cb3732006-08-19 11:45:59 +00001507 if (bs->drv) {
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001508 QObject *obj;
1509 QDict *bs_dict = qobject_to_qdict(bs_obj);
1510
1511 obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, "
1512 "'encrypted': %i }",
1513 bs->filename, bs->read_only,
1514 bs->drv->format_name,
1515 bdrv_is_encrypted(bs));
thsfef30742006-12-22 14:11:32 +00001516 if (bs->backing_file[0] != '\0') {
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001517 QDict *qdict = qobject_to_qdict(obj);
1518 qdict_put(qdict, "backing_file",
1519 qstring_from_str(bs->backing_file));
aliguori376253e2009-03-05 23:01:23 +00001520 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001521
1522 qdict_put_obj(bs_dict, "inserted", obj);
bellardb3380822004-03-14 21:38:54 +00001523 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001524 qlist_append_obj(bs_list, bs_obj);
bellardb3380822004-03-14 21:38:54 +00001525 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001526
1527 *ret_data = QOBJECT(bs_list);
bellardb3380822004-03-14 21:38:54 +00001528}
thsa36e69d2007-12-02 05:18:19 +00001529
Luiz Capitulino218a5362009-12-10 17:16:07 -02001530static void bdrv_stats_iter(QObject *data, void *opaque)
thsa36e69d2007-12-02 05:18:19 +00001531{
Luiz Capitulino218a5362009-12-10 17:16:07 -02001532 QDict *qdict;
1533 Monitor *mon = opaque;
1534
1535 qdict = qobject_to_qdict(data);
1536 monitor_printf(mon, "%s:", qdict_get_str(qdict, "device"));
1537
1538 qdict = qobject_to_qdict(qdict_get(qdict, "stats"));
1539 monitor_printf(mon, " rd_bytes=%" PRId64
1540 " wr_bytes=%" PRId64
1541 " rd_operations=%" PRId64
1542 " wr_operations=%" PRId64
1543 "\n",
1544 qdict_get_int(qdict, "rd_bytes"),
1545 qdict_get_int(qdict, "wr_bytes"),
1546 qdict_get_int(qdict, "rd_operations"),
1547 qdict_get_int(qdict, "wr_operations"));
1548}
1549
1550void bdrv_stats_print(Monitor *mon, const QObject *data)
1551{
1552 qlist_iter(qobject_to_qlist(data), bdrv_stats_iter, mon);
1553}
1554
Kevin Wolf294cc352010-04-28 14:34:01 +02001555static QObject* bdrv_info_stats_bs(BlockDriverState *bs)
1556{
1557 QObject *res;
1558 QDict *dict;
1559
1560 res = qobject_from_jsonf("{ 'stats': {"
1561 "'rd_bytes': %" PRId64 ","
1562 "'wr_bytes': %" PRId64 ","
1563 "'rd_operations': %" PRId64 ","
1564 "'wr_operations': %" PRId64 ","
1565 "'wr_highest_offset': %" PRId64
1566 "} }",
1567 bs->rd_bytes, bs->wr_bytes,
1568 bs->rd_ops, bs->wr_ops,
Jes Sorenseneb5a3162010-05-27 16:20:31 +02001569 bs->wr_highest_sector * (long)BDRV_SECTOR_SIZE);
Kevin Wolf294cc352010-04-28 14:34:01 +02001570 dict = qobject_to_qdict(res);
1571
1572 if (*bs->device_name) {
1573 qdict_put(dict, "device", qstring_from_str(bs->device_name));
1574 }
1575
1576 if (bs->file) {
1577 QObject *parent = bdrv_info_stats_bs(bs->file);
1578 qdict_put_obj(dict, "parent", parent);
1579 }
1580
1581 return res;
1582}
1583
Luiz Capitulino218a5362009-12-10 17:16:07 -02001584void bdrv_info_stats(Monitor *mon, QObject **ret_data)
1585{
1586 QObject *obj;
1587 QList *devices;
thsa36e69d2007-12-02 05:18:19 +00001588 BlockDriverState *bs;
1589
Luiz Capitulino218a5362009-12-10 17:16:07 -02001590 devices = qlist_new();
1591
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001592 QTAILQ_FOREACH(bs, &bdrv_states, list) {
Kevin Wolf294cc352010-04-28 14:34:01 +02001593 obj = bdrv_info_stats_bs(bs);
Luiz Capitulino218a5362009-12-10 17:16:07 -02001594 qlist_append_obj(devices, obj);
thsa36e69d2007-12-02 05:18:19 +00001595 }
Luiz Capitulino218a5362009-12-10 17:16:07 -02001596
1597 *ret_data = QOBJECT(devices);
thsa36e69d2007-12-02 05:18:19 +00001598}
bellardea2384d2004-08-01 21:59:26 +00001599
aliguori045df332009-03-05 23:00:48 +00001600const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1601{
1602 if (bs->backing_hd && bs->backing_hd->encrypted)
1603 return bs->backing_file;
1604 else if (bs->encrypted)
1605 return bs->filename;
1606 else
1607 return NULL;
1608}
1609
ths5fafdf22007-09-16 21:08:06 +00001610void bdrv_get_backing_filename(BlockDriverState *bs,
bellard83f64092006-08-01 16:21:11 +00001611 char *filename, int filename_size)
bellardea2384d2004-08-01 21:59:26 +00001612{
Kevin Wolfb783e402010-01-12 12:55:16 +01001613 if (!bs->backing_file) {
bellard83f64092006-08-01 16:21:11 +00001614 pstrcpy(filename, filename_size, "");
1615 } else {
1616 pstrcpy(filename, filename_size, bs->backing_file);
1617 }
bellardea2384d2004-08-01 21:59:26 +00001618}
1619
ths5fafdf22007-09-16 21:08:06 +00001620int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
bellardfaea38e2006-08-05 21:31:00 +00001621 const uint8_t *buf, int nb_sectors)
1622{
1623 BlockDriver *drv = bs->drv;
1624 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001625 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001626 if (!drv->bdrv_write_compressed)
1627 return -ENOTSUP;
Kevin Wolffbb7b4e2009-05-08 14:47:24 +02001628 if (bdrv_check_request(bs, sector_num, nb_sectors))
1629 return -EIO;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001630
Jan Kiszkac6d22832009-11-30 18:21:20 +01001631 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001632 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1633 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001634
bellardfaea38e2006-08-05 21:31:00 +00001635 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1636}
ths3b46e622007-09-17 08:09:54 +00001637
bellardfaea38e2006-08-05 21:31:00 +00001638int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1639{
1640 BlockDriver *drv = bs->drv;
1641 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001642 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001643 if (!drv->bdrv_get_info)
1644 return -ENOTSUP;
1645 memset(bdi, 0, sizeof(*bdi));
1646 return drv->bdrv_get_info(bs, bdi);
1647}
1648
Christoph Hellwig45566e92009-07-10 23:11:57 +02001649int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
1650 int64_t pos, int size)
aliguori178e08a2009-04-05 19:10:55 +00001651{
1652 BlockDriver *drv = bs->drv;
1653 if (!drv)
1654 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09001655 if (drv->bdrv_save_vmstate)
1656 return drv->bdrv_save_vmstate(bs, buf, pos, size);
1657 if (bs->file)
1658 return bdrv_save_vmstate(bs->file, buf, pos, size);
1659 return -ENOTSUP;
aliguori178e08a2009-04-05 19:10:55 +00001660}
1661
Christoph Hellwig45566e92009-07-10 23:11:57 +02001662int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1663 int64_t pos, int size)
aliguori178e08a2009-04-05 19:10:55 +00001664{
1665 BlockDriver *drv = bs->drv;
1666 if (!drv)
1667 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09001668 if (drv->bdrv_load_vmstate)
1669 return drv->bdrv_load_vmstate(bs, buf, pos, size);
1670 if (bs->file)
1671 return bdrv_load_vmstate(bs->file, buf, pos, size);
1672 return -ENOTSUP;
aliguori178e08a2009-04-05 19:10:55 +00001673}
1674
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01001675void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
1676{
1677 BlockDriver *drv = bs->drv;
1678
1679 if (!drv || !drv->bdrv_debug_event) {
1680 return;
1681 }
1682
1683 return drv->bdrv_debug_event(bs, event);
1684
1685}
1686
bellardfaea38e2006-08-05 21:31:00 +00001687/**************************************************************/
1688/* handling of snapshots */
1689
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03001690int bdrv_can_snapshot(BlockDriverState *bs)
1691{
1692 BlockDriver *drv = bs->drv;
1693 if (!drv || bdrv_is_removable(bs) || bdrv_is_read_only(bs)) {
1694 return 0;
1695 }
1696
1697 if (!drv->bdrv_snapshot_create) {
1698 if (bs->file != NULL) {
1699 return bdrv_can_snapshot(bs->file);
1700 }
1701 return 0;
1702 }
1703
1704 return 1;
1705}
1706
ths5fafdf22007-09-16 21:08:06 +00001707int bdrv_snapshot_create(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001708 QEMUSnapshotInfo *sn_info)
1709{
1710 BlockDriver *drv = bs->drv;
1711 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001712 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09001713 if (drv->bdrv_snapshot_create)
1714 return drv->bdrv_snapshot_create(bs, sn_info);
1715 if (bs->file)
1716 return bdrv_snapshot_create(bs->file, sn_info);
1717 return -ENOTSUP;
bellardfaea38e2006-08-05 21:31:00 +00001718}
1719
ths5fafdf22007-09-16 21:08:06 +00001720int bdrv_snapshot_goto(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001721 const char *snapshot_id)
1722{
1723 BlockDriver *drv = bs->drv;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09001724 int ret, open_ret;
1725
bellardfaea38e2006-08-05 21:31:00 +00001726 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001727 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09001728 if (drv->bdrv_snapshot_goto)
1729 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1730
1731 if (bs->file) {
1732 drv->bdrv_close(bs);
1733 ret = bdrv_snapshot_goto(bs->file, snapshot_id);
1734 open_ret = drv->bdrv_open(bs, bs->open_flags);
1735 if (open_ret < 0) {
1736 bdrv_delete(bs->file);
1737 bs->drv = NULL;
1738 return open_ret;
1739 }
1740 return ret;
1741 }
1742
1743 return -ENOTSUP;
bellardfaea38e2006-08-05 21:31:00 +00001744}
1745
1746int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1747{
1748 BlockDriver *drv = bs->drv;
1749 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001750 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09001751 if (drv->bdrv_snapshot_delete)
1752 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1753 if (bs->file)
1754 return bdrv_snapshot_delete(bs->file, snapshot_id);
1755 return -ENOTSUP;
bellardfaea38e2006-08-05 21:31:00 +00001756}
1757
ths5fafdf22007-09-16 21:08:06 +00001758int bdrv_snapshot_list(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001759 QEMUSnapshotInfo **psn_info)
1760{
1761 BlockDriver *drv = bs->drv;
1762 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001763 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09001764 if (drv->bdrv_snapshot_list)
1765 return drv->bdrv_snapshot_list(bs, psn_info);
1766 if (bs->file)
1767 return bdrv_snapshot_list(bs->file, psn_info);
1768 return -ENOTSUP;
bellardfaea38e2006-08-05 21:31:00 +00001769}
1770
1771#define NB_SUFFIXES 4
1772
1773char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1774{
1775 static const char suffixes[NB_SUFFIXES] = "KMGT";
1776 int64_t base;
1777 int i;
1778
1779 if (size <= 999) {
1780 snprintf(buf, buf_size, "%" PRId64, size);
1781 } else {
1782 base = 1024;
1783 for(i = 0; i < NB_SUFFIXES; i++) {
1784 if (size < (10 * base)) {
ths5fafdf22007-09-16 21:08:06 +00001785 snprintf(buf, buf_size, "%0.1f%c",
bellardfaea38e2006-08-05 21:31:00 +00001786 (double)size / base,
1787 suffixes[i]);
1788 break;
1789 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
ths5fafdf22007-09-16 21:08:06 +00001790 snprintf(buf, buf_size, "%" PRId64 "%c",
bellardfaea38e2006-08-05 21:31:00 +00001791 ((size + (base >> 1)) / base),
1792 suffixes[i]);
1793 break;
1794 }
1795 base = base * 1024;
1796 }
1797 }
1798 return buf;
1799}
1800
1801char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1802{
1803 char buf1[128], date_buf[128], clock_buf[128];
bellard3b9f94e2007-01-07 17:27:07 +00001804#ifdef _WIN32
1805 struct tm *ptm;
1806#else
bellardfaea38e2006-08-05 21:31:00 +00001807 struct tm tm;
bellard3b9f94e2007-01-07 17:27:07 +00001808#endif
bellardfaea38e2006-08-05 21:31:00 +00001809 time_t ti;
1810 int64_t secs;
1811
1812 if (!sn) {
ths5fafdf22007-09-16 21:08:06 +00001813 snprintf(buf, buf_size,
1814 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001815 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1816 } else {
1817 ti = sn->date_sec;
bellard3b9f94e2007-01-07 17:27:07 +00001818#ifdef _WIN32
1819 ptm = localtime(&ti);
1820 strftime(date_buf, sizeof(date_buf),
1821 "%Y-%m-%d %H:%M:%S", ptm);
1822#else
bellardfaea38e2006-08-05 21:31:00 +00001823 localtime_r(&ti, &tm);
1824 strftime(date_buf, sizeof(date_buf),
1825 "%Y-%m-%d %H:%M:%S", &tm);
bellard3b9f94e2007-01-07 17:27:07 +00001826#endif
bellardfaea38e2006-08-05 21:31:00 +00001827 secs = sn->vm_clock_nsec / 1000000000;
1828 snprintf(clock_buf, sizeof(clock_buf),
1829 "%02d:%02d:%02d.%03d",
1830 (int)(secs / 3600),
1831 (int)((secs / 60) % 60),
ths5fafdf22007-09-16 21:08:06 +00001832 (int)(secs % 60),
bellardfaea38e2006-08-05 21:31:00 +00001833 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1834 snprintf(buf, buf_size,
ths5fafdf22007-09-16 21:08:06 +00001835 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001836 sn->id_str, sn->name,
1837 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1838 date_buf,
1839 clock_buf);
1840 }
1841 return buf;
1842}
1843
bellardea2384d2004-08-01 21:59:26 +00001844
bellard83f64092006-08-01 16:21:11 +00001845/**************************************************************/
1846/* async I/Os */
1847
aliguori3b69e4b2009-01-22 16:59:24 +00001848BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
aliguorif141eaf2009-04-07 18:43:24 +00001849 QEMUIOVector *qiov, int nb_sectors,
aliguori3b69e4b2009-01-22 16:59:24 +00001850 BlockDriverCompletionFunc *cb, void *opaque)
1851{
bellard83f64092006-08-01 16:21:11 +00001852 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001853 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001854
bellard19cb3732006-08-19 11:45:59 +00001855 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001856 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001857 if (bdrv_check_request(bs, sector_num, nb_sectors))
1858 return NULL;
ths3b46e622007-09-17 08:09:54 +00001859
aliguorif141eaf2009-04-07 18:43:24 +00001860 ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
1861 cb, opaque);
thsa36e69d2007-12-02 05:18:19 +00001862
1863 if (ret) {
1864 /* Update stats even though technically transfer has not happened. */
Jan Kiszka6ea44302009-11-30 18:21:19 +01001865 bs->rd_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
thsa36e69d2007-12-02 05:18:19 +00001866 bs->rd_ops ++;
1867 }
1868
1869 return ret;
bellard83f64092006-08-01 16:21:11 +00001870}
1871
aliguorif141eaf2009-04-07 18:43:24 +00001872BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1873 QEMUIOVector *qiov, int nb_sectors,
1874 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001875{
bellard83f64092006-08-01 16:21:11 +00001876 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001877 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001878
bellard19cb3732006-08-19 11:45:59 +00001879 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001880 return NULL;
bellard83f64092006-08-01 16:21:11 +00001881 if (bs->read_only)
pbrookce1a14d2006-08-07 02:38:06 +00001882 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001883 if (bdrv_check_request(bs, sector_num, nb_sectors))
1884 return NULL;
bellard83f64092006-08-01 16:21:11 +00001885
Jan Kiszkac6d22832009-11-30 18:21:20 +01001886 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001887 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1888 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001889
aliguorif141eaf2009-04-07 18:43:24 +00001890 ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
1891 cb, opaque);
thsa36e69d2007-12-02 05:18:19 +00001892
1893 if (ret) {
Kevin Wolf294cc352010-04-28 14:34:01 +02001894 /* Update stats even though technically transfer has not happened. */
1895 bs->wr_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
1896 bs->wr_ops ++;
1897 if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
1898 bs->wr_highest_sector = sector_num + nb_sectors - 1;
1899 }
thsa36e69d2007-12-02 05:18:19 +00001900 }
1901
1902 return ret;
bellard83f64092006-08-01 16:21:11 +00001903}
1904
Kevin Wolf40b4f532009-09-09 17:53:37 +02001905
1906typedef struct MultiwriteCB {
1907 int error;
1908 int num_requests;
1909 int num_callbacks;
1910 struct {
1911 BlockDriverCompletionFunc *cb;
1912 void *opaque;
1913 QEMUIOVector *free_qiov;
1914 void *free_buf;
1915 } callbacks[];
1916} MultiwriteCB;
1917
1918static void multiwrite_user_cb(MultiwriteCB *mcb)
1919{
1920 int i;
1921
1922 for (i = 0; i < mcb->num_callbacks; i++) {
1923 mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
Stefan Hajnoczi1e1ea482010-04-21 20:35:45 +01001924 if (mcb->callbacks[i].free_qiov) {
1925 qemu_iovec_destroy(mcb->callbacks[i].free_qiov);
1926 }
Kevin Wolf40b4f532009-09-09 17:53:37 +02001927 qemu_free(mcb->callbacks[i].free_qiov);
Herve Poussineauf8a83242010-01-24 21:23:56 +00001928 qemu_vfree(mcb->callbacks[i].free_buf);
Kevin Wolf40b4f532009-09-09 17:53:37 +02001929 }
1930}
1931
1932static void multiwrite_cb(void *opaque, int ret)
1933{
1934 MultiwriteCB *mcb = opaque;
1935
Kevin Wolfcb6d3ca2010-04-01 22:48:44 +02001936 if (ret < 0 && !mcb->error) {
Kevin Wolf40b4f532009-09-09 17:53:37 +02001937 mcb->error = ret;
1938 multiwrite_user_cb(mcb);
1939 }
1940
1941 mcb->num_requests--;
1942 if (mcb->num_requests == 0) {
1943 if (mcb->error == 0) {
1944 multiwrite_user_cb(mcb);
1945 }
1946 qemu_free(mcb);
1947 }
1948}
1949
1950static int multiwrite_req_compare(const void *a, const void *b)
1951{
Christoph Hellwig77be4362010-05-19 20:53:10 +02001952 const BlockRequest *req1 = a, *req2 = b;
1953
1954 /*
1955 * Note that we can't simply subtract req2->sector from req1->sector
1956 * here as that could overflow the return value.
1957 */
1958 if (req1->sector > req2->sector) {
1959 return 1;
1960 } else if (req1->sector < req2->sector) {
1961 return -1;
1962 } else {
1963 return 0;
1964 }
Kevin Wolf40b4f532009-09-09 17:53:37 +02001965}
1966
1967/*
1968 * Takes a bunch of requests and tries to merge them. Returns the number of
1969 * requests that remain after merging.
1970 */
1971static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
1972 int num_reqs, MultiwriteCB *mcb)
1973{
1974 int i, outidx;
1975
1976 // Sort requests by start sector
1977 qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
1978
1979 // Check if adjacent requests touch the same clusters. If so, combine them,
1980 // filling up gaps with zero sectors.
1981 outidx = 0;
1982 for (i = 1; i < num_reqs; i++) {
1983 int merge = 0;
1984 int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
1985
1986 // This handles the cases that are valid for all block drivers, namely
1987 // exactly sequential writes and overlapping writes.
1988 if (reqs[i].sector <= oldreq_last) {
1989 merge = 1;
1990 }
1991
1992 // The block driver may decide that it makes sense to combine requests
1993 // even if there is a gap of some sectors between them. In this case,
1994 // the gap is filled with zeros (therefore only applicable for yet
1995 // unused space in format like qcow2).
1996 if (!merge && bs->drv->bdrv_merge_requests) {
1997 merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]);
1998 }
1999
Christoph Hellwige2a305f2010-01-26 14:49:08 +01002000 if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) {
2001 merge = 0;
2002 }
2003
Kevin Wolf40b4f532009-09-09 17:53:37 +02002004 if (merge) {
2005 size_t size;
2006 QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov));
2007 qemu_iovec_init(qiov,
2008 reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
2009
2010 // Add the first request to the merged one. If the requests are
2011 // overlapping, drop the last sectors of the first request.
2012 size = (reqs[i].sector - reqs[outidx].sector) << 9;
2013 qemu_iovec_concat(qiov, reqs[outidx].qiov, size);
2014
2015 // We might need to add some zeros between the two requests
2016 if (reqs[i].sector > oldreq_last) {
2017 size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
2018 uint8_t *buf = qemu_blockalign(bs, zero_bytes);
2019 memset(buf, 0, zero_bytes);
2020 qemu_iovec_add(qiov, buf, zero_bytes);
2021 mcb->callbacks[i].free_buf = buf;
2022 }
2023
2024 // Add the second request
2025 qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size);
2026
Kevin Wolfcbf1dff2010-05-21 11:09:42 +02002027 reqs[outidx].nb_sectors = qiov->size >> 9;
Kevin Wolf40b4f532009-09-09 17:53:37 +02002028 reqs[outidx].qiov = qiov;
2029
2030 mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
2031 } else {
2032 outidx++;
2033 reqs[outidx].sector = reqs[i].sector;
2034 reqs[outidx].nb_sectors = reqs[i].nb_sectors;
2035 reqs[outidx].qiov = reqs[i].qiov;
2036 }
2037 }
2038
2039 return outidx + 1;
2040}
2041
2042/*
2043 * Submit multiple AIO write requests at once.
2044 *
2045 * On success, the function returns 0 and all requests in the reqs array have
2046 * been submitted. In error case this function returns -1, and any of the
2047 * requests may or may not be submitted yet. In particular, this means that the
2048 * callback will be called for some of the requests, for others it won't. The
2049 * caller must check the error field of the BlockRequest to wait for the right
2050 * callbacks (if error != 0, no callback will be called).
2051 *
2052 * The implementation may modify the contents of the reqs array, e.g. to merge
2053 * requests. However, the fields opaque and error are left unmodified as they
2054 * are used to signal failure for a single request to the caller.
2055 */
2056int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
2057{
2058 BlockDriverAIOCB *acb;
2059 MultiwriteCB *mcb;
2060 int i;
2061
2062 if (num_reqs == 0) {
2063 return 0;
2064 }
2065
2066 // Create MultiwriteCB structure
2067 mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
2068 mcb->num_requests = 0;
2069 mcb->num_callbacks = num_reqs;
2070
2071 for (i = 0; i < num_reqs; i++) {
2072 mcb->callbacks[i].cb = reqs[i].cb;
2073 mcb->callbacks[i].opaque = reqs[i].opaque;
2074 }
2075
2076 // Check for mergable requests
2077 num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
2078
2079 // Run the aio requests
2080 for (i = 0; i < num_reqs; i++) {
2081 acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
2082 reqs[i].nb_sectors, multiwrite_cb, mcb);
2083
2084 if (acb == NULL) {
2085 // We can only fail the whole thing if no request has been
2086 // submitted yet. Otherwise we'll wait for the submitted AIOs to
2087 // complete and report the error in the callback.
2088 if (mcb->num_requests == 0) {
Kevin Wolf0f0b6042010-04-06 18:24:06 +02002089 reqs[i].error = -EIO;
Kevin Wolf40b4f532009-09-09 17:53:37 +02002090 goto fail;
2091 } else {
Kevin Wolf7eb58a62010-04-06 18:24:07 +02002092 mcb->num_requests++;
2093 multiwrite_cb(mcb, -EIO);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002094 break;
2095 }
2096 } else {
2097 mcb->num_requests++;
2098 }
2099 }
2100
2101 return 0;
2102
2103fail:
Bruce Rogersaf474592010-05-13 15:14:33 -06002104 qemu_free(mcb);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002105 return -1;
2106}
2107
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002108BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
2109 BlockDriverCompletionFunc *cb, void *opaque)
2110{
2111 BlockDriver *drv = bs->drv;
2112
Alexander Graf016f5cf2010-05-26 17:51:49 +02002113 if (bs->open_flags & BDRV_O_NO_FLUSH) {
2114 return bdrv_aio_noop_em(bs, cb, opaque);
2115 }
2116
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002117 if (!drv)
2118 return NULL;
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002119 return drv->bdrv_aio_flush(bs, cb, opaque);
2120}
2121
bellard83f64092006-08-01 16:21:11 +00002122void bdrv_aio_cancel(BlockDriverAIOCB *acb)
pbrookce1a14d2006-08-07 02:38:06 +00002123{
aliguori6bbff9a2009-03-20 18:25:59 +00002124 acb->pool->cancel(acb);
bellard83f64092006-08-01 16:21:11 +00002125}
2126
pbrookce1a14d2006-08-07 02:38:06 +00002127
bellard83f64092006-08-01 16:21:11 +00002128/**************************************************************/
2129/* async block device emulation */
2130
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002131typedef struct BlockDriverAIOCBSync {
2132 BlockDriverAIOCB common;
2133 QEMUBH *bh;
2134 int ret;
2135 /* vector translation state */
2136 QEMUIOVector *qiov;
2137 uint8_t *bounce;
2138 int is_write;
2139} BlockDriverAIOCBSync;
2140
2141static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
2142{
Kevin Wolfb666d232010-05-05 11:44:39 +02002143 BlockDriverAIOCBSync *acb =
2144 container_of(blockacb, BlockDriverAIOCBSync, common);
Dor Laor6a7ad292009-06-01 12:07:23 +03002145 qemu_bh_delete(acb->bh);
Avi Kivity36afc452009-06-23 16:20:36 +03002146 acb->bh = NULL;
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002147 qemu_aio_release(acb);
2148}
2149
2150static AIOPool bdrv_em_aio_pool = {
2151 .aiocb_size = sizeof(BlockDriverAIOCBSync),
2152 .cancel = bdrv_aio_cancel_em,
2153};
2154
bellard83f64092006-08-01 16:21:11 +00002155static void bdrv_aio_bh_cb(void *opaque)
bellardbeac80c2006-06-26 20:08:57 +00002156{
pbrookce1a14d2006-08-07 02:38:06 +00002157 BlockDriverAIOCBSync *acb = opaque;
aliguorif141eaf2009-04-07 18:43:24 +00002158
aliguorif141eaf2009-04-07 18:43:24 +00002159 if (!acb->is_write)
2160 qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
aliguoriceb42de2009-04-07 18:43:28 +00002161 qemu_vfree(acb->bounce);
pbrookce1a14d2006-08-07 02:38:06 +00002162 acb->common.cb(acb->common.opaque, acb->ret);
Dor Laor6a7ad292009-06-01 12:07:23 +03002163 qemu_bh_delete(acb->bh);
Avi Kivity36afc452009-06-23 16:20:36 +03002164 acb->bh = NULL;
pbrookce1a14d2006-08-07 02:38:06 +00002165 qemu_aio_release(acb);
bellardbeac80c2006-06-26 20:08:57 +00002166}
bellardbeac80c2006-06-26 20:08:57 +00002167
aliguorif141eaf2009-04-07 18:43:24 +00002168static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
2169 int64_t sector_num,
2170 QEMUIOVector *qiov,
2171 int nb_sectors,
2172 BlockDriverCompletionFunc *cb,
2173 void *opaque,
2174 int is_write)
2175
bellardea2384d2004-08-01 21:59:26 +00002176{
pbrookce1a14d2006-08-07 02:38:06 +00002177 BlockDriverAIOCBSync *acb;
pbrookce1a14d2006-08-07 02:38:06 +00002178
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002179 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
aliguorif141eaf2009-04-07 18:43:24 +00002180 acb->is_write = is_write;
2181 acb->qiov = qiov;
aliguorie268ca52009-04-22 20:20:00 +00002182 acb->bounce = qemu_blockalign(bs, qiov->size);
aliguorif141eaf2009-04-07 18:43:24 +00002183
pbrookce1a14d2006-08-07 02:38:06 +00002184 if (!acb->bh)
2185 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
aliguorif141eaf2009-04-07 18:43:24 +00002186
2187 if (is_write) {
2188 qemu_iovec_to_buffer(acb->qiov, acb->bounce);
2189 acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
2190 } else {
2191 acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
2192 }
2193
pbrookce1a14d2006-08-07 02:38:06 +00002194 qemu_bh_schedule(acb->bh);
aliguorif141eaf2009-04-07 18:43:24 +00002195
pbrookce1a14d2006-08-07 02:38:06 +00002196 return &acb->common;
pbrook7a6cba62006-06-04 11:39:07 +00002197}
2198
aliguorif141eaf2009-04-07 18:43:24 +00002199static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
2200 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
pbrookce1a14d2006-08-07 02:38:06 +00002201 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00002202{
aliguorif141eaf2009-04-07 18:43:24 +00002203 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
bellard83f64092006-08-01 16:21:11 +00002204}
2205
aliguorif141eaf2009-04-07 18:43:24 +00002206static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
2207 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2208 BlockDriverCompletionFunc *cb, void *opaque)
2209{
2210 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
2211}
2212
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002213static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
2214 BlockDriverCompletionFunc *cb, void *opaque)
2215{
2216 BlockDriverAIOCBSync *acb;
2217
2218 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2219 acb->is_write = 1; /* don't bounce in the completion hadler */
2220 acb->qiov = NULL;
2221 acb->bounce = NULL;
2222 acb->ret = 0;
2223
2224 if (!acb->bh)
2225 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2226
2227 bdrv_flush(bs);
2228 qemu_bh_schedule(acb->bh);
2229 return &acb->common;
2230}
2231
Alexander Graf016f5cf2010-05-26 17:51:49 +02002232static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,
2233 BlockDriverCompletionFunc *cb, void *opaque)
2234{
2235 BlockDriverAIOCBSync *acb;
2236
2237 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2238 acb->is_write = 1; /* don't bounce in the completion handler */
2239 acb->qiov = NULL;
2240 acb->bounce = NULL;
2241 acb->ret = 0;
2242
2243 if (!acb->bh) {
2244 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2245 }
2246
2247 qemu_bh_schedule(acb->bh);
2248 return &acb->common;
2249}
2250
bellard83f64092006-08-01 16:21:11 +00002251/**************************************************************/
2252/* sync block device emulation */
2253
2254static void bdrv_rw_em_cb(void *opaque, int ret)
2255{
2256 *(int *)opaque = ret;
2257}
2258
2259#define NOT_DONE 0x7fffffff
2260
ths5fafdf22007-09-16 21:08:06 +00002261static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +00002262 uint8_t *buf, int nb_sectors)
2263{
pbrookce1a14d2006-08-07 02:38:06 +00002264 int async_ret;
2265 BlockDriverAIOCB *acb;
aliguorif141eaf2009-04-07 18:43:24 +00002266 struct iovec iov;
2267 QEMUIOVector qiov;
bellard83f64092006-08-01 16:21:11 +00002268
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002269 async_context_push();
2270
bellard83f64092006-08-01 16:21:11 +00002271 async_ret = NOT_DONE;
blueswir13f4cb3d2009-04-13 16:31:01 +00002272 iov.iov_base = (void *)buf;
Jes Sorenseneb5a3162010-05-27 16:20:31 +02002273 iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
aliguorif141eaf2009-04-07 18:43:24 +00002274 qemu_iovec_init_external(&qiov, &iov, 1);
2275 acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
2276 bdrv_rw_em_cb, &async_ret);
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002277 if (acb == NULL) {
2278 async_ret = -1;
2279 goto fail;
2280 }
aliguoribaf35cb2008-09-10 15:45:19 +00002281
bellard83f64092006-08-01 16:21:11 +00002282 while (async_ret == NOT_DONE) {
2283 qemu_aio_wait();
2284 }
aliguoribaf35cb2008-09-10 15:45:19 +00002285
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002286
2287fail:
2288 async_context_pop();
bellard83f64092006-08-01 16:21:11 +00002289 return async_ret;
2290}
2291
2292static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
2293 const uint8_t *buf, int nb_sectors)
2294{
pbrookce1a14d2006-08-07 02:38:06 +00002295 int async_ret;
2296 BlockDriverAIOCB *acb;
aliguorif141eaf2009-04-07 18:43:24 +00002297 struct iovec iov;
2298 QEMUIOVector qiov;
bellard83f64092006-08-01 16:21:11 +00002299
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002300 async_context_push();
2301
bellard83f64092006-08-01 16:21:11 +00002302 async_ret = NOT_DONE;
aliguorif141eaf2009-04-07 18:43:24 +00002303 iov.iov_base = (void *)buf;
Jes Sorenseneb5a3162010-05-27 16:20:31 +02002304 iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
aliguorif141eaf2009-04-07 18:43:24 +00002305 qemu_iovec_init_external(&qiov, &iov, 1);
2306 acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
2307 bdrv_rw_em_cb, &async_ret);
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002308 if (acb == NULL) {
2309 async_ret = -1;
2310 goto fail;
2311 }
bellard83f64092006-08-01 16:21:11 +00002312 while (async_ret == NOT_DONE) {
2313 qemu_aio_wait();
2314 }
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002315
2316fail:
2317 async_context_pop();
bellard83f64092006-08-01 16:21:11 +00002318 return async_ret;
2319}
bellardea2384d2004-08-01 21:59:26 +00002320
2321void bdrv_init(void)
2322{
Anthony Liguori5efa9d52009-05-09 17:03:42 -05002323 module_call_init(MODULE_INIT_BLOCK);
bellardea2384d2004-08-01 21:59:26 +00002324}
pbrookce1a14d2006-08-07 02:38:06 +00002325
Markus Armbrustereb852012009-10-27 18:41:44 +01002326void bdrv_init_with_whitelist(void)
2327{
2328 use_bdrv_whitelist = 1;
2329 bdrv_init();
2330}
2331
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002332void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
2333 BlockDriverCompletionFunc *cb, void *opaque)
aliguori6bbff9a2009-03-20 18:25:59 +00002334{
pbrookce1a14d2006-08-07 02:38:06 +00002335 BlockDriverAIOCB *acb;
2336
aliguori6bbff9a2009-03-20 18:25:59 +00002337 if (pool->free_aiocb) {
2338 acb = pool->free_aiocb;
2339 pool->free_aiocb = acb->next;
pbrookce1a14d2006-08-07 02:38:06 +00002340 } else {
aliguori6bbff9a2009-03-20 18:25:59 +00002341 acb = qemu_mallocz(pool->aiocb_size);
2342 acb->pool = pool;
pbrookce1a14d2006-08-07 02:38:06 +00002343 }
2344 acb->bs = bs;
2345 acb->cb = cb;
2346 acb->opaque = opaque;
2347 return acb;
2348}
2349
2350void qemu_aio_release(void *p)
2351{
aliguori6bbff9a2009-03-20 18:25:59 +00002352 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
2353 AIOPool *pool = acb->pool;
2354 acb->next = pool->free_aiocb;
2355 pool->free_aiocb = acb;
pbrookce1a14d2006-08-07 02:38:06 +00002356}
bellard19cb3732006-08-19 11:45:59 +00002357
2358/**************************************************************/
2359/* removable device support */
2360
2361/**
2362 * Return TRUE if the media is present
2363 */
2364int bdrv_is_inserted(BlockDriverState *bs)
2365{
2366 BlockDriver *drv = bs->drv;
2367 int ret;
2368 if (!drv)
2369 return 0;
2370 if (!drv->bdrv_is_inserted)
2371 return 1;
2372 ret = drv->bdrv_is_inserted(bs);
2373 return ret;
2374}
2375
2376/**
2377 * Return TRUE if the media changed since the last call to this
ths5fafdf22007-09-16 21:08:06 +00002378 * function. It is currently only used for floppy disks
bellard19cb3732006-08-19 11:45:59 +00002379 */
2380int bdrv_media_changed(BlockDriverState *bs)
2381{
2382 BlockDriver *drv = bs->drv;
2383 int ret;
2384
2385 if (!drv || !drv->bdrv_media_changed)
2386 ret = -ENOTSUP;
2387 else
2388 ret = drv->bdrv_media_changed(bs);
2389 if (ret == -ENOTSUP)
2390 ret = bs->media_changed;
2391 bs->media_changed = 0;
2392 return ret;
2393}
2394
2395/**
2396 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
2397 */
Mark McLoughlinaea2a332009-05-27 10:06:11 +01002398int bdrv_eject(BlockDriverState *bs, int eject_flag)
bellard19cb3732006-08-19 11:45:59 +00002399{
2400 BlockDriver *drv = bs->drv;
2401 int ret;
2402
Mark McLoughlinaea2a332009-05-27 10:06:11 +01002403 if (bs->locked) {
2404 return -EBUSY;
2405 }
2406
bellard19cb3732006-08-19 11:45:59 +00002407 if (!drv || !drv->bdrv_eject) {
2408 ret = -ENOTSUP;
2409 } else {
2410 ret = drv->bdrv_eject(bs, eject_flag);
2411 }
2412 if (ret == -ENOTSUP) {
2413 if (eject_flag)
2414 bdrv_close(bs);
Mark McLoughlinaea2a332009-05-27 10:06:11 +01002415 ret = 0;
bellard19cb3732006-08-19 11:45:59 +00002416 }
Mark McLoughlinaea2a332009-05-27 10:06:11 +01002417
2418 return ret;
bellard19cb3732006-08-19 11:45:59 +00002419}
2420
2421int bdrv_is_locked(BlockDriverState *bs)
2422{
2423 return bs->locked;
2424}
2425
2426/**
2427 * Lock or unlock the media (if it is locked, the user won't be able
2428 * to eject it manually).
2429 */
2430void bdrv_set_locked(BlockDriverState *bs, int locked)
2431{
2432 BlockDriver *drv = bs->drv;
2433
2434 bs->locked = locked;
2435 if (drv && drv->bdrv_set_locked) {
2436 drv->bdrv_set_locked(bs, locked);
2437 }
2438}
ths985a03b2007-12-24 16:10:43 +00002439
2440/* needed for generic scsi interface */
2441
2442int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
2443{
2444 BlockDriver *drv = bs->drv;
2445
2446 if (drv && drv->bdrv_ioctl)
2447 return drv->bdrv_ioctl(bs, req, buf);
2448 return -ENOTSUP;
2449}
aliguori7d780662009-03-12 19:57:08 +00002450
aliguori221f7152009-03-28 17:28:41 +00002451BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
2452 unsigned long int req, void *buf,
2453 BlockDriverCompletionFunc *cb, void *opaque)
aliguori7d780662009-03-12 19:57:08 +00002454{
aliguori221f7152009-03-28 17:28:41 +00002455 BlockDriver *drv = bs->drv;
aliguori7d780662009-03-12 19:57:08 +00002456
aliguori221f7152009-03-28 17:28:41 +00002457 if (drv && drv->bdrv_aio_ioctl)
2458 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
2459 return NULL;
aliguori7d780662009-03-12 19:57:08 +00002460}
aliguorie268ca52009-04-22 20:20:00 +00002461
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002462
2463
aliguorie268ca52009-04-22 20:20:00 +00002464void *qemu_blockalign(BlockDriverState *bs, size_t size)
2465{
2466 return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
2467}
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002468
2469void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable)
2470{
2471 int64_t bitmap_size;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002472
Liran Schouraaa0eb72010-01-26 10:31:48 +02002473 bs->dirty_count = 0;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002474 if (enable) {
Jan Kiszkac6d22832009-11-30 18:21:20 +01002475 if (!bs->dirty_bitmap) {
2476 bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
2477 BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
2478 bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002479
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002480 bs->dirty_bitmap = qemu_mallocz(bitmap_size);
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002481 }
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002482 } else {
Jan Kiszkac6d22832009-11-30 18:21:20 +01002483 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002484 qemu_free(bs->dirty_bitmap);
Jan Kiszkac6d22832009-11-30 18:21:20 +01002485 bs->dirty_bitmap = NULL;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002486 }
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002487 }
2488}
2489
2490int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
2491{
Jan Kiszka6ea44302009-11-30 18:21:19 +01002492 int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002493
Jan Kiszkac6d22832009-11-30 18:21:20 +01002494 if (bs->dirty_bitmap &&
2495 (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) {
2496 return bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] &
2497 (1 << (chunk % (sizeof(unsigned long) * 8)));
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002498 } else {
2499 return 0;
2500 }
2501}
2502
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002503void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
2504 int nr_sectors)
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002505{
2506 set_dirty_bitmap(bs, cur_sector, nr_sectors, 0);
2507}
Liran Schouraaa0eb72010-01-26 10:31:48 +02002508
2509int64_t bdrv_get_dirty_count(BlockDriverState *bs)
2510{
2511 return bs->dirty_count;
2512}