]> git.ipfire.org Git - thirdparty/qemu.git/blame - block.c
block: reopen: Document option precedence and refactor accordingly
[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"
d49b6836 29#include "qemu/error-report.h"
1de7afc9 30#include "qemu/module.h"
cc7a8ea7 31#include "qapi/qmp/qerror.h"
7b1b5d19 32#include "qapi/qmp/qjson.h"
bfb197e0 33#include "sysemu/block-backend.h"
9c17d615 34#include "sysemu/sysemu.h"
1de7afc9 35#include "qemu/notify.h"
10817bf0 36#include "qemu/coroutine.h"
c13163fb 37#include "block/qapi.h"
b2023818 38#include "qmp-commands.h"
1de7afc9 39#include "qemu/timer.h"
a5ee7bd4 40#include "qapi-event.h"
db628338 41#include "block/throttle-groups.h"
fc01f7e7 42
71e72a19 43#ifdef CONFIG_BSD
7674e7bf
FB
44#include <sys/types.h>
45#include <sys/stat.h>
46#include <sys/ioctl.h>
72cf2d4f 47#include <sys/queue.h>
c5e97233 48#ifndef __DragonFly__
7674e7bf
FB
49#include <sys/disk.h>
50#endif
c5e97233 51#endif
7674e7bf 52
49dc768d
AL
53#ifdef _WIN32
54#include <windows.h>
55#endif
56
9bd2b08f
JS
57/**
58 * A BdrvDirtyBitmap can be in three possible states:
59 * (1) successor is NULL and disabled is false: full r/w mode
60 * (2) successor is NULL and disabled is true: read only mode ("disabled")
61 * (3) successor is set: frozen mode.
62 * A frozen bitmap cannot be renamed, deleted, anonymized, cleared, set,
63 * or enabled. A frozen bitmap can only abdicate() or reclaim().
64 */
e4654d2d 65struct BdrvDirtyBitmap {
aa0c7ca5
JS
66 HBitmap *bitmap; /* Dirty sector bitmap implementation */
67 BdrvDirtyBitmap *successor; /* Anonymous child; implies frozen status */
68 char *name; /* Optional non-empty unique ID */
69 int64_t size; /* Size of the bitmap (Number of sectors) */
70 bool disabled; /* Bitmap is read-only */
e4654d2d
FZ
71 QLIST_ENTRY(BdrvDirtyBitmap) list;
72};
73
1c9805a3
SH
74#define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
75
c69a4dd8 76struct BdrvStates bdrv_states = QTAILQ_HEAD_INITIALIZER(bdrv_states);
7ee930d0 77
dc364f4c
BC
78static QTAILQ_HEAD(, BlockDriverState) graph_bdrv_states =
79 QTAILQ_HEAD_INITIALIZER(graph_bdrv_states);
80
8a22f02a
SH
81static QLIST_HEAD(, BlockDriver) bdrv_drivers =
82 QLIST_HEAD_INITIALIZER(bdrv_drivers);
ea2384d3 83
f3930ed0
KW
84static int bdrv_open_inherit(BlockDriverState **pbs, const char *filename,
85 const char *reference, QDict *options, int flags,
86 BlockDriverState *parent,
ce343771 87 const BdrvChildRole *child_role, Error **errp);
f3930ed0 88
ce1ffea8 89static void bdrv_dirty_bitmap_truncate(BlockDriverState *bs);
eb852011
MA
90/* If non-zero, use only whitelisted block drivers */
91static int use_bdrv_whitelist;
92
9e0b22f4
SH
93#ifdef _WIN32
94static int is_windows_drive_prefix(const char *filename)
95{
96 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
97 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
98 filename[1] == ':');
99}
100
101int is_windows_drive(const char *filename)
102{
103 if (is_windows_drive_prefix(filename) &&
104 filename[2] == '\0')
105 return 1;
106 if (strstart(filename, "\\\\.\\", NULL) ||
107 strstart(filename, "//./", NULL))
108 return 1;
109 return 0;
110}
111#endif
112
339064d5
KW
113size_t bdrv_opt_mem_align(BlockDriverState *bs)
114{
115 if (!bs || !bs->drv) {
459b4e66
DL
116 /* page size or 4k (hdd sector size) should be on the safe side */
117 return MAX(4096, getpagesize());
339064d5
KW
118 }
119
120 return bs->bl.opt_mem_alignment;
121}
122
4196d2f0
DL
123size_t bdrv_min_mem_align(BlockDriverState *bs)
124{
125 if (!bs || !bs->drv) {
459b4e66
DL
126 /* page size or 4k (hdd sector size) should be on the safe side */
127 return MAX(4096, getpagesize());
4196d2f0
DL
128 }
129
130 return bs->bl.min_mem_alignment;
131}
132
9e0b22f4 133/* check if the path starts with "<protocol>:" */
5c98415b 134int path_has_protocol(const char *path)
9e0b22f4 135{
947995c0
PB
136 const char *p;
137
9e0b22f4
SH
138#ifdef _WIN32
139 if (is_windows_drive(path) ||
140 is_windows_drive_prefix(path)) {
141 return 0;
142 }
947995c0
PB
143 p = path + strcspn(path, ":/\\");
144#else
145 p = path + strcspn(path, ":/");
9e0b22f4
SH
146#endif
147
947995c0 148 return *p == ':';
9e0b22f4
SH
149}
150
83f64091 151int path_is_absolute(const char *path)
3b0d4f61 152{
21664424
FB
153#ifdef _WIN32
154 /* specific case for names like: "\\.\d:" */
f53f4da9 155 if (is_windows_drive(path) || is_windows_drive_prefix(path)) {
21664424 156 return 1;
f53f4da9
PB
157 }
158 return (*path == '/' || *path == '\\');
3b9f94e1 159#else
f53f4da9 160 return (*path == '/');
3b9f94e1 161#endif
3b0d4f61
FB
162}
163
83f64091
FB
164/* if filename is absolute, just copy it to dest. Otherwise, build a
165 path to it by considering it is relative to base_path. URL are
166 supported. */
167void path_combine(char *dest, int dest_size,
168 const char *base_path,
169 const char *filename)
3b0d4f61 170{
83f64091
FB
171 const char *p, *p1;
172 int len;
173
174 if (dest_size <= 0)
175 return;
176 if (path_is_absolute(filename)) {
177 pstrcpy(dest, dest_size, filename);
178 } else {
179 p = strchr(base_path, ':');
180 if (p)
181 p++;
182 else
183 p = base_path;
3b9f94e1
FB
184 p1 = strrchr(base_path, '/');
185#ifdef _WIN32
186 {
187 const char *p2;
188 p2 = strrchr(base_path, '\\');
189 if (!p1 || p2 > p1)
190 p1 = p2;
191 }
192#endif
83f64091
FB
193 if (p1)
194 p1++;
195 else
196 p1 = base_path;
197 if (p1 > p)
198 p = p1;
199 len = p - base_path;
200 if (len > dest_size - 1)
201 len = dest_size - 1;
202 memcpy(dest, base_path, len);
203 dest[len] = '\0';
204 pstrcat(dest, dest_size, filename);
3b0d4f61 205 }
3b0d4f61
FB
206}
207
0a82855a
HR
208void bdrv_get_full_backing_filename_from_filename(const char *backed,
209 const char *backing,
9f07429e
HR
210 char *dest, size_t sz,
211 Error **errp)
dc5a1371 212{
9f07429e
HR
213 if (backing[0] == '\0' || path_has_protocol(backing) ||
214 path_is_absolute(backing))
215 {
0a82855a 216 pstrcpy(dest, sz, backing);
9f07429e
HR
217 } else if (backed[0] == '\0' || strstart(backed, "json:", NULL)) {
218 error_setg(errp, "Cannot use relative backing file names for '%s'",
219 backed);
dc5a1371 220 } else {
0a82855a 221 path_combine(dest, sz, backed, backing);
dc5a1371
PB
222 }
223}
224
9f07429e
HR
225void bdrv_get_full_backing_filename(BlockDriverState *bs, char *dest, size_t sz,
226 Error **errp)
0a82855a 227{
9f07429e
HR
228 char *backed = bs->exact_filename[0] ? bs->exact_filename : bs->filename;
229
230 bdrv_get_full_backing_filename_from_filename(backed, bs->backing_file,
231 dest, sz, errp);
0a82855a
HR
232}
233
0eb7217e
SH
234void bdrv_register(BlockDriver *bdrv)
235{
236 bdrv_setup_io_funcs(bdrv);
b2e12bc6 237
8a22f02a 238 QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
ea2384d3 239}
b338082b 240
7f06d47e 241BlockDriverState *bdrv_new_root(void)
b338082b 242{
7f06d47e 243 BlockDriverState *bs = bdrv_new();
e4e9986b 244
e4e9986b 245 QTAILQ_INSERT_TAIL(&bdrv_states, bs, device_list);
e4e9986b
MA
246 return bs;
247}
248
249BlockDriverState *bdrv_new(void)
250{
251 BlockDriverState *bs;
252 int i;
253
5839e53b 254 bs = g_new0(BlockDriverState, 1);
e4654d2d 255 QLIST_INIT(&bs->dirty_bitmaps);
fbe40ff7
FZ
256 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
257 QLIST_INIT(&bs->op_blockers[i]);
258 }
d7d512f6 259 notifier_list_init(&bs->close_notifiers);
d616b224 260 notifier_with_return_list_init(&bs->before_write_notifiers);
cc0681c4
BC
261 qemu_co_queue_init(&bs->throttled_reqs[0]);
262 qemu_co_queue_init(&bs->throttled_reqs[1]);
9fcb0251 263 bs->refcnt = 1;
dcd04228 264 bs->aio_context = qemu_get_aio_context();
d7d512f6 265
b338082b
FB
266 return bs;
267}
268
d7d512f6
PB
269void bdrv_add_close_notifier(BlockDriverState *bs, Notifier *notify)
270{
271 notifier_list_add(&bs->close_notifiers, notify);
272}
273
ea2384d3
FB
274BlockDriver *bdrv_find_format(const char *format_name)
275{
276 BlockDriver *drv1;
8a22f02a
SH
277 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
278 if (!strcmp(drv1->format_name, format_name)) {
ea2384d3 279 return drv1;
8a22f02a 280 }
ea2384d3
FB
281 }
282 return NULL;
283}
284
b64ec4e4 285static int bdrv_is_whitelisted(BlockDriver *drv, bool read_only)
eb852011 286{
b64ec4e4
FZ
287 static const char *whitelist_rw[] = {
288 CONFIG_BDRV_RW_WHITELIST
289 };
290 static const char *whitelist_ro[] = {
291 CONFIG_BDRV_RO_WHITELIST
eb852011
MA
292 };
293 const char **p;
294
b64ec4e4 295 if (!whitelist_rw[0] && !whitelist_ro[0]) {
eb852011 296 return 1; /* no whitelist, anything goes */
b64ec4e4 297 }
eb852011 298
b64ec4e4 299 for (p = whitelist_rw; *p; p++) {
eb852011
MA
300 if (!strcmp(drv->format_name, *p)) {
301 return 1;
302 }
303 }
b64ec4e4
FZ
304 if (read_only) {
305 for (p = whitelist_ro; *p; p++) {
306 if (!strcmp(drv->format_name, *p)) {
307 return 1;
308 }
309 }
310 }
eb852011
MA
311 return 0;
312}
313
5b7e1542
ZYW
314typedef struct CreateCo {
315 BlockDriver *drv;
316 char *filename;
83d0521a 317 QemuOpts *opts;
5b7e1542 318 int ret;
cc84d90f 319 Error *err;
5b7e1542
ZYW
320} CreateCo;
321
322static void coroutine_fn bdrv_create_co_entry(void *opaque)
323{
cc84d90f
HR
324 Error *local_err = NULL;
325 int ret;
326
5b7e1542
ZYW
327 CreateCo *cco = opaque;
328 assert(cco->drv);
329
c282e1fd 330 ret = cco->drv->bdrv_create(cco->filename, cco->opts, &local_err);
84d18f06 331 if (local_err) {
cc84d90f
HR
332 error_propagate(&cco->err, local_err);
333 }
334 cco->ret = ret;
5b7e1542
ZYW
335}
336
0e7e1989 337int bdrv_create(BlockDriver *drv, const char* filename,
83d0521a 338 QemuOpts *opts, Error **errp)
ea2384d3 339{
5b7e1542
ZYW
340 int ret;
341
342 Coroutine *co;
343 CreateCo cco = {
344 .drv = drv,
345 .filename = g_strdup(filename),
83d0521a 346 .opts = opts,
5b7e1542 347 .ret = NOT_DONE,
cc84d90f 348 .err = NULL,
5b7e1542
ZYW
349 };
350
c282e1fd 351 if (!drv->bdrv_create) {
cc84d90f 352 error_setg(errp, "Driver '%s' does not support image creation", drv->format_name);
80168bff
LC
353 ret = -ENOTSUP;
354 goto out;
5b7e1542
ZYW
355 }
356
357 if (qemu_in_coroutine()) {
358 /* Fast-path if already in coroutine context */
359 bdrv_create_co_entry(&cco);
360 } else {
361 co = qemu_coroutine_create(bdrv_create_co_entry);
362 qemu_coroutine_enter(co, &cco);
363 while (cco.ret == NOT_DONE) {
b47ec2c4 364 aio_poll(qemu_get_aio_context(), true);
5b7e1542
ZYW
365 }
366 }
367
368 ret = cco.ret;
cc84d90f 369 if (ret < 0) {
84d18f06 370 if (cco.err) {
cc84d90f
HR
371 error_propagate(errp, cco.err);
372 } else {
373 error_setg_errno(errp, -ret, "Could not create image");
374 }
375 }
0e7e1989 376
80168bff
LC
377out:
378 g_free(cco.filename);
5b7e1542 379 return ret;
ea2384d3
FB
380}
381
c282e1fd 382int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp)
84a12e66
CH
383{
384 BlockDriver *drv;
cc84d90f
HR
385 Error *local_err = NULL;
386 int ret;
84a12e66 387
b65a5e12 388 drv = bdrv_find_protocol(filename, true, errp);
84a12e66 389 if (drv == NULL) {
16905d71 390 return -ENOENT;
84a12e66
CH
391 }
392
c282e1fd 393 ret = bdrv_create(drv, filename, opts, &local_err);
84d18f06 394 if (local_err) {
cc84d90f
HR
395 error_propagate(errp, local_err);
396 }
397 return ret;
84a12e66
CH
398}
399
892b7de8
ET
400/**
401 * Try to get @bs's logical and physical block size.
402 * On success, store them in @bsz struct and return 0.
403 * On failure return -errno.
404 * @bs must not be empty.
405 */
406int bdrv_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz)
407{
408 BlockDriver *drv = bs->drv;
409
410 if (drv && drv->bdrv_probe_blocksizes) {
411 return drv->bdrv_probe_blocksizes(bs, bsz);
412 }
413
414 return -ENOTSUP;
415}
416
417/**
418 * Try to get @bs's geometry (cyls, heads, sectors).
419 * On success, store them in @geo struct and return 0.
420 * On failure return -errno.
421 * @bs must not be empty.
422 */
423int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo)
424{
425 BlockDriver *drv = bs->drv;
426
427 if (drv && drv->bdrv_probe_geometry) {
428 return drv->bdrv_probe_geometry(bs, geo);
429 }
430
431 return -ENOTSUP;
432}
433
eba25057
JM
434/*
435 * Create a uniquely-named empty temporary file.
436 * Return 0 upon success, otherwise a negative errno value.
437 */
438int get_tmp_filename(char *filename, int size)
d5249393 439{
eba25057 440#ifdef _WIN32
3b9f94e1 441 char temp_dir[MAX_PATH];
eba25057
JM
442 /* GetTempFileName requires that its output buffer (4th param)
443 have length MAX_PATH or greater. */
444 assert(size >= MAX_PATH);
445 return (GetTempPath(MAX_PATH, temp_dir)
446 && GetTempFileName(temp_dir, "qem", 0, filename)
447 ? 0 : -GetLastError());
d5249393 448#else
67b915a5 449 int fd;
7ccfb2eb 450 const char *tmpdir;
0badc1ee 451 tmpdir = getenv("TMPDIR");
69bef793
AS
452 if (!tmpdir) {
453 tmpdir = "/var/tmp";
454 }
eba25057
JM
455 if (snprintf(filename, size, "%s/vl.XXXXXX", tmpdir) >= size) {
456 return -EOVERFLOW;
457 }
ea2384d3 458 fd = mkstemp(filename);
fe235a06
DH
459 if (fd < 0) {
460 return -errno;
461 }
462 if (close(fd) != 0) {
463 unlink(filename);
eba25057
JM
464 return -errno;
465 }
466 return 0;
d5249393 467#endif
eba25057 468}
fc01f7e7 469
84a12e66
CH
470/*
471 * Detect host devices. By convention, /dev/cdrom[N] is always
472 * recognized as a host CDROM.
473 */
474static BlockDriver *find_hdev_driver(const char *filename)
475{
476 int score_max = 0, score;
477 BlockDriver *drv = NULL, *d;
478
479 QLIST_FOREACH(d, &bdrv_drivers, list) {
480 if (d->bdrv_probe_device) {
481 score = d->bdrv_probe_device(filename);
482 if (score > score_max) {
483 score_max = score;
484 drv = d;
485 }
486 }
487 }
488
489 return drv;
490}
491
98289620 492BlockDriver *bdrv_find_protocol(const char *filename,
b65a5e12
HR
493 bool allow_protocol_prefix,
494 Error **errp)
83f64091
FB
495{
496 BlockDriver *drv1;
497 char protocol[128];
1cec71e3 498 int len;
83f64091 499 const char *p;
19cb3738 500
66f82cee
KW
501 /* TODO Drivers without bdrv_file_open must be specified explicitly */
502
39508e7a
CH
503 /*
504 * XXX(hch): we really should not let host device detection
505 * override an explicit protocol specification, but moving this
506 * later breaks access to device names with colons in them.
507 * Thanks to the brain-dead persistent naming schemes on udev-
508 * based Linux systems those actually are quite common.
509 */
510 drv1 = find_hdev_driver(filename);
511 if (drv1) {
512 return drv1;
513 }
514
98289620 515 if (!path_has_protocol(filename) || !allow_protocol_prefix) {
ef810437 516 return &bdrv_file;
84a12e66 517 }
98289620 518
9e0b22f4
SH
519 p = strchr(filename, ':');
520 assert(p != NULL);
1cec71e3
AL
521 len = p - filename;
522 if (len > sizeof(protocol) - 1)
523 len = sizeof(protocol) - 1;
524 memcpy(protocol, filename, len);
525 protocol[len] = '\0';
8a22f02a 526 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
5fafdf24 527 if (drv1->protocol_name &&
8a22f02a 528 !strcmp(drv1->protocol_name, protocol)) {
83f64091 529 return drv1;
8a22f02a 530 }
83f64091 531 }
b65a5e12
HR
532
533 error_setg(errp, "Unknown protocol '%s'", protocol);
83f64091
FB
534 return NULL;
535}
536
c6684249
MA
537/*
538 * Guess image format by probing its contents.
539 * This is not a good idea when your image is raw (CVE-2008-2004), but
540 * we do it anyway for backward compatibility.
541 *
542 * @buf contains the image's first @buf_size bytes.
7cddd372
KW
543 * @buf_size is the buffer size in bytes (generally BLOCK_PROBE_BUF_SIZE,
544 * but can be smaller if the image file is smaller)
c6684249
MA
545 * @filename is its filename.
546 *
547 * For all block drivers, call the bdrv_probe() method to get its
548 * probing score.
549 * Return the first block driver with the highest probing score.
550 */
38f3ef57
KW
551BlockDriver *bdrv_probe_all(const uint8_t *buf, int buf_size,
552 const char *filename)
c6684249
MA
553{
554 int score_max = 0, score;
555 BlockDriver *drv = NULL, *d;
556
557 QLIST_FOREACH(d, &bdrv_drivers, list) {
558 if (d->bdrv_probe) {
559 score = d->bdrv_probe(buf, buf_size, filename);
560 if (score > score_max) {
561 score_max = score;
562 drv = d;
563 }
564 }
565 }
566
567 return drv;
568}
569
f500a6d3 570static int find_image_format(BlockDriverState *bs, const char *filename,
34b5d2c6 571 BlockDriver **pdrv, Error **errp)
f3a5d3f8 572{
c6684249 573 BlockDriver *drv;
7cddd372 574 uint8_t buf[BLOCK_PROBE_BUF_SIZE];
f500a6d3 575 int ret = 0;
f8ea0b00 576
08a00559 577 /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
b192af8a 578 if (bdrv_is_sg(bs) || !bdrv_is_inserted(bs) || bdrv_getlength(bs) == 0) {
ef810437 579 *pdrv = &bdrv_raw;
c98ac35d 580 return ret;
1a396859 581 }
f8ea0b00 582
83f64091 583 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
83f64091 584 if (ret < 0) {
34b5d2c6
HR
585 error_setg_errno(errp, -ret, "Could not read image for determining its "
586 "format");
c98ac35d
SW
587 *pdrv = NULL;
588 return ret;
83f64091
FB
589 }
590
c6684249 591 drv = bdrv_probe_all(buf, ret, filename);
c98ac35d 592 if (!drv) {
34b5d2c6
HR
593 error_setg(errp, "Could not determine image format: No compatible "
594 "driver found");
c98ac35d
SW
595 ret = -ENOENT;
596 }
597 *pdrv = drv;
598 return ret;
ea2384d3
FB
599}
600
51762288
SH
601/**
602 * Set the current 'total_sectors' value
65a9bb25 603 * Return 0 on success, -errno on error.
51762288
SH
604 */
605static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
606{
607 BlockDriver *drv = bs->drv;
608
396759ad 609 /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
b192af8a 610 if (bdrv_is_sg(bs))
396759ad
NB
611 return 0;
612
51762288
SH
613 /* query actual device if possible, otherwise just trust the hint */
614 if (drv->bdrv_getlength) {
615 int64_t length = drv->bdrv_getlength(bs);
616 if (length < 0) {
617 return length;
618 }
7e382003 619 hint = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE);
51762288
SH
620 }
621
622 bs->total_sectors = hint;
623 return 0;
624}
625
cddff5ba
KW
626/**
627 * Combines a QDict of new block driver @options with any missing options taken
628 * from @old_options, so that leaving out an option defaults to its old value.
629 */
630static void bdrv_join_options(BlockDriverState *bs, QDict *options,
631 QDict *old_options)
632{
633 if (bs->drv && bs->drv->bdrv_join_options) {
634 bs->drv->bdrv_join_options(options, old_options);
635 } else {
636 qdict_join(options, old_options, false);
637 }
638}
639
9e8f1835
PB
640/**
641 * Set open flags for a given discard mode
642 *
643 * Return 0 on success, -1 if the discard mode was invalid.
644 */
645int bdrv_parse_discard_flags(const char *mode, int *flags)
646{
647 *flags &= ~BDRV_O_UNMAP;
648
649 if (!strcmp(mode, "off") || !strcmp(mode, "ignore")) {
650 /* do nothing */
651 } else if (!strcmp(mode, "on") || !strcmp(mode, "unmap")) {
652 *flags |= BDRV_O_UNMAP;
653 } else {
654 return -1;
655 }
656
657 return 0;
658}
659
c3993cdc
SH
660/**
661 * Set open flags for a given cache mode
662 *
663 * Return 0 on success, -1 if the cache mode was invalid.
664 */
665int bdrv_parse_cache_flags(const char *mode, int *flags)
666{
667 *flags &= ~BDRV_O_CACHE_MASK;
668
669 if (!strcmp(mode, "off") || !strcmp(mode, "none")) {
670 *flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
92196b2f
SH
671 } else if (!strcmp(mode, "directsync")) {
672 *flags |= BDRV_O_NOCACHE;
c3993cdc
SH
673 } else if (!strcmp(mode, "writeback")) {
674 *flags |= BDRV_O_CACHE_WB;
675 } else if (!strcmp(mode, "unsafe")) {
676 *flags |= BDRV_O_CACHE_WB;
677 *flags |= BDRV_O_NO_FLUSH;
678 } else if (!strcmp(mode, "writethrough")) {
679 /* this is the default */
680 } else {
681 return -1;
682 }
683
684 return 0;
685}
686
b1e6fc08
KW
687/*
688 * Returns the flags that a temporary snapshot should get, based on the
689 * originally requested flags (the originally requested image will have flags
690 * like a backing file)
691 */
692static int bdrv_temp_snapshot_flags(int flags)
693{
694 return (flags & ~BDRV_O_SNAPSHOT) | BDRV_O_TEMPORARY;
695}
696
0b50cc88 697/*
f3930ed0
KW
698 * Returns the flags that bs->file should get if a protocol driver is expected,
699 * based on the given flags for the parent BDS
0b50cc88
KW
700 */
701static int bdrv_inherited_flags(int flags)
702{
703 /* Enable protocol handling, disable format probing for bs->file */
704 flags |= BDRV_O_PROTOCOL;
705
706 /* Our block drivers take care to send flushes and respect unmap policy,
707 * so we can enable both unconditionally on lower layers. */
708 flags |= BDRV_O_CACHE_WB | BDRV_O_UNMAP;
709
0b50cc88 710 /* Clear flags that only apply to the top layer */
5669b44d 711 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ);
0b50cc88
KW
712
713 return flags;
714}
715
f3930ed0
KW
716const BdrvChildRole child_file = {
717 .inherit_flags = bdrv_inherited_flags,
718};
719
720/*
721 * Returns the flags that bs->file should get if the use of formats (and not
722 * only protocols) is permitted for it, based on the given flags for the parent
723 * BDS
724 */
725static int bdrv_inherited_fmt_flags(int parent_flags)
726{
727 int flags = child_file.inherit_flags(parent_flags);
728 return flags & ~BDRV_O_PROTOCOL;
729}
730
731const BdrvChildRole child_format = {
732 .inherit_flags = bdrv_inherited_fmt_flags,
733};
734
317fc44e 735/*
760e0063 736 * Returns the flags that bs->backing should get, based on the given flags
317fc44e
KW
737 * for the parent BDS
738 */
739static int bdrv_backing_flags(int flags)
740{
741 /* backing files always opened read-only */
742 flags &= ~(BDRV_O_RDWR | BDRV_O_COPY_ON_READ);
743
744 /* snapshot=on is handled on the top layer */
8bfea15d 745 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_TEMPORARY);
317fc44e
KW
746
747 return flags;
748}
749
f3930ed0
KW
750static const BdrvChildRole child_backing = {
751 .inherit_flags = bdrv_backing_flags,
752};
753
7b272452
KW
754static int bdrv_open_flags(BlockDriverState *bs, int flags)
755{
756 int open_flags = flags | BDRV_O_CACHE_WB;
757
758 /*
759 * Clear flags that are internal to the block layer before opening the
760 * image.
761 */
20cca275 762 open_flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_PROTOCOL);
7b272452
KW
763
764 /*
765 * Snapshots should be writable.
766 */
8bfea15d 767 if (flags & BDRV_O_TEMPORARY) {
7b272452
KW
768 open_flags |= BDRV_O_RDWR;
769 }
770
771 return open_flags;
772}
773
636ea370
KW
774static void bdrv_assign_node_name(BlockDriverState *bs,
775 const char *node_name,
776 Error **errp)
6913c0c2 777{
15489c76 778 char *gen_node_name = NULL;
6913c0c2 779
15489c76
JC
780 if (!node_name) {
781 node_name = gen_node_name = id_generate(ID_BLOCK);
782 } else if (!id_wellformed(node_name)) {
783 /*
784 * Check for empty string or invalid characters, but not if it is
785 * generated (generated names use characters not available to the user)
786 */
9aebf3b8 787 error_setg(errp, "Invalid node name");
636ea370 788 return;
6913c0c2
BC
789 }
790
0c5e94ee 791 /* takes care of avoiding namespaces collisions */
7f06d47e 792 if (blk_by_name(node_name)) {
0c5e94ee
BC
793 error_setg(errp, "node-name=%s is conflicting with a device id",
794 node_name);
15489c76 795 goto out;
0c5e94ee
BC
796 }
797
6913c0c2
BC
798 /* takes care of avoiding duplicates node names */
799 if (bdrv_find_node(node_name)) {
800 error_setg(errp, "Duplicate node name");
15489c76 801 goto out;
6913c0c2
BC
802 }
803
804 /* copy node name into the bs and insert it into the graph list */
805 pstrcpy(bs->node_name, sizeof(bs->node_name), node_name);
806 QTAILQ_INSERT_TAIL(&graph_bdrv_states, bs, node_list);
15489c76
JC
807out:
808 g_free(gen_node_name);
6913c0c2
BC
809}
810
18edf289
KW
811static QemuOptsList bdrv_runtime_opts = {
812 .name = "bdrv_common",
813 .head = QTAILQ_HEAD_INITIALIZER(bdrv_runtime_opts.head),
814 .desc = {
815 {
816 .name = "node-name",
817 .type = QEMU_OPT_STRING,
818 .help = "Node name of the block device node",
819 },
62392ebb
KW
820 {
821 .name = "driver",
822 .type = QEMU_OPT_STRING,
823 .help = "Block driver to use for the node",
824 },
18edf289
KW
825 { /* end of list */ }
826 },
827};
828
57915332
KW
829/*
830 * Common part for opening disk images and files
b6ad491a
KW
831 *
832 * Removes all processed options from *options.
57915332 833 */
9a4f4c31 834static int bdrv_open_common(BlockDriverState *bs, BdrvChild *file,
62392ebb 835 QDict *options, int flags, Error **errp)
57915332
KW
836{
837 int ret, open_flags;
035fccdf 838 const char *filename;
62392ebb 839 const char *driver_name = NULL;
6913c0c2 840 const char *node_name = NULL;
18edf289 841 QemuOpts *opts;
62392ebb 842 BlockDriver *drv;
34b5d2c6 843 Error *local_err = NULL;
57915332 844
6405875c 845 assert(bs->file == NULL);
707ff828 846 assert(options != NULL && bs->options != options);
57915332 847
62392ebb
KW
848 opts = qemu_opts_create(&bdrv_runtime_opts, NULL, 0, &error_abort);
849 qemu_opts_absorb_qdict(opts, options, &local_err);
850 if (local_err) {
851 error_propagate(errp, local_err);
852 ret = -EINVAL;
853 goto fail_opts;
854 }
855
856 driver_name = qemu_opt_get(opts, "driver");
857 drv = bdrv_find_format(driver_name);
858 assert(drv != NULL);
859
45673671 860 if (file != NULL) {
9a4f4c31 861 filename = file->bs->filename;
45673671
KW
862 } else {
863 filename = qdict_get_try_str(options, "filename");
864 }
865
765003db
KW
866 if (drv->bdrv_needs_filename && !filename) {
867 error_setg(errp, "The '%s' block driver requires a file name",
868 drv->format_name);
18edf289
KW
869 ret = -EINVAL;
870 goto fail_opts;
6913c0c2 871 }
6913c0c2 872
62392ebb
KW
873 trace_bdrv_open_common(bs, filename ?: "", flags, drv->format_name);
874
18edf289 875 node_name = qemu_opt_get(opts, "node-name");
636ea370 876 bdrv_assign_node_name(bs, node_name, &local_err);
0fb6395c 877 if (local_err) {
636ea370 878 error_propagate(errp, local_err);
18edf289
KW
879 ret = -EINVAL;
880 goto fail_opts;
5d186eb0
KW
881 }
882
c25f53b0 883 bs->request_alignment = 512;
0d51b4de 884 bs->zero_beyond_eof = true;
b64ec4e4
FZ
885 open_flags = bdrv_open_flags(bs, flags);
886 bs->read_only = !(open_flags & BDRV_O_RDWR);
887
888 if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, bs->read_only)) {
8f94a6e4
KW
889 error_setg(errp,
890 !bs->read_only && bdrv_is_whitelisted(drv, true)
891 ? "Driver '%s' can only be used for read-only devices"
892 : "Driver '%s' is not whitelisted",
893 drv->format_name);
18edf289
KW
894 ret = -ENOTSUP;
895 goto fail_opts;
b64ec4e4 896 }
57915332 897
53fec9d3 898 assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */
0ebd24e0
KW
899 if (flags & BDRV_O_COPY_ON_READ) {
900 if (!bs->read_only) {
901 bdrv_enable_copy_on_read(bs);
902 } else {
903 error_setg(errp, "Can't use copy-on-read on read-only device");
18edf289
KW
904 ret = -EINVAL;
905 goto fail_opts;
0ebd24e0 906 }
53fec9d3
SH
907 }
908
c2ad1b0c
KW
909 if (filename != NULL) {
910 pstrcpy(bs->filename, sizeof(bs->filename), filename);
911 } else {
912 bs->filename[0] = '\0';
913 }
91af7014 914 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), bs->filename);
57915332 915
57915332 916 bs->drv = drv;
7267c094 917 bs->opaque = g_malloc0(drv->instance_size);
57915332 918
03f541bd 919 bs->enable_write_cache = !!(flags & BDRV_O_CACHE_WB);
e7c63796 920
66f82cee
KW
921 /* Open the image, either directly or using a protocol */
922 if (drv->bdrv_file_open) {
5d186eb0 923 assert(file == NULL);
030be321 924 assert(!drv->bdrv_needs_filename || filename != NULL);
34b5d2c6 925 ret = drv->bdrv_file_open(bs, options, open_flags, &local_err);
f500a6d3 926 } else {
2af5ef70 927 if (file == NULL) {
34b5d2c6
HR
928 error_setg(errp, "Can't use '%s' as a block driver for the "
929 "protocol level", drv->format_name);
2af5ef70
KW
930 ret = -EINVAL;
931 goto free_and_fail;
932 }
f500a6d3 933 bs->file = file;
34b5d2c6 934 ret = drv->bdrv_open(bs, options, open_flags, &local_err);
66f82cee
KW
935 }
936
57915332 937 if (ret < 0) {
84d18f06 938 if (local_err) {
34b5d2c6 939 error_propagate(errp, local_err);
2fa9aa59
DH
940 } else if (bs->filename[0]) {
941 error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename);
34b5d2c6
HR
942 } else {
943 error_setg_errno(errp, -ret, "Could not open image");
944 }
57915332
KW
945 goto free_and_fail;
946 }
947
a1f688f4
MA
948 if (bs->encrypted) {
949 error_report("Encrypted images are deprecated");
950 error_printf("Support for them will be removed in a future release.\n"
951 "You can use 'qemu-img convert' to convert your image"
952 " to an unencrypted one.\n");
953 }
954
51762288
SH
955 ret = refresh_total_sectors(bs, bs->total_sectors);
956 if (ret < 0) {
34b5d2c6 957 error_setg_errno(errp, -ret, "Could not refresh total sector count");
51762288 958 goto free_and_fail;
57915332 959 }
51762288 960
3baca891
KW
961 bdrv_refresh_limits(bs, &local_err);
962 if (local_err) {
963 error_propagate(errp, local_err);
964 ret = -EINVAL;
965 goto free_and_fail;
966 }
967
c25f53b0 968 assert(bdrv_opt_mem_align(bs) != 0);
4196d2f0 969 assert(bdrv_min_mem_align(bs) != 0);
b192af8a 970 assert((bs->request_alignment != 0) || bdrv_is_sg(bs));
18edf289
KW
971
972 qemu_opts_del(opts);
57915332
KW
973 return 0;
974
975free_and_fail:
f500a6d3 976 bs->file = NULL;
7267c094 977 g_free(bs->opaque);
57915332
KW
978 bs->opaque = NULL;
979 bs->drv = NULL;
18edf289
KW
980fail_opts:
981 qemu_opts_del(opts);
57915332
KW
982 return ret;
983}
984
5e5c4f63
KW
985static QDict *parse_json_filename(const char *filename, Error **errp)
986{
987 QObject *options_obj;
988 QDict *options;
989 int ret;
990
991 ret = strstart(filename, "json:", &filename);
992 assert(ret);
993
994 options_obj = qobject_from_json(filename);
995 if (!options_obj) {
996 error_setg(errp, "Could not parse the JSON options");
997 return NULL;
998 }
999
1000 if (qobject_type(options_obj) != QTYPE_QDICT) {
1001 qobject_decref(options_obj);
1002 error_setg(errp, "Invalid JSON object given");
1003 return NULL;
1004 }
1005
1006 options = qobject_to_qdict(options_obj);
1007 qdict_flatten(options);
1008
1009 return options;
1010}
1011
b6ce07aa 1012/*
f54120ff
KW
1013 * Fills in default options for opening images and converts the legacy
1014 * filename/flags pair to option QDict entries.
53a29513
HR
1015 * The BDRV_O_PROTOCOL flag in *flags will be set or cleared accordingly if a
1016 * block driver has been specified explicitly.
b6ce07aa 1017 */
53a29513 1018static int bdrv_fill_options(QDict **options, const char **pfilename,
053e1578 1019 int *flags, Error **errp)
ea2384d3 1020{
5e5c4f63 1021 const char *filename = *pfilename;
c2ad1b0c 1022 const char *drvname;
53a29513 1023 bool protocol = *flags & BDRV_O_PROTOCOL;
e3fa4bfa 1024 bool parse_filename = false;
053e1578 1025 BlockDriver *drv = NULL;
34b5d2c6 1026 Error *local_err = NULL;
83f64091 1027
5e5c4f63
KW
1028 /* Parse json: pseudo-protocol */
1029 if (filename && g_str_has_prefix(filename, "json:")) {
1030 QDict *json_options = parse_json_filename(filename, &local_err);
1031 if (local_err) {
1032 error_propagate(errp, local_err);
1033 return -EINVAL;
1034 }
1035
1036 /* Options given in the filename have lower priority than options
1037 * specified directly */
1038 qdict_join(*options, json_options, false);
1039 QDECREF(json_options);
1040 *pfilename = filename = NULL;
1041 }
1042
53a29513 1043 drvname = qdict_get_try_str(*options, "driver");
053e1578
HR
1044 if (drvname) {
1045 drv = bdrv_find_format(drvname);
1046 if (!drv) {
1047 error_setg(errp, "Unknown driver '%s'", drvname);
1048 return -ENOENT;
1049 }
1050 /* If the user has explicitly specified the driver, this choice should
1051 * override the BDRV_O_PROTOCOL flag */
1052 protocol = drv->bdrv_file_open;
53a29513
HR
1053 }
1054
1055 if (protocol) {
1056 *flags |= BDRV_O_PROTOCOL;
1057 } else {
1058 *flags &= ~BDRV_O_PROTOCOL;
1059 }
1060
035fccdf 1061 /* Fetch the file name from the options QDict if necessary */
17b005f1 1062 if (protocol && filename) {
f54120ff
KW
1063 if (!qdict_haskey(*options, "filename")) {
1064 qdict_put(*options, "filename", qstring_from_str(filename));
1065 parse_filename = true;
1066 } else {
1067 error_setg(errp, "Can't specify 'file' and 'filename' options at "
1068 "the same time");
1069 return -EINVAL;
1070 }
035fccdf
KW
1071 }
1072
c2ad1b0c 1073 /* Find the right block driver */
f54120ff 1074 filename = qdict_get_try_str(*options, "filename");
f54120ff 1075
053e1578
HR
1076 if (!drvname && protocol) {
1077 if (filename) {
1078 drv = bdrv_find_protocol(filename, parse_filename, errp);
17b005f1 1079 if (!drv) {
053e1578 1080 return -EINVAL;
17b005f1 1081 }
053e1578
HR
1082
1083 drvname = drv->format_name;
1084 qdict_put(*options, "driver", qstring_from_str(drvname));
1085 } else {
1086 error_setg(errp, "Must specify either driver or file");
1087 return -EINVAL;
98289620 1088 }
c2ad1b0c
KW
1089 }
1090
17b005f1 1091 assert(drv || !protocol);
c2ad1b0c 1092
f54120ff 1093 /* Driver-specific filename parsing */
17b005f1 1094 if (drv && drv->bdrv_parse_filename && parse_filename) {
5acd9d81 1095 drv->bdrv_parse_filename(filename, *options, &local_err);
84d18f06 1096 if (local_err) {
34b5d2c6 1097 error_propagate(errp, local_err);
f54120ff 1098 return -EINVAL;
6963a30d 1099 }
cd5d031e
HR
1100
1101 if (!drv->bdrv_needs_filename) {
1102 qdict_del(*options, "filename");
cd5d031e 1103 }
6963a30d
KW
1104 }
1105
d44f928a
HR
1106 if (runstate_check(RUN_STATE_INMIGRATE)) {
1107 *flags |= BDRV_O_INCOMING;
1108 }
1109
f54120ff
KW
1110 return 0;
1111}
1112
b4b059f6
KW
1113static BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs,
1114 BlockDriverState *child_bs,
260fecf1 1115 const char *child_name,
b4b059f6 1116 const BdrvChildRole *child_role)
df581792
KW
1117{
1118 BdrvChild *child = g_new(BdrvChild, 1);
1119 *child = (BdrvChild) {
1120 .bs = child_bs,
260fecf1 1121 .name = g_strdup(child_name),
df581792
KW
1122 .role = child_role,
1123 };
1124
1125 QLIST_INSERT_HEAD(&parent_bs->children, child, next);
d42a8a93 1126 QLIST_INSERT_HEAD(&child_bs->parents, child, next_parent);
b4b059f6
KW
1127
1128 return child;
df581792
KW
1129}
1130
3f09bfbc 1131static void bdrv_detach_child(BdrvChild *child)
33a60407
KW
1132{
1133 QLIST_REMOVE(child, next);
d42a8a93 1134 QLIST_REMOVE(child, next_parent);
260fecf1 1135 g_free(child->name);
33a60407
KW
1136 g_free(child);
1137}
1138
1139void bdrv_unref_child(BlockDriverState *parent, BdrvChild *child)
1140{
779020cb
KW
1141 BlockDriverState *child_bs;
1142
1143 if (child == NULL) {
1144 return;
1145 }
33a60407
KW
1146
1147 if (child->bs->inherits_from == parent) {
1148 child->bs->inherits_from = NULL;
1149 }
1150
779020cb 1151 child_bs = child->bs;
33a60407
KW
1152 bdrv_detach_child(child);
1153 bdrv_unref(child_bs);
1154}
1155
5db15a57
KW
1156/*
1157 * Sets the backing file link of a BDS. A new reference is created; callers
1158 * which don't need their own reference any more must call bdrv_unref().
1159 */
8d24cce1
FZ
1160void bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd)
1161{
5db15a57
KW
1162 if (backing_hd) {
1163 bdrv_ref(backing_hd);
1164 }
8d24cce1 1165
760e0063 1166 if (bs->backing) {
826b6ca0 1167 assert(bs->backing_blocker);
760e0063 1168 bdrv_op_unblock_all(bs->backing->bs, bs->backing_blocker);
5db15a57 1169 bdrv_unref_child(bs, bs->backing);
826b6ca0
FZ
1170 } else if (backing_hd) {
1171 error_setg(&bs->backing_blocker,
81e5f78a
AG
1172 "node is used as backing hd of '%s'",
1173 bdrv_get_device_or_node_name(bs));
826b6ca0
FZ
1174 }
1175
8d24cce1 1176 if (!backing_hd) {
826b6ca0
FZ
1177 error_free(bs->backing_blocker);
1178 bs->backing_blocker = NULL;
760e0063 1179 bs->backing = NULL;
8d24cce1
FZ
1180 goto out;
1181 }
260fecf1 1182 bs->backing = bdrv_attach_child(bs, backing_hd, "backing", &child_backing);
8d24cce1
FZ
1183 bs->open_flags &= ~BDRV_O_NO_BACKING;
1184 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_hd->filename);
1185 pstrcpy(bs->backing_format, sizeof(bs->backing_format),
1186 backing_hd->drv ? backing_hd->drv->format_name : "");
826b6ca0 1187
760e0063 1188 bdrv_op_block_all(backing_hd, bs->backing_blocker);
826b6ca0 1189 /* Otherwise we won't be able to commit due to check in bdrv_commit */
760e0063 1190 bdrv_op_unblock(backing_hd, BLOCK_OP_TYPE_COMMIT_TARGET,
826b6ca0 1191 bs->backing_blocker);
8d24cce1 1192out:
3baca891 1193 bdrv_refresh_limits(bs, NULL);
8d24cce1
FZ
1194}
1195
31ca6d07
KW
1196/*
1197 * Opens the backing file for a BlockDriverState if not yet open
1198 *
d9b7b057
KW
1199 * bdref_key specifies the key for the image's BlockdevRef in the options QDict.
1200 * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict
1201 * itself, all options starting with "${bdref_key}." are considered part of the
1202 * BlockdevRef.
1203 *
1204 * TODO Can this be unified with bdrv_open_image()?
31ca6d07 1205 */
d9b7b057
KW
1206int bdrv_open_backing_file(BlockDriverState *bs, QDict *parent_options,
1207 const char *bdref_key, Error **errp)
9156df12 1208{
1ba4b6a5 1209 char *backing_filename = g_malloc0(PATH_MAX);
d9b7b057
KW
1210 char *bdref_key_dot;
1211 const char *reference = NULL;
317fc44e 1212 int ret = 0;
8d24cce1 1213 BlockDriverState *backing_hd;
d9b7b057
KW
1214 QDict *options;
1215 QDict *tmp_parent_options = NULL;
34b5d2c6 1216 Error *local_err = NULL;
9156df12 1217
760e0063 1218 if (bs->backing != NULL) {
1ba4b6a5 1219 goto free_exit;
9156df12
PB
1220 }
1221
31ca6d07 1222 /* NULL means an empty set of options */
d9b7b057
KW
1223 if (parent_options == NULL) {
1224 tmp_parent_options = qdict_new();
1225 parent_options = tmp_parent_options;
31ca6d07
KW
1226 }
1227
9156df12 1228 bs->open_flags &= ~BDRV_O_NO_BACKING;
d9b7b057
KW
1229
1230 bdref_key_dot = g_strdup_printf("%s.", bdref_key);
1231 qdict_extract_subqdict(parent_options, &options, bdref_key_dot);
1232 g_free(bdref_key_dot);
1233
1234 reference = qdict_get_try_str(parent_options, bdref_key);
1235 if (reference || qdict_haskey(options, "file.filename")) {
1cb6f506
KW
1236 backing_filename[0] = '\0';
1237 } else if (bs->backing_file[0] == '\0' && qdict_size(options) == 0) {
31ca6d07 1238 QDECREF(options);
1ba4b6a5 1239 goto free_exit;
dbecebdd 1240 } else {
9f07429e
HR
1241 bdrv_get_full_backing_filename(bs, backing_filename, PATH_MAX,
1242 &local_err);
1243 if (local_err) {
1244 ret = -EINVAL;
1245 error_propagate(errp, local_err);
1246 QDECREF(options);
1247 goto free_exit;
1248 }
9156df12
PB
1249 }
1250
8ee79e70
KW
1251 if (!bs->drv || !bs->drv->supports_backing) {
1252 ret = -EINVAL;
1253 error_setg(errp, "Driver doesn't support backing files");
1254 QDECREF(options);
1255 goto free_exit;
1256 }
1257
c5f6e493
KW
1258 if (bs->backing_format[0] != '\0' && !qdict_haskey(options, "driver")) {
1259 qdict_put(options, "driver", qstring_from_str(bs->backing_format));
9156df12
PB
1260 }
1261
d9b7b057 1262 backing_hd = NULL;
f3930ed0
KW
1263 ret = bdrv_open_inherit(&backing_hd,
1264 *backing_filename ? backing_filename : NULL,
d9b7b057
KW
1265 reference, options, 0, bs, &child_backing,
1266 &local_err);
9156df12 1267 if (ret < 0) {
9156df12 1268 bs->open_flags |= BDRV_O_NO_BACKING;
b04b6b6e
FZ
1269 error_setg(errp, "Could not open backing file: %s",
1270 error_get_pretty(local_err));
1271 error_free(local_err);
1ba4b6a5 1272 goto free_exit;
9156df12 1273 }
df581792 1274
5db15a57
KW
1275 /* Hook up the backing file link; drop our reference, bs owns the
1276 * backing_hd reference now */
8d24cce1 1277 bdrv_set_backing_hd(bs, backing_hd);
5db15a57 1278 bdrv_unref(backing_hd);
d80ac658 1279
d9b7b057
KW
1280 qdict_del(parent_options, bdref_key);
1281
1ba4b6a5
BC
1282free_exit:
1283 g_free(backing_filename);
d9b7b057 1284 QDECREF(tmp_parent_options);
1ba4b6a5 1285 return ret;
9156df12
PB
1286}
1287
da557aac
HR
1288/*
1289 * Opens a disk image whose options are given as BlockdevRef in another block
1290 * device's options.
1291 *
da557aac 1292 * If allow_none is true, no image will be opened if filename is false and no
b4b059f6 1293 * BlockdevRef is given. NULL will be returned, but errp remains unset.
da557aac
HR
1294 *
1295 * bdrev_key specifies the key for the image's BlockdevRef in the options QDict.
1296 * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict
1297 * itself, all options starting with "${bdref_key}." are considered part of the
1298 * BlockdevRef.
1299 *
1300 * The BlockdevRef will be removed from the options QDict.
1301 */
b4b059f6
KW
1302BdrvChild *bdrv_open_child(const char *filename,
1303 QDict *options, const char *bdref_key,
1304 BlockDriverState* parent,
1305 const BdrvChildRole *child_role,
1306 bool allow_none, Error **errp)
da557aac 1307{
b4b059f6
KW
1308 BdrvChild *c = NULL;
1309 BlockDriverState *bs;
da557aac
HR
1310 QDict *image_options;
1311 int ret;
1312 char *bdref_key_dot;
1313 const char *reference;
1314
df581792 1315 assert(child_role != NULL);
f67503e5 1316
da557aac
HR
1317 bdref_key_dot = g_strdup_printf("%s.", bdref_key);
1318 qdict_extract_subqdict(options, &image_options, bdref_key_dot);
1319 g_free(bdref_key_dot);
1320
1321 reference = qdict_get_try_str(options, bdref_key);
1322 if (!filename && !reference && !qdict_size(image_options)) {
b4b059f6 1323 if (!allow_none) {
da557aac
HR
1324 error_setg(errp, "A block device must be specified for \"%s\"",
1325 bdref_key);
da557aac 1326 }
b20e61e0 1327 QDECREF(image_options);
da557aac
HR
1328 goto done;
1329 }
1330
b4b059f6
KW
1331 bs = NULL;
1332 ret = bdrv_open_inherit(&bs, filename, reference, image_options, 0,
ce343771 1333 parent, child_role, errp);
df581792
KW
1334 if (ret < 0) {
1335 goto done;
1336 }
1337
260fecf1 1338 c = bdrv_attach_child(parent, bs, bdref_key, child_role);
da557aac
HR
1339
1340done:
1341 qdict_del(options, bdref_key);
b4b059f6
KW
1342 return c;
1343}
1344
6b8aeca5 1345int bdrv_append_temp_snapshot(BlockDriverState *bs, int flags, Error **errp)
b998875d
KW
1346{
1347 /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */
1ba4b6a5 1348 char *tmp_filename = g_malloc0(PATH_MAX + 1);
b998875d 1349 int64_t total_size;
83d0521a 1350 QemuOpts *opts = NULL;
b998875d
KW
1351 QDict *snapshot_options;
1352 BlockDriverState *bs_snapshot;
c2e0dbbf 1353 Error *local_err = NULL;
b998875d
KW
1354 int ret;
1355
1356 /* if snapshot, we create a temporary backing file and open it
1357 instead of opening 'filename' directly */
1358
1359 /* Get the required size from the image */
f187743a
KW
1360 total_size = bdrv_getlength(bs);
1361 if (total_size < 0) {
6b8aeca5 1362 ret = total_size;
f187743a 1363 error_setg_errno(errp, -total_size, "Could not get image size");
1ba4b6a5 1364 goto out;
f187743a 1365 }
b998875d
KW
1366
1367 /* Create the temporary image */
1ba4b6a5 1368 ret = get_tmp_filename(tmp_filename, PATH_MAX + 1);
b998875d
KW
1369 if (ret < 0) {
1370 error_setg_errno(errp, -ret, "Could not get temporary filename");
1ba4b6a5 1371 goto out;
b998875d
KW
1372 }
1373
ef810437 1374 opts = qemu_opts_create(bdrv_qcow2.create_opts, NULL, 0,
c282e1fd 1375 &error_abort);
39101f25 1376 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_size, &error_abort);
ef810437 1377 ret = bdrv_create(&bdrv_qcow2, tmp_filename, opts, &local_err);
83d0521a 1378 qemu_opts_del(opts);
b998875d
KW
1379 if (ret < 0) {
1380 error_setg_errno(errp, -ret, "Could not create temporary overlay "
1381 "'%s': %s", tmp_filename,
1382 error_get_pretty(local_err));
1383 error_free(local_err);
1ba4b6a5 1384 goto out;
b998875d
KW
1385 }
1386
1387 /* Prepare a new options QDict for the temporary file */
1388 snapshot_options = qdict_new();
1389 qdict_put(snapshot_options, "file.driver",
1390 qstring_from_str("file"));
1391 qdict_put(snapshot_options, "file.filename",
1392 qstring_from_str(tmp_filename));
e6641719
HR
1393 qdict_put(snapshot_options, "driver",
1394 qstring_from_str("qcow2"));
b998875d 1395
e4e9986b 1396 bs_snapshot = bdrv_new();
b998875d
KW
1397
1398 ret = bdrv_open(&bs_snapshot, NULL, NULL, snapshot_options,
6ebf9aa2 1399 flags, &local_err);
b998875d
KW
1400 if (ret < 0) {
1401 error_propagate(errp, local_err);
1ba4b6a5 1402 goto out;
b998875d
KW
1403 }
1404
1405 bdrv_append(bs_snapshot, bs);
1ba4b6a5
BC
1406
1407out:
1408 g_free(tmp_filename);
6b8aeca5 1409 return ret;
b998875d
KW
1410}
1411
b6ce07aa
KW
1412/*
1413 * Opens a disk image (raw, qcow2, vmdk, ...)
de9c0cec
KW
1414 *
1415 * options is a QDict of options to pass to the block drivers, or NULL for an
1416 * empty set of options. The reference to the QDict belongs to the block layer
1417 * after the call (even on failure), so if the caller intends to reuse the
1418 * dictionary, it needs to use QINCREF() before calling bdrv_open.
f67503e5
HR
1419 *
1420 * If *pbs is NULL, a new BDS will be created with a pointer to it stored there.
1421 * If it is not NULL, the referenced BDS will be reused.
ddf5636d
HR
1422 *
1423 * The reference parameter may be used to specify an existing block device which
1424 * should be opened. If specified, neither options nor a filename may be given,
1425 * nor can an existing BDS be reused (that is, *pbs has to be NULL).
b6ce07aa 1426 */
f3930ed0
KW
1427static int bdrv_open_inherit(BlockDriverState **pbs, const char *filename,
1428 const char *reference, QDict *options, int flags,
1429 BlockDriverState *parent,
ce343771 1430 const BdrvChildRole *child_role, Error **errp)
ea2384d3 1431{
b6ce07aa 1432 int ret;
9a4f4c31
KW
1433 BdrvChild *file = NULL;
1434 BlockDriverState *bs;
ce343771 1435 BlockDriver *drv = NULL;
74fe54f2 1436 const char *drvname;
3e8c2e57 1437 const char *backing;
34b5d2c6 1438 Error *local_err = NULL;
b1e6fc08 1439 int snapshot_flags = 0;
712e7874 1440
f67503e5 1441 assert(pbs);
f3930ed0
KW
1442 assert(!child_role || !flags);
1443 assert(!child_role == !parent);
f67503e5 1444
ddf5636d
HR
1445 if (reference) {
1446 bool options_non_empty = options ? qdict_size(options) : false;
1447 QDECREF(options);
1448
1449 if (*pbs) {
1450 error_setg(errp, "Cannot reuse an existing BDS when referencing "
1451 "another block device");
1452 return -EINVAL;
1453 }
1454
1455 if (filename || options_non_empty) {
1456 error_setg(errp, "Cannot reference an existing block device with "
1457 "additional options or a new filename");
1458 return -EINVAL;
1459 }
1460
1461 bs = bdrv_lookup_bs(reference, reference, errp);
1462 if (!bs) {
1463 return -ENODEV;
1464 }
1465 bdrv_ref(bs);
1466 *pbs = bs;
1467 return 0;
1468 }
1469
f67503e5
HR
1470 if (*pbs) {
1471 bs = *pbs;
1472 } else {
e4e9986b 1473 bs = bdrv_new();
f67503e5
HR
1474 }
1475
de9c0cec
KW
1476 /* NULL means an empty set of options */
1477 if (options == NULL) {
1478 options = qdict_new();
1479 }
1480
f3930ed0 1481 if (child_role) {
bddcec37 1482 bs->inherits_from = parent;
f3930ed0
KW
1483 flags = child_role->inherit_flags(parent->open_flags);
1484 }
1485
053e1578 1486 ret = bdrv_fill_options(&options, &filename, &flags, &local_err);
462f5bcf
KW
1487 if (local_err) {
1488 goto fail;
1489 }
1490
62392ebb
KW
1491 bs->open_flags = flags;
1492 bs->options = options;
1493 options = qdict_clone_shallow(options);
1494
76c591b0 1495 /* Find the right image format driver */
76c591b0
KW
1496 drvname = qdict_get_try_str(options, "driver");
1497 if (drvname) {
1498 drv = bdrv_find_format(drvname);
76c591b0
KW
1499 if (!drv) {
1500 error_setg(errp, "Unknown driver: '%s'", drvname);
1501 ret = -EINVAL;
1502 goto fail;
1503 }
1504 }
1505
1506 assert(drvname || !(flags & BDRV_O_PROTOCOL));
76c591b0 1507
3e8c2e57
AG
1508 backing = qdict_get_try_str(options, "backing");
1509 if (backing && *backing == '\0') {
1510 flags |= BDRV_O_NO_BACKING;
1511 qdict_del(options, "backing");
1512 }
1513
f500a6d3 1514 /* Open image file without format layer */
f4788adc
KW
1515 if ((flags & BDRV_O_PROTOCOL) == 0) {
1516 if (flags & BDRV_O_RDWR) {
1517 flags |= BDRV_O_ALLOW_RDWR;
1518 }
1519 if (flags & BDRV_O_SNAPSHOT) {
1520 snapshot_flags = bdrv_temp_snapshot_flags(flags);
1521 flags = bdrv_backing_flags(flags);
1522 }
f500a6d3 1523
f3930ed0 1524 bs->open_flags = flags;
1fdd6933 1525
9a4f4c31
KW
1526 file = bdrv_open_child(filename, options, "file", bs,
1527 &child_file, true, &local_err);
1fdd6933
KW
1528 if (local_err) {
1529 ret = -EINVAL;
f4788adc
KW
1530 goto fail;
1531 }
f500a6d3
KW
1532 }
1533
76c591b0 1534 /* Image format probing */
38f3ef57 1535 bs->probed = !drv;
76c591b0 1536 if (!drv && file) {
9a4f4c31 1537 ret = find_image_format(file->bs, filename, &drv, &local_err);
17b005f1 1538 if (ret < 0) {
8bfea15d 1539 goto fail;
2a05cbe4 1540 }
62392ebb
KW
1541 /*
1542 * This option update would logically belong in bdrv_fill_options(),
1543 * but we first need to open bs->file for the probing to work, while
1544 * opening bs->file already requires the (mostly) final set of options
1545 * so that cache mode etc. can be inherited.
1546 *
1547 * Adding the driver later is somewhat ugly, but it's not an option
1548 * that would ever be inherited, so it's correct. We just need to make
1549 * sure to update both bs->options (which has the full effective
1550 * options for bs) and options (which has file.* already removed).
1551 */
1552 qdict_put(bs->options, "driver", qstring_from_str(drv->format_name));
1553 qdict_put(options, "driver", qstring_from_str(drv->format_name));
76c591b0 1554 } else if (!drv) {
17b005f1
KW
1555 error_setg(errp, "Must specify either driver or file");
1556 ret = -EINVAL;
8bfea15d 1557 goto fail;
ea2384d3 1558 }
b6ce07aa 1559
53a29513
HR
1560 /* BDRV_O_PROTOCOL must be set iff a protocol BDS is about to be created */
1561 assert(!!(flags & BDRV_O_PROTOCOL) == !!drv->bdrv_file_open);
1562 /* file must be NULL if a protocol BDS is about to be created
1563 * (the inverse results in an error message from bdrv_open_common()) */
1564 assert(!(flags & BDRV_O_PROTOCOL) || !file);
1565
b6ce07aa 1566 /* Open the image */
62392ebb 1567 ret = bdrv_open_common(bs, file, options, flags, &local_err);
b6ce07aa 1568 if (ret < 0) {
8bfea15d 1569 goto fail;
6987307c
CH
1570 }
1571
2a05cbe4 1572 if (file && (bs->file != file)) {
9a4f4c31 1573 bdrv_unref_child(bs, file);
f500a6d3
KW
1574 file = NULL;
1575 }
1576
b6ce07aa 1577 /* If there is a backing file, use it */
9156df12 1578 if ((flags & BDRV_O_NO_BACKING) == 0) {
d9b7b057 1579 ret = bdrv_open_backing_file(bs, options, "backing", &local_err);
b6ce07aa 1580 if (ret < 0) {
b6ad491a 1581 goto close_and_fail;
b6ce07aa 1582 }
b6ce07aa
KW
1583 }
1584
91af7014
HR
1585 bdrv_refresh_filename(bs);
1586
b6ad491a 1587 /* Check if any unknown options were used */
5acd9d81 1588 if (options && (qdict_size(options) != 0)) {
b6ad491a 1589 const QDictEntry *entry = qdict_first(options);
5acd9d81
HR
1590 if (flags & BDRV_O_PROTOCOL) {
1591 error_setg(errp, "Block protocol '%s' doesn't support the option "
1592 "'%s'", drv->format_name, entry->key);
1593 } else {
1594 error_setg(errp, "Block format '%s' used by device '%s' doesn't "
1595 "support the option '%s'", drv->format_name,
bfb197e0 1596 bdrv_get_device_name(bs), entry->key);
5acd9d81 1597 }
b6ad491a
KW
1598
1599 ret = -EINVAL;
1600 goto close_and_fail;
1601 }
b6ad491a 1602
b6ce07aa 1603 if (!bdrv_key_required(bs)) {
a7f53e26
MA
1604 if (bs->blk) {
1605 blk_dev_change_media_cb(bs->blk, true);
1606 }
c3adb58f
MA
1607 } else if (!runstate_check(RUN_STATE_PRELAUNCH)
1608 && !runstate_check(RUN_STATE_INMIGRATE)
1609 && !runstate_check(RUN_STATE_PAUSED)) { /* HACK */
1610 error_setg(errp,
1611 "Guest must be stopped for opening of encrypted image");
1612 ret = -EBUSY;
1613 goto close_and_fail;
b6ce07aa
KW
1614 }
1615
c3adb58f 1616 QDECREF(options);
f67503e5 1617 *pbs = bs;
dd62f1ca
KW
1618
1619 /* For snapshot=on, create a temporary qcow2 overlay. bs points to the
1620 * temporary snapshot afterwards. */
1621 if (snapshot_flags) {
1622 ret = bdrv_append_temp_snapshot(bs, snapshot_flags, &local_err);
1623 if (local_err) {
1624 goto close_and_fail;
1625 }
1626 }
1627
b6ce07aa
KW
1628 return 0;
1629
8bfea15d 1630fail:
f500a6d3 1631 if (file != NULL) {
9a4f4c31 1632 bdrv_unref_child(bs, file);
f500a6d3 1633 }
de9c0cec 1634 QDECREF(bs->options);
b6ad491a 1635 QDECREF(options);
de9c0cec 1636 bs->options = NULL;
f67503e5
HR
1637 if (!*pbs) {
1638 /* If *pbs is NULL, a new BDS has been created in this function and
1639 needs to be freed now. Otherwise, it does not need to be closed,
1640 since it has not really been opened yet. */
1641 bdrv_unref(bs);
1642 }
84d18f06 1643 if (local_err) {
34b5d2c6
HR
1644 error_propagate(errp, local_err);
1645 }
b6ad491a 1646 return ret;
de9c0cec 1647
b6ad491a 1648close_and_fail:
f67503e5
HR
1649 /* See fail path, but now the BDS has to be always closed */
1650 if (*pbs) {
1651 bdrv_close(bs);
1652 } else {
1653 bdrv_unref(bs);
1654 }
b6ad491a 1655 QDECREF(options);
84d18f06 1656 if (local_err) {
34b5d2c6
HR
1657 error_propagate(errp, local_err);
1658 }
b6ce07aa
KW
1659 return ret;
1660}
1661
f3930ed0 1662int bdrv_open(BlockDriverState **pbs, const char *filename,
6ebf9aa2 1663 const char *reference, QDict *options, int flags, Error **errp)
f3930ed0
KW
1664{
1665 return bdrv_open_inherit(pbs, filename, reference, options, flags, NULL,
ce343771 1666 NULL, errp);
f3930ed0
KW
1667}
1668
e971aa12
JC
1669typedef struct BlockReopenQueueEntry {
1670 bool prepared;
1671 BDRVReopenState state;
1672 QSIMPLEQ_ENTRY(BlockReopenQueueEntry) entry;
1673} BlockReopenQueueEntry;
1674
1675/*
1676 * Adds a BlockDriverState to a simple queue for an atomic, transactional
1677 * reopen of multiple devices.
1678 *
1679 * bs_queue can either be an existing BlockReopenQueue that has had QSIMPLE_INIT
1680 * already performed, or alternatively may be NULL a new BlockReopenQueue will
1681 * be created and initialized. This newly created BlockReopenQueue should be
1682 * passed back in for subsequent calls that are intended to be of the same
1683 * atomic 'set'.
1684 *
1685 * bs is the BlockDriverState to add to the reopen queue.
1686 *
4d2cb092
KW
1687 * options contains the changed options for the associated bs
1688 * (the BlockReopenQueue takes ownership)
1689 *
e971aa12
JC
1690 * flags contains the open flags for the associated bs
1691 *
1692 * returns a pointer to bs_queue, which is either the newly allocated
1693 * bs_queue, or the existing bs_queue being used.
1694 *
1695 */
28518102
KW
1696static BlockReopenQueue *bdrv_reopen_queue_child(BlockReopenQueue *bs_queue,
1697 BlockDriverState *bs,
1698 QDict *options,
1699 int flags,
1700 const BdrvChildRole *role,
1701 QDict *parent_options,
1702 int parent_flags)
e971aa12
JC
1703{
1704 assert(bs != NULL);
1705
1706 BlockReopenQueueEntry *bs_entry;
67251a31 1707 BdrvChild *child;
4d2cb092 1708 QDict *old_options;
67251a31 1709
e971aa12
JC
1710 if (bs_queue == NULL) {
1711 bs_queue = g_new0(BlockReopenQueue, 1);
1712 QSIMPLEQ_INIT(bs_queue);
1713 }
1714
4d2cb092
KW
1715 if (!options) {
1716 options = qdict_new();
1717 }
1718
28518102
KW
1719 /*
1720 * Precedence of options:
1721 * 1. Explicitly passed in options (highest)
1722 * 2. TODO Set in flags (only for top level)
1723 * 3. TODO Retained from explicitly set options of bs
1724 * 4. TODO Inherited from parent node
1725 * 5. Retained from effective options of bs
1726 */
1727
1728 /* Inherit from parent node */
1729 if (parent_options) {
1730 assert(!flags);
1731 flags = role->inherit_flags(parent_flags);
1732 }
1733
1734 /* Old values are used for options that aren't set yet */
4d2cb092 1735 old_options = qdict_clone_shallow(bs->options);
cddff5ba 1736 bdrv_join_options(bs, options, old_options);
4d2cb092
KW
1737 QDECREF(old_options);
1738
f1f25a2e
KW
1739 /* bdrv_open() masks this flag out */
1740 flags &= ~BDRV_O_PROTOCOL;
1741
67251a31 1742 QLIST_FOREACH(child, &bs->children, next) {
4c9dfe5d
KW
1743 QDict *new_child_options;
1744 char *child_key_dot;
67251a31 1745
4c9dfe5d
KW
1746 /* reopen can only change the options of block devices that were
1747 * implicitly created and inherited options. For other (referenced)
1748 * block devices, a syntax like "backing.foo" results in an error. */
67251a31
KW
1749 if (child->bs->inherits_from != bs) {
1750 continue;
1751 }
1752
4c9dfe5d
KW
1753 child_key_dot = g_strdup_printf("%s.", child->name);
1754 qdict_extract_subqdict(options, &new_child_options, child_key_dot);
1755 g_free(child_key_dot);
1756
28518102
KW
1757 bdrv_reopen_queue_child(bs_queue, child->bs, new_child_options, 0,
1758 child->role, options, flags);
e971aa12
JC
1759 }
1760
1761 bs_entry = g_new0(BlockReopenQueueEntry, 1);
1762 QSIMPLEQ_INSERT_TAIL(bs_queue, bs_entry, entry);
1763
1764 bs_entry->state.bs = bs;
4d2cb092 1765 bs_entry->state.options = options;
e971aa12
JC
1766 bs_entry->state.flags = flags;
1767
1768 return bs_queue;
1769}
1770
28518102
KW
1771BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue,
1772 BlockDriverState *bs,
1773 QDict *options, int flags)
1774{
1775 return bdrv_reopen_queue_child(bs_queue, bs, options, flags,
1776 NULL, NULL, 0);
1777}
1778
e971aa12
JC
1779/*
1780 * Reopen multiple BlockDriverStates atomically & transactionally.
1781 *
1782 * The queue passed in (bs_queue) must have been built up previous
1783 * via bdrv_reopen_queue().
1784 *
1785 * Reopens all BDS specified in the queue, with the appropriate
1786 * flags. All devices are prepared for reopen, and failure of any
1787 * device will cause all device changes to be abandonded, and intermediate
1788 * data cleaned up.
1789 *
1790 * If all devices prepare successfully, then the changes are committed
1791 * to all devices.
1792 *
1793 */
1794int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp)
1795{
1796 int ret = -1;
1797 BlockReopenQueueEntry *bs_entry, *next;
1798 Error *local_err = NULL;
1799
1800 assert(bs_queue != NULL);
1801
1802 bdrv_drain_all();
1803
1804 QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) {
1805 if (bdrv_reopen_prepare(&bs_entry->state, bs_queue, &local_err)) {
1806 error_propagate(errp, local_err);
1807 goto cleanup;
1808 }
1809 bs_entry->prepared = true;
1810 }
1811
1812 /* If we reach this point, we have success and just need to apply the
1813 * changes
1814 */
1815 QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) {
1816 bdrv_reopen_commit(&bs_entry->state);
1817 }
1818
1819 ret = 0;
1820
1821cleanup:
1822 QSIMPLEQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) {
1823 if (ret && bs_entry->prepared) {
1824 bdrv_reopen_abort(&bs_entry->state);
1825 }
4d2cb092 1826 QDECREF(bs_entry->state.options);
e971aa12
JC
1827 g_free(bs_entry);
1828 }
1829 g_free(bs_queue);
1830 return ret;
1831}
1832
1833
1834/* Reopen a single BlockDriverState with the specified flags. */
1835int bdrv_reopen(BlockDriverState *bs, int bdrv_flags, Error **errp)
1836{
1837 int ret = -1;
1838 Error *local_err = NULL;
4d2cb092 1839 BlockReopenQueue *queue = bdrv_reopen_queue(NULL, bs, NULL, bdrv_flags);
e971aa12
JC
1840
1841 ret = bdrv_reopen_multiple(queue, &local_err);
1842 if (local_err != NULL) {
1843 error_propagate(errp, local_err);
1844 }
1845 return ret;
1846}
1847
1848
1849/*
1850 * Prepares a BlockDriverState for reopen. All changes are staged in the
1851 * 'opaque' field of the BDRVReopenState, which is used and allocated by
1852 * the block driver layer .bdrv_reopen_prepare()
1853 *
1854 * bs is the BlockDriverState to reopen
1855 * flags are the new open flags
1856 * queue is the reopen queue
1857 *
1858 * Returns 0 on success, non-zero on error. On error errp will be set
1859 * as well.
1860 *
1861 * On failure, bdrv_reopen_abort() will be called to clean up any data.
1862 * It is the responsibility of the caller to then call the abort() or
1863 * commit() for any other BDS that have been left in a prepare() state
1864 *
1865 */
1866int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue,
1867 Error **errp)
1868{
1869 int ret = -1;
1870 Error *local_err = NULL;
1871 BlockDriver *drv;
1872
1873 assert(reopen_state != NULL);
1874 assert(reopen_state->bs->drv != NULL);
1875 drv = reopen_state->bs->drv;
1876
1877 /* if we are to stay read-only, do not allow permission change
1878 * to r/w */
1879 if (!(reopen_state->bs->open_flags & BDRV_O_ALLOW_RDWR) &&
1880 reopen_state->flags & BDRV_O_RDWR) {
81e5f78a
AG
1881 error_setg(errp, "Node '%s' is read only",
1882 bdrv_get_device_or_node_name(reopen_state->bs));
e971aa12
JC
1883 goto error;
1884 }
1885
1886
1887 ret = bdrv_flush(reopen_state->bs);
1888 if (ret) {
455b0fde 1889 error_setg_errno(errp, -ret, "Error flushing drive");
e971aa12
JC
1890 goto error;
1891 }
1892
1893 if (drv->bdrv_reopen_prepare) {
1894 ret = drv->bdrv_reopen_prepare(reopen_state, queue, &local_err);
1895 if (ret) {
1896 if (local_err != NULL) {
1897 error_propagate(errp, local_err);
1898 } else {
d8b6895f
LC
1899 error_setg(errp, "failed while preparing to reopen image '%s'",
1900 reopen_state->bs->filename);
e971aa12
JC
1901 }
1902 goto error;
1903 }
1904 } else {
1905 /* It is currently mandatory to have a bdrv_reopen_prepare()
1906 * handler for each supported drv. */
81e5f78a
AG
1907 error_setg(errp, "Block format '%s' used by node '%s' "
1908 "does not support reopening files", drv->format_name,
1909 bdrv_get_device_or_node_name(reopen_state->bs));
e971aa12
JC
1910 ret = -1;
1911 goto error;
1912 }
1913
4d2cb092
KW
1914 /* Options that are not handled are only okay if they are unchanged
1915 * compared to the old state. It is expected that some options are only
1916 * used for the initial open, but not reopen (e.g. filename) */
1917 if (qdict_size(reopen_state->options)) {
1918 const QDictEntry *entry = qdict_first(reopen_state->options);
1919
1920 do {
1921 QString *new_obj = qobject_to_qstring(entry->value);
1922 const char *new = qstring_get_str(new_obj);
1923 const char *old = qdict_get_try_str(reopen_state->bs->options,
1924 entry->key);
1925
1926 if (!old || strcmp(new, old)) {
1927 error_setg(errp, "Cannot change the option '%s'", entry->key);
1928 ret = -EINVAL;
1929 goto error;
1930 }
1931 } while ((entry = qdict_next(reopen_state->options, entry)));
1932 }
1933
e971aa12
JC
1934 ret = 0;
1935
1936error:
1937 return ret;
1938}
1939
1940/*
1941 * Takes the staged changes for the reopen from bdrv_reopen_prepare(), and
1942 * makes them final by swapping the staging BlockDriverState contents into
1943 * the active BlockDriverState contents.
1944 */
1945void bdrv_reopen_commit(BDRVReopenState *reopen_state)
1946{
1947 BlockDriver *drv;
1948
1949 assert(reopen_state != NULL);
1950 drv = reopen_state->bs->drv;
1951 assert(drv != NULL);
1952
1953 /* If there are any driver level actions to take */
1954 if (drv->bdrv_reopen_commit) {
1955 drv->bdrv_reopen_commit(reopen_state);
1956 }
1957
1958 /* set BDS specific flags now */
1959 reopen_state->bs->open_flags = reopen_state->flags;
1960 reopen_state->bs->enable_write_cache = !!(reopen_state->flags &
1961 BDRV_O_CACHE_WB);
1962 reopen_state->bs->read_only = !(reopen_state->flags & BDRV_O_RDWR);
355ef4ac 1963
3baca891 1964 bdrv_refresh_limits(reopen_state->bs, NULL);
e971aa12
JC
1965}
1966
1967/*
1968 * Abort the reopen, and delete and free the staged changes in
1969 * reopen_state
1970 */
1971void bdrv_reopen_abort(BDRVReopenState *reopen_state)
1972{
1973 BlockDriver *drv;
1974
1975 assert(reopen_state != NULL);
1976 drv = reopen_state->bs->drv;
1977 assert(drv != NULL);
1978
1979 if (drv->bdrv_reopen_abort) {
1980 drv->bdrv_reopen_abort(reopen_state);
1981 }
1982}
1983
1984
fc01f7e7
FB
1985void bdrv_close(BlockDriverState *bs)
1986{
33384421
HR
1987 BdrvAioNotifier *ban, *ban_next;
1988
3cbc002c
PB
1989 if (bs->job) {
1990 block_job_cancel_sync(bs->job);
1991 }
99b7e775
AG
1992
1993 /* Disable I/O limits and drain all pending throttled requests */
a0d64a61 1994 if (bs->throttle_state) {
99b7e775
AG
1995 bdrv_io_limits_disable(bs);
1996 }
1997
53ec73e2 1998 bdrv_drain(bs); /* complete I/O */
58fda173 1999 bdrv_flush(bs);
53ec73e2 2000 bdrv_drain(bs); /* in case flush left pending I/O */
d7d512f6 2001 notifier_list_notify(&bs->close_notifiers, bs);
7094f12f 2002
b4d02820
HR
2003 if (bs->blk) {
2004 blk_dev_change_media_cb(bs->blk, false);
2005 }
2006
3cbc002c 2007 if (bs->drv) {
6e93e7c4
KW
2008 BdrvChild *child, *next;
2009
9a7dedbc 2010 bs->drv->bdrv_close(bs);
9a4f4c31 2011 bs->drv = NULL;
9a7dedbc 2012
5db15a57 2013 bdrv_set_backing_hd(bs, NULL);
9a7dedbc 2014
9a4f4c31
KW
2015 if (bs->file != NULL) {
2016 bdrv_unref_child(bs, bs->file);
2017 bs->file = NULL;
2018 }
2019
6e93e7c4 2020 QLIST_FOREACH_SAFE(child, &bs->children, next, next) {
33a60407
KW
2021 /* TODO Remove bdrv_unref() from drivers' close function and use
2022 * bdrv_unref_child() here */
bddcec37
KW
2023 if (child->bs->inherits_from == bs) {
2024 child->bs->inherits_from = NULL;
2025 }
33a60407 2026 bdrv_detach_child(child);
6e93e7c4
KW
2027 }
2028
7267c094 2029 g_free(bs->opaque);
ea2384d3 2030 bs->opaque = NULL;
53fec9d3 2031 bs->copy_on_read = 0;
a275fa42
PB
2032 bs->backing_file[0] = '\0';
2033 bs->backing_format[0] = '\0';
6405875c
PB
2034 bs->total_sectors = 0;
2035 bs->encrypted = 0;
2036 bs->valid_key = 0;
2037 bs->sg = 0;
0d51b4de 2038 bs->zero_beyond_eof = false;
de9c0cec
KW
2039 QDECREF(bs->options);
2040 bs->options = NULL;
91af7014
HR
2041 QDECREF(bs->full_open_options);
2042 bs->full_open_options = NULL;
b338082b 2043 }
98f90dba 2044
33384421
HR
2045 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) {
2046 g_free(ban);
2047 }
2048 QLIST_INIT(&bs->aio_notifiers);
b338082b
FB
2049}
2050
2bc93fed
MK
2051void bdrv_close_all(void)
2052{
2053 BlockDriverState *bs;
2054
dc364f4c 2055 QTAILQ_FOREACH(bs, &bdrv_states, device_list) {
ed78cda3
SH
2056 AioContext *aio_context = bdrv_get_aio_context(bs);
2057
2058 aio_context_acquire(aio_context);
2bc93fed 2059 bdrv_close(bs);
ed78cda3 2060 aio_context_release(aio_context);
2bc93fed
MK
2061 }
2062}
2063
dc364f4c
BC
2064/* make a BlockDriverState anonymous by removing from bdrv_state and
2065 * graph_bdrv_state list.
d22b2f41
RH
2066 Also, NULL terminate the device_name to prevent double remove */
2067void bdrv_make_anon(BlockDriverState *bs)
2068{
bfb197e0
MA
2069 /*
2070 * Take care to remove bs from bdrv_states only when it's actually
2071 * in it. Note that bs->device_list.tqe_prev is initially null,
2072 * and gets set to non-null by QTAILQ_INSERT_TAIL(). Establish
2073 * the useful invariant "bs in bdrv_states iff bs->tqe_prev" by
2074 * resetting it to null on remove.
2075 */
2076 if (bs->device_list.tqe_prev) {
dc364f4c 2077 QTAILQ_REMOVE(&bdrv_states, bs, device_list);
bfb197e0 2078 bs->device_list.tqe_prev = NULL;
d22b2f41 2079 }
dc364f4c
BC
2080 if (bs->node_name[0] != '\0') {
2081 QTAILQ_REMOVE(&graph_bdrv_states, bs, node_list);
2082 }
2083 bs->node_name[0] = '\0';
d22b2f41
RH
2084}
2085
8e419aef 2086/* Fields that need to stay with the top-level BDS */
4ddc07ca
PB
2087static void bdrv_move_feature_fields(BlockDriverState *bs_dest,
2088 BlockDriverState *bs_src)
8802d1fd 2089{
4ddc07ca 2090 /* move some fields that need to stay attached to the device */
8802d1fd
JC
2091
2092 /* dev info */
4ddc07ca 2093 bs_dest->copy_on_read = bs_src->copy_on_read;
8802d1fd 2094
4ddc07ca 2095 bs_dest->enable_write_cache = bs_src->enable_write_cache;
c4a248a1 2096
a9fc4408 2097 /* dirty bitmap */
e4654d2d 2098 bs_dest->dirty_bitmaps = bs_src->dirty_bitmaps;
063dd40e 2099}
a9fc4408 2100
dd62f1ca
KW
2101static void change_parent_backing_link(BlockDriverState *from,
2102 BlockDriverState *to)
2103{
2104 BdrvChild *c, *next;
2105
2106 QLIST_FOREACH_SAFE(c, &from->parents, next_parent, next) {
2107 assert(c->role != &child_backing);
2108 c->bs = to;
2109 QLIST_REMOVE(c, next_parent);
2110 QLIST_INSERT_HEAD(&to->parents, c, next_parent);
2111 bdrv_ref(to);
2112 bdrv_unref(from);
2113 }
2114 if (from->blk) {
2115 blk_set_bs(from->blk, to);
2116 if (!to->device_list.tqe_prev) {
2117 QTAILQ_INSERT_BEFORE(from, to, device_list);
2118 }
2119 QTAILQ_REMOVE(&bdrv_states, from, device_list);
2120 }
2121}
2122
2123static void swap_feature_fields(BlockDriverState *bs_top,
2124 BlockDriverState *bs_new)
2125{
2126 BlockDriverState tmp;
2127
2128 bdrv_move_feature_fields(&tmp, bs_top);
2129 bdrv_move_feature_fields(bs_top, bs_new);
2130 bdrv_move_feature_fields(bs_new, &tmp);
2131
2132 assert(!bs_new->throttle_state);
2133 if (bs_top->throttle_state) {
2134 assert(bs_top->io_limits_enabled);
2135 bdrv_io_limits_enable(bs_new, throttle_group_get_name(bs_top));
2136 bdrv_io_limits_disable(bs_top);
2137 }
2138}
2139
4ddc07ca
PB
2140/*
2141 * Add new bs contents at the top of an image chain while the chain is
2142 * live, while keeping required fields on the top layer.
2143 *
2144 * This will modify the BlockDriverState fields, and swap contents
2145 * between bs_new and bs_top. Both bs_new and bs_top are modified.
2146 *
bfb197e0 2147 * bs_new must not be attached to a BlockBackend.
4ddc07ca
PB
2148 *
2149 * This function does not create any image files.
dd62f1ca
KW
2150 *
2151 * bdrv_append() takes ownership of a bs_new reference and unrefs it because
2152 * that's what the callers commonly need. bs_new will be referenced by the old
2153 * parents of bs_top after bdrv_append() returns. If the caller needs to keep a
2154 * reference of its own, it must call bdrv_ref().
4ddc07ca
PB
2155 */
2156void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top)
2157{
dd62f1ca
KW
2158 assert(!bdrv_requests_pending(bs_top));
2159 assert(!bdrv_requests_pending(bs_new));
2160
2161 bdrv_ref(bs_top);
2162 change_parent_backing_link(bs_top, bs_new);
2163
2164 /* Some fields always stay on top of the backing file chain */
2165 swap_feature_fields(bs_top, bs_new);
2166
2167 bdrv_set_backing_hd(bs_new, bs_top);
2168 bdrv_unref(bs_top);
4ddc07ca 2169
dd62f1ca
KW
2170 /* bs_new is now referenced by its new parents, we don't need the
2171 * additional reference any more. */
2172 bdrv_unref(bs_new);
8802d1fd
JC
2173}
2174
3f09bfbc
KW
2175void bdrv_replace_in_backing_chain(BlockDriverState *old, BlockDriverState *new)
2176{
2177 assert(!bdrv_requests_pending(old));
2178 assert(!bdrv_requests_pending(new));
2179
2180 bdrv_ref(old);
2181
2182 if (old->blk) {
2183 /* As long as these fields aren't in BlockBackend, but in the top-level
2184 * BlockDriverState, it's not possible for a BDS to have two BBs.
2185 *
2186 * We really want to copy the fields from old to new, but we go for a
2187 * swap instead so that pointers aren't duplicated and cause trouble.
2188 * (Also, bdrv_swap() used to do the same.) */
2189 assert(!new->blk);
2190 swap_feature_fields(old, new);
2191 }
2192 change_parent_backing_link(old, new);
2193
2194 /* Change backing files if a previously independent node is added to the
2195 * chain. For active commit, we replace top by its own (indirect) backing
2196 * file and don't do anything here so we don't build a loop. */
2197 if (new->backing == NULL && !bdrv_chain_contains(backing_bs(old), new)) {
2198 bdrv_set_backing_hd(new, backing_bs(old));
2199 bdrv_set_backing_hd(old, NULL);
2200 }
2201
2202 bdrv_unref(old);
2203}
2204
4f6fd349 2205static void bdrv_delete(BlockDriverState *bs)
b338082b 2206{
3e914655 2207 assert(!bs->job);
3718d8ab 2208 assert(bdrv_op_blocker_is_empty(bs));
4f6fd349 2209 assert(!bs->refcnt);
e4654d2d 2210 assert(QLIST_EMPTY(&bs->dirty_bitmaps));
18846dee 2211
e1b5c52e
SH
2212 bdrv_close(bs);
2213
1b7bdbc1 2214 /* remove from list, if necessary */
d22b2f41 2215 bdrv_make_anon(bs);
34c6f050 2216
7267c094 2217 g_free(bs);
fc01f7e7
FB
2218}
2219
e97fc193
AL
2220/*
2221 * Run consistency checks on an image
2222 *
e076f338 2223 * Returns 0 if the check could be completed (it doesn't mean that the image is
a1c7273b 2224 * free of errors) or -errno when an internal error occurred. The results of the
e076f338 2225 * check are stored in res.
e97fc193 2226 */
4534ff54 2227int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix)
e97fc193 2228{
908bcd54
HR
2229 if (bs->drv == NULL) {
2230 return -ENOMEDIUM;
2231 }
e97fc193
AL
2232 if (bs->drv->bdrv_check == NULL) {
2233 return -ENOTSUP;
2234 }
2235
e076f338 2236 memset(res, 0, sizeof(*res));
4534ff54 2237 return bs->drv->bdrv_check(bs, res, fix);
e97fc193
AL
2238}
2239
8a426614
KW
2240#define COMMIT_BUF_SECTORS 2048
2241
33e3963e
FB
2242/* commit COW file into the raw image */
2243int bdrv_commit(BlockDriverState *bs)
2244{
19cb3738 2245 BlockDriver *drv = bs->drv;
72706ea4 2246 int64_t sector, total_sectors, length, backing_length;
8a426614 2247 int n, ro, open_flags;
0bce597d 2248 int ret = 0;
72706ea4 2249 uint8_t *buf = NULL;
33e3963e 2250
19cb3738
FB
2251 if (!drv)
2252 return -ENOMEDIUM;
6bb45158 2253
760e0063 2254 if (!bs->backing) {
4dca4b63 2255 return -ENOTSUP;
33e3963e
FB
2256 }
2257
bb00021d 2258 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, NULL) ||
760e0063 2259 bdrv_op_is_blocked(bs->backing->bs, BLOCK_OP_TYPE_COMMIT_TARGET, NULL)) {
2d3735d3
SH
2260 return -EBUSY;
2261 }
2262
760e0063
KW
2263 ro = bs->backing->bs->read_only;
2264 open_flags = bs->backing->bs->open_flags;
4dca4b63
NS
2265
2266 if (ro) {
760e0063 2267 if (bdrv_reopen(bs->backing->bs, open_flags | BDRV_O_RDWR, NULL)) {
0bce597d 2268 return -EACCES;
4dca4b63 2269 }
ea2384d3 2270 }
33e3963e 2271
72706ea4
JC
2272 length = bdrv_getlength(bs);
2273 if (length < 0) {
2274 ret = length;
2275 goto ro_cleanup;
2276 }
2277
760e0063 2278 backing_length = bdrv_getlength(bs->backing->bs);
72706ea4
JC
2279 if (backing_length < 0) {
2280 ret = backing_length;
2281 goto ro_cleanup;
2282 }
2283
2284 /* If our top snapshot is larger than the backing file image,
2285 * grow the backing file image if possible. If not possible,
2286 * we must return an error */
2287 if (length > backing_length) {
760e0063 2288 ret = bdrv_truncate(bs->backing->bs, length);
72706ea4
JC
2289 if (ret < 0) {
2290 goto ro_cleanup;
2291 }
2292 }
2293
2294 total_sectors = length >> BDRV_SECTOR_BITS;
857d4f46
KW
2295
2296 /* qemu_try_blockalign() for bs will choose an alignment that works for
760e0063 2297 * bs->backing->bs as well, so no need to compare the alignment manually. */
857d4f46
KW
2298 buf = qemu_try_blockalign(bs, COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE);
2299 if (buf == NULL) {
2300 ret = -ENOMEM;
2301 goto ro_cleanup;
2302 }
8a426614
KW
2303
2304 for (sector = 0; sector < total_sectors; sector += n) {
d663640c
PB
2305 ret = bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n);
2306 if (ret < 0) {
2307 goto ro_cleanup;
2308 }
2309 if (ret) {
dabfa6cc
KW
2310 ret = bdrv_read(bs, sector, buf, n);
2311 if (ret < 0) {
8a426614
KW
2312 goto ro_cleanup;
2313 }
2314
760e0063 2315 ret = bdrv_write(bs->backing->bs, sector, buf, n);
dabfa6cc 2316 if (ret < 0) {
8a426614
KW
2317 goto ro_cleanup;
2318 }
ea2384d3 2319 }
33e3963e 2320 }
95389c86 2321
1d44952f
CH
2322 if (drv->bdrv_make_empty) {
2323 ret = drv->bdrv_make_empty(bs);
dabfa6cc
KW
2324 if (ret < 0) {
2325 goto ro_cleanup;
2326 }
1d44952f
CH
2327 bdrv_flush(bs);
2328 }
95389c86 2329
3f5075ae
CH
2330 /*
2331 * Make sure all data we wrote to the backing device is actually
2332 * stable on disk.
2333 */
760e0063
KW
2334 if (bs->backing) {
2335 bdrv_flush(bs->backing->bs);
dabfa6cc 2336 }
4dca4b63 2337
dabfa6cc 2338 ret = 0;
4dca4b63 2339ro_cleanup:
857d4f46 2340 qemu_vfree(buf);
4dca4b63
NS
2341
2342 if (ro) {
0bce597d 2343 /* ignoring error return here */
760e0063 2344 bdrv_reopen(bs->backing->bs, open_flags & ~BDRV_O_RDWR, NULL);
4dca4b63
NS
2345 }
2346
1d44952f 2347 return ret;
33e3963e
FB
2348}
2349
e8877497 2350int bdrv_commit_all(void)
6ab4b5ab
MA
2351{
2352 BlockDriverState *bs;
2353
dc364f4c 2354 QTAILQ_FOREACH(bs, &bdrv_states, device_list) {
ed78cda3
SH
2355 AioContext *aio_context = bdrv_get_aio_context(bs);
2356
2357 aio_context_acquire(aio_context);
760e0063 2358 if (bs->drv && bs->backing) {
272d2d8e
JC
2359 int ret = bdrv_commit(bs);
2360 if (ret < 0) {
ed78cda3 2361 aio_context_release(aio_context);
272d2d8e
JC
2362 return ret;
2363 }
e8877497 2364 }
ed78cda3 2365 aio_context_release(aio_context);
6ab4b5ab 2366 }
e8877497 2367 return 0;
6ab4b5ab
MA
2368}
2369
756e6736
KW
2370/*
2371 * Return values:
2372 * 0 - success
2373 * -EINVAL - backing format specified, but no file
2374 * -ENOSPC - can't update the backing file because no space is left in the
2375 * image file header
2376 * -ENOTSUP - format driver doesn't support changing the backing file
2377 */
2378int bdrv_change_backing_file(BlockDriverState *bs,
2379 const char *backing_file, const char *backing_fmt)
2380{
2381 BlockDriver *drv = bs->drv;
469ef350 2382 int ret;
756e6736 2383
5f377794
PB
2384 /* Backing file format doesn't make sense without a backing file */
2385 if (backing_fmt && !backing_file) {
2386 return -EINVAL;
2387 }
2388
756e6736 2389 if (drv->bdrv_change_backing_file != NULL) {
469ef350 2390 ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
756e6736 2391 } else {
469ef350 2392 ret = -ENOTSUP;
756e6736 2393 }
469ef350
PB
2394
2395 if (ret == 0) {
2396 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
2397 pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
2398 }
2399 return ret;
756e6736
KW
2400}
2401
6ebdcee2
JC
2402/*
2403 * Finds the image layer in the chain that has 'bs' as its backing file.
2404 *
2405 * active is the current topmost image.
2406 *
2407 * Returns NULL if bs is not found in active's image chain,
2408 * or if active == bs.
4caf0fcd
JC
2409 *
2410 * Returns the bottommost base image if bs == NULL.
6ebdcee2
JC
2411 */
2412BlockDriverState *bdrv_find_overlay(BlockDriverState *active,
2413 BlockDriverState *bs)
2414{
760e0063
KW
2415 while (active && bs != backing_bs(active)) {
2416 active = backing_bs(active);
6ebdcee2
JC
2417 }
2418
4caf0fcd
JC
2419 return active;
2420}
6ebdcee2 2421
4caf0fcd
JC
2422/* Given a BDS, searches for the base layer. */
2423BlockDriverState *bdrv_find_base(BlockDriverState *bs)
2424{
2425 return bdrv_find_overlay(bs, NULL);
6ebdcee2
JC
2426}
2427
6ebdcee2
JC
2428/*
2429 * Drops images above 'base' up to and including 'top', and sets the image
2430 * above 'top' to have base as its backing file.
2431 *
2432 * Requires that the overlay to 'top' is opened r/w, so that the backing file
2433 * information in 'bs' can be properly updated.
2434 *
2435 * E.g., this will convert the following chain:
2436 * bottom <- base <- intermediate <- top <- active
2437 *
2438 * to
2439 *
2440 * bottom <- base <- active
2441 *
2442 * It is allowed for bottom==base, in which case it converts:
2443 *
2444 * base <- intermediate <- top <- active
2445 *
2446 * to
2447 *
2448 * base <- active
2449 *
54e26900
JC
2450 * If backing_file_str is non-NULL, it will be used when modifying top's
2451 * overlay image metadata.
2452 *
6ebdcee2
JC
2453 * Error conditions:
2454 * if active == top, that is considered an error
2455 *
2456 */
2457int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top,
54e26900 2458 BlockDriverState *base, const char *backing_file_str)
6ebdcee2 2459{
6ebdcee2 2460 BlockDriverState *new_top_bs = NULL;
6ebdcee2
JC
2461 int ret = -EIO;
2462
6ebdcee2
JC
2463 if (!top->drv || !base->drv) {
2464 goto exit;
2465 }
2466
2467 new_top_bs = bdrv_find_overlay(active, top);
2468
2469 if (new_top_bs == NULL) {
2470 /* we could not find the image above 'top', this is an error */
2471 goto exit;
2472 }
2473
760e0063 2474 /* special case of new_top_bs->backing->bs already pointing to base - nothing
6ebdcee2 2475 * to do, no intermediate images */
760e0063 2476 if (backing_bs(new_top_bs) == base) {
6ebdcee2
JC
2477 ret = 0;
2478 goto exit;
2479 }
2480
5db15a57
KW
2481 /* Make sure that base is in the backing chain of top */
2482 if (!bdrv_chain_contains(top, base)) {
6ebdcee2
JC
2483 goto exit;
2484 }
2485
2486 /* success - we can delete the intermediate states, and link top->base */
5db15a57 2487 backing_file_str = backing_file_str ? backing_file_str : base->filename;
54e26900 2488 ret = bdrv_change_backing_file(new_top_bs, backing_file_str,
5db15a57 2489 base->drv ? base->drv->format_name : "");
6ebdcee2
JC
2490 if (ret) {
2491 goto exit;
2492 }
5db15a57 2493 bdrv_set_backing_hd(new_top_bs, base);
6ebdcee2 2494
6ebdcee2 2495 ret = 0;
6ebdcee2 2496exit:
6ebdcee2
JC
2497 return ret;
2498}
2499
61007b31
SH
2500/**
2501 * Truncate file to 'offset' bytes (needed only for file protocols)
2502 */
2503int bdrv_truncate(BlockDriverState *bs, int64_t offset)
71d0770c 2504{
61007b31
SH
2505 BlockDriver *drv = bs->drv;
2506 int ret;
2507 if (!drv)
71d0770c 2508 return -ENOMEDIUM;
61007b31
SH
2509 if (!drv->bdrv_truncate)
2510 return -ENOTSUP;
2511 if (bs->read_only)
2512 return -EACCES;
71d0770c 2513
61007b31
SH
2514 ret = drv->bdrv_truncate(bs, offset);
2515 if (ret == 0) {
2516 ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
2517 bdrv_dirty_bitmap_truncate(bs);
2518 if (bs->blk) {
2519 blk_dev_resize_cb(bs->blk);
2520 }
c0191e76 2521 }
61007b31 2522 return ret;
71d0770c
AL
2523}
2524
61007b31
SH
2525/**
2526 * Length of a allocated file in bytes. Sparse files are counted by actual
2527 * allocated space. Return < 0 if error or unknown.
2528 */
2529int64_t bdrv_get_allocated_file_size(BlockDriverState *bs)
71d0770c 2530{
61007b31
SH
2531 BlockDriver *drv = bs->drv;
2532 if (!drv) {
2533 return -ENOMEDIUM;
8f4754ed 2534 }
61007b31
SH
2535 if (drv->bdrv_get_allocated_file_size) {
2536 return drv->bdrv_get_allocated_file_size(bs);
2537 }
2538 if (bs->file) {
9a4f4c31 2539 return bdrv_get_allocated_file_size(bs->file->bs);
1c9805a3 2540 }
61007b31 2541 return -ENOTSUP;
1c9805a3 2542}
e7a8a783 2543
61007b31
SH
2544/**
2545 * Return number of sectors on success, -errno on error.
1c9805a3 2546 */
61007b31 2547int64_t bdrv_nb_sectors(BlockDriverState *bs)
1c9805a3 2548{
61007b31 2549 BlockDriver *drv = bs->drv;
498e386c 2550
61007b31
SH
2551 if (!drv)
2552 return -ENOMEDIUM;
2572b37a 2553
61007b31
SH
2554 if (drv->has_variable_length) {
2555 int ret = refresh_total_sectors(bs, bs->total_sectors);
2556 if (ret < 0) {
2557 return ret;
1c9805a3
SH
2558 }
2559 }
61007b31 2560 return bs->total_sectors;
1c9805a3 2561}
b338082b 2562
61007b31
SH
2563/**
2564 * Return length in bytes on success, -errno on error.
2565 * The length is always a multiple of BDRV_SECTOR_SIZE.
8d3b1a2d 2566 */
61007b31 2567int64_t bdrv_getlength(BlockDriverState *bs)
8d3b1a2d 2568{
61007b31 2569 int64_t ret = bdrv_nb_sectors(bs);
8d3b1a2d 2570
4a9c9ea0 2571 ret = ret > INT64_MAX / BDRV_SECTOR_SIZE ? -EFBIG : ret;
61007b31 2572 return ret < 0 ? ret : ret * BDRV_SECTOR_SIZE;
fc01f7e7
FB
2573}
2574
61007b31
SH
2575/* return 0 as number of sectors if no device present or error */
2576void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
07d27a44 2577{
61007b31 2578 int64_t nb_sectors = bdrv_nb_sectors(bs);
07d27a44 2579
61007b31 2580 *nb_sectors_ptr = nb_sectors < 0 ? 0 : nb_sectors;
07d27a44
MA
2581}
2582
61007b31 2583int bdrv_is_read_only(BlockDriverState *bs)
8d3b1a2d 2584{
61007b31 2585 return bs->read_only;
83f64091 2586}
83f64091 2587
61007b31 2588int bdrv_is_sg(BlockDriverState *bs)
f08145fe 2589{
61007b31 2590 return bs->sg;
f08145fe
KW
2591}
2592
61007b31 2593int bdrv_enable_write_cache(BlockDriverState *bs)
ab185921 2594{
61007b31 2595 return bs->enable_write_cache;
ab185921
SH
2596}
2597
61007b31 2598void bdrv_set_enable_write_cache(BlockDriverState *bs, bool wce)
da1fa91d 2599{
61007b31 2600 bs->enable_write_cache = wce;
ab185921 2601
61007b31
SH
2602 /* so a reopen() will preserve wce */
2603 if (wce) {
2604 bs->open_flags |= BDRV_O_CACHE_WB;
893a8f62 2605 } else {
61007b31 2606 bs->open_flags &= ~BDRV_O_CACHE_WB;
893a8f62 2607 }
da1fa91d
KW
2608}
2609
61007b31 2610int bdrv_is_encrypted(BlockDriverState *bs)
fc3959e4 2611{
760e0063 2612 if (bs->backing && bs->backing->bs->encrypted) {
61007b31 2613 return 1;
760e0063 2614 }
61007b31 2615 return bs->encrypted;
fc3959e4
FZ
2616}
2617
61007b31 2618int bdrv_key_required(BlockDriverState *bs)
fc3959e4 2619{
760e0063 2620 BdrvChild *backing = bs->backing;
61007b31 2621
760e0063 2622 if (backing && backing->bs->encrypted && !backing->bs->valid_key) {
61007b31 2623 return 1;
760e0063 2624 }
61007b31 2625 return (bs->encrypted && !bs->valid_key);
fc3959e4
FZ
2626}
2627
61007b31 2628int bdrv_set_key(BlockDriverState *bs, const char *key)
d0c7f642 2629{
d0c7f642 2630 int ret;
760e0063
KW
2631 if (bs->backing && bs->backing->bs->encrypted) {
2632 ret = bdrv_set_key(bs->backing->bs, key);
61007b31
SH
2633 if (ret < 0)
2634 return ret;
2635 if (!bs->encrypted)
2636 return 0;
2637 }
2638 if (!bs->encrypted) {
2639 return -EINVAL;
2640 } else if (!bs->drv || !bs->drv->bdrv_set_key) {
d0c7f642
KW
2641 return -ENOMEDIUM;
2642 }
61007b31 2643 ret = bs->drv->bdrv_set_key(bs, key);
b9c64947 2644 if (ret < 0) {
61007b31
SH
2645 bs->valid_key = 0;
2646 } else if (!bs->valid_key) {
2647 bs->valid_key = 1;
2648 if (bs->blk) {
2649 /* call the change callback now, we skipped it on open */
2650 blk_dev_change_media_cb(bs->blk, true);
2651 }
1b0288ae 2652 }
61007b31
SH
2653 return ret;
2654}
f08f2dda 2655
c5fbe571 2656/*
61007b31
SH
2657 * Provide an encryption key for @bs.
2658 * If @key is non-null:
2659 * If @bs is not encrypted, fail.
2660 * Else if the key is invalid, fail.
2661 * Else set @bs's key to @key, replacing the existing key, if any.
2662 * If @key is null:
2663 * If @bs is encrypted and still lacks a key, fail.
2664 * Else do nothing.
2665 * On failure, store an error object through @errp if non-null.
c5fbe571 2666 */
61007b31 2667void bdrv_add_key(BlockDriverState *bs, const char *key, Error **errp)
c5fbe571 2668{
61007b31
SH
2669 if (key) {
2670 if (!bdrv_is_encrypted(bs)) {
2671 error_setg(errp, "Node '%s' is not encrypted",
2672 bdrv_get_device_or_node_name(bs));
2673 } else if (bdrv_set_key(bs, key) < 0) {
c6bd8c70 2674 error_setg(errp, QERR_INVALID_PASSWORD);
4d2855a3
MA
2675 }
2676 } else {
2677 if (bdrv_key_required(bs)) {
b1ca6391
MA
2678 error_set(errp, ERROR_CLASS_DEVICE_ENCRYPTED,
2679 "'%s' (%s) is encrypted",
81e5f78a 2680 bdrv_get_device_or_node_name(bs),
4d2855a3
MA
2681 bdrv_get_encrypted_filename(bs));
2682 }
2683 }
2684}
2685
61007b31 2686const char *bdrv_get_format_name(BlockDriverState *bs)
40b4f539 2687{
61007b31 2688 return bs->drv ? bs->drv->format_name : NULL;
40b4f539
KW
2689}
2690
61007b31 2691static int qsort_strcmp(const void *a, const void *b)
40b4f539 2692{
61007b31 2693 return strcmp(a, b);
40b4f539
KW
2694}
2695
61007b31
SH
2696void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
2697 void *opaque)
40b4f539 2698{
61007b31
SH
2699 BlockDriver *drv;
2700 int count = 0;
2701 int i;
2702 const char **formats = NULL;
40b4f539 2703
61007b31
SH
2704 QLIST_FOREACH(drv, &bdrv_drivers, list) {
2705 if (drv->format_name) {
2706 bool found = false;
2707 int i = count;
2708 while (formats && i && !found) {
2709 found = !strcmp(formats[--i], drv->format_name);
2710 }
e2a305fb 2711
61007b31
SH
2712 if (!found) {
2713 formats = g_renew(const char *, formats, count + 1);
2714 formats[count++] = drv->format_name;
2715 }
6c5a42ac 2716 }
61007b31 2717 }
6c5a42ac 2718
61007b31 2719 qsort(formats, count, sizeof(formats[0]), qsort_strcmp);
40b4f539 2720
61007b31
SH
2721 for (i = 0; i < count; i++) {
2722 it(opaque, formats[i]);
2723 }
40b4f539 2724
61007b31
SH
2725 g_free(formats);
2726}
40b4f539 2727
61007b31
SH
2728/* This function is to find a node in the bs graph */
2729BlockDriverState *bdrv_find_node(const char *node_name)
2730{
2731 BlockDriverState *bs;
391827eb 2732
61007b31 2733 assert(node_name);
40b4f539 2734
61007b31
SH
2735 QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) {
2736 if (!strcmp(node_name, bs->node_name)) {
2737 return bs;
40b4f539
KW
2738 }
2739 }
61007b31 2740 return NULL;
40b4f539
KW
2741}
2742
61007b31
SH
2743/* Put this QMP function here so it can access the static graph_bdrv_states. */
2744BlockDeviceInfoList *bdrv_named_nodes_list(Error **errp)
40b4f539 2745{
61007b31
SH
2746 BlockDeviceInfoList *list, *entry;
2747 BlockDriverState *bs;
40b4f539 2748
61007b31
SH
2749 list = NULL;
2750 QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) {
2751 BlockDeviceInfo *info = bdrv_block_device_info(bs, errp);
2752 if (!info) {
2753 qapi_free_BlockDeviceInfoList(list);
2754 return NULL;
301db7c2 2755 }
61007b31
SH
2756 entry = g_malloc0(sizeof(*entry));
2757 entry->value = info;
2758 entry->next = list;
2759 list = entry;
301db7c2
RH
2760 }
2761
61007b31
SH
2762 return list;
2763}
40b4f539 2764
61007b31
SH
2765BlockDriverState *bdrv_lookup_bs(const char *device,
2766 const char *node_name,
2767 Error **errp)
2768{
2769 BlockBackend *blk;
2770 BlockDriverState *bs;
40b4f539 2771
61007b31
SH
2772 if (device) {
2773 blk = blk_by_name(device);
40b4f539 2774
61007b31 2775 if (blk) {
9f4ed6fb
AG
2776 bs = blk_bs(blk);
2777 if (!bs) {
5433c24f 2778 error_setg(errp, "Device '%s' has no medium", device);
5433c24f
HR
2779 }
2780
9f4ed6fb 2781 return bs;
61007b31
SH
2782 }
2783 }
40b4f539 2784
61007b31
SH
2785 if (node_name) {
2786 bs = bdrv_find_node(node_name);
6d519a5f 2787
61007b31
SH
2788 if (bs) {
2789 return bs;
2790 }
40b4f539
KW
2791 }
2792
61007b31
SH
2793 error_setg(errp, "Cannot find device=%s nor node_name=%s",
2794 device ? device : "",
2795 node_name ? node_name : "");
2796 return NULL;
40b4f539
KW
2797}
2798
61007b31
SH
2799/* If 'base' is in the same chain as 'top', return true. Otherwise,
2800 * return false. If either argument is NULL, return false. */
2801bool bdrv_chain_contains(BlockDriverState *top, BlockDriverState *base)
83f64091 2802{
61007b31 2803 while (top && top != base) {
760e0063 2804 top = backing_bs(top);
02c50efe 2805 }
61007b31
SH
2806
2807 return top != NULL;
02c50efe
FZ
2808}
2809
61007b31 2810BlockDriverState *bdrv_next_node(BlockDriverState *bs)
02c50efe 2811{
61007b31
SH
2812 if (!bs) {
2813 return QTAILQ_FIRST(&graph_bdrv_states);
02c50efe 2814 }
61007b31 2815 return QTAILQ_NEXT(bs, node_list);
83f64091
FB
2816}
2817
61007b31 2818BlockDriverState *bdrv_next(BlockDriverState *bs)
83f64091 2819{
61007b31
SH
2820 if (!bs) {
2821 return QTAILQ_FIRST(&bdrv_states);
857d4f46 2822 }
61007b31 2823 return QTAILQ_NEXT(bs, device_list);
83f64091 2824}
beac80cd 2825
61007b31 2826const char *bdrv_get_node_name(const BlockDriverState *bs)
83f64091 2827{
61007b31 2828 return bs->node_name;
beac80cd
FB
2829}
2830
61007b31
SH
2831/* TODO check what callers really want: bs->node_name or blk_name() */
2832const char *bdrv_get_device_name(const BlockDriverState *bs)
beac80cd 2833{
61007b31 2834 return bs->blk ? blk_name(bs->blk) : "";
f141eafe 2835}
83f64091 2836
61007b31
SH
2837/* This can be used to identify nodes that might not have a device
2838 * name associated. Since node and device names live in the same
2839 * namespace, the result is unambiguous. The exception is if both are
2840 * absent, then this returns an empty (non-null) string. */
2841const char *bdrv_get_device_or_node_name(const BlockDriverState *bs)
f141eafe 2842{
61007b31 2843 return bs->blk ? blk_name(bs->blk) : bs->node_name;
beac80cd 2844}
beac80cd 2845
61007b31 2846int bdrv_get_flags(BlockDriverState *bs)
0b5a2445 2847{
61007b31 2848 return bs->open_flags;
0b5a2445
PB
2849}
2850
61007b31 2851int bdrv_has_zero_init_1(BlockDriverState *bs)
68485420 2852{
61007b31 2853 return 1;
0b5a2445
PB
2854}
2855
61007b31 2856int bdrv_has_zero_init(BlockDriverState *bs)
0b5a2445 2857{
61007b31 2858 assert(bs->drv);
0b5a2445 2859
61007b31
SH
2860 /* If BS is a copy on write image, it is initialized to
2861 the contents of the base image, which may not be zeroes. */
760e0063 2862 if (bs->backing) {
61007b31
SH
2863 return 0;
2864 }
2865 if (bs->drv->bdrv_has_zero_init) {
2866 return bs->drv->bdrv_has_zero_init(bs);
0b5a2445 2867 }
61007b31
SH
2868
2869 /* safe default */
2870 return 0;
68485420
KW
2871}
2872
61007b31 2873bool bdrv_unallocated_blocks_are_zero(BlockDriverState *bs)
b2a61371 2874{
61007b31 2875 BlockDriverInfo bdi;
b2a61371 2876
760e0063 2877 if (bs->backing) {
61007b31
SH
2878 return false;
2879 }
2880
2881 if (bdrv_get_info(bs, &bdi) == 0) {
2882 return bdi.unallocated_blocks_are_zero;
b2a61371
SH
2883 }
2884
61007b31 2885 return false;
b2a61371
SH
2886}
2887
61007b31 2888bool bdrv_can_write_zeroes_with_unmap(BlockDriverState *bs)
68485420 2889{
61007b31 2890 BlockDriverInfo bdi;
68485420 2891
760e0063 2892 if (bs->backing || !(bs->open_flags & BDRV_O_UNMAP)) {
61007b31
SH
2893 return false;
2894 }
68485420 2895
61007b31
SH
2896 if (bdrv_get_info(bs, &bdi) == 0) {
2897 return bdi.can_write_zeroes_with_unmap;
2898 }
68485420 2899
61007b31 2900 return false;
68485420
KW
2901}
2902
61007b31 2903const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
b2e12bc6 2904{
760e0063 2905 if (bs->backing && bs->backing->bs->encrypted)
61007b31
SH
2906 return bs->backing_file;
2907 else if (bs->encrypted)
2908 return bs->filename;
2909 else
2910 return NULL;
b2e12bc6
CH
2911}
2912
61007b31
SH
2913void bdrv_get_backing_filename(BlockDriverState *bs,
2914 char *filename, int filename_size)
016f5cf6 2915{
61007b31
SH
2916 pstrcpy(filename, filename_size, bs->backing_file);
2917}
d318aea9 2918
61007b31
SH
2919int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
2920{
2921 BlockDriver *drv = bs->drv;
2922 if (!drv)
2923 return -ENOMEDIUM;
2924 if (!drv->bdrv_get_info)
2925 return -ENOTSUP;
2926 memset(bdi, 0, sizeof(*bdi));
2927 return drv->bdrv_get_info(bs, bdi);
2928}
016f5cf6 2929
61007b31
SH
2930ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs)
2931{
2932 BlockDriver *drv = bs->drv;
2933 if (drv && drv->bdrv_get_specific_info) {
2934 return drv->bdrv_get_specific_info(bs);
2935 }
2936 return NULL;
016f5cf6
AG
2937}
2938
a31939e6 2939void bdrv_debug_event(BlockDriverState *bs, BlkdebugEvent event)
4265d620 2940{
61007b31
SH
2941 if (!bs || !bs->drv || !bs->drv->bdrv_debug_event) {
2942 return;
2943 }
4265d620 2944
61007b31 2945 bs->drv->bdrv_debug_event(bs, event);
4265d620
PB
2946}
2947
61007b31
SH
2948int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event,
2949 const char *tag)
4265d620 2950{
61007b31 2951 while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) {
9a4f4c31 2952 bs = bs->file ? bs->file->bs : NULL;
61007b31 2953 }
4265d620 2954
61007b31
SH
2955 if (bs && bs->drv && bs->drv->bdrv_debug_breakpoint) {
2956 return bs->drv->bdrv_debug_breakpoint(bs, event, tag);
2957 }
4265d620 2958
61007b31 2959 return -ENOTSUP;
4265d620
PB
2960}
2961
61007b31 2962int bdrv_debug_remove_breakpoint(BlockDriverState *bs, const char *tag)
ea2384d3 2963{
61007b31 2964 while (bs && bs->drv && !bs->drv->bdrv_debug_remove_breakpoint) {
9a4f4c31 2965 bs = bs->file ? bs->file->bs : NULL;
61007b31 2966 }
ce1a14dc 2967
61007b31
SH
2968 if (bs && bs->drv && bs->drv->bdrv_debug_remove_breakpoint) {
2969 return bs->drv->bdrv_debug_remove_breakpoint(bs, tag);
2970 }
2971
2972 return -ENOTSUP;
eb852011
MA
2973}
2974
61007b31 2975int bdrv_debug_resume(BlockDriverState *bs, const char *tag)
ce1a14dc 2976{
61007b31 2977 while (bs && (!bs->drv || !bs->drv->bdrv_debug_resume)) {
9a4f4c31 2978 bs = bs->file ? bs->file->bs : NULL;
61007b31 2979 }
ce1a14dc 2980
61007b31
SH
2981 if (bs && bs->drv && bs->drv->bdrv_debug_resume) {
2982 return bs->drv->bdrv_debug_resume(bs, tag);
2983 }
ce1a14dc 2984
61007b31 2985 return -ENOTSUP;
f197fe2b
FZ
2986}
2987
61007b31 2988bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag)
ce1a14dc 2989{
61007b31 2990 while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) {
9a4f4c31 2991 bs = bs->file ? bs->file->bs : NULL;
f197fe2b 2992 }
19cb3738 2993
61007b31
SH
2994 if (bs && bs->drv && bs->drv->bdrv_debug_is_suspended) {
2995 return bs->drv->bdrv_debug_is_suspended(bs, tag);
2996 }
f9f05dc5 2997
61007b31
SH
2998 return false;
2999}
f9f05dc5 3000
61007b31 3001int bdrv_is_snapshot(BlockDriverState *bs)
f9f05dc5 3002{
61007b31 3003 return !!(bs->open_flags & BDRV_O_SNAPSHOT);
f9f05dc5
KW
3004}
3005
61007b31
SH
3006/* backing_file can either be relative, or absolute, or a protocol. If it is
3007 * relative, it must be relative to the chain. So, passing in bs->filename
3008 * from a BDS as backing_file should not be done, as that may be relative to
3009 * the CWD rather than the chain. */
3010BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs,
3011 const char *backing_file)
f9f05dc5 3012{
61007b31
SH
3013 char *filename_full = NULL;
3014 char *backing_file_full = NULL;
3015 char *filename_tmp = NULL;
3016 int is_protocol = 0;
3017 BlockDriverState *curr_bs = NULL;
3018 BlockDriverState *retval = NULL;
f9f05dc5 3019
61007b31
SH
3020 if (!bs || !bs->drv || !backing_file) {
3021 return NULL;
f9f05dc5
KW
3022 }
3023
61007b31
SH
3024 filename_full = g_malloc(PATH_MAX);
3025 backing_file_full = g_malloc(PATH_MAX);
3026 filename_tmp = g_malloc(PATH_MAX);
f9f05dc5 3027
61007b31 3028 is_protocol = path_has_protocol(backing_file);
f9f05dc5 3029
760e0063 3030 for (curr_bs = bs; curr_bs->backing; curr_bs = curr_bs->backing->bs) {
f9f05dc5 3031
61007b31
SH
3032 /* If either of the filename paths is actually a protocol, then
3033 * compare unmodified paths; otherwise make paths relative */
3034 if (is_protocol || path_has_protocol(curr_bs->backing_file)) {
3035 if (strcmp(backing_file, curr_bs->backing_file) == 0) {
760e0063 3036 retval = curr_bs->backing->bs;
61007b31
SH
3037 break;
3038 }
3039 } else {
3040 /* If not an absolute filename path, make it relative to the current
3041 * image's filename path */
3042 path_combine(filename_tmp, PATH_MAX, curr_bs->filename,
3043 backing_file);
f9f05dc5 3044
61007b31
SH
3045 /* We are going to compare absolute pathnames */
3046 if (!realpath(filename_tmp, filename_full)) {
3047 continue;
3048 }
07f07615 3049
61007b31
SH
3050 /* We need to make sure the backing filename we are comparing against
3051 * is relative to the current image filename (or absolute) */
3052 path_combine(filename_tmp, PATH_MAX, curr_bs->filename,
3053 curr_bs->backing_file);
07f07615 3054
61007b31
SH
3055 if (!realpath(filename_tmp, backing_file_full)) {
3056 continue;
3057 }
eb489bb1 3058
61007b31 3059 if (strcmp(backing_file_full, filename_full) == 0) {
760e0063 3060 retval = curr_bs->backing->bs;
61007b31
SH
3061 break;
3062 }
3063 }
eb489bb1
KW
3064 }
3065
61007b31
SH
3066 g_free(filename_full);
3067 g_free(backing_file_full);
3068 g_free(filename_tmp);
3069 return retval;
3070}
3071
3072int bdrv_get_backing_file_depth(BlockDriverState *bs)
3073{
3074 if (!bs->drv) {
3075 return 0;
eb489bb1
KW
3076 }
3077
760e0063 3078 if (!bs->backing) {
61007b31 3079 return 0;
ca716364
KW
3080 }
3081
760e0063 3082 return 1 + bdrv_get_backing_file_depth(bs->backing->bs);
61007b31 3083}
07f07615 3084
61007b31
SH
3085void bdrv_init(void)
3086{
3087 module_call_init(MODULE_INIT_BLOCK);
3088}
29cdb251 3089
61007b31
SH
3090void bdrv_init_with_whitelist(void)
3091{
3092 use_bdrv_whitelist = 1;
3093 bdrv_init();
07f07615
PB
3094}
3095
5a8a30db 3096void bdrv_invalidate_cache(BlockDriverState *bs, Error **errp)
0f15423c 3097{
5a8a30db
KW
3098 Error *local_err = NULL;
3099 int ret;
3100
3456a8d1
KW
3101 if (!bs->drv) {
3102 return;
3103 }
3104
7ea2d269
AK
3105 if (!(bs->open_flags & BDRV_O_INCOMING)) {
3106 return;
3107 }
3108 bs->open_flags &= ~BDRV_O_INCOMING;
3109
3456a8d1 3110 if (bs->drv->bdrv_invalidate_cache) {
5a8a30db 3111 bs->drv->bdrv_invalidate_cache(bs, &local_err);
3456a8d1 3112 } else if (bs->file) {
9a4f4c31 3113 bdrv_invalidate_cache(bs->file->bs, &local_err);
5a8a30db
KW
3114 }
3115 if (local_err) {
3116 error_propagate(errp, local_err);
3117 return;
0f15423c 3118 }
3456a8d1 3119
5a8a30db
KW
3120 ret = refresh_total_sectors(bs, bs->total_sectors);
3121 if (ret < 0) {
3122 error_setg_errno(errp, -ret, "Could not refresh total sector count");
3123 return;
3124 }
0f15423c
AL
3125}
3126
5a8a30db 3127void bdrv_invalidate_cache_all(Error **errp)
0f15423c
AL
3128{
3129 BlockDriverState *bs;
5a8a30db 3130 Error *local_err = NULL;
0f15423c 3131
dc364f4c 3132 QTAILQ_FOREACH(bs, &bdrv_states, device_list) {
ed78cda3
SH
3133 AioContext *aio_context = bdrv_get_aio_context(bs);
3134
3135 aio_context_acquire(aio_context);
5a8a30db 3136 bdrv_invalidate_cache(bs, &local_err);
ed78cda3 3137 aio_context_release(aio_context);
5a8a30db
KW
3138 if (local_err) {
3139 error_propagate(errp, local_err);
3140 return;
3141 }
0f15423c
AL
3142 }
3143}
3144
19cb3738
FB
3145/**************************************************************/
3146/* removable device support */
3147
3148/**
3149 * Return TRUE if the media is present
3150 */
e031f750 3151bool bdrv_is_inserted(BlockDriverState *bs)
19cb3738
FB
3152{
3153 BlockDriver *drv = bs->drv;
28d7a789 3154 BdrvChild *child;
a1aff5bf 3155
e031f750
HR
3156 if (!drv) {
3157 return false;
3158 }
28d7a789
HR
3159 if (drv->bdrv_is_inserted) {
3160 return drv->bdrv_is_inserted(bs);
3161 }
3162 QLIST_FOREACH(child, &bs->children, next) {
3163 if (!bdrv_is_inserted(child->bs)) {
3164 return false;
3165 }
e031f750 3166 }
28d7a789 3167 return true;
19cb3738
FB
3168}
3169
3170/**
8e49ca46
MA
3171 * Return whether the media changed since the last call to this
3172 * function, or -ENOTSUP if we don't know. Most drivers don't know.
19cb3738
FB
3173 */
3174int bdrv_media_changed(BlockDriverState *bs)
3175{
3176 BlockDriver *drv = bs->drv;
19cb3738 3177
8e49ca46
MA
3178 if (drv && drv->bdrv_media_changed) {
3179 return drv->bdrv_media_changed(bs);
3180 }
3181 return -ENOTSUP;
19cb3738
FB
3182}
3183
3184/**
3185 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
3186 */
f36f3949 3187void bdrv_eject(BlockDriverState *bs, bool eject_flag)
19cb3738
FB
3188{
3189 BlockDriver *drv = bs->drv;
bfb197e0 3190 const char *device_name;
19cb3738 3191
822e1cd1
MA
3192 if (drv && drv->bdrv_eject) {
3193 drv->bdrv_eject(bs, eject_flag);
19cb3738 3194 }
6f382ed2 3195
bfb197e0
MA
3196 device_name = bdrv_get_device_name(bs);
3197 if (device_name[0] != '\0') {
3198 qapi_event_send_device_tray_moved(device_name,
a5ee7bd4 3199 eject_flag, &error_abort);
6f382ed2 3200 }
19cb3738
FB
3201}
3202
19cb3738
FB
3203/**
3204 * Lock or unlock the media (if it is locked, the user won't be able
3205 * to eject it manually).
3206 */
025e849a 3207void bdrv_lock_medium(BlockDriverState *bs, bool locked)
19cb3738
FB
3208{
3209 BlockDriver *drv = bs->drv;
3210
025e849a 3211 trace_bdrv_lock_medium(bs, locked);
b8c6d095 3212
025e849a
MA
3213 if (drv && drv->bdrv_lock_medium) {
3214 drv->bdrv_lock_medium(bs, locked);
19cb3738
FB
3215 }
3216}
985a03b0 3217
0db6e54a
FZ
3218BdrvDirtyBitmap *bdrv_find_dirty_bitmap(BlockDriverState *bs, const char *name)
3219{
3220 BdrvDirtyBitmap *bm;
3221
3222 assert(name);
3223 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
3224 if (bm->name && !strcmp(name, bm->name)) {
3225 return bm;
3226 }
3227 }
3228 return NULL;
3229}
3230
20dca810 3231void bdrv_dirty_bitmap_make_anon(BdrvDirtyBitmap *bitmap)
0db6e54a 3232{
9bd2b08f 3233 assert(!bdrv_dirty_bitmap_frozen(bitmap));
0db6e54a
FZ
3234 g_free(bitmap->name);
3235 bitmap->name = NULL;
3236}
3237
3238BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs,
5fba6c0e 3239 uint32_t granularity,
0db6e54a 3240 const char *name,
b8afb520 3241 Error **errp)
7cd1e32a
LS
3242{
3243 int64_t bitmap_size;
e4654d2d 3244 BdrvDirtyBitmap *bitmap;
5fba6c0e 3245 uint32_t sector_granularity;
a55eb92c 3246
50717e94
PB
3247 assert((granularity & (granularity - 1)) == 0);
3248
0db6e54a
FZ
3249 if (name && bdrv_find_dirty_bitmap(bs, name)) {
3250 error_setg(errp, "Bitmap already exists: %s", name);
3251 return NULL;
3252 }
5fba6c0e
JS
3253 sector_granularity = granularity >> BDRV_SECTOR_BITS;
3254 assert(sector_granularity);
57322b78 3255 bitmap_size = bdrv_nb_sectors(bs);
b8afb520
FZ
3256 if (bitmap_size < 0) {
3257 error_setg_errno(errp, -bitmap_size, "could not get length of device");
3258 errno = -bitmap_size;
3259 return NULL;
3260 }
5839e53b 3261 bitmap = g_new0(BdrvDirtyBitmap, 1);
5fba6c0e 3262 bitmap->bitmap = hbitmap_alloc(bitmap_size, ctz32(sector_granularity));
e74e6b78 3263 bitmap->size = bitmap_size;
0db6e54a 3264 bitmap->name = g_strdup(name);
b8e6fb75 3265 bitmap->disabled = false;
e4654d2d
FZ
3266 QLIST_INSERT_HEAD(&bs->dirty_bitmaps, bitmap, list);
3267 return bitmap;
3268}
3269
9bd2b08f
JS
3270bool bdrv_dirty_bitmap_frozen(BdrvDirtyBitmap *bitmap)
3271{
3272 return bitmap->successor;
3273}
3274
b8e6fb75
JS
3275bool bdrv_dirty_bitmap_enabled(BdrvDirtyBitmap *bitmap)
3276{
9bd2b08f
JS
3277 return !(bitmap->disabled || bitmap->successor);
3278}
3279
9abe3bdc
JS
3280DirtyBitmapStatus bdrv_dirty_bitmap_status(BdrvDirtyBitmap *bitmap)
3281{
3282 if (bdrv_dirty_bitmap_frozen(bitmap)) {
3283 return DIRTY_BITMAP_STATUS_FROZEN;
3284 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
3285 return DIRTY_BITMAP_STATUS_DISABLED;
3286 } else {
3287 return DIRTY_BITMAP_STATUS_ACTIVE;
3288 }
3289}
3290
9bd2b08f
JS
3291/**
3292 * Create a successor bitmap destined to replace this bitmap after an operation.
3293 * Requires that the bitmap is not frozen and has no successor.
3294 */
3295int bdrv_dirty_bitmap_create_successor(BlockDriverState *bs,
3296 BdrvDirtyBitmap *bitmap, Error **errp)
3297{
3298 uint64_t granularity;
3299 BdrvDirtyBitmap *child;
3300
3301 if (bdrv_dirty_bitmap_frozen(bitmap)) {
3302 error_setg(errp, "Cannot create a successor for a bitmap that is "
3303 "currently frozen");
3304 return -1;
3305 }
3306 assert(!bitmap->successor);
3307
3308 /* Create an anonymous successor */
3309 granularity = bdrv_dirty_bitmap_granularity(bitmap);
3310 child = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp);
3311 if (!child) {
3312 return -1;
3313 }
3314
3315 /* Successor will be on or off based on our current state. */
3316 child->disabled = bitmap->disabled;
3317
3318 /* Install the successor and freeze the parent */
3319 bitmap->successor = child;
3320 return 0;
3321}
3322
3323/**
3324 * For a bitmap with a successor, yield our name to the successor,
3325 * delete the old bitmap, and return a handle to the new bitmap.
3326 */
3327BdrvDirtyBitmap *bdrv_dirty_bitmap_abdicate(BlockDriverState *bs,
3328 BdrvDirtyBitmap *bitmap,
3329 Error **errp)
3330{
3331 char *name;
3332 BdrvDirtyBitmap *successor = bitmap->successor;
3333
3334 if (successor == NULL) {
3335 error_setg(errp, "Cannot relinquish control if "
3336 "there's no successor present");
3337 return NULL;
3338 }
3339
3340 name = bitmap->name;
3341 bitmap->name = NULL;
3342 successor->name = name;
3343 bitmap->successor = NULL;
3344 bdrv_release_dirty_bitmap(bs, bitmap);
3345
3346 return successor;
3347}
3348
3349/**
3350 * In cases of failure where we can no longer safely delete the parent,
3351 * we may wish to re-join the parent and child/successor.
3352 * The merged parent will be un-frozen, but not explicitly re-enabled.
3353 */
3354BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BlockDriverState *bs,
3355 BdrvDirtyBitmap *parent,
3356 Error **errp)
3357{
3358 BdrvDirtyBitmap *successor = parent->successor;
3359
3360 if (!successor) {
3361 error_setg(errp, "Cannot reclaim a successor when none is present");
3362 return NULL;
3363 }
3364
3365 if (!hbitmap_merge(parent->bitmap, successor->bitmap)) {
3366 error_setg(errp, "Merging of parent and successor bitmap failed");
3367 return NULL;
3368 }
3369 bdrv_release_dirty_bitmap(bs, successor);
3370 parent->successor = NULL;
3371
3372 return parent;
b8e6fb75
JS
3373}
3374
ce1ffea8
JS
3375/**
3376 * Truncates _all_ bitmaps attached to a BDS.
3377 */
3378static void bdrv_dirty_bitmap_truncate(BlockDriverState *bs)
3379{
3380 BdrvDirtyBitmap *bitmap;
3381 uint64_t size = bdrv_nb_sectors(bs);
3382
3383 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
06207b0f 3384 assert(!bdrv_dirty_bitmap_frozen(bitmap));
ce1ffea8 3385 hbitmap_truncate(bitmap->bitmap, size);
5270b6a0 3386 bitmap->size = size;
ce1ffea8
JS
3387 }
3388}
3389
e4654d2d
FZ
3390void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap)
3391{
3392 BdrvDirtyBitmap *bm, *next;
3393 QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) {
3394 if (bm == bitmap) {
9bd2b08f 3395 assert(!bdrv_dirty_bitmap_frozen(bm));
e4654d2d
FZ
3396 QLIST_REMOVE(bitmap, list);
3397 hbitmap_free(bitmap->bitmap);
0db6e54a 3398 g_free(bitmap->name);
e4654d2d
FZ
3399 g_free(bitmap);
3400 return;
a55eb92c 3401 }
7cd1e32a
LS
3402 }
3403}
3404
b8e6fb75
JS
3405void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
3406{
9bd2b08f 3407 assert(!bdrv_dirty_bitmap_frozen(bitmap));
b8e6fb75
JS
3408 bitmap->disabled = true;
3409}
3410
3411void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
3412{
9bd2b08f 3413 assert(!bdrv_dirty_bitmap_frozen(bitmap));
b8e6fb75
JS
3414 bitmap->disabled = false;
3415}
3416
21b56835
FZ
3417BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs)
3418{
3419 BdrvDirtyBitmap *bm;
3420 BlockDirtyInfoList *list = NULL;
3421 BlockDirtyInfoList **plist = &list;
3422
3423 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
5839e53b
MA
3424 BlockDirtyInfo *info = g_new0(BlockDirtyInfo, 1);
3425 BlockDirtyInfoList *entry = g_new0(BlockDirtyInfoList, 1);
20dca810 3426 info->count = bdrv_get_dirty_count(bm);
592fdd02 3427 info->granularity = bdrv_dirty_bitmap_granularity(bm);
0db6e54a
FZ
3428 info->has_name = !!bm->name;
3429 info->name = g_strdup(bm->name);
9abe3bdc 3430 info->status = bdrv_dirty_bitmap_status(bm);
21b56835
FZ
3431 entry->value = info;
3432 *plist = entry;
3433 plist = &entry->next;
3434 }
3435
3436 return list;
3437}
3438
e4654d2d 3439int bdrv_get_dirty(BlockDriverState *bs, BdrvDirtyBitmap *bitmap, int64_t sector)
7cd1e32a 3440{
e4654d2d
FZ
3441 if (bitmap) {
3442 return hbitmap_get(bitmap->bitmap, sector);
7cd1e32a
LS
3443 } else {
3444 return 0;
3445 }
3446}
3447
341ebc2f
JS
3448/**
3449 * Chooses a default granularity based on the existing cluster size,
3450 * but clamped between [4K, 64K]. Defaults to 64K in the case that there
3451 * is no cluster size information available.
3452 */
3453uint32_t bdrv_get_default_bitmap_granularity(BlockDriverState *bs)
3454{
3455 BlockDriverInfo bdi;
3456 uint32_t granularity;
3457
3458 if (bdrv_get_info(bs, &bdi) >= 0 && bdi.cluster_size > 0) {
3459 granularity = MAX(4096, bdi.cluster_size);
3460 granularity = MIN(65536, granularity);
3461 } else {
3462 granularity = 65536;
3463 }
3464
3465 return granularity;
3466}
3467
592fdd02
JS
3468uint32_t bdrv_dirty_bitmap_granularity(BdrvDirtyBitmap *bitmap)
3469{
3470 return BDRV_SECTOR_SIZE << hbitmap_granularity(bitmap->bitmap);
3471}
3472
20dca810 3473void bdrv_dirty_iter_init(BdrvDirtyBitmap *bitmap, HBitmapIter *hbi)
1755da16 3474{
e4654d2d 3475 hbitmap_iter_init(hbi, bitmap->bitmap, 0);
1755da16
PB
3476}
3477
20dca810 3478void bdrv_set_dirty_bitmap(BdrvDirtyBitmap *bitmap,
c4237dfa
VSO
3479 int64_t cur_sector, int nr_sectors)
3480{
b8e6fb75 3481 assert(bdrv_dirty_bitmap_enabled(bitmap));
c4237dfa
VSO
3482 hbitmap_set(bitmap->bitmap, cur_sector, nr_sectors);
3483}
3484
20dca810 3485void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap *bitmap,
c4237dfa
VSO
3486 int64_t cur_sector, int nr_sectors)
3487{
b8e6fb75 3488 assert(bdrv_dirty_bitmap_enabled(bitmap));
c4237dfa
VSO
3489 hbitmap_reset(bitmap->bitmap, cur_sector, nr_sectors);
3490}
3491
df9a681d 3492void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap **out)
e74e6b78
JS
3493{
3494 assert(bdrv_dirty_bitmap_enabled(bitmap));
df9a681d
FZ
3495 if (!out) {
3496 hbitmap_reset_all(bitmap->bitmap);
3497 } else {
3498 HBitmap *backup = bitmap->bitmap;
3499 bitmap->bitmap = hbitmap_alloc(bitmap->size,
3500 hbitmap_granularity(backup));
3501 *out = backup;
3502 }
3503}
3504
3505void bdrv_undo_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap *in)
3506{
3507 HBitmap *tmp = bitmap->bitmap;
3508 assert(bdrv_dirty_bitmap_enabled(bitmap));
3509 bitmap->bitmap = in;
3510 hbitmap_free(tmp);
e74e6b78
JS
3511}
3512
e0c47b6c
SH
3513void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector,
3514 int nr_sectors)
1755da16 3515{
e4654d2d
FZ
3516 BdrvDirtyBitmap *bitmap;
3517 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
b8e6fb75
JS
3518 if (!bdrv_dirty_bitmap_enabled(bitmap)) {
3519 continue;
3520 }
e4654d2d
FZ
3521 hbitmap_set(bitmap->bitmap, cur_sector, nr_sectors);
3522 }
1755da16
PB
3523}
3524
d58d8453
JS
3525/**
3526 * Advance an HBitmapIter to an arbitrary offset.
3527 */
3528void bdrv_set_dirty_iter(HBitmapIter *hbi, int64_t offset)
3529{
3530 assert(hbi->hb);
3531 hbitmap_iter_init(hbi, hbi->hb, offset);
3532}
3533
20dca810 3534int64_t bdrv_get_dirty_count(BdrvDirtyBitmap *bitmap)
aaa0eb75 3535{
e4654d2d 3536 return hbitmap_count(bitmap->bitmap);
aaa0eb75 3537}
f88e1a42 3538
9fcb0251
FZ
3539/* Get a reference to bs */
3540void bdrv_ref(BlockDriverState *bs)
3541{
3542 bs->refcnt++;
3543}
3544
3545/* Release a previously grabbed reference to bs.
3546 * If after releasing, reference count is zero, the BlockDriverState is
3547 * deleted. */
3548void bdrv_unref(BlockDriverState *bs)
3549{
9a4d5ca6
JC
3550 if (!bs) {
3551 return;
3552 }
9fcb0251
FZ
3553 assert(bs->refcnt > 0);
3554 if (--bs->refcnt == 0) {
3555 bdrv_delete(bs);
3556 }
3557}
3558
fbe40ff7
FZ
3559struct BdrvOpBlocker {
3560 Error *reason;
3561 QLIST_ENTRY(BdrvOpBlocker) list;
3562};
3563
3564bool bdrv_op_is_blocked(BlockDriverState *bs, BlockOpType op, Error **errp)
3565{
3566 BdrvOpBlocker *blocker;
3567 assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
3568 if (!QLIST_EMPTY(&bs->op_blockers[op])) {
3569 blocker = QLIST_FIRST(&bs->op_blockers[op]);
3570 if (errp) {
81e5f78a
AG
3571 error_setg(errp, "Node '%s' is busy: %s",
3572 bdrv_get_device_or_node_name(bs),
bfb197e0 3573 error_get_pretty(blocker->reason));
fbe40ff7
FZ
3574 }
3575 return true;
3576 }
3577 return false;
3578}
3579
3580void bdrv_op_block(BlockDriverState *bs, BlockOpType op, Error *reason)
3581{
3582 BdrvOpBlocker *blocker;
3583 assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
3584
5839e53b 3585 blocker = g_new0(BdrvOpBlocker, 1);
fbe40ff7
FZ
3586 blocker->reason = reason;
3587 QLIST_INSERT_HEAD(&bs->op_blockers[op], blocker, list);
3588}
3589
3590void bdrv_op_unblock(BlockDriverState *bs, BlockOpType op, Error *reason)
3591{
3592 BdrvOpBlocker *blocker, *next;
3593 assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
3594 QLIST_FOREACH_SAFE(blocker, &bs->op_blockers[op], list, next) {
3595 if (blocker->reason == reason) {
3596 QLIST_REMOVE(blocker, list);
3597 g_free(blocker);
3598 }
3599 }
3600}
3601
3602void bdrv_op_block_all(BlockDriverState *bs, Error *reason)
3603{
3604 int i;
3605 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
3606 bdrv_op_block(bs, i, reason);
3607 }
3608}
3609
3610void bdrv_op_unblock_all(BlockDriverState *bs, Error *reason)
3611{
3612 int i;
3613 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
3614 bdrv_op_unblock(bs, i, reason);
3615 }
3616}
3617
3618bool bdrv_op_blocker_is_empty(BlockDriverState *bs)
3619{
3620 int i;
3621
3622 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
3623 if (!QLIST_EMPTY(&bs->op_blockers[i])) {
3624 return false;
3625 }
3626 }
3627 return true;
3628}
3629
d92ada22
LC
3630void bdrv_img_create(const char *filename, const char *fmt,
3631 const char *base_filename, const char *base_fmt,
f382d43a
MR
3632 char *options, uint64_t img_size, int flags,
3633 Error **errp, bool quiet)
f88e1a42 3634{
83d0521a
CL
3635 QemuOptsList *create_opts = NULL;
3636 QemuOpts *opts = NULL;
3637 const char *backing_fmt, *backing_file;
3638 int64_t size;
f88e1a42 3639 BlockDriver *drv, *proto_drv;
cc84d90f 3640 Error *local_err = NULL;
f88e1a42
JS
3641 int ret = 0;
3642
3643 /* Find driver and parse its options */
3644 drv = bdrv_find_format(fmt);
3645 if (!drv) {
71c79813 3646 error_setg(errp, "Unknown file format '%s'", fmt);
d92ada22 3647 return;
f88e1a42
JS
3648 }
3649
b65a5e12 3650 proto_drv = bdrv_find_protocol(filename, true, errp);
f88e1a42 3651 if (!proto_drv) {
d92ada22 3652 return;
f88e1a42
JS
3653 }
3654
c6149724
HR
3655 if (!drv->create_opts) {
3656 error_setg(errp, "Format driver '%s' does not support image creation",
3657 drv->format_name);
3658 return;
3659 }
3660
3661 if (!proto_drv->create_opts) {
3662 error_setg(errp, "Protocol driver '%s' does not support image creation",
3663 proto_drv->format_name);
3664 return;
3665 }
3666
c282e1fd
CL
3667 create_opts = qemu_opts_append(create_opts, drv->create_opts);
3668 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
f88e1a42
JS
3669
3670 /* Create parameter list with default values */
83d0521a 3671 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
39101f25 3672 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
f88e1a42
JS
3673
3674 /* Parse -o options */
3675 if (options) {
dc523cd3
MA
3676 qemu_opts_do_parse(opts, options, NULL, &local_err);
3677 if (local_err) {
3678 error_report_err(local_err);
3679 local_err = NULL;
83d0521a 3680 error_setg(errp, "Invalid options for file format '%s'", fmt);
f88e1a42
JS
3681 goto out;
3682 }
3683 }
3684
3685 if (base_filename) {
f43e47db 3686 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &local_err);
6be4194b 3687 if (local_err) {
71c79813
LC
3688 error_setg(errp, "Backing file not supported for file format '%s'",
3689 fmt);
f88e1a42
JS
3690 goto out;
3691 }
3692 }
3693
3694 if (base_fmt) {
f43e47db 3695 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &local_err);
6be4194b 3696 if (local_err) {
71c79813
LC
3697 error_setg(errp, "Backing file format not supported for file "
3698 "format '%s'", fmt);
f88e1a42
JS
3699 goto out;
3700 }
3701 }
3702
83d0521a
CL
3703 backing_file = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
3704 if (backing_file) {
3705 if (!strcmp(filename, backing_file)) {
71c79813
LC
3706 error_setg(errp, "Error: Trying to create an image with the "
3707 "same filename as the backing file");
792da93a
JS
3708 goto out;
3709 }
3710 }
3711
83d0521a 3712 backing_fmt = qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT);
f88e1a42
JS
3713
3714 // The size for the image must always be specified, with one exception:
3715 // If we are using a backing file, we can obtain the size from there
83d0521a
CL
3716 size = qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 0);
3717 if (size == -1) {
3718 if (backing_file) {
66f6b814 3719 BlockDriverState *bs;
29168018 3720 char *full_backing = g_new0(char, PATH_MAX);
52bf1e72 3721 int64_t size;
63090dac 3722 int back_flags;
e6641719 3723 QDict *backing_options = NULL;
63090dac 3724
29168018
HR
3725 bdrv_get_full_backing_filename_from_filename(filename, backing_file,
3726 full_backing, PATH_MAX,
3727 &local_err);
3728 if (local_err) {
3729 g_free(full_backing);
3730 goto out;
3731 }
3732
63090dac
PB
3733 /* backing files always opened read-only */
3734 back_flags =
3735 flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
f88e1a42 3736
e6641719
HR
3737 if (backing_fmt) {
3738 backing_options = qdict_new();
3739 qdict_put(backing_options, "driver",
3740 qstring_from_str(backing_fmt));
3741 }
3742
f67503e5 3743 bs = NULL;
e6641719 3744 ret = bdrv_open(&bs, full_backing, NULL, backing_options,
6ebf9aa2 3745 back_flags, &local_err);
29168018 3746 g_free(full_backing);
f88e1a42 3747 if (ret < 0) {
f88e1a42
JS
3748 goto out;
3749 }
52bf1e72
MA
3750 size = bdrv_getlength(bs);
3751 if (size < 0) {
3752 error_setg_errno(errp, -size, "Could not get size of '%s'",
3753 backing_file);
3754 bdrv_unref(bs);
3755 goto out;
3756 }
f88e1a42 3757
39101f25 3758 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, size, &error_abort);
66f6b814
HR
3759
3760 bdrv_unref(bs);
f88e1a42 3761 } else {
71c79813 3762 error_setg(errp, "Image creation needs a size parameter");
f88e1a42
JS
3763 goto out;
3764 }
3765 }
3766
f382d43a 3767 if (!quiet) {
fe646693 3768 printf("Formatting '%s', fmt=%s ", filename, fmt);
43c5d8f8 3769 qemu_opts_print(opts, " ");
f382d43a
MR
3770 puts("");
3771 }
83d0521a 3772
c282e1fd 3773 ret = bdrv_create(drv, filename, opts, &local_err);
83d0521a 3774
cc84d90f
HR
3775 if (ret == -EFBIG) {
3776 /* This is generally a better message than whatever the driver would
3777 * deliver (especially because of the cluster_size_hint), since that
3778 * is most probably not much different from "image too large". */
3779 const char *cluster_size_hint = "";
83d0521a 3780 if (qemu_opt_get_size(opts, BLOCK_OPT_CLUSTER_SIZE, 0)) {
cc84d90f 3781 cluster_size_hint = " (try using a larger cluster size)";
f88e1a42 3782 }
cc84d90f
HR
3783 error_setg(errp, "The image size is too large for file format '%s'"
3784 "%s", fmt, cluster_size_hint);
3785 error_free(local_err);
3786 local_err = NULL;
f88e1a42
JS
3787 }
3788
3789out:
83d0521a
CL
3790 qemu_opts_del(opts);
3791 qemu_opts_free(create_opts);
84d18f06 3792 if (local_err) {
cc84d90f
HR
3793 error_propagate(errp, local_err);
3794 }
f88e1a42 3795}
85d126f3
SH
3796
3797AioContext *bdrv_get_aio_context(BlockDriverState *bs)
3798{
dcd04228
SH
3799 return bs->aio_context;
3800}
3801
3802void bdrv_detach_aio_context(BlockDriverState *bs)
3803{
33384421
HR
3804 BdrvAioNotifier *baf;
3805
dcd04228
SH
3806 if (!bs->drv) {
3807 return;
3808 }
3809
33384421
HR
3810 QLIST_FOREACH(baf, &bs->aio_notifiers, list) {
3811 baf->detach_aio_context(baf->opaque);
3812 }
3813
a0d64a61 3814 if (bs->throttle_state) {
0e5b0a2d 3815 throttle_timers_detach_aio_context(&bs->throttle_timers);
13af91eb 3816 }
dcd04228
SH
3817 if (bs->drv->bdrv_detach_aio_context) {
3818 bs->drv->bdrv_detach_aio_context(bs);
3819 }
3820 if (bs->file) {
9a4f4c31 3821 bdrv_detach_aio_context(bs->file->bs);
dcd04228 3822 }
760e0063
KW
3823 if (bs->backing) {
3824 bdrv_detach_aio_context(bs->backing->bs);
dcd04228
SH
3825 }
3826
3827 bs->aio_context = NULL;
3828}
3829
3830void bdrv_attach_aio_context(BlockDriverState *bs,
3831 AioContext *new_context)
3832{
33384421
HR
3833 BdrvAioNotifier *ban;
3834
dcd04228
SH
3835 if (!bs->drv) {
3836 return;
3837 }
3838
3839 bs->aio_context = new_context;
3840
760e0063
KW
3841 if (bs->backing) {
3842 bdrv_attach_aio_context(bs->backing->bs, new_context);
dcd04228
SH
3843 }
3844 if (bs->file) {
9a4f4c31 3845 bdrv_attach_aio_context(bs->file->bs, new_context);
dcd04228
SH
3846 }
3847 if (bs->drv->bdrv_attach_aio_context) {
3848 bs->drv->bdrv_attach_aio_context(bs, new_context);
3849 }
a0d64a61 3850 if (bs->throttle_state) {
0e5b0a2d 3851 throttle_timers_attach_aio_context(&bs->throttle_timers, new_context);
13af91eb 3852 }
33384421
HR
3853
3854 QLIST_FOREACH(ban, &bs->aio_notifiers, list) {
3855 ban->attached_aio_context(new_context, ban->opaque);
3856 }
dcd04228
SH
3857}
3858
3859void bdrv_set_aio_context(BlockDriverState *bs, AioContext *new_context)
3860{
53ec73e2 3861 bdrv_drain(bs); /* ensure there are no in-flight requests */
dcd04228
SH
3862
3863 bdrv_detach_aio_context(bs);
3864
3865 /* This function executes in the old AioContext so acquire the new one in
3866 * case it runs in a different thread.
3867 */
3868 aio_context_acquire(new_context);
3869 bdrv_attach_aio_context(bs, new_context);
3870 aio_context_release(new_context);
85d126f3 3871}
d616b224 3872
33384421
HR
3873void bdrv_add_aio_context_notifier(BlockDriverState *bs,
3874 void (*attached_aio_context)(AioContext *new_context, void *opaque),
3875 void (*detach_aio_context)(void *opaque), void *opaque)
3876{
3877 BdrvAioNotifier *ban = g_new(BdrvAioNotifier, 1);
3878 *ban = (BdrvAioNotifier){
3879 .attached_aio_context = attached_aio_context,
3880 .detach_aio_context = detach_aio_context,
3881 .opaque = opaque
3882 };
3883
3884 QLIST_INSERT_HEAD(&bs->aio_notifiers, ban, list);
3885}
3886
3887void bdrv_remove_aio_context_notifier(BlockDriverState *bs,
3888 void (*attached_aio_context)(AioContext *,
3889 void *),
3890 void (*detach_aio_context)(void *),
3891 void *opaque)
3892{
3893 BdrvAioNotifier *ban, *ban_next;
3894
3895 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) {
3896 if (ban->attached_aio_context == attached_aio_context &&
3897 ban->detach_aio_context == detach_aio_context &&
3898 ban->opaque == opaque)
3899 {
3900 QLIST_REMOVE(ban, list);
3901 g_free(ban);
3902
3903 return;
3904 }
3905 }
3906
3907 abort();
3908}
3909
77485434
HR
3910int bdrv_amend_options(BlockDriverState *bs, QemuOpts *opts,
3911 BlockDriverAmendStatusCB *status_cb)
6f176b48 3912{
c282e1fd 3913 if (!bs->drv->bdrv_amend_options) {
6f176b48
HR
3914 return -ENOTSUP;
3915 }
77485434 3916 return bs->drv->bdrv_amend_options(bs, opts, status_cb);
6f176b48 3917}
f6186f49 3918
b5042a36
BC
3919/* This function will be called by the bdrv_recurse_is_first_non_filter method
3920 * of block filter and by bdrv_is_first_non_filter.
3921 * It is used to test if the given bs is the candidate or recurse more in the
3922 * node graph.
212a5a8f 3923 */
b5042a36 3924bool bdrv_recurse_is_first_non_filter(BlockDriverState *bs,
212a5a8f 3925 BlockDriverState *candidate)
f6186f49 3926{
b5042a36
BC
3927 /* return false if basic checks fails */
3928 if (!bs || !bs->drv) {
212a5a8f 3929 return false;
f6186f49
BC
3930 }
3931
b5042a36
BC
3932 /* the code reached a non block filter driver -> check if the bs is
3933 * the same as the candidate. It's the recursion termination condition.
3934 */
3935 if (!bs->drv->is_filter) {
3936 return bs == candidate;
212a5a8f 3937 }
b5042a36 3938 /* Down this path the driver is a block filter driver */
212a5a8f 3939
b5042a36
BC
3940 /* If the block filter recursion method is defined use it to recurse down
3941 * the node graph.
3942 */
3943 if (bs->drv->bdrv_recurse_is_first_non_filter) {
212a5a8f 3944 return bs->drv->bdrv_recurse_is_first_non_filter(bs, candidate);
f6186f49
BC
3945 }
3946
b5042a36
BC
3947 /* the driver is a block filter but don't allow to recurse -> return false
3948 */
3949 return false;
f6186f49
BC
3950}
3951
212a5a8f
BC
3952/* This function checks if the candidate is the first non filter bs down it's
3953 * bs chain. Since we don't have pointers to parents it explore all bs chains
3954 * from the top. Some filters can choose not to pass down the recursion.
3955 */
3956bool bdrv_is_first_non_filter(BlockDriverState *candidate)
f6186f49 3957{
212a5a8f
BC
3958 BlockDriverState *bs;
3959
3960 /* walk down the bs forest recursively */
3961 QTAILQ_FOREACH(bs, &bdrv_states, device_list) {
3962 bool perm;
3963
b5042a36 3964 /* try to recurse in this top level bs */
e6dc8a1f 3965 perm = bdrv_recurse_is_first_non_filter(bs, candidate);
212a5a8f
BC
3966
3967 /* candidate is the first non filter */
3968 if (perm) {
3969 return true;
3970 }
3971 }
3972
3973 return false;
f6186f49 3974}
09158f00 3975
e12f3784
WC
3976BlockDriverState *check_to_replace_node(BlockDriverState *parent_bs,
3977 const char *node_name, Error **errp)
09158f00
BC
3978{
3979 BlockDriverState *to_replace_bs = bdrv_find_node(node_name);
5a7e7a0b
SH
3980 AioContext *aio_context;
3981
09158f00
BC
3982 if (!to_replace_bs) {
3983 error_setg(errp, "Node name '%s' not found", node_name);
3984 return NULL;
3985 }
3986
5a7e7a0b
SH
3987 aio_context = bdrv_get_aio_context(to_replace_bs);
3988 aio_context_acquire(aio_context);
3989
09158f00 3990 if (bdrv_op_is_blocked(to_replace_bs, BLOCK_OP_TYPE_REPLACE, errp)) {
5a7e7a0b
SH
3991 to_replace_bs = NULL;
3992 goto out;
09158f00
BC
3993 }
3994
3995 /* We don't want arbitrary node of the BDS chain to be replaced only the top
3996 * most non filter in order to prevent data corruption.
3997 * Another benefit is that this tests exclude backing files which are
3998 * blocked by the backing blockers.
3999 */
e12f3784 4000 if (!bdrv_recurse_is_first_non_filter(parent_bs, to_replace_bs)) {
09158f00 4001 error_setg(errp, "Only top most non filter can be replaced");
5a7e7a0b
SH
4002 to_replace_bs = NULL;
4003 goto out;
09158f00
BC
4004 }
4005
5a7e7a0b
SH
4006out:
4007 aio_context_release(aio_context);
09158f00
BC
4008 return to_replace_bs;
4009}
448ad91d 4010
91af7014
HR
4011static bool append_open_options(QDict *d, BlockDriverState *bs)
4012{
4013 const QDictEntry *entry;
9e700c1a 4014 QemuOptDesc *desc;
260fecf1 4015 BdrvChild *child;
91af7014 4016 bool found_any = false;
260fecf1 4017 const char *p;
91af7014
HR
4018
4019 for (entry = qdict_first(bs->options); entry;
4020 entry = qdict_next(bs->options, entry))
4021 {
260fecf1
KW
4022 /* Exclude options for children */
4023 QLIST_FOREACH(child, &bs->children, next) {
4024 if (strstart(qdict_entry_key(entry), child->name, &p)
4025 && (!*p || *p == '.'))
4026 {
4027 break;
4028 }
4029 }
4030 if (child) {
9e700c1a 4031 continue;
91af7014 4032 }
9e700c1a
KW
4033
4034 /* And exclude all non-driver-specific options */
4035 for (desc = bdrv_runtime_opts.desc; desc->name; desc++) {
4036 if (!strcmp(qdict_entry_key(entry), desc->name)) {
4037 break;
4038 }
4039 }
4040 if (desc->name) {
4041 continue;
4042 }
4043
4044 qobject_incref(qdict_entry_value(entry));
4045 qdict_put_obj(d, qdict_entry_key(entry), qdict_entry_value(entry));
4046 found_any = true;
91af7014
HR
4047 }
4048
4049 return found_any;
4050}
4051
4052/* Updates the following BDS fields:
4053 * - exact_filename: A filename which may be used for opening a block device
4054 * which (mostly) equals the given BDS (even without any
4055 * other options; so reading and writing must return the same
4056 * results, but caching etc. may be different)
4057 * - full_open_options: Options which, when given when opening a block device
4058 * (without a filename), result in a BDS (mostly)
4059 * equalling the given one
4060 * - filename: If exact_filename is set, it is copied here. Otherwise,
4061 * full_open_options is converted to a JSON object, prefixed with
4062 * "json:" (for use through the JSON pseudo protocol) and put here.
4063 */
4064void bdrv_refresh_filename(BlockDriverState *bs)
4065{
4066 BlockDriver *drv = bs->drv;
4067 QDict *opts;
4068
4069 if (!drv) {
4070 return;
4071 }
4072
4073 /* This BDS's file name will most probably depend on its file's name, so
4074 * refresh that first */
4075 if (bs->file) {
9a4f4c31 4076 bdrv_refresh_filename(bs->file->bs);
91af7014
HR
4077 }
4078
4079 if (drv->bdrv_refresh_filename) {
4080 /* Obsolete information is of no use here, so drop the old file name
4081 * information before refreshing it */
4082 bs->exact_filename[0] = '\0';
4083 if (bs->full_open_options) {
4084 QDECREF(bs->full_open_options);
4085 bs->full_open_options = NULL;
4086 }
4087
4cdd01d3
KW
4088 opts = qdict_new();
4089 append_open_options(opts, bs);
4090 drv->bdrv_refresh_filename(bs, opts);
4091 QDECREF(opts);
91af7014
HR
4092 } else if (bs->file) {
4093 /* Try to reconstruct valid information from the underlying file */
4094 bool has_open_options;
4095
4096 bs->exact_filename[0] = '\0';
4097 if (bs->full_open_options) {
4098 QDECREF(bs->full_open_options);
4099 bs->full_open_options = NULL;
4100 }
4101
4102 opts = qdict_new();
4103 has_open_options = append_open_options(opts, bs);
4104
4105 /* If no specific options have been given for this BDS, the filename of
4106 * the underlying file should suffice for this one as well */
9a4f4c31
KW
4107 if (bs->file->bs->exact_filename[0] && !has_open_options) {
4108 strcpy(bs->exact_filename, bs->file->bs->exact_filename);
91af7014
HR
4109 }
4110 /* Reconstructing the full options QDict is simple for most format block
4111 * drivers, as long as the full options are known for the underlying
4112 * file BDS. The full options QDict of that file BDS should somehow
4113 * contain a representation of the filename, therefore the following
4114 * suffices without querying the (exact_)filename of this BDS. */
9a4f4c31 4115 if (bs->file->bs->full_open_options) {
91af7014
HR
4116 qdict_put_obj(opts, "driver",
4117 QOBJECT(qstring_from_str(drv->format_name)));
9a4f4c31
KW
4118 QINCREF(bs->file->bs->full_open_options);
4119 qdict_put_obj(opts, "file",
4120 QOBJECT(bs->file->bs->full_open_options));
91af7014
HR
4121
4122 bs->full_open_options = opts;
4123 } else {
4124 QDECREF(opts);
4125 }
4126 } else if (!bs->full_open_options && qdict_size(bs->options)) {
4127 /* There is no underlying file BDS (at least referenced by BDS.file),
4128 * so the full options QDict should be equal to the options given
4129 * specifically for this block device when it was opened (plus the
4130 * driver specification).
4131 * Because those options don't change, there is no need to update
4132 * full_open_options when it's already set. */
4133
4134 opts = qdict_new();
4135 append_open_options(opts, bs);
4136 qdict_put_obj(opts, "driver",
4137 QOBJECT(qstring_from_str(drv->format_name)));
4138
4139 if (bs->exact_filename[0]) {
4140 /* This may not work for all block protocol drivers (some may
4141 * require this filename to be parsed), but we have to find some
4142 * default solution here, so just include it. If some block driver
4143 * does not support pure options without any filename at all or
4144 * needs some special format of the options QDict, it needs to
4145 * implement the driver-specific bdrv_refresh_filename() function.
4146 */
4147 qdict_put_obj(opts, "filename",
4148 QOBJECT(qstring_from_str(bs->exact_filename)));
4149 }
4150
4151 bs->full_open_options = opts;
4152 }
4153
4154 if (bs->exact_filename[0]) {
4155 pstrcpy(bs->filename, sizeof(bs->filename), bs->exact_filename);
4156 } else if (bs->full_open_options) {
4157 QString *json = qobject_to_json(QOBJECT(bs->full_open_options));
4158 snprintf(bs->filename, sizeof(bs->filename), "json:%s",
4159 qstring_get_str(json));
4160 QDECREF(json);
4161 }
4162}