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