blob: 53a10de9dcd2c95b5d65a138b90b639f96d003ba [file] [log] [blame]
bellardfc01f7e2003-06-30 10:03:06 +00001/*
2 * QEMU System Emulator block driver
ths5fafdf22007-09-16 21:08:06 +00003 *
bellardfc01f7e2003-06-30 10:03:06 +00004 * Copyright (c) 2003 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellardfc01f7e2003-06-30 10:03:06 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
blueswir13990d092008-12-05 17:53:21 +000024#include "config-host.h"
pbrookfaf07962007-11-11 02:51:17 +000025#include "qemu-common.h"
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +010026#include "trace.h"
aliguori376253e2009-03-05 23:01:23 +000027#include "monitor.h"
bellardea2384d2004-08-01 21:59:26 +000028#include "block_int.h"
Anthony Liguori5efa9d52009-05-09 17:03:42 -050029#include "module.h"
Luiz Capitulinod15e5462009-12-10 17:16:06 -020030#include "qemu-objects.h"
bellardfc01f7e2003-06-30 10:03:06 +000031
Juan Quintela71e72a12009-07-27 16:12:56 +020032#ifdef CONFIG_BSD
bellard7674e7b2005-04-26 21:59:26 +000033#include <sys/types.h>
34#include <sys/stat.h>
35#include <sys/ioctl.h>
Blue Swirl72cf2d42009-09-12 07:36:22 +000036#include <sys/queue.h>
blueswir1c5e97232009-03-07 20:06:23 +000037#ifndef __DragonFly__
bellard7674e7b2005-04-26 21:59:26 +000038#include <sys/disk.h>
39#endif
blueswir1c5e97232009-03-07 20:06:23 +000040#endif
bellard7674e7b2005-04-26 21:59:26 +000041
aliguori49dc7682009-03-08 16:26:59 +000042#ifdef _WIN32
43#include <windows.h>
44#endif
45
aliguorif141eaf2009-04-07 18:43:24 +000046static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
47 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
aliguoric87c0672009-04-07 18:43:20 +000048 BlockDriverCompletionFunc *cb, void *opaque);
aliguorif141eaf2009-04-07 18:43:24 +000049static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
50 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
pbrookce1a14d2006-08-07 02:38:06 +000051 BlockDriverCompletionFunc *cb, void *opaque);
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +020052static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
53 BlockDriverCompletionFunc *cb, void *opaque);
Alexander Graf016f5cf2010-05-26 17:51:49 +020054static BlockDriverAIOCB *bdrv_aio_noop_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);
bellardec530c82006-04-25 22:36:06 +000060
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +010061static QTAILQ_HEAD(, BlockDriverState) bdrv_states =
62 QTAILQ_HEAD_INITIALIZER(bdrv_states);
blueswir17ee930d2008-09-17 19:04:14 +000063
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +010064static QLIST_HEAD(, BlockDriver) bdrv_drivers =
65 QLIST_HEAD_INITIALIZER(bdrv_drivers);
bellardea2384d2004-08-01 21:59:26 +000066
Markus Armbrusterf9092b12010-06-25 10:33:39 +020067/* The device to use for VM snapshots */
68static BlockDriverState *bs_snapshots;
69
Markus Armbrustereb852012009-10-27 18:41:44 +010070/* If non-zero, use only whitelisted block drivers */
71static int use_bdrv_whitelist;
72
bellard83f64092006-08-01 16:21:11 +000073int path_is_absolute(const char *path)
74{
75 const char *p;
bellard21664422007-01-07 18:22:37 +000076#ifdef _WIN32
77 /* specific case for names like: "\\.\d:" */
78 if (*path == '/' || *path == '\\')
79 return 1;
80#endif
bellard83f64092006-08-01 16:21:11 +000081 p = strchr(path, ':');
82 if (p)
83 p++;
84 else
85 p = path;
bellard3b9f94e2007-01-07 17:27:07 +000086#ifdef _WIN32
87 return (*p == '/' || *p == '\\');
88#else
89 return (*p == '/');
90#endif
bellard83f64092006-08-01 16:21:11 +000091}
92
93/* if filename is absolute, just copy it to dest. Otherwise, build a
94 path to it by considering it is relative to base_path. URL are
95 supported. */
96void path_combine(char *dest, int dest_size,
97 const char *base_path,
98 const char *filename)
99{
100 const char *p, *p1;
101 int len;
102
103 if (dest_size <= 0)
104 return;
105 if (path_is_absolute(filename)) {
106 pstrcpy(dest, dest_size, filename);
107 } else {
108 p = strchr(base_path, ':');
109 if (p)
110 p++;
111 else
112 p = base_path;
bellard3b9f94e2007-01-07 17:27:07 +0000113 p1 = strrchr(base_path, '/');
114#ifdef _WIN32
115 {
116 const char *p2;
117 p2 = strrchr(base_path, '\\');
118 if (!p1 || p2 > p1)
119 p1 = p2;
120 }
121#endif
bellard83f64092006-08-01 16:21:11 +0000122 if (p1)
123 p1++;
124 else
125 p1 = base_path;
126 if (p1 > p)
127 p = p1;
128 len = p - base_path;
129 if (len > dest_size - 1)
130 len = dest_size - 1;
131 memcpy(dest, base_path, len);
132 dest[len] = '\0';
133 pstrcat(dest, dest_size, filename);
134 }
135}
136
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500137void bdrv_register(BlockDriver *bdrv)
bellardea2384d2004-08-01 21:59:26 +0000138{
aliguorif141eaf2009-04-07 18:43:24 +0000139 if (!bdrv->bdrv_aio_readv) {
bellard83f64092006-08-01 16:21:11 +0000140 /* add AIO emulation layer */
aliguorif141eaf2009-04-07 18:43:24 +0000141 bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
142 bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
aliguorieda578e2009-03-12 19:57:16 +0000143 } else if (!bdrv->bdrv_read) {
bellard83f64092006-08-01 16:21:11 +0000144 /* add synchronous IO emulation layer */
145 bdrv->bdrv_read = bdrv_read_em;
146 bdrv->bdrv_write = bdrv_write_em;
147 }
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +0200148
149 if (!bdrv->bdrv_aio_flush)
150 bdrv->bdrv_aio_flush = bdrv_aio_flush_em;
151
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100152 QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
bellardea2384d2004-08-01 21:59:26 +0000153}
bellardb3380822004-03-14 21:38:54 +0000154
155/* create a new block device (by default it is empty) */
156BlockDriverState *bdrv_new(const char *device_name)
bellardfc01f7e2003-06-30 10:03:06 +0000157{
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +0100158 BlockDriverState *bs;
bellardb3380822004-03-14 21:38:54 +0000159
160 bs = qemu_mallocz(sizeof(BlockDriverState));
bellardb3380822004-03-14 21:38:54 +0000161 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
bellardea2384d2004-08-01 21:59:26 +0000162 if (device_name[0] != '\0') {
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +0100163 QTAILQ_INSERT_TAIL(&bdrv_states, bs, list);
bellardea2384d2004-08-01 21:59:26 +0000164 }
bellardb3380822004-03-14 21:38:54 +0000165 return bs;
166}
167
bellardea2384d2004-08-01 21:59:26 +0000168BlockDriver *bdrv_find_format(const char *format_name)
169{
170 BlockDriver *drv1;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100171 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
172 if (!strcmp(drv1->format_name, format_name)) {
bellardea2384d2004-08-01 21:59:26 +0000173 return drv1;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100174 }
bellardea2384d2004-08-01 21:59:26 +0000175 }
176 return NULL;
177}
178
Markus Armbrustereb852012009-10-27 18:41:44 +0100179static int bdrv_is_whitelisted(BlockDriver *drv)
180{
181 static const char *whitelist[] = {
182 CONFIG_BDRV_WHITELIST
183 };
184 const char **p;
185
186 if (!whitelist[0])
187 return 1; /* no whitelist, anything goes */
188
189 for (p = whitelist; *p; p++) {
190 if (!strcmp(drv->format_name, *p)) {
191 return 1;
192 }
193 }
194 return 0;
195}
196
197BlockDriver *bdrv_find_whitelisted_format(const char *format_name)
198{
199 BlockDriver *drv = bdrv_find_format(format_name);
200 return drv && bdrv_is_whitelisted(drv) ? drv : NULL;
201}
202
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200203int bdrv_create(BlockDriver *drv, const char* filename,
204 QEMUOptionParameter *options)
bellardea2384d2004-08-01 21:59:26 +0000205{
206 if (!drv->bdrv_create)
207 return -ENOTSUP;
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200208
209 return drv->bdrv_create(filename, options);
bellardea2384d2004-08-01 21:59:26 +0000210}
211
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200212int bdrv_create_file(const char* filename, QEMUOptionParameter *options)
213{
214 BlockDriver *drv;
215
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900216 drv = bdrv_find_protocol(filename);
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200217 if (drv == NULL) {
218 drv = bdrv_find_format("file");
219 }
220
221 return bdrv_create(drv, filename, options);
222}
223
bellardd5249392004-08-03 21:14:23 +0000224#ifdef _WIN32
bellard95389c82005-12-18 18:28:15 +0000225void get_tmp_filename(char *filename, int size)
bellardd5249392004-08-03 21:14:23 +0000226{
bellard3b9f94e2007-01-07 17:27:07 +0000227 char temp_dir[MAX_PATH];
ths3b46e622007-09-17 08:09:54 +0000228
bellard3b9f94e2007-01-07 17:27:07 +0000229 GetTempPath(MAX_PATH, temp_dir);
230 GetTempFileName(temp_dir, "qem", 0, filename);
bellardd5249392004-08-03 21:14:23 +0000231}
232#else
bellard95389c82005-12-18 18:28:15 +0000233void get_tmp_filename(char *filename, int size)
bellardea2384d2004-08-01 21:59:26 +0000234{
235 int fd;
blueswir17ccfb2e2008-09-14 06:45:34 +0000236 const char *tmpdir;
bellardd5249392004-08-03 21:14:23 +0000237 /* XXX: race condition possible */
aurel320badc1e2008-03-10 00:05:34 +0000238 tmpdir = getenv("TMPDIR");
239 if (!tmpdir)
240 tmpdir = "/tmp";
241 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
bellardea2384d2004-08-01 21:59:26 +0000242 fd = mkstemp(filename);
243 close(fd);
244}
bellardd5249392004-08-03 21:14:23 +0000245#endif
bellardea2384d2004-08-01 21:59:26 +0000246
bellard19cb3732006-08-19 11:45:59 +0000247#ifdef _WIN32
bellardf45512f2006-08-23 21:40:13 +0000248static int is_windows_drive_prefix(const char *filename)
249{
250 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
251 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
252 filename[1] == ':');
253}
ths3b46e622007-09-17 08:09:54 +0000254
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200255int is_windows_drive(const char *filename)
bellard19cb3732006-08-19 11:45:59 +0000256{
ths5fafdf22007-09-16 21:08:06 +0000257 if (is_windows_drive_prefix(filename) &&
bellardf45512f2006-08-23 21:40:13 +0000258 filename[2] == '\0')
bellard19cb3732006-08-19 11:45:59 +0000259 return 1;
260 if (strstart(filename, "\\\\.\\", NULL) ||
261 strstart(filename, "//./", NULL))
262 return 1;
263 return 0;
264}
265#endif
266
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200267/*
268 * Detect host devices. By convention, /dev/cdrom[N] is always
269 * recognized as a host CDROM.
270 */
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200271static BlockDriver *find_hdev_driver(const char *filename)
272{
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200273 int score_max = 0, score;
274 BlockDriver *drv = NULL, *d;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200275
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100276 QLIST_FOREACH(d, &bdrv_drivers, list) {
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200277 if (d->bdrv_probe_device) {
278 score = d->bdrv_probe_device(filename);
279 if (score > score_max) {
280 score_max = score;
281 drv = d;
282 }
283 }
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200284 }
285
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200286 return drv;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200287}
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200288
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900289BlockDriver *bdrv_find_protocol(const char *filename)
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200290{
291 BlockDriver *drv1;
292 char protocol[128];
293 int len;
294 const char *p;
295
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200296 /* TODO Drivers without bdrv_file_open must be specified explicitly */
297
Christoph Hellwig39508e72010-06-23 12:25:17 +0200298 /*
299 * XXX(hch): we really should not let host device detection
300 * override an explicit protocol specification, but moving this
301 * later breaks access to device names with colons in them.
302 * Thanks to the brain-dead persistent naming schemes on udev-
303 * based Linux systems those actually are quite common.
304 */
305 drv1 = find_hdev_driver(filename);
306 if (drv1) {
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200307 return drv1;
308 }
Christoph Hellwig39508e72010-06-23 12:25:17 +0200309
310#ifdef _WIN32
311 if (is_windows_drive(filename) ||
312 is_windows_drive_prefix(filename))
313 return bdrv_find_format("file");
314#endif
315
316 p = strchr(filename, ':');
317 if (!p) {
318 return bdrv_find_format("file");
319 }
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200320 len = p - filename;
321 if (len > sizeof(protocol) - 1)
322 len = sizeof(protocol) - 1;
323 memcpy(protocol, filename, len);
324 protocol[len] = '\0';
325 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
326 if (drv1->protocol_name &&
327 !strcmp(drv1->protocol_name, protocol)) {
328 return drv1;
329 }
330 }
331 return NULL;
332}
333
Stefan Weilc98ac352010-07-21 21:51:51 +0200334static int find_image_format(const char *filename, BlockDriver **pdrv)
bellardea2384d2004-08-01 21:59:26 +0000335{
bellard83f64092006-08-01 16:21:11 +0000336 int ret, score, score_max;
bellardea2384d2004-08-01 21:59:26 +0000337 BlockDriver *drv1, *drv;
bellard83f64092006-08-01 16:21:11 +0000338 uint8_t buf[2048];
339 BlockDriverState *bs;
ths3b46e622007-09-17 08:09:54 +0000340
Naphtali Spreif5edb012010-01-17 16:48:13 +0200341 ret = bdrv_file_open(&bs, filename, 0);
Stefan Weilc98ac352010-07-21 21:51:51 +0200342 if (ret < 0) {
343 *pdrv = NULL;
344 return ret;
345 }
Nicholas Bellingerf8ea0b02010-05-17 09:45:57 -0700346
Kevin Wolf08a00552010-06-01 18:37:31 +0200347 /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
348 if (bs->sg || !bdrv_is_inserted(bs)) {
Nicholas A. Bellinger1a396852010-05-27 08:56:28 -0700349 bdrv_delete(bs);
Stefan Weilc98ac352010-07-21 21:51:51 +0200350 drv = bdrv_find_format("raw");
351 if (!drv) {
352 ret = -ENOENT;
353 }
354 *pdrv = drv;
355 return ret;
Nicholas A. Bellinger1a396852010-05-27 08:56:28 -0700356 }
Nicholas Bellingerf8ea0b02010-05-17 09:45:57 -0700357
bellard83f64092006-08-01 16:21:11 +0000358 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
359 bdrv_delete(bs);
360 if (ret < 0) {
Stefan Weilc98ac352010-07-21 21:51:51 +0200361 *pdrv = NULL;
362 return ret;
bellard83f64092006-08-01 16:21:11 +0000363 }
364
bellardea2384d2004-08-01 21:59:26 +0000365 score_max = 0;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200366 drv = NULL;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100367 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
bellard83f64092006-08-01 16:21:11 +0000368 if (drv1->bdrv_probe) {
369 score = drv1->bdrv_probe(buf, ret, filename);
370 if (score > score_max) {
371 score_max = score;
372 drv = drv1;
373 }
bellardea2384d2004-08-01 21:59:26 +0000374 }
375 }
Stefan Weilc98ac352010-07-21 21:51:51 +0200376 if (!drv) {
377 ret = -ENOENT;
378 }
379 *pdrv = drv;
380 return ret;
bellardea2384d2004-08-01 21:59:26 +0000381}
382
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100383/**
384 * Set the current 'total_sectors' value
385 */
386static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
387{
388 BlockDriver *drv = bs->drv;
389
Nicholas Bellinger396759a2010-05-17 09:46:04 -0700390 /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
391 if (bs->sg)
392 return 0;
393
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100394 /* query actual device if possible, otherwise just trust the hint */
395 if (drv->bdrv_getlength) {
396 int64_t length = drv->bdrv_getlength(bs);
397 if (length < 0) {
398 return length;
399 }
400 hint = length >> BDRV_SECTOR_BITS;
401 }
402
403 bs->total_sectors = hint;
404 return 0;
405}
406
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200407/*
Kevin Wolf57915332010-04-14 15:24:50 +0200408 * Common part for opening disk images and files
409 */
410static int bdrv_open_common(BlockDriverState *bs, const char *filename,
411 int flags, BlockDriver *drv)
412{
413 int ret, open_flags;
414
415 assert(drv != NULL);
416
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200417 bs->file = NULL;
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100418 bs->total_sectors = 0;
Kevin Wolf57915332010-04-14 15:24:50 +0200419 bs->encrypted = 0;
420 bs->valid_key = 0;
421 bs->open_flags = flags;
422 /* buffer_alignment defaulted to 512, drivers can change this value */
423 bs->buffer_alignment = 512;
424
425 pstrcpy(bs->filename, sizeof(bs->filename), filename);
426
427 if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {
428 return -ENOTSUP;
429 }
430
431 bs->drv = drv;
432 bs->opaque = qemu_mallocz(drv->instance_size);
433
434 /*
435 * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a
436 * write cache to the guest. We do need the fdatasync to flush
437 * out transactions for block allocations, and we maybe have a
438 * volatile write cache in our backing device to deal with.
439 */
440 if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE))
441 bs->enable_write_cache = 1;
442
443 /*
444 * Clear flags that are internal to the block layer before opening the
445 * image.
446 */
447 open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
448
449 /*
450 * Snapshots should be writeable.
451 */
452 if (bs->is_temporary) {
453 open_flags |= BDRV_O_RDWR;
454 }
455
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200456 /* Open the image, either directly or using a protocol */
457 if (drv->bdrv_file_open) {
458 ret = drv->bdrv_file_open(bs, filename, open_flags);
459 } else {
460 ret = bdrv_file_open(&bs->file, filename, open_flags);
461 if (ret >= 0) {
462 ret = drv->bdrv_open(bs, open_flags);
463 }
464 }
465
Kevin Wolf57915332010-04-14 15:24:50 +0200466 if (ret < 0) {
467 goto free_and_fail;
468 }
469
470 bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR);
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100471
472 ret = refresh_total_sectors(bs, bs->total_sectors);
473 if (ret < 0) {
474 goto free_and_fail;
Kevin Wolf57915332010-04-14 15:24:50 +0200475 }
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100476
Kevin Wolf57915332010-04-14 15:24:50 +0200477#ifndef _WIN32
478 if (bs->is_temporary) {
479 unlink(filename);
480 }
481#endif
482 return 0;
483
484free_and_fail:
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200485 if (bs->file) {
486 bdrv_delete(bs->file);
487 bs->file = NULL;
488 }
Kevin Wolf57915332010-04-14 15:24:50 +0200489 qemu_free(bs->opaque);
490 bs->opaque = NULL;
491 bs->drv = NULL;
492 return ret;
493}
494
495/*
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200496 * Opens a file using a protocol (file, host_device, nbd, ...)
497 */
bellard83f64092006-08-01 16:21:11 +0000498int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
bellardb3380822004-03-14 21:38:54 +0000499{
bellard83f64092006-08-01 16:21:11 +0000500 BlockDriverState *bs;
Christoph Hellwig6db95602010-04-05 16:53:57 +0200501 BlockDriver *drv;
bellard83f64092006-08-01 16:21:11 +0000502 int ret;
503
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900504 drv = bdrv_find_protocol(filename);
Christoph Hellwig6db95602010-04-05 16:53:57 +0200505 if (!drv) {
506 return -ENOENT;
507 }
508
bellard83f64092006-08-01 16:21:11 +0000509 bs = bdrv_new("");
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200510 ret = bdrv_open_common(bs, filename, flags, drv);
bellard83f64092006-08-01 16:21:11 +0000511 if (ret < 0) {
512 bdrv_delete(bs);
513 return ret;
bellard3b0d4f62005-10-30 18:30:10 +0000514 }
aliguori71d07702009-03-03 17:37:16 +0000515 bs->growable = 1;
bellard83f64092006-08-01 16:21:11 +0000516 *pbs = bs;
517 return 0;
bellardea2384d2004-08-01 21:59:26 +0000518}
bellardfc01f7e2003-06-30 10:03:06 +0000519
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200520/*
521 * Opens a disk image (raw, qcow2, vmdk, ...)
522 */
Kevin Wolfd6e90982010-03-31 14:40:27 +0200523int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
524 BlockDriver *drv)
bellardea2384d2004-08-01 21:59:26 +0000525{
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200526 int ret;
bellard712e7872005-04-28 21:09:32 +0000527
bellard83f64092006-08-01 16:21:11 +0000528 if (flags & BDRV_O_SNAPSHOT) {
bellardea2384d2004-08-01 21:59:26 +0000529 BlockDriverState *bs1;
530 int64_t total_size;
aliguori7c96d462008-09-12 17:54:13 +0000531 int is_protocol = 0;
Kevin Wolf91a073a2009-05-27 14:48:06 +0200532 BlockDriver *bdrv_qcow2;
533 QEMUOptionParameter *options;
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200534 char tmp_filename[PATH_MAX];
535 char backing_filename[PATH_MAX];
ths3b46e622007-09-17 08:09:54 +0000536
bellardea2384d2004-08-01 21:59:26 +0000537 /* if snapshot, we create a temporary backing file and open it
538 instead of opening 'filename' directly */
539
540 /* if there is a backing file, use it */
541 bs1 = bdrv_new("");
Kevin Wolfd6e90982010-03-31 14:40:27 +0200542 ret = bdrv_open(bs1, filename, 0, drv);
aliguori51d7c002009-03-05 23:00:29 +0000543 if (ret < 0) {
bellardea2384d2004-08-01 21:59:26 +0000544 bdrv_delete(bs1);
aliguori51d7c002009-03-05 23:00:29 +0000545 return ret;
bellardea2384d2004-08-01 21:59:26 +0000546 }
Jes Sorensen3e829902010-05-27 16:20:30 +0200547 total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK;
aliguori7c96d462008-09-12 17:54:13 +0000548
549 if (bs1->drv && bs1->drv->protocol_name)
550 is_protocol = 1;
551
bellardea2384d2004-08-01 21:59:26 +0000552 bdrv_delete(bs1);
ths3b46e622007-09-17 08:09:54 +0000553
bellardea2384d2004-08-01 21:59:26 +0000554 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
aliguori7c96d462008-09-12 17:54:13 +0000555
556 /* Real path is meaningless for protocols */
557 if (is_protocol)
558 snprintf(backing_filename, sizeof(backing_filename),
559 "%s", filename);
Kirill A. Shutemov114cdfa2009-12-25 18:19:22 +0000560 else if (!realpath(filename, backing_filename))
561 return -errno;
aliguori7c96d462008-09-12 17:54:13 +0000562
Kevin Wolf91a073a2009-05-27 14:48:06 +0200563 bdrv_qcow2 = bdrv_find_format("qcow2");
564 options = parse_option_parameters("", bdrv_qcow2->create_options, NULL);
565
Jes Sorensen3e829902010-05-27 16:20:30 +0200566 set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size);
Kevin Wolf91a073a2009-05-27 14:48:06 +0200567 set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename);
568 if (drv) {
569 set_option_parameter(options, BLOCK_OPT_BACKING_FMT,
570 drv->format_name);
571 }
572
573 ret = bdrv_create(bdrv_qcow2, tmp_filename, options);
Jan Kiszkad7487682010-04-29 18:24:50 +0200574 free_option_parameters(options);
aliguori51d7c002009-03-05 23:00:29 +0000575 if (ret < 0) {
576 return ret;
bellardea2384d2004-08-01 21:59:26 +0000577 }
Kevin Wolf91a073a2009-05-27 14:48:06 +0200578
bellardea2384d2004-08-01 21:59:26 +0000579 filename = tmp_filename;
Kevin Wolf91a073a2009-05-27 14:48:06 +0200580 drv = bdrv_qcow2;
bellardea2384d2004-08-01 21:59:26 +0000581 bs->is_temporary = 1;
582 }
bellard712e7872005-04-28 21:09:32 +0000583
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200584 /* Find the right image format driver */
Christoph Hellwig6db95602010-04-05 16:53:57 +0200585 if (!drv) {
Stefan Weilc98ac352010-07-21 21:51:51 +0200586 ret = find_image_format(filename, &drv);
aliguori51d7c002009-03-05 23:00:29 +0000587 }
Christoph Hellwig69873072010-01-20 18:13:25 +0100588
aliguori51d7c002009-03-05 23:00:29 +0000589 if (!drv) {
aliguori51d7c002009-03-05 23:00:29 +0000590 goto unlink_and_fail;
bellardea2384d2004-08-01 21:59:26 +0000591 }
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200592
593 /* Open the image */
594 ret = bdrv_open_common(bs, filename, flags, drv);
595 if (ret < 0) {
Christoph Hellwig69873072010-01-20 18:13:25 +0100596 goto unlink_and_fail;
597 }
598
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200599 /* If there is a backing file, use it */
600 if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') {
601 char backing_filename[PATH_MAX];
602 int back_flags;
603 BlockDriver *back_drv = NULL;
604
605 bs->backing_hd = bdrv_new("");
606 path_combine(backing_filename, sizeof(backing_filename),
607 filename, bs->backing_file);
608 if (bs->backing_format[0] != '\0')
609 back_drv = bdrv_find_format(bs->backing_format);
610
611 /* backing files always opened read-only */
612 back_flags =
613 flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
614
615 ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv);
616 if (ret < 0) {
617 bdrv_close(bs);
618 return ret;
619 }
620 if (bs->is_temporary) {
621 bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR);
622 } else {
623 /* base image inherits from "parent" */
624 bs->backing_hd->keep_read_only = bs->keep_read_only;
625 }
626 }
627
628 if (!bdrv_key_required(bs)) {
629 /* call the change callback */
630 bs->media_changed = 1;
631 if (bs->change_cb)
632 bs->change_cb(bs->change_opaque);
633 }
634
635 return 0;
636
637unlink_and_fail:
638 if (bs->is_temporary) {
639 unlink(filename);
640 }
641 return ret;
642}
643
bellardfc01f7e2003-06-30 10:03:06 +0000644void bdrv_close(BlockDriverState *bs)
645{
bellard19cb3732006-08-19 11:45:59 +0000646 if (bs->drv) {
Markus Armbrusterf9092b12010-06-25 10:33:39 +0200647 if (bs == bs_snapshots) {
648 bs_snapshots = NULL;
649 }
Stefan Hajnoczi557df6a2010-04-17 10:49:06 +0100650 if (bs->backing_hd) {
bellardea2384d2004-08-01 21:59:26 +0000651 bdrv_delete(bs->backing_hd);
Stefan Hajnoczi557df6a2010-04-17 10:49:06 +0100652 bs->backing_hd = NULL;
653 }
bellardea2384d2004-08-01 21:59:26 +0000654 bs->drv->bdrv_close(bs);
655 qemu_free(bs->opaque);
656#ifdef _WIN32
657 if (bs->is_temporary) {
658 unlink(bs->filename);
659 }
bellard67b915a2004-03-31 23:37:16 +0000660#endif
bellardea2384d2004-08-01 21:59:26 +0000661 bs->opaque = NULL;
662 bs->drv = NULL;
bellardb3380822004-03-14 21:38:54 +0000663
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200664 if (bs->file != NULL) {
665 bdrv_close(bs->file);
666 }
667
bellardb3380822004-03-14 21:38:54 +0000668 /* call the change callback */
bellard19cb3732006-08-19 11:45:59 +0000669 bs->media_changed = 1;
bellardb3380822004-03-14 21:38:54 +0000670 if (bs->change_cb)
671 bs->change_cb(bs->change_opaque);
672 }
673}
674
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +0900675void bdrv_close_all(void)
676{
677 BlockDriverState *bs;
678
679 QTAILQ_FOREACH(bs, &bdrv_states, list) {
680 bdrv_close(bs);
681 }
682}
683
bellardb3380822004-03-14 21:38:54 +0000684void bdrv_delete(BlockDriverState *bs)
685{
Markus Armbruster18846de2010-06-29 16:58:30 +0200686 assert(!bs->peer);
687
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +0100688 /* remove from list, if necessary */
689 if (bs->device_name[0] != '\0') {
690 QTAILQ_REMOVE(&bdrv_states, bs, list);
691 }
aurel3234c6f052008-04-08 19:51:21 +0000692
bellardb3380822004-03-14 21:38:54 +0000693 bdrv_close(bs);
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200694 if (bs->file != NULL) {
695 bdrv_delete(bs->file);
696 }
697
Markus Armbrusterf9092b12010-06-25 10:33:39 +0200698 assert(bs != bs_snapshots);
bellardb3380822004-03-14 21:38:54 +0000699 qemu_free(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000700}
701
Markus Armbruster18846de2010-06-29 16:58:30 +0200702int bdrv_attach(BlockDriverState *bs, DeviceState *qdev)
703{
704 if (bs->peer) {
705 return -EBUSY;
706 }
707 bs->peer = qdev;
708 return 0;
709}
710
711void bdrv_detach(BlockDriverState *bs, DeviceState *qdev)
712{
713 assert(bs->peer == qdev);
714 bs->peer = NULL;
715}
716
717DeviceState *bdrv_get_attached(BlockDriverState *bs)
718{
719 return bs->peer;
720}
721
aliguorie97fc192009-04-21 23:11:50 +0000722/*
723 * Run consistency checks on an image
724 *
Kevin Wolfe076f332010-06-29 11:43:13 +0200725 * Returns 0 if the check could be completed (it doesn't mean that the image is
726 * free of errors) or -errno when an internal error occured. The results of the
727 * check are stored in res.
aliguorie97fc192009-04-21 23:11:50 +0000728 */
Kevin Wolfe076f332010-06-29 11:43:13 +0200729int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res)
aliguorie97fc192009-04-21 23:11:50 +0000730{
731 if (bs->drv->bdrv_check == NULL) {
732 return -ENOTSUP;
733 }
734
Kevin Wolfe076f332010-06-29 11:43:13 +0200735 memset(res, 0, sizeof(*res));
Kevin Wolf9ac228e2010-06-29 12:37:54 +0200736 return bs->drv->bdrv_check(bs, res);
aliguorie97fc192009-04-21 23:11:50 +0000737}
738
Kevin Wolf8a426612010-07-16 17:17:01 +0200739#define COMMIT_BUF_SECTORS 2048
740
bellard33e39632003-07-06 17:15:21 +0000741/* commit COW file into the raw image */
742int bdrv_commit(BlockDriverState *bs)
743{
bellard19cb3732006-08-19 11:45:59 +0000744 BlockDriver *drv = bs->drv;
Kevin Wolfee181192010-08-05 13:05:22 +0200745 BlockDriver *backing_drv;
Kevin Wolf8a426612010-07-16 17:17:01 +0200746 int64_t sector, total_sectors;
747 int n, ro, open_flags;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200748 int ret = 0, rw_ret = 0;
Kevin Wolf8a426612010-07-16 17:17:01 +0200749 uint8_t *buf;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200750 char filename[1024];
751 BlockDriverState *bs_rw, *bs_ro;
bellard33e39632003-07-06 17:15:21 +0000752
bellard19cb3732006-08-19 11:45:59 +0000753 if (!drv)
754 return -ENOMEDIUM;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200755
756 if (!bs->backing_hd) {
757 return -ENOTSUP;
bellard33e39632003-07-06 17:15:21 +0000758 }
759
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200760 if (bs->backing_hd->keep_read_only) {
761 return -EACCES;
762 }
Kevin Wolfee181192010-08-05 13:05:22 +0200763
764 backing_drv = bs->backing_hd->drv;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200765 ro = bs->backing_hd->read_only;
766 strncpy(filename, bs->backing_hd->filename, sizeof(filename));
767 open_flags = bs->backing_hd->open_flags;
768
769 if (ro) {
770 /* re-open as RW */
771 bdrv_delete(bs->backing_hd);
772 bs->backing_hd = NULL;
773 bs_rw = bdrv_new("");
Kevin Wolfee181192010-08-05 13:05:22 +0200774 rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR,
775 backing_drv);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200776 if (rw_ret < 0) {
777 bdrv_delete(bs_rw);
778 /* try to re-open read-only */
779 bs_ro = bdrv_new("");
Kevin Wolfee181192010-08-05 13:05:22 +0200780 ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR,
781 backing_drv);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200782 if (ret < 0) {
783 bdrv_delete(bs_ro);
784 /* drive not functional anymore */
785 bs->drv = NULL;
786 return ret;
787 }
788 bs->backing_hd = bs_ro;
789 return rw_ret;
790 }
791 bs->backing_hd = bs_rw;
bellard33e39632003-07-06 17:15:21 +0000792 }
bellardea2384d2004-08-01 21:59:26 +0000793
Jan Kiszka6ea44302009-11-30 18:21:19 +0100794 total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
Kevin Wolf8a426612010-07-16 17:17:01 +0200795 buf = qemu_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE);
bellardea2384d2004-08-01 21:59:26 +0000796
Kevin Wolf8a426612010-07-16 17:17:01 +0200797 for (sector = 0; sector < total_sectors; sector += n) {
798 if (drv->bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) {
799
800 if (bdrv_read(bs, sector, buf, n) != 0) {
801 ret = -EIO;
802 goto ro_cleanup;
803 }
804
805 if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) {
806 ret = -EIO;
807 goto ro_cleanup;
808 }
bellardea2384d2004-08-01 21:59:26 +0000809 }
810 }
bellard95389c82005-12-18 18:28:15 +0000811
Christoph Hellwig1d449522010-01-17 12:32:30 +0100812 if (drv->bdrv_make_empty) {
813 ret = drv->bdrv_make_empty(bs);
814 bdrv_flush(bs);
815 }
bellard95389c82005-12-18 18:28:15 +0000816
Christoph Hellwig3f5075a2010-01-12 13:49:23 +0100817 /*
818 * Make sure all data we wrote to the backing device is actually
819 * stable on disk.
820 */
821 if (bs->backing_hd)
822 bdrv_flush(bs->backing_hd);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200823
824ro_cleanup:
Kevin Wolf8a426612010-07-16 17:17:01 +0200825 qemu_free(buf);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200826
827 if (ro) {
828 /* re-open as RO */
829 bdrv_delete(bs->backing_hd);
830 bs->backing_hd = NULL;
831 bs_ro = bdrv_new("");
Kevin Wolfee181192010-08-05 13:05:22 +0200832 ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR,
833 backing_drv);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +0200834 if (ret < 0) {
835 bdrv_delete(bs_ro);
836 /* drive not functional anymore */
837 bs->drv = NULL;
838 return ret;
839 }
840 bs->backing_hd = bs_ro;
841 bs->backing_hd->keep_read_only = 0;
842 }
843
Christoph Hellwig1d449522010-01-17 12:32:30 +0100844 return ret;
bellard33e39632003-07-06 17:15:21 +0000845}
846
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200847void bdrv_commit_all(void)
848{
849 BlockDriverState *bs;
850
851 QTAILQ_FOREACH(bs, &bdrv_states, list) {
852 bdrv_commit(bs);
853 }
854}
855
Kevin Wolf756e6732010-01-12 12:55:17 +0100856/*
857 * Return values:
858 * 0 - success
859 * -EINVAL - backing format specified, but no file
860 * -ENOSPC - can't update the backing file because no space is left in the
861 * image file header
862 * -ENOTSUP - format driver doesn't support changing the backing file
863 */
864int bdrv_change_backing_file(BlockDriverState *bs,
865 const char *backing_file, const char *backing_fmt)
866{
867 BlockDriver *drv = bs->drv;
868
869 if (drv->bdrv_change_backing_file != NULL) {
870 return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
871 } else {
872 return -ENOTSUP;
873 }
874}
875
aliguori71d07702009-03-03 17:37:16 +0000876static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
877 size_t size)
878{
879 int64_t len;
880
881 if (!bdrv_is_inserted(bs))
882 return -ENOMEDIUM;
883
884 if (bs->growable)
885 return 0;
886
887 len = bdrv_getlength(bs);
888
Kevin Wolffbb7b4e2009-05-08 14:47:24 +0200889 if (offset < 0)
890 return -EIO;
891
892 if ((offset > len) || (len - offset < size))
aliguori71d07702009-03-03 17:37:16 +0000893 return -EIO;
894
895 return 0;
896}
897
898static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
899 int nb_sectors)
900{
Jes Sorenseneb5a3162010-05-27 16:20:31 +0200901 return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE,
902 nb_sectors * BDRV_SECTOR_SIZE);
aliguori71d07702009-03-03 17:37:16 +0000903}
904
bellard19cb3732006-08-19 11:45:59 +0000905/* return < 0 if error. See bdrv_write() for the return codes */
ths5fafdf22007-09-16 21:08:06 +0000906int bdrv_read(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000907 uint8_t *buf, int nb_sectors)
908{
bellardea2384d2004-08-01 21:59:26 +0000909 BlockDriver *drv = bs->drv;
910
bellard19cb3732006-08-19 11:45:59 +0000911 if (!drv)
912 return -ENOMEDIUM;
aliguori71d07702009-03-03 17:37:16 +0000913 if (bdrv_check_request(bs, sector_num, nb_sectors))
914 return -EIO;
bellardb3380822004-03-14 21:38:54 +0000915
aliguorieda578e2009-03-12 19:57:16 +0000916 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
bellardfc01f7e2003-06-30 10:03:06 +0000917}
918
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +0200919static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num,
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100920 int nb_sectors, int dirty)
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +0200921{
922 int64_t start, end;
Jan Kiszkac6d22832009-11-30 18:21:20 +0100923 unsigned long val, idx, bit;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100924
Jan Kiszka6ea44302009-11-30 18:21:19 +0100925 start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkac6d22832009-11-30 18:21:20 +0100926 end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100927
928 for (; start <= end; start++) {
Jan Kiszkac6d22832009-11-30 18:21:20 +0100929 idx = start / (sizeof(unsigned long) * 8);
930 bit = start % (sizeof(unsigned long) * 8);
931 val = bs->dirty_bitmap[idx];
932 if (dirty) {
Marcelo Tosatti6d59fec2010-11-08 17:02:54 -0200933 if (!(val & (1UL << bit))) {
Liran Schouraaa0eb72010-01-26 10:31:48 +0200934 bs->dirty_count++;
Marcelo Tosatti6d59fec2010-11-08 17:02:54 -0200935 val |= 1UL << bit;
Liran Schouraaa0eb72010-01-26 10:31:48 +0200936 }
Jan Kiszkac6d22832009-11-30 18:21:20 +0100937 } else {
Marcelo Tosatti6d59fec2010-11-08 17:02:54 -0200938 if (val & (1UL << bit)) {
Liran Schouraaa0eb72010-01-26 10:31:48 +0200939 bs->dirty_count--;
Marcelo Tosatti6d59fec2010-11-08 17:02:54 -0200940 val &= ~(1UL << bit);
Liran Schouraaa0eb72010-01-26 10:31:48 +0200941 }
Jan Kiszkac6d22832009-11-30 18:21:20 +0100942 }
943 bs->dirty_bitmap[idx] = val;
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +0200944 }
945}
946
ths5fafdf22007-09-16 21:08:06 +0000947/* Return < 0 if error. Important errors are:
bellard19cb3732006-08-19 11:45:59 +0000948 -EIO generic I/O error (may happen for all errors)
949 -ENOMEDIUM No media inserted.
950 -EINVAL Invalid sector number or nb_sectors
951 -EACCES Trying to write a read-only device
952*/
ths5fafdf22007-09-16 21:08:06 +0000953int bdrv_write(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000954 const uint8_t *buf, int nb_sectors)
955{
bellard83f64092006-08-01 16:21:11 +0000956 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +0000957 if (!bs->drv)
958 return -ENOMEDIUM;
bellard0849bf02003-06-30 23:17:31 +0000959 if (bs->read_only)
bellard19cb3732006-08-19 11:45:59 +0000960 return -EACCES;
aliguori71d07702009-03-03 17:37:16 +0000961 if (bdrv_check_request(bs, sector_num, nb_sectors))
962 return -EIO;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100963
Jan Kiszkac6d22832009-11-30 18:21:20 +0100964 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +0200965 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
966 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100967
Kevin Wolf294cc352010-04-28 14:34:01 +0200968 if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
969 bs->wr_highest_sector = sector_num + nb_sectors - 1;
970 }
971
aliguori42fb2802009-01-15 20:43:39 +0000972 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
bellard83f64092006-08-01 16:21:11 +0000973}
974
aliguorieda578e2009-03-12 19:57:16 +0000975int bdrv_pread(BlockDriverState *bs, int64_t offset,
976 void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000977{
Jan Kiszka6ea44302009-11-30 18:21:19 +0100978 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
bellard83f64092006-08-01 16:21:11 +0000979 int len, nb_sectors, count;
980 int64_t sector_num;
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100981 int ret;
bellard83f64092006-08-01 16:21:11 +0000982
983 count = count1;
984 /* first read to align to sector start */
Jan Kiszka6ea44302009-11-30 18:21:19 +0100985 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
bellard83f64092006-08-01 16:21:11 +0000986 if (len > count)
987 len = count;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100988 sector_num = offset >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000989 if (len > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +0100990 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
991 return ret;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100992 memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
bellard83f64092006-08-01 16:21:11 +0000993 count -= len;
994 if (count == 0)
995 return count1;
996 sector_num++;
997 buf += len;
998 }
999
1000 /* read the sectors "in place" */
Jan Kiszka6ea44302009-11-30 18:21:19 +01001001 nb_sectors = count >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001002 if (nb_sectors > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001003 if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0)
1004 return ret;
bellard83f64092006-08-01 16:21:11 +00001005 sector_num += nb_sectors;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001006 len = nb_sectors << BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001007 buf += len;
1008 count -= len;
1009 }
1010
1011 /* add data from the last sector */
1012 if (count > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001013 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1014 return ret;
bellard83f64092006-08-01 16:21:11 +00001015 memcpy(buf, tmp_buf, count);
1016 }
1017 return count1;
1018}
1019
aliguorieda578e2009-03-12 19:57:16 +00001020int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
1021 const void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +00001022{
Jan Kiszka6ea44302009-11-30 18:21:19 +01001023 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
bellard83f64092006-08-01 16:21:11 +00001024 int len, nb_sectors, count;
1025 int64_t sector_num;
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001026 int ret;
bellard83f64092006-08-01 16:21:11 +00001027
1028 count = count1;
1029 /* first write to align to sector start */
Jan Kiszka6ea44302009-11-30 18:21:19 +01001030 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
bellard83f64092006-08-01 16:21:11 +00001031 if (len > count)
1032 len = count;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001033 sector_num = offset >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001034 if (len > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001035 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1036 return ret;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001037 memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len);
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001038 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
1039 return ret;
bellard83f64092006-08-01 16:21:11 +00001040 count -= len;
1041 if (count == 0)
1042 return count1;
1043 sector_num++;
1044 buf += len;
1045 }
1046
1047 /* write the sectors "in place" */
Jan Kiszka6ea44302009-11-30 18:21:19 +01001048 nb_sectors = count >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001049 if (nb_sectors > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001050 if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0)
1051 return ret;
bellard83f64092006-08-01 16:21:11 +00001052 sector_num += nb_sectors;
Jan Kiszka6ea44302009-11-30 18:21:19 +01001053 len = nb_sectors << BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +00001054 buf += len;
1055 count -= len;
1056 }
1057
1058 /* add data from the last sector */
1059 if (count > 0) {
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001060 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1061 return ret;
bellard83f64092006-08-01 16:21:11 +00001062 memcpy(tmp_buf, buf, count);
Kevin Wolf9a8c4cc2010-01-20 15:03:02 +01001063 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
1064 return ret;
bellard83f64092006-08-01 16:21:11 +00001065 }
1066 return count1;
1067}
bellard83f64092006-08-01 16:21:11 +00001068
Kevin Wolff08145f2010-06-16 16:38:15 +02001069/*
1070 * Writes to the file and ensures that no writes are reordered across this
1071 * request (acts as a barrier)
1072 *
1073 * Returns 0 on success, -errno in error cases.
1074 */
1075int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset,
1076 const void *buf, int count)
1077{
1078 int ret;
1079
1080 ret = bdrv_pwrite(bs, offset, buf, count);
1081 if (ret < 0) {
1082 return ret;
1083 }
1084
1085 /* No flush needed for cache=writethrough, it uses O_DSYNC */
1086 if ((bs->open_flags & BDRV_O_CACHE_MASK) != 0) {
1087 bdrv_flush(bs);
1088 }
1089
1090 return 0;
1091}
1092
1093/*
1094 * Writes to the file and ensures that no writes are reordered across this
1095 * request (acts as a barrier)
1096 *
1097 * Returns 0 on success, -errno in error cases.
1098 */
1099int bdrv_write_sync(BlockDriverState *bs, int64_t sector_num,
1100 const uint8_t *buf, int nb_sectors)
1101{
1102 return bdrv_pwrite_sync(bs, BDRV_SECTOR_SIZE * sector_num,
1103 buf, BDRV_SECTOR_SIZE * nb_sectors);
1104}
1105
bellard83f64092006-08-01 16:21:11 +00001106/**
bellard83f64092006-08-01 16:21:11 +00001107 * Truncate file to 'offset' bytes (needed only for file protocols)
1108 */
1109int bdrv_truncate(BlockDriverState *bs, int64_t offset)
1110{
1111 BlockDriver *drv = bs->drv;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001112 int ret;
bellard83f64092006-08-01 16:21:11 +00001113 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001114 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +00001115 if (!drv->bdrv_truncate)
1116 return -ENOTSUP;
Naphtali Sprei59f26892009-10-26 16:25:16 +02001117 if (bs->read_only)
1118 return -EACCES;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001119 ret = drv->bdrv_truncate(bs, offset);
1120 if (ret == 0) {
1121 ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
1122 }
1123 return ret;
bellard83f64092006-08-01 16:21:11 +00001124}
1125
1126/**
1127 * Length of a file in bytes. Return < 0 if error or unknown.
1128 */
1129int64_t bdrv_getlength(BlockDriverState *bs)
1130{
1131 BlockDriver *drv = bs->drv;
1132 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001133 return -ENOMEDIUM;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001134
1135 /* Fixed size devices use the total_sectors value for speed instead of
1136 issuing a length query (like lseek) on each call. Also, legacy block
1137 drivers don't provide a bdrv_getlength function and must use
1138 total_sectors. */
1139 if (!bs->growable || !drv->bdrv_getlength) {
Jan Kiszka6ea44302009-11-30 18:21:19 +01001140 return bs->total_sectors * BDRV_SECTOR_SIZE;
bellard83f64092006-08-01 16:21:11 +00001141 }
1142 return drv->bdrv_getlength(bs);
bellardfc01f7e2003-06-30 10:03:06 +00001143}
1144
bellard19cb3732006-08-19 11:45:59 +00001145/* return 0 as number of sectors if no device present or error */
ths96b8f132007-12-17 01:35:20 +00001146void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
bellardfc01f7e2003-06-30 10:03:06 +00001147{
bellard19cb3732006-08-19 11:45:59 +00001148 int64_t length;
1149 length = bdrv_getlength(bs);
1150 if (length < 0)
1151 length = 0;
1152 else
Jan Kiszka6ea44302009-11-30 18:21:19 +01001153 length = length >> BDRV_SECTOR_BITS;
bellard19cb3732006-08-19 11:45:59 +00001154 *nb_sectors_ptr = length;
bellardfc01f7e2003-06-30 10:03:06 +00001155}
bellardcf989512004-02-16 21:56:36 +00001156
aliguorif3d54fc2008-11-25 21:50:24 +00001157struct partition {
1158 uint8_t boot_ind; /* 0x80 - active */
1159 uint8_t head; /* starting head */
1160 uint8_t sector; /* starting sector */
1161 uint8_t cyl; /* starting cylinder */
1162 uint8_t sys_ind; /* What partition type */
1163 uint8_t end_head; /* end head */
1164 uint8_t end_sector; /* end sector */
1165 uint8_t end_cyl; /* end cylinder */
1166 uint32_t start_sect; /* starting sector counting from 0 */
1167 uint32_t nr_sects; /* nr of sectors in partition */
1168} __attribute__((packed));
1169
1170/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
1171static int guess_disk_lchs(BlockDriverState *bs,
1172 int *pcylinders, int *pheads, int *psectors)
1173{
Jes Sorenseneb5a3162010-05-27 16:20:31 +02001174 uint8_t buf[BDRV_SECTOR_SIZE];
aliguorif3d54fc2008-11-25 21:50:24 +00001175 int ret, i, heads, sectors, cylinders;
1176 struct partition *p;
1177 uint32_t nr_sects;
blueswir1a38131b2008-12-05 17:56:40 +00001178 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +00001179
1180 bdrv_get_geometry(bs, &nb_sectors);
1181
1182 ret = bdrv_read(bs, 0, buf, 1);
1183 if (ret < 0)
1184 return -1;
1185 /* test msdos magic */
1186 if (buf[510] != 0x55 || buf[511] != 0xaa)
1187 return -1;
1188 for(i = 0; i < 4; i++) {
1189 p = ((struct partition *)(buf + 0x1be)) + i;
1190 nr_sects = le32_to_cpu(p->nr_sects);
1191 if (nr_sects && p->end_head) {
1192 /* We make the assumption that the partition terminates on
1193 a cylinder boundary */
1194 heads = p->end_head + 1;
1195 sectors = p->end_sector & 63;
1196 if (sectors == 0)
1197 continue;
1198 cylinders = nb_sectors / (heads * sectors);
1199 if (cylinders < 1 || cylinders > 16383)
1200 continue;
1201 *pheads = heads;
1202 *psectors = sectors;
1203 *pcylinders = cylinders;
1204#if 0
1205 printf("guessed geometry: LCHS=%d %d %d\n",
1206 cylinders, heads, sectors);
1207#endif
1208 return 0;
1209 }
1210 }
1211 return -1;
1212}
1213
1214void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
1215{
1216 int translation, lba_detected = 0;
1217 int cylinders, heads, secs;
blueswir1a38131b2008-12-05 17:56:40 +00001218 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +00001219
1220 /* if a geometry hint is available, use it */
1221 bdrv_get_geometry(bs, &nb_sectors);
1222 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
1223 translation = bdrv_get_translation_hint(bs);
1224 if (cylinders != 0) {
1225 *pcyls = cylinders;
1226 *pheads = heads;
1227 *psecs = secs;
1228 } else {
1229 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
1230 if (heads > 16) {
1231 /* if heads > 16, it means that a BIOS LBA
1232 translation was active, so the default
1233 hardware geometry is OK */
1234 lba_detected = 1;
1235 goto default_geometry;
1236 } else {
1237 *pcyls = cylinders;
1238 *pheads = heads;
1239 *psecs = secs;
1240 /* disable any translation to be in sync with
1241 the logical geometry */
1242 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
1243 bdrv_set_translation_hint(bs,
1244 BIOS_ATA_TRANSLATION_NONE);
1245 }
1246 }
1247 } else {
1248 default_geometry:
1249 /* if no geometry, use a standard physical disk geometry */
1250 cylinders = nb_sectors / (16 * 63);
1251
1252 if (cylinders > 16383)
1253 cylinders = 16383;
1254 else if (cylinders < 2)
1255 cylinders = 2;
1256 *pcyls = cylinders;
1257 *pheads = 16;
1258 *psecs = 63;
1259 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
1260 if ((*pcyls * *pheads) <= 131072) {
1261 bdrv_set_translation_hint(bs,
1262 BIOS_ATA_TRANSLATION_LARGE);
1263 } else {
1264 bdrv_set_translation_hint(bs,
1265 BIOS_ATA_TRANSLATION_LBA);
1266 }
1267 }
1268 }
1269 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
1270 }
1271}
1272
ths5fafdf22007-09-16 21:08:06 +00001273void bdrv_set_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +00001274 int cyls, int heads, int secs)
1275{
1276 bs->cyls = cyls;
1277 bs->heads = heads;
1278 bs->secs = secs;
1279}
1280
1281void bdrv_set_type_hint(BlockDriverState *bs, int type)
1282{
1283 bs->type = type;
1284 bs->removable = ((type == BDRV_TYPE_CDROM ||
1285 type == BDRV_TYPE_FLOPPY));
1286}
1287
bellard46d47672004-11-16 01:45:27 +00001288void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
1289{
1290 bs->translation = translation;
1291}
1292
ths5fafdf22007-09-16 21:08:06 +00001293void bdrv_get_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +00001294 int *pcyls, int *pheads, int *psecs)
1295{
1296 *pcyls = bs->cyls;
1297 *pheads = bs->heads;
1298 *psecs = bs->secs;
1299}
1300
1301int bdrv_get_type_hint(BlockDriverState *bs)
1302{
1303 return bs->type;
1304}
1305
bellard46d47672004-11-16 01:45:27 +00001306int bdrv_get_translation_hint(BlockDriverState *bs)
1307{
1308 return bs->translation;
1309}
1310
Markus Armbrusterabd7f682010-06-02 18:55:17 +02001311void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error,
1312 BlockErrorAction on_write_error)
1313{
1314 bs->on_read_error = on_read_error;
1315 bs->on_write_error = on_write_error;
1316}
1317
1318BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read)
1319{
1320 return is_read ? bs->on_read_error : bs->on_write_error;
1321}
1322
Markus Armbruster7d0d6952010-06-25 13:42:14 +02001323void bdrv_set_removable(BlockDriverState *bs, int removable)
1324{
1325 bs->removable = removable;
1326 if (removable && bs == bs_snapshots) {
1327 bs_snapshots = NULL;
1328 }
1329}
1330
bellardb3380822004-03-14 21:38:54 +00001331int bdrv_is_removable(BlockDriverState *bs)
1332{
1333 return bs->removable;
1334}
1335
1336int bdrv_is_read_only(BlockDriverState *bs)
1337{
1338 return bs->read_only;
1339}
1340
ths985a03b2007-12-24 16:10:43 +00001341int bdrv_is_sg(BlockDriverState *bs)
1342{
1343 return bs->sg;
1344}
1345
Christoph Hellwige900a7b2009-09-04 19:01:15 +02001346int bdrv_enable_write_cache(BlockDriverState *bs)
1347{
1348 return bs->enable_write_cache;
1349}
1350
bellard19cb3732006-08-19 11:45:59 +00001351/* XXX: no longer used */
ths5fafdf22007-09-16 21:08:06 +00001352void bdrv_set_change_cb(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +00001353 void (*change_cb)(void *opaque), void *opaque)
1354{
1355 bs->change_cb = change_cb;
1356 bs->change_opaque = opaque;
1357}
1358
bellardea2384d2004-08-01 21:59:26 +00001359int bdrv_is_encrypted(BlockDriverState *bs)
1360{
1361 if (bs->backing_hd && bs->backing_hd->encrypted)
1362 return 1;
1363 return bs->encrypted;
1364}
1365
aliguoric0f4ce72009-03-05 23:01:01 +00001366int bdrv_key_required(BlockDriverState *bs)
1367{
1368 BlockDriverState *backing_hd = bs->backing_hd;
1369
1370 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
1371 return 1;
1372 return (bs->encrypted && !bs->valid_key);
1373}
1374
bellardea2384d2004-08-01 21:59:26 +00001375int bdrv_set_key(BlockDriverState *bs, const char *key)
1376{
1377 int ret;
1378 if (bs->backing_hd && bs->backing_hd->encrypted) {
1379 ret = bdrv_set_key(bs->backing_hd, key);
1380 if (ret < 0)
1381 return ret;
1382 if (!bs->encrypted)
1383 return 0;
1384 }
Shahar Havivifd04a2a2010-03-06 00:26:13 +02001385 if (!bs->encrypted) {
1386 return -EINVAL;
1387 } else if (!bs->drv || !bs->drv->bdrv_set_key) {
1388 return -ENOMEDIUM;
1389 }
aliguoric0f4ce72009-03-05 23:01:01 +00001390 ret = bs->drv->bdrv_set_key(bs, key);
aliguoribb5fc202009-03-05 23:01:15 +00001391 if (ret < 0) {
1392 bs->valid_key = 0;
1393 } else if (!bs->valid_key) {
1394 bs->valid_key = 1;
1395 /* call the change callback now, we skipped it on open */
1396 bs->media_changed = 1;
1397 if (bs->change_cb)
1398 bs->change_cb(bs->change_opaque);
1399 }
aliguoric0f4ce72009-03-05 23:01:01 +00001400 return ret;
bellardea2384d2004-08-01 21:59:26 +00001401}
1402
1403void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
1404{
bellard19cb3732006-08-19 11:45:59 +00001405 if (!bs->drv) {
bellardea2384d2004-08-01 21:59:26 +00001406 buf[0] = '\0';
1407 } else {
1408 pstrcpy(buf, buf_size, bs->drv->format_name);
1409 }
1410}
1411
ths5fafdf22007-09-16 21:08:06 +00001412void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
bellardea2384d2004-08-01 21:59:26 +00001413 void *opaque)
1414{
1415 BlockDriver *drv;
1416
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +01001417 QLIST_FOREACH(drv, &bdrv_drivers, list) {
bellardea2384d2004-08-01 21:59:26 +00001418 it(opaque, drv->format_name);
1419 }
1420}
1421
bellardb3380822004-03-14 21:38:54 +00001422BlockDriverState *bdrv_find(const char *name)
1423{
1424 BlockDriverState *bs;
1425
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001426 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1427 if (!strcmp(name, bs->device_name)) {
bellardb3380822004-03-14 21:38:54 +00001428 return bs;
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001429 }
bellardb3380822004-03-14 21:38:54 +00001430 }
1431 return NULL;
1432}
1433
Markus Armbruster2f399b02010-06-02 18:55:20 +02001434BlockDriverState *bdrv_next(BlockDriverState *bs)
1435{
1436 if (!bs) {
1437 return QTAILQ_FIRST(&bdrv_states);
1438 }
1439 return QTAILQ_NEXT(bs, list);
1440}
1441
aliguori51de9762009-03-05 23:00:43 +00001442void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
bellard81d09122004-07-14 17:21:37 +00001443{
1444 BlockDriverState *bs;
1445
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001446 QTAILQ_FOREACH(bs, &bdrv_states, list) {
aliguori51de9762009-03-05 23:00:43 +00001447 it(opaque, bs);
bellard81d09122004-07-14 17:21:37 +00001448 }
1449}
1450
bellardea2384d2004-08-01 21:59:26 +00001451const char *bdrv_get_device_name(BlockDriverState *bs)
1452{
1453 return bs->device_name;
1454}
1455
Kevin Wolf205ef792010-10-21 16:43:43 +02001456int bdrv_flush(BlockDriverState *bs)
pbrook7a6cba62006-06-04 11:39:07 +00001457{
Alexander Graf016f5cf2010-05-26 17:51:49 +02001458 if (bs->open_flags & BDRV_O_NO_FLUSH) {
Kevin Wolf205ef792010-10-21 16:43:43 +02001459 return 0;
Alexander Graf016f5cf2010-05-26 17:51:49 +02001460 }
1461
Kevin Wolf205ef792010-10-21 16:43:43 +02001462 if (bs->drv && bs->drv->bdrv_flush) {
1463 return bs->drv->bdrv_flush(bs);
1464 }
1465
1466 /*
1467 * Some block drivers always operate in either writethrough or unsafe mode
1468 * and don't support bdrv_flush therefore. Usually qemu doesn't know how
1469 * the server works (because the behaviour is hardcoded or depends on
1470 * server-side configuration), so we can't ensure that everything is safe
1471 * on disk. Returning an error doesn't work because that would break guests
1472 * even if the server operates in writethrough mode.
1473 *
1474 * Let's hope the user knows what he's doing.
1475 */
1476 return 0;
pbrook7a6cba62006-06-04 11:39:07 +00001477}
1478
aliguoric6ca28d2008-10-06 13:55:43 +00001479void bdrv_flush_all(void)
1480{
1481 BlockDriverState *bs;
1482
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001483 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1484 if (bs->drv && !bdrv_is_read_only(bs) &&
1485 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) {
aliguoric6ca28d2008-10-06 13:55:43 +00001486 bdrv_flush(bs);
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001487 }
1488 }
aliguoric6ca28d2008-10-06 13:55:43 +00001489}
1490
Kevin Wolff2feebb2010-04-14 17:30:35 +02001491int bdrv_has_zero_init(BlockDriverState *bs)
1492{
1493 assert(bs->drv);
1494
Kevin Wolf336c1c12010-07-28 11:26:29 +02001495 if (bs->drv->bdrv_has_zero_init) {
1496 return bs->drv->bdrv_has_zero_init(bs);
Kevin Wolff2feebb2010-04-14 17:30:35 +02001497 }
1498
1499 return 1;
1500}
1501
thsf58c7b32008-06-05 21:53:49 +00001502/*
1503 * Returns true iff the specified sector is present in the disk image. Drivers
1504 * not implementing the functionality are assumed to not support backing files,
1505 * hence all their sectors are reported as allocated.
1506 *
1507 * 'pnum' is set to the number of sectors (including and immediately following
1508 * the specified sector) that are known to be in the same
1509 * allocated/unallocated state.
1510 *
1511 * 'nb_sectors' is the max value 'pnum' should be set to.
1512 */
1513int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1514 int *pnum)
1515{
1516 int64_t n;
1517 if (!bs->drv->bdrv_is_allocated) {
1518 if (sector_num >= bs->total_sectors) {
1519 *pnum = 0;
1520 return 0;
1521 }
1522 n = bs->total_sectors - sector_num;
1523 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1524 return 1;
1525 }
1526 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1527}
1528
Luiz Capitulino2582bfe2010-02-03 12:41:01 -02001529void bdrv_mon_event(const BlockDriverState *bdrv,
1530 BlockMonEventAction action, int is_read)
1531{
1532 QObject *data;
1533 const char *action_str;
1534
1535 switch (action) {
1536 case BDRV_ACTION_REPORT:
1537 action_str = "report";
1538 break;
1539 case BDRV_ACTION_IGNORE:
1540 action_str = "ignore";
1541 break;
1542 case BDRV_ACTION_STOP:
1543 action_str = "stop";
1544 break;
1545 default:
1546 abort();
1547 }
1548
1549 data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
1550 bdrv->device_name,
1551 action_str,
1552 is_read ? "read" : "write");
1553 monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data);
1554
1555 qobject_decref(data);
1556}
1557
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001558static void bdrv_print_dict(QObject *obj, void *opaque)
bellardb3380822004-03-14 21:38:54 +00001559{
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001560 QDict *bs_dict;
1561 Monitor *mon = opaque;
1562
1563 bs_dict = qobject_to_qdict(obj);
1564
1565 monitor_printf(mon, "%s: type=%s removable=%d",
1566 qdict_get_str(bs_dict, "device"),
1567 qdict_get_str(bs_dict, "type"),
1568 qdict_get_bool(bs_dict, "removable"));
1569
1570 if (qdict_get_bool(bs_dict, "removable")) {
1571 monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked"));
1572 }
1573
1574 if (qdict_haskey(bs_dict, "inserted")) {
1575 QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted"));
1576
1577 monitor_printf(mon, " file=");
1578 monitor_print_filename(mon, qdict_get_str(qdict, "file"));
1579 if (qdict_haskey(qdict, "backing_file")) {
1580 monitor_printf(mon, " backing_file=");
1581 monitor_print_filename(mon, qdict_get_str(qdict, "backing_file"));
1582 }
1583 monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
1584 qdict_get_bool(qdict, "ro"),
1585 qdict_get_str(qdict, "drv"),
1586 qdict_get_bool(qdict, "encrypted"));
1587 } else {
1588 monitor_printf(mon, " [not inserted]");
1589 }
1590
1591 monitor_printf(mon, "\n");
1592}
1593
1594void bdrv_info_print(Monitor *mon, const QObject *data)
1595{
1596 qlist_iter(qobject_to_qlist(data), bdrv_print_dict, mon);
1597}
1598
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001599void bdrv_info(Monitor *mon, QObject **ret_data)
1600{
1601 QList *bs_list;
bellardb3380822004-03-14 21:38:54 +00001602 BlockDriverState *bs;
1603
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001604 bs_list = qlist_new();
1605
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001606 QTAILQ_FOREACH(bs, &bdrv_states, list) {
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001607 QObject *bs_obj;
1608 const char *type = "unknown";
1609
bellardb3380822004-03-14 21:38:54 +00001610 switch(bs->type) {
1611 case BDRV_TYPE_HD:
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001612 type = "hd";
bellardb3380822004-03-14 21:38:54 +00001613 break;
1614 case BDRV_TYPE_CDROM:
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001615 type = "cdrom";
bellardb3380822004-03-14 21:38:54 +00001616 break;
1617 case BDRV_TYPE_FLOPPY:
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001618 type = "floppy";
bellardb3380822004-03-14 21:38:54 +00001619 break;
1620 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001621
1622 bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': %s, "
1623 "'removable': %i, 'locked': %i }",
1624 bs->device_name, type, bs->removable,
1625 bs->locked);
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001626
bellard19cb3732006-08-19 11:45:59 +00001627 if (bs->drv) {
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001628 QObject *obj;
1629 QDict *bs_dict = qobject_to_qdict(bs_obj);
1630
1631 obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, "
1632 "'encrypted': %i }",
1633 bs->filename, bs->read_only,
1634 bs->drv->format_name,
1635 bdrv_is_encrypted(bs));
thsfef30742006-12-22 14:11:32 +00001636 if (bs->backing_file[0] != '\0') {
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001637 QDict *qdict = qobject_to_qdict(obj);
1638 qdict_put(qdict, "backing_file",
1639 qstring_from_str(bs->backing_file));
aliguori376253e2009-03-05 23:01:23 +00001640 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001641
1642 qdict_put_obj(bs_dict, "inserted", obj);
bellardb3380822004-03-14 21:38:54 +00001643 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001644 qlist_append_obj(bs_list, bs_obj);
bellardb3380822004-03-14 21:38:54 +00001645 }
Luiz Capitulinod15e5462009-12-10 17:16:06 -02001646
1647 *ret_data = QOBJECT(bs_list);
bellardb3380822004-03-14 21:38:54 +00001648}
thsa36e69d2007-12-02 05:18:19 +00001649
Luiz Capitulino218a5362009-12-10 17:16:07 -02001650static void bdrv_stats_iter(QObject *data, void *opaque)
thsa36e69d2007-12-02 05:18:19 +00001651{
Luiz Capitulino218a5362009-12-10 17:16:07 -02001652 QDict *qdict;
1653 Monitor *mon = opaque;
1654
1655 qdict = qobject_to_qdict(data);
1656 monitor_printf(mon, "%s:", qdict_get_str(qdict, "device"));
1657
1658 qdict = qobject_to_qdict(qdict_get(qdict, "stats"));
1659 monitor_printf(mon, " rd_bytes=%" PRId64
1660 " wr_bytes=%" PRId64
1661 " rd_operations=%" PRId64
1662 " wr_operations=%" PRId64
1663 "\n",
1664 qdict_get_int(qdict, "rd_bytes"),
1665 qdict_get_int(qdict, "wr_bytes"),
1666 qdict_get_int(qdict, "rd_operations"),
1667 qdict_get_int(qdict, "wr_operations"));
1668}
1669
1670void bdrv_stats_print(Monitor *mon, const QObject *data)
1671{
1672 qlist_iter(qobject_to_qlist(data), bdrv_stats_iter, mon);
1673}
1674
Kevin Wolf294cc352010-04-28 14:34:01 +02001675static QObject* bdrv_info_stats_bs(BlockDriverState *bs)
1676{
1677 QObject *res;
1678 QDict *dict;
1679
1680 res = qobject_from_jsonf("{ 'stats': {"
1681 "'rd_bytes': %" PRId64 ","
1682 "'wr_bytes': %" PRId64 ","
1683 "'rd_operations': %" PRId64 ","
1684 "'wr_operations': %" PRId64 ","
1685 "'wr_highest_offset': %" PRId64
1686 "} }",
1687 bs->rd_bytes, bs->wr_bytes,
1688 bs->rd_ops, bs->wr_ops,
Blue Swirl5ffbbc62010-06-14 18:55:33 +00001689 bs->wr_highest_sector *
1690 (uint64_t)BDRV_SECTOR_SIZE);
Kevin Wolf294cc352010-04-28 14:34:01 +02001691 dict = qobject_to_qdict(res);
1692
1693 if (*bs->device_name) {
1694 qdict_put(dict, "device", qstring_from_str(bs->device_name));
1695 }
1696
1697 if (bs->file) {
1698 QObject *parent = bdrv_info_stats_bs(bs->file);
1699 qdict_put_obj(dict, "parent", parent);
1700 }
1701
1702 return res;
1703}
1704
Luiz Capitulino218a5362009-12-10 17:16:07 -02001705void bdrv_info_stats(Monitor *mon, QObject **ret_data)
1706{
1707 QObject *obj;
1708 QList *devices;
thsa36e69d2007-12-02 05:18:19 +00001709 BlockDriverState *bs;
1710
Luiz Capitulino218a5362009-12-10 17:16:07 -02001711 devices = qlist_new();
1712
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01001713 QTAILQ_FOREACH(bs, &bdrv_states, list) {
Kevin Wolf294cc352010-04-28 14:34:01 +02001714 obj = bdrv_info_stats_bs(bs);
Luiz Capitulino218a5362009-12-10 17:16:07 -02001715 qlist_append_obj(devices, obj);
thsa36e69d2007-12-02 05:18:19 +00001716 }
Luiz Capitulino218a5362009-12-10 17:16:07 -02001717
1718 *ret_data = QOBJECT(devices);
thsa36e69d2007-12-02 05:18:19 +00001719}
bellardea2384d2004-08-01 21:59:26 +00001720
aliguori045df332009-03-05 23:00:48 +00001721const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1722{
1723 if (bs->backing_hd && bs->backing_hd->encrypted)
1724 return bs->backing_file;
1725 else if (bs->encrypted)
1726 return bs->filename;
1727 else
1728 return NULL;
1729}
1730
ths5fafdf22007-09-16 21:08:06 +00001731void bdrv_get_backing_filename(BlockDriverState *bs,
bellard83f64092006-08-01 16:21:11 +00001732 char *filename, int filename_size)
bellardea2384d2004-08-01 21:59:26 +00001733{
Kevin Wolfb783e402010-01-12 12:55:16 +01001734 if (!bs->backing_file) {
bellard83f64092006-08-01 16:21:11 +00001735 pstrcpy(filename, filename_size, "");
1736 } else {
1737 pstrcpy(filename, filename_size, bs->backing_file);
1738 }
bellardea2384d2004-08-01 21:59:26 +00001739}
1740
ths5fafdf22007-09-16 21:08:06 +00001741int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
bellardfaea38e2006-08-05 21:31:00 +00001742 const uint8_t *buf, int nb_sectors)
1743{
1744 BlockDriver *drv = bs->drv;
1745 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001746 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001747 if (!drv->bdrv_write_compressed)
1748 return -ENOTSUP;
Kevin Wolffbb7b4e2009-05-08 14:47:24 +02001749 if (bdrv_check_request(bs, sector_num, nb_sectors))
1750 return -EIO;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001751
Jan Kiszkac6d22832009-11-30 18:21:20 +01001752 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001753 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1754 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001755
bellardfaea38e2006-08-05 21:31:00 +00001756 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1757}
ths3b46e622007-09-17 08:09:54 +00001758
bellardfaea38e2006-08-05 21:31:00 +00001759int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1760{
1761 BlockDriver *drv = bs->drv;
1762 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001763 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001764 if (!drv->bdrv_get_info)
1765 return -ENOTSUP;
1766 memset(bdi, 0, sizeof(*bdi));
1767 return drv->bdrv_get_info(bs, bdi);
1768}
1769
Christoph Hellwig45566e92009-07-10 23:11:57 +02001770int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
1771 int64_t pos, int size)
aliguori178e08a2009-04-05 19:10:55 +00001772{
1773 BlockDriver *drv = bs->drv;
1774 if (!drv)
1775 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09001776 if (drv->bdrv_save_vmstate)
1777 return drv->bdrv_save_vmstate(bs, buf, pos, size);
1778 if (bs->file)
1779 return bdrv_save_vmstate(bs->file, buf, pos, size);
1780 return -ENOTSUP;
aliguori178e08a2009-04-05 19:10:55 +00001781}
1782
Christoph Hellwig45566e92009-07-10 23:11:57 +02001783int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1784 int64_t pos, int size)
aliguori178e08a2009-04-05 19:10:55 +00001785{
1786 BlockDriver *drv = bs->drv;
1787 if (!drv)
1788 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09001789 if (drv->bdrv_load_vmstate)
1790 return drv->bdrv_load_vmstate(bs, buf, pos, size);
1791 if (bs->file)
1792 return bdrv_load_vmstate(bs->file, buf, pos, size);
1793 return -ENOTSUP;
aliguori178e08a2009-04-05 19:10:55 +00001794}
1795
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01001796void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
1797{
1798 BlockDriver *drv = bs->drv;
1799
1800 if (!drv || !drv->bdrv_debug_event) {
1801 return;
1802 }
1803
1804 return drv->bdrv_debug_event(bs, event);
1805
1806}
1807
bellardfaea38e2006-08-05 21:31:00 +00001808/**************************************************************/
1809/* handling of snapshots */
1810
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03001811int bdrv_can_snapshot(BlockDriverState *bs)
1812{
1813 BlockDriver *drv = bs->drv;
1814 if (!drv || bdrv_is_removable(bs) || bdrv_is_read_only(bs)) {
1815 return 0;
1816 }
1817
1818 if (!drv->bdrv_snapshot_create) {
1819 if (bs->file != NULL) {
1820 return bdrv_can_snapshot(bs->file);
1821 }
1822 return 0;
1823 }
1824
1825 return 1;
1826}
1827
Blue Swirl199630b2010-07-25 20:49:34 +00001828int bdrv_is_snapshot(BlockDriverState *bs)
1829{
1830 return !!(bs->open_flags & BDRV_O_SNAPSHOT);
1831}
1832
Markus Armbrusterf9092b12010-06-25 10:33:39 +02001833BlockDriverState *bdrv_snapshots(void)
1834{
1835 BlockDriverState *bs;
1836
Markus Armbruster3ac906f2010-07-01 09:30:38 +02001837 if (bs_snapshots) {
Markus Armbrusterf9092b12010-06-25 10:33:39 +02001838 return bs_snapshots;
Markus Armbruster3ac906f2010-07-01 09:30:38 +02001839 }
Markus Armbrusterf9092b12010-06-25 10:33:39 +02001840
1841 bs = NULL;
1842 while ((bs = bdrv_next(bs))) {
1843 if (bdrv_can_snapshot(bs)) {
Markus Armbruster3ac906f2010-07-01 09:30:38 +02001844 bs_snapshots = bs;
1845 return bs;
Markus Armbrusterf9092b12010-06-25 10:33:39 +02001846 }
1847 }
1848 return NULL;
Markus Armbrusterf9092b12010-06-25 10:33:39 +02001849}
1850
ths5fafdf22007-09-16 21:08:06 +00001851int bdrv_snapshot_create(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001852 QEMUSnapshotInfo *sn_info)
1853{
1854 BlockDriver *drv = bs->drv;
1855 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001856 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09001857 if (drv->bdrv_snapshot_create)
1858 return drv->bdrv_snapshot_create(bs, sn_info);
1859 if (bs->file)
1860 return bdrv_snapshot_create(bs->file, sn_info);
1861 return -ENOTSUP;
bellardfaea38e2006-08-05 21:31:00 +00001862}
1863
ths5fafdf22007-09-16 21:08:06 +00001864int bdrv_snapshot_goto(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001865 const char *snapshot_id)
1866{
1867 BlockDriver *drv = bs->drv;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09001868 int ret, open_ret;
1869
bellardfaea38e2006-08-05 21:31:00 +00001870 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001871 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09001872 if (drv->bdrv_snapshot_goto)
1873 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1874
1875 if (bs->file) {
1876 drv->bdrv_close(bs);
1877 ret = bdrv_snapshot_goto(bs->file, snapshot_id);
1878 open_ret = drv->bdrv_open(bs, bs->open_flags);
1879 if (open_ret < 0) {
1880 bdrv_delete(bs->file);
1881 bs->drv = NULL;
1882 return open_ret;
1883 }
1884 return ret;
1885 }
1886
1887 return -ENOTSUP;
bellardfaea38e2006-08-05 21:31:00 +00001888}
1889
1890int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1891{
1892 BlockDriver *drv = bs->drv;
1893 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001894 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09001895 if (drv->bdrv_snapshot_delete)
1896 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1897 if (bs->file)
1898 return bdrv_snapshot_delete(bs->file, snapshot_id);
1899 return -ENOTSUP;
bellardfaea38e2006-08-05 21:31:00 +00001900}
1901
ths5fafdf22007-09-16 21:08:06 +00001902int bdrv_snapshot_list(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001903 QEMUSnapshotInfo **psn_info)
1904{
1905 BlockDriver *drv = bs->drv;
1906 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001907 return -ENOMEDIUM;
MORITA Kazutaka7cdb1f62010-05-28 11:44:58 +09001908 if (drv->bdrv_snapshot_list)
1909 return drv->bdrv_snapshot_list(bs, psn_info);
1910 if (bs->file)
1911 return bdrv_snapshot_list(bs->file, psn_info);
1912 return -ENOTSUP;
bellardfaea38e2006-08-05 21:31:00 +00001913}
1914
edison51ef6722010-09-21 19:58:41 -07001915int bdrv_snapshot_load_tmp(BlockDriverState *bs,
1916 const char *snapshot_name)
1917{
1918 BlockDriver *drv = bs->drv;
1919 if (!drv) {
1920 return -ENOMEDIUM;
1921 }
1922 if (!bs->read_only) {
1923 return -EINVAL;
1924 }
1925 if (drv->bdrv_snapshot_load_tmp) {
1926 return drv->bdrv_snapshot_load_tmp(bs, snapshot_name);
1927 }
1928 return -ENOTSUP;
1929}
1930
bellardfaea38e2006-08-05 21:31:00 +00001931#define NB_SUFFIXES 4
1932
1933char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1934{
1935 static const char suffixes[NB_SUFFIXES] = "KMGT";
1936 int64_t base;
1937 int i;
1938
1939 if (size <= 999) {
1940 snprintf(buf, buf_size, "%" PRId64, size);
1941 } else {
1942 base = 1024;
1943 for(i = 0; i < NB_SUFFIXES; i++) {
1944 if (size < (10 * base)) {
ths5fafdf22007-09-16 21:08:06 +00001945 snprintf(buf, buf_size, "%0.1f%c",
bellardfaea38e2006-08-05 21:31:00 +00001946 (double)size / base,
1947 suffixes[i]);
1948 break;
1949 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
ths5fafdf22007-09-16 21:08:06 +00001950 snprintf(buf, buf_size, "%" PRId64 "%c",
bellardfaea38e2006-08-05 21:31:00 +00001951 ((size + (base >> 1)) / base),
1952 suffixes[i]);
1953 break;
1954 }
1955 base = base * 1024;
1956 }
1957 }
1958 return buf;
1959}
1960
1961char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1962{
1963 char buf1[128], date_buf[128], clock_buf[128];
bellard3b9f94e2007-01-07 17:27:07 +00001964#ifdef _WIN32
1965 struct tm *ptm;
1966#else
bellardfaea38e2006-08-05 21:31:00 +00001967 struct tm tm;
bellard3b9f94e2007-01-07 17:27:07 +00001968#endif
bellardfaea38e2006-08-05 21:31:00 +00001969 time_t ti;
1970 int64_t secs;
1971
1972 if (!sn) {
ths5fafdf22007-09-16 21:08:06 +00001973 snprintf(buf, buf_size,
1974 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001975 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1976 } else {
1977 ti = sn->date_sec;
bellard3b9f94e2007-01-07 17:27:07 +00001978#ifdef _WIN32
1979 ptm = localtime(&ti);
1980 strftime(date_buf, sizeof(date_buf),
1981 "%Y-%m-%d %H:%M:%S", ptm);
1982#else
bellardfaea38e2006-08-05 21:31:00 +00001983 localtime_r(&ti, &tm);
1984 strftime(date_buf, sizeof(date_buf),
1985 "%Y-%m-%d %H:%M:%S", &tm);
bellard3b9f94e2007-01-07 17:27:07 +00001986#endif
bellardfaea38e2006-08-05 21:31:00 +00001987 secs = sn->vm_clock_nsec / 1000000000;
1988 snprintf(clock_buf, sizeof(clock_buf),
1989 "%02d:%02d:%02d.%03d",
1990 (int)(secs / 3600),
1991 (int)((secs / 60) % 60),
ths5fafdf22007-09-16 21:08:06 +00001992 (int)(secs % 60),
bellardfaea38e2006-08-05 21:31:00 +00001993 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1994 snprintf(buf, buf_size,
ths5fafdf22007-09-16 21:08:06 +00001995 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001996 sn->id_str, sn->name,
1997 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1998 date_buf,
1999 clock_buf);
2000 }
2001 return buf;
2002}
2003
bellardea2384d2004-08-01 21:59:26 +00002004
bellard83f64092006-08-01 16:21:11 +00002005/**************************************************************/
2006/* async I/Os */
2007
aliguori3b69e4b2009-01-22 16:59:24 +00002008BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
aliguorif141eaf2009-04-07 18:43:24 +00002009 QEMUIOVector *qiov, int nb_sectors,
aliguori3b69e4b2009-01-22 16:59:24 +00002010 BlockDriverCompletionFunc *cb, void *opaque)
2011{
bellard83f64092006-08-01 16:21:11 +00002012 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00002013 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00002014
Stefan Hajnoczibbf0a442010-10-05 14:28:53 +01002015 trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque);
2016
bellard19cb3732006-08-19 11:45:59 +00002017 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00002018 return NULL;
aliguori71d07702009-03-03 17:37:16 +00002019 if (bdrv_check_request(bs, sector_num, nb_sectors))
2020 return NULL;
ths3b46e622007-09-17 08:09:54 +00002021
aliguorif141eaf2009-04-07 18:43:24 +00002022 ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
2023 cb, opaque);
thsa36e69d2007-12-02 05:18:19 +00002024
2025 if (ret) {
2026 /* Update stats even though technically transfer has not happened. */
Jan Kiszka6ea44302009-11-30 18:21:19 +01002027 bs->rd_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
thsa36e69d2007-12-02 05:18:19 +00002028 bs->rd_ops ++;
2029 }
2030
2031 return ret;
bellard83f64092006-08-01 16:21:11 +00002032}
2033
aliguorif141eaf2009-04-07 18:43:24 +00002034BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
2035 QEMUIOVector *qiov, int nb_sectors,
2036 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00002037{
bellard83f64092006-08-01 16:21:11 +00002038 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00002039 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00002040
Stefan Hajnoczibbf0a442010-10-05 14:28:53 +01002041 trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque);
2042
bellard19cb3732006-08-19 11:45:59 +00002043 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00002044 return NULL;
bellard83f64092006-08-01 16:21:11 +00002045 if (bs->read_only)
pbrookce1a14d2006-08-07 02:38:06 +00002046 return NULL;
aliguori71d07702009-03-03 17:37:16 +00002047 if (bdrv_check_request(bs, sector_num, nb_sectors))
2048 return NULL;
bellard83f64092006-08-01 16:21:11 +00002049
Jan Kiszkac6d22832009-11-30 18:21:20 +01002050 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002051 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
2052 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002053
aliguorif141eaf2009-04-07 18:43:24 +00002054 ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
2055 cb, opaque);
thsa36e69d2007-12-02 05:18:19 +00002056
2057 if (ret) {
Kevin Wolf294cc352010-04-28 14:34:01 +02002058 /* Update stats even though technically transfer has not happened. */
2059 bs->wr_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
2060 bs->wr_ops ++;
2061 if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
2062 bs->wr_highest_sector = sector_num + nb_sectors - 1;
2063 }
thsa36e69d2007-12-02 05:18:19 +00002064 }
2065
2066 return ret;
bellard83f64092006-08-01 16:21:11 +00002067}
2068
Kevin Wolf40b4f532009-09-09 17:53:37 +02002069
2070typedef struct MultiwriteCB {
2071 int error;
2072 int num_requests;
2073 int num_callbacks;
2074 struct {
2075 BlockDriverCompletionFunc *cb;
2076 void *opaque;
2077 QEMUIOVector *free_qiov;
2078 void *free_buf;
2079 } callbacks[];
2080} MultiwriteCB;
2081
2082static void multiwrite_user_cb(MultiwriteCB *mcb)
2083{
2084 int i;
2085
2086 for (i = 0; i < mcb->num_callbacks; i++) {
2087 mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
Stefan Hajnoczi1e1ea482010-04-21 20:35:45 +01002088 if (mcb->callbacks[i].free_qiov) {
2089 qemu_iovec_destroy(mcb->callbacks[i].free_qiov);
2090 }
Kevin Wolf40b4f532009-09-09 17:53:37 +02002091 qemu_free(mcb->callbacks[i].free_qiov);
Herve Poussineauf8a83242010-01-24 21:23:56 +00002092 qemu_vfree(mcb->callbacks[i].free_buf);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002093 }
2094}
2095
2096static void multiwrite_cb(void *opaque, int ret)
2097{
2098 MultiwriteCB *mcb = opaque;
2099
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +01002100 trace_multiwrite_cb(mcb, ret);
2101
Kevin Wolfcb6d3ca2010-04-01 22:48:44 +02002102 if (ret < 0 && !mcb->error) {
Kevin Wolf40b4f532009-09-09 17:53:37 +02002103 mcb->error = ret;
Kevin Wolf40b4f532009-09-09 17:53:37 +02002104 }
2105
2106 mcb->num_requests--;
2107 if (mcb->num_requests == 0) {
Kevin Wolfde189a12010-07-01 16:08:51 +02002108 multiwrite_user_cb(mcb);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002109 qemu_free(mcb);
2110 }
2111}
2112
2113static int multiwrite_req_compare(const void *a, const void *b)
2114{
Christoph Hellwig77be4362010-05-19 20:53:10 +02002115 const BlockRequest *req1 = a, *req2 = b;
2116
2117 /*
2118 * Note that we can't simply subtract req2->sector from req1->sector
2119 * here as that could overflow the return value.
2120 */
2121 if (req1->sector > req2->sector) {
2122 return 1;
2123 } else if (req1->sector < req2->sector) {
2124 return -1;
2125 } else {
2126 return 0;
2127 }
Kevin Wolf40b4f532009-09-09 17:53:37 +02002128}
2129
2130/*
2131 * Takes a bunch of requests and tries to merge them. Returns the number of
2132 * requests that remain after merging.
2133 */
2134static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
2135 int num_reqs, MultiwriteCB *mcb)
2136{
2137 int i, outidx;
2138
2139 // Sort requests by start sector
2140 qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
2141
2142 // Check if adjacent requests touch the same clusters. If so, combine them,
2143 // filling up gaps with zero sectors.
2144 outidx = 0;
2145 for (i = 1; i < num_reqs; i++) {
2146 int merge = 0;
2147 int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
2148
2149 // This handles the cases that are valid for all block drivers, namely
2150 // exactly sequential writes and overlapping writes.
2151 if (reqs[i].sector <= oldreq_last) {
2152 merge = 1;
2153 }
2154
2155 // The block driver may decide that it makes sense to combine requests
2156 // even if there is a gap of some sectors between them. In this case,
2157 // the gap is filled with zeros (therefore only applicable for yet
2158 // unused space in format like qcow2).
2159 if (!merge && bs->drv->bdrv_merge_requests) {
2160 merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]);
2161 }
2162
Christoph Hellwige2a305f2010-01-26 14:49:08 +01002163 if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) {
2164 merge = 0;
2165 }
2166
Kevin Wolf40b4f532009-09-09 17:53:37 +02002167 if (merge) {
2168 size_t size;
2169 QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov));
2170 qemu_iovec_init(qiov,
2171 reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
2172
2173 // Add the first request to the merged one. If the requests are
2174 // overlapping, drop the last sectors of the first request.
2175 size = (reqs[i].sector - reqs[outidx].sector) << 9;
2176 qemu_iovec_concat(qiov, reqs[outidx].qiov, size);
2177
2178 // We might need to add some zeros between the two requests
2179 if (reqs[i].sector > oldreq_last) {
2180 size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
2181 uint8_t *buf = qemu_blockalign(bs, zero_bytes);
2182 memset(buf, 0, zero_bytes);
2183 qemu_iovec_add(qiov, buf, zero_bytes);
2184 mcb->callbacks[i].free_buf = buf;
2185 }
2186
2187 // Add the second request
2188 qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size);
2189
Kevin Wolfcbf1dff2010-05-21 11:09:42 +02002190 reqs[outidx].nb_sectors = qiov->size >> 9;
Kevin Wolf40b4f532009-09-09 17:53:37 +02002191 reqs[outidx].qiov = qiov;
2192
2193 mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
2194 } else {
2195 outidx++;
2196 reqs[outidx].sector = reqs[i].sector;
2197 reqs[outidx].nb_sectors = reqs[i].nb_sectors;
2198 reqs[outidx].qiov = reqs[i].qiov;
2199 }
2200 }
2201
2202 return outidx + 1;
2203}
2204
2205/*
2206 * Submit multiple AIO write requests at once.
2207 *
2208 * On success, the function returns 0 and all requests in the reqs array have
2209 * been submitted. In error case this function returns -1, and any of the
2210 * requests may or may not be submitted yet. In particular, this means that the
2211 * callback will be called for some of the requests, for others it won't. The
2212 * caller must check the error field of the BlockRequest to wait for the right
2213 * callbacks (if error != 0, no callback will be called).
2214 *
2215 * The implementation may modify the contents of the reqs array, e.g. to merge
2216 * requests. However, the fields opaque and error are left unmodified as they
2217 * are used to signal failure for a single request to the caller.
2218 */
2219int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
2220{
2221 BlockDriverAIOCB *acb;
2222 MultiwriteCB *mcb;
2223 int i;
2224
2225 if (num_reqs == 0) {
2226 return 0;
2227 }
2228
2229 // Create MultiwriteCB structure
2230 mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
2231 mcb->num_requests = 0;
2232 mcb->num_callbacks = num_reqs;
2233
2234 for (i = 0; i < num_reqs; i++) {
2235 mcb->callbacks[i].cb = reqs[i].cb;
2236 mcb->callbacks[i].opaque = reqs[i].opaque;
2237 }
2238
2239 // Check for mergable requests
2240 num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
2241
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +01002242 trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs);
2243
Kevin Wolf453f9a12010-07-02 14:01:21 +02002244 /*
2245 * Run the aio requests. As soon as one request can't be submitted
2246 * successfully, fail all requests that are not yet submitted (we must
2247 * return failure for all requests anyway)
2248 *
2249 * num_requests cannot be set to the right value immediately: If
2250 * bdrv_aio_writev fails for some request, num_requests would be too high
2251 * and therefore multiwrite_cb() would never recognize the multiwrite
2252 * request as completed. We also cannot use the loop variable i to set it
2253 * when the first request fails because the callback may already have been
2254 * called for previously submitted requests. Thus, num_requests must be
2255 * incremented for each request that is submitted.
2256 *
2257 * The problem that callbacks may be called early also means that we need
2258 * to take care that num_requests doesn't become 0 before all requests are
2259 * submitted - multiwrite_cb() would consider the multiwrite request
2260 * completed. A dummy request that is "completed" by a manual call to
2261 * multiwrite_cb() takes care of this.
2262 */
2263 mcb->num_requests = 1;
2264
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +01002265 // Run the aio requests
Kevin Wolf40b4f532009-09-09 17:53:37 +02002266 for (i = 0; i < num_reqs; i++) {
Kevin Wolf453f9a12010-07-02 14:01:21 +02002267 mcb->num_requests++;
Kevin Wolf40b4f532009-09-09 17:53:37 +02002268 acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
2269 reqs[i].nb_sectors, multiwrite_cb, mcb);
2270
2271 if (acb == NULL) {
2272 // We can only fail the whole thing if no request has been
2273 // submitted yet. Otherwise we'll wait for the submitted AIOs to
2274 // complete and report the error in the callback.
Kevin Wolf453f9a12010-07-02 14:01:21 +02002275 if (i == 0) {
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +01002276 trace_bdrv_aio_multiwrite_earlyfail(mcb);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002277 goto fail;
2278 } else {
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +01002279 trace_bdrv_aio_multiwrite_latefail(mcb, i);
Kevin Wolf7eb58a62010-04-06 18:24:07 +02002280 multiwrite_cb(mcb, -EIO);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002281 break;
2282 }
Kevin Wolf40b4f532009-09-09 17:53:37 +02002283 }
2284 }
2285
Kevin Wolf453f9a12010-07-02 14:01:21 +02002286 /* Complete the dummy request */
2287 multiwrite_cb(mcb, 0);
2288
Kevin Wolf40b4f532009-09-09 17:53:37 +02002289 return 0;
2290
2291fail:
Kevin Wolf453f9a12010-07-02 14:01:21 +02002292 for (i = 0; i < mcb->num_callbacks; i++) {
2293 reqs[i].error = -EIO;
2294 }
Bruce Rogersaf474592010-05-13 15:14:33 -06002295 qemu_free(mcb);
Kevin Wolf40b4f532009-09-09 17:53:37 +02002296 return -1;
2297}
2298
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002299BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
2300 BlockDriverCompletionFunc *cb, void *opaque)
2301{
2302 BlockDriver *drv = bs->drv;
2303
Alexander Graf016f5cf2010-05-26 17:51:49 +02002304 if (bs->open_flags & BDRV_O_NO_FLUSH) {
2305 return bdrv_aio_noop_em(bs, cb, opaque);
2306 }
2307
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002308 if (!drv)
2309 return NULL;
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002310 return drv->bdrv_aio_flush(bs, cb, opaque);
2311}
2312
bellard83f64092006-08-01 16:21:11 +00002313void bdrv_aio_cancel(BlockDriverAIOCB *acb)
pbrookce1a14d2006-08-07 02:38:06 +00002314{
aliguori6bbff9a2009-03-20 18:25:59 +00002315 acb->pool->cancel(acb);
bellard83f64092006-08-01 16:21:11 +00002316}
2317
pbrookce1a14d2006-08-07 02:38:06 +00002318
bellard83f64092006-08-01 16:21:11 +00002319/**************************************************************/
2320/* async block device emulation */
2321
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002322typedef struct BlockDriverAIOCBSync {
2323 BlockDriverAIOCB common;
2324 QEMUBH *bh;
2325 int ret;
2326 /* vector translation state */
2327 QEMUIOVector *qiov;
2328 uint8_t *bounce;
2329 int is_write;
2330} BlockDriverAIOCBSync;
2331
2332static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
2333{
Kevin Wolfb666d232010-05-05 11:44:39 +02002334 BlockDriverAIOCBSync *acb =
2335 container_of(blockacb, BlockDriverAIOCBSync, common);
Dor Laor6a7ad292009-06-01 12:07:23 +03002336 qemu_bh_delete(acb->bh);
Avi Kivity36afc452009-06-23 16:20:36 +03002337 acb->bh = NULL;
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002338 qemu_aio_release(acb);
2339}
2340
2341static AIOPool bdrv_em_aio_pool = {
2342 .aiocb_size = sizeof(BlockDriverAIOCBSync),
2343 .cancel = bdrv_aio_cancel_em,
2344};
2345
bellard83f64092006-08-01 16:21:11 +00002346static void bdrv_aio_bh_cb(void *opaque)
bellardbeac80c2006-06-26 20:08:57 +00002347{
pbrookce1a14d2006-08-07 02:38:06 +00002348 BlockDriverAIOCBSync *acb = opaque;
aliguorif141eaf2009-04-07 18:43:24 +00002349
aliguorif141eaf2009-04-07 18:43:24 +00002350 if (!acb->is_write)
2351 qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
aliguoriceb42de2009-04-07 18:43:28 +00002352 qemu_vfree(acb->bounce);
pbrookce1a14d2006-08-07 02:38:06 +00002353 acb->common.cb(acb->common.opaque, acb->ret);
Dor Laor6a7ad292009-06-01 12:07:23 +03002354 qemu_bh_delete(acb->bh);
Avi Kivity36afc452009-06-23 16:20:36 +03002355 acb->bh = NULL;
pbrookce1a14d2006-08-07 02:38:06 +00002356 qemu_aio_release(acb);
bellardbeac80c2006-06-26 20:08:57 +00002357}
bellardbeac80c2006-06-26 20:08:57 +00002358
aliguorif141eaf2009-04-07 18:43:24 +00002359static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
2360 int64_t sector_num,
2361 QEMUIOVector *qiov,
2362 int nb_sectors,
2363 BlockDriverCompletionFunc *cb,
2364 void *opaque,
2365 int is_write)
2366
bellardea2384d2004-08-01 21:59:26 +00002367{
pbrookce1a14d2006-08-07 02:38:06 +00002368 BlockDriverAIOCBSync *acb;
pbrookce1a14d2006-08-07 02:38:06 +00002369
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002370 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
aliguorif141eaf2009-04-07 18:43:24 +00002371 acb->is_write = is_write;
2372 acb->qiov = qiov;
aliguorie268ca52009-04-22 20:20:00 +00002373 acb->bounce = qemu_blockalign(bs, qiov->size);
aliguorif141eaf2009-04-07 18:43:24 +00002374
pbrookce1a14d2006-08-07 02:38:06 +00002375 if (!acb->bh)
2376 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
aliguorif141eaf2009-04-07 18:43:24 +00002377
2378 if (is_write) {
2379 qemu_iovec_to_buffer(acb->qiov, acb->bounce);
2380 acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
2381 } else {
2382 acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
2383 }
2384
pbrookce1a14d2006-08-07 02:38:06 +00002385 qemu_bh_schedule(acb->bh);
aliguorif141eaf2009-04-07 18:43:24 +00002386
pbrookce1a14d2006-08-07 02:38:06 +00002387 return &acb->common;
pbrook7a6cba62006-06-04 11:39:07 +00002388}
2389
aliguorif141eaf2009-04-07 18:43:24 +00002390static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
2391 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
pbrookce1a14d2006-08-07 02:38:06 +00002392 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00002393{
aliguorif141eaf2009-04-07 18:43:24 +00002394 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
bellard83f64092006-08-01 16:21:11 +00002395}
2396
aliguorif141eaf2009-04-07 18:43:24 +00002397static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
2398 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2399 BlockDriverCompletionFunc *cb, void *opaque)
2400{
2401 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
2402}
2403
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02002404static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
2405 BlockDriverCompletionFunc *cb, void *opaque)
2406{
2407 BlockDriverAIOCBSync *acb;
2408
2409 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2410 acb->is_write = 1; /* don't bounce in the completion hadler */
2411 acb->qiov = NULL;
2412 acb->bounce = NULL;
2413 acb->ret = 0;
2414
2415 if (!acb->bh)
2416 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2417
2418 bdrv_flush(bs);
2419 qemu_bh_schedule(acb->bh);
2420 return &acb->common;
2421}
2422
Alexander Graf016f5cf2010-05-26 17:51:49 +02002423static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,
2424 BlockDriverCompletionFunc *cb, void *opaque)
2425{
2426 BlockDriverAIOCBSync *acb;
2427
2428 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2429 acb->is_write = 1; /* don't bounce in the completion handler */
2430 acb->qiov = NULL;
2431 acb->bounce = NULL;
2432 acb->ret = 0;
2433
2434 if (!acb->bh) {
2435 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2436 }
2437
2438 qemu_bh_schedule(acb->bh);
2439 return &acb->common;
2440}
2441
bellard83f64092006-08-01 16:21:11 +00002442/**************************************************************/
2443/* sync block device emulation */
2444
2445static void bdrv_rw_em_cb(void *opaque, int ret)
2446{
2447 *(int *)opaque = ret;
2448}
2449
2450#define NOT_DONE 0x7fffffff
2451
ths5fafdf22007-09-16 21:08:06 +00002452static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +00002453 uint8_t *buf, int nb_sectors)
2454{
pbrookce1a14d2006-08-07 02:38:06 +00002455 int async_ret;
2456 BlockDriverAIOCB *acb;
aliguorif141eaf2009-04-07 18:43:24 +00002457 struct iovec iov;
2458 QEMUIOVector qiov;
bellard83f64092006-08-01 16:21:11 +00002459
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002460 async_context_push();
2461
bellard83f64092006-08-01 16:21:11 +00002462 async_ret = NOT_DONE;
blueswir13f4cb3d2009-04-13 16:31:01 +00002463 iov.iov_base = (void *)buf;
Jes Sorenseneb5a3162010-05-27 16:20:31 +02002464 iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
aliguorif141eaf2009-04-07 18:43:24 +00002465 qemu_iovec_init_external(&qiov, &iov, 1);
2466 acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
2467 bdrv_rw_em_cb, &async_ret);
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002468 if (acb == NULL) {
2469 async_ret = -1;
2470 goto fail;
2471 }
aliguoribaf35cb2008-09-10 15:45:19 +00002472
bellard83f64092006-08-01 16:21:11 +00002473 while (async_ret == NOT_DONE) {
2474 qemu_aio_wait();
2475 }
aliguoribaf35cb2008-09-10 15:45:19 +00002476
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002477
2478fail:
2479 async_context_pop();
bellard83f64092006-08-01 16:21:11 +00002480 return async_ret;
2481}
2482
2483static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
2484 const uint8_t *buf, int nb_sectors)
2485{
pbrookce1a14d2006-08-07 02:38:06 +00002486 int async_ret;
2487 BlockDriverAIOCB *acb;
aliguorif141eaf2009-04-07 18:43:24 +00002488 struct iovec iov;
2489 QEMUIOVector qiov;
bellard83f64092006-08-01 16:21:11 +00002490
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002491 async_context_push();
2492
bellard83f64092006-08-01 16:21:11 +00002493 async_ret = NOT_DONE;
aliguorif141eaf2009-04-07 18:43:24 +00002494 iov.iov_base = (void *)buf;
Jes Sorenseneb5a3162010-05-27 16:20:31 +02002495 iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
aliguorif141eaf2009-04-07 18:43:24 +00002496 qemu_iovec_init_external(&qiov, &iov, 1);
2497 acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
2498 bdrv_rw_em_cb, &async_ret);
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002499 if (acb == NULL) {
2500 async_ret = -1;
2501 goto fail;
2502 }
bellard83f64092006-08-01 16:21:11 +00002503 while (async_ret == NOT_DONE) {
2504 qemu_aio_wait();
2505 }
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02002506
2507fail:
2508 async_context_pop();
bellard83f64092006-08-01 16:21:11 +00002509 return async_ret;
2510}
bellardea2384d2004-08-01 21:59:26 +00002511
2512void bdrv_init(void)
2513{
Anthony Liguori5efa9d52009-05-09 17:03:42 -05002514 module_call_init(MODULE_INIT_BLOCK);
bellardea2384d2004-08-01 21:59:26 +00002515}
pbrookce1a14d2006-08-07 02:38:06 +00002516
Markus Armbrustereb852012009-10-27 18:41:44 +01002517void bdrv_init_with_whitelist(void)
2518{
2519 use_bdrv_whitelist = 1;
2520 bdrv_init();
2521}
2522
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02002523void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
2524 BlockDriverCompletionFunc *cb, void *opaque)
aliguori6bbff9a2009-03-20 18:25:59 +00002525{
pbrookce1a14d2006-08-07 02:38:06 +00002526 BlockDriverAIOCB *acb;
2527
aliguori6bbff9a2009-03-20 18:25:59 +00002528 if (pool->free_aiocb) {
2529 acb = pool->free_aiocb;
2530 pool->free_aiocb = acb->next;
pbrookce1a14d2006-08-07 02:38:06 +00002531 } else {
aliguori6bbff9a2009-03-20 18:25:59 +00002532 acb = qemu_mallocz(pool->aiocb_size);
2533 acb->pool = pool;
pbrookce1a14d2006-08-07 02:38:06 +00002534 }
2535 acb->bs = bs;
2536 acb->cb = cb;
2537 acb->opaque = opaque;
2538 return acb;
2539}
2540
2541void qemu_aio_release(void *p)
2542{
aliguori6bbff9a2009-03-20 18:25:59 +00002543 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
2544 AIOPool *pool = acb->pool;
2545 acb->next = pool->free_aiocb;
2546 pool->free_aiocb = acb;
pbrookce1a14d2006-08-07 02:38:06 +00002547}
bellard19cb3732006-08-19 11:45:59 +00002548
2549/**************************************************************/
2550/* removable device support */
2551
2552/**
2553 * Return TRUE if the media is present
2554 */
2555int bdrv_is_inserted(BlockDriverState *bs)
2556{
2557 BlockDriver *drv = bs->drv;
2558 int ret;
2559 if (!drv)
2560 return 0;
2561 if (!drv->bdrv_is_inserted)
Markus Armbruster4be97622010-07-27 14:02:01 +02002562 return !bs->tray_open;
bellard19cb3732006-08-19 11:45:59 +00002563 ret = drv->bdrv_is_inserted(bs);
2564 return ret;
2565}
2566
2567/**
2568 * Return TRUE if the media changed since the last call to this
ths5fafdf22007-09-16 21:08:06 +00002569 * function. It is currently only used for floppy disks
bellard19cb3732006-08-19 11:45:59 +00002570 */
2571int bdrv_media_changed(BlockDriverState *bs)
2572{
2573 BlockDriver *drv = bs->drv;
2574 int ret;
2575
2576 if (!drv || !drv->bdrv_media_changed)
2577 ret = -ENOTSUP;
2578 else
2579 ret = drv->bdrv_media_changed(bs);
2580 if (ret == -ENOTSUP)
2581 ret = bs->media_changed;
2582 bs->media_changed = 0;
2583 return ret;
2584}
2585
2586/**
2587 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
2588 */
Mark McLoughlinaea2a332009-05-27 10:06:11 +01002589int bdrv_eject(BlockDriverState *bs, int eject_flag)
bellard19cb3732006-08-19 11:45:59 +00002590{
2591 BlockDriver *drv = bs->drv;
2592 int ret;
2593
Mark McLoughlinaea2a332009-05-27 10:06:11 +01002594 if (bs->locked) {
2595 return -EBUSY;
2596 }
2597
bellard19cb3732006-08-19 11:45:59 +00002598 if (!drv || !drv->bdrv_eject) {
2599 ret = -ENOTSUP;
2600 } else {
2601 ret = drv->bdrv_eject(bs, eject_flag);
2602 }
2603 if (ret == -ENOTSUP) {
Mark McLoughlinaea2a332009-05-27 10:06:11 +01002604 ret = 0;
bellard19cb3732006-08-19 11:45:59 +00002605 }
Markus Armbruster4be97622010-07-27 14:02:01 +02002606 if (ret >= 0) {
2607 bs->tray_open = eject_flag;
2608 }
Mark McLoughlinaea2a332009-05-27 10:06:11 +01002609
2610 return ret;
bellard19cb3732006-08-19 11:45:59 +00002611}
2612
2613int bdrv_is_locked(BlockDriverState *bs)
2614{
2615 return bs->locked;
2616}
2617
2618/**
2619 * Lock or unlock the media (if it is locked, the user won't be able
2620 * to eject it manually).
2621 */
2622void bdrv_set_locked(BlockDriverState *bs, int locked)
2623{
2624 BlockDriver *drv = bs->drv;
2625
2626 bs->locked = locked;
2627 if (drv && drv->bdrv_set_locked) {
2628 drv->bdrv_set_locked(bs, locked);
2629 }
2630}
ths985a03b2007-12-24 16:10:43 +00002631
2632/* needed for generic scsi interface */
2633
2634int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
2635{
2636 BlockDriver *drv = bs->drv;
2637
2638 if (drv && drv->bdrv_ioctl)
2639 return drv->bdrv_ioctl(bs, req, buf);
2640 return -ENOTSUP;
2641}
aliguori7d780662009-03-12 19:57:08 +00002642
aliguori221f7152009-03-28 17:28:41 +00002643BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
2644 unsigned long int req, void *buf,
2645 BlockDriverCompletionFunc *cb, void *opaque)
aliguori7d780662009-03-12 19:57:08 +00002646{
aliguori221f7152009-03-28 17:28:41 +00002647 BlockDriver *drv = bs->drv;
aliguori7d780662009-03-12 19:57:08 +00002648
aliguori221f7152009-03-28 17:28:41 +00002649 if (drv && drv->bdrv_aio_ioctl)
2650 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
2651 return NULL;
aliguori7d780662009-03-12 19:57:08 +00002652}
aliguorie268ca52009-04-22 20:20:00 +00002653
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002654
2655
aliguorie268ca52009-04-22 20:20:00 +00002656void *qemu_blockalign(BlockDriverState *bs, size_t size)
2657{
2658 return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
2659}
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002660
2661void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable)
2662{
2663 int64_t bitmap_size;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002664
Liran Schouraaa0eb72010-01-26 10:31:48 +02002665 bs->dirty_count = 0;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002666 if (enable) {
Jan Kiszkac6d22832009-11-30 18:21:20 +01002667 if (!bs->dirty_bitmap) {
2668 bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
2669 BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
2670 bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002671
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002672 bs->dirty_bitmap = qemu_mallocz(bitmap_size);
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002673 }
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002674 } else {
Jan Kiszkac6d22832009-11-30 18:21:20 +01002675 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002676 qemu_free(bs->dirty_bitmap);
Jan Kiszkac6d22832009-11-30 18:21:20 +01002677 bs->dirty_bitmap = NULL;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002678 }
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002679 }
2680}
2681
2682int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
2683{
Jan Kiszka6ea44302009-11-30 18:21:19 +01002684 int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002685
Jan Kiszkac6d22832009-11-30 18:21:20 +01002686 if (bs->dirty_bitmap &&
2687 (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) {
Marcelo Tosatti6d59fec2010-11-08 17:02:54 -02002688 return !!(bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] &
2689 (1UL << (chunk % (sizeof(unsigned long) * 8))));
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002690 } else {
2691 return 0;
2692 }
2693}
2694
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002695void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
2696 int nr_sectors)
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002697{
2698 set_dirty_bitmap(bs, cur_sector, nr_sectors, 0);
2699}
Liran Schouraaa0eb72010-01-26 10:31:48 +02002700
2701int64_t bdrv_get_dirty_count(BlockDriverState *bs)
2702{
2703 return bs->dirty_count;
2704}