blob: fb9341c9d213a1d2b86fa1589a67b82910ee6d0b [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"
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +010026#include "trace.h"
aliguori376253e2009-03-05 23:01:23 +000027#include "monitor.h"
bellardea2384d2004-08-01 21:59:26 +000028#include "block_int.h"
Anthony Liguori5efa9d52009-05-09 17:03:42 -050029#include "module.h"
Luiz Capitulinod15e5462009-12-10 17:16:06 -020030#include "qemu-objects.h"
Kevin Wolf68485422011-06-30 10:05:46 +020031#include "qemu-coroutine.h"
bellardfc01f7e2003-06-30 10:03:06 +000032
Juan Quintela71e72a12009-07-27 16:12:56 +020033#ifdef CONFIG_BSD
bellard7674e7b2005-04-26 21:59:26 +000034#include <sys/types.h>
35#include <sys/stat.h>
36#include <sys/ioctl.h>
Blue Swirl72cf2d42009-09-12 07:36:22 +000037#include <sys/queue.h>
blueswir1c5e97232009-03-07 20:06:23 +000038#ifndef __DragonFly__
bellard7674e7b2005-04-26 21:59:26 +000039#include <sys/disk.h>
40#endif
blueswir1c5e97232009-03-07 20:06:23 +000041#endif
bellard7674e7b2005-04-26 21:59:26 +000042
aliguori49dc7682009-03-08 16:26:59 +000043#ifdef _WIN32
44#include <windows.h>
45#endif
46
aliguorif141eaf2009-04-07 18:43:24 +000047static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
48 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
aliguoric87c0672009-04-07 18:43:20 +000049 BlockDriverCompletionFunc *cb, void *opaque);
aliguorif141eaf2009-04-07 18:43:24 +000050static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
51 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
pbrookce1a14d2006-08-07 02:38:06 +000052 BlockDriverCompletionFunc *cb, void *opaque);
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +020053static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
54 BlockDriverCompletionFunc *cb, void *opaque);
Alexander Graf016f5cf2010-05-26 17:51:49 +020055static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,
56 BlockDriverCompletionFunc *cb, void *opaque);
ths5fafdf22007-09-16 21:08:06 +000057static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +000058 uint8_t *buf, int nb_sectors);
59static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
60 const uint8_t *buf, int nb_sectors);
Kevin Wolf68485422011-06-30 10:05:46 +020061static BlockDriverAIOCB *bdrv_co_aio_readv_em(BlockDriverState *bs,
62 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
63 BlockDriverCompletionFunc *cb, void *opaque);
64static BlockDriverAIOCB *bdrv_co_aio_writev_em(BlockDriverState *bs,
65 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
66 BlockDriverCompletionFunc *cb, void *opaque);
Kevin Wolff9f05dc2011-07-15 13:50:26 +020067static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs,
68 int64_t sector_num, int nb_sectors,
69 QEMUIOVector *iov);
70static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs,
71 int64_t sector_num, int nb_sectors,
72 QEMUIOVector *iov);
Kevin Wolfe7a8a782011-07-15 16:05:00 +020073static int coroutine_fn bdrv_co_flush_em(BlockDriverState *bs);
bellardec530c82006-04-25 22:36:06 +000074
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +010075static QTAILQ_HEAD(, BlockDriverState) bdrv_states =
76 QTAILQ_HEAD_INITIALIZER(bdrv_states);
blueswir17ee930d2008-09-17 19:04:14 +000077
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +010078static QLIST_HEAD(, BlockDriver) bdrv_drivers =
79 QLIST_HEAD_INITIALIZER(bdrv_drivers);
bellardea2384d2004-08-01 21:59:26 +000080
Markus Armbrusterf9092b12010-06-25 10:33:39 +020081/* The device to use for VM snapshots */
82static BlockDriverState *bs_snapshots;
83
Markus Armbrustereb852012009-10-27 18:41:44 +010084/* If non-zero, use only whitelisted block drivers */
85static int use_bdrv_whitelist;
86
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +000087#ifdef _WIN32
88static int is_windows_drive_prefix(const char *filename)
89{
90 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
91 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
92 filename[1] == ':');
93}
94
95int is_windows_drive(const char *filename)
96{
97 if (is_windows_drive_prefix(filename) &&
98 filename[2] == '\0')
99 return 1;
100 if (strstart(filename, "\\\\.\\", NULL) ||
101 strstart(filename, "//./", NULL))
102 return 1;
103 return 0;
104}
105#endif
106
107/* check if the path starts with "<protocol>:" */
108static int path_has_protocol(const char *path)
109{
110#ifdef _WIN32
111 if (is_windows_drive(path) ||
112 is_windows_drive_prefix(path)) {
113 return 0;
114 }
115#endif
116
117 return strchr(path, ':') != NULL;
118}
119
bellard83f64092006-08-01 16:21:11 +0000120int path_is_absolute(const char *path)
121{
122 const char *p;
bellard21664422007-01-07 18:22:37 +0000123#ifdef _WIN32
124 /* specific case for names like: "\\.\d:" */
125 if (*path == '/' || *path == '\\')
126 return 1;
127#endif
bellard83f64092006-08-01 16:21:11 +0000128 p = strchr(path, ':');
129 if (p)
130 p++;
131 else
132 p = path;
bellard3b9f94e2007-01-07 17:27:07 +0000133#ifdef _WIN32
134 return (*p == '/' || *p == '\\');
135#else
136 return (*p == '/');
137#endif
bellard83f64092006-08-01 16:21:11 +0000138}
139
140/* if filename is absolute, just copy it to dest. Otherwise, build a
141 path to it by considering it is relative to base_path. URL are
142 supported. */
143void path_combine(char *dest, int dest_size,
144 const char *base_path,
145 const char *filename)
146{
147 const char *p, *p1;
148 int len;
149
150 if (dest_size <= 0)
151 return;
152 if (path_is_absolute(filename)) {
153 pstrcpy(dest, dest_size, filename);
154 } else {
155 p = strchr(base_path, ':');
156 if (p)
157 p++;
158 else
159 p = base_path;
bellard3b9f94e2007-01-07 17:27:07 +0000160 p1 = strrchr(base_path, '/');
161#ifdef _WIN32
162 {
163 const char *p2;
164 p2 = strrchr(base_path, '\\');
165 if (!p1 || p2 > p1)
166 p1 = p2;
167 }
168#endif
bellard83f64092006-08-01 16:21:11 +0000169 if (p1)
170 p1++;
171 else
172 p1 = base_path;
173 if (p1 > p)
174 p = p1;
175 len = p - base_path;
176 if (len > dest_size - 1)
177 len = dest_size - 1;
178 memcpy(dest, base_path, len);
179 dest[len] = '\0';
180 pstrcat(dest, dest_size, filename);
181 }
182}
183
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500184void bdrv_register(BlockDriver *bdrv)
bellardea2384d2004-08-01 21:59:26 +0000185{
Kevin Wolf68485422011-06-30 10:05:46 +0200186 if (bdrv->bdrv_co_readv) {
187 /* Emulate AIO by coroutines, and sync by AIO */
188 bdrv->bdrv_aio_readv = bdrv_co_aio_readv_em;
189 bdrv->bdrv_aio_writev = bdrv_co_aio_writev_em;
190 bdrv->bdrv_read = bdrv_read_em;
191 bdrv->bdrv_write = bdrv_write_em;
Kevin Wolff9f05dc2011-07-15 13:50:26 +0200192 } else {
193 bdrv->bdrv_co_readv = bdrv_co_readv_em;
194 bdrv->bdrv_co_writev = bdrv_co_writev_em;
195
196 if (!bdrv->bdrv_aio_readv) {
197 /* add AIO emulation layer */
198 bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
199 bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
200 } else if (!bdrv->bdrv_read) {
201 /* add synchronous IO emulation layer */
202 bdrv->bdrv_read = bdrv_read_em;
203 bdrv->bdrv_write = bdrv_write_em;
204 }
bellard83f64092006-08-01 16:21:11 +0000205 }
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +0200206
207 if (!bdrv->bdrv_aio_flush)
208 bdrv->bdrv_aio_flush = bdrv_aio_flush_em;
209
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100210 QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
bellardea2384d2004-08-01 21:59:26 +0000211}
bellardb3380822004-03-14 21:38:54 +0000212
213/* create a new block device (by default it is empty) */
214BlockDriverState *bdrv_new(const char *device_name)
bellardfc01f7e2003-06-30 10:03:06 +0000215{
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +0100216 BlockDriverState *bs;
bellardb3380822004-03-14 21:38:54 +0000217
Anthony Liguori7267c092011-08-20 22:09:37 -0500218 bs = g_malloc0(sizeof(BlockDriverState));
bellardb3380822004-03-14 21:38:54 +0000219 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
bellardea2384d2004-08-01 21:59:26 +0000220 if (device_name[0] != '\0') {
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +0100221 QTAILQ_INSERT_TAIL(&bdrv_states, bs, list);
bellardea2384d2004-08-01 21:59:26 +0000222 }
bellardb3380822004-03-14 21:38:54 +0000223 return bs;
224}
225
bellardea2384d2004-08-01 21:59:26 +0000226BlockDriver *bdrv_find_format(const char *format_name)
227{
228 BlockDriver *drv1;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100229 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
230 if (!strcmp(drv1->format_name, format_name)) {
bellardea2384d2004-08-01 21:59:26 +0000231 return drv1;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100232 }
bellardea2384d2004-08-01 21:59:26 +0000233 }
234 return NULL;
235}
236
Markus Armbrustereb852012009-10-27 18:41:44 +0100237static int bdrv_is_whitelisted(BlockDriver *drv)
238{
239 static const char *whitelist[] = {
240 CONFIG_BDRV_WHITELIST
241 };
242 const char **p;
243
244 if (!whitelist[0])
245 return 1; /* no whitelist, anything goes */
246
247 for (p = whitelist; *p; p++) {
248 if (!strcmp(drv->format_name, *p)) {
249 return 1;
250 }
251 }
252 return 0;
253}
254
255BlockDriver *bdrv_find_whitelisted_format(const char *format_name)
256{
257 BlockDriver *drv = bdrv_find_format(format_name);
258 return drv && bdrv_is_whitelisted(drv) ? drv : NULL;
259}
260
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200261int bdrv_create(BlockDriver *drv, const char* filename,
262 QEMUOptionParameter *options)
bellardea2384d2004-08-01 21:59:26 +0000263{
264 if (!drv->bdrv_create)
265 return -ENOTSUP;
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200266
267 return drv->bdrv_create(filename, options);
bellardea2384d2004-08-01 21:59:26 +0000268}
269
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200270int bdrv_create_file(const char* filename, QEMUOptionParameter *options)
271{
272 BlockDriver *drv;
273
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900274 drv = bdrv_find_protocol(filename);
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200275 if (drv == NULL) {
Stefan Hajnoczi16905d72010-11-30 15:14:14 +0000276 return -ENOENT;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200277 }
278
279 return bdrv_create(drv, filename, options);
280}
281
bellardd5249392004-08-03 21:14:23 +0000282#ifdef _WIN32
bellard95389c82005-12-18 18:28:15 +0000283void get_tmp_filename(char *filename, int size)
bellardd5249392004-08-03 21:14:23 +0000284{
bellard3b9f94e2007-01-07 17:27:07 +0000285 char temp_dir[MAX_PATH];
ths3b46e622007-09-17 08:09:54 +0000286
bellard3b9f94e2007-01-07 17:27:07 +0000287 GetTempPath(MAX_PATH, temp_dir);
288 GetTempFileName(temp_dir, "qem", 0, filename);
bellardd5249392004-08-03 21:14:23 +0000289}
290#else
bellard95389c82005-12-18 18:28:15 +0000291void get_tmp_filename(char *filename, int size)
bellardea2384d2004-08-01 21:59:26 +0000292{
293 int fd;
blueswir17ccfb2e2008-09-14 06:45:34 +0000294 const char *tmpdir;
bellardd5249392004-08-03 21:14:23 +0000295 /* XXX: race condition possible */
aurel320badc1e2008-03-10 00:05:34 +0000296 tmpdir = getenv("TMPDIR");
297 if (!tmpdir)
298 tmpdir = "/tmp";
299 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
bellardea2384d2004-08-01 21:59:26 +0000300 fd = mkstemp(filename);
301 close(fd);
302}
bellardd5249392004-08-03 21:14:23 +0000303#endif
bellardea2384d2004-08-01 21:59:26 +0000304
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200305/*
306 * Detect host devices. By convention, /dev/cdrom[N] is always
307 * recognized as a host CDROM.
308 */
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200309static BlockDriver *find_hdev_driver(const char *filename)
310{
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200311 int score_max = 0, score;
312 BlockDriver *drv = NULL, *d;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200313
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100314 QLIST_FOREACH(d, &bdrv_drivers, list) {
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200315 if (d->bdrv_probe_device) {
316 score = d->bdrv_probe_device(filename);
317 if (score > score_max) {
318 score_max = score;
319 drv = d;
320 }
321 }
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200322 }
323
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200324 return drv;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200325}
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200326
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900327BlockDriver *bdrv_find_protocol(const char *filename)
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200328{
329 BlockDriver *drv1;
330 char protocol[128];
331 int len;
332 const char *p;
333
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200334 /* TODO Drivers without bdrv_file_open must be specified explicitly */
335
Christoph Hellwig39508e72010-06-23 12:25:17 +0200336 /*
337 * XXX(hch): we really should not let host device detection
338 * override an explicit protocol specification, but moving this
339 * later breaks access to device names with colons in them.
340 * Thanks to the brain-dead persistent naming schemes on udev-
341 * based Linux systems those actually are quite common.
342 */
343 drv1 = find_hdev_driver(filename);
344 if (drv1) {
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200345 return drv1;
346 }
Christoph Hellwig39508e72010-06-23 12:25:17 +0200347
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000348 if (!path_has_protocol(filename)) {
Christoph Hellwig39508e72010-06-23 12:25:17 +0200349 return bdrv_find_format("file");
350 }
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000351 p = strchr(filename, ':');
352 assert(p != NULL);
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200353 len = p - filename;
354 if (len > sizeof(protocol) - 1)
355 len = sizeof(protocol) - 1;
356 memcpy(protocol, filename, len);
357 protocol[len] = '\0';
358 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
359 if (drv1->protocol_name &&
360 !strcmp(drv1->protocol_name, protocol)) {
361 return drv1;
362 }
363 }
364 return NULL;
365}
366
Stefan Weilc98ac352010-07-21 21:51:51 +0200367static int find_image_format(const char *filename, BlockDriver **pdrv)
bellardea2384d2004-08-01 21:59:26 +0000368{
bellard83f64092006-08-01 16:21:11 +0000369 int ret, score, score_max;
bellardea2384d2004-08-01 21:59:26 +0000370 BlockDriver *drv1, *drv;
bellard83f64092006-08-01 16:21:11 +0000371 uint8_t buf[2048];
372 BlockDriverState *bs;
ths3b46e622007-09-17 08:09:54 +0000373
Naphtali Spreif5edb012010-01-17 16:48:13 +0200374 ret = bdrv_file_open(&bs, filename, 0);
Stefan Weilc98ac352010-07-21 21:51:51 +0200375 if (ret < 0) {
376 *pdrv = NULL;
377 return ret;
378 }
Nicholas Bellingerf8ea0b02010-05-17 09:45:57 -0700379
Kevin Wolf08a00552010-06-01 18:37:31 +0200380 /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
381 if (bs->sg || !bdrv_is_inserted(bs)) {
Nicholas A. Bellinger1a396852010-05-27 08:56:28 -0700382 bdrv_delete(bs);
Stefan Weilc98ac352010-07-21 21:51:51 +0200383 drv = bdrv_find_format("raw");
384 if (!drv) {
385 ret = -ENOENT;
386 }
387 *pdrv = drv;
388 return ret;
Nicholas A. Bellinger1a396852010-05-27 08:56:28 -0700389 }
Nicholas Bellingerf8ea0b02010-05-17 09:45:57 -0700390
bellard83f64092006-08-01 16:21:11 +0000391 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
392 bdrv_delete(bs);
393 if (ret < 0) {
Stefan Weilc98ac352010-07-21 21:51:51 +0200394 *pdrv = NULL;
395 return ret;
bellard83f64092006-08-01 16:21:11 +0000396 }
397
bellardea2384d2004-08-01 21:59:26 +0000398 score_max = 0;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200399 drv = NULL;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100400 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
bellard83f64092006-08-01 16:21:11 +0000401 if (drv1->bdrv_probe) {
402 score = drv1->bdrv_probe(buf, ret, filename);
403 if (score > score_max) {
404 score_max = score;
405 drv = drv1;
406 }
bellardea2384d2004-08-01 21:59:26 +0000407 }
408 }
Stefan Weilc98ac352010-07-21 21:51:51 +0200409 if (!drv) {
410 ret = -ENOENT;
411 }
412 *pdrv = drv;
413 return ret;
bellardea2384d2004-08-01 21:59:26 +0000414}
415
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100416/**
417 * Set the current 'total_sectors' value
418 */
419static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
420{
421 BlockDriver *drv = bs->drv;
422
Nicholas Bellinger396759a2010-05-17 09:46:04 -0700423 /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
424 if (bs->sg)
425 return 0;
426
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100427 /* query actual device if possible, otherwise just trust the hint */
428 if (drv->bdrv_getlength) {
429 int64_t length = drv->bdrv_getlength(bs);
430 if (length < 0) {
431 return length;
432 }
433 hint = length >> BDRV_SECTOR_BITS;
434 }
435
436 bs->total_sectors = hint;
437 return 0;
438}
439
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100440/**
441 * Set open flags for a given cache mode
442 *
443 * Return 0 on success, -1 if the cache mode was invalid.
444 */
445int bdrv_parse_cache_flags(const char *mode, int *flags)
446{
447 *flags &= ~BDRV_O_CACHE_MASK;
448
449 if (!strcmp(mode, "off") || !strcmp(mode, "none")) {
450 *flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
Stefan Hajnoczi92196b22011-08-04 12:26:52 +0100451 } else if (!strcmp(mode, "directsync")) {
452 *flags |= BDRV_O_NOCACHE;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100453 } else if (!strcmp(mode, "writeback")) {
454 *flags |= BDRV_O_CACHE_WB;
455 } else if (!strcmp(mode, "unsafe")) {
456 *flags |= BDRV_O_CACHE_WB;
457 *flags |= BDRV_O_NO_FLUSH;
458 } else if (!strcmp(mode, "writethrough")) {
459 /* this is the default */
460 } else {
461 return -1;
462 }
463
464 return 0;
465}
466
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200467/*
Kevin Wolf57915332010-04-14 15:24:50 +0200468 * Common part for opening disk images and files
469 */
470static int bdrv_open_common(BlockDriverState *bs, const char *filename,
471 int flags, BlockDriver *drv)
472{
473 int ret, open_flags;
474
475 assert(drv != NULL);
476
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200477 bs->file = NULL;
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100478 bs->total_sectors = 0;
Kevin Wolf57915332010-04-14 15:24:50 +0200479 bs->encrypted = 0;
480 bs->valid_key = 0;
481 bs->open_flags = flags;
482 /* buffer_alignment defaulted to 512, drivers can change this value */
483 bs->buffer_alignment = 512;
484
485 pstrcpy(bs->filename, sizeof(bs->filename), filename);
486
487 if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {
488 return -ENOTSUP;
489 }
490
491 bs->drv = drv;
Anthony Liguori7267c092011-08-20 22:09:37 -0500492 bs->opaque = g_malloc0(drv->instance_size);
Kevin Wolf57915332010-04-14 15:24:50 +0200493
Christoph Hellwiga6599792011-05-17 18:04:06 +0200494 if (flags & BDRV_O_CACHE_WB)
Kevin Wolf57915332010-04-14 15:24:50 +0200495 bs->enable_write_cache = 1;
496
497 /*
498 * Clear flags that are internal to the block layer before opening the
499 * image.
500 */
501 open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
502
503 /*
Stefan Weilebabb672011-04-26 10:29:36 +0200504 * Snapshots should be writable.
Kevin Wolf57915332010-04-14 15:24:50 +0200505 */
506 if (bs->is_temporary) {
507 open_flags |= BDRV_O_RDWR;
508 }
509
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200510 /* Open the image, either directly or using a protocol */
511 if (drv->bdrv_file_open) {
512 ret = drv->bdrv_file_open(bs, filename, open_flags);
513 } else {
514 ret = bdrv_file_open(&bs->file, filename, open_flags);
515 if (ret >= 0) {
516 ret = drv->bdrv_open(bs, open_flags);
517 }
518 }
519
Kevin Wolf57915332010-04-14 15:24:50 +0200520 if (ret < 0) {
521 goto free_and_fail;
522 }
523
524 bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR);
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100525
526 ret = refresh_total_sectors(bs, bs->total_sectors);
527 if (ret < 0) {
528 goto free_and_fail;
Kevin Wolf57915332010-04-14 15:24:50 +0200529 }
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100530
Kevin Wolf57915332010-04-14 15:24:50 +0200531#ifndef _WIN32
532 if (bs->is_temporary) {
533 unlink(filename);
534 }
535#endif
536 return 0;
537
538free_and_fail:
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200539 if (bs->file) {
540 bdrv_delete(bs->file);
541 bs->file = NULL;
542 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500543 g_free(bs->opaque);
Kevin Wolf57915332010-04-14 15:24:50 +0200544 bs->opaque = NULL;
545 bs->drv = NULL;
546 return ret;
547}
548
549/*
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200550 * Opens a file using a protocol (file, host_device, nbd, ...)
551 */
bellard83f64092006-08-01 16:21:11 +0000552int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
bellardb3380822004-03-14 21:38:54 +0000553{
bellard83f64092006-08-01 16:21:11 +0000554 BlockDriverState *bs;
Christoph Hellwig6db95602010-04-05 16:53:57 +0200555 BlockDriver *drv;
bellard83f64092006-08-01 16:21:11 +0000556 int ret;
557
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900558 drv = bdrv_find_protocol(filename);
Christoph Hellwig6db95602010-04-05 16:53:57 +0200559 if (!drv) {
560 return -ENOENT;
561 }
562
bellard83f64092006-08-01 16:21:11 +0000563 bs = bdrv_new("");
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200564 ret = bdrv_open_common(bs, filename, flags, drv);
bellard83f64092006-08-01 16:21:11 +0000565 if (ret < 0) {
566 bdrv_delete(bs);
567 return ret;
bellard3b0d4f62005-10-30 18:30:10 +0000568 }
aliguori71d07702009-03-03 17:37:16 +0000569 bs->growable = 1;
bellard83f64092006-08-01 16:21:11 +0000570 *pbs = bs;
571 return 0;
bellardea2384d2004-08-01 21:59:26 +0000572}
bellardfc01f7e2003-06-30 10:03:06 +0000573
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200574/*
575 * Opens a disk image (raw, qcow2, vmdk, ...)
576 */
Kevin Wolfd6e90982010-03-31 14:40:27 +0200577int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
578 BlockDriver *drv)
bellardea2384d2004-08-01 21:59:26 +0000579{
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200580 int ret;
bellard712e7872005-04-28 21:09:32 +0000581
bellard83f64092006-08-01 16:21:11 +0000582 if (flags & BDRV_O_SNAPSHOT) {
bellardea2384d2004-08-01 21:59:26 +0000583 BlockDriverState *bs1;
584 int64_t total_size;
aliguori7c96d462008-09-12 17:54:13 +0000585 int is_protocol = 0;
Kevin Wolf91a073a2009-05-27 14:48:06 +0200586 BlockDriver *bdrv_qcow2;
587 QEMUOptionParameter *options;
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200588 char tmp_filename[PATH_MAX];
589 char backing_filename[PATH_MAX];
ths3b46e622007-09-17 08:09:54 +0000590
bellardea2384d2004-08-01 21:59:26 +0000591 /* if snapshot, we create a temporary backing file and open it
592 instead of opening 'filename' directly */
593
594 /* if there is a backing file, use it */
595 bs1 = bdrv_new("");
Kevin Wolfd6e90982010-03-31 14:40:27 +0200596 ret = bdrv_open(bs1, filename, 0, drv);
aliguori51d7c002009-03-05 23:00:29 +0000597 if (ret < 0) {
bellardea2384d2004-08-01 21:59:26 +0000598 bdrv_delete(bs1);
aliguori51d7c002009-03-05 23:00:29 +0000599 return ret;
bellardea2384d2004-08-01 21:59:26 +0000600 }
Jes Sorensen3e829902010-05-27 16:20:30 +0200601 total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK;
aliguori7c96d462008-09-12 17:54:13 +0000602
603 if (bs1->drv && bs1->drv->protocol_name)
604 is_protocol = 1;
605
bellardea2384d2004-08-01 21:59:26 +0000606 bdrv_delete(bs1);
ths3b46e622007-09-17 08:09:54 +0000607
bellardea2384d2004-08-01 21:59:26 +0000608 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
aliguori7c96d462008-09-12 17:54:13 +0000609
610 /* Real path is meaningless for protocols */
611 if (is_protocol)
612 snprintf(backing_filename, sizeof(backing_filename),
613 "%s", filename);
Kirill A. Shutemov114cdfa2009-12-25 18:19:22 +0000614 else if (!realpath(filename, backing_filename))
615 return -errno;
aliguori7c96d462008-09-12 17:54:13 +0000616
Kevin Wolf91a073a2009-05-27 14:48:06 +0200617 bdrv_qcow2 = bdrv_find_format("qcow2");
618 options = parse_option_parameters("", bdrv_qcow2->create_options, NULL);
619
Jes Sorensen3e829902010-05-27 16:20:30 +0200620 set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size);
Kevin Wolf91a073a2009-05-27 14:48:06 +0200621 set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename);
622 if (drv) {
623 set_option_parameter(options, BLOCK_OPT_BACKING_FMT,
624 drv->format_name);
625 }
626
627 ret = bdrv_create(bdrv_qcow2, tmp_filename, options);
Jan Kiszkad7487682010-04-29 18:24:50 +0200628 free_option_parameters(options);
aliguori51d7c002009-03-05 23:00:29 +0000629 if (ret < 0) {
630 return ret;
bellardea2384d2004-08-01 21:59:26 +0000631 }
Kevin Wolf91a073a2009-05-27 14:48:06 +0200632
bellardea2384d2004-08-01 21:59:26 +0000633 filename = tmp_filename;
Kevin Wolf91a073a2009-05-27 14:48:06 +0200634 drv = bdrv_qcow2;
bellardea2384d2004-08-01 21:59:26 +0000635 bs->is_temporary = 1;
636 }
bellard712e7872005-04-28 21:09:32 +0000637
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200638 /* Find the right image format driver */
Christoph Hellwig6db95602010-04-05 16:53:57 +0200639 if (!drv) {
Stefan Weilc98ac352010-07-21 21:51:51 +0200640 ret = find_image_format(filename, &drv);
aliguori51d7c002009-03-05 23:00:29 +0000641 }
Christoph Hellwig69873072010-01-20 18:13:25 +0100642
aliguori51d7c002009-03-05 23:00:29 +0000643 if (!drv) {
aliguori51d7c002009-03-05 23:00:29 +0000644 goto unlink_and_fail;
bellardea2384d2004-08-01 21:59:26 +0000645 }
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200646
647 /* Open the image */
648 ret = bdrv_open_common(bs, filename, flags, drv);
649 if (ret < 0) {
Christoph Hellwig69873072010-01-20 18:13:25 +0100650 goto unlink_and_fail;
651 }
652
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200653 /* If there is a backing file, use it */
654 if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') {
655 char backing_filename[PATH_MAX];
656 int back_flags;
657 BlockDriver *back_drv = NULL;
658
659 bs->backing_hd = bdrv_new("");
Stefan Hajnoczidf2dbb42010-12-02 16:54:13 +0000660
661 if (path_has_protocol(bs->backing_file)) {
662 pstrcpy(backing_filename, sizeof(backing_filename),
663 bs->backing_file);
664 } else {
665 path_combine(backing_filename, sizeof(backing_filename),
666 filename, bs->backing_file);
667 }
668
669 if (bs->backing_format[0] != '\0') {
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200670 back_drv = bdrv_find_format(bs->backing_format);
Stefan Hajnoczidf2dbb42010-12-02 16:54:13 +0000671 }
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200672
673 /* backing files always opened read-only */
674 back_flags =
675 flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
676
677 ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv);
678 if (ret < 0) {
679 bdrv_close(bs);
680 return ret;
681 }
682 if (bs->is_temporary) {
683 bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR);
684 } else {
685 /* base image inherits from "parent" */
686 bs->backing_hd->keep_read_only = bs->keep_read_only;
687 }
688 }
689
690 if (!bdrv_key_required(bs)) {
691 /* call the change callback */
692 bs->media_changed = 1;
693 if (bs->change_cb)
Christoph Hellwigdb97ee62011-01-24 13:32:41 +0100694 bs->change_cb(bs->change_opaque, CHANGE_MEDIA);
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200695 }
696
697 return 0;
698
699unlink_and_fail:
700 if (bs->is_temporary) {
701 unlink(filename);
702 }
703 return ret;
704}
705
bellardfc01f7e2003-06-30 10:03:06 +0000706void bdrv_close(BlockDriverState *bs)
707{
bellard19cb3732006-08-19 11:45:59 +0000708 if (bs->drv) {
Markus Armbrusterf9092b12010-06-25 10:33:39 +0200709 if (bs == bs_snapshots) {
710 bs_snapshots = NULL;
711 }
Stefan Hajnoczi557df6a2010-04-17 10:49:06 +0100712 if (bs->backing_hd) {
bellardea2384d2004-08-01 21:59:26 +0000713 bdrv_delete(bs->backing_hd);
Stefan Hajnoczi557df6a2010-04-17 10:49:06 +0100714 bs->backing_hd = NULL;
715 }
bellardea2384d2004-08-01 21:59:26 +0000716 bs->drv->bdrv_close(bs);
Anthony Liguori7267c092011-08-20 22:09:37 -0500717 g_free(bs->opaque);
bellardea2384d2004-08-01 21:59:26 +0000718#ifdef _WIN32
719 if (bs->is_temporary) {
720 unlink(bs->filename);
721 }
bellard67b915a2004-03-31 23:37:16 +0000722#endif
bellardea2384d2004-08-01 21:59:26 +0000723 bs->opaque = NULL;
724 bs->drv = NULL;
bellardb3380822004-03-14 21:38:54 +0000725
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200726 if (bs->file != NULL) {
727 bdrv_close(bs->file);
728 }
729
bellardb3380822004-03-14 21:38:54 +0000730 /* call the change callback */
bellard19cb3732006-08-19 11:45:59 +0000731 bs->media_changed = 1;
bellardb3380822004-03-14 21:38:54 +0000732 if (bs->change_cb)
Christoph Hellwigdb97ee62011-01-24 13:32:41 +0100733 bs->change_cb(bs->change_opaque, CHANGE_MEDIA);
bellardb3380822004-03-14 21:38:54 +0000734 }
735}
736
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +0900737void bdrv_close_all(void)
738{
739 BlockDriverState *bs;
740
741 QTAILQ_FOREACH(bs, &bdrv_states, list) {
742 bdrv_close(bs);
743 }
744}
745
Ryan Harperd22b2f42011-03-29 20:51:47 -0500746/* make a BlockDriverState anonymous by removing from bdrv_state list.
747 Also, NULL terminate the device_name to prevent double remove */
748void bdrv_make_anon(BlockDriverState *bs)
749{
750 if (bs->device_name[0] != '\0') {
751 QTAILQ_REMOVE(&bdrv_states, bs, list);
752 }
753 bs->device_name[0] = '\0';
754}
755
bellardb3380822004-03-14 21:38:54 +0000756void bdrv_delete(BlockDriverState *bs)
757{
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200758 assert(!bs->dev);
Markus Armbruster18846de2010-06-29 16:58:30 +0200759
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +0100760 /* remove from list, if necessary */
Ryan Harperd22b2f42011-03-29 20:51:47 -0500761 bdrv_make_anon(bs);
aurel3234c6f052008-04-08 19:51:21 +0000762
bellardb3380822004-03-14 21:38:54 +0000763 bdrv_close(bs);
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200764 if (bs->file != NULL) {
765 bdrv_delete(bs->file);
766 }
767
Markus Armbrusterf9092b12010-06-25 10:33:39 +0200768 assert(bs != bs_snapshots);
Anthony Liguori7267c092011-08-20 22:09:37 -0500769 g_free(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000770}
771
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200772int bdrv_attach_dev(BlockDriverState *bs, void *dev)
773/* TODO change to DeviceState *dev when all users are qdevified */
Markus Armbruster18846de2010-06-29 16:58:30 +0200774{
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200775 if (bs->dev) {
Markus Armbruster18846de2010-06-29 16:58:30 +0200776 return -EBUSY;
777 }
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200778 bs->dev = dev;
Markus Armbruster18846de2010-06-29 16:58:30 +0200779 return 0;
780}
781
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200782/* TODO qdevified devices don't use this, remove when devices are qdevified */
783void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev)
Markus Armbruster18846de2010-06-29 16:58:30 +0200784{
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200785 if (bdrv_attach_dev(bs, dev) < 0) {
786 abort();
787 }
788}
789
790void bdrv_detach_dev(BlockDriverState *bs, void *dev)
791/* TODO change to DeviceState *dev when all users are qdevified */
792{
793 assert(bs->dev == dev);
794 bs->dev = NULL;
Markus Armbrustera19712b2011-07-20 18:23:36 +0200795 bs->change_cb = NULL;
796 bs->change_opaque = NULL;
Markus Armbruster18846de2010-06-29 16:58:30 +0200797}
798
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200799/* TODO change to return DeviceState * when all users are qdevified */
800void *bdrv_get_attached_dev(BlockDriverState *bs)
Markus Armbruster18846de2010-06-29 16:58:30 +0200801{
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200802 return bs->dev;
Markus Armbruster18846de2010-06-29 16:58:30 +0200803}
804
aliguorie97fc192009-04-21 23:11:50 +0000805/*
806 * Run consistency checks on an image
807 *
Kevin Wolfe076f332010-06-29 11:43:13 +0200808 * Returns 0 if the check could be completed (it doesn't mean that the image is
Stefan Weila1c72732011-04-28 17:20:38 +0200809 * free of errors) or -errno when an internal error occurred. The results of the
Kevin Wolfe076f332010-06-29 11:43:13 +0200810 * check are stored in res.
aliguorie97fc192009-04-21 23:11:50 +0000811 */
Kevin Wolfe076f332010-06-29 11:43:13 +0200812int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res)
aliguorie97fc192009-04-21 23:11:50 +0000813{
814 if (bs->drv->bdrv_check == NULL) {
815 return -ENOTSUP;
816 }
817
Kevin Wolfe076f332010-06-29 11:43:13 +0200818 memset(res, 0, sizeof(*res));
Kevin Wolf9ac228e2010-06-29 12:37:54 +0200819 return bs->drv->bdrv_check(bs, res);
aliguorie97fc192009-04-21 23:11:50 +0000820}
821
Kevin Wolf8a426612010-07-16 17:17:01 +0200822#define COMMIT_BUF_SECTORS 2048
823
bellard33e39632003-07-06 17:15:21 +0000824/* commit COW file into the raw image */
825int bdrv_commit(BlockDriverState *bs)
826{
bellard19cb3732006-08-19 11:45:59 +0000827 BlockDriver *drv = bs->drv;
Kevin Wolfee181192010-08-05 13:05:22 +0200828 BlockDriver *backing_drv;
Kevin Wolf8a426612010-07-16 17:17:01 +0200829 int64_t sector, total_sectors;
830 int n, ro, open_flags;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200831 int ret = 0, rw_ret = 0;
Kevin Wolf8a426612010-07-16 17:17:01 +0200832 uint8_t *buf;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200833 char filename[1024];
834 BlockDriverState *bs_rw, *bs_ro;
bellard33e39632003-07-06 17:15:21 +0000835
bellard19cb3732006-08-19 11:45:59 +0000836 if (!drv)
837 return -ENOMEDIUM;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200838
839 if (!bs->backing_hd) {
840 return -ENOTSUP;
bellard33e39632003-07-06 17:15:21 +0000841 }
842
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200843 if (bs->backing_hd->keep_read_only) {
844 return -EACCES;
845 }
Kevin Wolfee181192010-08-05 13:05:22 +0200846
847 backing_drv = bs->backing_hd->drv;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200848 ro = bs->backing_hd->read_only;
849 strncpy(filename, bs->backing_hd->filename, sizeof(filename));
850 open_flags = bs->backing_hd->open_flags;
851
852 if (ro) {
853 /* re-open as RW */
854 bdrv_delete(bs->backing_hd);
855 bs->backing_hd = NULL;
856 bs_rw = bdrv_new("");
Kevin Wolfee181192010-08-05 13:05:22 +0200857 rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR,
858 backing_drv);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200859 if (rw_ret < 0) {
860 bdrv_delete(bs_rw);
861 /* try to re-open read-only */
862 bs_ro = bdrv_new("");
Kevin Wolfee181192010-08-05 13:05:22 +0200863 ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR,
864 backing_drv);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200865 if (ret < 0) {
866 bdrv_delete(bs_ro);
867 /* drive not functional anymore */
868 bs->drv = NULL;
869 return ret;
870 }
871 bs->backing_hd = bs_ro;
872 return rw_ret;
873 }
874 bs->backing_hd = bs_rw;
bellard33e39632003-07-06 17:15:21 +0000875 }
bellardea2384d2004-08-01 21:59:26 +0000876
Jan Kiszka6ea44302009-11-30 18:21:19 +0100877 total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
Anthony Liguori7267c092011-08-20 22:09:37 -0500878 buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE);
bellardea2384d2004-08-01 21:59:26 +0000879
Kevin Wolf8a426612010-07-16 17:17:01 +0200880 for (sector = 0; sector < total_sectors; sector += n) {
881 if (drv->bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) {
882
883 if (bdrv_read(bs, sector, buf, n) != 0) {
884 ret = -EIO;
885 goto ro_cleanup;
886 }
887
888 if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) {
889 ret = -EIO;
890 goto ro_cleanup;
891 }
bellardea2384d2004-08-01 21:59:26 +0000892 }
893 }
bellard95389c82005-12-18 18:28:15 +0000894
Christoph Hellwig1d449522010-01-17 12:32:30 +0100895 if (drv->bdrv_make_empty) {
896 ret = drv->bdrv_make_empty(bs);
897 bdrv_flush(bs);
898 }
bellard95389c82005-12-18 18:28:15 +0000899
Christoph Hellwig3f5075a2010-01-12 13:49:23 +0100900 /*
901 * Make sure all data we wrote to the backing device is actually
902 * stable on disk.
903 */
904 if (bs->backing_hd)
905 bdrv_flush(bs->backing_hd);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200906
907ro_cleanup:
Anthony Liguori7267c092011-08-20 22:09:37 -0500908 g_free(buf);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200909
910 if (ro) {
911 /* re-open as RO */
912 bdrv_delete(bs->backing_hd);
913 bs->backing_hd = NULL;
914 bs_ro = bdrv_new("");
Kevin Wolfee181192010-08-05 13:05:22 +0200915 ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR,
916 backing_drv);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200917 if (ret < 0) {
918 bdrv_delete(bs_ro);
919 /* drive not functional anymore */
920 bs->drv = NULL;
921 return ret;
922 }
923 bs->backing_hd = bs_ro;
924 bs->backing_hd->keep_read_only = 0;
925 }
926
Christoph Hellwig1d449522010-01-17 12:32:30 +0100927 return ret;
bellard33e39632003-07-06 17:15:21 +0000928}
929
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200930void bdrv_commit_all(void)
931{
932 BlockDriverState *bs;
933
934 QTAILQ_FOREACH(bs, &bdrv_states, list) {
935 bdrv_commit(bs);
936 }
937}
938
Kevin Wolf756e6732010-01-12 12:55:17 +0100939/*
940 * Return values:
941 * 0 - success
942 * -EINVAL - backing format specified, but no file
943 * -ENOSPC - can't update the backing file because no space is left in the
944 * image file header
945 * -ENOTSUP - format driver doesn't support changing the backing file
946 */
947int bdrv_change_backing_file(BlockDriverState *bs,
948 const char *backing_file, const char *backing_fmt)
949{
950 BlockDriver *drv = bs->drv;
951
952 if (drv->bdrv_change_backing_file != NULL) {
953 return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
954 } else {
955 return -ENOTSUP;
956 }
957}
958
aliguori71d07702009-03-03 17:37:16 +0000959static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
960 size_t size)
961{
962 int64_t len;
963
964 if (!bdrv_is_inserted(bs))
965 return -ENOMEDIUM;
966
967 if (bs->growable)
968 return 0;
969
970 len = bdrv_getlength(bs);
971
Kevin Wolffbb7b4e2009-05-08 14:47:24 +0200972 if (offset < 0)
973 return -EIO;
974
975 if ((offset > len) || (len - offset < size))
aliguori71d07702009-03-03 17:37:16 +0000976 return -EIO;
977
978 return 0;
979}
980
981static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
982 int nb_sectors)
983{
Jes Sorenseneb5a3162010-05-27 16:20:31 +0200984 return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE,
985 nb_sectors * BDRV_SECTOR_SIZE);
aliguori71d07702009-03-03 17:37:16 +0000986}
987
Kevin Wolfe7a8a782011-07-15 16:05:00 +0200988static inline bool bdrv_has_async_rw(BlockDriver *drv)
989{
990 return drv->bdrv_co_readv != bdrv_co_readv_em
991 || drv->bdrv_aio_readv != bdrv_aio_readv_em;
992}
993
994static inline bool bdrv_has_async_flush(BlockDriver *drv)
995{
996 return drv->bdrv_aio_flush != bdrv_aio_flush_em;
997}
998
bellard19cb3732006-08-19 11:45:59 +0000999/* return < 0 if error. See bdrv_write() for the return codes */
ths5fafdf22007-09-16 21:08:06 +00001000int bdrv_read(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +00001001 uint8_t *buf, int nb_sectors)
1002{
bellardea2384d2004-08-01 21:59:26 +00001003 BlockDriver *drv = bs->drv;
1004
bellard19cb3732006-08-19 11:45:59 +00001005 if (!drv)
1006 return -ENOMEDIUM;
Kevin Wolfe7a8a782011-07-15 16:05:00 +02001007
1008 if (bdrv_has_async_rw(drv) && qemu_in_coroutine()) {
1009 QEMUIOVector qiov;
1010 struct iovec iov = {
1011 .iov_base = (void *)buf,
1012 .iov_len = nb_sectors * BDRV_SECTOR_SIZE,
1013 };
1014
1015 qemu_iovec_init_external(&qiov, &iov, 1);
1016 return bdrv_co_readv(bs, sector_num, nb_sectors, &qiov);
1017 }
1018
aliguori71d07702009-03-03 17:37:16 +00001019 if (bdrv_check_request(bs, sector_num, nb_sectors))
1020 return -EIO;
bellardb3380822004-03-14 21:38:54 +00001021
aliguorieda578e2009-03-12 19:57:16 +00001022 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
bellardfc01f7e2003-06-30 10:03:06 +00001023}
1024
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001025static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num,
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001026 int nb_sectors, int dirty)
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001027{
1028 int64_t start, end;
Jan Kiszkac6d22832009-11-30 18:21:20 +01001029 unsigned long val, idx, bit;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001030
Jan Kiszka6ea44302009-11-30 18:21:19 +01001031 start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkac6d22832009-11-30 18:21:20 +01001032 end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001033
1034 for (; start <= end; start++) {
Jan Kiszkac6d22832009-11-30 18:21:20 +01001035 idx = start / (sizeof(unsigned long) * 8);
1036 bit = start % (sizeof(unsigned long) * 8);
1037 val = bs->dirty_bitmap[idx];
1038 if (dirty) {
Marcelo Tosatti6d59fec2010-11-08 17:02:54 -02001039 if (!(val & (1UL << bit))) {
Liran Schouraaa0eb72010-01-26 10:31:48 +02001040 bs->dirty_count++;
Marcelo Tosatti6d59fec2010-11-08 17:02:54 -02001041 val |= 1UL << bit;
Liran Schouraaa0eb72010-01-26 10:31:48 +02001042 }
Jan Kiszkac6d22832009-11-30 18:21:20 +01001043 } else {
Marcelo Tosatti6d59fec2010-11-08 17:02:54 -02001044 if (val & (1UL << bit)) {
Liran Schouraaa0eb72010-01-26 10:31:48 +02001045 bs->dirty_count--;
Marcelo Tosatti6d59fec2010-11-08 17:02:54 -02001046 val &= ~(1UL << bit);
Liran Schouraaa0eb72010-01-26 10:31:48 +02001047 }
Jan Kiszkac6d22832009-11-30 18:21:20 +01001048 }
1049 bs->dirty_bitmap[idx] = val;
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001050 }
1051}
1052
ths5fafdf22007-09-16 21:08:06 +00001053/* Return < 0 if error. Important errors are:
bellard19cb3732006-08-19 11:45:59 +00001054 -EIO generic I/O error (may happen for all errors)
1055 -ENOMEDIUM No media inserted.
1056 -EINVAL Invalid sector number or nb_sectors
1057 -EACCES Trying to write a read-only device
1058*/
ths5fafdf22007-09-16 21:08:06 +00001059int bdrv_write(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +00001060 const uint8_t *buf, int nb_sectors)
1061{
bellard83f64092006-08-01 16:21:11 +00001062 BlockDriver *drv = bs->drv;
Kevin Wolfe7a8a782011-07-15 16:05:00 +02001063
bellard19cb3732006-08-19 11:45:59 +00001064 if (!bs->drv)
1065 return -ENOMEDIUM;
Kevin Wolfe7a8a782011-07-15 16:05:00 +02001066
1067 if (bdrv_has_async_rw(drv) && qemu_in_coroutine()) {
1068 QEMUIOVector qiov;
1069 struct iovec iov = {
1070 .iov_base = (void *)buf,
1071 .iov_len = nb_sectors * BDRV_SECTOR_SIZE,
1072 };
1073
1074 qemu_iovec_init_external(&qiov, &iov, 1);
1075 return bdrv_co_writev(bs, sector_num, nb_sectors, &qiov);
1076 }
1077
bellard0849bf02003-06-30 23:17:31 +00001078 if (bs->read_only)
bellard19cb3732006-08-19 11:45:59 +00001079 return -EACCES;
aliguori71d07702009-03-03 17:37:16 +00001080 if (bdrv_check_request(bs, sector_num, nb_sectors))
1081 return -EIO;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001082
Jan Kiszkac6d22832009-11-30 18:21:20 +01001083 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001084 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1085 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001086
Kevin Wolf294cc352010-04-28 14:34:01 +02001087 if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
1088 bs->wr_highest_sector = sector_num + nb_sectors - 1;
1089 }
1090
aliguori42fb2802009-01-15 20:43:39 +00001091 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
bellard83f64092006-08-01 16:21:11 +00001092}
1093
aliguorieda578e2009-03-12 19:57:16 +00001094int bdrv_pread(BlockDriverState *bs, int64_t offset,
1095 void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +00001096{
Jan Kiszka6ea44302009-11-30 18:21:19 +01001097 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
bellard83f64092006-08-01 16:21:11 +00001098 int len, nb_sectors, count;
1099 int64_t sector_num;
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001100 int ret;
bellard83f64092006-08-01 16:21:11 +00001101
1102 count = count1;
1103 /* first read to align to sector start */
Jan Kiszka6ea44302009-11-30 18:21:19 +01001104 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
bellard83f64092006-08-01 16:21:11 +00001105 if (len > count)
1106 len = count;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001107 sector_num = offset >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001108 if (len > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001109 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1110 return ret;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001111 memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
bellard83f64092006-08-01 16:21:11 +00001112 count -= len;
1113 if (count == 0)
1114 return count1;
1115 sector_num++;
1116 buf += len;
1117 }
1118
1119 /* read the sectors "in place" */
Jan Kiszka6ea44302009-11-30 18:21:19 +01001120 nb_sectors = count >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001121 if (nb_sectors > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001122 if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0)
1123 return ret;
bellard83f64092006-08-01 16:21:11 +00001124 sector_num += nb_sectors;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001125 len = nb_sectors << BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001126 buf += len;
1127 count -= len;
1128 }
1129
1130 /* add data from the last sector */
1131 if (count > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001132 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1133 return ret;
bellard83f64092006-08-01 16:21:11 +00001134 memcpy(buf, tmp_buf, count);
1135 }
1136 return count1;
1137}
1138
aliguorieda578e2009-03-12 19:57:16 +00001139int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
1140 const void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +00001141{
Jan Kiszka6ea44302009-11-30 18:21:19 +01001142 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
bellard83f64092006-08-01 16:21:11 +00001143 int len, nb_sectors, count;
1144 int64_t sector_num;
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001145 int ret;
bellard83f64092006-08-01 16:21:11 +00001146
1147 count = count1;
1148 /* first write to align to sector start */
Jan Kiszka6ea44302009-11-30 18:21:19 +01001149 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
bellard83f64092006-08-01 16:21:11 +00001150 if (len > count)
1151 len = count;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001152 sector_num = offset >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001153 if (len > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001154 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1155 return ret;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001156 memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len);
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001157 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
1158 return ret;
bellard83f64092006-08-01 16:21:11 +00001159 count -= len;
1160 if (count == 0)
1161 return count1;
1162 sector_num++;
1163 buf += len;
1164 }
1165
1166 /* write the sectors "in place" */
Jan Kiszka6ea44302009-11-30 18:21:19 +01001167 nb_sectors = count >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001168 if (nb_sectors > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001169 if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0)
1170 return ret;
bellard83f64092006-08-01 16:21:11 +00001171 sector_num += nb_sectors;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001172 len = nb_sectors << BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001173 buf += len;
1174 count -= len;
1175 }
1176
1177 /* add data from the last sector */
1178 if (count > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001179 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1180 return ret;
bellard83f64092006-08-01 16:21:11 +00001181 memcpy(tmp_buf, buf, count);
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001182 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
1183 return ret;
bellard83f64092006-08-01 16:21:11 +00001184 }
1185 return count1;
1186}
bellard83f64092006-08-01 16:21:11 +00001187
Kevin Wolff08145f2010-06-16 16:38:15 +02001188/*
1189 * Writes to the file and ensures that no writes are reordered across this
1190 * request (acts as a barrier)
1191 *
1192 * Returns 0 on success, -errno in error cases.
1193 */
1194int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset,
1195 const void *buf, int count)
1196{
1197 int ret;
1198
1199 ret = bdrv_pwrite(bs, offset, buf, count);
1200 if (ret < 0) {
1201 return ret;
1202 }
1203
Stefan Hajnoczi92196b22011-08-04 12:26:52 +01001204 /* No flush needed for cache modes that use O_DSYNC */
1205 if ((bs->open_flags & BDRV_O_CACHE_WB) != 0) {
Kevin Wolff08145f2010-06-16 16:38:15 +02001206 bdrv_flush(bs);
1207 }
1208
1209 return 0;
1210}
1211
Kevin Wolfda1fa912011-07-14 17:27:13 +02001212int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num,
1213 int nb_sectors, QEMUIOVector *qiov)
1214{
1215 BlockDriver *drv = bs->drv;
1216
1217 trace_bdrv_co_readv(bs, sector_num, nb_sectors);
1218
1219 if (!drv) {
1220 return -ENOMEDIUM;
1221 }
1222 if (bdrv_check_request(bs, sector_num, nb_sectors)) {
1223 return -EIO;
1224 }
1225
1226 return drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov);
1227}
1228
1229int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num,
1230 int nb_sectors, QEMUIOVector *qiov)
1231{
1232 BlockDriver *drv = bs->drv;
1233
1234 trace_bdrv_co_writev(bs, sector_num, nb_sectors);
1235
1236 if (!bs->drv) {
1237 return -ENOMEDIUM;
1238 }
1239 if (bs->read_only) {
1240 return -EACCES;
1241 }
1242 if (bdrv_check_request(bs, sector_num, nb_sectors)) {
1243 return -EIO;
1244 }
1245
1246 if (bs->dirty_bitmap) {
1247 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1248 }
1249
1250 if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
1251 bs->wr_highest_sector = sector_num + nb_sectors - 1;
1252 }
1253
1254 return drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov);
1255}
1256
bellard83f64092006-08-01 16:21:11 +00001257/**
bellard83f64092006-08-01 16:21:11 +00001258 * Truncate file to 'offset' bytes (needed only for file protocols)
1259 */
1260int bdrv_truncate(BlockDriverState *bs, int64_t offset)
1261{
1262 BlockDriver *drv = bs->drv;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001263 int ret;
bellard83f64092006-08-01 16:21:11 +00001264 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001265 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +00001266 if (!drv->bdrv_truncate)
1267 return -ENOTSUP;
Naphtali Sprei59f26892009-10-26 16:25:16 +02001268 if (bs->read_only)
1269 return -EACCES;
Marcelo Tosatti85916752011-01-26 12:12:35 -02001270 if (bdrv_in_use(bs))
1271 return -EBUSY;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001272 ret = drv->bdrv_truncate(bs, offset);
1273 if (ret == 0) {
1274 ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
Christoph Hellwigdb97ee62011-01-24 13:32:41 +01001275 if (bs->change_cb) {
1276 bs->change_cb(bs->change_opaque, CHANGE_SIZE);
1277 }
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001278 }
1279 return ret;
bellard83f64092006-08-01 16:21:11 +00001280}
1281
1282/**
Fam Zheng4a1d5e12011-07-12 19:56:39 +08001283 * Length of a allocated file in bytes. Sparse files are counted by actual
1284 * allocated space. Return < 0 if error or unknown.
1285 */
1286int64_t bdrv_get_allocated_file_size(BlockDriverState *bs)
1287{
1288 BlockDriver *drv = bs->drv;
1289 if (!drv) {
1290 return -ENOMEDIUM;
1291 }
1292 if (drv->bdrv_get_allocated_file_size) {
1293 return drv->bdrv_get_allocated_file_size(bs);
1294 }
1295 if (bs->file) {
1296 return bdrv_get_allocated_file_size(bs->file);
1297 }
1298 return -ENOTSUP;
1299}
1300
1301/**
bellard83f64092006-08-01 16:21:11 +00001302 * Length of a file in bytes. Return < 0 if error or unknown.
1303 */
1304int64_t bdrv_getlength(BlockDriverState *bs)
1305{
1306 BlockDriver *drv = bs->drv;
1307 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001308 return -ENOMEDIUM;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001309
Stefan Hajnoczi46a4e4e2011-03-29 20:04:41 +01001310 if (bs->growable || bs->removable) {
1311 if (drv->bdrv_getlength) {
1312 return drv->bdrv_getlength(bs);
1313 }
bellard83f64092006-08-01 16:21:11 +00001314 }
Stefan Hajnoczi46a4e4e2011-03-29 20:04:41 +01001315 return bs->total_sectors * BDRV_SECTOR_SIZE;
bellardfc01f7e2003-06-30 10:03:06 +00001316}
1317
bellard19cb3732006-08-19 11:45:59 +00001318/* return 0 as number of sectors if no device present or error */
ths96b8f132007-12-17 01:35:20 +00001319void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
bellardfc01f7e2003-06-30 10:03:06 +00001320{
bellard19cb3732006-08-19 11:45:59 +00001321 int64_t length;
1322 length = bdrv_getlength(bs);
1323 if (length < 0)
1324 length = 0;
1325 else
Jan Kiszka6ea44302009-11-30 18:21:19 +01001326 length = length >> BDRV_SECTOR_BITS;
bellard19cb3732006-08-19 11:45:59 +00001327 *nb_sectors_ptr = length;
bellardfc01f7e2003-06-30 10:03:06 +00001328}
bellardcf989512004-02-16 21:56:36 +00001329
aliguorif3d54fc2008-11-25 21:50:24 +00001330struct partition {
1331 uint8_t boot_ind; /* 0x80 - active */
1332 uint8_t head; /* starting head */
1333 uint8_t sector; /* starting sector */
1334 uint8_t cyl; /* starting cylinder */
1335 uint8_t sys_ind; /* What partition type */
1336 uint8_t end_head; /* end head */
1337 uint8_t end_sector; /* end sector */
1338 uint8_t end_cyl; /* end cylinder */
1339 uint32_t start_sect; /* starting sector counting from 0 */
1340 uint32_t nr_sects; /* nr of sectors in partition */
Stefan Weil541dc0d2011-08-31 12:38:01 +02001341} QEMU_PACKED;
aliguorif3d54fc2008-11-25 21:50:24 +00001342
1343/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
1344static int guess_disk_lchs(BlockDriverState *bs,
1345 int *pcylinders, int *pheads, int *psectors)
1346{
Jes Sorenseneb5a3162010-05-27 16:20:31 +02001347 uint8_t buf[BDRV_SECTOR_SIZE];
aliguorif3d54fc2008-11-25 21:50:24 +00001348 int ret, i, heads, sectors, cylinders;
1349 struct partition *p;
1350 uint32_t nr_sects;
blueswir1a38131b2008-12-05 17:56:40 +00001351 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +00001352
1353 bdrv_get_geometry(bs, &nb_sectors);
1354
1355 ret = bdrv_read(bs, 0, buf, 1);
1356 if (ret < 0)
1357 return -1;
1358 /* test msdos magic */
1359 if (buf[510] != 0x55 || buf[511] != 0xaa)
1360 return -1;
1361 for(i = 0; i < 4; i++) {
1362 p = ((struct partition *)(buf + 0x1be)) + i;
1363 nr_sects = le32_to_cpu(p->nr_sects);
1364 if (nr_sects && p->end_head) {
1365 /* We make the assumption that the partition terminates on
1366 a cylinder boundary */
1367 heads = p->end_head + 1;
1368 sectors = p->end_sector & 63;
1369 if (sectors == 0)
1370 continue;
1371 cylinders = nb_sectors / (heads * sectors);
1372 if (cylinders < 1 || cylinders > 16383)
1373 continue;
1374 *pheads = heads;
1375 *psectors = sectors;
1376 *pcylinders = cylinders;
1377#if 0
1378 printf("guessed geometry: LCHS=%d %d %d\n",
1379 cylinders, heads, sectors);
1380#endif
1381 return 0;
1382 }
1383 }
1384 return -1;
1385}
1386
1387void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
1388{
1389 int translation, lba_detected = 0;
1390 int cylinders, heads, secs;
blueswir1a38131b2008-12-05 17:56:40 +00001391 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +00001392
1393 /* if a geometry hint is available, use it */
1394 bdrv_get_geometry(bs, &nb_sectors);
1395 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
1396 translation = bdrv_get_translation_hint(bs);
1397 if (cylinders != 0) {
1398 *pcyls = cylinders;
1399 *pheads = heads;
1400 *psecs = secs;
1401 } else {
1402 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
1403 if (heads > 16) {
1404 /* if heads > 16, it means that a BIOS LBA
1405 translation was active, so the default
1406 hardware geometry is OK */
1407 lba_detected = 1;
1408 goto default_geometry;
1409 } else {
1410 *pcyls = cylinders;
1411 *pheads = heads;
1412 *psecs = secs;
1413 /* disable any translation to be in sync with
1414 the logical geometry */
1415 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
1416 bdrv_set_translation_hint(bs,
1417 BIOS_ATA_TRANSLATION_NONE);
1418 }
1419 }
1420 } else {
1421 default_geometry:
1422 /* if no geometry, use a standard physical disk geometry */
1423 cylinders = nb_sectors / (16 * 63);
1424
1425 if (cylinders > 16383)
1426 cylinders = 16383;
1427 else if (cylinders < 2)
1428 cylinders = 2;
1429 *pcyls = cylinders;
1430 *pheads = 16;
1431 *psecs = 63;
1432 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
1433 if ((*pcyls * *pheads) <= 131072) {
1434 bdrv_set_translation_hint(bs,
1435 BIOS_ATA_TRANSLATION_LARGE);
1436 } else {
1437 bdrv_set_translation_hint(bs,
1438 BIOS_ATA_TRANSLATION_LBA);
1439 }
1440 }
1441 }
1442 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
1443 }
1444}
1445
ths5fafdf22007-09-16 21:08:06 +00001446void bdrv_set_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +00001447 int cyls, int heads, int secs)
1448{
1449 bs->cyls = cyls;
1450 bs->heads = heads;
1451 bs->secs = secs;
1452}
1453
bellard46d47672004-11-16 01:45:27 +00001454void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
1455{
1456 bs->translation = translation;
1457}
1458
ths5fafdf22007-09-16 21:08:06 +00001459void bdrv_get_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +00001460 int *pcyls, int *pheads, int *psecs)
1461{
1462 *pcyls = bs->cyls;
1463 *pheads = bs->heads;
1464 *psecs = bs->secs;
1465}
1466
Blue Swirl5bbdbb42011-02-12 20:43:32 +00001467/* Recognize floppy formats */
1468typedef struct FDFormat {
1469 FDriveType drive;
1470 uint8_t last_sect;
1471 uint8_t max_track;
1472 uint8_t max_head;
1473} FDFormat;
1474
1475static const FDFormat fd_formats[] = {
1476 /* First entry is default format */
1477 /* 1.44 MB 3"1/2 floppy disks */
1478 { FDRIVE_DRV_144, 18, 80, 1, },
1479 { FDRIVE_DRV_144, 20, 80, 1, },
1480 { FDRIVE_DRV_144, 21, 80, 1, },
1481 { FDRIVE_DRV_144, 21, 82, 1, },
1482 { FDRIVE_DRV_144, 21, 83, 1, },
1483 { FDRIVE_DRV_144, 22, 80, 1, },
1484 { FDRIVE_DRV_144, 23, 80, 1, },
1485 { FDRIVE_DRV_144, 24, 80, 1, },
1486 /* 2.88 MB 3"1/2 floppy disks */
1487 { FDRIVE_DRV_288, 36, 80, 1, },
1488 { FDRIVE_DRV_288, 39, 80, 1, },
1489 { FDRIVE_DRV_288, 40, 80, 1, },
1490 { FDRIVE_DRV_288, 44, 80, 1, },
1491 { FDRIVE_DRV_288, 48, 80, 1, },
1492 /* 720 kB 3"1/2 floppy disks */
1493 { FDRIVE_DRV_144, 9, 80, 1, },
1494 { FDRIVE_DRV_144, 10, 80, 1, },
1495 { FDRIVE_DRV_144, 10, 82, 1, },
1496 { FDRIVE_DRV_144, 10, 83, 1, },
1497 { FDRIVE_DRV_144, 13, 80, 1, },
1498 { FDRIVE_DRV_144, 14, 80, 1, },
1499 /* 1.2 MB 5"1/4 floppy disks */
1500 { FDRIVE_DRV_120, 15, 80, 1, },
1501 { FDRIVE_DRV_120, 18, 80, 1, },
1502 { FDRIVE_DRV_120, 18, 82, 1, },
1503 { FDRIVE_DRV_120, 18, 83, 1, },
1504 { FDRIVE_DRV_120, 20, 80, 1, },
1505 /* 720 kB 5"1/4 floppy disks */
1506 { FDRIVE_DRV_120, 9, 80, 1, },
1507 { FDRIVE_DRV_120, 11, 80, 1, },
1508 /* 360 kB 5"1/4 floppy disks */
1509 { FDRIVE_DRV_120, 9, 40, 1, },
1510 { FDRIVE_DRV_120, 9, 40, 0, },
1511 { FDRIVE_DRV_120, 10, 41, 1, },
1512 { FDRIVE_DRV_120, 10, 42, 1, },
1513 /* 320 kB 5"1/4 floppy disks */
1514 { FDRIVE_DRV_120, 8, 40, 1, },
1515 { FDRIVE_DRV_120, 8, 40, 0, },
1516 /* 360 kB must match 5"1/4 better than 3"1/2... */
1517 { FDRIVE_DRV_144, 9, 80, 0, },
1518 /* end */
1519 { FDRIVE_DRV_NONE, -1, -1, 0, },
1520};
1521
1522void bdrv_get_floppy_geometry_hint(BlockDriverState *bs, int *nb_heads,
1523 int *max_track, int *last_sect,
1524 FDriveType drive_in, FDriveType *drive)
1525{
1526 const FDFormat *parse;
1527 uint64_t nb_sectors, size;
1528 int i, first_match, match;
1529
1530 bdrv_get_geometry_hint(bs, nb_heads, max_track, last_sect);
1531 if (*nb_heads != 0 && *max_track != 0 && *last_sect != 0) {
1532 /* User defined disk */
1533 } else {
1534 bdrv_get_geometry(bs, &nb_sectors);
1535 match = -1;
1536 first_match = -1;
1537 for (i = 0; ; i++) {
1538 parse = &fd_formats[i];
1539 if (parse->drive == FDRIVE_DRV_NONE) {
1540 break;
1541 }
1542 if (drive_in == parse->drive ||
1543 drive_in == FDRIVE_DRV_NONE) {
1544 size = (parse->max_head + 1) * parse->max_track *
1545 parse->last_sect;
1546 if (nb_sectors == size) {
1547 match = i;
1548 break;
1549 }
1550 if (first_match == -1) {
1551 first_match = i;
1552 }
1553 }
1554 }
1555 if (match == -1) {
1556 if (first_match == -1) {
1557 match = 1;
1558 } else {
1559 match = first_match;
1560 }
1561 parse = &fd_formats[match];
1562 }
1563 *nb_heads = parse->max_head + 1;
1564 *max_track = parse->max_track;
1565 *last_sect = parse->last_sect;
1566 *drive = parse->drive;
1567 }
1568}
1569
bellard46d47672004-11-16 01:45:27 +00001570int bdrv_get_translation_hint(BlockDriverState *bs)
1571{
1572 return bs->translation;
1573}
1574
Markus Armbrusterabd7f682010-06-02 18:55:17 +02001575void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error,
1576 BlockErrorAction on_write_error)
1577{
1578 bs->on_read_error = on_read_error;
1579 bs->on_write_error = on_write_error;
1580}
1581
1582BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read)
1583{
1584 return is_read ? bs->on_read_error : bs->on_write_error;
1585}
1586
Markus Armbruster7d0d6952010-06-25 13:42:14 +02001587void bdrv_set_removable(BlockDriverState *bs, int removable)
1588{
1589 bs->removable = removable;
1590 if (removable && bs == bs_snapshots) {
1591 bs_snapshots = NULL;
1592 }
1593}
1594
bellardb3380822004-03-14 21:38:54 +00001595int bdrv_is_removable(BlockDriverState *bs)
1596{
1597 return bs->removable;
1598}
1599
1600int bdrv_is_read_only(BlockDriverState *bs)
1601{
1602 return bs->read_only;
1603}
1604
ths985a03b2007-12-24 16:10:43 +00001605int bdrv_is_sg(BlockDriverState *bs)
1606{
1607 return bs->sg;
1608}
1609
Christoph Hellwige900a7b2009-09-04 19:01:15 +02001610int bdrv_enable_write_cache(BlockDriverState *bs)
1611{
1612 return bs->enable_write_cache;
1613}
1614
bellard19cb3732006-08-19 11:45:59 +00001615/* XXX: no longer used */
ths5fafdf22007-09-16 21:08:06 +00001616void bdrv_set_change_cb(BlockDriverState *bs,
Christoph Hellwigdb97ee62011-01-24 13:32:41 +01001617 void (*change_cb)(void *opaque, int reason),
1618 void *opaque)
bellardb3380822004-03-14 21:38:54 +00001619{
1620 bs->change_cb = change_cb;
1621 bs->change_opaque = opaque;
1622}
1623
bellardea2384d2004-08-01 21:59:26 +00001624int bdrv_is_encrypted(BlockDriverState *bs)
1625{
1626 if (bs->backing_hd && bs->backing_hd->encrypted)
1627 return 1;
1628 return bs->encrypted;
1629}
1630
aliguoric0f4ce72009-03-05 23:01:01 +00001631int bdrv_key_required(BlockDriverState *bs)
1632{
1633 BlockDriverState *backing_hd = bs->backing_hd;
1634
1635 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
1636 return 1;
1637 return (bs->encrypted && !bs->valid_key);
1638}
1639
bellardea2384d2004-08-01 21:59:26 +00001640int bdrv_set_key(BlockDriverState *bs, const char *key)
1641{
1642 int ret;
1643 if (bs->backing_hd && bs->backing_hd->encrypted) {
1644 ret = bdrv_set_key(bs->backing_hd, key);
1645 if (ret < 0)
1646 return ret;
1647 if (!bs->encrypted)
1648 return 0;
1649 }
Shahar Havivifd04a2a2010-03-06 00:26:13 +02001650 if (!bs->encrypted) {
1651 return -EINVAL;
1652 } else if (!bs->drv || !bs->drv->bdrv_set_key) {
1653 return -ENOMEDIUM;
1654 }
aliguoric0f4ce72009-03-05 23:01:01 +00001655 ret = bs->drv->bdrv_set_key(bs, key);
aliguoribb5fc202009-03-05 23:01:15 +00001656 if (ret < 0) {
1657 bs->valid_key = 0;
1658 } else if (!bs->valid_key) {
1659 bs->valid_key = 1;
1660 /* call the change callback now, we skipped it on open */
1661 bs->media_changed = 1;
1662 if (bs->change_cb)
Christoph Hellwigdb97ee62011-01-24 13:32:41 +01001663 bs->change_cb(bs->change_opaque, CHANGE_MEDIA);
aliguoribb5fc202009-03-05 23:01:15 +00001664 }
aliguoric0f4ce72009-03-05 23:01:01 +00001665 return ret;
bellardea2384d2004-08-01 21:59:26 +00001666}
1667
1668void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
1669{
bellard19cb3732006-08-19 11:45:59 +00001670 if (!bs->drv) {
bellardea2384d2004-08-01 21:59:26 +00001671 buf[0] = '\0';
1672 } else {
1673 pstrcpy(buf, buf_size, bs->drv->format_name);
1674 }
1675}
1676
ths5fafdf22007-09-16 21:08:06 +00001677void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
bellardea2384d2004-08-01 21:59:26 +00001678 void *opaque)
1679{
1680 BlockDriver *drv;
1681
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +01001682 QLIST_FOREACH(drv, &bdrv_drivers, list) {
bellardea2384d2004-08-01 21:59:26 +00001683 it(opaque, drv->format_name);
1684 }
1685}
1686
bellardb3380822004-03-14 21:38:54 +00001687BlockDriverState *bdrv_find(const char *name)
1688{
1689 BlockDriverState *bs;
1690
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001691 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1692 if (!strcmp(name, bs->device_name)) {
bellardb3380822004-03-14 21:38:54 +00001693 return bs;
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001694 }
bellardb3380822004-03-14 21:38:54 +00001695 }
1696 return NULL;
1697}
1698
Markus Armbruster2f399b02010-06-02 18:55:20 +02001699BlockDriverState *bdrv_next(BlockDriverState *bs)
1700{
1701 if (!bs) {
1702 return QTAILQ_FIRST(&bdrv_states);
1703 }
1704 return QTAILQ_NEXT(bs, list);
1705}
1706
aliguori51de9762009-03-05 23:00:43 +00001707void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
bellard81d09122004-07-14 17:21:37 +00001708{
1709 BlockDriverState *bs;
1710
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001711 QTAILQ_FOREACH(bs, &bdrv_states, list) {
aliguori51de9762009-03-05 23:00:43 +00001712 it(opaque, bs);
bellard81d09122004-07-14 17:21:37 +00001713 }
1714}
1715
bellardea2384d2004-08-01 21:59:26 +00001716const char *bdrv_get_device_name(BlockDriverState *bs)
1717{
1718 return bs->device_name;
1719}
1720
Kevin Wolf205ef792010-10-21 16:43:43 +02001721int bdrv_flush(BlockDriverState *bs)
pbrook7a6cba62006-06-04 11:39:07 +00001722{
Alexander Graf016f5cf2010-05-26 17:51:49 +02001723 if (bs->open_flags & BDRV_O_NO_FLUSH) {
Kevin Wolf205ef792010-10-21 16:43:43 +02001724 return 0;
Alexander Graf016f5cf2010-05-26 17:51:49 +02001725 }
1726
Kevin Wolfe7a8a782011-07-15 16:05:00 +02001727 if (bs->drv && bdrv_has_async_flush(bs->drv) && qemu_in_coroutine()) {
1728 return bdrv_co_flush_em(bs);
1729 }
1730
Kevin Wolf205ef792010-10-21 16:43:43 +02001731 if (bs->drv && bs->drv->bdrv_flush) {
1732 return bs->drv->bdrv_flush(bs);
1733 }
1734
1735 /*
1736 * Some block drivers always operate in either writethrough or unsafe mode
1737 * and don't support bdrv_flush therefore. Usually qemu doesn't know how
1738 * the server works (because the behaviour is hardcoded or depends on
1739 * server-side configuration), so we can't ensure that everything is safe
1740 * on disk. Returning an error doesn't work because that would break guests
1741 * even if the server operates in writethrough mode.
1742 *
1743 * Let's hope the user knows what he's doing.
1744 */
1745 return 0;
pbrook7a6cba62006-06-04 11:39:07 +00001746}
1747
aliguoric6ca28d2008-10-06 13:55:43 +00001748void bdrv_flush_all(void)
1749{
1750 BlockDriverState *bs;
1751
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001752 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1753 if (bs->drv && !bdrv_is_read_only(bs) &&
1754 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) {
aliguoric6ca28d2008-10-06 13:55:43 +00001755 bdrv_flush(bs);
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001756 }
1757 }
aliguoric6ca28d2008-10-06 13:55:43 +00001758}
1759
Kevin Wolff2feebb2010-04-14 17:30:35 +02001760int bdrv_has_zero_init(BlockDriverState *bs)
1761{
1762 assert(bs->drv);
1763
Kevin Wolf336c1c12010-07-28 11:26:29 +02001764 if (bs->drv->bdrv_has_zero_init) {
1765 return bs->drv->bdrv_has_zero_init(bs);
Kevin Wolff2feebb2010-04-14 17:30:35 +02001766 }
1767
1768 return 1;
1769}
1770
Christoph Hellwigbb8bf762010-12-16 19:36:31 +01001771int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors)
1772{
1773 if (!bs->drv) {
1774 return -ENOMEDIUM;
1775 }
1776 if (!bs->drv->bdrv_discard) {
1777 return 0;
1778 }
1779 return bs->drv->bdrv_discard(bs, sector_num, nb_sectors);
1780}
1781
thsf58c7b32008-06-05 21:53:49 +00001782/*
1783 * Returns true iff the specified sector is present in the disk image. Drivers
1784 * not implementing the functionality are assumed to not support backing files,
1785 * hence all their sectors are reported as allocated.
1786 *
1787 * 'pnum' is set to the number of sectors (including and immediately following
1788 * the specified sector) that are known to be in the same
1789 * allocated/unallocated state.
1790 *
1791 * 'nb_sectors' is the max value 'pnum' should be set to.
1792 */
1793int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1794 int *pnum)
1795{
1796 int64_t n;
1797 if (!bs->drv->bdrv_is_allocated) {
1798 if (sector_num >= bs->total_sectors) {
1799 *pnum = 0;
1800 return 0;
1801 }
1802 n = bs->total_sectors - sector_num;
1803 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1804 return 1;
1805 }
1806 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1807}
1808
Luiz Capitulino2582bfe2010-02-03 12:41:01 -02001809void bdrv_mon_event(const BlockDriverState *bdrv,
1810 BlockMonEventAction action, int is_read)
1811{
1812 QObject *data;
1813 const char *action_str;
1814
1815 switch (action) {
1816 case BDRV_ACTION_REPORT:
1817 action_str = "report";
1818 break;
1819 case BDRV_ACTION_IGNORE:
1820 action_str = "ignore";
1821 break;
1822 case BDRV_ACTION_STOP:
1823 action_str = "stop";
1824 break;
1825 default:
1826 abort();
1827 }
1828
1829 data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
1830 bdrv->device_name,
1831 action_str,
1832 is_read ? "read" : "write");
1833 monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data);
1834
1835 qobject_decref(data);
1836}
1837
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001838static void bdrv_print_dict(QObject *obj, void *opaque)
bellardb3380822004-03-14 21:38:54 +00001839{
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001840 QDict *bs_dict;
1841 Monitor *mon = opaque;
1842
1843 bs_dict = qobject_to_qdict(obj);
1844
Markus Armbrusterd8aeeb32011-05-16 15:04:55 +02001845 monitor_printf(mon, "%s: removable=%d",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001846 qdict_get_str(bs_dict, "device"),
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001847 qdict_get_bool(bs_dict, "removable"));
1848
1849 if (qdict_get_bool(bs_dict, "removable")) {
1850 monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked"));
1851 }
1852
1853 if (qdict_haskey(bs_dict, "inserted")) {
1854 QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted"));
1855
1856 monitor_printf(mon, " file=");
1857 monitor_print_filename(mon, qdict_get_str(qdict, "file"));
1858 if (qdict_haskey(qdict, "backing_file")) {
1859 monitor_printf(mon, " backing_file=");
1860 monitor_print_filename(mon, qdict_get_str(qdict, "backing_file"));
1861 }
1862 monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
1863 qdict_get_bool(qdict, "ro"),
1864 qdict_get_str(qdict, "drv"),
1865 qdict_get_bool(qdict, "encrypted"));
1866 } else {
1867 monitor_printf(mon, " [not inserted]");
1868 }
1869
1870 monitor_printf(mon, "\n");
1871}
1872
1873void bdrv_info_print(Monitor *mon, const QObject *data)
1874{
1875 qlist_iter(qobject_to_qlist(data), bdrv_print_dict, mon);
1876}
1877
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001878void bdrv_info(Monitor *mon, QObject **ret_data)
1879{
1880 QList *bs_list;
bellardb3380822004-03-14 21:38:54 +00001881 BlockDriverState *bs;
1882
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001883 bs_list = qlist_new();
1884
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001885 QTAILQ_FOREACH(bs, &bdrv_states, list) {
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001886 QObject *bs_obj;
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001887
Markus Armbrusterd8aeeb32011-05-16 15:04:55 +02001888 bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': 'unknown', "
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001889 "'removable': %i, 'locked': %i }",
Markus Armbrusterd8aeeb32011-05-16 15:04:55 +02001890 bs->device_name, bs->removable,
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001891 bs->locked);
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001892
bellard19cb3732006-08-19 11:45:59 +00001893 if (bs->drv) {
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001894 QObject *obj;
1895 QDict *bs_dict = qobject_to_qdict(bs_obj);
1896
1897 obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, "
1898 "'encrypted': %i }",
1899 bs->filename, bs->read_only,
1900 bs->drv->format_name,
1901 bdrv_is_encrypted(bs));
thsfef30742006-12-22 14:11:32 +00001902 if (bs->backing_file[0] != '\0') {
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001903 QDict *qdict = qobject_to_qdict(obj);
1904 qdict_put(qdict, "backing_file",
1905 qstring_from_str(bs->backing_file));
aliguori376253e2009-03-05 23:01:23 +00001906 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001907
1908 qdict_put_obj(bs_dict, "inserted", obj);
bellardb3380822004-03-14 21:38:54 +00001909 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001910 qlist_append_obj(bs_list, bs_obj);
bellardb3380822004-03-14 21:38:54 +00001911 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001912
1913 *ret_data = QOBJECT(bs_list);
bellardb3380822004-03-14 21:38:54 +00001914}
thsa36e69d2007-12-02 05:18:19 +00001915
Luiz Capitulino218a5362009-12-10 17:16:07 -02001916static void bdrv_stats_iter(QObject *data, void *opaque)
thsa36e69d2007-12-02 05:18:19 +00001917{
Luiz Capitulino218a5362009-12-10 17:16:07 -02001918 QDict *qdict;
1919 Monitor *mon = opaque;
1920
1921 qdict = qobject_to_qdict(data);
1922 monitor_printf(mon, "%s:", qdict_get_str(qdict, "device"));
1923
1924 qdict = qobject_to_qdict(qdict_get(qdict, "stats"));
1925 monitor_printf(mon, " rd_bytes=%" PRId64
1926 " wr_bytes=%" PRId64
1927 " rd_operations=%" PRId64
1928 " wr_operations=%" PRId64
Christoph Hellwige8045d62011-08-22 00:25:58 +02001929 " flush_operations=%" PRId64
Christoph Hellwigc488c7f2011-08-25 08:26:10 +02001930 " wr_total_time_ns=%" PRId64
1931 " rd_total_time_ns=%" PRId64
1932 " flush_total_time_ns=%" PRId64
Luiz Capitulino218a5362009-12-10 17:16:07 -02001933 "\n",
1934 qdict_get_int(qdict, "rd_bytes"),
1935 qdict_get_int(qdict, "wr_bytes"),
1936 qdict_get_int(qdict, "rd_operations"),
Christoph Hellwige8045d62011-08-22 00:25:58 +02001937 qdict_get_int(qdict, "wr_operations"),
Christoph Hellwigc488c7f2011-08-25 08:26:10 +02001938 qdict_get_int(qdict, "flush_operations"),
1939 qdict_get_int(qdict, "wr_total_time_ns"),
1940 qdict_get_int(qdict, "rd_total_time_ns"),
1941 qdict_get_int(qdict, "flush_total_time_ns"));
Luiz Capitulino218a5362009-12-10 17:16:07 -02001942}
1943
1944void bdrv_stats_print(Monitor *mon, const QObject *data)
1945{
1946 qlist_iter(qobject_to_qlist(data), bdrv_stats_iter, mon);
1947}
1948
Kevin Wolf294cc352010-04-28 14:34:01 +02001949static QObject* bdrv_info_stats_bs(BlockDriverState *bs)
1950{
1951 QObject *res;
1952 QDict *dict;
1953
1954 res = qobject_from_jsonf("{ 'stats': {"
1955 "'rd_bytes': %" PRId64 ","
1956 "'wr_bytes': %" PRId64 ","
1957 "'rd_operations': %" PRId64 ","
1958 "'wr_operations': %" PRId64 ","
Christoph Hellwige8045d62011-08-22 00:25:58 +02001959 "'wr_highest_offset': %" PRId64 ","
Christoph Hellwigc488c7f2011-08-25 08:26:10 +02001960 "'flush_operations': %" PRId64 ","
1961 "'wr_total_time_ns': %" PRId64 ","
1962 "'rd_total_time_ns': %" PRId64 ","
1963 "'flush_total_time_ns': %" PRId64
Kevin Wolf294cc352010-04-28 14:34:01 +02001964 "} }",
Christoph Hellwiga597e792011-08-25 08:26:01 +02001965 bs->nr_bytes[BDRV_ACCT_READ],
1966 bs->nr_bytes[BDRV_ACCT_WRITE],
1967 bs->nr_ops[BDRV_ACCT_READ],
1968 bs->nr_ops[BDRV_ACCT_WRITE],
Blue Swirl5ffbbc62010-06-14 18:55:33 +00001969 bs->wr_highest_sector *
Christoph Hellwige8045d62011-08-22 00:25:58 +02001970 (uint64_t)BDRV_SECTOR_SIZE,
Christoph Hellwigc488c7f2011-08-25 08:26:10 +02001971 bs->nr_ops[BDRV_ACCT_FLUSH],
1972 bs->total_time_ns[BDRV_ACCT_WRITE],
1973 bs->total_time_ns[BDRV_ACCT_READ],
1974 bs->total_time_ns[BDRV_ACCT_FLUSH]);
Kevin Wolf294cc352010-04-28 14:34:01 +02001975 dict = qobject_to_qdict(res);
1976
1977 if (*bs->device_name) {
1978 qdict_put(dict, "device", qstring_from_str(bs->device_name));
1979 }
1980
1981 if (bs->file) {
1982 QObject *parent = bdrv_info_stats_bs(bs->file);
1983 qdict_put_obj(dict, "parent", parent);
1984 }
1985
1986 return res;
1987}
1988
Luiz Capitulino218a5362009-12-10 17:16:07 -02001989void bdrv_info_stats(Monitor *mon, QObject **ret_data)
1990{
1991 QObject *obj;
1992 QList *devices;
thsa36e69d2007-12-02 05:18:19 +00001993 BlockDriverState *bs;
1994
Luiz Capitulino218a5362009-12-10 17:16:07 -02001995 devices = qlist_new();
1996
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001997 QTAILQ_FOREACH(bs, &bdrv_states, list) {
Kevin Wolf294cc352010-04-28 14:34:01 +02001998 obj = bdrv_info_stats_bs(bs);
Luiz Capitulino218a5362009-12-10 17:16:07 -02001999 qlist_append_obj(devices, obj);
thsa36e69d2007-12-02 05:18:19 +00002000 }
Luiz Capitulino218a5362009-12-10 17:16:07 -02002001
2002 *ret_data = QOBJECT(devices);
thsa36e69d2007-12-02 05:18:19 +00002003}
bellardea2384d2004-08-01 21:59:26 +00002004
aliguori045df332009-03-05 23:00:48 +00002005const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
2006{
2007 if (bs->backing_hd && bs->backing_hd->encrypted)
2008 return bs->backing_file;
2009 else if (bs->encrypted)
2010 return bs->filename;
2011 else
2012 return NULL;
2013}
2014
ths5fafdf22007-09-16 21:08:06 +00002015void bdrv_get_backing_filename(BlockDriverState *bs,
bellard83f64092006-08-01 16:21:11 +00002016 char *filename, int filename_size)
bellardea2384d2004-08-01 21:59:26 +00002017{
Kevin Wolfb783e402010-01-12 12:55:16 +01002018 if (!bs->backing_file) {
bellard83f64092006-08-01 16:21:11 +00002019 pstrcpy(filename, filename_size, "");
2020 } else {
2021 pstrcpy(filename, filename_size, bs->backing_file);
2022 }
bellardea2384d2004-08-01 21:59:26 +00002023}
2024
ths5fafdf22007-09-16 21:08:06 +00002025int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
bellardfaea38e2006-08-05 21:31:00 +00002026 const uint8_t *buf, int nb_sectors)
2027{
2028 BlockDriver *drv = bs->drv;
2029 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002030 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00002031 if (!drv->bdrv_write_compressed)
2032 return -ENOTSUP;
Kevin Wolffbb7b4e2009-05-08 14:47:24 +02002033 if (bdrv_check_request(bs, sector_num, nb_sectors))
2034 return -EIO;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002035
Jan Kiszkac6d22832009-11-30 18:21:20 +01002036 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002037 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
2038 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002039
bellardfaea38e2006-08-05 21:31:00 +00002040 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
2041}
ths3b46e622007-09-17 08:09:54 +00002042
bellardfaea38e2006-08-05 21:31:00 +00002043int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
2044{
2045 BlockDriver *drv = bs->drv;
2046 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002047 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00002048 if (!drv->bdrv_get_info)
2049 return -ENOTSUP;
2050 memset(bdi, 0, sizeof(*bdi));
2051 return drv->bdrv_get_info(bs, bdi);
2052}
2053
Christoph Hellwig45566e92009-07-10 23:11:57 +02002054int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
2055 int64_t pos, int size)
aliguori178e08a2009-04-05 19:10:55 +00002056{
2057 BlockDriver *drv = bs->drv;
2058 if (!drv)
2059 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09002060 if (drv->bdrv_save_vmstate)
2061 return drv->bdrv_save_vmstate(bs, buf, pos, size);
2062 if (bs->file)
2063 return bdrv_save_vmstate(bs->file, buf, pos, size);
2064 return -ENOTSUP;
aliguori178e08a2009-04-05 19:10:55 +00002065}
2066
Christoph Hellwig45566e92009-07-10 23:11:57 +02002067int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
2068 int64_t pos, int size)
aliguori178e08a2009-04-05 19:10:55 +00002069{
2070 BlockDriver *drv = bs->drv;
2071 if (!drv)
2072 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09002073 if (drv->bdrv_load_vmstate)
2074 return drv->bdrv_load_vmstate(bs, buf, pos, size);
2075 if (bs->file)
2076 return bdrv_load_vmstate(bs->file, buf, pos, size);
2077 return -ENOTSUP;
aliguori178e08a2009-04-05 19:10:55 +00002078}
2079
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01002080void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
2081{
2082 BlockDriver *drv = bs->drv;
2083
2084 if (!drv || !drv->bdrv_debug_event) {
2085 return;
2086 }
2087
2088 return drv->bdrv_debug_event(bs, event);
2089
2090}
2091
bellardfaea38e2006-08-05 21:31:00 +00002092/**************************************************************/
2093/* handling of snapshots */
2094
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002095int bdrv_can_snapshot(BlockDriverState *bs)
2096{
2097 BlockDriver *drv = bs->drv;
2098 if (!drv || bdrv_is_removable(bs) || bdrv_is_read_only(bs)) {
2099 return 0;
2100 }
2101
2102 if (!drv->bdrv_snapshot_create) {
2103 if (bs->file != NULL) {
2104 return bdrv_can_snapshot(bs->file);
2105 }
2106 return 0;
2107 }
2108
2109 return 1;
2110}
2111
Blue Swirl199630b2010-07-25 20:49:34 +00002112int bdrv_is_snapshot(BlockDriverState *bs)
2113{
2114 return !!(bs->open_flags & BDRV_O_SNAPSHOT);
2115}
2116
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002117BlockDriverState *bdrv_snapshots(void)
2118{
2119 BlockDriverState *bs;
2120
Markus Armbruster3ac906f2010-07-01 09:30:38 +02002121 if (bs_snapshots) {
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002122 return bs_snapshots;
Markus Armbruster3ac906f2010-07-01 09:30:38 +02002123 }
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002124
2125 bs = NULL;
2126 while ((bs = bdrv_next(bs))) {
2127 if (bdrv_can_snapshot(bs)) {
Markus Armbruster3ac906f2010-07-01 09:30:38 +02002128 bs_snapshots = bs;
2129 return bs;
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002130 }
2131 }
2132 return NULL;
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002133}
2134
ths5fafdf22007-09-16 21:08:06 +00002135int bdrv_snapshot_create(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00002136 QEMUSnapshotInfo *sn_info)
2137{
2138 BlockDriver *drv = bs->drv;
2139 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002140 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09002141 if (drv->bdrv_snapshot_create)
2142 return drv->bdrv_snapshot_create(bs, sn_info);
2143 if (bs->file)
2144 return bdrv_snapshot_create(bs->file, sn_info);
2145 return -ENOTSUP;
bellardfaea38e2006-08-05 21:31:00 +00002146}
2147
ths5fafdf22007-09-16 21:08:06 +00002148int bdrv_snapshot_goto(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00002149 const char *snapshot_id)
2150{
2151 BlockDriver *drv = bs->drv;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09002152 int ret, open_ret;
2153
bellardfaea38e2006-08-05 21:31:00 +00002154 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002155 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09002156 if (drv->bdrv_snapshot_goto)
2157 return drv->bdrv_snapshot_goto(bs, snapshot_id);
2158
2159 if (bs->file) {
2160 drv->bdrv_close(bs);
2161 ret = bdrv_snapshot_goto(bs->file, snapshot_id);
2162 open_ret = drv->bdrv_open(bs, bs->open_flags);
2163 if (open_ret < 0) {
2164 bdrv_delete(bs->file);
2165 bs->drv = NULL;
2166 return open_ret;
2167 }
2168 return ret;
2169 }
2170
2171 return -ENOTSUP;
bellardfaea38e2006-08-05 21:31:00 +00002172}
2173
2174int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
2175{
2176 BlockDriver *drv = bs->drv;
2177 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002178 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09002179 if (drv->bdrv_snapshot_delete)
2180 return drv->bdrv_snapshot_delete(bs, snapshot_id);
2181 if (bs->file)
2182 return bdrv_snapshot_delete(bs->file, snapshot_id);
2183 return -ENOTSUP;
bellardfaea38e2006-08-05 21:31:00 +00002184}
2185
ths5fafdf22007-09-16 21:08:06 +00002186int bdrv_snapshot_list(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00002187 QEMUSnapshotInfo **psn_info)
2188{
2189 BlockDriver *drv = bs->drv;
2190 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002191 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09002192 if (drv->bdrv_snapshot_list)
2193 return drv->bdrv_snapshot_list(bs, psn_info);
2194 if (bs->file)
2195 return bdrv_snapshot_list(bs->file, psn_info);
2196 return -ENOTSUP;
bellardfaea38e2006-08-05 21:31:00 +00002197}
2198
edison51ef6722010-09-21 19:58:41 -07002199int bdrv_snapshot_load_tmp(BlockDriverState *bs,
2200 const char *snapshot_name)
2201{
2202 BlockDriver *drv = bs->drv;
2203 if (!drv) {
2204 return -ENOMEDIUM;
2205 }
2206 if (!bs->read_only) {
2207 return -EINVAL;
2208 }
2209 if (drv->bdrv_snapshot_load_tmp) {
2210 return drv->bdrv_snapshot_load_tmp(bs, snapshot_name);
2211 }
2212 return -ENOTSUP;
2213}
2214
bellardfaea38e2006-08-05 21:31:00 +00002215#define NB_SUFFIXES 4
2216
2217char *get_human_readable_size(char *buf, int buf_size, int64_t size)
2218{
2219 static const char suffixes[NB_SUFFIXES] = "KMGT";
2220 int64_t base;
2221 int i;
2222
2223 if (size <= 999) {
2224 snprintf(buf, buf_size, "%" PRId64, size);
2225 } else {
2226 base = 1024;
2227 for(i = 0; i < NB_SUFFIXES; i++) {
2228 if (size < (10 * base)) {
ths5fafdf22007-09-16 21:08:06 +00002229 snprintf(buf, buf_size, "%0.1f%c",
bellardfaea38e2006-08-05 21:31:00 +00002230 (double)size / base,
2231 suffixes[i]);
2232 break;
2233 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
ths5fafdf22007-09-16 21:08:06 +00002234 snprintf(buf, buf_size, "%" PRId64 "%c",
bellardfaea38e2006-08-05 21:31:00 +00002235 ((size + (base >> 1)) / base),
2236 suffixes[i]);
2237 break;
2238 }
2239 base = base * 1024;
2240 }
2241 }
2242 return buf;
2243}
2244
2245char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
2246{
2247 char buf1[128], date_buf[128], clock_buf[128];
bellard3b9f94e2007-01-07 17:27:07 +00002248#ifdef _WIN32
2249 struct tm *ptm;
2250#else
bellardfaea38e2006-08-05 21:31:00 +00002251 struct tm tm;
bellard3b9f94e2007-01-07 17:27:07 +00002252#endif
bellardfaea38e2006-08-05 21:31:00 +00002253 time_t ti;
2254 int64_t secs;
2255
2256 if (!sn) {
ths5fafdf22007-09-16 21:08:06 +00002257 snprintf(buf, buf_size,
2258 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00002259 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
2260 } else {
2261 ti = sn->date_sec;
bellard3b9f94e2007-01-07 17:27:07 +00002262#ifdef _WIN32
2263 ptm = localtime(&ti);
2264 strftime(date_buf, sizeof(date_buf),
2265 "%Y-%m-%d %H:%M:%S", ptm);
2266#else
bellardfaea38e2006-08-05 21:31:00 +00002267 localtime_r(&ti, &tm);
2268 strftime(date_buf, sizeof(date_buf),
2269 "%Y-%m-%d %H:%M:%S", &tm);
bellard3b9f94e2007-01-07 17:27:07 +00002270#endif
bellardfaea38e2006-08-05 21:31:00 +00002271 secs = sn->vm_clock_nsec / 1000000000;
2272 snprintf(clock_buf, sizeof(clock_buf),
2273 "%02d:%02d:%02d.%03d",
2274 (int)(secs / 3600),
2275 (int)((secs / 60) % 60),
ths5fafdf22007-09-16 21:08:06 +00002276 (int)(secs % 60),
bellardfaea38e2006-08-05 21:31:00 +00002277 (int)((sn->vm_clock_nsec / 1000000) % 1000));
2278 snprintf(buf, buf_size,
ths5fafdf22007-09-16 21:08:06 +00002279 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00002280 sn->id_str, sn->name,
2281 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
2282 date_buf,
2283 clock_buf);
2284 }
2285 return buf;
2286}
2287
bellard83f64092006-08-01 16:21:11 +00002288/**************************************************************/
2289/* async I/Os */
2290
aliguori3b69e4b2009-01-22 16:59:24 +00002291BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
aliguorif141eaf2009-04-07 18:43:24 +00002292 QEMUIOVector *qiov, int nb_sectors,
aliguori3b69e4b2009-01-22 16:59:24 +00002293 BlockDriverCompletionFunc *cb, void *opaque)
2294{
bellard83f64092006-08-01 16:21:11 +00002295 BlockDriver *drv = bs->drv;
bellard83f64092006-08-01 16:21:11 +00002296
Stefan Hajnoczibbf0a442010-10-05 14:28:53 +01002297 trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque);
2298
bellard19cb3732006-08-19 11:45:59 +00002299 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00002300 return NULL;
aliguori71d07702009-03-03 17:37:16 +00002301 if (bdrv_check_request(bs, sector_num, nb_sectors))
2302 return NULL;
ths3b46e622007-09-17 08:09:54 +00002303
Christoph Hellwiga597e792011-08-25 08:26:01 +02002304 return drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
2305 cb, opaque);
bellard83f64092006-08-01 16:21:11 +00002306}
2307
Marcelo Tosatti4dcafbb2010-11-08 17:02:55 -02002308typedef struct BlockCompleteData {
2309 BlockDriverCompletionFunc *cb;
2310 void *opaque;
2311 BlockDriverState *bs;
2312 int64_t sector_num;
2313 int nb_sectors;
2314} BlockCompleteData;
2315
2316static void block_complete_cb(void *opaque, int ret)
2317{
2318 BlockCompleteData *b = opaque;
2319
2320 if (b->bs->dirty_bitmap) {
2321 set_dirty_bitmap(b->bs, b->sector_num, b->nb_sectors, 1);
2322 }
2323 b->cb(b->opaque, ret);
Anthony Liguori7267c092011-08-20 22:09:37 -05002324 g_free(b);
Marcelo Tosatti4dcafbb2010-11-08 17:02:55 -02002325}
2326
2327static BlockCompleteData *blk_dirty_cb_alloc(BlockDriverState *bs,
2328 int64_t sector_num,
2329 int nb_sectors,
2330 BlockDriverCompletionFunc *cb,
2331 void *opaque)
2332{
Anthony Liguori7267c092011-08-20 22:09:37 -05002333 BlockCompleteData *blkdata = g_malloc0(sizeof(BlockCompleteData));
Marcelo Tosatti4dcafbb2010-11-08 17:02:55 -02002334
2335 blkdata->bs = bs;
2336 blkdata->cb = cb;
2337 blkdata->opaque = opaque;
2338 blkdata->sector_num = sector_num;
2339 blkdata->nb_sectors = nb_sectors;
2340
2341 return blkdata;
2342}
2343
aliguorif141eaf2009-04-07 18:43:24 +00002344BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
2345 QEMUIOVector *qiov, int nb_sectors,
2346 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00002347{
bellard83f64092006-08-01 16:21:11 +00002348 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00002349 BlockDriverAIOCB *ret;
Marcelo Tosatti4dcafbb2010-11-08 17:02:55 -02002350 BlockCompleteData *blk_cb_data;
bellard83f64092006-08-01 16:21:11 +00002351
Stefan Hajnoczibbf0a442010-10-05 14:28:53 +01002352 trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque);
2353
bellard19cb3732006-08-19 11:45:59 +00002354 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00002355 return NULL;
bellard83f64092006-08-01 16:21:11 +00002356 if (bs->read_only)
pbrookce1a14d2006-08-07 02:38:06 +00002357 return NULL;
aliguori71d07702009-03-03 17:37:16 +00002358 if (bdrv_check_request(bs, sector_num, nb_sectors))
2359 return NULL;
bellard83f64092006-08-01 16:21:11 +00002360
Jan Kiszkac6d22832009-11-30 18:21:20 +01002361 if (bs->dirty_bitmap) {
Marcelo Tosatti4dcafbb2010-11-08 17:02:55 -02002362 blk_cb_data = blk_dirty_cb_alloc(bs, sector_num, nb_sectors, cb,
2363 opaque);
2364 cb = &block_complete_cb;
2365 opaque = blk_cb_data;
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002366 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002367
aliguorif141eaf2009-04-07 18:43:24 +00002368 ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
2369 cb, opaque);
thsa36e69d2007-12-02 05:18:19 +00002370
2371 if (ret) {
Kevin Wolf294cc352010-04-28 14:34:01 +02002372 if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
2373 bs->wr_highest_sector = sector_num + nb_sectors - 1;
2374 }
thsa36e69d2007-12-02 05:18:19 +00002375 }
2376
2377 return ret;
bellard83f64092006-08-01 16:21:11 +00002378}
2379
Kevin Wolf40b4f532009-09-09 17:53:37 +02002380
2381typedef struct MultiwriteCB {
2382 int error;
2383 int num_requests;
2384 int num_callbacks;
2385 struct {
2386 BlockDriverCompletionFunc *cb;
2387 void *opaque;
2388 QEMUIOVector *free_qiov;
2389 void *free_buf;
2390 } callbacks[];
2391} MultiwriteCB;
2392
2393static void multiwrite_user_cb(MultiwriteCB *mcb)
2394{
2395 int i;
2396
2397 for (i = 0; i < mcb->num_callbacks; i++) {
2398 mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
Stefan Hajnoczi1e1ea482010-04-21 20:35:45 +01002399 if (mcb->callbacks[i].free_qiov) {
2400 qemu_iovec_destroy(mcb->callbacks[i].free_qiov);
2401 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002402 g_free(mcb->callbacks[i].free_qiov);
Herve Poussineauf8a83242010-01-24 21:23:56 +00002403 qemu_vfree(mcb->callbacks[i].free_buf);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002404 }
2405}
2406
2407static void multiwrite_cb(void *opaque, int ret)
2408{
2409 MultiwriteCB *mcb = opaque;
2410
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +01002411 trace_multiwrite_cb(mcb, ret);
2412
Kevin Wolfcb6d3ca2010-04-01 22:48:44 +02002413 if (ret < 0 && !mcb->error) {
Kevin Wolf40b4f532009-09-09 17:53:37 +02002414 mcb->error = ret;
Kevin Wolf40b4f532009-09-09 17:53:37 +02002415 }
2416
2417 mcb->num_requests--;
2418 if (mcb->num_requests == 0) {
Kevin Wolfde189a12010-07-01 16:08:51 +02002419 multiwrite_user_cb(mcb);
Anthony Liguori7267c092011-08-20 22:09:37 -05002420 g_free(mcb);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002421 }
2422}
2423
2424static int multiwrite_req_compare(const void *a, const void *b)
2425{
Christoph Hellwig77be4362010-05-19 20:53:10 +02002426 const BlockRequest *req1 = a, *req2 = b;
2427
2428 /*
2429 * Note that we can't simply subtract req2->sector from req1->sector
2430 * here as that could overflow the return value.
2431 */
2432 if (req1->sector > req2->sector) {
2433 return 1;
2434 } else if (req1->sector < req2->sector) {
2435 return -1;
2436 } else {
2437 return 0;
2438 }
Kevin Wolf40b4f532009-09-09 17:53:37 +02002439}
2440
2441/*
2442 * Takes a bunch of requests and tries to merge them. Returns the number of
2443 * requests that remain after merging.
2444 */
2445static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
2446 int num_reqs, MultiwriteCB *mcb)
2447{
2448 int i, outidx;
2449
2450 // Sort requests by start sector
2451 qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
2452
2453 // Check if adjacent requests touch the same clusters. If so, combine them,
2454 // filling up gaps with zero sectors.
2455 outidx = 0;
2456 for (i = 1; i < num_reqs; i++) {
2457 int merge = 0;
2458 int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
2459
2460 // This handles the cases that are valid for all block drivers, namely
2461 // exactly sequential writes and overlapping writes.
2462 if (reqs[i].sector <= oldreq_last) {
2463 merge = 1;
2464 }
2465
2466 // The block driver may decide that it makes sense to combine requests
2467 // even if there is a gap of some sectors between them. In this case,
2468 // the gap is filled with zeros (therefore only applicable for yet
2469 // unused space in format like qcow2).
2470 if (!merge && bs->drv->bdrv_merge_requests) {
2471 merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]);
2472 }
2473
Christoph Hellwige2a305f2010-01-26 14:49:08 +01002474 if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) {
2475 merge = 0;
2476 }
2477
Kevin Wolf40b4f532009-09-09 17:53:37 +02002478 if (merge) {
2479 size_t size;
Anthony Liguori7267c092011-08-20 22:09:37 -05002480 QEMUIOVector *qiov = g_malloc0(sizeof(*qiov));
Kevin Wolf40b4f532009-09-09 17:53:37 +02002481 qemu_iovec_init(qiov,
2482 reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
2483
2484 // Add the first request to the merged one. If the requests are
2485 // overlapping, drop the last sectors of the first request.
2486 size = (reqs[i].sector - reqs[outidx].sector) << 9;
2487 qemu_iovec_concat(qiov, reqs[outidx].qiov, size);
2488
2489 // We might need to add some zeros between the two requests
2490 if (reqs[i].sector > oldreq_last) {
2491 size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
2492 uint8_t *buf = qemu_blockalign(bs, zero_bytes);
2493 memset(buf, 0, zero_bytes);
2494 qemu_iovec_add(qiov, buf, zero_bytes);
2495 mcb->callbacks[i].free_buf = buf;
2496 }
2497
2498 // Add the second request
2499 qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size);
2500
Kevin Wolfcbf1dff2010-05-21 11:09:42 +02002501 reqs[outidx].nb_sectors = qiov->size >> 9;
Kevin Wolf40b4f532009-09-09 17:53:37 +02002502 reqs[outidx].qiov = qiov;
2503
2504 mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
2505 } else {
2506 outidx++;
2507 reqs[outidx].sector = reqs[i].sector;
2508 reqs[outidx].nb_sectors = reqs[i].nb_sectors;
2509 reqs[outidx].qiov = reqs[i].qiov;
2510 }
2511 }
2512
2513 return outidx + 1;
2514}
2515
2516/*
2517 * Submit multiple AIO write requests at once.
2518 *
2519 * On success, the function returns 0 and all requests in the reqs array have
2520 * been submitted. In error case this function returns -1, and any of the
2521 * requests may or may not be submitted yet. In particular, this means that the
2522 * callback will be called for some of the requests, for others it won't. The
2523 * caller must check the error field of the BlockRequest to wait for the right
2524 * callbacks (if error != 0, no callback will be called).
2525 *
2526 * The implementation may modify the contents of the reqs array, e.g. to merge
2527 * requests. However, the fields opaque and error are left unmodified as they
2528 * are used to signal failure for a single request to the caller.
2529 */
2530int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
2531{
2532 BlockDriverAIOCB *acb;
2533 MultiwriteCB *mcb;
2534 int i;
2535
Ryan Harper301db7c2011-03-07 10:01:04 -06002536 /* don't submit writes if we don't have a medium */
2537 if (bs->drv == NULL) {
2538 for (i = 0; i < num_reqs; i++) {
2539 reqs[i].error = -ENOMEDIUM;
2540 }
2541 return -1;
2542 }
2543
Kevin Wolf40b4f532009-09-09 17:53:37 +02002544 if (num_reqs == 0) {
2545 return 0;
2546 }
2547
2548 // Create MultiwriteCB structure
Anthony Liguori7267c092011-08-20 22:09:37 -05002549 mcb = g_malloc0(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
Kevin Wolf40b4f532009-09-09 17:53:37 +02002550 mcb->num_requests = 0;
2551 mcb->num_callbacks = num_reqs;
2552
2553 for (i = 0; i < num_reqs; i++) {
2554 mcb->callbacks[i].cb = reqs[i].cb;
2555 mcb->callbacks[i].opaque = reqs[i].opaque;
2556 }
2557
2558 // Check for mergable requests
2559 num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
2560
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +01002561 trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs);
2562
Kevin Wolf453f9a12010-07-02 14:01:21 +02002563 /*
2564 * Run the aio requests. As soon as one request can't be submitted
2565 * successfully, fail all requests that are not yet submitted (we must
2566 * return failure for all requests anyway)
2567 *
2568 * num_requests cannot be set to the right value immediately: If
2569 * bdrv_aio_writev fails for some request, num_requests would be too high
2570 * and therefore multiwrite_cb() would never recognize the multiwrite
2571 * request as completed. We also cannot use the loop variable i to set it
2572 * when the first request fails because the callback may already have been
2573 * called for previously submitted requests. Thus, num_requests must be
2574 * incremented for each request that is submitted.
2575 *
2576 * The problem that callbacks may be called early also means that we need
2577 * to take care that num_requests doesn't become 0 before all requests are
2578 * submitted - multiwrite_cb() would consider the multiwrite request
2579 * completed. A dummy request that is "completed" by a manual call to
2580 * multiwrite_cb() takes care of this.
2581 */
2582 mcb->num_requests = 1;
2583
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +01002584 // Run the aio requests
Kevin Wolf40b4f532009-09-09 17:53:37 +02002585 for (i = 0; i < num_reqs; i++) {
Kevin Wolf453f9a12010-07-02 14:01:21 +02002586 mcb->num_requests++;
Kevin Wolf40b4f532009-09-09 17:53:37 +02002587 acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
2588 reqs[i].nb_sectors, multiwrite_cb, mcb);
2589
2590 if (acb == NULL) {
2591 // We can only fail the whole thing if no request has been
2592 // submitted yet. Otherwise we'll wait for the submitted AIOs to
2593 // complete and report the error in the callback.
Kevin Wolf453f9a12010-07-02 14:01:21 +02002594 if (i == 0) {
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +01002595 trace_bdrv_aio_multiwrite_earlyfail(mcb);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002596 goto fail;
2597 } else {
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +01002598 trace_bdrv_aio_multiwrite_latefail(mcb, i);
Kevin Wolf7eb58a62010-04-06 18:24:07 +02002599 multiwrite_cb(mcb, -EIO);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002600 break;
2601 }
Kevin Wolf40b4f532009-09-09 17:53:37 +02002602 }
2603 }
2604
Kevin Wolf453f9a12010-07-02 14:01:21 +02002605 /* Complete the dummy request */
2606 multiwrite_cb(mcb, 0);
2607
Kevin Wolf40b4f532009-09-09 17:53:37 +02002608 return 0;
2609
2610fail:
Kevin Wolf453f9a12010-07-02 14:01:21 +02002611 for (i = 0; i < mcb->num_callbacks; i++) {
2612 reqs[i].error = -EIO;
2613 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002614 g_free(mcb);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002615 return -1;
2616}
2617
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002618BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
2619 BlockDriverCompletionFunc *cb, void *opaque)
2620{
2621 BlockDriver *drv = bs->drv;
2622
Stefan Hajnoczia13aac02011-03-07 07:58:04 +00002623 trace_bdrv_aio_flush(bs, opaque);
2624
Alexander Graf016f5cf2010-05-26 17:51:49 +02002625 if (bs->open_flags & BDRV_O_NO_FLUSH) {
2626 return bdrv_aio_noop_em(bs, cb, opaque);
2627 }
2628
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002629 if (!drv)
2630 return NULL;
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002631 return drv->bdrv_aio_flush(bs, cb, opaque);
2632}
2633
bellard83f64092006-08-01 16:21:11 +00002634void bdrv_aio_cancel(BlockDriverAIOCB *acb)
pbrookce1a14d2006-08-07 02:38:06 +00002635{
aliguori6bbff9a2009-03-20 18:25:59 +00002636 acb->pool->cancel(acb);
bellard83f64092006-08-01 16:21:11 +00002637}
2638
pbrookce1a14d2006-08-07 02:38:06 +00002639
bellard83f64092006-08-01 16:21:11 +00002640/**************************************************************/
2641/* async block device emulation */
2642
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002643typedef struct BlockDriverAIOCBSync {
2644 BlockDriverAIOCB common;
2645 QEMUBH *bh;
2646 int ret;
2647 /* vector translation state */
2648 QEMUIOVector *qiov;
2649 uint8_t *bounce;
2650 int is_write;
2651} BlockDriverAIOCBSync;
2652
2653static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
2654{
Kevin Wolfb666d232010-05-05 11:44:39 +02002655 BlockDriverAIOCBSync *acb =
2656 container_of(blockacb, BlockDriverAIOCBSync, common);
Dor Laor6a7ad292009-06-01 12:07:23 +03002657 qemu_bh_delete(acb->bh);
Avi Kivity36afc452009-06-23 16:20:36 +03002658 acb->bh = NULL;
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002659 qemu_aio_release(acb);
2660}
2661
2662static AIOPool bdrv_em_aio_pool = {
2663 .aiocb_size = sizeof(BlockDriverAIOCBSync),
2664 .cancel = bdrv_aio_cancel_em,
2665};
2666
bellard83f64092006-08-01 16:21:11 +00002667static void bdrv_aio_bh_cb(void *opaque)
bellardbeac80c2006-06-26 20:08:57 +00002668{
pbrookce1a14d2006-08-07 02:38:06 +00002669 BlockDriverAIOCBSync *acb = opaque;
aliguorif141eaf2009-04-07 18:43:24 +00002670
aliguorif141eaf2009-04-07 18:43:24 +00002671 if (!acb->is_write)
2672 qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
aliguoriceb42de2009-04-07 18:43:28 +00002673 qemu_vfree(acb->bounce);
pbrookce1a14d2006-08-07 02:38:06 +00002674 acb->common.cb(acb->common.opaque, acb->ret);
Dor Laor6a7ad292009-06-01 12:07:23 +03002675 qemu_bh_delete(acb->bh);
Avi Kivity36afc452009-06-23 16:20:36 +03002676 acb->bh = NULL;
pbrookce1a14d2006-08-07 02:38:06 +00002677 qemu_aio_release(acb);
bellardbeac80c2006-06-26 20:08:57 +00002678}
bellardbeac80c2006-06-26 20:08:57 +00002679
aliguorif141eaf2009-04-07 18:43:24 +00002680static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
2681 int64_t sector_num,
2682 QEMUIOVector *qiov,
2683 int nb_sectors,
2684 BlockDriverCompletionFunc *cb,
2685 void *opaque,
2686 int is_write)
2687
bellardea2384d2004-08-01 21:59:26 +00002688{
pbrookce1a14d2006-08-07 02:38:06 +00002689 BlockDriverAIOCBSync *acb;
pbrookce1a14d2006-08-07 02:38:06 +00002690
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002691 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
aliguorif141eaf2009-04-07 18:43:24 +00002692 acb->is_write = is_write;
2693 acb->qiov = qiov;
aliguorie268ca52009-04-22 20:20:00 +00002694 acb->bounce = qemu_blockalign(bs, qiov->size);
aliguorif141eaf2009-04-07 18:43:24 +00002695
pbrookce1a14d2006-08-07 02:38:06 +00002696 if (!acb->bh)
2697 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
aliguorif141eaf2009-04-07 18:43:24 +00002698
2699 if (is_write) {
2700 qemu_iovec_to_buffer(acb->qiov, acb->bounce);
2701 acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
2702 } else {
2703 acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
2704 }
2705
pbrookce1a14d2006-08-07 02:38:06 +00002706 qemu_bh_schedule(acb->bh);
aliguorif141eaf2009-04-07 18:43:24 +00002707
pbrookce1a14d2006-08-07 02:38:06 +00002708 return &acb->common;
pbrook7a6cba62006-06-04 11:39:07 +00002709}
2710
aliguorif141eaf2009-04-07 18:43:24 +00002711static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
2712 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
pbrookce1a14d2006-08-07 02:38:06 +00002713 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00002714{
aliguorif141eaf2009-04-07 18:43:24 +00002715 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
bellard83f64092006-08-01 16:21:11 +00002716}
2717
aliguorif141eaf2009-04-07 18:43:24 +00002718static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
2719 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2720 BlockDriverCompletionFunc *cb, void *opaque)
2721{
2722 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
2723}
2724
Kevin Wolf68485422011-06-30 10:05:46 +02002725
2726typedef struct BlockDriverAIOCBCoroutine {
2727 BlockDriverAIOCB common;
2728 BlockRequest req;
2729 bool is_write;
2730 QEMUBH* bh;
2731} BlockDriverAIOCBCoroutine;
2732
2733static void bdrv_aio_co_cancel_em(BlockDriverAIOCB *blockacb)
2734{
2735 qemu_aio_flush();
2736}
2737
2738static AIOPool bdrv_em_co_aio_pool = {
2739 .aiocb_size = sizeof(BlockDriverAIOCBCoroutine),
2740 .cancel = bdrv_aio_co_cancel_em,
2741};
2742
2743static void bdrv_co_rw_bh(void *opaque)
2744{
2745 BlockDriverAIOCBCoroutine *acb = opaque;
2746
2747 acb->common.cb(acb->common.opaque, acb->req.error);
2748 qemu_bh_delete(acb->bh);
2749 qemu_aio_release(acb);
2750}
2751
2752static void coroutine_fn bdrv_co_rw(void *opaque)
2753{
2754 BlockDriverAIOCBCoroutine *acb = opaque;
2755 BlockDriverState *bs = acb->common.bs;
2756
2757 if (!acb->is_write) {
2758 acb->req.error = bs->drv->bdrv_co_readv(bs, acb->req.sector,
2759 acb->req.nb_sectors, acb->req.qiov);
2760 } else {
2761 acb->req.error = bs->drv->bdrv_co_writev(bs, acb->req.sector,
2762 acb->req.nb_sectors, acb->req.qiov);
2763 }
2764
2765 acb->bh = qemu_bh_new(bdrv_co_rw_bh, acb);
2766 qemu_bh_schedule(acb->bh);
2767}
2768
2769static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs,
2770 int64_t sector_num,
2771 QEMUIOVector *qiov,
2772 int nb_sectors,
2773 BlockDriverCompletionFunc *cb,
2774 void *opaque,
2775 bool is_write)
2776{
2777 Coroutine *co;
2778 BlockDriverAIOCBCoroutine *acb;
2779
2780 acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque);
2781 acb->req.sector = sector_num;
2782 acb->req.nb_sectors = nb_sectors;
2783 acb->req.qiov = qiov;
2784 acb->is_write = is_write;
2785
2786 co = qemu_coroutine_create(bdrv_co_rw);
2787 qemu_coroutine_enter(co, acb);
2788
2789 return &acb->common;
2790}
2791
2792static BlockDriverAIOCB *bdrv_co_aio_readv_em(BlockDriverState *bs,
2793 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2794 BlockDriverCompletionFunc *cb, void *opaque)
2795{
2796 return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque,
2797 false);
2798}
2799
2800static BlockDriverAIOCB *bdrv_co_aio_writev_em(BlockDriverState *bs,
2801 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2802 BlockDriverCompletionFunc *cb, void *opaque)
2803{
2804 return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque,
2805 true);
2806}
2807
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002808static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
2809 BlockDriverCompletionFunc *cb, void *opaque)
2810{
2811 BlockDriverAIOCBSync *acb;
2812
2813 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2814 acb->is_write = 1; /* don't bounce in the completion hadler */
2815 acb->qiov = NULL;
2816 acb->bounce = NULL;
2817 acb->ret = 0;
2818
2819 if (!acb->bh)
2820 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2821
2822 bdrv_flush(bs);
2823 qemu_bh_schedule(acb->bh);
2824 return &acb->common;
2825}
2826
Alexander Graf016f5cf2010-05-26 17:51:49 +02002827static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,
2828 BlockDriverCompletionFunc *cb, void *opaque)
2829{
2830 BlockDriverAIOCBSync *acb;
2831
2832 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2833 acb->is_write = 1; /* don't bounce in the completion handler */
2834 acb->qiov = NULL;
2835 acb->bounce = NULL;
2836 acb->ret = 0;
2837
2838 if (!acb->bh) {
2839 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2840 }
2841
2842 qemu_bh_schedule(acb->bh);
2843 return &acb->common;
2844}
2845
bellard83f64092006-08-01 16:21:11 +00002846/**************************************************************/
2847/* sync block device emulation */
2848
2849static void bdrv_rw_em_cb(void *opaque, int ret)
2850{
2851 *(int *)opaque = ret;
2852}
2853
2854#define NOT_DONE 0x7fffffff
2855
ths5fafdf22007-09-16 21:08:06 +00002856static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +00002857 uint8_t *buf, int nb_sectors)
2858{
pbrookce1a14d2006-08-07 02:38:06 +00002859 int async_ret;
2860 BlockDriverAIOCB *acb;
aliguorif141eaf2009-04-07 18:43:24 +00002861 struct iovec iov;
2862 QEMUIOVector qiov;
bellard83f64092006-08-01 16:21:11 +00002863
bellard83f64092006-08-01 16:21:11 +00002864 async_ret = NOT_DONE;
blueswir13f4cb3d2009-04-13 16:31:01 +00002865 iov.iov_base = (void *)buf;
Jes Sorenseneb5a3162010-05-27 16:20:31 +02002866 iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
aliguorif141eaf2009-04-07 18:43:24 +00002867 qemu_iovec_init_external(&qiov, &iov, 1);
2868 acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
2869 bdrv_rw_em_cb, &async_ret);
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002870 if (acb == NULL) {
2871 async_ret = -1;
2872 goto fail;
2873 }
aliguoribaf35cb2008-09-10 15:45:19 +00002874
bellard83f64092006-08-01 16:21:11 +00002875 while (async_ret == NOT_DONE) {
2876 qemu_aio_wait();
2877 }
aliguoribaf35cb2008-09-10 15:45:19 +00002878
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002879
2880fail:
bellard83f64092006-08-01 16:21:11 +00002881 return async_ret;
2882}
2883
2884static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
2885 const uint8_t *buf, int nb_sectors)
2886{
pbrookce1a14d2006-08-07 02:38:06 +00002887 int async_ret;
2888 BlockDriverAIOCB *acb;
aliguorif141eaf2009-04-07 18:43:24 +00002889 struct iovec iov;
2890 QEMUIOVector qiov;
bellard83f64092006-08-01 16:21:11 +00002891
bellard83f64092006-08-01 16:21:11 +00002892 async_ret = NOT_DONE;
aliguorif141eaf2009-04-07 18:43:24 +00002893 iov.iov_base = (void *)buf;
Jes Sorenseneb5a3162010-05-27 16:20:31 +02002894 iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
aliguorif141eaf2009-04-07 18:43:24 +00002895 qemu_iovec_init_external(&qiov, &iov, 1);
2896 acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
2897 bdrv_rw_em_cb, &async_ret);
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002898 if (acb == NULL) {
2899 async_ret = -1;
2900 goto fail;
2901 }
bellard83f64092006-08-01 16:21:11 +00002902 while (async_ret == NOT_DONE) {
2903 qemu_aio_wait();
2904 }
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002905
2906fail:
bellard83f64092006-08-01 16:21:11 +00002907 return async_ret;
2908}
bellardea2384d2004-08-01 21:59:26 +00002909
2910void bdrv_init(void)
2911{
Anthony Liguori5efa9d52009-05-09 17:03:42 -05002912 module_call_init(MODULE_INIT_BLOCK);
bellardea2384d2004-08-01 21:59:26 +00002913}
pbrookce1a14d2006-08-07 02:38:06 +00002914
Markus Armbrustereb852012009-10-27 18:41:44 +01002915void bdrv_init_with_whitelist(void)
2916{
2917 use_bdrv_whitelist = 1;
2918 bdrv_init();
2919}
2920
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002921void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
2922 BlockDriverCompletionFunc *cb, void *opaque)
aliguori6bbff9a2009-03-20 18:25:59 +00002923{
pbrookce1a14d2006-08-07 02:38:06 +00002924 BlockDriverAIOCB *acb;
2925
aliguori6bbff9a2009-03-20 18:25:59 +00002926 if (pool->free_aiocb) {
2927 acb = pool->free_aiocb;
2928 pool->free_aiocb = acb->next;
pbrookce1a14d2006-08-07 02:38:06 +00002929 } else {
Anthony Liguori7267c092011-08-20 22:09:37 -05002930 acb = g_malloc0(pool->aiocb_size);
aliguori6bbff9a2009-03-20 18:25:59 +00002931 acb->pool = pool;
pbrookce1a14d2006-08-07 02:38:06 +00002932 }
2933 acb->bs = bs;
2934 acb->cb = cb;
2935 acb->opaque = opaque;
2936 return acb;
2937}
2938
2939void qemu_aio_release(void *p)
2940{
aliguori6bbff9a2009-03-20 18:25:59 +00002941 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
2942 AIOPool *pool = acb->pool;
2943 acb->next = pool->free_aiocb;
2944 pool->free_aiocb = acb;
pbrookce1a14d2006-08-07 02:38:06 +00002945}
bellard19cb3732006-08-19 11:45:59 +00002946
2947/**************************************************************/
Kevin Wolff9f05dc2011-07-15 13:50:26 +02002948/* Coroutine block device emulation */
2949
2950typedef struct CoroutineIOCompletion {
2951 Coroutine *coroutine;
2952 int ret;
2953} CoroutineIOCompletion;
2954
2955static void bdrv_co_io_em_complete(void *opaque, int ret)
2956{
2957 CoroutineIOCompletion *co = opaque;
2958
2959 co->ret = ret;
2960 qemu_coroutine_enter(co->coroutine, NULL);
2961}
2962
2963static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num,
2964 int nb_sectors, QEMUIOVector *iov,
2965 bool is_write)
2966{
2967 CoroutineIOCompletion co = {
2968 .coroutine = qemu_coroutine_self(),
2969 };
2970 BlockDriverAIOCB *acb;
2971
2972 if (is_write) {
2973 acb = bdrv_aio_writev(bs, sector_num, iov, nb_sectors,
2974 bdrv_co_io_em_complete, &co);
2975 } else {
2976 acb = bdrv_aio_readv(bs, sector_num, iov, nb_sectors,
2977 bdrv_co_io_em_complete, &co);
2978 }
2979
2980 trace_bdrv_co_io(is_write, acb);
2981 if (!acb) {
2982 return -EIO;
2983 }
2984 qemu_coroutine_yield();
2985
2986 return co.ret;
2987}
2988
2989static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs,
2990 int64_t sector_num, int nb_sectors,
2991 QEMUIOVector *iov)
2992{
2993 return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false);
2994}
2995
2996static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs,
2997 int64_t sector_num, int nb_sectors,
2998 QEMUIOVector *iov)
2999{
3000 return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true);
3001}
3002
Kevin Wolfe7a8a782011-07-15 16:05:00 +02003003static int coroutine_fn bdrv_co_flush_em(BlockDriverState *bs)
3004{
3005 CoroutineIOCompletion co = {
3006 .coroutine = qemu_coroutine_self(),
3007 };
3008 BlockDriverAIOCB *acb;
3009
3010 acb = bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co);
3011 if (!acb) {
3012 return -EIO;
3013 }
3014 qemu_coroutine_yield();
3015 return co.ret;
3016}
3017
Kevin Wolff9f05dc2011-07-15 13:50:26 +02003018/**************************************************************/
bellard19cb3732006-08-19 11:45:59 +00003019/* removable device support */
3020
3021/**
3022 * Return TRUE if the media is present
3023 */
3024int bdrv_is_inserted(BlockDriverState *bs)
3025{
3026 BlockDriver *drv = bs->drv;
3027 int ret;
3028 if (!drv)
3029 return 0;
3030 if (!drv->bdrv_is_inserted)
Markus Armbruster4be97622010-07-27 14:02:01 +02003031 return !bs->tray_open;
bellard19cb3732006-08-19 11:45:59 +00003032 ret = drv->bdrv_is_inserted(bs);
3033 return ret;
3034}
3035
3036/**
3037 * Return TRUE if the media changed since the last call to this
ths5fafdf22007-09-16 21:08:06 +00003038 * function. It is currently only used for floppy disks
bellard19cb3732006-08-19 11:45:59 +00003039 */
3040int bdrv_media_changed(BlockDriverState *bs)
3041{
3042 BlockDriver *drv = bs->drv;
3043 int ret;
3044
3045 if (!drv || !drv->bdrv_media_changed)
3046 ret = -ENOTSUP;
3047 else
3048 ret = drv->bdrv_media_changed(bs);
3049 if (ret == -ENOTSUP)
3050 ret = bs->media_changed;
3051 bs->media_changed = 0;
3052 return ret;
3053}
3054
3055/**
3056 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
3057 */
Mark McLoughlinaea2a332009-05-27 10:06:11 +01003058int bdrv_eject(BlockDriverState *bs, int eject_flag)
bellard19cb3732006-08-19 11:45:59 +00003059{
3060 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +00003061
Markus Armbruster49aa46b2011-07-20 18:23:43 +02003062 if (eject_flag && bs->locked) {
Mark McLoughlinaea2a332009-05-27 10:06:11 +01003063 return -EBUSY;
3064 }
3065
Markus Armbruster822e1cd2011-07-20 18:23:42 +02003066 if (drv && drv->bdrv_eject) {
3067 drv->bdrv_eject(bs, eject_flag);
bellard19cb3732006-08-19 11:45:59 +00003068 }
Markus Armbruster822e1cd2011-07-20 18:23:42 +02003069 bs->tray_open = eject_flag;
3070 return 0;
bellard19cb3732006-08-19 11:45:59 +00003071}
3072
3073int bdrv_is_locked(BlockDriverState *bs)
3074{
3075 return bs->locked;
3076}
3077
3078/**
3079 * Lock or unlock the media (if it is locked, the user won't be able
3080 * to eject it manually).
3081 */
3082void bdrv_set_locked(BlockDriverState *bs, int locked)
3083{
3084 BlockDriver *drv = bs->drv;
3085
Stefan Hajnoczib8c6d092011-03-29 20:04:40 +01003086 trace_bdrv_set_locked(bs, locked);
3087
bellard19cb3732006-08-19 11:45:59 +00003088 bs->locked = locked;
3089 if (drv && drv->bdrv_set_locked) {
3090 drv->bdrv_set_locked(bs, locked);
3091 }
3092}
ths985a03b2007-12-24 16:10:43 +00003093
3094/* needed for generic scsi interface */
3095
3096int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
3097{
3098 BlockDriver *drv = bs->drv;
3099
3100 if (drv && drv->bdrv_ioctl)
3101 return drv->bdrv_ioctl(bs, req, buf);
3102 return -ENOTSUP;
3103}
aliguori7d780662009-03-12 19:57:08 +00003104
aliguori221f7152009-03-28 17:28:41 +00003105BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
3106 unsigned long int req, void *buf,
3107 BlockDriverCompletionFunc *cb, void *opaque)
aliguori7d780662009-03-12 19:57:08 +00003108{
aliguori221f7152009-03-28 17:28:41 +00003109 BlockDriver *drv = bs->drv;
aliguori7d780662009-03-12 19:57:08 +00003110
aliguori221f7152009-03-28 17:28:41 +00003111 if (drv && drv->bdrv_aio_ioctl)
3112 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
3113 return NULL;
aliguori7d780662009-03-12 19:57:08 +00003114}
aliguorie268ca52009-04-22 20:20:00 +00003115
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02003116
3117
aliguorie268ca52009-04-22 20:20:00 +00003118void *qemu_blockalign(BlockDriverState *bs, size_t size)
3119{
3120 return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
3121}
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02003122
3123void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable)
3124{
3125 int64_t bitmap_size;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01003126
Liran Schouraaa0eb72010-01-26 10:31:48 +02003127 bs->dirty_count = 0;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01003128 if (enable) {
Jan Kiszkac6d22832009-11-30 18:21:20 +01003129 if (!bs->dirty_bitmap) {
3130 bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
3131 BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
3132 bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01003133
Anthony Liguori7267c092011-08-20 22:09:37 -05003134 bs->dirty_bitmap = g_malloc0(bitmap_size);
Jan Kiszkaa55eb922009-11-30 18:21:19 +01003135 }
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02003136 } else {
Jan Kiszkac6d22832009-11-30 18:21:20 +01003137 if (bs->dirty_bitmap) {
Anthony Liguori7267c092011-08-20 22:09:37 -05003138 g_free(bs->dirty_bitmap);
Jan Kiszkac6d22832009-11-30 18:21:20 +01003139 bs->dirty_bitmap = NULL;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01003140 }
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02003141 }
3142}
3143
3144int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
3145{
Jan Kiszka6ea44302009-11-30 18:21:19 +01003146 int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01003147
Jan Kiszkac6d22832009-11-30 18:21:20 +01003148 if (bs->dirty_bitmap &&
3149 (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) {
Marcelo Tosatti6d59fec2010-11-08 17:02:54 -02003150 return !!(bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] &
3151 (1UL << (chunk % (sizeof(unsigned long) * 8))));
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02003152 } else {
3153 return 0;
3154 }
3155}
3156
Jan Kiszkaa55eb922009-11-30 18:21:19 +01003157void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
3158 int nr_sectors)
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02003159{
3160 set_dirty_bitmap(bs, cur_sector, nr_sectors, 0);
3161}
Liran Schouraaa0eb72010-01-26 10:31:48 +02003162
3163int64_t bdrv_get_dirty_count(BlockDriverState *bs)
3164{
3165 return bs->dirty_count;
3166}
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003167
Marcelo Tosattidb593f22011-01-26 12:12:34 -02003168void bdrv_set_in_use(BlockDriverState *bs, int in_use)
3169{
3170 assert(bs->in_use != in_use);
3171 bs->in_use = in_use;
3172}
3173
3174int bdrv_in_use(BlockDriverState *bs)
3175{
3176 return bs->in_use;
3177}
3178
Christoph Hellwiga597e792011-08-25 08:26:01 +02003179void
3180bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, int64_t bytes,
3181 enum BlockAcctType type)
3182{
3183 assert(type < BDRV_MAX_IOTYPE);
3184
3185 cookie->bytes = bytes;
Christoph Hellwigc488c7f2011-08-25 08:26:10 +02003186 cookie->start_time_ns = get_clock();
Christoph Hellwiga597e792011-08-25 08:26:01 +02003187 cookie->type = type;
3188}
3189
3190void
3191bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie)
3192{
3193 assert(cookie->type < BDRV_MAX_IOTYPE);
3194
3195 bs->nr_bytes[cookie->type] += cookie->bytes;
3196 bs->nr_ops[cookie->type]++;
Christoph Hellwigc488c7f2011-08-25 08:26:10 +02003197 bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns;
Christoph Hellwiga597e792011-08-25 08:26:01 +02003198}
3199
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003200int bdrv_img_create(const char *filename, const char *fmt,
3201 const char *base_filename, const char *base_fmt,
3202 char *options, uint64_t img_size, int flags)
3203{
3204 QEMUOptionParameter *param = NULL, *create_options = NULL;
Kevin Wolfd2208942011-06-01 14:03:31 +02003205 QEMUOptionParameter *backing_fmt, *backing_file, *size;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003206 BlockDriverState *bs = NULL;
3207 BlockDriver *drv, *proto_drv;
Stefan Hajnoczi96df67d2011-01-24 09:32:20 +00003208 BlockDriver *backing_drv = NULL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003209 int ret = 0;
3210
3211 /* Find driver and parse its options */
3212 drv = bdrv_find_format(fmt);
3213 if (!drv) {
3214 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4f70f242010-12-16 13:52:18 +01003215 ret = -EINVAL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003216 goto out;
3217 }
3218
3219 proto_drv = bdrv_find_protocol(filename);
3220 if (!proto_drv) {
3221 error_report("Unknown protocol '%s'", filename);
Jes Sorensen4f70f242010-12-16 13:52:18 +01003222 ret = -EINVAL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003223 goto out;
3224 }
3225
3226 create_options = append_option_parameters(create_options,
3227 drv->create_options);
3228 create_options = append_option_parameters(create_options,
3229 proto_drv->create_options);
3230
3231 /* Create parameter list with default values */
3232 param = parse_option_parameters("", create_options, param);
3233
3234 set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size);
3235
3236 /* Parse -o options */
3237 if (options) {
3238 param = parse_option_parameters(options, create_options, param);
3239 if (param == NULL) {
3240 error_report("Invalid options for file format '%s'.", fmt);
Jes Sorensen4f70f242010-12-16 13:52:18 +01003241 ret = -EINVAL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003242 goto out;
3243 }
3244 }
3245
3246 if (base_filename) {
3247 if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE,
3248 base_filename)) {
3249 error_report("Backing file not supported for file format '%s'",
3250 fmt);
Jes Sorensen4f70f242010-12-16 13:52:18 +01003251 ret = -EINVAL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003252 goto out;
3253 }
3254 }
3255
3256 if (base_fmt) {
3257 if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) {
3258 error_report("Backing file format not supported for file "
3259 "format '%s'", fmt);
Jes Sorensen4f70f242010-12-16 13:52:18 +01003260 ret = -EINVAL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003261 goto out;
3262 }
3263 }
3264
Jes Sorensen792da932010-12-16 13:52:17 +01003265 backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
3266 if (backing_file && backing_file->value.s) {
3267 if (!strcmp(filename, backing_file->value.s)) {
3268 error_report("Error: Trying to create an image with the "
3269 "same filename as the backing file");
Jes Sorensen4f70f242010-12-16 13:52:18 +01003270 ret = -EINVAL;
Jes Sorensen792da932010-12-16 13:52:17 +01003271 goto out;
3272 }
3273 }
3274
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003275 backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT);
3276 if (backing_fmt && backing_fmt->value.s) {
Stefan Hajnoczi96df67d2011-01-24 09:32:20 +00003277 backing_drv = bdrv_find_format(backing_fmt->value.s);
3278 if (!backing_drv) {
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003279 error_report("Unknown backing file format '%s'",
3280 backing_fmt->value.s);
Jes Sorensen4f70f242010-12-16 13:52:18 +01003281 ret = -EINVAL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003282 goto out;
3283 }
3284 }
3285
3286 // The size for the image must always be specified, with one exception:
3287 // If we are using a backing file, we can obtain the size from there
Kevin Wolfd2208942011-06-01 14:03:31 +02003288 size = get_option_parameter(param, BLOCK_OPT_SIZE);
3289 if (size && size->value.n == -1) {
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003290 if (backing_file && backing_file->value.s) {
3291 uint64_t size;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003292 char buf[32];
3293
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003294 bs = bdrv_new("");
3295
Stefan Hajnoczi96df67d2011-01-24 09:32:20 +00003296 ret = bdrv_open(bs, backing_file->value.s, flags, backing_drv);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003297 if (ret < 0) {
Stefan Hajnoczi96df67d2011-01-24 09:32:20 +00003298 error_report("Could not open '%s'", backing_file->value.s);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003299 goto out;
3300 }
3301 bdrv_get_geometry(bs, &size);
3302 size *= 512;
3303
3304 snprintf(buf, sizeof(buf), "%" PRId64, size);
3305 set_option_parameter(param, BLOCK_OPT_SIZE, buf);
3306 } else {
3307 error_report("Image creation needs a size parameter");
Jes Sorensen4f70f242010-12-16 13:52:18 +01003308 ret = -EINVAL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003309 goto out;
3310 }
3311 }
3312
3313 printf("Formatting '%s', fmt=%s ", filename, fmt);
3314 print_option_parameters(param);
3315 puts("");
3316
3317 ret = bdrv_create(drv, filename, param);
3318
3319 if (ret < 0) {
3320 if (ret == -ENOTSUP) {
3321 error_report("Formatting or formatting option not supported for "
3322 "file format '%s'", fmt);
3323 } else if (ret == -EFBIG) {
3324 error_report("The image size is too large for file format '%s'",
3325 fmt);
3326 } else {
3327 error_report("%s: error while creating %s: %s", filename, fmt,
3328 strerror(-ret));
3329 }
3330 }
3331
3332out:
3333 free_option_parameters(create_options);
3334 free_option_parameters(param);
3335
3336 if (bs) {
3337 bdrv_delete(bs);
3338 }
Jes Sorensen4f70f242010-12-16 13:52:18 +01003339
3340 return ret;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003341}