]> git.ipfire.org Git - thirdparty/qemu.git/blame - block.c
block: Allow specifying child options in reopen
[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 */
1696BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue,
4d2cb092
KW
1697 BlockDriverState *bs,
1698 QDict *options, int flags)
e971aa12
JC
1699{
1700 assert(bs != NULL);
1701
1702 BlockReopenQueueEntry *bs_entry;
67251a31 1703 BdrvChild *child;
4d2cb092 1704 QDict *old_options;
67251a31 1705
e971aa12
JC
1706 if (bs_queue == NULL) {
1707 bs_queue = g_new0(BlockReopenQueue, 1);
1708 QSIMPLEQ_INIT(bs_queue);
1709 }
1710
4d2cb092
KW
1711 if (!options) {
1712 options = qdict_new();
1713 }
1714
1715 old_options = qdict_clone_shallow(bs->options);
cddff5ba 1716 bdrv_join_options(bs, options, old_options);
4d2cb092
KW
1717 QDECREF(old_options);
1718
f1f25a2e
KW
1719 /* bdrv_open() masks this flag out */
1720 flags &= ~BDRV_O_PROTOCOL;
1721
67251a31 1722 QLIST_FOREACH(child, &bs->children, next) {
4c9dfe5d
KW
1723 QDict *new_child_options;
1724 char *child_key_dot;
67251a31
KW
1725 int child_flags;
1726
4c9dfe5d
KW
1727 /* reopen can only change the options of block devices that were
1728 * implicitly created and inherited options. For other (referenced)
1729 * block devices, a syntax like "backing.foo" results in an error. */
67251a31
KW
1730 if (child->bs->inherits_from != bs) {
1731 continue;
1732 }
1733
4c9dfe5d
KW
1734 child_key_dot = g_strdup_printf("%s.", child->name);
1735 qdict_extract_subqdict(options, &new_child_options, child_key_dot);
1736 g_free(child_key_dot);
1737
67251a31 1738 child_flags = child->role->inherit_flags(flags);
4c9dfe5d 1739 bdrv_reopen_queue(bs_queue, child->bs, new_child_options, child_flags);
e971aa12
JC
1740 }
1741
1742 bs_entry = g_new0(BlockReopenQueueEntry, 1);
1743 QSIMPLEQ_INSERT_TAIL(bs_queue, bs_entry, entry);
1744
1745 bs_entry->state.bs = bs;
4d2cb092 1746 bs_entry->state.options = options;
e971aa12
JC
1747 bs_entry->state.flags = flags;
1748
1749 return bs_queue;
1750}
1751
1752/*
1753 * Reopen multiple BlockDriverStates atomically & transactionally.
1754 *
1755 * The queue passed in (bs_queue) must have been built up previous
1756 * via bdrv_reopen_queue().
1757 *
1758 * Reopens all BDS specified in the queue, with the appropriate
1759 * flags. All devices are prepared for reopen, and failure of any
1760 * device will cause all device changes to be abandonded, and intermediate
1761 * data cleaned up.
1762 *
1763 * If all devices prepare successfully, then the changes are committed
1764 * to all devices.
1765 *
1766 */
1767int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp)
1768{
1769 int ret = -1;
1770 BlockReopenQueueEntry *bs_entry, *next;
1771 Error *local_err = NULL;
1772
1773 assert(bs_queue != NULL);
1774
1775 bdrv_drain_all();
1776
1777 QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) {
1778 if (bdrv_reopen_prepare(&bs_entry->state, bs_queue, &local_err)) {
1779 error_propagate(errp, local_err);
1780 goto cleanup;
1781 }
1782 bs_entry->prepared = true;
1783 }
1784
1785 /* If we reach this point, we have success and just need to apply the
1786 * changes
1787 */
1788 QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) {
1789 bdrv_reopen_commit(&bs_entry->state);
1790 }
1791
1792 ret = 0;
1793
1794cleanup:
1795 QSIMPLEQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) {
1796 if (ret && bs_entry->prepared) {
1797 bdrv_reopen_abort(&bs_entry->state);
1798 }
4d2cb092 1799 QDECREF(bs_entry->state.options);
e971aa12
JC
1800 g_free(bs_entry);
1801 }
1802 g_free(bs_queue);
1803 return ret;
1804}
1805
1806
1807/* Reopen a single BlockDriverState with the specified flags. */
1808int bdrv_reopen(BlockDriverState *bs, int bdrv_flags, Error **errp)
1809{
1810 int ret = -1;
1811 Error *local_err = NULL;
4d2cb092 1812 BlockReopenQueue *queue = bdrv_reopen_queue(NULL, bs, NULL, bdrv_flags);
e971aa12
JC
1813
1814 ret = bdrv_reopen_multiple(queue, &local_err);
1815 if (local_err != NULL) {
1816 error_propagate(errp, local_err);
1817 }
1818 return ret;
1819}
1820
1821
1822/*
1823 * Prepares a BlockDriverState for reopen. All changes are staged in the
1824 * 'opaque' field of the BDRVReopenState, which is used and allocated by
1825 * the block driver layer .bdrv_reopen_prepare()
1826 *
1827 * bs is the BlockDriverState to reopen
1828 * flags are the new open flags
1829 * queue is the reopen queue
1830 *
1831 * Returns 0 on success, non-zero on error. On error errp will be set
1832 * as well.
1833 *
1834 * On failure, bdrv_reopen_abort() will be called to clean up any data.
1835 * It is the responsibility of the caller to then call the abort() or
1836 * commit() for any other BDS that have been left in a prepare() state
1837 *
1838 */
1839int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue,
1840 Error **errp)
1841{
1842 int ret = -1;
1843 Error *local_err = NULL;
1844 BlockDriver *drv;
1845
1846 assert(reopen_state != NULL);
1847 assert(reopen_state->bs->drv != NULL);
1848 drv = reopen_state->bs->drv;
1849
1850 /* if we are to stay read-only, do not allow permission change
1851 * to r/w */
1852 if (!(reopen_state->bs->open_flags & BDRV_O_ALLOW_RDWR) &&
1853 reopen_state->flags & BDRV_O_RDWR) {
81e5f78a
AG
1854 error_setg(errp, "Node '%s' is read only",
1855 bdrv_get_device_or_node_name(reopen_state->bs));
e971aa12
JC
1856 goto error;
1857 }
1858
1859
1860 ret = bdrv_flush(reopen_state->bs);
1861 if (ret) {
455b0fde 1862 error_setg_errno(errp, -ret, "Error flushing drive");
e971aa12
JC
1863 goto error;
1864 }
1865
1866 if (drv->bdrv_reopen_prepare) {
1867 ret = drv->bdrv_reopen_prepare(reopen_state, queue, &local_err);
1868 if (ret) {
1869 if (local_err != NULL) {
1870 error_propagate(errp, local_err);
1871 } else {
d8b6895f
LC
1872 error_setg(errp, "failed while preparing to reopen image '%s'",
1873 reopen_state->bs->filename);
e971aa12
JC
1874 }
1875 goto error;
1876 }
1877 } else {
1878 /* It is currently mandatory to have a bdrv_reopen_prepare()
1879 * handler for each supported drv. */
81e5f78a
AG
1880 error_setg(errp, "Block format '%s' used by node '%s' "
1881 "does not support reopening files", drv->format_name,
1882 bdrv_get_device_or_node_name(reopen_state->bs));
e971aa12
JC
1883 ret = -1;
1884 goto error;
1885 }
1886
4d2cb092
KW
1887 /* Options that are not handled are only okay if they are unchanged
1888 * compared to the old state. It is expected that some options are only
1889 * used for the initial open, but not reopen (e.g. filename) */
1890 if (qdict_size(reopen_state->options)) {
1891 const QDictEntry *entry = qdict_first(reopen_state->options);
1892
1893 do {
1894 QString *new_obj = qobject_to_qstring(entry->value);
1895 const char *new = qstring_get_str(new_obj);
1896 const char *old = qdict_get_try_str(reopen_state->bs->options,
1897 entry->key);
1898
1899 if (!old || strcmp(new, old)) {
1900 error_setg(errp, "Cannot change the option '%s'", entry->key);
1901 ret = -EINVAL;
1902 goto error;
1903 }
1904 } while ((entry = qdict_next(reopen_state->options, entry)));
1905 }
1906
e971aa12
JC
1907 ret = 0;
1908
1909error:
1910 return ret;
1911}
1912
1913/*
1914 * Takes the staged changes for the reopen from bdrv_reopen_prepare(), and
1915 * makes them final by swapping the staging BlockDriverState contents into
1916 * the active BlockDriverState contents.
1917 */
1918void bdrv_reopen_commit(BDRVReopenState *reopen_state)
1919{
1920 BlockDriver *drv;
1921
1922 assert(reopen_state != NULL);
1923 drv = reopen_state->bs->drv;
1924 assert(drv != NULL);
1925
1926 /* If there are any driver level actions to take */
1927 if (drv->bdrv_reopen_commit) {
1928 drv->bdrv_reopen_commit(reopen_state);
1929 }
1930
1931 /* set BDS specific flags now */
1932 reopen_state->bs->open_flags = reopen_state->flags;
1933 reopen_state->bs->enable_write_cache = !!(reopen_state->flags &
1934 BDRV_O_CACHE_WB);
1935 reopen_state->bs->read_only = !(reopen_state->flags & BDRV_O_RDWR);
355ef4ac 1936
3baca891 1937 bdrv_refresh_limits(reopen_state->bs, NULL);
e971aa12
JC
1938}
1939
1940/*
1941 * Abort the reopen, and delete and free the staged changes in
1942 * reopen_state
1943 */
1944void bdrv_reopen_abort(BDRVReopenState *reopen_state)
1945{
1946 BlockDriver *drv;
1947
1948 assert(reopen_state != NULL);
1949 drv = reopen_state->bs->drv;
1950 assert(drv != NULL);
1951
1952 if (drv->bdrv_reopen_abort) {
1953 drv->bdrv_reopen_abort(reopen_state);
1954 }
1955}
1956
1957
fc01f7e7
FB
1958void bdrv_close(BlockDriverState *bs)
1959{
33384421
HR
1960 BdrvAioNotifier *ban, *ban_next;
1961
3cbc002c
PB
1962 if (bs->job) {
1963 block_job_cancel_sync(bs->job);
1964 }
99b7e775
AG
1965
1966 /* Disable I/O limits and drain all pending throttled requests */
a0d64a61 1967 if (bs->throttle_state) {
99b7e775
AG
1968 bdrv_io_limits_disable(bs);
1969 }
1970
53ec73e2 1971 bdrv_drain(bs); /* complete I/O */
58fda173 1972 bdrv_flush(bs);
53ec73e2 1973 bdrv_drain(bs); /* in case flush left pending I/O */
d7d512f6 1974 notifier_list_notify(&bs->close_notifiers, bs);
7094f12f 1975
b4d02820
HR
1976 if (bs->blk) {
1977 blk_dev_change_media_cb(bs->blk, false);
1978 }
1979
3cbc002c 1980 if (bs->drv) {
6e93e7c4
KW
1981 BdrvChild *child, *next;
1982
9a7dedbc 1983 bs->drv->bdrv_close(bs);
9a4f4c31 1984 bs->drv = NULL;
9a7dedbc 1985
5db15a57 1986 bdrv_set_backing_hd(bs, NULL);
9a7dedbc 1987
9a4f4c31
KW
1988 if (bs->file != NULL) {
1989 bdrv_unref_child(bs, bs->file);
1990 bs->file = NULL;
1991 }
1992
6e93e7c4 1993 QLIST_FOREACH_SAFE(child, &bs->children, next, next) {
33a60407
KW
1994 /* TODO Remove bdrv_unref() from drivers' close function and use
1995 * bdrv_unref_child() here */
bddcec37
KW
1996 if (child->bs->inherits_from == bs) {
1997 child->bs->inherits_from = NULL;
1998 }
33a60407 1999 bdrv_detach_child(child);
6e93e7c4
KW
2000 }
2001
7267c094 2002 g_free(bs->opaque);
ea2384d3 2003 bs->opaque = NULL;
53fec9d3 2004 bs->copy_on_read = 0;
a275fa42
PB
2005 bs->backing_file[0] = '\0';
2006 bs->backing_format[0] = '\0';
6405875c
PB
2007 bs->total_sectors = 0;
2008 bs->encrypted = 0;
2009 bs->valid_key = 0;
2010 bs->sg = 0;
0d51b4de 2011 bs->zero_beyond_eof = false;
de9c0cec
KW
2012 QDECREF(bs->options);
2013 bs->options = NULL;
91af7014
HR
2014 QDECREF(bs->full_open_options);
2015 bs->full_open_options = NULL;
b338082b 2016 }
98f90dba 2017
33384421
HR
2018 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) {
2019 g_free(ban);
2020 }
2021 QLIST_INIT(&bs->aio_notifiers);
b338082b
FB
2022}
2023
2bc93fed
MK
2024void bdrv_close_all(void)
2025{
2026 BlockDriverState *bs;
2027
dc364f4c 2028 QTAILQ_FOREACH(bs, &bdrv_states, device_list) {
ed78cda3
SH
2029 AioContext *aio_context = bdrv_get_aio_context(bs);
2030
2031 aio_context_acquire(aio_context);
2bc93fed 2032 bdrv_close(bs);
ed78cda3 2033 aio_context_release(aio_context);
2bc93fed
MK
2034 }
2035}
2036
dc364f4c
BC
2037/* make a BlockDriverState anonymous by removing from bdrv_state and
2038 * graph_bdrv_state list.
d22b2f41
RH
2039 Also, NULL terminate the device_name to prevent double remove */
2040void bdrv_make_anon(BlockDriverState *bs)
2041{
bfb197e0
MA
2042 /*
2043 * Take care to remove bs from bdrv_states only when it's actually
2044 * in it. Note that bs->device_list.tqe_prev is initially null,
2045 * and gets set to non-null by QTAILQ_INSERT_TAIL(). Establish
2046 * the useful invariant "bs in bdrv_states iff bs->tqe_prev" by
2047 * resetting it to null on remove.
2048 */
2049 if (bs->device_list.tqe_prev) {
dc364f4c 2050 QTAILQ_REMOVE(&bdrv_states, bs, device_list);
bfb197e0 2051 bs->device_list.tqe_prev = NULL;
d22b2f41 2052 }
dc364f4c
BC
2053 if (bs->node_name[0] != '\0') {
2054 QTAILQ_REMOVE(&graph_bdrv_states, bs, node_list);
2055 }
2056 bs->node_name[0] = '\0';
d22b2f41
RH
2057}
2058
8e419aef 2059/* Fields that need to stay with the top-level BDS */
4ddc07ca
PB
2060static void bdrv_move_feature_fields(BlockDriverState *bs_dest,
2061 BlockDriverState *bs_src)
8802d1fd 2062{
4ddc07ca 2063 /* move some fields that need to stay attached to the device */
8802d1fd
JC
2064
2065 /* dev info */
4ddc07ca 2066 bs_dest->copy_on_read = bs_src->copy_on_read;
8802d1fd 2067
4ddc07ca 2068 bs_dest->enable_write_cache = bs_src->enable_write_cache;
c4a248a1 2069
a9fc4408 2070 /* dirty bitmap */
e4654d2d 2071 bs_dest->dirty_bitmaps = bs_src->dirty_bitmaps;
063dd40e 2072}
a9fc4408 2073
dd62f1ca
KW
2074static void change_parent_backing_link(BlockDriverState *from,
2075 BlockDriverState *to)
2076{
2077 BdrvChild *c, *next;
2078
2079 QLIST_FOREACH_SAFE(c, &from->parents, next_parent, next) {
2080 assert(c->role != &child_backing);
2081 c->bs = to;
2082 QLIST_REMOVE(c, next_parent);
2083 QLIST_INSERT_HEAD(&to->parents, c, next_parent);
2084 bdrv_ref(to);
2085 bdrv_unref(from);
2086 }
2087 if (from->blk) {
2088 blk_set_bs(from->blk, to);
2089 if (!to->device_list.tqe_prev) {
2090 QTAILQ_INSERT_BEFORE(from, to, device_list);
2091 }
2092 QTAILQ_REMOVE(&bdrv_states, from, device_list);
2093 }
2094}
2095
2096static void swap_feature_fields(BlockDriverState *bs_top,
2097 BlockDriverState *bs_new)
2098{
2099 BlockDriverState tmp;
2100
2101 bdrv_move_feature_fields(&tmp, bs_top);
2102 bdrv_move_feature_fields(bs_top, bs_new);
2103 bdrv_move_feature_fields(bs_new, &tmp);
2104
2105 assert(!bs_new->throttle_state);
2106 if (bs_top->throttle_state) {
2107 assert(bs_top->io_limits_enabled);
2108 bdrv_io_limits_enable(bs_new, throttle_group_get_name(bs_top));
2109 bdrv_io_limits_disable(bs_top);
2110 }
2111}
2112
4ddc07ca
PB
2113/*
2114 * Add new bs contents at the top of an image chain while the chain is
2115 * live, while keeping required fields on the top layer.
2116 *
2117 * This will modify the BlockDriverState fields, and swap contents
2118 * between bs_new and bs_top. Both bs_new and bs_top are modified.
2119 *
bfb197e0 2120 * bs_new must not be attached to a BlockBackend.
4ddc07ca
PB
2121 *
2122 * This function does not create any image files.
dd62f1ca
KW
2123 *
2124 * bdrv_append() takes ownership of a bs_new reference and unrefs it because
2125 * that's what the callers commonly need. bs_new will be referenced by the old
2126 * parents of bs_top after bdrv_append() returns. If the caller needs to keep a
2127 * reference of its own, it must call bdrv_ref().
4ddc07ca
PB
2128 */
2129void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top)
2130{
dd62f1ca
KW
2131 assert(!bdrv_requests_pending(bs_top));
2132 assert(!bdrv_requests_pending(bs_new));
2133
2134 bdrv_ref(bs_top);
2135 change_parent_backing_link(bs_top, bs_new);
2136
2137 /* Some fields always stay on top of the backing file chain */
2138 swap_feature_fields(bs_top, bs_new);
2139
2140 bdrv_set_backing_hd(bs_new, bs_top);
2141 bdrv_unref(bs_top);
4ddc07ca 2142
dd62f1ca
KW
2143 /* bs_new is now referenced by its new parents, we don't need the
2144 * additional reference any more. */
2145 bdrv_unref(bs_new);
8802d1fd
JC
2146}
2147
3f09bfbc
KW
2148void bdrv_replace_in_backing_chain(BlockDriverState *old, BlockDriverState *new)
2149{
2150 assert(!bdrv_requests_pending(old));
2151 assert(!bdrv_requests_pending(new));
2152
2153 bdrv_ref(old);
2154
2155 if (old->blk) {
2156 /* As long as these fields aren't in BlockBackend, but in the top-level
2157 * BlockDriverState, it's not possible for a BDS to have two BBs.
2158 *
2159 * We really want to copy the fields from old to new, but we go for a
2160 * swap instead so that pointers aren't duplicated and cause trouble.
2161 * (Also, bdrv_swap() used to do the same.) */
2162 assert(!new->blk);
2163 swap_feature_fields(old, new);
2164 }
2165 change_parent_backing_link(old, new);
2166
2167 /* Change backing files if a previously independent node is added to the
2168 * chain. For active commit, we replace top by its own (indirect) backing
2169 * file and don't do anything here so we don't build a loop. */
2170 if (new->backing == NULL && !bdrv_chain_contains(backing_bs(old), new)) {
2171 bdrv_set_backing_hd(new, backing_bs(old));
2172 bdrv_set_backing_hd(old, NULL);
2173 }
2174
2175 bdrv_unref(old);
2176}
2177
4f6fd349 2178static void bdrv_delete(BlockDriverState *bs)
b338082b 2179{
3e914655 2180 assert(!bs->job);
3718d8ab 2181 assert(bdrv_op_blocker_is_empty(bs));
4f6fd349 2182 assert(!bs->refcnt);
e4654d2d 2183 assert(QLIST_EMPTY(&bs->dirty_bitmaps));
18846dee 2184
e1b5c52e
SH
2185 bdrv_close(bs);
2186
1b7bdbc1 2187 /* remove from list, if necessary */
d22b2f41 2188 bdrv_make_anon(bs);
34c6f050 2189
7267c094 2190 g_free(bs);
fc01f7e7
FB
2191}
2192
e97fc193
AL
2193/*
2194 * Run consistency checks on an image
2195 *
e076f338 2196 * Returns 0 if the check could be completed (it doesn't mean that the image is
a1c7273b 2197 * free of errors) or -errno when an internal error occurred. The results of the
e076f338 2198 * check are stored in res.
e97fc193 2199 */
4534ff54 2200int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix)
e97fc193 2201{
908bcd54
HR
2202 if (bs->drv == NULL) {
2203 return -ENOMEDIUM;
2204 }
e97fc193
AL
2205 if (bs->drv->bdrv_check == NULL) {
2206 return -ENOTSUP;
2207 }
2208
e076f338 2209 memset(res, 0, sizeof(*res));
4534ff54 2210 return bs->drv->bdrv_check(bs, res, fix);
e97fc193
AL
2211}
2212
8a426614
KW
2213#define COMMIT_BUF_SECTORS 2048
2214
33e3963e
FB
2215/* commit COW file into the raw image */
2216int bdrv_commit(BlockDriverState *bs)
2217{
19cb3738 2218 BlockDriver *drv = bs->drv;
72706ea4 2219 int64_t sector, total_sectors, length, backing_length;
8a426614 2220 int n, ro, open_flags;
0bce597d 2221 int ret = 0;
72706ea4 2222 uint8_t *buf = NULL;
33e3963e 2223
19cb3738
FB
2224 if (!drv)
2225 return -ENOMEDIUM;
6bb45158 2226
760e0063 2227 if (!bs->backing) {
4dca4b63 2228 return -ENOTSUP;
33e3963e
FB
2229 }
2230
bb00021d 2231 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, NULL) ||
760e0063 2232 bdrv_op_is_blocked(bs->backing->bs, BLOCK_OP_TYPE_COMMIT_TARGET, NULL)) {
2d3735d3
SH
2233 return -EBUSY;
2234 }
2235
760e0063
KW
2236 ro = bs->backing->bs->read_only;
2237 open_flags = bs->backing->bs->open_flags;
4dca4b63
NS
2238
2239 if (ro) {
760e0063 2240 if (bdrv_reopen(bs->backing->bs, open_flags | BDRV_O_RDWR, NULL)) {
0bce597d 2241 return -EACCES;
4dca4b63 2242 }
ea2384d3 2243 }
33e3963e 2244
72706ea4
JC
2245 length = bdrv_getlength(bs);
2246 if (length < 0) {
2247 ret = length;
2248 goto ro_cleanup;
2249 }
2250
760e0063 2251 backing_length = bdrv_getlength(bs->backing->bs);
72706ea4
JC
2252 if (backing_length < 0) {
2253 ret = backing_length;
2254 goto ro_cleanup;
2255 }
2256
2257 /* If our top snapshot is larger than the backing file image,
2258 * grow the backing file image if possible. If not possible,
2259 * we must return an error */
2260 if (length > backing_length) {
760e0063 2261 ret = bdrv_truncate(bs->backing->bs, length);
72706ea4
JC
2262 if (ret < 0) {
2263 goto ro_cleanup;
2264 }
2265 }
2266
2267 total_sectors = length >> BDRV_SECTOR_BITS;
857d4f46
KW
2268
2269 /* qemu_try_blockalign() for bs will choose an alignment that works for
760e0063 2270 * bs->backing->bs as well, so no need to compare the alignment manually. */
857d4f46
KW
2271 buf = qemu_try_blockalign(bs, COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE);
2272 if (buf == NULL) {
2273 ret = -ENOMEM;
2274 goto ro_cleanup;
2275 }
8a426614
KW
2276
2277 for (sector = 0; sector < total_sectors; sector += n) {
d663640c
PB
2278 ret = bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n);
2279 if (ret < 0) {
2280 goto ro_cleanup;
2281 }
2282 if (ret) {
dabfa6cc
KW
2283 ret = bdrv_read(bs, sector, buf, n);
2284 if (ret < 0) {
8a426614
KW
2285 goto ro_cleanup;
2286 }
2287
760e0063 2288 ret = bdrv_write(bs->backing->bs, sector, buf, n);
dabfa6cc 2289 if (ret < 0) {
8a426614
KW
2290 goto ro_cleanup;
2291 }
ea2384d3 2292 }
33e3963e 2293 }
95389c86 2294
1d44952f
CH
2295 if (drv->bdrv_make_empty) {
2296 ret = drv->bdrv_make_empty(bs);
dabfa6cc
KW
2297 if (ret < 0) {
2298 goto ro_cleanup;
2299 }
1d44952f
CH
2300 bdrv_flush(bs);
2301 }
95389c86 2302
3f5075ae
CH
2303 /*
2304 * Make sure all data we wrote to the backing device is actually
2305 * stable on disk.
2306 */
760e0063
KW
2307 if (bs->backing) {
2308 bdrv_flush(bs->backing->bs);
dabfa6cc 2309 }
4dca4b63 2310
dabfa6cc 2311 ret = 0;
4dca4b63 2312ro_cleanup:
857d4f46 2313 qemu_vfree(buf);
4dca4b63
NS
2314
2315 if (ro) {
0bce597d 2316 /* ignoring error return here */
760e0063 2317 bdrv_reopen(bs->backing->bs, open_flags & ~BDRV_O_RDWR, NULL);
4dca4b63
NS
2318 }
2319
1d44952f 2320 return ret;
33e3963e
FB
2321}
2322
e8877497 2323int bdrv_commit_all(void)
6ab4b5ab
MA
2324{
2325 BlockDriverState *bs;
2326
dc364f4c 2327 QTAILQ_FOREACH(bs, &bdrv_states, device_list) {
ed78cda3
SH
2328 AioContext *aio_context = bdrv_get_aio_context(bs);
2329
2330 aio_context_acquire(aio_context);
760e0063 2331 if (bs->drv && bs->backing) {
272d2d8e
JC
2332 int ret = bdrv_commit(bs);
2333 if (ret < 0) {
ed78cda3 2334 aio_context_release(aio_context);
272d2d8e
JC
2335 return ret;
2336 }
e8877497 2337 }
ed78cda3 2338 aio_context_release(aio_context);
6ab4b5ab 2339 }
e8877497 2340 return 0;
6ab4b5ab
MA
2341}
2342
756e6736
KW
2343/*
2344 * Return values:
2345 * 0 - success
2346 * -EINVAL - backing format specified, but no file
2347 * -ENOSPC - can't update the backing file because no space is left in the
2348 * image file header
2349 * -ENOTSUP - format driver doesn't support changing the backing file
2350 */
2351int bdrv_change_backing_file(BlockDriverState *bs,
2352 const char *backing_file, const char *backing_fmt)
2353{
2354 BlockDriver *drv = bs->drv;
469ef350 2355 int ret;
756e6736 2356
5f377794
PB
2357 /* Backing file format doesn't make sense without a backing file */
2358 if (backing_fmt && !backing_file) {
2359 return -EINVAL;
2360 }
2361
756e6736 2362 if (drv->bdrv_change_backing_file != NULL) {
469ef350 2363 ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
756e6736 2364 } else {
469ef350 2365 ret = -ENOTSUP;
756e6736 2366 }
469ef350
PB
2367
2368 if (ret == 0) {
2369 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
2370 pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
2371 }
2372 return ret;
756e6736
KW
2373}
2374
6ebdcee2
JC
2375/*
2376 * Finds the image layer in the chain that has 'bs' as its backing file.
2377 *
2378 * active is the current topmost image.
2379 *
2380 * Returns NULL if bs is not found in active's image chain,
2381 * or if active == bs.
4caf0fcd
JC
2382 *
2383 * Returns the bottommost base image if bs == NULL.
6ebdcee2
JC
2384 */
2385BlockDriverState *bdrv_find_overlay(BlockDriverState *active,
2386 BlockDriverState *bs)
2387{
760e0063
KW
2388 while (active && bs != backing_bs(active)) {
2389 active = backing_bs(active);
6ebdcee2
JC
2390 }
2391
4caf0fcd
JC
2392 return active;
2393}
6ebdcee2 2394
4caf0fcd
JC
2395/* Given a BDS, searches for the base layer. */
2396BlockDriverState *bdrv_find_base(BlockDriverState *bs)
2397{
2398 return bdrv_find_overlay(bs, NULL);
6ebdcee2
JC
2399}
2400
6ebdcee2
JC
2401/*
2402 * Drops images above 'base' up to and including 'top', and sets the image
2403 * above 'top' to have base as its backing file.
2404 *
2405 * Requires that the overlay to 'top' is opened r/w, so that the backing file
2406 * information in 'bs' can be properly updated.
2407 *
2408 * E.g., this will convert the following chain:
2409 * bottom <- base <- intermediate <- top <- active
2410 *
2411 * to
2412 *
2413 * bottom <- base <- active
2414 *
2415 * It is allowed for bottom==base, in which case it converts:
2416 *
2417 * base <- intermediate <- top <- active
2418 *
2419 * to
2420 *
2421 * base <- active
2422 *
54e26900
JC
2423 * If backing_file_str is non-NULL, it will be used when modifying top's
2424 * overlay image metadata.
2425 *
6ebdcee2
JC
2426 * Error conditions:
2427 * if active == top, that is considered an error
2428 *
2429 */
2430int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top,
54e26900 2431 BlockDriverState *base, const char *backing_file_str)
6ebdcee2 2432{
6ebdcee2 2433 BlockDriverState *new_top_bs = NULL;
6ebdcee2
JC
2434 int ret = -EIO;
2435
6ebdcee2
JC
2436 if (!top->drv || !base->drv) {
2437 goto exit;
2438 }
2439
2440 new_top_bs = bdrv_find_overlay(active, top);
2441
2442 if (new_top_bs == NULL) {
2443 /* we could not find the image above 'top', this is an error */
2444 goto exit;
2445 }
2446
760e0063 2447 /* special case of new_top_bs->backing->bs already pointing to base - nothing
6ebdcee2 2448 * to do, no intermediate images */
760e0063 2449 if (backing_bs(new_top_bs) == base) {
6ebdcee2
JC
2450 ret = 0;
2451 goto exit;
2452 }
2453
5db15a57
KW
2454 /* Make sure that base is in the backing chain of top */
2455 if (!bdrv_chain_contains(top, base)) {
6ebdcee2
JC
2456 goto exit;
2457 }
2458
2459 /* success - we can delete the intermediate states, and link top->base */
5db15a57 2460 backing_file_str = backing_file_str ? backing_file_str : base->filename;
54e26900 2461 ret = bdrv_change_backing_file(new_top_bs, backing_file_str,
5db15a57 2462 base->drv ? base->drv->format_name : "");
6ebdcee2
JC
2463 if (ret) {
2464 goto exit;
2465 }
5db15a57 2466 bdrv_set_backing_hd(new_top_bs, base);
6ebdcee2 2467
6ebdcee2 2468 ret = 0;
6ebdcee2 2469exit:
6ebdcee2
JC
2470 return ret;
2471}
2472
61007b31
SH
2473/**
2474 * Truncate file to 'offset' bytes (needed only for file protocols)
2475 */
2476int bdrv_truncate(BlockDriverState *bs, int64_t offset)
71d0770c 2477{
61007b31
SH
2478 BlockDriver *drv = bs->drv;
2479 int ret;
2480 if (!drv)
71d0770c 2481 return -ENOMEDIUM;
61007b31
SH
2482 if (!drv->bdrv_truncate)
2483 return -ENOTSUP;
2484 if (bs->read_only)
2485 return -EACCES;
71d0770c 2486
61007b31
SH
2487 ret = drv->bdrv_truncate(bs, offset);
2488 if (ret == 0) {
2489 ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
2490 bdrv_dirty_bitmap_truncate(bs);
2491 if (bs->blk) {
2492 blk_dev_resize_cb(bs->blk);
2493 }
c0191e76 2494 }
61007b31 2495 return ret;
71d0770c
AL
2496}
2497
61007b31
SH
2498/**
2499 * Length of a allocated file in bytes. Sparse files are counted by actual
2500 * allocated space. Return < 0 if error or unknown.
2501 */
2502int64_t bdrv_get_allocated_file_size(BlockDriverState *bs)
71d0770c 2503{
61007b31
SH
2504 BlockDriver *drv = bs->drv;
2505 if (!drv) {
2506 return -ENOMEDIUM;
8f4754ed 2507 }
61007b31
SH
2508 if (drv->bdrv_get_allocated_file_size) {
2509 return drv->bdrv_get_allocated_file_size(bs);
2510 }
2511 if (bs->file) {
9a4f4c31 2512 return bdrv_get_allocated_file_size(bs->file->bs);
1c9805a3 2513 }
61007b31 2514 return -ENOTSUP;
1c9805a3 2515}
e7a8a783 2516
61007b31
SH
2517/**
2518 * Return number of sectors on success, -errno on error.
1c9805a3 2519 */
61007b31 2520int64_t bdrv_nb_sectors(BlockDriverState *bs)
1c9805a3 2521{
61007b31 2522 BlockDriver *drv = bs->drv;
498e386c 2523
61007b31
SH
2524 if (!drv)
2525 return -ENOMEDIUM;
2572b37a 2526
61007b31
SH
2527 if (drv->has_variable_length) {
2528 int ret = refresh_total_sectors(bs, bs->total_sectors);
2529 if (ret < 0) {
2530 return ret;
1c9805a3
SH
2531 }
2532 }
61007b31 2533 return bs->total_sectors;
1c9805a3 2534}
b338082b 2535
61007b31
SH
2536/**
2537 * Return length in bytes on success, -errno on error.
2538 * The length is always a multiple of BDRV_SECTOR_SIZE.
8d3b1a2d 2539 */
61007b31 2540int64_t bdrv_getlength(BlockDriverState *bs)
8d3b1a2d 2541{
61007b31 2542 int64_t ret = bdrv_nb_sectors(bs);
8d3b1a2d 2543
4a9c9ea0 2544 ret = ret > INT64_MAX / BDRV_SECTOR_SIZE ? -EFBIG : ret;
61007b31 2545 return ret < 0 ? ret : ret * BDRV_SECTOR_SIZE;
fc01f7e7
FB
2546}
2547
61007b31
SH
2548/* return 0 as number of sectors if no device present or error */
2549void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
07d27a44 2550{
61007b31 2551 int64_t nb_sectors = bdrv_nb_sectors(bs);
07d27a44 2552
61007b31 2553 *nb_sectors_ptr = nb_sectors < 0 ? 0 : nb_sectors;
07d27a44
MA
2554}
2555
61007b31 2556int bdrv_is_read_only(BlockDriverState *bs)
8d3b1a2d 2557{
61007b31 2558 return bs->read_only;
83f64091 2559}
83f64091 2560
61007b31 2561int bdrv_is_sg(BlockDriverState *bs)
f08145fe 2562{
61007b31 2563 return bs->sg;
f08145fe
KW
2564}
2565
61007b31 2566int bdrv_enable_write_cache(BlockDriverState *bs)
ab185921 2567{
61007b31 2568 return bs->enable_write_cache;
ab185921
SH
2569}
2570
61007b31 2571void bdrv_set_enable_write_cache(BlockDriverState *bs, bool wce)
da1fa91d 2572{
61007b31 2573 bs->enable_write_cache = wce;
ab185921 2574
61007b31
SH
2575 /* so a reopen() will preserve wce */
2576 if (wce) {
2577 bs->open_flags |= BDRV_O_CACHE_WB;
893a8f62 2578 } else {
61007b31 2579 bs->open_flags &= ~BDRV_O_CACHE_WB;
893a8f62 2580 }
da1fa91d
KW
2581}
2582
61007b31 2583int bdrv_is_encrypted(BlockDriverState *bs)
fc3959e4 2584{
760e0063 2585 if (bs->backing && bs->backing->bs->encrypted) {
61007b31 2586 return 1;
760e0063 2587 }
61007b31 2588 return bs->encrypted;
fc3959e4
FZ
2589}
2590
61007b31 2591int bdrv_key_required(BlockDriverState *bs)
fc3959e4 2592{
760e0063 2593 BdrvChild *backing = bs->backing;
61007b31 2594
760e0063 2595 if (backing && backing->bs->encrypted && !backing->bs->valid_key) {
61007b31 2596 return 1;
760e0063 2597 }
61007b31 2598 return (bs->encrypted && !bs->valid_key);
fc3959e4
FZ
2599}
2600
61007b31 2601int bdrv_set_key(BlockDriverState *bs, const char *key)
d0c7f642 2602{
d0c7f642 2603 int ret;
760e0063
KW
2604 if (bs->backing && bs->backing->bs->encrypted) {
2605 ret = bdrv_set_key(bs->backing->bs, key);
61007b31
SH
2606 if (ret < 0)
2607 return ret;
2608 if (!bs->encrypted)
2609 return 0;
2610 }
2611 if (!bs->encrypted) {
2612 return -EINVAL;
2613 } else if (!bs->drv || !bs->drv->bdrv_set_key) {
d0c7f642
KW
2614 return -ENOMEDIUM;
2615 }
61007b31 2616 ret = bs->drv->bdrv_set_key(bs, key);
b9c64947 2617 if (ret < 0) {
61007b31
SH
2618 bs->valid_key = 0;
2619 } else if (!bs->valid_key) {
2620 bs->valid_key = 1;
2621 if (bs->blk) {
2622 /* call the change callback now, we skipped it on open */
2623 blk_dev_change_media_cb(bs->blk, true);
2624 }
1b0288ae 2625 }
61007b31
SH
2626 return ret;
2627}
f08f2dda 2628
c5fbe571 2629/*
61007b31
SH
2630 * Provide an encryption key for @bs.
2631 * If @key is non-null:
2632 * If @bs is not encrypted, fail.
2633 * Else if the key is invalid, fail.
2634 * Else set @bs's key to @key, replacing the existing key, if any.
2635 * If @key is null:
2636 * If @bs is encrypted and still lacks a key, fail.
2637 * Else do nothing.
2638 * On failure, store an error object through @errp if non-null.
c5fbe571 2639 */
61007b31 2640void bdrv_add_key(BlockDriverState *bs, const char *key, Error **errp)
c5fbe571 2641{
61007b31
SH
2642 if (key) {
2643 if (!bdrv_is_encrypted(bs)) {
2644 error_setg(errp, "Node '%s' is not encrypted",
2645 bdrv_get_device_or_node_name(bs));
2646 } else if (bdrv_set_key(bs, key) < 0) {
c6bd8c70 2647 error_setg(errp, QERR_INVALID_PASSWORD);
4d2855a3
MA
2648 }
2649 } else {
2650 if (bdrv_key_required(bs)) {
b1ca6391
MA
2651 error_set(errp, ERROR_CLASS_DEVICE_ENCRYPTED,
2652 "'%s' (%s) is encrypted",
81e5f78a 2653 bdrv_get_device_or_node_name(bs),
4d2855a3
MA
2654 bdrv_get_encrypted_filename(bs));
2655 }
2656 }
2657}
2658
61007b31 2659const char *bdrv_get_format_name(BlockDriverState *bs)
40b4f539 2660{
61007b31 2661 return bs->drv ? bs->drv->format_name : NULL;
40b4f539
KW
2662}
2663
61007b31 2664static int qsort_strcmp(const void *a, const void *b)
40b4f539 2665{
61007b31 2666 return strcmp(a, b);
40b4f539
KW
2667}
2668
61007b31
SH
2669void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
2670 void *opaque)
40b4f539 2671{
61007b31
SH
2672 BlockDriver *drv;
2673 int count = 0;
2674 int i;
2675 const char **formats = NULL;
40b4f539 2676
61007b31
SH
2677 QLIST_FOREACH(drv, &bdrv_drivers, list) {
2678 if (drv->format_name) {
2679 bool found = false;
2680 int i = count;
2681 while (formats && i && !found) {
2682 found = !strcmp(formats[--i], drv->format_name);
2683 }
e2a305fb 2684
61007b31
SH
2685 if (!found) {
2686 formats = g_renew(const char *, formats, count + 1);
2687 formats[count++] = drv->format_name;
2688 }
6c5a42ac 2689 }
61007b31 2690 }
6c5a42ac 2691
61007b31 2692 qsort(formats, count, sizeof(formats[0]), qsort_strcmp);
40b4f539 2693
61007b31
SH
2694 for (i = 0; i < count; i++) {
2695 it(opaque, formats[i]);
2696 }
40b4f539 2697
61007b31
SH
2698 g_free(formats);
2699}
40b4f539 2700
61007b31
SH
2701/* This function is to find a node in the bs graph */
2702BlockDriverState *bdrv_find_node(const char *node_name)
2703{
2704 BlockDriverState *bs;
391827eb 2705
61007b31 2706 assert(node_name);
40b4f539 2707
61007b31
SH
2708 QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) {
2709 if (!strcmp(node_name, bs->node_name)) {
2710 return bs;
40b4f539
KW
2711 }
2712 }
61007b31 2713 return NULL;
40b4f539
KW
2714}
2715
61007b31
SH
2716/* Put this QMP function here so it can access the static graph_bdrv_states. */
2717BlockDeviceInfoList *bdrv_named_nodes_list(Error **errp)
40b4f539 2718{
61007b31
SH
2719 BlockDeviceInfoList *list, *entry;
2720 BlockDriverState *bs;
40b4f539 2721
61007b31
SH
2722 list = NULL;
2723 QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) {
2724 BlockDeviceInfo *info = bdrv_block_device_info(bs, errp);
2725 if (!info) {
2726 qapi_free_BlockDeviceInfoList(list);
2727 return NULL;
301db7c2 2728 }
61007b31
SH
2729 entry = g_malloc0(sizeof(*entry));
2730 entry->value = info;
2731 entry->next = list;
2732 list = entry;
301db7c2
RH
2733 }
2734
61007b31
SH
2735 return list;
2736}
40b4f539 2737
61007b31
SH
2738BlockDriverState *bdrv_lookup_bs(const char *device,
2739 const char *node_name,
2740 Error **errp)
2741{
2742 BlockBackend *blk;
2743 BlockDriverState *bs;
40b4f539 2744
61007b31
SH
2745 if (device) {
2746 blk = blk_by_name(device);
40b4f539 2747
61007b31 2748 if (blk) {
9f4ed6fb
AG
2749 bs = blk_bs(blk);
2750 if (!bs) {
5433c24f 2751 error_setg(errp, "Device '%s' has no medium", device);
5433c24f
HR
2752 }
2753
9f4ed6fb 2754 return bs;
61007b31
SH
2755 }
2756 }
40b4f539 2757
61007b31
SH
2758 if (node_name) {
2759 bs = bdrv_find_node(node_name);
6d519a5f 2760
61007b31
SH
2761 if (bs) {
2762 return bs;
2763 }
40b4f539
KW
2764 }
2765
61007b31
SH
2766 error_setg(errp, "Cannot find device=%s nor node_name=%s",
2767 device ? device : "",
2768 node_name ? node_name : "");
2769 return NULL;
40b4f539
KW
2770}
2771
61007b31
SH
2772/* If 'base' is in the same chain as 'top', return true. Otherwise,
2773 * return false. If either argument is NULL, return false. */
2774bool bdrv_chain_contains(BlockDriverState *top, BlockDriverState *base)
83f64091 2775{
61007b31 2776 while (top && top != base) {
760e0063 2777 top = backing_bs(top);
02c50efe 2778 }
61007b31
SH
2779
2780 return top != NULL;
02c50efe
FZ
2781}
2782
61007b31 2783BlockDriverState *bdrv_next_node(BlockDriverState *bs)
02c50efe 2784{
61007b31
SH
2785 if (!bs) {
2786 return QTAILQ_FIRST(&graph_bdrv_states);
02c50efe 2787 }
61007b31 2788 return QTAILQ_NEXT(bs, node_list);
83f64091
FB
2789}
2790
61007b31 2791BlockDriverState *bdrv_next(BlockDriverState *bs)
83f64091 2792{
61007b31
SH
2793 if (!bs) {
2794 return QTAILQ_FIRST(&bdrv_states);
857d4f46 2795 }
61007b31 2796 return QTAILQ_NEXT(bs, device_list);
83f64091 2797}
beac80cd 2798
61007b31 2799const char *bdrv_get_node_name(const BlockDriverState *bs)
83f64091 2800{
61007b31 2801 return bs->node_name;
beac80cd
FB
2802}
2803
61007b31
SH
2804/* TODO check what callers really want: bs->node_name or blk_name() */
2805const char *bdrv_get_device_name(const BlockDriverState *bs)
beac80cd 2806{
61007b31 2807 return bs->blk ? blk_name(bs->blk) : "";
f141eafe 2808}
83f64091 2809
61007b31
SH
2810/* This can be used to identify nodes that might not have a device
2811 * name associated. Since node and device names live in the same
2812 * namespace, the result is unambiguous. The exception is if both are
2813 * absent, then this returns an empty (non-null) string. */
2814const char *bdrv_get_device_or_node_name(const BlockDriverState *bs)
f141eafe 2815{
61007b31 2816 return bs->blk ? blk_name(bs->blk) : bs->node_name;
beac80cd 2817}
beac80cd 2818
61007b31 2819int bdrv_get_flags(BlockDriverState *bs)
0b5a2445 2820{
61007b31 2821 return bs->open_flags;
0b5a2445
PB
2822}
2823
61007b31 2824int bdrv_has_zero_init_1(BlockDriverState *bs)
68485420 2825{
61007b31 2826 return 1;
0b5a2445
PB
2827}
2828
61007b31 2829int bdrv_has_zero_init(BlockDriverState *bs)
0b5a2445 2830{
61007b31 2831 assert(bs->drv);
0b5a2445 2832
61007b31
SH
2833 /* If BS is a copy on write image, it is initialized to
2834 the contents of the base image, which may not be zeroes. */
760e0063 2835 if (bs->backing) {
61007b31
SH
2836 return 0;
2837 }
2838 if (bs->drv->bdrv_has_zero_init) {
2839 return bs->drv->bdrv_has_zero_init(bs);
0b5a2445 2840 }
61007b31
SH
2841
2842 /* safe default */
2843 return 0;
68485420
KW
2844}
2845
61007b31 2846bool bdrv_unallocated_blocks_are_zero(BlockDriverState *bs)
b2a61371 2847{
61007b31 2848 BlockDriverInfo bdi;
b2a61371 2849
760e0063 2850 if (bs->backing) {
61007b31
SH
2851 return false;
2852 }
2853
2854 if (bdrv_get_info(bs, &bdi) == 0) {
2855 return bdi.unallocated_blocks_are_zero;
b2a61371
SH
2856 }
2857
61007b31 2858 return false;
b2a61371
SH
2859}
2860
61007b31 2861bool bdrv_can_write_zeroes_with_unmap(BlockDriverState *bs)
68485420 2862{
61007b31 2863 BlockDriverInfo bdi;
68485420 2864
760e0063 2865 if (bs->backing || !(bs->open_flags & BDRV_O_UNMAP)) {
61007b31
SH
2866 return false;
2867 }
68485420 2868
61007b31
SH
2869 if (bdrv_get_info(bs, &bdi) == 0) {
2870 return bdi.can_write_zeroes_with_unmap;
2871 }
68485420 2872
61007b31 2873 return false;
68485420
KW
2874}
2875
61007b31 2876const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
b2e12bc6 2877{
760e0063 2878 if (bs->backing && bs->backing->bs->encrypted)
61007b31
SH
2879 return bs->backing_file;
2880 else if (bs->encrypted)
2881 return bs->filename;
2882 else
2883 return NULL;
b2e12bc6
CH
2884}
2885
61007b31
SH
2886void bdrv_get_backing_filename(BlockDriverState *bs,
2887 char *filename, int filename_size)
016f5cf6 2888{
61007b31
SH
2889 pstrcpy(filename, filename_size, bs->backing_file);
2890}
d318aea9 2891
61007b31
SH
2892int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
2893{
2894 BlockDriver *drv = bs->drv;
2895 if (!drv)
2896 return -ENOMEDIUM;
2897 if (!drv->bdrv_get_info)
2898 return -ENOTSUP;
2899 memset(bdi, 0, sizeof(*bdi));
2900 return drv->bdrv_get_info(bs, bdi);
2901}
016f5cf6 2902
61007b31
SH
2903ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs)
2904{
2905 BlockDriver *drv = bs->drv;
2906 if (drv && drv->bdrv_get_specific_info) {
2907 return drv->bdrv_get_specific_info(bs);
2908 }
2909 return NULL;
016f5cf6
AG
2910}
2911
a31939e6 2912void bdrv_debug_event(BlockDriverState *bs, BlkdebugEvent event)
4265d620 2913{
61007b31
SH
2914 if (!bs || !bs->drv || !bs->drv->bdrv_debug_event) {
2915 return;
2916 }
4265d620 2917
61007b31 2918 bs->drv->bdrv_debug_event(bs, event);
4265d620
PB
2919}
2920
61007b31
SH
2921int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event,
2922 const char *tag)
4265d620 2923{
61007b31 2924 while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) {
9a4f4c31 2925 bs = bs->file ? bs->file->bs : NULL;
61007b31 2926 }
4265d620 2927
61007b31
SH
2928 if (bs && bs->drv && bs->drv->bdrv_debug_breakpoint) {
2929 return bs->drv->bdrv_debug_breakpoint(bs, event, tag);
2930 }
4265d620 2931
61007b31 2932 return -ENOTSUP;
4265d620
PB
2933}
2934
61007b31 2935int bdrv_debug_remove_breakpoint(BlockDriverState *bs, const char *tag)
ea2384d3 2936{
61007b31 2937 while (bs && bs->drv && !bs->drv->bdrv_debug_remove_breakpoint) {
9a4f4c31 2938 bs = bs->file ? bs->file->bs : NULL;
61007b31 2939 }
ce1a14dc 2940
61007b31
SH
2941 if (bs && bs->drv && bs->drv->bdrv_debug_remove_breakpoint) {
2942 return bs->drv->bdrv_debug_remove_breakpoint(bs, tag);
2943 }
2944
2945 return -ENOTSUP;
eb852011
MA
2946}
2947
61007b31 2948int bdrv_debug_resume(BlockDriverState *bs, const char *tag)
ce1a14dc 2949{
61007b31 2950 while (bs && (!bs->drv || !bs->drv->bdrv_debug_resume)) {
9a4f4c31 2951 bs = bs->file ? bs->file->bs : NULL;
61007b31 2952 }
ce1a14dc 2953
61007b31
SH
2954 if (bs && bs->drv && bs->drv->bdrv_debug_resume) {
2955 return bs->drv->bdrv_debug_resume(bs, tag);
2956 }
ce1a14dc 2957
61007b31 2958 return -ENOTSUP;
f197fe2b
FZ
2959}
2960
61007b31 2961bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag)
ce1a14dc 2962{
61007b31 2963 while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) {
9a4f4c31 2964 bs = bs->file ? bs->file->bs : NULL;
f197fe2b 2965 }
19cb3738 2966
61007b31
SH
2967 if (bs && bs->drv && bs->drv->bdrv_debug_is_suspended) {
2968 return bs->drv->bdrv_debug_is_suspended(bs, tag);
2969 }
f9f05dc5 2970
61007b31
SH
2971 return false;
2972}
f9f05dc5 2973
61007b31 2974int bdrv_is_snapshot(BlockDriverState *bs)
f9f05dc5 2975{
61007b31 2976 return !!(bs->open_flags & BDRV_O_SNAPSHOT);
f9f05dc5
KW
2977}
2978
61007b31
SH
2979/* backing_file can either be relative, or absolute, or a protocol. If it is
2980 * relative, it must be relative to the chain. So, passing in bs->filename
2981 * from a BDS as backing_file should not be done, as that may be relative to
2982 * the CWD rather than the chain. */
2983BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs,
2984 const char *backing_file)
f9f05dc5 2985{
61007b31
SH
2986 char *filename_full = NULL;
2987 char *backing_file_full = NULL;
2988 char *filename_tmp = NULL;
2989 int is_protocol = 0;
2990 BlockDriverState *curr_bs = NULL;
2991 BlockDriverState *retval = NULL;
f9f05dc5 2992
61007b31
SH
2993 if (!bs || !bs->drv || !backing_file) {
2994 return NULL;
f9f05dc5
KW
2995 }
2996
61007b31
SH
2997 filename_full = g_malloc(PATH_MAX);
2998 backing_file_full = g_malloc(PATH_MAX);
2999 filename_tmp = g_malloc(PATH_MAX);
f9f05dc5 3000
61007b31 3001 is_protocol = path_has_protocol(backing_file);
f9f05dc5 3002
760e0063 3003 for (curr_bs = bs; curr_bs->backing; curr_bs = curr_bs->backing->bs) {
f9f05dc5 3004
61007b31
SH
3005 /* If either of the filename paths is actually a protocol, then
3006 * compare unmodified paths; otherwise make paths relative */
3007 if (is_protocol || path_has_protocol(curr_bs->backing_file)) {
3008 if (strcmp(backing_file, curr_bs->backing_file) == 0) {
760e0063 3009 retval = curr_bs->backing->bs;
61007b31
SH
3010 break;
3011 }
3012 } else {
3013 /* If not an absolute filename path, make it relative to the current
3014 * image's filename path */
3015 path_combine(filename_tmp, PATH_MAX, curr_bs->filename,
3016 backing_file);
f9f05dc5 3017
61007b31
SH
3018 /* We are going to compare absolute pathnames */
3019 if (!realpath(filename_tmp, filename_full)) {
3020 continue;
3021 }
07f07615 3022
61007b31
SH
3023 /* We need to make sure the backing filename we are comparing against
3024 * is relative to the current image filename (or absolute) */
3025 path_combine(filename_tmp, PATH_MAX, curr_bs->filename,
3026 curr_bs->backing_file);
07f07615 3027
61007b31
SH
3028 if (!realpath(filename_tmp, backing_file_full)) {
3029 continue;
3030 }
eb489bb1 3031
61007b31 3032 if (strcmp(backing_file_full, filename_full) == 0) {
760e0063 3033 retval = curr_bs->backing->bs;
61007b31
SH
3034 break;
3035 }
3036 }
eb489bb1
KW
3037 }
3038
61007b31
SH
3039 g_free(filename_full);
3040 g_free(backing_file_full);
3041 g_free(filename_tmp);
3042 return retval;
3043}
3044
3045int bdrv_get_backing_file_depth(BlockDriverState *bs)
3046{
3047 if (!bs->drv) {
3048 return 0;
eb489bb1
KW
3049 }
3050
760e0063 3051 if (!bs->backing) {
61007b31 3052 return 0;
ca716364
KW
3053 }
3054
760e0063 3055 return 1 + bdrv_get_backing_file_depth(bs->backing->bs);
61007b31 3056}
07f07615 3057
61007b31
SH
3058void bdrv_init(void)
3059{
3060 module_call_init(MODULE_INIT_BLOCK);
3061}
29cdb251 3062
61007b31
SH
3063void bdrv_init_with_whitelist(void)
3064{
3065 use_bdrv_whitelist = 1;
3066 bdrv_init();
07f07615
PB
3067}
3068
5a8a30db 3069void bdrv_invalidate_cache(BlockDriverState *bs, Error **errp)
0f15423c 3070{
5a8a30db
KW
3071 Error *local_err = NULL;
3072 int ret;
3073
3456a8d1
KW
3074 if (!bs->drv) {
3075 return;
3076 }
3077
7ea2d269
AK
3078 if (!(bs->open_flags & BDRV_O_INCOMING)) {
3079 return;
3080 }
3081 bs->open_flags &= ~BDRV_O_INCOMING;
3082
3456a8d1 3083 if (bs->drv->bdrv_invalidate_cache) {
5a8a30db 3084 bs->drv->bdrv_invalidate_cache(bs, &local_err);
3456a8d1 3085 } else if (bs->file) {
9a4f4c31 3086 bdrv_invalidate_cache(bs->file->bs, &local_err);
5a8a30db
KW
3087 }
3088 if (local_err) {
3089 error_propagate(errp, local_err);
3090 return;
0f15423c 3091 }
3456a8d1 3092
5a8a30db
KW
3093 ret = refresh_total_sectors(bs, bs->total_sectors);
3094 if (ret < 0) {
3095 error_setg_errno(errp, -ret, "Could not refresh total sector count");
3096 return;
3097 }
0f15423c
AL
3098}
3099
5a8a30db 3100void bdrv_invalidate_cache_all(Error **errp)
0f15423c
AL
3101{
3102 BlockDriverState *bs;
5a8a30db 3103 Error *local_err = NULL;
0f15423c 3104
dc364f4c 3105 QTAILQ_FOREACH(bs, &bdrv_states, device_list) {
ed78cda3
SH
3106 AioContext *aio_context = bdrv_get_aio_context(bs);
3107
3108 aio_context_acquire(aio_context);
5a8a30db 3109 bdrv_invalidate_cache(bs, &local_err);
ed78cda3 3110 aio_context_release(aio_context);
5a8a30db
KW
3111 if (local_err) {
3112 error_propagate(errp, local_err);
3113 return;
3114 }
0f15423c
AL
3115 }
3116}
3117
19cb3738
FB
3118/**************************************************************/
3119/* removable device support */
3120
3121/**
3122 * Return TRUE if the media is present
3123 */
e031f750 3124bool bdrv_is_inserted(BlockDriverState *bs)
19cb3738
FB
3125{
3126 BlockDriver *drv = bs->drv;
28d7a789 3127 BdrvChild *child;
a1aff5bf 3128
e031f750
HR
3129 if (!drv) {
3130 return false;
3131 }
28d7a789
HR
3132 if (drv->bdrv_is_inserted) {
3133 return drv->bdrv_is_inserted(bs);
3134 }
3135 QLIST_FOREACH(child, &bs->children, next) {
3136 if (!bdrv_is_inserted(child->bs)) {
3137 return false;
3138 }
e031f750 3139 }
28d7a789 3140 return true;
19cb3738
FB
3141}
3142
3143/**
8e49ca46
MA
3144 * Return whether the media changed since the last call to this
3145 * function, or -ENOTSUP if we don't know. Most drivers don't know.
19cb3738
FB
3146 */
3147int bdrv_media_changed(BlockDriverState *bs)
3148{
3149 BlockDriver *drv = bs->drv;
19cb3738 3150
8e49ca46
MA
3151 if (drv && drv->bdrv_media_changed) {
3152 return drv->bdrv_media_changed(bs);
3153 }
3154 return -ENOTSUP;
19cb3738
FB
3155}
3156
3157/**
3158 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
3159 */
f36f3949 3160void bdrv_eject(BlockDriverState *bs, bool eject_flag)
19cb3738
FB
3161{
3162 BlockDriver *drv = bs->drv;
bfb197e0 3163 const char *device_name;
19cb3738 3164
822e1cd1
MA
3165 if (drv && drv->bdrv_eject) {
3166 drv->bdrv_eject(bs, eject_flag);
19cb3738 3167 }
6f382ed2 3168
bfb197e0
MA
3169 device_name = bdrv_get_device_name(bs);
3170 if (device_name[0] != '\0') {
3171 qapi_event_send_device_tray_moved(device_name,
a5ee7bd4 3172 eject_flag, &error_abort);
6f382ed2 3173 }
19cb3738
FB
3174}
3175
19cb3738
FB
3176/**
3177 * Lock or unlock the media (if it is locked, the user won't be able
3178 * to eject it manually).
3179 */
025e849a 3180void bdrv_lock_medium(BlockDriverState *bs, bool locked)
19cb3738
FB
3181{
3182 BlockDriver *drv = bs->drv;
3183
025e849a 3184 trace_bdrv_lock_medium(bs, locked);
b8c6d095 3185
025e849a
MA
3186 if (drv && drv->bdrv_lock_medium) {
3187 drv->bdrv_lock_medium(bs, locked);
19cb3738
FB
3188 }
3189}
985a03b0 3190
0db6e54a
FZ
3191BdrvDirtyBitmap *bdrv_find_dirty_bitmap(BlockDriverState *bs, const char *name)
3192{
3193 BdrvDirtyBitmap *bm;
3194
3195 assert(name);
3196 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
3197 if (bm->name && !strcmp(name, bm->name)) {
3198 return bm;
3199 }
3200 }
3201 return NULL;
3202}
3203
20dca810 3204void bdrv_dirty_bitmap_make_anon(BdrvDirtyBitmap *bitmap)
0db6e54a 3205{
9bd2b08f 3206 assert(!bdrv_dirty_bitmap_frozen(bitmap));
0db6e54a
FZ
3207 g_free(bitmap->name);
3208 bitmap->name = NULL;
3209}
3210
3211BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs,
5fba6c0e 3212 uint32_t granularity,
0db6e54a 3213 const char *name,
b8afb520 3214 Error **errp)
7cd1e32a
LS
3215{
3216 int64_t bitmap_size;
e4654d2d 3217 BdrvDirtyBitmap *bitmap;
5fba6c0e 3218 uint32_t sector_granularity;
a55eb92c 3219
50717e94
PB
3220 assert((granularity & (granularity - 1)) == 0);
3221
0db6e54a
FZ
3222 if (name && bdrv_find_dirty_bitmap(bs, name)) {
3223 error_setg(errp, "Bitmap already exists: %s", name);
3224 return NULL;
3225 }
5fba6c0e
JS
3226 sector_granularity = granularity >> BDRV_SECTOR_BITS;
3227 assert(sector_granularity);
57322b78 3228 bitmap_size = bdrv_nb_sectors(bs);
b8afb520
FZ
3229 if (bitmap_size < 0) {
3230 error_setg_errno(errp, -bitmap_size, "could not get length of device");
3231 errno = -bitmap_size;
3232 return NULL;
3233 }
5839e53b 3234 bitmap = g_new0(BdrvDirtyBitmap, 1);
5fba6c0e 3235 bitmap->bitmap = hbitmap_alloc(bitmap_size, ctz32(sector_granularity));
e74e6b78 3236 bitmap->size = bitmap_size;
0db6e54a 3237 bitmap->name = g_strdup(name);
b8e6fb75 3238 bitmap->disabled = false;
e4654d2d
FZ
3239 QLIST_INSERT_HEAD(&bs->dirty_bitmaps, bitmap, list);
3240 return bitmap;
3241}
3242
9bd2b08f
JS
3243bool bdrv_dirty_bitmap_frozen(BdrvDirtyBitmap *bitmap)
3244{
3245 return bitmap->successor;
3246}
3247
b8e6fb75
JS
3248bool bdrv_dirty_bitmap_enabled(BdrvDirtyBitmap *bitmap)
3249{
9bd2b08f
JS
3250 return !(bitmap->disabled || bitmap->successor);
3251}
3252
9abe3bdc
JS
3253DirtyBitmapStatus bdrv_dirty_bitmap_status(BdrvDirtyBitmap *bitmap)
3254{
3255 if (bdrv_dirty_bitmap_frozen(bitmap)) {
3256 return DIRTY_BITMAP_STATUS_FROZEN;
3257 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
3258 return DIRTY_BITMAP_STATUS_DISABLED;
3259 } else {
3260 return DIRTY_BITMAP_STATUS_ACTIVE;
3261 }
3262}
3263
9bd2b08f
JS
3264/**
3265 * Create a successor bitmap destined to replace this bitmap after an operation.
3266 * Requires that the bitmap is not frozen and has no successor.
3267 */
3268int bdrv_dirty_bitmap_create_successor(BlockDriverState *bs,
3269 BdrvDirtyBitmap *bitmap, Error **errp)
3270{
3271 uint64_t granularity;
3272 BdrvDirtyBitmap *child;
3273
3274 if (bdrv_dirty_bitmap_frozen(bitmap)) {
3275 error_setg(errp, "Cannot create a successor for a bitmap that is "
3276 "currently frozen");
3277 return -1;
3278 }
3279 assert(!bitmap->successor);
3280
3281 /* Create an anonymous successor */
3282 granularity = bdrv_dirty_bitmap_granularity(bitmap);
3283 child = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp);
3284 if (!child) {
3285 return -1;
3286 }
3287
3288 /* Successor will be on or off based on our current state. */
3289 child->disabled = bitmap->disabled;
3290
3291 /* Install the successor and freeze the parent */
3292 bitmap->successor = child;
3293 return 0;
3294}
3295
3296/**
3297 * For a bitmap with a successor, yield our name to the successor,
3298 * delete the old bitmap, and return a handle to the new bitmap.
3299 */
3300BdrvDirtyBitmap *bdrv_dirty_bitmap_abdicate(BlockDriverState *bs,
3301 BdrvDirtyBitmap *bitmap,
3302 Error **errp)
3303{
3304 char *name;
3305 BdrvDirtyBitmap *successor = bitmap->successor;
3306
3307 if (successor == NULL) {
3308 error_setg(errp, "Cannot relinquish control if "
3309 "there's no successor present");
3310 return NULL;
3311 }
3312
3313 name = bitmap->name;
3314 bitmap->name = NULL;
3315 successor->name = name;
3316 bitmap->successor = NULL;
3317 bdrv_release_dirty_bitmap(bs, bitmap);
3318
3319 return successor;
3320}
3321
3322/**
3323 * In cases of failure where we can no longer safely delete the parent,
3324 * we may wish to re-join the parent and child/successor.
3325 * The merged parent will be un-frozen, but not explicitly re-enabled.
3326 */
3327BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BlockDriverState *bs,
3328 BdrvDirtyBitmap *parent,
3329 Error **errp)
3330{
3331 BdrvDirtyBitmap *successor = parent->successor;
3332
3333 if (!successor) {
3334 error_setg(errp, "Cannot reclaim a successor when none is present");
3335 return NULL;
3336 }
3337
3338 if (!hbitmap_merge(parent->bitmap, successor->bitmap)) {
3339 error_setg(errp, "Merging of parent and successor bitmap failed");
3340 return NULL;
3341 }
3342 bdrv_release_dirty_bitmap(bs, successor);
3343 parent->successor = NULL;
3344
3345 return parent;
b8e6fb75
JS
3346}
3347
ce1ffea8
JS
3348/**
3349 * Truncates _all_ bitmaps attached to a BDS.
3350 */
3351static void bdrv_dirty_bitmap_truncate(BlockDriverState *bs)
3352{
3353 BdrvDirtyBitmap *bitmap;
3354 uint64_t size = bdrv_nb_sectors(bs);
3355
3356 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
06207b0f 3357 assert(!bdrv_dirty_bitmap_frozen(bitmap));
ce1ffea8 3358 hbitmap_truncate(bitmap->bitmap, size);
5270b6a0 3359 bitmap->size = size;
ce1ffea8
JS
3360 }
3361}
3362
e4654d2d
FZ
3363void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap)
3364{
3365 BdrvDirtyBitmap *bm, *next;
3366 QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) {
3367 if (bm == bitmap) {
9bd2b08f 3368 assert(!bdrv_dirty_bitmap_frozen(bm));
e4654d2d
FZ
3369 QLIST_REMOVE(bitmap, list);
3370 hbitmap_free(bitmap->bitmap);
0db6e54a 3371 g_free(bitmap->name);
e4654d2d
FZ
3372 g_free(bitmap);
3373 return;
a55eb92c 3374 }
7cd1e32a
LS
3375 }
3376}
3377
b8e6fb75
JS
3378void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
3379{
9bd2b08f 3380 assert(!bdrv_dirty_bitmap_frozen(bitmap));
b8e6fb75
JS
3381 bitmap->disabled = true;
3382}
3383
3384void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
3385{
9bd2b08f 3386 assert(!bdrv_dirty_bitmap_frozen(bitmap));
b8e6fb75
JS
3387 bitmap->disabled = false;
3388}
3389
21b56835
FZ
3390BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs)
3391{
3392 BdrvDirtyBitmap *bm;
3393 BlockDirtyInfoList *list = NULL;
3394 BlockDirtyInfoList **plist = &list;
3395
3396 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
5839e53b
MA
3397 BlockDirtyInfo *info = g_new0(BlockDirtyInfo, 1);
3398 BlockDirtyInfoList *entry = g_new0(BlockDirtyInfoList, 1);
20dca810 3399 info->count = bdrv_get_dirty_count(bm);
592fdd02 3400 info->granularity = bdrv_dirty_bitmap_granularity(bm);
0db6e54a
FZ
3401 info->has_name = !!bm->name;
3402 info->name = g_strdup(bm->name);
9abe3bdc 3403 info->status = bdrv_dirty_bitmap_status(bm);
21b56835
FZ
3404 entry->value = info;
3405 *plist = entry;
3406 plist = &entry->next;
3407 }
3408
3409 return list;
3410}
3411
e4654d2d 3412int bdrv_get_dirty(BlockDriverState *bs, BdrvDirtyBitmap *bitmap, int64_t sector)
7cd1e32a 3413{
e4654d2d
FZ
3414 if (bitmap) {
3415 return hbitmap_get(bitmap->bitmap, sector);
7cd1e32a
LS
3416 } else {
3417 return 0;
3418 }
3419}
3420
341ebc2f
JS
3421/**
3422 * Chooses a default granularity based on the existing cluster size,
3423 * but clamped between [4K, 64K]. Defaults to 64K in the case that there
3424 * is no cluster size information available.
3425 */
3426uint32_t bdrv_get_default_bitmap_granularity(BlockDriverState *bs)
3427{
3428 BlockDriverInfo bdi;
3429 uint32_t granularity;
3430
3431 if (bdrv_get_info(bs, &bdi) >= 0 && bdi.cluster_size > 0) {
3432 granularity = MAX(4096, bdi.cluster_size);
3433 granularity = MIN(65536, granularity);
3434 } else {
3435 granularity = 65536;
3436 }
3437
3438 return granularity;
3439}
3440
592fdd02
JS
3441uint32_t bdrv_dirty_bitmap_granularity(BdrvDirtyBitmap *bitmap)
3442{
3443 return BDRV_SECTOR_SIZE << hbitmap_granularity(bitmap->bitmap);
3444}
3445
20dca810 3446void bdrv_dirty_iter_init(BdrvDirtyBitmap *bitmap, HBitmapIter *hbi)
1755da16 3447{
e4654d2d 3448 hbitmap_iter_init(hbi, bitmap->bitmap, 0);
1755da16
PB
3449}
3450
20dca810 3451void bdrv_set_dirty_bitmap(BdrvDirtyBitmap *bitmap,
c4237dfa
VSO
3452 int64_t cur_sector, int nr_sectors)
3453{
b8e6fb75 3454 assert(bdrv_dirty_bitmap_enabled(bitmap));
c4237dfa
VSO
3455 hbitmap_set(bitmap->bitmap, cur_sector, nr_sectors);
3456}
3457
20dca810 3458void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap *bitmap,
c4237dfa
VSO
3459 int64_t cur_sector, int nr_sectors)
3460{
b8e6fb75 3461 assert(bdrv_dirty_bitmap_enabled(bitmap));
c4237dfa
VSO
3462 hbitmap_reset(bitmap->bitmap, cur_sector, nr_sectors);
3463}
3464
df9a681d 3465void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap **out)
e74e6b78
JS
3466{
3467 assert(bdrv_dirty_bitmap_enabled(bitmap));
df9a681d
FZ
3468 if (!out) {
3469 hbitmap_reset_all(bitmap->bitmap);
3470 } else {
3471 HBitmap *backup = bitmap->bitmap;
3472 bitmap->bitmap = hbitmap_alloc(bitmap->size,
3473 hbitmap_granularity(backup));
3474 *out = backup;
3475 }
3476}
3477
3478void bdrv_undo_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap *in)
3479{
3480 HBitmap *tmp = bitmap->bitmap;
3481 assert(bdrv_dirty_bitmap_enabled(bitmap));
3482 bitmap->bitmap = in;
3483 hbitmap_free(tmp);
e74e6b78
JS
3484}
3485
e0c47b6c
SH
3486void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector,
3487 int nr_sectors)
1755da16 3488{
e4654d2d
FZ
3489 BdrvDirtyBitmap *bitmap;
3490 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
b8e6fb75
JS
3491 if (!bdrv_dirty_bitmap_enabled(bitmap)) {
3492 continue;
3493 }
e4654d2d
FZ
3494 hbitmap_set(bitmap->bitmap, cur_sector, nr_sectors);
3495 }
1755da16
PB
3496}
3497
d58d8453
JS
3498/**
3499 * Advance an HBitmapIter to an arbitrary offset.
3500 */
3501void bdrv_set_dirty_iter(HBitmapIter *hbi, int64_t offset)
3502{
3503 assert(hbi->hb);
3504 hbitmap_iter_init(hbi, hbi->hb, offset);
3505}
3506
20dca810 3507int64_t bdrv_get_dirty_count(BdrvDirtyBitmap *bitmap)
aaa0eb75 3508{
e4654d2d 3509 return hbitmap_count(bitmap->bitmap);
aaa0eb75 3510}
f88e1a42 3511
9fcb0251
FZ
3512/* Get a reference to bs */
3513void bdrv_ref(BlockDriverState *bs)
3514{
3515 bs->refcnt++;
3516}
3517
3518/* Release a previously grabbed reference to bs.
3519 * If after releasing, reference count is zero, the BlockDriverState is
3520 * deleted. */
3521void bdrv_unref(BlockDriverState *bs)
3522{
9a4d5ca6
JC
3523 if (!bs) {
3524 return;
3525 }
9fcb0251
FZ
3526 assert(bs->refcnt > 0);
3527 if (--bs->refcnt == 0) {
3528 bdrv_delete(bs);
3529 }
3530}
3531
fbe40ff7
FZ
3532struct BdrvOpBlocker {
3533 Error *reason;
3534 QLIST_ENTRY(BdrvOpBlocker) list;
3535};
3536
3537bool bdrv_op_is_blocked(BlockDriverState *bs, BlockOpType op, Error **errp)
3538{
3539 BdrvOpBlocker *blocker;
3540 assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
3541 if (!QLIST_EMPTY(&bs->op_blockers[op])) {
3542 blocker = QLIST_FIRST(&bs->op_blockers[op]);
3543 if (errp) {
81e5f78a
AG
3544 error_setg(errp, "Node '%s' is busy: %s",
3545 bdrv_get_device_or_node_name(bs),
bfb197e0 3546 error_get_pretty(blocker->reason));
fbe40ff7
FZ
3547 }
3548 return true;
3549 }
3550 return false;
3551}
3552
3553void bdrv_op_block(BlockDriverState *bs, BlockOpType op, Error *reason)
3554{
3555 BdrvOpBlocker *blocker;
3556 assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
3557
5839e53b 3558 blocker = g_new0(BdrvOpBlocker, 1);
fbe40ff7
FZ
3559 blocker->reason = reason;
3560 QLIST_INSERT_HEAD(&bs->op_blockers[op], blocker, list);
3561}
3562
3563void bdrv_op_unblock(BlockDriverState *bs, BlockOpType op, Error *reason)
3564{
3565 BdrvOpBlocker *blocker, *next;
3566 assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
3567 QLIST_FOREACH_SAFE(blocker, &bs->op_blockers[op], list, next) {
3568 if (blocker->reason == reason) {
3569 QLIST_REMOVE(blocker, list);
3570 g_free(blocker);
3571 }
3572 }
3573}
3574
3575void bdrv_op_block_all(BlockDriverState *bs, Error *reason)
3576{
3577 int i;
3578 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
3579 bdrv_op_block(bs, i, reason);
3580 }
3581}
3582
3583void bdrv_op_unblock_all(BlockDriverState *bs, Error *reason)
3584{
3585 int i;
3586 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
3587 bdrv_op_unblock(bs, i, reason);
3588 }
3589}
3590
3591bool bdrv_op_blocker_is_empty(BlockDriverState *bs)
3592{
3593 int i;
3594
3595 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
3596 if (!QLIST_EMPTY(&bs->op_blockers[i])) {
3597 return false;
3598 }
3599 }
3600 return true;
3601}
3602
d92ada22
LC
3603void bdrv_img_create(const char *filename, const char *fmt,
3604 const char *base_filename, const char *base_fmt,
f382d43a
MR
3605 char *options, uint64_t img_size, int flags,
3606 Error **errp, bool quiet)
f88e1a42 3607{
83d0521a
CL
3608 QemuOptsList *create_opts = NULL;
3609 QemuOpts *opts = NULL;
3610 const char *backing_fmt, *backing_file;
3611 int64_t size;
f88e1a42 3612 BlockDriver *drv, *proto_drv;
cc84d90f 3613 Error *local_err = NULL;
f88e1a42
JS
3614 int ret = 0;
3615
3616 /* Find driver and parse its options */
3617 drv = bdrv_find_format(fmt);
3618 if (!drv) {
71c79813 3619 error_setg(errp, "Unknown file format '%s'", fmt);
d92ada22 3620 return;
f88e1a42
JS
3621 }
3622
b65a5e12 3623 proto_drv = bdrv_find_protocol(filename, true, errp);
f88e1a42 3624 if (!proto_drv) {
d92ada22 3625 return;
f88e1a42
JS
3626 }
3627
c6149724
HR
3628 if (!drv->create_opts) {
3629 error_setg(errp, "Format driver '%s' does not support image creation",
3630 drv->format_name);
3631 return;
3632 }
3633
3634 if (!proto_drv->create_opts) {
3635 error_setg(errp, "Protocol driver '%s' does not support image creation",
3636 proto_drv->format_name);
3637 return;
3638 }
3639
c282e1fd
CL
3640 create_opts = qemu_opts_append(create_opts, drv->create_opts);
3641 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
f88e1a42
JS
3642
3643 /* Create parameter list with default values */
83d0521a 3644 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
39101f25 3645 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
f88e1a42
JS
3646
3647 /* Parse -o options */
3648 if (options) {
dc523cd3
MA
3649 qemu_opts_do_parse(opts, options, NULL, &local_err);
3650 if (local_err) {
3651 error_report_err(local_err);
3652 local_err = NULL;
83d0521a 3653 error_setg(errp, "Invalid options for file format '%s'", fmt);
f88e1a42
JS
3654 goto out;
3655 }
3656 }
3657
3658 if (base_filename) {
f43e47db 3659 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &local_err);
6be4194b 3660 if (local_err) {
71c79813
LC
3661 error_setg(errp, "Backing file not supported for file format '%s'",
3662 fmt);
f88e1a42
JS
3663 goto out;
3664 }
3665 }
3666
3667 if (base_fmt) {
f43e47db 3668 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &local_err);
6be4194b 3669 if (local_err) {
71c79813
LC
3670 error_setg(errp, "Backing file format not supported for file "
3671 "format '%s'", fmt);
f88e1a42
JS
3672 goto out;
3673 }
3674 }
3675
83d0521a
CL
3676 backing_file = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
3677 if (backing_file) {
3678 if (!strcmp(filename, backing_file)) {
71c79813
LC
3679 error_setg(errp, "Error: Trying to create an image with the "
3680 "same filename as the backing file");
792da93a
JS
3681 goto out;
3682 }
3683 }
3684
83d0521a 3685 backing_fmt = qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT);
f88e1a42
JS
3686
3687 // The size for the image must always be specified, with one exception:
3688 // If we are using a backing file, we can obtain the size from there
83d0521a
CL
3689 size = qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 0);
3690 if (size == -1) {
3691 if (backing_file) {
66f6b814 3692 BlockDriverState *bs;
29168018 3693 char *full_backing = g_new0(char, PATH_MAX);
52bf1e72 3694 int64_t size;
63090dac 3695 int back_flags;
e6641719 3696 QDict *backing_options = NULL;
63090dac 3697
29168018
HR
3698 bdrv_get_full_backing_filename_from_filename(filename, backing_file,
3699 full_backing, PATH_MAX,
3700 &local_err);
3701 if (local_err) {
3702 g_free(full_backing);
3703 goto out;
3704 }
3705
63090dac
PB
3706 /* backing files always opened read-only */
3707 back_flags =
3708 flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
f88e1a42 3709
e6641719
HR
3710 if (backing_fmt) {
3711 backing_options = qdict_new();
3712 qdict_put(backing_options, "driver",
3713 qstring_from_str(backing_fmt));
3714 }
3715
f67503e5 3716 bs = NULL;
e6641719 3717 ret = bdrv_open(&bs, full_backing, NULL, backing_options,
6ebf9aa2 3718 back_flags, &local_err);
29168018 3719 g_free(full_backing);
f88e1a42 3720 if (ret < 0) {
f88e1a42
JS
3721 goto out;
3722 }
52bf1e72
MA
3723 size = bdrv_getlength(bs);
3724 if (size < 0) {
3725 error_setg_errno(errp, -size, "Could not get size of '%s'",
3726 backing_file);
3727 bdrv_unref(bs);
3728 goto out;
3729 }
f88e1a42 3730
39101f25 3731 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, size, &error_abort);
66f6b814
HR
3732
3733 bdrv_unref(bs);
f88e1a42 3734 } else {
71c79813 3735 error_setg(errp, "Image creation needs a size parameter");
f88e1a42
JS
3736 goto out;
3737 }
3738 }
3739
f382d43a 3740 if (!quiet) {
fe646693 3741 printf("Formatting '%s', fmt=%s ", filename, fmt);
43c5d8f8 3742 qemu_opts_print(opts, " ");
f382d43a
MR
3743 puts("");
3744 }
83d0521a 3745
c282e1fd 3746 ret = bdrv_create(drv, filename, opts, &local_err);
83d0521a 3747
cc84d90f
HR
3748 if (ret == -EFBIG) {
3749 /* This is generally a better message than whatever the driver would
3750 * deliver (especially because of the cluster_size_hint), since that
3751 * is most probably not much different from "image too large". */
3752 const char *cluster_size_hint = "";
83d0521a 3753 if (qemu_opt_get_size(opts, BLOCK_OPT_CLUSTER_SIZE, 0)) {
cc84d90f 3754 cluster_size_hint = " (try using a larger cluster size)";
f88e1a42 3755 }
cc84d90f
HR
3756 error_setg(errp, "The image size is too large for file format '%s'"
3757 "%s", fmt, cluster_size_hint);
3758 error_free(local_err);
3759 local_err = NULL;
f88e1a42
JS
3760 }
3761
3762out:
83d0521a
CL
3763 qemu_opts_del(opts);
3764 qemu_opts_free(create_opts);
84d18f06 3765 if (local_err) {
cc84d90f
HR
3766 error_propagate(errp, local_err);
3767 }
f88e1a42 3768}
85d126f3
SH
3769
3770AioContext *bdrv_get_aio_context(BlockDriverState *bs)
3771{
dcd04228
SH
3772 return bs->aio_context;
3773}
3774
3775void bdrv_detach_aio_context(BlockDriverState *bs)
3776{
33384421
HR
3777 BdrvAioNotifier *baf;
3778
dcd04228
SH
3779 if (!bs->drv) {
3780 return;
3781 }
3782
33384421
HR
3783 QLIST_FOREACH(baf, &bs->aio_notifiers, list) {
3784 baf->detach_aio_context(baf->opaque);
3785 }
3786
a0d64a61 3787 if (bs->throttle_state) {
0e5b0a2d 3788 throttle_timers_detach_aio_context(&bs->throttle_timers);
13af91eb 3789 }
dcd04228
SH
3790 if (bs->drv->bdrv_detach_aio_context) {
3791 bs->drv->bdrv_detach_aio_context(bs);
3792 }
3793 if (bs->file) {
9a4f4c31 3794 bdrv_detach_aio_context(bs->file->bs);
dcd04228 3795 }
760e0063
KW
3796 if (bs->backing) {
3797 bdrv_detach_aio_context(bs->backing->bs);
dcd04228
SH
3798 }
3799
3800 bs->aio_context = NULL;
3801}
3802
3803void bdrv_attach_aio_context(BlockDriverState *bs,
3804 AioContext *new_context)
3805{
33384421
HR
3806 BdrvAioNotifier *ban;
3807
dcd04228
SH
3808 if (!bs->drv) {
3809 return;
3810 }
3811
3812 bs->aio_context = new_context;
3813
760e0063
KW
3814 if (bs->backing) {
3815 bdrv_attach_aio_context(bs->backing->bs, new_context);
dcd04228
SH
3816 }
3817 if (bs->file) {
9a4f4c31 3818 bdrv_attach_aio_context(bs->file->bs, new_context);
dcd04228
SH
3819 }
3820 if (bs->drv->bdrv_attach_aio_context) {
3821 bs->drv->bdrv_attach_aio_context(bs, new_context);
3822 }
a0d64a61 3823 if (bs->throttle_state) {
0e5b0a2d 3824 throttle_timers_attach_aio_context(&bs->throttle_timers, new_context);
13af91eb 3825 }
33384421
HR
3826
3827 QLIST_FOREACH(ban, &bs->aio_notifiers, list) {
3828 ban->attached_aio_context(new_context, ban->opaque);
3829 }
dcd04228
SH
3830}
3831
3832void bdrv_set_aio_context(BlockDriverState *bs, AioContext *new_context)
3833{
53ec73e2 3834 bdrv_drain(bs); /* ensure there are no in-flight requests */
dcd04228
SH
3835
3836 bdrv_detach_aio_context(bs);
3837
3838 /* This function executes in the old AioContext so acquire the new one in
3839 * case it runs in a different thread.
3840 */
3841 aio_context_acquire(new_context);
3842 bdrv_attach_aio_context(bs, new_context);
3843 aio_context_release(new_context);
85d126f3 3844}
d616b224 3845
33384421
HR
3846void bdrv_add_aio_context_notifier(BlockDriverState *bs,
3847 void (*attached_aio_context)(AioContext *new_context, void *opaque),
3848 void (*detach_aio_context)(void *opaque), void *opaque)
3849{
3850 BdrvAioNotifier *ban = g_new(BdrvAioNotifier, 1);
3851 *ban = (BdrvAioNotifier){
3852 .attached_aio_context = attached_aio_context,
3853 .detach_aio_context = detach_aio_context,
3854 .opaque = opaque
3855 };
3856
3857 QLIST_INSERT_HEAD(&bs->aio_notifiers, ban, list);
3858}
3859
3860void bdrv_remove_aio_context_notifier(BlockDriverState *bs,
3861 void (*attached_aio_context)(AioContext *,
3862 void *),
3863 void (*detach_aio_context)(void *),
3864 void *opaque)
3865{
3866 BdrvAioNotifier *ban, *ban_next;
3867
3868 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) {
3869 if (ban->attached_aio_context == attached_aio_context &&
3870 ban->detach_aio_context == detach_aio_context &&
3871 ban->opaque == opaque)
3872 {
3873 QLIST_REMOVE(ban, list);
3874 g_free(ban);
3875
3876 return;
3877 }
3878 }
3879
3880 abort();
3881}
3882
77485434
HR
3883int bdrv_amend_options(BlockDriverState *bs, QemuOpts *opts,
3884 BlockDriverAmendStatusCB *status_cb)
6f176b48 3885{
c282e1fd 3886 if (!bs->drv->bdrv_amend_options) {
6f176b48
HR
3887 return -ENOTSUP;
3888 }
77485434 3889 return bs->drv->bdrv_amend_options(bs, opts, status_cb);
6f176b48 3890}
f6186f49 3891
b5042a36
BC
3892/* This function will be called by the bdrv_recurse_is_first_non_filter method
3893 * of block filter and by bdrv_is_first_non_filter.
3894 * It is used to test if the given bs is the candidate or recurse more in the
3895 * node graph.
212a5a8f 3896 */
b5042a36 3897bool bdrv_recurse_is_first_non_filter(BlockDriverState *bs,
212a5a8f 3898 BlockDriverState *candidate)
f6186f49 3899{
b5042a36
BC
3900 /* return false if basic checks fails */
3901 if (!bs || !bs->drv) {
212a5a8f 3902 return false;
f6186f49
BC
3903 }
3904
b5042a36
BC
3905 /* the code reached a non block filter driver -> check if the bs is
3906 * the same as the candidate. It's the recursion termination condition.
3907 */
3908 if (!bs->drv->is_filter) {
3909 return bs == candidate;
212a5a8f 3910 }
b5042a36 3911 /* Down this path the driver is a block filter driver */
212a5a8f 3912
b5042a36
BC
3913 /* If the block filter recursion method is defined use it to recurse down
3914 * the node graph.
3915 */
3916 if (bs->drv->bdrv_recurse_is_first_non_filter) {
212a5a8f 3917 return bs->drv->bdrv_recurse_is_first_non_filter(bs, candidate);
f6186f49
BC
3918 }
3919
b5042a36
BC
3920 /* the driver is a block filter but don't allow to recurse -> return false
3921 */
3922 return false;
f6186f49
BC
3923}
3924
212a5a8f
BC
3925/* This function checks if the candidate is the first non filter bs down it's
3926 * bs chain. Since we don't have pointers to parents it explore all bs chains
3927 * from the top. Some filters can choose not to pass down the recursion.
3928 */
3929bool bdrv_is_first_non_filter(BlockDriverState *candidate)
f6186f49 3930{
212a5a8f
BC
3931 BlockDriverState *bs;
3932
3933 /* walk down the bs forest recursively */
3934 QTAILQ_FOREACH(bs, &bdrv_states, device_list) {
3935 bool perm;
3936
b5042a36 3937 /* try to recurse in this top level bs */
e6dc8a1f 3938 perm = bdrv_recurse_is_first_non_filter(bs, candidate);
212a5a8f
BC
3939
3940 /* candidate is the first non filter */
3941 if (perm) {
3942 return true;
3943 }
3944 }
3945
3946 return false;
f6186f49 3947}
09158f00 3948
e12f3784
WC
3949BlockDriverState *check_to_replace_node(BlockDriverState *parent_bs,
3950 const char *node_name, Error **errp)
09158f00
BC
3951{
3952 BlockDriverState *to_replace_bs = bdrv_find_node(node_name);
5a7e7a0b
SH
3953 AioContext *aio_context;
3954
09158f00
BC
3955 if (!to_replace_bs) {
3956 error_setg(errp, "Node name '%s' not found", node_name);
3957 return NULL;
3958 }
3959
5a7e7a0b
SH
3960 aio_context = bdrv_get_aio_context(to_replace_bs);
3961 aio_context_acquire(aio_context);
3962
09158f00 3963 if (bdrv_op_is_blocked(to_replace_bs, BLOCK_OP_TYPE_REPLACE, errp)) {
5a7e7a0b
SH
3964 to_replace_bs = NULL;
3965 goto out;
09158f00
BC
3966 }
3967
3968 /* We don't want arbitrary node of the BDS chain to be replaced only the top
3969 * most non filter in order to prevent data corruption.
3970 * Another benefit is that this tests exclude backing files which are
3971 * blocked by the backing blockers.
3972 */
e12f3784 3973 if (!bdrv_recurse_is_first_non_filter(parent_bs, to_replace_bs)) {
09158f00 3974 error_setg(errp, "Only top most non filter can be replaced");
5a7e7a0b
SH
3975 to_replace_bs = NULL;
3976 goto out;
09158f00
BC
3977 }
3978
5a7e7a0b
SH
3979out:
3980 aio_context_release(aio_context);
09158f00
BC
3981 return to_replace_bs;
3982}
448ad91d 3983
91af7014
HR
3984static bool append_open_options(QDict *d, BlockDriverState *bs)
3985{
3986 const QDictEntry *entry;
9e700c1a 3987 QemuOptDesc *desc;
260fecf1 3988 BdrvChild *child;
91af7014 3989 bool found_any = false;
260fecf1 3990 const char *p;
91af7014
HR
3991
3992 for (entry = qdict_first(bs->options); entry;
3993 entry = qdict_next(bs->options, entry))
3994 {
260fecf1
KW
3995 /* Exclude options for children */
3996 QLIST_FOREACH(child, &bs->children, next) {
3997 if (strstart(qdict_entry_key(entry), child->name, &p)
3998 && (!*p || *p == '.'))
3999 {
4000 break;
4001 }
4002 }
4003 if (child) {
9e700c1a 4004 continue;
91af7014 4005 }
9e700c1a
KW
4006
4007 /* And exclude all non-driver-specific options */
4008 for (desc = bdrv_runtime_opts.desc; desc->name; desc++) {
4009 if (!strcmp(qdict_entry_key(entry), desc->name)) {
4010 break;
4011 }
4012 }
4013 if (desc->name) {
4014 continue;
4015 }
4016
4017 qobject_incref(qdict_entry_value(entry));
4018 qdict_put_obj(d, qdict_entry_key(entry), qdict_entry_value(entry));
4019 found_any = true;
91af7014
HR
4020 }
4021
4022 return found_any;
4023}
4024
4025/* Updates the following BDS fields:
4026 * - exact_filename: A filename which may be used for opening a block device
4027 * which (mostly) equals the given BDS (even without any
4028 * other options; so reading and writing must return the same
4029 * results, but caching etc. may be different)
4030 * - full_open_options: Options which, when given when opening a block device
4031 * (without a filename), result in a BDS (mostly)
4032 * equalling the given one
4033 * - filename: If exact_filename is set, it is copied here. Otherwise,
4034 * full_open_options is converted to a JSON object, prefixed with
4035 * "json:" (for use through the JSON pseudo protocol) and put here.
4036 */
4037void bdrv_refresh_filename(BlockDriverState *bs)
4038{
4039 BlockDriver *drv = bs->drv;
4040 QDict *opts;
4041
4042 if (!drv) {
4043 return;
4044 }
4045
4046 /* This BDS's file name will most probably depend on its file's name, so
4047 * refresh that first */
4048 if (bs->file) {
9a4f4c31 4049 bdrv_refresh_filename(bs->file->bs);
91af7014
HR
4050 }
4051
4052 if (drv->bdrv_refresh_filename) {
4053 /* Obsolete information is of no use here, so drop the old file name
4054 * information before refreshing it */
4055 bs->exact_filename[0] = '\0';
4056 if (bs->full_open_options) {
4057 QDECREF(bs->full_open_options);
4058 bs->full_open_options = NULL;
4059 }
4060
4cdd01d3
KW
4061 opts = qdict_new();
4062 append_open_options(opts, bs);
4063 drv->bdrv_refresh_filename(bs, opts);
4064 QDECREF(opts);
91af7014
HR
4065 } else if (bs->file) {
4066 /* Try to reconstruct valid information from the underlying file */
4067 bool has_open_options;
4068
4069 bs->exact_filename[0] = '\0';
4070 if (bs->full_open_options) {
4071 QDECREF(bs->full_open_options);
4072 bs->full_open_options = NULL;
4073 }
4074
4075 opts = qdict_new();
4076 has_open_options = append_open_options(opts, bs);
4077
4078 /* If no specific options have been given for this BDS, the filename of
4079 * the underlying file should suffice for this one as well */
9a4f4c31
KW
4080 if (bs->file->bs->exact_filename[0] && !has_open_options) {
4081 strcpy(bs->exact_filename, bs->file->bs->exact_filename);
91af7014
HR
4082 }
4083 /* Reconstructing the full options QDict is simple for most format block
4084 * drivers, as long as the full options are known for the underlying
4085 * file BDS. The full options QDict of that file BDS should somehow
4086 * contain a representation of the filename, therefore the following
4087 * suffices without querying the (exact_)filename of this BDS. */
9a4f4c31 4088 if (bs->file->bs->full_open_options) {
91af7014
HR
4089 qdict_put_obj(opts, "driver",
4090 QOBJECT(qstring_from_str(drv->format_name)));
9a4f4c31
KW
4091 QINCREF(bs->file->bs->full_open_options);
4092 qdict_put_obj(opts, "file",
4093 QOBJECT(bs->file->bs->full_open_options));
91af7014
HR
4094
4095 bs->full_open_options = opts;
4096 } else {
4097 QDECREF(opts);
4098 }
4099 } else if (!bs->full_open_options && qdict_size(bs->options)) {
4100 /* There is no underlying file BDS (at least referenced by BDS.file),
4101 * so the full options QDict should be equal to the options given
4102 * specifically for this block device when it was opened (plus the
4103 * driver specification).
4104 * Because those options don't change, there is no need to update
4105 * full_open_options when it's already set. */
4106
4107 opts = qdict_new();
4108 append_open_options(opts, bs);
4109 qdict_put_obj(opts, "driver",
4110 QOBJECT(qstring_from_str(drv->format_name)));
4111
4112 if (bs->exact_filename[0]) {
4113 /* This may not work for all block protocol drivers (some may
4114 * require this filename to be parsed), but we have to find some
4115 * default solution here, so just include it. If some block driver
4116 * does not support pure options without any filename at all or
4117 * needs some special format of the options QDict, it needs to
4118 * implement the driver-specific bdrv_refresh_filename() function.
4119 */
4120 qdict_put_obj(opts, "filename",
4121 QOBJECT(qstring_from_str(bs->exact_filename)));
4122 }
4123
4124 bs->full_open_options = opts;
4125 }
4126
4127 if (bs->exact_filename[0]) {
4128 pstrcpy(bs->filename, sizeof(bs->filename), bs->exact_filename);
4129 } else if (bs->full_open_options) {
4130 QString *json = qobject_to_json(QOBJECT(bs->full_open_options));
4131 snprintf(bs->filename, sizeof(bs->filename), "json:%s",
4132 qstring_get_str(json));
4133 QDECREF(json);
4134 }
4135}