]> git.proxmox.com Git - qemu.git/blame - block.c
block: Rename bdrv_co_flush to bdrv_co_flush_to_disk
[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"
6d519a5f 26#include "trace.h"
376253ec 27#include "monitor.h"
ea2384d3 28#include "block_int.h"
5efa9d5a 29#include "module.h"
f795e743 30#include "qjson.h"
68485420 31#include "qemu-coroutine.h"
b2023818 32#include "qmp-commands.h"
fc01f7e7 33
71e72a19 34#ifdef CONFIG_BSD
7674e7bf
FB
35#include <sys/types.h>
36#include <sys/stat.h>
37#include <sys/ioctl.h>
72cf2d4f 38#include <sys/queue.h>
c5e97233 39#ifndef __DragonFly__
7674e7bf
FB
40#include <sys/disk.h>
41#endif
c5e97233 42#endif
7674e7bf 43
49dc768d
AL
44#ifdef _WIN32
45#include <windows.h>
46#endif
47
1c9805a3
SH
48#define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
49
7d4b4ba5 50static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load);
f141eafe
AL
51static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
52 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
c87c0672 53 BlockDriverCompletionFunc *cb, void *opaque);
f141eafe
AL
54static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
55 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
ce1a14dc 56 BlockDriverCompletionFunc *cb, void *opaque);
f9f05dc5
KW
57static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs,
58 int64_t sector_num, int nb_sectors,
59 QEMUIOVector *iov);
60static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs,
61 int64_t sector_num, int nb_sectors,
62 QEMUIOVector *iov);
c5fbe571
SH
63static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs,
64 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
1c9805a3
SH
65static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs,
66 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
b2a61371
SH
67static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs,
68 int64_t sector_num,
69 QEMUIOVector *qiov,
70 int nb_sectors,
71 BlockDriverCompletionFunc *cb,
72 void *opaque,
8c5873d6 73 bool is_write);
b2a61371 74static void coroutine_fn bdrv_co_do_rw(void *opaque);
ec530c81 75
1b7bdbc1
SH
76static QTAILQ_HEAD(, BlockDriverState) bdrv_states =
77 QTAILQ_HEAD_INITIALIZER(bdrv_states);
7ee930d0 78
8a22f02a
SH
79static QLIST_HEAD(, BlockDriver) bdrv_drivers =
80 QLIST_HEAD_INITIALIZER(bdrv_drivers);
ea2384d3 81
f9092b10
MA
82/* The device to use for VM snapshots */
83static BlockDriverState *bs_snapshots;
84
eb852011
MA
85/* If non-zero, use only whitelisted block drivers */
86static int use_bdrv_whitelist;
87
9e0b22f4
SH
88#ifdef _WIN32
89static int is_windows_drive_prefix(const char *filename)
90{
91 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
92 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
93 filename[1] == ':');
94}
95
96int is_windows_drive(const char *filename)
97{
98 if (is_windows_drive_prefix(filename) &&
99 filename[2] == '\0')
100 return 1;
101 if (strstart(filename, "\\\\.\\", NULL) ||
102 strstart(filename, "//./", NULL))
103 return 1;
104 return 0;
105}
106#endif
107
108/* check if the path starts with "<protocol>:" */
109static int path_has_protocol(const char *path)
110{
111#ifdef _WIN32
112 if (is_windows_drive(path) ||
113 is_windows_drive_prefix(path)) {
114 return 0;
115 }
116#endif
117
118 return strchr(path, ':') != NULL;
119}
120
83f64091 121int path_is_absolute(const char *path)
3b0d4f61 122{
83f64091 123 const char *p;
21664424
FB
124#ifdef _WIN32
125 /* specific case for names like: "\\.\d:" */
126 if (*path == '/' || *path == '\\')
127 return 1;
128#endif
83f64091
FB
129 p = strchr(path, ':');
130 if (p)
131 p++;
132 else
133 p = path;
3b9f94e1
FB
134#ifdef _WIN32
135 return (*p == '/' || *p == '\\');
136#else
137 return (*p == '/');
138#endif
3b0d4f61
FB
139}
140
83f64091
FB
141/* if filename is absolute, just copy it to dest. Otherwise, build a
142 path to it by considering it is relative to base_path. URL are
143 supported. */
144void path_combine(char *dest, int dest_size,
145 const char *base_path,
146 const char *filename)
3b0d4f61 147{
83f64091
FB
148 const char *p, *p1;
149 int len;
150
151 if (dest_size <= 0)
152 return;
153 if (path_is_absolute(filename)) {
154 pstrcpy(dest, dest_size, filename);
155 } else {
156 p = strchr(base_path, ':');
157 if (p)
158 p++;
159 else
160 p = base_path;
3b9f94e1
FB
161 p1 = strrchr(base_path, '/');
162#ifdef _WIN32
163 {
164 const char *p2;
165 p2 = strrchr(base_path, '\\');
166 if (!p1 || p2 > p1)
167 p1 = p2;
168 }
169#endif
83f64091
FB
170 if (p1)
171 p1++;
172 else
173 p1 = base_path;
174 if (p1 > p)
175 p = p1;
176 len = p - base_path;
177 if (len > dest_size - 1)
178 len = dest_size - 1;
179 memcpy(dest, base_path, len);
180 dest[len] = '\0';
181 pstrcat(dest, dest_size, filename);
3b0d4f61 182 }
3b0d4f61
FB
183}
184
5efa9d5a 185void bdrv_register(BlockDriver *bdrv)
ea2384d3 186{
8c5873d6
SH
187 /* Block drivers without coroutine functions need emulation */
188 if (!bdrv->bdrv_co_readv) {
f9f05dc5
KW
189 bdrv->bdrv_co_readv = bdrv_co_readv_em;
190 bdrv->bdrv_co_writev = bdrv_co_writev_em;
191
f8c35c1d
SH
192 /* bdrv_co_readv_em()/brdv_co_writev_em() work in terms of aio, so if
193 * the block driver lacks aio we need to emulate that too.
194 */
f9f05dc5
KW
195 if (!bdrv->bdrv_aio_readv) {
196 /* add AIO emulation layer */
197 bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
198 bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
f9f05dc5 199 }
83f64091 200 }
b2e12bc6 201
8a22f02a 202 QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
ea2384d3 203}
b338082b
FB
204
205/* create a new block device (by default it is empty) */
206BlockDriverState *bdrv_new(const char *device_name)
207{
1b7bdbc1 208 BlockDriverState *bs;
b338082b 209
7267c094 210 bs = g_malloc0(sizeof(BlockDriverState));
b338082b 211 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
ea2384d3 212 if (device_name[0] != '\0') {
1b7bdbc1 213 QTAILQ_INSERT_TAIL(&bdrv_states, bs, list);
ea2384d3 214 }
28a7282a 215 bdrv_iostatus_disable(bs);
b338082b
FB
216 return bs;
217}
218
ea2384d3
FB
219BlockDriver *bdrv_find_format(const char *format_name)
220{
221 BlockDriver *drv1;
8a22f02a
SH
222 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
223 if (!strcmp(drv1->format_name, format_name)) {
ea2384d3 224 return drv1;
8a22f02a 225 }
ea2384d3
FB
226 }
227 return NULL;
228}
229
eb852011
MA
230static int bdrv_is_whitelisted(BlockDriver *drv)
231{
232 static const char *whitelist[] = {
233 CONFIG_BDRV_WHITELIST
234 };
235 const char **p;
236
237 if (!whitelist[0])
238 return 1; /* no whitelist, anything goes */
239
240 for (p = whitelist; *p; p++) {
241 if (!strcmp(drv->format_name, *p)) {
242 return 1;
243 }
244 }
245 return 0;
246}
247
248BlockDriver *bdrv_find_whitelisted_format(const char *format_name)
249{
250 BlockDriver *drv = bdrv_find_format(format_name);
251 return drv && bdrv_is_whitelisted(drv) ? drv : NULL;
252}
253
0e7e1989
KW
254int bdrv_create(BlockDriver *drv, const char* filename,
255 QEMUOptionParameter *options)
ea2384d3
FB
256{
257 if (!drv->bdrv_create)
258 return -ENOTSUP;
0e7e1989
KW
259
260 return drv->bdrv_create(filename, options);
ea2384d3
FB
261}
262
84a12e66
CH
263int bdrv_create_file(const char* filename, QEMUOptionParameter *options)
264{
265 BlockDriver *drv;
266
b50cbabc 267 drv = bdrv_find_protocol(filename);
84a12e66 268 if (drv == NULL) {
16905d71 269 return -ENOENT;
84a12e66
CH
270 }
271
272 return bdrv_create(drv, filename, options);
273}
274
d5249393 275#ifdef _WIN32
95389c86 276void get_tmp_filename(char *filename, int size)
d5249393 277{
3b9f94e1 278 char temp_dir[MAX_PATH];
3b46e624 279
3b9f94e1
FB
280 GetTempPath(MAX_PATH, temp_dir);
281 GetTempFileName(temp_dir, "qem", 0, filename);
d5249393
FB
282}
283#else
95389c86 284void get_tmp_filename(char *filename, int size)
fc01f7e7 285{
67b915a5 286 int fd;
7ccfb2eb 287 const char *tmpdir;
d5249393 288 /* XXX: race condition possible */
0badc1ee
AJ
289 tmpdir = getenv("TMPDIR");
290 if (!tmpdir)
291 tmpdir = "/tmp";
292 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
ea2384d3
FB
293 fd = mkstemp(filename);
294 close(fd);
295}
d5249393 296#endif
fc01f7e7 297
84a12e66
CH
298/*
299 * Detect host devices. By convention, /dev/cdrom[N] is always
300 * recognized as a host CDROM.
301 */
302static BlockDriver *find_hdev_driver(const char *filename)
303{
304 int score_max = 0, score;
305 BlockDriver *drv = NULL, *d;
306
307 QLIST_FOREACH(d, &bdrv_drivers, list) {
308 if (d->bdrv_probe_device) {
309 score = d->bdrv_probe_device(filename);
310 if (score > score_max) {
311 score_max = score;
312 drv = d;
313 }
314 }
315 }
316
317 return drv;
318}
319
b50cbabc 320BlockDriver *bdrv_find_protocol(const char *filename)
83f64091
FB
321{
322 BlockDriver *drv1;
323 char protocol[128];
1cec71e3 324 int len;
83f64091 325 const char *p;
19cb3738 326
66f82cee
KW
327 /* TODO Drivers without bdrv_file_open must be specified explicitly */
328
39508e7a
CH
329 /*
330 * XXX(hch): we really should not let host device detection
331 * override an explicit protocol specification, but moving this
332 * later breaks access to device names with colons in them.
333 * Thanks to the brain-dead persistent naming schemes on udev-
334 * based Linux systems those actually are quite common.
335 */
336 drv1 = find_hdev_driver(filename);
337 if (drv1) {
338 return drv1;
339 }
340
9e0b22f4 341 if (!path_has_protocol(filename)) {
39508e7a 342 return bdrv_find_format("file");
84a12e66 343 }
9e0b22f4
SH
344 p = strchr(filename, ':');
345 assert(p != NULL);
1cec71e3
AL
346 len = p - filename;
347 if (len > sizeof(protocol) - 1)
348 len = sizeof(protocol) - 1;
349 memcpy(protocol, filename, len);
350 protocol[len] = '\0';
8a22f02a 351 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
5fafdf24 352 if (drv1->protocol_name &&
8a22f02a 353 !strcmp(drv1->protocol_name, protocol)) {
83f64091 354 return drv1;
8a22f02a 355 }
83f64091
FB
356 }
357 return NULL;
358}
359
c98ac35d 360static int find_image_format(const char *filename, BlockDriver **pdrv)
f3a5d3f8
CH
361{
362 int ret, score, score_max;
363 BlockDriver *drv1, *drv;
364 uint8_t buf[2048];
365 BlockDriverState *bs;
366
f5edb014 367 ret = bdrv_file_open(&bs, filename, 0);
c98ac35d
SW
368 if (ret < 0) {
369 *pdrv = NULL;
370 return ret;
371 }
f8ea0b00 372
08a00559
KW
373 /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
374 if (bs->sg || !bdrv_is_inserted(bs)) {
1a396859 375 bdrv_delete(bs);
c98ac35d
SW
376 drv = bdrv_find_format("raw");
377 if (!drv) {
378 ret = -ENOENT;
379 }
380 *pdrv = drv;
381 return ret;
1a396859 382 }
f8ea0b00 383
83f64091
FB
384 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
385 bdrv_delete(bs);
386 if (ret < 0) {
c98ac35d
SW
387 *pdrv = NULL;
388 return ret;
83f64091
FB
389 }
390
ea2384d3 391 score_max = 0;
84a12e66 392 drv = NULL;
8a22f02a 393 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
83f64091
FB
394 if (drv1->bdrv_probe) {
395 score = drv1->bdrv_probe(buf, ret, filename);
396 if (score > score_max) {
397 score_max = score;
398 drv = drv1;
399 }
0849bf08 400 }
fc01f7e7 401 }
c98ac35d
SW
402 if (!drv) {
403 ret = -ENOENT;
404 }
405 *pdrv = drv;
406 return ret;
ea2384d3
FB
407}
408
51762288
SH
409/**
410 * Set the current 'total_sectors' value
411 */
412static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
413{
414 BlockDriver *drv = bs->drv;
415
396759ad
NB
416 /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
417 if (bs->sg)
418 return 0;
419
51762288
SH
420 /* query actual device if possible, otherwise just trust the hint */
421 if (drv->bdrv_getlength) {
422 int64_t length = drv->bdrv_getlength(bs);
423 if (length < 0) {
424 return length;
425 }
426 hint = length >> BDRV_SECTOR_BITS;
427 }
428
429 bs->total_sectors = hint;
430 return 0;
431}
432
c3993cdc
SH
433/**
434 * Set open flags for a given cache mode
435 *
436 * Return 0 on success, -1 if the cache mode was invalid.
437 */
438int bdrv_parse_cache_flags(const char *mode, int *flags)
439{
440 *flags &= ~BDRV_O_CACHE_MASK;
441
442 if (!strcmp(mode, "off") || !strcmp(mode, "none")) {
443 *flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
92196b2f
SH
444 } else if (!strcmp(mode, "directsync")) {
445 *flags |= BDRV_O_NOCACHE;
c3993cdc
SH
446 } else if (!strcmp(mode, "writeback")) {
447 *flags |= BDRV_O_CACHE_WB;
448 } else if (!strcmp(mode, "unsafe")) {
449 *flags |= BDRV_O_CACHE_WB;
450 *flags |= BDRV_O_NO_FLUSH;
451 } else if (!strcmp(mode, "writethrough")) {
452 /* this is the default */
453 } else {
454 return -1;
455 }
456
457 return 0;
458}
459
57915332
KW
460/*
461 * Common part for opening disk images and files
462 */
463static int bdrv_open_common(BlockDriverState *bs, const char *filename,
464 int flags, BlockDriver *drv)
465{
466 int ret, open_flags;
467
468 assert(drv != NULL);
469
28dcee10
SH
470 trace_bdrv_open_common(bs, filename, flags, drv->format_name);
471
66f82cee 472 bs->file = NULL;
51762288 473 bs->total_sectors = 0;
57915332
KW
474 bs->encrypted = 0;
475 bs->valid_key = 0;
03f541bd 476 bs->sg = 0;
57915332 477 bs->open_flags = flags;
03f541bd 478 bs->growable = 0;
57915332
KW
479 bs->buffer_alignment = 512;
480
481 pstrcpy(bs->filename, sizeof(bs->filename), filename);
03f541bd 482 bs->backing_file[0] = '\0';
57915332
KW
483
484 if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {
485 return -ENOTSUP;
486 }
487
488 bs->drv = drv;
7267c094 489 bs->opaque = g_malloc0(drv->instance_size);
57915332 490
03f541bd 491 bs->enable_write_cache = !!(flags & BDRV_O_CACHE_WB);
57915332
KW
492
493 /*
494 * Clear flags that are internal to the block layer before opening the
495 * image.
496 */
497 open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
498
499 /*
ebabb67a 500 * Snapshots should be writable.
57915332
KW
501 */
502 if (bs->is_temporary) {
503 open_flags |= BDRV_O_RDWR;
504 }
505
e7c63796
SH
506 bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR);
507
66f82cee
KW
508 /* Open the image, either directly or using a protocol */
509 if (drv->bdrv_file_open) {
510 ret = drv->bdrv_file_open(bs, filename, open_flags);
511 } else {
512 ret = bdrv_file_open(&bs->file, filename, open_flags);
513 if (ret >= 0) {
514 ret = drv->bdrv_open(bs, open_flags);
515 }
516 }
517
57915332
KW
518 if (ret < 0) {
519 goto free_and_fail;
520 }
521
51762288
SH
522 ret = refresh_total_sectors(bs, bs->total_sectors);
523 if (ret < 0) {
524 goto free_and_fail;
57915332 525 }
51762288 526
57915332
KW
527#ifndef _WIN32
528 if (bs->is_temporary) {
529 unlink(filename);
530 }
531#endif
532 return 0;
533
534free_and_fail:
66f82cee
KW
535 if (bs->file) {
536 bdrv_delete(bs->file);
537 bs->file = NULL;
538 }
7267c094 539 g_free(bs->opaque);
57915332
KW
540 bs->opaque = NULL;
541 bs->drv = NULL;
542 return ret;
543}
544
b6ce07aa
KW
545/*
546 * Opens a file using a protocol (file, host_device, nbd, ...)
547 */
83f64091 548int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
ea2384d3 549{
83f64091 550 BlockDriverState *bs;
6db95603 551 BlockDriver *drv;
83f64091
FB
552 int ret;
553
b50cbabc 554 drv = bdrv_find_protocol(filename);
6db95603
CH
555 if (!drv) {
556 return -ENOENT;
557 }
558
83f64091 559 bs = bdrv_new("");
b6ce07aa 560 ret = bdrv_open_common(bs, filename, flags, drv);
83f64091
FB
561 if (ret < 0) {
562 bdrv_delete(bs);
563 return ret;
3b0d4f61 564 }
71d0770c 565 bs->growable = 1;
83f64091
FB
566 *pbs = bs;
567 return 0;
568}
569
b6ce07aa
KW
570/*
571 * Opens a disk image (raw, qcow2, vmdk, ...)
572 */
d6e9098e
KW
573int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
574 BlockDriver *drv)
ea2384d3 575{
b6ce07aa 576 int ret;
2b572816 577 char tmp_filename[PATH_MAX];
712e7874 578
83f64091 579 if (flags & BDRV_O_SNAPSHOT) {
ea2384d3
FB
580 BlockDriverState *bs1;
581 int64_t total_size;
7c96d46e 582 int is_protocol = 0;
91a073a9
KW
583 BlockDriver *bdrv_qcow2;
584 QEMUOptionParameter *options;
b6ce07aa 585 char backing_filename[PATH_MAX];
3b46e624 586
ea2384d3
FB
587 /* if snapshot, we create a temporary backing file and open it
588 instead of opening 'filename' directly */
33e3963e 589
ea2384d3
FB
590 /* if there is a backing file, use it */
591 bs1 = bdrv_new("");
d6e9098e 592 ret = bdrv_open(bs1, filename, 0, drv);
51d7c00c 593 if (ret < 0) {
ea2384d3 594 bdrv_delete(bs1);
51d7c00c 595 return ret;
ea2384d3 596 }
3e82990b 597 total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK;
7c96d46e
AL
598
599 if (bs1->drv && bs1->drv->protocol_name)
600 is_protocol = 1;
601
ea2384d3 602 bdrv_delete(bs1);
3b46e624 603
ea2384d3 604 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
7c96d46e
AL
605
606 /* Real path is meaningless for protocols */
607 if (is_protocol)
608 snprintf(backing_filename, sizeof(backing_filename),
609 "%s", filename);
114cdfa9
KS
610 else if (!realpath(filename, backing_filename))
611 return -errno;
7c96d46e 612
91a073a9
KW
613 bdrv_qcow2 = bdrv_find_format("qcow2");
614 options = parse_option_parameters("", bdrv_qcow2->create_options, NULL);
615
3e82990b 616 set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size);
91a073a9
KW
617 set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename);
618 if (drv) {
619 set_option_parameter(options, BLOCK_OPT_BACKING_FMT,
620 drv->format_name);
621 }
622
623 ret = bdrv_create(bdrv_qcow2, tmp_filename, options);
d748768c 624 free_option_parameters(options);
51d7c00c
AL
625 if (ret < 0) {
626 return ret;
ea2384d3 627 }
91a073a9 628
ea2384d3 629 filename = tmp_filename;
91a073a9 630 drv = bdrv_qcow2;
ea2384d3
FB
631 bs->is_temporary = 1;
632 }
712e7874 633
b6ce07aa 634 /* Find the right image format driver */
6db95603 635 if (!drv) {
c98ac35d 636 ret = find_image_format(filename, &drv);
51d7c00c 637 }
6987307c 638
51d7c00c 639 if (!drv) {
51d7c00c 640 goto unlink_and_fail;
ea2384d3 641 }
b6ce07aa
KW
642
643 /* Open the image */
644 ret = bdrv_open_common(bs, filename, flags, drv);
645 if (ret < 0) {
6987307c
CH
646 goto unlink_and_fail;
647 }
648
b6ce07aa
KW
649 /* If there is a backing file, use it */
650 if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') {
651 char backing_filename[PATH_MAX];
652 int back_flags;
653 BlockDriver *back_drv = NULL;
654
655 bs->backing_hd = bdrv_new("");
df2dbb4a
SH
656
657 if (path_has_protocol(bs->backing_file)) {
658 pstrcpy(backing_filename, sizeof(backing_filename),
659 bs->backing_file);
660 } else {
661 path_combine(backing_filename, sizeof(backing_filename),
662 filename, bs->backing_file);
663 }
664
665 if (bs->backing_format[0] != '\0') {
b6ce07aa 666 back_drv = bdrv_find_format(bs->backing_format);
df2dbb4a 667 }
b6ce07aa
KW
668
669 /* backing files always opened read-only */
670 back_flags =
671 flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
672
673 ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv);
674 if (ret < 0) {
675 bdrv_close(bs);
676 return ret;
677 }
678 if (bs->is_temporary) {
679 bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR);
680 } else {
681 /* base image inherits from "parent" */
682 bs->backing_hd->keep_read_only = bs->keep_read_only;
683 }
684 }
685
686 if (!bdrv_key_required(bs)) {
7d4b4ba5 687 bdrv_dev_change_media_cb(bs, true);
b6ce07aa
KW
688 }
689
690 return 0;
691
692unlink_and_fail:
693 if (bs->is_temporary) {
694 unlink(filename);
695 }
696 return ret;
697}
698
fc01f7e7
FB
699void bdrv_close(BlockDriverState *bs)
700{
19cb3738 701 if (bs->drv) {
f9092b10
MA
702 if (bs == bs_snapshots) {
703 bs_snapshots = NULL;
704 }
557df6ac 705 if (bs->backing_hd) {
ea2384d3 706 bdrv_delete(bs->backing_hd);
557df6ac
SH
707 bs->backing_hd = NULL;
708 }
ea2384d3 709 bs->drv->bdrv_close(bs);
7267c094 710 g_free(bs->opaque);
ea2384d3
FB
711#ifdef _WIN32
712 if (bs->is_temporary) {
713 unlink(bs->filename);
714 }
67b915a5 715#endif
ea2384d3
FB
716 bs->opaque = NULL;
717 bs->drv = NULL;
b338082b 718
66f82cee
KW
719 if (bs->file != NULL) {
720 bdrv_close(bs->file);
721 }
722
7d4b4ba5 723 bdrv_dev_change_media_cb(bs, false);
b338082b
FB
724 }
725}
726
2bc93fed
MK
727void bdrv_close_all(void)
728{
729 BlockDriverState *bs;
730
731 QTAILQ_FOREACH(bs, &bdrv_states, list) {
732 bdrv_close(bs);
733 }
734}
735
d22b2f41
RH
736/* make a BlockDriverState anonymous by removing from bdrv_state list.
737 Also, NULL terminate the device_name to prevent double remove */
738void bdrv_make_anon(BlockDriverState *bs)
739{
740 if (bs->device_name[0] != '\0') {
741 QTAILQ_REMOVE(&bdrv_states, bs, list);
742 }
743 bs->device_name[0] = '\0';
744}
745
b338082b
FB
746void bdrv_delete(BlockDriverState *bs)
747{
fa879d62 748 assert(!bs->dev);
18846dee 749
1b7bdbc1 750 /* remove from list, if necessary */
d22b2f41 751 bdrv_make_anon(bs);
34c6f050 752
b338082b 753 bdrv_close(bs);
66f82cee
KW
754 if (bs->file != NULL) {
755 bdrv_delete(bs->file);
756 }
757
f9092b10 758 assert(bs != bs_snapshots);
7267c094 759 g_free(bs);
fc01f7e7
FB
760}
761
fa879d62
MA
762int bdrv_attach_dev(BlockDriverState *bs, void *dev)
763/* TODO change to DeviceState *dev when all users are qdevified */
18846dee 764{
fa879d62 765 if (bs->dev) {
18846dee
MA
766 return -EBUSY;
767 }
fa879d62 768 bs->dev = dev;
28a7282a 769 bdrv_iostatus_reset(bs);
18846dee
MA
770 return 0;
771}
772
fa879d62
MA
773/* TODO qdevified devices don't use this, remove when devices are qdevified */
774void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev)
18846dee 775{
fa879d62
MA
776 if (bdrv_attach_dev(bs, dev) < 0) {
777 abort();
778 }
779}
780
781void bdrv_detach_dev(BlockDriverState *bs, void *dev)
782/* TODO change to DeviceState *dev when all users are qdevified */
783{
784 assert(bs->dev == dev);
785 bs->dev = NULL;
0e49de52
MA
786 bs->dev_ops = NULL;
787 bs->dev_opaque = NULL;
29e05f20 788 bs->buffer_alignment = 512;
18846dee
MA
789}
790
fa879d62
MA
791/* TODO change to return DeviceState * when all users are qdevified */
792void *bdrv_get_attached_dev(BlockDriverState *bs)
18846dee 793{
fa879d62 794 return bs->dev;
18846dee
MA
795}
796
0e49de52
MA
797void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops,
798 void *opaque)
799{
800 bs->dev_ops = ops;
801 bs->dev_opaque = opaque;
2c6942fa
MA
802 if (bdrv_dev_has_removable_media(bs) && bs == bs_snapshots) {
803 bs_snapshots = NULL;
804 }
0e49de52
MA
805}
806
7d4b4ba5 807static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load)
0e49de52 808{
145feb17 809 if (bs->dev_ops && bs->dev_ops->change_media_cb) {
7d4b4ba5 810 bs->dev_ops->change_media_cb(bs->dev_opaque, load);
145feb17
MA
811 }
812}
813
2c6942fa
MA
814bool bdrv_dev_has_removable_media(BlockDriverState *bs)
815{
816 return !bs->dev || (bs->dev_ops && bs->dev_ops->change_media_cb);
817}
818
025ccaa7
PB
819void bdrv_dev_eject_request(BlockDriverState *bs, bool force)
820{
821 if (bs->dev_ops && bs->dev_ops->eject_request_cb) {
822 bs->dev_ops->eject_request_cb(bs->dev_opaque, force);
823 }
824}
825
e4def80b
MA
826bool bdrv_dev_is_tray_open(BlockDriverState *bs)
827{
828 if (bs->dev_ops && bs->dev_ops->is_tray_open) {
829 return bs->dev_ops->is_tray_open(bs->dev_opaque);
830 }
831 return false;
832}
833
145feb17
MA
834static void bdrv_dev_resize_cb(BlockDriverState *bs)
835{
836 if (bs->dev_ops && bs->dev_ops->resize_cb) {
837 bs->dev_ops->resize_cb(bs->dev_opaque);
0e49de52
MA
838 }
839}
840
f107639a
MA
841bool bdrv_dev_is_medium_locked(BlockDriverState *bs)
842{
843 if (bs->dev_ops && bs->dev_ops->is_medium_locked) {
844 return bs->dev_ops->is_medium_locked(bs->dev_opaque);
845 }
846 return false;
847}
848
e97fc193
AL
849/*
850 * Run consistency checks on an image
851 *
e076f338 852 * Returns 0 if the check could be completed (it doesn't mean that the image is
a1c7273b 853 * free of errors) or -errno when an internal error occurred. The results of the
e076f338 854 * check are stored in res.
e97fc193 855 */
e076f338 856int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res)
e97fc193
AL
857{
858 if (bs->drv->bdrv_check == NULL) {
859 return -ENOTSUP;
860 }
861
e076f338 862 memset(res, 0, sizeof(*res));
9ac228e0 863 return bs->drv->bdrv_check(bs, res);
e97fc193
AL
864}
865
8a426614
KW
866#define COMMIT_BUF_SECTORS 2048
867
33e3963e
FB
868/* commit COW file into the raw image */
869int bdrv_commit(BlockDriverState *bs)
870{
19cb3738 871 BlockDriver *drv = bs->drv;
ee181196 872 BlockDriver *backing_drv;
8a426614
KW
873 int64_t sector, total_sectors;
874 int n, ro, open_flags;
4dca4b63 875 int ret = 0, rw_ret = 0;
8a426614 876 uint8_t *buf;
4dca4b63
NS
877 char filename[1024];
878 BlockDriverState *bs_rw, *bs_ro;
33e3963e 879
19cb3738
FB
880 if (!drv)
881 return -ENOMEDIUM;
4dca4b63
NS
882
883 if (!bs->backing_hd) {
884 return -ENOTSUP;
33e3963e
FB
885 }
886
4dca4b63
NS
887 if (bs->backing_hd->keep_read_only) {
888 return -EACCES;
889 }
ee181196
KW
890
891 backing_drv = bs->backing_hd->drv;
4dca4b63
NS
892 ro = bs->backing_hd->read_only;
893 strncpy(filename, bs->backing_hd->filename, sizeof(filename));
894 open_flags = bs->backing_hd->open_flags;
895
896 if (ro) {
897 /* re-open as RW */
898 bdrv_delete(bs->backing_hd);
899 bs->backing_hd = NULL;
900 bs_rw = bdrv_new("");
ee181196
KW
901 rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR,
902 backing_drv);
4dca4b63
NS
903 if (rw_ret < 0) {
904 bdrv_delete(bs_rw);
905 /* try to re-open read-only */
906 bs_ro = bdrv_new("");
ee181196
KW
907 ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR,
908 backing_drv);
4dca4b63
NS
909 if (ret < 0) {
910 bdrv_delete(bs_ro);
911 /* drive not functional anymore */
912 bs->drv = NULL;
913 return ret;
914 }
915 bs->backing_hd = bs_ro;
916 return rw_ret;
917 }
918 bs->backing_hd = bs_rw;
ea2384d3 919 }
33e3963e 920
6ea44308 921 total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
7267c094 922 buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE);
8a426614
KW
923
924 for (sector = 0; sector < total_sectors; sector += n) {
925 if (drv->bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) {
926
927 if (bdrv_read(bs, sector, buf, n) != 0) {
928 ret = -EIO;
929 goto ro_cleanup;
930 }
931
932 if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) {
933 ret = -EIO;
934 goto ro_cleanup;
935 }
ea2384d3 936 }
33e3963e 937 }
95389c86 938
1d44952f
CH
939 if (drv->bdrv_make_empty) {
940 ret = drv->bdrv_make_empty(bs);
941 bdrv_flush(bs);
942 }
95389c86 943
3f5075ae
CH
944 /*
945 * Make sure all data we wrote to the backing device is actually
946 * stable on disk.
947 */
948 if (bs->backing_hd)
949 bdrv_flush(bs->backing_hd);
4dca4b63
NS
950
951ro_cleanup:
7267c094 952 g_free(buf);
4dca4b63
NS
953
954 if (ro) {
955 /* re-open as RO */
956 bdrv_delete(bs->backing_hd);
957 bs->backing_hd = NULL;
958 bs_ro = bdrv_new("");
ee181196
KW
959 ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR,
960 backing_drv);
4dca4b63
NS
961 if (ret < 0) {
962 bdrv_delete(bs_ro);
963 /* drive not functional anymore */
964 bs->drv = NULL;
965 return ret;
966 }
967 bs->backing_hd = bs_ro;
968 bs->backing_hd->keep_read_only = 0;
969 }
970
1d44952f 971 return ret;
33e3963e
FB
972}
973
6ab4b5ab
MA
974void bdrv_commit_all(void)
975{
976 BlockDriverState *bs;
977
978 QTAILQ_FOREACH(bs, &bdrv_states, list) {
979 bdrv_commit(bs);
980 }
981}
982
756e6736
KW
983/*
984 * Return values:
985 * 0 - success
986 * -EINVAL - backing format specified, but no file
987 * -ENOSPC - can't update the backing file because no space is left in the
988 * image file header
989 * -ENOTSUP - format driver doesn't support changing the backing file
990 */
991int bdrv_change_backing_file(BlockDriverState *bs,
992 const char *backing_file, const char *backing_fmt)
993{
994 BlockDriver *drv = bs->drv;
995
996 if (drv->bdrv_change_backing_file != NULL) {
997 return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
998 } else {
999 return -ENOTSUP;
1000 }
1001}
1002
71d0770c
AL
1003static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
1004 size_t size)
1005{
1006 int64_t len;
1007
1008 if (!bdrv_is_inserted(bs))
1009 return -ENOMEDIUM;
1010
1011 if (bs->growable)
1012 return 0;
1013
1014 len = bdrv_getlength(bs);
1015
fbb7b4e0
KW
1016 if (offset < 0)
1017 return -EIO;
1018
1019 if ((offset > len) || (len - offset < size))
71d0770c
AL
1020 return -EIO;
1021
1022 return 0;
1023}
1024
1025static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
1026 int nb_sectors)
1027{
eb5a3165
JS
1028 return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE,
1029 nb_sectors * BDRV_SECTOR_SIZE);
71d0770c
AL
1030}
1031
1c9805a3
SH
1032typedef struct RwCo {
1033 BlockDriverState *bs;
1034 int64_t sector_num;
1035 int nb_sectors;
1036 QEMUIOVector *qiov;
1037 bool is_write;
1038 int ret;
1039} RwCo;
1040
1041static void coroutine_fn bdrv_rw_co_entry(void *opaque)
fc01f7e7 1042{
1c9805a3 1043 RwCo *rwco = opaque;
ea2384d3 1044
1c9805a3
SH
1045 if (!rwco->is_write) {
1046 rwco->ret = bdrv_co_do_readv(rwco->bs, rwco->sector_num,
1047 rwco->nb_sectors, rwco->qiov);
1048 } else {
1049 rwco->ret = bdrv_co_do_writev(rwco->bs, rwco->sector_num,
1050 rwco->nb_sectors, rwco->qiov);
1051 }
1052}
e7a8a783 1053
1c9805a3
SH
1054/*
1055 * Process a synchronous request using coroutines
1056 */
1057static int bdrv_rw_co(BlockDriverState *bs, int64_t sector_num, uint8_t *buf,
1058 int nb_sectors, bool is_write)
1059{
1060 QEMUIOVector qiov;
1061 struct iovec iov = {
1062 .iov_base = (void *)buf,
1063 .iov_len = nb_sectors * BDRV_SECTOR_SIZE,
1064 };
1065 Coroutine *co;
1066 RwCo rwco = {
1067 .bs = bs,
1068 .sector_num = sector_num,
1069 .nb_sectors = nb_sectors,
1070 .qiov = &qiov,
1071 .is_write = is_write,
1072 .ret = NOT_DONE,
1073 };
e7a8a783 1074
1c9805a3 1075 qemu_iovec_init_external(&qiov, &iov, 1);
e7a8a783 1076
1c9805a3
SH
1077 if (qemu_in_coroutine()) {
1078 /* Fast-path if already in coroutine context */
1079 bdrv_rw_co_entry(&rwco);
1080 } else {
1081 co = qemu_coroutine_create(bdrv_rw_co_entry);
1082 qemu_coroutine_enter(co, &rwco);
1083 while (rwco.ret == NOT_DONE) {
1084 qemu_aio_wait();
1085 }
1086 }
1087 return rwco.ret;
1088}
b338082b 1089
1c9805a3
SH
1090/* return < 0 if error. See bdrv_write() for the return codes */
1091int bdrv_read(BlockDriverState *bs, int64_t sector_num,
1092 uint8_t *buf, int nb_sectors)
1093{
1094 return bdrv_rw_co(bs, sector_num, buf, nb_sectors, false);
fc01f7e7
FB
1095}
1096
7cd1e32a 1097static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num,
a55eb92c 1098 int nb_sectors, int dirty)
7cd1e32a 1099{
1100 int64_t start, end;
c6d22830 1101 unsigned long val, idx, bit;
a55eb92c 1102
6ea44308 1103 start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
c6d22830 1104 end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
a55eb92c
JK
1105
1106 for (; start <= end; start++) {
c6d22830
JK
1107 idx = start / (sizeof(unsigned long) * 8);
1108 bit = start % (sizeof(unsigned long) * 8);
1109 val = bs->dirty_bitmap[idx];
1110 if (dirty) {
6d59fec1 1111 if (!(val & (1UL << bit))) {
aaa0eb75 1112 bs->dirty_count++;
6d59fec1 1113 val |= 1UL << bit;
aaa0eb75 1114 }
c6d22830 1115 } else {
6d59fec1 1116 if (val & (1UL << bit)) {
aaa0eb75 1117 bs->dirty_count--;
6d59fec1 1118 val &= ~(1UL << bit);
aaa0eb75 1119 }
c6d22830
JK
1120 }
1121 bs->dirty_bitmap[idx] = val;
7cd1e32a 1122 }
1123}
1124
5fafdf24 1125/* Return < 0 if error. Important errors are:
19cb3738
FB
1126 -EIO generic I/O error (may happen for all errors)
1127 -ENOMEDIUM No media inserted.
1128 -EINVAL Invalid sector number or nb_sectors
1129 -EACCES Trying to write a read-only device
1130*/
5fafdf24 1131int bdrv_write(BlockDriverState *bs, int64_t sector_num,
fc01f7e7
FB
1132 const uint8_t *buf, int nb_sectors)
1133{
1c9805a3 1134 return bdrv_rw_co(bs, sector_num, (uint8_t *)buf, nb_sectors, true);
83f64091
FB
1135}
1136
eda578e5
AL
1137int bdrv_pread(BlockDriverState *bs, int64_t offset,
1138 void *buf, int count1)
83f64091 1139{
6ea44308 1140 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
83f64091
FB
1141 int len, nb_sectors, count;
1142 int64_t sector_num;
9a8c4cce 1143 int ret;
83f64091
FB
1144
1145 count = count1;
1146 /* first read to align to sector start */
6ea44308 1147 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
83f64091
FB
1148 if (len > count)
1149 len = count;
6ea44308 1150 sector_num = offset >> BDRV_SECTOR_BITS;
83f64091 1151 if (len > 0) {
9a8c4cce
KW
1152 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1153 return ret;
6ea44308 1154 memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
83f64091
FB
1155 count -= len;
1156 if (count == 0)
1157 return count1;
1158 sector_num++;
1159 buf += len;
1160 }
1161
1162 /* read the sectors "in place" */
6ea44308 1163 nb_sectors = count >> BDRV_SECTOR_BITS;
83f64091 1164 if (nb_sectors > 0) {
9a8c4cce
KW
1165 if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0)
1166 return ret;
83f64091 1167 sector_num += nb_sectors;
6ea44308 1168 len = nb_sectors << BDRV_SECTOR_BITS;
83f64091
FB
1169 buf += len;
1170 count -= len;
1171 }
1172
1173 /* add data from the last sector */
1174 if (count > 0) {
9a8c4cce
KW
1175 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1176 return ret;
83f64091
FB
1177 memcpy(buf, tmp_buf, count);
1178 }
1179 return count1;
1180}
1181
eda578e5
AL
1182int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
1183 const void *buf, int count1)
83f64091 1184{
6ea44308 1185 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
83f64091
FB
1186 int len, nb_sectors, count;
1187 int64_t sector_num;
9a8c4cce 1188 int ret;
83f64091
FB
1189
1190 count = count1;
1191 /* first write to align to sector start */
6ea44308 1192 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
83f64091
FB
1193 if (len > count)
1194 len = count;
6ea44308 1195 sector_num = offset >> BDRV_SECTOR_BITS;
83f64091 1196 if (len > 0) {
9a8c4cce
KW
1197 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1198 return ret;
6ea44308 1199 memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len);
9a8c4cce
KW
1200 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
1201 return ret;
83f64091
FB
1202 count -= len;
1203 if (count == 0)
1204 return count1;
1205 sector_num++;
1206 buf += len;
1207 }
1208
1209 /* write the sectors "in place" */
6ea44308 1210 nb_sectors = count >> BDRV_SECTOR_BITS;
83f64091 1211 if (nb_sectors > 0) {
9a8c4cce
KW
1212 if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0)
1213 return ret;
83f64091 1214 sector_num += nb_sectors;
6ea44308 1215 len = nb_sectors << BDRV_SECTOR_BITS;
83f64091
FB
1216 buf += len;
1217 count -= len;
1218 }
1219
1220 /* add data from the last sector */
1221 if (count > 0) {
9a8c4cce
KW
1222 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1223 return ret;
83f64091 1224 memcpy(tmp_buf, buf, count);
9a8c4cce
KW
1225 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
1226 return ret;
83f64091
FB
1227 }
1228 return count1;
1229}
83f64091 1230
f08145fe
KW
1231/*
1232 * Writes to the file and ensures that no writes are reordered across this
1233 * request (acts as a barrier)
1234 *
1235 * Returns 0 on success, -errno in error cases.
1236 */
1237int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset,
1238 const void *buf, int count)
1239{
1240 int ret;
1241
1242 ret = bdrv_pwrite(bs, offset, buf, count);
1243 if (ret < 0) {
1244 return ret;
1245 }
1246
92196b2f
SH
1247 /* No flush needed for cache modes that use O_DSYNC */
1248 if ((bs->open_flags & BDRV_O_CACHE_WB) != 0) {
f08145fe
KW
1249 bdrv_flush(bs);
1250 }
1251
1252 return 0;
1253}
1254
c5fbe571
SH
1255/*
1256 * Handle a read request in coroutine context
1257 */
1258static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs,
1259 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
da1fa91d
KW
1260{
1261 BlockDriver *drv = bs->drv;
1262
da1fa91d
KW
1263 if (!drv) {
1264 return -ENOMEDIUM;
1265 }
1266 if (bdrv_check_request(bs, sector_num, nb_sectors)) {
1267 return -EIO;
1268 }
1269
1270 return drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov);
1271}
1272
c5fbe571 1273int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num,
da1fa91d
KW
1274 int nb_sectors, QEMUIOVector *qiov)
1275{
c5fbe571 1276 trace_bdrv_co_readv(bs, sector_num, nb_sectors);
da1fa91d 1277
c5fbe571
SH
1278 return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov);
1279}
1280
1281/*
1282 * Handle a write request in coroutine context
1283 */
1284static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs,
1285 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
1286{
1287 BlockDriver *drv = bs->drv;
6b7cb247 1288 int ret;
da1fa91d
KW
1289
1290 if (!bs->drv) {
1291 return -ENOMEDIUM;
1292 }
1293 if (bs->read_only) {
1294 return -EACCES;
1295 }
1296 if (bdrv_check_request(bs, sector_num, nb_sectors)) {
1297 return -EIO;
1298 }
1299
6b7cb247
SH
1300 ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov);
1301
da1fa91d
KW
1302 if (bs->dirty_bitmap) {
1303 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1304 }
1305
1306 if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
1307 bs->wr_highest_sector = sector_num + nb_sectors - 1;
1308 }
1309
6b7cb247 1310 return ret;
da1fa91d
KW
1311}
1312
c5fbe571
SH
1313int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num,
1314 int nb_sectors, QEMUIOVector *qiov)
1315{
1316 trace_bdrv_co_writev(bs, sector_num, nb_sectors);
1317
1318 return bdrv_co_do_writev(bs, sector_num, nb_sectors, qiov);
1319}
1320
83f64091
FB
1321/**
1322 * Truncate file to 'offset' bytes (needed only for file protocols)
1323 */
1324int bdrv_truncate(BlockDriverState *bs, int64_t offset)
1325{
1326 BlockDriver *drv = bs->drv;
51762288 1327 int ret;
83f64091 1328 if (!drv)
19cb3738 1329 return -ENOMEDIUM;
83f64091
FB
1330 if (!drv->bdrv_truncate)
1331 return -ENOTSUP;
59f2689d
NS
1332 if (bs->read_only)
1333 return -EACCES;
8591675f
MT
1334 if (bdrv_in_use(bs))
1335 return -EBUSY;
51762288
SH
1336 ret = drv->bdrv_truncate(bs, offset);
1337 if (ret == 0) {
1338 ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
145feb17 1339 bdrv_dev_resize_cb(bs);
51762288
SH
1340 }
1341 return ret;
83f64091
FB
1342}
1343
4a1d5e1f
FZ
1344/**
1345 * Length of a allocated file in bytes. Sparse files are counted by actual
1346 * allocated space. Return < 0 if error or unknown.
1347 */
1348int64_t bdrv_get_allocated_file_size(BlockDriverState *bs)
1349{
1350 BlockDriver *drv = bs->drv;
1351 if (!drv) {
1352 return -ENOMEDIUM;
1353 }
1354 if (drv->bdrv_get_allocated_file_size) {
1355 return drv->bdrv_get_allocated_file_size(bs);
1356 }
1357 if (bs->file) {
1358 return bdrv_get_allocated_file_size(bs->file);
1359 }
1360 return -ENOTSUP;
1361}
1362
83f64091
FB
1363/**
1364 * Length of a file in bytes. Return < 0 if error or unknown.
1365 */
1366int64_t bdrv_getlength(BlockDriverState *bs)
1367{
1368 BlockDriver *drv = bs->drv;
1369 if (!drv)
19cb3738 1370 return -ENOMEDIUM;
51762288 1371
2c6942fa 1372 if (bs->growable || bdrv_dev_has_removable_media(bs)) {
46a4e4e6
SH
1373 if (drv->bdrv_getlength) {
1374 return drv->bdrv_getlength(bs);
1375 }
83f64091 1376 }
46a4e4e6 1377 return bs->total_sectors * BDRV_SECTOR_SIZE;
fc01f7e7
FB
1378}
1379
19cb3738 1380/* return 0 as number of sectors if no device present or error */
96b8f136 1381void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
fc01f7e7 1382{
19cb3738
FB
1383 int64_t length;
1384 length = bdrv_getlength(bs);
1385 if (length < 0)
1386 length = 0;
1387 else
6ea44308 1388 length = length >> BDRV_SECTOR_BITS;
19cb3738 1389 *nb_sectors_ptr = length;
fc01f7e7 1390}
cf98951b 1391
f3d54fc4
AL
1392struct partition {
1393 uint8_t boot_ind; /* 0x80 - active */
1394 uint8_t head; /* starting head */
1395 uint8_t sector; /* starting sector */
1396 uint8_t cyl; /* starting cylinder */
1397 uint8_t sys_ind; /* What partition type */
1398 uint8_t end_head; /* end head */
1399 uint8_t end_sector; /* end sector */
1400 uint8_t end_cyl; /* end cylinder */
1401 uint32_t start_sect; /* starting sector counting from 0 */
1402 uint32_t nr_sects; /* nr of sectors in partition */
541dc0d4 1403} QEMU_PACKED;
f3d54fc4
AL
1404
1405/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
1406static int guess_disk_lchs(BlockDriverState *bs,
1407 int *pcylinders, int *pheads, int *psectors)
1408{
eb5a3165 1409 uint8_t buf[BDRV_SECTOR_SIZE];
f3d54fc4
AL
1410 int ret, i, heads, sectors, cylinders;
1411 struct partition *p;
1412 uint32_t nr_sects;
a38131b6 1413 uint64_t nb_sectors;
f3d54fc4
AL
1414
1415 bdrv_get_geometry(bs, &nb_sectors);
1416
1417 ret = bdrv_read(bs, 0, buf, 1);
1418 if (ret < 0)
1419 return -1;
1420 /* test msdos magic */
1421 if (buf[510] != 0x55 || buf[511] != 0xaa)
1422 return -1;
1423 for(i = 0; i < 4; i++) {
1424 p = ((struct partition *)(buf + 0x1be)) + i;
1425 nr_sects = le32_to_cpu(p->nr_sects);
1426 if (nr_sects && p->end_head) {
1427 /* We make the assumption that the partition terminates on
1428 a cylinder boundary */
1429 heads = p->end_head + 1;
1430 sectors = p->end_sector & 63;
1431 if (sectors == 0)
1432 continue;
1433 cylinders = nb_sectors / (heads * sectors);
1434 if (cylinders < 1 || cylinders > 16383)
1435 continue;
1436 *pheads = heads;
1437 *psectors = sectors;
1438 *pcylinders = cylinders;
1439#if 0
1440 printf("guessed geometry: LCHS=%d %d %d\n",
1441 cylinders, heads, sectors);
1442#endif
1443 return 0;
1444 }
1445 }
1446 return -1;
1447}
1448
1449void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
1450{
1451 int translation, lba_detected = 0;
1452 int cylinders, heads, secs;
a38131b6 1453 uint64_t nb_sectors;
f3d54fc4
AL
1454
1455 /* if a geometry hint is available, use it */
1456 bdrv_get_geometry(bs, &nb_sectors);
1457 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
1458 translation = bdrv_get_translation_hint(bs);
1459 if (cylinders != 0) {
1460 *pcyls = cylinders;
1461 *pheads = heads;
1462 *psecs = secs;
1463 } else {
1464 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
1465 if (heads > 16) {
1466 /* if heads > 16, it means that a BIOS LBA
1467 translation was active, so the default
1468 hardware geometry is OK */
1469 lba_detected = 1;
1470 goto default_geometry;
1471 } else {
1472 *pcyls = cylinders;
1473 *pheads = heads;
1474 *psecs = secs;
1475 /* disable any translation to be in sync with
1476 the logical geometry */
1477 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
1478 bdrv_set_translation_hint(bs,
1479 BIOS_ATA_TRANSLATION_NONE);
1480 }
1481 }
1482 } else {
1483 default_geometry:
1484 /* if no geometry, use a standard physical disk geometry */
1485 cylinders = nb_sectors / (16 * 63);
1486
1487 if (cylinders > 16383)
1488 cylinders = 16383;
1489 else if (cylinders < 2)
1490 cylinders = 2;
1491 *pcyls = cylinders;
1492 *pheads = 16;
1493 *psecs = 63;
1494 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
1495 if ((*pcyls * *pheads) <= 131072) {
1496 bdrv_set_translation_hint(bs,
1497 BIOS_ATA_TRANSLATION_LARGE);
1498 } else {
1499 bdrv_set_translation_hint(bs,
1500 BIOS_ATA_TRANSLATION_LBA);
1501 }
1502 }
1503 }
1504 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
1505 }
1506}
1507
5fafdf24 1508void bdrv_set_geometry_hint(BlockDriverState *bs,
b338082b
FB
1509 int cyls, int heads, int secs)
1510{
1511 bs->cyls = cyls;
1512 bs->heads = heads;
1513 bs->secs = secs;
1514}
1515
46d4767d
FB
1516void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
1517{
1518 bs->translation = translation;
1519}
1520
5fafdf24 1521void bdrv_get_geometry_hint(BlockDriverState *bs,
b338082b
FB
1522 int *pcyls, int *pheads, int *psecs)
1523{
1524 *pcyls = bs->cyls;
1525 *pheads = bs->heads;
1526 *psecs = bs->secs;
1527}
1528
5bbdbb46
BS
1529/* Recognize floppy formats */
1530typedef struct FDFormat {
1531 FDriveType drive;
1532 uint8_t last_sect;
1533 uint8_t max_track;
1534 uint8_t max_head;
1535} FDFormat;
1536
1537static const FDFormat fd_formats[] = {
1538 /* First entry is default format */
1539 /* 1.44 MB 3"1/2 floppy disks */
1540 { FDRIVE_DRV_144, 18, 80, 1, },
1541 { FDRIVE_DRV_144, 20, 80, 1, },
1542 { FDRIVE_DRV_144, 21, 80, 1, },
1543 { FDRIVE_DRV_144, 21, 82, 1, },
1544 { FDRIVE_DRV_144, 21, 83, 1, },
1545 { FDRIVE_DRV_144, 22, 80, 1, },
1546 { FDRIVE_DRV_144, 23, 80, 1, },
1547 { FDRIVE_DRV_144, 24, 80, 1, },
1548 /* 2.88 MB 3"1/2 floppy disks */
1549 { FDRIVE_DRV_288, 36, 80, 1, },
1550 { FDRIVE_DRV_288, 39, 80, 1, },
1551 { FDRIVE_DRV_288, 40, 80, 1, },
1552 { FDRIVE_DRV_288, 44, 80, 1, },
1553 { FDRIVE_DRV_288, 48, 80, 1, },
1554 /* 720 kB 3"1/2 floppy disks */
1555 { FDRIVE_DRV_144, 9, 80, 1, },
1556 { FDRIVE_DRV_144, 10, 80, 1, },
1557 { FDRIVE_DRV_144, 10, 82, 1, },
1558 { FDRIVE_DRV_144, 10, 83, 1, },
1559 { FDRIVE_DRV_144, 13, 80, 1, },
1560 { FDRIVE_DRV_144, 14, 80, 1, },
1561 /* 1.2 MB 5"1/4 floppy disks */
1562 { FDRIVE_DRV_120, 15, 80, 1, },
1563 { FDRIVE_DRV_120, 18, 80, 1, },
1564 { FDRIVE_DRV_120, 18, 82, 1, },
1565 { FDRIVE_DRV_120, 18, 83, 1, },
1566 { FDRIVE_DRV_120, 20, 80, 1, },
1567 /* 720 kB 5"1/4 floppy disks */
1568 { FDRIVE_DRV_120, 9, 80, 1, },
1569 { FDRIVE_DRV_120, 11, 80, 1, },
1570 /* 360 kB 5"1/4 floppy disks */
1571 { FDRIVE_DRV_120, 9, 40, 1, },
1572 { FDRIVE_DRV_120, 9, 40, 0, },
1573 { FDRIVE_DRV_120, 10, 41, 1, },
1574 { FDRIVE_DRV_120, 10, 42, 1, },
1575 /* 320 kB 5"1/4 floppy disks */
1576 { FDRIVE_DRV_120, 8, 40, 1, },
1577 { FDRIVE_DRV_120, 8, 40, 0, },
1578 /* 360 kB must match 5"1/4 better than 3"1/2... */
1579 { FDRIVE_DRV_144, 9, 80, 0, },
1580 /* end */
1581 { FDRIVE_DRV_NONE, -1, -1, 0, },
1582};
1583
1584void bdrv_get_floppy_geometry_hint(BlockDriverState *bs, int *nb_heads,
1585 int *max_track, int *last_sect,
1586 FDriveType drive_in, FDriveType *drive)
1587{
1588 const FDFormat *parse;
1589 uint64_t nb_sectors, size;
1590 int i, first_match, match;
1591
1592 bdrv_get_geometry_hint(bs, nb_heads, max_track, last_sect);
1593 if (*nb_heads != 0 && *max_track != 0 && *last_sect != 0) {
1594 /* User defined disk */
1595 } else {
1596 bdrv_get_geometry(bs, &nb_sectors);
1597 match = -1;
1598 first_match = -1;
1599 for (i = 0; ; i++) {
1600 parse = &fd_formats[i];
1601 if (parse->drive == FDRIVE_DRV_NONE) {
1602 break;
1603 }
1604 if (drive_in == parse->drive ||
1605 drive_in == FDRIVE_DRV_NONE) {
1606 size = (parse->max_head + 1) * parse->max_track *
1607 parse->last_sect;
1608 if (nb_sectors == size) {
1609 match = i;
1610 break;
1611 }
1612 if (first_match == -1) {
1613 first_match = i;
1614 }
1615 }
1616 }
1617 if (match == -1) {
1618 if (first_match == -1) {
1619 match = 1;
1620 } else {
1621 match = first_match;
1622 }
1623 parse = &fd_formats[match];
1624 }
1625 *nb_heads = parse->max_head + 1;
1626 *max_track = parse->max_track;
1627 *last_sect = parse->last_sect;
1628 *drive = parse->drive;
1629 }
1630}
1631
46d4767d
FB
1632int bdrv_get_translation_hint(BlockDriverState *bs)
1633{
1634 return bs->translation;
1635}
1636
abd7f68d
MA
1637void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error,
1638 BlockErrorAction on_write_error)
1639{
1640 bs->on_read_error = on_read_error;
1641 bs->on_write_error = on_write_error;
1642}
1643
1644BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read)
1645{
1646 return is_read ? bs->on_read_error : bs->on_write_error;
1647}
1648
b338082b
FB
1649int bdrv_is_read_only(BlockDriverState *bs)
1650{
1651 return bs->read_only;
1652}
1653
985a03b0
TS
1654int bdrv_is_sg(BlockDriverState *bs)
1655{
1656 return bs->sg;
1657}
1658
e900a7b7
CH
1659int bdrv_enable_write_cache(BlockDriverState *bs)
1660{
1661 return bs->enable_write_cache;
1662}
1663
ea2384d3
FB
1664int bdrv_is_encrypted(BlockDriverState *bs)
1665{
1666 if (bs->backing_hd && bs->backing_hd->encrypted)
1667 return 1;
1668 return bs->encrypted;
1669}
1670
c0f4ce77
AL
1671int bdrv_key_required(BlockDriverState *bs)
1672{
1673 BlockDriverState *backing_hd = bs->backing_hd;
1674
1675 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
1676 return 1;
1677 return (bs->encrypted && !bs->valid_key);
1678}
1679
ea2384d3
FB
1680int bdrv_set_key(BlockDriverState *bs, const char *key)
1681{
1682 int ret;
1683 if (bs->backing_hd && bs->backing_hd->encrypted) {
1684 ret = bdrv_set_key(bs->backing_hd, key);
1685 if (ret < 0)
1686 return ret;
1687 if (!bs->encrypted)
1688 return 0;
1689 }
fd04a2ae
SH
1690 if (!bs->encrypted) {
1691 return -EINVAL;
1692 } else if (!bs->drv || !bs->drv->bdrv_set_key) {
1693 return -ENOMEDIUM;
1694 }
c0f4ce77 1695 ret = bs->drv->bdrv_set_key(bs, key);
bb5fc20f
AL
1696 if (ret < 0) {
1697 bs->valid_key = 0;
1698 } else if (!bs->valid_key) {
1699 bs->valid_key = 1;
1700 /* call the change callback now, we skipped it on open */
7d4b4ba5 1701 bdrv_dev_change_media_cb(bs, true);
bb5fc20f 1702 }
c0f4ce77 1703 return ret;
ea2384d3
FB
1704}
1705
1706void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
1707{
19cb3738 1708 if (!bs->drv) {
ea2384d3
FB
1709 buf[0] = '\0';
1710 } else {
1711 pstrcpy(buf, buf_size, bs->drv->format_name);
1712 }
1713}
1714
5fafdf24 1715void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
ea2384d3
FB
1716 void *opaque)
1717{
1718 BlockDriver *drv;
1719
8a22f02a 1720 QLIST_FOREACH(drv, &bdrv_drivers, list) {
ea2384d3
FB
1721 it(opaque, drv->format_name);
1722 }
1723}
1724
b338082b
FB
1725BlockDriverState *bdrv_find(const char *name)
1726{
1727 BlockDriverState *bs;
1728
1b7bdbc1
SH
1729 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1730 if (!strcmp(name, bs->device_name)) {
b338082b 1731 return bs;
1b7bdbc1 1732 }
b338082b
FB
1733 }
1734 return NULL;
1735}
1736
2f399b0a
MA
1737BlockDriverState *bdrv_next(BlockDriverState *bs)
1738{
1739 if (!bs) {
1740 return QTAILQ_FIRST(&bdrv_states);
1741 }
1742 return QTAILQ_NEXT(bs, list);
1743}
1744
51de9760 1745void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
81d0912d
FB
1746{
1747 BlockDriverState *bs;
1748
1b7bdbc1 1749 QTAILQ_FOREACH(bs, &bdrv_states, list) {
51de9760 1750 it(opaque, bs);
81d0912d
FB
1751 }
1752}
1753
ea2384d3
FB
1754const char *bdrv_get_device_name(BlockDriverState *bs)
1755{
1756 return bs->device_name;
1757}
1758
c6ca28d6
AL
1759void bdrv_flush_all(void)
1760{
1761 BlockDriverState *bs;
1762
1b7bdbc1 1763 QTAILQ_FOREACH(bs, &bdrv_states, list) {
c602a489 1764 if (!bdrv_is_read_only(bs) && bdrv_is_inserted(bs)) {
c6ca28d6 1765 bdrv_flush(bs);
1b7bdbc1
SH
1766 }
1767 }
c6ca28d6
AL
1768}
1769
f2feebbd
KW
1770int bdrv_has_zero_init(BlockDriverState *bs)
1771{
1772 assert(bs->drv);
1773
336c1c12
KW
1774 if (bs->drv->bdrv_has_zero_init) {
1775 return bs->drv->bdrv_has_zero_init(bs);
f2feebbd
KW
1776 }
1777
1778 return 1;
1779}
1780
f58c7b35
TS
1781/*
1782 * Returns true iff the specified sector is present in the disk image. Drivers
1783 * not implementing the functionality are assumed to not support backing files,
1784 * hence all their sectors are reported as allocated.
1785 *
1786 * 'pnum' is set to the number of sectors (including and immediately following
1787 * the specified sector) that are known to be in the same
1788 * allocated/unallocated state.
1789 *
1790 * 'nb_sectors' is the max value 'pnum' should be set to.
1791 */
1792int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1793 int *pnum)
1794{
1795 int64_t n;
1796 if (!bs->drv->bdrv_is_allocated) {
1797 if (sector_num >= bs->total_sectors) {
1798 *pnum = 0;
1799 return 0;
1800 }
1801 n = bs->total_sectors - sector_num;
1802 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1803 return 1;
1804 }
1805 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1806}
1807
2582bfed
LC
1808void bdrv_mon_event(const BlockDriverState *bdrv,
1809 BlockMonEventAction action, int is_read)
1810{
1811 QObject *data;
1812 const char *action_str;
1813
1814 switch (action) {
1815 case BDRV_ACTION_REPORT:
1816 action_str = "report";
1817 break;
1818 case BDRV_ACTION_IGNORE:
1819 action_str = "ignore";
1820 break;
1821 case BDRV_ACTION_STOP:
1822 action_str = "stop";
1823 break;
1824 default:
1825 abort();
1826 }
1827
1828 data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
1829 bdrv->device_name,
1830 action_str,
1831 is_read ? "read" : "write");
1832 monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data);
1833
1834 qobject_decref(data);
1835}
1836
b2023818 1837BlockInfoList *qmp_query_block(Error **errp)
b338082b 1838{
b2023818 1839 BlockInfoList *head = NULL, *cur_item = NULL;
b338082b
FB
1840 BlockDriverState *bs;
1841
1b7bdbc1 1842 QTAILQ_FOREACH(bs, &bdrv_states, list) {
b2023818 1843 BlockInfoList *info = g_malloc0(sizeof(*info));
d15e5465 1844
b2023818
LC
1845 info->value = g_malloc0(sizeof(*info->value));
1846 info->value->device = g_strdup(bs->device_name);
1847 info->value->type = g_strdup("unknown");
1848 info->value->locked = bdrv_dev_is_medium_locked(bs);
1849 info->value->removable = bdrv_dev_has_removable_media(bs);
d15e5465 1850
e4def80b 1851 if (bdrv_dev_has_removable_media(bs)) {
b2023818
LC
1852 info->value->has_tray_open = true;
1853 info->value->tray_open = bdrv_dev_is_tray_open(bs);
e4def80b 1854 }
f04ef601
LC
1855
1856 if (bdrv_iostatus_is_enabled(bs)) {
b2023818
LC
1857 info->value->has_io_status = true;
1858 info->value->io_status = bs->iostatus;
f04ef601
LC
1859 }
1860
19cb3738 1861 if (bs->drv) {
b2023818
LC
1862 info->value->has_inserted = true;
1863 info->value->inserted = g_malloc0(sizeof(*info->value->inserted));
1864 info->value->inserted->file = g_strdup(bs->filename);
1865 info->value->inserted->ro = bs->read_only;
1866 info->value->inserted->drv = g_strdup(bs->drv->format_name);
1867 info->value->inserted->encrypted = bs->encrypted;
1868 if (bs->backing_file[0]) {
1869 info->value->inserted->has_backing_file = true;
1870 info->value->inserted->backing_file = g_strdup(bs->backing_file);
376253ec 1871 }
b2023818 1872 }
d15e5465 1873
b2023818
LC
1874 /* XXX: waiting for the qapi to support GSList */
1875 if (!cur_item) {
1876 head = cur_item = info;
1877 } else {
1878 cur_item->next = info;
1879 cur_item = info;
b338082b 1880 }
b338082b 1881 }
d15e5465 1882
b2023818 1883 return head;
b338082b 1884}
a36e69dd 1885
f11f57e4
LC
1886/* Consider exposing this as a full fledged QMP command */
1887static BlockStats *qmp_query_blockstat(const BlockDriverState *bs, Error **errp)
1888{
1889 BlockStats *s;
1890
1891 s = g_malloc0(sizeof(*s));
1892
1893 if (bs->device_name[0]) {
1894 s->has_device = true;
1895 s->device = g_strdup(bs->device_name);
294cc35f
KW
1896 }
1897
f11f57e4
LC
1898 s->stats = g_malloc0(sizeof(*s->stats));
1899 s->stats->rd_bytes = bs->nr_bytes[BDRV_ACCT_READ];
1900 s->stats->wr_bytes = bs->nr_bytes[BDRV_ACCT_WRITE];
1901 s->stats->rd_operations = bs->nr_ops[BDRV_ACCT_READ];
1902 s->stats->wr_operations = bs->nr_ops[BDRV_ACCT_WRITE];
1903 s->stats->wr_highest_offset = bs->wr_highest_sector * BDRV_SECTOR_SIZE;
1904 s->stats->flush_operations = bs->nr_ops[BDRV_ACCT_FLUSH];
1905 s->stats->wr_total_time_ns = bs->total_time_ns[BDRV_ACCT_WRITE];
1906 s->stats->rd_total_time_ns = bs->total_time_ns[BDRV_ACCT_READ];
1907 s->stats->flush_total_time_ns = bs->total_time_ns[BDRV_ACCT_FLUSH];
1908
294cc35f 1909 if (bs->file) {
f11f57e4
LC
1910 s->has_parent = true;
1911 s->parent = qmp_query_blockstat(bs->file, NULL);
294cc35f
KW
1912 }
1913
f11f57e4 1914 return s;
294cc35f
KW
1915}
1916
f11f57e4 1917BlockStatsList *qmp_query_blockstats(Error **errp)
218a536a 1918{
f11f57e4 1919 BlockStatsList *head = NULL, *cur_item = NULL;
a36e69dd
TS
1920 BlockDriverState *bs;
1921
1b7bdbc1 1922 QTAILQ_FOREACH(bs, &bdrv_states, list) {
f11f57e4
LC
1923 BlockStatsList *info = g_malloc0(sizeof(*info));
1924 info->value = qmp_query_blockstat(bs, NULL);
1925
1926 /* XXX: waiting for the qapi to support GSList */
1927 if (!cur_item) {
1928 head = cur_item = info;
1929 } else {
1930 cur_item->next = info;
1931 cur_item = info;
1932 }
a36e69dd 1933 }
218a536a 1934
f11f57e4 1935 return head;
a36e69dd 1936}
ea2384d3 1937
045df330
AL
1938const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1939{
1940 if (bs->backing_hd && bs->backing_hd->encrypted)
1941 return bs->backing_file;
1942 else if (bs->encrypted)
1943 return bs->filename;
1944 else
1945 return NULL;
1946}
1947
5fafdf24 1948void bdrv_get_backing_filename(BlockDriverState *bs,
83f64091
FB
1949 char *filename, int filename_size)
1950{
3574c608 1951 pstrcpy(filename, filename_size, bs->backing_file);
83f64091
FB
1952}
1953
5fafdf24 1954int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
faea38e7
FB
1955 const uint8_t *buf, int nb_sectors)
1956{
1957 BlockDriver *drv = bs->drv;
1958 if (!drv)
19cb3738 1959 return -ENOMEDIUM;
faea38e7
FB
1960 if (!drv->bdrv_write_compressed)
1961 return -ENOTSUP;
fbb7b4e0
KW
1962 if (bdrv_check_request(bs, sector_num, nb_sectors))
1963 return -EIO;
a55eb92c 1964
c6d22830 1965 if (bs->dirty_bitmap) {
7cd1e32a 1966 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1967 }
a55eb92c 1968
faea38e7
FB
1969 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1970}
3b46e624 1971
faea38e7
FB
1972int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1973{
1974 BlockDriver *drv = bs->drv;
1975 if (!drv)
19cb3738 1976 return -ENOMEDIUM;
faea38e7
FB
1977 if (!drv->bdrv_get_info)
1978 return -ENOTSUP;
1979 memset(bdi, 0, sizeof(*bdi));
1980 return drv->bdrv_get_info(bs, bdi);
1981}
1982
45566e9c
CH
1983int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
1984 int64_t pos, int size)
178e08a5
AL
1985{
1986 BlockDriver *drv = bs->drv;
1987 if (!drv)
1988 return -ENOMEDIUM;
7cdb1f6d
MK
1989 if (drv->bdrv_save_vmstate)
1990 return drv->bdrv_save_vmstate(bs, buf, pos, size);
1991 if (bs->file)
1992 return bdrv_save_vmstate(bs->file, buf, pos, size);
1993 return -ENOTSUP;
178e08a5
AL
1994}
1995
45566e9c
CH
1996int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1997 int64_t pos, int size)
178e08a5
AL
1998{
1999 BlockDriver *drv = bs->drv;
2000 if (!drv)
2001 return -ENOMEDIUM;
7cdb1f6d
MK
2002 if (drv->bdrv_load_vmstate)
2003 return drv->bdrv_load_vmstate(bs, buf, pos, size);
2004 if (bs->file)
2005 return bdrv_load_vmstate(bs->file, buf, pos, size);
2006 return -ENOTSUP;
178e08a5
AL
2007}
2008
8b9b0cc2
KW
2009void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
2010{
2011 BlockDriver *drv = bs->drv;
2012
2013 if (!drv || !drv->bdrv_debug_event) {
2014 return;
2015 }
2016
2017 return drv->bdrv_debug_event(bs, event);
2018
2019}
2020
faea38e7
FB
2021/**************************************************************/
2022/* handling of snapshots */
2023
feeee5ac
MDCF
2024int bdrv_can_snapshot(BlockDriverState *bs)
2025{
2026 BlockDriver *drv = bs->drv;
07b70bfb 2027 if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
feeee5ac
MDCF
2028 return 0;
2029 }
2030
2031 if (!drv->bdrv_snapshot_create) {
2032 if (bs->file != NULL) {
2033 return bdrv_can_snapshot(bs->file);
2034 }
2035 return 0;
2036 }
2037
2038 return 1;
2039}
2040
199630b6
BS
2041int bdrv_is_snapshot(BlockDriverState *bs)
2042{
2043 return !!(bs->open_flags & BDRV_O_SNAPSHOT);
2044}
2045
f9092b10
MA
2046BlockDriverState *bdrv_snapshots(void)
2047{
2048 BlockDriverState *bs;
2049
3ac906f7 2050 if (bs_snapshots) {
f9092b10 2051 return bs_snapshots;
3ac906f7 2052 }
f9092b10
MA
2053
2054 bs = NULL;
2055 while ((bs = bdrv_next(bs))) {
2056 if (bdrv_can_snapshot(bs)) {
3ac906f7
MA
2057 bs_snapshots = bs;
2058 return bs;
f9092b10
MA
2059 }
2060 }
2061 return NULL;
f9092b10
MA
2062}
2063
5fafdf24 2064int bdrv_snapshot_create(BlockDriverState *bs,
faea38e7
FB
2065 QEMUSnapshotInfo *sn_info)
2066{
2067 BlockDriver *drv = bs->drv;
2068 if (!drv)
19cb3738 2069 return -ENOMEDIUM;
7cdb1f6d
MK
2070 if (drv->bdrv_snapshot_create)
2071 return drv->bdrv_snapshot_create(bs, sn_info);
2072 if (bs->file)
2073 return bdrv_snapshot_create(bs->file, sn_info);
2074 return -ENOTSUP;
faea38e7
FB
2075}
2076
5fafdf24 2077int bdrv_snapshot_goto(BlockDriverState *bs,
faea38e7
FB
2078 const char *snapshot_id)
2079{
2080 BlockDriver *drv = bs->drv;
7cdb1f6d
MK
2081 int ret, open_ret;
2082
faea38e7 2083 if (!drv)
19cb3738 2084 return -ENOMEDIUM;
7cdb1f6d
MK
2085 if (drv->bdrv_snapshot_goto)
2086 return drv->bdrv_snapshot_goto(bs, snapshot_id);
2087
2088 if (bs->file) {
2089 drv->bdrv_close(bs);
2090 ret = bdrv_snapshot_goto(bs->file, snapshot_id);
2091 open_ret = drv->bdrv_open(bs, bs->open_flags);
2092 if (open_ret < 0) {
2093 bdrv_delete(bs->file);
2094 bs->drv = NULL;
2095 return open_ret;
2096 }
2097 return ret;
2098 }
2099
2100 return -ENOTSUP;
faea38e7
FB
2101}
2102
2103int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
2104{
2105 BlockDriver *drv = bs->drv;
2106 if (!drv)
19cb3738 2107 return -ENOMEDIUM;
7cdb1f6d
MK
2108 if (drv->bdrv_snapshot_delete)
2109 return drv->bdrv_snapshot_delete(bs, snapshot_id);
2110 if (bs->file)
2111 return bdrv_snapshot_delete(bs->file, snapshot_id);
2112 return -ENOTSUP;
faea38e7
FB
2113}
2114
5fafdf24 2115int bdrv_snapshot_list(BlockDriverState *bs,
faea38e7
FB
2116 QEMUSnapshotInfo **psn_info)
2117{
2118 BlockDriver *drv = bs->drv;
2119 if (!drv)
19cb3738 2120 return -ENOMEDIUM;
7cdb1f6d
MK
2121 if (drv->bdrv_snapshot_list)
2122 return drv->bdrv_snapshot_list(bs, psn_info);
2123 if (bs->file)
2124 return bdrv_snapshot_list(bs->file, psn_info);
2125 return -ENOTSUP;
faea38e7
FB
2126}
2127
51ef6727 2128int bdrv_snapshot_load_tmp(BlockDriverState *bs,
2129 const char *snapshot_name)
2130{
2131 BlockDriver *drv = bs->drv;
2132 if (!drv) {
2133 return -ENOMEDIUM;
2134 }
2135 if (!bs->read_only) {
2136 return -EINVAL;
2137 }
2138 if (drv->bdrv_snapshot_load_tmp) {
2139 return drv->bdrv_snapshot_load_tmp(bs, snapshot_name);
2140 }
2141 return -ENOTSUP;
2142}
2143
faea38e7
FB
2144#define NB_SUFFIXES 4
2145
2146char *get_human_readable_size(char *buf, int buf_size, int64_t size)
2147{
2148 static const char suffixes[NB_SUFFIXES] = "KMGT";
2149 int64_t base;
2150 int i;
2151
2152 if (size <= 999) {
2153 snprintf(buf, buf_size, "%" PRId64, size);
2154 } else {
2155 base = 1024;
2156 for(i = 0; i < NB_SUFFIXES; i++) {
2157 if (size < (10 * base)) {
5fafdf24 2158 snprintf(buf, buf_size, "%0.1f%c",
faea38e7
FB
2159 (double)size / base,
2160 suffixes[i]);
2161 break;
2162 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
5fafdf24 2163 snprintf(buf, buf_size, "%" PRId64 "%c",
faea38e7
FB
2164 ((size + (base >> 1)) / base),
2165 suffixes[i]);
2166 break;
2167 }
2168 base = base * 1024;
2169 }
2170 }
2171 return buf;
2172}
2173
2174char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
2175{
2176 char buf1[128], date_buf[128], clock_buf[128];
3b9f94e1
FB
2177#ifdef _WIN32
2178 struct tm *ptm;
2179#else
faea38e7 2180 struct tm tm;
3b9f94e1 2181#endif
faea38e7
FB
2182 time_t ti;
2183 int64_t secs;
2184
2185 if (!sn) {
5fafdf24
TS
2186 snprintf(buf, buf_size,
2187 "%-10s%-20s%7s%20s%15s",
faea38e7
FB
2188 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
2189 } else {
2190 ti = sn->date_sec;
3b9f94e1
FB
2191#ifdef _WIN32
2192 ptm = localtime(&ti);
2193 strftime(date_buf, sizeof(date_buf),
2194 "%Y-%m-%d %H:%M:%S", ptm);
2195#else
faea38e7
FB
2196 localtime_r(&ti, &tm);
2197 strftime(date_buf, sizeof(date_buf),
2198 "%Y-%m-%d %H:%M:%S", &tm);
3b9f94e1 2199#endif
faea38e7
FB
2200 secs = sn->vm_clock_nsec / 1000000000;
2201 snprintf(clock_buf, sizeof(clock_buf),
2202 "%02d:%02d:%02d.%03d",
2203 (int)(secs / 3600),
2204 (int)((secs / 60) % 60),
5fafdf24 2205 (int)(secs % 60),
faea38e7
FB
2206 (int)((sn->vm_clock_nsec / 1000000) % 1000));
2207 snprintf(buf, buf_size,
5fafdf24 2208 "%-10s%-20s%7s%20s%15s",
faea38e7
FB
2209 sn->id_str, sn->name,
2210 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
2211 date_buf,
2212 clock_buf);
2213 }
2214 return buf;
2215}
2216
ea2384d3 2217/**************************************************************/
83f64091 2218/* async I/Os */
ea2384d3 2219
3b69e4b9 2220BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
f141eafe 2221 QEMUIOVector *qiov, int nb_sectors,
3b69e4b9 2222 BlockDriverCompletionFunc *cb, void *opaque)
83f64091 2223{
bbf0a440
SH
2224 trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque);
2225
b2a61371 2226 return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors,
8c5873d6 2227 cb, opaque, false);
ea2384d3
FB
2228}
2229
f141eafe
AL
2230BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
2231 QEMUIOVector *qiov, int nb_sectors,
2232 BlockDriverCompletionFunc *cb, void *opaque)
ea2384d3 2233{
bbf0a440
SH
2234 trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque);
2235
1a6e115b 2236 return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors,
8c5873d6 2237 cb, opaque, true);
83f64091
FB
2238}
2239
40b4f539
KW
2240
2241typedef struct MultiwriteCB {
2242 int error;
2243 int num_requests;
2244 int num_callbacks;
2245 struct {
2246 BlockDriverCompletionFunc *cb;
2247 void *opaque;
2248 QEMUIOVector *free_qiov;
2249 void *free_buf;
2250 } callbacks[];
2251} MultiwriteCB;
2252
2253static void multiwrite_user_cb(MultiwriteCB *mcb)
2254{
2255 int i;
2256
2257 for (i = 0; i < mcb->num_callbacks; i++) {
2258 mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
1e1ea48d
SH
2259 if (mcb->callbacks[i].free_qiov) {
2260 qemu_iovec_destroy(mcb->callbacks[i].free_qiov);
2261 }
7267c094 2262 g_free(mcb->callbacks[i].free_qiov);
f8a83245 2263 qemu_vfree(mcb->callbacks[i].free_buf);
40b4f539
KW
2264 }
2265}
2266
2267static void multiwrite_cb(void *opaque, int ret)
2268{
2269 MultiwriteCB *mcb = opaque;
2270
6d519a5f
SH
2271 trace_multiwrite_cb(mcb, ret);
2272
cb6d3ca0 2273 if (ret < 0 && !mcb->error) {
40b4f539 2274 mcb->error = ret;
40b4f539
KW
2275 }
2276
2277 mcb->num_requests--;
2278 if (mcb->num_requests == 0) {
de189a1b 2279 multiwrite_user_cb(mcb);
7267c094 2280 g_free(mcb);
40b4f539
KW
2281 }
2282}
2283
2284static int multiwrite_req_compare(const void *a, const void *b)
2285{
77be4366
CH
2286 const BlockRequest *req1 = a, *req2 = b;
2287
2288 /*
2289 * Note that we can't simply subtract req2->sector from req1->sector
2290 * here as that could overflow the return value.
2291 */
2292 if (req1->sector > req2->sector) {
2293 return 1;
2294 } else if (req1->sector < req2->sector) {
2295 return -1;
2296 } else {
2297 return 0;
2298 }
40b4f539
KW
2299}
2300
2301/*
2302 * Takes a bunch of requests and tries to merge them. Returns the number of
2303 * requests that remain after merging.
2304 */
2305static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
2306 int num_reqs, MultiwriteCB *mcb)
2307{
2308 int i, outidx;
2309
2310 // Sort requests by start sector
2311 qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
2312
2313 // Check if adjacent requests touch the same clusters. If so, combine them,
2314 // filling up gaps with zero sectors.
2315 outidx = 0;
2316 for (i = 1; i < num_reqs; i++) {
2317 int merge = 0;
2318 int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
2319
2320 // This handles the cases that are valid for all block drivers, namely
2321 // exactly sequential writes and overlapping writes.
2322 if (reqs[i].sector <= oldreq_last) {
2323 merge = 1;
2324 }
2325
2326 // The block driver may decide that it makes sense to combine requests
2327 // even if there is a gap of some sectors between them. In this case,
2328 // the gap is filled with zeros (therefore only applicable for yet
2329 // unused space in format like qcow2).
2330 if (!merge && bs->drv->bdrv_merge_requests) {
2331 merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]);
2332 }
2333
e2a305fb
CH
2334 if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) {
2335 merge = 0;
2336 }
2337
40b4f539
KW
2338 if (merge) {
2339 size_t size;
7267c094 2340 QEMUIOVector *qiov = g_malloc0(sizeof(*qiov));
40b4f539
KW
2341 qemu_iovec_init(qiov,
2342 reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
2343
2344 // Add the first request to the merged one. If the requests are
2345 // overlapping, drop the last sectors of the first request.
2346 size = (reqs[i].sector - reqs[outidx].sector) << 9;
2347 qemu_iovec_concat(qiov, reqs[outidx].qiov, size);
2348
2349 // We might need to add some zeros between the two requests
2350 if (reqs[i].sector > oldreq_last) {
2351 size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
2352 uint8_t *buf = qemu_blockalign(bs, zero_bytes);
2353 memset(buf, 0, zero_bytes);
2354 qemu_iovec_add(qiov, buf, zero_bytes);
2355 mcb->callbacks[i].free_buf = buf;
2356 }
2357
2358 // Add the second request
2359 qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size);
2360
cbf1dff2 2361 reqs[outidx].nb_sectors = qiov->size >> 9;
40b4f539
KW
2362 reqs[outidx].qiov = qiov;
2363
2364 mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
2365 } else {
2366 outidx++;
2367 reqs[outidx].sector = reqs[i].sector;
2368 reqs[outidx].nb_sectors = reqs[i].nb_sectors;
2369 reqs[outidx].qiov = reqs[i].qiov;
2370 }
2371 }
2372
2373 return outidx + 1;
2374}
2375
2376/*
2377 * Submit multiple AIO write requests at once.
2378 *
2379 * On success, the function returns 0 and all requests in the reqs array have
2380 * been submitted. In error case this function returns -1, and any of the
2381 * requests may or may not be submitted yet. In particular, this means that the
2382 * callback will be called for some of the requests, for others it won't. The
2383 * caller must check the error field of the BlockRequest to wait for the right
2384 * callbacks (if error != 0, no callback will be called).
2385 *
2386 * The implementation may modify the contents of the reqs array, e.g. to merge
2387 * requests. However, the fields opaque and error are left unmodified as they
2388 * are used to signal failure for a single request to the caller.
2389 */
2390int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
2391{
2392 BlockDriverAIOCB *acb;
2393 MultiwriteCB *mcb;
2394 int i;
2395
301db7c2
RH
2396 /* don't submit writes if we don't have a medium */
2397 if (bs->drv == NULL) {
2398 for (i = 0; i < num_reqs; i++) {
2399 reqs[i].error = -ENOMEDIUM;
2400 }
2401 return -1;
2402 }
2403
40b4f539
KW
2404 if (num_reqs == 0) {
2405 return 0;
2406 }
2407
2408 // Create MultiwriteCB structure
7267c094 2409 mcb = g_malloc0(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
40b4f539
KW
2410 mcb->num_requests = 0;
2411 mcb->num_callbacks = num_reqs;
2412
2413 for (i = 0; i < num_reqs; i++) {
2414 mcb->callbacks[i].cb = reqs[i].cb;
2415 mcb->callbacks[i].opaque = reqs[i].opaque;
2416 }
2417
2418 // Check for mergable requests
2419 num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
2420
6d519a5f
SH
2421 trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs);
2422
453f9a16
KW
2423 /*
2424 * Run the aio requests. As soon as one request can't be submitted
2425 * successfully, fail all requests that are not yet submitted (we must
2426 * return failure for all requests anyway)
2427 *
2428 * num_requests cannot be set to the right value immediately: If
2429 * bdrv_aio_writev fails for some request, num_requests would be too high
2430 * and therefore multiwrite_cb() would never recognize the multiwrite
2431 * request as completed. We also cannot use the loop variable i to set it
2432 * when the first request fails because the callback may already have been
2433 * called for previously submitted requests. Thus, num_requests must be
2434 * incremented for each request that is submitted.
2435 *
2436 * The problem that callbacks may be called early also means that we need
2437 * to take care that num_requests doesn't become 0 before all requests are
2438 * submitted - multiwrite_cb() would consider the multiwrite request
2439 * completed. A dummy request that is "completed" by a manual call to
2440 * multiwrite_cb() takes care of this.
2441 */
2442 mcb->num_requests = 1;
2443
6d519a5f 2444 // Run the aio requests
40b4f539 2445 for (i = 0; i < num_reqs; i++) {
453f9a16 2446 mcb->num_requests++;
40b4f539
KW
2447 acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
2448 reqs[i].nb_sectors, multiwrite_cb, mcb);
2449
2450 if (acb == NULL) {
2451 // We can only fail the whole thing if no request has been
2452 // submitted yet. Otherwise we'll wait for the submitted AIOs to
2453 // complete and report the error in the callback.
453f9a16 2454 if (i == 0) {
6d519a5f 2455 trace_bdrv_aio_multiwrite_earlyfail(mcb);
40b4f539
KW
2456 goto fail;
2457 } else {
6d519a5f 2458 trace_bdrv_aio_multiwrite_latefail(mcb, i);
7eb58a6c 2459 multiwrite_cb(mcb, -EIO);
40b4f539
KW
2460 break;
2461 }
40b4f539
KW
2462 }
2463 }
2464
453f9a16
KW
2465 /* Complete the dummy request */
2466 multiwrite_cb(mcb, 0);
2467
40b4f539
KW
2468 return 0;
2469
2470fail:
453f9a16
KW
2471 for (i = 0; i < mcb->num_callbacks; i++) {
2472 reqs[i].error = -EIO;
2473 }
7267c094 2474 g_free(mcb);
40b4f539
KW
2475 return -1;
2476}
2477
83f64091 2478void bdrv_aio_cancel(BlockDriverAIOCB *acb)
83f64091 2479{
6bbff9a0 2480 acb->pool->cancel(acb);
83f64091
FB
2481}
2482
ce1a14dc 2483
83f64091
FB
2484/**************************************************************/
2485/* async block device emulation */
2486
c16b5a2c
CH
2487typedef struct BlockDriverAIOCBSync {
2488 BlockDriverAIOCB common;
2489 QEMUBH *bh;
2490 int ret;
2491 /* vector translation state */
2492 QEMUIOVector *qiov;
2493 uint8_t *bounce;
2494 int is_write;
2495} BlockDriverAIOCBSync;
2496
2497static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
2498{
b666d239
KW
2499 BlockDriverAIOCBSync *acb =
2500 container_of(blockacb, BlockDriverAIOCBSync, common);
6a7ad299 2501 qemu_bh_delete(acb->bh);
36afc451 2502 acb->bh = NULL;
c16b5a2c
CH
2503 qemu_aio_release(acb);
2504}
2505
2506static AIOPool bdrv_em_aio_pool = {
2507 .aiocb_size = sizeof(BlockDriverAIOCBSync),
2508 .cancel = bdrv_aio_cancel_em,
2509};
2510
ce1a14dc 2511static void bdrv_aio_bh_cb(void *opaque)
83f64091 2512{
ce1a14dc 2513 BlockDriverAIOCBSync *acb = opaque;
f141eafe 2514
f141eafe
AL
2515 if (!acb->is_write)
2516 qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
ceb42de8 2517 qemu_vfree(acb->bounce);
ce1a14dc 2518 acb->common.cb(acb->common.opaque, acb->ret);
6a7ad299 2519 qemu_bh_delete(acb->bh);
36afc451 2520 acb->bh = NULL;
ce1a14dc 2521 qemu_aio_release(acb);
83f64091 2522}
beac80cd 2523
f141eafe
AL
2524static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
2525 int64_t sector_num,
2526 QEMUIOVector *qiov,
2527 int nb_sectors,
2528 BlockDriverCompletionFunc *cb,
2529 void *opaque,
2530 int is_write)
2531
83f64091 2532{
ce1a14dc 2533 BlockDriverAIOCBSync *acb;
ce1a14dc 2534
c16b5a2c 2535 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
f141eafe
AL
2536 acb->is_write = is_write;
2537 acb->qiov = qiov;
e268ca52 2538 acb->bounce = qemu_blockalign(bs, qiov->size);
f141eafe 2539
ce1a14dc
PB
2540 if (!acb->bh)
2541 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
f141eafe
AL
2542
2543 if (is_write) {
2544 qemu_iovec_to_buffer(acb->qiov, acb->bounce);
1ed20acf 2545 acb->ret = bs->drv->bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
f141eafe 2546 } else {
1ed20acf 2547 acb->ret = bs->drv->bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
f141eafe
AL
2548 }
2549
ce1a14dc 2550 qemu_bh_schedule(acb->bh);
f141eafe 2551
ce1a14dc 2552 return &acb->common;
beac80cd
FB
2553}
2554
f141eafe
AL
2555static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
2556 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
ce1a14dc 2557 BlockDriverCompletionFunc *cb, void *opaque)
beac80cd 2558{
f141eafe
AL
2559 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
2560}
83f64091 2561
f141eafe
AL
2562static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
2563 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2564 BlockDriverCompletionFunc *cb, void *opaque)
2565{
2566 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
beac80cd 2567}
beac80cd 2568
68485420
KW
2569
2570typedef struct BlockDriverAIOCBCoroutine {
2571 BlockDriverAIOCB common;
2572 BlockRequest req;
2573 bool is_write;
2574 QEMUBH* bh;
2575} BlockDriverAIOCBCoroutine;
2576
2577static void bdrv_aio_co_cancel_em(BlockDriverAIOCB *blockacb)
2578{
2579 qemu_aio_flush();
2580}
2581
2582static AIOPool bdrv_em_co_aio_pool = {
2583 .aiocb_size = sizeof(BlockDriverAIOCBCoroutine),
2584 .cancel = bdrv_aio_co_cancel_em,
2585};
2586
35246a68 2587static void bdrv_co_em_bh(void *opaque)
68485420
KW
2588{
2589 BlockDriverAIOCBCoroutine *acb = opaque;
2590
2591 acb->common.cb(acb->common.opaque, acb->req.error);
2592 qemu_bh_delete(acb->bh);
2593 qemu_aio_release(acb);
2594}
2595
b2a61371
SH
2596/* Invoke bdrv_co_do_readv/bdrv_co_do_writev */
2597static void coroutine_fn bdrv_co_do_rw(void *opaque)
2598{
2599 BlockDriverAIOCBCoroutine *acb = opaque;
2600 BlockDriverState *bs = acb->common.bs;
2601
2602 if (!acb->is_write) {
2603 acb->req.error = bdrv_co_do_readv(bs, acb->req.sector,
2604 acb->req.nb_sectors, acb->req.qiov);
2605 } else {
2606 acb->req.error = bdrv_co_do_writev(bs, acb->req.sector,
2607 acb->req.nb_sectors, acb->req.qiov);
2608 }
2609
35246a68 2610 acb->bh = qemu_bh_new(bdrv_co_em_bh, acb);
b2a61371
SH
2611 qemu_bh_schedule(acb->bh);
2612}
2613
68485420
KW
2614static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs,
2615 int64_t sector_num,
2616 QEMUIOVector *qiov,
2617 int nb_sectors,
2618 BlockDriverCompletionFunc *cb,
2619 void *opaque,
8c5873d6 2620 bool is_write)
68485420
KW
2621{
2622 Coroutine *co;
2623 BlockDriverAIOCBCoroutine *acb;
2624
2625 acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque);
2626 acb->req.sector = sector_num;
2627 acb->req.nb_sectors = nb_sectors;
2628 acb->req.qiov = qiov;
2629 acb->is_write = is_write;
2630
8c5873d6 2631 co = qemu_coroutine_create(bdrv_co_do_rw);
68485420
KW
2632 qemu_coroutine_enter(co, acb);
2633
2634 return &acb->common;
2635}
2636
07f07615 2637static void coroutine_fn bdrv_aio_flush_co_entry(void *opaque)
b2e12bc6 2638{
07f07615
PB
2639 BlockDriverAIOCBCoroutine *acb = opaque;
2640 BlockDriverState *bs = acb->common.bs;
b2e12bc6 2641
07f07615
PB
2642 acb->req.error = bdrv_co_flush(bs);
2643 acb->bh = qemu_bh_new(bdrv_co_em_bh, acb);
b2e12bc6 2644 qemu_bh_schedule(acb->bh);
b2e12bc6
CH
2645}
2646
07f07615 2647BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
016f5cf6
AG
2648 BlockDriverCompletionFunc *cb, void *opaque)
2649{
07f07615 2650 trace_bdrv_aio_flush(bs, opaque);
016f5cf6 2651
07f07615
PB
2652 Coroutine *co;
2653 BlockDriverAIOCBCoroutine *acb;
016f5cf6 2654
07f07615
PB
2655 acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque);
2656 co = qemu_coroutine_create(bdrv_aio_flush_co_entry);
2657 qemu_coroutine_enter(co, acb);
016f5cf6 2658
016f5cf6
AG
2659 return &acb->common;
2660}
2661
4265d620
PB
2662static void coroutine_fn bdrv_aio_discard_co_entry(void *opaque)
2663{
2664 BlockDriverAIOCBCoroutine *acb = opaque;
2665 BlockDriverState *bs = acb->common.bs;
2666
2667 acb->req.error = bdrv_co_discard(bs, acb->req.sector, acb->req.nb_sectors);
2668 acb->bh = qemu_bh_new(bdrv_co_em_bh, acb);
2669 qemu_bh_schedule(acb->bh);
2670}
2671
2672BlockDriverAIOCB *bdrv_aio_discard(BlockDriverState *bs,
2673 int64_t sector_num, int nb_sectors,
2674 BlockDriverCompletionFunc *cb, void *opaque)
2675{
2676 Coroutine *co;
2677 BlockDriverAIOCBCoroutine *acb;
2678
2679 trace_bdrv_aio_discard(bs, sector_num, nb_sectors, opaque);
2680
2681 acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque);
2682 acb->req.sector = sector_num;
2683 acb->req.nb_sectors = nb_sectors;
2684 co = qemu_coroutine_create(bdrv_aio_discard_co_entry);
2685 qemu_coroutine_enter(co, acb);
2686
2687 return &acb->common;
2688}
2689
ea2384d3
FB
2690void bdrv_init(void)
2691{
5efa9d5a 2692 module_call_init(MODULE_INIT_BLOCK);
ea2384d3 2693}
ce1a14dc 2694
eb852011
MA
2695void bdrv_init_with_whitelist(void)
2696{
2697 use_bdrv_whitelist = 1;
2698 bdrv_init();
2699}
2700
c16b5a2c
CH
2701void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
2702 BlockDriverCompletionFunc *cb, void *opaque)
ce1a14dc 2703{
ce1a14dc
PB
2704 BlockDriverAIOCB *acb;
2705
6bbff9a0
AL
2706 if (pool->free_aiocb) {
2707 acb = pool->free_aiocb;
2708 pool->free_aiocb = acb->next;
ce1a14dc 2709 } else {
7267c094 2710 acb = g_malloc0(pool->aiocb_size);
6bbff9a0 2711 acb->pool = pool;
ce1a14dc
PB
2712 }
2713 acb->bs = bs;
2714 acb->cb = cb;
2715 acb->opaque = opaque;
2716 return acb;
2717}
2718
2719void qemu_aio_release(void *p)
2720{
6bbff9a0
AL
2721 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
2722 AIOPool *pool = acb->pool;
2723 acb->next = pool->free_aiocb;
2724 pool->free_aiocb = acb;
ce1a14dc 2725}
19cb3738 2726
f9f05dc5
KW
2727/**************************************************************/
2728/* Coroutine block device emulation */
2729
2730typedef struct CoroutineIOCompletion {
2731 Coroutine *coroutine;
2732 int ret;
2733} CoroutineIOCompletion;
2734
2735static void bdrv_co_io_em_complete(void *opaque, int ret)
2736{
2737 CoroutineIOCompletion *co = opaque;
2738
2739 co->ret = ret;
2740 qemu_coroutine_enter(co->coroutine, NULL);
2741}
2742
2743static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num,
2744 int nb_sectors, QEMUIOVector *iov,
2745 bool is_write)
2746{
2747 CoroutineIOCompletion co = {
2748 .coroutine = qemu_coroutine_self(),
2749 };
2750 BlockDriverAIOCB *acb;
2751
2752 if (is_write) {
a652d160
SH
2753 acb = bs->drv->bdrv_aio_writev(bs, sector_num, iov, nb_sectors,
2754 bdrv_co_io_em_complete, &co);
f9f05dc5 2755 } else {
a652d160
SH
2756 acb = bs->drv->bdrv_aio_readv(bs, sector_num, iov, nb_sectors,
2757 bdrv_co_io_em_complete, &co);
f9f05dc5
KW
2758 }
2759
59370aaa 2760 trace_bdrv_co_io_em(bs, sector_num, nb_sectors, is_write, acb);
f9f05dc5
KW
2761 if (!acb) {
2762 return -EIO;
2763 }
2764 qemu_coroutine_yield();
2765
2766 return co.ret;
2767}
2768
2769static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs,
2770 int64_t sector_num, int nb_sectors,
2771 QEMUIOVector *iov)
2772{
2773 return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false);
2774}
2775
2776static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs,
2777 int64_t sector_num, int nb_sectors,
2778 QEMUIOVector *iov)
2779{
2780 return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true);
2781}
2782
07f07615 2783static void coroutine_fn bdrv_flush_co_entry(void *opaque)
e7a8a783 2784{
07f07615
PB
2785 RwCo *rwco = opaque;
2786
2787 rwco->ret = bdrv_co_flush(rwco->bs);
2788}
2789
2790int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
2791{
2792 if (bs->open_flags & BDRV_O_NO_FLUSH) {
2793 return 0;
2794 } else if (!bs->drv) {
2795 return 0;
c68b89ac
KW
2796 } else if (bs->drv->bdrv_co_flush_to_disk) {
2797 return bs->drv->bdrv_co_flush_to_disk(bs);
07f07615
PB
2798 } else if (bs->drv->bdrv_aio_flush) {
2799 BlockDriverAIOCB *acb;
2800 CoroutineIOCompletion co = {
2801 .coroutine = qemu_coroutine_self(),
2802 };
2803
2804 acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co);
2805 if (acb == NULL) {
2806 return -EIO;
2807 } else {
2808 qemu_coroutine_yield();
2809 return co.ret;
2810 }
07f07615
PB
2811 } else {
2812 /*
2813 * Some block drivers always operate in either writethrough or unsafe
2814 * mode and don't support bdrv_flush therefore. Usually qemu doesn't
2815 * know how the server works (because the behaviour is hardcoded or
2816 * depends on server-side configuration), so we can't ensure that
2817 * everything is safe on disk. Returning an error doesn't work because
2818 * that would break guests even if the server operates in writethrough
2819 * mode.
2820 *
2821 * Let's hope the user knows what he's doing.
2822 */
2823 return 0;
2824 }
2825}
2826
2827int bdrv_flush(BlockDriverState *bs)
2828{
2829 Coroutine *co;
2830 RwCo rwco = {
2831 .bs = bs,
2832 .ret = NOT_DONE,
e7a8a783 2833 };
e7a8a783 2834
07f07615
PB
2835 if (qemu_in_coroutine()) {
2836 /* Fast-path if already in coroutine context */
2837 bdrv_flush_co_entry(&rwco);
2838 } else {
2839 co = qemu_coroutine_create(bdrv_flush_co_entry);
2840 qemu_coroutine_enter(co, &rwco);
2841 while (rwco.ret == NOT_DONE) {
2842 qemu_aio_wait();
2843 }
e7a8a783 2844 }
07f07615
PB
2845
2846 return rwco.ret;
e7a8a783
KW
2847}
2848
4265d620
PB
2849static void coroutine_fn bdrv_discard_co_entry(void *opaque)
2850{
2851 RwCo *rwco = opaque;
2852
2853 rwco->ret = bdrv_co_discard(rwco->bs, rwco->sector_num, rwco->nb_sectors);
2854}
2855
2856int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num,
2857 int nb_sectors)
2858{
2859 if (!bs->drv) {
2860 return -ENOMEDIUM;
2861 } else if (bdrv_check_request(bs, sector_num, nb_sectors)) {
2862 return -EIO;
2863 } else if (bs->read_only) {
2864 return -EROFS;
2865 } else if (bs->drv->bdrv_co_discard) {
2866 return bs->drv->bdrv_co_discard(bs, sector_num, nb_sectors);
2867 } else if (bs->drv->bdrv_aio_discard) {
2868 BlockDriverAIOCB *acb;
2869 CoroutineIOCompletion co = {
2870 .coroutine = qemu_coroutine_self(),
2871 };
2872
2873 acb = bs->drv->bdrv_aio_discard(bs, sector_num, nb_sectors,
2874 bdrv_co_io_em_complete, &co);
2875 if (acb == NULL) {
2876 return -EIO;
2877 } else {
2878 qemu_coroutine_yield();
2879 return co.ret;
2880 }
4265d620
PB
2881 } else {
2882 return 0;
2883 }
2884}
2885
2886int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors)
2887{
2888 Coroutine *co;
2889 RwCo rwco = {
2890 .bs = bs,
2891 .sector_num = sector_num,
2892 .nb_sectors = nb_sectors,
2893 .ret = NOT_DONE,
2894 };
2895
2896 if (qemu_in_coroutine()) {
2897 /* Fast-path if already in coroutine context */
2898 bdrv_discard_co_entry(&rwco);
2899 } else {
2900 co = qemu_coroutine_create(bdrv_discard_co_entry);
2901 qemu_coroutine_enter(co, &rwco);
2902 while (rwco.ret == NOT_DONE) {
2903 qemu_aio_wait();
2904 }
2905 }
2906
2907 return rwco.ret;
2908}
2909
19cb3738
FB
2910/**************************************************************/
2911/* removable device support */
2912
2913/**
2914 * Return TRUE if the media is present
2915 */
2916int bdrv_is_inserted(BlockDriverState *bs)
2917{
2918 BlockDriver *drv = bs->drv;
a1aff5bf 2919
19cb3738
FB
2920 if (!drv)
2921 return 0;
2922 if (!drv->bdrv_is_inserted)
a1aff5bf
MA
2923 return 1;
2924 return drv->bdrv_is_inserted(bs);
19cb3738
FB
2925}
2926
2927/**
8e49ca46
MA
2928 * Return whether the media changed since the last call to this
2929 * function, or -ENOTSUP if we don't know. Most drivers don't know.
19cb3738
FB
2930 */
2931int bdrv_media_changed(BlockDriverState *bs)
2932{
2933 BlockDriver *drv = bs->drv;
19cb3738 2934
8e49ca46
MA
2935 if (drv && drv->bdrv_media_changed) {
2936 return drv->bdrv_media_changed(bs);
2937 }
2938 return -ENOTSUP;
19cb3738
FB
2939}
2940
2941/**
2942 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
2943 */
fdec4404 2944void bdrv_eject(BlockDriverState *bs, int eject_flag)
19cb3738
FB
2945{
2946 BlockDriver *drv = bs->drv;
19cb3738 2947
822e1cd1
MA
2948 if (drv && drv->bdrv_eject) {
2949 drv->bdrv_eject(bs, eject_flag);
19cb3738
FB
2950 }
2951}
2952
19cb3738
FB
2953/**
2954 * Lock or unlock the media (if it is locked, the user won't be able
2955 * to eject it manually).
2956 */
025e849a 2957void bdrv_lock_medium(BlockDriverState *bs, bool locked)
19cb3738
FB
2958{
2959 BlockDriver *drv = bs->drv;
2960
025e849a 2961 trace_bdrv_lock_medium(bs, locked);
b8c6d095 2962
025e849a
MA
2963 if (drv && drv->bdrv_lock_medium) {
2964 drv->bdrv_lock_medium(bs, locked);
19cb3738
FB
2965 }
2966}
985a03b0
TS
2967
2968/* needed for generic scsi interface */
2969
2970int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
2971{
2972 BlockDriver *drv = bs->drv;
2973
2974 if (drv && drv->bdrv_ioctl)
2975 return drv->bdrv_ioctl(bs, req, buf);
2976 return -ENOTSUP;
2977}
7d780669 2978
221f715d
AL
2979BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
2980 unsigned long int req, void *buf,
2981 BlockDriverCompletionFunc *cb, void *opaque)
7d780669 2982{
221f715d 2983 BlockDriver *drv = bs->drv;
7d780669 2984
221f715d
AL
2985 if (drv && drv->bdrv_aio_ioctl)
2986 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
2987 return NULL;
7d780669 2988}
e268ca52 2989
7b6f9300
MA
2990void bdrv_set_buffer_alignment(BlockDriverState *bs, int align)
2991{
2992 bs->buffer_alignment = align;
2993}
7cd1e32a 2994
e268ca52
AL
2995void *qemu_blockalign(BlockDriverState *bs, size_t size)
2996{
2997 return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
2998}
7cd1e32a 2999
3000void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable)
3001{
3002 int64_t bitmap_size;
a55eb92c 3003
aaa0eb75 3004 bs->dirty_count = 0;
a55eb92c 3005 if (enable) {
c6d22830
JK
3006 if (!bs->dirty_bitmap) {
3007 bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
3008 BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
3009 bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
a55eb92c 3010
7267c094 3011 bs->dirty_bitmap = g_malloc0(bitmap_size);
a55eb92c 3012 }
7cd1e32a 3013 } else {
c6d22830 3014 if (bs->dirty_bitmap) {
7267c094 3015 g_free(bs->dirty_bitmap);
c6d22830 3016 bs->dirty_bitmap = NULL;
a55eb92c 3017 }
7cd1e32a 3018 }
3019}
3020
3021int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
3022{
6ea44308 3023 int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
a55eb92c 3024
c6d22830
JK
3025 if (bs->dirty_bitmap &&
3026 (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) {
6d59fec1
MT
3027 return !!(bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] &
3028 (1UL << (chunk % (sizeof(unsigned long) * 8))));
7cd1e32a 3029 } else {
3030 return 0;
3031 }
3032}
3033
a55eb92c
JK
3034void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
3035 int nr_sectors)
7cd1e32a 3036{
3037 set_dirty_bitmap(bs, cur_sector, nr_sectors, 0);
3038}
aaa0eb75
LS
3039
3040int64_t bdrv_get_dirty_count(BlockDriverState *bs)
3041{
3042 return bs->dirty_count;
3043}
f88e1a42 3044
db593f25
MT
3045void bdrv_set_in_use(BlockDriverState *bs, int in_use)
3046{
3047 assert(bs->in_use != in_use);
3048 bs->in_use = in_use;
3049}
3050
3051int bdrv_in_use(BlockDriverState *bs)
3052{
3053 return bs->in_use;
3054}
3055
28a7282a
LC
3056void bdrv_iostatus_enable(BlockDriverState *bs)
3057{
d6bf279e 3058 bs->iostatus_enabled = true;
58e21ef5 3059 bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
28a7282a
LC
3060}
3061
3062/* The I/O status is only enabled if the drive explicitly
3063 * enables it _and_ the VM is configured to stop on errors */
3064bool bdrv_iostatus_is_enabled(const BlockDriverState *bs)
3065{
d6bf279e 3066 return (bs->iostatus_enabled &&
28a7282a
LC
3067 (bs->on_write_error == BLOCK_ERR_STOP_ENOSPC ||
3068 bs->on_write_error == BLOCK_ERR_STOP_ANY ||
3069 bs->on_read_error == BLOCK_ERR_STOP_ANY));
3070}
3071
3072void bdrv_iostatus_disable(BlockDriverState *bs)
3073{
d6bf279e 3074 bs->iostatus_enabled = false;
28a7282a
LC
3075}
3076
3077void bdrv_iostatus_reset(BlockDriverState *bs)
3078{
3079 if (bdrv_iostatus_is_enabled(bs)) {
58e21ef5 3080 bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
28a7282a
LC
3081 }
3082}
3083
3084/* XXX: Today this is set by device models because it makes the implementation
3085 quite simple. However, the block layer knows about the error, so it's
3086 possible to implement this without device models being involved */
3087void bdrv_iostatus_set_err(BlockDriverState *bs, int error)
3088{
58e21ef5
LC
3089 if (bdrv_iostatus_is_enabled(bs) &&
3090 bs->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
28a7282a 3091 assert(error >= 0);
58e21ef5
LC
3092 bs->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
3093 BLOCK_DEVICE_IO_STATUS_FAILED;
28a7282a
LC
3094 }
3095}
3096
a597e79c
CH
3097void
3098bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, int64_t bytes,
3099 enum BlockAcctType type)
3100{
3101 assert(type < BDRV_MAX_IOTYPE);
3102
3103 cookie->bytes = bytes;
c488c7f6 3104 cookie->start_time_ns = get_clock();
a597e79c
CH
3105 cookie->type = type;
3106}
3107
3108void
3109bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie)
3110{
3111 assert(cookie->type < BDRV_MAX_IOTYPE);
3112
3113 bs->nr_bytes[cookie->type] += cookie->bytes;
3114 bs->nr_ops[cookie->type]++;
c488c7f6 3115 bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns;
a597e79c
CH
3116}
3117
f88e1a42
JS
3118int bdrv_img_create(const char *filename, const char *fmt,
3119 const char *base_filename, const char *base_fmt,
3120 char *options, uint64_t img_size, int flags)
3121{
3122 QEMUOptionParameter *param = NULL, *create_options = NULL;
d220894e 3123 QEMUOptionParameter *backing_fmt, *backing_file, *size;
f88e1a42
JS
3124 BlockDriverState *bs = NULL;
3125 BlockDriver *drv, *proto_drv;
96df67d1 3126 BlockDriver *backing_drv = NULL;
f88e1a42
JS
3127 int ret = 0;
3128
3129 /* Find driver and parse its options */
3130 drv = bdrv_find_format(fmt);
3131 if (!drv) {
3132 error_report("Unknown file format '%s'", fmt);
4f70f249 3133 ret = -EINVAL;
f88e1a42
JS
3134 goto out;
3135 }
3136
3137 proto_drv = bdrv_find_protocol(filename);
3138 if (!proto_drv) {
3139 error_report("Unknown protocol '%s'", filename);
4f70f249 3140 ret = -EINVAL;
f88e1a42
JS
3141 goto out;
3142 }
3143
3144 create_options = append_option_parameters(create_options,
3145 drv->create_options);
3146 create_options = append_option_parameters(create_options,
3147 proto_drv->create_options);
3148
3149 /* Create parameter list with default values */
3150 param = parse_option_parameters("", create_options, param);
3151
3152 set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size);
3153
3154 /* Parse -o options */
3155 if (options) {
3156 param = parse_option_parameters(options, create_options, param);
3157 if (param == NULL) {
3158 error_report("Invalid options for file format '%s'.", fmt);
4f70f249 3159 ret = -EINVAL;
f88e1a42
JS
3160 goto out;
3161 }
3162 }
3163
3164 if (base_filename) {
3165 if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE,
3166 base_filename)) {
3167 error_report("Backing file not supported for file format '%s'",
3168 fmt);
4f70f249 3169 ret = -EINVAL;
f88e1a42
JS
3170 goto out;
3171 }
3172 }
3173
3174 if (base_fmt) {
3175 if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) {
3176 error_report("Backing file format not supported for file "
3177 "format '%s'", fmt);
4f70f249 3178 ret = -EINVAL;
f88e1a42
JS
3179 goto out;
3180 }
3181 }
3182
792da93a
JS
3183 backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
3184 if (backing_file && backing_file->value.s) {
3185 if (!strcmp(filename, backing_file->value.s)) {
3186 error_report("Error: Trying to create an image with the "
3187 "same filename as the backing file");
4f70f249 3188 ret = -EINVAL;
792da93a
JS
3189 goto out;
3190 }
3191 }
3192
f88e1a42
JS
3193 backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT);
3194 if (backing_fmt && backing_fmt->value.s) {
96df67d1
SH
3195 backing_drv = bdrv_find_format(backing_fmt->value.s);
3196 if (!backing_drv) {
f88e1a42
JS
3197 error_report("Unknown backing file format '%s'",
3198 backing_fmt->value.s);
4f70f249 3199 ret = -EINVAL;
f88e1a42
JS
3200 goto out;
3201 }
3202 }
3203
3204 // The size for the image must always be specified, with one exception:
3205 // If we are using a backing file, we can obtain the size from there
d220894e
KW
3206 size = get_option_parameter(param, BLOCK_OPT_SIZE);
3207 if (size && size->value.n == -1) {
f88e1a42
JS
3208 if (backing_file && backing_file->value.s) {
3209 uint64_t size;
f88e1a42
JS
3210 char buf[32];
3211
f88e1a42
JS
3212 bs = bdrv_new("");
3213
96df67d1 3214 ret = bdrv_open(bs, backing_file->value.s, flags, backing_drv);
f88e1a42 3215 if (ret < 0) {
96df67d1 3216 error_report("Could not open '%s'", backing_file->value.s);
f88e1a42
JS
3217 goto out;
3218 }
3219 bdrv_get_geometry(bs, &size);
3220 size *= 512;
3221
3222 snprintf(buf, sizeof(buf), "%" PRId64, size);
3223 set_option_parameter(param, BLOCK_OPT_SIZE, buf);
3224 } else {
3225 error_report("Image creation needs a size parameter");
4f70f249 3226 ret = -EINVAL;
f88e1a42
JS
3227 goto out;
3228 }
3229 }
3230
3231 printf("Formatting '%s', fmt=%s ", filename, fmt);
3232 print_option_parameters(param);
3233 puts("");
3234
3235 ret = bdrv_create(drv, filename, param);
3236
3237 if (ret < 0) {
3238 if (ret == -ENOTSUP) {
3239 error_report("Formatting or formatting option not supported for "
3240 "file format '%s'", fmt);
3241 } else if (ret == -EFBIG) {
3242 error_report("The image size is too large for file format '%s'",
3243 fmt);
3244 } else {
3245 error_report("%s: error while creating %s: %s", filename, fmt,
3246 strerror(-ret));
3247 }
3248 }
3249
3250out:
3251 free_option_parameters(create_options);
3252 free_option_parameters(param);
3253
3254 if (bs) {
3255 bdrv_delete(bs);
3256 }
4f70f249
JS
3257
3258 return ret;
f88e1a42 3259}