]> git.ipfire.org Git - thirdparty/qemu.git/blame - block.c
block: extract bdrv_setup_io_funcs()
[thirdparty/qemu.git] / block.c
CommitLineData
fc01f7e7
FB
1/*
2 * QEMU System Emulator block driver
5fafdf24 3 *
fc01f7e7 4 * Copyright (c) 2003 Fabrice Bellard
5fafdf24 5 *
fc01f7e7
FB
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
3990d09a 24#include "config-host.h"
faf07963 25#include "qemu-common.h"
6d519a5f 26#include "trace.h"
737e150e
PB
27#include "block/block_int.h"
28#include "block/blockjob.h"
1de7afc9 29#include "qemu/module.h"
7b1b5d19 30#include "qapi/qmp/qjson.h"
bfb197e0 31#include "sysemu/block-backend.h"
9c17d615 32#include "sysemu/sysemu.h"
de50a20a 33#include "sysemu/qtest.h"
1de7afc9 34#include "qemu/notify.h"
737e150e 35#include "block/coroutine.h"
c13163fb 36#include "block/qapi.h"
b2023818 37#include "qmp-commands.h"
1de7afc9 38#include "qemu/timer.h"
a5ee7bd4 39#include "qapi-event.h"
fc01f7e7 40
71e72a19 41#ifdef CONFIG_BSD
7674e7bf
FB
42#include <sys/types.h>
43#include <sys/stat.h>
44#include <sys/ioctl.h>
72cf2d4f 45#include <sys/queue.h>
c5e97233 46#ifndef __DragonFly__
7674e7bf
FB
47#include <sys/disk.h>
48#endif
c5e97233 49#endif
7674e7bf 50
49dc768d
AL
51#ifdef _WIN32
52#include <windows.h>
53#endif
54
9bd2b08f
JS
55/**
56 * A BdrvDirtyBitmap can be in three possible states:
57 * (1) successor is NULL and disabled is false: full r/w mode
58 * (2) successor is NULL and disabled is true: read only mode ("disabled")
59 * (3) successor is set: frozen mode.
60 * A frozen bitmap cannot be renamed, deleted, anonymized, cleared, set,
61 * or enabled. A frozen bitmap can only abdicate() or reclaim().
62 */
e4654d2d 63struct BdrvDirtyBitmap {
aa0c7ca5
JS
64 HBitmap *bitmap; /* Dirty sector bitmap implementation */
65 BdrvDirtyBitmap *successor; /* Anonymous child; implies frozen status */
66 char *name; /* Optional non-empty unique ID */
67 int64_t size; /* Size of the bitmap (Number of sectors) */
68 bool disabled; /* Bitmap is read-only */
e4654d2d
FZ
69 QLIST_ENTRY(BdrvDirtyBitmap) list;
70};
71
1c9805a3
SH
72#define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
73
7c84b1b8 74static BlockAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
f141eafe 75 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
097310b5 76 BlockCompletionFunc *cb, void *opaque);
7c84b1b8 77static BlockAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
f141eafe 78 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
097310b5 79 BlockCompletionFunc *cb, void *opaque);
f9f05dc5
KW
80static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs,
81 int64_t sector_num, int nb_sectors,
82 QEMUIOVector *iov);
83static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs,
84 int64_t sector_num, int nb_sectors,
85 QEMUIOVector *iov);
775aa8b6
KW
86static int coroutine_fn bdrv_co_do_preadv(BlockDriverState *bs,
87 int64_t offset, unsigned int bytes, QEMUIOVector *qiov,
470c0504 88 BdrvRequestFlags flags);
775aa8b6
KW
89static int coroutine_fn bdrv_co_do_pwritev(BlockDriverState *bs,
90 int64_t offset, unsigned int bytes, QEMUIOVector *qiov,
f08f2dda 91 BdrvRequestFlags flags);
7c84b1b8
MA
92static BlockAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs,
93 int64_t sector_num,
94 QEMUIOVector *qiov,
95 int nb_sectors,
96 BdrvRequestFlags flags,
097310b5 97 BlockCompletionFunc *cb,
7c84b1b8
MA
98 void *opaque,
99 bool is_write);
b2a61371 100static void coroutine_fn bdrv_co_do_rw(void *opaque);
621f0589 101static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs,
aa7bfbff 102 int64_t sector_num, int nb_sectors, BdrvRequestFlags flags);
ec530c81 103
1b7bdbc1
SH
104static QTAILQ_HEAD(, BlockDriverState) bdrv_states =
105 QTAILQ_HEAD_INITIALIZER(bdrv_states);
7ee930d0 106
dc364f4c
BC
107static QTAILQ_HEAD(, BlockDriverState) graph_bdrv_states =
108 QTAILQ_HEAD_INITIALIZER(graph_bdrv_states);
109
8a22f02a
SH
110static QLIST_HEAD(, BlockDriver) bdrv_drivers =
111 QLIST_HEAD_INITIALIZER(bdrv_drivers);
ea2384d3 112
ce1ffea8 113static void bdrv_dirty_bitmap_truncate(BlockDriverState *bs);
eb852011
MA
114/* If non-zero, use only whitelisted block drivers */
115static int use_bdrv_whitelist;
116
9e0b22f4
SH
117#ifdef _WIN32
118static int is_windows_drive_prefix(const char *filename)
119{
120 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
121 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
122 filename[1] == ':');
123}
124
125int is_windows_drive(const char *filename)
126{
127 if (is_windows_drive_prefix(filename) &&
128 filename[2] == '\0')
129 return 1;
130 if (strstart(filename, "\\\\.\\", NULL) ||
131 strstart(filename, "//./", NULL))
132 return 1;
133 return 0;
134}
135#endif
136
0563e191 137/* throttling disk I/O limits */
cc0681c4
BC
138void bdrv_set_io_limits(BlockDriverState *bs,
139 ThrottleConfig *cfg)
98f90dba 140{
cc0681c4 141 int i;
98f90dba 142
cc0681c4 143 throttle_config(&bs->throttle_state, cfg);
98f90dba 144
cc0681c4
BC
145 for (i = 0; i < 2; i++) {
146 qemu_co_enter_next(&bs->throttled_reqs[i]);
98f90dba 147 }
cc0681c4
BC
148}
149
150/* this function drain all the throttled IOs */
151static bool bdrv_start_throttled_reqs(BlockDriverState *bs)
152{
153 bool drained = false;
154 bool enabled = bs->io_limits_enabled;
155 int i;
156
157 bs->io_limits_enabled = false;
158
159 for (i = 0; i < 2; i++) {
160 while (qemu_co_enter_next(&bs->throttled_reqs[i])) {
161 drained = true;
162 }
163 }
164
165 bs->io_limits_enabled = enabled;
98f90dba 166
cc0681c4 167 return drained;
98f90dba
ZYW
168}
169
cc0681c4 170void bdrv_io_limits_disable(BlockDriverState *bs)
0563e191 171{
cc0681c4 172 bs->io_limits_enabled = false;
0563e191 173
cc0681c4
BC
174 bdrv_start_throttled_reqs(bs);
175
176 throttle_destroy(&bs->throttle_state);
0563e191
ZYW
177}
178
cc0681c4 179static void bdrv_throttle_read_timer_cb(void *opaque)
0563e191 180{
cc0681c4
BC
181 BlockDriverState *bs = opaque;
182 qemu_co_enter_next(&bs->throttled_reqs[0]);
0563e191
ZYW
183}
184
cc0681c4 185static void bdrv_throttle_write_timer_cb(void *opaque)
0563e191 186{
cc0681c4
BC
187 BlockDriverState *bs = opaque;
188 qemu_co_enter_next(&bs->throttled_reqs[1]);
0563e191
ZYW
189}
190
cc0681c4
BC
191/* should be called before bdrv_set_io_limits if a limit is set */
192void bdrv_io_limits_enable(BlockDriverState *bs)
193{
de50a20a
FZ
194 int clock_type = QEMU_CLOCK_REALTIME;
195
196 if (qtest_enabled()) {
197 /* For testing block IO throttling only */
198 clock_type = QEMU_CLOCK_VIRTUAL;
199 }
cc0681c4
BC
200 assert(!bs->io_limits_enabled);
201 throttle_init(&bs->throttle_state,
13af91eb 202 bdrv_get_aio_context(bs),
de50a20a 203 clock_type,
cc0681c4
BC
204 bdrv_throttle_read_timer_cb,
205 bdrv_throttle_write_timer_cb,
206 bs);
207 bs->io_limits_enabled = true;
208}
209
210/* This function makes an IO wait if needed
211 *
212 * @nb_sectors: the number of sectors of the IO
213 * @is_write: is the IO a write
214 */
98f90dba 215static void bdrv_io_limits_intercept(BlockDriverState *bs,
d5103588 216 unsigned int bytes,
cc0681c4 217 bool is_write)
98f90dba 218{
cc0681c4
BC
219 /* does this io must wait */
220 bool must_wait = throttle_schedule_timer(&bs->throttle_state, is_write);
98f90dba 221
cc0681c4
BC
222 /* if must wait or any request of this type throttled queue the IO */
223 if (must_wait ||
224 !qemu_co_queue_empty(&bs->throttled_reqs[is_write])) {
225 qemu_co_queue_wait(&bs->throttled_reqs[is_write]);
98f90dba
ZYW
226 }
227
cc0681c4 228 /* the IO will be executed, do the accounting */
d5103588
KW
229 throttle_account(&bs->throttle_state, is_write, bytes);
230
98f90dba 231
cc0681c4
BC
232 /* if the next request must wait -> do nothing */
233 if (throttle_schedule_timer(&bs->throttle_state, is_write)) {
234 return;
98f90dba
ZYW
235 }
236
cc0681c4
BC
237 /* else queue next request for execution */
238 qemu_co_queue_next(&bs->throttled_reqs[is_write]);
98f90dba
ZYW
239}
240
339064d5
KW
241size_t bdrv_opt_mem_align(BlockDriverState *bs)
242{
243 if (!bs || !bs->drv) {
244 /* 4k should be on the safe side */
245 return 4096;
246 }
247
248 return bs->bl.opt_mem_alignment;
249}
250
9e0b22f4 251/* check if the path starts with "<protocol>:" */
5c98415b 252int path_has_protocol(const char *path)
9e0b22f4 253{
947995c0
PB
254 const char *p;
255
9e0b22f4
SH
256#ifdef _WIN32
257 if (is_windows_drive(path) ||
258 is_windows_drive_prefix(path)) {
259 return 0;
260 }
947995c0
PB
261 p = path + strcspn(path, ":/\\");
262#else
263 p = path + strcspn(path, ":/");
9e0b22f4
SH
264#endif
265
947995c0 266 return *p == ':';
9e0b22f4
SH
267}
268
83f64091 269int path_is_absolute(const char *path)
3b0d4f61 270{
21664424
FB
271#ifdef _WIN32
272 /* specific case for names like: "\\.\d:" */
f53f4da9 273 if (is_windows_drive(path) || is_windows_drive_prefix(path)) {
21664424 274 return 1;
f53f4da9
PB
275 }
276 return (*path == '/' || *path == '\\');
3b9f94e1 277#else
f53f4da9 278 return (*path == '/');
3b9f94e1 279#endif
3b0d4f61
FB
280}
281
83f64091
FB
282/* if filename is absolute, just copy it to dest. Otherwise, build a
283 path to it by considering it is relative to base_path. URL are
284 supported. */
285void path_combine(char *dest, int dest_size,
286 const char *base_path,
287 const char *filename)
3b0d4f61 288{
83f64091
FB
289 const char *p, *p1;
290 int len;
291
292 if (dest_size <= 0)
293 return;
294 if (path_is_absolute(filename)) {
295 pstrcpy(dest, dest_size, filename);
296 } else {
297 p = strchr(base_path, ':');
298 if (p)
299 p++;
300 else
301 p = base_path;
3b9f94e1
FB
302 p1 = strrchr(base_path, '/');
303#ifdef _WIN32
304 {
305 const char *p2;
306 p2 = strrchr(base_path, '\\');
307 if (!p1 || p2 > p1)
308 p1 = p2;
309 }
310#endif
83f64091
FB
311 if (p1)
312 p1++;
313 else
314 p1 = base_path;
315 if (p1 > p)
316 p = p1;
317 len = p - base_path;
318 if (len > dest_size - 1)
319 len = dest_size - 1;
320 memcpy(dest, base_path, len);
321 dest[len] = '\0';
322 pstrcat(dest, dest_size, filename);
3b0d4f61 323 }
3b0d4f61
FB
324}
325
0a82855a
HR
326void bdrv_get_full_backing_filename_from_filename(const char *backed,
327 const char *backing,
9f07429e
HR
328 char *dest, size_t sz,
329 Error **errp)
dc5a1371 330{
9f07429e
HR
331 if (backing[0] == '\0' || path_has_protocol(backing) ||
332 path_is_absolute(backing))
333 {
0a82855a 334 pstrcpy(dest, sz, backing);
9f07429e
HR
335 } else if (backed[0] == '\0' || strstart(backed, "json:", NULL)) {
336 error_setg(errp, "Cannot use relative backing file names for '%s'",
337 backed);
dc5a1371 338 } else {
0a82855a 339 path_combine(dest, sz, backed, backing);
dc5a1371
PB
340 }
341}
342
9f07429e
HR
343void bdrv_get_full_backing_filename(BlockDriverState *bs, char *dest, size_t sz,
344 Error **errp)
0a82855a 345{
9f07429e
HR
346 char *backed = bs->exact_filename[0] ? bs->exact_filename : bs->filename;
347
348 bdrv_get_full_backing_filename_from_filename(backed, bs->backing_file,
349 dest, sz, errp);
0a82855a
HR
350}
351
0eb7217e 352void bdrv_setup_io_funcs(BlockDriver *bdrv)
ea2384d3 353{
8c5873d6
SH
354 /* Block drivers without coroutine functions need emulation */
355 if (!bdrv->bdrv_co_readv) {
f9f05dc5
KW
356 bdrv->bdrv_co_readv = bdrv_co_readv_em;
357 bdrv->bdrv_co_writev = bdrv_co_writev_em;
358
f8c35c1d
SH
359 /* bdrv_co_readv_em()/brdv_co_writev_em() work in terms of aio, so if
360 * the block driver lacks aio we need to emulate that too.
361 */
f9f05dc5
KW
362 if (!bdrv->bdrv_aio_readv) {
363 /* add AIO emulation layer */
364 bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
365 bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
f9f05dc5 366 }
83f64091 367 }
0eb7217e
SH
368}
369
370void bdrv_register(BlockDriver *bdrv)
371{
372 bdrv_setup_io_funcs(bdrv);
b2e12bc6 373
8a22f02a 374 QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
ea2384d3 375}
b338082b 376
7f06d47e 377BlockDriverState *bdrv_new_root(void)
b338082b 378{
7f06d47e 379 BlockDriverState *bs = bdrv_new();
e4e9986b 380
e4e9986b 381 QTAILQ_INSERT_TAIL(&bdrv_states, bs, device_list);
e4e9986b
MA
382 return bs;
383}
384
385BlockDriverState *bdrv_new(void)
386{
387 BlockDriverState *bs;
388 int i;
389
5839e53b 390 bs = g_new0(BlockDriverState, 1);
e4654d2d 391 QLIST_INIT(&bs->dirty_bitmaps);
fbe40ff7
FZ
392 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
393 QLIST_INIT(&bs->op_blockers[i]);
394 }
28a7282a 395 bdrv_iostatus_disable(bs);
d7d512f6 396 notifier_list_init(&bs->close_notifiers);
d616b224 397 notifier_with_return_list_init(&bs->before_write_notifiers);
cc0681c4
BC
398 qemu_co_queue_init(&bs->throttled_reqs[0]);
399 qemu_co_queue_init(&bs->throttled_reqs[1]);
9fcb0251 400 bs->refcnt = 1;
dcd04228 401 bs->aio_context = qemu_get_aio_context();
d7d512f6 402
b338082b
FB
403 return bs;
404}
405
d7d512f6
PB
406void bdrv_add_close_notifier(BlockDriverState *bs, Notifier *notify)
407{
408 notifier_list_add(&bs->close_notifiers, notify);
409}
410
ea2384d3
FB
411BlockDriver *bdrv_find_format(const char *format_name)
412{
413 BlockDriver *drv1;
8a22f02a
SH
414 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
415 if (!strcmp(drv1->format_name, format_name)) {
ea2384d3 416 return drv1;
8a22f02a 417 }
ea2384d3
FB
418 }
419 return NULL;
420}
421
b64ec4e4 422static int bdrv_is_whitelisted(BlockDriver *drv, bool read_only)
eb852011 423{
b64ec4e4
FZ
424 static const char *whitelist_rw[] = {
425 CONFIG_BDRV_RW_WHITELIST
426 };
427 static const char *whitelist_ro[] = {
428 CONFIG_BDRV_RO_WHITELIST
eb852011
MA
429 };
430 const char **p;
431
b64ec4e4 432 if (!whitelist_rw[0] && !whitelist_ro[0]) {
eb852011 433 return 1; /* no whitelist, anything goes */
b64ec4e4 434 }
eb852011 435
b64ec4e4 436 for (p = whitelist_rw; *p; p++) {
eb852011
MA
437 if (!strcmp(drv->format_name, *p)) {
438 return 1;
439 }
440 }
b64ec4e4
FZ
441 if (read_only) {
442 for (p = whitelist_ro; *p; p++) {
443 if (!strcmp(drv->format_name, *p)) {
444 return 1;
445 }
446 }
447 }
eb852011
MA
448 return 0;
449}
450
b64ec4e4
FZ
451BlockDriver *bdrv_find_whitelisted_format(const char *format_name,
452 bool read_only)
eb852011
MA
453{
454 BlockDriver *drv = bdrv_find_format(format_name);
b64ec4e4 455 return drv && bdrv_is_whitelisted(drv, read_only) ? drv : NULL;
eb852011
MA
456}
457
5b7e1542
ZYW
458typedef struct CreateCo {
459 BlockDriver *drv;
460 char *filename;
83d0521a 461 QemuOpts *opts;
5b7e1542 462 int ret;
cc84d90f 463 Error *err;
5b7e1542
ZYW
464} CreateCo;
465
466static void coroutine_fn bdrv_create_co_entry(void *opaque)
467{
cc84d90f
HR
468 Error *local_err = NULL;
469 int ret;
470
5b7e1542
ZYW
471 CreateCo *cco = opaque;
472 assert(cco->drv);
473
c282e1fd 474 ret = cco->drv->bdrv_create(cco->filename, cco->opts, &local_err);
84d18f06 475 if (local_err) {
cc84d90f
HR
476 error_propagate(&cco->err, local_err);
477 }
478 cco->ret = ret;
5b7e1542
ZYW
479}
480
0e7e1989 481int bdrv_create(BlockDriver *drv, const char* filename,
83d0521a 482 QemuOpts *opts, Error **errp)
ea2384d3 483{
5b7e1542
ZYW
484 int ret;
485
486 Coroutine *co;
487 CreateCo cco = {
488 .drv = drv,
489 .filename = g_strdup(filename),
83d0521a 490 .opts = opts,
5b7e1542 491 .ret = NOT_DONE,
cc84d90f 492 .err = NULL,
5b7e1542
ZYW
493 };
494
c282e1fd 495 if (!drv->bdrv_create) {
cc84d90f 496 error_setg(errp, "Driver '%s' does not support image creation", drv->format_name);
80168bff
LC
497 ret = -ENOTSUP;
498 goto out;
5b7e1542
ZYW
499 }
500
501 if (qemu_in_coroutine()) {
502 /* Fast-path if already in coroutine context */
503 bdrv_create_co_entry(&cco);
504 } else {
505 co = qemu_coroutine_create(bdrv_create_co_entry);
506 qemu_coroutine_enter(co, &cco);
507 while (cco.ret == NOT_DONE) {
b47ec2c4 508 aio_poll(qemu_get_aio_context(), true);
5b7e1542
ZYW
509 }
510 }
511
512 ret = cco.ret;
cc84d90f 513 if (ret < 0) {
84d18f06 514 if (cco.err) {
cc84d90f
HR
515 error_propagate(errp, cco.err);
516 } else {
517 error_setg_errno(errp, -ret, "Could not create image");
518 }
519 }
0e7e1989 520
80168bff
LC
521out:
522 g_free(cco.filename);
5b7e1542 523 return ret;
ea2384d3
FB
524}
525
c282e1fd 526int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp)
84a12e66
CH
527{
528 BlockDriver *drv;
cc84d90f
HR
529 Error *local_err = NULL;
530 int ret;
84a12e66 531
b65a5e12 532 drv = bdrv_find_protocol(filename, true, errp);
84a12e66 533 if (drv == NULL) {
16905d71 534 return -ENOENT;
84a12e66
CH
535 }
536
c282e1fd 537 ret = bdrv_create(drv, filename, opts, &local_err);
84d18f06 538 if (local_err) {
cc84d90f
HR
539 error_propagate(errp, local_err);
540 }
541 return ret;
84a12e66
CH
542}
543
3baca891 544void bdrv_refresh_limits(BlockDriverState *bs, Error **errp)
d34682cd
KW
545{
546 BlockDriver *drv = bs->drv;
3baca891 547 Error *local_err = NULL;
d34682cd
KW
548
549 memset(&bs->bl, 0, sizeof(bs->bl));
550
466ad822 551 if (!drv) {
3baca891 552 return;
466ad822
KW
553 }
554
555 /* Take some limits from the children as a default */
556 if (bs->file) {
3baca891
KW
557 bdrv_refresh_limits(bs->file, &local_err);
558 if (local_err) {
559 error_propagate(errp, local_err);
560 return;
561 }
466ad822 562 bs->bl.opt_transfer_length = bs->file->bl.opt_transfer_length;
2647fab5 563 bs->bl.max_transfer_length = bs->file->bl.max_transfer_length;
339064d5
KW
564 bs->bl.opt_mem_alignment = bs->file->bl.opt_mem_alignment;
565 } else {
566 bs->bl.opt_mem_alignment = 512;
466ad822
KW
567 }
568
569 if (bs->backing_hd) {
3baca891
KW
570 bdrv_refresh_limits(bs->backing_hd, &local_err);
571 if (local_err) {
572 error_propagate(errp, local_err);
573 return;
574 }
466ad822
KW
575 bs->bl.opt_transfer_length =
576 MAX(bs->bl.opt_transfer_length,
577 bs->backing_hd->bl.opt_transfer_length);
2647fab5
PL
578 bs->bl.max_transfer_length =
579 MIN_NON_ZERO(bs->bl.max_transfer_length,
580 bs->backing_hd->bl.max_transfer_length);
339064d5
KW
581 bs->bl.opt_mem_alignment =
582 MAX(bs->bl.opt_mem_alignment,
583 bs->backing_hd->bl.opt_mem_alignment);
466ad822
KW
584 }
585
586 /* Then let the driver override it */
587 if (drv->bdrv_refresh_limits) {
3baca891 588 drv->bdrv_refresh_limits(bs, errp);
d34682cd 589 }
d34682cd
KW
590}
591
892b7de8
ET
592/**
593 * Try to get @bs's logical and physical block size.
594 * On success, store them in @bsz struct and return 0.
595 * On failure return -errno.
596 * @bs must not be empty.
597 */
598int bdrv_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz)
599{
600 BlockDriver *drv = bs->drv;
601
602 if (drv && drv->bdrv_probe_blocksizes) {
603 return drv->bdrv_probe_blocksizes(bs, bsz);
604 }
605
606 return -ENOTSUP;
607}
608
609/**
610 * Try to get @bs's geometry (cyls, heads, sectors).
611 * On success, store them in @geo struct and return 0.
612 * On failure return -errno.
613 * @bs must not be empty.
614 */
615int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo)
616{
617 BlockDriver *drv = bs->drv;
618
619 if (drv && drv->bdrv_probe_geometry) {
620 return drv->bdrv_probe_geometry(bs, geo);
621 }
622
623 return -ENOTSUP;
624}
625
eba25057
JM
626/*
627 * Create a uniquely-named empty temporary file.
628 * Return 0 upon success, otherwise a negative errno value.
629 */
630int get_tmp_filename(char *filename, int size)
d5249393 631{
eba25057 632#ifdef _WIN32
3b9f94e1 633 char temp_dir[MAX_PATH];
eba25057
JM
634 /* GetTempFileName requires that its output buffer (4th param)
635 have length MAX_PATH or greater. */
636 assert(size >= MAX_PATH);
637 return (GetTempPath(MAX_PATH, temp_dir)
638 && GetTempFileName(temp_dir, "qem", 0, filename)
639 ? 0 : -GetLastError());
d5249393 640#else
67b915a5 641 int fd;
7ccfb2eb 642 const char *tmpdir;
0badc1ee 643 tmpdir = getenv("TMPDIR");
69bef793
AS
644 if (!tmpdir) {
645 tmpdir = "/var/tmp";
646 }
eba25057
JM
647 if (snprintf(filename, size, "%s/vl.XXXXXX", tmpdir) >= size) {
648 return -EOVERFLOW;
649 }
ea2384d3 650 fd = mkstemp(filename);
fe235a06
DH
651 if (fd < 0) {
652 return -errno;
653 }
654 if (close(fd) != 0) {
655 unlink(filename);
eba25057
JM
656 return -errno;
657 }
658 return 0;
d5249393 659#endif
eba25057 660}
fc01f7e7 661
84a12e66
CH
662/*
663 * Detect host devices. By convention, /dev/cdrom[N] is always
664 * recognized as a host CDROM.
665 */
666static BlockDriver *find_hdev_driver(const char *filename)
667{
668 int score_max = 0, score;
669 BlockDriver *drv = NULL, *d;
670
671 QLIST_FOREACH(d, &bdrv_drivers, list) {
672 if (d->bdrv_probe_device) {
673 score = d->bdrv_probe_device(filename);
674 if (score > score_max) {
675 score_max = score;
676 drv = d;
677 }
678 }
679 }
680
681 return drv;
682}
683
98289620 684BlockDriver *bdrv_find_protocol(const char *filename,
b65a5e12
HR
685 bool allow_protocol_prefix,
686 Error **errp)
83f64091
FB
687{
688 BlockDriver *drv1;
689 char protocol[128];
1cec71e3 690 int len;
83f64091 691 const char *p;
19cb3738 692
66f82cee
KW
693 /* TODO Drivers without bdrv_file_open must be specified explicitly */
694
39508e7a
CH
695 /*
696 * XXX(hch): we really should not let host device detection
697 * override an explicit protocol specification, but moving this
698 * later breaks access to device names with colons in them.
699 * Thanks to the brain-dead persistent naming schemes on udev-
700 * based Linux systems those actually are quite common.
701 */
702 drv1 = find_hdev_driver(filename);
703 if (drv1) {
704 return drv1;
705 }
706
98289620 707 if (!path_has_protocol(filename) || !allow_protocol_prefix) {
ef810437 708 return &bdrv_file;
84a12e66 709 }
98289620 710
9e0b22f4
SH
711 p = strchr(filename, ':');
712 assert(p != NULL);
1cec71e3
AL
713 len = p - filename;
714 if (len > sizeof(protocol) - 1)
715 len = sizeof(protocol) - 1;
716 memcpy(protocol, filename, len);
717 protocol[len] = '\0';
8a22f02a 718 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
5fafdf24 719 if (drv1->protocol_name &&
8a22f02a 720 !strcmp(drv1->protocol_name, protocol)) {
83f64091 721 return drv1;
8a22f02a 722 }
83f64091 723 }
b65a5e12
HR
724
725 error_setg(errp, "Unknown protocol '%s'", protocol);
83f64091
FB
726 return NULL;
727}
728
c6684249
MA
729/*
730 * Guess image format by probing its contents.
731 * This is not a good idea when your image is raw (CVE-2008-2004), but
732 * we do it anyway for backward compatibility.
733 *
734 * @buf contains the image's first @buf_size bytes.
7cddd372
KW
735 * @buf_size is the buffer size in bytes (generally BLOCK_PROBE_BUF_SIZE,
736 * but can be smaller if the image file is smaller)
c6684249
MA
737 * @filename is its filename.
738 *
739 * For all block drivers, call the bdrv_probe() method to get its
740 * probing score.
741 * Return the first block driver with the highest probing score.
742 */
38f3ef57
KW
743BlockDriver *bdrv_probe_all(const uint8_t *buf, int buf_size,
744 const char *filename)
c6684249
MA
745{
746 int score_max = 0, score;
747 BlockDriver *drv = NULL, *d;
748
749 QLIST_FOREACH(d, &bdrv_drivers, list) {
750 if (d->bdrv_probe) {
751 score = d->bdrv_probe(buf, buf_size, filename);
752 if (score > score_max) {
753 score_max = score;
754 drv = d;
755 }
756 }
757 }
758
759 return drv;
760}
761
f500a6d3 762static int find_image_format(BlockDriverState *bs, const char *filename,
34b5d2c6 763 BlockDriver **pdrv, Error **errp)
f3a5d3f8 764{
c6684249 765 BlockDriver *drv;
7cddd372 766 uint8_t buf[BLOCK_PROBE_BUF_SIZE];
f500a6d3 767 int ret = 0;
f8ea0b00 768
08a00559 769 /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
8e895599 770 if (bs->sg || !bdrv_is_inserted(bs) || bdrv_getlength(bs) == 0) {
ef810437 771 *pdrv = &bdrv_raw;
c98ac35d 772 return ret;
1a396859 773 }
f8ea0b00 774
83f64091 775 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
83f64091 776 if (ret < 0) {
34b5d2c6
HR
777 error_setg_errno(errp, -ret, "Could not read image for determining its "
778 "format");
c98ac35d
SW
779 *pdrv = NULL;
780 return ret;
83f64091
FB
781 }
782
c6684249 783 drv = bdrv_probe_all(buf, ret, filename);
c98ac35d 784 if (!drv) {
34b5d2c6
HR
785 error_setg(errp, "Could not determine image format: No compatible "
786 "driver found");
c98ac35d
SW
787 ret = -ENOENT;
788 }
789 *pdrv = drv;
790 return ret;
ea2384d3
FB
791}
792
51762288
SH
793/**
794 * Set the current 'total_sectors' value
65a9bb25 795 * Return 0 on success, -errno on error.
51762288
SH
796 */
797static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
798{
799 BlockDriver *drv = bs->drv;
800
396759ad
NB
801 /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
802 if (bs->sg)
803 return 0;
804
51762288
SH
805 /* query actual device if possible, otherwise just trust the hint */
806 if (drv->bdrv_getlength) {
807 int64_t length = drv->bdrv_getlength(bs);
808 if (length < 0) {
809 return length;
810 }
7e382003 811 hint = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE);
51762288
SH
812 }
813
814 bs->total_sectors = hint;
815 return 0;
816}
817
9e8f1835
PB
818/**
819 * Set open flags for a given discard mode
820 *
821 * Return 0 on success, -1 if the discard mode was invalid.
822 */
823int bdrv_parse_discard_flags(const char *mode, int *flags)
824{
825 *flags &= ~BDRV_O_UNMAP;
826
827 if (!strcmp(mode, "off") || !strcmp(mode, "ignore")) {
828 /* do nothing */
829 } else if (!strcmp(mode, "on") || !strcmp(mode, "unmap")) {
830 *flags |= BDRV_O_UNMAP;
831 } else {
832 return -1;
833 }
834
835 return 0;
836}
837
c3993cdc
SH
838/**
839 * Set open flags for a given cache mode
840 *
841 * Return 0 on success, -1 if the cache mode was invalid.
842 */
843int bdrv_parse_cache_flags(const char *mode, int *flags)
844{
845 *flags &= ~BDRV_O_CACHE_MASK;
846
847 if (!strcmp(mode, "off") || !strcmp(mode, "none")) {
848 *flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
92196b2f
SH
849 } else if (!strcmp(mode, "directsync")) {
850 *flags |= BDRV_O_NOCACHE;
c3993cdc
SH
851 } else if (!strcmp(mode, "writeback")) {
852 *flags |= BDRV_O_CACHE_WB;
853 } else if (!strcmp(mode, "unsafe")) {
854 *flags |= BDRV_O_CACHE_WB;
855 *flags |= BDRV_O_NO_FLUSH;
856 } else if (!strcmp(mode, "writethrough")) {
857 /* this is the default */
858 } else {
859 return -1;
860 }
861
862 return 0;
863}
864
53fec9d3
SH
865/**
866 * The copy-on-read flag is actually a reference count so multiple users may
867 * use the feature without worrying about clobbering its previous state.
868 * Copy-on-read stays enabled until all users have called to disable it.
869 */
870void bdrv_enable_copy_on_read(BlockDriverState *bs)
871{
872 bs->copy_on_read++;
873}
874
875void bdrv_disable_copy_on_read(BlockDriverState *bs)
876{
877 assert(bs->copy_on_read > 0);
878 bs->copy_on_read--;
879}
880
b1e6fc08
KW
881/*
882 * Returns the flags that a temporary snapshot should get, based on the
883 * originally requested flags (the originally requested image will have flags
884 * like a backing file)
885 */
886static int bdrv_temp_snapshot_flags(int flags)
887{
888 return (flags & ~BDRV_O_SNAPSHOT) | BDRV_O_TEMPORARY;
889}
890
0b50cc88
KW
891/*
892 * Returns the flags that bs->file should get, based on the given flags for
893 * the parent BDS
894 */
895static int bdrv_inherited_flags(int flags)
896{
897 /* Enable protocol handling, disable format probing for bs->file */
898 flags |= BDRV_O_PROTOCOL;
899
900 /* Our block drivers take care to send flushes and respect unmap policy,
901 * so we can enable both unconditionally on lower layers. */
902 flags |= BDRV_O_CACHE_WB | BDRV_O_UNMAP;
903
0b50cc88 904 /* Clear flags that only apply to the top layer */
5669b44d 905 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ);
0b50cc88
KW
906
907 return flags;
908}
909
317fc44e
KW
910/*
911 * Returns the flags that bs->backing_hd should get, based on the given flags
912 * for the parent BDS
913 */
914static int bdrv_backing_flags(int flags)
915{
916 /* backing files always opened read-only */
917 flags &= ~(BDRV_O_RDWR | BDRV_O_COPY_ON_READ);
918
919 /* snapshot=on is handled on the top layer */
8bfea15d 920 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_TEMPORARY);
317fc44e
KW
921
922 return flags;
923}
924
7b272452
KW
925static int bdrv_open_flags(BlockDriverState *bs, int flags)
926{
927 int open_flags = flags | BDRV_O_CACHE_WB;
928
929 /*
930 * Clear flags that are internal to the block layer before opening the
931 * image.
932 */
20cca275 933 open_flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_PROTOCOL);
7b272452
KW
934
935 /*
936 * Snapshots should be writable.
937 */
8bfea15d 938 if (flags & BDRV_O_TEMPORARY) {
7b272452
KW
939 open_flags |= BDRV_O_RDWR;
940 }
941
942 return open_flags;
943}
944
636ea370
KW
945static void bdrv_assign_node_name(BlockDriverState *bs,
946 const char *node_name,
947 Error **errp)
6913c0c2
BC
948{
949 if (!node_name) {
636ea370 950 return;
6913c0c2
BC
951 }
952
9aebf3b8 953 /* Check for empty string or invalid characters */
f5bebbbb 954 if (!id_wellformed(node_name)) {
9aebf3b8 955 error_setg(errp, "Invalid node name");
636ea370 956 return;
6913c0c2
BC
957 }
958
0c5e94ee 959 /* takes care of avoiding namespaces collisions */
7f06d47e 960 if (blk_by_name(node_name)) {
0c5e94ee
BC
961 error_setg(errp, "node-name=%s is conflicting with a device id",
962 node_name);
636ea370 963 return;
0c5e94ee
BC
964 }
965
6913c0c2
BC
966 /* takes care of avoiding duplicates node names */
967 if (bdrv_find_node(node_name)) {
968 error_setg(errp, "Duplicate node name");
636ea370 969 return;
6913c0c2
BC
970 }
971
972 /* copy node name into the bs and insert it into the graph list */
973 pstrcpy(bs->node_name, sizeof(bs->node_name), node_name);
974 QTAILQ_INSERT_TAIL(&graph_bdrv_states, bs, node_list);
6913c0c2
BC
975}
976
57915332
KW
977/*
978 * Common part for opening disk images and files
b6ad491a
KW
979 *
980 * Removes all processed options from *options.
57915332 981 */
f500a6d3 982static int bdrv_open_common(BlockDriverState *bs, BlockDriverState *file,
34b5d2c6 983 QDict *options, int flags, BlockDriver *drv, Error **errp)
57915332
KW
984{
985 int ret, open_flags;
035fccdf 986 const char *filename;
6913c0c2 987 const char *node_name = NULL;
34b5d2c6 988 Error *local_err = NULL;
57915332
KW
989
990 assert(drv != NULL);
6405875c 991 assert(bs->file == NULL);
707ff828 992 assert(options != NULL && bs->options != options);
57915332 993
45673671
KW
994 if (file != NULL) {
995 filename = file->filename;
996 } else {
997 filename = qdict_get_try_str(options, "filename");
998 }
999
765003db
KW
1000 if (drv->bdrv_needs_filename && !filename) {
1001 error_setg(errp, "The '%s' block driver requires a file name",
1002 drv->format_name);
1003 return -EINVAL;
1004 }
1005
45673671 1006 trace_bdrv_open_common(bs, filename ?: "", flags, drv->format_name);
28dcee10 1007
6913c0c2 1008 node_name = qdict_get_try_str(options, "node-name");
636ea370 1009 bdrv_assign_node_name(bs, node_name, &local_err);
0fb6395c 1010 if (local_err) {
636ea370
KW
1011 error_propagate(errp, local_err);
1012 return -EINVAL;
6913c0c2
BC
1013 }
1014 qdict_del(options, "node-name");
1015
5d186eb0
KW
1016 /* bdrv_open() with directly using a protocol as drv. This layer is already
1017 * opened, so assign it to bs (while file becomes a closed BlockDriverState)
1018 * and return immediately. */
1019 if (file != NULL && drv->bdrv_file_open) {
1020 bdrv_swap(file, bs);
1021 return 0;
1022 }
1023
57915332 1024 bs->open_flags = flags;
1b7fd729 1025 bs->guest_block_size = 512;
c25f53b0 1026 bs->request_alignment = 512;
0d51b4de 1027 bs->zero_beyond_eof = true;
b64ec4e4
FZ
1028 open_flags = bdrv_open_flags(bs, flags);
1029 bs->read_only = !(open_flags & BDRV_O_RDWR);
1030
1031 if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, bs->read_only)) {
8f94a6e4
KW
1032 error_setg(errp,
1033 !bs->read_only && bdrv_is_whitelisted(drv, true)
1034 ? "Driver '%s' can only be used for read-only devices"
1035 : "Driver '%s' is not whitelisted",
1036 drv->format_name);
b64ec4e4
FZ
1037 return -ENOTSUP;
1038 }
57915332 1039
53fec9d3 1040 assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */
0ebd24e0
KW
1041 if (flags & BDRV_O_COPY_ON_READ) {
1042 if (!bs->read_only) {
1043 bdrv_enable_copy_on_read(bs);
1044 } else {
1045 error_setg(errp, "Can't use copy-on-read on read-only device");
1046 return -EINVAL;
1047 }
53fec9d3
SH
1048 }
1049
c2ad1b0c
KW
1050 if (filename != NULL) {
1051 pstrcpy(bs->filename, sizeof(bs->filename), filename);
1052 } else {
1053 bs->filename[0] = '\0';
1054 }
91af7014 1055 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), bs->filename);
57915332 1056
57915332 1057 bs->drv = drv;
7267c094 1058 bs->opaque = g_malloc0(drv->instance_size);
57915332 1059
03f541bd 1060 bs->enable_write_cache = !!(flags & BDRV_O_CACHE_WB);
e7c63796 1061
66f82cee
KW
1062 /* Open the image, either directly or using a protocol */
1063 if (drv->bdrv_file_open) {
5d186eb0 1064 assert(file == NULL);
030be321 1065 assert(!drv->bdrv_needs_filename || filename != NULL);
34b5d2c6 1066 ret = drv->bdrv_file_open(bs, options, open_flags, &local_err);
f500a6d3 1067 } else {
2af5ef70 1068 if (file == NULL) {
34b5d2c6
HR
1069 error_setg(errp, "Can't use '%s' as a block driver for the "
1070 "protocol level", drv->format_name);
2af5ef70
KW
1071 ret = -EINVAL;
1072 goto free_and_fail;
1073 }
f500a6d3 1074 bs->file = file;
34b5d2c6 1075 ret = drv->bdrv_open(bs, options, open_flags, &local_err);
66f82cee
KW
1076 }
1077
57915332 1078 if (ret < 0) {
84d18f06 1079 if (local_err) {
34b5d2c6 1080 error_propagate(errp, local_err);
2fa9aa59
DH
1081 } else if (bs->filename[0]) {
1082 error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename);
34b5d2c6
HR
1083 } else {
1084 error_setg_errno(errp, -ret, "Could not open image");
1085 }
57915332
KW
1086 goto free_and_fail;
1087 }
1088
a1f688f4
MA
1089 if (bs->encrypted) {
1090 error_report("Encrypted images are deprecated");
1091 error_printf("Support for them will be removed in a future release.\n"
1092 "You can use 'qemu-img convert' to convert your image"
1093 " to an unencrypted one.\n");
1094 }
1095
51762288
SH
1096 ret = refresh_total_sectors(bs, bs->total_sectors);
1097 if (ret < 0) {
34b5d2c6 1098 error_setg_errno(errp, -ret, "Could not refresh total sector count");
51762288 1099 goto free_and_fail;
57915332 1100 }
51762288 1101
3baca891
KW
1102 bdrv_refresh_limits(bs, &local_err);
1103 if (local_err) {
1104 error_propagate(errp, local_err);
1105 ret = -EINVAL;
1106 goto free_and_fail;
1107 }
1108
c25f53b0 1109 assert(bdrv_opt_mem_align(bs) != 0);
47ea2de2 1110 assert((bs->request_alignment != 0) || bs->sg);
57915332
KW
1111 return 0;
1112
1113free_and_fail:
f500a6d3 1114 bs->file = NULL;
7267c094 1115 g_free(bs->opaque);
57915332
KW
1116 bs->opaque = NULL;
1117 bs->drv = NULL;
1118 return ret;
1119}
1120
5e5c4f63
KW
1121static QDict *parse_json_filename(const char *filename, Error **errp)
1122{
1123 QObject *options_obj;
1124 QDict *options;
1125 int ret;
1126
1127 ret = strstart(filename, "json:", &filename);
1128 assert(ret);
1129
1130 options_obj = qobject_from_json(filename);
1131 if (!options_obj) {
1132 error_setg(errp, "Could not parse the JSON options");
1133 return NULL;
1134 }
1135
1136 if (qobject_type(options_obj) != QTYPE_QDICT) {
1137 qobject_decref(options_obj);
1138 error_setg(errp, "Invalid JSON object given");
1139 return NULL;
1140 }
1141
1142 options = qobject_to_qdict(options_obj);
1143 qdict_flatten(options);
1144
1145 return options;
1146}
1147
b6ce07aa 1148/*
f54120ff
KW
1149 * Fills in default options for opening images and converts the legacy
1150 * filename/flags pair to option QDict entries.
b6ce07aa 1151 */
5e5c4f63 1152static int bdrv_fill_options(QDict **options, const char **pfilename, int flags,
17b005f1 1153 BlockDriver *drv, Error **errp)
ea2384d3 1154{
5e5c4f63 1155 const char *filename = *pfilename;
c2ad1b0c 1156 const char *drvname;
462f5bcf 1157 bool protocol = flags & BDRV_O_PROTOCOL;
e3fa4bfa 1158 bool parse_filename = false;
34b5d2c6 1159 Error *local_err = NULL;
83f64091 1160
5e5c4f63
KW
1161 /* Parse json: pseudo-protocol */
1162 if (filename && g_str_has_prefix(filename, "json:")) {
1163 QDict *json_options = parse_json_filename(filename, &local_err);
1164 if (local_err) {
1165 error_propagate(errp, local_err);
1166 return -EINVAL;
1167 }
1168
1169 /* Options given in the filename have lower priority than options
1170 * specified directly */
1171 qdict_join(*options, json_options, false);
1172 QDECREF(json_options);
1173 *pfilename = filename = NULL;
1174 }
1175
035fccdf 1176 /* Fetch the file name from the options QDict if necessary */
17b005f1 1177 if (protocol && filename) {
f54120ff
KW
1178 if (!qdict_haskey(*options, "filename")) {
1179 qdict_put(*options, "filename", qstring_from_str(filename));
1180 parse_filename = true;
1181 } else {
1182 error_setg(errp, "Can't specify 'file' and 'filename' options at "
1183 "the same time");
1184 return -EINVAL;
1185 }
035fccdf
KW
1186 }
1187
c2ad1b0c 1188 /* Find the right block driver */
f54120ff 1189 filename = qdict_get_try_str(*options, "filename");
5acd9d81 1190 drvname = qdict_get_try_str(*options, "driver");
f54120ff 1191
17b005f1
KW
1192 if (drv) {
1193 if (drvname) {
1194 error_setg(errp, "Driver specified twice");
1195 return -EINVAL;
1196 }
1197 drvname = drv->format_name;
1198 qdict_put(*options, "driver", qstring_from_str(drvname));
1199 } else {
1200 if (!drvname && protocol) {
1201 if (filename) {
b65a5e12 1202 drv = bdrv_find_protocol(filename, parse_filename, errp);
17b005f1 1203 if (!drv) {
17b005f1
KW
1204 return -EINVAL;
1205 }
1206
1207 drvname = drv->format_name;
1208 qdict_put(*options, "driver", qstring_from_str(drvname));
1209 } else {
1210 error_setg(errp, "Must specify either driver or file");
f54120ff
KW
1211 return -EINVAL;
1212 }
17b005f1
KW
1213 } else if (drvname) {
1214 drv = bdrv_find_format(drvname);
1215 if (!drv) {
1216 error_setg(errp, "Unknown driver '%s'", drvname);
1217 return -ENOENT;
1218 }
98289620 1219 }
c2ad1b0c
KW
1220 }
1221
17b005f1 1222 assert(drv || !protocol);
c2ad1b0c 1223
f54120ff 1224 /* Driver-specific filename parsing */
17b005f1 1225 if (drv && drv->bdrv_parse_filename && parse_filename) {
5acd9d81 1226 drv->bdrv_parse_filename(filename, *options, &local_err);
84d18f06 1227 if (local_err) {
34b5d2c6 1228 error_propagate(errp, local_err);
f54120ff 1229 return -EINVAL;
6963a30d 1230 }
cd5d031e
HR
1231
1232 if (!drv->bdrv_needs_filename) {
1233 qdict_del(*options, "filename");
cd5d031e 1234 }
6963a30d
KW
1235 }
1236
f54120ff
KW
1237 return 0;
1238}
1239
8d24cce1
FZ
1240void bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd)
1241{
1242
826b6ca0
FZ
1243 if (bs->backing_hd) {
1244 assert(bs->backing_blocker);
1245 bdrv_op_unblock_all(bs->backing_hd, bs->backing_blocker);
1246 } else if (backing_hd) {
1247 error_setg(&bs->backing_blocker,
81e5f78a
AG
1248 "node is used as backing hd of '%s'",
1249 bdrv_get_device_or_node_name(bs));
826b6ca0
FZ
1250 }
1251
8d24cce1
FZ
1252 bs->backing_hd = backing_hd;
1253 if (!backing_hd) {
826b6ca0
FZ
1254 error_free(bs->backing_blocker);
1255 bs->backing_blocker = NULL;
8d24cce1
FZ
1256 goto out;
1257 }
1258 bs->open_flags &= ~BDRV_O_NO_BACKING;
1259 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_hd->filename);
1260 pstrcpy(bs->backing_format, sizeof(bs->backing_format),
1261 backing_hd->drv ? backing_hd->drv->format_name : "");
826b6ca0
FZ
1262
1263 bdrv_op_block_all(bs->backing_hd, bs->backing_blocker);
1264 /* Otherwise we won't be able to commit due to check in bdrv_commit */
bb00021d 1265 bdrv_op_unblock(bs->backing_hd, BLOCK_OP_TYPE_COMMIT_TARGET,
826b6ca0 1266 bs->backing_blocker);
8d24cce1 1267out:
3baca891 1268 bdrv_refresh_limits(bs, NULL);
8d24cce1
FZ
1269}
1270
31ca6d07
KW
1271/*
1272 * Opens the backing file for a BlockDriverState if not yet open
1273 *
1274 * options is a QDict of options to pass to the block drivers, or NULL for an
1275 * empty set of options. The reference to the QDict is transferred to this
1276 * function (even on failure), so if the caller intends to reuse the dictionary,
1277 * it needs to use QINCREF() before calling bdrv_file_open.
1278 */
34b5d2c6 1279int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp)
9156df12 1280{
1ba4b6a5 1281 char *backing_filename = g_malloc0(PATH_MAX);
317fc44e 1282 int ret = 0;
8d24cce1 1283 BlockDriverState *backing_hd;
34b5d2c6 1284 Error *local_err = NULL;
9156df12
PB
1285
1286 if (bs->backing_hd != NULL) {
31ca6d07 1287 QDECREF(options);
1ba4b6a5 1288 goto free_exit;
9156df12
PB
1289 }
1290
31ca6d07
KW
1291 /* NULL means an empty set of options */
1292 if (options == NULL) {
1293 options = qdict_new();
1294 }
1295
9156df12 1296 bs->open_flags &= ~BDRV_O_NO_BACKING;
1cb6f506
KW
1297 if (qdict_haskey(options, "file.filename")) {
1298 backing_filename[0] = '\0';
1299 } else if (bs->backing_file[0] == '\0' && qdict_size(options) == 0) {
31ca6d07 1300 QDECREF(options);
1ba4b6a5 1301 goto free_exit;
dbecebdd 1302 } else {
9f07429e
HR
1303 bdrv_get_full_backing_filename(bs, backing_filename, PATH_MAX,
1304 &local_err);
1305 if (local_err) {
1306 ret = -EINVAL;
1307 error_propagate(errp, local_err);
1308 QDECREF(options);
1309 goto free_exit;
1310 }
9156df12
PB
1311 }
1312
8ee79e70
KW
1313 if (!bs->drv || !bs->drv->supports_backing) {
1314 ret = -EINVAL;
1315 error_setg(errp, "Driver doesn't support backing files");
1316 QDECREF(options);
1317 goto free_exit;
1318 }
1319
e4e9986b 1320 backing_hd = bdrv_new();
8d24cce1 1321
c5f6e493
KW
1322 if (bs->backing_format[0] != '\0' && !qdict_haskey(options, "driver")) {
1323 qdict_put(options, "driver", qstring_from_str(bs->backing_format));
9156df12
PB
1324 }
1325
f67503e5 1326 assert(bs->backing_hd == NULL);
8d24cce1 1327 ret = bdrv_open(&backing_hd,
ddf5636d 1328 *backing_filename ? backing_filename : NULL, NULL, options,
c5f6e493 1329 bdrv_backing_flags(bs->open_flags), NULL, &local_err);
9156df12 1330 if (ret < 0) {
8d24cce1
FZ
1331 bdrv_unref(backing_hd);
1332 backing_hd = NULL;
9156df12 1333 bs->open_flags |= BDRV_O_NO_BACKING;
b04b6b6e
FZ
1334 error_setg(errp, "Could not open backing file: %s",
1335 error_get_pretty(local_err));
1336 error_free(local_err);
1ba4b6a5 1337 goto free_exit;
9156df12 1338 }
8d24cce1 1339 bdrv_set_backing_hd(bs, backing_hd);
d80ac658 1340
1ba4b6a5
BC
1341free_exit:
1342 g_free(backing_filename);
1343 return ret;
9156df12
PB
1344}
1345
da557aac
HR
1346/*
1347 * Opens a disk image whose options are given as BlockdevRef in another block
1348 * device's options.
1349 *
da557aac
HR
1350 * If allow_none is true, no image will be opened if filename is false and no
1351 * BlockdevRef is given. *pbs will remain unchanged and 0 will be returned.
1352 *
1353 * bdrev_key specifies the key for the image's BlockdevRef in the options QDict.
1354 * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict
1355 * itself, all options starting with "${bdref_key}." are considered part of the
1356 * BlockdevRef.
1357 *
1358 * The BlockdevRef will be removed from the options QDict.
f67503e5
HR
1359 *
1360 * To conform with the behavior of bdrv_open(), *pbs has to be NULL.
da557aac
HR
1361 */
1362int bdrv_open_image(BlockDriverState **pbs, const char *filename,
1363 QDict *options, const char *bdref_key, int flags,
f7d9fd8c 1364 bool allow_none, Error **errp)
da557aac
HR
1365{
1366 QDict *image_options;
1367 int ret;
1368 char *bdref_key_dot;
1369 const char *reference;
1370
f67503e5
HR
1371 assert(pbs);
1372 assert(*pbs == NULL);
1373
da557aac
HR
1374 bdref_key_dot = g_strdup_printf("%s.", bdref_key);
1375 qdict_extract_subqdict(options, &image_options, bdref_key_dot);
1376 g_free(bdref_key_dot);
1377
1378 reference = qdict_get_try_str(options, bdref_key);
1379 if (!filename && !reference && !qdict_size(image_options)) {
1380 if (allow_none) {
1381 ret = 0;
1382 } else {
1383 error_setg(errp, "A block device must be specified for \"%s\"",
1384 bdref_key);
1385 ret = -EINVAL;
1386 }
b20e61e0 1387 QDECREF(image_options);
da557aac
HR
1388 goto done;
1389 }
1390
f7d9fd8c 1391 ret = bdrv_open(pbs, filename, reference, image_options, flags, NULL, errp);
da557aac
HR
1392
1393done:
1394 qdict_del(options, bdref_key);
1395 return ret;
1396}
1397
6b8aeca5 1398int bdrv_append_temp_snapshot(BlockDriverState *bs, int flags, Error **errp)
b998875d
KW
1399{
1400 /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */
1ba4b6a5 1401 char *tmp_filename = g_malloc0(PATH_MAX + 1);
b998875d 1402 int64_t total_size;
83d0521a 1403 QemuOpts *opts = NULL;
b998875d
KW
1404 QDict *snapshot_options;
1405 BlockDriverState *bs_snapshot;
1406 Error *local_err;
1407 int ret;
1408
1409 /* if snapshot, we create a temporary backing file and open it
1410 instead of opening 'filename' directly */
1411
1412 /* Get the required size from the image */
f187743a
KW
1413 total_size = bdrv_getlength(bs);
1414 if (total_size < 0) {
6b8aeca5 1415 ret = total_size;
f187743a 1416 error_setg_errno(errp, -total_size, "Could not get image size");
1ba4b6a5 1417 goto out;
f187743a 1418 }
b998875d
KW
1419
1420 /* Create the temporary image */
1ba4b6a5 1421 ret = get_tmp_filename(tmp_filename, PATH_MAX + 1);
b998875d
KW
1422 if (ret < 0) {
1423 error_setg_errno(errp, -ret, "Could not get temporary filename");
1ba4b6a5 1424 goto out;
b998875d
KW
1425 }
1426
ef810437 1427 opts = qemu_opts_create(bdrv_qcow2.create_opts, NULL, 0,
c282e1fd 1428 &error_abort);
39101f25 1429 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_size, &error_abort);
ef810437 1430 ret = bdrv_create(&bdrv_qcow2, tmp_filename, opts, &local_err);
83d0521a 1431 qemu_opts_del(opts);
b998875d
KW
1432 if (ret < 0) {
1433 error_setg_errno(errp, -ret, "Could not create temporary overlay "
1434 "'%s': %s", tmp_filename,
1435 error_get_pretty(local_err));
1436 error_free(local_err);
1ba4b6a5 1437 goto out;
b998875d
KW
1438 }
1439
1440 /* Prepare a new options QDict for the temporary file */
1441 snapshot_options = qdict_new();
1442 qdict_put(snapshot_options, "file.driver",
1443 qstring_from_str("file"));
1444 qdict_put(snapshot_options, "file.filename",
1445 qstring_from_str(tmp_filename));
1446
e4e9986b 1447 bs_snapshot = bdrv_new();
b998875d
KW
1448
1449 ret = bdrv_open(&bs_snapshot, NULL, NULL, snapshot_options,
ef810437 1450 flags, &bdrv_qcow2, &local_err);
b998875d
KW
1451 if (ret < 0) {
1452 error_propagate(errp, local_err);
1ba4b6a5 1453 goto out;
b998875d
KW
1454 }
1455
1456 bdrv_append(bs_snapshot, bs);
1ba4b6a5
BC
1457
1458out:
1459 g_free(tmp_filename);
6b8aeca5 1460 return ret;
b998875d
KW
1461}
1462
b6ce07aa
KW
1463/*
1464 * Opens a disk image (raw, qcow2, vmdk, ...)
de9c0cec
KW
1465 *
1466 * options is a QDict of options to pass to the block drivers, or NULL for an
1467 * empty set of options. The reference to the QDict belongs to the block layer
1468 * after the call (even on failure), so if the caller intends to reuse the
1469 * dictionary, it needs to use QINCREF() before calling bdrv_open.
f67503e5
HR
1470 *
1471 * If *pbs is NULL, a new BDS will be created with a pointer to it stored there.
1472 * If it is not NULL, the referenced BDS will be reused.
ddf5636d
HR
1473 *
1474 * The reference parameter may be used to specify an existing block device which
1475 * should be opened. If specified, neither options nor a filename may be given,
1476 * nor can an existing BDS be reused (that is, *pbs has to be NULL).
b6ce07aa 1477 */
ddf5636d
HR
1478int bdrv_open(BlockDriverState **pbs, const char *filename,
1479 const char *reference, QDict *options, int flags,
1480 BlockDriver *drv, Error **errp)
ea2384d3 1481{
b6ce07aa 1482 int ret;
f67503e5 1483 BlockDriverState *file = NULL, *bs;
74fe54f2 1484 const char *drvname;
34b5d2c6 1485 Error *local_err = NULL;
b1e6fc08 1486 int snapshot_flags = 0;
712e7874 1487
f67503e5
HR
1488 assert(pbs);
1489
ddf5636d
HR
1490 if (reference) {
1491 bool options_non_empty = options ? qdict_size(options) : false;
1492 QDECREF(options);
1493
1494 if (*pbs) {
1495 error_setg(errp, "Cannot reuse an existing BDS when referencing "
1496 "another block device");
1497 return -EINVAL;
1498 }
1499
1500 if (filename || options_non_empty) {
1501 error_setg(errp, "Cannot reference an existing block device with "
1502 "additional options or a new filename");
1503 return -EINVAL;
1504 }
1505
1506 bs = bdrv_lookup_bs(reference, reference, errp);
1507 if (!bs) {
1508 return -ENODEV;
1509 }
1510 bdrv_ref(bs);
1511 *pbs = bs;
1512 return 0;
1513 }
1514
f67503e5
HR
1515 if (*pbs) {
1516 bs = *pbs;
1517 } else {
e4e9986b 1518 bs = bdrv_new();
f67503e5
HR
1519 }
1520
de9c0cec
KW
1521 /* NULL means an empty set of options */
1522 if (options == NULL) {
1523 options = qdict_new();
1524 }
1525
17b005f1 1526 ret = bdrv_fill_options(&options, &filename, flags, drv, &local_err);
462f5bcf
KW
1527 if (local_err) {
1528 goto fail;
1529 }
1530
76c591b0
KW
1531 /* Find the right image format driver */
1532 drv = NULL;
1533 drvname = qdict_get_try_str(options, "driver");
1534 if (drvname) {
1535 drv = bdrv_find_format(drvname);
1536 qdict_del(options, "driver");
1537 if (!drv) {
1538 error_setg(errp, "Unknown driver: '%s'", drvname);
1539 ret = -EINVAL;
1540 goto fail;
1541 }
1542 }
1543
1544 assert(drvname || !(flags & BDRV_O_PROTOCOL));
1545 if (drv && !drv->bdrv_file_open) {
1546 /* If the user explicitly wants a format driver here, we'll need to add
1547 * another layer for the protocol in bs->file */
1548 flags &= ~BDRV_O_PROTOCOL;
1549 }
1550
de9c0cec 1551 bs->options = options;
b6ad491a 1552 options = qdict_clone_shallow(options);
de9c0cec 1553
f500a6d3 1554 /* Open image file without format layer */
f4788adc
KW
1555 if ((flags & BDRV_O_PROTOCOL) == 0) {
1556 if (flags & BDRV_O_RDWR) {
1557 flags |= BDRV_O_ALLOW_RDWR;
1558 }
1559 if (flags & BDRV_O_SNAPSHOT) {
1560 snapshot_flags = bdrv_temp_snapshot_flags(flags);
1561 flags = bdrv_backing_flags(flags);
1562 }
f500a6d3 1563
f4788adc
KW
1564 assert(file == NULL);
1565 ret = bdrv_open_image(&file, filename, options, "file",
1566 bdrv_inherited_flags(flags),
1567 true, &local_err);
1568 if (ret < 0) {
1569 goto fail;
1570 }
f500a6d3
KW
1571 }
1572
76c591b0 1573 /* Image format probing */
38f3ef57 1574 bs->probed = !drv;
76c591b0 1575 if (!drv && file) {
17b005f1
KW
1576 ret = find_image_format(file, filename, &drv, &local_err);
1577 if (ret < 0) {
8bfea15d 1578 goto fail;
2a05cbe4 1579 }
76c591b0 1580 } else if (!drv) {
17b005f1
KW
1581 error_setg(errp, "Must specify either driver or file");
1582 ret = -EINVAL;
8bfea15d 1583 goto fail;
ea2384d3 1584 }
b6ce07aa
KW
1585
1586 /* Open the image */
34b5d2c6 1587 ret = bdrv_open_common(bs, file, options, flags, drv, &local_err);
b6ce07aa 1588 if (ret < 0) {
8bfea15d 1589 goto fail;
6987307c
CH
1590 }
1591
2a05cbe4 1592 if (file && (bs->file != file)) {
4f6fd349 1593 bdrv_unref(file);
f500a6d3
KW
1594 file = NULL;
1595 }
1596
b6ce07aa 1597 /* If there is a backing file, use it */
9156df12 1598 if ((flags & BDRV_O_NO_BACKING) == 0) {
31ca6d07
KW
1599 QDict *backing_options;
1600
5726d872 1601 qdict_extract_subqdict(options, &backing_options, "backing.");
34b5d2c6 1602 ret = bdrv_open_backing_file(bs, backing_options, &local_err);
b6ce07aa 1603 if (ret < 0) {
b6ad491a 1604 goto close_and_fail;
b6ce07aa 1605 }
b6ce07aa
KW
1606 }
1607
91af7014
HR
1608 bdrv_refresh_filename(bs);
1609
b998875d
KW
1610 /* For snapshot=on, create a temporary qcow2 overlay. bs points to the
1611 * temporary snapshot afterwards. */
b1e6fc08 1612 if (snapshot_flags) {
6b8aeca5 1613 ret = bdrv_append_temp_snapshot(bs, snapshot_flags, &local_err);
b998875d 1614 if (local_err) {
b998875d
KW
1615 goto close_and_fail;
1616 }
1617 }
1618
b6ad491a 1619 /* Check if any unknown options were used */
5acd9d81 1620 if (options && (qdict_size(options) != 0)) {
b6ad491a 1621 const QDictEntry *entry = qdict_first(options);
5acd9d81
HR
1622 if (flags & BDRV_O_PROTOCOL) {
1623 error_setg(errp, "Block protocol '%s' doesn't support the option "
1624 "'%s'", drv->format_name, entry->key);
1625 } else {
1626 error_setg(errp, "Block format '%s' used by device '%s' doesn't "
1627 "support the option '%s'", drv->format_name,
bfb197e0 1628 bdrv_get_device_name(bs), entry->key);
5acd9d81 1629 }
b6ad491a
KW
1630
1631 ret = -EINVAL;
1632 goto close_and_fail;
1633 }
b6ad491a 1634
b6ce07aa 1635 if (!bdrv_key_required(bs)) {
a7f53e26
MA
1636 if (bs->blk) {
1637 blk_dev_change_media_cb(bs->blk, true);
1638 }
c3adb58f
MA
1639 } else if (!runstate_check(RUN_STATE_PRELAUNCH)
1640 && !runstate_check(RUN_STATE_INMIGRATE)
1641 && !runstate_check(RUN_STATE_PAUSED)) { /* HACK */
1642 error_setg(errp,
1643 "Guest must be stopped for opening of encrypted image");
1644 ret = -EBUSY;
1645 goto close_and_fail;
b6ce07aa
KW
1646 }
1647
c3adb58f 1648 QDECREF(options);
f67503e5 1649 *pbs = bs;
b6ce07aa
KW
1650 return 0;
1651
8bfea15d 1652fail:
f500a6d3 1653 if (file != NULL) {
4f6fd349 1654 bdrv_unref(file);
f500a6d3 1655 }
de9c0cec 1656 QDECREF(bs->options);
b6ad491a 1657 QDECREF(options);
de9c0cec 1658 bs->options = NULL;
f67503e5
HR
1659 if (!*pbs) {
1660 /* If *pbs is NULL, a new BDS has been created in this function and
1661 needs to be freed now. Otherwise, it does not need to be closed,
1662 since it has not really been opened yet. */
1663 bdrv_unref(bs);
1664 }
84d18f06 1665 if (local_err) {
34b5d2c6
HR
1666 error_propagate(errp, local_err);
1667 }
b6ad491a 1668 return ret;
de9c0cec 1669
b6ad491a 1670close_and_fail:
f67503e5
HR
1671 /* See fail path, but now the BDS has to be always closed */
1672 if (*pbs) {
1673 bdrv_close(bs);
1674 } else {
1675 bdrv_unref(bs);
1676 }
b6ad491a 1677 QDECREF(options);
84d18f06 1678 if (local_err) {
34b5d2c6
HR
1679 error_propagate(errp, local_err);
1680 }
b6ce07aa
KW
1681 return ret;
1682}
1683
e971aa12
JC
1684typedef struct BlockReopenQueueEntry {
1685 bool prepared;
1686 BDRVReopenState state;
1687 QSIMPLEQ_ENTRY(BlockReopenQueueEntry) entry;
1688} BlockReopenQueueEntry;
1689
1690/*
1691 * Adds a BlockDriverState to a simple queue for an atomic, transactional
1692 * reopen of multiple devices.
1693 *
1694 * bs_queue can either be an existing BlockReopenQueue that has had QSIMPLE_INIT
1695 * already performed, or alternatively may be NULL a new BlockReopenQueue will
1696 * be created and initialized. This newly created BlockReopenQueue should be
1697 * passed back in for subsequent calls that are intended to be of the same
1698 * atomic 'set'.
1699 *
1700 * bs is the BlockDriverState to add to the reopen queue.
1701 *
1702 * flags contains the open flags for the associated bs
1703 *
1704 * returns a pointer to bs_queue, which is either the newly allocated
1705 * bs_queue, or the existing bs_queue being used.
1706 *
1707 */
1708BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue,
1709 BlockDriverState *bs, int flags)
1710{
1711 assert(bs != NULL);
1712
1713 BlockReopenQueueEntry *bs_entry;
1714 if (bs_queue == NULL) {
1715 bs_queue = g_new0(BlockReopenQueue, 1);
1716 QSIMPLEQ_INIT(bs_queue);
1717 }
1718
f1f25a2e
KW
1719 /* bdrv_open() masks this flag out */
1720 flags &= ~BDRV_O_PROTOCOL;
1721
e971aa12 1722 if (bs->file) {
f1f25a2e 1723 bdrv_reopen_queue(bs_queue, bs->file, bdrv_inherited_flags(flags));
e971aa12
JC
1724 }
1725
1726 bs_entry = g_new0(BlockReopenQueueEntry, 1);
1727 QSIMPLEQ_INSERT_TAIL(bs_queue, bs_entry, entry);
1728
1729 bs_entry->state.bs = bs;
1730 bs_entry->state.flags = flags;
1731
1732 return bs_queue;
1733}
1734
1735/*
1736 * Reopen multiple BlockDriverStates atomically & transactionally.
1737 *
1738 * The queue passed in (bs_queue) must have been built up previous
1739 * via bdrv_reopen_queue().
1740 *
1741 * Reopens all BDS specified in the queue, with the appropriate
1742 * flags. All devices are prepared for reopen, and failure of any
1743 * device will cause all device changes to be abandonded, and intermediate
1744 * data cleaned up.
1745 *
1746 * If all devices prepare successfully, then the changes are committed
1747 * to all devices.
1748 *
1749 */
1750int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp)
1751{
1752 int ret = -1;
1753 BlockReopenQueueEntry *bs_entry, *next;
1754 Error *local_err = NULL;
1755
1756 assert(bs_queue != NULL);
1757
1758 bdrv_drain_all();
1759
1760 QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) {
1761 if (bdrv_reopen_prepare(&bs_entry->state, bs_queue, &local_err)) {
1762 error_propagate(errp, local_err);
1763 goto cleanup;
1764 }
1765 bs_entry->prepared = true;
1766 }
1767
1768 /* If we reach this point, we have success and just need to apply the
1769 * changes
1770 */
1771 QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) {
1772 bdrv_reopen_commit(&bs_entry->state);
1773 }
1774
1775 ret = 0;
1776
1777cleanup:
1778 QSIMPLEQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) {
1779 if (ret && bs_entry->prepared) {
1780 bdrv_reopen_abort(&bs_entry->state);
1781 }
1782 g_free(bs_entry);
1783 }
1784 g_free(bs_queue);
1785 return ret;
1786}
1787
1788
1789/* Reopen a single BlockDriverState with the specified flags. */
1790int bdrv_reopen(BlockDriverState *bs, int bdrv_flags, Error **errp)
1791{
1792 int ret = -1;
1793 Error *local_err = NULL;
1794 BlockReopenQueue *queue = bdrv_reopen_queue(NULL, bs, bdrv_flags);
1795
1796 ret = bdrv_reopen_multiple(queue, &local_err);
1797 if (local_err != NULL) {
1798 error_propagate(errp, local_err);
1799 }
1800 return ret;
1801}
1802
1803
1804/*
1805 * Prepares a BlockDriverState for reopen. All changes are staged in the
1806 * 'opaque' field of the BDRVReopenState, which is used and allocated by
1807 * the block driver layer .bdrv_reopen_prepare()
1808 *
1809 * bs is the BlockDriverState to reopen
1810 * flags are the new open flags
1811 * queue is the reopen queue
1812 *
1813 * Returns 0 on success, non-zero on error. On error errp will be set
1814 * as well.
1815 *
1816 * On failure, bdrv_reopen_abort() will be called to clean up any data.
1817 * It is the responsibility of the caller to then call the abort() or
1818 * commit() for any other BDS that have been left in a prepare() state
1819 *
1820 */
1821int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue,
1822 Error **errp)
1823{
1824 int ret = -1;
1825 Error *local_err = NULL;
1826 BlockDriver *drv;
1827
1828 assert(reopen_state != NULL);
1829 assert(reopen_state->bs->drv != NULL);
1830 drv = reopen_state->bs->drv;
1831
1832 /* if we are to stay read-only, do not allow permission change
1833 * to r/w */
1834 if (!(reopen_state->bs->open_flags & BDRV_O_ALLOW_RDWR) &&
1835 reopen_state->flags & BDRV_O_RDWR) {
81e5f78a
AG
1836 error_setg(errp, "Node '%s' is read only",
1837 bdrv_get_device_or_node_name(reopen_state->bs));
e971aa12
JC
1838 goto error;
1839 }
1840
1841
1842 ret = bdrv_flush(reopen_state->bs);
1843 if (ret) {
1844 error_set(errp, ERROR_CLASS_GENERIC_ERROR, "Error (%s) flushing drive",
1845 strerror(-ret));
1846 goto error;
1847 }
1848
1849 if (drv->bdrv_reopen_prepare) {
1850 ret = drv->bdrv_reopen_prepare(reopen_state, queue, &local_err);
1851 if (ret) {
1852 if (local_err != NULL) {
1853 error_propagate(errp, local_err);
1854 } else {
d8b6895f
LC
1855 error_setg(errp, "failed while preparing to reopen image '%s'",
1856 reopen_state->bs->filename);
e971aa12
JC
1857 }
1858 goto error;
1859 }
1860 } else {
1861 /* It is currently mandatory to have a bdrv_reopen_prepare()
1862 * handler for each supported drv. */
81e5f78a
AG
1863 error_setg(errp, "Block format '%s' used by node '%s' "
1864 "does not support reopening files", drv->format_name,
1865 bdrv_get_device_or_node_name(reopen_state->bs));
e971aa12
JC
1866 ret = -1;
1867 goto error;
1868 }
1869
1870 ret = 0;
1871
1872error:
1873 return ret;
1874}
1875
1876/*
1877 * Takes the staged changes for the reopen from bdrv_reopen_prepare(), and
1878 * makes them final by swapping the staging BlockDriverState contents into
1879 * the active BlockDriverState contents.
1880 */
1881void bdrv_reopen_commit(BDRVReopenState *reopen_state)
1882{
1883 BlockDriver *drv;
1884
1885 assert(reopen_state != NULL);
1886 drv = reopen_state->bs->drv;
1887 assert(drv != NULL);
1888
1889 /* If there are any driver level actions to take */
1890 if (drv->bdrv_reopen_commit) {
1891 drv->bdrv_reopen_commit(reopen_state);
1892 }
1893
1894 /* set BDS specific flags now */
1895 reopen_state->bs->open_flags = reopen_state->flags;
1896 reopen_state->bs->enable_write_cache = !!(reopen_state->flags &
1897 BDRV_O_CACHE_WB);
1898 reopen_state->bs->read_only = !(reopen_state->flags & BDRV_O_RDWR);
355ef4ac 1899
3baca891 1900 bdrv_refresh_limits(reopen_state->bs, NULL);
e971aa12
JC
1901}
1902
1903/*
1904 * Abort the reopen, and delete and free the staged changes in
1905 * reopen_state
1906 */
1907void bdrv_reopen_abort(BDRVReopenState *reopen_state)
1908{
1909 BlockDriver *drv;
1910
1911 assert(reopen_state != NULL);
1912 drv = reopen_state->bs->drv;
1913 assert(drv != NULL);
1914
1915 if (drv->bdrv_reopen_abort) {
1916 drv->bdrv_reopen_abort(reopen_state);
1917 }
1918}
1919
1920
fc01f7e7
FB
1921void bdrv_close(BlockDriverState *bs)
1922{
33384421
HR
1923 BdrvAioNotifier *ban, *ban_next;
1924
3cbc002c
PB
1925 if (bs->job) {
1926 block_job_cancel_sync(bs->job);
1927 }
58fda173
SH
1928 bdrv_drain_all(); /* complete I/O */
1929 bdrv_flush(bs);
1930 bdrv_drain_all(); /* in case flush left pending I/O */
d7d512f6 1931 notifier_list_notify(&bs->close_notifiers, bs);
7094f12f 1932
3cbc002c 1933 if (bs->drv) {
557df6ac 1934 if (bs->backing_hd) {
826b6ca0
FZ
1935 BlockDriverState *backing_hd = bs->backing_hd;
1936 bdrv_set_backing_hd(bs, NULL);
1937 bdrv_unref(backing_hd);
557df6ac 1938 }
ea2384d3 1939 bs->drv->bdrv_close(bs);
7267c094 1940 g_free(bs->opaque);
ea2384d3
FB
1941 bs->opaque = NULL;
1942 bs->drv = NULL;
53fec9d3 1943 bs->copy_on_read = 0;
a275fa42
PB
1944 bs->backing_file[0] = '\0';
1945 bs->backing_format[0] = '\0';
6405875c
PB
1946 bs->total_sectors = 0;
1947 bs->encrypted = 0;
1948 bs->valid_key = 0;
1949 bs->sg = 0;
0d51b4de 1950 bs->zero_beyond_eof = false;
de9c0cec
KW
1951 QDECREF(bs->options);
1952 bs->options = NULL;
91af7014
HR
1953 QDECREF(bs->full_open_options);
1954 bs->full_open_options = NULL;
b338082b 1955
66f82cee 1956 if (bs->file != NULL) {
4f6fd349 1957 bdrv_unref(bs->file);
0ac9377d 1958 bs->file = NULL;
66f82cee 1959 }
b338082b 1960 }
98f90dba 1961
a7f53e26
MA
1962 if (bs->blk) {
1963 blk_dev_change_media_cb(bs->blk, false);
1964 }
9ca11154 1965
98f90dba
ZYW
1966 /*throttling disk I/O limits*/
1967 if (bs->io_limits_enabled) {
1968 bdrv_io_limits_disable(bs);
1969 }
33384421
HR
1970
1971 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) {
1972 g_free(ban);
1973 }
1974 QLIST_INIT(&bs->aio_notifiers);
b338082b
FB
1975}
1976
2bc93fed
MK
1977void bdrv_close_all(void)
1978{
1979 BlockDriverState *bs;
1980
dc364f4c 1981 QTAILQ_FOREACH(bs, &bdrv_states, device_list) {
ed78cda3
SH
1982 AioContext *aio_context = bdrv_get_aio_context(bs);
1983
1984 aio_context_acquire(aio_context);
2bc93fed 1985 bdrv_close(bs);
ed78cda3 1986 aio_context_release(aio_context);
2bc93fed
MK
1987 }
1988}
1989
88266f5a
SH
1990/* Check if any requests are in-flight (including throttled requests) */
1991static bool bdrv_requests_pending(BlockDriverState *bs)
1992{
1993 if (!QLIST_EMPTY(&bs->tracked_requests)) {
1994 return true;
1995 }
cc0681c4
BC
1996 if (!qemu_co_queue_empty(&bs->throttled_reqs[0])) {
1997 return true;
1998 }
1999 if (!qemu_co_queue_empty(&bs->throttled_reqs[1])) {
88266f5a
SH
2000 return true;
2001 }
2002 if (bs->file && bdrv_requests_pending(bs->file)) {
2003 return true;
2004 }
2005 if (bs->backing_hd && bdrv_requests_pending(bs->backing_hd)) {
2006 return true;
2007 }
2008 return false;
2009}
2010
5b98db0a
SH
2011static bool bdrv_drain_one(BlockDriverState *bs)
2012{
2013 bool bs_busy;
2014
2015 bdrv_flush_io_queue(bs);
2016 bdrv_start_throttled_reqs(bs);
2017 bs_busy = bdrv_requests_pending(bs);
2018 bs_busy |= aio_poll(bdrv_get_aio_context(bs), bs_busy);
2019 return bs_busy;
2020}
2021
2022/*
2023 * Wait for pending requests to complete on a single BlockDriverState subtree
2024 *
2025 * See the warning in bdrv_drain_all(). This function can only be called if
2026 * you are sure nothing can generate I/O because you have op blockers
2027 * installed.
2028 *
2029 * Note that unlike bdrv_drain_all(), the caller must hold the BlockDriverState
2030 * AioContext.
2031 */
2032void bdrv_drain(BlockDriverState *bs)
2033{
2034 while (bdrv_drain_one(bs)) {
2035 /* Keep iterating */
2036 }
2037}
2038
922453bc
SH
2039/*
2040 * Wait for pending requests to complete across all BlockDriverStates
2041 *
2042 * This function does not flush data to disk, use bdrv_flush_all() for that
2043 * after calling this function.
4c355d53
ZYW
2044 *
2045 * Note that completion of an asynchronous I/O operation can trigger any
2046 * number of other I/O operations on other devices---for example a coroutine
2047 * can be arbitrarily complex and a constant flow of I/O can come until the
2048 * coroutine is complete. Because of this, it is not possible to have a
2049 * function to drain a single device's I/O queue.
922453bc
SH
2050 */
2051void bdrv_drain_all(void)
2052{
88266f5a
SH
2053 /* Always run first iteration so any pending completion BHs run */
2054 bool busy = true;
4f5472cb 2055 BlockDriverState *bs = NULL;
922453bc 2056
4f5472cb 2057 while ((bs = bdrv_next(bs))) {
69da3b0b
FZ
2058 AioContext *aio_context = bdrv_get_aio_context(bs);
2059
2060 aio_context_acquire(aio_context);
2061 if (bs->job) {
2062 block_job_pause(bs->job);
2063 }
2064 aio_context_release(aio_context);
2065 }
2066
88266f5a 2067 while (busy) {
9b536adc 2068 busy = false;
4f5472cb 2069 bs = NULL;
9b536adc 2070
4f5472cb 2071 while ((bs = bdrv_next(bs))) {
9b536adc 2072 AioContext *aio_context = bdrv_get_aio_context(bs);
9b536adc
SH
2073
2074 aio_context_acquire(aio_context);
5b98db0a 2075 busy |= bdrv_drain_one(bs);
9b536adc 2076 aio_context_release(aio_context);
9b536adc 2077 }
922453bc 2078 }
69da3b0b 2079
4f5472cb
SH
2080 bs = NULL;
2081 while ((bs = bdrv_next(bs))) {
69da3b0b
FZ
2082 AioContext *aio_context = bdrv_get_aio_context(bs);
2083
2084 aio_context_acquire(aio_context);
2085 if (bs->job) {
2086 block_job_resume(bs->job);
2087 }
2088 aio_context_release(aio_context);
2089 }
922453bc
SH
2090}
2091
dc364f4c
BC
2092/* make a BlockDriverState anonymous by removing from bdrv_state and
2093 * graph_bdrv_state list.
d22b2f41
RH
2094 Also, NULL terminate the device_name to prevent double remove */
2095void bdrv_make_anon(BlockDriverState *bs)
2096{
bfb197e0
MA
2097 /*
2098 * Take care to remove bs from bdrv_states only when it's actually
2099 * in it. Note that bs->device_list.tqe_prev is initially null,
2100 * and gets set to non-null by QTAILQ_INSERT_TAIL(). Establish
2101 * the useful invariant "bs in bdrv_states iff bs->tqe_prev" by
2102 * resetting it to null on remove.
2103 */
2104 if (bs->device_list.tqe_prev) {
dc364f4c 2105 QTAILQ_REMOVE(&bdrv_states, bs, device_list);
bfb197e0 2106 bs->device_list.tqe_prev = NULL;
d22b2f41 2107 }
dc364f4c
BC
2108 if (bs->node_name[0] != '\0') {
2109 QTAILQ_REMOVE(&graph_bdrv_states, bs, node_list);
2110 }
2111 bs->node_name[0] = '\0';
d22b2f41
RH
2112}
2113
e023b2e2
PB
2114static void bdrv_rebind(BlockDriverState *bs)
2115{
2116 if (bs->drv && bs->drv->bdrv_rebind) {
2117 bs->drv->bdrv_rebind(bs);
2118 }
2119}
2120
4ddc07ca
PB
2121static void bdrv_move_feature_fields(BlockDriverState *bs_dest,
2122 BlockDriverState *bs_src)
8802d1fd 2123{
4ddc07ca 2124 /* move some fields that need to stay attached to the device */
8802d1fd
JC
2125
2126 /* dev info */
1b7fd729 2127 bs_dest->guest_block_size = bs_src->guest_block_size;
4ddc07ca 2128 bs_dest->copy_on_read = bs_src->copy_on_read;
8802d1fd 2129
4ddc07ca 2130 bs_dest->enable_write_cache = bs_src->enable_write_cache;
c4a248a1 2131
cc0681c4
BC
2132 /* i/o throttled req */
2133 memcpy(&bs_dest->throttle_state,
2134 &bs_src->throttle_state,
2135 sizeof(ThrottleState));
2136 bs_dest->throttled_reqs[0] = bs_src->throttled_reqs[0];
2137 bs_dest->throttled_reqs[1] = bs_src->throttled_reqs[1];
4ddc07ca 2138 bs_dest->io_limits_enabled = bs_src->io_limits_enabled;
8802d1fd 2139
8802d1fd 2140 /* r/w error */
4ddc07ca
PB
2141 bs_dest->on_read_error = bs_src->on_read_error;
2142 bs_dest->on_write_error = bs_src->on_write_error;
8802d1fd
JC
2143
2144 /* i/o status */
4ddc07ca
PB
2145 bs_dest->iostatus_enabled = bs_src->iostatus_enabled;
2146 bs_dest->iostatus = bs_src->iostatus;
8802d1fd 2147
a9fc4408 2148 /* dirty bitmap */
e4654d2d 2149 bs_dest->dirty_bitmaps = bs_src->dirty_bitmaps;
a9fc4408 2150
9fcb0251
FZ
2151 /* reference count */
2152 bs_dest->refcnt = bs_src->refcnt;
2153
a9fc4408 2154 /* job */
4ddc07ca 2155 bs_dest->job = bs_src->job;
a9fc4408 2156
8802d1fd 2157 /* keep the same entry in bdrv_states */
dc364f4c 2158 bs_dest->device_list = bs_src->device_list;
7e7d56d9
MA
2159 bs_dest->blk = bs_src->blk;
2160
fbe40ff7
FZ
2161 memcpy(bs_dest->op_blockers, bs_src->op_blockers,
2162 sizeof(bs_dest->op_blockers));
4ddc07ca 2163}
8802d1fd 2164
4ddc07ca
PB
2165/*
2166 * Swap bs contents for two image chains while they are live,
2167 * while keeping required fields on the BlockDriverState that is
2168 * actually attached to a device.
2169 *
2170 * This will modify the BlockDriverState fields, and swap contents
2171 * between bs_new and bs_old. Both bs_new and bs_old are modified.
2172 *
bfb197e0 2173 * bs_new must not be attached to a BlockBackend.
4ddc07ca
PB
2174 *
2175 * This function does not create any image files.
2176 */
2177void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old)
2178{
2179 BlockDriverState tmp;
f6801b83 2180
90ce8a06
BC
2181 /* The code needs to swap the node_name but simply swapping node_list won't
2182 * work so first remove the nodes from the graph list, do the swap then
2183 * insert them back if needed.
2184 */
2185 if (bs_new->node_name[0] != '\0') {
2186 QTAILQ_REMOVE(&graph_bdrv_states, bs_new, node_list);
2187 }
2188 if (bs_old->node_name[0] != '\0') {
2189 QTAILQ_REMOVE(&graph_bdrv_states, bs_old, node_list);
2190 }
2191
bfb197e0 2192 /* bs_new must be unattached and shouldn't have anything fancy enabled */
7e7d56d9 2193 assert(!bs_new->blk);
e4654d2d 2194 assert(QLIST_EMPTY(&bs_new->dirty_bitmaps));
4ddc07ca 2195 assert(bs_new->job == NULL);
4ddc07ca 2196 assert(bs_new->io_limits_enabled == false);
cc0681c4 2197 assert(!throttle_have_timer(&bs_new->throttle_state));
8802d1fd 2198
4ddc07ca
PB
2199 tmp = *bs_new;
2200 *bs_new = *bs_old;
2201 *bs_old = tmp;
a9fc4408 2202
4ddc07ca
PB
2203 /* there are some fields that should not be swapped, move them back */
2204 bdrv_move_feature_fields(&tmp, bs_old);
2205 bdrv_move_feature_fields(bs_old, bs_new);
2206 bdrv_move_feature_fields(bs_new, &tmp);
8802d1fd 2207
bfb197e0 2208 /* bs_new must remain unattached */
7e7d56d9 2209 assert(!bs_new->blk);
4ddc07ca
PB
2210
2211 /* Check a few fields that should remain attached to the device */
4ddc07ca 2212 assert(bs_new->job == NULL);
4ddc07ca 2213 assert(bs_new->io_limits_enabled == false);
cc0681c4 2214 assert(!throttle_have_timer(&bs_new->throttle_state));
e023b2e2 2215
90ce8a06
BC
2216 /* insert the nodes back into the graph node list if needed */
2217 if (bs_new->node_name[0] != '\0') {
2218 QTAILQ_INSERT_TAIL(&graph_bdrv_states, bs_new, node_list);
2219 }
2220 if (bs_old->node_name[0] != '\0') {
2221 QTAILQ_INSERT_TAIL(&graph_bdrv_states, bs_old, node_list);
2222 }
2223
e023b2e2 2224 bdrv_rebind(bs_new);
4ddc07ca
PB
2225 bdrv_rebind(bs_old);
2226}
2227
2228/*
2229 * Add new bs contents at the top of an image chain while the chain is
2230 * live, while keeping required fields on the top layer.
2231 *
2232 * This will modify the BlockDriverState fields, and swap contents
2233 * between bs_new and bs_top. Both bs_new and bs_top are modified.
2234 *
bfb197e0 2235 * bs_new must not be attached to a BlockBackend.
4ddc07ca
PB
2236 *
2237 * This function does not create any image files.
2238 */
2239void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top)
2240{
2241 bdrv_swap(bs_new, bs_top);
2242
2243 /* The contents of 'tmp' will become bs_top, as we are
2244 * swapping bs_new and bs_top contents. */
8d24cce1 2245 bdrv_set_backing_hd(bs_top, bs_new);
8802d1fd
JC
2246}
2247
4f6fd349 2248static void bdrv_delete(BlockDriverState *bs)
b338082b 2249{
3e914655 2250 assert(!bs->job);
3718d8ab 2251 assert(bdrv_op_blocker_is_empty(bs));
4f6fd349 2252 assert(!bs->refcnt);
e4654d2d 2253 assert(QLIST_EMPTY(&bs->dirty_bitmaps));
18846dee 2254
e1b5c52e
SH
2255 bdrv_close(bs);
2256
1b7bdbc1 2257 /* remove from list, if necessary */
d22b2f41 2258 bdrv_make_anon(bs);
34c6f050 2259
7267c094 2260 g_free(bs);
fc01f7e7
FB
2261}
2262
e97fc193
AL
2263/*
2264 * Run consistency checks on an image
2265 *
e076f338 2266 * Returns 0 if the check could be completed (it doesn't mean that the image is
a1c7273b 2267 * free of errors) or -errno when an internal error occurred. The results of the
e076f338 2268 * check are stored in res.
e97fc193 2269 */
4534ff54 2270int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix)
e97fc193 2271{
908bcd54
HR
2272 if (bs->drv == NULL) {
2273 return -ENOMEDIUM;
2274 }
e97fc193
AL
2275 if (bs->drv->bdrv_check == NULL) {
2276 return -ENOTSUP;
2277 }
2278
e076f338 2279 memset(res, 0, sizeof(*res));
4534ff54 2280 return bs->drv->bdrv_check(bs, res, fix);
e97fc193
AL
2281}
2282
8a426614
KW
2283#define COMMIT_BUF_SECTORS 2048
2284
33e3963e
FB
2285/* commit COW file into the raw image */
2286int bdrv_commit(BlockDriverState *bs)
2287{
19cb3738 2288 BlockDriver *drv = bs->drv;
72706ea4 2289 int64_t sector, total_sectors, length, backing_length;
8a426614 2290 int n, ro, open_flags;
0bce597d 2291 int ret = 0;
72706ea4 2292 uint8_t *buf = NULL;
33e3963e 2293
19cb3738
FB
2294 if (!drv)
2295 return -ENOMEDIUM;
6bb45158 2296
4dca4b63
NS
2297 if (!bs->backing_hd) {
2298 return -ENOTSUP;
33e3963e
FB
2299 }
2300
bb00021d
FZ
2301 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, NULL) ||
2302 bdrv_op_is_blocked(bs->backing_hd, BLOCK_OP_TYPE_COMMIT_TARGET, NULL)) {
2d3735d3
SH
2303 return -EBUSY;
2304 }
2305
4dca4b63 2306 ro = bs->backing_hd->read_only;
4dca4b63
NS
2307 open_flags = bs->backing_hd->open_flags;
2308
2309 if (ro) {
0bce597d
JC
2310 if (bdrv_reopen(bs->backing_hd, open_flags | BDRV_O_RDWR, NULL)) {
2311 return -EACCES;
4dca4b63 2312 }
ea2384d3 2313 }
33e3963e 2314
72706ea4
JC
2315 length = bdrv_getlength(bs);
2316 if (length < 0) {
2317 ret = length;
2318 goto ro_cleanup;
2319 }
2320
2321 backing_length = bdrv_getlength(bs->backing_hd);
2322 if (backing_length < 0) {
2323 ret = backing_length;
2324 goto ro_cleanup;
2325 }
2326
2327 /* If our top snapshot is larger than the backing file image,
2328 * grow the backing file image if possible. If not possible,
2329 * we must return an error */
2330 if (length > backing_length) {
2331 ret = bdrv_truncate(bs->backing_hd, length);
2332 if (ret < 0) {
2333 goto ro_cleanup;
2334 }
2335 }
2336
2337 total_sectors = length >> BDRV_SECTOR_BITS;
857d4f46
KW
2338
2339 /* qemu_try_blockalign() for bs will choose an alignment that works for
2340 * bs->backing_hd as well, so no need to compare the alignment manually. */
2341 buf = qemu_try_blockalign(bs, COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE);
2342 if (buf == NULL) {
2343 ret = -ENOMEM;
2344 goto ro_cleanup;
2345 }
8a426614
KW
2346
2347 for (sector = 0; sector < total_sectors; sector += n) {
d663640c
PB
2348 ret = bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n);
2349 if (ret < 0) {
2350 goto ro_cleanup;
2351 }
2352 if (ret) {
dabfa6cc
KW
2353 ret = bdrv_read(bs, sector, buf, n);
2354 if (ret < 0) {
8a426614
KW
2355 goto ro_cleanup;
2356 }
2357
dabfa6cc
KW
2358 ret = bdrv_write(bs->backing_hd, sector, buf, n);
2359 if (ret < 0) {
8a426614
KW
2360 goto ro_cleanup;
2361 }
ea2384d3 2362 }
33e3963e 2363 }
95389c86 2364
1d44952f
CH
2365 if (drv->bdrv_make_empty) {
2366 ret = drv->bdrv_make_empty(bs);
dabfa6cc
KW
2367 if (ret < 0) {
2368 goto ro_cleanup;
2369 }
1d44952f
CH
2370 bdrv_flush(bs);
2371 }
95389c86 2372
3f5075ae
CH
2373 /*
2374 * Make sure all data we wrote to the backing device is actually
2375 * stable on disk.
2376 */
dabfa6cc 2377 if (bs->backing_hd) {
3f5075ae 2378 bdrv_flush(bs->backing_hd);
dabfa6cc 2379 }
4dca4b63 2380
dabfa6cc 2381 ret = 0;
4dca4b63 2382ro_cleanup:
857d4f46 2383 qemu_vfree(buf);
4dca4b63
NS
2384
2385 if (ro) {
0bce597d
JC
2386 /* ignoring error return here */
2387 bdrv_reopen(bs->backing_hd, open_flags & ~BDRV_O_RDWR, NULL);
4dca4b63
NS
2388 }
2389
1d44952f 2390 return ret;
33e3963e
FB
2391}
2392
e8877497 2393int bdrv_commit_all(void)
6ab4b5ab
MA
2394{
2395 BlockDriverState *bs;
2396
dc364f4c 2397 QTAILQ_FOREACH(bs, &bdrv_states, device_list) {
ed78cda3
SH
2398 AioContext *aio_context = bdrv_get_aio_context(bs);
2399
2400 aio_context_acquire(aio_context);
272d2d8e
JC
2401 if (bs->drv && bs->backing_hd) {
2402 int ret = bdrv_commit(bs);
2403 if (ret < 0) {
ed78cda3 2404 aio_context_release(aio_context);
272d2d8e
JC
2405 return ret;
2406 }
e8877497 2407 }
ed78cda3 2408 aio_context_release(aio_context);
6ab4b5ab 2409 }
e8877497 2410 return 0;
6ab4b5ab
MA
2411}
2412
dbffbdcf
SH
2413/**
2414 * Remove an active request from the tracked requests list
2415 *
2416 * This function should be called when a tracked request is completing.
2417 */
2418static void tracked_request_end(BdrvTrackedRequest *req)
2419{
2dbafdc0
KW
2420 if (req->serialising) {
2421 req->bs->serialising_in_flight--;
2422 }
2423
dbffbdcf 2424 QLIST_REMOVE(req, list);
f4658285 2425 qemu_co_queue_restart_all(&req->wait_queue);
dbffbdcf
SH
2426}
2427
2428/**
2429 * Add an active request to the tracked requests list
2430 */
2431static void tracked_request_begin(BdrvTrackedRequest *req,
2432 BlockDriverState *bs,
793ed47a
KW
2433 int64_t offset,
2434 unsigned int bytes, bool is_write)
dbffbdcf
SH
2435{
2436 *req = (BdrvTrackedRequest){
2437 .bs = bs,
2dbafdc0
KW
2438 .offset = offset,
2439 .bytes = bytes,
2440 .is_write = is_write,
2441 .co = qemu_coroutine_self(),
2442 .serialising = false,
7327145f
KW
2443 .overlap_offset = offset,
2444 .overlap_bytes = bytes,
dbffbdcf
SH
2445 };
2446
f4658285
SH
2447 qemu_co_queue_init(&req->wait_queue);
2448
dbffbdcf
SH
2449 QLIST_INSERT_HEAD(&bs->tracked_requests, req, list);
2450}
2451
e96126ff 2452static void mark_request_serialising(BdrvTrackedRequest *req, uint64_t align)
2dbafdc0 2453{
7327145f 2454 int64_t overlap_offset = req->offset & ~(align - 1);
e96126ff
KW
2455 unsigned int overlap_bytes = ROUND_UP(req->offset + req->bytes, align)
2456 - overlap_offset;
7327145f 2457
2dbafdc0
KW
2458 if (!req->serialising) {
2459 req->bs->serialising_in_flight++;
2460 req->serialising = true;
2461 }
7327145f
KW
2462
2463 req->overlap_offset = MIN(req->overlap_offset, overlap_offset);
2464 req->overlap_bytes = MAX(req->overlap_bytes, overlap_bytes);
2dbafdc0
KW
2465}
2466
d83947ac
SH
2467/**
2468 * Round a region to cluster boundaries
2469 */
343bded4
PB
2470void bdrv_round_to_clusters(BlockDriverState *bs,
2471 int64_t sector_num, int nb_sectors,
2472 int64_t *cluster_sector_num,
2473 int *cluster_nb_sectors)
d83947ac
SH
2474{
2475 BlockDriverInfo bdi;
2476
2477 if (bdrv_get_info(bs, &bdi) < 0 || bdi.cluster_size == 0) {
2478 *cluster_sector_num = sector_num;
2479 *cluster_nb_sectors = nb_sectors;
2480 } else {
2481 int64_t c = bdi.cluster_size / BDRV_SECTOR_SIZE;
2482 *cluster_sector_num = QEMU_ALIGN_DOWN(sector_num, c);
2483 *cluster_nb_sectors = QEMU_ALIGN_UP(sector_num - *cluster_sector_num +
2484 nb_sectors, c);
2485 }
2486}
2487
7327145f 2488static int bdrv_get_cluster_size(BlockDriverState *bs)
793ed47a
KW
2489{
2490 BlockDriverInfo bdi;
7327145f 2491 int ret;
793ed47a 2492
7327145f
KW
2493 ret = bdrv_get_info(bs, &bdi);
2494 if (ret < 0 || bdi.cluster_size == 0) {
2495 return bs->request_alignment;
793ed47a 2496 } else {
7327145f 2497 return bdi.cluster_size;
793ed47a
KW
2498 }
2499}
2500
f4658285 2501static bool tracked_request_overlaps(BdrvTrackedRequest *req,
793ed47a
KW
2502 int64_t offset, unsigned int bytes)
2503{
d83947ac 2504 /* aaaa bbbb */
7327145f 2505 if (offset >= req->overlap_offset + req->overlap_bytes) {
d83947ac
SH
2506 return false;
2507 }
2508 /* bbbb aaaa */
7327145f 2509 if (req->overlap_offset >= offset + bytes) {
d83947ac
SH
2510 return false;
2511 }
2512 return true;
f4658285
SH
2513}
2514
28de2dcd 2515static bool coroutine_fn wait_serialising_requests(BdrvTrackedRequest *self)
f4658285 2516{
2dbafdc0 2517 BlockDriverState *bs = self->bs;
f4658285
SH
2518 BdrvTrackedRequest *req;
2519 bool retry;
28de2dcd 2520 bool waited = false;
f4658285 2521
2dbafdc0 2522 if (!bs->serialising_in_flight) {
28de2dcd 2523 return false;
2dbafdc0
KW
2524 }
2525
f4658285
SH
2526 do {
2527 retry = false;
2528 QLIST_FOREACH(req, &bs->tracked_requests, list) {
2dbafdc0 2529 if (req == self || (!req->serialising && !self->serialising)) {
65afd211
KW
2530 continue;
2531 }
7327145f
KW
2532 if (tracked_request_overlaps(req, self->overlap_offset,
2533 self->overlap_bytes))
2534 {
5f8b6491
SH
2535 /* Hitting this means there was a reentrant request, for
2536 * example, a block driver issuing nested requests. This must
2537 * never happen since it means deadlock.
2538 */
2539 assert(qemu_coroutine_self() != req->co);
2540
6460440f
KW
2541 /* If the request is already (indirectly) waiting for us, or
2542 * will wait for us as soon as it wakes up, then just go on
2543 * (instead of producing a deadlock in the former case). */
2544 if (!req->waiting_for) {
2545 self->waiting_for = req;
2546 qemu_co_queue_wait(&req->wait_queue);
2547 self->waiting_for = NULL;
2548 retry = true;
28de2dcd 2549 waited = true;
6460440f
KW
2550 break;
2551 }
f4658285
SH
2552 }
2553 }
2554 } while (retry);
28de2dcd
KW
2555
2556 return waited;
f4658285
SH
2557}
2558
756e6736
KW
2559/*
2560 * Return values:
2561 * 0 - success
2562 * -EINVAL - backing format specified, but no file
2563 * -ENOSPC - can't update the backing file because no space is left in the
2564 * image file header
2565 * -ENOTSUP - format driver doesn't support changing the backing file
2566 */
2567int bdrv_change_backing_file(BlockDriverState *bs,
2568 const char *backing_file, const char *backing_fmt)
2569{
2570 BlockDriver *drv = bs->drv;
469ef350 2571 int ret;
756e6736 2572
5f377794
PB
2573 /* Backing file format doesn't make sense without a backing file */
2574 if (backing_fmt && !backing_file) {
2575 return -EINVAL;
2576 }
2577
756e6736 2578 if (drv->bdrv_change_backing_file != NULL) {
469ef350 2579 ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
756e6736 2580 } else {
469ef350 2581 ret = -ENOTSUP;
756e6736 2582 }
469ef350
PB
2583
2584 if (ret == 0) {
2585 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
2586 pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
2587 }
2588 return ret;
756e6736
KW
2589}
2590
6ebdcee2
JC
2591/*
2592 * Finds the image layer in the chain that has 'bs' as its backing file.
2593 *
2594 * active is the current topmost image.
2595 *
2596 * Returns NULL if bs is not found in active's image chain,
2597 * or if active == bs.
4caf0fcd
JC
2598 *
2599 * Returns the bottommost base image if bs == NULL.
6ebdcee2
JC
2600 */
2601BlockDriverState *bdrv_find_overlay(BlockDriverState *active,
2602 BlockDriverState *bs)
2603{
4caf0fcd
JC
2604 while (active && bs != active->backing_hd) {
2605 active = active->backing_hd;
6ebdcee2
JC
2606 }
2607
4caf0fcd
JC
2608 return active;
2609}
6ebdcee2 2610
4caf0fcd
JC
2611/* Given a BDS, searches for the base layer. */
2612BlockDriverState *bdrv_find_base(BlockDriverState *bs)
2613{
2614 return bdrv_find_overlay(bs, NULL);
6ebdcee2
JC
2615}
2616
2617typedef struct BlkIntermediateStates {
2618 BlockDriverState *bs;
2619 QSIMPLEQ_ENTRY(BlkIntermediateStates) entry;
2620} BlkIntermediateStates;
2621
2622
2623/*
2624 * Drops images above 'base' up to and including 'top', and sets the image
2625 * above 'top' to have base as its backing file.
2626 *
2627 * Requires that the overlay to 'top' is opened r/w, so that the backing file
2628 * information in 'bs' can be properly updated.
2629 *
2630 * E.g., this will convert the following chain:
2631 * bottom <- base <- intermediate <- top <- active
2632 *
2633 * to
2634 *
2635 * bottom <- base <- active
2636 *
2637 * It is allowed for bottom==base, in which case it converts:
2638 *
2639 * base <- intermediate <- top <- active
2640 *
2641 * to
2642 *
2643 * base <- active
2644 *
54e26900
JC
2645 * If backing_file_str is non-NULL, it will be used when modifying top's
2646 * overlay image metadata.
2647 *
6ebdcee2
JC
2648 * Error conditions:
2649 * if active == top, that is considered an error
2650 *
2651 */
2652int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top,
54e26900 2653 BlockDriverState *base, const char *backing_file_str)
6ebdcee2
JC
2654{
2655 BlockDriverState *intermediate;
2656 BlockDriverState *base_bs = NULL;
2657 BlockDriverState *new_top_bs = NULL;
2658 BlkIntermediateStates *intermediate_state, *next;
2659 int ret = -EIO;
2660
2661 QSIMPLEQ_HEAD(states_to_delete, BlkIntermediateStates) states_to_delete;
2662 QSIMPLEQ_INIT(&states_to_delete);
2663
2664 if (!top->drv || !base->drv) {
2665 goto exit;
2666 }
2667
2668 new_top_bs = bdrv_find_overlay(active, top);
2669
2670 if (new_top_bs == NULL) {
2671 /* we could not find the image above 'top', this is an error */
2672 goto exit;
2673 }
2674
2675 /* special case of new_top_bs->backing_hd already pointing to base - nothing
2676 * to do, no intermediate images */
2677 if (new_top_bs->backing_hd == base) {
2678 ret = 0;
2679 goto exit;
2680 }
2681
2682 intermediate = top;
2683
2684 /* now we will go down through the list, and add each BDS we find
2685 * into our deletion queue, until we hit the 'base'
2686 */
2687 while (intermediate) {
5839e53b 2688 intermediate_state = g_new0(BlkIntermediateStates, 1);
6ebdcee2
JC
2689 intermediate_state->bs = intermediate;
2690 QSIMPLEQ_INSERT_TAIL(&states_to_delete, intermediate_state, entry);
2691
2692 if (intermediate->backing_hd == base) {
2693 base_bs = intermediate->backing_hd;
2694 break;
2695 }
2696 intermediate = intermediate->backing_hd;
2697 }
2698 if (base_bs == NULL) {
2699 /* something went wrong, we did not end at the base. safely
2700 * unravel everything, and exit with error */
2701 goto exit;
2702 }
2703
2704 /* success - we can delete the intermediate states, and link top->base */
54e26900
JC
2705 backing_file_str = backing_file_str ? backing_file_str : base_bs->filename;
2706 ret = bdrv_change_backing_file(new_top_bs, backing_file_str,
6ebdcee2
JC
2707 base_bs->drv ? base_bs->drv->format_name : "");
2708 if (ret) {
2709 goto exit;
2710 }
920beae1 2711 bdrv_set_backing_hd(new_top_bs, base_bs);
6ebdcee2
JC
2712
2713 QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) {
2714 /* so that bdrv_close() does not recursively close the chain */
920beae1 2715 bdrv_set_backing_hd(intermediate_state->bs, NULL);
4f6fd349 2716 bdrv_unref(intermediate_state->bs);
6ebdcee2
JC
2717 }
2718 ret = 0;
2719
2720exit:
2721 QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) {
2722 g_free(intermediate_state);
2723 }
2724 return ret;
2725}
2726
2727
71d0770c
AL
2728static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
2729 size_t size)
2730{
75af1f34 2731 if (size > BDRV_REQUEST_MAX_SECTORS << BDRV_SECTOR_BITS) {
1dd3a447
KW
2732 return -EIO;
2733 }
2734
c0191e76 2735 if (!bdrv_is_inserted(bs)) {
71d0770c 2736 return -ENOMEDIUM;
c0191e76 2737 }
71d0770c 2738
c0191e76 2739 if (offset < 0) {
71d0770c 2740 return -EIO;
c0191e76 2741 }
71d0770c
AL
2742
2743 return 0;
2744}
2745
2746static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
2747 int nb_sectors)
2748{
75af1f34 2749 if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) {
8f4754ed
KW
2750 return -EIO;
2751 }
2752
eb5a3165
JS
2753 return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE,
2754 nb_sectors * BDRV_SECTOR_SIZE);
71d0770c
AL
2755}
2756
1c9805a3
SH
2757typedef struct RwCo {
2758 BlockDriverState *bs;
775aa8b6 2759 int64_t offset;
1c9805a3
SH
2760 QEMUIOVector *qiov;
2761 bool is_write;
2762 int ret;
4105eaaa 2763 BdrvRequestFlags flags;
1c9805a3
SH
2764} RwCo;
2765
2766static void coroutine_fn bdrv_rw_co_entry(void *opaque)
fc01f7e7 2767{
1c9805a3 2768 RwCo *rwco = opaque;
ea2384d3 2769
1c9805a3 2770 if (!rwco->is_write) {
775aa8b6
KW
2771 rwco->ret = bdrv_co_do_preadv(rwco->bs, rwco->offset,
2772 rwco->qiov->size, rwco->qiov,
4105eaaa 2773 rwco->flags);
775aa8b6
KW
2774 } else {
2775 rwco->ret = bdrv_co_do_pwritev(rwco->bs, rwco->offset,
2776 rwco->qiov->size, rwco->qiov,
2777 rwco->flags);
1c9805a3
SH
2778 }
2779}
e7a8a783 2780
1c9805a3 2781/*
8d3b1a2d 2782 * Process a vectored synchronous request using coroutines
1c9805a3 2783 */
775aa8b6
KW
2784static int bdrv_prwv_co(BlockDriverState *bs, int64_t offset,
2785 QEMUIOVector *qiov, bool is_write,
2786 BdrvRequestFlags flags)
1c9805a3 2787{
1c9805a3
SH
2788 Coroutine *co;
2789 RwCo rwco = {
2790 .bs = bs,
775aa8b6 2791 .offset = offset,
8d3b1a2d 2792 .qiov = qiov,
1c9805a3
SH
2793 .is_write = is_write,
2794 .ret = NOT_DONE,
4105eaaa 2795 .flags = flags,
1c9805a3 2796 };
e7a8a783 2797
498e386c
ZYW
2798 /**
2799 * In sync call context, when the vcpu is blocked, this throttling timer
2800 * will not fire; so the I/O throttling function has to be disabled here
2801 * if it has been enabled.
2802 */
2803 if (bs->io_limits_enabled) {
2804 fprintf(stderr, "Disabling I/O throttling on '%s' due "
2805 "to synchronous I/O.\n", bdrv_get_device_name(bs));
2806 bdrv_io_limits_disable(bs);
2807 }
2808
1c9805a3
SH
2809 if (qemu_in_coroutine()) {
2810 /* Fast-path if already in coroutine context */
2811 bdrv_rw_co_entry(&rwco);
2812 } else {
2572b37a
SH
2813 AioContext *aio_context = bdrv_get_aio_context(bs);
2814
1c9805a3
SH
2815 co = qemu_coroutine_create(bdrv_rw_co_entry);
2816 qemu_coroutine_enter(co, &rwco);
2817 while (rwco.ret == NOT_DONE) {
2572b37a 2818 aio_poll(aio_context, true);
1c9805a3
SH
2819 }
2820 }
2821 return rwco.ret;
2822}
b338082b 2823
8d3b1a2d
KW
2824/*
2825 * Process a synchronous request using coroutines
2826 */
2827static int bdrv_rw_co(BlockDriverState *bs, int64_t sector_num, uint8_t *buf,
4105eaaa 2828 int nb_sectors, bool is_write, BdrvRequestFlags flags)
8d3b1a2d
KW
2829{
2830 QEMUIOVector qiov;
2831 struct iovec iov = {
2832 .iov_base = (void *)buf,
2833 .iov_len = nb_sectors * BDRV_SECTOR_SIZE,
2834 };
2835
75af1f34 2836 if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) {
da15ee51
KW
2837 return -EINVAL;
2838 }
2839
8d3b1a2d 2840 qemu_iovec_init_external(&qiov, &iov, 1);
775aa8b6
KW
2841 return bdrv_prwv_co(bs, sector_num << BDRV_SECTOR_BITS,
2842 &qiov, is_write, flags);
8d3b1a2d
KW
2843}
2844
1c9805a3
SH
2845/* return < 0 if error. See bdrv_write() for the return codes */
2846int bdrv_read(BlockDriverState *bs, int64_t sector_num,
2847 uint8_t *buf, int nb_sectors)
2848{
4105eaaa 2849 return bdrv_rw_co(bs, sector_num, buf, nb_sectors, false, 0);
fc01f7e7
FB
2850}
2851
07d27a44
MA
2852/* Just like bdrv_read(), but with I/O throttling temporarily disabled */
2853int bdrv_read_unthrottled(BlockDriverState *bs, int64_t sector_num,
2854 uint8_t *buf, int nb_sectors)
2855{
2856 bool enabled;
2857 int ret;
2858
2859 enabled = bs->io_limits_enabled;
2860 bs->io_limits_enabled = false;
4e7395e8 2861 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
07d27a44
MA
2862 bs->io_limits_enabled = enabled;
2863 return ret;
2864}
2865
5fafdf24 2866/* Return < 0 if error. Important errors are:
19cb3738
FB
2867 -EIO generic I/O error (may happen for all errors)
2868 -ENOMEDIUM No media inserted.
2869 -EINVAL Invalid sector number or nb_sectors
2870 -EACCES Trying to write a read-only device
2871*/
5fafdf24 2872int bdrv_write(BlockDriverState *bs, int64_t sector_num,
fc01f7e7
FB
2873 const uint8_t *buf, int nb_sectors)
2874{
4105eaaa 2875 return bdrv_rw_co(bs, sector_num, (uint8_t *)buf, nb_sectors, true, 0);
83f64091
FB
2876}
2877
aa7bfbff
PL
2878int bdrv_write_zeroes(BlockDriverState *bs, int64_t sector_num,
2879 int nb_sectors, BdrvRequestFlags flags)
4105eaaa
PL
2880{
2881 return bdrv_rw_co(bs, sector_num, NULL, nb_sectors, true,
aa7bfbff 2882 BDRV_REQ_ZERO_WRITE | flags);
8d3b1a2d
KW
2883}
2884
d75cbb5e
PL
2885/*
2886 * Completely zero out a block device with the help of bdrv_write_zeroes.
2887 * The operation is sped up by checking the block status and only writing
2888 * zeroes to the device if they currently do not return zeroes. Optional
2889 * flags are passed through to bdrv_write_zeroes (e.g. BDRV_REQ_MAY_UNMAP).
2890 *
2891 * Returns < 0 on error, 0 on success. For error codes see bdrv_write().
2892 */
2893int bdrv_make_zero(BlockDriverState *bs, BdrvRequestFlags flags)
2894{
d32f7c10 2895 int64_t target_sectors, ret, nb_sectors, sector_num = 0;
d75cbb5e
PL
2896 int n;
2897
d32f7c10
MA
2898 target_sectors = bdrv_nb_sectors(bs);
2899 if (target_sectors < 0) {
2900 return target_sectors;
9ce10c0b 2901 }
9ce10c0b 2902
d75cbb5e 2903 for (;;) {
75af1f34 2904 nb_sectors = MIN(target_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
d75cbb5e
PL
2905 if (nb_sectors <= 0) {
2906 return 0;
2907 }
d75cbb5e 2908 ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &n);
3d94ce60
PL
2909 if (ret < 0) {
2910 error_report("error getting block status at sector %" PRId64 ": %s",
2911 sector_num, strerror(-ret));
2912 return ret;
2913 }
d75cbb5e
PL
2914 if (ret & BDRV_BLOCK_ZERO) {
2915 sector_num += n;
2916 continue;
2917 }
2918 ret = bdrv_write_zeroes(bs, sector_num, n, flags);
2919 if (ret < 0) {
2920 error_report("error writing zeroes at sector %" PRId64 ": %s",
2921 sector_num, strerror(-ret));
2922 return ret;
2923 }
2924 sector_num += n;
2925 }
2926}
2927
a3ef6571 2928int bdrv_pread(BlockDriverState *bs, int64_t offset, void *buf, int bytes)
83f64091 2929{
a3ef6571
KW
2930 QEMUIOVector qiov;
2931 struct iovec iov = {
2932 .iov_base = (void *)buf,
2933 .iov_len = bytes,
2934 };
9a8c4cce 2935 int ret;
83f64091 2936
a3ef6571
KW
2937 if (bytes < 0) {
2938 return -EINVAL;
83f64091
FB
2939 }
2940
a3ef6571
KW
2941 qemu_iovec_init_external(&qiov, &iov, 1);
2942 ret = bdrv_prwv_co(bs, offset, &qiov, false, 0);
2943 if (ret < 0) {
2944 return ret;
83f64091 2945 }
a3ef6571
KW
2946
2947 return bytes;
83f64091
FB
2948}
2949
8d3b1a2d 2950int bdrv_pwritev(BlockDriverState *bs, int64_t offset, QEMUIOVector *qiov)
83f64091 2951{
9a8c4cce 2952 int ret;
83f64091 2953
8407d5d7
KW
2954 ret = bdrv_prwv_co(bs, offset, qiov, true, 0);
2955 if (ret < 0) {
2956 return ret;
83f64091
FB
2957 }
2958
8d3b1a2d
KW
2959 return qiov->size;
2960}
2961
2962int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
8407d5d7 2963 const void *buf, int bytes)
8d3b1a2d
KW
2964{
2965 QEMUIOVector qiov;
2966 struct iovec iov = {
2967 .iov_base = (void *) buf,
8407d5d7 2968 .iov_len = bytes,
8d3b1a2d
KW
2969 };
2970
8407d5d7
KW
2971 if (bytes < 0) {
2972 return -EINVAL;
2973 }
2974
8d3b1a2d
KW
2975 qemu_iovec_init_external(&qiov, &iov, 1);
2976 return bdrv_pwritev(bs, offset, &qiov);
83f64091 2977}
83f64091 2978
f08145fe
KW
2979/*
2980 * Writes to the file and ensures that no writes are reordered across this
2981 * request (acts as a barrier)
2982 *
2983 * Returns 0 on success, -errno in error cases.
2984 */
2985int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset,
2986 const void *buf, int count)
2987{
2988 int ret;
2989
2990 ret = bdrv_pwrite(bs, offset, buf, count);
2991 if (ret < 0) {
2992 return ret;
2993 }
2994
f05fa4ad
PB
2995 /* No flush needed for cache modes that already do it */
2996 if (bs->enable_write_cache) {
f08145fe
KW
2997 bdrv_flush(bs);
2998 }
2999
3000 return 0;
3001}
3002
470c0504 3003static int coroutine_fn bdrv_co_do_copy_on_readv(BlockDriverState *bs,
ab185921
SH
3004 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
3005{
3006 /* Perform I/O through a temporary buffer so that users who scribble over
3007 * their read buffer while the operation is in progress do not end up
3008 * modifying the image file. This is critical for zero-copy guest I/O
3009 * where anything might happen inside guest memory.
3010 */
3011 void *bounce_buffer;
3012
79c053bd 3013 BlockDriver *drv = bs->drv;
ab185921
SH
3014 struct iovec iov;
3015 QEMUIOVector bounce_qiov;
3016 int64_t cluster_sector_num;
3017 int cluster_nb_sectors;
3018 size_t skip_bytes;
3019 int ret;
3020
3021 /* Cover entire cluster so no additional backing file I/O is required when
3022 * allocating cluster in the image file.
3023 */
343bded4
PB
3024 bdrv_round_to_clusters(bs, sector_num, nb_sectors,
3025 &cluster_sector_num, &cluster_nb_sectors);
ab185921 3026
470c0504
SH
3027 trace_bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors,
3028 cluster_sector_num, cluster_nb_sectors);
ab185921
SH
3029
3030 iov.iov_len = cluster_nb_sectors * BDRV_SECTOR_SIZE;
857d4f46
KW
3031 iov.iov_base = bounce_buffer = qemu_try_blockalign(bs, iov.iov_len);
3032 if (bounce_buffer == NULL) {
3033 ret = -ENOMEM;
3034 goto err;
3035 }
3036
ab185921
SH
3037 qemu_iovec_init_external(&bounce_qiov, &iov, 1);
3038
79c053bd
SH
3039 ret = drv->bdrv_co_readv(bs, cluster_sector_num, cluster_nb_sectors,
3040 &bounce_qiov);
ab185921
SH
3041 if (ret < 0) {
3042 goto err;
3043 }
3044
79c053bd
SH
3045 if (drv->bdrv_co_write_zeroes &&
3046 buffer_is_zero(bounce_buffer, iov.iov_len)) {
621f0589 3047 ret = bdrv_co_do_write_zeroes(bs, cluster_sector_num,
aa7bfbff 3048 cluster_nb_sectors, 0);
79c053bd 3049 } else {
f05fa4ad
PB
3050 /* This does not change the data on the disk, it is not necessary
3051 * to flush even in cache=writethrough mode.
3052 */
79c053bd 3053 ret = drv->bdrv_co_writev(bs, cluster_sector_num, cluster_nb_sectors,
ab185921 3054 &bounce_qiov);
79c053bd
SH
3055 }
3056
ab185921
SH
3057 if (ret < 0) {
3058 /* It might be okay to ignore write errors for guest requests. If this
3059 * is a deliberate copy-on-read then we don't want to ignore the error.
3060 * Simply report it in all cases.
3061 */
3062 goto err;
3063 }
3064
3065 skip_bytes = (sector_num - cluster_sector_num) * BDRV_SECTOR_SIZE;
03396148
MT
3066 qemu_iovec_from_buf(qiov, 0, bounce_buffer + skip_bytes,
3067 nb_sectors * BDRV_SECTOR_SIZE);
ab185921
SH
3068
3069err:
3070 qemu_vfree(bounce_buffer);
3071 return ret;
3072}
3073
c5fbe571 3074/*
d0c7f642
KW
3075 * Forwards an already correctly aligned request to the BlockDriver. This
3076 * handles copy on read and zeroing after EOF; any other features must be
3077 * implemented by the caller.
c5fbe571 3078 */
d0c7f642 3079static int coroutine_fn bdrv_aligned_preadv(BlockDriverState *bs,
65afd211 3080 BdrvTrackedRequest *req, int64_t offset, unsigned int bytes,
ec746e10 3081 int64_t align, QEMUIOVector *qiov, int flags)
da1fa91d
KW
3082{
3083 BlockDriver *drv = bs->drv;
dbffbdcf 3084 int ret;
da1fa91d 3085
d0c7f642
KW
3086 int64_t sector_num = offset >> BDRV_SECTOR_BITS;
3087 unsigned int nb_sectors = bytes >> BDRV_SECTOR_BITS;
da1fa91d 3088
d0c7f642
KW
3089 assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
3090 assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
8eb029c2 3091 assert(!qiov || bytes == qiov->size);
d0c7f642
KW
3092
3093 /* Handle Copy on Read and associated serialisation */
470c0504 3094 if (flags & BDRV_REQ_COPY_ON_READ) {
7327145f
KW
3095 /* If we touch the same cluster it counts as an overlap. This
3096 * guarantees that allocating writes will be serialized and not race
3097 * with each other for the same cluster. For example, in copy-on-read
3098 * it ensures that the CoR read and write operations are atomic and
3099 * guest writes cannot interleave between them. */
3100 mark_request_serialising(req, bdrv_get_cluster_size(bs));
470c0504
SH
3101 }
3102
2dbafdc0 3103 wait_serialising_requests(req);
f4658285 3104
470c0504 3105 if (flags & BDRV_REQ_COPY_ON_READ) {
ab185921
SH
3106 int pnum;
3107
bdad13b9 3108 ret = bdrv_is_allocated(bs, sector_num, nb_sectors, &pnum);
ab185921
SH
3109 if (ret < 0) {
3110 goto out;
3111 }
3112
3113 if (!ret || pnum != nb_sectors) {
470c0504 3114 ret = bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, qiov);
ab185921
SH
3115 goto out;
3116 }
3117 }
3118
d0c7f642 3119 /* Forward the request to the BlockDriver */
c0191e76 3120 if (!bs->zero_beyond_eof) {
893a8f62
MK
3121 ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov);
3122 } else {
c0191e76 3123 /* Read zeros after EOF */
4049082c 3124 int64_t total_sectors, max_nb_sectors;
893a8f62 3125
4049082c
MA
3126 total_sectors = bdrv_nb_sectors(bs);
3127 if (total_sectors < 0) {
3128 ret = total_sectors;
893a8f62
MK
3129 goto out;
3130 }
3131
5f5bcd80
KW
3132 max_nb_sectors = ROUND_UP(MAX(0, total_sectors - sector_num),
3133 align >> BDRV_SECTOR_BITS);
e012b78c
PB
3134 if (nb_sectors < max_nb_sectors) {
3135 ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov);
3136 } else if (max_nb_sectors > 0) {
33f461e0 3137 QEMUIOVector local_qiov;
33f461e0
KW
3138
3139 qemu_iovec_init(&local_qiov, qiov->niov);
3140 qemu_iovec_concat(&local_qiov, qiov, 0,
e012b78c 3141 max_nb_sectors * BDRV_SECTOR_SIZE);
33f461e0 3142
e012b78c 3143 ret = drv->bdrv_co_readv(bs, sector_num, max_nb_sectors,
33f461e0
KW
3144 &local_qiov);
3145
3146 qemu_iovec_destroy(&local_qiov);
893a8f62
MK
3147 } else {
3148 ret = 0;
3149 }
3150
3151 /* Reading beyond end of file is supposed to produce zeroes */
3152 if (ret == 0 && total_sectors < sector_num + nb_sectors) {
3153 uint64_t offset = MAX(0, total_sectors - sector_num);
3154 uint64_t bytes = (sector_num + nb_sectors - offset) *
3155 BDRV_SECTOR_SIZE;
3156 qemu_iovec_memset(qiov, offset * BDRV_SECTOR_SIZE, 0, bytes);
3157 }
3158 }
ab185921
SH
3159
3160out:
dbffbdcf 3161 return ret;
da1fa91d
KW
3162}
3163
fc3959e4
FZ
3164static inline uint64_t bdrv_get_align(BlockDriverState *bs)
3165{
3166 /* TODO Lift BDRV_SECTOR_SIZE restriction in BlockDriver interface */
3167 return MAX(BDRV_SECTOR_SIZE, bs->request_alignment);
3168}
3169
3170static inline bool bdrv_req_is_aligned(BlockDriverState *bs,
3171 int64_t offset, size_t bytes)
3172{
3173 int64_t align = bdrv_get_align(bs);
3174 return !(offset & (align - 1) || (bytes & (align - 1)));
3175}
3176
d0c7f642
KW
3177/*
3178 * Handle a read request in coroutine context
3179 */
1b0288ae
KW
3180static int coroutine_fn bdrv_co_do_preadv(BlockDriverState *bs,
3181 int64_t offset, unsigned int bytes, QEMUIOVector *qiov,
d0c7f642
KW
3182 BdrvRequestFlags flags)
3183{
3184 BlockDriver *drv = bs->drv;
65afd211
KW
3185 BdrvTrackedRequest req;
3186
fc3959e4 3187 uint64_t align = bdrv_get_align(bs);
1b0288ae
KW
3188 uint8_t *head_buf = NULL;
3189 uint8_t *tail_buf = NULL;
3190 QEMUIOVector local_qiov;
3191 bool use_local_qiov = false;
d0c7f642
KW
3192 int ret;
3193
3194 if (!drv) {
3195 return -ENOMEDIUM;
3196 }
b9c64947
HR
3197
3198 ret = bdrv_check_byte_request(bs, offset, bytes);
3199 if (ret < 0) {
3200 return ret;
d0c7f642
KW
3201 }
3202
3203 if (bs->copy_on_read) {
3204 flags |= BDRV_REQ_COPY_ON_READ;
3205 }
3206
3207 /* throttling disk I/O */
3208 if (bs->io_limits_enabled) {
d5103588 3209 bdrv_io_limits_intercept(bs, bytes, false);
1b0288ae
KW
3210 }
3211
3212 /* Align read if necessary by padding qiov */
3213 if (offset & (align - 1)) {
3214 head_buf = qemu_blockalign(bs, align);
3215 qemu_iovec_init(&local_qiov, qiov->niov + 2);
3216 qemu_iovec_add(&local_qiov, head_buf, offset & (align - 1));
3217 qemu_iovec_concat(&local_qiov, qiov, 0, qiov->size);
3218 use_local_qiov = true;
3219
3220 bytes += offset & (align - 1);
3221 offset = offset & ~(align - 1);
3222 }
3223
3224 if ((offset + bytes) & (align - 1)) {
3225 if (!use_local_qiov) {
3226 qemu_iovec_init(&local_qiov, qiov->niov + 1);
3227 qemu_iovec_concat(&local_qiov, qiov, 0, qiov->size);
3228 use_local_qiov = true;
3229 }
3230 tail_buf = qemu_blockalign(bs, align);
3231 qemu_iovec_add(&local_qiov, tail_buf,
3232 align - ((offset + bytes) & (align - 1)));
3233
3234 bytes = ROUND_UP(bytes, align);
3235 }
3236
65afd211 3237 tracked_request_begin(&req, bs, offset, bytes, false);
ec746e10 3238 ret = bdrv_aligned_preadv(bs, &req, offset, bytes, align,
1b0288ae
KW
3239 use_local_qiov ? &local_qiov : qiov,
3240 flags);
65afd211 3241 tracked_request_end(&req);
1b0288ae
KW
3242
3243 if (use_local_qiov) {
3244 qemu_iovec_destroy(&local_qiov);
3245 qemu_vfree(head_buf);
3246 qemu_vfree(tail_buf);
d0c7f642
KW
3247 }
3248
d0c7f642
KW
3249 return ret;
3250}
3251
1b0288ae
KW
3252static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs,
3253 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
3254 BdrvRequestFlags flags)
3255{
75af1f34 3256 if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) {
1b0288ae
KW
3257 return -EINVAL;
3258 }
3259
3260 return bdrv_co_do_preadv(bs, sector_num << BDRV_SECTOR_BITS,
3261 nb_sectors << BDRV_SECTOR_BITS, qiov, flags);
3262}
3263
c5fbe571 3264int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num,
da1fa91d
KW
3265 int nb_sectors, QEMUIOVector *qiov)
3266{
c5fbe571 3267 trace_bdrv_co_readv(bs, sector_num, nb_sectors);
da1fa91d 3268
470c0504
SH
3269 return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 0);
3270}
3271
3272int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs,
3273 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
3274{
3275 trace_bdrv_co_copy_on_readv(bs, sector_num, nb_sectors);
3276
3277 return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov,
3278 BDRV_REQ_COPY_ON_READ);
c5fbe571
SH
3279}
3280
98764152 3281#define MAX_WRITE_ZEROES_BOUNCE_BUFFER 32768
c31cb707 3282
f08f2dda 3283static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs,
aa7bfbff 3284 int64_t sector_num, int nb_sectors, BdrvRequestFlags flags)
f08f2dda
SH
3285{
3286 BlockDriver *drv = bs->drv;
3287 QEMUIOVector qiov;
c31cb707
PL
3288 struct iovec iov = {0};
3289 int ret = 0;
f08f2dda 3290
75af1f34
PL
3291 int max_write_zeroes = MIN_NON_ZERO(bs->bl.max_write_zeroes,
3292 BDRV_REQUEST_MAX_SECTORS);
621f0589 3293
c31cb707
PL
3294 while (nb_sectors > 0 && !ret) {
3295 int num = nb_sectors;
3296
b8d71c09
PB
3297 /* Align request. Block drivers can expect the "bulk" of the request
3298 * to be aligned.
3299 */
3300 if (bs->bl.write_zeroes_alignment
3301 && num > bs->bl.write_zeroes_alignment) {
3302 if (sector_num % bs->bl.write_zeroes_alignment != 0) {
3303 /* Make a small request up to the first aligned sector. */
c31cb707 3304 num = bs->bl.write_zeroes_alignment;
b8d71c09
PB
3305 num -= sector_num % bs->bl.write_zeroes_alignment;
3306 } else if ((sector_num + num) % bs->bl.write_zeroes_alignment != 0) {
3307 /* Shorten the request to the last aligned sector. num cannot
3308 * underflow because num > bs->bl.write_zeroes_alignment.
3309 */
3310 num -= (sector_num + num) % bs->bl.write_zeroes_alignment;
c31cb707 3311 }
621f0589 3312 }
f08f2dda 3313
c31cb707
PL
3314 /* limit request size */
3315 if (num > max_write_zeroes) {
3316 num = max_write_zeroes;
3317 }
3318
3319 ret = -ENOTSUP;
3320 /* First try the efficient write zeroes operation */
3321 if (drv->bdrv_co_write_zeroes) {
3322 ret = drv->bdrv_co_write_zeroes(bs, sector_num, num, flags);
3323 }
3324
3325 if (ret == -ENOTSUP) {
3326 /* Fall back to bounce buffer if write zeroes is unsupported */
095e4fa4 3327 int max_xfer_len = MIN_NON_ZERO(bs->bl.max_transfer_length,
98764152 3328 MAX_WRITE_ZEROES_BOUNCE_BUFFER);
095e4fa4 3329 num = MIN(num, max_xfer_len);
c31cb707
PL
3330 iov.iov_len = num * BDRV_SECTOR_SIZE;
3331 if (iov.iov_base == NULL) {
857d4f46
KW
3332 iov.iov_base = qemu_try_blockalign(bs, num * BDRV_SECTOR_SIZE);
3333 if (iov.iov_base == NULL) {
3334 ret = -ENOMEM;
3335 goto fail;
3336 }
b8d71c09 3337 memset(iov.iov_base, 0, num * BDRV_SECTOR_SIZE);
c31cb707
PL
3338 }
3339 qemu_iovec_init_external(&qiov, &iov, 1);
f08f2dda 3340
c31cb707 3341 ret = drv->bdrv_co_writev(bs, sector_num, num, &qiov);
b8d71c09
PB
3342
3343 /* Keep bounce buffer around if it is big enough for all
3344 * all future requests.
3345 */
095e4fa4 3346 if (num < max_xfer_len) {
b8d71c09
PB
3347 qemu_vfree(iov.iov_base);
3348 iov.iov_base = NULL;
3349 }
c31cb707
PL
3350 }
3351
3352 sector_num += num;
3353 nb_sectors -= num;
3354 }
f08f2dda 3355
857d4f46 3356fail:
f08f2dda
SH
3357 qemu_vfree(iov.iov_base);
3358 return ret;
3359}
3360
c5fbe571 3361/*
b404f720 3362 * Forwards an already correctly aligned write request to the BlockDriver.
c5fbe571 3363 */
b404f720 3364static int coroutine_fn bdrv_aligned_pwritev(BlockDriverState *bs,
65afd211
KW
3365 BdrvTrackedRequest *req, int64_t offset, unsigned int bytes,
3366 QEMUIOVector *qiov, int flags)
c5fbe571
SH
3367{
3368 BlockDriver *drv = bs->drv;
28de2dcd 3369 bool waited;
6b7cb247 3370 int ret;
da1fa91d 3371
b404f720
KW
3372 int64_t sector_num = offset >> BDRV_SECTOR_BITS;
3373 unsigned int nb_sectors = bytes >> BDRV_SECTOR_BITS;
f4658285 3374
b404f720
KW
3375 assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
3376 assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
8eb029c2 3377 assert(!qiov || bytes == qiov->size);
cc0681c4 3378
28de2dcd
KW
3379 waited = wait_serialising_requests(req);
3380 assert(!waited || !req->serialising);
af91f9a7
KW
3381 assert(req->overlap_offset <= offset);
3382 assert(offset + bytes <= req->overlap_offset + req->overlap_bytes);
244eadef 3383
65afd211 3384 ret = notifier_with_return_list_notify(&bs->before_write_notifiers, req);
d616b224 3385
465bee1d
PL
3386 if (!ret && bs->detect_zeroes != BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF &&
3387 !(flags & BDRV_REQ_ZERO_WRITE) && drv->bdrv_co_write_zeroes &&
3388 qemu_iovec_is_zero(qiov)) {
3389 flags |= BDRV_REQ_ZERO_WRITE;
3390 if (bs->detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP) {
3391 flags |= BDRV_REQ_MAY_UNMAP;
3392 }
3393 }
3394
d616b224
SH
3395 if (ret < 0) {
3396 /* Do nothing, write notifier decided to fail this request */
3397 } else if (flags & BDRV_REQ_ZERO_WRITE) {
9e1cb96d 3398 BLKDBG_EVENT(bs, BLKDBG_PWRITEV_ZERO);
aa7bfbff 3399 ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors, flags);
f08f2dda 3400 } else {
9e1cb96d 3401 BLKDBG_EVENT(bs, BLKDBG_PWRITEV);
f08f2dda
SH
3402 ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov);
3403 }
9e1cb96d 3404 BLKDBG_EVENT(bs, BLKDBG_PWRITEV_DONE);
6b7cb247 3405
f05fa4ad
PB
3406 if (ret == 0 && !bs->enable_write_cache) {
3407 ret = bdrv_co_flush(bs);
3408 }
3409
e4654d2d 3410 bdrv_set_dirty(bs, sector_num, nb_sectors);
da1fa91d 3411
5366d0c8 3412 block_acct_highest_sector(&bs->stats, sector_num, nb_sectors);
5e5a94b6 3413
c0191e76 3414 if (ret >= 0) {
df2a6f29
PB
3415 bs->total_sectors = MAX(bs->total_sectors, sector_num + nb_sectors);
3416 }
da1fa91d 3417
6b7cb247 3418 return ret;
da1fa91d
KW
3419}
3420
b404f720
KW
3421/*
3422 * Handle a write request in coroutine context
3423 */
6601553e
KW
3424static int coroutine_fn bdrv_co_do_pwritev(BlockDriverState *bs,
3425 int64_t offset, unsigned int bytes, QEMUIOVector *qiov,
b404f720
KW
3426 BdrvRequestFlags flags)
3427{
65afd211 3428 BdrvTrackedRequest req;
fc3959e4 3429 uint64_t align = bdrv_get_align(bs);
3b8242e0
KW
3430 uint8_t *head_buf = NULL;
3431 uint8_t *tail_buf = NULL;
3432 QEMUIOVector local_qiov;
3433 bool use_local_qiov = false;
b404f720
KW
3434 int ret;
3435
3436 if (!bs->drv) {
3437 return -ENOMEDIUM;
3438 }
3439 if (bs->read_only) {
3440 return -EACCES;
3441 }
b9c64947
HR
3442
3443 ret = bdrv_check_byte_request(bs, offset, bytes);
3444 if (ret < 0) {
3445 return ret;
b404f720
KW
3446 }
3447
b404f720
KW
3448 /* throttling disk I/O */
3449 if (bs->io_limits_enabled) {
d5103588 3450 bdrv_io_limits_intercept(bs, bytes, true);
b404f720
KW
3451 }
3452
3b8242e0
KW
3453 /*
3454 * Align write if necessary by performing a read-modify-write cycle.
3455 * Pad qiov with the read parts and be sure to have a tracked request not
3456 * only for bdrv_aligned_pwritev, but also for the reads of the RMW cycle.
3457 */
65afd211 3458 tracked_request_begin(&req, bs, offset, bytes, true);
3b8242e0
KW
3459
3460 if (offset & (align - 1)) {
3461 QEMUIOVector head_qiov;
3462 struct iovec head_iov;
3463
3464 mark_request_serialising(&req, align);
3465 wait_serialising_requests(&req);
3466
3467 head_buf = qemu_blockalign(bs, align);
3468 head_iov = (struct iovec) {
3469 .iov_base = head_buf,
3470 .iov_len = align,
3471 };
3472 qemu_iovec_init_external(&head_qiov, &head_iov, 1);
3473
9e1cb96d 3474 BLKDBG_EVENT(bs, BLKDBG_PWRITEV_RMW_HEAD);
3b8242e0
KW
3475 ret = bdrv_aligned_preadv(bs, &req, offset & ~(align - 1), align,
3476 align, &head_qiov, 0);
3477 if (ret < 0) {
3478 goto fail;
3479 }
9e1cb96d 3480 BLKDBG_EVENT(bs, BLKDBG_PWRITEV_RMW_AFTER_HEAD);
3b8242e0
KW
3481
3482 qemu_iovec_init(&local_qiov, qiov->niov + 2);
3483 qemu_iovec_add(&local_qiov, head_buf, offset & (align - 1));
3484 qemu_iovec_concat(&local_qiov, qiov, 0, qiov->size);
3485 use_local_qiov = true;
3486
3487 bytes += offset & (align - 1);
3488 offset = offset & ~(align - 1);
3489 }
3490
3491 if ((offset + bytes) & (align - 1)) {
3492 QEMUIOVector tail_qiov;
3493 struct iovec tail_iov;
3494 size_t tail_bytes;
28de2dcd 3495 bool waited;
3b8242e0
KW
3496
3497 mark_request_serialising(&req, align);
28de2dcd
KW
3498 waited = wait_serialising_requests(&req);
3499 assert(!waited || !use_local_qiov);
3b8242e0
KW
3500
3501 tail_buf = qemu_blockalign(bs, align);
3502 tail_iov = (struct iovec) {
3503 .iov_base = tail_buf,
3504 .iov_len = align,
3505 };
3506 qemu_iovec_init_external(&tail_qiov, &tail_iov, 1);
3507
9e1cb96d 3508 BLKDBG_EVENT(bs, BLKDBG_PWRITEV_RMW_TAIL);
3b8242e0
KW
3509 ret = bdrv_aligned_preadv(bs, &req, (offset + bytes) & ~(align - 1), align,
3510 align, &tail_qiov, 0);
3511 if (ret < 0) {
3512 goto fail;
3513 }
9e1cb96d 3514 BLKDBG_EVENT(bs, BLKDBG_PWRITEV_RMW_AFTER_TAIL);
3b8242e0
KW
3515
3516 if (!use_local_qiov) {
3517 qemu_iovec_init(&local_qiov, qiov->niov + 1);
3518 qemu_iovec_concat(&local_qiov, qiov, 0, qiov->size);
3519 use_local_qiov = true;
3520 }
3521
3522 tail_bytes = (offset + bytes) & (align - 1);
3523 qemu_iovec_add(&local_qiov, tail_buf + tail_bytes, align - tail_bytes);
3524
3525 bytes = ROUND_UP(bytes, align);
3526 }
3527
fc3959e4
FZ
3528 if (use_local_qiov) {
3529 /* Local buffer may have non-zero data. */
3530 flags &= ~BDRV_REQ_ZERO_WRITE;
3531 }
3b8242e0
KW
3532 ret = bdrv_aligned_pwritev(bs, &req, offset, bytes,
3533 use_local_qiov ? &local_qiov : qiov,
3534 flags);
3535
3536fail:
65afd211 3537 tracked_request_end(&req);
b404f720 3538
3b8242e0
KW
3539 if (use_local_qiov) {
3540 qemu_iovec_destroy(&local_qiov);
3b8242e0 3541 }
99c4a85c
KW
3542 qemu_vfree(head_buf);
3543 qemu_vfree(tail_buf);
3b8242e0 3544
b404f720
KW
3545 return ret;
3546}
3547
6601553e
KW
3548static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs,
3549 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
3550 BdrvRequestFlags flags)
3551{
75af1f34 3552 if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) {
6601553e
KW
3553 return -EINVAL;
3554 }
3555
3556 return bdrv_co_do_pwritev(bs, sector_num << BDRV_SECTOR_BITS,
3557 nb_sectors << BDRV_SECTOR_BITS, qiov, flags);
3558}
3559
c5fbe571
SH
3560int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num,
3561 int nb_sectors, QEMUIOVector *qiov)
3562{
3563 trace_bdrv_co_writev(bs, sector_num, nb_sectors);
3564
f08f2dda
SH
3565 return bdrv_co_do_writev(bs, sector_num, nb_sectors, qiov, 0);
3566}
3567
3568int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs,
aa7bfbff
PL
3569 int64_t sector_num, int nb_sectors,
3570 BdrvRequestFlags flags)
f08f2dda 3571{
fc3959e4
FZ
3572 int ret;
3573
94d6ff21 3574 trace_bdrv_co_write_zeroes(bs, sector_num, nb_sectors, flags);
f08f2dda 3575
d32f35cb
PL
3576 if (!(bs->open_flags & BDRV_O_UNMAP)) {
3577 flags &= ~BDRV_REQ_MAY_UNMAP;
3578 }
fc3959e4
FZ
3579 if (bdrv_req_is_aligned(bs, sector_num << BDRV_SECTOR_BITS,
3580 nb_sectors << BDRV_SECTOR_BITS)) {
3581 ret = bdrv_co_do_writev(bs, sector_num, nb_sectors, NULL,
3582 BDRV_REQ_ZERO_WRITE | flags);
3583 } else {
3584 uint8_t *buf;
3585 QEMUIOVector local_qiov;
3586 size_t bytes = nb_sectors << BDRV_SECTOR_BITS;
3587
3588 buf = qemu_memalign(bdrv_opt_mem_align(bs), bytes);
3589 memset(buf, 0, bytes);
3590 qemu_iovec_init(&local_qiov, 1);
3591 qemu_iovec_add(&local_qiov, buf, bytes);
d32f35cb 3592
fc3959e4
FZ
3593 ret = bdrv_co_do_writev(bs, sector_num, nb_sectors, &local_qiov,
3594 BDRV_REQ_ZERO_WRITE | flags);
3595 qemu_vfree(buf);
3596 }
3597 return ret;
c5fbe571
SH
3598}
3599
83f64091
FB
3600/**
3601 * Truncate file to 'offset' bytes (needed only for file protocols)
3602 */
3603int bdrv_truncate(BlockDriverState *bs, int64_t offset)
3604{
3605 BlockDriver *drv = bs->drv;
51762288 3606 int ret;
83f64091 3607 if (!drv)
19cb3738 3608 return -ENOMEDIUM;
83f64091
FB
3609 if (!drv->bdrv_truncate)
3610 return -ENOTSUP;
59f2689d
NS
3611 if (bs->read_only)
3612 return -EACCES;
9c75e168 3613
51762288
SH
3614 ret = drv->bdrv_truncate(bs, offset);
3615 if (ret == 0) {
3616 ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
ce1ffea8 3617 bdrv_dirty_bitmap_truncate(bs);
a7f53e26
MA
3618 if (bs->blk) {
3619 blk_dev_resize_cb(bs->blk);
3620 }
51762288
SH
3621 }
3622 return ret;
83f64091
FB
3623}
3624
4a1d5e1f
FZ
3625/**
3626 * Length of a allocated file in bytes. Sparse files are counted by actual
3627 * allocated space. Return < 0 if error or unknown.
3628 */
3629int64_t bdrv_get_allocated_file_size(BlockDriverState *bs)
3630{
3631 BlockDriver *drv = bs->drv;
3632 if (!drv) {
3633 return -ENOMEDIUM;
3634 }
3635 if (drv->bdrv_get_allocated_file_size) {
3636 return drv->bdrv_get_allocated_file_size(bs);
3637 }
3638 if (bs->file) {
3639 return bdrv_get_allocated_file_size(bs->file);
3640 }
3641 return -ENOTSUP;
3642}
3643
83f64091 3644/**
65a9bb25 3645 * Return number of sectors on success, -errno on error.
83f64091 3646 */
65a9bb25 3647int64_t bdrv_nb_sectors(BlockDriverState *bs)
83f64091
FB
3648{
3649 BlockDriver *drv = bs->drv;
65a9bb25 3650
83f64091 3651 if (!drv)
19cb3738 3652 return -ENOMEDIUM;
51762288 3653
b94a2610
KW
3654 if (drv->has_variable_length) {
3655 int ret = refresh_total_sectors(bs, bs->total_sectors);
3656 if (ret < 0) {
3657 return ret;
46a4e4e6 3658 }
83f64091 3659 }
65a9bb25
MA
3660 return bs->total_sectors;
3661}
3662
3663/**
3664 * Return length in bytes on success, -errno on error.
3665 * The length is always a multiple of BDRV_SECTOR_SIZE.
3666 */
3667int64_t bdrv_getlength(BlockDriverState *bs)
3668{
3669 int64_t ret = bdrv_nb_sectors(bs);
3670
3671 return ret < 0 ? ret : ret * BDRV_SECTOR_SIZE;
fc01f7e7
FB
3672}
3673
19cb3738 3674/* return 0 as number of sectors if no device present or error */
96b8f136 3675void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
fc01f7e7 3676{
65a9bb25
MA
3677 int64_t nb_sectors = bdrv_nb_sectors(bs);
3678
3679 *nb_sectors_ptr = nb_sectors < 0 ? 0 : nb_sectors;
fc01f7e7 3680}
cf98951b 3681
ff06f5f3
PB
3682void bdrv_set_on_error(BlockDriverState *bs, BlockdevOnError on_read_error,
3683 BlockdevOnError on_write_error)
abd7f68d
MA
3684{
3685 bs->on_read_error = on_read_error;
3686 bs->on_write_error = on_write_error;
3687}
3688
1ceee0d5 3689BlockdevOnError bdrv_get_on_error(BlockDriverState *bs, bool is_read)
abd7f68d
MA
3690{
3691 return is_read ? bs->on_read_error : bs->on_write_error;
3692}
3693
3e1caa5f
PB
3694BlockErrorAction bdrv_get_error_action(BlockDriverState *bs, bool is_read, int error)
3695{
3696 BlockdevOnError on_err = is_read ? bs->on_read_error : bs->on_write_error;
3697
3698 switch (on_err) {
3699 case BLOCKDEV_ON_ERROR_ENOSPC:
a589569f
WX
3700 return (error == ENOSPC) ?
3701 BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
3e1caa5f 3702 case BLOCKDEV_ON_ERROR_STOP:
a589569f 3703 return BLOCK_ERROR_ACTION_STOP;
3e1caa5f 3704 case BLOCKDEV_ON_ERROR_REPORT:
a589569f 3705 return BLOCK_ERROR_ACTION_REPORT;
3e1caa5f 3706 case BLOCKDEV_ON_ERROR_IGNORE:
a589569f 3707 return BLOCK_ERROR_ACTION_IGNORE;
3e1caa5f
PB
3708 default:
3709 abort();
3710 }
3711}
3712
c7c2ff0c
LC
3713static void send_qmp_error_event(BlockDriverState *bs,
3714 BlockErrorAction action,
3715 bool is_read, int error)
3716{
573742a5 3717 IoOperationType optype;
c7c2ff0c 3718
573742a5
PM
3719 optype = is_read ? IO_OPERATION_TYPE_READ : IO_OPERATION_TYPE_WRITE;
3720 qapi_event_send_block_io_error(bdrv_get_device_name(bs), optype, action,
c7c2ff0c 3721 bdrv_iostatus_is_enabled(bs),
624ff573
LC
3722 error == ENOSPC, strerror(error),
3723 &error_abort);
c7c2ff0c
LC
3724}
3725
3e1caa5f
PB
3726/* This is done by device models because, while the block layer knows
3727 * about the error, it does not know whether an operation comes from
3728 * the device or the block layer (from a job, for example).
3729 */
3730void bdrv_error_action(BlockDriverState *bs, BlockErrorAction action,
3731 bool is_read, int error)
3732{
3733 assert(error >= 0);
2bd3bce8 3734
a589569f 3735 if (action == BLOCK_ERROR_ACTION_STOP) {
2bd3bce8
PB
3736 /* First set the iostatus, so that "info block" returns an iostatus
3737 * that matches the events raised so far (an additional error iostatus
3738 * is fine, but not a lost one).
3739 */
3e1caa5f 3740 bdrv_iostatus_set_err(bs, error);
2bd3bce8
PB
3741
3742 /* Then raise the request to stop the VM and the event.
3743 * qemu_system_vmstop_request_prepare has two effects. First,
3744 * it ensures that the STOP event always comes after the
3745 * BLOCK_IO_ERROR event. Second, it ensures that even if management
3746 * can observe the STOP event and do a "cont" before the STOP
3747 * event is issued, the VM will not stop. In this case, vm_start()
3748 * also ensures that the STOP/RESUME pair of events is emitted.
3749 */
3750 qemu_system_vmstop_request_prepare();
c7c2ff0c 3751 send_qmp_error_event(bs, action, is_read, error);
2bd3bce8
PB
3752 qemu_system_vmstop_request(RUN_STATE_IO_ERROR);
3753 } else {
c7c2ff0c 3754 send_qmp_error_event(bs, action, is_read, error);
3e1caa5f
PB
3755 }
3756}
3757
b338082b
FB
3758int bdrv_is_read_only(BlockDriverState *bs)
3759{
3760 return bs->read_only;
3761}
3762
985a03b0
TS
3763int bdrv_is_sg(BlockDriverState *bs)
3764{
3765 return bs->sg;
3766}
3767
e900a7b7
CH
3768int bdrv_enable_write_cache(BlockDriverState *bs)
3769{
3770 return bs->enable_write_cache;
3771}
3772
425b0148
PB
3773void bdrv_set_enable_write_cache(BlockDriverState *bs, bool wce)
3774{
3775 bs->enable_write_cache = wce;
55b110f2
JC
3776
3777 /* so a reopen() will preserve wce */
3778 if (wce) {
3779 bs->open_flags |= BDRV_O_CACHE_WB;
3780 } else {
3781 bs->open_flags &= ~BDRV_O_CACHE_WB;
3782 }
425b0148
PB
3783}
3784
ea2384d3
FB
3785int bdrv_is_encrypted(BlockDriverState *bs)
3786{
3787 if (bs->backing_hd && bs->backing_hd->encrypted)
3788 return 1;
3789 return bs->encrypted;
3790}
3791
c0f4ce77
AL
3792int bdrv_key_required(BlockDriverState *bs)
3793{
3794 BlockDriverState *backing_hd = bs->backing_hd;
3795
3796 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
3797 return 1;
3798 return (bs->encrypted && !bs->valid_key);
3799}
3800
ea2384d3
FB
3801int bdrv_set_key(BlockDriverState *bs, const char *key)
3802{
3803 int ret;
3804 if (bs->backing_hd && bs->backing_hd->encrypted) {
3805 ret = bdrv_set_key(bs->backing_hd, key);
3806 if (ret < 0)
3807 return ret;
3808 if (!bs->encrypted)
3809 return 0;
3810 }
fd04a2ae
SH
3811 if (!bs->encrypted) {
3812 return -EINVAL;
3813 } else if (!bs->drv || !bs->drv->bdrv_set_key) {
3814 return -ENOMEDIUM;
3815 }
c0f4ce77 3816 ret = bs->drv->bdrv_set_key(bs, key);
bb5fc20f
AL
3817 if (ret < 0) {
3818 bs->valid_key = 0;
3819 } else if (!bs->valid_key) {
3820 bs->valid_key = 1;
a7f53e26
MA
3821 if (bs->blk) {
3822 /* call the change callback now, we skipped it on open */
3823 blk_dev_change_media_cb(bs->blk, true);
3824 }
bb5fc20f 3825 }
c0f4ce77 3826 return ret;
ea2384d3
FB
3827}
3828
4d2855a3
MA
3829/*
3830 * Provide an encryption key for @bs.
3831 * If @key is non-null:
3832 * If @bs is not encrypted, fail.
3833 * Else if the key is invalid, fail.
3834 * Else set @bs's key to @key, replacing the existing key, if any.
3835 * If @key is null:
3836 * If @bs is encrypted and still lacks a key, fail.
3837 * Else do nothing.
3838 * On failure, store an error object through @errp if non-null.
3839 */
3840void bdrv_add_key(BlockDriverState *bs, const char *key, Error **errp)
3841{
3842 if (key) {
3843 if (!bdrv_is_encrypted(bs)) {
81e5f78a
AG
3844 error_setg(errp, "Node '%s' is not encrypted",
3845 bdrv_get_device_or_node_name(bs));
4d2855a3
MA
3846 } else if (bdrv_set_key(bs, key) < 0) {
3847 error_set(errp, QERR_INVALID_PASSWORD);
3848 }
3849 } else {
3850 if (bdrv_key_required(bs)) {
b1ca6391
MA
3851 error_set(errp, ERROR_CLASS_DEVICE_ENCRYPTED,
3852 "'%s' (%s) is encrypted",
81e5f78a 3853 bdrv_get_device_or_node_name(bs),
4d2855a3
MA
3854 bdrv_get_encrypted_filename(bs));
3855 }
3856 }
3857}
3858
f8d6bba1 3859const char *bdrv_get_format_name(BlockDriverState *bs)
ea2384d3 3860{
f8d6bba1 3861 return bs->drv ? bs->drv->format_name : NULL;
ea2384d3
FB
3862}
3863
ada42401
SH
3864static int qsort_strcmp(const void *a, const void *b)
3865{
3866 return strcmp(a, b);
3867}
3868
5fafdf24 3869void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
ea2384d3
FB
3870 void *opaque)
3871{
3872 BlockDriver *drv;
e855e4fb 3873 int count = 0;
ada42401 3874 int i;
e855e4fb 3875 const char **formats = NULL;
ea2384d3 3876
8a22f02a 3877 QLIST_FOREACH(drv, &bdrv_drivers, list) {
e855e4fb
JC
3878 if (drv->format_name) {
3879 bool found = false;
3880 int i = count;
3881 while (formats && i && !found) {
3882 found = !strcmp(formats[--i], drv->format_name);
3883 }
3884
3885 if (!found) {
5839e53b 3886 formats = g_renew(const char *, formats, count + 1);
e855e4fb 3887 formats[count++] = drv->format_name;
e855e4fb
JC
3888 }
3889 }
ea2384d3 3890 }
ada42401
SH
3891
3892 qsort(formats, count, sizeof(formats[0]), qsort_strcmp);
3893
3894 for (i = 0; i < count; i++) {
3895 it(opaque, formats[i]);
3896 }
3897
e855e4fb 3898 g_free(formats);
ea2384d3
FB
3899}
3900
dc364f4c
BC
3901/* This function is to find a node in the bs graph */
3902BlockDriverState *bdrv_find_node(const char *node_name)
3903{
3904 BlockDriverState *bs;
3905
3906 assert(node_name);
3907
3908 QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) {
3909 if (!strcmp(node_name, bs->node_name)) {
3910 return bs;
3911 }
3912 }
3913 return NULL;
3914}
3915
c13163fb 3916/* Put this QMP function here so it can access the static graph_bdrv_states. */
d5a8ee60 3917BlockDeviceInfoList *bdrv_named_nodes_list(Error **errp)
c13163fb
BC
3918{
3919 BlockDeviceInfoList *list, *entry;
3920 BlockDriverState *bs;
3921
3922 list = NULL;
3923 QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) {
d5a8ee60
AG
3924 BlockDeviceInfo *info = bdrv_block_device_info(bs, errp);
3925 if (!info) {
3926 qapi_free_BlockDeviceInfoList(list);
3927 return NULL;
3928 }
c13163fb 3929 entry = g_malloc0(sizeof(*entry));
d5a8ee60 3930 entry->value = info;
c13163fb
BC
3931 entry->next = list;
3932 list = entry;
3933 }
3934
3935 return list;
3936}
3937
12d3ba82
BC
3938BlockDriverState *bdrv_lookup_bs(const char *device,
3939 const char *node_name,
3940 Error **errp)
3941{
7f06d47e
MA
3942 BlockBackend *blk;
3943 BlockDriverState *bs;
12d3ba82 3944
12d3ba82 3945 if (device) {
7f06d47e 3946 blk = blk_by_name(device);
12d3ba82 3947
7f06d47e
MA
3948 if (blk) {
3949 return blk_bs(blk);
12d3ba82 3950 }
12d3ba82
BC
3951 }
3952
dd67fa50
BC
3953 if (node_name) {
3954 bs = bdrv_find_node(node_name);
12d3ba82 3955
dd67fa50
BC
3956 if (bs) {
3957 return bs;
3958 }
12d3ba82
BC
3959 }
3960
dd67fa50
BC
3961 error_setg(errp, "Cannot find device=%s nor node_name=%s",
3962 device ? device : "",
3963 node_name ? node_name : "");
3964 return NULL;
12d3ba82
BC
3965}
3966
5a6684d2
JC
3967/* If 'base' is in the same chain as 'top', return true. Otherwise,
3968 * return false. If either argument is NULL, return false. */
3969bool bdrv_chain_contains(BlockDriverState *top, BlockDriverState *base)
3970{
3971 while (top && top != base) {
3972 top = top->backing_hd;
3973 }
3974
3975 return top != NULL;
3976}
3977
04df765a
FZ
3978BlockDriverState *bdrv_next_node(BlockDriverState *bs)
3979{
3980 if (!bs) {
3981 return QTAILQ_FIRST(&graph_bdrv_states);
3982 }
3983 return QTAILQ_NEXT(bs, node_list);
3984}
3985
2f399b0a
MA
3986BlockDriverState *bdrv_next(BlockDriverState *bs)
3987{
3988 if (!bs) {
3989 return QTAILQ_FIRST(&bdrv_states);
3990 }
dc364f4c 3991 return QTAILQ_NEXT(bs, device_list);
2f399b0a
MA
3992}
3993
20a9e77d
FZ
3994const char *bdrv_get_node_name(const BlockDriverState *bs)
3995{
3996 return bs->node_name;
3997}
3998
7f06d47e 3999/* TODO check what callers really want: bs->node_name or blk_name() */
bfb197e0 4000const char *bdrv_get_device_name(const BlockDriverState *bs)
ea2384d3 4001{
bfb197e0 4002 return bs->blk ? blk_name(bs->blk) : "";
ea2384d3
FB
4003}
4004
9b2aa84f
AG
4005/* This can be used to identify nodes that might not have a device
4006 * name associated. Since node and device names live in the same
4007 * namespace, the result is unambiguous. The exception is if both are
4008 * absent, then this returns an empty (non-null) string. */
4009const char *bdrv_get_device_or_node_name(const BlockDriverState *bs)
4010{
4011 return bs->blk ? blk_name(bs->blk) : bs->node_name;
4012}
4013
c8433287
MA
4014int bdrv_get_flags(BlockDriverState *bs)
4015{
4016 return bs->open_flags;
4017}
4018
f0f0fdfe 4019int bdrv_flush_all(void)
c6ca28d6 4020{
4f5472cb 4021 BlockDriverState *bs = NULL;
f0f0fdfe 4022 int result = 0;
c6ca28d6 4023
4f5472cb 4024 while ((bs = bdrv_next(bs))) {
ed78cda3
SH
4025 AioContext *aio_context = bdrv_get_aio_context(bs);
4026 int ret;
4027
4028 aio_context_acquire(aio_context);
4029 ret = bdrv_flush(bs);
f0f0fdfe
KW
4030 if (ret < 0 && !result) {
4031 result = ret;
4032 }
ed78cda3 4033 aio_context_release(aio_context);
1b7bdbc1 4034 }
f0f0fdfe
KW
4035
4036 return result;
c6ca28d6
AL
4037}
4038
3ac21627
PL
4039int bdrv_has_zero_init_1(BlockDriverState *bs)
4040{
4041 return 1;
4042}
4043
f2feebbd
KW
4044int bdrv_has_zero_init(BlockDriverState *bs)
4045{
4046 assert(bs->drv);
4047
11212d8f
PB
4048 /* If BS is a copy on write image, it is initialized to
4049 the contents of the base image, which may not be zeroes. */
4050 if (bs->backing_hd) {
4051 return 0;
4052 }
336c1c12
KW
4053 if (bs->drv->bdrv_has_zero_init) {
4054 return bs->drv->bdrv_has_zero_init(bs);
f2feebbd
KW
4055 }
4056
3ac21627
PL
4057 /* safe default */
4058 return 0;
f2feebbd
KW
4059}
4060
4ce78691
PL
4061bool bdrv_unallocated_blocks_are_zero(BlockDriverState *bs)
4062{
4063 BlockDriverInfo bdi;
4064
4065 if (bs->backing_hd) {
4066 return false;
4067 }
4068
4069 if (bdrv_get_info(bs, &bdi) == 0) {
4070 return bdi.unallocated_blocks_are_zero;
4071 }
4072
4073 return false;
4074}
4075
4076bool bdrv_can_write_zeroes_with_unmap(BlockDriverState *bs)
4077{
4078 BlockDriverInfo bdi;
4079
4080 if (bs->backing_hd || !(bs->open_flags & BDRV_O_UNMAP)) {
4081 return false;
4082 }
4083
4084 if (bdrv_get_info(bs, &bdi) == 0) {
4085 return bdi.can_write_zeroes_with_unmap;
4086 }
4087
4088 return false;
4089}
4090
b6b8a333 4091typedef struct BdrvCoGetBlockStatusData {
376ae3f1 4092 BlockDriverState *bs;
b35b2bba 4093 BlockDriverState *base;
376ae3f1
SH
4094 int64_t sector_num;
4095 int nb_sectors;
4096 int *pnum;
b6b8a333 4097 int64_t ret;
376ae3f1 4098 bool done;
b6b8a333 4099} BdrvCoGetBlockStatusData;
376ae3f1 4100
f58c7b35 4101/*
705be728
FZ
4102 * Returns the allocation status of the specified sectors.
4103 * Drivers not implementing the functionality are assumed to not support
4104 * backing files, hence all their sectors are reported as allocated.
f58c7b35 4105 *
bd9533e3
SH
4106 * If 'sector_num' is beyond the end of the disk image the return value is 0
4107 * and 'pnum' is set to 0.
4108 *
f58c7b35
TS
4109 * 'pnum' is set to the number of sectors (including and immediately following
4110 * the specified sector) that are known to be in the same
4111 * allocated/unallocated state.
4112 *
bd9533e3
SH
4113 * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
4114 * beyond the end of the disk image it will be clamped.
f58c7b35 4115 */
b6b8a333
PB
4116static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs,
4117 int64_t sector_num,
4118 int nb_sectors, int *pnum)
f58c7b35 4119{
30a7f2fc 4120 int64_t total_sectors;
bd9533e3 4121 int64_t n;
5daa74a6 4122 int64_t ret, ret2;
bd9533e3 4123
30a7f2fc
MA
4124 total_sectors = bdrv_nb_sectors(bs);
4125 if (total_sectors < 0) {
4126 return total_sectors;
617ccb46
PB
4127 }
4128
30a7f2fc 4129 if (sector_num >= total_sectors) {
bd9533e3
SH
4130 *pnum = 0;
4131 return 0;
4132 }
4133
30a7f2fc 4134 n = total_sectors - sector_num;
bd9533e3
SH
4135 if (n < nb_sectors) {
4136 nb_sectors = n;
4137 }
4138
b6b8a333 4139 if (!bs->drv->bdrv_co_get_block_status) {
bd9533e3 4140 *pnum = nb_sectors;
e88ae226 4141 ret = BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED;
918e92d7
PB
4142 if (bs->drv->protocol_name) {
4143 ret |= BDRV_BLOCK_OFFSET_VALID | (sector_num * BDRV_SECTOR_SIZE);
4144 }
4145 return ret;
f58c7b35 4146 }
6aebab14 4147
415b5b01
PB
4148 ret = bs->drv->bdrv_co_get_block_status(bs, sector_num, nb_sectors, pnum);
4149 if (ret < 0) {
3e0a233d 4150 *pnum = 0;
415b5b01
PB
4151 return ret;
4152 }
4153
92bc50a5
PL
4154 if (ret & BDRV_BLOCK_RAW) {
4155 assert(ret & BDRV_BLOCK_OFFSET_VALID);
4156 return bdrv_get_block_status(bs->file, ret >> BDRV_SECTOR_BITS,
4157 *pnum, pnum);
4158 }
4159
e88ae226
KW
4160 if (ret & (BDRV_BLOCK_DATA | BDRV_BLOCK_ZERO)) {
4161 ret |= BDRV_BLOCK_ALLOCATED;
4162 }
4163
c3d86884
PL
4164 if (!(ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO)) {
4165 if (bdrv_unallocated_blocks_are_zero(bs)) {
f0ad5712 4166 ret |= BDRV_BLOCK_ZERO;
1f9db224 4167 } else if (bs->backing_hd) {
f0ad5712 4168 BlockDriverState *bs2 = bs->backing_hd;
30a7f2fc
MA
4169 int64_t nb_sectors2 = bdrv_nb_sectors(bs2);
4170 if (nb_sectors2 >= 0 && sector_num >= nb_sectors2) {
f0ad5712
PB
4171 ret |= BDRV_BLOCK_ZERO;
4172 }
4173 }
415b5b01 4174 }
5daa74a6
PB
4175
4176 if (bs->file &&
4177 (ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO) &&
4178 (ret & BDRV_BLOCK_OFFSET_VALID)) {
59c9a95f
HR
4179 int file_pnum;
4180
5daa74a6 4181 ret2 = bdrv_co_get_block_status(bs->file, ret >> BDRV_SECTOR_BITS,
59c9a95f 4182 *pnum, &file_pnum);
5daa74a6
PB
4183 if (ret2 >= 0) {
4184 /* Ignore errors. This is just providing extra information, it
4185 * is useful but not necessary.
4186 */
59c9a95f
HR
4187 if (!file_pnum) {
4188 /* !file_pnum indicates an offset at or beyond the EOF; it is
4189 * perfectly valid for the format block driver to point to such
4190 * offsets, so catch it and mark everything as zero */
4191 ret |= BDRV_BLOCK_ZERO;
4192 } else {
4193 /* Limit request to the range reported by the protocol driver */
4194 *pnum = file_pnum;
4195 ret |= (ret2 & BDRV_BLOCK_ZERO);
4196 }
5daa74a6
PB
4197 }
4198 }
4199
415b5b01 4200 return ret;
060f51c9
SH
4201}
4202
b6b8a333
PB
4203/* Coroutine wrapper for bdrv_get_block_status() */
4204static void coroutine_fn bdrv_get_block_status_co_entry(void *opaque)
060f51c9 4205{
b6b8a333 4206 BdrvCoGetBlockStatusData *data = opaque;
060f51c9
SH
4207 BlockDriverState *bs = data->bs;
4208
b6b8a333
PB
4209 data->ret = bdrv_co_get_block_status(bs, data->sector_num, data->nb_sectors,
4210 data->pnum);
060f51c9
SH
4211 data->done = true;
4212}
4213
4214/*
b6b8a333 4215 * Synchronous wrapper around bdrv_co_get_block_status().
060f51c9 4216 *
b6b8a333 4217 * See bdrv_co_get_block_status() for details.
060f51c9 4218 */
b6b8a333
PB
4219int64_t bdrv_get_block_status(BlockDriverState *bs, int64_t sector_num,
4220 int nb_sectors, int *pnum)
060f51c9 4221{
6aebab14 4222 Coroutine *co;
b6b8a333 4223 BdrvCoGetBlockStatusData data = {
6aebab14
SH
4224 .bs = bs,
4225 .sector_num = sector_num,
4226 .nb_sectors = nb_sectors,
4227 .pnum = pnum,
4228 .done = false,
4229 };
4230
bdad13b9
PB
4231 if (qemu_in_coroutine()) {
4232 /* Fast-path if already in coroutine context */
b6b8a333 4233 bdrv_get_block_status_co_entry(&data);
bdad13b9 4234 } else {
2572b37a
SH
4235 AioContext *aio_context = bdrv_get_aio_context(bs);
4236
b6b8a333 4237 co = qemu_coroutine_create(bdrv_get_block_status_co_entry);
bdad13b9
PB
4238 qemu_coroutine_enter(co, &data);
4239 while (!data.done) {
2572b37a 4240 aio_poll(aio_context, true);
bdad13b9 4241 }
6aebab14
SH
4242 }
4243 return data.ret;
f58c7b35
TS
4244}
4245
b6b8a333
PB
4246int coroutine_fn bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num,
4247 int nb_sectors, int *pnum)
4248{
4333bb71
PB
4249 int64_t ret = bdrv_get_block_status(bs, sector_num, nb_sectors, pnum);
4250 if (ret < 0) {
4251 return ret;
4252 }
01fb2705 4253 return !!(ret & BDRV_BLOCK_ALLOCATED);
b6b8a333
PB
4254}
4255
188a7bbf
PB
4256/*
4257 * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP]
4258 *
4259 * Return true if the given sector is allocated in any image between
4260 * BASE and TOP (inclusive). BASE can be NULL to check if the given
4261 * sector is allocated in any image of the chain. Return false otherwise.
4262 *
4263 * 'pnum' is set to the number of sectors (including and immediately following
4264 * the specified sector) that are known to be in the same
4265 * allocated/unallocated state.
4266 *
4267 */
4f578637
PB
4268int bdrv_is_allocated_above(BlockDriverState *top,
4269 BlockDriverState *base,
4270 int64_t sector_num,
4271 int nb_sectors, int *pnum)
188a7bbf
PB
4272{
4273 BlockDriverState *intermediate;
4274 int ret, n = nb_sectors;
4275
4276 intermediate = top;
4277 while (intermediate && intermediate != base) {
4278 int pnum_inter;
bdad13b9
PB
4279 ret = bdrv_is_allocated(intermediate, sector_num, nb_sectors,
4280 &pnum_inter);
188a7bbf
PB
4281 if (ret < 0) {
4282 return ret;
4283 } else if (ret) {
4284 *pnum = pnum_inter;
4285 return 1;
4286 }
4287
4288 /*
4289 * [sector_num, nb_sectors] is unallocated on top but intermediate
4290 * might have
4291 *
4292 * [sector_num+x, nr_sectors] allocated.
4293 */
63ba17d3
VI
4294 if (n > pnum_inter &&
4295 (intermediate == top ||
4296 sector_num + pnum_inter < intermediate->total_sectors)) {
188a7bbf
PB
4297 n = pnum_inter;
4298 }
4299
4300 intermediate = intermediate->backing_hd;
4301 }
4302
4303 *pnum = n;
4304 return 0;
4305}
4306
045df330
AL
4307const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
4308{
4309 if (bs->backing_hd && bs->backing_hd->encrypted)
4310 return bs->backing_file;
4311 else if (bs->encrypted)
4312 return bs->filename;
4313 else
4314 return NULL;
4315}
4316
5fafdf24 4317void bdrv_get_backing_filename(BlockDriverState *bs,
83f64091
FB
4318 char *filename, int filename_size)
4319{
3574c608 4320 pstrcpy(filename, filename_size, bs->backing_file);
83f64091
FB
4321}
4322
5fafdf24 4323int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
faea38e7
FB
4324 const uint8_t *buf, int nb_sectors)
4325{
4326 BlockDriver *drv = bs->drv;
b9c64947
HR
4327 int ret;
4328
4329 if (!drv) {
19cb3738 4330 return -ENOMEDIUM;
b9c64947
HR
4331 }
4332 if (!drv->bdrv_write_compressed) {
faea38e7 4333 return -ENOTSUP;
b9c64947
HR
4334 }
4335 ret = bdrv_check_request(bs, sector_num, nb_sectors);
4336 if (ret < 0) {
4337 return ret;
4338 }
a55eb92c 4339
e4654d2d 4340 assert(QLIST_EMPTY(&bs->dirty_bitmaps));
a55eb92c 4341
faea38e7
FB
4342 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
4343}
3b46e624 4344
faea38e7
FB
4345int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
4346{
4347 BlockDriver *drv = bs->drv;
4348 if (!drv)
19cb3738 4349 return -ENOMEDIUM;
faea38e7
FB
4350 if (!drv->bdrv_get_info)
4351 return -ENOTSUP;
4352 memset(bdi, 0, sizeof(*bdi));
4353 return drv->bdrv_get_info(bs, bdi);
4354}
4355
eae041fe
HR
4356ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs)
4357{
4358 BlockDriver *drv = bs->drv;
4359 if (drv && drv->bdrv_get_specific_info) {
4360 return drv->bdrv_get_specific_info(bs);
4361 }
4362 return NULL;
4363}
4364
45566e9c
CH
4365int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
4366 int64_t pos, int size)
cf8074b3
KW
4367{
4368 QEMUIOVector qiov;
4369 struct iovec iov = {
4370 .iov_base = (void *) buf,
4371 .iov_len = size,
4372 };
4373
4374 qemu_iovec_init_external(&qiov, &iov, 1);
4375 return bdrv_writev_vmstate(bs, &qiov, pos);
4376}
4377
4378int bdrv_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos)
178e08a5
AL
4379{
4380 BlockDriver *drv = bs->drv;
cf8074b3
KW
4381
4382 if (!drv) {
178e08a5 4383 return -ENOMEDIUM;
cf8074b3
KW
4384 } else if (drv->bdrv_save_vmstate) {
4385 return drv->bdrv_save_vmstate(bs, qiov, pos);
4386 } else if (bs->file) {
4387 return bdrv_writev_vmstate(bs->file, qiov, pos);
4388 }
4389
7cdb1f6d 4390 return -ENOTSUP;
178e08a5
AL
4391}
4392
45566e9c
CH
4393int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
4394 int64_t pos, int size)
178e08a5
AL
4395{
4396 BlockDriver *drv = bs->drv;
4397 if (!drv)
4398 return -ENOMEDIUM;
7cdb1f6d
MK
4399 if (drv->bdrv_load_vmstate)
4400 return drv->bdrv_load_vmstate(bs, buf, pos, size);
4401 if (bs->file)
4402 return bdrv_load_vmstate(bs->file, buf, pos, size);
4403 return -ENOTSUP;
178e08a5
AL
4404}
4405
8b9b0cc2
KW
4406void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
4407{
bf736fe3 4408 if (!bs || !bs->drv || !bs->drv->bdrv_debug_event) {
8b9b0cc2
KW
4409 return;
4410 }
4411
bf736fe3 4412 bs->drv->bdrv_debug_event(bs, event);
41c695c7
KW
4413}
4414
4415int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event,
4416 const char *tag)
4417{
4418 while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) {
4419 bs = bs->file;
4420 }
4421
4422 if (bs && bs->drv && bs->drv->bdrv_debug_breakpoint) {
4423 return bs->drv->bdrv_debug_breakpoint(bs, event, tag);
4424 }
4425
4426 return -ENOTSUP;
4427}
4428
4cc70e93
FZ
4429int bdrv_debug_remove_breakpoint(BlockDriverState *bs, const char *tag)
4430{
4431 while (bs && bs->drv && !bs->drv->bdrv_debug_remove_breakpoint) {
4432 bs = bs->file;
4433 }
4434
4435 if (bs && bs->drv && bs->drv->bdrv_debug_remove_breakpoint) {
4436 return bs->drv->bdrv_debug_remove_breakpoint(bs, tag);
4437 }
4438
4439 return -ENOTSUP;
4440}
4441
41c695c7
KW
4442int bdrv_debug_resume(BlockDriverState *bs, const char *tag)
4443{
938789ea 4444 while (bs && (!bs->drv || !bs->drv->bdrv_debug_resume)) {
41c695c7
KW
4445 bs = bs->file;
4446 }
8b9b0cc2 4447
41c695c7
KW
4448 if (bs && bs->drv && bs->drv->bdrv_debug_resume) {
4449 return bs->drv->bdrv_debug_resume(bs, tag);
4450 }
4451
4452 return -ENOTSUP;
4453}
4454
4455bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag)
4456{
4457 while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) {
4458 bs = bs->file;
4459 }
4460
4461 if (bs && bs->drv && bs->drv->bdrv_debug_is_suspended) {
4462 return bs->drv->bdrv_debug_is_suspended(bs, tag);
4463 }
4464
4465 return false;
8b9b0cc2
KW
4466}
4467
199630b6
BS
4468int bdrv_is_snapshot(BlockDriverState *bs)
4469{
4470 return !!(bs->open_flags & BDRV_O_SNAPSHOT);
4471}
4472
b1b1d783
JC
4473/* backing_file can either be relative, or absolute, or a protocol. If it is
4474 * relative, it must be relative to the chain. So, passing in bs->filename
4475 * from a BDS as backing_file should not be done, as that may be relative to
4476 * the CWD rather than the chain. */
e8a6bb9c
MT
4477BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs,
4478 const char *backing_file)
4479{
b1b1d783
JC
4480 char *filename_full = NULL;
4481 char *backing_file_full = NULL;
4482 char *filename_tmp = NULL;
4483 int is_protocol = 0;
4484 BlockDriverState *curr_bs = NULL;
4485 BlockDriverState *retval = NULL;
4486
4487 if (!bs || !bs->drv || !backing_file) {
e8a6bb9c
MT
4488 return NULL;
4489 }
4490
b1b1d783
JC
4491 filename_full = g_malloc(PATH_MAX);
4492 backing_file_full = g_malloc(PATH_MAX);
4493 filename_tmp = g_malloc(PATH_MAX);
4494
4495 is_protocol = path_has_protocol(backing_file);
4496
4497 for (curr_bs = bs; curr_bs->backing_hd; curr_bs = curr_bs->backing_hd) {
4498
4499 /* If either of the filename paths is actually a protocol, then
4500 * compare unmodified paths; otherwise make paths relative */
4501 if (is_protocol || path_has_protocol(curr_bs->backing_file)) {
4502 if (strcmp(backing_file, curr_bs->backing_file) == 0) {
4503 retval = curr_bs->backing_hd;
4504 break;
4505 }
e8a6bb9c 4506 } else {
b1b1d783
JC
4507 /* If not an absolute filename path, make it relative to the current
4508 * image's filename path */
4509 path_combine(filename_tmp, PATH_MAX, curr_bs->filename,
4510 backing_file);
4511
4512 /* We are going to compare absolute pathnames */
4513 if (!realpath(filename_tmp, filename_full)) {
4514 continue;
4515 }
4516
4517 /* We need to make sure the backing filename we are comparing against
4518 * is relative to the current image filename (or absolute) */
4519 path_combine(filename_tmp, PATH_MAX, curr_bs->filename,
4520 curr_bs->backing_file);
4521
4522 if (!realpath(filename_tmp, backing_file_full)) {
4523 continue;
4524 }
4525
4526 if (strcmp(backing_file_full, filename_full) == 0) {
4527 retval = curr_bs->backing_hd;
4528 break;
4529 }
e8a6bb9c
MT
4530 }
4531 }
4532
b1b1d783
JC
4533 g_free(filename_full);
4534 g_free(backing_file_full);
4535 g_free(filename_tmp);
4536 return retval;
e8a6bb9c
MT
4537}
4538
f198fd1c
BC
4539int bdrv_get_backing_file_depth(BlockDriverState *bs)
4540{
4541 if (!bs->drv) {
4542 return 0;
4543 }
4544
4545 if (!bs->backing_hd) {
4546 return 0;
4547 }
4548
4549 return 1 + bdrv_get_backing_file_depth(bs->backing_hd);
4550}
4551
ea2384d3 4552/**************************************************************/
83f64091 4553/* async I/Os */
ea2384d3 4554
7c84b1b8
MA
4555BlockAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
4556 QEMUIOVector *qiov, int nb_sectors,
097310b5 4557 BlockCompletionFunc *cb, void *opaque)
83f64091 4558{
bbf0a440
SH
4559 trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque);
4560
d20d9b7c 4561 return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 0,
8c5873d6 4562 cb, opaque, false);
ea2384d3
FB
4563}
4564
7c84b1b8
MA
4565BlockAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
4566 QEMUIOVector *qiov, int nb_sectors,
097310b5 4567 BlockCompletionFunc *cb, void *opaque)
ea2384d3 4568{
bbf0a440
SH
4569 trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque);
4570
d20d9b7c 4571 return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 0,
8c5873d6 4572 cb, opaque, true);
83f64091
FB
4573}
4574
7c84b1b8 4575BlockAIOCB *bdrv_aio_write_zeroes(BlockDriverState *bs,
d5ef94d4 4576 int64_t sector_num, int nb_sectors, BdrvRequestFlags flags,
097310b5 4577 BlockCompletionFunc *cb, void *opaque)
d5ef94d4
PB
4578{
4579 trace_bdrv_aio_write_zeroes(bs, sector_num, nb_sectors, flags, opaque);
4580
4581 return bdrv_co_aio_rw_vector(bs, sector_num, NULL, nb_sectors,
4582 BDRV_REQ_ZERO_WRITE | flags,
4583 cb, opaque, true);
4584}
4585
40b4f539
KW
4586
4587typedef struct MultiwriteCB {
4588 int error;
4589 int num_requests;
4590 int num_callbacks;
4591 struct {
097310b5 4592 BlockCompletionFunc *cb;
40b4f539
KW
4593 void *opaque;
4594 QEMUIOVector *free_qiov;
40b4f539
KW
4595 } callbacks[];
4596} MultiwriteCB;
4597
4598static void multiwrite_user_cb(MultiwriteCB *mcb)
4599{
4600 int i;
4601
4602 for (i = 0; i < mcb->num_callbacks; i++) {
4603 mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
1e1ea48d
SH
4604 if (mcb->callbacks[i].free_qiov) {
4605 qemu_iovec_destroy(mcb->callbacks[i].free_qiov);
4606 }
7267c094 4607 g_free(mcb->callbacks[i].free_qiov);
40b4f539
KW
4608 }
4609}
4610
4611static void multiwrite_cb(void *opaque, int ret)
4612{
4613 MultiwriteCB *mcb = opaque;
4614
6d519a5f
SH
4615 trace_multiwrite_cb(mcb, ret);
4616
cb6d3ca0 4617 if (ret < 0 && !mcb->error) {
40b4f539 4618 mcb->error = ret;
40b4f539
KW
4619 }
4620
4621 mcb->num_requests--;
4622 if (mcb->num_requests == 0) {
de189a1b 4623 multiwrite_user_cb(mcb);
7267c094 4624 g_free(mcb);
40b4f539
KW
4625 }
4626}
4627
4628static int multiwrite_req_compare(const void *a, const void *b)
4629{
77be4366
CH
4630 const BlockRequest *req1 = a, *req2 = b;
4631
4632 /*
4633 * Note that we can't simply subtract req2->sector from req1->sector
4634 * here as that could overflow the return value.
4635 */
4636 if (req1->sector > req2->sector) {
4637 return 1;
4638 } else if (req1->sector < req2->sector) {
4639 return -1;
4640 } else {
4641 return 0;
4642 }
40b4f539
KW
4643}
4644
4645/*
4646 * Takes a bunch of requests and tries to merge them. Returns the number of
4647 * requests that remain after merging.
4648 */
4649static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
4650 int num_reqs, MultiwriteCB *mcb)
4651{
4652 int i, outidx;
4653
4654 // Sort requests by start sector
4655 qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
4656
4657 // Check if adjacent requests touch the same clusters. If so, combine them,
4658 // filling up gaps with zero sectors.
4659 outidx = 0;
4660 for (i = 1; i < num_reqs; i++) {
4661 int merge = 0;
4662 int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
4663
b6a127a1 4664 // Handle exactly sequential writes and overlapping writes.
40b4f539
KW
4665 if (reqs[i].sector <= oldreq_last) {
4666 merge = 1;
4667 }
4668
e2a305fb
CH
4669 if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) {
4670 merge = 0;
4671 }
4672
6c5a42ac
PL
4673 if (bs->bl.max_transfer_length && reqs[outidx].nb_sectors +
4674 reqs[i].nb_sectors > bs->bl.max_transfer_length) {
4675 merge = 0;
4676 }
4677
40b4f539
KW
4678 if (merge) {
4679 size_t size;
7267c094 4680 QEMUIOVector *qiov = g_malloc0(sizeof(*qiov));
40b4f539
KW
4681 qemu_iovec_init(qiov,
4682 reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
4683
4684 // Add the first request to the merged one. If the requests are
4685 // overlapping, drop the last sectors of the first request.
4686 size = (reqs[i].sector - reqs[outidx].sector) << 9;
1b093c48 4687 qemu_iovec_concat(qiov, reqs[outidx].qiov, 0, size);
40b4f539 4688
b6a127a1
PB
4689 // We should need to add any zeros between the two requests
4690 assert (reqs[i].sector <= oldreq_last);
40b4f539
KW
4691
4692 // Add the second request
1b093c48 4693 qemu_iovec_concat(qiov, reqs[i].qiov, 0, reqs[i].qiov->size);
40b4f539 4694
391827eb
SH
4695 // Add tail of first request, if necessary
4696 if (qiov->size < reqs[outidx].qiov->size) {
4697 qemu_iovec_concat(qiov, reqs[outidx].qiov, qiov->size,
4698 reqs[outidx].qiov->size - qiov->size);
4699 }
4700
cbf1dff2 4701 reqs[outidx].nb_sectors = qiov->size >> 9;
40b4f539
KW
4702 reqs[outidx].qiov = qiov;
4703
4704 mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
4705 } else {
4706 outidx++;
4707 reqs[outidx].sector = reqs[i].sector;
4708 reqs[outidx].nb_sectors = reqs[i].nb_sectors;
4709 reqs[outidx].qiov = reqs[i].qiov;
4710 }
4711 }
4712
f4564d53
PL
4713 block_acct_merge_done(&bs->stats, BLOCK_ACCT_WRITE, num_reqs - outidx - 1);
4714
40b4f539
KW
4715 return outidx + 1;
4716}
4717
4718/*
4719 * Submit multiple AIO write requests at once.
4720 *
4721 * On success, the function returns 0 and all requests in the reqs array have
4722 * been submitted. In error case this function returns -1, and any of the
4723 * requests may or may not be submitted yet. In particular, this means that the
4724 * callback will be called for some of the requests, for others it won't. The
4725 * caller must check the error field of the BlockRequest to wait for the right
4726 * callbacks (if error != 0, no callback will be called).
4727 *
4728 * The implementation may modify the contents of the reqs array, e.g. to merge
4729 * requests. However, the fields opaque and error are left unmodified as they
4730 * are used to signal failure for a single request to the caller.
4731 */
4732int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
4733{
40b4f539
KW
4734 MultiwriteCB *mcb;
4735 int i;
4736
301db7c2
RH
4737 /* don't submit writes if we don't have a medium */
4738 if (bs->drv == NULL) {
4739 for (i = 0; i < num_reqs; i++) {
4740 reqs[i].error = -ENOMEDIUM;
4741 }
4742 return -1;
4743 }
4744
40b4f539
KW
4745 if (num_reqs == 0) {
4746 return 0;
4747 }
4748
4749 // Create MultiwriteCB structure
7267c094 4750 mcb = g_malloc0(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
40b4f539
KW
4751 mcb->num_requests = 0;
4752 mcb->num_callbacks = num_reqs;
4753
4754 for (i = 0; i < num_reqs; i++) {
4755 mcb->callbacks[i].cb = reqs[i].cb;
4756 mcb->callbacks[i].opaque = reqs[i].opaque;
4757 }
4758
4759 // Check for mergable requests
4760 num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
4761
6d519a5f
SH
4762 trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs);
4763
df9309fb
PB
4764 /* Run the aio requests. */
4765 mcb->num_requests = num_reqs;
40b4f539 4766 for (i = 0; i < num_reqs; i++) {
d20d9b7c
PB
4767 bdrv_co_aio_rw_vector(bs, reqs[i].sector, reqs[i].qiov,
4768 reqs[i].nb_sectors, reqs[i].flags,
4769 multiwrite_cb, mcb,
4770 true);
40b4f539
KW
4771 }
4772
4773 return 0;
40b4f539
KW
4774}
4775
7c84b1b8 4776void bdrv_aio_cancel(BlockAIOCB *acb)
83f64091 4777{
ca5fd113
FZ
4778 qemu_aio_ref(acb);
4779 bdrv_aio_cancel_async(acb);
4780 while (acb->refcnt > 1) {
4781 if (acb->aiocb_info->get_aio_context) {
4782 aio_poll(acb->aiocb_info->get_aio_context(acb), true);
4783 } else if (acb->bs) {
4784 aio_poll(bdrv_get_aio_context(acb->bs), true);
4785 } else {
4786 abort();
02c50efe 4787 }
02c50efe 4788 }
8007429a 4789 qemu_aio_unref(acb);
02c50efe
FZ
4790}
4791
4792/* Async version of aio cancel. The caller is not blocked if the acb implements
4793 * cancel_async, otherwise we do nothing and let the request normally complete.
4794 * In either case the completion callback must be called. */
7c84b1b8 4795void bdrv_aio_cancel_async(BlockAIOCB *acb)
02c50efe
FZ
4796{
4797 if (acb->aiocb_info->cancel_async) {
4798 acb->aiocb_info->cancel_async(acb);
4799 }
83f64091
FB
4800}
4801
4802/**************************************************************/
4803/* async block device emulation */
4804
7c84b1b8
MA
4805typedef struct BlockAIOCBSync {
4806 BlockAIOCB common;
c16b5a2c
CH
4807 QEMUBH *bh;
4808 int ret;
4809 /* vector translation state */
4810 QEMUIOVector *qiov;
4811 uint8_t *bounce;
4812 int is_write;
7c84b1b8 4813} BlockAIOCBSync;
c16b5a2c 4814
d7331bed 4815static const AIOCBInfo bdrv_em_aiocb_info = {
7c84b1b8 4816 .aiocb_size = sizeof(BlockAIOCBSync),
c16b5a2c
CH
4817};
4818
ce1a14dc 4819static void bdrv_aio_bh_cb(void *opaque)
83f64091 4820{
7c84b1b8 4821 BlockAIOCBSync *acb = opaque;
f141eafe 4822
857d4f46 4823 if (!acb->is_write && acb->ret >= 0) {
03396148 4824 qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size);
857d4f46 4825 }
ceb42de8 4826 qemu_vfree(acb->bounce);
ce1a14dc 4827 acb->common.cb(acb->common.opaque, acb->ret);
6a7ad299 4828 qemu_bh_delete(acb->bh);
36afc451 4829 acb->bh = NULL;
8007429a 4830 qemu_aio_unref(acb);
83f64091 4831}
beac80cd 4832
7c84b1b8
MA
4833static BlockAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
4834 int64_t sector_num,
4835 QEMUIOVector *qiov,
4836 int nb_sectors,
097310b5 4837 BlockCompletionFunc *cb,
7c84b1b8
MA
4838 void *opaque,
4839 int is_write)
f141eafe 4840
83f64091 4841{
7c84b1b8 4842 BlockAIOCBSync *acb;
ce1a14dc 4843
d7331bed 4844 acb = qemu_aio_get(&bdrv_em_aiocb_info, bs, cb, opaque);
f141eafe
AL
4845 acb->is_write = is_write;
4846 acb->qiov = qiov;
857d4f46 4847 acb->bounce = qemu_try_blockalign(bs, qiov->size);
2572b37a 4848 acb->bh = aio_bh_new(bdrv_get_aio_context(bs), bdrv_aio_bh_cb, acb);
f141eafe 4849
857d4f46
KW
4850 if (acb->bounce == NULL) {
4851 acb->ret = -ENOMEM;
4852 } else if (is_write) {
d5e6b161 4853 qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size);
1ed20acf 4854 acb->ret = bs->drv->bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
f141eafe 4855 } else {
1ed20acf 4856 acb->ret = bs->drv->bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
f141eafe
AL
4857 }
4858
ce1a14dc 4859 qemu_bh_schedule(acb->bh);
f141eafe 4860
ce1a14dc 4861 return &acb->common;
beac80cd
FB
4862}
4863
7c84b1b8 4864static BlockAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
f141eafe 4865 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
097310b5 4866 BlockCompletionFunc *cb, void *opaque)
beac80cd 4867{
f141eafe
AL
4868 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
4869}
83f64091 4870
7c84b1b8 4871static BlockAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
f141eafe 4872 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
097310b5 4873 BlockCompletionFunc *cb, void *opaque)
f141eafe
AL
4874{
4875 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
beac80cd 4876}
beac80cd 4877
68485420 4878
7c84b1b8
MA
4879typedef struct BlockAIOCBCoroutine {
4880 BlockAIOCB common;
68485420
KW
4881 BlockRequest req;
4882 bool is_write;
0b5a2445 4883 bool need_bh;
d318aea9 4884 bool *done;
68485420 4885 QEMUBH* bh;
7c84b1b8 4886} BlockAIOCBCoroutine;
68485420 4887
d7331bed 4888static const AIOCBInfo bdrv_em_co_aiocb_info = {
7c84b1b8 4889 .aiocb_size = sizeof(BlockAIOCBCoroutine),
68485420
KW
4890};
4891
0b5a2445
PB
4892static void bdrv_co_complete(BlockAIOCBCoroutine *acb)
4893{
4894 if (!acb->need_bh) {
4895 acb->common.cb(acb->common.opaque, acb->req.error);
4896 qemu_aio_unref(acb);
4897 }
4898}
4899
35246a68 4900static void bdrv_co_em_bh(void *opaque)
68485420 4901{
7c84b1b8 4902 BlockAIOCBCoroutine *acb = opaque;
68485420 4903
0b5a2445 4904 assert(!acb->need_bh);
68485420 4905 qemu_bh_delete(acb->bh);
0b5a2445
PB
4906 bdrv_co_complete(acb);
4907}
4908
4909static void bdrv_co_maybe_schedule_bh(BlockAIOCBCoroutine *acb)
4910{
4911 acb->need_bh = false;
4912 if (acb->req.error != -EINPROGRESS) {
4913 BlockDriverState *bs = acb->common.bs;
4914
4915 acb->bh = aio_bh_new(bdrv_get_aio_context(bs), bdrv_co_em_bh, acb);
4916 qemu_bh_schedule(acb->bh);
4917 }
68485420
KW
4918}
4919
b2a61371
SH
4920/* Invoke bdrv_co_do_readv/bdrv_co_do_writev */
4921static void coroutine_fn bdrv_co_do_rw(void *opaque)
4922{
7c84b1b8 4923 BlockAIOCBCoroutine *acb = opaque;
b2a61371
SH
4924 BlockDriverState *bs = acb->common.bs;
4925
4926 if (!acb->is_write) {
4927 acb->req.error = bdrv_co_do_readv(bs, acb->req.sector,
d20d9b7c 4928 acb->req.nb_sectors, acb->req.qiov, acb->req.flags);
b2a61371
SH
4929 } else {
4930 acb->req.error = bdrv_co_do_writev(bs, acb->req.sector,
d20d9b7c 4931 acb->req.nb_sectors, acb->req.qiov, acb->req.flags);
b2a61371
SH
4932 }
4933
0b5a2445 4934 bdrv_co_complete(acb);
b2a61371
SH
4935}
4936
7c84b1b8
MA
4937static BlockAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs,
4938 int64_t sector_num,
4939 QEMUIOVector *qiov,
4940 int nb_sectors,
4941 BdrvRequestFlags flags,
097310b5 4942 BlockCompletionFunc *cb,
7c84b1b8
MA
4943 void *opaque,
4944 bool is_write)
68485420
KW
4945{
4946 Coroutine *co;
7c84b1b8 4947 BlockAIOCBCoroutine *acb;
68485420 4948
d7331bed 4949 acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque);
0b5a2445
PB
4950 acb->need_bh = true;
4951 acb->req.error = -EINPROGRESS;
68485420
KW
4952 acb->req.sector = sector_num;
4953 acb->req.nb_sectors = nb_sectors;
4954 acb->req.qiov = qiov;
d20d9b7c 4955 acb->req.flags = flags;
68485420
KW
4956 acb->is_write = is_write;
4957
8c5873d6 4958 co = qemu_coroutine_create(bdrv_co_do_rw);
68485420
KW
4959 qemu_coroutine_enter(co, acb);
4960
0b5a2445 4961 bdrv_co_maybe_schedule_bh(acb);
68485420
KW
4962 return &acb->common;
4963}
4964
07f07615 4965static void coroutine_fn bdrv_aio_flush_co_entry(void *opaque)
b2e12bc6 4966{
7c84b1b8 4967 BlockAIOCBCoroutine *acb = opaque;
07f07615 4968 BlockDriverState *bs = acb->common.bs;
b2e12bc6 4969
07f07615 4970 acb->req.error = bdrv_co_flush(bs);
0b5a2445 4971 bdrv_co_complete(acb);
b2e12bc6
CH
4972}
4973
7c84b1b8 4974BlockAIOCB *bdrv_aio_flush(BlockDriverState *bs,
097310b5 4975 BlockCompletionFunc *cb, void *opaque)
016f5cf6 4976{
07f07615 4977 trace_bdrv_aio_flush(bs, opaque);
016f5cf6 4978
07f07615 4979 Coroutine *co;
7c84b1b8 4980 BlockAIOCBCoroutine *acb;
016f5cf6 4981
d7331bed 4982 acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque);
0b5a2445
PB
4983 acb->need_bh = true;
4984 acb->req.error = -EINPROGRESS;
d318aea9 4985
07f07615
PB
4986 co = qemu_coroutine_create(bdrv_aio_flush_co_entry);
4987 qemu_coroutine_enter(co, acb);
016f5cf6 4988
0b5a2445 4989 bdrv_co_maybe_schedule_bh(acb);
016f5cf6
AG
4990 return &acb->common;
4991}
4992
4265d620
PB
4993static void coroutine_fn bdrv_aio_discard_co_entry(void *opaque)
4994{
7c84b1b8 4995 BlockAIOCBCoroutine *acb = opaque;
4265d620
PB
4996 BlockDriverState *bs = acb->common.bs;
4997
4998 acb->req.error = bdrv_co_discard(bs, acb->req.sector, acb->req.nb_sectors);
0b5a2445 4999 bdrv_co_complete(acb);
4265d620
PB
5000}
5001
7c84b1b8 5002BlockAIOCB *bdrv_aio_discard(BlockDriverState *bs,
4265d620 5003 int64_t sector_num, int nb_sectors,
097310b5 5004 BlockCompletionFunc *cb, void *opaque)
4265d620
PB
5005{
5006 Coroutine *co;
7c84b1b8 5007 BlockAIOCBCoroutine *acb;
4265d620
PB
5008
5009 trace_bdrv_aio_discard(bs, sector_num, nb_sectors, opaque);
5010
d7331bed 5011 acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque);
0b5a2445
PB
5012 acb->need_bh = true;
5013 acb->req.error = -EINPROGRESS;
4265d620
PB
5014 acb->req.sector = sector_num;
5015 acb->req.nb_sectors = nb_sectors;
5016 co = qemu_coroutine_create(bdrv_aio_discard_co_entry);
5017 qemu_coroutine_enter(co, acb);
5018
0b5a2445 5019 bdrv_co_maybe_schedule_bh(acb);
4265d620
PB
5020 return &acb->common;
5021}
5022
ea2384d3
FB
5023void bdrv_init(void)
5024{
5efa9d5a 5025 module_call_init(MODULE_INIT_BLOCK);
ea2384d3 5026}
ce1a14dc 5027
eb852011
MA
5028void bdrv_init_with_whitelist(void)
5029{
5030 use_bdrv_whitelist = 1;
5031 bdrv_init();
5032}
5033
d7331bed 5034void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs,
097310b5 5035 BlockCompletionFunc *cb, void *opaque)
ce1a14dc 5036{
7c84b1b8 5037 BlockAIOCB *acb;
ce1a14dc 5038
d7331bed
SH
5039 acb = g_slice_alloc(aiocb_info->aiocb_size);
5040 acb->aiocb_info = aiocb_info;
ce1a14dc
PB
5041 acb->bs = bs;
5042 acb->cb = cb;
5043 acb->opaque = opaque;
f197fe2b 5044 acb->refcnt = 1;
ce1a14dc
PB
5045 return acb;
5046}
5047
f197fe2b
FZ
5048void qemu_aio_ref(void *p)
5049{
7c84b1b8 5050 BlockAIOCB *acb = p;
f197fe2b
FZ
5051 acb->refcnt++;
5052}
5053
8007429a 5054void qemu_aio_unref(void *p)
ce1a14dc 5055{
7c84b1b8 5056 BlockAIOCB *acb = p;
f197fe2b
FZ
5057 assert(acb->refcnt > 0);
5058 if (--acb->refcnt == 0) {
5059 g_slice_free1(acb->aiocb_info->aiocb_size, acb);
5060 }
ce1a14dc 5061}
19cb3738 5062
f9f05dc5
KW
5063/**************************************************************/
5064/* Coroutine block device emulation */
5065
5066typedef struct CoroutineIOCompletion {
5067 Coroutine *coroutine;
5068 int ret;
5069} CoroutineIOCompletion;
5070
5071static void bdrv_co_io_em_complete(void *opaque, int ret)
5072{
5073 CoroutineIOCompletion *co = opaque;
5074
5075 co->ret = ret;
5076 qemu_coroutine_enter(co->coroutine, NULL);
5077}
5078
5079static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num,
5080 int nb_sectors, QEMUIOVector *iov,
5081 bool is_write)
5082{
5083 CoroutineIOCompletion co = {
5084 .coroutine = qemu_coroutine_self(),
5085 };
7c84b1b8 5086 BlockAIOCB *acb;
f9f05dc5
KW
5087
5088 if (is_write) {
a652d160
SH
5089 acb = bs->drv->bdrv_aio_writev(bs, sector_num, iov, nb_sectors,
5090 bdrv_co_io_em_complete, &co);
f9f05dc5 5091 } else {
a652d160
SH
5092 acb = bs->drv->bdrv_aio_readv(bs, sector_num, iov, nb_sectors,
5093 bdrv_co_io_em_complete, &co);
f9f05dc5
KW
5094 }
5095
59370aaa 5096 trace_bdrv_co_io_em(bs, sector_num, nb_sectors, is_write, acb);
f9f05dc5
KW
5097 if (!acb) {
5098 return -EIO;
5099 }
5100 qemu_coroutine_yield();
5101
5102 return co.ret;
5103}
5104
5105static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs,
5106 int64_t sector_num, int nb_sectors,
5107 QEMUIOVector *iov)
5108{
5109 return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false);
5110}
5111
5112static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs,
5113 int64_t sector_num, int nb_sectors,
5114 QEMUIOVector *iov)
5115{
5116 return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true);
5117}
5118
07f07615 5119static void coroutine_fn bdrv_flush_co_entry(void *opaque)
e7a8a783 5120{
07f07615
PB
5121 RwCo *rwco = opaque;
5122
5123 rwco->ret = bdrv_co_flush(rwco->bs);
5124}
5125
5126int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
5127{
eb489bb1
KW
5128 int ret;
5129
29cdb251 5130 if (!bs || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
07f07615 5131 return 0;
eb489bb1
KW
5132 }
5133
ca716364 5134 /* Write back cached data to the OS even with cache=unsafe */
bf736fe3 5135 BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_OS);
eb489bb1
KW
5136 if (bs->drv->bdrv_co_flush_to_os) {
5137 ret = bs->drv->bdrv_co_flush_to_os(bs);
5138 if (ret < 0) {
5139 return ret;
5140 }
5141 }
5142
ca716364
KW
5143 /* But don't actually force it to the disk with cache=unsafe */
5144 if (bs->open_flags & BDRV_O_NO_FLUSH) {
d4c82329 5145 goto flush_parent;
ca716364
KW
5146 }
5147
bf736fe3 5148 BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_DISK);
eb489bb1 5149 if (bs->drv->bdrv_co_flush_to_disk) {
29cdb251 5150 ret = bs->drv->bdrv_co_flush_to_disk(bs);
07f07615 5151 } else if (bs->drv->bdrv_aio_flush) {
7c84b1b8 5152 BlockAIOCB *acb;
07f07615
PB
5153 CoroutineIOCompletion co = {
5154 .coroutine = qemu_coroutine_self(),
5155 };
5156
5157 acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co);
5158 if (acb == NULL) {
29cdb251 5159 ret = -EIO;
07f07615
PB
5160 } else {
5161 qemu_coroutine_yield();
29cdb251 5162 ret = co.ret;
07f07615 5163 }
07f07615
PB
5164 } else {
5165 /*
5166 * Some block drivers always operate in either writethrough or unsafe
5167 * mode and don't support bdrv_flush therefore. Usually qemu doesn't
5168 * know how the server works (because the behaviour is hardcoded or
5169 * depends on server-side configuration), so we can't ensure that
5170 * everything is safe on disk. Returning an error doesn't work because
5171 * that would break guests even if the server operates in writethrough
5172 * mode.
5173 *
5174 * Let's hope the user knows what he's doing.
5175 */
29cdb251 5176 ret = 0;
07f07615 5177 }
29cdb251
PB
5178 if (ret < 0) {
5179 return ret;
5180 }
5181
5182 /* Now flush the underlying protocol. It will also have BDRV_O_NO_FLUSH
5183 * in the case of cache=unsafe, so there are no useless flushes.
5184 */
d4c82329 5185flush_parent:
29cdb251 5186 return bdrv_co_flush(bs->file);
07f07615
PB
5187}
5188
5a8a30db 5189void bdrv_invalidate_cache(BlockDriverState *bs, Error **errp)
0f15423c 5190{
5a8a30db
KW
5191 Error *local_err = NULL;
5192 int ret;
5193
3456a8d1
KW
5194 if (!bs->drv) {
5195 return;
5196 }
5197
7ea2d269
AK
5198 if (!(bs->open_flags & BDRV_O_INCOMING)) {
5199 return;
5200 }
5201 bs->open_flags &= ~BDRV_O_INCOMING;
5202
3456a8d1 5203 if (bs->drv->bdrv_invalidate_cache) {
5a8a30db 5204 bs->drv->bdrv_invalidate_cache(bs, &local_err);
3456a8d1 5205 } else if (bs->file) {
5a8a30db
KW
5206 bdrv_invalidate_cache(bs->file, &local_err);
5207 }
5208 if (local_err) {
5209 error_propagate(errp, local_err);
5210 return;
0f15423c 5211 }
3456a8d1 5212
5a8a30db
KW
5213 ret = refresh_total_sectors(bs, bs->total_sectors);
5214 if (ret < 0) {
5215 error_setg_errno(errp, -ret, "Could not refresh total sector count");
5216 return;
5217 }
0f15423c
AL
5218}
5219
5a8a30db 5220void bdrv_invalidate_cache_all(Error **errp)
0f15423c
AL
5221{
5222 BlockDriverState *bs;
5a8a30db 5223 Error *local_err = NULL;
0f15423c 5224
dc364f4c 5225 QTAILQ_FOREACH(bs, &bdrv_states, device_list) {
ed78cda3
SH
5226 AioContext *aio_context = bdrv_get_aio_context(bs);
5227
5228 aio_context_acquire(aio_context);
5a8a30db 5229 bdrv_invalidate_cache(bs, &local_err);
ed78cda3 5230 aio_context_release(aio_context);
5a8a30db
KW
5231 if (local_err) {
5232 error_propagate(errp, local_err);
5233 return;
5234 }
0f15423c
AL
5235 }
5236}
5237
07f07615
PB
5238int bdrv_flush(BlockDriverState *bs)
5239{
5240 Coroutine *co;
5241 RwCo rwco = {
5242 .bs = bs,
5243 .ret = NOT_DONE,
e7a8a783 5244 };
e7a8a783 5245
07f07615
PB
5246 if (qemu_in_coroutine()) {
5247 /* Fast-path if already in coroutine context */
5248 bdrv_flush_co_entry(&rwco);
5249 } else {
2572b37a
SH
5250 AioContext *aio_context = bdrv_get_aio_context(bs);
5251
07f07615
PB
5252 co = qemu_coroutine_create(bdrv_flush_co_entry);
5253 qemu_coroutine_enter(co, &rwco);
5254 while (rwco.ret == NOT_DONE) {
2572b37a 5255 aio_poll(aio_context, true);
07f07615 5256 }
e7a8a783 5257 }
07f07615
PB
5258
5259 return rwco.ret;
e7a8a783
KW
5260}
5261
775aa8b6
KW
5262typedef struct DiscardCo {
5263 BlockDriverState *bs;
5264 int64_t sector_num;
5265 int nb_sectors;
5266 int ret;
5267} DiscardCo;
4265d620
PB
5268static void coroutine_fn bdrv_discard_co_entry(void *opaque)
5269{
775aa8b6 5270 DiscardCo *rwco = opaque;
4265d620
PB
5271
5272 rwco->ret = bdrv_co_discard(rwco->bs, rwco->sector_num, rwco->nb_sectors);
5273}
5274
5275int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num,
5276 int nb_sectors)
5277{
b9c64947 5278 int max_discard, ret;
d51e9fe5 5279
4265d620
PB
5280 if (!bs->drv) {
5281 return -ENOMEDIUM;
b9c64947
HR
5282 }
5283
5284 ret = bdrv_check_request(bs, sector_num, nb_sectors);
5285 if (ret < 0) {
5286 return ret;
4265d620
PB
5287 } else if (bs->read_only) {
5288 return -EROFS;
df702c9b
PB
5289 }
5290
e4654d2d 5291 bdrv_reset_dirty(bs, sector_num, nb_sectors);
df702c9b 5292
9e8f1835
PB
5293 /* Do nothing if disabled. */
5294 if (!(bs->open_flags & BDRV_O_UNMAP)) {
5295 return 0;
5296 }
5297
d51e9fe5
PB
5298 if (!bs->drv->bdrv_co_discard && !bs->drv->bdrv_aio_discard) {
5299 return 0;
5300 }
6f14da52 5301
75af1f34 5302 max_discard = MIN_NON_ZERO(bs->bl.max_discard, BDRV_REQUEST_MAX_SECTORS);
d51e9fe5
PB
5303 while (nb_sectors > 0) {
5304 int ret;
5305 int num = nb_sectors;
6f14da52 5306
d51e9fe5
PB
5307 /* align request */
5308 if (bs->bl.discard_alignment &&
5309 num >= bs->bl.discard_alignment &&
5310 sector_num % bs->bl.discard_alignment) {
5311 if (num > bs->bl.discard_alignment) {
5312 num = bs->bl.discard_alignment;
6f14da52 5313 }
d51e9fe5
PB
5314 num -= sector_num % bs->bl.discard_alignment;
5315 }
6f14da52 5316
d51e9fe5
PB
5317 /* limit request size */
5318 if (num > max_discard) {
5319 num = max_discard;
5320 }
6f14da52 5321
d51e9fe5 5322 if (bs->drv->bdrv_co_discard) {
6f14da52 5323 ret = bs->drv->bdrv_co_discard(bs, sector_num, num);
d51e9fe5 5324 } else {
7c84b1b8 5325 BlockAIOCB *acb;
d51e9fe5
PB
5326 CoroutineIOCompletion co = {
5327 .coroutine = qemu_coroutine_self(),
5328 };
5329
5330 acb = bs->drv->bdrv_aio_discard(bs, sector_num, nb_sectors,
5331 bdrv_co_io_em_complete, &co);
5332 if (acb == NULL) {
5333 return -EIO;
5334 } else {
5335 qemu_coroutine_yield();
5336 ret = co.ret;
6f14da52 5337 }
6f14da52 5338 }
7ce21016 5339 if (ret && ret != -ENOTSUP) {
d51e9fe5 5340 return ret;
4265d620 5341 }
d51e9fe5
PB
5342
5343 sector_num += num;
5344 nb_sectors -= num;
4265d620 5345 }
d51e9fe5 5346 return 0;
4265d620
PB
5347}
5348
5349int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors)
5350{
5351 Coroutine *co;
775aa8b6 5352 DiscardCo rwco = {
4265d620
PB
5353 .bs = bs,
5354 .sector_num = sector_num,
5355 .nb_sectors = nb_sectors,
5356 .ret = NOT_DONE,
5357 };
5358
5359 if (qemu_in_coroutine()) {
5360 /* Fast-path if already in coroutine context */
5361 bdrv_discard_co_entry(&rwco);
5362 } else {
2572b37a
SH
5363 AioContext *aio_context = bdrv_get_aio_context(bs);
5364
4265d620
PB
5365 co = qemu_coroutine_create(bdrv_discard_co_entry);
5366 qemu_coroutine_enter(co, &rwco);
5367 while (rwco.ret == NOT_DONE) {
2572b37a 5368 aio_poll(aio_context, true);
4265d620
PB
5369 }
5370 }
5371
5372 return rwco.ret;
5373}
5374
19cb3738
FB
5375/**************************************************************/
5376/* removable device support */
5377
5378/**
5379 * Return TRUE if the media is present
5380 */
5381int bdrv_is_inserted(BlockDriverState *bs)
5382{
5383 BlockDriver *drv = bs->drv;
a1aff5bf 5384
19cb3738
FB
5385 if (!drv)
5386 return 0;
5387 if (!drv->bdrv_is_inserted)
a1aff5bf
MA
5388 return 1;
5389 return drv->bdrv_is_inserted(bs);
19cb3738
FB
5390}
5391
5392/**
8e49ca46
MA
5393 * Return whether the media changed since the last call to this
5394 * function, or -ENOTSUP if we don't know. Most drivers don't know.
19cb3738
FB
5395 */
5396int bdrv_media_changed(BlockDriverState *bs)
5397{
5398 BlockDriver *drv = bs->drv;
19cb3738 5399
8e49ca46
MA
5400 if (drv && drv->bdrv_media_changed) {
5401 return drv->bdrv_media_changed(bs);
5402 }
5403 return -ENOTSUP;
19cb3738
FB
5404}
5405
5406/**
5407 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
5408 */
f36f3949 5409void bdrv_eject(BlockDriverState *bs, bool eject_flag)
19cb3738
FB
5410{
5411 BlockDriver *drv = bs->drv;
bfb197e0 5412 const char *device_name;
19cb3738 5413
822e1cd1
MA
5414 if (drv && drv->bdrv_eject) {
5415 drv->bdrv_eject(bs, eject_flag);
19cb3738 5416 }
6f382ed2 5417
bfb197e0
MA
5418 device_name = bdrv_get_device_name(bs);
5419 if (device_name[0] != '\0') {
5420 qapi_event_send_device_tray_moved(device_name,
a5ee7bd4 5421 eject_flag, &error_abort);
6f382ed2 5422 }
19cb3738
FB
5423}
5424
19cb3738
FB
5425/**
5426 * Lock or unlock the media (if it is locked, the user won't be able
5427 * to eject it manually).
5428 */
025e849a 5429void bdrv_lock_medium(BlockDriverState *bs, bool locked)
19cb3738
FB
5430{
5431 BlockDriver *drv = bs->drv;
5432
025e849a 5433 trace_bdrv_lock_medium(bs, locked);
b8c6d095 5434
025e849a
MA
5435 if (drv && drv->bdrv_lock_medium) {
5436 drv->bdrv_lock_medium(bs, locked);
19cb3738
FB
5437 }
5438}
985a03b0
TS
5439
5440/* needed for generic scsi interface */
5441
5442int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
5443{
5444 BlockDriver *drv = bs->drv;
5445
5446 if (drv && drv->bdrv_ioctl)
5447 return drv->bdrv_ioctl(bs, req, buf);
5448 return -ENOTSUP;
5449}
7d780669 5450
7c84b1b8 5451BlockAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
221f715d 5452 unsigned long int req, void *buf,
097310b5 5453 BlockCompletionFunc *cb, void *opaque)
7d780669 5454{
221f715d 5455 BlockDriver *drv = bs->drv;
7d780669 5456
221f715d
AL
5457 if (drv && drv->bdrv_aio_ioctl)
5458 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
5459 return NULL;
7d780669 5460}
e268ca52 5461
1b7fd729 5462void bdrv_set_guest_block_size(BlockDriverState *bs, int align)
7b6f9300 5463{
1b7fd729 5464 bs->guest_block_size = align;
7b6f9300 5465}
7cd1e32a 5466
e268ca52
AL
5467void *qemu_blockalign(BlockDriverState *bs, size_t size)
5468{
339064d5 5469 return qemu_memalign(bdrv_opt_mem_align(bs), size);
e268ca52 5470}
7cd1e32a 5471
9ebd8448
HR
5472void *qemu_blockalign0(BlockDriverState *bs, size_t size)
5473{
5474 return memset(qemu_blockalign(bs, size), 0, size);
5475}
5476
7d2a35cc
KW
5477void *qemu_try_blockalign(BlockDriverState *bs, size_t size)
5478{
5479 size_t align = bdrv_opt_mem_align(bs);
5480
5481 /* Ensure that NULL is never returned on success */
5482 assert(align > 0);
5483 if (size == 0) {
5484 size = align;
5485 }
5486
5487 return qemu_try_memalign(align, size);
5488}
5489
9ebd8448
HR
5490void *qemu_try_blockalign0(BlockDriverState *bs, size_t size)
5491{
5492 void *mem = qemu_try_blockalign(bs, size);
5493
5494 if (mem) {
5495 memset(mem, 0, size);
5496 }
5497
5498 return mem;
5499}
5500
c53b1c51
SH
5501/*
5502 * Check if all memory in this vector is sector aligned.
5503 */
5504bool bdrv_qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov)
5505{
5506 int i;
339064d5 5507 size_t alignment = bdrv_opt_mem_align(bs);
c53b1c51
SH
5508
5509 for (i = 0; i < qiov->niov; i++) {
339064d5 5510 if ((uintptr_t) qiov->iov[i].iov_base % alignment) {
c53b1c51 5511 return false;
1ff735bd 5512 }
339064d5 5513 if (qiov->iov[i].iov_len % alignment) {
1ff735bd 5514 return false;
c53b1c51
SH
5515 }
5516 }
5517
5518 return true;
5519}
5520
0db6e54a
FZ
5521BdrvDirtyBitmap *bdrv_find_dirty_bitmap(BlockDriverState *bs, const char *name)
5522{
5523 BdrvDirtyBitmap *bm;
5524
5525 assert(name);
5526 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
5527 if (bm->name && !strcmp(name, bm->name)) {
5528 return bm;
5529 }
5530 }
5531 return NULL;
5532}
5533
20dca810 5534void bdrv_dirty_bitmap_make_anon(BdrvDirtyBitmap *bitmap)
0db6e54a 5535{
9bd2b08f 5536 assert(!bdrv_dirty_bitmap_frozen(bitmap));
0db6e54a
FZ
5537 g_free(bitmap->name);
5538 bitmap->name = NULL;
5539}
5540
5541BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs,
5fba6c0e 5542 uint32_t granularity,
0db6e54a 5543 const char *name,
b8afb520 5544 Error **errp)
7cd1e32a
LS
5545{
5546 int64_t bitmap_size;
e4654d2d 5547 BdrvDirtyBitmap *bitmap;
5fba6c0e 5548 uint32_t sector_granularity;
a55eb92c 5549
50717e94
PB
5550 assert((granularity & (granularity - 1)) == 0);
5551
0db6e54a
FZ
5552 if (name && bdrv_find_dirty_bitmap(bs, name)) {
5553 error_setg(errp, "Bitmap already exists: %s", name);
5554 return NULL;
5555 }
5fba6c0e
JS
5556 sector_granularity = granularity >> BDRV_SECTOR_BITS;
5557 assert(sector_granularity);
57322b78 5558 bitmap_size = bdrv_nb_sectors(bs);
b8afb520
FZ
5559 if (bitmap_size < 0) {
5560 error_setg_errno(errp, -bitmap_size, "could not get length of device");
5561 errno = -bitmap_size;
5562 return NULL;
5563 }
5839e53b 5564 bitmap = g_new0(BdrvDirtyBitmap, 1);
5fba6c0e 5565 bitmap->bitmap = hbitmap_alloc(bitmap_size, ctz32(sector_granularity));
e74e6b78 5566 bitmap->size = bitmap_size;
0db6e54a 5567 bitmap->name = g_strdup(name);
b8e6fb75 5568 bitmap->disabled = false;
e4654d2d
FZ
5569 QLIST_INSERT_HEAD(&bs->dirty_bitmaps, bitmap, list);
5570 return bitmap;
5571}
5572
9bd2b08f
JS
5573bool bdrv_dirty_bitmap_frozen(BdrvDirtyBitmap *bitmap)
5574{
5575 return bitmap->successor;
5576}
5577
b8e6fb75
JS
5578bool bdrv_dirty_bitmap_enabled(BdrvDirtyBitmap *bitmap)
5579{
9bd2b08f
JS
5580 return !(bitmap->disabled || bitmap->successor);
5581}
5582
5583/**
5584 * Create a successor bitmap destined to replace this bitmap after an operation.
5585 * Requires that the bitmap is not frozen and has no successor.
5586 */
5587int bdrv_dirty_bitmap_create_successor(BlockDriverState *bs,
5588 BdrvDirtyBitmap *bitmap, Error **errp)
5589{
5590 uint64_t granularity;
5591 BdrvDirtyBitmap *child;
5592
5593 if (bdrv_dirty_bitmap_frozen(bitmap)) {
5594 error_setg(errp, "Cannot create a successor for a bitmap that is "
5595 "currently frozen");
5596 return -1;
5597 }
5598 assert(!bitmap->successor);
5599
5600 /* Create an anonymous successor */
5601 granularity = bdrv_dirty_bitmap_granularity(bitmap);
5602 child = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp);
5603 if (!child) {
5604 return -1;
5605 }
5606
5607 /* Successor will be on or off based on our current state. */
5608 child->disabled = bitmap->disabled;
5609
5610 /* Install the successor and freeze the parent */
5611 bitmap->successor = child;
5612 return 0;
5613}
5614
5615/**
5616 * For a bitmap with a successor, yield our name to the successor,
5617 * delete the old bitmap, and return a handle to the new bitmap.
5618 */
5619BdrvDirtyBitmap *bdrv_dirty_bitmap_abdicate(BlockDriverState *bs,
5620 BdrvDirtyBitmap *bitmap,
5621 Error **errp)
5622{
5623 char *name;
5624 BdrvDirtyBitmap *successor = bitmap->successor;
5625
5626 if (successor == NULL) {
5627 error_setg(errp, "Cannot relinquish control if "
5628 "there's no successor present");
5629 return NULL;
5630 }
5631
5632 name = bitmap->name;
5633 bitmap->name = NULL;
5634 successor->name = name;
5635 bitmap->successor = NULL;
5636 bdrv_release_dirty_bitmap(bs, bitmap);
5637
5638 return successor;
5639}
5640
5641/**
5642 * In cases of failure where we can no longer safely delete the parent,
5643 * we may wish to re-join the parent and child/successor.
5644 * The merged parent will be un-frozen, but not explicitly re-enabled.
5645 */
5646BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BlockDriverState *bs,
5647 BdrvDirtyBitmap *parent,
5648 Error **errp)
5649{
5650 BdrvDirtyBitmap *successor = parent->successor;
5651
5652 if (!successor) {
5653 error_setg(errp, "Cannot reclaim a successor when none is present");
5654 return NULL;
5655 }
5656
5657 if (!hbitmap_merge(parent->bitmap, successor->bitmap)) {
5658 error_setg(errp, "Merging of parent and successor bitmap failed");
5659 return NULL;
5660 }
5661 bdrv_release_dirty_bitmap(bs, successor);
5662 parent->successor = NULL;
5663
5664 return parent;
b8e6fb75
JS
5665}
5666
ce1ffea8
JS
5667/**
5668 * Truncates _all_ bitmaps attached to a BDS.
5669 */
5670static void bdrv_dirty_bitmap_truncate(BlockDriverState *bs)
5671{
5672 BdrvDirtyBitmap *bitmap;
5673 uint64_t size = bdrv_nb_sectors(bs);
5674
5675 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
5676 if (bdrv_dirty_bitmap_frozen(bitmap)) {
5677 continue;
5678 }
5679 hbitmap_truncate(bitmap->bitmap, size);
5680 }
5681}
5682
e4654d2d
FZ
5683void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap)
5684{
5685 BdrvDirtyBitmap *bm, *next;
5686 QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) {
5687 if (bm == bitmap) {
9bd2b08f 5688 assert(!bdrv_dirty_bitmap_frozen(bm));
e4654d2d
FZ
5689 QLIST_REMOVE(bitmap, list);
5690 hbitmap_free(bitmap->bitmap);
0db6e54a 5691 g_free(bitmap->name);
e4654d2d
FZ
5692 g_free(bitmap);
5693 return;
a55eb92c 5694 }
7cd1e32a
LS
5695 }
5696}
5697
b8e6fb75
JS
5698void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
5699{
9bd2b08f 5700 assert(!bdrv_dirty_bitmap_frozen(bitmap));
b8e6fb75
JS
5701 bitmap->disabled = true;
5702}
5703
5704void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
5705{
9bd2b08f 5706 assert(!bdrv_dirty_bitmap_frozen(bitmap));
b8e6fb75
JS
5707 bitmap->disabled = false;
5708}
5709
21b56835
FZ
5710BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs)
5711{
5712 BdrvDirtyBitmap *bm;
5713 BlockDirtyInfoList *list = NULL;
5714 BlockDirtyInfoList **plist = &list;
5715
5716 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
5839e53b
MA
5717 BlockDirtyInfo *info = g_new0(BlockDirtyInfo, 1);
5718 BlockDirtyInfoList *entry = g_new0(BlockDirtyInfoList, 1);
20dca810 5719 info->count = bdrv_get_dirty_count(bm);
592fdd02 5720 info->granularity = bdrv_dirty_bitmap_granularity(bm);
0db6e54a
FZ
5721 info->has_name = !!bm->name;
5722 info->name = g_strdup(bm->name);
a113534f 5723 info->frozen = bdrv_dirty_bitmap_frozen(bm);
21b56835
FZ
5724 entry->value = info;
5725 *plist = entry;
5726 plist = &entry->next;
5727 }
5728
5729 return list;
5730}
5731
e4654d2d 5732int bdrv_get_dirty(BlockDriverState *bs, BdrvDirtyBitmap *bitmap, int64_t sector)
7cd1e32a 5733{
e4654d2d
FZ
5734 if (bitmap) {
5735 return hbitmap_get(bitmap->bitmap, sector);
7cd1e32a
LS
5736 } else {
5737 return 0;
5738 }
5739}
5740
341ebc2f
JS
5741/**
5742 * Chooses a default granularity based on the existing cluster size,
5743 * but clamped between [4K, 64K]. Defaults to 64K in the case that there
5744 * is no cluster size information available.
5745 */
5746uint32_t bdrv_get_default_bitmap_granularity(BlockDriverState *bs)
5747{
5748 BlockDriverInfo bdi;
5749 uint32_t granularity;
5750
5751 if (bdrv_get_info(bs, &bdi) >= 0 && bdi.cluster_size > 0) {
5752 granularity = MAX(4096, bdi.cluster_size);
5753 granularity = MIN(65536, granularity);
5754 } else {
5755 granularity = 65536;
5756 }
5757
5758 return granularity;
5759}
5760
592fdd02
JS
5761uint32_t bdrv_dirty_bitmap_granularity(BdrvDirtyBitmap *bitmap)
5762{
5763 return BDRV_SECTOR_SIZE << hbitmap_granularity(bitmap->bitmap);
5764}
5765
20dca810 5766void bdrv_dirty_iter_init(BdrvDirtyBitmap *bitmap, HBitmapIter *hbi)
1755da16 5767{
e4654d2d 5768 hbitmap_iter_init(hbi, bitmap->bitmap, 0);
1755da16
PB
5769}
5770
20dca810 5771void bdrv_set_dirty_bitmap(BdrvDirtyBitmap *bitmap,
c4237dfa
VSO
5772 int64_t cur_sector, int nr_sectors)
5773{
b8e6fb75 5774 assert(bdrv_dirty_bitmap_enabled(bitmap));
c4237dfa
VSO
5775 hbitmap_set(bitmap->bitmap, cur_sector, nr_sectors);
5776}
5777
20dca810 5778void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap *bitmap,
c4237dfa
VSO
5779 int64_t cur_sector, int nr_sectors)
5780{
b8e6fb75 5781 assert(bdrv_dirty_bitmap_enabled(bitmap));
c4237dfa
VSO
5782 hbitmap_reset(bitmap->bitmap, cur_sector, nr_sectors);
5783}
5784
e74e6b78
JS
5785void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap)
5786{
5787 assert(bdrv_dirty_bitmap_enabled(bitmap));
5788 hbitmap_reset(bitmap->bitmap, 0, bitmap->size);
5789}
5790
e0c47b6c
SH
5791void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector,
5792 int nr_sectors)
1755da16 5793{
e4654d2d
FZ
5794 BdrvDirtyBitmap *bitmap;
5795 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
b8e6fb75
JS
5796 if (!bdrv_dirty_bitmap_enabled(bitmap)) {
5797 continue;
5798 }
e4654d2d
FZ
5799 hbitmap_set(bitmap->bitmap, cur_sector, nr_sectors);
5800 }
1755da16
PB
5801}
5802
e0c47b6c
SH
5803void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
5804 int nr_sectors)
7cd1e32a 5805{
e4654d2d
FZ
5806 BdrvDirtyBitmap *bitmap;
5807 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
b8e6fb75
JS
5808 if (!bdrv_dirty_bitmap_enabled(bitmap)) {
5809 continue;
5810 }
e4654d2d
FZ
5811 hbitmap_reset(bitmap->bitmap, cur_sector, nr_sectors);
5812 }
7cd1e32a 5813}
aaa0eb75 5814
d58d8453
JS
5815/**
5816 * Advance an HBitmapIter to an arbitrary offset.
5817 */
5818void bdrv_set_dirty_iter(HBitmapIter *hbi, int64_t offset)
5819{
5820 assert(hbi->hb);
5821 hbitmap_iter_init(hbi, hbi->hb, offset);
5822}
5823
20dca810 5824int64_t bdrv_get_dirty_count(BdrvDirtyBitmap *bitmap)
aaa0eb75 5825{
e4654d2d 5826 return hbitmap_count(bitmap->bitmap);
aaa0eb75 5827}
f88e1a42 5828
9fcb0251
FZ
5829/* Get a reference to bs */
5830void bdrv_ref(BlockDriverState *bs)
5831{
5832 bs->refcnt++;
5833}
5834
5835/* Release a previously grabbed reference to bs.
5836 * If after releasing, reference count is zero, the BlockDriverState is
5837 * deleted. */
5838void bdrv_unref(BlockDriverState *bs)
5839{
9a4d5ca6
JC
5840 if (!bs) {
5841 return;
5842 }
9fcb0251
FZ
5843 assert(bs->refcnt > 0);
5844 if (--bs->refcnt == 0) {
5845 bdrv_delete(bs);
5846 }
5847}
5848
fbe40ff7
FZ
5849struct BdrvOpBlocker {
5850 Error *reason;
5851 QLIST_ENTRY(BdrvOpBlocker) list;
5852};
5853
5854bool bdrv_op_is_blocked(BlockDriverState *bs, BlockOpType op, Error **errp)
5855{
5856 BdrvOpBlocker *blocker;
5857 assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
5858 if (!QLIST_EMPTY(&bs->op_blockers[op])) {
5859 blocker = QLIST_FIRST(&bs->op_blockers[op]);
5860 if (errp) {
81e5f78a
AG
5861 error_setg(errp, "Node '%s' is busy: %s",
5862 bdrv_get_device_or_node_name(bs),
bfb197e0 5863 error_get_pretty(blocker->reason));
fbe40ff7
FZ
5864 }
5865 return true;
5866 }
5867 return false;
5868}
5869
5870void bdrv_op_block(BlockDriverState *bs, BlockOpType op, Error *reason)
5871{
5872 BdrvOpBlocker *blocker;
5873 assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
5874
5839e53b 5875 blocker = g_new0(BdrvOpBlocker, 1);
fbe40ff7
FZ
5876 blocker->reason = reason;
5877 QLIST_INSERT_HEAD(&bs->op_blockers[op], blocker, list);
5878}
5879
5880void bdrv_op_unblock(BlockDriverState *bs, BlockOpType op, Error *reason)
5881{
5882 BdrvOpBlocker *blocker, *next;
5883 assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
5884 QLIST_FOREACH_SAFE(blocker, &bs->op_blockers[op], list, next) {
5885 if (blocker->reason == reason) {
5886 QLIST_REMOVE(blocker, list);
5887 g_free(blocker);
5888 }
5889 }
5890}
5891
5892void bdrv_op_block_all(BlockDriverState *bs, Error *reason)
5893{
5894 int i;
5895 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
5896 bdrv_op_block(bs, i, reason);
5897 }
5898}
5899
5900void bdrv_op_unblock_all(BlockDriverState *bs, Error *reason)
5901{
5902 int i;
5903 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
5904 bdrv_op_unblock(bs, i, reason);
5905 }
5906}
5907
5908bool bdrv_op_blocker_is_empty(BlockDriverState *bs)
5909{
5910 int i;
5911
5912 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
5913 if (!QLIST_EMPTY(&bs->op_blockers[i])) {
5914 return false;
5915 }
5916 }
5917 return true;
5918}
5919
28a7282a
LC
5920void bdrv_iostatus_enable(BlockDriverState *bs)
5921{
d6bf279e 5922 bs->iostatus_enabled = true;
58e21ef5 5923 bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
28a7282a
LC
5924}
5925
5926/* The I/O status is only enabled if the drive explicitly
5927 * enables it _and_ the VM is configured to stop on errors */
5928bool bdrv_iostatus_is_enabled(const BlockDriverState *bs)
5929{
d6bf279e 5930 return (bs->iostatus_enabled &&
92aa5c6d
PB
5931 (bs->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC ||
5932 bs->on_write_error == BLOCKDEV_ON_ERROR_STOP ||
5933 bs->on_read_error == BLOCKDEV_ON_ERROR_STOP));
28a7282a
LC
5934}
5935
5936void bdrv_iostatus_disable(BlockDriverState *bs)
5937{
d6bf279e 5938 bs->iostatus_enabled = false;
28a7282a
LC
5939}
5940
5941void bdrv_iostatus_reset(BlockDriverState *bs)
5942{
5943 if (bdrv_iostatus_is_enabled(bs)) {
58e21ef5 5944 bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
3bd293c3
PB
5945 if (bs->job) {
5946 block_job_iostatus_reset(bs->job);
5947 }
28a7282a
LC
5948 }
5949}
5950
28a7282a
LC
5951void bdrv_iostatus_set_err(BlockDriverState *bs, int error)
5952{
3e1caa5f
PB
5953 assert(bdrv_iostatus_is_enabled(bs));
5954 if (bs->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
58e21ef5
LC
5955 bs->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
5956 BLOCK_DEVICE_IO_STATUS_FAILED;
28a7282a
LC
5957 }
5958}
5959
d92ada22
LC
5960void bdrv_img_create(const char *filename, const char *fmt,
5961 const char *base_filename, const char *base_fmt,
f382d43a
MR
5962 char *options, uint64_t img_size, int flags,
5963 Error **errp, bool quiet)
f88e1a42 5964{
83d0521a
CL
5965 QemuOptsList *create_opts = NULL;
5966 QemuOpts *opts = NULL;
5967 const char *backing_fmt, *backing_file;
5968 int64_t size;
f88e1a42 5969 BlockDriver *drv, *proto_drv;
96df67d1 5970 BlockDriver *backing_drv = NULL;
cc84d90f 5971 Error *local_err = NULL;
f88e1a42
JS
5972 int ret = 0;
5973
5974 /* Find driver and parse its options */
5975 drv = bdrv_find_format(fmt);
5976 if (!drv) {
71c79813 5977 error_setg(errp, "Unknown file format '%s'", fmt);
d92ada22 5978 return;
f88e1a42
JS
5979 }
5980
b65a5e12 5981 proto_drv = bdrv_find_protocol(filename, true, errp);
f88e1a42 5982 if (!proto_drv) {
d92ada22 5983 return;
f88e1a42
JS
5984 }
5985
c6149724
HR
5986 if (!drv->create_opts) {
5987 error_setg(errp, "Format driver '%s' does not support image creation",
5988 drv->format_name);
5989 return;
5990 }
5991
5992 if (!proto_drv->create_opts) {
5993 error_setg(errp, "Protocol driver '%s' does not support image creation",
5994 proto_drv->format_name);
5995 return;
5996 }
5997
c282e1fd
CL
5998 create_opts = qemu_opts_append(create_opts, drv->create_opts);
5999 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
f88e1a42
JS
6000
6001 /* Create parameter list with default values */
83d0521a 6002 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
39101f25 6003 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
f88e1a42
JS
6004
6005 /* Parse -o options */
6006 if (options) {
dc523cd3
MA
6007 qemu_opts_do_parse(opts, options, NULL, &local_err);
6008 if (local_err) {
6009 error_report_err(local_err);
6010 local_err = NULL;
83d0521a 6011 error_setg(errp, "Invalid options for file format '%s'", fmt);
f88e1a42
JS
6012 goto out;
6013 }
6014 }
6015
6016 if (base_filename) {
f43e47db 6017 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &local_err);
6be4194b 6018 if (local_err) {
71c79813
LC
6019 error_setg(errp, "Backing file not supported for file format '%s'",
6020 fmt);
f88e1a42
JS
6021 goto out;
6022 }
6023 }
6024
6025 if (base_fmt) {
f43e47db 6026 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &local_err);
6be4194b 6027 if (local_err) {
71c79813
LC
6028 error_setg(errp, "Backing file format not supported for file "
6029 "format '%s'", fmt);
f88e1a42
JS
6030 goto out;
6031 }
6032 }
6033
83d0521a
CL
6034 backing_file = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
6035 if (backing_file) {
6036 if (!strcmp(filename, backing_file)) {
71c79813
LC
6037 error_setg(errp, "Error: Trying to create an image with the "
6038 "same filename as the backing file");
792da93a
JS
6039 goto out;
6040 }
6041 }
6042
83d0521a
CL
6043 backing_fmt = qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT);
6044 if (backing_fmt) {
6045 backing_drv = bdrv_find_format(backing_fmt);
96df67d1 6046 if (!backing_drv) {
71c79813 6047 error_setg(errp, "Unknown backing file format '%s'",
83d0521a 6048 backing_fmt);
f88e1a42
JS
6049 goto out;
6050 }
6051 }
6052
6053 // The size for the image must always be specified, with one exception:
6054 // If we are using a backing file, we can obtain the size from there
83d0521a
CL
6055 size = qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 0);
6056 if (size == -1) {
6057 if (backing_file) {
66f6b814 6058 BlockDriverState *bs;
29168018 6059 char *full_backing = g_new0(char, PATH_MAX);
52bf1e72 6060 int64_t size;
63090dac
PB
6061 int back_flags;
6062
29168018
HR
6063 bdrv_get_full_backing_filename_from_filename(filename, backing_file,
6064 full_backing, PATH_MAX,
6065 &local_err);
6066 if (local_err) {
6067 g_free(full_backing);
6068 goto out;
6069 }
6070
63090dac
PB
6071 /* backing files always opened read-only */
6072 back_flags =
6073 flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
f88e1a42 6074
f67503e5 6075 bs = NULL;
29168018 6076 ret = bdrv_open(&bs, full_backing, NULL, NULL, back_flags,
cc84d90f 6077 backing_drv, &local_err);
29168018 6078 g_free(full_backing);
f88e1a42 6079 if (ret < 0) {
f88e1a42
JS
6080 goto out;
6081 }
52bf1e72
MA
6082 size = bdrv_getlength(bs);
6083 if (size < 0) {
6084 error_setg_errno(errp, -size, "Could not get size of '%s'",
6085 backing_file);
6086 bdrv_unref(bs);
6087 goto out;
6088 }
f88e1a42 6089
39101f25 6090 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, size, &error_abort);
66f6b814
HR
6091
6092 bdrv_unref(bs);
f88e1a42 6093 } else {
71c79813 6094 error_setg(errp, "Image creation needs a size parameter");
f88e1a42
JS
6095 goto out;
6096 }
6097 }
6098
f382d43a 6099 if (!quiet) {
43c5d8f8
FZ
6100 printf("Formatting '%s', fmt=%s", filename, fmt);
6101 qemu_opts_print(opts, " ");
f382d43a
MR
6102 puts("");
6103 }
83d0521a 6104
c282e1fd 6105 ret = bdrv_create(drv, filename, opts, &local_err);
83d0521a 6106
cc84d90f
HR
6107 if (ret == -EFBIG) {
6108 /* This is generally a better message than whatever the driver would
6109 * deliver (especially because of the cluster_size_hint), since that
6110 * is most probably not much different from "image too large". */
6111 const char *cluster_size_hint = "";
83d0521a 6112 if (qemu_opt_get_size(opts, BLOCK_OPT_CLUSTER_SIZE, 0)) {
cc84d90f 6113 cluster_size_hint = " (try using a larger cluster size)";
f88e1a42 6114 }
cc84d90f
HR
6115 error_setg(errp, "The image size is too large for file format '%s'"
6116 "%s", fmt, cluster_size_hint);
6117 error_free(local_err);
6118 local_err = NULL;
f88e1a42
JS
6119 }
6120
6121out:
83d0521a
CL
6122 qemu_opts_del(opts);
6123 qemu_opts_free(create_opts);
84d18f06 6124 if (local_err) {
cc84d90f
HR
6125 error_propagate(errp, local_err);
6126 }
f88e1a42 6127}
85d126f3
SH
6128
6129AioContext *bdrv_get_aio_context(BlockDriverState *bs)
6130{
dcd04228
SH
6131 return bs->aio_context;
6132}
6133
6134void bdrv_detach_aio_context(BlockDriverState *bs)
6135{
33384421
HR
6136 BdrvAioNotifier *baf;
6137
dcd04228
SH
6138 if (!bs->drv) {
6139 return;
6140 }
6141
33384421
HR
6142 QLIST_FOREACH(baf, &bs->aio_notifiers, list) {
6143 baf->detach_aio_context(baf->opaque);
6144 }
6145
13af91eb
SH
6146 if (bs->io_limits_enabled) {
6147 throttle_detach_aio_context(&bs->throttle_state);
6148 }
dcd04228
SH
6149 if (bs->drv->bdrv_detach_aio_context) {
6150 bs->drv->bdrv_detach_aio_context(bs);
6151 }
6152 if (bs->file) {
6153 bdrv_detach_aio_context(bs->file);
6154 }
6155 if (bs->backing_hd) {
6156 bdrv_detach_aio_context(bs->backing_hd);
6157 }
6158
6159 bs->aio_context = NULL;
6160}
6161
6162void bdrv_attach_aio_context(BlockDriverState *bs,
6163 AioContext *new_context)
6164{
33384421
HR
6165 BdrvAioNotifier *ban;
6166
dcd04228
SH
6167 if (!bs->drv) {
6168 return;
6169 }
6170
6171 bs->aio_context = new_context;
6172
6173 if (bs->backing_hd) {
6174 bdrv_attach_aio_context(bs->backing_hd, new_context);
6175 }
6176 if (bs->file) {
6177 bdrv_attach_aio_context(bs->file, new_context);
6178 }
6179 if (bs->drv->bdrv_attach_aio_context) {
6180 bs->drv->bdrv_attach_aio_context(bs, new_context);
6181 }
13af91eb
SH
6182 if (bs->io_limits_enabled) {
6183 throttle_attach_aio_context(&bs->throttle_state, new_context);
6184 }
33384421
HR
6185
6186 QLIST_FOREACH(ban, &bs->aio_notifiers, list) {
6187 ban->attached_aio_context(new_context, ban->opaque);
6188 }
dcd04228
SH
6189}
6190
6191void bdrv_set_aio_context(BlockDriverState *bs, AioContext *new_context)
6192{
6193 bdrv_drain_all(); /* ensure there are no in-flight requests */
6194
6195 bdrv_detach_aio_context(bs);
6196
6197 /* This function executes in the old AioContext so acquire the new one in
6198 * case it runs in a different thread.
6199 */
6200 aio_context_acquire(new_context);
6201 bdrv_attach_aio_context(bs, new_context);
6202 aio_context_release(new_context);
85d126f3 6203}
d616b224 6204
33384421
HR
6205void bdrv_add_aio_context_notifier(BlockDriverState *bs,
6206 void (*attached_aio_context)(AioContext *new_context, void *opaque),
6207 void (*detach_aio_context)(void *opaque), void *opaque)
6208{
6209 BdrvAioNotifier *ban = g_new(BdrvAioNotifier, 1);
6210 *ban = (BdrvAioNotifier){
6211 .attached_aio_context = attached_aio_context,
6212 .detach_aio_context = detach_aio_context,
6213 .opaque = opaque
6214 };
6215
6216 QLIST_INSERT_HEAD(&bs->aio_notifiers, ban, list);
6217}
6218
6219void bdrv_remove_aio_context_notifier(BlockDriverState *bs,
6220 void (*attached_aio_context)(AioContext *,
6221 void *),
6222 void (*detach_aio_context)(void *),
6223 void *opaque)
6224{
6225 BdrvAioNotifier *ban, *ban_next;
6226
6227 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) {
6228 if (ban->attached_aio_context == attached_aio_context &&
6229 ban->detach_aio_context == detach_aio_context &&
6230 ban->opaque == opaque)
6231 {
6232 QLIST_REMOVE(ban, list);
6233 g_free(ban);
6234
6235 return;
6236 }
6237 }
6238
6239 abort();
6240}
6241
d616b224
SH
6242void bdrv_add_before_write_notifier(BlockDriverState *bs,
6243 NotifierWithReturn *notifier)
6244{
6245 notifier_with_return_list_add(&bs->before_write_notifiers, notifier);
6246}
6f176b48 6247
77485434
HR
6248int bdrv_amend_options(BlockDriverState *bs, QemuOpts *opts,
6249 BlockDriverAmendStatusCB *status_cb)
6f176b48 6250{
c282e1fd 6251 if (!bs->drv->bdrv_amend_options) {
6f176b48
HR
6252 return -ENOTSUP;
6253 }
77485434 6254 return bs->drv->bdrv_amend_options(bs, opts, status_cb);
6f176b48 6255}
f6186f49 6256
b5042a36
BC
6257/* This function will be called by the bdrv_recurse_is_first_non_filter method
6258 * of block filter and by bdrv_is_first_non_filter.
6259 * It is used to test if the given bs is the candidate or recurse more in the
6260 * node graph.
212a5a8f 6261 */
b5042a36 6262bool bdrv_recurse_is_first_non_filter(BlockDriverState *bs,
212a5a8f 6263 BlockDriverState *candidate)
f6186f49 6264{
b5042a36
BC
6265 /* return false if basic checks fails */
6266 if (!bs || !bs->drv) {
212a5a8f 6267 return false;
f6186f49
BC
6268 }
6269
b5042a36
BC
6270 /* the code reached a non block filter driver -> check if the bs is
6271 * the same as the candidate. It's the recursion termination condition.
6272 */
6273 if (!bs->drv->is_filter) {
6274 return bs == candidate;
212a5a8f 6275 }
b5042a36 6276 /* Down this path the driver is a block filter driver */
212a5a8f 6277
b5042a36
BC
6278 /* If the block filter recursion method is defined use it to recurse down
6279 * the node graph.
6280 */
6281 if (bs->drv->bdrv_recurse_is_first_non_filter) {
212a5a8f 6282 return bs->drv->bdrv_recurse_is_first_non_filter(bs, candidate);
f6186f49
BC
6283 }
6284
b5042a36
BC
6285 /* the driver is a block filter but don't allow to recurse -> return false
6286 */
6287 return false;
f6186f49
BC
6288}
6289
212a5a8f
BC
6290/* This function checks if the candidate is the first non filter bs down it's
6291 * bs chain. Since we don't have pointers to parents it explore all bs chains
6292 * from the top. Some filters can choose not to pass down the recursion.
6293 */
6294bool bdrv_is_first_non_filter(BlockDriverState *candidate)
f6186f49 6295{
212a5a8f
BC
6296 BlockDriverState *bs;
6297
6298 /* walk down the bs forest recursively */
6299 QTAILQ_FOREACH(bs, &bdrv_states, device_list) {
6300 bool perm;
6301
b5042a36 6302 /* try to recurse in this top level bs */
e6dc8a1f 6303 perm = bdrv_recurse_is_first_non_filter(bs, candidate);
212a5a8f
BC
6304
6305 /* candidate is the first non filter */
6306 if (perm) {
6307 return true;
6308 }
6309 }
6310
6311 return false;
f6186f49 6312}
09158f00
BC
6313
6314BlockDriverState *check_to_replace_node(const char *node_name, Error **errp)
6315{
6316 BlockDriverState *to_replace_bs = bdrv_find_node(node_name);
5a7e7a0b
SH
6317 AioContext *aio_context;
6318
09158f00
BC
6319 if (!to_replace_bs) {
6320 error_setg(errp, "Node name '%s' not found", node_name);
6321 return NULL;
6322 }
6323
5a7e7a0b
SH
6324 aio_context = bdrv_get_aio_context(to_replace_bs);
6325 aio_context_acquire(aio_context);
6326
09158f00 6327 if (bdrv_op_is_blocked(to_replace_bs, BLOCK_OP_TYPE_REPLACE, errp)) {
5a7e7a0b
SH
6328 to_replace_bs = NULL;
6329 goto out;
09158f00
BC
6330 }
6331
6332 /* We don't want arbitrary node of the BDS chain to be replaced only the top
6333 * most non filter in order to prevent data corruption.
6334 * Another benefit is that this tests exclude backing files which are
6335 * blocked by the backing blockers.
6336 */
6337 if (!bdrv_is_first_non_filter(to_replace_bs)) {
6338 error_setg(errp, "Only top most non filter can be replaced");
5a7e7a0b
SH
6339 to_replace_bs = NULL;
6340 goto out;
09158f00
BC
6341 }
6342
5a7e7a0b
SH
6343out:
6344 aio_context_release(aio_context);
09158f00
BC
6345 return to_replace_bs;
6346}
448ad91d
ML
6347
6348void bdrv_io_plug(BlockDriverState *bs)
6349{
6350 BlockDriver *drv = bs->drv;
6351 if (drv && drv->bdrv_io_plug) {
6352 drv->bdrv_io_plug(bs);
6353 } else if (bs->file) {
6354 bdrv_io_plug(bs->file);
6355 }
6356}
6357
6358void bdrv_io_unplug(BlockDriverState *bs)
6359{
6360 BlockDriver *drv = bs->drv;
6361 if (drv && drv->bdrv_io_unplug) {
6362 drv->bdrv_io_unplug(bs);
6363 } else if (bs->file) {
6364 bdrv_io_unplug(bs->file);
6365 }
6366}
6367
6368void bdrv_flush_io_queue(BlockDriverState *bs)
6369{
6370 BlockDriver *drv = bs->drv;
6371 if (drv && drv->bdrv_flush_io_queue) {
6372 drv->bdrv_flush_io_queue(bs);
6373 } else if (bs->file) {
6374 bdrv_flush_io_queue(bs->file);
6375 }
6376}
91af7014
HR
6377
6378static bool append_open_options(QDict *d, BlockDriverState *bs)
6379{
6380 const QDictEntry *entry;
6381 bool found_any = false;
6382
6383 for (entry = qdict_first(bs->options); entry;
6384 entry = qdict_next(bs->options, entry))
6385 {
6386 /* Only take options for this level and exclude all non-driver-specific
6387 * options */
6388 if (!strchr(qdict_entry_key(entry), '.') &&
6389 strcmp(qdict_entry_key(entry), "node-name"))
6390 {
6391 qobject_incref(qdict_entry_value(entry));
6392 qdict_put_obj(d, qdict_entry_key(entry), qdict_entry_value(entry));
6393 found_any = true;
6394 }
6395 }
6396
6397 return found_any;
6398}
6399
6400/* Updates the following BDS fields:
6401 * - exact_filename: A filename which may be used for opening a block device
6402 * which (mostly) equals the given BDS (even without any
6403 * other options; so reading and writing must return the same
6404 * results, but caching etc. may be different)
6405 * - full_open_options: Options which, when given when opening a block device
6406 * (without a filename), result in a BDS (mostly)
6407 * equalling the given one
6408 * - filename: If exact_filename is set, it is copied here. Otherwise,
6409 * full_open_options is converted to a JSON object, prefixed with
6410 * "json:" (for use through the JSON pseudo protocol) and put here.
6411 */
6412void bdrv_refresh_filename(BlockDriverState *bs)
6413{
6414 BlockDriver *drv = bs->drv;
6415 QDict *opts;
6416
6417 if (!drv) {
6418 return;
6419 }
6420
6421 /* This BDS's file name will most probably depend on its file's name, so
6422 * refresh that first */
6423 if (bs->file) {
6424 bdrv_refresh_filename(bs->file);
6425 }
6426
6427 if (drv->bdrv_refresh_filename) {
6428 /* Obsolete information is of no use here, so drop the old file name
6429 * information before refreshing it */
6430 bs->exact_filename[0] = '\0';
6431 if (bs->full_open_options) {
6432 QDECREF(bs->full_open_options);
6433 bs->full_open_options = NULL;
6434 }
6435
6436 drv->bdrv_refresh_filename(bs);
6437 } else if (bs->file) {
6438 /* Try to reconstruct valid information from the underlying file */
6439 bool has_open_options;
6440
6441 bs->exact_filename[0] = '\0';
6442 if (bs->full_open_options) {
6443 QDECREF(bs->full_open_options);
6444 bs->full_open_options = NULL;
6445 }
6446
6447 opts = qdict_new();
6448 has_open_options = append_open_options(opts, bs);
6449
6450 /* If no specific options have been given for this BDS, the filename of
6451 * the underlying file should suffice for this one as well */
6452 if (bs->file->exact_filename[0] && !has_open_options) {
6453 strcpy(bs->exact_filename, bs->file->exact_filename);
6454 }
6455 /* Reconstructing the full options QDict is simple for most format block
6456 * drivers, as long as the full options are known for the underlying
6457 * file BDS. The full options QDict of that file BDS should somehow
6458 * contain a representation of the filename, therefore the following
6459 * suffices without querying the (exact_)filename of this BDS. */
6460 if (bs->file->full_open_options) {
6461 qdict_put_obj(opts, "driver",
6462 QOBJECT(qstring_from_str(drv->format_name)));
6463 QINCREF(bs->file->full_open_options);
6464 qdict_put_obj(opts, "file", QOBJECT(bs->file->full_open_options));
6465
6466 bs->full_open_options = opts;
6467 } else {
6468 QDECREF(opts);
6469 }
6470 } else if (!bs->full_open_options && qdict_size(bs->options)) {
6471 /* There is no underlying file BDS (at least referenced by BDS.file),
6472 * so the full options QDict should be equal to the options given
6473 * specifically for this block device when it was opened (plus the
6474 * driver specification).
6475 * Because those options don't change, there is no need to update
6476 * full_open_options when it's already set. */
6477
6478 opts = qdict_new();
6479 append_open_options(opts, bs);
6480 qdict_put_obj(opts, "driver",
6481 QOBJECT(qstring_from_str(drv->format_name)));
6482
6483 if (bs->exact_filename[0]) {
6484 /* This may not work for all block protocol drivers (some may
6485 * require this filename to be parsed), but we have to find some
6486 * default solution here, so just include it. If some block driver
6487 * does not support pure options without any filename at all or
6488 * needs some special format of the options QDict, it needs to
6489 * implement the driver-specific bdrv_refresh_filename() function.
6490 */
6491 qdict_put_obj(opts, "filename",
6492 QOBJECT(qstring_from_str(bs->exact_filename)));
6493 }
6494
6495 bs->full_open_options = opts;
6496 }
6497
6498 if (bs->exact_filename[0]) {
6499 pstrcpy(bs->filename, sizeof(bs->filename), bs->exact_filename);
6500 } else if (bs->full_open_options) {
6501 QString *json = qobject_to_json(QOBJECT(bs->full_open_options));
6502 snprintf(bs->filename, sizeof(bs->filename), "json:%s",
6503 qstring_get_str(json));
6504 QDECREF(json);
6505 }
6506}
5366d0c8
BC
6507
6508/* This accessor function purpose is to allow the device models to access the
6509 * BlockAcctStats structure embedded inside a BlockDriverState without being
6510 * aware of the BlockDriverState structure layout.
6511 * It will go away when the BlockAcctStats structure will be moved inside
6512 * the device models.
6513 */
6514BlockAcctStats *bdrv_get_stats(BlockDriverState *bs)
6515{
6516 return &bs->stats;
6517}