blob: a12be0bac1c9a83d29aca518a2c8c05683abd8bc [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
Stefan Weilc98ac352010-07-21 21:51:51 +0200333static int find_image_format(const char *filename, BlockDriver **pdrv)
bellardea2384d2004-08-01 21:59:26 +0000334{
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);
Stefan Weilc98ac352010-07-21 21:51:51 +0200341 if (ret < 0) {
342 *pdrv = NULL;
343 return ret;
344 }
Nicholas Bellingerf8ea0b02010-05-17 09:45:57 -0700345
Kevin Wolf08a00552010-06-01 18:37:31 +0200346 /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
347 if (bs->sg || !bdrv_is_inserted(bs)) {
Nicholas A. Bellinger1a396852010-05-27 08:56:28 -0700348 bdrv_delete(bs);
Stefan Weilc98ac352010-07-21 21:51:51 +0200349 drv = bdrv_find_format("raw");
350 if (!drv) {
351 ret = -ENOENT;
352 }
353 *pdrv = drv;
354 return ret;
Nicholas A. Bellinger1a396852010-05-27 08:56:28 -0700355 }
Nicholas Bellingerf8ea0b02010-05-17 09:45:57 -0700356
bellard83f64092006-08-01 16:21:11 +0000357 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
358 bdrv_delete(bs);
359 if (ret < 0) {
Stefan Weilc98ac352010-07-21 21:51:51 +0200360 *pdrv = NULL;
361 return ret;
bellard83f64092006-08-01 16:21:11 +0000362 }
363
bellardea2384d2004-08-01 21:59:26 +0000364 score_max = 0;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200365 drv = NULL;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100366 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
bellard83f64092006-08-01 16:21:11 +0000367 if (drv1->bdrv_probe) {
368 score = drv1->bdrv_probe(buf, ret, filename);
369 if (score > score_max) {
370 score_max = score;
371 drv = drv1;
372 }
bellardea2384d2004-08-01 21:59:26 +0000373 }
374 }
Stefan Weilc98ac352010-07-21 21:51:51 +0200375 if (!drv) {
376 ret = -ENOENT;
377 }
378 *pdrv = drv;
379 return ret;
bellardea2384d2004-08-01 21:59:26 +0000380}
381
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100382/**
383 * Set the current 'total_sectors' value
384 */
385static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
386{
387 BlockDriver *drv = bs->drv;
388
Nicholas Bellinger396759a2010-05-17 09:46:04 -0700389 /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
390 if (bs->sg)
391 return 0;
392
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100393 /* query actual device if possible, otherwise just trust the hint */
394 if (drv->bdrv_getlength) {
395 int64_t length = drv->bdrv_getlength(bs);
396 if (length < 0) {
397 return length;
398 }
399 hint = length >> BDRV_SECTOR_BITS;
400 }
401
402 bs->total_sectors = hint;
403 return 0;
404}
405
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200406/*
Kevin Wolf57915332010-04-14 15:24:50 +0200407 * Common part for opening disk images and files
408 */
409static int bdrv_open_common(BlockDriverState *bs, const char *filename,
410 int flags, BlockDriver *drv)
411{
412 int ret, open_flags;
413
414 assert(drv != NULL);
415
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200416 bs->file = NULL;
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100417 bs->total_sectors = 0;
Kevin Wolf57915332010-04-14 15:24:50 +0200418 bs->encrypted = 0;
419 bs->valid_key = 0;
420 bs->open_flags = flags;
421 /* buffer_alignment defaulted to 512, drivers can change this value */
422 bs->buffer_alignment = 512;
423
424 pstrcpy(bs->filename, sizeof(bs->filename), filename);
425
426 if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {
427 return -ENOTSUP;
428 }
429
430 bs->drv = drv;
431 bs->opaque = qemu_mallocz(drv->instance_size);
432
433 /*
434 * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a
435 * write cache to the guest. We do need the fdatasync to flush
436 * out transactions for block allocations, and we maybe have a
437 * volatile write cache in our backing device to deal with.
438 */
439 if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE))
440 bs->enable_write_cache = 1;
441
442 /*
443 * Clear flags that are internal to the block layer before opening the
444 * image.
445 */
446 open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
447
448 /*
449 * Snapshots should be writeable.
450 */
451 if (bs->is_temporary) {
452 open_flags |= BDRV_O_RDWR;
453 }
454
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200455 /* Open the image, either directly or using a protocol */
456 if (drv->bdrv_file_open) {
457 ret = drv->bdrv_file_open(bs, filename, open_flags);
458 } else {
459 ret = bdrv_file_open(&bs->file, filename, open_flags);
460 if (ret >= 0) {
461 ret = drv->bdrv_open(bs, open_flags);
462 }
463 }
464
Kevin Wolf57915332010-04-14 15:24:50 +0200465 if (ret < 0) {
466 goto free_and_fail;
467 }
468
469 bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR);
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100470
471 ret = refresh_total_sectors(bs, bs->total_sectors);
472 if (ret < 0) {
473 goto free_and_fail;
Kevin Wolf57915332010-04-14 15:24:50 +0200474 }
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100475
Kevin Wolf57915332010-04-14 15:24:50 +0200476#ifndef _WIN32
477 if (bs->is_temporary) {
478 unlink(filename);
479 }
480#endif
481 return 0;
482
483free_and_fail:
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200484 if (bs->file) {
485 bdrv_delete(bs->file);
486 bs->file = NULL;
487 }
Kevin Wolf57915332010-04-14 15:24:50 +0200488 qemu_free(bs->opaque);
489 bs->opaque = NULL;
490 bs->drv = NULL;
491 return ret;
492}
493
494/*
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200495 * Opens a file using a protocol (file, host_device, nbd, ...)
496 */
bellard83f64092006-08-01 16:21:11 +0000497int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
bellardb3380822004-03-14 21:38:54 +0000498{
bellard83f64092006-08-01 16:21:11 +0000499 BlockDriverState *bs;
Christoph Hellwig6db95602010-04-05 16:53:57 +0200500 BlockDriver *drv;
bellard83f64092006-08-01 16:21:11 +0000501 int ret;
502
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900503 drv = bdrv_find_protocol(filename);
Christoph Hellwig6db95602010-04-05 16:53:57 +0200504 if (!drv) {
505 return -ENOENT;
506 }
507
bellard83f64092006-08-01 16:21:11 +0000508 bs = bdrv_new("");
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200509 ret = bdrv_open_common(bs, filename, flags, drv);
bellard83f64092006-08-01 16:21:11 +0000510 if (ret < 0) {
511 bdrv_delete(bs);
512 return ret;
bellard3b0d4f62005-10-30 18:30:10 +0000513 }
aliguori71d07702009-03-03 17:37:16 +0000514 bs->growable = 1;
bellard83f64092006-08-01 16:21:11 +0000515 *pbs = bs;
516 return 0;
bellardea2384d2004-08-01 21:59:26 +0000517}
bellardfc01f7e2003-06-30 10:03:06 +0000518
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200519/*
520 * Opens a disk image (raw, qcow2, vmdk, ...)
521 */
Kevin Wolfd6e90982010-03-31 14:40:27 +0200522int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
523 BlockDriver *drv)
bellardea2384d2004-08-01 21:59:26 +0000524{
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200525 int ret;
Anthony Liguori79368c82010-07-14 10:58:00 -0500526 int probed = 0;
bellard712e7872005-04-28 21:09:32 +0000527
bellard83f64092006-08-01 16:21:11 +0000528 if (flags & BDRV_O_SNAPSHOT) {
bellardea2384d2004-08-01 21:59:26 +0000529 BlockDriverState *bs1;
530 int64_t total_size;
aliguori7c96d462008-09-12 17:54:13 +0000531 int is_protocol = 0;
Kevin Wolf91a073a2009-05-27 14:48:06 +0200532 BlockDriver *bdrv_qcow2;
533 QEMUOptionParameter *options;
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200534 char tmp_filename[PATH_MAX];
535 char backing_filename[PATH_MAX];
ths3b46e622007-09-17 08:09:54 +0000536
bellardea2384d2004-08-01 21:59:26 +0000537 /* if snapshot, we create a temporary backing file and open it
538 instead of opening 'filename' directly */
539
540 /* if there is a backing file, use it */
541 bs1 = bdrv_new("");
Kevin Wolfd6e90982010-03-31 14:40:27 +0200542 ret = bdrv_open(bs1, filename, 0, drv);
aliguori51d7c002009-03-05 23:00:29 +0000543 if (ret < 0) {
bellardea2384d2004-08-01 21:59:26 +0000544 bdrv_delete(bs1);
aliguori51d7c002009-03-05 23:00:29 +0000545 return ret;
bellardea2384d2004-08-01 21:59:26 +0000546 }
Jes Sorensen3e829902010-05-27 16:20:30 +0200547 total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK;
aliguori7c96d462008-09-12 17:54:13 +0000548
549 if (bs1->drv && bs1->drv->protocol_name)
550 is_protocol = 1;
551
bellardea2384d2004-08-01 21:59:26 +0000552 bdrv_delete(bs1);
ths3b46e622007-09-17 08:09:54 +0000553
bellardea2384d2004-08-01 21:59:26 +0000554 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
aliguori7c96d462008-09-12 17:54:13 +0000555
556 /* Real path is meaningless for protocols */
557 if (is_protocol)
558 snprintf(backing_filename, sizeof(backing_filename),
559 "%s", filename);
Kirill A. Shutemov114cdfa2009-12-25 18:19:22 +0000560 else if (!realpath(filename, backing_filename))
561 return -errno;
aliguori7c96d462008-09-12 17:54:13 +0000562
Kevin Wolf91a073a2009-05-27 14:48:06 +0200563 bdrv_qcow2 = bdrv_find_format("qcow2");
564 options = parse_option_parameters("", bdrv_qcow2->create_options, NULL);
565
Jes Sorensen3e829902010-05-27 16:20:30 +0200566 set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size);
Kevin Wolf91a073a2009-05-27 14:48:06 +0200567 set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename);
568 if (drv) {
569 set_option_parameter(options, BLOCK_OPT_BACKING_FMT,
570 drv->format_name);
571 }
572
573 ret = bdrv_create(bdrv_qcow2, tmp_filename, options);
Jan Kiszkad7487682010-04-29 18:24:50 +0200574 free_option_parameters(options);
aliguori51d7c002009-03-05 23:00:29 +0000575 if (ret < 0) {
576 return ret;
bellardea2384d2004-08-01 21:59:26 +0000577 }
Kevin Wolf91a073a2009-05-27 14:48:06 +0200578
bellardea2384d2004-08-01 21:59:26 +0000579 filename = tmp_filename;
Kevin Wolf91a073a2009-05-27 14:48:06 +0200580 drv = bdrv_qcow2;
bellardea2384d2004-08-01 21:59:26 +0000581 bs->is_temporary = 1;
582 }
bellard712e7872005-04-28 21:09:32 +0000583
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200584 /* Find the right image format driver */
Christoph Hellwig6db95602010-04-05 16:53:57 +0200585 if (!drv) {
Stefan Weilc98ac352010-07-21 21:51:51 +0200586 ret = find_image_format(filename, &drv);
Anthony Liguori79368c82010-07-14 10:58:00 -0500587 probed = 1;
aliguori51d7c002009-03-05 23:00:29 +0000588 }
Christoph Hellwig69873072010-01-20 18:13:25 +0100589
aliguori51d7c002009-03-05 23:00:29 +0000590 if (!drv) {
aliguori51d7c002009-03-05 23:00:29 +0000591 goto unlink_and_fail;
bellardea2384d2004-08-01 21:59:26 +0000592 }
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200593
594 /* Open the image */
595 ret = bdrv_open_common(bs, filename, flags, drv);
596 if (ret < 0) {
Christoph Hellwig69873072010-01-20 18:13:25 +0100597 goto unlink_and_fail;
598 }
599
Anthony Liguori79368c82010-07-14 10:58:00 -0500600 bs->probed = probed;
601
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200602 /* If there is a backing file, use it */
603 if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') {
604 char backing_filename[PATH_MAX];
605 int back_flags;
606 BlockDriver *back_drv = NULL;
607
608 bs->backing_hd = bdrv_new("");
609 path_combine(backing_filename, sizeof(backing_filename),
610 filename, bs->backing_file);
611 if (bs->backing_format[0] != '\0')
612 back_drv = bdrv_find_format(bs->backing_format);
613
614 /* backing files always opened read-only */
615 back_flags =
616 flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
617
618 ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv);
619 if (ret < 0) {
620 bdrv_close(bs);
621 return ret;
622 }
623 if (bs->is_temporary) {
624 bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR);
625 } else {
626 /* base image inherits from "parent" */
627 bs->backing_hd->keep_read_only = bs->keep_read_only;
628 }
629 }
630
631 if (!bdrv_key_required(bs)) {
632 /* call the change callback */
633 bs->media_changed = 1;
634 if (bs->change_cb)
635 bs->change_cb(bs->change_opaque);
636 }
637
638 return 0;
639
640unlink_and_fail:
641 if (bs->is_temporary) {
642 unlink(filename);
643 }
644 return ret;
645}
646
bellardfc01f7e2003-06-30 10:03:06 +0000647void bdrv_close(BlockDriverState *bs)
648{
bellard19cb3732006-08-19 11:45:59 +0000649 if (bs->drv) {
Markus Armbrusterf9092b12010-06-25 10:33:39 +0200650 if (bs == bs_snapshots) {
651 bs_snapshots = NULL;
652 }
Stefan Hajnoczi557df6a2010-04-17 10:49:06 +0100653 if (bs->backing_hd) {
bellardea2384d2004-08-01 21:59:26 +0000654 bdrv_delete(bs->backing_hd);
Stefan Hajnoczi557df6a2010-04-17 10:49:06 +0100655 bs->backing_hd = NULL;
656 }
bellardea2384d2004-08-01 21:59:26 +0000657 bs->drv->bdrv_close(bs);
658 qemu_free(bs->opaque);
659#ifdef _WIN32
660 if (bs->is_temporary) {
661 unlink(bs->filename);
662 }
bellard67b915a2004-03-31 23:37:16 +0000663#endif
bellardea2384d2004-08-01 21:59:26 +0000664 bs->opaque = NULL;
665 bs->drv = NULL;
bellardb3380822004-03-14 21:38:54 +0000666
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200667 if (bs->file != NULL) {
668 bdrv_close(bs->file);
669 }
670
bellardb3380822004-03-14 21:38:54 +0000671 /* call the change callback */
bellard19cb3732006-08-19 11:45:59 +0000672 bs->media_changed = 1;
bellardb3380822004-03-14 21:38:54 +0000673 if (bs->change_cb)
674 bs->change_cb(bs->change_opaque);
675 }
676}
677
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +0900678void bdrv_close_all(void)
679{
680 BlockDriverState *bs;
681
682 QTAILQ_FOREACH(bs, &bdrv_states, list) {
683 bdrv_close(bs);
684 }
685}
686
bellardb3380822004-03-14 21:38:54 +0000687void bdrv_delete(BlockDriverState *bs)
688{
Markus Armbruster18846de2010-06-29 16:58:30 +0200689 assert(!bs->peer);
690
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +0100691 /* remove from list, if necessary */
692 if (bs->device_name[0] != '\0') {
693 QTAILQ_REMOVE(&bdrv_states, bs, list);
694 }
aurel3234c6f052008-04-08 19:51:21 +0000695
bellardb3380822004-03-14 21:38:54 +0000696 bdrv_close(bs);
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200697 if (bs->file != NULL) {
698 bdrv_delete(bs->file);
699 }
700
Markus Armbrusterf9092b12010-06-25 10:33:39 +0200701 assert(bs != bs_snapshots);
bellardb3380822004-03-14 21:38:54 +0000702 qemu_free(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000703}
704
Markus Armbruster18846de2010-06-29 16:58:30 +0200705int bdrv_attach(BlockDriverState *bs, DeviceState *qdev)
706{
707 if (bs->peer) {
708 return -EBUSY;
709 }
710 bs->peer = qdev;
711 return 0;
712}
713
714void bdrv_detach(BlockDriverState *bs, DeviceState *qdev)
715{
716 assert(bs->peer == qdev);
717 bs->peer = NULL;
718}
719
720DeviceState *bdrv_get_attached(BlockDriverState *bs)
721{
722 return bs->peer;
723}
724
aliguorie97fc192009-04-21 23:11:50 +0000725/*
726 * Run consistency checks on an image
727 *
Kevin Wolfe076f332010-06-29 11:43:13 +0200728 * Returns 0 if the check could be completed (it doesn't mean that the image is
729 * free of errors) or -errno when an internal error occured. The results of the
730 * check are stored in res.
aliguorie97fc192009-04-21 23:11:50 +0000731 */
Kevin Wolfe076f332010-06-29 11:43:13 +0200732int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res)
aliguorie97fc192009-04-21 23:11:50 +0000733{
734 if (bs->drv->bdrv_check == NULL) {
735 return -ENOTSUP;
736 }
737
Kevin Wolfe076f332010-06-29 11:43:13 +0200738 memset(res, 0, sizeof(*res));
Kevin Wolf9ac228e2010-06-29 12:37:54 +0200739 return bs->drv->bdrv_check(bs, res);
aliguorie97fc192009-04-21 23:11:50 +0000740}
741
Kevin Wolf8a426612010-07-16 17:17:01 +0200742#define COMMIT_BUF_SECTORS 2048
743
bellard33e39632003-07-06 17:15:21 +0000744/* commit COW file into the raw image */
745int bdrv_commit(BlockDriverState *bs)
746{
bellard19cb3732006-08-19 11:45:59 +0000747 BlockDriver *drv = bs->drv;
Kevin Wolf8a426612010-07-16 17:17:01 +0200748 int64_t sector, total_sectors;
749 int n, ro, open_flags;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200750 int ret = 0, rw_ret = 0;
Kevin Wolf8a426612010-07-16 17:17:01 +0200751 uint8_t *buf;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200752 char filename[1024];
753 BlockDriverState *bs_rw, *bs_ro;
bellard33e39632003-07-06 17:15:21 +0000754
bellard19cb3732006-08-19 11:45:59 +0000755 if (!drv)
756 return -ENOMEDIUM;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200757
758 if (!bs->backing_hd) {
759 return -ENOTSUP;
bellard33e39632003-07-06 17:15:21 +0000760 }
761
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200762 if (bs->backing_hd->keep_read_only) {
763 return -EACCES;
764 }
765
766 ro = bs->backing_hd->read_only;
767 strncpy(filename, bs->backing_hd->filename, sizeof(filename));
768 open_flags = bs->backing_hd->open_flags;
769
770 if (ro) {
771 /* re-open as RW */
772 bdrv_delete(bs->backing_hd);
773 bs->backing_hd = NULL;
774 bs_rw = bdrv_new("");
Kevin Wolfc3349192010-05-06 16:34:56 +0200775 rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR, drv);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200776 if (rw_ret < 0) {
777 bdrv_delete(bs_rw);
778 /* try to re-open read-only */
779 bs_ro = bdrv_new("");
Kevin Wolfc3349192010-05-06 16:34:56 +0200780 ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, drv);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200781 if (ret < 0) {
782 bdrv_delete(bs_ro);
783 /* drive not functional anymore */
784 bs->drv = NULL;
785 return ret;
786 }
787 bs->backing_hd = bs_ro;
788 return rw_ret;
789 }
790 bs->backing_hd = bs_rw;
bellard33e39632003-07-06 17:15:21 +0000791 }
bellardea2384d2004-08-01 21:59:26 +0000792
Jan Kiszka6ea44302009-11-30 18:21:19 +0100793 total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
Kevin Wolf8a426612010-07-16 17:17:01 +0200794 buf = qemu_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE);
bellardea2384d2004-08-01 21:59:26 +0000795
Kevin Wolf8a426612010-07-16 17:17:01 +0200796 for (sector = 0; sector < total_sectors; sector += n) {
797 if (drv->bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) {
798
799 if (bdrv_read(bs, sector, buf, n) != 0) {
800 ret = -EIO;
801 goto ro_cleanup;
802 }
803
804 if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) {
805 ret = -EIO;
806 goto ro_cleanup;
807 }
bellardea2384d2004-08-01 21:59:26 +0000808 }
809 }
bellard95389c82005-12-18 18:28:15 +0000810
Christoph Hellwig1d449522010-01-17 12:32:30 +0100811 if (drv->bdrv_make_empty) {
812 ret = drv->bdrv_make_empty(bs);
813 bdrv_flush(bs);
814 }
bellard95389c82005-12-18 18:28:15 +0000815
Christoph Hellwig3f5075a2010-01-12 13:49:23 +0100816 /*
817 * Make sure all data we wrote to the backing device is actually
818 * stable on disk.
819 */
820 if (bs->backing_hd)
821 bdrv_flush(bs->backing_hd);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200822
823ro_cleanup:
Kevin Wolf8a426612010-07-16 17:17:01 +0200824 qemu_free(buf);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200825
826 if (ro) {
827 /* re-open as RO */
828 bdrv_delete(bs->backing_hd);
829 bs->backing_hd = NULL;
830 bs_ro = bdrv_new("");
Kevin Wolfc3349192010-05-06 16:34:56 +0200831 ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, drv);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200832 if (ret < 0) {
833 bdrv_delete(bs_ro);
834 /* drive not functional anymore */
835 bs->drv = NULL;
836 return ret;
837 }
838 bs->backing_hd = bs_ro;
839 bs->backing_hd->keep_read_only = 0;
840 }
841
Christoph Hellwig1d449522010-01-17 12:32:30 +0100842 return ret;
bellard33e39632003-07-06 17:15:21 +0000843}
844
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200845void bdrv_commit_all(void)
846{
847 BlockDriverState *bs;
848
849 QTAILQ_FOREACH(bs, &bdrv_states, list) {
850 bdrv_commit(bs);
851 }
852}
853
Kevin Wolf756e6732010-01-12 12:55:17 +0100854/*
855 * Return values:
856 * 0 - success
857 * -EINVAL - backing format specified, but no file
858 * -ENOSPC - can't update the backing file because no space is left in the
859 * image file header
860 * -ENOTSUP - format driver doesn't support changing the backing file
861 */
862int bdrv_change_backing_file(BlockDriverState *bs,
863 const char *backing_file, const char *backing_fmt)
864{
865 BlockDriver *drv = bs->drv;
866
867 if (drv->bdrv_change_backing_file != NULL) {
868 return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
869 } else {
870 return -ENOTSUP;
871 }
872}
873
aliguori71d07702009-03-03 17:37:16 +0000874static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
875 size_t size)
876{
877 int64_t len;
878
879 if (!bdrv_is_inserted(bs))
880 return -ENOMEDIUM;
881
882 if (bs->growable)
883 return 0;
884
885 len = bdrv_getlength(bs);
886
Kevin Wolffbb7b4e2009-05-08 14:47:24 +0200887 if (offset < 0)
888 return -EIO;
889
890 if ((offset > len) || (len - offset < size))
aliguori71d07702009-03-03 17:37:16 +0000891 return -EIO;
892
893 return 0;
894}
895
896static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
897 int nb_sectors)
898{
Jes Sorenseneb5a3162010-05-27 16:20:31 +0200899 return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE,
900 nb_sectors * BDRV_SECTOR_SIZE);
aliguori71d07702009-03-03 17:37:16 +0000901}
902
bellard19cb3732006-08-19 11:45:59 +0000903/* return < 0 if error. See bdrv_write() for the return codes */
ths5fafdf22007-09-16 21:08:06 +0000904int bdrv_read(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000905 uint8_t *buf, int nb_sectors)
906{
bellardea2384d2004-08-01 21:59:26 +0000907 BlockDriver *drv = bs->drv;
908
bellard19cb3732006-08-19 11:45:59 +0000909 if (!drv)
910 return -ENOMEDIUM;
aliguori71d07702009-03-03 17:37:16 +0000911 if (bdrv_check_request(bs, sector_num, nb_sectors))
912 return -EIO;
bellardb3380822004-03-14 21:38:54 +0000913
aliguorieda578e2009-03-12 19:57:16 +0000914 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
bellardfc01f7e2003-06-30 10:03:06 +0000915}
916
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +0200917static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num,
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100918 int nb_sectors, int dirty)
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +0200919{
920 int64_t start, end;
Jan Kiszkac6d22832009-11-30 18:21:20 +0100921 unsigned long val, idx, bit;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100922
Jan Kiszka6ea44302009-11-30 18:21:19 +0100923 start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkac6d22832009-11-30 18:21:20 +0100924 end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100925
926 for (; start <= end; start++) {
Jan Kiszkac6d22832009-11-30 18:21:20 +0100927 idx = start / (sizeof(unsigned long) * 8);
928 bit = start % (sizeof(unsigned long) * 8);
929 val = bs->dirty_bitmap[idx];
930 if (dirty) {
Liran Schouraaa0eb72010-01-26 10:31:48 +0200931 if (!(val & (1 << bit))) {
932 bs->dirty_count++;
933 val |= 1 << bit;
934 }
Jan Kiszkac6d22832009-11-30 18:21:20 +0100935 } else {
Liran Schouraaa0eb72010-01-26 10:31:48 +0200936 if (val & (1 << bit)) {
937 bs->dirty_count--;
938 val &= ~(1 << bit);
939 }
Jan Kiszkac6d22832009-11-30 18:21:20 +0100940 }
941 bs->dirty_bitmap[idx] = val;
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +0200942 }
943}
944
ths5fafdf22007-09-16 21:08:06 +0000945/* Return < 0 if error. Important errors are:
bellard19cb3732006-08-19 11:45:59 +0000946 -EIO generic I/O error (may happen for all errors)
947 -ENOMEDIUM No media inserted.
948 -EINVAL Invalid sector number or nb_sectors
949 -EACCES Trying to write a read-only device
950*/
ths5fafdf22007-09-16 21:08:06 +0000951int bdrv_write(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000952 const uint8_t *buf, int nb_sectors)
953{
bellard83f64092006-08-01 16:21:11 +0000954 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +0000955 if (!bs->drv)
956 return -ENOMEDIUM;
bellard0849bf02003-06-30 23:17:31 +0000957 if (bs->read_only)
bellard19cb3732006-08-19 11:45:59 +0000958 return -EACCES;
aliguori71d07702009-03-03 17:37:16 +0000959 if (bdrv_check_request(bs, sector_num, nb_sectors))
960 return -EIO;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100961
Jan Kiszkac6d22832009-11-30 18:21:20 +0100962 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +0200963 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
964 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100965
Kevin Wolf294cc352010-04-28 14:34:01 +0200966 if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
967 bs->wr_highest_sector = sector_num + nb_sectors - 1;
968 }
969
aliguori42fb2802009-01-15 20:43:39 +0000970 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
bellard83f64092006-08-01 16:21:11 +0000971}
972
aliguorieda578e2009-03-12 19:57:16 +0000973int bdrv_pread(BlockDriverState *bs, int64_t offset,
974 void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000975{
Jan Kiszka6ea44302009-11-30 18:21:19 +0100976 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
bellard83f64092006-08-01 16:21:11 +0000977 int len, nb_sectors, count;
978 int64_t sector_num;
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100979 int ret;
bellard83f64092006-08-01 16:21:11 +0000980
981 count = count1;
982 /* first read to align to sector start */
Jan Kiszka6ea44302009-11-30 18:21:19 +0100983 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
bellard83f64092006-08-01 16:21:11 +0000984 if (len > count)
985 len = count;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100986 sector_num = offset >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000987 if (len > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100988 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
989 return ret;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100990 memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
bellard83f64092006-08-01 16:21:11 +0000991 count -= len;
992 if (count == 0)
993 return count1;
994 sector_num++;
995 buf += len;
996 }
997
998 /* read the sectors "in place" */
Jan Kiszka6ea44302009-11-30 18:21:19 +0100999 nb_sectors = count >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001000 if (nb_sectors > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001001 if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0)
1002 return ret;
bellard83f64092006-08-01 16:21:11 +00001003 sector_num += nb_sectors;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001004 len = nb_sectors << BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001005 buf += len;
1006 count -= len;
1007 }
1008
1009 /* add data from the last sector */
1010 if (count > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001011 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1012 return ret;
bellard83f64092006-08-01 16:21:11 +00001013 memcpy(buf, tmp_buf, count);
1014 }
1015 return count1;
1016}
1017
aliguorieda578e2009-03-12 19:57:16 +00001018int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
1019 const void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +00001020{
Jan Kiszka6ea44302009-11-30 18:21:19 +01001021 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
bellard83f64092006-08-01 16:21:11 +00001022 int len, nb_sectors, count;
1023 int64_t sector_num;
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001024 int ret;
bellard83f64092006-08-01 16:21:11 +00001025
1026 count = count1;
1027 /* first write to align to sector start */
Jan Kiszka6ea44302009-11-30 18:21:19 +01001028 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
bellard83f64092006-08-01 16:21:11 +00001029 if (len > count)
1030 len = count;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001031 sector_num = offset >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001032 if (len > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001033 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1034 return ret;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001035 memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len);
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001036 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
1037 return ret;
bellard83f64092006-08-01 16:21:11 +00001038 count -= len;
1039 if (count == 0)
1040 return count1;
1041 sector_num++;
1042 buf += len;
1043 }
1044
1045 /* write the sectors "in place" */
Jan Kiszka6ea44302009-11-30 18:21:19 +01001046 nb_sectors = count >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001047 if (nb_sectors > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001048 if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0)
1049 return ret;
bellard83f64092006-08-01 16:21:11 +00001050 sector_num += nb_sectors;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001051 len = nb_sectors << BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001052 buf += len;
1053 count -= len;
1054 }
1055
1056 /* add data from the last sector */
1057 if (count > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001058 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1059 return ret;
bellard83f64092006-08-01 16:21:11 +00001060 memcpy(tmp_buf, buf, count);
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001061 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
1062 return ret;
bellard83f64092006-08-01 16:21:11 +00001063 }
1064 return count1;
1065}
bellard83f64092006-08-01 16:21:11 +00001066
Kevin Wolff08145f2010-06-16 16:38:15 +02001067/*
1068 * Writes to the file and ensures that no writes are reordered across this
1069 * request (acts as a barrier)
1070 *
1071 * Returns 0 on success, -errno in error cases.
1072 */
1073int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset,
1074 const void *buf, int count)
1075{
1076 int ret;
1077
1078 ret = bdrv_pwrite(bs, offset, buf, count);
1079 if (ret < 0) {
1080 return ret;
1081 }
1082
1083 /* No flush needed for cache=writethrough, it uses O_DSYNC */
1084 if ((bs->open_flags & BDRV_O_CACHE_MASK) != 0) {
1085 bdrv_flush(bs);
1086 }
1087
1088 return 0;
1089}
1090
1091/*
1092 * Writes to the file and ensures that no writes are reordered across this
1093 * request (acts as a barrier)
1094 *
1095 * Returns 0 on success, -errno in error cases.
1096 */
1097int bdrv_write_sync(BlockDriverState *bs, int64_t sector_num,
1098 const uint8_t *buf, int nb_sectors)
1099{
1100 return bdrv_pwrite_sync(bs, BDRV_SECTOR_SIZE * sector_num,
1101 buf, BDRV_SECTOR_SIZE * nb_sectors);
1102}
1103
bellard83f64092006-08-01 16:21:11 +00001104/**
bellard83f64092006-08-01 16:21:11 +00001105 * Truncate file to 'offset' bytes (needed only for file protocols)
1106 */
1107int bdrv_truncate(BlockDriverState *bs, int64_t offset)
1108{
1109 BlockDriver *drv = bs->drv;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001110 int ret;
bellard83f64092006-08-01 16:21:11 +00001111 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001112 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +00001113 if (!drv->bdrv_truncate)
1114 return -ENOTSUP;
Naphtali Sprei59f26892009-10-26 16:25:16 +02001115 if (bs->read_only)
1116 return -EACCES;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001117 ret = drv->bdrv_truncate(bs, offset);
1118 if (ret == 0) {
1119 ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
1120 }
1121 return ret;
bellard83f64092006-08-01 16:21:11 +00001122}
1123
1124/**
1125 * Length of a file in bytes. Return < 0 if error or unknown.
1126 */
1127int64_t bdrv_getlength(BlockDriverState *bs)
1128{
1129 BlockDriver *drv = bs->drv;
1130 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001131 return -ENOMEDIUM;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001132
1133 /* Fixed size devices use the total_sectors value for speed instead of
1134 issuing a length query (like lseek) on each call. Also, legacy block
1135 drivers don't provide a bdrv_getlength function and must use
1136 total_sectors. */
1137 if (!bs->growable || !drv->bdrv_getlength) {
Jan Kiszka6ea44302009-11-30 18:21:19 +01001138 return bs->total_sectors * BDRV_SECTOR_SIZE;
bellard83f64092006-08-01 16:21:11 +00001139 }
1140 return drv->bdrv_getlength(bs);
bellardfc01f7e2003-06-30 10:03:06 +00001141}
1142
bellard19cb3732006-08-19 11:45:59 +00001143/* return 0 as number of sectors if no device present or error */
ths96b8f132007-12-17 01:35:20 +00001144void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
bellardfc01f7e2003-06-30 10:03:06 +00001145{
bellard19cb3732006-08-19 11:45:59 +00001146 int64_t length;
1147 length = bdrv_getlength(bs);
1148 if (length < 0)
1149 length = 0;
1150 else
Jan Kiszka6ea44302009-11-30 18:21:19 +01001151 length = length >> BDRV_SECTOR_BITS;
bellard19cb3732006-08-19 11:45:59 +00001152 *nb_sectors_ptr = length;
bellardfc01f7e2003-06-30 10:03:06 +00001153}
bellardcf989512004-02-16 21:56:36 +00001154
aliguorif3d54fc2008-11-25 21:50:24 +00001155struct partition {
1156 uint8_t boot_ind; /* 0x80 - active */
1157 uint8_t head; /* starting head */
1158 uint8_t sector; /* starting sector */
1159 uint8_t cyl; /* starting cylinder */
1160 uint8_t sys_ind; /* What partition type */
1161 uint8_t end_head; /* end head */
1162 uint8_t end_sector; /* end sector */
1163 uint8_t end_cyl; /* end cylinder */
1164 uint32_t start_sect; /* starting sector counting from 0 */
1165 uint32_t nr_sects; /* nr of sectors in partition */
1166} __attribute__((packed));
1167
1168/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
1169static int guess_disk_lchs(BlockDriverState *bs,
1170 int *pcylinders, int *pheads, int *psectors)
1171{
Jes Sorenseneb5a3162010-05-27 16:20:31 +02001172 uint8_t buf[BDRV_SECTOR_SIZE];
aliguorif3d54fc2008-11-25 21:50:24 +00001173 int ret, i, heads, sectors, cylinders;
1174 struct partition *p;
1175 uint32_t nr_sects;
blueswir1a38131b2008-12-05 17:56:40 +00001176 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +00001177
1178 bdrv_get_geometry(bs, &nb_sectors);
1179
1180 ret = bdrv_read(bs, 0, buf, 1);
1181 if (ret < 0)
1182 return -1;
1183 /* test msdos magic */
1184 if (buf[510] != 0x55 || buf[511] != 0xaa)
1185 return -1;
1186 for(i = 0; i < 4; i++) {
1187 p = ((struct partition *)(buf + 0x1be)) + i;
1188 nr_sects = le32_to_cpu(p->nr_sects);
1189 if (nr_sects && p->end_head) {
1190 /* We make the assumption that the partition terminates on
1191 a cylinder boundary */
1192 heads = p->end_head + 1;
1193 sectors = p->end_sector & 63;
1194 if (sectors == 0)
1195 continue;
1196 cylinders = nb_sectors / (heads * sectors);
1197 if (cylinders < 1 || cylinders > 16383)
1198 continue;
1199 *pheads = heads;
1200 *psectors = sectors;
1201 *pcylinders = cylinders;
1202#if 0
1203 printf("guessed geometry: LCHS=%d %d %d\n",
1204 cylinders, heads, sectors);
1205#endif
1206 return 0;
1207 }
1208 }
1209 return -1;
1210}
1211
1212void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
1213{
1214 int translation, lba_detected = 0;
1215 int cylinders, heads, secs;
blueswir1a38131b2008-12-05 17:56:40 +00001216 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +00001217
1218 /* if a geometry hint is available, use it */
1219 bdrv_get_geometry(bs, &nb_sectors);
1220 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
1221 translation = bdrv_get_translation_hint(bs);
1222 if (cylinders != 0) {
1223 *pcyls = cylinders;
1224 *pheads = heads;
1225 *psecs = secs;
1226 } else {
1227 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
1228 if (heads > 16) {
1229 /* if heads > 16, it means that a BIOS LBA
1230 translation was active, so the default
1231 hardware geometry is OK */
1232 lba_detected = 1;
1233 goto default_geometry;
1234 } else {
1235 *pcyls = cylinders;
1236 *pheads = heads;
1237 *psecs = secs;
1238 /* disable any translation to be in sync with
1239 the logical geometry */
1240 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
1241 bdrv_set_translation_hint(bs,
1242 BIOS_ATA_TRANSLATION_NONE);
1243 }
1244 }
1245 } else {
1246 default_geometry:
1247 /* if no geometry, use a standard physical disk geometry */
1248 cylinders = nb_sectors / (16 * 63);
1249
1250 if (cylinders > 16383)
1251 cylinders = 16383;
1252 else if (cylinders < 2)
1253 cylinders = 2;
1254 *pcyls = cylinders;
1255 *pheads = 16;
1256 *psecs = 63;
1257 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
1258 if ((*pcyls * *pheads) <= 131072) {
1259 bdrv_set_translation_hint(bs,
1260 BIOS_ATA_TRANSLATION_LARGE);
1261 } else {
1262 bdrv_set_translation_hint(bs,
1263 BIOS_ATA_TRANSLATION_LBA);
1264 }
1265 }
1266 }
1267 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
1268 }
1269}
1270
ths5fafdf22007-09-16 21:08:06 +00001271void bdrv_set_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +00001272 int cyls, int heads, int secs)
1273{
1274 bs->cyls = cyls;
1275 bs->heads = heads;
1276 bs->secs = secs;
1277}
1278
1279void bdrv_set_type_hint(BlockDriverState *bs, int type)
1280{
1281 bs->type = type;
1282 bs->removable = ((type == BDRV_TYPE_CDROM ||
1283 type == BDRV_TYPE_FLOPPY));
1284}
1285
bellard46d47672004-11-16 01:45:27 +00001286void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
1287{
1288 bs->translation = translation;
1289}
1290
ths5fafdf22007-09-16 21:08:06 +00001291void bdrv_get_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +00001292 int *pcyls, int *pheads, int *psecs)
1293{
1294 *pcyls = bs->cyls;
1295 *pheads = bs->heads;
1296 *psecs = bs->secs;
1297}
1298
1299int bdrv_get_type_hint(BlockDriverState *bs)
1300{
1301 return bs->type;
1302}
1303
bellard46d47672004-11-16 01:45:27 +00001304int bdrv_get_translation_hint(BlockDriverState *bs)
1305{
1306 return bs->translation;
1307}
1308
Markus Armbrusterabd7f682010-06-02 18:55:17 +02001309void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error,
1310 BlockErrorAction on_write_error)
1311{
1312 bs->on_read_error = on_read_error;
1313 bs->on_write_error = on_write_error;
1314}
1315
1316BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read)
1317{
1318 return is_read ? bs->on_read_error : bs->on_write_error;
1319}
1320
Markus Armbruster7d0d6952010-06-25 13:42:14 +02001321void bdrv_set_removable(BlockDriverState *bs, int removable)
1322{
1323 bs->removable = removable;
1324 if (removable && bs == bs_snapshots) {
1325 bs_snapshots = NULL;
1326 }
1327}
1328
bellardb3380822004-03-14 21:38:54 +00001329int bdrv_is_removable(BlockDriverState *bs)
1330{
1331 return bs->removable;
1332}
1333
1334int bdrv_is_read_only(BlockDriverState *bs)
1335{
1336 return bs->read_only;
1337}
1338
ths985a03b2007-12-24 16:10:43 +00001339int bdrv_is_sg(BlockDriverState *bs)
1340{
1341 return bs->sg;
1342}
1343
Christoph Hellwige900a7b2009-09-04 19:01:15 +02001344int bdrv_enable_write_cache(BlockDriverState *bs)
1345{
1346 return bs->enable_write_cache;
1347}
1348
bellard19cb3732006-08-19 11:45:59 +00001349/* XXX: no longer used */
ths5fafdf22007-09-16 21:08:06 +00001350void bdrv_set_change_cb(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +00001351 void (*change_cb)(void *opaque), void *opaque)
1352{
1353 bs->change_cb = change_cb;
1354 bs->change_opaque = opaque;
1355}
1356
bellardea2384d2004-08-01 21:59:26 +00001357int bdrv_is_encrypted(BlockDriverState *bs)
1358{
1359 if (bs->backing_hd && bs->backing_hd->encrypted)
1360 return 1;
1361 return bs->encrypted;
1362}
1363
aliguoric0f4ce72009-03-05 23:01:01 +00001364int bdrv_key_required(BlockDriverState *bs)
1365{
1366 BlockDriverState *backing_hd = bs->backing_hd;
1367
1368 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
1369 return 1;
1370 return (bs->encrypted && !bs->valid_key);
1371}
1372
bellardea2384d2004-08-01 21:59:26 +00001373int bdrv_set_key(BlockDriverState *bs, const char *key)
1374{
1375 int ret;
1376 if (bs->backing_hd && bs->backing_hd->encrypted) {
1377 ret = bdrv_set_key(bs->backing_hd, key);
1378 if (ret < 0)
1379 return ret;
1380 if (!bs->encrypted)
1381 return 0;
1382 }
Shahar Havivifd04a2a2010-03-06 00:26:13 +02001383 if (!bs->encrypted) {
1384 return -EINVAL;
1385 } else if (!bs->drv || !bs->drv->bdrv_set_key) {
1386 return -ENOMEDIUM;
1387 }
aliguoric0f4ce72009-03-05 23:01:01 +00001388 ret = bs->drv->bdrv_set_key(bs, key);
aliguoribb5fc202009-03-05 23:01:15 +00001389 if (ret < 0) {
1390 bs->valid_key = 0;
1391 } else if (!bs->valid_key) {
1392 bs->valid_key = 1;
1393 /* call the change callback now, we skipped it on open */
1394 bs->media_changed = 1;
1395 if (bs->change_cb)
1396 bs->change_cb(bs->change_opaque);
1397 }
aliguoric0f4ce72009-03-05 23:01:01 +00001398 return ret;
bellardea2384d2004-08-01 21:59:26 +00001399}
1400
1401void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
1402{
bellard19cb3732006-08-19 11:45:59 +00001403 if (!bs->drv) {
bellardea2384d2004-08-01 21:59:26 +00001404 buf[0] = '\0';
1405 } else {
1406 pstrcpy(buf, buf_size, bs->drv->format_name);
1407 }
1408}
1409
ths5fafdf22007-09-16 21:08:06 +00001410void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
bellardea2384d2004-08-01 21:59:26 +00001411 void *opaque)
1412{
1413 BlockDriver *drv;
1414
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +01001415 QLIST_FOREACH(drv, &bdrv_drivers, list) {
bellardea2384d2004-08-01 21:59:26 +00001416 it(opaque, drv->format_name);
1417 }
1418}
1419
bellardb3380822004-03-14 21:38:54 +00001420BlockDriverState *bdrv_find(const char *name)
1421{
1422 BlockDriverState *bs;
1423
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001424 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1425 if (!strcmp(name, bs->device_name)) {
bellardb3380822004-03-14 21:38:54 +00001426 return bs;
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001427 }
bellardb3380822004-03-14 21:38:54 +00001428 }
1429 return NULL;
1430}
1431
Markus Armbruster2f399b02010-06-02 18:55:20 +02001432BlockDriverState *bdrv_next(BlockDriverState *bs)
1433{
1434 if (!bs) {
1435 return QTAILQ_FIRST(&bdrv_states);
1436 }
1437 return QTAILQ_NEXT(bs, list);
1438}
1439
aliguori51de9762009-03-05 23:00:43 +00001440void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
bellard81d09122004-07-14 17:21:37 +00001441{
1442 BlockDriverState *bs;
1443
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001444 QTAILQ_FOREACH(bs, &bdrv_states, list) {
aliguori51de9762009-03-05 23:00:43 +00001445 it(opaque, bs);
bellard81d09122004-07-14 17:21:37 +00001446 }
1447}
1448
bellardea2384d2004-08-01 21:59:26 +00001449const char *bdrv_get_device_name(BlockDriverState *bs)
1450{
1451 return bs->device_name;
1452}
1453
pbrook7a6cba62006-06-04 11:39:07 +00001454void bdrv_flush(BlockDriverState *bs)
1455{
Alexander Graf016f5cf2010-05-26 17:51:49 +02001456 if (bs->open_flags & BDRV_O_NO_FLUSH) {
1457 return;
1458 }
1459
Christoph Hellwig3f5075a2010-01-12 13:49:23 +01001460 if (bs->drv && bs->drv->bdrv_flush)
pbrook7a6cba62006-06-04 11:39:07 +00001461 bs->drv->bdrv_flush(bs);
pbrook7a6cba62006-06-04 11:39:07 +00001462}
1463
aliguoric6ca28d2008-10-06 13:55:43 +00001464void bdrv_flush_all(void)
1465{
1466 BlockDriverState *bs;
1467
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001468 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1469 if (bs->drv && !bdrv_is_read_only(bs) &&
1470 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) {
aliguoric6ca28d2008-10-06 13:55:43 +00001471 bdrv_flush(bs);
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001472 }
1473 }
aliguoric6ca28d2008-10-06 13:55:43 +00001474}
1475
Kevin Wolff2feebb2010-04-14 17:30:35 +02001476int bdrv_has_zero_init(BlockDriverState *bs)
1477{
1478 assert(bs->drv);
1479
Kevin Wolf336c1c12010-07-28 11:26:29 +02001480 if (bs->drv->bdrv_has_zero_init) {
1481 return bs->drv->bdrv_has_zero_init(bs);
Kevin Wolff2feebb2010-04-14 17:30:35 +02001482 }
1483
1484 return 1;
1485}
1486
thsf58c7b32008-06-05 21:53:49 +00001487/*
1488 * Returns true iff the specified sector is present in the disk image. Drivers
1489 * not implementing the functionality are assumed to not support backing files,
1490 * hence all their sectors are reported as allocated.
1491 *
1492 * 'pnum' is set to the number of sectors (including and immediately following
1493 * the specified sector) that are known to be in the same
1494 * allocated/unallocated state.
1495 *
1496 * 'nb_sectors' is the max value 'pnum' should be set to.
1497 */
1498int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1499 int *pnum)
1500{
1501 int64_t n;
1502 if (!bs->drv->bdrv_is_allocated) {
1503 if (sector_num >= bs->total_sectors) {
1504 *pnum = 0;
1505 return 0;
1506 }
1507 n = bs->total_sectors - sector_num;
1508 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1509 return 1;
1510 }
1511 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1512}
1513
Luiz Capitulino2582bfe2010-02-03 12:41:01 -02001514void bdrv_mon_event(const BlockDriverState *bdrv,
1515 BlockMonEventAction action, int is_read)
1516{
1517 QObject *data;
1518 const char *action_str;
1519
1520 switch (action) {
1521 case BDRV_ACTION_REPORT:
1522 action_str = "report";
1523 break;
1524 case BDRV_ACTION_IGNORE:
1525 action_str = "ignore";
1526 break;
1527 case BDRV_ACTION_STOP:
1528 action_str = "stop";
1529 break;
1530 default:
1531 abort();
1532 }
1533
1534 data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
1535 bdrv->device_name,
1536 action_str,
1537 is_read ? "read" : "write");
1538 monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data);
1539
1540 qobject_decref(data);
1541}
1542
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001543static void bdrv_print_dict(QObject *obj, void *opaque)
bellardb3380822004-03-14 21:38:54 +00001544{
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001545 QDict *bs_dict;
1546 Monitor *mon = opaque;
1547
1548 bs_dict = qobject_to_qdict(obj);
1549
1550 monitor_printf(mon, "%s: type=%s removable=%d",
1551 qdict_get_str(bs_dict, "device"),
1552 qdict_get_str(bs_dict, "type"),
1553 qdict_get_bool(bs_dict, "removable"));
1554
1555 if (qdict_get_bool(bs_dict, "removable")) {
1556 monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked"));
1557 }
1558
1559 if (qdict_haskey(bs_dict, "inserted")) {
1560 QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted"));
1561
1562 monitor_printf(mon, " file=");
1563 monitor_print_filename(mon, qdict_get_str(qdict, "file"));
1564 if (qdict_haskey(qdict, "backing_file")) {
1565 monitor_printf(mon, " backing_file=");
1566 monitor_print_filename(mon, qdict_get_str(qdict, "backing_file"));
1567 }
1568 monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
1569 qdict_get_bool(qdict, "ro"),
1570 qdict_get_str(qdict, "drv"),
1571 qdict_get_bool(qdict, "encrypted"));
1572 } else {
1573 monitor_printf(mon, " [not inserted]");
1574 }
1575
1576 monitor_printf(mon, "\n");
1577}
1578
1579void bdrv_info_print(Monitor *mon, const QObject *data)
1580{
1581 qlist_iter(qobject_to_qlist(data), bdrv_print_dict, mon);
1582}
1583
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001584void bdrv_info(Monitor *mon, QObject **ret_data)
1585{
1586 QList *bs_list;
bellardb3380822004-03-14 21:38:54 +00001587 BlockDriverState *bs;
1588
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001589 bs_list = qlist_new();
1590
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001591 QTAILQ_FOREACH(bs, &bdrv_states, list) {
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001592 QObject *bs_obj;
1593 const char *type = "unknown";
1594
bellardb3380822004-03-14 21:38:54 +00001595 switch(bs->type) {
1596 case BDRV_TYPE_HD:
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001597 type = "hd";
bellardb3380822004-03-14 21:38:54 +00001598 break;
1599 case BDRV_TYPE_CDROM:
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001600 type = "cdrom";
bellardb3380822004-03-14 21:38:54 +00001601 break;
1602 case BDRV_TYPE_FLOPPY:
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001603 type = "floppy";
bellardb3380822004-03-14 21:38:54 +00001604 break;
1605 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001606
1607 bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': %s, "
1608 "'removable': %i, 'locked': %i }",
1609 bs->device_name, type, bs->removable,
1610 bs->locked);
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001611
bellard19cb3732006-08-19 11:45:59 +00001612 if (bs->drv) {
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001613 QObject *obj;
1614 QDict *bs_dict = qobject_to_qdict(bs_obj);
1615
1616 obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, "
1617 "'encrypted': %i }",
1618 bs->filename, bs->read_only,
1619 bs->drv->format_name,
1620 bdrv_is_encrypted(bs));
thsfef30742006-12-22 14:11:32 +00001621 if (bs->backing_file[0] != '\0') {
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001622 QDict *qdict = qobject_to_qdict(obj);
1623 qdict_put(qdict, "backing_file",
1624 qstring_from_str(bs->backing_file));
aliguori376253e2009-03-05 23:01:23 +00001625 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001626
1627 qdict_put_obj(bs_dict, "inserted", obj);
bellardb3380822004-03-14 21:38:54 +00001628 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001629 qlist_append_obj(bs_list, bs_obj);
bellardb3380822004-03-14 21:38:54 +00001630 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001631
1632 *ret_data = QOBJECT(bs_list);
bellardb3380822004-03-14 21:38:54 +00001633}
thsa36e69d2007-12-02 05:18:19 +00001634
Luiz Capitulino218a5362009-12-10 17:16:07 -02001635static void bdrv_stats_iter(QObject *data, void *opaque)
thsa36e69d2007-12-02 05:18:19 +00001636{
Luiz Capitulino218a5362009-12-10 17:16:07 -02001637 QDict *qdict;
1638 Monitor *mon = opaque;
1639
1640 qdict = qobject_to_qdict(data);
1641 monitor_printf(mon, "%s:", qdict_get_str(qdict, "device"));
1642
1643 qdict = qobject_to_qdict(qdict_get(qdict, "stats"));
1644 monitor_printf(mon, " rd_bytes=%" PRId64
1645 " wr_bytes=%" PRId64
1646 " rd_operations=%" PRId64
1647 " wr_operations=%" PRId64
1648 "\n",
1649 qdict_get_int(qdict, "rd_bytes"),
1650 qdict_get_int(qdict, "wr_bytes"),
1651 qdict_get_int(qdict, "rd_operations"),
1652 qdict_get_int(qdict, "wr_operations"));
1653}
1654
1655void bdrv_stats_print(Monitor *mon, const QObject *data)
1656{
1657 qlist_iter(qobject_to_qlist(data), bdrv_stats_iter, mon);
1658}
1659
Kevin Wolf294cc352010-04-28 14:34:01 +02001660static QObject* bdrv_info_stats_bs(BlockDriverState *bs)
1661{
1662 QObject *res;
1663 QDict *dict;
1664
1665 res = qobject_from_jsonf("{ 'stats': {"
1666 "'rd_bytes': %" PRId64 ","
1667 "'wr_bytes': %" PRId64 ","
1668 "'rd_operations': %" PRId64 ","
1669 "'wr_operations': %" PRId64 ","
1670 "'wr_highest_offset': %" PRId64
1671 "} }",
1672 bs->rd_bytes, bs->wr_bytes,
1673 bs->rd_ops, bs->wr_ops,
Blue Swirl5ffbbc62010-06-14 18:55:33 +00001674 bs->wr_highest_sector *
1675 (uint64_t)BDRV_SECTOR_SIZE);
Kevin Wolf294cc352010-04-28 14:34:01 +02001676 dict = qobject_to_qdict(res);
1677
1678 if (*bs->device_name) {
1679 qdict_put(dict, "device", qstring_from_str(bs->device_name));
1680 }
1681
1682 if (bs->file) {
1683 QObject *parent = bdrv_info_stats_bs(bs->file);
1684 qdict_put_obj(dict, "parent", parent);
1685 }
1686
1687 return res;
1688}
1689
Luiz Capitulino218a5362009-12-10 17:16:07 -02001690void bdrv_info_stats(Monitor *mon, QObject **ret_data)
1691{
1692 QObject *obj;
1693 QList *devices;
thsa36e69d2007-12-02 05:18:19 +00001694 BlockDriverState *bs;
1695
Luiz Capitulino218a5362009-12-10 17:16:07 -02001696 devices = qlist_new();
1697
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001698 QTAILQ_FOREACH(bs, &bdrv_states, list) {
Kevin Wolf294cc352010-04-28 14:34:01 +02001699 obj = bdrv_info_stats_bs(bs);
Luiz Capitulino218a5362009-12-10 17:16:07 -02001700 qlist_append_obj(devices, obj);
thsa36e69d2007-12-02 05:18:19 +00001701 }
Luiz Capitulino218a5362009-12-10 17:16:07 -02001702
1703 *ret_data = QOBJECT(devices);
thsa36e69d2007-12-02 05:18:19 +00001704}
bellardea2384d2004-08-01 21:59:26 +00001705
aliguori045df332009-03-05 23:00:48 +00001706const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1707{
1708 if (bs->backing_hd && bs->backing_hd->encrypted)
1709 return bs->backing_file;
1710 else if (bs->encrypted)
1711 return bs->filename;
1712 else
1713 return NULL;
1714}
1715
ths5fafdf22007-09-16 21:08:06 +00001716void bdrv_get_backing_filename(BlockDriverState *bs,
bellard83f64092006-08-01 16:21:11 +00001717 char *filename, int filename_size)
bellardea2384d2004-08-01 21:59:26 +00001718{
Kevin Wolfb783e402010-01-12 12:55:16 +01001719 if (!bs->backing_file) {
bellard83f64092006-08-01 16:21:11 +00001720 pstrcpy(filename, filename_size, "");
1721 } else {
1722 pstrcpy(filename, filename_size, bs->backing_file);
1723 }
bellardea2384d2004-08-01 21:59:26 +00001724}
1725
ths5fafdf22007-09-16 21:08:06 +00001726int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
bellardfaea38e2006-08-05 21:31:00 +00001727 const uint8_t *buf, int nb_sectors)
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_write_compressed)
1733 return -ENOTSUP;
Kevin Wolffbb7b4e2009-05-08 14:47:24 +02001734 if (bdrv_check_request(bs, sector_num, nb_sectors))
1735 return -EIO;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001736
Jan Kiszkac6d22832009-11-30 18:21:20 +01001737 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001738 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1739 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001740
bellardfaea38e2006-08-05 21:31:00 +00001741 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1742}
ths3b46e622007-09-17 08:09:54 +00001743
bellardfaea38e2006-08-05 21:31:00 +00001744int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1745{
1746 BlockDriver *drv = bs->drv;
1747 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001748 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001749 if (!drv->bdrv_get_info)
1750 return -ENOTSUP;
1751 memset(bdi, 0, sizeof(*bdi));
1752 return drv->bdrv_get_info(bs, bdi);
1753}
1754
Christoph Hellwig45566e92009-07-10 23:11:57 +02001755int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
1756 int64_t pos, int size)
aliguori178e08a2009-04-05 19:10:55 +00001757{
1758 BlockDriver *drv = bs->drv;
1759 if (!drv)
1760 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09001761 if (drv->bdrv_save_vmstate)
1762 return drv->bdrv_save_vmstate(bs, buf, pos, size);
1763 if (bs->file)
1764 return bdrv_save_vmstate(bs->file, buf, pos, size);
1765 return -ENOTSUP;
aliguori178e08a2009-04-05 19:10:55 +00001766}
1767
Christoph Hellwig45566e92009-07-10 23:11:57 +02001768int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1769 int64_t pos, int size)
aliguori178e08a2009-04-05 19:10:55 +00001770{
1771 BlockDriver *drv = bs->drv;
1772 if (!drv)
1773 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09001774 if (drv->bdrv_load_vmstate)
1775 return drv->bdrv_load_vmstate(bs, buf, pos, size);
1776 if (bs->file)
1777 return bdrv_load_vmstate(bs->file, buf, pos, size);
1778 return -ENOTSUP;
aliguori178e08a2009-04-05 19:10:55 +00001779}
1780
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01001781void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
1782{
1783 BlockDriver *drv = bs->drv;
1784
1785 if (!drv || !drv->bdrv_debug_event) {
1786 return;
1787 }
1788
1789 return drv->bdrv_debug_event(bs, event);
1790
1791}
1792
bellardfaea38e2006-08-05 21:31:00 +00001793/**************************************************************/
1794/* handling of snapshots */
1795
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03001796int bdrv_can_snapshot(BlockDriverState *bs)
1797{
1798 BlockDriver *drv = bs->drv;
1799 if (!drv || bdrv_is_removable(bs) || bdrv_is_read_only(bs)) {
1800 return 0;
1801 }
1802
1803 if (!drv->bdrv_snapshot_create) {
1804 if (bs->file != NULL) {
1805 return bdrv_can_snapshot(bs->file);
1806 }
1807 return 0;
1808 }
1809
1810 return 1;
1811}
1812
Blue Swirl199630b2010-07-25 20:49:34 +00001813int bdrv_is_snapshot(BlockDriverState *bs)
1814{
1815 return !!(bs->open_flags & BDRV_O_SNAPSHOT);
1816}
1817
Markus Armbrusterf9092b12010-06-25 10:33:39 +02001818BlockDriverState *bdrv_snapshots(void)
1819{
1820 BlockDriverState *bs;
1821
Markus Armbruster3ac906f2010-07-01 09:30:38 +02001822 if (bs_snapshots) {
Markus Armbrusterf9092b12010-06-25 10:33:39 +02001823 return bs_snapshots;
Markus Armbruster3ac906f2010-07-01 09:30:38 +02001824 }
Markus Armbrusterf9092b12010-06-25 10:33:39 +02001825
1826 bs = NULL;
1827 while ((bs = bdrv_next(bs))) {
1828 if (bdrv_can_snapshot(bs)) {
Markus Armbruster3ac906f2010-07-01 09:30:38 +02001829 bs_snapshots = bs;
1830 return bs;
Markus Armbrusterf9092b12010-06-25 10:33:39 +02001831 }
1832 }
1833 return NULL;
Markus Armbrusterf9092b12010-06-25 10:33:39 +02001834}
1835
ths5fafdf22007-09-16 21:08:06 +00001836int bdrv_snapshot_create(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001837 QEMUSnapshotInfo *sn_info)
1838{
1839 BlockDriver *drv = bs->drv;
1840 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001841 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09001842 if (drv->bdrv_snapshot_create)
1843 return drv->bdrv_snapshot_create(bs, sn_info);
1844 if (bs->file)
1845 return bdrv_snapshot_create(bs->file, sn_info);
1846 return -ENOTSUP;
bellardfaea38e2006-08-05 21:31:00 +00001847}
1848
ths5fafdf22007-09-16 21:08:06 +00001849int bdrv_snapshot_goto(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001850 const char *snapshot_id)
1851{
1852 BlockDriver *drv = bs->drv;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09001853 int ret, open_ret;
1854
bellardfaea38e2006-08-05 21:31:00 +00001855 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001856 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09001857 if (drv->bdrv_snapshot_goto)
1858 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1859
1860 if (bs->file) {
1861 drv->bdrv_close(bs);
1862 ret = bdrv_snapshot_goto(bs->file, snapshot_id);
1863 open_ret = drv->bdrv_open(bs, bs->open_flags);
1864 if (open_ret < 0) {
1865 bdrv_delete(bs->file);
1866 bs->drv = NULL;
1867 return open_ret;
1868 }
1869 return ret;
1870 }
1871
1872 return -ENOTSUP;
bellardfaea38e2006-08-05 21:31:00 +00001873}
1874
1875int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1876{
1877 BlockDriver *drv = bs->drv;
1878 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001879 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09001880 if (drv->bdrv_snapshot_delete)
1881 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1882 if (bs->file)
1883 return bdrv_snapshot_delete(bs->file, snapshot_id);
1884 return -ENOTSUP;
bellardfaea38e2006-08-05 21:31:00 +00001885}
1886
ths5fafdf22007-09-16 21:08:06 +00001887int bdrv_snapshot_list(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001888 QEMUSnapshotInfo **psn_info)
1889{
1890 BlockDriver *drv = bs->drv;
1891 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001892 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09001893 if (drv->bdrv_snapshot_list)
1894 return drv->bdrv_snapshot_list(bs, psn_info);
1895 if (bs->file)
1896 return bdrv_snapshot_list(bs->file, psn_info);
1897 return -ENOTSUP;
bellardfaea38e2006-08-05 21:31:00 +00001898}
1899
1900#define NB_SUFFIXES 4
1901
1902char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1903{
1904 static const char suffixes[NB_SUFFIXES] = "KMGT";
1905 int64_t base;
1906 int i;
1907
1908 if (size <= 999) {
1909 snprintf(buf, buf_size, "%" PRId64, size);
1910 } else {
1911 base = 1024;
1912 for(i = 0; i < NB_SUFFIXES; i++) {
1913 if (size < (10 * base)) {
ths5fafdf22007-09-16 21:08:06 +00001914 snprintf(buf, buf_size, "%0.1f%c",
bellardfaea38e2006-08-05 21:31:00 +00001915 (double)size / base,
1916 suffixes[i]);
1917 break;
1918 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
ths5fafdf22007-09-16 21:08:06 +00001919 snprintf(buf, buf_size, "%" PRId64 "%c",
bellardfaea38e2006-08-05 21:31:00 +00001920 ((size + (base >> 1)) / base),
1921 suffixes[i]);
1922 break;
1923 }
1924 base = base * 1024;
1925 }
1926 }
1927 return buf;
1928}
1929
1930char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1931{
1932 char buf1[128], date_buf[128], clock_buf[128];
bellard3b9f94e2007-01-07 17:27:07 +00001933#ifdef _WIN32
1934 struct tm *ptm;
1935#else
bellardfaea38e2006-08-05 21:31:00 +00001936 struct tm tm;
bellard3b9f94e2007-01-07 17:27:07 +00001937#endif
bellardfaea38e2006-08-05 21:31:00 +00001938 time_t ti;
1939 int64_t secs;
1940
1941 if (!sn) {
ths5fafdf22007-09-16 21:08:06 +00001942 snprintf(buf, buf_size,
1943 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001944 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1945 } else {
1946 ti = sn->date_sec;
bellard3b9f94e2007-01-07 17:27:07 +00001947#ifdef _WIN32
1948 ptm = localtime(&ti);
1949 strftime(date_buf, sizeof(date_buf),
1950 "%Y-%m-%d %H:%M:%S", ptm);
1951#else
bellardfaea38e2006-08-05 21:31:00 +00001952 localtime_r(&ti, &tm);
1953 strftime(date_buf, sizeof(date_buf),
1954 "%Y-%m-%d %H:%M:%S", &tm);
bellard3b9f94e2007-01-07 17:27:07 +00001955#endif
bellardfaea38e2006-08-05 21:31:00 +00001956 secs = sn->vm_clock_nsec / 1000000000;
1957 snprintf(clock_buf, sizeof(clock_buf),
1958 "%02d:%02d:%02d.%03d",
1959 (int)(secs / 3600),
1960 (int)((secs / 60) % 60),
ths5fafdf22007-09-16 21:08:06 +00001961 (int)(secs % 60),
bellardfaea38e2006-08-05 21:31:00 +00001962 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1963 snprintf(buf, buf_size,
ths5fafdf22007-09-16 21:08:06 +00001964 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001965 sn->id_str, sn->name,
1966 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1967 date_buf,
1968 clock_buf);
1969 }
1970 return buf;
1971}
1972
bellardea2384d2004-08-01 21:59:26 +00001973
bellard83f64092006-08-01 16:21:11 +00001974/**************************************************************/
1975/* async I/Os */
1976
aliguori3b69e4b2009-01-22 16:59:24 +00001977BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
aliguorif141eaf2009-04-07 18:43:24 +00001978 QEMUIOVector *qiov, int nb_sectors,
aliguori3b69e4b2009-01-22 16:59:24 +00001979 BlockDriverCompletionFunc *cb, void *opaque)
1980{
bellard83f64092006-08-01 16:21:11 +00001981 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001982 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001983
bellard19cb3732006-08-19 11:45:59 +00001984 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001985 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001986 if (bdrv_check_request(bs, sector_num, nb_sectors))
1987 return NULL;
ths3b46e622007-09-17 08:09:54 +00001988
aliguorif141eaf2009-04-07 18:43:24 +00001989 ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
1990 cb, opaque);
thsa36e69d2007-12-02 05:18:19 +00001991
1992 if (ret) {
1993 /* Update stats even though technically transfer has not happened. */
Jan Kiszka6ea44302009-11-30 18:21:19 +01001994 bs->rd_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
thsa36e69d2007-12-02 05:18:19 +00001995 bs->rd_ops ++;
1996 }
1997
1998 return ret;
bellard83f64092006-08-01 16:21:11 +00001999}
2000
aliguorif141eaf2009-04-07 18:43:24 +00002001BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
2002 QEMUIOVector *qiov, int nb_sectors,
2003 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00002004{
bellard83f64092006-08-01 16:21:11 +00002005 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00002006 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00002007
bellard19cb3732006-08-19 11:45:59 +00002008 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00002009 return NULL;
bellard83f64092006-08-01 16:21:11 +00002010 if (bs->read_only)
pbrookce1a14d2006-08-07 02:38:06 +00002011 return NULL;
aliguori71d07702009-03-03 17:37:16 +00002012 if (bdrv_check_request(bs, sector_num, nb_sectors))
2013 return NULL;
bellard83f64092006-08-01 16:21:11 +00002014
Jan Kiszkac6d22832009-11-30 18:21:20 +01002015 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002016 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
2017 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002018
aliguorif141eaf2009-04-07 18:43:24 +00002019 ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
2020 cb, opaque);
thsa36e69d2007-12-02 05:18:19 +00002021
2022 if (ret) {
Kevin Wolf294cc352010-04-28 14:34:01 +02002023 /* Update stats even though technically transfer has not happened. */
2024 bs->wr_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
2025 bs->wr_ops ++;
2026 if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
2027 bs->wr_highest_sector = sector_num + nb_sectors - 1;
2028 }
thsa36e69d2007-12-02 05:18:19 +00002029 }
2030
2031 return ret;
bellard83f64092006-08-01 16:21:11 +00002032}
2033
Kevin Wolf40b4f532009-09-09 17:53:37 +02002034
2035typedef struct MultiwriteCB {
2036 int error;
2037 int num_requests;
2038 int num_callbacks;
2039 struct {
2040 BlockDriverCompletionFunc *cb;
2041 void *opaque;
2042 QEMUIOVector *free_qiov;
2043 void *free_buf;
2044 } callbacks[];
2045} MultiwriteCB;
2046
2047static void multiwrite_user_cb(MultiwriteCB *mcb)
2048{
2049 int i;
2050
2051 for (i = 0; i < mcb->num_callbacks; i++) {
2052 mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
Stefan Hajnoczi1e1ea482010-04-21 20:35:45 +01002053 if (mcb->callbacks[i].free_qiov) {
2054 qemu_iovec_destroy(mcb->callbacks[i].free_qiov);
2055 }
Kevin Wolf40b4f532009-09-09 17:53:37 +02002056 qemu_free(mcb->callbacks[i].free_qiov);
Herve Poussineauf8a83242010-01-24 21:23:56 +00002057 qemu_vfree(mcb->callbacks[i].free_buf);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002058 }
2059}
2060
2061static void multiwrite_cb(void *opaque, int ret)
2062{
2063 MultiwriteCB *mcb = opaque;
2064
Kevin Wolfcb6d3ca2010-04-01 22:48:44 +02002065 if (ret < 0 && !mcb->error) {
Kevin Wolf40b4f532009-09-09 17:53:37 +02002066 mcb->error = ret;
Kevin Wolf40b4f532009-09-09 17:53:37 +02002067 }
2068
2069 mcb->num_requests--;
2070 if (mcb->num_requests == 0) {
Kevin Wolfde189a12010-07-01 16:08:51 +02002071 multiwrite_user_cb(mcb);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002072 qemu_free(mcb);
2073 }
2074}
2075
2076static int multiwrite_req_compare(const void *a, const void *b)
2077{
Christoph Hellwig77be4362010-05-19 20:53:10 +02002078 const BlockRequest *req1 = a, *req2 = b;
2079
2080 /*
2081 * Note that we can't simply subtract req2->sector from req1->sector
2082 * here as that could overflow the return value.
2083 */
2084 if (req1->sector > req2->sector) {
2085 return 1;
2086 } else if (req1->sector < req2->sector) {
2087 return -1;
2088 } else {
2089 return 0;
2090 }
Kevin Wolf40b4f532009-09-09 17:53:37 +02002091}
2092
2093/*
2094 * Takes a bunch of requests and tries to merge them. Returns the number of
2095 * requests that remain after merging.
2096 */
2097static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
2098 int num_reqs, MultiwriteCB *mcb)
2099{
2100 int i, outidx;
2101
2102 // Sort requests by start sector
2103 qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
2104
2105 // Check if adjacent requests touch the same clusters. If so, combine them,
2106 // filling up gaps with zero sectors.
2107 outidx = 0;
2108 for (i = 1; i < num_reqs; i++) {
2109 int merge = 0;
2110 int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
2111
2112 // This handles the cases that are valid for all block drivers, namely
2113 // exactly sequential writes and overlapping writes.
2114 if (reqs[i].sector <= oldreq_last) {
2115 merge = 1;
2116 }
2117
2118 // The block driver may decide that it makes sense to combine requests
2119 // even if there is a gap of some sectors between them. In this case,
2120 // the gap is filled with zeros (therefore only applicable for yet
2121 // unused space in format like qcow2).
2122 if (!merge && bs->drv->bdrv_merge_requests) {
2123 merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]);
2124 }
2125
Christoph Hellwige2a305f2010-01-26 14:49:08 +01002126 if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) {
2127 merge = 0;
2128 }
2129
Kevin Wolf40b4f532009-09-09 17:53:37 +02002130 if (merge) {
2131 size_t size;
2132 QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov));
2133 qemu_iovec_init(qiov,
2134 reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
2135
2136 // Add the first request to the merged one. If the requests are
2137 // overlapping, drop the last sectors of the first request.
2138 size = (reqs[i].sector - reqs[outidx].sector) << 9;
2139 qemu_iovec_concat(qiov, reqs[outidx].qiov, size);
2140
2141 // We might need to add some zeros between the two requests
2142 if (reqs[i].sector > oldreq_last) {
2143 size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
2144 uint8_t *buf = qemu_blockalign(bs, zero_bytes);
2145 memset(buf, 0, zero_bytes);
2146 qemu_iovec_add(qiov, buf, zero_bytes);
2147 mcb->callbacks[i].free_buf = buf;
2148 }
2149
2150 // Add the second request
2151 qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size);
2152
Kevin Wolfcbf1dff2010-05-21 11:09:42 +02002153 reqs[outidx].nb_sectors = qiov->size >> 9;
Kevin Wolf40b4f532009-09-09 17:53:37 +02002154 reqs[outidx].qiov = qiov;
2155
2156 mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
2157 } else {
2158 outidx++;
2159 reqs[outidx].sector = reqs[i].sector;
2160 reqs[outidx].nb_sectors = reqs[i].nb_sectors;
2161 reqs[outidx].qiov = reqs[i].qiov;
2162 }
2163 }
2164
2165 return outidx + 1;
2166}
2167
2168/*
2169 * Submit multiple AIO write requests at once.
2170 *
2171 * On success, the function returns 0 and all requests in the reqs array have
2172 * been submitted. In error case this function returns -1, and any of the
2173 * requests may or may not be submitted yet. In particular, this means that the
2174 * callback will be called for some of the requests, for others it won't. The
2175 * caller must check the error field of the BlockRequest to wait for the right
2176 * callbacks (if error != 0, no callback will be called).
2177 *
2178 * The implementation may modify the contents of the reqs array, e.g. to merge
2179 * requests. However, the fields opaque and error are left unmodified as they
2180 * are used to signal failure for a single request to the caller.
2181 */
2182int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
2183{
2184 BlockDriverAIOCB *acb;
2185 MultiwriteCB *mcb;
2186 int i;
2187
2188 if (num_reqs == 0) {
2189 return 0;
2190 }
2191
2192 // Create MultiwriteCB structure
2193 mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
2194 mcb->num_requests = 0;
2195 mcb->num_callbacks = num_reqs;
2196
2197 for (i = 0; i < num_reqs; i++) {
2198 mcb->callbacks[i].cb = reqs[i].cb;
2199 mcb->callbacks[i].opaque = reqs[i].opaque;
2200 }
2201
2202 // Check for mergable requests
2203 num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
2204
Kevin Wolf453f9a12010-07-02 14:01:21 +02002205 /*
2206 * Run the aio requests. As soon as one request can't be submitted
2207 * successfully, fail all requests that are not yet submitted (we must
2208 * return failure for all requests anyway)
2209 *
2210 * num_requests cannot be set to the right value immediately: If
2211 * bdrv_aio_writev fails for some request, num_requests would be too high
2212 * and therefore multiwrite_cb() would never recognize the multiwrite
2213 * request as completed. We also cannot use the loop variable i to set it
2214 * when the first request fails because the callback may already have been
2215 * called for previously submitted requests. Thus, num_requests must be
2216 * incremented for each request that is submitted.
2217 *
2218 * The problem that callbacks may be called early also means that we need
2219 * to take care that num_requests doesn't become 0 before all requests are
2220 * submitted - multiwrite_cb() would consider the multiwrite request
2221 * completed. A dummy request that is "completed" by a manual call to
2222 * multiwrite_cb() takes care of this.
2223 */
2224 mcb->num_requests = 1;
2225
Kevin Wolf40b4f532009-09-09 17:53:37 +02002226 for (i = 0; i < num_reqs; i++) {
Kevin Wolf453f9a12010-07-02 14:01:21 +02002227 mcb->num_requests++;
Kevin Wolf40b4f532009-09-09 17:53:37 +02002228 acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
2229 reqs[i].nb_sectors, multiwrite_cb, mcb);
2230
2231 if (acb == NULL) {
2232 // We can only fail the whole thing if no request has been
2233 // submitted yet. Otherwise we'll wait for the submitted AIOs to
2234 // complete and report the error in the callback.
Kevin Wolf453f9a12010-07-02 14:01:21 +02002235 if (i == 0) {
Kevin Wolf40b4f532009-09-09 17:53:37 +02002236 goto fail;
2237 } else {
Kevin Wolf7eb58a62010-04-06 18:24:07 +02002238 multiwrite_cb(mcb, -EIO);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002239 break;
2240 }
Kevin Wolf40b4f532009-09-09 17:53:37 +02002241 }
2242 }
2243
Kevin Wolf453f9a12010-07-02 14:01:21 +02002244 /* Complete the dummy request */
2245 multiwrite_cb(mcb, 0);
2246
Kevin Wolf40b4f532009-09-09 17:53:37 +02002247 return 0;
2248
2249fail:
Kevin Wolf453f9a12010-07-02 14:01:21 +02002250 for (i = 0; i < mcb->num_callbacks; i++) {
2251 reqs[i].error = -EIO;
2252 }
Bruce Rogersaf474592010-05-13 15:14:33 -06002253 qemu_free(mcb);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002254 return -1;
2255}
2256
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002257BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
2258 BlockDriverCompletionFunc *cb, void *opaque)
2259{
2260 BlockDriver *drv = bs->drv;
2261
Alexander Graf016f5cf2010-05-26 17:51:49 +02002262 if (bs->open_flags & BDRV_O_NO_FLUSH) {
2263 return bdrv_aio_noop_em(bs, cb, opaque);
2264 }
2265
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002266 if (!drv)
2267 return NULL;
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002268 return drv->bdrv_aio_flush(bs, cb, opaque);
2269}
2270
bellard83f64092006-08-01 16:21:11 +00002271void bdrv_aio_cancel(BlockDriverAIOCB *acb)
pbrookce1a14d2006-08-07 02:38:06 +00002272{
aliguori6bbff9a2009-03-20 18:25:59 +00002273 acb->pool->cancel(acb);
bellard83f64092006-08-01 16:21:11 +00002274}
2275
pbrookce1a14d2006-08-07 02:38:06 +00002276
bellard83f64092006-08-01 16:21:11 +00002277/**************************************************************/
2278/* async block device emulation */
2279
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002280typedef struct BlockDriverAIOCBSync {
2281 BlockDriverAIOCB common;
2282 QEMUBH *bh;
2283 int ret;
2284 /* vector translation state */
2285 QEMUIOVector *qiov;
2286 uint8_t *bounce;
2287 int is_write;
2288} BlockDriverAIOCBSync;
2289
2290static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
2291{
Kevin Wolfb666d232010-05-05 11:44:39 +02002292 BlockDriverAIOCBSync *acb =
2293 container_of(blockacb, BlockDriverAIOCBSync, common);
Dor Laor6a7ad292009-06-01 12:07:23 +03002294 qemu_bh_delete(acb->bh);
Avi Kivity36afc452009-06-23 16:20:36 +03002295 acb->bh = NULL;
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002296 qemu_aio_release(acb);
2297}
2298
2299static AIOPool bdrv_em_aio_pool = {
2300 .aiocb_size = sizeof(BlockDriverAIOCBSync),
2301 .cancel = bdrv_aio_cancel_em,
2302};
2303
bellard83f64092006-08-01 16:21:11 +00002304static void bdrv_aio_bh_cb(void *opaque)
bellardbeac80c2006-06-26 20:08:57 +00002305{
pbrookce1a14d2006-08-07 02:38:06 +00002306 BlockDriverAIOCBSync *acb = opaque;
aliguorif141eaf2009-04-07 18:43:24 +00002307
aliguorif141eaf2009-04-07 18:43:24 +00002308 if (!acb->is_write)
2309 qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
aliguoriceb42de2009-04-07 18:43:28 +00002310 qemu_vfree(acb->bounce);
pbrookce1a14d2006-08-07 02:38:06 +00002311 acb->common.cb(acb->common.opaque, acb->ret);
Dor Laor6a7ad292009-06-01 12:07:23 +03002312 qemu_bh_delete(acb->bh);
Avi Kivity36afc452009-06-23 16:20:36 +03002313 acb->bh = NULL;
pbrookce1a14d2006-08-07 02:38:06 +00002314 qemu_aio_release(acb);
bellardbeac80c2006-06-26 20:08:57 +00002315}
bellardbeac80c2006-06-26 20:08:57 +00002316
aliguorif141eaf2009-04-07 18:43:24 +00002317static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
2318 int64_t sector_num,
2319 QEMUIOVector *qiov,
2320 int nb_sectors,
2321 BlockDriverCompletionFunc *cb,
2322 void *opaque,
2323 int is_write)
2324
bellardea2384d2004-08-01 21:59:26 +00002325{
pbrookce1a14d2006-08-07 02:38:06 +00002326 BlockDriverAIOCBSync *acb;
pbrookce1a14d2006-08-07 02:38:06 +00002327
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002328 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
aliguorif141eaf2009-04-07 18:43:24 +00002329 acb->is_write = is_write;
2330 acb->qiov = qiov;
aliguorie268ca52009-04-22 20:20:00 +00002331 acb->bounce = qemu_blockalign(bs, qiov->size);
aliguorif141eaf2009-04-07 18:43:24 +00002332
pbrookce1a14d2006-08-07 02:38:06 +00002333 if (!acb->bh)
2334 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
aliguorif141eaf2009-04-07 18:43:24 +00002335
2336 if (is_write) {
2337 qemu_iovec_to_buffer(acb->qiov, acb->bounce);
2338 acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
2339 } else {
2340 acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
2341 }
2342
pbrookce1a14d2006-08-07 02:38:06 +00002343 qemu_bh_schedule(acb->bh);
aliguorif141eaf2009-04-07 18:43:24 +00002344
pbrookce1a14d2006-08-07 02:38:06 +00002345 return &acb->common;
pbrook7a6cba62006-06-04 11:39:07 +00002346}
2347
aliguorif141eaf2009-04-07 18:43:24 +00002348static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
2349 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
pbrookce1a14d2006-08-07 02:38:06 +00002350 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00002351{
aliguorif141eaf2009-04-07 18:43:24 +00002352 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
bellard83f64092006-08-01 16:21:11 +00002353}
2354
aliguorif141eaf2009-04-07 18:43:24 +00002355static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
2356 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2357 BlockDriverCompletionFunc *cb, void *opaque)
2358{
2359 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
2360}
2361
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002362static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
2363 BlockDriverCompletionFunc *cb, void *opaque)
2364{
2365 BlockDriverAIOCBSync *acb;
2366
2367 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2368 acb->is_write = 1; /* don't bounce in the completion hadler */
2369 acb->qiov = NULL;
2370 acb->bounce = NULL;
2371 acb->ret = 0;
2372
2373 if (!acb->bh)
2374 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2375
2376 bdrv_flush(bs);
2377 qemu_bh_schedule(acb->bh);
2378 return &acb->common;
2379}
2380
Alexander Graf016f5cf2010-05-26 17:51:49 +02002381static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,
2382 BlockDriverCompletionFunc *cb, void *opaque)
2383{
2384 BlockDriverAIOCBSync *acb;
2385
2386 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2387 acb->is_write = 1; /* don't bounce in the completion handler */
2388 acb->qiov = NULL;
2389 acb->bounce = NULL;
2390 acb->ret = 0;
2391
2392 if (!acb->bh) {
2393 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2394 }
2395
2396 qemu_bh_schedule(acb->bh);
2397 return &acb->common;
2398}
2399
bellard83f64092006-08-01 16:21:11 +00002400/**************************************************************/
2401/* sync block device emulation */
2402
2403static void bdrv_rw_em_cb(void *opaque, int ret)
2404{
2405 *(int *)opaque = ret;
2406}
2407
2408#define NOT_DONE 0x7fffffff
2409
ths5fafdf22007-09-16 21:08:06 +00002410static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +00002411 uint8_t *buf, int nb_sectors)
2412{
pbrookce1a14d2006-08-07 02:38:06 +00002413 int async_ret;
2414 BlockDriverAIOCB *acb;
aliguorif141eaf2009-04-07 18:43:24 +00002415 struct iovec iov;
2416 QEMUIOVector qiov;
bellard83f64092006-08-01 16:21:11 +00002417
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002418 async_context_push();
2419
bellard83f64092006-08-01 16:21:11 +00002420 async_ret = NOT_DONE;
blueswir13f4cb3d2009-04-13 16:31:01 +00002421 iov.iov_base = (void *)buf;
Jes Sorenseneb5a3162010-05-27 16:20:31 +02002422 iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
aliguorif141eaf2009-04-07 18:43:24 +00002423 qemu_iovec_init_external(&qiov, &iov, 1);
2424 acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
2425 bdrv_rw_em_cb, &async_ret);
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002426 if (acb == NULL) {
2427 async_ret = -1;
2428 goto fail;
2429 }
aliguoribaf35cb2008-09-10 15:45:19 +00002430
bellard83f64092006-08-01 16:21:11 +00002431 while (async_ret == NOT_DONE) {
2432 qemu_aio_wait();
2433 }
aliguoribaf35cb2008-09-10 15:45:19 +00002434
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002435
2436fail:
2437 async_context_pop();
bellard83f64092006-08-01 16:21:11 +00002438 return async_ret;
2439}
2440
2441static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
2442 const uint8_t *buf, int nb_sectors)
2443{
pbrookce1a14d2006-08-07 02:38:06 +00002444 int async_ret;
2445 BlockDriverAIOCB *acb;
aliguorif141eaf2009-04-07 18:43:24 +00002446 struct iovec iov;
2447 QEMUIOVector qiov;
bellard83f64092006-08-01 16:21:11 +00002448
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002449 async_context_push();
2450
bellard83f64092006-08-01 16:21:11 +00002451 async_ret = NOT_DONE;
aliguorif141eaf2009-04-07 18:43:24 +00002452 iov.iov_base = (void *)buf;
Jes Sorenseneb5a3162010-05-27 16:20:31 +02002453 iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
aliguorif141eaf2009-04-07 18:43:24 +00002454 qemu_iovec_init_external(&qiov, &iov, 1);
2455 acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
2456 bdrv_rw_em_cb, &async_ret);
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002457 if (acb == NULL) {
2458 async_ret = -1;
2459 goto fail;
2460 }
bellard83f64092006-08-01 16:21:11 +00002461 while (async_ret == NOT_DONE) {
2462 qemu_aio_wait();
2463 }
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002464
2465fail:
2466 async_context_pop();
bellard83f64092006-08-01 16:21:11 +00002467 return async_ret;
2468}
bellardea2384d2004-08-01 21:59:26 +00002469
2470void bdrv_init(void)
2471{
Anthony Liguori5efa9d52009-05-09 17:03:42 -05002472 module_call_init(MODULE_INIT_BLOCK);
bellardea2384d2004-08-01 21:59:26 +00002473}
pbrookce1a14d2006-08-07 02:38:06 +00002474
Markus Armbrustereb852012009-10-27 18:41:44 +01002475void bdrv_init_with_whitelist(void)
2476{
2477 use_bdrv_whitelist = 1;
2478 bdrv_init();
2479}
2480
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002481void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
2482 BlockDriverCompletionFunc *cb, void *opaque)
aliguori6bbff9a2009-03-20 18:25:59 +00002483{
pbrookce1a14d2006-08-07 02:38:06 +00002484 BlockDriverAIOCB *acb;
2485
aliguori6bbff9a2009-03-20 18:25:59 +00002486 if (pool->free_aiocb) {
2487 acb = pool->free_aiocb;
2488 pool->free_aiocb = acb->next;
pbrookce1a14d2006-08-07 02:38:06 +00002489 } else {
aliguori6bbff9a2009-03-20 18:25:59 +00002490 acb = qemu_mallocz(pool->aiocb_size);
2491 acb->pool = pool;
pbrookce1a14d2006-08-07 02:38:06 +00002492 }
2493 acb->bs = bs;
2494 acb->cb = cb;
2495 acb->opaque = opaque;
2496 return acb;
2497}
2498
2499void qemu_aio_release(void *p)
2500{
aliguori6bbff9a2009-03-20 18:25:59 +00002501 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
2502 AIOPool *pool = acb->pool;
2503 acb->next = pool->free_aiocb;
2504 pool->free_aiocb = acb;
pbrookce1a14d2006-08-07 02:38:06 +00002505}
bellard19cb3732006-08-19 11:45:59 +00002506
2507/**************************************************************/
2508/* removable device support */
2509
2510/**
2511 * Return TRUE if the media is present
2512 */
2513int bdrv_is_inserted(BlockDriverState *bs)
2514{
2515 BlockDriver *drv = bs->drv;
2516 int ret;
2517 if (!drv)
2518 return 0;
2519 if (!drv->bdrv_is_inserted)
2520 return 1;
2521 ret = drv->bdrv_is_inserted(bs);
2522 return ret;
2523}
2524
2525/**
2526 * Return TRUE if the media changed since the last call to this
ths5fafdf22007-09-16 21:08:06 +00002527 * function. It is currently only used for floppy disks
bellard19cb3732006-08-19 11:45:59 +00002528 */
2529int bdrv_media_changed(BlockDriverState *bs)
2530{
2531 BlockDriver *drv = bs->drv;
2532 int ret;
2533
2534 if (!drv || !drv->bdrv_media_changed)
2535 ret = -ENOTSUP;
2536 else
2537 ret = drv->bdrv_media_changed(bs);
2538 if (ret == -ENOTSUP)
2539 ret = bs->media_changed;
2540 bs->media_changed = 0;
2541 return ret;
2542}
2543
2544/**
2545 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
2546 */
Mark McLoughlinaea2a332009-05-27 10:06:11 +01002547int bdrv_eject(BlockDriverState *bs, int eject_flag)
bellard19cb3732006-08-19 11:45:59 +00002548{
2549 BlockDriver *drv = bs->drv;
2550 int ret;
2551
Mark McLoughlinaea2a332009-05-27 10:06:11 +01002552 if (bs->locked) {
2553 return -EBUSY;
2554 }
2555
bellard19cb3732006-08-19 11:45:59 +00002556 if (!drv || !drv->bdrv_eject) {
2557 ret = -ENOTSUP;
2558 } else {
2559 ret = drv->bdrv_eject(bs, eject_flag);
2560 }
2561 if (ret == -ENOTSUP) {
2562 if (eject_flag)
2563 bdrv_close(bs);
Mark McLoughlinaea2a332009-05-27 10:06:11 +01002564 ret = 0;
bellard19cb3732006-08-19 11:45:59 +00002565 }
Mark McLoughlinaea2a332009-05-27 10:06:11 +01002566
2567 return ret;
bellard19cb3732006-08-19 11:45:59 +00002568}
2569
2570int bdrv_is_locked(BlockDriverState *bs)
2571{
2572 return bs->locked;
2573}
2574
2575/**
2576 * Lock or unlock the media (if it is locked, the user won't be able
2577 * to eject it manually).
2578 */
2579void bdrv_set_locked(BlockDriverState *bs, int locked)
2580{
2581 BlockDriver *drv = bs->drv;
2582
2583 bs->locked = locked;
2584 if (drv && drv->bdrv_set_locked) {
2585 drv->bdrv_set_locked(bs, locked);
2586 }
2587}
ths985a03b2007-12-24 16:10:43 +00002588
2589/* needed for generic scsi interface */
2590
2591int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
2592{
2593 BlockDriver *drv = bs->drv;
2594
2595 if (drv && drv->bdrv_ioctl)
2596 return drv->bdrv_ioctl(bs, req, buf);
2597 return -ENOTSUP;
2598}
aliguori7d780662009-03-12 19:57:08 +00002599
aliguori221f7152009-03-28 17:28:41 +00002600BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
2601 unsigned long int req, void *buf,
2602 BlockDriverCompletionFunc *cb, void *opaque)
aliguori7d780662009-03-12 19:57:08 +00002603{
aliguori221f7152009-03-28 17:28:41 +00002604 BlockDriver *drv = bs->drv;
aliguori7d780662009-03-12 19:57:08 +00002605
aliguori221f7152009-03-28 17:28:41 +00002606 if (drv && drv->bdrv_aio_ioctl)
2607 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
2608 return NULL;
aliguori7d780662009-03-12 19:57:08 +00002609}
aliguorie268ca52009-04-22 20:20:00 +00002610
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002611
2612
aliguorie268ca52009-04-22 20:20:00 +00002613void *qemu_blockalign(BlockDriverState *bs, size_t size)
2614{
2615 return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
2616}
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002617
2618void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable)
2619{
2620 int64_t bitmap_size;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002621
Liran Schouraaa0eb72010-01-26 10:31:48 +02002622 bs->dirty_count = 0;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002623 if (enable) {
Jan Kiszkac6d22832009-11-30 18:21:20 +01002624 if (!bs->dirty_bitmap) {
2625 bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
2626 BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
2627 bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002628
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002629 bs->dirty_bitmap = qemu_mallocz(bitmap_size);
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002630 }
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002631 } else {
Jan Kiszkac6d22832009-11-30 18:21:20 +01002632 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002633 qemu_free(bs->dirty_bitmap);
Jan Kiszkac6d22832009-11-30 18:21:20 +01002634 bs->dirty_bitmap = NULL;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002635 }
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002636 }
2637}
2638
2639int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
2640{
Jan Kiszka6ea44302009-11-30 18:21:19 +01002641 int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002642
Jan Kiszkac6d22832009-11-30 18:21:20 +01002643 if (bs->dirty_bitmap &&
2644 (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) {
2645 return bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] &
2646 (1 << (chunk % (sizeof(unsigned long) * 8)));
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002647 } else {
2648 return 0;
2649 }
2650}
2651
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002652void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
2653 int nr_sectors)
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002654{
2655 set_dirty_bitmap(bs, cur_sector, nr_sectors, 0);
2656}
Liran Schouraaa0eb72010-01-26 10:31:48 +02002657
2658int64_t bdrv_get_dirty_count(BlockDriverState *bs)
2659{
2660 return bs->dirty_count;
2661}