blob: 05c83115b3bdb9238ba67be8d2a095f4362484e7 [file] [log] [blame]
bellardfc01f7e2003-06-30 10:03:06 +00001/*
2 * QEMU System Emulator block driver
ths5fafdf22007-09-16 21:08:06 +00003 *
bellardfc01f7e2003-06-30 10:03:06 +00004 * Copyright (c) 2003 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellardfc01f7e2003-06-30 10:03:06 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
blueswir13990d092008-12-05 17:53:21 +000024#include "config-host.h"
pbrookfaf07962007-11-11 02:51:17 +000025#include "qemu-common.h"
aliguori376253e2009-03-05 23:01:23 +000026#include "monitor.h"
bellardea2384d2004-08-01 21:59:26 +000027#include "block_int.h"
Anthony Liguori5efa9d52009-05-09 17:03:42 -050028#include "module.h"
bellardfc01f7e2003-06-30 10:03:06 +000029
Juan Quintela71e72a12009-07-27 16:12:56 +020030#ifdef CONFIG_BSD
bellard7674e7b2005-04-26 21:59:26 +000031#include <sys/types.h>
32#include <sys/stat.h>
33#include <sys/ioctl.h>
Blue Swirl72cf2d42009-09-12 07:36:22 +000034#include <sys/queue.h>
blueswir1c5e97232009-03-07 20:06:23 +000035#ifndef __DragonFly__
bellard7674e7b2005-04-26 21:59:26 +000036#include <sys/disk.h>
37#endif
blueswir1c5e97232009-03-07 20:06:23 +000038#endif
bellard7674e7b2005-04-26 21:59:26 +000039
aliguori49dc7682009-03-08 16:26:59 +000040#ifdef _WIN32
41#include <windows.h>
42#endif
43
bellard83f64092006-08-01 16:21:11 +000044#define SECTOR_BITS 9
45#define SECTOR_SIZE (1 << SECTOR_BITS)
bellard3b0d4f62005-10-30 18:30:10 +000046
aliguorif141eaf2009-04-07 18:43:24 +000047static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
48 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
aliguoric87c0672009-04-07 18:43:20 +000049 BlockDriverCompletionFunc *cb, void *opaque);
aliguorif141eaf2009-04-07 18:43:24 +000050static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
51 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
pbrookce1a14d2006-08-07 02:38:06 +000052 BlockDriverCompletionFunc *cb, void *opaque);
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +020053static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
54 BlockDriverCompletionFunc *cb, void *opaque);
ths5fafdf22007-09-16 21:08:06 +000055static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +000056 uint8_t *buf, int nb_sectors);
57static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
58 const uint8_t *buf, int nb_sectors);
bellardec530c82006-04-25 22:36:06 +000059
blueswir17ee930d2008-09-17 19:04:14 +000060BlockDriverState *bdrv_first;
61
bellardea2384d2004-08-01 21:59:26 +000062static BlockDriver *first_drv;
63
bellard83f64092006-08-01 16:21:11 +000064int path_is_absolute(const char *path)
65{
66 const char *p;
bellard21664422007-01-07 18:22:37 +000067#ifdef _WIN32
68 /* specific case for names like: "\\.\d:" */
69 if (*path == '/' || *path == '\\')
70 return 1;
71#endif
bellard83f64092006-08-01 16:21:11 +000072 p = strchr(path, ':');
73 if (p)
74 p++;
75 else
76 p = path;
bellard3b9f94e2007-01-07 17:27:07 +000077#ifdef _WIN32
78 return (*p == '/' || *p == '\\');
79#else
80 return (*p == '/');
81#endif
bellard83f64092006-08-01 16:21:11 +000082}
83
84/* if filename is absolute, just copy it to dest. Otherwise, build a
85 path to it by considering it is relative to base_path. URL are
86 supported. */
87void path_combine(char *dest, int dest_size,
88 const char *base_path,
89 const char *filename)
90{
91 const char *p, *p1;
92 int len;
93
94 if (dest_size <= 0)
95 return;
96 if (path_is_absolute(filename)) {
97 pstrcpy(dest, dest_size, filename);
98 } else {
99 p = strchr(base_path, ':');
100 if (p)
101 p++;
102 else
103 p = base_path;
bellard3b9f94e2007-01-07 17:27:07 +0000104 p1 = strrchr(base_path, '/');
105#ifdef _WIN32
106 {
107 const char *p2;
108 p2 = strrchr(base_path, '\\');
109 if (!p1 || p2 > p1)
110 p1 = p2;
111 }
112#endif
bellard83f64092006-08-01 16:21:11 +0000113 if (p1)
114 p1++;
115 else
116 p1 = base_path;
117 if (p1 > p)
118 p = p1;
119 len = p - base_path;
120 if (len > dest_size - 1)
121 len = dest_size - 1;
122 memcpy(dest, base_path, len);
123 dest[len] = '\0';
124 pstrcat(dest, dest_size, filename);
125 }
126}
127
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500128void bdrv_register(BlockDriver *bdrv)
bellardea2384d2004-08-01 21:59:26 +0000129{
aliguorif141eaf2009-04-07 18:43:24 +0000130 if (!bdrv->bdrv_aio_readv) {
bellard83f64092006-08-01 16:21:11 +0000131 /* add AIO emulation layer */
aliguorif141eaf2009-04-07 18:43:24 +0000132 bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
133 bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
aliguorieda578e2009-03-12 19:57:16 +0000134 } else if (!bdrv->bdrv_read) {
bellard83f64092006-08-01 16:21:11 +0000135 /* add synchronous IO emulation layer */
136 bdrv->bdrv_read = bdrv_read_em;
137 bdrv->bdrv_write = bdrv_write_em;
138 }
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +0200139
140 if (!bdrv->bdrv_aio_flush)
141 bdrv->bdrv_aio_flush = bdrv_aio_flush_em;
142
bellardea2384d2004-08-01 21:59:26 +0000143 bdrv->next = first_drv;
144 first_drv = bdrv;
145}
bellardb3380822004-03-14 21:38:54 +0000146
147/* create a new block device (by default it is empty) */
148BlockDriverState *bdrv_new(const char *device_name)
bellardfc01f7e2003-06-30 10:03:06 +0000149{
bellardb3380822004-03-14 21:38:54 +0000150 BlockDriverState **pbs, *bs;
151
152 bs = qemu_mallocz(sizeof(BlockDriverState));
bellardb3380822004-03-14 21:38:54 +0000153 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
bellardea2384d2004-08-01 21:59:26 +0000154 if (device_name[0] != '\0') {
155 /* insert at the end */
156 pbs = &bdrv_first;
157 while (*pbs != NULL)
158 pbs = &(*pbs)->next;
159 *pbs = bs;
160 }
bellardb3380822004-03-14 21:38:54 +0000161 return bs;
162}
163
bellardea2384d2004-08-01 21:59:26 +0000164BlockDriver *bdrv_find_format(const char *format_name)
165{
166 BlockDriver *drv1;
167 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
168 if (!strcmp(drv1->format_name, format_name))
169 return drv1;
170 }
171 return NULL;
172}
173
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200174int bdrv_create(BlockDriver *drv, const char* filename,
175 QEMUOptionParameter *options)
bellardea2384d2004-08-01 21:59:26 +0000176{
177 if (!drv->bdrv_create)
178 return -ENOTSUP;
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200179
180 return drv->bdrv_create(filename, options);
bellardea2384d2004-08-01 21:59:26 +0000181}
182
bellardd5249392004-08-03 21:14:23 +0000183#ifdef _WIN32
bellard95389c82005-12-18 18:28:15 +0000184void get_tmp_filename(char *filename, int size)
bellardd5249392004-08-03 21:14:23 +0000185{
bellard3b9f94e2007-01-07 17:27:07 +0000186 char temp_dir[MAX_PATH];
ths3b46e622007-09-17 08:09:54 +0000187
bellard3b9f94e2007-01-07 17:27:07 +0000188 GetTempPath(MAX_PATH, temp_dir);
189 GetTempFileName(temp_dir, "qem", 0, filename);
bellardd5249392004-08-03 21:14:23 +0000190}
191#else
bellard95389c82005-12-18 18:28:15 +0000192void get_tmp_filename(char *filename, int size)
bellardea2384d2004-08-01 21:59:26 +0000193{
194 int fd;
blueswir17ccfb2e2008-09-14 06:45:34 +0000195 const char *tmpdir;
bellardd5249392004-08-03 21:14:23 +0000196 /* XXX: race condition possible */
aurel320badc1e2008-03-10 00:05:34 +0000197 tmpdir = getenv("TMPDIR");
198 if (!tmpdir)
199 tmpdir = "/tmp";
200 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
bellardea2384d2004-08-01 21:59:26 +0000201 fd = mkstemp(filename);
202 close(fd);
203}
bellardd5249392004-08-03 21:14:23 +0000204#endif
bellardea2384d2004-08-01 21:59:26 +0000205
bellard19cb3732006-08-19 11:45:59 +0000206#ifdef _WIN32
bellardf45512f2006-08-23 21:40:13 +0000207static int is_windows_drive_prefix(const char *filename)
208{
209 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
210 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
211 filename[1] == ':');
212}
ths3b46e622007-09-17 08:09:54 +0000213
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200214int is_windows_drive(const char *filename)
bellard19cb3732006-08-19 11:45:59 +0000215{
ths5fafdf22007-09-16 21:08:06 +0000216 if (is_windows_drive_prefix(filename) &&
bellardf45512f2006-08-23 21:40:13 +0000217 filename[2] == '\0')
bellard19cb3732006-08-19 11:45:59 +0000218 return 1;
219 if (strstart(filename, "\\\\.\\", NULL) ||
220 strstart(filename, "//./", NULL))
221 return 1;
222 return 0;
223}
224#endif
225
bellard83f64092006-08-01 16:21:11 +0000226static BlockDriver *find_protocol(const char *filename)
227{
228 BlockDriver *drv1;
229 char protocol[128];
Anthony Liguori1cec71e2009-07-02 08:12:26 -0500230 int len;
bellard83f64092006-08-01 16:21:11 +0000231 const char *p;
bellard19cb3732006-08-19 11:45:59 +0000232
233#ifdef _WIN32
bellardf45512f2006-08-23 21:40:13 +0000234 if (is_windows_drive(filename) ||
235 is_windows_drive_prefix(filename))
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500236 return bdrv_find_format("raw");
bellard19cb3732006-08-19 11:45:59 +0000237#endif
Anthony Liguori1cec71e2009-07-02 08:12:26 -0500238 p = strchr(filename, ':');
239 if (!p)
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500240 return bdrv_find_format("raw");
Anthony Liguori1cec71e2009-07-02 08:12:26 -0500241 len = p - filename;
242 if (len > sizeof(protocol) - 1)
243 len = sizeof(protocol) - 1;
244 memcpy(protocol, filename, len);
245 protocol[len] = '\0';
bellard83f64092006-08-01 16:21:11 +0000246 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
ths5fafdf22007-09-16 21:08:06 +0000247 if (drv1->protocol_name &&
bellard83f64092006-08-01 16:21:11 +0000248 !strcmp(drv1->protocol_name, protocol))
249 return drv1;
250 }
251 return NULL;
252}
253
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200254/*
255 * Detect host devices. By convention, /dev/cdrom[N] is always
256 * recognized as a host CDROM.
257 */
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200258static BlockDriver *find_hdev_driver(const char *filename)
259{
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200260 int score_max = 0, score;
261 BlockDriver *drv = NULL, *d;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200262
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200263 for (d = first_drv; d; d = d->next) {
264 if (d->bdrv_probe_device) {
265 score = d->bdrv_probe_device(filename);
266 if (score > score_max) {
267 score_max = score;
268 drv = d;
269 }
270 }
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200271 }
272
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200273 return drv;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200274}
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200275
bellardea2384d2004-08-01 21:59:26 +0000276static BlockDriver *find_image_format(const char *filename)
277{
bellard83f64092006-08-01 16:21:11 +0000278 int ret, score, score_max;
bellardea2384d2004-08-01 21:59:26 +0000279 BlockDriver *drv1, *drv;
bellard83f64092006-08-01 16:21:11 +0000280 uint8_t buf[2048];
281 BlockDriverState *bs;
ths3b46e622007-09-17 08:09:54 +0000282
bellard83f64092006-08-01 16:21:11 +0000283 drv = find_protocol(filename);
bellard19cb3732006-08-19 11:45:59 +0000284 /* no need to test disk image formats for vvfat */
Anthony Liguoric833ab72009-05-22 08:17:55 -0500285 if (drv && strcmp(drv->format_name, "vvfat") == 0)
bellard83f64092006-08-01 16:21:11 +0000286 return drv;
bellard19cb3732006-08-19 11:45:59 +0000287
bellard83f64092006-08-01 16:21:11 +0000288 ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
289 if (ret < 0)
290 return NULL;
291 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
292 bdrv_delete(bs);
293 if (ret < 0) {
294 return NULL;
295 }
296
bellardea2384d2004-08-01 21:59:26 +0000297 score_max = 0;
298 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
bellard83f64092006-08-01 16:21:11 +0000299 if (drv1->bdrv_probe) {
300 score = drv1->bdrv_probe(buf, ret, filename);
301 if (score > score_max) {
302 score_max = score;
303 drv = drv1;
304 }
bellardea2384d2004-08-01 21:59:26 +0000305 }
306 }
307 return drv;
308}
309
bellard83f64092006-08-01 16:21:11 +0000310int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
bellardb3380822004-03-14 21:38:54 +0000311{
bellard83f64092006-08-01 16:21:11 +0000312 BlockDriverState *bs;
313 int ret;
314
315 bs = bdrv_new("");
bellard83f64092006-08-01 16:21:11 +0000316 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
317 if (ret < 0) {
318 bdrv_delete(bs);
319 return ret;
bellard3b0d4f62005-10-30 18:30:10 +0000320 }
aliguori71d07702009-03-03 17:37:16 +0000321 bs->growable = 1;
bellard83f64092006-08-01 16:21:11 +0000322 *pbs = bs;
323 return 0;
bellardea2384d2004-08-01 21:59:26 +0000324}
bellardfc01f7e2003-06-30 10:03:06 +0000325
bellard83f64092006-08-01 16:21:11 +0000326int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
327{
328 return bdrv_open2(bs, filename, flags, NULL);
329}
330
331int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
bellardea2384d2004-08-01 21:59:26 +0000332 BlockDriver *drv)
333{
Naphtali Sprei59f26892009-10-26 16:25:16 +0200334 int ret, open_flags, try_rw;
thseb5c8512007-02-11 15:06:09 +0000335 char tmp_filename[PATH_MAX];
336 char backing_filename[PATH_MAX];
ths3b46e622007-09-17 08:09:54 +0000337
bellardea2384d2004-08-01 21:59:26 +0000338 bs->is_temporary = 0;
339 bs->encrypted = 0;
aliguoric0f4ce72009-03-05 23:01:01 +0000340 bs->valid_key = 0;
aliguorie268ca52009-04-22 20:20:00 +0000341 /* buffer_alignment defaulted to 512, drivers can change this value */
342 bs->buffer_alignment = 512;
bellard712e7872005-04-28 21:09:32 +0000343
bellard83f64092006-08-01 16:21:11 +0000344 if (flags & BDRV_O_SNAPSHOT) {
bellardea2384d2004-08-01 21:59:26 +0000345 BlockDriverState *bs1;
346 int64_t total_size;
aliguori7c96d462008-09-12 17:54:13 +0000347 int is_protocol = 0;
Kevin Wolf91a073a2009-05-27 14:48:06 +0200348 BlockDriver *bdrv_qcow2;
349 QEMUOptionParameter *options;
ths3b46e622007-09-17 08:09:54 +0000350
bellardea2384d2004-08-01 21:59:26 +0000351 /* if snapshot, we create a temporary backing file and open it
352 instead of opening 'filename' directly */
353
354 /* if there is a backing file, use it */
355 bs1 = bdrv_new("");
aliguori5eb45632009-03-28 17:55:10 +0000356 ret = bdrv_open2(bs1, filename, 0, drv);
aliguori51d7c002009-03-05 23:00:29 +0000357 if (ret < 0) {
bellardea2384d2004-08-01 21:59:26 +0000358 bdrv_delete(bs1);
aliguori51d7c002009-03-05 23:00:29 +0000359 return ret;
bellardea2384d2004-08-01 21:59:26 +0000360 }
bellard83f64092006-08-01 16:21:11 +0000361 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
aliguori7c96d462008-09-12 17:54:13 +0000362
363 if (bs1->drv && bs1->drv->protocol_name)
364 is_protocol = 1;
365
bellardea2384d2004-08-01 21:59:26 +0000366 bdrv_delete(bs1);
ths3b46e622007-09-17 08:09:54 +0000367
bellardea2384d2004-08-01 21:59:26 +0000368 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
aliguori7c96d462008-09-12 17:54:13 +0000369
370 /* Real path is meaningless for protocols */
371 if (is_protocol)
372 snprintf(backing_filename, sizeof(backing_filename),
373 "%s", filename);
374 else
375 realpath(filename, backing_filename);
376
Kevin Wolf91a073a2009-05-27 14:48:06 +0200377 bdrv_qcow2 = bdrv_find_format("qcow2");
378 options = parse_option_parameters("", bdrv_qcow2->create_options, NULL);
379
380 set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size * 512);
381 set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename);
382 if (drv) {
383 set_option_parameter(options, BLOCK_OPT_BACKING_FMT,
384 drv->format_name);
385 }
386
387 ret = bdrv_create(bdrv_qcow2, tmp_filename, options);
aliguori51d7c002009-03-05 23:00:29 +0000388 if (ret < 0) {
389 return ret;
bellardea2384d2004-08-01 21:59:26 +0000390 }
Kevin Wolf91a073a2009-05-27 14:48:06 +0200391
bellardea2384d2004-08-01 21:59:26 +0000392 filename = tmp_filename;
Kevin Wolf91a073a2009-05-27 14:48:06 +0200393 drv = bdrv_qcow2;
bellardea2384d2004-08-01 21:59:26 +0000394 bs->is_temporary = 1;
395 }
bellard712e7872005-04-28 21:09:32 +0000396
bellardea2384d2004-08-01 21:59:26 +0000397 pstrcpy(bs->filename, sizeof(bs->filename), filename);
bellard83f64092006-08-01 16:21:11 +0000398 if (flags & BDRV_O_FILE) {
399 drv = find_protocol(filename);
aliguori51d7c002009-03-05 23:00:29 +0000400 } else if (!drv) {
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200401 drv = find_hdev_driver(filename);
402 if (!drv) {
403 drv = find_image_format(filename);
404 }
aliguori51d7c002009-03-05 23:00:29 +0000405 }
406 if (!drv) {
407 ret = -ENOENT;
408 goto unlink_and_fail;
bellardea2384d2004-08-01 21:59:26 +0000409 }
410 bs->drv = drv;
411 bs->opaque = qemu_mallocz(drv->instance_size);
Christoph Hellwige900a7b2009-09-04 19:01:15 +0200412
413 /*
414 * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a
415 * write cache to the guest. We do need the fdatasync to flush
416 * out transactions for block allocations, and we maybe have a
417 * volatile write cache in our backing device to deal with.
418 */
419 if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE))
420 bs->enable_write_cache = 1;
421
bellard83f64092006-08-01 16:21:11 +0000422 /* Note: for compatibility, we open disk image files as RDWR, and
423 RDONLY as fallback */
Naphtali Sprei59f26892009-10-26 16:25:16 +0200424 try_rw = !bs->read_only || bs->is_temporary;
bellard83f64092006-08-01 16:21:11 +0000425 if (!(flags & BDRV_O_FILE))
Naphtali Sprei59f26892009-10-26 16:25:16 +0200426 open_flags = (try_rw ? BDRV_O_RDWR : 0) |
427 (flags & (BDRV_O_CACHE_MASK|BDRV_O_NATIVE_AIO));
bellard83f64092006-08-01 16:21:11 +0000428 else
429 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
Anthony Liguori1cec71e2009-07-02 08:12:26 -0500430 ret = drv->bdrv_open(bs, filename, open_flags);
aurel32a0a83532008-10-13 21:08:34 +0000431 if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
Anthony Liguori1cec71e2009-07-02 08:12:26 -0500432 ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
bellard83f64092006-08-01 16:21:11 +0000433 bs->read_only = 1;
434 }
bellardea2384d2004-08-01 21:59:26 +0000435 if (ret < 0) {
436 qemu_free(bs->opaque);
bellard6b21b972006-08-23 21:14:37 +0000437 bs->opaque = NULL;
438 bs->drv = NULL;
aliguori51d7c002009-03-05 23:00:29 +0000439 unlink_and_fail:
440 if (bs->is_temporary)
441 unlink(filename);
bellard83f64092006-08-01 16:21:11 +0000442 return ret;
bellardea2384d2004-08-01 21:59:26 +0000443 }
bellardd15a7712006-08-06 13:35:09 +0000444 if (drv->bdrv_getlength) {
445 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
446 }
bellardea2384d2004-08-01 21:59:26 +0000447#ifndef _WIN32
448 if (bs->is_temporary) {
449 unlink(filename);
450 }
451#endif
bellard83f64092006-08-01 16:21:11 +0000452 if (bs->backing_file[0] != '\0') {
bellardea2384d2004-08-01 21:59:26 +0000453 /* if there is a backing file, use it */
aliguori5eb45632009-03-28 17:55:10 +0000454 BlockDriver *back_drv = NULL;
bellardea2384d2004-08-01 21:59:26 +0000455 bs->backing_hd = bdrv_new("");
Naphtali Sprei59f26892009-10-26 16:25:16 +0200456 /* pass on read_only property to the backing_hd */
457 bs->backing_hd->read_only = bs->read_only;
bellard83f64092006-08-01 16:21:11 +0000458 path_combine(backing_filename, sizeof(backing_filename),
459 filename, bs->backing_file);
aliguori5eb45632009-03-28 17:55:10 +0000460 if (bs->backing_format[0] != '\0')
461 back_drv = bdrv_find_format(bs->backing_format);
462 ret = bdrv_open2(bs->backing_hd, backing_filename, open_flags,
463 back_drv);
aliguori51d7c002009-03-05 23:00:29 +0000464 if (ret < 0) {
465 bdrv_close(bs);
466 return ret;
467 }
bellardea2384d2004-08-01 21:59:26 +0000468 }
469
aliguoribb5fc202009-03-05 23:01:15 +0000470 if (!bdrv_key_required(bs)) {
471 /* call the change callback */
472 bs->media_changed = 1;
473 if (bs->change_cb)
474 bs->change_cb(bs->change_opaque);
475 }
bellardb3380822004-03-14 21:38:54 +0000476 return 0;
bellardfc01f7e2003-06-30 10:03:06 +0000477}
478
479void bdrv_close(BlockDriverState *bs)
480{
bellard19cb3732006-08-19 11:45:59 +0000481 if (bs->drv) {
bellardea2384d2004-08-01 21:59:26 +0000482 if (bs->backing_hd)
483 bdrv_delete(bs->backing_hd);
484 bs->drv->bdrv_close(bs);
485 qemu_free(bs->opaque);
486#ifdef _WIN32
487 if (bs->is_temporary) {
488 unlink(bs->filename);
489 }
bellard67b915a2004-03-31 23:37:16 +0000490#endif
bellardea2384d2004-08-01 21:59:26 +0000491 bs->opaque = NULL;
492 bs->drv = NULL;
bellardb3380822004-03-14 21:38:54 +0000493
494 /* call the change callback */
bellard19cb3732006-08-19 11:45:59 +0000495 bs->media_changed = 1;
bellardb3380822004-03-14 21:38:54 +0000496 if (bs->change_cb)
497 bs->change_cb(bs->change_opaque);
498 }
499}
500
501void bdrv_delete(BlockDriverState *bs)
502{
aurel3234c6f052008-04-08 19:51:21 +0000503 BlockDriverState **pbs;
504
505 pbs = &bdrv_first;
506 while (*pbs != bs && *pbs != NULL)
507 pbs = &(*pbs)->next;
508 if (*pbs == bs)
509 *pbs = bs->next;
510
bellardb3380822004-03-14 21:38:54 +0000511 bdrv_close(bs);
512 qemu_free(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000513}
514
aliguorie97fc192009-04-21 23:11:50 +0000515/*
516 * Run consistency checks on an image
517 *
518 * Returns the number of errors or -errno when an internal error occurs
519 */
520int bdrv_check(BlockDriverState *bs)
521{
522 if (bs->drv->bdrv_check == NULL) {
523 return -ENOTSUP;
524 }
525
526 return bs->drv->bdrv_check(bs);
527}
528
bellard33e39632003-07-06 17:15:21 +0000529/* commit COW file into the raw image */
530int bdrv_commit(BlockDriverState *bs)
531{
bellard19cb3732006-08-19 11:45:59 +0000532 BlockDriver *drv = bs->drv;
bellard83f64092006-08-01 16:21:11 +0000533 int64_t i, total_sectors;
bellardea2384d2004-08-01 21:59:26 +0000534 int n, j;
535 unsigned char sector[512];
bellard33e39632003-07-06 17:15:21 +0000536
bellard19cb3732006-08-19 11:45:59 +0000537 if (!drv)
538 return -ENOMEDIUM;
bellard33e39632003-07-06 17:15:21 +0000539
540 if (bs->read_only) {
bellardea2384d2004-08-01 21:59:26 +0000541 return -EACCES;
bellard33e39632003-07-06 17:15:21 +0000542 }
543
bellardea2384d2004-08-01 21:59:26 +0000544 if (!bs->backing_hd) {
545 return -ENOTSUP;
bellard33e39632003-07-06 17:15:21 +0000546 }
bellardea2384d2004-08-01 21:59:26 +0000547
bellard83f64092006-08-01 16:21:11 +0000548 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
549 for (i = 0; i < total_sectors;) {
bellard19cb3732006-08-19 11:45:59 +0000550 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
bellardea2384d2004-08-01 21:59:26 +0000551 for(j = 0; j < n; j++) {
552 if (bdrv_read(bs, i, sector, 1) != 0) {
553 return -EIO;
554 }
555
556 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
557 return -EIO;
558 }
559 i++;
560 }
561 } else {
562 i += n;
563 }
564 }
bellard95389c82005-12-18 18:28:15 +0000565
bellard19cb3732006-08-19 11:45:59 +0000566 if (drv->bdrv_make_empty)
567 return drv->bdrv_make_empty(bs);
bellard95389c82005-12-18 18:28:15 +0000568
bellard33e39632003-07-06 17:15:21 +0000569 return 0;
570}
571
aliguori71d07702009-03-03 17:37:16 +0000572static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
573 size_t size)
574{
575 int64_t len;
576
577 if (!bdrv_is_inserted(bs))
578 return -ENOMEDIUM;
579
580 if (bs->growable)
581 return 0;
582
583 len = bdrv_getlength(bs);
584
Kevin Wolffbb7b4e2009-05-08 14:47:24 +0200585 if (offset < 0)
586 return -EIO;
587
588 if ((offset > len) || (len - offset < size))
aliguori71d07702009-03-03 17:37:16 +0000589 return -EIO;
590
591 return 0;
592}
593
594static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
595 int nb_sectors)
596{
aliguori999dec52009-03-29 01:31:48 +0000597 return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512);
aliguori71d07702009-03-03 17:37:16 +0000598}
599
bellard19cb3732006-08-19 11:45:59 +0000600/* return < 0 if error. See bdrv_write() for the return codes */
ths5fafdf22007-09-16 21:08:06 +0000601int bdrv_read(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000602 uint8_t *buf, int nb_sectors)
603{
bellardea2384d2004-08-01 21:59:26 +0000604 BlockDriver *drv = bs->drv;
605
bellard19cb3732006-08-19 11:45:59 +0000606 if (!drv)
607 return -ENOMEDIUM;
aliguori71d07702009-03-03 17:37:16 +0000608 if (bdrv_check_request(bs, sector_num, nb_sectors))
609 return -EIO;
bellardb3380822004-03-14 21:38:54 +0000610
aliguorieda578e2009-03-12 19:57:16 +0000611 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
bellardfc01f7e2003-06-30 10:03:06 +0000612}
613
ths5fafdf22007-09-16 21:08:06 +0000614/* Return < 0 if error. Important errors are:
bellard19cb3732006-08-19 11:45:59 +0000615 -EIO generic I/O error (may happen for all errors)
616 -ENOMEDIUM No media inserted.
617 -EINVAL Invalid sector number or nb_sectors
618 -EACCES Trying to write a read-only device
619*/
ths5fafdf22007-09-16 21:08:06 +0000620int bdrv_write(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000621 const uint8_t *buf, int nb_sectors)
622{
bellard83f64092006-08-01 16:21:11 +0000623 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +0000624 if (!bs->drv)
625 return -ENOMEDIUM;
bellard0849bf02003-06-30 23:17:31 +0000626 if (bs->read_only)
bellard19cb3732006-08-19 11:45:59 +0000627 return -EACCES;
aliguori71d07702009-03-03 17:37:16 +0000628 if (bdrv_check_request(bs, sector_num, nb_sectors))
629 return -EIO;
630
aliguori42fb2802009-01-15 20:43:39 +0000631 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
bellard83f64092006-08-01 16:21:11 +0000632}
633
aliguorieda578e2009-03-12 19:57:16 +0000634int bdrv_pread(BlockDriverState *bs, int64_t offset,
635 void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000636{
bellard83f64092006-08-01 16:21:11 +0000637 uint8_t tmp_buf[SECTOR_SIZE];
638 int len, nb_sectors, count;
639 int64_t sector_num;
640
641 count = count1;
642 /* first read to align to sector start */
643 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
644 if (len > count)
645 len = count;
646 sector_num = offset >> SECTOR_BITS;
647 if (len > 0) {
648 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
649 return -EIO;
650 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
651 count -= len;
652 if (count == 0)
653 return count1;
654 sector_num++;
655 buf += len;
656 }
657
658 /* read the sectors "in place" */
659 nb_sectors = count >> SECTOR_BITS;
660 if (nb_sectors > 0) {
661 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
662 return -EIO;
663 sector_num += nb_sectors;
664 len = nb_sectors << SECTOR_BITS;
665 buf += len;
666 count -= len;
667 }
668
669 /* add data from the last sector */
670 if (count > 0) {
671 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
672 return -EIO;
673 memcpy(buf, tmp_buf, count);
674 }
675 return count1;
676}
677
aliguorieda578e2009-03-12 19:57:16 +0000678int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
679 const void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000680{
bellard83f64092006-08-01 16:21:11 +0000681 uint8_t tmp_buf[SECTOR_SIZE];
682 int len, nb_sectors, count;
683 int64_t sector_num;
684
685 count = count1;
686 /* first write to align to sector start */
687 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
688 if (len > count)
689 len = count;
690 sector_num = offset >> SECTOR_BITS;
691 if (len > 0) {
692 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
693 return -EIO;
694 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
695 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
696 return -EIO;
697 count -= len;
698 if (count == 0)
699 return count1;
700 sector_num++;
701 buf += len;
702 }
703
704 /* write the sectors "in place" */
705 nb_sectors = count >> SECTOR_BITS;
706 if (nb_sectors > 0) {
707 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
708 return -EIO;
709 sector_num += nb_sectors;
710 len = nb_sectors << SECTOR_BITS;
711 buf += len;
712 count -= len;
713 }
714
715 /* add data from the last sector */
716 if (count > 0) {
717 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
718 return -EIO;
719 memcpy(tmp_buf, buf, count);
720 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
721 return -EIO;
722 }
723 return count1;
724}
bellard83f64092006-08-01 16:21:11 +0000725
726/**
bellard83f64092006-08-01 16:21:11 +0000727 * Truncate file to 'offset' bytes (needed only for file protocols)
728 */
729int bdrv_truncate(BlockDriverState *bs, int64_t offset)
730{
731 BlockDriver *drv = bs->drv;
732 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000733 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000734 if (!drv->bdrv_truncate)
735 return -ENOTSUP;
Naphtali Sprei59f26892009-10-26 16:25:16 +0200736 if (bs->read_only)
737 return -EACCES;
bellard83f64092006-08-01 16:21:11 +0000738 return drv->bdrv_truncate(bs, offset);
739}
740
741/**
742 * Length of a file in bytes. Return < 0 if error or unknown.
743 */
744int64_t bdrv_getlength(BlockDriverState *bs)
745{
746 BlockDriver *drv = bs->drv;
747 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000748 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000749 if (!drv->bdrv_getlength) {
750 /* legacy mode */
751 return bs->total_sectors * SECTOR_SIZE;
752 }
753 return drv->bdrv_getlength(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000754}
755
bellard19cb3732006-08-19 11:45:59 +0000756/* return 0 as number of sectors if no device present or error */
ths96b8f132007-12-17 01:35:20 +0000757void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
bellardfc01f7e2003-06-30 10:03:06 +0000758{
bellard19cb3732006-08-19 11:45:59 +0000759 int64_t length;
760 length = bdrv_getlength(bs);
761 if (length < 0)
762 length = 0;
763 else
764 length = length >> SECTOR_BITS;
765 *nb_sectors_ptr = length;
bellardfc01f7e2003-06-30 10:03:06 +0000766}
bellardcf989512004-02-16 21:56:36 +0000767
aliguorif3d54fc2008-11-25 21:50:24 +0000768struct partition {
769 uint8_t boot_ind; /* 0x80 - active */
770 uint8_t head; /* starting head */
771 uint8_t sector; /* starting sector */
772 uint8_t cyl; /* starting cylinder */
773 uint8_t sys_ind; /* What partition type */
774 uint8_t end_head; /* end head */
775 uint8_t end_sector; /* end sector */
776 uint8_t end_cyl; /* end cylinder */
777 uint32_t start_sect; /* starting sector counting from 0 */
778 uint32_t nr_sects; /* nr of sectors in partition */
779} __attribute__((packed));
780
781/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
782static int guess_disk_lchs(BlockDriverState *bs,
783 int *pcylinders, int *pheads, int *psectors)
784{
785 uint8_t buf[512];
786 int ret, i, heads, sectors, cylinders;
787 struct partition *p;
788 uint32_t nr_sects;
blueswir1a38131b2008-12-05 17:56:40 +0000789 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +0000790
791 bdrv_get_geometry(bs, &nb_sectors);
792
793 ret = bdrv_read(bs, 0, buf, 1);
794 if (ret < 0)
795 return -1;
796 /* test msdos magic */
797 if (buf[510] != 0x55 || buf[511] != 0xaa)
798 return -1;
799 for(i = 0; i < 4; i++) {
800 p = ((struct partition *)(buf + 0x1be)) + i;
801 nr_sects = le32_to_cpu(p->nr_sects);
802 if (nr_sects && p->end_head) {
803 /* We make the assumption that the partition terminates on
804 a cylinder boundary */
805 heads = p->end_head + 1;
806 sectors = p->end_sector & 63;
807 if (sectors == 0)
808 continue;
809 cylinders = nb_sectors / (heads * sectors);
810 if (cylinders < 1 || cylinders > 16383)
811 continue;
812 *pheads = heads;
813 *psectors = sectors;
814 *pcylinders = cylinders;
815#if 0
816 printf("guessed geometry: LCHS=%d %d %d\n",
817 cylinders, heads, sectors);
818#endif
819 return 0;
820 }
821 }
822 return -1;
823}
824
825void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
826{
827 int translation, lba_detected = 0;
828 int cylinders, heads, secs;
blueswir1a38131b2008-12-05 17:56:40 +0000829 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +0000830
831 /* if a geometry hint is available, use it */
832 bdrv_get_geometry(bs, &nb_sectors);
833 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
834 translation = bdrv_get_translation_hint(bs);
835 if (cylinders != 0) {
836 *pcyls = cylinders;
837 *pheads = heads;
838 *psecs = secs;
839 } else {
840 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
841 if (heads > 16) {
842 /* if heads > 16, it means that a BIOS LBA
843 translation was active, so the default
844 hardware geometry is OK */
845 lba_detected = 1;
846 goto default_geometry;
847 } else {
848 *pcyls = cylinders;
849 *pheads = heads;
850 *psecs = secs;
851 /* disable any translation to be in sync with
852 the logical geometry */
853 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
854 bdrv_set_translation_hint(bs,
855 BIOS_ATA_TRANSLATION_NONE);
856 }
857 }
858 } else {
859 default_geometry:
860 /* if no geometry, use a standard physical disk geometry */
861 cylinders = nb_sectors / (16 * 63);
862
863 if (cylinders > 16383)
864 cylinders = 16383;
865 else if (cylinders < 2)
866 cylinders = 2;
867 *pcyls = cylinders;
868 *pheads = 16;
869 *psecs = 63;
870 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
871 if ((*pcyls * *pheads) <= 131072) {
872 bdrv_set_translation_hint(bs,
873 BIOS_ATA_TRANSLATION_LARGE);
874 } else {
875 bdrv_set_translation_hint(bs,
876 BIOS_ATA_TRANSLATION_LBA);
877 }
878 }
879 }
880 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
881 }
882}
883
ths5fafdf22007-09-16 21:08:06 +0000884void bdrv_set_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000885 int cyls, int heads, int secs)
886{
887 bs->cyls = cyls;
888 bs->heads = heads;
889 bs->secs = secs;
890}
891
892void bdrv_set_type_hint(BlockDriverState *bs, int type)
893{
894 bs->type = type;
895 bs->removable = ((type == BDRV_TYPE_CDROM ||
896 type == BDRV_TYPE_FLOPPY));
897}
898
bellard46d47672004-11-16 01:45:27 +0000899void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
900{
901 bs->translation = translation;
902}
903
ths5fafdf22007-09-16 21:08:06 +0000904void bdrv_get_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000905 int *pcyls, int *pheads, int *psecs)
906{
907 *pcyls = bs->cyls;
908 *pheads = bs->heads;
909 *psecs = bs->secs;
910}
911
912int bdrv_get_type_hint(BlockDriverState *bs)
913{
914 return bs->type;
915}
916
bellard46d47672004-11-16 01:45:27 +0000917int bdrv_get_translation_hint(BlockDriverState *bs)
918{
919 return bs->translation;
920}
921
bellardb3380822004-03-14 21:38:54 +0000922int bdrv_is_removable(BlockDriverState *bs)
923{
924 return bs->removable;
925}
926
927int bdrv_is_read_only(BlockDriverState *bs)
928{
929 return bs->read_only;
930}
931
Naphtali Sprei59f26892009-10-26 16:25:16 +0200932int bdrv_set_read_only(BlockDriverState *bs, int read_only)
933{
934 int ret = bs->read_only;
935 bs->read_only = read_only;
936 return ret;
937}
938
ths985a03b2007-12-24 16:10:43 +0000939int bdrv_is_sg(BlockDriverState *bs)
940{
941 return bs->sg;
942}
943
Christoph Hellwige900a7b2009-09-04 19:01:15 +0200944int bdrv_enable_write_cache(BlockDriverState *bs)
945{
946 return bs->enable_write_cache;
947}
948
bellard19cb3732006-08-19 11:45:59 +0000949/* XXX: no longer used */
ths5fafdf22007-09-16 21:08:06 +0000950void bdrv_set_change_cb(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000951 void (*change_cb)(void *opaque), void *opaque)
952{
953 bs->change_cb = change_cb;
954 bs->change_opaque = opaque;
955}
956
bellardea2384d2004-08-01 21:59:26 +0000957int bdrv_is_encrypted(BlockDriverState *bs)
958{
959 if (bs->backing_hd && bs->backing_hd->encrypted)
960 return 1;
961 return bs->encrypted;
962}
963
aliguoric0f4ce72009-03-05 23:01:01 +0000964int bdrv_key_required(BlockDriverState *bs)
965{
966 BlockDriverState *backing_hd = bs->backing_hd;
967
968 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
969 return 1;
970 return (bs->encrypted && !bs->valid_key);
971}
972
bellardea2384d2004-08-01 21:59:26 +0000973int bdrv_set_key(BlockDriverState *bs, const char *key)
974{
975 int ret;
976 if (bs->backing_hd && bs->backing_hd->encrypted) {
977 ret = bdrv_set_key(bs->backing_hd, key);
978 if (ret < 0)
979 return ret;
980 if (!bs->encrypted)
981 return 0;
982 }
983 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
984 return -1;
aliguoric0f4ce72009-03-05 23:01:01 +0000985 ret = bs->drv->bdrv_set_key(bs, key);
aliguoribb5fc202009-03-05 23:01:15 +0000986 if (ret < 0) {
987 bs->valid_key = 0;
988 } else if (!bs->valid_key) {
989 bs->valid_key = 1;
990 /* call the change callback now, we skipped it on open */
991 bs->media_changed = 1;
992 if (bs->change_cb)
993 bs->change_cb(bs->change_opaque);
994 }
aliguoric0f4ce72009-03-05 23:01:01 +0000995 return ret;
bellardea2384d2004-08-01 21:59:26 +0000996}
997
998void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
999{
bellard19cb3732006-08-19 11:45:59 +00001000 if (!bs->drv) {
bellardea2384d2004-08-01 21:59:26 +00001001 buf[0] = '\0';
1002 } else {
1003 pstrcpy(buf, buf_size, bs->drv->format_name);
1004 }
1005}
1006
ths5fafdf22007-09-16 21:08:06 +00001007void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
bellardea2384d2004-08-01 21:59:26 +00001008 void *opaque)
1009{
1010 BlockDriver *drv;
1011
1012 for (drv = first_drv; drv != NULL; drv = drv->next) {
1013 it(opaque, drv->format_name);
1014 }
1015}
1016
bellardb3380822004-03-14 21:38:54 +00001017BlockDriverState *bdrv_find(const char *name)
1018{
1019 BlockDriverState *bs;
1020
1021 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1022 if (!strcmp(name, bs->device_name))
1023 return bs;
1024 }
1025 return NULL;
1026}
1027
aliguori51de9762009-03-05 23:00:43 +00001028void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
bellard81d09122004-07-14 17:21:37 +00001029{
1030 BlockDriverState *bs;
1031
1032 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori51de9762009-03-05 23:00:43 +00001033 it(opaque, bs);
bellard81d09122004-07-14 17:21:37 +00001034 }
1035}
1036
bellardea2384d2004-08-01 21:59:26 +00001037const char *bdrv_get_device_name(BlockDriverState *bs)
1038{
1039 return bs->device_name;
1040}
1041
pbrook7a6cba62006-06-04 11:39:07 +00001042void bdrv_flush(BlockDriverState *bs)
1043{
aliguori081501d2009-03-29 01:31:51 +00001044 if (!bs->drv)
1045 return;
pbrook7a6cba62006-06-04 11:39:07 +00001046 if (bs->drv->bdrv_flush)
1047 bs->drv->bdrv_flush(bs);
1048 if (bs->backing_hd)
1049 bdrv_flush(bs->backing_hd);
1050}
1051
aliguoric6ca28d2008-10-06 13:55:43 +00001052void bdrv_flush_all(void)
1053{
1054 BlockDriverState *bs;
1055
1056 for (bs = bdrv_first; bs != NULL; bs = bs->next)
1057 if (bs->drv && !bdrv_is_read_only(bs) &&
1058 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1059 bdrv_flush(bs);
1060}
1061
thsf58c7b32008-06-05 21:53:49 +00001062/*
1063 * Returns true iff the specified sector is present in the disk image. Drivers
1064 * not implementing the functionality are assumed to not support backing files,
1065 * hence all their sectors are reported as allocated.
1066 *
1067 * 'pnum' is set to the number of sectors (including and immediately following
1068 * the specified sector) that are known to be in the same
1069 * allocated/unallocated state.
1070 *
1071 * 'nb_sectors' is the max value 'pnum' should be set to.
1072 */
1073int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1074 int *pnum)
1075{
1076 int64_t n;
1077 if (!bs->drv->bdrv_is_allocated) {
1078 if (sector_num >= bs->total_sectors) {
1079 *pnum = 0;
1080 return 0;
1081 }
1082 n = bs->total_sectors - sector_num;
1083 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1084 return 1;
1085 }
1086 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1087}
1088
aliguori376253e2009-03-05 23:01:23 +00001089void bdrv_info(Monitor *mon)
bellardb3380822004-03-14 21:38:54 +00001090{
1091 BlockDriverState *bs;
1092
1093 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori376253e2009-03-05 23:01:23 +00001094 monitor_printf(mon, "%s:", bs->device_name);
1095 monitor_printf(mon, " type=");
bellardb3380822004-03-14 21:38:54 +00001096 switch(bs->type) {
1097 case BDRV_TYPE_HD:
aliguori376253e2009-03-05 23:01:23 +00001098 monitor_printf(mon, "hd");
bellardb3380822004-03-14 21:38:54 +00001099 break;
1100 case BDRV_TYPE_CDROM:
aliguori376253e2009-03-05 23:01:23 +00001101 monitor_printf(mon, "cdrom");
bellardb3380822004-03-14 21:38:54 +00001102 break;
1103 case BDRV_TYPE_FLOPPY:
aliguori376253e2009-03-05 23:01:23 +00001104 monitor_printf(mon, "floppy");
bellardb3380822004-03-14 21:38:54 +00001105 break;
1106 }
aliguori376253e2009-03-05 23:01:23 +00001107 monitor_printf(mon, " removable=%d", bs->removable);
bellardb3380822004-03-14 21:38:54 +00001108 if (bs->removable) {
aliguori376253e2009-03-05 23:01:23 +00001109 monitor_printf(mon, " locked=%d", bs->locked);
bellardb3380822004-03-14 21:38:54 +00001110 }
bellard19cb3732006-08-19 11:45:59 +00001111 if (bs->drv) {
aliguori376253e2009-03-05 23:01:23 +00001112 monitor_printf(mon, " file=");
1113 monitor_print_filename(mon, bs->filename);
thsfef30742006-12-22 14:11:32 +00001114 if (bs->backing_file[0] != '\0') {
aliguori376253e2009-03-05 23:01:23 +00001115 monitor_printf(mon, " backing_file=");
1116 monitor_print_filename(mon, bs->backing_file);
1117 }
1118 monitor_printf(mon, " ro=%d", bs->read_only);
1119 monitor_printf(mon, " drv=%s", bs->drv->format_name);
1120 monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs));
bellardb3380822004-03-14 21:38:54 +00001121 } else {
aliguori376253e2009-03-05 23:01:23 +00001122 monitor_printf(mon, " [not inserted]");
bellardb3380822004-03-14 21:38:54 +00001123 }
aliguori376253e2009-03-05 23:01:23 +00001124 monitor_printf(mon, "\n");
bellardb3380822004-03-14 21:38:54 +00001125 }
1126}
thsa36e69d2007-12-02 05:18:19 +00001127
1128/* The "info blockstats" command. */
aliguori376253e2009-03-05 23:01:23 +00001129void bdrv_info_stats(Monitor *mon)
thsa36e69d2007-12-02 05:18:19 +00001130{
1131 BlockDriverState *bs;
1132
1133 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori376253e2009-03-05 23:01:23 +00001134 monitor_printf(mon, "%s:"
1135 " rd_bytes=%" PRIu64
1136 " wr_bytes=%" PRIu64
1137 " rd_operations=%" PRIu64
1138 " wr_operations=%" PRIu64
aliguoriebf53fc2009-03-11 20:05:29 +00001139 "\n",
aliguori376253e2009-03-05 23:01:23 +00001140 bs->device_name,
1141 bs->rd_bytes, bs->wr_bytes,
1142 bs->rd_ops, bs->wr_ops);
thsa36e69d2007-12-02 05:18:19 +00001143 }
1144}
bellardea2384d2004-08-01 21:59:26 +00001145
aliguori045df332009-03-05 23:00:48 +00001146const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1147{
1148 if (bs->backing_hd && bs->backing_hd->encrypted)
1149 return bs->backing_file;
1150 else if (bs->encrypted)
1151 return bs->filename;
1152 else
1153 return NULL;
1154}
1155
ths5fafdf22007-09-16 21:08:06 +00001156void bdrv_get_backing_filename(BlockDriverState *bs,
bellard83f64092006-08-01 16:21:11 +00001157 char *filename, int filename_size)
bellardea2384d2004-08-01 21:59:26 +00001158{
bellard83f64092006-08-01 16:21:11 +00001159 if (!bs->backing_hd) {
1160 pstrcpy(filename, filename_size, "");
1161 } else {
1162 pstrcpy(filename, filename_size, bs->backing_file);
1163 }
bellardea2384d2004-08-01 21:59:26 +00001164}
1165
ths5fafdf22007-09-16 21:08:06 +00001166int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
bellardfaea38e2006-08-05 21:31:00 +00001167 const uint8_t *buf, int nb_sectors)
1168{
1169 BlockDriver *drv = bs->drv;
1170 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001171 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001172 if (!drv->bdrv_write_compressed)
1173 return -ENOTSUP;
Kevin Wolffbb7b4e2009-05-08 14:47:24 +02001174 if (bdrv_check_request(bs, sector_num, nb_sectors))
1175 return -EIO;
bellardfaea38e2006-08-05 21:31:00 +00001176 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1177}
ths3b46e622007-09-17 08:09:54 +00001178
bellardfaea38e2006-08-05 21:31:00 +00001179int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1180{
1181 BlockDriver *drv = bs->drv;
1182 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001183 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001184 if (!drv->bdrv_get_info)
1185 return -ENOTSUP;
1186 memset(bdi, 0, sizeof(*bdi));
1187 return drv->bdrv_get_info(bs, bdi);
1188}
1189
Christoph Hellwig45566e92009-07-10 23:11:57 +02001190int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
1191 int64_t pos, int size)
aliguori178e08a2009-04-05 19:10:55 +00001192{
1193 BlockDriver *drv = bs->drv;
1194 if (!drv)
1195 return -ENOMEDIUM;
Christoph Hellwig45566e92009-07-10 23:11:57 +02001196 if (!drv->bdrv_save_vmstate)
aliguori178e08a2009-04-05 19:10:55 +00001197 return -ENOTSUP;
Christoph Hellwig45566e92009-07-10 23:11:57 +02001198 return drv->bdrv_save_vmstate(bs, buf, pos, size);
aliguori178e08a2009-04-05 19:10:55 +00001199}
1200
Christoph Hellwig45566e92009-07-10 23:11:57 +02001201int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1202 int64_t pos, int size)
aliguori178e08a2009-04-05 19:10:55 +00001203{
1204 BlockDriver *drv = bs->drv;
1205 if (!drv)
1206 return -ENOMEDIUM;
Christoph Hellwig45566e92009-07-10 23:11:57 +02001207 if (!drv->bdrv_load_vmstate)
aliguori178e08a2009-04-05 19:10:55 +00001208 return -ENOTSUP;
Christoph Hellwig45566e92009-07-10 23:11:57 +02001209 return drv->bdrv_load_vmstate(bs, buf, pos, size);
aliguori178e08a2009-04-05 19:10:55 +00001210}
1211
bellardfaea38e2006-08-05 21:31:00 +00001212/**************************************************************/
1213/* handling of snapshots */
1214
ths5fafdf22007-09-16 21:08:06 +00001215int bdrv_snapshot_create(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001216 QEMUSnapshotInfo *sn_info)
1217{
1218 BlockDriver *drv = bs->drv;
1219 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001220 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001221 if (!drv->bdrv_snapshot_create)
1222 return -ENOTSUP;
1223 return drv->bdrv_snapshot_create(bs, sn_info);
1224}
1225
ths5fafdf22007-09-16 21:08:06 +00001226int bdrv_snapshot_goto(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001227 const char *snapshot_id)
1228{
1229 BlockDriver *drv = bs->drv;
1230 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001231 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001232 if (!drv->bdrv_snapshot_goto)
1233 return -ENOTSUP;
1234 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1235}
1236
1237int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1238{
1239 BlockDriver *drv = bs->drv;
1240 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001241 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001242 if (!drv->bdrv_snapshot_delete)
1243 return -ENOTSUP;
1244 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1245}
1246
ths5fafdf22007-09-16 21:08:06 +00001247int bdrv_snapshot_list(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001248 QEMUSnapshotInfo **psn_info)
1249{
1250 BlockDriver *drv = bs->drv;
1251 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001252 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001253 if (!drv->bdrv_snapshot_list)
1254 return -ENOTSUP;
1255 return drv->bdrv_snapshot_list(bs, psn_info);
1256}
1257
1258#define NB_SUFFIXES 4
1259
1260char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1261{
1262 static const char suffixes[NB_SUFFIXES] = "KMGT";
1263 int64_t base;
1264 int i;
1265
1266 if (size <= 999) {
1267 snprintf(buf, buf_size, "%" PRId64, size);
1268 } else {
1269 base = 1024;
1270 for(i = 0; i < NB_SUFFIXES; i++) {
1271 if (size < (10 * base)) {
ths5fafdf22007-09-16 21:08:06 +00001272 snprintf(buf, buf_size, "%0.1f%c",
bellardfaea38e2006-08-05 21:31:00 +00001273 (double)size / base,
1274 suffixes[i]);
1275 break;
1276 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
ths5fafdf22007-09-16 21:08:06 +00001277 snprintf(buf, buf_size, "%" PRId64 "%c",
bellardfaea38e2006-08-05 21:31:00 +00001278 ((size + (base >> 1)) / base),
1279 suffixes[i]);
1280 break;
1281 }
1282 base = base * 1024;
1283 }
1284 }
1285 return buf;
1286}
1287
1288char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1289{
1290 char buf1[128], date_buf[128], clock_buf[128];
bellard3b9f94e2007-01-07 17:27:07 +00001291#ifdef _WIN32
1292 struct tm *ptm;
1293#else
bellardfaea38e2006-08-05 21:31:00 +00001294 struct tm tm;
bellard3b9f94e2007-01-07 17:27:07 +00001295#endif
bellardfaea38e2006-08-05 21:31:00 +00001296 time_t ti;
1297 int64_t secs;
1298
1299 if (!sn) {
ths5fafdf22007-09-16 21:08:06 +00001300 snprintf(buf, buf_size,
1301 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001302 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1303 } else {
1304 ti = sn->date_sec;
bellard3b9f94e2007-01-07 17:27:07 +00001305#ifdef _WIN32
1306 ptm = localtime(&ti);
1307 strftime(date_buf, sizeof(date_buf),
1308 "%Y-%m-%d %H:%M:%S", ptm);
1309#else
bellardfaea38e2006-08-05 21:31:00 +00001310 localtime_r(&ti, &tm);
1311 strftime(date_buf, sizeof(date_buf),
1312 "%Y-%m-%d %H:%M:%S", &tm);
bellard3b9f94e2007-01-07 17:27:07 +00001313#endif
bellardfaea38e2006-08-05 21:31:00 +00001314 secs = sn->vm_clock_nsec / 1000000000;
1315 snprintf(clock_buf, sizeof(clock_buf),
1316 "%02d:%02d:%02d.%03d",
1317 (int)(secs / 3600),
1318 (int)((secs / 60) % 60),
ths5fafdf22007-09-16 21:08:06 +00001319 (int)(secs % 60),
bellardfaea38e2006-08-05 21:31:00 +00001320 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1321 snprintf(buf, buf_size,
ths5fafdf22007-09-16 21:08:06 +00001322 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001323 sn->id_str, sn->name,
1324 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1325 date_buf,
1326 clock_buf);
1327 }
1328 return buf;
1329}
1330
bellardea2384d2004-08-01 21:59:26 +00001331
bellard83f64092006-08-01 16:21:11 +00001332/**************************************************************/
1333/* async I/Os */
1334
aliguori3b69e4b2009-01-22 16:59:24 +00001335BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
aliguorif141eaf2009-04-07 18:43:24 +00001336 QEMUIOVector *qiov, int nb_sectors,
aliguori3b69e4b2009-01-22 16:59:24 +00001337 BlockDriverCompletionFunc *cb, void *opaque)
1338{
bellard83f64092006-08-01 16:21:11 +00001339 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001340 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001341
bellard19cb3732006-08-19 11:45:59 +00001342 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001343 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001344 if (bdrv_check_request(bs, sector_num, nb_sectors))
1345 return NULL;
ths3b46e622007-09-17 08:09:54 +00001346
aliguorif141eaf2009-04-07 18:43:24 +00001347 ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
1348 cb, opaque);
thsa36e69d2007-12-02 05:18:19 +00001349
1350 if (ret) {
1351 /* Update stats even though technically transfer has not happened. */
1352 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1353 bs->rd_ops ++;
1354 }
1355
1356 return ret;
bellard83f64092006-08-01 16:21:11 +00001357}
1358
aliguorif141eaf2009-04-07 18:43:24 +00001359BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1360 QEMUIOVector *qiov, int nb_sectors,
1361 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001362{
bellard83f64092006-08-01 16:21:11 +00001363 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001364 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001365
bellard19cb3732006-08-19 11:45:59 +00001366 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001367 return NULL;
bellard83f64092006-08-01 16:21:11 +00001368 if (bs->read_only)
pbrookce1a14d2006-08-07 02:38:06 +00001369 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001370 if (bdrv_check_request(bs, sector_num, nb_sectors))
1371 return NULL;
bellard83f64092006-08-01 16:21:11 +00001372
aliguorif141eaf2009-04-07 18:43:24 +00001373 ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
1374 cb, opaque);
thsa36e69d2007-12-02 05:18:19 +00001375
1376 if (ret) {
1377 /* Update stats even though technically transfer has not happened. */
1378 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1379 bs->wr_ops ++;
1380 }
1381
1382 return ret;
bellard83f64092006-08-01 16:21:11 +00001383}
1384
Kevin Wolf40b4f532009-09-09 17:53:37 +02001385
1386typedef struct MultiwriteCB {
1387 int error;
1388 int num_requests;
1389 int num_callbacks;
1390 struct {
1391 BlockDriverCompletionFunc *cb;
1392 void *opaque;
1393 QEMUIOVector *free_qiov;
1394 void *free_buf;
1395 } callbacks[];
1396} MultiwriteCB;
1397
1398static void multiwrite_user_cb(MultiwriteCB *mcb)
1399{
1400 int i;
1401
1402 for (i = 0; i < mcb->num_callbacks; i++) {
1403 mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
1404 qemu_free(mcb->callbacks[i].free_qiov);
1405 qemu_free(mcb->callbacks[i].free_buf);
1406 }
1407}
1408
1409static void multiwrite_cb(void *opaque, int ret)
1410{
1411 MultiwriteCB *mcb = opaque;
1412
1413 if (ret < 0) {
1414 mcb->error = ret;
1415 multiwrite_user_cb(mcb);
1416 }
1417
1418 mcb->num_requests--;
1419 if (mcb->num_requests == 0) {
1420 if (mcb->error == 0) {
1421 multiwrite_user_cb(mcb);
1422 }
1423 qemu_free(mcb);
1424 }
1425}
1426
1427static int multiwrite_req_compare(const void *a, const void *b)
1428{
1429 return (((BlockRequest*) a)->sector - ((BlockRequest*) b)->sector);
1430}
1431
1432/*
1433 * Takes a bunch of requests and tries to merge them. Returns the number of
1434 * requests that remain after merging.
1435 */
1436static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
1437 int num_reqs, MultiwriteCB *mcb)
1438{
1439 int i, outidx;
1440
1441 // Sort requests by start sector
1442 qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
1443
1444 // Check if adjacent requests touch the same clusters. If so, combine them,
1445 // filling up gaps with zero sectors.
1446 outidx = 0;
1447 for (i = 1; i < num_reqs; i++) {
1448 int merge = 0;
1449 int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
1450
1451 // This handles the cases that are valid for all block drivers, namely
1452 // exactly sequential writes and overlapping writes.
1453 if (reqs[i].sector <= oldreq_last) {
1454 merge = 1;
1455 }
1456
1457 // The block driver may decide that it makes sense to combine requests
1458 // even if there is a gap of some sectors between them. In this case,
1459 // the gap is filled with zeros (therefore only applicable for yet
1460 // unused space in format like qcow2).
1461 if (!merge && bs->drv->bdrv_merge_requests) {
1462 merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]);
1463 }
1464
1465 if (merge) {
1466 size_t size;
1467 QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov));
1468 qemu_iovec_init(qiov,
1469 reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
1470
1471 // Add the first request to the merged one. If the requests are
1472 // overlapping, drop the last sectors of the first request.
1473 size = (reqs[i].sector - reqs[outidx].sector) << 9;
1474 qemu_iovec_concat(qiov, reqs[outidx].qiov, size);
1475
1476 // We might need to add some zeros between the two requests
1477 if (reqs[i].sector > oldreq_last) {
1478 size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
1479 uint8_t *buf = qemu_blockalign(bs, zero_bytes);
1480 memset(buf, 0, zero_bytes);
1481 qemu_iovec_add(qiov, buf, zero_bytes);
1482 mcb->callbacks[i].free_buf = buf;
1483 }
1484
1485 // Add the second request
1486 qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size);
1487
1488 reqs[outidx].nb_sectors += reqs[i].nb_sectors;
1489 reqs[outidx].qiov = qiov;
1490
1491 mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
1492 } else {
1493 outidx++;
1494 reqs[outidx].sector = reqs[i].sector;
1495 reqs[outidx].nb_sectors = reqs[i].nb_sectors;
1496 reqs[outidx].qiov = reqs[i].qiov;
1497 }
1498 }
1499
1500 return outidx + 1;
1501}
1502
1503/*
1504 * Submit multiple AIO write requests at once.
1505 *
1506 * On success, the function returns 0 and all requests in the reqs array have
1507 * been submitted. In error case this function returns -1, and any of the
1508 * requests may or may not be submitted yet. In particular, this means that the
1509 * callback will be called for some of the requests, for others it won't. The
1510 * caller must check the error field of the BlockRequest to wait for the right
1511 * callbacks (if error != 0, no callback will be called).
1512 *
1513 * The implementation may modify the contents of the reqs array, e.g. to merge
1514 * requests. However, the fields opaque and error are left unmodified as they
1515 * are used to signal failure for a single request to the caller.
1516 */
1517int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
1518{
1519 BlockDriverAIOCB *acb;
1520 MultiwriteCB *mcb;
1521 int i;
1522
1523 if (num_reqs == 0) {
1524 return 0;
1525 }
1526
1527 // Create MultiwriteCB structure
1528 mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
1529 mcb->num_requests = 0;
1530 mcb->num_callbacks = num_reqs;
1531
1532 for (i = 0; i < num_reqs; i++) {
1533 mcb->callbacks[i].cb = reqs[i].cb;
1534 mcb->callbacks[i].opaque = reqs[i].opaque;
1535 }
1536
1537 // Check for mergable requests
1538 num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
1539
1540 // Run the aio requests
1541 for (i = 0; i < num_reqs; i++) {
1542 acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
1543 reqs[i].nb_sectors, multiwrite_cb, mcb);
1544
1545 if (acb == NULL) {
1546 // We can only fail the whole thing if no request has been
1547 // submitted yet. Otherwise we'll wait for the submitted AIOs to
1548 // complete and report the error in the callback.
1549 if (mcb->num_requests == 0) {
1550 reqs[i].error = EIO;
1551 goto fail;
1552 } else {
1553 mcb->error = EIO;
1554 break;
1555 }
1556 } else {
1557 mcb->num_requests++;
1558 }
1559 }
1560
1561 return 0;
1562
1563fail:
1564 free(mcb);
1565 return -1;
1566}
1567
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02001568BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
1569 BlockDriverCompletionFunc *cb, void *opaque)
1570{
1571 BlockDriver *drv = bs->drv;
1572
1573 if (!drv)
1574 return NULL;
1575
1576 /*
1577 * Note that unlike bdrv_flush the driver is reponsible for flushing a
1578 * backing image if it exists.
1579 */
1580 return drv->bdrv_aio_flush(bs, cb, opaque);
1581}
1582
bellard83f64092006-08-01 16:21:11 +00001583void bdrv_aio_cancel(BlockDriverAIOCB *acb)
pbrookce1a14d2006-08-07 02:38:06 +00001584{
aliguori6bbff9a2009-03-20 18:25:59 +00001585 acb->pool->cancel(acb);
bellard83f64092006-08-01 16:21:11 +00001586}
1587
pbrookce1a14d2006-08-07 02:38:06 +00001588
bellard83f64092006-08-01 16:21:11 +00001589/**************************************************************/
1590/* async block device emulation */
1591
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02001592typedef struct BlockDriverAIOCBSync {
1593 BlockDriverAIOCB common;
1594 QEMUBH *bh;
1595 int ret;
1596 /* vector translation state */
1597 QEMUIOVector *qiov;
1598 uint8_t *bounce;
1599 int is_write;
1600} BlockDriverAIOCBSync;
1601
1602static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1603{
1604 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
Dor Laor6a7ad292009-06-01 12:07:23 +03001605 qemu_bh_delete(acb->bh);
Avi Kivity36afc452009-06-23 16:20:36 +03001606 acb->bh = NULL;
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02001607 qemu_aio_release(acb);
1608}
1609
1610static AIOPool bdrv_em_aio_pool = {
1611 .aiocb_size = sizeof(BlockDriverAIOCBSync),
1612 .cancel = bdrv_aio_cancel_em,
1613};
1614
bellard83f64092006-08-01 16:21:11 +00001615static void bdrv_aio_bh_cb(void *opaque)
bellardbeac80c2006-06-26 20:08:57 +00001616{
pbrookce1a14d2006-08-07 02:38:06 +00001617 BlockDriverAIOCBSync *acb = opaque;
aliguorif141eaf2009-04-07 18:43:24 +00001618
aliguorif141eaf2009-04-07 18:43:24 +00001619 if (!acb->is_write)
1620 qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
aliguoriceb42de2009-04-07 18:43:28 +00001621 qemu_vfree(acb->bounce);
pbrookce1a14d2006-08-07 02:38:06 +00001622 acb->common.cb(acb->common.opaque, acb->ret);
Dor Laor6a7ad292009-06-01 12:07:23 +03001623 qemu_bh_delete(acb->bh);
Avi Kivity36afc452009-06-23 16:20:36 +03001624 acb->bh = NULL;
pbrookce1a14d2006-08-07 02:38:06 +00001625 qemu_aio_release(acb);
bellardbeac80c2006-06-26 20:08:57 +00001626}
bellardbeac80c2006-06-26 20:08:57 +00001627
aliguorif141eaf2009-04-07 18:43:24 +00001628static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
1629 int64_t sector_num,
1630 QEMUIOVector *qiov,
1631 int nb_sectors,
1632 BlockDriverCompletionFunc *cb,
1633 void *opaque,
1634 int is_write)
1635
bellardea2384d2004-08-01 21:59:26 +00001636{
pbrookce1a14d2006-08-07 02:38:06 +00001637 BlockDriverAIOCBSync *acb;
pbrookce1a14d2006-08-07 02:38:06 +00001638
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02001639 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
aliguorif141eaf2009-04-07 18:43:24 +00001640 acb->is_write = is_write;
1641 acb->qiov = qiov;
aliguorie268ca52009-04-22 20:20:00 +00001642 acb->bounce = qemu_blockalign(bs, qiov->size);
aliguorif141eaf2009-04-07 18:43:24 +00001643
pbrookce1a14d2006-08-07 02:38:06 +00001644 if (!acb->bh)
1645 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
aliguorif141eaf2009-04-07 18:43:24 +00001646
1647 if (is_write) {
1648 qemu_iovec_to_buffer(acb->qiov, acb->bounce);
1649 acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
1650 } else {
1651 acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
1652 }
1653
pbrookce1a14d2006-08-07 02:38:06 +00001654 qemu_bh_schedule(acb->bh);
aliguorif141eaf2009-04-07 18:43:24 +00001655
pbrookce1a14d2006-08-07 02:38:06 +00001656 return &acb->common;
pbrook7a6cba62006-06-04 11:39:07 +00001657}
1658
aliguorif141eaf2009-04-07 18:43:24 +00001659static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
1660 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
pbrookce1a14d2006-08-07 02:38:06 +00001661 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001662{
aliguorif141eaf2009-04-07 18:43:24 +00001663 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
bellard83f64092006-08-01 16:21:11 +00001664}
1665
aliguorif141eaf2009-04-07 18:43:24 +00001666static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
1667 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
1668 BlockDriverCompletionFunc *cb, void *opaque)
1669{
1670 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
1671}
1672
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02001673static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
1674 BlockDriverCompletionFunc *cb, void *opaque)
1675{
1676 BlockDriverAIOCBSync *acb;
1677
1678 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
1679 acb->is_write = 1; /* don't bounce in the completion hadler */
1680 acb->qiov = NULL;
1681 acb->bounce = NULL;
1682 acb->ret = 0;
1683
1684 if (!acb->bh)
1685 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1686
1687 bdrv_flush(bs);
1688 qemu_bh_schedule(acb->bh);
1689 return &acb->common;
1690}
1691
bellard83f64092006-08-01 16:21:11 +00001692/**************************************************************/
1693/* sync block device emulation */
1694
1695static void bdrv_rw_em_cb(void *opaque, int ret)
1696{
1697 *(int *)opaque = ret;
1698}
1699
1700#define NOT_DONE 0x7fffffff
1701
ths5fafdf22007-09-16 21:08:06 +00001702static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +00001703 uint8_t *buf, int nb_sectors)
1704{
pbrookce1a14d2006-08-07 02:38:06 +00001705 int async_ret;
1706 BlockDriverAIOCB *acb;
aliguorif141eaf2009-04-07 18:43:24 +00001707 struct iovec iov;
1708 QEMUIOVector qiov;
bellard83f64092006-08-01 16:21:11 +00001709
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02001710 async_context_push();
1711
bellard83f64092006-08-01 16:21:11 +00001712 async_ret = NOT_DONE;
blueswir13f4cb3d2009-04-13 16:31:01 +00001713 iov.iov_base = (void *)buf;
aliguorif141eaf2009-04-07 18:43:24 +00001714 iov.iov_len = nb_sectors * 512;
1715 qemu_iovec_init_external(&qiov, &iov, 1);
1716 acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
1717 bdrv_rw_em_cb, &async_ret);
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02001718 if (acb == NULL) {
1719 async_ret = -1;
1720 goto fail;
1721 }
aliguoribaf35cb2008-09-10 15:45:19 +00001722
bellard83f64092006-08-01 16:21:11 +00001723 while (async_ret == NOT_DONE) {
1724 qemu_aio_wait();
1725 }
aliguoribaf35cb2008-09-10 15:45:19 +00001726
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02001727
1728fail:
1729 async_context_pop();
bellard83f64092006-08-01 16:21:11 +00001730 return async_ret;
1731}
1732
1733static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1734 const uint8_t *buf, int nb_sectors)
1735{
pbrookce1a14d2006-08-07 02:38:06 +00001736 int async_ret;
1737 BlockDriverAIOCB *acb;
aliguorif141eaf2009-04-07 18:43:24 +00001738 struct iovec iov;
1739 QEMUIOVector qiov;
bellard83f64092006-08-01 16:21:11 +00001740
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02001741 async_context_push();
1742
bellard83f64092006-08-01 16:21:11 +00001743 async_ret = NOT_DONE;
aliguorif141eaf2009-04-07 18:43:24 +00001744 iov.iov_base = (void *)buf;
1745 iov.iov_len = nb_sectors * 512;
1746 qemu_iovec_init_external(&qiov, &iov, 1);
1747 acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
1748 bdrv_rw_em_cb, &async_ret);
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02001749 if (acb == NULL) {
1750 async_ret = -1;
1751 goto fail;
1752 }
bellard83f64092006-08-01 16:21:11 +00001753 while (async_ret == NOT_DONE) {
1754 qemu_aio_wait();
1755 }
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02001756
1757fail:
1758 async_context_pop();
bellard83f64092006-08-01 16:21:11 +00001759 return async_ret;
1760}
bellardea2384d2004-08-01 21:59:26 +00001761
1762void bdrv_init(void)
1763{
Anthony Liguori5efa9d52009-05-09 17:03:42 -05001764 module_call_init(MODULE_INIT_BLOCK);
bellardea2384d2004-08-01 21:59:26 +00001765}
pbrookce1a14d2006-08-07 02:38:06 +00001766
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02001767void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
1768 BlockDriverCompletionFunc *cb, void *opaque)
aliguori6bbff9a2009-03-20 18:25:59 +00001769{
pbrookce1a14d2006-08-07 02:38:06 +00001770 BlockDriverAIOCB *acb;
1771
aliguori6bbff9a2009-03-20 18:25:59 +00001772 if (pool->free_aiocb) {
1773 acb = pool->free_aiocb;
1774 pool->free_aiocb = acb->next;
pbrookce1a14d2006-08-07 02:38:06 +00001775 } else {
aliguori6bbff9a2009-03-20 18:25:59 +00001776 acb = qemu_mallocz(pool->aiocb_size);
1777 acb->pool = pool;
pbrookce1a14d2006-08-07 02:38:06 +00001778 }
1779 acb->bs = bs;
1780 acb->cb = cb;
1781 acb->opaque = opaque;
1782 return acb;
1783}
1784
1785void qemu_aio_release(void *p)
1786{
aliguori6bbff9a2009-03-20 18:25:59 +00001787 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
1788 AIOPool *pool = acb->pool;
1789 acb->next = pool->free_aiocb;
1790 pool->free_aiocb = acb;
pbrookce1a14d2006-08-07 02:38:06 +00001791}
bellard19cb3732006-08-19 11:45:59 +00001792
1793/**************************************************************/
1794/* removable device support */
1795
1796/**
1797 * Return TRUE if the media is present
1798 */
1799int bdrv_is_inserted(BlockDriverState *bs)
1800{
1801 BlockDriver *drv = bs->drv;
1802 int ret;
1803 if (!drv)
1804 return 0;
1805 if (!drv->bdrv_is_inserted)
1806 return 1;
1807 ret = drv->bdrv_is_inserted(bs);
1808 return ret;
1809}
1810
1811/**
1812 * Return TRUE if the media changed since the last call to this
ths5fafdf22007-09-16 21:08:06 +00001813 * function. It is currently only used for floppy disks
bellard19cb3732006-08-19 11:45:59 +00001814 */
1815int bdrv_media_changed(BlockDriverState *bs)
1816{
1817 BlockDriver *drv = bs->drv;
1818 int ret;
1819
1820 if (!drv || !drv->bdrv_media_changed)
1821 ret = -ENOTSUP;
1822 else
1823 ret = drv->bdrv_media_changed(bs);
1824 if (ret == -ENOTSUP)
1825 ret = bs->media_changed;
1826 bs->media_changed = 0;
1827 return ret;
1828}
1829
1830/**
1831 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1832 */
Mark McLoughlinaea2a332009-05-27 10:06:11 +01001833int bdrv_eject(BlockDriverState *bs, int eject_flag)
bellard19cb3732006-08-19 11:45:59 +00001834{
1835 BlockDriver *drv = bs->drv;
1836 int ret;
1837
Mark McLoughlinaea2a332009-05-27 10:06:11 +01001838 if (bs->locked) {
1839 return -EBUSY;
1840 }
1841
bellard19cb3732006-08-19 11:45:59 +00001842 if (!drv || !drv->bdrv_eject) {
1843 ret = -ENOTSUP;
1844 } else {
1845 ret = drv->bdrv_eject(bs, eject_flag);
1846 }
1847 if (ret == -ENOTSUP) {
1848 if (eject_flag)
1849 bdrv_close(bs);
Mark McLoughlinaea2a332009-05-27 10:06:11 +01001850 ret = 0;
bellard19cb3732006-08-19 11:45:59 +00001851 }
Mark McLoughlinaea2a332009-05-27 10:06:11 +01001852
1853 return ret;
bellard19cb3732006-08-19 11:45:59 +00001854}
1855
1856int bdrv_is_locked(BlockDriverState *bs)
1857{
1858 return bs->locked;
1859}
1860
1861/**
1862 * Lock or unlock the media (if it is locked, the user won't be able
1863 * to eject it manually).
1864 */
1865void bdrv_set_locked(BlockDriverState *bs, int locked)
1866{
1867 BlockDriver *drv = bs->drv;
1868
1869 bs->locked = locked;
1870 if (drv && drv->bdrv_set_locked) {
1871 drv->bdrv_set_locked(bs, locked);
1872 }
1873}
ths985a03b2007-12-24 16:10:43 +00001874
1875/* needed for generic scsi interface */
1876
1877int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1878{
1879 BlockDriver *drv = bs->drv;
1880
1881 if (drv && drv->bdrv_ioctl)
1882 return drv->bdrv_ioctl(bs, req, buf);
1883 return -ENOTSUP;
1884}
aliguori7d780662009-03-12 19:57:08 +00001885
aliguori221f7152009-03-28 17:28:41 +00001886BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
1887 unsigned long int req, void *buf,
1888 BlockDriverCompletionFunc *cb, void *opaque)
aliguori7d780662009-03-12 19:57:08 +00001889{
aliguori221f7152009-03-28 17:28:41 +00001890 BlockDriver *drv = bs->drv;
aliguori7d780662009-03-12 19:57:08 +00001891
aliguori221f7152009-03-28 17:28:41 +00001892 if (drv && drv->bdrv_aio_ioctl)
1893 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
1894 return NULL;
aliguori7d780662009-03-12 19:57:08 +00001895}
aliguorie268ca52009-04-22 20:20:00 +00001896
1897void *qemu_blockalign(BlockDriverState *bs, size_t size)
1898{
1899 return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
1900}