blob: 8cb41c0b41f8f08998f5c499496f521d014df220 [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"
Zhi Yong Wu0563e192011-11-03 16:57:25 +080033#include "qemu-timer.h"
bellardfc01f7e2003-06-30 10:03:06 +000034
Juan Quintela71e72a12009-07-27 16:12:56 +020035#ifdef CONFIG_BSD
bellard7674e7b2005-04-26 21:59:26 +000036#include <sys/types.h>
37#include <sys/stat.h>
38#include <sys/ioctl.h>
Blue Swirl72cf2d42009-09-12 07:36:22 +000039#include <sys/queue.h>
blueswir1c5e97232009-03-07 20:06:23 +000040#ifndef __DragonFly__
bellard7674e7b2005-04-26 21:59:26 +000041#include <sys/disk.h>
42#endif
blueswir1c5e97232009-03-07 20:06:23 +000043#endif
bellard7674e7b2005-04-26 21:59:26 +000044
aliguori49dc7682009-03-08 16:26:59 +000045#ifdef _WIN32
46#include <windows.h>
47#endif
48
Stefan Hajnoczi1c9805a2011-10-13 13:08:22 +010049#define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
50
Markus Armbruster7d4b4ba2011-09-06 18:58:59 +020051static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load);
aliguorif141eaf2009-04-07 18:43:24 +000052static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
53 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
aliguoric87c0672009-04-07 18:43:20 +000054 BlockDriverCompletionFunc *cb, void *opaque);
aliguorif141eaf2009-04-07 18:43:24 +000055static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
56 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
pbrookce1a14d2006-08-07 02:38:06 +000057 BlockDriverCompletionFunc *cb, void *opaque);
Kevin Wolff9f05dc2011-07-15 13:50:26 +020058static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs,
59 int64_t sector_num, int nb_sectors,
60 QEMUIOVector *iov);
61static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs,
62 int64_t sector_num, int nb_sectors,
63 QEMUIOVector *iov);
Stefan Hajnoczic5fbe572011-10-05 17:17:03 +010064static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs,
65 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
Stefan Hajnoczi1c9805a2011-10-13 13:08:22 +010066static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs,
67 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
Stefan Hajnoczib2a61372011-10-13 13:08:23 +010068static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs,
69 int64_t sector_num,
70 QEMUIOVector *qiov,
71 int nb_sectors,
72 BlockDriverCompletionFunc *cb,
73 void *opaque,
Stefan Hajnoczi8c5873d2011-10-13 21:09:28 +010074 bool is_write);
Stefan Hajnoczib2a61372011-10-13 13:08:23 +010075static void coroutine_fn bdrv_co_do_rw(void *opaque);
bellardec530c82006-04-25 22:36:06 +000076
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +010077static QTAILQ_HEAD(, BlockDriverState) bdrv_states =
78 QTAILQ_HEAD_INITIALIZER(bdrv_states);
blueswir17ee930d2008-09-17 19:04:14 +000079
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +010080static QLIST_HEAD(, BlockDriver) bdrv_drivers =
81 QLIST_HEAD_INITIALIZER(bdrv_drivers);
bellardea2384d2004-08-01 21:59:26 +000082
Markus Armbrusterf9092b12010-06-25 10:33:39 +020083/* The device to use for VM snapshots */
84static BlockDriverState *bs_snapshots;
85
Markus Armbrustereb852012009-10-27 18:41:44 +010086/* If non-zero, use only whitelisted block drivers */
87static int use_bdrv_whitelist;
88
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +000089#ifdef _WIN32
90static int is_windows_drive_prefix(const char *filename)
91{
92 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
93 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
94 filename[1] == ':');
95}
96
97int is_windows_drive(const char *filename)
98{
99 if (is_windows_drive_prefix(filename) &&
100 filename[2] == '\0')
101 return 1;
102 if (strstart(filename, "\\\\.\\", NULL) ||
103 strstart(filename, "//./", NULL))
104 return 1;
105 return 0;
106}
107#endif
108
Zhi Yong Wu0563e192011-11-03 16:57:25 +0800109/* throttling disk I/O limits */
110static void bdrv_block_timer(void *opaque)
111{
112 BlockDriverState *bs = opaque;
113
114 qemu_co_queue_next(&bs->throttled_reqs);
115}
116
117void bdrv_io_limits_enable(BlockDriverState *bs)
118{
119 qemu_co_queue_init(&bs->throttled_reqs);
120 bs->block_timer = qemu_new_timer_ns(vm_clock, bdrv_block_timer, bs);
121 bs->slice_time = 5 * BLOCK_IO_SLICE_TIME;
122 bs->slice_start = qemu_get_clock_ns(vm_clock);
123 bs->slice_end = bs->slice_start + bs->slice_time;
124 memset(&bs->io_base, 0, sizeof(bs->io_base));
125 bs->io_limits_enabled = true;
126}
127
128bool bdrv_io_limits_enabled(BlockDriverState *bs)
129{
130 BlockIOLimit *io_limits = &bs->io_limits;
131 return io_limits->bps[BLOCK_IO_LIMIT_READ]
132 || io_limits->bps[BLOCK_IO_LIMIT_WRITE]
133 || io_limits->bps[BLOCK_IO_LIMIT_TOTAL]
134 || io_limits->iops[BLOCK_IO_LIMIT_READ]
135 || io_limits->iops[BLOCK_IO_LIMIT_WRITE]
136 || io_limits->iops[BLOCK_IO_LIMIT_TOTAL];
137}
138
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000139/* check if the path starts with "<protocol>:" */
140static int path_has_protocol(const char *path)
141{
142#ifdef _WIN32
143 if (is_windows_drive(path) ||
144 is_windows_drive_prefix(path)) {
145 return 0;
146 }
147#endif
148
149 return strchr(path, ':') != NULL;
150}
151
bellard83f64092006-08-01 16:21:11 +0000152int path_is_absolute(const char *path)
153{
154 const char *p;
bellard21664422007-01-07 18:22:37 +0000155#ifdef _WIN32
156 /* specific case for names like: "\\.\d:" */
157 if (*path == '/' || *path == '\\')
158 return 1;
159#endif
bellard83f64092006-08-01 16:21:11 +0000160 p = strchr(path, ':');
161 if (p)
162 p++;
163 else
164 p = path;
bellard3b9f94e2007-01-07 17:27:07 +0000165#ifdef _WIN32
166 return (*p == '/' || *p == '\\');
167#else
168 return (*p == '/');
169#endif
bellard83f64092006-08-01 16:21:11 +0000170}
171
172/* if filename is absolute, just copy it to dest. Otherwise, build a
173 path to it by considering it is relative to base_path. URL are
174 supported. */
175void path_combine(char *dest, int dest_size,
176 const char *base_path,
177 const char *filename)
178{
179 const char *p, *p1;
180 int len;
181
182 if (dest_size <= 0)
183 return;
184 if (path_is_absolute(filename)) {
185 pstrcpy(dest, dest_size, filename);
186 } else {
187 p = strchr(base_path, ':');
188 if (p)
189 p++;
190 else
191 p = base_path;
bellard3b9f94e2007-01-07 17:27:07 +0000192 p1 = strrchr(base_path, '/');
193#ifdef _WIN32
194 {
195 const char *p2;
196 p2 = strrchr(base_path, '\\');
197 if (!p1 || p2 > p1)
198 p1 = p2;
199 }
200#endif
bellard83f64092006-08-01 16:21:11 +0000201 if (p1)
202 p1++;
203 else
204 p1 = base_path;
205 if (p1 > p)
206 p = p1;
207 len = p - base_path;
208 if (len > dest_size - 1)
209 len = dest_size - 1;
210 memcpy(dest, base_path, len);
211 dest[len] = '\0';
212 pstrcat(dest, dest_size, filename);
213 }
214}
215
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500216void bdrv_register(BlockDriver *bdrv)
bellardea2384d2004-08-01 21:59:26 +0000217{
Stefan Hajnoczi8c5873d2011-10-13 21:09:28 +0100218 /* Block drivers without coroutine functions need emulation */
219 if (!bdrv->bdrv_co_readv) {
Kevin Wolff9f05dc2011-07-15 13:50:26 +0200220 bdrv->bdrv_co_readv = bdrv_co_readv_em;
221 bdrv->bdrv_co_writev = bdrv_co_writev_em;
222
Stefan Hajnoczif8c35c12011-10-13 21:09:31 +0100223 /* bdrv_co_readv_em()/brdv_co_writev_em() work in terms of aio, so if
224 * the block driver lacks aio we need to emulate that too.
225 */
Kevin Wolff9f05dc2011-07-15 13:50:26 +0200226 if (!bdrv->bdrv_aio_readv) {
227 /* add AIO emulation layer */
228 bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
229 bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
Kevin Wolff9f05dc2011-07-15 13:50:26 +0200230 }
bellard83f64092006-08-01 16:21:11 +0000231 }
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +0200232
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100233 QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
bellardea2384d2004-08-01 21:59:26 +0000234}
bellardb3380822004-03-14 21:38:54 +0000235
236/* create a new block device (by default it is empty) */
237BlockDriverState *bdrv_new(const char *device_name)
bellardfc01f7e2003-06-30 10:03:06 +0000238{
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +0100239 BlockDriverState *bs;
bellardb3380822004-03-14 21:38:54 +0000240
Anthony Liguori7267c092011-08-20 22:09:37 -0500241 bs = g_malloc0(sizeof(BlockDriverState));
bellardb3380822004-03-14 21:38:54 +0000242 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
bellardea2384d2004-08-01 21:59:26 +0000243 if (device_name[0] != '\0') {
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +0100244 QTAILQ_INSERT_TAIL(&bdrv_states, bs, list);
bellardea2384d2004-08-01 21:59:26 +0000245 }
Luiz Capitulino28a72822011-09-26 17:43:50 -0300246 bdrv_iostatus_disable(bs);
bellardb3380822004-03-14 21:38:54 +0000247 return bs;
248}
249
bellardea2384d2004-08-01 21:59:26 +0000250BlockDriver *bdrv_find_format(const char *format_name)
251{
252 BlockDriver *drv1;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100253 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
254 if (!strcmp(drv1->format_name, format_name)) {
bellardea2384d2004-08-01 21:59:26 +0000255 return drv1;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100256 }
bellardea2384d2004-08-01 21:59:26 +0000257 }
258 return NULL;
259}
260
Markus Armbrustereb852012009-10-27 18:41:44 +0100261static int bdrv_is_whitelisted(BlockDriver *drv)
262{
263 static const char *whitelist[] = {
264 CONFIG_BDRV_WHITELIST
265 };
266 const char **p;
267
268 if (!whitelist[0])
269 return 1; /* no whitelist, anything goes */
270
271 for (p = whitelist; *p; p++) {
272 if (!strcmp(drv->format_name, *p)) {
273 return 1;
274 }
275 }
276 return 0;
277}
278
279BlockDriver *bdrv_find_whitelisted_format(const char *format_name)
280{
281 BlockDriver *drv = bdrv_find_format(format_name);
282 return drv && bdrv_is_whitelisted(drv) ? drv : NULL;
283}
284
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200285int bdrv_create(BlockDriver *drv, const char* filename,
286 QEMUOptionParameter *options)
bellardea2384d2004-08-01 21:59:26 +0000287{
288 if (!drv->bdrv_create)
289 return -ENOTSUP;
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200290
291 return drv->bdrv_create(filename, options);
bellardea2384d2004-08-01 21:59:26 +0000292}
293
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200294int bdrv_create_file(const char* filename, QEMUOptionParameter *options)
295{
296 BlockDriver *drv;
297
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900298 drv = bdrv_find_protocol(filename);
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200299 if (drv == NULL) {
Stefan Hajnoczi16905d72010-11-30 15:14:14 +0000300 return -ENOENT;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200301 }
302
303 return bdrv_create(drv, filename, options);
304}
305
bellardd5249392004-08-03 21:14:23 +0000306#ifdef _WIN32
bellard95389c82005-12-18 18:28:15 +0000307void get_tmp_filename(char *filename, int size)
bellardd5249392004-08-03 21:14:23 +0000308{
bellard3b9f94e2007-01-07 17:27:07 +0000309 char temp_dir[MAX_PATH];
ths3b46e622007-09-17 08:09:54 +0000310
bellard3b9f94e2007-01-07 17:27:07 +0000311 GetTempPath(MAX_PATH, temp_dir);
312 GetTempFileName(temp_dir, "qem", 0, filename);
bellardd5249392004-08-03 21:14:23 +0000313}
314#else
bellard95389c82005-12-18 18:28:15 +0000315void get_tmp_filename(char *filename, int size)
bellardea2384d2004-08-01 21:59:26 +0000316{
317 int fd;
blueswir17ccfb2e2008-09-14 06:45:34 +0000318 const char *tmpdir;
bellardd5249392004-08-03 21:14:23 +0000319 /* XXX: race condition possible */
aurel320badc1e2008-03-10 00:05:34 +0000320 tmpdir = getenv("TMPDIR");
321 if (!tmpdir)
322 tmpdir = "/tmp";
323 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
bellardea2384d2004-08-01 21:59:26 +0000324 fd = mkstemp(filename);
325 close(fd);
326}
bellardd5249392004-08-03 21:14:23 +0000327#endif
bellardea2384d2004-08-01 21:59:26 +0000328
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200329/*
330 * Detect host devices. By convention, /dev/cdrom[N] is always
331 * recognized as a host CDROM.
332 */
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200333static BlockDriver *find_hdev_driver(const char *filename)
334{
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200335 int score_max = 0, score;
336 BlockDriver *drv = NULL, *d;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200337
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100338 QLIST_FOREACH(d, &bdrv_drivers, list) {
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200339 if (d->bdrv_probe_device) {
340 score = d->bdrv_probe_device(filename);
341 if (score > score_max) {
342 score_max = score;
343 drv = d;
344 }
345 }
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200346 }
347
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200348 return drv;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200349}
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200350
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900351BlockDriver *bdrv_find_protocol(const char *filename)
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200352{
353 BlockDriver *drv1;
354 char protocol[128];
355 int len;
356 const char *p;
357
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200358 /* TODO Drivers without bdrv_file_open must be specified explicitly */
359
Christoph Hellwig39508e72010-06-23 12:25:17 +0200360 /*
361 * XXX(hch): we really should not let host device detection
362 * override an explicit protocol specification, but moving this
363 * later breaks access to device names with colons in them.
364 * Thanks to the brain-dead persistent naming schemes on udev-
365 * based Linux systems those actually are quite common.
366 */
367 drv1 = find_hdev_driver(filename);
368 if (drv1) {
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200369 return drv1;
370 }
Christoph Hellwig39508e72010-06-23 12:25:17 +0200371
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000372 if (!path_has_protocol(filename)) {
Christoph Hellwig39508e72010-06-23 12:25:17 +0200373 return bdrv_find_format("file");
374 }
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000375 p = strchr(filename, ':');
376 assert(p != NULL);
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200377 len = p - filename;
378 if (len > sizeof(protocol) - 1)
379 len = sizeof(protocol) - 1;
380 memcpy(protocol, filename, len);
381 protocol[len] = '\0';
382 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
383 if (drv1->protocol_name &&
384 !strcmp(drv1->protocol_name, protocol)) {
385 return drv1;
386 }
387 }
388 return NULL;
389}
390
Stefan Weilc98ac352010-07-21 21:51:51 +0200391static int find_image_format(const char *filename, BlockDriver **pdrv)
bellardea2384d2004-08-01 21:59:26 +0000392{
bellard83f64092006-08-01 16:21:11 +0000393 int ret, score, score_max;
bellardea2384d2004-08-01 21:59:26 +0000394 BlockDriver *drv1, *drv;
bellard83f64092006-08-01 16:21:11 +0000395 uint8_t buf[2048];
396 BlockDriverState *bs;
ths3b46e622007-09-17 08:09:54 +0000397
Naphtali Spreif5edb012010-01-17 16:48:13 +0200398 ret = bdrv_file_open(&bs, filename, 0);
Stefan Weilc98ac352010-07-21 21:51:51 +0200399 if (ret < 0) {
400 *pdrv = NULL;
401 return ret;
402 }
Nicholas Bellingerf8ea0b02010-05-17 09:45:57 -0700403
Kevin Wolf08a00552010-06-01 18:37:31 +0200404 /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
405 if (bs->sg || !bdrv_is_inserted(bs)) {
Nicholas A. Bellinger1a396852010-05-27 08:56:28 -0700406 bdrv_delete(bs);
Stefan Weilc98ac352010-07-21 21:51:51 +0200407 drv = bdrv_find_format("raw");
408 if (!drv) {
409 ret = -ENOENT;
410 }
411 *pdrv = drv;
412 return ret;
Nicholas A. Bellinger1a396852010-05-27 08:56:28 -0700413 }
Nicholas Bellingerf8ea0b02010-05-17 09:45:57 -0700414
bellard83f64092006-08-01 16:21:11 +0000415 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
416 bdrv_delete(bs);
417 if (ret < 0) {
Stefan Weilc98ac352010-07-21 21:51:51 +0200418 *pdrv = NULL;
419 return ret;
bellard83f64092006-08-01 16:21:11 +0000420 }
421
bellardea2384d2004-08-01 21:59:26 +0000422 score_max = 0;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200423 drv = NULL;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100424 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
bellard83f64092006-08-01 16:21:11 +0000425 if (drv1->bdrv_probe) {
426 score = drv1->bdrv_probe(buf, ret, filename);
427 if (score > score_max) {
428 score_max = score;
429 drv = drv1;
430 }
bellardea2384d2004-08-01 21:59:26 +0000431 }
432 }
Stefan Weilc98ac352010-07-21 21:51:51 +0200433 if (!drv) {
434 ret = -ENOENT;
435 }
436 *pdrv = drv;
437 return ret;
bellardea2384d2004-08-01 21:59:26 +0000438}
439
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100440/**
441 * Set the current 'total_sectors' value
442 */
443static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
444{
445 BlockDriver *drv = bs->drv;
446
Nicholas Bellinger396759a2010-05-17 09:46:04 -0700447 /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
448 if (bs->sg)
449 return 0;
450
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100451 /* query actual device if possible, otherwise just trust the hint */
452 if (drv->bdrv_getlength) {
453 int64_t length = drv->bdrv_getlength(bs);
454 if (length < 0) {
455 return length;
456 }
457 hint = length >> BDRV_SECTOR_BITS;
458 }
459
460 bs->total_sectors = hint;
461 return 0;
462}
463
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100464/**
465 * Set open flags for a given cache mode
466 *
467 * Return 0 on success, -1 if the cache mode was invalid.
468 */
469int bdrv_parse_cache_flags(const char *mode, int *flags)
470{
471 *flags &= ~BDRV_O_CACHE_MASK;
472
473 if (!strcmp(mode, "off") || !strcmp(mode, "none")) {
474 *flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
Stefan Hajnoczi92196b22011-08-04 12:26:52 +0100475 } else if (!strcmp(mode, "directsync")) {
476 *flags |= BDRV_O_NOCACHE;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100477 } else if (!strcmp(mode, "writeback")) {
478 *flags |= BDRV_O_CACHE_WB;
479 } else if (!strcmp(mode, "unsafe")) {
480 *flags |= BDRV_O_CACHE_WB;
481 *flags |= BDRV_O_NO_FLUSH;
482 } else if (!strcmp(mode, "writethrough")) {
483 /* this is the default */
484 } else {
485 return -1;
486 }
487
488 return 0;
489}
490
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200491/*
Kevin Wolf57915332010-04-14 15:24:50 +0200492 * Common part for opening disk images and files
493 */
494static int bdrv_open_common(BlockDriverState *bs, const char *filename,
495 int flags, BlockDriver *drv)
496{
497 int ret, open_flags;
498
499 assert(drv != NULL);
500
Stefan Hajnoczi28dcee12011-09-22 20:14:12 +0100501 trace_bdrv_open_common(bs, filename, flags, drv->format_name);
502
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200503 bs->file = NULL;
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100504 bs->total_sectors = 0;
Kevin Wolf57915332010-04-14 15:24:50 +0200505 bs->encrypted = 0;
506 bs->valid_key = 0;
Stefan Hajnoczi03f541b2011-10-27 10:54:28 +0100507 bs->sg = 0;
Kevin Wolf57915332010-04-14 15:24:50 +0200508 bs->open_flags = flags;
Stefan Hajnoczi03f541b2011-10-27 10:54:28 +0100509 bs->growable = 0;
Kevin Wolf57915332010-04-14 15:24:50 +0200510 bs->buffer_alignment = 512;
511
512 pstrcpy(bs->filename, sizeof(bs->filename), filename);
Stefan Hajnoczi03f541b2011-10-27 10:54:28 +0100513 bs->backing_file[0] = '\0';
Kevin Wolf57915332010-04-14 15:24:50 +0200514
515 if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {
516 return -ENOTSUP;
517 }
518
519 bs->drv = drv;
Anthony Liguori7267c092011-08-20 22:09:37 -0500520 bs->opaque = g_malloc0(drv->instance_size);
Kevin Wolf57915332010-04-14 15:24:50 +0200521
Stefan Hajnoczi03f541b2011-10-27 10:54:28 +0100522 bs->enable_write_cache = !!(flags & BDRV_O_CACHE_WB);
Kevin Wolf57915332010-04-14 15:24:50 +0200523
524 /*
525 * Clear flags that are internal to the block layer before opening the
526 * image.
527 */
528 open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
529
530 /*
Stefan Weilebabb672011-04-26 10:29:36 +0200531 * Snapshots should be writable.
Kevin Wolf57915332010-04-14 15:24:50 +0200532 */
533 if (bs->is_temporary) {
534 open_flags |= BDRV_O_RDWR;
535 }
536
Stefan Hajnoczie7c63792011-10-27 10:54:27 +0100537 bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR);
538
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200539 /* Open the image, either directly or using a protocol */
540 if (drv->bdrv_file_open) {
541 ret = drv->bdrv_file_open(bs, filename, open_flags);
542 } else {
543 ret = bdrv_file_open(&bs->file, filename, open_flags);
544 if (ret >= 0) {
545 ret = drv->bdrv_open(bs, open_flags);
546 }
547 }
548
Kevin Wolf57915332010-04-14 15:24:50 +0200549 if (ret < 0) {
550 goto free_and_fail;
551 }
552
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100553 ret = refresh_total_sectors(bs, bs->total_sectors);
554 if (ret < 0) {
555 goto free_and_fail;
Kevin Wolf57915332010-04-14 15:24:50 +0200556 }
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100557
Kevin Wolf57915332010-04-14 15:24:50 +0200558#ifndef _WIN32
559 if (bs->is_temporary) {
560 unlink(filename);
561 }
562#endif
563 return 0;
564
565free_and_fail:
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200566 if (bs->file) {
567 bdrv_delete(bs->file);
568 bs->file = NULL;
569 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500570 g_free(bs->opaque);
Kevin Wolf57915332010-04-14 15:24:50 +0200571 bs->opaque = NULL;
572 bs->drv = NULL;
573 return ret;
574}
575
576/*
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200577 * Opens a file using a protocol (file, host_device, nbd, ...)
578 */
bellard83f64092006-08-01 16:21:11 +0000579int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
bellardb3380822004-03-14 21:38:54 +0000580{
bellard83f64092006-08-01 16:21:11 +0000581 BlockDriverState *bs;
Christoph Hellwig6db95602010-04-05 16:53:57 +0200582 BlockDriver *drv;
bellard83f64092006-08-01 16:21:11 +0000583 int ret;
584
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900585 drv = bdrv_find_protocol(filename);
Christoph Hellwig6db95602010-04-05 16:53:57 +0200586 if (!drv) {
587 return -ENOENT;
588 }
589
bellard83f64092006-08-01 16:21:11 +0000590 bs = bdrv_new("");
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200591 ret = bdrv_open_common(bs, filename, flags, drv);
bellard83f64092006-08-01 16:21:11 +0000592 if (ret < 0) {
593 bdrv_delete(bs);
594 return ret;
bellard3b0d4f62005-10-30 18:30:10 +0000595 }
aliguori71d07702009-03-03 17:37:16 +0000596 bs->growable = 1;
bellard83f64092006-08-01 16:21:11 +0000597 *pbs = bs;
598 return 0;
bellardea2384d2004-08-01 21:59:26 +0000599}
bellardfc01f7e2003-06-30 10:03:06 +0000600
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200601/*
602 * Opens a disk image (raw, qcow2, vmdk, ...)
603 */
Kevin Wolfd6e90982010-03-31 14:40:27 +0200604int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
605 BlockDriver *drv)
bellardea2384d2004-08-01 21:59:26 +0000606{
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200607 int ret;
Kevin Wolf2b572812011-10-26 11:03:01 +0200608 char tmp_filename[PATH_MAX];
bellard712e7872005-04-28 21:09:32 +0000609
bellard83f64092006-08-01 16:21:11 +0000610 if (flags & BDRV_O_SNAPSHOT) {
bellardea2384d2004-08-01 21:59:26 +0000611 BlockDriverState *bs1;
612 int64_t total_size;
aliguori7c96d462008-09-12 17:54:13 +0000613 int is_protocol = 0;
Kevin Wolf91a073a2009-05-27 14:48:06 +0200614 BlockDriver *bdrv_qcow2;
615 QEMUOptionParameter *options;
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200616 char backing_filename[PATH_MAX];
ths3b46e622007-09-17 08:09:54 +0000617
bellardea2384d2004-08-01 21:59:26 +0000618 /* if snapshot, we create a temporary backing file and open it
619 instead of opening 'filename' directly */
620
621 /* if there is a backing file, use it */
622 bs1 = bdrv_new("");
Kevin Wolfd6e90982010-03-31 14:40:27 +0200623 ret = bdrv_open(bs1, filename, 0, drv);
aliguori51d7c002009-03-05 23:00:29 +0000624 if (ret < 0) {
bellardea2384d2004-08-01 21:59:26 +0000625 bdrv_delete(bs1);
aliguori51d7c002009-03-05 23:00:29 +0000626 return ret;
bellardea2384d2004-08-01 21:59:26 +0000627 }
Jes Sorensen3e829902010-05-27 16:20:30 +0200628 total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK;
aliguori7c96d462008-09-12 17:54:13 +0000629
630 if (bs1->drv && bs1->drv->protocol_name)
631 is_protocol = 1;
632
bellardea2384d2004-08-01 21:59:26 +0000633 bdrv_delete(bs1);
ths3b46e622007-09-17 08:09:54 +0000634
bellardea2384d2004-08-01 21:59:26 +0000635 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
aliguori7c96d462008-09-12 17:54:13 +0000636
637 /* Real path is meaningless for protocols */
638 if (is_protocol)
639 snprintf(backing_filename, sizeof(backing_filename),
640 "%s", filename);
Kirill A. Shutemov114cdfa2009-12-25 18:19:22 +0000641 else if (!realpath(filename, backing_filename))
642 return -errno;
aliguori7c96d462008-09-12 17:54:13 +0000643
Kevin Wolf91a073a2009-05-27 14:48:06 +0200644 bdrv_qcow2 = bdrv_find_format("qcow2");
645 options = parse_option_parameters("", bdrv_qcow2->create_options, NULL);
646
Jes Sorensen3e829902010-05-27 16:20:30 +0200647 set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size);
Kevin Wolf91a073a2009-05-27 14:48:06 +0200648 set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename);
649 if (drv) {
650 set_option_parameter(options, BLOCK_OPT_BACKING_FMT,
651 drv->format_name);
652 }
653
654 ret = bdrv_create(bdrv_qcow2, tmp_filename, options);
Jan Kiszkad7487682010-04-29 18:24:50 +0200655 free_option_parameters(options);
aliguori51d7c002009-03-05 23:00:29 +0000656 if (ret < 0) {
657 return ret;
bellardea2384d2004-08-01 21:59:26 +0000658 }
Kevin Wolf91a073a2009-05-27 14:48:06 +0200659
bellardea2384d2004-08-01 21:59:26 +0000660 filename = tmp_filename;
Kevin Wolf91a073a2009-05-27 14:48:06 +0200661 drv = bdrv_qcow2;
bellardea2384d2004-08-01 21:59:26 +0000662 bs->is_temporary = 1;
663 }
bellard712e7872005-04-28 21:09:32 +0000664
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200665 /* Find the right image format driver */
Christoph Hellwig6db95602010-04-05 16:53:57 +0200666 if (!drv) {
Stefan Weilc98ac352010-07-21 21:51:51 +0200667 ret = find_image_format(filename, &drv);
aliguori51d7c002009-03-05 23:00:29 +0000668 }
Christoph Hellwig69873072010-01-20 18:13:25 +0100669
aliguori51d7c002009-03-05 23:00:29 +0000670 if (!drv) {
aliguori51d7c002009-03-05 23:00:29 +0000671 goto unlink_and_fail;
bellardea2384d2004-08-01 21:59:26 +0000672 }
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200673
674 /* Open the image */
675 ret = bdrv_open_common(bs, filename, flags, drv);
676 if (ret < 0) {
Christoph Hellwig69873072010-01-20 18:13:25 +0100677 goto unlink_and_fail;
678 }
679
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200680 /* If there is a backing file, use it */
681 if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') {
682 char backing_filename[PATH_MAX];
683 int back_flags;
684 BlockDriver *back_drv = NULL;
685
686 bs->backing_hd = bdrv_new("");
Stefan Hajnoczidf2dbb42010-12-02 16:54:13 +0000687
688 if (path_has_protocol(bs->backing_file)) {
689 pstrcpy(backing_filename, sizeof(backing_filename),
690 bs->backing_file);
691 } else {
692 path_combine(backing_filename, sizeof(backing_filename),
693 filename, bs->backing_file);
694 }
695
696 if (bs->backing_format[0] != '\0') {
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200697 back_drv = bdrv_find_format(bs->backing_format);
Stefan Hajnoczidf2dbb42010-12-02 16:54:13 +0000698 }
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200699
700 /* backing files always opened read-only */
701 back_flags =
702 flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
703
704 ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv);
705 if (ret < 0) {
706 bdrv_close(bs);
707 return ret;
708 }
709 if (bs->is_temporary) {
710 bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR);
711 } else {
712 /* base image inherits from "parent" */
713 bs->backing_hd->keep_read_only = bs->keep_read_only;
714 }
715 }
716
717 if (!bdrv_key_required(bs)) {
Markus Armbruster7d4b4ba2011-09-06 18:58:59 +0200718 bdrv_dev_change_media_cb(bs, true);
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200719 }
720
721 return 0;
722
723unlink_and_fail:
724 if (bs->is_temporary) {
725 unlink(filename);
726 }
727 return ret;
728}
729
bellardfc01f7e2003-06-30 10:03:06 +0000730void bdrv_close(BlockDriverState *bs)
731{
bellard19cb3732006-08-19 11:45:59 +0000732 if (bs->drv) {
Markus Armbrusterf9092b12010-06-25 10:33:39 +0200733 if (bs == bs_snapshots) {
734 bs_snapshots = NULL;
735 }
Stefan Hajnoczi557df6a2010-04-17 10:49:06 +0100736 if (bs->backing_hd) {
bellardea2384d2004-08-01 21:59:26 +0000737 bdrv_delete(bs->backing_hd);
Stefan Hajnoczi557df6a2010-04-17 10:49:06 +0100738 bs->backing_hd = NULL;
739 }
bellardea2384d2004-08-01 21:59:26 +0000740 bs->drv->bdrv_close(bs);
Anthony Liguori7267c092011-08-20 22:09:37 -0500741 g_free(bs->opaque);
bellardea2384d2004-08-01 21:59:26 +0000742#ifdef _WIN32
743 if (bs->is_temporary) {
744 unlink(bs->filename);
745 }
bellard67b915a2004-03-31 23:37:16 +0000746#endif
bellardea2384d2004-08-01 21:59:26 +0000747 bs->opaque = NULL;
748 bs->drv = NULL;
bellardb3380822004-03-14 21:38:54 +0000749
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200750 if (bs->file != NULL) {
751 bdrv_close(bs->file);
752 }
753
Markus Armbruster7d4b4ba2011-09-06 18:58:59 +0200754 bdrv_dev_change_media_cb(bs, false);
bellardb3380822004-03-14 21:38:54 +0000755 }
756}
757
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +0900758void bdrv_close_all(void)
759{
760 BlockDriverState *bs;
761
762 QTAILQ_FOREACH(bs, &bdrv_states, list) {
763 bdrv_close(bs);
764 }
765}
766
Ryan Harperd22b2f42011-03-29 20:51:47 -0500767/* make a BlockDriverState anonymous by removing from bdrv_state list.
768 Also, NULL terminate the device_name to prevent double remove */
769void bdrv_make_anon(BlockDriverState *bs)
770{
771 if (bs->device_name[0] != '\0') {
772 QTAILQ_REMOVE(&bdrv_states, bs, list);
773 }
774 bs->device_name[0] = '\0';
775}
776
bellardb3380822004-03-14 21:38:54 +0000777void bdrv_delete(BlockDriverState *bs)
778{
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200779 assert(!bs->dev);
Markus Armbruster18846de2010-06-29 16:58:30 +0200780
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +0100781 /* remove from list, if necessary */
Ryan Harperd22b2f42011-03-29 20:51:47 -0500782 bdrv_make_anon(bs);
aurel3234c6f052008-04-08 19:51:21 +0000783
bellardb3380822004-03-14 21:38:54 +0000784 bdrv_close(bs);
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200785 if (bs->file != NULL) {
786 bdrv_delete(bs->file);
787 }
788
Markus Armbrusterf9092b12010-06-25 10:33:39 +0200789 assert(bs != bs_snapshots);
Anthony Liguori7267c092011-08-20 22:09:37 -0500790 g_free(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000791}
792
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200793int bdrv_attach_dev(BlockDriverState *bs, void *dev)
794/* TODO change to DeviceState *dev when all users are qdevified */
Markus Armbruster18846de2010-06-29 16:58:30 +0200795{
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200796 if (bs->dev) {
Markus Armbruster18846de2010-06-29 16:58:30 +0200797 return -EBUSY;
798 }
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200799 bs->dev = dev;
Luiz Capitulino28a72822011-09-26 17:43:50 -0300800 bdrv_iostatus_reset(bs);
Markus Armbruster18846de2010-06-29 16:58:30 +0200801 return 0;
802}
803
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200804/* TODO qdevified devices don't use this, remove when devices are qdevified */
805void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev)
Markus Armbruster18846de2010-06-29 16:58:30 +0200806{
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200807 if (bdrv_attach_dev(bs, dev) < 0) {
808 abort();
809 }
810}
811
812void bdrv_detach_dev(BlockDriverState *bs, void *dev)
813/* TODO change to DeviceState *dev when all users are qdevified */
814{
815 assert(bs->dev == dev);
816 bs->dev = NULL;
Markus Armbruster0e49de52011-08-03 15:07:41 +0200817 bs->dev_ops = NULL;
818 bs->dev_opaque = NULL;
Markus Armbruster29e05f22011-09-06 18:58:57 +0200819 bs->buffer_alignment = 512;
Markus Armbruster18846de2010-06-29 16:58:30 +0200820}
821
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200822/* TODO change to return DeviceState * when all users are qdevified */
823void *bdrv_get_attached_dev(BlockDriverState *bs)
Markus Armbruster18846de2010-06-29 16:58:30 +0200824{
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200825 return bs->dev;
Markus Armbruster18846de2010-06-29 16:58:30 +0200826}
827
Markus Armbruster0e49de52011-08-03 15:07:41 +0200828void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops,
829 void *opaque)
830{
831 bs->dev_ops = ops;
832 bs->dev_opaque = opaque;
Markus Armbruster2c6942f2011-09-06 18:58:51 +0200833 if (bdrv_dev_has_removable_media(bs) && bs == bs_snapshots) {
834 bs_snapshots = NULL;
835 }
Markus Armbruster0e49de52011-08-03 15:07:41 +0200836}
837
Markus Armbruster7d4b4ba2011-09-06 18:58:59 +0200838static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load)
Markus Armbruster0e49de52011-08-03 15:07:41 +0200839{
Markus Armbruster145feb12011-08-03 15:07:42 +0200840 if (bs->dev_ops && bs->dev_ops->change_media_cb) {
Markus Armbruster7d4b4ba2011-09-06 18:58:59 +0200841 bs->dev_ops->change_media_cb(bs->dev_opaque, load);
Markus Armbruster145feb12011-08-03 15:07:42 +0200842 }
843}
844
Markus Armbruster2c6942f2011-09-06 18:58:51 +0200845bool bdrv_dev_has_removable_media(BlockDriverState *bs)
846{
847 return !bs->dev || (bs->dev_ops && bs->dev_ops->change_media_cb);
848}
849
Paolo Bonzini025ccaa2011-11-07 17:50:13 +0100850void bdrv_dev_eject_request(BlockDriverState *bs, bool force)
851{
852 if (bs->dev_ops && bs->dev_ops->eject_request_cb) {
853 bs->dev_ops->eject_request_cb(bs->dev_opaque, force);
854 }
855}
856
Markus Armbrustere4def802011-09-06 18:58:53 +0200857bool bdrv_dev_is_tray_open(BlockDriverState *bs)
858{
859 if (bs->dev_ops && bs->dev_ops->is_tray_open) {
860 return bs->dev_ops->is_tray_open(bs->dev_opaque);
861 }
862 return false;
863}
864
Markus Armbruster145feb12011-08-03 15:07:42 +0200865static void bdrv_dev_resize_cb(BlockDriverState *bs)
866{
867 if (bs->dev_ops && bs->dev_ops->resize_cb) {
868 bs->dev_ops->resize_cb(bs->dev_opaque);
Markus Armbruster0e49de52011-08-03 15:07:41 +0200869 }
870}
871
Markus Armbrusterf1076392011-09-06 18:58:46 +0200872bool bdrv_dev_is_medium_locked(BlockDriverState *bs)
873{
874 if (bs->dev_ops && bs->dev_ops->is_medium_locked) {
875 return bs->dev_ops->is_medium_locked(bs->dev_opaque);
876 }
877 return false;
878}
879
aliguorie97fc192009-04-21 23:11:50 +0000880/*
881 * Run consistency checks on an image
882 *
Kevin Wolfe076f332010-06-29 11:43:13 +0200883 * Returns 0 if the check could be completed (it doesn't mean that the image is
Stefan Weila1c72732011-04-28 17:20:38 +0200884 * free of errors) or -errno when an internal error occurred. The results of the
Kevin Wolfe076f332010-06-29 11:43:13 +0200885 * check are stored in res.
aliguorie97fc192009-04-21 23:11:50 +0000886 */
Kevin Wolfe076f332010-06-29 11:43:13 +0200887int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res)
aliguorie97fc192009-04-21 23:11:50 +0000888{
889 if (bs->drv->bdrv_check == NULL) {
890 return -ENOTSUP;
891 }
892
Kevin Wolfe076f332010-06-29 11:43:13 +0200893 memset(res, 0, sizeof(*res));
Kevin Wolf9ac228e2010-06-29 12:37:54 +0200894 return bs->drv->bdrv_check(bs, res);
aliguorie97fc192009-04-21 23:11:50 +0000895}
896
Kevin Wolf8a426612010-07-16 17:17:01 +0200897#define COMMIT_BUF_SECTORS 2048
898
bellard33e39632003-07-06 17:15:21 +0000899/* commit COW file into the raw image */
900int bdrv_commit(BlockDriverState *bs)
901{
bellard19cb3732006-08-19 11:45:59 +0000902 BlockDriver *drv = bs->drv;
Kevin Wolfee181192010-08-05 13:05:22 +0200903 BlockDriver *backing_drv;
Kevin Wolf8a426612010-07-16 17:17:01 +0200904 int64_t sector, total_sectors;
905 int n, ro, open_flags;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200906 int ret = 0, rw_ret = 0;
Kevin Wolf8a426612010-07-16 17:17:01 +0200907 uint8_t *buf;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200908 char filename[1024];
909 BlockDriverState *bs_rw, *bs_ro;
bellard33e39632003-07-06 17:15:21 +0000910
bellard19cb3732006-08-19 11:45:59 +0000911 if (!drv)
912 return -ENOMEDIUM;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200913
914 if (!bs->backing_hd) {
915 return -ENOTSUP;
bellard33e39632003-07-06 17:15:21 +0000916 }
917
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200918 if (bs->backing_hd->keep_read_only) {
919 return -EACCES;
920 }
Kevin Wolfee181192010-08-05 13:05:22 +0200921
922 backing_drv = bs->backing_hd->drv;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200923 ro = bs->backing_hd->read_only;
924 strncpy(filename, bs->backing_hd->filename, sizeof(filename));
925 open_flags = bs->backing_hd->open_flags;
926
927 if (ro) {
928 /* re-open as RW */
929 bdrv_delete(bs->backing_hd);
930 bs->backing_hd = NULL;
931 bs_rw = bdrv_new("");
Kevin Wolfee181192010-08-05 13:05:22 +0200932 rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR,
933 backing_drv);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200934 if (rw_ret < 0) {
935 bdrv_delete(bs_rw);
936 /* try to re-open read-only */
937 bs_ro = bdrv_new("");
Kevin Wolfee181192010-08-05 13:05:22 +0200938 ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR,
939 backing_drv);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200940 if (ret < 0) {
941 bdrv_delete(bs_ro);
942 /* drive not functional anymore */
943 bs->drv = NULL;
944 return ret;
945 }
946 bs->backing_hd = bs_ro;
947 return rw_ret;
948 }
949 bs->backing_hd = bs_rw;
bellard33e39632003-07-06 17:15:21 +0000950 }
bellardea2384d2004-08-01 21:59:26 +0000951
Jan Kiszka6ea44302009-11-30 18:21:19 +0100952 total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
Anthony Liguori7267c092011-08-20 22:09:37 -0500953 buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE);
bellardea2384d2004-08-01 21:59:26 +0000954
Kevin Wolf8a426612010-07-16 17:17:01 +0200955 for (sector = 0; sector < total_sectors; sector += n) {
956 if (drv->bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) {
957
958 if (bdrv_read(bs, sector, buf, n) != 0) {
959 ret = -EIO;
960 goto ro_cleanup;
961 }
962
963 if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) {
964 ret = -EIO;
965 goto ro_cleanup;
966 }
bellardea2384d2004-08-01 21:59:26 +0000967 }
968 }
bellard95389c82005-12-18 18:28:15 +0000969
Christoph Hellwig1d449522010-01-17 12:32:30 +0100970 if (drv->bdrv_make_empty) {
971 ret = drv->bdrv_make_empty(bs);
972 bdrv_flush(bs);
973 }
bellard95389c82005-12-18 18:28:15 +0000974
Christoph Hellwig3f5075a2010-01-12 13:49:23 +0100975 /*
976 * Make sure all data we wrote to the backing device is actually
977 * stable on disk.
978 */
979 if (bs->backing_hd)
980 bdrv_flush(bs->backing_hd);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200981
982ro_cleanup:
Anthony Liguori7267c092011-08-20 22:09:37 -0500983 g_free(buf);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200984
985 if (ro) {
986 /* re-open as RO */
987 bdrv_delete(bs->backing_hd);
988 bs->backing_hd = NULL;
989 bs_ro = bdrv_new("");
Kevin Wolfee181192010-08-05 13:05:22 +0200990 ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR,
991 backing_drv);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200992 if (ret < 0) {
993 bdrv_delete(bs_ro);
994 /* drive not functional anymore */
995 bs->drv = NULL;
996 return ret;
997 }
998 bs->backing_hd = bs_ro;
999 bs->backing_hd->keep_read_only = 0;
1000 }
1001
Christoph Hellwig1d449522010-01-17 12:32:30 +01001002 return ret;
bellard33e39632003-07-06 17:15:21 +00001003}
1004
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +02001005void bdrv_commit_all(void)
1006{
1007 BlockDriverState *bs;
1008
1009 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1010 bdrv_commit(bs);
1011 }
1012}
1013
Kevin Wolf756e6732010-01-12 12:55:17 +01001014/*
1015 * Return values:
1016 * 0 - success
1017 * -EINVAL - backing format specified, but no file
1018 * -ENOSPC - can't update the backing file because no space is left in the
1019 * image file header
1020 * -ENOTSUP - format driver doesn't support changing the backing file
1021 */
1022int bdrv_change_backing_file(BlockDriverState *bs,
1023 const char *backing_file, const char *backing_fmt)
1024{
1025 BlockDriver *drv = bs->drv;
1026
1027 if (drv->bdrv_change_backing_file != NULL) {
1028 return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
1029 } else {
1030 return -ENOTSUP;
1031 }
1032}
1033
aliguori71d07702009-03-03 17:37:16 +00001034static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
1035 size_t size)
1036{
1037 int64_t len;
1038
1039 if (!bdrv_is_inserted(bs))
1040 return -ENOMEDIUM;
1041
1042 if (bs->growable)
1043 return 0;
1044
1045 len = bdrv_getlength(bs);
1046
Kevin Wolffbb7b4e2009-05-08 14:47:24 +02001047 if (offset < 0)
1048 return -EIO;
1049
1050 if ((offset > len) || (len - offset < size))
aliguori71d07702009-03-03 17:37:16 +00001051 return -EIO;
1052
1053 return 0;
1054}
1055
1056static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
1057 int nb_sectors)
1058{
Jes Sorenseneb5a3162010-05-27 16:20:31 +02001059 return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE,
1060 nb_sectors * BDRV_SECTOR_SIZE);
aliguori71d07702009-03-03 17:37:16 +00001061}
1062
Stefan Hajnoczi1c9805a2011-10-13 13:08:22 +01001063typedef struct RwCo {
1064 BlockDriverState *bs;
1065 int64_t sector_num;
1066 int nb_sectors;
1067 QEMUIOVector *qiov;
1068 bool is_write;
1069 int ret;
1070} RwCo;
1071
1072static void coroutine_fn bdrv_rw_co_entry(void *opaque)
1073{
1074 RwCo *rwco = opaque;
1075
1076 if (!rwco->is_write) {
1077 rwco->ret = bdrv_co_do_readv(rwco->bs, rwco->sector_num,
1078 rwco->nb_sectors, rwco->qiov);
1079 } else {
1080 rwco->ret = bdrv_co_do_writev(rwco->bs, rwco->sector_num,
1081 rwco->nb_sectors, rwco->qiov);
1082 }
1083}
1084
1085/*
1086 * Process a synchronous request using coroutines
1087 */
1088static int bdrv_rw_co(BlockDriverState *bs, int64_t sector_num, uint8_t *buf,
1089 int nb_sectors, bool is_write)
1090{
1091 QEMUIOVector qiov;
1092 struct iovec iov = {
1093 .iov_base = (void *)buf,
1094 .iov_len = nb_sectors * BDRV_SECTOR_SIZE,
1095 };
1096 Coroutine *co;
1097 RwCo rwco = {
1098 .bs = bs,
1099 .sector_num = sector_num,
1100 .nb_sectors = nb_sectors,
1101 .qiov = &qiov,
1102 .is_write = is_write,
1103 .ret = NOT_DONE,
1104 };
1105
1106 qemu_iovec_init_external(&qiov, &iov, 1);
1107
1108 if (qemu_in_coroutine()) {
1109 /* Fast-path if already in coroutine context */
1110 bdrv_rw_co_entry(&rwco);
1111 } else {
1112 co = qemu_coroutine_create(bdrv_rw_co_entry);
1113 qemu_coroutine_enter(co, &rwco);
1114 while (rwco.ret == NOT_DONE) {
1115 qemu_aio_wait();
1116 }
1117 }
1118 return rwco.ret;
1119}
1120
bellard19cb3732006-08-19 11:45:59 +00001121/* return < 0 if error. See bdrv_write() for the return codes */
ths5fafdf22007-09-16 21:08:06 +00001122int bdrv_read(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +00001123 uint8_t *buf, int nb_sectors)
1124{
Stefan Hajnoczi1c9805a2011-10-13 13:08:22 +01001125 return bdrv_rw_co(bs, sector_num, buf, nb_sectors, false);
bellardfc01f7e2003-06-30 10:03:06 +00001126}
1127
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001128static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num,
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001129 int nb_sectors, int dirty)
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001130{
1131 int64_t start, end;
Jan Kiszkac6d22832009-11-30 18:21:20 +01001132 unsigned long val, idx, bit;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001133
Jan Kiszka6ea44302009-11-30 18:21:19 +01001134 start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkac6d22832009-11-30 18:21:20 +01001135 end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001136
1137 for (; start <= end; start++) {
Jan Kiszkac6d22832009-11-30 18:21:20 +01001138 idx = start / (sizeof(unsigned long) * 8);
1139 bit = start % (sizeof(unsigned long) * 8);
1140 val = bs->dirty_bitmap[idx];
1141 if (dirty) {
Marcelo Tosatti6d59fec2010-11-08 17:02:54 -02001142 if (!(val & (1UL << bit))) {
Liran Schouraaa0eb72010-01-26 10:31:48 +02001143 bs->dirty_count++;
Marcelo Tosatti6d59fec2010-11-08 17:02:54 -02001144 val |= 1UL << bit;
Liran Schouraaa0eb72010-01-26 10:31:48 +02001145 }
Jan Kiszkac6d22832009-11-30 18:21:20 +01001146 } else {
Marcelo Tosatti6d59fec2010-11-08 17:02:54 -02001147 if (val & (1UL << bit)) {
Liran Schouraaa0eb72010-01-26 10:31:48 +02001148 bs->dirty_count--;
Marcelo Tosatti6d59fec2010-11-08 17:02:54 -02001149 val &= ~(1UL << bit);
Liran Schouraaa0eb72010-01-26 10:31:48 +02001150 }
Jan Kiszkac6d22832009-11-30 18:21:20 +01001151 }
1152 bs->dirty_bitmap[idx] = val;
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001153 }
1154}
1155
ths5fafdf22007-09-16 21:08:06 +00001156/* Return < 0 if error. Important errors are:
bellard19cb3732006-08-19 11:45:59 +00001157 -EIO generic I/O error (may happen for all errors)
1158 -ENOMEDIUM No media inserted.
1159 -EINVAL Invalid sector number or nb_sectors
1160 -EACCES Trying to write a read-only device
1161*/
ths5fafdf22007-09-16 21:08:06 +00001162int bdrv_write(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +00001163 const uint8_t *buf, int nb_sectors)
1164{
Stefan Hajnoczi1c9805a2011-10-13 13:08:22 +01001165 return bdrv_rw_co(bs, sector_num, (uint8_t *)buf, nb_sectors, true);
bellard83f64092006-08-01 16:21:11 +00001166}
1167
aliguorieda578e2009-03-12 19:57:16 +00001168int bdrv_pread(BlockDriverState *bs, int64_t offset,
1169 void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +00001170{
Jan Kiszka6ea44302009-11-30 18:21:19 +01001171 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
bellard83f64092006-08-01 16:21:11 +00001172 int len, nb_sectors, count;
1173 int64_t sector_num;
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001174 int ret;
bellard83f64092006-08-01 16:21:11 +00001175
1176 count = count1;
1177 /* first read to align to sector start */
Jan Kiszka6ea44302009-11-30 18:21:19 +01001178 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
bellard83f64092006-08-01 16:21:11 +00001179 if (len > count)
1180 len = count;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001181 sector_num = offset >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001182 if (len > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001183 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1184 return ret;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001185 memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
bellard83f64092006-08-01 16:21:11 +00001186 count -= len;
1187 if (count == 0)
1188 return count1;
1189 sector_num++;
1190 buf += len;
1191 }
1192
1193 /* read the sectors "in place" */
Jan Kiszka6ea44302009-11-30 18:21:19 +01001194 nb_sectors = count >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001195 if (nb_sectors > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001196 if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0)
1197 return ret;
bellard83f64092006-08-01 16:21:11 +00001198 sector_num += nb_sectors;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001199 len = nb_sectors << BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001200 buf += len;
1201 count -= len;
1202 }
1203
1204 /* add data from the last sector */
1205 if (count > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001206 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1207 return ret;
bellard83f64092006-08-01 16:21:11 +00001208 memcpy(buf, tmp_buf, count);
1209 }
1210 return count1;
1211}
1212
aliguorieda578e2009-03-12 19:57:16 +00001213int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
1214 const void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +00001215{
Jan Kiszka6ea44302009-11-30 18:21:19 +01001216 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
bellard83f64092006-08-01 16:21:11 +00001217 int len, nb_sectors, count;
1218 int64_t sector_num;
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001219 int ret;
bellard83f64092006-08-01 16:21:11 +00001220
1221 count = count1;
1222 /* first write to align to sector start */
Jan Kiszka6ea44302009-11-30 18:21:19 +01001223 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
bellard83f64092006-08-01 16:21:11 +00001224 if (len > count)
1225 len = count;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001226 sector_num = offset >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001227 if (len > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001228 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1229 return ret;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001230 memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len);
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001231 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
1232 return ret;
bellard83f64092006-08-01 16:21:11 +00001233 count -= len;
1234 if (count == 0)
1235 return count1;
1236 sector_num++;
1237 buf += len;
1238 }
1239
1240 /* write the sectors "in place" */
Jan Kiszka6ea44302009-11-30 18:21:19 +01001241 nb_sectors = count >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001242 if (nb_sectors > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001243 if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0)
1244 return ret;
bellard83f64092006-08-01 16:21:11 +00001245 sector_num += nb_sectors;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001246 len = nb_sectors << BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001247 buf += len;
1248 count -= len;
1249 }
1250
1251 /* add data from the last sector */
1252 if (count > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001253 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1254 return ret;
bellard83f64092006-08-01 16:21:11 +00001255 memcpy(tmp_buf, buf, count);
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001256 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
1257 return ret;
bellard83f64092006-08-01 16:21:11 +00001258 }
1259 return count1;
1260}
bellard83f64092006-08-01 16:21:11 +00001261
Kevin Wolff08145f2010-06-16 16:38:15 +02001262/*
1263 * Writes to the file and ensures that no writes are reordered across this
1264 * request (acts as a barrier)
1265 *
1266 * Returns 0 on success, -errno in error cases.
1267 */
1268int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset,
1269 const void *buf, int count)
1270{
1271 int ret;
1272
1273 ret = bdrv_pwrite(bs, offset, buf, count);
1274 if (ret < 0) {
1275 return ret;
1276 }
1277
Stefan Hajnoczi92196b22011-08-04 12:26:52 +01001278 /* No flush needed for cache modes that use O_DSYNC */
1279 if ((bs->open_flags & BDRV_O_CACHE_WB) != 0) {
Kevin Wolff08145f2010-06-16 16:38:15 +02001280 bdrv_flush(bs);
1281 }
1282
1283 return 0;
1284}
1285
Stefan Hajnoczic5fbe572011-10-05 17:17:03 +01001286/*
1287 * Handle a read request in coroutine context
1288 */
1289static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs,
1290 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
Kevin Wolfda1fa912011-07-14 17:27:13 +02001291{
1292 BlockDriver *drv = bs->drv;
1293
Kevin Wolfda1fa912011-07-14 17:27:13 +02001294 if (!drv) {
1295 return -ENOMEDIUM;
1296 }
1297 if (bdrv_check_request(bs, sector_num, nb_sectors)) {
1298 return -EIO;
1299 }
1300
1301 return drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov);
1302}
1303
Stefan Hajnoczic5fbe572011-10-05 17:17:03 +01001304int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num,
Kevin Wolfda1fa912011-07-14 17:27:13 +02001305 int nb_sectors, QEMUIOVector *qiov)
1306{
Stefan Hajnoczic5fbe572011-10-05 17:17:03 +01001307 trace_bdrv_co_readv(bs, sector_num, nb_sectors);
Kevin Wolfda1fa912011-07-14 17:27:13 +02001308
Stefan Hajnoczic5fbe572011-10-05 17:17:03 +01001309 return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov);
1310}
1311
1312/*
1313 * Handle a write request in coroutine context
1314 */
1315static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs,
1316 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
1317{
1318 BlockDriver *drv = bs->drv;
Stefan Hajnoczi6b7cb242011-10-13 13:08:24 +01001319 int ret;
Kevin Wolfda1fa912011-07-14 17:27:13 +02001320
1321 if (!bs->drv) {
1322 return -ENOMEDIUM;
1323 }
1324 if (bs->read_only) {
1325 return -EACCES;
1326 }
1327 if (bdrv_check_request(bs, sector_num, nb_sectors)) {
1328 return -EIO;
1329 }
1330
Stefan Hajnoczi6b7cb242011-10-13 13:08:24 +01001331 ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov);
1332
Kevin Wolfda1fa912011-07-14 17:27:13 +02001333 if (bs->dirty_bitmap) {
1334 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1335 }
1336
1337 if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
1338 bs->wr_highest_sector = sector_num + nb_sectors - 1;
1339 }
1340
Stefan Hajnoczi6b7cb242011-10-13 13:08:24 +01001341 return ret;
Kevin Wolfda1fa912011-07-14 17:27:13 +02001342}
1343
Stefan Hajnoczic5fbe572011-10-05 17:17:03 +01001344int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num,
1345 int nb_sectors, QEMUIOVector *qiov)
1346{
1347 trace_bdrv_co_writev(bs, sector_num, nb_sectors);
1348
1349 return bdrv_co_do_writev(bs, sector_num, nb_sectors, qiov);
1350}
1351
bellard83f64092006-08-01 16:21:11 +00001352/**
bellard83f64092006-08-01 16:21:11 +00001353 * Truncate file to 'offset' bytes (needed only for file protocols)
1354 */
1355int bdrv_truncate(BlockDriverState *bs, int64_t offset)
1356{
1357 BlockDriver *drv = bs->drv;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001358 int ret;
bellard83f64092006-08-01 16:21:11 +00001359 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001360 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +00001361 if (!drv->bdrv_truncate)
1362 return -ENOTSUP;
Naphtali Sprei59f26892009-10-26 16:25:16 +02001363 if (bs->read_only)
1364 return -EACCES;
Marcelo Tosatti85916752011-01-26 12:12:35 -02001365 if (bdrv_in_use(bs))
1366 return -EBUSY;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001367 ret = drv->bdrv_truncate(bs, offset);
1368 if (ret == 0) {
1369 ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
Markus Armbruster145feb12011-08-03 15:07:42 +02001370 bdrv_dev_resize_cb(bs);
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001371 }
1372 return ret;
bellard83f64092006-08-01 16:21:11 +00001373}
1374
1375/**
Fam Zheng4a1d5e12011-07-12 19:56:39 +08001376 * Length of a allocated file in bytes. Sparse files are counted by actual
1377 * allocated space. Return < 0 if error or unknown.
1378 */
1379int64_t bdrv_get_allocated_file_size(BlockDriverState *bs)
1380{
1381 BlockDriver *drv = bs->drv;
1382 if (!drv) {
1383 return -ENOMEDIUM;
1384 }
1385 if (drv->bdrv_get_allocated_file_size) {
1386 return drv->bdrv_get_allocated_file_size(bs);
1387 }
1388 if (bs->file) {
1389 return bdrv_get_allocated_file_size(bs->file);
1390 }
1391 return -ENOTSUP;
1392}
1393
1394/**
bellard83f64092006-08-01 16:21:11 +00001395 * Length of a file in bytes. Return < 0 if error or unknown.
1396 */
1397int64_t bdrv_getlength(BlockDriverState *bs)
1398{
1399 BlockDriver *drv = bs->drv;
1400 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001401 return -ENOMEDIUM;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001402
Markus Armbruster2c6942f2011-09-06 18:58:51 +02001403 if (bs->growable || bdrv_dev_has_removable_media(bs)) {
Stefan Hajnoczi46a4e4e2011-03-29 20:04:41 +01001404 if (drv->bdrv_getlength) {
1405 return drv->bdrv_getlength(bs);
1406 }
bellard83f64092006-08-01 16:21:11 +00001407 }
Stefan Hajnoczi46a4e4e2011-03-29 20:04:41 +01001408 return bs->total_sectors * BDRV_SECTOR_SIZE;
bellardfc01f7e2003-06-30 10:03:06 +00001409}
1410
bellard19cb3732006-08-19 11:45:59 +00001411/* return 0 as number of sectors if no device present or error */
ths96b8f132007-12-17 01:35:20 +00001412void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
bellardfc01f7e2003-06-30 10:03:06 +00001413{
bellard19cb3732006-08-19 11:45:59 +00001414 int64_t length;
1415 length = bdrv_getlength(bs);
1416 if (length < 0)
1417 length = 0;
1418 else
Jan Kiszka6ea44302009-11-30 18:21:19 +01001419 length = length >> BDRV_SECTOR_BITS;
bellard19cb3732006-08-19 11:45:59 +00001420 *nb_sectors_ptr = length;
bellardfc01f7e2003-06-30 10:03:06 +00001421}
bellardcf989512004-02-16 21:56:36 +00001422
aliguorif3d54fc2008-11-25 21:50:24 +00001423struct partition {
1424 uint8_t boot_ind; /* 0x80 - active */
1425 uint8_t head; /* starting head */
1426 uint8_t sector; /* starting sector */
1427 uint8_t cyl; /* starting cylinder */
1428 uint8_t sys_ind; /* What partition type */
1429 uint8_t end_head; /* end head */
1430 uint8_t end_sector; /* end sector */
1431 uint8_t end_cyl; /* end cylinder */
1432 uint32_t start_sect; /* starting sector counting from 0 */
1433 uint32_t nr_sects; /* nr of sectors in partition */
Stefan Weil541dc0d2011-08-31 12:38:01 +02001434} QEMU_PACKED;
aliguorif3d54fc2008-11-25 21:50:24 +00001435
1436/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
1437static int guess_disk_lchs(BlockDriverState *bs,
1438 int *pcylinders, int *pheads, int *psectors)
1439{
Jes Sorenseneb5a3162010-05-27 16:20:31 +02001440 uint8_t buf[BDRV_SECTOR_SIZE];
aliguorif3d54fc2008-11-25 21:50:24 +00001441 int ret, i, heads, sectors, cylinders;
1442 struct partition *p;
1443 uint32_t nr_sects;
blueswir1a38131b2008-12-05 17:56:40 +00001444 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +00001445
1446 bdrv_get_geometry(bs, &nb_sectors);
1447
1448 ret = bdrv_read(bs, 0, buf, 1);
1449 if (ret < 0)
1450 return -1;
1451 /* test msdos magic */
1452 if (buf[510] != 0x55 || buf[511] != 0xaa)
1453 return -1;
1454 for(i = 0; i < 4; i++) {
1455 p = ((struct partition *)(buf + 0x1be)) + i;
1456 nr_sects = le32_to_cpu(p->nr_sects);
1457 if (nr_sects && p->end_head) {
1458 /* We make the assumption that the partition terminates on
1459 a cylinder boundary */
1460 heads = p->end_head + 1;
1461 sectors = p->end_sector & 63;
1462 if (sectors == 0)
1463 continue;
1464 cylinders = nb_sectors / (heads * sectors);
1465 if (cylinders < 1 || cylinders > 16383)
1466 continue;
1467 *pheads = heads;
1468 *psectors = sectors;
1469 *pcylinders = cylinders;
1470#if 0
1471 printf("guessed geometry: LCHS=%d %d %d\n",
1472 cylinders, heads, sectors);
1473#endif
1474 return 0;
1475 }
1476 }
1477 return -1;
1478}
1479
1480void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
1481{
1482 int translation, lba_detected = 0;
1483 int cylinders, heads, secs;
blueswir1a38131b2008-12-05 17:56:40 +00001484 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +00001485
1486 /* if a geometry hint is available, use it */
1487 bdrv_get_geometry(bs, &nb_sectors);
1488 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
1489 translation = bdrv_get_translation_hint(bs);
1490 if (cylinders != 0) {
1491 *pcyls = cylinders;
1492 *pheads = heads;
1493 *psecs = secs;
1494 } else {
1495 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
1496 if (heads > 16) {
1497 /* if heads > 16, it means that a BIOS LBA
1498 translation was active, so the default
1499 hardware geometry is OK */
1500 lba_detected = 1;
1501 goto default_geometry;
1502 } else {
1503 *pcyls = cylinders;
1504 *pheads = heads;
1505 *psecs = secs;
1506 /* disable any translation to be in sync with
1507 the logical geometry */
1508 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
1509 bdrv_set_translation_hint(bs,
1510 BIOS_ATA_TRANSLATION_NONE);
1511 }
1512 }
1513 } else {
1514 default_geometry:
1515 /* if no geometry, use a standard physical disk geometry */
1516 cylinders = nb_sectors / (16 * 63);
1517
1518 if (cylinders > 16383)
1519 cylinders = 16383;
1520 else if (cylinders < 2)
1521 cylinders = 2;
1522 *pcyls = cylinders;
1523 *pheads = 16;
1524 *psecs = 63;
1525 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
1526 if ((*pcyls * *pheads) <= 131072) {
1527 bdrv_set_translation_hint(bs,
1528 BIOS_ATA_TRANSLATION_LARGE);
1529 } else {
1530 bdrv_set_translation_hint(bs,
1531 BIOS_ATA_TRANSLATION_LBA);
1532 }
1533 }
1534 }
1535 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
1536 }
1537}
1538
ths5fafdf22007-09-16 21:08:06 +00001539void bdrv_set_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +00001540 int cyls, int heads, int secs)
1541{
1542 bs->cyls = cyls;
1543 bs->heads = heads;
1544 bs->secs = secs;
1545}
1546
bellard46d47672004-11-16 01:45:27 +00001547void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
1548{
1549 bs->translation = translation;
1550}
1551
ths5fafdf22007-09-16 21:08:06 +00001552void bdrv_get_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +00001553 int *pcyls, int *pheads, int *psecs)
1554{
1555 *pcyls = bs->cyls;
1556 *pheads = bs->heads;
1557 *psecs = bs->secs;
1558}
1559
Zhi Yong Wu0563e192011-11-03 16:57:25 +08001560/* throttling disk io limits */
1561void bdrv_set_io_limits(BlockDriverState *bs,
1562 BlockIOLimit *io_limits)
1563{
1564 bs->io_limits = *io_limits;
1565 bs->io_limits_enabled = bdrv_io_limits_enabled(bs);
1566}
1567
Blue Swirl5bbdbb42011-02-12 20:43:32 +00001568/* Recognize floppy formats */
1569typedef struct FDFormat {
1570 FDriveType drive;
1571 uint8_t last_sect;
1572 uint8_t max_track;
1573 uint8_t max_head;
1574} FDFormat;
1575
1576static const FDFormat fd_formats[] = {
1577 /* First entry is default format */
1578 /* 1.44 MB 3"1/2 floppy disks */
1579 { FDRIVE_DRV_144, 18, 80, 1, },
1580 { FDRIVE_DRV_144, 20, 80, 1, },
1581 { FDRIVE_DRV_144, 21, 80, 1, },
1582 { FDRIVE_DRV_144, 21, 82, 1, },
1583 { FDRIVE_DRV_144, 21, 83, 1, },
1584 { FDRIVE_DRV_144, 22, 80, 1, },
1585 { FDRIVE_DRV_144, 23, 80, 1, },
1586 { FDRIVE_DRV_144, 24, 80, 1, },
1587 /* 2.88 MB 3"1/2 floppy disks */
1588 { FDRIVE_DRV_288, 36, 80, 1, },
1589 { FDRIVE_DRV_288, 39, 80, 1, },
1590 { FDRIVE_DRV_288, 40, 80, 1, },
1591 { FDRIVE_DRV_288, 44, 80, 1, },
1592 { FDRIVE_DRV_288, 48, 80, 1, },
1593 /* 720 kB 3"1/2 floppy disks */
1594 { FDRIVE_DRV_144, 9, 80, 1, },
1595 { FDRIVE_DRV_144, 10, 80, 1, },
1596 { FDRIVE_DRV_144, 10, 82, 1, },
1597 { FDRIVE_DRV_144, 10, 83, 1, },
1598 { FDRIVE_DRV_144, 13, 80, 1, },
1599 { FDRIVE_DRV_144, 14, 80, 1, },
1600 /* 1.2 MB 5"1/4 floppy disks */
1601 { FDRIVE_DRV_120, 15, 80, 1, },
1602 { FDRIVE_DRV_120, 18, 80, 1, },
1603 { FDRIVE_DRV_120, 18, 82, 1, },
1604 { FDRIVE_DRV_120, 18, 83, 1, },
1605 { FDRIVE_DRV_120, 20, 80, 1, },
1606 /* 720 kB 5"1/4 floppy disks */
1607 { FDRIVE_DRV_120, 9, 80, 1, },
1608 { FDRIVE_DRV_120, 11, 80, 1, },
1609 /* 360 kB 5"1/4 floppy disks */
1610 { FDRIVE_DRV_120, 9, 40, 1, },
1611 { FDRIVE_DRV_120, 9, 40, 0, },
1612 { FDRIVE_DRV_120, 10, 41, 1, },
1613 { FDRIVE_DRV_120, 10, 42, 1, },
1614 /* 320 kB 5"1/4 floppy disks */
1615 { FDRIVE_DRV_120, 8, 40, 1, },
1616 { FDRIVE_DRV_120, 8, 40, 0, },
1617 /* 360 kB must match 5"1/4 better than 3"1/2... */
1618 { FDRIVE_DRV_144, 9, 80, 0, },
1619 /* end */
1620 { FDRIVE_DRV_NONE, -1, -1, 0, },
1621};
1622
1623void bdrv_get_floppy_geometry_hint(BlockDriverState *bs, int *nb_heads,
1624 int *max_track, int *last_sect,
1625 FDriveType drive_in, FDriveType *drive)
1626{
1627 const FDFormat *parse;
1628 uint64_t nb_sectors, size;
1629 int i, first_match, match;
1630
1631 bdrv_get_geometry_hint(bs, nb_heads, max_track, last_sect);
1632 if (*nb_heads != 0 && *max_track != 0 && *last_sect != 0) {
1633 /* User defined disk */
1634 } else {
1635 bdrv_get_geometry(bs, &nb_sectors);
1636 match = -1;
1637 first_match = -1;
1638 for (i = 0; ; i++) {
1639 parse = &fd_formats[i];
1640 if (parse->drive == FDRIVE_DRV_NONE) {
1641 break;
1642 }
1643 if (drive_in == parse->drive ||
1644 drive_in == FDRIVE_DRV_NONE) {
1645 size = (parse->max_head + 1) * parse->max_track *
1646 parse->last_sect;
1647 if (nb_sectors == size) {
1648 match = i;
1649 break;
1650 }
1651 if (first_match == -1) {
1652 first_match = i;
1653 }
1654 }
1655 }
1656 if (match == -1) {
1657 if (first_match == -1) {
1658 match = 1;
1659 } else {
1660 match = first_match;
1661 }
1662 parse = &fd_formats[match];
1663 }
1664 *nb_heads = parse->max_head + 1;
1665 *max_track = parse->max_track;
1666 *last_sect = parse->last_sect;
1667 *drive = parse->drive;
1668 }
1669}
1670
bellard46d47672004-11-16 01:45:27 +00001671int bdrv_get_translation_hint(BlockDriverState *bs)
1672{
1673 return bs->translation;
1674}
1675
Markus Armbrusterabd7f682010-06-02 18:55:17 +02001676void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error,
1677 BlockErrorAction on_write_error)
1678{
1679 bs->on_read_error = on_read_error;
1680 bs->on_write_error = on_write_error;
1681}
1682
1683BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read)
1684{
1685 return is_read ? bs->on_read_error : bs->on_write_error;
1686}
1687
bellardb3380822004-03-14 21:38:54 +00001688int bdrv_is_read_only(BlockDriverState *bs)
1689{
1690 return bs->read_only;
1691}
1692
ths985a03b2007-12-24 16:10:43 +00001693int bdrv_is_sg(BlockDriverState *bs)
1694{
1695 return bs->sg;
1696}
1697
Christoph Hellwige900a7b2009-09-04 19:01:15 +02001698int bdrv_enable_write_cache(BlockDriverState *bs)
1699{
1700 return bs->enable_write_cache;
1701}
1702
bellardea2384d2004-08-01 21:59:26 +00001703int bdrv_is_encrypted(BlockDriverState *bs)
1704{
1705 if (bs->backing_hd && bs->backing_hd->encrypted)
1706 return 1;
1707 return bs->encrypted;
1708}
1709
aliguoric0f4ce72009-03-05 23:01:01 +00001710int bdrv_key_required(BlockDriverState *bs)
1711{
1712 BlockDriverState *backing_hd = bs->backing_hd;
1713
1714 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
1715 return 1;
1716 return (bs->encrypted && !bs->valid_key);
1717}
1718
bellardea2384d2004-08-01 21:59:26 +00001719int bdrv_set_key(BlockDriverState *bs, const char *key)
1720{
1721 int ret;
1722 if (bs->backing_hd && bs->backing_hd->encrypted) {
1723 ret = bdrv_set_key(bs->backing_hd, key);
1724 if (ret < 0)
1725 return ret;
1726 if (!bs->encrypted)
1727 return 0;
1728 }
Shahar Havivifd04a2a2010-03-06 00:26:13 +02001729 if (!bs->encrypted) {
1730 return -EINVAL;
1731 } else if (!bs->drv || !bs->drv->bdrv_set_key) {
1732 return -ENOMEDIUM;
1733 }
aliguoric0f4ce72009-03-05 23:01:01 +00001734 ret = bs->drv->bdrv_set_key(bs, key);
aliguoribb5fc202009-03-05 23:01:15 +00001735 if (ret < 0) {
1736 bs->valid_key = 0;
1737 } else if (!bs->valid_key) {
1738 bs->valid_key = 1;
1739 /* call the change callback now, we skipped it on open */
Markus Armbruster7d4b4ba2011-09-06 18:58:59 +02001740 bdrv_dev_change_media_cb(bs, true);
aliguoribb5fc202009-03-05 23:01:15 +00001741 }
aliguoric0f4ce72009-03-05 23:01:01 +00001742 return ret;
bellardea2384d2004-08-01 21:59:26 +00001743}
1744
1745void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
1746{
bellard19cb3732006-08-19 11:45:59 +00001747 if (!bs->drv) {
bellardea2384d2004-08-01 21:59:26 +00001748 buf[0] = '\0';
1749 } else {
1750 pstrcpy(buf, buf_size, bs->drv->format_name);
1751 }
1752}
1753
ths5fafdf22007-09-16 21:08:06 +00001754void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
bellardea2384d2004-08-01 21:59:26 +00001755 void *opaque)
1756{
1757 BlockDriver *drv;
1758
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +01001759 QLIST_FOREACH(drv, &bdrv_drivers, list) {
bellardea2384d2004-08-01 21:59:26 +00001760 it(opaque, drv->format_name);
1761 }
1762}
1763
bellardb3380822004-03-14 21:38:54 +00001764BlockDriverState *bdrv_find(const char *name)
1765{
1766 BlockDriverState *bs;
1767
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001768 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1769 if (!strcmp(name, bs->device_name)) {
bellardb3380822004-03-14 21:38:54 +00001770 return bs;
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001771 }
bellardb3380822004-03-14 21:38:54 +00001772 }
1773 return NULL;
1774}
1775
Markus Armbruster2f399b02010-06-02 18:55:20 +02001776BlockDriverState *bdrv_next(BlockDriverState *bs)
1777{
1778 if (!bs) {
1779 return QTAILQ_FIRST(&bdrv_states);
1780 }
1781 return QTAILQ_NEXT(bs, list);
1782}
1783
aliguori51de9762009-03-05 23:00:43 +00001784void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
bellard81d09122004-07-14 17:21:37 +00001785{
1786 BlockDriverState *bs;
1787
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001788 QTAILQ_FOREACH(bs, &bdrv_states, list) {
aliguori51de9762009-03-05 23:00:43 +00001789 it(opaque, bs);
bellard81d09122004-07-14 17:21:37 +00001790 }
1791}
1792
bellardea2384d2004-08-01 21:59:26 +00001793const char *bdrv_get_device_name(BlockDriverState *bs)
1794{
1795 return bs->device_name;
1796}
1797
aliguoric6ca28d2008-10-06 13:55:43 +00001798void bdrv_flush_all(void)
1799{
1800 BlockDriverState *bs;
1801
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001802 QTAILQ_FOREACH(bs, &bdrv_states, list) {
Markus Armbrusterc602a482011-08-03 15:08:10 +02001803 if (!bdrv_is_read_only(bs) && bdrv_is_inserted(bs)) {
aliguoric6ca28d2008-10-06 13:55:43 +00001804 bdrv_flush(bs);
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001805 }
1806 }
aliguoric6ca28d2008-10-06 13:55:43 +00001807}
1808
Kevin Wolff2feebb2010-04-14 17:30:35 +02001809int bdrv_has_zero_init(BlockDriverState *bs)
1810{
1811 assert(bs->drv);
1812
Kevin Wolf336c1c12010-07-28 11:26:29 +02001813 if (bs->drv->bdrv_has_zero_init) {
1814 return bs->drv->bdrv_has_zero_init(bs);
Kevin Wolff2feebb2010-04-14 17:30:35 +02001815 }
1816
1817 return 1;
1818}
1819
thsf58c7b32008-06-05 21:53:49 +00001820/*
1821 * Returns true iff the specified sector is present in the disk image. Drivers
1822 * not implementing the functionality are assumed to not support backing files,
1823 * hence all their sectors are reported as allocated.
1824 *
1825 * 'pnum' is set to the number of sectors (including and immediately following
1826 * the specified sector) that are known to be in the same
1827 * allocated/unallocated state.
1828 *
1829 * 'nb_sectors' is the max value 'pnum' should be set to.
1830 */
1831int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1832 int *pnum)
1833{
1834 int64_t n;
1835 if (!bs->drv->bdrv_is_allocated) {
1836 if (sector_num >= bs->total_sectors) {
1837 *pnum = 0;
1838 return 0;
1839 }
1840 n = bs->total_sectors - sector_num;
1841 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1842 return 1;
1843 }
1844 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1845}
1846
Luiz Capitulino2582bfe2010-02-03 12:41:01 -02001847void bdrv_mon_event(const BlockDriverState *bdrv,
1848 BlockMonEventAction action, int is_read)
1849{
1850 QObject *data;
1851 const char *action_str;
1852
1853 switch (action) {
1854 case BDRV_ACTION_REPORT:
1855 action_str = "report";
1856 break;
1857 case BDRV_ACTION_IGNORE:
1858 action_str = "ignore";
1859 break;
1860 case BDRV_ACTION_STOP:
1861 action_str = "stop";
1862 break;
1863 default:
1864 abort();
1865 }
1866
1867 data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
1868 bdrv->device_name,
1869 action_str,
1870 is_read ? "read" : "write");
1871 monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data);
1872
1873 qobject_decref(data);
1874}
1875
Luiz Capitulinob2023812011-09-21 17:16:47 -03001876BlockInfoList *qmp_query_block(Error **errp)
bellardb3380822004-03-14 21:38:54 +00001877{
Luiz Capitulinob2023812011-09-21 17:16:47 -03001878 BlockInfoList *head = NULL, *cur_item = NULL;
bellardb3380822004-03-14 21:38:54 +00001879 BlockDriverState *bs;
1880
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001881 QTAILQ_FOREACH(bs, &bdrv_states, list) {
Luiz Capitulinob2023812011-09-21 17:16:47 -03001882 BlockInfoList *info = g_malloc0(sizeof(*info));
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001883
Luiz Capitulinob2023812011-09-21 17:16:47 -03001884 info->value = g_malloc0(sizeof(*info->value));
1885 info->value->device = g_strdup(bs->device_name);
1886 info->value->type = g_strdup("unknown");
1887 info->value->locked = bdrv_dev_is_medium_locked(bs);
1888 info->value->removable = bdrv_dev_has_removable_media(bs);
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001889
Markus Armbrustere4def802011-09-06 18:58:53 +02001890 if (bdrv_dev_has_removable_media(bs)) {
Luiz Capitulinob2023812011-09-21 17:16:47 -03001891 info->value->has_tray_open = true;
1892 info->value->tray_open = bdrv_dev_is_tray_open(bs);
Markus Armbrustere4def802011-09-06 18:58:53 +02001893 }
Luiz Capitulinof04ef602011-09-26 17:43:54 -03001894
1895 if (bdrv_iostatus_is_enabled(bs)) {
Luiz Capitulinob2023812011-09-21 17:16:47 -03001896 info->value->has_io_status = true;
1897 info->value->io_status = bs->iostatus;
Luiz Capitulinof04ef602011-09-26 17:43:54 -03001898 }
1899
bellard19cb3732006-08-19 11:45:59 +00001900 if (bs->drv) {
Luiz Capitulinob2023812011-09-21 17:16:47 -03001901 info->value->has_inserted = true;
1902 info->value->inserted = g_malloc0(sizeof(*info->value->inserted));
1903 info->value->inserted->file = g_strdup(bs->filename);
1904 info->value->inserted->ro = bs->read_only;
1905 info->value->inserted->drv = g_strdup(bs->drv->format_name);
1906 info->value->inserted->encrypted = bs->encrypted;
1907 if (bs->backing_file[0]) {
1908 info->value->inserted->has_backing_file = true;
1909 info->value->inserted->backing_file = g_strdup(bs->backing_file);
aliguori376253e2009-03-05 23:01:23 +00001910 }
bellardb3380822004-03-14 21:38:54 +00001911 }
Luiz Capitulinob2023812011-09-21 17:16:47 -03001912
1913 /* XXX: waiting for the qapi to support GSList */
1914 if (!cur_item) {
1915 head = cur_item = info;
1916 } else {
1917 cur_item->next = info;
1918 cur_item = info;
1919 }
bellardb3380822004-03-14 21:38:54 +00001920 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001921
Luiz Capitulinob2023812011-09-21 17:16:47 -03001922 return head;
bellardb3380822004-03-14 21:38:54 +00001923}
thsa36e69d2007-12-02 05:18:19 +00001924
Luiz Capitulinof11f57e2011-09-22 15:56:36 -03001925/* Consider exposing this as a full fledged QMP command */
1926static BlockStats *qmp_query_blockstat(const BlockDriverState *bs, Error **errp)
thsa36e69d2007-12-02 05:18:19 +00001927{
Luiz Capitulinof11f57e2011-09-22 15:56:36 -03001928 BlockStats *s;
Luiz Capitulino218a5362009-12-10 17:16:07 -02001929
Luiz Capitulinof11f57e2011-09-22 15:56:36 -03001930 s = g_malloc0(sizeof(*s));
Luiz Capitulino218a5362009-12-10 17:16:07 -02001931
Luiz Capitulinof11f57e2011-09-22 15:56:36 -03001932 if (bs->device_name[0]) {
1933 s->has_device = true;
1934 s->device = g_strdup(bs->device_name);
Kevin Wolf294cc352010-04-28 14:34:01 +02001935 }
1936
Luiz Capitulinof11f57e2011-09-22 15:56:36 -03001937 s->stats = g_malloc0(sizeof(*s->stats));
1938 s->stats->rd_bytes = bs->nr_bytes[BDRV_ACCT_READ];
1939 s->stats->wr_bytes = bs->nr_bytes[BDRV_ACCT_WRITE];
1940 s->stats->rd_operations = bs->nr_ops[BDRV_ACCT_READ];
1941 s->stats->wr_operations = bs->nr_ops[BDRV_ACCT_WRITE];
1942 s->stats->wr_highest_offset = bs->wr_highest_sector * BDRV_SECTOR_SIZE;
1943 s->stats->flush_operations = bs->nr_ops[BDRV_ACCT_FLUSH];
1944 s->stats->wr_total_time_ns = bs->total_time_ns[BDRV_ACCT_WRITE];
1945 s->stats->rd_total_time_ns = bs->total_time_ns[BDRV_ACCT_READ];
1946 s->stats->flush_total_time_ns = bs->total_time_ns[BDRV_ACCT_FLUSH];
1947
Kevin Wolf294cc352010-04-28 14:34:01 +02001948 if (bs->file) {
Luiz Capitulinof11f57e2011-09-22 15:56:36 -03001949 s->has_parent = true;
1950 s->parent = qmp_query_blockstat(bs->file, NULL);
Kevin Wolf294cc352010-04-28 14:34:01 +02001951 }
1952
Luiz Capitulinof11f57e2011-09-22 15:56:36 -03001953 return s;
Kevin Wolf294cc352010-04-28 14:34:01 +02001954}
1955
Luiz Capitulinof11f57e2011-09-22 15:56:36 -03001956BlockStatsList *qmp_query_blockstats(Error **errp)
Luiz Capitulino218a5362009-12-10 17:16:07 -02001957{
Luiz Capitulinof11f57e2011-09-22 15:56:36 -03001958 BlockStatsList *head = NULL, *cur_item = NULL;
thsa36e69d2007-12-02 05:18:19 +00001959 BlockDriverState *bs;
1960
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001961 QTAILQ_FOREACH(bs, &bdrv_states, list) {
Luiz Capitulinof11f57e2011-09-22 15:56:36 -03001962 BlockStatsList *info = g_malloc0(sizeof(*info));
1963 info->value = qmp_query_blockstat(bs, NULL);
1964
1965 /* XXX: waiting for the qapi to support GSList */
1966 if (!cur_item) {
1967 head = cur_item = info;
1968 } else {
1969 cur_item->next = info;
1970 cur_item = info;
1971 }
thsa36e69d2007-12-02 05:18:19 +00001972 }
Luiz Capitulino218a5362009-12-10 17:16:07 -02001973
Luiz Capitulinof11f57e2011-09-22 15:56:36 -03001974 return head;
thsa36e69d2007-12-02 05:18:19 +00001975}
bellardea2384d2004-08-01 21:59:26 +00001976
aliguori045df332009-03-05 23:00:48 +00001977const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1978{
1979 if (bs->backing_hd && bs->backing_hd->encrypted)
1980 return bs->backing_file;
1981 else if (bs->encrypted)
1982 return bs->filename;
1983 else
1984 return NULL;
1985}
1986
ths5fafdf22007-09-16 21:08:06 +00001987void bdrv_get_backing_filename(BlockDriverState *bs,
bellard83f64092006-08-01 16:21:11 +00001988 char *filename, int filename_size)
bellardea2384d2004-08-01 21:59:26 +00001989{
Kevin Wolf3574c602011-10-26 11:02:11 +02001990 pstrcpy(filename, filename_size, bs->backing_file);
bellardea2384d2004-08-01 21:59:26 +00001991}
1992
ths5fafdf22007-09-16 21:08:06 +00001993int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
bellardfaea38e2006-08-05 21:31:00 +00001994 const uint8_t *buf, int nb_sectors)
1995{
1996 BlockDriver *drv = bs->drv;
1997 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001998 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001999 if (!drv->bdrv_write_compressed)
2000 return -ENOTSUP;
Kevin Wolffbb7b4e2009-05-08 14:47:24 +02002001 if (bdrv_check_request(bs, sector_num, nb_sectors))
2002 return -EIO;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002003
Jan Kiszkac6d22832009-11-30 18:21:20 +01002004 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002005 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
2006 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002007
bellardfaea38e2006-08-05 21:31:00 +00002008 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
2009}
ths3b46e622007-09-17 08:09:54 +00002010
bellardfaea38e2006-08-05 21:31:00 +00002011int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
2012{
2013 BlockDriver *drv = bs->drv;
2014 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002015 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00002016 if (!drv->bdrv_get_info)
2017 return -ENOTSUP;
2018 memset(bdi, 0, sizeof(*bdi));
2019 return drv->bdrv_get_info(bs, bdi);
2020}
2021
Christoph Hellwig45566e92009-07-10 23:11:57 +02002022int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
2023 int64_t pos, int size)
aliguori178e08a2009-04-05 19:10:55 +00002024{
2025 BlockDriver *drv = bs->drv;
2026 if (!drv)
2027 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09002028 if (drv->bdrv_save_vmstate)
2029 return drv->bdrv_save_vmstate(bs, buf, pos, size);
2030 if (bs->file)
2031 return bdrv_save_vmstate(bs->file, buf, pos, size);
2032 return -ENOTSUP;
aliguori178e08a2009-04-05 19:10:55 +00002033}
2034
Christoph Hellwig45566e92009-07-10 23:11:57 +02002035int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
2036 int64_t pos, int size)
aliguori178e08a2009-04-05 19:10:55 +00002037{
2038 BlockDriver *drv = bs->drv;
2039 if (!drv)
2040 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09002041 if (drv->bdrv_load_vmstate)
2042 return drv->bdrv_load_vmstate(bs, buf, pos, size);
2043 if (bs->file)
2044 return bdrv_load_vmstate(bs->file, buf, pos, size);
2045 return -ENOTSUP;
aliguori178e08a2009-04-05 19:10:55 +00002046}
2047
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01002048void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
2049{
2050 BlockDriver *drv = bs->drv;
2051
2052 if (!drv || !drv->bdrv_debug_event) {
2053 return;
2054 }
2055
2056 return drv->bdrv_debug_event(bs, event);
2057
2058}
2059
bellardfaea38e2006-08-05 21:31:00 +00002060/**************************************************************/
2061/* handling of snapshots */
2062
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002063int bdrv_can_snapshot(BlockDriverState *bs)
2064{
2065 BlockDriver *drv = bs->drv;
Markus Armbruster07b70bf2011-08-03 15:08:11 +02002066 if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002067 return 0;
2068 }
2069
2070 if (!drv->bdrv_snapshot_create) {
2071 if (bs->file != NULL) {
2072 return bdrv_can_snapshot(bs->file);
2073 }
2074 return 0;
2075 }
2076
2077 return 1;
2078}
2079
Blue Swirl199630b2010-07-25 20:49:34 +00002080int bdrv_is_snapshot(BlockDriverState *bs)
2081{
2082 return !!(bs->open_flags & BDRV_O_SNAPSHOT);
2083}
2084
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002085BlockDriverState *bdrv_snapshots(void)
2086{
2087 BlockDriverState *bs;
2088
Markus Armbruster3ac906f2010-07-01 09:30:38 +02002089 if (bs_snapshots) {
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002090 return bs_snapshots;
Markus Armbruster3ac906f2010-07-01 09:30:38 +02002091 }
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002092
2093 bs = NULL;
2094 while ((bs = bdrv_next(bs))) {
2095 if (bdrv_can_snapshot(bs)) {
Markus Armbruster3ac906f2010-07-01 09:30:38 +02002096 bs_snapshots = bs;
2097 return bs;
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002098 }
2099 }
2100 return NULL;
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002101}
2102
ths5fafdf22007-09-16 21:08:06 +00002103int bdrv_snapshot_create(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00002104 QEMUSnapshotInfo *sn_info)
2105{
2106 BlockDriver *drv = bs->drv;
2107 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002108 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09002109 if (drv->bdrv_snapshot_create)
2110 return drv->bdrv_snapshot_create(bs, sn_info);
2111 if (bs->file)
2112 return bdrv_snapshot_create(bs->file, sn_info);
2113 return -ENOTSUP;
bellardfaea38e2006-08-05 21:31:00 +00002114}
2115
ths5fafdf22007-09-16 21:08:06 +00002116int bdrv_snapshot_goto(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00002117 const char *snapshot_id)
2118{
2119 BlockDriver *drv = bs->drv;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09002120 int ret, open_ret;
2121
bellardfaea38e2006-08-05 21:31:00 +00002122 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002123 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09002124 if (drv->bdrv_snapshot_goto)
2125 return drv->bdrv_snapshot_goto(bs, snapshot_id);
2126
2127 if (bs->file) {
2128 drv->bdrv_close(bs);
2129 ret = bdrv_snapshot_goto(bs->file, snapshot_id);
2130 open_ret = drv->bdrv_open(bs, bs->open_flags);
2131 if (open_ret < 0) {
2132 bdrv_delete(bs->file);
2133 bs->drv = NULL;
2134 return open_ret;
2135 }
2136 return ret;
2137 }
2138
2139 return -ENOTSUP;
bellardfaea38e2006-08-05 21:31:00 +00002140}
2141
2142int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
2143{
2144 BlockDriver *drv = bs->drv;
2145 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002146 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09002147 if (drv->bdrv_snapshot_delete)
2148 return drv->bdrv_snapshot_delete(bs, snapshot_id);
2149 if (bs->file)
2150 return bdrv_snapshot_delete(bs->file, snapshot_id);
2151 return -ENOTSUP;
bellardfaea38e2006-08-05 21:31:00 +00002152}
2153
ths5fafdf22007-09-16 21:08:06 +00002154int bdrv_snapshot_list(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00002155 QEMUSnapshotInfo **psn_info)
2156{
2157 BlockDriver *drv = bs->drv;
2158 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002159 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09002160 if (drv->bdrv_snapshot_list)
2161 return drv->bdrv_snapshot_list(bs, psn_info);
2162 if (bs->file)
2163 return bdrv_snapshot_list(bs->file, psn_info);
2164 return -ENOTSUP;
bellardfaea38e2006-08-05 21:31:00 +00002165}
2166
edison51ef6722010-09-21 19:58:41 -07002167int bdrv_snapshot_load_tmp(BlockDriverState *bs,
2168 const char *snapshot_name)
2169{
2170 BlockDriver *drv = bs->drv;
2171 if (!drv) {
2172 return -ENOMEDIUM;
2173 }
2174 if (!bs->read_only) {
2175 return -EINVAL;
2176 }
2177 if (drv->bdrv_snapshot_load_tmp) {
2178 return drv->bdrv_snapshot_load_tmp(bs, snapshot_name);
2179 }
2180 return -ENOTSUP;
2181}
2182
bellardfaea38e2006-08-05 21:31:00 +00002183#define NB_SUFFIXES 4
2184
2185char *get_human_readable_size(char *buf, int buf_size, int64_t size)
2186{
2187 static const char suffixes[NB_SUFFIXES] = "KMGT";
2188 int64_t base;
2189 int i;
2190
2191 if (size <= 999) {
2192 snprintf(buf, buf_size, "%" PRId64, size);
2193 } else {
2194 base = 1024;
2195 for(i = 0; i < NB_SUFFIXES; i++) {
2196 if (size < (10 * base)) {
ths5fafdf22007-09-16 21:08:06 +00002197 snprintf(buf, buf_size, "%0.1f%c",
bellardfaea38e2006-08-05 21:31:00 +00002198 (double)size / base,
2199 suffixes[i]);
2200 break;
2201 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
ths5fafdf22007-09-16 21:08:06 +00002202 snprintf(buf, buf_size, "%" PRId64 "%c",
bellardfaea38e2006-08-05 21:31:00 +00002203 ((size + (base >> 1)) / base),
2204 suffixes[i]);
2205 break;
2206 }
2207 base = base * 1024;
2208 }
2209 }
2210 return buf;
2211}
2212
2213char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
2214{
2215 char buf1[128], date_buf[128], clock_buf[128];
bellard3b9f94e2007-01-07 17:27:07 +00002216#ifdef _WIN32
2217 struct tm *ptm;
2218#else
bellardfaea38e2006-08-05 21:31:00 +00002219 struct tm tm;
bellard3b9f94e2007-01-07 17:27:07 +00002220#endif
bellardfaea38e2006-08-05 21:31:00 +00002221 time_t ti;
2222 int64_t secs;
2223
2224 if (!sn) {
ths5fafdf22007-09-16 21:08:06 +00002225 snprintf(buf, buf_size,
2226 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00002227 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
2228 } else {
2229 ti = sn->date_sec;
bellard3b9f94e2007-01-07 17:27:07 +00002230#ifdef _WIN32
2231 ptm = localtime(&ti);
2232 strftime(date_buf, sizeof(date_buf),
2233 "%Y-%m-%d %H:%M:%S", ptm);
2234#else
bellardfaea38e2006-08-05 21:31:00 +00002235 localtime_r(&ti, &tm);
2236 strftime(date_buf, sizeof(date_buf),
2237 "%Y-%m-%d %H:%M:%S", &tm);
bellard3b9f94e2007-01-07 17:27:07 +00002238#endif
bellardfaea38e2006-08-05 21:31:00 +00002239 secs = sn->vm_clock_nsec / 1000000000;
2240 snprintf(clock_buf, sizeof(clock_buf),
2241 "%02d:%02d:%02d.%03d",
2242 (int)(secs / 3600),
2243 (int)((secs / 60) % 60),
ths5fafdf22007-09-16 21:08:06 +00002244 (int)(secs % 60),
bellardfaea38e2006-08-05 21:31:00 +00002245 (int)((sn->vm_clock_nsec / 1000000) % 1000));
2246 snprintf(buf, buf_size,
ths5fafdf22007-09-16 21:08:06 +00002247 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00002248 sn->id_str, sn->name,
2249 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
2250 date_buf,
2251 clock_buf);
2252 }
2253 return buf;
2254}
2255
bellard83f64092006-08-01 16:21:11 +00002256/**************************************************************/
2257/* async I/Os */
2258
aliguori3b69e4b2009-01-22 16:59:24 +00002259BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
aliguorif141eaf2009-04-07 18:43:24 +00002260 QEMUIOVector *qiov, int nb_sectors,
aliguori3b69e4b2009-01-22 16:59:24 +00002261 BlockDriverCompletionFunc *cb, void *opaque)
2262{
Stefan Hajnoczibbf0a442010-10-05 14:28:53 +01002263 trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque);
2264
Stefan Hajnoczib2a61372011-10-13 13:08:23 +01002265 return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors,
Stefan Hajnoczi8c5873d2011-10-13 21:09:28 +01002266 cb, opaque, false);
bellard83f64092006-08-01 16:21:11 +00002267}
2268
aliguorif141eaf2009-04-07 18:43:24 +00002269BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
2270 QEMUIOVector *qiov, int nb_sectors,
2271 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00002272{
Stefan Hajnoczibbf0a442010-10-05 14:28:53 +01002273 trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque);
2274
Stefan Hajnoczi1a6e1152011-10-13 13:08:25 +01002275 return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors,
Stefan Hajnoczi8c5873d2011-10-13 21:09:28 +01002276 cb, opaque, true);
bellard83f64092006-08-01 16:21:11 +00002277}
2278
Kevin Wolf40b4f532009-09-09 17:53:37 +02002279
2280typedef struct MultiwriteCB {
2281 int error;
2282 int num_requests;
2283 int num_callbacks;
2284 struct {
2285 BlockDriverCompletionFunc *cb;
2286 void *opaque;
2287 QEMUIOVector *free_qiov;
2288 void *free_buf;
2289 } callbacks[];
2290} MultiwriteCB;
2291
2292static void multiwrite_user_cb(MultiwriteCB *mcb)
2293{
2294 int i;
2295
2296 for (i = 0; i < mcb->num_callbacks; i++) {
2297 mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
Stefan Hajnoczi1e1ea482010-04-21 20:35:45 +01002298 if (mcb->callbacks[i].free_qiov) {
2299 qemu_iovec_destroy(mcb->callbacks[i].free_qiov);
2300 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002301 g_free(mcb->callbacks[i].free_qiov);
Herve Poussineauf8a83242010-01-24 21:23:56 +00002302 qemu_vfree(mcb->callbacks[i].free_buf);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002303 }
2304}
2305
2306static void multiwrite_cb(void *opaque, int ret)
2307{
2308 MultiwriteCB *mcb = opaque;
2309
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +01002310 trace_multiwrite_cb(mcb, ret);
2311
Kevin Wolfcb6d3ca2010-04-01 22:48:44 +02002312 if (ret < 0 && !mcb->error) {
Kevin Wolf40b4f532009-09-09 17:53:37 +02002313 mcb->error = ret;
Kevin Wolf40b4f532009-09-09 17:53:37 +02002314 }
2315
2316 mcb->num_requests--;
2317 if (mcb->num_requests == 0) {
Kevin Wolfde189a12010-07-01 16:08:51 +02002318 multiwrite_user_cb(mcb);
Anthony Liguori7267c092011-08-20 22:09:37 -05002319 g_free(mcb);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002320 }
2321}
2322
2323static int multiwrite_req_compare(const void *a, const void *b)
2324{
Christoph Hellwig77be4362010-05-19 20:53:10 +02002325 const BlockRequest *req1 = a, *req2 = b;
2326
2327 /*
2328 * Note that we can't simply subtract req2->sector from req1->sector
2329 * here as that could overflow the return value.
2330 */
2331 if (req1->sector > req2->sector) {
2332 return 1;
2333 } else if (req1->sector < req2->sector) {
2334 return -1;
2335 } else {
2336 return 0;
2337 }
Kevin Wolf40b4f532009-09-09 17:53:37 +02002338}
2339
2340/*
2341 * Takes a bunch of requests and tries to merge them. Returns the number of
2342 * requests that remain after merging.
2343 */
2344static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
2345 int num_reqs, MultiwriteCB *mcb)
2346{
2347 int i, outidx;
2348
2349 // Sort requests by start sector
2350 qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
2351
2352 // Check if adjacent requests touch the same clusters. If so, combine them,
2353 // filling up gaps with zero sectors.
2354 outidx = 0;
2355 for (i = 1; i < num_reqs; i++) {
2356 int merge = 0;
2357 int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
2358
2359 // This handles the cases that are valid for all block drivers, namely
2360 // exactly sequential writes and overlapping writes.
2361 if (reqs[i].sector <= oldreq_last) {
2362 merge = 1;
2363 }
2364
2365 // The block driver may decide that it makes sense to combine requests
2366 // even if there is a gap of some sectors between them. In this case,
2367 // the gap is filled with zeros (therefore only applicable for yet
2368 // unused space in format like qcow2).
2369 if (!merge && bs->drv->bdrv_merge_requests) {
2370 merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]);
2371 }
2372
Christoph Hellwige2a305f2010-01-26 14:49:08 +01002373 if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) {
2374 merge = 0;
2375 }
2376
Kevin Wolf40b4f532009-09-09 17:53:37 +02002377 if (merge) {
2378 size_t size;
Anthony Liguori7267c092011-08-20 22:09:37 -05002379 QEMUIOVector *qiov = g_malloc0(sizeof(*qiov));
Kevin Wolf40b4f532009-09-09 17:53:37 +02002380 qemu_iovec_init(qiov,
2381 reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
2382
2383 // Add the first request to the merged one. If the requests are
2384 // overlapping, drop the last sectors of the first request.
2385 size = (reqs[i].sector - reqs[outidx].sector) << 9;
2386 qemu_iovec_concat(qiov, reqs[outidx].qiov, size);
2387
2388 // We might need to add some zeros between the two requests
2389 if (reqs[i].sector > oldreq_last) {
2390 size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
2391 uint8_t *buf = qemu_blockalign(bs, zero_bytes);
2392 memset(buf, 0, zero_bytes);
2393 qemu_iovec_add(qiov, buf, zero_bytes);
2394 mcb->callbacks[i].free_buf = buf;
2395 }
2396
2397 // Add the second request
2398 qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size);
2399
Kevin Wolfcbf1dff2010-05-21 11:09:42 +02002400 reqs[outidx].nb_sectors = qiov->size >> 9;
Kevin Wolf40b4f532009-09-09 17:53:37 +02002401 reqs[outidx].qiov = qiov;
2402
2403 mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
2404 } else {
2405 outidx++;
2406 reqs[outidx].sector = reqs[i].sector;
2407 reqs[outidx].nb_sectors = reqs[i].nb_sectors;
2408 reqs[outidx].qiov = reqs[i].qiov;
2409 }
2410 }
2411
2412 return outidx + 1;
2413}
2414
2415/*
2416 * Submit multiple AIO write requests at once.
2417 *
2418 * On success, the function returns 0 and all requests in the reqs array have
2419 * been submitted. In error case this function returns -1, and any of the
2420 * requests may or may not be submitted yet. In particular, this means that the
2421 * callback will be called for some of the requests, for others it won't. The
2422 * caller must check the error field of the BlockRequest to wait for the right
2423 * callbacks (if error != 0, no callback will be called).
2424 *
2425 * The implementation may modify the contents of the reqs array, e.g. to merge
2426 * requests. However, the fields opaque and error are left unmodified as they
2427 * are used to signal failure for a single request to the caller.
2428 */
2429int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
2430{
2431 BlockDriverAIOCB *acb;
2432 MultiwriteCB *mcb;
2433 int i;
2434
Ryan Harper301db7c2011-03-07 10:01:04 -06002435 /* don't submit writes if we don't have a medium */
2436 if (bs->drv == NULL) {
2437 for (i = 0; i < num_reqs; i++) {
2438 reqs[i].error = -ENOMEDIUM;
2439 }
2440 return -1;
2441 }
2442
Kevin Wolf40b4f532009-09-09 17:53:37 +02002443 if (num_reqs == 0) {
2444 return 0;
2445 }
2446
2447 // Create MultiwriteCB structure
Anthony Liguori7267c092011-08-20 22:09:37 -05002448 mcb = g_malloc0(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
Kevin Wolf40b4f532009-09-09 17:53:37 +02002449 mcb->num_requests = 0;
2450 mcb->num_callbacks = num_reqs;
2451
2452 for (i = 0; i < num_reqs; i++) {
2453 mcb->callbacks[i].cb = reqs[i].cb;
2454 mcb->callbacks[i].opaque = reqs[i].opaque;
2455 }
2456
2457 // Check for mergable requests
2458 num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
2459
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +01002460 trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs);
2461
Kevin Wolf453f9a12010-07-02 14:01:21 +02002462 /*
2463 * Run the aio requests. As soon as one request can't be submitted
2464 * successfully, fail all requests that are not yet submitted (we must
2465 * return failure for all requests anyway)
2466 *
2467 * num_requests cannot be set to the right value immediately: If
2468 * bdrv_aio_writev fails for some request, num_requests would be too high
2469 * and therefore multiwrite_cb() would never recognize the multiwrite
2470 * request as completed. We also cannot use the loop variable i to set it
2471 * when the first request fails because the callback may already have been
2472 * called for previously submitted requests. Thus, num_requests must be
2473 * incremented for each request that is submitted.
2474 *
2475 * The problem that callbacks may be called early also means that we need
2476 * to take care that num_requests doesn't become 0 before all requests are
2477 * submitted - multiwrite_cb() would consider the multiwrite request
2478 * completed. A dummy request that is "completed" by a manual call to
2479 * multiwrite_cb() takes care of this.
2480 */
2481 mcb->num_requests = 1;
2482
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +01002483 // Run the aio requests
Kevin Wolf40b4f532009-09-09 17:53:37 +02002484 for (i = 0; i < num_reqs; i++) {
Kevin Wolf453f9a12010-07-02 14:01:21 +02002485 mcb->num_requests++;
Kevin Wolf40b4f532009-09-09 17:53:37 +02002486 acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
2487 reqs[i].nb_sectors, multiwrite_cb, mcb);
2488
2489 if (acb == NULL) {
2490 // We can only fail the whole thing if no request has been
2491 // submitted yet. Otherwise we'll wait for the submitted AIOs to
2492 // complete and report the error in the callback.
Kevin Wolf453f9a12010-07-02 14:01:21 +02002493 if (i == 0) {
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +01002494 trace_bdrv_aio_multiwrite_earlyfail(mcb);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002495 goto fail;
2496 } else {
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +01002497 trace_bdrv_aio_multiwrite_latefail(mcb, i);
Kevin Wolf7eb58a62010-04-06 18:24:07 +02002498 multiwrite_cb(mcb, -EIO);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002499 break;
2500 }
Kevin Wolf40b4f532009-09-09 17:53:37 +02002501 }
2502 }
2503
Kevin Wolf453f9a12010-07-02 14:01:21 +02002504 /* Complete the dummy request */
2505 multiwrite_cb(mcb, 0);
2506
Kevin Wolf40b4f532009-09-09 17:53:37 +02002507 return 0;
2508
2509fail:
Kevin Wolf453f9a12010-07-02 14:01:21 +02002510 for (i = 0; i < mcb->num_callbacks; i++) {
2511 reqs[i].error = -EIO;
2512 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002513 g_free(mcb);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002514 return -1;
2515}
2516
bellard83f64092006-08-01 16:21:11 +00002517void bdrv_aio_cancel(BlockDriverAIOCB *acb)
pbrookce1a14d2006-08-07 02:38:06 +00002518{
aliguori6bbff9a2009-03-20 18:25:59 +00002519 acb->pool->cancel(acb);
bellard83f64092006-08-01 16:21:11 +00002520}
2521
pbrookce1a14d2006-08-07 02:38:06 +00002522
bellard83f64092006-08-01 16:21:11 +00002523/**************************************************************/
2524/* async block device emulation */
2525
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002526typedef struct BlockDriverAIOCBSync {
2527 BlockDriverAIOCB common;
2528 QEMUBH *bh;
2529 int ret;
2530 /* vector translation state */
2531 QEMUIOVector *qiov;
2532 uint8_t *bounce;
2533 int is_write;
2534} BlockDriverAIOCBSync;
2535
2536static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
2537{
Kevin Wolfb666d232010-05-05 11:44:39 +02002538 BlockDriverAIOCBSync *acb =
2539 container_of(blockacb, BlockDriverAIOCBSync, common);
Dor Laor6a7ad292009-06-01 12:07:23 +03002540 qemu_bh_delete(acb->bh);
Avi Kivity36afc452009-06-23 16:20:36 +03002541 acb->bh = NULL;
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002542 qemu_aio_release(acb);
2543}
2544
2545static AIOPool bdrv_em_aio_pool = {
2546 .aiocb_size = sizeof(BlockDriverAIOCBSync),
2547 .cancel = bdrv_aio_cancel_em,
2548};
2549
bellard83f64092006-08-01 16:21:11 +00002550static void bdrv_aio_bh_cb(void *opaque)
bellardbeac80c2006-06-26 20:08:57 +00002551{
pbrookce1a14d2006-08-07 02:38:06 +00002552 BlockDriverAIOCBSync *acb = opaque;
aliguorif141eaf2009-04-07 18:43:24 +00002553
aliguorif141eaf2009-04-07 18:43:24 +00002554 if (!acb->is_write)
2555 qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
aliguoriceb42de2009-04-07 18:43:28 +00002556 qemu_vfree(acb->bounce);
pbrookce1a14d2006-08-07 02:38:06 +00002557 acb->common.cb(acb->common.opaque, acb->ret);
Dor Laor6a7ad292009-06-01 12:07:23 +03002558 qemu_bh_delete(acb->bh);
Avi Kivity36afc452009-06-23 16:20:36 +03002559 acb->bh = NULL;
pbrookce1a14d2006-08-07 02:38:06 +00002560 qemu_aio_release(acb);
bellardbeac80c2006-06-26 20:08:57 +00002561}
bellardbeac80c2006-06-26 20:08:57 +00002562
aliguorif141eaf2009-04-07 18:43:24 +00002563static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
2564 int64_t sector_num,
2565 QEMUIOVector *qiov,
2566 int nb_sectors,
2567 BlockDriverCompletionFunc *cb,
2568 void *opaque,
2569 int is_write)
2570
bellardea2384d2004-08-01 21:59:26 +00002571{
pbrookce1a14d2006-08-07 02:38:06 +00002572 BlockDriverAIOCBSync *acb;
pbrookce1a14d2006-08-07 02:38:06 +00002573
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002574 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
aliguorif141eaf2009-04-07 18:43:24 +00002575 acb->is_write = is_write;
2576 acb->qiov = qiov;
aliguorie268ca52009-04-22 20:20:00 +00002577 acb->bounce = qemu_blockalign(bs, qiov->size);
aliguorif141eaf2009-04-07 18:43:24 +00002578
pbrookce1a14d2006-08-07 02:38:06 +00002579 if (!acb->bh)
2580 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
aliguorif141eaf2009-04-07 18:43:24 +00002581
2582 if (is_write) {
2583 qemu_iovec_to_buffer(acb->qiov, acb->bounce);
Stefan Hajnoczi1ed20ac2011-10-13 13:08:21 +01002584 acb->ret = bs->drv->bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
aliguorif141eaf2009-04-07 18:43:24 +00002585 } else {
Stefan Hajnoczi1ed20ac2011-10-13 13:08:21 +01002586 acb->ret = bs->drv->bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
aliguorif141eaf2009-04-07 18:43:24 +00002587 }
2588
pbrookce1a14d2006-08-07 02:38:06 +00002589 qemu_bh_schedule(acb->bh);
aliguorif141eaf2009-04-07 18:43:24 +00002590
pbrookce1a14d2006-08-07 02:38:06 +00002591 return &acb->common;
pbrook7a6cba62006-06-04 11:39:07 +00002592}
2593
aliguorif141eaf2009-04-07 18:43:24 +00002594static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
2595 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
pbrookce1a14d2006-08-07 02:38:06 +00002596 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00002597{
aliguorif141eaf2009-04-07 18:43:24 +00002598 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
bellard83f64092006-08-01 16:21:11 +00002599}
2600
aliguorif141eaf2009-04-07 18:43:24 +00002601static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
2602 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2603 BlockDriverCompletionFunc *cb, void *opaque)
2604{
2605 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
2606}
2607
Kevin Wolf68485422011-06-30 10:05:46 +02002608
2609typedef struct BlockDriverAIOCBCoroutine {
2610 BlockDriverAIOCB common;
2611 BlockRequest req;
2612 bool is_write;
2613 QEMUBH* bh;
2614} BlockDriverAIOCBCoroutine;
2615
2616static void bdrv_aio_co_cancel_em(BlockDriverAIOCB *blockacb)
2617{
2618 qemu_aio_flush();
2619}
2620
2621static AIOPool bdrv_em_co_aio_pool = {
2622 .aiocb_size = sizeof(BlockDriverAIOCBCoroutine),
2623 .cancel = bdrv_aio_co_cancel_em,
2624};
2625
Paolo Bonzini35246a62011-10-14 10:41:29 +02002626static void bdrv_co_em_bh(void *opaque)
Kevin Wolf68485422011-06-30 10:05:46 +02002627{
2628 BlockDriverAIOCBCoroutine *acb = opaque;
2629
2630 acb->common.cb(acb->common.opaque, acb->req.error);
2631 qemu_bh_delete(acb->bh);
2632 qemu_aio_release(acb);
2633}
2634
Stefan Hajnoczib2a61372011-10-13 13:08:23 +01002635/* Invoke bdrv_co_do_readv/bdrv_co_do_writev */
2636static void coroutine_fn bdrv_co_do_rw(void *opaque)
2637{
2638 BlockDriverAIOCBCoroutine *acb = opaque;
2639 BlockDriverState *bs = acb->common.bs;
2640
2641 if (!acb->is_write) {
2642 acb->req.error = bdrv_co_do_readv(bs, acb->req.sector,
2643 acb->req.nb_sectors, acb->req.qiov);
2644 } else {
2645 acb->req.error = bdrv_co_do_writev(bs, acb->req.sector,
2646 acb->req.nb_sectors, acb->req.qiov);
2647 }
2648
Paolo Bonzini35246a62011-10-14 10:41:29 +02002649 acb->bh = qemu_bh_new(bdrv_co_em_bh, acb);
Stefan Hajnoczib2a61372011-10-13 13:08:23 +01002650 qemu_bh_schedule(acb->bh);
2651}
2652
Kevin Wolf68485422011-06-30 10:05:46 +02002653static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs,
2654 int64_t sector_num,
2655 QEMUIOVector *qiov,
2656 int nb_sectors,
2657 BlockDriverCompletionFunc *cb,
2658 void *opaque,
Stefan Hajnoczi8c5873d2011-10-13 21:09:28 +01002659 bool is_write)
Kevin Wolf68485422011-06-30 10:05:46 +02002660{
2661 Coroutine *co;
2662 BlockDriverAIOCBCoroutine *acb;
2663
2664 acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque);
2665 acb->req.sector = sector_num;
2666 acb->req.nb_sectors = nb_sectors;
2667 acb->req.qiov = qiov;
2668 acb->is_write = is_write;
2669
Stefan Hajnoczi8c5873d2011-10-13 21:09:28 +01002670 co = qemu_coroutine_create(bdrv_co_do_rw);
Kevin Wolf68485422011-06-30 10:05:46 +02002671 qemu_coroutine_enter(co, acb);
2672
2673 return &acb->common;
2674}
2675
Paolo Bonzini07f07612011-10-17 12:32:12 +02002676static void coroutine_fn bdrv_aio_flush_co_entry(void *opaque)
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002677{
Paolo Bonzini07f07612011-10-17 12:32:12 +02002678 BlockDriverAIOCBCoroutine *acb = opaque;
2679 BlockDriverState *bs = acb->common.bs;
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002680
Paolo Bonzini07f07612011-10-17 12:32:12 +02002681 acb->req.error = bdrv_co_flush(bs);
2682 acb->bh = qemu_bh_new(bdrv_co_em_bh, acb);
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002683 qemu_bh_schedule(acb->bh);
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002684}
2685
Paolo Bonzini07f07612011-10-17 12:32:12 +02002686BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
Alexander Graf016f5cf2010-05-26 17:51:49 +02002687 BlockDriverCompletionFunc *cb, void *opaque)
2688{
Paolo Bonzini07f07612011-10-17 12:32:12 +02002689 trace_bdrv_aio_flush(bs, opaque);
Alexander Graf016f5cf2010-05-26 17:51:49 +02002690
Paolo Bonzini07f07612011-10-17 12:32:12 +02002691 Coroutine *co;
2692 BlockDriverAIOCBCoroutine *acb;
Alexander Graf016f5cf2010-05-26 17:51:49 +02002693
Paolo Bonzini07f07612011-10-17 12:32:12 +02002694 acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque);
2695 co = qemu_coroutine_create(bdrv_aio_flush_co_entry);
2696 qemu_coroutine_enter(co, acb);
Alexander Graf016f5cf2010-05-26 17:51:49 +02002697
Alexander Graf016f5cf2010-05-26 17:51:49 +02002698 return &acb->common;
2699}
2700
Paolo Bonzini4265d622011-10-17 12:32:14 +02002701static void coroutine_fn bdrv_aio_discard_co_entry(void *opaque)
2702{
2703 BlockDriverAIOCBCoroutine *acb = opaque;
2704 BlockDriverState *bs = acb->common.bs;
2705
2706 acb->req.error = bdrv_co_discard(bs, acb->req.sector, acb->req.nb_sectors);
2707 acb->bh = qemu_bh_new(bdrv_co_em_bh, acb);
2708 qemu_bh_schedule(acb->bh);
2709}
2710
2711BlockDriverAIOCB *bdrv_aio_discard(BlockDriverState *bs,
2712 int64_t sector_num, int nb_sectors,
2713 BlockDriverCompletionFunc *cb, void *opaque)
2714{
2715 Coroutine *co;
2716 BlockDriverAIOCBCoroutine *acb;
2717
2718 trace_bdrv_aio_discard(bs, sector_num, nb_sectors, opaque);
2719
2720 acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque);
2721 acb->req.sector = sector_num;
2722 acb->req.nb_sectors = nb_sectors;
2723 co = qemu_coroutine_create(bdrv_aio_discard_co_entry);
2724 qemu_coroutine_enter(co, acb);
2725
2726 return &acb->common;
2727}
2728
bellardea2384d2004-08-01 21:59:26 +00002729void bdrv_init(void)
2730{
Anthony Liguori5efa9d52009-05-09 17:03:42 -05002731 module_call_init(MODULE_INIT_BLOCK);
bellardea2384d2004-08-01 21:59:26 +00002732}
pbrookce1a14d2006-08-07 02:38:06 +00002733
Markus Armbrustereb852012009-10-27 18:41:44 +01002734void bdrv_init_with_whitelist(void)
2735{
2736 use_bdrv_whitelist = 1;
2737 bdrv_init();
2738}
2739
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002740void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
2741 BlockDriverCompletionFunc *cb, void *opaque)
aliguori6bbff9a2009-03-20 18:25:59 +00002742{
pbrookce1a14d2006-08-07 02:38:06 +00002743 BlockDriverAIOCB *acb;
2744
aliguori6bbff9a2009-03-20 18:25:59 +00002745 if (pool->free_aiocb) {
2746 acb = pool->free_aiocb;
2747 pool->free_aiocb = acb->next;
pbrookce1a14d2006-08-07 02:38:06 +00002748 } else {
Anthony Liguori7267c092011-08-20 22:09:37 -05002749 acb = g_malloc0(pool->aiocb_size);
aliguori6bbff9a2009-03-20 18:25:59 +00002750 acb->pool = pool;
pbrookce1a14d2006-08-07 02:38:06 +00002751 }
2752 acb->bs = bs;
2753 acb->cb = cb;
2754 acb->opaque = opaque;
2755 return acb;
2756}
2757
2758void qemu_aio_release(void *p)
2759{
aliguori6bbff9a2009-03-20 18:25:59 +00002760 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
2761 AIOPool *pool = acb->pool;
2762 acb->next = pool->free_aiocb;
2763 pool->free_aiocb = acb;
pbrookce1a14d2006-08-07 02:38:06 +00002764}
bellard19cb3732006-08-19 11:45:59 +00002765
2766/**************************************************************/
Kevin Wolff9f05dc2011-07-15 13:50:26 +02002767/* Coroutine block device emulation */
2768
2769typedef struct CoroutineIOCompletion {
2770 Coroutine *coroutine;
2771 int ret;
2772} CoroutineIOCompletion;
2773
2774static void bdrv_co_io_em_complete(void *opaque, int ret)
2775{
2776 CoroutineIOCompletion *co = opaque;
2777
2778 co->ret = ret;
2779 qemu_coroutine_enter(co->coroutine, NULL);
2780}
2781
2782static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num,
2783 int nb_sectors, QEMUIOVector *iov,
2784 bool is_write)
2785{
2786 CoroutineIOCompletion co = {
2787 .coroutine = qemu_coroutine_self(),
2788 };
2789 BlockDriverAIOCB *acb;
2790
2791 if (is_write) {
Stefan Hajnoczia652d162011-10-05 17:17:02 +01002792 acb = bs->drv->bdrv_aio_writev(bs, sector_num, iov, nb_sectors,
2793 bdrv_co_io_em_complete, &co);
Kevin Wolff9f05dc2011-07-15 13:50:26 +02002794 } else {
Stefan Hajnoczia652d162011-10-05 17:17:02 +01002795 acb = bs->drv->bdrv_aio_readv(bs, sector_num, iov, nb_sectors,
2796 bdrv_co_io_em_complete, &co);
Kevin Wolff9f05dc2011-07-15 13:50:26 +02002797 }
2798
Stefan Hajnoczi59370aa2011-09-30 17:34:58 +01002799 trace_bdrv_co_io_em(bs, sector_num, nb_sectors, is_write, acb);
Kevin Wolff9f05dc2011-07-15 13:50:26 +02002800 if (!acb) {
2801 return -EIO;
2802 }
2803 qemu_coroutine_yield();
2804
2805 return co.ret;
2806}
2807
2808static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs,
2809 int64_t sector_num, int nb_sectors,
2810 QEMUIOVector *iov)
2811{
2812 return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false);
2813}
2814
2815static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs,
2816 int64_t sector_num, int nb_sectors,
2817 QEMUIOVector *iov)
2818{
2819 return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true);
2820}
2821
Paolo Bonzini07f07612011-10-17 12:32:12 +02002822static void coroutine_fn bdrv_flush_co_entry(void *opaque)
Kevin Wolfe7a8a782011-07-15 16:05:00 +02002823{
Paolo Bonzini07f07612011-10-17 12:32:12 +02002824 RwCo *rwco = opaque;
Kevin Wolfe7a8a782011-07-15 16:05:00 +02002825
Paolo Bonzini07f07612011-10-17 12:32:12 +02002826 rwco->ret = bdrv_co_flush(rwco->bs);
2827}
2828
2829int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
2830{
Kevin Wolfeb489bb2011-11-10 18:10:11 +01002831 int ret;
2832
Kevin Wolfca716362011-11-10 18:13:59 +01002833 if (!bs->drv) {
Paolo Bonzini07f07612011-10-17 12:32:12 +02002834 return 0;
Kevin Wolfeb489bb2011-11-10 18:10:11 +01002835 }
2836
Kevin Wolfca716362011-11-10 18:13:59 +01002837 /* Write back cached data to the OS even with cache=unsafe */
Kevin Wolfeb489bb2011-11-10 18:10:11 +01002838 if (bs->drv->bdrv_co_flush_to_os) {
2839 ret = bs->drv->bdrv_co_flush_to_os(bs);
2840 if (ret < 0) {
2841 return ret;
2842 }
2843 }
2844
Kevin Wolfca716362011-11-10 18:13:59 +01002845 /* But don't actually force it to the disk with cache=unsafe */
2846 if (bs->open_flags & BDRV_O_NO_FLUSH) {
2847 return 0;
2848 }
2849
Kevin Wolfeb489bb2011-11-10 18:10:11 +01002850 if (bs->drv->bdrv_co_flush_to_disk) {
Kevin Wolfc68b89a2011-11-10 17:25:44 +01002851 return bs->drv->bdrv_co_flush_to_disk(bs);
Paolo Bonzini07f07612011-10-17 12:32:12 +02002852 } else if (bs->drv->bdrv_aio_flush) {
2853 BlockDriverAIOCB *acb;
2854 CoroutineIOCompletion co = {
2855 .coroutine = qemu_coroutine_self(),
2856 };
2857
2858 acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co);
2859 if (acb == NULL) {
2860 return -EIO;
2861 } else {
2862 qemu_coroutine_yield();
2863 return co.ret;
2864 }
Paolo Bonzini07f07612011-10-17 12:32:12 +02002865 } else {
2866 /*
2867 * Some block drivers always operate in either writethrough or unsafe
2868 * mode and don't support bdrv_flush therefore. Usually qemu doesn't
2869 * know how the server works (because the behaviour is hardcoded or
2870 * depends on server-side configuration), so we can't ensure that
2871 * everything is safe on disk. Returning an error doesn't work because
2872 * that would break guests even if the server operates in writethrough
2873 * mode.
2874 *
2875 * Let's hope the user knows what he's doing.
2876 */
2877 return 0;
Kevin Wolfe7a8a782011-07-15 16:05:00 +02002878 }
Paolo Bonzini07f07612011-10-17 12:32:12 +02002879}
2880
Anthony Liguori0f154232011-11-14 15:09:45 -06002881void bdrv_invalidate_cache(BlockDriverState *bs)
2882{
2883 if (bs->drv && bs->drv->bdrv_invalidate_cache) {
2884 bs->drv->bdrv_invalidate_cache(bs);
2885 }
2886}
2887
2888void bdrv_invalidate_cache_all(void)
2889{
2890 BlockDriverState *bs;
2891
2892 QTAILQ_FOREACH(bs, &bdrv_states, list) {
2893 bdrv_invalidate_cache(bs);
2894 }
2895}
2896
Paolo Bonzini07f07612011-10-17 12:32:12 +02002897int bdrv_flush(BlockDriverState *bs)
2898{
2899 Coroutine *co;
2900 RwCo rwco = {
2901 .bs = bs,
2902 .ret = NOT_DONE,
2903 };
2904
2905 if (qemu_in_coroutine()) {
2906 /* Fast-path if already in coroutine context */
2907 bdrv_flush_co_entry(&rwco);
2908 } else {
2909 co = qemu_coroutine_create(bdrv_flush_co_entry);
2910 qemu_coroutine_enter(co, &rwco);
2911 while (rwco.ret == NOT_DONE) {
2912 qemu_aio_wait();
2913 }
2914 }
2915
2916 return rwco.ret;
Kevin Wolfe7a8a782011-07-15 16:05:00 +02002917}
2918
Paolo Bonzini4265d622011-10-17 12:32:14 +02002919static void coroutine_fn bdrv_discard_co_entry(void *opaque)
2920{
2921 RwCo *rwco = opaque;
2922
2923 rwco->ret = bdrv_co_discard(rwco->bs, rwco->sector_num, rwco->nb_sectors);
2924}
2925
2926int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num,
2927 int nb_sectors)
2928{
2929 if (!bs->drv) {
2930 return -ENOMEDIUM;
2931 } else if (bdrv_check_request(bs, sector_num, nb_sectors)) {
2932 return -EIO;
2933 } else if (bs->read_only) {
2934 return -EROFS;
2935 } else if (bs->drv->bdrv_co_discard) {
2936 return bs->drv->bdrv_co_discard(bs, sector_num, nb_sectors);
2937 } else if (bs->drv->bdrv_aio_discard) {
2938 BlockDriverAIOCB *acb;
2939 CoroutineIOCompletion co = {
2940 .coroutine = qemu_coroutine_self(),
2941 };
2942
2943 acb = bs->drv->bdrv_aio_discard(bs, sector_num, nb_sectors,
2944 bdrv_co_io_em_complete, &co);
2945 if (acb == NULL) {
2946 return -EIO;
2947 } else {
2948 qemu_coroutine_yield();
2949 return co.ret;
2950 }
Paolo Bonzini4265d622011-10-17 12:32:14 +02002951 } else {
2952 return 0;
2953 }
2954}
2955
2956int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors)
2957{
2958 Coroutine *co;
2959 RwCo rwco = {
2960 .bs = bs,
2961 .sector_num = sector_num,
2962 .nb_sectors = nb_sectors,
2963 .ret = NOT_DONE,
2964 };
2965
2966 if (qemu_in_coroutine()) {
2967 /* Fast-path if already in coroutine context */
2968 bdrv_discard_co_entry(&rwco);
2969 } else {
2970 co = qemu_coroutine_create(bdrv_discard_co_entry);
2971 qemu_coroutine_enter(co, &rwco);
2972 while (rwco.ret == NOT_DONE) {
2973 qemu_aio_wait();
2974 }
2975 }
2976
2977 return rwco.ret;
2978}
2979
Kevin Wolff9f05dc2011-07-15 13:50:26 +02002980/**************************************************************/
bellard19cb3732006-08-19 11:45:59 +00002981/* removable device support */
2982
2983/**
2984 * Return TRUE if the media is present
2985 */
2986int bdrv_is_inserted(BlockDriverState *bs)
2987{
2988 BlockDriver *drv = bs->drv;
Markus Armbrustera1aff5b2011-09-06 18:58:41 +02002989
bellard19cb3732006-08-19 11:45:59 +00002990 if (!drv)
2991 return 0;
2992 if (!drv->bdrv_is_inserted)
Markus Armbrustera1aff5b2011-09-06 18:58:41 +02002993 return 1;
2994 return drv->bdrv_is_inserted(bs);
bellard19cb3732006-08-19 11:45:59 +00002995}
2996
2997/**
Markus Armbruster8e49ca42011-08-03 15:08:08 +02002998 * Return whether the media changed since the last call to this
2999 * function, or -ENOTSUP if we don't know. Most drivers don't know.
bellard19cb3732006-08-19 11:45:59 +00003000 */
3001int bdrv_media_changed(BlockDriverState *bs)
3002{
3003 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +00003004
Markus Armbruster8e49ca42011-08-03 15:08:08 +02003005 if (drv && drv->bdrv_media_changed) {
3006 return drv->bdrv_media_changed(bs);
3007 }
3008 return -ENOTSUP;
bellard19cb3732006-08-19 11:45:59 +00003009}
3010
3011/**
3012 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
3013 */
Markus Armbrusterfdec4402011-09-06 18:58:45 +02003014void bdrv_eject(BlockDriverState *bs, int eject_flag)
bellard19cb3732006-08-19 11:45:59 +00003015{
3016 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +00003017
Markus Armbruster822e1cd2011-07-20 18:23:42 +02003018 if (drv && drv->bdrv_eject) {
3019 drv->bdrv_eject(bs, eject_flag);
bellard19cb3732006-08-19 11:45:59 +00003020 }
bellard19cb3732006-08-19 11:45:59 +00003021}
3022
bellard19cb3732006-08-19 11:45:59 +00003023/**
3024 * Lock or unlock the media (if it is locked, the user won't be able
3025 * to eject it manually).
3026 */
Markus Armbruster025e8492011-09-06 18:58:47 +02003027void bdrv_lock_medium(BlockDriverState *bs, bool locked)
bellard19cb3732006-08-19 11:45:59 +00003028{
3029 BlockDriver *drv = bs->drv;
3030
Markus Armbruster025e8492011-09-06 18:58:47 +02003031 trace_bdrv_lock_medium(bs, locked);
Stefan Hajnoczib8c6d092011-03-29 20:04:40 +01003032
Markus Armbruster025e8492011-09-06 18:58:47 +02003033 if (drv && drv->bdrv_lock_medium) {
3034 drv->bdrv_lock_medium(bs, locked);
bellard19cb3732006-08-19 11:45:59 +00003035 }
3036}
ths985a03b2007-12-24 16:10:43 +00003037
3038/* needed for generic scsi interface */
3039
3040int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
3041{
3042 BlockDriver *drv = bs->drv;
3043
3044 if (drv && drv->bdrv_ioctl)
3045 return drv->bdrv_ioctl(bs, req, buf);
3046 return -ENOTSUP;
3047}
aliguori7d780662009-03-12 19:57:08 +00003048
aliguori221f7152009-03-28 17:28:41 +00003049BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
3050 unsigned long int req, void *buf,
3051 BlockDriverCompletionFunc *cb, void *opaque)
aliguori7d780662009-03-12 19:57:08 +00003052{
aliguori221f7152009-03-28 17:28:41 +00003053 BlockDriver *drv = bs->drv;
aliguori7d780662009-03-12 19:57:08 +00003054
aliguori221f7152009-03-28 17:28:41 +00003055 if (drv && drv->bdrv_aio_ioctl)
3056 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
3057 return NULL;
aliguori7d780662009-03-12 19:57:08 +00003058}
aliguorie268ca52009-04-22 20:20:00 +00003059
Markus Armbruster7b6f9302011-09-06 18:58:56 +02003060void bdrv_set_buffer_alignment(BlockDriverState *bs, int align)
3061{
3062 bs->buffer_alignment = align;
3063}
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02003064
aliguorie268ca52009-04-22 20:20:00 +00003065void *qemu_blockalign(BlockDriverState *bs, size_t size)
3066{
3067 return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
3068}
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02003069
3070void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable)
3071{
3072 int64_t bitmap_size;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01003073
Liran Schouraaa0eb72010-01-26 10:31:48 +02003074 bs->dirty_count = 0;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01003075 if (enable) {
Jan Kiszkac6d22832009-11-30 18:21:20 +01003076 if (!bs->dirty_bitmap) {
3077 bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
3078 BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
3079 bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01003080
Anthony Liguori7267c092011-08-20 22:09:37 -05003081 bs->dirty_bitmap = g_malloc0(bitmap_size);
Jan Kiszkaa55eb922009-11-30 18:21:19 +01003082 }
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02003083 } else {
Jan Kiszkac6d22832009-11-30 18:21:20 +01003084 if (bs->dirty_bitmap) {
Anthony Liguori7267c092011-08-20 22:09:37 -05003085 g_free(bs->dirty_bitmap);
Jan Kiszkac6d22832009-11-30 18:21:20 +01003086 bs->dirty_bitmap = NULL;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01003087 }
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02003088 }
3089}
3090
3091int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
3092{
Jan Kiszka6ea44302009-11-30 18:21:19 +01003093 int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01003094
Jan Kiszkac6d22832009-11-30 18:21:20 +01003095 if (bs->dirty_bitmap &&
3096 (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) {
Marcelo Tosatti6d59fec2010-11-08 17:02:54 -02003097 return !!(bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] &
3098 (1UL << (chunk % (sizeof(unsigned long) * 8))));
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02003099 } else {
3100 return 0;
3101 }
3102}
3103
Jan Kiszkaa55eb922009-11-30 18:21:19 +01003104void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
3105 int nr_sectors)
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02003106{
3107 set_dirty_bitmap(bs, cur_sector, nr_sectors, 0);
3108}
Liran Schouraaa0eb72010-01-26 10:31:48 +02003109
3110int64_t bdrv_get_dirty_count(BlockDriverState *bs)
3111{
3112 return bs->dirty_count;
3113}
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003114
Marcelo Tosattidb593f22011-01-26 12:12:34 -02003115void bdrv_set_in_use(BlockDriverState *bs, int in_use)
3116{
3117 assert(bs->in_use != in_use);
3118 bs->in_use = in_use;
3119}
3120
3121int bdrv_in_use(BlockDriverState *bs)
3122{
3123 return bs->in_use;
3124}
3125
Luiz Capitulino28a72822011-09-26 17:43:50 -03003126void bdrv_iostatus_enable(BlockDriverState *bs)
3127{
Luiz Capitulinod6bf2792011-10-14 17:11:23 -03003128 bs->iostatus_enabled = true;
Luiz Capitulino58e21ef2011-10-14 17:22:24 -03003129 bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
Luiz Capitulino28a72822011-09-26 17:43:50 -03003130}
3131
3132/* The I/O status is only enabled if the drive explicitly
3133 * enables it _and_ the VM is configured to stop on errors */
3134bool bdrv_iostatus_is_enabled(const BlockDriverState *bs)
3135{
Luiz Capitulinod6bf2792011-10-14 17:11:23 -03003136 return (bs->iostatus_enabled &&
Luiz Capitulino28a72822011-09-26 17:43:50 -03003137 (bs->on_write_error == BLOCK_ERR_STOP_ENOSPC ||
3138 bs->on_write_error == BLOCK_ERR_STOP_ANY ||
3139 bs->on_read_error == BLOCK_ERR_STOP_ANY));
3140}
3141
3142void bdrv_iostatus_disable(BlockDriverState *bs)
3143{
Luiz Capitulinod6bf2792011-10-14 17:11:23 -03003144 bs->iostatus_enabled = false;
Luiz Capitulino28a72822011-09-26 17:43:50 -03003145}
3146
3147void bdrv_iostatus_reset(BlockDriverState *bs)
3148{
3149 if (bdrv_iostatus_is_enabled(bs)) {
Luiz Capitulino58e21ef2011-10-14 17:22:24 -03003150 bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
Luiz Capitulino28a72822011-09-26 17:43:50 -03003151 }
3152}
3153
3154/* XXX: Today this is set by device models because it makes the implementation
3155 quite simple. However, the block layer knows about the error, so it's
3156 possible to implement this without device models being involved */
3157void bdrv_iostatus_set_err(BlockDriverState *bs, int error)
3158{
Luiz Capitulino58e21ef2011-10-14 17:22:24 -03003159 if (bdrv_iostatus_is_enabled(bs) &&
3160 bs->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
Luiz Capitulino28a72822011-09-26 17:43:50 -03003161 assert(error >= 0);
Luiz Capitulino58e21ef2011-10-14 17:22:24 -03003162 bs->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
3163 BLOCK_DEVICE_IO_STATUS_FAILED;
Luiz Capitulino28a72822011-09-26 17:43:50 -03003164 }
3165}
3166
Christoph Hellwiga597e792011-08-25 08:26:01 +02003167void
3168bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, int64_t bytes,
3169 enum BlockAcctType type)
3170{
3171 assert(type < BDRV_MAX_IOTYPE);
3172
3173 cookie->bytes = bytes;
Christoph Hellwigc488c7f2011-08-25 08:26:10 +02003174 cookie->start_time_ns = get_clock();
Christoph Hellwiga597e792011-08-25 08:26:01 +02003175 cookie->type = type;
3176}
3177
3178void
3179bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie)
3180{
3181 assert(cookie->type < BDRV_MAX_IOTYPE);
3182
3183 bs->nr_bytes[cookie->type] += cookie->bytes;
3184 bs->nr_ops[cookie->type]++;
Christoph Hellwigc488c7f2011-08-25 08:26:10 +02003185 bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns;
Christoph Hellwiga597e792011-08-25 08:26:01 +02003186}
3187
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003188int bdrv_img_create(const char *filename, const char *fmt,
3189 const char *base_filename, const char *base_fmt,
3190 char *options, uint64_t img_size, int flags)
3191{
3192 QEMUOptionParameter *param = NULL, *create_options = NULL;
Kevin Wolfd2208942011-06-01 14:03:31 +02003193 QEMUOptionParameter *backing_fmt, *backing_file, *size;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003194 BlockDriverState *bs = NULL;
3195 BlockDriver *drv, *proto_drv;
Stefan Hajnoczi96df67d2011-01-24 09:32:20 +00003196 BlockDriver *backing_drv = NULL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003197 int ret = 0;
3198
3199 /* Find driver and parse its options */
3200 drv = bdrv_find_format(fmt);
3201 if (!drv) {
3202 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4f70f242010-12-16 13:52:18 +01003203 ret = -EINVAL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003204 goto out;
3205 }
3206
3207 proto_drv = bdrv_find_protocol(filename);
3208 if (!proto_drv) {
3209 error_report("Unknown protocol '%s'", filename);
Jes Sorensen4f70f242010-12-16 13:52:18 +01003210 ret = -EINVAL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003211 goto out;
3212 }
3213
3214 create_options = append_option_parameters(create_options,
3215 drv->create_options);
3216 create_options = append_option_parameters(create_options,
3217 proto_drv->create_options);
3218
3219 /* Create parameter list with default values */
3220 param = parse_option_parameters("", create_options, param);
3221
3222 set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size);
3223
3224 /* Parse -o options */
3225 if (options) {
3226 param = parse_option_parameters(options, create_options, param);
3227 if (param == NULL) {
3228 error_report("Invalid options for file format '%s'.", fmt);
Jes Sorensen4f70f242010-12-16 13:52:18 +01003229 ret = -EINVAL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003230 goto out;
3231 }
3232 }
3233
3234 if (base_filename) {
3235 if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE,
3236 base_filename)) {
3237 error_report("Backing file not supported for file format '%s'",
3238 fmt);
Jes Sorensen4f70f242010-12-16 13:52:18 +01003239 ret = -EINVAL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003240 goto out;
3241 }
3242 }
3243
3244 if (base_fmt) {
3245 if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) {
3246 error_report("Backing file format not supported for file "
3247 "format '%s'", fmt);
Jes Sorensen4f70f242010-12-16 13:52:18 +01003248 ret = -EINVAL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003249 goto out;
3250 }
3251 }
3252
Jes Sorensen792da932010-12-16 13:52:17 +01003253 backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
3254 if (backing_file && backing_file->value.s) {
3255 if (!strcmp(filename, backing_file->value.s)) {
3256 error_report("Error: Trying to create an image with the "
3257 "same filename as the backing file");
Jes Sorensen4f70f242010-12-16 13:52:18 +01003258 ret = -EINVAL;
Jes Sorensen792da932010-12-16 13:52:17 +01003259 goto out;
3260 }
3261 }
3262
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003263 backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT);
3264 if (backing_fmt && backing_fmt->value.s) {
Stefan Hajnoczi96df67d2011-01-24 09:32:20 +00003265 backing_drv = bdrv_find_format(backing_fmt->value.s);
3266 if (!backing_drv) {
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003267 error_report("Unknown backing file format '%s'",
3268 backing_fmt->value.s);
Jes Sorensen4f70f242010-12-16 13:52:18 +01003269 ret = -EINVAL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003270 goto out;
3271 }
3272 }
3273
3274 // The size for the image must always be specified, with one exception:
3275 // If we are using a backing file, we can obtain the size from there
Kevin Wolfd2208942011-06-01 14:03:31 +02003276 size = get_option_parameter(param, BLOCK_OPT_SIZE);
3277 if (size && size->value.n == -1) {
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003278 if (backing_file && backing_file->value.s) {
3279 uint64_t size;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003280 char buf[32];
3281
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003282 bs = bdrv_new("");
3283
Stefan Hajnoczi96df67d2011-01-24 09:32:20 +00003284 ret = bdrv_open(bs, backing_file->value.s, flags, backing_drv);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003285 if (ret < 0) {
Stefan Hajnoczi96df67d2011-01-24 09:32:20 +00003286 error_report("Could not open '%s'", backing_file->value.s);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003287 goto out;
3288 }
3289 bdrv_get_geometry(bs, &size);
3290 size *= 512;
3291
3292 snprintf(buf, sizeof(buf), "%" PRId64, size);
3293 set_option_parameter(param, BLOCK_OPT_SIZE, buf);
3294 } else {
3295 error_report("Image creation needs a size parameter");
Jes Sorensen4f70f242010-12-16 13:52:18 +01003296 ret = -EINVAL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003297 goto out;
3298 }
3299 }
3300
3301 printf("Formatting '%s', fmt=%s ", filename, fmt);
3302 print_option_parameters(param);
3303 puts("");
3304
3305 ret = bdrv_create(drv, filename, param);
3306
3307 if (ret < 0) {
3308 if (ret == -ENOTSUP) {
3309 error_report("Formatting or formatting option not supported for "
3310 "file format '%s'", fmt);
3311 } else if (ret == -EFBIG) {
3312 error_report("The image size is too large for file format '%s'",
3313 fmt);
3314 } else {
3315 error_report("%s: error while creating %s: %s", filename, fmt,
3316 strerror(-ret));
3317 }
3318 }
3319
3320out:
3321 free_option_parameters(create_options);
3322 free_option_parameters(param);
3323
3324 if (bs) {
3325 bdrv_delete(bs);
3326 }
Jes Sorensen4f70f242010-12-16 13:52:18 +01003327
3328 return ret;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003329}