block: Allow recursive "file"s
[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/monitor.h"
28 #include "block/block_int.h"
29 #include "block/blockjob.h"
30 #include "qemu/module.h"
31 #include "qapi/qmp/qjson.h"
32 #include "sysemu/sysemu.h"
33 #include "qemu/notify.h"
34 #include "block/coroutine.h"
35 #include "qmp-commands.h"
36 #include "qemu/timer.h"
37
38 #ifdef CONFIG_BSD
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <sys/ioctl.h>
42 #include <sys/queue.h>
43 #ifndef __DragonFly__
44 #include <sys/disk.h>
45 #endif
46 #endif
47
48 #ifdef _WIN32
49 #include <windows.h>
50 #endif
51
52 struct BdrvDirtyBitmap {
53 HBitmap *bitmap;
54 QLIST_ENTRY(BdrvDirtyBitmap) list;
55 };
56
57 #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
58
59 static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load);
60 static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
61 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
62 BlockDriverCompletionFunc *cb, void *opaque);
63 static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
64 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
65 BlockDriverCompletionFunc *cb, void *opaque);
66 static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs,
67 int64_t sector_num, int nb_sectors,
68 QEMUIOVector *iov);
69 static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs,
70 int64_t sector_num, int nb_sectors,
71 QEMUIOVector *iov);
72 static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs,
73 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
74 BdrvRequestFlags flags);
75 static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs,
76 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
77 BdrvRequestFlags flags);
78 static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs,
79 int64_t sector_num,
80 QEMUIOVector *qiov,
81 int nb_sectors,
82 BdrvRequestFlags flags,
83 BlockDriverCompletionFunc *cb,
84 void *opaque,
85 bool is_write);
86 static void coroutine_fn bdrv_co_do_rw(void *opaque);
87 static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs,
88 int64_t sector_num, int nb_sectors, BdrvRequestFlags flags);
89
90 static QTAILQ_HEAD(, BlockDriverState) bdrv_states =
91 QTAILQ_HEAD_INITIALIZER(bdrv_states);
92
93 static QLIST_HEAD(, BlockDriver) bdrv_drivers =
94 QLIST_HEAD_INITIALIZER(bdrv_drivers);
95
96 /* If non-zero, use only whitelisted block drivers */
97 static int use_bdrv_whitelist;
98
99 #ifdef _WIN32
100 static int is_windows_drive_prefix(const char *filename)
101 {
102 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
103 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
104 filename[1] == ':');
105 }
106
107 int is_windows_drive(const char *filename)
108 {
109 if (is_windows_drive_prefix(filename) &&
110 filename[2] == '\0')
111 return 1;
112 if (strstart(filename, "\\\\.\\", NULL) ||
113 strstart(filename, "//./", NULL))
114 return 1;
115 return 0;
116 }
117 #endif
118
119 /* throttling disk I/O limits */
120 void bdrv_set_io_limits(BlockDriverState *bs,
121 ThrottleConfig *cfg)
122 {
123 int i;
124
125 throttle_config(&bs->throttle_state, cfg);
126
127 for (i = 0; i < 2; i++) {
128 qemu_co_enter_next(&bs->throttled_reqs[i]);
129 }
130 }
131
132 /* this function drain all the throttled IOs */
133 static bool bdrv_start_throttled_reqs(BlockDriverState *bs)
134 {
135 bool drained = false;
136 bool enabled = bs->io_limits_enabled;
137 int i;
138
139 bs->io_limits_enabled = false;
140
141 for (i = 0; i < 2; i++) {
142 while (qemu_co_enter_next(&bs->throttled_reqs[i])) {
143 drained = true;
144 }
145 }
146
147 bs->io_limits_enabled = enabled;
148
149 return drained;
150 }
151
152 void bdrv_io_limits_disable(BlockDriverState *bs)
153 {
154 bs->io_limits_enabled = false;
155
156 bdrv_start_throttled_reqs(bs);
157
158 throttle_destroy(&bs->throttle_state);
159 }
160
161 static void bdrv_throttle_read_timer_cb(void *opaque)
162 {
163 BlockDriverState *bs = opaque;
164 qemu_co_enter_next(&bs->throttled_reqs[0]);
165 }
166
167 static void bdrv_throttle_write_timer_cb(void *opaque)
168 {
169 BlockDriverState *bs = opaque;
170 qemu_co_enter_next(&bs->throttled_reqs[1]);
171 }
172
173 /* should be called before bdrv_set_io_limits if a limit is set */
174 void bdrv_io_limits_enable(BlockDriverState *bs)
175 {
176 assert(!bs->io_limits_enabled);
177 throttle_init(&bs->throttle_state,
178 QEMU_CLOCK_VIRTUAL,
179 bdrv_throttle_read_timer_cb,
180 bdrv_throttle_write_timer_cb,
181 bs);
182 bs->io_limits_enabled = true;
183 }
184
185 /* This function makes an IO wait if needed
186 *
187 * @nb_sectors: the number of sectors of the IO
188 * @is_write: is the IO a write
189 */
190 static void bdrv_io_limits_intercept(BlockDriverState *bs,
191 int nb_sectors,
192 bool is_write)
193 {
194 /* does this io must wait */
195 bool must_wait = throttle_schedule_timer(&bs->throttle_state, is_write);
196
197 /* if must wait or any request of this type throttled queue the IO */
198 if (must_wait ||
199 !qemu_co_queue_empty(&bs->throttled_reqs[is_write])) {
200 qemu_co_queue_wait(&bs->throttled_reqs[is_write]);
201 }
202
203 /* the IO will be executed, do the accounting */
204 throttle_account(&bs->throttle_state,
205 is_write,
206 nb_sectors * BDRV_SECTOR_SIZE);
207
208 /* if the next request must wait -> do nothing */
209 if (throttle_schedule_timer(&bs->throttle_state, is_write)) {
210 return;
211 }
212
213 /* else queue next request for execution */
214 qemu_co_queue_next(&bs->throttled_reqs[is_write]);
215 }
216
217 /* check if the path starts with "<protocol>:" */
218 static int path_has_protocol(const char *path)
219 {
220 const char *p;
221
222 #ifdef _WIN32
223 if (is_windows_drive(path) ||
224 is_windows_drive_prefix(path)) {
225 return 0;
226 }
227 p = path + strcspn(path, ":/\\");
228 #else
229 p = path + strcspn(path, ":/");
230 #endif
231
232 return *p == ':';
233 }
234
235 int path_is_absolute(const char *path)
236 {
237 #ifdef _WIN32
238 /* specific case for names like: "\\.\d:" */
239 if (is_windows_drive(path) || is_windows_drive_prefix(path)) {
240 return 1;
241 }
242 return (*path == '/' || *path == '\\');
243 #else
244 return (*path == '/');
245 #endif
246 }
247
248 /* if filename is absolute, just copy it to dest. Otherwise, build a
249 path to it by considering it is relative to base_path. URL are
250 supported. */
251 void path_combine(char *dest, int dest_size,
252 const char *base_path,
253 const char *filename)
254 {
255 const char *p, *p1;
256 int len;
257
258 if (dest_size <= 0)
259 return;
260 if (path_is_absolute(filename)) {
261 pstrcpy(dest, dest_size, filename);
262 } else {
263 p = strchr(base_path, ':');
264 if (p)
265 p++;
266 else
267 p = base_path;
268 p1 = strrchr(base_path, '/');
269 #ifdef _WIN32
270 {
271 const char *p2;
272 p2 = strrchr(base_path, '\\');
273 if (!p1 || p2 > p1)
274 p1 = p2;
275 }
276 #endif
277 if (p1)
278 p1++;
279 else
280 p1 = base_path;
281 if (p1 > p)
282 p = p1;
283 len = p - base_path;
284 if (len > dest_size - 1)
285 len = dest_size - 1;
286 memcpy(dest, base_path, len);
287 dest[len] = '\0';
288 pstrcat(dest, dest_size, filename);
289 }
290 }
291
292 void bdrv_get_full_backing_filename(BlockDriverState *bs, char *dest, size_t sz)
293 {
294 if (bs->backing_file[0] == '\0' || path_has_protocol(bs->backing_file)) {
295 pstrcpy(dest, sz, bs->backing_file);
296 } else {
297 path_combine(dest, sz, bs->filename, bs->backing_file);
298 }
299 }
300
301 void bdrv_register(BlockDriver *bdrv)
302 {
303 /* Block drivers without coroutine functions need emulation */
304 if (!bdrv->bdrv_co_readv) {
305 bdrv->bdrv_co_readv = bdrv_co_readv_em;
306 bdrv->bdrv_co_writev = bdrv_co_writev_em;
307
308 /* bdrv_co_readv_em()/brdv_co_writev_em() work in terms of aio, so if
309 * the block driver lacks aio we need to emulate that too.
310 */
311 if (!bdrv->bdrv_aio_readv) {
312 /* add AIO emulation layer */
313 bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
314 bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
315 }
316 }
317
318 QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
319 }
320
321 /* create a new block device (by default it is empty) */
322 BlockDriverState *bdrv_new(const char *device_name)
323 {
324 BlockDriverState *bs;
325
326 bs = g_malloc0(sizeof(BlockDriverState));
327 QLIST_INIT(&bs->dirty_bitmaps);
328 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
329 if (device_name[0] != '\0') {
330 QTAILQ_INSERT_TAIL(&bdrv_states, bs, list);
331 }
332 bdrv_iostatus_disable(bs);
333 notifier_list_init(&bs->close_notifiers);
334 notifier_with_return_list_init(&bs->before_write_notifiers);
335 qemu_co_queue_init(&bs->throttled_reqs[0]);
336 qemu_co_queue_init(&bs->throttled_reqs[1]);
337 bs->refcnt = 1;
338
339 return bs;
340 }
341
342 void bdrv_add_close_notifier(BlockDriverState *bs, Notifier *notify)
343 {
344 notifier_list_add(&bs->close_notifiers, notify);
345 }
346
347 BlockDriver *bdrv_find_format(const char *format_name)
348 {
349 BlockDriver *drv1;
350 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
351 if (!strcmp(drv1->format_name, format_name)) {
352 return drv1;
353 }
354 }
355 return NULL;
356 }
357
358 static int bdrv_is_whitelisted(BlockDriver *drv, bool read_only)
359 {
360 static const char *whitelist_rw[] = {
361 CONFIG_BDRV_RW_WHITELIST
362 };
363 static const char *whitelist_ro[] = {
364 CONFIG_BDRV_RO_WHITELIST
365 };
366 const char **p;
367
368 if (!whitelist_rw[0] && !whitelist_ro[0]) {
369 return 1; /* no whitelist, anything goes */
370 }
371
372 for (p = whitelist_rw; *p; p++) {
373 if (!strcmp(drv->format_name, *p)) {
374 return 1;
375 }
376 }
377 if (read_only) {
378 for (p = whitelist_ro; *p; p++) {
379 if (!strcmp(drv->format_name, *p)) {
380 return 1;
381 }
382 }
383 }
384 return 0;
385 }
386
387 BlockDriver *bdrv_find_whitelisted_format(const char *format_name,
388 bool read_only)
389 {
390 BlockDriver *drv = bdrv_find_format(format_name);
391 return drv && bdrv_is_whitelisted(drv, read_only) ? drv : NULL;
392 }
393
394 typedef struct CreateCo {
395 BlockDriver *drv;
396 char *filename;
397 QEMUOptionParameter *options;
398 int ret;
399 Error *err;
400 } CreateCo;
401
402 static void coroutine_fn bdrv_create_co_entry(void *opaque)
403 {
404 Error *local_err = NULL;
405 int ret;
406
407 CreateCo *cco = opaque;
408 assert(cco->drv);
409
410 ret = cco->drv->bdrv_create(cco->filename, cco->options, &local_err);
411 if (error_is_set(&local_err)) {
412 error_propagate(&cco->err, local_err);
413 }
414 cco->ret = ret;
415 }
416
417 int bdrv_create(BlockDriver *drv, const char* filename,
418 QEMUOptionParameter *options, Error **errp)
419 {
420 int ret;
421
422 Coroutine *co;
423 CreateCo cco = {
424 .drv = drv,
425 .filename = g_strdup(filename),
426 .options = options,
427 .ret = NOT_DONE,
428 .err = NULL,
429 };
430
431 if (!drv->bdrv_create) {
432 error_setg(errp, "Driver '%s' does not support image creation", drv->format_name);
433 ret = -ENOTSUP;
434 goto out;
435 }
436
437 if (qemu_in_coroutine()) {
438 /* Fast-path if already in coroutine context */
439 bdrv_create_co_entry(&cco);
440 } else {
441 co = qemu_coroutine_create(bdrv_create_co_entry);
442 qemu_coroutine_enter(co, &cco);
443 while (cco.ret == NOT_DONE) {
444 qemu_aio_wait();
445 }
446 }
447
448 ret = cco.ret;
449 if (ret < 0) {
450 if (error_is_set(&cco.err)) {
451 error_propagate(errp, cco.err);
452 } else {
453 error_setg_errno(errp, -ret, "Could not create image");
454 }
455 }
456
457 out:
458 g_free(cco.filename);
459 return ret;
460 }
461
462 int bdrv_create_file(const char* filename, QEMUOptionParameter *options,
463 Error **errp)
464 {
465 BlockDriver *drv;
466 Error *local_err = NULL;
467 int ret;
468
469 drv = bdrv_find_protocol(filename, true);
470 if (drv == NULL) {
471 error_setg(errp, "Could not find protocol for file '%s'", filename);
472 return -ENOENT;
473 }
474
475 ret = bdrv_create(drv, filename, options, &local_err);
476 if (error_is_set(&local_err)) {
477 error_propagate(errp, local_err);
478 }
479 return ret;
480 }
481
482 /*
483 * Create a uniquely-named empty temporary file.
484 * Return 0 upon success, otherwise a negative errno value.
485 */
486 int get_tmp_filename(char *filename, int size)
487 {
488 #ifdef _WIN32
489 char temp_dir[MAX_PATH];
490 /* GetTempFileName requires that its output buffer (4th param)
491 have length MAX_PATH or greater. */
492 assert(size >= MAX_PATH);
493 return (GetTempPath(MAX_PATH, temp_dir)
494 && GetTempFileName(temp_dir, "qem", 0, filename)
495 ? 0 : -GetLastError());
496 #else
497 int fd;
498 const char *tmpdir;
499 tmpdir = getenv("TMPDIR");
500 if (!tmpdir)
501 tmpdir = "/tmp";
502 if (snprintf(filename, size, "%s/vl.XXXXXX", tmpdir) >= size) {
503 return -EOVERFLOW;
504 }
505 fd = mkstemp(filename);
506 if (fd < 0) {
507 return -errno;
508 }
509 if (close(fd) != 0) {
510 unlink(filename);
511 return -errno;
512 }
513 return 0;
514 #endif
515 }
516
517 /*
518 * Detect host devices. By convention, /dev/cdrom[N] is always
519 * recognized as a host CDROM.
520 */
521 static BlockDriver *find_hdev_driver(const char *filename)
522 {
523 int score_max = 0, score;
524 BlockDriver *drv = NULL, *d;
525
526 QLIST_FOREACH(d, &bdrv_drivers, list) {
527 if (d->bdrv_probe_device) {
528 score = d->bdrv_probe_device(filename);
529 if (score > score_max) {
530 score_max = score;
531 drv = d;
532 }
533 }
534 }
535
536 return drv;
537 }
538
539 BlockDriver *bdrv_find_protocol(const char *filename,
540 bool allow_protocol_prefix)
541 {
542 BlockDriver *drv1;
543 char protocol[128];
544 int len;
545 const char *p;
546
547 /* TODO Drivers without bdrv_file_open must be specified explicitly */
548
549 /*
550 * XXX(hch): we really should not let host device detection
551 * override an explicit protocol specification, but moving this
552 * later breaks access to device names with colons in them.
553 * Thanks to the brain-dead persistent naming schemes on udev-
554 * based Linux systems those actually are quite common.
555 */
556 drv1 = find_hdev_driver(filename);
557 if (drv1) {
558 return drv1;
559 }
560
561 if (!path_has_protocol(filename) || !allow_protocol_prefix) {
562 return bdrv_find_format("file");
563 }
564
565 p = strchr(filename, ':');
566 assert(p != NULL);
567 len = p - filename;
568 if (len > sizeof(protocol) - 1)
569 len = sizeof(protocol) - 1;
570 memcpy(protocol, filename, len);
571 protocol[len] = '\0';
572 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
573 if (drv1->protocol_name &&
574 !strcmp(drv1->protocol_name, protocol)) {
575 return drv1;
576 }
577 }
578 return NULL;
579 }
580
581 static int find_image_format(BlockDriverState *bs, const char *filename,
582 BlockDriver **pdrv, Error **errp)
583 {
584 int score, score_max;
585 BlockDriver *drv1, *drv;
586 uint8_t buf[2048];
587 int ret = 0;
588
589 /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
590 if (bs->sg || !bdrv_is_inserted(bs) || bdrv_getlength(bs) == 0) {
591 drv = bdrv_find_format("raw");
592 if (!drv) {
593 error_setg(errp, "Could not find raw image format");
594 ret = -ENOENT;
595 }
596 *pdrv = drv;
597 return ret;
598 }
599
600 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
601 if (ret < 0) {
602 error_setg_errno(errp, -ret, "Could not read image for determining its "
603 "format");
604 *pdrv = NULL;
605 return ret;
606 }
607
608 score_max = 0;
609 drv = NULL;
610 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
611 if (drv1->bdrv_probe) {
612 score = drv1->bdrv_probe(buf, ret, filename);
613 if (score > score_max) {
614 score_max = score;
615 drv = drv1;
616 }
617 }
618 }
619 if (!drv) {
620 error_setg(errp, "Could not determine image format: No compatible "
621 "driver found");
622 ret = -ENOENT;
623 }
624 *pdrv = drv;
625 return ret;
626 }
627
628 /**
629 * Set the current 'total_sectors' value
630 */
631 static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
632 {
633 BlockDriver *drv = bs->drv;
634
635 /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
636 if (bs->sg)
637 return 0;
638
639 /* query actual device if possible, otherwise just trust the hint */
640 if (drv->bdrv_getlength) {
641 int64_t length = drv->bdrv_getlength(bs);
642 if (length < 0) {
643 return length;
644 }
645 hint = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE);
646 }
647
648 bs->total_sectors = hint;
649 return 0;
650 }
651
652 /**
653 * Set open flags for a given discard mode
654 *
655 * Return 0 on success, -1 if the discard mode was invalid.
656 */
657 int bdrv_parse_discard_flags(const char *mode, int *flags)
658 {
659 *flags &= ~BDRV_O_UNMAP;
660
661 if (!strcmp(mode, "off") || !strcmp(mode, "ignore")) {
662 /* do nothing */
663 } else if (!strcmp(mode, "on") || !strcmp(mode, "unmap")) {
664 *flags |= BDRV_O_UNMAP;
665 } else {
666 return -1;
667 }
668
669 return 0;
670 }
671
672 /**
673 * Set open flags for a given cache mode
674 *
675 * Return 0 on success, -1 if the cache mode was invalid.
676 */
677 int bdrv_parse_cache_flags(const char *mode, int *flags)
678 {
679 *flags &= ~BDRV_O_CACHE_MASK;
680
681 if (!strcmp(mode, "off") || !strcmp(mode, "none")) {
682 *flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
683 } else if (!strcmp(mode, "directsync")) {
684 *flags |= BDRV_O_NOCACHE;
685 } else if (!strcmp(mode, "writeback")) {
686 *flags |= BDRV_O_CACHE_WB;
687 } else if (!strcmp(mode, "unsafe")) {
688 *flags |= BDRV_O_CACHE_WB;
689 *flags |= BDRV_O_NO_FLUSH;
690 } else if (!strcmp(mode, "writethrough")) {
691 /* this is the default */
692 } else {
693 return -1;
694 }
695
696 return 0;
697 }
698
699 /**
700 * The copy-on-read flag is actually a reference count so multiple users may
701 * use the feature without worrying about clobbering its previous state.
702 * Copy-on-read stays enabled until all users have called to disable it.
703 */
704 void bdrv_enable_copy_on_read(BlockDriverState *bs)
705 {
706 bs->copy_on_read++;
707 }
708
709 void bdrv_disable_copy_on_read(BlockDriverState *bs)
710 {
711 assert(bs->copy_on_read > 0);
712 bs->copy_on_read--;
713 }
714
715 static int bdrv_open_flags(BlockDriverState *bs, int flags)
716 {
717 int open_flags = flags | BDRV_O_CACHE_WB;
718
719 /*
720 * Clear flags that are internal to the block layer before opening the
721 * image.
722 */
723 open_flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
724
725 /*
726 * Snapshots should be writable.
727 */
728 if (bs->is_temporary) {
729 open_flags |= BDRV_O_RDWR;
730 }
731
732 return open_flags;
733 }
734
735 /*
736 * Common part for opening disk images and files
737 *
738 * Removes all processed options from *options.
739 */
740 static int bdrv_open_common(BlockDriverState *bs, BlockDriverState *file,
741 QDict *options, int flags, BlockDriver *drv, Error **errp)
742 {
743 int ret, open_flags;
744 const char *filename;
745 Error *local_err = NULL;
746
747 assert(drv != NULL);
748 assert(bs->file == NULL);
749 assert(options != NULL && bs->options != options);
750
751 if (file != NULL) {
752 filename = file->filename;
753 } else {
754 filename = qdict_get_try_str(options, "filename");
755 }
756
757 trace_bdrv_open_common(bs, filename ?: "", flags, drv->format_name);
758
759 /* bdrv_open() with directly using a protocol as drv. This layer is already
760 * opened, so assign it to bs (while file becomes a closed BlockDriverState)
761 * and return immediately. */
762 if (file != NULL && drv->bdrv_file_open) {
763 bdrv_swap(file, bs);
764 return 0;
765 }
766
767 bs->open_flags = flags;
768 bs->buffer_alignment = 512;
769 bs->zero_beyond_eof = true;
770 open_flags = bdrv_open_flags(bs, flags);
771 bs->read_only = !(open_flags & BDRV_O_RDWR);
772
773 if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, bs->read_only)) {
774 error_setg(errp,
775 !bs->read_only && bdrv_is_whitelisted(drv, true)
776 ? "Driver '%s' can only be used for read-only devices"
777 : "Driver '%s' is not whitelisted",
778 drv->format_name);
779 return -ENOTSUP;
780 }
781
782 assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */
783 if (flags & BDRV_O_COPY_ON_READ) {
784 if (!bs->read_only) {
785 bdrv_enable_copy_on_read(bs);
786 } else {
787 error_setg(errp, "Can't use copy-on-read on read-only device");
788 return -EINVAL;
789 }
790 }
791
792 if (filename != NULL) {
793 pstrcpy(bs->filename, sizeof(bs->filename), filename);
794 } else {
795 bs->filename[0] = '\0';
796 }
797
798 bs->drv = drv;
799 bs->opaque = g_malloc0(drv->instance_size);
800
801 bs->enable_write_cache = !!(flags & BDRV_O_CACHE_WB);
802
803 /* Open the image, either directly or using a protocol */
804 if (drv->bdrv_file_open) {
805 assert(file == NULL);
806 assert(!drv->bdrv_needs_filename || filename != NULL);
807 ret = drv->bdrv_file_open(bs, options, open_flags, &local_err);
808 } else {
809 if (file == NULL) {
810 error_setg(errp, "Can't use '%s' as a block driver for the "
811 "protocol level", drv->format_name);
812 ret = -EINVAL;
813 goto free_and_fail;
814 }
815 bs->file = file;
816 ret = drv->bdrv_open(bs, options, open_flags, &local_err);
817 }
818
819 if (ret < 0) {
820 if (error_is_set(&local_err)) {
821 error_propagate(errp, local_err);
822 } else if (bs->filename[0]) {
823 error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename);
824 } else {
825 error_setg_errno(errp, -ret, "Could not open image");
826 }
827 goto free_and_fail;
828 }
829
830 ret = refresh_total_sectors(bs, bs->total_sectors);
831 if (ret < 0) {
832 error_setg_errno(errp, -ret, "Could not refresh total sector count");
833 goto free_and_fail;
834 }
835
836 #ifndef _WIN32
837 if (bs->is_temporary) {
838 assert(bs->filename[0] != '\0');
839 unlink(bs->filename);
840 }
841 #endif
842 return 0;
843
844 free_and_fail:
845 bs->file = NULL;
846 g_free(bs->opaque);
847 bs->opaque = NULL;
848 bs->drv = NULL;
849 return ret;
850 }
851
852 /*
853 * Opens a file using a protocol (file, host_device, nbd, ...)
854 *
855 * options is a QDict of options to pass to the block drivers, or NULL for an
856 * empty set of options. The reference to the QDict belongs to the block layer
857 * after the call (even on failure), so if the caller intends to reuse the
858 * dictionary, it needs to use QINCREF() before calling bdrv_file_open.
859 */
860 int bdrv_file_open(BlockDriverState **pbs, const char *filename,
861 const char *reference, QDict *options, int flags,
862 Error **errp)
863 {
864 BlockDriverState *bs = NULL;
865 BlockDriver *drv;
866 const char *drvname;
867 bool allow_protocol_prefix = false;
868 Error *local_err = NULL;
869 int ret;
870
871 /* NULL means an empty set of options */
872 if (options == NULL) {
873 options = qdict_new();
874 }
875
876 if (reference) {
877 if (filename || qdict_size(options)) {
878 error_setg(errp, "Cannot reference an existing block device with "
879 "additional options or a new filename");
880 return -EINVAL;
881 }
882 QDECREF(options);
883
884 bs = bdrv_find(reference);
885 if (!bs) {
886 error_setg(errp, "Cannot find block device '%s'", reference);
887 return -ENODEV;
888 }
889 bdrv_ref(bs);
890 *pbs = bs;
891 return 0;
892 }
893
894 bs = bdrv_new("");
895 bs->options = options;
896 options = qdict_clone_shallow(options);
897
898 /* Fetch the file name from the options QDict if necessary */
899 if (!filename) {
900 filename = qdict_get_try_str(options, "filename");
901 } else if (filename && !qdict_haskey(options, "filename")) {
902 qdict_put(options, "filename", qstring_from_str(filename));
903 allow_protocol_prefix = true;
904 } else {
905 error_setg(errp, "Can't specify 'file' and 'filename' options at the "
906 "same time");
907 ret = -EINVAL;
908 goto fail;
909 }
910
911 /* Find the right block driver */
912 drvname = qdict_get_try_str(options, "driver");
913 if (drvname) {
914 drv = bdrv_find_format(drvname);
915 if (!drv) {
916 error_setg(errp, "Unknown driver '%s'", drvname);
917 }
918 qdict_del(options, "driver");
919 } else if (filename) {
920 drv = bdrv_find_protocol(filename, allow_protocol_prefix);
921 if (!drv) {
922 error_setg(errp, "Unknown protocol");
923 }
924 } else {
925 error_setg(errp, "Must specify either driver or file");
926 drv = NULL;
927 }
928
929 if (!drv) {
930 /* errp has been set already */
931 ret = -ENOENT;
932 goto fail;
933 }
934
935 /* Parse the filename and open it */
936 if (drv->bdrv_parse_filename && filename) {
937 drv->bdrv_parse_filename(filename, options, &local_err);
938 if (error_is_set(&local_err)) {
939 error_propagate(errp, local_err);
940 ret = -EINVAL;
941 goto fail;
942 }
943 qdict_del(options, "filename");
944 } else if (drv->bdrv_needs_filename && !filename) {
945 error_setg(errp, "The '%s' block driver requires a file name",
946 drv->format_name);
947 ret = -EINVAL;
948 goto fail;
949 }
950
951 if (!drv->bdrv_file_open) {
952 ret = bdrv_open(bs, filename, options, flags, drv, &local_err);
953 options = NULL;
954 } else {
955 ret = bdrv_open_common(bs, NULL, options, flags, drv, &local_err);
956 }
957 if (ret < 0) {
958 error_propagate(errp, local_err);
959 goto fail;
960 }
961
962 /* Check if any unknown options were used */
963 if (options && (qdict_size(options) != 0)) {
964 const QDictEntry *entry = qdict_first(options);
965 error_setg(errp, "Block protocol '%s' doesn't support the option '%s'",
966 drv->format_name, entry->key);
967 ret = -EINVAL;
968 goto fail;
969 }
970 QDECREF(options);
971
972 bs->growable = 1;
973 *pbs = bs;
974 return 0;
975
976 fail:
977 QDECREF(options);
978 if (!bs->drv) {
979 QDECREF(bs->options);
980 }
981 bdrv_unref(bs);
982 return ret;
983 }
984
985 /*
986 * Opens the backing file for a BlockDriverState if not yet open
987 *
988 * options is a QDict of options to pass to the block drivers, or NULL for an
989 * empty set of options. The reference to the QDict is transferred to this
990 * function (even on failure), so if the caller intends to reuse the dictionary,
991 * it needs to use QINCREF() before calling bdrv_file_open.
992 */
993 int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp)
994 {
995 char backing_filename[PATH_MAX];
996 int back_flags, ret;
997 BlockDriver *back_drv = NULL;
998 Error *local_err = NULL;
999
1000 if (bs->backing_hd != NULL) {
1001 QDECREF(options);
1002 return 0;
1003 }
1004
1005 /* NULL means an empty set of options */
1006 if (options == NULL) {
1007 options = qdict_new();
1008 }
1009
1010 bs->open_flags &= ~BDRV_O_NO_BACKING;
1011 if (qdict_haskey(options, "file.filename")) {
1012 backing_filename[0] = '\0';
1013 } else if (bs->backing_file[0] == '\0' && qdict_size(options) == 0) {
1014 QDECREF(options);
1015 return 0;
1016 } else {
1017 bdrv_get_full_backing_filename(bs, backing_filename,
1018 sizeof(backing_filename));
1019 }
1020
1021 bs->backing_hd = bdrv_new("");
1022
1023 if (bs->backing_format[0] != '\0') {
1024 back_drv = bdrv_find_format(bs->backing_format);
1025 }
1026
1027 /* backing files always opened read-only */
1028 back_flags = bs->open_flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT |
1029 BDRV_O_COPY_ON_READ);
1030
1031 ret = bdrv_open(bs->backing_hd,
1032 *backing_filename ? backing_filename : NULL, options,
1033 back_flags, back_drv, &local_err);
1034 if (ret < 0) {
1035 bdrv_unref(bs->backing_hd);
1036 bs->backing_hd = NULL;
1037 bs->open_flags |= BDRV_O_NO_BACKING;
1038 error_setg(errp, "Could not open backing file: %s",
1039 error_get_pretty(local_err));
1040 error_free(local_err);
1041 return ret;
1042 }
1043 pstrcpy(bs->backing_file, sizeof(bs->backing_file),
1044 bs->backing_hd->file->filename);
1045 return 0;
1046 }
1047
1048 /*
1049 * Opens a disk image whose options are given as BlockdevRef in another block
1050 * device's options.
1051 *
1052 * If force_raw is true, bdrv_file_open() will be used, thereby preventing any
1053 * image format auto-detection. If it is false and a filename is given,
1054 * bdrv_open() will be used for auto-detection.
1055 *
1056 * If allow_none is true, no image will be opened if filename is false and no
1057 * BlockdevRef is given. *pbs will remain unchanged and 0 will be returned.
1058 *
1059 * bdrev_key specifies the key for the image's BlockdevRef in the options QDict.
1060 * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict
1061 * itself, all options starting with "${bdref_key}." are considered part of the
1062 * BlockdevRef.
1063 *
1064 * The BlockdevRef will be removed from the options QDict.
1065 */
1066 int bdrv_open_image(BlockDriverState **pbs, const char *filename,
1067 QDict *options, const char *bdref_key, int flags,
1068 bool force_raw, bool allow_none, Error **errp)
1069 {
1070 QDict *image_options;
1071 int ret;
1072 char *bdref_key_dot;
1073 const char *reference;
1074
1075 bdref_key_dot = g_strdup_printf("%s.", bdref_key);
1076 qdict_extract_subqdict(options, &image_options, bdref_key_dot);
1077 g_free(bdref_key_dot);
1078
1079 reference = qdict_get_try_str(options, bdref_key);
1080 if (!filename && !reference && !qdict_size(image_options)) {
1081 if (allow_none) {
1082 ret = 0;
1083 } else {
1084 error_setg(errp, "A block device must be specified for \"%s\"",
1085 bdref_key);
1086 ret = -EINVAL;
1087 }
1088 goto done;
1089 }
1090
1091 if (filename && !force_raw) {
1092 /* If a filename is given and the block driver should be detected
1093 automatically (instead of using none), use bdrv_open() in order to do
1094 that auto-detection. */
1095 BlockDriverState *bs;
1096
1097 if (reference) {
1098 error_setg(errp, "Cannot reference an existing block device while "
1099 "giving a filename");
1100 ret = -EINVAL;
1101 goto done;
1102 }
1103
1104 bs = bdrv_new("");
1105 ret = bdrv_open(bs, filename, image_options, flags, NULL, errp);
1106 if (ret < 0) {
1107 bdrv_unref(bs);
1108 } else {
1109 *pbs = bs;
1110 }
1111 } else {
1112 ret = bdrv_file_open(pbs, filename, reference, image_options, flags,
1113 errp);
1114 }
1115
1116 done:
1117 qdict_del(options, bdref_key);
1118 return ret;
1119 }
1120
1121 /*
1122 * Opens a disk image (raw, qcow2, vmdk, ...)
1123 *
1124 * options is a QDict of options to pass to the block drivers, or NULL for an
1125 * empty set of options. The reference to the QDict belongs to the block layer
1126 * after the call (even on failure), so if the caller intends to reuse the
1127 * dictionary, it needs to use QINCREF() before calling bdrv_open.
1128 */
1129 int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
1130 int flags, BlockDriver *drv, Error **errp)
1131 {
1132 int ret;
1133 /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */
1134 char tmp_filename[PATH_MAX + 1];
1135 BlockDriverState *file = NULL;
1136 const char *drvname;
1137 Error *local_err = NULL;
1138
1139 /* NULL means an empty set of options */
1140 if (options == NULL) {
1141 options = qdict_new();
1142 }
1143
1144 bs->options = options;
1145 options = qdict_clone_shallow(options);
1146
1147 /* For snapshot=on, create a temporary qcow2 overlay */
1148 if (flags & BDRV_O_SNAPSHOT) {
1149 BlockDriverState *bs1;
1150 int64_t total_size;
1151 BlockDriver *bdrv_qcow2;
1152 QEMUOptionParameter *create_options;
1153 QDict *snapshot_options;
1154
1155 /* if snapshot, we create a temporary backing file and open it
1156 instead of opening 'filename' directly */
1157
1158 /* Get the required size from the image */
1159 bs1 = bdrv_new("");
1160 QINCREF(options);
1161 ret = bdrv_open(bs1, filename, options, BDRV_O_NO_BACKING,
1162 drv, &local_err);
1163 if (ret < 0) {
1164 bdrv_unref(bs1);
1165 goto fail;
1166 }
1167 total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK;
1168
1169 bdrv_unref(bs1);
1170
1171 /* Create the temporary image */
1172 ret = get_tmp_filename(tmp_filename, sizeof(tmp_filename));
1173 if (ret < 0) {
1174 error_setg_errno(errp, -ret, "Could not get temporary filename");
1175 goto fail;
1176 }
1177
1178 bdrv_qcow2 = bdrv_find_format("qcow2");
1179 create_options = parse_option_parameters("", bdrv_qcow2->create_options,
1180 NULL);
1181
1182 set_option_parameter_int(create_options, BLOCK_OPT_SIZE, total_size);
1183
1184 ret = bdrv_create(bdrv_qcow2, tmp_filename, create_options, &local_err);
1185 free_option_parameters(create_options);
1186 if (ret < 0) {
1187 error_setg_errno(errp, -ret, "Could not create temporary overlay "
1188 "'%s': %s", tmp_filename,
1189 error_get_pretty(local_err));
1190 error_free(local_err);
1191 local_err = NULL;
1192 goto fail;
1193 }
1194
1195 /* Prepare a new options QDict for the temporary file, where user
1196 * options refer to the backing file */
1197 if (filename) {
1198 qdict_put(options, "file.filename", qstring_from_str(filename));
1199 }
1200 if (drv) {
1201 qdict_put(options, "driver", qstring_from_str(drv->format_name));
1202 }
1203
1204 snapshot_options = qdict_new();
1205 qdict_put(snapshot_options, "backing", options);
1206 qdict_flatten(snapshot_options);
1207
1208 bs->options = snapshot_options;
1209 options = qdict_clone_shallow(bs->options);
1210
1211 filename = tmp_filename;
1212 drv = bdrv_qcow2;
1213 bs->is_temporary = 1;
1214 }
1215
1216 /* Open image file without format layer */
1217 if (flags & BDRV_O_RDWR) {
1218 flags |= BDRV_O_ALLOW_RDWR;
1219 }
1220
1221 ret = bdrv_open_image(&file, filename, options, "file",
1222 bdrv_open_flags(bs, flags | BDRV_O_UNMAP), true, true,
1223 &local_err);
1224 if (ret < 0) {
1225 goto fail;
1226 }
1227
1228 /* Find the right image format driver */
1229 drvname = qdict_get_try_str(options, "driver");
1230 if (drvname) {
1231 drv = bdrv_find_format(drvname);
1232 qdict_del(options, "driver");
1233 if (!drv) {
1234 error_setg(errp, "Invalid driver: '%s'", drvname);
1235 ret = -EINVAL;
1236 goto unlink_and_fail;
1237 }
1238 }
1239
1240 if (!drv) {
1241 if (file) {
1242 ret = find_image_format(file, filename, &drv, &local_err);
1243 } else {
1244 error_setg(errp, "Must specify either driver or file");
1245 ret = -EINVAL;
1246 goto unlink_and_fail;
1247 }
1248 }
1249
1250 if (!drv) {
1251 goto unlink_and_fail;
1252 }
1253
1254 /* Open the image */
1255 ret = bdrv_open_common(bs, file, options, flags, drv, &local_err);
1256 if (ret < 0) {
1257 goto unlink_and_fail;
1258 }
1259
1260 if (file && (bs->file != file)) {
1261 bdrv_unref(file);
1262 file = NULL;
1263 }
1264
1265 /* If there is a backing file, use it */
1266 if ((flags & BDRV_O_NO_BACKING) == 0) {
1267 QDict *backing_options;
1268
1269 qdict_extract_subqdict(options, &backing_options, "backing.");
1270 ret = bdrv_open_backing_file(bs, backing_options, &local_err);
1271 if (ret < 0) {
1272 goto close_and_fail;
1273 }
1274 }
1275
1276 /* Check if any unknown options were used */
1277 if (qdict_size(options) != 0) {
1278 const QDictEntry *entry = qdict_first(options);
1279 error_setg(errp, "Block format '%s' used by device '%s' doesn't "
1280 "support the option '%s'", drv->format_name, bs->device_name,
1281 entry->key);
1282
1283 ret = -EINVAL;
1284 goto close_and_fail;
1285 }
1286 QDECREF(options);
1287
1288 if (!bdrv_key_required(bs)) {
1289 bdrv_dev_change_media_cb(bs, true);
1290 }
1291
1292 return 0;
1293
1294 unlink_and_fail:
1295 if (file != NULL) {
1296 bdrv_unref(file);
1297 }
1298 if (bs->is_temporary) {
1299 unlink(filename);
1300 }
1301 fail:
1302 QDECREF(bs->options);
1303 QDECREF(options);
1304 bs->options = NULL;
1305 if (error_is_set(&local_err)) {
1306 error_propagate(errp, local_err);
1307 }
1308 return ret;
1309
1310 close_and_fail:
1311 bdrv_close(bs);
1312 QDECREF(options);
1313 if (error_is_set(&local_err)) {
1314 error_propagate(errp, local_err);
1315 }
1316 return ret;
1317 }
1318
1319 typedef struct BlockReopenQueueEntry {
1320 bool prepared;
1321 BDRVReopenState state;
1322 QSIMPLEQ_ENTRY(BlockReopenQueueEntry) entry;
1323 } BlockReopenQueueEntry;
1324
1325 /*
1326 * Adds a BlockDriverState to a simple queue for an atomic, transactional
1327 * reopen of multiple devices.
1328 *
1329 * bs_queue can either be an existing BlockReopenQueue that has had QSIMPLE_INIT
1330 * already performed, or alternatively may be NULL a new BlockReopenQueue will
1331 * be created and initialized. This newly created BlockReopenQueue should be
1332 * passed back in for subsequent calls that are intended to be of the same
1333 * atomic 'set'.
1334 *
1335 * bs is the BlockDriverState to add to the reopen queue.
1336 *
1337 * flags contains the open flags for the associated bs
1338 *
1339 * returns a pointer to bs_queue, which is either the newly allocated
1340 * bs_queue, or the existing bs_queue being used.
1341 *
1342 */
1343 BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue,
1344 BlockDriverState *bs, int flags)
1345 {
1346 assert(bs != NULL);
1347
1348 BlockReopenQueueEntry *bs_entry;
1349 if (bs_queue == NULL) {
1350 bs_queue = g_new0(BlockReopenQueue, 1);
1351 QSIMPLEQ_INIT(bs_queue);
1352 }
1353
1354 if (bs->file) {
1355 bdrv_reopen_queue(bs_queue, bs->file, flags);
1356 }
1357
1358 bs_entry = g_new0(BlockReopenQueueEntry, 1);
1359 QSIMPLEQ_INSERT_TAIL(bs_queue, bs_entry, entry);
1360
1361 bs_entry->state.bs = bs;
1362 bs_entry->state.flags = flags;
1363
1364 return bs_queue;
1365 }
1366
1367 /*
1368 * Reopen multiple BlockDriverStates atomically & transactionally.
1369 *
1370 * The queue passed in (bs_queue) must have been built up previous
1371 * via bdrv_reopen_queue().
1372 *
1373 * Reopens all BDS specified in the queue, with the appropriate
1374 * flags. All devices are prepared for reopen, and failure of any
1375 * device will cause all device changes to be abandonded, and intermediate
1376 * data cleaned up.
1377 *
1378 * If all devices prepare successfully, then the changes are committed
1379 * to all devices.
1380 *
1381 */
1382 int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp)
1383 {
1384 int ret = -1;
1385 BlockReopenQueueEntry *bs_entry, *next;
1386 Error *local_err = NULL;
1387
1388 assert(bs_queue != NULL);
1389
1390 bdrv_drain_all();
1391
1392 QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) {
1393 if (bdrv_reopen_prepare(&bs_entry->state, bs_queue, &local_err)) {
1394 error_propagate(errp, local_err);
1395 goto cleanup;
1396 }
1397 bs_entry->prepared = true;
1398 }
1399
1400 /* If we reach this point, we have success and just need to apply the
1401 * changes
1402 */
1403 QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) {
1404 bdrv_reopen_commit(&bs_entry->state);
1405 }
1406
1407 ret = 0;
1408
1409 cleanup:
1410 QSIMPLEQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) {
1411 if (ret && bs_entry->prepared) {
1412 bdrv_reopen_abort(&bs_entry->state);
1413 }
1414 g_free(bs_entry);
1415 }
1416 g_free(bs_queue);
1417 return ret;
1418 }
1419
1420
1421 /* Reopen a single BlockDriverState with the specified flags. */
1422 int bdrv_reopen(BlockDriverState *bs, int bdrv_flags, Error **errp)
1423 {
1424 int ret = -1;
1425 Error *local_err = NULL;
1426 BlockReopenQueue *queue = bdrv_reopen_queue(NULL, bs, bdrv_flags);
1427
1428 ret = bdrv_reopen_multiple(queue, &local_err);
1429 if (local_err != NULL) {
1430 error_propagate(errp, local_err);
1431 }
1432 return ret;
1433 }
1434
1435
1436 /*
1437 * Prepares a BlockDriverState for reopen. All changes are staged in the
1438 * 'opaque' field of the BDRVReopenState, which is used and allocated by
1439 * the block driver layer .bdrv_reopen_prepare()
1440 *
1441 * bs is the BlockDriverState to reopen
1442 * flags are the new open flags
1443 * queue is the reopen queue
1444 *
1445 * Returns 0 on success, non-zero on error. On error errp will be set
1446 * as well.
1447 *
1448 * On failure, bdrv_reopen_abort() will be called to clean up any data.
1449 * It is the responsibility of the caller to then call the abort() or
1450 * commit() for any other BDS that have been left in a prepare() state
1451 *
1452 */
1453 int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue,
1454 Error **errp)
1455 {
1456 int ret = -1;
1457 Error *local_err = NULL;
1458 BlockDriver *drv;
1459
1460 assert(reopen_state != NULL);
1461 assert(reopen_state->bs->drv != NULL);
1462 drv = reopen_state->bs->drv;
1463
1464 /* if we are to stay read-only, do not allow permission change
1465 * to r/w */
1466 if (!(reopen_state->bs->open_flags & BDRV_O_ALLOW_RDWR) &&
1467 reopen_state->flags & BDRV_O_RDWR) {
1468 error_set(errp, QERR_DEVICE_IS_READ_ONLY,
1469 reopen_state->bs->device_name);
1470 goto error;
1471 }
1472
1473
1474 ret = bdrv_flush(reopen_state->bs);
1475 if (ret) {
1476 error_set(errp, ERROR_CLASS_GENERIC_ERROR, "Error (%s) flushing drive",
1477 strerror(-ret));
1478 goto error;
1479 }
1480
1481 if (drv->bdrv_reopen_prepare) {
1482 ret = drv->bdrv_reopen_prepare(reopen_state, queue, &local_err);
1483 if (ret) {
1484 if (local_err != NULL) {
1485 error_propagate(errp, local_err);
1486 } else {
1487 error_setg(errp, "failed while preparing to reopen image '%s'",
1488 reopen_state->bs->filename);
1489 }
1490 goto error;
1491 }
1492 } else {
1493 /* It is currently mandatory to have a bdrv_reopen_prepare()
1494 * handler for each supported drv. */
1495 error_set(errp, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
1496 drv->format_name, reopen_state->bs->device_name,
1497 "reopening of file");
1498 ret = -1;
1499 goto error;
1500 }
1501
1502 ret = 0;
1503
1504 error:
1505 return ret;
1506 }
1507
1508 /*
1509 * Takes the staged changes for the reopen from bdrv_reopen_prepare(), and
1510 * makes them final by swapping the staging BlockDriverState contents into
1511 * the active BlockDriverState contents.
1512 */
1513 void bdrv_reopen_commit(BDRVReopenState *reopen_state)
1514 {
1515 BlockDriver *drv;
1516
1517 assert(reopen_state != NULL);
1518 drv = reopen_state->bs->drv;
1519 assert(drv != NULL);
1520
1521 /* If there are any driver level actions to take */
1522 if (drv->bdrv_reopen_commit) {
1523 drv->bdrv_reopen_commit(reopen_state);
1524 }
1525
1526 /* set BDS specific flags now */
1527 reopen_state->bs->open_flags = reopen_state->flags;
1528 reopen_state->bs->enable_write_cache = !!(reopen_state->flags &
1529 BDRV_O_CACHE_WB);
1530 reopen_state->bs->read_only = !(reopen_state->flags & BDRV_O_RDWR);
1531 }
1532
1533 /*
1534 * Abort the reopen, and delete and free the staged changes in
1535 * reopen_state
1536 */
1537 void bdrv_reopen_abort(BDRVReopenState *reopen_state)
1538 {
1539 BlockDriver *drv;
1540
1541 assert(reopen_state != NULL);
1542 drv = reopen_state->bs->drv;
1543 assert(drv != NULL);
1544
1545 if (drv->bdrv_reopen_abort) {
1546 drv->bdrv_reopen_abort(reopen_state);
1547 }
1548 }
1549
1550
1551 void bdrv_close(BlockDriverState *bs)
1552 {
1553 if (bs->job) {
1554 block_job_cancel_sync(bs->job);
1555 }
1556 bdrv_drain_all(); /* complete I/O */
1557 bdrv_flush(bs);
1558 bdrv_drain_all(); /* in case flush left pending I/O */
1559 notifier_list_notify(&bs->close_notifiers, bs);
1560
1561 if (bs->drv) {
1562 if (bs->backing_hd) {
1563 bdrv_unref(bs->backing_hd);
1564 bs->backing_hd = NULL;
1565 }
1566 bs->drv->bdrv_close(bs);
1567 g_free(bs->opaque);
1568 #ifdef _WIN32
1569 if (bs->is_temporary) {
1570 unlink(bs->filename);
1571 }
1572 #endif
1573 bs->opaque = NULL;
1574 bs->drv = NULL;
1575 bs->copy_on_read = 0;
1576 bs->backing_file[0] = '\0';
1577 bs->backing_format[0] = '\0';
1578 bs->total_sectors = 0;
1579 bs->encrypted = 0;
1580 bs->valid_key = 0;
1581 bs->sg = 0;
1582 bs->growable = 0;
1583 bs->zero_beyond_eof = false;
1584 QDECREF(bs->options);
1585 bs->options = NULL;
1586
1587 if (bs->file != NULL) {
1588 bdrv_unref(bs->file);
1589 bs->file = NULL;
1590 }
1591 }
1592
1593 bdrv_dev_change_media_cb(bs, false);
1594
1595 /*throttling disk I/O limits*/
1596 if (bs->io_limits_enabled) {
1597 bdrv_io_limits_disable(bs);
1598 }
1599 }
1600
1601 void bdrv_close_all(void)
1602 {
1603 BlockDriverState *bs;
1604
1605 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1606 bdrv_close(bs);
1607 }
1608 }
1609
1610 /* Check if any requests are in-flight (including throttled requests) */
1611 static bool bdrv_requests_pending(BlockDriverState *bs)
1612 {
1613 if (!QLIST_EMPTY(&bs->tracked_requests)) {
1614 return true;
1615 }
1616 if (!qemu_co_queue_empty(&bs->throttled_reqs[0])) {
1617 return true;
1618 }
1619 if (!qemu_co_queue_empty(&bs->throttled_reqs[1])) {
1620 return true;
1621 }
1622 if (bs->file && bdrv_requests_pending(bs->file)) {
1623 return true;
1624 }
1625 if (bs->backing_hd && bdrv_requests_pending(bs->backing_hd)) {
1626 return true;
1627 }
1628 return false;
1629 }
1630
1631 static bool bdrv_requests_pending_all(void)
1632 {
1633 BlockDriverState *bs;
1634 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1635 if (bdrv_requests_pending(bs)) {
1636 return true;
1637 }
1638 }
1639 return false;
1640 }
1641
1642 /*
1643 * Wait for pending requests to complete across all BlockDriverStates
1644 *
1645 * This function does not flush data to disk, use bdrv_flush_all() for that
1646 * after calling this function.
1647 *
1648 * Note that completion of an asynchronous I/O operation can trigger any
1649 * number of other I/O operations on other devices---for example a coroutine
1650 * can be arbitrarily complex and a constant flow of I/O can come until the
1651 * coroutine is complete. Because of this, it is not possible to have a
1652 * function to drain a single device's I/O queue.
1653 */
1654 void bdrv_drain_all(void)
1655 {
1656 /* Always run first iteration so any pending completion BHs run */
1657 bool busy = true;
1658 BlockDriverState *bs;
1659
1660 while (busy) {
1661 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1662 bdrv_start_throttled_reqs(bs);
1663 }
1664
1665 busy = bdrv_requests_pending_all();
1666 busy |= aio_poll(qemu_get_aio_context(), busy);
1667 }
1668 }
1669
1670 /* make a BlockDriverState anonymous by removing from bdrv_state list.
1671 Also, NULL terminate the device_name to prevent double remove */
1672 void bdrv_make_anon(BlockDriverState *bs)
1673 {
1674 if (bs->device_name[0] != '\0') {
1675 QTAILQ_REMOVE(&bdrv_states, bs, list);
1676 }
1677 bs->device_name[0] = '\0';
1678 }
1679
1680 static void bdrv_rebind(BlockDriverState *bs)
1681 {
1682 if (bs->drv && bs->drv->bdrv_rebind) {
1683 bs->drv->bdrv_rebind(bs);
1684 }
1685 }
1686
1687 static void bdrv_move_feature_fields(BlockDriverState *bs_dest,
1688 BlockDriverState *bs_src)
1689 {
1690 /* move some fields that need to stay attached to the device */
1691 bs_dest->open_flags = bs_src->open_flags;
1692
1693 /* dev info */
1694 bs_dest->dev_ops = bs_src->dev_ops;
1695 bs_dest->dev_opaque = bs_src->dev_opaque;
1696 bs_dest->dev = bs_src->dev;
1697 bs_dest->buffer_alignment = bs_src->buffer_alignment;
1698 bs_dest->copy_on_read = bs_src->copy_on_read;
1699
1700 bs_dest->enable_write_cache = bs_src->enable_write_cache;
1701
1702 /* i/o throttled req */
1703 memcpy(&bs_dest->throttle_state,
1704 &bs_src->throttle_state,
1705 sizeof(ThrottleState));
1706 bs_dest->throttled_reqs[0] = bs_src->throttled_reqs[0];
1707 bs_dest->throttled_reqs[1] = bs_src->throttled_reqs[1];
1708 bs_dest->io_limits_enabled = bs_src->io_limits_enabled;
1709
1710 /* r/w error */
1711 bs_dest->on_read_error = bs_src->on_read_error;
1712 bs_dest->on_write_error = bs_src->on_write_error;
1713
1714 /* i/o status */
1715 bs_dest->iostatus_enabled = bs_src->iostatus_enabled;
1716 bs_dest->iostatus = bs_src->iostatus;
1717
1718 /* dirty bitmap */
1719 bs_dest->dirty_bitmaps = bs_src->dirty_bitmaps;
1720
1721 /* reference count */
1722 bs_dest->refcnt = bs_src->refcnt;
1723
1724 /* job */
1725 bs_dest->in_use = bs_src->in_use;
1726 bs_dest->job = bs_src->job;
1727
1728 /* keep the same entry in bdrv_states */
1729 pstrcpy(bs_dest->device_name, sizeof(bs_dest->device_name),
1730 bs_src->device_name);
1731 bs_dest->list = bs_src->list;
1732 }
1733
1734 /*
1735 * Swap bs contents for two image chains while they are live,
1736 * while keeping required fields on the BlockDriverState that is
1737 * actually attached to a device.
1738 *
1739 * This will modify the BlockDriverState fields, and swap contents
1740 * between bs_new and bs_old. Both bs_new and bs_old are modified.
1741 *
1742 * bs_new is required to be anonymous.
1743 *
1744 * This function does not create any image files.
1745 */
1746 void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old)
1747 {
1748 BlockDriverState tmp;
1749
1750 /* bs_new must be anonymous and shouldn't have anything fancy enabled */
1751 assert(bs_new->device_name[0] == '\0');
1752 assert(QLIST_EMPTY(&bs_new->dirty_bitmaps));
1753 assert(bs_new->job == NULL);
1754 assert(bs_new->dev == NULL);
1755 assert(bs_new->in_use == 0);
1756 assert(bs_new->io_limits_enabled == false);
1757 assert(!throttle_have_timer(&bs_new->throttle_state));
1758
1759 tmp = *bs_new;
1760 *bs_new = *bs_old;
1761 *bs_old = tmp;
1762
1763 /* there are some fields that should not be swapped, move them back */
1764 bdrv_move_feature_fields(&tmp, bs_old);
1765 bdrv_move_feature_fields(bs_old, bs_new);
1766 bdrv_move_feature_fields(bs_new, &tmp);
1767
1768 /* bs_new shouldn't be in bdrv_states even after the swap! */
1769 assert(bs_new->device_name[0] == '\0');
1770
1771 /* Check a few fields that should remain attached to the device */
1772 assert(bs_new->dev == NULL);
1773 assert(bs_new->job == NULL);
1774 assert(bs_new->in_use == 0);
1775 assert(bs_new->io_limits_enabled == false);
1776 assert(!throttle_have_timer(&bs_new->throttle_state));
1777
1778 bdrv_rebind(bs_new);
1779 bdrv_rebind(bs_old);
1780 }
1781
1782 /*
1783 * Add new bs contents at the top of an image chain while the chain is
1784 * live, while keeping required fields on the top layer.
1785 *
1786 * This will modify the BlockDriverState fields, and swap contents
1787 * between bs_new and bs_top. Both bs_new and bs_top are modified.
1788 *
1789 * bs_new is required to be anonymous.
1790 *
1791 * This function does not create any image files.
1792 */
1793 void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top)
1794 {
1795 bdrv_swap(bs_new, bs_top);
1796
1797 /* The contents of 'tmp' will become bs_top, as we are
1798 * swapping bs_new and bs_top contents. */
1799 bs_top->backing_hd = bs_new;
1800 bs_top->open_flags &= ~BDRV_O_NO_BACKING;
1801 pstrcpy(bs_top->backing_file, sizeof(bs_top->backing_file),
1802 bs_new->filename);
1803 pstrcpy(bs_top->backing_format, sizeof(bs_top->backing_format),
1804 bs_new->drv ? bs_new->drv->format_name : "");
1805 }
1806
1807 static void bdrv_delete(BlockDriverState *bs)
1808 {
1809 assert(!bs->dev);
1810 assert(!bs->job);
1811 assert(!bs->in_use);
1812 assert(!bs->refcnt);
1813 assert(QLIST_EMPTY(&bs->dirty_bitmaps));
1814
1815 bdrv_close(bs);
1816
1817 /* remove from list, if necessary */
1818 bdrv_make_anon(bs);
1819
1820 g_free(bs);
1821 }
1822
1823 int bdrv_attach_dev(BlockDriverState *bs, void *dev)
1824 /* TODO change to DeviceState *dev when all users are qdevified */
1825 {
1826 if (bs->dev) {
1827 return -EBUSY;
1828 }
1829 bs->dev = dev;
1830 bdrv_iostatus_reset(bs);
1831 return 0;
1832 }
1833
1834 /* TODO qdevified devices don't use this, remove when devices are qdevified */
1835 void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev)
1836 {
1837 if (bdrv_attach_dev(bs, dev) < 0) {
1838 abort();
1839 }
1840 }
1841
1842 void bdrv_detach_dev(BlockDriverState *bs, void *dev)
1843 /* TODO change to DeviceState *dev when all users are qdevified */
1844 {
1845 assert(bs->dev == dev);
1846 bs->dev = NULL;
1847 bs->dev_ops = NULL;
1848 bs->dev_opaque = NULL;
1849 bs->buffer_alignment = 512;
1850 }
1851
1852 /* TODO change to return DeviceState * when all users are qdevified */
1853 void *bdrv_get_attached_dev(BlockDriverState *bs)
1854 {
1855 return bs->dev;
1856 }
1857
1858 void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops,
1859 void *opaque)
1860 {
1861 bs->dev_ops = ops;
1862 bs->dev_opaque = opaque;
1863 }
1864
1865 void bdrv_emit_qmp_error_event(const BlockDriverState *bdrv,
1866 enum MonitorEvent ev,
1867 BlockErrorAction action, bool is_read)
1868 {
1869 QObject *data;
1870 const char *action_str;
1871
1872 switch (action) {
1873 case BDRV_ACTION_REPORT:
1874 action_str = "report";
1875 break;
1876 case BDRV_ACTION_IGNORE:
1877 action_str = "ignore";
1878 break;
1879 case BDRV_ACTION_STOP:
1880 action_str = "stop";
1881 break;
1882 default:
1883 abort();
1884 }
1885
1886 data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
1887 bdrv->device_name,
1888 action_str,
1889 is_read ? "read" : "write");
1890 monitor_protocol_event(ev, data);
1891
1892 qobject_decref(data);
1893 }
1894
1895 static void bdrv_emit_qmp_eject_event(BlockDriverState *bs, bool ejected)
1896 {
1897 QObject *data;
1898
1899 data = qobject_from_jsonf("{ 'device': %s, 'tray-open': %i }",
1900 bdrv_get_device_name(bs), ejected);
1901 monitor_protocol_event(QEVENT_DEVICE_TRAY_MOVED, data);
1902
1903 qobject_decref(data);
1904 }
1905
1906 static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load)
1907 {
1908 if (bs->dev_ops && bs->dev_ops->change_media_cb) {
1909 bool tray_was_closed = !bdrv_dev_is_tray_open(bs);
1910 bs->dev_ops->change_media_cb(bs->dev_opaque, load);
1911 if (tray_was_closed) {
1912 /* tray open */
1913 bdrv_emit_qmp_eject_event(bs, true);
1914 }
1915 if (load) {
1916 /* tray close */
1917 bdrv_emit_qmp_eject_event(bs, false);
1918 }
1919 }
1920 }
1921
1922 bool bdrv_dev_has_removable_media(BlockDriverState *bs)
1923 {
1924 return !bs->dev || (bs->dev_ops && bs->dev_ops->change_media_cb);
1925 }
1926
1927 void bdrv_dev_eject_request(BlockDriverState *bs, bool force)
1928 {
1929 if (bs->dev_ops && bs->dev_ops->eject_request_cb) {
1930 bs->dev_ops->eject_request_cb(bs->dev_opaque, force);
1931 }
1932 }
1933
1934 bool bdrv_dev_is_tray_open(BlockDriverState *bs)
1935 {
1936 if (bs->dev_ops && bs->dev_ops->is_tray_open) {
1937 return bs->dev_ops->is_tray_open(bs->dev_opaque);
1938 }
1939 return false;
1940 }
1941
1942 static void bdrv_dev_resize_cb(BlockDriverState *bs)
1943 {
1944 if (bs->dev_ops && bs->dev_ops->resize_cb) {
1945 bs->dev_ops->resize_cb(bs->dev_opaque);
1946 }
1947 }
1948
1949 bool bdrv_dev_is_medium_locked(BlockDriverState *bs)
1950 {
1951 if (bs->dev_ops && bs->dev_ops->is_medium_locked) {
1952 return bs->dev_ops->is_medium_locked(bs->dev_opaque);
1953 }
1954 return false;
1955 }
1956
1957 /*
1958 * Run consistency checks on an image
1959 *
1960 * Returns 0 if the check could be completed (it doesn't mean that the image is
1961 * free of errors) or -errno when an internal error occurred. The results of the
1962 * check are stored in res.
1963 */
1964 int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix)
1965 {
1966 if (bs->drv->bdrv_check == NULL) {
1967 return -ENOTSUP;
1968 }
1969
1970 memset(res, 0, sizeof(*res));
1971 return bs->drv->bdrv_check(bs, res, fix);
1972 }
1973
1974 #define COMMIT_BUF_SECTORS 2048
1975
1976 /* commit COW file into the raw image */
1977 int bdrv_commit(BlockDriverState *bs)
1978 {
1979 BlockDriver *drv = bs->drv;
1980 int64_t sector, total_sectors;
1981 int n, ro, open_flags;
1982 int ret = 0;
1983 uint8_t *buf;
1984 char filename[PATH_MAX];
1985
1986 if (!drv)
1987 return -ENOMEDIUM;
1988
1989 if (!bs->backing_hd) {
1990 return -ENOTSUP;
1991 }
1992
1993 if (bdrv_in_use(bs) || bdrv_in_use(bs->backing_hd)) {
1994 return -EBUSY;
1995 }
1996
1997 ro = bs->backing_hd->read_only;
1998 /* Use pstrcpy (not strncpy): filename must be NUL-terminated. */
1999 pstrcpy(filename, sizeof(filename), bs->backing_hd->filename);
2000 open_flags = bs->backing_hd->open_flags;
2001
2002 if (ro) {
2003 if (bdrv_reopen(bs->backing_hd, open_flags | BDRV_O_RDWR, NULL)) {
2004 return -EACCES;
2005 }
2006 }
2007
2008 total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
2009 buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE);
2010
2011 for (sector = 0; sector < total_sectors; sector += n) {
2012 ret = bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n);
2013 if (ret < 0) {
2014 goto ro_cleanup;
2015 }
2016 if (ret) {
2017 if (bdrv_read(bs, sector, buf, n) != 0) {
2018 ret = -EIO;
2019 goto ro_cleanup;
2020 }
2021
2022 if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) {
2023 ret = -EIO;
2024 goto ro_cleanup;
2025 }
2026 }
2027 }
2028
2029 if (drv->bdrv_make_empty) {
2030 ret = drv->bdrv_make_empty(bs);
2031 bdrv_flush(bs);
2032 }
2033
2034 /*
2035 * Make sure all data we wrote to the backing device is actually
2036 * stable on disk.
2037 */
2038 if (bs->backing_hd)
2039 bdrv_flush(bs->backing_hd);
2040
2041 ro_cleanup:
2042 g_free(buf);
2043
2044 if (ro) {
2045 /* ignoring error return here */
2046 bdrv_reopen(bs->backing_hd, open_flags & ~BDRV_O_RDWR, NULL);
2047 }
2048
2049 return ret;
2050 }
2051
2052 int bdrv_commit_all(void)
2053 {
2054 BlockDriverState *bs;
2055
2056 QTAILQ_FOREACH(bs, &bdrv_states, list) {
2057 if (bs->drv && bs->backing_hd) {
2058 int ret = bdrv_commit(bs);
2059 if (ret < 0) {
2060 return ret;
2061 }
2062 }
2063 }
2064 return 0;
2065 }
2066
2067 /**
2068 * Remove an active request from the tracked requests list
2069 *
2070 * This function should be called when a tracked request is completing.
2071 */
2072 static void tracked_request_end(BdrvTrackedRequest *req)
2073 {
2074 QLIST_REMOVE(req, list);
2075 qemu_co_queue_restart_all(&req->wait_queue);
2076 }
2077
2078 /**
2079 * Add an active request to the tracked requests list
2080 */
2081 static void tracked_request_begin(BdrvTrackedRequest *req,
2082 BlockDriverState *bs,
2083 int64_t sector_num,
2084 int nb_sectors, bool is_write)
2085 {
2086 *req = (BdrvTrackedRequest){
2087 .bs = bs,
2088 .sector_num = sector_num,
2089 .nb_sectors = nb_sectors,
2090 .is_write = is_write,
2091 .co = qemu_coroutine_self(),
2092 };
2093
2094 qemu_co_queue_init(&req->wait_queue);
2095
2096 QLIST_INSERT_HEAD(&bs->tracked_requests, req, list);
2097 }
2098
2099 /**
2100 * Round a region to cluster boundaries
2101 */
2102 void bdrv_round_to_clusters(BlockDriverState *bs,
2103 int64_t sector_num, int nb_sectors,
2104 int64_t *cluster_sector_num,
2105 int *cluster_nb_sectors)
2106 {
2107 BlockDriverInfo bdi;
2108
2109 if (bdrv_get_info(bs, &bdi) < 0 || bdi.cluster_size == 0) {
2110 *cluster_sector_num = sector_num;
2111 *cluster_nb_sectors = nb_sectors;
2112 } else {
2113 int64_t c = bdi.cluster_size / BDRV_SECTOR_SIZE;
2114 *cluster_sector_num = QEMU_ALIGN_DOWN(sector_num, c);
2115 *cluster_nb_sectors = QEMU_ALIGN_UP(sector_num - *cluster_sector_num +
2116 nb_sectors, c);
2117 }
2118 }
2119
2120 static bool tracked_request_overlaps(BdrvTrackedRequest *req,
2121 int64_t sector_num, int nb_sectors) {
2122 /* aaaa bbbb */
2123 if (sector_num >= req->sector_num + req->nb_sectors) {
2124 return false;
2125 }
2126 /* bbbb aaaa */
2127 if (req->sector_num >= sector_num + nb_sectors) {
2128 return false;
2129 }
2130 return true;
2131 }
2132
2133 static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs,
2134 int64_t sector_num, int nb_sectors)
2135 {
2136 BdrvTrackedRequest *req;
2137 int64_t cluster_sector_num;
2138 int cluster_nb_sectors;
2139 bool retry;
2140
2141 /* If we touch the same cluster it counts as an overlap. This guarantees
2142 * that allocating writes will be serialized and not race with each other
2143 * for the same cluster. For example, in copy-on-read it ensures that the
2144 * CoR read and write operations are atomic and guest writes cannot
2145 * interleave between them.
2146 */
2147 bdrv_round_to_clusters(bs, sector_num, nb_sectors,
2148 &cluster_sector_num, &cluster_nb_sectors);
2149
2150 do {
2151 retry = false;
2152 QLIST_FOREACH(req, &bs->tracked_requests, list) {
2153 if (tracked_request_overlaps(req, cluster_sector_num,
2154 cluster_nb_sectors)) {
2155 /* Hitting this means there was a reentrant request, for
2156 * example, a block driver issuing nested requests. This must
2157 * never happen since it means deadlock.
2158 */
2159 assert(qemu_coroutine_self() != req->co);
2160
2161 qemu_co_queue_wait(&req->wait_queue);
2162 retry = true;
2163 break;
2164 }
2165 }
2166 } while (retry);
2167 }
2168
2169 /*
2170 * Return values:
2171 * 0 - success
2172 * -EINVAL - backing format specified, but no file
2173 * -ENOSPC - can't update the backing file because no space is left in the
2174 * image file header
2175 * -ENOTSUP - format driver doesn't support changing the backing file
2176 */
2177 int bdrv_change_backing_file(BlockDriverState *bs,
2178 const char *backing_file, const char *backing_fmt)
2179 {
2180 BlockDriver *drv = bs->drv;
2181 int ret;
2182
2183 /* Backing file format doesn't make sense without a backing file */
2184 if (backing_fmt && !backing_file) {
2185 return -EINVAL;
2186 }
2187
2188 if (drv->bdrv_change_backing_file != NULL) {
2189 ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
2190 } else {
2191 ret = -ENOTSUP;
2192 }
2193
2194 if (ret == 0) {
2195 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
2196 pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
2197 }
2198 return ret;
2199 }
2200
2201 /*
2202 * Finds the image layer in the chain that has 'bs' as its backing file.
2203 *
2204 * active is the current topmost image.
2205 *
2206 * Returns NULL if bs is not found in active's image chain,
2207 * or if active == bs.
2208 */
2209 BlockDriverState *bdrv_find_overlay(BlockDriverState *active,
2210 BlockDriverState *bs)
2211 {
2212 BlockDriverState *overlay = NULL;
2213 BlockDriverState *intermediate;
2214
2215 assert(active != NULL);
2216 assert(bs != NULL);
2217
2218 /* if bs is the same as active, then by definition it has no overlay
2219 */
2220 if (active == bs) {
2221 return NULL;
2222 }
2223
2224 intermediate = active;
2225 while (intermediate->backing_hd) {
2226 if (intermediate->backing_hd == bs) {
2227 overlay = intermediate;
2228 break;
2229 }
2230 intermediate = intermediate->backing_hd;
2231 }
2232
2233 return overlay;
2234 }
2235
2236 typedef struct BlkIntermediateStates {
2237 BlockDriverState *bs;
2238 QSIMPLEQ_ENTRY(BlkIntermediateStates) entry;
2239 } BlkIntermediateStates;
2240
2241
2242 /*
2243 * Drops images above 'base' up to and including 'top', and sets the image
2244 * above 'top' to have base as its backing file.
2245 *
2246 * Requires that the overlay to 'top' is opened r/w, so that the backing file
2247 * information in 'bs' can be properly updated.
2248 *
2249 * E.g., this will convert the following chain:
2250 * bottom <- base <- intermediate <- top <- active
2251 *
2252 * to
2253 *
2254 * bottom <- base <- active
2255 *
2256 * It is allowed for bottom==base, in which case it converts:
2257 *
2258 * base <- intermediate <- top <- active
2259 *
2260 * to
2261 *
2262 * base <- active
2263 *
2264 * Error conditions:
2265 * if active == top, that is considered an error
2266 *
2267 */
2268 int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top,
2269 BlockDriverState *base)
2270 {
2271 BlockDriverState *intermediate;
2272 BlockDriverState *base_bs = NULL;
2273 BlockDriverState *new_top_bs = NULL;
2274 BlkIntermediateStates *intermediate_state, *next;
2275 int ret = -EIO;
2276
2277 QSIMPLEQ_HEAD(states_to_delete, BlkIntermediateStates) states_to_delete;
2278 QSIMPLEQ_INIT(&states_to_delete);
2279
2280 if (!top->drv || !base->drv) {
2281 goto exit;
2282 }
2283
2284 new_top_bs = bdrv_find_overlay(active, top);
2285
2286 if (new_top_bs == NULL) {
2287 /* we could not find the image above 'top', this is an error */
2288 goto exit;
2289 }
2290
2291 /* special case of new_top_bs->backing_hd already pointing to base - nothing
2292 * to do, no intermediate images */
2293 if (new_top_bs->backing_hd == base) {
2294 ret = 0;
2295 goto exit;
2296 }
2297
2298 intermediate = top;
2299
2300 /* now we will go down through the list, and add each BDS we find
2301 * into our deletion queue, until we hit the 'base'
2302 */
2303 while (intermediate) {
2304 intermediate_state = g_malloc0(sizeof(BlkIntermediateStates));
2305 intermediate_state->bs = intermediate;
2306 QSIMPLEQ_INSERT_TAIL(&states_to_delete, intermediate_state, entry);
2307
2308 if (intermediate->backing_hd == base) {
2309 base_bs = intermediate->backing_hd;
2310 break;
2311 }
2312 intermediate = intermediate->backing_hd;
2313 }
2314 if (base_bs == NULL) {
2315 /* something went wrong, we did not end at the base. safely
2316 * unravel everything, and exit with error */
2317 goto exit;
2318 }
2319
2320 /* success - we can delete the intermediate states, and link top->base */
2321 ret = bdrv_change_backing_file(new_top_bs, base_bs->filename,
2322 base_bs->drv ? base_bs->drv->format_name : "");
2323 if (ret) {
2324 goto exit;
2325 }
2326 new_top_bs->backing_hd = base_bs;
2327
2328
2329 QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) {
2330 /* so that bdrv_close() does not recursively close the chain */
2331 intermediate_state->bs->backing_hd = NULL;
2332 bdrv_unref(intermediate_state->bs);
2333 }
2334 ret = 0;
2335
2336 exit:
2337 QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) {
2338 g_free(intermediate_state);
2339 }
2340 return ret;
2341 }
2342
2343
2344 static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
2345 size_t size)
2346 {
2347 int64_t len;
2348
2349 if (!bdrv_is_inserted(bs))
2350 return -ENOMEDIUM;
2351
2352 if (bs->growable)
2353 return 0;
2354
2355 len = bdrv_getlength(bs);
2356
2357 if (offset < 0)
2358 return -EIO;
2359
2360 if ((offset > len) || (len - offset < size))
2361 return -EIO;
2362
2363 return 0;
2364 }
2365
2366 static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
2367 int nb_sectors)
2368 {
2369 return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE,
2370 nb_sectors * BDRV_SECTOR_SIZE);
2371 }
2372
2373 typedef struct RwCo {
2374 BlockDriverState *bs;
2375 int64_t sector_num;
2376 int nb_sectors;
2377 QEMUIOVector *qiov;
2378 bool is_write;
2379 int ret;
2380 BdrvRequestFlags flags;
2381 } RwCo;
2382
2383 static void coroutine_fn bdrv_rw_co_entry(void *opaque)
2384 {
2385 RwCo *rwco = opaque;
2386
2387 if (!rwco->is_write) {
2388 rwco->ret = bdrv_co_do_readv(rwco->bs, rwco->sector_num,
2389 rwco->nb_sectors, rwco->qiov,
2390 rwco->flags);
2391 } else {
2392 rwco->ret = bdrv_co_do_writev(rwco->bs, rwco->sector_num,
2393 rwco->nb_sectors, rwco->qiov,
2394 rwco->flags);
2395 }
2396 }
2397
2398 /*
2399 * Process a vectored synchronous request using coroutines
2400 */
2401 static int bdrv_rwv_co(BlockDriverState *bs, int64_t sector_num,
2402 QEMUIOVector *qiov, bool is_write,
2403 BdrvRequestFlags flags)
2404 {
2405 Coroutine *co;
2406 RwCo rwco = {
2407 .bs = bs,
2408 .sector_num = sector_num,
2409 .nb_sectors = qiov->size >> BDRV_SECTOR_BITS,
2410 .qiov = qiov,
2411 .is_write = is_write,
2412 .ret = NOT_DONE,
2413 .flags = flags,
2414 };
2415 assert((qiov->size & (BDRV_SECTOR_SIZE - 1)) == 0);
2416
2417 /**
2418 * In sync call context, when the vcpu is blocked, this throttling timer
2419 * will not fire; so the I/O throttling function has to be disabled here
2420 * if it has been enabled.
2421 */
2422 if (bs->io_limits_enabled) {
2423 fprintf(stderr, "Disabling I/O throttling on '%s' due "
2424 "to synchronous I/O.\n", bdrv_get_device_name(bs));
2425 bdrv_io_limits_disable(bs);
2426 }
2427
2428 if (qemu_in_coroutine()) {
2429 /* Fast-path if already in coroutine context */
2430 bdrv_rw_co_entry(&rwco);
2431 } else {
2432 co = qemu_coroutine_create(bdrv_rw_co_entry);
2433 qemu_coroutine_enter(co, &rwco);
2434 while (rwco.ret == NOT_DONE) {
2435 qemu_aio_wait();
2436 }
2437 }
2438 return rwco.ret;
2439 }
2440
2441 /*
2442 * Process a synchronous request using coroutines
2443 */
2444 static int bdrv_rw_co(BlockDriverState *bs, int64_t sector_num, uint8_t *buf,
2445 int nb_sectors, bool is_write, BdrvRequestFlags flags)
2446 {
2447 QEMUIOVector qiov;
2448 struct iovec iov = {
2449 .iov_base = (void *)buf,
2450 .iov_len = nb_sectors * BDRV_SECTOR_SIZE,
2451 };
2452
2453 qemu_iovec_init_external(&qiov, &iov, 1);
2454 return bdrv_rwv_co(bs, sector_num, &qiov, is_write, flags);
2455 }
2456
2457 /* return < 0 if error. See bdrv_write() for the return codes */
2458 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
2459 uint8_t *buf, int nb_sectors)
2460 {
2461 return bdrv_rw_co(bs, sector_num, buf, nb_sectors, false, 0);
2462 }
2463
2464 /* Just like bdrv_read(), but with I/O throttling temporarily disabled */
2465 int bdrv_read_unthrottled(BlockDriverState *bs, int64_t sector_num,
2466 uint8_t *buf, int nb_sectors)
2467 {
2468 bool enabled;
2469 int ret;
2470
2471 enabled = bs->io_limits_enabled;
2472 bs->io_limits_enabled = false;
2473 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
2474 bs->io_limits_enabled = enabled;
2475 return ret;
2476 }
2477
2478 /* Return < 0 if error. Important errors are:
2479 -EIO generic I/O error (may happen for all errors)
2480 -ENOMEDIUM No media inserted.
2481 -EINVAL Invalid sector number or nb_sectors
2482 -EACCES Trying to write a read-only device
2483 */
2484 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
2485 const uint8_t *buf, int nb_sectors)
2486 {
2487 return bdrv_rw_co(bs, sector_num, (uint8_t *)buf, nb_sectors, true, 0);
2488 }
2489
2490 int bdrv_writev(BlockDriverState *bs, int64_t sector_num, QEMUIOVector *qiov)
2491 {
2492 return bdrv_rwv_co(bs, sector_num, qiov, true, 0);
2493 }
2494
2495 int bdrv_write_zeroes(BlockDriverState *bs, int64_t sector_num,
2496 int nb_sectors, BdrvRequestFlags flags)
2497 {
2498 return bdrv_rw_co(bs, sector_num, NULL, nb_sectors, true,
2499 BDRV_REQ_ZERO_WRITE | flags);
2500 }
2501
2502 /*
2503 * Completely zero out a block device with the help of bdrv_write_zeroes.
2504 * The operation is sped up by checking the block status and only writing
2505 * zeroes to the device if they currently do not return zeroes. Optional
2506 * flags are passed through to bdrv_write_zeroes (e.g. BDRV_REQ_MAY_UNMAP).
2507 *
2508 * Returns < 0 on error, 0 on success. For error codes see bdrv_write().
2509 */
2510 int bdrv_make_zero(BlockDriverState *bs, BdrvRequestFlags flags)
2511 {
2512 int64_t target_size = bdrv_getlength(bs) / BDRV_SECTOR_SIZE;
2513 int64_t ret, nb_sectors, sector_num = 0;
2514 int n;
2515
2516 for (;;) {
2517 nb_sectors = target_size - sector_num;
2518 if (nb_sectors <= 0) {
2519 return 0;
2520 }
2521 if (nb_sectors > INT_MAX) {
2522 nb_sectors = INT_MAX;
2523 }
2524 ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &n);
2525 if (ret < 0) {
2526 error_report("error getting block status at sector %" PRId64 ": %s",
2527 sector_num, strerror(-ret));
2528 return ret;
2529 }
2530 if (ret & BDRV_BLOCK_ZERO) {
2531 sector_num += n;
2532 continue;
2533 }
2534 ret = bdrv_write_zeroes(bs, sector_num, n, flags);
2535 if (ret < 0) {
2536 error_report("error writing zeroes at sector %" PRId64 ": %s",
2537 sector_num, strerror(-ret));
2538 return ret;
2539 }
2540 sector_num += n;
2541 }
2542 }
2543
2544 int bdrv_pread(BlockDriverState *bs, int64_t offset,
2545 void *buf, int count1)
2546 {
2547 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
2548 int len, nb_sectors, count;
2549 int64_t sector_num;
2550 int ret;
2551
2552 count = count1;
2553 /* first read to align to sector start */
2554 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
2555 if (len > count)
2556 len = count;
2557 sector_num = offset >> BDRV_SECTOR_BITS;
2558 if (len > 0) {
2559 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
2560 return ret;
2561 memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
2562 count -= len;
2563 if (count == 0)
2564 return count1;
2565 sector_num++;
2566 buf += len;
2567 }
2568
2569 /* read the sectors "in place" */
2570 nb_sectors = count >> BDRV_SECTOR_BITS;
2571 if (nb_sectors > 0) {
2572 if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0)
2573 return ret;
2574 sector_num += nb_sectors;
2575 len = nb_sectors << BDRV_SECTOR_BITS;
2576 buf += len;
2577 count -= len;
2578 }
2579
2580 /* add data from the last sector */
2581 if (count > 0) {
2582 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
2583 return ret;
2584 memcpy(buf, tmp_buf, count);
2585 }
2586 return count1;
2587 }
2588
2589 int bdrv_pwritev(BlockDriverState *bs, int64_t offset, QEMUIOVector *qiov)
2590 {
2591 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
2592 int len, nb_sectors, count;
2593 int64_t sector_num;
2594 int ret;
2595
2596 count = qiov->size;
2597
2598 /* first write to align to sector start */
2599 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
2600 if (len > count)
2601 len = count;
2602 sector_num = offset >> BDRV_SECTOR_BITS;
2603 if (len > 0) {
2604 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
2605 return ret;
2606 qemu_iovec_to_buf(qiov, 0, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)),
2607 len);
2608 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
2609 return ret;
2610 count -= len;
2611 if (count == 0)
2612 return qiov->size;
2613 sector_num++;
2614 }
2615
2616 /* write the sectors "in place" */
2617 nb_sectors = count >> BDRV_SECTOR_BITS;
2618 if (nb_sectors > 0) {
2619 QEMUIOVector qiov_inplace;
2620
2621 qemu_iovec_init(&qiov_inplace, qiov->niov);
2622 qemu_iovec_concat(&qiov_inplace, qiov, len,
2623 nb_sectors << BDRV_SECTOR_BITS);
2624 ret = bdrv_writev(bs, sector_num, &qiov_inplace);
2625 qemu_iovec_destroy(&qiov_inplace);
2626 if (ret < 0) {
2627 return ret;
2628 }
2629
2630 sector_num += nb_sectors;
2631 len = nb_sectors << BDRV_SECTOR_BITS;
2632 count -= len;
2633 }
2634
2635 /* add data from the last sector */
2636 if (count > 0) {
2637 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
2638 return ret;
2639 qemu_iovec_to_buf(qiov, qiov->size - count, tmp_buf, count);
2640 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
2641 return ret;
2642 }
2643 return qiov->size;
2644 }
2645
2646 int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
2647 const void *buf, int count1)
2648 {
2649 QEMUIOVector qiov;
2650 struct iovec iov = {
2651 .iov_base = (void *) buf,
2652 .iov_len = count1,
2653 };
2654
2655 qemu_iovec_init_external(&qiov, &iov, 1);
2656 return bdrv_pwritev(bs, offset, &qiov);
2657 }
2658
2659 /*
2660 * Writes to the file and ensures that no writes are reordered across this
2661 * request (acts as a barrier)
2662 *
2663 * Returns 0 on success, -errno in error cases.
2664 */
2665 int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset,
2666 const void *buf, int count)
2667 {
2668 int ret;
2669
2670 ret = bdrv_pwrite(bs, offset, buf, count);
2671 if (ret < 0) {
2672 return ret;
2673 }
2674
2675 /* No flush needed for cache modes that already do it */
2676 if (bs->enable_write_cache) {
2677 bdrv_flush(bs);
2678 }
2679
2680 return 0;
2681 }
2682
2683 static int coroutine_fn bdrv_co_do_copy_on_readv(BlockDriverState *bs,
2684 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
2685 {
2686 /* Perform I/O through a temporary buffer so that users who scribble over
2687 * their read buffer while the operation is in progress do not end up
2688 * modifying the image file. This is critical for zero-copy guest I/O
2689 * where anything might happen inside guest memory.
2690 */
2691 void *bounce_buffer;
2692
2693 BlockDriver *drv = bs->drv;
2694 struct iovec iov;
2695 QEMUIOVector bounce_qiov;
2696 int64_t cluster_sector_num;
2697 int cluster_nb_sectors;
2698 size_t skip_bytes;
2699 int ret;
2700
2701 /* Cover entire cluster so no additional backing file I/O is required when
2702 * allocating cluster in the image file.
2703 */
2704 bdrv_round_to_clusters(bs, sector_num, nb_sectors,
2705 &cluster_sector_num, &cluster_nb_sectors);
2706
2707 trace_bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors,
2708 cluster_sector_num, cluster_nb_sectors);
2709
2710 iov.iov_len = cluster_nb_sectors * BDRV_SECTOR_SIZE;
2711 iov.iov_base = bounce_buffer = qemu_blockalign(bs, iov.iov_len);
2712 qemu_iovec_init_external(&bounce_qiov, &iov, 1);
2713
2714 ret = drv->bdrv_co_readv(bs, cluster_sector_num, cluster_nb_sectors,
2715 &bounce_qiov);
2716 if (ret < 0) {
2717 goto err;
2718 }
2719
2720 if (drv->bdrv_co_write_zeroes &&
2721 buffer_is_zero(bounce_buffer, iov.iov_len)) {
2722 ret = bdrv_co_do_write_zeroes(bs, cluster_sector_num,
2723 cluster_nb_sectors, 0);
2724 } else {
2725 /* This does not change the data on the disk, it is not necessary
2726 * to flush even in cache=writethrough mode.
2727 */
2728 ret = drv->bdrv_co_writev(bs, cluster_sector_num, cluster_nb_sectors,
2729 &bounce_qiov);
2730 }
2731
2732 if (ret < 0) {
2733 /* It might be okay to ignore write errors for guest requests. If this
2734 * is a deliberate copy-on-read then we don't want to ignore the error.
2735 * Simply report it in all cases.
2736 */
2737 goto err;
2738 }
2739
2740 skip_bytes = (sector_num - cluster_sector_num) * BDRV_SECTOR_SIZE;
2741 qemu_iovec_from_buf(qiov, 0, bounce_buffer + skip_bytes,
2742 nb_sectors * BDRV_SECTOR_SIZE);
2743
2744 err:
2745 qemu_vfree(bounce_buffer);
2746 return ret;
2747 }
2748
2749 /*
2750 * Handle a read request in coroutine context
2751 */
2752 static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs,
2753 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
2754 BdrvRequestFlags flags)
2755 {
2756 BlockDriver *drv = bs->drv;
2757 BdrvTrackedRequest req;
2758 int ret;
2759
2760 if (!drv) {
2761 return -ENOMEDIUM;
2762 }
2763 if (bdrv_check_request(bs, sector_num, nb_sectors)) {
2764 return -EIO;
2765 }
2766
2767 if (bs->copy_on_read) {
2768 flags |= BDRV_REQ_COPY_ON_READ;
2769 }
2770 if (flags & BDRV_REQ_COPY_ON_READ) {
2771 bs->copy_on_read_in_flight++;
2772 }
2773
2774 if (bs->copy_on_read_in_flight) {
2775 wait_for_overlapping_requests(bs, sector_num, nb_sectors);
2776 }
2777
2778 /* throttling disk I/O */
2779 if (bs->io_limits_enabled) {
2780 bdrv_io_limits_intercept(bs, nb_sectors, false);
2781 }
2782
2783 tracked_request_begin(&req, bs, sector_num, nb_sectors, false);
2784
2785 if (flags & BDRV_REQ_COPY_ON_READ) {
2786 int pnum;
2787
2788 ret = bdrv_is_allocated(bs, sector_num, nb_sectors, &pnum);
2789 if (ret < 0) {
2790 goto out;
2791 }
2792
2793 if (!ret || pnum != nb_sectors) {
2794 ret = bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, qiov);
2795 goto out;
2796 }
2797 }
2798
2799 if (!(bs->zero_beyond_eof && bs->growable)) {
2800 ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov);
2801 } else {
2802 /* Read zeros after EOF of growable BDSes */
2803 int64_t len, total_sectors, max_nb_sectors;
2804
2805 len = bdrv_getlength(bs);
2806 if (len < 0) {
2807 ret = len;
2808 goto out;
2809 }
2810
2811 total_sectors = DIV_ROUND_UP(len, BDRV_SECTOR_SIZE);
2812 max_nb_sectors = MAX(0, total_sectors - sector_num);
2813 if (max_nb_sectors > 0) {
2814 ret = drv->bdrv_co_readv(bs, sector_num,
2815 MIN(nb_sectors, max_nb_sectors), qiov);
2816 } else {
2817 ret = 0;
2818 }
2819
2820 /* Reading beyond end of file is supposed to produce zeroes */
2821 if (ret == 0 && total_sectors < sector_num + nb_sectors) {
2822 uint64_t offset = MAX(0, total_sectors - sector_num);
2823 uint64_t bytes = (sector_num + nb_sectors - offset) *
2824 BDRV_SECTOR_SIZE;
2825 qemu_iovec_memset(qiov, offset * BDRV_SECTOR_SIZE, 0, bytes);
2826 }
2827 }
2828
2829 out:
2830 tracked_request_end(&req);
2831
2832 if (flags & BDRV_REQ_COPY_ON_READ) {
2833 bs->copy_on_read_in_flight--;
2834 }
2835
2836 return ret;
2837 }
2838
2839 int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num,
2840 int nb_sectors, QEMUIOVector *qiov)
2841 {
2842 trace_bdrv_co_readv(bs, sector_num, nb_sectors);
2843
2844 return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 0);
2845 }
2846
2847 int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs,
2848 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
2849 {
2850 trace_bdrv_co_copy_on_readv(bs, sector_num, nb_sectors);
2851
2852 return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov,
2853 BDRV_REQ_COPY_ON_READ);
2854 }
2855
2856 /* if no limit is specified in the BlockLimits use a default
2857 * of 32768 512-byte sectors (16 MiB) per request.
2858 */
2859 #define MAX_WRITE_ZEROES_DEFAULT 32768
2860
2861 static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs,
2862 int64_t sector_num, int nb_sectors, BdrvRequestFlags flags)
2863 {
2864 BlockDriver *drv = bs->drv;
2865 QEMUIOVector qiov;
2866 struct iovec iov = {0};
2867 int ret = 0;
2868
2869 int max_write_zeroes = bs->bl.max_write_zeroes ?
2870 bs->bl.max_write_zeroes : MAX_WRITE_ZEROES_DEFAULT;
2871
2872 while (nb_sectors > 0 && !ret) {
2873 int num = nb_sectors;
2874
2875 /* Align request. Block drivers can expect the "bulk" of the request
2876 * to be aligned.
2877 */
2878 if (bs->bl.write_zeroes_alignment
2879 && num > bs->bl.write_zeroes_alignment) {
2880 if (sector_num % bs->bl.write_zeroes_alignment != 0) {
2881 /* Make a small request up to the first aligned sector. */
2882 num = bs->bl.write_zeroes_alignment;
2883 num -= sector_num % bs->bl.write_zeroes_alignment;
2884 } else if ((sector_num + num) % bs->bl.write_zeroes_alignment != 0) {
2885 /* Shorten the request to the last aligned sector. num cannot
2886 * underflow because num > bs->bl.write_zeroes_alignment.
2887 */
2888 num -= (sector_num + num) % bs->bl.write_zeroes_alignment;
2889 }
2890 }
2891
2892 /* limit request size */
2893 if (num > max_write_zeroes) {
2894 num = max_write_zeroes;
2895 }
2896
2897 ret = -ENOTSUP;
2898 /* First try the efficient write zeroes operation */
2899 if (drv->bdrv_co_write_zeroes) {
2900 ret = drv->bdrv_co_write_zeroes(bs, sector_num, num, flags);
2901 }
2902
2903 if (ret == -ENOTSUP) {
2904 /* Fall back to bounce buffer if write zeroes is unsupported */
2905 iov.iov_len = num * BDRV_SECTOR_SIZE;
2906 if (iov.iov_base == NULL) {
2907 iov.iov_base = qemu_blockalign(bs, num * BDRV_SECTOR_SIZE);
2908 memset(iov.iov_base, 0, num * BDRV_SECTOR_SIZE);
2909 }
2910 qemu_iovec_init_external(&qiov, &iov, 1);
2911
2912 ret = drv->bdrv_co_writev(bs, sector_num, num, &qiov);
2913
2914 /* Keep bounce buffer around if it is big enough for all
2915 * all future requests.
2916 */
2917 if (num < max_write_zeroes) {
2918 qemu_vfree(iov.iov_base);
2919 iov.iov_base = NULL;
2920 }
2921 }
2922
2923 sector_num += num;
2924 nb_sectors -= num;
2925 }
2926
2927 qemu_vfree(iov.iov_base);
2928 return ret;
2929 }
2930
2931 /*
2932 * Handle a write request in coroutine context
2933 */
2934 static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs,
2935 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
2936 BdrvRequestFlags flags)
2937 {
2938 BlockDriver *drv = bs->drv;
2939 BdrvTrackedRequest req;
2940 int ret;
2941
2942 if (!bs->drv) {
2943 return -ENOMEDIUM;
2944 }
2945 if (bs->read_only) {
2946 return -EACCES;
2947 }
2948 if (bdrv_check_request(bs, sector_num, nb_sectors)) {
2949 return -EIO;
2950 }
2951
2952 if (bs->copy_on_read_in_flight) {
2953 wait_for_overlapping_requests(bs, sector_num, nb_sectors);
2954 }
2955
2956 /* throttling disk I/O */
2957 if (bs->io_limits_enabled) {
2958 bdrv_io_limits_intercept(bs, nb_sectors, true);
2959 }
2960
2961 tracked_request_begin(&req, bs, sector_num, nb_sectors, true);
2962
2963 ret = notifier_with_return_list_notify(&bs->before_write_notifiers, &req);
2964
2965 if (ret < 0) {
2966 /* Do nothing, write notifier decided to fail this request */
2967 } else if (flags & BDRV_REQ_ZERO_WRITE) {
2968 ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors, flags);
2969 } else {
2970 ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov);
2971 }
2972
2973 if (ret == 0 && !bs->enable_write_cache) {
2974 ret = bdrv_co_flush(bs);
2975 }
2976
2977 bdrv_set_dirty(bs, sector_num, nb_sectors);
2978
2979 if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
2980 bs->wr_highest_sector = sector_num + nb_sectors - 1;
2981 }
2982 if (bs->growable && ret >= 0) {
2983 bs->total_sectors = MAX(bs->total_sectors, sector_num + nb_sectors);
2984 }
2985
2986 tracked_request_end(&req);
2987
2988 return ret;
2989 }
2990
2991 int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num,
2992 int nb_sectors, QEMUIOVector *qiov)
2993 {
2994 trace_bdrv_co_writev(bs, sector_num, nb_sectors);
2995
2996 return bdrv_co_do_writev(bs, sector_num, nb_sectors, qiov, 0);
2997 }
2998
2999 int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs,
3000 int64_t sector_num, int nb_sectors,
3001 BdrvRequestFlags flags)
3002 {
3003 trace_bdrv_co_write_zeroes(bs, sector_num, nb_sectors, flags);
3004
3005 if (!(bs->open_flags & BDRV_O_UNMAP)) {
3006 flags &= ~BDRV_REQ_MAY_UNMAP;
3007 }
3008
3009 return bdrv_co_do_writev(bs, sector_num, nb_sectors, NULL,
3010 BDRV_REQ_ZERO_WRITE | flags);
3011 }
3012
3013 /**
3014 * Truncate file to 'offset' bytes (needed only for file protocols)
3015 */
3016 int bdrv_truncate(BlockDriverState *bs, int64_t offset)
3017 {
3018 BlockDriver *drv = bs->drv;
3019 int ret;
3020 if (!drv)
3021 return -ENOMEDIUM;
3022 if (!drv->bdrv_truncate)
3023 return -ENOTSUP;
3024 if (bs->read_only)
3025 return -EACCES;
3026 if (bdrv_in_use(bs))
3027 return -EBUSY;
3028 ret = drv->bdrv_truncate(bs, offset);
3029 if (ret == 0) {
3030 ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
3031 bdrv_dev_resize_cb(bs);
3032 }
3033 return ret;
3034 }
3035
3036 /**
3037 * Length of a allocated file in bytes. Sparse files are counted by actual
3038 * allocated space. Return < 0 if error or unknown.
3039 */
3040 int64_t bdrv_get_allocated_file_size(BlockDriverState *bs)
3041 {
3042 BlockDriver *drv = bs->drv;
3043 if (!drv) {
3044 return -ENOMEDIUM;
3045 }
3046 if (drv->bdrv_get_allocated_file_size) {
3047 return drv->bdrv_get_allocated_file_size(bs);
3048 }
3049 if (bs->file) {
3050 return bdrv_get_allocated_file_size(bs->file);
3051 }
3052 return -ENOTSUP;
3053 }
3054
3055 /**
3056 * Length of a file in bytes. Return < 0 if error or unknown.
3057 */
3058 int64_t bdrv_getlength(BlockDriverState *bs)
3059 {
3060 BlockDriver *drv = bs->drv;
3061 if (!drv)
3062 return -ENOMEDIUM;
3063
3064 if (drv->has_variable_length) {
3065 int ret = refresh_total_sectors(bs, bs->total_sectors);
3066 if (ret < 0) {
3067 return ret;
3068 }
3069 }
3070 return bs->total_sectors * BDRV_SECTOR_SIZE;
3071 }
3072
3073 /* return 0 as number of sectors if no device present or error */
3074 void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
3075 {
3076 int64_t length;
3077 length = bdrv_getlength(bs);
3078 if (length < 0)
3079 length = 0;
3080 else
3081 length = length >> BDRV_SECTOR_BITS;
3082 *nb_sectors_ptr = length;
3083 }
3084
3085 void bdrv_set_on_error(BlockDriverState *bs, BlockdevOnError on_read_error,
3086 BlockdevOnError on_write_error)
3087 {
3088 bs->on_read_error = on_read_error;
3089 bs->on_write_error = on_write_error;
3090 }
3091
3092 BlockdevOnError bdrv_get_on_error(BlockDriverState *bs, bool is_read)
3093 {
3094 return is_read ? bs->on_read_error : bs->on_write_error;
3095 }
3096
3097 BlockErrorAction bdrv_get_error_action(BlockDriverState *bs, bool is_read, int error)
3098 {
3099 BlockdevOnError on_err = is_read ? bs->on_read_error : bs->on_write_error;
3100
3101 switch (on_err) {
3102 case BLOCKDEV_ON_ERROR_ENOSPC:
3103 return (error == ENOSPC) ? BDRV_ACTION_STOP : BDRV_ACTION_REPORT;
3104 case BLOCKDEV_ON_ERROR_STOP:
3105 return BDRV_ACTION_STOP;
3106 case BLOCKDEV_ON_ERROR_REPORT:
3107 return BDRV_ACTION_REPORT;
3108 case BLOCKDEV_ON_ERROR_IGNORE:
3109 return BDRV_ACTION_IGNORE;
3110 default:
3111 abort();
3112 }
3113 }
3114
3115 /* This is done by device models because, while the block layer knows
3116 * about the error, it does not know whether an operation comes from
3117 * the device or the block layer (from a job, for example).
3118 */
3119 void bdrv_error_action(BlockDriverState *bs, BlockErrorAction action,
3120 bool is_read, int error)
3121 {
3122 assert(error >= 0);
3123 bdrv_emit_qmp_error_event(bs, QEVENT_BLOCK_IO_ERROR, action, is_read);
3124 if (action == BDRV_ACTION_STOP) {
3125 vm_stop(RUN_STATE_IO_ERROR);
3126 bdrv_iostatus_set_err(bs, error);
3127 }
3128 }
3129
3130 int bdrv_is_read_only(BlockDriverState *bs)
3131 {
3132 return bs->read_only;
3133 }
3134
3135 int bdrv_is_sg(BlockDriverState *bs)
3136 {
3137 return bs->sg;
3138 }
3139
3140 int bdrv_enable_write_cache(BlockDriverState *bs)
3141 {
3142 return bs->enable_write_cache;
3143 }
3144
3145 void bdrv_set_enable_write_cache(BlockDriverState *bs, bool wce)
3146 {
3147 bs->enable_write_cache = wce;
3148
3149 /* so a reopen() will preserve wce */
3150 if (wce) {
3151 bs->open_flags |= BDRV_O_CACHE_WB;
3152 } else {
3153 bs->open_flags &= ~BDRV_O_CACHE_WB;
3154 }
3155 }
3156
3157 int bdrv_is_encrypted(BlockDriverState *bs)
3158 {
3159 if (bs->backing_hd && bs->backing_hd->encrypted)
3160 return 1;
3161 return bs->encrypted;
3162 }
3163
3164 int bdrv_key_required(BlockDriverState *bs)
3165 {
3166 BlockDriverState *backing_hd = bs->backing_hd;
3167
3168 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
3169 return 1;
3170 return (bs->encrypted && !bs->valid_key);
3171 }
3172
3173 int bdrv_set_key(BlockDriverState *bs, const char *key)
3174 {
3175 int ret;
3176 if (bs->backing_hd && bs->backing_hd->encrypted) {
3177 ret = bdrv_set_key(bs->backing_hd, key);
3178 if (ret < 0)
3179 return ret;
3180 if (!bs->encrypted)
3181 return 0;
3182 }
3183 if (!bs->encrypted) {
3184 return -EINVAL;
3185 } else if (!bs->drv || !bs->drv->bdrv_set_key) {
3186 return -ENOMEDIUM;
3187 }
3188 ret = bs->drv->bdrv_set_key(bs, key);
3189 if (ret < 0) {
3190 bs->valid_key = 0;
3191 } else if (!bs->valid_key) {
3192 bs->valid_key = 1;
3193 /* call the change callback now, we skipped it on open */
3194 bdrv_dev_change_media_cb(bs, true);
3195 }
3196 return ret;
3197 }
3198
3199 const char *bdrv_get_format_name(BlockDriverState *bs)
3200 {
3201 return bs->drv ? bs->drv->format_name : NULL;
3202 }
3203
3204 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
3205 void *opaque)
3206 {
3207 BlockDriver *drv;
3208
3209 QLIST_FOREACH(drv, &bdrv_drivers, list) {
3210 it(opaque, drv->format_name);
3211 }
3212 }
3213
3214 BlockDriverState *bdrv_find(const char *name)
3215 {
3216 BlockDriverState *bs;
3217
3218 QTAILQ_FOREACH(bs, &bdrv_states, list) {
3219 if (!strcmp(name, bs->device_name)) {
3220 return bs;
3221 }
3222 }
3223 return NULL;
3224 }
3225
3226 BlockDriverState *bdrv_next(BlockDriverState *bs)
3227 {
3228 if (!bs) {
3229 return QTAILQ_FIRST(&bdrv_states);
3230 }
3231 return QTAILQ_NEXT(bs, list);
3232 }
3233
3234 void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
3235 {
3236 BlockDriverState *bs;
3237
3238 QTAILQ_FOREACH(bs, &bdrv_states, list) {
3239 it(opaque, bs);
3240 }
3241 }
3242
3243 const char *bdrv_get_device_name(BlockDriverState *bs)
3244 {
3245 return bs->device_name;
3246 }
3247
3248 int bdrv_get_flags(BlockDriverState *bs)
3249 {
3250 return bs->open_flags;
3251 }
3252
3253 int bdrv_flush_all(void)
3254 {
3255 BlockDriverState *bs;
3256 int result = 0;
3257
3258 QTAILQ_FOREACH(bs, &bdrv_states, list) {
3259 int ret = bdrv_flush(bs);
3260 if (ret < 0 && !result) {
3261 result = ret;
3262 }
3263 }
3264
3265 return result;
3266 }
3267
3268 int bdrv_has_zero_init_1(BlockDriverState *bs)
3269 {
3270 return 1;
3271 }
3272
3273 int bdrv_has_zero_init(BlockDriverState *bs)
3274 {
3275 assert(bs->drv);
3276
3277 /* If BS is a copy on write image, it is initialized to
3278 the contents of the base image, which may not be zeroes. */
3279 if (bs->backing_hd) {
3280 return 0;
3281 }
3282 if (bs->drv->bdrv_has_zero_init) {
3283 return bs->drv->bdrv_has_zero_init(bs);
3284 }
3285
3286 /* safe default */
3287 return 0;
3288 }
3289
3290 bool bdrv_unallocated_blocks_are_zero(BlockDriverState *bs)
3291 {
3292 BlockDriverInfo bdi;
3293
3294 if (bs->backing_hd) {
3295 return false;
3296 }
3297
3298 if (bdrv_get_info(bs, &bdi) == 0) {
3299 return bdi.unallocated_blocks_are_zero;
3300 }
3301
3302 return false;
3303 }
3304
3305 bool bdrv_can_write_zeroes_with_unmap(BlockDriverState *bs)
3306 {
3307 BlockDriverInfo bdi;
3308
3309 if (bs->backing_hd || !(bs->open_flags & BDRV_O_UNMAP)) {
3310 return false;
3311 }
3312
3313 if (bdrv_get_info(bs, &bdi) == 0) {
3314 return bdi.can_write_zeroes_with_unmap;
3315 }
3316
3317 return false;
3318 }
3319
3320 typedef struct BdrvCoGetBlockStatusData {
3321 BlockDriverState *bs;
3322 BlockDriverState *base;
3323 int64_t sector_num;
3324 int nb_sectors;
3325 int *pnum;
3326 int64_t ret;
3327 bool done;
3328 } BdrvCoGetBlockStatusData;
3329
3330 /*
3331 * Returns true iff the specified sector is present in the disk image. Drivers
3332 * not implementing the functionality are assumed to not support backing files,
3333 * hence all their sectors are reported as allocated.
3334 *
3335 * If 'sector_num' is beyond the end of the disk image the return value is 0
3336 * and 'pnum' is set to 0.
3337 *
3338 * 'pnum' is set to the number of sectors (including and immediately following
3339 * the specified sector) that are known to be in the same
3340 * allocated/unallocated state.
3341 *
3342 * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
3343 * beyond the end of the disk image it will be clamped.
3344 */
3345 static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs,
3346 int64_t sector_num,
3347 int nb_sectors, int *pnum)
3348 {
3349 int64_t length;
3350 int64_t n;
3351 int64_t ret, ret2;
3352
3353 length = bdrv_getlength(bs);
3354 if (length < 0) {
3355 return length;
3356 }
3357
3358 if (sector_num >= (length >> BDRV_SECTOR_BITS)) {
3359 *pnum = 0;
3360 return 0;
3361 }
3362
3363 n = bs->total_sectors - sector_num;
3364 if (n < nb_sectors) {
3365 nb_sectors = n;
3366 }
3367
3368 if (!bs->drv->bdrv_co_get_block_status) {
3369 *pnum = nb_sectors;
3370 ret = BDRV_BLOCK_DATA;
3371 if (bs->drv->protocol_name) {
3372 ret |= BDRV_BLOCK_OFFSET_VALID | (sector_num * BDRV_SECTOR_SIZE);
3373 }
3374 return ret;
3375 }
3376
3377 ret = bs->drv->bdrv_co_get_block_status(bs, sector_num, nb_sectors, pnum);
3378 if (ret < 0) {
3379 *pnum = 0;
3380 return ret;
3381 }
3382
3383 if (ret & BDRV_BLOCK_RAW) {
3384 assert(ret & BDRV_BLOCK_OFFSET_VALID);
3385 return bdrv_get_block_status(bs->file, ret >> BDRV_SECTOR_BITS,
3386 *pnum, pnum);
3387 }
3388
3389 if (!(ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO)) {
3390 if (bdrv_unallocated_blocks_are_zero(bs)) {
3391 ret |= BDRV_BLOCK_ZERO;
3392 } else if (bs->backing_hd) {
3393 BlockDriverState *bs2 = bs->backing_hd;
3394 int64_t length2 = bdrv_getlength(bs2);
3395 if (length2 >= 0 && sector_num >= (length2 >> BDRV_SECTOR_BITS)) {
3396 ret |= BDRV_BLOCK_ZERO;
3397 }
3398 }
3399 }
3400
3401 if (bs->file &&
3402 (ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO) &&
3403 (ret & BDRV_BLOCK_OFFSET_VALID)) {
3404 ret2 = bdrv_co_get_block_status(bs->file, ret >> BDRV_SECTOR_BITS,
3405 *pnum, pnum);
3406 if (ret2 >= 0) {
3407 /* Ignore errors. This is just providing extra information, it
3408 * is useful but not necessary.
3409 */
3410 ret |= (ret2 & BDRV_BLOCK_ZERO);
3411 }
3412 }
3413
3414 return ret;
3415 }
3416
3417 /* Coroutine wrapper for bdrv_get_block_status() */
3418 static void coroutine_fn bdrv_get_block_status_co_entry(void *opaque)
3419 {
3420 BdrvCoGetBlockStatusData *data = opaque;
3421 BlockDriverState *bs = data->bs;
3422
3423 data->ret = bdrv_co_get_block_status(bs, data->sector_num, data->nb_sectors,
3424 data->pnum);
3425 data->done = true;
3426 }
3427
3428 /*
3429 * Synchronous wrapper around bdrv_co_get_block_status().
3430 *
3431 * See bdrv_co_get_block_status() for details.
3432 */
3433 int64_t bdrv_get_block_status(BlockDriverState *bs, int64_t sector_num,
3434 int nb_sectors, int *pnum)
3435 {
3436 Coroutine *co;
3437 BdrvCoGetBlockStatusData data = {
3438 .bs = bs,
3439 .sector_num = sector_num,
3440 .nb_sectors = nb_sectors,
3441 .pnum = pnum,
3442 .done = false,
3443 };
3444
3445 if (qemu_in_coroutine()) {
3446 /* Fast-path if already in coroutine context */
3447 bdrv_get_block_status_co_entry(&data);
3448 } else {
3449 co = qemu_coroutine_create(bdrv_get_block_status_co_entry);
3450 qemu_coroutine_enter(co, &data);
3451 while (!data.done) {
3452 qemu_aio_wait();
3453 }
3454 }
3455 return data.ret;
3456 }
3457
3458 int coroutine_fn bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num,
3459 int nb_sectors, int *pnum)
3460 {
3461 int64_t ret = bdrv_get_block_status(bs, sector_num, nb_sectors, pnum);
3462 if (ret < 0) {
3463 return ret;
3464 }
3465 return
3466 (ret & BDRV_BLOCK_DATA) ||
3467 ((ret & BDRV_BLOCK_ZERO) && !bdrv_has_zero_init(bs));
3468 }
3469
3470 /*
3471 * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP]
3472 *
3473 * Return true if the given sector is allocated in any image between
3474 * BASE and TOP (inclusive). BASE can be NULL to check if the given
3475 * sector is allocated in any image of the chain. Return false otherwise.
3476 *
3477 * 'pnum' is set to the number of sectors (including and immediately following
3478 * the specified sector) that are known to be in the same
3479 * allocated/unallocated state.
3480 *
3481 */
3482 int bdrv_is_allocated_above(BlockDriverState *top,
3483 BlockDriverState *base,
3484 int64_t sector_num,
3485 int nb_sectors, int *pnum)
3486 {
3487 BlockDriverState *intermediate;
3488 int ret, n = nb_sectors;
3489
3490 intermediate = top;
3491 while (intermediate && intermediate != base) {
3492 int pnum_inter;
3493 ret = bdrv_is_allocated(intermediate, sector_num, nb_sectors,
3494 &pnum_inter);
3495 if (ret < 0) {
3496 return ret;
3497 } else if (ret) {
3498 *pnum = pnum_inter;
3499 return 1;
3500 }
3501
3502 /*
3503 * [sector_num, nb_sectors] is unallocated on top but intermediate
3504 * might have
3505 *
3506 * [sector_num+x, nr_sectors] allocated.
3507 */
3508 if (n > pnum_inter &&
3509 (intermediate == top ||
3510 sector_num + pnum_inter < intermediate->total_sectors)) {
3511 n = pnum_inter;
3512 }
3513
3514 intermediate = intermediate->backing_hd;
3515 }
3516
3517 *pnum = n;
3518 return 0;
3519 }
3520
3521 const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
3522 {
3523 if (bs->backing_hd && bs->backing_hd->encrypted)
3524 return bs->backing_file;
3525 else if (bs->encrypted)
3526 return bs->filename;
3527 else
3528 return NULL;
3529 }
3530
3531 void bdrv_get_backing_filename(BlockDriverState *bs,
3532 char *filename, int filename_size)
3533 {
3534 pstrcpy(filename, filename_size, bs->backing_file);
3535 }
3536
3537 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
3538 const uint8_t *buf, int nb_sectors)
3539 {
3540 BlockDriver *drv = bs->drv;
3541 if (!drv)
3542 return -ENOMEDIUM;
3543 if (!drv->bdrv_write_compressed)
3544 return -ENOTSUP;
3545 if (bdrv_check_request(bs, sector_num, nb_sectors))
3546 return -EIO;
3547
3548 assert(QLIST_EMPTY(&bs->dirty_bitmaps));
3549
3550 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
3551 }
3552
3553 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
3554 {
3555 BlockDriver *drv = bs->drv;
3556 if (!drv)
3557 return -ENOMEDIUM;
3558 if (!drv->bdrv_get_info)
3559 return -ENOTSUP;
3560 memset(bdi, 0, sizeof(*bdi));
3561 return drv->bdrv_get_info(bs, bdi);
3562 }
3563
3564 ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs)
3565 {
3566 BlockDriver *drv = bs->drv;
3567 if (drv && drv->bdrv_get_specific_info) {
3568 return drv->bdrv_get_specific_info(bs);
3569 }
3570 return NULL;
3571 }
3572
3573 int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
3574 int64_t pos, int size)
3575 {
3576 QEMUIOVector qiov;
3577 struct iovec iov = {
3578 .iov_base = (void *) buf,
3579 .iov_len = size,
3580 };
3581
3582 qemu_iovec_init_external(&qiov, &iov, 1);
3583 return bdrv_writev_vmstate(bs, &qiov, pos);
3584 }
3585
3586 int bdrv_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos)
3587 {
3588 BlockDriver *drv = bs->drv;
3589
3590 if (!drv) {
3591 return -ENOMEDIUM;
3592 } else if (drv->bdrv_save_vmstate) {
3593 return drv->bdrv_save_vmstate(bs, qiov, pos);
3594 } else if (bs->file) {
3595 return bdrv_writev_vmstate(bs->file, qiov, pos);
3596 }
3597
3598 return -ENOTSUP;
3599 }
3600
3601 int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
3602 int64_t pos, int size)
3603 {
3604 BlockDriver *drv = bs->drv;
3605 if (!drv)
3606 return -ENOMEDIUM;
3607 if (drv->bdrv_load_vmstate)
3608 return drv->bdrv_load_vmstate(bs, buf, pos, size);
3609 if (bs->file)
3610 return bdrv_load_vmstate(bs->file, buf, pos, size);
3611 return -ENOTSUP;
3612 }
3613
3614 void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
3615 {
3616 if (!bs || !bs->drv || !bs->drv->bdrv_debug_event) {
3617 return;
3618 }
3619
3620 bs->drv->bdrv_debug_event(bs, event);
3621 }
3622
3623 int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event,
3624 const char *tag)
3625 {
3626 while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) {
3627 bs = bs->file;
3628 }
3629
3630 if (bs && bs->drv && bs->drv->bdrv_debug_breakpoint) {
3631 return bs->drv->bdrv_debug_breakpoint(bs, event, tag);
3632 }
3633
3634 return -ENOTSUP;
3635 }
3636
3637 int bdrv_debug_remove_breakpoint(BlockDriverState *bs, const char *tag)
3638 {
3639 while (bs && bs->drv && !bs->drv->bdrv_debug_remove_breakpoint) {
3640 bs = bs->file;
3641 }
3642
3643 if (bs && bs->drv && bs->drv->bdrv_debug_remove_breakpoint) {
3644 return bs->drv->bdrv_debug_remove_breakpoint(bs, tag);
3645 }
3646
3647 return -ENOTSUP;
3648 }
3649
3650 int bdrv_debug_resume(BlockDriverState *bs, const char *tag)
3651 {
3652 while (bs && bs->drv && !bs->drv->bdrv_debug_resume) {
3653 bs = bs->file;
3654 }
3655
3656 if (bs && bs->drv && bs->drv->bdrv_debug_resume) {
3657 return bs->drv->bdrv_debug_resume(bs, tag);
3658 }
3659
3660 return -ENOTSUP;
3661 }
3662
3663 bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag)
3664 {
3665 while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) {
3666 bs = bs->file;
3667 }
3668
3669 if (bs && bs->drv && bs->drv->bdrv_debug_is_suspended) {
3670 return bs->drv->bdrv_debug_is_suspended(bs, tag);
3671 }
3672
3673 return false;
3674 }
3675
3676 int bdrv_is_snapshot(BlockDriverState *bs)
3677 {
3678 return !!(bs->open_flags & BDRV_O_SNAPSHOT);
3679 }
3680
3681 /* backing_file can either be relative, or absolute, or a protocol. If it is
3682 * relative, it must be relative to the chain. So, passing in bs->filename
3683 * from a BDS as backing_file should not be done, as that may be relative to
3684 * the CWD rather than the chain. */
3685 BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs,
3686 const char *backing_file)
3687 {
3688 char *filename_full = NULL;
3689 char *backing_file_full = NULL;
3690 char *filename_tmp = NULL;
3691 int is_protocol = 0;
3692 BlockDriverState *curr_bs = NULL;
3693 BlockDriverState *retval = NULL;
3694
3695 if (!bs || !bs->drv || !backing_file) {
3696 return NULL;
3697 }
3698
3699 filename_full = g_malloc(PATH_MAX);
3700 backing_file_full = g_malloc(PATH_MAX);
3701 filename_tmp = g_malloc(PATH_MAX);
3702
3703 is_protocol = path_has_protocol(backing_file);
3704
3705 for (curr_bs = bs; curr_bs->backing_hd; curr_bs = curr_bs->backing_hd) {
3706
3707 /* If either of the filename paths is actually a protocol, then
3708 * compare unmodified paths; otherwise make paths relative */
3709 if (is_protocol || path_has_protocol(curr_bs->backing_file)) {
3710 if (strcmp(backing_file, curr_bs->backing_file) == 0) {
3711 retval = curr_bs->backing_hd;
3712 break;
3713 }
3714 } else {
3715 /* If not an absolute filename path, make it relative to the current
3716 * image's filename path */
3717 path_combine(filename_tmp, PATH_MAX, curr_bs->filename,
3718 backing_file);
3719
3720 /* We are going to compare absolute pathnames */
3721 if (!realpath(filename_tmp, filename_full)) {
3722 continue;
3723 }
3724
3725 /* We need to make sure the backing filename we are comparing against
3726 * is relative to the current image filename (or absolute) */
3727 path_combine(filename_tmp, PATH_MAX, curr_bs->filename,
3728 curr_bs->backing_file);
3729
3730 if (!realpath(filename_tmp, backing_file_full)) {
3731 continue;
3732 }
3733
3734 if (strcmp(backing_file_full, filename_full) == 0) {
3735 retval = curr_bs->backing_hd;
3736 break;
3737 }
3738 }
3739 }
3740
3741 g_free(filename_full);
3742 g_free(backing_file_full);
3743 g_free(filename_tmp);
3744 return retval;
3745 }
3746
3747 int bdrv_get_backing_file_depth(BlockDriverState *bs)
3748 {
3749 if (!bs->drv) {
3750 return 0;
3751 }
3752
3753 if (!bs->backing_hd) {
3754 return 0;
3755 }
3756
3757 return 1 + bdrv_get_backing_file_depth(bs->backing_hd);
3758 }
3759
3760 BlockDriverState *bdrv_find_base(BlockDriverState *bs)
3761 {
3762 BlockDriverState *curr_bs = NULL;
3763
3764 if (!bs) {
3765 return NULL;
3766 }
3767
3768 curr_bs = bs;
3769
3770 while (curr_bs->backing_hd) {
3771 curr_bs = curr_bs->backing_hd;
3772 }
3773 return curr_bs;
3774 }
3775
3776 /**************************************************************/
3777 /* async I/Os */
3778
3779 BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
3780 QEMUIOVector *qiov, int nb_sectors,
3781 BlockDriverCompletionFunc *cb, void *opaque)
3782 {
3783 trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque);
3784
3785 return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 0,
3786 cb, opaque, false);
3787 }
3788
3789 BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
3790 QEMUIOVector *qiov, int nb_sectors,
3791 BlockDriverCompletionFunc *cb, void *opaque)
3792 {
3793 trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque);
3794
3795 return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 0,
3796 cb, opaque, true);
3797 }
3798
3799 BlockDriverAIOCB *bdrv_aio_write_zeroes(BlockDriverState *bs,
3800 int64_t sector_num, int nb_sectors, BdrvRequestFlags flags,
3801 BlockDriverCompletionFunc *cb, void *opaque)
3802 {
3803 trace_bdrv_aio_write_zeroes(bs, sector_num, nb_sectors, flags, opaque);
3804
3805 return bdrv_co_aio_rw_vector(bs, sector_num, NULL, nb_sectors,
3806 BDRV_REQ_ZERO_WRITE | flags,
3807 cb, opaque, true);
3808 }
3809
3810
3811 typedef struct MultiwriteCB {
3812 int error;
3813 int num_requests;
3814 int num_callbacks;
3815 struct {
3816 BlockDriverCompletionFunc *cb;
3817 void *opaque;
3818 QEMUIOVector *free_qiov;
3819 } callbacks[];
3820 } MultiwriteCB;
3821
3822 static void multiwrite_user_cb(MultiwriteCB *mcb)
3823 {
3824 int i;
3825
3826 for (i = 0; i < mcb->num_callbacks; i++) {
3827 mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
3828 if (mcb->callbacks[i].free_qiov) {
3829 qemu_iovec_destroy(mcb->callbacks[i].free_qiov);
3830 }
3831 g_free(mcb->callbacks[i].free_qiov);
3832 }
3833 }
3834
3835 static void multiwrite_cb(void *opaque, int ret)
3836 {
3837 MultiwriteCB *mcb = opaque;
3838
3839 trace_multiwrite_cb(mcb, ret);
3840
3841 if (ret < 0 && !mcb->error) {
3842 mcb->error = ret;
3843 }
3844
3845 mcb->num_requests--;
3846 if (mcb->num_requests == 0) {
3847 multiwrite_user_cb(mcb);
3848 g_free(mcb);
3849 }
3850 }
3851
3852 static int multiwrite_req_compare(const void *a, const void *b)
3853 {
3854 const BlockRequest *req1 = a, *req2 = b;
3855
3856 /*
3857 * Note that we can't simply subtract req2->sector from req1->sector
3858 * here as that could overflow the return value.
3859 */
3860 if (req1->sector > req2->sector) {
3861 return 1;
3862 } else if (req1->sector < req2->sector) {
3863 return -1;
3864 } else {
3865 return 0;
3866 }
3867 }
3868
3869 /*
3870 * Takes a bunch of requests and tries to merge them. Returns the number of
3871 * requests that remain after merging.
3872 */
3873 static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
3874 int num_reqs, MultiwriteCB *mcb)
3875 {
3876 int i, outidx;
3877
3878 // Sort requests by start sector
3879 qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
3880
3881 // Check if adjacent requests touch the same clusters. If so, combine them,
3882 // filling up gaps with zero sectors.
3883 outidx = 0;
3884 for (i = 1; i < num_reqs; i++) {
3885 int merge = 0;
3886 int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
3887
3888 // Handle exactly sequential writes and overlapping writes.
3889 if (reqs[i].sector <= oldreq_last) {
3890 merge = 1;
3891 }
3892
3893 if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) {
3894 merge = 0;
3895 }
3896
3897 if (merge) {
3898 size_t size;
3899 QEMUIOVector *qiov = g_malloc0(sizeof(*qiov));
3900 qemu_iovec_init(qiov,
3901 reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
3902
3903 // Add the first request to the merged one. If the requests are
3904 // overlapping, drop the last sectors of the first request.
3905 size = (reqs[i].sector - reqs[outidx].sector) << 9;
3906 qemu_iovec_concat(qiov, reqs[outidx].qiov, 0, size);
3907
3908 // We should need to add any zeros between the two requests
3909 assert (reqs[i].sector <= oldreq_last);
3910
3911 // Add the second request
3912 qemu_iovec_concat(qiov, reqs[i].qiov, 0, reqs[i].qiov->size);
3913
3914 reqs[outidx].nb_sectors = qiov->size >> 9;
3915 reqs[outidx].qiov = qiov;
3916
3917 mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
3918 } else {
3919 outidx++;
3920 reqs[outidx].sector = reqs[i].sector;
3921 reqs[outidx].nb_sectors = reqs[i].nb_sectors;
3922 reqs[outidx].qiov = reqs[i].qiov;
3923 }
3924 }
3925
3926 return outidx + 1;
3927 }
3928
3929 /*
3930 * Submit multiple AIO write requests at once.
3931 *
3932 * On success, the function returns 0 and all requests in the reqs array have
3933 * been submitted. In error case this function returns -1, and any of the
3934 * requests may or may not be submitted yet. In particular, this means that the
3935 * callback will be called for some of the requests, for others it won't. The
3936 * caller must check the error field of the BlockRequest to wait for the right
3937 * callbacks (if error != 0, no callback will be called).
3938 *
3939 * The implementation may modify the contents of the reqs array, e.g. to merge
3940 * requests. However, the fields opaque and error are left unmodified as they
3941 * are used to signal failure for a single request to the caller.
3942 */
3943 int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
3944 {
3945 MultiwriteCB *mcb;
3946 int i;
3947
3948 /* don't submit writes if we don't have a medium */
3949 if (bs->drv == NULL) {
3950 for (i = 0; i < num_reqs; i++) {
3951 reqs[i].error = -ENOMEDIUM;
3952 }
3953 return -1;
3954 }
3955
3956 if (num_reqs == 0) {
3957 return 0;
3958 }
3959
3960 // Create MultiwriteCB structure
3961 mcb = g_malloc0(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
3962 mcb->num_requests = 0;
3963 mcb->num_callbacks = num_reqs;
3964
3965 for (i = 0; i < num_reqs; i++) {
3966 mcb->callbacks[i].cb = reqs[i].cb;
3967 mcb->callbacks[i].opaque = reqs[i].opaque;
3968 }
3969
3970 // Check for mergable requests
3971 num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
3972
3973 trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs);
3974
3975 /* Run the aio requests. */
3976 mcb->num_requests = num_reqs;
3977 for (i = 0; i < num_reqs; i++) {
3978 bdrv_co_aio_rw_vector(bs, reqs[i].sector, reqs[i].qiov,
3979 reqs[i].nb_sectors, reqs[i].flags,
3980 multiwrite_cb, mcb,
3981 true);
3982 }
3983
3984 return 0;
3985 }
3986
3987 void bdrv_aio_cancel(BlockDriverAIOCB *acb)
3988 {
3989 acb->aiocb_info->cancel(acb);
3990 }
3991
3992 /**************************************************************/
3993 /* async block device emulation */
3994
3995 typedef struct BlockDriverAIOCBSync {
3996 BlockDriverAIOCB common;
3997 QEMUBH *bh;
3998 int ret;
3999 /* vector translation state */
4000 QEMUIOVector *qiov;
4001