blob: d4485209516b9a9c93e2dd0daa93545cdd480338 [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"
Kevin Wolfcd7fca92016-07-06 11:22:39 +020028#include "block/nbd.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010029#include "qemu/error-report.h"
Marc Mari88d88792016-08-12 09:27:03 -040030#include "module_block.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010031#include "qemu/module.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010032#include "qapi/qmp/qerror.h"
Kevin Wolf91a097e2015-05-08 17:49:53 +020033#include "qapi/qmp/qbool.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010034#include "qapi/qmp/qjson.h"
Markus Armbrusterbfb197e2014-10-07 13:59:11 +020035#include "sysemu/block-backend.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010036#include "sysemu/sysemu.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010037#include "qemu/notify.h"
Daniel P. Berrange10817bf2015-09-01 14:48:02 +010038#include "qemu/coroutine.h"
Benoît Canetc13163f2014-01-23 21:31:34 +010039#include "block/qapi.h"
Luiz Capitulinob2023812011-09-21 17:16:47 -030040#include "qmp-commands.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010041#include "qemu/timer.h"
Wenchao Xiaa5ee7bd2014-06-18 08:43:44 +020042#include "qapi-event.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020043#include "qemu/cutils.h"
44#include "qemu/id.h"
bellardfc01f7e2003-06-30 10:03:06 +000045
Juan Quintela71e72a12009-07-27 16:12:56 +020046#ifdef CONFIG_BSD
bellard7674e7b2005-04-26 21:59:26 +000047#include <sys/ioctl.h>
Blue Swirl72cf2d42009-09-12 07:36:22 +000048#include <sys/queue.h>
blueswir1c5e97232009-03-07 20:06:23 +000049#ifndef __DragonFly__
bellard7674e7b2005-04-26 21:59:26 +000050#include <sys/disk.h>
51#endif
blueswir1c5e97232009-03-07 20:06:23 +000052#endif
bellard7674e7b2005-04-26 21:59:26 +000053
aliguori49dc7682009-03-08 16:26:59 +000054#ifdef _WIN32
55#include <windows.h>
56#endif
57
Stefan Hajnoczi1c9805a2011-10-13 13:08:22 +010058#define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
59
Benoît Canetdc364f42014-01-23 21:31:32 +010060static QTAILQ_HEAD(, BlockDriverState) graph_bdrv_states =
61 QTAILQ_HEAD_INITIALIZER(graph_bdrv_states);
62
Max Reitz2c1d04e2016-01-29 16:36:11 +010063static QTAILQ_HEAD(, BlockDriverState) all_bdrv_states =
64 QTAILQ_HEAD_INITIALIZER(all_bdrv_states);
65
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +010066static QLIST_HEAD(, BlockDriver) bdrv_drivers =
67 QLIST_HEAD_INITIALIZER(bdrv_drivers);
bellardea2384d2004-08-01 21:59:26 +000068
Max Reitz5b363932016-05-17 16:41:31 +020069static BlockDriverState *bdrv_open_inherit(const char *filename,
70 const char *reference,
71 QDict *options, int flags,
72 BlockDriverState *parent,
73 const BdrvChildRole *child_role,
74 Error **errp);
Kevin Wolff3930ed2015-04-08 13:43:47 +020075
Markus Armbrustereb852012009-10-27 18:41:44 +010076/* If non-zero, use only whitelisted block drivers */
77static int use_bdrv_whitelist;
78
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +000079#ifdef _WIN32
80static int is_windows_drive_prefix(const char *filename)
81{
82 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
83 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
84 filename[1] == ':');
85}
86
87int is_windows_drive(const char *filename)
88{
89 if (is_windows_drive_prefix(filename) &&
90 filename[2] == '\0')
91 return 1;
92 if (strstart(filename, "\\\\.\\", NULL) ||
93 strstart(filename, "//./", NULL))
94 return 1;
95 return 0;
96}
97#endif
98
Kevin Wolf339064d2013-11-28 10:23:32 +010099size_t bdrv_opt_mem_align(BlockDriverState *bs)
100{
101 if (!bs || !bs->drv) {
Denis V. Lunev459b4e62015-05-12 17:30:56 +0300102 /* page size or 4k (hdd sector size) should be on the safe side */
103 return MAX(4096, getpagesize());
Kevin Wolf339064d2013-11-28 10:23:32 +0100104 }
105
106 return bs->bl.opt_mem_alignment;
107}
108
Denis V. Lunev4196d2f2015-05-12 17:30:55 +0300109size_t bdrv_min_mem_align(BlockDriverState *bs)
110{
111 if (!bs || !bs->drv) {
Denis V. Lunev459b4e62015-05-12 17:30:56 +0300112 /* page size or 4k (hdd sector size) should be on the safe side */
113 return MAX(4096, getpagesize());
Denis V. Lunev4196d2f2015-05-12 17:30:55 +0300114 }
115
116 return bs->bl.min_mem_alignment;
117}
118
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000119/* check if the path starts with "<protocol>:" */
Max Reitz5c984152014-12-03 14:57:22 +0100120int path_has_protocol(const char *path)
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000121{
Paolo Bonzini947995c2012-05-08 16:51:48 +0200122 const char *p;
123
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000124#ifdef _WIN32
125 if (is_windows_drive(path) ||
126 is_windows_drive_prefix(path)) {
127 return 0;
128 }
Paolo Bonzini947995c2012-05-08 16:51:48 +0200129 p = path + strcspn(path, ":/\\");
130#else
131 p = path + strcspn(path, ":/");
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000132#endif
133
Paolo Bonzini947995c2012-05-08 16:51:48 +0200134 return *p == ':';
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000135}
136
bellard83f64092006-08-01 16:21:11 +0000137int path_is_absolute(const char *path)
138{
bellard21664422007-01-07 18:22:37 +0000139#ifdef _WIN32
140 /* specific case for names like: "\\.\d:" */
Paolo Bonzinif53f4da2012-05-08 16:51:47 +0200141 if (is_windows_drive(path) || is_windows_drive_prefix(path)) {
bellard21664422007-01-07 18:22:37 +0000142 return 1;
Paolo Bonzinif53f4da2012-05-08 16:51:47 +0200143 }
144 return (*path == '/' || *path == '\\');
bellard3b9f94e2007-01-07 17:27:07 +0000145#else
Paolo Bonzinif53f4da2012-05-08 16:51:47 +0200146 return (*path == '/');
bellard3b9f94e2007-01-07 17:27:07 +0000147#endif
bellard83f64092006-08-01 16:21:11 +0000148}
149
150/* if filename is absolute, just copy it to dest. Otherwise, build a
151 path to it by considering it is relative to base_path. URL are
152 supported. */
153void path_combine(char *dest, int dest_size,
154 const char *base_path,
155 const char *filename)
156{
157 const char *p, *p1;
158 int len;
159
160 if (dest_size <= 0)
161 return;
162 if (path_is_absolute(filename)) {
163 pstrcpy(dest, dest_size, filename);
164 } else {
165 p = strchr(base_path, ':');
166 if (p)
167 p++;
168 else
169 p = base_path;
bellard3b9f94e2007-01-07 17:27:07 +0000170 p1 = strrchr(base_path, '/');
171#ifdef _WIN32
172 {
173 const char *p2;
174 p2 = strrchr(base_path, '\\');
175 if (!p1 || p2 > p1)
176 p1 = p2;
177 }
178#endif
bellard83f64092006-08-01 16:21:11 +0000179 if (p1)
180 p1++;
181 else
182 p1 = base_path;
183 if (p1 > p)
184 p = p1;
185 len = p - base_path;
186 if (len > dest_size - 1)
187 len = dest_size - 1;
188 memcpy(dest, base_path, len);
189 dest[len] = '\0';
190 pstrcat(dest, dest_size, filename);
191 }
192}
193
Max Reitz0a828552014-11-26 17:20:25 +0100194void bdrv_get_full_backing_filename_from_filename(const char *backed,
195 const char *backing,
Max Reitz9f074292014-11-26 17:20:26 +0100196 char *dest, size_t sz,
197 Error **errp)
Max Reitz0a828552014-11-26 17:20:25 +0100198{
Max Reitz9f074292014-11-26 17:20:26 +0100199 if (backing[0] == '\0' || path_has_protocol(backing) ||
200 path_is_absolute(backing))
201 {
Max Reitz0a828552014-11-26 17:20:25 +0100202 pstrcpy(dest, sz, backing);
Max Reitz9f074292014-11-26 17:20:26 +0100203 } else if (backed[0] == '\0' || strstart(backed, "json:", NULL)) {
204 error_setg(errp, "Cannot use relative backing file names for '%s'",
205 backed);
Max Reitz0a828552014-11-26 17:20:25 +0100206 } else {
207 path_combine(dest, sz, backed, backing);
208 }
209}
210
Max Reitz9f074292014-11-26 17:20:26 +0100211void bdrv_get_full_backing_filename(BlockDriverState *bs, char *dest, size_t sz,
212 Error **errp)
Paolo Bonzinidc5a1372012-05-08 16:51:50 +0200213{
Max Reitz9f074292014-11-26 17:20:26 +0100214 char *backed = bs->exact_filename[0] ? bs->exact_filename : bs->filename;
215
216 bdrv_get_full_backing_filename_from_filename(backed, bs->backing_file,
217 dest, sz, errp);
Paolo Bonzinidc5a1372012-05-08 16:51:50 +0200218}
219
Stefan Hajnoczi0eb72172015-04-28 14:27:51 +0100220void bdrv_register(BlockDriver *bdrv)
221{
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100222 QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
bellardea2384d2004-08-01 21:59:26 +0000223}
bellardb3380822004-03-14 21:38:54 +0000224
Markus Armbrustere4e99862014-10-07 13:59:03 +0200225BlockDriverState *bdrv_new(void)
226{
227 BlockDriverState *bs;
228 int i;
229
Markus Armbruster5839e532014-08-19 10:31:08 +0200230 bs = g_new0(BlockDriverState, 1);
Fam Zhenge4654d22013-11-13 18:29:43 +0800231 QLIST_INIT(&bs->dirty_bitmaps);
Fam Zhengfbe40ff2014-05-23 21:29:42 +0800232 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
233 QLIST_INIT(&bs->op_blockers[i]);
234 }
Stefan Hajnoczid616b222013-06-24 17:13:10 +0200235 notifier_with_return_list_init(&bs->before_write_notifiers);
Fam Zheng9fcb0252013-08-23 09:14:46 +0800236 bs->refcnt = 1;
Stefan Hajnoczidcd04222014-05-08 16:34:37 +0200237 bs->aio_context = qemu_get_aio_context();
Paolo Bonzinid7d512f2012-08-23 11:20:36 +0200238
Evgeny Yakovlev3ff2f672016-07-18 22:39:52 +0300239 qemu_co_queue_init(&bs->flush_queue);
240
Max Reitz2c1d04e2016-01-29 16:36:11 +0100241 QTAILQ_INSERT_TAIL(&all_bdrv_states, bs, bs_list);
242
bellardb3380822004-03-14 21:38:54 +0000243 return bs;
244}
245
Marc Mari88d88792016-08-12 09:27:03 -0400246static BlockDriver *bdrv_do_find_format(const char *format_name)
bellardea2384d2004-08-01 21:59:26 +0000247{
248 BlockDriver *drv1;
Marc Mari88d88792016-08-12 09:27:03 -0400249
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100250 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
251 if (!strcmp(drv1->format_name, format_name)) {
bellardea2384d2004-08-01 21:59:26 +0000252 return drv1;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100253 }
bellardea2384d2004-08-01 21:59:26 +0000254 }
Marc Mari88d88792016-08-12 09:27:03 -0400255
bellardea2384d2004-08-01 21:59:26 +0000256 return NULL;
257}
258
Marc Mari88d88792016-08-12 09:27:03 -0400259BlockDriver *bdrv_find_format(const char *format_name)
260{
261 BlockDriver *drv1;
262 int i;
263
264 drv1 = bdrv_do_find_format(format_name);
265 if (drv1) {
266 return drv1;
267 }
268
269 /* The driver isn't registered, maybe we need to load a module */
270 for (i = 0; i < (int)ARRAY_SIZE(block_driver_modules); ++i) {
271 if (!strcmp(block_driver_modules[i].format_name, format_name)) {
272 block_module_load_one(block_driver_modules[i].library_name);
273 break;
274 }
275 }
276
277 return bdrv_do_find_format(format_name);
278}
279
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800280static int bdrv_is_whitelisted(BlockDriver *drv, bool read_only)
Markus Armbrustereb852012009-10-27 18:41:44 +0100281{
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800282 static const char *whitelist_rw[] = {
283 CONFIG_BDRV_RW_WHITELIST
284 };
285 static const char *whitelist_ro[] = {
286 CONFIG_BDRV_RO_WHITELIST
Markus Armbrustereb852012009-10-27 18:41:44 +0100287 };
288 const char **p;
289
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800290 if (!whitelist_rw[0] && !whitelist_ro[0]) {
Markus Armbrustereb852012009-10-27 18:41:44 +0100291 return 1; /* no whitelist, anything goes */
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800292 }
Markus Armbrustereb852012009-10-27 18:41:44 +0100293
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800294 for (p = whitelist_rw; *p; p++) {
Markus Armbrustereb852012009-10-27 18:41:44 +0100295 if (!strcmp(drv->format_name, *p)) {
296 return 1;
297 }
298 }
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800299 if (read_only) {
300 for (p = whitelist_ro; *p; p++) {
301 if (!strcmp(drv->format_name, *p)) {
302 return 1;
303 }
304 }
305 }
Markus Armbrustereb852012009-10-27 18:41:44 +0100306 return 0;
307}
308
Daniel P. Berrangee6ff69b2016-03-21 14:11:48 +0000309bool bdrv_uses_whitelist(void)
310{
311 return use_bdrv_whitelist;
312}
313
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800314typedef struct CreateCo {
315 BlockDriver *drv;
316 char *filename;
Chunyan Liu83d05212014-06-05 17:20:51 +0800317 QemuOpts *opts;
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800318 int ret;
Max Reitzcc84d902013-09-06 17:14:26 +0200319 Error *err;
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800320} CreateCo;
321
322static void coroutine_fn bdrv_create_co_entry(void *opaque)
323{
Max Reitzcc84d902013-09-06 17:14:26 +0200324 Error *local_err = NULL;
325 int ret;
326
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800327 CreateCo *cco = opaque;
328 assert(cco->drv);
329
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800330 ret = cco->drv->bdrv_create(cco->filename, cco->opts, &local_err);
Eduardo Habkost621ff942016-06-13 18:57:56 -0300331 error_propagate(&cco->err, local_err);
Max Reitzcc84d902013-09-06 17:14:26 +0200332 cco->ret = ret;
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800333}
334
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200335int bdrv_create(BlockDriver *drv, const char* filename,
Chunyan Liu83d05212014-06-05 17:20:51 +0800336 QemuOpts *opts, Error **errp)
bellardea2384d2004-08-01 21:59:26 +0000337{
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800338 int ret;
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200339
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800340 Coroutine *co;
341 CreateCo cco = {
342 .drv = drv,
343 .filename = g_strdup(filename),
Chunyan Liu83d05212014-06-05 17:20:51 +0800344 .opts = opts,
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800345 .ret = NOT_DONE,
Max Reitzcc84d902013-09-06 17:14:26 +0200346 .err = NULL,
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800347 };
348
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800349 if (!drv->bdrv_create) {
Max Reitzcc84d902013-09-06 17:14:26 +0200350 error_setg(errp, "Driver '%s' does not support image creation", drv->format_name);
Luiz Capitulino80168bf2012-10-17 16:45:25 -0300351 ret = -ENOTSUP;
352 goto out;
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800353 }
354
355 if (qemu_in_coroutine()) {
356 /* Fast-path if already in coroutine context */
357 bdrv_create_co_entry(&cco);
358 } else {
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200359 co = qemu_coroutine_create(bdrv_create_co_entry, &cco);
360 qemu_coroutine_enter(co);
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800361 while (cco.ret == NOT_DONE) {
Paolo Bonzinib47ec2c2014-07-07 15:18:01 +0200362 aio_poll(qemu_get_aio_context(), true);
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800363 }
364 }
365
366 ret = cco.ret;
Max Reitzcc84d902013-09-06 17:14:26 +0200367 if (ret < 0) {
Markus Armbruster84d18f02014-01-30 15:07:28 +0100368 if (cco.err) {
Max Reitzcc84d902013-09-06 17:14:26 +0200369 error_propagate(errp, cco.err);
370 } else {
371 error_setg_errno(errp, -ret, "Could not create image");
372 }
373 }
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800374
Luiz Capitulino80168bf2012-10-17 16:45:25 -0300375out:
376 g_free(cco.filename);
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800377 return ret;
bellardea2384d2004-08-01 21:59:26 +0000378}
379
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800380int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp)
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200381{
382 BlockDriver *drv;
Max Reitzcc84d902013-09-06 17:14:26 +0200383 Error *local_err = NULL;
384 int ret;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200385
Max Reitzb65a5e12015-02-05 13:58:12 -0500386 drv = bdrv_find_protocol(filename, true, errp);
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200387 if (drv == NULL) {
Stefan Hajnoczi16905d72010-11-30 15:14:14 +0000388 return -ENOENT;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200389 }
390
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800391 ret = bdrv_create(drv, filename, opts, &local_err);
Eduardo Habkost621ff942016-06-13 18:57:56 -0300392 error_propagate(errp, local_err);
Max Reitzcc84d902013-09-06 17:14:26 +0200393 return ret;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200394}
395
Ekaterina Tumanova892b7de2015-02-16 12:47:54 +0100396/**
397 * Try to get @bs's logical and physical block size.
398 * On success, store them in @bsz struct and return 0.
399 * On failure return -errno.
400 * @bs must not be empty.
401 */
402int bdrv_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz)
403{
404 BlockDriver *drv = bs->drv;
405
406 if (drv && drv->bdrv_probe_blocksizes) {
407 return drv->bdrv_probe_blocksizes(bs, bsz);
408 }
409
410 return -ENOTSUP;
411}
412
413/**
414 * Try to get @bs's geometry (cyls, heads, sectors).
415 * On success, store them in @geo struct and return 0.
416 * On failure return -errno.
417 * @bs must not be empty.
418 */
419int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo)
420{
421 BlockDriver *drv = bs->drv;
422
423 if (drv && drv->bdrv_probe_geometry) {
424 return drv->bdrv_probe_geometry(bs, geo);
425 }
426
427 return -ENOTSUP;
428}
429
Jim Meyeringeba25052012-05-28 09:27:54 +0200430/*
431 * Create a uniquely-named empty temporary file.
432 * Return 0 upon success, otherwise a negative errno value.
433 */
434int get_tmp_filename(char *filename, int size)
435{
bellardd5249392004-08-03 21:14:23 +0000436#ifdef _WIN32
bellard3b9f94e2007-01-07 17:27:07 +0000437 char temp_dir[MAX_PATH];
Jim Meyeringeba25052012-05-28 09:27:54 +0200438 /* GetTempFileName requires that its output buffer (4th param)
439 have length MAX_PATH or greater. */
440 assert(size >= MAX_PATH);
441 return (GetTempPath(MAX_PATH, temp_dir)
442 && GetTempFileName(temp_dir, "qem", 0, filename)
443 ? 0 : -GetLastError());
bellardd5249392004-08-03 21:14:23 +0000444#else
bellardea2384d2004-08-01 21:59:26 +0000445 int fd;
blueswir17ccfb2e2008-09-14 06:45:34 +0000446 const char *tmpdir;
aurel320badc1e2008-03-10 00:05:34 +0000447 tmpdir = getenv("TMPDIR");
Amit Shah69bef792014-02-26 15:12:37 +0530448 if (!tmpdir) {
449 tmpdir = "/var/tmp";
450 }
Jim Meyeringeba25052012-05-28 09:27:54 +0200451 if (snprintf(filename, size, "%s/vl.XXXXXX", tmpdir) >= size) {
452 return -EOVERFLOW;
453 }
bellardea2384d2004-08-01 21:59:26 +0000454 fd = mkstemp(filename);
Dunrong Huangfe235a02012-09-05 21:26:22 +0800455 if (fd < 0) {
456 return -errno;
457 }
458 if (close(fd) != 0) {
459 unlink(filename);
Jim Meyeringeba25052012-05-28 09:27:54 +0200460 return -errno;
461 }
462 return 0;
bellardd5249392004-08-03 21:14:23 +0000463#endif
Jim Meyeringeba25052012-05-28 09:27:54 +0200464}
bellardea2384d2004-08-01 21:59:26 +0000465
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200466/*
467 * Detect host devices. By convention, /dev/cdrom[N] is always
468 * recognized as a host CDROM.
469 */
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200470static BlockDriver *find_hdev_driver(const char *filename)
471{
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200472 int score_max = 0, score;
473 BlockDriver *drv = NULL, *d;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200474
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100475 QLIST_FOREACH(d, &bdrv_drivers, list) {
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200476 if (d->bdrv_probe_device) {
477 score = d->bdrv_probe_device(filename);
478 if (score > score_max) {
479 score_max = score;
480 drv = d;
481 }
482 }
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200483 }
484
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200485 return drv;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200486}
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200487
Marc Mari88d88792016-08-12 09:27:03 -0400488static BlockDriver *bdrv_do_find_protocol(const char *protocol)
489{
490 BlockDriver *drv1;
491
492 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
493 if (drv1->protocol_name && !strcmp(drv1->protocol_name, protocol)) {
494 return drv1;
495 }
496 }
497
498 return NULL;
499}
500
Kevin Wolf98289622013-07-10 15:47:39 +0200501BlockDriver *bdrv_find_protocol(const char *filename,
Max Reitzb65a5e12015-02-05 13:58:12 -0500502 bool allow_protocol_prefix,
503 Error **errp)
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200504{
505 BlockDriver *drv1;
506 char protocol[128];
507 int len;
508 const char *p;
Marc Mari88d88792016-08-12 09:27:03 -0400509 int i;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200510
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200511 /* TODO Drivers without bdrv_file_open must be specified explicitly */
512
Christoph Hellwig39508e72010-06-23 12:25:17 +0200513 /*
514 * XXX(hch): we really should not let host device detection
515 * override an explicit protocol specification, but moving this
516 * later breaks access to device names with colons in them.
517 * Thanks to the brain-dead persistent naming schemes on udev-
518 * based Linux systems those actually are quite common.
519 */
520 drv1 = find_hdev_driver(filename);
521 if (drv1) {
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200522 return drv1;
523 }
Christoph Hellwig39508e72010-06-23 12:25:17 +0200524
Kevin Wolf98289622013-07-10 15:47:39 +0200525 if (!path_has_protocol(filename) || !allow_protocol_prefix) {
Max Reitzef810432014-12-02 18:32:42 +0100526 return &bdrv_file;
Christoph Hellwig39508e72010-06-23 12:25:17 +0200527 }
Kevin Wolf98289622013-07-10 15:47:39 +0200528
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000529 p = strchr(filename, ':');
530 assert(p != NULL);
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200531 len = p - filename;
532 if (len > sizeof(protocol) - 1)
533 len = sizeof(protocol) - 1;
534 memcpy(protocol, filename, len);
535 protocol[len] = '\0';
Marc Mari88d88792016-08-12 09:27:03 -0400536
537 drv1 = bdrv_do_find_protocol(protocol);
538 if (drv1) {
539 return drv1;
540 }
541
542 for (i = 0; i < (int)ARRAY_SIZE(block_driver_modules); ++i) {
543 if (block_driver_modules[i].protocol_name &&
544 !strcmp(block_driver_modules[i].protocol_name, protocol)) {
545 block_module_load_one(block_driver_modules[i].library_name);
546 break;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200547 }
548 }
Max Reitzb65a5e12015-02-05 13:58:12 -0500549
Marc Mari88d88792016-08-12 09:27:03 -0400550 drv1 = bdrv_do_find_protocol(protocol);
551 if (!drv1) {
552 error_setg(errp, "Unknown protocol '%s'", protocol);
553 }
554 return drv1;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200555}
556
Markus Armbrusterc6684242014-11-20 16:27:10 +0100557/*
558 * Guess image format by probing its contents.
559 * This is not a good idea when your image is raw (CVE-2008-2004), but
560 * we do it anyway for backward compatibility.
561 *
562 * @buf contains the image's first @buf_size bytes.
Kevin Wolf7cddd372014-11-20 16:27:11 +0100563 * @buf_size is the buffer size in bytes (generally BLOCK_PROBE_BUF_SIZE,
564 * but can be smaller if the image file is smaller)
Markus Armbrusterc6684242014-11-20 16:27:10 +0100565 * @filename is its filename.
566 *
567 * For all block drivers, call the bdrv_probe() method to get its
568 * probing score.
569 * Return the first block driver with the highest probing score.
570 */
Kevin Wolf38f3ef52014-11-20 16:27:12 +0100571BlockDriver *bdrv_probe_all(const uint8_t *buf, int buf_size,
572 const char *filename)
Markus Armbrusterc6684242014-11-20 16:27:10 +0100573{
574 int score_max = 0, score;
575 BlockDriver *drv = NULL, *d;
576
577 QLIST_FOREACH(d, &bdrv_drivers, list) {
578 if (d->bdrv_probe) {
579 score = d->bdrv_probe(buf, buf_size, filename);
580 if (score > score_max) {
581 score_max = score;
582 drv = d;
583 }
584 }
585 }
586
587 return drv;
588}
589
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200590static int find_image_format(BdrvChild *file, const char *filename,
Max Reitz34b5d2c2013-09-05 14:45:29 +0200591 BlockDriver **pdrv, Error **errp)
bellardea2384d2004-08-01 21:59:26 +0000592{
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200593 BlockDriverState *bs = file->bs;
Markus Armbrusterc6684242014-11-20 16:27:10 +0100594 BlockDriver *drv;
Kevin Wolf7cddd372014-11-20 16:27:11 +0100595 uint8_t buf[BLOCK_PROBE_BUF_SIZE];
Kevin Wolff500a6d2012-11-12 17:35:27 +0100596 int ret = 0;
Nicholas Bellingerf8ea0b02010-05-17 09:45:57 -0700597
Kevin Wolf08a00552010-06-01 18:37:31 +0200598 /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
Dimitris Aragiorgisb192af82015-06-23 13:44:56 +0300599 if (bdrv_is_sg(bs) || !bdrv_is_inserted(bs) || bdrv_getlength(bs) == 0) {
Max Reitzef810432014-12-02 18:32:42 +0100600 *pdrv = &bdrv_raw;
Stefan Weilc98ac352010-07-21 21:51:51 +0200601 return ret;
Nicholas A. Bellinger1a396852010-05-27 08:56:28 -0700602 }
Nicholas Bellingerf8ea0b02010-05-17 09:45:57 -0700603
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200604 ret = bdrv_pread(file, 0, buf, sizeof(buf));
bellard83f64092006-08-01 16:21:11 +0000605 if (ret < 0) {
Max Reitz34b5d2c2013-09-05 14:45:29 +0200606 error_setg_errno(errp, -ret, "Could not read image for determining its "
607 "format");
Stefan Weilc98ac352010-07-21 21:51:51 +0200608 *pdrv = NULL;
609 return ret;
bellard83f64092006-08-01 16:21:11 +0000610 }
611
Markus Armbrusterc6684242014-11-20 16:27:10 +0100612 drv = bdrv_probe_all(buf, ret, filename);
Stefan Weilc98ac352010-07-21 21:51:51 +0200613 if (!drv) {
Max Reitz34b5d2c2013-09-05 14:45:29 +0200614 error_setg(errp, "Could not determine image format: No compatible "
615 "driver found");
Stefan Weilc98ac352010-07-21 21:51:51 +0200616 ret = -ENOENT;
617 }
618 *pdrv = drv;
619 return ret;
bellardea2384d2004-08-01 21:59:26 +0000620}
621
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100622/**
623 * Set the current 'total_sectors' value
Markus Armbruster65a9bb22014-06-26 13:23:17 +0200624 * Return 0 on success, -errno on error.
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100625 */
626static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
627{
628 BlockDriver *drv = bs->drv;
629
Nicholas Bellinger396759a2010-05-17 09:46:04 -0700630 /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
Dimitris Aragiorgisb192af82015-06-23 13:44:56 +0300631 if (bdrv_is_sg(bs))
Nicholas Bellinger396759a2010-05-17 09:46:04 -0700632 return 0;
633
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100634 /* query actual device if possible, otherwise just trust the hint */
635 if (drv->bdrv_getlength) {
636 int64_t length = drv->bdrv_getlength(bs);
637 if (length < 0) {
638 return length;
639 }
Fam Zheng7e382002013-11-06 19:48:06 +0800640 hint = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE);
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100641 }
642
643 bs->total_sectors = hint;
644 return 0;
645}
646
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100647/**
Kevin Wolfcddff5b2015-11-16 16:43:27 +0100648 * Combines a QDict of new block driver @options with any missing options taken
649 * from @old_options, so that leaving out an option defaults to its old value.
650 */
651static void bdrv_join_options(BlockDriverState *bs, QDict *options,
652 QDict *old_options)
653{
654 if (bs->drv && bs->drv->bdrv_join_options) {
655 bs->drv->bdrv_join_options(options, old_options);
656 } else {
657 qdict_join(options, old_options, false);
658 }
659}
660
661/**
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100662 * Set open flags for a given discard mode
663 *
664 * Return 0 on success, -1 if the discard mode was invalid.
665 */
666int bdrv_parse_discard_flags(const char *mode, int *flags)
667{
668 *flags &= ~BDRV_O_UNMAP;
669
670 if (!strcmp(mode, "off") || !strcmp(mode, "ignore")) {
671 /* do nothing */
672 } else if (!strcmp(mode, "on") || !strcmp(mode, "unmap")) {
673 *flags |= BDRV_O_UNMAP;
674 } else {
675 return -1;
676 }
677
678 return 0;
679}
680
681/**
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100682 * Set open flags for a given cache mode
683 *
684 * Return 0 on success, -1 if the cache mode was invalid.
685 */
Kevin Wolf53e8ae02016-03-18 15:36:58 +0100686int bdrv_parse_cache_mode(const char *mode, int *flags, bool *writethrough)
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100687{
688 *flags &= ~BDRV_O_CACHE_MASK;
689
690 if (!strcmp(mode, "off") || !strcmp(mode, "none")) {
Kevin Wolf53e8ae02016-03-18 15:36:58 +0100691 *writethrough = false;
692 *flags |= BDRV_O_NOCACHE;
Stefan Hajnoczi92196b22011-08-04 12:26:52 +0100693 } else if (!strcmp(mode, "directsync")) {
Kevin Wolf53e8ae02016-03-18 15:36:58 +0100694 *writethrough = true;
Stefan Hajnoczi92196b22011-08-04 12:26:52 +0100695 *flags |= BDRV_O_NOCACHE;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100696 } else if (!strcmp(mode, "writeback")) {
Kevin Wolf53e8ae02016-03-18 15:36:58 +0100697 *writethrough = false;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100698 } else if (!strcmp(mode, "unsafe")) {
Kevin Wolf53e8ae02016-03-18 15:36:58 +0100699 *writethrough = false;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100700 *flags |= BDRV_O_NO_FLUSH;
701 } else if (!strcmp(mode, "writethrough")) {
Kevin Wolf53e8ae02016-03-18 15:36:58 +0100702 *writethrough = true;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100703 } else {
704 return -1;
705 }
706
707 return 0;
708}
709
Kevin Wolf20018e12016-05-23 18:46:59 +0200710static void bdrv_child_cb_drained_begin(BdrvChild *child)
711{
712 BlockDriverState *bs = child->opaque;
713 bdrv_drained_begin(bs);
714}
715
716static void bdrv_child_cb_drained_end(BdrvChild *child)
717{
718 BlockDriverState *bs = child->opaque;
719 bdrv_drained_end(bs);
720}
721
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200722/*
Kevin Wolf73176be2016-03-07 13:02:15 +0100723 * Returns the options and flags that a temporary snapshot should get, based on
724 * the originally requested flags (the originally requested image will have
725 * flags like a backing file)
Kevin Wolfb1e6fc02014-05-06 12:11:42 +0200726 */
Kevin Wolf73176be2016-03-07 13:02:15 +0100727static void bdrv_temp_snapshot_options(int *child_flags, QDict *child_options,
728 int parent_flags, QDict *parent_options)
Kevin Wolfb1e6fc02014-05-06 12:11:42 +0200729{
Kevin Wolf73176be2016-03-07 13:02:15 +0100730 *child_flags = (parent_flags & ~BDRV_O_SNAPSHOT) | BDRV_O_TEMPORARY;
731
732 /* For temporary files, unconditional cache=unsafe is fine */
Kevin Wolf73176be2016-03-07 13:02:15 +0100733 qdict_set_default_str(child_options, BDRV_OPT_CACHE_DIRECT, "off");
734 qdict_set_default_str(child_options, BDRV_OPT_CACHE_NO_FLUSH, "on");
Kevin Wolf41869042016-06-16 12:59:30 +0200735
736 /* aio=native doesn't work for cache.direct=off, so disable it for the
737 * temporary snapshot */
738 *child_flags &= ~BDRV_O_NATIVE_AIO;
Kevin Wolfb1e6fc02014-05-06 12:11:42 +0200739}
740
741/*
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200742 * Returns the options and flags that bs->file should get if a protocol driver
743 * is expected, based on the given options and flags for the parent BDS
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200744 */
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200745static void bdrv_inherited_options(int *child_flags, QDict *child_options,
746 int parent_flags, QDict *parent_options)
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200747{
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200748 int flags = parent_flags;
749
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200750 /* Enable protocol handling, disable format probing for bs->file */
751 flags |= BDRV_O_PROTOCOL;
752
Kevin Wolf91a097e2015-05-08 17:49:53 +0200753 /* If the cache mode isn't explicitly set, inherit direct and no-flush from
754 * the parent. */
755 qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_DIRECT);
756 qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_NO_FLUSH);
757
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200758 /* Our block drivers take care to send flushes and respect unmap policy,
Kevin Wolf91a097e2015-05-08 17:49:53 +0200759 * so we can default to enable both on lower layers regardless of the
760 * corresponding parent options. */
Kevin Wolf91a097e2015-05-08 17:49:53 +0200761 flags |= BDRV_O_UNMAP;
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200762
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200763 /* Clear flags that only apply to the top layer */
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000764 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ |
765 BDRV_O_NO_IO);
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200766
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200767 *child_flags = flags;
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200768}
769
Kevin Wolff3930ed2015-04-08 13:43:47 +0200770const BdrvChildRole child_file = {
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200771 .inherit_options = bdrv_inherited_options,
Kevin Wolf20018e12016-05-23 18:46:59 +0200772 .drained_begin = bdrv_child_cb_drained_begin,
773 .drained_end = bdrv_child_cb_drained_end,
Kevin Wolff3930ed2015-04-08 13:43:47 +0200774};
775
776/*
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200777 * Returns the options and flags that bs->file should get if the use of formats
778 * (and not only protocols) is permitted for it, based on the given options and
779 * flags for the parent BDS
Kevin Wolff3930ed2015-04-08 13:43:47 +0200780 */
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200781static void bdrv_inherited_fmt_options(int *child_flags, QDict *child_options,
782 int parent_flags, QDict *parent_options)
Kevin Wolff3930ed2015-04-08 13:43:47 +0200783{
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200784 child_file.inherit_options(child_flags, child_options,
785 parent_flags, parent_options);
786
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000787 *child_flags &= ~(BDRV_O_PROTOCOL | BDRV_O_NO_IO);
Kevin Wolff3930ed2015-04-08 13:43:47 +0200788}
789
790const BdrvChildRole child_format = {
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200791 .inherit_options = bdrv_inherited_fmt_options,
Kevin Wolf20018e12016-05-23 18:46:59 +0200792 .drained_begin = bdrv_child_cb_drained_begin,
793 .drained_end = bdrv_child_cb_drained_end,
Kevin Wolff3930ed2015-04-08 13:43:47 +0200794};
795
Kevin Wolf317fc442014-04-25 13:27:34 +0200796/*
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200797 * Returns the options and flags that bs->backing should get, based on the
798 * given options and flags for the parent BDS
Kevin Wolf317fc442014-04-25 13:27:34 +0200799 */
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200800static void bdrv_backing_options(int *child_flags, QDict *child_options,
801 int parent_flags, QDict *parent_options)
Kevin Wolf317fc442014-04-25 13:27:34 +0200802{
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200803 int flags = parent_flags;
804
Kevin Wolfb8816a42016-03-04 14:52:32 +0100805 /* The cache mode is inherited unmodified for backing files; except WCE,
806 * which is only applied on the top level (BlockBackend) */
Kevin Wolf91a097e2015-05-08 17:49:53 +0200807 qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_DIRECT);
808 qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_NO_FLUSH);
809
Kevin Wolf317fc442014-04-25 13:27:34 +0200810 /* backing files always opened read-only */
811 flags &= ~(BDRV_O_RDWR | BDRV_O_COPY_ON_READ);
812
813 /* snapshot=on is handled on the top layer */
Kevin Wolf8bfea152014-04-11 19:16:36 +0200814 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_TEMPORARY);
Kevin Wolf317fc442014-04-25 13:27:34 +0200815
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200816 *child_flags = flags;
Kevin Wolf317fc442014-04-25 13:27:34 +0200817}
818
Kevin Wolff3930ed2015-04-08 13:43:47 +0200819static const BdrvChildRole child_backing = {
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200820 .inherit_options = bdrv_backing_options,
Kevin Wolf20018e12016-05-23 18:46:59 +0200821 .drained_begin = bdrv_child_cb_drained_begin,
822 .drained_end = bdrv_child_cb_drained_end,
Kevin Wolff3930ed2015-04-08 13:43:47 +0200823};
824
Kevin Wolf7b272452012-11-12 17:05:39 +0100825static int bdrv_open_flags(BlockDriverState *bs, int flags)
826{
Kevin Wolf61de4c62016-03-18 17:46:45 +0100827 int open_flags = flags;
Kevin Wolf7b272452012-11-12 17:05:39 +0100828
829 /*
830 * Clear flags that are internal to the block layer before opening the
831 * image.
832 */
Kevin Wolf20cca272014-06-04 14:33:27 +0200833 open_flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_PROTOCOL);
Kevin Wolf7b272452012-11-12 17:05:39 +0100834
835 /*
836 * Snapshots should be writable.
837 */
Kevin Wolf8bfea152014-04-11 19:16:36 +0200838 if (flags & BDRV_O_TEMPORARY) {
Kevin Wolf7b272452012-11-12 17:05:39 +0100839 open_flags |= BDRV_O_RDWR;
840 }
841
842 return open_flags;
843}
844
Kevin Wolf91a097e2015-05-08 17:49:53 +0200845static void update_flags_from_options(int *flags, QemuOpts *opts)
846{
847 *flags &= ~BDRV_O_CACHE_MASK;
848
Kevin Wolf91a097e2015-05-08 17:49:53 +0200849 assert(qemu_opt_find(opts, BDRV_OPT_CACHE_NO_FLUSH));
850 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, false)) {
851 *flags |= BDRV_O_NO_FLUSH;
852 }
853
854 assert(qemu_opt_find(opts, BDRV_OPT_CACHE_DIRECT));
855 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_DIRECT, false)) {
856 *flags |= BDRV_O_NOCACHE;
857 }
858}
859
860static void update_options_from_flags(QDict *options, int flags)
861{
Kevin Wolf91a097e2015-05-08 17:49:53 +0200862 if (!qdict_haskey(options, BDRV_OPT_CACHE_DIRECT)) {
863 qdict_put(options, BDRV_OPT_CACHE_DIRECT,
864 qbool_from_bool(flags & BDRV_O_NOCACHE));
865 }
866 if (!qdict_haskey(options, BDRV_OPT_CACHE_NO_FLUSH)) {
867 qdict_put(options, BDRV_OPT_CACHE_NO_FLUSH,
868 qbool_from_bool(flags & BDRV_O_NO_FLUSH));
869 }
870}
871
Kevin Wolf636ea372014-01-24 14:11:52 +0100872static void bdrv_assign_node_name(BlockDriverState *bs,
873 const char *node_name,
874 Error **errp)
Benoît Canet6913c0c2014-01-23 21:31:33 +0100875{
Jeff Cody15489c72015-10-12 19:36:50 -0400876 char *gen_node_name = NULL;
Benoît Canet6913c0c2014-01-23 21:31:33 +0100877
Jeff Cody15489c72015-10-12 19:36:50 -0400878 if (!node_name) {
879 node_name = gen_node_name = id_generate(ID_BLOCK);
880 } else if (!id_wellformed(node_name)) {
881 /*
882 * Check for empty string or invalid characters, but not if it is
883 * generated (generated names use characters not available to the user)
884 */
Kevin Wolf9aebf3b2014-09-25 09:54:02 +0200885 error_setg(errp, "Invalid node name");
Kevin Wolf636ea372014-01-24 14:11:52 +0100886 return;
Benoît Canet6913c0c2014-01-23 21:31:33 +0100887 }
888
Benoît Canet0c5e94e2014-02-12 17:15:07 +0100889 /* takes care of avoiding namespaces collisions */
Markus Armbruster7f06d472014-10-07 13:59:12 +0200890 if (blk_by_name(node_name)) {
Benoît Canet0c5e94e2014-02-12 17:15:07 +0100891 error_setg(errp, "node-name=%s is conflicting with a device id",
892 node_name);
Jeff Cody15489c72015-10-12 19:36:50 -0400893 goto out;
Benoît Canet0c5e94e2014-02-12 17:15:07 +0100894 }
895
Benoît Canet6913c0c2014-01-23 21:31:33 +0100896 /* takes care of avoiding duplicates node names */
897 if (bdrv_find_node(node_name)) {
898 error_setg(errp, "Duplicate node name");
Jeff Cody15489c72015-10-12 19:36:50 -0400899 goto out;
Benoît Canet6913c0c2014-01-23 21:31:33 +0100900 }
901
902 /* copy node name into the bs and insert it into the graph list */
903 pstrcpy(bs->node_name, sizeof(bs->node_name), node_name);
904 QTAILQ_INSERT_TAIL(&graph_bdrv_states, bs, node_list);
Jeff Cody15489c72015-10-12 19:36:50 -0400905out:
906 g_free(gen_node_name);
Benoît Canet6913c0c2014-01-23 21:31:33 +0100907}
908
Kevin Wolf18edf282015-04-07 17:12:56 +0200909static QemuOptsList bdrv_runtime_opts = {
910 .name = "bdrv_common",
911 .head = QTAILQ_HEAD_INITIALIZER(bdrv_runtime_opts.head),
912 .desc = {
913 {
914 .name = "node-name",
915 .type = QEMU_OPT_STRING,
916 .help = "Node name of the block device node",
917 },
Kevin Wolf62392eb2015-04-24 16:38:02 +0200918 {
919 .name = "driver",
920 .type = QEMU_OPT_STRING,
921 .help = "Block driver to use for the node",
922 },
Kevin Wolf91a097e2015-05-08 17:49:53 +0200923 {
Kevin Wolf91a097e2015-05-08 17:49:53 +0200924 .name = BDRV_OPT_CACHE_DIRECT,
925 .type = QEMU_OPT_BOOL,
926 .help = "Bypass software writeback cache on the host",
927 },
928 {
929 .name = BDRV_OPT_CACHE_NO_FLUSH,
930 .type = QEMU_OPT_BOOL,
931 .help = "Ignore flush requests",
932 },
Kevin Wolf18edf282015-04-07 17:12:56 +0200933 { /* end of list */ }
934 },
935};
936
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200937/*
Kevin Wolf57915332010-04-14 15:24:50 +0200938 * Common part for opening disk images and files
Kevin Wolfb6ad4912013-03-15 10:35:04 +0100939 *
940 * Removes all processed options from *options.
Kevin Wolf57915332010-04-14 15:24:50 +0200941 */
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200942static int bdrv_open_common(BlockDriverState *bs, BdrvChild *file,
Kevin Wolf82dc8b42016-01-11 19:07:50 +0100943 QDict *options, Error **errp)
Kevin Wolf57915332010-04-14 15:24:50 +0200944{
945 int ret, open_flags;
Kevin Wolf035fccd2013-04-09 14:34:19 +0200946 const char *filename;
Kevin Wolf62392eb2015-04-24 16:38:02 +0200947 const char *driver_name = NULL;
Benoît Canet6913c0c2014-01-23 21:31:33 +0100948 const char *node_name = NULL;
Kevin Wolf18edf282015-04-07 17:12:56 +0200949 QemuOpts *opts;
Kevin Wolf62392eb2015-04-24 16:38:02 +0200950 BlockDriver *drv;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200951 Error *local_err = NULL;
Kevin Wolf57915332010-04-14 15:24:50 +0200952
Paolo Bonzini64058752012-05-08 16:51:49 +0200953 assert(bs->file == NULL);
Kevin Wolf707ff822013-03-06 12:20:31 +0100954 assert(options != NULL && bs->options != options);
Kevin Wolf57915332010-04-14 15:24:50 +0200955
Kevin Wolf62392eb2015-04-24 16:38:02 +0200956 opts = qemu_opts_create(&bdrv_runtime_opts, NULL, 0, &error_abort);
957 qemu_opts_absorb_qdict(opts, options, &local_err);
958 if (local_err) {
959 error_propagate(errp, local_err);
960 ret = -EINVAL;
961 goto fail_opts;
962 }
963
964 driver_name = qemu_opt_get(opts, "driver");
965 drv = bdrv_find_format(driver_name);
966 assert(drv != NULL);
967
Kevin Wolf45673672013-04-22 17:48:40 +0200968 if (file != NULL) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200969 filename = file->bs->filename;
Kevin Wolf45673672013-04-22 17:48:40 +0200970 } else {
971 filename = qdict_get_try_str(options, "filename");
972 }
973
Kevin Wolf765003d2014-02-03 14:49:42 +0100974 if (drv->bdrv_needs_filename && !filename) {
975 error_setg(errp, "The '%s' block driver requires a file name",
976 drv->format_name);
Kevin Wolf18edf282015-04-07 17:12:56 +0200977 ret = -EINVAL;
978 goto fail_opts;
979 }
980
Kevin Wolf82dc8b42016-01-11 19:07:50 +0100981 trace_bdrv_open_common(bs, filename ?: "", bs->open_flags,
982 drv->format_name);
Kevin Wolf62392eb2015-04-24 16:38:02 +0200983
Kevin Wolf18edf282015-04-07 17:12:56 +0200984 node_name = qemu_opt_get(opts, "node-name");
Kevin Wolf636ea372014-01-24 14:11:52 +0100985 bdrv_assign_node_name(bs, node_name, &local_err);
Markus Armbruster0fb63952014-04-25 16:50:31 +0200986 if (local_err) {
Kevin Wolf636ea372014-01-24 14:11:52 +0100987 error_propagate(errp, local_err);
Kevin Wolf18edf282015-04-07 17:12:56 +0200988 ret = -EINVAL;
989 goto fail_opts;
Kevin Wolf5d186eb2013-03-27 17:28:18 +0100990 }
991
Kevin Wolf82dc8b42016-01-11 19:07:50 +0100992 bs->read_only = !(bs->open_flags & BDRV_O_RDWR);
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800993
994 if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, bs->read_only)) {
Kevin Wolf8f94a6e2013-10-10 11:45:55 +0200995 error_setg(errp,
996 !bs->read_only && bdrv_is_whitelisted(drv, true)
997 ? "Driver '%s' can only be used for read-only devices"
998 : "Driver '%s' is not whitelisted",
999 drv->format_name);
Kevin Wolf18edf282015-04-07 17:12:56 +02001000 ret = -ENOTSUP;
1001 goto fail_opts;
Fam Zhengb64ec4e2013-05-29 19:35:40 +08001002 }
Kevin Wolf57915332010-04-14 15:24:50 +02001003
Stefan Hajnoczi53fec9d2011-11-28 16:08:47 +00001004 assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */
Kevin Wolf82dc8b42016-01-11 19:07:50 +01001005 if (bs->open_flags & BDRV_O_COPY_ON_READ) {
Kevin Wolf0ebd24e2013-09-19 15:12:18 +02001006 if (!bs->read_only) {
1007 bdrv_enable_copy_on_read(bs);
1008 } else {
1009 error_setg(errp, "Can't use copy-on-read on read-only device");
Kevin Wolf18edf282015-04-07 17:12:56 +02001010 ret = -EINVAL;
1011 goto fail_opts;
Kevin Wolf0ebd24e2013-09-19 15:12:18 +02001012 }
Stefan Hajnoczi53fec9d2011-11-28 16:08:47 +00001013 }
1014
Kevin Wolfc2ad1b02013-03-18 16:40:51 +01001015 if (filename != NULL) {
1016 pstrcpy(bs->filename, sizeof(bs->filename), filename);
1017 } else {
1018 bs->filename[0] = '\0';
1019 }
Max Reitz91af7012014-07-18 20:24:56 +02001020 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), bs->filename);
Kevin Wolf57915332010-04-14 15:24:50 +02001021
Kevin Wolf57915332010-04-14 15:24:50 +02001022 bs->drv = drv;
Anthony Liguori7267c092011-08-20 22:09:37 -05001023 bs->opaque = g_malloc0(drv->instance_size);
Kevin Wolf57915332010-04-14 15:24:50 +02001024
Kevin Wolf91a097e2015-05-08 17:49:53 +02001025 /* Apply cache mode options */
1026 update_flags_from_options(&bs->open_flags, opts);
Kevin Wolf73ac4512016-03-14 15:46:03 +01001027
Kevin Wolf66f82ce2010-04-14 14:17:38 +02001028 /* Open the image, either directly or using a protocol */
Kevin Wolf82dc8b42016-01-11 19:07:50 +01001029 open_flags = bdrv_open_flags(bs, bs->open_flags);
Kevin Wolf66f82ce2010-04-14 14:17:38 +02001030 if (drv->bdrv_file_open) {
Kevin Wolf5d186eb2013-03-27 17:28:18 +01001031 assert(file == NULL);
Benoît Canet030be322013-09-24 17:07:04 +02001032 assert(!drv->bdrv_needs_filename || filename != NULL);
Max Reitz34b5d2c2013-09-05 14:45:29 +02001033 ret = drv->bdrv_file_open(bs, options, open_flags, &local_err);
Kevin Wolff500a6d2012-11-12 17:35:27 +01001034 } else {
Kevin Wolf2af5ef72013-04-09 13:19:18 +02001035 if (file == NULL) {
Max Reitz34b5d2c2013-09-05 14:45:29 +02001036 error_setg(errp, "Can't use '%s' as a block driver for the "
1037 "protocol level", drv->format_name);
Kevin Wolf2af5ef72013-04-09 13:19:18 +02001038 ret = -EINVAL;
1039 goto free_and_fail;
1040 }
Kevin Wolff500a6d2012-11-12 17:35:27 +01001041 bs->file = file;
Max Reitz34b5d2c2013-09-05 14:45:29 +02001042 ret = drv->bdrv_open(bs, options, open_flags, &local_err);
Kevin Wolf66f82ce2010-04-14 14:17:38 +02001043 }
1044
Kevin Wolf57915332010-04-14 15:24:50 +02001045 if (ret < 0) {
Markus Armbruster84d18f02014-01-30 15:07:28 +01001046 if (local_err) {
Max Reitz34b5d2c2013-09-05 14:45:29 +02001047 error_propagate(errp, local_err);
Dunrong Huang2fa9aa52013-09-24 18:14:01 +08001048 } else if (bs->filename[0]) {
1049 error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename);
Max Reitz34b5d2c2013-09-05 14:45:29 +02001050 } else {
1051 error_setg_errno(errp, -ret, "Could not open image");
1052 }
Kevin Wolf57915332010-04-14 15:24:50 +02001053 goto free_and_fail;
1054 }
1055
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001056 ret = refresh_total_sectors(bs, bs->total_sectors);
1057 if (ret < 0) {
Max Reitz34b5d2c2013-09-05 14:45:29 +02001058 error_setg_errno(errp, -ret, "Could not refresh total sector count");
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001059 goto free_and_fail;
Kevin Wolf57915332010-04-14 15:24:50 +02001060 }
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001061
Kevin Wolf3baca892014-07-16 17:48:16 +02001062 bdrv_refresh_limits(bs, &local_err);
1063 if (local_err) {
1064 error_propagate(errp, local_err);
1065 ret = -EINVAL;
1066 goto free_and_fail;
1067 }
1068
Paolo Bonzinic25f53b2011-11-29 12:42:20 +01001069 assert(bdrv_opt_mem_align(bs) != 0);
Denis V. Lunev4196d2f2015-05-12 17:30:55 +03001070 assert(bdrv_min_mem_align(bs) != 0);
Eric Blakea5b8dd22016-06-23 16:37:24 -06001071 assert(is_power_of_2(bs->bl.request_alignment));
Kevin Wolf18edf282015-04-07 17:12:56 +02001072
1073 qemu_opts_del(opts);
Kevin Wolf57915332010-04-14 15:24:50 +02001074 return 0;
1075
1076free_and_fail:
Kevin Wolff500a6d2012-11-12 17:35:27 +01001077 bs->file = NULL;
Anthony Liguori7267c092011-08-20 22:09:37 -05001078 g_free(bs->opaque);
Kevin Wolf57915332010-04-14 15:24:50 +02001079 bs->opaque = NULL;
1080 bs->drv = NULL;
Kevin Wolf18edf282015-04-07 17:12:56 +02001081fail_opts:
1082 qemu_opts_del(opts);
Kevin Wolf57915332010-04-14 15:24:50 +02001083 return ret;
1084}
1085
Kevin Wolf5e5c4f62014-05-26 11:45:08 +02001086static QDict *parse_json_filename(const char *filename, Error **errp)
1087{
1088 QObject *options_obj;
1089 QDict *options;
1090 int ret;
1091
1092 ret = strstart(filename, "json:", &filename);
1093 assert(ret);
1094
1095 options_obj = qobject_from_json(filename);
1096 if (!options_obj) {
1097 error_setg(errp, "Could not parse the JSON options");
1098 return NULL;
1099 }
1100
1101 if (qobject_type(options_obj) != QTYPE_QDICT) {
1102 qobject_decref(options_obj);
1103 error_setg(errp, "Invalid JSON object given");
1104 return NULL;
1105 }
1106
1107 options = qobject_to_qdict(options_obj);
1108 qdict_flatten(options);
1109
1110 return options;
1111}
1112
Kevin Wolfde3b53f2015-10-29 15:24:41 +01001113static void parse_json_protocol(QDict *options, const char **pfilename,
1114 Error **errp)
1115{
1116 QDict *json_options;
1117 Error *local_err = NULL;
1118
1119 /* Parse json: pseudo-protocol */
1120 if (!*pfilename || !g_str_has_prefix(*pfilename, "json:")) {
1121 return;
1122 }
1123
1124 json_options = parse_json_filename(*pfilename, &local_err);
1125 if (local_err) {
1126 error_propagate(errp, local_err);
1127 return;
1128 }
1129
1130 /* Options given in the filename have lower priority than options
1131 * specified directly */
1132 qdict_join(options, json_options, false);
1133 QDECREF(json_options);
1134 *pfilename = NULL;
1135}
1136
Kevin Wolf57915332010-04-14 15:24:50 +02001137/*
Kevin Wolff54120f2014-05-26 11:09:59 +02001138 * Fills in default options for opening images and converts the legacy
1139 * filename/flags pair to option QDict entries.
Max Reitz53a29512015-03-19 14:53:16 -04001140 * The BDRV_O_PROTOCOL flag in *flags will be set or cleared accordingly if a
1141 * block driver has been specified explicitly.
Kevin Wolff54120f2014-05-26 11:09:59 +02001142 */
Kevin Wolfde3b53f2015-10-29 15:24:41 +01001143static int bdrv_fill_options(QDict **options, const char *filename,
Max Reitz053e1572015-08-26 19:47:51 +02001144 int *flags, Error **errp)
Kevin Wolff54120f2014-05-26 11:09:59 +02001145{
1146 const char *drvname;
Max Reitz53a29512015-03-19 14:53:16 -04001147 bool protocol = *flags & BDRV_O_PROTOCOL;
Kevin Wolff54120f2014-05-26 11:09:59 +02001148 bool parse_filename = false;
Max Reitz053e1572015-08-26 19:47:51 +02001149 BlockDriver *drv = NULL;
Kevin Wolff54120f2014-05-26 11:09:59 +02001150 Error *local_err = NULL;
Kevin Wolff54120f2014-05-26 11:09:59 +02001151
Max Reitz53a29512015-03-19 14:53:16 -04001152 drvname = qdict_get_try_str(*options, "driver");
Max Reitz053e1572015-08-26 19:47:51 +02001153 if (drvname) {
1154 drv = bdrv_find_format(drvname);
1155 if (!drv) {
1156 error_setg(errp, "Unknown driver '%s'", drvname);
1157 return -ENOENT;
1158 }
1159 /* If the user has explicitly specified the driver, this choice should
1160 * override the BDRV_O_PROTOCOL flag */
1161 protocol = drv->bdrv_file_open;
Max Reitz53a29512015-03-19 14:53:16 -04001162 }
1163
1164 if (protocol) {
1165 *flags |= BDRV_O_PROTOCOL;
1166 } else {
1167 *flags &= ~BDRV_O_PROTOCOL;
1168 }
1169
Kevin Wolf91a097e2015-05-08 17:49:53 +02001170 /* Translate cache options from flags into options */
1171 update_options_from_flags(*options, *flags);
1172
Kevin Wolff54120f2014-05-26 11:09:59 +02001173 /* Fetch the file name from the options QDict if necessary */
Kevin Wolf17b005f2014-05-27 10:50:29 +02001174 if (protocol && filename) {
Kevin Wolff54120f2014-05-26 11:09:59 +02001175 if (!qdict_haskey(*options, "filename")) {
1176 qdict_put(*options, "filename", qstring_from_str(filename));
1177 parse_filename = true;
1178 } else {
1179 error_setg(errp, "Can't specify 'file' and 'filename' options at "
1180 "the same time");
1181 return -EINVAL;
1182 }
1183 }
1184
1185 /* Find the right block driver */
1186 filename = qdict_get_try_str(*options, "filename");
Kevin Wolff54120f2014-05-26 11:09:59 +02001187
Max Reitz053e1572015-08-26 19:47:51 +02001188 if (!drvname && protocol) {
1189 if (filename) {
1190 drv = bdrv_find_protocol(filename, parse_filename, errp);
1191 if (!drv) {
Kevin Wolff54120f2014-05-26 11:09:59 +02001192 return -EINVAL;
1193 }
Max Reitz053e1572015-08-26 19:47:51 +02001194
1195 drvname = drv->format_name;
1196 qdict_put(*options, "driver", qstring_from_str(drvname));
1197 } else {
1198 error_setg(errp, "Must specify either driver or file");
1199 return -EINVAL;
Kevin Wolff54120f2014-05-26 11:09:59 +02001200 }
1201 }
1202
Kevin Wolf17b005f2014-05-27 10:50:29 +02001203 assert(drv || !protocol);
Kevin Wolff54120f2014-05-26 11:09:59 +02001204
1205 /* Driver-specific filename parsing */
Kevin Wolf17b005f2014-05-27 10:50:29 +02001206 if (drv && drv->bdrv_parse_filename && parse_filename) {
Kevin Wolff54120f2014-05-26 11:09:59 +02001207 drv->bdrv_parse_filename(filename, *options, &local_err);
1208 if (local_err) {
1209 error_propagate(errp, local_err);
1210 return -EINVAL;
1211 }
1212
1213 if (!drv->bdrv_needs_filename) {
1214 qdict_del(*options, "filename");
1215 }
1216 }
1217
1218 return 0;
1219}
1220
Kevin Wolfe9740bc2016-05-23 15:52:26 +02001221static void bdrv_replace_child(BdrvChild *child, BlockDriverState *new_bs)
1222{
1223 BlockDriverState *old_bs = child->bs;
1224
1225 if (old_bs) {
Kevin Wolf36fe1332016-05-17 14:51:55 +02001226 if (old_bs->quiesce_counter && child->role->drained_end) {
1227 child->role->drained_end(child);
1228 }
Kevin Wolfe9740bc2016-05-23 15:52:26 +02001229 QLIST_REMOVE(child, next_parent);
1230 }
Kevin Wolfe9740bc2016-05-23 15:52:26 +02001231
1232 child->bs = new_bs;
Kevin Wolf36fe1332016-05-17 14:51:55 +02001233
1234 if (new_bs) {
1235 QLIST_INSERT_HEAD(&new_bs->parents, child, next_parent);
1236 if (new_bs->quiesce_counter && child->role->drained_begin) {
1237 child->role->drained_begin(child);
1238 }
1239 }
Kevin Wolfe9740bc2016-05-23 15:52:26 +02001240}
1241
Kevin Wolff21d96d2016-03-08 13:47:46 +01001242BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs,
1243 const char *child_name,
Kevin Wolf36fe1332016-05-17 14:51:55 +02001244 const BdrvChildRole *child_role,
1245 void *opaque)
Kevin Wolfdf581792015-06-15 11:53:47 +02001246{
1247 BdrvChild *child = g_new(BdrvChild, 1);
1248 *child = (BdrvChild) {
Kevin Wolfe9740bc2016-05-23 15:52:26 +02001249 .bs = NULL,
Kevin Wolf260fecf2015-04-27 13:46:22 +02001250 .name = g_strdup(child_name),
Kevin Wolfdf581792015-06-15 11:53:47 +02001251 .role = child_role,
Kevin Wolf36fe1332016-05-17 14:51:55 +02001252 .opaque = opaque,
Kevin Wolfdf581792015-06-15 11:53:47 +02001253 };
1254
Kevin Wolfe9740bc2016-05-23 15:52:26 +02001255 bdrv_replace_child(child, child_bs);
Kevin Wolfb4b059f2015-06-15 13:24:19 +02001256
1257 return child;
Kevin Wolfdf581792015-06-15 11:53:47 +02001258}
1259
Wen Congyang98292c62016-05-10 15:36:38 +08001260BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs,
1261 BlockDriverState *child_bs,
1262 const char *child_name,
1263 const BdrvChildRole *child_role)
Kevin Wolff21d96d2016-03-08 13:47:46 +01001264{
Kevin Wolf36fe1332016-05-17 14:51:55 +02001265 BdrvChild *child = bdrv_root_attach_child(child_bs, child_name, child_role,
Kevin Wolf20018e12016-05-23 18:46:59 +02001266 parent_bs);
Kevin Wolff21d96d2016-03-08 13:47:46 +01001267 QLIST_INSERT_HEAD(&parent_bs->children, child, next);
1268 return child;
1269}
1270
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001271static void bdrv_detach_child(BdrvChild *child)
Kevin Wolf33a60402015-06-15 13:51:04 +02001272{
Kevin Wolff21d96d2016-03-08 13:47:46 +01001273 if (child->next.le_prev) {
1274 QLIST_REMOVE(child, next);
1275 child->next.le_prev = NULL;
1276 }
Kevin Wolfe9740bc2016-05-23 15:52:26 +02001277
1278 bdrv_replace_child(child, NULL);
1279
Kevin Wolf260fecf2015-04-27 13:46:22 +02001280 g_free(child->name);
Kevin Wolf33a60402015-06-15 13:51:04 +02001281 g_free(child);
1282}
1283
Kevin Wolff21d96d2016-03-08 13:47:46 +01001284void bdrv_root_unref_child(BdrvChild *child)
Kevin Wolf33a60402015-06-15 13:51:04 +02001285{
Kevin Wolf779020c2015-10-13 14:09:44 +02001286 BlockDriverState *child_bs;
1287
Kevin Wolff21d96d2016-03-08 13:47:46 +01001288 child_bs = child->bs;
1289 bdrv_detach_child(child);
1290 bdrv_unref(child_bs);
1291}
1292
1293void bdrv_unref_child(BlockDriverState *parent, BdrvChild *child)
1294{
Kevin Wolf779020c2015-10-13 14:09:44 +02001295 if (child == NULL) {
1296 return;
1297 }
Kevin Wolf33a60402015-06-15 13:51:04 +02001298
1299 if (child->bs->inherits_from == parent) {
1300 child->bs->inherits_from = NULL;
1301 }
1302
Kevin Wolff21d96d2016-03-08 13:47:46 +01001303 bdrv_root_unref_child(child);
Kevin Wolf33a60402015-06-15 13:51:04 +02001304}
1305
Kevin Wolf5c8cab42016-02-24 15:13:35 +01001306
1307static void bdrv_parent_cb_change_media(BlockDriverState *bs, bool load)
1308{
1309 BdrvChild *c;
1310 QLIST_FOREACH(c, &bs->parents, next_parent) {
1311 if (c->role->change_media) {
1312 c->role->change_media(c, load);
1313 }
1314 }
1315}
1316
1317static void bdrv_parent_cb_resize(BlockDriverState *bs)
1318{
1319 BdrvChild *c;
1320 QLIST_FOREACH(c, &bs->parents, next_parent) {
1321 if (c->role->resize) {
1322 c->role->resize(c);
1323 }
1324 }
1325}
1326
Kevin Wolf5db15a52015-09-14 15:33:33 +02001327/*
1328 * Sets the backing file link of a BDS. A new reference is created; callers
1329 * which don't need their own reference any more must call bdrv_unref().
1330 */
Fam Zheng8d24cce2014-05-23 21:29:45 +08001331void bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd)
1332{
Kevin Wolf5db15a52015-09-14 15:33:33 +02001333 if (backing_hd) {
1334 bdrv_ref(backing_hd);
1335 }
Fam Zheng8d24cce2014-05-23 21:29:45 +08001336
Kevin Wolf760e0062015-06-17 14:55:21 +02001337 if (bs->backing) {
Fam Zheng826b6ca2014-05-23 21:29:47 +08001338 assert(bs->backing_blocker);
Kevin Wolf760e0062015-06-17 14:55:21 +02001339 bdrv_op_unblock_all(bs->backing->bs, bs->backing_blocker);
Kevin Wolf5db15a52015-09-14 15:33:33 +02001340 bdrv_unref_child(bs, bs->backing);
Fam Zheng826b6ca2014-05-23 21:29:47 +08001341 } else if (backing_hd) {
1342 error_setg(&bs->backing_blocker,
Alberto Garcia81e5f782015-04-08 12:29:19 +03001343 "node is used as backing hd of '%s'",
1344 bdrv_get_device_or_node_name(bs));
Fam Zheng826b6ca2014-05-23 21:29:47 +08001345 }
1346
Fam Zheng8d24cce2014-05-23 21:29:45 +08001347 if (!backing_hd) {
Fam Zheng826b6ca2014-05-23 21:29:47 +08001348 error_free(bs->backing_blocker);
1349 bs->backing_blocker = NULL;
Kevin Wolf760e0062015-06-17 14:55:21 +02001350 bs->backing = NULL;
Fam Zheng8d24cce2014-05-23 21:29:45 +08001351 goto out;
1352 }
Kevin Wolf260fecf2015-04-27 13:46:22 +02001353 bs->backing = bdrv_attach_child(bs, backing_hd, "backing", &child_backing);
Fam Zheng8d24cce2014-05-23 21:29:45 +08001354 bs->open_flags &= ~BDRV_O_NO_BACKING;
1355 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_hd->filename);
1356 pstrcpy(bs->backing_format, sizeof(bs->backing_format),
1357 backing_hd->drv ? backing_hd->drv->format_name : "");
Fam Zheng826b6ca2014-05-23 21:29:47 +08001358
Kevin Wolf760e0062015-06-17 14:55:21 +02001359 bdrv_op_block_all(backing_hd, bs->backing_blocker);
Fam Zheng826b6ca2014-05-23 21:29:47 +08001360 /* Otherwise we won't be able to commit due to check in bdrv_commit */
Kevin Wolf760e0062015-06-17 14:55:21 +02001361 bdrv_op_unblock(backing_hd, BLOCK_OP_TYPE_COMMIT_TARGET,
Fam Zheng826b6ca2014-05-23 21:29:47 +08001362 bs->backing_blocker);
Wen Congyange9d64562016-07-27 15:01:42 +08001363 /*
1364 * We do backup in 3 ways:
1365 * 1. drive backup
1366 * The target bs is new opened, and the source is top BDS
1367 * 2. blockdev backup
1368 * Both the source and the target are top BDSes.
1369 * 3. internal backup(used for block replication)
1370 * Both the source and the target are backing file
1371 *
1372 * In case 1 and 2, neither the source nor the target is the backing file.
1373 * In case 3, we will block the top BDS, so there is only one block job
1374 * for the top BDS and its backing chain.
1375 */
1376 bdrv_op_unblock(backing_hd, BLOCK_OP_TYPE_BACKUP_SOURCE,
1377 bs->backing_blocker);
1378 bdrv_op_unblock(backing_hd, BLOCK_OP_TYPE_BACKUP_TARGET,
1379 bs->backing_blocker);
Fam Zheng8d24cce2014-05-23 21:29:45 +08001380out:
Kevin Wolf3baca892014-07-16 17:48:16 +02001381 bdrv_refresh_limits(bs, NULL);
Fam Zheng8d24cce2014-05-23 21:29:45 +08001382}
1383
Kevin Wolf31ca6d02013-03-28 15:29:24 +01001384/*
1385 * Opens the backing file for a BlockDriverState if not yet open
1386 *
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001387 * bdref_key specifies the key for the image's BlockdevRef in the options QDict.
1388 * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict
1389 * itself, all options starting with "${bdref_key}." are considered part of the
1390 * BlockdevRef.
1391 *
1392 * TODO Can this be unified with bdrv_open_image()?
Kevin Wolf31ca6d02013-03-28 15:29:24 +01001393 */
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001394int bdrv_open_backing_file(BlockDriverState *bs, QDict *parent_options,
1395 const char *bdref_key, Error **errp)
Paolo Bonzini9156df12012-10-18 16:49:17 +02001396{
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001397 char *backing_filename = g_malloc0(PATH_MAX);
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001398 char *bdref_key_dot;
1399 const char *reference = NULL;
Kevin Wolf317fc442014-04-25 13:27:34 +02001400 int ret = 0;
Fam Zheng8d24cce2014-05-23 21:29:45 +08001401 BlockDriverState *backing_hd;
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001402 QDict *options;
1403 QDict *tmp_parent_options = NULL;
Max Reitz34b5d2c2013-09-05 14:45:29 +02001404 Error *local_err = NULL;
Paolo Bonzini9156df12012-10-18 16:49:17 +02001405
Kevin Wolf760e0062015-06-17 14:55:21 +02001406 if (bs->backing != NULL) {
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001407 goto free_exit;
Paolo Bonzini9156df12012-10-18 16:49:17 +02001408 }
1409
Kevin Wolf31ca6d02013-03-28 15:29:24 +01001410 /* NULL means an empty set of options */
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001411 if (parent_options == NULL) {
1412 tmp_parent_options = qdict_new();
1413 parent_options = tmp_parent_options;
Kevin Wolf31ca6d02013-03-28 15:29:24 +01001414 }
1415
Paolo Bonzini9156df12012-10-18 16:49:17 +02001416 bs->open_flags &= ~BDRV_O_NO_BACKING;
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001417
1418 bdref_key_dot = g_strdup_printf("%s.", bdref_key);
1419 qdict_extract_subqdict(parent_options, &options, bdref_key_dot);
1420 g_free(bdref_key_dot);
1421
1422 reference = qdict_get_try_str(parent_options, bdref_key);
1423 if (reference || qdict_haskey(options, "file.filename")) {
Kevin Wolf1cb6f502013-04-12 20:27:07 +02001424 backing_filename[0] = '\0';
1425 } else if (bs->backing_file[0] == '\0' && qdict_size(options) == 0) {
Kevin Wolf31ca6d02013-03-28 15:29:24 +01001426 QDECREF(options);
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001427 goto free_exit;
Fam Zhengdbecebd2013-09-22 20:05:06 +08001428 } else {
Max Reitz9f074292014-11-26 17:20:26 +01001429 bdrv_get_full_backing_filename(bs, backing_filename, PATH_MAX,
1430 &local_err);
1431 if (local_err) {
1432 ret = -EINVAL;
1433 error_propagate(errp, local_err);
1434 QDECREF(options);
1435 goto free_exit;
1436 }
Paolo Bonzini9156df12012-10-18 16:49:17 +02001437 }
1438
Kevin Wolf8ee79e72014-06-04 15:09:35 +02001439 if (!bs->drv || !bs->drv->supports_backing) {
1440 ret = -EINVAL;
1441 error_setg(errp, "Driver doesn't support backing files");
1442 QDECREF(options);
1443 goto free_exit;
1444 }
1445
Kevin Wolfc5f6e492014-11-25 18:12:42 +01001446 if (bs->backing_format[0] != '\0' && !qdict_haskey(options, "driver")) {
1447 qdict_put(options, "driver", qstring_from_str(bs->backing_format));
Paolo Bonzini9156df12012-10-18 16:49:17 +02001448 }
1449
Max Reitz5b363932016-05-17 16:41:31 +02001450 backing_hd = bdrv_open_inherit(*backing_filename ? backing_filename : NULL,
1451 reference, options, 0, bs, &child_backing,
1452 errp);
1453 if (!backing_hd) {
Paolo Bonzini9156df12012-10-18 16:49:17 +02001454 bs->open_flags |= BDRV_O_NO_BACKING;
Markus Armbrustere43bfd92015-12-18 16:35:15 +01001455 error_prepend(errp, "Could not open backing file: ");
Max Reitz5b363932016-05-17 16:41:31 +02001456 ret = -EINVAL;
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001457 goto free_exit;
Paolo Bonzini9156df12012-10-18 16:49:17 +02001458 }
Kevin Wolfdf581792015-06-15 11:53:47 +02001459
Kevin Wolf5db15a52015-09-14 15:33:33 +02001460 /* Hook up the backing file link; drop our reference, bs owns the
1461 * backing_hd reference now */
Fam Zheng8d24cce2014-05-23 21:29:45 +08001462 bdrv_set_backing_hd(bs, backing_hd);
Kevin Wolf5db15a52015-09-14 15:33:33 +02001463 bdrv_unref(backing_hd);
Peter Feinerd80ac652014-01-08 19:43:25 +00001464
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001465 qdict_del(parent_options, bdref_key);
1466
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001467free_exit:
1468 g_free(backing_filename);
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001469 QDECREF(tmp_parent_options);
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001470 return ret;
Paolo Bonzini9156df12012-10-18 16:49:17 +02001471}
1472
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001473/*
Max Reitzda557aa2013-12-20 19:28:11 +01001474 * Opens a disk image whose options are given as BlockdevRef in another block
1475 * device's options.
1476 *
Max Reitzda557aa2013-12-20 19:28:11 +01001477 * If allow_none is true, no image will be opened if filename is false and no
Kevin Wolfb4b059f2015-06-15 13:24:19 +02001478 * BlockdevRef is given. NULL will be returned, but errp remains unset.
Max Reitzda557aa2013-12-20 19:28:11 +01001479 *
1480 * bdrev_key specifies the key for the image's BlockdevRef in the options QDict.
1481 * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict
1482 * itself, all options starting with "${bdref_key}." are considered part of the
1483 * BlockdevRef.
1484 *
1485 * The BlockdevRef will be removed from the options QDict.
1486 */
Kevin Wolfb4b059f2015-06-15 13:24:19 +02001487BdrvChild *bdrv_open_child(const char *filename,
1488 QDict *options, const char *bdref_key,
1489 BlockDriverState* parent,
1490 const BdrvChildRole *child_role,
1491 bool allow_none, Error **errp)
Max Reitzda557aa2013-12-20 19:28:11 +01001492{
Kevin Wolfb4b059f2015-06-15 13:24:19 +02001493 BdrvChild *c = NULL;
1494 BlockDriverState *bs;
Max Reitzda557aa2013-12-20 19:28:11 +01001495 QDict *image_options;
Max Reitzda557aa2013-12-20 19:28:11 +01001496 char *bdref_key_dot;
1497 const char *reference;
1498
Kevin Wolfdf581792015-06-15 11:53:47 +02001499 assert(child_role != NULL);
Max Reitzf67503e2014-02-18 18:33:05 +01001500
Max Reitzda557aa2013-12-20 19:28:11 +01001501 bdref_key_dot = g_strdup_printf("%s.", bdref_key);
1502 qdict_extract_subqdict(options, &image_options, bdref_key_dot);
1503 g_free(bdref_key_dot);
1504
1505 reference = qdict_get_try_str(options, bdref_key);
1506 if (!filename && !reference && !qdict_size(image_options)) {
Kevin Wolfb4b059f2015-06-15 13:24:19 +02001507 if (!allow_none) {
Max Reitzda557aa2013-12-20 19:28:11 +01001508 error_setg(errp, "A block device must be specified for \"%s\"",
1509 bdref_key);
Max Reitzda557aa2013-12-20 19:28:11 +01001510 }
Markus Armbrusterb20e61e2014-05-28 11:16:57 +02001511 QDECREF(image_options);
Max Reitzda557aa2013-12-20 19:28:11 +01001512 goto done;
1513 }
1514
Max Reitz5b363932016-05-17 16:41:31 +02001515 bs = bdrv_open_inherit(filename, reference, image_options, 0,
1516 parent, child_role, errp);
1517 if (!bs) {
Kevin Wolfdf581792015-06-15 11:53:47 +02001518 goto done;
1519 }
1520
Kevin Wolf260fecf2015-04-27 13:46:22 +02001521 c = bdrv_attach_child(parent, bs, bdref_key, child_role);
Max Reitzda557aa2013-12-20 19:28:11 +01001522
1523done:
1524 qdict_del(options, bdref_key);
Kevin Wolfb4b059f2015-06-15 13:24:19 +02001525 return c;
1526}
1527
Max Reitz66836182016-05-17 16:41:27 +02001528static BlockDriverState *bdrv_append_temp_snapshot(BlockDriverState *bs,
1529 int flags,
1530 QDict *snapshot_options,
1531 Error **errp)
Kevin Wolfb9988752014-04-03 12:09:34 +02001532{
1533 /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001534 char *tmp_filename = g_malloc0(PATH_MAX + 1);
Kevin Wolfb9988752014-04-03 12:09:34 +02001535 int64_t total_size;
Chunyan Liu83d05212014-06-05 17:20:51 +08001536 QemuOpts *opts = NULL;
Kevin Wolfb9988752014-04-03 12:09:34 +02001537 BlockDriverState *bs_snapshot;
Kevin Wolfb9988752014-04-03 12:09:34 +02001538 int ret;
1539
1540 /* if snapshot, we create a temporary backing file and open it
1541 instead of opening 'filename' directly */
1542
1543 /* Get the required size from the image */
Kevin Wolff1877432014-04-04 17:07:19 +02001544 total_size = bdrv_getlength(bs);
1545 if (total_size < 0) {
1546 error_setg_errno(errp, -total_size, "Could not get image size");
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001547 goto out;
Kevin Wolff1877432014-04-04 17:07:19 +02001548 }
Kevin Wolfb9988752014-04-03 12:09:34 +02001549
1550 /* Create the temporary image */
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001551 ret = get_tmp_filename(tmp_filename, PATH_MAX + 1);
Kevin Wolfb9988752014-04-03 12:09:34 +02001552 if (ret < 0) {
1553 error_setg_errno(errp, -ret, "Could not get temporary filename");
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001554 goto out;
Kevin Wolfb9988752014-04-03 12:09:34 +02001555 }
1556
Max Reitzef810432014-12-02 18:32:42 +01001557 opts = qemu_opts_create(bdrv_qcow2.create_opts, NULL, 0,
Chunyan Liuc282e1f2014-06-05 17:21:11 +08001558 &error_abort);
Markus Armbruster39101f22015-02-12 16:46:36 +01001559 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_size, &error_abort);
Markus Armbrustere43bfd92015-12-18 16:35:15 +01001560 ret = bdrv_create(&bdrv_qcow2, tmp_filename, opts, errp);
Chunyan Liu83d05212014-06-05 17:20:51 +08001561 qemu_opts_del(opts);
Kevin Wolfb9988752014-04-03 12:09:34 +02001562 if (ret < 0) {
Markus Armbrustere43bfd92015-12-18 16:35:15 +01001563 error_prepend(errp, "Could not create temporary overlay '%s': ",
1564 tmp_filename);
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001565 goto out;
Kevin Wolfb9988752014-04-03 12:09:34 +02001566 }
1567
Kevin Wolf73176be2016-03-07 13:02:15 +01001568 /* Prepare options QDict for the temporary file */
Kevin Wolfb9988752014-04-03 12:09:34 +02001569 qdict_put(snapshot_options, "file.driver",
1570 qstring_from_str("file"));
1571 qdict_put(snapshot_options, "file.filename",
1572 qstring_from_str(tmp_filename));
Max Reitze6641712015-08-26 19:47:48 +02001573 qdict_put(snapshot_options, "driver",
1574 qstring_from_str("qcow2"));
Kevin Wolfb9988752014-04-03 12:09:34 +02001575
Max Reitz5b363932016-05-17 16:41:31 +02001576 bs_snapshot = bdrv_open(NULL, NULL, snapshot_options, flags, errp);
Kevin Wolf73176be2016-03-07 13:02:15 +01001577 snapshot_options = NULL;
Max Reitz5b363932016-05-17 16:41:31 +02001578 if (!bs_snapshot) {
1579 ret = -EINVAL;
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001580 goto out;
Kevin Wolfb9988752014-04-03 12:09:34 +02001581 }
1582
Max Reitz66836182016-05-17 16:41:27 +02001583 /* bdrv_append() consumes a strong reference to bs_snapshot (i.e. it will
1584 * call bdrv_unref() on it), so in order to be able to return one, we have
1585 * to increase bs_snapshot's refcount here */
1586 bdrv_ref(bs_snapshot);
Kevin Wolfb9988752014-04-03 12:09:34 +02001587 bdrv_append(bs_snapshot, bs);
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001588
Max Reitz66836182016-05-17 16:41:27 +02001589 g_free(tmp_filename);
1590 return bs_snapshot;
1591
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001592out:
Kevin Wolf73176be2016-03-07 13:02:15 +01001593 QDECREF(snapshot_options);
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001594 g_free(tmp_filename);
Max Reitz66836182016-05-17 16:41:27 +02001595 return NULL;
Kevin Wolfb9988752014-04-03 12:09:34 +02001596}
1597
Max Reitzda557aa2013-12-20 19:28:11 +01001598/*
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001599 * Opens a disk image (raw, qcow2, vmdk, ...)
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001600 *
1601 * options is a QDict of options to pass to the block drivers, or NULL for an
1602 * empty set of options. The reference to the QDict belongs to the block layer
1603 * after the call (even on failure), so if the caller intends to reuse the
1604 * dictionary, it needs to use QINCREF() before calling bdrv_open.
Max Reitzf67503e2014-02-18 18:33:05 +01001605 *
1606 * If *pbs is NULL, a new BDS will be created with a pointer to it stored there.
1607 * If it is not NULL, the referenced BDS will be reused.
Max Reitzddf56362014-02-18 18:33:06 +01001608 *
1609 * The reference parameter may be used to specify an existing block device which
1610 * should be opened. If specified, neither options nor a filename may be given,
1611 * nor can an existing BDS be reused (that is, *pbs has to be NULL).
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001612 */
Max Reitz5b363932016-05-17 16:41:31 +02001613static BlockDriverState *bdrv_open_inherit(const char *filename,
1614 const char *reference,
1615 QDict *options, int flags,
1616 BlockDriverState *parent,
1617 const BdrvChildRole *child_role,
1618 Error **errp)
bellardea2384d2004-08-01 21:59:26 +00001619{
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001620 int ret;
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02001621 BdrvChild *file = NULL;
1622 BlockDriverState *bs;
Max Reitzce343772015-08-26 19:47:50 +02001623 BlockDriver *drv = NULL;
Kevin Wolf74fe54f2013-07-09 11:09:02 +02001624 const char *drvname;
Alberto Garcia3e8c2e52015-10-26 14:27:15 +02001625 const char *backing;
Max Reitz34b5d2c2013-09-05 14:45:29 +02001626 Error *local_err = NULL;
Kevin Wolf73176be2016-03-07 13:02:15 +01001627 QDict *snapshot_options = NULL;
Kevin Wolfb1e6fc02014-05-06 12:11:42 +02001628 int snapshot_flags = 0;
bellard712e7872005-04-28 21:09:32 +00001629
Kevin Wolff3930ed2015-04-08 13:43:47 +02001630 assert(!child_role || !flags);
1631 assert(!child_role == !parent);
Max Reitzf67503e2014-02-18 18:33:05 +01001632
Max Reitzddf56362014-02-18 18:33:06 +01001633 if (reference) {
1634 bool options_non_empty = options ? qdict_size(options) : false;
1635 QDECREF(options);
1636
Max Reitzddf56362014-02-18 18:33:06 +01001637 if (filename || options_non_empty) {
1638 error_setg(errp, "Cannot reference an existing block device with "
1639 "additional options or a new filename");
Max Reitz5b363932016-05-17 16:41:31 +02001640 return NULL;
Max Reitzddf56362014-02-18 18:33:06 +01001641 }
1642
1643 bs = bdrv_lookup_bs(reference, reference, errp);
1644 if (!bs) {
Max Reitz5b363932016-05-17 16:41:31 +02001645 return NULL;
Max Reitzddf56362014-02-18 18:33:06 +01001646 }
Kevin Wolf76b22322016-04-04 17:11:13 +02001647
Max Reitzddf56362014-02-18 18:33:06 +01001648 bdrv_ref(bs);
Max Reitz5b363932016-05-17 16:41:31 +02001649 return bs;
Max Reitzddf56362014-02-18 18:33:06 +01001650 }
1651
Max Reitz5b363932016-05-17 16:41:31 +02001652 bs = bdrv_new();
Max Reitzf67503e2014-02-18 18:33:05 +01001653
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001654 /* NULL means an empty set of options */
1655 if (options == NULL) {
1656 options = qdict_new();
1657 }
1658
Kevin Wolf145f5982015-05-08 16:15:03 +02001659 /* json: syntax counts as explicit options, as if in the QDict */
Kevin Wolfde3b53f2015-10-29 15:24:41 +01001660 parse_json_protocol(options, &filename, &local_err);
1661 if (local_err) {
Kevin Wolfde3b53f2015-10-29 15:24:41 +01001662 goto fail;
1663 }
1664
Kevin Wolf145f5982015-05-08 16:15:03 +02001665 bs->explicit_options = qdict_clone_shallow(options);
1666
Kevin Wolff3930ed2015-04-08 13:43:47 +02001667 if (child_role) {
Kevin Wolfbddcec32015-04-09 18:47:50 +02001668 bs->inherits_from = parent;
Kevin Wolf8e2160e2015-04-29 17:29:39 +02001669 child_role->inherit_options(&flags, options,
1670 parent->open_flags, parent->options);
Kevin Wolff3930ed2015-04-08 13:43:47 +02001671 }
1672
Kevin Wolfde3b53f2015-10-29 15:24:41 +01001673 ret = bdrv_fill_options(&options, filename, &flags, &local_err);
Kevin Wolf462f5bc2014-05-26 11:39:55 +02001674 if (local_err) {
1675 goto fail;
1676 }
1677
Kevin Wolf62392eb2015-04-24 16:38:02 +02001678 bs->open_flags = flags;
1679 bs->options = options;
1680 options = qdict_clone_shallow(options);
1681
Kevin Wolf76c591b2014-06-04 14:19:44 +02001682 /* Find the right image format driver */
Kevin Wolf76c591b2014-06-04 14:19:44 +02001683 drvname = qdict_get_try_str(options, "driver");
1684 if (drvname) {
1685 drv = bdrv_find_format(drvname);
Kevin Wolf76c591b2014-06-04 14:19:44 +02001686 if (!drv) {
1687 error_setg(errp, "Unknown driver: '%s'", drvname);
Kevin Wolf76c591b2014-06-04 14:19:44 +02001688 goto fail;
1689 }
1690 }
1691
1692 assert(drvname || !(flags & BDRV_O_PROTOCOL));
Kevin Wolf76c591b2014-06-04 14:19:44 +02001693
Alberto Garcia3e8c2e52015-10-26 14:27:15 +02001694 backing = qdict_get_try_str(options, "backing");
1695 if (backing && *backing == '\0') {
1696 flags |= BDRV_O_NO_BACKING;
1697 qdict_del(options, "backing");
1698 }
1699
Kevin Wolff4788ad2014-06-03 16:44:19 +02001700 /* Open image file without format layer */
1701 if ((flags & BDRV_O_PROTOCOL) == 0) {
1702 if (flags & BDRV_O_RDWR) {
1703 flags |= BDRV_O_ALLOW_RDWR;
1704 }
1705 if (flags & BDRV_O_SNAPSHOT) {
Kevin Wolf73176be2016-03-07 13:02:15 +01001706 snapshot_options = qdict_new();
1707 bdrv_temp_snapshot_options(&snapshot_flags, snapshot_options,
1708 flags, options);
Kevin Wolf8e2160e2015-04-29 17:29:39 +02001709 bdrv_backing_options(&flags, options, flags, options);
Kevin Wolff4788ad2014-06-03 16:44:19 +02001710 }
1711
Kevin Wolff3930ed2015-04-08 13:43:47 +02001712 bs->open_flags = flags;
Kevin Wolf1fdd6932015-06-15 14:11:51 +02001713
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02001714 file = bdrv_open_child(filename, options, "file", bs,
1715 &child_file, true, &local_err);
Kevin Wolf1fdd6932015-06-15 14:11:51 +02001716 if (local_err) {
Max Reitz5469a2a2014-02-18 18:33:10 +01001717 goto fail;
1718 }
1719 }
1720
Kevin Wolf76c591b2014-06-04 14:19:44 +02001721 /* Image format probing */
Kevin Wolf38f3ef52014-11-20 16:27:12 +01001722 bs->probed = !drv;
Kevin Wolf76c591b2014-06-04 14:19:44 +02001723 if (!drv && file) {
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +02001724 ret = find_image_format(file, filename, &drv, &local_err);
Kevin Wolf17b005f2014-05-27 10:50:29 +02001725 if (ret < 0) {
Kevin Wolf8bfea152014-04-11 19:16:36 +02001726 goto fail;
Max Reitz2a05cbe2013-12-20 19:28:10 +01001727 }
Kevin Wolf62392eb2015-04-24 16:38:02 +02001728 /*
1729 * This option update would logically belong in bdrv_fill_options(),
1730 * but we first need to open bs->file for the probing to work, while
1731 * opening bs->file already requires the (mostly) final set of options
1732 * so that cache mode etc. can be inherited.
1733 *
1734 * Adding the driver later is somewhat ugly, but it's not an option
1735 * that would ever be inherited, so it's correct. We just need to make
1736 * sure to update both bs->options (which has the full effective
1737 * options for bs) and options (which has file.* already removed).
1738 */
1739 qdict_put(bs->options, "driver", qstring_from_str(drv->format_name));
1740 qdict_put(options, "driver", qstring_from_str(drv->format_name));
Kevin Wolf76c591b2014-06-04 14:19:44 +02001741 } else if (!drv) {
Kevin Wolf17b005f2014-05-27 10:50:29 +02001742 error_setg(errp, "Must specify either driver or file");
Kevin Wolf8bfea152014-04-11 19:16:36 +02001743 goto fail;
Kevin Wolff500a6d2012-11-12 17:35:27 +01001744 }
1745
Max Reitz53a29512015-03-19 14:53:16 -04001746 /* BDRV_O_PROTOCOL must be set iff a protocol BDS is about to be created */
1747 assert(!!(flags & BDRV_O_PROTOCOL) == !!drv->bdrv_file_open);
1748 /* file must be NULL if a protocol BDS is about to be created
1749 * (the inverse results in an error message from bdrv_open_common()) */
1750 assert(!(flags & BDRV_O_PROTOCOL) || !file);
1751
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001752 /* Open the image */
Kevin Wolf82dc8b42016-01-11 19:07:50 +01001753 ret = bdrv_open_common(bs, file, options, &local_err);
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001754 if (ret < 0) {
Kevin Wolf8bfea152014-04-11 19:16:36 +02001755 goto fail;
Christoph Hellwig69873072010-01-20 18:13:25 +01001756 }
1757
Max Reitz2a05cbe2013-12-20 19:28:10 +01001758 if (file && (bs->file != file)) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02001759 bdrv_unref_child(bs, file);
Kevin Wolff500a6d2012-11-12 17:35:27 +01001760 file = NULL;
1761 }
1762
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001763 /* If there is a backing file, use it */
Paolo Bonzini9156df12012-10-18 16:49:17 +02001764 if ((flags & BDRV_O_NO_BACKING) == 0) {
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001765 ret = bdrv_open_backing_file(bs, options, "backing", &local_err);
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001766 if (ret < 0) {
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001767 goto close_and_fail;
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001768 }
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001769 }
1770
Max Reitz91af7012014-07-18 20:24:56 +02001771 bdrv_refresh_filename(bs);
1772
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001773 /* Check if any unknown options were used */
Max Reitz5acd9d82014-02-18 18:33:11 +01001774 if (options && (qdict_size(options) != 0)) {
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001775 const QDictEntry *entry = qdict_first(options);
Max Reitz5acd9d82014-02-18 18:33:11 +01001776 if (flags & BDRV_O_PROTOCOL) {
1777 error_setg(errp, "Block protocol '%s' doesn't support the option "
1778 "'%s'", drv->format_name, entry->key);
1779 } else {
Max Reitzd0e46a52016-03-16 19:54:34 +01001780 error_setg(errp,
1781 "Block format '%s' does not support the option '%s'",
1782 drv->format_name, entry->key);
Max Reitz5acd9d82014-02-18 18:33:11 +01001783 }
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001784
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001785 goto close_and_fail;
1786 }
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001787
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001788 if (!bdrv_key_required(bs)) {
Kevin Wolf5c8cab42016-02-24 15:13:35 +01001789 bdrv_parent_cb_change_media(bs, true);
Markus Armbrusterc3adb582014-03-14 09:22:48 +01001790 } else if (!runstate_check(RUN_STATE_PRELAUNCH)
1791 && !runstate_check(RUN_STATE_INMIGRATE)
1792 && !runstate_check(RUN_STATE_PAUSED)) { /* HACK */
1793 error_setg(errp,
1794 "Guest must be stopped for opening of encrypted image");
Markus Armbrusterc3adb582014-03-14 09:22:48 +01001795 goto close_and_fail;
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001796 }
1797
Markus Armbrusterc3adb582014-03-14 09:22:48 +01001798 QDECREF(options);
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02001799
1800 /* For snapshot=on, create a temporary qcow2 overlay. bs points to the
1801 * temporary snapshot afterwards. */
1802 if (snapshot_flags) {
Max Reitz66836182016-05-17 16:41:27 +02001803 BlockDriverState *snapshot_bs;
1804 snapshot_bs = bdrv_append_temp_snapshot(bs, snapshot_flags,
1805 snapshot_options, &local_err);
Kevin Wolf73176be2016-03-07 13:02:15 +01001806 snapshot_options = NULL;
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02001807 if (local_err) {
1808 goto close_and_fail;
1809 }
Max Reitz5b363932016-05-17 16:41:31 +02001810 /* We are not going to return bs but the overlay on top of it
1811 * (snapshot_bs); thus, we have to drop the strong reference to bs
1812 * (which we obtained by calling bdrv_new()). bs will not be deleted,
1813 * though, because the overlay still has a reference to it. */
1814 bdrv_unref(bs);
1815 bs = snapshot_bs;
Max Reitz66836182016-05-17 16:41:27 +02001816 }
1817
Max Reitz5b363932016-05-17 16:41:31 +02001818 return bs;
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001819
Kevin Wolf8bfea152014-04-11 19:16:36 +02001820fail:
Kevin Wolff500a6d2012-11-12 17:35:27 +01001821 if (file != NULL) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02001822 bdrv_unref_child(bs, file);
Kevin Wolff500a6d2012-11-12 17:35:27 +01001823 }
Kevin Wolf73176be2016-03-07 13:02:15 +01001824 QDECREF(snapshot_options);
Kevin Wolf145f5982015-05-08 16:15:03 +02001825 QDECREF(bs->explicit_options);
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001826 QDECREF(bs->options);
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001827 QDECREF(options);
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001828 bs->options = NULL;
Max Reitz5b363932016-05-17 16:41:31 +02001829 bdrv_unref(bs);
Eduardo Habkost621ff942016-06-13 18:57:56 -03001830 error_propagate(errp, local_err);
Max Reitz5b363932016-05-17 16:41:31 +02001831 return NULL;
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001832
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001833close_and_fail:
Max Reitz5b363932016-05-17 16:41:31 +02001834 bdrv_unref(bs);
Kevin Wolf73176be2016-03-07 13:02:15 +01001835 QDECREF(snapshot_options);
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001836 QDECREF(options);
Eduardo Habkost621ff942016-06-13 18:57:56 -03001837 error_propagate(errp, local_err);
Max Reitz5b363932016-05-17 16:41:31 +02001838 return NULL;
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001839}
1840
Max Reitz5b363932016-05-17 16:41:31 +02001841BlockDriverState *bdrv_open(const char *filename, const char *reference,
1842 QDict *options, int flags, Error **errp)
Kevin Wolff3930ed2015-04-08 13:43:47 +02001843{
Max Reitz5b363932016-05-17 16:41:31 +02001844 return bdrv_open_inherit(filename, reference, options, flags, NULL,
Max Reitzce343772015-08-26 19:47:50 +02001845 NULL, errp);
Kevin Wolff3930ed2015-04-08 13:43:47 +02001846}
1847
Jeff Codye971aa12012-09-20 15:13:19 -04001848typedef struct BlockReopenQueueEntry {
1849 bool prepared;
1850 BDRVReopenState state;
1851 QSIMPLEQ_ENTRY(BlockReopenQueueEntry) entry;
1852} BlockReopenQueueEntry;
1853
1854/*
1855 * Adds a BlockDriverState to a simple queue for an atomic, transactional
1856 * reopen of multiple devices.
1857 *
1858 * bs_queue can either be an existing BlockReopenQueue that has had QSIMPLE_INIT
1859 * already performed, or alternatively may be NULL a new BlockReopenQueue will
1860 * be created and initialized. This newly created BlockReopenQueue should be
1861 * passed back in for subsequent calls that are intended to be of the same
1862 * atomic 'set'.
1863 *
1864 * bs is the BlockDriverState to add to the reopen queue.
1865 *
Kevin Wolf4d2cb092015-04-10 17:50:50 +02001866 * options contains the changed options for the associated bs
1867 * (the BlockReopenQueue takes ownership)
1868 *
Jeff Codye971aa12012-09-20 15:13:19 -04001869 * flags contains the open flags for the associated bs
1870 *
1871 * returns a pointer to bs_queue, which is either the newly allocated
1872 * bs_queue, or the existing bs_queue being used.
1873 *
1874 */
Kevin Wolf28518102015-05-08 17:07:31 +02001875static BlockReopenQueue *bdrv_reopen_queue_child(BlockReopenQueue *bs_queue,
1876 BlockDriverState *bs,
1877 QDict *options,
1878 int flags,
1879 const BdrvChildRole *role,
1880 QDict *parent_options,
1881 int parent_flags)
Jeff Codye971aa12012-09-20 15:13:19 -04001882{
1883 assert(bs != NULL);
1884
1885 BlockReopenQueueEntry *bs_entry;
Kevin Wolf67251a32015-04-09 18:54:04 +02001886 BdrvChild *child;
Kevin Wolf145f5982015-05-08 16:15:03 +02001887 QDict *old_options, *explicit_options;
Kevin Wolf67251a32015-04-09 18:54:04 +02001888
Jeff Codye971aa12012-09-20 15:13:19 -04001889 if (bs_queue == NULL) {
1890 bs_queue = g_new0(BlockReopenQueue, 1);
1891 QSIMPLEQ_INIT(bs_queue);
1892 }
1893
Kevin Wolf4d2cb092015-04-10 17:50:50 +02001894 if (!options) {
1895 options = qdict_new();
1896 }
1897
Kevin Wolf28518102015-05-08 17:07:31 +02001898 /*
1899 * Precedence of options:
1900 * 1. Explicitly passed in options (highest)
Kevin Wolf91a097e2015-05-08 17:49:53 +02001901 * 2. Set in flags (only for top level)
Kevin Wolf145f5982015-05-08 16:15:03 +02001902 * 3. Retained from explicitly set options of bs
Kevin Wolf8e2160e2015-04-29 17:29:39 +02001903 * 4. Inherited from parent node
Kevin Wolf28518102015-05-08 17:07:31 +02001904 * 5. Retained from effective options of bs
1905 */
1906
Kevin Wolf91a097e2015-05-08 17:49:53 +02001907 if (!parent_options) {
1908 /*
1909 * Any setting represented by flags is always updated. If the
1910 * corresponding QDict option is set, it takes precedence. Otherwise
1911 * the flag is translated into a QDict option. The old setting of bs is
1912 * not considered.
1913 */
1914 update_options_from_flags(options, flags);
1915 }
1916
Kevin Wolf145f5982015-05-08 16:15:03 +02001917 /* Old explicitly set values (don't overwrite by inherited value) */
1918 old_options = qdict_clone_shallow(bs->explicit_options);
1919 bdrv_join_options(bs, options, old_options);
1920 QDECREF(old_options);
1921
1922 explicit_options = qdict_clone_shallow(options);
1923
Kevin Wolf28518102015-05-08 17:07:31 +02001924 /* Inherit from parent node */
1925 if (parent_options) {
1926 assert(!flags);
Kevin Wolf8e2160e2015-04-29 17:29:39 +02001927 role->inherit_options(&flags, options, parent_flags, parent_options);
Kevin Wolf28518102015-05-08 17:07:31 +02001928 }
1929
1930 /* Old values are used for options that aren't set yet */
Kevin Wolf4d2cb092015-04-10 17:50:50 +02001931 old_options = qdict_clone_shallow(bs->options);
Kevin Wolfcddff5b2015-11-16 16:43:27 +01001932 bdrv_join_options(bs, options, old_options);
Kevin Wolf4d2cb092015-04-10 17:50:50 +02001933 QDECREF(old_options);
1934
Kevin Wolff1f25a22014-04-25 19:04:55 +02001935 /* bdrv_open() masks this flag out */
1936 flags &= ~BDRV_O_PROTOCOL;
1937
Kevin Wolf67251a32015-04-09 18:54:04 +02001938 QLIST_FOREACH(child, &bs->children, next) {
Kevin Wolf4c9dfe52015-05-08 15:14:15 +02001939 QDict *new_child_options;
1940 char *child_key_dot;
Kevin Wolf67251a32015-04-09 18:54:04 +02001941
Kevin Wolf4c9dfe52015-05-08 15:14:15 +02001942 /* reopen can only change the options of block devices that were
1943 * implicitly created and inherited options. For other (referenced)
1944 * block devices, a syntax like "backing.foo" results in an error. */
Kevin Wolf67251a32015-04-09 18:54:04 +02001945 if (child->bs->inherits_from != bs) {
1946 continue;
1947 }
1948
Kevin Wolf4c9dfe52015-05-08 15:14:15 +02001949 child_key_dot = g_strdup_printf("%s.", child->name);
1950 qdict_extract_subqdict(options, &new_child_options, child_key_dot);
1951 g_free(child_key_dot);
1952
Kevin Wolf28518102015-05-08 17:07:31 +02001953 bdrv_reopen_queue_child(bs_queue, child->bs, new_child_options, 0,
1954 child->role, options, flags);
Jeff Codye971aa12012-09-20 15:13:19 -04001955 }
1956
1957 bs_entry = g_new0(BlockReopenQueueEntry, 1);
1958 QSIMPLEQ_INSERT_TAIL(bs_queue, bs_entry, entry);
1959
1960 bs_entry->state.bs = bs;
Kevin Wolf4d2cb092015-04-10 17:50:50 +02001961 bs_entry->state.options = options;
Kevin Wolf145f5982015-05-08 16:15:03 +02001962 bs_entry->state.explicit_options = explicit_options;
Jeff Codye971aa12012-09-20 15:13:19 -04001963 bs_entry->state.flags = flags;
1964
1965 return bs_queue;
1966}
1967
Kevin Wolf28518102015-05-08 17:07:31 +02001968BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue,
1969 BlockDriverState *bs,
1970 QDict *options, int flags)
1971{
1972 return bdrv_reopen_queue_child(bs_queue, bs, options, flags,
1973 NULL, NULL, 0);
1974}
1975
Jeff Codye971aa12012-09-20 15:13:19 -04001976/*
1977 * Reopen multiple BlockDriverStates atomically & transactionally.
1978 *
1979 * The queue passed in (bs_queue) must have been built up previous
1980 * via bdrv_reopen_queue().
1981 *
1982 * Reopens all BDS specified in the queue, with the appropriate
1983 * flags. All devices are prepared for reopen, and failure of any
1984 * device will cause all device changes to be abandonded, and intermediate
1985 * data cleaned up.
1986 *
1987 * If all devices prepare successfully, then the changes are committed
1988 * to all devices.
1989 *
1990 */
1991int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp)
1992{
1993 int ret = -1;
1994 BlockReopenQueueEntry *bs_entry, *next;
1995 Error *local_err = NULL;
1996
1997 assert(bs_queue != NULL);
1998
1999 bdrv_drain_all();
2000
2001 QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) {
2002 if (bdrv_reopen_prepare(&bs_entry->state, bs_queue, &local_err)) {
2003 error_propagate(errp, local_err);
2004 goto cleanup;
2005 }
2006 bs_entry->prepared = true;
2007 }
2008
2009 /* If we reach this point, we have success and just need to apply the
2010 * changes
2011 */
2012 QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) {
2013 bdrv_reopen_commit(&bs_entry->state);
2014 }
2015
2016 ret = 0;
2017
2018cleanup:
2019 QSIMPLEQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) {
2020 if (ret && bs_entry->prepared) {
2021 bdrv_reopen_abort(&bs_entry->state);
Kevin Wolf145f5982015-05-08 16:15:03 +02002022 } else if (ret) {
2023 QDECREF(bs_entry->state.explicit_options);
Jeff Codye971aa12012-09-20 15:13:19 -04002024 }
Kevin Wolf4d2cb092015-04-10 17:50:50 +02002025 QDECREF(bs_entry->state.options);
Jeff Codye971aa12012-09-20 15:13:19 -04002026 g_free(bs_entry);
2027 }
2028 g_free(bs_queue);
2029 return ret;
2030}
2031
2032
2033/* Reopen a single BlockDriverState with the specified flags. */
2034int bdrv_reopen(BlockDriverState *bs, int bdrv_flags, Error **errp)
2035{
2036 int ret = -1;
2037 Error *local_err = NULL;
Kevin Wolf4d2cb092015-04-10 17:50:50 +02002038 BlockReopenQueue *queue = bdrv_reopen_queue(NULL, bs, NULL, bdrv_flags);
Jeff Codye971aa12012-09-20 15:13:19 -04002039
2040 ret = bdrv_reopen_multiple(queue, &local_err);
2041 if (local_err != NULL) {
2042 error_propagate(errp, local_err);
2043 }
2044 return ret;
2045}
2046
2047
2048/*
2049 * Prepares a BlockDriverState for reopen. All changes are staged in the
2050 * 'opaque' field of the BDRVReopenState, which is used and allocated by
2051 * the block driver layer .bdrv_reopen_prepare()
2052 *
2053 * bs is the BlockDriverState to reopen
2054 * flags are the new open flags
2055 * queue is the reopen queue
2056 *
2057 * Returns 0 on success, non-zero on error. On error errp will be set
2058 * as well.
2059 *
2060 * On failure, bdrv_reopen_abort() will be called to clean up any data.
2061 * It is the responsibility of the caller to then call the abort() or
2062 * commit() for any other BDS that have been left in a prepare() state
2063 *
2064 */
2065int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue,
2066 Error **errp)
2067{
2068 int ret = -1;
2069 Error *local_err = NULL;
2070 BlockDriver *drv;
Kevin Wolfccf9dc02015-05-08 17:24:56 +02002071 QemuOpts *opts;
2072 const char *value;
Jeff Codye971aa12012-09-20 15:13:19 -04002073
2074 assert(reopen_state != NULL);
2075 assert(reopen_state->bs->drv != NULL);
2076 drv = reopen_state->bs->drv;
2077
Kevin Wolfccf9dc02015-05-08 17:24:56 +02002078 /* Process generic block layer options */
2079 opts = qemu_opts_create(&bdrv_runtime_opts, NULL, 0, &error_abort);
2080 qemu_opts_absorb_qdict(opts, reopen_state->options, &local_err);
2081 if (local_err) {
2082 error_propagate(errp, local_err);
2083 ret = -EINVAL;
2084 goto error;
2085 }
2086
Kevin Wolf91a097e2015-05-08 17:49:53 +02002087 update_flags_from_options(&reopen_state->flags, opts);
2088
Kevin Wolfccf9dc02015-05-08 17:24:56 +02002089 /* node-name and driver must be unchanged. Put them back into the QDict, so
2090 * that they are checked at the end of this function. */
2091 value = qemu_opt_get(opts, "node-name");
2092 if (value) {
2093 qdict_put(reopen_state->options, "node-name", qstring_from_str(value));
2094 }
2095
2096 value = qemu_opt_get(opts, "driver");
2097 if (value) {
2098 qdict_put(reopen_state->options, "driver", qstring_from_str(value));
2099 }
2100
Jeff Codye971aa12012-09-20 15:13:19 -04002101 /* if we are to stay read-only, do not allow permission change
2102 * to r/w */
2103 if (!(reopen_state->bs->open_flags & BDRV_O_ALLOW_RDWR) &&
2104 reopen_state->flags & BDRV_O_RDWR) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03002105 error_setg(errp, "Node '%s' is read only",
2106 bdrv_get_device_or_node_name(reopen_state->bs));
Jeff Codye971aa12012-09-20 15:13:19 -04002107 goto error;
2108 }
2109
2110
2111 ret = bdrv_flush(reopen_state->bs);
2112 if (ret) {
Eric Blake455b0fd2015-11-10 23:51:20 -07002113 error_setg_errno(errp, -ret, "Error flushing drive");
Jeff Codye971aa12012-09-20 15:13:19 -04002114 goto error;
2115 }
2116
2117 if (drv->bdrv_reopen_prepare) {
2118 ret = drv->bdrv_reopen_prepare(reopen_state, queue, &local_err);
2119 if (ret) {
2120 if (local_err != NULL) {
2121 error_propagate(errp, local_err);
2122 } else {
Luiz Capitulinod8b68952013-06-10 11:29:27 -04002123 error_setg(errp, "failed while preparing to reopen image '%s'",
2124 reopen_state->bs->filename);
Jeff Codye971aa12012-09-20 15:13:19 -04002125 }
2126 goto error;
2127 }
2128 } else {
2129 /* It is currently mandatory to have a bdrv_reopen_prepare()
2130 * handler for each supported drv. */
Alberto Garcia81e5f782015-04-08 12:29:19 +03002131 error_setg(errp, "Block format '%s' used by node '%s' "
2132 "does not support reopening files", drv->format_name,
2133 bdrv_get_device_or_node_name(reopen_state->bs));
Jeff Codye971aa12012-09-20 15:13:19 -04002134 ret = -1;
2135 goto error;
2136 }
2137
Kevin Wolf4d2cb092015-04-10 17:50:50 +02002138 /* Options that are not handled are only okay if they are unchanged
2139 * compared to the old state. It is expected that some options are only
2140 * used for the initial open, but not reopen (e.g. filename) */
2141 if (qdict_size(reopen_state->options)) {
2142 const QDictEntry *entry = qdict_first(reopen_state->options);
2143
2144 do {
2145 QString *new_obj = qobject_to_qstring(entry->value);
2146 const char *new = qstring_get_str(new_obj);
2147 const char *old = qdict_get_try_str(reopen_state->bs->options,
2148 entry->key);
2149
2150 if (!old || strcmp(new, old)) {
2151 error_setg(errp, "Cannot change the option '%s'", entry->key);
2152 ret = -EINVAL;
2153 goto error;
2154 }
2155 } while ((entry = qdict_next(reopen_state->options, entry)));
2156 }
2157
Jeff Codye971aa12012-09-20 15:13:19 -04002158 ret = 0;
2159
2160error:
Kevin Wolfccf9dc02015-05-08 17:24:56 +02002161 qemu_opts_del(opts);
Jeff Codye971aa12012-09-20 15:13:19 -04002162 return ret;
2163}
2164
2165/*
2166 * Takes the staged changes for the reopen from bdrv_reopen_prepare(), and
2167 * makes them final by swapping the staging BlockDriverState contents into
2168 * the active BlockDriverState contents.
2169 */
2170void bdrv_reopen_commit(BDRVReopenState *reopen_state)
2171{
2172 BlockDriver *drv;
2173
2174 assert(reopen_state != NULL);
2175 drv = reopen_state->bs->drv;
2176 assert(drv != NULL);
2177
2178 /* If there are any driver level actions to take */
2179 if (drv->bdrv_reopen_commit) {
2180 drv->bdrv_reopen_commit(reopen_state);
2181 }
2182
2183 /* set BDS specific flags now */
Kevin Wolf145f5982015-05-08 16:15:03 +02002184 QDECREF(reopen_state->bs->explicit_options);
2185
2186 reopen_state->bs->explicit_options = reopen_state->explicit_options;
Jeff Codye971aa12012-09-20 15:13:19 -04002187 reopen_state->bs->open_flags = reopen_state->flags;
Jeff Codye971aa12012-09-20 15:13:19 -04002188 reopen_state->bs->read_only = !(reopen_state->flags & BDRV_O_RDWR);
Kevin Wolf355ef4a2013-12-11 20:14:09 +01002189
Kevin Wolf3baca892014-07-16 17:48:16 +02002190 bdrv_refresh_limits(reopen_state->bs, NULL);
Jeff Codye971aa12012-09-20 15:13:19 -04002191}
2192
2193/*
2194 * Abort the reopen, and delete and free the staged changes in
2195 * reopen_state
2196 */
2197void bdrv_reopen_abort(BDRVReopenState *reopen_state)
2198{
2199 BlockDriver *drv;
2200
2201 assert(reopen_state != NULL);
2202 drv = reopen_state->bs->drv;
2203 assert(drv != NULL);
2204
2205 if (drv->bdrv_reopen_abort) {
2206 drv->bdrv_reopen_abort(reopen_state);
2207 }
Kevin Wolf145f5982015-05-08 16:15:03 +02002208
2209 QDECREF(reopen_state->explicit_options);
Jeff Codye971aa12012-09-20 15:13:19 -04002210}
2211
2212
Max Reitz64dff522016-01-29 16:36:10 +01002213static void bdrv_close(BlockDriverState *bs)
bellardfc01f7e2003-06-30 10:03:06 +00002214{
Max Reitz33384422014-06-20 21:57:33 +02002215 BdrvAioNotifier *ban, *ban_next;
2216
Max Reitzca9bd242016-01-29 16:36:14 +01002217 assert(!bs->job);
Max Reitz30f55fb2016-05-17 16:41:32 +02002218 assert(!bs->refcnt);
Alberto Garcia99b7e772015-09-25 16:41:44 +03002219
Paolo Bonzinifc272912015-12-23 11:48:24 +01002220 bdrv_drained_begin(bs); /* complete I/O */
Stefan Hajnoczi58fda172013-07-02 15:36:25 +02002221 bdrv_flush(bs);
Fam Zheng53ec73e2015-05-29 18:53:14 +08002222 bdrv_drain(bs); /* in case flush left pending I/O */
Paolo Bonzinifc272912015-12-23 11:48:24 +01002223
Max Reitzc5acdc92016-01-29 16:36:01 +01002224 bdrv_release_named_dirty_bitmaps(bs);
2225 assert(QLIST_EMPTY(&bs->dirty_bitmaps));
2226
Paolo Bonzini3cbc0022012-10-19 11:36:48 +02002227 if (bs->drv) {
Kevin Wolf6e93e7c2015-04-08 13:49:41 +02002228 BdrvChild *child, *next;
2229
Kevin Wolf9a7dedb2015-06-16 10:58:20 +02002230 bs->drv->bdrv_close(bs);
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02002231 bs->drv = NULL;
Kevin Wolf9a7dedb2015-06-16 10:58:20 +02002232
Kevin Wolf5db15a52015-09-14 15:33:33 +02002233 bdrv_set_backing_hd(bs, NULL);
Kevin Wolf9a7dedb2015-06-16 10:58:20 +02002234
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02002235 if (bs->file != NULL) {
2236 bdrv_unref_child(bs, bs->file);
2237 bs->file = NULL;
2238 }
2239
Kevin Wolf6e93e7c2015-04-08 13:49:41 +02002240 QLIST_FOREACH_SAFE(child, &bs->children, next, next) {
Kevin Wolf33a60402015-06-15 13:51:04 +02002241 /* TODO Remove bdrv_unref() from drivers' close function and use
2242 * bdrv_unref_child() here */
Kevin Wolfbddcec32015-04-09 18:47:50 +02002243 if (child->bs->inherits_from == bs) {
2244 child->bs->inherits_from = NULL;
2245 }
Kevin Wolf33a60402015-06-15 13:51:04 +02002246 bdrv_detach_child(child);
Kevin Wolf6e93e7c2015-04-08 13:49:41 +02002247 }
2248
Anthony Liguori7267c092011-08-20 22:09:37 -05002249 g_free(bs->opaque);
bellardea2384d2004-08-01 21:59:26 +00002250 bs->opaque = NULL;
Stefan Hajnoczi53fec9d2011-11-28 16:08:47 +00002251 bs->copy_on_read = 0;
Paolo Bonzinia275fa42012-05-08 16:51:43 +02002252 bs->backing_file[0] = '\0';
2253 bs->backing_format[0] = '\0';
Paolo Bonzini64058752012-05-08 16:51:49 +02002254 bs->total_sectors = 0;
Eric Blake54115412016-06-23 16:37:26 -06002255 bs->encrypted = false;
2256 bs->valid_key = false;
2257 bs->sg = false;
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01002258 QDECREF(bs->options);
Kevin Wolf145f5982015-05-08 16:15:03 +02002259 QDECREF(bs->explicit_options);
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01002260 bs->options = NULL;
Max Reitz91af7012014-07-18 20:24:56 +02002261 QDECREF(bs->full_open_options);
2262 bs->full_open_options = NULL;
bellardb3380822004-03-14 21:38:54 +00002263 }
Zhi Yong Wu98f90db2011-11-08 13:00:14 +08002264
Max Reitz33384422014-06-20 21:57:33 +02002265 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) {
2266 g_free(ban);
2267 }
2268 QLIST_INIT(&bs->aio_notifiers);
Paolo Bonzinifc272912015-12-23 11:48:24 +01002269 bdrv_drained_end(bs);
bellardb3380822004-03-14 21:38:54 +00002270}
2271
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +09002272void bdrv_close_all(void)
2273{
Kevin Wolfa1a2af02016-04-08 18:26:37 +02002274 block_job_cancel_sync_all();
Kevin Wolfcd7fca92016-07-06 11:22:39 +02002275 nbd_export_close_all();
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +09002276
Max Reitzca9bd242016-01-29 16:36:14 +01002277 /* Drop references from requests still in flight, such as canceled block
2278 * jobs whose AIO context has not been polled yet */
2279 bdrv_drain_all();
Stefan Hajnoczied78cda2014-05-08 16:34:35 +02002280
Max Reitzca9bd242016-01-29 16:36:14 +01002281 blk_remove_all_bs();
2282 blockdev_close_all_bdrv_states();
2283
Kevin Wolfa1a2af02016-04-08 18:26:37 +02002284 assert(QTAILQ_EMPTY(&all_bdrv_states));
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +09002285}
2286
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002287static void change_parent_backing_link(BlockDriverState *from,
2288 BlockDriverState *to)
2289{
Max Reitz9bd910e2016-06-10 20:57:46 +02002290 BdrvChild *c, *next, *to_c;
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002291
2292 QLIST_FOREACH_SAFE(c, &from->parents, next_parent, next) {
Max Reitz9bd910e2016-06-10 20:57:46 +02002293 if (c->role == &child_backing) {
2294 /* @from is generally not allowed to be a backing file, except for
2295 * when @to is the overlay. In that case, @from may not be replaced
2296 * by @to as @to's backing node. */
2297 QLIST_FOREACH(to_c, &to->children, next) {
2298 if (to_c == c) {
2299 break;
2300 }
2301 }
2302 if (to_c) {
2303 continue;
2304 }
2305 }
2306
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002307 assert(c->role != &child_backing);
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002308 bdrv_ref(to);
Kevin Wolfe9740bc2016-05-23 15:52:26 +02002309 bdrv_replace_child(c, to);
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002310 bdrv_unref(from);
2311 }
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002312}
2313
Jeff Cody8802d1f2012-02-28 15:54:06 -05002314/*
2315 * Add new bs contents at the top of an image chain while the chain is
2316 * live, while keeping required fields on the top layer.
2317 *
2318 * This will modify the BlockDriverState fields, and swap contents
2319 * between bs_new and bs_top. Both bs_new and bs_top are modified.
2320 *
Markus Armbrusterbfb197e2014-10-07 13:59:11 +02002321 * bs_new must not be attached to a BlockBackend.
Jeff Codyf6801b82012-03-27 16:30:19 -04002322 *
Jeff Cody8802d1f2012-02-28 15:54:06 -05002323 * This function does not create any image files.
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002324 *
2325 * bdrv_append() takes ownership of a bs_new reference and unrefs it because
2326 * that's what the callers commonly need. bs_new will be referenced by the old
2327 * parents of bs_top after bdrv_append() returns. If the caller needs to keep a
2328 * reference of its own, it must call bdrv_ref().
Jeff Cody8802d1f2012-02-28 15:54:06 -05002329 */
2330void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top)
2331{
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002332 assert(!bdrv_requests_pending(bs_top));
2333 assert(!bdrv_requests_pending(bs_new));
Jeff Cody8802d1f2012-02-28 15:54:06 -05002334
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002335 bdrv_ref(bs_top);
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002336
Kevin Wolf27ccdd52016-03-21 12:56:44 +01002337 change_parent_backing_link(bs_top, bs_new);
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002338 bdrv_set_backing_hd(bs_new, bs_top);
2339 bdrv_unref(bs_top);
2340
2341 /* bs_new is now referenced by its new parents, we don't need the
2342 * additional reference any more. */
2343 bdrv_unref(bs_new);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002344}
2345
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02002346void bdrv_replace_in_backing_chain(BlockDriverState *old, BlockDriverState *new)
2347{
2348 assert(!bdrv_requests_pending(old));
2349 assert(!bdrv_requests_pending(new));
2350
2351 bdrv_ref(old);
2352
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02002353 change_parent_backing_link(old, new);
2354
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02002355 bdrv_unref(old);
2356}
2357
Fam Zheng4f6fd342013-08-23 09:14:47 +08002358static void bdrv_delete(BlockDriverState *bs)
bellardb3380822004-03-14 21:38:54 +00002359{
Paolo Bonzini3e914652012-03-30 13:17:11 +02002360 assert(!bs->job);
Fam Zheng3718d8a2014-05-23 21:29:43 +08002361 assert(bdrv_op_blocker_is_empty(bs));
Fam Zheng4f6fd342013-08-23 09:14:47 +08002362 assert(!bs->refcnt);
Markus Armbruster18846de2010-06-29 16:58:30 +02002363
Stefan Hajnoczie1b5c522013-06-27 15:32:26 +02002364 bdrv_close(bs);
2365
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01002366 /* remove from list, if necessary */
Kevin Wolf63eaaae2016-03-18 10:46:57 +01002367 if (bs->node_name[0] != '\0') {
2368 QTAILQ_REMOVE(&graph_bdrv_states, bs, node_list);
2369 }
Max Reitz2c1d04e2016-01-29 16:36:11 +01002370 QTAILQ_REMOVE(&all_bdrv_states, bs, bs_list);
2371
Anthony Liguori7267c092011-08-20 22:09:37 -05002372 g_free(bs);
bellardfc01f7e2003-06-30 10:03:06 +00002373}
2374
aliguorie97fc192009-04-21 23:11:50 +00002375/*
2376 * Run consistency checks on an image
2377 *
Kevin Wolfe076f332010-06-29 11:43:13 +02002378 * Returns 0 if the check could be completed (it doesn't mean that the image is
Stefan Weila1c72732011-04-28 17:20:38 +02002379 * free of errors) or -errno when an internal error occurred. The results of the
Kevin Wolfe076f332010-06-29 11:43:13 +02002380 * check are stored in res.
aliguorie97fc192009-04-21 23:11:50 +00002381 */
Kevin Wolf4534ff52012-05-11 16:07:02 +02002382int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix)
aliguorie97fc192009-04-21 23:11:50 +00002383{
Max Reitz908bcd52014-08-07 22:47:55 +02002384 if (bs->drv == NULL) {
2385 return -ENOMEDIUM;
2386 }
aliguorie97fc192009-04-21 23:11:50 +00002387 if (bs->drv->bdrv_check == NULL) {
2388 return -ENOTSUP;
2389 }
2390
Kevin Wolfe076f332010-06-29 11:43:13 +02002391 memset(res, 0, sizeof(*res));
Kevin Wolf4534ff52012-05-11 16:07:02 +02002392 return bs->drv->bdrv_check(bs, res, fix);
aliguorie97fc192009-04-21 23:11:50 +00002393}
2394
Kevin Wolf756e6732010-01-12 12:55:17 +01002395/*
2396 * Return values:
2397 * 0 - success
2398 * -EINVAL - backing format specified, but no file
2399 * -ENOSPC - can't update the backing file because no space is left in the
2400 * image file header
2401 * -ENOTSUP - format driver doesn't support changing the backing file
2402 */
2403int bdrv_change_backing_file(BlockDriverState *bs,
2404 const char *backing_file, const char *backing_fmt)
2405{
2406 BlockDriver *drv = bs->drv;
Paolo Bonzini469ef352012-04-12 14:01:02 +02002407 int ret;
Kevin Wolf756e6732010-01-12 12:55:17 +01002408
Paolo Bonzini5f377792012-04-12 14:01:01 +02002409 /* Backing file format doesn't make sense without a backing file */
2410 if (backing_fmt && !backing_file) {
2411 return -EINVAL;
2412 }
2413
Kevin Wolf756e6732010-01-12 12:55:17 +01002414 if (drv->bdrv_change_backing_file != NULL) {
Paolo Bonzini469ef352012-04-12 14:01:02 +02002415 ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
Kevin Wolf756e6732010-01-12 12:55:17 +01002416 } else {
Paolo Bonzini469ef352012-04-12 14:01:02 +02002417 ret = -ENOTSUP;
Kevin Wolf756e6732010-01-12 12:55:17 +01002418 }
Paolo Bonzini469ef352012-04-12 14:01:02 +02002419
2420 if (ret == 0) {
2421 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
2422 pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
2423 }
2424 return ret;
Kevin Wolf756e6732010-01-12 12:55:17 +01002425}
2426
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002427/*
2428 * Finds the image layer in the chain that has 'bs' as its backing file.
2429 *
2430 * active is the current topmost image.
2431 *
2432 * Returns NULL if bs is not found in active's image chain,
2433 * or if active == bs.
Jeff Cody4caf0fc2014-06-25 15:35:26 -04002434 *
2435 * Returns the bottommost base image if bs == NULL.
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002436 */
2437BlockDriverState *bdrv_find_overlay(BlockDriverState *active,
2438 BlockDriverState *bs)
2439{
Kevin Wolf760e0062015-06-17 14:55:21 +02002440 while (active && bs != backing_bs(active)) {
2441 active = backing_bs(active);
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002442 }
2443
Jeff Cody4caf0fc2014-06-25 15:35:26 -04002444 return active;
2445}
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002446
Jeff Cody4caf0fc2014-06-25 15:35:26 -04002447/* Given a BDS, searches for the base layer. */
2448BlockDriverState *bdrv_find_base(BlockDriverState *bs)
2449{
2450 return bdrv_find_overlay(bs, NULL);
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002451}
2452
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002453/*
2454 * Drops images above 'base' up to and including 'top', and sets the image
2455 * above 'top' to have base as its backing file.
2456 *
2457 * Requires that the overlay to 'top' is opened r/w, so that the backing file
2458 * information in 'bs' can be properly updated.
2459 *
2460 * E.g., this will convert the following chain:
2461 * bottom <- base <- intermediate <- top <- active
2462 *
2463 * to
2464 *
2465 * bottom <- base <- active
2466 *
2467 * It is allowed for bottom==base, in which case it converts:
2468 *
2469 * base <- intermediate <- top <- active
2470 *
2471 * to
2472 *
2473 * base <- active
2474 *
Jeff Cody54e26902014-06-25 15:40:10 -04002475 * If backing_file_str is non-NULL, it will be used when modifying top's
2476 * overlay image metadata.
2477 *
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002478 * Error conditions:
2479 * if active == top, that is considered an error
2480 *
2481 */
2482int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top,
Jeff Cody54e26902014-06-25 15:40:10 -04002483 BlockDriverState *base, const char *backing_file_str)
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002484{
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002485 BlockDriverState *new_top_bs = NULL;
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002486 int ret = -EIO;
2487
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002488 if (!top->drv || !base->drv) {
2489 goto exit;
2490 }
2491
2492 new_top_bs = bdrv_find_overlay(active, top);
2493
2494 if (new_top_bs == NULL) {
2495 /* we could not find the image above 'top', this is an error */
2496 goto exit;
2497 }
2498
Kevin Wolf760e0062015-06-17 14:55:21 +02002499 /* special case of new_top_bs->backing->bs already pointing to base - nothing
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002500 * to do, no intermediate images */
Kevin Wolf760e0062015-06-17 14:55:21 +02002501 if (backing_bs(new_top_bs) == base) {
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002502 ret = 0;
2503 goto exit;
2504 }
2505
Kevin Wolf5db15a52015-09-14 15:33:33 +02002506 /* Make sure that base is in the backing chain of top */
2507 if (!bdrv_chain_contains(top, base)) {
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002508 goto exit;
2509 }
2510
2511 /* success - we can delete the intermediate states, and link top->base */
Kevin Wolf5db15a52015-09-14 15:33:33 +02002512 backing_file_str = backing_file_str ? backing_file_str : base->filename;
Jeff Cody54e26902014-06-25 15:40:10 -04002513 ret = bdrv_change_backing_file(new_top_bs, backing_file_str,
Kevin Wolf5db15a52015-09-14 15:33:33 +02002514 base->drv ? base->drv->format_name : "");
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002515 if (ret) {
2516 goto exit;
2517 }
Kevin Wolf5db15a52015-09-14 15:33:33 +02002518 bdrv_set_backing_hd(new_top_bs, base);
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002519
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002520 ret = 0;
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002521exit:
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002522 return ret;
2523}
2524
bellard83f64092006-08-01 16:21:11 +00002525/**
bellard83f64092006-08-01 16:21:11 +00002526 * Truncate file to 'offset' bytes (needed only for file protocols)
2527 */
2528int bdrv_truncate(BlockDriverState *bs, int64_t offset)
2529{
2530 BlockDriver *drv = bs->drv;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01002531 int ret;
bellard83f64092006-08-01 16:21:11 +00002532 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002533 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +00002534 if (!drv->bdrv_truncate)
2535 return -ENOTSUP;
Naphtali Sprei59f26892009-10-26 16:25:16 +02002536 if (bs->read_only)
2537 return -EACCES;
Jeff Cody9c75e162014-06-25 16:55:30 -04002538
Stefan Hajnoczi51762282010-04-19 16:56:41 +01002539 ret = drv->bdrv_truncate(bs, offset);
2540 if (ret == 0) {
2541 ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
John Snowce1ffea2015-04-17 19:50:03 -04002542 bdrv_dirty_bitmap_truncate(bs);
Kevin Wolf5c8cab42016-02-24 15:13:35 +01002543 bdrv_parent_cb_resize(bs);
Evgeny Yakovlev3ff2f672016-07-18 22:39:52 +03002544 ++bs->write_gen;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01002545 }
2546 return ret;
bellard83f64092006-08-01 16:21:11 +00002547}
2548
2549/**
Fam Zheng4a1d5e12011-07-12 19:56:39 +08002550 * Length of a allocated file in bytes. Sparse files are counted by actual
2551 * allocated space. Return < 0 if error or unknown.
2552 */
2553int64_t bdrv_get_allocated_file_size(BlockDriverState *bs)
2554{
2555 BlockDriver *drv = bs->drv;
2556 if (!drv) {
2557 return -ENOMEDIUM;
2558 }
2559 if (drv->bdrv_get_allocated_file_size) {
2560 return drv->bdrv_get_allocated_file_size(bs);
2561 }
2562 if (bs->file) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02002563 return bdrv_get_allocated_file_size(bs->file->bs);
Fam Zheng4a1d5e12011-07-12 19:56:39 +08002564 }
2565 return -ENOTSUP;
2566}
2567
2568/**
Markus Armbruster65a9bb22014-06-26 13:23:17 +02002569 * Return number of sectors on success, -errno on error.
bellard83f64092006-08-01 16:21:11 +00002570 */
Markus Armbruster65a9bb22014-06-26 13:23:17 +02002571int64_t bdrv_nb_sectors(BlockDriverState *bs)
bellard83f64092006-08-01 16:21:11 +00002572{
2573 BlockDriver *drv = bs->drv;
Markus Armbruster65a9bb22014-06-26 13:23:17 +02002574
bellard83f64092006-08-01 16:21:11 +00002575 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002576 return -ENOMEDIUM;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01002577
Kevin Wolfb94a2612013-10-29 12:18:58 +01002578 if (drv->has_variable_length) {
2579 int ret = refresh_total_sectors(bs, bs->total_sectors);
2580 if (ret < 0) {
2581 return ret;
Stefan Hajnoczi46a4e4e2011-03-29 20:04:41 +01002582 }
bellard83f64092006-08-01 16:21:11 +00002583 }
Markus Armbruster65a9bb22014-06-26 13:23:17 +02002584 return bs->total_sectors;
2585}
2586
2587/**
2588 * Return length in bytes on success, -errno on error.
2589 * The length is always a multiple of BDRV_SECTOR_SIZE.
2590 */
2591int64_t bdrv_getlength(BlockDriverState *bs)
2592{
2593 int64_t ret = bdrv_nb_sectors(bs);
2594
Fam Zheng4a9c9ea2015-05-15 16:36:05 +08002595 ret = ret > INT64_MAX / BDRV_SECTOR_SIZE ? -EFBIG : ret;
Markus Armbruster65a9bb22014-06-26 13:23:17 +02002596 return ret < 0 ? ret : ret * BDRV_SECTOR_SIZE;
bellardfc01f7e2003-06-30 10:03:06 +00002597}
2598
bellard19cb3732006-08-19 11:45:59 +00002599/* return 0 as number of sectors if no device present or error */
ths96b8f132007-12-17 01:35:20 +00002600void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
bellardfc01f7e2003-06-30 10:03:06 +00002601{
Markus Armbruster65a9bb22014-06-26 13:23:17 +02002602 int64_t nb_sectors = bdrv_nb_sectors(bs);
2603
2604 *nb_sectors_ptr = nb_sectors < 0 ? 0 : nb_sectors;
bellardfc01f7e2003-06-30 10:03:06 +00002605}
bellardcf989512004-02-16 21:56:36 +00002606
Eric Blake54115412016-06-23 16:37:26 -06002607bool bdrv_is_read_only(BlockDriverState *bs)
bellardb3380822004-03-14 21:38:54 +00002608{
2609 return bs->read_only;
2610}
2611
Eric Blake54115412016-06-23 16:37:26 -06002612bool bdrv_is_sg(BlockDriverState *bs)
ths985a03b2007-12-24 16:10:43 +00002613{
2614 return bs->sg;
2615}
2616
Eric Blake54115412016-06-23 16:37:26 -06002617bool bdrv_is_encrypted(BlockDriverState *bs)
bellardea2384d2004-08-01 21:59:26 +00002618{
Kevin Wolf760e0062015-06-17 14:55:21 +02002619 if (bs->backing && bs->backing->bs->encrypted) {
Eric Blake54115412016-06-23 16:37:26 -06002620 return true;
Kevin Wolf760e0062015-06-17 14:55:21 +02002621 }
bellardea2384d2004-08-01 21:59:26 +00002622 return bs->encrypted;
2623}
2624
Eric Blake54115412016-06-23 16:37:26 -06002625bool bdrv_key_required(BlockDriverState *bs)
aliguoric0f4ce72009-03-05 23:01:01 +00002626{
Kevin Wolf760e0062015-06-17 14:55:21 +02002627 BdrvChild *backing = bs->backing;
aliguoric0f4ce72009-03-05 23:01:01 +00002628
Kevin Wolf760e0062015-06-17 14:55:21 +02002629 if (backing && backing->bs->encrypted && !backing->bs->valid_key) {
Eric Blake54115412016-06-23 16:37:26 -06002630 return true;
Kevin Wolf760e0062015-06-17 14:55:21 +02002631 }
aliguoric0f4ce72009-03-05 23:01:01 +00002632 return (bs->encrypted && !bs->valid_key);
2633}
2634
bellardea2384d2004-08-01 21:59:26 +00002635int bdrv_set_key(BlockDriverState *bs, const char *key)
2636{
2637 int ret;
Kevin Wolf760e0062015-06-17 14:55:21 +02002638 if (bs->backing && bs->backing->bs->encrypted) {
2639 ret = bdrv_set_key(bs->backing->bs, key);
bellardea2384d2004-08-01 21:59:26 +00002640 if (ret < 0)
2641 return ret;
2642 if (!bs->encrypted)
2643 return 0;
2644 }
Shahar Havivifd04a2a2010-03-06 00:26:13 +02002645 if (!bs->encrypted) {
2646 return -EINVAL;
2647 } else if (!bs->drv || !bs->drv->bdrv_set_key) {
2648 return -ENOMEDIUM;
2649 }
aliguoric0f4ce72009-03-05 23:01:01 +00002650 ret = bs->drv->bdrv_set_key(bs, key);
aliguoribb5fc202009-03-05 23:01:15 +00002651 if (ret < 0) {
Eric Blake54115412016-06-23 16:37:26 -06002652 bs->valid_key = false;
aliguoribb5fc202009-03-05 23:01:15 +00002653 } else if (!bs->valid_key) {
Kevin Wolf5c8cab42016-02-24 15:13:35 +01002654 /* call the change callback now, we skipped it on open */
Eric Blake54115412016-06-23 16:37:26 -06002655 bs->valid_key = true;
Kevin Wolf5c8cab42016-02-24 15:13:35 +01002656 bdrv_parent_cb_change_media(bs, true);
aliguoribb5fc202009-03-05 23:01:15 +00002657 }
aliguoric0f4ce72009-03-05 23:01:01 +00002658 return ret;
bellardea2384d2004-08-01 21:59:26 +00002659}
2660
Markus Armbruster4d2855a2015-01-29 10:37:00 +01002661/*
2662 * Provide an encryption key for @bs.
2663 * If @key is non-null:
2664 * If @bs is not encrypted, fail.
2665 * Else if the key is invalid, fail.
2666 * Else set @bs's key to @key, replacing the existing key, if any.
2667 * If @key is null:
2668 * If @bs is encrypted and still lacks a key, fail.
2669 * Else do nothing.
2670 * On failure, store an error object through @errp if non-null.
2671 */
2672void bdrv_add_key(BlockDriverState *bs, const char *key, Error **errp)
2673{
2674 if (key) {
2675 if (!bdrv_is_encrypted(bs)) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03002676 error_setg(errp, "Node '%s' is not encrypted",
2677 bdrv_get_device_or_node_name(bs));
Markus Armbruster4d2855a2015-01-29 10:37:00 +01002678 } else if (bdrv_set_key(bs, key) < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002679 error_setg(errp, QERR_INVALID_PASSWORD);
Markus Armbruster4d2855a2015-01-29 10:37:00 +01002680 }
2681 } else {
2682 if (bdrv_key_required(bs)) {
Markus Armbrusterb1ca6392015-01-29 10:37:01 +01002683 error_set(errp, ERROR_CLASS_DEVICE_ENCRYPTED,
2684 "'%s' (%s) is encrypted",
Alberto Garcia81e5f782015-04-08 12:29:19 +03002685 bdrv_get_device_or_node_name(bs),
Markus Armbruster4d2855a2015-01-29 10:37:00 +01002686 bdrv_get_encrypted_filename(bs));
2687 }
2688 }
2689}
2690
Markus Armbrusterf8d6bba2012-06-13 10:11:48 +02002691const char *bdrv_get_format_name(BlockDriverState *bs)
bellardea2384d2004-08-01 21:59:26 +00002692{
Markus Armbrusterf8d6bba2012-06-13 10:11:48 +02002693 return bs->drv ? bs->drv->format_name : NULL;
bellardea2384d2004-08-01 21:59:26 +00002694}
2695
Stefan Hajnocziada42402014-08-27 12:08:55 +01002696static int qsort_strcmp(const void *a, const void *b)
2697{
2698 return strcmp(a, b);
2699}
2700
ths5fafdf22007-09-16 21:08:06 +00002701void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
bellardea2384d2004-08-01 21:59:26 +00002702 void *opaque)
2703{
2704 BlockDriver *drv;
Jeff Codye855e4f2014-04-28 18:29:54 -04002705 int count = 0;
Stefan Hajnocziada42402014-08-27 12:08:55 +01002706 int i;
Jeff Codye855e4f2014-04-28 18:29:54 -04002707 const char **formats = NULL;
bellardea2384d2004-08-01 21:59:26 +00002708
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +01002709 QLIST_FOREACH(drv, &bdrv_drivers, list) {
Jeff Codye855e4f2014-04-28 18:29:54 -04002710 if (drv->format_name) {
2711 bool found = false;
2712 int i = count;
2713 while (formats && i && !found) {
2714 found = !strcmp(formats[--i], drv->format_name);
2715 }
2716
2717 if (!found) {
Markus Armbruster5839e532014-08-19 10:31:08 +02002718 formats = g_renew(const char *, formats, count + 1);
Jeff Codye855e4f2014-04-28 18:29:54 -04002719 formats[count++] = drv->format_name;
Jeff Codye855e4f2014-04-28 18:29:54 -04002720 }
2721 }
bellardea2384d2004-08-01 21:59:26 +00002722 }
Stefan Hajnocziada42402014-08-27 12:08:55 +01002723
2724 qsort(formats, count, sizeof(formats[0]), qsort_strcmp);
2725
2726 for (i = 0; i < count; i++) {
2727 it(opaque, formats[i]);
2728 }
2729
Jeff Codye855e4f2014-04-28 18:29:54 -04002730 g_free(formats);
bellardea2384d2004-08-01 21:59:26 +00002731}
2732
Benoît Canetdc364f42014-01-23 21:31:32 +01002733/* This function is to find a node in the bs graph */
2734BlockDriverState *bdrv_find_node(const char *node_name)
2735{
2736 BlockDriverState *bs;
2737
2738 assert(node_name);
2739
2740 QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) {
2741 if (!strcmp(node_name, bs->node_name)) {
2742 return bs;
2743 }
2744 }
2745 return NULL;
2746}
2747
Benoît Canetc13163f2014-01-23 21:31:34 +01002748/* Put this QMP function here so it can access the static graph_bdrv_states. */
Alberto Garciad5a8ee62015-04-17 14:52:43 +03002749BlockDeviceInfoList *bdrv_named_nodes_list(Error **errp)
Benoît Canetc13163f2014-01-23 21:31:34 +01002750{
2751 BlockDeviceInfoList *list, *entry;
2752 BlockDriverState *bs;
2753
2754 list = NULL;
2755 QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) {
Kevin Wolfc83f9fb2016-03-03 11:37:48 +01002756 BlockDeviceInfo *info = bdrv_block_device_info(NULL, bs, errp);
Alberto Garciad5a8ee62015-04-17 14:52:43 +03002757 if (!info) {
2758 qapi_free_BlockDeviceInfoList(list);
2759 return NULL;
2760 }
Benoît Canetc13163f2014-01-23 21:31:34 +01002761 entry = g_malloc0(sizeof(*entry));
Alberto Garciad5a8ee62015-04-17 14:52:43 +03002762 entry->value = info;
Benoît Canetc13163f2014-01-23 21:31:34 +01002763 entry->next = list;
2764 list = entry;
2765 }
2766
2767 return list;
2768}
2769
Benoît Canet12d3ba82014-01-23 21:31:35 +01002770BlockDriverState *bdrv_lookup_bs(const char *device,
2771 const char *node_name,
2772 Error **errp)
2773{
Markus Armbruster7f06d472014-10-07 13:59:12 +02002774 BlockBackend *blk;
2775 BlockDriverState *bs;
Benoît Canet12d3ba82014-01-23 21:31:35 +01002776
Benoît Canet12d3ba82014-01-23 21:31:35 +01002777 if (device) {
Markus Armbruster7f06d472014-10-07 13:59:12 +02002778 blk = blk_by_name(device);
Benoît Canet12d3ba82014-01-23 21:31:35 +01002779
Markus Armbruster7f06d472014-10-07 13:59:12 +02002780 if (blk) {
Alberto Garcia9f4ed6f2015-10-26 16:46:49 +02002781 bs = blk_bs(blk);
2782 if (!bs) {
Max Reitz5433c242015-10-19 17:53:29 +02002783 error_setg(errp, "Device '%s' has no medium", device);
Max Reitz5433c242015-10-19 17:53:29 +02002784 }
2785
Alberto Garcia9f4ed6f2015-10-26 16:46:49 +02002786 return bs;
Benoît Canet12d3ba82014-01-23 21:31:35 +01002787 }
Benoît Canet12d3ba82014-01-23 21:31:35 +01002788 }
2789
Benoît Canetdd67fa52014-02-12 17:15:06 +01002790 if (node_name) {
2791 bs = bdrv_find_node(node_name);
Benoît Canet12d3ba82014-01-23 21:31:35 +01002792
Benoît Canetdd67fa52014-02-12 17:15:06 +01002793 if (bs) {
2794 return bs;
2795 }
Benoît Canet12d3ba82014-01-23 21:31:35 +01002796 }
2797
Benoît Canetdd67fa52014-02-12 17:15:06 +01002798 error_setg(errp, "Cannot find device=%s nor node_name=%s",
2799 device ? device : "",
2800 node_name ? node_name : "");
2801 return NULL;
Benoît Canet12d3ba82014-01-23 21:31:35 +01002802}
2803
Jeff Cody5a6684d2014-06-25 15:40:09 -04002804/* If 'base' is in the same chain as 'top', return true. Otherwise,
2805 * return false. If either argument is NULL, return false. */
2806bool bdrv_chain_contains(BlockDriverState *top, BlockDriverState *base)
2807{
2808 while (top && top != base) {
Kevin Wolf760e0062015-06-17 14:55:21 +02002809 top = backing_bs(top);
Jeff Cody5a6684d2014-06-25 15:40:09 -04002810 }
2811
2812 return top != NULL;
2813}
2814
Fam Zheng04df7652014-10-31 11:32:54 +08002815BlockDriverState *bdrv_next_node(BlockDriverState *bs)
2816{
2817 if (!bs) {
2818 return QTAILQ_FIRST(&graph_bdrv_states);
2819 }
2820 return QTAILQ_NEXT(bs, node_list);
2821}
2822
Fam Zheng20a9e772014-10-31 11:32:55 +08002823const char *bdrv_get_node_name(const BlockDriverState *bs)
2824{
2825 return bs->node_name;
2826}
2827
Kevin Wolf1f0c4612016-03-22 18:38:44 +01002828const char *bdrv_get_parent_name(const BlockDriverState *bs)
Kevin Wolf4c265bf2016-02-26 10:22:16 +01002829{
2830 BdrvChild *c;
2831 const char *name;
2832
2833 /* If multiple parents have a name, just pick the first one. */
2834 QLIST_FOREACH(c, &bs->parents, next_parent) {
2835 if (c->role->get_name) {
2836 name = c->role->get_name(c);
2837 if (name && *name) {
2838 return name;
2839 }
2840 }
2841 }
2842
2843 return NULL;
2844}
2845
Markus Armbruster7f06d472014-10-07 13:59:12 +02002846/* TODO check what callers really want: bs->node_name or blk_name() */
Markus Armbrusterbfb197e2014-10-07 13:59:11 +02002847const char *bdrv_get_device_name(const BlockDriverState *bs)
bellardea2384d2004-08-01 21:59:26 +00002848{
Kevin Wolf4c265bf2016-02-26 10:22:16 +01002849 return bdrv_get_parent_name(bs) ?: "";
bellardea2384d2004-08-01 21:59:26 +00002850}
2851
Alberto Garcia9b2aa842015-04-08 12:29:18 +03002852/* This can be used to identify nodes that might not have a device
2853 * name associated. Since node and device names live in the same
2854 * namespace, the result is unambiguous. The exception is if both are
2855 * absent, then this returns an empty (non-null) string. */
2856const char *bdrv_get_device_or_node_name(const BlockDriverState *bs)
2857{
Kevin Wolf4c265bf2016-02-26 10:22:16 +01002858 return bdrv_get_parent_name(bs) ?: bs->node_name;
Alberto Garcia9b2aa842015-04-08 12:29:18 +03002859}
2860
Markus Armbrusterc8433282012-06-05 16:49:24 +02002861int bdrv_get_flags(BlockDriverState *bs)
2862{
2863 return bs->open_flags;
2864}
2865
Peter Lieven3ac21622013-06-28 12:47:42 +02002866int bdrv_has_zero_init_1(BlockDriverState *bs)
2867{
2868 return 1;
2869}
2870
Kevin Wolff2feebb2010-04-14 17:30:35 +02002871int bdrv_has_zero_init(BlockDriverState *bs)
2872{
2873 assert(bs->drv);
2874
Paolo Bonzini11212d82013-09-04 19:00:27 +02002875 /* If BS is a copy on write image, it is initialized to
2876 the contents of the base image, which may not be zeroes. */
Kevin Wolf760e0062015-06-17 14:55:21 +02002877 if (bs->backing) {
Paolo Bonzini11212d82013-09-04 19:00:27 +02002878 return 0;
2879 }
Kevin Wolf336c1c12010-07-28 11:26:29 +02002880 if (bs->drv->bdrv_has_zero_init) {
2881 return bs->drv->bdrv_has_zero_init(bs);
Kevin Wolff2feebb2010-04-14 17:30:35 +02002882 }
2883
Peter Lieven3ac21622013-06-28 12:47:42 +02002884 /* safe default */
2885 return 0;
Kevin Wolff2feebb2010-04-14 17:30:35 +02002886}
2887
Peter Lieven4ce78692013-10-24 12:06:54 +02002888bool bdrv_unallocated_blocks_are_zero(BlockDriverState *bs)
2889{
2890 BlockDriverInfo bdi;
2891
Kevin Wolf760e0062015-06-17 14:55:21 +02002892 if (bs->backing) {
Peter Lieven4ce78692013-10-24 12:06:54 +02002893 return false;
2894 }
2895
2896 if (bdrv_get_info(bs, &bdi) == 0) {
2897 return bdi.unallocated_blocks_are_zero;
2898 }
2899
2900 return false;
2901}
2902
2903bool bdrv_can_write_zeroes_with_unmap(BlockDriverState *bs)
2904{
2905 BlockDriverInfo bdi;
2906
Denis V. Lunev2f0342e2016-07-14 16:33:26 +03002907 if (!(bs->open_flags & BDRV_O_UNMAP)) {
Peter Lieven4ce78692013-10-24 12:06:54 +02002908 return false;
2909 }
2910
2911 if (bdrv_get_info(bs, &bdi) == 0) {
2912 return bdi.can_write_zeroes_with_unmap;
2913 }
2914
2915 return false;
2916}
2917
aliguori045df332009-03-05 23:00:48 +00002918const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
2919{
Kevin Wolf760e0062015-06-17 14:55:21 +02002920 if (bs->backing && bs->backing->bs->encrypted)
aliguori045df332009-03-05 23:00:48 +00002921 return bs->backing_file;
2922 else if (bs->encrypted)
2923 return bs->filename;
2924 else
2925 return NULL;
2926}
2927
ths5fafdf22007-09-16 21:08:06 +00002928void bdrv_get_backing_filename(BlockDriverState *bs,
bellard83f64092006-08-01 16:21:11 +00002929 char *filename, int filename_size)
bellardea2384d2004-08-01 21:59:26 +00002930{
Kevin Wolf3574c602011-10-26 11:02:11 +02002931 pstrcpy(filename, filename_size, bs->backing_file);
bellardea2384d2004-08-01 21:59:26 +00002932}
2933
bellardfaea38e2006-08-05 21:31:00 +00002934int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
2935{
2936 BlockDriver *drv = bs->drv;
2937 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002938 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00002939 if (!drv->bdrv_get_info)
2940 return -ENOTSUP;
2941 memset(bdi, 0, sizeof(*bdi));
2942 return drv->bdrv_get_info(bs, bdi);
2943}
2944
Max Reitzeae041f2013-10-09 10:46:16 +02002945ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs)
2946{
2947 BlockDriver *drv = bs->drv;
2948 if (drv && drv->bdrv_get_specific_info) {
2949 return drv->bdrv_get_specific_info(bs);
2950 }
2951 return NULL;
2952}
2953
Eric Blakea31939e2015-11-18 01:52:54 -07002954void bdrv_debug_event(BlockDriverState *bs, BlkdebugEvent event)
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01002955{
Kevin Wolfbf736fe2013-06-05 15:17:55 +02002956 if (!bs || !bs->drv || !bs->drv->bdrv_debug_event) {
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01002957 return;
2958 }
2959
Kevin Wolfbf736fe2013-06-05 15:17:55 +02002960 bs->drv->bdrv_debug_event(bs, event);
Kevin Wolf41c695c2012-12-06 14:32:58 +01002961}
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01002962
Kevin Wolf41c695c2012-12-06 14:32:58 +01002963int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event,
2964 const char *tag)
2965{
2966 while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02002967 bs = bs->file ? bs->file->bs : NULL;
Kevin Wolf41c695c2012-12-06 14:32:58 +01002968 }
2969
2970 if (bs && bs->drv && bs->drv->bdrv_debug_breakpoint) {
2971 return bs->drv->bdrv_debug_breakpoint(bs, event, tag);
2972 }
2973
2974 return -ENOTSUP;
2975}
2976
Fam Zheng4cc70e92013-11-20 10:01:54 +08002977int bdrv_debug_remove_breakpoint(BlockDriverState *bs, const char *tag)
2978{
2979 while (bs && bs->drv && !bs->drv->bdrv_debug_remove_breakpoint) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02002980 bs = bs->file ? bs->file->bs : NULL;
Fam Zheng4cc70e92013-11-20 10:01:54 +08002981 }
2982
2983 if (bs && bs->drv && bs->drv->bdrv_debug_remove_breakpoint) {
2984 return bs->drv->bdrv_debug_remove_breakpoint(bs, tag);
2985 }
2986
2987 return -ENOTSUP;
2988}
2989
Kevin Wolf41c695c2012-12-06 14:32:58 +01002990int bdrv_debug_resume(BlockDriverState *bs, const char *tag)
2991{
Max Reitz938789e2014-03-10 23:44:08 +01002992 while (bs && (!bs->drv || !bs->drv->bdrv_debug_resume)) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02002993 bs = bs->file ? bs->file->bs : NULL;
Kevin Wolf41c695c2012-12-06 14:32:58 +01002994 }
2995
2996 if (bs && bs->drv && bs->drv->bdrv_debug_resume) {
2997 return bs->drv->bdrv_debug_resume(bs, tag);
2998 }
2999
3000 return -ENOTSUP;
3001}
3002
3003bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag)
3004{
3005 while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003006 bs = bs->file ? bs->file->bs : NULL;
Kevin Wolf41c695c2012-12-06 14:32:58 +01003007 }
3008
3009 if (bs && bs->drv && bs->drv->bdrv_debug_is_suspended) {
3010 return bs->drv->bdrv_debug_is_suspended(bs, tag);
3011 }
3012
3013 return false;
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01003014}
3015
Jeff Codyb1b1d782012-10-16 15:49:09 -04003016/* backing_file can either be relative, or absolute, or a protocol. If it is
3017 * relative, it must be relative to the chain. So, passing in bs->filename
3018 * from a BDS as backing_file should not be done, as that may be relative to
3019 * the CWD rather than the chain. */
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00003020BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs,
3021 const char *backing_file)
3022{
Jeff Codyb1b1d782012-10-16 15:49:09 -04003023 char *filename_full = NULL;
3024 char *backing_file_full = NULL;
3025 char *filename_tmp = NULL;
3026 int is_protocol = 0;
3027 BlockDriverState *curr_bs = NULL;
3028 BlockDriverState *retval = NULL;
3029
3030 if (!bs || !bs->drv || !backing_file) {
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00003031 return NULL;
3032 }
3033
Jeff Codyb1b1d782012-10-16 15:49:09 -04003034 filename_full = g_malloc(PATH_MAX);
3035 backing_file_full = g_malloc(PATH_MAX);
3036 filename_tmp = g_malloc(PATH_MAX);
3037
3038 is_protocol = path_has_protocol(backing_file);
3039
Kevin Wolf760e0062015-06-17 14:55:21 +02003040 for (curr_bs = bs; curr_bs->backing; curr_bs = curr_bs->backing->bs) {
Jeff Codyb1b1d782012-10-16 15:49:09 -04003041
3042 /* If either of the filename paths is actually a protocol, then
3043 * compare unmodified paths; otherwise make paths relative */
3044 if (is_protocol || path_has_protocol(curr_bs->backing_file)) {
3045 if (strcmp(backing_file, curr_bs->backing_file) == 0) {
Kevin Wolf760e0062015-06-17 14:55:21 +02003046 retval = curr_bs->backing->bs;
Jeff Codyb1b1d782012-10-16 15:49:09 -04003047 break;
3048 }
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00003049 } else {
Jeff Codyb1b1d782012-10-16 15:49:09 -04003050 /* If not an absolute filename path, make it relative to the current
3051 * image's filename path */
3052 path_combine(filename_tmp, PATH_MAX, curr_bs->filename,
3053 backing_file);
3054
3055 /* We are going to compare absolute pathnames */
3056 if (!realpath(filename_tmp, filename_full)) {
3057 continue;
3058 }
3059
3060 /* We need to make sure the backing filename we are comparing against
3061 * is relative to the current image filename (or absolute) */
3062 path_combine(filename_tmp, PATH_MAX, curr_bs->filename,
3063 curr_bs->backing_file);
3064
3065 if (!realpath(filename_tmp, backing_file_full)) {
3066 continue;
3067 }
3068
3069 if (strcmp(backing_file_full, filename_full) == 0) {
Kevin Wolf760e0062015-06-17 14:55:21 +02003070 retval = curr_bs->backing->bs;
Jeff Codyb1b1d782012-10-16 15:49:09 -04003071 break;
3072 }
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00003073 }
3074 }
3075
Jeff Codyb1b1d782012-10-16 15:49:09 -04003076 g_free(filename_full);
3077 g_free(backing_file_full);
3078 g_free(filename_tmp);
3079 return retval;
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00003080}
3081
Benoît Canetf198fd12012-08-02 10:22:47 +02003082int bdrv_get_backing_file_depth(BlockDriverState *bs)
3083{
3084 if (!bs->drv) {
3085 return 0;
3086 }
3087
Kevin Wolf760e0062015-06-17 14:55:21 +02003088 if (!bs->backing) {
Benoît Canetf198fd12012-08-02 10:22:47 +02003089 return 0;
3090 }
3091
Kevin Wolf760e0062015-06-17 14:55:21 +02003092 return 1 + bdrv_get_backing_file_depth(bs->backing->bs);
Benoît Canetf198fd12012-08-02 10:22:47 +02003093}
3094
bellardea2384d2004-08-01 21:59:26 +00003095void bdrv_init(void)
3096{
Anthony Liguori5efa9d52009-05-09 17:03:42 -05003097 module_call_init(MODULE_INIT_BLOCK);
bellardea2384d2004-08-01 21:59:26 +00003098}
pbrookce1a14d2006-08-07 02:38:06 +00003099
Markus Armbrustereb852012009-10-27 18:41:44 +01003100void bdrv_init_with_whitelist(void)
3101{
3102 use_bdrv_whitelist = 1;
3103 bdrv_init();
3104}
3105
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003106void bdrv_invalidate_cache(BlockDriverState *bs, Error **errp)
Anthony Liguori0f154232011-11-14 15:09:45 -06003107{
Fam Zheng0d1c5c92016-05-11 10:45:33 +08003108 BdrvChild *child;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003109 Error *local_err = NULL;
3110 int ret;
3111
Kevin Wolf3456a8d2014-03-11 10:58:39 +01003112 if (!bs->drv) {
3113 return;
Anthony Liguori0f154232011-11-14 15:09:45 -06003114 }
Kevin Wolf3456a8d2014-03-11 10:58:39 +01003115
Kevin Wolf04c01a52016-01-13 15:56:06 +01003116 if (!(bs->open_flags & BDRV_O_INACTIVE)) {
Alexey Kardashevskiy7ea2d262014-10-09 13:50:46 +11003117 return;
3118 }
Kevin Wolf04c01a52016-01-13 15:56:06 +01003119 bs->open_flags &= ~BDRV_O_INACTIVE;
Alexey Kardashevskiy7ea2d262014-10-09 13:50:46 +11003120
Kevin Wolf3456a8d2014-03-11 10:58:39 +01003121 if (bs->drv->bdrv_invalidate_cache) {
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003122 bs->drv->bdrv_invalidate_cache(bs, &local_err);
Fam Zheng0d1c5c92016-05-11 10:45:33 +08003123 if (local_err) {
3124 bs->open_flags |= BDRV_O_INACTIVE;
3125 error_propagate(errp, local_err);
3126 return;
3127 }
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003128 }
Fam Zheng0d1c5c92016-05-11 10:45:33 +08003129
3130 QLIST_FOREACH(child, &bs->children, next) {
3131 bdrv_invalidate_cache(child->bs, &local_err);
3132 if (local_err) {
3133 bs->open_flags |= BDRV_O_INACTIVE;
3134 error_propagate(errp, local_err);
3135 return;
3136 }
Kevin Wolf3456a8d2014-03-11 10:58:39 +01003137 }
3138
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003139 ret = refresh_total_sectors(bs, bs->total_sectors);
3140 if (ret < 0) {
Kevin Wolf04c01a52016-01-13 15:56:06 +01003141 bs->open_flags |= BDRV_O_INACTIVE;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003142 error_setg_errno(errp, -ret, "Could not refresh total sector count");
3143 return;
3144 }
Anthony Liguori0f154232011-11-14 15:09:45 -06003145}
3146
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003147void bdrv_invalidate_cache_all(Error **errp)
Anthony Liguori0f154232011-11-14 15:09:45 -06003148{
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003149 BlockDriverState *bs;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003150 Error *local_err = NULL;
Kevin Wolf88be7b42016-05-20 18:49:07 +02003151 BdrvNextIterator it;
Anthony Liguori0f154232011-11-14 15:09:45 -06003152
Kevin Wolf88be7b42016-05-20 18:49:07 +02003153 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Stefan Hajnoczied78cda2014-05-08 16:34:35 +02003154 AioContext *aio_context = bdrv_get_aio_context(bs);
3155
3156 aio_context_acquire(aio_context);
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003157 bdrv_invalidate_cache(bs, &local_err);
Stefan Hajnoczied78cda2014-05-08 16:34:35 +02003158 aio_context_release(aio_context);
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003159 if (local_err) {
3160 error_propagate(errp, local_err);
3161 return;
3162 }
Anthony Liguori0f154232011-11-14 15:09:45 -06003163 }
3164}
3165
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003166static int bdrv_inactivate_recurse(BlockDriverState *bs,
3167 bool setting_flag)
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003168{
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003169 BdrvChild *child;
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003170 int ret;
3171
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003172 if (!setting_flag && bs->drv->bdrv_inactivate) {
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003173 ret = bs->drv->bdrv_inactivate(bs);
3174 if (ret < 0) {
3175 return ret;
3176 }
3177 }
3178
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003179 QLIST_FOREACH(child, &bs->children, next) {
3180 ret = bdrv_inactivate_recurse(child->bs, setting_flag);
3181 if (ret < 0) {
3182 return ret;
3183 }
3184 }
3185
3186 if (setting_flag) {
3187 bs->open_flags |= BDRV_O_INACTIVE;
3188 }
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003189 return 0;
3190}
3191
3192int bdrv_inactivate_all(void)
3193{
Max Reitz79720af2016-03-16 19:54:44 +01003194 BlockDriverState *bs = NULL;
Kevin Wolf88be7b42016-05-20 18:49:07 +02003195 BdrvNextIterator it;
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003196 int ret = 0;
3197 int pass;
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003198
Kevin Wolf88be7b42016-05-20 18:49:07 +02003199 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003200 aio_context_acquire(bdrv_get_aio_context(bs));
3201 }
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003202
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003203 /* We do two passes of inactivation. The first pass calls to drivers'
3204 * .bdrv_inactivate callbacks recursively so all cache is flushed to disk;
3205 * the second pass sets the BDRV_O_INACTIVE flag so that no further write
3206 * is allowed. */
3207 for (pass = 0; pass < 2; pass++) {
Kevin Wolf88be7b42016-05-20 18:49:07 +02003208 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003209 ret = bdrv_inactivate_recurse(bs, pass);
3210 if (ret < 0) {
3211 goto out;
3212 }
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003213 }
3214 }
3215
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003216out:
Kevin Wolf88be7b42016-05-20 18:49:07 +02003217 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003218 aio_context_release(bdrv_get_aio_context(bs));
3219 }
3220
3221 return ret;
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003222}
3223
Kevin Wolff9f05dc2011-07-15 13:50:26 +02003224/**************************************************************/
bellard19cb3732006-08-19 11:45:59 +00003225/* removable device support */
3226
3227/**
3228 * Return TRUE if the media is present
3229 */
Max Reitze031f752015-10-19 17:53:11 +02003230bool bdrv_is_inserted(BlockDriverState *bs)
bellard19cb3732006-08-19 11:45:59 +00003231{
3232 BlockDriver *drv = bs->drv;
Max Reitz28d7a782015-10-19 17:53:13 +02003233 BdrvChild *child;
Markus Armbrustera1aff5b2011-09-06 18:58:41 +02003234
Max Reitze031f752015-10-19 17:53:11 +02003235 if (!drv) {
3236 return false;
3237 }
Max Reitz28d7a782015-10-19 17:53:13 +02003238 if (drv->bdrv_is_inserted) {
3239 return drv->bdrv_is_inserted(bs);
Max Reitze031f752015-10-19 17:53:11 +02003240 }
Max Reitz28d7a782015-10-19 17:53:13 +02003241 QLIST_FOREACH(child, &bs->children, next) {
3242 if (!bdrv_is_inserted(child->bs)) {
3243 return false;
3244 }
3245 }
3246 return true;
bellard19cb3732006-08-19 11:45:59 +00003247}
3248
3249/**
Markus Armbruster8e49ca42011-08-03 15:08:08 +02003250 * Return whether the media changed since the last call to this
3251 * function, or -ENOTSUP if we don't know. Most drivers don't know.
bellard19cb3732006-08-19 11:45:59 +00003252 */
3253int bdrv_media_changed(BlockDriverState *bs)
3254{
3255 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +00003256
Markus Armbruster8e49ca42011-08-03 15:08:08 +02003257 if (drv && drv->bdrv_media_changed) {
3258 return drv->bdrv_media_changed(bs);
3259 }
3260 return -ENOTSUP;
bellard19cb3732006-08-19 11:45:59 +00003261}
3262
3263/**
3264 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
3265 */
Luiz Capitulinof36f3942012-02-03 16:24:53 -02003266void bdrv_eject(BlockDriverState *bs, bool eject_flag)
bellard19cb3732006-08-19 11:45:59 +00003267{
3268 BlockDriver *drv = bs->drv;
Markus Armbrusterbfb197e2014-10-07 13:59:11 +02003269 const char *device_name;
bellard19cb3732006-08-19 11:45:59 +00003270
Markus Armbruster822e1cd2011-07-20 18:23:42 +02003271 if (drv && drv->bdrv_eject) {
3272 drv->bdrv_eject(bs, eject_flag);
bellard19cb3732006-08-19 11:45:59 +00003273 }
Luiz Capitulino6f382ed2012-02-14 13:41:13 -02003274
Markus Armbrusterbfb197e2014-10-07 13:59:11 +02003275 device_name = bdrv_get_device_name(bs);
3276 if (device_name[0] != '\0') {
3277 qapi_event_send_device_tray_moved(device_name,
Wenchao Xiaa5ee7bd2014-06-18 08:43:44 +02003278 eject_flag, &error_abort);
Luiz Capitulino6f382ed2012-02-14 13:41:13 -02003279 }
bellard19cb3732006-08-19 11:45:59 +00003280}
3281
bellard19cb3732006-08-19 11:45:59 +00003282/**
3283 * Lock or unlock the media (if it is locked, the user won't be able
3284 * to eject it manually).
3285 */
Markus Armbruster025e8492011-09-06 18:58:47 +02003286void bdrv_lock_medium(BlockDriverState *bs, bool locked)
bellard19cb3732006-08-19 11:45:59 +00003287{
3288 BlockDriver *drv = bs->drv;
3289
Markus Armbruster025e8492011-09-06 18:58:47 +02003290 trace_bdrv_lock_medium(bs, locked);
Stefan Hajnoczib8c6d092011-03-29 20:04:40 +01003291
Markus Armbruster025e8492011-09-06 18:58:47 +02003292 if (drv && drv->bdrv_lock_medium) {
3293 drv->bdrv_lock_medium(bs, locked);
bellard19cb3732006-08-19 11:45:59 +00003294 }
3295}
ths985a03b2007-12-24 16:10:43 +00003296
Fam Zheng9fcb0252013-08-23 09:14:46 +08003297/* Get a reference to bs */
3298void bdrv_ref(BlockDriverState *bs)
3299{
3300 bs->refcnt++;
3301}
3302
3303/* Release a previously grabbed reference to bs.
3304 * If after releasing, reference count is zero, the BlockDriverState is
3305 * deleted. */
3306void bdrv_unref(BlockDriverState *bs)
3307{
Jeff Cody9a4d5ca2014-07-23 17:22:57 -04003308 if (!bs) {
3309 return;
3310 }
Fam Zheng9fcb0252013-08-23 09:14:46 +08003311 assert(bs->refcnt > 0);
3312 if (--bs->refcnt == 0) {
3313 bdrv_delete(bs);
3314 }
3315}
3316
Fam Zhengfbe40ff2014-05-23 21:29:42 +08003317struct BdrvOpBlocker {
3318 Error *reason;
3319 QLIST_ENTRY(BdrvOpBlocker) list;
3320};
3321
3322bool bdrv_op_is_blocked(BlockDriverState *bs, BlockOpType op, Error **errp)
3323{
3324 BdrvOpBlocker *blocker;
3325 assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
3326 if (!QLIST_EMPTY(&bs->op_blockers[op])) {
3327 blocker = QLIST_FIRST(&bs->op_blockers[op]);
3328 if (errp) {
Markus Armbrustere43bfd92015-12-18 16:35:15 +01003329 *errp = error_copy(blocker->reason);
3330 error_prepend(errp, "Node '%s' is busy: ",
3331 bdrv_get_device_or_node_name(bs));
Fam Zhengfbe40ff2014-05-23 21:29:42 +08003332 }
3333 return true;
3334 }
3335 return false;
3336}
3337
3338void bdrv_op_block(BlockDriverState *bs, BlockOpType op, Error *reason)
3339{
3340 BdrvOpBlocker *blocker;
3341 assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
3342
Markus Armbruster5839e532014-08-19 10:31:08 +02003343 blocker = g_new0(BdrvOpBlocker, 1);
Fam Zhengfbe40ff2014-05-23 21:29:42 +08003344 blocker->reason = reason;
3345 QLIST_INSERT_HEAD(&bs->op_blockers[op], blocker, list);
3346}
3347
3348void bdrv_op_unblock(BlockDriverState *bs, BlockOpType op, Error *reason)
3349{
3350 BdrvOpBlocker *blocker, *next;
3351 assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
3352 QLIST_FOREACH_SAFE(blocker, &bs->op_blockers[op], list, next) {
3353 if (blocker->reason == reason) {
3354 QLIST_REMOVE(blocker, list);
3355 g_free(blocker);
3356 }
3357 }
3358}
3359
3360void bdrv_op_block_all(BlockDriverState *bs, Error *reason)
3361{
3362 int i;
3363 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
3364 bdrv_op_block(bs, i, reason);
3365 }
3366}
3367
3368void bdrv_op_unblock_all(BlockDriverState *bs, Error *reason)
3369{
3370 int i;
3371 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
3372 bdrv_op_unblock(bs, i, reason);
3373 }
3374}
3375
3376bool bdrv_op_blocker_is_empty(BlockDriverState *bs)
3377{
3378 int i;
3379
3380 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
3381 if (!QLIST_EMPTY(&bs->op_blockers[i])) {
3382 return false;
3383 }
3384 }
3385 return true;
3386}
3387
Luiz Capitulinod92ada22012-11-30 10:52:09 -02003388void bdrv_img_create(const char *filename, const char *fmt,
3389 const char *base_filename, const char *base_fmt,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003390 char *options, uint64_t img_size, int flags,
3391 Error **errp, bool quiet)
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003392{
Chunyan Liu83d05212014-06-05 17:20:51 +08003393 QemuOptsList *create_opts = NULL;
3394 QemuOpts *opts = NULL;
3395 const char *backing_fmt, *backing_file;
3396 int64_t size;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003397 BlockDriver *drv, *proto_drv;
Max Reitzcc84d902013-09-06 17:14:26 +02003398 Error *local_err = NULL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003399 int ret = 0;
3400
3401 /* Find driver and parse its options */
3402 drv = bdrv_find_format(fmt);
3403 if (!drv) {
Luiz Capitulino71c79812012-11-30 10:52:04 -02003404 error_setg(errp, "Unknown file format '%s'", fmt);
Luiz Capitulinod92ada22012-11-30 10:52:09 -02003405 return;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003406 }
3407
Max Reitzb65a5e12015-02-05 13:58:12 -05003408 proto_drv = bdrv_find_protocol(filename, true, errp);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003409 if (!proto_drv) {
Luiz Capitulinod92ada22012-11-30 10:52:09 -02003410 return;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003411 }
3412
Max Reitzc6149722014-12-02 18:32:45 +01003413 if (!drv->create_opts) {
3414 error_setg(errp, "Format driver '%s' does not support image creation",
3415 drv->format_name);
3416 return;
3417 }
3418
3419 if (!proto_drv->create_opts) {
3420 error_setg(errp, "Protocol driver '%s' does not support image creation",
3421 proto_drv->format_name);
3422 return;
3423 }
3424
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003425 create_opts = qemu_opts_append(create_opts, drv->create_opts);
3426 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003427
3428 /* Create parameter list with default values */
Chunyan Liu83d05212014-06-05 17:20:51 +08003429 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbruster39101f22015-02-12 16:46:36 +01003430 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003431
3432 /* Parse -o options */
3433 if (options) {
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003434 qemu_opts_do_parse(opts, options, NULL, &local_err);
3435 if (local_err) {
3436 error_report_err(local_err);
3437 local_err = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003438 error_setg(errp, "Invalid options for file format '%s'", fmt);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003439 goto out;
3440 }
3441 }
3442
3443 if (base_filename) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003444 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &local_err);
Markus Armbruster6be41942015-02-12 17:49:02 +01003445 if (local_err) {
Luiz Capitulino71c79812012-11-30 10:52:04 -02003446 error_setg(errp, "Backing file not supported for file format '%s'",
3447 fmt);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003448 goto out;
3449 }
3450 }
3451
3452 if (base_fmt) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003453 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &local_err);
Markus Armbruster6be41942015-02-12 17:49:02 +01003454 if (local_err) {
Luiz Capitulino71c79812012-11-30 10:52:04 -02003455 error_setg(errp, "Backing file format not supported for file "
3456 "format '%s'", fmt);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003457 goto out;
3458 }
3459 }
3460
Chunyan Liu83d05212014-06-05 17:20:51 +08003461 backing_file = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
3462 if (backing_file) {
3463 if (!strcmp(filename, backing_file)) {
Luiz Capitulino71c79812012-11-30 10:52:04 -02003464 error_setg(errp, "Error: Trying to create an image with the "
3465 "same filename as the backing file");
Jes Sorensen792da932010-12-16 13:52:17 +01003466 goto out;
3467 }
3468 }
3469
Chunyan Liu83d05212014-06-05 17:20:51 +08003470 backing_fmt = qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003471
3472 // The size for the image must always be specified, with one exception:
3473 // If we are using a backing file, we can obtain the size from there
Chunyan Liu83d05212014-06-05 17:20:51 +08003474 size = qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 0);
3475 if (size == -1) {
3476 if (backing_file) {
Max Reitz66f6b812013-12-03 14:57:52 +01003477 BlockDriverState *bs;
Max Reitz29168012014-11-26 17:20:27 +01003478 char *full_backing = g_new0(char, PATH_MAX);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003479 int64_t size;
Paolo Bonzini63090da2012-04-12 14:01:03 +02003480 int back_flags;
Max Reitze6641712015-08-26 19:47:48 +02003481 QDict *backing_options = NULL;
Paolo Bonzini63090da2012-04-12 14:01:03 +02003482
Max Reitz29168012014-11-26 17:20:27 +01003483 bdrv_get_full_backing_filename_from_filename(filename, backing_file,
3484 full_backing, PATH_MAX,
3485 &local_err);
3486 if (local_err) {
3487 g_free(full_backing);
3488 goto out;
3489 }
3490
Paolo Bonzini63090da2012-04-12 14:01:03 +02003491 /* backing files always opened read-only */
Kevin Wolf61de4c62016-03-18 17:46:45 +01003492 back_flags = flags;
Kevin Wolfbfd18d12016-03-04 14:28:01 +01003493 back_flags &= ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003494
Max Reitze6641712015-08-26 19:47:48 +02003495 if (backing_fmt) {
3496 backing_options = qdict_new();
3497 qdict_put(backing_options, "driver",
3498 qstring_from_str(backing_fmt));
3499 }
3500
Max Reitz5b363932016-05-17 16:41:31 +02003501 bs = bdrv_open(full_backing, NULL, backing_options, back_flags,
3502 &local_err);
Max Reitz29168012014-11-26 17:20:27 +01003503 g_free(full_backing);
Max Reitz5b363932016-05-17 16:41:31 +02003504 if (!bs) {
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003505 goto out;
3506 }
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003507 size = bdrv_getlength(bs);
3508 if (size < 0) {
3509 error_setg_errno(errp, -size, "Could not get size of '%s'",
3510 backing_file);
3511 bdrv_unref(bs);
3512 goto out;
3513 }
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003514
Markus Armbruster39101f22015-02-12 16:46:36 +01003515 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, size, &error_abort);
Max Reitz66f6b812013-12-03 14:57:52 +01003516
3517 bdrv_unref(bs);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003518 } else {
Luiz Capitulino71c79812012-11-30 10:52:04 -02003519 error_setg(errp, "Image creation needs a size parameter");
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003520 goto out;
3521 }
3522 }
3523
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003524 if (!quiet) {
Kővágó, Zoltánfe646692015-07-07 16:42:10 +02003525 printf("Formatting '%s', fmt=%s ", filename, fmt);
Fam Zheng43c5d8f2014-12-09 15:38:04 +08003526 qemu_opts_print(opts, " ");
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003527 puts("");
3528 }
Chunyan Liu83d05212014-06-05 17:20:51 +08003529
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003530 ret = bdrv_create(drv, filename, opts, &local_err);
Chunyan Liu83d05212014-06-05 17:20:51 +08003531
Max Reitzcc84d902013-09-06 17:14:26 +02003532 if (ret == -EFBIG) {
3533 /* This is generally a better message than whatever the driver would
3534 * deliver (especially because of the cluster_size_hint), since that
3535 * is most probably not much different from "image too large". */
3536 const char *cluster_size_hint = "";
Chunyan Liu83d05212014-06-05 17:20:51 +08003537 if (qemu_opt_get_size(opts, BLOCK_OPT_CLUSTER_SIZE, 0)) {
Max Reitzcc84d902013-09-06 17:14:26 +02003538 cluster_size_hint = " (try using a larger cluster size)";
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003539 }
Max Reitzcc84d902013-09-06 17:14:26 +02003540 error_setg(errp, "The image size is too large for file format '%s'"
3541 "%s", fmt, cluster_size_hint);
3542 error_free(local_err);
3543 local_err = NULL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003544 }
3545
3546out:
Chunyan Liu83d05212014-06-05 17:20:51 +08003547 qemu_opts_del(opts);
3548 qemu_opts_free(create_opts);
Eduardo Habkost621ff942016-06-13 18:57:56 -03003549 error_propagate(errp, local_err);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003550}
Stefan Hajnoczi85d126f2013-03-07 13:41:48 +01003551
3552AioContext *bdrv_get_aio_context(BlockDriverState *bs)
3553{
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003554 return bs->aio_context;
3555}
3556
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003557static void bdrv_do_remove_aio_context_notifier(BdrvAioNotifier *ban)
3558{
3559 QLIST_REMOVE(ban, list);
3560 g_free(ban);
3561}
3562
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003563void bdrv_detach_aio_context(BlockDriverState *bs)
3564{
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003565 BdrvAioNotifier *baf, *baf_tmp;
Max Reitzb97511c2016-05-17 13:38:04 +02003566 BdrvChild *child;
Max Reitz33384422014-06-20 21:57:33 +02003567
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003568 if (!bs->drv) {
3569 return;
3570 }
3571
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003572 assert(!bs->walking_aio_notifiers);
3573 bs->walking_aio_notifiers = true;
3574 QLIST_FOREACH_SAFE(baf, &bs->aio_notifiers, list, baf_tmp) {
3575 if (baf->deleted) {
3576 bdrv_do_remove_aio_context_notifier(baf);
3577 } else {
3578 baf->detach_aio_context(baf->opaque);
3579 }
Max Reitz33384422014-06-20 21:57:33 +02003580 }
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003581 /* Never mind iterating again to check for ->deleted. bdrv_close() will
3582 * remove remaining aio notifiers if we aren't called again.
3583 */
3584 bs->walking_aio_notifiers = false;
Max Reitz33384422014-06-20 21:57:33 +02003585
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003586 if (bs->drv->bdrv_detach_aio_context) {
3587 bs->drv->bdrv_detach_aio_context(bs);
3588 }
Max Reitzb97511c2016-05-17 13:38:04 +02003589 QLIST_FOREACH(child, &bs->children, next) {
3590 bdrv_detach_aio_context(child->bs);
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003591 }
3592
3593 bs->aio_context = NULL;
3594}
3595
3596void bdrv_attach_aio_context(BlockDriverState *bs,
3597 AioContext *new_context)
3598{
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003599 BdrvAioNotifier *ban, *ban_tmp;
Max Reitzb97511c2016-05-17 13:38:04 +02003600 BdrvChild *child;
Max Reitz33384422014-06-20 21:57:33 +02003601
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003602 if (!bs->drv) {
3603 return;
3604 }
3605
3606 bs->aio_context = new_context;
3607
Max Reitzb97511c2016-05-17 13:38:04 +02003608 QLIST_FOREACH(child, &bs->children, next) {
3609 bdrv_attach_aio_context(child->bs, new_context);
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003610 }
3611 if (bs->drv->bdrv_attach_aio_context) {
3612 bs->drv->bdrv_attach_aio_context(bs, new_context);
3613 }
Max Reitz33384422014-06-20 21:57:33 +02003614
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003615 assert(!bs->walking_aio_notifiers);
3616 bs->walking_aio_notifiers = true;
3617 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_tmp) {
3618 if (ban->deleted) {
3619 bdrv_do_remove_aio_context_notifier(ban);
3620 } else {
3621 ban->attached_aio_context(new_context, ban->opaque);
3622 }
Max Reitz33384422014-06-20 21:57:33 +02003623 }
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003624 bs->walking_aio_notifiers = false;
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003625}
3626
3627void bdrv_set_aio_context(BlockDriverState *bs, AioContext *new_context)
3628{
Fam Zheng53ec73e2015-05-29 18:53:14 +08003629 bdrv_drain(bs); /* ensure there are no in-flight requests */
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003630
3631 bdrv_detach_aio_context(bs);
3632
3633 /* This function executes in the old AioContext so acquire the new one in
3634 * case it runs in a different thread.
3635 */
3636 aio_context_acquire(new_context);
3637 bdrv_attach_aio_context(bs, new_context);
3638 aio_context_release(new_context);
Stefan Hajnoczi85d126f2013-03-07 13:41:48 +01003639}
Stefan Hajnoczid616b222013-06-24 17:13:10 +02003640
Max Reitz33384422014-06-20 21:57:33 +02003641void bdrv_add_aio_context_notifier(BlockDriverState *bs,
3642 void (*attached_aio_context)(AioContext *new_context, void *opaque),
3643 void (*detach_aio_context)(void *opaque), void *opaque)
3644{
3645 BdrvAioNotifier *ban = g_new(BdrvAioNotifier, 1);
3646 *ban = (BdrvAioNotifier){
3647 .attached_aio_context = attached_aio_context,
3648 .detach_aio_context = detach_aio_context,
3649 .opaque = opaque
3650 };
3651
3652 QLIST_INSERT_HEAD(&bs->aio_notifiers, ban, list);
3653}
3654
3655void bdrv_remove_aio_context_notifier(BlockDriverState *bs,
3656 void (*attached_aio_context)(AioContext *,
3657 void *),
3658 void (*detach_aio_context)(void *),
3659 void *opaque)
3660{
3661 BdrvAioNotifier *ban, *ban_next;
3662
3663 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) {
3664 if (ban->attached_aio_context == attached_aio_context &&
3665 ban->detach_aio_context == detach_aio_context &&
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003666 ban->opaque == opaque &&
3667 ban->deleted == false)
Max Reitz33384422014-06-20 21:57:33 +02003668 {
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003669 if (bs->walking_aio_notifiers) {
3670 ban->deleted = true;
3671 } else {
3672 bdrv_do_remove_aio_context_notifier(ban);
3673 }
Max Reitz33384422014-06-20 21:57:33 +02003674 return;
3675 }
3676 }
3677
3678 abort();
3679}
3680
Max Reitz77485432014-10-27 11:12:50 +01003681int bdrv_amend_options(BlockDriverState *bs, QemuOpts *opts,
Max Reitz8b139762015-07-27 17:51:32 +02003682 BlockDriverAmendStatusCB *status_cb, void *cb_opaque)
Max Reitz6f176b42013-09-03 10:09:50 +02003683{
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003684 if (!bs->drv->bdrv_amend_options) {
Max Reitz6f176b42013-09-03 10:09:50 +02003685 return -ENOTSUP;
3686 }
Max Reitz8b139762015-07-27 17:51:32 +02003687 return bs->drv->bdrv_amend_options(bs, opts, status_cb, cb_opaque);
Max Reitz6f176b42013-09-03 10:09:50 +02003688}
Benoît Canetf6186f42013-10-02 14:33:48 +02003689
Benoît Canetb5042a32014-03-03 19:11:34 +01003690/* This function will be called by the bdrv_recurse_is_first_non_filter method
3691 * of block filter and by bdrv_is_first_non_filter.
3692 * It is used to test if the given bs is the candidate or recurse more in the
3693 * node graph.
Benoît Canet212a5a82014-01-23 21:31:36 +01003694 */
Benoît Canet212a5a82014-01-23 21:31:36 +01003695bool bdrv_recurse_is_first_non_filter(BlockDriverState *bs,
3696 BlockDriverState *candidate)
Benoît Canetf6186f42013-10-02 14:33:48 +02003697{
Benoît Canetb5042a32014-03-03 19:11:34 +01003698 /* return false if basic checks fails */
3699 if (!bs || !bs->drv) {
3700 return false;
3701 }
3702
3703 /* the code reached a non block filter driver -> check if the bs is
3704 * the same as the candidate. It's the recursion termination condition.
3705 */
3706 if (!bs->drv->is_filter) {
3707 return bs == candidate;
3708 }
3709 /* Down this path the driver is a block filter driver */
3710
3711 /* If the block filter recursion method is defined use it to recurse down
3712 * the node graph.
3713 */
3714 if (bs->drv->bdrv_recurse_is_first_non_filter) {
Benoît Canet212a5a82014-01-23 21:31:36 +01003715 return bs->drv->bdrv_recurse_is_first_non_filter(bs, candidate);
3716 }
3717
Benoît Canetb5042a32014-03-03 19:11:34 +01003718 /* the driver is a block filter but don't allow to recurse -> return false
3719 */
3720 return false;
Benoît Canet212a5a82014-01-23 21:31:36 +01003721}
3722
3723/* This function checks if the candidate is the first non filter bs down it's
3724 * bs chain. Since we don't have pointers to parents it explore all bs chains
3725 * from the top. Some filters can choose not to pass down the recursion.
3726 */
3727bool bdrv_is_first_non_filter(BlockDriverState *candidate)
3728{
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003729 BlockDriverState *bs;
Kevin Wolf88be7b42016-05-20 18:49:07 +02003730 BdrvNextIterator it;
Benoît Canet212a5a82014-01-23 21:31:36 +01003731
3732 /* walk down the bs forest recursively */
Kevin Wolf88be7b42016-05-20 18:49:07 +02003733 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Benoît Canet212a5a82014-01-23 21:31:36 +01003734 bool perm;
3735
Benoît Canetb5042a32014-03-03 19:11:34 +01003736 /* try to recurse in this top level bs */
Kevin Wolfe6dc8a12014-02-04 11:45:31 +01003737 perm = bdrv_recurse_is_first_non_filter(bs, candidate);
Benoît Canet212a5a82014-01-23 21:31:36 +01003738
3739 /* candidate is the first non filter */
3740 if (perm) {
3741 return true;
3742 }
3743 }
3744
3745 return false;
Benoît Canetf6186f42013-10-02 14:33:48 +02003746}
Benoît Canet09158f02014-06-27 18:25:25 +02003747
Wen Congyange12f3782015-07-17 10:12:22 +08003748BlockDriverState *check_to_replace_node(BlockDriverState *parent_bs,
3749 const char *node_name, Error **errp)
Benoît Canet09158f02014-06-27 18:25:25 +02003750{
3751 BlockDriverState *to_replace_bs = bdrv_find_node(node_name);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003752 AioContext *aio_context;
3753
Benoît Canet09158f02014-06-27 18:25:25 +02003754 if (!to_replace_bs) {
3755 error_setg(errp, "Node name '%s' not found", node_name);
3756 return NULL;
3757 }
3758
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003759 aio_context = bdrv_get_aio_context(to_replace_bs);
3760 aio_context_acquire(aio_context);
3761
Benoît Canet09158f02014-06-27 18:25:25 +02003762 if (bdrv_op_is_blocked(to_replace_bs, BLOCK_OP_TYPE_REPLACE, errp)) {
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003763 to_replace_bs = NULL;
3764 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003765 }
3766
3767 /* We don't want arbitrary node of the BDS chain to be replaced only the top
3768 * most non filter in order to prevent data corruption.
3769 * Another benefit is that this tests exclude backing files which are
3770 * blocked by the backing blockers.
3771 */
Wen Congyange12f3782015-07-17 10:12:22 +08003772 if (!bdrv_recurse_is_first_non_filter(parent_bs, to_replace_bs)) {
Benoît Canet09158f02014-06-27 18:25:25 +02003773 error_setg(errp, "Only top most non filter can be replaced");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003774 to_replace_bs = NULL;
3775 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003776 }
3777
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003778out:
3779 aio_context_release(aio_context);
Benoît Canet09158f02014-06-27 18:25:25 +02003780 return to_replace_bs;
3781}
Ming Lei448ad912014-07-04 18:04:33 +08003782
Max Reitz91af7012014-07-18 20:24:56 +02003783static bool append_open_options(QDict *d, BlockDriverState *bs)
3784{
3785 const QDictEntry *entry;
Kevin Wolf9e700c12015-04-24 15:20:28 +02003786 QemuOptDesc *desc;
Kevin Wolf260fecf2015-04-27 13:46:22 +02003787 BdrvChild *child;
Max Reitz91af7012014-07-18 20:24:56 +02003788 bool found_any = false;
Kevin Wolf260fecf2015-04-27 13:46:22 +02003789 const char *p;
Max Reitz91af7012014-07-18 20:24:56 +02003790
3791 for (entry = qdict_first(bs->options); entry;
3792 entry = qdict_next(bs->options, entry))
3793 {
Kevin Wolf260fecf2015-04-27 13:46:22 +02003794 /* Exclude options for children */
3795 QLIST_FOREACH(child, &bs->children, next) {
3796 if (strstart(qdict_entry_key(entry), child->name, &p)
3797 && (!*p || *p == '.'))
3798 {
3799 break;
3800 }
3801 }
3802 if (child) {
Kevin Wolf9e700c12015-04-24 15:20:28 +02003803 continue;
Max Reitz91af7012014-07-18 20:24:56 +02003804 }
Kevin Wolf9e700c12015-04-24 15:20:28 +02003805
3806 /* And exclude all non-driver-specific options */
3807 for (desc = bdrv_runtime_opts.desc; desc->name; desc++) {
3808 if (!strcmp(qdict_entry_key(entry), desc->name)) {
3809 break;
3810 }
3811 }
3812 if (desc->name) {
3813 continue;
3814 }
3815
3816 qobject_incref(qdict_entry_value(entry));
3817 qdict_put_obj(d, qdict_entry_key(entry), qdict_entry_value(entry));
3818 found_any = true;
Max Reitz91af7012014-07-18 20:24:56 +02003819 }
3820
3821 return found_any;
3822}
3823
3824/* Updates the following BDS fields:
3825 * - exact_filename: A filename which may be used for opening a block device
3826 * which (mostly) equals the given BDS (even without any
3827 * other options; so reading and writing must return the same
3828 * results, but caching etc. may be different)
3829 * - full_open_options: Options which, when given when opening a block device
3830 * (without a filename), result in a BDS (mostly)
3831 * equalling the given one
3832 * - filename: If exact_filename is set, it is copied here. Otherwise,
3833 * full_open_options is converted to a JSON object, prefixed with
3834 * "json:" (for use through the JSON pseudo protocol) and put here.
3835 */
3836void bdrv_refresh_filename(BlockDriverState *bs)
3837{
3838 BlockDriver *drv = bs->drv;
3839 QDict *opts;
3840
3841 if (!drv) {
3842 return;
3843 }
3844
3845 /* This BDS's file name will most probably depend on its file's name, so
3846 * refresh that first */
3847 if (bs->file) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003848 bdrv_refresh_filename(bs->file->bs);
Max Reitz91af7012014-07-18 20:24:56 +02003849 }
3850
3851 if (drv->bdrv_refresh_filename) {
3852 /* Obsolete information is of no use here, so drop the old file name
3853 * information before refreshing it */
3854 bs->exact_filename[0] = '\0';
3855 if (bs->full_open_options) {
3856 QDECREF(bs->full_open_options);
3857 bs->full_open_options = NULL;
3858 }
3859
Kevin Wolf4cdd01d2015-04-27 13:50:54 +02003860 opts = qdict_new();
3861 append_open_options(opts, bs);
3862 drv->bdrv_refresh_filename(bs, opts);
3863 QDECREF(opts);
Max Reitz91af7012014-07-18 20:24:56 +02003864 } else if (bs->file) {
3865 /* Try to reconstruct valid information from the underlying file */
3866 bool has_open_options;
3867
3868 bs->exact_filename[0] = '\0';
3869 if (bs->full_open_options) {
3870 QDECREF(bs->full_open_options);
3871 bs->full_open_options = NULL;
3872 }
3873
3874 opts = qdict_new();
3875 has_open_options = append_open_options(opts, bs);
3876
3877 /* If no specific options have been given for this BDS, the filename of
3878 * the underlying file should suffice for this one as well */
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003879 if (bs->file->bs->exact_filename[0] && !has_open_options) {
3880 strcpy(bs->exact_filename, bs->file->bs->exact_filename);
Max Reitz91af7012014-07-18 20:24:56 +02003881 }
3882 /* Reconstructing the full options QDict is simple for most format block
3883 * drivers, as long as the full options are known for the underlying
3884 * file BDS. The full options QDict of that file BDS should somehow
3885 * contain a representation of the filename, therefore the following
3886 * suffices without querying the (exact_)filename of this BDS. */
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003887 if (bs->file->bs->full_open_options) {
Max Reitz91af7012014-07-18 20:24:56 +02003888 qdict_put_obj(opts, "driver",
3889 QOBJECT(qstring_from_str(drv->format_name)));
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003890 QINCREF(bs->file->bs->full_open_options);
3891 qdict_put_obj(opts, "file",
3892 QOBJECT(bs->file->bs->full_open_options));
Max Reitz91af7012014-07-18 20:24:56 +02003893
3894 bs->full_open_options = opts;
3895 } else {
3896 QDECREF(opts);
3897 }
3898 } else if (!bs->full_open_options && qdict_size(bs->options)) {
3899 /* There is no underlying file BDS (at least referenced by BDS.file),
3900 * so the full options QDict should be equal to the options given
3901 * specifically for this block device when it was opened (plus the
3902 * driver specification).
3903 * Because those options don't change, there is no need to update
3904 * full_open_options when it's already set. */
3905
3906 opts = qdict_new();
3907 append_open_options(opts, bs);
3908 qdict_put_obj(opts, "driver",
3909 QOBJECT(qstring_from_str(drv->format_name)));
3910
3911 if (bs->exact_filename[0]) {
3912 /* This may not work for all block protocol drivers (some may
3913 * require this filename to be parsed), but we have to find some
3914 * default solution here, so just include it. If some block driver
3915 * does not support pure options without any filename at all or
3916 * needs some special format of the options QDict, it needs to
3917 * implement the driver-specific bdrv_refresh_filename() function.
3918 */
3919 qdict_put_obj(opts, "filename",
3920 QOBJECT(qstring_from_str(bs->exact_filename)));
3921 }
3922
3923 bs->full_open_options = opts;
3924 }
3925
3926 if (bs->exact_filename[0]) {
3927 pstrcpy(bs->filename, sizeof(bs->filename), bs->exact_filename);
3928 } else if (bs->full_open_options) {
3929 QString *json = qobject_to_json(QOBJECT(bs->full_open_options));
3930 snprintf(bs->filename, sizeof(bs->filename), "json:%s",
3931 qstring_get_str(json));
3932 QDECREF(json);
3933 }
3934}
Wen Congyange06018a2016-05-10 15:36:37 +08003935
3936/*
3937 * Hot add/remove a BDS's child. So the user can take a child offline when
3938 * it is broken and take a new child online
3939 */
3940void bdrv_add_child(BlockDriverState *parent_bs, BlockDriverState *child_bs,
3941 Error **errp)
3942{
3943
3944 if (!parent_bs->drv || !parent_bs->drv->bdrv_add_child) {
3945 error_setg(errp, "The node %s does not support adding a child",
3946 bdrv_get_device_or_node_name(parent_bs));
3947 return;
3948 }
3949
3950 if (!QLIST_EMPTY(&child_bs->parents)) {
3951 error_setg(errp, "The node %s already has a parent",
3952 child_bs->node_name);
3953 return;
3954 }
3955
3956 parent_bs->drv->bdrv_add_child(parent_bs, child_bs, errp);
3957}
3958
3959void bdrv_del_child(BlockDriverState *parent_bs, BdrvChild *child, Error **errp)
3960{
3961 BdrvChild *tmp;
3962
3963 if (!parent_bs->drv || !parent_bs->drv->bdrv_del_child) {
3964 error_setg(errp, "The node %s does not support removing a child",
3965 bdrv_get_device_or_node_name(parent_bs));
3966 return;
3967 }
3968
3969 QLIST_FOREACH(tmp, &parent_bs->children, next) {
3970 if (tmp == child) {
3971 break;
3972 }
3973 }
3974
3975 if (!tmp) {
3976 error_setg(errp, "The node %s does not have a child named %s",
3977 bdrv_get_device_or_node_name(parent_bs),
3978 bdrv_get_device_or_node_name(child->bs));
3979 return;
3980 }
3981
3982 parent_bs->drv->bdrv_del_child(parent_bs, child, errp);
3983}