blob: 823ff1d7abdd73ec97a76fb7795be5b0a7fa53cf [file] [log] [blame]
bellardfc01f7e2003-06-30 10:03:06 +00001/*
2 * QEMU System Emulator block driver
ths5fafdf22007-09-16 21:08:06 +00003 *
bellardfc01f7e2003-06-30 10:03:06 +00004 * Copyright (c) 2003 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellardfc01f7e2003-06-30 10:03:06 +00006 * 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 */
Peter Maydelld38ea872016-01-29 17:50:05 +000024#include "qemu/osdep.h"
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +010025#include "trace.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010026#include "block/block_int.h"
27#include "block/blockjob.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010028#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010029#include "qemu/module.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010030#include "qapi/qmp/qerror.h"
Kevin Wolf91a097e2015-05-08 17:49:53 +020031#include "qapi/qmp/qbool.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010032#include "qapi/qmp/qjson.h"
Markus Armbrusterbfb197e2014-10-07 13:59:11 +020033#include "sysemu/block-backend.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010034#include "sysemu/sysemu.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010035#include "qemu/notify.h"
Daniel P. Berrange10817bf2015-09-01 14:48:02 +010036#include "qemu/coroutine.h"
Benoît Canetc13163f2014-01-23 21:31:34 +010037#include "block/qapi.h"
Luiz Capitulinob2023812011-09-21 17:16:47 -030038#include "qmp-commands.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010039#include "qemu/timer.h"
Wenchao Xiaa5ee7bd2014-06-18 08:43:44 +020040#include "qapi-event.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020041#include "qemu/cutils.h"
42#include "qemu/id.h"
bellardfc01f7e2003-06-30 10:03:06 +000043
Juan Quintela71e72a12009-07-27 16:12:56 +020044#ifdef CONFIG_BSD
bellard7674e7b2005-04-26 21:59:26 +000045#include <sys/ioctl.h>
Blue Swirl72cf2d42009-09-12 07:36:22 +000046#include <sys/queue.h>
blueswir1c5e97232009-03-07 20:06:23 +000047#ifndef __DragonFly__
bellard7674e7b2005-04-26 21:59:26 +000048#include <sys/disk.h>
49#endif
blueswir1c5e97232009-03-07 20:06:23 +000050#endif
bellard7674e7b2005-04-26 21:59:26 +000051
aliguori49dc7682009-03-08 16:26:59 +000052#ifdef _WIN32
53#include <windows.h>
54#endif
55
Stefan Hajnoczi1c9805a2011-10-13 13:08:22 +010056#define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
57
Benoît Canetdc364f42014-01-23 21:31:32 +010058static QTAILQ_HEAD(, BlockDriverState) graph_bdrv_states =
59 QTAILQ_HEAD_INITIALIZER(graph_bdrv_states);
60
Max Reitz2c1d04e2016-01-29 16:36:11 +010061static QTAILQ_HEAD(, BlockDriverState) all_bdrv_states =
62 QTAILQ_HEAD_INITIALIZER(all_bdrv_states);
63
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +010064static QLIST_HEAD(, BlockDriver) bdrv_drivers =
65 QLIST_HEAD_INITIALIZER(bdrv_drivers);
bellardea2384d2004-08-01 21:59:26 +000066
Max Reitz5b363932016-05-17 16:41:31 +020067static BlockDriverState *bdrv_open_inherit(const char *filename,
68 const char *reference,
69 QDict *options, int flags,
70 BlockDriverState *parent,
71 const BdrvChildRole *child_role,
72 Error **errp);
Kevin Wolff3930ed2015-04-08 13:43:47 +020073
Markus Armbrustereb852012009-10-27 18:41:44 +010074/* If non-zero, use only whitelisted block drivers */
75static int use_bdrv_whitelist;
76
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +000077#ifdef _WIN32
78static int is_windows_drive_prefix(const char *filename)
79{
80 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
81 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
82 filename[1] == ':');
83}
84
85int is_windows_drive(const char *filename)
86{
87 if (is_windows_drive_prefix(filename) &&
88 filename[2] == '\0')
89 return 1;
90 if (strstart(filename, "\\\\.\\", NULL) ||
91 strstart(filename, "//./", NULL))
92 return 1;
93 return 0;
94}
95#endif
96
Kevin Wolf339064d2013-11-28 10:23:32 +010097size_t bdrv_opt_mem_align(BlockDriverState *bs)
98{
99 if (!bs || !bs->drv) {
Denis V. Lunev459b4e62015-05-12 17:30:56 +0300100 /* page size or 4k (hdd sector size) should be on the safe side */
101 return MAX(4096, getpagesize());
Kevin Wolf339064d2013-11-28 10:23:32 +0100102 }
103
104 return bs->bl.opt_mem_alignment;
105}
106
Denis V. Lunev4196d2f2015-05-12 17:30:55 +0300107size_t bdrv_min_mem_align(BlockDriverState *bs)
108{
109 if (!bs || !bs->drv) {
Denis V. Lunev459b4e62015-05-12 17:30:56 +0300110 /* page size or 4k (hdd sector size) should be on the safe side */
111 return MAX(4096, getpagesize());
Denis V. Lunev4196d2f2015-05-12 17:30:55 +0300112 }
113
114 return bs->bl.min_mem_alignment;
115}
116
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000117/* check if the path starts with "<protocol>:" */
Max Reitz5c984152014-12-03 14:57:22 +0100118int path_has_protocol(const char *path)
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000119{
Paolo Bonzini947995c2012-05-08 16:51:48 +0200120 const char *p;
121
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000122#ifdef _WIN32
123 if (is_windows_drive(path) ||
124 is_windows_drive_prefix(path)) {
125 return 0;
126 }
Paolo Bonzini947995c2012-05-08 16:51:48 +0200127 p = path + strcspn(path, ":/\\");
128#else
129 p = path + strcspn(path, ":/");
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000130#endif
131
Paolo Bonzini947995c2012-05-08 16:51:48 +0200132 return *p == ':';
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000133}
134
bellard83f64092006-08-01 16:21:11 +0000135int path_is_absolute(const char *path)
136{
bellard21664422007-01-07 18:22:37 +0000137#ifdef _WIN32
138 /* specific case for names like: "\\.\d:" */
Paolo Bonzinif53f4da2012-05-08 16:51:47 +0200139 if (is_windows_drive(path) || is_windows_drive_prefix(path)) {
bellard21664422007-01-07 18:22:37 +0000140 return 1;
Paolo Bonzinif53f4da2012-05-08 16:51:47 +0200141 }
142 return (*path == '/' || *path == '\\');
bellard3b9f94e2007-01-07 17:27:07 +0000143#else
Paolo Bonzinif53f4da2012-05-08 16:51:47 +0200144 return (*path == '/');
bellard3b9f94e2007-01-07 17:27:07 +0000145#endif
bellard83f64092006-08-01 16:21:11 +0000146}
147
148/* if filename is absolute, just copy it to dest. Otherwise, build a
149 path to it by considering it is relative to base_path. URL are
150 supported. */
151void path_combine(char *dest, int dest_size,
152 const char *base_path,
153 const char *filename)
154{
155 const char *p, *p1;
156 int len;
157
158 if (dest_size <= 0)
159 return;
160 if (path_is_absolute(filename)) {
161 pstrcpy(dest, dest_size, filename);
162 } else {
163 p = strchr(base_path, ':');
164 if (p)
165 p++;
166 else
167 p = base_path;
bellard3b9f94e2007-01-07 17:27:07 +0000168 p1 = strrchr(base_path, '/');
169#ifdef _WIN32
170 {
171 const char *p2;
172 p2 = strrchr(base_path, '\\');
173 if (!p1 || p2 > p1)
174 p1 = p2;
175 }
176#endif
bellard83f64092006-08-01 16:21:11 +0000177 if (p1)
178 p1++;
179 else
180 p1 = base_path;
181 if (p1 > p)
182 p = p1;
183 len = p - base_path;
184 if (len > dest_size - 1)
185 len = dest_size - 1;
186 memcpy(dest, base_path, len);
187 dest[len] = '\0';
188 pstrcat(dest, dest_size, filename);
189 }
190}
191
Max Reitz0a828552014-11-26 17:20:25 +0100192void bdrv_get_full_backing_filename_from_filename(const char *backed,
193 const char *backing,
Max Reitz9f074292014-11-26 17:20:26 +0100194 char *dest, size_t sz,
195 Error **errp)
Max Reitz0a828552014-11-26 17:20:25 +0100196{
Max Reitz9f074292014-11-26 17:20:26 +0100197 if (backing[0] == '\0' || path_has_protocol(backing) ||
198 path_is_absolute(backing))
199 {
Max Reitz0a828552014-11-26 17:20:25 +0100200 pstrcpy(dest, sz, backing);
Max Reitz9f074292014-11-26 17:20:26 +0100201 } else if (backed[0] == '\0' || strstart(backed, "json:", NULL)) {
202 error_setg(errp, "Cannot use relative backing file names for '%s'",
203 backed);
Max Reitz0a828552014-11-26 17:20:25 +0100204 } else {
205 path_combine(dest, sz, backed, backing);
206 }
207}
208
Max Reitz9f074292014-11-26 17:20:26 +0100209void bdrv_get_full_backing_filename(BlockDriverState *bs, char *dest, size_t sz,
210 Error **errp)
Paolo Bonzinidc5a1372012-05-08 16:51:50 +0200211{
Max Reitz9f074292014-11-26 17:20:26 +0100212 char *backed = bs->exact_filename[0] ? bs->exact_filename : bs->filename;
213
214 bdrv_get_full_backing_filename_from_filename(backed, bs->backing_file,
215 dest, sz, errp);
Paolo Bonzinidc5a1372012-05-08 16:51:50 +0200216}
217
Stefan Hajnoczi0eb72172015-04-28 14:27:51 +0100218void bdrv_register(BlockDriver *bdrv)
219{
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100220 QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
bellardea2384d2004-08-01 21:59:26 +0000221}
bellardb3380822004-03-14 21:38:54 +0000222
Markus Armbrustere4e99862014-10-07 13:59:03 +0200223BlockDriverState *bdrv_new(void)
224{
225 BlockDriverState *bs;
226 int i;
227
Markus Armbruster5839e532014-08-19 10:31:08 +0200228 bs = g_new0(BlockDriverState, 1);
Fam Zhenge4654d22013-11-13 18:29:43 +0800229 QLIST_INIT(&bs->dirty_bitmaps);
Fam Zhengfbe40ff2014-05-23 21:29:42 +0800230 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
231 QLIST_INIT(&bs->op_blockers[i]);
232 }
Stefan Hajnoczid616b222013-06-24 17:13:10 +0200233 notifier_with_return_list_init(&bs->before_write_notifiers);
Fam Zheng9fcb0252013-08-23 09:14:46 +0800234 bs->refcnt = 1;
Stefan Hajnoczidcd04222014-05-08 16:34:37 +0200235 bs->aio_context = qemu_get_aio_context();
Paolo Bonzinid7d512f2012-08-23 11:20:36 +0200236
Max Reitz2c1d04e2016-01-29 16:36:11 +0100237 QTAILQ_INSERT_TAIL(&all_bdrv_states, bs, bs_list);
238
bellardb3380822004-03-14 21:38:54 +0000239 return bs;
240}
241
bellardea2384d2004-08-01 21:59:26 +0000242BlockDriver *bdrv_find_format(const char *format_name)
243{
244 BlockDriver *drv1;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100245 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
246 if (!strcmp(drv1->format_name, format_name)) {
bellardea2384d2004-08-01 21:59:26 +0000247 return drv1;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100248 }
bellardea2384d2004-08-01 21:59:26 +0000249 }
250 return NULL;
251}
252
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800253static int bdrv_is_whitelisted(BlockDriver *drv, bool read_only)
Markus Armbrustereb852012009-10-27 18:41:44 +0100254{
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800255 static const char *whitelist_rw[] = {
256 CONFIG_BDRV_RW_WHITELIST
257 };
258 static const char *whitelist_ro[] = {
259 CONFIG_BDRV_RO_WHITELIST
Markus Armbrustereb852012009-10-27 18:41:44 +0100260 };
261 const char **p;
262
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800263 if (!whitelist_rw[0] && !whitelist_ro[0]) {
Markus Armbrustereb852012009-10-27 18:41:44 +0100264 return 1; /* no whitelist, anything goes */
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800265 }
Markus Armbrustereb852012009-10-27 18:41:44 +0100266
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800267 for (p = whitelist_rw; *p; p++) {
Markus Armbrustereb852012009-10-27 18:41:44 +0100268 if (!strcmp(drv->format_name, *p)) {
269 return 1;
270 }
271 }
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800272 if (read_only) {
273 for (p = whitelist_ro; *p; p++) {
274 if (!strcmp(drv->format_name, *p)) {
275 return 1;
276 }
277 }
278 }
Markus Armbrustereb852012009-10-27 18:41:44 +0100279 return 0;
280}
281
Daniel P. Berrangee6ff69b2016-03-21 14:11:48 +0000282bool bdrv_uses_whitelist(void)
283{
284 return use_bdrv_whitelist;
285}
286
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800287typedef struct CreateCo {
288 BlockDriver *drv;
289 char *filename;
Chunyan Liu83d05212014-06-05 17:20:51 +0800290 QemuOpts *opts;
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800291 int ret;
Max Reitzcc84d902013-09-06 17:14:26 +0200292 Error *err;
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800293} CreateCo;
294
295static void coroutine_fn bdrv_create_co_entry(void *opaque)
296{
Max Reitzcc84d902013-09-06 17:14:26 +0200297 Error *local_err = NULL;
298 int ret;
299
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800300 CreateCo *cco = opaque;
301 assert(cco->drv);
302
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800303 ret = cco->drv->bdrv_create(cco->filename, cco->opts, &local_err);
Eduardo Habkost621ff942016-06-13 18:57:56 -0300304 error_propagate(&cco->err, local_err);
Max Reitzcc84d902013-09-06 17:14:26 +0200305 cco->ret = ret;
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800306}
307
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200308int bdrv_create(BlockDriver *drv, const char* filename,
Chunyan Liu83d05212014-06-05 17:20:51 +0800309 QemuOpts *opts, Error **errp)
bellardea2384d2004-08-01 21:59:26 +0000310{
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800311 int ret;
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200312
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800313 Coroutine *co;
314 CreateCo cco = {
315 .drv = drv,
316 .filename = g_strdup(filename),
Chunyan Liu83d05212014-06-05 17:20:51 +0800317 .opts = opts,
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800318 .ret = NOT_DONE,
Max Reitzcc84d902013-09-06 17:14:26 +0200319 .err = NULL,
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800320 };
321
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800322 if (!drv->bdrv_create) {
Max Reitzcc84d902013-09-06 17:14:26 +0200323 error_setg(errp, "Driver '%s' does not support image creation", drv->format_name);
Luiz Capitulino80168bf2012-10-17 16:45:25 -0300324 ret = -ENOTSUP;
325 goto out;
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800326 }
327
328 if (qemu_in_coroutine()) {
329 /* Fast-path if already in coroutine context */
330 bdrv_create_co_entry(&cco);
331 } else {
332 co = qemu_coroutine_create(bdrv_create_co_entry);
333 qemu_coroutine_enter(co, &cco);
334 while (cco.ret == NOT_DONE) {
Paolo Bonzinib47ec2c2014-07-07 15:18:01 +0200335 aio_poll(qemu_get_aio_context(), true);
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800336 }
337 }
338
339 ret = cco.ret;
Max Reitzcc84d902013-09-06 17:14:26 +0200340 if (ret < 0) {
Markus Armbruster84d18f02014-01-30 15:07:28 +0100341 if (cco.err) {
Max Reitzcc84d902013-09-06 17:14:26 +0200342 error_propagate(errp, cco.err);
343 } else {
344 error_setg_errno(errp, -ret, "Could not create image");
345 }
346 }
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800347
Luiz Capitulino80168bf2012-10-17 16:45:25 -0300348out:
349 g_free(cco.filename);
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800350 return ret;
bellardea2384d2004-08-01 21:59:26 +0000351}
352
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800353int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp)
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200354{
355 BlockDriver *drv;
Max Reitzcc84d902013-09-06 17:14:26 +0200356 Error *local_err = NULL;
357 int ret;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200358
Max Reitzb65a5e12015-02-05 13:58:12 -0500359 drv = bdrv_find_protocol(filename, true, errp);
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200360 if (drv == NULL) {
Stefan Hajnoczi16905d72010-11-30 15:14:14 +0000361 return -ENOENT;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200362 }
363
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800364 ret = bdrv_create(drv, filename, opts, &local_err);
Eduardo Habkost621ff942016-06-13 18:57:56 -0300365 error_propagate(errp, local_err);
Max Reitzcc84d902013-09-06 17:14:26 +0200366 return ret;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200367}
368
Ekaterina Tumanova892b7de2015-02-16 12:47:54 +0100369/**
370 * Try to get @bs's logical and physical block size.
371 * On success, store them in @bsz struct and return 0.
372 * On failure return -errno.
373 * @bs must not be empty.
374 */
375int bdrv_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz)
376{
377 BlockDriver *drv = bs->drv;
378
379 if (drv && drv->bdrv_probe_blocksizes) {
380 return drv->bdrv_probe_blocksizes(bs, bsz);
381 }
382
383 return -ENOTSUP;
384}
385
386/**
387 * Try to get @bs's geometry (cyls, heads, sectors).
388 * On success, store them in @geo struct and return 0.
389 * On failure return -errno.
390 * @bs must not be empty.
391 */
392int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo)
393{
394 BlockDriver *drv = bs->drv;
395
396 if (drv && drv->bdrv_probe_geometry) {
397 return drv->bdrv_probe_geometry(bs, geo);
398 }
399
400 return -ENOTSUP;
401}
402
Jim Meyeringeba25052012-05-28 09:27:54 +0200403/*
404 * Create a uniquely-named empty temporary file.
405 * Return 0 upon success, otherwise a negative errno value.
406 */
407int get_tmp_filename(char *filename, int size)
408{
bellardd5249392004-08-03 21:14:23 +0000409#ifdef _WIN32
bellard3b9f94e2007-01-07 17:27:07 +0000410 char temp_dir[MAX_PATH];
Jim Meyeringeba25052012-05-28 09:27:54 +0200411 /* GetTempFileName requires that its output buffer (4th param)
412 have length MAX_PATH or greater. */
413 assert(size >= MAX_PATH);
414 return (GetTempPath(MAX_PATH, temp_dir)
415 && GetTempFileName(temp_dir, "qem", 0, filename)
416 ? 0 : -GetLastError());
bellardd5249392004-08-03 21:14:23 +0000417#else
bellardea2384d2004-08-01 21:59:26 +0000418 int fd;
blueswir17ccfb2e2008-09-14 06:45:34 +0000419 const char *tmpdir;
aurel320badc1e2008-03-10 00:05:34 +0000420 tmpdir = getenv("TMPDIR");
Amit Shah69bef792014-02-26 15:12:37 +0530421 if (!tmpdir) {
422 tmpdir = "/var/tmp";
423 }
Jim Meyeringeba25052012-05-28 09:27:54 +0200424 if (snprintf(filename, size, "%s/vl.XXXXXX", tmpdir) >= size) {
425 return -EOVERFLOW;
426 }
bellardea2384d2004-08-01 21:59:26 +0000427 fd = mkstemp(filename);
Dunrong Huangfe235a02012-09-05 21:26:22 +0800428 if (fd < 0) {
429 return -errno;
430 }
431 if (close(fd) != 0) {
432 unlink(filename);
Jim Meyeringeba25052012-05-28 09:27:54 +0200433 return -errno;
434 }
435 return 0;
bellardd5249392004-08-03 21:14:23 +0000436#endif
Jim Meyeringeba25052012-05-28 09:27:54 +0200437}
bellardea2384d2004-08-01 21:59:26 +0000438
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200439/*
440 * Detect host devices. By convention, /dev/cdrom[N] is always
441 * recognized as a host CDROM.
442 */
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200443static BlockDriver *find_hdev_driver(const char *filename)
444{
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200445 int score_max = 0, score;
446 BlockDriver *drv = NULL, *d;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200447
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100448 QLIST_FOREACH(d, &bdrv_drivers, list) {
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200449 if (d->bdrv_probe_device) {
450 score = d->bdrv_probe_device(filename);
451 if (score > score_max) {
452 score_max = score;
453 drv = d;
454 }
455 }
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200456 }
457
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200458 return drv;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200459}
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200460
Kevin Wolf98289622013-07-10 15:47:39 +0200461BlockDriver *bdrv_find_protocol(const char *filename,
Max Reitzb65a5e12015-02-05 13:58:12 -0500462 bool allow_protocol_prefix,
463 Error **errp)
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200464{
465 BlockDriver *drv1;
466 char protocol[128];
467 int len;
468 const char *p;
469
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200470 /* TODO Drivers without bdrv_file_open must be specified explicitly */
471
Christoph Hellwig39508e72010-06-23 12:25:17 +0200472 /*
473 * XXX(hch): we really should not let host device detection
474 * override an explicit protocol specification, but moving this
475 * later breaks access to device names with colons in them.
476 * Thanks to the brain-dead persistent naming schemes on udev-
477 * based Linux systems those actually are quite common.
478 */
479 drv1 = find_hdev_driver(filename);
480 if (drv1) {
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200481 return drv1;
482 }
Christoph Hellwig39508e72010-06-23 12:25:17 +0200483
Kevin Wolf98289622013-07-10 15:47:39 +0200484 if (!path_has_protocol(filename) || !allow_protocol_prefix) {
Max Reitzef810432014-12-02 18:32:42 +0100485 return &bdrv_file;
Christoph Hellwig39508e72010-06-23 12:25:17 +0200486 }
Kevin Wolf98289622013-07-10 15:47:39 +0200487
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000488 p = strchr(filename, ':');
489 assert(p != NULL);
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200490 len = p - filename;
491 if (len > sizeof(protocol) - 1)
492 len = sizeof(protocol) - 1;
493 memcpy(protocol, filename, len);
494 protocol[len] = '\0';
495 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
496 if (drv1->protocol_name &&
497 !strcmp(drv1->protocol_name, protocol)) {
498 return drv1;
499 }
500 }
Max Reitzb65a5e12015-02-05 13:58:12 -0500501
502 error_setg(errp, "Unknown protocol '%s'", protocol);
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200503 return NULL;
504}
505
Markus Armbrusterc6684242014-11-20 16:27:10 +0100506/*
507 * Guess image format by probing its contents.
508 * This is not a good idea when your image is raw (CVE-2008-2004), but
509 * we do it anyway for backward compatibility.
510 *
511 * @buf contains the image's first @buf_size bytes.
Kevin Wolf7cddd372014-11-20 16:27:11 +0100512 * @buf_size is the buffer size in bytes (generally BLOCK_PROBE_BUF_SIZE,
513 * but can be smaller if the image file is smaller)
Markus Armbrusterc6684242014-11-20 16:27:10 +0100514 * @filename is its filename.
515 *
516 * For all block drivers, call the bdrv_probe() method to get its
517 * probing score.
518 * Return the first block driver with the highest probing score.
519 */
Kevin Wolf38f3ef52014-11-20 16:27:12 +0100520BlockDriver *bdrv_probe_all(const uint8_t *buf, int buf_size,
521 const char *filename)
Markus Armbrusterc6684242014-11-20 16:27:10 +0100522{
523 int score_max = 0, score;
524 BlockDriver *drv = NULL, *d;
525
526 QLIST_FOREACH(d, &bdrv_drivers, list) {
527 if (d->bdrv_probe) {
528 score = d->bdrv_probe(buf, buf_size, filename);
529 if (score > score_max) {
530 score_max = score;
531 drv = d;
532 }
533 }
534 }
535
536 return drv;
537}
538
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200539static int find_image_format(BdrvChild *file, const char *filename,
Max Reitz34b5d2c2013-09-05 14:45:29 +0200540 BlockDriver **pdrv, Error **errp)
bellardea2384d2004-08-01 21:59:26 +0000541{
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200542 BlockDriverState *bs = file->bs;
Markus Armbrusterc6684242014-11-20 16:27:10 +0100543 BlockDriver *drv;
Kevin Wolf7cddd372014-11-20 16:27:11 +0100544 uint8_t buf[BLOCK_PROBE_BUF_SIZE];
Kevin Wolff500a6d2012-11-12 17:35:27 +0100545 int ret = 0;
Nicholas Bellingerf8ea0b02010-05-17 09:45:57 -0700546
Kevin Wolf08a00552010-06-01 18:37:31 +0200547 /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
Dimitris Aragiorgisb192af82015-06-23 13:44:56 +0300548 if (bdrv_is_sg(bs) || !bdrv_is_inserted(bs) || bdrv_getlength(bs) == 0) {
Max Reitzef810432014-12-02 18:32:42 +0100549 *pdrv = &bdrv_raw;
Stefan Weilc98ac352010-07-21 21:51:51 +0200550 return ret;
Nicholas A. Bellinger1a396852010-05-27 08:56:28 -0700551 }
Nicholas Bellingerf8ea0b02010-05-17 09:45:57 -0700552
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200553 ret = bdrv_pread(file, 0, buf, sizeof(buf));
bellard83f64092006-08-01 16:21:11 +0000554 if (ret < 0) {
Max Reitz34b5d2c2013-09-05 14:45:29 +0200555 error_setg_errno(errp, -ret, "Could not read image for determining its "
556 "format");
Stefan Weilc98ac352010-07-21 21:51:51 +0200557 *pdrv = NULL;
558 return ret;
bellard83f64092006-08-01 16:21:11 +0000559 }
560
Markus Armbrusterc6684242014-11-20 16:27:10 +0100561 drv = bdrv_probe_all(buf, ret, filename);
Stefan Weilc98ac352010-07-21 21:51:51 +0200562 if (!drv) {
Max Reitz34b5d2c2013-09-05 14:45:29 +0200563 error_setg(errp, "Could not determine image format: No compatible "
564 "driver found");
Stefan Weilc98ac352010-07-21 21:51:51 +0200565 ret = -ENOENT;
566 }
567 *pdrv = drv;
568 return ret;
bellardea2384d2004-08-01 21:59:26 +0000569}
570
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100571/**
572 * Set the current 'total_sectors' value
Markus Armbruster65a9bb22014-06-26 13:23:17 +0200573 * Return 0 on success, -errno on error.
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100574 */
575static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
576{
577 BlockDriver *drv = bs->drv;
578
Nicholas Bellinger396759a2010-05-17 09:46:04 -0700579 /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
Dimitris Aragiorgisb192af82015-06-23 13:44:56 +0300580 if (bdrv_is_sg(bs))
Nicholas Bellinger396759a2010-05-17 09:46:04 -0700581 return 0;
582
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100583 /* query actual device if possible, otherwise just trust the hint */
584 if (drv->bdrv_getlength) {
585 int64_t length = drv->bdrv_getlength(bs);
586 if (length < 0) {
587 return length;
588 }
Fam Zheng7e382002013-11-06 19:48:06 +0800589 hint = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE);
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100590 }
591
592 bs->total_sectors = hint;
593 return 0;
594}
595
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100596/**
Kevin Wolfcddff5b2015-11-16 16:43:27 +0100597 * Combines a QDict of new block driver @options with any missing options taken
598 * from @old_options, so that leaving out an option defaults to its old value.
599 */
600static void bdrv_join_options(BlockDriverState *bs, QDict *options,
601 QDict *old_options)
602{
603 if (bs->drv && bs->drv->bdrv_join_options) {
604 bs->drv->bdrv_join_options(options, old_options);
605 } else {
606 qdict_join(options, old_options, false);
607 }
608}
609
610/**
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100611 * Set open flags for a given discard mode
612 *
613 * Return 0 on success, -1 if the discard mode was invalid.
614 */
615int bdrv_parse_discard_flags(const char *mode, int *flags)
616{
617 *flags &= ~BDRV_O_UNMAP;
618
619 if (!strcmp(mode, "off") || !strcmp(mode, "ignore")) {
620 /* do nothing */
621 } else if (!strcmp(mode, "on") || !strcmp(mode, "unmap")) {
622 *flags |= BDRV_O_UNMAP;
623 } else {
624 return -1;
625 }
626
627 return 0;
628}
629
630/**
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100631 * Set open flags for a given cache mode
632 *
633 * Return 0 on success, -1 if the cache mode was invalid.
634 */
Kevin Wolf53e8ae02016-03-18 15:36:58 +0100635int bdrv_parse_cache_mode(const char *mode, int *flags, bool *writethrough)
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100636{
637 *flags &= ~BDRV_O_CACHE_MASK;
638
639 if (!strcmp(mode, "off") || !strcmp(mode, "none")) {
Kevin Wolf53e8ae02016-03-18 15:36:58 +0100640 *writethrough = false;
641 *flags |= BDRV_O_NOCACHE;
Stefan Hajnoczi92196b22011-08-04 12:26:52 +0100642 } else if (!strcmp(mode, "directsync")) {
Kevin Wolf53e8ae02016-03-18 15:36:58 +0100643 *writethrough = true;
Stefan Hajnoczi92196b22011-08-04 12:26:52 +0100644 *flags |= BDRV_O_NOCACHE;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100645 } else if (!strcmp(mode, "writeback")) {
Kevin Wolf53e8ae02016-03-18 15:36:58 +0100646 *writethrough = false;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100647 } else if (!strcmp(mode, "unsafe")) {
Kevin Wolf53e8ae02016-03-18 15:36:58 +0100648 *writethrough = false;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100649 *flags |= BDRV_O_NO_FLUSH;
650 } else if (!strcmp(mode, "writethrough")) {
Kevin Wolf53e8ae02016-03-18 15:36:58 +0100651 *writethrough = true;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100652 } else {
653 return -1;
654 }
655
656 return 0;
657}
658
Kevin Wolf20018e12016-05-23 18:46:59 +0200659static void bdrv_child_cb_drained_begin(BdrvChild *child)
660{
661 BlockDriverState *bs = child->opaque;
662 bdrv_drained_begin(bs);
663}
664
665static void bdrv_child_cb_drained_end(BdrvChild *child)
666{
667 BlockDriverState *bs = child->opaque;
668 bdrv_drained_end(bs);
669}
670
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200671/*
Kevin Wolf73176be2016-03-07 13:02:15 +0100672 * Returns the options and flags that a temporary snapshot should get, based on
673 * the originally requested flags (the originally requested image will have
674 * flags like a backing file)
Kevin Wolfb1e6fc02014-05-06 12:11:42 +0200675 */
Kevin Wolf73176be2016-03-07 13:02:15 +0100676static void bdrv_temp_snapshot_options(int *child_flags, QDict *child_options,
677 int parent_flags, QDict *parent_options)
Kevin Wolfb1e6fc02014-05-06 12:11:42 +0200678{
Kevin Wolf73176be2016-03-07 13:02:15 +0100679 *child_flags = (parent_flags & ~BDRV_O_SNAPSHOT) | BDRV_O_TEMPORARY;
680
681 /* For temporary files, unconditional cache=unsafe is fine */
Kevin Wolf73176be2016-03-07 13:02:15 +0100682 qdict_set_default_str(child_options, BDRV_OPT_CACHE_DIRECT, "off");
683 qdict_set_default_str(child_options, BDRV_OPT_CACHE_NO_FLUSH, "on");
Kevin Wolf41869042016-06-16 12:59:30 +0200684
685 /* aio=native doesn't work for cache.direct=off, so disable it for the
686 * temporary snapshot */
687 *child_flags &= ~BDRV_O_NATIVE_AIO;
Kevin Wolfb1e6fc02014-05-06 12:11:42 +0200688}
689
690/*
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200691 * Returns the options and flags that bs->file should get if a protocol driver
692 * is expected, based on the given options and flags for the parent BDS
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200693 */
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200694static void bdrv_inherited_options(int *child_flags, QDict *child_options,
695 int parent_flags, QDict *parent_options)
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200696{
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200697 int flags = parent_flags;
698
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200699 /* Enable protocol handling, disable format probing for bs->file */
700 flags |= BDRV_O_PROTOCOL;
701
Kevin Wolf91a097e2015-05-08 17:49:53 +0200702 /* If the cache mode isn't explicitly set, inherit direct and no-flush from
703 * the parent. */
704 qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_DIRECT);
705 qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_NO_FLUSH);
706
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200707 /* Our block drivers take care to send flushes and respect unmap policy,
Kevin Wolf91a097e2015-05-08 17:49:53 +0200708 * so we can default to enable both on lower layers regardless of the
709 * corresponding parent options. */
Kevin Wolf91a097e2015-05-08 17:49:53 +0200710 flags |= BDRV_O_UNMAP;
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200711
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200712 /* Clear flags that only apply to the top layer */
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000713 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ |
714 BDRV_O_NO_IO);
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200715
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200716 *child_flags = flags;
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200717}
718
Kevin Wolff3930ed2015-04-08 13:43:47 +0200719const BdrvChildRole child_file = {
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200720 .inherit_options = bdrv_inherited_options,
Kevin Wolf20018e12016-05-23 18:46:59 +0200721 .drained_begin = bdrv_child_cb_drained_begin,
722 .drained_end = bdrv_child_cb_drained_end,
Kevin Wolff3930ed2015-04-08 13:43:47 +0200723};
724
725/*
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200726 * Returns the options and flags that bs->file should get if the use of formats
727 * (and not only protocols) is permitted for it, based on the given options and
728 * flags for the parent BDS
Kevin Wolff3930ed2015-04-08 13:43:47 +0200729 */
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200730static void bdrv_inherited_fmt_options(int *child_flags, QDict *child_options,
731 int parent_flags, QDict *parent_options)
Kevin Wolff3930ed2015-04-08 13:43:47 +0200732{
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200733 child_file.inherit_options(child_flags, child_options,
734 parent_flags, parent_options);
735
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000736 *child_flags &= ~(BDRV_O_PROTOCOL | BDRV_O_NO_IO);
Kevin Wolff3930ed2015-04-08 13:43:47 +0200737}
738
739const BdrvChildRole child_format = {
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200740 .inherit_options = bdrv_inherited_fmt_options,
Kevin Wolf20018e12016-05-23 18:46:59 +0200741 .drained_begin = bdrv_child_cb_drained_begin,
742 .drained_end = bdrv_child_cb_drained_end,
Kevin Wolff3930ed2015-04-08 13:43:47 +0200743};
744
Kevin Wolf317fc442014-04-25 13:27:34 +0200745/*
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200746 * Returns the options and flags that bs->backing should get, based on the
747 * given options and flags for the parent BDS
Kevin Wolf317fc442014-04-25 13:27:34 +0200748 */
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200749static void bdrv_backing_options(int *child_flags, QDict *child_options,
750 int parent_flags, QDict *parent_options)
Kevin Wolf317fc442014-04-25 13:27:34 +0200751{
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200752 int flags = parent_flags;
753
Kevin Wolfb8816a42016-03-04 14:52:32 +0100754 /* The cache mode is inherited unmodified for backing files; except WCE,
755 * which is only applied on the top level (BlockBackend) */
Kevin Wolf91a097e2015-05-08 17:49:53 +0200756 qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_DIRECT);
757 qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_NO_FLUSH);
758
Kevin Wolf317fc442014-04-25 13:27:34 +0200759 /* backing files always opened read-only */
760 flags &= ~(BDRV_O_RDWR | BDRV_O_COPY_ON_READ);
761
762 /* snapshot=on is handled on the top layer */
Kevin Wolf8bfea152014-04-11 19:16:36 +0200763 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_TEMPORARY);
Kevin Wolf317fc442014-04-25 13:27:34 +0200764
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200765 *child_flags = flags;
Kevin Wolf317fc442014-04-25 13:27:34 +0200766}
767
Kevin Wolff3930ed2015-04-08 13:43:47 +0200768static const BdrvChildRole child_backing = {
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200769 .inherit_options = bdrv_backing_options,
Kevin Wolf20018e12016-05-23 18:46:59 +0200770 .drained_begin = bdrv_child_cb_drained_begin,
771 .drained_end = bdrv_child_cb_drained_end,
Kevin Wolff3930ed2015-04-08 13:43:47 +0200772};
773
Kevin Wolf7b272452012-11-12 17:05:39 +0100774static int bdrv_open_flags(BlockDriverState *bs, int flags)
775{
Kevin Wolf61de4c62016-03-18 17:46:45 +0100776 int open_flags = flags;
Kevin Wolf7b272452012-11-12 17:05:39 +0100777
778 /*
779 * Clear flags that are internal to the block layer before opening the
780 * image.
781 */
Kevin Wolf20cca272014-06-04 14:33:27 +0200782 open_flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_PROTOCOL);
Kevin Wolf7b272452012-11-12 17:05:39 +0100783
784 /*
785 * Snapshots should be writable.
786 */
Kevin Wolf8bfea152014-04-11 19:16:36 +0200787 if (flags & BDRV_O_TEMPORARY) {
Kevin Wolf7b272452012-11-12 17:05:39 +0100788 open_flags |= BDRV_O_RDWR;
789 }
790
791 return open_flags;
792}
793
Kevin Wolf91a097e2015-05-08 17:49:53 +0200794static void update_flags_from_options(int *flags, QemuOpts *opts)
795{
796 *flags &= ~BDRV_O_CACHE_MASK;
797
Kevin Wolf91a097e2015-05-08 17:49:53 +0200798 assert(qemu_opt_find(opts, BDRV_OPT_CACHE_NO_FLUSH));
799 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, false)) {
800 *flags |= BDRV_O_NO_FLUSH;
801 }
802
803 assert(qemu_opt_find(opts, BDRV_OPT_CACHE_DIRECT));
804 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_DIRECT, false)) {
805 *flags |= BDRV_O_NOCACHE;
806 }
807}
808
809static void update_options_from_flags(QDict *options, int flags)
810{
Kevin Wolf91a097e2015-05-08 17:49:53 +0200811 if (!qdict_haskey(options, BDRV_OPT_CACHE_DIRECT)) {
812 qdict_put(options, BDRV_OPT_CACHE_DIRECT,
813 qbool_from_bool(flags & BDRV_O_NOCACHE));
814 }
815 if (!qdict_haskey(options, BDRV_OPT_CACHE_NO_FLUSH)) {
816 qdict_put(options, BDRV_OPT_CACHE_NO_FLUSH,
817 qbool_from_bool(flags & BDRV_O_NO_FLUSH));
818 }
819}
820
Kevin Wolf636ea372014-01-24 14:11:52 +0100821static void bdrv_assign_node_name(BlockDriverState *bs,
822 const char *node_name,
823 Error **errp)
Benoît Canet6913c0c2014-01-23 21:31:33 +0100824{
Jeff Cody15489c72015-10-12 19:36:50 -0400825 char *gen_node_name = NULL;
Benoît Canet6913c0c2014-01-23 21:31:33 +0100826
Jeff Cody15489c72015-10-12 19:36:50 -0400827 if (!node_name) {
828 node_name = gen_node_name = id_generate(ID_BLOCK);
829 } else if (!id_wellformed(node_name)) {
830 /*
831 * Check for empty string or invalid characters, but not if it is
832 * generated (generated names use characters not available to the user)
833 */
Kevin Wolf9aebf3b2014-09-25 09:54:02 +0200834 error_setg(errp, "Invalid node name");
Kevin Wolf636ea372014-01-24 14:11:52 +0100835 return;
Benoît Canet6913c0c2014-01-23 21:31:33 +0100836 }
837
Benoît Canet0c5e94e2014-02-12 17:15:07 +0100838 /* takes care of avoiding namespaces collisions */
Markus Armbruster7f06d472014-10-07 13:59:12 +0200839 if (blk_by_name(node_name)) {
Benoît Canet0c5e94e2014-02-12 17:15:07 +0100840 error_setg(errp, "node-name=%s is conflicting with a device id",
841 node_name);
Jeff Cody15489c72015-10-12 19:36:50 -0400842 goto out;
Benoît Canet0c5e94e2014-02-12 17:15:07 +0100843 }
844
Benoît Canet6913c0c2014-01-23 21:31:33 +0100845 /* takes care of avoiding duplicates node names */
846 if (bdrv_find_node(node_name)) {
847 error_setg(errp, "Duplicate node name");
Jeff Cody15489c72015-10-12 19:36:50 -0400848 goto out;
Benoît Canet6913c0c2014-01-23 21:31:33 +0100849 }
850
851 /* copy node name into the bs and insert it into the graph list */
852 pstrcpy(bs->node_name, sizeof(bs->node_name), node_name);
853 QTAILQ_INSERT_TAIL(&graph_bdrv_states, bs, node_list);
Jeff Cody15489c72015-10-12 19:36:50 -0400854out:
855 g_free(gen_node_name);
Benoît Canet6913c0c2014-01-23 21:31:33 +0100856}
857
Kevin Wolf18edf282015-04-07 17:12:56 +0200858static QemuOptsList bdrv_runtime_opts = {
859 .name = "bdrv_common",
860 .head = QTAILQ_HEAD_INITIALIZER(bdrv_runtime_opts.head),
861 .desc = {
862 {
863 .name = "node-name",
864 .type = QEMU_OPT_STRING,
865 .help = "Node name of the block device node",
866 },
Kevin Wolf62392eb2015-04-24 16:38:02 +0200867 {
868 .name = "driver",
869 .type = QEMU_OPT_STRING,
870 .help = "Block driver to use for the node",
871 },
Kevin Wolf91a097e2015-05-08 17:49:53 +0200872 {
Kevin Wolf91a097e2015-05-08 17:49:53 +0200873 .name = BDRV_OPT_CACHE_DIRECT,
874 .type = QEMU_OPT_BOOL,
875 .help = "Bypass software writeback cache on the host",
876 },
877 {
878 .name = BDRV_OPT_CACHE_NO_FLUSH,
879 .type = QEMU_OPT_BOOL,
880 .help = "Ignore flush requests",
881 },
Kevin Wolf18edf282015-04-07 17:12:56 +0200882 { /* end of list */ }
883 },
884};
885
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200886/*
Kevin Wolf57915332010-04-14 15:24:50 +0200887 * Common part for opening disk images and files
Kevin Wolfb6ad4912013-03-15 10:35:04 +0100888 *
889 * Removes all processed options from *options.
Kevin Wolf57915332010-04-14 15:24:50 +0200890 */
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200891static int bdrv_open_common(BlockDriverState *bs, BdrvChild *file,
Kevin Wolf82dc8b42016-01-11 19:07:50 +0100892 QDict *options, Error **errp)
Kevin Wolf57915332010-04-14 15:24:50 +0200893{
894 int ret, open_flags;
Kevin Wolf035fccd2013-04-09 14:34:19 +0200895 const char *filename;
Kevin Wolf62392eb2015-04-24 16:38:02 +0200896 const char *driver_name = NULL;
Benoît Canet6913c0c2014-01-23 21:31:33 +0100897 const char *node_name = NULL;
Kevin Wolf18edf282015-04-07 17:12:56 +0200898 QemuOpts *opts;
Kevin Wolf62392eb2015-04-24 16:38:02 +0200899 BlockDriver *drv;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200900 Error *local_err = NULL;
Kevin Wolf57915332010-04-14 15:24:50 +0200901
Paolo Bonzini64058752012-05-08 16:51:49 +0200902 assert(bs->file == NULL);
Kevin Wolf707ff822013-03-06 12:20:31 +0100903 assert(options != NULL && bs->options != options);
Kevin Wolf57915332010-04-14 15:24:50 +0200904
Kevin Wolf62392eb2015-04-24 16:38:02 +0200905 opts = qemu_opts_create(&bdrv_runtime_opts, NULL, 0, &error_abort);
906 qemu_opts_absorb_qdict(opts, options, &local_err);
907 if (local_err) {
908 error_propagate(errp, local_err);
909 ret = -EINVAL;
910 goto fail_opts;
911 }
912
913 driver_name = qemu_opt_get(opts, "driver");
914 drv = bdrv_find_format(driver_name);
915 assert(drv != NULL);
916
Kevin Wolf45673672013-04-22 17:48:40 +0200917 if (file != NULL) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200918 filename = file->bs->filename;
Kevin Wolf45673672013-04-22 17:48:40 +0200919 } else {
920 filename = qdict_get_try_str(options, "filename");
921 }
922
Kevin Wolf765003d2014-02-03 14:49:42 +0100923 if (drv->bdrv_needs_filename && !filename) {
924 error_setg(errp, "The '%s' block driver requires a file name",
925 drv->format_name);
Kevin Wolf18edf282015-04-07 17:12:56 +0200926 ret = -EINVAL;
927 goto fail_opts;
928 }
929
Kevin Wolf82dc8b42016-01-11 19:07:50 +0100930 trace_bdrv_open_common(bs, filename ?: "", bs->open_flags,
931 drv->format_name);
Kevin Wolf62392eb2015-04-24 16:38:02 +0200932
Kevin Wolf18edf282015-04-07 17:12:56 +0200933 node_name = qemu_opt_get(opts, "node-name");
Kevin Wolf636ea372014-01-24 14:11:52 +0100934 bdrv_assign_node_name(bs, node_name, &local_err);
Markus Armbruster0fb63952014-04-25 16:50:31 +0200935 if (local_err) {
Kevin Wolf636ea372014-01-24 14:11:52 +0100936 error_propagate(errp, local_err);
Kevin Wolf18edf282015-04-07 17:12:56 +0200937 ret = -EINVAL;
938 goto fail_opts;
Kevin Wolf5d186eb2013-03-27 17:28:18 +0100939 }
940
Kevin Wolf82dc8b42016-01-11 19:07:50 +0100941 bs->read_only = !(bs->open_flags & BDRV_O_RDWR);
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800942
943 if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, bs->read_only)) {
Kevin Wolf8f94a6e2013-10-10 11:45:55 +0200944 error_setg(errp,
945 !bs->read_only && bdrv_is_whitelisted(drv, true)
946 ? "Driver '%s' can only be used for read-only devices"
947 : "Driver '%s' is not whitelisted",
948 drv->format_name);
Kevin Wolf18edf282015-04-07 17:12:56 +0200949 ret = -ENOTSUP;
950 goto fail_opts;
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800951 }
Kevin Wolf57915332010-04-14 15:24:50 +0200952
Stefan Hajnoczi53fec9d2011-11-28 16:08:47 +0000953 assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */
Kevin Wolf82dc8b42016-01-11 19:07:50 +0100954 if (bs->open_flags & BDRV_O_COPY_ON_READ) {
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200955 if (!bs->read_only) {
956 bdrv_enable_copy_on_read(bs);
957 } else {
958 error_setg(errp, "Can't use copy-on-read on read-only device");
Kevin Wolf18edf282015-04-07 17:12:56 +0200959 ret = -EINVAL;
960 goto fail_opts;
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200961 }
Stefan Hajnoczi53fec9d2011-11-28 16:08:47 +0000962 }
963
Kevin Wolfc2ad1b02013-03-18 16:40:51 +0100964 if (filename != NULL) {
965 pstrcpy(bs->filename, sizeof(bs->filename), filename);
966 } else {
967 bs->filename[0] = '\0';
968 }
Max Reitz91af7012014-07-18 20:24:56 +0200969 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), bs->filename);
Kevin Wolf57915332010-04-14 15:24:50 +0200970
Kevin Wolf57915332010-04-14 15:24:50 +0200971 bs->drv = drv;
Anthony Liguori7267c092011-08-20 22:09:37 -0500972 bs->opaque = g_malloc0(drv->instance_size);
Kevin Wolf57915332010-04-14 15:24:50 +0200973
Kevin Wolf91a097e2015-05-08 17:49:53 +0200974 /* Apply cache mode options */
975 update_flags_from_options(&bs->open_flags, opts);
Kevin Wolf73ac4512016-03-14 15:46:03 +0100976
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200977 /* Open the image, either directly or using a protocol */
Kevin Wolf82dc8b42016-01-11 19:07:50 +0100978 open_flags = bdrv_open_flags(bs, bs->open_flags);
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200979 if (drv->bdrv_file_open) {
Kevin Wolf5d186eb2013-03-27 17:28:18 +0100980 assert(file == NULL);
Benoît Canet030be322013-09-24 17:07:04 +0200981 assert(!drv->bdrv_needs_filename || filename != NULL);
Max Reitz34b5d2c2013-09-05 14:45:29 +0200982 ret = drv->bdrv_file_open(bs, options, open_flags, &local_err);
Kevin Wolff500a6d2012-11-12 17:35:27 +0100983 } else {
Kevin Wolf2af5ef72013-04-09 13:19:18 +0200984 if (file == NULL) {
Max Reitz34b5d2c2013-09-05 14:45:29 +0200985 error_setg(errp, "Can't use '%s' as a block driver for the "
986 "protocol level", drv->format_name);
Kevin Wolf2af5ef72013-04-09 13:19:18 +0200987 ret = -EINVAL;
988 goto free_and_fail;
989 }
Kevin Wolff500a6d2012-11-12 17:35:27 +0100990 bs->file = file;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200991 ret = drv->bdrv_open(bs, options, open_flags, &local_err);
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200992 }
993
Kevin Wolf57915332010-04-14 15:24:50 +0200994 if (ret < 0) {
Markus Armbruster84d18f02014-01-30 15:07:28 +0100995 if (local_err) {
Max Reitz34b5d2c2013-09-05 14:45:29 +0200996 error_propagate(errp, local_err);
Dunrong Huang2fa9aa52013-09-24 18:14:01 +0800997 } else if (bs->filename[0]) {
998 error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename);
Max Reitz34b5d2c2013-09-05 14:45:29 +0200999 } else {
1000 error_setg_errno(errp, -ret, "Could not open image");
1001 }
Kevin Wolf57915332010-04-14 15:24:50 +02001002 goto free_and_fail;
1003 }
1004
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001005 ret = refresh_total_sectors(bs, bs->total_sectors);
1006 if (ret < 0) {
Max Reitz34b5d2c2013-09-05 14:45:29 +02001007 error_setg_errno(errp, -ret, "Could not refresh total sector count");
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001008 goto free_and_fail;
Kevin Wolf57915332010-04-14 15:24:50 +02001009 }
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001010
Kevin Wolf3baca892014-07-16 17:48:16 +02001011 bdrv_refresh_limits(bs, &local_err);
1012 if (local_err) {
1013 error_propagate(errp, local_err);
1014 ret = -EINVAL;
1015 goto free_and_fail;
1016 }
1017
Paolo Bonzinic25f53b2011-11-29 12:42:20 +01001018 assert(bdrv_opt_mem_align(bs) != 0);
Denis V. Lunev4196d2f2015-05-12 17:30:55 +03001019 assert(bdrv_min_mem_align(bs) != 0);
Eric Blakea5b8dd22016-06-23 16:37:24 -06001020 assert(is_power_of_2(bs->bl.request_alignment));
Kevin Wolf18edf282015-04-07 17:12:56 +02001021
1022 qemu_opts_del(opts);
Kevin Wolf57915332010-04-14 15:24:50 +02001023 return 0;
1024
1025free_and_fail:
Kevin Wolff500a6d2012-11-12 17:35:27 +01001026 bs->file = NULL;
Anthony Liguori7267c092011-08-20 22:09:37 -05001027 g_free(bs->opaque);
Kevin Wolf57915332010-04-14 15:24:50 +02001028 bs->opaque = NULL;
1029 bs->drv = NULL;
Kevin Wolf18edf282015-04-07 17:12:56 +02001030fail_opts:
1031 qemu_opts_del(opts);
Kevin Wolf57915332010-04-14 15:24:50 +02001032 return ret;
1033}
1034
Kevin Wolf5e5c4f62014-05-26 11:45:08 +02001035static QDict *parse_json_filename(const char *filename, Error **errp)
1036{
1037 QObject *options_obj;
1038 QDict *options;
1039 int ret;
1040
1041 ret = strstart(filename, "json:", &filename);
1042 assert(ret);
1043
1044 options_obj = qobject_from_json(filename);
1045 if (!options_obj) {
1046 error_setg(errp, "Could not parse the JSON options");
1047 return NULL;
1048 }
1049
1050 if (qobject_type(options_obj) != QTYPE_QDICT) {
1051 qobject_decref(options_obj);
1052 error_setg(errp, "Invalid JSON object given");
1053 return NULL;
1054 }
1055
1056 options = qobject_to_qdict(options_obj);
1057 qdict_flatten(options);
1058
1059 return options;
1060}
1061
Kevin Wolfde3b53f2015-10-29 15:24:41 +01001062static void parse_json_protocol(QDict *options, const char **pfilename,
1063 Error **errp)
1064{
1065 QDict *json_options;
1066 Error *local_err = NULL;
1067
1068 /* Parse json: pseudo-protocol */
1069 if (!*pfilename || !g_str_has_prefix(*pfilename, "json:")) {
1070 return;
1071 }
1072
1073 json_options = parse_json_filename(*pfilename, &local_err);
1074 if (local_err) {
1075 error_propagate(errp, local_err);
1076 return;
1077 }
1078
1079 /* Options given in the filename have lower priority than options
1080 * specified directly */
1081 qdict_join(options, json_options, false);
1082 QDECREF(json_options);
1083 *pfilename = NULL;
1084}
1085
Kevin Wolf57915332010-04-14 15:24:50 +02001086/*
Kevin Wolff54120f2014-05-26 11:09:59 +02001087 * Fills in default options for opening images and converts the legacy
1088 * filename/flags pair to option QDict entries.
Max Reitz53a29512015-03-19 14:53:16 -04001089 * The BDRV_O_PROTOCOL flag in *flags will be set or cleared accordingly if a
1090 * block driver has been specified explicitly.
Kevin Wolff54120f2014-05-26 11:09:59 +02001091 */
Kevin Wolfde3b53f2015-10-29 15:24:41 +01001092static int bdrv_fill_options(QDict **options, const char *filename,
Max Reitz053e1572015-08-26 19:47:51 +02001093 int *flags, Error **errp)
Kevin Wolff54120f2014-05-26 11:09:59 +02001094{
1095 const char *drvname;
Max Reitz53a29512015-03-19 14:53:16 -04001096 bool protocol = *flags & BDRV_O_PROTOCOL;
Kevin Wolff54120f2014-05-26 11:09:59 +02001097 bool parse_filename = false;
Max Reitz053e1572015-08-26 19:47:51 +02001098 BlockDriver *drv = NULL;
Kevin Wolff54120f2014-05-26 11:09:59 +02001099 Error *local_err = NULL;
Kevin Wolff54120f2014-05-26 11:09:59 +02001100
Max Reitz53a29512015-03-19 14:53:16 -04001101 drvname = qdict_get_try_str(*options, "driver");
Max Reitz053e1572015-08-26 19:47:51 +02001102 if (drvname) {
1103 drv = bdrv_find_format(drvname);
1104 if (!drv) {
1105 error_setg(errp, "Unknown driver '%s'", drvname);
1106 return -ENOENT;
1107 }
1108 /* If the user has explicitly specified the driver, this choice should
1109 * override the BDRV_O_PROTOCOL flag */
1110 protocol = drv->bdrv_file_open;
Max Reitz53a29512015-03-19 14:53:16 -04001111 }
1112
1113 if (protocol) {
1114 *flags |= BDRV_O_PROTOCOL;
1115 } else {
1116 *flags &= ~BDRV_O_PROTOCOL;
1117 }
1118
Kevin Wolf91a097e2015-05-08 17:49:53 +02001119 /* Translate cache options from flags into options */
1120 update_options_from_flags(*options, *flags);
1121
Kevin Wolff54120f2014-05-26 11:09:59 +02001122 /* Fetch the file name from the options QDict if necessary */
Kevin Wolf17b005f2014-05-27 10:50:29 +02001123 if (protocol && filename) {
Kevin Wolff54120f2014-05-26 11:09:59 +02001124 if (!qdict_haskey(*options, "filename")) {
1125 qdict_put(*options, "filename", qstring_from_str(filename));
1126 parse_filename = true;
1127 } else {
1128 error_setg(errp, "Can't specify 'file' and 'filename' options at "
1129 "the same time");
1130 return -EINVAL;
1131 }
1132 }
1133
1134 /* Find the right block driver */
1135 filename = qdict_get_try_str(*options, "filename");
Kevin Wolff54120f2014-05-26 11:09:59 +02001136
Max Reitz053e1572015-08-26 19:47:51 +02001137 if (!drvname && protocol) {
1138 if (filename) {
1139 drv = bdrv_find_protocol(filename, parse_filename, errp);
1140 if (!drv) {
Kevin Wolff54120f2014-05-26 11:09:59 +02001141 return -EINVAL;
1142 }
Max Reitz053e1572015-08-26 19:47:51 +02001143
1144 drvname = drv->format_name;
1145 qdict_put(*options, "driver", qstring_from_str(drvname));
1146 } else {
1147 error_setg(errp, "Must specify either driver or file");
1148 return -EINVAL;
Kevin Wolff54120f2014-05-26 11:09:59 +02001149 }
1150 }
1151
Kevin Wolf17b005f2014-05-27 10:50:29 +02001152 assert(drv || !protocol);
Kevin Wolff54120f2014-05-26 11:09:59 +02001153
1154 /* Driver-specific filename parsing */
Kevin Wolf17b005f2014-05-27 10:50:29 +02001155 if (drv && drv->bdrv_parse_filename && parse_filename) {
Kevin Wolff54120f2014-05-26 11:09:59 +02001156 drv->bdrv_parse_filename(filename, *options, &local_err);
1157 if (local_err) {
1158 error_propagate(errp, local_err);
1159 return -EINVAL;
1160 }
1161
1162 if (!drv->bdrv_needs_filename) {
1163 qdict_del(*options, "filename");
1164 }
1165 }
1166
1167 return 0;
1168}
1169
Kevin Wolfe9740bc2016-05-23 15:52:26 +02001170static void bdrv_replace_child(BdrvChild *child, BlockDriverState *new_bs)
1171{
1172 BlockDriverState *old_bs = child->bs;
1173
1174 if (old_bs) {
Kevin Wolf36fe1332016-05-17 14:51:55 +02001175 if (old_bs->quiesce_counter && child->role->drained_end) {
1176 child->role->drained_end(child);
1177 }
Kevin Wolfe9740bc2016-05-23 15:52:26 +02001178 QLIST_REMOVE(child, next_parent);
1179 }
Kevin Wolfe9740bc2016-05-23 15:52:26 +02001180
1181 child->bs = new_bs;
Kevin Wolf36fe1332016-05-17 14:51:55 +02001182
1183 if (new_bs) {
1184 QLIST_INSERT_HEAD(&new_bs->parents, child, next_parent);
1185 if (new_bs->quiesce_counter && child->role->drained_begin) {
1186 child->role->drained_begin(child);
1187 }
1188 }
Kevin Wolfe9740bc2016-05-23 15:52:26 +02001189}
1190
Kevin Wolff21d96d2016-03-08 13:47:46 +01001191BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs,
1192 const char *child_name,
Kevin Wolf36fe1332016-05-17 14:51:55 +02001193 const BdrvChildRole *child_role,
1194 void *opaque)
Kevin Wolfdf581792015-06-15 11:53:47 +02001195{
1196 BdrvChild *child = g_new(BdrvChild, 1);
1197 *child = (BdrvChild) {
Kevin Wolfe9740bc2016-05-23 15:52:26 +02001198 .bs = NULL,
Kevin Wolf260fecf2015-04-27 13:46:22 +02001199 .name = g_strdup(child_name),
Kevin Wolfdf581792015-06-15 11:53:47 +02001200 .role = child_role,
Kevin Wolf36fe1332016-05-17 14:51:55 +02001201 .opaque = opaque,
Kevin Wolfdf581792015-06-15 11:53:47 +02001202 };
1203
Kevin Wolfe9740bc2016-05-23 15:52:26 +02001204 bdrv_replace_child(child, child_bs);
Kevin Wolfb4b059f2015-06-15 13:24:19 +02001205
1206 return child;
Kevin Wolfdf581792015-06-15 11:53:47 +02001207}
1208
Wen Congyang98292c62016-05-10 15:36:38 +08001209BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs,
1210 BlockDriverState *child_bs,
1211 const char *child_name,
1212 const BdrvChildRole *child_role)
Kevin Wolff21d96d2016-03-08 13:47:46 +01001213{
Kevin Wolf36fe1332016-05-17 14:51:55 +02001214 BdrvChild *child = bdrv_root_attach_child(child_bs, child_name, child_role,
Kevin Wolf20018e12016-05-23 18:46:59 +02001215 parent_bs);
Kevin Wolff21d96d2016-03-08 13:47:46 +01001216 QLIST_INSERT_HEAD(&parent_bs->children, child, next);
1217 return child;
1218}
1219
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001220static void bdrv_detach_child(BdrvChild *child)
Kevin Wolf33a60402015-06-15 13:51:04 +02001221{
Kevin Wolff21d96d2016-03-08 13:47:46 +01001222 if (child->next.le_prev) {
1223 QLIST_REMOVE(child, next);
1224 child->next.le_prev = NULL;
1225 }
Kevin Wolfe9740bc2016-05-23 15:52:26 +02001226
1227 bdrv_replace_child(child, NULL);
1228
Kevin Wolf260fecf2015-04-27 13:46:22 +02001229 g_free(child->name);
Kevin Wolf33a60402015-06-15 13:51:04 +02001230 g_free(child);
1231}
1232
Kevin Wolff21d96d2016-03-08 13:47:46 +01001233void bdrv_root_unref_child(BdrvChild *child)
Kevin Wolf33a60402015-06-15 13:51:04 +02001234{
Kevin Wolf779020c2015-10-13 14:09:44 +02001235 BlockDriverState *child_bs;
1236
Kevin Wolff21d96d2016-03-08 13:47:46 +01001237 child_bs = child->bs;
1238 bdrv_detach_child(child);
1239 bdrv_unref(child_bs);
1240}
1241
1242void bdrv_unref_child(BlockDriverState *parent, BdrvChild *child)
1243{
Kevin Wolf779020c2015-10-13 14:09:44 +02001244 if (child == NULL) {
1245 return;
1246 }
Kevin Wolf33a60402015-06-15 13:51:04 +02001247
1248 if (child->bs->inherits_from == parent) {
1249 child->bs->inherits_from = NULL;
1250 }
1251
Kevin Wolff21d96d2016-03-08 13:47:46 +01001252 bdrv_root_unref_child(child);
Kevin Wolf33a60402015-06-15 13:51:04 +02001253}
1254
Kevin Wolf5c8cab42016-02-24 15:13:35 +01001255
1256static void bdrv_parent_cb_change_media(BlockDriverState *bs, bool load)
1257{
1258 BdrvChild *c;
1259 QLIST_FOREACH(c, &bs->parents, next_parent) {
1260 if (c->role->change_media) {
1261 c->role->change_media(c, load);
1262 }
1263 }
1264}
1265
1266static void bdrv_parent_cb_resize(BlockDriverState *bs)
1267{
1268 BdrvChild *c;
1269 QLIST_FOREACH(c, &bs->parents, next_parent) {
1270 if (c->role->resize) {
1271 c->role->resize(c);
1272 }
1273 }
1274}
1275
Kevin Wolf5db15a52015-09-14 15:33:33 +02001276/*
1277 * Sets the backing file link of a BDS. A new reference is created; callers
1278 * which don't need their own reference any more must call bdrv_unref().
1279 */
Fam Zheng8d24cce2014-05-23 21:29:45 +08001280void bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd)
1281{
Kevin Wolf5db15a52015-09-14 15:33:33 +02001282 if (backing_hd) {
1283 bdrv_ref(backing_hd);
1284 }
Fam Zheng8d24cce2014-05-23 21:29:45 +08001285
Kevin Wolf760e0062015-06-17 14:55:21 +02001286 if (bs->backing) {
Fam Zheng826b6ca2014-05-23 21:29:47 +08001287 assert(bs->backing_blocker);
Kevin Wolf760e0062015-06-17 14:55:21 +02001288 bdrv_op_unblock_all(bs->backing->bs, bs->backing_blocker);
Kevin Wolf5db15a52015-09-14 15:33:33 +02001289 bdrv_unref_child(bs, bs->backing);
Fam Zheng826b6ca2014-05-23 21:29:47 +08001290 } else if (backing_hd) {
1291 error_setg(&bs->backing_blocker,
Alberto Garcia81e5f782015-04-08 12:29:19 +03001292 "node is used as backing hd of '%s'",
1293 bdrv_get_device_or_node_name(bs));
Fam Zheng826b6ca2014-05-23 21:29:47 +08001294 }
1295
Fam Zheng8d24cce2014-05-23 21:29:45 +08001296 if (!backing_hd) {
Fam Zheng826b6ca2014-05-23 21:29:47 +08001297 error_free(bs->backing_blocker);
1298 bs->backing_blocker = NULL;
Kevin Wolf760e0062015-06-17 14:55:21 +02001299 bs->backing = NULL;
Fam Zheng8d24cce2014-05-23 21:29:45 +08001300 goto out;
1301 }
Kevin Wolf260fecf2015-04-27 13:46:22 +02001302 bs->backing = bdrv_attach_child(bs, backing_hd, "backing", &child_backing);
Fam Zheng8d24cce2014-05-23 21:29:45 +08001303 bs->open_flags &= ~BDRV_O_NO_BACKING;
1304 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_hd->filename);
1305 pstrcpy(bs->backing_format, sizeof(bs->backing_format),
1306 backing_hd->drv ? backing_hd->drv->format_name : "");
Fam Zheng826b6ca2014-05-23 21:29:47 +08001307
Kevin Wolf760e0062015-06-17 14:55:21 +02001308 bdrv_op_block_all(backing_hd, bs->backing_blocker);
Fam Zheng826b6ca2014-05-23 21:29:47 +08001309 /* Otherwise we won't be able to commit due to check in bdrv_commit */
Kevin Wolf760e0062015-06-17 14:55:21 +02001310 bdrv_op_unblock(backing_hd, BLOCK_OP_TYPE_COMMIT_TARGET,
Fam Zheng826b6ca2014-05-23 21:29:47 +08001311 bs->backing_blocker);
Fam Zheng8d24cce2014-05-23 21:29:45 +08001312out:
Kevin Wolf3baca892014-07-16 17:48:16 +02001313 bdrv_refresh_limits(bs, NULL);
Fam Zheng8d24cce2014-05-23 21:29:45 +08001314}
1315
Kevin Wolf31ca6d02013-03-28 15:29:24 +01001316/*
1317 * Opens the backing file for a BlockDriverState if not yet open
1318 *
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001319 * bdref_key specifies the key for the image's BlockdevRef in the options QDict.
1320 * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict
1321 * itself, all options starting with "${bdref_key}." are considered part of the
1322 * BlockdevRef.
1323 *
1324 * TODO Can this be unified with bdrv_open_image()?
Kevin Wolf31ca6d02013-03-28 15:29:24 +01001325 */
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001326int bdrv_open_backing_file(BlockDriverState *bs, QDict *parent_options,
1327 const char *bdref_key, Error **errp)
Paolo Bonzini9156df12012-10-18 16:49:17 +02001328{
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001329 char *backing_filename = g_malloc0(PATH_MAX);
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001330 char *bdref_key_dot;
1331 const char *reference = NULL;
Kevin Wolf317fc442014-04-25 13:27:34 +02001332 int ret = 0;
Fam Zheng8d24cce2014-05-23 21:29:45 +08001333 BlockDriverState *backing_hd;
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001334 QDict *options;
1335 QDict *tmp_parent_options = NULL;
Max Reitz34b5d2c2013-09-05 14:45:29 +02001336 Error *local_err = NULL;
Paolo Bonzini9156df12012-10-18 16:49:17 +02001337
Kevin Wolf760e0062015-06-17 14:55:21 +02001338 if (bs->backing != NULL) {
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001339 goto free_exit;
Paolo Bonzini9156df12012-10-18 16:49:17 +02001340 }
1341
Kevin Wolf31ca6d02013-03-28 15:29:24 +01001342 /* NULL means an empty set of options */
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001343 if (parent_options == NULL) {
1344 tmp_parent_options = qdict_new();
1345 parent_options = tmp_parent_options;
Kevin Wolf31ca6d02013-03-28 15:29:24 +01001346 }
1347
Paolo Bonzini9156df12012-10-18 16:49:17 +02001348 bs->open_flags &= ~BDRV_O_NO_BACKING;
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001349
1350 bdref_key_dot = g_strdup_printf("%s.", bdref_key);
1351 qdict_extract_subqdict(parent_options, &options, bdref_key_dot);
1352 g_free(bdref_key_dot);
1353
1354 reference = qdict_get_try_str(parent_options, bdref_key);
1355 if (reference || qdict_haskey(options, "file.filename")) {
Kevin Wolf1cb6f502013-04-12 20:27:07 +02001356 backing_filename[0] = '\0';
1357 } else if (bs->backing_file[0] == '\0' && qdict_size(options) == 0) {
Kevin Wolf31ca6d02013-03-28 15:29:24 +01001358 QDECREF(options);
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001359 goto free_exit;
Fam Zhengdbecebd2013-09-22 20:05:06 +08001360 } else {
Max Reitz9f074292014-11-26 17:20:26 +01001361 bdrv_get_full_backing_filename(bs, backing_filename, PATH_MAX,
1362 &local_err);
1363 if (local_err) {
1364 ret = -EINVAL;
1365 error_propagate(errp, local_err);
1366 QDECREF(options);
1367 goto free_exit;
1368 }
Paolo Bonzini9156df12012-10-18 16:49:17 +02001369 }
1370
Kevin Wolf8ee79e72014-06-04 15:09:35 +02001371 if (!bs->drv || !bs->drv->supports_backing) {
1372 ret = -EINVAL;
1373 error_setg(errp, "Driver doesn't support backing files");
1374 QDECREF(options);
1375 goto free_exit;
1376 }
1377
Kevin Wolfc5f6e492014-11-25 18:12:42 +01001378 if (bs->backing_format[0] != '\0' && !qdict_haskey(options, "driver")) {
1379 qdict_put(options, "driver", qstring_from_str(bs->backing_format));
Paolo Bonzini9156df12012-10-18 16:49:17 +02001380 }
1381
Max Reitz5b363932016-05-17 16:41:31 +02001382 backing_hd = bdrv_open_inherit(*backing_filename ? backing_filename : NULL,
1383 reference, options, 0, bs, &child_backing,
1384 errp);
1385 if (!backing_hd) {
Paolo Bonzini9156df12012-10-18 16:49:17 +02001386 bs->open_flags |= BDRV_O_NO_BACKING;
Markus Armbrustere43bfd92015-12-18 16:35:15 +01001387 error_prepend(errp, "Could not open backing file: ");
Max Reitz5b363932016-05-17 16:41:31 +02001388 ret = -EINVAL;
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001389 goto free_exit;
Paolo Bonzini9156df12012-10-18 16:49:17 +02001390 }
Kevin Wolfdf581792015-06-15 11:53:47 +02001391
Kevin Wolf5db15a52015-09-14 15:33:33 +02001392 /* Hook up the backing file link; drop our reference, bs owns the
1393 * backing_hd reference now */
Fam Zheng8d24cce2014-05-23 21:29:45 +08001394 bdrv_set_backing_hd(bs, backing_hd);
Kevin Wolf5db15a52015-09-14 15:33:33 +02001395 bdrv_unref(backing_hd);
Peter Feinerd80ac652014-01-08 19:43:25 +00001396
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001397 qdict_del(parent_options, bdref_key);
1398
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001399free_exit:
1400 g_free(backing_filename);
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001401 QDECREF(tmp_parent_options);
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001402 return ret;
Paolo Bonzini9156df12012-10-18 16:49:17 +02001403}
1404
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001405/*
Max Reitzda557aa2013-12-20 19:28:11 +01001406 * Opens a disk image whose options are given as BlockdevRef in another block
1407 * device's options.
1408 *
Max Reitzda557aa2013-12-20 19:28:11 +01001409 * If allow_none is true, no image will be opened if filename is false and no
Kevin Wolfb4b059f2015-06-15 13:24:19 +02001410 * BlockdevRef is given. NULL will be returned, but errp remains unset.
Max Reitzda557aa2013-12-20 19:28:11 +01001411 *
1412 * bdrev_key specifies the key for the image's BlockdevRef in the options QDict.
1413 * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict
1414 * itself, all options starting with "${bdref_key}." are considered part of the
1415 * BlockdevRef.
1416 *
1417 * The BlockdevRef will be removed from the options QDict.
1418 */
Kevin Wolfb4b059f2015-06-15 13:24:19 +02001419BdrvChild *bdrv_open_child(const char *filename,
1420 QDict *options, const char *bdref_key,
1421 BlockDriverState* parent,
1422 const BdrvChildRole *child_role,
1423 bool allow_none, Error **errp)
Max Reitzda557aa2013-12-20 19:28:11 +01001424{
Kevin Wolfb4b059f2015-06-15 13:24:19 +02001425 BdrvChild *c = NULL;
1426 BlockDriverState *bs;
Max Reitzda557aa2013-12-20 19:28:11 +01001427 QDict *image_options;
Max Reitzda557aa2013-12-20 19:28:11 +01001428 char *bdref_key_dot;
1429 const char *reference;
1430
Kevin Wolfdf581792015-06-15 11:53:47 +02001431 assert(child_role != NULL);
Max Reitzf67503e2014-02-18 18:33:05 +01001432
Max Reitzda557aa2013-12-20 19:28:11 +01001433 bdref_key_dot = g_strdup_printf("%s.", bdref_key);
1434 qdict_extract_subqdict(options, &image_options, bdref_key_dot);
1435 g_free(bdref_key_dot);
1436
1437 reference = qdict_get_try_str(options, bdref_key);
1438 if (!filename && !reference && !qdict_size(image_options)) {
Kevin Wolfb4b059f2015-06-15 13:24:19 +02001439 if (!allow_none) {
Max Reitzda557aa2013-12-20 19:28:11 +01001440 error_setg(errp, "A block device must be specified for \"%s\"",
1441 bdref_key);
Max Reitzda557aa2013-12-20 19:28:11 +01001442 }
Markus Armbrusterb20e61e2014-05-28 11:16:57 +02001443 QDECREF(image_options);
Max Reitzda557aa2013-12-20 19:28:11 +01001444 goto done;
1445 }
1446
Max Reitz5b363932016-05-17 16:41:31 +02001447 bs = bdrv_open_inherit(filename, reference, image_options, 0,
1448 parent, child_role, errp);
1449 if (!bs) {
Kevin Wolfdf581792015-06-15 11:53:47 +02001450 goto done;
1451 }
1452
Kevin Wolf260fecf2015-04-27 13:46:22 +02001453 c = bdrv_attach_child(parent, bs, bdref_key, child_role);
Max Reitzda557aa2013-12-20 19:28:11 +01001454
1455done:
1456 qdict_del(options, bdref_key);
Kevin Wolfb4b059f2015-06-15 13:24:19 +02001457 return c;
1458}
1459
Max Reitz66836182016-05-17 16:41:27 +02001460static BlockDriverState *bdrv_append_temp_snapshot(BlockDriverState *bs,
1461 int flags,
1462 QDict *snapshot_options,
1463 Error **errp)
Kevin Wolfb9988752014-04-03 12:09:34 +02001464{
1465 /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001466 char *tmp_filename = g_malloc0(PATH_MAX + 1);
Kevin Wolfb9988752014-04-03 12:09:34 +02001467 int64_t total_size;
Chunyan Liu83d05212014-06-05 17:20:51 +08001468 QemuOpts *opts = NULL;
Kevin Wolfb9988752014-04-03 12:09:34 +02001469 BlockDriverState *bs_snapshot;
Kevin Wolfb9988752014-04-03 12:09:34 +02001470 int ret;
1471
1472 /* if snapshot, we create a temporary backing file and open it
1473 instead of opening 'filename' directly */
1474
1475 /* Get the required size from the image */
Kevin Wolff1877432014-04-04 17:07:19 +02001476 total_size = bdrv_getlength(bs);
1477 if (total_size < 0) {
1478 error_setg_errno(errp, -total_size, "Could not get image size");
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001479 goto out;
Kevin Wolff1877432014-04-04 17:07:19 +02001480 }
Kevin Wolfb9988752014-04-03 12:09:34 +02001481
1482 /* Create the temporary image */
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001483 ret = get_tmp_filename(tmp_filename, PATH_MAX + 1);
Kevin Wolfb9988752014-04-03 12:09:34 +02001484 if (ret < 0) {
1485 error_setg_errno(errp, -ret, "Could not get temporary filename");
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001486 goto out;
Kevin Wolfb9988752014-04-03 12:09:34 +02001487 }
1488
Max Reitzef810432014-12-02 18:32:42 +01001489 opts = qemu_opts_create(bdrv_qcow2.create_opts, NULL, 0,
Chunyan Liuc282e1f2014-06-05 17:21:11 +08001490 &error_abort);
Markus Armbruster39101f22015-02-12 16:46:36 +01001491 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_size, &error_abort);
Markus Armbrustere43bfd92015-12-18 16:35:15 +01001492 ret = bdrv_create(&bdrv_qcow2, tmp_filename, opts, errp);
Chunyan Liu83d05212014-06-05 17:20:51 +08001493 qemu_opts_del(opts);
Kevin Wolfb9988752014-04-03 12:09:34 +02001494 if (ret < 0) {
Markus Armbrustere43bfd92015-12-18 16:35:15 +01001495 error_prepend(errp, "Could not create temporary overlay '%s': ",
1496 tmp_filename);
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001497 goto out;
Kevin Wolfb9988752014-04-03 12:09:34 +02001498 }
1499
Kevin Wolf73176be2016-03-07 13:02:15 +01001500 /* Prepare options QDict for the temporary file */
Kevin Wolfb9988752014-04-03 12:09:34 +02001501 qdict_put(snapshot_options, "file.driver",
1502 qstring_from_str("file"));
1503 qdict_put(snapshot_options, "file.filename",
1504 qstring_from_str(tmp_filename));
Max Reitze6641712015-08-26 19:47:48 +02001505 qdict_put(snapshot_options, "driver",
1506 qstring_from_str("qcow2"));
Kevin Wolfb9988752014-04-03 12:09:34 +02001507
Max Reitz5b363932016-05-17 16:41:31 +02001508 bs_snapshot = bdrv_open(NULL, NULL, snapshot_options, flags, errp);
Kevin Wolf73176be2016-03-07 13:02:15 +01001509 snapshot_options = NULL;
Max Reitz5b363932016-05-17 16:41:31 +02001510 if (!bs_snapshot) {
1511 ret = -EINVAL;
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001512 goto out;
Kevin Wolfb9988752014-04-03 12:09:34 +02001513 }
1514
Max Reitz66836182016-05-17 16:41:27 +02001515 /* bdrv_append() consumes a strong reference to bs_snapshot (i.e. it will
1516 * call bdrv_unref() on it), so in order to be able to return one, we have
1517 * to increase bs_snapshot's refcount here */
1518 bdrv_ref(bs_snapshot);
Kevin Wolfb9988752014-04-03 12:09:34 +02001519 bdrv_append(bs_snapshot, bs);
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001520
Max Reitz66836182016-05-17 16:41:27 +02001521 g_free(tmp_filename);
1522 return bs_snapshot;
1523
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001524out:
Kevin Wolf73176be2016-03-07 13:02:15 +01001525 QDECREF(snapshot_options);
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001526 g_free(tmp_filename);
Max Reitz66836182016-05-17 16:41:27 +02001527 return NULL;
Kevin Wolfb9988752014-04-03 12:09:34 +02001528}
1529
Max Reitzda557aa2013-12-20 19:28:11 +01001530/*
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001531 * Opens a disk image (raw, qcow2, vmdk, ...)
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001532 *
1533 * options is a QDict of options to pass to the block drivers, or NULL for an
1534 * empty set of options. The reference to the QDict belongs to the block layer
1535 * after the call (even on failure), so if the caller intends to reuse the
1536 * dictionary, it needs to use QINCREF() before calling bdrv_open.
Max Reitzf67503e2014-02-18 18:33:05 +01001537 *
1538 * If *pbs is NULL, a new BDS will be created with a pointer to it stored there.
1539 * If it is not NULL, the referenced BDS will be reused.
Max Reitzddf56362014-02-18 18:33:06 +01001540 *
1541 * The reference parameter may be used to specify an existing block device which
1542 * should be opened. If specified, neither options nor a filename may be given,
1543 * nor can an existing BDS be reused (that is, *pbs has to be NULL).
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001544 */
Max Reitz5b363932016-05-17 16:41:31 +02001545static BlockDriverState *bdrv_open_inherit(const char *filename,
1546 const char *reference,
1547 QDict *options, int flags,
1548 BlockDriverState *parent,
1549 const BdrvChildRole *child_role,
1550 Error **errp)
bellardea2384d2004-08-01 21:59:26 +00001551{
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001552 int ret;
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02001553 BdrvChild *file = NULL;
1554 BlockDriverState *bs;
Max Reitzce343772015-08-26 19:47:50 +02001555 BlockDriver *drv = NULL;
Kevin Wolf74fe54f2013-07-09 11:09:02 +02001556 const char *drvname;
Alberto Garcia3e8c2e52015-10-26 14:27:15 +02001557 const char *backing;
Max Reitz34b5d2c2013-09-05 14:45:29 +02001558 Error *local_err = NULL;
Kevin Wolf73176be2016-03-07 13:02:15 +01001559 QDict *snapshot_options = NULL;
Kevin Wolfb1e6fc02014-05-06 12:11:42 +02001560 int snapshot_flags = 0;
bellard712e7872005-04-28 21:09:32 +00001561
Kevin Wolff3930ed2015-04-08 13:43:47 +02001562 assert(!child_role || !flags);
1563 assert(!child_role == !parent);
Max Reitzf67503e2014-02-18 18:33:05 +01001564
Max Reitzddf56362014-02-18 18:33:06 +01001565 if (reference) {
1566 bool options_non_empty = options ? qdict_size(options) : false;
1567 QDECREF(options);
1568
Max Reitzddf56362014-02-18 18:33:06 +01001569 if (filename || options_non_empty) {
1570 error_setg(errp, "Cannot reference an existing block device with "
1571 "additional options or a new filename");
Max Reitz5b363932016-05-17 16:41:31 +02001572 return NULL;
Max Reitzddf56362014-02-18 18:33:06 +01001573 }
1574
1575 bs = bdrv_lookup_bs(reference, reference, errp);
1576 if (!bs) {
Max Reitz5b363932016-05-17 16:41:31 +02001577 return NULL;
Max Reitzddf56362014-02-18 18:33:06 +01001578 }
Kevin Wolf76b22322016-04-04 17:11:13 +02001579
Max Reitzddf56362014-02-18 18:33:06 +01001580 bdrv_ref(bs);
Max Reitz5b363932016-05-17 16:41:31 +02001581 return bs;
Max Reitzddf56362014-02-18 18:33:06 +01001582 }
1583
Max Reitz5b363932016-05-17 16:41:31 +02001584 bs = bdrv_new();
Max Reitzf67503e2014-02-18 18:33:05 +01001585
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001586 /* NULL means an empty set of options */
1587 if (options == NULL) {
1588 options = qdict_new();
1589 }
1590
Kevin Wolf145f5982015-05-08 16:15:03 +02001591 /* json: syntax counts as explicit options, as if in the QDict */
Kevin Wolfde3b53f2015-10-29 15:24:41 +01001592 parse_json_protocol(options, &filename, &local_err);
1593 if (local_err) {
Kevin Wolfde3b53f2015-10-29 15:24:41 +01001594 goto fail;
1595 }
1596
Kevin Wolf145f5982015-05-08 16:15:03 +02001597 bs->explicit_options = qdict_clone_shallow(options);
1598
Kevin Wolff3930ed2015-04-08 13:43:47 +02001599 if (child_role) {
Kevin Wolfbddcec32015-04-09 18:47:50 +02001600 bs->inherits_from = parent;
Kevin Wolf8e2160e2015-04-29 17:29:39 +02001601 child_role->inherit_options(&flags, options,
1602 parent->open_flags, parent->options);
Kevin Wolff3930ed2015-04-08 13:43:47 +02001603 }
1604
Kevin Wolfde3b53f2015-10-29 15:24:41 +01001605 ret = bdrv_fill_options(&options, filename, &flags, &local_err);
Kevin Wolf462f5bc2014-05-26 11:39:55 +02001606 if (local_err) {
1607 goto fail;
1608 }
1609
Kevin Wolf62392eb2015-04-24 16:38:02 +02001610 bs->open_flags = flags;
1611 bs->options = options;
1612 options = qdict_clone_shallow(options);
1613
Kevin Wolf76c591b2014-06-04 14:19:44 +02001614 /* Find the right image format driver */
Kevin Wolf76c591b2014-06-04 14:19:44 +02001615 drvname = qdict_get_try_str(options, "driver");
1616 if (drvname) {
1617 drv = bdrv_find_format(drvname);
Kevin Wolf76c591b2014-06-04 14:19:44 +02001618 if (!drv) {
1619 error_setg(errp, "Unknown driver: '%s'", drvname);
Kevin Wolf76c591b2014-06-04 14:19:44 +02001620 goto fail;
1621 }
1622 }
1623
1624 assert(drvname || !(flags & BDRV_O_PROTOCOL));
Kevin Wolf76c591b2014-06-04 14:19:44 +02001625
Alberto Garcia3e8c2e52015-10-26 14:27:15 +02001626 backing = qdict_get_try_str(options, "backing");
1627 if (backing && *backing == '\0') {
1628 flags |= BDRV_O_NO_BACKING;
1629 qdict_del(options, "backing");
1630 }
1631
Kevin Wolff4788ad2014-06-03 16:44:19 +02001632 /* Open image file without format layer */
1633 if ((flags & BDRV_O_PROTOCOL) == 0) {
1634 if (flags & BDRV_O_RDWR) {
1635 flags |= BDRV_O_ALLOW_RDWR;
1636 }
1637 if (flags & BDRV_O_SNAPSHOT) {
Kevin Wolf73176be2016-03-07 13:02:15 +01001638 snapshot_options = qdict_new();
1639 bdrv_temp_snapshot_options(&snapshot_flags, snapshot_options,
1640 flags, options);
Kevin Wolf8e2160e2015-04-29 17:29:39 +02001641 bdrv_backing_options(&flags, options, flags, options);
Kevin Wolff4788ad2014-06-03 16:44:19 +02001642 }
1643
Kevin Wolff3930ed2015-04-08 13:43:47 +02001644 bs->open_flags = flags;
Kevin Wolf1fdd6932015-06-15 14:11:51 +02001645
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02001646 file = bdrv_open_child(filename, options, "file", bs,
1647 &child_file, true, &local_err);
Kevin Wolf1fdd6932015-06-15 14:11:51 +02001648 if (local_err) {
Max Reitz5469a2a2014-02-18 18:33:10 +01001649 goto fail;
1650 }
1651 }
1652
Kevin Wolf76c591b2014-06-04 14:19:44 +02001653 /* Image format probing */
Kevin Wolf38f3ef52014-11-20 16:27:12 +01001654 bs->probed = !drv;
Kevin Wolf76c591b2014-06-04 14:19:44 +02001655 if (!drv && file) {
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +02001656 ret = find_image_format(file, filename, &drv, &local_err);
Kevin Wolf17b005f2014-05-27 10:50:29 +02001657 if (ret < 0) {
Kevin Wolf8bfea152014-04-11 19:16:36 +02001658 goto fail;
Max Reitz2a05cbe2013-12-20 19:28:10 +01001659 }
Kevin Wolf62392eb2015-04-24 16:38:02 +02001660 /*
1661 * This option update would logically belong in bdrv_fill_options(),
1662 * but we first need to open bs->file for the probing to work, while
1663 * opening bs->file already requires the (mostly) final set of options
1664 * so that cache mode etc. can be inherited.
1665 *
1666 * Adding the driver later is somewhat ugly, but it's not an option
1667 * that would ever be inherited, so it's correct. We just need to make
1668 * sure to update both bs->options (which has the full effective
1669 * options for bs) and options (which has file.* already removed).
1670 */
1671 qdict_put(bs->options, "driver", qstring_from_str(drv->format_name));
1672 qdict_put(options, "driver", qstring_from_str(drv->format_name));
Kevin Wolf76c591b2014-06-04 14:19:44 +02001673 } else if (!drv) {
Kevin Wolf17b005f2014-05-27 10:50:29 +02001674 error_setg(errp, "Must specify either driver or file");
Kevin Wolf8bfea152014-04-11 19:16:36 +02001675 goto fail;
Kevin Wolff500a6d2012-11-12 17:35:27 +01001676 }
1677
Max Reitz53a29512015-03-19 14:53:16 -04001678 /* BDRV_O_PROTOCOL must be set iff a protocol BDS is about to be created */
1679 assert(!!(flags & BDRV_O_PROTOCOL) == !!drv->bdrv_file_open);
1680 /* file must be NULL if a protocol BDS is about to be created
1681 * (the inverse results in an error message from bdrv_open_common()) */
1682 assert(!(flags & BDRV_O_PROTOCOL) || !file);
1683
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001684 /* Open the image */
Kevin Wolf82dc8b42016-01-11 19:07:50 +01001685 ret = bdrv_open_common(bs, file, options, &local_err);
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001686 if (ret < 0) {
Kevin Wolf8bfea152014-04-11 19:16:36 +02001687 goto fail;
Christoph Hellwig69873072010-01-20 18:13:25 +01001688 }
1689
Max Reitz2a05cbe2013-12-20 19:28:10 +01001690 if (file && (bs->file != file)) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02001691 bdrv_unref_child(bs, file);
Kevin Wolff500a6d2012-11-12 17:35:27 +01001692 file = NULL;
1693 }
1694
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001695 /* If there is a backing file, use it */
Paolo Bonzini9156df12012-10-18 16:49:17 +02001696 if ((flags & BDRV_O_NO_BACKING) == 0) {
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001697 ret = bdrv_open_backing_file(bs, options, "backing", &local_err);
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001698 if (ret < 0) {
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001699 goto close_and_fail;
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001700 }
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001701 }
1702
Max Reitz91af7012014-07-18 20:24:56 +02001703 bdrv_refresh_filename(bs);
1704
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001705 /* Check if any unknown options were used */
Max Reitz5acd9d82014-02-18 18:33:11 +01001706 if (options && (qdict_size(options) != 0)) {
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001707 const QDictEntry *entry = qdict_first(options);
Max Reitz5acd9d82014-02-18 18:33:11 +01001708 if (flags & BDRV_O_PROTOCOL) {
1709 error_setg(errp, "Block protocol '%s' doesn't support the option "
1710 "'%s'", drv->format_name, entry->key);
1711 } else {
Max Reitzd0e46a52016-03-16 19:54:34 +01001712 error_setg(errp,
1713 "Block format '%s' does not support the option '%s'",
1714 drv->format_name, entry->key);
Max Reitz5acd9d82014-02-18 18:33:11 +01001715 }
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001716
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001717 goto close_and_fail;
1718 }
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001719
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001720 if (!bdrv_key_required(bs)) {
Kevin Wolf5c8cab42016-02-24 15:13:35 +01001721 bdrv_parent_cb_change_media(bs, true);
Markus Armbrusterc3adb582014-03-14 09:22:48 +01001722 } else if (!runstate_check(RUN_STATE_PRELAUNCH)
1723 && !runstate_check(RUN_STATE_INMIGRATE)
1724 && !runstate_check(RUN_STATE_PAUSED)) { /* HACK */
1725 error_setg(errp,
1726 "Guest must be stopped for opening of encrypted image");
Markus Armbrusterc3adb582014-03-14 09:22:48 +01001727 goto close_and_fail;
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001728 }
1729
Markus Armbrusterc3adb582014-03-14 09:22:48 +01001730 QDECREF(options);
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02001731
1732 /* For snapshot=on, create a temporary qcow2 overlay. bs points to the
1733 * temporary snapshot afterwards. */
1734 if (snapshot_flags) {
Max Reitz66836182016-05-17 16:41:27 +02001735 BlockDriverState *snapshot_bs;
1736 snapshot_bs = bdrv_append_temp_snapshot(bs, snapshot_flags,
1737 snapshot_options, &local_err);
Kevin Wolf73176be2016-03-07 13:02:15 +01001738 snapshot_options = NULL;
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02001739 if (local_err) {
1740 goto close_and_fail;
1741 }
Max Reitz5b363932016-05-17 16:41:31 +02001742 /* We are not going to return bs but the overlay on top of it
1743 * (snapshot_bs); thus, we have to drop the strong reference to bs
1744 * (which we obtained by calling bdrv_new()). bs will not be deleted,
1745 * though, because the overlay still has a reference to it. */
1746 bdrv_unref(bs);
1747 bs = snapshot_bs;
Max Reitz66836182016-05-17 16:41:27 +02001748 }
1749
Max Reitz5b363932016-05-17 16:41:31 +02001750 return bs;
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001751
Kevin Wolf8bfea152014-04-11 19:16:36 +02001752fail:
Kevin Wolff500a6d2012-11-12 17:35:27 +01001753 if (file != NULL) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02001754 bdrv_unref_child(bs, file);
Kevin Wolff500a6d2012-11-12 17:35:27 +01001755 }
Kevin Wolf73176be2016-03-07 13:02:15 +01001756 QDECREF(snapshot_options);
Kevin Wolf145f5982015-05-08 16:15:03 +02001757 QDECREF(bs->explicit_options);
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001758 QDECREF(bs->options);
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001759 QDECREF(options);
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001760 bs->options = NULL;
Max Reitz5b363932016-05-17 16:41:31 +02001761 bdrv_unref(bs);
Eduardo Habkost621ff942016-06-13 18:57:56 -03001762 error_propagate(errp, local_err);
Max Reitz5b363932016-05-17 16:41:31 +02001763 return NULL;
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001764
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001765close_and_fail:
Max Reitz5b363932016-05-17 16:41:31 +02001766 bdrv_unref(bs);
Kevin Wolf73176be2016-03-07 13:02:15 +01001767 QDECREF(snapshot_options);
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001768 QDECREF(options);
Eduardo Habkost621ff942016-06-13 18:57:56 -03001769 error_propagate(errp, local_err);
Max Reitz5b363932016-05-17 16:41:31 +02001770 return NULL;
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001771}
1772
Max Reitz5b363932016-05-17 16:41:31 +02001773BlockDriverState *bdrv_open(const char *filename, const char *reference,
1774 QDict *options, int flags, Error **errp)
Kevin Wolff3930ed2015-04-08 13:43:47 +02001775{
Max Reitz5b363932016-05-17 16:41:31 +02001776 return bdrv_open_inherit(filename, reference, options, flags, NULL,
Max Reitzce343772015-08-26 19:47:50 +02001777 NULL, errp);
Kevin Wolff3930ed2015-04-08 13:43:47 +02001778}
1779
Jeff Codye971aa12012-09-20 15:13:19 -04001780typedef struct BlockReopenQueueEntry {
1781 bool prepared;
1782 BDRVReopenState state;
1783 QSIMPLEQ_ENTRY(BlockReopenQueueEntry) entry;
1784} BlockReopenQueueEntry;
1785
1786/*
1787 * Adds a BlockDriverState to a simple queue for an atomic, transactional
1788 * reopen of multiple devices.
1789 *
1790 * bs_queue can either be an existing BlockReopenQueue that has had QSIMPLE_INIT
1791 * already performed, or alternatively may be NULL a new BlockReopenQueue will
1792 * be created and initialized. This newly created BlockReopenQueue should be
1793 * passed back in for subsequent calls that are intended to be of the same
1794 * atomic 'set'.
1795 *
1796 * bs is the BlockDriverState to add to the reopen queue.
1797 *
Kevin Wolf4d2cb092015-04-10 17:50:50 +02001798 * options contains the changed options for the associated bs
1799 * (the BlockReopenQueue takes ownership)
1800 *
Jeff Codye971aa12012-09-20 15:13:19 -04001801 * flags contains the open flags for the associated bs
1802 *
1803 * returns a pointer to bs_queue, which is either the newly allocated
1804 * bs_queue, or the existing bs_queue being used.
1805 *
1806 */
Kevin Wolf28518102015-05-08 17:07:31 +02001807static BlockReopenQueue *bdrv_reopen_queue_child(BlockReopenQueue *bs_queue,
1808 BlockDriverState *bs,
1809 QDict *options,
1810 int flags,
1811 const BdrvChildRole *role,
1812 QDict *parent_options,
1813 int parent_flags)
Jeff Codye971aa12012-09-20 15:13:19 -04001814{
1815 assert(bs != NULL);
1816
1817 BlockReopenQueueEntry *bs_entry;
Kevin Wolf67251a32015-04-09 18:54:04 +02001818 BdrvChild *child;
Kevin Wolf145f5982015-05-08 16:15:03 +02001819 QDict *old_options, *explicit_options;
Kevin Wolf67251a32015-04-09 18:54:04 +02001820
Jeff Codye971aa12012-09-20 15:13:19 -04001821 if (bs_queue == NULL) {
1822 bs_queue = g_new0(BlockReopenQueue, 1);
1823 QSIMPLEQ_INIT(bs_queue);
1824 }
1825
Kevin Wolf4d2cb092015-04-10 17:50:50 +02001826 if (!options) {
1827 options = qdict_new();
1828 }
1829
Kevin Wolf28518102015-05-08 17:07:31 +02001830 /*
1831 * Precedence of options:
1832 * 1. Explicitly passed in options (highest)
Kevin Wolf91a097e2015-05-08 17:49:53 +02001833 * 2. Set in flags (only for top level)
Kevin Wolf145f5982015-05-08 16:15:03 +02001834 * 3. Retained from explicitly set options of bs
Kevin Wolf8e2160e2015-04-29 17:29:39 +02001835 * 4. Inherited from parent node
Kevin Wolf28518102015-05-08 17:07:31 +02001836 * 5. Retained from effective options of bs
1837 */
1838
Kevin Wolf91a097e2015-05-08 17:49:53 +02001839 if (!parent_options) {
1840 /*
1841 * Any setting represented by flags is always updated. If the
1842 * corresponding QDict option is set, it takes precedence. Otherwise
1843 * the flag is translated into a QDict option. The old setting of bs is
1844 * not considered.
1845 */
1846 update_options_from_flags(options, flags);
1847 }
1848
Kevin Wolf145f5982015-05-08 16:15:03 +02001849 /* Old explicitly set values (don't overwrite by inherited value) */
1850 old_options = qdict_clone_shallow(bs->explicit_options);
1851 bdrv_join_options(bs, options, old_options);
1852 QDECREF(old_options);
1853
1854 explicit_options = qdict_clone_shallow(options);
1855
Kevin Wolf28518102015-05-08 17:07:31 +02001856 /* Inherit from parent node */
1857 if (parent_options) {
1858 assert(!flags);
Kevin Wolf8e2160e2015-04-29 17:29:39 +02001859 role->inherit_options(&flags, options, parent_flags, parent_options);
Kevin Wolf28518102015-05-08 17:07:31 +02001860 }
1861
1862 /* Old values are used for options that aren't set yet */
Kevin Wolf4d2cb092015-04-10 17:50:50 +02001863 old_options = qdict_clone_shallow(bs->options);
Kevin Wolfcddff5b2015-11-16 16:43:27 +01001864 bdrv_join_options(bs, options, old_options);
Kevin Wolf4d2cb092015-04-10 17:50:50 +02001865 QDECREF(old_options);
1866
Kevin Wolff1f25a22014-04-25 19:04:55 +02001867 /* bdrv_open() masks this flag out */
1868 flags &= ~BDRV_O_PROTOCOL;
1869
Kevin Wolf67251a32015-04-09 18:54:04 +02001870 QLIST_FOREACH(child, &bs->children, next) {
Kevin Wolf4c9dfe52015-05-08 15:14:15 +02001871 QDict *new_child_options;
1872 char *child_key_dot;
Kevin Wolf67251a32015-04-09 18:54:04 +02001873
Kevin Wolf4c9dfe52015-05-08 15:14:15 +02001874 /* reopen can only change the options of block devices that were
1875 * implicitly created and inherited options. For other (referenced)
1876 * block devices, a syntax like "backing.foo" results in an error. */
Kevin Wolf67251a32015-04-09 18:54:04 +02001877 if (child->bs->inherits_from != bs) {
1878 continue;
1879 }
1880
Kevin Wolf4c9dfe52015-05-08 15:14:15 +02001881 child_key_dot = g_strdup_printf("%s.", child->name);
1882 qdict_extract_subqdict(options, &new_child_options, child_key_dot);
1883 g_free(child_key_dot);
1884
Kevin Wolf28518102015-05-08 17:07:31 +02001885 bdrv_reopen_queue_child(bs_queue, child->bs, new_child_options, 0,
1886 child->role, options, flags);
Jeff Codye971aa12012-09-20 15:13:19 -04001887 }
1888
1889 bs_entry = g_new0(BlockReopenQueueEntry, 1);
1890 QSIMPLEQ_INSERT_TAIL(bs_queue, bs_entry, entry);
1891
1892 bs_entry->state.bs = bs;
Kevin Wolf4d2cb092015-04-10 17:50:50 +02001893 bs_entry->state.options = options;
Kevin Wolf145f5982015-05-08 16:15:03 +02001894 bs_entry->state.explicit_options = explicit_options;
Jeff Codye971aa12012-09-20 15:13:19 -04001895 bs_entry->state.flags = flags;
1896
1897 return bs_queue;
1898}
1899
Kevin Wolf28518102015-05-08 17:07:31 +02001900BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue,
1901 BlockDriverState *bs,
1902 QDict *options, int flags)
1903{
1904 return bdrv_reopen_queue_child(bs_queue, bs, options, flags,
1905 NULL, NULL, 0);
1906}
1907
Jeff Codye971aa12012-09-20 15:13:19 -04001908/*
1909 * Reopen multiple BlockDriverStates atomically & transactionally.
1910 *
1911 * The queue passed in (bs_queue) must have been built up previous
1912 * via bdrv_reopen_queue().
1913 *
1914 * Reopens all BDS specified in the queue, with the appropriate
1915 * flags. All devices are prepared for reopen, and failure of any
1916 * device will cause all device changes to be abandonded, and intermediate
1917 * data cleaned up.
1918 *
1919 * If all devices prepare successfully, then the changes are committed
1920 * to all devices.
1921 *
1922 */
1923int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp)
1924{
1925 int ret = -1;
1926 BlockReopenQueueEntry *bs_entry, *next;
1927 Error *local_err = NULL;
1928
1929 assert(bs_queue != NULL);
1930
1931 bdrv_drain_all();
1932
1933 QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) {
1934 if (bdrv_reopen_prepare(&bs_entry->state, bs_queue, &local_err)) {
1935 error_propagate(errp, local_err);
1936 goto cleanup;
1937 }
1938 bs_entry->prepared = true;
1939 }
1940
1941 /* If we reach this point, we have success and just need to apply the
1942 * changes
1943 */
1944 QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) {
1945 bdrv_reopen_commit(&bs_entry->state);
1946 }
1947
1948 ret = 0;
1949
1950cleanup:
1951 QSIMPLEQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) {
1952 if (ret && bs_entry->prepared) {
1953 bdrv_reopen_abort(&bs_entry->state);
Kevin Wolf145f5982015-05-08 16:15:03 +02001954 } else if (ret) {
1955 QDECREF(bs_entry->state.explicit_options);
Jeff Codye971aa12012-09-20 15:13:19 -04001956 }
Kevin Wolf4d2cb092015-04-10 17:50:50 +02001957 QDECREF(bs_entry->state.options);
Jeff Codye971aa12012-09-20 15:13:19 -04001958 g_free(bs_entry);
1959 }
1960 g_free(bs_queue);
1961 return ret;
1962}
1963
1964
1965/* Reopen a single BlockDriverState with the specified flags. */
1966int bdrv_reopen(BlockDriverState *bs, int bdrv_flags, Error **errp)
1967{
1968 int ret = -1;
1969 Error *local_err = NULL;
Kevin Wolf4d2cb092015-04-10 17:50:50 +02001970 BlockReopenQueue *queue = bdrv_reopen_queue(NULL, bs, NULL, bdrv_flags);
Jeff Codye971aa12012-09-20 15:13:19 -04001971
1972 ret = bdrv_reopen_multiple(queue, &local_err);
1973 if (local_err != NULL) {
1974 error_propagate(errp, local_err);
1975 }
1976 return ret;
1977}
1978
1979
1980/*
1981 * Prepares a BlockDriverState for reopen. All changes are staged in the
1982 * 'opaque' field of the BDRVReopenState, which is used and allocated by
1983 * the block driver layer .bdrv_reopen_prepare()
1984 *
1985 * bs is the BlockDriverState to reopen
1986 * flags are the new open flags
1987 * queue is the reopen queue
1988 *
1989 * Returns 0 on success, non-zero on error. On error errp will be set
1990 * as well.
1991 *
1992 * On failure, bdrv_reopen_abort() will be called to clean up any data.
1993 * It is the responsibility of the caller to then call the abort() or
1994 * commit() for any other BDS that have been left in a prepare() state
1995 *
1996 */
1997int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue,
1998 Error **errp)
1999{
2000 int ret = -1;
2001 Error *local_err = NULL;
2002 BlockDriver *drv;
Kevin Wolfccf9dc02015-05-08 17:24:56 +02002003 QemuOpts *opts;
2004 const char *value;
Jeff Codye971aa12012-09-20 15:13:19 -04002005
2006 assert(reopen_state != NULL);
2007 assert(reopen_state->bs->drv != NULL);
2008 drv = reopen_state->bs->drv;
2009
Kevin Wolfccf9dc02015-05-08 17:24:56 +02002010 /* Process generic block layer options */
2011 opts = qemu_opts_create(&bdrv_runtime_opts, NULL, 0, &error_abort);
2012 qemu_opts_absorb_qdict(opts, reopen_state->options, &local_err);
2013 if (local_err) {
2014 error_propagate(errp, local_err);
2015 ret = -EINVAL;
2016 goto error;
2017 }
2018
Kevin Wolf91a097e2015-05-08 17:49:53 +02002019 update_flags_from_options(&reopen_state->flags, opts);
2020
Kevin Wolfccf9dc02015-05-08 17:24:56 +02002021 /* node-name and driver must be unchanged. Put them back into the QDict, so
2022 * that they are checked at the end of this function. */
2023 value = qemu_opt_get(opts, "node-name");
2024 if (value) {
2025 qdict_put(reopen_state->options, "node-name", qstring_from_str(value));
2026 }
2027
2028 value = qemu_opt_get(opts, "driver");
2029 if (value) {
2030 qdict_put(reopen_state->options, "driver", qstring_from_str(value));
2031 }
2032
Jeff Codye971aa12012-09-20 15:13:19 -04002033 /* if we are to stay read-only, do not allow permission change
2034 * to r/w */
2035 if (!(reopen_state->bs->open_flags & BDRV_O_ALLOW_RDWR) &&
2036 reopen_state->flags & BDRV_O_RDWR) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03002037 error_setg(errp, "Node '%s' is read only",
2038 bdrv_get_device_or_node_name(reopen_state->bs));
Jeff Codye971aa12012-09-20 15:13:19 -04002039 goto error;
2040 }
2041
2042
2043 ret = bdrv_flush(reopen_state->bs);
2044 if (ret) {
Eric Blake455b0fd2015-11-10 23:51:20 -07002045 error_setg_errno(errp, -ret, "Error flushing drive");
Jeff Codye971aa12012-09-20 15:13:19 -04002046 goto error;
2047 }
2048
2049 if (drv->bdrv_reopen_prepare) {
2050 ret = drv->bdrv_reopen_prepare(reopen_state, queue, &local_err);
2051 if (ret) {
2052 if (local_err != NULL) {
2053 error_propagate(errp, local_err);
2054 } else {
Luiz Capitulinod8b68952013-06-10 11:29:27 -04002055 error_setg(errp, "failed while preparing to reopen image '%s'",
2056 reopen_state->bs->filename);
Jeff Codye971aa12012-09-20 15:13:19 -04002057 }
2058 goto error;
2059 }
2060 } else {
2061 /* It is currently mandatory to have a bdrv_reopen_prepare()
2062 * handler for each supported drv. */
Alberto Garcia81e5f782015-04-08 12:29:19 +03002063 error_setg(errp, "Block format '%s' used by node '%s' "
2064 "does not support reopening files", drv->format_name,
2065 bdrv_get_device_or_node_name(reopen_state->bs));
Jeff Codye971aa12012-09-20 15:13:19 -04002066 ret = -1;
2067 goto error;
2068 }
2069
Kevin Wolf4d2cb092015-04-10 17:50:50 +02002070 /* Options that are not handled are only okay if they are unchanged
2071 * compared to the old state. It is expected that some options are only
2072 * used for the initial open, but not reopen (e.g. filename) */
2073 if (qdict_size(reopen_state->options)) {
2074 const QDictEntry *entry = qdict_first(reopen_state->options);
2075
2076 do {
2077 QString *new_obj = qobject_to_qstring(entry->value);
2078 const char *new = qstring_get_str(new_obj);
2079 const char *old = qdict_get_try_str(reopen_state->bs->options,
2080 entry->key);
2081
2082 if (!old || strcmp(new, old)) {
2083 error_setg(errp, "Cannot change the option '%s'", entry->key);
2084 ret = -EINVAL;
2085 goto error;
2086 }
2087 } while ((entry = qdict_next(reopen_state->options, entry)));
2088 }
2089
Jeff Codye971aa12012-09-20 15:13:19 -04002090 ret = 0;
2091
2092error:
Kevin Wolfccf9dc02015-05-08 17:24:56 +02002093 qemu_opts_del(opts);
Jeff Codye971aa12012-09-20 15:13:19 -04002094 return ret;
2095}
2096
2097/*
2098 * Takes the staged changes for the reopen from bdrv_reopen_prepare(), and
2099 * makes them final by swapping the staging BlockDriverState contents into
2100 * the active BlockDriverState contents.
2101 */
2102void bdrv_reopen_commit(BDRVReopenState *reopen_state)
2103{
2104 BlockDriver *drv;
2105
2106 assert(reopen_state != NULL);
2107 drv = reopen_state->bs->drv;
2108 assert(drv != NULL);
2109
2110 /* If there are any driver level actions to take */
2111 if (drv->bdrv_reopen_commit) {
2112 drv->bdrv_reopen_commit(reopen_state);
2113 }
2114
2115 /* set BDS specific flags now */
Kevin Wolf145f5982015-05-08 16:15:03 +02002116 QDECREF(reopen_state->bs->explicit_options);
2117
2118 reopen_state->bs->explicit_options = reopen_state->explicit_options;
Jeff Codye971aa12012-09-20 15:13:19 -04002119 reopen_state->bs->open_flags = reopen_state->flags;
Jeff Codye971aa12012-09-20 15:13:19 -04002120 reopen_state->bs->read_only = !(reopen_state->flags & BDRV_O_RDWR);
Kevin Wolf355ef4a2013-12-11 20:14:09 +01002121
Kevin Wolf3baca892014-07-16 17:48:16 +02002122 bdrv_refresh_limits(reopen_state->bs, NULL);
Jeff Codye971aa12012-09-20 15:13:19 -04002123}
2124
2125/*
2126 * Abort the reopen, and delete and free the staged changes in
2127 * reopen_state
2128 */
2129void bdrv_reopen_abort(BDRVReopenState *reopen_state)
2130{
2131 BlockDriver *drv;
2132
2133 assert(reopen_state != NULL);
2134 drv = reopen_state->bs->drv;
2135 assert(drv != NULL);
2136
2137 if (drv->bdrv_reopen_abort) {
2138 drv->bdrv_reopen_abort(reopen_state);
2139 }
Kevin Wolf145f5982015-05-08 16:15:03 +02002140
2141 QDECREF(reopen_state->explicit_options);
Jeff Codye971aa12012-09-20 15:13:19 -04002142}
2143
2144
Max Reitz64dff522016-01-29 16:36:10 +01002145static void bdrv_close(BlockDriverState *bs)
bellardfc01f7e2003-06-30 10:03:06 +00002146{
Max Reitz33384422014-06-20 21:57:33 +02002147 BdrvAioNotifier *ban, *ban_next;
2148
Max Reitzca9bd242016-01-29 16:36:14 +01002149 assert(!bs->job);
Max Reitz30f55fb2016-05-17 16:41:32 +02002150 assert(!bs->refcnt);
Alberto Garcia99b7e772015-09-25 16:41:44 +03002151
Paolo Bonzinifc272912015-12-23 11:48:24 +01002152 bdrv_drained_begin(bs); /* complete I/O */
Stefan Hajnoczi58fda172013-07-02 15:36:25 +02002153 bdrv_flush(bs);
Fam Zheng53ec73e2015-05-29 18:53:14 +08002154 bdrv_drain(bs); /* in case flush left pending I/O */
Paolo Bonzinifc272912015-12-23 11:48:24 +01002155
Max Reitzc5acdc92016-01-29 16:36:01 +01002156 bdrv_release_named_dirty_bitmaps(bs);
2157 assert(QLIST_EMPTY(&bs->dirty_bitmaps));
2158
Paolo Bonzini3cbc0022012-10-19 11:36:48 +02002159 if (bs->drv) {
Kevin Wolf6e93e7c2015-04-08 13:49:41 +02002160 BdrvChild *child, *next;
2161
Kevin Wolf9a7dedb2015-06-16 10:58:20 +02002162 bs->drv->bdrv_close(bs);
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02002163 bs->drv = NULL;
Kevin Wolf9a7dedb2015-06-16 10:58:20 +02002164
Kevin Wolf5db15a52015-09-14 15:33:33 +02002165 bdrv_set_backing_hd(bs, NULL);
Kevin Wolf9a7dedb2015-06-16 10:58:20 +02002166
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02002167 if (bs->file != NULL) {
2168 bdrv_unref_child(bs, bs->file);
2169 bs->file = NULL;
2170 }
2171
Kevin Wolf6e93e7c2015-04-08 13:49:41 +02002172 QLIST_FOREACH_SAFE(child, &bs->children, next, next) {
Kevin Wolf33a60402015-06-15 13:51:04 +02002173 /* TODO Remove bdrv_unref() from drivers' close function and use
2174 * bdrv_unref_child() here */
Kevin Wolfbddcec32015-04-09 18:47:50 +02002175 if (child->bs->inherits_from == bs) {
2176 child->bs->inherits_from = NULL;
2177 }
Kevin Wolf33a60402015-06-15 13:51:04 +02002178 bdrv_detach_child(child);
Kevin Wolf6e93e7c2015-04-08 13:49:41 +02002179 }
2180
Anthony Liguori7267c092011-08-20 22:09:37 -05002181 g_free(bs->opaque);
bellardea2384d2004-08-01 21:59:26 +00002182 bs->opaque = NULL;
Stefan Hajnoczi53fec9d2011-11-28 16:08:47 +00002183 bs->copy_on_read = 0;
Paolo Bonzinia275fa42012-05-08 16:51:43 +02002184 bs->backing_file[0] = '\0';
2185 bs->backing_format[0] = '\0';
Paolo Bonzini64058752012-05-08 16:51:49 +02002186 bs->total_sectors = 0;
Eric Blake54115412016-06-23 16:37:26 -06002187 bs->encrypted = false;
2188 bs->valid_key = false;
2189 bs->sg = false;
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01002190 QDECREF(bs->options);
Kevin Wolf145f5982015-05-08 16:15:03 +02002191 QDECREF(bs->explicit_options);
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01002192 bs->options = NULL;
Max Reitz91af7012014-07-18 20:24:56 +02002193 QDECREF(bs->full_open_options);
2194 bs->full_open_options = NULL;
bellardb3380822004-03-14 21:38:54 +00002195 }
Zhi Yong Wu98f90db2011-11-08 13:00:14 +08002196
Max Reitz33384422014-06-20 21:57:33 +02002197 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) {
2198 g_free(ban);
2199 }
2200 QLIST_INIT(&bs->aio_notifiers);
Paolo Bonzinifc272912015-12-23 11:48:24 +01002201 bdrv_drained_end(bs);
bellardb3380822004-03-14 21:38:54 +00002202}
2203
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +09002204void bdrv_close_all(void)
2205{
Kevin Wolfa1a2af02016-04-08 18:26:37 +02002206 block_job_cancel_sync_all();
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +09002207
Max Reitzca9bd242016-01-29 16:36:14 +01002208 /* Drop references from requests still in flight, such as canceled block
2209 * jobs whose AIO context has not been polled yet */
2210 bdrv_drain_all();
Stefan Hajnoczied78cda2014-05-08 16:34:35 +02002211
Max Reitzca9bd242016-01-29 16:36:14 +01002212 blk_remove_all_bs();
2213 blockdev_close_all_bdrv_states();
2214
Kevin Wolfa1a2af02016-04-08 18:26:37 +02002215 assert(QTAILQ_EMPTY(&all_bdrv_states));
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +09002216}
2217
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002218static void change_parent_backing_link(BlockDriverState *from,
2219 BlockDriverState *to)
2220{
Max Reitz9bd910e2016-06-10 20:57:46 +02002221 BdrvChild *c, *next, *to_c;
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002222
2223 QLIST_FOREACH_SAFE(c, &from->parents, next_parent, next) {
Max Reitz9bd910e2016-06-10 20:57:46 +02002224 if (c->role == &child_backing) {
2225 /* @from is generally not allowed to be a backing file, except for
2226 * when @to is the overlay. In that case, @from may not be replaced
2227 * by @to as @to's backing node. */
2228 QLIST_FOREACH(to_c, &to->children, next) {
2229 if (to_c == c) {
2230 break;
2231 }
2232 }
2233 if (to_c) {
2234 continue;
2235 }
2236 }
2237
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002238 assert(c->role != &child_backing);
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002239 bdrv_ref(to);
Kevin Wolfe9740bc2016-05-23 15:52:26 +02002240 bdrv_replace_child(c, to);
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002241 bdrv_unref(from);
2242 }
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002243}
2244
Jeff Cody8802d1f2012-02-28 15:54:06 -05002245/*
2246 * Add new bs contents at the top of an image chain while the chain is
2247 * live, while keeping required fields on the top layer.
2248 *
2249 * This will modify the BlockDriverState fields, and swap contents
2250 * between bs_new and bs_top. Both bs_new and bs_top are modified.
2251 *
Markus Armbrusterbfb197e2014-10-07 13:59:11 +02002252 * bs_new must not be attached to a BlockBackend.
Jeff Codyf6801b82012-03-27 16:30:19 -04002253 *
Jeff Cody8802d1f2012-02-28 15:54:06 -05002254 * This function does not create any image files.
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002255 *
2256 * bdrv_append() takes ownership of a bs_new reference and unrefs it because
2257 * that's what the callers commonly need. bs_new will be referenced by the old
2258 * parents of bs_top after bdrv_append() returns. If the caller needs to keep a
2259 * reference of its own, it must call bdrv_ref().
Jeff Cody8802d1f2012-02-28 15:54:06 -05002260 */
2261void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top)
2262{
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002263 assert(!bdrv_requests_pending(bs_top));
2264 assert(!bdrv_requests_pending(bs_new));
Jeff Cody8802d1f2012-02-28 15:54:06 -05002265
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002266 bdrv_ref(bs_top);
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002267
Kevin Wolf27ccdd52016-03-21 12:56:44 +01002268 change_parent_backing_link(bs_top, bs_new);
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002269 bdrv_set_backing_hd(bs_new, bs_top);
2270 bdrv_unref(bs_top);
2271
2272 /* bs_new is now referenced by its new parents, we don't need the
2273 * additional reference any more. */
2274 bdrv_unref(bs_new);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002275}
2276
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02002277void bdrv_replace_in_backing_chain(BlockDriverState *old, BlockDriverState *new)
2278{
2279 assert(!bdrv_requests_pending(old));
2280 assert(!bdrv_requests_pending(new));
2281
2282 bdrv_ref(old);
2283
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02002284 change_parent_backing_link(old, new);
2285
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02002286 bdrv_unref(old);
2287}
2288
Fam Zheng4f6fd342013-08-23 09:14:47 +08002289static void bdrv_delete(BlockDriverState *bs)
bellardb3380822004-03-14 21:38:54 +00002290{
Paolo Bonzini3e914652012-03-30 13:17:11 +02002291 assert(!bs->job);
Fam Zheng3718d8a2014-05-23 21:29:43 +08002292 assert(bdrv_op_blocker_is_empty(bs));
Fam Zheng4f6fd342013-08-23 09:14:47 +08002293 assert(!bs->refcnt);
Markus Armbruster18846de2010-06-29 16:58:30 +02002294
Stefan Hajnoczie1b5c522013-06-27 15:32:26 +02002295 bdrv_close(bs);
2296
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01002297 /* remove from list, if necessary */
Kevin Wolf63eaaae2016-03-18 10:46:57 +01002298 if (bs->node_name[0] != '\0') {
2299 QTAILQ_REMOVE(&graph_bdrv_states, bs, node_list);
2300 }
Max Reitz2c1d04e2016-01-29 16:36:11 +01002301 QTAILQ_REMOVE(&all_bdrv_states, bs, bs_list);
2302
Anthony Liguori7267c092011-08-20 22:09:37 -05002303 g_free(bs);
bellardfc01f7e2003-06-30 10:03:06 +00002304}
2305
aliguorie97fc192009-04-21 23:11:50 +00002306/*
2307 * Run consistency checks on an image
2308 *
Kevin Wolfe076f332010-06-29 11:43:13 +02002309 * Returns 0 if the check could be completed (it doesn't mean that the image is
Stefan Weila1c72732011-04-28 17:20:38 +02002310 * free of errors) or -errno when an internal error occurred. The results of the
Kevin Wolfe076f332010-06-29 11:43:13 +02002311 * check are stored in res.
aliguorie97fc192009-04-21 23:11:50 +00002312 */
Kevin Wolf4534ff52012-05-11 16:07:02 +02002313int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix)
aliguorie97fc192009-04-21 23:11:50 +00002314{
Max Reitz908bcd52014-08-07 22:47:55 +02002315 if (bs->drv == NULL) {
2316 return -ENOMEDIUM;
2317 }
aliguorie97fc192009-04-21 23:11:50 +00002318 if (bs->drv->bdrv_check == NULL) {
2319 return -ENOTSUP;
2320 }
2321
Kevin Wolfe076f332010-06-29 11:43:13 +02002322 memset(res, 0, sizeof(*res));
Kevin Wolf4534ff52012-05-11 16:07:02 +02002323 return bs->drv->bdrv_check(bs, res, fix);
aliguorie97fc192009-04-21 23:11:50 +00002324}
2325
Kevin Wolf756e6732010-01-12 12:55:17 +01002326/*
2327 * Return values:
2328 * 0 - success
2329 * -EINVAL - backing format specified, but no file
2330 * -ENOSPC - can't update the backing file because no space is left in the
2331 * image file header
2332 * -ENOTSUP - format driver doesn't support changing the backing file
2333 */
2334int bdrv_change_backing_file(BlockDriverState *bs,
2335 const char *backing_file, const char *backing_fmt)
2336{
2337 BlockDriver *drv = bs->drv;
Paolo Bonzini469ef352012-04-12 14:01:02 +02002338 int ret;
Kevin Wolf756e6732010-01-12 12:55:17 +01002339
Paolo Bonzini5f377792012-04-12 14:01:01 +02002340 /* Backing file format doesn't make sense without a backing file */
2341 if (backing_fmt && !backing_file) {
2342 return -EINVAL;
2343 }
2344
Kevin Wolf756e6732010-01-12 12:55:17 +01002345 if (drv->bdrv_change_backing_file != NULL) {
Paolo Bonzini469ef352012-04-12 14:01:02 +02002346 ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
Kevin Wolf756e6732010-01-12 12:55:17 +01002347 } else {
Paolo Bonzini469ef352012-04-12 14:01:02 +02002348 ret = -ENOTSUP;
Kevin Wolf756e6732010-01-12 12:55:17 +01002349 }
Paolo Bonzini469ef352012-04-12 14:01:02 +02002350
2351 if (ret == 0) {
2352 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
2353 pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
2354 }
2355 return ret;
Kevin Wolf756e6732010-01-12 12:55:17 +01002356}
2357
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002358/*
2359 * Finds the image layer in the chain that has 'bs' as its backing file.
2360 *
2361 * active is the current topmost image.
2362 *
2363 * Returns NULL if bs is not found in active's image chain,
2364 * or if active == bs.
Jeff Cody4caf0fc2014-06-25 15:35:26 -04002365 *
2366 * Returns the bottommost base image if bs == NULL.
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002367 */
2368BlockDriverState *bdrv_find_overlay(BlockDriverState *active,
2369 BlockDriverState *bs)
2370{
Kevin Wolf760e0062015-06-17 14:55:21 +02002371 while (active && bs != backing_bs(active)) {
2372 active = backing_bs(active);
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002373 }
2374
Jeff Cody4caf0fc2014-06-25 15:35:26 -04002375 return active;
2376}
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002377
Jeff Cody4caf0fc2014-06-25 15:35:26 -04002378/* Given a BDS, searches for the base layer. */
2379BlockDriverState *bdrv_find_base(BlockDriverState *bs)
2380{
2381 return bdrv_find_overlay(bs, NULL);
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002382}
2383
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002384/*
2385 * Drops images above 'base' up to and including 'top', and sets the image
2386 * above 'top' to have base as its backing file.
2387 *
2388 * Requires that the overlay to 'top' is opened r/w, so that the backing file
2389 * information in 'bs' can be properly updated.
2390 *
2391 * E.g., this will convert the following chain:
2392 * bottom <- base <- intermediate <- top <- active
2393 *
2394 * to
2395 *
2396 * bottom <- base <- active
2397 *
2398 * It is allowed for bottom==base, in which case it converts:
2399 *
2400 * base <- intermediate <- top <- active
2401 *
2402 * to
2403 *
2404 * base <- active
2405 *
Jeff Cody54e26902014-06-25 15:40:10 -04002406 * If backing_file_str is non-NULL, it will be used when modifying top's
2407 * overlay image metadata.
2408 *
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002409 * Error conditions:
2410 * if active == top, that is considered an error
2411 *
2412 */
2413int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top,
Jeff Cody54e26902014-06-25 15:40:10 -04002414 BlockDriverState *base, const char *backing_file_str)
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002415{
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002416 BlockDriverState *new_top_bs = NULL;
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002417 int ret = -EIO;
2418
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002419 if (!top->drv || !base->drv) {
2420 goto exit;
2421 }
2422
2423 new_top_bs = bdrv_find_overlay(active, top);
2424
2425 if (new_top_bs == NULL) {
2426 /* we could not find the image above 'top', this is an error */
2427 goto exit;
2428 }
2429
Kevin Wolf760e0062015-06-17 14:55:21 +02002430 /* special case of new_top_bs->backing->bs already pointing to base - nothing
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002431 * to do, no intermediate images */
Kevin Wolf760e0062015-06-17 14:55:21 +02002432 if (backing_bs(new_top_bs) == base) {
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002433 ret = 0;
2434 goto exit;
2435 }
2436
Kevin Wolf5db15a52015-09-14 15:33:33 +02002437 /* Make sure that base is in the backing chain of top */
2438 if (!bdrv_chain_contains(top, base)) {
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002439 goto exit;
2440 }
2441
2442 /* success - we can delete the intermediate states, and link top->base */
Kevin Wolf5db15a52015-09-14 15:33:33 +02002443 backing_file_str = backing_file_str ? backing_file_str : base->filename;
Jeff Cody54e26902014-06-25 15:40:10 -04002444 ret = bdrv_change_backing_file(new_top_bs, backing_file_str,
Kevin Wolf5db15a52015-09-14 15:33:33 +02002445 base->drv ? base->drv->format_name : "");
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002446 if (ret) {
2447 goto exit;
2448 }
Kevin Wolf5db15a52015-09-14 15:33:33 +02002449 bdrv_set_backing_hd(new_top_bs, base);
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002450
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002451 ret = 0;
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002452exit:
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002453 return ret;
2454}
2455
bellard83f64092006-08-01 16:21:11 +00002456/**
bellard83f64092006-08-01 16:21:11 +00002457 * Truncate file to 'offset' bytes (needed only for file protocols)
2458 */
2459int bdrv_truncate(BlockDriverState *bs, int64_t offset)
2460{
2461 BlockDriver *drv = bs->drv;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01002462 int ret;
bellard83f64092006-08-01 16:21:11 +00002463 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002464 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +00002465 if (!drv->bdrv_truncate)
2466 return -ENOTSUP;
Naphtali Sprei59f26892009-10-26 16:25:16 +02002467 if (bs->read_only)
2468 return -EACCES;
Jeff Cody9c75e162014-06-25 16:55:30 -04002469
Stefan Hajnoczi51762282010-04-19 16:56:41 +01002470 ret = drv->bdrv_truncate(bs, offset);
2471 if (ret == 0) {
2472 ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
John Snowce1ffea2015-04-17 19:50:03 -04002473 bdrv_dirty_bitmap_truncate(bs);
Kevin Wolf5c8cab42016-02-24 15:13:35 +01002474 bdrv_parent_cb_resize(bs);
Stefan Hajnoczi51762282010-04-19 16:56:41 +01002475 }
2476 return ret;
bellard83f64092006-08-01 16:21:11 +00002477}
2478
2479/**
Fam Zheng4a1d5e12011-07-12 19:56:39 +08002480 * Length of a allocated file in bytes. Sparse files are counted by actual
2481 * allocated space. Return < 0 if error or unknown.
2482 */
2483int64_t bdrv_get_allocated_file_size(BlockDriverState *bs)
2484{
2485 BlockDriver *drv = bs->drv;
2486 if (!drv) {
2487 return -ENOMEDIUM;
2488 }
2489 if (drv->bdrv_get_allocated_file_size) {
2490 return drv->bdrv_get_allocated_file_size(bs);
2491 }
2492 if (bs->file) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02002493 return bdrv_get_allocated_file_size(bs->file->bs);
Fam Zheng4a1d5e12011-07-12 19:56:39 +08002494 }
2495 return -ENOTSUP;
2496}
2497
2498/**
Markus Armbruster65a9bb22014-06-26 13:23:17 +02002499 * Return number of sectors on success, -errno on error.
bellard83f64092006-08-01 16:21:11 +00002500 */
Markus Armbruster65a9bb22014-06-26 13:23:17 +02002501int64_t bdrv_nb_sectors(BlockDriverState *bs)
bellard83f64092006-08-01 16:21:11 +00002502{
2503 BlockDriver *drv = bs->drv;
Markus Armbruster65a9bb22014-06-26 13:23:17 +02002504
bellard83f64092006-08-01 16:21:11 +00002505 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002506 return -ENOMEDIUM;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01002507
Kevin Wolfb94a2612013-10-29 12:18:58 +01002508 if (drv->has_variable_length) {
2509 int ret = refresh_total_sectors(bs, bs->total_sectors);
2510 if (ret < 0) {
2511 return ret;
Stefan Hajnoczi46a4e4e2011-03-29 20:04:41 +01002512 }
bellard83f64092006-08-01 16:21:11 +00002513 }
Markus Armbruster65a9bb22014-06-26 13:23:17 +02002514 return bs->total_sectors;
2515}
2516
2517/**
2518 * Return length in bytes on success, -errno on error.
2519 * The length is always a multiple of BDRV_SECTOR_SIZE.
2520 */
2521int64_t bdrv_getlength(BlockDriverState *bs)
2522{
2523 int64_t ret = bdrv_nb_sectors(bs);
2524
Fam Zheng4a9c9ea2015-05-15 16:36:05 +08002525 ret = ret > INT64_MAX / BDRV_SECTOR_SIZE ? -EFBIG : ret;
Markus Armbruster65a9bb22014-06-26 13:23:17 +02002526 return ret < 0 ? ret : ret * BDRV_SECTOR_SIZE;
bellardfc01f7e2003-06-30 10:03:06 +00002527}
2528
bellard19cb3732006-08-19 11:45:59 +00002529/* return 0 as number of sectors if no device present or error */
ths96b8f132007-12-17 01:35:20 +00002530void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
bellardfc01f7e2003-06-30 10:03:06 +00002531{
Markus Armbruster65a9bb22014-06-26 13:23:17 +02002532 int64_t nb_sectors = bdrv_nb_sectors(bs);
2533
2534 *nb_sectors_ptr = nb_sectors < 0 ? 0 : nb_sectors;
bellardfc01f7e2003-06-30 10:03:06 +00002535}
bellardcf989512004-02-16 21:56:36 +00002536
Eric Blake54115412016-06-23 16:37:26 -06002537bool bdrv_is_read_only(BlockDriverState *bs)
bellardb3380822004-03-14 21:38:54 +00002538{
2539 return bs->read_only;
2540}
2541
Eric Blake54115412016-06-23 16:37:26 -06002542bool bdrv_is_sg(BlockDriverState *bs)
ths985a03b2007-12-24 16:10:43 +00002543{
2544 return bs->sg;
2545}
2546
Eric Blake54115412016-06-23 16:37:26 -06002547bool bdrv_is_encrypted(BlockDriverState *bs)
bellardea2384d2004-08-01 21:59:26 +00002548{
Kevin Wolf760e0062015-06-17 14:55:21 +02002549 if (bs->backing && bs->backing->bs->encrypted) {
Eric Blake54115412016-06-23 16:37:26 -06002550 return true;
Kevin Wolf760e0062015-06-17 14:55:21 +02002551 }
bellardea2384d2004-08-01 21:59:26 +00002552 return bs->encrypted;
2553}
2554
Eric Blake54115412016-06-23 16:37:26 -06002555bool bdrv_key_required(BlockDriverState *bs)
aliguoric0f4ce72009-03-05 23:01:01 +00002556{
Kevin Wolf760e0062015-06-17 14:55:21 +02002557 BdrvChild *backing = bs->backing;
aliguoric0f4ce72009-03-05 23:01:01 +00002558
Kevin Wolf760e0062015-06-17 14:55:21 +02002559 if (backing && backing->bs->encrypted && !backing->bs->valid_key) {
Eric Blake54115412016-06-23 16:37:26 -06002560 return true;
Kevin Wolf760e0062015-06-17 14:55:21 +02002561 }
aliguoric0f4ce72009-03-05 23:01:01 +00002562 return (bs->encrypted && !bs->valid_key);
2563}
2564
bellardea2384d2004-08-01 21:59:26 +00002565int bdrv_set_key(BlockDriverState *bs, const char *key)
2566{
2567 int ret;
Kevin Wolf760e0062015-06-17 14:55:21 +02002568 if (bs->backing && bs->backing->bs->encrypted) {
2569 ret = bdrv_set_key(bs->backing->bs, key);
bellardea2384d2004-08-01 21:59:26 +00002570 if (ret < 0)
2571 return ret;
2572 if (!bs->encrypted)
2573 return 0;
2574 }
Shahar Havivifd04a2a2010-03-06 00:26:13 +02002575 if (!bs->encrypted) {
2576 return -EINVAL;
2577 } else if (!bs->drv || !bs->drv->bdrv_set_key) {
2578 return -ENOMEDIUM;
2579 }
aliguoric0f4ce72009-03-05 23:01:01 +00002580 ret = bs->drv->bdrv_set_key(bs, key);
aliguoribb5fc202009-03-05 23:01:15 +00002581 if (ret < 0) {
Eric Blake54115412016-06-23 16:37:26 -06002582 bs->valid_key = false;
aliguoribb5fc202009-03-05 23:01:15 +00002583 } else if (!bs->valid_key) {
Kevin Wolf5c8cab42016-02-24 15:13:35 +01002584 /* call the change callback now, we skipped it on open */
Eric Blake54115412016-06-23 16:37:26 -06002585 bs->valid_key = true;
Kevin Wolf5c8cab42016-02-24 15:13:35 +01002586 bdrv_parent_cb_change_media(bs, true);
aliguoribb5fc202009-03-05 23:01:15 +00002587 }
aliguoric0f4ce72009-03-05 23:01:01 +00002588 return ret;
bellardea2384d2004-08-01 21:59:26 +00002589}
2590
Markus Armbruster4d2855a2015-01-29 10:37:00 +01002591/*
2592 * Provide an encryption key for @bs.
2593 * If @key is non-null:
2594 * If @bs is not encrypted, fail.
2595 * Else if the key is invalid, fail.
2596 * Else set @bs's key to @key, replacing the existing key, if any.
2597 * If @key is null:
2598 * If @bs is encrypted and still lacks a key, fail.
2599 * Else do nothing.
2600 * On failure, store an error object through @errp if non-null.
2601 */
2602void bdrv_add_key(BlockDriverState *bs, const char *key, Error **errp)
2603{
2604 if (key) {
2605 if (!bdrv_is_encrypted(bs)) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03002606 error_setg(errp, "Node '%s' is not encrypted",
2607 bdrv_get_device_or_node_name(bs));
Markus Armbruster4d2855a2015-01-29 10:37:00 +01002608 } else if (bdrv_set_key(bs, key) < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002609 error_setg(errp, QERR_INVALID_PASSWORD);
Markus Armbruster4d2855a2015-01-29 10:37:00 +01002610 }
2611 } else {
2612 if (bdrv_key_required(bs)) {
Markus Armbrusterb1ca6392015-01-29 10:37:01 +01002613 error_set(errp, ERROR_CLASS_DEVICE_ENCRYPTED,
2614 "'%s' (%s) is encrypted",
Alberto Garcia81e5f782015-04-08 12:29:19 +03002615 bdrv_get_device_or_node_name(bs),
Markus Armbruster4d2855a2015-01-29 10:37:00 +01002616 bdrv_get_encrypted_filename(bs));
2617 }
2618 }
2619}
2620
Markus Armbrusterf8d6bba2012-06-13 10:11:48 +02002621const char *bdrv_get_format_name(BlockDriverState *bs)
bellardea2384d2004-08-01 21:59:26 +00002622{
Markus Armbrusterf8d6bba2012-06-13 10:11:48 +02002623 return bs->drv ? bs->drv->format_name : NULL;
bellardea2384d2004-08-01 21:59:26 +00002624}
2625
Stefan Hajnocziada42402014-08-27 12:08:55 +01002626static int qsort_strcmp(const void *a, const void *b)
2627{
2628 return strcmp(a, b);
2629}
2630
ths5fafdf22007-09-16 21:08:06 +00002631void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
bellardea2384d2004-08-01 21:59:26 +00002632 void *opaque)
2633{
2634 BlockDriver *drv;
Jeff Codye855e4f2014-04-28 18:29:54 -04002635 int count = 0;
Stefan Hajnocziada42402014-08-27 12:08:55 +01002636 int i;
Jeff Codye855e4f2014-04-28 18:29:54 -04002637 const char **formats = NULL;
bellardea2384d2004-08-01 21:59:26 +00002638
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +01002639 QLIST_FOREACH(drv, &bdrv_drivers, list) {
Jeff Codye855e4f2014-04-28 18:29:54 -04002640 if (drv->format_name) {
2641 bool found = false;
2642 int i = count;
2643 while (formats && i && !found) {
2644 found = !strcmp(formats[--i], drv->format_name);
2645 }
2646
2647 if (!found) {
Markus Armbruster5839e532014-08-19 10:31:08 +02002648 formats = g_renew(const char *, formats, count + 1);
Jeff Codye855e4f2014-04-28 18:29:54 -04002649 formats[count++] = drv->format_name;
Jeff Codye855e4f2014-04-28 18:29:54 -04002650 }
2651 }
bellardea2384d2004-08-01 21:59:26 +00002652 }
Stefan Hajnocziada42402014-08-27 12:08:55 +01002653
2654 qsort(formats, count, sizeof(formats[0]), qsort_strcmp);
2655
2656 for (i = 0; i < count; i++) {
2657 it(opaque, formats[i]);
2658 }
2659
Jeff Codye855e4f2014-04-28 18:29:54 -04002660 g_free(formats);
bellardea2384d2004-08-01 21:59:26 +00002661}
2662
Benoît Canetdc364f42014-01-23 21:31:32 +01002663/* This function is to find a node in the bs graph */
2664BlockDriverState *bdrv_find_node(const char *node_name)
2665{
2666 BlockDriverState *bs;
2667
2668 assert(node_name);
2669
2670 QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) {
2671 if (!strcmp(node_name, bs->node_name)) {
2672 return bs;
2673 }
2674 }
2675 return NULL;
2676}
2677
Benoît Canetc13163f2014-01-23 21:31:34 +01002678/* Put this QMP function here so it can access the static graph_bdrv_states. */
Alberto Garciad5a8ee62015-04-17 14:52:43 +03002679BlockDeviceInfoList *bdrv_named_nodes_list(Error **errp)
Benoît Canetc13163f2014-01-23 21:31:34 +01002680{
2681 BlockDeviceInfoList *list, *entry;
2682 BlockDriverState *bs;
2683
2684 list = NULL;
2685 QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) {
Kevin Wolfc83f9fb2016-03-03 11:37:48 +01002686 BlockDeviceInfo *info = bdrv_block_device_info(NULL, bs, errp);
Alberto Garciad5a8ee62015-04-17 14:52:43 +03002687 if (!info) {
2688 qapi_free_BlockDeviceInfoList(list);
2689 return NULL;
2690 }
Benoît Canetc13163f2014-01-23 21:31:34 +01002691 entry = g_malloc0(sizeof(*entry));
Alberto Garciad5a8ee62015-04-17 14:52:43 +03002692 entry->value = info;
Benoît Canetc13163f2014-01-23 21:31:34 +01002693 entry->next = list;
2694 list = entry;
2695 }
2696
2697 return list;
2698}
2699
Benoît Canet12d3ba82014-01-23 21:31:35 +01002700BlockDriverState *bdrv_lookup_bs(const char *device,
2701 const char *node_name,
2702 Error **errp)
2703{
Markus Armbruster7f06d472014-10-07 13:59:12 +02002704 BlockBackend *blk;
2705 BlockDriverState *bs;
Benoît Canet12d3ba82014-01-23 21:31:35 +01002706
Benoît Canet12d3ba82014-01-23 21:31:35 +01002707 if (device) {
Markus Armbruster7f06d472014-10-07 13:59:12 +02002708 blk = blk_by_name(device);
Benoît Canet12d3ba82014-01-23 21:31:35 +01002709
Markus Armbruster7f06d472014-10-07 13:59:12 +02002710 if (blk) {
Alberto Garcia9f4ed6f2015-10-26 16:46:49 +02002711 bs = blk_bs(blk);
2712 if (!bs) {
Max Reitz5433c242015-10-19 17:53:29 +02002713 error_setg(errp, "Device '%s' has no medium", device);
Max Reitz5433c242015-10-19 17:53:29 +02002714 }
2715
Alberto Garcia9f4ed6f2015-10-26 16:46:49 +02002716 return bs;
Benoît Canet12d3ba82014-01-23 21:31:35 +01002717 }
Benoît Canet12d3ba82014-01-23 21:31:35 +01002718 }
2719
Benoît Canetdd67fa52014-02-12 17:15:06 +01002720 if (node_name) {
2721 bs = bdrv_find_node(node_name);
Benoît Canet12d3ba82014-01-23 21:31:35 +01002722
Benoît Canetdd67fa52014-02-12 17:15:06 +01002723 if (bs) {
2724 return bs;
2725 }
Benoît Canet12d3ba82014-01-23 21:31:35 +01002726 }
2727
Benoît Canetdd67fa52014-02-12 17:15:06 +01002728 error_setg(errp, "Cannot find device=%s nor node_name=%s",
2729 device ? device : "",
2730 node_name ? node_name : "");
2731 return NULL;
Benoît Canet12d3ba82014-01-23 21:31:35 +01002732}
2733
Jeff Cody5a6684d2014-06-25 15:40:09 -04002734/* If 'base' is in the same chain as 'top', return true. Otherwise,
2735 * return false. If either argument is NULL, return false. */
2736bool bdrv_chain_contains(BlockDriverState *top, BlockDriverState *base)
2737{
2738 while (top && top != base) {
Kevin Wolf760e0062015-06-17 14:55:21 +02002739 top = backing_bs(top);
Jeff Cody5a6684d2014-06-25 15:40:09 -04002740 }
2741
2742 return top != NULL;
2743}
2744
Fam Zheng04df7652014-10-31 11:32:54 +08002745BlockDriverState *bdrv_next_node(BlockDriverState *bs)
2746{
2747 if (!bs) {
2748 return QTAILQ_FIRST(&graph_bdrv_states);
2749 }
2750 return QTAILQ_NEXT(bs, node_list);
2751}
2752
Fam Zheng20a9e772014-10-31 11:32:55 +08002753const char *bdrv_get_node_name(const BlockDriverState *bs)
2754{
2755 return bs->node_name;
2756}
2757
Kevin Wolf1f0c4612016-03-22 18:38:44 +01002758const char *bdrv_get_parent_name(const BlockDriverState *bs)
Kevin Wolf4c265bf2016-02-26 10:22:16 +01002759{
2760 BdrvChild *c;
2761 const char *name;
2762
2763 /* If multiple parents have a name, just pick the first one. */
2764 QLIST_FOREACH(c, &bs->parents, next_parent) {
2765 if (c->role->get_name) {
2766 name = c->role->get_name(c);
2767 if (name && *name) {
2768 return name;
2769 }
2770 }
2771 }
2772
2773 return NULL;
2774}
2775
Markus Armbruster7f06d472014-10-07 13:59:12 +02002776/* TODO check what callers really want: bs->node_name or blk_name() */
Markus Armbrusterbfb197e2014-10-07 13:59:11 +02002777const char *bdrv_get_device_name(const BlockDriverState *bs)
bellardea2384d2004-08-01 21:59:26 +00002778{
Kevin Wolf4c265bf2016-02-26 10:22:16 +01002779 return bdrv_get_parent_name(bs) ?: "";
bellardea2384d2004-08-01 21:59:26 +00002780}
2781
Alberto Garcia9b2aa842015-04-08 12:29:18 +03002782/* This can be used to identify nodes that might not have a device
2783 * name associated. Since node and device names live in the same
2784 * namespace, the result is unambiguous. The exception is if both are
2785 * absent, then this returns an empty (non-null) string. */
2786const char *bdrv_get_device_or_node_name(const BlockDriverState *bs)
2787{
Kevin Wolf4c265bf2016-02-26 10:22:16 +01002788 return bdrv_get_parent_name(bs) ?: bs->node_name;
Alberto Garcia9b2aa842015-04-08 12:29:18 +03002789}
2790
Markus Armbrusterc8433282012-06-05 16:49:24 +02002791int bdrv_get_flags(BlockDriverState *bs)
2792{
2793 return bs->open_flags;
2794}
2795
Peter Lieven3ac21622013-06-28 12:47:42 +02002796int bdrv_has_zero_init_1(BlockDriverState *bs)
2797{
2798 return 1;
2799}
2800
Kevin Wolff2feebb2010-04-14 17:30:35 +02002801int bdrv_has_zero_init(BlockDriverState *bs)
2802{
2803 assert(bs->drv);
2804
Paolo Bonzini11212d82013-09-04 19:00:27 +02002805 /* If BS is a copy on write image, it is initialized to
2806 the contents of the base image, which may not be zeroes. */
Kevin Wolf760e0062015-06-17 14:55:21 +02002807 if (bs->backing) {
Paolo Bonzini11212d82013-09-04 19:00:27 +02002808 return 0;
2809 }
Kevin Wolf336c1c12010-07-28 11:26:29 +02002810 if (bs->drv->bdrv_has_zero_init) {
2811 return bs->drv->bdrv_has_zero_init(bs);
Kevin Wolff2feebb2010-04-14 17:30:35 +02002812 }
2813
Peter Lieven3ac21622013-06-28 12:47:42 +02002814 /* safe default */
2815 return 0;
Kevin Wolff2feebb2010-04-14 17:30:35 +02002816}
2817
Peter Lieven4ce78692013-10-24 12:06:54 +02002818bool bdrv_unallocated_blocks_are_zero(BlockDriverState *bs)
2819{
2820 BlockDriverInfo bdi;
2821
Kevin Wolf760e0062015-06-17 14:55:21 +02002822 if (bs->backing) {
Peter Lieven4ce78692013-10-24 12:06:54 +02002823 return false;
2824 }
2825
2826 if (bdrv_get_info(bs, &bdi) == 0) {
2827 return bdi.unallocated_blocks_are_zero;
2828 }
2829
2830 return false;
2831}
2832
2833bool bdrv_can_write_zeroes_with_unmap(BlockDriverState *bs)
2834{
2835 BlockDriverInfo bdi;
2836
Kevin Wolf760e0062015-06-17 14:55:21 +02002837 if (bs->backing || !(bs->open_flags & BDRV_O_UNMAP)) {
Peter Lieven4ce78692013-10-24 12:06:54 +02002838 return false;
2839 }
2840
2841 if (bdrv_get_info(bs, &bdi) == 0) {
2842 return bdi.can_write_zeroes_with_unmap;
2843 }
2844
2845 return false;
2846}
2847
aliguori045df332009-03-05 23:00:48 +00002848const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
2849{
Kevin Wolf760e0062015-06-17 14:55:21 +02002850 if (bs->backing && bs->backing->bs->encrypted)
aliguori045df332009-03-05 23:00:48 +00002851 return bs->backing_file;
2852 else if (bs->encrypted)
2853 return bs->filename;
2854 else
2855 return NULL;
2856}
2857
ths5fafdf22007-09-16 21:08:06 +00002858void bdrv_get_backing_filename(BlockDriverState *bs,
bellard83f64092006-08-01 16:21:11 +00002859 char *filename, int filename_size)
bellardea2384d2004-08-01 21:59:26 +00002860{
Kevin Wolf3574c602011-10-26 11:02:11 +02002861 pstrcpy(filename, filename_size, bs->backing_file);
bellardea2384d2004-08-01 21:59:26 +00002862}
2863
bellardfaea38e2006-08-05 21:31:00 +00002864int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
2865{
2866 BlockDriver *drv = bs->drv;
2867 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002868 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00002869 if (!drv->bdrv_get_info)
2870 return -ENOTSUP;
2871 memset(bdi, 0, sizeof(*bdi));
2872 return drv->bdrv_get_info(bs, bdi);
2873}
2874
Max Reitzeae041f2013-10-09 10:46:16 +02002875ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs)
2876{
2877 BlockDriver *drv = bs->drv;
2878 if (drv && drv->bdrv_get_specific_info) {
2879 return drv->bdrv_get_specific_info(bs);
2880 }
2881 return NULL;
2882}
2883
Eric Blakea31939e2015-11-18 01:52:54 -07002884void bdrv_debug_event(BlockDriverState *bs, BlkdebugEvent event)
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01002885{
Kevin Wolfbf736fe2013-06-05 15:17:55 +02002886 if (!bs || !bs->drv || !bs->drv->bdrv_debug_event) {
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01002887 return;
2888 }
2889
Kevin Wolfbf736fe2013-06-05 15:17:55 +02002890 bs->drv->bdrv_debug_event(bs, event);
Kevin Wolf41c695c2012-12-06 14:32:58 +01002891}
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01002892
Kevin Wolf41c695c2012-12-06 14:32:58 +01002893int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event,
2894 const char *tag)
2895{
2896 while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02002897 bs = bs->file ? bs->file->bs : NULL;
Kevin Wolf41c695c2012-12-06 14:32:58 +01002898 }
2899
2900 if (bs && bs->drv && bs->drv->bdrv_debug_breakpoint) {
2901 return bs->drv->bdrv_debug_breakpoint(bs, event, tag);
2902 }
2903
2904 return -ENOTSUP;
2905}
2906
Fam Zheng4cc70e92013-11-20 10:01:54 +08002907int bdrv_debug_remove_breakpoint(BlockDriverState *bs, const char *tag)
2908{
2909 while (bs && bs->drv && !bs->drv->bdrv_debug_remove_breakpoint) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02002910 bs = bs->file ? bs->file->bs : NULL;
Fam Zheng4cc70e92013-11-20 10:01:54 +08002911 }
2912
2913 if (bs && bs->drv && bs->drv->bdrv_debug_remove_breakpoint) {
2914 return bs->drv->bdrv_debug_remove_breakpoint(bs, tag);
2915 }
2916
2917 return -ENOTSUP;
2918}
2919
Kevin Wolf41c695c2012-12-06 14:32:58 +01002920int bdrv_debug_resume(BlockDriverState *bs, const char *tag)
2921{
Max Reitz938789e2014-03-10 23:44:08 +01002922 while (bs && (!bs->drv || !bs->drv->bdrv_debug_resume)) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02002923 bs = bs->file ? bs->file->bs : NULL;
Kevin Wolf41c695c2012-12-06 14:32:58 +01002924 }
2925
2926 if (bs && bs->drv && bs->drv->bdrv_debug_resume) {
2927 return bs->drv->bdrv_debug_resume(bs, tag);
2928 }
2929
2930 return -ENOTSUP;
2931}
2932
2933bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag)
2934{
2935 while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02002936 bs = bs->file ? bs->file->bs : NULL;
Kevin Wolf41c695c2012-12-06 14:32:58 +01002937 }
2938
2939 if (bs && bs->drv && bs->drv->bdrv_debug_is_suspended) {
2940 return bs->drv->bdrv_debug_is_suspended(bs, tag);
2941 }
2942
2943 return false;
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01002944}
2945
Blue Swirl199630b2010-07-25 20:49:34 +00002946int bdrv_is_snapshot(BlockDriverState *bs)
2947{
2948 return !!(bs->open_flags & BDRV_O_SNAPSHOT);
2949}
2950
Jeff Codyb1b1d782012-10-16 15:49:09 -04002951/* backing_file can either be relative, or absolute, or a protocol. If it is
2952 * relative, it must be relative to the chain. So, passing in bs->filename
2953 * from a BDS as backing_file should not be done, as that may be relative to
2954 * the CWD rather than the chain. */
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00002955BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs,
2956 const char *backing_file)
2957{
Jeff Codyb1b1d782012-10-16 15:49:09 -04002958 char *filename_full = NULL;
2959 char *backing_file_full = NULL;
2960 char *filename_tmp = NULL;
2961 int is_protocol = 0;
2962 BlockDriverState *curr_bs = NULL;
2963 BlockDriverState *retval = NULL;
2964
2965 if (!bs || !bs->drv || !backing_file) {
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00002966 return NULL;
2967 }
2968
Jeff Codyb1b1d782012-10-16 15:49:09 -04002969 filename_full = g_malloc(PATH_MAX);
2970 backing_file_full = g_malloc(PATH_MAX);
2971 filename_tmp = g_malloc(PATH_MAX);
2972
2973 is_protocol = path_has_protocol(backing_file);
2974
Kevin Wolf760e0062015-06-17 14:55:21 +02002975 for (curr_bs = bs; curr_bs->backing; curr_bs = curr_bs->backing->bs) {
Jeff Codyb1b1d782012-10-16 15:49:09 -04002976
2977 /* If either of the filename paths is actually a protocol, then
2978 * compare unmodified paths; otherwise make paths relative */
2979 if (is_protocol || path_has_protocol(curr_bs->backing_file)) {
2980 if (strcmp(backing_file, curr_bs->backing_file) == 0) {
Kevin Wolf760e0062015-06-17 14:55:21 +02002981 retval = curr_bs->backing->bs;
Jeff Codyb1b1d782012-10-16 15:49:09 -04002982 break;
2983 }
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00002984 } else {
Jeff Codyb1b1d782012-10-16 15:49:09 -04002985 /* If not an absolute filename path, make it relative to the current
2986 * image's filename path */
2987 path_combine(filename_tmp, PATH_MAX, curr_bs->filename,
2988 backing_file);
2989
2990 /* We are going to compare absolute pathnames */
2991 if (!realpath(filename_tmp, filename_full)) {
2992 continue;
2993 }
2994
2995 /* We need to make sure the backing filename we are comparing against
2996 * is relative to the current image filename (or absolute) */
2997 path_combine(filename_tmp, PATH_MAX, curr_bs->filename,
2998 curr_bs->backing_file);
2999
3000 if (!realpath(filename_tmp, backing_file_full)) {
3001 continue;
3002 }
3003
3004 if (strcmp(backing_file_full, filename_full) == 0) {
Kevin Wolf760e0062015-06-17 14:55:21 +02003005 retval = curr_bs->backing->bs;
Jeff Codyb1b1d782012-10-16 15:49:09 -04003006 break;
3007 }
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00003008 }
3009 }
3010
Jeff Codyb1b1d782012-10-16 15:49:09 -04003011 g_free(filename_full);
3012 g_free(backing_file_full);
3013 g_free(filename_tmp);
3014 return retval;
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00003015}
3016
Benoît Canetf198fd12012-08-02 10:22:47 +02003017int bdrv_get_backing_file_depth(BlockDriverState *bs)
3018{
3019 if (!bs->drv) {
3020 return 0;
3021 }
3022
Kevin Wolf760e0062015-06-17 14:55:21 +02003023 if (!bs->backing) {
Benoît Canetf198fd12012-08-02 10:22:47 +02003024 return 0;
3025 }
3026
Kevin Wolf760e0062015-06-17 14:55:21 +02003027 return 1 + bdrv_get_backing_file_depth(bs->backing->bs);
Benoît Canetf198fd12012-08-02 10:22:47 +02003028}
3029
bellardea2384d2004-08-01 21:59:26 +00003030void bdrv_init(void)
3031{
Anthony Liguori5efa9d52009-05-09 17:03:42 -05003032 module_call_init(MODULE_INIT_BLOCK);
bellardea2384d2004-08-01 21:59:26 +00003033}
pbrookce1a14d2006-08-07 02:38:06 +00003034
Markus Armbrustereb852012009-10-27 18:41:44 +01003035void bdrv_init_with_whitelist(void)
3036{
3037 use_bdrv_whitelist = 1;
3038 bdrv_init();
3039}
3040
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003041void bdrv_invalidate_cache(BlockDriverState *bs, Error **errp)
Anthony Liguori0f154232011-11-14 15:09:45 -06003042{
Fam Zheng0d1c5c92016-05-11 10:45:33 +08003043 BdrvChild *child;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003044 Error *local_err = NULL;
3045 int ret;
3046
Kevin Wolf3456a8d2014-03-11 10:58:39 +01003047 if (!bs->drv) {
3048 return;
Anthony Liguori0f154232011-11-14 15:09:45 -06003049 }
Kevin Wolf3456a8d2014-03-11 10:58:39 +01003050
Kevin Wolf04c01a52016-01-13 15:56:06 +01003051 if (!(bs->open_flags & BDRV_O_INACTIVE)) {
Alexey Kardashevskiy7ea2d262014-10-09 13:50:46 +11003052 return;
3053 }
Kevin Wolf04c01a52016-01-13 15:56:06 +01003054 bs->open_flags &= ~BDRV_O_INACTIVE;
Alexey Kardashevskiy7ea2d262014-10-09 13:50:46 +11003055
Kevin Wolf3456a8d2014-03-11 10:58:39 +01003056 if (bs->drv->bdrv_invalidate_cache) {
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003057 bs->drv->bdrv_invalidate_cache(bs, &local_err);
Fam Zheng0d1c5c92016-05-11 10:45:33 +08003058 if (local_err) {
3059 bs->open_flags |= BDRV_O_INACTIVE;
3060 error_propagate(errp, local_err);
3061 return;
3062 }
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003063 }
Fam Zheng0d1c5c92016-05-11 10:45:33 +08003064
3065 QLIST_FOREACH(child, &bs->children, next) {
3066 bdrv_invalidate_cache(child->bs, &local_err);
3067 if (local_err) {
3068 bs->open_flags |= BDRV_O_INACTIVE;
3069 error_propagate(errp, local_err);
3070 return;
3071 }
Kevin Wolf3456a8d2014-03-11 10:58:39 +01003072 }
3073
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003074 ret = refresh_total_sectors(bs, bs->total_sectors);
3075 if (ret < 0) {
Kevin Wolf04c01a52016-01-13 15:56:06 +01003076 bs->open_flags |= BDRV_O_INACTIVE;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003077 error_setg_errno(errp, -ret, "Could not refresh total sector count");
3078 return;
3079 }
Anthony Liguori0f154232011-11-14 15:09:45 -06003080}
3081
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003082void bdrv_invalidate_cache_all(Error **errp)
Anthony Liguori0f154232011-11-14 15:09:45 -06003083{
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003084 BlockDriverState *bs;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003085 Error *local_err = NULL;
Kevin Wolf88be7b42016-05-20 18:49:07 +02003086 BdrvNextIterator it;
Anthony Liguori0f154232011-11-14 15:09:45 -06003087
Kevin Wolf88be7b42016-05-20 18:49:07 +02003088 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Stefan Hajnoczied78cda2014-05-08 16:34:35 +02003089 AioContext *aio_context = bdrv_get_aio_context(bs);
3090
3091 aio_context_acquire(aio_context);
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003092 bdrv_invalidate_cache(bs, &local_err);
Stefan Hajnoczied78cda2014-05-08 16:34:35 +02003093 aio_context_release(aio_context);
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003094 if (local_err) {
3095 error_propagate(errp, local_err);
3096 return;
3097 }
Anthony Liguori0f154232011-11-14 15:09:45 -06003098 }
3099}
3100
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003101static int bdrv_inactivate_recurse(BlockDriverState *bs,
3102 bool setting_flag)
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003103{
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003104 BdrvChild *child;
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003105 int ret;
3106
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003107 if (!setting_flag && bs->drv->bdrv_inactivate) {
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003108 ret = bs->drv->bdrv_inactivate(bs);
3109 if (ret < 0) {
3110 return ret;
3111 }
3112 }
3113
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003114 QLIST_FOREACH(child, &bs->children, next) {
3115 ret = bdrv_inactivate_recurse(child->bs, setting_flag);
3116 if (ret < 0) {
3117 return ret;
3118 }
3119 }
3120
3121 if (setting_flag) {
3122 bs->open_flags |= BDRV_O_INACTIVE;
3123 }
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003124 return 0;
3125}
3126
3127int bdrv_inactivate_all(void)
3128{
Max Reitz79720af2016-03-16 19:54:44 +01003129 BlockDriverState *bs = NULL;
Kevin Wolf88be7b42016-05-20 18:49:07 +02003130 BdrvNextIterator it;
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003131 int ret = 0;
3132 int pass;
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003133
Kevin Wolf88be7b42016-05-20 18:49:07 +02003134 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003135 aio_context_acquire(bdrv_get_aio_context(bs));
3136 }
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003137
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003138 /* We do two passes of inactivation. The first pass calls to drivers'
3139 * .bdrv_inactivate callbacks recursively so all cache is flushed to disk;
3140 * the second pass sets the BDRV_O_INACTIVE flag so that no further write
3141 * is allowed. */
3142 for (pass = 0; pass < 2; pass++) {
Kevin Wolf88be7b42016-05-20 18:49:07 +02003143 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003144 ret = bdrv_inactivate_recurse(bs, pass);
3145 if (ret < 0) {
3146 goto out;
3147 }
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003148 }
3149 }
3150
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003151out:
Kevin Wolf88be7b42016-05-20 18:49:07 +02003152 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003153 aio_context_release(bdrv_get_aio_context(bs));
3154 }
3155
3156 return ret;
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003157}
3158
Kevin Wolff9f05dc2011-07-15 13:50:26 +02003159/**************************************************************/
bellard19cb3732006-08-19 11:45:59 +00003160/* removable device support */
3161
3162/**
3163 * Return TRUE if the media is present
3164 */
Max Reitze031f752015-10-19 17:53:11 +02003165bool bdrv_is_inserted(BlockDriverState *bs)
bellard19cb3732006-08-19 11:45:59 +00003166{
3167 BlockDriver *drv = bs->drv;
Max Reitz28d7a782015-10-19 17:53:13 +02003168 BdrvChild *child;
Markus Armbrustera1aff5b2011-09-06 18:58:41 +02003169
Max Reitze031f752015-10-19 17:53:11 +02003170 if (!drv) {
3171 return false;
3172 }
Max Reitz28d7a782015-10-19 17:53:13 +02003173 if (drv->bdrv_is_inserted) {
3174 return drv->bdrv_is_inserted(bs);
Max Reitze031f752015-10-19 17:53:11 +02003175 }
Max Reitz28d7a782015-10-19 17:53:13 +02003176 QLIST_FOREACH(child, &bs->children, next) {
3177 if (!bdrv_is_inserted(child->bs)) {
3178 return false;
3179 }
3180 }
3181 return true;
bellard19cb3732006-08-19 11:45:59 +00003182}
3183
3184/**
Markus Armbruster8e49ca42011-08-03 15:08:08 +02003185 * Return whether the media changed since the last call to this
3186 * function, or -ENOTSUP if we don't know. Most drivers don't know.
bellard19cb3732006-08-19 11:45:59 +00003187 */
3188int bdrv_media_changed(BlockDriverState *bs)
3189{
3190 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +00003191
Markus Armbruster8e49ca42011-08-03 15:08:08 +02003192 if (drv && drv->bdrv_media_changed) {
3193 return drv->bdrv_media_changed(bs);
3194 }
3195 return -ENOTSUP;
bellard19cb3732006-08-19 11:45:59 +00003196}
3197
3198/**
3199 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
3200 */
Luiz Capitulinof36f3942012-02-03 16:24:53 -02003201void bdrv_eject(BlockDriverState *bs, bool eject_flag)
bellard19cb3732006-08-19 11:45:59 +00003202{
3203 BlockDriver *drv = bs->drv;
Markus Armbrusterbfb197e2014-10-07 13:59:11 +02003204 const char *device_name;
bellard19cb3732006-08-19 11:45:59 +00003205
Markus Armbruster822e1cd2011-07-20 18:23:42 +02003206 if (drv && drv->bdrv_eject) {
3207 drv->bdrv_eject(bs, eject_flag);
bellard19cb3732006-08-19 11:45:59 +00003208 }
Luiz Capitulino6f382ed2012-02-14 13:41:13 -02003209
Markus Armbrusterbfb197e2014-10-07 13:59:11 +02003210 device_name = bdrv_get_device_name(bs);
3211 if (device_name[0] != '\0') {
3212 qapi_event_send_device_tray_moved(device_name,
Wenchao Xiaa5ee7bd2014-06-18 08:43:44 +02003213 eject_flag, &error_abort);
Luiz Capitulino6f382ed2012-02-14 13:41:13 -02003214 }
bellard19cb3732006-08-19 11:45:59 +00003215}
3216
bellard19cb3732006-08-19 11:45:59 +00003217/**
3218 * Lock or unlock the media (if it is locked, the user won't be able
3219 * to eject it manually).
3220 */
Markus Armbruster025e8492011-09-06 18:58:47 +02003221void bdrv_lock_medium(BlockDriverState *bs, bool locked)
bellard19cb3732006-08-19 11:45:59 +00003222{
3223 BlockDriver *drv = bs->drv;
3224
Markus Armbruster025e8492011-09-06 18:58:47 +02003225 trace_bdrv_lock_medium(bs, locked);
Stefan Hajnoczib8c6d092011-03-29 20:04:40 +01003226
Markus Armbruster025e8492011-09-06 18:58:47 +02003227 if (drv && drv->bdrv_lock_medium) {
3228 drv->bdrv_lock_medium(bs, locked);
bellard19cb3732006-08-19 11:45:59 +00003229 }
3230}
ths985a03b2007-12-24 16:10:43 +00003231
Fam Zheng9fcb0252013-08-23 09:14:46 +08003232/* Get a reference to bs */
3233void bdrv_ref(BlockDriverState *bs)
3234{
3235 bs->refcnt++;
3236}
3237
3238/* Release a previously grabbed reference to bs.
3239 * If after releasing, reference count is zero, the BlockDriverState is
3240 * deleted. */
3241void bdrv_unref(BlockDriverState *bs)
3242{
Jeff Cody9a4d5ca2014-07-23 17:22:57 -04003243 if (!bs) {
3244 return;
3245 }
Fam Zheng9fcb0252013-08-23 09:14:46 +08003246 assert(bs->refcnt > 0);
3247 if (--bs->refcnt == 0) {
3248 bdrv_delete(bs);
3249 }
3250}
3251
Fam Zhengfbe40ff2014-05-23 21:29:42 +08003252struct BdrvOpBlocker {
3253 Error *reason;
3254 QLIST_ENTRY(BdrvOpBlocker) list;
3255};
3256
3257bool bdrv_op_is_blocked(BlockDriverState *bs, BlockOpType op, Error **errp)
3258{
3259 BdrvOpBlocker *blocker;
3260 assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
3261 if (!QLIST_EMPTY(&bs->op_blockers[op])) {
3262 blocker = QLIST_FIRST(&bs->op_blockers[op]);
3263 if (errp) {
Markus Armbrustere43bfd92015-12-18 16:35:15 +01003264 *errp = error_copy(blocker->reason);
3265 error_prepend(errp, "Node '%s' is busy: ",
3266 bdrv_get_device_or_node_name(bs));
Fam Zhengfbe40ff2014-05-23 21:29:42 +08003267 }
3268 return true;
3269 }
3270 return false;
3271}
3272
3273void bdrv_op_block(BlockDriverState *bs, BlockOpType op, Error *reason)
3274{
3275 BdrvOpBlocker *blocker;
3276 assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
3277
Markus Armbruster5839e532014-08-19 10:31:08 +02003278 blocker = g_new0(BdrvOpBlocker, 1);
Fam Zhengfbe40ff2014-05-23 21:29:42 +08003279 blocker->reason = reason;
3280 QLIST_INSERT_HEAD(&bs->op_blockers[op], blocker, list);
3281}
3282
3283void bdrv_op_unblock(BlockDriverState *bs, BlockOpType op, Error *reason)
3284{
3285 BdrvOpBlocker *blocker, *next;
3286 assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
3287 QLIST_FOREACH_SAFE(blocker, &bs->op_blockers[op], list, next) {
3288 if (blocker->reason == reason) {
3289 QLIST_REMOVE(blocker, list);
3290 g_free(blocker);
3291 }
3292 }
3293}
3294
3295void bdrv_op_block_all(BlockDriverState *bs, Error *reason)
3296{
3297 int i;
3298 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
3299 bdrv_op_block(bs, i, reason);
3300 }
3301}
3302
3303void bdrv_op_unblock_all(BlockDriverState *bs, Error *reason)
3304{
3305 int i;
3306 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
3307 bdrv_op_unblock(bs, i, reason);
3308 }
3309}
3310
3311bool bdrv_op_blocker_is_empty(BlockDriverState *bs)
3312{
3313 int i;
3314
3315 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
3316 if (!QLIST_EMPTY(&bs->op_blockers[i])) {
3317 return false;
3318 }
3319 }
3320 return true;
3321}
3322
Luiz Capitulinod92ada22012-11-30 10:52:09 -02003323void bdrv_img_create(const char *filename, const char *fmt,
3324 const char *base_filename, const char *base_fmt,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003325 char *options, uint64_t img_size, int flags,
3326 Error **errp, bool quiet)
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003327{
Chunyan Liu83d05212014-06-05 17:20:51 +08003328 QemuOptsList *create_opts = NULL;
3329 QemuOpts *opts = NULL;
3330 const char *backing_fmt, *backing_file;
3331 int64_t size;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003332 BlockDriver *drv, *proto_drv;
Max Reitzcc84d902013-09-06 17:14:26 +02003333 Error *local_err = NULL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003334 int ret = 0;
3335
3336 /* Find driver and parse its options */
3337 drv = bdrv_find_format(fmt);
3338 if (!drv) {
Luiz Capitulino71c79812012-11-30 10:52:04 -02003339 error_setg(errp, "Unknown file format '%s'", fmt);
Luiz Capitulinod92ada22012-11-30 10:52:09 -02003340 return;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003341 }
3342
Max Reitzb65a5e12015-02-05 13:58:12 -05003343 proto_drv = bdrv_find_protocol(filename, true, errp);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003344 if (!proto_drv) {
Luiz Capitulinod92ada22012-11-30 10:52:09 -02003345 return;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003346 }
3347
Max Reitzc6149722014-12-02 18:32:45 +01003348 if (!drv->create_opts) {
3349 error_setg(errp, "Format driver '%s' does not support image creation",
3350 drv->format_name);
3351 return;
3352 }
3353
3354 if (!proto_drv->create_opts) {
3355 error_setg(errp, "Protocol driver '%s' does not support image creation",
3356 proto_drv->format_name);
3357 return;
3358 }
3359
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003360 create_opts = qemu_opts_append(create_opts, drv->create_opts);
3361 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003362
3363 /* Create parameter list with default values */
Chunyan Liu83d05212014-06-05 17:20:51 +08003364 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbruster39101f22015-02-12 16:46:36 +01003365 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003366
3367 /* Parse -o options */
3368 if (options) {
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003369 qemu_opts_do_parse(opts, options, NULL, &local_err);
3370 if (local_err) {
3371 error_report_err(local_err);
3372 local_err = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003373 error_setg(errp, "Invalid options for file format '%s'", fmt);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003374 goto out;
3375 }
3376 }
3377
3378 if (base_filename) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003379 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &local_err);
Markus Armbruster6be41942015-02-12 17:49:02 +01003380 if (local_err) {
Luiz Capitulino71c79812012-11-30 10:52:04 -02003381 error_setg(errp, "Backing file not supported for file format '%s'",
3382 fmt);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003383 goto out;
3384 }
3385 }
3386
3387 if (base_fmt) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003388 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &local_err);
Markus Armbruster6be41942015-02-12 17:49:02 +01003389 if (local_err) {
Luiz Capitulino71c79812012-11-30 10:52:04 -02003390 error_setg(errp, "Backing file format not supported for file "
3391 "format '%s'", fmt);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003392 goto out;
3393 }
3394 }
3395
Chunyan Liu83d05212014-06-05 17:20:51 +08003396 backing_file = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
3397 if (backing_file) {
3398 if (!strcmp(filename, backing_file)) {
Luiz Capitulino71c79812012-11-30 10:52:04 -02003399 error_setg(errp, "Error: Trying to create an image with the "
3400 "same filename as the backing file");
Jes Sorensen792da932010-12-16 13:52:17 +01003401 goto out;
3402 }
3403 }
3404
Chunyan Liu83d05212014-06-05 17:20:51 +08003405 backing_fmt = qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003406
3407 // The size for the image must always be specified, with one exception:
3408 // If we are using a backing file, we can obtain the size from there
Chunyan Liu83d05212014-06-05 17:20:51 +08003409 size = qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 0);
3410 if (size == -1) {
3411 if (backing_file) {
Max Reitz66f6b812013-12-03 14:57:52 +01003412 BlockDriverState *bs;
Max Reitz29168012014-11-26 17:20:27 +01003413 char *full_backing = g_new0(char, PATH_MAX);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003414 int64_t size;
Paolo Bonzini63090da2012-04-12 14:01:03 +02003415 int back_flags;
Max Reitze6641712015-08-26 19:47:48 +02003416 QDict *backing_options = NULL;
Paolo Bonzini63090da2012-04-12 14:01:03 +02003417
Max Reitz29168012014-11-26 17:20:27 +01003418 bdrv_get_full_backing_filename_from_filename(filename, backing_file,
3419 full_backing, PATH_MAX,
3420 &local_err);
3421 if (local_err) {
3422 g_free(full_backing);
3423 goto out;
3424 }
3425
Paolo Bonzini63090da2012-04-12 14:01:03 +02003426 /* backing files always opened read-only */
Kevin Wolf61de4c62016-03-18 17:46:45 +01003427 back_flags = flags;
Kevin Wolfbfd18d12016-03-04 14:28:01 +01003428 back_flags &= ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003429
Max Reitze6641712015-08-26 19:47:48 +02003430 if (backing_fmt) {
3431 backing_options = qdict_new();
3432 qdict_put(backing_options, "driver",
3433 qstring_from_str(backing_fmt));
3434 }
3435
Max Reitz5b363932016-05-17 16:41:31 +02003436 bs = bdrv_open(full_backing, NULL, backing_options, back_flags,
3437 &local_err);
Max Reitz29168012014-11-26 17:20:27 +01003438 g_free(full_backing);
Max Reitz5b363932016-05-17 16:41:31 +02003439 if (!bs) {
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003440 goto out;
3441 }
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003442 size = bdrv_getlength(bs);
3443 if (size < 0) {
3444 error_setg_errno(errp, -size, "Could not get size of '%s'",
3445 backing_file);
3446 bdrv_unref(bs);
3447 goto out;
3448 }
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003449
Markus Armbruster39101f22015-02-12 16:46:36 +01003450 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, size, &error_abort);
Max Reitz66f6b812013-12-03 14:57:52 +01003451
3452 bdrv_unref(bs);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003453 } else {
Luiz Capitulino71c79812012-11-30 10:52:04 -02003454 error_setg(errp, "Image creation needs a size parameter");
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003455 goto out;
3456 }
3457 }
3458
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003459 if (!quiet) {
Kővágó, Zoltánfe646692015-07-07 16:42:10 +02003460 printf("Formatting '%s', fmt=%s ", filename, fmt);
Fam Zheng43c5d8f2014-12-09 15:38:04 +08003461 qemu_opts_print(opts, " ");
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003462 puts("");
3463 }
Chunyan Liu83d05212014-06-05 17:20:51 +08003464
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003465 ret = bdrv_create(drv, filename, opts, &local_err);
Chunyan Liu83d05212014-06-05 17:20:51 +08003466
Max Reitzcc84d902013-09-06 17:14:26 +02003467 if (ret == -EFBIG) {
3468 /* This is generally a better message than whatever the driver would
3469 * deliver (especially because of the cluster_size_hint), since that
3470 * is most probably not much different from "image too large". */
3471 const char *cluster_size_hint = "";
Chunyan Liu83d05212014-06-05 17:20:51 +08003472 if (qemu_opt_get_size(opts, BLOCK_OPT_CLUSTER_SIZE, 0)) {
Max Reitzcc84d902013-09-06 17:14:26 +02003473 cluster_size_hint = " (try using a larger cluster size)";
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003474 }
Max Reitzcc84d902013-09-06 17:14:26 +02003475 error_setg(errp, "The image size is too large for file format '%s'"
3476 "%s", fmt, cluster_size_hint);
3477 error_free(local_err);
3478 local_err = NULL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003479 }
3480
3481out:
Chunyan Liu83d05212014-06-05 17:20:51 +08003482 qemu_opts_del(opts);
3483 qemu_opts_free(create_opts);
Eduardo Habkost621ff942016-06-13 18:57:56 -03003484 error_propagate(errp, local_err);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003485}
Stefan Hajnoczi85d126f2013-03-07 13:41:48 +01003486
3487AioContext *bdrv_get_aio_context(BlockDriverState *bs)
3488{
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003489 return bs->aio_context;
3490}
3491
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003492static void bdrv_do_remove_aio_context_notifier(BdrvAioNotifier *ban)
3493{
3494 QLIST_REMOVE(ban, list);
3495 g_free(ban);
3496}
3497
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003498void bdrv_detach_aio_context(BlockDriverState *bs)
3499{
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003500 BdrvAioNotifier *baf, *baf_tmp;
Max Reitzb97511c2016-05-17 13:38:04 +02003501 BdrvChild *child;
Max Reitz33384422014-06-20 21:57:33 +02003502
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003503 if (!bs->drv) {
3504 return;
3505 }
3506
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003507 assert(!bs->walking_aio_notifiers);
3508 bs->walking_aio_notifiers = true;
3509 QLIST_FOREACH_SAFE(baf, &bs->aio_notifiers, list, baf_tmp) {
3510 if (baf->deleted) {
3511 bdrv_do_remove_aio_context_notifier(baf);
3512 } else {
3513 baf->detach_aio_context(baf->opaque);
3514 }
Max Reitz33384422014-06-20 21:57:33 +02003515 }
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003516 /* Never mind iterating again to check for ->deleted. bdrv_close() will
3517 * remove remaining aio notifiers if we aren't called again.
3518 */
3519 bs->walking_aio_notifiers = false;
Max Reitz33384422014-06-20 21:57:33 +02003520
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003521 if (bs->drv->bdrv_detach_aio_context) {
3522 bs->drv->bdrv_detach_aio_context(bs);
3523 }
Max Reitzb97511c2016-05-17 13:38:04 +02003524 QLIST_FOREACH(child, &bs->children, next) {
3525 bdrv_detach_aio_context(child->bs);
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003526 }
3527
3528 bs->aio_context = NULL;
3529}
3530
3531void bdrv_attach_aio_context(BlockDriverState *bs,
3532 AioContext *new_context)
3533{
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003534 BdrvAioNotifier *ban, *ban_tmp;
Max Reitzb97511c2016-05-17 13:38:04 +02003535 BdrvChild *child;
Max Reitz33384422014-06-20 21:57:33 +02003536
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003537 if (!bs->drv) {
3538 return;
3539 }
3540
3541 bs->aio_context = new_context;
3542
Max Reitzb97511c2016-05-17 13:38:04 +02003543 QLIST_FOREACH(child, &bs->children, next) {
3544 bdrv_attach_aio_context(child->bs, new_context);
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003545 }
3546 if (bs->drv->bdrv_attach_aio_context) {
3547 bs->drv->bdrv_attach_aio_context(bs, new_context);
3548 }
Max Reitz33384422014-06-20 21:57:33 +02003549
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003550 assert(!bs->walking_aio_notifiers);
3551 bs->walking_aio_notifiers = true;
3552 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_tmp) {
3553 if (ban->deleted) {
3554 bdrv_do_remove_aio_context_notifier(ban);
3555 } else {
3556 ban->attached_aio_context(new_context, ban->opaque);
3557 }
Max Reitz33384422014-06-20 21:57:33 +02003558 }
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003559 bs->walking_aio_notifiers = false;
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003560}
3561
3562void bdrv_set_aio_context(BlockDriverState *bs, AioContext *new_context)
3563{
Fam Zheng53ec73e2015-05-29 18:53:14 +08003564 bdrv_drain(bs); /* ensure there are no in-flight requests */
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003565
3566 bdrv_detach_aio_context(bs);
3567
3568 /* This function executes in the old AioContext so acquire the new one in
3569 * case it runs in a different thread.
3570 */
3571 aio_context_acquire(new_context);
3572 bdrv_attach_aio_context(bs, new_context);
3573 aio_context_release(new_context);
Stefan Hajnoczi85d126f2013-03-07 13:41:48 +01003574}
Stefan Hajnoczid616b222013-06-24 17:13:10 +02003575
Max Reitz33384422014-06-20 21:57:33 +02003576void bdrv_add_aio_context_notifier(BlockDriverState *bs,
3577 void (*attached_aio_context)(AioContext *new_context, void *opaque),
3578 void (*detach_aio_context)(void *opaque), void *opaque)
3579{
3580 BdrvAioNotifier *ban = g_new(BdrvAioNotifier, 1);
3581 *ban = (BdrvAioNotifier){
3582 .attached_aio_context = attached_aio_context,
3583 .detach_aio_context = detach_aio_context,
3584 .opaque = opaque
3585 };
3586
3587 QLIST_INSERT_HEAD(&bs->aio_notifiers, ban, list);
3588}
3589
3590void bdrv_remove_aio_context_notifier(BlockDriverState *bs,
3591 void (*attached_aio_context)(AioContext *,
3592 void *),
3593 void (*detach_aio_context)(void *),
3594 void *opaque)
3595{
3596 BdrvAioNotifier *ban, *ban_next;
3597
3598 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) {
3599 if (ban->attached_aio_context == attached_aio_context &&
3600 ban->detach_aio_context == detach_aio_context &&
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003601 ban->opaque == opaque &&
3602 ban->deleted == false)
Max Reitz33384422014-06-20 21:57:33 +02003603 {
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003604 if (bs->walking_aio_notifiers) {
3605 ban->deleted = true;
3606 } else {
3607 bdrv_do_remove_aio_context_notifier(ban);
3608 }
Max Reitz33384422014-06-20 21:57:33 +02003609 return;
3610 }
3611 }
3612
3613 abort();
3614}
3615
Max Reitz77485432014-10-27 11:12:50 +01003616int bdrv_amend_options(BlockDriverState *bs, QemuOpts *opts,
Max Reitz8b139762015-07-27 17:51:32 +02003617 BlockDriverAmendStatusCB *status_cb, void *cb_opaque)
Max Reitz6f176b42013-09-03 10:09:50 +02003618{
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003619 if (!bs->drv->bdrv_amend_options) {
Max Reitz6f176b42013-09-03 10:09:50 +02003620 return -ENOTSUP;
3621 }
Max Reitz8b139762015-07-27 17:51:32 +02003622 return bs->drv->bdrv_amend_options(bs, opts, status_cb, cb_opaque);
Max Reitz6f176b42013-09-03 10:09:50 +02003623}
Benoît Canetf6186f42013-10-02 14:33:48 +02003624
Benoît Canetb5042a32014-03-03 19:11:34 +01003625/* This function will be called by the bdrv_recurse_is_first_non_filter method
3626 * of block filter and by bdrv_is_first_non_filter.
3627 * It is used to test if the given bs is the candidate or recurse more in the
3628 * node graph.
Benoît Canet212a5a82014-01-23 21:31:36 +01003629 */
Benoît Canet212a5a82014-01-23 21:31:36 +01003630bool bdrv_recurse_is_first_non_filter(BlockDriverState *bs,
3631 BlockDriverState *candidate)
Benoît Canetf6186f42013-10-02 14:33:48 +02003632{
Benoît Canetb5042a32014-03-03 19:11:34 +01003633 /* return false if basic checks fails */
3634 if (!bs || !bs->drv) {
3635 return false;
3636 }
3637
3638 /* the code reached a non block filter driver -> check if the bs is
3639 * the same as the candidate. It's the recursion termination condition.
3640 */
3641 if (!bs->drv->is_filter) {
3642 return bs == candidate;
3643 }
3644 /* Down this path the driver is a block filter driver */
3645
3646 /* If the block filter recursion method is defined use it to recurse down
3647 * the node graph.
3648 */
3649 if (bs->drv->bdrv_recurse_is_first_non_filter) {
Benoît Canet212a5a82014-01-23 21:31:36 +01003650 return bs->drv->bdrv_recurse_is_first_non_filter(bs, candidate);
3651 }
3652
Benoît Canetb5042a32014-03-03 19:11:34 +01003653 /* the driver is a block filter but don't allow to recurse -> return false
3654 */
3655 return false;
Benoît Canet212a5a82014-01-23 21:31:36 +01003656}
3657
3658/* This function checks if the candidate is the first non filter bs down it's
3659 * bs chain. Since we don't have pointers to parents it explore all bs chains
3660 * from the top. Some filters can choose not to pass down the recursion.
3661 */
3662bool bdrv_is_first_non_filter(BlockDriverState *candidate)
3663{
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003664 BlockDriverState *bs;
Kevin Wolf88be7b42016-05-20 18:49:07 +02003665 BdrvNextIterator it;
Benoît Canet212a5a82014-01-23 21:31:36 +01003666
3667 /* walk down the bs forest recursively */
Kevin Wolf88be7b42016-05-20 18:49:07 +02003668 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Benoît Canet212a5a82014-01-23 21:31:36 +01003669 bool perm;
3670
Benoît Canetb5042a32014-03-03 19:11:34 +01003671 /* try to recurse in this top level bs */
Kevin Wolfe6dc8a12014-02-04 11:45:31 +01003672 perm = bdrv_recurse_is_first_non_filter(bs, candidate);
Benoît Canet212a5a82014-01-23 21:31:36 +01003673
3674 /* candidate is the first non filter */
3675 if (perm) {
3676 return true;
3677 }
3678 }
3679
3680 return false;
Benoît Canetf6186f42013-10-02 14:33:48 +02003681}
Benoît Canet09158f02014-06-27 18:25:25 +02003682
Wen Congyange12f3782015-07-17 10:12:22 +08003683BlockDriverState *check_to_replace_node(BlockDriverState *parent_bs,
3684 const char *node_name, Error **errp)
Benoît Canet09158f02014-06-27 18:25:25 +02003685{
3686 BlockDriverState *to_replace_bs = bdrv_find_node(node_name);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003687 AioContext *aio_context;
3688
Benoît Canet09158f02014-06-27 18:25:25 +02003689 if (!to_replace_bs) {
3690 error_setg(errp, "Node name '%s' not found", node_name);
3691 return NULL;
3692 }
3693
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003694 aio_context = bdrv_get_aio_context(to_replace_bs);
3695 aio_context_acquire(aio_context);
3696
Benoît Canet09158f02014-06-27 18:25:25 +02003697 if (bdrv_op_is_blocked(to_replace_bs, BLOCK_OP_TYPE_REPLACE, errp)) {
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003698 to_replace_bs = NULL;
3699 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003700 }
3701
3702 /* We don't want arbitrary node of the BDS chain to be replaced only the top
3703 * most non filter in order to prevent data corruption.
3704 * Another benefit is that this tests exclude backing files which are
3705 * blocked by the backing blockers.
3706 */
Wen Congyange12f3782015-07-17 10:12:22 +08003707 if (!bdrv_recurse_is_first_non_filter(parent_bs, to_replace_bs)) {
Benoît Canet09158f02014-06-27 18:25:25 +02003708 error_setg(errp, "Only top most non filter can be replaced");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003709 to_replace_bs = NULL;
3710 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003711 }
3712
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003713out:
3714 aio_context_release(aio_context);
Benoît Canet09158f02014-06-27 18:25:25 +02003715 return to_replace_bs;
3716}
Ming Lei448ad912014-07-04 18:04:33 +08003717
Max Reitz91af7012014-07-18 20:24:56 +02003718static bool append_open_options(QDict *d, BlockDriverState *bs)
3719{
3720 const QDictEntry *entry;
Kevin Wolf9e700c12015-04-24 15:20:28 +02003721 QemuOptDesc *desc;
Kevin Wolf260fecf2015-04-27 13:46:22 +02003722 BdrvChild *child;
Max Reitz91af7012014-07-18 20:24:56 +02003723 bool found_any = false;
Kevin Wolf260fecf2015-04-27 13:46:22 +02003724 const char *p;
Max Reitz91af7012014-07-18 20:24:56 +02003725
3726 for (entry = qdict_first(bs->options); entry;
3727 entry = qdict_next(bs->options, entry))
3728 {
Kevin Wolf260fecf2015-04-27 13:46:22 +02003729 /* Exclude options for children */
3730 QLIST_FOREACH(child, &bs->children, next) {
3731 if (strstart(qdict_entry_key(entry), child->name, &p)
3732 && (!*p || *p == '.'))
3733 {
3734 break;
3735 }
3736 }
3737 if (child) {
Kevin Wolf9e700c12015-04-24 15:20:28 +02003738 continue;
Max Reitz91af7012014-07-18 20:24:56 +02003739 }
Kevin Wolf9e700c12015-04-24 15:20:28 +02003740
3741 /* And exclude all non-driver-specific options */
3742 for (desc = bdrv_runtime_opts.desc; desc->name; desc++) {
3743 if (!strcmp(qdict_entry_key(entry), desc->name)) {
3744 break;
3745 }
3746 }
3747 if (desc->name) {
3748 continue;
3749 }
3750
3751 qobject_incref(qdict_entry_value(entry));
3752 qdict_put_obj(d, qdict_entry_key(entry), qdict_entry_value(entry));
3753 found_any = true;
Max Reitz91af7012014-07-18 20:24:56 +02003754 }
3755
3756 return found_any;
3757}
3758
3759/* Updates the following BDS fields:
3760 * - exact_filename: A filename which may be used for opening a block device
3761 * which (mostly) equals the given BDS (even without any
3762 * other options; so reading and writing must return the same
3763 * results, but caching etc. may be different)
3764 * - full_open_options: Options which, when given when opening a block device
3765 * (without a filename), result in a BDS (mostly)
3766 * equalling the given one
3767 * - filename: If exact_filename is set, it is copied here. Otherwise,
3768 * full_open_options is converted to a JSON object, prefixed with
3769 * "json:" (for use through the JSON pseudo protocol) and put here.
3770 */
3771void bdrv_refresh_filename(BlockDriverState *bs)
3772{
3773 BlockDriver *drv = bs->drv;
3774 QDict *opts;
3775
3776 if (!drv) {
3777 return;
3778 }
3779
3780 /* This BDS's file name will most probably depend on its file's name, so
3781 * refresh that first */
3782 if (bs->file) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003783 bdrv_refresh_filename(bs->file->bs);
Max Reitz91af7012014-07-18 20:24:56 +02003784 }
3785
3786 if (drv->bdrv_refresh_filename) {
3787 /* Obsolete information is of no use here, so drop the old file name
3788 * information before refreshing it */
3789 bs->exact_filename[0] = '\0';
3790 if (bs->full_open_options) {
3791 QDECREF(bs->full_open_options);
3792 bs->full_open_options = NULL;
3793 }
3794
Kevin Wolf4cdd01d2015-04-27 13:50:54 +02003795 opts = qdict_new();
3796 append_open_options(opts, bs);
3797 drv->bdrv_refresh_filename(bs, opts);
3798 QDECREF(opts);
Max Reitz91af7012014-07-18 20:24:56 +02003799 } else if (bs->file) {
3800 /* Try to reconstruct valid information from the underlying file */
3801 bool has_open_options;
3802
3803 bs->exact_filename[0] = '\0';
3804 if (bs->full_open_options) {
3805 QDECREF(bs->full_open_options);
3806 bs->full_open_options = NULL;
3807 }
3808
3809 opts = qdict_new();
3810 has_open_options = append_open_options(opts, bs);
3811
3812 /* If no specific options have been given for this BDS, the filename of
3813 * the underlying file should suffice for this one as well */
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003814 if (bs->file->bs->exact_filename[0] && !has_open_options) {
3815 strcpy(bs->exact_filename, bs->file->bs->exact_filename);
Max Reitz91af7012014-07-18 20:24:56 +02003816 }
3817 /* Reconstructing the full options QDict is simple for most format block
3818 * drivers, as long as the full options are known for the underlying
3819 * file BDS. The full options QDict of that file BDS should somehow
3820 * contain a representation of the filename, therefore the following
3821 * suffices without querying the (exact_)filename of this BDS. */
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003822 if (bs->file->bs->full_open_options) {
Max Reitz91af7012014-07-18 20:24:56 +02003823 qdict_put_obj(opts, "driver",
3824 QOBJECT(qstring_from_str(drv->format_name)));
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003825 QINCREF(bs->file->bs->full_open_options);
3826 qdict_put_obj(opts, "file",
3827 QOBJECT(bs->file->bs->full_open_options));
Max Reitz91af7012014-07-18 20:24:56 +02003828
3829 bs->full_open_options = opts;
3830 } else {
3831 QDECREF(opts);
3832 }
3833 } else if (!bs->full_open_options && qdict_size(bs->options)) {
3834 /* There is no underlying file BDS (at least referenced by BDS.file),
3835 * so the full options QDict should be equal to the options given
3836 * specifically for this block device when it was opened (plus the
3837 * driver specification).
3838 * Because those options don't change, there is no need to update
3839 * full_open_options when it's already set. */
3840
3841 opts = qdict_new();
3842 append_open_options(opts, bs);
3843 qdict_put_obj(opts, "driver",
3844 QOBJECT(qstring_from_str(drv->format_name)));
3845
3846 if (bs->exact_filename[0]) {
3847 /* This may not work for all block protocol drivers (some may
3848 * require this filename to be parsed), but we have to find some
3849 * default solution here, so just include it. If some block driver
3850 * does not support pure options without any filename at all or
3851 * needs some special format of the options QDict, it needs to
3852 * implement the driver-specific bdrv_refresh_filename() function.
3853 */
3854 qdict_put_obj(opts, "filename",
3855 QOBJECT(qstring_from_str(bs->exact_filename)));
3856 }
3857
3858 bs->full_open_options = opts;
3859 }
3860
3861 if (bs->exact_filename[0]) {
3862 pstrcpy(bs->filename, sizeof(bs->filename), bs->exact_filename);
3863 } else if (bs->full_open_options) {
3864 QString *json = qobject_to_json(QOBJECT(bs->full_open_options));
3865 snprintf(bs->filename, sizeof(bs->filename), "json:%s",
3866 qstring_get_str(json));
3867 QDECREF(json);
3868 }
3869}
Wen Congyange06018a2016-05-10 15:36:37 +08003870
3871/*
3872 * Hot add/remove a BDS's child. So the user can take a child offline when
3873 * it is broken and take a new child online
3874 */
3875void bdrv_add_child(BlockDriverState *parent_bs, BlockDriverState *child_bs,
3876 Error **errp)
3877{
3878
3879 if (!parent_bs->drv || !parent_bs->drv->bdrv_add_child) {
3880 error_setg(errp, "The node %s does not support adding a child",
3881 bdrv_get_device_or_node_name(parent_bs));
3882 return;
3883 }
3884
3885 if (!QLIST_EMPTY(&child_bs->parents)) {
3886 error_setg(errp, "The node %s already has a parent",
3887 child_bs->node_name);
3888 return;
3889 }
3890
3891 parent_bs->drv->bdrv_add_child(parent_bs, child_bs, errp);
3892}
3893
3894void bdrv_del_child(BlockDriverState *parent_bs, BdrvChild *child, Error **errp)
3895{
3896 BdrvChild *tmp;
3897
3898 if (!parent_bs->drv || !parent_bs->drv->bdrv_del_child) {
3899 error_setg(errp, "The node %s does not support removing a child",
3900 bdrv_get_device_or_node_name(parent_bs));
3901 return;
3902 }
3903
3904 QLIST_FOREACH(tmp, &parent_bs->children, next) {
3905 if (tmp == child) {
3906 break;
3907 }
3908 }
3909
3910 if (!tmp) {
3911 error_setg(errp, "The node %s does not have a child named %s",
3912 bdrv_get_device_or_node_name(parent_bs),
3913 bdrv_get_device_or_node_name(child->bs));
3914 return;
3915 }
3916
3917 parent_bs->drv->bdrv_del_child(parent_bs, child, errp);
3918}