blob: 9176decf333425f24326fb11819da75e392e33a3 [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 Armbrusterf9092b12010-06-25 10:33:39 +020066/* The device to use for VM snapshots */
67static BlockDriverState *bs_snapshots;
68
Markus Armbrustereb852012009-10-27 18:41:44 +010069/* If non-zero, use only whitelisted block drivers */
70static int use_bdrv_whitelist;
71
bellard83f64092006-08-01 16:21:11 +000072int path_is_absolute(const char *path)
73{
74 const char *p;
bellard21664422007-01-07 18:22:37 +000075#ifdef _WIN32
76 /* specific case for names like: "\\.\d:" */
77 if (*path == '/' || *path == '\\')
78 return 1;
79#endif
bellard83f64092006-08-01 16:21:11 +000080 p = strchr(path, ':');
81 if (p)
82 p++;
83 else
84 p = path;
bellard3b9f94e2007-01-07 17:27:07 +000085#ifdef _WIN32
86 return (*p == '/' || *p == '\\');
87#else
88 return (*p == '/');
89#endif
bellard83f64092006-08-01 16:21:11 +000090}
91
92/* if filename is absolute, just copy it to dest. Otherwise, build a
93 path to it by considering it is relative to base_path. URL are
94 supported. */
95void path_combine(char *dest, int dest_size,
96 const char *base_path,
97 const char *filename)
98{
99 const char *p, *p1;
100 int len;
101
102 if (dest_size <= 0)
103 return;
104 if (path_is_absolute(filename)) {
105 pstrcpy(dest, dest_size, filename);
106 } else {
107 p = strchr(base_path, ':');
108 if (p)
109 p++;
110 else
111 p = base_path;
bellard3b9f94e2007-01-07 17:27:07 +0000112 p1 = strrchr(base_path, '/');
113#ifdef _WIN32
114 {
115 const char *p2;
116 p2 = strrchr(base_path, '\\');
117 if (!p1 || p2 > p1)
118 p1 = p2;
119 }
120#endif
bellard83f64092006-08-01 16:21:11 +0000121 if (p1)
122 p1++;
123 else
124 p1 = base_path;
125 if (p1 > p)
126 p = p1;
127 len = p - base_path;
128 if (len > dest_size - 1)
129 len = dest_size - 1;
130 memcpy(dest, base_path, len);
131 dest[len] = '\0';
132 pstrcat(dest, dest_size, filename);
133 }
134}
135
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500136void bdrv_register(BlockDriver *bdrv)
bellardea2384d2004-08-01 21:59:26 +0000137{
aliguorif141eaf2009-04-07 18:43:24 +0000138 if (!bdrv->bdrv_aio_readv) {
bellard83f64092006-08-01 16:21:11 +0000139 /* add AIO emulation layer */
aliguorif141eaf2009-04-07 18:43:24 +0000140 bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
141 bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
aliguorieda578e2009-03-12 19:57:16 +0000142 } else if (!bdrv->bdrv_read) {
bellard83f64092006-08-01 16:21:11 +0000143 /* add synchronous IO emulation layer */
144 bdrv->bdrv_read = bdrv_read_em;
145 bdrv->bdrv_write = bdrv_write_em;
146 }
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +0200147
148 if (!bdrv->bdrv_aio_flush)
149 bdrv->bdrv_aio_flush = bdrv_aio_flush_em;
150
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100151 QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
bellardea2384d2004-08-01 21:59:26 +0000152}
bellardb3380822004-03-14 21:38:54 +0000153
154/* create a new block device (by default it is empty) */
155BlockDriverState *bdrv_new(const char *device_name)
bellardfc01f7e2003-06-30 10:03:06 +0000156{
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +0100157 BlockDriverState *bs;
bellardb3380822004-03-14 21:38:54 +0000158
159 bs = qemu_mallocz(sizeof(BlockDriverState));
bellardb3380822004-03-14 21:38:54 +0000160 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
bellardea2384d2004-08-01 21:59:26 +0000161 if (device_name[0] != '\0') {
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +0100162 QTAILQ_INSERT_TAIL(&bdrv_states, bs, list);
bellardea2384d2004-08-01 21:59:26 +0000163 }
bellardb3380822004-03-14 21:38:54 +0000164 return bs;
165}
166
bellardea2384d2004-08-01 21:59:26 +0000167BlockDriver *bdrv_find_format(const char *format_name)
168{
169 BlockDriver *drv1;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100170 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
171 if (!strcmp(drv1->format_name, format_name)) {
bellardea2384d2004-08-01 21:59:26 +0000172 return drv1;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100173 }
bellardea2384d2004-08-01 21:59:26 +0000174 }
175 return NULL;
176}
177
Markus Armbrustereb852012009-10-27 18:41:44 +0100178static int bdrv_is_whitelisted(BlockDriver *drv)
179{
180 static const char *whitelist[] = {
181 CONFIG_BDRV_WHITELIST
182 };
183 const char **p;
184
185 if (!whitelist[0])
186 return 1; /* no whitelist, anything goes */
187
188 for (p = whitelist; *p; p++) {
189 if (!strcmp(drv->format_name, *p)) {
190 return 1;
191 }
192 }
193 return 0;
194}
195
196BlockDriver *bdrv_find_whitelisted_format(const char *format_name)
197{
198 BlockDriver *drv = bdrv_find_format(format_name);
199 return drv && bdrv_is_whitelisted(drv) ? drv : NULL;
200}
201
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200202int bdrv_create(BlockDriver *drv, const char* filename,
203 QEMUOptionParameter *options)
bellardea2384d2004-08-01 21:59:26 +0000204{
205 if (!drv->bdrv_create)
206 return -ENOTSUP;
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200207
208 return drv->bdrv_create(filename, options);
bellardea2384d2004-08-01 21:59:26 +0000209}
210
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200211int bdrv_create_file(const char* filename, QEMUOptionParameter *options)
212{
213 BlockDriver *drv;
214
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900215 drv = bdrv_find_protocol(filename);
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200216 if (drv == NULL) {
217 drv = bdrv_find_format("file");
218 }
219
220 return bdrv_create(drv, filename, options);
221}
222
bellardd5249392004-08-03 21:14:23 +0000223#ifdef _WIN32
bellard95389c82005-12-18 18:28:15 +0000224void get_tmp_filename(char *filename, int size)
bellardd5249392004-08-03 21:14:23 +0000225{
bellard3b9f94e2007-01-07 17:27:07 +0000226 char temp_dir[MAX_PATH];
ths3b46e622007-09-17 08:09:54 +0000227
bellard3b9f94e2007-01-07 17:27:07 +0000228 GetTempPath(MAX_PATH, temp_dir);
229 GetTempFileName(temp_dir, "qem", 0, filename);
bellardd5249392004-08-03 21:14:23 +0000230}
231#else
bellard95389c82005-12-18 18:28:15 +0000232void get_tmp_filename(char *filename, int size)
bellardea2384d2004-08-01 21:59:26 +0000233{
234 int fd;
blueswir17ccfb2e2008-09-14 06:45:34 +0000235 const char *tmpdir;
bellardd5249392004-08-03 21:14:23 +0000236 /* XXX: race condition possible */
aurel320badc1e2008-03-10 00:05:34 +0000237 tmpdir = getenv("TMPDIR");
238 if (!tmpdir)
239 tmpdir = "/tmp";
240 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
bellardea2384d2004-08-01 21:59:26 +0000241 fd = mkstemp(filename);
242 close(fd);
243}
bellardd5249392004-08-03 21:14:23 +0000244#endif
bellardea2384d2004-08-01 21:59:26 +0000245
bellard19cb3732006-08-19 11:45:59 +0000246#ifdef _WIN32
bellardf45512f2006-08-23 21:40:13 +0000247static int is_windows_drive_prefix(const char *filename)
248{
249 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
250 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
251 filename[1] == ':');
252}
ths3b46e622007-09-17 08:09:54 +0000253
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200254int is_windows_drive(const char *filename)
bellard19cb3732006-08-19 11:45:59 +0000255{
ths5fafdf22007-09-16 21:08:06 +0000256 if (is_windows_drive_prefix(filename) &&
bellardf45512f2006-08-23 21:40:13 +0000257 filename[2] == '\0')
bellard19cb3732006-08-19 11:45:59 +0000258 return 1;
259 if (strstart(filename, "\\\\.\\", NULL) ||
260 strstart(filename, "//./", NULL))
261 return 1;
262 return 0;
263}
264#endif
265
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200266/*
267 * Detect host devices. By convention, /dev/cdrom[N] is always
268 * recognized as a host CDROM.
269 */
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200270static BlockDriver *find_hdev_driver(const char *filename)
271{
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200272 int score_max = 0, score;
273 BlockDriver *drv = NULL, *d;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200274
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100275 QLIST_FOREACH(d, &bdrv_drivers, list) {
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200276 if (d->bdrv_probe_device) {
277 score = d->bdrv_probe_device(filename);
278 if (score > score_max) {
279 score_max = score;
280 drv = d;
281 }
282 }
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200283 }
284
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200285 return drv;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200286}
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200287
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900288BlockDriver *bdrv_find_protocol(const char *filename)
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200289{
290 BlockDriver *drv1;
291 char protocol[128];
292 int len;
293 const char *p;
294
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200295 /* TODO Drivers without bdrv_file_open must be specified explicitly */
296
Christoph Hellwig39508e72010-06-23 12:25:17 +0200297 /*
298 * XXX(hch): we really should not let host device detection
299 * override an explicit protocol specification, but moving this
300 * later breaks access to device names with colons in them.
301 * Thanks to the brain-dead persistent naming schemes on udev-
302 * based Linux systems those actually are quite common.
303 */
304 drv1 = find_hdev_driver(filename);
305 if (drv1) {
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200306 return drv1;
307 }
Christoph Hellwig39508e72010-06-23 12:25:17 +0200308
309#ifdef _WIN32
310 if (is_windows_drive(filename) ||
311 is_windows_drive_prefix(filename))
312 return bdrv_find_format("file");
313#endif
314
315 p = strchr(filename, ':');
316 if (!p) {
317 return bdrv_find_format("file");
318 }
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200319 len = p - filename;
320 if (len > sizeof(protocol) - 1)
321 len = sizeof(protocol) - 1;
322 memcpy(protocol, filename, len);
323 protocol[len] = '\0';
324 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
325 if (drv1->protocol_name &&
326 !strcmp(drv1->protocol_name, protocol)) {
327 return drv1;
328 }
329 }
330 return NULL;
331}
332
bellardea2384d2004-08-01 21:59:26 +0000333static BlockDriver *find_image_format(const char *filename)
334{
bellard83f64092006-08-01 16:21:11 +0000335 int ret, score, score_max;
bellardea2384d2004-08-01 21:59:26 +0000336 BlockDriver *drv1, *drv;
bellard83f64092006-08-01 16:21:11 +0000337 uint8_t buf[2048];
338 BlockDriverState *bs;
ths3b46e622007-09-17 08:09:54 +0000339
Naphtali Spreif5edb012010-01-17 16:48:13 +0200340 ret = bdrv_file_open(&bs, filename, 0);
bellard83f64092006-08-01 16:21:11 +0000341 if (ret < 0)
342 return NULL;
Nicholas Bellingerf8ea0b02010-05-17 09:45:57 -0700343
Kevin Wolf08a00552010-06-01 18:37:31 +0200344 /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
345 if (bs->sg || !bdrv_is_inserted(bs)) {
Nicholas A. Bellinger1a396852010-05-27 08:56:28 -0700346 bdrv_delete(bs);
Nicholas Bellingerf8ea0b02010-05-17 09:45:57 -0700347 return bdrv_find_format("raw");
Nicholas A. Bellinger1a396852010-05-27 08:56:28 -0700348 }
Nicholas Bellingerf8ea0b02010-05-17 09:45:57 -0700349
bellard83f64092006-08-01 16:21:11 +0000350 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
351 bdrv_delete(bs);
352 if (ret < 0) {
353 return NULL;
354 }
355
bellardea2384d2004-08-01 21:59:26 +0000356 score_max = 0;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200357 drv = NULL;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100358 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
bellard83f64092006-08-01 16:21:11 +0000359 if (drv1->bdrv_probe) {
360 score = drv1->bdrv_probe(buf, ret, filename);
361 if (score > score_max) {
362 score_max = score;
363 drv = drv1;
364 }
bellardea2384d2004-08-01 21:59:26 +0000365 }
366 }
367 return drv;
368}
369
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100370/**
371 * Set the current 'total_sectors' value
372 */
373static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
374{
375 BlockDriver *drv = bs->drv;
376
Nicholas Bellinger396759a2010-05-17 09:46:04 -0700377 /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
378 if (bs->sg)
379 return 0;
380
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100381 /* query actual device if possible, otherwise just trust the hint */
382 if (drv->bdrv_getlength) {
383 int64_t length = drv->bdrv_getlength(bs);
384 if (length < 0) {
385 return length;
386 }
387 hint = length >> BDRV_SECTOR_BITS;
388 }
389
390 bs->total_sectors = hint;
391 return 0;
392}
393
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200394/*
Kevin Wolf57915332010-04-14 15:24:50 +0200395 * Common part for opening disk images and files
396 */
397static int bdrv_open_common(BlockDriverState *bs, const char *filename,
398 int flags, BlockDriver *drv)
399{
400 int ret, open_flags;
401
402 assert(drv != NULL);
403
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200404 bs->file = NULL;
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100405 bs->total_sectors = 0;
Kevin Wolf57915332010-04-14 15:24:50 +0200406 bs->encrypted = 0;
407 bs->valid_key = 0;
408 bs->open_flags = flags;
409 /* buffer_alignment defaulted to 512, drivers can change this value */
410 bs->buffer_alignment = 512;
411
412 pstrcpy(bs->filename, sizeof(bs->filename), filename);
413
414 if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {
415 return -ENOTSUP;
416 }
417
418 bs->drv = drv;
419 bs->opaque = qemu_mallocz(drv->instance_size);
420
421 /*
422 * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a
423 * write cache to the guest. We do need the fdatasync to flush
424 * out transactions for block allocations, and we maybe have a
425 * volatile write cache in our backing device to deal with.
426 */
427 if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE))
428 bs->enable_write_cache = 1;
429
430 /*
431 * Clear flags that are internal to the block layer before opening the
432 * image.
433 */
434 open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
435
436 /*
437 * Snapshots should be writeable.
438 */
439 if (bs->is_temporary) {
440 open_flags |= BDRV_O_RDWR;
441 }
442
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200443 /* Open the image, either directly or using a protocol */
444 if (drv->bdrv_file_open) {
445 ret = drv->bdrv_file_open(bs, filename, open_flags);
446 } else {
447 ret = bdrv_file_open(&bs->file, filename, open_flags);
448 if (ret >= 0) {
449 ret = drv->bdrv_open(bs, open_flags);
450 }
451 }
452
Kevin Wolf57915332010-04-14 15:24:50 +0200453 if (ret < 0) {
454 goto free_and_fail;
455 }
456
457 bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR);
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100458
459 ret = refresh_total_sectors(bs, bs->total_sectors);
460 if (ret < 0) {
461 goto free_and_fail;
Kevin Wolf57915332010-04-14 15:24:50 +0200462 }
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100463
Kevin Wolf57915332010-04-14 15:24:50 +0200464#ifndef _WIN32
465 if (bs->is_temporary) {
466 unlink(filename);
467 }
468#endif
469 return 0;
470
471free_and_fail:
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200472 if (bs->file) {
473 bdrv_delete(bs->file);
474 bs->file = NULL;
475 }
Kevin Wolf57915332010-04-14 15:24:50 +0200476 qemu_free(bs->opaque);
477 bs->opaque = NULL;
478 bs->drv = NULL;
479 return ret;
480}
481
482/*
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200483 * Opens a file using a protocol (file, host_device, nbd, ...)
484 */
bellard83f64092006-08-01 16:21:11 +0000485int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
bellardb3380822004-03-14 21:38:54 +0000486{
bellard83f64092006-08-01 16:21:11 +0000487 BlockDriverState *bs;
Christoph Hellwig6db95602010-04-05 16:53:57 +0200488 BlockDriver *drv;
bellard83f64092006-08-01 16:21:11 +0000489 int ret;
490
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900491 drv = bdrv_find_protocol(filename);
Christoph Hellwig6db95602010-04-05 16:53:57 +0200492 if (!drv) {
493 return -ENOENT;
494 }
495
bellard83f64092006-08-01 16:21:11 +0000496 bs = bdrv_new("");
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200497 ret = bdrv_open_common(bs, filename, flags, drv);
bellard83f64092006-08-01 16:21:11 +0000498 if (ret < 0) {
499 bdrv_delete(bs);
500 return ret;
bellard3b0d4f62005-10-30 18:30:10 +0000501 }
aliguori71d07702009-03-03 17:37:16 +0000502 bs->growable = 1;
bellard83f64092006-08-01 16:21:11 +0000503 *pbs = bs;
504 return 0;
bellardea2384d2004-08-01 21:59:26 +0000505}
bellardfc01f7e2003-06-30 10:03:06 +0000506
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200507/*
508 * Opens a disk image (raw, qcow2, vmdk, ...)
509 */
Kevin Wolfd6e90982010-03-31 14:40:27 +0200510int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
511 BlockDriver *drv)
bellardea2384d2004-08-01 21:59:26 +0000512{
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200513 int ret;
bellard712e7872005-04-28 21:09:32 +0000514
bellard83f64092006-08-01 16:21:11 +0000515 if (flags & BDRV_O_SNAPSHOT) {
bellardea2384d2004-08-01 21:59:26 +0000516 BlockDriverState *bs1;
517 int64_t total_size;
aliguori7c96d462008-09-12 17:54:13 +0000518 int is_protocol = 0;
Kevin Wolf91a073a2009-05-27 14:48:06 +0200519 BlockDriver *bdrv_qcow2;
520 QEMUOptionParameter *options;
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200521 char tmp_filename[PATH_MAX];
522 char backing_filename[PATH_MAX];
ths3b46e622007-09-17 08:09:54 +0000523
bellardea2384d2004-08-01 21:59:26 +0000524 /* if snapshot, we create a temporary backing file and open it
525 instead of opening 'filename' directly */
526
527 /* if there is a backing file, use it */
528 bs1 = bdrv_new("");
Kevin Wolfd6e90982010-03-31 14:40:27 +0200529 ret = bdrv_open(bs1, filename, 0, drv);
aliguori51d7c002009-03-05 23:00:29 +0000530 if (ret < 0) {
bellardea2384d2004-08-01 21:59:26 +0000531 bdrv_delete(bs1);
aliguori51d7c002009-03-05 23:00:29 +0000532 return ret;
bellardea2384d2004-08-01 21:59:26 +0000533 }
Jes Sorensen3e829902010-05-27 16:20:30 +0200534 total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK;
aliguori7c96d462008-09-12 17:54:13 +0000535
536 if (bs1->drv && bs1->drv->protocol_name)
537 is_protocol = 1;
538
bellardea2384d2004-08-01 21:59:26 +0000539 bdrv_delete(bs1);
ths3b46e622007-09-17 08:09:54 +0000540
bellardea2384d2004-08-01 21:59:26 +0000541 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
aliguori7c96d462008-09-12 17:54:13 +0000542
543 /* Real path is meaningless for protocols */
544 if (is_protocol)
545 snprintf(backing_filename, sizeof(backing_filename),
546 "%s", filename);
Kirill A. Shutemov114cdfa2009-12-25 18:19:22 +0000547 else if (!realpath(filename, backing_filename))
548 return -errno;
aliguori7c96d462008-09-12 17:54:13 +0000549
Kevin Wolf91a073a2009-05-27 14:48:06 +0200550 bdrv_qcow2 = bdrv_find_format("qcow2");
551 options = parse_option_parameters("", bdrv_qcow2->create_options, NULL);
552
Jes Sorensen3e829902010-05-27 16:20:30 +0200553 set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size);
Kevin Wolf91a073a2009-05-27 14:48:06 +0200554 set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename);
555 if (drv) {
556 set_option_parameter(options, BLOCK_OPT_BACKING_FMT,
557 drv->format_name);
558 }
559
560 ret = bdrv_create(bdrv_qcow2, tmp_filename, options);
Jan Kiszkad7487682010-04-29 18:24:50 +0200561 free_option_parameters(options);
aliguori51d7c002009-03-05 23:00:29 +0000562 if (ret < 0) {
563 return ret;
bellardea2384d2004-08-01 21:59:26 +0000564 }
Kevin Wolf91a073a2009-05-27 14:48:06 +0200565
bellardea2384d2004-08-01 21:59:26 +0000566 filename = tmp_filename;
Kevin Wolf91a073a2009-05-27 14:48:06 +0200567 drv = bdrv_qcow2;
bellardea2384d2004-08-01 21:59:26 +0000568 bs->is_temporary = 1;
569 }
bellard712e7872005-04-28 21:09:32 +0000570
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200571 /* Find the right image format driver */
Christoph Hellwig6db95602010-04-05 16:53:57 +0200572 if (!drv) {
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200573 drv = find_image_format(filename);
aliguori51d7c002009-03-05 23:00:29 +0000574 }
Christoph Hellwig69873072010-01-20 18:13:25 +0100575
aliguori51d7c002009-03-05 23:00:29 +0000576 if (!drv) {
577 ret = -ENOENT;
578 goto unlink_and_fail;
bellardea2384d2004-08-01 21:59:26 +0000579 }
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200580
581 /* Open the image */
582 ret = bdrv_open_common(bs, filename, flags, drv);
583 if (ret < 0) {
Christoph Hellwig69873072010-01-20 18:13:25 +0100584 goto unlink_and_fail;
585 }
586
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200587 /* If there is a backing file, use it */
588 if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') {
589 char backing_filename[PATH_MAX];
590 int back_flags;
591 BlockDriver *back_drv = NULL;
592
593 bs->backing_hd = bdrv_new("");
594 path_combine(backing_filename, sizeof(backing_filename),
595 filename, bs->backing_file);
596 if (bs->backing_format[0] != '\0')
597 back_drv = bdrv_find_format(bs->backing_format);
598
599 /* backing files always opened read-only */
600 back_flags =
601 flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
602
603 ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv);
604 if (ret < 0) {
605 bdrv_close(bs);
606 return ret;
607 }
608 if (bs->is_temporary) {
609 bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR);
610 } else {
611 /* base image inherits from "parent" */
612 bs->backing_hd->keep_read_only = bs->keep_read_only;
613 }
614 }
615
616 if (!bdrv_key_required(bs)) {
617 /* call the change callback */
618 bs->media_changed = 1;
619 if (bs->change_cb)
620 bs->change_cb(bs->change_opaque);
621 }
622
623 return 0;
624
625unlink_and_fail:
626 if (bs->is_temporary) {
627 unlink(filename);
628 }
629 return ret;
630}
631
bellardfc01f7e2003-06-30 10:03:06 +0000632void bdrv_close(BlockDriverState *bs)
633{
bellard19cb3732006-08-19 11:45:59 +0000634 if (bs->drv) {
Markus Armbrusterf9092b12010-06-25 10:33:39 +0200635 if (bs == bs_snapshots) {
636 bs_snapshots = NULL;
637 }
Stefan Hajnoczi557df6a2010-04-17 10:49:06 +0100638 if (bs->backing_hd) {
bellardea2384d2004-08-01 21:59:26 +0000639 bdrv_delete(bs->backing_hd);
Stefan Hajnoczi557df6a2010-04-17 10:49:06 +0100640 bs->backing_hd = NULL;
641 }
bellardea2384d2004-08-01 21:59:26 +0000642 bs->drv->bdrv_close(bs);
643 qemu_free(bs->opaque);
644#ifdef _WIN32
645 if (bs->is_temporary) {
646 unlink(bs->filename);
647 }
bellard67b915a2004-03-31 23:37:16 +0000648#endif
bellardea2384d2004-08-01 21:59:26 +0000649 bs->opaque = NULL;
650 bs->drv = NULL;
bellardb3380822004-03-14 21:38:54 +0000651
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200652 if (bs->file != NULL) {
653 bdrv_close(bs->file);
654 }
655
bellardb3380822004-03-14 21:38:54 +0000656 /* call the change callback */
bellard19cb3732006-08-19 11:45:59 +0000657 bs->media_changed = 1;
bellardb3380822004-03-14 21:38:54 +0000658 if (bs->change_cb)
659 bs->change_cb(bs->change_opaque);
660 }
661}
662
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +0900663void bdrv_close_all(void)
664{
665 BlockDriverState *bs;
666
667 QTAILQ_FOREACH(bs, &bdrv_states, list) {
668 bdrv_close(bs);
669 }
670}
671
bellardb3380822004-03-14 21:38:54 +0000672void bdrv_delete(BlockDriverState *bs)
673{
Markus Armbruster18846de2010-06-29 16:58:30 +0200674 assert(!bs->peer);
675
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +0100676 /* remove from list, if necessary */
677 if (bs->device_name[0] != '\0') {
678 QTAILQ_REMOVE(&bdrv_states, bs, list);
679 }
aurel3234c6f052008-04-08 19:51:21 +0000680
bellardb3380822004-03-14 21:38:54 +0000681 bdrv_close(bs);
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200682 if (bs->file != NULL) {
683 bdrv_delete(bs->file);
684 }
685
Markus Armbrusterf9092b12010-06-25 10:33:39 +0200686 assert(bs != bs_snapshots);
bellardb3380822004-03-14 21:38:54 +0000687 qemu_free(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000688}
689
Markus Armbruster18846de2010-06-29 16:58:30 +0200690int bdrv_attach(BlockDriverState *bs, DeviceState *qdev)
691{
692 if (bs->peer) {
693 return -EBUSY;
694 }
695 bs->peer = qdev;
696 return 0;
697}
698
699void bdrv_detach(BlockDriverState *bs, DeviceState *qdev)
700{
701 assert(bs->peer == qdev);
702 bs->peer = NULL;
703}
704
705DeviceState *bdrv_get_attached(BlockDriverState *bs)
706{
707 return bs->peer;
708}
709
aliguorie97fc192009-04-21 23:11:50 +0000710/*
711 * Run consistency checks on an image
712 *
713 * Returns the number of errors or -errno when an internal error occurs
714 */
715int bdrv_check(BlockDriverState *bs)
716{
717 if (bs->drv->bdrv_check == NULL) {
718 return -ENOTSUP;
719 }
720
721 return bs->drv->bdrv_check(bs);
722}
723
bellard33e39632003-07-06 17:15:21 +0000724/* commit COW file into the raw image */
725int bdrv_commit(BlockDriverState *bs)
726{
bellard19cb3732006-08-19 11:45:59 +0000727 BlockDriver *drv = bs->drv;
bellard83f64092006-08-01 16:21:11 +0000728 int64_t i, total_sectors;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200729 int n, j, ro, open_flags;
730 int ret = 0, rw_ret = 0;
Jes Sorenseneb5a3162010-05-27 16:20:31 +0200731 unsigned char sector[BDRV_SECTOR_SIZE];
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200732 char filename[1024];
733 BlockDriverState *bs_rw, *bs_ro;
bellard33e39632003-07-06 17:15:21 +0000734
bellard19cb3732006-08-19 11:45:59 +0000735 if (!drv)
736 return -ENOMEDIUM;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200737
738 if (!bs->backing_hd) {
739 return -ENOTSUP;
bellard33e39632003-07-06 17:15:21 +0000740 }
741
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200742 if (bs->backing_hd->keep_read_only) {
743 return -EACCES;
744 }
745
746 ro = bs->backing_hd->read_only;
747 strncpy(filename, bs->backing_hd->filename, sizeof(filename));
748 open_flags = bs->backing_hd->open_flags;
749
750 if (ro) {
751 /* re-open as RW */
752 bdrv_delete(bs->backing_hd);
753 bs->backing_hd = NULL;
754 bs_rw = bdrv_new("");
Kevin Wolfc3349192010-05-06 16:34:56 +0200755 rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR, drv);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200756 if (rw_ret < 0) {
757 bdrv_delete(bs_rw);
758 /* try to re-open read-only */
759 bs_ro = bdrv_new("");
Kevin Wolfc3349192010-05-06 16:34:56 +0200760 ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, drv);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200761 if (ret < 0) {
762 bdrv_delete(bs_ro);
763 /* drive not functional anymore */
764 bs->drv = NULL;
765 return ret;
766 }
767 bs->backing_hd = bs_ro;
768 return rw_ret;
769 }
770 bs->backing_hd = bs_rw;
bellard33e39632003-07-06 17:15:21 +0000771 }
bellardea2384d2004-08-01 21:59:26 +0000772
Jan Kiszka6ea44302009-11-30 18:21:19 +0100773 total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000774 for (i = 0; i < total_sectors;) {
bellard19cb3732006-08-19 11:45:59 +0000775 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
bellardea2384d2004-08-01 21:59:26 +0000776 for(j = 0; j < n; j++) {
777 if (bdrv_read(bs, i, sector, 1) != 0) {
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200778 ret = -EIO;
779 goto ro_cleanup;
bellardea2384d2004-08-01 21:59:26 +0000780 }
781
782 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200783 ret = -EIO;
784 goto ro_cleanup;
bellardea2384d2004-08-01 21:59:26 +0000785 }
786 i++;
787 }
788 } else {
789 i += n;
790 }
791 }
bellard95389c82005-12-18 18:28:15 +0000792
Christoph Hellwig1d449522010-01-17 12:32:30 +0100793 if (drv->bdrv_make_empty) {
794 ret = drv->bdrv_make_empty(bs);
795 bdrv_flush(bs);
796 }
bellard95389c82005-12-18 18:28:15 +0000797
Christoph Hellwig3f5075a2010-01-12 13:49:23 +0100798 /*
799 * Make sure all data we wrote to the backing device is actually
800 * stable on disk.
801 */
802 if (bs->backing_hd)
803 bdrv_flush(bs->backing_hd);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200804
805ro_cleanup:
806
807 if (ro) {
808 /* re-open as RO */
809 bdrv_delete(bs->backing_hd);
810 bs->backing_hd = NULL;
811 bs_ro = bdrv_new("");
Kevin Wolfc3349192010-05-06 16:34:56 +0200812 ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, drv);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200813 if (ret < 0) {
814 bdrv_delete(bs_ro);
815 /* drive not functional anymore */
816 bs->drv = NULL;
817 return ret;
818 }
819 bs->backing_hd = bs_ro;
820 bs->backing_hd->keep_read_only = 0;
821 }
822
Christoph Hellwig1d449522010-01-17 12:32:30 +0100823 return ret;
bellard33e39632003-07-06 17:15:21 +0000824}
825
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200826void bdrv_commit_all(void)
827{
828 BlockDriverState *bs;
829
830 QTAILQ_FOREACH(bs, &bdrv_states, list) {
831 bdrv_commit(bs);
832 }
833}
834
Kevin Wolf756e6732010-01-12 12:55:17 +0100835/*
836 * Return values:
837 * 0 - success
838 * -EINVAL - backing format specified, but no file
839 * -ENOSPC - can't update the backing file because no space is left in the
840 * image file header
841 * -ENOTSUP - format driver doesn't support changing the backing file
842 */
843int bdrv_change_backing_file(BlockDriverState *bs,
844 const char *backing_file, const char *backing_fmt)
845{
846 BlockDriver *drv = bs->drv;
847
848 if (drv->bdrv_change_backing_file != NULL) {
849 return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
850 } else {
851 return -ENOTSUP;
852 }
853}
854
aliguori71d07702009-03-03 17:37:16 +0000855static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
856 size_t size)
857{
858 int64_t len;
859
860 if (!bdrv_is_inserted(bs))
861 return -ENOMEDIUM;
862
863 if (bs->growable)
864 return 0;
865
866 len = bdrv_getlength(bs);
867
Kevin Wolffbb7b4e2009-05-08 14:47:24 +0200868 if (offset < 0)
869 return -EIO;
870
871 if ((offset > len) || (len - offset < size))
aliguori71d07702009-03-03 17:37:16 +0000872 return -EIO;
873
874 return 0;
875}
876
877static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
878 int nb_sectors)
879{
Jes Sorenseneb5a3162010-05-27 16:20:31 +0200880 return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE,
881 nb_sectors * BDRV_SECTOR_SIZE);
aliguori71d07702009-03-03 17:37:16 +0000882}
883
bellard19cb3732006-08-19 11:45:59 +0000884/* return < 0 if error. See bdrv_write() for the return codes */
ths5fafdf22007-09-16 21:08:06 +0000885int bdrv_read(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000886 uint8_t *buf, int nb_sectors)
887{
bellardea2384d2004-08-01 21:59:26 +0000888 BlockDriver *drv = bs->drv;
889
bellard19cb3732006-08-19 11:45:59 +0000890 if (!drv)
891 return -ENOMEDIUM;
aliguori71d07702009-03-03 17:37:16 +0000892 if (bdrv_check_request(bs, sector_num, nb_sectors))
893 return -EIO;
bellardb3380822004-03-14 21:38:54 +0000894
aliguorieda578e2009-03-12 19:57:16 +0000895 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
bellardfc01f7e2003-06-30 10:03:06 +0000896}
897
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +0200898static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num,
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100899 int nb_sectors, int dirty)
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +0200900{
901 int64_t start, end;
Jan Kiszkac6d22832009-11-30 18:21:20 +0100902 unsigned long val, idx, bit;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100903
Jan Kiszka6ea44302009-11-30 18:21:19 +0100904 start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkac6d22832009-11-30 18:21:20 +0100905 end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100906
907 for (; start <= end; start++) {
Jan Kiszkac6d22832009-11-30 18:21:20 +0100908 idx = start / (sizeof(unsigned long) * 8);
909 bit = start % (sizeof(unsigned long) * 8);
910 val = bs->dirty_bitmap[idx];
911 if (dirty) {
Liran Schouraaa0eb72010-01-26 10:31:48 +0200912 if (!(val & (1 << bit))) {
913 bs->dirty_count++;
914 val |= 1 << bit;
915 }
Jan Kiszkac6d22832009-11-30 18:21:20 +0100916 } else {
Liran Schouraaa0eb72010-01-26 10:31:48 +0200917 if (val & (1 << bit)) {
918 bs->dirty_count--;
919 val &= ~(1 << bit);
920 }
Jan Kiszkac6d22832009-11-30 18:21:20 +0100921 }
922 bs->dirty_bitmap[idx] = val;
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +0200923 }
924}
925
ths5fafdf22007-09-16 21:08:06 +0000926/* Return < 0 if error. Important errors are:
bellard19cb3732006-08-19 11:45:59 +0000927 -EIO generic I/O error (may happen for all errors)
928 -ENOMEDIUM No media inserted.
929 -EINVAL Invalid sector number or nb_sectors
930 -EACCES Trying to write a read-only device
931*/
ths5fafdf22007-09-16 21:08:06 +0000932int bdrv_write(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000933 const uint8_t *buf, int nb_sectors)
934{
bellard83f64092006-08-01 16:21:11 +0000935 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +0000936 if (!bs->drv)
937 return -ENOMEDIUM;
bellard0849bf02003-06-30 23:17:31 +0000938 if (bs->read_only)
bellard19cb3732006-08-19 11:45:59 +0000939 return -EACCES;
aliguori71d07702009-03-03 17:37:16 +0000940 if (bdrv_check_request(bs, sector_num, nb_sectors))
941 return -EIO;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100942
Jan Kiszkac6d22832009-11-30 18:21:20 +0100943 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +0200944 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
945 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100946
Kevin Wolf294cc352010-04-28 14:34:01 +0200947 if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
948 bs->wr_highest_sector = sector_num + nb_sectors - 1;
949 }
950
aliguori42fb2802009-01-15 20:43:39 +0000951 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
bellard83f64092006-08-01 16:21:11 +0000952}
953
aliguorieda578e2009-03-12 19:57:16 +0000954int bdrv_pread(BlockDriverState *bs, int64_t offset,
955 void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000956{
Jan Kiszka6ea44302009-11-30 18:21:19 +0100957 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
bellard83f64092006-08-01 16:21:11 +0000958 int len, nb_sectors, count;
959 int64_t sector_num;
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100960 int ret;
bellard83f64092006-08-01 16:21:11 +0000961
962 count = count1;
963 /* first read to align to sector start */
Jan Kiszka6ea44302009-11-30 18:21:19 +0100964 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
bellard83f64092006-08-01 16:21:11 +0000965 if (len > count)
966 len = count;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100967 sector_num = offset >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000968 if (len > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100969 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
970 return ret;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100971 memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
bellard83f64092006-08-01 16:21:11 +0000972 count -= len;
973 if (count == 0)
974 return count1;
975 sector_num++;
976 buf += len;
977 }
978
979 /* read the sectors "in place" */
Jan Kiszka6ea44302009-11-30 18:21:19 +0100980 nb_sectors = count >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000981 if (nb_sectors > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100982 if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0)
983 return ret;
bellard83f64092006-08-01 16:21:11 +0000984 sector_num += nb_sectors;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100985 len = nb_sectors << BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000986 buf += len;
987 count -= len;
988 }
989
990 /* add data from the last sector */
991 if (count > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100992 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
993 return ret;
bellard83f64092006-08-01 16:21:11 +0000994 memcpy(buf, tmp_buf, count);
995 }
996 return count1;
997}
998
aliguorieda578e2009-03-12 19:57:16 +0000999int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
1000 const void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +00001001{
Jan Kiszka6ea44302009-11-30 18:21:19 +01001002 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
bellard83f64092006-08-01 16:21:11 +00001003 int len, nb_sectors, count;
1004 int64_t sector_num;
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001005 int ret;
bellard83f64092006-08-01 16:21:11 +00001006
1007 count = count1;
1008 /* first write to align to sector start */
Jan Kiszka6ea44302009-11-30 18:21:19 +01001009 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
bellard83f64092006-08-01 16:21:11 +00001010 if (len > count)
1011 len = count;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001012 sector_num = offset >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001013 if (len > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001014 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1015 return ret;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001016 memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len);
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001017 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
1018 return ret;
bellard83f64092006-08-01 16:21:11 +00001019 count -= len;
1020 if (count == 0)
1021 return count1;
1022 sector_num++;
1023 buf += len;
1024 }
1025
1026 /* write the sectors "in place" */
Jan Kiszka6ea44302009-11-30 18:21:19 +01001027 nb_sectors = count >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001028 if (nb_sectors > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001029 if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0)
1030 return ret;
bellard83f64092006-08-01 16:21:11 +00001031 sector_num += nb_sectors;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001032 len = nb_sectors << BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001033 buf += len;
1034 count -= len;
1035 }
1036
1037 /* add data from the last sector */
1038 if (count > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001039 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1040 return ret;
bellard83f64092006-08-01 16:21:11 +00001041 memcpy(tmp_buf, buf, count);
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001042 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
1043 return ret;
bellard83f64092006-08-01 16:21:11 +00001044 }
1045 return count1;
1046}
bellard83f64092006-08-01 16:21:11 +00001047
Kevin Wolff08145f2010-06-16 16:38:15 +02001048/*
1049 * Writes to the file and ensures that no writes are reordered across this
1050 * request (acts as a barrier)
1051 *
1052 * Returns 0 on success, -errno in error cases.
1053 */
1054int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset,
1055 const void *buf, int count)
1056{
1057 int ret;
1058
1059 ret = bdrv_pwrite(bs, offset, buf, count);
1060 if (ret < 0) {
1061 return ret;
1062 }
1063
1064 /* No flush needed for cache=writethrough, it uses O_DSYNC */
1065 if ((bs->open_flags & BDRV_O_CACHE_MASK) != 0) {
1066 bdrv_flush(bs);
1067 }
1068
1069 return 0;
1070}
1071
1072/*
1073 * Writes to the file and ensures that no writes are reordered across this
1074 * request (acts as a barrier)
1075 *
1076 * Returns 0 on success, -errno in error cases.
1077 */
1078int bdrv_write_sync(BlockDriverState *bs, int64_t sector_num,
1079 const uint8_t *buf, int nb_sectors)
1080{
1081 return bdrv_pwrite_sync(bs, BDRV_SECTOR_SIZE * sector_num,
1082 buf, BDRV_SECTOR_SIZE * nb_sectors);
1083}
1084
bellard83f64092006-08-01 16:21:11 +00001085/**
bellard83f64092006-08-01 16:21:11 +00001086 * Truncate file to 'offset' bytes (needed only for file protocols)
1087 */
1088int bdrv_truncate(BlockDriverState *bs, int64_t offset)
1089{
1090 BlockDriver *drv = bs->drv;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001091 int ret;
bellard83f64092006-08-01 16:21:11 +00001092 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001093 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +00001094 if (!drv->bdrv_truncate)
1095 return -ENOTSUP;
Naphtali Sprei59f26892009-10-26 16:25:16 +02001096 if (bs->read_only)
1097 return -EACCES;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001098 ret = drv->bdrv_truncate(bs, offset);
1099 if (ret == 0) {
1100 ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
1101 }
1102 return ret;
bellard83f64092006-08-01 16:21:11 +00001103}
1104
1105/**
1106 * Length of a file in bytes. Return < 0 if error or unknown.
1107 */
1108int64_t bdrv_getlength(BlockDriverState *bs)
1109{
1110 BlockDriver *drv = bs->drv;
1111 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001112 return -ENOMEDIUM;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001113
1114 /* Fixed size devices use the total_sectors value for speed instead of
1115 issuing a length query (like lseek) on each call. Also, legacy block
1116 drivers don't provide a bdrv_getlength function and must use
1117 total_sectors. */
1118 if (!bs->growable || !drv->bdrv_getlength) {
Jan Kiszka6ea44302009-11-30 18:21:19 +01001119 return bs->total_sectors * BDRV_SECTOR_SIZE;
bellard83f64092006-08-01 16:21:11 +00001120 }
1121 return drv->bdrv_getlength(bs);
bellardfc01f7e2003-06-30 10:03:06 +00001122}
1123
bellard19cb3732006-08-19 11:45:59 +00001124/* return 0 as number of sectors if no device present or error */
ths96b8f132007-12-17 01:35:20 +00001125void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
bellardfc01f7e2003-06-30 10:03:06 +00001126{
bellard19cb3732006-08-19 11:45:59 +00001127 int64_t length;
1128 length = bdrv_getlength(bs);
1129 if (length < 0)
1130 length = 0;
1131 else
Jan Kiszka6ea44302009-11-30 18:21:19 +01001132 length = length >> BDRV_SECTOR_BITS;
bellard19cb3732006-08-19 11:45:59 +00001133 *nb_sectors_ptr = length;
bellardfc01f7e2003-06-30 10:03:06 +00001134}
bellardcf989512004-02-16 21:56:36 +00001135
aliguorif3d54fc2008-11-25 21:50:24 +00001136struct partition {
1137 uint8_t boot_ind; /* 0x80 - active */
1138 uint8_t head; /* starting head */
1139 uint8_t sector; /* starting sector */
1140 uint8_t cyl; /* starting cylinder */
1141 uint8_t sys_ind; /* What partition type */
1142 uint8_t end_head; /* end head */
1143 uint8_t end_sector; /* end sector */
1144 uint8_t end_cyl; /* end cylinder */
1145 uint32_t start_sect; /* starting sector counting from 0 */
1146 uint32_t nr_sects; /* nr of sectors in partition */
1147} __attribute__((packed));
1148
1149/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
1150static int guess_disk_lchs(BlockDriverState *bs,
1151 int *pcylinders, int *pheads, int *psectors)
1152{
Jes Sorenseneb5a3162010-05-27 16:20:31 +02001153 uint8_t buf[BDRV_SECTOR_SIZE];
aliguorif3d54fc2008-11-25 21:50:24 +00001154 int ret, i, heads, sectors, cylinders;
1155 struct partition *p;
1156 uint32_t nr_sects;
blueswir1a38131b2008-12-05 17:56:40 +00001157 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +00001158
1159 bdrv_get_geometry(bs, &nb_sectors);
1160
1161 ret = bdrv_read(bs, 0, buf, 1);
1162 if (ret < 0)
1163 return -1;
1164 /* test msdos magic */
1165 if (buf[510] != 0x55 || buf[511] != 0xaa)
1166 return -1;
1167 for(i = 0; i < 4; i++) {
1168 p = ((struct partition *)(buf + 0x1be)) + i;
1169 nr_sects = le32_to_cpu(p->nr_sects);
1170 if (nr_sects && p->end_head) {
1171 /* We make the assumption that the partition terminates on
1172 a cylinder boundary */
1173 heads = p->end_head + 1;
1174 sectors = p->end_sector & 63;
1175 if (sectors == 0)
1176 continue;
1177 cylinders = nb_sectors / (heads * sectors);
1178 if (cylinders < 1 || cylinders > 16383)
1179 continue;
1180 *pheads = heads;
1181 *psectors = sectors;
1182 *pcylinders = cylinders;
1183#if 0
1184 printf("guessed geometry: LCHS=%d %d %d\n",
1185 cylinders, heads, sectors);
1186#endif
1187 return 0;
1188 }
1189 }
1190 return -1;
1191}
1192
1193void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
1194{
1195 int translation, lba_detected = 0;
1196 int cylinders, heads, secs;
blueswir1a38131b2008-12-05 17:56:40 +00001197 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +00001198
1199 /* if a geometry hint is available, use it */
1200 bdrv_get_geometry(bs, &nb_sectors);
1201 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
1202 translation = bdrv_get_translation_hint(bs);
1203 if (cylinders != 0) {
1204 *pcyls = cylinders;
1205 *pheads = heads;
1206 *psecs = secs;
1207 } else {
1208 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
1209 if (heads > 16) {
1210 /* if heads > 16, it means that a BIOS LBA
1211 translation was active, so the default
1212 hardware geometry is OK */
1213 lba_detected = 1;
1214 goto default_geometry;
1215 } else {
1216 *pcyls = cylinders;
1217 *pheads = heads;
1218 *psecs = secs;
1219 /* disable any translation to be in sync with
1220 the logical geometry */
1221 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
1222 bdrv_set_translation_hint(bs,
1223 BIOS_ATA_TRANSLATION_NONE);
1224 }
1225 }
1226 } else {
1227 default_geometry:
1228 /* if no geometry, use a standard physical disk geometry */
1229 cylinders = nb_sectors / (16 * 63);
1230
1231 if (cylinders > 16383)
1232 cylinders = 16383;
1233 else if (cylinders < 2)
1234 cylinders = 2;
1235 *pcyls = cylinders;
1236 *pheads = 16;
1237 *psecs = 63;
1238 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
1239 if ((*pcyls * *pheads) <= 131072) {
1240 bdrv_set_translation_hint(bs,
1241 BIOS_ATA_TRANSLATION_LARGE);
1242 } else {
1243 bdrv_set_translation_hint(bs,
1244 BIOS_ATA_TRANSLATION_LBA);
1245 }
1246 }
1247 }
1248 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
1249 }
1250}
1251
ths5fafdf22007-09-16 21:08:06 +00001252void bdrv_set_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +00001253 int cyls, int heads, int secs)
1254{
1255 bs->cyls = cyls;
1256 bs->heads = heads;
1257 bs->secs = secs;
1258}
1259
1260void bdrv_set_type_hint(BlockDriverState *bs, int type)
1261{
1262 bs->type = type;
1263 bs->removable = ((type == BDRV_TYPE_CDROM ||
1264 type == BDRV_TYPE_FLOPPY));
1265}
1266
bellard46d47672004-11-16 01:45:27 +00001267void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
1268{
1269 bs->translation = translation;
1270}
1271
ths5fafdf22007-09-16 21:08:06 +00001272void bdrv_get_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +00001273 int *pcyls, int *pheads, int *psecs)
1274{
1275 *pcyls = bs->cyls;
1276 *pheads = bs->heads;
1277 *psecs = bs->secs;
1278}
1279
1280int bdrv_get_type_hint(BlockDriverState *bs)
1281{
1282 return bs->type;
1283}
1284
bellard46d47672004-11-16 01:45:27 +00001285int bdrv_get_translation_hint(BlockDriverState *bs)
1286{
1287 return bs->translation;
1288}
1289
Markus Armbrusterabd7f682010-06-02 18:55:17 +02001290void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error,
1291 BlockErrorAction on_write_error)
1292{
1293 bs->on_read_error = on_read_error;
1294 bs->on_write_error = on_write_error;
1295}
1296
1297BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read)
1298{
1299 return is_read ? bs->on_read_error : bs->on_write_error;
1300}
1301
Markus Armbruster7d0d6952010-06-25 13:42:14 +02001302void bdrv_set_removable(BlockDriverState *bs, int removable)
1303{
1304 bs->removable = removable;
1305 if (removable && bs == bs_snapshots) {
1306 bs_snapshots = NULL;
1307 }
1308}
1309
bellardb3380822004-03-14 21:38:54 +00001310int bdrv_is_removable(BlockDriverState *bs)
1311{
1312 return bs->removable;
1313}
1314
1315int bdrv_is_read_only(BlockDriverState *bs)
1316{
1317 return bs->read_only;
1318}
1319
ths985a03b2007-12-24 16:10:43 +00001320int bdrv_is_sg(BlockDriverState *bs)
1321{
1322 return bs->sg;
1323}
1324
Christoph Hellwige900a7b2009-09-04 19:01:15 +02001325int bdrv_enable_write_cache(BlockDriverState *bs)
1326{
1327 return bs->enable_write_cache;
1328}
1329
bellard19cb3732006-08-19 11:45:59 +00001330/* XXX: no longer used */
ths5fafdf22007-09-16 21:08:06 +00001331void bdrv_set_change_cb(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +00001332 void (*change_cb)(void *opaque), void *opaque)
1333{
1334 bs->change_cb = change_cb;
1335 bs->change_opaque = opaque;
1336}
1337
bellardea2384d2004-08-01 21:59:26 +00001338int bdrv_is_encrypted(BlockDriverState *bs)
1339{
1340 if (bs->backing_hd && bs->backing_hd->encrypted)
1341 return 1;
1342 return bs->encrypted;
1343}
1344
aliguoric0f4ce72009-03-05 23:01:01 +00001345int bdrv_key_required(BlockDriverState *bs)
1346{
1347 BlockDriverState *backing_hd = bs->backing_hd;
1348
1349 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
1350 return 1;
1351 return (bs->encrypted && !bs->valid_key);
1352}
1353
bellardea2384d2004-08-01 21:59:26 +00001354int bdrv_set_key(BlockDriverState *bs, const char *key)
1355{
1356 int ret;
1357 if (bs->backing_hd && bs->backing_hd->encrypted) {
1358 ret = bdrv_set_key(bs->backing_hd, key);
1359 if (ret < 0)
1360 return ret;
1361 if (!bs->encrypted)
1362 return 0;
1363 }
Shahar Havivifd04a2a2010-03-06 00:26:13 +02001364 if (!bs->encrypted) {
1365 return -EINVAL;
1366 } else if (!bs->drv || !bs->drv->bdrv_set_key) {
1367 return -ENOMEDIUM;
1368 }
aliguoric0f4ce72009-03-05 23:01:01 +00001369 ret = bs->drv->bdrv_set_key(bs, key);
aliguoribb5fc202009-03-05 23:01:15 +00001370 if (ret < 0) {
1371 bs->valid_key = 0;
1372 } else if (!bs->valid_key) {
1373 bs->valid_key = 1;
1374 /* call the change callback now, we skipped it on open */
1375 bs->media_changed = 1;
1376 if (bs->change_cb)
1377 bs->change_cb(bs->change_opaque);
1378 }
aliguoric0f4ce72009-03-05 23:01:01 +00001379 return ret;
bellardea2384d2004-08-01 21:59:26 +00001380}
1381
1382void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
1383{
bellard19cb3732006-08-19 11:45:59 +00001384 if (!bs->drv) {
bellardea2384d2004-08-01 21:59:26 +00001385 buf[0] = '\0';
1386 } else {
1387 pstrcpy(buf, buf_size, bs->drv->format_name);
1388 }
1389}
1390
ths5fafdf22007-09-16 21:08:06 +00001391void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
bellardea2384d2004-08-01 21:59:26 +00001392 void *opaque)
1393{
1394 BlockDriver *drv;
1395
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +01001396 QLIST_FOREACH(drv, &bdrv_drivers, list) {
bellardea2384d2004-08-01 21:59:26 +00001397 it(opaque, drv->format_name);
1398 }
1399}
1400
bellardb3380822004-03-14 21:38:54 +00001401BlockDriverState *bdrv_find(const char *name)
1402{
1403 BlockDriverState *bs;
1404
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001405 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1406 if (!strcmp(name, bs->device_name)) {
bellardb3380822004-03-14 21:38:54 +00001407 return bs;
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001408 }
bellardb3380822004-03-14 21:38:54 +00001409 }
1410 return NULL;
1411}
1412
Markus Armbruster2f399b02010-06-02 18:55:20 +02001413BlockDriverState *bdrv_next(BlockDriverState *bs)
1414{
1415 if (!bs) {
1416 return QTAILQ_FIRST(&bdrv_states);
1417 }
1418 return QTAILQ_NEXT(bs, list);
1419}
1420
aliguori51de9762009-03-05 23:00:43 +00001421void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
bellard81d09122004-07-14 17:21:37 +00001422{
1423 BlockDriverState *bs;
1424
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001425 QTAILQ_FOREACH(bs, &bdrv_states, list) {
aliguori51de9762009-03-05 23:00:43 +00001426 it(opaque, bs);
bellard81d09122004-07-14 17:21:37 +00001427 }
1428}
1429
bellardea2384d2004-08-01 21:59:26 +00001430const char *bdrv_get_device_name(BlockDriverState *bs)
1431{
1432 return bs->device_name;
1433}
1434
pbrook7a6cba62006-06-04 11:39:07 +00001435void bdrv_flush(BlockDriverState *bs)
1436{
Alexander Graf016f5cf2010-05-26 17:51:49 +02001437 if (bs->open_flags & BDRV_O_NO_FLUSH) {
1438 return;
1439 }
1440
Christoph Hellwig3f5075a2010-01-12 13:49:23 +01001441 if (bs->drv && bs->drv->bdrv_flush)
pbrook7a6cba62006-06-04 11:39:07 +00001442 bs->drv->bdrv_flush(bs);
pbrook7a6cba62006-06-04 11:39:07 +00001443}
1444
aliguoric6ca28d2008-10-06 13:55:43 +00001445void bdrv_flush_all(void)
1446{
1447 BlockDriverState *bs;
1448
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001449 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1450 if (bs->drv && !bdrv_is_read_only(bs) &&
1451 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) {
aliguoric6ca28d2008-10-06 13:55:43 +00001452 bdrv_flush(bs);
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001453 }
1454 }
aliguoric6ca28d2008-10-06 13:55:43 +00001455}
1456
Kevin Wolff2feebb2010-04-14 17:30:35 +02001457int bdrv_has_zero_init(BlockDriverState *bs)
1458{
1459 assert(bs->drv);
1460
1461 if (bs->drv->no_zero_init) {
1462 return 0;
1463 } else if (bs->file) {
1464 return bdrv_has_zero_init(bs->file);
1465 }
1466
1467 return 1;
1468}
1469
thsf58c7b32008-06-05 21:53:49 +00001470/*
1471 * Returns true iff the specified sector is present in the disk image. Drivers
1472 * not implementing the functionality are assumed to not support backing files,
1473 * hence all their sectors are reported as allocated.
1474 *
1475 * 'pnum' is set to the number of sectors (including and immediately following
1476 * the specified sector) that are known to be in the same
1477 * allocated/unallocated state.
1478 *
1479 * 'nb_sectors' is the max value 'pnum' should be set to.
1480 */
1481int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1482 int *pnum)
1483{
1484 int64_t n;
1485 if (!bs->drv->bdrv_is_allocated) {
1486 if (sector_num >= bs->total_sectors) {
1487 *pnum = 0;
1488 return 0;
1489 }
1490 n = bs->total_sectors - sector_num;
1491 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1492 return 1;
1493 }
1494 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1495}
1496
Luiz Capitulino2582bfe2010-02-03 12:41:01 -02001497void bdrv_mon_event(const BlockDriverState *bdrv,
1498 BlockMonEventAction action, int is_read)
1499{
1500 QObject *data;
1501 const char *action_str;
1502
1503 switch (action) {
1504 case BDRV_ACTION_REPORT:
1505 action_str = "report";
1506 break;
1507 case BDRV_ACTION_IGNORE:
1508 action_str = "ignore";
1509 break;
1510 case BDRV_ACTION_STOP:
1511 action_str = "stop";
1512 break;
1513 default:
1514 abort();
1515 }
1516
1517 data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
1518 bdrv->device_name,
1519 action_str,
1520 is_read ? "read" : "write");
1521 monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data);
1522
1523 qobject_decref(data);
1524}
1525
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001526static void bdrv_print_dict(QObject *obj, void *opaque)
bellardb3380822004-03-14 21:38:54 +00001527{
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001528 QDict *bs_dict;
1529 Monitor *mon = opaque;
1530
1531 bs_dict = qobject_to_qdict(obj);
1532
1533 monitor_printf(mon, "%s: type=%s removable=%d",
1534 qdict_get_str(bs_dict, "device"),
1535 qdict_get_str(bs_dict, "type"),
1536 qdict_get_bool(bs_dict, "removable"));
1537
1538 if (qdict_get_bool(bs_dict, "removable")) {
1539 monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked"));
1540 }
1541
1542 if (qdict_haskey(bs_dict, "inserted")) {
1543 QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted"));
1544
1545 monitor_printf(mon, " file=");
1546 monitor_print_filename(mon, qdict_get_str(qdict, "file"));
1547 if (qdict_haskey(qdict, "backing_file")) {
1548 monitor_printf(mon, " backing_file=");
1549 monitor_print_filename(mon, qdict_get_str(qdict, "backing_file"));
1550 }
1551 monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
1552 qdict_get_bool(qdict, "ro"),
1553 qdict_get_str(qdict, "drv"),
1554 qdict_get_bool(qdict, "encrypted"));
1555 } else {
1556 monitor_printf(mon, " [not inserted]");
1557 }
1558
1559 monitor_printf(mon, "\n");
1560}
1561
1562void bdrv_info_print(Monitor *mon, const QObject *data)
1563{
1564 qlist_iter(qobject_to_qlist(data), bdrv_print_dict, mon);
1565}
1566
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001567void bdrv_info(Monitor *mon, QObject **ret_data)
1568{
1569 QList *bs_list;
bellardb3380822004-03-14 21:38:54 +00001570 BlockDriverState *bs;
1571
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001572 bs_list = qlist_new();
1573
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001574 QTAILQ_FOREACH(bs, &bdrv_states, list) {
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001575 QObject *bs_obj;
1576 const char *type = "unknown";
1577
bellardb3380822004-03-14 21:38:54 +00001578 switch(bs->type) {
1579 case BDRV_TYPE_HD:
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001580 type = "hd";
bellardb3380822004-03-14 21:38:54 +00001581 break;
1582 case BDRV_TYPE_CDROM:
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001583 type = "cdrom";
bellardb3380822004-03-14 21:38:54 +00001584 break;
1585 case BDRV_TYPE_FLOPPY:
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001586 type = "floppy";
bellardb3380822004-03-14 21:38:54 +00001587 break;
1588 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001589
1590 bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': %s, "
1591 "'removable': %i, 'locked': %i }",
1592 bs->device_name, type, bs->removable,
1593 bs->locked);
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001594
bellard19cb3732006-08-19 11:45:59 +00001595 if (bs->drv) {
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001596 QObject *obj;
1597 QDict *bs_dict = qobject_to_qdict(bs_obj);
1598
1599 obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, "
1600 "'encrypted': %i }",
1601 bs->filename, bs->read_only,
1602 bs->drv->format_name,
1603 bdrv_is_encrypted(bs));
thsfef30742006-12-22 14:11:32 +00001604 if (bs->backing_file[0] != '\0') {
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001605 QDict *qdict = qobject_to_qdict(obj);
1606 qdict_put(qdict, "backing_file",
1607 qstring_from_str(bs->backing_file));
aliguori376253e2009-03-05 23:01:23 +00001608 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001609
1610 qdict_put_obj(bs_dict, "inserted", obj);
bellardb3380822004-03-14 21:38:54 +00001611 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001612 qlist_append_obj(bs_list, bs_obj);
bellardb3380822004-03-14 21:38:54 +00001613 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001614
1615 *ret_data = QOBJECT(bs_list);
bellardb3380822004-03-14 21:38:54 +00001616}
thsa36e69d2007-12-02 05:18:19 +00001617
Luiz Capitulino218a5362009-12-10 17:16:07 -02001618static void bdrv_stats_iter(QObject *data, void *opaque)
thsa36e69d2007-12-02 05:18:19 +00001619{
Luiz Capitulino218a5362009-12-10 17:16:07 -02001620 QDict *qdict;
1621 Monitor *mon = opaque;
1622
1623 qdict = qobject_to_qdict(data);
1624 monitor_printf(mon, "%s:", qdict_get_str(qdict, "device"));
1625
1626 qdict = qobject_to_qdict(qdict_get(qdict, "stats"));
1627 monitor_printf(mon, " rd_bytes=%" PRId64
1628 " wr_bytes=%" PRId64
1629 " rd_operations=%" PRId64
1630 " wr_operations=%" PRId64
1631 "\n",
1632 qdict_get_int(qdict, "rd_bytes"),
1633 qdict_get_int(qdict, "wr_bytes"),
1634 qdict_get_int(qdict, "rd_operations"),
1635 qdict_get_int(qdict, "wr_operations"));
1636}
1637
1638void bdrv_stats_print(Monitor *mon, const QObject *data)
1639{
1640 qlist_iter(qobject_to_qlist(data), bdrv_stats_iter, mon);
1641}
1642
Kevin Wolf294cc352010-04-28 14:34:01 +02001643static QObject* bdrv_info_stats_bs(BlockDriverState *bs)
1644{
1645 QObject *res;
1646 QDict *dict;
1647
1648 res = qobject_from_jsonf("{ 'stats': {"
1649 "'rd_bytes': %" PRId64 ","
1650 "'wr_bytes': %" PRId64 ","
1651 "'rd_operations': %" PRId64 ","
1652 "'wr_operations': %" PRId64 ","
1653 "'wr_highest_offset': %" PRId64
1654 "} }",
1655 bs->rd_bytes, bs->wr_bytes,
1656 bs->rd_ops, bs->wr_ops,
Blue Swirl5ffbbc62010-06-14 18:55:33 +00001657 bs->wr_highest_sector *
1658 (uint64_t)BDRV_SECTOR_SIZE);
Kevin Wolf294cc352010-04-28 14:34:01 +02001659 dict = qobject_to_qdict(res);
1660
1661 if (*bs->device_name) {
1662 qdict_put(dict, "device", qstring_from_str(bs->device_name));
1663 }
1664
1665 if (bs->file) {
1666 QObject *parent = bdrv_info_stats_bs(bs->file);
1667 qdict_put_obj(dict, "parent", parent);
1668 }
1669
1670 return res;
1671}
1672
Luiz Capitulino218a5362009-12-10 17:16:07 -02001673void bdrv_info_stats(Monitor *mon, QObject **ret_data)
1674{
1675 QObject *obj;
1676 QList *devices;
thsa36e69d2007-12-02 05:18:19 +00001677 BlockDriverState *bs;
1678
Luiz Capitulino218a5362009-12-10 17:16:07 -02001679 devices = qlist_new();
1680
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001681 QTAILQ_FOREACH(bs, &bdrv_states, list) {
Kevin Wolf294cc352010-04-28 14:34:01 +02001682 obj = bdrv_info_stats_bs(bs);
Luiz Capitulino218a5362009-12-10 17:16:07 -02001683 qlist_append_obj(devices, obj);
thsa36e69d2007-12-02 05:18:19 +00001684 }
Luiz Capitulino218a5362009-12-10 17:16:07 -02001685
1686 *ret_data = QOBJECT(devices);
thsa36e69d2007-12-02 05:18:19 +00001687}
bellardea2384d2004-08-01 21:59:26 +00001688
aliguori045df332009-03-05 23:00:48 +00001689const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1690{
1691 if (bs->backing_hd && bs->backing_hd->encrypted)
1692 return bs->backing_file;
1693 else if (bs->encrypted)
1694 return bs->filename;
1695 else
1696 return NULL;
1697}
1698
ths5fafdf22007-09-16 21:08:06 +00001699void bdrv_get_backing_filename(BlockDriverState *bs,
bellard83f64092006-08-01 16:21:11 +00001700 char *filename, int filename_size)
bellardea2384d2004-08-01 21:59:26 +00001701{
Kevin Wolfb783e402010-01-12 12:55:16 +01001702 if (!bs->backing_file) {
bellard83f64092006-08-01 16:21:11 +00001703 pstrcpy(filename, filename_size, "");
1704 } else {
1705 pstrcpy(filename, filename_size, bs->backing_file);
1706 }
bellardea2384d2004-08-01 21:59:26 +00001707}
1708
ths5fafdf22007-09-16 21:08:06 +00001709int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
bellardfaea38e2006-08-05 21:31:00 +00001710 const uint8_t *buf, int nb_sectors)
1711{
1712 BlockDriver *drv = bs->drv;
1713 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001714 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001715 if (!drv->bdrv_write_compressed)
1716 return -ENOTSUP;
Kevin Wolffbb7b4e2009-05-08 14:47:24 +02001717 if (bdrv_check_request(bs, sector_num, nb_sectors))
1718 return -EIO;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001719
Jan Kiszkac6d22832009-11-30 18:21:20 +01001720 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001721 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1722 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001723
bellardfaea38e2006-08-05 21:31:00 +00001724 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1725}
ths3b46e622007-09-17 08:09:54 +00001726
bellardfaea38e2006-08-05 21:31:00 +00001727int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1728{
1729 BlockDriver *drv = bs->drv;
1730 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001731 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001732 if (!drv->bdrv_get_info)
1733 return -ENOTSUP;
1734 memset(bdi, 0, sizeof(*bdi));
1735 return drv->bdrv_get_info(bs, bdi);
1736}
1737
Christoph Hellwig45566e92009-07-10 23:11:57 +02001738int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
1739 int64_t pos, int size)
aliguori178e08a2009-04-05 19:10:55 +00001740{
1741 BlockDriver *drv = bs->drv;
1742 if (!drv)
1743 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09001744 if (drv->bdrv_save_vmstate)
1745 return drv->bdrv_save_vmstate(bs, buf, pos, size);
1746 if (bs->file)
1747 return bdrv_save_vmstate(bs->file, buf, pos, size);
1748 return -ENOTSUP;
aliguori178e08a2009-04-05 19:10:55 +00001749}
1750
Christoph Hellwig45566e92009-07-10 23:11:57 +02001751int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1752 int64_t pos, int size)
aliguori178e08a2009-04-05 19:10:55 +00001753{
1754 BlockDriver *drv = bs->drv;
1755 if (!drv)
1756 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09001757 if (drv->bdrv_load_vmstate)
1758 return drv->bdrv_load_vmstate(bs, buf, pos, size);
1759 if (bs->file)
1760 return bdrv_load_vmstate(bs->file, buf, pos, size);
1761 return -ENOTSUP;
aliguori178e08a2009-04-05 19:10:55 +00001762}
1763
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01001764void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
1765{
1766 BlockDriver *drv = bs->drv;
1767
1768 if (!drv || !drv->bdrv_debug_event) {
1769 return;
1770 }
1771
1772 return drv->bdrv_debug_event(bs, event);
1773
1774}
1775
bellardfaea38e2006-08-05 21:31:00 +00001776/**************************************************************/
1777/* handling of snapshots */
1778
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03001779int bdrv_can_snapshot(BlockDriverState *bs)
1780{
1781 BlockDriver *drv = bs->drv;
1782 if (!drv || bdrv_is_removable(bs) || bdrv_is_read_only(bs)) {
1783 return 0;
1784 }
1785
1786 if (!drv->bdrv_snapshot_create) {
1787 if (bs->file != NULL) {
1788 return bdrv_can_snapshot(bs->file);
1789 }
1790 return 0;
1791 }
1792
1793 return 1;
1794}
1795
Markus Armbrusterf9092b12010-06-25 10:33:39 +02001796BlockDriverState *bdrv_snapshots(void)
1797{
1798 BlockDriverState *bs;
1799
Markus Armbruster3ac906f2010-07-01 09:30:38 +02001800 if (bs_snapshots) {
Markus Armbrusterf9092b12010-06-25 10:33:39 +02001801 return bs_snapshots;
Markus Armbruster3ac906f2010-07-01 09:30:38 +02001802 }
Markus Armbrusterf9092b12010-06-25 10:33:39 +02001803
1804 bs = NULL;
1805 while ((bs = bdrv_next(bs))) {
1806 if (bdrv_can_snapshot(bs)) {
Markus Armbruster3ac906f2010-07-01 09:30:38 +02001807 bs_snapshots = bs;
1808 return bs;
Markus Armbrusterf9092b12010-06-25 10:33:39 +02001809 }
1810 }
1811 return NULL;
Markus Armbrusterf9092b12010-06-25 10:33:39 +02001812}
1813
ths5fafdf22007-09-16 21:08:06 +00001814int bdrv_snapshot_create(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001815 QEMUSnapshotInfo *sn_info)
1816{
1817 BlockDriver *drv = bs->drv;
1818 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001819 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09001820 if (drv->bdrv_snapshot_create)
1821 return drv->bdrv_snapshot_create(bs, sn_info);
1822 if (bs->file)
1823 return bdrv_snapshot_create(bs->file, sn_info);
1824 return -ENOTSUP;
bellardfaea38e2006-08-05 21:31:00 +00001825}
1826
ths5fafdf22007-09-16 21:08:06 +00001827int bdrv_snapshot_goto(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001828 const char *snapshot_id)
1829{
1830 BlockDriver *drv = bs->drv;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09001831 int ret, open_ret;
1832
bellardfaea38e2006-08-05 21:31:00 +00001833 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001834 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09001835 if (drv->bdrv_snapshot_goto)
1836 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1837
1838 if (bs->file) {
1839 drv->bdrv_close(bs);
1840 ret = bdrv_snapshot_goto(bs->file, snapshot_id);
1841 open_ret = drv->bdrv_open(bs, bs->open_flags);
1842 if (open_ret < 0) {
1843 bdrv_delete(bs->file);
1844 bs->drv = NULL;
1845 return open_ret;
1846 }
1847 return ret;
1848 }
1849
1850 return -ENOTSUP;
bellardfaea38e2006-08-05 21:31:00 +00001851}
1852
1853int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1854{
1855 BlockDriver *drv = bs->drv;
1856 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001857 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09001858 if (drv->bdrv_snapshot_delete)
1859 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1860 if (bs->file)
1861 return bdrv_snapshot_delete(bs->file, snapshot_id);
1862 return -ENOTSUP;
bellardfaea38e2006-08-05 21:31:00 +00001863}
1864
ths5fafdf22007-09-16 21:08:06 +00001865int bdrv_snapshot_list(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001866 QEMUSnapshotInfo **psn_info)
1867{
1868 BlockDriver *drv = bs->drv;
1869 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001870 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09001871 if (drv->bdrv_snapshot_list)
1872 return drv->bdrv_snapshot_list(bs, psn_info);
1873 if (bs->file)
1874 return bdrv_snapshot_list(bs->file, psn_info);
1875 return -ENOTSUP;
bellardfaea38e2006-08-05 21:31:00 +00001876}
1877
1878#define NB_SUFFIXES 4
1879
1880char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1881{
1882 static const char suffixes[NB_SUFFIXES] = "KMGT";
1883 int64_t base;
1884 int i;
1885
1886 if (size <= 999) {
1887 snprintf(buf, buf_size, "%" PRId64, size);
1888 } else {
1889 base = 1024;
1890 for(i = 0; i < NB_SUFFIXES; i++) {
1891 if (size < (10 * base)) {
ths5fafdf22007-09-16 21:08:06 +00001892 snprintf(buf, buf_size, "%0.1f%c",
bellardfaea38e2006-08-05 21:31:00 +00001893 (double)size / base,
1894 suffixes[i]);
1895 break;
1896 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
ths5fafdf22007-09-16 21:08:06 +00001897 snprintf(buf, buf_size, "%" PRId64 "%c",
bellardfaea38e2006-08-05 21:31:00 +00001898 ((size + (base >> 1)) / base),
1899 suffixes[i]);
1900 break;
1901 }
1902 base = base * 1024;
1903 }
1904 }
1905 return buf;
1906}
1907
1908char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1909{
1910 char buf1[128], date_buf[128], clock_buf[128];
bellard3b9f94e2007-01-07 17:27:07 +00001911#ifdef _WIN32
1912 struct tm *ptm;
1913#else
bellardfaea38e2006-08-05 21:31:00 +00001914 struct tm tm;
bellard3b9f94e2007-01-07 17:27:07 +00001915#endif
bellardfaea38e2006-08-05 21:31:00 +00001916 time_t ti;
1917 int64_t secs;
1918
1919 if (!sn) {
ths5fafdf22007-09-16 21:08:06 +00001920 snprintf(buf, buf_size,
1921 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001922 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1923 } else {
1924 ti = sn->date_sec;
bellard3b9f94e2007-01-07 17:27:07 +00001925#ifdef _WIN32
1926 ptm = localtime(&ti);
1927 strftime(date_buf, sizeof(date_buf),
1928 "%Y-%m-%d %H:%M:%S", ptm);
1929#else
bellardfaea38e2006-08-05 21:31:00 +00001930 localtime_r(&ti, &tm);
1931 strftime(date_buf, sizeof(date_buf),
1932 "%Y-%m-%d %H:%M:%S", &tm);
bellard3b9f94e2007-01-07 17:27:07 +00001933#endif
bellardfaea38e2006-08-05 21:31:00 +00001934 secs = sn->vm_clock_nsec / 1000000000;
1935 snprintf(clock_buf, sizeof(clock_buf),
1936 "%02d:%02d:%02d.%03d",
1937 (int)(secs / 3600),
1938 (int)((secs / 60) % 60),
ths5fafdf22007-09-16 21:08:06 +00001939 (int)(secs % 60),
bellardfaea38e2006-08-05 21:31:00 +00001940 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1941 snprintf(buf, buf_size,
ths5fafdf22007-09-16 21:08:06 +00001942 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001943 sn->id_str, sn->name,
1944 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1945 date_buf,
1946 clock_buf);
1947 }
1948 return buf;
1949}
1950
bellardea2384d2004-08-01 21:59:26 +00001951
bellard83f64092006-08-01 16:21:11 +00001952/**************************************************************/
1953/* async I/Os */
1954
aliguori3b69e4b2009-01-22 16:59:24 +00001955BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
aliguorif141eaf2009-04-07 18:43:24 +00001956 QEMUIOVector *qiov, int nb_sectors,
aliguori3b69e4b2009-01-22 16:59:24 +00001957 BlockDriverCompletionFunc *cb, void *opaque)
1958{
bellard83f64092006-08-01 16:21:11 +00001959 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001960 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001961
bellard19cb3732006-08-19 11:45:59 +00001962 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001963 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001964 if (bdrv_check_request(bs, sector_num, nb_sectors))
1965 return NULL;
ths3b46e622007-09-17 08:09:54 +00001966
aliguorif141eaf2009-04-07 18:43:24 +00001967 ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
1968 cb, opaque);
thsa36e69d2007-12-02 05:18:19 +00001969
1970 if (ret) {
1971 /* Update stats even though technically transfer has not happened. */
Jan Kiszka6ea44302009-11-30 18:21:19 +01001972 bs->rd_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
thsa36e69d2007-12-02 05:18:19 +00001973 bs->rd_ops ++;
1974 }
1975
1976 return ret;
bellard83f64092006-08-01 16:21:11 +00001977}
1978
aliguorif141eaf2009-04-07 18:43:24 +00001979BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1980 QEMUIOVector *qiov, int nb_sectors,
1981 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001982{
bellard83f64092006-08-01 16:21:11 +00001983 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001984 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001985
bellard19cb3732006-08-19 11:45:59 +00001986 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001987 return NULL;
bellard83f64092006-08-01 16:21:11 +00001988 if (bs->read_only)
pbrookce1a14d2006-08-07 02:38:06 +00001989 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001990 if (bdrv_check_request(bs, sector_num, nb_sectors))
1991 return NULL;
bellard83f64092006-08-01 16:21:11 +00001992
Jan Kiszkac6d22832009-11-30 18:21:20 +01001993 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001994 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1995 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001996
aliguorif141eaf2009-04-07 18:43:24 +00001997 ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
1998 cb, opaque);
thsa36e69d2007-12-02 05:18:19 +00001999
2000 if (ret) {
Kevin Wolf294cc352010-04-28 14:34:01 +02002001 /* Update stats even though technically transfer has not happened. */
2002 bs->wr_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
2003 bs->wr_ops ++;
2004 if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
2005 bs->wr_highest_sector = sector_num + nb_sectors - 1;
2006 }
thsa36e69d2007-12-02 05:18:19 +00002007 }
2008
2009 return ret;
bellard83f64092006-08-01 16:21:11 +00002010}
2011
Kevin Wolf40b4f532009-09-09 17:53:37 +02002012
2013typedef struct MultiwriteCB {
2014 int error;
2015 int num_requests;
2016 int num_callbacks;
2017 struct {
2018 BlockDriverCompletionFunc *cb;
2019 void *opaque;
2020 QEMUIOVector *free_qiov;
2021 void *free_buf;
2022 } callbacks[];
2023} MultiwriteCB;
2024
2025static void multiwrite_user_cb(MultiwriteCB *mcb)
2026{
2027 int i;
2028
2029 for (i = 0; i < mcb->num_callbacks; i++) {
2030 mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
Stefan Hajnoczi1e1ea482010-04-21 20:35:45 +01002031 if (mcb->callbacks[i].free_qiov) {
2032 qemu_iovec_destroy(mcb->callbacks[i].free_qiov);
2033 }
Kevin Wolf40b4f532009-09-09 17:53:37 +02002034 qemu_free(mcb->callbacks[i].free_qiov);
Herve Poussineauf8a83242010-01-24 21:23:56 +00002035 qemu_vfree(mcb->callbacks[i].free_buf);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002036 }
2037}
2038
2039static void multiwrite_cb(void *opaque, int ret)
2040{
2041 MultiwriteCB *mcb = opaque;
2042
Kevin Wolfcb6d3ca2010-04-01 22:48:44 +02002043 if (ret < 0 && !mcb->error) {
Kevin Wolf40b4f532009-09-09 17:53:37 +02002044 mcb->error = ret;
2045 multiwrite_user_cb(mcb);
2046 }
2047
2048 mcb->num_requests--;
2049 if (mcb->num_requests == 0) {
2050 if (mcb->error == 0) {
2051 multiwrite_user_cb(mcb);
2052 }
2053 qemu_free(mcb);
2054 }
2055}
2056
2057static int multiwrite_req_compare(const void *a, const void *b)
2058{
Christoph Hellwig77be4362010-05-19 20:53:10 +02002059 const BlockRequest *req1 = a, *req2 = b;
2060
2061 /*
2062 * Note that we can't simply subtract req2->sector from req1->sector
2063 * here as that could overflow the return value.
2064 */
2065 if (req1->sector > req2->sector) {
2066 return 1;
2067 } else if (req1->sector < req2->sector) {
2068 return -1;
2069 } else {
2070 return 0;
2071 }
Kevin Wolf40b4f532009-09-09 17:53:37 +02002072}
2073
2074/*
2075 * Takes a bunch of requests and tries to merge them. Returns the number of
2076 * requests that remain after merging.
2077 */
2078static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
2079 int num_reqs, MultiwriteCB *mcb)
2080{
2081 int i, outidx;
2082
2083 // Sort requests by start sector
2084 qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
2085
2086 // Check if adjacent requests touch the same clusters. If so, combine them,
2087 // filling up gaps with zero sectors.
2088 outidx = 0;
2089 for (i = 1; i < num_reqs; i++) {
2090 int merge = 0;
2091 int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
2092
2093 // This handles the cases that are valid for all block drivers, namely
2094 // exactly sequential writes and overlapping writes.
2095 if (reqs[i].sector <= oldreq_last) {
2096 merge = 1;
2097 }
2098
2099 // The block driver may decide that it makes sense to combine requests
2100 // even if there is a gap of some sectors between them. In this case,
2101 // the gap is filled with zeros (therefore only applicable for yet
2102 // unused space in format like qcow2).
2103 if (!merge && bs->drv->bdrv_merge_requests) {
2104 merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]);
2105 }
2106
Christoph Hellwige2a305f2010-01-26 14:49:08 +01002107 if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) {
2108 merge = 0;
2109 }
2110
Kevin Wolf40b4f532009-09-09 17:53:37 +02002111 if (merge) {
2112 size_t size;
2113 QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov));
2114 qemu_iovec_init(qiov,
2115 reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
2116
2117 // Add the first request to the merged one. If the requests are
2118 // overlapping, drop the last sectors of the first request.
2119 size = (reqs[i].sector - reqs[outidx].sector) << 9;
2120 qemu_iovec_concat(qiov, reqs[outidx].qiov, size);
2121
2122 // We might need to add some zeros between the two requests
2123 if (reqs[i].sector > oldreq_last) {
2124 size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
2125 uint8_t *buf = qemu_blockalign(bs, zero_bytes);
2126 memset(buf, 0, zero_bytes);
2127 qemu_iovec_add(qiov, buf, zero_bytes);
2128 mcb->callbacks[i].free_buf = buf;
2129 }
2130
2131 // Add the second request
2132 qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size);
2133
Kevin Wolfcbf1dff2010-05-21 11:09:42 +02002134 reqs[outidx].nb_sectors = qiov->size >> 9;
Kevin Wolf40b4f532009-09-09 17:53:37 +02002135 reqs[outidx].qiov = qiov;
2136
2137 mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
2138 } else {
2139 outidx++;
2140 reqs[outidx].sector = reqs[i].sector;
2141 reqs[outidx].nb_sectors = reqs[i].nb_sectors;
2142 reqs[outidx].qiov = reqs[i].qiov;
2143 }
2144 }
2145
2146 return outidx + 1;
2147}
2148
2149/*
2150 * Submit multiple AIO write requests at once.
2151 *
2152 * On success, the function returns 0 and all requests in the reqs array have
2153 * been submitted. In error case this function returns -1, and any of the
2154 * requests may or may not be submitted yet. In particular, this means that the
2155 * callback will be called for some of the requests, for others it won't. The
2156 * caller must check the error field of the BlockRequest to wait for the right
2157 * callbacks (if error != 0, no callback will be called).
2158 *
2159 * The implementation may modify the contents of the reqs array, e.g. to merge
2160 * requests. However, the fields opaque and error are left unmodified as they
2161 * are used to signal failure for a single request to the caller.
2162 */
2163int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
2164{
2165 BlockDriverAIOCB *acb;
2166 MultiwriteCB *mcb;
2167 int i;
2168
2169 if (num_reqs == 0) {
2170 return 0;
2171 }
2172
2173 // Create MultiwriteCB structure
2174 mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
2175 mcb->num_requests = 0;
2176 mcb->num_callbacks = num_reqs;
2177
2178 for (i = 0; i < num_reqs; i++) {
2179 mcb->callbacks[i].cb = reqs[i].cb;
2180 mcb->callbacks[i].opaque = reqs[i].opaque;
2181 }
2182
2183 // Check for mergable requests
2184 num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
2185
2186 // Run the aio requests
2187 for (i = 0; i < num_reqs; i++) {
2188 acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
2189 reqs[i].nb_sectors, multiwrite_cb, mcb);
2190
2191 if (acb == NULL) {
2192 // We can only fail the whole thing if no request has been
2193 // submitted yet. Otherwise we'll wait for the submitted AIOs to
2194 // complete and report the error in the callback.
2195 if (mcb->num_requests == 0) {
Kevin Wolf0f0b6042010-04-06 18:24:06 +02002196 reqs[i].error = -EIO;
Kevin Wolf40b4f532009-09-09 17:53:37 +02002197 goto fail;
2198 } else {
Kevin Wolf7eb58a62010-04-06 18:24:07 +02002199 mcb->num_requests++;
2200 multiwrite_cb(mcb, -EIO);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002201 break;
2202 }
2203 } else {
2204 mcb->num_requests++;
2205 }
2206 }
2207
2208 return 0;
2209
2210fail:
Bruce Rogersaf474592010-05-13 15:14:33 -06002211 qemu_free(mcb);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002212 return -1;
2213}
2214
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002215BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
2216 BlockDriverCompletionFunc *cb, void *opaque)
2217{
2218 BlockDriver *drv = bs->drv;
2219
Alexander Graf016f5cf2010-05-26 17:51:49 +02002220 if (bs->open_flags & BDRV_O_NO_FLUSH) {
2221 return bdrv_aio_noop_em(bs, cb, opaque);
2222 }
2223
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002224 if (!drv)
2225 return NULL;
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002226 return drv->bdrv_aio_flush(bs, cb, opaque);
2227}
2228
bellard83f64092006-08-01 16:21:11 +00002229void bdrv_aio_cancel(BlockDriverAIOCB *acb)
pbrookce1a14d2006-08-07 02:38:06 +00002230{
aliguori6bbff9a2009-03-20 18:25:59 +00002231 acb->pool->cancel(acb);
bellard83f64092006-08-01 16:21:11 +00002232}
2233
pbrookce1a14d2006-08-07 02:38:06 +00002234
bellard83f64092006-08-01 16:21:11 +00002235/**************************************************************/
2236/* async block device emulation */
2237
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002238typedef struct BlockDriverAIOCBSync {
2239 BlockDriverAIOCB common;
2240 QEMUBH *bh;
2241 int ret;
2242 /* vector translation state */
2243 QEMUIOVector *qiov;
2244 uint8_t *bounce;
2245 int is_write;
2246} BlockDriverAIOCBSync;
2247
2248static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
2249{
Kevin Wolfb666d232010-05-05 11:44:39 +02002250 BlockDriverAIOCBSync *acb =
2251 container_of(blockacb, BlockDriverAIOCBSync, common);
Dor Laor6a7ad292009-06-01 12:07:23 +03002252 qemu_bh_delete(acb->bh);
Avi Kivity36afc452009-06-23 16:20:36 +03002253 acb->bh = NULL;
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002254 qemu_aio_release(acb);
2255}
2256
2257static AIOPool bdrv_em_aio_pool = {
2258 .aiocb_size = sizeof(BlockDriverAIOCBSync),
2259 .cancel = bdrv_aio_cancel_em,
2260};
2261
bellard83f64092006-08-01 16:21:11 +00002262static void bdrv_aio_bh_cb(void *opaque)
bellardbeac80c2006-06-26 20:08:57 +00002263{
pbrookce1a14d2006-08-07 02:38:06 +00002264 BlockDriverAIOCBSync *acb = opaque;
aliguorif141eaf2009-04-07 18:43:24 +00002265
aliguorif141eaf2009-04-07 18:43:24 +00002266 if (!acb->is_write)
2267 qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
aliguoriceb42de2009-04-07 18:43:28 +00002268 qemu_vfree(acb->bounce);
pbrookce1a14d2006-08-07 02:38:06 +00002269 acb->common.cb(acb->common.opaque, acb->ret);
Dor Laor6a7ad292009-06-01 12:07:23 +03002270 qemu_bh_delete(acb->bh);
Avi Kivity36afc452009-06-23 16:20:36 +03002271 acb->bh = NULL;
pbrookce1a14d2006-08-07 02:38:06 +00002272 qemu_aio_release(acb);
bellardbeac80c2006-06-26 20:08:57 +00002273}
bellardbeac80c2006-06-26 20:08:57 +00002274
aliguorif141eaf2009-04-07 18:43:24 +00002275static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
2276 int64_t sector_num,
2277 QEMUIOVector *qiov,
2278 int nb_sectors,
2279 BlockDriverCompletionFunc *cb,
2280 void *opaque,
2281 int is_write)
2282
bellardea2384d2004-08-01 21:59:26 +00002283{
pbrookce1a14d2006-08-07 02:38:06 +00002284 BlockDriverAIOCBSync *acb;
pbrookce1a14d2006-08-07 02:38:06 +00002285
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002286 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
aliguorif141eaf2009-04-07 18:43:24 +00002287 acb->is_write = is_write;
2288 acb->qiov = qiov;
aliguorie268ca52009-04-22 20:20:00 +00002289 acb->bounce = qemu_blockalign(bs, qiov->size);
aliguorif141eaf2009-04-07 18:43:24 +00002290
pbrookce1a14d2006-08-07 02:38:06 +00002291 if (!acb->bh)
2292 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
aliguorif141eaf2009-04-07 18:43:24 +00002293
2294 if (is_write) {
2295 qemu_iovec_to_buffer(acb->qiov, acb->bounce);
2296 acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
2297 } else {
2298 acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
2299 }
2300
pbrookce1a14d2006-08-07 02:38:06 +00002301 qemu_bh_schedule(acb->bh);
aliguorif141eaf2009-04-07 18:43:24 +00002302
pbrookce1a14d2006-08-07 02:38:06 +00002303 return &acb->common;
pbrook7a6cba62006-06-04 11:39:07 +00002304}
2305
aliguorif141eaf2009-04-07 18:43:24 +00002306static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
2307 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
pbrookce1a14d2006-08-07 02:38:06 +00002308 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00002309{
aliguorif141eaf2009-04-07 18:43:24 +00002310 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
bellard83f64092006-08-01 16:21:11 +00002311}
2312
aliguorif141eaf2009-04-07 18:43:24 +00002313static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
2314 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2315 BlockDriverCompletionFunc *cb, void *opaque)
2316{
2317 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
2318}
2319
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002320static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
2321 BlockDriverCompletionFunc *cb, void *opaque)
2322{
2323 BlockDriverAIOCBSync *acb;
2324
2325 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2326 acb->is_write = 1; /* don't bounce in the completion hadler */
2327 acb->qiov = NULL;
2328 acb->bounce = NULL;
2329 acb->ret = 0;
2330
2331 if (!acb->bh)
2332 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2333
2334 bdrv_flush(bs);
2335 qemu_bh_schedule(acb->bh);
2336 return &acb->common;
2337}
2338
Alexander Graf016f5cf2010-05-26 17:51:49 +02002339static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,
2340 BlockDriverCompletionFunc *cb, void *opaque)
2341{
2342 BlockDriverAIOCBSync *acb;
2343
2344 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2345 acb->is_write = 1; /* don't bounce in the completion handler */
2346 acb->qiov = NULL;
2347 acb->bounce = NULL;
2348 acb->ret = 0;
2349
2350 if (!acb->bh) {
2351 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2352 }
2353
2354 qemu_bh_schedule(acb->bh);
2355 return &acb->common;
2356}
2357
bellard83f64092006-08-01 16:21:11 +00002358/**************************************************************/
2359/* sync block device emulation */
2360
2361static void bdrv_rw_em_cb(void *opaque, int ret)
2362{
2363 *(int *)opaque = ret;
2364}
2365
2366#define NOT_DONE 0x7fffffff
2367
ths5fafdf22007-09-16 21:08:06 +00002368static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +00002369 uint8_t *buf, int nb_sectors)
2370{
pbrookce1a14d2006-08-07 02:38:06 +00002371 int async_ret;
2372 BlockDriverAIOCB *acb;
aliguorif141eaf2009-04-07 18:43:24 +00002373 struct iovec iov;
2374 QEMUIOVector qiov;
bellard83f64092006-08-01 16:21:11 +00002375
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002376 async_context_push();
2377
bellard83f64092006-08-01 16:21:11 +00002378 async_ret = NOT_DONE;
blueswir13f4cb3d2009-04-13 16:31:01 +00002379 iov.iov_base = (void *)buf;
Jes Sorenseneb5a3162010-05-27 16:20:31 +02002380 iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
aliguorif141eaf2009-04-07 18:43:24 +00002381 qemu_iovec_init_external(&qiov, &iov, 1);
2382 acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
2383 bdrv_rw_em_cb, &async_ret);
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002384 if (acb == NULL) {
2385 async_ret = -1;
2386 goto fail;
2387 }
aliguoribaf35cb2008-09-10 15:45:19 +00002388
bellard83f64092006-08-01 16:21:11 +00002389 while (async_ret == NOT_DONE) {
2390 qemu_aio_wait();
2391 }
aliguoribaf35cb2008-09-10 15:45:19 +00002392
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002393
2394fail:
2395 async_context_pop();
bellard83f64092006-08-01 16:21:11 +00002396 return async_ret;
2397}
2398
2399static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
2400 const uint8_t *buf, int nb_sectors)
2401{
pbrookce1a14d2006-08-07 02:38:06 +00002402 int async_ret;
2403 BlockDriverAIOCB *acb;
aliguorif141eaf2009-04-07 18:43:24 +00002404 struct iovec iov;
2405 QEMUIOVector qiov;
bellard83f64092006-08-01 16:21:11 +00002406
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002407 async_context_push();
2408
bellard83f64092006-08-01 16:21:11 +00002409 async_ret = NOT_DONE;
aliguorif141eaf2009-04-07 18:43:24 +00002410 iov.iov_base = (void *)buf;
Jes Sorenseneb5a3162010-05-27 16:20:31 +02002411 iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
aliguorif141eaf2009-04-07 18:43:24 +00002412 qemu_iovec_init_external(&qiov, &iov, 1);
2413 acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
2414 bdrv_rw_em_cb, &async_ret);
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002415 if (acb == NULL) {
2416 async_ret = -1;
2417 goto fail;
2418 }
bellard83f64092006-08-01 16:21:11 +00002419 while (async_ret == NOT_DONE) {
2420 qemu_aio_wait();
2421 }
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002422
2423fail:
2424 async_context_pop();
bellard83f64092006-08-01 16:21:11 +00002425 return async_ret;
2426}
bellardea2384d2004-08-01 21:59:26 +00002427
2428void bdrv_init(void)
2429{
Anthony Liguori5efa9d52009-05-09 17:03:42 -05002430 module_call_init(MODULE_INIT_BLOCK);
bellardea2384d2004-08-01 21:59:26 +00002431}
pbrookce1a14d2006-08-07 02:38:06 +00002432
Markus Armbrustereb852012009-10-27 18:41:44 +01002433void bdrv_init_with_whitelist(void)
2434{
2435 use_bdrv_whitelist = 1;
2436 bdrv_init();
2437}
2438
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002439void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
2440 BlockDriverCompletionFunc *cb, void *opaque)
aliguori6bbff9a2009-03-20 18:25:59 +00002441{
pbrookce1a14d2006-08-07 02:38:06 +00002442 BlockDriverAIOCB *acb;
2443
aliguori6bbff9a2009-03-20 18:25:59 +00002444 if (pool->free_aiocb) {
2445 acb = pool->free_aiocb;
2446 pool->free_aiocb = acb->next;
pbrookce1a14d2006-08-07 02:38:06 +00002447 } else {
aliguori6bbff9a2009-03-20 18:25:59 +00002448 acb = qemu_mallocz(pool->aiocb_size);
2449 acb->pool = pool;
pbrookce1a14d2006-08-07 02:38:06 +00002450 }
2451 acb->bs = bs;
2452 acb->cb = cb;
2453 acb->opaque = opaque;
2454 return acb;
2455}
2456
2457void qemu_aio_release(void *p)
2458{
aliguori6bbff9a2009-03-20 18:25:59 +00002459 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
2460 AIOPool *pool = acb->pool;
2461 acb->next = pool->free_aiocb;
2462 pool->free_aiocb = acb;
pbrookce1a14d2006-08-07 02:38:06 +00002463}
bellard19cb3732006-08-19 11:45:59 +00002464
2465/**************************************************************/
2466/* removable device support */
2467
2468/**
2469 * Return TRUE if the media is present
2470 */
2471int bdrv_is_inserted(BlockDriverState *bs)
2472{
2473 BlockDriver *drv = bs->drv;
2474 int ret;
2475 if (!drv)
2476 return 0;
2477 if (!drv->bdrv_is_inserted)
2478 return 1;
2479 ret = drv->bdrv_is_inserted(bs);
2480 return ret;
2481}
2482
2483/**
2484 * Return TRUE if the media changed since the last call to this
ths5fafdf22007-09-16 21:08:06 +00002485 * function. It is currently only used for floppy disks
bellard19cb3732006-08-19 11:45:59 +00002486 */
2487int bdrv_media_changed(BlockDriverState *bs)
2488{
2489 BlockDriver *drv = bs->drv;
2490 int ret;
2491
2492 if (!drv || !drv->bdrv_media_changed)
2493 ret = -ENOTSUP;
2494 else
2495 ret = drv->bdrv_media_changed(bs);
2496 if (ret == -ENOTSUP)
2497 ret = bs->media_changed;
2498 bs->media_changed = 0;
2499 return ret;
2500}
2501
2502/**
2503 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
2504 */
Mark McLoughlinaea2a332009-05-27 10:06:11 +01002505int bdrv_eject(BlockDriverState *bs, int eject_flag)
bellard19cb3732006-08-19 11:45:59 +00002506{
2507 BlockDriver *drv = bs->drv;
2508 int ret;
2509
Mark McLoughlinaea2a332009-05-27 10:06:11 +01002510 if (bs->locked) {
2511 return -EBUSY;
2512 }
2513
bellard19cb3732006-08-19 11:45:59 +00002514 if (!drv || !drv->bdrv_eject) {
2515 ret = -ENOTSUP;
2516 } else {
2517 ret = drv->bdrv_eject(bs, eject_flag);
2518 }
2519 if (ret == -ENOTSUP) {
2520 if (eject_flag)
2521 bdrv_close(bs);
Mark McLoughlinaea2a332009-05-27 10:06:11 +01002522 ret = 0;
bellard19cb3732006-08-19 11:45:59 +00002523 }
Mark McLoughlinaea2a332009-05-27 10:06:11 +01002524
2525 return ret;
bellard19cb3732006-08-19 11:45:59 +00002526}
2527
2528int bdrv_is_locked(BlockDriverState *bs)
2529{
2530 return bs->locked;
2531}
2532
2533/**
2534 * Lock or unlock the media (if it is locked, the user won't be able
2535 * to eject it manually).
2536 */
2537void bdrv_set_locked(BlockDriverState *bs, int locked)
2538{
2539 BlockDriver *drv = bs->drv;
2540
2541 bs->locked = locked;
2542 if (drv && drv->bdrv_set_locked) {
2543 drv->bdrv_set_locked(bs, locked);
2544 }
2545}
ths985a03b2007-12-24 16:10:43 +00002546
2547/* needed for generic scsi interface */
2548
2549int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
2550{
2551 BlockDriver *drv = bs->drv;
2552
2553 if (drv && drv->bdrv_ioctl)
2554 return drv->bdrv_ioctl(bs, req, buf);
2555 return -ENOTSUP;
2556}
aliguori7d780662009-03-12 19:57:08 +00002557
aliguori221f7152009-03-28 17:28:41 +00002558BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
2559 unsigned long int req, void *buf,
2560 BlockDriverCompletionFunc *cb, void *opaque)
aliguori7d780662009-03-12 19:57:08 +00002561{
aliguori221f7152009-03-28 17:28:41 +00002562 BlockDriver *drv = bs->drv;
aliguori7d780662009-03-12 19:57:08 +00002563
aliguori221f7152009-03-28 17:28:41 +00002564 if (drv && drv->bdrv_aio_ioctl)
2565 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
2566 return NULL;
aliguori7d780662009-03-12 19:57:08 +00002567}
aliguorie268ca52009-04-22 20:20:00 +00002568
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002569
2570
aliguorie268ca52009-04-22 20:20:00 +00002571void *qemu_blockalign(BlockDriverState *bs, size_t size)
2572{
2573 return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
2574}
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002575
2576void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable)
2577{
2578 int64_t bitmap_size;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002579
Liran Schouraaa0eb72010-01-26 10:31:48 +02002580 bs->dirty_count = 0;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002581 if (enable) {
Jan Kiszkac6d22832009-11-30 18:21:20 +01002582 if (!bs->dirty_bitmap) {
2583 bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
2584 BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
2585 bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002586
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002587 bs->dirty_bitmap = qemu_mallocz(bitmap_size);
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002588 }
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002589 } else {
Jan Kiszkac6d22832009-11-30 18:21:20 +01002590 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002591 qemu_free(bs->dirty_bitmap);
Jan Kiszkac6d22832009-11-30 18:21:20 +01002592 bs->dirty_bitmap = NULL;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002593 }
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002594 }
2595}
2596
2597int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
2598{
Jan Kiszka6ea44302009-11-30 18:21:19 +01002599 int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002600
Jan Kiszkac6d22832009-11-30 18:21:20 +01002601 if (bs->dirty_bitmap &&
2602 (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) {
2603 return bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] &
2604 (1 << (chunk % (sizeof(unsigned long) * 8)));
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002605 } else {
2606 return 0;
2607 }
2608}
2609
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002610void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
2611 int nr_sectors)
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002612{
2613 set_dirty_bitmap(bs, cur_sector, nr_sectors, 0);
2614}
Liran Schouraaa0eb72010-01-26 10:31:48 +02002615
2616int64_t bdrv_get_dirty_count(BlockDriverState *bs)
2617{
2618 return bs->dirty_count;
2619}