]> git.proxmox.com Git - qemu.git/blame - block.c
microblaze: Correct signal frame setup.
[qemu.git] / block.c
CommitLineData
fc01f7e7
FB
1/*
2 * QEMU System Emulator block driver
5fafdf24 3 *
fc01f7e7 4 * Copyright (c) 2003 Fabrice Bellard
5fafdf24 5 *
fc01f7e7
FB
6 * 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 */
3990d09a 24#include "config-host.h"
faf07963 25#include "qemu-common.h"
376253ec 26#include "monitor.h"
ea2384d3 27#include "block_int.h"
5efa9d5a 28#include "module.h"
d15e5465 29#include "qemu-objects.h"
fc01f7e7 30
71e72a19 31#ifdef CONFIG_BSD
7674e7bf
FB
32#include <sys/types.h>
33#include <sys/stat.h>
34#include <sys/ioctl.h>
72cf2d4f 35#include <sys/queue.h>
c5e97233 36#ifndef __DragonFly__
7674e7bf
FB
37#include <sys/disk.h>
38#endif
c5e97233 39#endif
7674e7bf 40
49dc768d
AL
41#ifdef _WIN32
42#include <windows.h>
43#endif
44
f141eafe
AL
45static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
46 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
c87c0672 47 BlockDriverCompletionFunc *cb, void *opaque);
f141eafe
AL
48static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
49 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
ce1a14dc 50 BlockDriverCompletionFunc *cb, void *opaque);
b2e12bc6
CH
51static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
52 BlockDriverCompletionFunc *cb, void *opaque);
016f5cf6
AG
53static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,
54 BlockDriverCompletionFunc *cb, void *opaque);
5fafdf24 55static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
83f64091
FB
56 uint8_t *buf, int nb_sectors);
57static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
58 const uint8_t *buf, int nb_sectors);
ec530c81 59
1b7bdbc1
SH
60static QTAILQ_HEAD(, BlockDriverState) bdrv_states =
61 QTAILQ_HEAD_INITIALIZER(bdrv_states);
7ee930d0 62
8a22f02a
SH
63static QLIST_HEAD(, BlockDriver) bdrv_drivers =
64 QLIST_HEAD_INITIALIZER(bdrv_drivers);
ea2384d3 65
f9092b10
MA
66/* The device to use for VM snapshots */
67static BlockDriverState *bs_snapshots;
68
eb852011
MA
69/* If non-zero, use only whitelisted block drivers */
70static int use_bdrv_whitelist;
71
83f64091 72int path_is_absolute(const char *path)
3b0d4f61 73{
83f64091 74 const char *p;
21664424
FB
75#ifdef _WIN32
76 /* specific case for names like: "\\.\d:" */
77 if (*path == '/' || *path == '\\')
78 return 1;
79#endif
83f64091
FB
80 p = strchr(path, ':');
81 if (p)
82 p++;
83 else
84 p = path;
3b9f94e1
FB
85#ifdef _WIN32
86 return (*p == '/' || *p == '\\');
87#else
88 return (*p == '/');
89#endif
3b0d4f61
FB
90}
91
83f64091
FB
92/* if filename is absolute, just copy it to dest. Otherwise, build a
93 path to it by considering it is relative to base_path. URL are
94 supported. */
95void path_combine(char *dest, int dest_size,
96 const char *base_path,
97 const char *filename)
3b0d4f61 98{
83f64091
FB
99 const char *p, *p1;
100 int len;
101
102 if (dest_size <= 0)
103 return;
104 if (path_is_absolute(filename)) {
105 pstrcpy(dest, dest_size, filename);
106 } else {
107 p = strchr(base_path, ':');
108 if (p)
109 p++;
110 else
111 p = base_path;
3b9f94e1
FB
112 p1 = strrchr(base_path, '/');
113#ifdef _WIN32
114 {
115 const char *p2;
116 p2 = strrchr(base_path, '\\');
117 if (!p1 || p2 > p1)
118 p1 = p2;
119 }
120#endif
83f64091
FB
121 if (p1)
122 p1++;
123 else
124 p1 = base_path;
125 if (p1 > p)
126 p = p1;
127 len = p - base_path;
128 if (len > dest_size - 1)
129 len = dest_size - 1;
130 memcpy(dest, base_path, len);
131 dest[len] = '\0';
132 pstrcat(dest, dest_size, filename);
3b0d4f61 133 }
3b0d4f61
FB
134}
135
5efa9d5a 136void bdrv_register(BlockDriver *bdrv)
ea2384d3 137{
f141eafe 138 if (!bdrv->bdrv_aio_readv) {
83f64091 139 /* add AIO emulation layer */
f141eafe
AL
140 bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
141 bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
eda578e5 142 } else if (!bdrv->bdrv_read) {
83f64091
FB
143 /* add synchronous IO emulation layer */
144 bdrv->bdrv_read = bdrv_read_em;
145 bdrv->bdrv_write = bdrv_write_em;
146 }
b2e12bc6
CH
147
148 if (!bdrv->bdrv_aio_flush)
149 bdrv->bdrv_aio_flush = bdrv_aio_flush_em;
150
8a22f02a 151 QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
ea2384d3 152}
b338082b
FB
153
154/* create a new block device (by default it is empty) */
155BlockDriverState *bdrv_new(const char *device_name)
156{
1b7bdbc1 157 BlockDriverState *bs;
b338082b
FB
158
159 bs = qemu_mallocz(sizeof(BlockDriverState));
b338082b 160 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
ea2384d3 161 if (device_name[0] != '\0') {
1b7bdbc1 162 QTAILQ_INSERT_TAIL(&bdrv_states, bs, list);
ea2384d3 163 }
b338082b
FB
164 return bs;
165}
166
ea2384d3
FB
167BlockDriver *bdrv_find_format(const char *format_name)
168{
169 BlockDriver *drv1;
8a22f02a
SH
170 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
171 if (!strcmp(drv1->format_name, format_name)) {
ea2384d3 172 return drv1;
8a22f02a 173 }
ea2384d3
FB
174 }
175 return NULL;
176}
177
eb852011
MA
178static int bdrv_is_whitelisted(BlockDriver *drv)
179{
180 static const char *whitelist[] = {
181 CONFIG_BDRV_WHITELIST
182 };
183 const char **p;
184
185 if (!whitelist[0])
186 return 1; /* no whitelist, anything goes */
187
188 for (p = whitelist; *p; p++) {
189 if (!strcmp(drv->format_name, *p)) {
190 return 1;
191 }
192 }
193 return 0;
194}
195
196BlockDriver *bdrv_find_whitelisted_format(const char *format_name)
197{
198 BlockDriver *drv = bdrv_find_format(format_name);
199 return drv && bdrv_is_whitelisted(drv) ? drv : NULL;
200}
201
0e7e1989
KW
202int bdrv_create(BlockDriver *drv, const char* filename,
203 QEMUOptionParameter *options)
ea2384d3
FB
204{
205 if (!drv->bdrv_create)
206 return -ENOTSUP;
0e7e1989
KW
207
208 return drv->bdrv_create(filename, options);
ea2384d3
FB
209}
210
84a12e66
CH
211int bdrv_create_file(const char* filename, QEMUOptionParameter *options)
212{
213 BlockDriver *drv;
214
b50cbabc 215 drv = bdrv_find_protocol(filename);
84a12e66
CH
216 if (drv == NULL) {
217 drv = bdrv_find_format("file");
218 }
219
220 return bdrv_create(drv, filename, options);
221}
222
d5249393 223#ifdef _WIN32
95389c86 224void get_tmp_filename(char *filename, int size)
d5249393 225{
3b9f94e1 226 char temp_dir[MAX_PATH];
3b46e624 227
3b9f94e1
FB
228 GetTempPath(MAX_PATH, temp_dir);
229 GetTempFileName(temp_dir, "qem", 0, filename);
d5249393
FB
230}
231#else
95389c86 232void get_tmp_filename(char *filename, int size)
fc01f7e7 233{
67b915a5 234 int fd;
7ccfb2eb 235 const char *tmpdir;
d5249393 236 /* XXX: race condition possible */
0badc1ee
AJ
237 tmpdir = getenv("TMPDIR");
238 if (!tmpdir)
239 tmpdir = "/tmp";
240 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
ea2384d3
FB
241 fd = mkstemp(filename);
242 close(fd);
243}
d5249393 244#endif
fc01f7e7 245
19cb3738 246#ifdef _WIN32
f45512fe
FB
247static int is_windows_drive_prefix(const char *filename)
248{
249 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
250 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
251 filename[1] == ':');
252}
3b46e624 253
508c7cb3 254int is_windows_drive(const char *filename)
19cb3738 255{
5fafdf24 256 if (is_windows_drive_prefix(filename) &&
f45512fe 257 filename[2] == '\0')
19cb3738
FB
258 return 1;
259 if (strstart(filename, "\\\\.\\", NULL) ||
260 strstart(filename, "//./", NULL))
261 return 1;
262 return 0;
263}
264#endif
265
84a12e66
CH
266/*
267 * Detect host devices. By convention, /dev/cdrom[N] is always
268 * recognized as a host CDROM.
269 */
270static BlockDriver *find_hdev_driver(const char *filename)
271{
272 int score_max = 0, score;
273 BlockDriver *drv = NULL, *d;
274
275 QLIST_FOREACH(d, &bdrv_drivers, list) {
276 if (d->bdrv_probe_device) {
277 score = d->bdrv_probe_device(filename);
278 if (score > score_max) {
279 score_max = score;
280 drv = d;
281 }
282 }
283 }
284
285 return drv;
286}
287
b50cbabc 288BlockDriver *bdrv_find_protocol(const char *filename)
83f64091
FB
289{
290 BlockDriver *drv1;
291 char protocol[128];
1cec71e3 292 int len;
83f64091 293 const char *p;
19cb3738 294
66f82cee
KW
295 /* TODO Drivers without bdrv_file_open must be specified explicitly */
296
39508e7a
CH
297 /*
298 * XXX(hch): we really should not let host device detection
299 * override an explicit protocol specification, but moving this
300 * later breaks access to device names with colons in them.
301 * Thanks to the brain-dead persistent naming schemes on udev-
302 * based Linux systems those actually are quite common.
303 */
304 drv1 = find_hdev_driver(filename);
305 if (drv1) {
306 return drv1;
307 }
308
19cb3738 309#ifdef _WIN32
39508e7a
CH
310 if (is_windows_drive(filename) ||
311 is_windows_drive_prefix(filename))
312 return bdrv_find_format("file");
19cb3738 313#endif
39508e7a 314
1cec71e3 315 p = strchr(filename, ':');
39508e7a
CH
316 if (!p) {
317 return bdrv_find_format("file");
84a12e66 318 }
1cec71e3
AL
319 len = p - filename;
320 if (len > sizeof(protocol) - 1)
321 len = sizeof(protocol) - 1;
322 memcpy(protocol, filename, len);
323 protocol[len] = '\0';
8a22f02a 324 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
5fafdf24 325 if (drv1->protocol_name &&
8a22f02a 326 !strcmp(drv1->protocol_name, protocol)) {
83f64091 327 return drv1;
8a22f02a 328 }
83f64091
FB
329 }
330 return NULL;
331}
332
f3a5d3f8
CH
333static BlockDriver *find_image_format(const char *filename)
334{
335 int ret, score, score_max;
336 BlockDriver *drv1, *drv;
337 uint8_t buf[2048];
338 BlockDriverState *bs;
339
f5edb014 340 ret = bdrv_file_open(&bs, filename, 0);
83f64091
FB
341 if (ret < 0)
342 return NULL;
f8ea0b00 343
08a00559
KW
344 /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
345 if (bs->sg || !bdrv_is_inserted(bs)) {
1a396859 346 bdrv_delete(bs);
f8ea0b00 347 return bdrv_find_format("raw");
1a396859 348 }
f8ea0b00 349
83f64091
FB
350 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
351 bdrv_delete(bs);
352 if (ret < 0) {
353 return NULL;
354 }
355
ea2384d3 356 score_max = 0;
84a12e66 357 drv = NULL;
8a22f02a 358 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
83f64091
FB
359 if (drv1->bdrv_probe) {
360 score = drv1->bdrv_probe(buf, ret, filename);
361 if (score > score_max) {
362 score_max = score;
363 drv = drv1;
364 }
0849bf08 365 }
fc01f7e7 366 }
ea2384d3
FB
367 return drv;
368}
369
51762288
SH
370/**
371 * Set the current 'total_sectors' value
372 */
373static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
374{
375 BlockDriver *drv = bs->drv;
376
396759ad
NB
377 /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
378 if (bs->sg)
379 return 0;
380
51762288
SH
381 /* query actual device if possible, otherwise just trust the hint */
382 if (drv->bdrv_getlength) {
383 int64_t length = drv->bdrv_getlength(bs);
384 if (length < 0) {
385 return length;
386 }
387 hint = length >> BDRV_SECTOR_BITS;
388 }
389
390 bs->total_sectors = hint;
391 return 0;
392}
393
57915332
KW
394/*
395 * Common part for opening disk images and files
396 */
397static int bdrv_open_common(BlockDriverState *bs, const char *filename,
398 int flags, BlockDriver *drv)
399{
400 int ret, open_flags;
401
402 assert(drv != NULL);
403
66f82cee 404 bs->file = NULL;
51762288 405 bs->total_sectors = 0;
57915332
KW
406 bs->encrypted = 0;
407 bs->valid_key = 0;
408 bs->open_flags = flags;
409 /* buffer_alignment defaulted to 512, drivers can change this value */
410 bs->buffer_alignment = 512;
411
412 pstrcpy(bs->filename, sizeof(bs->filename), filename);
413
414 if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {
415 return -ENOTSUP;
416 }
417
418 bs->drv = drv;
419 bs->opaque = qemu_mallocz(drv->instance_size);
420
421 /*
422 * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a
423 * write cache to the guest. We do need the fdatasync to flush
424 * out transactions for block allocations, and we maybe have a
425 * volatile write cache in our backing device to deal with.
426 */
427 if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE))
428 bs->enable_write_cache = 1;
429
430 /*
431 * Clear flags that are internal to the block layer before opening the
432 * image.
433 */
434 open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
435
436 /*
437 * Snapshots should be writeable.
438 */
439 if (bs->is_temporary) {
440 open_flags |= BDRV_O_RDWR;
441 }
442
66f82cee
KW
443 /* Open the image, either directly or using a protocol */
444 if (drv->bdrv_file_open) {
445 ret = drv->bdrv_file_open(bs, filename, open_flags);
446 } else {
447 ret = bdrv_file_open(&bs->file, filename, open_flags);
448 if (ret >= 0) {
449 ret = drv->bdrv_open(bs, open_flags);
450 }
451 }
452
57915332
KW
453 if (ret < 0) {
454 goto free_and_fail;
455 }
456
457 bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR);
51762288
SH
458
459 ret = refresh_total_sectors(bs, bs->total_sectors);
460 if (ret < 0) {
461 goto free_and_fail;
57915332 462 }
51762288 463
57915332
KW
464#ifndef _WIN32
465 if (bs->is_temporary) {
466 unlink(filename);
467 }
468#endif
469 return 0;
470
471free_and_fail:
66f82cee
KW
472 if (bs->file) {
473 bdrv_delete(bs->file);
474 bs->file = NULL;
475 }
57915332
KW
476 qemu_free(bs->opaque);
477 bs->opaque = NULL;
478 bs->drv = NULL;
479 return ret;
480}
481
b6ce07aa
KW
482/*
483 * Opens a file using a protocol (file, host_device, nbd, ...)
484 */
83f64091 485int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
ea2384d3 486{
83f64091 487 BlockDriverState *bs;
6db95603 488 BlockDriver *drv;
83f64091
FB
489 int ret;
490
b50cbabc 491 drv = bdrv_find_protocol(filename);
6db95603
CH
492 if (!drv) {
493 return -ENOENT;
494 }
495
83f64091 496 bs = bdrv_new("");
b6ce07aa 497 ret = bdrv_open_common(bs, filename, flags, drv);
83f64091
FB
498 if (ret < 0) {
499 bdrv_delete(bs);
500 return ret;
3b0d4f61 501 }
71d0770c 502 bs->growable = 1;
83f64091
FB
503 *pbs = bs;
504 return 0;
505}
506
b6ce07aa
KW
507/*
508 * Opens a disk image (raw, qcow2, vmdk, ...)
509 */
d6e9098e
KW
510int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
511 BlockDriver *drv)
ea2384d3 512{
b6ce07aa 513 int ret;
712e7874 514
83f64091 515 if (flags & BDRV_O_SNAPSHOT) {
ea2384d3
FB
516 BlockDriverState *bs1;
517 int64_t total_size;
7c96d46e 518 int is_protocol = 0;
91a073a9
KW
519 BlockDriver *bdrv_qcow2;
520 QEMUOptionParameter *options;
b6ce07aa
KW
521 char tmp_filename[PATH_MAX];
522 char backing_filename[PATH_MAX];
3b46e624 523
ea2384d3
FB
524 /* if snapshot, we create a temporary backing file and open it
525 instead of opening 'filename' directly */
33e3963e 526
ea2384d3
FB
527 /* if there is a backing file, use it */
528 bs1 = bdrv_new("");
d6e9098e 529 ret = bdrv_open(bs1, filename, 0, drv);
51d7c00c 530 if (ret < 0) {
ea2384d3 531 bdrv_delete(bs1);
51d7c00c 532 return ret;
ea2384d3 533 }
3e82990b 534 total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK;
7c96d46e
AL
535
536 if (bs1->drv && bs1->drv->protocol_name)
537 is_protocol = 1;
538
ea2384d3 539 bdrv_delete(bs1);
3b46e624 540
ea2384d3 541 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
7c96d46e
AL
542
543 /* Real path is meaningless for protocols */
544 if (is_protocol)
545 snprintf(backing_filename, sizeof(backing_filename),
546 "%s", filename);
114cdfa9
KS
547 else if (!realpath(filename, backing_filename))
548 return -errno;
7c96d46e 549
91a073a9
KW
550 bdrv_qcow2 = bdrv_find_format("qcow2");
551 options = parse_option_parameters("", bdrv_qcow2->create_options, NULL);
552
3e82990b 553 set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size);
91a073a9
KW
554 set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename);
555 if (drv) {
556 set_option_parameter(options, BLOCK_OPT_BACKING_FMT,
557 drv->format_name);
558 }
559
560 ret = bdrv_create(bdrv_qcow2, tmp_filename, options);
d748768c 561 free_option_parameters(options);
51d7c00c
AL
562 if (ret < 0) {
563 return ret;
ea2384d3 564 }
91a073a9 565
ea2384d3 566 filename = tmp_filename;
91a073a9 567 drv = bdrv_qcow2;
ea2384d3
FB
568 bs->is_temporary = 1;
569 }
712e7874 570
b6ce07aa 571 /* Find the right image format driver */
6db95603 572 if (!drv) {
84a12e66 573 drv = find_image_format(filename);
51d7c00c 574 }
6987307c 575
51d7c00c
AL
576 if (!drv) {
577 ret = -ENOENT;
578 goto unlink_and_fail;
ea2384d3 579 }
b6ce07aa
KW
580
581 /* Open the image */
582 ret = bdrv_open_common(bs, filename, flags, drv);
583 if (ret < 0) {
6987307c
CH
584 goto unlink_and_fail;
585 }
586
b6ce07aa
KW
587 /* If there is a backing file, use it */
588 if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') {
589 char backing_filename[PATH_MAX];
590 int back_flags;
591 BlockDriver *back_drv = NULL;
592
593 bs->backing_hd = bdrv_new("");
594 path_combine(backing_filename, sizeof(backing_filename),
595 filename, bs->backing_file);
596 if (bs->backing_format[0] != '\0')
597 back_drv = bdrv_find_format(bs->backing_format);
598
599 /* backing files always opened read-only */
600 back_flags =
601 flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
602
603 ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv);
604 if (ret < 0) {
605 bdrv_close(bs);
606 return ret;
607 }
608 if (bs->is_temporary) {
609 bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR);
610 } else {
611 /* base image inherits from "parent" */
612 bs->backing_hd->keep_read_only = bs->keep_read_only;
613 }
614 }
615
616 if (!bdrv_key_required(bs)) {
617 /* call the change callback */
618 bs->media_changed = 1;
619 if (bs->change_cb)
620 bs->change_cb(bs->change_opaque);
621 }
622
623 return 0;
624
625unlink_and_fail:
626 if (bs->is_temporary) {
627 unlink(filename);
628 }
629 return ret;
630}
631
fc01f7e7
FB
632void bdrv_close(BlockDriverState *bs)
633{
19cb3738 634 if (bs->drv) {
f9092b10
MA
635 if (bs == bs_snapshots) {
636 bs_snapshots = NULL;
637 }
557df6ac 638 if (bs->backing_hd) {
ea2384d3 639 bdrv_delete(bs->backing_hd);
557df6ac
SH
640 bs->backing_hd = NULL;
641 }
ea2384d3
FB
642 bs->drv->bdrv_close(bs);
643 qemu_free(bs->opaque);
644#ifdef _WIN32
645 if (bs->is_temporary) {
646 unlink(bs->filename);
647 }
67b915a5 648#endif
ea2384d3
FB
649 bs->opaque = NULL;
650 bs->drv = NULL;
b338082b 651
66f82cee
KW
652 if (bs->file != NULL) {
653 bdrv_close(bs->file);
654 }
655
b338082b 656 /* call the change callback */
19cb3738 657 bs->media_changed = 1;
b338082b
FB
658 if (bs->change_cb)
659 bs->change_cb(bs->change_opaque);
660 }
661}
662
2bc93fed
MK
663void bdrv_close_all(void)
664{
665 BlockDriverState *bs;
666
667 QTAILQ_FOREACH(bs, &bdrv_states, list) {
668 bdrv_close(bs);
669 }
670}
671
b338082b
FB
672void bdrv_delete(BlockDriverState *bs)
673{
18846dee
MA
674 assert(!bs->peer);
675
1b7bdbc1
SH
676 /* remove from list, if necessary */
677 if (bs->device_name[0] != '\0') {
678 QTAILQ_REMOVE(&bdrv_states, bs, list);
679 }
34c6f050 680
b338082b 681 bdrv_close(bs);
66f82cee
KW
682 if (bs->file != NULL) {
683 bdrv_delete(bs->file);
684 }
685
f9092b10 686 assert(bs != bs_snapshots);
b338082b 687 qemu_free(bs);
fc01f7e7
FB
688}
689
18846dee
MA
690int bdrv_attach(BlockDriverState *bs, DeviceState *qdev)
691{
692 if (bs->peer) {
693 return -EBUSY;
694 }
695 bs->peer = qdev;
696 return 0;
697}
698
699void bdrv_detach(BlockDriverState *bs, DeviceState *qdev)
700{
701 assert(bs->peer == qdev);
702 bs->peer = NULL;
703}
704
705DeviceState *bdrv_get_attached(BlockDriverState *bs)
706{
707 return bs->peer;
708}
709
e97fc193
AL
710/*
711 * Run consistency checks on an image
712 *
e076f338
KW
713 * Returns 0 if the check could be completed (it doesn't mean that the image is
714 * free of errors) or -errno when an internal error occured. The results of the
715 * check are stored in res.
e97fc193 716 */
e076f338 717int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res)
e97fc193
AL
718{
719 if (bs->drv->bdrv_check == NULL) {
720 return -ENOTSUP;
721 }
722
e076f338 723 memset(res, 0, sizeof(*res));
9ac228e0 724 return bs->drv->bdrv_check(bs, res);
e97fc193
AL
725}
726
33e3963e
FB
727/* commit COW file into the raw image */
728int bdrv_commit(BlockDriverState *bs)
729{
19cb3738 730 BlockDriver *drv = bs->drv;
83f64091 731 int64_t i, total_sectors;
4dca4b63
NS
732 int n, j, ro, open_flags;
733 int ret = 0, rw_ret = 0;
eb5a3165 734 unsigned char sector[BDRV_SECTOR_SIZE];
4dca4b63
NS
735 char filename[1024];
736 BlockDriverState *bs_rw, *bs_ro;
33e3963e 737
19cb3738
FB
738 if (!drv)
739 return -ENOMEDIUM;
4dca4b63
NS
740
741 if (!bs->backing_hd) {
742 return -ENOTSUP;
33e3963e
FB
743 }
744
4dca4b63
NS
745 if (bs->backing_hd->keep_read_only) {
746 return -EACCES;
747 }
748
749 ro = bs->backing_hd->read_only;
750 strncpy(filename, bs->backing_hd->filename, sizeof(filename));
751 open_flags = bs->backing_hd->open_flags;
752
753 if (ro) {
754 /* re-open as RW */
755 bdrv_delete(bs->backing_hd);
756 bs->backing_hd = NULL;
757 bs_rw = bdrv_new("");
c3349197 758 rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR, drv);
4dca4b63
NS
759 if (rw_ret < 0) {
760 bdrv_delete(bs_rw);
761 /* try to re-open read-only */
762 bs_ro = bdrv_new("");
c3349197 763 ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, drv);
4dca4b63
NS
764 if (ret < 0) {
765 bdrv_delete(bs_ro);
766 /* drive not functional anymore */
767 bs->drv = NULL;
768 return ret;
769 }
770 bs->backing_hd = bs_ro;
771 return rw_ret;
772 }
773 bs->backing_hd = bs_rw;
ea2384d3 774 }
33e3963e 775
6ea44308 776 total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
83f64091 777 for (i = 0; i < total_sectors;) {
19cb3738 778 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
ea2384d3
FB
779 for(j = 0; j < n; j++) {
780 if (bdrv_read(bs, i, sector, 1) != 0) {
4dca4b63
NS
781 ret = -EIO;
782 goto ro_cleanup;
ea2384d3
FB
783 }
784
785 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
4dca4b63
NS
786 ret = -EIO;
787 goto ro_cleanup;
ea2384d3
FB
788 }
789 i++;
33e3963e 790 }
ea2384d3
FB
791 } else {
792 i += n;
793 }
33e3963e 794 }
95389c86 795
1d44952f
CH
796 if (drv->bdrv_make_empty) {
797 ret = drv->bdrv_make_empty(bs);
798 bdrv_flush(bs);
799 }
95389c86 800
3f5075ae
CH
801 /*
802 * Make sure all data we wrote to the backing device is actually
803 * stable on disk.
804 */
805 if (bs->backing_hd)
806 bdrv_flush(bs->backing_hd);
4dca4b63
NS
807
808ro_cleanup:
809
810 if (ro) {
811 /* re-open as RO */
812 bdrv_delete(bs->backing_hd);
813 bs->backing_hd = NULL;
814 bs_ro = bdrv_new("");
c3349197 815 ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, drv);
4dca4b63
NS
816 if (ret < 0) {
817 bdrv_delete(bs_ro);
818 /* drive not functional anymore */
819 bs->drv = NULL;
820 return ret;
821 }
822 bs->backing_hd = bs_ro;
823 bs->backing_hd->keep_read_only = 0;
824 }
825
1d44952f 826 return ret;
33e3963e
FB
827}
828
6ab4b5ab
MA
829void bdrv_commit_all(void)
830{
831 BlockDriverState *bs;
832
833 QTAILQ_FOREACH(bs, &bdrv_states, list) {
834 bdrv_commit(bs);
835 }
836}
837
756e6736
KW
838/*
839 * Return values:
840 * 0 - success
841 * -EINVAL - backing format specified, but no file
842 * -ENOSPC - can't update the backing file because no space is left in the
843 * image file header
844 * -ENOTSUP - format driver doesn't support changing the backing file
845 */
846int bdrv_change_backing_file(BlockDriverState *bs,
847 const char *backing_file, const char *backing_fmt)
848{
849 BlockDriver *drv = bs->drv;
850
851 if (drv->bdrv_change_backing_file != NULL) {
852 return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
853 } else {
854 return -ENOTSUP;
855 }
856}
857
71d0770c
AL
858static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
859 size_t size)
860{
861 int64_t len;
862
863 if (!bdrv_is_inserted(bs))
864 return -ENOMEDIUM;
865
866 if (bs->growable)
867 return 0;
868
869 len = bdrv_getlength(bs);
870
fbb7b4e0
KW
871 if (offset < 0)
872 return -EIO;
873
874 if ((offset > len) || (len - offset < size))
71d0770c
AL
875 return -EIO;
876
877 return 0;
878}
879
880static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
881 int nb_sectors)
882{
eb5a3165
JS
883 return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE,
884 nb_sectors * BDRV_SECTOR_SIZE);
71d0770c
AL
885}
886
19cb3738 887/* return < 0 if error. See bdrv_write() for the return codes */
5fafdf24 888int bdrv_read(BlockDriverState *bs, int64_t sector_num,
fc01f7e7
FB
889 uint8_t *buf, int nb_sectors)
890{
ea2384d3
FB
891 BlockDriver *drv = bs->drv;
892
19cb3738
FB
893 if (!drv)
894 return -ENOMEDIUM;
71d0770c
AL
895 if (bdrv_check_request(bs, sector_num, nb_sectors))
896 return -EIO;
b338082b 897
eda578e5 898 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
fc01f7e7
FB
899}
900
7cd1e32a 901static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num,
a55eb92c 902 int nb_sectors, int dirty)
7cd1e32a 903{
904 int64_t start, end;
c6d22830 905 unsigned long val, idx, bit;
a55eb92c 906
6ea44308 907 start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
c6d22830 908 end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
a55eb92c
JK
909
910 for (; start <= end; start++) {
c6d22830
JK
911 idx = start / (sizeof(unsigned long) * 8);
912 bit = start % (sizeof(unsigned long) * 8);
913 val = bs->dirty_bitmap[idx];
914 if (dirty) {
aaa0eb75
LS
915 if (!(val & (1 << bit))) {
916 bs->dirty_count++;
917 val |= 1 << bit;
918 }
c6d22830 919 } else {
aaa0eb75
LS
920 if (val & (1 << bit)) {
921 bs->dirty_count--;
922 val &= ~(1 << bit);
923 }
c6d22830
JK
924 }
925 bs->dirty_bitmap[idx] = val;
7cd1e32a 926 }
927}
928
5fafdf24 929/* Return < 0 if error. Important errors are:
19cb3738
FB
930 -EIO generic I/O error (may happen for all errors)
931 -ENOMEDIUM No media inserted.
932 -EINVAL Invalid sector number or nb_sectors
933 -EACCES Trying to write a read-only device
934*/
5fafdf24 935int bdrv_write(BlockDriverState *bs, int64_t sector_num,
fc01f7e7
FB
936 const uint8_t *buf, int nb_sectors)
937{
83f64091 938 BlockDriver *drv = bs->drv;
19cb3738
FB
939 if (!bs->drv)
940 return -ENOMEDIUM;
0849bf08 941 if (bs->read_only)
19cb3738 942 return -EACCES;
71d0770c
AL
943 if (bdrv_check_request(bs, sector_num, nb_sectors))
944 return -EIO;
a55eb92c 945
c6d22830 946 if (bs->dirty_bitmap) {
7cd1e32a 947 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
948 }
a55eb92c 949
294cc35f
KW
950 if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
951 bs->wr_highest_sector = sector_num + nb_sectors - 1;
952 }
953
42fb2807 954 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
83f64091
FB
955}
956
eda578e5
AL
957int bdrv_pread(BlockDriverState *bs, int64_t offset,
958 void *buf, int count1)
83f64091 959{
6ea44308 960 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
83f64091
FB
961 int len, nb_sectors, count;
962 int64_t sector_num;
9a8c4cce 963 int ret;
83f64091
FB
964
965 count = count1;
966 /* first read to align to sector start */
6ea44308 967 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
83f64091
FB
968 if (len > count)
969 len = count;
6ea44308 970 sector_num = offset >> BDRV_SECTOR_BITS;
83f64091 971 if (len > 0) {
9a8c4cce
KW
972 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
973 return ret;
6ea44308 974 memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
83f64091
FB
975 count -= len;
976 if (count == 0)
977 return count1;
978 sector_num++;
979 buf += len;
980 }
981
982 /* read the sectors "in place" */
6ea44308 983 nb_sectors = count >> BDRV_SECTOR_BITS;
83f64091 984 if (nb_sectors > 0) {
9a8c4cce
KW
985 if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0)
986 return ret;
83f64091 987 sector_num += nb_sectors;
6ea44308 988 len = nb_sectors << BDRV_SECTOR_BITS;
83f64091
FB
989 buf += len;
990 count -= len;
991 }
992
993 /* add data from the last sector */
994 if (count > 0) {
9a8c4cce
KW
995 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
996 return ret;
83f64091
FB
997 memcpy(buf, tmp_buf, count);
998 }
999 return count1;
1000}
1001
eda578e5
AL
1002int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
1003 const void *buf, int count1)
83f64091 1004{
6ea44308 1005 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
83f64091
FB
1006 int len, nb_sectors, count;
1007 int64_t sector_num;
9a8c4cce 1008 int ret;
83f64091
FB
1009
1010 count = count1;
1011 /* first write to align to sector start */
6ea44308 1012 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
83f64091
FB
1013 if (len > count)
1014 len = count;
6ea44308 1015 sector_num = offset >> BDRV_SECTOR_BITS;
83f64091 1016 if (len > 0) {
9a8c4cce
KW
1017 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1018 return ret;
6ea44308 1019 memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len);
9a8c4cce
KW
1020 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
1021 return ret;
83f64091
FB
1022 count -= len;
1023 if (count == 0)
1024 return count1;
1025 sector_num++;
1026 buf += len;
1027 }
1028
1029 /* write the sectors "in place" */
6ea44308 1030 nb_sectors = count >> BDRV_SECTOR_BITS;
83f64091 1031 if (nb_sectors > 0) {
9a8c4cce
KW
1032 if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0)
1033 return ret;
83f64091 1034 sector_num += nb_sectors;
6ea44308 1035 len = nb_sectors << BDRV_SECTOR_BITS;
83f64091
FB
1036 buf += len;
1037 count -= len;
1038 }
1039
1040 /* add data from the last sector */
1041 if (count > 0) {
9a8c4cce
KW
1042 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1043 return ret;
83f64091 1044 memcpy(tmp_buf, buf, count);
9a8c4cce
KW
1045 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
1046 return ret;
83f64091
FB
1047 }
1048 return count1;
1049}
83f64091 1050
f08145fe
KW
1051/*
1052 * Writes to the file and ensures that no writes are reordered across this
1053 * request (acts as a barrier)
1054 *
1055 * Returns 0 on success, -errno in error cases.
1056 */
1057int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset,
1058 const void *buf, int count)
1059{
1060 int ret;
1061
1062 ret = bdrv_pwrite(bs, offset, buf, count);
1063 if (ret < 0) {
1064 return ret;
1065 }
1066
1067 /* No flush needed for cache=writethrough, it uses O_DSYNC */
1068 if ((bs->open_flags & BDRV_O_CACHE_MASK) != 0) {
1069 bdrv_flush(bs);
1070 }
1071
1072 return 0;
1073}
1074
1075/*
1076 * Writes to the file and ensures that no writes are reordered across this
1077 * request (acts as a barrier)
1078 *
1079 * Returns 0 on success, -errno in error cases.
1080 */
1081int bdrv_write_sync(BlockDriverState *bs, int64_t sector_num,
1082 const uint8_t *buf, int nb_sectors)
1083{
1084 return bdrv_pwrite_sync(bs, BDRV_SECTOR_SIZE * sector_num,
1085 buf, BDRV_SECTOR_SIZE * nb_sectors);
1086}
1087
83f64091
FB
1088/**
1089 * Truncate file to 'offset' bytes (needed only for file protocols)
1090 */
1091int bdrv_truncate(BlockDriverState *bs, int64_t offset)
1092{
1093 BlockDriver *drv = bs->drv;
51762288 1094 int ret;
83f64091 1095 if (!drv)
19cb3738 1096 return -ENOMEDIUM;
83f64091
FB
1097 if (!drv->bdrv_truncate)
1098 return -ENOTSUP;
59f2689d
NS
1099 if (bs->read_only)
1100 return -EACCES;
51762288
SH
1101 ret = drv->bdrv_truncate(bs, offset);
1102 if (ret == 0) {
1103 ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
1104 }
1105 return ret;
83f64091
FB
1106}
1107
1108/**
1109 * Length of a file in bytes. Return < 0 if error or unknown.
1110 */
1111int64_t bdrv_getlength(BlockDriverState *bs)
1112{
1113 BlockDriver *drv = bs->drv;
1114 if (!drv)
19cb3738 1115 return -ENOMEDIUM;
51762288
SH
1116
1117 /* Fixed size devices use the total_sectors value for speed instead of
1118 issuing a length query (like lseek) on each call. Also, legacy block
1119 drivers don't provide a bdrv_getlength function and must use
1120 total_sectors. */
1121 if (!bs->growable || !drv->bdrv_getlength) {
6ea44308 1122 return bs->total_sectors * BDRV_SECTOR_SIZE;
83f64091
FB
1123 }
1124 return drv->bdrv_getlength(bs);
fc01f7e7
FB
1125}
1126
19cb3738 1127/* return 0 as number of sectors if no device present or error */
96b8f136 1128void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
fc01f7e7 1129{
19cb3738
FB
1130 int64_t length;
1131 length = bdrv_getlength(bs);
1132 if (length < 0)
1133 length = 0;
1134 else
6ea44308 1135 length = length >> BDRV_SECTOR_BITS;
19cb3738 1136 *nb_sectors_ptr = length;
fc01f7e7 1137}
cf98951b 1138
f3d54fc4
AL
1139struct partition {
1140 uint8_t boot_ind; /* 0x80 - active */
1141 uint8_t head; /* starting head */
1142 uint8_t sector; /* starting sector */
1143 uint8_t cyl; /* starting cylinder */
1144 uint8_t sys_ind; /* What partition type */
1145 uint8_t end_head; /* end head */
1146 uint8_t end_sector; /* end sector */
1147 uint8_t end_cyl; /* end cylinder */
1148 uint32_t start_sect; /* starting sector counting from 0 */
1149 uint32_t nr_sects; /* nr of sectors in partition */
1150} __attribute__((packed));
1151
1152/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
1153static int guess_disk_lchs(BlockDriverState *bs,
1154 int *pcylinders, int *pheads, int *psectors)
1155{
eb5a3165 1156 uint8_t buf[BDRV_SECTOR_SIZE];
f3d54fc4
AL
1157 int ret, i, heads, sectors, cylinders;
1158 struct partition *p;
1159 uint32_t nr_sects;
a38131b6 1160 uint64_t nb_sectors;
f3d54fc4
AL
1161
1162 bdrv_get_geometry(bs, &nb_sectors);
1163
1164 ret = bdrv_read(bs, 0, buf, 1);
1165 if (ret < 0)
1166 return -1;
1167 /* test msdos magic */
1168 if (buf[510] != 0x55 || buf[511] != 0xaa)
1169 return -1;
1170 for(i = 0; i < 4; i++) {
1171 p = ((struct partition *)(buf + 0x1be)) + i;
1172 nr_sects = le32_to_cpu(p->nr_sects);
1173 if (nr_sects && p->end_head) {
1174 /* We make the assumption that the partition terminates on
1175 a cylinder boundary */
1176 heads = p->end_head + 1;
1177 sectors = p->end_sector & 63;
1178 if (sectors == 0)
1179 continue;
1180 cylinders = nb_sectors / (heads * sectors);
1181 if (cylinders < 1 || cylinders > 16383)
1182 continue;
1183 *pheads = heads;
1184 *psectors = sectors;
1185 *pcylinders = cylinders;
1186#if 0
1187 printf("guessed geometry: LCHS=%d %d %d\n",
1188 cylinders, heads, sectors);
1189#endif
1190 return 0;
1191 }
1192 }
1193 return -1;
1194}
1195
1196void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
1197{
1198 int translation, lba_detected = 0;
1199 int cylinders, heads, secs;
a38131b6 1200 uint64_t nb_sectors;
f3d54fc4
AL
1201
1202 /* if a geometry hint is available, use it */
1203 bdrv_get_geometry(bs, &nb_sectors);
1204 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
1205 translation = bdrv_get_translation_hint(bs);
1206 if (cylinders != 0) {
1207 *pcyls = cylinders;
1208 *pheads = heads;
1209 *psecs = secs;
1210 } else {
1211 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
1212 if (heads > 16) {
1213 /* if heads > 16, it means that a BIOS LBA
1214 translation was active, so the default
1215 hardware geometry is OK */
1216 lba_detected = 1;
1217 goto default_geometry;
1218 } else {
1219 *pcyls = cylinders;
1220 *pheads = heads;
1221 *psecs = secs;
1222 /* disable any translation to be in sync with
1223 the logical geometry */
1224 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
1225 bdrv_set_translation_hint(bs,
1226 BIOS_ATA_TRANSLATION_NONE);
1227 }
1228 }
1229 } else {
1230 default_geometry:
1231 /* if no geometry, use a standard physical disk geometry */
1232 cylinders = nb_sectors / (16 * 63);
1233
1234 if (cylinders > 16383)
1235 cylinders = 16383;
1236 else if (cylinders < 2)
1237 cylinders = 2;
1238 *pcyls = cylinders;
1239 *pheads = 16;
1240 *psecs = 63;
1241 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
1242 if ((*pcyls * *pheads) <= 131072) {
1243 bdrv_set_translation_hint(bs,
1244 BIOS_ATA_TRANSLATION_LARGE);
1245 } else {
1246 bdrv_set_translation_hint(bs,
1247 BIOS_ATA_TRANSLATION_LBA);
1248 }
1249 }
1250 }
1251 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
1252 }
1253}
1254
5fafdf24 1255void bdrv_set_geometry_hint(BlockDriverState *bs,
b338082b
FB
1256 int cyls, int heads, int secs)
1257{
1258 bs->cyls = cyls;
1259 bs->heads = heads;
1260 bs->secs = secs;
1261}
1262
1263void bdrv_set_type_hint(BlockDriverState *bs, int type)
1264{
1265 bs->type = type;
1266 bs->removable = ((type == BDRV_TYPE_CDROM ||
1267 type == BDRV_TYPE_FLOPPY));
1268}
1269
46d4767d
FB
1270void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
1271{
1272 bs->translation = translation;
1273}
1274
5fafdf24 1275void bdrv_get_geometry_hint(BlockDriverState *bs,
b338082b
FB
1276 int *pcyls, int *pheads, int *psecs)
1277{
1278 *pcyls = bs->cyls;
1279 *pheads = bs->heads;
1280 *psecs = bs->secs;
1281}
1282
1283int bdrv_get_type_hint(BlockDriverState *bs)
1284{
1285 return bs->type;
1286}
1287
46d4767d
FB
1288int bdrv_get_translation_hint(BlockDriverState *bs)
1289{
1290 return bs->translation;
1291}
1292
abd7f68d
MA
1293void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error,
1294 BlockErrorAction on_write_error)
1295{
1296 bs->on_read_error = on_read_error;
1297 bs->on_write_error = on_write_error;
1298}
1299
1300BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read)
1301{
1302 return is_read ? bs->on_read_error : bs->on_write_error;
1303}
1304
7d0d6950
MA
1305void bdrv_set_removable(BlockDriverState *bs, int removable)
1306{
1307 bs->removable = removable;
1308 if (removable && bs == bs_snapshots) {
1309 bs_snapshots = NULL;
1310 }
1311}
1312
b338082b
FB
1313int bdrv_is_removable(BlockDriverState *bs)
1314{
1315 return bs->removable;
1316}
1317
1318int bdrv_is_read_only(BlockDriverState *bs)
1319{
1320 return bs->read_only;
1321}
1322
985a03b0
TS
1323int bdrv_is_sg(BlockDriverState *bs)
1324{
1325 return bs->sg;
1326}
1327
e900a7b7
CH
1328int bdrv_enable_write_cache(BlockDriverState *bs)
1329{
1330 return bs->enable_write_cache;
1331}
1332
19cb3738 1333/* XXX: no longer used */
5fafdf24 1334void bdrv_set_change_cb(BlockDriverState *bs,
b338082b
FB
1335 void (*change_cb)(void *opaque), void *opaque)
1336{
1337 bs->change_cb = change_cb;
1338 bs->change_opaque = opaque;
1339}
1340
ea2384d3
FB
1341int bdrv_is_encrypted(BlockDriverState *bs)
1342{
1343 if (bs->backing_hd && bs->backing_hd->encrypted)
1344 return 1;
1345 return bs->encrypted;
1346}
1347
c0f4ce77
AL
1348int bdrv_key_required(BlockDriverState *bs)
1349{
1350 BlockDriverState *backing_hd = bs->backing_hd;
1351
1352 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
1353 return 1;
1354 return (bs->encrypted && !bs->valid_key);
1355}
1356
ea2384d3
FB
1357int bdrv_set_key(BlockDriverState *bs, const char *key)
1358{
1359 int ret;
1360 if (bs->backing_hd && bs->backing_hd->encrypted) {
1361 ret = bdrv_set_key(bs->backing_hd, key);
1362 if (ret < 0)
1363 return ret;
1364 if (!bs->encrypted)
1365 return 0;
1366 }
fd04a2ae
SH
1367 if (!bs->encrypted) {
1368 return -EINVAL;
1369 } else if (!bs->drv || !bs->drv->bdrv_set_key) {
1370 return -ENOMEDIUM;
1371 }
c0f4ce77 1372 ret = bs->drv->bdrv_set_key(bs, key);
bb5fc20f
AL
1373 if (ret < 0) {
1374 bs->valid_key = 0;
1375 } else if (!bs->valid_key) {
1376 bs->valid_key = 1;
1377 /* call the change callback now, we skipped it on open */
1378 bs->media_changed = 1;
1379 if (bs->change_cb)
1380 bs->change_cb(bs->change_opaque);
1381 }
c0f4ce77 1382 return ret;
ea2384d3
FB
1383}
1384
1385void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
1386{
19cb3738 1387 if (!bs->drv) {
ea2384d3
FB
1388 buf[0] = '\0';
1389 } else {
1390 pstrcpy(buf, buf_size, bs->drv->format_name);
1391 }
1392}
1393
5fafdf24 1394void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
ea2384d3
FB
1395 void *opaque)
1396{
1397 BlockDriver *drv;
1398
8a22f02a 1399 QLIST_FOREACH(drv, &bdrv_drivers, list) {
ea2384d3
FB
1400 it(opaque, drv->format_name);
1401 }
1402}
1403
b338082b
FB
1404BlockDriverState *bdrv_find(const char *name)
1405{
1406 BlockDriverState *bs;
1407
1b7bdbc1
SH
1408 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1409 if (!strcmp(name, bs->device_name)) {
b338082b 1410 return bs;
1b7bdbc1 1411 }
b338082b
FB
1412 }
1413 return NULL;
1414}
1415
2f399b0a
MA
1416BlockDriverState *bdrv_next(BlockDriverState *bs)
1417{
1418 if (!bs) {
1419 return QTAILQ_FIRST(&bdrv_states);
1420 }
1421 return QTAILQ_NEXT(bs, list);
1422}
1423
51de9760 1424void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
81d0912d
FB
1425{
1426 BlockDriverState *bs;
1427
1b7bdbc1 1428 QTAILQ_FOREACH(bs, &bdrv_states, list) {
51de9760 1429 it(opaque, bs);
81d0912d
FB
1430 }
1431}
1432
ea2384d3
FB
1433const char *bdrv_get_device_name(BlockDriverState *bs)
1434{
1435 return bs->device_name;
1436}
1437
7a6cba61
PB
1438void bdrv_flush(BlockDriverState *bs)
1439{
016f5cf6
AG
1440 if (bs->open_flags & BDRV_O_NO_FLUSH) {
1441 return;
1442 }
1443
3f5075ae 1444 if (bs->drv && bs->drv->bdrv_flush)
7a6cba61 1445 bs->drv->bdrv_flush(bs);
7a6cba61
PB
1446}
1447
c6ca28d6
AL
1448void bdrv_flush_all(void)
1449{
1450 BlockDriverState *bs;
1451
1b7bdbc1
SH
1452 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1453 if (bs->drv && !bdrv_is_read_only(bs) &&
1454 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) {
c6ca28d6 1455 bdrv_flush(bs);
1b7bdbc1
SH
1456 }
1457 }
c6ca28d6
AL
1458}
1459
f2feebbd
KW
1460int bdrv_has_zero_init(BlockDriverState *bs)
1461{
1462 assert(bs->drv);
1463
1464 if (bs->drv->no_zero_init) {
1465 return 0;
1466 } else if (bs->file) {
1467 return bdrv_has_zero_init(bs->file);
1468 }
1469
1470 return 1;
1471}
1472
f58c7b35
TS
1473/*
1474 * Returns true iff the specified sector is present in the disk image. Drivers
1475 * not implementing the functionality are assumed to not support backing files,
1476 * hence all their sectors are reported as allocated.
1477 *
1478 * 'pnum' is set to the number of sectors (including and immediately following
1479 * the specified sector) that are known to be in the same
1480 * allocated/unallocated state.
1481 *
1482 * 'nb_sectors' is the max value 'pnum' should be set to.
1483 */
1484int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1485 int *pnum)
1486{
1487 int64_t n;
1488 if (!bs->drv->bdrv_is_allocated) {
1489 if (sector_num >= bs->total_sectors) {
1490 *pnum = 0;
1491 return 0;
1492 }
1493 n = bs->total_sectors - sector_num;
1494 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1495 return 1;
1496 }
1497 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1498}
1499
2582bfed
LC
1500void bdrv_mon_event(const BlockDriverState *bdrv,
1501 BlockMonEventAction action, int is_read)
1502{
1503 QObject *data;
1504 const char *action_str;
1505
1506 switch (action) {
1507 case BDRV_ACTION_REPORT:
1508 action_str = "report";
1509 break;
1510 case BDRV_ACTION_IGNORE:
1511 action_str = "ignore";
1512 break;
1513 case BDRV_ACTION_STOP:
1514 action_str = "stop";
1515 break;
1516 default:
1517 abort();
1518 }
1519
1520 data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
1521 bdrv->device_name,
1522 action_str,
1523 is_read ? "read" : "write");
1524 monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data);
1525
1526 qobject_decref(data);
1527}
1528
d15e5465 1529static void bdrv_print_dict(QObject *obj, void *opaque)
b338082b 1530{
d15e5465
LC
1531 QDict *bs_dict;
1532 Monitor *mon = opaque;
1533
1534 bs_dict = qobject_to_qdict(obj);
1535
1536 monitor_printf(mon, "%s: type=%s removable=%d",
1537 qdict_get_str(bs_dict, "device"),
1538 qdict_get_str(bs_dict, "type"),
1539 qdict_get_bool(bs_dict, "removable"));
1540
1541 if (qdict_get_bool(bs_dict, "removable")) {
1542 monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked"));
1543 }
1544
1545 if (qdict_haskey(bs_dict, "inserted")) {
1546 QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted"));
1547
1548 monitor_printf(mon, " file=");
1549 monitor_print_filename(mon, qdict_get_str(qdict, "file"));
1550 if (qdict_haskey(qdict, "backing_file")) {
1551 monitor_printf(mon, " backing_file=");
1552 monitor_print_filename(mon, qdict_get_str(qdict, "backing_file"));
1553 }
1554 monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
1555 qdict_get_bool(qdict, "ro"),
1556 qdict_get_str(qdict, "drv"),
1557 qdict_get_bool(qdict, "encrypted"));
1558 } else {
1559 monitor_printf(mon, " [not inserted]");
1560 }
1561
1562 monitor_printf(mon, "\n");
1563}
1564
1565void bdrv_info_print(Monitor *mon, const QObject *data)
1566{
1567 qlist_iter(qobject_to_qlist(data), bdrv_print_dict, mon);
1568}
1569
d15e5465
LC
1570void bdrv_info(Monitor *mon, QObject **ret_data)
1571{
1572 QList *bs_list;
b338082b
FB
1573 BlockDriverState *bs;
1574
d15e5465
LC
1575 bs_list = qlist_new();
1576
1b7bdbc1 1577 QTAILQ_FOREACH(bs, &bdrv_states, list) {
d15e5465
LC
1578 QObject *bs_obj;
1579 const char *type = "unknown";
1580
b338082b
FB
1581 switch(bs->type) {
1582 case BDRV_TYPE_HD:
d15e5465 1583 type = "hd";
b338082b
FB
1584 break;
1585 case BDRV_TYPE_CDROM:
d15e5465 1586 type = "cdrom";
b338082b
FB
1587 break;
1588 case BDRV_TYPE_FLOPPY:
d15e5465 1589 type = "floppy";
b338082b
FB
1590 break;
1591 }
d15e5465
LC
1592
1593 bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': %s, "
1594 "'removable': %i, 'locked': %i }",
1595 bs->device_name, type, bs->removable,
1596 bs->locked);
d15e5465 1597
19cb3738 1598 if (bs->drv) {
d15e5465
LC
1599 QObject *obj;
1600 QDict *bs_dict = qobject_to_qdict(bs_obj);
1601
1602 obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, "
1603 "'encrypted': %i }",
1604 bs->filename, bs->read_only,
1605 bs->drv->format_name,
1606 bdrv_is_encrypted(bs));
fef30743 1607 if (bs->backing_file[0] != '\0') {
d15e5465
LC
1608 QDict *qdict = qobject_to_qdict(obj);
1609 qdict_put(qdict, "backing_file",
1610 qstring_from_str(bs->backing_file));
376253ec 1611 }
d15e5465
LC
1612
1613 qdict_put_obj(bs_dict, "inserted", obj);
b338082b 1614 }
d15e5465 1615 qlist_append_obj(bs_list, bs_obj);
b338082b 1616 }
d15e5465
LC
1617
1618 *ret_data = QOBJECT(bs_list);
b338082b 1619}
a36e69dd 1620
218a536a 1621static void bdrv_stats_iter(QObject *data, void *opaque)
a36e69dd 1622{
218a536a
LC
1623 QDict *qdict;
1624 Monitor *mon = opaque;
1625
1626 qdict = qobject_to_qdict(data);
1627 monitor_printf(mon, "%s:", qdict_get_str(qdict, "device"));
1628
1629 qdict = qobject_to_qdict(qdict_get(qdict, "stats"));
1630 monitor_printf(mon, " rd_bytes=%" PRId64
1631 " wr_bytes=%" PRId64
1632 " rd_operations=%" PRId64
1633 " wr_operations=%" PRId64
1634 "\n",
1635 qdict_get_int(qdict, "rd_bytes"),
1636 qdict_get_int(qdict, "wr_bytes"),
1637 qdict_get_int(qdict, "rd_operations"),
1638 qdict_get_int(qdict, "wr_operations"));
1639}
1640
1641void bdrv_stats_print(Monitor *mon, const QObject *data)
1642{
1643 qlist_iter(qobject_to_qlist(data), bdrv_stats_iter, mon);
1644}
1645
294cc35f
KW
1646static QObject* bdrv_info_stats_bs(BlockDriverState *bs)
1647{
1648 QObject *res;
1649 QDict *dict;
1650
1651 res = qobject_from_jsonf("{ 'stats': {"
1652 "'rd_bytes': %" PRId64 ","
1653 "'wr_bytes': %" PRId64 ","
1654 "'rd_operations': %" PRId64 ","
1655 "'wr_operations': %" PRId64 ","
1656 "'wr_highest_offset': %" PRId64
1657 "} }",
1658 bs->rd_bytes, bs->wr_bytes,
1659 bs->rd_ops, bs->wr_ops,
5ffbbc67
BS
1660 bs->wr_highest_sector *
1661 (uint64_t)BDRV_SECTOR_SIZE);
294cc35f
KW
1662 dict = qobject_to_qdict(res);
1663
1664 if (*bs->device_name) {
1665 qdict_put(dict, "device", qstring_from_str(bs->device_name));
1666 }
1667
1668 if (bs->file) {
1669 QObject *parent = bdrv_info_stats_bs(bs->file);
1670 qdict_put_obj(dict, "parent", parent);
1671 }
1672
1673 return res;
1674}
1675
218a536a
LC
1676void bdrv_info_stats(Monitor *mon, QObject **ret_data)
1677{
1678 QObject *obj;
1679 QList *devices;
a36e69dd
TS
1680 BlockDriverState *bs;
1681
218a536a
LC
1682 devices = qlist_new();
1683
1b7bdbc1 1684 QTAILQ_FOREACH(bs, &bdrv_states, list) {
294cc35f 1685 obj = bdrv_info_stats_bs(bs);
218a536a 1686 qlist_append_obj(devices, obj);
a36e69dd 1687 }
218a536a
LC
1688
1689 *ret_data = QOBJECT(devices);
a36e69dd 1690}
ea2384d3 1691
045df330
AL
1692const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1693{
1694 if (bs->backing_hd && bs->backing_hd->encrypted)
1695 return bs->backing_file;
1696 else if (bs->encrypted)
1697 return bs->filename;
1698 else
1699 return NULL;
1700}
1701
5fafdf24 1702void bdrv_get_backing_filename(BlockDriverState *bs,
83f64091
FB
1703 char *filename, int filename_size)
1704{
b783e409 1705 if (!bs->backing_file) {
83f64091
FB
1706 pstrcpy(filename, filename_size, "");
1707 } else {
1708 pstrcpy(filename, filename_size, bs->backing_file);
1709 }
1710}
1711
5fafdf24 1712int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
faea38e7
FB
1713 const uint8_t *buf, int nb_sectors)
1714{
1715 BlockDriver *drv = bs->drv;
1716 if (!drv)
19cb3738 1717 return -ENOMEDIUM;
faea38e7
FB
1718 if (!drv->bdrv_write_compressed)
1719 return -ENOTSUP;
fbb7b4e0
KW
1720 if (bdrv_check_request(bs, sector_num, nb_sectors))
1721 return -EIO;
a55eb92c 1722
c6d22830 1723 if (bs->dirty_bitmap) {
7cd1e32a 1724 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1725 }
a55eb92c 1726
faea38e7
FB
1727 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1728}
3b46e624 1729
faea38e7
FB
1730int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1731{
1732 BlockDriver *drv = bs->drv;
1733 if (!drv)
19cb3738 1734 return -ENOMEDIUM;
faea38e7
FB
1735 if (!drv->bdrv_get_info)
1736 return -ENOTSUP;
1737 memset(bdi, 0, sizeof(*bdi));
1738 return drv->bdrv_get_info(bs, bdi);
1739}
1740
45566e9c
CH
1741int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
1742 int64_t pos, int size)
178e08a5
AL
1743{
1744 BlockDriver *drv = bs->drv;
1745 if (!drv)
1746 return -ENOMEDIUM;
7cdb1f6d
MK
1747 if (drv->bdrv_save_vmstate)
1748 return drv->bdrv_save_vmstate(bs, buf, pos, size);
1749 if (bs->file)
1750 return bdrv_save_vmstate(bs->file, buf, pos, size);
1751 return -ENOTSUP;
178e08a5
AL
1752}
1753
45566e9c
CH
1754int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1755 int64_t pos, int size)
178e08a5
AL
1756{
1757 BlockDriver *drv = bs->drv;
1758 if (!drv)
1759 return -ENOMEDIUM;
7cdb1f6d
MK
1760 if (drv->bdrv_load_vmstate)
1761 return drv->bdrv_load_vmstate(bs, buf, pos, size);
1762 if (bs->file)
1763 return bdrv_load_vmstate(bs->file, buf, pos, size);
1764 return -ENOTSUP;
178e08a5
AL
1765}
1766
8b9b0cc2
KW
1767void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
1768{
1769 BlockDriver *drv = bs->drv;
1770
1771 if (!drv || !drv->bdrv_debug_event) {
1772 return;
1773 }
1774
1775 return drv->bdrv_debug_event(bs, event);
1776
1777}
1778
faea38e7
FB
1779/**************************************************************/
1780/* handling of snapshots */
1781
feeee5ac
MDCF
1782int bdrv_can_snapshot(BlockDriverState *bs)
1783{
1784 BlockDriver *drv = bs->drv;
1785 if (!drv || bdrv_is_removable(bs) || bdrv_is_read_only(bs)) {
1786 return 0;
1787 }
1788
1789 if (!drv->bdrv_snapshot_create) {
1790 if (bs->file != NULL) {
1791 return bdrv_can_snapshot(bs->file);
1792 }
1793 return 0;
1794 }
1795
1796 return 1;
1797}
1798
f9092b10
MA
1799BlockDriverState *bdrv_snapshots(void)
1800{
1801 BlockDriverState *bs;
1802
3ac906f7 1803 if (bs_snapshots) {
f9092b10 1804 return bs_snapshots;
3ac906f7 1805 }
f9092b10
MA
1806
1807 bs = NULL;
1808 while ((bs = bdrv_next(bs))) {
1809 if (bdrv_can_snapshot(bs)) {
3ac906f7
MA
1810 bs_snapshots = bs;
1811 return bs;
f9092b10
MA
1812 }
1813 }
1814 return NULL;
f9092b10
MA
1815}
1816
5fafdf24 1817int bdrv_snapshot_create(BlockDriverState *bs,
faea38e7
FB
1818 QEMUSnapshotInfo *sn_info)
1819{
1820 BlockDriver *drv = bs->drv;
1821 if (!drv)
19cb3738 1822 return -ENOMEDIUM;
7cdb1f6d
MK
1823 if (drv->bdrv_snapshot_create)
1824 return drv->bdrv_snapshot_create(bs, sn_info);
1825 if (bs->file)
1826 return bdrv_snapshot_create(bs->file, sn_info);
1827 return -ENOTSUP;
faea38e7
FB
1828}
1829
5fafdf24 1830int bdrv_snapshot_goto(BlockDriverState *bs,
faea38e7
FB
1831 const char *snapshot_id)
1832{
1833 BlockDriver *drv = bs->drv;
7cdb1f6d
MK
1834 int ret, open_ret;
1835
faea38e7 1836 if (!drv)
19cb3738 1837 return -ENOMEDIUM;
7cdb1f6d
MK
1838 if (drv->bdrv_snapshot_goto)
1839 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1840
1841 if (bs->file) {
1842 drv->bdrv_close(bs);
1843 ret = bdrv_snapshot_goto(bs->file, snapshot_id);
1844 open_ret = drv->bdrv_open(bs, bs->open_flags);
1845 if (open_ret < 0) {
1846 bdrv_delete(bs->file);
1847 bs->drv = NULL;
1848 return open_ret;
1849 }
1850 return ret;
1851 }
1852
1853 return -ENOTSUP;
faea38e7
FB
1854}
1855
1856int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1857{
1858 BlockDriver *drv = bs->drv;
1859 if (!drv)
19cb3738 1860 return -ENOMEDIUM;
7cdb1f6d
MK
1861 if (drv->bdrv_snapshot_delete)
1862 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1863 if (bs->file)
1864 return bdrv_snapshot_delete(bs->file, snapshot_id);
1865 return -ENOTSUP;
faea38e7
FB
1866}
1867
5fafdf24 1868int bdrv_snapshot_list(BlockDriverState *bs,
faea38e7
FB
1869 QEMUSnapshotInfo **psn_info)
1870{
1871 BlockDriver *drv = bs->drv;
1872 if (!drv)
19cb3738 1873 return -ENOMEDIUM;
7cdb1f6d
MK
1874 if (drv->bdrv_snapshot_list)
1875 return drv->bdrv_snapshot_list(bs, psn_info);
1876 if (bs->file)
1877 return bdrv_snapshot_list(bs->file, psn_info);
1878 return -ENOTSUP;
faea38e7
FB
1879}
1880
1881#define NB_SUFFIXES 4
1882
1883char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1884{
1885 static const char suffixes[NB_SUFFIXES] = "KMGT";
1886 int64_t base;
1887 int i;
1888
1889 if (size <= 999) {
1890 snprintf(buf, buf_size, "%" PRId64, size);
1891 } else {
1892 base = 1024;
1893 for(i = 0; i < NB_SUFFIXES; i++) {
1894 if (size < (10 * base)) {
5fafdf24 1895 snprintf(buf, buf_size, "%0.1f%c",
faea38e7
FB
1896 (double)size / base,
1897 suffixes[i]);
1898 break;
1899 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
5fafdf24 1900 snprintf(buf, buf_size, "%" PRId64 "%c",
faea38e7
FB
1901 ((size + (base >> 1)) / base),
1902 suffixes[i]);
1903 break;
1904 }
1905 base = base * 1024;
1906 }
1907 }
1908 return buf;
1909}
1910
1911char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1912{
1913 char buf1[128], date_buf[128], clock_buf[128];
3b9f94e1
FB
1914#ifdef _WIN32
1915 struct tm *ptm;
1916#else
faea38e7 1917 struct tm tm;
3b9f94e1 1918#endif
faea38e7
FB
1919 time_t ti;
1920 int64_t secs;
1921
1922 if (!sn) {
5fafdf24
TS
1923 snprintf(buf, buf_size,
1924 "%-10s%-20s%7s%20s%15s",
faea38e7
FB
1925 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1926 } else {
1927 ti = sn->date_sec;
3b9f94e1
FB
1928#ifdef _WIN32
1929 ptm = localtime(&ti);
1930 strftime(date_buf, sizeof(date_buf),
1931 "%Y-%m-%d %H:%M:%S", ptm);
1932#else
faea38e7
FB
1933 localtime_r(&ti, &tm);
1934 strftime(date_buf, sizeof(date_buf),
1935 "%Y-%m-%d %H:%M:%S", &tm);
3b9f94e1 1936#endif
faea38e7
FB
1937 secs = sn->vm_clock_nsec / 1000000000;
1938 snprintf(clock_buf, sizeof(clock_buf),
1939 "%02d:%02d:%02d.%03d",
1940 (int)(secs / 3600),
1941 (int)((secs / 60) % 60),
5fafdf24 1942 (int)(secs % 60),
faea38e7
FB
1943 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1944 snprintf(buf, buf_size,
5fafdf24 1945 "%-10s%-20s%7s%20s%15s",
faea38e7
FB
1946 sn->id_str, sn->name,
1947 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1948 date_buf,
1949 clock_buf);
1950 }
1951 return buf;
1952}
1953
83f64091 1954
ea2384d3 1955/**************************************************************/
83f64091 1956/* async I/Os */
ea2384d3 1957
3b69e4b9 1958BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
f141eafe 1959 QEMUIOVector *qiov, int nb_sectors,
3b69e4b9 1960 BlockDriverCompletionFunc *cb, void *opaque)
83f64091
FB
1961{
1962 BlockDriver *drv = bs->drv;
a36e69dd 1963 BlockDriverAIOCB *ret;
83f64091 1964
19cb3738 1965 if (!drv)
ce1a14dc 1966 return NULL;
71d0770c
AL
1967 if (bdrv_check_request(bs, sector_num, nb_sectors))
1968 return NULL;
3b46e624 1969
f141eafe
AL
1970 ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
1971 cb, opaque);
a36e69dd
TS
1972
1973 if (ret) {
1974 /* Update stats even though technically transfer has not happened. */
6ea44308 1975 bs->rd_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
a36e69dd
TS
1976 bs->rd_ops ++;
1977 }
1978
1979 return ret;
ea2384d3
FB
1980}
1981
f141eafe
AL
1982BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1983 QEMUIOVector *qiov, int nb_sectors,
1984 BlockDriverCompletionFunc *cb, void *opaque)
ea2384d3 1985{
83f64091 1986 BlockDriver *drv = bs->drv;
a36e69dd 1987 BlockDriverAIOCB *ret;
ea2384d3 1988
19cb3738 1989 if (!drv)
ce1a14dc 1990 return NULL;
83f64091 1991 if (bs->read_only)
ce1a14dc 1992 return NULL;
71d0770c
AL
1993 if (bdrv_check_request(bs, sector_num, nb_sectors))
1994 return NULL;
83f64091 1995
c6d22830 1996 if (bs->dirty_bitmap) {
7cd1e32a 1997 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1998 }
a55eb92c 1999
f141eafe
AL
2000 ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
2001 cb, opaque);
a36e69dd
TS
2002
2003 if (ret) {
294cc35f
KW
2004 /* Update stats even though technically transfer has not happened. */
2005 bs->wr_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
2006 bs->wr_ops ++;
2007 if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
2008 bs->wr_highest_sector = sector_num + nb_sectors - 1;
2009 }
a36e69dd
TS
2010 }
2011
2012 return ret;
83f64091
FB
2013}
2014
40b4f539
KW
2015
2016typedef struct MultiwriteCB {
2017 int error;
2018 int num_requests;
2019 int num_callbacks;
2020 struct {
2021 BlockDriverCompletionFunc *cb;
2022 void *opaque;
2023 QEMUIOVector *free_qiov;
2024 void *free_buf;
2025 } callbacks[];
2026} MultiwriteCB;
2027
2028static void multiwrite_user_cb(MultiwriteCB *mcb)
2029{
2030 int i;
2031
2032 for (i = 0; i < mcb->num_callbacks; i++) {
2033 mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
1e1ea48d
SH
2034 if (mcb->callbacks[i].free_qiov) {
2035 qemu_iovec_destroy(mcb->callbacks[i].free_qiov);
2036 }
40b4f539 2037 qemu_free(mcb->callbacks[i].free_qiov);
f8a83245 2038 qemu_vfree(mcb->callbacks[i].free_buf);
40b4f539
KW
2039 }
2040}
2041
2042static void multiwrite_cb(void *opaque, int ret)
2043{
2044 MultiwriteCB *mcb = opaque;
2045
cb6d3ca0 2046 if (ret < 0 && !mcb->error) {
40b4f539 2047 mcb->error = ret;
40b4f539
KW
2048 }
2049
2050 mcb->num_requests--;
2051 if (mcb->num_requests == 0) {
de189a1b 2052 multiwrite_user_cb(mcb);
40b4f539
KW
2053 qemu_free(mcb);
2054 }
2055}
2056
2057static int multiwrite_req_compare(const void *a, const void *b)
2058{
77be4366
CH
2059 const BlockRequest *req1 = a, *req2 = b;
2060
2061 /*
2062 * Note that we can't simply subtract req2->sector from req1->sector
2063 * here as that could overflow the return value.
2064 */
2065 if (req1->sector > req2->sector) {
2066 return 1;
2067 } else if (req1->sector < req2->sector) {
2068 return -1;
2069 } else {
2070 return 0;
2071 }
40b4f539
KW
2072}
2073
2074/*
2075 * Takes a bunch of requests and tries to merge them. Returns the number of
2076 * requests that remain after merging.
2077 */
2078static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
2079 int num_reqs, MultiwriteCB *mcb)
2080{
2081 int i, outidx;
2082
2083 // Sort requests by start sector
2084 qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
2085
2086 // Check if adjacent requests touch the same clusters. If so, combine them,
2087 // filling up gaps with zero sectors.
2088 outidx = 0;
2089 for (i = 1; i < num_reqs; i++) {
2090 int merge = 0;
2091 int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
2092
2093 // This handles the cases that are valid for all block drivers, namely
2094 // exactly sequential writes and overlapping writes.
2095 if (reqs[i].sector <= oldreq_last) {
2096 merge = 1;
2097 }
2098
2099 // The block driver may decide that it makes sense to combine requests
2100 // even if there is a gap of some sectors between them. In this case,
2101 // the gap is filled with zeros (therefore only applicable for yet
2102 // unused space in format like qcow2).
2103 if (!merge && bs->drv->bdrv_merge_requests) {
2104 merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]);
2105 }
2106
e2a305fb
CH
2107 if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) {
2108 merge = 0;
2109 }
2110
40b4f539
KW
2111 if (merge) {
2112 size_t size;
2113 QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov));
2114 qemu_iovec_init(qiov,
2115 reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
2116
2117 // Add the first request to the merged one. If the requests are
2118 // overlapping, drop the last sectors of the first request.
2119 size = (reqs[i].sector - reqs[outidx].sector) << 9;
2120 qemu_iovec_concat(qiov, reqs[outidx].qiov, size);
2121
2122 // We might need to add some zeros between the two requests
2123 if (reqs[i].sector > oldreq_last) {
2124 size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
2125 uint8_t *buf = qemu_blockalign(bs, zero_bytes);
2126 memset(buf, 0, zero_bytes);
2127 qemu_iovec_add(qiov, buf, zero_bytes);
2128 mcb->callbacks[i].free_buf = buf;
2129 }
2130
2131 // Add the second request
2132 qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size);
2133
cbf1dff2 2134 reqs[outidx].nb_sectors = qiov->size >> 9;
40b4f539
KW
2135 reqs[outidx].qiov = qiov;
2136
2137 mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
2138 } else {
2139 outidx++;
2140 reqs[outidx].sector = reqs[i].sector;
2141 reqs[outidx].nb_sectors = reqs[i].nb_sectors;
2142 reqs[outidx].qiov = reqs[i].qiov;
2143 }
2144 }
2145
2146 return outidx + 1;
2147}
2148
2149/*
2150 * Submit multiple AIO write requests at once.
2151 *
2152 * On success, the function returns 0 and all requests in the reqs array have
2153 * been submitted. In error case this function returns -1, and any of the
2154 * requests may or may not be submitted yet. In particular, this means that the
2155 * callback will be called for some of the requests, for others it won't. The
2156 * caller must check the error field of the BlockRequest to wait for the right
2157 * callbacks (if error != 0, no callback will be called).
2158 *
2159 * The implementation may modify the contents of the reqs array, e.g. to merge
2160 * requests. However, the fields opaque and error are left unmodified as they
2161 * are used to signal failure for a single request to the caller.
2162 */
2163int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
2164{
2165 BlockDriverAIOCB *acb;
2166 MultiwriteCB *mcb;
2167 int i;
2168
2169 if (num_reqs == 0) {
2170 return 0;
2171 }
2172
2173 // Create MultiwriteCB structure
2174 mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
2175 mcb->num_requests = 0;
2176 mcb->num_callbacks = num_reqs;
2177
2178 for (i = 0; i < num_reqs; i++) {
2179 mcb->callbacks[i].cb = reqs[i].cb;
2180 mcb->callbacks[i].opaque = reqs[i].opaque;
2181 }
2182
2183 // Check for mergable requests
2184 num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
2185
453f9a16
KW
2186 /*
2187 * Run the aio requests. As soon as one request can't be submitted
2188 * successfully, fail all requests that are not yet submitted (we must
2189 * return failure for all requests anyway)
2190 *
2191 * num_requests cannot be set to the right value immediately: If
2192 * bdrv_aio_writev fails for some request, num_requests would be too high
2193 * and therefore multiwrite_cb() would never recognize the multiwrite
2194 * request as completed. We also cannot use the loop variable i to set it
2195 * when the first request fails because the callback may already have been
2196 * called for previously submitted requests. Thus, num_requests must be
2197 * incremented for each request that is submitted.
2198 *
2199 * The problem that callbacks may be called early also means that we need
2200 * to take care that num_requests doesn't become 0 before all requests are
2201 * submitted - multiwrite_cb() would consider the multiwrite request
2202 * completed. A dummy request that is "completed" by a manual call to
2203 * multiwrite_cb() takes care of this.
2204 */
2205 mcb->num_requests = 1;
2206
40b4f539 2207 for (i = 0; i < num_reqs; i++) {
453f9a16 2208 mcb->num_requests++;
40b4f539
KW
2209 acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
2210 reqs[i].nb_sectors, multiwrite_cb, mcb);
2211
2212 if (acb == NULL) {
2213 // We can only fail the whole thing if no request has been
2214 // submitted yet. Otherwise we'll wait for the submitted AIOs to
2215 // complete and report the error in the callback.
453f9a16 2216 if (i == 0) {
40b4f539
KW
2217 goto fail;
2218 } else {
7eb58a6c 2219 multiwrite_cb(mcb, -EIO);
40b4f539
KW
2220 break;
2221 }
40b4f539
KW
2222 }
2223 }
2224
453f9a16
KW
2225 /* Complete the dummy request */
2226 multiwrite_cb(mcb, 0);
2227
40b4f539
KW
2228 return 0;
2229
2230fail:
453f9a16
KW
2231 for (i = 0; i < mcb->num_callbacks; i++) {
2232 reqs[i].error = -EIO;
2233 }
af474591 2234 qemu_free(mcb);
40b4f539
KW
2235 return -1;
2236}
2237
b2e12bc6
CH
2238BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
2239 BlockDriverCompletionFunc *cb, void *opaque)
2240{
2241 BlockDriver *drv = bs->drv;
2242
016f5cf6
AG
2243 if (bs->open_flags & BDRV_O_NO_FLUSH) {
2244 return bdrv_aio_noop_em(bs, cb, opaque);
2245 }
2246
b2e12bc6
CH
2247 if (!drv)
2248 return NULL;
b2e12bc6
CH
2249 return drv->bdrv_aio_flush(bs, cb, opaque);
2250}
2251
83f64091 2252void bdrv_aio_cancel(BlockDriverAIOCB *acb)
83f64091 2253{
6bbff9a0 2254 acb->pool->cancel(acb);
83f64091
FB
2255}
2256
ce1a14dc 2257
83f64091
FB
2258/**************************************************************/
2259/* async block device emulation */
2260
c16b5a2c
CH
2261typedef struct BlockDriverAIOCBSync {
2262 BlockDriverAIOCB common;
2263 QEMUBH *bh;
2264 int ret;
2265 /* vector translation state */
2266 QEMUIOVector *qiov;
2267 uint8_t *bounce;
2268 int is_write;
2269} BlockDriverAIOCBSync;
2270
2271static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
2272{
b666d239
KW
2273 BlockDriverAIOCBSync *acb =
2274 container_of(blockacb, BlockDriverAIOCBSync, common);
6a7ad299 2275 qemu_bh_delete(acb->bh);
36afc451 2276 acb->bh = NULL;
c16b5a2c
CH
2277 qemu_aio_release(acb);
2278}
2279
2280static AIOPool bdrv_em_aio_pool = {
2281 .aiocb_size = sizeof(BlockDriverAIOCBSync),
2282 .cancel = bdrv_aio_cancel_em,
2283};
2284
ce1a14dc 2285static void bdrv_aio_bh_cb(void *opaque)
83f64091 2286{
ce1a14dc 2287 BlockDriverAIOCBSync *acb = opaque;
f141eafe 2288
f141eafe
AL
2289 if (!acb->is_write)
2290 qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
ceb42de8 2291 qemu_vfree(acb->bounce);
ce1a14dc 2292 acb->common.cb(acb->common.opaque, acb->ret);
6a7ad299 2293 qemu_bh_delete(acb->bh);
36afc451 2294 acb->bh = NULL;
ce1a14dc 2295 qemu_aio_release(acb);
83f64091 2296}
beac80cd 2297
f141eafe
AL
2298static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
2299 int64_t sector_num,
2300 QEMUIOVector *qiov,
2301 int nb_sectors,
2302 BlockDriverCompletionFunc *cb,
2303 void *opaque,
2304 int is_write)
2305
83f64091 2306{
ce1a14dc 2307 BlockDriverAIOCBSync *acb;
ce1a14dc 2308
c16b5a2c 2309 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
f141eafe
AL
2310 acb->is_write = is_write;
2311 acb->qiov = qiov;
e268ca52 2312 acb->bounce = qemu_blockalign(bs, qiov->size);
f141eafe 2313
ce1a14dc
PB
2314 if (!acb->bh)
2315 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
f141eafe
AL
2316
2317 if (is_write) {
2318 qemu_iovec_to_buffer(acb->qiov, acb->bounce);
2319 acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
2320 } else {
2321 acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
2322 }
2323
ce1a14dc 2324 qemu_bh_schedule(acb->bh);
f141eafe 2325
ce1a14dc 2326 return &acb->common;
beac80cd
FB
2327}
2328
f141eafe
AL
2329static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
2330 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
ce1a14dc 2331 BlockDriverCompletionFunc *cb, void *opaque)
beac80cd 2332{
f141eafe
AL
2333 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
2334}
83f64091 2335
f141eafe
AL
2336static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
2337 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2338 BlockDriverCompletionFunc *cb, void *opaque)
2339{
2340 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
beac80cd 2341}
beac80cd 2342
b2e12bc6
CH
2343static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
2344 BlockDriverCompletionFunc *cb, void *opaque)
2345{
2346 BlockDriverAIOCBSync *acb;
2347
2348 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2349 acb->is_write = 1; /* don't bounce in the completion hadler */
2350 acb->qiov = NULL;
2351 acb->bounce = NULL;
2352 acb->ret = 0;
2353
2354 if (!acb->bh)
2355 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2356
2357 bdrv_flush(bs);
2358 qemu_bh_schedule(acb->bh);
2359 return &acb->common;
2360}
2361
016f5cf6
AG
2362static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,
2363 BlockDriverCompletionFunc *cb, void *opaque)
2364{
2365 BlockDriverAIOCBSync *acb;
2366
2367 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2368 acb->is_write = 1; /* don't bounce in the completion handler */
2369 acb->qiov = NULL;
2370 acb->bounce = NULL;
2371 acb->ret = 0;
2372
2373 if (!acb->bh) {
2374 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2375 }
2376
2377 qemu_bh_schedule(acb->bh);
2378 return &acb->common;
2379}
2380
83f64091
FB
2381/**************************************************************/
2382/* sync block device emulation */
ea2384d3 2383
83f64091
FB
2384static void bdrv_rw_em_cb(void *opaque, int ret)
2385{
2386 *(int *)opaque = ret;
ea2384d3
FB
2387}
2388
83f64091
FB
2389#define NOT_DONE 0x7fffffff
2390
5fafdf24 2391static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
83f64091 2392 uint8_t *buf, int nb_sectors)
7a6cba61 2393{
ce1a14dc
PB
2394 int async_ret;
2395 BlockDriverAIOCB *acb;
f141eafe
AL
2396 struct iovec iov;
2397 QEMUIOVector qiov;
83f64091 2398
65d6b3d8
KW
2399 async_context_push();
2400
83f64091 2401 async_ret = NOT_DONE;
3f4cb3d3 2402 iov.iov_base = (void *)buf;
eb5a3165 2403 iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
f141eafe
AL
2404 qemu_iovec_init_external(&qiov, &iov, 1);
2405 acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
2406 bdrv_rw_em_cb, &async_ret);
65d6b3d8
KW
2407 if (acb == NULL) {
2408 async_ret = -1;
2409 goto fail;
2410 }
baf35cb9 2411
83f64091
FB
2412 while (async_ret == NOT_DONE) {
2413 qemu_aio_wait();
2414 }
baf35cb9 2415
65d6b3d8
KW
2416
2417fail:
2418 async_context_pop();
83f64091 2419 return async_ret;
7a6cba61
PB
2420}
2421
83f64091
FB
2422static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
2423 const uint8_t *buf, int nb_sectors)
2424{
ce1a14dc
PB
2425 int async_ret;
2426 BlockDriverAIOCB *acb;
f141eafe
AL
2427 struct iovec iov;
2428 QEMUIOVector qiov;
83f64091 2429
65d6b3d8
KW
2430 async_context_push();
2431
83f64091 2432 async_ret = NOT_DONE;
f141eafe 2433 iov.iov_base = (void *)buf;
eb5a3165 2434 iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
f141eafe
AL
2435 qemu_iovec_init_external(&qiov, &iov, 1);
2436 acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
2437 bdrv_rw_em_cb, &async_ret);
65d6b3d8
KW
2438 if (acb == NULL) {
2439 async_ret = -1;
2440 goto fail;
2441 }
83f64091
FB
2442 while (async_ret == NOT_DONE) {
2443 qemu_aio_wait();
2444 }
65d6b3d8
KW
2445
2446fail:
2447 async_context_pop();
83f64091
FB
2448 return async_ret;
2449}
ea2384d3
FB
2450
2451void bdrv_init(void)
2452{
5efa9d5a 2453 module_call_init(MODULE_INIT_BLOCK);
ea2384d3 2454}
ce1a14dc 2455
eb852011
MA
2456void bdrv_init_with_whitelist(void)
2457{
2458 use_bdrv_whitelist = 1;
2459 bdrv_init();
2460}
2461
c16b5a2c
CH
2462void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
2463 BlockDriverCompletionFunc *cb, void *opaque)
ce1a14dc 2464{
ce1a14dc
PB
2465 BlockDriverAIOCB *acb;
2466
6bbff9a0
AL
2467 if (pool->free_aiocb) {
2468 acb = pool->free_aiocb;
2469 pool->free_aiocb = acb->next;
ce1a14dc 2470 } else {
6bbff9a0
AL
2471 acb = qemu_mallocz(pool->aiocb_size);
2472 acb->pool = pool;
ce1a14dc
PB
2473 }
2474 acb->bs = bs;
2475 acb->cb = cb;
2476 acb->opaque = opaque;
2477 return acb;
2478}
2479
2480void qemu_aio_release(void *p)
2481{
6bbff9a0
AL
2482 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
2483 AIOPool *pool = acb->pool;
2484 acb->next = pool->free_aiocb;
2485 pool->free_aiocb = acb;
ce1a14dc 2486}
19cb3738
FB
2487
2488/**************************************************************/
2489/* removable device support */
2490
2491/**
2492 * Return TRUE if the media is present
2493 */
2494int bdrv_is_inserted(BlockDriverState *bs)
2495{
2496 BlockDriver *drv = bs->drv;
2497 int ret;
2498 if (!drv)
2499 return 0;
2500 if (!drv->bdrv_is_inserted)
2501 return 1;
2502 ret = drv->bdrv_is_inserted(bs);
2503 return ret;
2504}
2505
2506/**
2507 * Return TRUE if the media changed since the last call to this
5fafdf24 2508 * function. It is currently only used for floppy disks
19cb3738
FB
2509 */
2510int bdrv_media_changed(BlockDriverState *bs)
2511{
2512 BlockDriver *drv = bs->drv;
2513 int ret;
2514
2515 if (!drv || !drv->bdrv_media_changed)
2516 ret = -ENOTSUP;
2517 else
2518 ret = drv->bdrv_media_changed(bs);
2519 if (ret == -ENOTSUP)
2520 ret = bs->media_changed;
2521 bs->media_changed = 0;
2522 return ret;
2523}
2524
2525/**
2526 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
2527 */
aea2a33c 2528int bdrv_eject(BlockDriverState *bs, int eject_flag)
19cb3738
FB
2529{
2530 BlockDriver *drv = bs->drv;
2531 int ret;
2532
aea2a33c
MM
2533 if (bs->locked) {
2534 return -EBUSY;
2535 }
2536
19cb3738
FB
2537 if (!drv || !drv->bdrv_eject) {
2538 ret = -ENOTSUP;
2539 } else {
2540 ret = drv->bdrv_eject(bs, eject_flag);
2541 }
2542 if (ret == -ENOTSUP) {
2543 if (eject_flag)
2544 bdrv_close(bs);
aea2a33c 2545 ret = 0;
19cb3738 2546 }
aea2a33c
MM
2547
2548 return ret;
19cb3738
FB
2549}
2550
2551int bdrv_is_locked(BlockDriverState *bs)
2552{
2553 return bs->locked;
2554}
2555
2556/**
2557 * Lock or unlock the media (if it is locked, the user won't be able
2558 * to eject it manually).
2559 */
2560void bdrv_set_locked(BlockDriverState *bs, int locked)
2561{
2562 BlockDriver *drv = bs->drv;
2563
2564 bs->locked = locked;
2565 if (drv && drv->bdrv_set_locked) {
2566 drv->bdrv_set_locked(bs, locked);
2567 }
2568}
985a03b0
TS
2569
2570/* needed for generic scsi interface */
2571
2572int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
2573{
2574 BlockDriver *drv = bs->drv;
2575
2576 if (drv && drv->bdrv_ioctl)
2577 return drv->bdrv_ioctl(bs, req, buf);
2578 return -ENOTSUP;
2579}
7d780669 2580
221f715d
AL
2581BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
2582 unsigned long int req, void *buf,
2583 BlockDriverCompletionFunc *cb, void *opaque)
7d780669 2584{
221f715d 2585 BlockDriver *drv = bs->drv;
7d780669 2586
221f715d
AL
2587 if (drv && drv->bdrv_aio_ioctl)
2588 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
2589 return NULL;
7d780669 2590}
e268ca52 2591
7cd1e32a 2592
2593
e268ca52
AL
2594void *qemu_blockalign(BlockDriverState *bs, size_t size)
2595{
2596 return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
2597}
7cd1e32a 2598
2599void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable)
2600{
2601 int64_t bitmap_size;
a55eb92c 2602
aaa0eb75 2603 bs->dirty_count = 0;
a55eb92c 2604 if (enable) {
c6d22830
JK
2605 if (!bs->dirty_bitmap) {
2606 bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
2607 BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
2608 bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
a55eb92c 2609
7cd1e32a 2610 bs->dirty_bitmap = qemu_mallocz(bitmap_size);
a55eb92c 2611 }
7cd1e32a 2612 } else {
c6d22830 2613 if (bs->dirty_bitmap) {
7cd1e32a 2614 qemu_free(bs->dirty_bitmap);
c6d22830 2615 bs->dirty_bitmap = NULL;
a55eb92c 2616 }
7cd1e32a 2617 }
2618}
2619
2620int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
2621{
6ea44308 2622 int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
a55eb92c 2623
c6d22830
JK
2624 if (bs->dirty_bitmap &&
2625 (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) {
2626 return bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] &
2627 (1 << (chunk % (sizeof(unsigned long) * 8)));
7cd1e32a 2628 } else {
2629 return 0;
2630 }
2631}
2632
a55eb92c
JK
2633void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
2634 int nr_sectors)
7cd1e32a 2635{
2636 set_dirty_bitmap(bs, cur_sector, nr_sectors, 0);
2637}
aaa0eb75
LS
2638
2639int64_t bdrv_get_dirty_count(BlockDriverState *bs)
2640{
2641 return bs->dirty_count;
2642}