blob: 7974215ea4767c2d0a70c63d6abb44f27c67776f [file] [log] [blame]
bellardfc01f7e2003-06-30 10:03:06 +00001/*
2 * QEMU System Emulator block driver
ths5fafdf22007-09-16 21:08:06 +00003 *
bellardfc01f7e2003-06-30 10:03:06 +00004 * Copyright (c) 2003 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellardfc01f7e2003-06-30 10:03:06 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
blueswir13990d092008-12-05 17:53:21 +000024#include "config-host.h"
pbrookfaf07962007-11-11 02:51:17 +000025#include "qemu-common.h"
aliguori376253e2009-03-05 23:01:23 +000026#include "monitor.h"
bellardea2384d2004-08-01 21:59:26 +000027#include "block_int.h"
Anthony Liguori5efa9d52009-05-09 17:03:42 -050028#include "module.h"
Luiz Capitulinod15e5462009-12-10 17:16:06 -020029#include "qemu-objects.h"
bellardfc01f7e2003-06-30 10:03:06 +000030
Juan Quintela71e72a12009-07-27 16:12:56 +020031#ifdef CONFIG_BSD
bellard7674e7b2005-04-26 21:59:26 +000032#include <sys/types.h>
33#include <sys/stat.h>
34#include <sys/ioctl.h>
Blue Swirl72cf2d42009-09-12 07:36:22 +000035#include <sys/queue.h>
blueswir1c5e97232009-03-07 20:06:23 +000036#ifndef __DragonFly__
bellard7674e7b2005-04-26 21:59:26 +000037#include <sys/disk.h>
38#endif
blueswir1c5e97232009-03-07 20:06:23 +000039#endif
bellard7674e7b2005-04-26 21:59:26 +000040
aliguori49dc7682009-03-08 16:26:59 +000041#ifdef _WIN32
42#include <windows.h>
43#endif
44
aliguorif141eaf2009-04-07 18:43:24 +000045static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
46 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
aliguoric87c0672009-04-07 18:43:20 +000047 BlockDriverCompletionFunc *cb, void *opaque);
aliguorif141eaf2009-04-07 18:43:24 +000048static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
49 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
pbrookce1a14d2006-08-07 02:38:06 +000050 BlockDriverCompletionFunc *cb, void *opaque);
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +020051static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
52 BlockDriverCompletionFunc *cb, void *opaque);
ths5fafdf22007-09-16 21:08:06 +000053static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +000054 uint8_t *buf, int nb_sectors);
55static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
56 const uint8_t *buf, int nb_sectors);
bellardec530c82006-04-25 22:36:06 +000057
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +010058static QTAILQ_HEAD(, BlockDriverState) bdrv_states =
59 QTAILQ_HEAD_INITIALIZER(bdrv_states);
blueswir17ee930d2008-09-17 19:04:14 +000060
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +010061static QLIST_HEAD(, BlockDriver) bdrv_drivers =
62 QLIST_HEAD_INITIALIZER(bdrv_drivers);
bellardea2384d2004-08-01 21:59:26 +000063
Markus Armbrustereb852012009-10-27 18:41:44 +010064/* If non-zero, use only whitelisted block drivers */
65static int use_bdrv_whitelist;
66
bellard83f64092006-08-01 16:21:11 +000067int path_is_absolute(const char *path)
68{
69 const char *p;
bellard21664422007-01-07 18:22:37 +000070#ifdef _WIN32
71 /* specific case for names like: "\\.\d:" */
72 if (*path == '/' || *path == '\\')
73 return 1;
74#endif
bellard83f64092006-08-01 16:21:11 +000075 p = strchr(path, ':');
76 if (p)
77 p++;
78 else
79 p = path;
bellard3b9f94e2007-01-07 17:27:07 +000080#ifdef _WIN32
81 return (*p == '/' || *p == '\\');
82#else
83 return (*p == '/');
84#endif
bellard83f64092006-08-01 16:21:11 +000085}
86
87/* if filename is absolute, just copy it to dest. Otherwise, build a
88 path to it by considering it is relative to base_path. URL are
89 supported. */
90void path_combine(char *dest, int dest_size,
91 const char *base_path,
92 const char *filename)
93{
94 const char *p, *p1;
95 int len;
96
97 if (dest_size <= 0)
98 return;
99 if (path_is_absolute(filename)) {
100 pstrcpy(dest, dest_size, filename);
101 } else {
102 p = strchr(base_path, ':');
103 if (p)
104 p++;
105 else
106 p = base_path;
bellard3b9f94e2007-01-07 17:27:07 +0000107 p1 = strrchr(base_path, '/');
108#ifdef _WIN32
109 {
110 const char *p2;
111 p2 = strrchr(base_path, '\\');
112 if (!p1 || p2 > p1)
113 p1 = p2;
114 }
115#endif
bellard83f64092006-08-01 16:21:11 +0000116 if (p1)
117 p1++;
118 else
119 p1 = base_path;
120 if (p1 > p)
121 p = p1;
122 len = p - base_path;
123 if (len > dest_size - 1)
124 len = dest_size - 1;
125 memcpy(dest, base_path, len);
126 dest[len] = '\0';
127 pstrcat(dest, dest_size, filename);
128 }
129}
130
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500131void bdrv_register(BlockDriver *bdrv)
bellardea2384d2004-08-01 21:59:26 +0000132{
aliguorif141eaf2009-04-07 18:43:24 +0000133 if (!bdrv->bdrv_aio_readv) {
bellard83f64092006-08-01 16:21:11 +0000134 /* add AIO emulation layer */
aliguorif141eaf2009-04-07 18:43:24 +0000135 bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
136 bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
aliguorieda578e2009-03-12 19:57:16 +0000137 } else if (!bdrv->bdrv_read) {
bellard83f64092006-08-01 16:21:11 +0000138 /* add synchronous IO emulation layer */
139 bdrv->bdrv_read = bdrv_read_em;
140 bdrv->bdrv_write = bdrv_write_em;
141 }
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +0200142
143 if (!bdrv->bdrv_aio_flush)
144 bdrv->bdrv_aio_flush = bdrv_aio_flush_em;
145
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100146 QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
bellardea2384d2004-08-01 21:59:26 +0000147}
bellardb3380822004-03-14 21:38:54 +0000148
149/* create a new block device (by default it is empty) */
150BlockDriverState *bdrv_new(const char *device_name)
bellardfc01f7e2003-06-30 10:03:06 +0000151{
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +0100152 BlockDriverState *bs;
bellardb3380822004-03-14 21:38:54 +0000153
154 bs = qemu_mallocz(sizeof(BlockDriverState));
bellardb3380822004-03-14 21:38:54 +0000155 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
bellardea2384d2004-08-01 21:59:26 +0000156 if (device_name[0] != '\0') {
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +0100157 QTAILQ_INSERT_TAIL(&bdrv_states, bs, list);
bellardea2384d2004-08-01 21:59:26 +0000158 }
bellardb3380822004-03-14 21:38:54 +0000159 return bs;
160}
161
bellardea2384d2004-08-01 21:59:26 +0000162BlockDriver *bdrv_find_format(const char *format_name)
163{
164 BlockDriver *drv1;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100165 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
166 if (!strcmp(drv1->format_name, format_name)) {
bellardea2384d2004-08-01 21:59:26 +0000167 return drv1;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100168 }
bellardea2384d2004-08-01 21:59:26 +0000169 }
170 return NULL;
171}
172
Markus Armbrustereb852012009-10-27 18:41:44 +0100173static int bdrv_is_whitelisted(BlockDriver *drv)
174{
175 static const char *whitelist[] = {
176 CONFIG_BDRV_WHITELIST
177 };
178 const char **p;
179
180 if (!whitelist[0])
181 return 1; /* no whitelist, anything goes */
182
183 for (p = whitelist; *p; p++) {
184 if (!strcmp(drv->format_name, *p)) {
185 return 1;
186 }
187 }
188 return 0;
189}
190
191BlockDriver *bdrv_find_whitelisted_format(const char *format_name)
192{
193 BlockDriver *drv = bdrv_find_format(format_name);
194 return drv && bdrv_is_whitelisted(drv) ? drv : NULL;
195}
196
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200197int bdrv_create(BlockDriver *drv, const char* filename,
198 QEMUOptionParameter *options)
bellardea2384d2004-08-01 21:59:26 +0000199{
200 if (!drv->bdrv_create)
201 return -ENOTSUP;
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200202
203 return drv->bdrv_create(filename, options);
bellardea2384d2004-08-01 21:59:26 +0000204}
205
bellardd5249392004-08-03 21:14:23 +0000206#ifdef _WIN32
bellard95389c82005-12-18 18:28:15 +0000207void get_tmp_filename(char *filename, int size)
bellardd5249392004-08-03 21:14:23 +0000208{
bellard3b9f94e2007-01-07 17:27:07 +0000209 char temp_dir[MAX_PATH];
ths3b46e622007-09-17 08:09:54 +0000210
bellard3b9f94e2007-01-07 17:27:07 +0000211 GetTempPath(MAX_PATH, temp_dir);
212 GetTempFileName(temp_dir, "qem", 0, filename);
bellardd5249392004-08-03 21:14:23 +0000213}
214#else
bellard95389c82005-12-18 18:28:15 +0000215void get_tmp_filename(char *filename, int size)
bellardea2384d2004-08-01 21:59:26 +0000216{
217 int fd;
blueswir17ccfb2e2008-09-14 06:45:34 +0000218 const char *tmpdir;
bellardd5249392004-08-03 21:14:23 +0000219 /* XXX: race condition possible */
aurel320badc1e2008-03-10 00:05:34 +0000220 tmpdir = getenv("TMPDIR");
221 if (!tmpdir)
222 tmpdir = "/tmp";
223 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
bellardea2384d2004-08-01 21:59:26 +0000224 fd = mkstemp(filename);
225 close(fd);
226}
bellardd5249392004-08-03 21:14:23 +0000227#endif
bellardea2384d2004-08-01 21:59:26 +0000228
bellard19cb3732006-08-19 11:45:59 +0000229#ifdef _WIN32
bellardf45512f2006-08-23 21:40:13 +0000230static int is_windows_drive_prefix(const char *filename)
231{
232 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
233 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
234 filename[1] == ':');
235}
ths3b46e622007-09-17 08:09:54 +0000236
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200237int is_windows_drive(const char *filename)
bellard19cb3732006-08-19 11:45:59 +0000238{
ths5fafdf22007-09-16 21:08:06 +0000239 if (is_windows_drive_prefix(filename) &&
bellardf45512f2006-08-23 21:40:13 +0000240 filename[2] == '\0')
bellard19cb3732006-08-19 11:45:59 +0000241 return 1;
242 if (strstart(filename, "\\\\.\\", NULL) ||
243 strstart(filename, "//./", NULL))
244 return 1;
245 return 0;
246}
247#endif
248
bellard83f64092006-08-01 16:21:11 +0000249static BlockDriver *find_protocol(const char *filename)
250{
251 BlockDriver *drv1;
252 char protocol[128];
Anthony Liguori1cec71e2009-07-02 08:12:26 -0500253 int len;
bellard83f64092006-08-01 16:21:11 +0000254 const char *p;
bellard19cb3732006-08-19 11:45:59 +0000255
256#ifdef _WIN32
bellardf45512f2006-08-23 21:40:13 +0000257 if (is_windows_drive(filename) ||
258 is_windows_drive_prefix(filename))
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500259 return bdrv_find_format("raw");
bellard19cb3732006-08-19 11:45:59 +0000260#endif
Anthony Liguori1cec71e2009-07-02 08:12:26 -0500261 p = strchr(filename, ':');
262 if (!p)
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500263 return bdrv_find_format("raw");
Anthony Liguori1cec71e2009-07-02 08:12:26 -0500264 len = p - filename;
265 if (len > sizeof(protocol) - 1)
266 len = sizeof(protocol) - 1;
267 memcpy(protocol, filename, len);
268 protocol[len] = '\0';
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100269 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
ths5fafdf22007-09-16 21:08:06 +0000270 if (drv1->protocol_name &&
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100271 !strcmp(drv1->protocol_name, protocol)) {
bellard83f64092006-08-01 16:21:11 +0000272 return drv1;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100273 }
bellard83f64092006-08-01 16:21:11 +0000274 }
275 return NULL;
276}
277
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200278/*
279 * Detect host devices. By convention, /dev/cdrom[N] is always
280 * recognized as a host CDROM.
281 */
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200282static BlockDriver *find_hdev_driver(const char *filename)
283{
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200284 int score_max = 0, score;
285 BlockDriver *drv = NULL, *d;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200286
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100287 QLIST_FOREACH(d, &bdrv_drivers, list) {
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200288 if (d->bdrv_probe_device) {
289 score = d->bdrv_probe_device(filename);
290 if (score > score_max) {
291 score_max = score;
292 drv = d;
293 }
294 }
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200295 }
296
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200297 return drv;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200298}
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200299
bellardea2384d2004-08-01 21:59:26 +0000300static BlockDriver *find_image_format(const char *filename)
301{
bellard83f64092006-08-01 16:21:11 +0000302 int ret, score, score_max;
bellardea2384d2004-08-01 21:59:26 +0000303 BlockDriver *drv1, *drv;
bellard83f64092006-08-01 16:21:11 +0000304 uint8_t buf[2048];
305 BlockDriverState *bs;
ths3b46e622007-09-17 08:09:54 +0000306
bellard83f64092006-08-01 16:21:11 +0000307 drv = find_protocol(filename);
bellard19cb3732006-08-19 11:45:59 +0000308 /* no need to test disk image formats for vvfat */
Anthony Liguoric833ab72009-05-22 08:17:55 -0500309 if (drv && strcmp(drv->format_name, "vvfat") == 0)
bellard83f64092006-08-01 16:21:11 +0000310 return drv;
bellard19cb3732006-08-19 11:45:59 +0000311
Naphtali Spreif5edb012010-01-17 16:48:13 +0200312 ret = bdrv_file_open(&bs, filename, 0);
bellard83f64092006-08-01 16:21:11 +0000313 if (ret < 0)
314 return NULL;
315 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
316 bdrv_delete(bs);
317 if (ret < 0) {
318 return NULL;
319 }
320
bellardea2384d2004-08-01 21:59:26 +0000321 score_max = 0;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100322 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
bellard83f64092006-08-01 16:21:11 +0000323 if (drv1->bdrv_probe) {
324 score = drv1->bdrv_probe(buf, ret, filename);
325 if (score > score_max) {
326 score_max = score;
327 drv = drv1;
328 }
bellardea2384d2004-08-01 21:59:26 +0000329 }
330 }
331 return drv;
332}
333
bellard83f64092006-08-01 16:21:11 +0000334int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
bellardb3380822004-03-14 21:38:54 +0000335{
bellard83f64092006-08-01 16:21:11 +0000336 BlockDriverState *bs;
Christoph Hellwig6db95602010-04-05 16:53:57 +0200337 BlockDriver *drv;
bellard83f64092006-08-01 16:21:11 +0000338 int ret;
339
Christoph Hellwig6db95602010-04-05 16:53:57 +0200340 drv = find_protocol(filename);
341 if (!drv) {
342 return -ENOENT;
343 }
344
bellard83f64092006-08-01 16:21:11 +0000345 bs = bdrv_new("");
Christoph Hellwig6db95602010-04-05 16:53:57 +0200346 ret = bdrv_open(bs, filename, flags, drv);
bellard83f64092006-08-01 16:21:11 +0000347 if (ret < 0) {
348 bdrv_delete(bs);
349 return ret;
bellard3b0d4f62005-10-30 18:30:10 +0000350 }
aliguori71d07702009-03-03 17:37:16 +0000351 bs->growable = 1;
bellard83f64092006-08-01 16:21:11 +0000352 *pbs = bs;
353 return 0;
bellardea2384d2004-08-01 21:59:26 +0000354}
bellardfc01f7e2003-06-30 10:03:06 +0000355
Kevin Wolfd6e90982010-03-31 14:40:27 +0200356int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
357 BlockDriver *drv)
bellardea2384d2004-08-01 21:59:26 +0000358{
Naphtali Spreif5edb012010-01-17 16:48:13 +0200359 int ret, open_flags;
thseb5c8512007-02-11 15:06:09 +0000360 char tmp_filename[PATH_MAX];
361 char backing_filename[PATH_MAX];
ths3b46e622007-09-17 08:09:54 +0000362
bellardea2384d2004-08-01 21:59:26 +0000363 bs->is_temporary = 0;
364 bs->encrypted = 0;
aliguoric0f4ce72009-03-05 23:01:01 +0000365 bs->valid_key = 0;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200366 bs->open_flags = flags;
aliguorie268ca52009-04-22 20:20:00 +0000367 /* buffer_alignment defaulted to 512, drivers can change this value */
368 bs->buffer_alignment = 512;
bellard712e7872005-04-28 21:09:32 +0000369
bellard83f64092006-08-01 16:21:11 +0000370 if (flags & BDRV_O_SNAPSHOT) {
bellardea2384d2004-08-01 21:59:26 +0000371 BlockDriverState *bs1;
372 int64_t total_size;
aliguori7c96d462008-09-12 17:54:13 +0000373 int is_protocol = 0;
Kevin Wolf91a073a2009-05-27 14:48:06 +0200374 BlockDriver *bdrv_qcow2;
375 QEMUOptionParameter *options;
ths3b46e622007-09-17 08:09:54 +0000376
bellardea2384d2004-08-01 21:59:26 +0000377 /* if snapshot, we create a temporary backing file and open it
378 instead of opening 'filename' directly */
379
380 /* if there is a backing file, use it */
381 bs1 = bdrv_new("");
Kevin Wolfd6e90982010-03-31 14:40:27 +0200382 ret = bdrv_open(bs1, filename, 0, drv);
aliguori51d7c002009-03-05 23:00:29 +0000383 if (ret < 0) {
bellardea2384d2004-08-01 21:59:26 +0000384 bdrv_delete(bs1);
aliguori51d7c002009-03-05 23:00:29 +0000385 return ret;
bellardea2384d2004-08-01 21:59:26 +0000386 }
Jan Kiszka6ea44302009-11-30 18:21:19 +0100387 total_size = bdrv_getlength(bs1) >> BDRV_SECTOR_BITS;
aliguori7c96d462008-09-12 17:54:13 +0000388
389 if (bs1->drv && bs1->drv->protocol_name)
390 is_protocol = 1;
391
bellardea2384d2004-08-01 21:59:26 +0000392 bdrv_delete(bs1);
ths3b46e622007-09-17 08:09:54 +0000393
bellardea2384d2004-08-01 21:59:26 +0000394 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
aliguori7c96d462008-09-12 17:54:13 +0000395
396 /* Real path is meaningless for protocols */
397 if (is_protocol)
398 snprintf(backing_filename, sizeof(backing_filename),
399 "%s", filename);
Kirill A. Shutemov114cdfa2009-12-25 18:19:22 +0000400 else if (!realpath(filename, backing_filename))
401 return -errno;
aliguori7c96d462008-09-12 17:54:13 +0000402
Kevin Wolf91a073a2009-05-27 14:48:06 +0200403 bdrv_qcow2 = bdrv_find_format("qcow2");
404 options = parse_option_parameters("", bdrv_qcow2->create_options, NULL);
405
406 set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size * 512);
407 set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename);
408 if (drv) {
409 set_option_parameter(options, BLOCK_OPT_BACKING_FMT,
410 drv->format_name);
411 }
412
413 ret = bdrv_create(bdrv_qcow2, tmp_filename, options);
aliguori51d7c002009-03-05 23:00:29 +0000414 if (ret < 0) {
415 return ret;
bellardea2384d2004-08-01 21:59:26 +0000416 }
Kevin Wolf91a073a2009-05-27 14:48:06 +0200417
bellardea2384d2004-08-01 21:59:26 +0000418 filename = tmp_filename;
Kevin Wolf91a073a2009-05-27 14:48:06 +0200419 drv = bdrv_qcow2;
bellardea2384d2004-08-01 21:59:26 +0000420 bs->is_temporary = 1;
421 }
bellard712e7872005-04-28 21:09:32 +0000422
bellardea2384d2004-08-01 21:59:26 +0000423 pstrcpy(bs->filename, sizeof(bs->filename), filename);
Christoph Hellwig6db95602010-04-05 16:53:57 +0200424
425 if (!drv) {
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200426 drv = find_hdev_driver(filename);
427 if (!drv) {
428 drv = find_image_format(filename);
429 }
aliguori51d7c002009-03-05 23:00:29 +0000430 }
Christoph Hellwig69873072010-01-20 18:13:25 +0100431
aliguori51d7c002009-03-05 23:00:29 +0000432 if (!drv) {
433 ret = -ENOENT;
434 goto unlink_and_fail;
bellardea2384d2004-08-01 21:59:26 +0000435 }
Christoph Hellwig69873072010-01-20 18:13:25 +0100436 if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {
437 ret = -ENOTSUP;
438 goto unlink_and_fail;
439 }
440
bellardea2384d2004-08-01 21:59:26 +0000441 bs->drv = drv;
442 bs->opaque = qemu_mallocz(drv->instance_size);
Christoph Hellwige900a7b2009-09-04 19:01:15 +0200443
444 /*
445 * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a
446 * write cache to the guest. We do need the fdatasync to flush
447 * out transactions for block allocations, and we maybe have a
448 * volatile write cache in our backing device to deal with.
449 */
450 if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE))
451 bs->enable_write_cache = 1;
452
Christoph Hellwig15dc2692010-01-28 15:19:12 +0100453 /*
454 * Clear flags that are internal to the block layer before opening the
455 * image.
456 */
Christoph Hellwig6db95602010-04-05 16:53:57 +0200457 open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
Christoph Hellwig15dc2692010-01-28 15:19:12 +0100458
459 /*
460 * Snapshots should be writeable.
Christoph Hellwig15dc2692010-01-28 15:19:12 +0100461 */
Christoph Hellwig6db95602010-04-05 16:53:57 +0200462 if (bs->is_temporary) {
Christoph Hellwig15dc2692010-01-28 15:19:12 +0100463 open_flags |= BDRV_O_RDWR;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200464 }
Christoph Hellwig69873072010-01-20 18:13:25 +0100465
466 ret = drv->bdrv_open(bs, filename, open_flags);
bellardea2384d2004-08-01 21:59:26 +0000467 if (ret < 0) {
Christoph Hellwig69873072010-01-20 18:13:25 +0100468 goto free_and_fail;
bellardea2384d2004-08-01 21:59:26 +0000469 }
Christoph Hellwig69873072010-01-20 18:13:25 +0100470
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200471 bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR);
bellardd15a7712006-08-06 13:35:09 +0000472 if (drv->bdrv_getlength) {
Jan Kiszka6ea44302009-11-30 18:21:19 +0100473 bs->total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
bellardd15a7712006-08-06 13:35:09 +0000474 }
bellardea2384d2004-08-01 21:59:26 +0000475#ifndef _WIN32
476 if (bs->is_temporary) {
477 unlink(filename);
478 }
479#endif
Kevin Wolfb783e402010-01-12 12:55:16 +0100480 if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') {
bellardea2384d2004-08-01 21:59:26 +0000481 /* if there is a backing file, use it */
aliguori5eb45632009-03-28 17:55:10 +0000482 BlockDriver *back_drv = NULL;
bellardea2384d2004-08-01 21:59:26 +0000483 bs->backing_hd = bdrv_new("");
bellard83f64092006-08-01 16:21:11 +0000484 path_combine(backing_filename, sizeof(backing_filename),
485 filename, bs->backing_file);
aliguori5eb45632009-03-28 17:55:10 +0000486 if (bs->backing_format[0] != '\0')
487 back_drv = bdrv_find_format(bs->backing_format);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200488
489 /* backing files always opened read-only */
490 open_flags &= ~BDRV_O_RDWR;
Kevin Wolfd6e90982010-03-31 14:40:27 +0200491
492 ret = bdrv_open(bs->backing_hd, backing_filename, open_flags, back_drv);
aliguori51d7c002009-03-05 23:00:29 +0000493 if (ret < 0) {
494 bdrv_close(bs);
495 return ret;
496 }
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200497 if (bs->is_temporary) {
498 bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR);
499 } else {
500 /* base image inherits from "parent" */
501 bs->backing_hd->keep_read_only = bs->keep_read_only;
502 }
bellardea2384d2004-08-01 21:59:26 +0000503 }
504
aliguoribb5fc202009-03-05 23:01:15 +0000505 if (!bdrv_key_required(bs)) {
506 /* call the change callback */
507 bs->media_changed = 1;
508 if (bs->change_cb)
509 bs->change_cb(bs->change_opaque);
510 }
bellardb3380822004-03-14 21:38:54 +0000511 return 0;
Christoph Hellwig69873072010-01-20 18:13:25 +0100512
513free_and_fail:
514 qemu_free(bs->opaque);
515 bs->opaque = NULL;
516 bs->drv = NULL;
517unlink_and_fail:
518 if (bs->is_temporary)
519 unlink(filename);
520 return ret;
bellardfc01f7e2003-06-30 10:03:06 +0000521}
522
523void bdrv_close(BlockDriverState *bs)
524{
bellard19cb3732006-08-19 11:45:59 +0000525 if (bs->drv) {
bellardea2384d2004-08-01 21:59:26 +0000526 if (bs->backing_hd)
527 bdrv_delete(bs->backing_hd);
528 bs->drv->bdrv_close(bs);
529 qemu_free(bs->opaque);
530#ifdef _WIN32
531 if (bs->is_temporary) {
532 unlink(bs->filename);
533 }
bellard67b915a2004-03-31 23:37:16 +0000534#endif
bellardea2384d2004-08-01 21:59:26 +0000535 bs->opaque = NULL;
536 bs->drv = NULL;
bellardb3380822004-03-14 21:38:54 +0000537
538 /* call the change callback */
bellard19cb3732006-08-19 11:45:59 +0000539 bs->media_changed = 1;
bellardb3380822004-03-14 21:38:54 +0000540 if (bs->change_cb)
541 bs->change_cb(bs->change_opaque);
542 }
543}
544
545void bdrv_delete(BlockDriverState *bs)
546{
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +0100547 /* remove from list, if necessary */
548 if (bs->device_name[0] != '\0') {
549 QTAILQ_REMOVE(&bdrv_states, bs, list);
550 }
aurel3234c6f052008-04-08 19:51:21 +0000551
bellardb3380822004-03-14 21:38:54 +0000552 bdrv_close(bs);
553 qemu_free(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000554}
555
aliguorie97fc192009-04-21 23:11:50 +0000556/*
557 * Run consistency checks on an image
558 *
559 * Returns the number of errors or -errno when an internal error occurs
560 */
561int bdrv_check(BlockDriverState *bs)
562{
563 if (bs->drv->bdrv_check == NULL) {
564 return -ENOTSUP;
565 }
566
567 return bs->drv->bdrv_check(bs);
568}
569
bellard33e39632003-07-06 17:15:21 +0000570/* commit COW file into the raw image */
571int bdrv_commit(BlockDriverState *bs)
572{
bellard19cb3732006-08-19 11:45:59 +0000573 BlockDriver *drv = bs->drv;
bellard83f64092006-08-01 16:21:11 +0000574 int64_t i, total_sectors;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200575 int n, j, ro, open_flags;
576 int ret = 0, rw_ret = 0;
bellardea2384d2004-08-01 21:59:26 +0000577 unsigned char sector[512];
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200578 char filename[1024];
579 BlockDriverState *bs_rw, *bs_ro;
bellard33e39632003-07-06 17:15:21 +0000580
bellard19cb3732006-08-19 11:45:59 +0000581 if (!drv)
582 return -ENOMEDIUM;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200583
584 if (!bs->backing_hd) {
585 return -ENOTSUP;
bellard33e39632003-07-06 17:15:21 +0000586 }
587
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200588 if (bs->backing_hd->keep_read_only) {
589 return -EACCES;
590 }
591
592 ro = bs->backing_hd->read_only;
593 strncpy(filename, bs->backing_hd->filename, sizeof(filename));
594 open_flags = bs->backing_hd->open_flags;
595
596 if (ro) {
597 /* re-open as RW */
598 bdrv_delete(bs->backing_hd);
599 bs->backing_hd = NULL;
600 bs_rw = bdrv_new("");
Kevin Wolfd6e90982010-03-31 14:40:27 +0200601 rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR, NULL);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200602 if (rw_ret < 0) {
603 bdrv_delete(bs_rw);
604 /* try to re-open read-only */
605 bs_ro = bdrv_new("");
Kevin Wolfd6e90982010-03-31 14:40:27 +0200606 ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, NULL);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200607 if (ret < 0) {
608 bdrv_delete(bs_ro);
609 /* drive not functional anymore */
610 bs->drv = NULL;
611 return ret;
612 }
613 bs->backing_hd = bs_ro;
614 return rw_ret;
615 }
616 bs->backing_hd = bs_rw;
bellard33e39632003-07-06 17:15:21 +0000617 }
bellardea2384d2004-08-01 21:59:26 +0000618
Jan Kiszka6ea44302009-11-30 18:21:19 +0100619 total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000620 for (i = 0; i < total_sectors;) {
bellard19cb3732006-08-19 11:45:59 +0000621 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
bellardea2384d2004-08-01 21:59:26 +0000622 for(j = 0; j < n; j++) {
623 if (bdrv_read(bs, i, sector, 1) != 0) {
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200624 ret = -EIO;
625 goto ro_cleanup;
bellardea2384d2004-08-01 21:59:26 +0000626 }
627
628 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200629 ret = -EIO;
630 goto ro_cleanup;
bellardea2384d2004-08-01 21:59:26 +0000631 }
632 i++;
633 }
634 } else {
635 i += n;
636 }
637 }
bellard95389c82005-12-18 18:28:15 +0000638
Christoph Hellwig1d449522010-01-17 12:32:30 +0100639 if (drv->bdrv_make_empty) {
640 ret = drv->bdrv_make_empty(bs);
641 bdrv_flush(bs);
642 }
bellard95389c82005-12-18 18:28:15 +0000643
Christoph Hellwig3f5075a2010-01-12 13:49:23 +0100644 /*
645 * Make sure all data we wrote to the backing device is actually
646 * stable on disk.
647 */
648 if (bs->backing_hd)
649 bdrv_flush(bs->backing_hd);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200650
651ro_cleanup:
652
653 if (ro) {
654 /* re-open as RO */
655 bdrv_delete(bs->backing_hd);
656 bs->backing_hd = NULL;
657 bs_ro = bdrv_new("");
Kevin Wolfd6e90982010-03-31 14:40:27 +0200658 ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, NULL);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200659 if (ret < 0) {
660 bdrv_delete(bs_ro);
661 /* drive not functional anymore */
662 bs->drv = NULL;
663 return ret;
664 }
665 bs->backing_hd = bs_ro;
666 bs->backing_hd->keep_read_only = 0;
667 }
668
Christoph Hellwig1d449522010-01-17 12:32:30 +0100669 return ret;
bellard33e39632003-07-06 17:15:21 +0000670}
671
Kevin Wolf756e6732010-01-12 12:55:17 +0100672/*
673 * Return values:
674 * 0 - success
675 * -EINVAL - backing format specified, but no file
676 * -ENOSPC - can't update the backing file because no space is left in the
677 * image file header
678 * -ENOTSUP - format driver doesn't support changing the backing file
679 */
680int bdrv_change_backing_file(BlockDriverState *bs,
681 const char *backing_file, const char *backing_fmt)
682{
683 BlockDriver *drv = bs->drv;
684
685 if (drv->bdrv_change_backing_file != NULL) {
686 return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
687 } else {
688 return -ENOTSUP;
689 }
690}
691
aliguori71d07702009-03-03 17:37:16 +0000692static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
693 size_t size)
694{
695 int64_t len;
696
697 if (!bdrv_is_inserted(bs))
698 return -ENOMEDIUM;
699
700 if (bs->growable)
701 return 0;
702
703 len = bdrv_getlength(bs);
704
Kevin Wolffbb7b4e2009-05-08 14:47:24 +0200705 if (offset < 0)
706 return -EIO;
707
708 if ((offset > len) || (len - offset < size))
aliguori71d07702009-03-03 17:37:16 +0000709 return -EIO;
710
711 return 0;
712}
713
714static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
715 int nb_sectors)
716{
aliguori999dec52009-03-29 01:31:48 +0000717 return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512);
aliguori71d07702009-03-03 17:37:16 +0000718}
719
bellard19cb3732006-08-19 11:45:59 +0000720/* return < 0 if error. See bdrv_write() for the return codes */
ths5fafdf22007-09-16 21:08:06 +0000721int bdrv_read(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000722 uint8_t *buf, int nb_sectors)
723{
bellardea2384d2004-08-01 21:59:26 +0000724 BlockDriver *drv = bs->drv;
725
bellard19cb3732006-08-19 11:45:59 +0000726 if (!drv)
727 return -ENOMEDIUM;
aliguori71d07702009-03-03 17:37:16 +0000728 if (bdrv_check_request(bs, sector_num, nb_sectors))
729 return -EIO;
bellardb3380822004-03-14 21:38:54 +0000730
aliguorieda578e2009-03-12 19:57:16 +0000731 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
bellardfc01f7e2003-06-30 10:03:06 +0000732}
733
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +0200734static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num,
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100735 int nb_sectors, int dirty)
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +0200736{
737 int64_t start, end;
Jan Kiszkac6d22832009-11-30 18:21:20 +0100738 unsigned long val, idx, bit;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100739
Jan Kiszka6ea44302009-11-30 18:21:19 +0100740 start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkac6d22832009-11-30 18:21:20 +0100741 end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100742
743 for (; start <= end; start++) {
Jan Kiszkac6d22832009-11-30 18:21:20 +0100744 idx = start / (sizeof(unsigned long) * 8);
745 bit = start % (sizeof(unsigned long) * 8);
746 val = bs->dirty_bitmap[idx];
747 if (dirty) {
Liran Schouraaa0eb72010-01-26 10:31:48 +0200748 if (!(val & (1 << bit))) {
749 bs->dirty_count++;
750 val |= 1 << bit;
751 }
Jan Kiszkac6d22832009-11-30 18:21:20 +0100752 } else {
Liran Schouraaa0eb72010-01-26 10:31:48 +0200753 if (val & (1 << bit)) {
754 bs->dirty_count--;
755 val &= ~(1 << bit);
756 }
Jan Kiszkac6d22832009-11-30 18:21:20 +0100757 }
758 bs->dirty_bitmap[idx] = val;
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +0200759 }
760}
761
ths5fafdf22007-09-16 21:08:06 +0000762/* Return < 0 if error. Important errors are:
bellard19cb3732006-08-19 11:45:59 +0000763 -EIO generic I/O error (may happen for all errors)
764 -ENOMEDIUM No media inserted.
765 -EINVAL Invalid sector number or nb_sectors
766 -EACCES Trying to write a read-only device
767*/
ths5fafdf22007-09-16 21:08:06 +0000768int bdrv_write(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000769 const uint8_t *buf, int nb_sectors)
770{
bellard83f64092006-08-01 16:21:11 +0000771 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +0000772 if (!bs->drv)
773 return -ENOMEDIUM;
bellard0849bf02003-06-30 23:17:31 +0000774 if (bs->read_only)
bellard19cb3732006-08-19 11:45:59 +0000775 return -EACCES;
aliguori71d07702009-03-03 17:37:16 +0000776 if (bdrv_check_request(bs, sector_num, nb_sectors))
777 return -EIO;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100778
Jan Kiszkac6d22832009-11-30 18:21:20 +0100779 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +0200780 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
781 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100782
aliguori42fb2802009-01-15 20:43:39 +0000783 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
bellard83f64092006-08-01 16:21:11 +0000784}
785
aliguorieda578e2009-03-12 19:57:16 +0000786int bdrv_pread(BlockDriverState *bs, int64_t offset,
787 void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000788{
Jan Kiszka6ea44302009-11-30 18:21:19 +0100789 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
bellard83f64092006-08-01 16:21:11 +0000790 int len, nb_sectors, count;
791 int64_t sector_num;
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100792 int ret;
bellard83f64092006-08-01 16:21:11 +0000793
794 count = count1;
795 /* first read to align to sector start */
Jan Kiszka6ea44302009-11-30 18:21:19 +0100796 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
bellard83f64092006-08-01 16:21:11 +0000797 if (len > count)
798 len = count;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100799 sector_num = offset >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000800 if (len > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100801 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
802 return ret;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100803 memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
bellard83f64092006-08-01 16:21:11 +0000804 count -= len;
805 if (count == 0)
806 return count1;
807 sector_num++;
808 buf += len;
809 }
810
811 /* read the sectors "in place" */
Jan Kiszka6ea44302009-11-30 18:21:19 +0100812 nb_sectors = count >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000813 if (nb_sectors > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100814 if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0)
815 return ret;
bellard83f64092006-08-01 16:21:11 +0000816 sector_num += nb_sectors;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100817 len = nb_sectors << BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000818 buf += len;
819 count -= len;
820 }
821
822 /* add data from the last sector */
823 if (count > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100824 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
825 return ret;
bellard83f64092006-08-01 16:21:11 +0000826 memcpy(buf, tmp_buf, count);
827 }
828 return count1;
829}
830
aliguorieda578e2009-03-12 19:57:16 +0000831int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
832 const void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000833{
Jan Kiszka6ea44302009-11-30 18:21:19 +0100834 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
bellard83f64092006-08-01 16:21:11 +0000835 int len, nb_sectors, count;
836 int64_t sector_num;
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100837 int ret;
bellard83f64092006-08-01 16:21:11 +0000838
839 count = count1;
840 /* first write to align to sector start */
Jan Kiszka6ea44302009-11-30 18:21:19 +0100841 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
bellard83f64092006-08-01 16:21:11 +0000842 if (len > count)
843 len = count;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100844 sector_num = offset >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000845 if (len > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100846 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
847 return ret;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100848 memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len);
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100849 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
850 return ret;
bellard83f64092006-08-01 16:21:11 +0000851 count -= len;
852 if (count == 0)
853 return count1;
854 sector_num++;
855 buf += len;
856 }
857
858 /* write the sectors "in place" */
Jan Kiszka6ea44302009-11-30 18:21:19 +0100859 nb_sectors = count >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000860 if (nb_sectors > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100861 if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0)
862 return ret;
bellard83f64092006-08-01 16:21:11 +0000863 sector_num += nb_sectors;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100864 len = nb_sectors << BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000865 buf += len;
866 count -= len;
867 }
868
869 /* add data from the last sector */
870 if (count > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100871 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
872 return ret;
bellard83f64092006-08-01 16:21:11 +0000873 memcpy(tmp_buf, buf, count);
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100874 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
875 return ret;
bellard83f64092006-08-01 16:21:11 +0000876 }
877 return count1;
878}
bellard83f64092006-08-01 16:21:11 +0000879
880/**
bellard83f64092006-08-01 16:21:11 +0000881 * Truncate file to 'offset' bytes (needed only for file protocols)
882 */
883int bdrv_truncate(BlockDriverState *bs, int64_t offset)
884{
885 BlockDriver *drv = bs->drv;
886 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000887 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000888 if (!drv->bdrv_truncate)
889 return -ENOTSUP;
Naphtali Sprei59f26892009-10-26 16:25:16 +0200890 if (bs->read_only)
891 return -EACCES;
bellard83f64092006-08-01 16:21:11 +0000892 return drv->bdrv_truncate(bs, offset);
893}
894
895/**
896 * Length of a file in bytes. Return < 0 if error or unknown.
897 */
898int64_t bdrv_getlength(BlockDriverState *bs)
899{
900 BlockDriver *drv = bs->drv;
901 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000902 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000903 if (!drv->bdrv_getlength) {
904 /* legacy mode */
Jan Kiszka6ea44302009-11-30 18:21:19 +0100905 return bs->total_sectors * BDRV_SECTOR_SIZE;
bellard83f64092006-08-01 16:21:11 +0000906 }
907 return drv->bdrv_getlength(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000908}
909
bellard19cb3732006-08-19 11:45:59 +0000910/* return 0 as number of sectors if no device present or error */
ths96b8f132007-12-17 01:35:20 +0000911void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
bellardfc01f7e2003-06-30 10:03:06 +0000912{
bellard19cb3732006-08-19 11:45:59 +0000913 int64_t length;
914 length = bdrv_getlength(bs);
915 if (length < 0)
916 length = 0;
917 else
Jan Kiszka6ea44302009-11-30 18:21:19 +0100918 length = length >> BDRV_SECTOR_BITS;
bellard19cb3732006-08-19 11:45:59 +0000919 *nb_sectors_ptr = length;
bellardfc01f7e2003-06-30 10:03:06 +0000920}
bellardcf989512004-02-16 21:56:36 +0000921
aliguorif3d54fc2008-11-25 21:50:24 +0000922struct partition {
923 uint8_t boot_ind; /* 0x80 - active */
924 uint8_t head; /* starting head */
925 uint8_t sector; /* starting sector */
926 uint8_t cyl; /* starting cylinder */
927 uint8_t sys_ind; /* What partition type */
928 uint8_t end_head; /* end head */
929 uint8_t end_sector; /* end sector */
930 uint8_t end_cyl; /* end cylinder */
931 uint32_t start_sect; /* starting sector counting from 0 */
932 uint32_t nr_sects; /* nr of sectors in partition */
933} __attribute__((packed));
934
935/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
936static int guess_disk_lchs(BlockDriverState *bs,
937 int *pcylinders, int *pheads, int *psectors)
938{
939 uint8_t buf[512];
940 int ret, i, heads, sectors, cylinders;
941 struct partition *p;
942 uint32_t nr_sects;
blueswir1a38131b2008-12-05 17:56:40 +0000943 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +0000944
945 bdrv_get_geometry(bs, &nb_sectors);
946
947 ret = bdrv_read(bs, 0, buf, 1);
948 if (ret < 0)
949 return -1;
950 /* test msdos magic */
951 if (buf[510] != 0x55 || buf[511] != 0xaa)
952 return -1;
953 for(i = 0; i < 4; i++) {
954 p = ((struct partition *)(buf + 0x1be)) + i;
955 nr_sects = le32_to_cpu(p->nr_sects);
956 if (nr_sects && p->end_head) {
957 /* We make the assumption that the partition terminates on
958 a cylinder boundary */
959 heads = p->end_head + 1;
960 sectors = p->end_sector & 63;
961 if (sectors == 0)
962 continue;
963 cylinders = nb_sectors / (heads * sectors);
964 if (cylinders < 1 || cylinders > 16383)
965 continue;
966 *pheads = heads;
967 *psectors = sectors;
968 *pcylinders = cylinders;
969#if 0
970 printf("guessed geometry: LCHS=%d %d %d\n",
971 cylinders, heads, sectors);
972#endif
973 return 0;
974 }
975 }
976 return -1;
977}
978
979void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
980{
981 int translation, lba_detected = 0;
982 int cylinders, heads, secs;
blueswir1a38131b2008-12-05 17:56:40 +0000983 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +0000984
985 /* if a geometry hint is available, use it */
986 bdrv_get_geometry(bs, &nb_sectors);
987 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
988 translation = bdrv_get_translation_hint(bs);
989 if (cylinders != 0) {
990 *pcyls = cylinders;
991 *pheads = heads;
992 *psecs = secs;
993 } else {
994 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
995 if (heads > 16) {
996 /* if heads > 16, it means that a BIOS LBA
997 translation was active, so the default
998 hardware geometry is OK */
999 lba_detected = 1;
1000 goto default_geometry;
1001 } else {
1002 *pcyls = cylinders;
1003 *pheads = heads;
1004 *psecs = secs;
1005 /* disable any translation to be in sync with
1006 the logical geometry */
1007 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
1008 bdrv_set_translation_hint(bs,
1009 BIOS_ATA_TRANSLATION_NONE);
1010 }
1011 }
1012 } else {
1013 default_geometry:
1014 /* if no geometry, use a standard physical disk geometry */
1015 cylinders = nb_sectors / (16 * 63);
1016
1017 if (cylinders > 16383)
1018 cylinders = 16383;
1019 else if (cylinders < 2)
1020 cylinders = 2;
1021 *pcyls = cylinders;
1022 *pheads = 16;
1023 *psecs = 63;
1024 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
1025 if ((*pcyls * *pheads) <= 131072) {
1026 bdrv_set_translation_hint(bs,
1027 BIOS_ATA_TRANSLATION_LARGE);
1028 } else {
1029 bdrv_set_translation_hint(bs,
1030 BIOS_ATA_TRANSLATION_LBA);
1031 }
1032 }
1033 }
1034 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
1035 }
1036}
1037
ths5fafdf22007-09-16 21:08:06 +00001038void bdrv_set_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +00001039 int cyls, int heads, int secs)
1040{
1041 bs->cyls = cyls;
1042 bs->heads = heads;
1043 bs->secs = secs;
1044}
1045
1046void bdrv_set_type_hint(BlockDriverState *bs, int type)
1047{
1048 bs->type = type;
1049 bs->removable = ((type == BDRV_TYPE_CDROM ||
1050 type == BDRV_TYPE_FLOPPY));
1051}
1052
bellard46d47672004-11-16 01:45:27 +00001053void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
1054{
1055 bs->translation = translation;
1056}
1057
ths5fafdf22007-09-16 21:08:06 +00001058void bdrv_get_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +00001059 int *pcyls, int *pheads, int *psecs)
1060{
1061 *pcyls = bs->cyls;
1062 *pheads = bs->heads;
1063 *psecs = bs->secs;
1064}
1065
1066int bdrv_get_type_hint(BlockDriverState *bs)
1067{
1068 return bs->type;
1069}
1070
bellard46d47672004-11-16 01:45:27 +00001071int bdrv_get_translation_hint(BlockDriverState *bs)
1072{
1073 return bs->translation;
1074}
1075
bellardb3380822004-03-14 21:38:54 +00001076int bdrv_is_removable(BlockDriverState *bs)
1077{
1078 return bs->removable;
1079}
1080
1081int bdrv_is_read_only(BlockDriverState *bs)
1082{
1083 return bs->read_only;
1084}
1085
ths985a03b2007-12-24 16:10:43 +00001086int bdrv_is_sg(BlockDriverState *bs)
1087{
1088 return bs->sg;
1089}
1090
Christoph Hellwige900a7b2009-09-04 19:01:15 +02001091int bdrv_enable_write_cache(BlockDriverState *bs)
1092{
1093 return bs->enable_write_cache;
1094}
1095
bellard19cb3732006-08-19 11:45:59 +00001096/* XXX: no longer used */
ths5fafdf22007-09-16 21:08:06 +00001097void bdrv_set_change_cb(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +00001098 void (*change_cb)(void *opaque), void *opaque)
1099{
1100 bs->change_cb = change_cb;
1101 bs->change_opaque = opaque;
1102}
1103
bellardea2384d2004-08-01 21:59:26 +00001104int bdrv_is_encrypted(BlockDriverState *bs)
1105{
1106 if (bs->backing_hd && bs->backing_hd->encrypted)
1107 return 1;
1108 return bs->encrypted;
1109}
1110
aliguoric0f4ce72009-03-05 23:01:01 +00001111int bdrv_key_required(BlockDriverState *bs)
1112{
1113 BlockDriverState *backing_hd = bs->backing_hd;
1114
1115 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
1116 return 1;
1117 return (bs->encrypted && !bs->valid_key);
1118}
1119
bellardea2384d2004-08-01 21:59:26 +00001120int bdrv_set_key(BlockDriverState *bs, const char *key)
1121{
1122 int ret;
1123 if (bs->backing_hd && bs->backing_hd->encrypted) {
1124 ret = bdrv_set_key(bs->backing_hd, key);
1125 if (ret < 0)
1126 return ret;
1127 if (!bs->encrypted)
1128 return 0;
1129 }
Shahar Havivifd04a2a2010-03-06 00:26:13 +02001130 if (!bs->encrypted) {
1131 return -EINVAL;
1132 } else if (!bs->drv || !bs->drv->bdrv_set_key) {
1133 return -ENOMEDIUM;
1134 }
aliguoric0f4ce72009-03-05 23:01:01 +00001135 ret = bs->drv->bdrv_set_key(bs, key);
aliguoribb5fc202009-03-05 23:01:15 +00001136 if (ret < 0) {
1137 bs->valid_key = 0;
1138 } else if (!bs->valid_key) {
1139 bs->valid_key = 1;
1140 /* call the change callback now, we skipped it on open */
1141 bs->media_changed = 1;
1142 if (bs->change_cb)
1143 bs->change_cb(bs->change_opaque);
1144 }
aliguoric0f4ce72009-03-05 23:01:01 +00001145 return ret;
bellardea2384d2004-08-01 21:59:26 +00001146}
1147
1148void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
1149{
bellard19cb3732006-08-19 11:45:59 +00001150 if (!bs->drv) {
bellardea2384d2004-08-01 21:59:26 +00001151 buf[0] = '\0';
1152 } else {
1153 pstrcpy(buf, buf_size, bs->drv->format_name);
1154 }
1155}
1156
ths5fafdf22007-09-16 21:08:06 +00001157void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
bellardea2384d2004-08-01 21:59:26 +00001158 void *opaque)
1159{
1160 BlockDriver *drv;
1161
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +01001162 QLIST_FOREACH(drv, &bdrv_drivers, list) {
bellardea2384d2004-08-01 21:59:26 +00001163 it(opaque, drv->format_name);
1164 }
1165}
1166
bellardb3380822004-03-14 21:38:54 +00001167BlockDriverState *bdrv_find(const char *name)
1168{
1169 BlockDriverState *bs;
1170
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001171 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1172 if (!strcmp(name, bs->device_name)) {
bellardb3380822004-03-14 21:38:54 +00001173 return bs;
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001174 }
bellardb3380822004-03-14 21:38:54 +00001175 }
1176 return NULL;
1177}
1178
aliguori51de9762009-03-05 23:00:43 +00001179void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
bellard81d09122004-07-14 17:21:37 +00001180{
1181 BlockDriverState *bs;
1182
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001183 QTAILQ_FOREACH(bs, &bdrv_states, list) {
aliguori51de9762009-03-05 23:00:43 +00001184 it(opaque, bs);
bellard81d09122004-07-14 17:21:37 +00001185 }
1186}
1187
bellardea2384d2004-08-01 21:59:26 +00001188const char *bdrv_get_device_name(BlockDriverState *bs)
1189{
1190 return bs->device_name;
1191}
1192
pbrook7a6cba62006-06-04 11:39:07 +00001193void bdrv_flush(BlockDriverState *bs)
1194{
Christoph Hellwig3f5075a2010-01-12 13:49:23 +01001195 if (bs->drv && bs->drv->bdrv_flush)
pbrook7a6cba62006-06-04 11:39:07 +00001196 bs->drv->bdrv_flush(bs);
pbrook7a6cba62006-06-04 11:39:07 +00001197}
1198
aliguoric6ca28d2008-10-06 13:55:43 +00001199void bdrv_flush_all(void)
1200{
1201 BlockDriverState *bs;
1202
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001203 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1204 if (bs->drv && !bdrv_is_read_only(bs) &&
1205 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) {
aliguoric6ca28d2008-10-06 13:55:43 +00001206 bdrv_flush(bs);
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001207 }
1208 }
aliguoric6ca28d2008-10-06 13:55:43 +00001209}
1210
thsf58c7b32008-06-05 21:53:49 +00001211/*
1212 * Returns true iff the specified sector is present in the disk image. Drivers
1213 * not implementing the functionality are assumed to not support backing files,
1214 * hence all their sectors are reported as allocated.
1215 *
1216 * 'pnum' is set to the number of sectors (including and immediately following
1217 * the specified sector) that are known to be in the same
1218 * allocated/unallocated state.
1219 *
1220 * 'nb_sectors' is the max value 'pnum' should be set to.
1221 */
1222int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1223 int *pnum)
1224{
1225 int64_t n;
1226 if (!bs->drv->bdrv_is_allocated) {
1227 if (sector_num >= bs->total_sectors) {
1228 *pnum = 0;
1229 return 0;
1230 }
1231 n = bs->total_sectors - sector_num;
1232 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1233 return 1;
1234 }
1235 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1236}
1237
Luiz Capitulino2582bfe2010-02-03 12:41:01 -02001238void bdrv_mon_event(const BlockDriverState *bdrv,
1239 BlockMonEventAction action, int is_read)
1240{
1241 QObject *data;
1242 const char *action_str;
1243
1244 switch (action) {
1245 case BDRV_ACTION_REPORT:
1246 action_str = "report";
1247 break;
1248 case BDRV_ACTION_IGNORE:
1249 action_str = "ignore";
1250 break;
1251 case BDRV_ACTION_STOP:
1252 action_str = "stop";
1253 break;
1254 default:
1255 abort();
1256 }
1257
1258 data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
1259 bdrv->device_name,
1260 action_str,
1261 is_read ? "read" : "write");
1262 monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data);
1263
1264 qobject_decref(data);
1265}
1266
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001267static void bdrv_print_dict(QObject *obj, void *opaque)
bellardb3380822004-03-14 21:38:54 +00001268{
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001269 QDict *bs_dict;
1270 Monitor *mon = opaque;
1271
1272 bs_dict = qobject_to_qdict(obj);
1273
1274 monitor_printf(mon, "%s: type=%s removable=%d",
1275 qdict_get_str(bs_dict, "device"),
1276 qdict_get_str(bs_dict, "type"),
1277 qdict_get_bool(bs_dict, "removable"));
1278
1279 if (qdict_get_bool(bs_dict, "removable")) {
1280 monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked"));
1281 }
1282
1283 if (qdict_haskey(bs_dict, "inserted")) {
1284 QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted"));
1285
1286 monitor_printf(mon, " file=");
1287 monitor_print_filename(mon, qdict_get_str(qdict, "file"));
1288 if (qdict_haskey(qdict, "backing_file")) {
1289 monitor_printf(mon, " backing_file=");
1290 monitor_print_filename(mon, qdict_get_str(qdict, "backing_file"));
1291 }
1292 monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
1293 qdict_get_bool(qdict, "ro"),
1294 qdict_get_str(qdict, "drv"),
1295 qdict_get_bool(qdict, "encrypted"));
1296 } else {
1297 monitor_printf(mon, " [not inserted]");
1298 }
1299
1300 monitor_printf(mon, "\n");
1301}
1302
1303void bdrv_info_print(Monitor *mon, const QObject *data)
1304{
1305 qlist_iter(qobject_to_qlist(data), bdrv_print_dict, mon);
1306}
1307
1308/**
1309 * bdrv_info(): Block devices information
1310 *
1311 * Each block device information is stored in a QDict and the
1312 * returned QObject is a QList of all devices.
1313 *
1314 * The QDict contains the following:
1315 *
1316 * - "device": device name
1317 * - "type": device type
1318 * - "removable": true if the device is removable, false otherwise
1319 * - "locked": true if the device is locked, false otherwise
1320 * - "inserted": only present if the device is inserted, it is a QDict
1321 * containing the following:
1322 * - "file": device file name
1323 * - "ro": true if read-only, false otherwise
1324 * - "drv": driver format name
1325 * - "backing_file": backing file name if one is used
1326 * - "encrypted": true if encrypted, false otherwise
1327 *
1328 * Example:
1329 *
1330 * [ { "device": "ide0-hd0", "type": "hd", "removable": false, "locked": false,
1331 * "inserted": { "file": "/tmp/foobar", "ro": false, "drv": "qcow2" } },
1332 * { "device": "floppy0", "type": "floppy", "removable": true,
1333 * "locked": false } ]
1334 */
1335void bdrv_info(Monitor *mon, QObject **ret_data)
1336{
1337 QList *bs_list;
bellardb3380822004-03-14 21:38:54 +00001338 BlockDriverState *bs;
1339
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001340 bs_list = qlist_new();
1341
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001342 QTAILQ_FOREACH(bs, &bdrv_states, list) {
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001343 QObject *bs_obj;
1344 const char *type = "unknown";
1345
bellardb3380822004-03-14 21:38:54 +00001346 switch(bs->type) {
1347 case BDRV_TYPE_HD:
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001348 type = "hd";
bellardb3380822004-03-14 21:38:54 +00001349 break;
1350 case BDRV_TYPE_CDROM:
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001351 type = "cdrom";
bellardb3380822004-03-14 21:38:54 +00001352 break;
1353 case BDRV_TYPE_FLOPPY:
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001354 type = "floppy";
bellardb3380822004-03-14 21:38:54 +00001355 break;
1356 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001357
1358 bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': %s, "
1359 "'removable': %i, 'locked': %i }",
1360 bs->device_name, type, bs->removable,
1361 bs->locked);
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001362
bellard19cb3732006-08-19 11:45:59 +00001363 if (bs->drv) {
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001364 QObject *obj;
1365 QDict *bs_dict = qobject_to_qdict(bs_obj);
1366
1367 obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, "
1368 "'encrypted': %i }",
1369 bs->filename, bs->read_only,
1370 bs->drv->format_name,
1371 bdrv_is_encrypted(bs));
thsfef30742006-12-22 14:11:32 +00001372 if (bs->backing_file[0] != '\0') {
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001373 QDict *qdict = qobject_to_qdict(obj);
1374 qdict_put(qdict, "backing_file",
1375 qstring_from_str(bs->backing_file));
aliguori376253e2009-03-05 23:01:23 +00001376 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001377
1378 qdict_put_obj(bs_dict, "inserted", obj);
bellardb3380822004-03-14 21:38:54 +00001379 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001380 qlist_append_obj(bs_list, bs_obj);
bellardb3380822004-03-14 21:38:54 +00001381 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001382
1383 *ret_data = QOBJECT(bs_list);
bellardb3380822004-03-14 21:38:54 +00001384}
thsa36e69d2007-12-02 05:18:19 +00001385
Luiz Capitulino218a5362009-12-10 17:16:07 -02001386static void bdrv_stats_iter(QObject *data, void *opaque)
thsa36e69d2007-12-02 05:18:19 +00001387{
Luiz Capitulino218a5362009-12-10 17:16:07 -02001388 QDict *qdict;
1389 Monitor *mon = opaque;
1390
1391 qdict = qobject_to_qdict(data);
1392 monitor_printf(mon, "%s:", qdict_get_str(qdict, "device"));
1393
1394 qdict = qobject_to_qdict(qdict_get(qdict, "stats"));
1395 monitor_printf(mon, " rd_bytes=%" PRId64
1396 " wr_bytes=%" PRId64
1397 " rd_operations=%" PRId64
1398 " wr_operations=%" PRId64
1399 "\n",
1400 qdict_get_int(qdict, "rd_bytes"),
1401 qdict_get_int(qdict, "wr_bytes"),
1402 qdict_get_int(qdict, "rd_operations"),
1403 qdict_get_int(qdict, "wr_operations"));
1404}
1405
1406void bdrv_stats_print(Monitor *mon, const QObject *data)
1407{
1408 qlist_iter(qobject_to_qlist(data), bdrv_stats_iter, mon);
1409}
1410
1411/**
1412 * bdrv_info_stats(): show block device statistics
1413 *
1414 * Each device statistic information is stored in a QDict and
1415 * the returned QObject is a QList of all devices.
1416 *
1417 * The QDict contains the following:
1418 *
1419 * - "device": device name
1420 * - "stats": A QDict with the statistics information, it contains:
1421 * - "rd_bytes": bytes read
1422 * - "wr_bytes": bytes written
1423 * - "rd_operations": read operations
1424 * - "wr_operations": write operations
1425 *
1426 * Example:
1427 *
1428 * [ { "device": "ide0-hd0",
1429 * "stats": { "rd_bytes": 512,
1430 * "wr_bytes": 0,
1431 * "rd_operations": 1,
1432 * "wr_operations": 0 } },
1433 * { "device": "ide1-cd0",
1434 * "stats": { "rd_bytes": 0,
1435 * "wr_bytes": 0,
1436 * "rd_operations": 0,
1437 * "wr_operations": 0 } } ]
1438 */
1439void bdrv_info_stats(Monitor *mon, QObject **ret_data)
1440{
1441 QObject *obj;
1442 QList *devices;
thsa36e69d2007-12-02 05:18:19 +00001443 BlockDriverState *bs;
1444
Luiz Capitulino218a5362009-12-10 17:16:07 -02001445 devices = qlist_new();
1446
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001447 QTAILQ_FOREACH(bs, &bdrv_states, list) {
Luiz Capitulino218a5362009-12-10 17:16:07 -02001448 obj = qobject_from_jsonf("{ 'device': %s, 'stats': {"
1449 "'rd_bytes': %" PRId64 ","
1450 "'wr_bytes': %" PRId64 ","
1451 "'rd_operations': %" PRId64 ","
1452 "'wr_operations': %" PRId64
1453 "} }",
1454 bs->device_name,
1455 bs->rd_bytes, bs->wr_bytes,
1456 bs->rd_ops, bs->wr_ops);
Luiz Capitulino218a5362009-12-10 17:16:07 -02001457 qlist_append_obj(devices, obj);
thsa36e69d2007-12-02 05:18:19 +00001458 }
Luiz Capitulino218a5362009-12-10 17:16:07 -02001459
1460 *ret_data = QOBJECT(devices);
thsa36e69d2007-12-02 05:18:19 +00001461}
bellardea2384d2004-08-01 21:59:26 +00001462
aliguori045df332009-03-05 23:00:48 +00001463const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1464{
1465 if (bs->backing_hd && bs->backing_hd->encrypted)
1466 return bs->backing_file;
1467 else if (bs->encrypted)
1468 return bs->filename;
1469 else
1470 return NULL;
1471}
1472
ths5fafdf22007-09-16 21:08:06 +00001473void bdrv_get_backing_filename(BlockDriverState *bs,
bellard83f64092006-08-01 16:21:11 +00001474 char *filename, int filename_size)
bellardea2384d2004-08-01 21:59:26 +00001475{
Kevin Wolfb783e402010-01-12 12:55:16 +01001476 if (!bs->backing_file) {
bellard83f64092006-08-01 16:21:11 +00001477 pstrcpy(filename, filename_size, "");
1478 } else {
1479 pstrcpy(filename, filename_size, bs->backing_file);
1480 }
bellardea2384d2004-08-01 21:59:26 +00001481}
1482
ths5fafdf22007-09-16 21:08:06 +00001483int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
bellardfaea38e2006-08-05 21:31:00 +00001484 const uint8_t *buf, int nb_sectors)
1485{
1486 BlockDriver *drv = bs->drv;
1487 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001488 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001489 if (!drv->bdrv_write_compressed)
1490 return -ENOTSUP;
Kevin Wolffbb7b4e2009-05-08 14:47:24 +02001491 if (bdrv_check_request(bs, sector_num, nb_sectors))
1492 return -EIO;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001493
Jan Kiszkac6d22832009-11-30 18:21:20 +01001494 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001495 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1496 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001497
bellardfaea38e2006-08-05 21:31:00 +00001498 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1499}
ths3b46e622007-09-17 08:09:54 +00001500
bellardfaea38e2006-08-05 21:31:00 +00001501int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1502{
1503 BlockDriver *drv = bs->drv;
1504 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001505 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001506 if (!drv->bdrv_get_info)
1507 return -ENOTSUP;
1508 memset(bdi, 0, sizeof(*bdi));
1509 return drv->bdrv_get_info(bs, bdi);
1510}
1511
Christoph Hellwig45566e92009-07-10 23:11:57 +02001512int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
1513 int64_t pos, int size)
aliguori178e08a2009-04-05 19:10:55 +00001514{
1515 BlockDriver *drv = bs->drv;
1516 if (!drv)
1517 return -ENOMEDIUM;
Christoph Hellwig45566e92009-07-10 23:11:57 +02001518 if (!drv->bdrv_save_vmstate)
aliguori178e08a2009-04-05 19:10:55 +00001519 return -ENOTSUP;
Christoph Hellwig45566e92009-07-10 23:11:57 +02001520 return drv->bdrv_save_vmstate(bs, buf, pos, size);
aliguori178e08a2009-04-05 19:10:55 +00001521}
1522
Christoph Hellwig45566e92009-07-10 23:11:57 +02001523int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1524 int64_t pos, int size)
aliguori178e08a2009-04-05 19:10:55 +00001525{
1526 BlockDriver *drv = bs->drv;
1527 if (!drv)
1528 return -ENOMEDIUM;
Christoph Hellwig45566e92009-07-10 23:11:57 +02001529 if (!drv->bdrv_load_vmstate)
aliguori178e08a2009-04-05 19:10:55 +00001530 return -ENOTSUP;
Christoph Hellwig45566e92009-07-10 23:11:57 +02001531 return drv->bdrv_load_vmstate(bs, buf, pos, size);
aliguori178e08a2009-04-05 19:10:55 +00001532}
1533
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01001534void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
1535{
1536 BlockDriver *drv = bs->drv;
1537
1538 if (!drv || !drv->bdrv_debug_event) {
1539 return;
1540 }
1541
1542 return drv->bdrv_debug_event(bs, event);
1543
1544}
1545
bellardfaea38e2006-08-05 21:31:00 +00001546/**************************************************************/
1547/* handling of snapshots */
1548
ths5fafdf22007-09-16 21:08:06 +00001549int bdrv_snapshot_create(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001550 QEMUSnapshotInfo *sn_info)
1551{
1552 BlockDriver *drv = bs->drv;
1553 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001554 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001555 if (!drv->bdrv_snapshot_create)
1556 return -ENOTSUP;
1557 return drv->bdrv_snapshot_create(bs, sn_info);
1558}
1559
ths5fafdf22007-09-16 21:08:06 +00001560int bdrv_snapshot_goto(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001561 const char *snapshot_id)
1562{
1563 BlockDriver *drv = bs->drv;
1564 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001565 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001566 if (!drv->bdrv_snapshot_goto)
1567 return -ENOTSUP;
1568 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1569}
1570
1571int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1572{
1573 BlockDriver *drv = bs->drv;
1574 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001575 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001576 if (!drv->bdrv_snapshot_delete)
1577 return -ENOTSUP;
1578 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1579}
1580
ths5fafdf22007-09-16 21:08:06 +00001581int bdrv_snapshot_list(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001582 QEMUSnapshotInfo **psn_info)
1583{
1584 BlockDriver *drv = bs->drv;
1585 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001586 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001587 if (!drv->bdrv_snapshot_list)
1588 return -ENOTSUP;
1589 return drv->bdrv_snapshot_list(bs, psn_info);
1590}
1591
1592#define NB_SUFFIXES 4
1593
1594char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1595{
1596 static const char suffixes[NB_SUFFIXES] = "KMGT";
1597 int64_t base;
1598 int i;
1599
1600 if (size <= 999) {
1601 snprintf(buf, buf_size, "%" PRId64, size);
1602 } else {
1603 base = 1024;
1604 for(i = 0; i < NB_SUFFIXES; i++) {
1605 if (size < (10 * base)) {
ths5fafdf22007-09-16 21:08:06 +00001606 snprintf(buf, buf_size, "%0.1f%c",
bellardfaea38e2006-08-05 21:31:00 +00001607 (double)size / base,
1608 suffixes[i]);
1609 break;
1610 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
ths5fafdf22007-09-16 21:08:06 +00001611 snprintf(buf, buf_size, "%" PRId64 "%c",
bellardfaea38e2006-08-05 21:31:00 +00001612 ((size + (base >> 1)) / base),
1613 suffixes[i]);
1614 break;
1615 }
1616 base = base * 1024;
1617 }
1618 }
1619 return buf;
1620}
1621
1622char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1623{
1624 char buf1[128], date_buf[128], clock_buf[128];
bellard3b9f94e2007-01-07 17:27:07 +00001625#ifdef _WIN32
1626 struct tm *ptm;
1627#else
bellardfaea38e2006-08-05 21:31:00 +00001628 struct tm tm;
bellard3b9f94e2007-01-07 17:27:07 +00001629#endif
bellardfaea38e2006-08-05 21:31:00 +00001630 time_t ti;
1631 int64_t secs;
1632
1633 if (!sn) {
ths5fafdf22007-09-16 21:08:06 +00001634 snprintf(buf, buf_size,
1635 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001636 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1637 } else {
1638 ti = sn->date_sec;
bellard3b9f94e2007-01-07 17:27:07 +00001639#ifdef _WIN32
1640 ptm = localtime(&ti);
1641 strftime(date_buf, sizeof(date_buf),
1642 "%Y-%m-%d %H:%M:%S", ptm);
1643#else
bellardfaea38e2006-08-05 21:31:00 +00001644 localtime_r(&ti, &tm);
1645 strftime(date_buf, sizeof(date_buf),
1646 "%Y-%m-%d %H:%M:%S", &tm);
bellard3b9f94e2007-01-07 17:27:07 +00001647#endif
bellardfaea38e2006-08-05 21:31:00 +00001648 secs = sn->vm_clock_nsec / 1000000000;
1649 snprintf(clock_buf, sizeof(clock_buf),
1650 "%02d:%02d:%02d.%03d",
1651 (int)(secs / 3600),
1652 (int)((secs / 60) % 60),
ths5fafdf22007-09-16 21:08:06 +00001653 (int)(secs % 60),
bellardfaea38e2006-08-05 21:31:00 +00001654 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1655 snprintf(buf, buf_size,
ths5fafdf22007-09-16 21:08:06 +00001656 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001657 sn->id_str, sn->name,
1658 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1659 date_buf,
1660 clock_buf);
1661 }
1662 return buf;
1663}
1664
bellardea2384d2004-08-01 21:59:26 +00001665
bellard83f64092006-08-01 16:21:11 +00001666/**************************************************************/
1667/* async I/Os */
1668
aliguori3b69e4b2009-01-22 16:59:24 +00001669BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
aliguorif141eaf2009-04-07 18:43:24 +00001670 QEMUIOVector *qiov, int nb_sectors,
aliguori3b69e4b2009-01-22 16:59:24 +00001671 BlockDriverCompletionFunc *cb, void *opaque)
1672{
bellard83f64092006-08-01 16:21:11 +00001673 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001674 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001675
bellard19cb3732006-08-19 11:45:59 +00001676 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001677 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001678 if (bdrv_check_request(bs, sector_num, nb_sectors))
1679 return NULL;
ths3b46e622007-09-17 08:09:54 +00001680
aliguorif141eaf2009-04-07 18:43:24 +00001681 ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
1682 cb, opaque);
thsa36e69d2007-12-02 05:18:19 +00001683
1684 if (ret) {
1685 /* Update stats even though technically transfer has not happened. */
Jan Kiszka6ea44302009-11-30 18:21:19 +01001686 bs->rd_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
thsa36e69d2007-12-02 05:18:19 +00001687 bs->rd_ops ++;
1688 }
1689
1690 return ret;
bellard83f64092006-08-01 16:21:11 +00001691}
1692
aliguorif141eaf2009-04-07 18:43:24 +00001693BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1694 QEMUIOVector *qiov, int nb_sectors,
1695 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001696{
bellard83f64092006-08-01 16:21:11 +00001697 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001698 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001699
bellard19cb3732006-08-19 11:45:59 +00001700 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001701 return NULL;
bellard83f64092006-08-01 16:21:11 +00001702 if (bs->read_only)
pbrookce1a14d2006-08-07 02:38:06 +00001703 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001704 if (bdrv_check_request(bs, sector_num, nb_sectors))
1705 return NULL;
bellard83f64092006-08-01 16:21:11 +00001706
Jan Kiszkac6d22832009-11-30 18:21:20 +01001707 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001708 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1709 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001710
aliguorif141eaf2009-04-07 18:43:24 +00001711 ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
1712 cb, opaque);
thsa36e69d2007-12-02 05:18:19 +00001713
1714 if (ret) {
1715 /* Update stats even though technically transfer has not happened. */
Jan Kiszka6ea44302009-11-30 18:21:19 +01001716 bs->wr_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
thsa36e69d2007-12-02 05:18:19 +00001717 bs->wr_ops ++;
1718 }
1719
1720 return ret;
bellard83f64092006-08-01 16:21:11 +00001721}
1722
Kevin Wolf40b4f532009-09-09 17:53:37 +02001723
1724typedef struct MultiwriteCB {
1725 int error;
1726 int num_requests;
1727 int num_callbacks;
1728 struct {
1729 BlockDriverCompletionFunc *cb;
1730 void *opaque;
1731 QEMUIOVector *free_qiov;
1732 void *free_buf;
1733 } callbacks[];
1734} MultiwriteCB;
1735
1736static void multiwrite_user_cb(MultiwriteCB *mcb)
1737{
1738 int i;
1739
1740 for (i = 0; i < mcb->num_callbacks; i++) {
1741 mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
Stefan Hajnoczi1e1ea482010-04-21 20:35:45 +01001742 if (mcb->callbacks[i].free_qiov) {
1743 qemu_iovec_destroy(mcb->callbacks[i].free_qiov);
1744 }
Kevin Wolf40b4f532009-09-09 17:53:37 +02001745 qemu_free(mcb->callbacks[i].free_qiov);
Herve Poussineauf8a83242010-01-24 21:23:56 +00001746 qemu_vfree(mcb->callbacks[i].free_buf);
Kevin Wolf40b4f532009-09-09 17:53:37 +02001747 }
1748}
1749
1750static void multiwrite_cb(void *opaque, int ret)
1751{
1752 MultiwriteCB *mcb = opaque;
1753
Kevin Wolfcb6d3ca2010-04-01 22:48:44 +02001754 if (ret < 0 && !mcb->error) {
Kevin Wolf40b4f532009-09-09 17:53:37 +02001755 mcb->error = ret;
1756 multiwrite_user_cb(mcb);
1757 }
1758
1759 mcb->num_requests--;
1760 if (mcb->num_requests == 0) {
1761 if (mcb->error == 0) {
1762 multiwrite_user_cb(mcb);
1763 }
1764 qemu_free(mcb);
1765 }
1766}
1767
1768static int multiwrite_req_compare(const void *a, const void *b)
1769{
1770 return (((BlockRequest*) a)->sector - ((BlockRequest*) b)->sector);
1771}
1772
1773/*
1774 * Takes a bunch of requests and tries to merge them. Returns the number of
1775 * requests that remain after merging.
1776 */
1777static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
1778 int num_reqs, MultiwriteCB *mcb)
1779{
1780 int i, outidx;
1781
1782 // Sort requests by start sector
1783 qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
1784
1785 // Check if adjacent requests touch the same clusters. If so, combine them,
1786 // filling up gaps with zero sectors.
1787 outidx = 0;
1788 for (i = 1; i < num_reqs; i++) {
1789 int merge = 0;
1790 int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
1791
1792 // This handles the cases that are valid for all block drivers, namely
1793 // exactly sequential writes and overlapping writes.
1794 if (reqs[i].sector <= oldreq_last) {
1795 merge = 1;
1796 }
1797
1798 // The block driver may decide that it makes sense to combine requests
1799 // even if there is a gap of some sectors between them. In this case,
1800 // the gap is filled with zeros (therefore only applicable for yet
1801 // unused space in format like qcow2).
1802 if (!merge && bs->drv->bdrv_merge_requests) {
1803 merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]);
1804 }
1805
Christoph Hellwige2a305f2010-01-26 14:49:08 +01001806 if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) {
1807 merge = 0;
1808 }
1809
Kevin Wolf40b4f532009-09-09 17:53:37 +02001810 if (merge) {
1811 size_t size;
1812 QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov));
1813 qemu_iovec_init(qiov,
1814 reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
1815
1816 // Add the first request to the merged one. If the requests are
1817 // overlapping, drop the last sectors of the first request.
1818 size = (reqs[i].sector - reqs[outidx].sector) << 9;
1819 qemu_iovec_concat(qiov, reqs[outidx].qiov, size);
1820
1821 // We might need to add some zeros between the two requests
1822 if (reqs[i].sector > oldreq_last) {
1823 size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
1824 uint8_t *buf = qemu_blockalign(bs, zero_bytes);
1825 memset(buf, 0, zero_bytes);
1826 qemu_iovec_add(qiov, buf, zero_bytes);
1827 mcb->callbacks[i].free_buf = buf;
1828 }
1829
1830 // Add the second request
1831 qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size);
1832
1833 reqs[outidx].nb_sectors += reqs[i].nb_sectors;
1834 reqs[outidx].qiov = qiov;
1835
1836 mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
1837 } else {
1838 outidx++;
1839 reqs[outidx].sector = reqs[i].sector;
1840 reqs[outidx].nb_sectors = reqs[i].nb_sectors;
1841 reqs[outidx].qiov = reqs[i].qiov;
1842 }
1843 }
1844
1845 return outidx + 1;
1846}
1847
1848/*
1849 * Submit multiple AIO write requests at once.
1850 *
1851 * On success, the function returns 0 and all requests in the reqs array have
1852 * been submitted. In error case this function returns -1, and any of the
1853 * requests may or may not be submitted yet. In particular, this means that the
1854 * callback will be called for some of the requests, for others it won't. The
1855 * caller must check the error field of the BlockRequest to wait for the right
1856 * callbacks (if error != 0, no callback will be called).
1857 *
1858 * The implementation may modify the contents of the reqs array, e.g. to merge
1859 * requests. However, the fields opaque and error are left unmodified as they
1860 * are used to signal failure for a single request to the caller.
1861 */
1862int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
1863{
1864 BlockDriverAIOCB *acb;
1865 MultiwriteCB *mcb;
1866 int i;
1867
1868 if (num_reqs == 0) {
1869 return 0;
1870 }
1871
1872 // Create MultiwriteCB structure
1873 mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
1874 mcb->num_requests = 0;
1875 mcb->num_callbacks = num_reqs;
1876
1877 for (i = 0; i < num_reqs; i++) {
1878 mcb->callbacks[i].cb = reqs[i].cb;
1879 mcb->callbacks[i].opaque = reqs[i].opaque;
1880 }
1881
1882 // Check for mergable requests
1883 num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
1884
1885 // Run the aio requests
1886 for (i = 0; i < num_reqs; i++) {
1887 acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
1888 reqs[i].nb_sectors, multiwrite_cb, mcb);
1889
1890 if (acb == NULL) {
1891 // We can only fail the whole thing if no request has been
1892 // submitted yet. Otherwise we'll wait for the submitted AIOs to
1893 // complete and report the error in the callback.
1894 if (mcb->num_requests == 0) {
Kevin Wolf0f0b6042010-04-06 18:24:06 +02001895 reqs[i].error = -EIO;
Kevin Wolf40b4f532009-09-09 17:53:37 +02001896 goto fail;
1897 } else {
Kevin Wolf7eb58a62010-04-06 18:24:07 +02001898 mcb->num_requests++;
1899 multiwrite_cb(mcb, -EIO);
Kevin Wolf40b4f532009-09-09 17:53:37 +02001900 break;
1901 }
1902 } else {
1903 mcb->num_requests++;
1904 }
1905 }
1906
1907 return 0;
1908
1909fail:
1910 free(mcb);
1911 return -1;
1912}
1913
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02001914BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
1915 BlockDriverCompletionFunc *cb, void *opaque)
1916{
1917 BlockDriver *drv = bs->drv;
1918
1919 if (!drv)
1920 return NULL;
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02001921 return drv->bdrv_aio_flush(bs, cb, opaque);
1922}
1923
bellard83f64092006-08-01 16:21:11 +00001924void bdrv_aio_cancel(BlockDriverAIOCB *acb)
pbrookce1a14d2006-08-07 02:38:06 +00001925{
aliguori6bbff9a2009-03-20 18:25:59 +00001926 acb->pool->cancel(acb);
bellard83f64092006-08-01 16:21:11 +00001927}
1928
pbrookce1a14d2006-08-07 02:38:06 +00001929
bellard83f64092006-08-01 16:21:11 +00001930/**************************************************************/
1931/* async block device emulation */
1932
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02001933typedef struct BlockDriverAIOCBSync {
1934 BlockDriverAIOCB common;
1935 QEMUBH *bh;
1936 int ret;
1937 /* vector translation state */
1938 QEMUIOVector *qiov;
1939 uint8_t *bounce;
1940 int is_write;
1941} BlockDriverAIOCBSync;
1942
1943static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1944{
1945 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
Dor Laor6a7ad292009-06-01 12:07:23 +03001946 qemu_bh_delete(acb->bh);
Avi Kivity36afc452009-06-23 16:20:36 +03001947 acb->bh = NULL;
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02001948 qemu_aio_release(acb);
1949}
1950
1951static AIOPool bdrv_em_aio_pool = {
1952 .aiocb_size = sizeof(BlockDriverAIOCBSync),
1953 .cancel = bdrv_aio_cancel_em,
1954};
1955
bellard83f64092006-08-01 16:21:11 +00001956static void bdrv_aio_bh_cb(void *opaque)
bellardbeac80c2006-06-26 20:08:57 +00001957{
pbrookce1a14d2006-08-07 02:38:06 +00001958 BlockDriverAIOCBSync *acb = opaque;
aliguorif141eaf2009-04-07 18:43:24 +00001959
aliguorif141eaf2009-04-07 18:43:24 +00001960 if (!acb->is_write)
1961 qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
aliguoriceb42de2009-04-07 18:43:28 +00001962 qemu_vfree(acb->bounce);
pbrookce1a14d2006-08-07 02:38:06 +00001963 acb->common.cb(acb->common.opaque, acb->ret);
Dor Laor6a7ad292009-06-01 12:07:23 +03001964 qemu_bh_delete(acb->bh);
Avi Kivity36afc452009-06-23 16:20:36 +03001965 acb->bh = NULL;
pbrookce1a14d2006-08-07 02:38:06 +00001966 qemu_aio_release(acb);
bellardbeac80c2006-06-26 20:08:57 +00001967}
bellardbeac80c2006-06-26 20:08:57 +00001968
aliguorif141eaf2009-04-07 18:43:24 +00001969static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
1970 int64_t sector_num,
1971 QEMUIOVector *qiov,
1972 int nb_sectors,
1973 BlockDriverCompletionFunc *cb,
1974 void *opaque,
1975 int is_write)
1976
bellardea2384d2004-08-01 21:59:26 +00001977{
pbrookce1a14d2006-08-07 02:38:06 +00001978 BlockDriverAIOCBSync *acb;
pbrookce1a14d2006-08-07 02:38:06 +00001979
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02001980 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
aliguorif141eaf2009-04-07 18:43:24 +00001981 acb->is_write = is_write;
1982 acb->qiov = qiov;
aliguorie268ca52009-04-22 20:20:00 +00001983 acb->bounce = qemu_blockalign(bs, qiov->size);
aliguorif141eaf2009-04-07 18:43:24 +00001984
pbrookce1a14d2006-08-07 02:38:06 +00001985 if (!acb->bh)
1986 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
aliguorif141eaf2009-04-07 18:43:24 +00001987
1988 if (is_write) {
1989 qemu_iovec_to_buffer(acb->qiov, acb->bounce);
1990 acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
1991 } else {
1992 acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
1993 }
1994
pbrookce1a14d2006-08-07 02:38:06 +00001995 qemu_bh_schedule(acb->bh);
aliguorif141eaf2009-04-07 18:43:24 +00001996
pbrookce1a14d2006-08-07 02:38:06 +00001997 return &acb->common;
pbrook7a6cba62006-06-04 11:39:07 +00001998}
1999
aliguorif141eaf2009-04-07 18:43:24 +00002000static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
2001 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
pbrookce1a14d2006-08-07 02:38:06 +00002002 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00002003{
aliguorif141eaf2009-04-07 18:43:24 +00002004 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
bellard83f64092006-08-01 16:21:11 +00002005}
2006
aliguorif141eaf2009-04-07 18:43:24 +00002007static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
2008 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2009 BlockDriverCompletionFunc *cb, void *opaque)
2010{
2011 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
2012}
2013
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002014static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
2015 BlockDriverCompletionFunc *cb, void *opaque)
2016{
2017 BlockDriverAIOCBSync *acb;
2018
2019 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2020 acb->is_write = 1; /* don't bounce in the completion hadler */
2021 acb->qiov = NULL;
2022 acb->bounce = NULL;
2023 acb->ret = 0;
2024
2025 if (!acb->bh)
2026 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2027
2028 bdrv_flush(bs);
2029 qemu_bh_schedule(acb->bh);
2030 return &acb->common;
2031}
2032
bellard83f64092006-08-01 16:21:11 +00002033/**************************************************************/
2034/* sync block device emulation */
2035
2036static void bdrv_rw_em_cb(void *opaque, int ret)
2037{
2038 *(int *)opaque = ret;
2039}
2040
2041#define NOT_DONE 0x7fffffff
2042
ths5fafdf22007-09-16 21:08:06 +00002043static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +00002044 uint8_t *buf, int nb_sectors)
2045{
pbrookce1a14d2006-08-07 02:38:06 +00002046 int async_ret;
2047 BlockDriverAIOCB *acb;
aliguorif141eaf2009-04-07 18:43:24 +00002048 struct iovec iov;
2049 QEMUIOVector qiov;
bellard83f64092006-08-01 16:21:11 +00002050
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002051 async_context_push();
2052
bellard83f64092006-08-01 16:21:11 +00002053 async_ret = NOT_DONE;
blueswir13f4cb3d2009-04-13 16:31:01 +00002054 iov.iov_base = (void *)buf;
aliguorif141eaf2009-04-07 18:43:24 +00002055 iov.iov_len = nb_sectors * 512;
2056 qemu_iovec_init_external(&qiov, &iov, 1);
2057 acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
2058 bdrv_rw_em_cb, &async_ret);
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002059 if (acb == NULL) {
2060 async_ret = -1;
2061 goto fail;
2062 }
aliguoribaf35cb2008-09-10 15:45:19 +00002063
bellard83f64092006-08-01 16:21:11 +00002064 while (async_ret == NOT_DONE) {
2065 qemu_aio_wait();
2066 }
aliguoribaf35cb2008-09-10 15:45:19 +00002067
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002068
2069fail:
2070 async_context_pop();
bellard83f64092006-08-01 16:21:11 +00002071 return async_ret;
2072}
2073
2074static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
2075 const uint8_t *buf, int nb_sectors)
2076{
pbrookce1a14d2006-08-07 02:38:06 +00002077 int async_ret;
2078 BlockDriverAIOCB *acb;
aliguorif141eaf2009-04-07 18:43:24 +00002079 struct iovec iov;
2080 QEMUIOVector qiov;
bellard83f64092006-08-01 16:21:11 +00002081
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002082 async_context_push();
2083
bellard83f64092006-08-01 16:21:11 +00002084 async_ret = NOT_DONE;
aliguorif141eaf2009-04-07 18:43:24 +00002085 iov.iov_base = (void *)buf;
2086 iov.iov_len = nb_sectors * 512;
2087 qemu_iovec_init_external(&qiov, &iov, 1);
2088 acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
2089 bdrv_rw_em_cb, &async_ret);
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002090 if (acb == NULL) {
2091 async_ret = -1;
2092 goto fail;
2093 }
bellard83f64092006-08-01 16:21:11 +00002094 while (async_ret == NOT_DONE) {
2095 qemu_aio_wait();
2096 }
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002097
2098fail:
2099 async_context_pop();
bellard83f64092006-08-01 16:21:11 +00002100 return async_ret;
2101}
bellardea2384d2004-08-01 21:59:26 +00002102
2103void bdrv_init(void)
2104{
Anthony Liguori5efa9d52009-05-09 17:03:42 -05002105 module_call_init(MODULE_INIT_BLOCK);
bellardea2384d2004-08-01 21:59:26 +00002106}
pbrookce1a14d2006-08-07 02:38:06 +00002107
Markus Armbrustereb852012009-10-27 18:41:44 +01002108void bdrv_init_with_whitelist(void)
2109{
2110 use_bdrv_whitelist = 1;
2111 bdrv_init();
2112}
2113
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002114void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
2115 BlockDriverCompletionFunc *cb, void *opaque)
aliguori6bbff9a2009-03-20 18:25:59 +00002116{
pbrookce1a14d2006-08-07 02:38:06 +00002117 BlockDriverAIOCB *acb;
2118
aliguori6bbff9a2009-03-20 18:25:59 +00002119 if (pool->free_aiocb) {
2120 acb = pool->free_aiocb;
2121 pool->free_aiocb = acb->next;
pbrookce1a14d2006-08-07 02:38:06 +00002122 } else {
aliguori6bbff9a2009-03-20 18:25:59 +00002123 acb = qemu_mallocz(pool->aiocb_size);
2124 acb->pool = pool;
pbrookce1a14d2006-08-07 02:38:06 +00002125 }
2126 acb->bs = bs;
2127 acb->cb = cb;
2128 acb->opaque = opaque;
2129 return acb;
2130}
2131
2132void qemu_aio_release(void *p)
2133{
aliguori6bbff9a2009-03-20 18:25:59 +00002134 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
2135 AIOPool *pool = acb->pool;
2136 acb->next = pool->free_aiocb;
2137 pool->free_aiocb = acb;
pbrookce1a14d2006-08-07 02:38:06 +00002138}
bellard19cb3732006-08-19 11:45:59 +00002139
2140/**************************************************************/
2141/* removable device support */
2142
2143/**
2144 * Return TRUE if the media is present
2145 */
2146int bdrv_is_inserted(BlockDriverState *bs)
2147{
2148 BlockDriver *drv = bs->drv;
2149 int ret;
2150 if (!drv)
2151 return 0;
2152 if (!drv->bdrv_is_inserted)
2153 return 1;
2154 ret = drv->bdrv_is_inserted(bs);
2155 return ret;
2156}
2157
2158/**
2159 * Return TRUE if the media changed since the last call to this
ths5fafdf22007-09-16 21:08:06 +00002160 * function. It is currently only used for floppy disks
bellard19cb3732006-08-19 11:45:59 +00002161 */
2162int bdrv_media_changed(BlockDriverState *bs)
2163{
2164 BlockDriver *drv = bs->drv;
2165 int ret;
2166
2167 if (!drv || !drv->bdrv_media_changed)
2168 ret = -ENOTSUP;
2169 else
2170 ret = drv->bdrv_media_changed(bs);
2171 if (ret == -ENOTSUP)
2172 ret = bs->media_changed;
2173 bs->media_changed = 0;
2174 return ret;
2175}
2176
2177/**
2178 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
2179 */
Mark McLoughlinaea2a332009-05-27 10:06:11 +01002180int bdrv_eject(BlockDriverState *bs, int eject_flag)
bellard19cb3732006-08-19 11:45:59 +00002181{
2182 BlockDriver *drv = bs->drv;
2183 int ret;
2184
Mark McLoughlinaea2a332009-05-27 10:06:11 +01002185 if (bs->locked) {
2186 return -EBUSY;
2187 }
2188
bellard19cb3732006-08-19 11:45:59 +00002189 if (!drv || !drv->bdrv_eject) {
2190 ret = -ENOTSUP;
2191 } else {
2192 ret = drv->bdrv_eject(bs, eject_flag);
2193 }
2194 if (ret == -ENOTSUP) {
2195 if (eject_flag)
2196 bdrv_close(bs);
Mark McLoughlinaea2a332009-05-27 10:06:11 +01002197 ret = 0;
bellard19cb3732006-08-19 11:45:59 +00002198 }
Mark McLoughlinaea2a332009-05-27 10:06:11 +01002199
2200 return ret;
bellard19cb3732006-08-19 11:45:59 +00002201}
2202
2203int bdrv_is_locked(BlockDriverState *bs)
2204{
2205 return bs->locked;
2206}
2207
2208/**
2209 * Lock or unlock the media (if it is locked, the user won't be able
2210 * to eject it manually).
2211 */
2212void bdrv_set_locked(BlockDriverState *bs, int locked)
2213{
2214 BlockDriver *drv = bs->drv;
2215
2216 bs->locked = locked;
2217 if (drv && drv->bdrv_set_locked) {
2218 drv->bdrv_set_locked(bs, locked);
2219 }
2220}
ths985a03b2007-12-24 16:10:43 +00002221
2222/* needed for generic scsi interface */
2223
2224int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
2225{
2226 BlockDriver *drv = bs->drv;
2227
2228 if (drv && drv->bdrv_ioctl)
2229 return drv->bdrv_ioctl(bs, req, buf);
2230 return -ENOTSUP;
2231}
aliguori7d780662009-03-12 19:57:08 +00002232
aliguori221f7152009-03-28 17:28:41 +00002233BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
2234 unsigned long int req, void *buf,
2235 BlockDriverCompletionFunc *cb, void *opaque)
aliguori7d780662009-03-12 19:57:08 +00002236{
aliguori221f7152009-03-28 17:28:41 +00002237 BlockDriver *drv = bs->drv;
aliguori7d780662009-03-12 19:57:08 +00002238
aliguori221f7152009-03-28 17:28:41 +00002239 if (drv && drv->bdrv_aio_ioctl)
2240 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
2241 return NULL;
aliguori7d780662009-03-12 19:57:08 +00002242}
aliguorie268ca52009-04-22 20:20:00 +00002243
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002244
2245
aliguorie268ca52009-04-22 20:20:00 +00002246void *qemu_blockalign(BlockDriverState *bs, size_t size)
2247{
2248 return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
2249}
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002250
2251void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable)
2252{
2253 int64_t bitmap_size;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002254
Liran Schouraaa0eb72010-01-26 10:31:48 +02002255 bs->dirty_count = 0;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002256 if (enable) {
Jan Kiszkac6d22832009-11-30 18:21:20 +01002257 if (!bs->dirty_bitmap) {
2258 bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
2259 BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
2260 bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002261
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002262 bs->dirty_bitmap = qemu_mallocz(bitmap_size);
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002263 }
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002264 } else {
Jan Kiszkac6d22832009-11-30 18:21:20 +01002265 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002266 qemu_free(bs->dirty_bitmap);
Jan Kiszkac6d22832009-11-30 18:21:20 +01002267 bs->dirty_bitmap = NULL;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002268 }
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002269 }
2270}
2271
2272int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
2273{
Jan Kiszka6ea44302009-11-30 18:21:19 +01002274 int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002275
Jan Kiszkac6d22832009-11-30 18:21:20 +01002276 if (bs->dirty_bitmap &&
2277 (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) {
2278 return bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] &
2279 (1 << (chunk % (sizeof(unsigned long) * 8)));
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002280 } else {
2281 return 0;
2282 }
2283}
2284
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002285void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
2286 int nr_sectors)
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002287{
2288 set_dirty_bitmap(bs, cur_sector, nr_sectors, 0);
2289}
Liran Schouraaa0eb72010-01-26 10:31:48 +02002290
2291int64_t bdrv_get_dirty_count(BlockDriverState *bs)
2292{
2293 return bs->dirty_count;
2294}