blob: 40621b1acba95cfa3d51ac173c02aeda2ad55b03 [file] [log] [blame]
bellardfc01f7e2003-06-30 10:03:06 +00001/*
2 * QEMU System Emulator block driver
ths5fafdf22007-09-16 21:08:06 +00003 *
bellardfc01f7e2003-06-30 10:03:06 +00004 * Copyright (c) 2003 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellardfc01f7e2003-06-30 10:03:06 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
blueswir13990d092008-12-05 17:53:21 +000024#include "config-host.h"
pbrookfaf07962007-11-11 02:51:17 +000025#include "qemu-common.h"
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +010026#include "trace.h"
aliguori376253e2009-03-05 23:01:23 +000027#include "monitor.h"
bellardea2384d2004-08-01 21:59:26 +000028#include "block_int.h"
Anthony Liguori5efa9d52009-05-09 17:03:42 -050029#include "module.h"
Luiz Capitulinod15e5462009-12-10 17:16:06 -020030#include "qemu-objects.h"
Kevin Wolf68485422011-06-30 10:05:46 +020031#include "qemu-coroutine.h"
bellardfc01f7e2003-06-30 10:03:06 +000032
Juan Quintela71e72a12009-07-27 16:12:56 +020033#ifdef CONFIG_BSD
bellard7674e7b2005-04-26 21:59:26 +000034#include <sys/types.h>
35#include <sys/stat.h>
36#include <sys/ioctl.h>
Blue Swirl72cf2d42009-09-12 07:36:22 +000037#include <sys/queue.h>
blueswir1c5e97232009-03-07 20:06:23 +000038#ifndef __DragonFly__
bellard7674e7b2005-04-26 21:59:26 +000039#include <sys/disk.h>
40#endif
blueswir1c5e97232009-03-07 20:06:23 +000041#endif
bellard7674e7b2005-04-26 21:59:26 +000042
aliguori49dc7682009-03-08 16:26:59 +000043#ifdef _WIN32
44#include <windows.h>
45#endif
46
Markus Armbruster7d4b4ba2011-09-06 18:58:59 +020047static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load);
aliguorif141eaf2009-04-07 18:43:24 +000048static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
49 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
aliguoric87c0672009-04-07 18:43:20 +000050 BlockDriverCompletionFunc *cb, void *opaque);
aliguorif141eaf2009-04-07 18:43:24 +000051static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
52 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
pbrookce1a14d2006-08-07 02:38:06 +000053 BlockDriverCompletionFunc *cb, void *opaque);
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +020054static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
55 BlockDriverCompletionFunc *cb, void *opaque);
Alexander Graf016f5cf2010-05-26 17:51:49 +020056static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,
57 BlockDriverCompletionFunc *cb, void *opaque);
ths5fafdf22007-09-16 21:08:06 +000058static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +000059 uint8_t *buf, int nb_sectors);
60static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
61 const uint8_t *buf, int nb_sectors);
Kevin Wolf68485422011-06-30 10:05:46 +020062static BlockDriverAIOCB *bdrv_co_aio_readv_em(BlockDriverState *bs,
63 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
64 BlockDriverCompletionFunc *cb, void *opaque);
65static BlockDriverAIOCB *bdrv_co_aio_writev_em(BlockDriverState *bs,
66 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
67 BlockDriverCompletionFunc *cb, void *opaque);
Kevin Wolff9f05dc2011-07-15 13:50:26 +020068static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs,
69 int64_t sector_num, int nb_sectors,
70 QEMUIOVector *iov);
71static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs,
72 int64_t sector_num, int nb_sectors,
73 QEMUIOVector *iov);
Kevin Wolfe7a8a782011-07-15 16:05:00 +020074static int coroutine_fn bdrv_co_flush_em(BlockDriverState *bs);
bellardec530c82006-04-25 22:36:06 +000075
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +010076static QTAILQ_HEAD(, BlockDriverState) bdrv_states =
77 QTAILQ_HEAD_INITIALIZER(bdrv_states);
blueswir17ee930d2008-09-17 19:04:14 +000078
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +010079static QLIST_HEAD(, BlockDriver) bdrv_drivers =
80 QLIST_HEAD_INITIALIZER(bdrv_drivers);
bellardea2384d2004-08-01 21:59:26 +000081
Markus Armbrusterf9092b12010-06-25 10:33:39 +020082/* The device to use for VM snapshots */
83static BlockDriverState *bs_snapshots;
84
Markus Armbrustereb852012009-10-27 18:41:44 +010085/* If non-zero, use only whitelisted block drivers */
86static int use_bdrv_whitelist;
87
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +000088#ifdef _WIN32
89static int is_windows_drive_prefix(const char *filename)
90{
91 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
92 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
93 filename[1] == ':');
94}
95
96int is_windows_drive(const char *filename)
97{
98 if (is_windows_drive_prefix(filename) &&
99 filename[2] == '\0')
100 return 1;
101 if (strstart(filename, "\\\\.\\", NULL) ||
102 strstart(filename, "//./", NULL))
103 return 1;
104 return 0;
105}
106#endif
107
108/* check if the path starts with "<protocol>:" */
109static int path_has_protocol(const char *path)
110{
111#ifdef _WIN32
112 if (is_windows_drive(path) ||
113 is_windows_drive_prefix(path)) {
114 return 0;
115 }
116#endif
117
118 return strchr(path, ':') != NULL;
119}
120
bellard83f64092006-08-01 16:21:11 +0000121int path_is_absolute(const char *path)
122{
123 const char *p;
bellard21664422007-01-07 18:22:37 +0000124#ifdef _WIN32
125 /* specific case for names like: "\\.\d:" */
126 if (*path == '/' || *path == '\\')
127 return 1;
128#endif
bellard83f64092006-08-01 16:21:11 +0000129 p = strchr(path, ':');
130 if (p)
131 p++;
132 else
133 p = path;
bellard3b9f94e2007-01-07 17:27:07 +0000134#ifdef _WIN32
135 return (*p == '/' || *p == '\\');
136#else
137 return (*p == '/');
138#endif
bellard83f64092006-08-01 16:21:11 +0000139}
140
141/* if filename is absolute, just copy it to dest. Otherwise, build a
142 path to it by considering it is relative to base_path. URL are
143 supported. */
144void path_combine(char *dest, int dest_size,
145 const char *base_path,
146 const char *filename)
147{
148 const char *p, *p1;
149 int len;
150
151 if (dest_size <= 0)
152 return;
153 if (path_is_absolute(filename)) {
154 pstrcpy(dest, dest_size, filename);
155 } else {
156 p = strchr(base_path, ':');
157 if (p)
158 p++;
159 else
160 p = base_path;
bellard3b9f94e2007-01-07 17:27:07 +0000161 p1 = strrchr(base_path, '/');
162#ifdef _WIN32
163 {
164 const char *p2;
165 p2 = strrchr(base_path, '\\');
166 if (!p1 || p2 > p1)
167 p1 = p2;
168 }
169#endif
bellard83f64092006-08-01 16:21:11 +0000170 if (p1)
171 p1++;
172 else
173 p1 = base_path;
174 if (p1 > p)
175 p = p1;
176 len = p - base_path;
177 if (len > dest_size - 1)
178 len = dest_size - 1;
179 memcpy(dest, base_path, len);
180 dest[len] = '\0';
181 pstrcat(dest, dest_size, filename);
182 }
183}
184
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500185void bdrv_register(BlockDriver *bdrv)
bellardea2384d2004-08-01 21:59:26 +0000186{
Kevin Wolf68485422011-06-30 10:05:46 +0200187 if (bdrv->bdrv_co_readv) {
188 /* Emulate AIO by coroutines, and sync by AIO */
189 bdrv->bdrv_aio_readv = bdrv_co_aio_readv_em;
190 bdrv->bdrv_aio_writev = bdrv_co_aio_writev_em;
191 bdrv->bdrv_read = bdrv_read_em;
192 bdrv->bdrv_write = bdrv_write_em;
Kevin Wolff9f05dc2011-07-15 13:50:26 +0200193 } else {
194 bdrv->bdrv_co_readv = bdrv_co_readv_em;
195 bdrv->bdrv_co_writev = bdrv_co_writev_em;
196
197 if (!bdrv->bdrv_aio_readv) {
198 /* add AIO emulation layer */
199 bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
200 bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
201 } else if (!bdrv->bdrv_read) {
202 /* add synchronous IO emulation layer */
203 bdrv->bdrv_read = bdrv_read_em;
204 bdrv->bdrv_write = bdrv_write_em;
205 }
bellard83f64092006-08-01 16:21:11 +0000206 }
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +0200207
208 if (!bdrv->bdrv_aio_flush)
209 bdrv->bdrv_aio_flush = bdrv_aio_flush_em;
210
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100211 QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
bellardea2384d2004-08-01 21:59:26 +0000212}
bellardb3380822004-03-14 21:38:54 +0000213
214/* create a new block device (by default it is empty) */
215BlockDriverState *bdrv_new(const char *device_name)
bellardfc01f7e2003-06-30 10:03:06 +0000216{
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +0100217 BlockDriverState *bs;
bellardb3380822004-03-14 21:38:54 +0000218
Anthony Liguori7267c092011-08-20 22:09:37 -0500219 bs = g_malloc0(sizeof(BlockDriverState));
bellardb3380822004-03-14 21:38:54 +0000220 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
bellardea2384d2004-08-01 21:59:26 +0000221 if (device_name[0] != '\0') {
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +0100222 QTAILQ_INSERT_TAIL(&bdrv_states, bs, list);
bellardea2384d2004-08-01 21:59:26 +0000223 }
Luiz Capitulino28a72822011-09-26 17:43:50 -0300224 bdrv_iostatus_disable(bs);
bellardb3380822004-03-14 21:38:54 +0000225 return bs;
226}
227
bellardea2384d2004-08-01 21:59:26 +0000228BlockDriver *bdrv_find_format(const char *format_name)
229{
230 BlockDriver *drv1;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100231 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
232 if (!strcmp(drv1->format_name, format_name)) {
bellardea2384d2004-08-01 21:59:26 +0000233 return drv1;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100234 }
bellardea2384d2004-08-01 21:59:26 +0000235 }
236 return NULL;
237}
238
Markus Armbrustereb852012009-10-27 18:41:44 +0100239static int bdrv_is_whitelisted(BlockDriver *drv)
240{
241 static const char *whitelist[] = {
242 CONFIG_BDRV_WHITELIST
243 };
244 const char **p;
245
246 if (!whitelist[0])
247 return 1; /* no whitelist, anything goes */
248
249 for (p = whitelist; *p; p++) {
250 if (!strcmp(drv->format_name, *p)) {
251 return 1;
252 }
253 }
254 return 0;
255}
256
257BlockDriver *bdrv_find_whitelisted_format(const char *format_name)
258{
259 BlockDriver *drv = bdrv_find_format(format_name);
260 return drv && bdrv_is_whitelisted(drv) ? drv : NULL;
261}
262
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200263int bdrv_create(BlockDriver *drv, const char* filename,
264 QEMUOptionParameter *options)
bellardea2384d2004-08-01 21:59:26 +0000265{
266 if (!drv->bdrv_create)
267 return -ENOTSUP;
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200268
269 return drv->bdrv_create(filename, options);
bellardea2384d2004-08-01 21:59:26 +0000270}
271
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200272int bdrv_create_file(const char* filename, QEMUOptionParameter *options)
273{
274 BlockDriver *drv;
275
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900276 drv = bdrv_find_protocol(filename);
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200277 if (drv == NULL) {
Stefan Hajnoczi16905d72010-11-30 15:14:14 +0000278 return -ENOENT;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200279 }
280
281 return bdrv_create(drv, filename, options);
282}
283
bellardd5249392004-08-03 21:14:23 +0000284#ifdef _WIN32
bellard95389c82005-12-18 18:28:15 +0000285void get_tmp_filename(char *filename, int size)
bellardd5249392004-08-03 21:14:23 +0000286{
bellard3b9f94e2007-01-07 17:27:07 +0000287 char temp_dir[MAX_PATH];
ths3b46e622007-09-17 08:09:54 +0000288
bellard3b9f94e2007-01-07 17:27:07 +0000289 GetTempPath(MAX_PATH, temp_dir);
290 GetTempFileName(temp_dir, "qem", 0, filename);
bellardd5249392004-08-03 21:14:23 +0000291}
292#else
bellard95389c82005-12-18 18:28:15 +0000293void get_tmp_filename(char *filename, int size)
bellardea2384d2004-08-01 21:59:26 +0000294{
295 int fd;
blueswir17ccfb2e2008-09-14 06:45:34 +0000296 const char *tmpdir;
bellardd5249392004-08-03 21:14:23 +0000297 /* XXX: race condition possible */
aurel320badc1e2008-03-10 00:05:34 +0000298 tmpdir = getenv("TMPDIR");
299 if (!tmpdir)
300 tmpdir = "/tmp";
301 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
bellardea2384d2004-08-01 21:59:26 +0000302 fd = mkstemp(filename);
303 close(fd);
304}
bellardd5249392004-08-03 21:14:23 +0000305#endif
bellardea2384d2004-08-01 21:59:26 +0000306
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200307/*
308 * Detect host devices. By convention, /dev/cdrom[N] is always
309 * recognized as a host CDROM.
310 */
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200311static BlockDriver *find_hdev_driver(const char *filename)
312{
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200313 int score_max = 0, score;
314 BlockDriver *drv = NULL, *d;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200315
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100316 QLIST_FOREACH(d, &bdrv_drivers, list) {
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200317 if (d->bdrv_probe_device) {
318 score = d->bdrv_probe_device(filename);
319 if (score > score_max) {
320 score_max = score;
321 drv = d;
322 }
323 }
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200324 }
325
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200326 return drv;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200327}
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200328
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900329BlockDriver *bdrv_find_protocol(const char *filename)
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200330{
331 BlockDriver *drv1;
332 char protocol[128];
333 int len;
334 const char *p;
335
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200336 /* TODO Drivers without bdrv_file_open must be specified explicitly */
337
Christoph Hellwig39508e72010-06-23 12:25:17 +0200338 /*
339 * XXX(hch): we really should not let host device detection
340 * override an explicit protocol specification, but moving this
341 * later breaks access to device names with colons in them.
342 * Thanks to the brain-dead persistent naming schemes on udev-
343 * based Linux systems those actually are quite common.
344 */
345 drv1 = find_hdev_driver(filename);
346 if (drv1) {
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200347 return drv1;
348 }
Christoph Hellwig39508e72010-06-23 12:25:17 +0200349
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000350 if (!path_has_protocol(filename)) {
Christoph Hellwig39508e72010-06-23 12:25:17 +0200351 return bdrv_find_format("file");
352 }
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000353 p = strchr(filename, ':');
354 assert(p != NULL);
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200355 len = p - filename;
356 if (len > sizeof(protocol) - 1)
357 len = sizeof(protocol) - 1;
358 memcpy(protocol, filename, len);
359 protocol[len] = '\0';
360 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
361 if (drv1->protocol_name &&
362 !strcmp(drv1->protocol_name, protocol)) {
363 return drv1;
364 }
365 }
366 return NULL;
367}
368
Stefan Weilc98ac352010-07-21 21:51:51 +0200369static int find_image_format(const char *filename, BlockDriver **pdrv)
bellardea2384d2004-08-01 21:59:26 +0000370{
bellard83f64092006-08-01 16:21:11 +0000371 int ret, score, score_max;
bellardea2384d2004-08-01 21:59:26 +0000372 BlockDriver *drv1, *drv;
bellard83f64092006-08-01 16:21:11 +0000373 uint8_t buf[2048];
374 BlockDriverState *bs;
ths3b46e622007-09-17 08:09:54 +0000375
Naphtali Spreif5edb012010-01-17 16:48:13 +0200376 ret = bdrv_file_open(&bs, filename, 0);
Stefan Weilc98ac352010-07-21 21:51:51 +0200377 if (ret < 0) {
378 *pdrv = NULL;
379 return ret;
380 }
Nicholas Bellingerf8ea0b02010-05-17 09:45:57 -0700381
Kevin Wolf08a00552010-06-01 18:37:31 +0200382 /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
383 if (bs->sg || !bdrv_is_inserted(bs)) {
Nicholas A. Bellinger1a396852010-05-27 08:56:28 -0700384 bdrv_delete(bs);
Stefan Weilc98ac352010-07-21 21:51:51 +0200385 drv = bdrv_find_format("raw");
386 if (!drv) {
387 ret = -ENOENT;
388 }
389 *pdrv = drv;
390 return ret;
Nicholas A. Bellinger1a396852010-05-27 08:56:28 -0700391 }
Nicholas Bellingerf8ea0b02010-05-17 09:45:57 -0700392
bellard83f64092006-08-01 16:21:11 +0000393 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
394 bdrv_delete(bs);
395 if (ret < 0) {
Stefan Weilc98ac352010-07-21 21:51:51 +0200396 *pdrv = NULL;
397 return ret;
bellard83f64092006-08-01 16:21:11 +0000398 }
399
bellardea2384d2004-08-01 21:59:26 +0000400 score_max = 0;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200401 drv = NULL;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100402 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
bellard83f64092006-08-01 16:21:11 +0000403 if (drv1->bdrv_probe) {
404 score = drv1->bdrv_probe(buf, ret, filename);
405 if (score > score_max) {
406 score_max = score;
407 drv = drv1;
408 }
bellardea2384d2004-08-01 21:59:26 +0000409 }
410 }
Stefan Weilc98ac352010-07-21 21:51:51 +0200411 if (!drv) {
412 ret = -ENOENT;
413 }
414 *pdrv = drv;
415 return ret;
bellardea2384d2004-08-01 21:59:26 +0000416}
417
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100418/**
419 * Set the current 'total_sectors' value
420 */
421static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
422{
423 BlockDriver *drv = bs->drv;
424
Nicholas Bellinger396759a2010-05-17 09:46:04 -0700425 /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
426 if (bs->sg)
427 return 0;
428
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100429 /* query actual device if possible, otherwise just trust the hint */
430 if (drv->bdrv_getlength) {
431 int64_t length = drv->bdrv_getlength(bs);
432 if (length < 0) {
433 return length;
434 }
435 hint = length >> BDRV_SECTOR_BITS;
436 }
437
438 bs->total_sectors = hint;
439 return 0;
440}
441
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100442/**
443 * Set open flags for a given cache mode
444 *
445 * Return 0 on success, -1 if the cache mode was invalid.
446 */
447int bdrv_parse_cache_flags(const char *mode, int *flags)
448{
449 *flags &= ~BDRV_O_CACHE_MASK;
450
451 if (!strcmp(mode, "off") || !strcmp(mode, "none")) {
452 *flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
Stefan Hajnoczi92196b22011-08-04 12:26:52 +0100453 } else if (!strcmp(mode, "directsync")) {
454 *flags |= BDRV_O_NOCACHE;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100455 } else if (!strcmp(mode, "writeback")) {
456 *flags |= BDRV_O_CACHE_WB;
457 } else if (!strcmp(mode, "unsafe")) {
458 *flags |= BDRV_O_CACHE_WB;
459 *flags |= BDRV_O_NO_FLUSH;
460 } else if (!strcmp(mode, "writethrough")) {
461 /* this is the default */
462 } else {
463 return -1;
464 }
465
466 return 0;
467}
468
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200469/*
Kevin Wolf57915332010-04-14 15:24:50 +0200470 * Common part for opening disk images and files
471 */
472static int bdrv_open_common(BlockDriverState *bs, const char *filename,
473 int flags, BlockDriver *drv)
474{
475 int ret, open_flags;
476
477 assert(drv != NULL);
478
Stefan Hajnoczi28dcee12011-09-22 20:14:12 +0100479 trace_bdrv_open_common(bs, filename, flags, drv->format_name);
480
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200481 bs->file = NULL;
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100482 bs->total_sectors = 0;
Kevin Wolf57915332010-04-14 15:24:50 +0200483 bs->encrypted = 0;
484 bs->valid_key = 0;
485 bs->open_flags = flags;
Kevin Wolf57915332010-04-14 15:24:50 +0200486 bs->buffer_alignment = 512;
487
488 pstrcpy(bs->filename, sizeof(bs->filename), filename);
489
490 if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {
491 return -ENOTSUP;
492 }
493
494 bs->drv = drv;
Anthony Liguori7267c092011-08-20 22:09:37 -0500495 bs->opaque = g_malloc0(drv->instance_size);
Kevin Wolf57915332010-04-14 15:24:50 +0200496
Christoph Hellwiga6599792011-05-17 18:04:06 +0200497 if (flags & BDRV_O_CACHE_WB)
Kevin Wolf57915332010-04-14 15:24:50 +0200498 bs->enable_write_cache = 1;
499
500 /*
501 * Clear flags that are internal to the block layer before opening the
502 * image.
503 */
504 open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
505
506 /*
Stefan Weilebabb672011-04-26 10:29:36 +0200507 * Snapshots should be writable.
Kevin Wolf57915332010-04-14 15:24:50 +0200508 */
509 if (bs->is_temporary) {
510 open_flags |= BDRV_O_RDWR;
511 }
512
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200513 /* Open the image, either directly or using a protocol */
514 if (drv->bdrv_file_open) {
515 ret = drv->bdrv_file_open(bs, filename, open_flags);
516 } else {
517 ret = bdrv_file_open(&bs->file, filename, open_flags);
518 if (ret >= 0) {
519 ret = drv->bdrv_open(bs, open_flags);
520 }
521 }
522
Kevin Wolf57915332010-04-14 15:24:50 +0200523 if (ret < 0) {
524 goto free_and_fail;
525 }
526
527 bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR);
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100528
529 ret = refresh_total_sectors(bs, bs->total_sectors);
530 if (ret < 0) {
531 goto free_and_fail;
Kevin Wolf57915332010-04-14 15:24:50 +0200532 }
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100533
Kevin Wolf57915332010-04-14 15:24:50 +0200534#ifndef _WIN32
535 if (bs->is_temporary) {
536 unlink(filename);
537 }
538#endif
539 return 0;
540
541free_and_fail:
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200542 if (bs->file) {
543 bdrv_delete(bs->file);
544 bs->file = NULL;
545 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500546 g_free(bs->opaque);
Kevin Wolf57915332010-04-14 15:24:50 +0200547 bs->opaque = NULL;
548 bs->drv = NULL;
549 return ret;
550}
551
552/*
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200553 * Opens a file using a protocol (file, host_device, nbd, ...)
554 */
bellard83f64092006-08-01 16:21:11 +0000555int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
bellardb3380822004-03-14 21:38:54 +0000556{
bellard83f64092006-08-01 16:21:11 +0000557 BlockDriverState *bs;
Christoph Hellwig6db95602010-04-05 16:53:57 +0200558 BlockDriver *drv;
bellard83f64092006-08-01 16:21:11 +0000559 int ret;
560
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900561 drv = bdrv_find_protocol(filename);
Christoph Hellwig6db95602010-04-05 16:53:57 +0200562 if (!drv) {
563 return -ENOENT;
564 }
565
bellard83f64092006-08-01 16:21:11 +0000566 bs = bdrv_new("");
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200567 ret = bdrv_open_common(bs, filename, flags, drv);
bellard83f64092006-08-01 16:21:11 +0000568 if (ret < 0) {
569 bdrv_delete(bs);
570 return ret;
bellard3b0d4f62005-10-30 18:30:10 +0000571 }
aliguori71d07702009-03-03 17:37:16 +0000572 bs->growable = 1;
bellard83f64092006-08-01 16:21:11 +0000573 *pbs = bs;
574 return 0;
bellardea2384d2004-08-01 21:59:26 +0000575}
bellardfc01f7e2003-06-30 10:03:06 +0000576
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200577/*
578 * Opens a disk image (raw, qcow2, vmdk, ...)
579 */
Kevin Wolfd6e90982010-03-31 14:40:27 +0200580int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
581 BlockDriver *drv)
bellardea2384d2004-08-01 21:59:26 +0000582{
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200583 int ret;
bellard712e7872005-04-28 21:09:32 +0000584
bellard83f64092006-08-01 16:21:11 +0000585 if (flags & BDRV_O_SNAPSHOT) {
bellardea2384d2004-08-01 21:59:26 +0000586 BlockDriverState *bs1;
587 int64_t total_size;
aliguori7c96d462008-09-12 17:54:13 +0000588 int is_protocol = 0;
Kevin Wolf91a073a2009-05-27 14:48:06 +0200589 BlockDriver *bdrv_qcow2;
590 QEMUOptionParameter *options;
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200591 char tmp_filename[PATH_MAX];
592 char backing_filename[PATH_MAX];
ths3b46e622007-09-17 08:09:54 +0000593
bellardea2384d2004-08-01 21:59:26 +0000594 /* if snapshot, we create a temporary backing file and open it
595 instead of opening 'filename' directly */
596
597 /* if there is a backing file, use it */
598 bs1 = bdrv_new("");
Kevin Wolfd6e90982010-03-31 14:40:27 +0200599 ret = bdrv_open(bs1, filename, 0, drv);
aliguori51d7c002009-03-05 23:00:29 +0000600 if (ret < 0) {
bellardea2384d2004-08-01 21:59:26 +0000601 bdrv_delete(bs1);
aliguori51d7c002009-03-05 23:00:29 +0000602 return ret;
bellardea2384d2004-08-01 21:59:26 +0000603 }
Jes Sorensen3e829902010-05-27 16:20:30 +0200604 total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK;
aliguori7c96d462008-09-12 17:54:13 +0000605
606 if (bs1->drv && bs1->drv->protocol_name)
607 is_protocol = 1;
608
bellardea2384d2004-08-01 21:59:26 +0000609 bdrv_delete(bs1);
ths3b46e622007-09-17 08:09:54 +0000610
bellardea2384d2004-08-01 21:59:26 +0000611 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
aliguori7c96d462008-09-12 17:54:13 +0000612
613 /* Real path is meaningless for protocols */
614 if (is_protocol)
615 snprintf(backing_filename, sizeof(backing_filename),
616 "%s", filename);
Kirill A. Shutemov114cdfa2009-12-25 18:19:22 +0000617 else if (!realpath(filename, backing_filename))
618 return -errno;
aliguori7c96d462008-09-12 17:54:13 +0000619
Kevin Wolf91a073a2009-05-27 14:48:06 +0200620 bdrv_qcow2 = bdrv_find_format("qcow2");
621 options = parse_option_parameters("", bdrv_qcow2->create_options, NULL);
622
Jes Sorensen3e829902010-05-27 16:20:30 +0200623 set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size);
Kevin Wolf91a073a2009-05-27 14:48:06 +0200624 set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename);
625 if (drv) {
626 set_option_parameter(options, BLOCK_OPT_BACKING_FMT,
627 drv->format_name);
628 }
629
630 ret = bdrv_create(bdrv_qcow2, tmp_filename, options);
Jan Kiszkad7487682010-04-29 18:24:50 +0200631 free_option_parameters(options);
aliguori51d7c002009-03-05 23:00:29 +0000632 if (ret < 0) {
633 return ret;
bellardea2384d2004-08-01 21:59:26 +0000634 }
Kevin Wolf91a073a2009-05-27 14:48:06 +0200635
bellardea2384d2004-08-01 21:59:26 +0000636 filename = tmp_filename;
Kevin Wolf91a073a2009-05-27 14:48:06 +0200637 drv = bdrv_qcow2;
bellardea2384d2004-08-01 21:59:26 +0000638 bs->is_temporary = 1;
639 }
bellard712e7872005-04-28 21:09:32 +0000640
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200641 /* Find the right image format driver */
Christoph Hellwig6db95602010-04-05 16:53:57 +0200642 if (!drv) {
Stefan Weilc98ac352010-07-21 21:51:51 +0200643 ret = find_image_format(filename, &drv);
aliguori51d7c002009-03-05 23:00:29 +0000644 }
Christoph Hellwig69873072010-01-20 18:13:25 +0100645
aliguori51d7c002009-03-05 23:00:29 +0000646 if (!drv) {
aliguori51d7c002009-03-05 23:00:29 +0000647 goto unlink_and_fail;
bellardea2384d2004-08-01 21:59:26 +0000648 }
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200649
650 /* Open the image */
651 ret = bdrv_open_common(bs, filename, flags, drv);
652 if (ret < 0) {
Christoph Hellwig69873072010-01-20 18:13:25 +0100653 goto unlink_and_fail;
654 }
655
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200656 /* If there is a backing file, use it */
657 if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') {
658 char backing_filename[PATH_MAX];
659 int back_flags;
660 BlockDriver *back_drv = NULL;
661
662 bs->backing_hd = bdrv_new("");
Stefan Hajnoczidf2dbb42010-12-02 16:54:13 +0000663
664 if (path_has_protocol(bs->backing_file)) {
665 pstrcpy(backing_filename, sizeof(backing_filename),
666 bs->backing_file);
667 } else {
668 path_combine(backing_filename, sizeof(backing_filename),
669 filename, bs->backing_file);
670 }
671
672 if (bs->backing_format[0] != '\0') {
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200673 back_drv = bdrv_find_format(bs->backing_format);
Stefan Hajnoczidf2dbb42010-12-02 16:54:13 +0000674 }
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200675
676 /* backing files always opened read-only */
677 back_flags =
678 flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
679
680 ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv);
681 if (ret < 0) {
682 bdrv_close(bs);
683 return ret;
684 }
685 if (bs->is_temporary) {
686 bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR);
687 } else {
688 /* base image inherits from "parent" */
689 bs->backing_hd->keep_read_only = bs->keep_read_only;
690 }
691 }
692
693 if (!bdrv_key_required(bs)) {
Markus Armbruster7d4b4ba2011-09-06 18:58:59 +0200694 bdrv_dev_change_media_cb(bs, true);
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200695 }
696
697 return 0;
698
699unlink_and_fail:
700 if (bs->is_temporary) {
701 unlink(filename);
702 }
703 return ret;
704}
705
bellardfc01f7e2003-06-30 10:03:06 +0000706void bdrv_close(BlockDriverState *bs)
707{
bellard19cb3732006-08-19 11:45:59 +0000708 if (bs->drv) {
Markus Armbrusterf9092b12010-06-25 10:33:39 +0200709 if (bs == bs_snapshots) {
710 bs_snapshots = NULL;
711 }
Stefan Hajnoczi557df6a2010-04-17 10:49:06 +0100712 if (bs->backing_hd) {
bellardea2384d2004-08-01 21:59:26 +0000713 bdrv_delete(bs->backing_hd);
Stefan Hajnoczi557df6a2010-04-17 10:49:06 +0100714 bs->backing_hd = NULL;
715 }
bellardea2384d2004-08-01 21:59:26 +0000716 bs->drv->bdrv_close(bs);
Anthony Liguori7267c092011-08-20 22:09:37 -0500717 g_free(bs->opaque);
bellardea2384d2004-08-01 21:59:26 +0000718#ifdef _WIN32
719 if (bs->is_temporary) {
720 unlink(bs->filename);
721 }
bellard67b915a2004-03-31 23:37:16 +0000722#endif
bellardea2384d2004-08-01 21:59:26 +0000723 bs->opaque = NULL;
724 bs->drv = NULL;
bellardb3380822004-03-14 21:38:54 +0000725
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200726 if (bs->file != NULL) {
727 bdrv_close(bs->file);
728 }
729
Markus Armbruster7d4b4ba2011-09-06 18:58:59 +0200730 bdrv_dev_change_media_cb(bs, false);
bellardb3380822004-03-14 21:38:54 +0000731 }
732}
733
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +0900734void bdrv_close_all(void)
735{
736 BlockDriverState *bs;
737
738 QTAILQ_FOREACH(bs, &bdrv_states, list) {
739 bdrv_close(bs);
740 }
741}
742
Ryan Harperd22b2f42011-03-29 20:51:47 -0500743/* make a BlockDriverState anonymous by removing from bdrv_state list.
744 Also, NULL terminate the device_name to prevent double remove */
745void bdrv_make_anon(BlockDriverState *bs)
746{
747 if (bs->device_name[0] != '\0') {
748 QTAILQ_REMOVE(&bdrv_states, bs, list);
749 }
750 bs->device_name[0] = '\0';
751}
752
bellardb3380822004-03-14 21:38:54 +0000753void bdrv_delete(BlockDriverState *bs)
754{
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200755 assert(!bs->dev);
Markus Armbruster18846de2010-06-29 16:58:30 +0200756
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +0100757 /* remove from list, if necessary */
Ryan Harperd22b2f42011-03-29 20:51:47 -0500758 bdrv_make_anon(bs);
aurel3234c6f052008-04-08 19:51:21 +0000759
bellardb3380822004-03-14 21:38:54 +0000760 bdrv_close(bs);
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200761 if (bs->file != NULL) {
762 bdrv_delete(bs->file);
763 }
764
Markus Armbrusterf9092b12010-06-25 10:33:39 +0200765 assert(bs != bs_snapshots);
Anthony Liguori7267c092011-08-20 22:09:37 -0500766 g_free(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000767}
768
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200769int bdrv_attach_dev(BlockDriverState *bs, void *dev)
770/* TODO change to DeviceState *dev when all users are qdevified */
Markus Armbruster18846de2010-06-29 16:58:30 +0200771{
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200772 if (bs->dev) {
Markus Armbruster18846de2010-06-29 16:58:30 +0200773 return -EBUSY;
774 }
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200775 bs->dev = dev;
Luiz Capitulino28a72822011-09-26 17:43:50 -0300776 bdrv_iostatus_reset(bs);
Markus Armbruster18846de2010-06-29 16:58:30 +0200777 return 0;
778}
779
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200780/* TODO qdevified devices don't use this, remove when devices are qdevified */
781void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev)
Markus Armbruster18846de2010-06-29 16:58:30 +0200782{
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200783 if (bdrv_attach_dev(bs, dev) < 0) {
784 abort();
785 }
786}
787
788void bdrv_detach_dev(BlockDriverState *bs, void *dev)
789/* TODO change to DeviceState *dev when all users are qdevified */
790{
791 assert(bs->dev == dev);
792 bs->dev = NULL;
Markus Armbruster0e49de52011-08-03 15:07:41 +0200793 bs->dev_ops = NULL;
794 bs->dev_opaque = NULL;
Markus Armbruster29e05f22011-09-06 18:58:57 +0200795 bs->buffer_alignment = 512;
Markus Armbruster18846de2010-06-29 16:58:30 +0200796}
797
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200798/* TODO change to return DeviceState * when all users are qdevified */
799void *bdrv_get_attached_dev(BlockDriverState *bs)
Markus Armbruster18846de2010-06-29 16:58:30 +0200800{
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200801 return bs->dev;
Markus Armbruster18846de2010-06-29 16:58:30 +0200802}
803
Markus Armbruster0e49de52011-08-03 15:07:41 +0200804void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops,
805 void *opaque)
806{
807 bs->dev_ops = ops;
808 bs->dev_opaque = opaque;
Markus Armbruster2c6942f2011-09-06 18:58:51 +0200809 if (bdrv_dev_has_removable_media(bs) && bs == bs_snapshots) {
810 bs_snapshots = NULL;
811 }
Markus Armbruster0e49de52011-08-03 15:07:41 +0200812}
813
Markus Armbruster7d4b4ba2011-09-06 18:58:59 +0200814static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load)
Markus Armbruster0e49de52011-08-03 15:07:41 +0200815{
Markus Armbruster145feb12011-08-03 15:07:42 +0200816 if (bs->dev_ops && bs->dev_ops->change_media_cb) {
Markus Armbruster7d4b4ba2011-09-06 18:58:59 +0200817 bs->dev_ops->change_media_cb(bs->dev_opaque, load);
Markus Armbruster145feb12011-08-03 15:07:42 +0200818 }
819}
820
Markus Armbruster2c6942f2011-09-06 18:58:51 +0200821bool bdrv_dev_has_removable_media(BlockDriverState *bs)
822{
823 return !bs->dev || (bs->dev_ops && bs->dev_ops->change_media_cb);
824}
825
Markus Armbrustere4def802011-09-06 18:58:53 +0200826bool bdrv_dev_is_tray_open(BlockDriverState *bs)
827{
828 if (bs->dev_ops && bs->dev_ops->is_tray_open) {
829 return bs->dev_ops->is_tray_open(bs->dev_opaque);
830 }
831 return false;
832}
833
Markus Armbruster145feb12011-08-03 15:07:42 +0200834static void bdrv_dev_resize_cb(BlockDriverState *bs)
835{
836 if (bs->dev_ops && bs->dev_ops->resize_cb) {
837 bs->dev_ops->resize_cb(bs->dev_opaque);
Markus Armbruster0e49de52011-08-03 15:07:41 +0200838 }
839}
840
Markus Armbrusterf1076392011-09-06 18:58:46 +0200841bool bdrv_dev_is_medium_locked(BlockDriverState *bs)
842{
843 if (bs->dev_ops && bs->dev_ops->is_medium_locked) {
844 return bs->dev_ops->is_medium_locked(bs->dev_opaque);
845 }
846 return false;
847}
848
aliguorie97fc192009-04-21 23:11:50 +0000849/*
850 * Run consistency checks on an image
851 *
Kevin Wolfe076f332010-06-29 11:43:13 +0200852 * Returns 0 if the check could be completed (it doesn't mean that the image is
Stefan Weila1c72732011-04-28 17:20:38 +0200853 * free of errors) or -errno when an internal error occurred. The results of the
Kevin Wolfe076f332010-06-29 11:43:13 +0200854 * check are stored in res.
aliguorie97fc192009-04-21 23:11:50 +0000855 */
Kevin Wolfe076f332010-06-29 11:43:13 +0200856int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res)
aliguorie97fc192009-04-21 23:11:50 +0000857{
858 if (bs->drv->bdrv_check == NULL) {
859 return -ENOTSUP;
860 }
861
Kevin Wolfe076f332010-06-29 11:43:13 +0200862 memset(res, 0, sizeof(*res));
Kevin Wolf9ac228e2010-06-29 12:37:54 +0200863 return bs->drv->bdrv_check(bs, res);
aliguorie97fc192009-04-21 23:11:50 +0000864}
865
Kevin Wolf8a426612010-07-16 17:17:01 +0200866#define COMMIT_BUF_SECTORS 2048
867
bellard33e39632003-07-06 17:15:21 +0000868/* commit COW file into the raw image */
869int bdrv_commit(BlockDriverState *bs)
870{
bellard19cb3732006-08-19 11:45:59 +0000871 BlockDriver *drv = bs->drv;
Kevin Wolfee181192010-08-05 13:05:22 +0200872 BlockDriver *backing_drv;
Kevin Wolf8a426612010-07-16 17:17:01 +0200873 int64_t sector, total_sectors;
874 int n, ro, open_flags;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200875 int ret = 0, rw_ret = 0;
Kevin Wolf8a426612010-07-16 17:17:01 +0200876 uint8_t *buf;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200877 char filename[1024];
878 BlockDriverState *bs_rw, *bs_ro;
bellard33e39632003-07-06 17:15:21 +0000879
bellard19cb3732006-08-19 11:45:59 +0000880 if (!drv)
881 return -ENOMEDIUM;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200882
883 if (!bs->backing_hd) {
884 return -ENOTSUP;
bellard33e39632003-07-06 17:15:21 +0000885 }
886
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200887 if (bs->backing_hd->keep_read_only) {
888 return -EACCES;
889 }
Kevin Wolfee181192010-08-05 13:05:22 +0200890
891 backing_drv = bs->backing_hd->drv;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200892 ro = bs->backing_hd->read_only;
893 strncpy(filename, bs->backing_hd->filename, sizeof(filename));
894 open_flags = bs->backing_hd->open_flags;
895
896 if (ro) {
897 /* re-open as RW */
898 bdrv_delete(bs->backing_hd);
899 bs->backing_hd = NULL;
900 bs_rw = bdrv_new("");
Kevin Wolfee181192010-08-05 13:05:22 +0200901 rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR,
902 backing_drv);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200903 if (rw_ret < 0) {
904 bdrv_delete(bs_rw);
905 /* try to re-open read-only */
906 bs_ro = bdrv_new("");
Kevin Wolfee181192010-08-05 13:05:22 +0200907 ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR,
908 backing_drv);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200909 if (ret < 0) {
910 bdrv_delete(bs_ro);
911 /* drive not functional anymore */
912 bs->drv = NULL;
913 return ret;
914 }
915 bs->backing_hd = bs_ro;
916 return rw_ret;
917 }
918 bs->backing_hd = bs_rw;
bellard33e39632003-07-06 17:15:21 +0000919 }
bellardea2384d2004-08-01 21:59:26 +0000920
Jan Kiszka6ea44302009-11-30 18:21:19 +0100921 total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
Anthony Liguori7267c092011-08-20 22:09:37 -0500922 buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE);
bellardea2384d2004-08-01 21:59:26 +0000923
Kevin Wolf8a426612010-07-16 17:17:01 +0200924 for (sector = 0; sector < total_sectors; sector += n) {
925 if (drv->bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) {
926
927 if (bdrv_read(bs, sector, buf, n) != 0) {
928 ret = -EIO;
929 goto ro_cleanup;
930 }
931
932 if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) {
933 ret = -EIO;
934 goto ro_cleanup;
935 }
bellardea2384d2004-08-01 21:59:26 +0000936 }
937 }
bellard95389c82005-12-18 18:28:15 +0000938
Christoph Hellwig1d449522010-01-17 12:32:30 +0100939 if (drv->bdrv_make_empty) {
940 ret = drv->bdrv_make_empty(bs);
941 bdrv_flush(bs);
942 }
bellard95389c82005-12-18 18:28:15 +0000943
Christoph Hellwig3f5075a2010-01-12 13:49:23 +0100944 /*
945 * Make sure all data we wrote to the backing device is actually
946 * stable on disk.
947 */
948 if (bs->backing_hd)
949 bdrv_flush(bs->backing_hd);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200950
951ro_cleanup:
Anthony Liguori7267c092011-08-20 22:09:37 -0500952 g_free(buf);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200953
954 if (ro) {
955 /* re-open as RO */
956 bdrv_delete(bs->backing_hd);
957 bs->backing_hd = NULL;
958 bs_ro = bdrv_new("");
Kevin Wolfee181192010-08-05 13:05:22 +0200959 ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR,
960 backing_drv);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200961 if (ret < 0) {
962 bdrv_delete(bs_ro);
963 /* drive not functional anymore */
964 bs->drv = NULL;
965 return ret;
966 }
967 bs->backing_hd = bs_ro;
968 bs->backing_hd->keep_read_only = 0;
969 }
970
Christoph Hellwig1d449522010-01-17 12:32:30 +0100971 return ret;
bellard33e39632003-07-06 17:15:21 +0000972}
973
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200974void bdrv_commit_all(void)
975{
976 BlockDriverState *bs;
977
978 QTAILQ_FOREACH(bs, &bdrv_states, list) {
979 bdrv_commit(bs);
980 }
981}
982
Kevin Wolf756e6732010-01-12 12:55:17 +0100983/*
984 * Return values:
985 * 0 - success
986 * -EINVAL - backing format specified, but no file
987 * -ENOSPC - can't update the backing file because no space is left in the
988 * image file header
989 * -ENOTSUP - format driver doesn't support changing the backing file
990 */
991int bdrv_change_backing_file(BlockDriverState *bs,
992 const char *backing_file, const char *backing_fmt)
993{
994 BlockDriver *drv = bs->drv;
995
996 if (drv->bdrv_change_backing_file != NULL) {
997 return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
998 } else {
999 return -ENOTSUP;
1000 }
1001}
1002
aliguori71d07702009-03-03 17:37:16 +00001003static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
1004 size_t size)
1005{
1006 int64_t len;
1007
1008 if (!bdrv_is_inserted(bs))
1009 return -ENOMEDIUM;
1010
1011 if (bs->growable)
1012 return 0;
1013
1014 len = bdrv_getlength(bs);
1015
Kevin Wolffbb7b4e2009-05-08 14:47:24 +02001016 if (offset < 0)
1017 return -EIO;
1018
1019 if ((offset > len) || (len - offset < size))
aliguori71d07702009-03-03 17:37:16 +00001020 return -EIO;
1021
1022 return 0;
1023}
1024
1025static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
1026 int nb_sectors)
1027{
Jes Sorenseneb5a3162010-05-27 16:20:31 +02001028 return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE,
1029 nb_sectors * BDRV_SECTOR_SIZE);
aliguori71d07702009-03-03 17:37:16 +00001030}
1031
Kevin Wolfe7a8a782011-07-15 16:05:00 +02001032static inline bool bdrv_has_async_rw(BlockDriver *drv)
1033{
1034 return drv->bdrv_co_readv != bdrv_co_readv_em
1035 || drv->bdrv_aio_readv != bdrv_aio_readv_em;
1036}
1037
1038static inline bool bdrv_has_async_flush(BlockDriver *drv)
1039{
1040 return drv->bdrv_aio_flush != bdrv_aio_flush_em;
1041}
1042
bellard19cb3732006-08-19 11:45:59 +00001043/* return < 0 if error. See bdrv_write() for the return codes */
ths5fafdf22007-09-16 21:08:06 +00001044int bdrv_read(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +00001045 uint8_t *buf, int nb_sectors)
1046{
bellardea2384d2004-08-01 21:59:26 +00001047 BlockDriver *drv = bs->drv;
1048
bellard19cb3732006-08-19 11:45:59 +00001049 if (!drv)
1050 return -ENOMEDIUM;
Kevin Wolfe7a8a782011-07-15 16:05:00 +02001051
1052 if (bdrv_has_async_rw(drv) && qemu_in_coroutine()) {
1053 QEMUIOVector qiov;
1054 struct iovec iov = {
1055 .iov_base = (void *)buf,
1056 .iov_len = nb_sectors * BDRV_SECTOR_SIZE,
1057 };
1058
1059 qemu_iovec_init_external(&qiov, &iov, 1);
1060 return bdrv_co_readv(bs, sector_num, nb_sectors, &qiov);
1061 }
1062
aliguori71d07702009-03-03 17:37:16 +00001063 if (bdrv_check_request(bs, sector_num, nb_sectors))
1064 return -EIO;
bellardb3380822004-03-14 21:38:54 +00001065
aliguorieda578e2009-03-12 19:57:16 +00001066 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
bellardfc01f7e2003-06-30 10:03:06 +00001067}
1068
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001069static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num,
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001070 int nb_sectors, int dirty)
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001071{
1072 int64_t start, end;
Jan Kiszkac6d22832009-11-30 18:21:20 +01001073 unsigned long val, idx, bit;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001074
Jan Kiszka6ea44302009-11-30 18:21:19 +01001075 start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkac6d22832009-11-30 18:21:20 +01001076 end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001077
1078 for (; start <= end; start++) {
Jan Kiszkac6d22832009-11-30 18:21:20 +01001079 idx = start / (sizeof(unsigned long) * 8);
1080 bit = start % (sizeof(unsigned long) * 8);
1081 val = bs->dirty_bitmap[idx];
1082 if (dirty) {
Marcelo Tosatti6d59fec2010-11-08 17:02:54 -02001083 if (!(val & (1UL << bit))) {
Liran Schouraaa0eb72010-01-26 10:31:48 +02001084 bs->dirty_count++;
Marcelo Tosatti6d59fec2010-11-08 17:02:54 -02001085 val |= 1UL << bit;
Liran Schouraaa0eb72010-01-26 10:31:48 +02001086 }
Jan Kiszkac6d22832009-11-30 18:21:20 +01001087 } else {
Marcelo Tosatti6d59fec2010-11-08 17:02:54 -02001088 if (val & (1UL << bit)) {
Liran Schouraaa0eb72010-01-26 10:31:48 +02001089 bs->dirty_count--;
Marcelo Tosatti6d59fec2010-11-08 17:02:54 -02001090 val &= ~(1UL << bit);
Liran Schouraaa0eb72010-01-26 10:31:48 +02001091 }
Jan Kiszkac6d22832009-11-30 18:21:20 +01001092 }
1093 bs->dirty_bitmap[idx] = val;
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001094 }
1095}
1096
ths5fafdf22007-09-16 21:08:06 +00001097/* Return < 0 if error. Important errors are:
bellard19cb3732006-08-19 11:45:59 +00001098 -EIO generic I/O error (may happen for all errors)
1099 -ENOMEDIUM No media inserted.
1100 -EINVAL Invalid sector number or nb_sectors
1101 -EACCES Trying to write a read-only device
1102*/
ths5fafdf22007-09-16 21:08:06 +00001103int bdrv_write(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +00001104 const uint8_t *buf, int nb_sectors)
1105{
bellard83f64092006-08-01 16:21:11 +00001106 BlockDriver *drv = bs->drv;
Kevin Wolfe7a8a782011-07-15 16:05:00 +02001107
bellard19cb3732006-08-19 11:45:59 +00001108 if (!bs->drv)
1109 return -ENOMEDIUM;
Kevin Wolfe7a8a782011-07-15 16:05:00 +02001110
1111 if (bdrv_has_async_rw(drv) && qemu_in_coroutine()) {
1112 QEMUIOVector qiov;
1113 struct iovec iov = {
1114 .iov_base = (void *)buf,
1115 .iov_len = nb_sectors * BDRV_SECTOR_SIZE,
1116 };
1117
1118 qemu_iovec_init_external(&qiov, &iov, 1);
1119 return bdrv_co_writev(bs, sector_num, nb_sectors, &qiov);
1120 }
1121
bellard0849bf02003-06-30 23:17:31 +00001122 if (bs->read_only)
bellard19cb3732006-08-19 11:45:59 +00001123 return -EACCES;
aliguori71d07702009-03-03 17:37:16 +00001124 if (bdrv_check_request(bs, sector_num, nb_sectors))
1125 return -EIO;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001126
Jan Kiszkac6d22832009-11-30 18:21:20 +01001127 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001128 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1129 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001130
Kevin Wolf294cc352010-04-28 14:34:01 +02001131 if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
1132 bs->wr_highest_sector = sector_num + nb_sectors - 1;
1133 }
1134
aliguori42fb2802009-01-15 20:43:39 +00001135 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
bellard83f64092006-08-01 16:21:11 +00001136}
1137
aliguorieda578e2009-03-12 19:57:16 +00001138int bdrv_pread(BlockDriverState *bs, int64_t offset,
1139 void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +00001140{
Jan Kiszka6ea44302009-11-30 18:21:19 +01001141 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
bellard83f64092006-08-01 16:21:11 +00001142 int len, nb_sectors, count;
1143 int64_t sector_num;
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001144 int ret;
bellard83f64092006-08-01 16:21:11 +00001145
1146 count = count1;
1147 /* first read to align to sector start */
Jan Kiszka6ea44302009-11-30 18:21:19 +01001148 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
bellard83f64092006-08-01 16:21:11 +00001149 if (len > count)
1150 len = count;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001151 sector_num = offset >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001152 if (len > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001153 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1154 return ret;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001155 memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
bellard83f64092006-08-01 16:21:11 +00001156 count -= len;
1157 if (count == 0)
1158 return count1;
1159 sector_num++;
1160 buf += len;
1161 }
1162
1163 /* read the sectors "in place" */
Jan Kiszka6ea44302009-11-30 18:21:19 +01001164 nb_sectors = count >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001165 if (nb_sectors > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001166 if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0)
1167 return ret;
bellard83f64092006-08-01 16:21:11 +00001168 sector_num += nb_sectors;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001169 len = nb_sectors << BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001170 buf += len;
1171 count -= len;
1172 }
1173
1174 /* add data from the last sector */
1175 if (count > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001176 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1177 return ret;
bellard83f64092006-08-01 16:21:11 +00001178 memcpy(buf, tmp_buf, count);
1179 }
1180 return count1;
1181}
1182
aliguorieda578e2009-03-12 19:57:16 +00001183int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
1184 const void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +00001185{
Jan Kiszka6ea44302009-11-30 18:21:19 +01001186 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
bellard83f64092006-08-01 16:21:11 +00001187 int len, nb_sectors, count;
1188 int64_t sector_num;
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001189 int ret;
bellard83f64092006-08-01 16:21:11 +00001190
1191 count = count1;
1192 /* first write to align to sector start */
Jan Kiszka6ea44302009-11-30 18:21:19 +01001193 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
bellard83f64092006-08-01 16:21:11 +00001194 if (len > count)
1195 len = count;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001196 sector_num = offset >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001197 if (len > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001198 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1199 return ret;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001200 memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len);
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001201 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
1202 return ret;
bellard83f64092006-08-01 16:21:11 +00001203 count -= len;
1204 if (count == 0)
1205 return count1;
1206 sector_num++;
1207 buf += len;
1208 }
1209
1210 /* write the sectors "in place" */
Jan Kiszka6ea44302009-11-30 18:21:19 +01001211 nb_sectors = count >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001212 if (nb_sectors > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001213 if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0)
1214 return ret;
bellard83f64092006-08-01 16:21:11 +00001215 sector_num += nb_sectors;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001216 len = nb_sectors << BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001217 buf += len;
1218 count -= len;
1219 }
1220
1221 /* add data from the last sector */
1222 if (count > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001223 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1224 return ret;
bellard83f64092006-08-01 16:21:11 +00001225 memcpy(tmp_buf, buf, count);
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001226 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
1227 return ret;
bellard83f64092006-08-01 16:21:11 +00001228 }
1229 return count1;
1230}
bellard83f64092006-08-01 16:21:11 +00001231
Kevin Wolff08145f2010-06-16 16:38:15 +02001232/*
1233 * Writes to the file and ensures that no writes are reordered across this
1234 * request (acts as a barrier)
1235 *
1236 * Returns 0 on success, -errno in error cases.
1237 */
1238int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset,
1239 const void *buf, int count)
1240{
1241 int ret;
1242
1243 ret = bdrv_pwrite(bs, offset, buf, count);
1244 if (ret < 0) {
1245 return ret;
1246 }
1247
Stefan Hajnoczi92196b22011-08-04 12:26:52 +01001248 /* No flush needed for cache modes that use O_DSYNC */
1249 if ((bs->open_flags & BDRV_O_CACHE_WB) != 0) {
Kevin Wolff08145f2010-06-16 16:38:15 +02001250 bdrv_flush(bs);
1251 }
1252
1253 return 0;
1254}
1255
Kevin Wolfda1fa912011-07-14 17:27:13 +02001256int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num,
1257 int nb_sectors, QEMUIOVector *qiov)
1258{
1259 BlockDriver *drv = bs->drv;
1260
1261 trace_bdrv_co_readv(bs, sector_num, nb_sectors);
1262
1263 if (!drv) {
1264 return -ENOMEDIUM;
1265 }
1266 if (bdrv_check_request(bs, sector_num, nb_sectors)) {
1267 return -EIO;
1268 }
1269
1270 return drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov);
1271}
1272
1273int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num,
1274 int nb_sectors, QEMUIOVector *qiov)
1275{
1276 BlockDriver *drv = bs->drv;
1277
1278 trace_bdrv_co_writev(bs, sector_num, nb_sectors);
1279
1280 if (!bs->drv) {
1281 return -ENOMEDIUM;
1282 }
1283 if (bs->read_only) {
1284 return -EACCES;
1285 }
1286 if (bdrv_check_request(bs, sector_num, nb_sectors)) {
1287 return -EIO;
1288 }
1289
1290 if (bs->dirty_bitmap) {
1291 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1292 }
1293
1294 if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
1295 bs->wr_highest_sector = sector_num + nb_sectors - 1;
1296 }
1297
1298 return drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov);
1299}
1300
bellard83f64092006-08-01 16:21:11 +00001301/**
bellard83f64092006-08-01 16:21:11 +00001302 * Truncate file to 'offset' bytes (needed only for file protocols)
1303 */
1304int bdrv_truncate(BlockDriverState *bs, int64_t offset)
1305{
1306 BlockDriver *drv = bs->drv;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001307 int ret;
bellard83f64092006-08-01 16:21:11 +00001308 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001309 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +00001310 if (!drv->bdrv_truncate)
1311 return -ENOTSUP;
Naphtali Sprei59f26892009-10-26 16:25:16 +02001312 if (bs->read_only)
1313 return -EACCES;
Marcelo Tosatti85916752011-01-26 12:12:35 -02001314 if (bdrv_in_use(bs))
1315 return -EBUSY;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001316 ret = drv->bdrv_truncate(bs, offset);
1317 if (ret == 0) {
1318 ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
Markus Armbruster145feb12011-08-03 15:07:42 +02001319 bdrv_dev_resize_cb(bs);
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001320 }
1321 return ret;
bellard83f64092006-08-01 16:21:11 +00001322}
1323
1324/**
Fam Zheng4a1d5e12011-07-12 19:56:39 +08001325 * Length of a allocated file in bytes. Sparse files are counted by actual
1326 * allocated space. Return < 0 if error or unknown.
1327 */
1328int64_t bdrv_get_allocated_file_size(BlockDriverState *bs)
1329{
1330 BlockDriver *drv = bs->drv;
1331 if (!drv) {
1332 return -ENOMEDIUM;
1333 }
1334 if (drv->bdrv_get_allocated_file_size) {
1335 return drv->bdrv_get_allocated_file_size(bs);
1336 }
1337 if (bs->file) {
1338 return bdrv_get_allocated_file_size(bs->file);
1339 }
1340 return -ENOTSUP;
1341}
1342
1343/**
bellard83f64092006-08-01 16:21:11 +00001344 * Length of a file in bytes. Return < 0 if error or unknown.
1345 */
1346int64_t bdrv_getlength(BlockDriverState *bs)
1347{
1348 BlockDriver *drv = bs->drv;
1349 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001350 return -ENOMEDIUM;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001351
Markus Armbruster2c6942f2011-09-06 18:58:51 +02001352 if (bs->growable || bdrv_dev_has_removable_media(bs)) {
Stefan Hajnoczi46a4e4e2011-03-29 20:04:41 +01001353 if (drv->bdrv_getlength) {
1354 return drv->bdrv_getlength(bs);
1355 }
bellard83f64092006-08-01 16:21:11 +00001356 }
Stefan Hajnoczi46a4e4e2011-03-29 20:04:41 +01001357 return bs->total_sectors * BDRV_SECTOR_SIZE;
bellardfc01f7e2003-06-30 10:03:06 +00001358}
1359
bellard19cb3732006-08-19 11:45:59 +00001360/* return 0 as number of sectors if no device present or error */
ths96b8f132007-12-17 01:35:20 +00001361void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
bellardfc01f7e2003-06-30 10:03:06 +00001362{
bellard19cb3732006-08-19 11:45:59 +00001363 int64_t length;
1364 length = bdrv_getlength(bs);
1365 if (length < 0)
1366 length = 0;
1367 else
Jan Kiszka6ea44302009-11-30 18:21:19 +01001368 length = length >> BDRV_SECTOR_BITS;
bellard19cb3732006-08-19 11:45:59 +00001369 *nb_sectors_ptr = length;
bellardfc01f7e2003-06-30 10:03:06 +00001370}
bellardcf989512004-02-16 21:56:36 +00001371
aliguorif3d54fc2008-11-25 21:50:24 +00001372struct partition {
1373 uint8_t boot_ind; /* 0x80 - active */
1374 uint8_t head; /* starting head */
1375 uint8_t sector; /* starting sector */
1376 uint8_t cyl; /* starting cylinder */
1377 uint8_t sys_ind; /* What partition type */
1378 uint8_t end_head; /* end head */
1379 uint8_t end_sector; /* end sector */
1380 uint8_t end_cyl; /* end cylinder */
1381 uint32_t start_sect; /* starting sector counting from 0 */
1382 uint32_t nr_sects; /* nr of sectors in partition */
Stefan Weil541dc0d2011-08-31 12:38:01 +02001383} QEMU_PACKED;
aliguorif3d54fc2008-11-25 21:50:24 +00001384
1385/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
1386static int guess_disk_lchs(BlockDriverState *bs,
1387 int *pcylinders, int *pheads, int *psectors)
1388{
Jes Sorenseneb5a3162010-05-27 16:20:31 +02001389 uint8_t buf[BDRV_SECTOR_SIZE];
aliguorif3d54fc2008-11-25 21:50:24 +00001390 int ret, i, heads, sectors, cylinders;
1391 struct partition *p;
1392 uint32_t nr_sects;
blueswir1a38131b2008-12-05 17:56:40 +00001393 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +00001394
1395 bdrv_get_geometry(bs, &nb_sectors);
1396
1397 ret = bdrv_read(bs, 0, buf, 1);
1398 if (ret < 0)
1399 return -1;
1400 /* test msdos magic */
1401 if (buf[510] != 0x55 || buf[511] != 0xaa)
1402 return -1;
1403 for(i = 0; i < 4; i++) {
1404 p = ((struct partition *)(buf + 0x1be)) + i;
1405 nr_sects = le32_to_cpu(p->nr_sects);
1406 if (nr_sects && p->end_head) {
1407 /* We make the assumption that the partition terminates on
1408 a cylinder boundary */
1409 heads = p->end_head + 1;
1410 sectors = p->end_sector & 63;
1411 if (sectors == 0)
1412 continue;
1413 cylinders = nb_sectors / (heads * sectors);
1414 if (cylinders < 1 || cylinders > 16383)
1415 continue;
1416 *pheads = heads;
1417 *psectors = sectors;
1418 *pcylinders = cylinders;
1419#if 0
1420 printf("guessed geometry: LCHS=%d %d %d\n",
1421 cylinders, heads, sectors);
1422#endif
1423 return 0;
1424 }
1425 }
1426 return -1;
1427}
1428
1429void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
1430{
1431 int translation, lba_detected = 0;
1432 int cylinders, heads, secs;
blueswir1a38131b2008-12-05 17:56:40 +00001433 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +00001434
1435 /* if a geometry hint is available, use it */
1436 bdrv_get_geometry(bs, &nb_sectors);
1437 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
1438 translation = bdrv_get_translation_hint(bs);
1439 if (cylinders != 0) {
1440 *pcyls = cylinders;
1441 *pheads = heads;
1442 *psecs = secs;
1443 } else {
1444 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
1445 if (heads > 16) {
1446 /* if heads > 16, it means that a BIOS LBA
1447 translation was active, so the default
1448 hardware geometry is OK */
1449 lba_detected = 1;
1450 goto default_geometry;
1451 } else {
1452 *pcyls = cylinders;
1453 *pheads = heads;
1454 *psecs = secs;
1455 /* disable any translation to be in sync with
1456 the logical geometry */
1457 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
1458 bdrv_set_translation_hint(bs,
1459 BIOS_ATA_TRANSLATION_NONE);
1460 }
1461 }
1462 } else {
1463 default_geometry:
1464 /* if no geometry, use a standard physical disk geometry */
1465 cylinders = nb_sectors / (16 * 63);
1466
1467 if (cylinders > 16383)
1468 cylinders = 16383;
1469 else if (cylinders < 2)
1470 cylinders = 2;
1471 *pcyls = cylinders;
1472 *pheads = 16;
1473 *psecs = 63;
1474 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
1475 if ((*pcyls * *pheads) <= 131072) {
1476 bdrv_set_translation_hint(bs,
1477 BIOS_ATA_TRANSLATION_LARGE);
1478 } else {
1479 bdrv_set_translation_hint(bs,
1480 BIOS_ATA_TRANSLATION_LBA);
1481 }
1482 }
1483 }
1484 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
1485 }
1486}
1487
ths5fafdf22007-09-16 21:08:06 +00001488void bdrv_set_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +00001489 int cyls, int heads, int secs)
1490{
1491 bs->cyls = cyls;
1492 bs->heads = heads;
1493 bs->secs = secs;
1494}
1495
bellard46d47672004-11-16 01:45:27 +00001496void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
1497{
1498 bs->translation = translation;
1499}
1500
ths5fafdf22007-09-16 21:08:06 +00001501void bdrv_get_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +00001502 int *pcyls, int *pheads, int *psecs)
1503{
1504 *pcyls = bs->cyls;
1505 *pheads = bs->heads;
1506 *psecs = bs->secs;
1507}
1508
Blue Swirl5bbdbb42011-02-12 20:43:32 +00001509/* Recognize floppy formats */
1510typedef struct FDFormat {
1511 FDriveType drive;
1512 uint8_t last_sect;
1513 uint8_t max_track;
1514 uint8_t max_head;
1515} FDFormat;
1516
1517static const FDFormat fd_formats[] = {
1518 /* First entry is default format */
1519 /* 1.44 MB 3"1/2 floppy disks */
1520 { FDRIVE_DRV_144, 18, 80, 1, },
1521 { FDRIVE_DRV_144, 20, 80, 1, },
1522 { FDRIVE_DRV_144, 21, 80, 1, },
1523 { FDRIVE_DRV_144, 21, 82, 1, },
1524 { FDRIVE_DRV_144, 21, 83, 1, },
1525 { FDRIVE_DRV_144, 22, 80, 1, },
1526 { FDRIVE_DRV_144, 23, 80, 1, },
1527 { FDRIVE_DRV_144, 24, 80, 1, },
1528 /* 2.88 MB 3"1/2 floppy disks */
1529 { FDRIVE_DRV_288, 36, 80, 1, },
1530 { FDRIVE_DRV_288, 39, 80, 1, },
1531 { FDRIVE_DRV_288, 40, 80, 1, },
1532 { FDRIVE_DRV_288, 44, 80, 1, },
1533 { FDRIVE_DRV_288, 48, 80, 1, },
1534 /* 720 kB 3"1/2 floppy disks */
1535 { FDRIVE_DRV_144, 9, 80, 1, },
1536 { FDRIVE_DRV_144, 10, 80, 1, },
1537 { FDRIVE_DRV_144, 10, 82, 1, },
1538 { FDRIVE_DRV_144, 10, 83, 1, },
1539 { FDRIVE_DRV_144, 13, 80, 1, },
1540 { FDRIVE_DRV_144, 14, 80, 1, },
1541 /* 1.2 MB 5"1/4 floppy disks */
1542 { FDRIVE_DRV_120, 15, 80, 1, },
1543 { FDRIVE_DRV_120, 18, 80, 1, },
1544 { FDRIVE_DRV_120, 18, 82, 1, },
1545 { FDRIVE_DRV_120, 18, 83, 1, },
1546 { FDRIVE_DRV_120, 20, 80, 1, },
1547 /* 720 kB 5"1/4 floppy disks */
1548 { FDRIVE_DRV_120, 9, 80, 1, },
1549 { FDRIVE_DRV_120, 11, 80, 1, },
1550 /* 360 kB 5"1/4 floppy disks */
1551 { FDRIVE_DRV_120, 9, 40, 1, },
1552 { FDRIVE_DRV_120, 9, 40, 0, },
1553 { FDRIVE_DRV_120, 10, 41, 1, },
1554 { FDRIVE_DRV_120, 10, 42, 1, },
1555 /* 320 kB 5"1/4 floppy disks */
1556 { FDRIVE_DRV_120, 8, 40, 1, },
1557 { FDRIVE_DRV_120, 8, 40, 0, },
1558 /* 360 kB must match 5"1/4 better than 3"1/2... */
1559 { FDRIVE_DRV_144, 9, 80, 0, },
1560 /* end */
1561 { FDRIVE_DRV_NONE, -1, -1, 0, },
1562};
1563
1564void bdrv_get_floppy_geometry_hint(BlockDriverState *bs, int *nb_heads,
1565 int *max_track, int *last_sect,
1566 FDriveType drive_in, FDriveType *drive)
1567{
1568 const FDFormat *parse;
1569 uint64_t nb_sectors, size;
1570 int i, first_match, match;
1571
1572 bdrv_get_geometry_hint(bs, nb_heads, max_track, last_sect);
1573 if (*nb_heads != 0 && *max_track != 0 && *last_sect != 0) {
1574 /* User defined disk */
1575 } else {
1576 bdrv_get_geometry(bs, &nb_sectors);
1577 match = -1;
1578 first_match = -1;
1579 for (i = 0; ; i++) {
1580 parse = &fd_formats[i];
1581 if (parse->drive == FDRIVE_DRV_NONE) {
1582 break;
1583 }
1584 if (drive_in == parse->drive ||
1585 drive_in == FDRIVE_DRV_NONE) {
1586 size = (parse->max_head + 1) * parse->max_track *
1587 parse->last_sect;
1588 if (nb_sectors == size) {
1589 match = i;
1590 break;
1591 }
1592 if (first_match == -1) {
1593 first_match = i;
1594 }
1595 }
1596 }
1597 if (match == -1) {
1598 if (first_match == -1) {
1599 match = 1;
1600 } else {
1601 match = first_match;
1602 }
1603 parse = &fd_formats[match];
1604 }
1605 *nb_heads = parse->max_head + 1;
1606 *max_track = parse->max_track;
1607 *last_sect = parse->last_sect;
1608 *drive = parse->drive;
1609 }
1610}
1611
bellard46d47672004-11-16 01:45:27 +00001612int bdrv_get_translation_hint(BlockDriverState *bs)
1613{
1614 return bs->translation;
1615}
1616
Markus Armbrusterabd7f682010-06-02 18:55:17 +02001617void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error,
1618 BlockErrorAction on_write_error)
1619{
1620 bs->on_read_error = on_read_error;
1621 bs->on_write_error = on_write_error;
1622}
1623
1624BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read)
1625{
1626 return is_read ? bs->on_read_error : bs->on_write_error;
1627}
1628
bellardb3380822004-03-14 21:38:54 +00001629int bdrv_is_read_only(BlockDriverState *bs)
1630{
1631 return bs->read_only;
1632}
1633
ths985a03b2007-12-24 16:10:43 +00001634int bdrv_is_sg(BlockDriverState *bs)
1635{
1636 return bs->sg;
1637}
1638
Christoph Hellwige900a7b2009-09-04 19:01:15 +02001639int bdrv_enable_write_cache(BlockDriverState *bs)
1640{
1641 return bs->enable_write_cache;
1642}
1643
bellardea2384d2004-08-01 21:59:26 +00001644int bdrv_is_encrypted(BlockDriverState *bs)
1645{
1646 if (bs->backing_hd && bs->backing_hd->encrypted)
1647 return 1;
1648 return bs->encrypted;
1649}
1650
aliguoric0f4ce72009-03-05 23:01:01 +00001651int bdrv_key_required(BlockDriverState *bs)
1652{
1653 BlockDriverState *backing_hd = bs->backing_hd;
1654
1655 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
1656 return 1;
1657 return (bs->encrypted && !bs->valid_key);
1658}
1659
bellardea2384d2004-08-01 21:59:26 +00001660int bdrv_set_key(BlockDriverState *bs, const char *key)
1661{
1662 int ret;
1663 if (bs->backing_hd && bs->backing_hd->encrypted) {
1664 ret = bdrv_set_key(bs->backing_hd, key);
1665 if (ret < 0)
1666 return ret;
1667 if (!bs->encrypted)
1668 return 0;
1669 }
Shahar Havivifd04a2a2010-03-06 00:26:13 +02001670 if (!bs->encrypted) {
1671 return -EINVAL;
1672 } else if (!bs->drv || !bs->drv->bdrv_set_key) {
1673 return -ENOMEDIUM;
1674 }
aliguoric0f4ce72009-03-05 23:01:01 +00001675 ret = bs->drv->bdrv_set_key(bs, key);
aliguoribb5fc202009-03-05 23:01:15 +00001676 if (ret < 0) {
1677 bs->valid_key = 0;
1678 } else if (!bs->valid_key) {
1679 bs->valid_key = 1;
1680 /* call the change callback now, we skipped it on open */
Markus Armbruster7d4b4ba2011-09-06 18:58:59 +02001681 bdrv_dev_change_media_cb(bs, true);
aliguoribb5fc202009-03-05 23:01:15 +00001682 }
aliguoric0f4ce72009-03-05 23:01:01 +00001683 return ret;
bellardea2384d2004-08-01 21:59:26 +00001684}
1685
1686void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
1687{
bellard19cb3732006-08-19 11:45:59 +00001688 if (!bs->drv) {
bellardea2384d2004-08-01 21:59:26 +00001689 buf[0] = '\0';
1690 } else {
1691 pstrcpy(buf, buf_size, bs->drv->format_name);
1692 }
1693}
1694
ths5fafdf22007-09-16 21:08:06 +00001695void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
bellardea2384d2004-08-01 21:59:26 +00001696 void *opaque)
1697{
1698 BlockDriver *drv;
1699
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +01001700 QLIST_FOREACH(drv, &bdrv_drivers, list) {
bellardea2384d2004-08-01 21:59:26 +00001701 it(opaque, drv->format_name);
1702 }
1703}
1704
bellardb3380822004-03-14 21:38:54 +00001705BlockDriverState *bdrv_find(const char *name)
1706{
1707 BlockDriverState *bs;
1708
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001709 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1710 if (!strcmp(name, bs->device_name)) {
bellardb3380822004-03-14 21:38:54 +00001711 return bs;
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001712 }
bellardb3380822004-03-14 21:38:54 +00001713 }
1714 return NULL;
1715}
1716
Markus Armbruster2f399b02010-06-02 18:55:20 +02001717BlockDriverState *bdrv_next(BlockDriverState *bs)
1718{
1719 if (!bs) {
1720 return QTAILQ_FIRST(&bdrv_states);
1721 }
1722 return QTAILQ_NEXT(bs, list);
1723}
1724
aliguori51de9762009-03-05 23:00:43 +00001725void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
bellard81d09122004-07-14 17:21:37 +00001726{
1727 BlockDriverState *bs;
1728
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001729 QTAILQ_FOREACH(bs, &bdrv_states, list) {
aliguori51de9762009-03-05 23:00:43 +00001730 it(opaque, bs);
bellard81d09122004-07-14 17:21:37 +00001731 }
1732}
1733
bellardea2384d2004-08-01 21:59:26 +00001734const char *bdrv_get_device_name(BlockDriverState *bs)
1735{
1736 return bs->device_name;
1737}
1738
Kevin Wolf205ef792010-10-21 16:43:43 +02001739int bdrv_flush(BlockDriverState *bs)
pbrook7a6cba62006-06-04 11:39:07 +00001740{
Alexander Graf016f5cf2010-05-26 17:51:49 +02001741 if (bs->open_flags & BDRV_O_NO_FLUSH) {
Kevin Wolf205ef792010-10-21 16:43:43 +02001742 return 0;
Alexander Graf016f5cf2010-05-26 17:51:49 +02001743 }
1744
Kevin Wolfe7a8a782011-07-15 16:05:00 +02001745 if (bs->drv && bdrv_has_async_flush(bs->drv) && qemu_in_coroutine()) {
1746 return bdrv_co_flush_em(bs);
1747 }
1748
Kevin Wolf205ef792010-10-21 16:43:43 +02001749 if (bs->drv && bs->drv->bdrv_flush) {
1750 return bs->drv->bdrv_flush(bs);
1751 }
1752
1753 /*
1754 * Some block drivers always operate in either writethrough or unsafe mode
1755 * and don't support bdrv_flush therefore. Usually qemu doesn't know how
1756 * the server works (because the behaviour is hardcoded or depends on
1757 * server-side configuration), so we can't ensure that everything is safe
1758 * on disk. Returning an error doesn't work because that would break guests
1759 * even if the server operates in writethrough mode.
1760 *
1761 * Let's hope the user knows what he's doing.
1762 */
1763 return 0;
pbrook7a6cba62006-06-04 11:39:07 +00001764}
1765
aliguoric6ca28d2008-10-06 13:55:43 +00001766void bdrv_flush_all(void)
1767{
1768 BlockDriverState *bs;
1769
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001770 QTAILQ_FOREACH(bs, &bdrv_states, list) {
Markus Armbrusterc602a482011-08-03 15:08:10 +02001771 if (!bdrv_is_read_only(bs) && bdrv_is_inserted(bs)) {
aliguoric6ca28d2008-10-06 13:55:43 +00001772 bdrv_flush(bs);
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001773 }
1774 }
aliguoric6ca28d2008-10-06 13:55:43 +00001775}
1776
Kevin Wolff2feebb2010-04-14 17:30:35 +02001777int bdrv_has_zero_init(BlockDriverState *bs)
1778{
1779 assert(bs->drv);
1780
Kevin Wolf336c1c12010-07-28 11:26:29 +02001781 if (bs->drv->bdrv_has_zero_init) {
1782 return bs->drv->bdrv_has_zero_init(bs);
Kevin Wolff2feebb2010-04-14 17:30:35 +02001783 }
1784
1785 return 1;
1786}
1787
Christoph Hellwigbb8bf762010-12-16 19:36:31 +01001788int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors)
1789{
1790 if (!bs->drv) {
1791 return -ENOMEDIUM;
1792 }
1793 if (!bs->drv->bdrv_discard) {
1794 return 0;
1795 }
1796 return bs->drv->bdrv_discard(bs, sector_num, nb_sectors);
1797}
1798
thsf58c7b32008-06-05 21:53:49 +00001799/*
1800 * Returns true iff the specified sector is present in the disk image. Drivers
1801 * not implementing the functionality are assumed to not support backing files,
1802 * hence all their sectors are reported as allocated.
1803 *
1804 * 'pnum' is set to the number of sectors (including and immediately following
1805 * the specified sector) that are known to be in the same
1806 * allocated/unallocated state.
1807 *
1808 * 'nb_sectors' is the max value 'pnum' should be set to.
1809 */
1810int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1811 int *pnum)
1812{
1813 int64_t n;
1814 if (!bs->drv->bdrv_is_allocated) {
1815 if (sector_num >= bs->total_sectors) {
1816 *pnum = 0;
1817 return 0;
1818 }
1819 n = bs->total_sectors - sector_num;
1820 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1821 return 1;
1822 }
1823 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1824}
1825
Luiz Capitulino2582bfe2010-02-03 12:41:01 -02001826void bdrv_mon_event(const BlockDriverState *bdrv,
1827 BlockMonEventAction action, int is_read)
1828{
1829 QObject *data;
1830 const char *action_str;
1831
1832 switch (action) {
1833 case BDRV_ACTION_REPORT:
1834 action_str = "report";
1835 break;
1836 case BDRV_ACTION_IGNORE:
1837 action_str = "ignore";
1838 break;
1839 case BDRV_ACTION_STOP:
1840 action_str = "stop";
1841 break;
1842 default:
1843 abort();
1844 }
1845
1846 data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
1847 bdrv->device_name,
1848 action_str,
1849 is_read ? "read" : "write");
1850 monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data);
1851
1852 qobject_decref(data);
1853}
1854
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001855static void bdrv_print_dict(QObject *obj, void *opaque)
bellardb3380822004-03-14 21:38:54 +00001856{
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001857 QDict *bs_dict;
1858 Monitor *mon = opaque;
1859
1860 bs_dict = qobject_to_qdict(obj);
1861
Markus Armbrusterd8aeeb32011-05-16 15:04:55 +02001862 monitor_printf(mon, "%s: removable=%d",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001863 qdict_get_str(bs_dict, "device"),
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001864 qdict_get_bool(bs_dict, "removable"));
1865
1866 if (qdict_get_bool(bs_dict, "removable")) {
1867 monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked"));
Markus Armbrustere4def802011-09-06 18:58:53 +02001868 monitor_printf(mon, " tray-open=%d",
1869 qdict_get_bool(bs_dict, "tray-open"));
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001870 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001871 if (qdict_haskey(bs_dict, "inserted")) {
1872 QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted"));
1873
1874 monitor_printf(mon, " file=");
1875 monitor_print_filename(mon, qdict_get_str(qdict, "file"));
1876 if (qdict_haskey(qdict, "backing_file")) {
1877 monitor_printf(mon, " backing_file=");
1878 monitor_print_filename(mon, qdict_get_str(qdict, "backing_file"));
1879 }
1880 monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
1881 qdict_get_bool(qdict, "ro"),
1882 qdict_get_str(qdict, "drv"),
1883 qdict_get_bool(qdict, "encrypted"));
1884 } else {
1885 monitor_printf(mon, " [not inserted]");
1886 }
1887
1888 monitor_printf(mon, "\n");
1889}
1890
1891void bdrv_info_print(Monitor *mon, const QObject *data)
1892{
1893 qlist_iter(qobject_to_qlist(data), bdrv_print_dict, mon);
1894}
1895
Luiz Capitulinof04ef602011-09-26 17:43:54 -03001896static const char *const io_status_name[BDRV_IOS_MAX] = {
1897 [BDRV_IOS_OK] = "ok",
1898 [BDRV_IOS_FAILED] = "failed",
1899 [BDRV_IOS_ENOSPC] = "nospace",
1900};
1901
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001902void bdrv_info(Monitor *mon, QObject **ret_data)
1903{
1904 QList *bs_list;
bellardb3380822004-03-14 21:38:54 +00001905 BlockDriverState *bs;
1906
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001907 bs_list = qlist_new();
1908
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001909 QTAILQ_FOREACH(bs, &bdrv_states, list) {
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001910 QObject *bs_obj;
Markus Armbrustere4def802011-09-06 18:58:53 +02001911 QDict *bs_dict;
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001912
Markus Armbrusterd8aeeb32011-05-16 15:04:55 +02001913 bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': 'unknown', "
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001914 "'removable': %i, 'locked': %i }",
Markus Armbruster2c6942f2011-09-06 18:58:51 +02001915 bs->device_name,
1916 bdrv_dev_has_removable_media(bs),
Markus Armbrusterf1076392011-09-06 18:58:46 +02001917 bdrv_dev_is_medium_locked(bs));
Markus Armbrustere4def802011-09-06 18:58:53 +02001918 bs_dict = qobject_to_qdict(bs_obj);
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001919
Markus Armbrustere4def802011-09-06 18:58:53 +02001920 if (bdrv_dev_has_removable_media(bs)) {
1921 qdict_put(bs_dict, "tray-open",
1922 qbool_from_int(bdrv_dev_is_tray_open(bs)));
1923 }
Luiz Capitulinof04ef602011-09-26 17:43:54 -03001924
1925 if (bdrv_iostatus_is_enabled(bs)) {
1926 qdict_put(bs_dict, "io-status",
1927 qstring_from_str(io_status_name[bs->iostatus]));
1928 }
1929
bellard19cb3732006-08-19 11:45:59 +00001930 if (bs->drv) {
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001931 QObject *obj;
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001932
1933 obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, "
1934 "'encrypted': %i }",
1935 bs->filename, bs->read_only,
1936 bs->drv->format_name,
1937 bdrv_is_encrypted(bs));
thsfef30742006-12-22 14:11:32 +00001938 if (bs->backing_file[0] != '\0') {
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001939 QDict *qdict = qobject_to_qdict(obj);
1940 qdict_put(qdict, "backing_file",
1941 qstring_from_str(bs->backing_file));
aliguori376253e2009-03-05 23:01:23 +00001942 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001943
1944 qdict_put_obj(bs_dict, "inserted", obj);
bellardb3380822004-03-14 21:38:54 +00001945 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001946 qlist_append_obj(bs_list, bs_obj);
bellardb3380822004-03-14 21:38:54 +00001947 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001948
1949 *ret_data = QOBJECT(bs_list);
bellardb3380822004-03-14 21:38:54 +00001950}
thsa36e69d2007-12-02 05:18:19 +00001951
Luiz Capitulino218a5362009-12-10 17:16:07 -02001952static void bdrv_stats_iter(QObject *data, void *opaque)
thsa36e69d2007-12-02 05:18:19 +00001953{
Luiz Capitulino218a5362009-12-10 17:16:07 -02001954 QDict *qdict;
1955 Monitor *mon = opaque;
1956
1957 qdict = qobject_to_qdict(data);
1958 monitor_printf(mon, "%s:", qdict_get_str(qdict, "device"));
1959
1960 qdict = qobject_to_qdict(qdict_get(qdict, "stats"));
1961 monitor_printf(mon, " rd_bytes=%" PRId64
1962 " wr_bytes=%" PRId64
1963 " rd_operations=%" PRId64
1964 " wr_operations=%" PRId64
Christoph Hellwige8045d62011-08-22 00:25:58 +02001965 " flush_operations=%" PRId64
Christoph Hellwigc488c7f2011-08-25 08:26:10 +02001966 " wr_total_time_ns=%" PRId64
1967 " rd_total_time_ns=%" PRId64
1968 " flush_total_time_ns=%" PRId64
Luiz Capitulino218a5362009-12-10 17:16:07 -02001969 "\n",
1970 qdict_get_int(qdict, "rd_bytes"),
1971 qdict_get_int(qdict, "wr_bytes"),
1972 qdict_get_int(qdict, "rd_operations"),
Christoph Hellwige8045d62011-08-22 00:25:58 +02001973 qdict_get_int(qdict, "wr_operations"),
Christoph Hellwigc488c7f2011-08-25 08:26:10 +02001974 qdict_get_int(qdict, "flush_operations"),
1975 qdict_get_int(qdict, "wr_total_time_ns"),
1976 qdict_get_int(qdict, "rd_total_time_ns"),
1977 qdict_get_int(qdict, "flush_total_time_ns"));
Luiz Capitulino218a5362009-12-10 17:16:07 -02001978}
1979
1980void bdrv_stats_print(Monitor *mon, const QObject *data)
1981{
1982 qlist_iter(qobject_to_qlist(data), bdrv_stats_iter, mon);
1983}
1984
Kevin Wolf294cc352010-04-28 14:34:01 +02001985static QObject* bdrv_info_stats_bs(BlockDriverState *bs)
1986{
1987 QObject *res;
1988 QDict *dict;
1989
1990 res = qobject_from_jsonf("{ 'stats': {"
1991 "'rd_bytes': %" PRId64 ","
1992 "'wr_bytes': %" PRId64 ","
1993 "'rd_operations': %" PRId64 ","
1994 "'wr_operations': %" PRId64 ","
Christoph Hellwige8045d62011-08-22 00:25:58 +02001995 "'wr_highest_offset': %" PRId64 ","
Christoph Hellwigc488c7f2011-08-25 08:26:10 +02001996 "'flush_operations': %" PRId64 ","
1997 "'wr_total_time_ns': %" PRId64 ","
1998 "'rd_total_time_ns': %" PRId64 ","
1999 "'flush_total_time_ns': %" PRId64
Kevin Wolf294cc352010-04-28 14:34:01 +02002000 "} }",
Christoph Hellwiga597e792011-08-25 08:26:01 +02002001 bs->nr_bytes[BDRV_ACCT_READ],
2002 bs->nr_bytes[BDRV_ACCT_WRITE],
2003 bs->nr_ops[BDRV_ACCT_READ],
2004 bs->nr_ops[BDRV_ACCT_WRITE],
Blue Swirl5ffbbc62010-06-14 18:55:33 +00002005 bs->wr_highest_sector *
Christoph Hellwige8045d62011-08-22 00:25:58 +02002006 (uint64_t)BDRV_SECTOR_SIZE,
Christoph Hellwigc488c7f2011-08-25 08:26:10 +02002007 bs->nr_ops[BDRV_ACCT_FLUSH],
2008 bs->total_time_ns[BDRV_ACCT_WRITE],
2009 bs->total_time_ns[BDRV_ACCT_READ],
2010 bs->total_time_ns[BDRV_ACCT_FLUSH]);
Kevin Wolf294cc352010-04-28 14:34:01 +02002011 dict = qobject_to_qdict(res);
2012
2013 if (*bs->device_name) {
2014 qdict_put(dict, "device", qstring_from_str(bs->device_name));
2015 }
2016
2017 if (bs->file) {
2018 QObject *parent = bdrv_info_stats_bs(bs->file);
2019 qdict_put_obj(dict, "parent", parent);
2020 }
2021
2022 return res;
2023}
2024
Luiz Capitulino218a5362009-12-10 17:16:07 -02002025void bdrv_info_stats(Monitor *mon, QObject **ret_data)
2026{
2027 QObject *obj;
2028 QList *devices;
thsa36e69d2007-12-02 05:18:19 +00002029 BlockDriverState *bs;
2030
Luiz Capitulino218a5362009-12-10 17:16:07 -02002031 devices = qlist_new();
2032
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01002033 QTAILQ_FOREACH(bs, &bdrv_states, list) {
Kevin Wolf294cc352010-04-28 14:34:01 +02002034 obj = bdrv_info_stats_bs(bs);
Luiz Capitulino218a5362009-12-10 17:16:07 -02002035 qlist_append_obj(devices, obj);
thsa36e69d2007-12-02 05:18:19 +00002036 }
Luiz Capitulino218a5362009-12-10 17:16:07 -02002037
2038 *ret_data = QOBJECT(devices);
thsa36e69d2007-12-02 05:18:19 +00002039}
bellardea2384d2004-08-01 21:59:26 +00002040
aliguori045df332009-03-05 23:00:48 +00002041const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
2042{
2043 if (bs->backing_hd && bs->backing_hd->encrypted)
2044 return bs->backing_file;
2045 else if (bs->encrypted)
2046 return bs->filename;
2047 else
2048 return NULL;
2049}
2050
ths5fafdf22007-09-16 21:08:06 +00002051void bdrv_get_backing_filename(BlockDriverState *bs,
bellard83f64092006-08-01 16:21:11 +00002052 char *filename, int filename_size)
bellardea2384d2004-08-01 21:59:26 +00002053{
Kevin Wolfb783e402010-01-12 12:55:16 +01002054 if (!bs->backing_file) {
bellard83f64092006-08-01 16:21:11 +00002055 pstrcpy(filename, filename_size, "");
2056 } else {
2057 pstrcpy(filename, filename_size, bs->backing_file);
2058 }
bellardea2384d2004-08-01 21:59:26 +00002059}
2060
ths5fafdf22007-09-16 21:08:06 +00002061int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
bellardfaea38e2006-08-05 21:31:00 +00002062 const uint8_t *buf, int nb_sectors)
2063{
2064 BlockDriver *drv = bs->drv;
2065 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002066 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00002067 if (!drv->bdrv_write_compressed)
2068 return -ENOTSUP;
Kevin Wolffbb7b4e2009-05-08 14:47:24 +02002069 if (bdrv_check_request(bs, sector_num, nb_sectors))
2070 return -EIO;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002071
Jan Kiszkac6d22832009-11-30 18:21:20 +01002072 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002073 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
2074 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002075
bellardfaea38e2006-08-05 21:31:00 +00002076 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
2077}
ths3b46e622007-09-17 08:09:54 +00002078
bellardfaea38e2006-08-05 21:31:00 +00002079int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
2080{
2081 BlockDriver *drv = bs->drv;
2082 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002083 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00002084 if (!drv->bdrv_get_info)
2085 return -ENOTSUP;
2086 memset(bdi, 0, sizeof(*bdi));
2087 return drv->bdrv_get_info(bs, bdi);
2088}
2089
Christoph Hellwig45566e92009-07-10 23:11:57 +02002090int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
2091 int64_t pos, int size)
aliguori178e08a2009-04-05 19:10:55 +00002092{
2093 BlockDriver *drv = bs->drv;
2094 if (!drv)
2095 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09002096 if (drv->bdrv_save_vmstate)
2097 return drv->bdrv_save_vmstate(bs, buf, pos, size);
2098 if (bs->file)
2099 return bdrv_save_vmstate(bs->file, buf, pos, size);
2100 return -ENOTSUP;
aliguori178e08a2009-04-05 19:10:55 +00002101}
2102
Christoph Hellwig45566e92009-07-10 23:11:57 +02002103int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
2104 int64_t pos, int size)
aliguori178e08a2009-04-05 19:10:55 +00002105{
2106 BlockDriver *drv = bs->drv;
2107 if (!drv)
2108 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09002109 if (drv->bdrv_load_vmstate)
2110 return drv->bdrv_load_vmstate(bs, buf, pos, size);
2111 if (bs->file)
2112 return bdrv_load_vmstate(bs->file, buf, pos, size);
2113 return -ENOTSUP;
aliguori178e08a2009-04-05 19:10:55 +00002114}
2115
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01002116void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
2117{
2118 BlockDriver *drv = bs->drv;
2119
2120 if (!drv || !drv->bdrv_debug_event) {
2121 return;
2122 }
2123
2124 return drv->bdrv_debug_event(bs, event);
2125
2126}
2127
bellardfaea38e2006-08-05 21:31:00 +00002128/**************************************************************/
2129/* handling of snapshots */
2130
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002131int bdrv_can_snapshot(BlockDriverState *bs)
2132{
2133 BlockDriver *drv = bs->drv;
Markus Armbruster07b70bf2011-08-03 15:08:11 +02002134 if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002135 return 0;
2136 }
2137
2138 if (!drv->bdrv_snapshot_create) {
2139 if (bs->file != NULL) {
2140 return bdrv_can_snapshot(bs->file);
2141 }
2142 return 0;
2143 }
2144
2145 return 1;
2146}
2147
Blue Swirl199630b2010-07-25 20:49:34 +00002148int bdrv_is_snapshot(BlockDriverState *bs)
2149{
2150 return !!(bs->open_flags & BDRV_O_SNAPSHOT);
2151}
2152
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002153BlockDriverState *bdrv_snapshots(void)
2154{
2155 BlockDriverState *bs;
2156
Markus Armbruster3ac906f2010-07-01 09:30:38 +02002157 if (bs_snapshots) {
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002158 return bs_snapshots;
Markus Armbruster3ac906f2010-07-01 09:30:38 +02002159 }
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002160
2161 bs = NULL;
2162 while ((bs = bdrv_next(bs))) {
2163 if (bdrv_can_snapshot(bs)) {
Markus Armbruster3ac906f2010-07-01 09:30:38 +02002164 bs_snapshots = bs;
2165 return bs;
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002166 }
2167 }
2168 return NULL;
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002169}
2170
ths5fafdf22007-09-16 21:08:06 +00002171int bdrv_snapshot_create(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00002172 QEMUSnapshotInfo *sn_info)
2173{
2174 BlockDriver *drv = bs->drv;
2175 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002176 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09002177 if (drv->bdrv_snapshot_create)
2178 return drv->bdrv_snapshot_create(bs, sn_info);
2179 if (bs->file)
2180 return bdrv_snapshot_create(bs->file, sn_info);
2181 return -ENOTSUP;
bellardfaea38e2006-08-05 21:31:00 +00002182}
2183
ths5fafdf22007-09-16 21:08:06 +00002184int bdrv_snapshot_goto(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00002185 const char *snapshot_id)
2186{
2187 BlockDriver *drv = bs->drv;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09002188 int ret, open_ret;
2189
bellardfaea38e2006-08-05 21:31:00 +00002190 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002191 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09002192 if (drv->bdrv_snapshot_goto)
2193 return drv->bdrv_snapshot_goto(bs, snapshot_id);
2194
2195 if (bs->file) {
2196 drv->bdrv_close(bs);
2197 ret = bdrv_snapshot_goto(bs->file, snapshot_id);
2198 open_ret = drv->bdrv_open(bs, bs->open_flags);
2199 if (open_ret < 0) {
2200 bdrv_delete(bs->file);
2201 bs->drv = NULL;
2202 return open_ret;
2203 }
2204 return ret;
2205 }
2206
2207 return -ENOTSUP;
bellardfaea38e2006-08-05 21:31:00 +00002208}
2209
2210int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
2211{
2212 BlockDriver *drv = bs->drv;
2213 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002214 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09002215 if (drv->bdrv_snapshot_delete)
2216 return drv->bdrv_snapshot_delete(bs, snapshot_id);
2217 if (bs->file)
2218 return bdrv_snapshot_delete(bs->file, snapshot_id);
2219 return -ENOTSUP;
bellardfaea38e2006-08-05 21:31:00 +00002220}
2221
ths5fafdf22007-09-16 21:08:06 +00002222int bdrv_snapshot_list(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00002223 QEMUSnapshotInfo **psn_info)
2224{
2225 BlockDriver *drv = bs->drv;
2226 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002227 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09002228 if (drv->bdrv_snapshot_list)
2229 return drv->bdrv_snapshot_list(bs, psn_info);
2230 if (bs->file)
2231 return bdrv_snapshot_list(bs->file, psn_info);
2232 return -ENOTSUP;
bellardfaea38e2006-08-05 21:31:00 +00002233}
2234
edison51ef6722010-09-21 19:58:41 -07002235int bdrv_snapshot_load_tmp(BlockDriverState *bs,
2236 const char *snapshot_name)
2237{
2238 BlockDriver *drv = bs->drv;
2239 if (!drv) {
2240 return -ENOMEDIUM;
2241 }
2242 if (!bs->read_only) {
2243 return -EINVAL;
2244 }
2245 if (drv->bdrv_snapshot_load_tmp) {
2246 return drv->bdrv_snapshot_load_tmp(bs, snapshot_name);
2247 }
2248 return -ENOTSUP;
2249}
2250
bellardfaea38e2006-08-05 21:31:00 +00002251#define NB_SUFFIXES 4
2252
2253char *get_human_readable_size(char *buf, int buf_size, int64_t size)
2254{
2255 static const char suffixes[NB_SUFFIXES] = "KMGT";
2256 int64_t base;
2257 int i;
2258
2259 if (size <= 999) {
2260 snprintf(buf, buf_size, "%" PRId64, size);
2261 } else {
2262 base = 1024;
2263 for(i = 0; i < NB_SUFFIXES; i++) {
2264 if (size < (10 * base)) {
ths5fafdf22007-09-16 21:08:06 +00002265 snprintf(buf, buf_size, "%0.1f%c",
bellardfaea38e2006-08-05 21:31:00 +00002266 (double)size / base,
2267 suffixes[i]);
2268 break;
2269 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
ths5fafdf22007-09-16 21:08:06 +00002270 snprintf(buf, buf_size, "%" PRId64 "%c",
bellardfaea38e2006-08-05 21:31:00 +00002271 ((size + (base >> 1)) / base),
2272 suffixes[i]);
2273 break;
2274 }
2275 base = base * 1024;
2276 }
2277 }
2278 return buf;
2279}
2280
2281char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
2282{
2283 char buf1[128], date_buf[128], clock_buf[128];
bellard3b9f94e2007-01-07 17:27:07 +00002284#ifdef _WIN32
2285 struct tm *ptm;
2286#else
bellardfaea38e2006-08-05 21:31:00 +00002287 struct tm tm;
bellard3b9f94e2007-01-07 17:27:07 +00002288#endif
bellardfaea38e2006-08-05 21:31:00 +00002289 time_t ti;
2290 int64_t secs;
2291
2292 if (!sn) {
ths5fafdf22007-09-16 21:08:06 +00002293 snprintf(buf, buf_size,
2294 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00002295 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
2296 } else {
2297 ti = sn->date_sec;
bellard3b9f94e2007-01-07 17:27:07 +00002298#ifdef _WIN32
2299 ptm = localtime(&ti);
2300 strftime(date_buf, sizeof(date_buf),
2301 "%Y-%m-%d %H:%M:%S", ptm);
2302#else
bellardfaea38e2006-08-05 21:31:00 +00002303 localtime_r(&ti, &tm);
2304 strftime(date_buf, sizeof(date_buf),
2305 "%Y-%m-%d %H:%M:%S", &tm);
bellard3b9f94e2007-01-07 17:27:07 +00002306#endif
bellardfaea38e2006-08-05 21:31:00 +00002307 secs = sn->vm_clock_nsec / 1000000000;
2308 snprintf(clock_buf, sizeof(clock_buf),
2309 "%02d:%02d:%02d.%03d",
2310 (int)(secs / 3600),
2311 (int)((secs / 60) % 60),
ths5fafdf22007-09-16 21:08:06 +00002312 (int)(secs % 60),
bellardfaea38e2006-08-05 21:31:00 +00002313 (int)((sn->vm_clock_nsec / 1000000) % 1000));
2314 snprintf(buf, buf_size,
ths5fafdf22007-09-16 21:08:06 +00002315 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00002316 sn->id_str, sn->name,
2317 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
2318 date_buf,
2319 clock_buf);
2320 }
2321 return buf;
2322}
2323
bellard83f64092006-08-01 16:21:11 +00002324/**************************************************************/
2325/* async I/Os */
2326
aliguori3b69e4b2009-01-22 16:59:24 +00002327BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
aliguorif141eaf2009-04-07 18:43:24 +00002328 QEMUIOVector *qiov, int nb_sectors,
aliguori3b69e4b2009-01-22 16:59:24 +00002329 BlockDriverCompletionFunc *cb, void *opaque)
2330{
bellard83f64092006-08-01 16:21:11 +00002331 BlockDriver *drv = bs->drv;
bellard83f64092006-08-01 16:21:11 +00002332
Stefan Hajnoczibbf0a442010-10-05 14:28:53 +01002333 trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque);
2334
bellard19cb3732006-08-19 11:45:59 +00002335 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00002336 return NULL;
aliguori71d07702009-03-03 17:37:16 +00002337 if (bdrv_check_request(bs, sector_num, nb_sectors))
2338 return NULL;
ths3b46e622007-09-17 08:09:54 +00002339
Christoph Hellwiga597e792011-08-25 08:26:01 +02002340 return drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
2341 cb, opaque);
bellard83f64092006-08-01 16:21:11 +00002342}
2343
Marcelo Tosatti4dcafbb2010-11-08 17:02:55 -02002344typedef struct BlockCompleteData {
2345 BlockDriverCompletionFunc *cb;
2346 void *opaque;
2347 BlockDriverState *bs;
2348 int64_t sector_num;
2349 int nb_sectors;
2350} BlockCompleteData;
2351
2352static void block_complete_cb(void *opaque, int ret)
2353{
2354 BlockCompleteData *b = opaque;
2355
2356 if (b->bs->dirty_bitmap) {
2357 set_dirty_bitmap(b->bs, b->sector_num, b->nb_sectors, 1);
2358 }
2359 b->cb(b->opaque, ret);
Anthony Liguori7267c092011-08-20 22:09:37 -05002360 g_free(b);
Marcelo Tosatti4dcafbb2010-11-08 17:02:55 -02002361}
2362
2363static BlockCompleteData *blk_dirty_cb_alloc(BlockDriverState *bs,
2364 int64_t sector_num,
2365 int nb_sectors,
2366 BlockDriverCompletionFunc *cb,
2367 void *opaque)
2368{
Anthony Liguori7267c092011-08-20 22:09:37 -05002369 BlockCompleteData *blkdata = g_malloc0(sizeof(BlockCompleteData));
Marcelo Tosatti4dcafbb2010-11-08 17:02:55 -02002370
2371 blkdata->bs = bs;
2372 blkdata->cb = cb;
2373 blkdata->opaque = opaque;
2374 blkdata->sector_num = sector_num;
2375 blkdata->nb_sectors = nb_sectors;
2376
2377 return blkdata;
2378}
2379
aliguorif141eaf2009-04-07 18:43:24 +00002380BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
2381 QEMUIOVector *qiov, int nb_sectors,
2382 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00002383{
bellard83f64092006-08-01 16:21:11 +00002384 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00002385 BlockDriverAIOCB *ret;
Marcelo Tosatti4dcafbb2010-11-08 17:02:55 -02002386 BlockCompleteData *blk_cb_data;
bellard83f64092006-08-01 16:21:11 +00002387
Stefan Hajnoczibbf0a442010-10-05 14:28:53 +01002388 trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque);
2389
bellard19cb3732006-08-19 11:45:59 +00002390 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00002391 return NULL;
bellard83f64092006-08-01 16:21:11 +00002392 if (bs->read_only)
pbrookce1a14d2006-08-07 02:38:06 +00002393 return NULL;
aliguori71d07702009-03-03 17:37:16 +00002394 if (bdrv_check_request(bs, sector_num, nb_sectors))
2395 return NULL;
bellard83f64092006-08-01 16:21:11 +00002396
Jan Kiszkac6d22832009-11-30 18:21:20 +01002397 if (bs->dirty_bitmap) {
Marcelo Tosatti4dcafbb2010-11-08 17:02:55 -02002398 blk_cb_data = blk_dirty_cb_alloc(bs, sector_num, nb_sectors, cb,
2399 opaque);
2400 cb = &block_complete_cb;
2401 opaque = blk_cb_data;
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002402 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002403
aliguorif141eaf2009-04-07 18:43:24 +00002404 ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
2405 cb, opaque);
thsa36e69d2007-12-02 05:18:19 +00002406
2407 if (ret) {
Kevin Wolf294cc352010-04-28 14:34:01 +02002408 if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
2409 bs->wr_highest_sector = sector_num + nb_sectors - 1;
2410 }
thsa36e69d2007-12-02 05:18:19 +00002411 }
2412
2413 return ret;
bellard83f64092006-08-01 16:21:11 +00002414}
2415
Kevin Wolf40b4f532009-09-09 17:53:37 +02002416
2417typedef struct MultiwriteCB {
2418 int error;
2419 int num_requests;
2420 int num_callbacks;
2421 struct {
2422 BlockDriverCompletionFunc *cb;
2423 void *opaque;
2424 QEMUIOVector *free_qiov;
2425 void *free_buf;
2426 } callbacks[];
2427} MultiwriteCB;
2428
2429static void multiwrite_user_cb(MultiwriteCB *mcb)
2430{
2431 int i;
2432
2433 for (i = 0; i < mcb->num_callbacks; i++) {
2434 mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
Stefan Hajnoczi1e1ea482010-04-21 20:35:45 +01002435 if (mcb->callbacks[i].free_qiov) {
2436 qemu_iovec_destroy(mcb->callbacks[i].free_qiov);
2437 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002438 g_free(mcb->callbacks[i].free_qiov);
Herve Poussineauf8a83242010-01-24 21:23:56 +00002439 qemu_vfree(mcb->callbacks[i].free_buf);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002440 }
2441}
2442
2443static void multiwrite_cb(void *opaque, int ret)
2444{
2445 MultiwriteCB *mcb = opaque;
2446
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +01002447 trace_multiwrite_cb(mcb, ret);
2448
Kevin Wolfcb6d3ca2010-04-01 22:48:44 +02002449 if (ret < 0 && !mcb->error) {
Kevin Wolf40b4f532009-09-09 17:53:37 +02002450 mcb->error = ret;
Kevin Wolf40b4f532009-09-09 17:53:37 +02002451 }
2452
2453 mcb->num_requests--;
2454 if (mcb->num_requests == 0) {
Kevin Wolfde189a12010-07-01 16:08:51 +02002455 multiwrite_user_cb(mcb);
Anthony Liguori7267c092011-08-20 22:09:37 -05002456 g_free(mcb);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002457 }
2458}
2459
2460static int multiwrite_req_compare(const void *a, const void *b)
2461{
Christoph Hellwig77be4362010-05-19 20:53:10 +02002462 const BlockRequest *req1 = a, *req2 = b;
2463
2464 /*
2465 * Note that we can't simply subtract req2->sector from req1->sector
2466 * here as that could overflow the return value.
2467 */
2468 if (req1->sector > req2->sector) {
2469 return 1;
2470 } else if (req1->sector < req2->sector) {
2471 return -1;
2472 } else {
2473 return 0;
2474 }
Kevin Wolf40b4f532009-09-09 17:53:37 +02002475}
2476
2477/*
2478 * Takes a bunch of requests and tries to merge them. Returns the number of
2479 * requests that remain after merging.
2480 */
2481static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
2482 int num_reqs, MultiwriteCB *mcb)
2483{
2484 int i, outidx;
2485
2486 // Sort requests by start sector
2487 qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
2488
2489 // Check if adjacent requests touch the same clusters. If so, combine them,
2490 // filling up gaps with zero sectors.
2491 outidx = 0;
2492 for (i = 1; i < num_reqs; i++) {
2493 int merge = 0;
2494 int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
2495
2496 // This handles the cases that are valid for all block drivers, namely
2497 // exactly sequential writes and overlapping writes.
2498 if (reqs[i].sector <= oldreq_last) {
2499 merge = 1;
2500 }
2501
2502 // The block driver may decide that it makes sense to combine requests
2503 // even if there is a gap of some sectors between them. In this case,
2504 // the gap is filled with zeros (therefore only applicable for yet
2505 // unused space in format like qcow2).
2506 if (!merge && bs->drv->bdrv_merge_requests) {
2507 merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]);
2508 }
2509
Christoph Hellwige2a305f2010-01-26 14:49:08 +01002510 if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) {
2511 merge = 0;
2512 }
2513
Kevin Wolf40b4f532009-09-09 17:53:37 +02002514 if (merge) {
2515 size_t size;
Anthony Liguori7267c092011-08-20 22:09:37 -05002516 QEMUIOVector *qiov = g_malloc0(sizeof(*qiov));
Kevin Wolf40b4f532009-09-09 17:53:37 +02002517 qemu_iovec_init(qiov,
2518 reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
2519
2520 // Add the first request to the merged one. If the requests are
2521 // overlapping, drop the last sectors of the first request.
2522 size = (reqs[i].sector - reqs[outidx].sector) << 9;
2523 qemu_iovec_concat(qiov, reqs[outidx].qiov, size);
2524
2525 // We might need to add some zeros between the two requests
2526 if (reqs[i].sector > oldreq_last) {
2527 size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
2528 uint8_t *buf = qemu_blockalign(bs, zero_bytes);
2529 memset(buf, 0, zero_bytes);
2530 qemu_iovec_add(qiov, buf, zero_bytes);
2531 mcb->callbacks[i].free_buf = buf;
2532 }
2533
2534 // Add the second request
2535 qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size);
2536
Kevin Wolfcbf1dff2010-05-21 11:09:42 +02002537 reqs[outidx].nb_sectors = qiov->size >> 9;
Kevin Wolf40b4f532009-09-09 17:53:37 +02002538 reqs[outidx].qiov = qiov;
2539
2540 mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
2541 } else {
2542 outidx++;
2543 reqs[outidx].sector = reqs[i].sector;
2544 reqs[outidx].nb_sectors = reqs[i].nb_sectors;
2545 reqs[outidx].qiov = reqs[i].qiov;
2546 }
2547 }
2548
2549 return outidx + 1;
2550}
2551
2552/*
2553 * Submit multiple AIO write requests at once.
2554 *
2555 * On success, the function returns 0 and all requests in the reqs array have
2556 * been submitted. In error case this function returns -1, and any of the
2557 * requests may or may not be submitted yet. In particular, this means that the
2558 * callback will be called for some of the requests, for others it won't. The
2559 * caller must check the error field of the BlockRequest to wait for the right
2560 * callbacks (if error != 0, no callback will be called).
2561 *
2562 * The implementation may modify the contents of the reqs array, e.g. to merge
2563 * requests. However, the fields opaque and error are left unmodified as they
2564 * are used to signal failure for a single request to the caller.
2565 */
2566int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
2567{
2568 BlockDriverAIOCB *acb;
2569 MultiwriteCB *mcb;
2570 int i;
2571
Ryan Harper301db7c2011-03-07 10:01:04 -06002572 /* don't submit writes if we don't have a medium */
2573 if (bs->drv == NULL) {
2574 for (i = 0; i < num_reqs; i++) {
2575 reqs[i].error = -ENOMEDIUM;
2576 }
2577 return -1;
2578 }
2579
Kevin Wolf40b4f532009-09-09 17:53:37 +02002580 if (num_reqs == 0) {
2581 return 0;
2582 }
2583
2584 // Create MultiwriteCB structure
Anthony Liguori7267c092011-08-20 22:09:37 -05002585 mcb = g_malloc0(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
Kevin Wolf40b4f532009-09-09 17:53:37 +02002586 mcb->num_requests = 0;
2587 mcb->num_callbacks = num_reqs;
2588
2589 for (i = 0; i < num_reqs; i++) {
2590 mcb->callbacks[i].cb = reqs[i].cb;
2591 mcb->callbacks[i].opaque = reqs[i].opaque;
2592 }
2593
2594 // Check for mergable requests
2595 num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
2596
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +01002597 trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs);
2598
Kevin Wolf453f9a12010-07-02 14:01:21 +02002599 /*
2600 * Run the aio requests. As soon as one request can't be submitted
2601 * successfully, fail all requests that are not yet submitted (we must
2602 * return failure for all requests anyway)
2603 *
2604 * num_requests cannot be set to the right value immediately: If
2605 * bdrv_aio_writev fails for some request, num_requests would be too high
2606 * and therefore multiwrite_cb() would never recognize the multiwrite
2607 * request as completed. We also cannot use the loop variable i to set it
2608 * when the first request fails because the callback may already have been
2609 * called for previously submitted requests. Thus, num_requests must be
2610 * incremented for each request that is submitted.
2611 *
2612 * The problem that callbacks may be called early also means that we need
2613 * to take care that num_requests doesn't become 0 before all requests are
2614 * submitted - multiwrite_cb() would consider the multiwrite request
2615 * completed. A dummy request that is "completed" by a manual call to
2616 * multiwrite_cb() takes care of this.
2617 */
2618 mcb->num_requests = 1;
2619
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +01002620 // Run the aio requests
Kevin Wolf40b4f532009-09-09 17:53:37 +02002621 for (i = 0; i < num_reqs; i++) {
Kevin Wolf453f9a12010-07-02 14:01:21 +02002622 mcb->num_requests++;
Kevin Wolf40b4f532009-09-09 17:53:37 +02002623 acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
2624 reqs[i].nb_sectors, multiwrite_cb, mcb);
2625
2626 if (acb == NULL) {
2627 // We can only fail the whole thing if no request has been
2628 // submitted yet. Otherwise we'll wait for the submitted AIOs to
2629 // complete and report the error in the callback.
Kevin Wolf453f9a12010-07-02 14:01:21 +02002630 if (i == 0) {
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +01002631 trace_bdrv_aio_multiwrite_earlyfail(mcb);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002632 goto fail;
2633 } else {
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +01002634 trace_bdrv_aio_multiwrite_latefail(mcb, i);
Kevin Wolf7eb58a62010-04-06 18:24:07 +02002635 multiwrite_cb(mcb, -EIO);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002636 break;
2637 }
Kevin Wolf40b4f532009-09-09 17:53:37 +02002638 }
2639 }
2640
Kevin Wolf453f9a12010-07-02 14:01:21 +02002641 /* Complete the dummy request */
2642 multiwrite_cb(mcb, 0);
2643
Kevin Wolf40b4f532009-09-09 17:53:37 +02002644 return 0;
2645
2646fail:
Kevin Wolf453f9a12010-07-02 14:01:21 +02002647 for (i = 0; i < mcb->num_callbacks; i++) {
2648 reqs[i].error = -EIO;
2649 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002650 g_free(mcb);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002651 return -1;
2652}
2653
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002654BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
2655 BlockDriverCompletionFunc *cb, void *opaque)
2656{
2657 BlockDriver *drv = bs->drv;
2658
Stefan Hajnoczia13aac02011-03-07 07:58:04 +00002659 trace_bdrv_aio_flush(bs, opaque);
2660
Alexander Graf016f5cf2010-05-26 17:51:49 +02002661 if (bs->open_flags & BDRV_O_NO_FLUSH) {
2662 return bdrv_aio_noop_em(bs, cb, opaque);
2663 }
2664
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002665 if (!drv)
2666 return NULL;
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002667 return drv->bdrv_aio_flush(bs, cb, opaque);
2668}
2669
bellard83f64092006-08-01 16:21:11 +00002670void bdrv_aio_cancel(BlockDriverAIOCB *acb)
pbrookce1a14d2006-08-07 02:38:06 +00002671{
aliguori6bbff9a2009-03-20 18:25:59 +00002672 acb->pool->cancel(acb);
bellard83f64092006-08-01 16:21:11 +00002673}
2674
pbrookce1a14d2006-08-07 02:38:06 +00002675
bellard83f64092006-08-01 16:21:11 +00002676/**************************************************************/
2677/* async block device emulation */
2678
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002679typedef struct BlockDriverAIOCBSync {
2680 BlockDriverAIOCB common;
2681 QEMUBH *bh;
2682 int ret;
2683 /* vector translation state */
2684 QEMUIOVector *qiov;
2685 uint8_t *bounce;
2686 int is_write;
2687} BlockDriverAIOCBSync;
2688
2689static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
2690{
Kevin Wolfb666d232010-05-05 11:44:39 +02002691 BlockDriverAIOCBSync *acb =
2692 container_of(blockacb, BlockDriverAIOCBSync, common);
Dor Laor6a7ad292009-06-01 12:07:23 +03002693 qemu_bh_delete(acb->bh);
Avi Kivity36afc452009-06-23 16:20:36 +03002694 acb->bh = NULL;
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002695 qemu_aio_release(acb);
2696}
2697
2698static AIOPool bdrv_em_aio_pool = {
2699 .aiocb_size = sizeof(BlockDriverAIOCBSync),
2700 .cancel = bdrv_aio_cancel_em,
2701};
2702
bellard83f64092006-08-01 16:21:11 +00002703static void bdrv_aio_bh_cb(void *opaque)
bellardbeac80c2006-06-26 20:08:57 +00002704{
pbrookce1a14d2006-08-07 02:38:06 +00002705 BlockDriverAIOCBSync *acb = opaque;
aliguorif141eaf2009-04-07 18:43:24 +00002706
aliguorif141eaf2009-04-07 18:43:24 +00002707 if (!acb->is_write)
2708 qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
aliguoriceb42de2009-04-07 18:43:28 +00002709 qemu_vfree(acb->bounce);
pbrookce1a14d2006-08-07 02:38:06 +00002710 acb->common.cb(acb->common.opaque, acb->ret);
Dor Laor6a7ad292009-06-01 12:07:23 +03002711 qemu_bh_delete(acb->bh);
Avi Kivity36afc452009-06-23 16:20:36 +03002712 acb->bh = NULL;
pbrookce1a14d2006-08-07 02:38:06 +00002713 qemu_aio_release(acb);
bellardbeac80c2006-06-26 20:08:57 +00002714}
bellardbeac80c2006-06-26 20:08:57 +00002715
aliguorif141eaf2009-04-07 18:43:24 +00002716static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
2717 int64_t sector_num,
2718 QEMUIOVector *qiov,
2719 int nb_sectors,
2720 BlockDriverCompletionFunc *cb,
2721 void *opaque,
2722 int is_write)
2723
bellardea2384d2004-08-01 21:59:26 +00002724{
pbrookce1a14d2006-08-07 02:38:06 +00002725 BlockDriverAIOCBSync *acb;
pbrookce1a14d2006-08-07 02:38:06 +00002726
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002727 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
aliguorif141eaf2009-04-07 18:43:24 +00002728 acb->is_write = is_write;
2729 acb->qiov = qiov;
aliguorie268ca52009-04-22 20:20:00 +00002730 acb->bounce = qemu_blockalign(bs, qiov->size);
aliguorif141eaf2009-04-07 18:43:24 +00002731
pbrookce1a14d2006-08-07 02:38:06 +00002732 if (!acb->bh)
2733 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
aliguorif141eaf2009-04-07 18:43:24 +00002734
2735 if (is_write) {
2736 qemu_iovec_to_buffer(acb->qiov, acb->bounce);
2737 acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
2738 } else {
2739 acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
2740 }
2741
pbrookce1a14d2006-08-07 02:38:06 +00002742 qemu_bh_schedule(acb->bh);
aliguorif141eaf2009-04-07 18:43:24 +00002743
pbrookce1a14d2006-08-07 02:38:06 +00002744 return &acb->common;
pbrook7a6cba62006-06-04 11:39:07 +00002745}
2746
aliguorif141eaf2009-04-07 18:43:24 +00002747static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
2748 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
pbrookce1a14d2006-08-07 02:38:06 +00002749 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00002750{
aliguorif141eaf2009-04-07 18:43:24 +00002751 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
bellard83f64092006-08-01 16:21:11 +00002752}
2753
aliguorif141eaf2009-04-07 18:43:24 +00002754static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
2755 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2756 BlockDriverCompletionFunc *cb, void *opaque)
2757{
2758 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
2759}
2760
Kevin Wolf68485422011-06-30 10:05:46 +02002761
2762typedef struct BlockDriverAIOCBCoroutine {
2763 BlockDriverAIOCB common;
2764 BlockRequest req;
2765 bool is_write;
2766 QEMUBH* bh;
2767} BlockDriverAIOCBCoroutine;
2768
2769static void bdrv_aio_co_cancel_em(BlockDriverAIOCB *blockacb)
2770{
2771 qemu_aio_flush();
2772}
2773
2774static AIOPool bdrv_em_co_aio_pool = {
2775 .aiocb_size = sizeof(BlockDriverAIOCBCoroutine),
2776 .cancel = bdrv_aio_co_cancel_em,
2777};
2778
2779static void bdrv_co_rw_bh(void *opaque)
2780{
2781 BlockDriverAIOCBCoroutine *acb = opaque;
2782
2783 acb->common.cb(acb->common.opaque, acb->req.error);
2784 qemu_bh_delete(acb->bh);
2785 qemu_aio_release(acb);
2786}
2787
2788static void coroutine_fn bdrv_co_rw(void *opaque)
2789{
2790 BlockDriverAIOCBCoroutine *acb = opaque;
2791 BlockDriverState *bs = acb->common.bs;
2792
2793 if (!acb->is_write) {
2794 acb->req.error = bs->drv->bdrv_co_readv(bs, acb->req.sector,
2795 acb->req.nb_sectors, acb->req.qiov);
2796 } else {
2797 acb->req.error = bs->drv->bdrv_co_writev(bs, acb->req.sector,
2798 acb->req.nb_sectors, acb->req.qiov);
2799 }
2800
2801 acb->bh = qemu_bh_new(bdrv_co_rw_bh, acb);
2802 qemu_bh_schedule(acb->bh);
2803}
2804
2805static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs,
2806 int64_t sector_num,
2807 QEMUIOVector *qiov,
2808 int nb_sectors,
2809 BlockDriverCompletionFunc *cb,
2810 void *opaque,
2811 bool is_write)
2812{
2813 Coroutine *co;
2814 BlockDriverAIOCBCoroutine *acb;
2815
2816 acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque);
2817 acb->req.sector = sector_num;
2818 acb->req.nb_sectors = nb_sectors;
2819 acb->req.qiov = qiov;
2820 acb->is_write = is_write;
2821
2822 co = qemu_coroutine_create(bdrv_co_rw);
2823 qemu_coroutine_enter(co, acb);
2824
2825 return &acb->common;
2826}
2827
2828static BlockDriverAIOCB *bdrv_co_aio_readv_em(BlockDriverState *bs,
2829 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2830 BlockDriverCompletionFunc *cb, void *opaque)
2831{
2832 return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque,
2833 false);
2834}
2835
2836static BlockDriverAIOCB *bdrv_co_aio_writev_em(BlockDriverState *bs,
2837 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2838 BlockDriverCompletionFunc *cb, void *opaque)
2839{
2840 return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque,
2841 true);
2842}
2843
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002844static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
2845 BlockDriverCompletionFunc *cb, void *opaque)
2846{
2847 BlockDriverAIOCBSync *acb;
2848
2849 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2850 acb->is_write = 1; /* don't bounce in the completion hadler */
2851 acb->qiov = NULL;
2852 acb->bounce = NULL;
2853 acb->ret = 0;
2854
2855 if (!acb->bh)
2856 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2857
2858 bdrv_flush(bs);
2859 qemu_bh_schedule(acb->bh);
2860 return &acb->common;
2861}
2862
Alexander Graf016f5cf2010-05-26 17:51:49 +02002863static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,
2864 BlockDriverCompletionFunc *cb, void *opaque)
2865{
2866 BlockDriverAIOCBSync *acb;
2867
2868 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2869 acb->is_write = 1; /* don't bounce in the completion handler */
2870 acb->qiov = NULL;
2871 acb->bounce = NULL;
2872 acb->ret = 0;
2873
2874 if (!acb->bh) {
2875 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2876 }
2877
2878 qemu_bh_schedule(acb->bh);
2879 return &acb->common;
2880}
2881
bellard83f64092006-08-01 16:21:11 +00002882/**************************************************************/
2883/* sync block device emulation */
2884
2885static void bdrv_rw_em_cb(void *opaque, int ret)
2886{
2887 *(int *)opaque = ret;
2888}
2889
2890#define NOT_DONE 0x7fffffff
2891
ths5fafdf22007-09-16 21:08:06 +00002892static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +00002893 uint8_t *buf, int nb_sectors)
2894{
pbrookce1a14d2006-08-07 02:38:06 +00002895 int async_ret;
2896 BlockDriverAIOCB *acb;
aliguorif141eaf2009-04-07 18:43:24 +00002897 struct iovec iov;
2898 QEMUIOVector qiov;
bellard83f64092006-08-01 16:21:11 +00002899
bellard83f64092006-08-01 16:21:11 +00002900 async_ret = NOT_DONE;
blueswir13f4cb3d2009-04-13 16:31:01 +00002901 iov.iov_base = (void *)buf;
Jes Sorenseneb5a3162010-05-27 16:20:31 +02002902 iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
aliguorif141eaf2009-04-07 18:43:24 +00002903 qemu_iovec_init_external(&qiov, &iov, 1);
2904 acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
2905 bdrv_rw_em_cb, &async_ret);
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002906 if (acb == NULL) {
2907 async_ret = -1;
2908 goto fail;
2909 }
aliguoribaf35cb2008-09-10 15:45:19 +00002910
bellard83f64092006-08-01 16:21:11 +00002911 while (async_ret == NOT_DONE) {
2912 qemu_aio_wait();
2913 }
aliguoribaf35cb2008-09-10 15:45:19 +00002914
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002915
2916fail:
bellard83f64092006-08-01 16:21:11 +00002917 return async_ret;
2918}
2919
2920static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
2921 const uint8_t *buf, int nb_sectors)
2922{
pbrookce1a14d2006-08-07 02:38:06 +00002923 int async_ret;
2924 BlockDriverAIOCB *acb;
aliguorif141eaf2009-04-07 18:43:24 +00002925 struct iovec iov;
2926 QEMUIOVector qiov;
bellard83f64092006-08-01 16:21:11 +00002927
bellard83f64092006-08-01 16:21:11 +00002928 async_ret = NOT_DONE;
aliguorif141eaf2009-04-07 18:43:24 +00002929 iov.iov_base = (void *)buf;
Jes Sorenseneb5a3162010-05-27 16:20:31 +02002930 iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
aliguorif141eaf2009-04-07 18:43:24 +00002931 qemu_iovec_init_external(&qiov, &iov, 1);
2932 acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
2933 bdrv_rw_em_cb, &async_ret);
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002934 if (acb == NULL) {
2935 async_ret = -1;
2936 goto fail;
2937 }
bellard83f64092006-08-01 16:21:11 +00002938 while (async_ret == NOT_DONE) {
2939 qemu_aio_wait();
2940 }
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002941
2942fail:
bellard83f64092006-08-01 16:21:11 +00002943 return async_ret;
2944}
bellardea2384d2004-08-01 21:59:26 +00002945
2946void bdrv_init(void)
2947{
Anthony Liguori5efa9d52009-05-09 17:03:42 -05002948 module_call_init(MODULE_INIT_BLOCK);
bellardea2384d2004-08-01 21:59:26 +00002949}
pbrookce1a14d2006-08-07 02:38:06 +00002950
Markus Armbrustereb852012009-10-27 18:41:44 +01002951void bdrv_init_with_whitelist(void)
2952{
2953 use_bdrv_whitelist = 1;
2954 bdrv_init();
2955}
2956
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002957void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
2958 BlockDriverCompletionFunc *cb, void *opaque)
aliguori6bbff9a2009-03-20 18:25:59 +00002959{
pbrookce1a14d2006-08-07 02:38:06 +00002960 BlockDriverAIOCB *acb;
2961
aliguori6bbff9a2009-03-20 18:25:59 +00002962 if (pool->free_aiocb) {
2963 acb = pool->free_aiocb;
2964 pool->free_aiocb = acb->next;
pbrookce1a14d2006-08-07 02:38:06 +00002965 } else {
Anthony Liguori7267c092011-08-20 22:09:37 -05002966 acb = g_malloc0(pool->aiocb_size);
aliguori6bbff9a2009-03-20 18:25:59 +00002967 acb->pool = pool;
pbrookce1a14d2006-08-07 02:38:06 +00002968 }
2969 acb->bs = bs;
2970 acb->cb = cb;
2971 acb->opaque = opaque;
2972 return acb;
2973}
2974
2975void qemu_aio_release(void *p)
2976{
aliguori6bbff9a2009-03-20 18:25:59 +00002977 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
2978 AIOPool *pool = acb->pool;
2979 acb->next = pool->free_aiocb;
2980 pool->free_aiocb = acb;
pbrookce1a14d2006-08-07 02:38:06 +00002981}
bellard19cb3732006-08-19 11:45:59 +00002982
2983/**************************************************************/
Kevin Wolff9f05dc2011-07-15 13:50:26 +02002984/* Coroutine block device emulation */
2985
2986typedef struct CoroutineIOCompletion {
2987 Coroutine *coroutine;
2988 int ret;
2989} CoroutineIOCompletion;
2990
2991static void bdrv_co_io_em_complete(void *opaque, int ret)
2992{
2993 CoroutineIOCompletion *co = opaque;
2994
2995 co->ret = ret;
2996 qemu_coroutine_enter(co->coroutine, NULL);
2997}
2998
2999static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num,
3000 int nb_sectors, QEMUIOVector *iov,
3001 bool is_write)
3002{
3003 CoroutineIOCompletion co = {
3004 .coroutine = qemu_coroutine_self(),
3005 };
3006 BlockDriverAIOCB *acb;
3007
3008 if (is_write) {
3009 acb = bdrv_aio_writev(bs, sector_num, iov, nb_sectors,
3010 bdrv_co_io_em_complete, &co);
3011 } else {
3012 acb = bdrv_aio_readv(bs, sector_num, iov, nb_sectors,
3013 bdrv_co_io_em_complete, &co);
3014 }
3015
Stefan Hajnoczi59370aa2011-09-30 17:34:58 +01003016 trace_bdrv_co_io_em(bs, sector_num, nb_sectors, is_write, acb);
Kevin Wolff9f05dc2011-07-15 13:50:26 +02003017 if (!acb) {
3018 return -EIO;
3019 }
3020 qemu_coroutine_yield();
3021
3022 return co.ret;
3023}
3024
3025static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs,
3026 int64_t sector_num, int nb_sectors,
3027 QEMUIOVector *iov)
3028{
3029 return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false);
3030}
3031
3032static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs,
3033 int64_t sector_num, int nb_sectors,
3034 QEMUIOVector *iov)
3035{
3036 return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true);
3037}
3038
Kevin Wolfe7a8a782011-07-15 16:05:00 +02003039static int coroutine_fn bdrv_co_flush_em(BlockDriverState *bs)
3040{
3041 CoroutineIOCompletion co = {
3042 .coroutine = qemu_coroutine_self(),
3043 };
3044 BlockDriverAIOCB *acb;
3045
3046 acb = bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co);
3047 if (!acb) {
3048 return -EIO;
3049 }
3050 qemu_coroutine_yield();
3051 return co.ret;
3052}
3053
Kevin Wolff9f05dc2011-07-15 13:50:26 +02003054/**************************************************************/
bellard19cb3732006-08-19 11:45:59 +00003055/* removable device support */
3056
3057/**
3058 * Return TRUE if the media is present
3059 */
3060int bdrv_is_inserted(BlockDriverState *bs)
3061{
3062 BlockDriver *drv = bs->drv;
Markus Armbrustera1aff5b2011-09-06 18:58:41 +02003063
bellard19cb3732006-08-19 11:45:59 +00003064 if (!drv)
3065 return 0;
3066 if (!drv->bdrv_is_inserted)
Markus Armbrustera1aff5b2011-09-06 18:58:41 +02003067 return 1;
3068 return drv->bdrv_is_inserted(bs);
bellard19cb3732006-08-19 11:45:59 +00003069}
3070
3071/**
Markus Armbruster8e49ca42011-08-03 15:08:08 +02003072 * Return whether the media changed since the last call to this
3073 * function, or -ENOTSUP if we don't know. Most drivers don't know.
bellard19cb3732006-08-19 11:45:59 +00003074 */
3075int bdrv_media_changed(BlockDriverState *bs)
3076{
3077 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +00003078
Markus Armbruster8e49ca42011-08-03 15:08:08 +02003079 if (drv && drv->bdrv_media_changed) {
3080 return drv->bdrv_media_changed(bs);
3081 }
3082 return -ENOTSUP;
bellard19cb3732006-08-19 11:45:59 +00003083}
3084
3085/**
3086 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
3087 */
Markus Armbrusterfdec4402011-09-06 18:58:45 +02003088void bdrv_eject(BlockDriverState *bs, int eject_flag)
bellard19cb3732006-08-19 11:45:59 +00003089{
3090 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +00003091
Markus Armbruster822e1cd2011-07-20 18:23:42 +02003092 if (drv && drv->bdrv_eject) {
3093 drv->bdrv_eject(bs, eject_flag);
bellard19cb3732006-08-19 11:45:59 +00003094 }
bellard19cb3732006-08-19 11:45:59 +00003095}
3096
bellard19cb3732006-08-19 11:45:59 +00003097/**
3098 * Lock or unlock the media (if it is locked, the user won't be able
3099 * to eject it manually).
3100 */
Markus Armbruster025e8492011-09-06 18:58:47 +02003101void bdrv_lock_medium(BlockDriverState *bs, bool locked)
bellard19cb3732006-08-19 11:45:59 +00003102{
3103 BlockDriver *drv = bs->drv;
3104
Markus Armbruster025e8492011-09-06 18:58:47 +02003105 trace_bdrv_lock_medium(bs, locked);
Stefan Hajnoczib8c6d092011-03-29 20:04:40 +01003106
Markus Armbruster025e8492011-09-06 18:58:47 +02003107 if (drv && drv->bdrv_lock_medium) {
3108 drv->bdrv_lock_medium(bs, locked);
bellard19cb3732006-08-19 11:45:59 +00003109 }
3110}
ths985a03b2007-12-24 16:10:43 +00003111
3112/* needed for generic scsi interface */
3113
3114int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
3115{
3116 BlockDriver *drv = bs->drv;
3117
3118 if (drv && drv->bdrv_ioctl)
3119 return drv->bdrv_ioctl(bs, req, buf);
3120 return -ENOTSUP;
3121}
aliguori7d780662009-03-12 19:57:08 +00003122
aliguori221f7152009-03-28 17:28:41 +00003123BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
3124 unsigned long int req, void *buf,
3125 BlockDriverCompletionFunc *cb, void *opaque)
aliguori7d780662009-03-12 19:57:08 +00003126{
aliguori221f7152009-03-28 17:28:41 +00003127 BlockDriver *drv = bs->drv;
aliguori7d780662009-03-12 19:57:08 +00003128
aliguori221f7152009-03-28 17:28:41 +00003129 if (drv && drv->bdrv_aio_ioctl)
3130 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
3131 return NULL;
aliguori7d780662009-03-12 19:57:08 +00003132}
aliguorie268ca52009-04-22 20:20:00 +00003133
Markus Armbruster7b6f9302011-09-06 18:58:56 +02003134void bdrv_set_buffer_alignment(BlockDriverState *bs, int align)
3135{
3136 bs->buffer_alignment = align;
3137}
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02003138
aliguorie268ca52009-04-22 20:20:00 +00003139void *qemu_blockalign(BlockDriverState *bs, size_t size)
3140{
3141 return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
3142}
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02003143
3144void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable)
3145{
3146 int64_t bitmap_size;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01003147
Liran Schouraaa0eb72010-01-26 10:31:48 +02003148 bs->dirty_count = 0;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01003149 if (enable) {
Jan Kiszkac6d22832009-11-30 18:21:20 +01003150 if (!bs->dirty_bitmap) {
3151 bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
3152 BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
3153 bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01003154
Anthony Liguori7267c092011-08-20 22:09:37 -05003155 bs->dirty_bitmap = g_malloc0(bitmap_size);
Jan Kiszkaa55eb922009-11-30 18:21:19 +01003156 }
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02003157 } else {
Jan Kiszkac6d22832009-11-30 18:21:20 +01003158 if (bs->dirty_bitmap) {
Anthony Liguori7267c092011-08-20 22:09:37 -05003159 g_free(bs->dirty_bitmap);
Jan Kiszkac6d22832009-11-30 18:21:20 +01003160 bs->dirty_bitmap = NULL;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01003161 }
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02003162 }
3163}
3164
3165int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
3166{
Jan Kiszka6ea44302009-11-30 18:21:19 +01003167 int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01003168
Jan Kiszkac6d22832009-11-30 18:21:20 +01003169 if (bs->dirty_bitmap &&
3170 (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) {
Marcelo Tosatti6d59fec2010-11-08 17:02:54 -02003171 return !!(bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] &
3172 (1UL << (chunk % (sizeof(unsigned long) * 8))));
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02003173 } else {
3174 return 0;
3175 }
3176}
3177
Jan Kiszkaa55eb922009-11-30 18:21:19 +01003178void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
3179 int nr_sectors)
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02003180{
3181 set_dirty_bitmap(bs, cur_sector, nr_sectors, 0);
3182}
Liran Schouraaa0eb72010-01-26 10:31:48 +02003183
3184int64_t bdrv_get_dirty_count(BlockDriverState *bs)
3185{
3186 return bs->dirty_count;
3187}
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003188
Marcelo Tosattidb593f22011-01-26 12:12:34 -02003189void bdrv_set_in_use(BlockDriverState *bs, int in_use)
3190{
3191 assert(bs->in_use != in_use);
3192 bs->in_use = in_use;
3193}
3194
3195int bdrv_in_use(BlockDriverState *bs)
3196{
3197 return bs->in_use;
3198}
3199
Luiz Capitulino28a72822011-09-26 17:43:50 -03003200void bdrv_iostatus_enable(BlockDriverState *bs)
3201{
3202 bs->iostatus = BDRV_IOS_OK;
3203}
3204
3205/* The I/O status is only enabled if the drive explicitly
3206 * enables it _and_ the VM is configured to stop on errors */
3207bool bdrv_iostatus_is_enabled(const BlockDriverState *bs)
3208{
3209 return (bs->iostatus != BDRV_IOS_INVAL &&
3210 (bs->on_write_error == BLOCK_ERR_STOP_ENOSPC ||
3211 bs->on_write_error == BLOCK_ERR_STOP_ANY ||
3212 bs->on_read_error == BLOCK_ERR_STOP_ANY));
3213}
3214
3215void bdrv_iostatus_disable(BlockDriverState *bs)
3216{
3217 bs->iostatus = BDRV_IOS_INVAL;
3218}
3219
3220void bdrv_iostatus_reset(BlockDriverState *bs)
3221{
3222 if (bdrv_iostatus_is_enabled(bs)) {
3223 bs->iostatus = BDRV_IOS_OK;
3224 }
3225}
3226
3227/* XXX: Today this is set by device models because it makes the implementation
3228 quite simple. However, the block layer knows about the error, so it's
3229 possible to implement this without device models being involved */
3230void bdrv_iostatus_set_err(BlockDriverState *bs, int error)
3231{
3232 if (bdrv_iostatus_is_enabled(bs) && bs->iostatus == BDRV_IOS_OK) {
3233 assert(error >= 0);
3234 bs->iostatus = error == ENOSPC ? BDRV_IOS_ENOSPC : BDRV_IOS_FAILED;
3235 }
3236}
3237
Christoph Hellwiga597e792011-08-25 08:26:01 +02003238void
3239bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, int64_t bytes,
3240 enum BlockAcctType type)
3241{
3242 assert(type < BDRV_MAX_IOTYPE);
3243
3244 cookie->bytes = bytes;
Christoph Hellwigc488c7f2011-08-25 08:26:10 +02003245 cookie->start_time_ns = get_clock();
Christoph Hellwiga597e792011-08-25 08:26:01 +02003246 cookie->type = type;
3247}
3248
3249void
3250bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie)
3251{
3252 assert(cookie->type < BDRV_MAX_IOTYPE);
3253
3254 bs->nr_bytes[cookie->type] += cookie->bytes;
3255 bs->nr_ops[cookie->type]++;
Christoph Hellwigc488c7f2011-08-25 08:26:10 +02003256 bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns;
Christoph Hellwiga597e792011-08-25 08:26:01 +02003257}
3258
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003259int bdrv_img_create(const char *filename, const char *fmt,
3260 const char *base_filename, const char *base_fmt,
3261 char *options, uint64_t img_size, int flags)
3262{
3263 QEMUOptionParameter *param = NULL, *create_options = NULL;
Kevin Wolfd2208942011-06-01 14:03:31 +02003264 QEMUOptionParameter *backing_fmt, *backing_file, *size;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003265 BlockDriverState *bs = NULL;
3266 BlockDriver *drv, *proto_drv;
Stefan Hajnoczi96df67d2011-01-24 09:32:20 +00003267 BlockDriver *backing_drv = NULL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003268 int ret = 0;
3269
3270 /* Find driver and parse its options */
3271 drv = bdrv_find_format(fmt);
3272 if (!drv) {
3273 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4f70f242010-12-16 13:52:18 +01003274 ret = -EINVAL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003275 goto out;
3276 }
3277
3278 proto_drv = bdrv_find_protocol(filename);
3279 if (!proto_drv) {
3280 error_report("Unknown protocol '%s'", filename);
Jes Sorensen4f70f242010-12-16 13:52:18 +01003281 ret = -EINVAL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003282 goto out;
3283 }
3284
3285 create_options = append_option_parameters(create_options,
3286 drv->create_options);
3287 create_options = append_option_parameters(create_options,
3288 proto_drv->create_options);
3289
3290 /* Create parameter list with default values */
3291 param = parse_option_parameters("", create_options, param);
3292
3293 set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size);
3294
3295 /* Parse -o options */
3296 if (options) {
3297 param = parse_option_parameters(options, create_options, param);
3298 if (param == NULL) {
3299 error_report("Invalid options for file format '%s'.", fmt);
Jes Sorensen4f70f242010-12-16 13:52:18 +01003300 ret = -EINVAL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003301 goto out;
3302 }
3303 }
3304
3305 if (base_filename) {
3306 if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE,
3307 base_filename)) {
3308 error_report("Backing file not supported for file format '%s'",
3309 fmt);
Jes Sorensen4f70f242010-12-16 13:52:18 +01003310 ret = -EINVAL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003311 goto out;
3312 }
3313 }
3314
3315 if (base_fmt) {
3316 if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) {
3317 error_report("Backing file format not supported for file "
3318 "format '%s'", fmt);
Jes Sorensen4f70f242010-12-16 13:52:18 +01003319 ret = -EINVAL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003320 goto out;
3321 }
3322 }
3323
Jes Sorensen792da932010-12-16 13:52:17 +01003324 backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
3325 if (backing_file && backing_file->value.s) {
3326 if (!strcmp(filename, backing_file->value.s)) {
3327 error_report("Error: Trying to create an image with the "
3328 "same filename as the backing file");
Jes Sorensen4f70f242010-12-16 13:52:18 +01003329 ret = -EINVAL;
Jes Sorensen792da932010-12-16 13:52:17 +01003330 goto out;
3331 }
3332 }
3333
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003334 backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT);
3335 if (backing_fmt && backing_fmt->value.s) {
Stefan Hajnoczi96df67d2011-01-24 09:32:20 +00003336 backing_drv = bdrv_find_format(backing_fmt->value.s);
3337 if (!backing_drv) {
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003338 error_report("Unknown backing file format '%s'",
3339 backing_fmt->value.s);
Jes Sorensen4f70f242010-12-16 13:52:18 +01003340 ret = -EINVAL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003341 goto out;
3342 }
3343 }
3344
3345 // The size for the image must always be specified, with one exception:
3346 // If we are using a backing file, we can obtain the size from there
Kevin Wolfd2208942011-06-01 14:03:31 +02003347 size = get_option_parameter(param, BLOCK_OPT_SIZE);
3348 if (size && size->value.n == -1) {
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003349 if (backing_file && backing_file->value.s) {
3350 uint64_t size;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003351 char buf[32];
3352
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003353 bs = bdrv_new("");
3354
Stefan Hajnoczi96df67d2011-01-24 09:32:20 +00003355 ret = bdrv_open(bs, backing_file->value.s, flags, backing_drv);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003356 if (ret < 0) {
Stefan Hajnoczi96df67d2011-01-24 09:32:20 +00003357 error_report("Could not open '%s'", backing_file->value.s);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003358 goto out;
3359 }
3360 bdrv_get_geometry(bs, &size);
3361 size *= 512;
3362
3363 snprintf(buf, sizeof(buf), "%" PRId64, size);
3364 set_option_parameter(param, BLOCK_OPT_SIZE, buf);
3365 } else {
3366 error_report("Image creation needs a size parameter");
Jes Sorensen4f70f242010-12-16 13:52:18 +01003367 ret = -EINVAL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003368 goto out;
3369 }
3370 }
3371
3372 printf("Formatting '%s', fmt=%s ", filename, fmt);
3373 print_option_parameters(param);
3374 puts("");
3375
3376 ret = bdrv_create(drv, filename, param);
3377
3378 if (ret < 0) {
3379 if (ret == -ENOTSUP) {
3380 error_report("Formatting or formatting option not supported for "
3381 "file format '%s'", fmt);
3382 } else if (ret == -EFBIG) {
3383 error_report("The image size is too large for file format '%s'",
3384 fmt);
3385 } else {
3386 error_report("%s: error while creating %s: %s", filename, fmt,
3387 strerror(-ret));
3388 }
3389 }
3390
3391out:
3392 free_option_parameters(create_options);
3393 free_option_parameters(param);
3394
3395 if (bs) {
3396 bdrv_delete(bs);
3397 }
Jes Sorensen4f70f242010-12-16 13:52:18 +01003398
3399 return ret;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003400}