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