blob: 6efc2b3cbe7f1b188487d6a9b0579b77b878366f [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
Kevin Wolfb6ce07a2010-04-12 16:37:13 +020045static int bdrv_open_common(BlockDriverState *bs, const char *filename,
46 int flags, BlockDriver *drv);
47
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);
ths5fafdf22007-09-16 21:08:06 +000056static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +000057 uint8_t *buf, int nb_sectors);
58static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
59 const uint8_t *buf, int nb_sectors);
Christoph Hellwig84a12e62010-04-07 22:30:24 +020060static BlockDriver *find_protocol(const char *filename);
bellardec530c82006-04-25 22:36:06 +000061
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +010062static QTAILQ_HEAD(, BlockDriverState) bdrv_states =
63 QTAILQ_HEAD_INITIALIZER(bdrv_states);
blueswir17ee930d2008-09-17 19:04:14 +000064
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +010065static QLIST_HEAD(, BlockDriver) bdrv_drivers =
66 QLIST_HEAD_INITIALIZER(bdrv_drivers);
bellardea2384d2004-08-01 21:59:26 +000067
Markus Armbrustereb852012009-10-27 18:41:44 +010068/* If non-zero, use only whitelisted block drivers */
69static int use_bdrv_whitelist;
70
bellard83f64092006-08-01 16:21:11 +000071int path_is_absolute(const char *path)
72{
73 const char *p;
bellard21664422007-01-07 18:22:37 +000074#ifdef _WIN32
75 /* specific case for names like: "\\.\d:" */
76 if (*path == '/' || *path == '\\')
77 return 1;
78#endif
bellard83f64092006-08-01 16:21:11 +000079 p = strchr(path, ':');
80 if (p)
81 p++;
82 else
83 p = path;
bellard3b9f94e2007-01-07 17:27:07 +000084#ifdef _WIN32
85 return (*p == '/' || *p == '\\');
86#else
87 return (*p == '/');
88#endif
bellard83f64092006-08-01 16:21:11 +000089}
90
91/* if filename is absolute, just copy it to dest. Otherwise, build a
92 path to it by considering it is relative to base_path. URL are
93 supported. */
94void path_combine(char *dest, int dest_size,
95 const char *base_path,
96 const char *filename)
97{
98 const char *p, *p1;
99 int len;
100
101 if (dest_size <= 0)
102 return;
103 if (path_is_absolute(filename)) {
104 pstrcpy(dest, dest_size, filename);
105 } else {
106 p = strchr(base_path, ':');
107 if (p)
108 p++;
109 else
110 p = base_path;
bellard3b9f94e2007-01-07 17:27:07 +0000111 p1 = strrchr(base_path, '/');
112#ifdef _WIN32
113 {
114 const char *p2;
115 p2 = strrchr(base_path, '\\');
116 if (!p1 || p2 > p1)
117 p1 = p2;
118 }
119#endif
bellard83f64092006-08-01 16:21:11 +0000120 if (p1)
121 p1++;
122 else
123 p1 = base_path;
124 if (p1 > p)
125 p = p1;
126 len = p - base_path;
127 if (len > dest_size - 1)
128 len = dest_size - 1;
129 memcpy(dest, base_path, len);
130 dest[len] = '\0';
131 pstrcat(dest, dest_size, filename);
132 }
133}
134
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500135void bdrv_register(BlockDriver *bdrv)
bellardea2384d2004-08-01 21:59:26 +0000136{
aliguorif141eaf2009-04-07 18:43:24 +0000137 if (!bdrv->bdrv_aio_readv) {
bellard83f64092006-08-01 16:21:11 +0000138 /* add AIO emulation layer */
aliguorif141eaf2009-04-07 18:43:24 +0000139 bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
140 bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
aliguorieda578e2009-03-12 19:57:16 +0000141 } else if (!bdrv->bdrv_read) {
bellard83f64092006-08-01 16:21:11 +0000142 /* add synchronous IO emulation layer */
143 bdrv->bdrv_read = bdrv_read_em;
144 bdrv->bdrv_write = bdrv_write_em;
145 }
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +0200146
147 if (!bdrv->bdrv_aio_flush)
148 bdrv->bdrv_aio_flush = bdrv_aio_flush_em;
149
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100150 QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
bellardea2384d2004-08-01 21:59:26 +0000151}
bellardb3380822004-03-14 21:38:54 +0000152
153/* create a new block device (by default it is empty) */
154BlockDriverState *bdrv_new(const char *device_name)
bellardfc01f7e2003-06-30 10:03:06 +0000155{
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +0100156 BlockDriverState *bs;
bellardb3380822004-03-14 21:38:54 +0000157
158 bs = qemu_mallocz(sizeof(BlockDriverState));
bellardb3380822004-03-14 21:38:54 +0000159 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
bellardea2384d2004-08-01 21:59:26 +0000160 if (device_name[0] != '\0') {
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +0100161 QTAILQ_INSERT_TAIL(&bdrv_states, bs, list);
bellardea2384d2004-08-01 21:59:26 +0000162 }
bellardb3380822004-03-14 21:38:54 +0000163 return bs;
164}
165
bellardea2384d2004-08-01 21:59:26 +0000166BlockDriver *bdrv_find_format(const char *format_name)
167{
168 BlockDriver *drv1;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100169 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
170 if (!strcmp(drv1->format_name, format_name)) {
bellardea2384d2004-08-01 21:59:26 +0000171 return drv1;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100172 }
bellardea2384d2004-08-01 21:59:26 +0000173 }
174 return NULL;
175}
176
Markus Armbrustereb852012009-10-27 18:41:44 +0100177static int bdrv_is_whitelisted(BlockDriver *drv)
178{
179 static const char *whitelist[] = {
180 CONFIG_BDRV_WHITELIST
181 };
182 const char **p;
183
184 if (!whitelist[0])
185 return 1; /* no whitelist, anything goes */
186
187 for (p = whitelist; *p; p++) {
188 if (!strcmp(drv->format_name, *p)) {
189 return 1;
190 }
191 }
192 return 0;
193}
194
195BlockDriver *bdrv_find_whitelisted_format(const char *format_name)
196{
197 BlockDriver *drv = bdrv_find_format(format_name);
198 return drv && bdrv_is_whitelisted(drv) ? drv : NULL;
199}
200
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200201int bdrv_create(BlockDriver *drv, const char* filename,
202 QEMUOptionParameter *options)
bellardea2384d2004-08-01 21:59:26 +0000203{
204 if (!drv->bdrv_create)
205 return -ENOTSUP;
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200206
207 return drv->bdrv_create(filename, options);
bellardea2384d2004-08-01 21:59:26 +0000208}
209
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200210int bdrv_create_file(const char* filename, QEMUOptionParameter *options)
211{
212 BlockDriver *drv;
213
214 drv = find_protocol(filename);
215 if (drv == NULL) {
216 drv = bdrv_find_format("file");
217 }
218
219 return bdrv_create(drv, filename, options);
220}
221
bellardd5249392004-08-03 21:14:23 +0000222#ifdef _WIN32
bellard95389c82005-12-18 18:28:15 +0000223void get_tmp_filename(char *filename, int size)
bellardd5249392004-08-03 21:14:23 +0000224{
bellard3b9f94e2007-01-07 17:27:07 +0000225 char temp_dir[MAX_PATH];
ths3b46e622007-09-17 08:09:54 +0000226
bellard3b9f94e2007-01-07 17:27:07 +0000227 GetTempPath(MAX_PATH, temp_dir);
228 GetTempFileName(temp_dir, "qem", 0, filename);
bellardd5249392004-08-03 21:14:23 +0000229}
230#else
bellard95389c82005-12-18 18:28:15 +0000231void get_tmp_filename(char *filename, int size)
bellardea2384d2004-08-01 21:59:26 +0000232{
233 int fd;
blueswir17ccfb2e2008-09-14 06:45:34 +0000234 const char *tmpdir;
bellardd5249392004-08-03 21:14:23 +0000235 /* XXX: race condition possible */
aurel320badc1e2008-03-10 00:05:34 +0000236 tmpdir = getenv("TMPDIR");
237 if (!tmpdir)
238 tmpdir = "/tmp";
239 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
bellardea2384d2004-08-01 21:59:26 +0000240 fd = mkstemp(filename);
241 close(fd);
242}
bellardd5249392004-08-03 21:14:23 +0000243#endif
bellardea2384d2004-08-01 21:59:26 +0000244
bellard19cb3732006-08-19 11:45:59 +0000245#ifdef _WIN32
bellardf45512f2006-08-23 21:40:13 +0000246static int is_windows_drive_prefix(const char *filename)
247{
248 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
249 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
250 filename[1] == ':');
251}
ths3b46e622007-09-17 08:09:54 +0000252
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200253int is_windows_drive(const char *filename)
bellard19cb3732006-08-19 11:45:59 +0000254{
ths5fafdf22007-09-16 21:08:06 +0000255 if (is_windows_drive_prefix(filename) &&
bellardf45512f2006-08-23 21:40:13 +0000256 filename[2] == '\0')
bellard19cb3732006-08-19 11:45:59 +0000257 return 1;
258 if (strstart(filename, "\\\\.\\", NULL) ||
259 strstart(filename, "//./", NULL))
260 return 1;
261 return 0;
262}
263#endif
264
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200265/*
266 * Detect host devices. By convention, /dev/cdrom[N] is always
267 * recognized as a host CDROM.
268 */
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200269static BlockDriver *find_hdev_driver(const char *filename)
270{
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200271 int score_max = 0, score;
272 BlockDriver *drv = NULL, *d;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200273
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100274 QLIST_FOREACH(d, &bdrv_drivers, list) {
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200275 if (d->bdrv_probe_device) {
276 score = d->bdrv_probe_device(filename);
277 if (score > score_max) {
278 score_max = score;
279 drv = d;
280 }
281 }
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200282 }
283
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200284 return drv;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200285}
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200286
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200287static BlockDriver *find_protocol(const char *filename)
288{
289 BlockDriver *drv1;
290 char protocol[128];
291 int len;
292 const char *p;
293
294#ifdef _WIN32
295 if (is_windows_drive(filename) ||
296 is_windows_drive_prefix(filename))
297 return bdrv_find_format("file");
298#endif
299 p = strchr(filename, ':');
300 if (!p) {
301 drv1 = find_hdev_driver(filename);
302 if (!drv1) {
303 drv1 = bdrv_find_format("file");
304 }
305 return drv1;
306 }
307 len = p - filename;
308 if (len > sizeof(protocol) - 1)
309 len = sizeof(protocol) - 1;
310 memcpy(protocol, filename, len);
311 protocol[len] = '\0';
312 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
313 if (drv1->protocol_name &&
314 !strcmp(drv1->protocol_name, protocol)) {
315 return drv1;
316 }
317 }
318 return NULL;
319}
320
bellardea2384d2004-08-01 21:59:26 +0000321static BlockDriver *find_image_format(const char *filename)
322{
bellard83f64092006-08-01 16:21:11 +0000323 int ret, score, score_max;
bellardea2384d2004-08-01 21:59:26 +0000324 BlockDriver *drv1, *drv;
bellard83f64092006-08-01 16:21:11 +0000325 uint8_t buf[2048];
326 BlockDriverState *bs;
ths3b46e622007-09-17 08:09:54 +0000327
bellard83f64092006-08-01 16:21:11 +0000328 drv = find_protocol(filename);
bellard19cb3732006-08-19 11:45:59 +0000329 /* no need to test disk image formats for vvfat */
Anthony Liguoric833ab72009-05-22 08:17:55 -0500330 if (drv && strcmp(drv->format_name, "vvfat") == 0)
bellard83f64092006-08-01 16:21:11 +0000331 return drv;
bellard19cb3732006-08-19 11:45:59 +0000332
Naphtali Spreif5edb012010-01-17 16:48:13 +0200333 ret = bdrv_file_open(&bs, filename, 0);
bellard83f64092006-08-01 16:21:11 +0000334 if (ret < 0)
335 return NULL;
336 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
337 bdrv_delete(bs);
338 if (ret < 0) {
339 return NULL;
340 }
341
bellardea2384d2004-08-01 21:59:26 +0000342 score_max = 0;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200343 drv = NULL;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100344 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
bellard83f64092006-08-01 16:21:11 +0000345 if (drv1->bdrv_probe) {
346 score = drv1->bdrv_probe(buf, ret, filename);
347 if (score > score_max) {
348 score_max = score;
349 drv = drv1;
350 }
bellardea2384d2004-08-01 21:59:26 +0000351 }
352 }
353 return drv;
354}
355
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200356/*
357 * Opens a file using a protocol (file, host_device, nbd, ...)
358 */
bellard83f64092006-08-01 16:21:11 +0000359int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
bellardb3380822004-03-14 21:38:54 +0000360{
bellard83f64092006-08-01 16:21:11 +0000361 BlockDriverState *bs;
Christoph Hellwig6db95602010-04-05 16:53:57 +0200362 BlockDriver *drv;
bellard83f64092006-08-01 16:21:11 +0000363 int ret;
364
Christoph Hellwig6db95602010-04-05 16:53:57 +0200365 drv = find_protocol(filename);
366 if (!drv) {
367 return -ENOENT;
368 }
369
bellard83f64092006-08-01 16:21:11 +0000370 bs = bdrv_new("");
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200371 ret = bdrv_open_common(bs, filename, flags, drv);
bellard83f64092006-08-01 16:21:11 +0000372 if (ret < 0) {
373 bdrv_delete(bs);
374 return ret;
bellard3b0d4f62005-10-30 18:30:10 +0000375 }
aliguori71d07702009-03-03 17:37:16 +0000376 bs->growable = 1;
bellard83f64092006-08-01 16:21:11 +0000377 *pbs = bs;
378 return 0;
bellardea2384d2004-08-01 21:59:26 +0000379}
bellardfc01f7e2003-06-30 10:03:06 +0000380
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200381/*
382 * Opens a disk image (raw, qcow2, vmdk, ...)
383 */
Kevin Wolfd6e90982010-03-31 14:40:27 +0200384int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
385 BlockDriver *drv)
bellardea2384d2004-08-01 21:59:26 +0000386{
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200387 int ret;
bellard712e7872005-04-28 21:09:32 +0000388
bellard83f64092006-08-01 16:21:11 +0000389 if (flags & BDRV_O_SNAPSHOT) {
bellardea2384d2004-08-01 21:59:26 +0000390 BlockDriverState *bs1;
391 int64_t total_size;
aliguori7c96d462008-09-12 17:54:13 +0000392 int is_protocol = 0;
Kevin Wolf91a073a2009-05-27 14:48:06 +0200393 BlockDriver *bdrv_qcow2;
394 QEMUOptionParameter *options;
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200395 char tmp_filename[PATH_MAX];
396 char backing_filename[PATH_MAX];
ths3b46e622007-09-17 08:09:54 +0000397
bellardea2384d2004-08-01 21:59:26 +0000398 /* if snapshot, we create a temporary backing file and open it
399 instead of opening 'filename' directly */
400
401 /* if there is a backing file, use it */
402 bs1 = bdrv_new("");
Kevin Wolfd6e90982010-03-31 14:40:27 +0200403 ret = bdrv_open(bs1, filename, 0, drv);
aliguori51d7c002009-03-05 23:00:29 +0000404 if (ret < 0) {
bellardea2384d2004-08-01 21:59:26 +0000405 bdrv_delete(bs1);
aliguori51d7c002009-03-05 23:00:29 +0000406 return ret;
bellardea2384d2004-08-01 21:59:26 +0000407 }
Jan Kiszka6ea44302009-11-30 18:21:19 +0100408 total_size = bdrv_getlength(bs1) >> BDRV_SECTOR_BITS;
aliguori7c96d462008-09-12 17:54:13 +0000409
410 if (bs1->drv && bs1->drv->protocol_name)
411 is_protocol = 1;
412
bellardea2384d2004-08-01 21:59:26 +0000413 bdrv_delete(bs1);
ths3b46e622007-09-17 08:09:54 +0000414
bellardea2384d2004-08-01 21:59:26 +0000415 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
aliguori7c96d462008-09-12 17:54:13 +0000416
417 /* Real path is meaningless for protocols */
418 if (is_protocol)
419 snprintf(backing_filename, sizeof(backing_filename),
420 "%s", filename);
Kirill A. Shutemov114cdfa2009-12-25 18:19:22 +0000421 else if (!realpath(filename, backing_filename))
422 return -errno;
aliguori7c96d462008-09-12 17:54:13 +0000423
Kevin Wolf91a073a2009-05-27 14:48:06 +0200424 bdrv_qcow2 = bdrv_find_format("qcow2");
425 options = parse_option_parameters("", bdrv_qcow2->create_options, NULL);
426
427 set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size * 512);
428 set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename);
429 if (drv) {
430 set_option_parameter(options, BLOCK_OPT_BACKING_FMT,
431 drv->format_name);
432 }
433
434 ret = bdrv_create(bdrv_qcow2, tmp_filename, options);
aliguori51d7c002009-03-05 23:00:29 +0000435 if (ret < 0) {
436 return ret;
bellardea2384d2004-08-01 21:59:26 +0000437 }
Kevin Wolf91a073a2009-05-27 14:48:06 +0200438
bellardea2384d2004-08-01 21:59:26 +0000439 filename = tmp_filename;
Kevin Wolf91a073a2009-05-27 14:48:06 +0200440 drv = bdrv_qcow2;
bellardea2384d2004-08-01 21:59:26 +0000441 bs->is_temporary = 1;
442 }
bellard712e7872005-04-28 21:09:32 +0000443
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200444 /* Find the right image format driver */
Christoph Hellwig6db95602010-04-05 16:53:57 +0200445 if (!drv) {
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200446 drv = find_image_format(filename);
aliguori51d7c002009-03-05 23:00:29 +0000447 }
Christoph Hellwig69873072010-01-20 18:13:25 +0100448
aliguori51d7c002009-03-05 23:00:29 +0000449 if (!drv) {
450 ret = -ENOENT;
451 goto unlink_and_fail;
bellardea2384d2004-08-01 21:59:26 +0000452 }
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200453
454 /* Open the image */
455 ret = bdrv_open_common(bs, filename, flags, drv);
456 if (ret < 0) {
Christoph Hellwig69873072010-01-20 18:13:25 +0100457 goto unlink_and_fail;
458 }
459
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200460 /* If there is a backing file, use it */
461 if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') {
462 char backing_filename[PATH_MAX];
463 int back_flags;
464 BlockDriver *back_drv = NULL;
465
466 bs->backing_hd = bdrv_new("");
467 path_combine(backing_filename, sizeof(backing_filename),
468 filename, bs->backing_file);
469 if (bs->backing_format[0] != '\0')
470 back_drv = bdrv_find_format(bs->backing_format);
471
472 /* backing files always opened read-only */
473 back_flags =
474 flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
475
476 ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv);
477 if (ret < 0) {
478 bdrv_close(bs);
479 return ret;
480 }
481 if (bs->is_temporary) {
482 bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR);
483 } else {
484 /* base image inherits from "parent" */
485 bs->backing_hd->keep_read_only = bs->keep_read_only;
486 }
487 }
488
489 if (!bdrv_key_required(bs)) {
490 /* call the change callback */
491 bs->media_changed = 1;
492 if (bs->change_cb)
493 bs->change_cb(bs->change_opaque);
494 }
495
496 return 0;
497
498unlink_and_fail:
499 if (bs->is_temporary) {
500 unlink(filename);
501 }
502 return ret;
503}
504
505/*
506 * Common part for opening disk images and files
507 */
508static int bdrv_open_common(BlockDriverState *bs, const char *filename,
509 int flags, BlockDriver *drv)
510{
511 int ret, open_flags;
512
513 assert(drv != NULL);
514
515 bs->is_temporary = 0;
516 bs->encrypted = 0;
517 bs->valid_key = 0;
518 bs->open_flags = flags;
519 /* buffer_alignment defaulted to 512, drivers can change this value */
520 bs->buffer_alignment = 512;
521
522 pstrcpy(bs->filename, sizeof(bs->filename), filename);
523
524 if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {
525 return -ENOTSUP;
526 }
527
bellardea2384d2004-08-01 21:59:26 +0000528 bs->drv = drv;
529 bs->opaque = qemu_mallocz(drv->instance_size);
Christoph Hellwige900a7b2009-09-04 19:01:15 +0200530
531 /*
532 * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a
533 * write cache to the guest. We do need the fdatasync to flush
534 * out transactions for block allocations, and we maybe have a
535 * volatile write cache in our backing device to deal with.
536 */
537 if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE))
538 bs->enable_write_cache = 1;
539
Christoph Hellwig15dc2692010-01-28 15:19:12 +0100540 /*
541 * Clear flags that are internal to the block layer before opening the
542 * image.
543 */
Christoph Hellwig6db95602010-04-05 16:53:57 +0200544 open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
Christoph Hellwig15dc2692010-01-28 15:19:12 +0100545
546 /*
547 * Snapshots should be writeable.
Christoph Hellwig15dc2692010-01-28 15:19:12 +0100548 */
Christoph Hellwig6db95602010-04-05 16:53:57 +0200549 if (bs->is_temporary) {
Christoph Hellwig15dc2692010-01-28 15:19:12 +0100550 open_flags |= BDRV_O_RDWR;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200551 }
Christoph Hellwig69873072010-01-20 18:13:25 +0100552
553 ret = drv->bdrv_open(bs, filename, open_flags);
bellardea2384d2004-08-01 21:59:26 +0000554 if (ret < 0) {
Christoph Hellwig69873072010-01-20 18:13:25 +0100555 goto free_and_fail;
bellardea2384d2004-08-01 21:59:26 +0000556 }
Christoph Hellwig69873072010-01-20 18:13:25 +0100557
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200558 bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR);
bellardd15a7712006-08-06 13:35:09 +0000559 if (drv->bdrv_getlength) {
Jan Kiszka6ea44302009-11-30 18:21:19 +0100560 bs->total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
bellardd15a7712006-08-06 13:35:09 +0000561 }
bellardea2384d2004-08-01 21:59:26 +0000562#ifndef _WIN32
563 if (bs->is_temporary) {
564 unlink(filename);
565 }
566#endif
bellardb3380822004-03-14 21:38:54 +0000567 return 0;
Christoph Hellwig69873072010-01-20 18:13:25 +0100568
569free_and_fail:
570 qemu_free(bs->opaque);
571 bs->opaque = NULL;
572 bs->drv = NULL;
Christoph Hellwig69873072010-01-20 18:13:25 +0100573 return ret;
bellardfc01f7e2003-06-30 10:03:06 +0000574}
575
576void bdrv_close(BlockDriverState *bs)
577{
bellard19cb3732006-08-19 11:45:59 +0000578 if (bs->drv) {
bellardea2384d2004-08-01 21:59:26 +0000579 if (bs->backing_hd)
580 bdrv_delete(bs->backing_hd);
581 bs->drv->bdrv_close(bs);
582 qemu_free(bs->opaque);
583#ifdef _WIN32
584 if (bs->is_temporary) {
585 unlink(bs->filename);
586 }
bellard67b915a2004-03-31 23:37:16 +0000587#endif
bellardea2384d2004-08-01 21:59:26 +0000588 bs->opaque = NULL;
589 bs->drv = NULL;
bellardb3380822004-03-14 21:38:54 +0000590
591 /* call the change callback */
bellard19cb3732006-08-19 11:45:59 +0000592 bs->media_changed = 1;
bellardb3380822004-03-14 21:38:54 +0000593 if (bs->change_cb)
594 bs->change_cb(bs->change_opaque);
595 }
596}
597
598void bdrv_delete(BlockDriverState *bs)
599{
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +0100600 /* remove from list, if necessary */
601 if (bs->device_name[0] != '\0') {
602 QTAILQ_REMOVE(&bdrv_states, bs, list);
603 }
aurel3234c6f052008-04-08 19:51:21 +0000604
bellardb3380822004-03-14 21:38:54 +0000605 bdrv_close(bs);
606 qemu_free(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000607}
608
aliguorie97fc192009-04-21 23:11:50 +0000609/*
610 * Run consistency checks on an image
611 *
612 * Returns the number of errors or -errno when an internal error occurs
613 */
614int bdrv_check(BlockDriverState *bs)
615{
616 if (bs->drv->bdrv_check == NULL) {
617 return -ENOTSUP;
618 }
619
620 return bs->drv->bdrv_check(bs);
621}
622
bellard33e39632003-07-06 17:15:21 +0000623/* commit COW file into the raw image */
624int bdrv_commit(BlockDriverState *bs)
625{
bellard19cb3732006-08-19 11:45:59 +0000626 BlockDriver *drv = bs->drv;
bellard83f64092006-08-01 16:21:11 +0000627 int64_t i, total_sectors;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200628 int n, j, ro, open_flags;
629 int ret = 0, rw_ret = 0;
bellardea2384d2004-08-01 21:59:26 +0000630 unsigned char sector[512];
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200631 char filename[1024];
632 BlockDriverState *bs_rw, *bs_ro;
bellard33e39632003-07-06 17:15:21 +0000633
bellard19cb3732006-08-19 11:45:59 +0000634 if (!drv)
635 return -ENOMEDIUM;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200636
637 if (!bs->backing_hd) {
638 return -ENOTSUP;
bellard33e39632003-07-06 17:15:21 +0000639 }
640
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200641 if (bs->backing_hd->keep_read_only) {
642 return -EACCES;
643 }
644
645 ro = bs->backing_hd->read_only;
646 strncpy(filename, bs->backing_hd->filename, sizeof(filename));
647 open_flags = bs->backing_hd->open_flags;
648
649 if (ro) {
650 /* re-open as RW */
651 bdrv_delete(bs->backing_hd);
652 bs->backing_hd = NULL;
653 bs_rw = bdrv_new("");
Kevin Wolfd6e90982010-03-31 14:40:27 +0200654 rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR, NULL);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200655 if (rw_ret < 0) {
656 bdrv_delete(bs_rw);
657 /* try to re-open read-only */
658 bs_ro = bdrv_new("");
Kevin Wolfd6e90982010-03-31 14:40:27 +0200659 ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, NULL);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200660 if (ret < 0) {
661 bdrv_delete(bs_ro);
662 /* drive not functional anymore */
663 bs->drv = NULL;
664 return ret;
665 }
666 bs->backing_hd = bs_ro;
667 return rw_ret;
668 }
669 bs->backing_hd = bs_rw;
bellard33e39632003-07-06 17:15:21 +0000670 }
bellardea2384d2004-08-01 21:59:26 +0000671
Jan Kiszka6ea44302009-11-30 18:21:19 +0100672 total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000673 for (i = 0; i < total_sectors;) {
bellard19cb3732006-08-19 11:45:59 +0000674 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
bellardea2384d2004-08-01 21:59:26 +0000675 for(j = 0; j < n; j++) {
676 if (bdrv_read(bs, i, sector, 1) != 0) {
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200677 ret = -EIO;
678 goto ro_cleanup;
bellardea2384d2004-08-01 21:59:26 +0000679 }
680
681 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200682 ret = -EIO;
683 goto ro_cleanup;
bellardea2384d2004-08-01 21:59:26 +0000684 }
685 i++;
686 }
687 } else {
688 i += n;
689 }
690 }
bellard95389c82005-12-18 18:28:15 +0000691
Christoph Hellwig1d449522010-01-17 12:32:30 +0100692 if (drv->bdrv_make_empty) {
693 ret = drv->bdrv_make_empty(bs);
694 bdrv_flush(bs);
695 }
bellard95389c82005-12-18 18:28:15 +0000696
Christoph Hellwig3f5075a2010-01-12 13:49:23 +0100697 /*
698 * Make sure all data we wrote to the backing device is actually
699 * stable on disk.
700 */
701 if (bs->backing_hd)
702 bdrv_flush(bs->backing_hd);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200703
704ro_cleanup:
705
706 if (ro) {
707 /* re-open as RO */
708 bdrv_delete(bs->backing_hd);
709 bs->backing_hd = NULL;
710 bs_ro = bdrv_new("");
Kevin Wolfd6e90982010-03-31 14:40:27 +0200711 ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, NULL);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200712 if (ret < 0) {
713 bdrv_delete(bs_ro);
714 /* drive not functional anymore */
715 bs->drv = NULL;
716 return ret;
717 }
718 bs->backing_hd = bs_ro;
719 bs->backing_hd->keep_read_only = 0;
720 }
721
Christoph Hellwig1d449522010-01-17 12:32:30 +0100722 return ret;
bellard33e39632003-07-06 17:15:21 +0000723}
724
Kevin Wolf756e6732010-01-12 12:55:17 +0100725/*
726 * Return values:
727 * 0 - success
728 * -EINVAL - backing format specified, but no file
729 * -ENOSPC - can't update the backing file because no space is left in the
730 * image file header
731 * -ENOTSUP - format driver doesn't support changing the backing file
732 */
733int bdrv_change_backing_file(BlockDriverState *bs,
734 const char *backing_file, const char *backing_fmt)
735{
736 BlockDriver *drv = bs->drv;
737
738 if (drv->bdrv_change_backing_file != NULL) {
739 return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
740 } else {
741 return -ENOTSUP;
742 }
743}
744
aliguori71d07702009-03-03 17:37:16 +0000745static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
746 size_t size)
747{
748 int64_t len;
749
750 if (!bdrv_is_inserted(bs))
751 return -ENOMEDIUM;
752
753 if (bs->growable)
754 return 0;
755
756 len = bdrv_getlength(bs);
757
Kevin Wolffbb7b4e2009-05-08 14:47:24 +0200758 if (offset < 0)
759 return -EIO;
760
761 if ((offset > len) || (len - offset < size))
aliguori71d07702009-03-03 17:37:16 +0000762 return -EIO;
763
764 return 0;
765}
766
767static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
768 int nb_sectors)
769{
aliguori999dec52009-03-29 01:31:48 +0000770 return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512);
aliguori71d07702009-03-03 17:37:16 +0000771}
772
bellard19cb3732006-08-19 11:45:59 +0000773/* return < 0 if error. See bdrv_write() for the return codes */
ths5fafdf22007-09-16 21:08:06 +0000774int bdrv_read(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000775 uint8_t *buf, int nb_sectors)
776{
bellardea2384d2004-08-01 21:59:26 +0000777 BlockDriver *drv = bs->drv;
778
bellard19cb3732006-08-19 11:45:59 +0000779 if (!drv)
780 return -ENOMEDIUM;
aliguori71d07702009-03-03 17:37:16 +0000781 if (bdrv_check_request(bs, sector_num, nb_sectors))
782 return -EIO;
bellardb3380822004-03-14 21:38:54 +0000783
aliguorieda578e2009-03-12 19:57:16 +0000784 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
bellardfc01f7e2003-06-30 10:03:06 +0000785}
786
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +0200787static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num,
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100788 int nb_sectors, int dirty)
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +0200789{
790 int64_t start, end;
Jan Kiszkac6d22832009-11-30 18:21:20 +0100791 unsigned long val, idx, bit;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100792
Jan Kiszka6ea44302009-11-30 18:21:19 +0100793 start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkac6d22832009-11-30 18:21:20 +0100794 end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100795
796 for (; start <= end; start++) {
Jan Kiszkac6d22832009-11-30 18:21:20 +0100797 idx = start / (sizeof(unsigned long) * 8);
798 bit = start % (sizeof(unsigned long) * 8);
799 val = bs->dirty_bitmap[idx];
800 if (dirty) {
Liran Schouraaa0eb72010-01-26 10:31:48 +0200801 if (!(val & (1 << bit))) {
802 bs->dirty_count++;
803 val |= 1 << bit;
804 }
Jan Kiszkac6d22832009-11-30 18:21:20 +0100805 } else {
Liran Schouraaa0eb72010-01-26 10:31:48 +0200806 if (val & (1 << bit)) {
807 bs->dirty_count--;
808 val &= ~(1 << bit);
809 }
Jan Kiszkac6d22832009-11-30 18:21:20 +0100810 }
811 bs->dirty_bitmap[idx] = val;
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +0200812 }
813}
814
ths5fafdf22007-09-16 21:08:06 +0000815/* Return < 0 if error. Important errors are:
bellard19cb3732006-08-19 11:45:59 +0000816 -EIO generic I/O error (may happen for all errors)
817 -ENOMEDIUM No media inserted.
818 -EINVAL Invalid sector number or nb_sectors
819 -EACCES Trying to write a read-only device
820*/
ths5fafdf22007-09-16 21:08:06 +0000821int bdrv_write(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000822 const uint8_t *buf, int nb_sectors)
823{
bellard83f64092006-08-01 16:21:11 +0000824 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +0000825 if (!bs->drv)
826 return -ENOMEDIUM;
bellard0849bf02003-06-30 23:17:31 +0000827 if (bs->read_only)
bellard19cb3732006-08-19 11:45:59 +0000828 return -EACCES;
aliguori71d07702009-03-03 17:37:16 +0000829 if (bdrv_check_request(bs, sector_num, nb_sectors))
830 return -EIO;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100831
Jan Kiszkac6d22832009-11-30 18:21:20 +0100832 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +0200833 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
834 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100835
aliguori42fb2802009-01-15 20:43:39 +0000836 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
bellard83f64092006-08-01 16:21:11 +0000837}
838
aliguorieda578e2009-03-12 19:57:16 +0000839int bdrv_pread(BlockDriverState *bs, int64_t offset,
840 void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000841{
Jan Kiszka6ea44302009-11-30 18:21:19 +0100842 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
bellard83f64092006-08-01 16:21:11 +0000843 int len, nb_sectors, count;
844 int64_t sector_num;
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100845 int ret;
bellard83f64092006-08-01 16:21:11 +0000846
847 count = count1;
848 /* first read to align to sector start */
Jan Kiszka6ea44302009-11-30 18:21:19 +0100849 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
bellard83f64092006-08-01 16:21:11 +0000850 if (len > count)
851 len = count;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100852 sector_num = offset >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000853 if (len > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100854 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
855 return ret;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100856 memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
bellard83f64092006-08-01 16:21:11 +0000857 count -= len;
858 if (count == 0)
859 return count1;
860 sector_num++;
861 buf += len;
862 }
863
864 /* read the sectors "in place" */
Jan Kiszka6ea44302009-11-30 18:21:19 +0100865 nb_sectors = count >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000866 if (nb_sectors > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100867 if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0)
868 return ret;
bellard83f64092006-08-01 16:21:11 +0000869 sector_num += nb_sectors;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100870 len = nb_sectors << BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000871 buf += len;
872 count -= len;
873 }
874
875 /* add data from the last sector */
876 if (count > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100877 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
878 return ret;
bellard83f64092006-08-01 16:21:11 +0000879 memcpy(buf, tmp_buf, count);
880 }
881 return count1;
882}
883
aliguorieda578e2009-03-12 19:57:16 +0000884int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
885 const void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000886{
Jan Kiszka6ea44302009-11-30 18:21:19 +0100887 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
bellard83f64092006-08-01 16:21:11 +0000888 int len, nb_sectors, count;
889 int64_t sector_num;
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100890 int ret;
bellard83f64092006-08-01 16:21:11 +0000891
892 count = count1;
893 /* first write to align to sector start */
Jan Kiszka6ea44302009-11-30 18:21:19 +0100894 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
bellard83f64092006-08-01 16:21:11 +0000895 if (len > count)
896 len = count;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100897 sector_num = offset >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000898 if (len > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100899 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
900 return ret;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100901 memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len);
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100902 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
903 return ret;
bellard83f64092006-08-01 16:21:11 +0000904 count -= len;
905 if (count == 0)
906 return count1;
907 sector_num++;
908 buf += len;
909 }
910
911 /* write the sectors "in place" */
Jan Kiszka6ea44302009-11-30 18:21:19 +0100912 nb_sectors = count >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000913 if (nb_sectors > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100914 if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0)
915 return ret;
bellard83f64092006-08-01 16:21:11 +0000916 sector_num += nb_sectors;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100917 len = nb_sectors << BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000918 buf += len;
919 count -= len;
920 }
921
922 /* add data from the last sector */
923 if (count > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100924 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
925 return ret;
bellard83f64092006-08-01 16:21:11 +0000926 memcpy(tmp_buf, buf, count);
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100927 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
928 return ret;
bellard83f64092006-08-01 16:21:11 +0000929 }
930 return count1;
931}
bellard83f64092006-08-01 16:21:11 +0000932
933/**
bellard83f64092006-08-01 16:21:11 +0000934 * Truncate file to 'offset' bytes (needed only for file protocols)
935 */
936int bdrv_truncate(BlockDriverState *bs, int64_t offset)
937{
938 BlockDriver *drv = bs->drv;
939 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000940 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000941 if (!drv->bdrv_truncate)
942 return -ENOTSUP;
Naphtali Sprei59f26892009-10-26 16:25:16 +0200943 if (bs->read_only)
944 return -EACCES;
bellard83f64092006-08-01 16:21:11 +0000945 return drv->bdrv_truncate(bs, offset);
946}
947
948/**
949 * Length of a file in bytes. Return < 0 if error or unknown.
950 */
951int64_t bdrv_getlength(BlockDriverState *bs)
952{
953 BlockDriver *drv = bs->drv;
954 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000955 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000956 if (!drv->bdrv_getlength) {
957 /* legacy mode */
Jan Kiszka6ea44302009-11-30 18:21:19 +0100958 return bs->total_sectors * BDRV_SECTOR_SIZE;
bellard83f64092006-08-01 16:21:11 +0000959 }
960 return drv->bdrv_getlength(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000961}
962
bellard19cb3732006-08-19 11:45:59 +0000963/* return 0 as number of sectors if no device present or error */
ths96b8f132007-12-17 01:35:20 +0000964void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
bellardfc01f7e2003-06-30 10:03:06 +0000965{
bellard19cb3732006-08-19 11:45:59 +0000966 int64_t length;
967 length = bdrv_getlength(bs);
968 if (length < 0)
969 length = 0;
970 else
Jan Kiszka6ea44302009-11-30 18:21:19 +0100971 length = length >> BDRV_SECTOR_BITS;
bellard19cb3732006-08-19 11:45:59 +0000972 *nb_sectors_ptr = length;
bellardfc01f7e2003-06-30 10:03:06 +0000973}
bellardcf989512004-02-16 21:56:36 +0000974
aliguorif3d54fc2008-11-25 21:50:24 +0000975struct partition {
976 uint8_t boot_ind; /* 0x80 - active */
977 uint8_t head; /* starting head */
978 uint8_t sector; /* starting sector */
979 uint8_t cyl; /* starting cylinder */
980 uint8_t sys_ind; /* What partition type */
981 uint8_t end_head; /* end head */
982 uint8_t end_sector; /* end sector */
983 uint8_t end_cyl; /* end cylinder */
984 uint32_t start_sect; /* starting sector counting from 0 */
985 uint32_t nr_sects; /* nr of sectors in partition */
986} __attribute__((packed));
987
988/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
989static int guess_disk_lchs(BlockDriverState *bs,
990 int *pcylinders, int *pheads, int *psectors)
991{
992 uint8_t buf[512];
993 int ret, i, heads, sectors, cylinders;
994 struct partition *p;
995 uint32_t nr_sects;
blueswir1a38131b2008-12-05 17:56:40 +0000996 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +0000997
998 bdrv_get_geometry(bs, &nb_sectors);
999
1000 ret = bdrv_read(bs, 0, buf, 1);
1001 if (ret < 0)
1002 return -1;
1003 /* test msdos magic */
1004 if (buf[510] != 0x55 || buf[511] != 0xaa)
1005 return -1;
1006 for(i = 0; i < 4; i++) {
1007 p = ((struct partition *)(buf + 0x1be)) + i;
1008 nr_sects = le32_to_cpu(p->nr_sects);
1009 if (nr_sects && p->end_head) {
1010 /* We make the assumption that the partition terminates on
1011 a cylinder boundary */
1012 heads = p->end_head + 1;
1013 sectors = p->end_sector & 63;
1014 if (sectors == 0)
1015 continue;
1016 cylinders = nb_sectors / (heads * sectors);
1017 if (cylinders < 1 || cylinders > 16383)
1018 continue;
1019 *pheads = heads;
1020 *psectors = sectors;
1021 *pcylinders = cylinders;
1022#if 0
1023 printf("guessed geometry: LCHS=%d %d %d\n",
1024 cylinders, heads, sectors);
1025#endif
1026 return 0;
1027 }
1028 }
1029 return -1;
1030}
1031
1032void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
1033{
1034 int translation, lba_detected = 0;
1035 int cylinders, heads, secs;
blueswir1a38131b2008-12-05 17:56:40 +00001036 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +00001037
1038 /* if a geometry hint is available, use it */
1039 bdrv_get_geometry(bs, &nb_sectors);
1040 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
1041 translation = bdrv_get_translation_hint(bs);
1042 if (cylinders != 0) {
1043 *pcyls = cylinders;
1044 *pheads = heads;
1045 *psecs = secs;
1046 } else {
1047 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
1048 if (heads > 16) {
1049 /* if heads > 16, it means that a BIOS LBA
1050 translation was active, so the default
1051 hardware geometry is OK */
1052 lba_detected = 1;
1053 goto default_geometry;
1054 } else {
1055 *pcyls = cylinders;
1056 *pheads = heads;
1057 *psecs = secs;
1058 /* disable any translation to be in sync with
1059 the logical geometry */
1060 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
1061 bdrv_set_translation_hint(bs,
1062 BIOS_ATA_TRANSLATION_NONE);
1063 }
1064 }
1065 } else {
1066 default_geometry:
1067 /* if no geometry, use a standard physical disk geometry */
1068 cylinders = nb_sectors / (16 * 63);
1069
1070 if (cylinders > 16383)
1071 cylinders = 16383;
1072 else if (cylinders < 2)
1073 cylinders = 2;
1074 *pcyls = cylinders;
1075 *pheads = 16;
1076 *psecs = 63;
1077 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
1078 if ((*pcyls * *pheads) <= 131072) {
1079 bdrv_set_translation_hint(bs,
1080 BIOS_ATA_TRANSLATION_LARGE);
1081 } else {
1082 bdrv_set_translation_hint(bs,
1083 BIOS_ATA_TRANSLATION_LBA);
1084 }
1085 }
1086 }
1087 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
1088 }
1089}
1090
ths5fafdf22007-09-16 21:08:06 +00001091void bdrv_set_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +00001092 int cyls, int heads, int secs)
1093{
1094 bs->cyls = cyls;
1095 bs->heads = heads;
1096 bs->secs = secs;
1097}
1098
1099void bdrv_set_type_hint(BlockDriverState *bs, int type)
1100{
1101 bs->type = type;
1102 bs->removable = ((type == BDRV_TYPE_CDROM ||
1103 type == BDRV_TYPE_FLOPPY));
1104}
1105
bellard46d47672004-11-16 01:45:27 +00001106void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
1107{
1108 bs->translation = translation;
1109}
1110
ths5fafdf22007-09-16 21:08:06 +00001111void bdrv_get_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +00001112 int *pcyls, int *pheads, int *psecs)
1113{
1114 *pcyls = bs->cyls;
1115 *pheads = bs->heads;
1116 *psecs = bs->secs;
1117}
1118
1119int bdrv_get_type_hint(BlockDriverState *bs)
1120{
1121 return bs->type;
1122}
1123
bellard46d47672004-11-16 01:45:27 +00001124int bdrv_get_translation_hint(BlockDriverState *bs)
1125{
1126 return bs->translation;
1127}
1128
bellardb3380822004-03-14 21:38:54 +00001129int bdrv_is_removable(BlockDriverState *bs)
1130{
1131 return bs->removable;
1132}
1133
1134int bdrv_is_read_only(BlockDriverState *bs)
1135{
1136 return bs->read_only;
1137}
1138
ths985a03b2007-12-24 16:10:43 +00001139int bdrv_is_sg(BlockDriverState *bs)
1140{
1141 return bs->sg;
1142}
1143
Christoph Hellwige900a7b2009-09-04 19:01:15 +02001144int bdrv_enable_write_cache(BlockDriverState *bs)
1145{
1146 return bs->enable_write_cache;
1147}
1148
bellard19cb3732006-08-19 11:45:59 +00001149/* XXX: no longer used */
ths5fafdf22007-09-16 21:08:06 +00001150void bdrv_set_change_cb(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +00001151 void (*change_cb)(void *opaque), void *opaque)
1152{
1153 bs->change_cb = change_cb;
1154 bs->change_opaque = opaque;
1155}
1156
bellardea2384d2004-08-01 21:59:26 +00001157int bdrv_is_encrypted(BlockDriverState *bs)
1158{
1159 if (bs->backing_hd && bs->backing_hd->encrypted)
1160 return 1;
1161 return bs->encrypted;
1162}
1163
aliguoric0f4ce72009-03-05 23:01:01 +00001164int bdrv_key_required(BlockDriverState *bs)
1165{
1166 BlockDriverState *backing_hd = bs->backing_hd;
1167
1168 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
1169 return 1;
1170 return (bs->encrypted && !bs->valid_key);
1171}
1172
bellardea2384d2004-08-01 21:59:26 +00001173int bdrv_set_key(BlockDriverState *bs, const char *key)
1174{
1175 int ret;
1176 if (bs->backing_hd && bs->backing_hd->encrypted) {
1177 ret = bdrv_set_key(bs->backing_hd, key);
1178 if (ret < 0)
1179 return ret;
1180 if (!bs->encrypted)
1181 return 0;
1182 }
Shahar Havivifd04a2a2010-03-06 00:26:13 +02001183 if (!bs->encrypted) {
1184 return -EINVAL;
1185 } else if (!bs->drv || !bs->drv->bdrv_set_key) {
1186 return -ENOMEDIUM;
1187 }
aliguoric0f4ce72009-03-05 23:01:01 +00001188 ret = bs->drv->bdrv_set_key(bs, key);
aliguoribb5fc202009-03-05 23:01:15 +00001189 if (ret < 0) {
1190 bs->valid_key = 0;
1191 } else if (!bs->valid_key) {
1192 bs->valid_key = 1;
1193 /* call the change callback now, we skipped it on open */
1194 bs->media_changed = 1;
1195 if (bs->change_cb)
1196 bs->change_cb(bs->change_opaque);
1197 }
aliguoric0f4ce72009-03-05 23:01:01 +00001198 return ret;
bellardea2384d2004-08-01 21:59:26 +00001199}
1200
1201void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
1202{
bellard19cb3732006-08-19 11:45:59 +00001203 if (!bs->drv) {
bellardea2384d2004-08-01 21:59:26 +00001204 buf[0] = '\0';
1205 } else {
1206 pstrcpy(buf, buf_size, bs->drv->format_name);
1207 }
1208}
1209
ths5fafdf22007-09-16 21:08:06 +00001210void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
bellardea2384d2004-08-01 21:59:26 +00001211 void *opaque)
1212{
1213 BlockDriver *drv;
1214
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +01001215 QLIST_FOREACH(drv, &bdrv_drivers, list) {
bellardea2384d2004-08-01 21:59:26 +00001216 it(opaque, drv->format_name);
1217 }
1218}
1219
bellardb3380822004-03-14 21:38:54 +00001220BlockDriverState *bdrv_find(const char *name)
1221{
1222 BlockDriverState *bs;
1223
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001224 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1225 if (!strcmp(name, bs->device_name)) {
bellardb3380822004-03-14 21:38:54 +00001226 return bs;
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001227 }
bellardb3380822004-03-14 21:38:54 +00001228 }
1229 return NULL;
1230}
1231
aliguori51de9762009-03-05 23:00:43 +00001232void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
bellard81d09122004-07-14 17:21:37 +00001233{
1234 BlockDriverState *bs;
1235
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001236 QTAILQ_FOREACH(bs, &bdrv_states, list) {
aliguori51de9762009-03-05 23:00:43 +00001237 it(opaque, bs);
bellard81d09122004-07-14 17:21:37 +00001238 }
1239}
1240
bellardea2384d2004-08-01 21:59:26 +00001241const char *bdrv_get_device_name(BlockDriverState *bs)
1242{
1243 return bs->device_name;
1244}
1245
pbrook7a6cba62006-06-04 11:39:07 +00001246void bdrv_flush(BlockDriverState *bs)
1247{
Christoph Hellwig3f5075a2010-01-12 13:49:23 +01001248 if (bs->drv && bs->drv->bdrv_flush)
pbrook7a6cba62006-06-04 11:39:07 +00001249 bs->drv->bdrv_flush(bs);
pbrook7a6cba62006-06-04 11:39:07 +00001250}
1251
aliguoric6ca28d2008-10-06 13:55:43 +00001252void bdrv_flush_all(void)
1253{
1254 BlockDriverState *bs;
1255
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001256 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1257 if (bs->drv && !bdrv_is_read_only(bs) &&
1258 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) {
aliguoric6ca28d2008-10-06 13:55:43 +00001259 bdrv_flush(bs);
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001260 }
1261 }
aliguoric6ca28d2008-10-06 13:55:43 +00001262}
1263
thsf58c7b32008-06-05 21:53:49 +00001264/*
1265 * Returns true iff the specified sector is present in the disk image. Drivers
1266 * not implementing the functionality are assumed to not support backing files,
1267 * hence all their sectors are reported as allocated.
1268 *
1269 * 'pnum' is set to the number of sectors (including and immediately following
1270 * the specified sector) that are known to be in the same
1271 * allocated/unallocated state.
1272 *
1273 * 'nb_sectors' is the max value 'pnum' should be set to.
1274 */
1275int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1276 int *pnum)
1277{
1278 int64_t n;
1279 if (!bs->drv->bdrv_is_allocated) {
1280 if (sector_num >= bs->total_sectors) {
1281 *pnum = 0;
1282 return 0;
1283 }
1284 n = bs->total_sectors - sector_num;
1285 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1286 return 1;
1287 }
1288 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1289}
1290
Luiz Capitulino2582bfe2010-02-03 12:41:01 -02001291void bdrv_mon_event(const BlockDriverState *bdrv,
1292 BlockMonEventAction action, int is_read)
1293{
1294 QObject *data;
1295 const char *action_str;
1296
1297 switch (action) {
1298 case BDRV_ACTION_REPORT:
1299 action_str = "report";
1300 break;
1301 case BDRV_ACTION_IGNORE:
1302 action_str = "ignore";
1303 break;
1304 case BDRV_ACTION_STOP:
1305 action_str = "stop";
1306 break;
1307 default:
1308 abort();
1309 }
1310
1311 data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
1312 bdrv->device_name,
1313 action_str,
1314 is_read ? "read" : "write");
1315 monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data);
1316
1317 qobject_decref(data);
1318}
1319
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001320static void bdrv_print_dict(QObject *obj, void *opaque)
bellardb3380822004-03-14 21:38:54 +00001321{
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001322 QDict *bs_dict;
1323 Monitor *mon = opaque;
1324
1325 bs_dict = qobject_to_qdict(obj);
1326
1327 monitor_printf(mon, "%s: type=%s removable=%d",
1328 qdict_get_str(bs_dict, "device"),
1329 qdict_get_str(bs_dict, "type"),
1330 qdict_get_bool(bs_dict, "removable"));
1331
1332 if (qdict_get_bool(bs_dict, "removable")) {
1333 monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked"));
1334 }
1335
1336 if (qdict_haskey(bs_dict, "inserted")) {
1337 QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted"));
1338
1339 monitor_printf(mon, " file=");
1340 monitor_print_filename(mon, qdict_get_str(qdict, "file"));
1341 if (qdict_haskey(qdict, "backing_file")) {
1342 monitor_printf(mon, " backing_file=");
1343 monitor_print_filename(mon, qdict_get_str(qdict, "backing_file"));
1344 }
1345 monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
1346 qdict_get_bool(qdict, "ro"),
1347 qdict_get_str(qdict, "drv"),
1348 qdict_get_bool(qdict, "encrypted"));
1349 } else {
1350 monitor_printf(mon, " [not inserted]");
1351 }
1352
1353 monitor_printf(mon, "\n");
1354}
1355
1356void bdrv_info_print(Monitor *mon, const QObject *data)
1357{
1358 qlist_iter(qobject_to_qlist(data), bdrv_print_dict, mon);
1359}
1360
1361/**
1362 * bdrv_info(): Block devices information
1363 *
1364 * Each block device information is stored in a QDict and the
1365 * returned QObject is a QList of all devices.
1366 *
1367 * The QDict contains the following:
1368 *
1369 * - "device": device name
1370 * - "type": device type
1371 * - "removable": true if the device is removable, false otherwise
1372 * - "locked": true if the device is locked, false otherwise
1373 * - "inserted": only present if the device is inserted, it is a QDict
1374 * containing the following:
1375 * - "file": device file name
1376 * - "ro": true if read-only, false otherwise
1377 * - "drv": driver format name
1378 * - "backing_file": backing file name if one is used
1379 * - "encrypted": true if encrypted, false otherwise
1380 *
1381 * Example:
1382 *
1383 * [ { "device": "ide0-hd0", "type": "hd", "removable": false, "locked": false,
1384 * "inserted": { "file": "/tmp/foobar", "ro": false, "drv": "qcow2" } },
1385 * { "device": "floppy0", "type": "floppy", "removable": true,
1386 * "locked": false } ]
1387 */
1388void bdrv_info(Monitor *mon, QObject **ret_data)
1389{
1390 QList *bs_list;
bellardb3380822004-03-14 21:38:54 +00001391 BlockDriverState *bs;
1392
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001393 bs_list = qlist_new();
1394
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001395 QTAILQ_FOREACH(bs, &bdrv_states, list) {
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001396 QObject *bs_obj;
1397 const char *type = "unknown";
1398
bellardb3380822004-03-14 21:38:54 +00001399 switch(bs->type) {
1400 case BDRV_TYPE_HD:
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001401 type = "hd";
bellardb3380822004-03-14 21:38:54 +00001402 break;
1403 case BDRV_TYPE_CDROM:
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001404 type = "cdrom";
bellardb3380822004-03-14 21:38:54 +00001405 break;
1406 case BDRV_TYPE_FLOPPY:
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001407 type = "floppy";
bellardb3380822004-03-14 21:38:54 +00001408 break;
1409 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001410
1411 bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': %s, "
1412 "'removable': %i, 'locked': %i }",
1413 bs->device_name, type, bs->removable,
1414 bs->locked);
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001415
bellard19cb3732006-08-19 11:45:59 +00001416 if (bs->drv) {
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001417 QObject *obj;
1418 QDict *bs_dict = qobject_to_qdict(bs_obj);
1419
1420 obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, "
1421 "'encrypted': %i }",
1422 bs->filename, bs->read_only,
1423 bs->drv->format_name,
1424 bdrv_is_encrypted(bs));
thsfef30742006-12-22 14:11:32 +00001425 if (bs->backing_file[0] != '\0') {
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001426 QDict *qdict = qobject_to_qdict(obj);
1427 qdict_put(qdict, "backing_file",
1428 qstring_from_str(bs->backing_file));
aliguori376253e2009-03-05 23:01:23 +00001429 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001430
1431 qdict_put_obj(bs_dict, "inserted", obj);
bellardb3380822004-03-14 21:38:54 +00001432 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001433 qlist_append_obj(bs_list, bs_obj);
bellardb3380822004-03-14 21:38:54 +00001434 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001435
1436 *ret_data = QOBJECT(bs_list);
bellardb3380822004-03-14 21:38:54 +00001437}
thsa36e69d2007-12-02 05:18:19 +00001438
Luiz Capitulino218a5362009-12-10 17:16:07 -02001439static void bdrv_stats_iter(QObject *data, void *opaque)
thsa36e69d2007-12-02 05:18:19 +00001440{
Luiz Capitulino218a5362009-12-10 17:16:07 -02001441 QDict *qdict;
1442 Monitor *mon = opaque;
1443
1444 qdict = qobject_to_qdict(data);
1445 monitor_printf(mon, "%s:", qdict_get_str(qdict, "device"));
1446
1447 qdict = qobject_to_qdict(qdict_get(qdict, "stats"));
1448 monitor_printf(mon, " rd_bytes=%" PRId64
1449 " wr_bytes=%" PRId64
1450 " rd_operations=%" PRId64
1451 " wr_operations=%" PRId64
1452 "\n",
1453 qdict_get_int(qdict, "rd_bytes"),
1454 qdict_get_int(qdict, "wr_bytes"),
1455 qdict_get_int(qdict, "rd_operations"),
1456 qdict_get_int(qdict, "wr_operations"));
1457}
1458
1459void bdrv_stats_print(Monitor *mon, const QObject *data)
1460{
1461 qlist_iter(qobject_to_qlist(data), bdrv_stats_iter, mon);
1462}
1463
1464/**
1465 * bdrv_info_stats(): show block device statistics
1466 *
1467 * Each device statistic information is stored in a QDict and
1468 * the returned QObject is a QList of all devices.
1469 *
1470 * The QDict contains the following:
1471 *
1472 * - "device": device name
1473 * - "stats": A QDict with the statistics information, it contains:
1474 * - "rd_bytes": bytes read
1475 * - "wr_bytes": bytes written
1476 * - "rd_operations": read operations
1477 * - "wr_operations": write operations
1478 *
1479 * Example:
1480 *
1481 * [ { "device": "ide0-hd0",
1482 * "stats": { "rd_bytes": 512,
1483 * "wr_bytes": 0,
1484 * "rd_operations": 1,
1485 * "wr_operations": 0 } },
1486 * { "device": "ide1-cd0",
1487 * "stats": { "rd_bytes": 0,
1488 * "wr_bytes": 0,
1489 * "rd_operations": 0,
1490 * "wr_operations": 0 } } ]
1491 */
1492void bdrv_info_stats(Monitor *mon, QObject **ret_data)
1493{
1494 QObject *obj;
1495 QList *devices;
thsa36e69d2007-12-02 05:18:19 +00001496 BlockDriverState *bs;
1497
Luiz Capitulino218a5362009-12-10 17:16:07 -02001498 devices = qlist_new();
1499
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001500 QTAILQ_FOREACH(bs, &bdrv_states, list) {
Luiz Capitulino218a5362009-12-10 17:16:07 -02001501 obj = qobject_from_jsonf("{ 'device': %s, 'stats': {"
1502 "'rd_bytes': %" PRId64 ","
1503 "'wr_bytes': %" PRId64 ","
1504 "'rd_operations': %" PRId64 ","
1505 "'wr_operations': %" PRId64
1506 "} }",
1507 bs->device_name,
1508 bs->rd_bytes, bs->wr_bytes,
1509 bs->rd_ops, bs->wr_ops);
Luiz Capitulino218a5362009-12-10 17:16:07 -02001510 qlist_append_obj(devices, obj);
thsa36e69d2007-12-02 05:18:19 +00001511 }
Luiz Capitulino218a5362009-12-10 17:16:07 -02001512
1513 *ret_data = QOBJECT(devices);
thsa36e69d2007-12-02 05:18:19 +00001514}
bellardea2384d2004-08-01 21:59:26 +00001515
aliguori045df332009-03-05 23:00:48 +00001516const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1517{
1518 if (bs->backing_hd && bs->backing_hd->encrypted)
1519 return bs->backing_file;
1520 else if (bs->encrypted)
1521 return bs->filename;
1522 else
1523 return NULL;
1524}
1525
ths5fafdf22007-09-16 21:08:06 +00001526void bdrv_get_backing_filename(BlockDriverState *bs,
bellard83f64092006-08-01 16:21:11 +00001527 char *filename, int filename_size)
bellardea2384d2004-08-01 21:59:26 +00001528{
Kevin Wolfb783e402010-01-12 12:55:16 +01001529 if (!bs->backing_file) {
bellard83f64092006-08-01 16:21:11 +00001530 pstrcpy(filename, filename_size, "");
1531 } else {
1532 pstrcpy(filename, filename_size, bs->backing_file);
1533 }
bellardea2384d2004-08-01 21:59:26 +00001534}
1535
ths5fafdf22007-09-16 21:08:06 +00001536int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
bellardfaea38e2006-08-05 21:31:00 +00001537 const uint8_t *buf, int nb_sectors)
1538{
1539 BlockDriver *drv = bs->drv;
1540 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001541 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001542 if (!drv->bdrv_write_compressed)
1543 return -ENOTSUP;
Kevin Wolffbb7b4e2009-05-08 14:47:24 +02001544 if (bdrv_check_request(bs, sector_num, nb_sectors))
1545 return -EIO;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001546
Jan Kiszkac6d22832009-11-30 18:21:20 +01001547 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001548 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1549 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001550
bellardfaea38e2006-08-05 21:31:00 +00001551 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1552}
ths3b46e622007-09-17 08:09:54 +00001553
bellardfaea38e2006-08-05 21:31:00 +00001554int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1555{
1556 BlockDriver *drv = bs->drv;
1557 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001558 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001559 if (!drv->bdrv_get_info)
1560 return -ENOTSUP;
1561 memset(bdi, 0, sizeof(*bdi));
1562 return drv->bdrv_get_info(bs, bdi);
1563}
1564
Christoph Hellwig45566e92009-07-10 23:11:57 +02001565int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
1566 int64_t pos, int size)
aliguori178e08a2009-04-05 19:10:55 +00001567{
1568 BlockDriver *drv = bs->drv;
1569 if (!drv)
1570 return -ENOMEDIUM;
Christoph Hellwig45566e92009-07-10 23:11:57 +02001571 if (!drv->bdrv_save_vmstate)
aliguori178e08a2009-04-05 19:10:55 +00001572 return -ENOTSUP;
Christoph Hellwig45566e92009-07-10 23:11:57 +02001573 return drv->bdrv_save_vmstate(bs, buf, pos, size);
aliguori178e08a2009-04-05 19:10:55 +00001574}
1575
Christoph Hellwig45566e92009-07-10 23:11:57 +02001576int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1577 int64_t pos, int size)
aliguori178e08a2009-04-05 19:10:55 +00001578{
1579 BlockDriver *drv = bs->drv;
1580 if (!drv)
1581 return -ENOMEDIUM;
Christoph Hellwig45566e92009-07-10 23:11:57 +02001582 if (!drv->bdrv_load_vmstate)
aliguori178e08a2009-04-05 19:10:55 +00001583 return -ENOTSUP;
Christoph Hellwig45566e92009-07-10 23:11:57 +02001584 return drv->bdrv_load_vmstate(bs, buf, pos, size);
aliguori178e08a2009-04-05 19:10:55 +00001585}
1586
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01001587void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
1588{
1589 BlockDriver *drv = bs->drv;
1590
1591 if (!drv || !drv->bdrv_debug_event) {
1592 return;
1593 }
1594
1595 return drv->bdrv_debug_event(bs, event);
1596
1597}
1598
bellardfaea38e2006-08-05 21:31:00 +00001599/**************************************************************/
1600/* handling of snapshots */
1601
ths5fafdf22007-09-16 21:08:06 +00001602int bdrv_snapshot_create(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001603 QEMUSnapshotInfo *sn_info)
1604{
1605 BlockDriver *drv = bs->drv;
1606 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001607 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001608 if (!drv->bdrv_snapshot_create)
1609 return -ENOTSUP;
1610 return drv->bdrv_snapshot_create(bs, sn_info);
1611}
1612
ths5fafdf22007-09-16 21:08:06 +00001613int bdrv_snapshot_goto(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001614 const char *snapshot_id)
1615{
1616 BlockDriver *drv = bs->drv;
1617 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001618 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001619 if (!drv->bdrv_snapshot_goto)
1620 return -ENOTSUP;
1621 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1622}
1623
1624int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1625{
1626 BlockDriver *drv = bs->drv;
1627 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001628 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001629 if (!drv->bdrv_snapshot_delete)
1630 return -ENOTSUP;
1631 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1632}
1633
ths5fafdf22007-09-16 21:08:06 +00001634int bdrv_snapshot_list(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001635 QEMUSnapshotInfo **psn_info)
1636{
1637 BlockDriver *drv = bs->drv;
1638 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001639 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001640 if (!drv->bdrv_snapshot_list)
1641 return -ENOTSUP;
1642 return drv->bdrv_snapshot_list(bs, psn_info);
1643}
1644
1645#define NB_SUFFIXES 4
1646
1647char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1648{
1649 static const char suffixes[NB_SUFFIXES] = "KMGT";
1650 int64_t base;
1651 int i;
1652
1653 if (size <= 999) {
1654 snprintf(buf, buf_size, "%" PRId64, size);
1655 } else {
1656 base = 1024;
1657 for(i = 0; i < NB_SUFFIXES; i++) {
1658 if (size < (10 * base)) {
ths5fafdf22007-09-16 21:08:06 +00001659 snprintf(buf, buf_size, "%0.1f%c",
bellardfaea38e2006-08-05 21:31:00 +00001660 (double)size / base,
1661 suffixes[i]);
1662 break;
1663 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
ths5fafdf22007-09-16 21:08:06 +00001664 snprintf(buf, buf_size, "%" PRId64 "%c",
bellardfaea38e2006-08-05 21:31:00 +00001665 ((size + (base >> 1)) / base),
1666 suffixes[i]);
1667 break;
1668 }
1669 base = base * 1024;
1670 }
1671 }
1672 return buf;
1673}
1674
1675char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1676{
1677 char buf1[128], date_buf[128], clock_buf[128];
bellard3b9f94e2007-01-07 17:27:07 +00001678#ifdef _WIN32
1679 struct tm *ptm;
1680#else
bellardfaea38e2006-08-05 21:31:00 +00001681 struct tm tm;
bellard3b9f94e2007-01-07 17:27:07 +00001682#endif
bellardfaea38e2006-08-05 21:31:00 +00001683 time_t ti;
1684 int64_t secs;
1685
1686 if (!sn) {
ths5fafdf22007-09-16 21:08:06 +00001687 snprintf(buf, buf_size,
1688 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001689 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1690 } else {
1691 ti = sn->date_sec;
bellard3b9f94e2007-01-07 17:27:07 +00001692#ifdef _WIN32
1693 ptm = localtime(&ti);
1694 strftime(date_buf, sizeof(date_buf),
1695 "%Y-%m-%d %H:%M:%S", ptm);
1696#else
bellardfaea38e2006-08-05 21:31:00 +00001697 localtime_r(&ti, &tm);
1698 strftime(date_buf, sizeof(date_buf),
1699 "%Y-%m-%d %H:%M:%S", &tm);
bellard3b9f94e2007-01-07 17:27:07 +00001700#endif
bellardfaea38e2006-08-05 21:31:00 +00001701 secs = sn->vm_clock_nsec / 1000000000;
1702 snprintf(clock_buf, sizeof(clock_buf),
1703 "%02d:%02d:%02d.%03d",
1704 (int)(secs / 3600),
1705 (int)((secs / 60) % 60),
ths5fafdf22007-09-16 21:08:06 +00001706 (int)(secs % 60),
bellardfaea38e2006-08-05 21:31:00 +00001707 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1708 snprintf(buf, buf_size,
ths5fafdf22007-09-16 21:08:06 +00001709 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001710 sn->id_str, sn->name,
1711 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1712 date_buf,
1713 clock_buf);
1714 }
1715 return buf;
1716}
1717
bellardea2384d2004-08-01 21:59:26 +00001718
bellard83f64092006-08-01 16:21:11 +00001719/**************************************************************/
1720/* async I/Os */
1721
aliguori3b69e4b2009-01-22 16:59:24 +00001722BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
aliguorif141eaf2009-04-07 18:43:24 +00001723 QEMUIOVector *qiov, int nb_sectors,
aliguori3b69e4b2009-01-22 16:59:24 +00001724 BlockDriverCompletionFunc *cb, void *opaque)
1725{
bellard83f64092006-08-01 16:21:11 +00001726 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001727 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001728
bellard19cb3732006-08-19 11:45:59 +00001729 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001730 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001731 if (bdrv_check_request(bs, sector_num, nb_sectors))
1732 return NULL;
ths3b46e622007-09-17 08:09:54 +00001733
aliguorif141eaf2009-04-07 18:43:24 +00001734 ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
1735 cb, opaque);
thsa36e69d2007-12-02 05:18:19 +00001736
1737 if (ret) {
1738 /* Update stats even though technically transfer has not happened. */
Jan Kiszka6ea44302009-11-30 18:21:19 +01001739 bs->rd_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
thsa36e69d2007-12-02 05:18:19 +00001740 bs->rd_ops ++;
1741 }
1742
1743 return ret;
bellard83f64092006-08-01 16:21:11 +00001744}
1745
aliguorif141eaf2009-04-07 18:43:24 +00001746BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1747 QEMUIOVector *qiov, int nb_sectors,
1748 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001749{
bellard83f64092006-08-01 16:21:11 +00001750 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001751 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001752
bellard19cb3732006-08-19 11:45:59 +00001753 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001754 return NULL;
bellard83f64092006-08-01 16:21:11 +00001755 if (bs->read_only)
pbrookce1a14d2006-08-07 02:38:06 +00001756 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001757 if (bdrv_check_request(bs, sector_num, nb_sectors))
1758 return NULL;
bellard83f64092006-08-01 16:21:11 +00001759
Jan Kiszkac6d22832009-11-30 18:21:20 +01001760 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001761 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1762 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001763
aliguorif141eaf2009-04-07 18:43:24 +00001764 ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
1765 cb, opaque);
thsa36e69d2007-12-02 05:18:19 +00001766
1767 if (ret) {
1768 /* Update stats even though technically transfer has not happened. */
Jan Kiszka6ea44302009-11-30 18:21:19 +01001769 bs->wr_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
thsa36e69d2007-12-02 05:18:19 +00001770 bs->wr_ops ++;
1771 }
1772
1773 return ret;
bellard83f64092006-08-01 16:21:11 +00001774}
1775
Kevin Wolf40b4f532009-09-09 17:53:37 +02001776
1777typedef struct MultiwriteCB {
1778 int error;
1779 int num_requests;
1780 int num_callbacks;
1781 struct {
1782 BlockDriverCompletionFunc *cb;
1783 void *opaque;
1784 QEMUIOVector *free_qiov;
1785 void *free_buf;
1786 } callbacks[];
1787} MultiwriteCB;
1788
1789static void multiwrite_user_cb(MultiwriteCB *mcb)
1790{
1791 int i;
1792
1793 for (i = 0; i < mcb->num_callbacks; i++) {
1794 mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
Stefan Hajnoczi1e1ea482010-04-21 20:35:45 +01001795 if (mcb->callbacks[i].free_qiov) {
1796 qemu_iovec_destroy(mcb->callbacks[i].free_qiov);
1797 }
Kevin Wolf40b4f532009-09-09 17:53:37 +02001798 qemu_free(mcb->callbacks[i].free_qiov);
Herve Poussineauf8a83242010-01-24 21:23:56 +00001799 qemu_vfree(mcb->callbacks[i].free_buf);
Kevin Wolf40b4f532009-09-09 17:53:37 +02001800 }
1801}
1802
1803static void multiwrite_cb(void *opaque, int ret)
1804{
1805 MultiwriteCB *mcb = opaque;
1806
Kevin Wolfcb6d3ca2010-04-01 22:48:44 +02001807 if (ret < 0 && !mcb->error) {
Kevin Wolf40b4f532009-09-09 17:53:37 +02001808 mcb->error = ret;
1809 multiwrite_user_cb(mcb);
1810 }
1811
1812 mcb->num_requests--;
1813 if (mcb->num_requests == 0) {
1814 if (mcb->error == 0) {
1815 multiwrite_user_cb(mcb);
1816 }
1817 qemu_free(mcb);
1818 }
1819}
1820
1821static int multiwrite_req_compare(const void *a, const void *b)
1822{
1823 return (((BlockRequest*) a)->sector - ((BlockRequest*) b)->sector);
1824}
1825
1826/*
1827 * Takes a bunch of requests and tries to merge them. Returns the number of
1828 * requests that remain after merging.
1829 */
1830static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
1831 int num_reqs, MultiwriteCB *mcb)
1832{
1833 int i, outidx;
1834
1835 // Sort requests by start sector
1836 qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
1837
1838 // Check if adjacent requests touch the same clusters. If so, combine them,
1839 // filling up gaps with zero sectors.
1840 outidx = 0;
1841 for (i = 1; i < num_reqs; i++) {
1842 int merge = 0;
1843 int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
1844
1845 // This handles the cases that are valid for all block drivers, namely
1846 // exactly sequential writes and overlapping writes.
1847 if (reqs[i].sector <= oldreq_last) {
1848 merge = 1;
1849 }
1850
1851 // The block driver may decide that it makes sense to combine requests
1852 // even if there is a gap of some sectors between them. In this case,
1853 // the gap is filled with zeros (therefore only applicable for yet
1854 // unused space in format like qcow2).
1855 if (!merge && bs->drv->bdrv_merge_requests) {
1856 merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]);
1857 }
1858
Christoph Hellwige2a305f2010-01-26 14:49:08 +01001859 if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) {
1860 merge = 0;
1861 }
1862
Kevin Wolf40b4f532009-09-09 17:53:37 +02001863 if (merge) {
1864 size_t size;
1865 QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov));
1866 qemu_iovec_init(qiov,
1867 reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
1868
1869 // Add the first request to the merged one. If the requests are
1870 // overlapping, drop the last sectors of the first request.
1871 size = (reqs[i].sector - reqs[outidx].sector) << 9;
1872 qemu_iovec_concat(qiov, reqs[outidx].qiov, size);
1873
1874 // We might need to add some zeros between the two requests
1875 if (reqs[i].sector > oldreq_last) {
1876 size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
1877 uint8_t *buf = qemu_blockalign(bs, zero_bytes);
1878 memset(buf, 0, zero_bytes);
1879 qemu_iovec_add(qiov, buf, zero_bytes);
1880 mcb->callbacks[i].free_buf = buf;
1881 }
1882
1883 // Add the second request
1884 qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size);
1885
1886 reqs[outidx].nb_sectors += reqs[i].nb_sectors;
1887 reqs[outidx].qiov = qiov;
1888
1889 mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
1890 } else {
1891 outidx++;
1892 reqs[outidx].sector = reqs[i].sector;
1893 reqs[outidx].nb_sectors = reqs[i].nb_sectors;
1894 reqs[outidx].qiov = reqs[i].qiov;
1895 }
1896 }
1897
1898 return outidx + 1;
1899}
1900
1901/*
1902 * Submit multiple AIO write requests at once.
1903 *
1904 * On success, the function returns 0 and all requests in the reqs array have
1905 * been submitted. In error case this function returns -1, and any of the
1906 * requests may or may not be submitted yet. In particular, this means that the
1907 * callback will be called for some of the requests, for others it won't. The
1908 * caller must check the error field of the BlockRequest to wait for the right
1909 * callbacks (if error != 0, no callback will be called).
1910 *
1911 * The implementation may modify the contents of the reqs array, e.g. to merge
1912 * requests. However, the fields opaque and error are left unmodified as they
1913 * are used to signal failure for a single request to the caller.
1914 */
1915int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
1916{
1917 BlockDriverAIOCB *acb;
1918 MultiwriteCB *mcb;
1919 int i;
1920
1921 if (num_reqs == 0) {
1922 return 0;
1923 }
1924
1925 // Create MultiwriteCB structure
1926 mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
1927 mcb->num_requests = 0;
1928 mcb->num_callbacks = num_reqs;
1929
1930 for (i = 0; i < num_reqs; i++) {
1931 mcb->callbacks[i].cb = reqs[i].cb;
1932 mcb->callbacks[i].opaque = reqs[i].opaque;
1933 }
1934
1935 // Check for mergable requests
1936 num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
1937
1938 // Run the aio requests
1939 for (i = 0; i < num_reqs; i++) {
1940 acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
1941 reqs[i].nb_sectors, multiwrite_cb, mcb);
1942
1943 if (acb == NULL) {
1944 // We can only fail the whole thing if no request has been
1945 // submitted yet. Otherwise we'll wait for the submitted AIOs to
1946 // complete and report the error in the callback.
1947 if (mcb->num_requests == 0) {
Kevin Wolf0f0b6042010-04-06 18:24:06 +02001948 reqs[i].error = -EIO;
Kevin Wolf40b4f532009-09-09 17:53:37 +02001949 goto fail;
1950 } else {
Kevin Wolf7eb58a62010-04-06 18:24:07 +02001951 mcb->num_requests++;
1952 multiwrite_cb(mcb, -EIO);
Kevin Wolf40b4f532009-09-09 17:53:37 +02001953 break;
1954 }
1955 } else {
1956 mcb->num_requests++;
1957 }
1958 }
1959
1960 return 0;
1961
1962fail:
1963 free(mcb);
1964 return -1;
1965}
1966
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02001967BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
1968 BlockDriverCompletionFunc *cb, void *opaque)
1969{
1970 BlockDriver *drv = bs->drv;
1971
1972 if (!drv)
1973 return NULL;
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02001974 return drv->bdrv_aio_flush(bs, cb, opaque);
1975}
1976
bellard83f64092006-08-01 16:21:11 +00001977void bdrv_aio_cancel(BlockDriverAIOCB *acb)
pbrookce1a14d2006-08-07 02:38:06 +00001978{
aliguori6bbff9a2009-03-20 18:25:59 +00001979 acb->pool->cancel(acb);
bellard83f64092006-08-01 16:21:11 +00001980}
1981
pbrookce1a14d2006-08-07 02:38:06 +00001982
bellard83f64092006-08-01 16:21:11 +00001983/**************************************************************/
1984/* async block device emulation */
1985
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02001986typedef struct BlockDriverAIOCBSync {
1987 BlockDriverAIOCB common;
1988 QEMUBH *bh;
1989 int ret;
1990 /* vector translation state */
1991 QEMUIOVector *qiov;
1992 uint8_t *bounce;
1993 int is_write;
1994} BlockDriverAIOCBSync;
1995
1996static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1997{
1998 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
Dor Laor6a7ad292009-06-01 12:07:23 +03001999 qemu_bh_delete(acb->bh);
Avi Kivity36afc452009-06-23 16:20:36 +03002000 acb->bh = NULL;
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002001 qemu_aio_release(acb);
2002}
2003
2004static AIOPool bdrv_em_aio_pool = {
2005 .aiocb_size = sizeof(BlockDriverAIOCBSync),
2006 .cancel = bdrv_aio_cancel_em,
2007};
2008
bellard83f64092006-08-01 16:21:11 +00002009static void bdrv_aio_bh_cb(void *opaque)
bellardbeac80c2006-06-26 20:08:57 +00002010{
pbrookce1a14d2006-08-07 02:38:06 +00002011 BlockDriverAIOCBSync *acb = opaque;
aliguorif141eaf2009-04-07 18:43:24 +00002012
aliguorif141eaf2009-04-07 18:43:24 +00002013 if (!acb->is_write)
2014 qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
aliguoriceb42de2009-04-07 18:43:28 +00002015 qemu_vfree(acb->bounce);
pbrookce1a14d2006-08-07 02:38:06 +00002016 acb->common.cb(acb->common.opaque, acb->ret);
Dor Laor6a7ad292009-06-01 12:07:23 +03002017 qemu_bh_delete(acb->bh);
Avi Kivity36afc452009-06-23 16:20:36 +03002018 acb->bh = NULL;
pbrookce1a14d2006-08-07 02:38:06 +00002019 qemu_aio_release(acb);
bellardbeac80c2006-06-26 20:08:57 +00002020}
bellardbeac80c2006-06-26 20:08:57 +00002021
aliguorif141eaf2009-04-07 18:43:24 +00002022static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
2023 int64_t sector_num,
2024 QEMUIOVector *qiov,
2025 int nb_sectors,
2026 BlockDriverCompletionFunc *cb,
2027 void *opaque,
2028 int is_write)
2029
bellardea2384d2004-08-01 21:59:26 +00002030{
pbrookce1a14d2006-08-07 02:38:06 +00002031 BlockDriverAIOCBSync *acb;
pbrookce1a14d2006-08-07 02:38:06 +00002032
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002033 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
aliguorif141eaf2009-04-07 18:43:24 +00002034 acb->is_write = is_write;
2035 acb->qiov = qiov;
aliguorie268ca52009-04-22 20:20:00 +00002036 acb->bounce = qemu_blockalign(bs, qiov->size);
aliguorif141eaf2009-04-07 18:43:24 +00002037
pbrookce1a14d2006-08-07 02:38:06 +00002038 if (!acb->bh)
2039 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
aliguorif141eaf2009-04-07 18:43:24 +00002040
2041 if (is_write) {
2042 qemu_iovec_to_buffer(acb->qiov, acb->bounce);
2043 acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
2044 } else {
2045 acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
2046 }
2047
pbrookce1a14d2006-08-07 02:38:06 +00002048 qemu_bh_schedule(acb->bh);
aliguorif141eaf2009-04-07 18:43:24 +00002049
pbrookce1a14d2006-08-07 02:38:06 +00002050 return &acb->common;
pbrook7a6cba62006-06-04 11:39:07 +00002051}
2052
aliguorif141eaf2009-04-07 18:43:24 +00002053static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
2054 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
pbrookce1a14d2006-08-07 02:38:06 +00002055 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00002056{
aliguorif141eaf2009-04-07 18:43:24 +00002057 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
bellard83f64092006-08-01 16:21:11 +00002058}
2059
aliguorif141eaf2009-04-07 18:43:24 +00002060static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
2061 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2062 BlockDriverCompletionFunc *cb, void *opaque)
2063{
2064 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
2065}
2066
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002067static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
2068 BlockDriverCompletionFunc *cb, void *opaque)
2069{
2070 BlockDriverAIOCBSync *acb;
2071
2072 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2073 acb->is_write = 1; /* don't bounce in the completion hadler */
2074 acb->qiov = NULL;
2075 acb->bounce = NULL;
2076 acb->ret = 0;
2077
2078 if (!acb->bh)
2079 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2080
2081 bdrv_flush(bs);
2082 qemu_bh_schedule(acb->bh);
2083 return &acb->common;
2084}
2085
bellard83f64092006-08-01 16:21:11 +00002086/**************************************************************/
2087/* sync block device emulation */
2088
2089static void bdrv_rw_em_cb(void *opaque, int ret)
2090{
2091 *(int *)opaque = ret;
2092}
2093
2094#define NOT_DONE 0x7fffffff
2095
ths5fafdf22007-09-16 21:08:06 +00002096static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +00002097 uint8_t *buf, int nb_sectors)
2098{
pbrookce1a14d2006-08-07 02:38:06 +00002099 int async_ret;
2100 BlockDriverAIOCB *acb;
aliguorif141eaf2009-04-07 18:43:24 +00002101 struct iovec iov;
2102 QEMUIOVector qiov;
bellard83f64092006-08-01 16:21:11 +00002103
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002104 async_context_push();
2105
bellard83f64092006-08-01 16:21:11 +00002106 async_ret = NOT_DONE;
blueswir13f4cb3d2009-04-13 16:31:01 +00002107 iov.iov_base = (void *)buf;
aliguorif141eaf2009-04-07 18:43:24 +00002108 iov.iov_len = nb_sectors * 512;
2109 qemu_iovec_init_external(&qiov, &iov, 1);
2110 acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
2111 bdrv_rw_em_cb, &async_ret);
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002112 if (acb == NULL) {
2113 async_ret = -1;
2114 goto fail;
2115 }
aliguoribaf35cb2008-09-10 15:45:19 +00002116
bellard83f64092006-08-01 16:21:11 +00002117 while (async_ret == NOT_DONE) {
2118 qemu_aio_wait();
2119 }
aliguoribaf35cb2008-09-10 15:45:19 +00002120
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002121
2122fail:
2123 async_context_pop();
bellard83f64092006-08-01 16:21:11 +00002124 return async_ret;
2125}
2126
2127static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
2128 const uint8_t *buf, int nb_sectors)
2129{
pbrookce1a14d2006-08-07 02:38:06 +00002130 int async_ret;
2131 BlockDriverAIOCB *acb;
aliguorif141eaf2009-04-07 18:43:24 +00002132 struct iovec iov;
2133 QEMUIOVector qiov;
bellard83f64092006-08-01 16:21:11 +00002134
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002135 async_context_push();
2136
bellard83f64092006-08-01 16:21:11 +00002137 async_ret = NOT_DONE;
aliguorif141eaf2009-04-07 18:43:24 +00002138 iov.iov_base = (void *)buf;
2139 iov.iov_len = nb_sectors * 512;
2140 qemu_iovec_init_external(&qiov, &iov, 1);
2141 acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
2142 bdrv_rw_em_cb, &async_ret);
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002143 if (acb == NULL) {
2144 async_ret = -1;
2145 goto fail;
2146 }
bellard83f64092006-08-01 16:21:11 +00002147 while (async_ret == NOT_DONE) {
2148 qemu_aio_wait();
2149 }
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002150
2151fail:
2152 async_context_pop();
bellard83f64092006-08-01 16:21:11 +00002153 return async_ret;
2154}
bellardea2384d2004-08-01 21:59:26 +00002155
2156void bdrv_init(void)
2157{
Anthony Liguori5efa9d52009-05-09 17:03:42 -05002158 module_call_init(MODULE_INIT_BLOCK);
bellardea2384d2004-08-01 21:59:26 +00002159}
pbrookce1a14d2006-08-07 02:38:06 +00002160
Markus Armbrustereb852012009-10-27 18:41:44 +01002161void bdrv_init_with_whitelist(void)
2162{
2163 use_bdrv_whitelist = 1;
2164 bdrv_init();
2165}
2166
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002167void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
2168 BlockDriverCompletionFunc *cb, void *opaque)
aliguori6bbff9a2009-03-20 18:25:59 +00002169{
pbrookce1a14d2006-08-07 02:38:06 +00002170 BlockDriverAIOCB *acb;
2171
aliguori6bbff9a2009-03-20 18:25:59 +00002172 if (pool->free_aiocb) {
2173 acb = pool->free_aiocb;
2174 pool->free_aiocb = acb->next;
pbrookce1a14d2006-08-07 02:38:06 +00002175 } else {
aliguori6bbff9a2009-03-20 18:25:59 +00002176 acb = qemu_mallocz(pool->aiocb_size);
2177 acb->pool = pool;
pbrookce1a14d2006-08-07 02:38:06 +00002178 }
2179 acb->bs = bs;
2180 acb->cb = cb;
2181 acb->opaque = opaque;
2182 return acb;
2183}
2184
2185void qemu_aio_release(void *p)
2186{
aliguori6bbff9a2009-03-20 18:25:59 +00002187 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
2188 AIOPool *pool = acb->pool;
2189 acb->next = pool->free_aiocb;
2190 pool->free_aiocb = acb;
pbrookce1a14d2006-08-07 02:38:06 +00002191}
bellard19cb3732006-08-19 11:45:59 +00002192
2193/**************************************************************/
2194/* removable device support */
2195
2196/**
2197 * Return TRUE if the media is present
2198 */
2199int bdrv_is_inserted(BlockDriverState *bs)
2200{
2201 BlockDriver *drv = bs->drv;
2202 int ret;
2203 if (!drv)
2204 return 0;
2205 if (!drv->bdrv_is_inserted)
2206 return 1;
2207 ret = drv->bdrv_is_inserted(bs);
2208 return ret;
2209}
2210
2211/**
2212 * Return TRUE if the media changed since the last call to this
ths5fafdf22007-09-16 21:08:06 +00002213 * function. It is currently only used for floppy disks
bellard19cb3732006-08-19 11:45:59 +00002214 */
2215int bdrv_media_changed(BlockDriverState *bs)
2216{
2217 BlockDriver *drv = bs->drv;
2218 int ret;
2219
2220 if (!drv || !drv->bdrv_media_changed)
2221 ret = -ENOTSUP;
2222 else
2223 ret = drv->bdrv_media_changed(bs);
2224 if (ret == -ENOTSUP)
2225 ret = bs->media_changed;
2226 bs->media_changed = 0;
2227 return ret;
2228}
2229
2230/**
2231 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
2232 */
Mark McLoughlinaea2a332009-05-27 10:06:11 +01002233int bdrv_eject(BlockDriverState *bs, int eject_flag)
bellard19cb3732006-08-19 11:45:59 +00002234{
2235 BlockDriver *drv = bs->drv;
2236 int ret;
2237
Mark McLoughlinaea2a332009-05-27 10:06:11 +01002238 if (bs->locked) {
2239 return -EBUSY;
2240 }
2241
bellard19cb3732006-08-19 11:45:59 +00002242 if (!drv || !drv->bdrv_eject) {
2243 ret = -ENOTSUP;
2244 } else {
2245 ret = drv->bdrv_eject(bs, eject_flag);
2246 }
2247 if (ret == -ENOTSUP) {
2248 if (eject_flag)
2249 bdrv_close(bs);
Mark McLoughlinaea2a332009-05-27 10:06:11 +01002250 ret = 0;
bellard19cb3732006-08-19 11:45:59 +00002251 }
Mark McLoughlinaea2a332009-05-27 10:06:11 +01002252
2253 return ret;
bellard19cb3732006-08-19 11:45:59 +00002254}
2255
2256int bdrv_is_locked(BlockDriverState *bs)
2257{
2258 return bs->locked;
2259}
2260
2261/**
2262 * Lock or unlock the media (if it is locked, the user won't be able
2263 * to eject it manually).
2264 */
2265void bdrv_set_locked(BlockDriverState *bs, int locked)
2266{
2267 BlockDriver *drv = bs->drv;
2268
2269 bs->locked = locked;
2270 if (drv && drv->bdrv_set_locked) {
2271 drv->bdrv_set_locked(bs, locked);
2272 }
2273}
ths985a03b2007-12-24 16:10:43 +00002274
2275/* needed for generic scsi interface */
2276
2277int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
2278{
2279 BlockDriver *drv = bs->drv;
2280
2281 if (drv && drv->bdrv_ioctl)
2282 return drv->bdrv_ioctl(bs, req, buf);
2283 return -ENOTSUP;
2284}
aliguori7d780662009-03-12 19:57:08 +00002285
aliguori221f7152009-03-28 17:28:41 +00002286BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
2287 unsigned long int req, void *buf,
2288 BlockDriverCompletionFunc *cb, void *opaque)
aliguori7d780662009-03-12 19:57:08 +00002289{
aliguori221f7152009-03-28 17:28:41 +00002290 BlockDriver *drv = bs->drv;
aliguori7d780662009-03-12 19:57:08 +00002291
aliguori221f7152009-03-28 17:28:41 +00002292 if (drv && drv->bdrv_aio_ioctl)
2293 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
2294 return NULL;
aliguori7d780662009-03-12 19:57:08 +00002295}
aliguorie268ca52009-04-22 20:20:00 +00002296
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002297
2298
aliguorie268ca52009-04-22 20:20:00 +00002299void *qemu_blockalign(BlockDriverState *bs, size_t size)
2300{
2301 return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
2302}
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002303
2304void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable)
2305{
2306 int64_t bitmap_size;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002307
Liran Schouraaa0eb72010-01-26 10:31:48 +02002308 bs->dirty_count = 0;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002309 if (enable) {
Jan Kiszkac6d22832009-11-30 18:21:20 +01002310 if (!bs->dirty_bitmap) {
2311 bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
2312 BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
2313 bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002314
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002315 bs->dirty_bitmap = qemu_mallocz(bitmap_size);
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002316 }
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002317 } else {
Jan Kiszkac6d22832009-11-30 18:21:20 +01002318 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002319 qemu_free(bs->dirty_bitmap);
Jan Kiszkac6d22832009-11-30 18:21:20 +01002320 bs->dirty_bitmap = NULL;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002321 }
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002322 }
2323}
2324
2325int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
2326{
Jan Kiszka6ea44302009-11-30 18:21:19 +01002327 int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002328
Jan Kiszkac6d22832009-11-30 18:21:20 +01002329 if (bs->dirty_bitmap &&
2330 (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) {
2331 return bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] &
2332 (1 << (chunk % (sizeof(unsigned long) * 8)));
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002333 } else {
2334 return 0;
2335 }
2336}
2337
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002338void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
2339 int nr_sectors)
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002340{
2341 set_dirty_bitmap(bs, cur_sector, nr_sectors, 0);
2342}
Liran Schouraaa0eb72010-01-26 10:31:48 +02002343
2344int64_t bdrv_get_dirty_count(BlockDriverState *bs)
2345{
2346 return bs->dirty_count;
2347}