blob: e6b91c60ac37e7c0969c65a65a25de9da77a8850 [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"
blueswir1179a2c12009-03-08 08:23:32 +000025#ifdef HOST_BSD
blueswir13990d092008-12-05 17:53:21 +000026/* include native header before sys-queue.h */
27#include <sys/queue.h>
28#endif
29
pbrookfaf07962007-11-11 02:51:17 +000030#include "qemu-common.h"
aliguori376253e2009-03-05 23:01:23 +000031#include "monitor.h"
bellardea2384d2004-08-01 21:59:26 +000032#include "block_int.h"
Anthony Liguori5efa9d52009-05-09 17:03:42 -050033#include "module.h"
bellardfc01f7e2003-06-30 10:03:06 +000034
blueswir1179a2c12009-03-08 08:23:32 +000035#ifdef HOST_BSD
bellard7674e7b2005-04-26 21:59:26 +000036#include <sys/types.h>
37#include <sys/stat.h>
38#include <sys/ioctl.h>
blueswir1c5e97232009-03-07 20:06:23 +000039#ifndef __DragonFly__
bellard7674e7b2005-04-26 21:59:26 +000040#include <sys/disk.h>
41#endif
blueswir1c5e97232009-03-07 20:06:23 +000042#endif
bellard7674e7b2005-04-26 21:59:26 +000043
aliguori49dc7682009-03-08 16:26:59 +000044#ifdef _WIN32
45#include <windows.h>
46#endif
47
bellard83f64092006-08-01 16:21:11 +000048#define SECTOR_BITS 9
49#define SECTOR_SIZE (1 << SECTOR_BITS)
bellard3b0d4f62005-10-30 18:30:10 +000050
aliguorif141eaf2009-04-07 18:43:24 +000051static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
52 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
aliguoric87c0672009-04-07 18:43:20 +000053 BlockDriverCompletionFunc *cb, void *opaque);
aliguorif141eaf2009-04-07 18:43:24 +000054static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
55 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
pbrookce1a14d2006-08-07 02:38:06 +000056 BlockDriverCompletionFunc *cb, void *opaque);
ths5fafdf22007-09-16 21:08:06 +000057static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +000058 uint8_t *buf, int nb_sectors);
59static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
60 const uint8_t *buf, int nb_sectors);
bellardec530c82006-04-25 22:36:06 +000061
blueswir17ee930d2008-09-17 19:04:14 +000062BlockDriverState *bdrv_first;
63
bellardea2384d2004-08-01 21:59:26 +000064static BlockDriver *first_drv;
65
bellard83f64092006-08-01 16:21:11 +000066int path_is_absolute(const char *path)
67{
68 const char *p;
bellard21664422007-01-07 18:22:37 +000069#ifdef _WIN32
70 /* specific case for names like: "\\.\d:" */
71 if (*path == '/' || *path == '\\')
72 return 1;
73#endif
bellard83f64092006-08-01 16:21:11 +000074 p = strchr(path, ':');
75 if (p)
76 p++;
77 else
78 p = path;
bellard3b9f94e2007-01-07 17:27:07 +000079#ifdef _WIN32
80 return (*p == '/' || *p == '\\');
81#else
82 return (*p == '/');
83#endif
bellard83f64092006-08-01 16:21:11 +000084}
85
86/* if filename is absolute, just copy it to dest. Otherwise, build a
87 path to it by considering it is relative to base_path. URL are
88 supported. */
89void path_combine(char *dest, int dest_size,
90 const char *base_path,
91 const char *filename)
92{
93 const char *p, *p1;
94 int len;
95
96 if (dest_size <= 0)
97 return;
98 if (path_is_absolute(filename)) {
99 pstrcpy(dest, dest_size, filename);
100 } else {
101 p = strchr(base_path, ':');
102 if (p)
103 p++;
104 else
105 p = base_path;
bellard3b9f94e2007-01-07 17:27:07 +0000106 p1 = strrchr(base_path, '/');
107#ifdef _WIN32
108 {
109 const char *p2;
110 p2 = strrchr(base_path, '\\');
111 if (!p1 || p2 > p1)
112 p1 = p2;
113 }
114#endif
bellard83f64092006-08-01 16:21:11 +0000115 if (p1)
116 p1++;
117 else
118 p1 = base_path;
119 if (p1 > p)
120 p = p1;
121 len = p - base_path;
122 if (len > dest_size - 1)
123 len = dest_size - 1;
124 memcpy(dest, base_path, len);
125 dest[len] = '\0';
126 pstrcat(dest, dest_size, filename);
127 }
128}
129
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500130void bdrv_register(BlockDriver *bdrv)
bellardea2384d2004-08-01 21:59:26 +0000131{
aliguorif141eaf2009-04-07 18:43:24 +0000132 if (!bdrv->bdrv_aio_readv) {
bellard83f64092006-08-01 16:21:11 +0000133 /* add AIO emulation layer */
aliguorif141eaf2009-04-07 18:43:24 +0000134 bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
135 bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
aliguorieda578e2009-03-12 19:57:16 +0000136 } else if (!bdrv->bdrv_read) {
bellard83f64092006-08-01 16:21:11 +0000137 /* add synchronous IO emulation layer */
138 bdrv->bdrv_read = bdrv_read_em;
139 bdrv->bdrv_write = bdrv_write_em;
140 }
bellardea2384d2004-08-01 21:59:26 +0000141 bdrv->next = first_drv;
142 first_drv = bdrv;
143}
bellardb3380822004-03-14 21:38:54 +0000144
145/* create a new block device (by default it is empty) */
146BlockDriverState *bdrv_new(const char *device_name)
bellardfc01f7e2003-06-30 10:03:06 +0000147{
bellardb3380822004-03-14 21:38:54 +0000148 BlockDriverState **pbs, *bs;
149
150 bs = qemu_mallocz(sizeof(BlockDriverState));
bellardb3380822004-03-14 21:38:54 +0000151 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
bellardea2384d2004-08-01 21:59:26 +0000152 if (device_name[0] != '\0') {
153 /* insert at the end */
154 pbs = &bdrv_first;
155 while (*pbs != NULL)
156 pbs = &(*pbs)->next;
157 *pbs = bs;
158 }
bellardb3380822004-03-14 21:38:54 +0000159 return bs;
160}
161
bellardea2384d2004-08-01 21:59:26 +0000162BlockDriver *bdrv_find_format(const char *format_name)
163{
164 BlockDriver *drv1;
165 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
166 if (!strcmp(drv1->format_name, format_name))
167 return drv1;
168 }
169 return NULL;
170}
171
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200172int bdrv_create(BlockDriver *drv, const char* filename,
173 QEMUOptionParameter *options)
bellardea2384d2004-08-01 21:59:26 +0000174{
175 if (!drv->bdrv_create)
176 return -ENOTSUP;
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200177
178 return drv->bdrv_create(filename, options);
bellardea2384d2004-08-01 21:59:26 +0000179}
180
bellardd5249392004-08-03 21:14:23 +0000181#ifdef _WIN32
bellard95389c82005-12-18 18:28:15 +0000182void get_tmp_filename(char *filename, int size)
bellardd5249392004-08-03 21:14:23 +0000183{
bellard3b9f94e2007-01-07 17:27:07 +0000184 char temp_dir[MAX_PATH];
ths3b46e622007-09-17 08:09:54 +0000185
bellard3b9f94e2007-01-07 17:27:07 +0000186 GetTempPath(MAX_PATH, temp_dir);
187 GetTempFileName(temp_dir, "qem", 0, filename);
bellardd5249392004-08-03 21:14:23 +0000188}
189#else
bellard95389c82005-12-18 18:28:15 +0000190void get_tmp_filename(char *filename, int size)
bellardea2384d2004-08-01 21:59:26 +0000191{
192 int fd;
blueswir17ccfb2e2008-09-14 06:45:34 +0000193 const char *tmpdir;
bellardd5249392004-08-03 21:14:23 +0000194 /* XXX: race condition possible */
aurel320badc1e2008-03-10 00:05:34 +0000195 tmpdir = getenv("TMPDIR");
196 if (!tmpdir)
197 tmpdir = "/tmp";
198 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
bellardea2384d2004-08-01 21:59:26 +0000199 fd = mkstemp(filename);
200 close(fd);
201}
bellardd5249392004-08-03 21:14:23 +0000202#endif
bellardea2384d2004-08-01 21:59:26 +0000203
bellard19cb3732006-08-19 11:45:59 +0000204#ifdef _WIN32
bellardf45512f2006-08-23 21:40:13 +0000205static int is_windows_drive_prefix(const char *filename)
206{
207 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
208 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
209 filename[1] == ':');
210}
ths3b46e622007-09-17 08:09:54 +0000211
bellard19cb3732006-08-19 11:45:59 +0000212static int is_windows_drive(const char *filename)
213{
ths5fafdf22007-09-16 21:08:06 +0000214 if (is_windows_drive_prefix(filename) &&
bellardf45512f2006-08-23 21:40:13 +0000215 filename[2] == '\0')
bellard19cb3732006-08-19 11:45:59 +0000216 return 1;
217 if (strstart(filename, "\\\\.\\", NULL) ||
218 strstart(filename, "//./", NULL))
219 return 1;
220 return 0;
221}
222#endif
223
bellard83f64092006-08-01 16:21:11 +0000224static BlockDriver *find_protocol(const char *filename)
225{
226 BlockDriver *drv1;
227 char protocol[128];
228 int len;
229 const char *p;
bellard19cb3732006-08-19 11:45:59 +0000230
231#ifdef _WIN32
bellardf45512f2006-08-23 21:40:13 +0000232 if (is_windows_drive(filename) ||
233 is_windows_drive_prefix(filename))
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500234 return bdrv_find_format("raw");
bellard19cb3732006-08-19 11:45:59 +0000235#endif
bellard83f64092006-08-01 16:21:11 +0000236 p = strchr(filename, ':');
237 if (!p)
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500238 return bdrv_find_format("raw");
bellard83f64092006-08-01 16:21:11 +0000239 len = p - filename;
240 if (len > sizeof(protocol) - 1)
241 len = sizeof(protocol) - 1;
bellard83f64092006-08-01 16:21:11 +0000242 memcpy(protocol, filename, len);
243 protocol[len] = '\0';
244 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
ths5fafdf22007-09-16 21:08:06 +0000245 if (drv1->protocol_name &&
bellard83f64092006-08-01 16:21:11 +0000246 !strcmp(drv1->protocol_name, protocol))
247 return drv1;
248 }
249 return NULL;
250}
251
bellard7674e7b2005-04-26 21:59:26 +0000252/* XXX: force raw format if block or character device ? It would
253 simplify the BSD case */
bellardea2384d2004-08-01 21:59:26 +0000254static BlockDriver *find_image_format(const char *filename)
255{
bellard83f64092006-08-01 16:21:11 +0000256 int ret, score, score_max;
bellardea2384d2004-08-01 21:59:26 +0000257 BlockDriver *drv1, *drv;
bellard83f64092006-08-01 16:21:11 +0000258 uint8_t buf[2048];
259 BlockDriverState *bs;
ths3b46e622007-09-17 08:09:54 +0000260
bellard19cb3732006-08-19 11:45:59 +0000261 /* detect host devices. By convention, /dev/cdrom[N] is always
262 recognized as a host CDROM */
263 if (strstart(filename, "/dev/cdrom", NULL))
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500264 return bdrv_find_format("host_device");
bellard19cb3732006-08-19 11:45:59 +0000265#ifdef _WIN32
266 if (is_windows_drive(filename))
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500267 return bdrv_find_format("host_device");
bellard19cb3732006-08-19 11:45:59 +0000268#else
269 {
270 struct stat st;
ths5fafdf22007-09-16 21:08:06 +0000271 if (stat(filename, &st) >= 0 &&
bellard19cb3732006-08-19 11:45:59 +0000272 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500273 return bdrv_find_format("host_device");
bellard19cb3732006-08-19 11:45:59 +0000274 }
275 }
276#endif
ths3b46e622007-09-17 08:09:54 +0000277
bellard83f64092006-08-01 16:21:11 +0000278 drv = find_protocol(filename);
bellard19cb3732006-08-19 11:45:59 +0000279 /* no need to test disk image formats for vvfat */
Anthony Liguoric833ab72009-05-22 08:17:55 -0500280 if (drv && strcmp(drv->format_name, "vvfat") == 0)
bellard83f64092006-08-01 16:21:11 +0000281 return drv;
bellard19cb3732006-08-19 11:45:59 +0000282
bellard83f64092006-08-01 16:21:11 +0000283 ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
284 if (ret < 0)
285 return NULL;
286 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
287 bdrv_delete(bs);
288 if (ret < 0) {
289 return NULL;
290 }
291
bellardea2384d2004-08-01 21:59:26 +0000292 score_max = 0;
293 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
bellard83f64092006-08-01 16:21:11 +0000294 if (drv1->bdrv_probe) {
295 score = drv1->bdrv_probe(buf, ret, filename);
296 if (score > score_max) {
297 score_max = score;
298 drv = drv1;
299 }
bellardea2384d2004-08-01 21:59:26 +0000300 }
301 }
302 return drv;
303}
304
bellard83f64092006-08-01 16:21:11 +0000305int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
bellardb3380822004-03-14 21:38:54 +0000306{
bellard83f64092006-08-01 16:21:11 +0000307 BlockDriverState *bs;
308 int ret;
309
310 bs = bdrv_new("");
bellard83f64092006-08-01 16:21:11 +0000311 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
312 if (ret < 0) {
313 bdrv_delete(bs);
314 return ret;
bellard3b0d4f62005-10-30 18:30:10 +0000315 }
aliguori71d07702009-03-03 17:37:16 +0000316 bs->growable = 1;
bellard83f64092006-08-01 16:21:11 +0000317 *pbs = bs;
318 return 0;
bellardea2384d2004-08-01 21:59:26 +0000319}
bellardfc01f7e2003-06-30 10:03:06 +0000320
bellard83f64092006-08-01 16:21:11 +0000321int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
322{
323 return bdrv_open2(bs, filename, flags, NULL);
324}
325
326int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
bellardea2384d2004-08-01 21:59:26 +0000327 BlockDriver *drv)
328{
bellard83f64092006-08-01 16:21:11 +0000329 int ret, open_flags;
thseb5c8512007-02-11 15:06:09 +0000330 char tmp_filename[PATH_MAX];
331 char backing_filename[PATH_MAX];
ths3b46e622007-09-17 08:09:54 +0000332
bellardea2384d2004-08-01 21:59:26 +0000333 bs->read_only = 0;
334 bs->is_temporary = 0;
335 bs->encrypted = 0;
aliguoric0f4ce72009-03-05 23:01:01 +0000336 bs->valid_key = 0;
aliguorie268ca52009-04-22 20:20:00 +0000337 /* buffer_alignment defaulted to 512, drivers can change this value */
338 bs->buffer_alignment = 512;
bellard712e7872005-04-28 21:09:32 +0000339
bellard83f64092006-08-01 16:21:11 +0000340 if (flags & BDRV_O_SNAPSHOT) {
bellardea2384d2004-08-01 21:59:26 +0000341 BlockDriverState *bs1;
342 int64_t total_size;
aliguori7c96d462008-09-12 17:54:13 +0000343 int is_protocol = 0;
Kevin Wolf91a073a2009-05-27 14:48:06 +0200344 BlockDriver *bdrv_qcow2;
345 QEMUOptionParameter *options;
ths3b46e622007-09-17 08:09:54 +0000346
bellardea2384d2004-08-01 21:59:26 +0000347 /* if snapshot, we create a temporary backing file and open it
348 instead of opening 'filename' directly */
349
350 /* if there is a backing file, use it */
351 bs1 = bdrv_new("");
aliguori5eb45632009-03-28 17:55:10 +0000352 ret = bdrv_open2(bs1, filename, 0, drv);
aliguori51d7c002009-03-05 23:00:29 +0000353 if (ret < 0) {
bellardea2384d2004-08-01 21:59:26 +0000354 bdrv_delete(bs1);
aliguori51d7c002009-03-05 23:00:29 +0000355 return ret;
bellardea2384d2004-08-01 21:59:26 +0000356 }
bellard83f64092006-08-01 16:21:11 +0000357 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
aliguori7c96d462008-09-12 17:54:13 +0000358
359 if (bs1->drv && bs1->drv->protocol_name)
360 is_protocol = 1;
361
bellardea2384d2004-08-01 21:59:26 +0000362 bdrv_delete(bs1);
ths3b46e622007-09-17 08:09:54 +0000363
bellardea2384d2004-08-01 21:59:26 +0000364 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
aliguori7c96d462008-09-12 17:54:13 +0000365
366 /* Real path is meaningless for protocols */
367 if (is_protocol)
368 snprintf(backing_filename, sizeof(backing_filename),
369 "%s", filename);
370 else
371 realpath(filename, backing_filename);
372
Kevin Wolf91a073a2009-05-27 14:48:06 +0200373 bdrv_qcow2 = bdrv_find_format("qcow2");
374 options = parse_option_parameters("", bdrv_qcow2->create_options, NULL);
375
376 set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size * 512);
377 set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename);
378 if (drv) {
379 set_option_parameter(options, BLOCK_OPT_BACKING_FMT,
380 drv->format_name);
381 }
382
383 ret = bdrv_create(bdrv_qcow2, tmp_filename, options);
aliguori51d7c002009-03-05 23:00:29 +0000384 if (ret < 0) {
385 return ret;
bellardea2384d2004-08-01 21:59:26 +0000386 }
Kevin Wolf91a073a2009-05-27 14:48:06 +0200387
bellardea2384d2004-08-01 21:59:26 +0000388 filename = tmp_filename;
Kevin Wolf91a073a2009-05-27 14:48:06 +0200389 drv = bdrv_qcow2;
bellardea2384d2004-08-01 21:59:26 +0000390 bs->is_temporary = 1;
391 }
bellard712e7872005-04-28 21:09:32 +0000392
bellardea2384d2004-08-01 21:59:26 +0000393 pstrcpy(bs->filename, sizeof(bs->filename), filename);
bellard83f64092006-08-01 16:21:11 +0000394 if (flags & BDRV_O_FILE) {
395 drv = find_protocol(filename);
aliguori51d7c002009-03-05 23:00:29 +0000396 } else if (!drv) {
397 drv = find_image_format(filename);
398 }
399 if (!drv) {
400 ret = -ENOENT;
401 goto unlink_and_fail;
bellardea2384d2004-08-01 21:59:26 +0000402 }
403 bs->drv = drv;
404 bs->opaque = qemu_mallocz(drv->instance_size);
bellard83f64092006-08-01 16:21:11 +0000405 /* Note: for compatibility, we open disk image files as RDWR, and
406 RDONLY as fallback */
407 if (!(flags & BDRV_O_FILE))
aliguori9f7965c2008-10-14 14:42:54 +0000408 open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK);
bellard83f64092006-08-01 16:21:11 +0000409 else
410 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
411 ret = drv->bdrv_open(bs, filename, open_flags);
aurel32a0a83532008-10-13 21:08:34 +0000412 if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
aliguori9f7965c2008-10-14 14:42:54 +0000413 ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
bellard83f64092006-08-01 16:21:11 +0000414 bs->read_only = 1;
415 }
bellardea2384d2004-08-01 21:59:26 +0000416 if (ret < 0) {
417 qemu_free(bs->opaque);
bellard6b21b972006-08-23 21:14:37 +0000418 bs->opaque = NULL;
419 bs->drv = NULL;
aliguori51d7c002009-03-05 23:00:29 +0000420 unlink_and_fail:
421 if (bs->is_temporary)
422 unlink(filename);
bellard83f64092006-08-01 16:21:11 +0000423 return ret;
bellardea2384d2004-08-01 21:59:26 +0000424 }
bellardd15a7712006-08-06 13:35:09 +0000425 if (drv->bdrv_getlength) {
426 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
427 }
bellardea2384d2004-08-01 21:59:26 +0000428#ifndef _WIN32
429 if (bs->is_temporary) {
430 unlink(filename);
431 }
432#endif
bellard83f64092006-08-01 16:21:11 +0000433 if (bs->backing_file[0] != '\0') {
bellardea2384d2004-08-01 21:59:26 +0000434 /* if there is a backing file, use it */
aliguori5eb45632009-03-28 17:55:10 +0000435 BlockDriver *back_drv = NULL;
bellardea2384d2004-08-01 21:59:26 +0000436 bs->backing_hd = bdrv_new("");
bellard83f64092006-08-01 16:21:11 +0000437 path_combine(backing_filename, sizeof(backing_filename),
438 filename, bs->backing_file);
aliguori5eb45632009-03-28 17:55:10 +0000439 if (bs->backing_format[0] != '\0')
440 back_drv = bdrv_find_format(bs->backing_format);
441 ret = bdrv_open2(bs->backing_hd, backing_filename, open_flags,
442 back_drv);
aliguori51d7c002009-03-05 23:00:29 +0000443 if (ret < 0) {
444 bdrv_close(bs);
445 return ret;
446 }
bellardea2384d2004-08-01 21:59:26 +0000447 }
448
aliguoribb5fc202009-03-05 23:01:15 +0000449 if (!bdrv_key_required(bs)) {
450 /* call the change callback */
451 bs->media_changed = 1;
452 if (bs->change_cb)
453 bs->change_cb(bs->change_opaque);
454 }
bellardb3380822004-03-14 21:38:54 +0000455 return 0;
bellardfc01f7e2003-06-30 10:03:06 +0000456}
457
458void bdrv_close(BlockDriverState *bs)
459{
bellard19cb3732006-08-19 11:45:59 +0000460 if (bs->drv) {
bellardea2384d2004-08-01 21:59:26 +0000461 if (bs->backing_hd)
462 bdrv_delete(bs->backing_hd);
463 bs->drv->bdrv_close(bs);
464 qemu_free(bs->opaque);
465#ifdef _WIN32
466 if (bs->is_temporary) {
467 unlink(bs->filename);
468 }
bellard67b915a2004-03-31 23:37:16 +0000469#endif
bellardea2384d2004-08-01 21:59:26 +0000470 bs->opaque = NULL;
471 bs->drv = NULL;
bellardb3380822004-03-14 21:38:54 +0000472
473 /* call the change callback */
bellard19cb3732006-08-19 11:45:59 +0000474 bs->media_changed = 1;
bellardb3380822004-03-14 21:38:54 +0000475 if (bs->change_cb)
476 bs->change_cb(bs->change_opaque);
477 }
478}
479
480void bdrv_delete(BlockDriverState *bs)
481{
aurel3234c6f052008-04-08 19:51:21 +0000482 BlockDriverState **pbs;
483
484 pbs = &bdrv_first;
485 while (*pbs != bs && *pbs != NULL)
486 pbs = &(*pbs)->next;
487 if (*pbs == bs)
488 *pbs = bs->next;
489
bellardb3380822004-03-14 21:38:54 +0000490 bdrv_close(bs);
491 qemu_free(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000492}
493
aliguorie97fc192009-04-21 23:11:50 +0000494/*
495 * Run consistency checks on an image
496 *
497 * Returns the number of errors or -errno when an internal error occurs
498 */
499int bdrv_check(BlockDriverState *bs)
500{
501 if (bs->drv->bdrv_check == NULL) {
502 return -ENOTSUP;
503 }
504
505 return bs->drv->bdrv_check(bs);
506}
507
bellard33e39632003-07-06 17:15:21 +0000508/* commit COW file into the raw image */
509int bdrv_commit(BlockDriverState *bs)
510{
bellard19cb3732006-08-19 11:45:59 +0000511 BlockDriver *drv = bs->drv;
bellard83f64092006-08-01 16:21:11 +0000512 int64_t i, total_sectors;
bellardea2384d2004-08-01 21:59:26 +0000513 int n, j;
514 unsigned char sector[512];
bellard33e39632003-07-06 17:15:21 +0000515
bellard19cb3732006-08-19 11:45:59 +0000516 if (!drv)
517 return -ENOMEDIUM;
bellard33e39632003-07-06 17:15:21 +0000518
519 if (bs->read_only) {
bellardea2384d2004-08-01 21:59:26 +0000520 return -EACCES;
bellard33e39632003-07-06 17:15:21 +0000521 }
522
bellardea2384d2004-08-01 21:59:26 +0000523 if (!bs->backing_hd) {
524 return -ENOTSUP;
bellard33e39632003-07-06 17:15:21 +0000525 }
bellardea2384d2004-08-01 21:59:26 +0000526
bellard83f64092006-08-01 16:21:11 +0000527 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
528 for (i = 0; i < total_sectors;) {
bellard19cb3732006-08-19 11:45:59 +0000529 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
bellardea2384d2004-08-01 21:59:26 +0000530 for(j = 0; j < n; j++) {
531 if (bdrv_read(bs, i, sector, 1) != 0) {
532 return -EIO;
533 }
534
535 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
536 return -EIO;
537 }
538 i++;
539 }
540 } else {
541 i += n;
542 }
543 }
bellard95389c82005-12-18 18:28:15 +0000544
bellard19cb3732006-08-19 11:45:59 +0000545 if (drv->bdrv_make_empty)
546 return drv->bdrv_make_empty(bs);
bellard95389c82005-12-18 18:28:15 +0000547
bellard33e39632003-07-06 17:15:21 +0000548 return 0;
549}
550
aliguori71d07702009-03-03 17:37:16 +0000551static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
552 size_t size)
553{
554 int64_t len;
555
556 if (!bdrv_is_inserted(bs))
557 return -ENOMEDIUM;
558
559 if (bs->growable)
560 return 0;
561
562 len = bdrv_getlength(bs);
563
Kevin Wolffbb7b4e2009-05-08 14:47:24 +0200564 if (offset < 0)
565 return -EIO;
566
567 if ((offset > len) || (len - offset < size))
aliguori71d07702009-03-03 17:37:16 +0000568 return -EIO;
569
570 return 0;
571}
572
573static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
574 int nb_sectors)
575{
aliguori999dec52009-03-29 01:31:48 +0000576 return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512);
aliguori71d07702009-03-03 17:37:16 +0000577}
578
bellard19cb3732006-08-19 11:45:59 +0000579/* return < 0 if error. See bdrv_write() for the return codes */
ths5fafdf22007-09-16 21:08:06 +0000580int bdrv_read(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000581 uint8_t *buf, int nb_sectors)
582{
bellardea2384d2004-08-01 21:59:26 +0000583 BlockDriver *drv = bs->drv;
584
bellard19cb3732006-08-19 11:45:59 +0000585 if (!drv)
586 return -ENOMEDIUM;
aliguori71d07702009-03-03 17:37:16 +0000587 if (bdrv_check_request(bs, sector_num, nb_sectors))
588 return -EIO;
bellardb3380822004-03-14 21:38:54 +0000589
aliguorieda578e2009-03-12 19:57:16 +0000590 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
bellardfc01f7e2003-06-30 10:03:06 +0000591}
592
ths5fafdf22007-09-16 21:08:06 +0000593/* Return < 0 if error. Important errors are:
bellard19cb3732006-08-19 11:45:59 +0000594 -EIO generic I/O error (may happen for all errors)
595 -ENOMEDIUM No media inserted.
596 -EINVAL Invalid sector number or nb_sectors
597 -EACCES Trying to write a read-only device
598*/
ths5fafdf22007-09-16 21:08:06 +0000599int bdrv_write(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000600 const uint8_t *buf, int nb_sectors)
601{
bellard83f64092006-08-01 16:21:11 +0000602 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +0000603 if (!bs->drv)
604 return -ENOMEDIUM;
bellard0849bf02003-06-30 23:17:31 +0000605 if (bs->read_only)
bellard19cb3732006-08-19 11:45:59 +0000606 return -EACCES;
aliguori71d07702009-03-03 17:37:16 +0000607 if (bdrv_check_request(bs, sector_num, nb_sectors))
608 return -EIO;
609
aliguori42fb2802009-01-15 20:43:39 +0000610 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
bellard83f64092006-08-01 16:21:11 +0000611}
612
aliguorieda578e2009-03-12 19:57:16 +0000613int bdrv_pread(BlockDriverState *bs, int64_t offset,
614 void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000615{
bellard83f64092006-08-01 16:21:11 +0000616 uint8_t tmp_buf[SECTOR_SIZE];
617 int len, nb_sectors, count;
618 int64_t sector_num;
619
620 count = count1;
621 /* first read to align to sector start */
622 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
623 if (len > count)
624 len = count;
625 sector_num = offset >> SECTOR_BITS;
626 if (len > 0) {
627 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
628 return -EIO;
629 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
630 count -= len;
631 if (count == 0)
632 return count1;
633 sector_num++;
634 buf += len;
635 }
636
637 /* read the sectors "in place" */
638 nb_sectors = count >> SECTOR_BITS;
639 if (nb_sectors > 0) {
640 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
641 return -EIO;
642 sector_num += nb_sectors;
643 len = nb_sectors << SECTOR_BITS;
644 buf += len;
645 count -= len;
646 }
647
648 /* add data from the last sector */
649 if (count > 0) {
650 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
651 return -EIO;
652 memcpy(buf, tmp_buf, count);
653 }
654 return count1;
655}
656
aliguorieda578e2009-03-12 19:57:16 +0000657int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
658 const void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000659{
bellard83f64092006-08-01 16:21:11 +0000660 uint8_t tmp_buf[SECTOR_SIZE];
661 int len, nb_sectors, count;
662 int64_t sector_num;
663
664 count = count1;
665 /* first write to align to sector start */
666 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
667 if (len > count)
668 len = count;
669 sector_num = offset >> SECTOR_BITS;
670 if (len > 0) {
671 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
672 return -EIO;
673 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
674 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
675 return -EIO;
676 count -= len;
677 if (count == 0)
678 return count1;
679 sector_num++;
680 buf += len;
681 }
682
683 /* write the sectors "in place" */
684 nb_sectors = count >> SECTOR_BITS;
685 if (nb_sectors > 0) {
686 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
687 return -EIO;
688 sector_num += nb_sectors;
689 len = nb_sectors << SECTOR_BITS;
690 buf += len;
691 count -= len;
692 }
693
694 /* add data from the last sector */
695 if (count > 0) {
696 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
697 return -EIO;
698 memcpy(tmp_buf, buf, count);
699 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
700 return -EIO;
701 }
702 return count1;
703}
bellard83f64092006-08-01 16:21:11 +0000704
705/**
bellard83f64092006-08-01 16:21:11 +0000706 * Truncate file to 'offset' bytes (needed only for file protocols)
707 */
708int bdrv_truncate(BlockDriverState *bs, int64_t offset)
709{
710 BlockDriver *drv = bs->drv;
711 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000712 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000713 if (!drv->bdrv_truncate)
714 return -ENOTSUP;
715 return drv->bdrv_truncate(bs, offset);
716}
717
718/**
719 * Length of a file in bytes. Return < 0 if error or unknown.
720 */
721int64_t bdrv_getlength(BlockDriverState *bs)
722{
723 BlockDriver *drv = bs->drv;
724 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000725 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000726 if (!drv->bdrv_getlength) {
727 /* legacy mode */
728 return bs->total_sectors * SECTOR_SIZE;
729 }
730 return drv->bdrv_getlength(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000731}
732
bellard19cb3732006-08-19 11:45:59 +0000733/* return 0 as number of sectors if no device present or error */
ths96b8f132007-12-17 01:35:20 +0000734void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
bellardfc01f7e2003-06-30 10:03:06 +0000735{
bellard19cb3732006-08-19 11:45:59 +0000736 int64_t length;
737 length = bdrv_getlength(bs);
738 if (length < 0)
739 length = 0;
740 else
741 length = length >> SECTOR_BITS;
742 *nb_sectors_ptr = length;
bellardfc01f7e2003-06-30 10:03:06 +0000743}
bellardcf989512004-02-16 21:56:36 +0000744
aliguorif3d54fc2008-11-25 21:50:24 +0000745struct partition {
746 uint8_t boot_ind; /* 0x80 - active */
747 uint8_t head; /* starting head */
748 uint8_t sector; /* starting sector */
749 uint8_t cyl; /* starting cylinder */
750 uint8_t sys_ind; /* What partition type */
751 uint8_t end_head; /* end head */
752 uint8_t end_sector; /* end sector */
753 uint8_t end_cyl; /* end cylinder */
754 uint32_t start_sect; /* starting sector counting from 0 */
755 uint32_t nr_sects; /* nr of sectors in partition */
756} __attribute__((packed));
757
758/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
759static int guess_disk_lchs(BlockDriverState *bs,
760 int *pcylinders, int *pheads, int *psectors)
761{
762 uint8_t buf[512];
763 int ret, i, heads, sectors, cylinders;
764 struct partition *p;
765 uint32_t nr_sects;
blueswir1a38131b2008-12-05 17:56:40 +0000766 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +0000767
768 bdrv_get_geometry(bs, &nb_sectors);
769
770 ret = bdrv_read(bs, 0, buf, 1);
771 if (ret < 0)
772 return -1;
773 /* test msdos magic */
774 if (buf[510] != 0x55 || buf[511] != 0xaa)
775 return -1;
776 for(i = 0; i < 4; i++) {
777 p = ((struct partition *)(buf + 0x1be)) + i;
778 nr_sects = le32_to_cpu(p->nr_sects);
779 if (nr_sects && p->end_head) {
780 /* We make the assumption that the partition terminates on
781 a cylinder boundary */
782 heads = p->end_head + 1;
783 sectors = p->end_sector & 63;
784 if (sectors == 0)
785 continue;
786 cylinders = nb_sectors / (heads * sectors);
787 if (cylinders < 1 || cylinders > 16383)
788 continue;
789 *pheads = heads;
790 *psectors = sectors;
791 *pcylinders = cylinders;
792#if 0
793 printf("guessed geometry: LCHS=%d %d %d\n",
794 cylinders, heads, sectors);
795#endif
796 return 0;
797 }
798 }
799 return -1;
800}
801
802void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
803{
804 int translation, lba_detected = 0;
805 int cylinders, heads, secs;
blueswir1a38131b2008-12-05 17:56:40 +0000806 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +0000807
808 /* if a geometry hint is available, use it */
809 bdrv_get_geometry(bs, &nb_sectors);
810 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
811 translation = bdrv_get_translation_hint(bs);
812 if (cylinders != 0) {
813 *pcyls = cylinders;
814 *pheads = heads;
815 *psecs = secs;
816 } else {
817 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
818 if (heads > 16) {
819 /* if heads > 16, it means that a BIOS LBA
820 translation was active, so the default
821 hardware geometry is OK */
822 lba_detected = 1;
823 goto default_geometry;
824 } else {
825 *pcyls = cylinders;
826 *pheads = heads;
827 *psecs = secs;
828 /* disable any translation to be in sync with
829 the logical geometry */
830 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
831 bdrv_set_translation_hint(bs,
832 BIOS_ATA_TRANSLATION_NONE);
833 }
834 }
835 } else {
836 default_geometry:
837 /* if no geometry, use a standard physical disk geometry */
838 cylinders = nb_sectors / (16 * 63);
839
840 if (cylinders > 16383)
841 cylinders = 16383;
842 else if (cylinders < 2)
843 cylinders = 2;
844 *pcyls = cylinders;
845 *pheads = 16;
846 *psecs = 63;
847 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
848 if ((*pcyls * *pheads) <= 131072) {
849 bdrv_set_translation_hint(bs,
850 BIOS_ATA_TRANSLATION_LARGE);
851 } else {
852 bdrv_set_translation_hint(bs,
853 BIOS_ATA_TRANSLATION_LBA);
854 }
855 }
856 }
857 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
858 }
859}
860
ths5fafdf22007-09-16 21:08:06 +0000861void bdrv_set_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000862 int cyls, int heads, int secs)
863{
864 bs->cyls = cyls;
865 bs->heads = heads;
866 bs->secs = secs;
867}
868
869void bdrv_set_type_hint(BlockDriverState *bs, int type)
870{
871 bs->type = type;
872 bs->removable = ((type == BDRV_TYPE_CDROM ||
873 type == BDRV_TYPE_FLOPPY));
874}
875
bellard46d47672004-11-16 01:45:27 +0000876void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
877{
878 bs->translation = translation;
879}
880
ths5fafdf22007-09-16 21:08:06 +0000881void bdrv_get_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000882 int *pcyls, int *pheads, int *psecs)
883{
884 *pcyls = bs->cyls;
885 *pheads = bs->heads;
886 *psecs = bs->secs;
887}
888
889int bdrv_get_type_hint(BlockDriverState *bs)
890{
891 return bs->type;
892}
893
bellard46d47672004-11-16 01:45:27 +0000894int bdrv_get_translation_hint(BlockDriverState *bs)
895{
896 return bs->translation;
897}
898
bellardb3380822004-03-14 21:38:54 +0000899int bdrv_is_removable(BlockDriverState *bs)
900{
901 return bs->removable;
902}
903
904int bdrv_is_read_only(BlockDriverState *bs)
905{
906 return bs->read_only;
907}
908
ths985a03b2007-12-24 16:10:43 +0000909int bdrv_is_sg(BlockDriverState *bs)
910{
911 return bs->sg;
912}
913
bellard19cb3732006-08-19 11:45:59 +0000914/* XXX: no longer used */
ths5fafdf22007-09-16 21:08:06 +0000915void bdrv_set_change_cb(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000916 void (*change_cb)(void *opaque), void *opaque)
917{
918 bs->change_cb = change_cb;
919 bs->change_opaque = opaque;
920}
921
bellardea2384d2004-08-01 21:59:26 +0000922int bdrv_is_encrypted(BlockDriverState *bs)
923{
924 if (bs->backing_hd && bs->backing_hd->encrypted)
925 return 1;
926 return bs->encrypted;
927}
928
aliguoric0f4ce72009-03-05 23:01:01 +0000929int bdrv_key_required(BlockDriverState *bs)
930{
931 BlockDriverState *backing_hd = bs->backing_hd;
932
933 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
934 return 1;
935 return (bs->encrypted && !bs->valid_key);
936}
937
bellardea2384d2004-08-01 21:59:26 +0000938int bdrv_set_key(BlockDriverState *bs, const char *key)
939{
940 int ret;
941 if (bs->backing_hd && bs->backing_hd->encrypted) {
942 ret = bdrv_set_key(bs->backing_hd, key);
943 if (ret < 0)
944 return ret;
945 if (!bs->encrypted)
946 return 0;
947 }
948 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
949 return -1;
aliguoric0f4ce72009-03-05 23:01:01 +0000950 ret = bs->drv->bdrv_set_key(bs, key);
aliguoribb5fc202009-03-05 23:01:15 +0000951 if (ret < 0) {
952 bs->valid_key = 0;
953 } else if (!bs->valid_key) {
954 bs->valid_key = 1;
955 /* call the change callback now, we skipped it on open */
956 bs->media_changed = 1;
957 if (bs->change_cb)
958 bs->change_cb(bs->change_opaque);
959 }
aliguoric0f4ce72009-03-05 23:01:01 +0000960 return ret;
bellardea2384d2004-08-01 21:59:26 +0000961}
962
963void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
964{
bellard19cb3732006-08-19 11:45:59 +0000965 if (!bs->drv) {
bellardea2384d2004-08-01 21:59:26 +0000966 buf[0] = '\0';
967 } else {
968 pstrcpy(buf, buf_size, bs->drv->format_name);
969 }
970}
971
ths5fafdf22007-09-16 21:08:06 +0000972void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
bellardea2384d2004-08-01 21:59:26 +0000973 void *opaque)
974{
975 BlockDriver *drv;
976
977 for (drv = first_drv; drv != NULL; drv = drv->next) {
978 it(opaque, drv->format_name);
979 }
980}
981
bellardb3380822004-03-14 21:38:54 +0000982BlockDriverState *bdrv_find(const char *name)
983{
984 BlockDriverState *bs;
985
986 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
987 if (!strcmp(name, bs->device_name))
988 return bs;
989 }
990 return NULL;
991}
992
aliguori51de9762009-03-05 23:00:43 +0000993void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
bellard81d09122004-07-14 17:21:37 +0000994{
995 BlockDriverState *bs;
996
997 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori51de9762009-03-05 23:00:43 +0000998 it(opaque, bs);
bellard81d09122004-07-14 17:21:37 +0000999 }
1000}
1001
bellardea2384d2004-08-01 21:59:26 +00001002const char *bdrv_get_device_name(BlockDriverState *bs)
1003{
1004 return bs->device_name;
1005}
1006
pbrook7a6cba62006-06-04 11:39:07 +00001007void bdrv_flush(BlockDriverState *bs)
1008{
aliguori081501d2009-03-29 01:31:51 +00001009 if (!bs->drv)
1010 return;
pbrook7a6cba62006-06-04 11:39:07 +00001011 if (bs->drv->bdrv_flush)
1012 bs->drv->bdrv_flush(bs);
1013 if (bs->backing_hd)
1014 bdrv_flush(bs->backing_hd);
1015}
1016
aliguoric6ca28d2008-10-06 13:55:43 +00001017void bdrv_flush_all(void)
1018{
1019 BlockDriverState *bs;
1020
1021 for (bs = bdrv_first; bs != NULL; bs = bs->next)
1022 if (bs->drv && !bdrv_is_read_only(bs) &&
1023 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1024 bdrv_flush(bs);
1025}
1026
thsf58c7b32008-06-05 21:53:49 +00001027/*
1028 * Returns true iff the specified sector is present in the disk image. Drivers
1029 * not implementing the functionality are assumed to not support backing files,
1030 * hence all their sectors are reported as allocated.
1031 *
1032 * 'pnum' is set to the number of sectors (including and immediately following
1033 * the specified sector) that are known to be in the same
1034 * allocated/unallocated state.
1035 *
1036 * 'nb_sectors' is the max value 'pnum' should be set to.
1037 */
1038int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1039 int *pnum)
1040{
1041 int64_t n;
1042 if (!bs->drv->bdrv_is_allocated) {
1043 if (sector_num >= bs->total_sectors) {
1044 *pnum = 0;
1045 return 0;
1046 }
1047 n = bs->total_sectors - sector_num;
1048 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1049 return 1;
1050 }
1051 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1052}
1053
aliguori376253e2009-03-05 23:01:23 +00001054void bdrv_info(Monitor *mon)
bellardb3380822004-03-14 21:38:54 +00001055{
1056 BlockDriverState *bs;
1057
1058 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori376253e2009-03-05 23:01:23 +00001059 monitor_printf(mon, "%s:", bs->device_name);
1060 monitor_printf(mon, " type=");
bellardb3380822004-03-14 21:38:54 +00001061 switch(bs->type) {
1062 case BDRV_TYPE_HD:
aliguori376253e2009-03-05 23:01:23 +00001063 monitor_printf(mon, "hd");
bellardb3380822004-03-14 21:38:54 +00001064 break;
1065 case BDRV_TYPE_CDROM:
aliguori376253e2009-03-05 23:01:23 +00001066 monitor_printf(mon, "cdrom");
bellardb3380822004-03-14 21:38:54 +00001067 break;
1068 case BDRV_TYPE_FLOPPY:
aliguori376253e2009-03-05 23:01:23 +00001069 monitor_printf(mon, "floppy");
bellardb3380822004-03-14 21:38:54 +00001070 break;
1071 }
aliguori376253e2009-03-05 23:01:23 +00001072 monitor_printf(mon, " removable=%d", bs->removable);
bellardb3380822004-03-14 21:38:54 +00001073 if (bs->removable) {
aliguori376253e2009-03-05 23:01:23 +00001074 monitor_printf(mon, " locked=%d", bs->locked);
bellardb3380822004-03-14 21:38:54 +00001075 }
bellard19cb3732006-08-19 11:45:59 +00001076 if (bs->drv) {
aliguori376253e2009-03-05 23:01:23 +00001077 monitor_printf(mon, " file=");
1078 monitor_print_filename(mon, bs->filename);
thsfef30742006-12-22 14:11:32 +00001079 if (bs->backing_file[0] != '\0') {
aliguori376253e2009-03-05 23:01:23 +00001080 monitor_printf(mon, " backing_file=");
1081 monitor_print_filename(mon, bs->backing_file);
1082 }
1083 monitor_printf(mon, " ro=%d", bs->read_only);
1084 monitor_printf(mon, " drv=%s", bs->drv->format_name);
1085 monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs));
bellardb3380822004-03-14 21:38:54 +00001086 } else {
aliguori376253e2009-03-05 23:01:23 +00001087 monitor_printf(mon, " [not inserted]");
bellardb3380822004-03-14 21:38:54 +00001088 }
aliguori376253e2009-03-05 23:01:23 +00001089 monitor_printf(mon, "\n");
bellardb3380822004-03-14 21:38:54 +00001090 }
1091}
thsa36e69d2007-12-02 05:18:19 +00001092
1093/* The "info blockstats" command. */
aliguori376253e2009-03-05 23:01:23 +00001094void bdrv_info_stats(Monitor *mon)
thsa36e69d2007-12-02 05:18:19 +00001095{
1096 BlockDriverState *bs;
1097
1098 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori376253e2009-03-05 23:01:23 +00001099 monitor_printf(mon, "%s:"
1100 " rd_bytes=%" PRIu64
1101 " wr_bytes=%" PRIu64
1102 " rd_operations=%" PRIu64
1103 " wr_operations=%" PRIu64
aliguoriebf53fc2009-03-11 20:05:29 +00001104 "\n",
aliguori376253e2009-03-05 23:01:23 +00001105 bs->device_name,
1106 bs->rd_bytes, bs->wr_bytes,
1107 bs->rd_ops, bs->wr_ops);
thsa36e69d2007-12-02 05:18:19 +00001108 }
1109}
bellardea2384d2004-08-01 21:59:26 +00001110
aliguori045df332009-03-05 23:00:48 +00001111const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1112{
1113 if (bs->backing_hd && bs->backing_hd->encrypted)
1114 return bs->backing_file;
1115 else if (bs->encrypted)
1116 return bs->filename;
1117 else
1118 return NULL;
1119}
1120
ths5fafdf22007-09-16 21:08:06 +00001121void bdrv_get_backing_filename(BlockDriverState *bs,
bellard83f64092006-08-01 16:21:11 +00001122 char *filename, int filename_size)
bellardea2384d2004-08-01 21:59:26 +00001123{
bellard83f64092006-08-01 16:21:11 +00001124 if (!bs->backing_hd) {
1125 pstrcpy(filename, filename_size, "");
1126 } else {
1127 pstrcpy(filename, filename_size, bs->backing_file);
1128 }
bellardea2384d2004-08-01 21:59:26 +00001129}
1130
ths5fafdf22007-09-16 21:08:06 +00001131int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
bellardfaea38e2006-08-05 21:31:00 +00001132 const uint8_t *buf, int nb_sectors)
1133{
1134 BlockDriver *drv = bs->drv;
1135 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001136 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001137 if (!drv->bdrv_write_compressed)
1138 return -ENOTSUP;
Kevin Wolffbb7b4e2009-05-08 14:47:24 +02001139 if (bdrv_check_request(bs, sector_num, nb_sectors))
1140 return -EIO;
bellardfaea38e2006-08-05 21:31:00 +00001141 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1142}
ths3b46e622007-09-17 08:09:54 +00001143
bellardfaea38e2006-08-05 21:31:00 +00001144int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1145{
1146 BlockDriver *drv = bs->drv;
1147 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001148 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001149 if (!drv->bdrv_get_info)
1150 return -ENOTSUP;
1151 memset(bdi, 0, sizeof(*bdi));
1152 return drv->bdrv_get_info(bs, bdi);
1153}
1154
aliguori178e08a2009-04-05 19:10:55 +00001155int bdrv_put_buffer(BlockDriverState *bs, const uint8_t *buf, int64_t pos, int size)
1156{
1157 BlockDriver *drv = bs->drv;
1158 if (!drv)
1159 return -ENOMEDIUM;
1160 if (!drv->bdrv_put_buffer)
1161 return -ENOTSUP;
1162 return drv->bdrv_put_buffer(bs, buf, pos, size);
1163}
1164
1165int bdrv_get_buffer(BlockDriverState *bs, uint8_t *buf, int64_t pos, int size)
1166{
1167 BlockDriver *drv = bs->drv;
1168 if (!drv)
1169 return -ENOMEDIUM;
1170 if (!drv->bdrv_get_buffer)
1171 return -ENOTSUP;
1172 return drv->bdrv_get_buffer(bs, buf, pos, size);
1173}
1174
bellardfaea38e2006-08-05 21:31:00 +00001175/**************************************************************/
1176/* handling of snapshots */
1177
ths5fafdf22007-09-16 21:08:06 +00001178int bdrv_snapshot_create(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001179 QEMUSnapshotInfo *sn_info)
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_snapshot_create)
1185 return -ENOTSUP;
1186 return drv->bdrv_snapshot_create(bs, sn_info);
1187}
1188
ths5fafdf22007-09-16 21:08:06 +00001189int bdrv_snapshot_goto(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001190 const char *snapshot_id)
1191{
1192 BlockDriver *drv = bs->drv;
1193 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001194 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001195 if (!drv->bdrv_snapshot_goto)
1196 return -ENOTSUP;
1197 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1198}
1199
1200int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1201{
1202 BlockDriver *drv = bs->drv;
1203 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001204 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001205 if (!drv->bdrv_snapshot_delete)
1206 return -ENOTSUP;
1207 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1208}
1209
ths5fafdf22007-09-16 21:08:06 +00001210int bdrv_snapshot_list(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001211 QEMUSnapshotInfo **psn_info)
1212{
1213 BlockDriver *drv = bs->drv;
1214 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001215 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001216 if (!drv->bdrv_snapshot_list)
1217 return -ENOTSUP;
1218 return drv->bdrv_snapshot_list(bs, psn_info);
1219}
1220
1221#define NB_SUFFIXES 4
1222
1223char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1224{
1225 static const char suffixes[NB_SUFFIXES] = "KMGT";
1226 int64_t base;
1227 int i;
1228
1229 if (size <= 999) {
1230 snprintf(buf, buf_size, "%" PRId64, size);
1231 } else {
1232 base = 1024;
1233 for(i = 0; i < NB_SUFFIXES; i++) {
1234 if (size < (10 * base)) {
ths5fafdf22007-09-16 21:08:06 +00001235 snprintf(buf, buf_size, "%0.1f%c",
bellardfaea38e2006-08-05 21:31:00 +00001236 (double)size / base,
1237 suffixes[i]);
1238 break;
1239 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
ths5fafdf22007-09-16 21:08:06 +00001240 snprintf(buf, buf_size, "%" PRId64 "%c",
bellardfaea38e2006-08-05 21:31:00 +00001241 ((size + (base >> 1)) / base),
1242 suffixes[i]);
1243 break;
1244 }
1245 base = base * 1024;
1246 }
1247 }
1248 return buf;
1249}
1250
1251char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1252{
1253 char buf1[128], date_buf[128], clock_buf[128];
bellard3b9f94e2007-01-07 17:27:07 +00001254#ifdef _WIN32
1255 struct tm *ptm;
1256#else
bellardfaea38e2006-08-05 21:31:00 +00001257 struct tm tm;
bellard3b9f94e2007-01-07 17:27:07 +00001258#endif
bellardfaea38e2006-08-05 21:31:00 +00001259 time_t ti;
1260 int64_t secs;
1261
1262 if (!sn) {
ths5fafdf22007-09-16 21:08:06 +00001263 snprintf(buf, buf_size,
1264 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001265 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1266 } else {
1267 ti = sn->date_sec;
bellard3b9f94e2007-01-07 17:27:07 +00001268#ifdef _WIN32
1269 ptm = localtime(&ti);
1270 strftime(date_buf, sizeof(date_buf),
1271 "%Y-%m-%d %H:%M:%S", ptm);
1272#else
bellardfaea38e2006-08-05 21:31:00 +00001273 localtime_r(&ti, &tm);
1274 strftime(date_buf, sizeof(date_buf),
1275 "%Y-%m-%d %H:%M:%S", &tm);
bellard3b9f94e2007-01-07 17:27:07 +00001276#endif
bellardfaea38e2006-08-05 21:31:00 +00001277 secs = sn->vm_clock_nsec / 1000000000;
1278 snprintf(clock_buf, sizeof(clock_buf),
1279 "%02d:%02d:%02d.%03d",
1280 (int)(secs / 3600),
1281 (int)((secs / 60) % 60),
ths5fafdf22007-09-16 21:08:06 +00001282 (int)(secs % 60),
bellardfaea38e2006-08-05 21:31:00 +00001283 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1284 snprintf(buf, buf_size,
ths5fafdf22007-09-16 21:08:06 +00001285 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001286 sn->id_str, sn->name,
1287 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1288 date_buf,
1289 clock_buf);
1290 }
1291 return buf;
1292}
1293
bellardea2384d2004-08-01 21:59:26 +00001294
bellard83f64092006-08-01 16:21:11 +00001295/**************************************************************/
1296/* async I/Os */
1297
aliguori3b69e4b2009-01-22 16:59:24 +00001298BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
aliguorif141eaf2009-04-07 18:43:24 +00001299 QEMUIOVector *qiov, int nb_sectors,
aliguori3b69e4b2009-01-22 16:59:24 +00001300 BlockDriverCompletionFunc *cb, void *opaque)
1301{
bellard83f64092006-08-01 16:21:11 +00001302 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001303 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001304
bellard19cb3732006-08-19 11:45:59 +00001305 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001306 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001307 if (bdrv_check_request(bs, sector_num, nb_sectors))
1308 return NULL;
ths3b46e622007-09-17 08:09:54 +00001309
aliguorif141eaf2009-04-07 18:43:24 +00001310 ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
1311 cb, opaque);
thsa36e69d2007-12-02 05:18:19 +00001312
1313 if (ret) {
1314 /* Update stats even though technically transfer has not happened. */
1315 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1316 bs->rd_ops ++;
1317 }
1318
1319 return ret;
bellard83f64092006-08-01 16:21:11 +00001320}
1321
aliguorif141eaf2009-04-07 18:43:24 +00001322BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1323 QEMUIOVector *qiov, int nb_sectors,
1324 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001325{
bellard83f64092006-08-01 16:21:11 +00001326 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001327 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001328
bellard19cb3732006-08-19 11:45:59 +00001329 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001330 return NULL;
bellard83f64092006-08-01 16:21:11 +00001331 if (bs->read_only)
pbrookce1a14d2006-08-07 02:38:06 +00001332 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001333 if (bdrv_check_request(bs, sector_num, nb_sectors))
1334 return NULL;
bellard83f64092006-08-01 16:21:11 +00001335
aliguorif141eaf2009-04-07 18:43:24 +00001336 ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
1337 cb, opaque);
thsa36e69d2007-12-02 05:18:19 +00001338
1339 if (ret) {
1340 /* Update stats even though technically transfer has not happened. */
1341 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1342 bs->wr_ops ++;
1343 }
1344
1345 return ret;
bellard83f64092006-08-01 16:21:11 +00001346}
1347
1348void bdrv_aio_cancel(BlockDriverAIOCB *acb)
pbrookce1a14d2006-08-07 02:38:06 +00001349{
aliguori6bbff9a2009-03-20 18:25:59 +00001350 acb->pool->cancel(acb);
bellard83f64092006-08-01 16:21:11 +00001351}
1352
pbrookce1a14d2006-08-07 02:38:06 +00001353
bellard83f64092006-08-01 16:21:11 +00001354/**************************************************************/
1355/* async block device emulation */
1356
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02001357typedef struct BlockDriverAIOCBSync {
1358 BlockDriverAIOCB common;
1359 QEMUBH *bh;
1360 int ret;
1361 /* vector translation state */
1362 QEMUIOVector *qiov;
1363 uint8_t *bounce;
1364 int is_write;
1365} BlockDriverAIOCBSync;
1366
1367static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1368{
1369 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1370 qemu_bh_cancel(acb->bh);
1371 qemu_aio_release(acb);
1372}
1373
1374static AIOPool bdrv_em_aio_pool = {
1375 .aiocb_size = sizeof(BlockDriverAIOCBSync),
1376 .cancel = bdrv_aio_cancel_em,
1377};
1378
bellard83f64092006-08-01 16:21:11 +00001379static void bdrv_aio_bh_cb(void *opaque)
bellardbeac80c2006-06-26 20:08:57 +00001380{
pbrookce1a14d2006-08-07 02:38:06 +00001381 BlockDriverAIOCBSync *acb = opaque;
aliguorif141eaf2009-04-07 18:43:24 +00001382
aliguorif141eaf2009-04-07 18:43:24 +00001383 if (!acb->is_write)
1384 qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
aliguoriceb42de2009-04-07 18:43:28 +00001385 qemu_vfree(acb->bounce);
pbrookce1a14d2006-08-07 02:38:06 +00001386 acb->common.cb(acb->common.opaque, acb->ret);
aliguorif141eaf2009-04-07 18:43:24 +00001387
pbrookce1a14d2006-08-07 02:38:06 +00001388 qemu_aio_release(acb);
bellardbeac80c2006-06-26 20:08:57 +00001389}
bellardbeac80c2006-06-26 20:08:57 +00001390
aliguorif141eaf2009-04-07 18:43:24 +00001391static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
1392 int64_t sector_num,
1393 QEMUIOVector *qiov,
1394 int nb_sectors,
1395 BlockDriverCompletionFunc *cb,
1396 void *opaque,
1397 int is_write)
1398
bellardea2384d2004-08-01 21:59:26 +00001399{
pbrookce1a14d2006-08-07 02:38:06 +00001400 BlockDriverAIOCBSync *acb;
pbrookce1a14d2006-08-07 02:38:06 +00001401
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02001402 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
aliguorif141eaf2009-04-07 18:43:24 +00001403 acb->is_write = is_write;
1404 acb->qiov = qiov;
aliguorie268ca52009-04-22 20:20:00 +00001405 acb->bounce = qemu_blockalign(bs, qiov->size);
aliguorif141eaf2009-04-07 18:43:24 +00001406
pbrookce1a14d2006-08-07 02:38:06 +00001407 if (!acb->bh)
1408 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
aliguorif141eaf2009-04-07 18:43:24 +00001409
1410 if (is_write) {
1411 qemu_iovec_to_buffer(acb->qiov, acb->bounce);
1412 acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
1413 } else {
1414 acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
1415 }
1416
pbrookce1a14d2006-08-07 02:38:06 +00001417 qemu_bh_schedule(acb->bh);
aliguorif141eaf2009-04-07 18:43:24 +00001418
pbrookce1a14d2006-08-07 02:38:06 +00001419 return &acb->common;
pbrook7a6cba62006-06-04 11:39:07 +00001420}
1421
aliguorif141eaf2009-04-07 18:43:24 +00001422static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
1423 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
pbrookce1a14d2006-08-07 02:38:06 +00001424 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001425{
aliguorif141eaf2009-04-07 18:43:24 +00001426 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
bellard83f64092006-08-01 16:21:11 +00001427}
1428
aliguorif141eaf2009-04-07 18:43:24 +00001429static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
1430 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
1431 BlockDriverCompletionFunc *cb, void *opaque)
1432{
1433 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
1434}
1435
bellard83f64092006-08-01 16:21:11 +00001436/**************************************************************/
1437/* sync block device emulation */
1438
1439static void bdrv_rw_em_cb(void *opaque, int ret)
1440{
1441 *(int *)opaque = ret;
1442}
1443
1444#define NOT_DONE 0x7fffffff
1445
ths5fafdf22007-09-16 21:08:06 +00001446static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +00001447 uint8_t *buf, int nb_sectors)
1448{
pbrookce1a14d2006-08-07 02:38:06 +00001449 int async_ret;
1450 BlockDriverAIOCB *acb;
aliguorif141eaf2009-04-07 18:43:24 +00001451 struct iovec iov;
1452 QEMUIOVector qiov;
bellard83f64092006-08-01 16:21:11 +00001453
bellard83f64092006-08-01 16:21:11 +00001454 async_ret = NOT_DONE;
blueswir13f4cb3d2009-04-13 16:31:01 +00001455 iov.iov_base = (void *)buf;
aliguorif141eaf2009-04-07 18:43:24 +00001456 iov.iov_len = nb_sectors * 512;
1457 qemu_iovec_init_external(&qiov, &iov, 1);
1458 acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
1459 bdrv_rw_em_cb, &async_ret);
aliguoribaf35cb2008-09-10 15:45:19 +00001460 if (acb == NULL)
pbrookce1a14d2006-08-07 02:38:06 +00001461 return -1;
aliguoribaf35cb2008-09-10 15:45:19 +00001462
bellard83f64092006-08-01 16:21:11 +00001463 while (async_ret == NOT_DONE) {
1464 qemu_aio_wait();
1465 }
aliguoribaf35cb2008-09-10 15:45:19 +00001466
bellard83f64092006-08-01 16:21:11 +00001467 return async_ret;
1468}
1469
1470static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1471 const uint8_t *buf, int nb_sectors)
1472{
pbrookce1a14d2006-08-07 02:38:06 +00001473 int async_ret;
1474 BlockDriverAIOCB *acb;
aliguorif141eaf2009-04-07 18:43:24 +00001475 struct iovec iov;
1476 QEMUIOVector qiov;
bellard83f64092006-08-01 16:21:11 +00001477
bellard83f64092006-08-01 16:21:11 +00001478 async_ret = NOT_DONE;
aliguorif141eaf2009-04-07 18:43:24 +00001479 iov.iov_base = (void *)buf;
1480 iov.iov_len = nb_sectors * 512;
1481 qemu_iovec_init_external(&qiov, &iov, 1);
1482 acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
1483 bdrv_rw_em_cb, &async_ret);
aliguoribaf35cb2008-09-10 15:45:19 +00001484 if (acb == NULL)
pbrookce1a14d2006-08-07 02:38:06 +00001485 return -1;
bellard83f64092006-08-01 16:21:11 +00001486 while (async_ret == NOT_DONE) {
1487 qemu_aio_wait();
1488 }
bellard83f64092006-08-01 16:21:11 +00001489 return async_ret;
1490}
bellardea2384d2004-08-01 21:59:26 +00001491
1492void bdrv_init(void)
1493{
Anthony Liguori5efa9d52009-05-09 17:03:42 -05001494 module_call_init(MODULE_INIT_BLOCK);
bellardea2384d2004-08-01 21:59:26 +00001495}
pbrookce1a14d2006-08-07 02:38:06 +00001496
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02001497void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
1498 BlockDriverCompletionFunc *cb, void *opaque)
aliguori6bbff9a2009-03-20 18:25:59 +00001499{
pbrookce1a14d2006-08-07 02:38:06 +00001500 BlockDriverAIOCB *acb;
1501
aliguori6bbff9a2009-03-20 18:25:59 +00001502 if (pool->free_aiocb) {
1503 acb = pool->free_aiocb;
1504 pool->free_aiocb = acb->next;
pbrookce1a14d2006-08-07 02:38:06 +00001505 } else {
aliguori6bbff9a2009-03-20 18:25:59 +00001506 acb = qemu_mallocz(pool->aiocb_size);
1507 acb->pool = pool;
pbrookce1a14d2006-08-07 02:38:06 +00001508 }
1509 acb->bs = bs;
1510 acb->cb = cb;
1511 acb->opaque = opaque;
1512 return acb;
1513}
1514
1515void qemu_aio_release(void *p)
1516{
aliguori6bbff9a2009-03-20 18:25:59 +00001517 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
1518 AIOPool *pool = acb->pool;
1519 acb->next = pool->free_aiocb;
1520 pool->free_aiocb = acb;
pbrookce1a14d2006-08-07 02:38:06 +00001521}
bellard19cb3732006-08-19 11:45:59 +00001522
1523/**************************************************************/
1524/* removable device support */
1525
1526/**
1527 * Return TRUE if the media is present
1528 */
1529int bdrv_is_inserted(BlockDriverState *bs)
1530{
1531 BlockDriver *drv = bs->drv;
1532 int ret;
1533 if (!drv)
1534 return 0;
1535 if (!drv->bdrv_is_inserted)
1536 return 1;
1537 ret = drv->bdrv_is_inserted(bs);
1538 return ret;
1539}
1540
1541/**
1542 * Return TRUE if the media changed since the last call to this
ths5fafdf22007-09-16 21:08:06 +00001543 * function. It is currently only used for floppy disks
bellard19cb3732006-08-19 11:45:59 +00001544 */
1545int bdrv_media_changed(BlockDriverState *bs)
1546{
1547 BlockDriver *drv = bs->drv;
1548 int ret;
1549
1550 if (!drv || !drv->bdrv_media_changed)
1551 ret = -ENOTSUP;
1552 else
1553 ret = drv->bdrv_media_changed(bs);
1554 if (ret == -ENOTSUP)
1555 ret = bs->media_changed;
1556 bs->media_changed = 0;
1557 return ret;
1558}
1559
1560/**
1561 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1562 */
1563void bdrv_eject(BlockDriverState *bs, int eject_flag)
1564{
1565 BlockDriver *drv = bs->drv;
1566 int ret;
1567
1568 if (!drv || !drv->bdrv_eject) {
1569 ret = -ENOTSUP;
1570 } else {
1571 ret = drv->bdrv_eject(bs, eject_flag);
1572 }
1573 if (ret == -ENOTSUP) {
1574 if (eject_flag)
1575 bdrv_close(bs);
1576 }
1577}
1578
1579int bdrv_is_locked(BlockDriverState *bs)
1580{
1581 return bs->locked;
1582}
1583
1584/**
1585 * Lock or unlock the media (if it is locked, the user won't be able
1586 * to eject it manually).
1587 */
1588void bdrv_set_locked(BlockDriverState *bs, int locked)
1589{
1590 BlockDriver *drv = bs->drv;
1591
1592 bs->locked = locked;
1593 if (drv && drv->bdrv_set_locked) {
1594 drv->bdrv_set_locked(bs, locked);
1595 }
1596}
ths985a03b2007-12-24 16:10:43 +00001597
1598/* needed for generic scsi interface */
1599
1600int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1601{
1602 BlockDriver *drv = bs->drv;
1603
1604 if (drv && drv->bdrv_ioctl)
1605 return drv->bdrv_ioctl(bs, req, buf);
1606 return -ENOTSUP;
1607}
aliguori7d780662009-03-12 19:57:08 +00001608
aliguori221f7152009-03-28 17:28:41 +00001609BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
1610 unsigned long int req, void *buf,
1611 BlockDriverCompletionFunc *cb, void *opaque)
aliguori7d780662009-03-12 19:57:08 +00001612{
aliguori221f7152009-03-28 17:28:41 +00001613 BlockDriver *drv = bs->drv;
aliguori7d780662009-03-12 19:57:08 +00001614
aliguori221f7152009-03-28 17:28:41 +00001615 if (drv && drv->bdrv_aio_ioctl)
1616 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
1617 return NULL;
aliguori7d780662009-03-12 19:57:08 +00001618}
aliguorie268ca52009-04-22 20:20:00 +00001619
1620void *qemu_blockalign(BlockDriverState *bs, size_t size)
1621{
1622 return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
1623}