blob: 86910b046c27e116adeb0c575a1319dc53dde276 [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 Capitulinof795e742011-10-21 16:05:43 -020030#include "qjson.h"
Kevin Wolf68485422011-06-30 10:05:46 +020031#include "qemu-coroutine.h"
Luiz Capitulinob2023812011-09-21 17:16:47 -030032#include "qmp-commands.h"
bellardfc01f7e2003-06-30 10:03:06 +000033
Juan Quintela71e72a12009-07-27 16:12:56 +020034#ifdef CONFIG_BSD
bellard7674e7b2005-04-26 21:59:26 +000035#include <sys/types.h>
36#include <sys/stat.h>
37#include <sys/ioctl.h>
Blue Swirl72cf2d42009-09-12 07:36:22 +000038#include <sys/queue.h>
blueswir1c5e97232009-03-07 20:06:23 +000039#ifndef __DragonFly__
bellard7674e7b2005-04-26 21:59:26 +000040#include <sys/disk.h>
41#endif
blueswir1c5e97232009-03-07 20:06:23 +000042#endif
bellard7674e7b2005-04-26 21:59:26 +000043
aliguori49dc7682009-03-08 16:26:59 +000044#ifdef _WIN32
45#include <windows.h>
46#endif
47
Stefan Hajnoczi1c9805a2011-10-13 13:08:22 +010048#define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
49
Markus Armbruster7d4b4ba2011-09-06 18:58:59 +020050static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load);
aliguorif141eaf2009-04-07 18:43:24 +000051static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
52 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
aliguoric87c0672009-04-07 18:43:20 +000053 BlockDriverCompletionFunc *cb, void *opaque);
aliguorif141eaf2009-04-07 18:43:24 +000054static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
55 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
pbrookce1a14d2006-08-07 02:38:06 +000056 BlockDriverCompletionFunc *cb, void *opaque);
Kevin Wolff9f05dc2011-07-15 13:50:26 +020057static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs,
58 int64_t sector_num, int nb_sectors,
59 QEMUIOVector *iov);
60static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs,
61 int64_t sector_num, int nb_sectors,
62 QEMUIOVector *iov);
Stefan Hajnoczic5fbe572011-10-05 17:17:03 +010063static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs,
64 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
Stefan Hajnoczi1c9805a2011-10-13 13:08:22 +010065static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs,
66 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
Stefan Hajnoczib2a61372011-10-13 13:08:23 +010067static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs,
68 int64_t sector_num,
69 QEMUIOVector *qiov,
70 int nb_sectors,
71 BlockDriverCompletionFunc *cb,
72 void *opaque,
Stefan Hajnoczi8c5873d2011-10-13 21:09:28 +010073 bool is_write);
Stefan Hajnoczib2a61372011-10-13 13:08:23 +010074static void coroutine_fn bdrv_co_do_rw(void *opaque);
bellardec530c82006-04-25 22:36:06 +000075
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +010076static QTAILQ_HEAD(, BlockDriverState) bdrv_states =
77 QTAILQ_HEAD_INITIALIZER(bdrv_states);
blueswir17ee930d2008-09-17 19:04:14 +000078
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +010079static QLIST_HEAD(, BlockDriver) bdrv_drivers =
80 QLIST_HEAD_INITIALIZER(bdrv_drivers);
bellardea2384d2004-08-01 21:59:26 +000081
Markus Armbrusterf9092b12010-06-25 10:33:39 +020082/* The device to use for VM snapshots */
83static BlockDriverState *bs_snapshots;
84
Markus Armbrustereb852012009-10-27 18:41:44 +010085/* If non-zero, use only whitelisted block drivers */
86static int use_bdrv_whitelist;
87
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +000088#ifdef _WIN32
89static int is_windows_drive_prefix(const char *filename)
90{
91 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
92 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
93 filename[1] == ':');
94}
95
96int is_windows_drive(const char *filename)
97{
98 if (is_windows_drive_prefix(filename) &&
99 filename[2] == '\0')
100 return 1;
101 if (strstart(filename, "\\\\.\\", NULL) ||
102 strstart(filename, "//./", NULL))
103 return 1;
104 return 0;
105}
106#endif
107
108/* check if the path starts with "<protocol>:" */
109static int path_has_protocol(const char *path)
110{
111#ifdef _WIN32
112 if (is_windows_drive(path) ||
113 is_windows_drive_prefix(path)) {
114 return 0;
115 }
116#endif
117
118 return strchr(path, ':') != NULL;
119}
120
bellard83f64092006-08-01 16:21:11 +0000121int path_is_absolute(const char *path)
122{
123 const char *p;
bellard21664422007-01-07 18:22:37 +0000124#ifdef _WIN32
125 /* specific case for names like: "\\.\d:" */
126 if (*path == '/' || *path == '\\')
127 return 1;
128#endif
bellard83f64092006-08-01 16:21:11 +0000129 p = strchr(path, ':');
130 if (p)
131 p++;
132 else
133 p = path;
bellard3b9f94e2007-01-07 17:27:07 +0000134#ifdef _WIN32
135 return (*p == '/' || *p == '\\');
136#else
137 return (*p == '/');
138#endif
bellard83f64092006-08-01 16:21:11 +0000139}
140
141/* if filename is absolute, just copy it to dest. Otherwise, build a
142 path to it by considering it is relative to base_path. URL are
143 supported. */
144void path_combine(char *dest, int dest_size,
145 const char *base_path,
146 const char *filename)
147{
148 const char *p, *p1;
149 int len;
150
151 if (dest_size <= 0)
152 return;
153 if (path_is_absolute(filename)) {
154 pstrcpy(dest, dest_size, filename);
155 } else {
156 p = strchr(base_path, ':');
157 if (p)
158 p++;
159 else
160 p = base_path;
bellard3b9f94e2007-01-07 17:27:07 +0000161 p1 = strrchr(base_path, '/');
162#ifdef _WIN32
163 {
164 const char *p2;
165 p2 = strrchr(base_path, '\\');
166 if (!p1 || p2 > p1)
167 p1 = p2;
168 }
169#endif
bellard83f64092006-08-01 16:21:11 +0000170 if (p1)
171 p1++;
172 else
173 p1 = base_path;
174 if (p1 > p)
175 p = p1;
176 len = p - base_path;
177 if (len > dest_size - 1)
178 len = dest_size - 1;
179 memcpy(dest, base_path, len);
180 dest[len] = '\0';
181 pstrcat(dest, dest_size, filename);
182 }
183}
184
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500185void bdrv_register(BlockDriver *bdrv)
bellardea2384d2004-08-01 21:59:26 +0000186{
Stefan Hajnoczi8c5873d2011-10-13 21:09:28 +0100187 /* Block drivers without coroutine functions need emulation */
188 if (!bdrv->bdrv_co_readv) {
Kevin Wolff9f05dc2011-07-15 13:50:26 +0200189 bdrv->bdrv_co_readv = bdrv_co_readv_em;
190 bdrv->bdrv_co_writev = bdrv_co_writev_em;
191
Stefan Hajnoczif8c35c12011-10-13 21:09:31 +0100192 /* bdrv_co_readv_em()/brdv_co_writev_em() work in terms of aio, so if
193 * the block driver lacks aio we need to emulate that too.
194 */
Kevin Wolff9f05dc2011-07-15 13:50:26 +0200195 if (!bdrv->bdrv_aio_readv) {
196 /* add AIO emulation layer */
197 bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
198 bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
Kevin Wolff9f05dc2011-07-15 13:50:26 +0200199 }
bellard83f64092006-08-01 16:21:11 +0000200 }
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +0200201
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100202 QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
bellardea2384d2004-08-01 21:59:26 +0000203}
bellardb3380822004-03-14 21:38:54 +0000204
205/* create a new block device (by default it is empty) */
206BlockDriverState *bdrv_new(const char *device_name)
bellardfc01f7e2003-06-30 10:03:06 +0000207{
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +0100208 BlockDriverState *bs;
bellardb3380822004-03-14 21:38:54 +0000209
Anthony Liguori7267c092011-08-20 22:09:37 -0500210 bs = g_malloc0(sizeof(BlockDriverState));
bellardb3380822004-03-14 21:38:54 +0000211 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
bellardea2384d2004-08-01 21:59:26 +0000212 if (device_name[0] != '\0') {
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +0100213 QTAILQ_INSERT_TAIL(&bdrv_states, bs, list);
bellardea2384d2004-08-01 21:59:26 +0000214 }
Luiz Capitulino28a72822011-09-26 17:43:50 -0300215 bdrv_iostatus_disable(bs);
bellardb3380822004-03-14 21:38:54 +0000216 return bs;
217}
218
bellardea2384d2004-08-01 21:59:26 +0000219BlockDriver *bdrv_find_format(const char *format_name)
220{
221 BlockDriver *drv1;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100222 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
223 if (!strcmp(drv1->format_name, format_name)) {
bellardea2384d2004-08-01 21:59:26 +0000224 return drv1;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100225 }
bellardea2384d2004-08-01 21:59:26 +0000226 }
227 return NULL;
228}
229
Markus Armbrustereb852012009-10-27 18:41:44 +0100230static int bdrv_is_whitelisted(BlockDriver *drv)
231{
232 static const char *whitelist[] = {
233 CONFIG_BDRV_WHITELIST
234 };
235 const char **p;
236
237 if (!whitelist[0])
238 return 1; /* no whitelist, anything goes */
239
240 for (p = whitelist; *p; p++) {
241 if (!strcmp(drv->format_name, *p)) {
242 return 1;
243 }
244 }
245 return 0;
246}
247
248BlockDriver *bdrv_find_whitelisted_format(const char *format_name)
249{
250 BlockDriver *drv = bdrv_find_format(format_name);
251 return drv && bdrv_is_whitelisted(drv) ? drv : NULL;
252}
253
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200254int bdrv_create(BlockDriver *drv, const char* filename,
255 QEMUOptionParameter *options)
bellardea2384d2004-08-01 21:59:26 +0000256{
257 if (!drv->bdrv_create)
258 return -ENOTSUP;
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200259
260 return drv->bdrv_create(filename, options);
bellardea2384d2004-08-01 21:59:26 +0000261}
262
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200263int bdrv_create_file(const char* filename, QEMUOptionParameter *options)
264{
265 BlockDriver *drv;
266
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900267 drv = bdrv_find_protocol(filename);
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200268 if (drv == NULL) {
Stefan Hajnoczi16905d72010-11-30 15:14:14 +0000269 return -ENOENT;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200270 }
271
272 return bdrv_create(drv, filename, options);
273}
274
bellardd5249392004-08-03 21:14:23 +0000275#ifdef _WIN32
bellard95389c82005-12-18 18:28:15 +0000276void get_tmp_filename(char *filename, int size)
bellardd5249392004-08-03 21:14:23 +0000277{
bellard3b9f94e2007-01-07 17:27:07 +0000278 char temp_dir[MAX_PATH];
ths3b46e622007-09-17 08:09:54 +0000279
bellard3b9f94e2007-01-07 17:27:07 +0000280 GetTempPath(MAX_PATH, temp_dir);
281 GetTempFileName(temp_dir, "qem", 0, filename);
bellardd5249392004-08-03 21:14:23 +0000282}
283#else
bellard95389c82005-12-18 18:28:15 +0000284void get_tmp_filename(char *filename, int size)
bellardea2384d2004-08-01 21:59:26 +0000285{
286 int fd;
blueswir17ccfb2e2008-09-14 06:45:34 +0000287 const char *tmpdir;
bellardd5249392004-08-03 21:14:23 +0000288 /* XXX: race condition possible */
aurel320badc1e2008-03-10 00:05:34 +0000289 tmpdir = getenv("TMPDIR");
290 if (!tmpdir)
291 tmpdir = "/tmp";
292 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
bellardea2384d2004-08-01 21:59:26 +0000293 fd = mkstemp(filename);
294 close(fd);
295}
bellardd5249392004-08-03 21:14:23 +0000296#endif
bellardea2384d2004-08-01 21:59:26 +0000297
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200298/*
299 * Detect host devices. By convention, /dev/cdrom[N] is always
300 * recognized as a host CDROM.
301 */
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200302static BlockDriver *find_hdev_driver(const char *filename)
303{
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200304 int score_max = 0, score;
305 BlockDriver *drv = NULL, *d;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200306
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100307 QLIST_FOREACH(d, &bdrv_drivers, list) {
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200308 if (d->bdrv_probe_device) {
309 score = d->bdrv_probe_device(filename);
310 if (score > score_max) {
311 score_max = score;
312 drv = d;
313 }
314 }
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200315 }
316
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200317 return drv;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200318}
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200319
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900320BlockDriver *bdrv_find_protocol(const char *filename)
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200321{
322 BlockDriver *drv1;
323 char protocol[128];
324 int len;
325 const char *p;
326
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200327 /* TODO Drivers without bdrv_file_open must be specified explicitly */
328
Christoph Hellwig39508e72010-06-23 12:25:17 +0200329 /*
330 * XXX(hch): we really should not let host device detection
331 * override an explicit protocol specification, but moving this
332 * later breaks access to device names with colons in them.
333 * Thanks to the brain-dead persistent naming schemes on udev-
334 * based Linux systems those actually are quite common.
335 */
336 drv1 = find_hdev_driver(filename);
337 if (drv1) {
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200338 return drv1;
339 }
Christoph Hellwig39508e72010-06-23 12:25:17 +0200340
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000341 if (!path_has_protocol(filename)) {
Christoph Hellwig39508e72010-06-23 12:25:17 +0200342 return bdrv_find_format("file");
343 }
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000344 p = strchr(filename, ':');
345 assert(p != NULL);
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200346 len = p - filename;
347 if (len > sizeof(protocol) - 1)
348 len = sizeof(protocol) - 1;
349 memcpy(protocol, filename, len);
350 protocol[len] = '\0';
351 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
352 if (drv1->protocol_name &&
353 !strcmp(drv1->protocol_name, protocol)) {
354 return drv1;
355 }
356 }
357 return NULL;
358}
359
Stefan Weilc98ac352010-07-21 21:51:51 +0200360static int find_image_format(const char *filename, BlockDriver **pdrv)
bellardea2384d2004-08-01 21:59:26 +0000361{
bellard83f64092006-08-01 16:21:11 +0000362 int ret, score, score_max;
bellardea2384d2004-08-01 21:59:26 +0000363 BlockDriver *drv1, *drv;
bellard83f64092006-08-01 16:21:11 +0000364 uint8_t buf[2048];
365 BlockDriverState *bs;
ths3b46e622007-09-17 08:09:54 +0000366
Naphtali Spreif5edb012010-01-17 16:48:13 +0200367 ret = bdrv_file_open(&bs, filename, 0);
Stefan Weilc98ac352010-07-21 21:51:51 +0200368 if (ret < 0) {
369 *pdrv = NULL;
370 return ret;
371 }
Nicholas Bellingerf8ea0b02010-05-17 09:45:57 -0700372
Kevin Wolf08a00552010-06-01 18:37:31 +0200373 /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
374 if (bs->sg || !bdrv_is_inserted(bs)) {
Nicholas A. Bellinger1a396852010-05-27 08:56:28 -0700375 bdrv_delete(bs);
Stefan Weilc98ac352010-07-21 21:51:51 +0200376 drv = bdrv_find_format("raw");
377 if (!drv) {
378 ret = -ENOENT;
379 }
380 *pdrv = drv;
381 return ret;
Nicholas A. Bellinger1a396852010-05-27 08:56:28 -0700382 }
Nicholas Bellingerf8ea0b02010-05-17 09:45:57 -0700383
bellard83f64092006-08-01 16:21:11 +0000384 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
385 bdrv_delete(bs);
386 if (ret < 0) {
Stefan Weilc98ac352010-07-21 21:51:51 +0200387 *pdrv = NULL;
388 return ret;
bellard83f64092006-08-01 16:21:11 +0000389 }
390
bellardea2384d2004-08-01 21:59:26 +0000391 score_max = 0;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200392 drv = NULL;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100393 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
bellard83f64092006-08-01 16:21:11 +0000394 if (drv1->bdrv_probe) {
395 score = drv1->bdrv_probe(buf, ret, filename);
396 if (score > score_max) {
397 score_max = score;
398 drv = drv1;
399 }
bellardea2384d2004-08-01 21:59:26 +0000400 }
401 }
Stefan Weilc98ac352010-07-21 21:51:51 +0200402 if (!drv) {
403 ret = -ENOENT;
404 }
405 *pdrv = drv;
406 return ret;
bellardea2384d2004-08-01 21:59:26 +0000407}
408
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100409/**
410 * Set the current 'total_sectors' value
411 */
412static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
413{
414 BlockDriver *drv = bs->drv;
415
Nicholas Bellinger396759a2010-05-17 09:46:04 -0700416 /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
417 if (bs->sg)
418 return 0;
419
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100420 /* query actual device if possible, otherwise just trust the hint */
421 if (drv->bdrv_getlength) {
422 int64_t length = drv->bdrv_getlength(bs);
423 if (length < 0) {
424 return length;
425 }
426 hint = length >> BDRV_SECTOR_BITS;
427 }
428
429 bs->total_sectors = hint;
430 return 0;
431}
432
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100433/**
434 * Set open flags for a given cache mode
435 *
436 * Return 0 on success, -1 if the cache mode was invalid.
437 */
438int bdrv_parse_cache_flags(const char *mode, int *flags)
439{
440 *flags &= ~BDRV_O_CACHE_MASK;
441
442 if (!strcmp(mode, "off") || !strcmp(mode, "none")) {
443 *flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
Stefan Hajnoczi92196b22011-08-04 12:26:52 +0100444 } else if (!strcmp(mode, "directsync")) {
445 *flags |= BDRV_O_NOCACHE;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100446 } else if (!strcmp(mode, "writeback")) {
447 *flags |= BDRV_O_CACHE_WB;
448 } else if (!strcmp(mode, "unsafe")) {
449 *flags |= BDRV_O_CACHE_WB;
450 *flags |= BDRV_O_NO_FLUSH;
451 } else if (!strcmp(mode, "writethrough")) {
452 /* this is the default */
453 } else {
454 return -1;
455 }
456
457 return 0;
458}
459
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200460/*
Kevin Wolf57915332010-04-14 15:24:50 +0200461 * Common part for opening disk images and files
462 */
463static int bdrv_open_common(BlockDriverState *bs, const char *filename,
464 int flags, BlockDriver *drv)
465{
466 int ret, open_flags;
467
468 assert(drv != NULL);
469
Stefan Hajnoczi28dcee12011-09-22 20:14:12 +0100470 trace_bdrv_open_common(bs, filename, flags, drv->format_name);
471
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200472 bs->file = NULL;
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100473 bs->total_sectors = 0;
Kevin Wolf57915332010-04-14 15:24:50 +0200474 bs->encrypted = 0;
475 bs->valid_key = 0;
Stefan Hajnoczi03f541b2011-10-27 10:54:28 +0100476 bs->sg = 0;
Kevin Wolf57915332010-04-14 15:24:50 +0200477 bs->open_flags = flags;
Stefan Hajnoczi03f541b2011-10-27 10:54:28 +0100478 bs->growable = 0;
Kevin Wolf57915332010-04-14 15:24:50 +0200479 bs->buffer_alignment = 512;
480
481 pstrcpy(bs->filename, sizeof(bs->filename), filename);
Stefan Hajnoczi03f541b2011-10-27 10:54:28 +0100482 bs->backing_file[0] = '\0';
Kevin Wolf57915332010-04-14 15:24:50 +0200483
484 if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {
485 return -ENOTSUP;
486 }
487
488 bs->drv = drv;
Anthony Liguori7267c092011-08-20 22:09:37 -0500489 bs->opaque = g_malloc0(drv->instance_size);
Kevin Wolf57915332010-04-14 15:24:50 +0200490
Stefan Hajnoczi03f541b2011-10-27 10:54:28 +0100491 bs->enable_write_cache = !!(flags & BDRV_O_CACHE_WB);
Kevin Wolf57915332010-04-14 15:24:50 +0200492
493 /*
494 * Clear flags that are internal to the block layer before opening the
495 * image.
496 */
497 open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
498
499 /*
Stefan Weilebabb672011-04-26 10:29:36 +0200500 * Snapshots should be writable.
Kevin Wolf57915332010-04-14 15:24:50 +0200501 */
502 if (bs->is_temporary) {
503 open_flags |= BDRV_O_RDWR;
504 }
505
Stefan Hajnoczie7c63792011-10-27 10:54:27 +0100506 bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR);
507
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200508 /* Open the image, either directly or using a protocol */
509 if (drv->bdrv_file_open) {
510 ret = drv->bdrv_file_open(bs, filename, open_flags);
511 } else {
512 ret = bdrv_file_open(&bs->file, filename, open_flags);
513 if (ret >= 0) {
514 ret = drv->bdrv_open(bs, open_flags);
515 }
516 }
517
Kevin Wolf57915332010-04-14 15:24:50 +0200518 if (ret < 0) {
519 goto free_and_fail;
520 }
521
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100522 ret = refresh_total_sectors(bs, bs->total_sectors);
523 if (ret < 0) {
524 goto free_and_fail;
Kevin Wolf57915332010-04-14 15:24:50 +0200525 }
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100526
Kevin Wolf57915332010-04-14 15:24:50 +0200527#ifndef _WIN32
528 if (bs->is_temporary) {
529 unlink(filename);
530 }
531#endif
532 return 0;
533
534free_and_fail:
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200535 if (bs->file) {
536 bdrv_delete(bs->file);
537 bs->file = NULL;
538 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500539 g_free(bs->opaque);
Kevin Wolf57915332010-04-14 15:24:50 +0200540 bs->opaque = NULL;
541 bs->drv = NULL;
542 return ret;
543}
544
545/*
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200546 * Opens a file using a protocol (file, host_device, nbd, ...)
547 */
bellard83f64092006-08-01 16:21:11 +0000548int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
bellardb3380822004-03-14 21:38:54 +0000549{
bellard83f64092006-08-01 16:21:11 +0000550 BlockDriverState *bs;
Christoph Hellwig6db95602010-04-05 16:53:57 +0200551 BlockDriver *drv;
bellard83f64092006-08-01 16:21:11 +0000552 int ret;
553
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900554 drv = bdrv_find_protocol(filename);
Christoph Hellwig6db95602010-04-05 16:53:57 +0200555 if (!drv) {
556 return -ENOENT;
557 }
558
bellard83f64092006-08-01 16:21:11 +0000559 bs = bdrv_new("");
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200560 ret = bdrv_open_common(bs, filename, flags, drv);
bellard83f64092006-08-01 16:21:11 +0000561 if (ret < 0) {
562 bdrv_delete(bs);
563 return ret;
bellard3b0d4f62005-10-30 18:30:10 +0000564 }
aliguori71d07702009-03-03 17:37:16 +0000565 bs->growable = 1;
bellard83f64092006-08-01 16:21:11 +0000566 *pbs = bs;
567 return 0;
bellardea2384d2004-08-01 21:59:26 +0000568}
bellardfc01f7e2003-06-30 10:03:06 +0000569
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200570/*
571 * Opens a disk image (raw, qcow2, vmdk, ...)
572 */
Kevin Wolfd6e90982010-03-31 14:40:27 +0200573int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
574 BlockDriver *drv)
bellardea2384d2004-08-01 21:59:26 +0000575{
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200576 int ret;
Kevin Wolf2b572812011-10-26 11:03:01 +0200577 char tmp_filename[PATH_MAX];
bellard712e7872005-04-28 21:09:32 +0000578
bellard83f64092006-08-01 16:21:11 +0000579 if (flags & BDRV_O_SNAPSHOT) {
bellardea2384d2004-08-01 21:59:26 +0000580 BlockDriverState *bs1;
581 int64_t total_size;
aliguori7c96d462008-09-12 17:54:13 +0000582 int is_protocol = 0;
Kevin Wolf91a073a2009-05-27 14:48:06 +0200583 BlockDriver *bdrv_qcow2;
584 QEMUOptionParameter *options;
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200585 char backing_filename[PATH_MAX];
ths3b46e622007-09-17 08:09:54 +0000586
bellardea2384d2004-08-01 21:59:26 +0000587 /* if snapshot, we create a temporary backing file and open it
588 instead of opening 'filename' directly */
589
590 /* if there is a backing file, use it */
591 bs1 = bdrv_new("");
Kevin Wolfd6e90982010-03-31 14:40:27 +0200592 ret = bdrv_open(bs1, filename, 0, drv);
aliguori51d7c002009-03-05 23:00:29 +0000593 if (ret < 0) {
bellardea2384d2004-08-01 21:59:26 +0000594 bdrv_delete(bs1);
aliguori51d7c002009-03-05 23:00:29 +0000595 return ret;
bellardea2384d2004-08-01 21:59:26 +0000596 }
Jes Sorensen3e829902010-05-27 16:20:30 +0200597 total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK;
aliguori7c96d462008-09-12 17:54:13 +0000598
599 if (bs1->drv && bs1->drv->protocol_name)
600 is_protocol = 1;
601
bellardea2384d2004-08-01 21:59:26 +0000602 bdrv_delete(bs1);
ths3b46e622007-09-17 08:09:54 +0000603
bellardea2384d2004-08-01 21:59:26 +0000604 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
aliguori7c96d462008-09-12 17:54:13 +0000605
606 /* Real path is meaningless for protocols */
607 if (is_protocol)
608 snprintf(backing_filename, sizeof(backing_filename),
609 "%s", filename);
Kirill A. Shutemov114cdfa2009-12-25 18:19:22 +0000610 else if (!realpath(filename, backing_filename))
611 return -errno;
aliguori7c96d462008-09-12 17:54:13 +0000612
Kevin Wolf91a073a2009-05-27 14:48:06 +0200613 bdrv_qcow2 = bdrv_find_format("qcow2");
614 options = parse_option_parameters("", bdrv_qcow2->create_options, NULL);
615
Jes Sorensen3e829902010-05-27 16:20:30 +0200616 set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size);
Kevin Wolf91a073a2009-05-27 14:48:06 +0200617 set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename);
618 if (drv) {
619 set_option_parameter(options, BLOCK_OPT_BACKING_FMT,
620 drv->format_name);
621 }
622
623 ret = bdrv_create(bdrv_qcow2, tmp_filename, options);
Jan Kiszkad7487682010-04-29 18:24:50 +0200624 free_option_parameters(options);
aliguori51d7c002009-03-05 23:00:29 +0000625 if (ret < 0) {
626 return ret;
bellardea2384d2004-08-01 21:59:26 +0000627 }
Kevin Wolf91a073a2009-05-27 14:48:06 +0200628
bellardea2384d2004-08-01 21:59:26 +0000629 filename = tmp_filename;
Kevin Wolf91a073a2009-05-27 14:48:06 +0200630 drv = bdrv_qcow2;
bellardea2384d2004-08-01 21:59:26 +0000631 bs->is_temporary = 1;
632 }
bellard712e7872005-04-28 21:09:32 +0000633
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200634 /* Find the right image format driver */
Christoph Hellwig6db95602010-04-05 16:53:57 +0200635 if (!drv) {
Stefan Weilc98ac352010-07-21 21:51:51 +0200636 ret = find_image_format(filename, &drv);
aliguori51d7c002009-03-05 23:00:29 +0000637 }
Christoph Hellwig69873072010-01-20 18:13:25 +0100638
aliguori51d7c002009-03-05 23:00:29 +0000639 if (!drv) {
aliguori51d7c002009-03-05 23:00:29 +0000640 goto unlink_and_fail;
bellardea2384d2004-08-01 21:59:26 +0000641 }
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200642
643 /* Open the image */
644 ret = bdrv_open_common(bs, filename, flags, drv);
645 if (ret < 0) {
Christoph Hellwig69873072010-01-20 18:13:25 +0100646 goto unlink_and_fail;
647 }
648
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200649 /* If there is a backing file, use it */
650 if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') {
651 char backing_filename[PATH_MAX];
652 int back_flags;
653 BlockDriver *back_drv = NULL;
654
655 bs->backing_hd = bdrv_new("");
Stefan Hajnoczidf2dbb42010-12-02 16:54:13 +0000656
657 if (path_has_protocol(bs->backing_file)) {
658 pstrcpy(backing_filename, sizeof(backing_filename),
659 bs->backing_file);
660 } else {
661 path_combine(backing_filename, sizeof(backing_filename),
662 filename, bs->backing_file);
663 }
664
665 if (bs->backing_format[0] != '\0') {
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200666 back_drv = bdrv_find_format(bs->backing_format);
Stefan Hajnoczidf2dbb42010-12-02 16:54:13 +0000667 }
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200668
669 /* backing files always opened read-only */
670 back_flags =
671 flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
672
673 ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv);
674 if (ret < 0) {
675 bdrv_close(bs);
676 return ret;
677 }
678 if (bs->is_temporary) {
679 bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR);
680 } else {
681 /* base image inherits from "parent" */
682 bs->backing_hd->keep_read_only = bs->keep_read_only;
683 }
684 }
685
686 if (!bdrv_key_required(bs)) {
Markus Armbruster7d4b4ba2011-09-06 18:58:59 +0200687 bdrv_dev_change_media_cb(bs, true);
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200688 }
689
690 return 0;
691
692unlink_and_fail:
693 if (bs->is_temporary) {
694 unlink(filename);
695 }
696 return ret;
697}
698
bellardfc01f7e2003-06-30 10:03:06 +0000699void bdrv_close(BlockDriverState *bs)
700{
bellard19cb3732006-08-19 11:45:59 +0000701 if (bs->drv) {
Markus Armbrusterf9092b12010-06-25 10:33:39 +0200702 if (bs == bs_snapshots) {
703 bs_snapshots = NULL;
704 }
Stefan Hajnoczi557df6a2010-04-17 10:49:06 +0100705 if (bs->backing_hd) {
bellardea2384d2004-08-01 21:59:26 +0000706 bdrv_delete(bs->backing_hd);
Stefan Hajnoczi557df6a2010-04-17 10:49:06 +0100707 bs->backing_hd = NULL;
708 }
bellardea2384d2004-08-01 21:59:26 +0000709 bs->drv->bdrv_close(bs);
Anthony Liguori7267c092011-08-20 22:09:37 -0500710 g_free(bs->opaque);
bellardea2384d2004-08-01 21:59:26 +0000711#ifdef _WIN32
712 if (bs->is_temporary) {
713 unlink(bs->filename);
714 }
bellard67b915a2004-03-31 23:37:16 +0000715#endif
bellardea2384d2004-08-01 21:59:26 +0000716 bs->opaque = NULL;
717 bs->drv = NULL;
bellardb3380822004-03-14 21:38:54 +0000718
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200719 if (bs->file != NULL) {
720 bdrv_close(bs->file);
721 }
722
Markus Armbruster7d4b4ba2011-09-06 18:58:59 +0200723 bdrv_dev_change_media_cb(bs, false);
bellardb3380822004-03-14 21:38:54 +0000724 }
725}
726
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +0900727void bdrv_close_all(void)
728{
729 BlockDriverState *bs;
730
731 QTAILQ_FOREACH(bs, &bdrv_states, list) {
732 bdrv_close(bs);
733 }
734}
735
Ryan Harperd22b2f42011-03-29 20:51:47 -0500736/* make a BlockDriverState anonymous by removing from bdrv_state list.
737 Also, NULL terminate the device_name to prevent double remove */
738void bdrv_make_anon(BlockDriverState *bs)
739{
740 if (bs->device_name[0] != '\0') {
741 QTAILQ_REMOVE(&bdrv_states, bs, list);
742 }
743 bs->device_name[0] = '\0';
744}
745
bellardb3380822004-03-14 21:38:54 +0000746void bdrv_delete(BlockDriverState *bs)
747{
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200748 assert(!bs->dev);
Markus Armbruster18846de2010-06-29 16:58:30 +0200749
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +0100750 /* remove from list, if necessary */
Ryan Harperd22b2f42011-03-29 20:51:47 -0500751 bdrv_make_anon(bs);
aurel3234c6f052008-04-08 19:51:21 +0000752
bellardb3380822004-03-14 21:38:54 +0000753 bdrv_close(bs);
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200754 if (bs->file != NULL) {
755 bdrv_delete(bs->file);
756 }
757
Markus Armbrusterf9092b12010-06-25 10:33:39 +0200758 assert(bs != bs_snapshots);
Anthony Liguori7267c092011-08-20 22:09:37 -0500759 g_free(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000760}
761
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200762int bdrv_attach_dev(BlockDriverState *bs, void *dev)
763/* TODO change to DeviceState *dev when all users are qdevified */
Markus Armbruster18846de2010-06-29 16:58:30 +0200764{
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200765 if (bs->dev) {
Markus Armbruster18846de2010-06-29 16:58:30 +0200766 return -EBUSY;
767 }
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200768 bs->dev = dev;
Luiz Capitulino28a72822011-09-26 17:43:50 -0300769 bdrv_iostatus_reset(bs);
Markus Armbruster18846de2010-06-29 16:58:30 +0200770 return 0;
771}
772
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200773/* TODO qdevified devices don't use this, remove when devices are qdevified */
774void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev)
Markus Armbruster18846de2010-06-29 16:58:30 +0200775{
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200776 if (bdrv_attach_dev(bs, dev) < 0) {
777 abort();
778 }
779}
780
781void bdrv_detach_dev(BlockDriverState *bs, void *dev)
782/* TODO change to DeviceState *dev when all users are qdevified */
783{
784 assert(bs->dev == dev);
785 bs->dev = NULL;
Markus Armbruster0e49de52011-08-03 15:07:41 +0200786 bs->dev_ops = NULL;
787 bs->dev_opaque = NULL;
Markus Armbruster29e05f22011-09-06 18:58:57 +0200788 bs->buffer_alignment = 512;
Markus Armbruster18846de2010-06-29 16:58:30 +0200789}
790
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200791/* TODO change to return DeviceState * when all users are qdevified */
792void *bdrv_get_attached_dev(BlockDriverState *bs)
Markus Armbruster18846de2010-06-29 16:58:30 +0200793{
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200794 return bs->dev;
Markus Armbruster18846de2010-06-29 16:58:30 +0200795}
796
Markus Armbruster0e49de52011-08-03 15:07:41 +0200797void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops,
798 void *opaque)
799{
800 bs->dev_ops = ops;
801 bs->dev_opaque = opaque;
Markus Armbruster2c6942f2011-09-06 18:58:51 +0200802 if (bdrv_dev_has_removable_media(bs) && bs == bs_snapshots) {
803 bs_snapshots = NULL;
804 }
Markus Armbruster0e49de52011-08-03 15:07:41 +0200805}
806
Markus Armbruster7d4b4ba2011-09-06 18:58:59 +0200807static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load)
Markus Armbruster0e49de52011-08-03 15:07:41 +0200808{
Markus Armbruster145feb12011-08-03 15:07:42 +0200809 if (bs->dev_ops && bs->dev_ops->change_media_cb) {
Markus Armbruster7d4b4ba2011-09-06 18:58:59 +0200810 bs->dev_ops->change_media_cb(bs->dev_opaque, load);
Markus Armbruster145feb12011-08-03 15:07:42 +0200811 }
812}
813
Markus Armbruster2c6942f2011-09-06 18:58:51 +0200814bool bdrv_dev_has_removable_media(BlockDriverState *bs)
815{
816 return !bs->dev || (bs->dev_ops && bs->dev_ops->change_media_cb);
817}
818
Paolo Bonzini025ccaa2011-11-07 17:50:13 +0100819void bdrv_dev_eject_request(BlockDriverState *bs, bool force)
820{
821 if (bs->dev_ops && bs->dev_ops->eject_request_cb) {
822 bs->dev_ops->eject_request_cb(bs->dev_opaque, force);
823 }
824}
825
Markus Armbrustere4def802011-09-06 18:58:53 +0200826bool bdrv_dev_is_tray_open(BlockDriverState *bs)
827{
828 if (bs->dev_ops && bs->dev_ops->is_tray_open) {
829 return bs->dev_ops->is_tray_open(bs->dev_opaque);
830 }
831 return false;
832}
833
Markus Armbruster145feb12011-08-03 15:07:42 +0200834static void bdrv_dev_resize_cb(BlockDriverState *bs)
835{
836 if (bs->dev_ops && bs->dev_ops->resize_cb) {
837 bs->dev_ops->resize_cb(bs->dev_opaque);
Markus Armbruster0e49de52011-08-03 15:07:41 +0200838 }
839}
840
Markus Armbrusterf1076392011-09-06 18:58:46 +0200841bool bdrv_dev_is_medium_locked(BlockDriverState *bs)
842{
843 if (bs->dev_ops && bs->dev_ops->is_medium_locked) {
844 return bs->dev_ops->is_medium_locked(bs->dev_opaque);
845 }
846 return false;
847}
848
aliguorie97fc192009-04-21 23:11:50 +0000849/*
850 * Run consistency checks on an image
851 *
Kevin Wolfe076f332010-06-29 11:43:13 +0200852 * Returns 0 if the check could be completed (it doesn't mean that the image is
Stefan Weila1c72732011-04-28 17:20:38 +0200853 * free of errors) or -errno when an internal error occurred. The results of the
Kevin Wolfe076f332010-06-29 11:43:13 +0200854 * check are stored in res.
aliguorie97fc192009-04-21 23:11:50 +0000855 */
Kevin Wolfe076f332010-06-29 11:43:13 +0200856int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res)
aliguorie97fc192009-04-21 23:11:50 +0000857{
858 if (bs->drv->bdrv_check == NULL) {
859 return -ENOTSUP;
860 }
861
Kevin Wolfe076f332010-06-29 11:43:13 +0200862 memset(res, 0, sizeof(*res));
Kevin Wolf9ac228e2010-06-29 12:37:54 +0200863 return bs->drv->bdrv_check(bs, res);
aliguorie97fc192009-04-21 23:11:50 +0000864}
865
Kevin Wolf8a426612010-07-16 17:17:01 +0200866#define COMMIT_BUF_SECTORS 2048
867
bellard33e39632003-07-06 17:15:21 +0000868/* commit COW file into the raw image */
869int bdrv_commit(BlockDriverState *bs)
870{
bellard19cb3732006-08-19 11:45:59 +0000871 BlockDriver *drv = bs->drv;
Kevin Wolfee181192010-08-05 13:05:22 +0200872 BlockDriver *backing_drv;
Kevin Wolf8a426612010-07-16 17:17:01 +0200873 int64_t sector, total_sectors;
874 int n, ro, open_flags;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200875 int ret = 0, rw_ret = 0;
Kevin Wolf8a426612010-07-16 17:17:01 +0200876 uint8_t *buf;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200877 char filename[1024];
878 BlockDriverState *bs_rw, *bs_ro;
bellard33e39632003-07-06 17:15:21 +0000879
bellard19cb3732006-08-19 11:45:59 +0000880 if (!drv)
881 return -ENOMEDIUM;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200882
883 if (!bs->backing_hd) {
884 return -ENOTSUP;
bellard33e39632003-07-06 17:15:21 +0000885 }
886
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200887 if (bs->backing_hd->keep_read_only) {
888 return -EACCES;
889 }
Kevin Wolfee181192010-08-05 13:05:22 +0200890
891 backing_drv = bs->backing_hd->drv;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200892 ro = bs->backing_hd->read_only;
893 strncpy(filename, bs->backing_hd->filename, sizeof(filename));
894 open_flags = bs->backing_hd->open_flags;
895
896 if (ro) {
897 /* re-open as RW */
898 bdrv_delete(bs->backing_hd);
899 bs->backing_hd = NULL;
900 bs_rw = bdrv_new("");
Kevin Wolfee181192010-08-05 13:05:22 +0200901 rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR,
902 backing_drv);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200903 if (rw_ret < 0) {
904 bdrv_delete(bs_rw);
905 /* try to re-open read-only */
906 bs_ro = bdrv_new("");
Kevin Wolfee181192010-08-05 13:05:22 +0200907 ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR,
908 backing_drv);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200909 if (ret < 0) {
910 bdrv_delete(bs_ro);
911 /* drive not functional anymore */
912 bs->drv = NULL;
913 return ret;
914 }
915 bs->backing_hd = bs_ro;
916 return rw_ret;
917 }
918 bs->backing_hd = bs_rw;
bellard33e39632003-07-06 17:15:21 +0000919 }
bellardea2384d2004-08-01 21:59:26 +0000920
Jan Kiszka6ea44302009-11-30 18:21:19 +0100921 total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
Anthony Liguori7267c092011-08-20 22:09:37 -0500922 buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE);
bellardea2384d2004-08-01 21:59:26 +0000923
Kevin Wolf8a426612010-07-16 17:17:01 +0200924 for (sector = 0; sector < total_sectors; sector += n) {
925 if (drv->bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) {
926
927 if (bdrv_read(bs, sector, buf, n) != 0) {
928 ret = -EIO;
929 goto ro_cleanup;
930 }
931
932 if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) {
933 ret = -EIO;
934 goto ro_cleanup;
935 }
bellardea2384d2004-08-01 21:59:26 +0000936 }
937 }
bellard95389c82005-12-18 18:28:15 +0000938
Christoph Hellwig1d449522010-01-17 12:32:30 +0100939 if (drv->bdrv_make_empty) {
940 ret = drv->bdrv_make_empty(bs);
941 bdrv_flush(bs);
942 }
bellard95389c82005-12-18 18:28:15 +0000943
Christoph Hellwig3f5075a2010-01-12 13:49:23 +0100944 /*
945 * Make sure all data we wrote to the backing device is actually
946 * stable on disk.
947 */
948 if (bs->backing_hd)
949 bdrv_flush(bs->backing_hd);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200950
951ro_cleanup:
Anthony Liguori7267c092011-08-20 22:09:37 -0500952 g_free(buf);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200953
954 if (ro) {
955 /* re-open as RO */
956 bdrv_delete(bs->backing_hd);
957 bs->backing_hd = NULL;
958 bs_ro = bdrv_new("");
Kevin Wolfee181192010-08-05 13:05:22 +0200959 ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR,
960 backing_drv);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200961 if (ret < 0) {
962 bdrv_delete(bs_ro);
963 /* drive not functional anymore */
964 bs->drv = NULL;
965 return ret;
966 }
967 bs->backing_hd = bs_ro;
968 bs->backing_hd->keep_read_only = 0;
969 }
970
Christoph Hellwig1d449522010-01-17 12:32:30 +0100971 return ret;
bellard33e39632003-07-06 17:15:21 +0000972}
973
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200974void bdrv_commit_all(void)
975{
976 BlockDriverState *bs;
977
978 QTAILQ_FOREACH(bs, &bdrv_states, list) {
979 bdrv_commit(bs);
980 }
981}
982
Kevin Wolf756e6732010-01-12 12:55:17 +0100983/*
984 * Return values:
985 * 0 - success
986 * -EINVAL - backing format specified, but no file
987 * -ENOSPC - can't update the backing file because no space is left in the
988 * image file header
989 * -ENOTSUP - format driver doesn't support changing the backing file
990 */
991int bdrv_change_backing_file(BlockDriverState *bs,
992 const char *backing_file, const char *backing_fmt)
993{
994 BlockDriver *drv = bs->drv;
995
996 if (drv->bdrv_change_backing_file != NULL) {
997 return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
998 } else {
999 return -ENOTSUP;
1000 }
1001}
1002
aliguori71d07702009-03-03 17:37:16 +00001003static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
1004 size_t size)
1005{
1006 int64_t len;
1007
1008 if (!bdrv_is_inserted(bs))
1009 return -ENOMEDIUM;
1010
1011 if (bs->growable)
1012 return 0;
1013
1014 len = bdrv_getlength(bs);
1015
Kevin Wolffbb7b4e2009-05-08 14:47:24 +02001016 if (offset < 0)
1017 return -EIO;
1018
1019 if ((offset > len) || (len - offset < size))
aliguori71d07702009-03-03 17:37:16 +00001020 return -EIO;
1021
1022 return 0;
1023}
1024
1025static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
1026 int nb_sectors)
1027{
Jes Sorenseneb5a3162010-05-27 16:20:31 +02001028 return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE,
1029 nb_sectors * BDRV_SECTOR_SIZE);
aliguori71d07702009-03-03 17:37:16 +00001030}
1031
Stefan Hajnoczi1c9805a2011-10-13 13:08:22 +01001032typedef struct RwCo {
1033 BlockDriverState *bs;
1034 int64_t sector_num;
1035 int nb_sectors;
1036 QEMUIOVector *qiov;
1037 bool is_write;
1038 int ret;
1039} RwCo;
1040
1041static void coroutine_fn bdrv_rw_co_entry(void *opaque)
1042{
1043 RwCo *rwco = opaque;
1044
1045 if (!rwco->is_write) {
1046 rwco->ret = bdrv_co_do_readv(rwco->bs, rwco->sector_num,
1047 rwco->nb_sectors, rwco->qiov);
1048 } else {
1049 rwco->ret = bdrv_co_do_writev(rwco->bs, rwco->sector_num,
1050 rwco->nb_sectors, rwco->qiov);
1051 }
1052}
1053
1054/*
1055 * Process a synchronous request using coroutines
1056 */
1057static int bdrv_rw_co(BlockDriverState *bs, int64_t sector_num, uint8_t *buf,
1058 int nb_sectors, bool is_write)
1059{
1060 QEMUIOVector qiov;
1061 struct iovec iov = {
1062 .iov_base = (void *)buf,
1063 .iov_len = nb_sectors * BDRV_SECTOR_SIZE,
1064 };
1065 Coroutine *co;
1066 RwCo rwco = {
1067 .bs = bs,
1068 .sector_num = sector_num,
1069 .nb_sectors = nb_sectors,
1070 .qiov = &qiov,
1071 .is_write = is_write,
1072 .ret = NOT_DONE,
1073 };
1074
1075 qemu_iovec_init_external(&qiov, &iov, 1);
1076
1077 if (qemu_in_coroutine()) {
1078 /* Fast-path if already in coroutine context */
1079 bdrv_rw_co_entry(&rwco);
1080 } else {
1081 co = qemu_coroutine_create(bdrv_rw_co_entry);
1082 qemu_coroutine_enter(co, &rwco);
1083 while (rwco.ret == NOT_DONE) {
1084 qemu_aio_wait();
1085 }
1086 }
1087 return rwco.ret;
1088}
1089
bellard19cb3732006-08-19 11:45:59 +00001090/* return < 0 if error. See bdrv_write() for the return codes */
ths5fafdf22007-09-16 21:08:06 +00001091int bdrv_read(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +00001092 uint8_t *buf, int nb_sectors)
1093{
Stefan Hajnoczi1c9805a2011-10-13 13:08:22 +01001094 return bdrv_rw_co(bs, sector_num, buf, nb_sectors, false);
bellardfc01f7e2003-06-30 10:03:06 +00001095}
1096
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001097static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num,
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001098 int nb_sectors, int dirty)
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001099{
1100 int64_t start, end;
Jan Kiszkac6d22832009-11-30 18:21:20 +01001101 unsigned long val, idx, bit;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001102
Jan Kiszka6ea44302009-11-30 18:21:19 +01001103 start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkac6d22832009-11-30 18:21:20 +01001104 end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001105
1106 for (; start <= end; start++) {
Jan Kiszkac6d22832009-11-30 18:21:20 +01001107 idx = start / (sizeof(unsigned long) * 8);
1108 bit = start % (sizeof(unsigned long) * 8);
1109 val = bs->dirty_bitmap[idx];
1110 if (dirty) {
Marcelo Tosatti6d59fec2010-11-08 17:02:54 -02001111 if (!(val & (1UL << bit))) {
Liran Schouraaa0eb72010-01-26 10:31:48 +02001112 bs->dirty_count++;
Marcelo Tosatti6d59fec2010-11-08 17:02:54 -02001113 val |= 1UL << bit;
Liran Schouraaa0eb72010-01-26 10:31:48 +02001114 }
Jan Kiszkac6d22832009-11-30 18:21:20 +01001115 } else {
Marcelo Tosatti6d59fec2010-11-08 17:02:54 -02001116 if (val & (1UL << bit)) {
Liran Schouraaa0eb72010-01-26 10:31:48 +02001117 bs->dirty_count--;
Marcelo Tosatti6d59fec2010-11-08 17:02:54 -02001118 val &= ~(1UL << bit);
Liran Schouraaa0eb72010-01-26 10:31:48 +02001119 }
Jan Kiszkac6d22832009-11-30 18:21:20 +01001120 }
1121 bs->dirty_bitmap[idx] = val;
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001122 }
1123}
1124
ths5fafdf22007-09-16 21:08:06 +00001125/* Return < 0 if error. Important errors are:
bellard19cb3732006-08-19 11:45:59 +00001126 -EIO generic I/O error (may happen for all errors)
1127 -ENOMEDIUM No media inserted.
1128 -EINVAL Invalid sector number or nb_sectors
1129 -EACCES Trying to write a read-only device
1130*/
ths5fafdf22007-09-16 21:08:06 +00001131int bdrv_write(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +00001132 const uint8_t *buf, int nb_sectors)
1133{
Stefan Hajnoczi1c9805a2011-10-13 13:08:22 +01001134 return bdrv_rw_co(bs, sector_num, (uint8_t *)buf, nb_sectors, true);
bellard83f64092006-08-01 16:21:11 +00001135}
1136
aliguorieda578e2009-03-12 19:57:16 +00001137int bdrv_pread(BlockDriverState *bs, int64_t offset,
1138 void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +00001139{
Jan Kiszka6ea44302009-11-30 18:21:19 +01001140 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
bellard83f64092006-08-01 16:21:11 +00001141 int len, nb_sectors, count;
1142 int64_t sector_num;
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001143 int ret;
bellard83f64092006-08-01 16:21:11 +00001144
1145 count = count1;
1146 /* first read to align to sector start */
Jan Kiszka6ea44302009-11-30 18:21:19 +01001147 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
bellard83f64092006-08-01 16:21:11 +00001148 if (len > count)
1149 len = count;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001150 sector_num = offset >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001151 if (len > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001152 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1153 return ret;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001154 memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
bellard83f64092006-08-01 16:21:11 +00001155 count -= len;
1156 if (count == 0)
1157 return count1;
1158 sector_num++;
1159 buf += len;
1160 }
1161
1162 /* read the sectors "in place" */
Jan Kiszka6ea44302009-11-30 18:21:19 +01001163 nb_sectors = count >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001164 if (nb_sectors > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001165 if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0)
1166 return ret;
bellard83f64092006-08-01 16:21:11 +00001167 sector_num += nb_sectors;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001168 len = nb_sectors << BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001169 buf += len;
1170 count -= len;
1171 }
1172
1173 /* add data from the last sector */
1174 if (count > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001175 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1176 return ret;
bellard83f64092006-08-01 16:21:11 +00001177 memcpy(buf, tmp_buf, count);
1178 }
1179 return count1;
1180}
1181
aliguorieda578e2009-03-12 19:57:16 +00001182int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
1183 const void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +00001184{
Jan Kiszka6ea44302009-11-30 18:21:19 +01001185 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
bellard83f64092006-08-01 16:21:11 +00001186 int len, nb_sectors, count;
1187 int64_t sector_num;
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001188 int ret;
bellard83f64092006-08-01 16:21:11 +00001189
1190 count = count1;
1191 /* first write to align to sector start */
Jan Kiszka6ea44302009-11-30 18:21:19 +01001192 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
bellard83f64092006-08-01 16:21:11 +00001193 if (len > count)
1194 len = count;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001195 sector_num = offset >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001196 if (len > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001197 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1198 return ret;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001199 memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len);
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001200 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
1201 return ret;
bellard83f64092006-08-01 16:21:11 +00001202 count -= len;
1203 if (count == 0)
1204 return count1;
1205 sector_num++;
1206 buf += len;
1207 }
1208
1209 /* write the sectors "in place" */
Jan Kiszka6ea44302009-11-30 18:21:19 +01001210 nb_sectors = count >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001211 if (nb_sectors > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001212 if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0)
1213 return ret;
bellard83f64092006-08-01 16:21:11 +00001214 sector_num += nb_sectors;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001215 len = nb_sectors << BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001216 buf += len;
1217 count -= len;
1218 }
1219
1220 /* add data from the last sector */
1221 if (count > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001222 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1223 return ret;
bellard83f64092006-08-01 16:21:11 +00001224 memcpy(tmp_buf, buf, count);
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001225 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
1226 return ret;
bellard83f64092006-08-01 16:21:11 +00001227 }
1228 return count1;
1229}
bellard83f64092006-08-01 16:21:11 +00001230
Kevin Wolff08145f2010-06-16 16:38:15 +02001231/*
1232 * Writes to the file and ensures that no writes are reordered across this
1233 * request (acts as a barrier)
1234 *
1235 * Returns 0 on success, -errno in error cases.
1236 */
1237int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset,
1238 const void *buf, int count)
1239{
1240 int ret;
1241
1242 ret = bdrv_pwrite(bs, offset, buf, count);
1243 if (ret < 0) {
1244 return ret;
1245 }
1246
Stefan Hajnoczi92196b22011-08-04 12:26:52 +01001247 /* No flush needed for cache modes that use O_DSYNC */
1248 if ((bs->open_flags & BDRV_O_CACHE_WB) != 0) {
Kevin Wolff08145f2010-06-16 16:38:15 +02001249 bdrv_flush(bs);
1250 }
1251
1252 return 0;
1253}
1254
Stefan Hajnoczic5fbe572011-10-05 17:17:03 +01001255/*
1256 * Handle a read request in coroutine context
1257 */
1258static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs,
1259 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
Kevin Wolfda1fa912011-07-14 17:27:13 +02001260{
1261 BlockDriver *drv = bs->drv;
1262
Kevin Wolfda1fa912011-07-14 17:27:13 +02001263 if (!drv) {
1264 return -ENOMEDIUM;
1265 }
1266 if (bdrv_check_request(bs, sector_num, nb_sectors)) {
1267 return -EIO;
1268 }
1269
1270 return drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov);
1271}
1272
Stefan Hajnoczic5fbe572011-10-05 17:17:03 +01001273int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num,
Kevin Wolfda1fa912011-07-14 17:27:13 +02001274 int nb_sectors, QEMUIOVector *qiov)
1275{
Stefan Hajnoczic5fbe572011-10-05 17:17:03 +01001276 trace_bdrv_co_readv(bs, sector_num, nb_sectors);
Kevin Wolfda1fa912011-07-14 17:27:13 +02001277
Stefan Hajnoczic5fbe572011-10-05 17:17:03 +01001278 return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov);
1279}
1280
1281/*
1282 * Handle a write request in coroutine context
1283 */
1284static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs,
1285 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
1286{
1287 BlockDriver *drv = bs->drv;
Stefan Hajnoczi6b7cb242011-10-13 13:08:24 +01001288 int ret;
Kevin Wolfda1fa912011-07-14 17:27:13 +02001289
1290 if (!bs->drv) {
1291 return -ENOMEDIUM;
1292 }
1293 if (bs->read_only) {
1294 return -EACCES;
1295 }
1296 if (bdrv_check_request(bs, sector_num, nb_sectors)) {
1297 return -EIO;
1298 }
1299
Stefan Hajnoczi6b7cb242011-10-13 13:08:24 +01001300 ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov);
1301
Kevin Wolfda1fa912011-07-14 17:27:13 +02001302 if (bs->dirty_bitmap) {
1303 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1304 }
1305
1306 if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
1307 bs->wr_highest_sector = sector_num + nb_sectors - 1;
1308 }
1309
Stefan Hajnoczi6b7cb242011-10-13 13:08:24 +01001310 return ret;
Kevin Wolfda1fa912011-07-14 17:27:13 +02001311}
1312
Stefan Hajnoczic5fbe572011-10-05 17:17:03 +01001313int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num,
1314 int nb_sectors, QEMUIOVector *qiov)
1315{
1316 trace_bdrv_co_writev(bs, sector_num, nb_sectors);
1317
1318 return bdrv_co_do_writev(bs, sector_num, nb_sectors, qiov);
1319}
1320
bellard83f64092006-08-01 16:21:11 +00001321/**
bellard83f64092006-08-01 16:21:11 +00001322 * Truncate file to 'offset' bytes (needed only for file protocols)
1323 */
1324int bdrv_truncate(BlockDriverState *bs, int64_t offset)
1325{
1326 BlockDriver *drv = bs->drv;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001327 int ret;
bellard83f64092006-08-01 16:21:11 +00001328 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001329 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +00001330 if (!drv->bdrv_truncate)
1331 return -ENOTSUP;
Naphtali Sprei59f26892009-10-26 16:25:16 +02001332 if (bs->read_only)
1333 return -EACCES;
Marcelo Tosatti85916752011-01-26 12:12:35 -02001334 if (bdrv_in_use(bs))
1335 return -EBUSY;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001336 ret = drv->bdrv_truncate(bs, offset);
1337 if (ret == 0) {
1338 ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
Markus Armbruster145feb12011-08-03 15:07:42 +02001339 bdrv_dev_resize_cb(bs);
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001340 }
1341 return ret;
bellard83f64092006-08-01 16:21:11 +00001342}
1343
1344/**
Fam Zheng4a1d5e12011-07-12 19:56:39 +08001345 * Length of a allocated file in bytes. Sparse files are counted by actual
1346 * allocated space. Return < 0 if error or unknown.
1347 */
1348int64_t bdrv_get_allocated_file_size(BlockDriverState *bs)
1349{
1350 BlockDriver *drv = bs->drv;
1351 if (!drv) {
1352 return -ENOMEDIUM;
1353 }
1354 if (drv->bdrv_get_allocated_file_size) {
1355 return drv->bdrv_get_allocated_file_size(bs);
1356 }
1357 if (bs->file) {
1358 return bdrv_get_allocated_file_size(bs->file);
1359 }
1360 return -ENOTSUP;
1361}
1362
1363/**
bellard83f64092006-08-01 16:21:11 +00001364 * Length of a file in bytes. Return < 0 if error or unknown.
1365 */
1366int64_t bdrv_getlength(BlockDriverState *bs)
1367{
1368 BlockDriver *drv = bs->drv;
1369 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001370 return -ENOMEDIUM;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001371
Markus Armbruster2c6942f2011-09-06 18:58:51 +02001372 if (bs->growable || bdrv_dev_has_removable_media(bs)) {
Stefan Hajnoczi46a4e4e2011-03-29 20:04:41 +01001373 if (drv->bdrv_getlength) {
1374 return drv->bdrv_getlength(bs);
1375 }
bellard83f64092006-08-01 16:21:11 +00001376 }
Stefan Hajnoczi46a4e4e2011-03-29 20:04:41 +01001377 return bs->total_sectors * BDRV_SECTOR_SIZE;
bellardfc01f7e2003-06-30 10:03:06 +00001378}
1379
bellard19cb3732006-08-19 11:45:59 +00001380/* return 0 as number of sectors if no device present or error */
ths96b8f132007-12-17 01:35:20 +00001381void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
bellardfc01f7e2003-06-30 10:03:06 +00001382{
bellard19cb3732006-08-19 11:45:59 +00001383 int64_t length;
1384 length = bdrv_getlength(bs);
1385 if (length < 0)
1386 length = 0;
1387 else
Jan Kiszka6ea44302009-11-30 18:21:19 +01001388 length = length >> BDRV_SECTOR_BITS;
bellard19cb3732006-08-19 11:45:59 +00001389 *nb_sectors_ptr = length;
bellardfc01f7e2003-06-30 10:03:06 +00001390}
bellardcf989512004-02-16 21:56:36 +00001391
aliguorif3d54fc2008-11-25 21:50:24 +00001392struct partition {
1393 uint8_t boot_ind; /* 0x80 - active */
1394 uint8_t head; /* starting head */
1395 uint8_t sector; /* starting sector */
1396 uint8_t cyl; /* starting cylinder */
1397 uint8_t sys_ind; /* What partition type */
1398 uint8_t end_head; /* end head */
1399 uint8_t end_sector; /* end sector */
1400 uint8_t end_cyl; /* end cylinder */
1401 uint32_t start_sect; /* starting sector counting from 0 */
1402 uint32_t nr_sects; /* nr of sectors in partition */
Stefan Weil541dc0d2011-08-31 12:38:01 +02001403} QEMU_PACKED;
aliguorif3d54fc2008-11-25 21:50:24 +00001404
1405/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
1406static int guess_disk_lchs(BlockDriverState *bs,
1407 int *pcylinders, int *pheads, int *psectors)
1408{
Jes Sorenseneb5a3162010-05-27 16:20:31 +02001409 uint8_t buf[BDRV_SECTOR_SIZE];
aliguorif3d54fc2008-11-25 21:50:24 +00001410 int ret, i, heads, sectors, cylinders;
1411 struct partition *p;
1412 uint32_t nr_sects;
blueswir1a38131b2008-12-05 17:56:40 +00001413 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +00001414
1415 bdrv_get_geometry(bs, &nb_sectors);
1416
1417 ret = bdrv_read(bs, 0, buf, 1);
1418 if (ret < 0)
1419 return -1;
1420 /* test msdos magic */
1421 if (buf[510] != 0x55 || buf[511] != 0xaa)
1422 return -1;
1423 for(i = 0; i < 4; i++) {
1424 p = ((struct partition *)(buf + 0x1be)) + i;
1425 nr_sects = le32_to_cpu(p->nr_sects);
1426 if (nr_sects && p->end_head) {
1427 /* We make the assumption that the partition terminates on
1428 a cylinder boundary */
1429 heads = p->end_head + 1;
1430 sectors = p->end_sector & 63;
1431 if (sectors == 0)
1432 continue;
1433 cylinders = nb_sectors / (heads * sectors);
1434 if (cylinders < 1 || cylinders > 16383)
1435 continue;
1436 *pheads = heads;
1437 *psectors = sectors;
1438 *pcylinders = cylinders;
1439#if 0
1440 printf("guessed geometry: LCHS=%d %d %d\n",
1441 cylinders, heads, sectors);
1442#endif
1443 return 0;
1444 }
1445 }
1446 return -1;
1447}
1448
1449void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
1450{
1451 int translation, lba_detected = 0;
1452 int cylinders, heads, secs;
blueswir1a38131b2008-12-05 17:56:40 +00001453 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +00001454
1455 /* if a geometry hint is available, use it */
1456 bdrv_get_geometry(bs, &nb_sectors);
1457 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
1458 translation = bdrv_get_translation_hint(bs);
1459 if (cylinders != 0) {
1460 *pcyls = cylinders;
1461 *pheads = heads;
1462 *psecs = secs;
1463 } else {
1464 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
1465 if (heads > 16) {
1466 /* if heads > 16, it means that a BIOS LBA
1467 translation was active, so the default
1468 hardware geometry is OK */
1469 lba_detected = 1;
1470 goto default_geometry;
1471 } else {
1472 *pcyls = cylinders;
1473 *pheads = heads;
1474 *psecs = secs;
1475 /* disable any translation to be in sync with
1476 the logical geometry */
1477 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
1478 bdrv_set_translation_hint(bs,
1479 BIOS_ATA_TRANSLATION_NONE);
1480 }
1481 }
1482 } else {
1483 default_geometry:
1484 /* if no geometry, use a standard physical disk geometry */
1485 cylinders = nb_sectors / (16 * 63);
1486
1487 if (cylinders > 16383)
1488 cylinders = 16383;
1489 else if (cylinders < 2)
1490 cylinders = 2;
1491 *pcyls = cylinders;
1492 *pheads = 16;
1493 *psecs = 63;
1494 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
1495 if ((*pcyls * *pheads) <= 131072) {
1496 bdrv_set_translation_hint(bs,
1497 BIOS_ATA_TRANSLATION_LARGE);
1498 } else {
1499 bdrv_set_translation_hint(bs,
1500 BIOS_ATA_TRANSLATION_LBA);
1501 }
1502 }
1503 }
1504 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
1505 }
1506}
1507
ths5fafdf22007-09-16 21:08:06 +00001508void bdrv_set_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +00001509 int cyls, int heads, int secs)
1510{
1511 bs->cyls = cyls;
1512 bs->heads = heads;
1513 bs->secs = secs;
1514}
1515
bellard46d47672004-11-16 01:45:27 +00001516void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
1517{
1518 bs->translation = translation;
1519}
1520
ths5fafdf22007-09-16 21:08:06 +00001521void bdrv_get_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +00001522 int *pcyls, int *pheads, int *psecs)
1523{
1524 *pcyls = bs->cyls;
1525 *pheads = bs->heads;
1526 *psecs = bs->secs;
1527}
1528
Blue Swirl5bbdbb42011-02-12 20:43:32 +00001529/* Recognize floppy formats */
1530typedef struct FDFormat {
1531 FDriveType drive;
1532 uint8_t last_sect;
1533 uint8_t max_track;
1534 uint8_t max_head;
1535} FDFormat;
1536
1537static const FDFormat fd_formats[] = {
1538 /* First entry is default format */
1539 /* 1.44 MB 3"1/2 floppy disks */
1540 { FDRIVE_DRV_144, 18, 80, 1, },
1541 { FDRIVE_DRV_144, 20, 80, 1, },
1542 { FDRIVE_DRV_144, 21, 80, 1, },
1543 { FDRIVE_DRV_144, 21, 82, 1, },
1544 { FDRIVE_DRV_144, 21, 83, 1, },
1545 { FDRIVE_DRV_144, 22, 80, 1, },
1546 { FDRIVE_DRV_144, 23, 80, 1, },
1547 { FDRIVE_DRV_144, 24, 80, 1, },
1548 /* 2.88 MB 3"1/2 floppy disks */
1549 { FDRIVE_DRV_288, 36, 80, 1, },
1550 { FDRIVE_DRV_288, 39, 80, 1, },
1551 { FDRIVE_DRV_288, 40, 80, 1, },
1552 { FDRIVE_DRV_288, 44, 80, 1, },
1553 { FDRIVE_DRV_288, 48, 80, 1, },
1554 /* 720 kB 3"1/2 floppy disks */
1555 { FDRIVE_DRV_144, 9, 80, 1, },
1556 { FDRIVE_DRV_144, 10, 80, 1, },
1557 { FDRIVE_DRV_144, 10, 82, 1, },
1558 { FDRIVE_DRV_144, 10, 83, 1, },
1559 { FDRIVE_DRV_144, 13, 80, 1, },
1560 { FDRIVE_DRV_144, 14, 80, 1, },
1561 /* 1.2 MB 5"1/4 floppy disks */
1562 { FDRIVE_DRV_120, 15, 80, 1, },
1563 { FDRIVE_DRV_120, 18, 80, 1, },
1564 { FDRIVE_DRV_120, 18, 82, 1, },
1565 { FDRIVE_DRV_120, 18, 83, 1, },
1566 { FDRIVE_DRV_120, 20, 80, 1, },
1567 /* 720 kB 5"1/4 floppy disks */
1568 { FDRIVE_DRV_120, 9, 80, 1, },
1569 { FDRIVE_DRV_120, 11, 80, 1, },
1570 /* 360 kB 5"1/4 floppy disks */
1571 { FDRIVE_DRV_120, 9, 40, 1, },
1572 { FDRIVE_DRV_120, 9, 40, 0, },
1573 { FDRIVE_DRV_120, 10, 41, 1, },
1574 { FDRIVE_DRV_120, 10, 42, 1, },
1575 /* 320 kB 5"1/4 floppy disks */
1576 { FDRIVE_DRV_120, 8, 40, 1, },
1577 { FDRIVE_DRV_120, 8, 40, 0, },
1578 /* 360 kB must match 5"1/4 better than 3"1/2... */
1579 { FDRIVE_DRV_144, 9, 80, 0, },
1580 /* end */
1581 { FDRIVE_DRV_NONE, -1, -1, 0, },
1582};
1583
1584void bdrv_get_floppy_geometry_hint(BlockDriverState *bs, int *nb_heads,
1585 int *max_track, int *last_sect,
1586 FDriveType drive_in, FDriveType *drive)
1587{
1588 const FDFormat *parse;
1589 uint64_t nb_sectors, size;
1590 int i, first_match, match;
1591
1592 bdrv_get_geometry_hint(bs, nb_heads, max_track, last_sect);
1593 if (*nb_heads != 0 && *max_track != 0 && *last_sect != 0) {
1594 /* User defined disk */
1595 } else {
1596 bdrv_get_geometry(bs, &nb_sectors);
1597 match = -1;
1598 first_match = -1;
1599 for (i = 0; ; i++) {
1600 parse = &fd_formats[i];
1601 if (parse->drive == FDRIVE_DRV_NONE) {
1602 break;
1603 }
1604 if (drive_in == parse->drive ||
1605 drive_in == FDRIVE_DRV_NONE) {
1606 size = (parse->max_head + 1) * parse->max_track *
1607 parse->last_sect;
1608 if (nb_sectors == size) {
1609 match = i;
1610 break;
1611 }
1612 if (first_match == -1) {
1613 first_match = i;
1614 }
1615 }
1616 }
1617 if (match == -1) {
1618 if (first_match == -1) {
1619 match = 1;
1620 } else {
1621 match = first_match;
1622 }
1623 parse = &fd_formats[match];
1624 }
1625 *nb_heads = parse->max_head + 1;
1626 *max_track = parse->max_track;
1627 *last_sect = parse->last_sect;
1628 *drive = parse->drive;
1629 }
1630}
1631
bellard46d47672004-11-16 01:45:27 +00001632int bdrv_get_translation_hint(BlockDriverState *bs)
1633{
1634 return bs->translation;
1635}
1636
Markus Armbrusterabd7f682010-06-02 18:55:17 +02001637void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error,
1638 BlockErrorAction on_write_error)
1639{
1640 bs->on_read_error = on_read_error;
1641 bs->on_write_error = on_write_error;
1642}
1643
1644BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read)
1645{
1646 return is_read ? bs->on_read_error : bs->on_write_error;
1647}
1648
bellardb3380822004-03-14 21:38:54 +00001649int bdrv_is_read_only(BlockDriverState *bs)
1650{
1651 return bs->read_only;
1652}
1653
ths985a03b2007-12-24 16:10:43 +00001654int bdrv_is_sg(BlockDriverState *bs)
1655{
1656 return bs->sg;
1657}
1658
Christoph Hellwige900a7b2009-09-04 19:01:15 +02001659int bdrv_enable_write_cache(BlockDriverState *bs)
1660{
1661 return bs->enable_write_cache;
1662}
1663
bellardea2384d2004-08-01 21:59:26 +00001664int bdrv_is_encrypted(BlockDriverState *bs)
1665{
1666 if (bs->backing_hd && bs->backing_hd->encrypted)
1667 return 1;
1668 return bs->encrypted;
1669}
1670
aliguoric0f4ce72009-03-05 23:01:01 +00001671int bdrv_key_required(BlockDriverState *bs)
1672{
1673 BlockDriverState *backing_hd = bs->backing_hd;
1674
1675 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
1676 return 1;
1677 return (bs->encrypted && !bs->valid_key);
1678}
1679
bellardea2384d2004-08-01 21:59:26 +00001680int bdrv_set_key(BlockDriverState *bs, const char *key)
1681{
1682 int ret;
1683 if (bs->backing_hd && bs->backing_hd->encrypted) {
1684 ret = bdrv_set_key(bs->backing_hd, key);
1685 if (ret < 0)
1686 return ret;
1687 if (!bs->encrypted)
1688 return 0;
1689 }
Shahar Havivifd04a2a2010-03-06 00:26:13 +02001690 if (!bs->encrypted) {
1691 return -EINVAL;
1692 } else if (!bs->drv || !bs->drv->bdrv_set_key) {
1693 return -ENOMEDIUM;
1694 }
aliguoric0f4ce72009-03-05 23:01:01 +00001695 ret = bs->drv->bdrv_set_key(bs, key);
aliguoribb5fc202009-03-05 23:01:15 +00001696 if (ret < 0) {
1697 bs->valid_key = 0;
1698 } else if (!bs->valid_key) {
1699 bs->valid_key = 1;
1700 /* call the change callback now, we skipped it on open */
Markus Armbruster7d4b4ba2011-09-06 18:58:59 +02001701 bdrv_dev_change_media_cb(bs, true);
aliguoribb5fc202009-03-05 23:01:15 +00001702 }
aliguoric0f4ce72009-03-05 23:01:01 +00001703 return ret;
bellardea2384d2004-08-01 21:59:26 +00001704}
1705
1706void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
1707{
bellard19cb3732006-08-19 11:45:59 +00001708 if (!bs->drv) {
bellardea2384d2004-08-01 21:59:26 +00001709 buf[0] = '\0';
1710 } else {
1711 pstrcpy(buf, buf_size, bs->drv->format_name);
1712 }
1713}
1714
ths5fafdf22007-09-16 21:08:06 +00001715void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
bellardea2384d2004-08-01 21:59:26 +00001716 void *opaque)
1717{
1718 BlockDriver *drv;
1719
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +01001720 QLIST_FOREACH(drv, &bdrv_drivers, list) {
bellardea2384d2004-08-01 21:59:26 +00001721 it(opaque, drv->format_name);
1722 }
1723}
1724
bellardb3380822004-03-14 21:38:54 +00001725BlockDriverState *bdrv_find(const char *name)
1726{
1727 BlockDriverState *bs;
1728
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001729 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1730 if (!strcmp(name, bs->device_name)) {
bellardb3380822004-03-14 21:38:54 +00001731 return bs;
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001732 }
bellardb3380822004-03-14 21:38:54 +00001733 }
1734 return NULL;
1735}
1736
Markus Armbruster2f399b02010-06-02 18:55:20 +02001737BlockDriverState *bdrv_next(BlockDriverState *bs)
1738{
1739 if (!bs) {
1740 return QTAILQ_FIRST(&bdrv_states);
1741 }
1742 return QTAILQ_NEXT(bs, list);
1743}
1744
aliguori51de9762009-03-05 23:00:43 +00001745void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
bellard81d09122004-07-14 17:21:37 +00001746{
1747 BlockDriverState *bs;
1748
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001749 QTAILQ_FOREACH(bs, &bdrv_states, list) {
aliguori51de9762009-03-05 23:00:43 +00001750 it(opaque, bs);
bellard81d09122004-07-14 17:21:37 +00001751 }
1752}
1753
bellardea2384d2004-08-01 21:59:26 +00001754const char *bdrv_get_device_name(BlockDriverState *bs)
1755{
1756 return bs->device_name;
1757}
1758
aliguoric6ca28d2008-10-06 13:55:43 +00001759void bdrv_flush_all(void)
1760{
1761 BlockDriverState *bs;
1762
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001763 QTAILQ_FOREACH(bs, &bdrv_states, list) {
Markus Armbrusterc602a482011-08-03 15:08:10 +02001764 if (!bdrv_is_read_only(bs) && bdrv_is_inserted(bs)) {
aliguoric6ca28d2008-10-06 13:55:43 +00001765 bdrv_flush(bs);
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001766 }
1767 }
aliguoric6ca28d2008-10-06 13:55:43 +00001768}
1769
Kevin Wolff2feebb2010-04-14 17:30:35 +02001770int bdrv_has_zero_init(BlockDriverState *bs)
1771{
1772 assert(bs->drv);
1773
Kevin Wolf336c1c12010-07-28 11:26:29 +02001774 if (bs->drv->bdrv_has_zero_init) {
1775 return bs->drv->bdrv_has_zero_init(bs);
Kevin Wolff2feebb2010-04-14 17:30:35 +02001776 }
1777
1778 return 1;
1779}
1780
thsf58c7b32008-06-05 21:53:49 +00001781/*
1782 * Returns true iff the specified sector is present in the disk image. Drivers
1783 * not implementing the functionality are assumed to not support backing files,
1784 * hence all their sectors are reported as allocated.
1785 *
1786 * 'pnum' is set to the number of sectors (including and immediately following
1787 * the specified sector) that are known to be in the same
1788 * allocated/unallocated state.
1789 *
1790 * 'nb_sectors' is the max value 'pnum' should be set to.
1791 */
1792int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1793 int *pnum)
1794{
1795 int64_t n;
1796 if (!bs->drv->bdrv_is_allocated) {
1797 if (sector_num >= bs->total_sectors) {
1798 *pnum = 0;
1799 return 0;
1800 }
1801 n = bs->total_sectors - sector_num;
1802 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1803 return 1;
1804 }
1805 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1806}
1807
Luiz Capitulino2582bfe2010-02-03 12:41:01 -02001808void bdrv_mon_event(const BlockDriverState *bdrv,
1809 BlockMonEventAction action, int is_read)
1810{
1811 QObject *data;
1812 const char *action_str;
1813
1814 switch (action) {
1815 case BDRV_ACTION_REPORT:
1816 action_str = "report";
1817 break;
1818 case BDRV_ACTION_IGNORE:
1819 action_str = "ignore";
1820 break;
1821 case BDRV_ACTION_STOP:
1822 action_str = "stop";
1823 break;
1824 default:
1825 abort();
1826 }
1827
1828 data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
1829 bdrv->device_name,
1830 action_str,
1831 is_read ? "read" : "write");
1832 monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data);
1833
1834 qobject_decref(data);
1835}
1836
Luiz Capitulinob2023812011-09-21 17:16:47 -03001837BlockInfoList *qmp_query_block(Error **errp)
bellardb3380822004-03-14 21:38:54 +00001838{
Luiz Capitulinob2023812011-09-21 17:16:47 -03001839 BlockInfoList *head = NULL, *cur_item = NULL;
bellardb3380822004-03-14 21:38:54 +00001840 BlockDriverState *bs;
1841
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001842 QTAILQ_FOREACH(bs, &bdrv_states, list) {
Luiz Capitulinob2023812011-09-21 17:16:47 -03001843 BlockInfoList *info = g_malloc0(sizeof(*info));
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001844
Luiz Capitulinob2023812011-09-21 17:16:47 -03001845 info->value = g_malloc0(sizeof(*info->value));
1846 info->value->device = g_strdup(bs->device_name);
1847 info->value->type = g_strdup("unknown");
1848 info->value->locked = bdrv_dev_is_medium_locked(bs);
1849 info->value->removable = bdrv_dev_has_removable_media(bs);
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001850
Markus Armbrustere4def802011-09-06 18:58:53 +02001851 if (bdrv_dev_has_removable_media(bs)) {
Luiz Capitulinob2023812011-09-21 17:16:47 -03001852 info->value->has_tray_open = true;
1853 info->value->tray_open = bdrv_dev_is_tray_open(bs);
Markus Armbrustere4def802011-09-06 18:58:53 +02001854 }
Luiz Capitulinof04ef602011-09-26 17:43:54 -03001855
1856 if (bdrv_iostatus_is_enabled(bs)) {
Luiz Capitulinob2023812011-09-21 17:16:47 -03001857 info->value->has_io_status = true;
1858 info->value->io_status = bs->iostatus;
Luiz Capitulinof04ef602011-09-26 17:43:54 -03001859 }
1860
bellard19cb3732006-08-19 11:45:59 +00001861 if (bs->drv) {
Luiz Capitulinob2023812011-09-21 17:16:47 -03001862 info->value->has_inserted = true;
1863 info->value->inserted = g_malloc0(sizeof(*info->value->inserted));
1864 info->value->inserted->file = g_strdup(bs->filename);
1865 info->value->inserted->ro = bs->read_only;
1866 info->value->inserted->drv = g_strdup(bs->drv->format_name);
1867 info->value->inserted->encrypted = bs->encrypted;
1868 if (bs->backing_file[0]) {
1869 info->value->inserted->has_backing_file = true;
1870 info->value->inserted->backing_file = g_strdup(bs->backing_file);
aliguori376253e2009-03-05 23:01:23 +00001871 }
bellardb3380822004-03-14 21:38:54 +00001872 }
Luiz Capitulinob2023812011-09-21 17:16:47 -03001873
1874 /* XXX: waiting for the qapi to support GSList */
1875 if (!cur_item) {
1876 head = cur_item = info;
1877 } else {
1878 cur_item->next = info;
1879 cur_item = info;
1880 }
bellardb3380822004-03-14 21:38:54 +00001881 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001882
Luiz Capitulinob2023812011-09-21 17:16:47 -03001883 return head;
bellardb3380822004-03-14 21:38:54 +00001884}
thsa36e69d2007-12-02 05:18:19 +00001885
Luiz Capitulinof11f57e2011-09-22 15:56:36 -03001886/* Consider exposing this as a full fledged QMP command */
1887static BlockStats *qmp_query_blockstat(const BlockDriverState *bs, Error **errp)
thsa36e69d2007-12-02 05:18:19 +00001888{
Luiz Capitulinof11f57e2011-09-22 15:56:36 -03001889 BlockStats *s;
Luiz Capitulino218a5362009-12-10 17:16:07 -02001890
Luiz Capitulinof11f57e2011-09-22 15:56:36 -03001891 s = g_malloc0(sizeof(*s));
Luiz Capitulino218a5362009-12-10 17:16:07 -02001892
Luiz Capitulinof11f57e2011-09-22 15:56:36 -03001893 if (bs->device_name[0]) {
1894 s->has_device = true;
1895 s->device = g_strdup(bs->device_name);
Kevin Wolf294cc352010-04-28 14:34:01 +02001896 }
1897
Luiz Capitulinof11f57e2011-09-22 15:56:36 -03001898 s->stats = g_malloc0(sizeof(*s->stats));
1899 s->stats->rd_bytes = bs->nr_bytes[BDRV_ACCT_READ];
1900 s->stats->wr_bytes = bs->nr_bytes[BDRV_ACCT_WRITE];
1901 s->stats->rd_operations = bs->nr_ops[BDRV_ACCT_READ];
1902 s->stats->wr_operations = bs->nr_ops[BDRV_ACCT_WRITE];
1903 s->stats->wr_highest_offset = bs->wr_highest_sector * BDRV_SECTOR_SIZE;
1904 s->stats->flush_operations = bs->nr_ops[BDRV_ACCT_FLUSH];
1905 s->stats->wr_total_time_ns = bs->total_time_ns[BDRV_ACCT_WRITE];
1906 s->stats->rd_total_time_ns = bs->total_time_ns[BDRV_ACCT_READ];
1907 s->stats->flush_total_time_ns = bs->total_time_ns[BDRV_ACCT_FLUSH];
1908
Kevin Wolf294cc352010-04-28 14:34:01 +02001909 if (bs->file) {
Luiz Capitulinof11f57e2011-09-22 15:56:36 -03001910 s->has_parent = true;
1911 s->parent = qmp_query_blockstat(bs->file, NULL);
Kevin Wolf294cc352010-04-28 14:34:01 +02001912 }
1913
Luiz Capitulinof11f57e2011-09-22 15:56:36 -03001914 return s;
Kevin Wolf294cc352010-04-28 14:34:01 +02001915}
1916
Luiz Capitulinof11f57e2011-09-22 15:56:36 -03001917BlockStatsList *qmp_query_blockstats(Error **errp)
Luiz Capitulino218a5362009-12-10 17:16:07 -02001918{
Luiz Capitulinof11f57e2011-09-22 15:56:36 -03001919 BlockStatsList *head = NULL, *cur_item = NULL;
thsa36e69d2007-12-02 05:18:19 +00001920 BlockDriverState *bs;
1921
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001922 QTAILQ_FOREACH(bs, &bdrv_states, list) {
Luiz Capitulinof11f57e2011-09-22 15:56:36 -03001923 BlockStatsList *info = g_malloc0(sizeof(*info));
1924 info->value = qmp_query_blockstat(bs, NULL);
1925
1926 /* XXX: waiting for the qapi to support GSList */
1927 if (!cur_item) {
1928 head = cur_item = info;
1929 } else {
1930 cur_item->next = info;
1931 cur_item = info;
1932 }
thsa36e69d2007-12-02 05:18:19 +00001933 }
Luiz Capitulino218a5362009-12-10 17:16:07 -02001934
Luiz Capitulinof11f57e2011-09-22 15:56:36 -03001935 return head;
thsa36e69d2007-12-02 05:18:19 +00001936}
bellardea2384d2004-08-01 21:59:26 +00001937
aliguori045df332009-03-05 23:00:48 +00001938const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1939{
1940 if (bs->backing_hd && bs->backing_hd->encrypted)
1941 return bs->backing_file;
1942 else if (bs->encrypted)
1943 return bs->filename;
1944 else
1945 return NULL;
1946}
1947
ths5fafdf22007-09-16 21:08:06 +00001948void bdrv_get_backing_filename(BlockDriverState *bs,
bellard83f64092006-08-01 16:21:11 +00001949 char *filename, int filename_size)
bellardea2384d2004-08-01 21:59:26 +00001950{
Kevin Wolf3574c602011-10-26 11:02:11 +02001951 pstrcpy(filename, filename_size, bs->backing_file);
bellardea2384d2004-08-01 21:59:26 +00001952}
1953
ths5fafdf22007-09-16 21:08:06 +00001954int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
bellardfaea38e2006-08-05 21:31:00 +00001955 const uint8_t *buf, int nb_sectors)
1956{
1957 BlockDriver *drv = bs->drv;
1958 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001959 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001960 if (!drv->bdrv_write_compressed)
1961 return -ENOTSUP;
Kevin Wolffbb7b4e2009-05-08 14:47:24 +02001962 if (bdrv_check_request(bs, sector_num, nb_sectors))
1963 return -EIO;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001964
Jan Kiszkac6d22832009-11-30 18:21:20 +01001965 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001966 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1967 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001968
bellardfaea38e2006-08-05 21:31:00 +00001969 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1970}
ths3b46e622007-09-17 08:09:54 +00001971
bellardfaea38e2006-08-05 21:31:00 +00001972int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1973{
1974 BlockDriver *drv = bs->drv;
1975 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001976 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001977 if (!drv->bdrv_get_info)
1978 return -ENOTSUP;
1979 memset(bdi, 0, sizeof(*bdi));
1980 return drv->bdrv_get_info(bs, bdi);
1981}
1982
Christoph Hellwig45566e92009-07-10 23:11:57 +02001983int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
1984 int64_t pos, int size)
aliguori178e08a2009-04-05 19:10:55 +00001985{
1986 BlockDriver *drv = bs->drv;
1987 if (!drv)
1988 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09001989 if (drv->bdrv_save_vmstate)
1990 return drv->bdrv_save_vmstate(bs, buf, pos, size);
1991 if (bs->file)
1992 return bdrv_save_vmstate(bs->file, buf, pos, size);
1993 return -ENOTSUP;
aliguori178e08a2009-04-05 19:10:55 +00001994}
1995
Christoph Hellwig45566e92009-07-10 23:11:57 +02001996int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1997 int64_t pos, int size)
aliguori178e08a2009-04-05 19:10:55 +00001998{
1999 BlockDriver *drv = bs->drv;
2000 if (!drv)
2001 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09002002 if (drv->bdrv_load_vmstate)
2003 return drv->bdrv_load_vmstate(bs, buf, pos, size);
2004 if (bs->file)
2005 return bdrv_load_vmstate(bs->file, buf, pos, size);
2006 return -ENOTSUP;
aliguori178e08a2009-04-05 19:10:55 +00002007}
2008
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01002009void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
2010{
2011 BlockDriver *drv = bs->drv;
2012
2013 if (!drv || !drv->bdrv_debug_event) {
2014 return;
2015 }
2016
2017 return drv->bdrv_debug_event(bs, event);
2018
2019}
2020
bellardfaea38e2006-08-05 21:31:00 +00002021/**************************************************************/
2022/* handling of snapshots */
2023
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002024int bdrv_can_snapshot(BlockDriverState *bs)
2025{
2026 BlockDriver *drv = bs->drv;
Markus Armbruster07b70bf2011-08-03 15:08:11 +02002027 if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002028 return 0;
2029 }
2030
2031 if (!drv->bdrv_snapshot_create) {
2032 if (bs->file != NULL) {
2033 return bdrv_can_snapshot(bs->file);
2034 }
2035 return 0;
2036 }
2037
2038 return 1;
2039}
2040
Blue Swirl199630b2010-07-25 20:49:34 +00002041int bdrv_is_snapshot(BlockDriverState *bs)
2042{
2043 return !!(bs->open_flags & BDRV_O_SNAPSHOT);
2044}
2045
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002046BlockDriverState *bdrv_snapshots(void)
2047{
2048 BlockDriverState *bs;
2049
Markus Armbruster3ac906f2010-07-01 09:30:38 +02002050 if (bs_snapshots) {
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002051 return bs_snapshots;
Markus Armbruster3ac906f2010-07-01 09:30:38 +02002052 }
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002053
2054 bs = NULL;
2055 while ((bs = bdrv_next(bs))) {
2056 if (bdrv_can_snapshot(bs)) {
Markus Armbruster3ac906f2010-07-01 09:30:38 +02002057 bs_snapshots = bs;
2058 return bs;
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002059 }
2060 }
2061 return NULL;
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002062}
2063
ths5fafdf22007-09-16 21:08:06 +00002064int bdrv_snapshot_create(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00002065 QEMUSnapshotInfo *sn_info)
2066{
2067 BlockDriver *drv = bs->drv;
2068 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002069 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09002070 if (drv->bdrv_snapshot_create)
2071 return drv->bdrv_snapshot_create(bs, sn_info);
2072 if (bs->file)
2073 return bdrv_snapshot_create(bs->file, sn_info);
2074 return -ENOTSUP;
bellardfaea38e2006-08-05 21:31:00 +00002075}
2076
ths5fafdf22007-09-16 21:08:06 +00002077int bdrv_snapshot_goto(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00002078 const char *snapshot_id)
2079{
2080 BlockDriver *drv = bs->drv;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09002081 int ret, open_ret;
2082
bellardfaea38e2006-08-05 21:31:00 +00002083 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002084 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09002085 if (drv->bdrv_snapshot_goto)
2086 return drv->bdrv_snapshot_goto(bs, snapshot_id);
2087
2088 if (bs->file) {
2089 drv->bdrv_close(bs);
2090 ret = bdrv_snapshot_goto(bs->file, snapshot_id);
2091 open_ret = drv->bdrv_open(bs, bs->open_flags);
2092 if (open_ret < 0) {
2093 bdrv_delete(bs->file);
2094 bs->drv = NULL;
2095 return open_ret;
2096 }
2097 return ret;
2098 }
2099
2100 return -ENOTSUP;
bellardfaea38e2006-08-05 21:31:00 +00002101}
2102
2103int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
2104{
2105 BlockDriver *drv = bs->drv;
2106 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002107 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09002108 if (drv->bdrv_snapshot_delete)
2109 return drv->bdrv_snapshot_delete(bs, snapshot_id);
2110 if (bs->file)
2111 return bdrv_snapshot_delete(bs->file, snapshot_id);
2112 return -ENOTSUP;
bellardfaea38e2006-08-05 21:31:00 +00002113}
2114
ths5fafdf22007-09-16 21:08:06 +00002115int bdrv_snapshot_list(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00002116 QEMUSnapshotInfo **psn_info)
2117{
2118 BlockDriver *drv = bs->drv;
2119 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002120 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09002121 if (drv->bdrv_snapshot_list)
2122 return drv->bdrv_snapshot_list(bs, psn_info);
2123 if (bs->file)
2124 return bdrv_snapshot_list(bs->file, psn_info);
2125 return -ENOTSUP;
bellardfaea38e2006-08-05 21:31:00 +00002126}
2127
edison51ef6722010-09-21 19:58:41 -07002128int bdrv_snapshot_load_tmp(BlockDriverState *bs,
2129 const char *snapshot_name)
2130{
2131 BlockDriver *drv = bs->drv;
2132 if (!drv) {
2133 return -ENOMEDIUM;
2134 }
2135 if (!bs->read_only) {
2136 return -EINVAL;
2137 }
2138 if (drv->bdrv_snapshot_load_tmp) {
2139 return drv->bdrv_snapshot_load_tmp(bs, snapshot_name);
2140 }
2141 return -ENOTSUP;
2142}
2143
bellardfaea38e2006-08-05 21:31:00 +00002144#define NB_SUFFIXES 4
2145
2146char *get_human_readable_size(char *buf, int buf_size, int64_t size)
2147{
2148 static const char suffixes[NB_SUFFIXES] = "KMGT";
2149 int64_t base;
2150 int i;
2151
2152 if (size <= 999) {
2153 snprintf(buf, buf_size, "%" PRId64, size);
2154 } else {
2155 base = 1024;
2156 for(i = 0; i < NB_SUFFIXES; i++) {
2157 if (size < (10 * base)) {
ths5fafdf22007-09-16 21:08:06 +00002158 snprintf(buf, buf_size, "%0.1f%c",
bellardfaea38e2006-08-05 21:31:00 +00002159 (double)size / base,
2160 suffixes[i]);
2161 break;
2162 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
ths5fafdf22007-09-16 21:08:06 +00002163 snprintf(buf, buf_size, "%" PRId64 "%c",
bellardfaea38e2006-08-05 21:31:00 +00002164 ((size + (base >> 1)) / base),
2165 suffixes[i]);
2166 break;
2167 }
2168 base = base * 1024;
2169 }
2170 }
2171 return buf;
2172}
2173
2174char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
2175{
2176 char buf1[128], date_buf[128], clock_buf[128];
bellard3b9f94e2007-01-07 17:27:07 +00002177#ifdef _WIN32
2178 struct tm *ptm;
2179#else
bellardfaea38e2006-08-05 21:31:00 +00002180 struct tm tm;
bellard3b9f94e2007-01-07 17:27:07 +00002181#endif
bellardfaea38e2006-08-05 21:31:00 +00002182 time_t ti;
2183 int64_t secs;
2184
2185 if (!sn) {
ths5fafdf22007-09-16 21:08:06 +00002186 snprintf(buf, buf_size,
2187 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00002188 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
2189 } else {
2190 ti = sn->date_sec;
bellard3b9f94e2007-01-07 17:27:07 +00002191#ifdef _WIN32
2192 ptm = localtime(&ti);
2193 strftime(date_buf, sizeof(date_buf),
2194 "%Y-%m-%d %H:%M:%S", ptm);
2195#else
bellardfaea38e2006-08-05 21:31:00 +00002196 localtime_r(&ti, &tm);
2197 strftime(date_buf, sizeof(date_buf),
2198 "%Y-%m-%d %H:%M:%S", &tm);
bellard3b9f94e2007-01-07 17:27:07 +00002199#endif
bellardfaea38e2006-08-05 21:31:00 +00002200 secs = sn->vm_clock_nsec / 1000000000;
2201 snprintf(clock_buf, sizeof(clock_buf),
2202 "%02d:%02d:%02d.%03d",
2203 (int)(secs / 3600),
2204 (int)((secs / 60) % 60),
ths5fafdf22007-09-16 21:08:06 +00002205 (int)(secs % 60),
bellardfaea38e2006-08-05 21:31:00 +00002206 (int)((sn->vm_clock_nsec / 1000000) % 1000));
2207 snprintf(buf, buf_size,
ths5fafdf22007-09-16 21:08:06 +00002208 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00002209 sn->id_str, sn->name,
2210 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
2211 date_buf,
2212 clock_buf);
2213 }
2214 return buf;
2215}
2216
bellard83f64092006-08-01 16:21:11 +00002217/**************************************************************/
2218/* async I/Os */
2219
aliguori3b69e4b2009-01-22 16:59:24 +00002220BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
aliguorif141eaf2009-04-07 18:43:24 +00002221 QEMUIOVector *qiov, int nb_sectors,
aliguori3b69e4b2009-01-22 16:59:24 +00002222 BlockDriverCompletionFunc *cb, void *opaque)
2223{
Stefan Hajnoczibbf0a442010-10-05 14:28:53 +01002224 trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque);
2225
Stefan Hajnoczib2a61372011-10-13 13:08:23 +01002226 return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors,
Stefan Hajnoczi8c5873d2011-10-13 21:09:28 +01002227 cb, opaque, false);
bellard83f64092006-08-01 16:21:11 +00002228}
2229
aliguorif141eaf2009-04-07 18:43:24 +00002230BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
2231 QEMUIOVector *qiov, int nb_sectors,
2232 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00002233{
Stefan Hajnoczibbf0a442010-10-05 14:28:53 +01002234 trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque);
2235
Stefan Hajnoczi1a6e1152011-10-13 13:08:25 +01002236 return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors,
Stefan Hajnoczi8c5873d2011-10-13 21:09:28 +01002237 cb, opaque, true);
bellard83f64092006-08-01 16:21:11 +00002238}
2239
Kevin Wolf40b4f532009-09-09 17:53:37 +02002240
2241typedef struct MultiwriteCB {
2242 int error;
2243 int num_requests;
2244 int num_callbacks;
2245 struct {
2246 BlockDriverCompletionFunc *cb;
2247 void *opaque;
2248 QEMUIOVector *free_qiov;
2249 void *free_buf;
2250 } callbacks[];
2251} MultiwriteCB;
2252
2253static void multiwrite_user_cb(MultiwriteCB *mcb)
2254{
2255 int i;
2256
2257 for (i = 0; i < mcb->num_callbacks; i++) {
2258 mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
Stefan Hajnoczi1e1ea482010-04-21 20:35:45 +01002259 if (mcb->callbacks[i].free_qiov) {
2260 qemu_iovec_destroy(mcb->callbacks[i].free_qiov);
2261 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002262 g_free(mcb->callbacks[i].free_qiov);
Herve Poussineauf8a83242010-01-24 21:23:56 +00002263 qemu_vfree(mcb->callbacks[i].free_buf);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002264 }
2265}
2266
2267static void multiwrite_cb(void *opaque, int ret)
2268{
2269 MultiwriteCB *mcb = opaque;
2270
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +01002271 trace_multiwrite_cb(mcb, ret);
2272
Kevin Wolfcb6d3ca2010-04-01 22:48:44 +02002273 if (ret < 0 && !mcb->error) {
Kevin Wolf40b4f532009-09-09 17:53:37 +02002274 mcb->error = ret;
Kevin Wolf40b4f532009-09-09 17:53:37 +02002275 }
2276
2277 mcb->num_requests--;
2278 if (mcb->num_requests == 0) {
Kevin Wolfde189a12010-07-01 16:08:51 +02002279 multiwrite_user_cb(mcb);
Anthony Liguori7267c092011-08-20 22:09:37 -05002280 g_free(mcb);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002281 }
2282}
2283
2284static int multiwrite_req_compare(const void *a, const void *b)
2285{
Christoph Hellwig77be4362010-05-19 20:53:10 +02002286 const BlockRequest *req1 = a, *req2 = b;
2287
2288 /*
2289 * Note that we can't simply subtract req2->sector from req1->sector
2290 * here as that could overflow the return value.
2291 */
2292 if (req1->sector > req2->sector) {
2293 return 1;
2294 } else if (req1->sector < req2->sector) {
2295 return -1;
2296 } else {
2297 return 0;
2298 }
Kevin Wolf40b4f532009-09-09 17:53:37 +02002299}
2300
2301/*
2302 * Takes a bunch of requests and tries to merge them. Returns the number of
2303 * requests that remain after merging.
2304 */
2305static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
2306 int num_reqs, MultiwriteCB *mcb)
2307{
2308 int i, outidx;
2309
2310 // Sort requests by start sector
2311 qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
2312
2313 // Check if adjacent requests touch the same clusters. If so, combine them,
2314 // filling up gaps with zero sectors.
2315 outidx = 0;
2316 for (i = 1; i < num_reqs; i++) {
2317 int merge = 0;
2318 int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
2319
2320 // This handles the cases that are valid for all block drivers, namely
2321 // exactly sequential writes and overlapping writes.
2322 if (reqs[i].sector <= oldreq_last) {
2323 merge = 1;
2324 }
2325
2326 // The block driver may decide that it makes sense to combine requests
2327 // even if there is a gap of some sectors between them. In this case,
2328 // the gap is filled with zeros (therefore only applicable for yet
2329 // unused space in format like qcow2).
2330 if (!merge && bs->drv->bdrv_merge_requests) {
2331 merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]);
2332 }
2333
Christoph Hellwige2a305f2010-01-26 14:49:08 +01002334 if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) {
2335 merge = 0;
2336 }
2337
Kevin Wolf40b4f532009-09-09 17:53:37 +02002338 if (merge) {
2339 size_t size;
Anthony Liguori7267c092011-08-20 22:09:37 -05002340 QEMUIOVector *qiov = g_malloc0(sizeof(*qiov));
Kevin Wolf40b4f532009-09-09 17:53:37 +02002341 qemu_iovec_init(qiov,
2342 reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
2343
2344 // Add the first request to the merged one. If the requests are
2345 // overlapping, drop the last sectors of the first request.
2346 size = (reqs[i].sector - reqs[outidx].sector) << 9;
2347 qemu_iovec_concat(qiov, reqs[outidx].qiov, size);
2348
2349 // We might need to add some zeros between the two requests
2350 if (reqs[i].sector > oldreq_last) {
2351 size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
2352 uint8_t *buf = qemu_blockalign(bs, zero_bytes);
2353 memset(buf, 0, zero_bytes);
2354 qemu_iovec_add(qiov, buf, zero_bytes);
2355 mcb->callbacks[i].free_buf = buf;
2356 }
2357
2358 // Add the second request
2359 qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size);
2360
Kevin Wolfcbf1dff2010-05-21 11:09:42 +02002361 reqs[outidx].nb_sectors = qiov->size >> 9;
Kevin Wolf40b4f532009-09-09 17:53:37 +02002362 reqs[outidx].qiov = qiov;
2363
2364 mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
2365 } else {
2366 outidx++;
2367 reqs[outidx].sector = reqs[i].sector;
2368 reqs[outidx].nb_sectors = reqs[i].nb_sectors;
2369 reqs[outidx].qiov = reqs[i].qiov;
2370 }
2371 }
2372
2373 return outidx + 1;
2374}
2375
2376/*
2377 * Submit multiple AIO write requests at once.
2378 *
2379 * On success, the function returns 0 and all requests in the reqs array have
2380 * been submitted. In error case this function returns -1, and any of the
2381 * requests may or may not be submitted yet. In particular, this means that the
2382 * callback will be called for some of the requests, for others it won't. The
2383 * caller must check the error field of the BlockRequest to wait for the right
2384 * callbacks (if error != 0, no callback will be called).
2385 *
2386 * The implementation may modify the contents of the reqs array, e.g. to merge
2387 * requests. However, the fields opaque and error are left unmodified as they
2388 * are used to signal failure for a single request to the caller.
2389 */
2390int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
2391{
2392 BlockDriverAIOCB *acb;
2393 MultiwriteCB *mcb;
2394 int i;
2395
Ryan Harper301db7c2011-03-07 10:01:04 -06002396 /* don't submit writes if we don't have a medium */
2397 if (bs->drv == NULL) {
2398 for (i = 0; i < num_reqs; i++) {
2399 reqs[i].error = -ENOMEDIUM;
2400 }
2401 return -1;
2402 }
2403
Kevin Wolf40b4f532009-09-09 17:53:37 +02002404 if (num_reqs == 0) {
2405 return 0;
2406 }
2407
2408 // Create MultiwriteCB structure
Anthony Liguori7267c092011-08-20 22:09:37 -05002409 mcb = g_malloc0(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
Kevin Wolf40b4f532009-09-09 17:53:37 +02002410 mcb->num_requests = 0;
2411 mcb->num_callbacks = num_reqs;
2412
2413 for (i = 0; i < num_reqs; i++) {
2414 mcb->callbacks[i].cb = reqs[i].cb;
2415 mcb->callbacks[i].opaque = reqs[i].opaque;
2416 }
2417
2418 // Check for mergable requests
2419 num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
2420
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +01002421 trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs);
2422
Kevin Wolf453f9a12010-07-02 14:01:21 +02002423 /*
2424 * Run the aio requests. As soon as one request can't be submitted
2425 * successfully, fail all requests that are not yet submitted (we must
2426 * return failure for all requests anyway)
2427 *
2428 * num_requests cannot be set to the right value immediately: If
2429 * bdrv_aio_writev fails for some request, num_requests would be too high
2430 * and therefore multiwrite_cb() would never recognize the multiwrite
2431 * request as completed. We also cannot use the loop variable i to set it
2432 * when the first request fails because the callback may already have been
2433 * called for previously submitted requests. Thus, num_requests must be
2434 * incremented for each request that is submitted.
2435 *
2436 * The problem that callbacks may be called early also means that we need
2437 * to take care that num_requests doesn't become 0 before all requests are
2438 * submitted - multiwrite_cb() would consider the multiwrite request
2439 * completed. A dummy request that is "completed" by a manual call to
2440 * multiwrite_cb() takes care of this.
2441 */
2442 mcb->num_requests = 1;
2443
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +01002444 // Run the aio requests
Kevin Wolf40b4f532009-09-09 17:53:37 +02002445 for (i = 0; i < num_reqs; i++) {
Kevin Wolf453f9a12010-07-02 14:01:21 +02002446 mcb->num_requests++;
Kevin Wolf40b4f532009-09-09 17:53:37 +02002447 acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
2448 reqs[i].nb_sectors, multiwrite_cb, mcb);
2449
2450 if (acb == NULL) {
2451 // We can only fail the whole thing if no request has been
2452 // submitted yet. Otherwise we'll wait for the submitted AIOs to
2453 // complete and report the error in the callback.
Kevin Wolf453f9a12010-07-02 14:01:21 +02002454 if (i == 0) {
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +01002455 trace_bdrv_aio_multiwrite_earlyfail(mcb);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002456 goto fail;
2457 } else {
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +01002458 trace_bdrv_aio_multiwrite_latefail(mcb, i);
Kevin Wolf7eb58a62010-04-06 18:24:07 +02002459 multiwrite_cb(mcb, -EIO);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002460 break;
2461 }
Kevin Wolf40b4f532009-09-09 17:53:37 +02002462 }
2463 }
2464
Kevin Wolf453f9a12010-07-02 14:01:21 +02002465 /* Complete the dummy request */
2466 multiwrite_cb(mcb, 0);
2467
Kevin Wolf40b4f532009-09-09 17:53:37 +02002468 return 0;
2469
2470fail:
Kevin Wolf453f9a12010-07-02 14:01:21 +02002471 for (i = 0; i < mcb->num_callbacks; i++) {
2472 reqs[i].error = -EIO;
2473 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002474 g_free(mcb);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002475 return -1;
2476}
2477
bellard83f64092006-08-01 16:21:11 +00002478void bdrv_aio_cancel(BlockDriverAIOCB *acb)
pbrookce1a14d2006-08-07 02:38:06 +00002479{
aliguori6bbff9a2009-03-20 18:25:59 +00002480 acb->pool->cancel(acb);
bellard83f64092006-08-01 16:21:11 +00002481}
2482
pbrookce1a14d2006-08-07 02:38:06 +00002483
bellard83f64092006-08-01 16:21:11 +00002484/**************************************************************/
2485/* async block device emulation */
2486
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002487typedef struct BlockDriverAIOCBSync {
2488 BlockDriverAIOCB common;
2489 QEMUBH *bh;
2490 int ret;
2491 /* vector translation state */
2492 QEMUIOVector *qiov;
2493 uint8_t *bounce;
2494 int is_write;
2495} BlockDriverAIOCBSync;
2496
2497static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
2498{
Kevin Wolfb666d232010-05-05 11:44:39 +02002499 BlockDriverAIOCBSync *acb =
2500 container_of(blockacb, BlockDriverAIOCBSync, common);
Dor Laor6a7ad292009-06-01 12:07:23 +03002501 qemu_bh_delete(acb->bh);
Avi Kivity36afc452009-06-23 16:20:36 +03002502 acb->bh = NULL;
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002503 qemu_aio_release(acb);
2504}
2505
2506static AIOPool bdrv_em_aio_pool = {
2507 .aiocb_size = sizeof(BlockDriverAIOCBSync),
2508 .cancel = bdrv_aio_cancel_em,
2509};
2510
bellard83f64092006-08-01 16:21:11 +00002511static void bdrv_aio_bh_cb(void *opaque)
bellardbeac80c2006-06-26 20:08:57 +00002512{
pbrookce1a14d2006-08-07 02:38:06 +00002513 BlockDriverAIOCBSync *acb = opaque;
aliguorif141eaf2009-04-07 18:43:24 +00002514
aliguorif141eaf2009-04-07 18:43:24 +00002515 if (!acb->is_write)
2516 qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
aliguoriceb42de2009-04-07 18:43:28 +00002517 qemu_vfree(acb->bounce);
pbrookce1a14d2006-08-07 02:38:06 +00002518 acb->common.cb(acb->common.opaque, acb->ret);
Dor Laor6a7ad292009-06-01 12:07:23 +03002519 qemu_bh_delete(acb->bh);
Avi Kivity36afc452009-06-23 16:20:36 +03002520 acb->bh = NULL;
pbrookce1a14d2006-08-07 02:38:06 +00002521 qemu_aio_release(acb);
bellardbeac80c2006-06-26 20:08:57 +00002522}
bellardbeac80c2006-06-26 20:08:57 +00002523
aliguorif141eaf2009-04-07 18:43:24 +00002524static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
2525 int64_t sector_num,
2526 QEMUIOVector *qiov,
2527 int nb_sectors,
2528 BlockDriverCompletionFunc *cb,
2529 void *opaque,
2530 int is_write)
2531
bellardea2384d2004-08-01 21:59:26 +00002532{
pbrookce1a14d2006-08-07 02:38:06 +00002533 BlockDriverAIOCBSync *acb;
pbrookce1a14d2006-08-07 02:38:06 +00002534
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002535 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
aliguorif141eaf2009-04-07 18:43:24 +00002536 acb->is_write = is_write;
2537 acb->qiov = qiov;
aliguorie268ca52009-04-22 20:20:00 +00002538 acb->bounce = qemu_blockalign(bs, qiov->size);
aliguorif141eaf2009-04-07 18:43:24 +00002539
pbrookce1a14d2006-08-07 02:38:06 +00002540 if (!acb->bh)
2541 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
aliguorif141eaf2009-04-07 18:43:24 +00002542
2543 if (is_write) {
2544 qemu_iovec_to_buffer(acb->qiov, acb->bounce);
Stefan Hajnoczi1ed20ac2011-10-13 13:08:21 +01002545 acb->ret = bs->drv->bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
aliguorif141eaf2009-04-07 18:43:24 +00002546 } else {
Stefan Hajnoczi1ed20ac2011-10-13 13:08:21 +01002547 acb->ret = bs->drv->bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
aliguorif141eaf2009-04-07 18:43:24 +00002548 }
2549
pbrookce1a14d2006-08-07 02:38:06 +00002550 qemu_bh_schedule(acb->bh);
aliguorif141eaf2009-04-07 18:43:24 +00002551
pbrookce1a14d2006-08-07 02:38:06 +00002552 return &acb->common;
pbrook7a6cba62006-06-04 11:39:07 +00002553}
2554
aliguorif141eaf2009-04-07 18:43:24 +00002555static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
2556 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
pbrookce1a14d2006-08-07 02:38:06 +00002557 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00002558{
aliguorif141eaf2009-04-07 18:43:24 +00002559 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
bellard83f64092006-08-01 16:21:11 +00002560}
2561
aliguorif141eaf2009-04-07 18:43:24 +00002562static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
2563 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2564 BlockDriverCompletionFunc *cb, void *opaque)
2565{
2566 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
2567}
2568
Kevin Wolf68485422011-06-30 10:05:46 +02002569
2570typedef struct BlockDriverAIOCBCoroutine {
2571 BlockDriverAIOCB common;
2572 BlockRequest req;
2573 bool is_write;
2574 QEMUBH* bh;
2575} BlockDriverAIOCBCoroutine;
2576
2577static void bdrv_aio_co_cancel_em(BlockDriverAIOCB *blockacb)
2578{
2579 qemu_aio_flush();
2580}
2581
2582static AIOPool bdrv_em_co_aio_pool = {
2583 .aiocb_size = sizeof(BlockDriverAIOCBCoroutine),
2584 .cancel = bdrv_aio_co_cancel_em,
2585};
2586
Paolo Bonzini35246a62011-10-14 10:41:29 +02002587static void bdrv_co_em_bh(void *opaque)
Kevin Wolf68485422011-06-30 10:05:46 +02002588{
2589 BlockDriverAIOCBCoroutine *acb = opaque;
2590
2591 acb->common.cb(acb->common.opaque, acb->req.error);
2592 qemu_bh_delete(acb->bh);
2593 qemu_aio_release(acb);
2594}
2595
Stefan Hajnoczib2a61372011-10-13 13:08:23 +01002596/* Invoke bdrv_co_do_readv/bdrv_co_do_writev */
2597static void coroutine_fn bdrv_co_do_rw(void *opaque)
2598{
2599 BlockDriverAIOCBCoroutine *acb = opaque;
2600 BlockDriverState *bs = acb->common.bs;
2601
2602 if (!acb->is_write) {
2603 acb->req.error = bdrv_co_do_readv(bs, acb->req.sector,
2604 acb->req.nb_sectors, acb->req.qiov);
2605 } else {
2606 acb->req.error = bdrv_co_do_writev(bs, acb->req.sector,
2607 acb->req.nb_sectors, acb->req.qiov);
2608 }
2609
Paolo Bonzini35246a62011-10-14 10:41:29 +02002610 acb->bh = qemu_bh_new(bdrv_co_em_bh, acb);
Stefan Hajnoczib2a61372011-10-13 13:08:23 +01002611 qemu_bh_schedule(acb->bh);
2612}
2613
Kevin Wolf68485422011-06-30 10:05:46 +02002614static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs,
2615 int64_t sector_num,
2616 QEMUIOVector *qiov,
2617 int nb_sectors,
2618 BlockDriverCompletionFunc *cb,
2619 void *opaque,
Stefan Hajnoczi8c5873d2011-10-13 21:09:28 +01002620 bool is_write)
Kevin Wolf68485422011-06-30 10:05:46 +02002621{
2622 Coroutine *co;
2623 BlockDriverAIOCBCoroutine *acb;
2624
2625 acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque);
2626 acb->req.sector = sector_num;
2627 acb->req.nb_sectors = nb_sectors;
2628 acb->req.qiov = qiov;
2629 acb->is_write = is_write;
2630
Stefan Hajnoczi8c5873d2011-10-13 21:09:28 +01002631 co = qemu_coroutine_create(bdrv_co_do_rw);
Kevin Wolf68485422011-06-30 10:05:46 +02002632 qemu_coroutine_enter(co, acb);
2633
2634 return &acb->common;
2635}
2636
Paolo Bonzini07f07612011-10-17 12:32:12 +02002637static void coroutine_fn bdrv_aio_flush_co_entry(void *opaque)
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002638{
Paolo Bonzini07f07612011-10-17 12:32:12 +02002639 BlockDriverAIOCBCoroutine *acb = opaque;
2640 BlockDriverState *bs = acb->common.bs;
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002641
Paolo Bonzini07f07612011-10-17 12:32:12 +02002642 acb->req.error = bdrv_co_flush(bs);
2643 acb->bh = qemu_bh_new(bdrv_co_em_bh, acb);
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002644 qemu_bh_schedule(acb->bh);
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002645}
2646
Paolo Bonzini07f07612011-10-17 12:32:12 +02002647BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
Alexander Graf016f5cf2010-05-26 17:51:49 +02002648 BlockDriverCompletionFunc *cb, void *opaque)
2649{
Paolo Bonzini07f07612011-10-17 12:32:12 +02002650 trace_bdrv_aio_flush(bs, opaque);
Alexander Graf016f5cf2010-05-26 17:51:49 +02002651
Paolo Bonzini07f07612011-10-17 12:32:12 +02002652 Coroutine *co;
2653 BlockDriverAIOCBCoroutine *acb;
Alexander Graf016f5cf2010-05-26 17:51:49 +02002654
Paolo Bonzini07f07612011-10-17 12:32:12 +02002655 acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque);
2656 co = qemu_coroutine_create(bdrv_aio_flush_co_entry);
2657 qemu_coroutine_enter(co, acb);
Alexander Graf016f5cf2010-05-26 17:51:49 +02002658
Alexander Graf016f5cf2010-05-26 17:51:49 +02002659 return &acb->common;
2660}
2661
Paolo Bonzini4265d622011-10-17 12:32:14 +02002662static void coroutine_fn bdrv_aio_discard_co_entry(void *opaque)
2663{
2664 BlockDriverAIOCBCoroutine *acb = opaque;
2665 BlockDriverState *bs = acb->common.bs;
2666
2667 acb->req.error = bdrv_co_discard(bs, acb->req.sector, acb->req.nb_sectors);
2668 acb->bh = qemu_bh_new(bdrv_co_em_bh, acb);
2669 qemu_bh_schedule(acb->bh);
2670}
2671
2672BlockDriverAIOCB *bdrv_aio_discard(BlockDriverState *bs,
2673 int64_t sector_num, int nb_sectors,
2674 BlockDriverCompletionFunc *cb, void *opaque)
2675{
2676 Coroutine *co;
2677 BlockDriverAIOCBCoroutine *acb;
2678
2679 trace_bdrv_aio_discard(bs, sector_num, nb_sectors, opaque);
2680
2681 acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque);
2682 acb->req.sector = sector_num;
2683 acb->req.nb_sectors = nb_sectors;
2684 co = qemu_coroutine_create(bdrv_aio_discard_co_entry);
2685 qemu_coroutine_enter(co, acb);
2686
2687 return &acb->common;
2688}
2689
bellardea2384d2004-08-01 21:59:26 +00002690void bdrv_init(void)
2691{
Anthony Liguori5efa9d52009-05-09 17:03:42 -05002692 module_call_init(MODULE_INIT_BLOCK);
bellardea2384d2004-08-01 21:59:26 +00002693}
pbrookce1a14d2006-08-07 02:38:06 +00002694
Markus Armbrustereb852012009-10-27 18:41:44 +01002695void bdrv_init_with_whitelist(void)
2696{
2697 use_bdrv_whitelist = 1;
2698 bdrv_init();
2699}
2700
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002701void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
2702 BlockDriverCompletionFunc *cb, void *opaque)
aliguori6bbff9a2009-03-20 18:25:59 +00002703{
pbrookce1a14d2006-08-07 02:38:06 +00002704 BlockDriverAIOCB *acb;
2705
aliguori6bbff9a2009-03-20 18:25:59 +00002706 if (pool->free_aiocb) {
2707 acb = pool->free_aiocb;
2708 pool->free_aiocb = acb->next;
pbrookce1a14d2006-08-07 02:38:06 +00002709 } else {
Anthony Liguori7267c092011-08-20 22:09:37 -05002710 acb = g_malloc0(pool->aiocb_size);
aliguori6bbff9a2009-03-20 18:25:59 +00002711 acb->pool = pool;
pbrookce1a14d2006-08-07 02:38:06 +00002712 }
2713 acb->bs = bs;
2714 acb->cb = cb;
2715 acb->opaque = opaque;
2716 return acb;
2717}
2718
2719void qemu_aio_release(void *p)
2720{
aliguori6bbff9a2009-03-20 18:25:59 +00002721 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
2722 AIOPool *pool = acb->pool;
2723 acb->next = pool->free_aiocb;
2724 pool->free_aiocb = acb;
pbrookce1a14d2006-08-07 02:38:06 +00002725}
bellard19cb3732006-08-19 11:45:59 +00002726
2727/**************************************************************/
Kevin Wolff9f05dc2011-07-15 13:50:26 +02002728/* Coroutine block device emulation */
2729
2730typedef struct CoroutineIOCompletion {
2731 Coroutine *coroutine;
2732 int ret;
2733} CoroutineIOCompletion;
2734
2735static void bdrv_co_io_em_complete(void *opaque, int ret)
2736{
2737 CoroutineIOCompletion *co = opaque;
2738
2739 co->ret = ret;
2740 qemu_coroutine_enter(co->coroutine, NULL);
2741}
2742
2743static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num,
2744 int nb_sectors, QEMUIOVector *iov,
2745 bool is_write)
2746{
2747 CoroutineIOCompletion co = {
2748 .coroutine = qemu_coroutine_self(),
2749 };
2750 BlockDriverAIOCB *acb;
2751
2752 if (is_write) {
Stefan Hajnoczia652d162011-10-05 17:17:02 +01002753 acb = bs->drv->bdrv_aio_writev(bs, sector_num, iov, nb_sectors,
2754 bdrv_co_io_em_complete, &co);
Kevin Wolff9f05dc2011-07-15 13:50:26 +02002755 } else {
Stefan Hajnoczia652d162011-10-05 17:17:02 +01002756 acb = bs->drv->bdrv_aio_readv(bs, sector_num, iov, nb_sectors,
2757 bdrv_co_io_em_complete, &co);
Kevin Wolff9f05dc2011-07-15 13:50:26 +02002758 }
2759
Stefan Hajnoczi59370aa2011-09-30 17:34:58 +01002760 trace_bdrv_co_io_em(bs, sector_num, nb_sectors, is_write, acb);
Kevin Wolff9f05dc2011-07-15 13:50:26 +02002761 if (!acb) {
2762 return -EIO;
2763 }
2764 qemu_coroutine_yield();
2765
2766 return co.ret;
2767}
2768
2769static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs,
2770 int64_t sector_num, int nb_sectors,
2771 QEMUIOVector *iov)
2772{
2773 return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false);
2774}
2775
2776static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs,
2777 int64_t sector_num, int nb_sectors,
2778 QEMUIOVector *iov)
2779{
2780 return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true);
2781}
2782
Paolo Bonzini07f07612011-10-17 12:32:12 +02002783static void coroutine_fn bdrv_flush_co_entry(void *opaque)
Kevin Wolfe7a8a782011-07-15 16:05:00 +02002784{
Paolo Bonzini07f07612011-10-17 12:32:12 +02002785 RwCo *rwco = opaque;
Kevin Wolfe7a8a782011-07-15 16:05:00 +02002786
Paolo Bonzini07f07612011-10-17 12:32:12 +02002787 rwco->ret = bdrv_co_flush(rwco->bs);
2788}
2789
2790int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
2791{
Kevin Wolfeb489bb2011-11-10 18:10:11 +01002792 int ret;
2793
Kevin Wolfca716362011-11-10 18:13:59 +01002794 if (!bs->drv) {
Paolo Bonzini07f07612011-10-17 12:32:12 +02002795 return 0;
Kevin Wolfeb489bb2011-11-10 18:10:11 +01002796 }
2797
Kevin Wolfca716362011-11-10 18:13:59 +01002798 /* Write back cached data to the OS even with cache=unsafe */
Kevin Wolfeb489bb2011-11-10 18:10:11 +01002799 if (bs->drv->bdrv_co_flush_to_os) {
2800 ret = bs->drv->bdrv_co_flush_to_os(bs);
2801 if (ret < 0) {
2802 return ret;
2803 }
2804 }
2805
Kevin Wolfca716362011-11-10 18:13:59 +01002806 /* But don't actually force it to the disk with cache=unsafe */
2807 if (bs->open_flags & BDRV_O_NO_FLUSH) {
2808 return 0;
2809 }
2810
Kevin Wolfeb489bb2011-11-10 18:10:11 +01002811 if (bs->drv->bdrv_co_flush_to_disk) {
Kevin Wolfc68b89a2011-11-10 17:25:44 +01002812 return bs->drv->bdrv_co_flush_to_disk(bs);
Paolo Bonzini07f07612011-10-17 12:32:12 +02002813 } else if (bs->drv->bdrv_aio_flush) {
2814 BlockDriverAIOCB *acb;
2815 CoroutineIOCompletion co = {
2816 .coroutine = qemu_coroutine_self(),
2817 };
2818
2819 acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co);
2820 if (acb == NULL) {
2821 return -EIO;
2822 } else {
2823 qemu_coroutine_yield();
2824 return co.ret;
2825 }
Paolo Bonzini07f07612011-10-17 12:32:12 +02002826 } else {
2827 /*
2828 * Some block drivers always operate in either writethrough or unsafe
2829 * mode and don't support bdrv_flush therefore. Usually qemu doesn't
2830 * know how the server works (because the behaviour is hardcoded or
2831 * depends on server-side configuration), so we can't ensure that
2832 * everything is safe on disk. Returning an error doesn't work because
2833 * that would break guests even if the server operates in writethrough
2834 * mode.
2835 *
2836 * Let's hope the user knows what he's doing.
2837 */
2838 return 0;
Kevin Wolfe7a8a782011-07-15 16:05:00 +02002839 }
Paolo Bonzini07f07612011-10-17 12:32:12 +02002840}
2841
2842int bdrv_flush(BlockDriverState *bs)
2843{
2844 Coroutine *co;
2845 RwCo rwco = {
2846 .bs = bs,
2847 .ret = NOT_DONE,
2848 };
2849
2850 if (qemu_in_coroutine()) {
2851 /* Fast-path if already in coroutine context */
2852 bdrv_flush_co_entry(&rwco);
2853 } else {
2854 co = qemu_coroutine_create(bdrv_flush_co_entry);
2855 qemu_coroutine_enter(co, &rwco);
2856 while (rwco.ret == NOT_DONE) {
2857 qemu_aio_wait();
2858 }
2859 }
2860
2861 return rwco.ret;
Kevin Wolfe7a8a782011-07-15 16:05:00 +02002862}
2863
Paolo Bonzini4265d622011-10-17 12:32:14 +02002864static void coroutine_fn bdrv_discard_co_entry(void *opaque)
2865{
2866 RwCo *rwco = opaque;
2867
2868 rwco->ret = bdrv_co_discard(rwco->bs, rwco->sector_num, rwco->nb_sectors);
2869}
2870
2871int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num,
2872 int nb_sectors)
2873{
2874 if (!bs->drv) {
2875 return -ENOMEDIUM;
2876 } else if (bdrv_check_request(bs, sector_num, nb_sectors)) {
2877 return -EIO;
2878 } else if (bs->read_only) {
2879 return -EROFS;
2880 } else if (bs->drv->bdrv_co_discard) {
2881 return bs->drv->bdrv_co_discard(bs, sector_num, nb_sectors);
2882 } else if (bs->drv->bdrv_aio_discard) {
2883 BlockDriverAIOCB *acb;
2884 CoroutineIOCompletion co = {
2885 .coroutine = qemu_coroutine_self(),
2886 };
2887
2888 acb = bs->drv->bdrv_aio_discard(bs, sector_num, nb_sectors,
2889 bdrv_co_io_em_complete, &co);
2890 if (acb == NULL) {
2891 return -EIO;
2892 } else {
2893 qemu_coroutine_yield();
2894 return co.ret;
2895 }
Paolo Bonzini4265d622011-10-17 12:32:14 +02002896 } else {
2897 return 0;
2898 }
2899}
2900
2901int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors)
2902{
2903 Coroutine *co;
2904 RwCo rwco = {
2905 .bs = bs,
2906 .sector_num = sector_num,
2907 .nb_sectors = nb_sectors,
2908 .ret = NOT_DONE,
2909 };
2910
2911 if (qemu_in_coroutine()) {
2912 /* Fast-path if already in coroutine context */
2913 bdrv_discard_co_entry(&rwco);
2914 } else {
2915 co = qemu_coroutine_create(bdrv_discard_co_entry);
2916 qemu_coroutine_enter(co, &rwco);
2917 while (rwco.ret == NOT_DONE) {
2918 qemu_aio_wait();
2919 }
2920 }
2921
2922 return rwco.ret;
2923}
2924
Kevin Wolff9f05dc2011-07-15 13:50:26 +02002925/**************************************************************/
bellard19cb3732006-08-19 11:45:59 +00002926/* removable device support */
2927
2928/**
2929 * Return TRUE if the media is present
2930 */
2931int bdrv_is_inserted(BlockDriverState *bs)
2932{
2933 BlockDriver *drv = bs->drv;
Markus Armbrustera1aff5b2011-09-06 18:58:41 +02002934
bellard19cb3732006-08-19 11:45:59 +00002935 if (!drv)
2936 return 0;
2937 if (!drv->bdrv_is_inserted)
Markus Armbrustera1aff5b2011-09-06 18:58:41 +02002938 return 1;
2939 return drv->bdrv_is_inserted(bs);
bellard19cb3732006-08-19 11:45:59 +00002940}
2941
2942/**
Markus Armbruster8e49ca42011-08-03 15:08:08 +02002943 * Return whether the media changed since the last call to this
2944 * function, or -ENOTSUP if we don't know. Most drivers don't know.
bellard19cb3732006-08-19 11:45:59 +00002945 */
2946int bdrv_media_changed(BlockDriverState *bs)
2947{
2948 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +00002949
Markus Armbruster8e49ca42011-08-03 15:08:08 +02002950 if (drv && drv->bdrv_media_changed) {
2951 return drv->bdrv_media_changed(bs);
2952 }
2953 return -ENOTSUP;
bellard19cb3732006-08-19 11:45:59 +00002954}
2955
2956/**
2957 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
2958 */
Markus Armbrusterfdec4402011-09-06 18:58:45 +02002959void bdrv_eject(BlockDriverState *bs, int eject_flag)
bellard19cb3732006-08-19 11:45:59 +00002960{
2961 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +00002962
Markus Armbruster822e1cd2011-07-20 18:23:42 +02002963 if (drv && drv->bdrv_eject) {
2964 drv->bdrv_eject(bs, eject_flag);
bellard19cb3732006-08-19 11:45:59 +00002965 }
bellard19cb3732006-08-19 11:45:59 +00002966}
2967
bellard19cb3732006-08-19 11:45:59 +00002968/**
2969 * Lock or unlock the media (if it is locked, the user won't be able
2970 * to eject it manually).
2971 */
Markus Armbruster025e8492011-09-06 18:58:47 +02002972void bdrv_lock_medium(BlockDriverState *bs, bool locked)
bellard19cb3732006-08-19 11:45:59 +00002973{
2974 BlockDriver *drv = bs->drv;
2975
Markus Armbruster025e8492011-09-06 18:58:47 +02002976 trace_bdrv_lock_medium(bs, locked);
Stefan Hajnoczib8c6d092011-03-29 20:04:40 +01002977
Markus Armbruster025e8492011-09-06 18:58:47 +02002978 if (drv && drv->bdrv_lock_medium) {
2979 drv->bdrv_lock_medium(bs, locked);
bellard19cb3732006-08-19 11:45:59 +00002980 }
2981}
ths985a03b2007-12-24 16:10:43 +00002982
2983/* needed for generic scsi interface */
2984
2985int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
2986{
2987 BlockDriver *drv = bs->drv;
2988
2989 if (drv && drv->bdrv_ioctl)
2990 return drv->bdrv_ioctl(bs, req, buf);
2991 return -ENOTSUP;
2992}
aliguori7d780662009-03-12 19:57:08 +00002993
aliguori221f7152009-03-28 17:28:41 +00002994BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
2995 unsigned long int req, void *buf,
2996 BlockDriverCompletionFunc *cb, void *opaque)
aliguori7d780662009-03-12 19:57:08 +00002997{
aliguori221f7152009-03-28 17:28:41 +00002998 BlockDriver *drv = bs->drv;
aliguori7d780662009-03-12 19:57:08 +00002999
aliguori221f7152009-03-28 17:28:41 +00003000 if (drv && drv->bdrv_aio_ioctl)
3001 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
3002 return NULL;
aliguori7d780662009-03-12 19:57:08 +00003003}
aliguorie268ca52009-04-22 20:20:00 +00003004
Markus Armbruster7b6f9302011-09-06 18:58:56 +02003005void bdrv_set_buffer_alignment(BlockDriverState *bs, int align)
3006{
3007 bs->buffer_alignment = align;
3008}
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02003009
aliguorie268ca52009-04-22 20:20:00 +00003010void *qemu_blockalign(BlockDriverState *bs, size_t size)
3011{
3012 return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
3013}
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02003014
3015void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable)
3016{
3017 int64_t bitmap_size;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01003018
Liran Schouraaa0eb72010-01-26 10:31:48 +02003019 bs->dirty_count = 0;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01003020 if (enable) {
Jan Kiszkac6d22832009-11-30 18:21:20 +01003021 if (!bs->dirty_bitmap) {
3022 bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
3023 BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
3024 bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01003025
Anthony Liguori7267c092011-08-20 22:09:37 -05003026 bs->dirty_bitmap = g_malloc0(bitmap_size);
Jan Kiszkaa55eb922009-11-30 18:21:19 +01003027 }
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02003028 } else {
Jan Kiszkac6d22832009-11-30 18:21:20 +01003029 if (bs->dirty_bitmap) {
Anthony Liguori7267c092011-08-20 22:09:37 -05003030 g_free(bs->dirty_bitmap);
Jan Kiszkac6d22832009-11-30 18:21:20 +01003031 bs->dirty_bitmap = NULL;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01003032 }
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02003033 }
3034}
3035
3036int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
3037{
Jan Kiszka6ea44302009-11-30 18:21:19 +01003038 int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01003039
Jan Kiszkac6d22832009-11-30 18:21:20 +01003040 if (bs->dirty_bitmap &&
3041 (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) {
Marcelo Tosatti6d59fec2010-11-08 17:02:54 -02003042 return !!(bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] &
3043 (1UL << (chunk % (sizeof(unsigned long) * 8))));
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02003044 } else {
3045 return 0;
3046 }
3047}
3048
Jan Kiszkaa55eb922009-11-30 18:21:19 +01003049void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
3050 int nr_sectors)
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02003051{
3052 set_dirty_bitmap(bs, cur_sector, nr_sectors, 0);
3053}
Liran Schouraaa0eb72010-01-26 10:31:48 +02003054
3055int64_t bdrv_get_dirty_count(BlockDriverState *bs)
3056{
3057 return bs->dirty_count;
3058}
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003059
Marcelo Tosattidb593f22011-01-26 12:12:34 -02003060void bdrv_set_in_use(BlockDriverState *bs, int in_use)
3061{
3062 assert(bs->in_use != in_use);
3063 bs->in_use = in_use;
3064}
3065
3066int bdrv_in_use(BlockDriverState *bs)
3067{
3068 return bs->in_use;
3069}
3070
Luiz Capitulino28a72822011-09-26 17:43:50 -03003071void bdrv_iostatus_enable(BlockDriverState *bs)
3072{
Luiz Capitulinod6bf2792011-10-14 17:11:23 -03003073 bs->iostatus_enabled = true;
Luiz Capitulino58e21ef2011-10-14 17:22:24 -03003074 bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
Luiz Capitulino28a72822011-09-26 17:43:50 -03003075}
3076
3077/* The I/O status is only enabled if the drive explicitly
3078 * enables it _and_ the VM is configured to stop on errors */
3079bool bdrv_iostatus_is_enabled(const BlockDriverState *bs)
3080{
Luiz Capitulinod6bf2792011-10-14 17:11:23 -03003081 return (bs->iostatus_enabled &&
Luiz Capitulino28a72822011-09-26 17:43:50 -03003082 (bs->on_write_error == BLOCK_ERR_STOP_ENOSPC ||
3083 bs->on_write_error == BLOCK_ERR_STOP_ANY ||
3084 bs->on_read_error == BLOCK_ERR_STOP_ANY));
3085}
3086
3087void bdrv_iostatus_disable(BlockDriverState *bs)
3088{
Luiz Capitulinod6bf2792011-10-14 17:11:23 -03003089 bs->iostatus_enabled = false;
Luiz Capitulino28a72822011-09-26 17:43:50 -03003090}
3091
3092void bdrv_iostatus_reset(BlockDriverState *bs)
3093{
3094 if (bdrv_iostatus_is_enabled(bs)) {
Luiz Capitulino58e21ef2011-10-14 17:22:24 -03003095 bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
Luiz Capitulino28a72822011-09-26 17:43:50 -03003096 }
3097}
3098
3099/* XXX: Today this is set by device models because it makes the implementation
3100 quite simple. However, the block layer knows about the error, so it's
3101 possible to implement this without device models being involved */
3102void bdrv_iostatus_set_err(BlockDriverState *bs, int error)
3103{
Luiz Capitulino58e21ef2011-10-14 17:22:24 -03003104 if (bdrv_iostatus_is_enabled(bs) &&
3105 bs->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
Luiz Capitulino28a72822011-09-26 17:43:50 -03003106 assert(error >= 0);
Luiz Capitulino58e21ef2011-10-14 17:22:24 -03003107 bs->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
3108 BLOCK_DEVICE_IO_STATUS_FAILED;
Luiz Capitulino28a72822011-09-26 17:43:50 -03003109 }
3110}
3111
Christoph Hellwiga597e792011-08-25 08:26:01 +02003112void
3113bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, int64_t bytes,
3114 enum BlockAcctType type)
3115{
3116 assert(type < BDRV_MAX_IOTYPE);
3117
3118 cookie->bytes = bytes;
Christoph Hellwigc488c7f2011-08-25 08:26:10 +02003119 cookie->start_time_ns = get_clock();
Christoph Hellwiga597e792011-08-25 08:26:01 +02003120 cookie->type = type;
3121}
3122
3123void
3124bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie)
3125{
3126 assert(cookie->type < BDRV_MAX_IOTYPE);
3127
3128 bs->nr_bytes[cookie->type] += cookie->bytes;
3129 bs->nr_ops[cookie->type]++;
Christoph Hellwigc488c7f2011-08-25 08:26:10 +02003130 bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns;
Christoph Hellwiga597e792011-08-25 08:26:01 +02003131}
3132
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003133int bdrv_img_create(const char *filename, const char *fmt,
3134 const char *base_filename, const char *base_fmt,
3135 char *options, uint64_t img_size, int flags)
3136{
3137 QEMUOptionParameter *param = NULL, *create_options = NULL;
Kevin Wolfd2208942011-06-01 14:03:31 +02003138 QEMUOptionParameter *backing_fmt, *backing_file, *size;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003139 BlockDriverState *bs = NULL;
3140 BlockDriver *drv, *proto_drv;
Stefan Hajnoczi96df67d2011-01-24 09:32:20 +00003141 BlockDriver *backing_drv = NULL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003142 int ret = 0;
3143
3144 /* Find driver and parse its options */
3145 drv = bdrv_find_format(fmt);
3146 if (!drv) {
3147 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4f70f242010-12-16 13:52:18 +01003148 ret = -EINVAL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003149 goto out;
3150 }
3151
3152 proto_drv = bdrv_find_protocol(filename);
3153 if (!proto_drv) {
3154 error_report("Unknown protocol '%s'", filename);
Jes Sorensen4f70f242010-12-16 13:52:18 +01003155 ret = -EINVAL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003156 goto out;
3157 }
3158
3159 create_options = append_option_parameters(create_options,
3160 drv->create_options);
3161 create_options = append_option_parameters(create_options,
3162 proto_drv->create_options);
3163
3164 /* Create parameter list with default values */
3165 param = parse_option_parameters("", create_options, param);
3166
3167 set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size);
3168
3169 /* Parse -o options */
3170 if (options) {
3171 param = parse_option_parameters(options, create_options, param);
3172 if (param == NULL) {
3173 error_report("Invalid options for file format '%s'.", fmt);
Jes Sorensen4f70f242010-12-16 13:52:18 +01003174 ret = -EINVAL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003175 goto out;
3176 }
3177 }
3178
3179 if (base_filename) {
3180 if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE,
3181 base_filename)) {
3182 error_report("Backing file not supported for file format '%s'",
3183 fmt);
Jes Sorensen4f70f242010-12-16 13:52:18 +01003184 ret = -EINVAL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003185 goto out;
3186 }
3187 }
3188
3189 if (base_fmt) {
3190 if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) {
3191 error_report("Backing file format not supported for file "
3192 "format '%s'", fmt);
Jes Sorensen4f70f242010-12-16 13:52:18 +01003193 ret = -EINVAL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003194 goto out;
3195 }
3196 }
3197
Jes Sorensen792da932010-12-16 13:52:17 +01003198 backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
3199 if (backing_file && backing_file->value.s) {
3200 if (!strcmp(filename, backing_file->value.s)) {
3201 error_report("Error: Trying to create an image with the "
3202 "same filename as the backing file");
Jes Sorensen4f70f242010-12-16 13:52:18 +01003203 ret = -EINVAL;
Jes Sorensen792da932010-12-16 13:52:17 +01003204 goto out;
3205 }
3206 }
3207
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003208 backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT);
3209 if (backing_fmt && backing_fmt->value.s) {
Stefan Hajnoczi96df67d2011-01-24 09:32:20 +00003210 backing_drv = bdrv_find_format(backing_fmt->value.s);
3211 if (!backing_drv) {
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003212 error_report("Unknown backing file format '%s'",
3213 backing_fmt->value.s);
Jes Sorensen4f70f242010-12-16 13:52:18 +01003214 ret = -EINVAL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003215 goto out;
3216 }
3217 }
3218
3219 // The size for the image must always be specified, with one exception:
3220 // If we are using a backing file, we can obtain the size from there
Kevin Wolfd2208942011-06-01 14:03:31 +02003221 size = get_option_parameter(param, BLOCK_OPT_SIZE);
3222 if (size && size->value.n == -1) {
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003223 if (backing_file && backing_file->value.s) {
3224 uint64_t size;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003225 char buf[32];
3226
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003227 bs = bdrv_new("");
3228
Stefan Hajnoczi96df67d2011-01-24 09:32:20 +00003229 ret = bdrv_open(bs, backing_file->value.s, flags, backing_drv);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003230 if (ret < 0) {
Stefan Hajnoczi96df67d2011-01-24 09:32:20 +00003231 error_report("Could not open '%s'", backing_file->value.s);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003232 goto out;
3233 }
3234 bdrv_get_geometry(bs, &size);
3235 size *= 512;
3236
3237 snprintf(buf, sizeof(buf), "%" PRId64, size);
3238 set_option_parameter(param, BLOCK_OPT_SIZE, buf);
3239 } else {
3240 error_report("Image creation needs a size parameter");
Jes Sorensen4f70f242010-12-16 13:52:18 +01003241 ret = -EINVAL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003242 goto out;
3243 }
3244 }
3245
3246 printf("Formatting '%s', fmt=%s ", filename, fmt);
3247 print_option_parameters(param);
3248 puts("");
3249
3250 ret = bdrv_create(drv, filename, param);
3251
3252 if (ret < 0) {
3253 if (ret == -ENOTSUP) {
3254 error_report("Formatting or formatting option not supported for "
3255 "file format '%s'", fmt);
3256 } else if (ret == -EFBIG) {
3257 error_report("The image size is too large for file format '%s'",
3258 fmt);
3259 } else {
3260 error_report("%s: error while creating %s: %s", filename, fmt,
3261 strerror(-ret));
3262 }
3263 }
3264
3265out:
3266 free_option_parameters(create_options);
3267 free_option_parameters(param);
3268
3269 if (bs) {
3270 bdrv_delete(bs);
3271 }
Jes Sorensen4f70f242010-12-16 13:52:18 +01003272
3273 return ret;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003274}