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