blob: 6987400f5f45eeaf0b20cb4118812211888fcbcf [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"
Kevin Wolf692e01a2016-09-12 21:00:41 +020045#include "qapi/util.h"
bellardfc01f7e2003-06-30 10:03:06 +000046
Juan Quintela71e72a12009-07-27 16:12:56 +020047#ifdef CONFIG_BSD
bellard7674e7b2005-04-26 21:59:26 +000048#include <sys/ioctl.h>
Blue Swirl72cf2d42009-09-12 07:36:22 +000049#include <sys/queue.h>
blueswir1c5e97232009-03-07 20:06:23 +000050#ifndef __DragonFly__
bellard7674e7b2005-04-26 21:59:26 +000051#include <sys/disk.h>
52#endif
blueswir1c5e97232009-03-07 20:06:23 +000053#endif
bellard7674e7b2005-04-26 21:59:26 +000054
aliguori49dc7682009-03-08 16:26:59 +000055#ifdef _WIN32
56#include <windows.h>
57#endif
58
Stefan Hajnoczi1c9805a2011-10-13 13:08:22 +010059#define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
60
Benoît Canetdc364f42014-01-23 21:31:32 +010061static QTAILQ_HEAD(, BlockDriverState) graph_bdrv_states =
62 QTAILQ_HEAD_INITIALIZER(graph_bdrv_states);
63
Max Reitz2c1d04e2016-01-29 16:36:11 +010064static QTAILQ_HEAD(, BlockDriverState) all_bdrv_states =
65 QTAILQ_HEAD_INITIALIZER(all_bdrv_states);
66
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +010067static QLIST_HEAD(, BlockDriver) bdrv_drivers =
68 QLIST_HEAD_INITIALIZER(bdrv_drivers);
bellardea2384d2004-08-01 21:59:26 +000069
Max Reitz5b363932016-05-17 16:41:31 +020070static BlockDriverState *bdrv_open_inherit(const char *filename,
71 const char *reference,
72 QDict *options, int flags,
73 BlockDriverState *parent,
74 const BdrvChildRole *child_role,
75 Error **errp);
Kevin Wolff3930ed2015-04-08 13:43:47 +020076
Markus Armbrustereb852012009-10-27 18:41:44 +010077/* If non-zero, use only whitelisted block drivers */
78static int use_bdrv_whitelist;
79
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +000080#ifdef _WIN32
81static int is_windows_drive_prefix(const char *filename)
82{
83 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
84 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
85 filename[1] == ':');
86}
87
88int is_windows_drive(const char *filename)
89{
90 if (is_windows_drive_prefix(filename) &&
91 filename[2] == '\0')
92 return 1;
93 if (strstart(filename, "\\\\.\\", NULL) ||
94 strstart(filename, "//./", NULL))
95 return 1;
96 return 0;
97}
98#endif
99
Kevin Wolf339064d2013-11-28 10:23:32 +0100100size_t bdrv_opt_mem_align(BlockDriverState *bs)
101{
102 if (!bs || !bs->drv) {
Denis V. Lunev459b4e62015-05-12 17:30:56 +0300103 /* page size or 4k (hdd sector size) should be on the safe side */
104 return MAX(4096, getpagesize());
Kevin Wolf339064d2013-11-28 10:23:32 +0100105 }
106
107 return bs->bl.opt_mem_alignment;
108}
109
Denis V. Lunev4196d2f2015-05-12 17:30:55 +0300110size_t bdrv_min_mem_align(BlockDriverState *bs)
111{
112 if (!bs || !bs->drv) {
Denis V. Lunev459b4e62015-05-12 17:30:56 +0300113 /* page size or 4k (hdd sector size) should be on the safe side */
114 return MAX(4096, getpagesize());
Denis V. Lunev4196d2f2015-05-12 17:30:55 +0300115 }
116
117 return bs->bl.min_mem_alignment;
118}
119
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000120/* check if the path starts with "<protocol>:" */
Max Reitz5c984152014-12-03 14:57:22 +0100121int path_has_protocol(const char *path)
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000122{
Paolo Bonzini947995c2012-05-08 16:51:48 +0200123 const char *p;
124
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000125#ifdef _WIN32
126 if (is_windows_drive(path) ||
127 is_windows_drive_prefix(path)) {
128 return 0;
129 }
Paolo Bonzini947995c2012-05-08 16:51:48 +0200130 p = path + strcspn(path, ":/\\");
131#else
132 p = path + strcspn(path, ":/");
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000133#endif
134
Paolo Bonzini947995c2012-05-08 16:51:48 +0200135 return *p == ':';
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000136}
137
bellard83f64092006-08-01 16:21:11 +0000138int path_is_absolute(const char *path)
139{
bellard21664422007-01-07 18:22:37 +0000140#ifdef _WIN32
141 /* specific case for names like: "\\.\d:" */
Paolo Bonzinif53f4da2012-05-08 16:51:47 +0200142 if (is_windows_drive(path) || is_windows_drive_prefix(path)) {
bellard21664422007-01-07 18:22:37 +0000143 return 1;
Paolo Bonzinif53f4da2012-05-08 16:51:47 +0200144 }
145 return (*path == '/' || *path == '\\');
bellard3b9f94e2007-01-07 17:27:07 +0000146#else
Paolo Bonzinif53f4da2012-05-08 16:51:47 +0200147 return (*path == '/');
bellard3b9f94e2007-01-07 17:27:07 +0000148#endif
bellard83f64092006-08-01 16:21:11 +0000149}
150
151/* if filename is absolute, just copy it to dest. Otherwise, build a
152 path to it by considering it is relative to base_path. URL are
153 supported. */
154void path_combine(char *dest, int dest_size,
155 const char *base_path,
156 const char *filename)
157{
158 const char *p, *p1;
159 int len;
160
161 if (dest_size <= 0)
162 return;
163 if (path_is_absolute(filename)) {
164 pstrcpy(dest, dest_size, filename);
165 } else {
166 p = strchr(base_path, ':');
167 if (p)
168 p++;
169 else
170 p = base_path;
bellard3b9f94e2007-01-07 17:27:07 +0000171 p1 = strrchr(base_path, '/');
172#ifdef _WIN32
173 {
174 const char *p2;
175 p2 = strrchr(base_path, '\\');
176 if (!p1 || p2 > p1)
177 p1 = p2;
178 }
179#endif
bellard83f64092006-08-01 16:21:11 +0000180 if (p1)
181 p1++;
182 else
183 p1 = base_path;
184 if (p1 > p)
185 p = p1;
186 len = p - base_path;
187 if (len > dest_size - 1)
188 len = dest_size - 1;
189 memcpy(dest, base_path, len);
190 dest[len] = '\0';
191 pstrcat(dest, dest_size, filename);
192 }
193}
194
Max Reitz0a828552014-11-26 17:20:25 +0100195void bdrv_get_full_backing_filename_from_filename(const char *backed,
196 const char *backing,
Max Reitz9f074292014-11-26 17:20:26 +0100197 char *dest, size_t sz,
198 Error **errp)
Max Reitz0a828552014-11-26 17:20:25 +0100199{
Max Reitz9f074292014-11-26 17:20:26 +0100200 if (backing[0] == '\0' || path_has_protocol(backing) ||
201 path_is_absolute(backing))
202 {
Max Reitz0a828552014-11-26 17:20:25 +0100203 pstrcpy(dest, sz, backing);
Max Reitz9f074292014-11-26 17:20:26 +0100204 } else if (backed[0] == '\0' || strstart(backed, "json:", NULL)) {
205 error_setg(errp, "Cannot use relative backing file names for '%s'",
206 backed);
Max Reitz0a828552014-11-26 17:20:25 +0100207 } else {
208 path_combine(dest, sz, backed, backing);
209 }
210}
211
Max Reitz9f074292014-11-26 17:20:26 +0100212void bdrv_get_full_backing_filename(BlockDriverState *bs, char *dest, size_t sz,
213 Error **errp)
Paolo Bonzinidc5a1372012-05-08 16:51:50 +0200214{
Max Reitz9f074292014-11-26 17:20:26 +0100215 char *backed = bs->exact_filename[0] ? bs->exact_filename : bs->filename;
216
217 bdrv_get_full_backing_filename_from_filename(backed, bs->backing_file,
218 dest, sz, errp);
Paolo Bonzinidc5a1372012-05-08 16:51:50 +0200219}
220
Stefan Hajnoczi0eb72172015-04-28 14:27:51 +0100221void bdrv_register(BlockDriver *bdrv)
222{
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100223 QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
bellardea2384d2004-08-01 21:59:26 +0000224}
bellardb3380822004-03-14 21:38:54 +0000225
Markus Armbrustere4e99862014-10-07 13:59:03 +0200226BlockDriverState *bdrv_new(void)
227{
228 BlockDriverState *bs;
229 int i;
230
Markus Armbruster5839e532014-08-19 10:31:08 +0200231 bs = g_new0(BlockDriverState, 1);
Fam Zhenge4654d22013-11-13 18:29:43 +0800232 QLIST_INIT(&bs->dirty_bitmaps);
Fam Zhengfbe40ff2014-05-23 21:29:42 +0800233 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
234 QLIST_INIT(&bs->op_blockers[i]);
235 }
Stefan Hajnoczid616b222013-06-24 17:13:10 +0200236 notifier_with_return_list_init(&bs->before_write_notifiers);
Fam Zheng9fcb0252013-08-23 09:14:46 +0800237 bs->refcnt = 1;
Stefan Hajnoczidcd04222014-05-08 16:34:37 +0200238 bs->aio_context = qemu_get_aio_context();
Paolo Bonzinid7d512f2012-08-23 11:20:36 +0200239
Evgeny Yakovlev3ff2f672016-07-18 22:39:52 +0300240 qemu_co_queue_init(&bs->flush_queue);
241
Max Reitz2c1d04e2016-01-29 16:36:11 +0100242 QTAILQ_INSERT_TAIL(&all_bdrv_states, bs, bs_list);
243
bellardb3380822004-03-14 21:38:54 +0000244 return bs;
245}
246
Marc Mari88d88792016-08-12 09:27:03 -0400247static BlockDriver *bdrv_do_find_format(const char *format_name)
bellardea2384d2004-08-01 21:59:26 +0000248{
249 BlockDriver *drv1;
Marc Mari88d88792016-08-12 09:27:03 -0400250
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100251 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
252 if (!strcmp(drv1->format_name, format_name)) {
bellardea2384d2004-08-01 21:59:26 +0000253 return drv1;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100254 }
bellardea2384d2004-08-01 21:59:26 +0000255 }
Marc Mari88d88792016-08-12 09:27:03 -0400256
bellardea2384d2004-08-01 21:59:26 +0000257 return NULL;
258}
259
Marc Mari88d88792016-08-12 09:27:03 -0400260BlockDriver *bdrv_find_format(const char *format_name)
261{
262 BlockDriver *drv1;
263 int i;
264
265 drv1 = bdrv_do_find_format(format_name);
266 if (drv1) {
267 return drv1;
268 }
269
270 /* The driver isn't registered, maybe we need to load a module */
271 for (i = 0; i < (int)ARRAY_SIZE(block_driver_modules); ++i) {
272 if (!strcmp(block_driver_modules[i].format_name, format_name)) {
273 block_module_load_one(block_driver_modules[i].library_name);
274 break;
275 }
276 }
277
278 return bdrv_do_find_format(format_name);
279}
280
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800281static int bdrv_is_whitelisted(BlockDriver *drv, bool read_only)
Markus Armbrustereb852012009-10-27 18:41:44 +0100282{
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800283 static const char *whitelist_rw[] = {
284 CONFIG_BDRV_RW_WHITELIST
285 };
286 static const char *whitelist_ro[] = {
287 CONFIG_BDRV_RO_WHITELIST
Markus Armbrustereb852012009-10-27 18:41:44 +0100288 };
289 const char **p;
290
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800291 if (!whitelist_rw[0] && !whitelist_ro[0]) {
Markus Armbrustereb852012009-10-27 18:41:44 +0100292 return 1; /* no whitelist, anything goes */
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800293 }
Markus Armbrustereb852012009-10-27 18:41:44 +0100294
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800295 for (p = whitelist_rw; *p; p++) {
Markus Armbrustereb852012009-10-27 18:41:44 +0100296 if (!strcmp(drv->format_name, *p)) {
297 return 1;
298 }
299 }
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800300 if (read_only) {
301 for (p = whitelist_ro; *p; p++) {
302 if (!strcmp(drv->format_name, *p)) {
303 return 1;
304 }
305 }
306 }
Markus Armbrustereb852012009-10-27 18:41:44 +0100307 return 0;
308}
309
Daniel P. Berrangee6ff69b2016-03-21 14:11:48 +0000310bool bdrv_uses_whitelist(void)
311{
312 return use_bdrv_whitelist;
313}
314
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800315typedef struct CreateCo {
316 BlockDriver *drv;
317 char *filename;
Chunyan Liu83d05212014-06-05 17:20:51 +0800318 QemuOpts *opts;
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800319 int ret;
Max Reitzcc84d902013-09-06 17:14:26 +0200320 Error *err;
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800321} CreateCo;
322
323static void coroutine_fn bdrv_create_co_entry(void *opaque)
324{
Max Reitzcc84d902013-09-06 17:14:26 +0200325 Error *local_err = NULL;
326 int ret;
327
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800328 CreateCo *cco = opaque;
329 assert(cco->drv);
330
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800331 ret = cco->drv->bdrv_create(cco->filename, cco->opts, &local_err);
Eduardo Habkost621ff942016-06-13 18:57:56 -0300332 error_propagate(&cco->err, local_err);
Max Reitzcc84d902013-09-06 17:14:26 +0200333 cco->ret = ret;
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800334}
335
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200336int bdrv_create(BlockDriver *drv, const char* filename,
Chunyan Liu83d05212014-06-05 17:20:51 +0800337 QemuOpts *opts, Error **errp)
bellardea2384d2004-08-01 21:59:26 +0000338{
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800339 int ret;
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200340
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800341 Coroutine *co;
342 CreateCo cco = {
343 .drv = drv,
344 .filename = g_strdup(filename),
Chunyan Liu83d05212014-06-05 17:20:51 +0800345 .opts = opts,
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800346 .ret = NOT_DONE,
Max Reitzcc84d902013-09-06 17:14:26 +0200347 .err = NULL,
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800348 };
349
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800350 if (!drv->bdrv_create) {
Max Reitzcc84d902013-09-06 17:14:26 +0200351 error_setg(errp, "Driver '%s' does not support image creation", drv->format_name);
Luiz Capitulino80168bf2012-10-17 16:45:25 -0300352 ret = -ENOTSUP;
353 goto out;
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800354 }
355
356 if (qemu_in_coroutine()) {
357 /* Fast-path if already in coroutine context */
358 bdrv_create_co_entry(&cco);
359 } else {
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200360 co = qemu_coroutine_create(bdrv_create_co_entry, &cco);
361 qemu_coroutine_enter(co);
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800362 while (cco.ret == NOT_DONE) {
Paolo Bonzinib47ec2c2014-07-07 15:18:01 +0200363 aio_poll(qemu_get_aio_context(), true);
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800364 }
365 }
366
367 ret = cco.ret;
Max Reitzcc84d902013-09-06 17:14:26 +0200368 if (ret < 0) {
Markus Armbruster84d18f02014-01-30 15:07:28 +0100369 if (cco.err) {
Max Reitzcc84d902013-09-06 17:14:26 +0200370 error_propagate(errp, cco.err);
371 } else {
372 error_setg_errno(errp, -ret, "Could not create image");
373 }
374 }
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800375
Luiz Capitulino80168bf2012-10-17 16:45:25 -0300376out:
377 g_free(cco.filename);
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800378 return ret;
bellardea2384d2004-08-01 21:59:26 +0000379}
380
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800381int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp)
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200382{
383 BlockDriver *drv;
Max Reitzcc84d902013-09-06 17:14:26 +0200384 Error *local_err = NULL;
385 int ret;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200386
Max Reitzb65a5e12015-02-05 13:58:12 -0500387 drv = bdrv_find_protocol(filename, true, errp);
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200388 if (drv == NULL) {
Stefan Hajnoczi16905d72010-11-30 15:14:14 +0000389 return -ENOENT;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200390 }
391
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800392 ret = bdrv_create(drv, filename, opts, &local_err);
Eduardo Habkost621ff942016-06-13 18:57:56 -0300393 error_propagate(errp, local_err);
Max Reitzcc84d902013-09-06 17:14:26 +0200394 return ret;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200395}
396
Ekaterina Tumanova892b7de2015-02-16 12:47:54 +0100397/**
398 * Try to get @bs's logical and physical block size.
399 * On success, store them in @bsz struct and return 0.
400 * On failure return -errno.
401 * @bs must not be empty.
402 */
403int bdrv_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz)
404{
405 BlockDriver *drv = bs->drv;
406
407 if (drv && drv->bdrv_probe_blocksizes) {
408 return drv->bdrv_probe_blocksizes(bs, bsz);
409 }
410
411 return -ENOTSUP;
412}
413
414/**
415 * Try to get @bs's geometry (cyls, heads, sectors).
416 * On success, store them in @geo struct and return 0.
417 * On failure return -errno.
418 * @bs must not be empty.
419 */
420int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo)
421{
422 BlockDriver *drv = bs->drv;
423
424 if (drv && drv->bdrv_probe_geometry) {
425 return drv->bdrv_probe_geometry(bs, geo);
426 }
427
428 return -ENOTSUP;
429}
430
Jim Meyeringeba25052012-05-28 09:27:54 +0200431/*
432 * Create a uniquely-named empty temporary file.
433 * Return 0 upon success, otherwise a negative errno value.
434 */
435int get_tmp_filename(char *filename, int size)
436{
bellardd5249392004-08-03 21:14:23 +0000437#ifdef _WIN32
bellard3b9f94e2007-01-07 17:27:07 +0000438 char temp_dir[MAX_PATH];
Jim Meyeringeba25052012-05-28 09:27:54 +0200439 /* GetTempFileName requires that its output buffer (4th param)
440 have length MAX_PATH or greater. */
441 assert(size >= MAX_PATH);
442 return (GetTempPath(MAX_PATH, temp_dir)
443 && GetTempFileName(temp_dir, "qem", 0, filename)
444 ? 0 : -GetLastError());
bellardd5249392004-08-03 21:14:23 +0000445#else
bellardea2384d2004-08-01 21:59:26 +0000446 int fd;
blueswir17ccfb2e2008-09-14 06:45:34 +0000447 const char *tmpdir;
aurel320badc1e2008-03-10 00:05:34 +0000448 tmpdir = getenv("TMPDIR");
Amit Shah69bef792014-02-26 15:12:37 +0530449 if (!tmpdir) {
450 tmpdir = "/var/tmp";
451 }
Jim Meyeringeba25052012-05-28 09:27:54 +0200452 if (snprintf(filename, size, "%s/vl.XXXXXX", tmpdir) >= size) {
453 return -EOVERFLOW;
454 }
bellardea2384d2004-08-01 21:59:26 +0000455 fd = mkstemp(filename);
Dunrong Huangfe235a02012-09-05 21:26:22 +0800456 if (fd < 0) {
457 return -errno;
458 }
459 if (close(fd) != 0) {
460 unlink(filename);
Jim Meyeringeba25052012-05-28 09:27:54 +0200461 return -errno;
462 }
463 return 0;
bellardd5249392004-08-03 21:14:23 +0000464#endif
Jim Meyeringeba25052012-05-28 09:27:54 +0200465}
bellardea2384d2004-08-01 21:59:26 +0000466
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200467/*
468 * Detect host devices. By convention, /dev/cdrom[N] is always
469 * recognized as a host CDROM.
470 */
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200471static BlockDriver *find_hdev_driver(const char *filename)
472{
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200473 int score_max = 0, score;
474 BlockDriver *drv = NULL, *d;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200475
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100476 QLIST_FOREACH(d, &bdrv_drivers, list) {
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200477 if (d->bdrv_probe_device) {
478 score = d->bdrv_probe_device(filename);
479 if (score > score_max) {
480 score_max = score;
481 drv = d;
482 }
483 }
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200484 }
485
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200486 return drv;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200487}
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200488
Marc Mari88d88792016-08-12 09:27:03 -0400489static BlockDriver *bdrv_do_find_protocol(const char *protocol)
490{
491 BlockDriver *drv1;
492
493 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
494 if (drv1->protocol_name && !strcmp(drv1->protocol_name, protocol)) {
495 return drv1;
496 }
497 }
498
499 return NULL;
500}
501
Kevin Wolf98289622013-07-10 15:47:39 +0200502BlockDriver *bdrv_find_protocol(const char *filename,
Max Reitzb65a5e12015-02-05 13:58:12 -0500503 bool allow_protocol_prefix,
504 Error **errp)
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200505{
506 BlockDriver *drv1;
507 char protocol[128];
508 int len;
509 const char *p;
Marc Mari88d88792016-08-12 09:27:03 -0400510 int i;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200511
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200512 /* TODO Drivers without bdrv_file_open must be specified explicitly */
513
Christoph Hellwig39508e72010-06-23 12:25:17 +0200514 /*
515 * XXX(hch): we really should not let host device detection
516 * override an explicit protocol specification, but moving this
517 * later breaks access to device names with colons in them.
518 * Thanks to the brain-dead persistent naming schemes on udev-
519 * based Linux systems those actually are quite common.
520 */
521 drv1 = find_hdev_driver(filename);
522 if (drv1) {
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200523 return drv1;
524 }
Christoph Hellwig39508e72010-06-23 12:25:17 +0200525
Kevin Wolf98289622013-07-10 15:47:39 +0200526 if (!path_has_protocol(filename) || !allow_protocol_prefix) {
Max Reitzef810432014-12-02 18:32:42 +0100527 return &bdrv_file;
Christoph Hellwig39508e72010-06-23 12:25:17 +0200528 }
Kevin Wolf98289622013-07-10 15:47:39 +0200529
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000530 p = strchr(filename, ':');
531 assert(p != NULL);
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200532 len = p - filename;
533 if (len > sizeof(protocol) - 1)
534 len = sizeof(protocol) - 1;
535 memcpy(protocol, filename, len);
536 protocol[len] = '\0';
Marc Mari88d88792016-08-12 09:27:03 -0400537
538 drv1 = bdrv_do_find_protocol(protocol);
539 if (drv1) {
540 return drv1;
541 }
542
543 for (i = 0; i < (int)ARRAY_SIZE(block_driver_modules); ++i) {
544 if (block_driver_modules[i].protocol_name &&
545 !strcmp(block_driver_modules[i].protocol_name, protocol)) {
546 block_module_load_one(block_driver_modules[i].library_name);
547 break;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200548 }
549 }
Max Reitzb65a5e12015-02-05 13:58:12 -0500550
Marc Mari88d88792016-08-12 09:27:03 -0400551 drv1 = bdrv_do_find_protocol(protocol);
552 if (!drv1) {
553 error_setg(errp, "Unknown protocol '%s'", protocol);
554 }
555 return drv1;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200556}
557
Markus Armbrusterc6684242014-11-20 16:27:10 +0100558/*
559 * Guess image format by probing its contents.
560 * This is not a good idea when your image is raw (CVE-2008-2004), but
561 * we do it anyway for backward compatibility.
562 *
563 * @buf contains the image's first @buf_size bytes.
Kevin Wolf7cddd372014-11-20 16:27:11 +0100564 * @buf_size is the buffer size in bytes (generally BLOCK_PROBE_BUF_SIZE,
565 * but can be smaller if the image file is smaller)
Markus Armbrusterc6684242014-11-20 16:27:10 +0100566 * @filename is its filename.
567 *
568 * For all block drivers, call the bdrv_probe() method to get its
569 * probing score.
570 * Return the first block driver with the highest probing score.
571 */
Kevin Wolf38f3ef52014-11-20 16:27:12 +0100572BlockDriver *bdrv_probe_all(const uint8_t *buf, int buf_size,
573 const char *filename)
Markus Armbrusterc6684242014-11-20 16:27:10 +0100574{
575 int score_max = 0, score;
576 BlockDriver *drv = NULL, *d;
577
578 QLIST_FOREACH(d, &bdrv_drivers, list) {
579 if (d->bdrv_probe) {
580 score = d->bdrv_probe(buf, buf_size, filename);
581 if (score > score_max) {
582 score_max = score;
583 drv = d;
584 }
585 }
586 }
587
588 return drv;
589}
590
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200591static int find_image_format(BdrvChild *file, const char *filename,
Max Reitz34b5d2c2013-09-05 14:45:29 +0200592 BlockDriver **pdrv, Error **errp)
bellardea2384d2004-08-01 21:59:26 +0000593{
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200594 BlockDriverState *bs = file->bs;
Markus Armbrusterc6684242014-11-20 16:27:10 +0100595 BlockDriver *drv;
Kevin Wolf7cddd372014-11-20 16:27:11 +0100596 uint8_t buf[BLOCK_PROBE_BUF_SIZE];
Kevin Wolff500a6d2012-11-12 17:35:27 +0100597 int ret = 0;
Nicholas Bellingerf8ea0b02010-05-17 09:45:57 -0700598
Kevin Wolf08a00552010-06-01 18:37:31 +0200599 /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
Dimitris Aragiorgisb192af82015-06-23 13:44:56 +0300600 if (bdrv_is_sg(bs) || !bdrv_is_inserted(bs) || bdrv_getlength(bs) == 0) {
Max Reitzef810432014-12-02 18:32:42 +0100601 *pdrv = &bdrv_raw;
Stefan Weilc98ac352010-07-21 21:51:51 +0200602 return ret;
Nicholas A. Bellinger1a396852010-05-27 08:56:28 -0700603 }
Nicholas Bellingerf8ea0b02010-05-17 09:45:57 -0700604
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200605 ret = bdrv_pread(file, 0, buf, sizeof(buf));
bellard83f64092006-08-01 16:21:11 +0000606 if (ret < 0) {
Max Reitz34b5d2c2013-09-05 14:45:29 +0200607 error_setg_errno(errp, -ret, "Could not read image for determining its "
608 "format");
Stefan Weilc98ac352010-07-21 21:51:51 +0200609 *pdrv = NULL;
610 return ret;
bellard83f64092006-08-01 16:21:11 +0000611 }
612
Markus Armbrusterc6684242014-11-20 16:27:10 +0100613 drv = bdrv_probe_all(buf, ret, filename);
Stefan Weilc98ac352010-07-21 21:51:51 +0200614 if (!drv) {
Max Reitz34b5d2c2013-09-05 14:45:29 +0200615 error_setg(errp, "Could not determine image format: No compatible "
616 "driver found");
Stefan Weilc98ac352010-07-21 21:51:51 +0200617 ret = -ENOENT;
618 }
619 *pdrv = drv;
620 return ret;
bellardea2384d2004-08-01 21:59:26 +0000621}
622
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100623/**
624 * Set the current 'total_sectors' value
Markus Armbruster65a9bb22014-06-26 13:23:17 +0200625 * Return 0 on success, -errno on error.
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100626 */
627static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
628{
629 BlockDriver *drv = bs->drv;
630
Nicholas Bellinger396759a2010-05-17 09:46:04 -0700631 /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
Dimitris Aragiorgisb192af82015-06-23 13:44:56 +0300632 if (bdrv_is_sg(bs))
Nicholas Bellinger396759a2010-05-17 09:46:04 -0700633 return 0;
634
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100635 /* query actual device if possible, otherwise just trust the hint */
636 if (drv->bdrv_getlength) {
637 int64_t length = drv->bdrv_getlength(bs);
638 if (length < 0) {
639 return length;
640 }
Fam Zheng7e382002013-11-06 19:48:06 +0800641 hint = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE);
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100642 }
643
644 bs->total_sectors = hint;
645 return 0;
646}
647
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100648/**
Kevin Wolfcddff5b2015-11-16 16:43:27 +0100649 * Combines a QDict of new block driver @options with any missing options taken
650 * from @old_options, so that leaving out an option defaults to its old value.
651 */
652static void bdrv_join_options(BlockDriverState *bs, QDict *options,
653 QDict *old_options)
654{
655 if (bs->drv && bs->drv->bdrv_join_options) {
656 bs->drv->bdrv_join_options(options, old_options);
657 } else {
658 qdict_join(options, old_options, false);
659 }
660}
661
662/**
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100663 * Set open flags for a given discard mode
664 *
665 * Return 0 on success, -1 if the discard mode was invalid.
666 */
667int bdrv_parse_discard_flags(const char *mode, int *flags)
668{
669 *flags &= ~BDRV_O_UNMAP;
670
671 if (!strcmp(mode, "off") || !strcmp(mode, "ignore")) {
672 /* do nothing */
673 } else if (!strcmp(mode, "on") || !strcmp(mode, "unmap")) {
674 *flags |= BDRV_O_UNMAP;
675 } else {
676 return -1;
677 }
678
679 return 0;
680}
681
682/**
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100683 * Set open flags for a given cache mode
684 *
685 * Return 0 on success, -1 if the cache mode was invalid.
686 */
Kevin Wolf53e8ae02016-03-18 15:36:58 +0100687int bdrv_parse_cache_mode(const char *mode, int *flags, bool *writethrough)
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100688{
689 *flags &= ~BDRV_O_CACHE_MASK;
690
691 if (!strcmp(mode, "off") || !strcmp(mode, "none")) {
Kevin Wolf53e8ae02016-03-18 15:36:58 +0100692 *writethrough = false;
693 *flags |= BDRV_O_NOCACHE;
Stefan Hajnoczi92196b22011-08-04 12:26:52 +0100694 } else if (!strcmp(mode, "directsync")) {
Kevin Wolf53e8ae02016-03-18 15:36:58 +0100695 *writethrough = true;
Stefan Hajnoczi92196b22011-08-04 12:26:52 +0100696 *flags |= BDRV_O_NOCACHE;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100697 } else if (!strcmp(mode, "writeback")) {
Kevin Wolf53e8ae02016-03-18 15:36:58 +0100698 *writethrough = false;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100699 } else if (!strcmp(mode, "unsafe")) {
Kevin Wolf53e8ae02016-03-18 15:36:58 +0100700 *writethrough = false;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100701 *flags |= BDRV_O_NO_FLUSH;
702 } else if (!strcmp(mode, "writethrough")) {
Kevin Wolf53e8ae02016-03-18 15:36:58 +0100703 *writethrough = true;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100704 } else {
705 return -1;
706 }
707
708 return 0;
709}
710
Kevin Wolf20018e12016-05-23 18:46:59 +0200711static void bdrv_child_cb_drained_begin(BdrvChild *child)
712{
713 BlockDriverState *bs = child->opaque;
714 bdrv_drained_begin(bs);
715}
716
717static void bdrv_child_cb_drained_end(BdrvChild *child)
718{
719 BlockDriverState *bs = child->opaque;
720 bdrv_drained_end(bs);
721}
722
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200723/*
Kevin Wolf73176be2016-03-07 13:02:15 +0100724 * Returns the options and flags that a temporary snapshot should get, based on
725 * the originally requested flags (the originally requested image will have
726 * flags like a backing file)
Kevin Wolfb1e6fc02014-05-06 12:11:42 +0200727 */
Kevin Wolf73176be2016-03-07 13:02:15 +0100728static void bdrv_temp_snapshot_options(int *child_flags, QDict *child_options,
729 int parent_flags, QDict *parent_options)
Kevin Wolfb1e6fc02014-05-06 12:11:42 +0200730{
Kevin Wolf73176be2016-03-07 13:02:15 +0100731 *child_flags = (parent_flags & ~BDRV_O_SNAPSHOT) | BDRV_O_TEMPORARY;
732
733 /* For temporary files, unconditional cache=unsafe is fine */
Kevin Wolf73176be2016-03-07 13:02:15 +0100734 qdict_set_default_str(child_options, BDRV_OPT_CACHE_DIRECT, "off");
735 qdict_set_default_str(child_options, BDRV_OPT_CACHE_NO_FLUSH, "on");
Kevin Wolf41869042016-06-16 12:59:30 +0200736
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300737 /* Copy the read-only option from the parent */
738 qdict_copy_default(child_options, parent_options, BDRV_OPT_READ_ONLY);
739
Kevin Wolf41869042016-06-16 12:59:30 +0200740 /* aio=native doesn't work for cache.direct=off, so disable it for the
741 * temporary snapshot */
742 *child_flags &= ~BDRV_O_NATIVE_AIO;
Kevin Wolfb1e6fc02014-05-06 12:11:42 +0200743}
744
745/*
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200746 * Returns the options and flags that bs->file should get if a protocol driver
747 * is expected, based on the given options and flags for the parent BDS
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200748 */
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200749static void bdrv_inherited_options(int *child_flags, QDict *child_options,
750 int parent_flags, QDict *parent_options)
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200751{
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200752 int flags = parent_flags;
753
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200754 /* Enable protocol handling, disable format probing for bs->file */
755 flags |= BDRV_O_PROTOCOL;
756
Kevin Wolf91a097e2015-05-08 17:49:53 +0200757 /* If the cache mode isn't explicitly set, inherit direct and no-flush from
758 * the parent. */
759 qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_DIRECT);
760 qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_NO_FLUSH);
761
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300762 /* Inherit the read-only option from the parent if it's not set */
763 qdict_copy_default(child_options, parent_options, BDRV_OPT_READ_ONLY);
764
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200765 /* Our block drivers take care to send flushes and respect unmap policy,
Kevin Wolf91a097e2015-05-08 17:49:53 +0200766 * so we can default to enable both on lower layers regardless of the
767 * corresponding parent options. */
Kevin Wolf818584a2016-09-12 18:03:18 +0200768 qdict_set_default_str(child_options, BDRV_OPT_DISCARD, "unmap");
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200769
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200770 /* Clear flags that only apply to the top layer */
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000771 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ |
772 BDRV_O_NO_IO);
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200773
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200774 *child_flags = flags;
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200775}
776
Kevin Wolff3930ed2015-04-08 13:43:47 +0200777const BdrvChildRole child_file = {
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200778 .inherit_options = bdrv_inherited_options,
Kevin Wolf20018e12016-05-23 18:46:59 +0200779 .drained_begin = bdrv_child_cb_drained_begin,
780 .drained_end = bdrv_child_cb_drained_end,
Kevin Wolff3930ed2015-04-08 13:43:47 +0200781};
782
783/*
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200784 * Returns the options and flags that bs->file should get if the use of formats
785 * (and not only protocols) is permitted for it, based on the given options and
786 * flags for the parent BDS
Kevin Wolff3930ed2015-04-08 13:43:47 +0200787 */
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200788static void bdrv_inherited_fmt_options(int *child_flags, QDict *child_options,
789 int parent_flags, QDict *parent_options)
Kevin Wolff3930ed2015-04-08 13:43:47 +0200790{
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200791 child_file.inherit_options(child_flags, child_options,
792 parent_flags, parent_options);
793
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000794 *child_flags &= ~(BDRV_O_PROTOCOL | BDRV_O_NO_IO);
Kevin Wolff3930ed2015-04-08 13:43:47 +0200795}
796
797const BdrvChildRole child_format = {
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200798 .inherit_options = bdrv_inherited_fmt_options,
Kevin Wolf20018e12016-05-23 18:46:59 +0200799 .drained_begin = bdrv_child_cb_drained_begin,
800 .drained_end = bdrv_child_cb_drained_end,
Kevin Wolff3930ed2015-04-08 13:43:47 +0200801};
802
Kevin Wolf317fc442014-04-25 13:27:34 +0200803/*
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200804 * Returns the options and flags that bs->backing should get, based on the
805 * given options and flags for the parent BDS
Kevin Wolf317fc442014-04-25 13:27:34 +0200806 */
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200807static void bdrv_backing_options(int *child_flags, QDict *child_options,
808 int parent_flags, QDict *parent_options)
Kevin Wolf317fc442014-04-25 13:27:34 +0200809{
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200810 int flags = parent_flags;
811
Kevin Wolfb8816a42016-03-04 14:52:32 +0100812 /* The cache mode is inherited unmodified for backing files; except WCE,
813 * which is only applied on the top level (BlockBackend) */
Kevin Wolf91a097e2015-05-08 17:49:53 +0200814 qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_DIRECT);
815 qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_NO_FLUSH);
816
Kevin Wolf317fc442014-04-25 13:27:34 +0200817 /* backing files always opened read-only */
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300818 qdict_set_default_str(child_options, BDRV_OPT_READ_ONLY, "on");
819 flags &= ~BDRV_O_COPY_ON_READ;
Kevin Wolf317fc442014-04-25 13:27:34 +0200820
821 /* snapshot=on is handled on the top layer */
Kevin Wolf8bfea152014-04-11 19:16:36 +0200822 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_TEMPORARY);
Kevin Wolf317fc442014-04-25 13:27:34 +0200823
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200824 *child_flags = flags;
Kevin Wolf317fc442014-04-25 13:27:34 +0200825}
826
Kevin Wolff3930ed2015-04-08 13:43:47 +0200827static const BdrvChildRole child_backing = {
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200828 .inherit_options = bdrv_backing_options,
Kevin Wolf20018e12016-05-23 18:46:59 +0200829 .drained_begin = bdrv_child_cb_drained_begin,
830 .drained_end = bdrv_child_cb_drained_end,
Kevin Wolff3930ed2015-04-08 13:43:47 +0200831};
832
Kevin Wolf7b272452012-11-12 17:05:39 +0100833static int bdrv_open_flags(BlockDriverState *bs, int flags)
834{
Kevin Wolf61de4c62016-03-18 17:46:45 +0100835 int open_flags = flags;
Kevin Wolf7b272452012-11-12 17:05:39 +0100836
837 /*
838 * Clear flags that are internal to the block layer before opening the
839 * image.
840 */
Kevin Wolf20cca272014-06-04 14:33:27 +0200841 open_flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_PROTOCOL);
Kevin Wolf7b272452012-11-12 17:05:39 +0100842
843 /*
844 * Snapshots should be writable.
845 */
Kevin Wolf8bfea152014-04-11 19:16:36 +0200846 if (flags & BDRV_O_TEMPORARY) {
Kevin Wolf7b272452012-11-12 17:05:39 +0100847 open_flags |= BDRV_O_RDWR;
848 }
849
850 return open_flags;
851}
852
Kevin Wolf91a097e2015-05-08 17:49:53 +0200853static void update_flags_from_options(int *flags, QemuOpts *opts)
854{
855 *flags &= ~BDRV_O_CACHE_MASK;
856
Kevin Wolf91a097e2015-05-08 17:49:53 +0200857 assert(qemu_opt_find(opts, BDRV_OPT_CACHE_NO_FLUSH));
858 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, false)) {
859 *flags |= BDRV_O_NO_FLUSH;
860 }
861
862 assert(qemu_opt_find(opts, BDRV_OPT_CACHE_DIRECT));
863 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_DIRECT, false)) {
864 *flags |= BDRV_O_NOCACHE;
865 }
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300866
867 *flags &= ~BDRV_O_RDWR;
868
869 assert(qemu_opt_find(opts, BDRV_OPT_READ_ONLY));
870 if (!qemu_opt_get_bool(opts, BDRV_OPT_READ_ONLY, false)) {
871 *flags |= BDRV_O_RDWR;
872 }
873
Kevin Wolf91a097e2015-05-08 17:49:53 +0200874}
875
876static void update_options_from_flags(QDict *options, int flags)
877{
Kevin Wolf91a097e2015-05-08 17:49:53 +0200878 if (!qdict_haskey(options, BDRV_OPT_CACHE_DIRECT)) {
879 qdict_put(options, BDRV_OPT_CACHE_DIRECT,
880 qbool_from_bool(flags & BDRV_O_NOCACHE));
881 }
882 if (!qdict_haskey(options, BDRV_OPT_CACHE_NO_FLUSH)) {
883 qdict_put(options, BDRV_OPT_CACHE_NO_FLUSH,
884 qbool_from_bool(flags & BDRV_O_NO_FLUSH));
885 }
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300886 if (!qdict_haskey(options, BDRV_OPT_READ_ONLY)) {
887 qdict_put(options, BDRV_OPT_READ_ONLY,
888 qbool_from_bool(!(flags & BDRV_O_RDWR)));
889 }
Kevin Wolf91a097e2015-05-08 17:49:53 +0200890}
891
Kevin Wolf636ea372014-01-24 14:11:52 +0100892static void bdrv_assign_node_name(BlockDriverState *bs,
893 const char *node_name,
894 Error **errp)
Benoît Canet6913c0c2014-01-23 21:31:33 +0100895{
Jeff Cody15489c72015-10-12 19:36:50 -0400896 char *gen_node_name = NULL;
Benoît Canet6913c0c2014-01-23 21:31:33 +0100897
Jeff Cody15489c72015-10-12 19:36:50 -0400898 if (!node_name) {
899 node_name = gen_node_name = id_generate(ID_BLOCK);
900 } else if (!id_wellformed(node_name)) {
901 /*
902 * Check for empty string or invalid characters, but not if it is
903 * generated (generated names use characters not available to the user)
904 */
Kevin Wolf9aebf3b2014-09-25 09:54:02 +0200905 error_setg(errp, "Invalid node name");
Kevin Wolf636ea372014-01-24 14:11:52 +0100906 return;
Benoît Canet6913c0c2014-01-23 21:31:33 +0100907 }
908
Benoît Canet0c5e94e2014-02-12 17:15:07 +0100909 /* takes care of avoiding namespaces collisions */
Markus Armbruster7f06d472014-10-07 13:59:12 +0200910 if (blk_by_name(node_name)) {
Benoît Canet0c5e94e2014-02-12 17:15:07 +0100911 error_setg(errp, "node-name=%s is conflicting with a device id",
912 node_name);
Jeff Cody15489c72015-10-12 19:36:50 -0400913 goto out;
Benoît Canet0c5e94e2014-02-12 17:15:07 +0100914 }
915
Benoît Canet6913c0c2014-01-23 21:31:33 +0100916 /* takes care of avoiding duplicates node names */
917 if (bdrv_find_node(node_name)) {
918 error_setg(errp, "Duplicate node name");
Jeff Cody15489c72015-10-12 19:36:50 -0400919 goto out;
Benoît Canet6913c0c2014-01-23 21:31:33 +0100920 }
921
922 /* copy node name into the bs and insert it into the graph list */
923 pstrcpy(bs->node_name, sizeof(bs->node_name), node_name);
924 QTAILQ_INSERT_TAIL(&graph_bdrv_states, bs, node_list);
Jeff Cody15489c72015-10-12 19:36:50 -0400925out:
926 g_free(gen_node_name);
Benoît Canet6913c0c2014-01-23 21:31:33 +0100927}
928
Kevin Wolfc5f30142016-10-06 11:33:17 +0200929QemuOptsList bdrv_runtime_opts = {
Kevin Wolf18edf282015-04-07 17:12:56 +0200930 .name = "bdrv_common",
931 .head = QTAILQ_HEAD_INITIALIZER(bdrv_runtime_opts.head),
932 .desc = {
933 {
934 .name = "node-name",
935 .type = QEMU_OPT_STRING,
936 .help = "Node name of the block device node",
937 },
Kevin Wolf62392eb2015-04-24 16:38:02 +0200938 {
939 .name = "driver",
940 .type = QEMU_OPT_STRING,
941 .help = "Block driver to use for the node",
942 },
Kevin Wolf91a097e2015-05-08 17:49:53 +0200943 {
Kevin Wolf91a097e2015-05-08 17:49:53 +0200944 .name = BDRV_OPT_CACHE_DIRECT,
945 .type = QEMU_OPT_BOOL,
946 .help = "Bypass software writeback cache on the host",
947 },
948 {
949 .name = BDRV_OPT_CACHE_NO_FLUSH,
950 .type = QEMU_OPT_BOOL,
951 .help = "Ignore flush requests",
952 },
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300953 {
954 .name = BDRV_OPT_READ_ONLY,
955 .type = QEMU_OPT_BOOL,
956 .help = "Node is opened in read-only mode",
957 },
Kevin Wolf692e01a2016-09-12 21:00:41 +0200958 {
959 .name = "detect-zeroes",
960 .type = QEMU_OPT_STRING,
961 .help = "try to optimize zero writes (off, on, unmap)",
962 },
Kevin Wolf818584a2016-09-12 18:03:18 +0200963 {
964 .name = "discard",
965 .type = QEMU_OPT_STRING,
966 .help = "discard operation (ignore/off, unmap/on)",
967 },
Kevin Wolf18edf282015-04-07 17:12:56 +0200968 { /* end of list */ }
969 },
970};
971
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200972/*
Kevin Wolf57915332010-04-14 15:24:50 +0200973 * Common part for opening disk images and files
Kevin Wolfb6ad4912013-03-15 10:35:04 +0100974 *
975 * Removes all processed options from *options.
Kevin Wolf57915332010-04-14 15:24:50 +0200976 */
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200977static int bdrv_open_common(BlockDriverState *bs, BdrvChild *file,
Kevin Wolf82dc8b42016-01-11 19:07:50 +0100978 QDict *options, Error **errp)
Kevin Wolf57915332010-04-14 15:24:50 +0200979{
980 int ret, open_flags;
Kevin Wolf035fccd2013-04-09 14:34:19 +0200981 const char *filename;
Kevin Wolf62392eb2015-04-24 16:38:02 +0200982 const char *driver_name = NULL;
Benoît Canet6913c0c2014-01-23 21:31:33 +0100983 const char *node_name = NULL;
Kevin Wolf818584a2016-09-12 18:03:18 +0200984 const char *discard;
Kevin Wolf692e01a2016-09-12 21:00:41 +0200985 const char *detect_zeroes;
Kevin Wolf18edf282015-04-07 17:12:56 +0200986 QemuOpts *opts;
Kevin Wolf62392eb2015-04-24 16:38:02 +0200987 BlockDriver *drv;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200988 Error *local_err = NULL;
Kevin Wolf57915332010-04-14 15:24:50 +0200989
Paolo Bonzini64058752012-05-08 16:51:49 +0200990 assert(bs->file == NULL);
Kevin Wolf707ff822013-03-06 12:20:31 +0100991 assert(options != NULL && bs->options != options);
Kevin Wolf57915332010-04-14 15:24:50 +0200992
Kevin Wolf62392eb2015-04-24 16:38:02 +0200993 opts = qemu_opts_create(&bdrv_runtime_opts, NULL, 0, &error_abort);
994 qemu_opts_absorb_qdict(opts, options, &local_err);
995 if (local_err) {
996 error_propagate(errp, local_err);
997 ret = -EINVAL;
998 goto fail_opts;
999 }
1000
Alberto Garcia9b7e8692016-09-15 17:53:01 +03001001 update_flags_from_options(&bs->open_flags, opts);
1002
Kevin Wolf62392eb2015-04-24 16:38:02 +02001003 driver_name = qemu_opt_get(opts, "driver");
1004 drv = bdrv_find_format(driver_name);
1005 assert(drv != NULL);
1006
Kevin Wolf45673672013-04-22 17:48:40 +02001007 if (file != NULL) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02001008 filename = file->bs->filename;
Kevin Wolf45673672013-04-22 17:48:40 +02001009 } else {
1010 filename = qdict_get_try_str(options, "filename");
1011 }
1012
Kevin Wolf765003d2014-02-03 14:49:42 +01001013 if (drv->bdrv_needs_filename && !filename) {
1014 error_setg(errp, "The '%s' block driver requires a file name",
1015 drv->format_name);
Kevin Wolf18edf282015-04-07 17:12:56 +02001016 ret = -EINVAL;
1017 goto fail_opts;
1018 }
1019
Kevin Wolf82dc8b42016-01-11 19:07:50 +01001020 trace_bdrv_open_common(bs, filename ?: "", bs->open_flags,
1021 drv->format_name);
Kevin Wolf62392eb2015-04-24 16:38:02 +02001022
Kevin Wolf18edf282015-04-07 17:12:56 +02001023 node_name = qemu_opt_get(opts, "node-name");
Kevin Wolf636ea372014-01-24 14:11:52 +01001024 bdrv_assign_node_name(bs, node_name, &local_err);
Markus Armbruster0fb63952014-04-25 16:50:31 +02001025 if (local_err) {
Kevin Wolf636ea372014-01-24 14:11:52 +01001026 error_propagate(errp, local_err);
Kevin Wolf18edf282015-04-07 17:12:56 +02001027 ret = -EINVAL;
1028 goto fail_opts;
Kevin Wolf5d186eb2013-03-27 17:28:18 +01001029 }
1030
Kevin Wolf82dc8b42016-01-11 19:07:50 +01001031 bs->read_only = !(bs->open_flags & BDRV_O_RDWR);
Fam Zhengb64ec4e2013-05-29 19:35:40 +08001032
1033 if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, bs->read_only)) {
Kevin Wolf8f94a6e2013-10-10 11:45:55 +02001034 error_setg(errp,
1035 !bs->read_only && bdrv_is_whitelisted(drv, true)
1036 ? "Driver '%s' can only be used for read-only devices"
1037 : "Driver '%s' is not whitelisted",
1038 drv->format_name);
Kevin Wolf18edf282015-04-07 17:12:56 +02001039 ret = -ENOTSUP;
1040 goto fail_opts;
Fam Zhengb64ec4e2013-05-29 19:35:40 +08001041 }
Kevin Wolf57915332010-04-14 15:24:50 +02001042
Stefan Hajnoczi53fec9d2011-11-28 16:08:47 +00001043 assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */
Kevin Wolf82dc8b42016-01-11 19:07:50 +01001044 if (bs->open_flags & BDRV_O_COPY_ON_READ) {
Kevin Wolf0ebd24e2013-09-19 15:12:18 +02001045 if (!bs->read_only) {
1046 bdrv_enable_copy_on_read(bs);
1047 } else {
1048 error_setg(errp, "Can't use copy-on-read on read-only device");
Kevin Wolf18edf282015-04-07 17:12:56 +02001049 ret = -EINVAL;
1050 goto fail_opts;
Kevin Wolf0ebd24e2013-09-19 15:12:18 +02001051 }
Stefan Hajnoczi53fec9d2011-11-28 16:08:47 +00001052 }
1053
Kevin Wolf818584a2016-09-12 18:03:18 +02001054 discard = qemu_opt_get(opts, "discard");
1055 if (discard != NULL) {
1056 if (bdrv_parse_discard_flags(discard, &bs->open_flags) != 0) {
1057 error_setg(errp, "Invalid discard option");
1058 ret = -EINVAL;
1059 goto fail_opts;
1060 }
1061 }
1062
Kevin Wolf692e01a2016-09-12 21:00:41 +02001063 detect_zeroes = qemu_opt_get(opts, "detect-zeroes");
1064 if (detect_zeroes) {
1065 BlockdevDetectZeroesOptions value =
1066 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
1067 detect_zeroes,
1068 BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX,
1069 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
1070 &local_err);
1071 if (local_err) {
1072 error_propagate(errp, local_err);
1073 ret = -EINVAL;
1074 goto fail_opts;
1075 }
1076
1077 if (value == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
1078 !(bs->open_flags & BDRV_O_UNMAP))
1079 {
1080 error_setg(errp, "setting detect-zeroes to unmap is not allowed "
1081 "without setting discard operation to unmap");
1082 ret = -EINVAL;
1083 goto fail_opts;
1084 }
1085
1086 bs->detect_zeroes = value;
1087 }
1088
Kevin Wolfc2ad1b02013-03-18 16:40:51 +01001089 if (filename != NULL) {
1090 pstrcpy(bs->filename, sizeof(bs->filename), filename);
1091 } else {
1092 bs->filename[0] = '\0';
1093 }
Max Reitz91af7012014-07-18 20:24:56 +02001094 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), bs->filename);
Kevin Wolf57915332010-04-14 15:24:50 +02001095
Kevin Wolf57915332010-04-14 15:24:50 +02001096 bs->drv = drv;
Anthony Liguori7267c092011-08-20 22:09:37 -05001097 bs->opaque = g_malloc0(drv->instance_size);
Kevin Wolf57915332010-04-14 15:24:50 +02001098
Kevin Wolf66f82ce2010-04-14 14:17:38 +02001099 /* Open the image, either directly or using a protocol */
Kevin Wolf82dc8b42016-01-11 19:07:50 +01001100 open_flags = bdrv_open_flags(bs, bs->open_flags);
Kevin Wolf66f82ce2010-04-14 14:17:38 +02001101 if (drv->bdrv_file_open) {
Kevin Wolf5d186eb2013-03-27 17:28:18 +01001102 assert(file == NULL);
Benoît Canet030be322013-09-24 17:07:04 +02001103 assert(!drv->bdrv_needs_filename || filename != NULL);
Max Reitz34b5d2c2013-09-05 14:45:29 +02001104 ret = drv->bdrv_file_open(bs, options, open_flags, &local_err);
Kevin Wolff500a6d2012-11-12 17:35:27 +01001105 } else {
Max Reitz34b5d2c2013-09-05 14:45:29 +02001106 ret = drv->bdrv_open(bs, options, open_flags, &local_err);
Kevin Wolf66f82ce2010-04-14 14:17:38 +02001107 }
1108
Kevin Wolf57915332010-04-14 15:24:50 +02001109 if (ret < 0) {
Markus Armbruster84d18f02014-01-30 15:07:28 +01001110 if (local_err) {
Max Reitz34b5d2c2013-09-05 14:45:29 +02001111 error_propagate(errp, local_err);
Dunrong Huang2fa9aa52013-09-24 18:14:01 +08001112 } else if (bs->filename[0]) {
1113 error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename);
Max Reitz34b5d2c2013-09-05 14:45:29 +02001114 } else {
1115 error_setg_errno(errp, -ret, "Could not open image");
1116 }
Kevin Wolf57915332010-04-14 15:24:50 +02001117 goto free_and_fail;
1118 }
1119
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001120 ret = refresh_total_sectors(bs, bs->total_sectors);
1121 if (ret < 0) {
Max Reitz34b5d2c2013-09-05 14:45:29 +02001122 error_setg_errno(errp, -ret, "Could not refresh total sector count");
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001123 goto free_and_fail;
Kevin Wolf57915332010-04-14 15:24:50 +02001124 }
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001125
Kevin Wolf3baca892014-07-16 17:48:16 +02001126 bdrv_refresh_limits(bs, &local_err);
1127 if (local_err) {
1128 error_propagate(errp, local_err);
1129 ret = -EINVAL;
1130 goto free_and_fail;
1131 }
1132
Paolo Bonzinic25f53b2011-11-29 12:42:20 +01001133 assert(bdrv_opt_mem_align(bs) != 0);
Denis V. Lunev4196d2f2015-05-12 17:30:55 +03001134 assert(bdrv_min_mem_align(bs) != 0);
Eric Blakea5b8dd22016-06-23 16:37:24 -06001135 assert(is_power_of_2(bs->bl.request_alignment));
Kevin Wolf18edf282015-04-07 17:12:56 +02001136
1137 qemu_opts_del(opts);
Kevin Wolf57915332010-04-14 15:24:50 +02001138 return 0;
1139
1140free_and_fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05001141 g_free(bs->opaque);
Kevin Wolf57915332010-04-14 15:24:50 +02001142 bs->opaque = NULL;
1143 bs->drv = NULL;
Kevin Wolf18edf282015-04-07 17:12:56 +02001144fail_opts:
1145 qemu_opts_del(opts);
Kevin Wolf57915332010-04-14 15:24:50 +02001146 return ret;
1147}
1148
Kevin Wolf5e5c4f62014-05-26 11:45:08 +02001149static QDict *parse_json_filename(const char *filename, Error **errp)
1150{
1151 QObject *options_obj;
1152 QDict *options;
1153 int ret;
1154
1155 ret = strstart(filename, "json:", &filename);
1156 assert(ret);
1157
1158 options_obj = qobject_from_json(filename);
1159 if (!options_obj) {
1160 error_setg(errp, "Could not parse the JSON options");
1161 return NULL;
1162 }
1163
1164 if (qobject_type(options_obj) != QTYPE_QDICT) {
1165 qobject_decref(options_obj);
1166 error_setg(errp, "Invalid JSON object given");
1167 return NULL;
1168 }
1169
1170 options = qobject_to_qdict(options_obj);
1171 qdict_flatten(options);
1172
1173 return options;
1174}
1175
Kevin Wolfde3b53f2015-10-29 15:24:41 +01001176static void parse_json_protocol(QDict *options, const char **pfilename,
1177 Error **errp)
1178{
1179 QDict *json_options;
1180 Error *local_err = NULL;
1181
1182 /* Parse json: pseudo-protocol */
1183 if (!*pfilename || !g_str_has_prefix(*pfilename, "json:")) {
1184 return;
1185 }
1186
1187 json_options = parse_json_filename(*pfilename, &local_err);
1188 if (local_err) {
1189 error_propagate(errp, local_err);
1190 return;
1191 }
1192
1193 /* Options given in the filename have lower priority than options
1194 * specified directly */
1195 qdict_join(options, json_options, false);
1196 QDECREF(json_options);
1197 *pfilename = NULL;
1198}
1199
Kevin Wolf57915332010-04-14 15:24:50 +02001200/*
Kevin Wolff54120f2014-05-26 11:09:59 +02001201 * Fills in default options for opening images and converts the legacy
1202 * filename/flags pair to option QDict entries.
Max Reitz53a29512015-03-19 14:53:16 -04001203 * The BDRV_O_PROTOCOL flag in *flags will be set or cleared accordingly if a
1204 * block driver has been specified explicitly.
Kevin Wolff54120f2014-05-26 11:09:59 +02001205 */
Kevin Wolfde3b53f2015-10-29 15:24:41 +01001206static int bdrv_fill_options(QDict **options, const char *filename,
Max Reitz053e1572015-08-26 19:47:51 +02001207 int *flags, Error **errp)
Kevin Wolff54120f2014-05-26 11:09:59 +02001208{
1209 const char *drvname;
Max Reitz53a29512015-03-19 14:53:16 -04001210 bool protocol = *flags & BDRV_O_PROTOCOL;
Kevin Wolff54120f2014-05-26 11:09:59 +02001211 bool parse_filename = false;
Max Reitz053e1572015-08-26 19:47:51 +02001212 BlockDriver *drv = NULL;
Kevin Wolff54120f2014-05-26 11:09:59 +02001213 Error *local_err = NULL;
Kevin Wolff54120f2014-05-26 11:09:59 +02001214
Max Reitz53a29512015-03-19 14:53:16 -04001215 drvname = qdict_get_try_str(*options, "driver");
Max Reitz053e1572015-08-26 19:47:51 +02001216 if (drvname) {
1217 drv = bdrv_find_format(drvname);
1218 if (!drv) {
1219 error_setg(errp, "Unknown driver '%s'", drvname);
1220 return -ENOENT;
1221 }
1222 /* If the user has explicitly specified the driver, this choice should
1223 * override the BDRV_O_PROTOCOL flag */
1224 protocol = drv->bdrv_file_open;
Max Reitz53a29512015-03-19 14:53:16 -04001225 }
1226
1227 if (protocol) {
1228 *flags |= BDRV_O_PROTOCOL;
1229 } else {
1230 *flags &= ~BDRV_O_PROTOCOL;
1231 }
1232
Kevin Wolf91a097e2015-05-08 17:49:53 +02001233 /* Translate cache options from flags into options */
1234 update_options_from_flags(*options, *flags);
1235
Kevin Wolff54120f2014-05-26 11:09:59 +02001236 /* Fetch the file name from the options QDict if necessary */
Kevin Wolf17b005f2014-05-27 10:50:29 +02001237 if (protocol && filename) {
Kevin Wolff54120f2014-05-26 11:09:59 +02001238 if (!qdict_haskey(*options, "filename")) {
1239 qdict_put(*options, "filename", qstring_from_str(filename));
1240 parse_filename = true;
1241 } else {
1242 error_setg(errp, "Can't specify 'file' and 'filename' options at "
1243 "the same time");
1244 return -EINVAL;
1245 }
1246 }
1247
1248 /* Find the right block driver */
1249 filename = qdict_get_try_str(*options, "filename");
Kevin Wolff54120f2014-05-26 11:09:59 +02001250
Max Reitz053e1572015-08-26 19:47:51 +02001251 if (!drvname && protocol) {
1252 if (filename) {
1253 drv = bdrv_find_protocol(filename, parse_filename, errp);
1254 if (!drv) {
Kevin Wolff54120f2014-05-26 11:09:59 +02001255 return -EINVAL;
1256 }
Max Reitz053e1572015-08-26 19:47:51 +02001257
1258 drvname = drv->format_name;
1259 qdict_put(*options, "driver", qstring_from_str(drvname));
1260 } else {
1261 error_setg(errp, "Must specify either driver or file");
1262 return -EINVAL;
Kevin Wolff54120f2014-05-26 11:09:59 +02001263 }
1264 }
1265
Kevin Wolf17b005f2014-05-27 10:50:29 +02001266 assert(drv || !protocol);
Kevin Wolff54120f2014-05-26 11:09:59 +02001267
1268 /* Driver-specific filename parsing */
Kevin Wolf17b005f2014-05-27 10:50:29 +02001269 if (drv && drv->bdrv_parse_filename && parse_filename) {
Kevin Wolff54120f2014-05-26 11:09:59 +02001270 drv->bdrv_parse_filename(filename, *options, &local_err);
1271 if (local_err) {
1272 error_propagate(errp, local_err);
1273 return -EINVAL;
1274 }
1275
1276 if (!drv->bdrv_needs_filename) {
1277 qdict_del(*options, "filename");
1278 }
1279 }
1280
1281 return 0;
1282}
1283
Kevin Wolfe9740bc2016-05-23 15:52:26 +02001284static void bdrv_replace_child(BdrvChild *child, BlockDriverState *new_bs)
1285{
1286 BlockDriverState *old_bs = child->bs;
1287
1288 if (old_bs) {
Kevin Wolf36fe1332016-05-17 14:51:55 +02001289 if (old_bs->quiesce_counter && child->role->drained_end) {
1290 child->role->drained_end(child);
1291 }
Kevin Wolfe9740bc2016-05-23 15:52:26 +02001292 QLIST_REMOVE(child, next_parent);
1293 }
Kevin Wolfe9740bc2016-05-23 15:52:26 +02001294
1295 child->bs = new_bs;
Kevin Wolf36fe1332016-05-17 14:51:55 +02001296
1297 if (new_bs) {
1298 QLIST_INSERT_HEAD(&new_bs->parents, child, next_parent);
1299 if (new_bs->quiesce_counter && child->role->drained_begin) {
1300 child->role->drained_begin(child);
1301 }
1302 }
Kevin Wolfe9740bc2016-05-23 15:52:26 +02001303}
1304
Kevin Wolff21d96d2016-03-08 13:47:46 +01001305BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs,
1306 const char *child_name,
Kevin Wolf36fe1332016-05-17 14:51:55 +02001307 const BdrvChildRole *child_role,
1308 void *opaque)
Kevin Wolfdf581792015-06-15 11:53:47 +02001309{
1310 BdrvChild *child = g_new(BdrvChild, 1);
1311 *child = (BdrvChild) {
Kevin Wolfe9740bc2016-05-23 15:52:26 +02001312 .bs = NULL,
Kevin Wolf260fecf2015-04-27 13:46:22 +02001313 .name = g_strdup(child_name),
Kevin Wolfdf581792015-06-15 11:53:47 +02001314 .role = child_role,
Kevin Wolf36fe1332016-05-17 14:51:55 +02001315 .opaque = opaque,
Kevin Wolfdf581792015-06-15 11:53:47 +02001316 };
1317
Kevin Wolfe9740bc2016-05-23 15:52:26 +02001318 bdrv_replace_child(child, child_bs);
Kevin Wolfb4b059f2015-06-15 13:24:19 +02001319
1320 return child;
Kevin Wolfdf581792015-06-15 11:53:47 +02001321}
1322
Wen Congyang98292c62016-05-10 15:36:38 +08001323BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs,
1324 BlockDriverState *child_bs,
1325 const char *child_name,
1326 const BdrvChildRole *child_role)
Kevin Wolff21d96d2016-03-08 13:47:46 +01001327{
Kevin Wolf36fe1332016-05-17 14:51:55 +02001328 BdrvChild *child = bdrv_root_attach_child(child_bs, child_name, child_role,
Kevin Wolf20018e12016-05-23 18:46:59 +02001329 parent_bs);
Kevin Wolff21d96d2016-03-08 13:47:46 +01001330 QLIST_INSERT_HEAD(&parent_bs->children, child, next);
1331 return child;
1332}
1333
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001334static void bdrv_detach_child(BdrvChild *child)
Kevin Wolf33a60402015-06-15 13:51:04 +02001335{
Kevin Wolff21d96d2016-03-08 13:47:46 +01001336 if (child->next.le_prev) {
1337 QLIST_REMOVE(child, next);
1338 child->next.le_prev = NULL;
1339 }
Kevin Wolfe9740bc2016-05-23 15:52:26 +02001340
1341 bdrv_replace_child(child, NULL);
1342
Kevin Wolf260fecf2015-04-27 13:46:22 +02001343 g_free(child->name);
Kevin Wolf33a60402015-06-15 13:51:04 +02001344 g_free(child);
1345}
1346
Kevin Wolff21d96d2016-03-08 13:47:46 +01001347void bdrv_root_unref_child(BdrvChild *child)
Kevin Wolf33a60402015-06-15 13:51:04 +02001348{
Kevin Wolf779020c2015-10-13 14:09:44 +02001349 BlockDriverState *child_bs;
1350
Kevin Wolff21d96d2016-03-08 13:47:46 +01001351 child_bs = child->bs;
1352 bdrv_detach_child(child);
1353 bdrv_unref(child_bs);
1354}
1355
1356void bdrv_unref_child(BlockDriverState *parent, BdrvChild *child)
1357{
Kevin Wolf779020c2015-10-13 14:09:44 +02001358 if (child == NULL) {
1359 return;
1360 }
Kevin Wolf33a60402015-06-15 13:51:04 +02001361
1362 if (child->bs->inherits_from == parent) {
Kevin Wolf4e4bf5c2016-12-16 18:52:37 +01001363 BdrvChild *c;
1364
1365 /* Remove inherits_from only when the last reference between parent and
1366 * child->bs goes away. */
1367 QLIST_FOREACH(c, &parent->children, next) {
1368 if (c != child && c->bs == child->bs) {
1369 break;
1370 }
1371 }
1372 if (c == NULL) {
1373 child->bs->inherits_from = NULL;
1374 }
Kevin Wolf33a60402015-06-15 13:51:04 +02001375 }
1376
Kevin Wolff21d96d2016-03-08 13:47:46 +01001377 bdrv_root_unref_child(child);
Kevin Wolf33a60402015-06-15 13:51:04 +02001378}
1379
Kevin Wolf5c8cab42016-02-24 15:13:35 +01001380
1381static void bdrv_parent_cb_change_media(BlockDriverState *bs, bool load)
1382{
1383 BdrvChild *c;
1384 QLIST_FOREACH(c, &bs->parents, next_parent) {
1385 if (c->role->change_media) {
1386 c->role->change_media(c, load);
1387 }
1388 }
1389}
1390
1391static void bdrv_parent_cb_resize(BlockDriverState *bs)
1392{
1393 BdrvChild *c;
1394 QLIST_FOREACH(c, &bs->parents, next_parent) {
1395 if (c->role->resize) {
1396 c->role->resize(c);
1397 }
1398 }
1399}
1400
Kevin Wolf5db15a52015-09-14 15:33:33 +02001401/*
1402 * Sets the backing file link of a BDS. A new reference is created; callers
1403 * which don't need their own reference any more must call bdrv_unref().
1404 */
Fam Zheng8d24cce2014-05-23 21:29:45 +08001405void bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd)
1406{
Kevin Wolf5db15a52015-09-14 15:33:33 +02001407 if (backing_hd) {
1408 bdrv_ref(backing_hd);
1409 }
Fam Zheng8d24cce2014-05-23 21:29:45 +08001410
Kevin Wolf760e0062015-06-17 14:55:21 +02001411 if (bs->backing) {
Fam Zheng826b6ca2014-05-23 21:29:47 +08001412 assert(bs->backing_blocker);
Kevin Wolf760e0062015-06-17 14:55:21 +02001413 bdrv_op_unblock_all(bs->backing->bs, bs->backing_blocker);
Kevin Wolf5db15a52015-09-14 15:33:33 +02001414 bdrv_unref_child(bs, bs->backing);
Fam Zheng826b6ca2014-05-23 21:29:47 +08001415 } else if (backing_hd) {
1416 error_setg(&bs->backing_blocker,
Alberto Garcia81e5f782015-04-08 12:29:19 +03001417 "node is used as backing hd of '%s'",
1418 bdrv_get_device_or_node_name(bs));
Fam Zheng826b6ca2014-05-23 21:29:47 +08001419 }
1420
Fam Zheng8d24cce2014-05-23 21:29:45 +08001421 if (!backing_hd) {
Fam Zheng826b6ca2014-05-23 21:29:47 +08001422 error_free(bs->backing_blocker);
1423 bs->backing_blocker = NULL;
Kevin Wolf760e0062015-06-17 14:55:21 +02001424 bs->backing = NULL;
Fam Zheng8d24cce2014-05-23 21:29:45 +08001425 goto out;
1426 }
Kevin Wolf260fecf2015-04-27 13:46:22 +02001427 bs->backing = bdrv_attach_child(bs, backing_hd, "backing", &child_backing);
Fam Zheng8d24cce2014-05-23 21:29:45 +08001428 bs->open_flags &= ~BDRV_O_NO_BACKING;
1429 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_hd->filename);
1430 pstrcpy(bs->backing_format, sizeof(bs->backing_format),
1431 backing_hd->drv ? backing_hd->drv->format_name : "");
Fam Zheng826b6ca2014-05-23 21:29:47 +08001432
Kevin Wolf760e0062015-06-17 14:55:21 +02001433 bdrv_op_block_all(backing_hd, bs->backing_blocker);
Alberto Garcia61b49e42016-10-28 10:08:10 +03001434 /* Otherwise we won't be able to commit or stream */
Kevin Wolf760e0062015-06-17 14:55:21 +02001435 bdrv_op_unblock(backing_hd, BLOCK_OP_TYPE_COMMIT_TARGET,
Fam Zheng826b6ca2014-05-23 21:29:47 +08001436 bs->backing_blocker);
Alberto Garcia61b49e42016-10-28 10:08:10 +03001437 bdrv_op_unblock(backing_hd, BLOCK_OP_TYPE_STREAM,
1438 bs->backing_blocker);
Wen Congyange9d64562016-07-27 15:01:42 +08001439 /*
1440 * We do backup in 3 ways:
1441 * 1. drive backup
1442 * The target bs is new opened, and the source is top BDS
1443 * 2. blockdev backup
1444 * Both the source and the target are top BDSes.
1445 * 3. internal backup(used for block replication)
1446 * Both the source and the target are backing file
1447 *
1448 * In case 1 and 2, neither the source nor the target is the backing file.
1449 * In case 3, we will block the top BDS, so there is only one block job
1450 * for the top BDS and its backing chain.
1451 */
1452 bdrv_op_unblock(backing_hd, BLOCK_OP_TYPE_BACKUP_SOURCE,
1453 bs->backing_blocker);
1454 bdrv_op_unblock(backing_hd, BLOCK_OP_TYPE_BACKUP_TARGET,
1455 bs->backing_blocker);
Fam Zheng8d24cce2014-05-23 21:29:45 +08001456out:
Kevin Wolf3baca892014-07-16 17:48:16 +02001457 bdrv_refresh_limits(bs, NULL);
Fam Zheng8d24cce2014-05-23 21:29:45 +08001458}
1459
Kevin Wolf31ca6d02013-03-28 15:29:24 +01001460/*
1461 * Opens the backing file for a BlockDriverState if not yet open
1462 *
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001463 * bdref_key specifies the key for the image's BlockdevRef in the options QDict.
1464 * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict
1465 * itself, all options starting with "${bdref_key}." are considered part of the
1466 * BlockdevRef.
1467 *
1468 * TODO Can this be unified with bdrv_open_image()?
Kevin Wolf31ca6d02013-03-28 15:29:24 +01001469 */
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001470int bdrv_open_backing_file(BlockDriverState *bs, QDict *parent_options,
1471 const char *bdref_key, Error **errp)
Paolo Bonzini9156df12012-10-18 16:49:17 +02001472{
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001473 char *backing_filename = g_malloc0(PATH_MAX);
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001474 char *bdref_key_dot;
1475 const char *reference = NULL;
Kevin Wolf317fc442014-04-25 13:27:34 +02001476 int ret = 0;
Fam Zheng8d24cce2014-05-23 21:29:45 +08001477 BlockDriverState *backing_hd;
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001478 QDict *options;
1479 QDict *tmp_parent_options = NULL;
Max Reitz34b5d2c2013-09-05 14:45:29 +02001480 Error *local_err = NULL;
Paolo Bonzini9156df12012-10-18 16:49:17 +02001481
Kevin Wolf760e0062015-06-17 14:55:21 +02001482 if (bs->backing != NULL) {
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001483 goto free_exit;
Paolo Bonzini9156df12012-10-18 16:49:17 +02001484 }
1485
Kevin Wolf31ca6d02013-03-28 15:29:24 +01001486 /* NULL means an empty set of options */
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001487 if (parent_options == NULL) {
1488 tmp_parent_options = qdict_new();
1489 parent_options = tmp_parent_options;
Kevin Wolf31ca6d02013-03-28 15:29:24 +01001490 }
1491
Paolo Bonzini9156df12012-10-18 16:49:17 +02001492 bs->open_flags &= ~BDRV_O_NO_BACKING;
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001493
1494 bdref_key_dot = g_strdup_printf("%s.", bdref_key);
1495 qdict_extract_subqdict(parent_options, &options, bdref_key_dot);
1496 g_free(bdref_key_dot);
1497
1498 reference = qdict_get_try_str(parent_options, bdref_key);
1499 if (reference || qdict_haskey(options, "file.filename")) {
Kevin Wolf1cb6f502013-04-12 20:27:07 +02001500 backing_filename[0] = '\0';
1501 } else if (bs->backing_file[0] == '\0' && qdict_size(options) == 0) {
Kevin Wolf31ca6d02013-03-28 15:29:24 +01001502 QDECREF(options);
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001503 goto free_exit;
Fam Zhengdbecebd2013-09-22 20:05:06 +08001504 } else {
Max Reitz9f074292014-11-26 17:20:26 +01001505 bdrv_get_full_backing_filename(bs, backing_filename, PATH_MAX,
1506 &local_err);
1507 if (local_err) {
1508 ret = -EINVAL;
1509 error_propagate(errp, local_err);
1510 QDECREF(options);
1511 goto free_exit;
1512 }
Paolo Bonzini9156df12012-10-18 16:49:17 +02001513 }
1514
Kevin Wolf8ee79e72014-06-04 15:09:35 +02001515 if (!bs->drv || !bs->drv->supports_backing) {
1516 ret = -EINVAL;
1517 error_setg(errp, "Driver doesn't support backing files");
1518 QDECREF(options);
1519 goto free_exit;
1520 }
1521
Kevin Wolfc5f6e492014-11-25 18:12:42 +01001522 if (bs->backing_format[0] != '\0' && !qdict_haskey(options, "driver")) {
1523 qdict_put(options, "driver", qstring_from_str(bs->backing_format));
Paolo Bonzini9156df12012-10-18 16:49:17 +02001524 }
1525
Max Reitz5b363932016-05-17 16:41:31 +02001526 backing_hd = bdrv_open_inherit(*backing_filename ? backing_filename : NULL,
1527 reference, options, 0, bs, &child_backing,
1528 errp);
1529 if (!backing_hd) {
Paolo Bonzini9156df12012-10-18 16:49:17 +02001530 bs->open_flags |= BDRV_O_NO_BACKING;
Markus Armbrustere43bfd92015-12-18 16:35:15 +01001531 error_prepend(errp, "Could not open backing file: ");
Max Reitz5b363932016-05-17 16:41:31 +02001532 ret = -EINVAL;
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001533 goto free_exit;
Paolo Bonzini9156df12012-10-18 16:49:17 +02001534 }
Kevin Wolfdf581792015-06-15 11:53:47 +02001535
Kevin Wolf5db15a52015-09-14 15:33:33 +02001536 /* Hook up the backing file link; drop our reference, bs owns the
1537 * backing_hd reference now */
Fam Zheng8d24cce2014-05-23 21:29:45 +08001538 bdrv_set_backing_hd(bs, backing_hd);
Kevin Wolf5db15a52015-09-14 15:33:33 +02001539 bdrv_unref(backing_hd);
Peter Feinerd80ac652014-01-08 19:43:25 +00001540
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001541 qdict_del(parent_options, bdref_key);
1542
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001543free_exit:
1544 g_free(backing_filename);
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001545 QDECREF(tmp_parent_options);
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001546 return ret;
Paolo Bonzini9156df12012-10-18 16:49:17 +02001547}
1548
Kevin Wolf2d6b86a2017-02-17 17:43:59 +01001549static BlockDriverState *
1550bdrv_open_child_bs(const char *filename, QDict *options, const char *bdref_key,
1551 BlockDriverState *parent, const BdrvChildRole *child_role,
1552 bool allow_none, Error **errp)
Max Reitzda557aa2013-12-20 19:28:11 +01001553{
Kevin Wolf2d6b86a2017-02-17 17:43:59 +01001554 BlockDriverState *bs = NULL;
Max Reitzda557aa2013-12-20 19:28:11 +01001555 QDict *image_options;
Max Reitzda557aa2013-12-20 19:28:11 +01001556 char *bdref_key_dot;
1557 const char *reference;
1558
Kevin Wolfdf581792015-06-15 11:53:47 +02001559 assert(child_role != NULL);
Max Reitzf67503e2014-02-18 18:33:05 +01001560
Max Reitzda557aa2013-12-20 19:28:11 +01001561 bdref_key_dot = g_strdup_printf("%s.", bdref_key);
1562 qdict_extract_subqdict(options, &image_options, bdref_key_dot);
1563 g_free(bdref_key_dot);
1564
1565 reference = qdict_get_try_str(options, bdref_key);
1566 if (!filename && !reference && !qdict_size(image_options)) {
Kevin Wolfb4b059f2015-06-15 13:24:19 +02001567 if (!allow_none) {
Max Reitzda557aa2013-12-20 19:28:11 +01001568 error_setg(errp, "A block device must be specified for \"%s\"",
1569 bdref_key);
Max Reitzda557aa2013-12-20 19:28:11 +01001570 }
Markus Armbrusterb20e61e2014-05-28 11:16:57 +02001571 QDECREF(image_options);
Max Reitzda557aa2013-12-20 19:28:11 +01001572 goto done;
1573 }
1574
Max Reitz5b363932016-05-17 16:41:31 +02001575 bs = bdrv_open_inherit(filename, reference, image_options, 0,
1576 parent, child_role, errp);
1577 if (!bs) {
Kevin Wolfdf581792015-06-15 11:53:47 +02001578 goto done;
1579 }
1580
Max Reitzda557aa2013-12-20 19:28:11 +01001581done:
1582 qdict_del(options, bdref_key);
Kevin Wolf2d6b86a2017-02-17 17:43:59 +01001583 return bs;
1584}
1585
1586/*
1587 * Opens a disk image whose options are given as BlockdevRef in another block
1588 * device's options.
1589 *
1590 * If allow_none is true, no image will be opened if filename is false and no
1591 * BlockdevRef is given. NULL will be returned, but errp remains unset.
1592 *
1593 * bdrev_key specifies the key for the image's BlockdevRef in the options QDict.
1594 * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict
1595 * itself, all options starting with "${bdref_key}." are considered part of the
1596 * BlockdevRef.
1597 *
1598 * The BlockdevRef will be removed from the options QDict.
1599 */
1600BdrvChild *bdrv_open_child(const char *filename,
1601 QDict *options, const char *bdref_key,
1602 BlockDriverState *parent,
1603 const BdrvChildRole *child_role,
1604 bool allow_none, Error **errp)
1605{
1606 BlockDriverState *bs;
1607
1608 bs = bdrv_open_child_bs(filename, options, bdref_key, parent, child_role,
1609 allow_none, errp);
1610 if (bs == NULL) {
1611 return NULL;
1612 }
1613
1614 return bdrv_attach_child(parent, bs, bdref_key, child_role);
Kevin Wolfb4b059f2015-06-15 13:24:19 +02001615}
1616
Max Reitz66836182016-05-17 16:41:27 +02001617static BlockDriverState *bdrv_append_temp_snapshot(BlockDriverState *bs,
1618 int flags,
1619 QDict *snapshot_options,
1620 Error **errp)
Kevin Wolfb9988752014-04-03 12:09:34 +02001621{
1622 /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001623 char *tmp_filename = g_malloc0(PATH_MAX + 1);
Kevin Wolfb9988752014-04-03 12:09:34 +02001624 int64_t total_size;
Chunyan Liu83d05212014-06-05 17:20:51 +08001625 QemuOpts *opts = NULL;
Kevin Wolfb9988752014-04-03 12:09:34 +02001626 BlockDriverState *bs_snapshot;
Kevin Wolfb9988752014-04-03 12:09:34 +02001627 int ret;
1628
1629 /* if snapshot, we create a temporary backing file and open it
1630 instead of opening 'filename' directly */
1631
1632 /* Get the required size from the image */
Kevin Wolff1877432014-04-04 17:07:19 +02001633 total_size = bdrv_getlength(bs);
1634 if (total_size < 0) {
1635 error_setg_errno(errp, -total_size, "Could not get image size");
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001636 goto out;
Kevin Wolff1877432014-04-04 17:07:19 +02001637 }
Kevin Wolfb9988752014-04-03 12:09:34 +02001638
1639 /* Create the temporary image */
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001640 ret = get_tmp_filename(tmp_filename, PATH_MAX + 1);
Kevin Wolfb9988752014-04-03 12:09:34 +02001641 if (ret < 0) {
1642 error_setg_errno(errp, -ret, "Could not get temporary filename");
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001643 goto out;
Kevin Wolfb9988752014-04-03 12:09:34 +02001644 }
1645
Max Reitzef810432014-12-02 18:32:42 +01001646 opts = qemu_opts_create(bdrv_qcow2.create_opts, NULL, 0,
Chunyan Liuc282e1f2014-06-05 17:21:11 +08001647 &error_abort);
Markus Armbruster39101f22015-02-12 16:46:36 +01001648 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_size, &error_abort);
Markus Armbrustere43bfd92015-12-18 16:35:15 +01001649 ret = bdrv_create(&bdrv_qcow2, tmp_filename, opts, errp);
Chunyan Liu83d05212014-06-05 17:20:51 +08001650 qemu_opts_del(opts);
Kevin Wolfb9988752014-04-03 12:09:34 +02001651 if (ret < 0) {
Markus Armbrustere43bfd92015-12-18 16:35:15 +01001652 error_prepend(errp, "Could not create temporary overlay '%s': ",
1653 tmp_filename);
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001654 goto out;
Kevin Wolfb9988752014-04-03 12:09:34 +02001655 }
1656
Kevin Wolf73176be2016-03-07 13:02:15 +01001657 /* Prepare options QDict for the temporary file */
Kevin Wolfb9988752014-04-03 12:09:34 +02001658 qdict_put(snapshot_options, "file.driver",
1659 qstring_from_str("file"));
1660 qdict_put(snapshot_options, "file.filename",
1661 qstring_from_str(tmp_filename));
Max Reitze6641712015-08-26 19:47:48 +02001662 qdict_put(snapshot_options, "driver",
1663 qstring_from_str("qcow2"));
Kevin Wolfb9988752014-04-03 12:09:34 +02001664
Max Reitz5b363932016-05-17 16:41:31 +02001665 bs_snapshot = bdrv_open(NULL, NULL, snapshot_options, flags, errp);
Kevin Wolf73176be2016-03-07 13:02:15 +01001666 snapshot_options = NULL;
Max Reitz5b363932016-05-17 16:41:31 +02001667 if (!bs_snapshot) {
1668 ret = -EINVAL;
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001669 goto out;
Kevin Wolfb9988752014-04-03 12:09:34 +02001670 }
1671
Max Reitz66836182016-05-17 16:41:27 +02001672 /* bdrv_append() consumes a strong reference to bs_snapshot (i.e. it will
1673 * call bdrv_unref() on it), so in order to be able to return one, we have
1674 * to increase bs_snapshot's refcount here */
1675 bdrv_ref(bs_snapshot);
Kevin Wolfb9988752014-04-03 12:09:34 +02001676 bdrv_append(bs_snapshot, bs);
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001677
Max Reitz66836182016-05-17 16:41:27 +02001678 g_free(tmp_filename);
1679 return bs_snapshot;
1680
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001681out:
Kevin Wolf73176be2016-03-07 13:02:15 +01001682 QDECREF(snapshot_options);
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001683 g_free(tmp_filename);
Max Reitz66836182016-05-17 16:41:27 +02001684 return NULL;
Kevin Wolfb9988752014-04-03 12:09:34 +02001685}
1686
Max Reitzda557aa2013-12-20 19:28:11 +01001687/*
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001688 * Opens a disk image (raw, qcow2, vmdk, ...)
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001689 *
1690 * options is a QDict of options to pass to the block drivers, or NULL for an
1691 * empty set of options. The reference to the QDict belongs to the block layer
1692 * after the call (even on failure), so if the caller intends to reuse the
1693 * dictionary, it needs to use QINCREF() before calling bdrv_open.
Max Reitzf67503e2014-02-18 18:33:05 +01001694 *
1695 * If *pbs is NULL, a new BDS will be created with a pointer to it stored there.
1696 * If it is not NULL, the referenced BDS will be reused.
Max Reitzddf56362014-02-18 18:33:06 +01001697 *
1698 * The reference parameter may be used to specify an existing block device which
1699 * should be opened. If specified, neither options nor a filename may be given,
1700 * nor can an existing BDS be reused (that is, *pbs has to be NULL).
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001701 */
Max Reitz5b363932016-05-17 16:41:31 +02001702static BlockDriverState *bdrv_open_inherit(const char *filename,
1703 const char *reference,
1704 QDict *options, int flags,
1705 BlockDriverState *parent,
1706 const BdrvChildRole *child_role,
1707 Error **errp)
bellardea2384d2004-08-01 21:59:26 +00001708{
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001709 int ret;
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02001710 BdrvChild *file = NULL;
1711 BlockDriverState *bs;
Max Reitzce343772015-08-26 19:47:50 +02001712 BlockDriver *drv = NULL;
Kevin Wolf74fe54f2013-07-09 11:09:02 +02001713 const char *drvname;
Alberto Garcia3e8c2e52015-10-26 14:27:15 +02001714 const char *backing;
Max Reitz34b5d2c2013-09-05 14:45:29 +02001715 Error *local_err = NULL;
Kevin Wolf73176be2016-03-07 13:02:15 +01001716 QDict *snapshot_options = NULL;
Kevin Wolfb1e6fc02014-05-06 12:11:42 +02001717 int snapshot_flags = 0;
bellard712e7872005-04-28 21:09:32 +00001718
Kevin Wolff3930ed2015-04-08 13:43:47 +02001719 assert(!child_role || !flags);
1720 assert(!child_role == !parent);
Max Reitzf67503e2014-02-18 18:33:05 +01001721
Max Reitzddf56362014-02-18 18:33:06 +01001722 if (reference) {
1723 bool options_non_empty = options ? qdict_size(options) : false;
1724 QDECREF(options);
1725
Max Reitzddf56362014-02-18 18:33:06 +01001726 if (filename || options_non_empty) {
1727 error_setg(errp, "Cannot reference an existing block device with "
1728 "additional options or a new filename");
Max Reitz5b363932016-05-17 16:41:31 +02001729 return NULL;
Max Reitzddf56362014-02-18 18:33:06 +01001730 }
1731
1732 bs = bdrv_lookup_bs(reference, reference, errp);
1733 if (!bs) {
Max Reitz5b363932016-05-17 16:41:31 +02001734 return NULL;
Max Reitzddf56362014-02-18 18:33:06 +01001735 }
Kevin Wolf76b22322016-04-04 17:11:13 +02001736
Max Reitzddf56362014-02-18 18:33:06 +01001737 bdrv_ref(bs);
Max Reitz5b363932016-05-17 16:41:31 +02001738 return bs;
Max Reitzddf56362014-02-18 18:33:06 +01001739 }
1740
Max Reitz5b363932016-05-17 16:41:31 +02001741 bs = bdrv_new();
Max Reitzf67503e2014-02-18 18:33:05 +01001742
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001743 /* NULL means an empty set of options */
1744 if (options == NULL) {
1745 options = qdict_new();
1746 }
1747
Kevin Wolf145f5982015-05-08 16:15:03 +02001748 /* json: syntax counts as explicit options, as if in the QDict */
Kevin Wolfde3b53f2015-10-29 15:24:41 +01001749 parse_json_protocol(options, &filename, &local_err);
1750 if (local_err) {
Kevin Wolfde3b53f2015-10-29 15:24:41 +01001751 goto fail;
1752 }
1753
Kevin Wolf145f5982015-05-08 16:15:03 +02001754 bs->explicit_options = qdict_clone_shallow(options);
1755
Kevin Wolff3930ed2015-04-08 13:43:47 +02001756 if (child_role) {
Kevin Wolfbddcec32015-04-09 18:47:50 +02001757 bs->inherits_from = parent;
Kevin Wolf8e2160e2015-04-29 17:29:39 +02001758 child_role->inherit_options(&flags, options,
1759 parent->open_flags, parent->options);
Kevin Wolff3930ed2015-04-08 13:43:47 +02001760 }
1761
Kevin Wolfde3b53f2015-10-29 15:24:41 +01001762 ret = bdrv_fill_options(&options, filename, &flags, &local_err);
Kevin Wolf462f5bc2014-05-26 11:39:55 +02001763 if (local_err) {
1764 goto fail;
1765 }
1766
Alberto Garciaf87a0e22016-09-15 17:53:02 +03001767 /* Set the BDRV_O_RDWR and BDRV_O_ALLOW_RDWR flags.
1768 * FIXME: we're parsing the QDict to avoid having to create a
1769 * QemuOpts just for this, but neither option is optimal. */
1770 if (g_strcmp0(qdict_get_try_str(options, BDRV_OPT_READ_ONLY), "on") &&
1771 !qdict_get_try_bool(options, BDRV_OPT_READ_ONLY, false)) {
1772 flags |= (BDRV_O_RDWR | BDRV_O_ALLOW_RDWR);
1773 } else {
1774 flags &= ~BDRV_O_RDWR;
Alberto Garcia14499ea2016-09-15 17:53:00 +03001775 }
1776
1777 if (flags & BDRV_O_SNAPSHOT) {
1778 snapshot_options = qdict_new();
1779 bdrv_temp_snapshot_options(&snapshot_flags, snapshot_options,
1780 flags, options);
Alberto Garciaf87a0e22016-09-15 17:53:02 +03001781 /* Let bdrv_backing_options() override "read-only" */
1782 qdict_del(options, BDRV_OPT_READ_ONLY);
Alberto Garcia14499ea2016-09-15 17:53:00 +03001783 bdrv_backing_options(&flags, options, flags, options);
1784 }
1785
Kevin Wolf62392eb2015-04-24 16:38:02 +02001786 bs->open_flags = flags;
1787 bs->options = options;
1788 options = qdict_clone_shallow(options);
1789
Kevin Wolf76c591b2014-06-04 14:19:44 +02001790 /* Find the right image format driver */
Kevin Wolf76c591b2014-06-04 14:19:44 +02001791 drvname = qdict_get_try_str(options, "driver");
1792 if (drvname) {
1793 drv = bdrv_find_format(drvname);
Kevin Wolf76c591b2014-06-04 14:19:44 +02001794 if (!drv) {
1795 error_setg(errp, "Unknown driver: '%s'", drvname);
Kevin Wolf76c591b2014-06-04 14:19:44 +02001796 goto fail;
1797 }
1798 }
1799
1800 assert(drvname || !(flags & BDRV_O_PROTOCOL));
Kevin Wolf76c591b2014-06-04 14:19:44 +02001801
Alberto Garcia3e8c2e52015-10-26 14:27:15 +02001802 backing = qdict_get_try_str(options, "backing");
1803 if (backing && *backing == '\0') {
1804 flags |= BDRV_O_NO_BACKING;
1805 qdict_del(options, "backing");
1806 }
1807
Kevin Wolf4e4bf5c2016-12-16 18:52:37 +01001808 /* Open image file without format layer. This BdrvChild is only used for
1809 * probing, the block drivers will do their own bdrv_open_child() for the
1810 * same BDS, which is why we put the node name back into options. */
Kevin Wolff4788ad2014-06-03 16:44:19 +02001811 if ((flags & BDRV_O_PROTOCOL) == 0) {
Kevin Wolf4e4bf5c2016-12-16 18:52:37 +01001812 /* FIXME Shouldn't attach a child to a node that isn't opened yet. */
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02001813 file = bdrv_open_child(filename, options, "file", bs,
1814 &child_file, true, &local_err);
Kevin Wolf1fdd6932015-06-15 14:11:51 +02001815 if (local_err) {
Max Reitz5469a2a2014-02-18 18:33:10 +01001816 goto fail;
1817 }
Kevin Wolf4e4bf5c2016-12-16 18:52:37 +01001818 if (file != NULL) {
1819 qdict_put(options, "file",
1820 qstring_from_str(bdrv_get_node_name(file->bs)));
1821 }
Max Reitz5469a2a2014-02-18 18:33:10 +01001822 }
1823
Kevin Wolf76c591b2014-06-04 14:19:44 +02001824 /* Image format probing */
Kevin Wolf38f3ef52014-11-20 16:27:12 +01001825 bs->probed = !drv;
Kevin Wolf76c591b2014-06-04 14:19:44 +02001826 if (!drv && file) {
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +02001827 ret = find_image_format(file, filename, &drv, &local_err);
Kevin Wolf17b005f2014-05-27 10:50:29 +02001828 if (ret < 0) {
Kevin Wolf8bfea152014-04-11 19:16:36 +02001829 goto fail;
Max Reitz2a05cbe2013-12-20 19:28:10 +01001830 }
Kevin Wolf62392eb2015-04-24 16:38:02 +02001831 /*
1832 * This option update would logically belong in bdrv_fill_options(),
1833 * but we first need to open bs->file for the probing to work, while
1834 * opening bs->file already requires the (mostly) final set of options
1835 * so that cache mode etc. can be inherited.
1836 *
1837 * Adding the driver later is somewhat ugly, but it's not an option
1838 * that would ever be inherited, so it's correct. We just need to make
1839 * sure to update both bs->options (which has the full effective
1840 * options for bs) and options (which has file.* already removed).
1841 */
1842 qdict_put(bs->options, "driver", qstring_from_str(drv->format_name));
1843 qdict_put(options, "driver", qstring_from_str(drv->format_name));
Kevin Wolf76c591b2014-06-04 14:19:44 +02001844 } else if (!drv) {
Kevin Wolf17b005f2014-05-27 10:50:29 +02001845 error_setg(errp, "Must specify either driver or file");
Kevin Wolf8bfea152014-04-11 19:16:36 +02001846 goto fail;
Kevin Wolff500a6d2012-11-12 17:35:27 +01001847 }
1848
Max Reitz53a29512015-03-19 14:53:16 -04001849 /* BDRV_O_PROTOCOL must be set iff a protocol BDS is about to be created */
1850 assert(!!(flags & BDRV_O_PROTOCOL) == !!drv->bdrv_file_open);
1851 /* file must be NULL if a protocol BDS is about to be created
1852 * (the inverse results in an error message from bdrv_open_common()) */
1853 assert(!(flags & BDRV_O_PROTOCOL) || !file);
1854
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001855 /* Open the image */
Kevin Wolf82dc8b42016-01-11 19:07:50 +01001856 ret = bdrv_open_common(bs, file, options, &local_err);
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001857 if (ret < 0) {
Kevin Wolf8bfea152014-04-11 19:16:36 +02001858 goto fail;
Christoph Hellwig69873072010-01-20 18:13:25 +01001859 }
1860
Kevin Wolf4e4bf5c2016-12-16 18:52:37 +01001861 if (file) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02001862 bdrv_unref_child(bs, file);
Kevin Wolff500a6d2012-11-12 17:35:27 +01001863 file = NULL;
1864 }
1865
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001866 /* If there is a backing file, use it */
Paolo Bonzini9156df12012-10-18 16:49:17 +02001867 if ((flags & BDRV_O_NO_BACKING) == 0) {
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001868 ret = bdrv_open_backing_file(bs, options, "backing", &local_err);
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001869 if (ret < 0) {
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001870 goto close_and_fail;
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001871 }
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001872 }
1873
Max Reitz91af7012014-07-18 20:24:56 +02001874 bdrv_refresh_filename(bs);
1875
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001876 /* Check if any unknown options were used */
Paolo Bonzini7ad27572017-01-04 15:59:14 +01001877 if (qdict_size(options) != 0) {
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001878 const QDictEntry *entry = qdict_first(options);
Max Reitz5acd9d82014-02-18 18:33:11 +01001879 if (flags & BDRV_O_PROTOCOL) {
1880 error_setg(errp, "Block protocol '%s' doesn't support the option "
1881 "'%s'", drv->format_name, entry->key);
1882 } else {
Max Reitzd0e46a52016-03-16 19:54:34 +01001883 error_setg(errp,
1884 "Block format '%s' does not support the option '%s'",
1885 drv->format_name, entry->key);
Max Reitz5acd9d82014-02-18 18:33:11 +01001886 }
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001887
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001888 goto close_and_fail;
1889 }
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001890
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001891 if (!bdrv_key_required(bs)) {
Kevin Wolf5c8cab42016-02-24 15:13:35 +01001892 bdrv_parent_cb_change_media(bs, true);
Markus Armbrusterc3adb582014-03-14 09:22:48 +01001893 } else if (!runstate_check(RUN_STATE_PRELAUNCH)
1894 && !runstate_check(RUN_STATE_INMIGRATE)
1895 && !runstate_check(RUN_STATE_PAUSED)) { /* HACK */
1896 error_setg(errp,
1897 "Guest must be stopped for opening of encrypted image");
Markus Armbrusterc3adb582014-03-14 09:22:48 +01001898 goto close_and_fail;
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001899 }
1900
Markus Armbrusterc3adb582014-03-14 09:22:48 +01001901 QDECREF(options);
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02001902
1903 /* For snapshot=on, create a temporary qcow2 overlay. bs points to the
1904 * temporary snapshot afterwards. */
1905 if (snapshot_flags) {
Max Reitz66836182016-05-17 16:41:27 +02001906 BlockDriverState *snapshot_bs;
1907 snapshot_bs = bdrv_append_temp_snapshot(bs, snapshot_flags,
1908 snapshot_options, &local_err);
Kevin Wolf73176be2016-03-07 13:02:15 +01001909 snapshot_options = NULL;
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02001910 if (local_err) {
1911 goto close_and_fail;
1912 }
Max Reitz5b363932016-05-17 16:41:31 +02001913 /* We are not going to return bs but the overlay on top of it
1914 * (snapshot_bs); thus, we have to drop the strong reference to bs
1915 * (which we obtained by calling bdrv_new()). bs will not be deleted,
1916 * though, because the overlay still has a reference to it. */
1917 bdrv_unref(bs);
1918 bs = snapshot_bs;
Max Reitz66836182016-05-17 16:41:27 +02001919 }
1920
Max Reitz5b363932016-05-17 16:41:31 +02001921 return bs;
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001922
Kevin Wolf8bfea152014-04-11 19:16:36 +02001923fail:
Kevin Wolff500a6d2012-11-12 17:35:27 +01001924 if (file != NULL) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02001925 bdrv_unref_child(bs, file);
Kevin Wolff500a6d2012-11-12 17:35:27 +01001926 }
Kevin Wolf4e4bf5c2016-12-16 18:52:37 +01001927 if (bs->file != NULL) {
1928 bdrv_unref_child(bs, bs->file);
1929 }
Kevin Wolf73176be2016-03-07 13:02:15 +01001930 QDECREF(snapshot_options);
Kevin Wolf145f5982015-05-08 16:15:03 +02001931 QDECREF(bs->explicit_options);
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001932 QDECREF(bs->options);
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001933 QDECREF(options);
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001934 bs->options = NULL;
Max Reitz5b363932016-05-17 16:41:31 +02001935 bdrv_unref(bs);
Eduardo Habkost621ff942016-06-13 18:57:56 -03001936 error_propagate(errp, local_err);
Max Reitz5b363932016-05-17 16:41:31 +02001937 return NULL;
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001938
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001939close_and_fail:
Max Reitz5b363932016-05-17 16:41:31 +02001940 bdrv_unref(bs);
Kevin Wolf73176be2016-03-07 13:02:15 +01001941 QDECREF(snapshot_options);
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001942 QDECREF(options);
Eduardo Habkost621ff942016-06-13 18:57:56 -03001943 error_propagate(errp, local_err);
Max Reitz5b363932016-05-17 16:41:31 +02001944 return NULL;
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001945}
1946
Max Reitz5b363932016-05-17 16:41:31 +02001947BlockDriverState *bdrv_open(const char *filename, const char *reference,
1948 QDict *options, int flags, Error **errp)
Kevin Wolff3930ed2015-04-08 13:43:47 +02001949{
Max Reitz5b363932016-05-17 16:41:31 +02001950 return bdrv_open_inherit(filename, reference, options, flags, NULL,
Max Reitzce343772015-08-26 19:47:50 +02001951 NULL, errp);
Kevin Wolff3930ed2015-04-08 13:43:47 +02001952}
1953
Jeff Codye971aa12012-09-20 15:13:19 -04001954typedef struct BlockReopenQueueEntry {
1955 bool prepared;
1956 BDRVReopenState state;
1957 QSIMPLEQ_ENTRY(BlockReopenQueueEntry) entry;
1958} BlockReopenQueueEntry;
1959
1960/*
1961 * Adds a BlockDriverState to a simple queue for an atomic, transactional
1962 * reopen of multiple devices.
1963 *
1964 * bs_queue can either be an existing BlockReopenQueue that has had QSIMPLE_INIT
1965 * already performed, or alternatively may be NULL a new BlockReopenQueue will
1966 * be created and initialized. This newly created BlockReopenQueue should be
1967 * passed back in for subsequent calls that are intended to be of the same
1968 * atomic 'set'.
1969 *
1970 * bs is the BlockDriverState to add to the reopen queue.
1971 *
Kevin Wolf4d2cb092015-04-10 17:50:50 +02001972 * options contains the changed options for the associated bs
1973 * (the BlockReopenQueue takes ownership)
1974 *
Jeff Codye971aa12012-09-20 15:13:19 -04001975 * flags contains the open flags for the associated bs
1976 *
1977 * returns a pointer to bs_queue, which is either the newly allocated
1978 * bs_queue, or the existing bs_queue being used.
1979 *
1980 */
Kevin Wolf28518102015-05-08 17:07:31 +02001981static BlockReopenQueue *bdrv_reopen_queue_child(BlockReopenQueue *bs_queue,
1982 BlockDriverState *bs,
1983 QDict *options,
1984 int flags,
1985 const BdrvChildRole *role,
1986 QDict *parent_options,
1987 int parent_flags)
Jeff Codye971aa12012-09-20 15:13:19 -04001988{
1989 assert(bs != NULL);
1990
1991 BlockReopenQueueEntry *bs_entry;
Kevin Wolf67251a32015-04-09 18:54:04 +02001992 BdrvChild *child;
Kevin Wolf145f5982015-05-08 16:15:03 +02001993 QDict *old_options, *explicit_options;
Kevin Wolf67251a32015-04-09 18:54:04 +02001994
Jeff Codye971aa12012-09-20 15:13:19 -04001995 if (bs_queue == NULL) {
1996 bs_queue = g_new0(BlockReopenQueue, 1);
1997 QSIMPLEQ_INIT(bs_queue);
1998 }
1999
Kevin Wolf4d2cb092015-04-10 17:50:50 +02002000 if (!options) {
2001 options = qdict_new();
2002 }
2003
Alberto Garcia5b7ba052016-09-15 17:53:03 +03002004 /* Check if this BlockDriverState is already in the queue */
2005 QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) {
2006 if (bs == bs_entry->state.bs) {
2007 break;
2008 }
2009 }
2010
Kevin Wolf28518102015-05-08 17:07:31 +02002011 /*
2012 * Precedence of options:
2013 * 1. Explicitly passed in options (highest)
Kevin Wolf91a097e2015-05-08 17:49:53 +02002014 * 2. Set in flags (only for top level)
Kevin Wolf145f5982015-05-08 16:15:03 +02002015 * 3. Retained from explicitly set options of bs
Kevin Wolf8e2160e2015-04-29 17:29:39 +02002016 * 4. Inherited from parent node
Kevin Wolf28518102015-05-08 17:07:31 +02002017 * 5. Retained from effective options of bs
2018 */
2019
Kevin Wolf91a097e2015-05-08 17:49:53 +02002020 if (!parent_options) {
2021 /*
2022 * Any setting represented by flags is always updated. If the
2023 * corresponding QDict option is set, it takes precedence. Otherwise
2024 * the flag is translated into a QDict option. The old setting of bs is
2025 * not considered.
2026 */
2027 update_options_from_flags(options, flags);
2028 }
2029
Kevin Wolf145f5982015-05-08 16:15:03 +02002030 /* Old explicitly set values (don't overwrite by inherited value) */
Alberto Garcia5b7ba052016-09-15 17:53:03 +03002031 if (bs_entry) {
2032 old_options = qdict_clone_shallow(bs_entry->state.explicit_options);
2033 } else {
2034 old_options = qdict_clone_shallow(bs->explicit_options);
2035 }
Kevin Wolf145f5982015-05-08 16:15:03 +02002036 bdrv_join_options(bs, options, old_options);
2037 QDECREF(old_options);
2038
2039 explicit_options = qdict_clone_shallow(options);
2040
Kevin Wolf28518102015-05-08 17:07:31 +02002041 /* Inherit from parent node */
2042 if (parent_options) {
2043 assert(!flags);
Kevin Wolf8e2160e2015-04-29 17:29:39 +02002044 role->inherit_options(&flags, options, parent_flags, parent_options);
Kevin Wolf28518102015-05-08 17:07:31 +02002045 }
2046
2047 /* Old values are used for options that aren't set yet */
Kevin Wolf4d2cb092015-04-10 17:50:50 +02002048 old_options = qdict_clone_shallow(bs->options);
Kevin Wolfcddff5b2015-11-16 16:43:27 +01002049 bdrv_join_options(bs, options, old_options);
Kevin Wolf4d2cb092015-04-10 17:50:50 +02002050 QDECREF(old_options);
2051
Kevin Wolff1f25a22014-04-25 19:04:55 +02002052 /* bdrv_open() masks this flag out */
2053 flags &= ~BDRV_O_PROTOCOL;
2054
Kevin Wolf67251a32015-04-09 18:54:04 +02002055 QLIST_FOREACH(child, &bs->children, next) {
Kevin Wolf4c9dfe52015-05-08 15:14:15 +02002056 QDict *new_child_options;
2057 char *child_key_dot;
Kevin Wolf67251a32015-04-09 18:54:04 +02002058
Kevin Wolf4c9dfe52015-05-08 15:14:15 +02002059 /* reopen can only change the options of block devices that were
2060 * implicitly created and inherited options. For other (referenced)
2061 * block devices, a syntax like "backing.foo" results in an error. */
Kevin Wolf67251a32015-04-09 18:54:04 +02002062 if (child->bs->inherits_from != bs) {
2063 continue;
2064 }
2065
Kevin Wolf4c9dfe52015-05-08 15:14:15 +02002066 child_key_dot = g_strdup_printf("%s.", child->name);
2067 qdict_extract_subqdict(options, &new_child_options, child_key_dot);
2068 g_free(child_key_dot);
2069
Kevin Wolf28518102015-05-08 17:07:31 +02002070 bdrv_reopen_queue_child(bs_queue, child->bs, new_child_options, 0,
2071 child->role, options, flags);
Jeff Codye971aa12012-09-20 15:13:19 -04002072 }
2073
Alberto Garcia5b7ba052016-09-15 17:53:03 +03002074 if (!bs_entry) {
2075 bs_entry = g_new0(BlockReopenQueueEntry, 1);
2076 QSIMPLEQ_INSERT_TAIL(bs_queue, bs_entry, entry);
2077 } else {
2078 QDECREF(bs_entry->state.options);
2079 QDECREF(bs_entry->state.explicit_options);
2080 }
Jeff Codye971aa12012-09-20 15:13:19 -04002081
2082 bs_entry->state.bs = bs;
Kevin Wolf4d2cb092015-04-10 17:50:50 +02002083 bs_entry->state.options = options;
Kevin Wolf145f5982015-05-08 16:15:03 +02002084 bs_entry->state.explicit_options = explicit_options;
Jeff Codye971aa12012-09-20 15:13:19 -04002085 bs_entry->state.flags = flags;
2086
2087 return bs_queue;
2088}
2089
Kevin Wolf28518102015-05-08 17:07:31 +02002090BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue,
2091 BlockDriverState *bs,
2092 QDict *options, int flags)
2093{
2094 return bdrv_reopen_queue_child(bs_queue, bs, options, flags,
2095 NULL, NULL, 0);
2096}
2097
Jeff Codye971aa12012-09-20 15:13:19 -04002098/*
2099 * Reopen multiple BlockDriverStates atomically & transactionally.
2100 *
2101 * The queue passed in (bs_queue) must have been built up previous
2102 * via bdrv_reopen_queue().
2103 *
2104 * Reopens all BDS specified in the queue, with the appropriate
2105 * flags. All devices are prepared for reopen, and failure of any
2106 * device will cause all device changes to be abandonded, and intermediate
2107 * data cleaned up.
2108 *
2109 * If all devices prepare successfully, then the changes are committed
2110 * to all devices.
2111 *
2112 */
Paolo Bonzini720150f2016-10-27 12:49:02 +02002113int bdrv_reopen_multiple(AioContext *ctx, BlockReopenQueue *bs_queue, Error **errp)
Jeff Codye971aa12012-09-20 15:13:19 -04002114{
2115 int ret = -1;
2116 BlockReopenQueueEntry *bs_entry, *next;
2117 Error *local_err = NULL;
2118
2119 assert(bs_queue != NULL);
2120
Paolo Bonzinic9d1a562016-10-27 12:49:05 +02002121 aio_context_release(ctx);
Alberto Garcia40840e42016-10-28 10:08:03 +03002122 bdrv_drain_all_begin();
Paolo Bonzinic9d1a562016-10-27 12:49:05 +02002123 aio_context_acquire(ctx);
Jeff Codye971aa12012-09-20 15:13:19 -04002124
2125 QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) {
2126 if (bdrv_reopen_prepare(&bs_entry->state, bs_queue, &local_err)) {
2127 error_propagate(errp, local_err);
2128 goto cleanup;
2129 }
2130 bs_entry->prepared = true;
2131 }
2132
2133 /* If we reach this point, we have success and just need to apply the
2134 * changes
2135 */
2136 QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) {
2137 bdrv_reopen_commit(&bs_entry->state);
2138 }
2139
2140 ret = 0;
2141
2142cleanup:
2143 QSIMPLEQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) {
2144 if (ret && bs_entry->prepared) {
2145 bdrv_reopen_abort(&bs_entry->state);
Kevin Wolf145f5982015-05-08 16:15:03 +02002146 } else if (ret) {
2147 QDECREF(bs_entry->state.explicit_options);
Jeff Codye971aa12012-09-20 15:13:19 -04002148 }
Kevin Wolf4d2cb092015-04-10 17:50:50 +02002149 QDECREF(bs_entry->state.options);
Jeff Codye971aa12012-09-20 15:13:19 -04002150 g_free(bs_entry);
2151 }
2152 g_free(bs_queue);
Alberto Garcia40840e42016-10-28 10:08:03 +03002153
2154 bdrv_drain_all_end();
2155
Jeff Codye971aa12012-09-20 15:13:19 -04002156 return ret;
2157}
2158
2159
2160/* Reopen a single BlockDriverState with the specified flags. */
2161int bdrv_reopen(BlockDriverState *bs, int bdrv_flags, Error **errp)
2162{
2163 int ret = -1;
2164 Error *local_err = NULL;
Kevin Wolf4d2cb092015-04-10 17:50:50 +02002165 BlockReopenQueue *queue = bdrv_reopen_queue(NULL, bs, NULL, bdrv_flags);
Jeff Codye971aa12012-09-20 15:13:19 -04002166
Paolo Bonzini720150f2016-10-27 12:49:02 +02002167 ret = bdrv_reopen_multiple(bdrv_get_aio_context(bs), queue, &local_err);
Jeff Codye971aa12012-09-20 15:13:19 -04002168 if (local_err != NULL) {
2169 error_propagate(errp, local_err);
2170 }
2171 return ret;
2172}
2173
2174
2175/*
2176 * Prepares a BlockDriverState for reopen. All changes are staged in the
2177 * 'opaque' field of the BDRVReopenState, which is used and allocated by
2178 * the block driver layer .bdrv_reopen_prepare()
2179 *
2180 * bs is the BlockDriverState to reopen
2181 * flags are the new open flags
2182 * queue is the reopen queue
2183 *
2184 * Returns 0 on success, non-zero on error. On error errp will be set
2185 * as well.
2186 *
2187 * On failure, bdrv_reopen_abort() will be called to clean up any data.
2188 * It is the responsibility of the caller to then call the abort() or
2189 * commit() for any other BDS that have been left in a prepare() state
2190 *
2191 */
2192int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue,
2193 Error **errp)
2194{
2195 int ret = -1;
2196 Error *local_err = NULL;
2197 BlockDriver *drv;
Kevin Wolfccf9dc02015-05-08 17:24:56 +02002198 QemuOpts *opts;
2199 const char *value;
Jeff Codye971aa12012-09-20 15:13:19 -04002200
2201 assert(reopen_state != NULL);
2202 assert(reopen_state->bs->drv != NULL);
2203 drv = reopen_state->bs->drv;
2204
Kevin Wolfccf9dc02015-05-08 17:24:56 +02002205 /* Process generic block layer options */
2206 opts = qemu_opts_create(&bdrv_runtime_opts, NULL, 0, &error_abort);
2207 qemu_opts_absorb_qdict(opts, reopen_state->options, &local_err);
2208 if (local_err) {
2209 error_propagate(errp, local_err);
2210 ret = -EINVAL;
2211 goto error;
2212 }
2213
Kevin Wolf91a097e2015-05-08 17:49:53 +02002214 update_flags_from_options(&reopen_state->flags, opts);
2215
Kevin Wolfccf9dc02015-05-08 17:24:56 +02002216 /* node-name and driver must be unchanged. Put them back into the QDict, so
2217 * that they are checked at the end of this function. */
2218 value = qemu_opt_get(opts, "node-name");
2219 if (value) {
2220 qdict_put(reopen_state->options, "node-name", qstring_from_str(value));
2221 }
2222
2223 value = qemu_opt_get(opts, "driver");
2224 if (value) {
2225 qdict_put(reopen_state->options, "driver", qstring_from_str(value));
2226 }
2227
Jeff Codye971aa12012-09-20 15:13:19 -04002228 /* if we are to stay read-only, do not allow permission change
2229 * to r/w */
2230 if (!(reopen_state->bs->open_flags & BDRV_O_ALLOW_RDWR) &&
2231 reopen_state->flags & BDRV_O_RDWR) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03002232 error_setg(errp, "Node '%s' is read only",
2233 bdrv_get_device_or_node_name(reopen_state->bs));
Jeff Codye971aa12012-09-20 15:13:19 -04002234 goto error;
2235 }
2236
2237
2238 ret = bdrv_flush(reopen_state->bs);
2239 if (ret) {
Eric Blake455b0fd2015-11-10 23:51:20 -07002240 error_setg_errno(errp, -ret, "Error flushing drive");
Jeff Codye971aa12012-09-20 15:13:19 -04002241 goto error;
2242 }
2243
2244 if (drv->bdrv_reopen_prepare) {
2245 ret = drv->bdrv_reopen_prepare(reopen_state, queue, &local_err);
2246 if (ret) {
2247 if (local_err != NULL) {
2248 error_propagate(errp, local_err);
2249 } else {
Luiz Capitulinod8b68952013-06-10 11:29:27 -04002250 error_setg(errp, "failed while preparing to reopen image '%s'",
2251 reopen_state->bs->filename);
Jeff Codye971aa12012-09-20 15:13:19 -04002252 }
2253 goto error;
2254 }
2255 } else {
2256 /* It is currently mandatory to have a bdrv_reopen_prepare()
2257 * handler for each supported drv. */
Alberto Garcia81e5f782015-04-08 12:29:19 +03002258 error_setg(errp, "Block format '%s' used by node '%s' "
2259 "does not support reopening files", drv->format_name,
2260 bdrv_get_device_or_node_name(reopen_state->bs));
Jeff Codye971aa12012-09-20 15:13:19 -04002261 ret = -1;
2262 goto error;
2263 }
2264
Kevin Wolf4d2cb092015-04-10 17:50:50 +02002265 /* Options that are not handled are only okay if they are unchanged
2266 * compared to the old state. It is expected that some options are only
2267 * used for the initial open, but not reopen (e.g. filename) */
2268 if (qdict_size(reopen_state->options)) {
2269 const QDictEntry *entry = qdict_first(reopen_state->options);
2270
2271 do {
2272 QString *new_obj = qobject_to_qstring(entry->value);
2273 const char *new = qstring_get_str(new_obj);
2274 const char *old = qdict_get_try_str(reopen_state->bs->options,
2275 entry->key);
2276
2277 if (!old || strcmp(new, old)) {
2278 error_setg(errp, "Cannot change the option '%s'", entry->key);
2279 ret = -EINVAL;
2280 goto error;
2281 }
2282 } while ((entry = qdict_next(reopen_state->options, entry)));
2283 }
2284
Jeff Codye971aa12012-09-20 15:13:19 -04002285 ret = 0;
2286
2287error:
Kevin Wolfccf9dc02015-05-08 17:24:56 +02002288 qemu_opts_del(opts);
Jeff Codye971aa12012-09-20 15:13:19 -04002289 return ret;
2290}
2291
2292/*
2293 * Takes the staged changes for the reopen from bdrv_reopen_prepare(), and
2294 * makes them final by swapping the staging BlockDriverState contents into
2295 * the active BlockDriverState contents.
2296 */
2297void bdrv_reopen_commit(BDRVReopenState *reopen_state)
2298{
2299 BlockDriver *drv;
2300
2301 assert(reopen_state != NULL);
2302 drv = reopen_state->bs->drv;
2303 assert(drv != NULL);
2304
2305 /* If there are any driver level actions to take */
2306 if (drv->bdrv_reopen_commit) {
2307 drv->bdrv_reopen_commit(reopen_state);
2308 }
2309
2310 /* set BDS specific flags now */
Kevin Wolf145f5982015-05-08 16:15:03 +02002311 QDECREF(reopen_state->bs->explicit_options);
2312
2313 reopen_state->bs->explicit_options = reopen_state->explicit_options;
Jeff Codye971aa12012-09-20 15:13:19 -04002314 reopen_state->bs->open_flags = reopen_state->flags;
Jeff Codye971aa12012-09-20 15:13:19 -04002315 reopen_state->bs->read_only = !(reopen_state->flags & BDRV_O_RDWR);
Kevin Wolf355ef4a2013-12-11 20:14:09 +01002316
Kevin Wolf3baca892014-07-16 17:48:16 +02002317 bdrv_refresh_limits(reopen_state->bs, NULL);
Jeff Codye971aa12012-09-20 15:13:19 -04002318}
2319
2320/*
2321 * Abort the reopen, and delete and free the staged changes in
2322 * reopen_state
2323 */
2324void bdrv_reopen_abort(BDRVReopenState *reopen_state)
2325{
2326 BlockDriver *drv;
2327
2328 assert(reopen_state != NULL);
2329 drv = reopen_state->bs->drv;
2330 assert(drv != NULL);
2331
2332 if (drv->bdrv_reopen_abort) {
2333 drv->bdrv_reopen_abort(reopen_state);
2334 }
Kevin Wolf145f5982015-05-08 16:15:03 +02002335
2336 QDECREF(reopen_state->explicit_options);
Jeff Codye971aa12012-09-20 15:13:19 -04002337}
2338
2339
Max Reitz64dff522016-01-29 16:36:10 +01002340static void bdrv_close(BlockDriverState *bs)
bellardfc01f7e2003-06-30 10:03:06 +00002341{
Max Reitz33384422014-06-20 21:57:33 +02002342 BdrvAioNotifier *ban, *ban_next;
2343
Max Reitzca9bd242016-01-29 16:36:14 +01002344 assert(!bs->job);
Max Reitz30f55fb2016-05-17 16:41:32 +02002345 assert(!bs->refcnt);
Alberto Garcia99b7e772015-09-25 16:41:44 +03002346
Paolo Bonzinifc272912015-12-23 11:48:24 +01002347 bdrv_drained_begin(bs); /* complete I/O */
Stefan Hajnoczi58fda172013-07-02 15:36:25 +02002348 bdrv_flush(bs);
Fam Zheng53ec73e2015-05-29 18:53:14 +08002349 bdrv_drain(bs); /* in case flush left pending I/O */
Paolo Bonzinifc272912015-12-23 11:48:24 +01002350
Max Reitzc5acdc92016-01-29 16:36:01 +01002351 bdrv_release_named_dirty_bitmaps(bs);
2352 assert(QLIST_EMPTY(&bs->dirty_bitmaps));
2353
Paolo Bonzini3cbc0022012-10-19 11:36:48 +02002354 if (bs->drv) {
Kevin Wolf6e93e7c2015-04-08 13:49:41 +02002355 BdrvChild *child, *next;
2356
Kevin Wolf9a7dedb2015-06-16 10:58:20 +02002357 bs->drv->bdrv_close(bs);
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02002358 bs->drv = NULL;
Kevin Wolf9a7dedb2015-06-16 10:58:20 +02002359
Kevin Wolf5db15a52015-09-14 15:33:33 +02002360 bdrv_set_backing_hd(bs, NULL);
Kevin Wolf9a7dedb2015-06-16 10:58:20 +02002361
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02002362 if (bs->file != NULL) {
2363 bdrv_unref_child(bs, bs->file);
2364 bs->file = NULL;
2365 }
2366
Kevin Wolf6e93e7c2015-04-08 13:49:41 +02002367 QLIST_FOREACH_SAFE(child, &bs->children, next, next) {
Kevin Wolf33a60402015-06-15 13:51:04 +02002368 /* TODO Remove bdrv_unref() from drivers' close function and use
2369 * bdrv_unref_child() here */
Kevin Wolfbddcec32015-04-09 18:47:50 +02002370 if (child->bs->inherits_from == bs) {
2371 child->bs->inherits_from = NULL;
2372 }
Kevin Wolf33a60402015-06-15 13:51:04 +02002373 bdrv_detach_child(child);
Kevin Wolf6e93e7c2015-04-08 13:49:41 +02002374 }
2375
Anthony Liguori7267c092011-08-20 22:09:37 -05002376 g_free(bs->opaque);
bellardea2384d2004-08-01 21:59:26 +00002377 bs->opaque = NULL;
Stefan Hajnoczi53fec9d2011-11-28 16:08:47 +00002378 bs->copy_on_read = 0;
Paolo Bonzinia275fa42012-05-08 16:51:43 +02002379 bs->backing_file[0] = '\0';
2380 bs->backing_format[0] = '\0';
Paolo Bonzini64058752012-05-08 16:51:49 +02002381 bs->total_sectors = 0;
Eric Blake54115412016-06-23 16:37:26 -06002382 bs->encrypted = false;
2383 bs->valid_key = false;
2384 bs->sg = false;
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01002385 QDECREF(bs->options);
Kevin Wolf145f5982015-05-08 16:15:03 +02002386 QDECREF(bs->explicit_options);
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01002387 bs->options = NULL;
Max Reitz91af7012014-07-18 20:24:56 +02002388 QDECREF(bs->full_open_options);
2389 bs->full_open_options = NULL;
bellardb3380822004-03-14 21:38:54 +00002390 }
Zhi Yong Wu98f90db2011-11-08 13:00:14 +08002391
Max Reitz33384422014-06-20 21:57:33 +02002392 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) {
2393 g_free(ban);
2394 }
2395 QLIST_INIT(&bs->aio_notifiers);
Paolo Bonzinifc272912015-12-23 11:48:24 +01002396 bdrv_drained_end(bs);
bellardb3380822004-03-14 21:38:54 +00002397}
2398
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +09002399void bdrv_close_all(void)
2400{
Kevin Wolfa1a2af02016-04-08 18:26:37 +02002401 block_job_cancel_sync_all();
Kevin Wolfcd7fca92016-07-06 11:22:39 +02002402 nbd_export_close_all();
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +09002403
Max Reitzca9bd242016-01-29 16:36:14 +01002404 /* Drop references from requests still in flight, such as canceled block
2405 * jobs whose AIO context has not been polled yet */
2406 bdrv_drain_all();
Stefan Hajnoczied78cda2014-05-08 16:34:35 +02002407
Max Reitzca9bd242016-01-29 16:36:14 +01002408 blk_remove_all_bs();
2409 blockdev_close_all_bdrv_states();
2410
Kevin Wolfa1a2af02016-04-08 18:26:37 +02002411 assert(QTAILQ_EMPTY(&all_bdrv_states));
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +09002412}
2413
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002414static void change_parent_backing_link(BlockDriverState *from,
2415 BlockDriverState *to)
2416{
Max Reitz9bd910e2016-06-10 20:57:46 +02002417 BdrvChild *c, *next, *to_c;
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002418
2419 QLIST_FOREACH_SAFE(c, &from->parents, next_parent, next) {
Max Reitz9bd910e2016-06-10 20:57:46 +02002420 if (c->role == &child_backing) {
2421 /* @from is generally not allowed to be a backing file, except for
2422 * when @to is the overlay. In that case, @from may not be replaced
2423 * by @to as @to's backing node. */
2424 QLIST_FOREACH(to_c, &to->children, next) {
2425 if (to_c == c) {
2426 break;
2427 }
2428 }
2429 if (to_c) {
2430 continue;
2431 }
2432 }
2433
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002434 assert(c->role != &child_backing);
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002435 bdrv_ref(to);
Kevin Wolfe9740bc2016-05-23 15:52:26 +02002436 bdrv_replace_child(c, to);
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002437 bdrv_unref(from);
2438 }
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002439}
2440
Jeff Cody8802d1f2012-02-28 15:54:06 -05002441/*
2442 * Add new bs contents at the top of an image chain while the chain is
2443 * live, while keeping required fields on the top layer.
2444 *
2445 * This will modify the BlockDriverState fields, and swap contents
2446 * between bs_new and bs_top. Both bs_new and bs_top are modified.
2447 *
Markus Armbrusterbfb197e2014-10-07 13:59:11 +02002448 * bs_new must not be attached to a BlockBackend.
Jeff Codyf6801b82012-03-27 16:30:19 -04002449 *
Jeff Cody8802d1f2012-02-28 15:54:06 -05002450 * This function does not create any image files.
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002451 *
2452 * bdrv_append() takes ownership of a bs_new reference and unrefs it because
2453 * that's what the callers commonly need. bs_new will be referenced by the old
2454 * parents of bs_top after bdrv_append() returns. If the caller needs to keep a
2455 * reference of its own, it must call bdrv_ref().
Jeff Cody8802d1f2012-02-28 15:54:06 -05002456 */
2457void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top)
2458{
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002459 assert(!bdrv_requests_pending(bs_top));
2460 assert(!bdrv_requests_pending(bs_new));
Jeff Cody8802d1f2012-02-28 15:54:06 -05002461
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002462 bdrv_ref(bs_top);
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002463
Kevin Wolf27ccdd52016-03-21 12:56:44 +01002464 change_parent_backing_link(bs_top, bs_new);
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002465 bdrv_set_backing_hd(bs_new, bs_top);
2466 bdrv_unref(bs_top);
2467
2468 /* bs_new is now referenced by its new parents, we don't need the
2469 * additional reference any more. */
2470 bdrv_unref(bs_new);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002471}
2472
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02002473void bdrv_replace_in_backing_chain(BlockDriverState *old, BlockDriverState *new)
2474{
2475 assert(!bdrv_requests_pending(old));
2476 assert(!bdrv_requests_pending(new));
2477
2478 bdrv_ref(old);
2479
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02002480 change_parent_backing_link(old, new);
2481
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02002482 bdrv_unref(old);
2483}
2484
Fam Zheng4f6fd342013-08-23 09:14:47 +08002485static void bdrv_delete(BlockDriverState *bs)
bellardb3380822004-03-14 21:38:54 +00002486{
Paolo Bonzini3e914652012-03-30 13:17:11 +02002487 assert(!bs->job);
Fam Zheng3718d8a2014-05-23 21:29:43 +08002488 assert(bdrv_op_blocker_is_empty(bs));
Fam Zheng4f6fd342013-08-23 09:14:47 +08002489 assert(!bs->refcnt);
Markus Armbruster18846de2010-06-29 16:58:30 +02002490
Stefan Hajnoczie1b5c522013-06-27 15:32:26 +02002491 bdrv_close(bs);
2492
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01002493 /* remove from list, if necessary */
Kevin Wolf63eaaae2016-03-18 10:46:57 +01002494 if (bs->node_name[0] != '\0') {
2495 QTAILQ_REMOVE(&graph_bdrv_states, bs, node_list);
2496 }
Max Reitz2c1d04e2016-01-29 16:36:11 +01002497 QTAILQ_REMOVE(&all_bdrv_states, bs, bs_list);
2498
Anthony Liguori7267c092011-08-20 22:09:37 -05002499 g_free(bs);
bellardfc01f7e2003-06-30 10:03:06 +00002500}
2501
aliguorie97fc192009-04-21 23:11:50 +00002502/*
2503 * Run consistency checks on an image
2504 *
Kevin Wolfe076f332010-06-29 11:43:13 +02002505 * Returns 0 if the check could be completed (it doesn't mean that the image is
Stefan Weila1c72732011-04-28 17:20:38 +02002506 * free of errors) or -errno when an internal error occurred. The results of the
Kevin Wolfe076f332010-06-29 11:43:13 +02002507 * check are stored in res.
aliguorie97fc192009-04-21 23:11:50 +00002508 */
Kevin Wolf4534ff52012-05-11 16:07:02 +02002509int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix)
aliguorie97fc192009-04-21 23:11:50 +00002510{
Max Reitz908bcd52014-08-07 22:47:55 +02002511 if (bs->drv == NULL) {
2512 return -ENOMEDIUM;
2513 }
aliguorie97fc192009-04-21 23:11:50 +00002514 if (bs->drv->bdrv_check == NULL) {
2515 return -ENOTSUP;
2516 }
2517
Kevin Wolfe076f332010-06-29 11:43:13 +02002518 memset(res, 0, sizeof(*res));
Kevin Wolf4534ff52012-05-11 16:07:02 +02002519 return bs->drv->bdrv_check(bs, res, fix);
aliguorie97fc192009-04-21 23:11:50 +00002520}
2521
Kevin Wolf756e6732010-01-12 12:55:17 +01002522/*
2523 * Return values:
2524 * 0 - success
2525 * -EINVAL - backing format specified, but no file
2526 * -ENOSPC - can't update the backing file because no space is left in the
2527 * image file header
2528 * -ENOTSUP - format driver doesn't support changing the backing file
2529 */
2530int bdrv_change_backing_file(BlockDriverState *bs,
2531 const char *backing_file, const char *backing_fmt)
2532{
2533 BlockDriver *drv = bs->drv;
Paolo Bonzini469ef352012-04-12 14:01:02 +02002534 int ret;
Kevin Wolf756e6732010-01-12 12:55:17 +01002535
Paolo Bonzini5f377792012-04-12 14:01:01 +02002536 /* Backing file format doesn't make sense without a backing file */
2537 if (backing_fmt && !backing_file) {
2538 return -EINVAL;
2539 }
2540
Kevin Wolf756e6732010-01-12 12:55:17 +01002541 if (drv->bdrv_change_backing_file != NULL) {
Paolo Bonzini469ef352012-04-12 14:01:02 +02002542 ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
Kevin Wolf756e6732010-01-12 12:55:17 +01002543 } else {
Paolo Bonzini469ef352012-04-12 14:01:02 +02002544 ret = -ENOTSUP;
Kevin Wolf756e6732010-01-12 12:55:17 +01002545 }
Paolo Bonzini469ef352012-04-12 14:01:02 +02002546
2547 if (ret == 0) {
2548 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
2549 pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
2550 }
2551 return ret;
Kevin Wolf756e6732010-01-12 12:55:17 +01002552}
2553
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002554/*
2555 * Finds the image layer in the chain that has 'bs' as its backing file.
2556 *
2557 * active is the current topmost image.
2558 *
2559 * Returns NULL if bs is not found in active's image chain,
2560 * or if active == bs.
Jeff Cody4caf0fc2014-06-25 15:35:26 -04002561 *
2562 * Returns the bottommost base image if bs == NULL.
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002563 */
2564BlockDriverState *bdrv_find_overlay(BlockDriverState *active,
2565 BlockDriverState *bs)
2566{
Kevin Wolf760e0062015-06-17 14:55:21 +02002567 while (active && bs != backing_bs(active)) {
2568 active = backing_bs(active);
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002569 }
2570
Jeff Cody4caf0fc2014-06-25 15:35:26 -04002571 return active;
2572}
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002573
Jeff Cody4caf0fc2014-06-25 15:35:26 -04002574/* Given a BDS, searches for the base layer. */
2575BlockDriverState *bdrv_find_base(BlockDriverState *bs)
2576{
2577 return bdrv_find_overlay(bs, NULL);
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002578}
2579
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002580/*
2581 * Drops images above 'base' up to and including 'top', and sets the image
2582 * above 'top' to have base as its backing file.
2583 *
2584 * Requires that the overlay to 'top' is opened r/w, so that the backing file
2585 * information in 'bs' can be properly updated.
2586 *
2587 * E.g., this will convert the following chain:
2588 * bottom <- base <- intermediate <- top <- active
2589 *
2590 * to
2591 *
2592 * bottom <- base <- active
2593 *
2594 * It is allowed for bottom==base, in which case it converts:
2595 *
2596 * base <- intermediate <- top <- active
2597 *
2598 * to
2599 *
2600 * base <- active
2601 *
Jeff Cody54e26902014-06-25 15:40:10 -04002602 * If backing_file_str is non-NULL, it will be used when modifying top's
2603 * overlay image metadata.
2604 *
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002605 * Error conditions:
2606 * if active == top, that is considered an error
2607 *
2608 */
2609int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top,
Jeff Cody54e26902014-06-25 15:40:10 -04002610 BlockDriverState *base, const char *backing_file_str)
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002611{
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002612 BlockDriverState *new_top_bs = NULL;
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002613 int ret = -EIO;
2614
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002615 if (!top->drv || !base->drv) {
2616 goto exit;
2617 }
2618
2619 new_top_bs = bdrv_find_overlay(active, top);
2620
2621 if (new_top_bs == NULL) {
2622 /* we could not find the image above 'top', this is an error */
2623 goto exit;
2624 }
2625
Kevin Wolf760e0062015-06-17 14:55:21 +02002626 /* special case of new_top_bs->backing->bs already pointing to base - nothing
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002627 * to do, no intermediate images */
Kevin Wolf760e0062015-06-17 14:55:21 +02002628 if (backing_bs(new_top_bs) == base) {
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002629 ret = 0;
2630 goto exit;
2631 }
2632
Kevin Wolf5db15a52015-09-14 15:33:33 +02002633 /* Make sure that base is in the backing chain of top */
2634 if (!bdrv_chain_contains(top, base)) {
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002635 goto exit;
2636 }
2637
2638 /* success - we can delete the intermediate states, and link top->base */
Kevin Wolf5db15a52015-09-14 15:33:33 +02002639 backing_file_str = backing_file_str ? backing_file_str : base->filename;
Jeff Cody54e26902014-06-25 15:40:10 -04002640 ret = bdrv_change_backing_file(new_top_bs, backing_file_str,
Kevin Wolf5db15a52015-09-14 15:33:33 +02002641 base->drv ? base->drv->format_name : "");
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002642 if (ret) {
2643 goto exit;
2644 }
Kevin Wolf5db15a52015-09-14 15:33:33 +02002645 bdrv_set_backing_hd(new_top_bs, base);
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002646
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002647 ret = 0;
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002648exit:
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002649 return ret;
2650}
2651
bellard83f64092006-08-01 16:21:11 +00002652/**
bellard83f64092006-08-01 16:21:11 +00002653 * Truncate file to 'offset' bytes (needed only for file protocols)
2654 */
Kevin Wolf52cdbc52017-02-16 18:39:03 +01002655int bdrv_truncate(BdrvChild *child, int64_t offset)
bellard83f64092006-08-01 16:21:11 +00002656{
Kevin Wolf52cdbc52017-02-16 18:39:03 +01002657 BlockDriverState *bs = child->bs;
bellard83f64092006-08-01 16:21:11 +00002658 BlockDriver *drv = bs->drv;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01002659 int ret;
bellard83f64092006-08-01 16:21:11 +00002660 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002661 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +00002662 if (!drv->bdrv_truncate)
2663 return -ENOTSUP;
Naphtali Sprei59f26892009-10-26 16:25:16 +02002664 if (bs->read_only)
2665 return -EACCES;
Jeff Cody9c75e162014-06-25 16:55:30 -04002666
Stefan Hajnoczi51762282010-04-19 16:56:41 +01002667 ret = drv->bdrv_truncate(bs, offset);
2668 if (ret == 0) {
2669 ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
John Snowce1ffea2015-04-17 19:50:03 -04002670 bdrv_dirty_bitmap_truncate(bs);
Kevin Wolf5c8cab42016-02-24 15:13:35 +01002671 bdrv_parent_cb_resize(bs);
Evgeny Yakovlev3ff2f672016-07-18 22:39:52 +03002672 ++bs->write_gen;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01002673 }
2674 return ret;
bellard83f64092006-08-01 16:21:11 +00002675}
2676
2677/**
Fam Zheng4a1d5e12011-07-12 19:56:39 +08002678 * Length of a allocated file in bytes. Sparse files are counted by actual
2679 * allocated space. Return < 0 if error or unknown.
2680 */
2681int64_t bdrv_get_allocated_file_size(BlockDriverState *bs)
2682{
2683 BlockDriver *drv = bs->drv;
2684 if (!drv) {
2685 return -ENOMEDIUM;
2686 }
2687 if (drv->bdrv_get_allocated_file_size) {
2688 return drv->bdrv_get_allocated_file_size(bs);
2689 }
2690 if (bs->file) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02002691 return bdrv_get_allocated_file_size(bs->file->bs);
Fam Zheng4a1d5e12011-07-12 19:56:39 +08002692 }
2693 return -ENOTSUP;
2694}
2695
2696/**
Markus Armbruster65a9bb22014-06-26 13:23:17 +02002697 * Return number of sectors on success, -errno on error.
bellard83f64092006-08-01 16:21:11 +00002698 */
Markus Armbruster65a9bb22014-06-26 13:23:17 +02002699int64_t bdrv_nb_sectors(BlockDriverState *bs)
bellard83f64092006-08-01 16:21:11 +00002700{
2701 BlockDriver *drv = bs->drv;
Markus Armbruster65a9bb22014-06-26 13:23:17 +02002702
bellard83f64092006-08-01 16:21:11 +00002703 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002704 return -ENOMEDIUM;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01002705
Kevin Wolfb94a2612013-10-29 12:18:58 +01002706 if (drv->has_variable_length) {
2707 int ret = refresh_total_sectors(bs, bs->total_sectors);
2708 if (ret < 0) {
2709 return ret;
Stefan Hajnoczi46a4e4e2011-03-29 20:04:41 +01002710 }
bellard83f64092006-08-01 16:21:11 +00002711 }
Markus Armbruster65a9bb22014-06-26 13:23:17 +02002712 return bs->total_sectors;
2713}
2714
2715/**
2716 * Return length in bytes on success, -errno on error.
2717 * The length is always a multiple of BDRV_SECTOR_SIZE.
2718 */
2719int64_t bdrv_getlength(BlockDriverState *bs)
2720{
2721 int64_t ret = bdrv_nb_sectors(bs);
2722
Fam Zheng4a9c9ea2015-05-15 16:36:05 +08002723 ret = ret > INT64_MAX / BDRV_SECTOR_SIZE ? -EFBIG : ret;
Markus Armbruster65a9bb22014-06-26 13:23:17 +02002724 return ret < 0 ? ret : ret * BDRV_SECTOR_SIZE;
bellardfc01f7e2003-06-30 10:03:06 +00002725}
2726
bellard19cb3732006-08-19 11:45:59 +00002727/* return 0 as number of sectors if no device present or error */
ths96b8f132007-12-17 01:35:20 +00002728void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
bellardfc01f7e2003-06-30 10:03:06 +00002729{
Markus Armbruster65a9bb22014-06-26 13:23:17 +02002730 int64_t nb_sectors = bdrv_nb_sectors(bs);
2731
2732 *nb_sectors_ptr = nb_sectors < 0 ? 0 : nb_sectors;
bellardfc01f7e2003-06-30 10:03:06 +00002733}
bellardcf989512004-02-16 21:56:36 +00002734
Eric Blake54115412016-06-23 16:37:26 -06002735bool bdrv_is_read_only(BlockDriverState *bs)
bellardb3380822004-03-14 21:38:54 +00002736{
2737 return bs->read_only;
2738}
2739
Eric Blake54115412016-06-23 16:37:26 -06002740bool bdrv_is_sg(BlockDriverState *bs)
ths985a03b2007-12-24 16:10:43 +00002741{
2742 return bs->sg;
2743}
2744
Eric Blake54115412016-06-23 16:37:26 -06002745bool bdrv_is_encrypted(BlockDriverState *bs)
bellardea2384d2004-08-01 21:59:26 +00002746{
Kevin Wolf760e0062015-06-17 14:55:21 +02002747 if (bs->backing && bs->backing->bs->encrypted) {
Eric Blake54115412016-06-23 16:37:26 -06002748 return true;
Kevin Wolf760e0062015-06-17 14:55:21 +02002749 }
bellardea2384d2004-08-01 21:59:26 +00002750 return bs->encrypted;
2751}
2752
Eric Blake54115412016-06-23 16:37:26 -06002753bool bdrv_key_required(BlockDriverState *bs)
aliguoric0f4ce72009-03-05 23:01:01 +00002754{
Kevin Wolf760e0062015-06-17 14:55:21 +02002755 BdrvChild *backing = bs->backing;
aliguoric0f4ce72009-03-05 23:01:01 +00002756
Kevin Wolf760e0062015-06-17 14:55:21 +02002757 if (backing && backing->bs->encrypted && !backing->bs->valid_key) {
Eric Blake54115412016-06-23 16:37:26 -06002758 return true;
Kevin Wolf760e0062015-06-17 14:55:21 +02002759 }
aliguoric0f4ce72009-03-05 23:01:01 +00002760 return (bs->encrypted && !bs->valid_key);
2761}
2762
bellardea2384d2004-08-01 21:59:26 +00002763int bdrv_set_key(BlockDriverState *bs, const char *key)
2764{
2765 int ret;
Kevin Wolf760e0062015-06-17 14:55:21 +02002766 if (bs->backing && bs->backing->bs->encrypted) {
2767 ret = bdrv_set_key(bs->backing->bs, key);
bellardea2384d2004-08-01 21:59:26 +00002768 if (ret < 0)
2769 return ret;
2770 if (!bs->encrypted)
2771 return 0;
2772 }
Shahar Havivifd04a2a2010-03-06 00:26:13 +02002773 if (!bs->encrypted) {
2774 return -EINVAL;
2775 } else if (!bs->drv || !bs->drv->bdrv_set_key) {
2776 return -ENOMEDIUM;
2777 }
aliguoric0f4ce72009-03-05 23:01:01 +00002778 ret = bs->drv->bdrv_set_key(bs, key);
aliguoribb5fc202009-03-05 23:01:15 +00002779 if (ret < 0) {
Eric Blake54115412016-06-23 16:37:26 -06002780 bs->valid_key = false;
aliguoribb5fc202009-03-05 23:01:15 +00002781 } else if (!bs->valid_key) {
Kevin Wolf5c8cab42016-02-24 15:13:35 +01002782 /* call the change callback now, we skipped it on open */
Eric Blake54115412016-06-23 16:37:26 -06002783 bs->valid_key = true;
Kevin Wolf5c8cab42016-02-24 15:13:35 +01002784 bdrv_parent_cb_change_media(bs, true);
aliguoribb5fc202009-03-05 23:01:15 +00002785 }
aliguoric0f4ce72009-03-05 23:01:01 +00002786 return ret;
bellardea2384d2004-08-01 21:59:26 +00002787}
2788
Markus Armbruster4d2855a2015-01-29 10:37:00 +01002789/*
2790 * Provide an encryption key for @bs.
2791 * If @key is non-null:
2792 * If @bs is not encrypted, fail.
2793 * Else if the key is invalid, fail.
2794 * Else set @bs's key to @key, replacing the existing key, if any.
2795 * If @key is null:
2796 * If @bs is encrypted and still lacks a key, fail.
2797 * Else do nothing.
2798 * On failure, store an error object through @errp if non-null.
2799 */
2800void bdrv_add_key(BlockDriverState *bs, const char *key, Error **errp)
2801{
2802 if (key) {
2803 if (!bdrv_is_encrypted(bs)) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03002804 error_setg(errp, "Node '%s' is not encrypted",
2805 bdrv_get_device_or_node_name(bs));
Markus Armbruster4d2855a2015-01-29 10:37:00 +01002806 } else if (bdrv_set_key(bs, key) < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002807 error_setg(errp, QERR_INVALID_PASSWORD);
Markus Armbruster4d2855a2015-01-29 10:37:00 +01002808 }
2809 } else {
2810 if (bdrv_key_required(bs)) {
Markus Armbrusterb1ca6392015-01-29 10:37:01 +01002811 error_set(errp, ERROR_CLASS_DEVICE_ENCRYPTED,
2812 "'%s' (%s) is encrypted",
Alberto Garcia81e5f782015-04-08 12:29:19 +03002813 bdrv_get_device_or_node_name(bs),
Markus Armbruster4d2855a2015-01-29 10:37:00 +01002814 bdrv_get_encrypted_filename(bs));
2815 }
2816 }
2817}
2818
Markus Armbrusterf8d6bba2012-06-13 10:11:48 +02002819const char *bdrv_get_format_name(BlockDriverState *bs)
bellardea2384d2004-08-01 21:59:26 +00002820{
Markus Armbrusterf8d6bba2012-06-13 10:11:48 +02002821 return bs->drv ? bs->drv->format_name : NULL;
bellardea2384d2004-08-01 21:59:26 +00002822}
2823
Stefan Hajnocziada42402014-08-27 12:08:55 +01002824static int qsort_strcmp(const void *a, const void *b)
2825{
Max Reitzceff5bd2016-10-12 22:49:05 +02002826 return strcmp(*(char *const *)a, *(char *const *)b);
Stefan Hajnocziada42402014-08-27 12:08:55 +01002827}
2828
ths5fafdf22007-09-16 21:08:06 +00002829void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
bellardea2384d2004-08-01 21:59:26 +00002830 void *opaque)
2831{
2832 BlockDriver *drv;
Jeff Codye855e4f2014-04-28 18:29:54 -04002833 int count = 0;
Stefan Hajnocziada42402014-08-27 12:08:55 +01002834 int i;
Jeff Codye855e4f2014-04-28 18:29:54 -04002835 const char **formats = NULL;
bellardea2384d2004-08-01 21:59:26 +00002836
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +01002837 QLIST_FOREACH(drv, &bdrv_drivers, list) {
Jeff Codye855e4f2014-04-28 18:29:54 -04002838 if (drv->format_name) {
2839 bool found = false;
2840 int i = count;
2841 while (formats && i && !found) {
2842 found = !strcmp(formats[--i], drv->format_name);
2843 }
2844
2845 if (!found) {
Markus Armbruster5839e532014-08-19 10:31:08 +02002846 formats = g_renew(const char *, formats, count + 1);
Jeff Codye855e4f2014-04-28 18:29:54 -04002847 formats[count++] = drv->format_name;
Jeff Codye855e4f2014-04-28 18:29:54 -04002848 }
2849 }
bellardea2384d2004-08-01 21:59:26 +00002850 }
Stefan Hajnocziada42402014-08-27 12:08:55 +01002851
Max Reitzeb0df692016-10-12 22:49:06 +02002852 for (i = 0; i < (int)ARRAY_SIZE(block_driver_modules); i++) {
2853 const char *format_name = block_driver_modules[i].format_name;
2854
2855 if (format_name) {
2856 bool found = false;
2857 int j = count;
2858
2859 while (formats && j && !found) {
2860 found = !strcmp(formats[--j], format_name);
2861 }
2862
2863 if (!found) {
2864 formats = g_renew(const char *, formats, count + 1);
2865 formats[count++] = format_name;
2866 }
2867 }
2868 }
2869
Stefan Hajnocziada42402014-08-27 12:08:55 +01002870 qsort(formats, count, sizeof(formats[0]), qsort_strcmp);
2871
2872 for (i = 0; i < count; i++) {
2873 it(opaque, formats[i]);
2874 }
2875
Jeff Codye855e4f2014-04-28 18:29:54 -04002876 g_free(formats);
bellardea2384d2004-08-01 21:59:26 +00002877}
2878
Benoît Canetdc364f42014-01-23 21:31:32 +01002879/* This function is to find a node in the bs graph */
2880BlockDriverState *bdrv_find_node(const char *node_name)
2881{
2882 BlockDriverState *bs;
2883
2884 assert(node_name);
2885
2886 QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) {
2887 if (!strcmp(node_name, bs->node_name)) {
2888 return bs;
2889 }
2890 }
2891 return NULL;
2892}
2893
Benoît Canetc13163f2014-01-23 21:31:34 +01002894/* Put this QMP function here so it can access the static graph_bdrv_states. */
Alberto Garciad5a8ee62015-04-17 14:52:43 +03002895BlockDeviceInfoList *bdrv_named_nodes_list(Error **errp)
Benoît Canetc13163f2014-01-23 21:31:34 +01002896{
2897 BlockDeviceInfoList *list, *entry;
2898 BlockDriverState *bs;
2899
2900 list = NULL;
2901 QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) {
Kevin Wolfc83f9fb2016-03-03 11:37:48 +01002902 BlockDeviceInfo *info = bdrv_block_device_info(NULL, bs, errp);
Alberto Garciad5a8ee62015-04-17 14:52:43 +03002903 if (!info) {
2904 qapi_free_BlockDeviceInfoList(list);
2905 return NULL;
2906 }
Benoît Canetc13163f2014-01-23 21:31:34 +01002907 entry = g_malloc0(sizeof(*entry));
Alberto Garciad5a8ee62015-04-17 14:52:43 +03002908 entry->value = info;
Benoît Canetc13163f2014-01-23 21:31:34 +01002909 entry->next = list;
2910 list = entry;
2911 }
2912
2913 return list;
2914}
2915
Benoît Canet12d3ba82014-01-23 21:31:35 +01002916BlockDriverState *bdrv_lookup_bs(const char *device,
2917 const char *node_name,
2918 Error **errp)
2919{
Markus Armbruster7f06d472014-10-07 13:59:12 +02002920 BlockBackend *blk;
2921 BlockDriverState *bs;
Benoît Canet12d3ba82014-01-23 21:31:35 +01002922
Benoît Canet12d3ba82014-01-23 21:31:35 +01002923 if (device) {
Markus Armbruster7f06d472014-10-07 13:59:12 +02002924 blk = blk_by_name(device);
Benoît Canet12d3ba82014-01-23 21:31:35 +01002925
Markus Armbruster7f06d472014-10-07 13:59:12 +02002926 if (blk) {
Alberto Garcia9f4ed6f2015-10-26 16:46:49 +02002927 bs = blk_bs(blk);
2928 if (!bs) {
Max Reitz5433c242015-10-19 17:53:29 +02002929 error_setg(errp, "Device '%s' has no medium", device);
Max Reitz5433c242015-10-19 17:53:29 +02002930 }
2931
Alberto Garcia9f4ed6f2015-10-26 16:46:49 +02002932 return bs;
Benoît Canet12d3ba82014-01-23 21:31:35 +01002933 }
Benoît Canet12d3ba82014-01-23 21:31:35 +01002934 }
2935
Benoît Canetdd67fa52014-02-12 17:15:06 +01002936 if (node_name) {
2937 bs = bdrv_find_node(node_name);
Benoît Canet12d3ba82014-01-23 21:31:35 +01002938
Benoît Canetdd67fa52014-02-12 17:15:06 +01002939 if (bs) {
2940 return bs;
2941 }
Benoît Canet12d3ba82014-01-23 21:31:35 +01002942 }
2943
Benoît Canetdd67fa52014-02-12 17:15:06 +01002944 error_setg(errp, "Cannot find device=%s nor node_name=%s",
2945 device ? device : "",
2946 node_name ? node_name : "");
2947 return NULL;
Benoît Canet12d3ba82014-01-23 21:31:35 +01002948}
2949
Jeff Cody5a6684d2014-06-25 15:40:09 -04002950/* If 'base' is in the same chain as 'top', return true. Otherwise,
2951 * return false. If either argument is NULL, return false. */
2952bool bdrv_chain_contains(BlockDriverState *top, BlockDriverState *base)
2953{
2954 while (top && top != base) {
Kevin Wolf760e0062015-06-17 14:55:21 +02002955 top = backing_bs(top);
Jeff Cody5a6684d2014-06-25 15:40:09 -04002956 }
2957
2958 return top != NULL;
2959}
2960
Fam Zheng04df7652014-10-31 11:32:54 +08002961BlockDriverState *bdrv_next_node(BlockDriverState *bs)
2962{
2963 if (!bs) {
2964 return QTAILQ_FIRST(&graph_bdrv_states);
2965 }
2966 return QTAILQ_NEXT(bs, node_list);
2967}
2968
Fam Zheng20a9e772014-10-31 11:32:55 +08002969const char *bdrv_get_node_name(const BlockDriverState *bs)
2970{
2971 return bs->node_name;
2972}
2973
Kevin Wolf1f0c4612016-03-22 18:38:44 +01002974const char *bdrv_get_parent_name(const BlockDriverState *bs)
Kevin Wolf4c265bf2016-02-26 10:22:16 +01002975{
2976 BdrvChild *c;
2977 const char *name;
2978
2979 /* If multiple parents have a name, just pick the first one. */
2980 QLIST_FOREACH(c, &bs->parents, next_parent) {
2981 if (c->role->get_name) {
2982 name = c->role->get_name(c);
2983 if (name && *name) {
2984 return name;
2985 }
2986 }
2987 }
2988
2989 return NULL;
2990}
2991
Markus Armbruster7f06d472014-10-07 13:59:12 +02002992/* TODO check what callers really want: bs->node_name or blk_name() */
Markus Armbrusterbfb197e2014-10-07 13:59:11 +02002993const char *bdrv_get_device_name(const BlockDriverState *bs)
bellardea2384d2004-08-01 21:59:26 +00002994{
Kevin Wolf4c265bf2016-02-26 10:22:16 +01002995 return bdrv_get_parent_name(bs) ?: "";
bellardea2384d2004-08-01 21:59:26 +00002996}
2997
Alberto Garcia9b2aa842015-04-08 12:29:18 +03002998/* This can be used to identify nodes that might not have a device
2999 * name associated. Since node and device names live in the same
3000 * namespace, the result is unambiguous. The exception is if both are
3001 * absent, then this returns an empty (non-null) string. */
3002const char *bdrv_get_device_or_node_name(const BlockDriverState *bs)
3003{
Kevin Wolf4c265bf2016-02-26 10:22:16 +01003004 return bdrv_get_parent_name(bs) ?: bs->node_name;
Alberto Garcia9b2aa842015-04-08 12:29:18 +03003005}
3006
Markus Armbrusterc8433282012-06-05 16:49:24 +02003007int bdrv_get_flags(BlockDriverState *bs)
3008{
3009 return bs->open_flags;
3010}
3011
Peter Lieven3ac21622013-06-28 12:47:42 +02003012int bdrv_has_zero_init_1(BlockDriverState *bs)
3013{
3014 return 1;
3015}
3016
Kevin Wolff2feebb2010-04-14 17:30:35 +02003017int bdrv_has_zero_init(BlockDriverState *bs)
3018{
3019 assert(bs->drv);
3020
Paolo Bonzini11212d82013-09-04 19:00:27 +02003021 /* If BS is a copy on write image, it is initialized to
3022 the contents of the base image, which may not be zeroes. */
Kevin Wolf760e0062015-06-17 14:55:21 +02003023 if (bs->backing) {
Paolo Bonzini11212d82013-09-04 19:00:27 +02003024 return 0;
3025 }
Kevin Wolf336c1c12010-07-28 11:26:29 +02003026 if (bs->drv->bdrv_has_zero_init) {
3027 return bs->drv->bdrv_has_zero_init(bs);
Kevin Wolff2feebb2010-04-14 17:30:35 +02003028 }
3029
Peter Lieven3ac21622013-06-28 12:47:42 +02003030 /* safe default */
3031 return 0;
Kevin Wolff2feebb2010-04-14 17:30:35 +02003032}
3033
Peter Lieven4ce78692013-10-24 12:06:54 +02003034bool bdrv_unallocated_blocks_are_zero(BlockDriverState *bs)
3035{
3036 BlockDriverInfo bdi;
3037
Kevin Wolf760e0062015-06-17 14:55:21 +02003038 if (bs->backing) {
Peter Lieven4ce78692013-10-24 12:06:54 +02003039 return false;
3040 }
3041
3042 if (bdrv_get_info(bs, &bdi) == 0) {
3043 return bdi.unallocated_blocks_are_zero;
3044 }
3045
3046 return false;
3047}
3048
3049bool bdrv_can_write_zeroes_with_unmap(BlockDriverState *bs)
3050{
3051 BlockDriverInfo bdi;
3052
Denis V. Lunev2f0342e2016-07-14 16:33:26 +03003053 if (!(bs->open_flags & BDRV_O_UNMAP)) {
Peter Lieven4ce78692013-10-24 12:06:54 +02003054 return false;
3055 }
3056
3057 if (bdrv_get_info(bs, &bdi) == 0) {
3058 return bdi.can_write_zeroes_with_unmap;
3059 }
3060
3061 return false;
3062}
3063
aliguori045df332009-03-05 23:00:48 +00003064const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
3065{
Kevin Wolf760e0062015-06-17 14:55:21 +02003066 if (bs->backing && bs->backing->bs->encrypted)
aliguori045df332009-03-05 23:00:48 +00003067 return bs->backing_file;
3068 else if (bs->encrypted)
3069 return bs->filename;
3070 else
3071 return NULL;
3072}
3073
ths5fafdf22007-09-16 21:08:06 +00003074void bdrv_get_backing_filename(BlockDriverState *bs,
bellard83f64092006-08-01 16:21:11 +00003075 char *filename, int filename_size)
bellardea2384d2004-08-01 21:59:26 +00003076{
Kevin Wolf3574c602011-10-26 11:02:11 +02003077 pstrcpy(filename, filename_size, bs->backing_file);
bellardea2384d2004-08-01 21:59:26 +00003078}
3079
bellardfaea38e2006-08-05 21:31:00 +00003080int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
3081{
3082 BlockDriver *drv = bs->drv;
3083 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00003084 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00003085 if (!drv->bdrv_get_info)
3086 return -ENOTSUP;
3087 memset(bdi, 0, sizeof(*bdi));
3088 return drv->bdrv_get_info(bs, bdi);
3089}
3090
Max Reitzeae041f2013-10-09 10:46:16 +02003091ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs)
3092{
3093 BlockDriver *drv = bs->drv;
3094 if (drv && drv->bdrv_get_specific_info) {
3095 return drv->bdrv_get_specific_info(bs);
3096 }
3097 return NULL;
3098}
3099
Eric Blakea31939e2015-11-18 01:52:54 -07003100void bdrv_debug_event(BlockDriverState *bs, BlkdebugEvent event)
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01003101{
Kevin Wolfbf736fe2013-06-05 15:17:55 +02003102 if (!bs || !bs->drv || !bs->drv->bdrv_debug_event) {
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01003103 return;
3104 }
3105
Kevin Wolfbf736fe2013-06-05 15:17:55 +02003106 bs->drv->bdrv_debug_event(bs, event);
Kevin Wolf41c695c2012-12-06 14:32:58 +01003107}
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01003108
Kevin Wolf41c695c2012-12-06 14:32:58 +01003109int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event,
3110 const char *tag)
3111{
3112 while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003113 bs = bs->file ? bs->file->bs : NULL;
Kevin Wolf41c695c2012-12-06 14:32:58 +01003114 }
3115
3116 if (bs && bs->drv && bs->drv->bdrv_debug_breakpoint) {
3117 return bs->drv->bdrv_debug_breakpoint(bs, event, tag);
3118 }
3119
3120 return -ENOTSUP;
3121}
3122
Fam Zheng4cc70e92013-11-20 10:01:54 +08003123int bdrv_debug_remove_breakpoint(BlockDriverState *bs, const char *tag)
3124{
3125 while (bs && bs->drv && !bs->drv->bdrv_debug_remove_breakpoint) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003126 bs = bs->file ? bs->file->bs : NULL;
Fam Zheng4cc70e92013-11-20 10:01:54 +08003127 }
3128
3129 if (bs && bs->drv && bs->drv->bdrv_debug_remove_breakpoint) {
3130 return bs->drv->bdrv_debug_remove_breakpoint(bs, tag);
3131 }
3132
3133 return -ENOTSUP;
3134}
3135
Kevin Wolf41c695c2012-12-06 14:32:58 +01003136int bdrv_debug_resume(BlockDriverState *bs, const char *tag)
3137{
Max Reitz938789e2014-03-10 23:44:08 +01003138 while (bs && (!bs->drv || !bs->drv->bdrv_debug_resume)) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003139 bs = bs->file ? bs->file->bs : NULL;
Kevin Wolf41c695c2012-12-06 14:32:58 +01003140 }
3141
3142 if (bs && bs->drv && bs->drv->bdrv_debug_resume) {
3143 return bs->drv->bdrv_debug_resume(bs, tag);
3144 }
3145
3146 return -ENOTSUP;
3147}
3148
3149bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag)
3150{
3151 while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003152 bs = bs->file ? bs->file->bs : NULL;
Kevin Wolf41c695c2012-12-06 14:32:58 +01003153 }
3154
3155 if (bs && bs->drv && bs->drv->bdrv_debug_is_suspended) {
3156 return bs->drv->bdrv_debug_is_suspended(bs, tag);
3157 }
3158
3159 return false;
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01003160}
3161
Jeff Codyb1b1d782012-10-16 15:49:09 -04003162/* backing_file can either be relative, or absolute, or a protocol. If it is
3163 * relative, it must be relative to the chain. So, passing in bs->filename
3164 * from a BDS as backing_file should not be done, as that may be relative to
3165 * the CWD rather than the chain. */
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00003166BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs,
3167 const char *backing_file)
3168{
Jeff Codyb1b1d782012-10-16 15:49:09 -04003169 char *filename_full = NULL;
3170 char *backing_file_full = NULL;
3171 char *filename_tmp = NULL;
3172 int is_protocol = 0;
3173 BlockDriverState *curr_bs = NULL;
3174 BlockDriverState *retval = NULL;
Jeff Cody418661e2017-01-25 20:08:20 -05003175 Error *local_error = NULL;
Jeff Codyb1b1d782012-10-16 15:49:09 -04003176
3177 if (!bs || !bs->drv || !backing_file) {
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00003178 return NULL;
3179 }
3180
Jeff Codyb1b1d782012-10-16 15:49:09 -04003181 filename_full = g_malloc(PATH_MAX);
3182 backing_file_full = g_malloc(PATH_MAX);
3183 filename_tmp = g_malloc(PATH_MAX);
3184
3185 is_protocol = path_has_protocol(backing_file);
3186
Kevin Wolf760e0062015-06-17 14:55:21 +02003187 for (curr_bs = bs; curr_bs->backing; curr_bs = curr_bs->backing->bs) {
Jeff Codyb1b1d782012-10-16 15:49:09 -04003188
3189 /* If either of the filename paths is actually a protocol, then
3190 * compare unmodified paths; otherwise make paths relative */
3191 if (is_protocol || path_has_protocol(curr_bs->backing_file)) {
3192 if (strcmp(backing_file, curr_bs->backing_file) == 0) {
Kevin Wolf760e0062015-06-17 14:55:21 +02003193 retval = curr_bs->backing->bs;
Jeff Codyb1b1d782012-10-16 15:49:09 -04003194 break;
3195 }
Jeff Cody418661e2017-01-25 20:08:20 -05003196 /* Also check against the full backing filename for the image */
3197 bdrv_get_full_backing_filename(curr_bs, backing_file_full, PATH_MAX,
3198 &local_error);
3199 if (local_error == NULL) {
3200 if (strcmp(backing_file, backing_file_full) == 0) {
3201 retval = curr_bs->backing->bs;
3202 break;
3203 }
3204 } else {
3205 error_free(local_error);
3206 local_error = NULL;
3207 }
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00003208 } else {
Jeff Codyb1b1d782012-10-16 15:49:09 -04003209 /* If not an absolute filename path, make it relative to the current
3210 * image's filename path */
3211 path_combine(filename_tmp, PATH_MAX, curr_bs->filename,
3212 backing_file);
3213
3214 /* We are going to compare absolute pathnames */
3215 if (!realpath(filename_tmp, filename_full)) {
3216 continue;
3217 }
3218
3219 /* We need to make sure the backing filename we are comparing against
3220 * is relative to the current image filename (or absolute) */
3221 path_combine(filename_tmp, PATH_MAX, curr_bs->filename,
3222 curr_bs->backing_file);
3223
3224 if (!realpath(filename_tmp, backing_file_full)) {
3225 continue;
3226 }
3227
3228 if (strcmp(backing_file_full, filename_full) == 0) {
Kevin Wolf760e0062015-06-17 14:55:21 +02003229 retval = curr_bs->backing->bs;
Jeff Codyb1b1d782012-10-16 15:49:09 -04003230 break;
3231 }
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00003232 }
3233 }
3234
Jeff Codyb1b1d782012-10-16 15:49:09 -04003235 g_free(filename_full);
3236 g_free(backing_file_full);
3237 g_free(filename_tmp);
3238 return retval;
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00003239}
3240
Benoît Canetf198fd12012-08-02 10:22:47 +02003241int bdrv_get_backing_file_depth(BlockDriverState *bs)
3242{
3243 if (!bs->drv) {
3244 return 0;
3245 }
3246
Kevin Wolf760e0062015-06-17 14:55:21 +02003247 if (!bs->backing) {
Benoît Canetf198fd12012-08-02 10:22:47 +02003248 return 0;
3249 }
3250
Kevin Wolf760e0062015-06-17 14:55:21 +02003251 return 1 + bdrv_get_backing_file_depth(bs->backing->bs);
Benoît Canetf198fd12012-08-02 10:22:47 +02003252}
3253
bellardea2384d2004-08-01 21:59:26 +00003254void bdrv_init(void)
3255{
Anthony Liguori5efa9d52009-05-09 17:03:42 -05003256 module_call_init(MODULE_INIT_BLOCK);
bellardea2384d2004-08-01 21:59:26 +00003257}
pbrookce1a14d2006-08-07 02:38:06 +00003258
Markus Armbrustereb852012009-10-27 18:41:44 +01003259void bdrv_init_with_whitelist(void)
3260{
3261 use_bdrv_whitelist = 1;
3262 bdrv_init();
3263}
3264
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003265void bdrv_invalidate_cache(BlockDriverState *bs, Error **errp)
Anthony Liguori0f154232011-11-14 15:09:45 -06003266{
Fam Zheng0d1c5c92016-05-11 10:45:33 +08003267 BdrvChild *child;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003268 Error *local_err = NULL;
3269 int ret;
3270
Kevin Wolf3456a8d2014-03-11 10:58:39 +01003271 if (!bs->drv) {
3272 return;
Anthony Liguori0f154232011-11-14 15:09:45 -06003273 }
Kevin Wolf3456a8d2014-03-11 10:58:39 +01003274
Kevin Wolf04c01a52016-01-13 15:56:06 +01003275 if (!(bs->open_flags & BDRV_O_INACTIVE)) {
Alexey Kardashevskiy7ea2d262014-10-09 13:50:46 +11003276 return;
3277 }
Alexey Kardashevskiy7ea2d262014-10-09 13:50:46 +11003278
Vladimir Sementsov-Ogievskiy16e977d2017-01-31 14:23:08 +03003279 QLIST_FOREACH(child, &bs->children, next) {
3280 bdrv_invalidate_cache(child->bs, &local_err);
Fam Zheng0d1c5c92016-05-11 10:45:33 +08003281 if (local_err) {
Fam Zheng0d1c5c92016-05-11 10:45:33 +08003282 error_propagate(errp, local_err);
3283 return;
3284 }
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003285 }
Fam Zheng0d1c5c92016-05-11 10:45:33 +08003286
Vladimir Sementsov-Ogievskiy16e977d2017-01-31 14:23:08 +03003287 bs->open_flags &= ~BDRV_O_INACTIVE;
3288 if (bs->drv->bdrv_invalidate_cache) {
3289 bs->drv->bdrv_invalidate_cache(bs, &local_err);
Fam Zheng0d1c5c92016-05-11 10:45:33 +08003290 if (local_err) {
3291 bs->open_flags |= BDRV_O_INACTIVE;
3292 error_propagate(errp, local_err);
3293 return;
3294 }
Kevin Wolf3456a8d2014-03-11 10:58:39 +01003295 }
3296
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003297 ret = refresh_total_sectors(bs, bs->total_sectors);
3298 if (ret < 0) {
Kevin Wolf04c01a52016-01-13 15:56:06 +01003299 bs->open_flags |= BDRV_O_INACTIVE;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003300 error_setg_errno(errp, -ret, "Could not refresh total sector count");
3301 return;
3302 }
Anthony Liguori0f154232011-11-14 15:09:45 -06003303}
3304
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003305void bdrv_invalidate_cache_all(Error **errp)
Anthony Liguori0f154232011-11-14 15:09:45 -06003306{
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003307 BlockDriverState *bs;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003308 Error *local_err = NULL;
Kevin Wolf88be7b42016-05-20 18:49:07 +02003309 BdrvNextIterator it;
Anthony Liguori0f154232011-11-14 15:09:45 -06003310
Kevin Wolf88be7b42016-05-20 18:49:07 +02003311 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Stefan Hajnoczied78cda2014-05-08 16:34:35 +02003312 AioContext *aio_context = bdrv_get_aio_context(bs);
3313
3314 aio_context_acquire(aio_context);
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003315 bdrv_invalidate_cache(bs, &local_err);
Stefan Hajnoczied78cda2014-05-08 16:34:35 +02003316 aio_context_release(aio_context);
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003317 if (local_err) {
3318 error_propagate(errp, local_err);
3319 return;
3320 }
Anthony Liguori0f154232011-11-14 15:09:45 -06003321 }
3322}
3323
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003324static int bdrv_inactivate_recurse(BlockDriverState *bs,
3325 bool setting_flag)
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003326{
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003327 BdrvChild *child;
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003328 int ret;
3329
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003330 if (!setting_flag && bs->drv->bdrv_inactivate) {
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003331 ret = bs->drv->bdrv_inactivate(bs);
3332 if (ret < 0) {
3333 return ret;
3334 }
3335 }
3336
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003337 QLIST_FOREACH(child, &bs->children, next) {
3338 ret = bdrv_inactivate_recurse(child->bs, setting_flag);
3339 if (ret < 0) {
3340 return ret;
3341 }
3342 }
3343
3344 if (setting_flag) {
3345 bs->open_flags |= BDRV_O_INACTIVE;
3346 }
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003347 return 0;
3348}
3349
3350int bdrv_inactivate_all(void)
3351{
Max Reitz79720af2016-03-16 19:54:44 +01003352 BlockDriverState *bs = NULL;
Kevin Wolf88be7b42016-05-20 18:49:07 +02003353 BdrvNextIterator it;
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003354 int ret = 0;
3355 int pass;
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003356
Kevin Wolf88be7b42016-05-20 18:49:07 +02003357 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003358 aio_context_acquire(bdrv_get_aio_context(bs));
3359 }
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003360
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003361 /* We do two passes of inactivation. The first pass calls to drivers'
3362 * .bdrv_inactivate callbacks recursively so all cache is flushed to disk;
3363 * the second pass sets the BDRV_O_INACTIVE flag so that no further write
3364 * is allowed. */
3365 for (pass = 0; pass < 2; pass++) {
Kevin Wolf88be7b42016-05-20 18:49:07 +02003366 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003367 ret = bdrv_inactivate_recurse(bs, pass);
3368 if (ret < 0) {
3369 goto out;
3370 }
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003371 }
3372 }
3373
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003374out:
Kevin Wolf88be7b42016-05-20 18:49:07 +02003375 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003376 aio_context_release(bdrv_get_aio_context(bs));
3377 }
3378
3379 return ret;
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003380}
3381
Kevin Wolff9f05dc2011-07-15 13:50:26 +02003382/**************************************************************/
bellard19cb3732006-08-19 11:45:59 +00003383/* removable device support */
3384
3385/**
3386 * Return TRUE if the media is present
3387 */
Max Reitze031f752015-10-19 17:53:11 +02003388bool bdrv_is_inserted(BlockDriverState *bs)
bellard19cb3732006-08-19 11:45:59 +00003389{
3390 BlockDriver *drv = bs->drv;
Max Reitz28d7a782015-10-19 17:53:13 +02003391 BdrvChild *child;
Markus Armbrustera1aff5b2011-09-06 18:58:41 +02003392
Max Reitze031f752015-10-19 17:53:11 +02003393 if (!drv) {
3394 return false;
3395 }
Max Reitz28d7a782015-10-19 17:53:13 +02003396 if (drv->bdrv_is_inserted) {
3397 return drv->bdrv_is_inserted(bs);
Max Reitze031f752015-10-19 17:53:11 +02003398 }
Max Reitz28d7a782015-10-19 17:53:13 +02003399 QLIST_FOREACH(child, &bs->children, next) {
3400 if (!bdrv_is_inserted(child->bs)) {
3401 return false;
3402 }
3403 }
3404 return true;
bellard19cb3732006-08-19 11:45:59 +00003405}
3406
3407/**
Markus Armbruster8e49ca42011-08-03 15:08:08 +02003408 * Return whether the media changed since the last call to this
3409 * function, or -ENOTSUP if we don't know. Most drivers don't know.
bellard19cb3732006-08-19 11:45:59 +00003410 */
3411int bdrv_media_changed(BlockDriverState *bs)
3412{
3413 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +00003414
Markus Armbruster8e49ca42011-08-03 15:08:08 +02003415 if (drv && drv->bdrv_media_changed) {
3416 return drv->bdrv_media_changed(bs);
3417 }
3418 return -ENOTSUP;
bellard19cb3732006-08-19 11:45:59 +00003419}
3420
3421/**
3422 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
3423 */
Luiz Capitulinof36f3942012-02-03 16:24:53 -02003424void bdrv_eject(BlockDriverState *bs, bool eject_flag)
bellard19cb3732006-08-19 11:45:59 +00003425{
3426 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +00003427
Markus Armbruster822e1cd2011-07-20 18:23:42 +02003428 if (drv && drv->bdrv_eject) {
3429 drv->bdrv_eject(bs, eject_flag);
bellard19cb3732006-08-19 11:45:59 +00003430 }
bellard19cb3732006-08-19 11:45:59 +00003431}
3432
bellard19cb3732006-08-19 11:45:59 +00003433/**
3434 * Lock or unlock the media (if it is locked, the user won't be able
3435 * to eject it manually).
3436 */
Markus Armbruster025e8492011-09-06 18:58:47 +02003437void bdrv_lock_medium(BlockDriverState *bs, bool locked)
bellard19cb3732006-08-19 11:45:59 +00003438{
3439 BlockDriver *drv = bs->drv;
3440
Markus Armbruster025e8492011-09-06 18:58:47 +02003441 trace_bdrv_lock_medium(bs, locked);
Stefan Hajnoczib8c6d092011-03-29 20:04:40 +01003442
Markus Armbruster025e8492011-09-06 18:58:47 +02003443 if (drv && drv->bdrv_lock_medium) {
3444 drv->bdrv_lock_medium(bs, locked);
bellard19cb3732006-08-19 11:45:59 +00003445 }
3446}
ths985a03b2007-12-24 16:10:43 +00003447
Fam Zheng9fcb0252013-08-23 09:14:46 +08003448/* Get a reference to bs */
3449void bdrv_ref(BlockDriverState *bs)
3450{
3451 bs->refcnt++;
3452}
3453
3454/* Release a previously grabbed reference to bs.
3455 * If after releasing, reference count is zero, the BlockDriverState is
3456 * deleted. */
3457void bdrv_unref(BlockDriverState *bs)
3458{
Jeff Cody9a4d5ca2014-07-23 17:22:57 -04003459 if (!bs) {
3460 return;
3461 }
Fam Zheng9fcb0252013-08-23 09:14:46 +08003462 assert(bs->refcnt > 0);
3463 if (--bs->refcnt == 0) {
3464 bdrv_delete(bs);
3465 }
3466}
3467
Fam Zhengfbe40ff2014-05-23 21:29:42 +08003468struct BdrvOpBlocker {
3469 Error *reason;
3470 QLIST_ENTRY(BdrvOpBlocker) list;
3471};
3472
3473bool bdrv_op_is_blocked(BlockDriverState *bs, BlockOpType op, Error **errp)
3474{
3475 BdrvOpBlocker *blocker;
3476 assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
3477 if (!QLIST_EMPTY(&bs->op_blockers[op])) {
3478 blocker = QLIST_FIRST(&bs->op_blockers[op]);
3479 if (errp) {
Markus Armbrustere43bfd92015-12-18 16:35:15 +01003480 *errp = error_copy(blocker->reason);
3481 error_prepend(errp, "Node '%s' is busy: ",
3482 bdrv_get_device_or_node_name(bs));
Fam Zhengfbe40ff2014-05-23 21:29:42 +08003483 }
3484 return true;
3485 }
3486 return false;
3487}
3488
3489void bdrv_op_block(BlockDriverState *bs, BlockOpType op, Error *reason)
3490{
3491 BdrvOpBlocker *blocker;
3492 assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
3493
Markus Armbruster5839e532014-08-19 10:31:08 +02003494 blocker = g_new0(BdrvOpBlocker, 1);
Fam Zhengfbe40ff2014-05-23 21:29:42 +08003495 blocker->reason = reason;
3496 QLIST_INSERT_HEAD(&bs->op_blockers[op], blocker, list);
3497}
3498
3499void bdrv_op_unblock(BlockDriverState *bs, BlockOpType op, Error *reason)
3500{
3501 BdrvOpBlocker *blocker, *next;
3502 assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
3503 QLIST_FOREACH_SAFE(blocker, &bs->op_blockers[op], list, next) {
3504 if (blocker->reason == reason) {
3505 QLIST_REMOVE(blocker, list);
3506 g_free(blocker);
3507 }
3508 }
3509}
3510
3511void bdrv_op_block_all(BlockDriverState *bs, Error *reason)
3512{
3513 int i;
3514 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
3515 bdrv_op_block(bs, i, reason);
3516 }
3517}
3518
3519void bdrv_op_unblock_all(BlockDriverState *bs, Error *reason)
3520{
3521 int i;
3522 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
3523 bdrv_op_unblock(bs, i, reason);
3524 }
3525}
3526
3527bool bdrv_op_blocker_is_empty(BlockDriverState *bs)
3528{
3529 int i;
3530
3531 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
3532 if (!QLIST_EMPTY(&bs->op_blockers[i])) {
3533 return false;
3534 }
3535 }
3536 return true;
3537}
3538
Luiz Capitulinod92ada22012-11-30 10:52:09 -02003539void bdrv_img_create(const char *filename, const char *fmt,
3540 const char *base_filename, const char *base_fmt,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003541 char *options, uint64_t img_size, int flags,
3542 Error **errp, bool quiet)
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003543{
Chunyan Liu83d05212014-06-05 17:20:51 +08003544 QemuOptsList *create_opts = NULL;
3545 QemuOpts *opts = NULL;
3546 const char *backing_fmt, *backing_file;
3547 int64_t size;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003548 BlockDriver *drv, *proto_drv;
Max Reitzcc84d902013-09-06 17:14:26 +02003549 Error *local_err = NULL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003550 int ret = 0;
3551
3552 /* Find driver and parse its options */
3553 drv = bdrv_find_format(fmt);
3554 if (!drv) {
Luiz Capitulino71c79812012-11-30 10:52:04 -02003555 error_setg(errp, "Unknown file format '%s'", fmt);
Luiz Capitulinod92ada22012-11-30 10:52:09 -02003556 return;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003557 }
3558
Max Reitzb65a5e12015-02-05 13:58:12 -05003559 proto_drv = bdrv_find_protocol(filename, true, errp);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003560 if (!proto_drv) {
Luiz Capitulinod92ada22012-11-30 10:52:09 -02003561 return;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003562 }
3563
Max Reitzc6149722014-12-02 18:32:45 +01003564 if (!drv->create_opts) {
3565 error_setg(errp, "Format driver '%s' does not support image creation",
3566 drv->format_name);
3567 return;
3568 }
3569
3570 if (!proto_drv->create_opts) {
3571 error_setg(errp, "Protocol driver '%s' does not support image creation",
3572 proto_drv->format_name);
3573 return;
3574 }
3575
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003576 create_opts = qemu_opts_append(create_opts, drv->create_opts);
3577 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003578
3579 /* Create parameter list with default values */
Chunyan Liu83d05212014-06-05 17:20:51 +08003580 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbruster39101f22015-02-12 16:46:36 +01003581 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003582
3583 /* Parse -o options */
3584 if (options) {
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003585 qemu_opts_do_parse(opts, options, NULL, &local_err);
3586 if (local_err) {
3587 error_report_err(local_err);
3588 local_err = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003589 error_setg(errp, "Invalid options for file format '%s'", fmt);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003590 goto out;
3591 }
3592 }
3593
3594 if (base_filename) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003595 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &local_err);
Markus Armbruster6be41942015-02-12 17:49:02 +01003596 if (local_err) {
Luiz Capitulino71c79812012-11-30 10:52:04 -02003597 error_setg(errp, "Backing file not supported for file format '%s'",
3598 fmt);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003599 goto out;
3600 }
3601 }
3602
3603 if (base_fmt) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003604 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &local_err);
Markus Armbruster6be41942015-02-12 17:49:02 +01003605 if (local_err) {
Luiz Capitulino71c79812012-11-30 10:52:04 -02003606 error_setg(errp, "Backing file format not supported for file "
3607 "format '%s'", fmt);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003608 goto out;
3609 }
3610 }
3611
Chunyan Liu83d05212014-06-05 17:20:51 +08003612 backing_file = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
3613 if (backing_file) {
3614 if (!strcmp(filename, backing_file)) {
Luiz Capitulino71c79812012-11-30 10:52:04 -02003615 error_setg(errp, "Error: Trying to create an image with the "
3616 "same filename as the backing file");
Jes Sorensen792da932010-12-16 13:52:17 +01003617 goto out;
3618 }
3619 }
3620
Chunyan Liu83d05212014-06-05 17:20:51 +08003621 backing_fmt = qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003622
3623 // The size for the image must always be specified, with one exception:
3624 // If we are using a backing file, we can obtain the size from there
Chunyan Liu83d05212014-06-05 17:20:51 +08003625 size = qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 0);
3626 if (size == -1) {
3627 if (backing_file) {
Max Reitz66f6b812013-12-03 14:57:52 +01003628 BlockDriverState *bs;
Max Reitz29168012014-11-26 17:20:27 +01003629 char *full_backing = g_new0(char, PATH_MAX);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003630 int64_t size;
Paolo Bonzini63090da2012-04-12 14:01:03 +02003631 int back_flags;
Max Reitze6641712015-08-26 19:47:48 +02003632 QDict *backing_options = NULL;
Paolo Bonzini63090da2012-04-12 14:01:03 +02003633
Max Reitz29168012014-11-26 17:20:27 +01003634 bdrv_get_full_backing_filename_from_filename(filename, backing_file,
3635 full_backing, PATH_MAX,
3636 &local_err);
3637 if (local_err) {
3638 g_free(full_backing);
3639 goto out;
3640 }
3641
Paolo Bonzini63090da2012-04-12 14:01:03 +02003642 /* backing files always opened read-only */
Kevin Wolf61de4c62016-03-18 17:46:45 +01003643 back_flags = flags;
Kevin Wolfbfd18d12016-03-04 14:28:01 +01003644 back_flags &= ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003645
Max Reitze6641712015-08-26 19:47:48 +02003646 if (backing_fmt) {
3647 backing_options = qdict_new();
3648 qdict_put(backing_options, "driver",
3649 qstring_from_str(backing_fmt));
3650 }
3651
Max Reitz5b363932016-05-17 16:41:31 +02003652 bs = bdrv_open(full_backing, NULL, backing_options, back_flags,
3653 &local_err);
Max Reitz29168012014-11-26 17:20:27 +01003654 g_free(full_backing);
Max Reitz5b363932016-05-17 16:41:31 +02003655 if (!bs) {
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003656 goto out;
3657 }
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003658 size = bdrv_getlength(bs);
3659 if (size < 0) {
3660 error_setg_errno(errp, -size, "Could not get size of '%s'",
3661 backing_file);
3662 bdrv_unref(bs);
3663 goto out;
3664 }
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003665
Markus Armbruster39101f22015-02-12 16:46:36 +01003666 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, size, &error_abort);
Max Reitz66f6b812013-12-03 14:57:52 +01003667
3668 bdrv_unref(bs);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003669 } else {
Luiz Capitulino71c79812012-11-30 10:52:04 -02003670 error_setg(errp, "Image creation needs a size parameter");
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003671 goto out;
3672 }
3673 }
3674
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003675 if (!quiet) {
Kővágó, Zoltánfe646692015-07-07 16:42:10 +02003676 printf("Formatting '%s', fmt=%s ", filename, fmt);
Fam Zheng43c5d8f2014-12-09 15:38:04 +08003677 qemu_opts_print(opts, " ");
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003678 puts("");
3679 }
Chunyan Liu83d05212014-06-05 17:20:51 +08003680
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003681 ret = bdrv_create(drv, filename, opts, &local_err);
Chunyan Liu83d05212014-06-05 17:20:51 +08003682
Max Reitzcc84d902013-09-06 17:14:26 +02003683 if (ret == -EFBIG) {
3684 /* This is generally a better message than whatever the driver would
3685 * deliver (especially because of the cluster_size_hint), since that
3686 * is most probably not much different from "image too large". */
3687 const char *cluster_size_hint = "";
Chunyan Liu83d05212014-06-05 17:20:51 +08003688 if (qemu_opt_get_size(opts, BLOCK_OPT_CLUSTER_SIZE, 0)) {
Max Reitzcc84d902013-09-06 17:14:26 +02003689 cluster_size_hint = " (try using a larger cluster size)";
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003690 }
Max Reitzcc84d902013-09-06 17:14:26 +02003691 error_setg(errp, "The image size is too large for file format '%s'"
3692 "%s", fmt, cluster_size_hint);
3693 error_free(local_err);
3694 local_err = NULL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003695 }
3696
3697out:
Chunyan Liu83d05212014-06-05 17:20:51 +08003698 qemu_opts_del(opts);
3699 qemu_opts_free(create_opts);
Eduardo Habkost621ff942016-06-13 18:57:56 -03003700 error_propagate(errp, local_err);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003701}
Stefan Hajnoczi85d126f2013-03-07 13:41:48 +01003702
3703AioContext *bdrv_get_aio_context(BlockDriverState *bs)
3704{
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003705 return bs->aio_context;
3706}
3707
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003708static void bdrv_do_remove_aio_context_notifier(BdrvAioNotifier *ban)
3709{
3710 QLIST_REMOVE(ban, list);
3711 g_free(ban);
3712}
3713
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003714void bdrv_detach_aio_context(BlockDriverState *bs)
3715{
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003716 BdrvAioNotifier *baf, *baf_tmp;
Max Reitzb97511c2016-05-17 13:38:04 +02003717 BdrvChild *child;
Max Reitz33384422014-06-20 21:57:33 +02003718
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003719 if (!bs->drv) {
3720 return;
3721 }
3722
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003723 assert(!bs->walking_aio_notifiers);
3724 bs->walking_aio_notifiers = true;
3725 QLIST_FOREACH_SAFE(baf, &bs->aio_notifiers, list, baf_tmp) {
3726 if (baf->deleted) {
3727 bdrv_do_remove_aio_context_notifier(baf);
3728 } else {
3729 baf->detach_aio_context(baf->opaque);
3730 }
Max Reitz33384422014-06-20 21:57:33 +02003731 }
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003732 /* Never mind iterating again to check for ->deleted. bdrv_close() will
3733 * remove remaining aio notifiers if we aren't called again.
3734 */
3735 bs->walking_aio_notifiers = false;
Max Reitz33384422014-06-20 21:57:33 +02003736
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003737 if (bs->drv->bdrv_detach_aio_context) {
3738 bs->drv->bdrv_detach_aio_context(bs);
3739 }
Max Reitzb97511c2016-05-17 13:38:04 +02003740 QLIST_FOREACH(child, &bs->children, next) {
3741 bdrv_detach_aio_context(child->bs);
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003742 }
3743
3744 bs->aio_context = NULL;
3745}
3746
3747void bdrv_attach_aio_context(BlockDriverState *bs,
3748 AioContext *new_context)
3749{
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003750 BdrvAioNotifier *ban, *ban_tmp;
Max Reitzb97511c2016-05-17 13:38:04 +02003751 BdrvChild *child;
Max Reitz33384422014-06-20 21:57:33 +02003752
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003753 if (!bs->drv) {
3754 return;
3755 }
3756
3757 bs->aio_context = new_context;
3758
Max Reitzb97511c2016-05-17 13:38:04 +02003759 QLIST_FOREACH(child, &bs->children, next) {
3760 bdrv_attach_aio_context(child->bs, new_context);
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003761 }
3762 if (bs->drv->bdrv_attach_aio_context) {
3763 bs->drv->bdrv_attach_aio_context(bs, new_context);
3764 }
Max Reitz33384422014-06-20 21:57:33 +02003765
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003766 assert(!bs->walking_aio_notifiers);
3767 bs->walking_aio_notifiers = true;
3768 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_tmp) {
3769 if (ban->deleted) {
3770 bdrv_do_remove_aio_context_notifier(ban);
3771 } else {
3772 ban->attached_aio_context(new_context, ban->opaque);
3773 }
Max Reitz33384422014-06-20 21:57:33 +02003774 }
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003775 bs->walking_aio_notifiers = false;
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003776}
3777
3778void bdrv_set_aio_context(BlockDriverState *bs, AioContext *new_context)
3779{
Fam Zheng53ec73e2015-05-29 18:53:14 +08003780 bdrv_drain(bs); /* ensure there are no in-flight requests */
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003781
3782 bdrv_detach_aio_context(bs);
3783
3784 /* This function executes in the old AioContext so acquire the new one in
3785 * case it runs in a different thread.
3786 */
3787 aio_context_acquire(new_context);
3788 bdrv_attach_aio_context(bs, new_context);
3789 aio_context_release(new_context);
Stefan Hajnoczi85d126f2013-03-07 13:41:48 +01003790}
Stefan Hajnoczid616b222013-06-24 17:13:10 +02003791
Max Reitz33384422014-06-20 21:57:33 +02003792void bdrv_add_aio_context_notifier(BlockDriverState *bs,
3793 void (*attached_aio_context)(AioContext *new_context, void *opaque),
3794 void (*detach_aio_context)(void *opaque), void *opaque)
3795{
3796 BdrvAioNotifier *ban = g_new(BdrvAioNotifier, 1);
3797 *ban = (BdrvAioNotifier){
3798 .attached_aio_context = attached_aio_context,
3799 .detach_aio_context = detach_aio_context,
3800 .opaque = opaque
3801 };
3802
3803 QLIST_INSERT_HEAD(&bs->aio_notifiers, ban, list);
3804}
3805
3806void bdrv_remove_aio_context_notifier(BlockDriverState *bs,
3807 void (*attached_aio_context)(AioContext *,
3808 void *),
3809 void (*detach_aio_context)(void *),
3810 void *opaque)
3811{
3812 BdrvAioNotifier *ban, *ban_next;
3813
3814 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) {
3815 if (ban->attached_aio_context == attached_aio_context &&
3816 ban->detach_aio_context == detach_aio_context &&
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003817 ban->opaque == opaque &&
3818 ban->deleted == false)
Max Reitz33384422014-06-20 21:57:33 +02003819 {
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003820 if (bs->walking_aio_notifiers) {
3821 ban->deleted = true;
3822 } else {
3823 bdrv_do_remove_aio_context_notifier(ban);
3824 }
Max Reitz33384422014-06-20 21:57:33 +02003825 return;
3826 }
3827 }
3828
3829 abort();
3830}
3831
Max Reitz77485432014-10-27 11:12:50 +01003832int bdrv_amend_options(BlockDriverState *bs, QemuOpts *opts,
Max Reitz8b139762015-07-27 17:51:32 +02003833 BlockDriverAmendStatusCB *status_cb, void *cb_opaque)
Max Reitz6f176b42013-09-03 10:09:50 +02003834{
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003835 if (!bs->drv->bdrv_amend_options) {
Max Reitz6f176b42013-09-03 10:09:50 +02003836 return -ENOTSUP;
3837 }
Max Reitz8b139762015-07-27 17:51:32 +02003838 return bs->drv->bdrv_amend_options(bs, opts, status_cb, cb_opaque);
Max Reitz6f176b42013-09-03 10:09:50 +02003839}
Benoît Canetf6186f42013-10-02 14:33:48 +02003840
Benoît Canetb5042a32014-03-03 19:11:34 +01003841/* This function will be called by the bdrv_recurse_is_first_non_filter method
3842 * of block filter and by bdrv_is_first_non_filter.
3843 * It is used to test if the given bs is the candidate or recurse more in the
3844 * node graph.
Benoît Canet212a5a82014-01-23 21:31:36 +01003845 */
Benoît Canet212a5a82014-01-23 21:31:36 +01003846bool bdrv_recurse_is_first_non_filter(BlockDriverState *bs,
3847 BlockDriverState *candidate)
Benoît Canetf6186f42013-10-02 14:33:48 +02003848{
Benoît Canetb5042a32014-03-03 19:11:34 +01003849 /* return false if basic checks fails */
3850 if (!bs || !bs->drv) {
3851 return false;
3852 }
3853
3854 /* the code reached a non block filter driver -> check if the bs is
3855 * the same as the candidate. It's the recursion termination condition.
3856 */
3857 if (!bs->drv->is_filter) {
3858 return bs == candidate;
3859 }
3860 /* Down this path the driver is a block filter driver */
3861
3862 /* If the block filter recursion method is defined use it to recurse down
3863 * the node graph.
3864 */
3865 if (bs->drv->bdrv_recurse_is_first_non_filter) {
Benoît Canet212a5a82014-01-23 21:31:36 +01003866 return bs->drv->bdrv_recurse_is_first_non_filter(bs, candidate);
3867 }
3868
Benoît Canetb5042a32014-03-03 19:11:34 +01003869 /* the driver is a block filter but don't allow to recurse -> return false
3870 */
3871 return false;
Benoît Canet212a5a82014-01-23 21:31:36 +01003872}
3873
3874/* This function checks if the candidate is the first non filter bs down it's
3875 * bs chain. Since we don't have pointers to parents it explore all bs chains
3876 * from the top. Some filters can choose not to pass down the recursion.
3877 */
3878bool bdrv_is_first_non_filter(BlockDriverState *candidate)
3879{
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003880 BlockDriverState *bs;
Kevin Wolf88be7b42016-05-20 18:49:07 +02003881 BdrvNextIterator it;
Benoît Canet212a5a82014-01-23 21:31:36 +01003882
3883 /* walk down the bs forest recursively */
Kevin Wolf88be7b42016-05-20 18:49:07 +02003884 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Benoît Canet212a5a82014-01-23 21:31:36 +01003885 bool perm;
3886
Benoît Canetb5042a32014-03-03 19:11:34 +01003887 /* try to recurse in this top level bs */
Kevin Wolfe6dc8a12014-02-04 11:45:31 +01003888 perm = bdrv_recurse_is_first_non_filter(bs, candidate);
Benoît Canet212a5a82014-01-23 21:31:36 +01003889
3890 /* candidate is the first non filter */
3891 if (perm) {
3892 return true;
3893 }
3894 }
3895
3896 return false;
Benoît Canetf6186f42013-10-02 14:33:48 +02003897}
Benoît Canet09158f02014-06-27 18:25:25 +02003898
Wen Congyange12f3782015-07-17 10:12:22 +08003899BlockDriverState *check_to_replace_node(BlockDriverState *parent_bs,
3900 const char *node_name, Error **errp)
Benoît Canet09158f02014-06-27 18:25:25 +02003901{
3902 BlockDriverState *to_replace_bs = bdrv_find_node(node_name);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003903 AioContext *aio_context;
3904
Benoît Canet09158f02014-06-27 18:25:25 +02003905 if (!to_replace_bs) {
3906 error_setg(errp, "Node name '%s' not found", node_name);
3907 return NULL;
3908 }
3909
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003910 aio_context = bdrv_get_aio_context(to_replace_bs);
3911 aio_context_acquire(aio_context);
3912
Benoît Canet09158f02014-06-27 18:25:25 +02003913 if (bdrv_op_is_blocked(to_replace_bs, BLOCK_OP_TYPE_REPLACE, errp)) {
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003914 to_replace_bs = NULL;
3915 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003916 }
3917
3918 /* We don't want arbitrary node of the BDS chain to be replaced only the top
3919 * most non filter in order to prevent data corruption.
3920 * Another benefit is that this tests exclude backing files which are
3921 * blocked by the backing blockers.
3922 */
Wen Congyange12f3782015-07-17 10:12:22 +08003923 if (!bdrv_recurse_is_first_non_filter(parent_bs, to_replace_bs)) {
Benoît Canet09158f02014-06-27 18:25:25 +02003924 error_setg(errp, "Only top most non filter can be replaced");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003925 to_replace_bs = NULL;
3926 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003927 }
3928
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003929out:
3930 aio_context_release(aio_context);
Benoît Canet09158f02014-06-27 18:25:25 +02003931 return to_replace_bs;
3932}
Ming Lei448ad912014-07-04 18:04:33 +08003933
Max Reitz91af7012014-07-18 20:24:56 +02003934static bool append_open_options(QDict *d, BlockDriverState *bs)
3935{
3936 const QDictEntry *entry;
Kevin Wolf9e700c12015-04-24 15:20:28 +02003937 QemuOptDesc *desc;
Kevin Wolf260fecf2015-04-27 13:46:22 +02003938 BdrvChild *child;
Max Reitz91af7012014-07-18 20:24:56 +02003939 bool found_any = false;
Kevin Wolf260fecf2015-04-27 13:46:22 +02003940 const char *p;
Max Reitz91af7012014-07-18 20:24:56 +02003941
3942 for (entry = qdict_first(bs->options); entry;
3943 entry = qdict_next(bs->options, entry))
3944 {
Kevin Wolf260fecf2015-04-27 13:46:22 +02003945 /* Exclude options for children */
3946 QLIST_FOREACH(child, &bs->children, next) {
3947 if (strstart(qdict_entry_key(entry), child->name, &p)
3948 && (!*p || *p == '.'))
3949 {
3950 break;
3951 }
3952 }
3953 if (child) {
Kevin Wolf9e700c12015-04-24 15:20:28 +02003954 continue;
Max Reitz91af7012014-07-18 20:24:56 +02003955 }
Kevin Wolf9e700c12015-04-24 15:20:28 +02003956
3957 /* And exclude all non-driver-specific options */
3958 for (desc = bdrv_runtime_opts.desc; desc->name; desc++) {
3959 if (!strcmp(qdict_entry_key(entry), desc->name)) {
3960 break;
3961 }
3962 }
3963 if (desc->name) {
3964 continue;
3965 }
3966
3967 qobject_incref(qdict_entry_value(entry));
3968 qdict_put_obj(d, qdict_entry_key(entry), qdict_entry_value(entry));
3969 found_any = true;
Max Reitz91af7012014-07-18 20:24:56 +02003970 }
3971
3972 return found_any;
3973}
3974
3975/* Updates the following BDS fields:
3976 * - exact_filename: A filename which may be used for opening a block device
3977 * which (mostly) equals the given BDS (even without any
3978 * other options; so reading and writing must return the same
3979 * results, but caching etc. may be different)
3980 * - full_open_options: Options which, when given when opening a block device
3981 * (without a filename), result in a BDS (mostly)
3982 * equalling the given one
3983 * - filename: If exact_filename is set, it is copied here. Otherwise,
3984 * full_open_options is converted to a JSON object, prefixed with
3985 * "json:" (for use through the JSON pseudo protocol) and put here.
3986 */
3987void bdrv_refresh_filename(BlockDriverState *bs)
3988{
3989 BlockDriver *drv = bs->drv;
3990 QDict *opts;
3991
3992 if (!drv) {
3993 return;
3994 }
3995
3996 /* This BDS's file name will most probably depend on its file's name, so
3997 * refresh that first */
3998 if (bs->file) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003999 bdrv_refresh_filename(bs->file->bs);
Max Reitz91af7012014-07-18 20:24:56 +02004000 }
4001
4002 if (drv->bdrv_refresh_filename) {
4003 /* Obsolete information is of no use here, so drop the old file name
4004 * information before refreshing it */
4005 bs->exact_filename[0] = '\0';
4006 if (bs->full_open_options) {
4007 QDECREF(bs->full_open_options);
4008 bs->full_open_options = NULL;
4009 }
4010
Kevin Wolf4cdd01d2015-04-27 13:50:54 +02004011 opts = qdict_new();
4012 append_open_options(opts, bs);
4013 drv->bdrv_refresh_filename(bs, opts);
4014 QDECREF(opts);
Max Reitz91af7012014-07-18 20:24:56 +02004015 } else if (bs->file) {
4016 /* Try to reconstruct valid information from the underlying file */
4017 bool has_open_options;
4018
4019 bs->exact_filename[0] = '\0';
4020 if (bs->full_open_options) {
4021 QDECREF(bs->full_open_options);
4022 bs->full_open_options = NULL;
4023 }
4024
4025 opts = qdict_new();
4026 has_open_options = append_open_options(opts, bs);
4027
4028 /* If no specific options have been given for this BDS, the filename of
4029 * the underlying file should suffice for this one as well */
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02004030 if (bs->file->bs->exact_filename[0] && !has_open_options) {
4031 strcpy(bs->exact_filename, bs->file->bs->exact_filename);
Max Reitz91af7012014-07-18 20:24:56 +02004032 }
4033 /* Reconstructing the full options QDict is simple for most format block
4034 * drivers, as long as the full options are known for the underlying
4035 * file BDS. The full options QDict of that file BDS should somehow
4036 * contain a representation of the filename, therefore the following
4037 * suffices without querying the (exact_)filename of this BDS. */
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02004038 if (bs->file->bs->full_open_options) {
Max Reitz91af7012014-07-18 20:24:56 +02004039 qdict_put_obj(opts, "driver",
4040 QOBJECT(qstring_from_str(drv->format_name)));
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02004041 QINCREF(bs->file->bs->full_open_options);
4042 qdict_put_obj(opts, "file",
4043 QOBJECT(bs->file->bs->full_open_options));
Max Reitz91af7012014-07-18 20:24:56 +02004044
4045 bs->full_open_options = opts;
4046 } else {
4047 QDECREF(opts);
4048 }
4049 } else if (!bs->full_open_options && qdict_size(bs->options)) {
4050 /* There is no underlying file BDS (at least referenced by BDS.file),
4051 * so the full options QDict should be equal to the options given
4052 * specifically for this block device when it was opened (plus the
4053 * driver specification).
4054 * Because those options don't change, there is no need to update
4055 * full_open_options when it's already set. */
4056
4057 opts = qdict_new();
4058 append_open_options(opts, bs);
4059 qdict_put_obj(opts, "driver",
4060 QOBJECT(qstring_from_str(drv->format_name)));
4061
4062 if (bs->exact_filename[0]) {
4063 /* This may not work for all block protocol drivers (some may
4064 * require this filename to be parsed), but we have to find some
4065 * default solution here, so just include it. If some block driver
4066 * does not support pure options without any filename at all or
4067 * needs some special format of the options QDict, it needs to
4068 * implement the driver-specific bdrv_refresh_filename() function.
4069 */
4070 qdict_put_obj(opts, "filename",
4071 QOBJECT(qstring_from_str(bs->exact_filename)));
4072 }
4073
4074 bs->full_open_options = opts;
4075 }
4076
4077 if (bs->exact_filename[0]) {
4078 pstrcpy(bs->filename, sizeof(bs->filename), bs->exact_filename);
4079 } else if (bs->full_open_options) {
4080 QString *json = qobject_to_json(QOBJECT(bs->full_open_options));
4081 snprintf(bs->filename, sizeof(bs->filename), "json:%s",
4082 qstring_get_str(json));
4083 QDECREF(json);
4084 }
4085}
Wen Congyange06018a2016-05-10 15:36:37 +08004086
4087/*
4088 * Hot add/remove a BDS's child. So the user can take a child offline when
4089 * it is broken and take a new child online
4090 */
4091void bdrv_add_child(BlockDriverState *parent_bs, BlockDriverState *child_bs,
4092 Error **errp)
4093{
4094
4095 if (!parent_bs->drv || !parent_bs->drv->bdrv_add_child) {
4096 error_setg(errp, "The node %s does not support adding a child",
4097 bdrv_get_device_or_node_name(parent_bs));
4098 return;
4099 }
4100
4101 if (!QLIST_EMPTY(&child_bs->parents)) {
4102 error_setg(errp, "The node %s already has a parent",
4103 child_bs->node_name);
4104 return;
4105 }
4106
4107 parent_bs->drv->bdrv_add_child(parent_bs, child_bs, errp);
4108}
4109
4110void bdrv_del_child(BlockDriverState *parent_bs, BdrvChild *child, Error **errp)
4111{
4112 BdrvChild *tmp;
4113
4114 if (!parent_bs->drv || !parent_bs->drv->bdrv_del_child) {
4115 error_setg(errp, "The node %s does not support removing a child",
4116 bdrv_get_device_or_node_name(parent_bs));
4117 return;
4118 }
4119
4120 QLIST_FOREACH(tmp, &parent_bs->children, next) {
4121 if (tmp == child) {
4122 break;
4123 }
4124 }
4125
4126 if (!tmp) {
4127 error_setg(errp, "The node %s does not have a child named %s",
4128 bdrv_get_device_or_node_name(parent_bs),
4129 bdrv_get_device_or_node_name(child->bs));
4130 return;
4131 }
4132
4133 parent_bs->drv->bdrv_del_child(parent_bs, child, errp);
4134}