blob: b749bd640420b484ed1cbfc8dfd52acd52be8ff4 [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"
Daniel P. Berrange0ab8ed12017-01-25 16:14:15 +000025#include "block/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 {
Max Reitz0d54a6f2017-05-22 21:52:15 +0200165 const char *protocol_stripped = NULL;
166
167 if (path_has_protocol(base_path)) {
168 protocol_stripped = strchr(base_path, ':');
169 if (protocol_stripped) {
170 protocol_stripped++;
171 }
172 }
173 p = protocol_stripped ?: base_path;
174
bellard3b9f94e2007-01-07 17:27:07 +0000175 p1 = strrchr(base_path, '/');
176#ifdef _WIN32
177 {
178 const char *p2;
179 p2 = strrchr(base_path, '\\');
180 if (!p1 || p2 > p1)
181 p1 = p2;
182 }
183#endif
bellard83f64092006-08-01 16:21:11 +0000184 if (p1)
185 p1++;
186 else
187 p1 = base_path;
188 if (p1 > p)
189 p = p1;
190 len = p - base_path;
191 if (len > dest_size - 1)
192 len = dest_size - 1;
193 memcpy(dest, base_path, len);
194 dest[len] = '\0';
195 pstrcat(dest, dest_size, filename);
196 }
197}
198
Max Reitz03c320d2017-05-22 21:52:16 +0200199/*
200 * Helper function for bdrv_parse_filename() implementations to remove optional
201 * protocol prefixes (especially "file:") from a filename and for putting the
202 * stripped filename into the options QDict if there is such a prefix.
203 */
204void bdrv_parse_filename_strip_prefix(const char *filename, const char *prefix,
205 QDict *options)
206{
207 if (strstart(filename, prefix, &filename)) {
208 /* Stripping the explicit protocol prefix may result in a protocol
209 * prefix being (wrongly) detected (if the filename contains a colon) */
210 if (path_has_protocol(filename)) {
211 QString *fat_filename;
212
213 /* This means there is some colon before the first slash; therefore,
214 * this cannot be an absolute path */
215 assert(!path_is_absolute(filename));
216
217 /* And we can thus fix the protocol detection issue by prefixing it
218 * by "./" */
219 fat_filename = qstring_from_str("./");
220 qstring_append(fat_filename, filename);
221
222 assert(!path_has_protocol(qstring_get_str(fat_filename)));
223
224 qdict_put(options, "filename", fat_filename);
225 } else {
226 /* If no protocol prefix was detected, we can use the shortened
227 * filename as-is */
228 qdict_put_str(options, "filename", filename);
229 }
230 }
231}
232
233
Kevin Wolf9c5e6592017-05-04 18:52:40 +0200234/* Returns whether the image file is opened as read-only. Note that this can
235 * return false and writing to the image file is still not possible because the
236 * image is inactivated. */
Jeff Cody93ed5242017-04-07 16:55:28 -0400237bool bdrv_is_read_only(BlockDriverState *bs)
238{
239 return bs->read_only;
240}
241
Kevin Wolf9c5e6592017-05-04 18:52:40 +0200242/* Returns whether the image file can be written to right now */
243bool bdrv_is_writable(BlockDriverState *bs)
244{
245 return !bdrv_is_read_only(bs) && !(bs->open_flags & BDRV_O_INACTIVE);
246}
247
Kevin Wolf54a32bf2017-08-03 17:02:58 +0200248int bdrv_can_set_read_only(BlockDriverState *bs, bool read_only,
249 bool ignore_allow_rdw, Error **errp)
Jeff Codyfe5241b2017-04-07 16:55:25 -0400250{
Jeff Codye2b82472017-04-07 16:55:26 -0400251 /* Do not set read_only if copy_on_read is enabled */
252 if (bs->copy_on_read && read_only) {
253 error_setg(errp, "Can't set node '%s' to r/o with copy-on-read enabled",
254 bdrv_get_device_or_node_name(bs));
255 return -EINVAL;
256 }
257
Jeff Codyd6fcdf02017-04-07 16:55:27 -0400258 /* Do not clear read_only if it is prohibited */
Kevin Wolf54a32bf2017-08-03 17:02:58 +0200259 if (!read_only && !(bs->open_flags & BDRV_O_ALLOW_RDWR) &&
260 !ignore_allow_rdw)
261 {
Jeff Codyd6fcdf02017-04-07 16:55:27 -0400262 error_setg(errp, "Node '%s' is read only",
263 bdrv_get_device_or_node_name(bs));
264 return -EPERM;
265 }
266
Jeff Cody45803a02017-04-07 16:55:29 -0400267 return 0;
268}
269
270int bdrv_set_read_only(BlockDriverState *bs, bool read_only, Error **errp)
271{
272 int ret = 0;
273
Kevin Wolf54a32bf2017-08-03 17:02:58 +0200274 ret = bdrv_can_set_read_only(bs, read_only, false, errp);
Jeff Cody45803a02017-04-07 16:55:29 -0400275 if (ret < 0) {
276 return ret;
277 }
278
Jeff Codyfe5241b2017-04-07 16:55:25 -0400279 bs->read_only = read_only;
Jeff Codye2b82472017-04-07 16:55:26 -0400280 return 0;
Jeff Codyfe5241b2017-04-07 16:55:25 -0400281}
282
Max Reitz0a828552014-11-26 17:20:25 +0100283void bdrv_get_full_backing_filename_from_filename(const char *backed,
284 const char *backing,
Max Reitz9f074292014-11-26 17:20:26 +0100285 char *dest, size_t sz,
286 Error **errp)
Max Reitz0a828552014-11-26 17:20:25 +0100287{
Max Reitz9f074292014-11-26 17:20:26 +0100288 if (backing[0] == '\0' || path_has_protocol(backing) ||
289 path_is_absolute(backing))
290 {
Max Reitz0a828552014-11-26 17:20:25 +0100291 pstrcpy(dest, sz, backing);
Max Reitz9f074292014-11-26 17:20:26 +0100292 } else if (backed[0] == '\0' || strstart(backed, "json:", NULL)) {
293 error_setg(errp, "Cannot use relative backing file names for '%s'",
294 backed);
Max Reitz0a828552014-11-26 17:20:25 +0100295 } else {
296 path_combine(dest, sz, backed, backing);
297 }
298}
299
Max Reitz9f074292014-11-26 17:20:26 +0100300void bdrv_get_full_backing_filename(BlockDriverState *bs, char *dest, size_t sz,
301 Error **errp)
Paolo Bonzinidc5a1372012-05-08 16:51:50 +0200302{
Max Reitz9f074292014-11-26 17:20:26 +0100303 char *backed = bs->exact_filename[0] ? bs->exact_filename : bs->filename;
304
305 bdrv_get_full_backing_filename_from_filename(backed, bs->backing_file,
306 dest, sz, errp);
Paolo Bonzinidc5a1372012-05-08 16:51:50 +0200307}
308
Stefan Hajnoczi0eb72172015-04-28 14:27:51 +0100309void bdrv_register(BlockDriver *bdrv)
310{
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100311 QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
bellardea2384d2004-08-01 21:59:26 +0000312}
bellardb3380822004-03-14 21:38:54 +0000313
Markus Armbrustere4e99862014-10-07 13:59:03 +0200314BlockDriverState *bdrv_new(void)
315{
316 BlockDriverState *bs;
317 int i;
318
Markus Armbruster5839e532014-08-19 10:31:08 +0200319 bs = g_new0(BlockDriverState, 1);
Fam Zhenge4654d22013-11-13 18:29:43 +0800320 QLIST_INIT(&bs->dirty_bitmaps);
Fam Zhengfbe40ff2014-05-23 21:29:42 +0800321 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
322 QLIST_INIT(&bs->op_blockers[i]);
323 }
Stefan Hajnoczid616b222013-06-24 17:13:10 +0200324 notifier_with_return_list_init(&bs->before_write_notifiers);
Paolo Bonzini3783fa32017-06-05 14:39:02 +0200325 qemu_co_mutex_init(&bs->reqs_lock);
Paolo Bonzini21198822017-06-05 14:39:03 +0200326 qemu_mutex_init(&bs->dirty_bitmap_mutex);
Fam Zheng9fcb0252013-08-23 09:14:46 +0800327 bs->refcnt = 1;
Stefan Hajnoczidcd04222014-05-08 16:34:37 +0200328 bs->aio_context = qemu_get_aio_context();
Paolo Bonzinid7d512f2012-08-23 11:20:36 +0200329
Evgeny Yakovlev3ff2f672016-07-18 22:39:52 +0300330 qemu_co_queue_init(&bs->flush_queue);
331
Max Reitz2c1d04e2016-01-29 16:36:11 +0100332 QTAILQ_INSERT_TAIL(&all_bdrv_states, bs, bs_list);
333
bellardb3380822004-03-14 21:38:54 +0000334 return bs;
335}
336
Marc Mari88d88792016-08-12 09:27:03 -0400337static BlockDriver *bdrv_do_find_format(const char *format_name)
bellardea2384d2004-08-01 21:59:26 +0000338{
339 BlockDriver *drv1;
Marc Mari88d88792016-08-12 09:27:03 -0400340
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100341 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
342 if (!strcmp(drv1->format_name, format_name)) {
bellardea2384d2004-08-01 21:59:26 +0000343 return drv1;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100344 }
bellardea2384d2004-08-01 21:59:26 +0000345 }
Marc Mari88d88792016-08-12 09:27:03 -0400346
bellardea2384d2004-08-01 21:59:26 +0000347 return NULL;
348}
349
Marc Mari88d88792016-08-12 09:27:03 -0400350BlockDriver *bdrv_find_format(const char *format_name)
351{
352 BlockDriver *drv1;
353 int i;
354
355 drv1 = bdrv_do_find_format(format_name);
356 if (drv1) {
357 return drv1;
358 }
359
360 /* The driver isn't registered, maybe we need to load a module */
361 for (i = 0; i < (int)ARRAY_SIZE(block_driver_modules); ++i) {
362 if (!strcmp(block_driver_modules[i].format_name, format_name)) {
363 block_module_load_one(block_driver_modules[i].library_name);
364 break;
365 }
366 }
367
368 return bdrv_do_find_format(format_name);
369}
370
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800371static int bdrv_is_whitelisted(BlockDriver *drv, bool read_only)
Markus Armbrustereb852012009-10-27 18:41:44 +0100372{
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800373 static const char *whitelist_rw[] = {
374 CONFIG_BDRV_RW_WHITELIST
375 };
376 static const char *whitelist_ro[] = {
377 CONFIG_BDRV_RO_WHITELIST
Markus Armbrustereb852012009-10-27 18:41:44 +0100378 };
379 const char **p;
380
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800381 if (!whitelist_rw[0] && !whitelist_ro[0]) {
Markus Armbrustereb852012009-10-27 18:41:44 +0100382 return 1; /* no whitelist, anything goes */
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800383 }
Markus Armbrustereb852012009-10-27 18:41:44 +0100384
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800385 for (p = whitelist_rw; *p; p++) {
Markus Armbrustereb852012009-10-27 18:41:44 +0100386 if (!strcmp(drv->format_name, *p)) {
387 return 1;
388 }
389 }
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800390 if (read_only) {
391 for (p = whitelist_ro; *p; p++) {
392 if (!strcmp(drv->format_name, *p)) {
393 return 1;
394 }
395 }
396 }
Markus Armbrustereb852012009-10-27 18:41:44 +0100397 return 0;
398}
399
Daniel P. Berrangee6ff69b2016-03-21 14:11:48 +0000400bool bdrv_uses_whitelist(void)
401{
402 return use_bdrv_whitelist;
403}
404
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800405typedef struct CreateCo {
406 BlockDriver *drv;
407 char *filename;
Chunyan Liu83d05212014-06-05 17:20:51 +0800408 QemuOpts *opts;
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800409 int ret;
Max Reitzcc84d902013-09-06 17:14:26 +0200410 Error *err;
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800411} CreateCo;
412
413static void coroutine_fn bdrv_create_co_entry(void *opaque)
414{
Max Reitzcc84d902013-09-06 17:14:26 +0200415 Error *local_err = NULL;
416 int ret;
417
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800418 CreateCo *cco = opaque;
419 assert(cco->drv);
420
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800421 ret = cco->drv->bdrv_create(cco->filename, cco->opts, &local_err);
Eduardo Habkost621ff942016-06-13 18:57:56 -0300422 error_propagate(&cco->err, local_err);
Max Reitzcc84d902013-09-06 17:14:26 +0200423 cco->ret = ret;
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800424}
425
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200426int bdrv_create(BlockDriver *drv, const char* filename,
Chunyan Liu83d05212014-06-05 17:20:51 +0800427 QemuOpts *opts, Error **errp)
bellardea2384d2004-08-01 21:59:26 +0000428{
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800429 int ret;
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200430
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800431 Coroutine *co;
432 CreateCo cco = {
433 .drv = drv,
434 .filename = g_strdup(filename),
Chunyan Liu83d05212014-06-05 17:20:51 +0800435 .opts = opts,
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800436 .ret = NOT_DONE,
Max Reitzcc84d902013-09-06 17:14:26 +0200437 .err = NULL,
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800438 };
439
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800440 if (!drv->bdrv_create) {
Max Reitzcc84d902013-09-06 17:14:26 +0200441 error_setg(errp, "Driver '%s' does not support image creation", drv->format_name);
Luiz Capitulino80168bf2012-10-17 16:45:25 -0300442 ret = -ENOTSUP;
443 goto out;
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800444 }
445
446 if (qemu_in_coroutine()) {
447 /* Fast-path if already in coroutine context */
448 bdrv_create_co_entry(&cco);
449 } else {
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200450 co = qemu_coroutine_create(bdrv_create_co_entry, &cco);
451 qemu_coroutine_enter(co);
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800452 while (cco.ret == NOT_DONE) {
Paolo Bonzinib47ec2c2014-07-07 15:18:01 +0200453 aio_poll(qemu_get_aio_context(), true);
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800454 }
455 }
456
457 ret = cco.ret;
Max Reitzcc84d902013-09-06 17:14:26 +0200458 if (ret < 0) {
Markus Armbruster84d18f02014-01-30 15:07:28 +0100459 if (cco.err) {
Max Reitzcc84d902013-09-06 17:14:26 +0200460 error_propagate(errp, cco.err);
461 } else {
462 error_setg_errno(errp, -ret, "Could not create image");
463 }
464 }
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800465
Luiz Capitulino80168bf2012-10-17 16:45:25 -0300466out:
467 g_free(cco.filename);
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800468 return ret;
bellardea2384d2004-08-01 21:59:26 +0000469}
470
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800471int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp)
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200472{
473 BlockDriver *drv;
Max Reitzcc84d902013-09-06 17:14:26 +0200474 Error *local_err = NULL;
475 int ret;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200476
Max Reitzb65a5e12015-02-05 13:58:12 -0500477 drv = bdrv_find_protocol(filename, true, errp);
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200478 if (drv == NULL) {
Stefan Hajnoczi16905d72010-11-30 15:14:14 +0000479 return -ENOENT;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200480 }
481
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800482 ret = bdrv_create(drv, filename, opts, &local_err);
Eduardo Habkost621ff942016-06-13 18:57:56 -0300483 error_propagate(errp, local_err);
Max Reitzcc84d902013-09-06 17:14:26 +0200484 return ret;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200485}
486
Ekaterina Tumanova892b7de2015-02-16 12:47:54 +0100487/**
488 * Try to get @bs's logical and physical block size.
489 * On success, store them in @bsz struct and return 0.
490 * On failure return -errno.
491 * @bs must not be empty.
492 */
493int bdrv_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz)
494{
495 BlockDriver *drv = bs->drv;
496
497 if (drv && drv->bdrv_probe_blocksizes) {
498 return drv->bdrv_probe_blocksizes(bs, bsz);
499 }
500
501 return -ENOTSUP;
502}
503
504/**
505 * Try to get @bs's geometry (cyls, heads, sectors).
506 * On success, store them in @geo struct and return 0.
507 * On failure return -errno.
508 * @bs must not be empty.
509 */
510int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo)
511{
512 BlockDriver *drv = bs->drv;
513
514 if (drv && drv->bdrv_probe_geometry) {
515 return drv->bdrv_probe_geometry(bs, geo);
516 }
517
518 return -ENOTSUP;
519}
520
Jim Meyeringeba25052012-05-28 09:27:54 +0200521/*
522 * Create a uniquely-named empty temporary file.
523 * Return 0 upon success, otherwise a negative errno value.
524 */
525int get_tmp_filename(char *filename, int size)
526{
bellardd5249392004-08-03 21:14:23 +0000527#ifdef _WIN32
bellard3b9f94e2007-01-07 17:27:07 +0000528 char temp_dir[MAX_PATH];
Jim Meyeringeba25052012-05-28 09:27:54 +0200529 /* GetTempFileName requires that its output buffer (4th param)
530 have length MAX_PATH or greater. */
531 assert(size >= MAX_PATH);
532 return (GetTempPath(MAX_PATH, temp_dir)
533 && GetTempFileName(temp_dir, "qem", 0, filename)
534 ? 0 : -GetLastError());
bellardd5249392004-08-03 21:14:23 +0000535#else
bellardea2384d2004-08-01 21:59:26 +0000536 int fd;
blueswir17ccfb2e2008-09-14 06:45:34 +0000537 const char *tmpdir;
aurel320badc1e2008-03-10 00:05:34 +0000538 tmpdir = getenv("TMPDIR");
Amit Shah69bef792014-02-26 15:12:37 +0530539 if (!tmpdir) {
540 tmpdir = "/var/tmp";
541 }
Jim Meyeringeba25052012-05-28 09:27:54 +0200542 if (snprintf(filename, size, "%s/vl.XXXXXX", tmpdir) >= size) {
543 return -EOVERFLOW;
544 }
bellardea2384d2004-08-01 21:59:26 +0000545 fd = mkstemp(filename);
Dunrong Huangfe235a02012-09-05 21:26:22 +0800546 if (fd < 0) {
547 return -errno;
548 }
549 if (close(fd) != 0) {
550 unlink(filename);
Jim Meyeringeba25052012-05-28 09:27:54 +0200551 return -errno;
552 }
553 return 0;
bellardd5249392004-08-03 21:14:23 +0000554#endif
Jim Meyeringeba25052012-05-28 09:27:54 +0200555}
bellardea2384d2004-08-01 21:59:26 +0000556
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200557/*
558 * Detect host devices. By convention, /dev/cdrom[N] is always
559 * recognized as a host CDROM.
560 */
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200561static BlockDriver *find_hdev_driver(const char *filename)
562{
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200563 int score_max = 0, score;
564 BlockDriver *drv = NULL, *d;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200565
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100566 QLIST_FOREACH(d, &bdrv_drivers, list) {
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200567 if (d->bdrv_probe_device) {
568 score = d->bdrv_probe_device(filename);
569 if (score > score_max) {
570 score_max = score;
571 drv = d;
572 }
573 }
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200574 }
575
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200576 return drv;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200577}
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200578
Marc Mari88d88792016-08-12 09:27:03 -0400579static BlockDriver *bdrv_do_find_protocol(const char *protocol)
580{
581 BlockDriver *drv1;
582
583 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
584 if (drv1->protocol_name && !strcmp(drv1->protocol_name, protocol)) {
585 return drv1;
586 }
587 }
588
589 return NULL;
590}
591
Kevin Wolf98289622013-07-10 15:47:39 +0200592BlockDriver *bdrv_find_protocol(const char *filename,
Max Reitzb65a5e12015-02-05 13:58:12 -0500593 bool allow_protocol_prefix,
594 Error **errp)
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200595{
596 BlockDriver *drv1;
597 char protocol[128];
598 int len;
599 const char *p;
Marc Mari88d88792016-08-12 09:27:03 -0400600 int i;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200601
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200602 /* TODO Drivers without bdrv_file_open must be specified explicitly */
603
Christoph Hellwig39508e72010-06-23 12:25:17 +0200604 /*
605 * XXX(hch): we really should not let host device detection
606 * override an explicit protocol specification, but moving this
607 * later breaks access to device names with colons in them.
608 * Thanks to the brain-dead persistent naming schemes on udev-
609 * based Linux systems those actually are quite common.
610 */
611 drv1 = find_hdev_driver(filename);
612 if (drv1) {
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200613 return drv1;
614 }
Christoph Hellwig39508e72010-06-23 12:25:17 +0200615
Kevin Wolf98289622013-07-10 15:47:39 +0200616 if (!path_has_protocol(filename) || !allow_protocol_prefix) {
Max Reitzef810432014-12-02 18:32:42 +0100617 return &bdrv_file;
Christoph Hellwig39508e72010-06-23 12:25:17 +0200618 }
Kevin Wolf98289622013-07-10 15:47:39 +0200619
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000620 p = strchr(filename, ':');
621 assert(p != NULL);
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200622 len = p - filename;
623 if (len > sizeof(protocol) - 1)
624 len = sizeof(protocol) - 1;
625 memcpy(protocol, filename, len);
626 protocol[len] = '\0';
Marc Mari88d88792016-08-12 09:27:03 -0400627
628 drv1 = bdrv_do_find_protocol(protocol);
629 if (drv1) {
630 return drv1;
631 }
632
633 for (i = 0; i < (int)ARRAY_SIZE(block_driver_modules); ++i) {
634 if (block_driver_modules[i].protocol_name &&
635 !strcmp(block_driver_modules[i].protocol_name, protocol)) {
636 block_module_load_one(block_driver_modules[i].library_name);
637 break;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200638 }
639 }
Max Reitzb65a5e12015-02-05 13:58:12 -0500640
Marc Mari88d88792016-08-12 09:27:03 -0400641 drv1 = bdrv_do_find_protocol(protocol);
642 if (!drv1) {
643 error_setg(errp, "Unknown protocol '%s'", protocol);
644 }
645 return drv1;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200646}
647
Markus Armbrusterc6684242014-11-20 16:27:10 +0100648/*
649 * Guess image format by probing its contents.
650 * This is not a good idea when your image is raw (CVE-2008-2004), but
651 * we do it anyway for backward compatibility.
652 *
653 * @buf contains the image's first @buf_size bytes.
Kevin Wolf7cddd372014-11-20 16:27:11 +0100654 * @buf_size is the buffer size in bytes (generally BLOCK_PROBE_BUF_SIZE,
655 * but can be smaller if the image file is smaller)
Markus Armbrusterc6684242014-11-20 16:27:10 +0100656 * @filename is its filename.
657 *
658 * For all block drivers, call the bdrv_probe() method to get its
659 * probing score.
660 * Return the first block driver with the highest probing score.
661 */
Kevin Wolf38f3ef52014-11-20 16:27:12 +0100662BlockDriver *bdrv_probe_all(const uint8_t *buf, int buf_size,
663 const char *filename)
Markus Armbrusterc6684242014-11-20 16:27:10 +0100664{
665 int score_max = 0, score;
666 BlockDriver *drv = NULL, *d;
667
668 QLIST_FOREACH(d, &bdrv_drivers, list) {
669 if (d->bdrv_probe) {
670 score = d->bdrv_probe(buf, buf_size, filename);
671 if (score > score_max) {
672 score_max = score;
673 drv = d;
674 }
675 }
676 }
677
678 return drv;
679}
680
Kevin Wolf5696c6e2017-02-17 18:39:24 +0100681static int find_image_format(BlockBackend *file, const char *filename,
Max Reitz34b5d2c2013-09-05 14:45:29 +0200682 BlockDriver **pdrv, Error **errp)
bellardea2384d2004-08-01 21:59:26 +0000683{
Markus Armbrusterc6684242014-11-20 16:27:10 +0100684 BlockDriver *drv;
Kevin Wolf7cddd372014-11-20 16:27:11 +0100685 uint8_t buf[BLOCK_PROBE_BUF_SIZE];
Kevin Wolff500a6d2012-11-12 17:35:27 +0100686 int ret = 0;
Nicholas Bellingerf8ea0b02010-05-17 09:45:57 -0700687
Kevin Wolf08a00552010-06-01 18:37:31 +0200688 /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
Kevin Wolf5696c6e2017-02-17 18:39:24 +0100689 if (blk_is_sg(file) || !blk_is_inserted(file) || blk_getlength(file) == 0) {
Max Reitzef810432014-12-02 18:32:42 +0100690 *pdrv = &bdrv_raw;
Stefan Weilc98ac352010-07-21 21:51:51 +0200691 return ret;
Nicholas A. Bellinger1a396852010-05-27 08:56:28 -0700692 }
Nicholas Bellingerf8ea0b02010-05-17 09:45:57 -0700693
Kevin Wolf5696c6e2017-02-17 18:39:24 +0100694 ret = blk_pread(file, 0, buf, sizeof(buf));
bellard83f64092006-08-01 16:21:11 +0000695 if (ret < 0) {
Max Reitz34b5d2c2013-09-05 14:45:29 +0200696 error_setg_errno(errp, -ret, "Could not read image for determining its "
697 "format");
Stefan Weilc98ac352010-07-21 21:51:51 +0200698 *pdrv = NULL;
699 return ret;
bellard83f64092006-08-01 16:21:11 +0000700 }
701
Markus Armbrusterc6684242014-11-20 16:27:10 +0100702 drv = bdrv_probe_all(buf, ret, filename);
Stefan Weilc98ac352010-07-21 21:51:51 +0200703 if (!drv) {
Max Reitz34b5d2c2013-09-05 14:45:29 +0200704 error_setg(errp, "Could not determine image format: No compatible "
705 "driver found");
Stefan Weilc98ac352010-07-21 21:51:51 +0200706 ret = -ENOENT;
707 }
708 *pdrv = drv;
709 return ret;
bellardea2384d2004-08-01 21:59:26 +0000710}
711
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100712/**
713 * Set the current 'total_sectors' value
Markus Armbruster65a9bb22014-06-26 13:23:17 +0200714 * Return 0 on success, -errno on error.
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100715 */
716static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
717{
718 BlockDriver *drv = bs->drv;
719
Nicholas Bellinger396759a2010-05-17 09:46:04 -0700720 /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
Dimitris Aragiorgisb192af82015-06-23 13:44:56 +0300721 if (bdrv_is_sg(bs))
Nicholas Bellinger396759a2010-05-17 09:46:04 -0700722 return 0;
723
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100724 /* query actual device if possible, otherwise just trust the hint */
725 if (drv->bdrv_getlength) {
726 int64_t length = drv->bdrv_getlength(bs);
727 if (length < 0) {
728 return length;
729 }
Fam Zheng7e382002013-11-06 19:48:06 +0800730 hint = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE);
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100731 }
732
733 bs->total_sectors = hint;
734 return 0;
735}
736
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100737/**
Kevin Wolfcddff5b2015-11-16 16:43:27 +0100738 * Combines a QDict of new block driver @options with any missing options taken
739 * from @old_options, so that leaving out an option defaults to its old value.
740 */
741static void bdrv_join_options(BlockDriverState *bs, QDict *options,
742 QDict *old_options)
743{
744 if (bs->drv && bs->drv->bdrv_join_options) {
745 bs->drv->bdrv_join_options(options, old_options);
746 } else {
747 qdict_join(options, old_options, false);
748 }
749}
750
751/**
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100752 * Set open flags for a given discard mode
753 *
754 * Return 0 on success, -1 if the discard mode was invalid.
755 */
756int bdrv_parse_discard_flags(const char *mode, int *flags)
757{
758 *flags &= ~BDRV_O_UNMAP;
759
760 if (!strcmp(mode, "off") || !strcmp(mode, "ignore")) {
761 /* do nothing */
762 } else if (!strcmp(mode, "on") || !strcmp(mode, "unmap")) {
763 *flags |= BDRV_O_UNMAP;
764 } else {
765 return -1;
766 }
767
768 return 0;
769}
770
771/**
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100772 * Set open flags for a given cache mode
773 *
774 * Return 0 on success, -1 if the cache mode was invalid.
775 */
Kevin Wolf53e8ae02016-03-18 15:36:58 +0100776int bdrv_parse_cache_mode(const char *mode, int *flags, bool *writethrough)
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100777{
778 *flags &= ~BDRV_O_CACHE_MASK;
779
780 if (!strcmp(mode, "off") || !strcmp(mode, "none")) {
Kevin Wolf53e8ae02016-03-18 15:36:58 +0100781 *writethrough = false;
782 *flags |= BDRV_O_NOCACHE;
Stefan Hajnoczi92196b22011-08-04 12:26:52 +0100783 } else if (!strcmp(mode, "directsync")) {
Kevin Wolf53e8ae02016-03-18 15:36:58 +0100784 *writethrough = true;
Stefan Hajnoczi92196b22011-08-04 12:26:52 +0100785 *flags |= BDRV_O_NOCACHE;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100786 } else if (!strcmp(mode, "writeback")) {
Kevin Wolf53e8ae02016-03-18 15:36:58 +0100787 *writethrough = false;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100788 } else if (!strcmp(mode, "unsafe")) {
Kevin Wolf53e8ae02016-03-18 15:36:58 +0100789 *writethrough = false;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100790 *flags |= BDRV_O_NO_FLUSH;
791 } else if (!strcmp(mode, "writethrough")) {
Kevin Wolf53e8ae02016-03-18 15:36:58 +0100792 *writethrough = true;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100793 } else {
794 return -1;
795 }
796
797 return 0;
798}
799
Kevin Wolfb5411552017-01-17 15:56:16 +0100800static char *bdrv_child_get_parent_desc(BdrvChild *c)
801{
802 BlockDriverState *parent = c->opaque;
803 return g_strdup(bdrv_get_device_or_node_name(parent));
804}
805
Kevin Wolf20018e12016-05-23 18:46:59 +0200806static void bdrv_child_cb_drained_begin(BdrvChild *child)
807{
808 BlockDriverState *bs = child->opaque;
809 bdrv_drained_begin(bs);
810}
811
812static void bdrv_child_cb_drained_end(BdrvChild *child)
813{
814 BlockDriverState *bs = child->opaque;
815 bdrv_drained_end(bs);
816}
817
Kevin Wolf38701b62017-05-04 18:52:39 +0200818static int bdrv_child_cb_inactivate(BdrvChild *child)
819{
820 BlockDriverState *bs = child->opaque;
821 assert(bs->open_flags & BDRV_O_INACTIVE);
822 return 0;
823}
824
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200825/*
Kevin Wolf73176be2016-03-07 13:02:15 +0100826 * Returns the options and flags that a temporary snapshot should get, based on
827 * the originally requested flags (the originally requested image will have
828 * flags like a backing file)
Kevin Wolfb1e6fc02014-05-06 12:11:42 +0200829 */
Kevin Wolf73176be2016-03-07 13:02:15 +0100830static void bdrv_temp_snapshot_options(int *child_flags, QDict *child_options,
831 int parent_flags, QDict *parent_options)
Kevin Wolfb1e6fc02014-05-06 12:11:42 +0200832{
Kevin Wolf73176be2016-03-07 13:02:15 +0100833 *child_flags = (parent_flags & ~BDRV_O_SNAPSHOT) | BDRV_O_TEMPORARY;
834
835 /* For temporary files, unconditional cache=unsafe is fine */
Kevin Wolf73176be2016-03-07 13:02:15 +0100836 qdict_set_default_str(child_options, BDRV_OPT_CACHE_DIRECT, "off");
837 qdict_set_default_str(child_options, BDRV_OPT_CACHE_NO_FLUSH, "on");
Kevin Wolf41869042016-06-16 12:59:30 +0200838
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300839 /* Copy the read-only option from the parent */
840 qdict_copy_default(child_options, parent_options, BDRV_OPT_READ_ONLY);
841
Kevin Wolf41869042016-06-16 12:59:30 +0200842 /* aio=native doesn't work for cache.direct=off, so disable it for the
843 * temporary snapshot */
844 *child_flags &= ~BDRV_O_NATIVE_AIO;
Kevin Wolfb1e6fc02014-05-06 12:11:42 +0200845}
846
847/*
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200848 * Returns the options and flags that bs->file should get if a protocol driver
849 * is expected, based on the given options and flags for the parent BDS
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200850 */
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200851static void bdrv_inherited_options(int *child_flags, QDict *child_options,
852 int parent_flags, QDict *parent_options)
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200853{
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200854 int flags = parent_flags;
855
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200856 /* Enable protocol handling, disable format probing for bs->file */
857 flags |= BDRV_O_PROTOCOL;
858
Kevin Wolf91a097e2015-05-08 17:49:53 +0200859 /* If the cache mode isn't explicitly set, inherit direct and no-flush from
860 * the parent. */
861 qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_DIRECT);
862 qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_NO_FLUSH);
Fam Zheng5a9347c2017-05-03 00:35:37 +0800863 qdict_copy_default(child_options, parent_options, BDRV_OPT_FORCE_SHARE);
Kevin Wolf91a097e2015-05-08 17:49:53 +0200864
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300865 /* Inherit the read-only option from the parent if it's not set */
866 qdict_copy_default(child_options, parent_options, BDRV_OPT_READ_ONLY);
867
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200868 /* Our block drivers take care to send flushes and respect unmap policy,
Kevin Wolf91a097e2015-05-08 17:49:53 +0200869 * so we can default to enable both on lower layers regardless of the
870 * corresponding parent options. */
Kevin Wolf818584a2016-09-12 18:03:18 +0200871 qdict_set_default_str(child_options, BDRV_OPT_DISCARD, "unmap");
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200872
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200873 /* Clear flags that only apply to the top layer */
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000874 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ |
875 BDRV_O_NO_IO);
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200876
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200877 *child_flags = flags;
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200878}
879
Kevin Wolff3930ed2015-04-08 13:43:47 +0200880const BdrvChildRole child_file = {
Kevin Wolfb5411552017-01-17 15:56:16 +0100881 .get_parent_desc = bdrv_child_get_parent_desc,
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200882 .inherit_options = bdrv_inherited_options,
Kevin Wolf20018e12016-05-23 18:46:59 +0200883 .drained_begin = bdrv_child_cb_drained_begin,
884 .drained_end = bdrv_child_cb_drained_end,
Kevin Wolf38701b62017-05-04 18:52:39 +0200885 .inactivate = bdrv_child_cb_inactivate,
Kevin Wolff3930ed2015-04-08 13:43:47 +0200886};
887
888/*
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200889 * Returns the options and flags that bs->file should get if the use of formats
890 * (and not only protocols) is permitted for it, based on the given options and
891 * flags for the parent BDS
Kevin Wolff3930ed2015-04-08 13:43:47 +0200892 */
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200893static void bdrv_inherited_fmt_options(int *child_flags, QDict *child_options,
894 int parent_flags, QDict *parent_options)
Kevin Wolff3930ed2015-04-08 13:43:47 +0200895{
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200896 child_file.inherit_options(child_flags, child_options,
897 parent_flags, parent_options);
898
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000899 *child_flags &= ~(BDRV_O_PROTOCOL | BDRV_O_NO_IO);
Kevin Wolff3930ed2015-04-08 13:43:47 +0200900}
901
902const BdrvChildRole child_format = {
Kevin Wolfb5411552017-01-17 15:56:16 +0100903 .get_parent_desc = bdrv_child_get_parent_desc,
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200904 .inherit_options = bdrv_inherited_fmt_options,
Kevin Wolf20018e12016-05-23 18:46:59 +0200905 .drained_begin = bdrv_child_cb_drained_begin,
906 .drained_end = bdrv_child_cb_drained_end,
Kevin Wolf38701b62017-05-04 18:52:39 +0200907 .inactivate = bdrv_child_cb_inactivate,
Kevin Wolff3930ed2015-04-08 13:43:47 +0200908};
909
Kevin Wolfdb95dbb2017-02-08 11:28:52 +0100910static void bdrv_backing_attach(BdrvChild *c)
911{
912 BlockDriverState *parent = c->opaque;
913 BlockDriverState *backing_hd = c->bs;
914
915 assert(!parent->backing_blocker);
916 error_setg(&parent->backing_blocker,
917 "node is used as backing hd of '%s'",
918 bdrv_get_device_or_node_name(parent));
919
920 parent->open_flags &= ~BDRV_O_NO_BACKING;
921 pstrcpy(parent->backing_file, sizeof(parent->backing_file),
922 backing_hd->filename);
923 pstrcpy(parent->backing_format, sizeof(parent->backing_format),
924 backing_hd->drv ? backing_hd->drv->format_name : "");
925
926 bdrv_op_block_all(backing_hd, parent->backing_blocker);
927 /* Otherwise we won't be able to commit or stream */
928 bdrv_op_unblock(backing_hd, BLOCK_OP_TYPE_COMMIT_TARGET,
929 parent->backing_blocker);
930 bdrv_op_unblock(backing_hd, BLOCK_OP_TYPE_STREAM,
931 parent->backing_blocker);
932 /*
933 * We do backup in 3 ways:
934 * 1. drive backup
935 * The target bs is new opened, and the source is top BDS
936 * 2. blockdev backup
937 * Both the source and the target are top BDSes.
938 * 3. internal backup(used for block replication)
939 * Both the source and the target are backing file
940 *
941 * In case 1 and 2, neither the source nor the target is the backing file.
942 * In case 3, we will block the top BDS, so there is only one block job
943 * for the top BDS and its backing chain.
944 */
945 bdrv_op_unblock(backing_hd, BLOCK_OP_TYPE_BACKUP_SOURCE,
946 parent->backing_blocker);
947 bdrv_op_unblock(backing_hd, BLOCK_OP_TYPE_BACKUP_TARGET,
948 parent->backing_blocker);
949}
950
951static void bdrv_backing_detach(BdrvChild *c)
952{
953 BlockDriverState *parent = c->opaque;
954
955 assert(parent->backing_blocker);
956 bdrv_op_unblock_all(c->bs, parent->backing_blocker);
957 error_free(parent->backing_blocker);
958 parent->backing_blocker = NULL;
959}
960
Kevin Wolf317fc442014-04-25 13:27:34 +0200961/*
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200962 * Returns the options and flags that bs->backing should get, based on the
963 * given options and flags for the parent BDS
Kevin Wolf317fc442014-04-25 13:27:34 +0200964 */
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200965static void bdrv_backing_options(int *child_flags, QDict *child_options,
966 int parent_flags, QDict *parent_options)
Kevin Wolf317fc442014-04-25 13:27:34 +0200967{
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200968 int flags = parent_flags;
969
Kevin Wolfb8816a42016-03-04 14:52:32 +0100970 /* The cache mode is inherited unmodified for backing files; except WCE,
971 * which is only applied on the top level (BlockBackend) */
Kevin Wolf91a097e2015-05-08 17:49:53 +0200972 qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_DIRECT);
973 qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_NO_FLUSH);
Fam Zheng5a9347c2017-05-03 00:35:37 +0800974 qdict_copy_default(child_options, parent_options, BDRV_OPT_FORCE_SHARE);
Kevin Wolf91a097e2015-05-08 17:49:53 +0200975
Kevin Wolf317fc442014-04-25 13:27:34 +0200976 /* backing files always opened read-only */
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300977 qdict_set_default_str(child_options, BDRV_OPT_READ_ONLY, "on");
978 flags &= ~BDRV_O_COPY_ON_READ;
Kevin Wolf317fc442014-04-25 13:27:34 +0200979
980 /* snapshot=on is handled on the top layer */
Kevin Wolf8bfea152014-04-11 19:16:36 +0200981 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_TEMPORARY);
Kevin Wolf317fc442014-04-25 13:27:34 +0200982
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200983 *child_flags = flags;
Kevin Wolf317fc442014-04-25 13:27:34 +0200984}
985
Kevin Wolf91ef3822016-12-20 16:23:46 +0100986const BdrvChildRole child_backing = {
Kevin Wolfb5411552017-01-17 15:56:16 +0100987 .get_parent_desc = bdrv_child_get_parent_desc,
Kevin Wolfdb95dbb2017-02-08 11:28:52 +0100988 .attach = bdrv_backing_attach,
989 .detach = bdrv_backing_detach,
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200990 .inherit_options = bdrv_backing_options,
Kevin Wolf20018e12016-05-23 18:46:59 +0200991 .drained_begin = bdrv_child_cb_drained_begin,
992 .drained_end = bdrv_child_cb_drained_end,
Kevin Wolf38701b62017-05-04 18:52:39 +0200993 .inactivate = bdrv_child_cb_inactivate,
Kevin Wolff3930ed2015-04-08 13:43:47 +0200994};
995
Kevin Wolf7b272452012-11-12 17:05:39 +0100996static int bdrv_open_flags(BlockDriverState *bs, int flags)
997{
Kevin Wolf61de4c62016-03-18 17:46:45 +0100998 int open_flags = flags;
Kevin Wolf7b272452012-11-12 17:05:39 +0100999
1000 /*
1001 * Clear flags that are internal to the block layer before opening the
1002 * image.
1003 */
Kevin Wolf20cca272014-06-04 14:33:27 +02001004 open_flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_PROTOCOL);
Kevin Wolf7b272452012-11-12 17:05:39 +01001005
1006 /*
1007 * Snapshots should be writable.
1008 */
Kevin Wolf8bfea152014-04-11 19:16:36 +02001009 if (flags & BDRV_O_TEMPORARY) {
Kevin Wolf7b272452012-11-12 17:05:39 +01001010 open_flags |= BDRV_O_RDWR;
1011 }
1012
1013 return open_flags;
1014}
1015
Kevin Wolf91a097e2015-05-08 17:49:53 +02001016static void update_flags_from_options(int *flags, QemuOpts *opts)
1017{
1018 *flags &= ~BDRV_O_CACHE_MASK;
1019
Kevin Wolf91a097e2015-05-08 17:49:53 +02001020 assert(qemu_opt_find(opts, BDRV_OPT_CACHE_NO_FLUSH));
1021 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, false)) {
1022 *flags |= BDRV_O_NO_FLUSH;
1023 }
1024
1025 assert(qemu_opt_find(opts, BDRV_OPT_CACHE_DIRECT));
1026 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_DIRECT, false)) {
1027 *flags |= BDRV_O_NOCACHE;
1028 }
Alberto Garciaf87a0e22016-09-15 17:53:02 +03001029
1030 *flags &= ~BDRV_O_RDWR;
1031
1032 assert(qemu_opt_find(opts, BDRV_OPT_READ_ONLY));
1033 if (!qemu_opt_get_bool(opts, BDRV_OPT_READ_ONLY, false)) {
1034 *flags |= BDRV_O_RDWR;
1035 }
1036
Kevin Wolf91a097e2015-05-08 17:49:53 +02001037}
1038
1039static void update_options_from_flags(QDict *options, int flags)
1040{
Kevin Wolf91a097e2015-05-08 17:49:53 +02001041 if (!qdict_haskey(options, BDRV_OPT_CACHE_DIRECT)) {
Eric Blake46f5ac22017-04-27 16:58:17 -05001042 qdict_put_bool(options, BDRV_OPT_CACHE_DIRECT, flags & BDRV_O_NOCACHE);
Kevin Wolf91a097e2015-05-08 17:49:53 +02001043 }
1044 if (!qdict_haskey(options, BDRV_OPT_CACHE_NO_FLUSH)) {
Eric Blake46f5ac22017-04-27 16:58:17 -05001045 qdict_put_bool(options, BDRV_OPT_CACHE_NO_FLUSH,
1046 flags & BDRV_O_NO_FLUSH);
Kevin Wolf91a097e2015-05-08 17:49:53 +02001047 }
Alberto Garciaf87a0e22016-09-15 17:53:02 +03001048 if (!qdict_haskey(options, BDRV_OPT_READ_ONLY)) {
Eric Blake46f5ac22017-04-27 16:58:17 -05001049 qdict_put_bool(options, BDRV_OPT_READ_ONLY, !(flags & BDRV_O_RDWR));
Alberto Garciaf87a0e22016-09-15 17:53:02 +03001050 }
Kevin Wolf91a097e2015-05-08 17:49:53 +02001051}
1052
Kevin Wolf636ea372014-01-24 14:11:52 +01001053static void bdrv_assign_node_name(BlockDriverState *bs,
1054 const char *node_name,
1055 Error **errp)
Benoît Canet6913c0c2014-01-23 21:31:33 +01001056{
Jeff Cody15489c72015-10-12 19:36:50 -04001057 char *gen_node_name = NULL;
Benoît Canet6913c0c2014-01-23 21:31:33 +01001058
Jeff Cody15489c72015-10-12 19:36:50 -04001059 if (!node_name) {
1060 node_name = gen_node_name = id_generate(ID_BLOCK);
1061 } else if (!id_wellformed(node_name)) {
1062 /*
1063 * Check for empty string or invalid characters, but not if it is
1064 * generated (generated names use characters not available to the user)
1065 */
Kevin Wolf9aebf3b2014-09-25 09:54:02 +02001066 error_setg(errp, "Invalid node name");
Kevin Wolf636ea372014-01-24 14:11:52 +01001067 return;
Benoît Canet6913c0c2014-01-23 21:31:33 +01001068 }
1069
Benoît Canet0c5e94e2014-02-12 17:15:07 +01001070 /* takes care of avoiding namespaces collisions */
Markus Armbruster7f06d472014-10-07 13:59:12 +02001071 if (blk_by_name(node_name)) {
Benoît Canet0c5e94e2014-02-12 17:15:07 +01001072 error_setg(errp, "node-name=%s is conflicting with a device id",
1073 node_name);
Jeff Cody15489c72015-10-12 19:36:50 -04001074 goto out;
Benoît Canet0c5e94e2014-02-12 17:15:07 +01001075 }
1076
Benoît Canet6913c0c2014-01-23 21:31:33 +01001077 /* takes care of avoiding duplicates node names */
1078 if (bdrv_find_node(node_name)) {
1079 error_setg(errp, "Duplicate node name");
Jeff Cody15489c72015-10-12 19:36:50 -04001080 goto out;
Benoît Canet6913c0c2014-01-23 21:31:33 +01001081 }
1082
1083 /* copy node name into the bs and insert it into the graph list */
1084 pstrcpy(bs->node_name, sizeof(bs->node_name), node_name);
1085 QTAILQ_INSERT_TAIL(&graph_bdrv_states, bs, node_list);
Jeff Cody15489c72015-10-12 19:36:50 -04001086out:
1087 g_free(gen_node_name);
Benoît Canet6913c0c2014-01-23 21:31:33 +01001088}
1089
Kevin Wolf01a56502017-01-18 15:51:56 +01001090static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv,
1091 const char *node_name, QDict *options,
1092 int open_flags, Error **errp)
1093{
1094 Error *local_err = NULL;
1095 int ret;
1096
1097 bdrv_assign_node_name(bs, node_name, &local_err);
1098 if (local_err) {
1099 error_propagate(errp, local_err);
1100 return -EINVAL;
1101 }
1102
1103 bs->drv = drv;
Kevin Wolf680c7f92017-01-18 17:16:41 +01001104 bs->read_only = !(bs->open_flags & BDRV_O_RDWR);
Kevin Wolf01a56502017-01-18 15:51:56 +01001105 bs->opaque = g_malloc0(drv->instance_size);
1106
1107 if (drv->bdrv_file_open) {
1108 assert(!drv->bdrv_needs_filename || bs->filename[0]);
1109 ret = drv->bdrv_file_open(bs, options, open_flags, &local_err);
Kevin Wolf680c7f92017-01-18 17:16:41 +01001110 } else if (drv->bdrv_open) {
Kevin Wolf01a56502017-01-18 15:51:56 +01001111 ret = drv->bdrv_open(bs, options, open_flags, &local_err);
Kevin Wolf680c7f92017-01-18 17:16:41 +01001112 } else {
1113 ret = 0;
Kevin Wolf01a56502017-01-18 15:51:56 +01001114 }
1115
1116 if (ret < 0) {
1117 if (local_err) {
1118 error_propagate(errp, local_err);
1119 } else if (bs->filename[0]) {
1120 error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename);
1121 } else {
1122 error_setg_errno(errp, -ret, "Could not open image");
1123 }
Manos Pitsidianakis180ca192017-07-14 17:35:48 +03001124 goto open_failed;
Kevin Wolf01a56502017-01-18 15:51:56 +01001125 }
1126
1127 ret = refresh_total_sectors(bs, bs->total_sectors);
1128 if (ret < 0) {
1129 error_setg_errno(errp, -ret, "Could not refresh total sector count");
Manos Pitsidianakis180ca192017-07-14 17:35:48 +03001130 return ret;
Kevin Wolf01a56502017-01-18 15:51:56 +01001131 }
1132
1133 bdrv_refresh_limits(bs, &local_err);
1134 if (local_err) {
1135 error_propagate(errp, local_err);
Manos Pitsidianakis180ca192017-07-14 17:35:48 +03001136 return -EINVAL;
Kevin Wolf01a56502017-01-18 15:51:56 +01001137 }
1138
1139 assert(bdrv_opt_mem_align(bs) != 0);
1140 assert(bdrv_min_mem_align(bs) != 0);
1141 assert(is_power_of_2(bs->bl.request_alignment));
1142
1143 return 0;
Manos Pitsidianakis180ca192017-07-14 17:35:48 +03001144open_failed:
1145 bs->drv = NULL;
1146 if (bs->file != NULL) {
1147 bdrv_unref_child(bs, bs->file);
1148 bs->file = NULL;
1149 }
Kevin Wolf01a56502017-01-18 15:51:56 +01001150 g_free(bs->opaque);
1151 bs->opaque = NULL;
Kevin Wolf01a56502017-01-18 15:51:56 +01001152 return ret;
1153}
1154
Kevin Wolf680c7f92017-01-18 17:16:41 +01001155BlockDriverState *bdrv_new_open_driver(BlockDriver *drv, const char *node_name,
1156 int flags, Error **errp)
1157{
1158 BlockDriverState *bs;
1159 int ret;
1160
1161 bs = bdrv_new();
1162 bs->open_flags = flags;
1163 bs->explicit_options = qdict_new();
1164 bs->options = qdict_new();
1165 bs->opaque = NULL;
1166
1167 update_options_from_flags(bs->options, flags);
1168
1169 ret = bdrv_open_driver(bs, drv, node_name, bs->options, flags, errp);
1170 if (ret < 0) {
1171 QDECREF(bs->explicit_options);
Manos Pitsidianakis180ca192017-07-14 17:35:48 +03001172 bs->explicit_options = NULL;
Kevin Wolf680c7f92017-01-18 17:16:41 +01001173 QDECREF(bs->options);
Manos Pitsidianakis180ca192017-07-14 17:35:48 +03001174 bs->options = NULL;
Kevin Wolf680c7f92017-01-18 17:16:41 +01001175 bdrv_unref(bs);
1176 return NULL;
1177 }
1178
1179 return bs;
1180}
1181
Kevin Wolfc5f30142016-10-06 11:33:17 +02001182QemuOptsList bdrv_runtime_opts = {
Kevin Wolf18edf282015-04-07 17:12:56 +02001183 .name = "bdrv_common",
1184 .head = QTAILQ_HEAD_INITIALIZER(bdrv_runtime_opts.head),
1185 .desc = {
1186 {
1187 .name = "node-name",
1188 .type = QEMU_OPT_STRING,
1189 .help = "Node name of the block device node",
1190 },
Kevin Wolf62392eb2015-04-24 16:38:02 +02001191 {
1192 .name = "driver",
1193 .type = QEMU_OPT_STRING,
1194 .help = "Block driver to use for the node",
1195 },
Kevin Wolf91a097e2015-05-08 17:49:53 +02001196 {
Kevin Wolf91a097e2015-05-08 17:49:53 +02001197 .name = BDRV_OPT_CACHE_DIRECT,
1198 .type = QEMU_OPT_BOOL,
1199 .help = "Bypass software writeback cache on the host",
1200 },
1201 {
1202 .name = BDRV_OPT_CACHE_NO_FLUSH,
1203 .type = QEMU_OPT_BOOL,
1204 .help = "Ignore flush requests",
1205 },
Alberto Garciaf87a0e22016-09-15 17:53:02 +03001206 {
1207 .name = BDRV_OPT_READ_ONLY,
1208 .type = QEMU_OPT_BOOL,
1209 .help = "Node is opened in read-only mode",
1210 },
Kevin Wolf692e01a2016-09-12 21:00:41 +02001211 {
1212 .name = "detect-zeroes",
1213 .type = QEMU_OPT_STRING,
1214 .help = "try to optimize zero writes (off, on, unmap)",
1215 },
Kevin Wolf818584a2016-09-12 18:03:18 +02001216 {
1217 .name = "discard",
1218 .type = QEMU_OPT_STRING,
1219 .help = "discard operation (ignore/off, unmap/on)",
1220 },
Fam Zheng5a9347c2017-05-03 00:35:37 +08001221 {
1222 .name = BDRV_OPT_FORCE_SHARE,
1223 .type = QEMU_OPT_BOOL,
1224 .help = "always accept other writers (default: off)",
1225 },
Kevin Wolf18edf282015-04-07 17:12:56 +02001226 { /* end of list */ }
1227 },
1228};
1229
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001230/*
Kevin Wolf57915332010-04-14 15:24:50 +02001231 * Common part for opening disk images and files
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001232 *
1233 * Removes all processed options from *options.
Kevin Wolf57915332010-04-14 15:24:50 +02001234 */
Kevin Wolf5696c6e2017-02-17 18:39:24 +01001235static int bdrv_open_common(BlockDriverState *bs, BlockBackend *file,
Kevin Wolf82dc8b42016-01-11 19:07:50 +01001236 QDict *options, Error **errp)
Kevin Wolf57915332010-04-14 15:24:50 +02001237{
1238 int ret, open_flags;
Kevin Wolf035fccd2013-04-09 14:34:19 +02001239 const char *filename;
Kevin Wolf62392eb2015-04-24 16:38:02 +02001240 const char *driver_name = NULL;
Benoît Canet6913c0c2014-01-23 21:31:33 +01001241 const char *node_name = NULL;
Kevin Wolf818584a2016-09-12 18:03:18 +02001242 const char *discard;
Kevin Wolf692e01a2016-09-12 21:00:41 +02001243 const char *detect_zeroes;
Kevin Wolf18edf282015-04-07 17:12:56 +02001244 QemuOpts *opts;
Kevin Wolf62392eb2015-04-24 16:38:02 +02001245 BlockDriver *drv;
Max Reitz34b5d2c2013-09-05 14:45:29 +02001246 Error *local_err = NULL;
Kevin Wolf57915332010-04-14 15:24:50 +02001247
Paolo Bonzini64058752012-05-08 16:51:49 +02001248 assert(bs->file == NULL);
Kevin Wolf707ff822013-03-06 12:20:31 +01001249 assert(options != NULL && bs->options != options);
Kevin Wolf57915332010-04-14 15:24:50 +02001250
Kevin Wolf62392eb2015-04-24 16:38:02 +02001251 opts = qemu_opts_create(&bdrv_runtime_opts, NULL, 0, &error_abort);
1252 qemu_opts_absorb_qdict(opts, options, &local_err);
1253 if (local_err) {
1254 error_propagate(errp, local_err);
1255 ret = -EINVAL;
1256 goto fail_opts;
1257 }
1258
Alberto Garcia9b7e8692016-09-15 17:53:01 +03001259 update_flags_from_options(&bs->open_flags, opts);
1260
Kevin Wolf62392eb2015-04-24 16:38:02 +02001261 driver_name = qemu_opt_get(opts, "driver");
1262 drv = bdrv_find_format(driver_name);
1263 assert(drv != NULL);
1264
Fam Zheng5a9347c2017-05-03 00:35:37 +08001265 bs->force_share = qemu_opt_get_bool(opts, BDRV_OPT_FORCE_SHARE, false);
1266
1267 if (bs->force_share && (bs->open_flags & BDRV_O_RDWR)) {
1268 error_setg(errp,
1269 BDRV_OPT_FORCE_SHARE
1270 "=on can only be used with read-only images");
1271 ret = -EINVAL;
1272 goto fail_opts;
1273 }
1274
Kevin Wolf45673672013-04-22 17:48:40 +02001275 if (file != NULL) {
Kevin Wolf5696c6e2017-02-17 18:39:24 +01001276 filename = blk_bs(file)->filename;
Kevin Wolf45673672013-04-22 17:48:40 +02001277 } else {
Markus Armbruster129c7d12017-03-30 19:43:12 +02001278 /*
1279 * Caution: while qdict_get_try_str() is fine, getting
1280 * non-string types would require more care. When @options
1281 * come from -blockdev or blockdev_add, its members are typed
1282 * according to the QAPI schema, but when they come from
1283 * -drive, they're all QString.
1284 */
Kevin Wolf45673672013-04-22 17:48:40 +02001285 filename = qdict_get_try_str(options, "filename");
1286 }
1287
Max Reitz4a008242017-04-13 18:06:24 +02001288 if (drv->bdrv_needs_filename && (!filename || !filename[0])) {
Kevin Wolf765003d2014-02-03 14:49:42 +01001289 error_setg(errp, "The '%s' block driver requires a file name",
1290 drv->format_name);
Kevin Wolf18edf282015-04-07 17:12:56 +02001291 ret = -EINVAL;
1292 goto fail_opts;
1293 }
1294
Kevin Wolf82dc8b42016-01-11 19:07:50 +01001295 trace_bdrv_open_common(bs, filename ?: "", bs->open_flags,
1296 drv->format_name);
Kevin Wolf62392eb2015-04-24 16:38:02 +02001297
Kevin Wolf82dc8b42016-01-11 19:07:50 +01001298 bs->read_only = !(bs->open_flags & BDRV_O_RDWR);
Fam Zhengb64ec4e2013-05-29 19:35:40 +08001299
1300 if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, bs->read_only)) {
Kevin Wolf8f94a6e2013-10-10 11:45:55 +02001301 error_setg(errp,
1302 !bs->read_only && bdrv_is_whitelisted(drv, true)
1303 ? "Driver '%s' can only be used for read-only devices"
1304 : "Driver '%s' is not whitelisted",
1305 drv->format_name);
Kevin Wolf18edf282015-04-07 17:12:56 +02001306 ret = -ENOTSUP;
1307 goto fail_opts;
Fam Zhengb64ec4e2013-05-29 19:35:40 +08001308 }
Kevin Wolf57915332010-04-14 15:24:50 +02001309
Paolo Bonzinid3faa132017-06-05 14:38:50 +02001310 /* bdrv_new() and bdrv_close() make it so */
1311 assert(atomic_read(&bs->copy_on_read) == 0);
1312
Kevin Wolf82dc8b42016-01-11 19:07:50 +01001313 if (bs->open_flags & BDRV_O_COPY_ON_READ) {
Kevin Wolf0ebd24e2013-09-19 15:12:18 +02001314 if (!bs->read_only) {
1315 bdrv_enable_copy_on_read(bs);
1316 } else {
1317 error_setg(errp, "Can't use copy-on-read on read-only device");
Kevin Wolf18edf282015-04-07 17:12:56 +02001318 ret = -EINVAL;
1319 goto fail_opts;
Kevin Wolf0ebd24e2013-09-19 15:12:18 +02001320 }
Stefan Hajnoczi53fec9d2011-11-28 16:08:47 +00001321 }
1322
Kevin Wolf818584a2016-09-12 18:03:18 +02001323 discard = qemu_opt_get(opts, "discard");
1324 if (discard != NULL) {
1325 if (bdrv_parse_discard_flags(discard, &bs->open_flags) != 0) {
1326 error_setg(errp, "Invalid discard option");
1327 ret = -EINVAL;
1328 goto fail_opts;
1329 }
1330 }
1331
Kevin Wolf692e01a2016-09-12 21:00:41 +02001332 detect_zeroes = qemu_opt_get(opts, "detect-zeroes");
1333 if (detect_zeroes) {
1334 BlockdevDetectZeroesOptions value =
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +02001335 qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup,
Kevin Wolf692e01a2016-09-12 21:00:41 +02001336 detect_zeroes,
Kevin Wolf692e01a2016-09-12 21:00:41 +02001337 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
1338 &local_err);
1339 if (local_err) {
1340 error_propagate(errp, local_err);
1341 ret = -EINVAL;
1342 goto fail_opts;
1343 }
1344
1345 if (value == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
1346 !(bs->open_flags & BDRV_O_UNMAP))
1347 {
1348 error_setg(errp, "setting detect-zeroes to unmap is not allowed "
1349 "without setting discard operation to unmap");
1350 ret = -EINVAL;
1351 goto fail_opts;
1352 }
1353
1354 bs->detect_zeroes = value;
1355 }
1356
Kevin Wolfc2ad1b02013-03-18 16:40:51 +01001357 if (filename != NULL) {
1358 pstrcpy(bs->filename, sizeof(bs->filename), filename);
1359 } else {
1360 bs->filename[0] = '\0';
1361 }
Max Reitz91af7012014-07-18 20:24:56 +02001362 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), bs->filename);
Kevin Wolf57915332010-04-14 15:24:50 +02001363
Kevin Wolf66f82ce2010-04-14 14:17:38 +02001364 /* Open the image, either directly or using a protocol */
Kevin Wolf82dc8b42016-01-11 19:07:50 +01001365 open_flags = bdrv_open_flags(bs, bs->open_flags);
Kevin Wolf01a56502017-01-18 15:51:56 +01001366 node_name = qemu_opt_get(opts, "node-name");
Kevin Wolf66f82ce2010-04-14 14:17:38 +02001367
Kevin Wolf01a56502017-01-18 15:51:56 +01001368 assert(!drv->bdrv_file_open || file == NULL);
1369 ret = bdrv_open_driver(bs, drv, node_name, options, open_flags, errp);
Kevin Wolf57915332010-04-14 15:24:50 +02001370 if (ret < 0) {
Kevin Wolf01a56502017-01-18 15:51:56 +01001371 goto fail_opts;
Kevin Wolf57915332010-04-14 15:24:50 +02001372 }
1373
Kevin Wolf18edf282015-04-07 17:12:56 +02001374 qemu_opts_del(opts);
Kevin Wolf57915332010-04-14 15:24:50 +02001375 return 0;
1376
Kevin Wolf18edf282015-04-07 17:12:56 +02001377fail_opts:
1378 qemu_opts_del(opts);
Kevin Wolf57915332010-04-14 15:24:50 +02001379 return ret;
1380}
1381
Kevin Wolf5e5c4f62014-05-26 11:45:08 +02001382static QDict *parse_json_filename(const char *filename, Error **errp)
1383{
1384 QObject *options_obj;
1385 QDict *options;
1386 int ret;
1387
1388 ret = strstart(filename, "json:", &filename);
1389 assert(ret);
1390
Markus Armbruster5577fff2017-02-28 22:26:59 +01001391 options_obj = qobject_from_json(filename, errp);
Kevin Wolf5e5c4f62014-05-26 11:45:08 +02001392 if (!options_obj) {
Markus Armbruster5577fff2017-02-28 22:26:59 +01001393 /* Work around qobject_from_json() lossage TODO fix that */
1394 if (errp && !*errp) {
1395 error_setg(errp, "Could not parse the JSON options");
1396 return NULL;
1397 }
1398 error_prepend(errp, "Could not parse the JSON options: ");
Kevin Wolf5e5c4f62014-05-26 11:45:08 +02001399 return NULL;
1400 }
1401
Markus Armbrusterca6b6e12017-02-17 21:38:18 +01001402 options = qobject_to_qdict(options_obj);
1403 if (!options) {
Kevin Wolf5e5c4f62014-05-26 11:45:08 +02001404 qobject_decref(options_obj);
1405 error_setg(errp, "Invalid JSON object given");
1406 return NULL;
1407 }
1408
Kevin Wolf5e5c4f62014-05-26 11:45:08 +02001409 qdict_flatten(options);
1410
1411 return options;
1412}
1413
Kevin Wolfde3b53f2015-10-29 15:24:41 +01001414static void parse_json_protocol(QDict *options, const char **pfilename,
1415 Error **errp)
1416{
1417 QDict *json_options;
1418 Error *local_err = NULL;
1419
1420 /* Parse json: pseudo-protocol */
1421 if (!*pfilename || !g_str_has_prefix(*pfilename, "json:")) {
1422 return;
1423 }
1424
1425 json_options = parse_json_filename(*pfilename, &local_err);
1426 if (local_err) {
1427 error_propagate(errp, local_err);
1428 return;
1429 }
1430
1431 /* Options given in the filename have lower priority than options
1432 * specified directly */
1433 qdict_join(options, json_options, false);
1434 QDECREF(json_options);
1435 *pfilename = NULL;
1436}
1437
Kevin Wolf57915332010-04-14 15:24:50 +02001438/*
Kevin Wolff54120f2014-05-26 11:09:59 +02001439 * Fills in default options for opening images and converts the legacy
1440 * filename/flags pair to option QDict entries.
Max Reitz53a29512015-03-19 14:53:16 -04001441 * The BDRV_O_PROTOCOL flag in *flags will be set or cleared accordingly if a
1442 * block driver has been specified explicitly.
Kevin Wolff54120f2014-05-26 11:09:59 +02001443 */
Kevin Wolfde3b53f2015-10-29 15:24:41 +01001444static int bdrv_fill_options(QDict **options, const char *filename,
Max Reitz053e1572015-08-26 19:47:51 +02001445 int *flags, Error **errp)
Kevin Wolff54120f2014-05-26 11:09:59 +02001446{
1447 const char *drvname;
Max Reitz53a29512015-03-19 14:53:16 -04001448 bool protocol = *flags & BDRV_O_PROTOCOL;
Kevin Wolff54120f2014-05-26 11:09:59 +02001449 bool parse_filename = false;
Max Reitz053e1572015-08-26 19:47:51 +02001450 BlockDriver *drv = NULL;
Kevin Wolff54120f2014-05-26 11:09:59 +02001451 Error *local_err = NULL;
Kevin Wolff54120f2014-05-26 11:09:59 +02001452
Markus Armbruster129c7d12017-03-30 19:43:12 +02001453 /*
1454 * Caution: while qdict_get_try_str() is fine, getting non-string
1455 * types would require more care. When @options come from
1456 * -blockdev or blockdev_add, its members are typed according to
1457 * the QAPI schema, but when they come from -drive, they're all
1458 * QString.
1459 */
Max Reitz53a29512015-03-19 14:53:16 -04001460 drvname = qdict_get_try_str(*options, "driver");
Max Reitz053e1572015-08-26 19:47:51 +02001461 if (drvname) {
1462 drv = bdrv_find_format(drvname);
1463 if (!drv) {
1464 error_setg(errp, "Unknown driver '%s'", drvname);
1465 return -ENOENT;
1466 }
1467 /* If the user has explicitly specified the driver, this choice should
1468 * override the BDRV_O_PROTOCOL flag */
1469 protocol = drv->bdrv_file_open;
Max Reitz53a29512015-03-19 14:53:16 -04001470 }
1471
1472 if (protocol) {
1473 *flags |= BDRV_O_PROTOCOL;
1474 } else {
1475 *flags &= ~BDRV_O_PROTOCOL;
1476 }
1477
Kevin Wolf91a097e2015-05-08 17:49:53 +02001478 /* Translate cache options from flags into options */
1479 update_options_from_flags(*options, *flags);
1480
Kevin Wolff54120f2014-05-26 11:09:59 +02001481 /* Fetch the file name from the options QDict if necessary */
Kevin Wolf17b005f2014-05-27 10:50:29 +02001482 if (protocol && filename) {
Kevin Wolff54120f2014-05-26 11:09:59 +02001483 if (!qdict_haskey(*options, "filename")) {
Eric Blake46f5ac22017-04-27 16:58:17 -05001484 qdict_put_str(*options, "filename", filename);
Kevin Wolff54120f2014-05-26 11:09:59 +02001485 parse_filename = true;
1486 } else {
1487 error_setg(errp, "Can't specify 'file' and 'filename' options at "
1488 "the same time");
1489 return -EINVAL;
1490 }
1491 }
1492
1493 /* Find the right block driver */
Markus Armbruster129c7d12017-03-30 19:43:12 +02001494 /* See cautionary note on accessing @options above */
Kevin Wolff54120f2014-05-26 11:09:59 +02001495 filename = qdict_get_try_str(*options, "filename");
Kevin Wolff54120f2014-05-26 11:09:59 +02001496
Max Reitz053e1572015-08-26 19:47:51 +02001497 if (!drvname && protocol) {
1498 if (filename) {
1499 drv = bdrv_find_protocol(filename, parse_filename, errp);
1500 if (!drv) {
Kevin Wolff54120f2014-05-26 11:09:59 +02001501 return -EINVAL;
1502 }
Max Reitz053e1572015-08-26 19:47:51 +02001503
1504 drvname = drv->format_name;
Eric Blake46f5ac22017-04-27 16:58:17 -05001505 qdict_put_str(*options, "driver", drvname);
Max Reitz053e1572015-08-26 19:47:51 +02001506 } else {
1507 error_setg(errp, "Must specify either driver or file");
1508 return -EINVAL;
Kevin Wolff54120f2014-05-26 11:09:59 +02001509 }
1510 }
1511
Kevin Wolf17b005f2014-05-27 10:50:29 +02001512 assert(drv || !protocol);
Kevin Wolff54120f2014-05-26 11:09:59 +02001513
1514 /* Driver-specific filename parsing */
Kevin Wolf17b005f2014-05-27 10:50:29 +02001515 if (drv && drv->bdrv_parse_filename && parse_filename) {
Kevin Wolff54120f2014-05-26 11:09:59 +02001516 drv->bdrv_parse_filename(filename, *options, &local_err);
1517 if (local_err) {
1518 error_propagate(errp, local_err);
1519 return -EINVAL;
1520 }
1521
1522 if (!drv->bdrv_needs_filename) {
1523 qdict_del(*options, "filename");
1524 }
1525 }
1526
1527 return 0;
1528}
1529
Fam Zhengc1cef672017-03-14 10:30:50 +08001530static int bdrv_child_check_perm(BdrvChild *c, uint64_t perm, uint64_t shared,
1531 GSList *ignore_children, Error **errp);
1532static void bdrv_child_abort_perm_update(BdrvChild *c);
1533static void bdrv_child_set_perm(BdrvChild *c, uint64_t perm, uint64_t shared);
1534
Fam Zhengffd1a5a2017-05-03 00:35:38 +08001535static void bdrv_child_perm(BlockDriverState *bs, BlockDriverState *child_bs,
1536 BdrvChild *c,
1537 const BdrvChildRole *role,
1538 uint64_t parent_perm, uint64_t parent_shared,
1539 uint64_t *nperm, uint64_t *nshared)
1540{
1541 if (bs->drv && bs->drv->bdrv_child_perm) {
1542 bs->drv->bdrv_child_perm(bs, c, role,
1543 parent_perm, parent_shared,
1544 nperm, nshared);
1545 }
1546 if (child_bs && child_bs->force_share) {
1547 *nshared = BLK_PERM_ALL;
1548 }
1549}
1550
Kevin Wolf33a610c2016-12-15 13:04:20 +01001551/*
1552 * Check whether permissions on this node can be changed in a way that
1553 * @cumulative_perms and @cumulative_shared_perms are the new cumulative
1554 * permissions of all its parents. This involves checking whether all necessary
1555 * permission changes to child nodes can be performed.
1556 *
1557 * A call to this function must always be followed by a call to bdrv_set_perm()
1558 * or bdrv_abort_perm_update().
1559 */
1560static int bdrv_check_perm(BlockDriverState *bs, uint64_t cumulative_perms,
Kevin Wolf46181122017-03-06 15:00:13 +01001561 uint64_t cumulative_shared_perms,
1562 GSList *ignore_children, Error **errp)
Kevin Wolf33a610c2016-12-15 13:04:20 +01001563{
1564 BlockDriver *drv = bs->drv;
1565 BdrvChild *c;
1566 int ret;
1567
1568 /* Write permissions never work with read-only images */
1569 if ((cumulative_perms & (BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED)) &&
Kevin Wolf9c5e6592017-05-04 18:52:40 +02001570 !bdrv_is_writable(bs))
Kevin Wolf33a610c2016-12-15 13:04:20 +01001571 {
1572 error_setg(errp, "Block node is read-only");
1573 return -EPERM;
1574 }
1575
1576 /* Check this node */
1577 if (!drv) {
1578 return 0;
1579 }
1580
1581 if (drv->bdrv_check_perm) {
1582 return drv->bdrv_check_perm(bs, cumulative_perms,
1583 cumulative_shared_perms, errp);
1584 }
1585
Kevin Wolf78e421c2016-12-20 23:25:12 +01001586 /* Drivers that never have children can omit .bdrv_child_perm() */
Kevin Wolf33a610c2016-12-15 13:04:20 +01001587 if (!drv->bdrv_child_perm) {
Kevin Wolf78e421c2016-12-20 23:25:12 +01001588 assert(QLIST_EMPTY(&bs->children));
Kevin Wolf33a610c2016-12-15 13:04:20 +01001589 return 0;
1590 }
1591
1592 /* Check all children */
1593 QLIST_FOREACH(c, &bs->children, next) {
1594 uint64_t cur_perm, cur_shared;
Fam Zhengffd1a5a2017-05-03 00:35:38 +08001595 bdrv_child_perm(bs, c->bs, c, c->role,
1596 cumulative_perms, cumulative_shared_perms,
1597 &cur_perm, &cur_shared);
Kevin Wolf46181122017-03-06 15:00:13 +01001598 ret = bdrv_child_check_perm(c, cur_perm, cur_shared, ignore_children,
1599 errp);
Kevin Wolf33a610c2016-12-15 13:04:20 +01001600 if (ret < 0) {
1601 return ret;
1602 }
1603 }
1604
1605 return 0;
1606}
1607
1608/*
1609 * Notifies drivers that after a previous bdrv_check_perm() call, the
1610 * permission update is not performed and any preparations made for it (e.g.
1611 * taken file locks) need to be undone.
1612 *
1613 * This function recursively notifies all child nodes.
1614 */
1615static void bdrv_abort_perm_update(BlockDriverState *bs)
1616{
1617 BlockDriver *drv = bs->drv;
1618 BdrvChild *c;
1619
1620 if (!drv) {
1621 return;
1622 }
1623
1624 if (drv->bdrv_abort_perm_update) {
1625 drv->bdrv_abort_perm_update(bs);
1626 }
1627
1628 QLIST_FOREACH(c, &bs->children, next) {
1629 bdrv_child_abort_perm_update(c);
1630 }
1631}
1632
1633static void bdrv_set_perm(BlockDriverState *bs, uint64_t cumulative_perms,
1634 uint64_t cumulative_shared_perms)
1635{
1636 BlockDriver *drv = bs->drv;
1637 BdrvChild *c;
1638
1639 if (!drv) {
1640 return;
1641 }
1642
1643 /* Update this node */
1644 if (drv->bdrv_set_perm) {
1645 drv->bdrv_set_perm(bs, cumulative_perms, cumulative_shared_perms);
1646 }
1647
Kevin Wolf78e421c2016-12-20 23:25:12 +01001648 /* Drivers that never have children can omit .bdrv_child_perm() */
Kevin Wolf33a610c2016-12-15 13:04:20 +01001649 if (!drv->bdrv_child_perm) {
Kevin Wolf78e421c2016-12-20 23:25:12 +01001650 assert(QLIST_EMPTY(&bs->children));
Kevin Wolf33a610c2016-12-15 13:04:20 +01001651 return;
1652 }
1653
1654 /* Update all children */
1655 QLIST_FOREACH(c, &bs->children, next) {
1656 uint64_t cur_perm, cur_shared;
Fam Zhengffd1a5a2017-05-03 00:35:38 +08001657 bdrv_child_perm(bs, c->bs, c, c->role,
1658 cumulative_perms, cumulative_shared_perms,
1659 &cur_perm, &cur_shared);
Kevin Wolf33a610c2016-12-15 13:04:20 +01001660 bdrv_child_set_perm(c, cur_perm, cur_shared);
1661 }
1662}
1663
1664static void bdrv_get_cumulative_perm(BlockDriverState *bs, uint64_t *perm,
1665 uint64_t *shared_perm)
1666{
1667 BdrvChild *c;
1668 uint64_t cumulative_perms = 0;
1669 uint64_t cumulative_shared_perms = BLK_PERM_ALL;
1670
1671 QLIST_FOREACH(c, &bs->parents, next_parent) {
1672 cumulative_perms |= c->perm;
1673 cumulative_shared_perms &= c->shared_perm;
1674 }
1675
1676 *perm = cumulative_perms;
1677 *shared_perm = cumulative_shared_perms;
1678}
1679
Kevin Wolfd0833192017-01-16 18:26:20 +01001680static char *bdrv_child_user_desc(BdrvChild *c)
1681{
1682 if (c->role->get_parent_desc) {
1683 return c->role->get_parent_desc(c);
1684 }
1685
1686 return g_strdup("another user");
1687}
1688
Fam Zheng51761962017-05-03 00:35:36 +08001689char *bdrv_perm_names(uint64_t perm)
Kevin Wolfd0833192017-01-16 18:26:20 +01001690{
1691 struct perm_name {
1692 uint64_t perm;
1693 const char *name;
1694 } permissions[] = {
1695 { BLK_PERM_CONSISTENT_READ, "consistent read" },
1696 { BLK_PERM_WRITE, "write" },
1697 { BLK_PERM_WRITE_UNCHANGED, "write unchanged" },
1698 { BLK_PERM_RESIZE, "resize" },
1699 { BLK_PERM_GRAPH_MOD, "change children" },
1700 { 0, NULL }
1701 };
1702
1703 char *result = g_strdup("");
1704 struct perm_name *p;
1705
1706 for (p = permissions; p->name; p++) {
1707 if (perm & p->perm) {
1708 char *old = result;
1709 result = g_strdup_printf("%s%s%s", old, *old ? ", " : "", p->name);
1710 g_free(old);
1711 }
1712 }
1713
1714 return result;
1715}
1716
Kevin Wolf33a610c2016-12-15 13:04:20 +01001717/*
1718 * Checks whether a new reference to @bs can be added if the new user requires
Kevin Wolf46181122017-03-06 15:00:13 +01001719 * @new_used_perm/@new_shared_perm as its permissions. If @ignore_children is
1720 * set, the BdrvChild objects in this list are ignored in the calculations;
1721 * this allows checking permission updates for an existing reference.
Kevin Wolf33a610c2016-12-15 13:04:20 +01001722 *
1723 * Needs to be followed by a call to either bdrv_set_perm() or
1724 * bdrv_abort_perm_update(). */
Kevin Wolfd5e6f432016-12-14 17:24:36 +01001725static int bdrv_check_update_perm(BlockDriverState *bs, uint64_t new_used_perm,
1726 uint64_t new_shared_perm,
Kevin Wolf46181122017-03-06 15:00:13 +01001727 GSList *ignore_children, Error **errp)
Kevin Wolfd5e6f432016-12-14 17:24:36 +01001728{
1729 BdrvChild *c;
Kevin Wolf33a610c2016-12-15 13:04:20 +01001730 uint64_t cumulative_perms = new_used_perm;
1731 uint64_t cumulative_shared_perms = new_shared_perm;
Kevin Wolfd5e6f432016-12-14 17:24:36 +01001732
1733 /* There is no reason why anyone couldn't tolerate write_unchanged */
1734 assert(new_shared_perm & BLK_PERM_WRITE_UNCHANGED);
1735
1736 QLIST_FOREACH(c, &bs->parents, next_parent) {
Kevin Wolf46181122017-03-06 15:00:13 +01001737 if (g_slist_find(ignore_children, c)) {
Kevin Wolfd5e6f432016-12-14 17:24:36 +01001738 continue;
1739 }
1740
Kevin Wolfd0833192017-01-16 18:26:20 +01001741 if ((new_used_perm & c->shared_perm) != new_used_perm) {
1742 char *user = bdrv_child_user_desc(c);
1743 char *perm_names = bdrv_perm_names(new_used_perm & ~c->shared_perm);
1744 error_setg(errp, "Conflicts with use by %s as '%s', which does not "
1745 "allow '%s' on %s",
1746 user, c->name, perm_names, bdrv_get_node_name(c->bs));
1747 g_free(user);
1748 g_free(perm_names);
1749 return -EPERM;
1750 }
1751
1752 if ((c->perm & new_shared_perm) != c->perm) {
1753 char *user = bdrv_child_user_desc(c);
1754 char *perm_names = bdrv_perm_names(c->perm & ~new_shared_perm);
1755 error_setg(errp, "Conflicts with use by %s as '%s', which uses "
1756 "'%s' on %s",
1757 user, c->name, perm_names, bdrv_get_node_name(c->bs));
1758 g_free(user);
1759 g_free(perm_names);
Kevin Wolfd5e6f432016-12-14 17:24:36 +01001760 return -EPERM;
1761 }
Kevin Wolf33a610c2016-12-15 13:04:20 +01001762
1763 cumulative_perms |= c->perm;
1764 cumulative_shared_perms &= c->shared_perm;
Kevin Wolfd5e6f432016-12-14 17:24:36 +01001765 }
1766
Kevin Wolf46181122017-03-06 15:00:13 +01001767 return bdrv_check_perm(bs, cumulative_perms, cumulative_shared_perms,
1768 ignore_children, errp);
Kevin Wolf33a610c2016-12-15 13:04:20 +01001769}
1770
1771/* Needs to be followed by a call to either bdrv_child_set_perm() or
1772 * bdrv_child_abort_perm_update(). */
Fam Zhengc1cef672017-03-14 10:30:50 +08001773static int bdrv_child_check_perm(BdrvChild *c, uint64_t perm, uint64_t shared,
1774 GSList *ignore_children, Error **errp)
Kevin Wolf33a610c2016-12-15 13:04:20 +01001775{
Kevin Wolf46181122017-03-06 15:00:13 +01001776 int ret;
1777
1778 ignore_children = g_slist_prepend(g_slist_copy(ignore_children), c);
1779 ret = bdrv_check_update_perm(c->bs, perm, shared, ignore_children, errp);
1780 g_slist_free(ignore_children);
1781
1782 return ret;
Kevin Wolf33a610c2016-12-15 13:04:20 +01001783}
1784
Fam Zhengc1cef672017-03-14 10:30:50 +08001785static void bdrv_child_set_perm(BdrvChild *c, uint64_t perm, uint64_t shared)
Kevin Wolf33a610c2016-12-15 13:04:20 +01001786{
1787 uint64_t cumulative_perms, cumulative_shared_perms;
1788
1789 c->perm = perm;
1790 c->shared_perm = shared;
1791
1792 bdrv_get_cumulative_perm(c->bs, &cumulative_perms,
1793 &cumulative_shared_perms);
1794 bdrv_set_perm(c->bs, cumulative_perms, cumulative_shared_perms);
1795}
1796
Fam Zhengc1cef672017-03-14 10:30:50 +08001797static void bdrv_child_abort_perm_update(BdrvChild *c)
Kevin Wolf33a610c2016-12-15 13:04:20 +01001798{
1799 bdrv_abort_perm_update(c->bs);
1800}
1801
1802int bdrv_child_try_set_perm(BdrvChild *c, uint64_t perm, uint64_t shared,
1803 Error **errp)
1804{
1805 int ret;
1806
Kevin Wolf46181122017-03-06 15:00:13 +01001807 ret = bdrv_child_check_perm(c, perm, shared, NULL, errp);
Kevin Wolf33a610c2016-12-15 13:04:20 +01001808 if (ret < 0) {
1809 bdrv_child_abort_perm_update(c);
1810 return ret;
1811 }
1812
1813 bdrv_child_set_perm(c, perm, shared);
1814
Kevin Wolfd5e6f432016-12-14 17:24:36 +01001815 return 0;
1816}
1817
Kevin Wolf6a1b9ee2016-12-15 11:27:32 +01001818#define DEFAULT_PERM_PASSTHROUGH (BLK_PERM_CONSISTENT_READ \
1819 | BLK_PERM_WRITE \
1820 | BLK_PERM_WRITE_UNCHANGED \
1821 | BLK_PERM_RESIZE)
1822#define DEFAULT_PERM_UNCHANGED (BLK_PERM_ALL & ~DEFAULT_PERM_PASSTHROUGH)
1823
1824void bdrv_filter_default_perms(BlockDriverState *bs, BdrvChild *c,
1825 const BdrvChildRole *role,
1826 uint64_t perm, uint64_t shared,
1827 uint64_t *nperm, uint64_t *nshared)
1828{
1829 if (c == NULL) {
1830 *nperm = perm & DEFAULT_PERM_PASSTHROUGH;
1831 *nshared = (shared & DEFAULT_PERM_PASSTHROUGH) | DEFAULT_PERM_UNCHANGED;
1832 return;
1833 }
1834
1835 *nperm = (perm & DEFAULT_PERM_PASSTHROUGH) |
1836 (c->perm & DEFAULT_PERM_UNCHANGED);
1837 *nshared = (shared & DEFAULT_PERM_PASSTHROUGH) |
1838 (c->shared_perm & DEFAULT_PERM_UNCHANGED);
1839}
1840
Kevin Wolf6b1a0442016-12-19 15:21:48 +01001841void bdrv_format_default_perms(BlockDriverState *bs, BdrvChild *c,
1842 const BdrvChildRole *role,
1843 uint64_t perm, uint64_t shared,
1844 uint64_t *nperm, uint64_t *nshared)
1845{
1846 bool backing = (role == &child_backing);
1847 assert(role == &child_backing || role == &child_file);
1848
1849 if (!backing) {
1850 /* Apart from the modifications below, the same permissions are
1851 * forwarded and left alone as for filters */
1852 bdrv_filter_default_perms(bs, c, role, perm, shared, &perm, &shared);
1853
1854 /* Format drivers may touch metadata even if the guest doesn't write */
Kevin Wolf9c5e6592017-05-04 18:52:40 +02001855 if (bdrv_is_writable(bs)) {
Kevin Wolf6b1a0442016-12-19 15:21:48 +01001856 perm |= BLK_PERM_WRITE | BLK_PERM_RESIZE;
1857 }
1858
1859 /* bs->file always needs to be consistent because of the metadata. We
1860 * can never allow other users to resize or write to it. */
1861 perm |= BLK_PERM_CONSISTENT_READ;
1862 shared &= ~(BLK_PERM_WRITE | BLK_PERM_RESIZE);
1863 } else {
1864 /* We want consistent read from backing files if the parent needs it.
1865 * No other operations are performed on backing files. */
1866 perm &= BLK_PERM_CONSISTENT_READ;
1867
1868 /* If the parent can deal with changing data, we're okay with a
1869 * writable and resizable backing file. */
1870 /* TODO Require !(perm & BLK_PERM_CONSISTENT_READ), too? */
1871 if (shared & BLK_PERM_WRITE) {
1872 shared = BLK_PERM_WRITE | BLK_PERM_RESIZE;
1873 } else {
1874 shared = 0;
1875 }
1876
1877 shared |= BLK_PERM_CONSISTENT_READ | BLK_PERM_GRAPH_MOD |
1878 BLK_PERM_WRITE_UNCHANGED;
1879 }
1880
Kevin Wolf9c5e6592017-05-04 18:52:40 +02001881 if (bs->open_flags & BDRV_O_INACTIVE) {
1882 shared |= BLK_PERM_WRITE | BLK_PERM_RESIZE;
1883 }
1884
Kevin Wolf6b1a0442016-12-19 15:21:48 +01001885 *nperm = perm;
1886 *nshared = shared;
1887}
1888
Kevin Wolf8ee03992017-03-06 13:45:28 +01001889static void bdrv_replace_child_noperm(BdrvChild *child,
1890 BlockDriverState *new_bs)
Kevin Wolfe9740bc2016-05-23 15:52:26 +02001891{
1892 BlockDriverState *old_bs = child->bs;
1893
Fam Zhengbb2614e2017-04-07 14:54:10 +08001894 if (old_bs && new_bs) {
1895 assert(bdrv_get_aio_context(old_bs) == bdrv_get_aio_context(new_bs));
1896 }
Kevin Wolfe9740bc2016-05-23 15:52:26 +02001897 if (old_bs) {
Kevin Wolf36fe1332016-05-17 14:51:55 +02001898 if (old_bs->quiesce_counter && child->role->drained_end) {
1899 child->role->drained_end(child);
1900 }
Kevin Wolfdb95dbb2017-02-08 11:28:52 +01001901 if (child->role->detach) {
1902 child->role->detach(child);
1903 }
Kevin Wolfe9740bc2016-05-23 15:52:26 +02001904 QLIST_REMOVE(child, next_parent);
1905 }
Kevin Wolfe9740bc2016-05-23 15:52:26 +02001906
1907 child->bs = new_bs;
Kevin Wolf36fe1332016-05-17 14:51:55 +02001908
1909 if (new_bs) {
1910 QLIST_INSERT_HEAD(&new_bs->parents, child, next_parent);
1911 if (new_bs->quiesce_counter && child->role->drained_begin) {
1912 child->role->drained_begin(child);
1913 }
Kevin Wolf33a610c2016-12-15 13:04:20 +01001914
Kevin Wolfdb95dbb2017-02-08 11:28:52 +01001915 if (child->role->attach) {
1916 child->role->attach(child);
1917 }
Kevin Wolf36fe1332016-05-17 14:51:55 +02001918 }
Kevin Wolfe9740bc2016-05-23 15:52:26 +02001919}
1920
Kevin Wolf466787f2017-03-08 14:44:07 +01001921/*
1922 * Updates @child to change its reference to point to @new_bs, including
1923 * checking and applying the necessary permisson updates both to the old node
1924 * and to @new_bs.
1925 *
1926 * NULL is passed as @new_bs for removing the reference before freeing @child.
1927 *
1928 * If @new_bs is not NULL, bdrv_check_perm() must be called beforehand, as this
1929 * function uses bdrv_set_perm() to update the permissions according to the new
1930 * reference that @new_bs gets.
1931 */
1932static void bdrv_replace_child(BdrvChild *child, BlockDriverState *new_bs)
Kevin Wolf8ee03992017-03-06 13:45:28 +01001933{
1934 BlockDriverState *old_bs = child->bs;
1935 uint64_t perm, shared_perm;
1936
Kevin Wolf8aecf1d2017-08-03 17:02:57 +02001937 bdrv_replace_child_noperm(child, new_bs);
1938
Kevin Wolf8ee03992017-03-06 13:45:28 +01001939 if (old_bs) {
1940 /* Update permissions for old node. This is guaranteed to succeed
1941 * because we're just taking a parent away, so we're loosening
1942 * restrictions. */
1943 bdrv_get_cumulative_perm(old_bs, &perm, &shared_perm);
Kevin Wolf46181122017-03-06 15:00:13 +01001944 bdrv_check_perm(old_bs, perm, shared_perm, NULL, &error_abort);
Kevin Wolf8ee03992017-03-06 13:45:28 +01001945 bdrv_set_perm(old_bs, perm, shared_perm);
1946 }
1947
Kevin Wolf8ee03992017-03-06 13:45:28 +01001948 if (new_bs) {
Kevin Wolff54120f2014-05-26 11:09:59 +02001949 bdrv_get_cumulative_perm(new_bs, &perm, &shared_perm);
Kevin Wolff54120f2014-05-26 11:09:59 +02001950 bdrv_set_perm(new_bs, perm, shared_perm);
Kevin Wolff54120f2014-05-26 11:09:59 +02001951 }
1952}
1953
Kevin Wolff21d96d2016-03-08 13:47:46 +01001954BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs,
1955 const char *child_name,
Kevin Wolf36fe1332016-05-17 14:51:55 +02001956 const BdrvChildRole *child_role,
Kevin Wolfd5e6f432016-12-14 17:24:36 +01001957 uint64_t perm, uint64_t shared_perm,
1958 void *opaque, Error **errp)
Kevin Wolfdf581792015-06-15 11:53:47 +02001959{
Kevin Wolfd5e6f432016-12-14 17:24:36 +01001960 BdrvChild *child;
1961 int ret;
1962
1963 ret = bdrv_check_update_perm(child_bs, perm, shared_perm, NULL, errp);
1964 if (ret < 0) {
Kevin Wolf33a610c2016-12-15 13:04:20 +01001965 bdrv_abort_perm_update(child_bs);
Kevin Wolfd5e6f432016-12-14 17:24:36 +01001966 return NULL;
1967 }
1968
1969 child = g_new(BdrvChild, 1);
Kevin Wolfdf581792015-06-15 11:53:47 +02001970 *child = (BdrvChild) {
Kevin Wolfd5e6f432016-12-14 17:24:36 +01001971 .bs = NULL,
1972 .name = g_strdup(child_name),
1973 .role = child_role,
1974 .perm = perm,
1975 .shared_perm = shared_perm,
1976 .opaque = opaque,
Kevin Wolfdf581792015-06-15 11:53:47 +02001977 };
1978
Kevin Wolf33a610c2016-12-15 13:04:20 +01001979 /* This performs the matching bdrv_set_perm() for the above check. */
Kevin Wolf466787f2017-03-08 14:44:07 +01001980 bdrv_replace_child(child, child_bs);
Kevin Wolfb4b059f2015-06-15 13:24:19 +02001981
1982 return child;
Kevin Wolfdf581792015-06-15 11:53:47 +02001983}
1984
Wen Congyang98292c62016-05-10 15:36:38 +08001985BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs,
1986 BlockDriverState *child_bs,
1987 const char *child_name,
Kevin Wolf8b2ff522016-12-20 22:21:17 +01001988 const BdrvChildRole *child_role,
1989 Error **errp)
Kevin Wolff21d96d2016-03-08 13:47:46 +01001990{
Kevin Wolfd5e6f432016-12-14 17:24:36 +01001991 BdrvChild *child;
Kevin Wolff68c5982016-12-20 15:51:12 +01001992 uint64_t perm, shared_perm;
Kevin Wolfd5e6f432016-12-14 17:24:36 +01001993
Kevin Wolff68c5982016-12-20 15:51:12 +01001994 bdrv_get_cumulative_perm(parent_bs, &perm, &shared_perm);
1995
1996 assert(parent_bs->drv);
Fam Zhengbb2614e2017-04-07 14:54:10 +08001997 assert(bdrv_get_aio_context(parent_bs) == bdrv_get_aio_context(child_bs));
Fam Zhengffd1a5a2017-05-03 00:35:38 +08001998 bdrv_child_perm(parent_bs, child_bs, NULL, child_role,
1999 perm, shared_perm, &perm, &shared_perm);
Kevin Wolff68c5982016-12-20 15:51:12 +01002000
Kevin Wolfd5e6f432016-12-14 17:24:36 +01002001 child = bdrv_root_attach_child(child_bs, child_name, child_role,
Kevin Wolff68c5982016-12-20 15:51:12 +01002002 perm, shared_perm, parent_bs, errp);
Kevin Wolfd5e6f432016-12-14 17:24:36 +01002003 if (child == NULL) {
2004 return NULL;
2005 }
2006
Kevin Wolff21d96d2016-03-08 13:47:46 +01002007 QLIST_INSERT_HEAD(&parent_bs->children, child, next);
2008 return child;
2009}
2010
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02002011static void bdrv_detach_child(BdrvChild *child)
Kevin Wolf33a60402015-06-15 13:51:04 +02002012{
Kevin Wolff21d96d2016-03-08 13:47:46 +01002013 if (child->next.le_prev) {
2014 QLIST_REMOVE(child, next);
2015 child->next.le_prev = NULL;
2016 }
Kevin Wolfe9740bc2016-05-23 15:52:26 +02002017
Kevin Wolf466787f2017-03-08 14:44:07 +01002018 bdrv_replace_child(child, NULL);
Kevin Wolfe9740bc2016-05-23 15:52:26 +02002019
Kevin Wolf260fecf2015-04-27 13:46:22 +02002020 g_free(child->name);
Kevin Wolf33a60402015-06-15 13:51:04 +02002021 g_free(child);
2022}
2023
Kevin Wolff21d96d2016-03-08 13:47:46 +01002024void bdrv_root_unref_child(BdrvChild *child)
Kevin Wolf33a60402015-06-15 13:51:04 +02002025{
Kevin Wolf779020c2015-10-13 14:09:44 +02002026 BlockDriverState *child_bs;
2027
Kevin Wolff21d96d2016-03-08 13:47:46 +01002028 child_bs = child->bs;
2029 bdrv_detach_child(child);
2030 bdrv_unref(child_bs);
2031}
2032
2033void bdrv_unref_child(BlockDriverState *parent, BdrvChild *child)
2034{
Kevin Wolf779020c2015-10-13 14:09:44 +02002035 if (child == NULL) {
2036 return;
2037 }
Kevin Wolf33a60402015-06-15 13:51:04 +02002038
2039 if (child->bs->inherits_from == parent) {
Kevin Wolf4e4bf5c2016-12-16 18:52:37 +01002040 BdrvChild *c;
2041
2042 /* Remove inherits_from only when the last reference between parent and
2043 * child->bs goes away. */
2044 QLIST_FOREACH(c, &parent->children, next) {
2045 if (c != child && c->bs == child->bs) {
2046 break;
2047 }
2048 }
2049 if (c == NULL) {
2050 child->bs->inherits_from = NULL;
2051 }
Kevin Wolf33a60402015-06-15 13:51:04 +02002052 }
2053
Kevin Wolff21d96d2016-03-08 13:47:46 +01002054 bdrv_root_unref_child(child);
Kevin Wolf33a60402015-06-15 13:51:04 +02002055}
2056
Kevin Wolf5c8cab42016-02-24 15:13:35 +01002057
2058static void bdrv_parent_cb_change_media(BlockDriverState *bs, bool load)
2059{
2060 BdrvChild *c;
2061 QLIST_FOREACH(c, &bs->parents, next_parent) {
2062 if (c->role->change_media) {
2063 c->role->change_media(c, load);
2064 }
2065 }
2066}
2067
2068static void bdrv_parent_cb_resize(BlockDriverState *bs)
2069{
2070 BdrvChild *c;
2071 QLIST_FOREACH(c, &bs->parents, next_parent) {
2072 if (c->role->resize) {
2073 c->role->resize(c);
2074 }
2075 }
2076}
2077
Kevin Wolf5db15a52015-09-14 15:33:33 +02002078/*
2079 * Sets the backing file link of a BDS. A new reference is created; callers
2080 * which don't need their own reference any more must call bdrv_unref().
2081 */
Kevin Wolf12fa4af2017-02-17 20:42:32 +01002082void bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd,
2083 Error **errp)
Fam Zheng8d24cce2014-05-23 21:29:45 +08002084{
Kevin Wolf5db15a52015-09-14 15:33:33 +02002085 if (backing_hd) {
2086 bdrv_ref(backing_hd);
2087 }
Fam Zheng8d24cce2014-05-23 21:29:45 +08002088
Kevin Wolf760e0062015-06-17 14:55:21 +02002089 if (bs->backing) {
Kevin Wolf5db15a52015-09-14 15:33:33 +02002090 bdrv_unref_child(bs, bs->backing);
Fam Zheng826b6ca2014-05-23 21:29:47 +08002091 }
2092
Fam Zheng8d24cce2014-05-23 21:29:45 +08002093 if (!backing_hd) {
Kevin Wolf760e0062015-06-17 14:55:21 +02002094 bs->backing = NULL;
Fam Zheng8d24cce2014-05-23 21:29:45 +08002095 goto out;
2096 }
Kevin Wolf12fa4af2017-02-17 20:42:32 +01002097
Kevin Wolf8b2ff522016-12-20 22:21:17 +01002098 bs->backing = bdrv_attach_child(bs, backing_hd, "backing", &child_backing,
Kevin Wolf12fa4af2017-02-17 20:42:32 +01002099 errp);
2100 if (!bs->backing) {
2101 bdrv_unref(backing_hd);
2102 }
Fam Zheng826b6ca2014-05-23 21:29:47 +08002103
Kevin Wolf9e7e9402017-03-09 11:45:39 +01002104 bdrv_refresh_filename(bs);
2105
Fam Zheng8d24cce2014-05-23 21:29:45 +08002106out:
Kevin Wolf3baca892014-07-16 17:48:16 +02002107 bdrv_refresh_limits(bs, NULL);
Fam Zheng8d24cce2014-05-23 21:29:45 +08002108}
2109
Kevin Wolf31ca6d02013-03-28 15:29:24 +01002110/*
2111 * Opens the backing file for a BlockDriverState if not yet open
2112 *
Kevin Wolfd9b7b052015-01-16 18:23:41 +01002113 * bdref_key specifies the key for the image's BlockdevRef in the options QDict.
2114 * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict
2115 * itself, all options starting with "${bdref_key}." are considered part of the
2116 * BlockdevRef.
2117 *
2118 * TODO Can this be unified with bdrv_open_image()?
Kevin Wolf31ca6d02013-03-28 15:29:24 +01002119 */
Kevin Wolfd9b7b052015-01-16 18:23:41 +01002120int bdrv_open_backing_file(BlockDriverState *bs, QDict *parent_options,
2121 const char *bdref_key, Error **errp)
Paolo Bonzini9156df12012-10-18 16:49:17 +02002122{
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02002123 char *backing_filename = g_malloc0(PATH_MAX);
Kevin Wolfd9b7b052015-01-16 18:23:41 +01002124 char *bdref_key_dot;
2125 const char *reference = NULL;
Kevin Wolf317fc442014-04-25 13:27:34 +02002126 int ret = 0;
Fam Zheng8d24cce2014-05-23 21:29:45 +08002127 BlockDriverState *backing_hd;
Kevin Wolfd9b7b052015-01-16 18:23:41 +01002128 QDict *options;
2129 QDict *tmp_parent_options = NULL;
Max Reitz34b5d2c2013-09-05 14:45:29 +02002130 Error *local_err = NULL;
Paolo Bonzini9156df12012-10-18 16:49:17 +02002131
Kevin Wolf760e0062015-06-17 14:55:21 +02002132 if (bs->backing != NULL) {
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02002133 goto free_exit;
Paolo Bonzini9156df12012-10-18 16:49:17 +02002134 }
2135
Kevin Wolf31ca6d02013-03-28 15:29:24 +01002136 /* NULL means an empty set of options */
Kevin Wolfd9b7b052015-01-16 18:23:41 +01002137 if (parent_options == NULL) {
2138 tmp_parent_options = qdict_new();
2139 parent_options = tmp_parent_options;
Kevin Wolf31ca6d02013-03-28 15:29:24 +01002140 }
2141
Paolo Bonzini9156df12012-10-18 16:49:17 +02002142 bs->open_flags &= ~BDRV_O_NO_BACKING;
Kevin Wolfd9b7b052015-01-16 18:23:41 +01002143
2144 bdref_key_dot = g_strdup_printf("%s.", bdref_key);
2145 qdict_extract_subqdict(parent_options, &options, bdref_key_dot);
2146 g_free(bdref_key_dot);
2147
Markus Armbruster129c7d12017-03-30 19:43:12 +02002148 /*
2149 * Caution: while qdict_get_try_str() is fine, getting non-string
2150 * types would require more care. When @parent_options come from
2151 * -blockdev or blockdev_add, its members are typed according to
2152 * the QAPI schema, but when they come from -drive, they're all
2153 * QString.
2154 */
Kevin Wolfd9b7b052015-01-16 18:23:41 +01002155 reference = qdict_get_try_str(parent_options, bdref_key);
2156 if (reference || qdict_haskey(options, "file.filename")) {
Kevin Wolf1cb6f502013-04-12 20:27:07 +02002157 backing_filename[0] = '\0';
2158 } else if (bs->backing_file[0] == '\0' && qdict_size(options) == 0) {
Kevin Wolf31ca6d02013-03-28 15:29:24 +01002159 QDECREF(options);
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02002160 goto free_exit;
Fam Zhengdbecebd2013-09-22 20:05:06 +08002161 } else {
Max Reitz9f074292014-11-26 17:20:26 +01002162 bdrv_get_full_backing_filename(bs, backing_filename, PATH_MAX,
2163 &local_err);
2164 if (local_err) {
2165 ret = -EINVAL;
2166 error_propagate(errp, local_err);
2167 QDECREF(options);
2168 goto free_exit;
2169 }
Paolo Bonzini9156df12012-10-18 16:49:17 +02002170 }
2171
Kevin Wolf8ee79e72014-06-04 15:09:35 +02002172 if (!bs->drv || !bs->drv->supports_backing) {
2173 ret = -EINVAL;
2174 error_setg(errp, "Driver doesn't support backing files");
2175 QDECREF(options);
2176 goto free_exit;
2177 }
2178
Kevin Wolfc5f6e492014-11-25 18:12:42 +01002179 if (bs->backing_format[0] != '\0' && !qdict_haskey(options, "driver")) {
Eric Blake46f5ac22017-04-27 16:58:17 -05002180 qdict_put_str(options, "driver", bs->backing_format);
Paolo Bonzini9156df12012-10-18 16:49:17 +02002181 }
2182
Max Reitz5b363932016-05-17 16:41:31 +02002183 backing_hd = bdrv_open_inherit(*backing_filename ? backing_filename : NULL,
2184 reference, options, 0, bs, &child_backing,
2185 errp);
2186 if (!backing_hd) {
Paolo Bonzini9156df12012-10-18 16:49:17 +02002187 bs->open_flags |= BDRV_O_NO_BACKING;
Markus Armbrustere43bfd92015-12-18 16:35:15 +01002188 error_prepend(errp, "Could not open backing file: ");
Max Reitz5b363932016-05-17 16:41:31 +02002189 ret = -EINVAL;
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02002190 goto free_exit;
Paolo Bonzini9156df12012-10-18 16:49:17 +02002191 }
sochin.jiang5ce6bfe2017-06-26 19:04:24 +08002192 bdrv_set_aio_context(backing_hd, bdrv_get_aio_context(bs));
Kevin Wolfdf581792015-06-15 11:53:47 +02002193
Kevin Wolf5db15a52015-09-14 15:33:33 +02002194 /* Hook up the backing file link; drop our reference, bs owns the
2195 * backing_hd reference now */
Kevin Wolf12fa4af2017-02-17 20:42:32 +01002196 bdrv_set_backing_hd(bs, backing_hd, &local_err);
Kevin Wolf5db15a52015-09-14 15:33:33 +02002197 bdrv_unref(backing_hd);
Kevin Wolf12fa4af2017-02-17 20:42:32 +01002198 if (local_err) {
Fam Zheng8cd1a3e2017-03-17 09:56:30 +08002199 error_propagate(errp, local_err);
Kevin Wolf12fa4af2017-02-17 20:42:32 +01002200 ret = -EINVAL;
2201 goto free_exit;
2202 }
Peter Feinerd80ac652014-01-08 19:43:25 +00002203
Kevin Wolfd9b7b052015-01-16 18:23:41 +01002204 qdict_del(parent_options, bdref_key);
2205
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02002206free_exit:
2207 g_free(backing_filename);
Kevin Wolfd9b7b052015-01-16 18:23:41 +01002208 QDECREF(tmp_parent_options);
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02002209 return ret;
Paolo Bonzini9156df12012-10-18 16:49:17 +02002210}
2211
Kevin Wolf2d6b86a2017-02-17 17:43:59 +01002212static BlockDriverState *
2213bdrv_open_child_bs(const char *filename, QDict *options, const char *bdref_key,
2214 BlockDriverState *parent, const BdrvChildRole *child_role,
2215 bool allow_none, Error **errp)
Max Reitzda557aa2013-12-20 19:28:11 +01002216{
Kevin Wolf2d6b86a2017-02-17 17:43:59 +01002217 BlockDriverState *bs = NULL;
Max Reitzda557aa2013-12-20 19:28:11 +01002218 QDict *image_options;
Max Reitzda557aa2013-12-20 19:28:11 +01002219 char *bdref_key_dot;
2220 const char *reference;
2221
Kevin Wolfdf581792015-06-15 11:53:47 +02002222 assert(child_role != NULL);
Max Reitzf67503e2014-02-18 18:33:05 +01002223
Max Reitzda557aa2013-12-20 19:28:11 +01002224 bdref_key_dot = g_strdup_printf("%s.", bdref_key);
2225 qdict_extract_subqdict(options, &image_options, bdref_key_dot);
2226 g_free(bdref_key_dot);
2227
Markus Armbruster129c7d12017-03-30 19:43:12 +02002228 /*
2229 * Caution: while qdict_get_try_str() is fine, getting non-string
2230 * types would require more care. When @options come from
2231 * -blockdev or blockdev_add, its members are typed according to
2232 * the QAPI schema, but when they come from -drive, they're all
2233 * QString.
2234 */
Max Reitzda557aa2013-12-20 19:28:11 +01002235 reference = qdict_get_try_str(options, bdref_key);
2236 if (!filename && !reference && !qdict_size(image_options)) {
Kevin Wolfb4b059f2015-06-15 13:24:19 +02002237 if (!allow_none) {
Max Reitzda557aa2013-12-20 19:28:11 +01002238 error_setg(errp, "A block device must be specified for \"%s\"",
2239 bdref_key);
Max Reitzda557aa2013-12-20 19:28:11 +01002240 }
Markus Armbrusterb20e61e2014-05-28 11:16:57 +02002241 QDECREF(image_options);
Max Reitzda557aa2013-12-20 19:28:11 +01002242 goto done;
2243 }
2244
Max Reitz5b363932016-05-17 16:41:31 +02002245 bs = bdrv_open_inherit(filename, reference, image_options, 0,
2246 parent, child_role, errp);
2247 if (!bs) {
Kevin Wolfdf581792015-06-15 11:53:47 +02002248 goto done;
2249 }
2250
Max Reitzda557aa2013-12-20 19:28:11 +01002251done:
2252 qdict_del(options, bdref_key);
Kevin Wolf2d6b86a2017-02-17 17:43:59 +01002253 return bs;
2254}
2255
2256/*
2257 * Opens a disk image whose options are given as BlockdevRef in another block
2258 * device's options.
2259 *
2260 * If allow_none is true, no image will be opened if filename is false and no
2261 * BlockdevRef is given. NULL will be returned, but errp remains unset.
2262 *
2263 * bdrev_key specifies the key for the image's BlockdevRef in the options QDict.
2264 * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict
2265 * itself, all options starting with "${bdref_key}." are considered part of the
2266 * BlockdevRef.
2267 *
2268 * The BlockdevRef will be removed from the options QDict.
2269 */
2270BdrvChild *bdrv_open_child(const char *filename,
2271 QDict *options, const char *bdref_key,
2272 BlockDriverState *parent,
2273 const BdrvChildRole *child_role,
2274 bool allow_none, Error **errp)
2275{
Kevin Wolf8b2ff522016-12-20 22:21:17 +01002276 BdrvChild *c;
Kevin Wolf2d6b86a2017-02-17 17:43:59 +01002277 BlockDriverState *bs;
2278
2279 bs = bdrv_open_child_bs(filename, options, bdref_key, parent, child_role,
2280 allow_none, errp);
2281 if (bs == NULL) {
2282 return NULL;
2283 }
2284
Kevin Wolf8b2ff522016-12-20 22:21:17 +01002285 c = bdrv_attach_child(parent, bs, bdref_key, child_role, errp);
2286 if (!c) {
2287 bdrv_unref(bs);
2288 return NULL;
2289 }
2290
2291 return c;
Kevin Wolfb4b059f2015-06-15 13:24:19 +02002292}
2293
Max Reitz66836182016-05-17 16:41:27 +02002294static BlockDriverState *bdrv_append_temp_snapshot(BlockDriverState *bs,
2295 int flags,
2296 QDict *snapshot_options,
2297 Error **errp)
Kevin Wolfb9988752014-04-03 12:09:34 +02002298{
2299 /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02002300 char *tmp_filename = g_malloc0(PATH_MAX + 1);
Kevin Wolfb9988752014-04-03 12:09:34 +02002301 int64_t total_size;
Chunyan Liu83d05212014-06-05 17:20:51 +08002302 QemuOpts *opts = NULL;
Eric Blakeff6ed712017-04-27 16:58:18 -05002303 BlockDriverState *bs_snapshot = NULL;
Kevin Wolfb2c28322017-02-20 12:46:42 +01002304 Error *local_err = NULL;
Kevin Wolfb9988752014-04-03 12:09:34 +02002305 int ret;
2306
2307 /* if snapshot, we create a temporary backing file and open it
2308 instead of opening 'filename' directly */
2309
2310 /* Get the required size from the image */
Kevin Wolff1877432014-04-04 17:07:19 +02002311 total_size = bdrv_getlength(bs);
2312 if (total_size < 0) {
2313 error_setg_errno(errp, -total_size, "Could not get image size");
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02002314 goto out;
Kevin Wolff1877432014-04-04 17:07:19 +02002315 }
Kevin Wolfb9988752014-04-03 12:09:34 +02002316
2317 /* Create the temporary image */
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02002318 ret = get_tmp_filename(tmp_filename, PATH_MAX + 1);
Kevin Wolfb9988752014-04-03 12:09:34 +02002319 if (ret < 0) {
2320 error_setg_errno(errp, -ret, "Could not get temporary filename");
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02002321 goto out;
Kevin Wolfb9988752014-04-03 12:09:34 +02002322 }
2323
Max Reitzef810432014-12-02 18:32:42 +01002324 opts = qemu_opts_create(bdrv_qcow2.create_opts, NULL, 0,
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002325 &error_abort);
Markus Armbruster39101f22015-02-12 16:46:36 +01002326 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_size, &error_abort);
Markus Armbrustere43bfd92015-12-18 16:35:15 +01002327 ret = bdrv_create(&bdrv_qcow2, tmp_filename, opts, errp);
Chunyan Liu83d05212014-06-05 17:20:51 +08002328 qemu_opts_del(opts);
Kevin Wolfb9988752014-04-03 12:09:34 +02002329 if (ret < 0) {
Markus Armbrustere43bfd92015-12-18 16:35:15 +01002330 error_prepend(errp, "Could not create temporary overlay '%s': ",
2331 tmp_filename);
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02002332 goto out;
Kevin Wolfb9988752014-04-03 12:09:34 +02002333 }
2334
Kevin Wolf73176be2016-03-07 13:02:15 +01002335 /* Prepare options QDict for the temporary file */
Eric Blake46f5ac22017-04-27 16:58:17 -05002336 qdict_put_str(snapshot_options, "file.driver", "file");
2337 qdict_put_str(snapshot_options, "file.filename", tmp_filename);
2338 qdict_put_str(snapshot_options, "driver", "qcow2");
Kevin Wolfb9988752014-04-03 12:09:34 +02002339
Max Reitz5b363932016-05-17 16:41:31 +02002340 bs_snapshot = bdrv_open(NULL, NULL, snapshot_options, flags, errp);
Kevin Wolf73176be2016-03-07 13:02:15 +01002341 snapshot_options = NULL;
Max Reitz5b363932016-05-17 16:41:31 +02002342 if (!bs_snapshot) {
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02002343 goto out;
Kevin Wolfb9988752014-04-03 12:09:34 +02002344 }
2345
Eric Blakeff6ed712017-04-27 16:58:18 -05002346 /* bdrv_append() consumes a strong reference to bs_snapshot
2347 * (i.e. it will call bdrv_unref() on it) even on error, so in
2348 * order to be able to return one, we have to increase
2349 * bs_snapshot's refcount here */
Max Reitz66836182016-05-17 16:41:27 +02002350 bdrv_ref(bs_snapshot);
Kevin Wolfb2c28322017-02-20 12:46:42 +01002351 bdrv_append(bs_snapshot, bs, &local_err);
2352 if (local_err) {
2353 error_propagate(errp, local_err);
Eric Blakeff6ed712017-04-27 16:58:18 -05002354 bs_snapshot = NULL;
Kevin Wolfb2c28322017-02-20 12:46:42 +01002355 goto out;
2356 }
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02002357
2358out:
Kevin Wolf73176be2016-03-07 13:02:15 +01002359 QDECREF(snapshot_options);
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02002360 g_free(tmp_filename);
Eric Blakeff6ed712017-04-27 16:58:18 -05002361 return bs_snapshot;
Kevin Wolfb9988752014-04-03 12:09:34 +02002362}
2363
Max Reitzda557aa2013-12-20 19:28:11 +01002364/*
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02002365 * Opens a disk image (raw, qcow2, vmdk, ...)
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01002366 *
2367 * options is a QDict of options to pass to the block drivers, or NULL for an
2368 * empty set of options. The reference to the QDict belongs to the block layer
2369 * after the call (even on failure), so if the caller intends to reuse the
2370 * dictionary, it needs to use QINCREF() before calling bdrv_open.
Max Reitzf67503e2014-02-18 18:33:05 +01002371 *
2372 * If *pbs is NULL, a new BDS will be created with a pointer to it stored there.
2373 * If it is not NULL, the referenced BDS will be reused.
Max Reitzddf56362014-02-18 18:33:06 +01002374 *
2375 * The reference parameter may be used to specify an existing block device which
2376 * should be opened. If specified, neither options nor a filename may be given,
2377 * nor can an existing BDS be reused (that is, *pbs has to be NULL).
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02002378 */
Max Reitz5b363932016-05-17 16:41:31 +02002379static BlockDriverState *bdrv_open_inherit(const char *filename,
2380 const char *reference,
2381 QDict *options, int flags,
2382 BlockDriverState *parent,
2383 const BdrvChildRole *child_role,
2384 Error **errp)
bellardea2384d2004-08-01 21:59:26 +00002385{
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02002386 int ret;
Kevin Wolf5696c6e2017-02-17 18:39:24 +01002387 BlockBackend *file = NULL;
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02002388 BlockDriverState *bs;
Max Reitzce343772015-08-26 19:47:50 +02002389 BlockDriver *drv = NULL;
Kevin Wolf74fe54f2013-07-09 11:09:02 +02002390 const char *drvname;
Alberto Garcia3e8c2e52015-10-26 14:27:15 +02002391 const char *backing;
Max Reitz34b5d2c2013-09-05 14:45:29 +02002392 Error *local_err = NULL;
Kevin Wolf73176be2016-03-07 13:02:15 +01002393 QDict *snapshot_options = NULL;
Kevin Wolfb1e6fc02014-05-06 12:11:42 +02002394 int snapshot_flags = 0;
bellard712e7872005-04-28 21:09:32 +00002395
Kevin Wolff3930ed2015-04-08 13:43:47 +02002396 assert(!child_role || !flags);
2397 assert(!child_role == !parent);
Max Reitzf67503e2014-02-18 18:33:05 +01002398
Max Reitzddf56362014-02-18 18:33:06 +01002399 if (reference) {
2400 bool options_non_empty = options ? qdict_size(options) : false;
2401 QDECREF(options);
2402
Max Reitzddf56362014-02-18 18:33:06 +01002403 if (filename || options_non_empty) {
2404 error_setg(errp, "Cannot reference an existing block device with "
2405 "additional options or a new filename");
Max Reitz5b363932016-05-17 16:41:31 +02002406 return NULL;
Max Reitzddf56362014-02-18 18:33:06 +01002407 }
2408
2409 bs = bdrv_lookup_bs(reference, reference, errp);
2410 if (!bs) {
Max Reitz5b363932016-05-17 16:41:31 +02002411 return NULL;
Max Reitzddf56362014-02-18 18:33:06 +01002412 }
Kevin Wolf76b22322016-04-04 17:11:13 +02002413
Max Reitzddf56362014-02-18 18:33:06 +01002414 bdrv_ref(bs);
Max Reitz5b363932016-05-17 16:41:31 +02002415 return bs;
Max Reitzddf56362014-02-18 18:33:06 +01002416 }
2417
Max Reitz5b363932016-05-17 16:41:31 +02002418 bs = bdrv_new();
Max Reitzf67503e2014-02-18 18:33:05 +01002419
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01002420 /* NULL means an empty set of options */
2421 if (options == NULL) {
2422 options = qdict_new();
2423 }
2424
Kevin Wolf145f5982015-05-08 16:15:03 +02002425 /* json: syntax counts as explicit options, as if in the QDict */
Kevin Wolfde3b53f2015-10-29 15:24:41 +01002426 parse_json_protocol(options, &filename, &local_err);
2427 if (local_err) {
Kevin Wolfde3b53f2015-10-29 15:24:41 +01002428 goto fail;
2429 }
2430
Kevin Wolf145f5982015-05-08 16:15:03 +02002431 bs->explicit_options = qdict_clone_shallow(options);
2432
Kevin Wolff3930ed2015-04-08 13:43:47 +02002433 if (child_role) {
Kevin Wolfbddcec32015-04-09 18:47:50 +02002434 bs->inherits_from = parent;
Kevin Wolf8e2160e2015-04-29 17:29:39 +02002435 child_role->inherit_options(&flags, options,
2436 parent->open_flags, parent->options);
Kevin Wolff3930ed2015-04-08 13:43:47 +02002437 }
2438
Kevin Wolfde3b53f2015-10-29 15:24:41 +01002439 ret = bdrv_fill_options(&options, filename, &flags, &local_err);
Kevin Wolf462f5bc2014-05-26 11:39:55 +02002440 if (local_err) {
2441 goto fail;
2442 }
2443
Markus Armbruster129c7d12017-03-30 19:43:12 +02002444 /*
2445 * Set the BDRV_O_RDWR and BDRV_O_ALLOW_RDWR flags.
2446 * Caution: getting a boolean member of @options requires care.
2447 * When @options come from -blockdev or blockdev_add, members are
2448 * typed according to the QAPI schema, but when they come from
2449 * -drive, they're all QString.
2450 */
Alberto Garciaf87a0e22016-09-15 17:53:02 +03002451 if (g_strcmp0(qdict_get_try_str(options, BDRV_OPT_READ_ONLY), "on") &&
2452 !qdict_get_try_bool(options, BDRV_OPT_READ_ONLY, false)) {
2453 flags |= (BDRV_O_RDWR | BDRV_O_ALLOW_RDWR);
2454 } else {
2455 flags &= ~BDRV_O_RDWR;
Alberto Garcia14499ea2016-09-15 17:53:00 +03002456 }
2457
2458 if (flags & BDRV_O_SNAPSHOT) {
2459 snapshot_options = qdict_new();
2460 bdrv_temp_snapshot_options(&snapshot_flags, snapshot_options,
2461 flags, options);
Alberto Garciaf87a0e22016-09-15 17:53:02 +03002462 /* Let bdrv_backing_options() override "read-only" */
2463 qdict_del(options, BDRV_OPT_READ_ONLY);
Alberto Garcia14499ea2016-09-15 17:53:00 +03002464 bdrv_backing_options(&flags, options, flags, options);
2465 }
2466
Kevin Wolf62392eb2015-04-24 16:38:02 +02002467 bs->open_flags = flags;
2468 bs->options = options;
2469 options = qdict_clone_shallow(options);
2470
Kevin Wolf76c591b2014-06-04 14:19:44 +02002471 /* Find the right image format driver */
Markus Armbruster129c7d12017-03-30 19:43:12 +02002472 /* See cautionary note on accessing @options above */
Kevin Wolf76c591b2014-06-04 14:19:44 +02002473 drvname = qdict_get_try_str(options, "driver");
2474 if (drvname) {
2475 drv = bdrv_find_format(drvname);
Kevin Wolf76c591b2014-06-04 14:19:44 +02002476 if (!drv) {
2477 error_setg(errp, "Unknown driver: '%s'", drvname);
Kevin Wolf76c591b2014-06-04 14:19:44 +02002478 goto fail;
2479 }
2480 }
2481
2482 assert(drvname || !(flags & BDRV_O_PROTOCOL));
Kevin Wolf76c591b2014-06-04 14:19:44 +02002483
Markus Armbruster129c7d12017-03-30 19:43:12 +02002484 /* See cautionary note on accessing @options above */
Alberto Garcia3e8c2e52015-10-26 14:27:15 +02002485 backing = qdict_get_try_str(options, "backing");
2486 if (backing && *backing == '\0') {
2487 flags |= BDRV_O_NO_BACKING;
2488 qdict_del(options, "backing");
2489 }
2490
Kevin Wolf5696c6e2017-02-17 18:39:24 +01002491 /* Open image file without format layer. This BlockBackend is only used for
Kevin Wolf4e4bf5c2016-12-16 18:52:37 +01002492 * probing, the block drivers will do their own bdrv_open_child() for the
2493 * same BDS, which is why we put the node name back into options. */
Kevin Wolff4788ad2014-06-03 16:44:19 +02002494 if ((flags & BDRV_O_PROTOCOL) == 0) {
Kevin Wolf5696c6e2017-02-17 18:39:24 +01002495 BlockDriverState *file_bs;
2496
2497 file_bs = bdrv_open_child_bs(filename, options, "file", bs,
2498 &child_file, true, &local_err);
Kevin Wolf1fdd6932015-06-15 14:11:51 +02002499 if (local_err) {
Max Reitz5469a2a2014-02-18 18:33:10 +01002500 goto fail;
2501 }
Kevin Wolf5696c6e2017-02-17 18:39:24 +01002502 if (file_bs != NULL) {
Kevin Wolf6d0eb642017-01-20 17:07:26 +01002503 file = blk_new(BLK_PERM_CONSISTENT_READ, BLK_PERM_ALL);
Kevin Wolfd7086422017-01-13 19:02:32 +01002504 blk_insert_bs(file, file_bs, &local_err);
Kevin Wolf5696c6e2017-02-17 18:39:24 +01002505 bdrv_unref(file_bs);
Kevin Wolfd7086422017-01-13 19:02:32 +01002506 if (local_err) {
2507 goto fail;
2508 }
Kevin Wolf5696c6e2017-02-17 18:39:24 +01002509
Eric Blake46f5ac22017-04-27 16:58:17 -05002510 qdict_put_str(options, "file", bdrv_get_node_name(file_bs));
Kevin Wolf4e4bf5c2016-12-16 18:52:37 +01002511 }
Max Reitz5469a2a2014-02-18 18:33:10 +01002512 }
2513
Kevin Wolf76c591b2014-06-04 14:19:44 +02002514 /* Image format probing */
Kevin Wolf38f3ef52014-11-20 16:27:12 +01002515 bs->probed = !drv;
Kevin Wolf76c591b2014-06-04 14:19:44 +02002516 if (!drv && file) {
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +02002517 ret = find_image_format(file, filename, &drv, &local_err);
Kevin Wolf17b005f2014-05-27 10:50:29 +02002518 if (ret < 0) {
Kevin Wolf8bfea152014-04-11 19:16:36 +02002519 goto fail;
Max Reitz2a05cbe2013-12-20 19:28:10 +01002520 }
Kevin Wolf62392eb2015-04-24 16:38:02 +02002521 /*
2522 * This option update would logically belong in bdrv_fill_options(),
2523 * but we first need to open bs->file for the probing to work, while
2524 * opening bs->file already requires the (mostly) final set of options
2525 * so that cache mode etc. can be inherited.
2526 *
2527 * Adding the driver later is somewhat ugly, but it's not an option
2528 * that would ever be inherited, so it's correct. We just need to make
2529 * sure to update both bs->options (which has the full effective
2530 * options for bs) and options (which has file.* already removed).
2531 */
Eric Blake46f5ac22017-04-27 16:58:17 -05002532 qdict_put_str(bs->options, "driver", drv->format_name);
2533 qdict_put_str(options, "driver", drv->format_name);
Kevin Wolf76c591b2014-06-04 14:19:44 +02002534 } else if (!drv) {
Kevin Wolf17b005f2014-05-27 10:50:29 +02002535 error_setg(errp, "Must specify either driver or file");
Kevin Wolf8bfea152014-04-11 19:16:36 +02002536 goto fail;
Kevin Wolff500a6d2012-11-12 17:35:27 +01002537 }
2538
Max Reitz53a29512015-03-19 14:53:16 -04002539 /* BDRV_O_PROTOCOL must be set iff a protocol BDS is about to be created */
2540 assert(!!(flags & BDRV_O_PROTOCOL) == !!drv->bdrv_file_open);
2541 /* file must be NULL if a protocol BDS is about to be created
2542 * (the inverse results in an error message from bdrv_open_common()) */
2543 assert(!(flags & BDRV_O_PROTOCOL) || !file);
2544
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02002545 /* Open the image */
Kevin Wolf82dc8b42016-01-11 19:07:50 +01002546 ret = bdrv_open_common(bs, file, options, &local_err);
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02002547 if (ret < 0) {
Kevin Wolf8bfea152014-04-11 19:16:36 +02002548 goto fail;
Christoph Hellwig69873072010-01-20 18:13:25 +01002549 }
2550
Kevin Wolf4e4bf5c2016-12-16 18:52:37 +01002551 if (file) {
Kevin Wolf5696c6e2017-02-17 18:39:24 +01002552 blk_unref(file);
Kevin Wolff500a6d2012-11-12 17:35:27 +01002553 file = NULL;
2554 }
2555
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02002556 /* If there is a backing file, use it */
Paolo Bonzini9156df12012-10-18 16:49:17 +02002557 if ((flags & BDRV_O_NO_BACKING) == 0) {
Kevin Wolfd9b7b052015-01-16 18:23:41 +01002558 ret = bdrv_open_backing_file(bs, options, "backing", &local_err);
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02002559 if (ret < 0) {
Kevin Wolfb6ad4912013-03-15 10:35:04 +01002560 goto close_and_fail;
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02002561 }
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02002562 }
2563
Max Reitz91af7012014-07-18 20:24:56 +02002564 bdrv_refresh_filename(bs);
2565
Kevin Wolfb6ad4912013-03-15 10:35:04 +01002566 /* Check if any unknown options were used */
Paolo Bonzini7ad27572017-01-04 15:59:14 +01002567 if (qdict_size(options) != 0) {
Kevin Wolfb6ad4912013-03-15 10:35:04 +01002568 const QDictEntry *entry = qdict_first(options);
Max Reitz5acd9d82014-02-18 18:33:11 +01002569 if (flags & BDRV_O_PROTOCOL) {
2570 error_setg(errp, "Block protocol '%s' doesn't support the option "
2571 "'%s'", drv->format_name, entry->key);
2572 } else {
Max Reitzd0e46a52016-03-16 19:54:34 +01002573 error_setg(errp,
2574 "Block format '%s' does not support the option '%s'",
2575 drv->format_name, entry->key);
Max Reitz5acd9d82014-02-18 18:33:11 +01002576 }
Kevin Wolfb6ad4912013-03-15 10:35:04 +01002577
Kevin Wolfb6ad4912013-03-15 10:35:04 +01002578 goto close_and_fail;
2579 }
Kevin Wolfb6ad4912013-03-15 10:35:04 +01002580
Daniel P. Berrangec01c2142017-06-23 17:24:16 +01002581 bdrv_parent_cb_change_media(bs, true);
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02002582
Markus Armbrusterc3adb582014-03-14 09:22:48 +01002583 QDECREF(options);
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002584
2585 /* For snapshot=on, create a temporary qcow2 overlay. bs points to the
2586 * temporary snapshot afterwards. */
2587 if (snapshot_flags) {
Max Reitz66836182016-05-17 16:41:27 +02002588 BlockDriverState *snapshot_bs;
2589 snapshot_bs = bdrv_append_temp_snapshot(bs, snapshot_flags,
2590 snapshot_options, &local_err);
Kevin Wolf73176be2016-03-07 13:02:15 +01002591 snapshot_options = NULL;
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002592 if (local_err) {
2593 goto close_and_fail;
2594 }
Max Reitz5b363932016-05-17 16:41:31 +02002595 /* We are not going to return bs but the overlay on top of it
2596 * (snapshot_bs); thus, we have to drop the strong reference to bs
2597 * (which we obtained by calling bdrv_new()). bs will not be deleted,
2598 * though, because the overlay still has a reference to it. */
2599 bdrv_unref(bs);
2600 bs = snapshot_bs;
Max Reitz66836182016-05-17 16:41:27 +02002601 }
2602
Max Reitz5b363932016-05-17 16:41:31 +02002603 return bs;
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02002604
Kevin Wolf8bfea152014-04-11 19:16:36 +02002605fail:
Kevin Wolf5696c6e2017-02-17 18:39:24 +01002606 blk_unref(file);
Kevin Wolf73176be2016-03-07 13:02:15 +01002607 QDECREF(snapshot_options);
Kevin Wolf145f5982015-05-08 16:15:03 +02002608 QDECREF(bs->explicit_options);
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01002609 QDECREF(bs->options);
Kevin Wolfb6ad4912013-03-15 10:35:04 +01002610 QDECREF(options);
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01002611 bs->options = NULL;
Manos Pitsidianakis998cbd62017-07-14 17:35:47 +03002612 bs->explicit_options = NULL;
Max Reitz5b363932016-05-17 16:41:31 +02002613 bdrv_unref(bs);
Eduardo Habkost621ff942016-06-13 18:57:56 -03002614 error_propagate(errp, local_err);
Max Reitz5b363932016-05-17 16:41:31 +02002615 return NULL;
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01002616
Kevin Wolfb6ad4912013-03-15 10:35:04 +01002617close_and_fail:
Max Reitz5b363932016-05-17 16:41:31 +02002618 bdrv_unref(bs);
Kevin Wolf73176be2016-03-07 13:02:15 +01002619 QDECREF(snapshot_options);
Kevin Wolfb6ad4912013-03-15 10:35:04 +01002620 QDECREF(options);
Eduardo Habkost621ff942016-06-13 18:57:56 -03002621 error_propagate(errp, local_err);
Max Reitz5b363932016-05-17 16:41:31 +02002622 return NULL;
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02002623}
2624
Max Reitz5b363932016-05-17 16:41:31 +02002625BlockDriverState *bdrv_open(const char *filename, const char *reference,
2626 QDict *options, int flags, Error **errp)
Kevin Wolff3930ed2015-04-08 13:43:47 +02002627{
Max Reitz5b363932016-05-17 16:41:31 +02002628 return bdrv_open_inherit(filename, reference, options, flags, NULL,
Max Reitzce343772015-08-26 19:47:50 +02002629 NULL, errp);
Kevin Wolff3930ed2015-04-08 13:43:47 +02002630}
2631
Jeff Codye971aa12012-09-20 15:13:19 -04002632typedef struct BlockReopenQueueEntry {
2633 bool prepared;
2634 BDRVReopenState state;
2635 QSIMPLEQ_ENTRY(BlockReopenQueueEntry) entry;
2636} BlockReopenQueueEntry;
2637
2638/*
2639 * Adds a BlockDriverState to a simple queue for an atomic, transactional
2640 * reopen of multiple devices.
2641 *
2642 * bs_queue can either be an existing BlockReopenQueue that has had QSIMPLE_INIT
2643 * already performed, or alternatively may be NULL a new BlockReopenQueue will
2644 * be created and initialized. This newly created BlockReopenQueue should be
2645 * passed back in for subsequent calls that are intended to be of the same
2646 * atomic 'set'.
2647 *
2648 * bs is the BlockDriverState to add to the reopen queue.
2649 *
Kevin Wolf4d2cb092015-04-10 17:50:50 +02002650 * options contains the changed options for the associated bs
2651 * (the BlockReopenQueue takes ownership)
2652 *
Jeff Codye971aa12012-09-20 15:13:19 -04002653 * flags contains the open flags for the associated bs
2654 *
2655 * returns a pointer to bs_queue, which is either the newly allocated
2656 * bs_queue, or the existing bs_queue being used.
2657 *
2658 */
Kevin Wolf28518102015-05-08 17:07:31 +02002659static BlockReopenQueue *bdrv_reopen_queue_child(BlockReopenQueue *bs_queue,
2660 BlockDriverState *bs,
2661 QDict *options,
2662 int flags,
2663 const BdrvChildRole *role,
2664 QDict *parent_options,
2665 int parent_flags)
Jeff Codye971aa12012-09-20 15:13:19 -04002666{
2667 assert(bs != NULL);
2668
2669 BlockReopenQueueEntry *bs_entry;
Kevin Wolf67251a32015-04-09 18:54:04 +02002670 BdrvChild *child;
Kevin Wolf145f5982015-05-08 16:15:03 +02002671 QDict *old_options, *explicit_options;
Kevin Wolf67251a32015-04-09 18:54:04 +02002672
Jeff Codye971aa12012-09-20 15:13:19 -04002673 if (bs_queue == NULL) {
2674 bs_queue = g_new0(BlockReopenQueue, 1);
2675 QSIMPLEQ_INIT(bs_queue);
2676 }
2677
Kevin Wolf4d2cb092015-04-10 17:50:50 +02002678 if (!options) {
2679 options = qdict_new();
2680 }
2681
Alberto Garcia5b7ba052016-09-15 17:53:03 +03002682 /* Check if this BlockDriverState is already in the queue */
2683 QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) {
2684 if (bs == bs_entry->state.bs) {
2685 break;
2686 }
2687 }
2688
Kevin Wolf28518102015-05-08 17:07:31 +02002689 /*
2690 * Precedence of options:
2691 * 1. Explicitly passed in options (highest)
Kevin Wolf91a097e2015-05-08 17:49:53 +02002692 * 2. Set in flags (only for top level)
Kevin Wolf145f5982015-05-08 16:15:03 +02002693 * 3. Retained from explicitly set options of bs
Kevin Wolf8e2160e2015-04-29 17:29:39 +02002694 * 4. Inherited from parent node
Kevin Wolf28518102015-05-08 17:07:31 +02002695 * 5. Retained from effective options of bs
2696 */
2697
Kevin Wolf91a097e2015-05-08 17:49:53 +02002698 if (!parent_options) {
2699 /*
2700 * Any setting represented by flags is always updated. If the
2701 * corresponding QDict option is set, it takes precedence. Otherwise
2702 * the flag is translated into a QDict option. The old setting of bs is
2703 * not considered.
2704 */
2705 update_options_from_flags(options, flags);
2706 }
2707
Kevin Wolf145f5982015-05-08 16:15:03 +02002708 /* Old explicitly set values (don't overwrite by inherited value) */
Alberto Garcia5b7ba052016-09-15 17:53:03 +03002709 if (bs_entry) {
2710 old_options = qdict_clone_shallow(bs_entry->state.explicit_options);
2711 } else {
2712 old_options = qdict_clone_shallow(bs->explicit_options);
2713 }
Kevin Wolf145f5982015-05-08 16:15:03 +02002714 bdrv_join_options(bs, options, old_options);
2715 QDECREF(old_options);
2716
2717 explicit_options = qdict_clone_shallow(options);
2718
Kevin Wolf28518102015-05-08 17:07:31 +02002719 /* Inherit from parent node */
2720 if (parent_options) {
2721 assert(!flags);
Kevin Wolf8e2160e2015-04-29 17:29:39 +02002722 role->inherit_options(&flags, options, parent_flags, parent_options);
Kevin Wolf28518102015-05-08 17:07:31 +02002723 }
2724
2725 /* Old values are used for options that aren't set yet */
Kevin Wolf4d2cb092015-04-10 17:50:50 +02002726 old_options = qdict_clone_shallow(bs->options);
Kevin Wolfcddff5b2015-11-16 16:43:27 +01002727 bdrv_join_options(bs, options, old_options);
Kevin Wolf4d2cb092015-04-10 17:50:50 +02002728 QDECREF(old_options);
2729
Kevin Wolffd452022017-08-03 17:02:59 +02002730 /* bdrv_open_inherit() sets and clears some additional flags internally */
Kevin Wolff1f25a22014-04-25 19:04:55 +02002731 flags &= ~BDRV_O_PROTOCOL;
Kevin Wolffd452022017-08-03 17:02:59 +02002732 if (flags & BDRV_O_RDWR) {
2733 flags |= BDRV_O_ALLOW_RDWR;
2734 }
Kevin Wolff1f25a22014-04-25 19:04:55 +02002735
Kevin Wolf67251a32015-04-09 18:54:04 +02002736 QLIST_FOREACH(child, &bs->children, next) {
Kevin Wolf4c9dfe52015-05-08 15:14:15 +02002737 QDict *new_child_options;
2738 char *child_key_dot;
Kevin Wolf67251a32015-04-09 18:54:04 +02002739
Kevin Wolf4c9dfe52015-05-08 15:14:15 +02002740 /* reopen can only change the options of block devices that were
2741 * implicitly created and inherited options. For other (referenced)
2742 * block devices, a syntax like "backing.foo" results in an error. */
Kevin Wolf67251a32015-04-09 18:54:04 +02002743 if (child->bs->inherits_from != bs) {
2744 continue;
2745 }
2746
Kevin Wolf4c9dfe52015-05-08 15:14:15 +02002747 child_key_dot = g_strdup_printf("%s.", child->name);
2748 qdict_extract_subqdict(options, &new_child_options, child_key_dot);
2749 g_free(child_key_dot);
2750
Kevin Wolf28518102015-05-08 17:07:31 +02002751 bdrv_reopen_queue_child(bs_queue, child->bs, new_child_options, 0,
2752 child->role, options, flags);
Jeff Codye971aa12012-09-20 15:13:19 -04002753 }
2754
Alberto Garcia5b7ba052016-09-15 17:53:03 +03002755 if (!bs_entry) {
2756 bs_entry = g_new0(BlockReopenQueueEntry, 1);
2757 QSIMPLEQ_INSERT_TAIL(bs_queue, bs_entry, entry);
2758 } else {
2759 QDECREF(bs_entry->state.options);
2760 QDECREF(bs_entry->state.explicit_options);
2761 }
Jeff Codye971aa12012-09-20 15:13:19 -04002762
2763 bs_entry->state.bs = bs;
Kevin Wolf4d2cb092015-04-10 17:50:50 +02002764 bs_entry->state.options = options;
Kevin Wolf145f5982015-05-08 16:15:03 +02002765 bs_entry->state.explicit_options = explicit_options;
Jeff Codye971aa12012-09-20 15:13:19 -04002766 bs_entry->state.flags = flags;
2767
2768 return bs_queue;
2769}
2770
Kevin Wolf28518102015-05-08 17:07:31 +02002771BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue,
2772 BlockDriverState *bs,
2773 QDict *options, int flags)
2774{
2775 return bdrv_reopen_queue_child(bs_queue, bs, options, flags,
2776 NULL, NULL, 0);
2777}
2778
Jeff Codye971aa12012-09-20 15:13:19 -04002779/*
2780 * Reopen multiple BlockDriverStates atomically & transactionally.
2781 *
2782 * The queue passed in (bs_queue) must have been built up previous
2783 * via bdrv_reopen_queue().
2784 *
2785 * Reopens all BDS specified in the queue, with the appropriate
2786 * flags. All devices are prepared for reopen, and failure of any
2787 * device will cause all device changes to be abandonded, and intermediate
2788 * data cleaned up.
2789 *
2790 * If all devices prepare successfully, then the changes are committed
2791 * to all devices.
2792 *
2793 */
Paolo Bonzini720150f2016-10-27 12:49:02 +02002794int bdrv_reopen_multiple(AioContext *ctx, BlockReopenQueue *bs_queue, Error **errp)
Jeff Codye971aa12012-09-20 15:13:19 -04002795{
2796 int ret = -1;
2797 BlockReopenQueueEntry *bs_entry, *next;
2798 Error *local_err = NULL;
2799
2800 assert(bs_queue != NULL);
2801
Paolo Bonzinic9d1a562016-10-27 12:49:05 +02002802 aio_context_release(ctx);
Alberto Garcia40840e42016-10-28 10:08:03 +03002803 bdrv_drain_all_begin();
Paolo Bonzinic9d1a562016-10-27 12:49:05 +02002804 aio_context_acquire(ctx);
Jeff Codye971aa12012-09-20 15:13:19 -04002805
2806 QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) {
2807 if (bdrv_reopen_prepare(&bs_entry->state, bs_queue, &local_err)) {
2808 error_propagate(errp, local_err);
2809 goto cleanup;
2810 }
2811 bs_entry->prepared = true;
2812 }
2813
2814 /* If we reach this point, we have success and just need to apply the
2815 * changes
2816 */
2817 QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) {
2818 bdrv_reopen_commit(&bs_entry->state);
2819 }
2820
2821 ret = 0;
2822
2823cleanup:
2824 QSIMPLEQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) {
2825 if (ret && bs_entry->prepared) {
2826 bdrv_reopen_abort(&bs_entry->state);
Kevin Wolf145f5982015-05-08 16:15:03 +02002827 } else if (ret) {
2828 QDECREF(bs_entry->state.explicit_options);
Jeff Codye971aa12012-09-20 15:13:19 -04002829 }
Kevin Wolf4d2cb092015-04-10 17:50:50 +02002830 QDECREF(bs_entry->state.options);
Jeff Codye971aa12012-09-20 15:13:19 -04002831 g_free(bs_entry);
2832 }
2833 g_free(bs_queue);
Alberto Garcia40840e42016-10-28 10:08:03 +03002834
2835 bdrv_drain_all_end();
2836
Jeff Codye971aa12012-09-20 15:13:19 -04002837 return ret;
2838}
2839
2840
2841/* Reopen a single BlockDriverState with the specified flags. */
2842int bdrv_reopen(BlockDriverState *bs, int bdrv_flags, Error **errp)
2843{
2844 int ret = -1;
2845 Error *local_err = NULL;
Kevin Wolf4d2cb092015-04-10 17:50:50 +02002846 BlockReopenQueue *queue = bdrv_reopen_queue(NULL, bs, NULL, bdrv_flags);
Jeff Codye971aa12012-09-20 15:13:19 -04002847
Paolo Bonzini720150f2016-10-27 12:49:02 +02002848 ret = bdrv_reopen_multiple(bdrv_get_aio_context(bs), queue, &local_err);
Jeff Codye971aa12012-09-20 15:13:19 -04002849 if (local_err != NULL) {
2850 error_propagate(errp, local_err);
2851 }
2852 return ret;
2853}
2854
2855
2856/*
2857 * Prepares a BlockDriverState for reopen. All changes are staged in the
2858 * 'opaque' field of the BDRVReopenState, which is used and allocated by
2859 * the block driver layer .bdrv_reopen_prepare()
2860 *
2861 * bs is the BlockDriverState to reopen
2862 * flags are the new open flags
2863 * queue is the reopen queue
2864 *
2865 * Returns 0 on success, non-zero on error. On error errp will be set
2866 * as well.
2867 *
2868 * On failure, bdrv_reopen_abort() will be called to clean up any data.
2869 * It is the responsibility of the caller to then call the abort() or
2870 * commit() for any other BDS that have been left in a prepare() state
2871 *
2872 */
2873int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue,
2874 Error **errp)
2875{
2876 int ret = -1;
2877 Error *local_err = NULL;
2878 BlockDriver *drv;
Kevin Wolfccf9dc02015-05-08 17:24:56 +02002879 QemuOpts *opts;
2880 const char *value;
Jeff Cody3d8ce172017-04-07 16:55:30 -04002881 bool read_only;
Jeff Codye971aa12012-09-20 15:13:19 -04002882
2883 assert(reopen_state != NULL);
2884 assert(reopen_state->bs->drv != NULL);
2885 drv = reopen_state->bs->drv;
2886
Kevin Wolfccf9dc02015-05-08 17:24:56 +02002887 /* Process generic block layer options */
2888 opts = qemu_opts_create(&bdrv_runtime_opts, NULL, 0, &error_abort);
2889 qemu_opts_absorb_qdict(opts, reopen_state->options, &local_err);
2890 if (local_err) {
2891 error_propagate(errp, local_err);
2892 ret = -EINVAL;
2893 goto error;
2894 }
2895
Kevin Wolf91a097e2015-05-08 17:49:53 +02002896 update_flags_from_options(&reopen_state->flags, opts);
2897
Kevin Wolfccf9dc02015-05-08 17:24:56 +02002898 /* node-name and driver must be unchanged. Put them back into the QDict, so
2899 * that they are checked at the end of this function. */
2900 value = qemu_opt_get(opts, "node-name");
2901 if (value) {
Eric Blake46f5ac22017-04-27 16:58:17 -05002902 qdict_put_str(reopen_state->options, "node-name", value);
Kevin Wolfccf9dc02015-05-08 17:24:56 +02002903 }
2904
2905 value = qemu_opt_get(opts, "driver");
2906 if (value) {
Eric Blake46f5ac22017-04-27 16:58:17 -05002907 qdict_put_str(reopen_state->options, "driver", value);
Kevin Wolfccf9dc02015-05-08 17:24:56 +02002908 }
2909
Jeff Cody3d8ce172017-04-07 16:55:30 -04002910 /* If we are to stay read-only, do not allow permission change
2911 * to r/w. Attempting to set to r/w may fail if either BDRV_O_ALLOW_RDWR is
2912 * not set, or if the BDS still has copy_on_read enabled */
2913 read_only = !(reopen_state->flags & BDRV_O_RDWR);
Kevin Wolf54a32bf2017-08-03 17:02:58 +02002914 ret = bdrv_can_set_read_only(reopen_state->bs, read_only, true, &local_err);
Jeff Cody3d8ce172017-04-07 16:55:30 -04002915 if (local_err) {
2916 error_propagate(errp, local_err);
Jeff Codye971aa12012-09-20 15:13:19 -04002917 goto error;
2918 }
2919
2920
2921 ret = bdrv_flush(reopen_state->bs);
2922 if (ret) {
Eric Blake455b0fd2015-11-10 23:51:20 -07002923 error_setg_errno(errp, -ret, "Error flushing drive");
Jeff Codye971aa12012-09-20 15:13:19 -04002924 goto error;
2925 }
2926
2927 if (drv->bdrv_reopen_prepare) {
2928 ret = drv->bdrv_reopen_prepare(reopen_state, queue, &local_err);
2929 if (ret) {
2930 if (local_err != NULL) {
2931 error_propagate(errp, local_err);
2932 } else {
Luiz Capitulinod8b68952013-06-10 11:29:27 -04002933 error_setg(errp, "failed while preparing to reopen image '%s'",
2934 reopen_state->bs->filename);
Jeff Codye971aa12012-09-20 15:13:19 -04002935 }
2936 goto error;
2937 }
2938 } else {
2939 /* It is currently mandatory to have a bdrv_reopen_prepare()
2940 * handler for each supported drv. */
Alberto Garcia81e5f782015-04-08 12:29:19 +03002941 error_setg(errp, "Block format '%s' used by node '%s' "
2942 "does not support reopening files", drv->format_name,
2943 bdrv_get_device_or_node_name(reopen_state->bs));
Jeff Codye971aa12012-09-20 15:13:19 -04002944 ret = -1;
2945 goto error;
2946 }
2947
Kevin Wolf4d2cb092015-04-10 17:50:50 +02002948 /* Options that are not handled are only okay if they are unchanged
2949 * compared to the old state. It is expected that some options are only
2950 * used for the initial open, but not reopen (e.g. filename) */
2951 if (qdict_size(reopen_state->options)) {
2952 const QDictEntry *entry = qdict_first(reopen_state->options);
2953
2954 do {
2955 QString *new_obj = qobject_to_qstring(entry->value);
2956 const char *new = qstring_get_str(new_obj);
Markus Armbruster129c7d12017-03-30 19:43:12 +02002957 /*
2958 * Caution: while qdict_get_try_str() is fine, getting
2959 * non-string types would require more care. When
2960 * bs->options come from -blockdev or blockdev_add, its
2961 * members are typed according to the QAPI schema, but
2962 * when they come from -drive, they're all QString.
2963 */
Kevin Wolf4d2cb092015-04-10 17:50:50 +02002964 const char *old = qdict_get_try_str(reopen_state->bs->options,
2965 entry->key);
2966
2967 if (!old || strcmp(new, old)) {
2968 error_setg(errp, "Cannot change the option '%s'", entry->key);
2969 ret = -EINVAL;
2970 goto error;
2971 }
2972 } while ((entry = qdict_next(reopen_state->options, entry)));
2973 }
2974
Jeff Codye971aa12012-09-20 15:13:19 -04002975 ret = 0;
2976
2977error:
Kevin Wolfccf9dc02015-05-08 17:24:56 +02002978 qemu_opts_del(opts);
Jeff Codye971aa12012-09-20 15:13:19 -04002979 return ret;
2980}
2981
2982/*
2983 * Takes the staged changes for the reopen from bdrv_reopen_prepare(), and
2984 * makes them final by swapping the staging BlockDriverState contents into
2985 * the active BlockDriverState contents.
2986 */
2987void bdrv_reopen_commit(BDRVReopenState *reopen_state)
2988{
2989 BlockDriver *drv;
Vladimir Sementsov-Ogievskiy50bf65b2017-06-28 15:05:12 +03002990 BlockDriverState *bs;
Vladimir Sementsov-Ogievskiycb9ff6c2017-06-28 15:05:13 +03002991 bool old_can_write, new_can_write;
Jeff Codye971aa12012-09-20 15:13:19 -04002992
2993 assert(reopen_state != NULL);
Vladimir Sementsov-Ogievskiy50bf65b2017-06-28 15:05:12 +03002994 bs = reopen_state->bs;
2995 drv = bs->drv;
Jeff Codye971aa12012-09-20 15:13:19 -04002996 assert(drv != NULL);
2997
Vladimir Sementsov-Ogievskiycb9ff6c2017-06-28 15:05:13 +03002998 old_can_write =
2999 !bdrv_is_read_only(bs) && !(bdrv_get_flags(bs) & BDRV_O_INACTIVE);
3000
Jeff Codye971aa12012-09-20 15:13:19 -04003001 /* If there are any driver level actions to take */
3002 if (drv->bdrv_reopen_commit) {
3003 drv->bdrv_reopen_commit(reopen_state);
3004 }
3005
3006 /* set BDS specific flags now */
Vladimir Sementsov-Ogievskiy50bf65b2017-06-28 15:05:12 +03003007 QDECREF(bs->explicit_options);
Kevin Wolf145f5982015-05-08 16:15:03 +02003008
Vladimir Sementsov-Ogievskiy50bf65b2017-06-28 15:05:12 +03003009 bs->explicit_options = reopen_state->explicit_options;
3010 bs->open_flags = reopen_state->flags;
3011 bs->read_only = !(reopen_state->flags & BDRV_O_RDWR);
Kevin Wolf355ef4a2013-12-11 20:14:09 +01003012
Vladimir Sementsov-Ogievskiy50bf65b2017-06-28 15:05:12 +03003013 bdrv_refresh_limits(bs, NULL);
Vladimir Sementsov-Ogievskiycb9ff6c2017-06-28 15:05:13 +03003014
3015 new_can_write =
3016 !bdrv_is_read_only(bs) && !(bdrv_get_flags(bs) & BDRV_O_INACTIVE);
3017 if (!old_can_write && new_can_write && drv->bdrv_reopen_bitmaps_rw) {
3018 Error *local_err = NULL;
3019 if (drv->bdrv_reopen_bitmaps_rw(bs, &local_err) < 0) {
3020 /* This is not fatal, bitmaps just left read-only, so all following
3021 * writes will fail. User can remove read-only bitmaps to unblock
3022 * writes.
3023 */
3024 error_reportf_err(local_err,
3025 "%s: Failed to make dirty bitmaps writable: ",
3026 bdrv_get_node_name(bs));
3027 }
3028 }
Jeff Codye971aa12012-09-20 15:13:19 -04003029}
3030
3031/*
3032 * Abort the reopen, and delete and free the staged changes in
3033 * reopen_state
3034 */
3035void bdrv_reopen_abort(BDRVReopenState *reopen_state)
3036{
3037 BlockDriver *drv;
3038
3039 assert(reopen_state != NULL);
3040 drv = reopen_state->bs->drv;
3041 assert(drv != NULL);
3042
3043 if (drv->bdrv_reopen_abort) {
3044 drv->bdrv_reopen_abort(reopen_state);
3045 }
Kevin Wolf145f5982015-05-08 16:15:03 +02003046
3047 QDECREF(reopen_state->explicit_options);
Jeff Codye971aa12012-09-20 15:13:19 -04003048}
3049
3050
Max Reitz64dff522016-01-29 16:36:10 +01003051static void bdrv_close(BlockDriverState *bs)
bellardfc01f7e2003-06-30 10:03:06 +00003052{
Max Reitz33384422014-06-20 21:57:33 +02003053 BdrvAioNotifier *ban, *ban_next;
3054
Max Reitzca9bd242016-01-29 16:36:14 +01003055 assert(!bs->job);
Max Reitz30f55fb2016-05-17 16:41:32 +02003056 assert(!bs->refcnt);
Alberto Garcia99b7e772015-09-25 16:41:44 +03003057
Paolo Bonzinifc272912015-12-23 11:48:24 +01003058 bdrv_drained_begin(bs); /* complete I/O */
Stefan Hajnoczi58fda172013-07-02 15:36:25 +02003059 bdrv_flush(bs);
Fam Zheng53ec73e2015-05-29 18:53:14 +08003060 bdrv_drain(bs); /* in case flush left pending I/O */
Paolo Bonzinifc272912015-12-23 11:48:24 +01003061
Paolo Bonzini3cbc0022012-10-19 11:36:48 +02003062 if (bs->drv) {
Kevin Wolf6e93e7c2015-04-08 13:49:41 +02003063 BdrvChild *child, *next;
3064
Kevin Wolf9a7dedb2015-06-16 10:58:20 +02003065 bs->drv->bdrv_close(bs);
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003066 bs->drv = NULL;
Kevin Wolf9a7dedb2015-06-16 10:58:20 +02003067
Kevin Wolf12fa4af2017-02-17 20:42:32 +01003068 bdrv_set_backing_hd(bs, NULL, &error_abort);
Kevin Wolf9a7dedb2015-06-16 10:58:20 +02003069
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003070 if (bs->file != NULL) {
3071 bdrv_unref_child(bs, bs->file);
3072 bs->file = NULL;
3073 }
3074
Kevin Wolf6e93e7c2015-04-08 13:49:41 +02003075 QLIST_FOREACH_SAFE(child, &bs->children, next, next) {
Kevin Wolf33a60402015-06-15 13:51:04 +02003076 /* TODO Remove bdrv_unref() from drivers' close function and use
3077 * bdrv_unref_child() here */
Kevin Wolfbddcec32015-04-09 18:47:50 +02003078 if (child->bs->inherits_from == bs) {
3079 child->bs->inherits_from = NULL;
3080 }
Kevin Wolf33a60402015-06-15 13:51:04 +02003081 bdrv_detach_child(child);
Kevin Wolf6e93e7c2015-04-08 13:49:41 +02003082 }
3083
Anthony Liguori7267c092011-08-20 22:09:37 -05003084 g_free(bs->opaque);
bellardea2384d2004-08-01 21:59:26 +00003085 bs->opaque = NULL;
Paolo Bonzinid3faa132017-06-05 14:38:50 +02003086 atomic_set(&bs->copy_on_read, 0);
Paolo Bonzinia275fa42012-05-08 16:51:43 +02003087 bs->backing_file[0] = '\0';
3088 bs->backing_format[0] = '\0';
Paolo Bonzini64058752012-05-08 16:51:49 +02003089 bs->total_sectors = 0;
Eric Blake54115412016-06-23 16:37:26 -06003090 bs->encrypted = false;
Eric Blake54115412016-06-23 16:37:26 -06003091 bs->sg = false;
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01003092 QDECREF(bs->options);
Kevin Wolf145f5982015-05-08 16:15:03 +02003093 QDECREF(bs->explicit_options);
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01003094 bs->options = NULL;
Manos Pitsidianakis998cbd62017-07-14 17:35:47 +03003095 bs->explicit_options = NULL;
Max Reitz91af7012014-07-18 20:24:56 +02003096 QDECREF(bs->full_open_options);
3097 bs->full_open_options = NULL;
bellardb3380822004-03-14 21:38:54 +00003098 }
Zhi Yong Wu98f90db2011-11-08 13:00:14 +08003099
Vladimir Sementsov-Ogievskiycca43ae2017-06-28 15:05:16 +03003100 bdrv_release_named_dirty_bitmaps(bs);
3101 assert(QLIST_EMPTY(&bs->dirty_bitmaps));
3102
Max Reitz33384422014-06-20 21:57:33 +02003103 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) {
3104 g_free(ban);
3105 }
3106 QLIST_INIT(&bs->aio_notifiers);
Paolo Bonzinifc272912015-12-23 11:48:24 +01003107 bdrv_drained_end(bs);
bellardb3380822004-03-14 21:38:54 +00003108}
3109
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +09003110void bdrv_close_all(void)
3111{
Kevin Wolfa1a2af02016-04-08 18:26:37 +02003112 block_job_cancel_sync_all();
Kevin Wolfcd7fca92016-07-06 11:22:39 +02003113 nbd_export_close_all();
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +09003114
Max Reitzca9bd242016-01-29 16:36:14 +01003115 /* Drop references from requests still in flight, such as canceled block
3116 * jobs whose AIO context has not been polled yet */
3117 bdrv_drain_all();
Stefan Hajnoczied78cda2014-05-08 16:34:35 +02003118
Max Reitzca9bd242016-01-29 16:36:14 +01003119 blk_remove_all_bs();
3120 blockdev_close_all_bdrv_states();
3121
Kevin Wolfa1a2af02016-04-08 18:26:37 +02003122 assert(QTAILQ_EMPTY(&all_bdrv_states));
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +09003123}
3124
Kevin Wolfd0ac0382017-03-01 17:30:41 +01003125static bool should_update_child(BdrvChild *c, BlockDriverState *to)
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02003126{
Kevin Wolfd0ac0382017-03-01 17:30:41 +01003127 BdrvChild *to_c;
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02003128
Kevin Wolfd0ac0382017-03-01 17:30:41 +01003129 if (c->role->stay_at_node) {
3130 return false;
3131 }
3132
3133 if (c->role == &child_backing) {
3134 /* If @from is a backing file of @to, ignore the child to avoid
3135 * creating a loop. We only want to change the pointer of other
3136 * parents. */
3137 QLIST_FOREACH(to_c, &to->children, next) {
3138 if (to_c == c) {
3139 break;
3140 }
3141 }
3142 if (to_c) {
3143 return false;
3144 }
3145 }
3146
3147 return true;
3148}
3149
Kevin Wolf5fe31c22017-03-06 16:20:51 +01003150void bdrv_replace_node(BlockDriverState *from, BlockDriverState *to,
3151 Error **errp)
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02003152{
Kevin Wolfd0ac0382017-03-01 17:30:41 +01003153 BdrvChild *c, *next;
Kevin Wolf234ac1a2017-03-02 18:43:00 +01003154 GSList *list = NULL, *p;
3155 uint64_t old_perm, old_shared;
3156 uint64_t perm = 0, shared = BLK_PERM_ALL;
3157 int ret;
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02003158
Kevin Wolf5fe31c22017-03-06 16:20:51 +01003159 assert(!atomic_read(&from->in_flight));
3160 assert(!atomic_read(&to->in_flight));
3161
Kevin Wolf234ac1a2017-03-02 18:43:00 +01003162 /* Make sure that @from doesn't go away until we have successfully attached
3163 * all of its parents to @to. */
3164 bdrv_ref(from);
3165
3166 /* Put all parents into @list and calculate their cumulative permissions */
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02003167 QLIST_FOREACH_SAFE(c, &from->parents, next_parent, next) {
Kevin Wolfd0ac0382017-03-01 17:30:41 +01003168 if (!should_update_child(c, to)) {
Kevin Wolf26de9432017-01-17 13:39:34 +01003169 continue;
3170 }
Kevin Wolf234ac1a2017-03-02 18:43:00 +01003171 list = g_slist_prepend(list, c);
3172 perm |= c->perm;
3173 shared &= c->shared_perm;
3174 }
3175
3176 /* Check whether the required permissions can be granted on @to, ignoring
3177 * all BdrvChild in @list so that they can't block themselves. */
3178 ret = bdrv_check_update_perm(to, perm, shared, list, errp);
3179 if (ret < 0) {
3180 bdrv_abort_perm_update(to);
3181 goto out;
3182 }
3183
3184 /* Now actually perform the change. We performed the permission check for
3185 * all elements of @list at once, so set the permissions all at once at the
3186 * very end. */
3187 for (p = list; p != NULL; p = p->next) {
3188 c = p->data;
Max Reitz9bd910e2016-06-10 20:57:46 +02003189
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02003190 bdrv_ref(to);
Kevin Wolf234ac1a2017-03-02 18:43:00 +01003191 bdrv_replace_child_noperm(c, to);
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02003192 bdrv_unref(from);
3193 }
Kevin Wolf234ac1a2017-03-02 18:43:00 +01003194
3195 bdrv_get_cumulative_perm(to, &old_perm, &old_shared);
3196 bdrv_set_perm(to, old_perm | perm, old_shared | shared);
3197
3198out:
3199 g_slist_free(list);
3200 bdrv_unref(from);
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02003201}
3202
Jeff Cody8802d1f2012-02-28 15:54:06 -05003203/*
3204 * Add new bs contents at the top of an image chain while the chain is
3205 * live, while keeping required fields on the top layer.
3206 *
3207 * This will modify the BlockDriverState fields, and swap contents
3208 * between bs_new and bs_top. Both bs_new and bs_top are modified.
3209 *
Markus Armbrusterbfb197e2014-10-07 13:59:11 +02003210 * bs_new must not be attached to a BlockBackend.
Jeff Codyf6801b82012-03-27 16:30:19 -04003211 *
Jeff Cody8802d1f2012-02-28 15:54:06 -05003212 * This function does not create any image files.
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02003213 *
3214 * bdrv_append() takes ownership of a bs_new reference and unrefs it because
3215 * that's what the callers commonly need. bs_new will be referenced by the old
3216 * parents of bs_top after bdrv_append() returns. If the caller needs to keep a
3217 * reference of its own, it must call bdrv_ref().
Jeff Cody8802d1f2012-02-28 15:54:06 -05003218 */
Kevin Wolfb2c28322017-02-20 12:46:42 +01003219void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top,
3220 Error **errp)
Jeff Cody8802d1f2012-02-28 15:54:06 -05003221{
Kevin Wolfb2c28322017-02-20 12:46:42 +01003222 Error *local_err = NULL;
3223
Kevin Wolfb2c28322017-02-20 12:46:42 +01003224 bdrv_set_backing_hd(bs_new, bs_top, &local_err);
3225 if (local_err) {
3226 error_propagate(errp, local_err);
3227 goto out;
3228 }
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02003229
Kevin Wolf5fe31c22017-03-06 16:20:51 +01003230 bdrv_replace_node(bs_top, bs_new, &local_err);
Kevin Wolf234ac1a2017-03-02 18:43:00 +01003231 if (local_err) {
3232 error_propagate(errp, local_err);
3233 bdrv_set_backing_hd(bs_new, NULL, &error_abort);
3234 goto out;
3235 }
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02003236
3237 /* bs_new is now referenced by its new parents, we don't need the
3238 * additional reference any more. */
Kevin Wolfb2c28322017-02-20 12:46:42 +01003239out:
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02003240 bdrv_unref(bs_new);
Jeff Cody8802d1f2012-02-28 15:54:06 -05003241}
3242
Fam Zheng4f6fd342013-08-23 09:14:47 +08003243static void bdrv_delete(BlockDriverState *bs)
bellardb3380822004-03-14 21:38:54 +00003244{
Paolo Bonzini3e914652012-03-30 13:17:11 +02003245 assert(!bs->job);
Fam Zheng3718d8a2014-05-23 21:29:43 +08003246 assert(bdrv_op_blocker_is_empty(bs));
Fam Zheng4f6fd342013-08-23 09:14:47 +08003247 assert(!bs->refcnt);
Markus Armbruster18846de2010-06-29 16:58:30 +02003248
Stefan Hajnoczie1b5c522013-06-27 15:32:26 +02003249 bdrv_close(bs);
3250
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01003251 /* remove from list, if necessary */
Kevin Wolf63eaaae2016-03-18 10:46:57 +01003252 if (bs->node_name[0] != '\0') {
3253 QTAILQ_REMOVE(&graph_bdrv_states, bs, node_list);
3254 }
Max Reitz2c1d04e2016-01-29 16:36:11 +01003255 QTAILQ_REMOVE(&all_bdrv_states, bs, bs_list);
3256
Anthony Liguori7267c092011-08-20 22:09:37 -05003257 g_free(bs);
bellardfc01f7e2003-06-30 10:03:06 +00003258}
3259
aliguorie97fc192009-04-21 23:11:50 +00003260/*
3261 * Run consistency checks on an image
3262 *
Kevin Wolfe076f332010-06-29 11:43:13 +02003263 * Returns 0 if the check could be completed (it doesn't mean that the image is
Stefan Weila1c72732011-04-28 17:20:38 +02003264 * free of errors) or -errno when an internal error occurred. The results of the
Kevin Wolfe076f332010-06-29 11:43:13 +02003265 * check are stored in res.
aliguorie97fc192009-04-21 23:11:50 +00003266 */
Kevin Wolf4534ff52012-05-11 16:07:02 +02003267int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix)
aliguorie97fc192009-04-21 23:11:50 +00003268{
Max Reitz908bcd52014-08-07 22:47:55 +02003269 if (bs->drv == NULL) {
3270 return -ENOMEDIUM;
3271 }
aliguorie97fc192009-04-21 23:11:50 +00003272 if (bs->drv->bdrv_check == NULL) {
3273 return -ENOTSUP;
3274 }
3275
Kevin Wolfe076f332010-06-29 11:43:13 +02003276 memset(res, 0, sizeof(*res));
Kevin Wolf4534ff52012-05-11 16:07:02 +02003277 return bs->drv->bdrv_check(bs, res, fix);
aliguorie97fc192009-04-21 23:11:50 +00003278}
3279
Kevin Wolf756e6732010-01-12 12:55:17 +01003280/*
3281 * Return values:
3282 * 0 - success
3283 * -EINVAL - backing format specified, but no file
3284 * -ENOSPC - can't update the backing file because no space is left in the
3285 * image file header
3286 * -ENOTSUP - format driver doesn't support changing the backing file
3287 */
3288int bdrv_change_backing_file(BlockDriverState *bs,
3289 const char *backing_file, const char *backing_fmt)
3290{
3291 BlockDriver *drv = bs->drv;
Paolo Bonzini469ef352012-04-12 14:01:02 +02003292 int ret;
Kevin Wolf756e6732010-01-12 12:55:17 +01003293
Paolo Bonzini5f377792012-04-12 14:01:01 +02003294 /* Backing file format doesn't make sense without a backing file */
3295 if (backing_fmt && !backing_file) {
3296 return -EINVAL;
3297 }
3298
Kevin Wolf756e6732010-01-12 12:55:17 +01003299 if (drv->bdrv_change_backing_file != NULL) {
Paolo Bonzini469ef352012-04-12 14:01:02 +02003300 ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
Kevin Wolf756e6732010-01-12 12:55:17 +01003301 } else {
Paolo Bonzini469ef352012-04-12 14:01:02 +02003302 ret = -ENOTSUP;
Kevin Wolf756e6732010-01-12 12:55:17 +01003303 }
Paolo Bonzini469ef352012-04-12 14:01:02 +02003304
3305 if (ret == 0) {
3306 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
3307 pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
3308 }
3309 return ret;
Kevin Wolf756e6732010-01-12 12:55:17 +01003310}
3311
Jeff Cody6ebdcee2012-09-27 13:29:12 -04003312/*
3313 * Finds the image layer in the chain that has 'bs' as its backing file.
3314 *
3315 * active is the current topmost image.
3316 *
3317 * Returns NULL if bs is not found in active's image chain,
3318 * or if active == bs.
Jeff Cody4caf0fc2014-06-25 15:35:26 -04003319 *
3320 * Returns the bottommost base image if bs == NULL.
Jeff Cody6ebdcee2012-09-27 13:29:12 -04003321 */
3322BlockDriverState *bdrv_find_overlay(BlockDriverState *active,
3323 BlockDriverState *bs)
3324{
Kevin Wolf760e0062015-06-17 14:55:21 +02003325 while (active && bs != backing_bs(active)) {
3326 active = backing_bs(active);
Jeff Cody6ebdcee2012-09-27 13:29:12 -04003327 }
3328
Jeff Cody4caf0fc2014-06-25 15:35:26 -04003329 return active;
3330}
Jeff Cody6ebdcee2012-09-27 13:29:12 -04003331
Jeff Cody4caf0fc2014-06-25 15:35:26 -04003332/* Given a BDS, searches for the base layer. */
3333BlockDriverState *bdrv_find_base(BlockDriverState *bs)
3334{
3335 return bdrv_find_overlay(bs, NULL);
Jeff Cody6ebdcee2012-09-27 13:29:12 -04003336}
3337
Jeff Cody6ebdcee2012-09-27 13:29:12 -04003338/*
3339 * Drops images above 'base' up to and including 'top', and sets the image
3340 * above 'top' to have base as its backing file.
3341 *
3342 * Requires that the overlay to 'top' is opened r/w, so that the backing file
3343 * information in 'bs' can be properly updated.
3344 *
3345 * E.g., this will convert the following chain:
3346 * bottom <- base <- intermediate <- top <- active
3347 *
3348 * to
3349 *
3350 * bottom <- base <- active
3351 *
3352 * It is allowed for bottom==base, in which case it converts:
3353 *
3354 * base <- intermediate <- top <- active
3355 *
3356 * to
3357 *
3358 * base <- active
3359 *
Jeff Cody54e26902014-06-25 15:40:10 -04003360 * If backing_file_str is non-NULL, it will be used when modifying top's
3361 * overlay image metadata.
3362 *
Jeff Cody6ebdcee2012-09-27 13:29:12 -04003363 * Error conditions:
3364 * if active == top, that is considered an error
3365 *
3366 */
3367int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top,
Jeff Cody54e26902014-06-25 15:40:10 -04003368 BlockDriverState *base, const char *backing_file_str)
Jeff Cody6ebdcee2012-09-27 13:29:12 -04003369{
Jeff Cody6ebdcee2012-09-27 13:29:12 -04003370 BlockDriverState *new_top_bs = NULL;
Kevin Wolf12fa4af2017-02-17 20:42:32 +01003371 Error *local_err = NULL;
Jeff Cody6ebdcee2012-09-27 13:29:12 -04003372 int ret = -EIO;
3373
Jeff Cody6ebdcee2012-09-27 13:29:12 -04003374 if (!top->drv || !base->drv) {
3375 goto exit;
3376 }
3377
3378 new_top_bs = bdrv_find_overlay(active, top);
3379
3380 if (new_top_bs == NULL) {
3381 /* we could not find the image above 'top', this is an error */
3382 goto exit;
3383 }
3384
Kevin Wolf760e0062015-06-17 14:55:21 +02003385 /* special case of new_top_bs->backing->bs already pointing to base - nothing
Jeff Cody6ebdcee2012-09-27 13:29:12 -04003386 * to do, no intermediate images */
Kevin Wolf760e0062015-06-17 14:55:21 +02003387 if (backing_bs(new_top_bs) == base) {
Jeff Cody6ebdcee2012-09-27 13:29:12 -04003388 ret = 0;
3389 goto exit;
3390 }
3391
Kevin Wolf5db15a52015-09-14 15:33:33 +02003392 /* Make sure that base is in the backing chain of top */
3393 if (!bdrv_chain_contains(top, base)) {
Jeff Cody6ebdcee2012-09-27 13:29:12 -04003394 goto exit;
3395 }
3396
3397 /* success - we can delete the intermediate states, and link top->base */
Kevin Wolf5db15a52015-09-14 15:33:33 +02003398 backing_file_str = backing_file_str ? backing_file_str : base->filename;
Jeff Cody54e26902014-06-25 15:40:10 -04003399 ret = bdrv_change_backing_file(new_top_bs, backing_file_str,
Kevin Wolf5db15a52015-09-14 15:33:33 +02003400 base->drv ? base->drv->format_name : "");
Jeff Cody6ebdcee2012-09-27 13:29:12 -04003401 if (ret) {
3402 goto exit;
3403 }
Kevin Wolf12fa4af2017-02-17 20:42:32 +01003404
3405 bdrv_set_backing_hd(new_top_bs, base, &local_err);
3406 if (local_err) {
3407 ret = -EPERM;
3408 error_report_err(local_err);
3409 goto exit;
3410 }
Jeff Cody6ebdcee2012-09-27 13:29:12 -04003411
Jeff Cody6ebdcee2012-09-27 13:29:12 -04003412 ret = 0;
Jeff Cody6ebdcee2012-09-27 13:29:12 -04003413exit:
Jeff Cody6ebdcee2012-09-27 13:29:12 -04003414 return ret;
3415}
3416
bellard83f64092006-08-01 16:21:11 +00003417/**
bellard83f64092006-08-01 16:21:11 +00003418 * Truncate file to 'offset' bytes (needed only for file protocols)
3419 */
Max Reitz7ea37c32017-06-13 22:20:53 +02003420int bdrv_truncate(BdrvChild *child, int64_t offset, PreallocMode prealloc,
3421 Error **errp)
bellard83f64092006-08-01 16:21:11 +00003422{
Kevin Wolf52cdbc52017-02-16 18:39:03 +01003423 BlockDriverState *bs = child->bs;
bellard83f64092006-08-01 16:21:11 +00003424 BlockDriver *drv = bs->drv;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01003425 int ret;
Kevin Wolfc8f6d582017-02-17 14:52:00 +01003426
Max Reitz362b3782017-04-11 17:52:26 +02003427 assert(child->perm & BLK_PERM_RESIZE);
Kevin Wolfc8f6d582017-02-17 14:52:00 +01003428
Max Reitzed3d2ec2017-03-28 22:51:27 +02003429 if (!drv) {
3430 error_setg(errp, "No medium inserted");
bellard19cb3732006-08-19 11:45:59 +00003431 return -ENOMEDIUM;
Max Reitzed3d2ec2017-03-28 22:51:27 +02003432 }
3433 if (!drv->bdrv_truncate) {
3434 error_setg(errp, "Image format driver does not support resize");
bellard83f64092006-08-01 16:21:11 +00003435 return -ENOTSUP;
Max Reitzed3d2ec2017-03-28 22:51:27 +02003436 }
3437 if (bs->read_only) {
3438 error_setg(errp, "Image is read-only");
Naphtali Sprei59f26892009-10-26 16:25:16 +02003439 return -EACCES;
Max Reitzed3d2ec2017-03-28 22:51:27 +02003440 }
Jeff Cody9c75e162014-06-25 16:55:30 -04003441
Denis V. Lunev504c2052017-04-05 18:18:25 +03003442 assert(!(bs->open_flags & BDRV_O_INACTIVE));
3443
Max Reitz7ea37c32017-06-13 22:20:53 +02003444 ret = drv->bdrv_truncate(bs, offset, prealloc, errp);
Stefan Hajnoczi51762282010-04-19 16:56:41 +01003445 if (ret == 0) {
3446 ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
John Snowce1ffea2015-04-17 19:50:03 -04003447 bdrv_dirty_bitmap_truncate(bs);
Kevin Wolf5c8cab42016-02-24 15:13:35 +01003448 bdrv_parent_cb_resize(bs);
Paolo Bonzini47fec592017-06-05 14:39:01 +02003449 atomic_inc(&bs->write_gen);
Stefan Hajnoczi51762282010-04-19 16:56:41 +01003450 }
3451 return ret;
bellard83f64092006-08-01 16:21:11 +00003452}
3453
3454/**
Fam Zheng4a1d5e12011-07-12 19:56:39 +08003455 * Length of a allocated file in bytes. Sparse files are counted by actual
3456 * allocated space. Return < 0 if error or unknown.
3457 */
3458int64_t bdrv_get_allocated_file_size(BlockDriverState *bs)
3459{
3460 BlockDriver *drv = bs->drv;
3461 if (!drv) {
3462 return -ENOMEDIUM;
3463 }
3464 if (drv->bdrv_get_allocated_file_size) {
3465 return drv->bdrv_get_allocated_file_size(bs);
3466 }
3467 if (bs->file) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003468 return bdrv_get_allocated_file_size(bs->file->bs);
Fam Zheng4a1d5e12011-07-12 19:56:39 +08003469 }
3470 return -ENOTSUP;
3471}
3472
Stefan Hajnoczi90880ff2017-07-05 13:57:30 +01003473/*
3474 * bdrv_measure:
3475 * @drv: Format driver
3476 * @opts: Creation options for new image
3477 * @in_bs: Existing image containing data for new image (may be NULL)
3478 * @errp: Error object
3479 * Returns: A #BlockMeasureInfo (free using qapi_free_BlockMeasureInfo())
3480 * or NULL on error
3481 *
3482 * Calculate file size required to create a new image.
3483 *
3484 * If @in_bs is given then space for allocated clusters and zero clusters
3485 * from that image are included in the calculation. If @opts contains a
3486 * backing file that is shared by @in_bs then backing clusters may be omitted
3487 * from the calculation.
3488 *
3489 * If @in_bs is NULL then the calculation includes no allocated clusters
3490 * unless a preallocation option is given in @opts.
3491 *
3492 * Note that @in_bs may use a different BlockDriver from @drv.
3493 *
3494 * If an error occurs the @errp pointer is set.
3495 */
3496BlockMeasureInfo *bdrv_measure(BlockDriver *drv, QemuOpts *opts,
3497 BlockDriverState *in_bs, Error **errp)
3498{
3499 if (!drv->bdrv_measure) {
3500 error_setg(errp, "Block driver '%s' does not support size measurement",
3501 drv->format_name);
3502 return NULL;
3503 }
3504
3505 return drv->bdrv_measure(opts, in_bs, errp);
3506}
3507
Fam Zheng4a1d5e12011-07-12 19:56:39 +08003508/**
Markus Armbruster65a9bb22014-06-26 13:23:17 +02003509 * Return number of sectors on success, -errno on error.
bellard83f64092006-08-01 16:21:11 +00003510 */
Markus Armbruster65a9bb22014-06-26 13:23:17 +02003511int64_t bdrv_nb_sectors(BlockDriverState *bs)
bellard83f64092006-08-01 16:21:11 +00003512{
3513 BlockDriver *drv = bs->drv;
Markus Armbruster65a9bb22014-06-26 13:23:17 +02003514
bellard83f64092006-08-01 16:21:11 +00003515 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00003516 return -ENOMEDIUM;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01003517
Kevin Wolfb94a2612013-10-29 12:18:58 +01003518 if (drv->has_variable_length) {
3519 int ret = refresh_total_sectors(bs, bs->total_sectors);
3520 if (ret < 0) {
3521 return ret;
Stefan Hajnoczi46a4e4e2011-03-29 20:04:41 +01003522 }
bellard83f64092006-08-01 16:21:11 +00003523 }
Markus Armbruster65a9bb22014-06-26 13:23:17 +02003524 return bs->total_sectors;
3525}
3526
3527/**
3528 * Return length in bytes on success, -errno on error.
3529 * The length is always a multiple of BDRV_SECTOR_SIZE.
3530 */
3531int64_t bdrv_getlength(BlockDriverState *bs)
3532{
3533 int64_t ret = bdrv_nb_sectors(bs);
3534
Fam Zheng4a9c9ea2015-05-15 16:36:05 +08003535 ret = ret > INT64_MAX / BDRV_SECTOR_SIZE ? -EFBIG : ret;
Markus Armbruster65a9bb22014-06-26 13:23:17 +02003536 return ret < 0 ? ret : ret * BDRV_SECTOR_SIZE;
bellardfc01f7e2003-06-30 10:03:06 +00003537}
3538
bellard19cb3732006-08-19 11:45:59 +00003539/* return 0 as number of sectors if no device present or error */
ths96b8f132007-12-17 01:35:20 +00003540void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
bellardfc01f7e2003-06-30 10:03:06 +00003541{
Markus Armbruster65a9bb22014-06-26 13:23:17 +02003542 int64_t nb_sectors = bdrv_nb_sectors(bs);
3543
3544 *nb_sectors_ptr = nb_sectors < 0 ? 0 : nb_sectors;
bellardfc01f7e2003-06-30 10:03:06 +00003545}
bellardcf989512004-02-16 21:56:36 +00003546
Eric Blake54115412016-06-23 16:37:26 -06003547bool bdrv_is_sg(BlockDriverState *bs)
ths985a03b2007-12-24 16:10:43 +00003548{
3549 return bs->sg;
3550}
3551
Eric Blake54115412016-06-23 16:37:26 -06003552bool bdrv_is_encrypted(BlockDriverState *bs)
bellardea2384d2004-08-01 21:59:26 +00003553{
Kevin Wolf760e0062015-06-17 14:55:21 +02003554 if (bs->backing && bs->backing->bs->encrypted) {
Eric Blake54115412016-06-23 16:37:26 -06003555 return true;
Kevin Wolf760e0062015-06-17 14:55:21 +02003556 }
bellardea2384d2004-08-01 21:59:26 +00003557 return bs->encrypted;
3558}
3559
Markus Armbrusterf8d6bba2012-06-13 10:11:48 +02003560const char *bdrv_get_format_name(BlockDriverState *bs)
bellardea2384d2004-08-01 21:59:26 +00003561{
Markus Armbrusterf8d6bba2012-06-13 10:11:48 +02003562 return bs->drv ? bs->drv->format_name : NULL;
bellardea2384d2004-08-01 21:59:26 +00003563}
3564
Stefan Hajnocziada42402014-08-27 12:08:55 +01003565static int qsort_strcmp(const void *a, const void *b)
3566{
Max Reitzceff5bd2016-10-12 22:49:05 +02003567 return strcmp(*(char *const *)a, *(char *const *)b);
Stefan Hajnocziada42402014-08-27 12:08:55 +01003568}
3569
ths5fafdf22007-09-16 21:08:06 +00003570void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
bellardea2384d2004-08-01 21:59:26 +00003571 void *opaque)
3572{
3573 BlockDriver *drv;
Jeff Codye855e4f2014-04-28 18:29:54 -04003574 int count = 0;
Stefan Hajnocziada42402014-08-27 12:08:55 +01003575 int i;
Jeff Codye855e4f2014-04-28 18:29:54 -04003576 const char **formats = NULL;
bellardea2384d2004-08-01 21:59:26 +00003577
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +01003578 QLIST_FOREACH(drv, &bdrv_drivers, list) {
Jeff Codye855e4f2014-04-28 18:29:54 -04003579 if (drv->format_name) {
3580 bool found = false;
3581 int i = count;
3582 while (formats && i && !found) {
3583 found = !strcmp(formats[--i], drv->format_name);
3584 }
3585
3586 if (!found) {
Markus Armbruster5839e532014-08-19 10:31:08 +02003587 formats = g_renew(const char *, formats, count + 1);
Jeff Codye855e4f2014-04-28 18:29:54 -04003588 formats[count++] = drv->format_name;
Jeff Codye855e4f2014-04-28 18:29:54 -04003589 }
3590 }
bellardea2384d2004-08-01 21:59:26 +00003591 }
Stefan Hajnocziada42402014-08-27 12:08:55 +01003592
Max Reitzeb0df692016-10-12 22:49:06 +02003593 for (i = 0; i < (int)ARRAY_SIZE(block_driver_modules); i++) {
3594 const char *format_name = block_driver_modules[i].format_name;
3595
3596 if (format_name) {
3597 bool found = false;
3598 int j = count;
3599
3600 while (formats && j && !found) {
3601 found = !strcmp(formats[--j], format_name);
3602 }
3603
3604 if (!found) {
3605 formats = g_renew(const char *, formats, count + 1);
3606 formats[count++] = format_name;
3607 }
3608 }
3609 }
3610
Stefan Hajnocziada42402014-08-27 12:08:55 +01003611 qsort(formats, count, sizeof(formats[0]), qsort_strcmp);
3612
3613 for (i = 0; i < count; i++) {
3614 it(opaque, formats[i]);
3615 }
3616
Jeff Codye855e4f2014-04-28 18:29:54 -04003617 g_free(formats);
bellardea2384d2004-08-01 21:59:26 +00003618}
3619
Benoît Canetdc364f42014-01-23 21:31:32 +01003620/* This function is to find a node in the bs graph */
3621BlockDriverState *bdrv_find_node(const char *node_name)
3622{
3623 BlockDriverState *bs;
3624
3625 assert(node_name);
3626
3627 QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) {
3628 if (!strcmp(node_name, bs->node_name)) {
3629 return bs;
3630 }
3631 }
3632 return NULL;
3633}
3634
Benoît Canetc13163f2014-01-23 21:31:34 +01003635/* Put this QMP function here so it can access the static graph_bdrv_states. */
Alberto Garciad5a8ee62015-04-17 14:52:43 +03003636BlockDeviceInfoList *bdrv_named_nodes_list(Error **errp)
Benoît Canetc13163f2014-01-23 21:31:34 +01003637{
3638 BlockDeviceInfoList *list, *entry;
3639 BlockDriverState *bs;
3640
3641 list = NULL;
3642 QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) {
Kevin Wolfc83f9fb2016-03-03 11:37:48 +01003643 BlockDeviceInfo *info = bdrv_block_device_info(NULL, bs, errp);
Alberto Garciad5a8ee62015-04-17 14:52:43 +03003644 if (!info) {
3645 qapi_free_BlockDeviceInfoList(list);
3646 return NULL;
3647 }
Benoît Canetc13163f2014-01-23 21:31:34 +01003648 entry = g_malloc0(sizeof(*entry));
Alberto Garciad5a8ee62015-04-17 14:52:43 +03003649 entry->value = info;
Benoît Canetc13163f2014-01-23 21:31:34 +01003650 entry->next = list;
3651 list = entry;
3652 }
3653
3654 return list;
3655}
3656
Benoît Canet12d3ba82014-01-23 21:31:35 +01003657BlockDriverState *bdrv_lookup_bs(const char *device,
3658 const char *node_name,
3659 Error **errp)
3660{
Markus Armbruster7f06d472014-10-07 13:59:12 +02003661 BlockBackend *blk;
3662 BlockDriverState *bs;
Benoît Canet12d3ba82014-01-23 21:31:35 +01003663
Benoît Canet12d3ba82014-01-23 21:31:35 +01003664 if (device) {
Markus Armbruster7f06d472014-10-07 13:59:12 +02003665 blk = blk_by_name(device);
Benoît Canet12d3ba82014-01-23 21:31:35 +01003666
Markus Armbruster7f06d472014-10-07 13:59:12 +02003667 if (blk) {
Alberto Garcia9f4ed6f2015-10-26 16:46:49 +02003668 bs = blk_bs(blk);
3669 if (!bs) {
Max Reitz5433c242015-10-19 17:53:29 +02003670 error_setg(errp, "Device '%s' has no medium", device);
Max Reitz5433c242015-10-19 17:53:29 +02003671 }
3672
Alberto Garcia9f4ed6f2015-10-26 16:46:49 +02003673 return bs;
Benoît Canet12d3ba82014-01-23 21:31:35 +01003674 }
Benoît Canet12d3ba82014-01-23 21:31:35 +01003675 }
3676
Benoît Canetdd67fa52014-02-12 17:15:06 +01003677 if (node_name) {
3678 bs = bdrv_find_node(node_name);
Benoît Canet12d3ba82014-01-23 21:31:35 +01003679
Benoît Canetdd67fa52014-02-12 17:15:06 +01003680 if (bs) {
3681 return bs;
3682 }
Benoît Canet12d3ba82014-01-23 21:31:35 +01003683 }
3684
Benoît Canetdd67fa52014-02-12 17:15:06 +01003685 error_setg(errp, "Cannot find device=%s nor node_name=%s",
3686 device ? device : "",
3687 node_name ? node_name : "");
3688 return NULL;
Benoît Canet12d3ba82014-01-23 21:31:35 +01003689}
3690
Jeff Cody5a6684d2014-06-25 15:40:09 -04003691/* If 'base' is in the same chain as 'top', return true. Otherwise,
3692 * return false. If either argument is NULL, return false. */
3693bool bdrv_chain_contains(BlockDriverState *top, BlockDriverState *base)
3694{
3695 while (top && top != base) {
Kevin Wolf760e0062015-06-17 14:55:21 +02003696 top = backing_bs(top);
Jeff Cody5a6684d2014-06-25 15:40:09 -04003697 }
3698
3699 return top != NULL;
3700}
3701
Fam Zheng04df7652014-10-31 11:32:54 +08003702BlockDriverState *bdrv_next_node(BlockDriverState *bs)
3703{
3704 if (!bs) {
3705 return QTAILQ_FIRST(&graph_bdrv_states);
3706 }
3707 return QTAILQ_NEXT(bs, node_list);
3708}
3709
Fam Zheng20a9e772014-10-31 11:32:55 +08003710const char *bdrv_get_node_name(const BlockDriverState *bs)
3711{
3712 return bs->node_name;
3713}
3714
Kevin Wolf1f0c4612016-03-22 18:38:44 +01003715const char *bdrv_get_parent_name(const BlockDriverState *bs)
Kevin Wolf4c265bf2016-02-26 10:22:16 +01003716{
3717 BdrvChild *c;
3718 const char *name;
3719
3720 /* If multiple parents have a name, just pick the first one. */
3721 QLIST_FOREACH(c, &bs->parents, next_parent) {
3722 if (c->role->get_name) {
3723 name = c->role->get_name(c);
3724 if (name && *name) {
3725 return name;
3726 }
3727 }
3728 }
3729
3730 return NULL;
3731}
3732
Markus Armbruster7f06d472014-10-07 13:59:12 +02003733/* TODO check what callers really want: bs->node_name or blk_name() */
Markus Armbrusterbfb197e2014-10-07 13:59:11 +02003734const char *bdrv_get_device_name(const BlockDriverState *bs)
bellardea2384d2004-08-01 21:59:26 +00003735{
Kevin Wolf4c265bf2016-02-26 10:22:16 +01003736 return bdrv_get_parent_name(bs) ?: "";
bellardea2384d2004-08-01 21:59:26 +00003737}
3738
Alberto Garcia9b2aa842015-04-08 12:29:18 +03003739/* This can be used to identify nodes that might not have a device
3740 * name associated. Since node and device names live in the same
3741 * namespace, the result is unambiguous. The exception is if both are
3742 * absent, then this returns an empty (non-null) string. */
3743const char *bdrv_get_device_or_node_name(const BlockDriverState *bs)
3744{
Kevin Wolf4c265bf2016-02-26 10:22:16 +01003745 return bdrv_get_parent_name(bs) ?: bs->node_name;
Alberto Garcia9b2aa842015-04-08 12:29:18 +03003746}
3747
Markus Armbrusterc8433282012-06-05 16:49:24 +02003748int bdrv_get_flags(BlockDriverState *bs)
3749{
3750 return bs->open_flags;
3751}
3752
Peter Lieven3ac21622013-06-28 12:47:42 +02003753int bdrv_has_zero_init_1(BlockDriverState *bs)
3754{
3755 return 1;
3756}
3757
Kevin Wolff2feebb2010-04-14 17:30:35 +02003758int bdrv_has_zero_init(BlockDriverState *bs)
3759{
3760 assert(bs->drv);
3761
Paolo Bonzini11212d82013-09-04 19:00:27 +02003762 /* If BS is a copy on write image, it is initialized to
3763 the contents of the base image, which may not be zeroes. */
Kevin Wolf760e0062015-06-17 14:55:21 +02003764 if (bs->backing) {
Paolo Bonzini11212d82013-09-04 19:00:27 +02003765 return 0;
3766 }
Kevin Wolf336c1c12010-07-28 11:26:29 +02003767 if (bs->drv->bdrv_has_zero_init) {
3768 return bs->drv->bdrv_has_zero_init(bs);
Kevin Wolff2feebb2010-04-14 17:30:35 +02003769 }
3770
Peter Lieven3ac21622013-06-28 12:47:42 +02003771 /* safe default */
3772 return 0;
Kevin Wolff2feebb2010-04-14 17:30:35 +02003773}
3774
Peter Lieven4ce78692013-10-24 12:06:54 +02003775bool bdrv_unallocated_blocks_are_zero(BlockDriverState *bs)
3776{
3777 BlockDriverInfo bdi;
3778
Kevin Wolf760e0062015-06-17 14:55:21 +02003779 if (bs->backing) {
Peter Lieven4ce78692013-10-24 12:06:54 +02003780 return false;
3781 }
3782
3783 if (bdrv_get_info(bs, &bdi) == 0) {
3784 return bdi.unallocated_blocks_are_zero;
3785 }
3786
3787 return false;
3788}
3789
3790bool bdrv_can_write_zeroes_with_unmap(BlockDriverState *bs)
3791{
3792 BlockDriverInfo bdi;
3793
Denis V. Lunev2f0342e2016-07-14 16:33:26 +03003794 if (!(bs->open_flags & BDRV_O_UNMAP)) {
Peter Lieven4ce78692013-10-24 12:06:54 +02003795 return false;
3796 }
3797
3798 if (bdrv_get_info(bs, &bdi) == 0) {
3799 return bdi.can_write_zeroes_with_unmap;
3800 }
3801
3802 return false;
3803}
3804
aliguori045df332009-03-05 23:00:48 +00003805const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
3806{
Kevin Wolf760e0062015-06-17 14:55:21 +02003807 if (bs->backing && bs->backing->bs->encrypted)
aliguori045df332009-03-05 23:00:48 +00003808 return bs->backing_file;
3809 else if (bs->encrypted)
3810 return bs->filename;
3811 else
3812 return NULL;
3813}
3814
ths5fafdf22007-09-16 21:08:06 +00003815void bdrv_get_backing_filename(BlockDriverState *bs,
bellard83f64092006-08-01 16:21:11 +00003816 char *filename, int filename_size)
bellardea2384d2004-08-01 21:59:26 +00003817{
Kevin Wolf3574c602011-10-26 11:02:11 +02003818 pstrcpy(filename, filename_size, bs->backing_file);
bellardea2384d2004-08-01 21:59:26 +00003819}
3820
bellardfaea38e2006-08-05 21:31:00 +00003821int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
3822{
3823 BlockDriver *drv = bs->drv;
3824 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00003825 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00003826 if (!drv->bdrv_get_info)
3827 return -ENOTSUP;
3828 memset(bdi, 0, sizeof(*bdi));
3829 return drv->bdrv_get_info(bs, bdi);
3830}
3831
Max Reitzeae041f2013-10-09 10:46:16 +02003832ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs)
3833{
3834 BlockDriver *drv = bs->drv;
3835 if (drv && drv->bdrv_get_specific_info) {
3836 return drv->bdrv_get_specific_info(bs);
3837 }
3838 return NULL;
3839}
3840
Eric Blakea31939e2015-11-18 01:52:54 -07003841void bdrv_debug_event(BlockDriverState *bs, BlkdebugEvent event)
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01003842{
Kevin Wolfbf736fe2013-06-05 15:17:55 +02003843 if (!bs || !bs->drv || !bs->drv->bdrv_debug_event) {
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01003844 return;
3845 }
3846
Kevin Wolfbf736fe2013-06-05 15:17:55 +02003847 bs->drv->bdrv_debug_event(bs, event);
Kevin Wolf41c695c2012-12-06 14:32:58 +01003848}
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01003849
Kevin Wolf41c695c2012-12-06 14:32:58 +01003850int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event,
3851 const char *tag)
3852{
3853 while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003854 bs = bs->file ? bs->file->bs : NULL;
Kevin Wolf41c695c2012-12-06 14:32:58 +01003855 }
3856
3857 if (bs && bs->drv && bs->drv->bdrv_debug_breakpoint) {
3858 return bs->drv->bdrv_debug_breakpoint(bs, event, tag);
3859 }
3860
3861 return -ENOTSUP;
3862}
3863
Fam Zheng4cc70e92013-11-20 10:01:54 +08003864int bdrv_debug_remove_breakpoint(BlockDriverState *bs, const char *tag)
3865{
3866 while (bs && bs->drv && !bs->drv->bdrv_debug_remove_breakpoint) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003867 bs = bs->file ? bs->file->bs : NULL;
Fam Zheng4cc70e92013-11-20 10:01:54 +08003868 }
3869
3870 if (bs && bs->drv && bs->drv->bdrv_debug_remove_breakpoint) {
3871 return bs->drv->bdrv_debug_remove_breakpoint(bs, tag);
3872 }
3873
3874 return -ENOTSUP;
3875}
3876
Kevin Wolf41c695c2012-12-06 14:32:58 +01003877int bdrv_debug_resume(BlockDriverState *bs, const char *tag)
3878{
Max Reitz938789e2014-03-10 23:44:08 +01003879 while (bs && (!bs->drv || !bs->drv->bdrv_debug_resume)) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003880 bs = bs->file ? bs->file->bs : NULL;
Kevin Wolf41c695c2012-12-06 14:32:58 +01003881 }
3882
3883 if (bs && bs->drv && bs->drv->bdrv_debug_resume) {
3884 return bs->drv->bdrv_debug_resume(bs, tag);
3885 }
3886
3887 return -ENOTSUP;
3888}
3889
3890bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag)
3891{
3892 while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003893 bs = bs->file ? bs->file->bs : NULL;
Kevin Wolf41c695c2012-12-06 14:32:58 +01003894 }
3895
3896 if (bs && bs->drv && bs->drv->bdrv_debug_is_suspended) {
3897 return bs->drv->bdrv_debug_is_suspended(bs, tag);
3898 }
3899
3900 return false;
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01003901}
3902
Jeff Codyb1b1d782012-10-16 15:49:09 -04003903/* backing_file can either be relative, or absolute, or a protocol. If it is
3904 * relative, it must be relative to the chain. So, passing in bs->filename
3905 * from a BDS as backing_file should not be done, as that may be relative to
3906 * the CWD rather than the chain. */
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00003907BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs,
3908 const char *backing_file)
3909{
Jeff Codyb1b1d782012-10-16 15:49:09 -04003910 char *filename_full = NULL;
3911 char *backing_file_full = NULL;
3912 char *filename_tmp = NULL;
3913 int is_protocol = 0;
3914 BlockDriverState *curr_bs = NULL;
3915 BlockDriverState *retval = NULL;
Jeff Cody418661e2017-01-25 20:08:20 -05003916 Error *local_error = NULL;
Jeff Codyb1b1d782012-10-16 15:49:09 -04003917
3918 if (!bs || !bs->drv || !backing_file) {
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00003919 return NULL;
3920 }
3921
Jeff Codyb1b1d782012-10-16 15:49:09 -04003922 filename_full = g_malloc(PATH_MAX);
3923 backing_file_full = g_malloc(PATH_MAX);
3924 filename_tmp = g_malloc(PATH_MAX);
3925
3926 is_protocol = path_has_protocol(backing_file);
3927
Kevin Wolf760e0062015-06-17 14:55:21 +02003928 for (curr_bs = bs; curr_bs->backing; curr_bs = curr_bs->backing->bs) {
Jeff Codyb1b1d782012-10-16 15:49:09 -04003929
3930 /* If either of the filename paths is actually a protocol, then
3931 * compare unmodified paths; otherwise make paths relative */
3932 if (is_protocol || path_has_protocol(curr_bs->backing_file)) {
3933 if (strcmp(backing_file, curr_bs->backing_file) == 0) {
Kevin Wolf760e0062015-06-17 14:55:21 +02003934 retval = curr_bs->backing->bs;
Jeff Codyb1b1d782012-10-16 15:49:09 -04003935 break;
3936 }
Jeff Cody418661e2017-01-25 20:08:20 -05003937 /* Also check against the full backing filename for the image */
3938 bdrv_get_full_backing_filename(curr_bs, backing_file_full, PATH_MAX,
3939 &local_error);
3940 if (local_error == NULL) {
3941 if (strcmp(backing_file, backing_file_full) == 0) {
3942 retval = curr_bs->backing->bs;
3943 break;
3944 }
3945 } else {
3946 error_free(local_error);
3947 local_error = NULL;
3948 }
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00003949 } else {
Jeff Codyb1b1d782012-10-16 15:49:09 -04003950 /* If not an absolute filename path, make it relative to the current
3951 * image's filename path */
3952 path_combine(filename_tmp, PATH_MAX, curr_bs->filename,
3953 backing_file);
3954
3955 /* We are going to compare absolute pathnames */
3956 if (!realpath(filename_tmp, filename_full)) {
3957 continue;
3958 }
3959
3960 /* We need to make sure the backing filename we are comparing against
3961 * is relative to the current image filename (or absolute) */
3962 path_combine(filename_tmp, PATH_MAX, curr_bs->filename,
3963 curr_bs->backing_file);
3964
3965 if (!realpath(filename_tmp, backing_file_full)) {
3966 continue;
3967 }
3968
3969 if (strcmp(backing_file_full, filename_full) == 0) {
Kevin Wolf760e0062015-06-17 14:55:21 +02003970 retval = curr_bs->backing->bs;
Jeff Codyb1b1d782012-10-16 15:49:09 -04003971 break;
3972 }
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00003973 }
3974 }
3975
Jeff Codyb1b1d782012-10-16 15:49:09 -04003976 g_free(filename_full);
3977 g_free(backing_file_full);
3978 g_free(filename_tmp);
3979 return retval;
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00003980}
3981
bellardea2384d2004-08-01 21:59:26 +00003982void bdrv_init(void)
3983{
Anthony Liguori5efa9d52009-05-09 17:03:42 -05003984 module_call_init(MODULE_INIT_BLOCK);
bellardea2384d2004-08-01 21:59:26 +00003985}
pbrookce1a14d2006-08-07 02:38:06 +00003986
Markus Armbrustereb852012009-10-27 18:41:44 +01003987void bdrv_init_with_whitelist(void)
3988{
3989 use_bdrv_whitelist = 1;
3990 bdrv_init();
3991}
3992
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003993void bdrv_invalidate_cache(BlockDriverState *bs, Error **errp)
Anthony Liguori0f154232011-11-14 15:09:45 -06003994{
Kevin Wolf4417ab72017-05-04 18:52:37 +02003995 BdrvChild *child, *parent;
Kevin Wolf9c5e6592017-05-04 18:52:40 +02003996 uint64_t perm, shared_perm;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003997 Error *local_err = NULL;
3998 int ret;
3999
Kevin Wolf3456a8d2014-03-11 10:58:39 +01004000 if (!bs->drv) {
4001 return;
Anthony Liguori0f154232011-11-14 15:09:45 -06004002 }
Kevin Wolf3456a8d2014-03-11 10:58:39 +01004003
Kevin Wolf04c01a52016-01-13 15:56:06 +01004004 if (!(bs->open_flags & BDRV_O_INACTIVE)) {
Alexey Kardashevskiy7ea2d262014-10-09 13:50:46 +11004005 return;
4006 }
Alexey Kardashevskiy7ea2d262014-10-09 13:50:46 +11004007
Vladimir Sementsov-Ogievskiy16e977d2017-01-31 14:23:08 +03004008 QLIST_FOREACH(child, &bs->children, next) {
4009 bdrv_invalidate_cache(child->bs, &local_err);
Fam Zheng0d1c5c92016-05-11 10:45:33 +08004010 if (local_err) {
Fam Zheng0d1c5c92016-05-11 10:45:33 +08004011 error_propagate(errp, local_err);
4012 return;
4013 }
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01004014 }
Fam Zheng0d1c5c92016-05-11 10:45:33 +08004015
Vladimir Sementsov-Ogievskiy16e977d2017-01-31 14:23:08 +03004016 bs->open_flags &= ~BDRV_O_INACTIVE;
4017 if (bs->drv->bdrv_invalidate_cache) {
4018 bs->drv->bdrv_invalidate_cache(bs, &local_err);
Fam Zheng0d1c5c92016-05-11 10:45:33 +08004019 if (local_err) {
4020 bs->open_flags |= BDRV_O_INACTIVE;
4021 error_propagate(errp, local_err);
4022 return;
4023 }
Kevin Wolf3456a8d2014-03-11 10:58:39 +01004024 }
4025
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01004026 ret = refresh_total_sectors(bs, bs->total_sectors);
4027 if (ret < 0) {
Kevin Wolf04c01a52016-01-13 15:56:06 +01004028 bs->open_flags |= BDRV_O_INACTIVE;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01004029 error_setg_errno(errp, -ret, "Could not refresh total sector count");
4030 return;
4031 }
Kevin Wolf4417ab72017-05-04 18:52:37 +02004032
Kevin Wolf9c5e6592017-05-04 18:52:40 +02004033 /* Update permissions, they may differ for inactive nodes */
4034 bdrv_get_cumulative_perm(bs, &perm, &shared_perm);
4035 ret = bdrv_check_perm(bs, perm, shared_perm, NULL, &local_err);
4036 if (ret < 0) {
4037 bs->open_flags |= BDRV_O_INACTIVE;
4038 error_propagate(errp, local_err);
4039 return;
4040 }
4041 bdrv_set_perm(bs, perm, shared_perm);
4042
Kevin Wolf4417ab72017-05-04 18:52:37 +02004043 QLIST_FOREACH(parent, &bs->parents, next_parent) {
4044 if (parent->role->activate) {
4045 parent->role->activate(parent, &local_err);
4046 if (local_err) {
4047 error_propagate(errp, local_err);
4048 return;
4049 }
4050 }
4051 }
Anthony Liguori0f154232011-11-14 15:09:45 -06004052}
4053
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01004054void bdrv_invalidate_cache_all(Error **errp)
Anthony Liguori0f154232011-11-14 15:09:45 -06004055{
Kevin Wolf7c8eece2016-03-22 18:58:50 +01004056 BlockDriverState *bs;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01004057 Error *local_err = NULL;
Kevin Wolf88be7b42016-05-20 18:49:07 +02004058 BdrvNextIterator it;
Anthony Liguori0f154232011-11-14 15:09:45 -06004059
Kevin Wolf88be7b42016-05-20 18:49:07 +02004060 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Stefan Hajnoczied78cda2014-05-08 16:34:35 +02004061 AioContext *aio_context = bdrv_get_aio_context(bs);
4062
4063 aio_context_acquire(aio_context);
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01004064 bdrv_invalidate_cache(bs, &local_err);
Stefan Hajnoczied78cda2014-05-08 16:34:35 +02004065 aio_context_release(aio_context);
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01004066 if (local_err) {
4067 error_propagate(errp, local_err);
4068 return;
4069 }
Anthony Liguori0f154232011-11-14 15:09:45 -06004070 }
4071}
4072
Fam Zhengaad0b7a2016-05-11 10:45:35 +08004073static int bdrv_inactivate_recurse(BlockDriverState *bs,
4074 bool setting_flag)
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01004075{
Kevin Wolfcfa1a572017-05-04 18:52:38 +02004076 BdrvChild *child, *parent;
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01004077 int ret;
4078
Fam Zhengaad0b7a2016-05-11 10:45:35 +08004079 if (!setting_flag && bs->drv->bdrv_inactivate) {
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01004080 ret = bs->drv->bdrv_inactivate(bs);
4081 if (ret < 0) {
4082 return ret;
4083 }
4084 }
4085
Stefan Hajnoczi7d5b5262017-08-23 21:42:42 +08004086 if (setting_flag && !(bs->open_flags & BDRV_O_INACTIVE)) {
Kevin Wolf9c5e6592017-05-04 18:52:40 +02004087 uint64_t perm, shared_perm;
4088
Kevin Wolfcfa1a572017-05-04 18:52:38 +02004089 QLIST_FOREACH(parent, &bs->parents, next_parent) {
4090 if (parent->role->inactivate) {
4091 ret = parent->role->inactivate(parent);
4092 if (ret < 0) {
Kevin Wolfcfa1a572017-05-04 18:52:38 +02004093 return ret;
4094 }
4095 }
4096 }
Kevin Wolf9c5e6592017-05-04 18:52:40 +02004097
Stefan Hajnoczi7d5b5262017-08-23 21:42:42 +08004098 bs->open_flags |= BDRV_O_INACTIVE;
4099
Kevin Wolf9c5e6592017-05-04 18:52:40 +02004100 /* Update permissions, they may differ for inactive nodes */
4101 bdrv_get_cumulative_perm(bs, &perm, &shared_perm);
4102 bdrv_check_perm(bs, perm, shared_perm, NULL, &error_abort);
4103 bdrv_set_perm(bs, perm, shared_perm);
Fam Zhengaad0b7a2016-05-11 10:45:35 +08004104 }
Kevin Wolf38701b62017-05-04 18:52:39 +02004105
4106 QLIST_FOREACH(child, &bs->children, next) {
4107 ret = bdrv_inactivate_recurse(child->bs, setting_flag);
4108 if (ret < 0) {
4109 return ret;
4110 }
4111 }
4112
Vladimir Sementsov-Ogievskiy615b5dc2017-06-28 15:05:30 +03004113 /* At this point persistent bitmaps should be already stored by the format
4114 * driver */
4115 bdrv_release_persistent_dirty_bitmaps(bs);
4116
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01004117 return 0;
4118}
4119
4120int bdrv_inactivate_all(void)
4121{
Max Reitz79720af2016-03-16 19:54:44 +01004122 BlockDriverState *bs = NULL;
Kevin Wolf88be7b42016-05-20 18:49:07 +02004123 BdrvNextIterator it;
Fam Zhengaad0b7a2016-05-11 10:45:35 +08004124 int ret = 0;
4125 int pass;
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01004126
Kevin Wolf88be7b42016-05-20 18:49:07 +02004127 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Fam Zhengaad0b7a2016-05-11 10:45:35 +08004128 aio_context_acquire(bdrv_get_aio_context(bs));
4129 }
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01004130
Fam Zhengaad0b7a2016-05-11 10:45:35 +08004131 /* We do two passes of inactivation. The first pass calls to drivers'
4132 * .bdrv_inactivate callbacks recursively so all cache is flushed to disk;
4133 * the second pass sets the BDRV_O_INACTIVE flag so that no further write
4134 * is allowed. */
4135 for (pass = 0; pass < 2; pass++) {
Kevin Wolf88be7b42016-05-20 18:49:07 +02004136 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Fam Zhengaad0b7a2016-05-11 10:45:35 +08004137 ret = bdrv_inactivate_recurse(bs, pass);
4138 if (ret < 0) {
4139 goto out;
4140 }
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01004141 }
4142 }
4143
Fam Zhengaad0b7a2016-05-11 10:45:35 +08004144out:
Kevin Wolf88be7b42016-05-20 18:49:07 +02004145 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Fam Zhengaad0b7a2016-05-11 10:45:35 +08004146 aio_context_release(bdrv_get_aio_context(bs));
4147 }
4148
4149 return ret;
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01004150}
4151
Kevin Wolff9f05dc2011-07-15 13:50:26 +02004152/**************************************************************/
bellard19cb3732006-08-19 11:45:59 +00004153/* removable device support */
4154
4155/**
4156 * Return TRUE if the media is present
4157 */
Max Reitze031f752015-10-19 17:53:11 +02004158bool bdrv_is_inserted(BlockDriverState *bs)
bellard19cb3732006-08-19 11:45:59 +00004159{
4160 BlockDriver *drv = bs->drv;
Max Reitz28d7a782015-10-19 17:53:13 +02004161 BdrvChild *child;
Markus Armbrustera1aff5b2011-09-06 18:58:41 +02004162
Max Reitze031f752015-10-19 17:53:11 +02004163 if (!drv) {
4164 return false;
4165 }
Max Reitz28d7a782015-10-19 17:53:13 +02004166 if (drv->bdrv_is_inserted) {
4167 return drv->bdrv_is_inserted(bs);
Max Reitze031f752015-10-19 17:53:11 +02004168 }
Max Reitz28d7a782015-10-19 17:53:13 +02004169 QLIST_FOREACH(child, &bs->children, next) {
4170 if (!bdrv_is_inserted(child->bs)) {
4171 return false;
4172 }
4173 }
4174 return true;
bellard19cb3732006-08-19 11:45:59 +00004175}
4176
4177/**
Markus Armbruster8e49ca42011-08-03 15:08:08 +02004178 * Return whether the media changed since the last call to this
4179 * function, or -ENOTSUP if we don't know. Most drivers don't know.
bellard19cb3732006-08-19 11:45:59 +00004180 */
4181int bdrv_media_changed(BlockDriverState *bs)
4182{
4183 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +00004184
Markus Armbruster8e49ca42011-08-03 15:08:08 +02004185 if (drv && drv->bdrv_media_changed) {
4186 return drv->bdrv_media_changed(bs);
4187 }
4188 return -ENOTSUP;
bellard19cb3732006-08-19 11:45:59 +00004189}
4190
4191/**
4192 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
4193 */
Luiz Capitulinof36f3942012-02-03 16:24:53 -02004194void bdrv_eject(BlockDriverState *bs, bool eject_flag)
bellard19cb3732006-08-19 11:45:59 +00004195{
4196 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +00004197
Markus Armbruster822e1cd2011-07-20 18:23:42 +02004198 if (drv && drv->bdrv_eject) {
4199 drv->bdrv_eject(bs, eject_flag);
bellard19cb3732006-08-19 11:45:59 +00004200 }
bellard19cb3732006-08-19 11:45:59 +00004201}
4202
bellard19cb3732006-08-19 11:45:59 +00004203/**
4204 * Lock or unlock the media (if it is locked, the user won't be able
4205 * to eject it manually).
4206 */
Markus Armbruster025e8492011-09-06 18:58:47 +02004207void bdrv_lock_medium(BlockDriverState *bs, bool locked)
bellard19cb3732006-08-19 11:45:59 +00004208{
4209 BlockDriver *drv = bs->drv;
4210
Markus Armbruster025e8492011-09-06 18:58:47 +02004211 trace_bdrv_lock_medium(bs, locked);
Stefan Hajnoczib8c6d092011-03-29 20:04:40 +01004212
Markus Armbruster025e8492011-09-06 18:58:47 +02004213 if (drv && drv->bdrv_lock_medium) {
4214 drv->bdrv_lock_medium(bs, locked);
bellard19cb3732006-08-19 11:45:59 +00004215 }
4216}
ths985a03b2007-12-24 16:10:43 +00004217
Fam Zheng9fcb0252013-08-23 09:14:46 +08004218/* Get a reference to bs */
4219void bdrv_ref(BlockDriverState *bs)
4220{
4221 bs->refcnt++;
4222}
4223
4224/* Release a previously grabbed reference to bs.
4225 * If after releasing, reference count is zero, the BlockDriverState is
4226 * deleted. */
4227void bdrv_unref(BlockDriverState *bs)
4228{
Jeff Cody9a4d5ca2014-07-23 17:22:57 -04004229 if (!bs) {
4230 return;
4231 }
Fam Zheng9fcb0252013-08-23 09:14:46 +08004232 assert(bs->refcnt > 0);
4233 if (--bs->refcnt == 0) {
4234 bdrv_delete(bs);
4235 }
4236}
4237
Fam Zhengfbe40ff2014-05-23 21:29:42 +08004238struct BdrvOpBlocker {
4239 Error *reason;
4240 QLIST_ENTRY(BdrvOpBlocker) list;
4241};
4242
4243bool bdrv_op_is_blocked(BlockDriverState *bs, BlockOpType op, Error **errp)
4244{
4245 BdrvOpBlocker *blocker;
4246 assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
4247 if (!QLIST_EMPTY(&bs->op_blockers[op])) {
4248 blocker = QLIST_FIRST(&bs->op_blockers[op]);
Eduardo Habkost57ef3f12017-06-08 10:39:03 -03004249 error_propagate(errp, error_copy(blocker->reason));
4250 error_prepend(errp, "Node '%s' is busy: ",
4251 bdrv_get_device_or_node_name(bs));
Fam Zhengfbe40ff2014-05-23 21:29:42 +08004252 return true;
4253 }
4254 return false;
4255}
4256
4257void bdrv_op_block(BlockDriverState *bs, BlockOpType op, Error *reason)
4258{
4259 BdrvOpBlocker *blocker;
4260 assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
4261
Markus Armbruster5839e532014-08-19 10:31:08 +02004262 blocker = g_new0(BdrvOpBlocker, 1);
Fam Zhengfbe40ff2014-05-23 21:29:42 +08004263 blocker->reason = reason;
4264 QLIST_INSERT_HEAD(&bs->op_blockers[op], blocker, list);
4265}
4266
4267void bdrv_op_unblock(BlockDriverState *bs, BlockOpType op, Error *reason)
4268{
4269 BdrvOpBlocker *blocker, *next;
4270 assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
4271 QLIST_FOREACH_SAFE(blocker, &bs->op_blockers[op], list, next) {
4272 if (blocker->reason == reason) {
4273 QLIST_REMOVE(blocker, list);
4274 g_free(blocker);
4275 }
4276 }
4277}
4278
4279void bdrv_op_block_all(BlockDriverState *bs, Error *reason)
4280{
4281 int i;
4282 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
4283 bdrv_op_block(bs, i, reason);
4284 }
4285}
4286
4287void bdrv_op_unblock_all(BlockDriverState *bs, Error *reason)
4288{
4289 int i;
4290 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
4291 bdrv_op_unblock(bs, i, reason);
4292 }
4293}
4294
4295bool bdrv_op_blocker_is_empty(BlockDriverState *bs)
4296{
4297 int i;
4298
4299 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
4300 if (!QLIST_EMPTY(&bs->op_blockers[i])) {
4301 return false;
4302 }
4303 }
4304 return true;
4305}
4306
Luiz Capitulinod92ada22012-11-30 10:52:09 -02004307void bdrv_img_create(const char *filename, const char *fmt,
4308 const char *base_filename, const char *base_fmt,
Fam Zheng92172832017-04-21 20:27:01 +08004309 char *options, uint64_t img_size, int flags, bool quiet,
4310 Error **errp)
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004311{
Chunyan Liu83d05212014-06-05 17:20:51 +08004312 QemuOptsList *create_opts = NULL;
4313 QemuOpts *opts = NULL;
4314 const char *backing_fmt, *backing_file;
4315 int64_t size;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004316 BlockDriver *drv, *proto_drv;
Max Reitzcc84d902013-09-06 17:14:26 +02004317 Error *local_err = NULL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004318 int ret = 0;
4319
4320 /* Find driver and parse its options */
4321 drv = bdrv_find_format(fmt);
4322 if (!drv) {
Luiz Capitulino71c79812012-11-30 10:52:04 -02004323 error_setg(errp, "Unknown file format '%s'", fmt);
Luiz Capitulinod92ada22012-11-30 10:52:09 -02004324 return;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004325 }
4326
Max Reitzb65a5e12015-02-05 13:58:12 -05004327 proto_drv = bdrv_find_protocol(filename, true, errp);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004328 if (!proto_drv) {
Luiz Capitulinod92ada22012-11-30 10:52:09 -02004329 return;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004330 }
4331
Max Reitzc6149722014-12-02 18:32:45 +01004332 if (!drv->create_opts) {
4333 error_setg(errp, "Format driver '%s' does not support image creation",
4334 drv->format_name);
4335 return;
4336 }
4337
4338 if (!proto_drv->create_opts) {
4339 error_setg(errp, "Protocol driver '%s' does not support image creation",
4340 proto_drv->format_name);
4341 return;
4342 }
4343
Chunyan Liuc282e1f2014-06-05 17:21:11 +08004344 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4345 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004346
4347 /* Create parameter list with default values */
Chunyan Liu83d05212014-06-05 17:20:51 +08004348 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbruster39101f22015-02-12 16:46:36 +01004349 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004350
4351 /* Parse -o options */
4352 if (options) {
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01004353 qemu_opts_do_parse(opts, options, NULL, &local_err);
4354 if (local_err) {
4355 error_report_err(local_err);
4356 local_err = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08004357 error_setg(errp, "Invalid options for file format '%s'", fmt);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004358 goto out;
4359 }
4360 }
4361
4362 if (base_filename) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01004363 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &local_err);
Markus Armbruster6be41942015-02-12 17:49:02 +01004364 if (local_err) {
Luiz Capitulino71c79812012-11-30 10:52:04 -02004365 error_setg(errp, "Backing file not supported for file format '%s'",
4366 fmt);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004367 goto out;
4368 }
4369 }
4370
4371 if (base_fmt) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01004372 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &local_err);
Markus Armbruster6be41942015-02-12 17:49:02 +01004373 if (local_err) {
Luiz Capitulino71c79812012-11-30 10:52:04 -02004374 error_setg(errp, "Backing file format not supported for file "
4375 "format '%s'", fmt);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004376 goto out;
4377 }
4378 }
4379
Chunyan Liu83d05212014-06-05 17:20:51 +08004380 backing_file = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
4381 if (backing_file) {
4382 if (!strcmp(filename, backing_file)) {
Luiz Capitulino71c79812012-11-30 10:52:04 -02004383 error_setg(errp, "Error: Trying to create an image with the "
4384 "same filename as the backing file");
Jes Sorensen792da932010-12-16 13:52:17 +01004385 goto out;
4386 }
4387 }
4388
Chunyan Liu83d05212014-06-05 17:20:51 +08004389 backing_fmt = qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004390
John Snow6e6e55f2017-07-17 20:34:22 -04004391 /* The size for the image must always be specified, unless we have a backing
4392 * file and we have not been forbidden from opening it. */
Chunyan Liu83d05212014-06-05 17:20:51 +08004393 size = qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 0);
John Snow6e6e55f2017-07-17 20:34:22 -04004394 if (backing_file && !(flags & BDRV_O_NO_BACKING)) {
4395 BlockDriverState *bs;
4396 char *full_backing = g_new0(char, PATH_MAX);
4397 int back_flags;
4398 QDict *backing_options = NULL;
Paolo Bonzini63090da2012-04-12 14:01:03 +02004399
John Snow6e6e55f2017-07-17 20:34:22 -04004400 bdrv_get_full_backing_filename_from_filename(filename, backing_file,
4401 full_backing, PATH_MAX,
4402 &local_err);
4403 if (local_err) {
Max Reitz29168012014-11-26 17:20:27 +01004404 g_free(full_backing);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004405 goto out;
4406 }
John Snow6e6e55f2017-07-17 20:34:22 -04004407
4408 /* backing files always opened read-only */
4409 back_flags = flags;
4410 back_flags &= ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
4411
4412 if (backing_fmt) {
4413 backing_options = qdict_new();
4414 qdict_put_str(backing_options, "driver", backing_fmt);
4415 }
4416
4417 bs = bdrv_open(full_backing, NULL, backing_options, back_flags,
4418 &local_err);
4419 g_free(full_backing);
4420 if (!bs && size != -1) {
4421 /* Couldn't open BS, but we have a size, so it's nonfatal */
4422 warn_reportf_err(local_err,
4423 "Could not verify backing image. "
4424 "This may become an error in future versions.\n");
4425 local_err = NULL;
4426 } else if (!bs) {
4427 /* Couldn't open bs, do not have size */
4428 error_append_hint(&local_err,
4429 "Could not open backing image to determine size.\n");
4430 goto out;
4431 } else {
4432 if (size == -1) {
4433 /* Opened BS, have no size */
4434 size = bdrv_getlength(bs);
4435 if (size < 0) {
4436 error_setg_errno(errp, -size, "Could not get size of '%s'",
4437 backing_file);
4438 bdrv_unref(bs);
4439 goto out;
4440 }
4441 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, size, &error_abort);
4442 }
4443 bdrv_unref(bs);
4444 }
4445 } /* (backing_file && !(flags & BDRV_O_NO_BACKING)) */
4446
4447 if (size == -1) {
4448 error_setg(errp, "Image creation needs a size parameter");
4449 goto out;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004450 }
4451
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01004452 if (!quiet) {
Kővágó, Zoltánfe646692015-07-07 16:42:10 +02004453 printf("Formatting '%s', fmt=%s ", filename, fmt);
Fam Zheng43c5d8f2014-12-09 15:38:04 +08004454 qemu_opts_print(opts, " ");
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01004455 puts("");
4456 }
Chunyan Liu83d05212014-06-05 17:20:51 +08004457
Chunyan Liuc282e1f2014-06-05 17:21:11 +08004458 ret = bdrv_create(drv, filename, opts, &local_err);
Chunyan Liu83d05212014-06-05 17:20:51 +08004459
Max Reitzcc84d902013-09-06 17:14:26 +02004460 if (ret == -EFBIG) {
4461 /* This is generally a better message than whatever the driver would
4462 * deliver (especially because of the cluster_size_hint), since that
4463 * is most probably not much different from "image too large". */
4464 const char *cluster_size_hint = "";
Chunyan Liu83d05212014-06-05 17:20:51 +08004465 if (qemu_opt_get_size(opts, BLOCK_OPT_CLUSTER_SIZE, 0)) {
Max Reitzcc84d902013-09-06 17:14:26 +02004466 cluster_size_hint = " (try using a larger cluster size)";
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004467 }
Max Reitzcc84d902013-09-06 17:14:26 +02004468 error_setg(errp, "The image size is too large for file format '%s'"
4469 "%s", fmt, cluster_size_hint);
4470 error_free(local_err);
4471 local_err = NULL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004472 }
4473
4474out:
Chunyan Liu83d05212014-06-05 17:20:51 +08004475 qemu_opts_del(opts);
4476 qemu_opts_free(create_opts);
Eduardo Habkost621ff942016-06-13 18:57:56 -03004477 error_propagate(errp, local_err);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01004478}
Stefan Hajnoczi85d126f2013-03-07 13:41:48 +01004479
4480AioContext *bdrv_get_aio_context(BlockDriverState *bs)
4481{
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02004482 return bs->aio_context;
4483}
4484
Fam Zheng052a7572017-04-10 20:09:25 +08004485void bdrv_coroutine_enter(BlockDriverState *bs, Coroutine *co)
4486{
4487 aio_co_enter(bdrv_get_aio_context(bs), co);
4488}
4489
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01004490static void bdrv_do_remove_aio_context_notifier(BdrvAioNotifier *ban)
4491{
4492 QLIST_REMOVE(ban, list);
4493 g_free(ban);
4494}
4495
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02004496void bdrv_detach_aio_context(BlockDriverState *bs)
4497{
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01004498 BdrvAioNotifier *baf, *baf_tmp;
Max Reitzb97511c2016-05-17 13:38:04 +02004499 BdrvChild *child;
Max Reitz33384422014-06-20 21:57:33 +02004500
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02004501 if (!bs->drv) {
4502 return;
4503 }
4504
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01004505 assert(!bs->walking_aio_notifiers);
4506 bs->walking_aio_notifiers = true;
4507 QLIST_FOREACH_SAFE(baf, &bs->aio_notifiers, list, baf_tmp) {
4508 if (baf->deleted) {
4509 bdrv_do_remove_aio_context_notifier(baf);
4510 } else {
4511 baf->detach_aio_context(baf->opaque);
4512 }
Max Reitz33384422014-06-20 21:57:33 +02004513 }
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01004514 /* Never mind iterating again to check for ->deleted. bdrv_close() will
4515 * remove remaining aio notifiers if we aren't called again.
4516 */
4517 bs->walking_aio_notifiers = false;
Max Reitz33384422014-06-20 21:57:33 +02004518
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02004519 if (bs->drv->bdrv_detach_aio_context) {
4520 bs->drv->bdrv_detach_aio_context(bs);
4521 }
Max Reitzb97511c2016-05-17 13:38:04 +02004522 QLIST_FOREACH(child, &bs->children, next) {
4523 bdrv_detach_aio_context(child->bs);
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02004524 }
4525
4526 bs->aio_context = NULL;
4527}
4528
4529void bdrv_attach_aio_context(BlockDriverState *bs,
4530 AioContext *new_context)
4531{
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01004532 BdrvAioNotifier *ban, *ban_tmp;
Max Reitzb97511c2016-05-17 13:38:04 +02004533 BdrvChild *child;
Max Reitz33384422014-06-20 21:57:33 +02004534
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02004535 if (!bs->drv) {
4536 return;
4537 }
4538
4539 bs->aio_context = new_context;
4540
Max Reitzb97511c2016-05-17 13:38:04 +02004541 QLIST_FOREACH(child, &bs->children, next) {
4542 bdrv_attach_aio_context(child->bs, new_context);
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02004543 }
4544 if (bs->drv->bdrv_attach_aio_context) {
4545 bs->drv->bdrv_attach_aio_context(bs, new_context);
4546 }
Max Reitz33384422014-06-20 21:57:33 +02004547
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01004548 assert(!bs->walking_aio_notifiers);
4549 bs->walking_aio_notifiers = true;
4550 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_tmp) {
4551 if (ban->deleted) {
4552 bdrv_do_remove_aio_context_notifier(ban);
4553 } else {
4554 ban->attached_aio_context(new_context, ban->opaque);
4555 }
Max Reitz33384422014-06-20 21:57:33 +02004556 }
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01004557 bs->walking_aio_notifiers = false;
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02004558}
4559
4560void bdrv_set_aio_context(BlockDriverState *bs, AioContext *new_context)
4561{
Fam Zhengaabf5912017-04-05 14:44:24 +08004562 AioContext *ctx = bdrv_get_aio_context(bs);
Paolo Bonzinic2b64282017-03-14 12:11:57 +01004563
Fam Zhengaabf5912017-04-05 14:44:24 +08004564 aio_disable_external(ctx);
4565 bdrv_parent_drained_begin(bs);
Fam Zheng53ec73e2015-05-29 18:53:14 +08004566 bdrv_drain(bs); /* ensure there are no in-flight requests */
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02004567
Paolo Bonzinic2b64282017-03-14 12:11:57 +01004568 while (aio_poll(ctx, false)) {
4569 /* wait for all bottom halves to execute */
4570 }
4571
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02004572 bdrv_detach_aio_context(bs);
4573
4574 /* This function executes in the old AioContext so acquire the new one in
4575 * case it runs in a different thread.
4576 */
4577 aio_context_acquire(new_context);
4578 bdrv_attach_aio_context(bs, new_context);
Fam Zhengaabf5912017-04-05 14:44:24 +08004579 bdrv_parent_drained_end(bs);
4580 aio_enable_external(ctx);
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02004581 aio_context_release(new_context);
Stefan Hajnoczi85d126f2013-03-07 13:41:48 +01004582}
Stefan Hajnoczid616b222013-06-24 17:13:10 +02004583
Max Reitz33384422014-06-20 21:57:33 +02004584void bdrv_add_aio_context_notifier(BlockDriverState *bs,
4585 void (*attached_aio_context)(AioContext *new_context, void *opaque),
4586 void (*detach_aio_context)(void *opaque), void *opaque)
4587{
4588 BdrvAioNotifier *ban = g_new(BdrvAioNotifier, 1);
4589 *ban = (BdrvAioNotifier){
4590 .attached_aio_context = attached_aio_context,
4591 .detach_aio_context = detach_aio_context,
4592 .opaque = opaque
4593 };
4594
4595 QLIST_INSERT_HEAD(&bs->aio_notifiers, ban, list);
4596}
4597
4598void bdrv_remove_aio_context_notifier(BlockDriverState *bs,
4599 void (*attached_aio_context)(AioContext *,
4600 void *),
4601 void (*detach_aio_context)(void *),
4602 void *opaque)
4603{
4604 BdrvAioNotifier *ban, *ban_next;
4605
4606 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) {
4607 if (ban->attached_aio_context == attached_aio_context &&
4608 ban->detach_aio_context == detach_aio_context &&
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01004609 ban->opaque == opaque &&
4610 ban->deleted == false)
Max Reitz33384422014-06-20 21:57:33 +02004611 {
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01004612 if (bs->walking_aio_notifiers) {
4613 ban->deleted = true;
4614 } else {
4615 bdrv_do_remove_aio_context_notifier(ban);
4616 }
Max Reitz33384422014-06-20 21:57:33 +02004617 return;
4618 }
4619 }
4620
4621 abort();
4622}
4623
Max Reitz77485432014-10-27 11:12:50 +01004624int bdrv_amend_options(BlockDriverState *bs, QemuOpts *opts,
Max Reitz8b139762015-07-27 17:51:32 +02004625 BlockDriverAmendStatusCB *status_cb, void *cb_opaque)
Max Reitz6f176b42013-09-03 10:09:50 +02004626{
Chunyan Liuc282e1f2014-06-05 17:21:11 +08004627 if (!bs->drv->bdrv_amend_options) {
Max Reitz6f176b42013-09-03 10:09:50 +02004628 return -ENOTSUP;
4629 }
Max Reitz8b139762015-07-27 17:51:32 +02004630 return bs->drv->bdrv_amend_options(bs, opts, status_cb, cb_opaque);
Max Reitz6f176b42013-09-03 10:09:50 +02004631}
Benoît Canetf6186f42013-10-02 14:33:48 +02004632
Benoît Canetb5042a32014-03-03 19:11:34 +01004633/* This function will be called by the bdrv_recurse_is_first_non_filter method
4634 * of block filter and by bdrv_is_first_non_filter.
4635 * It is used to test if the given bs is the candidate or recurse more in the
4636 * node graph.
Benoît Canet212a5a82014-01-23 21:31:36 +01004637 */
Benoît Canet212a5a82014-01-23 21:31:36 +01004638bool bdrv_recurse_is_first_non_filter(BlockDriverState *bs,
4639 BlockDriverState *candidate)
Benoît Canetf6186f42013-10-02 14:33:48 +02004640{
Benoît Canetb5042a32014-03-03 19:11:34 +01004641 /* return false if basic checks fails */
4642 if (!bs || !bs->drv) {
4643 return false;
4644 }
4645
4646 /* the code reached a non block filter driver -> check if the bs is
4647 * the same as the candidate. It's the recursion termination condition.
4648 */
4649 if (!bs->drv->is_filter) {
4650 return bs == candidate;
4651 }
4652 /* Down this path the driver is a block filter driver */
4653
4654 /* If the block filter recursion method is defined use it to recurse down
4655 * the node graph.
4656 */
4657 if (bs->drv->bdrv_recurse_is_first_non_filter) {
Benoît Canet212a5a82014-01-23 21:31:36 +01004658 return bs->drv->bdrv_recurse_is_first_non_filter(bs, candidate);
4659 }
4660
Benoît Canetb5042a32014-03-03 19:11:34 +01004661 /* the driver is a block filter but don't allow to recurse -> return false
4662 */
4663 return false;
Benoît Canet212a5a82014-01-23 21:31:36 +01004664}
4665
4666/* This function checks if the candidate is the first non filter bs down it's
4667 * bs chain. Since we don't have pointers to parents it explore all bs chains
4668 * from the top. Some filters can choose not to pass down the recursion.
4669 */
4670bool bdrv_is_first_non_filter(BlockDriverState *candidate)
4671{
Kevin Wolf7c8eece2016-03-22 18:58:50 +01004672 BlockDriverState *bs;
Kevin Wolf88be7b42016-05-20 18:49:07 +02004673 BdrvNextIterator it;
Benoît Canet212a5a82014-01-23 21:31:36 +01004674
4675 /* walk down the bs forest recursively */
Kevin Wolf88be7b42016-05-20 18:49:07 +02004676 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Benoît Canet212a5a82014-01-23 21:31:36 +01004677 bool perm;
4678
Benoît Canetb5042a32014-03-03 19:11:34 +01004679 /* try to recurse in this top level bs */
Kevin Wolfe6dc8a12014-02-04 11:45:31 +01004680 perm = bdrv_recurse_is_first_non_filter(bs, candidate);
Benoît Canet212a5a82014-01-23 21:31:36 +01004681
4682 /* candidate is the first non filter */
4683 if (perm) {
4684 return true;
4685 }
4686 }
4687
4688 return false;
Benoît Canetf6186f42013-10-02 14:33:48 +02004689}
Benoît Canet09158f02014-06-27 18:25:25 +02004690
Wen Congyange12f3782015-07-17 10:12:22 +08004691BlockDriverState *check_to_replace_node(BlockDriverState *parent_bs,
4692 const char *node_name, Error **errp)
Benoît Canet09158f02014-06-27 18:25:25 +02004693{
4694 BlockDriverState *to_replace_bs = bdrv_find_node(node_name);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01004695 AioContext *aio_context;
4696
Benoît Canet09158f02014-06-27 18:25:25 +02004697 if (!to_replace_bs) {
4698 error_setg(errp, "Node name '%s' not found", node_name);
4699 return NULL;
4700 }
4701
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01004702 aio_context = bdrv_get_aio_context(to_replace_bs);
4703 aio_context_acquire(aio_context);
4704
Benoît Canet09158f02014-06-27 18:25:25 +02004705 if (bdrv_op_is_blocked(to_replace_bs, BLOCK_OP_TYPE_REPLACE, errp)) {
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01004706 to_replace_bs = NULL;
4707 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02004708 }
4709
4710 /* We don't want arbitrary node of the BDS chain to be replaced only the top
4711 * most non filter in order to prevent data corruption.
4712 * Another benefit is that this tests exclude backing files which are
4713 * blocked by the backing blockers.
4714 */
Wen Congyange12f3782015-07-17 10:12:22 +08004715 if (!bdrv_recurse_is_first_non_filter(parent_bs, to_replace_bs)) {
Benoît Canet09158f02014-06-27 18:25:25 +02004716 error_setg(errp, "Only top most non filter can be replaced");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01004717 to_replace_bs = NULL;
4718 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02004719 }
4720
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01004721out:
4722 aio_context_release(aio_context);
Benoît Canet09158f02014-06-27 18:25:25 +02004723 return to_replace_bs;
4724}
Ming Lei448ad912014-07-04 18:04:33 +08004725
Max Reitz91af7012014-07-18 20:24:56 +02004726static bool append_open_options(QDict *d, BlockDriverState *bs)
4727{
4728 const QDictEntry *entry;
Kevin Wolf9e700c12015-04-24 15:20:28 +02004729 QemuOptDesc *desc;
Kevin Wolf260fecf2015-04-27 13:46:22 +02004730 BdrvChild *child;
Max Reitz91af7012014-07-18 20:24:56 +02004731 bool found_any = false;
Kevin Wolf260fecf2015-04-27 13:46:22 +02004732 const char *p;
Max Reitz91af7012014-07-18 20:24:56 +02004733
4734 for (entry = qdict_first(bs->options); entry;
4735 entry = qdict_next(bs->options, entry))
4736 {
Kevin Wolf260fecf2015-04-27 13:46:22 +02004737 /* Exclude options for children */
4738 QLIST_FOREACH(child, &bs->children, next) {
4739 if (strstart(qdict_entry_key(entry), child->name, &p)
4740 && (!*p || *p == '.'))
4741 {
4742 break;
4743 }
4744 }
4745 if (child) {
Kevin Wolf9e700c12015-04-24 15:20:28 +02004746 continue;
Max Reitz91af7012014-07-18 20:24:56 +02004747 }
Kevin Wolf9e700c12015-04-24 15:20:28 +02004748
4749 /* And exclude all non-driver-specific options */
4750 for (desc = bdrv_runtime_opts.desc; desc->name; desc++) {
4751 if (!strcmp(qdict_entry_key(entry), desc->name)) {
4752 break;
4753 }
4754 }
4755 if (desc->name) {
4756 continue;
4757 }
4758
4759 qobject_incref(qdict_entry_value(entry));
4760 qdict_put_obj(d, qdict_entry_key(entry), qdict_entry_value(entry));
4761 found_any = true;
Max Reitz91af7012014-07-18 20:24:56 +02004762 }
4763
4764 return found_any;
4765}
4766
4767/* Updates the following BDS fields:
4768 * - exact_filename: A filename which may be used for opening a block device
4769 * which (mostly) equals the given BDS (even without any
4770 * other options; so reading and writing must return the same
4771 * results, but caching etc. may be different)
4772 * - full_open_options: Options which, when given when opening a block device
4773 * (without a filename), result in a BDS (mostly)
4774 * equalling the given one
4775 * - filename: If exact_filename is set, it is copied here. Otherwise,
4776 * full_open_options is converted to a JSON object, prefixed with
4777 * "json:" (for use through the JSON pseudo protocol) and put here.
4778 */
4779void bdrv_refresh_filename(BlockDriverState *bs)
4780{
4781 BlockDriver *drv = bs->drv;
4782 QDict *opts;
4783
4784 if (!drv) {
4785 return;
4786 }
4787
4788 /* This BDS's file name will most probably depend on its file's name, so
4789 * refresh that first */
4790 if (bs->file) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02004791 bdrv_refresh_filename(bs->file->bs);
Max Reitz91af7012014-07-18 20:24:56 +02004792 }
4793
4794 if (drv->bdrv_refresh_filename) {
4795 /* Obsolete information is of no use here, so drop the old file name
4796 * information before refreshing it */
4797 bs->exact_filename[0] = '\0';
4798 if (bs->full_open_options) {
4799 QDECREF(bs->full_open_options);
4800 bs->full_open_options = NULL;
4801 }
4802
Kevin Wolf4cdd01d2015-04-27 13:50:54 +02004803 opts = qdict_new();
4804 append_open_options(opts, bs);
4805 drv->bdrv_refresh_filename(bs, opts);
4806 QDECREF(opts);
Max Reitz91af7012014-07-18 20:24:56 +02004807 } else if (bs->file) {
4808 /* Try to reconstruct valid information from the underlying file */
4809 bool has_open_options;
4810
4811 bs->exact_filename[0] = '\0';
4812 if (bs->full_open_options) {
4813 QDECREF(bs->full_open_options);
4814 bs->full_open_options = NULL;
4815 }
4816
4817 opts = qdict_new();
4818 has_open_options = append_open_options(opts, bs);
4819
4820 /* If no specific options have been given for this BDS, the filename of
4821 * the underlying file should suffice for this one as well */
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02004822 if (bs->file->bs->exact_filename[0] && !has_open_options) {
4823 strcpy(bs->exact_filename, bs->file->bs->exact_filename);
Max Reitz91af7012014-07-18 20:24:56 +02004824 }
4825 /* Reconstructing the full options QDict is simple for most format block
4826 * drivers, as long as the full options are known for the underlying
4827 * file BDS. The full options QDict of that file BDS should somehow
4828 * contain a representation of the filename, therefore the following
4829 * suffices without querying the (exact_)filename of this BDS. */
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02004830 if (bs->file->bs->full_open_options) {
Eric Blake46f5ac22017-04-27 16:58:17 -05004831 qdict_put_str(opts, "driver", drv->format_name);
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02004832 QINCREF(bs->file->bs->full_open_options);
Eric Blakede6e7952017-04-27 16:58:15 -05004833 qdict_put(opts, "file", bs->file->bs->full_open_options);
Max Reitz91af7012014-07-18 20:24:56 +02004834
4835 bs->full_open_options = opts;
4836 } else {
4837 QDECREF(opts);
4838 }
4839 } else if (!bs->full_open_options && qdict_size(bs->options)) {
4840 /* There is no underlying file BDS (at least referenced by BDS.file),
4841 * so the full options QDict should be equal to the options given
4842 * specifically for this block device when it was opened (plus the
4843 * driver specification).
4844 * Because those options don't change, there is no need to update
4845 * full_open_options when it's already set. */
4846
4847 opts = qdict_new();
4848 append_open_options(opts, bs);
Eric Blake46f5ac22017-04-27 16:58:17 -05004849 qdict_put_str(opts, "driver", drv->format_name);
Max Reitz91af7012014-07-18 20:24:56 +02004850
4851 if (bs->exact_filename[0]) {
4852 /* This may not work for all block protocol drivers (some may
4853 * require this filename to be parsed), but we have to find some
4854 * default solution here, so just include it. If some block driver
4855 * does not support pure options without any filename at all or
4856 * needs some special format of the options QDict, it needs to
4857 * implement the driver-specific bdrv_refresh_filename() function.
4858 */
Eric Blake46f5ac22017-04-27 16:58:17 -05004859 qdict_put_str(opts, "filename", bs->exact_filename);
Max Reitz91af7012014-07-18 20:24:56 +02004860 }
4861
4862 bs->full_open_options = opts;
4863 }
4864
4865 if (bs->exact_filename[0]) {
4866 pstrcpy(bs->filename, sizeof(bs->filename), bs->exact_filename);
4867 } else if (bs->full_open_options) {
4868 QString *json = qobject_to_json(QOBJECT(bs->full_open_options));
4869 snprintf(bs->filename, sizeof(bs->filename), "json:%s",
4870 qstring_get_str(json));
4871 QDECREF(json);
4872 }
4873}
Wen Congyange06018a2016-05-10 15:36:37 +08004874
4875/*
4876 * Hot add/remove a BDS's child. So the user can take a child offline when
4877 * it is broken and take a new child online
4878 */
4879void bdrv_add_child(BlockDriverState *parent_bs, BlockDriverState *child_bs,
4880 Error **errp)
4881{
4882
4883 if (!parent_bs->drv || !parent_bs->drv->bdrv_add_child) {
4884 error_setg(errp, "The node %s does not support adding a child",
4885 bdrv_get_device_or_node_name(parent_bs));
4886 return;
4887 }
4888
4889 if (!QLIST_EMPTY(&child_bs->parents)) {
4890 error_setg(errp, "The node %s already has a parent",
4891 child_bs->node_name);
4892 return;
4893 }
4894
4895 parent_bs->drv->bdrv_add_child(parent_bs, child_bs, errp);
4896}
4897
4898void bdrv_del_child(BlockDriverState *parent_bs, BdrvChild *child, Error **errp)
4899{
4900 BdrvChild *tmp;
4901
4902 if (!parent_bs->drv || !parent_bs->drv->bdrv_del_child) {
4903 error_setg(errp, "The node %s does not support removing a child",
4904 bdrv_get_device_or_node_name(parent_bs));
4905 return;
4906 }
4907
4908 QLIST_FOREACH(tmp, &parent_bs->children, next) {
4909 if (tmp == child) {
4910 break;
4911 }
4912 }
4913
4914 if (!tmp) {
4915 error_setg(errp, "The node %s does not have a child named %s",
4916 bdrv_get_device_or_node_name(parent_bs),
4917 bdrv_get_device_or_node_name(child->bs));
4918 return;
4919 }
4920
4921 parent_bs->drv->bdrv_del_child(parent_bs, child, errp);
4922}
Vladimir Sementsov-Ogievskiy67b792f2017-06-28 15:05:21 +03004923
4924bool bdrv_can_store_new_dirty_bitmap(BlockDriverState *bs, const char *name,
4925 uint32_t granularity, Error **errp)
4926{
4927 BlockDriver *drv = bs->drv;
4928
4929 if (!drv) {
4930 error_setg_errno(errp, ENOMEDIUM,
4931 "Can't store persistent bitmaps to %s",
4932 bdrv_get_device_or_node_name(bs));
4933 return false;
4934 }
4935
4936 if (!drv->bdrv_can_store_new_dirty_bitmap) {
4937 error_setg_errno(errp, ENOTSUP,
4938 "Can't store persistent bitmaps to %s",
4939 bdrv_get_device_or_node_name(bs));
4940 return false;
4941 }
4942
4943 return drv->bdrv_can_store_new_dirty_bitmap(bs, name, granularity, errp);
4944}