blob: 853f0256a7ed99f2d40d7bd83daf708a628adff6 [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
aliguorif141eaf2009-04-07 18:43:24 +000044static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
45 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
aliguoric87c0672009-04-07 18:43:20 +000046 BlockDriverCompletionFunc *cb, void *opaque);
aliguorif141eaf2009-04-07 18:43:24 +000047static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
48 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
pbrookce1a14d2006-08-07 02:38:06 +000049 BlockDriverCompletionFunc *cb, void *opaque);
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +020050static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
51 BlockDriverCompletionFunc *cb, void *opaque);
ths5fafdf22007-09-16 21:08:06 +000052static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +000053 uint8_t *buf, int nb_sectors);
54static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
55 const uint8_t *buf, int nb_sectors);
bellardec530c82006-04-25 22:36:06 +000056
blueswir17ee930d2008-09-17 19:04:14 +000057BlockDriverState *bdrv_first;
58
bellardea2384d2004-08-01 21:59:26 +000059static BlockDriver *first_drv;
60
Markus Armbrustereb852012009-10-27 18:41:44 +010061/* If non-zero, use only whitelisted block drivers */
62static int use_bdrv_whitelist;
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
Markus Armbrustereb852012009-10-27 18:41:44 +0100174static int bdrv_is_whitelisted(BlockDriver *drv)
175{
176 static const char *whitelist[] = {
177 CONFIG_BDRV_WHITELIST
178 };
179 const char **p;
180
181 if (!whitelist[0])
182 return 1; /* no whitelist, anything goes */
183
184 for (p = whitelist; *p; p++) {
185 if (!strcmp(drv->format_name, *p)) {
186 return 1;
187 }
188 }
189 return 0;
190}
191
192BlockDriver *bdrv_find_whitelisted_format(const char *format_name)
193{
194 BlockDriver *drv = bdrv_find_format(format_name);
195 return drv && bdrv_is_whitelisted(drv) ? drv : NULL;
196}
197
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200198int bdrv_create(BlockDriver *drv, const char* filename,
199 QEMUOptionParameter *options)
bellardea2384d2004-08-01 21:59:26 +0000200{
201 if (!drv->bdrv_create)
202 return -ENOTSUP;
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200203
204 return drv->bdrv_create(filename, options);
bellardea2384d2004-08-01 21:59:26 +0000205}
206
bellardd5249392004-08-03 21:14:23 +0000207#ifdef _WIN32
bellard95389c82005-12-18 18:28:15 +0000208void get_tmp_filename(char *filename, int size)
bellardd5249392004-08-03 21:14:23 +0000209{
bellard3b9f94e2007-01-07 17:27:07 +0000210 char temp_dir[MAX_PATH];
ths3b46e622007-09-17 08:09:54 +0000211
bellard3b9f94e2007-01-07 17:27:07 +0000212 GetTempPath(MAX_PATH, temp_dir);
213 GetTempFileName(temp_dir, "qem", 0, filename);
bellardd5249392004-08-03 21:14:23 +0000214}
215#else
bellard95389c82005-12-18 18:28:15 +0000216void get_tmp_filename(char *filename, int size)
bellardea2384d2004-08-01 21:59:26 +0000217{
218 int fd;
blueswir17ccfb2e2008-09-14 06:45:34 +0000219 const char *tmpdir;
bellardd5249392004-08-03 21:14:23 +0000220 /* XXX: race condition possible */
aurel320badc1e2008-03-10 00:05:34 +0000221 tmpdir = getenv("TMPDIR");
222 if (!tmpdir)
223 tmpdir = "/tmp";
224 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
bellardea2384d2004-08-01 21:59:26 +0000225 fd = mkstemp(filename);
226 close(fd);
227}
bellardd5249392004-08-03 21:14:23 +0000228#endif
bellardea2384d2004-08-01 21:59:26 +0000229
bellard19cb3732006-08-19 11:45:59 +0000230#ifdef _WIN32
bellardf45512f2006-08-23 21:40:13 +0000231static int is_windows_drive_prefix(const char *filename)
232{
233 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
234 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
235 filename[1] == ':');
236}
ths3b46e622007-09-17 08:09:54 +0000237
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200238int is_windows_drive(const char *filename)
bellard19cb3732006-08-19 11:45:59 +0000239{
ths5fafdf22007-09-16 21:08:06 +0000240 if (is_windows_drive_prefix(filename) &&
bellardf45512f2006-08-23 21:40:13 +0000241 filename[2] == '\0')
bellard19cb3732006-08-19 11:45:59 +0000242 return 1;
243 if (strstart(filename, "\\\\.\\", NULL) ||
244 strstart(filename, "//./", NULL))
245 return 1;
246 return 0;
247}
248#endif
249
bellard83f64092006-08-01 16:21:11 +0000250static BlockDriver *find_protocol(const char *filename)
251{
252 BlockDriver *drv1;
253 char protocol[128];
Anthony Liguori1cec71e2009-07-02 08:12:26 -0500254 int len;
bellard83f64092006-08-01 16:21:11 +0000255 const char *p;
bellard19cb3732006-08-19 11:45:59 +0000256
257#ifdef _WIN32
bellardf45512f2006-08-23 21:40:13 +0000258 if (is_windows_drive(filename) ||
259 is_windows_drive_prefix(filename))
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500260 return bdrv_find_format("raw");
bellard19cb3732006-08-19 11:45:59 +0000261#endif
Anthony Liguori1cec71e2009-07-02 08:12:26 -0500262 p = strchr(filename, ':');
263 if (!p)
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500264 return bdrv_find_format("raw");
Anthony Liguori1cec71e2009-07-02 08:12:26 -0500265 len = p - filename;
266 if (len > sizeof(protocol) - 1)
267 len = sizeof(protocol) - 1;
268 memcpy(protocol, filename, len);
269 protocol[len] = '\0';
bellard83f64092006-08-01 16:21:11 +0000270 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
ths5fafdf22007-09-16 21:08:06 +0000271 if (drv1->protocol_name &&
bellard83f64092006-08-01 16:21:11 +0000272 !strcmp(drv1->protocol_name, protocol))
273 return drv1;
274 }
275 return NULL;
276}
277
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200278/*
279 * Detect host devices. By convention, /dev/cdrom[N] is always
280 * recognized as a host CDROM.
281 */
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200282static BlockDriver *find_hdev_driver(const char *filename)
283{
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200284 int score_max = 0, score;
285 BlockDriver *drv = NULL, *d;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200286
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200287 for (d = first_drv; d; d = d->next) {
288 if (d->bdrv_probe_device) {
289 score = d->bdrv_probe_device(filename);
290 if (score > score_max) {
291 score_max = score;
292 drv = d;
293 }
294 }
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200295 }
296
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200297 return drv;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200298}
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200299
bellardea2384d2004-08-01 21:59:26 +0000300static BlockDriver *find_image_format(const char *filename)
301{
bellard83f64092006-08-01 16:21:11 +0000302 int ret, score, score_max;
bellardea2384d2004-08-01 21:59:26 +0000303 BlockDriver *drv1, *drv;
bellard83f64092006-08-01 16:21:11 +0000304 uint8_t buf[2048];
305 BlockDriverState *bs;
ths3b46e622007-09-17 08:09:54 +0000306
bellard83f64092006-08-01 16:21:11 +0000307 drv = find_protocol(filename);
bellard19cb3732006-08-19 11:45:59 +0000308 /* no need to test disk image formats for vvfat */
Anthony Liguoric833ab72009-05-22 08:17:55 -0500309 if (drv && strcmp(drv->format_name, "vvfat") == 0)
bellard83f64092006-08-01 16:21:11 +0000310 return drv;
bellard19cb3732006-08-19 11:45:59 +0000311
bellard83f64092006-08-01 16:21:11 +0000312 ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
313 if (ret < 0)
314 return NULL;
315 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
316 bdrv_delete(bs);
317 if (ret < 0) {
318 return NULL;
319 }
320
bellardea2384d2004-08-01 21:59:26 +0000321 score_max = 0;
322 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
bellard83f64092006-08-01 16:21:11 +0000323 if (drv1->bdrv_probe) {
324 score = drv1->bdrv_probe(buf, ret, filename);
325 if (score > score_max) {
326 score_max = score;
327 drv = drv1;
328 }
bellardea2384d2004-08-01 21:59:26 +0000329 }
330 }
331 return drv;
332}
333
bellard83f64092006-08-01 16:21:11 +0000334int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
bellardb3380822004-03-14 21:38:54 +0000335{
bellard83f64092006-08-01 16:21:11 +0000336 BlockDriverState *bs;
337 int ret;
338
339 bs = bdrv_new("");
bellard83f64092006-08-01 16:21:11 +0000340 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
341 if (ret < 0) {
342 bdrv_delete(bs);
343 return ret;
bellard3b0d4f62005-10-30 18:30:10 +0000344 }
aliguori71d07702009-03-03 17:37:16 +0000345 bs->growable = 1;
bellard83f64092006-08-01 16:21:11 +0000346 *pbs = bs;
347 return 0;
bellardea2384d2004-08-01 21:59:26 +0000348}
bellardfc01f7e2003-06-30 10:03:06 +0000349
bellard83f64092006-08-01 16:21:11 +0000350int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
351{
352 return bdrv_open2(bs, filename, flags, NULL);
353}
354
355int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
bellardea2384d2004-08-01 21:59:26 +0000356 BlockDriver *drv)
357{
Naphtali Sprei59f26892009-10-26 16:25:16 +0200358 int ret, open_flags, try_rw;
thseb5c8512007-02-11 15:06:09 +0000359 char tmp_filename[PATH_MAX];
360 char backing_filename[PATH_MAX];
ths3b46e622007-09-17 08:09:54 +0000361
bellardea2384d2004-08-01 21:59:26 +0000362 bs->is_temporary = 0;
363 bs->encrypted = 0;
aliguoric0f4ce72009-03-05 23:01:01 +0000364 bs->valid_key = 0;
aliguorie268ca52009-04-22 20:20:00 +0000365 /* buffer_alignment defaulted to 512, drivers can change this value */
366 bs->buffer_alignment = 512;
bellard712e7872005-04-28 21:09:32 +0000367
bellard83f64092006-08-01 16:21:11 +0000368 if (flags & BDRV_O_SNAPSHOT) {
bellardea2384d2004-08-01 21:59:26 +0000369 BlockDriverState *bs1;
370 int64_t total_size;
aliguori7c96d462008-09-12 17:54:13 +0000371 int is_protocol = 0;
Kevin Wolf91a073a2009-05-27 14:48:06 +0200372 BlockDriver *bdrv_qcow2;
373 QEMUOptionParameter *options;
ths3b46e622007-09-17 08:09:54 +0000374
bellardea2384d2004-08-01 21:59:26 +0000375 /* if snapshot, we create a temporary backing file and open it
376 instead of opening 'filename' directly */
377
378 /* if there is a backing file, use it */
379 bs1 = bdrv_new("");
aliguori5eb45632009-03-28 17:55:10 +0000380 ret = bdrv_open2(bs1, filename, 0, drv);
aliguori51d7c002009-03-05 23:00:29 +0000381 if (ret < 0) {
bellardea2384d2004-08-01 21:59:26 +0000382 bdrv_delete(bs1);
aliguori51d7c002009-03-05 23:00:29 +0000383 return ret;
bellardea2384d2004-08-01 21:59:26 +0000384 }
Jan Kiszka6ea44302009-11-30 18:21:19 +0100385 total_size = bdrv_getlength(bs1) >> BDRV_SECTOR_BITS;
aliguori7c96d462008-09-12 17:54:13 +0000386
387 if (bs1->drv && bs1->drv->protocol_name)
388 is_protocol = 1;
389
bellardea2384d2004-08-01 21:59:26 +0000390 bdrv_delete(bs1);
ths3b46e622007-09-17 08:09:54 +0000391
bellardea2384d2004-08-01 21:59:26 +0000392 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
aliguori7c96d462008-09-12 17:54:13 +0000393
394 /* Real path is meaningless for protocols */
395 if (is_protocol)
396 snprintf(backing_filename, sizeof(backing_filename),
397 "%s", filename);
398 else
399 realpath(filename, backing_filename);
400
Kevin Wolf91a073a2009-05-27 14:48:06 +0200401 bdrv_qcow2 = bdrv_find_format("qcow2");
402 options = parse_option_parameters("", bdrv_qcow2->create_options, NULL);
403
404 set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size * 512);
405 set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename);
406 if (drv) {
407 set_option_parameter(options, BLOCK_OPT_BACKING_FMT,
408 drv->format_name);
409 }
410
411 ret = bdrv_create(bdrv_qcow2, tmp_filename, options);
aliguori51d7c002009-03-05 23:00:29 +0000412 if (ret < 0) {
413 return ret;
bellardea2384d2004-08-01 21:59:26 +0000414 }
Kevin Wolf91a073a2009-05-27 14:48:06 +0200415
bellardea2384d2004-08-01 21:59:26 +0000416 filename = tmp_filename;
Kevin Wolf91a073a2009-05-27 14:48:06 +0200417 drv = bdrv_qcow2;
bellardea2384d2004-08-01 21:59:26 +0000418 bs->is_temporary = 1;
419 }
bellard712e7872005-04-28 21:09:32 +0000420
bellardea2384d2004-08-01 21:59:26 +0000421 pstrcpy(bs->filename, sizeof(bs->filename), filename);
bellard83f64092006-08-01 16:21:11 +0000422 if (flags & BDRV_O_FILE) {
423 drv = find_protocol(filename);
aliguori51d7c002009-03-05 23:00:29 +0000424 } else if (!drv) {
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200425 drv = find_hdev_driver(filename);
426 if (!drv) {
427 drv = find_image_format(filename);
428 }
aliguori51d7c002009-03-05 23:00:29 +0000429 }
430 if (!drv) {
431 ret = -ENOENT;
432 goto unlink_and_fail;
bellardea2384d2004-08-01 21:59:26 +0000433 }
434 bs->drv = drv;
435 bs->opaque = qemu_mallocz(drv->instance_size);
Christoph Hellwige900a7b2009-09-04 19:01:15 +0200436
437 /*
438 * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a
439 * write cache to the guest. We do need the fdatasync to flush
440 * out transactions for block allocations, and we maybe have a
441 * volatile write cache in our backing device to deal with.
442 */
443 if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE))
444 bs->enable_write_cache = 1;
445
bellard83f64092006-08-01 16:21:11 +0000446 /* Note: for compatibility, we open disk image files as RDWR, and
447 RDONLY as fallback */
Naphtali Sprei59f26892009-10-26 16:25:16 +0200448 try_rw = !bs->read_only || bs->is_temporary;
bellard83f64092006-08-01 16:21:11 +0000449 if (!(flags & BDRV_O_FILE))
Naphtali Sprei59f26892009-10-26 16:25:16 +0200450 open_flags = (try_rw ? BDRV_O_RDWR : 0) |
451 (flags & (BDRV_O_CACHE_MASK|BDRV_O_NATIVE_AIO));
bellard83f64092006-08-01 16:21:11 +0000452 else
453 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
Markus Armbrustereb852012009-10-27 18:41:44 +0100454 if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv))
455 ret = -ENOTSUP;
456 else
457 ret = drv->bdrv_open(bs, filename, open_flags);
aurel32a0a83532008-10-13 21:08:34 +0000458 if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
Anthony Liguori1cec71e2009-07-02 08:12:26 -0500459 ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
bellard83f64092006-08-01 16:21:11 +0000460 bs->read_only = 1;
461 }
bellardea2384d2004-08-01 21:59:26 +0000462 if (ret < 0) {
463 qemu_free(bs->opaque);
bellard6b21b972006-08-23 21:14:37 +0000464 bs->opaque = NULL;
465 bs->drv = NULL;
aliguori51d7c002009-03-05 23:00:29 +0000466 unlink_and_fail:
467 if (bs->is_temporary)
468 unlink(filename);
bellard83f64092006-08-01 16:21:11 +0000469 return ret;
bellardea2384d2004-08-01 21:59:26 +0000470 }
bellardd15a7712006-08-06 13:35:09 +0000471 if (drv->bdrv_getlength) {
Jan Kiszka6ea44302009-11-30 18:21:19 +0100472 bs->total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
bellardd15a7712006-08-06 13:35:09 +0000473 }
bellardea2384d2004-08-01 21:59:26 +0000474#ifndef _WIN32
475 if (bs->is_temporary) {
476 unlink(filename);
477 }
478#endif
bellard83f64092006-08-01 16:21:11 +0000479 if (bs->backing_file[0] != '\0') {
bellardea2384d2004-08-01 21:59:26 +0000480 /* if there is a backing file, use it */
aliguori5eb45632009-03-28 17:55:10 +0000481 BlockDriver *back_drv = NULL;
bellardea2384d2004-08-01 21:59:26 +0000482 bs->backing_hd = bdrv_new("");
Naphtali Sprei59f26892009-10-26 16:25:16 +0200483 /* pass on read_only property to the backing_hd */
484 bs->backing_hd->read_only = bs->read_only;
bellard83f64092006-08-01 16:21:11 +0000485 path_combine(backing_filename, sizeof(backing_filename),
486 filename, bs->backing_file);
aliguori5eb45632009-03-28 17:55:10 +0000487 if (bs->backing_format[0] != '\0')
488 back_drv = bdrv_find_format(bs->backing_format);
489 ret = bdrv_open2(bs->backing_hd, backing_filename, open_flags,
490 back_drv);
aliguori51d7c002009-03-05 23:00:29 +0000491 if (ret < 0) {
492 bdrv_close(bs);
493 return ret;
494 }
bellardea2384d2004-08-01 21:59:26 +0000495 }
496
aliguoribb5fc202009-03-05 23:01:15 +0000497 if (!bdrv_key_required(bs)) {
498 /* call the change callback */
499 bs->media_changed = 1;
500 if (bs->change_cb)
501 bs->change_cb(bs->change_opaque);
502 }
bellardb3380822004-03-14 21:38:54 +0000503 return 0;
bellardfc01f7e2003-06-30 10:03:06 +0000504}
505
506void bdrv_close(BlockDriverState *bs)
507{
bellard19cb3732006-08-19 11:45:59 +0000508 if (bs->drv) {
bellardea2384d2004-08-01 21:59:26 +0000509 if (bs->backing_hd)
510 bdrv_delete(bs->backing_hd);
511 bs->drv->bdrv_close(bs);
512 qemu_free(bs->opaque);
513#ifdef _WIN32
514 if (bs->is_temporary) {
515 unlink(bs->filename);
516 }
bellard67b915a2004-03-31 23:37:16 +0000517#endif
bellardea2384d2004-08-01 21:59:26 +0000518 bs->opaque = NULL;
519 bs->drv = NULL;
bellardb3380822004-03-14 21:38:54 +0000520
521 /* call the change callback */
bellard19cb3732006-08-19 11:45:59 +0000522 bs->media_changed = 1;
bellardb3380822004-03-14 21:38:54 +0000523 if (bs->change_cb)
524 bs->change_cb(bs->change_opaque);
525 }
526}
527
528void bdrv_delete(BlockDriverState *bs)
529{
aurel3234c6f052008-04-08 19:51:21 +0000530 BlockDriverState **pbs;
531
532 pbs = &bdrv_first;
533 while (*pbs != bs && *pbs != NULL)
534 pbs = &(*pbs)->next;
535 if (*pbs == bs)
536 *pbs = bs->next;
537
bellardb3380822004-03-14 21:38:54 +0000538 bdrv_close(bs);
539 qemu_free(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000540}
541
aliguorie97fc192009-04-21 23:11:50 +0000542/*
543 * Run consistency checks on an image
544 *
545 * Returns the number of errors or -errno when an internal error occurs
546 */
547int bdrv_check(BlockDriverState *bs)
548{
549 if (bs->drv->bdrv_check == NULL) {
550 return -ENOTSUP;
551 }
552
553 return bs->drv->bdrv_check(bs);
554}
555
bellard33e39632003-07-06 17:15:21 +0000556/* commit COW file into the raw image */
557int bdrv_commit(BlockDriverState *bs)
558{
bellard19cb3732006-08-19 11:45:59 +0000559 BlockDriver *drv = bs->drv;
bellard83f64092006-08-01 16:21:11 +0000560 int64_t i, total_sectors;
bellardea2384d2004-08-01 21:59:26 +0000561 int n, j;
562 unsigned char sector[512];
bellard33e39632003-07-06 17:15:21 +0000563
bellard19cb3732006-08-19 11:45:59 +0000564 if (!drv)
565 return -ENOMEDIUM;
bellard33e39632003-07-06 17:15:21 +0000566
567 if (bs->read_only) {
bellardea2384d2004-08-01 21:59:26 +0000568 return -EACCES;
bellard33e39632003-07-06 17:15:21 +0000569 }
570
bellardea2384d2004-08-01 21:59:26 +0000571 if (!bs->backing_hd) {
572 return -ENOTSUP;
bellard33e39632003-07-06 17:15:21 +0000573 }
bellardea2384d2004-08-01 21:59:26 +0000574
Jan Kiszka6ea44302009-11-30 18:21:19 +0100575 total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000576 for (i = 0; i < total_sectors;) {
bellard19cb3732006-08-19 11:45:59 +0000577 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
bellardea2384d2004-08-01 21:59:26 +0000578 for(j = 0; j < n; j++) {
579 if (bdrv_read(bs, i, sector, 1) != 0) {
580 return -EIO;
581 }
582
583 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
584 return -EIO;
585 }
586 i++;
587 }
588 } else {
589 i += n;
590 }
591 }
bellard95389c82005-12-18 18:28:15 +0000592
bellard19cb3732006-08-19 11:45:59 +0000593 if (drv->bdrv_make_empty)
594 return drv->bdrv_make_empty(bs);
bellard95389c82005-12-18 18:28:15 +0000595
bellard33e39632003-07-06 17:15:21 +0000596 return 0;
597}
598
aliguori71d07702009-03-03 17:37:16 +0000599static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
600 size_t size)
601{
602 int64_t len;
603
604 if (!bdrv_is_inserted(bs))
605 return -ENOMEDIUM;
606
607 if (bs->growable)
608 return 0;
609
610 len = bdrv_getlength(bs);
611
Kevin Wolffbb7b4e2009-05-08 14:47:24 +0200612 if (offset < 0)
613 return -EIO;
614
615 if ((offset > len) || (len - offset < size))
aliguori71d07702009-03-03 17:37:16 +0000616 return -EIO;
617
618 return 0;
619}
620
621static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
622 int nb_sectors)
623{
aliguori999dec52009-03-29 01:31:48 +0000624 return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512);
aliguori71d07702009-03-03 17:37:16 +0000625}
626
bellard19cb3732006-08-19 11:45:59 +0000627/* return < 0 if error. See bdrv_write() for the return codes */
ths5fafdf22007-09-16 21:08:06 +0000628int bdrv_read(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000629 uint8_t *buf, int nb_sectors)
630{
bellardea2384d2004-08-01 21:59:26 +0000631 BlockDriver *drv = bs->drv;
632
bellard19cb3732006-08-19 11:45:59 +0000633 if (!drv)
634 return -ENOMEDIUM;
aliguori71d07702009-03-03 17:37:16 +0000635 if (bdrv_check_request(bs, sector_num, nb_sectors))
636 return -EIO;
bellardb3380822004-03-14 21:38:54 +0000637
aliguorieda578e2009-03-12 19:57:16 +0000638 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
bellardfc01f7e2003-06-30 10:03:06 +0000639}
640
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +0200641static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num,
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100642 int nb_sectors, int dirty)
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +0200643{
644 int64_t start, end;
Jan Kiszkac6d22832009-11-30 18:21:20 +0100645 unsigned long val, idx, bit;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100646
Jan Kiszka6ea44302009-11-30 18:21:19 +0100647 start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkac6d22832009-11-30 18:21:20 +0100648 end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100649
650 for (; start <= end; start++) {
Jan Kiszkac6d22832009-11-30 18:21:20 +0100651 idx = start / (sizeof(unsigned long) * 8);
652 bit = start % (sizeof(unsigned long) * 8);
653 val = bs->dirty_bitmap[idx];
654 if (dirty) {
655 val |= 1 << bit;
656 } else {
657 val &= ~(1 << bit);
658 }
659 bs->dirty_bitmap[idx] = val;
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +0200660 }
661}
662
ths5fafdf22007-09-16 21:08:06 +0000663/* Return < 0 if error. Important errors are:
bellard19cb3732006-08-19 11:45:59 +0000664 -EIO generic I/O error (may happen for all errors)
665 -ENOMEDIUM No media inserted.
666 -EINVAL Invalid sector number or nb_sectors
667 -EACCES Trying to write a read-only device
668*/
ths5fafdf22007-09-16 21:08:06 +0000669int bdrv_write(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000670 const uint8_t *buf, int nb_sectors)
671{
bellard83f64092006-08-01 16:21:11 +0000672 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +0000673 if (!bs->drv)
674 return -ENOMEDIUM;
bellard0849bf02003-06-30 23:17:31 +0000675 if (bs->read_only)
bellard19cb3732006-08-19 11:45:59 +0000676 return -EACCES;
aliguori71d07702009-03-03 17:37:16 +0000677 if (bdrv_check_request(bs, sector_num, nb_sectors))
678 return -EIO;
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100679
Jan Kiszkac6d22832009-11-30 18:21:20 +0100680 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +0200681 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
682 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +0100683
aliguori42fb2802009-01-15 20:43:39 +0000684 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
bellard83f64092006-08-01 16:21:11 +0000685}
686
aliguorieda578e2009-03-12 19:57:16 +0000687int bdrv_pread(BlockDriverState *bs, int64_t offset,
688 void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000689{
Jan Kiszka6ea44302009-11-30 18:21:19 +0100690 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
bellard83f64092006-08-01 16:21:11 +0000691 int len, nb_sectors, count;
692 int64_t sector_num;
693
694 count = count1;
695 /* first read to align to sector start */
Jan Kiszka6ea44302009-11-30 18:21:19 +0100696 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
bellard83f64092006-08-01 16:21:11 +0000697 if (len > count)
698 len = count;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100699 sector_num = offset >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000700 if (len > 0) {
701 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
702 return -EIO;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100703 memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
bellard83f64092006-08-01 16:21:11 +0000704 count -= len;
705 if (count == 0)
706 return count1;
707 sector_num++;
708 buf += len;
709 }
710
711 /* read the sectors "in place" */
Jan Kiszka6ea44302009-11-30 18:21:19 +0100712 nb_sectors = count >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000713 if (nb_sectors > 0) {
714 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
715 return -EIO;
716 sector_num += nb_sectors;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100717 len = nb_sectors << BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000718 buf += len;
719 count -= len;
720 }
721
722 /* add data from the last sector */
723 if (count > 0) {
724 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
725 return -EIO;
726 memcpy(buf, tmp_buf, count);
727 }
728 return count1;
729}
730
aliguorieda578e2009-03-12 19:57:16 +0000731int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
732 const void *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000733{
Jan Kiszka6ea44302009-11-30 18:21:19 +0100734 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
bellard83f64092006-08-01 16:21:11 +0000735 int len, nb_sectors, count;
736 int64_t sector_num;
737
738 count = count1;
739 /* first write to align to sector start */
Jan Kiszka6ea44302009-11-30 18:21:19 +0100740 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
bellard83f64092006-08-01 16:21:11 +0000741 if (len > count)
742 len = count;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100743 sector_num = offset >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000744 if (len > 0) {
745 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
746 return -EIO;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100747 memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len);
bellard83f64092006-08-01 16:21:11 +0000748 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
749 return -EIO;
750 count -= len;
751 if (count == 0)
752 return count1;
753 sector_num++;
754 buf += len;
755 }
756
757 /* write the sectors "in place" */
Jan Kiszka6ea44302009-11-30 18:21:19 +0100758 nb_sectors = count >> BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000759 if (nb_sectors > 0) {
760 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
761 return -EIO;
762 sector_num += nb_sectors;
Jan Kiszka6ea44302009-11-30 18:21:19 +0100763 len = nb_sectors << BDRV_SECTOR_BITS;
bellard83f64092006-08-01 16:21:11 +0000764 buf += len;
765 count -= len;
766 }
767
768 /* add data from the last sector */
769 if (count > 0) {
770 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
771 return -EIO;
772 memcpy(tmp_buf, buf, count);
773 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
774 return -EIO;
775 }
776 return count1;
777}
bellard83f64092006-08-01 16:21:11 +0000778
779/**
bellard83f64092006-08-01 16:21:11 +0000780 * Truncate file to 'offset' bytes (needed only for file protocols)
781 */
782int bdrv_truncate(BlockDriverState *bs, int64_t offset)
783{
784 BlockDriver *drv = bs->drv;
785 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000786 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000787 if (!drv->bdrv_truncate)
788 return -ENOTSUP;
Naphtali Sprei59f26892009-10-26 16:25:16 +0200789 if (bs->read_only)
790 return -EACCES;
bellard83f64092006-08-01 16:21:11 +0000791 return drv->bdrv_truncate(bs, offset);
792}
793
794/**
795 * Length of a file in bytes. Return < 0 if error or unknown.
796 */
797int64_t bdrv_getlength(BlockDriverState *bs)
798{
799 BlockDriver *drv = bs->drv;
800 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000801 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000802 if (!drv->bdrv_getlength) {
803 /* legacy mode */
Jan Kiszka6ea44302009-11-30 18:21:19 +0100804 return bs->total_sectors * BDRV_SECTOR_SIZE;
bellard83f64092006-08-01 16:21:11 +0000805 }
806 return drv->bdrv_getlength(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000807}
808
bellard19cb3732006-08-19 11:45:59 +0000809/* return 0 as number of sectors if no device present or error */
ths96b8f132007-12-17 01:35:20 +0000810void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
bellardfc01f7e2003-06-30 10:03:06 +0000811{
bellard19cb3732006-08-19 11:45:59 +0000812 int64_t length;
813 length = bdrv_getlength(bs);
814 if (length < 0)
815 length = 0;
816 else
Jan Kiszka6ea44302009-11-30 18:21:19 +0100817 length = length >> BDRV_SECTOR_BITS;
bellard19cb3732006-08-19 11:45:59 +0000818 *nb_sectors_ptr = length;
bellardfc01f7e2003-06-30 10:03:06 +0000819}
bellardcf989512004-02-16 21:56:36 +0000820
aliguorif3d54fc2008-11-25 21:50:24 +0000821struct partition {
822 uint8_t boot_ind; /* 0x80 - active */
823 uint8_t head; /* starting head */
824 uint8_t sector; /* starting sector */
825 uint8_t cyl; /* starting cylinder */
826 uint8_t sys_ind; /* What partition type */
827 uint8_t end_head; /* end head */
828 uint8_t end_sector; /* end sector */
829 uint8_t end_cyl; /* end cylinder */
830 uint32_t start_sect; /* starting sector counting from 0 */
831 uint32_t nr_sects; /* nr of sectors in partition */
832} __attribute__((packed));
833
834/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
835static int guess_disk_lchs(BlockDriverState *bs,
836 int *pcylinders, int *pheads, int *psectors)
837{
838 uint8_t buf[512];
839 int ret, i, heads, sectors, cylinders;
840 struct partition *p;
841 uint32_t nr_sects;
blueswir1a38131b2008-12-05 17:56:40 +0000842 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +0000843
844 bdrv_get_geometry(bs, &nb_sectors);
845
846 ret = bdrv_read(bs, 0, buf, 1);
847 if (ret < 0)
848 return -1;
849 /* test msdos magic */
850 if (buf[510] != 0x55 || buf[511] != 0xaa)
851 return -1;
852 for(i = 0; i < 4; i++) {
853 p = ((struct partition *)(buf + 0x1be)) + i;
854 nr_sects = le32_to_cpu(p->nr_sects);
855 if (nr_sects && p->end_head) {
856 /* We make the assumption that the partition terminates on
857 a cylinder boundary */
858 heads = p->end_head + 1;
859 sectors = p->end_sector & 63;
860 if (sectors == 0)
861 continue;
862 cylinders = nb_sectors / (heads * sectors);
863 if (cylinders < 1 || cylinders > 16383)
864 continue;
865 *pheads = heads;
866 *psectors = sectors;
867 *pcylinders = cylinders;
868#if 0
869 printf("guessed geometry: LCHS=%d %d %d\n",
870 cylinders, heads, sectors);
871#endif
872 return 0;
873 }
874 }
875 return -1;
876}
877
878void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
879{
880 int translation, lba_detected = 0;
881 int cylinders, heads, secs;
blueswir1a38131b2008-12-05 17:56:40 +0000882 uint64_t nb_sectors;
aliguorif3d54fc2008-11-25 21:50:24 +0000883
884 /* if a geometry hint is available, use it */
885 bdrv_get_geometry(bs, &nb_sectors);
886 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
887 translation = bdrv_get_translation_hint(bs);
888 if (cylinders != 0) {
889 *pcyls = cylinders;
890 *pheads = heads;
891 *psecs = secs;
892 } else {
893 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
894 if (heads > 16) {
895 /* if heads > 16, it means that a BIOS LBA
896 translation was active, so the default
897 hardware geometry is OK */
898 lba_detected = 1;
899 goto default_geometry;
900 } else {
901 *pcyls = cylinders;
902 *pheads = heads;
903 *psecs = secs;
904 /* disable any translation to be in sync with
905 the logical geometry */
906 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
907 bdrv_set_translation_hint(bs,
908 BIOS_ATA_TRANSLATION_NONE);
909 }
910 }
911 } else {
912 default_geometry:
913 /* if no geometry, use a standard physical disk geometry */
914 cylinders = nb_sectors / (16 * 63);
915
916 if (cylinders > 16383)
917 cylinders = 16383;
918 else if (cylinders < 2)
919 cylinders = 2;
920 *pcyls = cylinders;
921 *pheads = 16;
922 *psecs = 63;
923 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
924 if ((*pcyls * *pheads) <= 131072) {
925 bdrv_set_translation_hint(bs,
926 BIOS_ATA_TRANSLATION_LARGE);
927 } else {
928 bdrv_set_translation_hint(bs,
929 BIOS_ATA_TRANSLATION_LBA);
930 }
931 }
932 }
933 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
934 }
935}
936
ths5fafdf22007-09-16 21:08:06 +0000937void bdrv_set_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000938 int cyls, int heads, int secs)
939{
940 bs->cyls = cyls;
941 bs->heads = heads;
942 bs->secs = secs;
943}
944
945void bdrv_set_type_hint(BlockDriverState *bs, int type)
946{
947 bs->type = type;
948 bs->removable = ((type == BDRV_TYPE_CDROM ||
949 type == BDRV_TYPE_FLOPPY));
950}
951
bellard46d47672004-11-16 01:45:27 +0000952void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
953{
954 bs->translation = translation;
955}
956
ths5fafdf22007-09-16 21:08:06 +0000957void bdrv_get_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000958 int *pcyls, int *pheads, int *psecs)
959{
960 *pcyls = bs->cyls;
961 *pheads = bs->heads;
962 *psecs = bs->secs;
963}
964
965int bdrv_get_type_hint(BlockDriverState *bs)
966{
967 return bs->type;
968}
969
bellard46d47672004-11-16 01:45:27 +0000970int bdrv_get_translation_hint(BlockDriverState *bs)
971{
972 return bs->translation;
973}
974
bellardb3380822004-03-14 21:38:54 +0000975int bdrv_is_removable(BlockDriverState *bs)
976{
977 return bs->removable;
978}
979
980int bdrv_is_read_only(BlockDriverState *bs)
981{
982 return bs->read_only;
983}
984
Naphtali Sprei59f26892009-10-26 16:25:16 +0200985int bdrv_set_read_only(BlockDriverState *bs, int read_only)
986{
987 int ret = bs->read_only;
988 bs->read_only = read_only;
989 return ret;
990}
991
ths985a03b2007-12-24 16:10:43 +0000992int bdrv_is_sg(BlockDriverState *bs)
993{
994 return bs->sg;
995}
996
Christoph Hellwige900a7b2009-09-04 19:01:15 +0200997int bdrv_enable_write_cache(BlockDriverState *bs)
998{
999 return bs->enable_write_cache;
1000}
1001
bellard19cb3732006-08-19 11:45:59 +00001002/* XXX: no longer used */
ths5fafdf22007-09-16 21:08:06 +00001003void bdrv_set_change_cb(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +00001004 void (*change_cb)(void *opaque), void *opaque)
1005{
1006 bs->change_cb = change_cb;
1007 bs->change_opaque = opaque;
1008}
1009
bellardea2384d2004-08-01 21:59:26 +00001010int bdrv_is_encrypted(BlockDriverState *bs)
1011{
1012 if (bs->backing_hd && bs->backing_hd->encrypted)
1013 return 1;
1014 return bs->encrypted;
1015}
1016
aliguoric0f4ce72009-03-05 23:01:01 +00001017int bdrv_key_required(BlockDriverState *bs)
1018{
1019 BlockDriverState *backing_hd = bs->backing_hd;
1020
1021 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
1022 return 1;
1023 return (bs->encrypted && !bs->valid_key);
1024}
1025
bellardea2384d2004-08-01 21:59:26 +00001026int bdrv_set_key(BlockDriverState *bs, const char *key)
1027{
1028 int ret;
1029 if (bs->backing_hd && bs->backing_hd->encrypted) {
1030 ret = bdrv_set_key(bs->backing_hd, key);
1031 if (ret < 0)
1032 return ret;
1033 if (!bs->encrypted)
1034 return 0;
1035 }
1036 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
1037 return -1;
aliguoric0f4ce72009-03-05 23:01:01 +00001038 ret = bs->drv->bdrv_set_key(bs, key);
aliguoribb5fc202009-03-05 23:01:15 +00001039 if (ret < 0) {
1040 bs->valid_key = 0;
1041 } else if (!bs->valid_key) {
1042 bs->valid_key = 1;
1043 /* call the change callback now, we skipped it on open */
1044 bs->media_changed = 1;
1045 if (bs->change_cb)
1046 bs->change_cb(bs->change_opaque);
1047 }
aliguoric0f4ce72009-03-05 23:01:01 +00001048 return ret;
bellardea2384d2004-08-01 21:59:26 +00001049}
1050
1051void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
1052{
bellard19cb3732006-08-19 11:45:59 +00001053 if (!bs->drv) {
bellardea2384d2004-08-01 21:59:26 +00001054 buf[0] = '\0';
1055 } else {
1056 pstrcpy(buf, buf_size, bs->drv->format_name);
1057 }
1058}
1059
ths5fafdf22007-09-16 21:08:06 +00001060void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
bellardea2384d2004-08-01 21:59:26 +00001061 void *opaque)
1062{
1063 BlockDriver *drv;
1064
1065 for (drv = first_drv; drv != NULL; drv = drv->next) {
1066 it(opaque, drv->format_name);
1067 }
1068}
1069
bellardb3380822004-03-14 21:38:54 +00001070BlockDriverState *bdrv_find(const char *name)
1071{
1072 BlockDriverState *bs;
1073
1074 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1075 if (!strcmp(name, bs->device_name))
1076 return bs;
1077 }
1078 return NULL;
1079}
1080
aliguori51de9762009-03-05 23:00:43 +00001081void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
bellard81d09122004-07-14 17:21:37 +00001082{
1083 BlockDriverState *bs;
1084
1085 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori51de9762009-03-05 23:00:43 +00001086 it(opaque, bs);
bellard81d09122004-07-14 17:21:37 +00001087 }
1088}
1089
bellardea2384d2004-08-01 21:59:26 +00001090const char *bdrv_get_device_name(BlockDriverState *bs)
1091{
1092 return bs->device_name;
1093}
1094
pbrook7a6cba62006-06-04 11:39:07 +00001095void bdrv_flush(BlockDriverState *bs)
1096{
aliguori081501d2009-03-29 01:31:51 +00001097 if (!bs->drv)
1098 return;
pbrook7a6cba62006-06-04 11:39:07 +00001099 if (bs->drv->bdrv_flush)
1100 bs->drv->bdrv_flush(bs);
1101 if (bs->backing_hd)
1102 bdrv_flush(bs->backing_hd);
1103}
1104
aliguoric6ca28d2008-10-06 13:55:43 +00001105void bdrv_flush_all(void)
1106{
1107 BlockDriverState *bs;
1108
1109 for (bs = bdrv_first; bs != NULL; bs = bs->next)
1110 if (bs->drv && !bdrv_is_read_only(bs) &&
1111 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1112 bdrv_flush(bs);
1113}
1114
thsf58c7b32008-06-05 21:53:49 +00001115/*
1116 * Returns true iff the specified sector is present in the disk image. Drivers
1117 * not implementing the functionality are assumed to not support backing files,
1118 * hence all their sectors are reported as allocated.
1119 *
1120 * 'pnum' is set to the number of sectors (including and immediately following
1121 * the specified sector) that are known to be in the same
1122 * allocated/unallocated state.
1123 *
1124 * 'nb_sectors' is the max value 'pnum' should be set to.
1125 */
1126int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1127 int *pnum)
1128{
1129 int64_t n;
1130 if (!bs->drv->bdrv_is_allocated) {
1131 if (sector_num >= bs->total_sectors) {
1132 *pnum = 0;
1133 return 0;
1134 }
1135 n = bs->total_sectors - sector_num;
1136 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1137 return 1;
1138 }
1139 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1140}
1141
aliguori376253e2009-03-05 23:01:23 +00001142void bdrv_info(Monitor *mon)
bellardb3380822004-03-14 21:38:54 +00001143{
1144 BlockDriverState *bs;
1145
1146 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori376253e2009-03-05 23:01:23 +00001147 monitor_printf(mon, "%s:", bs->device_name);
1148 monitor_printf(mon, " type=");
bellardb3380822004-03-14 21:38:54 +00001149 switch(bs->type) {
1150 case BDRV_TYPE_HD:
aliguori376253e2009-03-05 23:01:23 +00001151 monitor_printf(mon, "hd");
bellardb3380822004-03-14 21:38:54 +00001152 break;
1153 case BDRV_TYPE_CDROM:
aliguori376253e2009-03-05 23:01:23 +00001154 monitor_printf(mon, "cdrom");
bellardb3380822004-03-14 21:38:54 +00001155 break;
1156 case BDRV_TYPE_FLOPPY:
aliguori376253e2009-03-05 23:01:23 +00001157 monitor_printf(mon, "floppy");
bellardb3380822004-03-14 21:38:54 +00001158 break;
1159 }
aliguori376253e2009-03-05 23:01:23 +00001160 monitor_printf(mon, " removable=%d", bs->removable);
bellardb3380822004-03-14 21:38:54 +00001161 if (bs->removable) {
aliguori376253e2009-03-05 23:01:23 +00001162 monitor_printf(mon, " locked=%d", bs->locked);
bellardb3380822004-03-14 21:38:54 +00001163 }
bellard19cb3732006-08-19 11:45:59 +00001164 if (bs->drv) {
aliguori376253e2009-03-05 23:01:23 +00001165 monitor_printf(mon, " file=");
1166 monitor_print_filename(mon, bs->filename);
thsfef30742006-12-22 14:11:32 +00001167 if (bs->backing_file[0] != '\0') {
aliguori376253e2009-03-05 23:01:23 +00001168 monitor_printf(mon, " backing_file=");
1169 monitor_print_filename(mon, bs->backing_file);
1170 }
1171 monitor_printf(mon, " ro=%d", bs->read_only);
1172 monitor_printf(mon, " drv=%s", bs->drv->format_name);
1173 monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs));
bellardb3380822004-03-14 21:38:54 +00001174 } else {
aliguori376253e2009-03-05 23:01:23 +00001175 monitor_printf(mon, " [not inserted]");
bellardb3380822004-03-14 21:38:54 +00001176 }
aliguori376253e2009-03-05 23:01:23 +00001177 monitor_printf(mon, "\n");
bellardb3380822004-03-14 21:38:54 +00001178 }
1179}
thsa36e69d2007-12-02 05:18:19 +00001180
1181/* The "info blockstats" command. */
aliguori376253e2009-03-05 23:01:23 +00001182void bdrv_info_stats(Monitor *mon)
thsa36e69d2007-12-02 05:18:19 +00001183{
1184 BlockDriverState *bs;
1185
1186 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
aliguori376253e2009-03-05 23:01:23 +00001187 monitor_printf(mon, "%s:"
1188 " rd_bytes=%" PRIu64
1189 " wr_bytes=%" PRIu64
1190 " rd_operations=%" PRIu64
1191 " wr_operations=%" PRIu64
aliguoriebf53fc2009-03-11 20:05:29 +00001192 "\n",
aliguori376253e2009-03-05 23:01:23 +00001193 bs->device_name,
1194 bs->rd_bytes, bs->wr_bytes,
1195 bs->rd_ops, bs->wr_ops);
thsa36e69d2007-12-02 05:18:19 +00001196 }
1197}
bellardea2384d2004-08-01 21:59:26 +00001198
aliguori045df332009-03-05 23:00:48 +00001199const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1200{
1201 if (bs->backing_hd && bs->backing_hd->encrypted)
1202 return bs->backing_file;
1203 else if (bs->encrypted)
1204 return bs->filename;
1205 else
1206 return NULL;
1207}
1208
ths5fafdf22007-09-16 21:08:06 +00001209void bdrv_get_backing_filename(BlockDriverState *bs,
bellard83f64092006-08-01 16:21:11 +00001210 char *filename, int filename_size)
bellardea2384d2004-08-01 21:59:26 +00001211{
bellard83f64092006-08-01 16:21:11 +00001212 if (!bs->backing_hd) {
1213 pstrcpy(filename, filename_size, "");
1214 } else {
1215 pstrcpy(filename, filename_size, bs->backing_file);
1216 }
bellardea2384d2004-08-01 21:59:26 +00001217}
1218
ths5fafdf22007-09-16 21:08:06 +00001219int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
bellardfaea38e2006-08-05 21:31:00 +00001220 const uint8_t *buf, int nb_sectors)
1221{
1222 BlockDriver *drv = bs->drv;
1223 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001224 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001225 if (!drv->bdrv_write_compressed)
1226 return -ENOTSUP;
Kevin Wolffbb7b4e2009-05-08 14:47:24 +02001227 if (bdrv_check_request(bs, sector_num, nb_sectors))
1228 return -EIO;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001229
Jan Kiszkac6d22832009-11-30 18:21:20 +01001230 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001231 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1232 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001233
bellardfaea38e2006-08-05 21:31:00 +00001234 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1235}
ths3b46e622007-09-17 08:09:54 +00001236
bellardfaea38e2006-08-05 21:31:00 +00001237int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
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_get_info)
1243 return -ENOTSUP;
1244 memset(bdi, 0, sizeof(*bdi));
1245 return drv->bdrv_get_info(bs, bdi);
1246}
1247
Christoph Hellwig45566e92009-07-10 23:11:57 +02001248int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
1249 int64_t pos, int size)
aliguori178e08a2009-04-05 19:10:55 +00001250{
1251 BlockDriver *drv = bs->drv;
1252 if (!drv)
1253 return -ENOMEDIUM;
Christoph Hellwig45566e92009-07-10 23:11:57 +02001254 if (!drv->bdrv_save_vmstate)
aliguori178e08a2009-04-05 19:10:55 +00001255 return -ENOTSUP;
Christoph Hellwig45566e92009-07-10 23:11:57 +02001256 return drv->bdrv_save_vmstate(bs, buf, pos, size);
aliguori178e08a2009-04-05 19:10:55 +00001257}
1258
Christoph Hellwig45566e92009-07-10 23:11:57 +02001259int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1260 int64_t pos, int size)
aliguori178e08a2009-04-05 19:10:55 +00001261{
1262 BlockDriver *drv = bs->drv;
1263 if (!drv)
1264 return -ENOMEDIUM;
Christoph Hellwig45566e92009-07-10 23:11:57 +02001265 if (!drv->bdrv_load_vmstate)
aliguori178e08a2009-04-05 19:10:55 +00001266 return -ENOTSUP;
Christoph Hellwig45566e92009-07-10 23:11:57 +02001267 return drv->bdrv_load_vmstate(bs, buf, pos, size);
aliguori178e08a2009-04-05 19:10:55 +00001268}
1269
bellardfaea38e2006-08-05 21:31:00 +00001270/**************************************************************/
1271/* handling of snapshots */
1272
ths5fafdf22007-09-16 21:08:06 +00001273int bdrv_snapshot_create(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001274 QEMUSnapshotInfo *sn_info)
1275{
1276 BlockDriver *drv = bs->drv;
1277 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001278 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001279 if (!drv->bdrv_snapshot_create)
1280 return -ENOTSUP;
1281 return drv->bdrv_snapshot_create(bs, sn_info);
1282}
1283
ths5fafdf22007-09-16 21:08:06 +00001284int bdrv_snapshot_goto(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001285 const char *snapshot_id)
1286{
1287 BlockDriver *drv = bs->drv;
1288 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001289 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001290 if (!drv->bdrv_snapshot_goto)
1291 return -ENOTSUP;
1292 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1293}
1294
1295int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1296{
1297 BlockDriver *drv = bs->drv;
1298 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001299 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001300 if (!drv->bdrv_snapshot_delete)
1301 return -ENOTSUP;
1302 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1303}
1304
ths5fafdf22007-09-16 21:08:06 +00001305int bdrv_snapshot_list(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001306 QEMUSnapshotInfo **psn_info)
1307{
1308 BlockDriver *drv = bs->drv;
1309 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001310 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001311 if (!drv->bdrv_snapshot_list)
1312 return -ENOTSUP;
1313 return drv->bdrv_snapshot_list(bs, psn_info);
1314}
1315
1316#define NB_SUFFIXES 4
1317
1318char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1319{
1320 static const char suffixes[NB_SUFFIXES] = "KMGT";
1321 int64_t base;
1322 int i;
1323
1324 if (size <= 999) {
1325 snprintf(buf, buf_size, "%" PRId64, size);
1326 } else {
1327 base = 1024;
1328 for(i = 0; i < NB_SUFFIXES; i++) {
1329 if (size < (10 * base)) {
ths5fafdf22007-09-16 21:08:06 +00001330 snprintf(buf, buf_size, "%0.1f%c",
bellardfaea38e2006-08-05 21:31:00 +00001331 (double)size / base,
1332 suffixes[i]);
1333 break;
1334 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
ths5fafdf22007-09-16 21:08:06 +00001335 snprintf(buf, buf_size, "%" PRId64 "%c",
bellardfaea38e2006-08-05 21:31:00 +00001336 ((size + (base >> 1)) / base),
1337 suffixes[i]);
1338 break;
1339 }
1340 base = base * 1024;
1341 }
1342 }
1343 return buf;
1344}
1345
1346char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1347{
1348 char buf1[128], date_buf[128], clock_buf[128];
bellard3b9f94e2007-01-07 17:27:07 +00001349#ifdef _WIN32
1350 struct tm *ptm;
1351#else
bellardfaea38e2006-08-05 21:31:00 +00001352 struct tm tm;
bellard3b9f94e2007-01-07 17:27:07 +00001353#endif
bellardfaea38e2006-08-05 21:31:00 +00001354 time_t ti;
1355 int64_t secs;
1356
1357 if (!sn) {
ths5fafdf22007-09-16 21:08:06 +00001358 snprintf(buf, buf_size,
1359 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001360 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1361 } else {
1362 ti = sn->date_sec;
bellard3b9f94e2007-01-07 17:27:07 +00001363#ifdef _WIN32
1364 ptm = localtime(&ti);
1365 strftime(date_buf, sizeof(date_buf),
1366 "%Y-%m-%d %H:%M:%S", ptm);
1367#else
bellardfaea38e2006-08-05 21:31:00 +00001368 localtime_r(&ti, &tm);
1369 strftime(date_buf, sizeof(date_buf),
1370 "%Y-%m-%d %H:%M:%S", &tm);
bellard3b9f94e2007-01-07 17:27:07 +00001371#endif
bellardfaea38e2006-08-05 21:31:00 +00001372 secs = sn->vm_clock_nsec / 1000000000;
1373 snprintf(clock_buf, sizeof(clock_buf),
1374 "%02d:%02d:%02d.%03d",
1375 (int)(secs / 3600),
1376 (int)((secs / 60) % 60),
ths5fafdf22007-09-16 21:08:06 +00001377 (int)(secs % 60),
bellardfaea38e2006-08-05 21:31:00 +00001378 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1379 snprintf(buf, buf_size,
ths5fafdf22007-09-16 21:08:06 +00001380 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001381 sn->id_str, sn->name,
1382 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1383 date_buf,
1384 clock_buf);
1385 }
1386 return buf;
1387}
1388
bellardea2384d2004-08-01 21:59:26 +00001389
bellard83f64092006-08-01 16:21:11 +00001390/**************************************************************/
1391/* async I/Os */
1392
aliguori3b69e4b2009-01-22 16:59:24 +00001393BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
aliguorif141eaf2009-04-07 18:43:24 +00001394 QEMUIOVector *qiov, int nb_sectors,
aliguori3b69e4b2009-01-22 16:59:24 +00001395 BlockDriverCompletionFunc *cb, void *opaque)
1396{
bellard83f64092006-08-01 16:21:11 +00001397 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001398 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001399
bellard19cb3732006-08-19 11:45:59 +00001400 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001401 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001402 if (bdrv_check_request(bs, sector_num, nb_sectors))
1403 return NULL;
ths3b46e622007-09-17 08:09:54 +00001404
aliguorif141eaf2009-04-07 18:43:24 +00001405 ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
1406 cb, opaque);
thsa36e69d2007-12-02 05:18:19 +00001407
1408 if (ret) {
1409 /* Update stats even though technically transfer has not happened. */
Jan Kiszka6ea44302009-11-30 18:21:19 +01001410 bs->rd_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
thsa36e69d2007-12-02 05:18:19 +00001411 bs->rd_ops ++;
1412 }
1413
1414 return ret;
bellard83f64092006-08-01 16:21:11 +00001415}
1416
aliguorif141eaf2009-04-07 18:43:24 +00001417BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1418 QEMUIOVector *qiov, int nb_sectors,
1419 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001420{
bellard83f64092006-08-01 16:21:11 +00001421 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001422 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001423
bellard19cb3732006-08-19 11:45:59 +00001424 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001425 return NULL;
bellard83f64092006-08-01 16:21:11 +00001426 if (bs->read_only)
pbrookce1a14d2006-08-07 02:38:06 +00001427 return NULL;
aliguori71d07702009-03-03 17:37:16 +00001428 if (bdrv_check_request(bs, sector_num, nb_sectors))
1429 return NULL;
bellard83f64092006-08-01 16:21:11 +00001430
Jan Kiszkac6d22832009-11-30 18:21:20 +01001431 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001432 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1433 }
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001434
aliguorif141eaf2009-04-07 18:43:24 +00001435 ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
1436 cb, opaque);
thsa36e69d2007-12-02 05:18:19 +00001437
1438 if (ret) {
1439 /* Update stats even though technically transfer has not happened. */
Jan Kiszka6ea44302009-11-30 18:21:19 +01001440 bs->wr_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
thsa36e69d2007-12-02 05:18:19 +00001441 bs->wr_ops ++;
1442 }
1443
1444 return ret;
bellard83f64092006-08-01 16:21:11 +00001445}
1446
Kevin Wolf40b4f532009-09-09 17:53:37 +02001447
1448typedef struct MultiwriteCB {
1449 int error;
1450 int num_requests;
1451 int num_callbacks;
1452 struct {
1453 BlockDriverCompletionFunc *cb;
1454 void *opaque;
1455 QEMUIOVector *free_qiov;
1456 void *free_buf;
1457 } callbacks[];
1458} MultiwriteCB;
1459
1460static void multiwrite_user_cb(MultiwriteCB *mcb)
1461{
1462 int i;
1463
1464 for (i = 0; i < mcb->num_callbacks; i++) {
1465 mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
1466 qemu_free(mcb->callbacks[i].free_qiov);
1467 qemu_free(mcb->callbacks[i].free_buf);
1468 }
1469}
1470
1471static void multiwrite_cb(void *opaque, int ret)
1472{
1473 MultiwriteCB *mcb = opaque;
1474
1475 if (ret < 0) {
1476 mcb->error = ret;
1477 multiwrite_user_cb(mcb);
1478 }
1479
1480 mcb->num_requests--;
1481 if (mcb->num_requests == 0) {
1482 if (mcb->error == 0) {
1483 multiwrite_user_cb(mcb);
1484 }
1485 qemu_free(mcb);
1486 }
1487}
1488
1489static int multiwrite_req_compare(const void *a, const void *b)
1490{
1491 return (((BlockRequest*) a)->sector - ((BlockRequest*) b)->sector);
1492}
1493
1494/*
1495 * Takes a bunch of requests and tries to merge them. Returns the number of
1496 * requests that remain after merging.
1497 */
1498static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
1499 int num_reqs, MultiwriteCB *mcb)
1500{
1501 int i, outidx;
1502
1503 // Sort requests by start sector
1504 qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
1505
1506 // Check if adjacent requests touch the same clusters. If so, combine them,
1507 // filling up gaps with zero sectors.
1508 outidx = 0;
1509 for (i = 1; i < num_reqs; i++) {
1510 int merge = 0;
1511 int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
1512
1513 // This handles the cases that are valid for all block drivers, namely
1514 // exactly sequential writes and overlapping writes.
1515 if (reqs[i].sector <= oldreq_last) {
1516 merge = 1;
1517 }
1518
1519 // The block driver may decide that it makes sense to combine requests
1520 // even if there is a gap of some sectors between them. In this case,
1521 // the gap is filled with zeros (therefore only applicable for yet
1522 // unused space in format like qcow2).
1523 if (!merge && bs->drv->bdrv_merge_requests) {
1524 merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]);
1525 }
1526
1527 if (merge) {
1528 size_t size;
1529 QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov));
1530 qemu_iovec_init(qiov,
1531 reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
1532
1533 // Add the first request to the merged one. If the requests are
1534 // overlapping, drop the last sectors of the first request.
1535 size = (reqs[i].sector - reqs[outidx].sector) << 9;
1536 qemu_iovec_concat(qiov, reqs[outidx].qiov, size);
1537
1538 // We might need to add some zeros between the two requests
1539 if (reqs[i].sector > oldreq_last) {
1540 size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
1541 uint8_t *buf = qemu_blockalign(bs, zero_bytes);
1542 memset(buf, 0, zero_bytes);
1543 qemu_iovec_add(qiov, buf, zero_bytes);
1544 mcb->callbacks[i].free_buf = buf;
1545 }
1546
1547 // Add the second request
1548 qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size);
1549
1550 reqs[outidx].nb_sectors += reqs[i].nb_sectors;
1551 reqs[outidx].qiov = qiov;
1552
1553 mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
1554 } else {
1555 outidx++;
1556 reqs[outidx].sector = reqs[i].sector;
1557 reqs[outidx].nb_sectors = reqs[i].nb_sectors;
1558 reqs[outidx].qiov = reqs[i].qiov;
1559 }
1560 }
1561
1562 return outidx + 1;
1563}
1564
1565/*
1566 * Submit multiple AIO write requests at once.
1567 *
1568 * On success, the function returns 0 and all requests in the reqs array have
1569 * been submitted. In error case this function returns -1, and any of the
1570 * requests may or may not be submitted yet. In particular, this means that the
1571 * callback will be called for some of the requests, for others it won't. The
1572 * caller must check the error field of the BlockRequest to wait for the right
1573 * callbacks (if error != 0, no callback will be called).
1574 *
1575 * The implementation may modify the contents of the reqs array, e.g. to merge
1576 * requests. However, the fields opaque and error are left unmodified as they
1577 * are used to signal failure for a single request to the caller.
1578 */
1579int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
1580{
1581 BlockDriverAIOCB *acb;
1582 MultiwriteCB *mcb;
1583 int i;
1584
1585 if (num_reqs == 0) {
1586 return 0;
1587 }
1588
1589 // Create MultiwriteCB structure
1590 mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
1591 mcb->num_requests = 0;
1592 mcb->num_callbacks = num_reqs;
1593
1594 for (i = 0; i < num_reqs; i++) {
1595 mcb->callbacks[i].cb = reqs[i].cb;
1596 mcb->callbacks[i].opaque = reqs[i].opaque;
1597 }
1598
1599 // Check for mergable requests
1600 num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
1601
1602 // Run the aio requests
1603 for (i = 0; i < num_reqs; i++) {
1604 acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
1605 reqs[i].nb_sectors, multiwrite_cb, mcb);
1606
1607 if (acb == NULL) {
1608 // We can only fail the whole thing if no request has been
1609 // submitted yet. Otherwise we'll wait for the submitted AIOs to
1610 // complete and report the error in the callback.
1611 if (mcb->num_requests == 0) {
1612 reqs[i].error = EIO;
1613 goto fail;
1614 } else {
1615 mcb->error = EIO;
1616 break;
1617 }
1618 } else {
1619 mcb->num_requests++;
1620 }
1621 }
1622
1623 return 0;
1624
1625fail:
1626 free(mcb);
1627 return -1;
1628}
1629
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02001630BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
1631 BlockDriverCompletionFunc *cb, void *opaque)
1632{
1633 BlockDriver *drv = bs->drv;
1634
1635 if (!drv)
1636 return NULL;
1637
1638 /*
1639 * Note that unlike bdrv_flush the driver is reponsible for flushing a
1640 * backing image if it exists.
1641 */
1642 return drv->bdrv_aio_flush(bs, cb, opaque);
1643}
1644
bellard83f64092006-08-01 16:21:11 +00001645void bdrv_aio_cancel(BlockDriverAIOCB *acb)
pbrookce1a14d2006-08-07 02:38:06 +00001646{
aliguori6bbff9a2009-03-20 18:25:59 +00001647 acb->pool->cancel(acb);
bellard83f64092006-08-01 16:21:11 +00001648}
1649
pbrookce1a14d2006-08-07 02:38:06 +00001650
bellard83f64092006-08-01 16:21:11 +00001651/**************************************************************/
1652/* async block device emulation */
1653
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02001654typedef struct BlockDriverAIOCBSync {
1655 BlockDriverAIOCB common;
1656 QEMUBH *bh;
1657 int ret;
1658 /* vector translation state */
1659 QEMUIOVector *qiov;
1660 uint8_t *bounce;
1661 int is_write;
1662} BlockDriverAIOCBSync;
1663
1664static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1665{
1666 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
Dor Laor6a7ad292009-06-01 12:07:23 +03001667 qemu_bh_delete(acb->bh);
Avi Kivity36afc452009-06-23 16:20:36 +03001668 acb->bh = NULL;
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02001669 qemu_aio_release(acb);
1670}
1671
1672static AIOPool bdrv_em_aio_pool = {
1673 .aiocb_size = sizeof(BlockDriverAIOCBSync),
1674 .cancel = bdrv_aio_cancel_em,
1675};
1676
bellard83f64092006-08-01 16:21:11 +00001677static void bdrv_aio_bh_cb(void *opaque)
bellardbeac80c2006-06-26 20:08:57 +00001678{
pbrookce1a14d2006-08-07 02:38:06 +00001679 BlockDriverAIOCBSync *acb = opaque;
aliguorif141eaf2009-04-07 18:43:24 +00001680
aliguorif141eaf2009-04-07 18:43:24 +00001681 if (!acb->is_write)
1682 qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
aliguoriceb42de2009-04-07 18:43:28 +00001683 qemu_vfree(acb->bounce);
pbrookce1a14d2006-08-07 02:38:06 +00001684 acb->common.cb(acb->common.opaque, acb->ret);
Dor Laor6a7ad292009-06-01 12:07:23 +03001685 qemu_bh_delete(acb->bh);
Avi Kivity36afc452009-06-23 16:20:36 +03001686 acb->bh = NULL;
pbrookce1a14d2006-08-07 02:38:06 +00001687 qemu_aio_release(acb);
bellardbeac80c2006-06-26 20:08:57 +00001688}
bellardbeac80c2006-06-26 20:08:57 +00001689
aliguorif141eaf2009-04-07 18:43:24 +00001690static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
1691 int64_t sector_num,
1692 QEMUIOVector *qiov,
1693 int nb_sectors,
1694 BlockDriverCompletionFunc *cb,
1695 void *opaque,
1696 int is_write)
1697
bellardea2384d2004-08-01 21:59:26 +00001698{
pbrookce1a14d2006-08-07 02:38:06 +00001699 BlockDriverAIOCBSync *acb;
pbrookce1a14d2006-08-07 02:38:06 +00001700
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02001701 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
aliguorif141eaf2009-04-07 18:43:24 +00001702 acb->is_write = is_write;
1703 acb->qiov = qiov;
aliguorie268ca52009-04-22 20:20:00 +00001704 acb->bounce = qemu_blockalign(bs, qiov->size);
aliguorif141eaf2009-04-07 18:43:24 +00001705
pbrookce1a14d2006-08-07 02:38:06 +00001706 if (!acb->bh)
1707 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
aliguorif141eaf2009-04-07 18:43:24 +00001708
1709 if (is_write) {
1710 qemu_iovec_to_buffer(acb->qiov, acb->bounce);
1711 acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
1712 } else {
1713 acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
1714 }
1715
pbrookce1a14d2006-08-07 02:38:06 +00001716 qemu_bh_schedule(acb->bh);
aliguorif141eaf2009-04-07 18:43:24 +00001717
pbrookce1a14d2006-08-07 02:38:06 +00001718 return &acb->common;
pbrook7a6cba62006-06-04 11:39:07 +00001719}
1720
aliguorif141eaf2009-04-07 18:43:24 +00001721static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
1722 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
pbrookce1a14d2006-08-07 02:38:06 +00001723 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001724{
aliguorif141eaf2009-04-07 18:43:24 +00001725 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
bellard83f64092006-08-01 16:21:11 +00001726}
1727
aliguorif141eaf2009-04-07 18:43:24 +00001728static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
1729 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
1730 BlockDriverCompletionFunc *cb, void *opaque)
1731{
1732 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
1733}
1734
Christoph Hellwigb2e12bc2009-09-04 19:01:49 +02001735static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
1736 BlockDriverCompletionFunc *cb, void *opaque)
1737{
1738 BlockDriverAIOCBSync *acb;
1739
1740 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
1741 acb->is_write = 1; /* don't bounce in the completion hadler */
1742 acb->qiov = NULL;
1743 acb->bounce = NULL;
1744 acb->ret = 0;
1745
1746 if (!acb->bh)
1747 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1748
1749 bdrv_flush(bs);
1750 qemu_bh_schedule(acb->bh);
1751 return &acb->common;
1752}
1753
bellard83f64092006-08-01 16:21:11 +00001754/**************************************************************/
1755/* sync block device emulation */
1756
1757static void bdrv_rw_em_cb(void *opaque, int ret)
1758{
1759 *(int *)opaque = ret;
1760}
1761
1762#define NOT_DONE 0x7fffffff
1763
ths5fafdf22007-09-16 21:08:06 +00001764static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +00001765 uint8_t *buf, int nb_sectors)
1766{
pbrookce1a14d2006-08-07 02:38:06 +00001767 int async_ret;
1768 BlockDriverAIOCB *acb;
aliguorif141eaf2009-04-07 18:43:24 +00001769 struct iovec iov;
1770 QEMUIOVector qiov;
bellard83f64092006-08-01 16:21:11 +00001771
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02001772 async_context_push();
1773
bellard83f64092006-08-01 16:21:11 +00001774 async_ret = NOT_DONE;
blueswir13f4cb3d2009-04-13 16:31:01 +00001775 iov.iov_base = (void *)buf;
aliguorif141eaf2009-04-07 18:43:24 +00001776 iov.iov_len = nb_sectors * 512;
1777 qemu_iovec_init_external(&qiov, &iov, 1);
1778 acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
1779 bdrv_rw_em_cb, &async_ret);
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02001780 if (acb == NULL) {
1781 async_ret = -1;
1782 goto fail;
1783 }
aliguoribaf35cb2008-09-10 15:45:19 +00001784
bellard83f64092006-08-01 16:21:11 +00001785 while (async_ret == NOT_DONE) {
1786 qemu_aio_wait();
1787 }
aliguoribaf35cb2008-09-10 15:45:19 +00001788
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02001789
1790fail:
1791 async_context_pop();
bellard83f64092006-08-01 16:21:11 +00001792 return async_ret;
1793}
1794
1795static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1796 const uint8_t *buf, int nb_sectors)
1797{
pbrookce1a14d2006-08-07 02:38:06 +00001798 int async_ret;
1799 BlockDriverAIOCB *acb;
aliguorif141eaf2009-04-07 18:43:24 +00001800 struct iovec iov;
1801 QEMUIOVector qiov;
bellard83f64092006-08-01 16:21:11 +00001802
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02001803 async_context_push();
1804
bellard83f64092006-08-01 16:21:11 +00001805 async_ret = NOT_DONE;
aliguorif141eaf2009-04-07 18:43:24 +00001806 iov.iov_base = (void *)buf;
1807 iov.iov_len = nb_sectors * 512;
1808 qemu_iovec_init_external(&qiov, &iov, 1);
1809 acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
1810 bdrv_rw_em_cb, &async_ret);
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02001811 if (acb == NULL) {
1812 async_ret = -1;
1813 goto fail;
1814 }
bellard83f64092006-08-01 16:21:11 +00001815 while (async_ret == NOT_DONE) {
1816 qemu_aio_wait();
1817 }
Kevin Wolf65d6b3d2009-10-22 17:54:39 +02001818
1819fail:
1820 async_context_pop();
bellard83f64092006-08-01 16:21:11 +00001821 return async_ret;
1822}
bellardea2384d2004-08-01 21:59:26 +00001823
1824void bdrv_init(void)
1825{
Anthony Liguori5efa9d52009-05-09 17:03:42 -05001826 module_call_init(MODULE_INIT_BLOCK);
bellardea2384d2004-08-01 21:59:26 +00001827}
pbrookce1a14d2006-08-07 02:38:06 +00001828
Markus Armbrustereb852012009-10-27 18:41:44 +01001829void bdrv_init_with_whitelist(void)
1830{
1831 use_bdrv_whitelist = 1;
1832 bdrv_init();
1833}
1834
Christoph Hellwigc16b5a22009-05-25 12:37:32 +02001835void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
1836 BlockDriverCompletionFunc *cb, void *opaque)
aliguori6bbff9a2009-03-20 18:25:59 +00001837{
pbrookce1a14d2006-08-07 02:38:06 +00001838 BlockDriverAIOCB *acb;
1839
aliguori6bbff9a2009-03-20 18:25:59 +00001840 if (pool->free_aiocb) {
1841 acb = pool->free_aiocb;
1842 pool->free_aiocb = acb->next;
pbrookce1a14d2006-08-07 02:38:06 +00001843 } else {
aliguori6bbff9a2009-03-20 18:25:59 +00001844 acb = qemu_mallocz(pool->aiocb_size);
1845 acb->pool = pool;
pbrookce1a14d2006-08-07 02:38:06 +00001846 }
1847 acb->bs = bs;
1848 acb->cb = cb;
1849 acb->opaque = opaque;
1850 return acb;
1851}
1852
1853void qemu_aio_release(void *p)
1854{
aliguori6bbff9a2009-03-20 18:25:59 +00001855 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
1856 AIOPool *pool = acb->pool;
1857 acb->next = pool->free_aiocb;
1858 pool->free_aiocb = acb;
pbrookce1a14d2006-08-07 02:38:06 +00001859}
bellard19cb3732006-08-19 11:45:59 +00001860
1861/**************************************************************/
1862/* removable device support */
1863
1864/**
1865 * Return TRUE if the media is present
1866 */
1867int bdrv_is_inserted(BlockDriverState *bs)
1868{
1869 BlockDriver *drv = bs->drv;
1870 int ret;
1871 if (!drv)
1872 return 0;
1873 if (!drv->bdrv_is_inserted)
1874 return 1;
1875 ret = drv->bdrv_is_inserted(bs);
1876 return ret;
1877}
1878
1879/**
1880 * Return TRUE if the media changed since the last call to this
ths5fafdf22007-09-16 21:08:06 +00001881 * function. It is currently only used for floppy disks
bellard19cb3732006-08-19 11:45:59 +00001882 */
1883int bdrv_media_changed(BlockDriverState *bs)
1884{
1885 BlockDriver *drv = bs->drv;
1886 int ret;
1887
1888 if (!drv || !drv->bdrv_media_changed)
1889 ret = -ENOTSUP;
1890 else
1891 ret = drv->bdrv_media_changed(bs);
1892 if (ret == -ENOTSUP)
1893 ret = bs->media_changed;
1894 bs->media_changed = 0;
1895 return ret;
1896}
1897
1898/**
1899 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1900 */
Mark McLoughlinaea2a332009-05-27 10:06:11 +01001901int bdrv_eject(BlockDriverState *bs, int eject_flag)
bellard19cb3732006-08-19 11:45:59 +00001902{
1903 BlockDriver *drv = bs->drv;
1904 int ret;
1905
Mark McLoughlinaea2a332009-05-27 10:06:11 +01001906 if (bs->locked) {
1907 return -EBUSY;
1908 }
1909
bellard19cb3732006-08-19 11:45:59 +00001910 if (!drv || !drv->bdrv_eject) {
1911 ret = -ENOTSUP;
1912 } else {
1913 ret = drv->bdrv_eject(bs, eject_flag);
1914 }
1915 if (ret == -ENOTSUP) {
1916 if (eject_flag)
1917 bdrv_close(bs);
Mark McLoughlinaea2a332009-05-27 10:06:11 +01001918 ret = 0;
bellard19cb3732006-08-19 11:45:59 +00001919 }
Mark McLoughlinaea2a332009-05-27 10:06:11 +01001920
1921 return ret;
bellard19cb3732006-08-19 11:45:59 +00001922}
1923
1924int bdrv_is_locked(BlockDriverState *bs)
1925{
1926 return bs->locked;
1927}
1928
1929/**
1930 * Lock or unlock the media (if it is locked, the user won't be able
1931 * to eject it manually).
1932 */
1933void bdrv_set_locked(BlockDriverState *bs, int locked)
1934{
1935 BlockDriver *drv = bs->drv;
1936
1937 bs->locked = locked;
1938 if (drv && drv->bdrv_set_locked) {
1939 drv->bdrv_set_locked(bs, locked);
1940 }
1941}
ths985a03b2007-12-24 16:10:43 +00001942
1943/* needed for generic scsi interface */
1944
1945int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1946{
1947 BlockDriver *drv = bs->drv;
1948
1949 if (drv && drv->bdrv_ioctl)
1950 return drv->bdrv_ioctl(bs, req, buf);
1951 return -ENOTSUP;
1952}
aliguori7d780662009-03-12 19:57:08 +00001953
aliguori221f7152009-03-28 17:28:41 +00001954BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
1955 unsigned long int req, void *buf,
1956 BlockDriverCompletionFunc *cb, void *opaque)
aliguori7d780662009-03-12 19:57:08 +00001957{
aliguori221f7152009-03-28 17:28:41 +00001958 BlockDriver *drv = bs->drv;
aliguori7d780662009-03-12 19:57:08 +00001959
aliguori221f7152009-03-28 17:28:41 +00001960 if (drv && drv->bdrv_aio_ioctl)
1961 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
1962 return NULL;
aliguori7d780662009-03-12 19:57:08 +00001963}
aliguorie268ca52009-04-22 20:20:00 +00001964
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001965
1966
aliguorie268ca52009-04-22 20:20:00 +00001967void *qemu_blockalign(BlockDriverState *bs, size_t size)
1968{
1969 return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
1970}
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001971
1972void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable)
1973{
1974 int64_t bitmap_size;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001975
1976 if (enable) {
Jan Kiszkac6d22832009-11-30 18:21:20 +01001977 if (!bs->dirty_bitmap) {
1978 bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
1979 BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
1980 bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001981
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001982 bs->dirty_bitmap = qemu_mallocz(bitmap_size);
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001983 }
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001984 } else {
Jan Kiszkac6d22832009-11-30 18:21:20 +01001985 if (bs->dirty_bitmap) {
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001986 qemu_free(bs->dirty_bitmap);
Jan Kiszkac6d22832009-11-30 18:21:20 +01001987 bs->dirty_bitmap = NULL;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001988 }
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02001989 }
1990}
1991
1992int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
1993{
Jan Kiszka6ea44302009-11-30 18:21:19 +01001994 int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
Jan Kiszkaa55eb922009-11-30 18:21:19 +01001995
Jan Kiszkac6d22832009-11-30 18:21:20 +01001996 if (bs->dirty_bitmap &&
1997 (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) {
1998 return bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] &
1999 (1 << (chunk % (sizeof(unsigned long) * 8)));
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002000 } else {
2001 return 0;
2002 }
2003}
2004
Jan Kiszkaa55eb922009-11-30 18:21:19 +01002005void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
2006 int nr_sectors)
lirans@il.ibm.com7cd1e322009-11-02 15:40:41 +02002007{
2008 set_dirty_bitmap(bs, cur_sector, nr_sectors, 0);
2009}