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