blob: 2c87186ecfcc4c47a78251ed5fea533c61f97a73 [file] [log] [blame]
bellardfc01f7e2003-06-30 10:03:06 +00001/*
2 * QEMU System Emulator block driver
ths5fafdf22007-09-16 21:08:06 +00003 *
bellardfc01f7e2003-06-30 10:03:06 +00004 * Copyright (c) 2003 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellardfc01f7e2003-06-30 10:03:06 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
Peter Maydelld38ea872016-01-29 17:50:05 +000024#include "qemu/osdep.h"
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +010025#include "trace.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010026#include "block/block_int.h"
27#include "block/blockjob.h"
Kevin Wolfcd7fca92016-07-06 11:22:39 +020028#include "block/nbd.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010029#include "qemu/error-report.h"
Marc Mari88d88792016-08-12 09:27:03 -040030#include "module_block.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010031#include "qemu/module.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010032#include "qapi/qmp/qerror.h"
Kevin Wolf91a097e2015-05-08 17:49:53 +020033#include "qapi/qmp/qbool.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010034#include "qapi/qmp/qjson.h"
Markus Armbrusterbfb197e2014-10-07 13:59:11 +020035#include "sysemu/block-backend.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010036#include "sysemu/sysemu.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010037#include "qemu/notify.h"
Daniel P. Berrange10817bf2015-09-01 14:48:02 +010038#include "qemu/coroutine.h"
Benoît Canetc13163f2014-01-23 21:31:34 +010039#include "block/qapi.h"
Luiz Capitulinob2023812011-09-21 17:16:47 -030040#include "qmp-commands.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010041#include "qemu/timer.h"
Wenchao Xiaa5ee7bd2014-06-18 08:43:44 +020042#include "qapi-event.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020043#include "qemu/cutils.h"
44#include "qemu/id.h"
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 {
Kevin Wolf2af5ef72013-04-09 13:19:18 +02001106 if (file == NULL) {
Max Reitz34b5d2c2013-09-05 14:45:29 +02001107 error_setg(errp, "Can't use '%s' as a block driver for the "
1108 "protocol level", drv->format_name);
Kevin Wolf2af5ef72013-04-09 13:19:18 +02001109 ret = -EINVAL;
1110 goto free_and_fail;
1111 }
Kevin Wolff500a6d2012-11-12 17:35:27 +01001112 bs->file = file;
Max Reitz34b5d2c2013-09-05 14:45:29 +02001113 ret = drv->bdrv_open(bs, options, open_flags, &local_err);
Kevin Wolf66f82ce2010-04-14 14:17:38 +02001114 }
1115
Kevin Wolf57915332010-04-14 15:24:50 +02001116 if (ret < 0) {
Markus Armbruster84d18f02014-01-30 15:07:28 +01001117 if (local_err) {
Max Reitz34b5d2c2013-09-05 14:45:29 +02001118 error_propagate(errp, local_err);
Dunrong Huang2fa9aa52013-09-24 18:14:01 +08001119 } else if (bs->filename[0]) {
1120 error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename);
Max Reitz34b5d2c2013-09-05 14:45:29 +02001121 } else {
1122 error_setg_errno(errp, -ret, "Could not open image");
1123 }
Kevin Wolf57915332010-04-14 15:24:50 +02001124 goto free_and_fail;
1125 }
1126
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001127 ret = refresh_total_sectors(bs, bs->total_sectors);
1128 if (ret < 0) {
Max Reitz34b5d2c2013-09-05 14:45:29 +02001129 error_setg_errno(errp, -ret, "Could not refresh total sector count");
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001130 goto free_and_fail;
Kevin Wolf57915332010-04-14 15:24:50 +02001131 }
Stefan Hajnoczi51762282010-04-19 16:56:41 +01001132
Kevin Wolf3baca892014-07-16 17:48:16 +02001133 bdrv_refresh_limits(bs, &local_err);
1134 if (local_err) {
1135 error_propagate(errp, local_err);
1136 ret = -EINVAL;
1137 goto free_and_fail;
1138 }
1139
Paolo Bonzinic25f53b2011-11-29 12:42:20 +01001140 assert(bdrv_opt_mem_align(bs) != 0);
Denis V. Lunev4196d2f2015-05-12 17:30:55 +03001141 assert(bdrv_min_mem_align(bs) != 0);
Eric Blakea5b8dd22016-06-23 16:37:24 -06001142 assert(is_power_of_2(bs->bl.request_alignment));
Kevin Wolf18edf282015-04-07 17:12:56 +02001143
1144 qemu_opts_del(opts);
Kevin Wolf57915332010-04-14 15:24:50 +02001145 return 0;
1146
1147free_and_fail:
Kevin Wolff500a6d2012-11-12 17:35:27 +01001148 bs->file = NULL;
Anthony Liguori7267c092011-08-20 22:09:37 -05001149 g_free(bs->opaque);
Kevin Wolf57915332010-04-14 15:24:50 +02001150 bs->opaque = NULL;
1151 bs->drv = NULL;
Kevin Wolf18edf282015-04-07 17:12:56 +02001152fail_opts:
1153 qemu_opts_del(opts);
Kevin Wolf57915332010-04-14 15:24:50 +02001154 return ret;
1155}
1156
Kevin Wolf5e5c4f62014-05-26 11:45:08 +02001157static QDict *parse_json_filename(const char *filename, Error **errp)
1158{
1159 QObject *options_obj;
1160 QDict *options;
1161 int ret;
1162
1163 ret = strstart(filename, "json:", &filename);
1164 assert(ret);
1165
1166 options_obj = qobject_from_json(filename);
1167 if (!options_obj) {
1168 error_setg(errp, "Could not parse the JSON options");
1169 return NULL;
1170 }
1171
1172 if (qobject_type(options_obj) != QTYPE_QDICT) {
1173 qobject_decref(options_obj);
1174 error_setg(errp, "Invalid JSON object given");
1175 return NULL;
1176 }
1177
1178 options = qobject_to_qdict(options_obj);
1179 qdict_flatten(options);
1180
1181 return options;
1182}
1183
Kevin Wolfde3b53f2015-10-29 15:24:41 +01001184static void parse_json_protocol(QDict *options, const char **pfilename,
1185 Error **errp)
1186{
1187 QDict *json_options;
1188 Error *local_err = NULL;
1189
1190 /* Parse json: pseudo-protocol */
1191 if (!*pfilename || !g_str_has_prefix(*pfilename, "json:")) {
1192 return;
1193 }
1194
1195 json_options = parse_json_filename(*pfilename, &local_err);
1196 if (local_err) {
1197 error_propagate(errp, local_err);
1198 return;
1199 }
1200
1201 /* Options given in the filename have lower priority than options
1202 * specified directly */
1203 qdict_join(options, json_options, false);
1204 QDECREF(json_options);
1205 *pfilename = NULL;
1206}
1207
Kevin Wolf57915332010-04-14 15:24:50 +02001208/*
Kevin Wolff54120f2014-05-26 11:09:59 +02001209 * Fills in default options for opening images and converts the legacy
1210 * filename/flags pair to option QDict entries.
Max Reitz53a29512015-03-19 14:53:16 -04001211 * The BDRV_O_PROTOCOL flag in *flags will be set or cleared accordingly if a
1212 * block driver has been specified explicitly.
Kevin Wolff54120f2014-05-26 11:09:59 +02001213 */
Kevin Wolfde3b53f2015-10-29 15:24:41 +01001214static int bdrv_fill_options(QDict **options, const char *filename,
Max Reitz053e1572015-08-26 19:47:51 +02001215 int *flags, Error **errp)
Kevin Wolff54120f2014-05-26 11:09:59 +02001216{
1217 const char *drvname;
Max Reitz53a29512015-03-19 14:53:16 -04001218 bool protocol = *flags & BDRV_O_PROTOCOL;
Kevin Wolff54120f2014-05-26 11:09:59 +02001219 bool parse_filename = false;
Max Reitz053e1572015-08-26 19:47:51 +02001220 BlockDriver *drv = NULL;
Kevin Wolff54120f2014-05-26 11:09:59 +02001221 Error *local_err = NULL;
Kevin Wolff54120f2014-05-26 11:09:59 +02001222
Max Reitz53a29512015-03-19 14:53:16 -04001223 drvname = qdict_get_try_str(*options, "driver");
Max Reitz053e1572015-08-26 19:47:51 +02001224 if (drvname) {
1225 drv = bdrv_find_format(drvname);
1226 if (!drv) {
1227 error_setg(errp, "Unknown driver '%s'", drvname);
1228 return -ENOENT;
1229 }
1230 /* If the user has explicitly specified the driver, this choice should
1231 * override the BDRV_O_PROTOCOL flag */
1232 protocol = drv->bdrv_file_open;
Max Reitz53a29512015-03-19 14:53:16 -04001233 }
1234
1235 if (protocol) {
1236 *flags |= BDRV_O_PROTOCOL;
1237 } else {
1238 *flags &= ~BDRV_O_PROTOCOL;
1239 }
1240
Kevin Wolf91a097e2015-05-08 17:49:53 +02001241 /* Translate cache options from flags into options */
1242 update_options_from_flags(*options, *flags);
1243
Kevin Wolff54120f2014-05-26 11:09:59 +02001244 /* Fetch the file name from the options QDict if necessary */
Kevin Wolf17b005f2014-05-27 10:50:29 +02001245 if (protocol && filename) {
Kevin Wolff54120f2014-05-26 11:09:59 +02001246 if (!qdict_haskey(*options, "filename")) {
1247 qdict_put(*options, "filename", qstring_from_str(filename));
1248 parse_filename = true;
1249 } else {
1250 error_setg(errp, "Can't specify 'file' and 'filename' options at "
1251 "the same time");
1252 return -EINVAL;
1253 }
1254 }
1255
1256 /* Find the right block driver */
1257 filename = qdict_get_try_str(*options, "filename");
Kevin Wolff54120f2014-05-26 11:09:59 +02001258
Max Reitz053e1572015-08-26 19:47:51 +02001259 if (!drvname && protocol) {
1260 if (filename) {
1261 drv = bdrv_find_protocol(filename, parse_filename, errp);
1262 if (!drv) {
Kevin Wolff54120f2014-05-26 11:09:59 +02001263 return -EINVAL;
1264 }
Max Reitz053e1572015-08-26 19:47:51 +02001265
1266 drvname = drv->format_name;
1267 qdict_put(*options, "driver", qstring_from_str(drvname));
1268 } else {
1269 error_setg(errp, "Must specify either driver or file");
1270 return -EINVAL;
Kevin Wolff54120f2014-05-26 11:09:59 +02001271 }
1272 }
1273
Kevin Wolf17b005f2014-05-27 10:50:29 +02001274 assert(drv || !protocol);
Kevin Wolff54120f2014-05-26 11:09:59 +02001275
1276 /* Driver-specific filename parsing */
Kevin Wolf17b005f2014-05-27 10:50:29 +02001277 if (drv && drv->bdrv_parse_filename && parse_filename) {
Kevin Wolff54120f2014-05-26 11:09:59 +02001278 drv->bdrv_parse_filename(filename, *options, &local_err);
1279 if (local_err) {
1280 error_propagate(errp, local_err);
1281 return -EINVAL;
1282 }
1283
1284 if (!drv->bdrv_needs_filename) {
1285 qdict_del(*options, "filename");
1286 }
1287 }
1288
1289 return 0;
1290}
1291
Kevin Wolfe9740bc2016-05-23 15:52:26 +02001292static void bdrv_replace_child(BdrvChild *child, BlockDriverState *new_bs)
1293{
1294 BlockDriverState *old_bs = child->bs;
1295
1296 if (old_bs) {
Kevin Wolf36fe1332016-05-17 14:51:55 +02001297 if (old_bs->quiesce_counter && child->role->drained_end) {
1298 child->role->drained_end(child);
1299 }
Kevin Wolfe9740bc2016-05-23 15:52:26 +02001300 QLIST_REMOVE(child, next_parent);
1301 }
Kevin Wolfe9740bc2016-05-23 15:52:26 +02001302
1303 child->bs = new_bs;
Kevin Wolf36fe1332016-05-17 14:51:55 +02001304
1305 if (new_bs) {
1306 QLIST_INSERT_HEAD(&new_bs->parents, child, next_parent);
1307 if (new_bs->quiesce_counter && child->role->drained_begin) {
1308 child->role->drained_begin(child);
1309 }
1310 }
Kevin Wolfe9740bc2016-05-23 15:52:26 +02001311}
1312
Kevin Wolff21d96d2016-03-08 13:47:46 +01001313BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs,
1314 const char *child_name,
Kevin Wolf36fe1332016-05-17 14:51:55 +02001315 const BdrvChildRole *child_role,
1316 void *opaque)
Kevin Wolfdf581792015-06-15 11:53:47 +02001317{
1318 BdrvChild *child = g_new(BdrvChild, 1);
1319 *child = (BdrvChild) {
Kevin Wolfe9740bc2016-05-23 15:52:26 +02001320 .bs = NULL,
Kevin Wolf260fecf2015-04-27 13:46:22 +02001321 .name = g_strdup(child_name),
Kevin Wolfdf581792015-06-15 11:53:47 +02001322 .role = child_role,
Kevin Wolf36fe1332016-05-17 14:51:55 +02001323 .opaque = opaque,
Kevin Wolfdf581792015-06-15 11:53:47 +02001324 };
1325
Kevin Wolfe9740bc2016-05-23 15:52:26 +02001326 bdrv_replace_child(child, child_bs);
Kevin Wolfb4b059f2015-06-15 13:24:19 +02001327
1328 return child;
Kevin Wolfdf581792015-06-15 11:53:47 +02001329}
1330
Wen Congyang98292c62016-05-10 15:36:38 +08001331BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs,
1332 BlockDriverState *child_bs,
1333 const char *child_name,
1334 const BdrvChildRole *child_role)
Kevin Wolff21d96d2016-03-08 13:47:46 +01001335{
Kevin Wolf36fe1332016-05-17 14:51:55 +02001336 BdrvChild *child = bdrv_root_attach_child(child_bs, child_name, child_role,
Kevin Wolf20018e12016-05-23 18:46:59 +02001337 parent_bs);
Kevin Wolff21d96d2016-03-08 13:47:46 +01001338 QLIST_INSERT_HEAD(&parent_bs->children, child, next);
1339 return child;
1340}
1341
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001342static void bdrv_detach_child(BdrvChild *child)
Kevin Wolf33a60402015-06-15 13:51:04 +02001343{
Kevin Wolff21d96d2016-03-08 13:47:46 +01001344 if (child->next.le_prev) {
1345 QLIST_REMOVE(child, next);
1346 child->next.le_prev = NULL;
1347 }
Kevin Wolfe9740bc2016-05-23 15:52:26 +02001348
1349 bdrv_replace_child(child, NULL);
1350
Kevin Wolf260fecf2015-04-27 13:46:22 +02001351 g_free(child->name);
Kevin Wolf33a60402015-06-15 13:51:04 +02001352 g_free(child);
1353}
1354
Kevin Wolff21d96d2016-03-08 13:47:46 +01001355void bdrv_root_unref_child(BdrvChild *child)
Kevin Wolf33a60402015-06-15 13:51:04 +02001356{
Kevin Wolf779020c2015-10-13 14:09:44 +02001357 BlockDriverState *child_bs;
1358
Kevin Wolff21d96d2016-03-08 13:47:46 +01001359 child_bs = child->bs;
1360 bdrv_detach_child(child);
1361 bdrv_unref(child_bs);
1362}
1363
1364void bdrv_unref_child(BlockDriverState *parent, BdrvChild *child)
1365{
Kevin Wolf779020c2015-10-13 14:09:44 +02001366 if (child == NULL) {
1367 return;
1368 }
Kevin Wolf33a60402015-06-15 13:51:04 +02001369
1370 if (child->bs->inherits_from == parent) {
1371 child->bs->inherits_from = NULL;
1372 }
1373
Kevin Wolff21d96d2016-03-08 13:47:46 +01001374 bdrv_root_unref_child(child);
Kevin Wolf33a60402015-06-15 13:51:04 +02001375}
1376
Kevin Wolf5c8cab42016-02-24 15:13:35 +01001377
1378static void bdrv_parent_cb_change_media(BlockDriverState *bs, bool load)
1379{
1380 BdrvChild *c;
1381 QLIST_FOREACH(c, &bs->parents, next_parent) {
1382 if (c->role->change_media) {
1383 c->role->change_media(c, load);
1384 }
1385 }
1386}
1387
1388static void bdrv_parent_cb_resize(BlockDriverState *bs)
1389{
1390 BdrvChild *c;
1391 QLIST_FOREACH(c, &bs->parents, next_parent) {
1392 if (c->role->resize) {
1393 c->role->resize(c);
1394 }
1395 }
1396}
1397
Kevin Wolf5db15a52015-09-14 15:33:33 +02001398/*
1399 * Sets the backing file link of a BDS. A new reference is created; callers
1400 * which don't need their own reference any more must call bdrv_unref().
1401 */
Fam Zheng8d24cce2014-05-23 21:29:45 +08001402void bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd)
1403{
Kevin Wolf5db15a52015-09-14 15:33:33 +02001404 if (backing_hd) {
1405 bdrv_ref(backing_hd);
1406 }
Fam Zheng8d24cce2014-05-23 21:29:45 +08001407
Kevin Wolf760e0062015-06-17 14:55:21 +02001408 if (bs->backing) {
Fam Zheng826b6ca2014-05-23 21:29:47 +08001409 assert(bs->backing_blocker);
Kevin Wolf760e0062015-06-17 14:55:21 +02001410 bdrv_op_unblock_all(bs->backing->bs, bs->backing_blocker);
Kevin Wolf5db15a52015-09-14 15:33:33 +02001411 bdrv_unref_child(bs, bs->backing);
Fam Zheng826b6ca2014-05-23 21:29:47 +08001412 } else if (backing_hd) {
1413 error_setg(&bs->backing_blocker,
Alberto Garcia81e5f782015-04-08 12:29:19 +03001414 "node is used as backing hd of '%s'",
1415 bdrv_get_device_or_node_name(bs));
Fam Zheng826b6ca2014-05-23 21:29:47 +08001416 }
1417
Fam Zheng8d24cce2014-05-23 21:29:45 +08001418 if (!backing_hd) {
Fam Zheng826b6ca2014-05-23 21:29:47 +08001419 error_free(bs->backing_blocker);
1420 bs->backing_blocker = NULL;
Kevin Wolf760e0062015-06-17 14:55:21 +02001421 bs->backing = NULL;
Fam Zheng8d24cce2014-05-23 21:29:45 +08001422 goto out;
1423 }
Kevin Wolf260fecf2015-04-27 13:46:22 +02001424 bs->backing = bdrv_attach_child(bs, backing_hd, "backing", &child_backing);
Fam Zheng8d24cce2014-05-23 21:29:45 +08001425 bs->open_flags &= ~BDRV_O_NO_BACKING;
1426 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_hd->filename);
1427 pstrcpy(bs->backing_format, sizeof(bs->backing_format),
1428 backing_hd->drv ? backing_hd->drv->format_name : "");
Fam Zheng826b6ca2014-05-23 21:29:47 +08001429
Kevin Wolf760e0062015-06-17 14:55:21 +02001430 bdrv_op_block_all(backing_hd, bs->backing_blocker);
Fam Zheng826b6ca2014-05-23 21:29:47 +08001431 /* Otherwise we won't be able to commit due to check in bdrv_commit */
Kevin Wolf760e0062015-06-17 14:55:21 +02001432 bdrv_op_unblock(backing_hd, BLOCK_OP_TYPE_COMMIT_TARGET,
Fam Zheng826b6ca2014-05-23 21:29:47 +08001433 bs->backing_blocker);
Wen Congyange9d64562016-07-27 15:01:42 +08001434 /*
1435 * We do backup in 3 ways:
1436 * 1. drive backup
1437 * The target bs is new opened, and the source is top BDS
1438 * 2. blockdev backup
1439 * Both the source and the target are top BDSes.
1440 * 3. internal backup(used for block replication)
1441 * Both the source and the target are backing file
1442 *
1443 * In case 1 and 2, neither the source nor the target is the backing file.
1444 * In case 3, we will block the top BDS, so there is only one block job
1445 * for the top BDS and its backing chain.
1446 */
1447 bdrv_op_unblock(backing_hd, BLOCK_OP_TYPE_BACKUP_SOURCE,
1448 bs->backing_blocker);
1449 bdrv_op_unblock(backing_hd, BLOCK_OP_TYPE_BACKUP_TARGET,
1450 bs->backing_blocker);
Fam Zheng8d24cce2014-05-23 21:29:45 +08001451out:
Kevin Wolf3baca892014-07-16 17:48:16 +02001452 bdrv_refresh_limits(bs, NULL);
Fam Zheng8d24cce2014-05-23 21:29:45 +08001453}
1454
Kevin Wolf31ca6d02013-03-28 15:29:24 +01001455/*
1456 * Opens the backing file for a BlockDriverState if not yet open
1457 *
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001458 * bdref_key specifies the key for the image's BlockdevRef in the options QDict.
1459 * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict
1460 * itself, all options starting with "${bdref_key}." are considered part of the
1461 * BlockdevRef.
1462 *
1463 * TODO Can this be unified with bdrv_open_image()?
Kevin Wolf31ca6d02013-03-28 15:29:24 +01001464 */
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001465int bdrv_open_backing_file(BlockDriverState *bs, QDict *parent_options,
1466 const char *bdref_key, Error **errp)
Paolo Bonzini9156df12012-10-18 16:49:17 +02001467{
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001468 char *backing_filename = g_malloc0(PATH_MAX);
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001469 char *bdref_key_dot;
1470 const char *reference = NULL;
Kevin Wolf317fc442014-04-25 13:27:34 +02001471 int ret = 0;
Fam Zheng8d24cce2014-05-23 21:29:45 +08001472 BlockDriverState *backing_hd;
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001473 QDict *options;
1474 QDict *tmp_parent_options = NULL;
Max Reitz34b5d2c2013-09-05 14:45:29 +02001475 Error *local_err = NULL;
Paolo Bonzini9156df12012-10-18 16:49:17 +02001476
Kevin Wolf760e0062015-06-17 14:55:21 +02001477 if (bs->backing != NULL) {
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001478 goto free_exit;
Paolo Bonzini9156df12012-10-18 16:49:17 +02001479 }
1480
Kevin Wolf31ca6d02013-03-28 15:29:24 +01001481 /* NULL means an empty set of options */
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001482 if (parent_options == NULL) {
1483 tmp_parent_options = qdict_new();
1484 parent_options = tmp_parent_options;
Kevin Wolf31ca6d02013-03-28 15:29:24 +01001485 }
1486
Paolo Bonzini9156df12012-10-18 16:49:17 +02001487 bs->open_flags &= ~BDRV_O_NO_BACKING;
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001488
1489 bdref_key_dot = g_strdup_printf("%s.", bdref_key);
1490 qdict_extract_subqdict(parent_options, &options, bdref_key_dot);
1491 g_free(bdref_key_dot);
1492
1493 reference = qdict_get_try_str(parent_options, bdref_key);
1494 if (reference || qdict_haskey(options, "file.filename")) {
Kevin Wolf1cb6f502013-04-12 20:27:07 +02001495 backing_filename[0] = '\0';
1496 } else if (bs->backing_file[0] == '\0' && qdict_size(options) == 0) {
Kevin Wolf31ca6d02013-03-28 15:29:24 +01001497 QDECREF(options);
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001498 goto free_exit;
Fam Zhengdbecebd2013-09-22 20:05:06 +08001499 } else {
Max Reitz9f074292014-11-26 17:20:26 +01001500 bdrv_get_full_backing_filename(bs, backing_filename, PATH_MAX,
1501 &local_err);
1502 if (local_err) {
1503 ret = -EINVAL;
1504 error_propagate(errp, local_err);
1505 QDECREF(options);
1506 goto free_exit;
1507 }
Paolo Bonzini9156df12012-10-18 16:49:17 +02001508 }
1509
Kevin Wolf8ee79e72014-06-04 15:09:35 +02001510 if (!bs->drv || !bs->drv->supports_backing) {
1511 ret = -EINVAL;
1512 error_setg(errp, "Driver doesn't support backing files");
1513 QDECREF(options);
1514 goto free_exit;
1515 }
1516
Kevin Wolfc5f6e492014-11-25 18:12:42 +01001517 if (bs->backing_format[0] != '\0' && !qdict_haskey(options, "driver")) {
1518 qdict_put(options, "driver", qstring_from_str(bs->backing_format));
Paolo Bonzini9156df12012-10-18 16:49:17 +02001519 }
1520
Max Reitz5b363932016-05-17 16:41:31 +02001521 backing_hd = bdrv_open_inherit(*backing_filename ? backing_filename : NULL,
1522 reference, options, 0, bs, &child_backing,
1523 errp);
1524 if (!backing_hd) {
Paolo Bonzini9156df12012-10-18 16:49:17 +02001525 bs->open_flags |= BDRV_O_NO_BACKING;
Markus Armbrustere43bfd92015-12-18 16:35:15 +01001526 error_prepend(errp, "Could not open backing file: ");
Max Reitz5b363932016-05-17 16:41:31 +02001527 ret = -EINVAL;
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001528 goto free_exit;
Paolo Bonzini9156df12012-10-18 16:49:17 +02001529 }
Kevin Wolfdf581792015-06-15 11:53:47 +02001530
Kevin Wolf5db15a52015-09-14 15:33:33 +02001531 /* Hook up the backing file link; drop our reference, bs owns the
1532 * backing_hd reference now */
Fam Zheng8d24cce2014-05-23 21:29:45 +08001533 bdrv_set_backing_hd(bs, backing_hd);
Kevin Wolf5db15a52015-09-14 15:33:33 +02001534 bdrv_unref(backing_hd);
Peter Feinerd80ac652014-01-08 19:43:25 +00001535
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001536 qdict_del(parent_options, bdref_key);
1537
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001538free_exit:
1539 g_free(backing_filename);
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001540 QDECREF(tmp_parent_options);
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001541 return ret;
Paolo Bonzini9156df12012-10-18 16:49:17 +02001542}
1543
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001544/*
Max Reitzda557aa2013-12-20 19:28:11 +01001545 * Opens a disk image whose options are given as BlockdevRef in another block
1546 * device's options.
1547 *
Max Reitzda557aa2013-12-20 19:28:11 +01001548 * If allow_none is true, no image will be opened if filename is false and no
Kevin Wolfb4b059f2015-06-15 13:24:19 +02001549 * BlockdevRef is given. NULL will be returned, but errp remains unset.
Max Reitzda557aa2013-12-20 19:28:11 +01001550 *
1551 * bdrev_key specifies the key for the image's BlockdevRef in the options QDict.
1552 * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict
1553 * itself, all options starting with "${bdref_key}." are considered part of the
1554 * BlockdevRef.
1555 *
1556 * The BlockdevRef will be removed from the options QDict.
1557 */
Kevin Wolfb4b059f2015-06-15 13:24:19 +02001558BdrvChild *bdrv_open_child(const char *filename,
1559 QDict *options, const char *bdref_key,
1560 BlockDriverState* parent,
1561 const BdrvChildRole *child_role,
1562 bool allow_none, Error **errp)
Max Reitzda557aa2013-12-20 19:28:11 +01001563{
Kevin Wolfb4b059f2015-06-15 13:24:19 +02001564 BdrvChild *c = NULL;
1565 BlockDriverState *bs;
Max Reitzda557aa2013-12-20 19:28:11 +01001566 QDict *image_options;
Max Reitzda557aa2013-12-20 19:28:11 +01001567 char *bdref_key_dot;
1568 const char *reference;
1569
Kevin Wolfdf581792015-06-15 11:53:47 +02001570 assert(child_role != NULL);
Max Reitzf67503e2014-02-18 18:33:05 +01001571
Max Reitzda557aa2013-12-20 19:28:11 +01001572 bdref_key_dot = g_strdup_printf("%s.", bdref_key);
1573 qdict_extract_subqdict(options, &image_options, bdref_key_dot);
1574 g_free(bdref_key_dot);
1575
1576 reference = qdict_get_try_str(options, bdref_key);
1577 if (!filename && !reference && !qdict_size(image_options)) {
Kevin Wolfb4b059f2015-06-15 13:24:19 +02001578 if (!allow_none) {
Max Reitzda557aa2013-12-20 19:28:11 +01001579 error_setg(errp, "A block device must be specified for \"%s\"",
1580 bdref_key);
Max Reitzda557aa2013-12-20 19:28:11 +01001581 }
Markus Armbrusterb20e61e2014-05-28 11:16:57 +02001582 QDECREF(image_options);
Max Reitzda557aa2013-12-20 19:28:11 +01001583 goto done;
1584 }
1585
Max Reitz5b363932016-05-17 16:41:31 +02001586 bs = bdrv_open_inherit(filename, reference, image_options, 0,
1587 parent, child_role, errp);
1588 if (!bs) {
Kevin Wolfdf581792015-06-15 11:53:47 +02001589 goto done;
1590 }
1591
Kevin Wolf260fecf2015-04-27 13:46:22 +02001592 c = bdrv_attach_child(parent, bs, bdref_key, child_role);
Max Reitzda557aa2013-12-20 19:28:11 +01001593
1594done:
1595 qdict_del(options, bdref_key);
Kevin Wolfb4b059f2015-06-15 13:24:19 +02001596 return c;
1597}
1598
Max Reitz66836182016-05-17 16:41:27 +02001599static BlockDriverState *bdrv_append_temp_snapshot(BlockDriverState *bs,
1600 int flags,
1601 QDict *snapshot_options,
1602 Error **errp)
Kevin Wolfb9988752014-04-03 12:09:34 +02001603{
1604 /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001605 char *tmp_filename = g_malloc0(PATH_MAX + 1);
Kevin Wolfb9988752014-04-03 12:09:34 +02001606 int64_t total_size;
Chunyan Liu83d05212014-06-05 17:20:51 +08001607 QemuOpts *opts = NULL;
Kevin Wolfb9988752014-04-03 12:09:34 +02001608 BlockDriverState *bs_snapshot;
Kevin Wolfb9988752014-04-03 12:09:34 +02001609 int ret;
1610
1611 /* if snapshot, we create a temporary backing file and open it
1612 instead of opening 'filename' directly */
1613
1614 /* Get the required size from the image */
Kevin Wolff1877432014-04-04 17:07:19 +02001615 total_size = bdrv_getlength(bs);
1616 if (total_size < 0) {
1617 error_setg_errno(errp, -total_size, "Could not get image size");
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001618 goto out;
Kevin Wolff1877432014-04-04 17:07:19 +02001619 }
Kevin Wolfb9988752014-04-03 12:09:34 +02001620
1621 /* Create the temporary image */
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001622 ret = get_tmp_filename(tmp_filename, PATH_MAX + 1);
Kevin Wolfb9988752014-04-03 12:09:34 +02001623 if (ret < 0) {
1624 error_setg_errno(errp, -ret, "Could not get temporary filename");
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001625 goto out;
Kevin Wolfb9988752014-04-03 12:09:34 +02001626 }
1627
Max Reitzef810432014-12-02 18:32:42 +01001628 opts = qemu_opts_create(bdrv_qcow2.create_opts, NULL, 0,
Chunyan Liuc282e1f2014-06-05 17:21:11 +08001629 &error_abort);
Markus Armbruster39101f22015-02-12 16:46:36 +01001630 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_size, &error_abort);
Markus Armbrustere43bfd92015-12-18 16:35:15 +01001631 ret = bdrv_create(&bdrv_qcow2, tmp_filename, opts, errp);
Chunyan Liu83d05212014-06-05 17:20:51 +08001632 qemu_opts_del(opts);
Kevin Wolfb9988752014-04-03 12:09:34 +02001633 if (ret < 0) {
Markus Armbrustere43bfd92015-12-18 16:35:15 +01001634 error_prepend(errp, "Could not create temporary overlay '%s': ",
1635 tmp_filename);
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001636 goto out;
Kevin Wolfb9988752014-04-03 12:09:34 +02001637 }
1638
Kevin Wolf73176be2016-03-07 13:02:15 +01001639 /* Prepare options QDict for the temporary file */
Kevin Wolfb9988752014-04-03 12:09:34 +02001640 qdict_put(snapshot_options, "file.driver",
1641 qstring_from_str("file"));
1642 qdict_put(snapshot_options, "file.filename",
1643 qstring_from_str(tmp_filename));
Max Reitze6641712015-08-26 19:47:48 +02001644 qdict_put(snapshot_options, "driver",
1645 qstring_from_str("qcow2"));
Kevin Wolfb9988752014-04-03 12:09:34 +02001646
Max Reitz5b363932016-05-17 16:41:31 +02001647 bs_snapshot = bdrv_open(NULL, NULL, snapshot_options, flags, errp);
Kevin Wolf73176be2016-03-07 13:02:15 +01001648 snapshot_options = NULL;
Max Reitz5b363932016-05-17 16:41:31 +02001649 if (!bs_snapshot) {
1650 ret = -EINVAL;
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001651 goto out;
Kevin Wolfb9988752014-04-03 12:09:34 +02001652 }
1653
Max Reitz66836182016-05-17 16:41:27 +02001654 /* bdrv_append() consumes a strong reference to bs_snapshot (i.e. it will
1655 * call bdrv_unref() on it), so in order to be able to return one, we have
1656 * to increase bs_snapshot's refcount here */
1657 bdrv_ref(bs_snapshot);
Kevin Wolfb9988752014-04-03 12:09:34 +02001658 bdrv_append(bs_snapshot, bs);
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001659
Max Reitz66836182016-05-17 16:41:27 +02001660 g_free(tmp_filename);
1661 return bs_snapshot;
1662
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001663out:
Kevin Wolf73176be2016-03-07 13:02:15 +01001664 QDECREF(snapshot_options);
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001665 g_free(tmp_filename);
Max Reitz66836182016-05-17 16:41:27 +02001666 return NULL;
Kevin Wolfb9988752014-04-03 12:09:34 +02001667}
1668
Max Reitzda557aa2013-12-20 19:28:11 +01001669/*
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001670 * Opens a disk image (raw, qcow2, vmdk, ...)
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001671 *
1672 * options is a QDict of options to pass to the block drivers, or NULL for an
1673 * empty set of options. The reference to the QDict belongs to the block layer
1674 * after the call (even on failure), so if the caller intends to reuse the
1675 * dictionary, it needs to use QINCREF() before calling bdrv_open.
Max Reitzf67503e2014-02-18 18:33:05 +01001676 *
1677 * If *pbs is NULL, a new BDS will be created with a pointer to it stored there.
1678 * If it is not NULL, the referenced BDS will be reused.
Max Reitzddf56362014-02-18 18:33:06 +01001679 *
1680 * The reference parameter may be used to specify an existing block device which
1681 * should be opened. If specified, neither options nor a filename may be given,
1682 * nor can an existing BDS be reused (that is, *pbs has to be NULL).
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001683 */
Max Reitz5b363932016-05-17 16:41:31 +02001684static BlockDriverState *bdrv_open_inherit(const char *filename,
1685 const char *reference,
1686 QDict *options, int flags,
1687 BlockDriverState *parent,
1688 const BdrvChildRole *child_role,
1689 Error **errp)
bellardea2384d2004-08-01 21:59:26 +00001690{
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001691 int ret;
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02001692 BdrvChild *file = NULL;
1693 BlockDriverState *bs;
Max Reitzce343772015-08-26 19:47:50 +02001694 BlockDriver *drv = NULL;
Kevin Wolf74fe54f2013-07-09 11:09:02 +02001695 const char *drvname;
Alberto Garcia3e8c2e52015-10-26 14:27:15 +02001696 const char *backing;
Max Reitz34b5d2c2013-09-05 14:45:29 +02001697 Error *local_err = NULL;
Kevin Wolf73176be2016-03-07 13:02:15 +01001698 QDict *snapshot_options = NULL;
Kevin Wolfb1e6fc02014-05-06 12:11:42 +02001699 int snapshot_flags = 0;
bellard712e7872005-04-28 21:09:32 +00001700
Kevin Wolff3930ed2015-04-08 13:43:47 +02001701 assert(!child_role || !flags);
1702 assert(!child_role == !parent);
Max Reitzf67503e2014-02-18 18:33:05 +01001703
Max Reitzddf56362014-02-18 18:33:06 +01001704 if (reference) {
1705 bool options_non_empty = options ? qdict_size(options) : false;
1706 QDECREF(options);
1707
Max Reitzddf56362014-02-18 18:33:06 +01001708 if (filename || options_non_empty) {
1709 error_setg(errp, "Cannot reference an existing block device with "
1710 "additional options or a new filename");
Max Reitz5b363932016-05-17 16:41:31 +02001711 return NULL;
Max Reitzddf56362014-02-18 18:33:06 +01001712 }
1713
1714 bs = bdrv_lookup_bs(reference, reference, errp);
1715 if (!bs) {
Max Reitz5b363932016-05-17 16:41:31 +02001716 return NULL;
Max Reitzddf56362014-02-18 18:33:06 +01001717 }
Kevin Wolf76b22322016-04-04 17:11:13 +02001718
Max Reitzddf56362014-02-18 18:33:06 +01001719 bdrv_ref(bs);
Max Reitz5b363932016-05-17 16:41:31 +02001720 return bs;
Max Reitzddf56362014-02-18 18:33:06 +01001721 }
1722
Max Reitz5b363932016-05-17 16:41:31 +02001723 bs = bdrv_new();
Max Reitzf67503e2014-02-18 18:33:05 +01001724
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001725 /* NULL means an empty set of options */
1726 if (options == NULL) {
1727 options = qdict_new();
1728 }
1729
Kevin Wolf145f5982015-05-08 16:15:03 +02001730 /* json: syntax counts as explicit options, as if in the QDict */
Kevin Wolfde3b53f2015-10-29 15:24:41 +01001731 parse_json_protocol(options, &filename, &local_err);
1732 if (local_err) {
Kevin Wolfde3b53f2015-10-29 15:24:41 +01001733 goto fail;
1734 }
1735
Kevin Wolf145f5982015-05-08 16:15:03 +02001736 bs->explicit_options = qdict_clone_shallow(options);
1737
Kevin Wolff3930ed2015-04-08 13:43:47 +02001738 if (child_role) {
Kevin Wolfbddcec32015-04-09 18:47:50 +02001739 bs->inherits_from = parent;
Kevin Wolf8e2160e2015-04-29 17:29:39 +02001740 child_role->inherit_options(&flags, options,
1741 parent->open_flags, parent->options);
Kevin Wolff3930ed2015-04-08 13:43:47 +02001742 }
1743
Kevin Wolfde3b53f2015-10-29 15:24:41 +01001744 ret = bdrv_fill_options(&options, filename, &flags, &local_err);
Kevin Wolf462f5bc2014-05-26 11:39:55 +02001745 if (local_err) {
1746 goto fail;
1747 }
1748
Alberto Garciaf87a0e22016-09-15 17:53:02 +03001749 /* Set the BDRV_O_RDWR and BDRV_O_ALLOW_RDWR flags.
1750 * FIXME: we're parsing the QDict to avoid having to create a
1751 * QemuOpts just for this, but neither option is optimal. */
1752 if (g_strcmp0(qdict_get_try_str(options, BDRV_OPT_READ_ONLY), "on") &&
1753 !qdict_get_try_bool(options, BDRV_OPT_READ_ONLY, false)) {
1754 flags |= (BDRV_O_RDWR | BDRV_O_ALLOW_RDWR);
1755 } else {
1756 flags &= ~BDRV_O_RDWR;
Alberto Garcia14499ea2016-09-15 17:53:00 +03001757 }
1758
1759 if (flags & BDRV_O_SNAPSHOT) {
1760 snapshot_options = qdict_new();
1761 bdrv_temp_snapshot_options(&snapshot_flags, snapshot_options,
1762 flags, options);
Alberto Garciaf87a0e22016-09-15 17:53:02 +03001763 /* Let bdrv_backing_options() override "read-only" */
1764 qdict_del(options, BDRV_OPT_READ_ONLY);
Alberto Garcia14499ea2016-09-15 17:53:00 +03001765 bdrv_backing_options(&flags, options, flags, options);
1766 }
1767
Kevin Wolf62392eb2015-04-24 16:38:02 +02001768 bs->open_flags = flags;
1769 bs->options = options;
1770 options = qdict_clone_shallow(options);
1771
Kevin Wolf76c591b2014-06-04 14:19:44 +02001772 /* Find the right image format driver */
Kevin Wolf76c591b2014-06-04 14:19:44 +02001773 drvname = qdict_get_try_str(options, "driver");
1774 if (drvname) {
1775 drv = bdrv_find_format(drvname);
Kevin Wolf76c591b2014-06-04 14:19:44 +02001776 if (!drv) {
1777 error_setg(errp, "Unknown driver: '%s'", drvname);
Kevin Wolf76c591b2014-06-04 14:19:44 +02001778 goto fail;
1779 }
1780 }
1781
1782 assert(drvname || !(flags & BDRV_O_PROTOCOL));
Kevin Wolf76c591b2014-06-04 14:19:44 +02001783
Alberto Garcia3e8c2e52015-10-26 14:27:15 +02001784 backing = qdict_get_try_str(options, "backing");
1785 if (backing && *backing == '\0') {
1786 flags |= BDRV_O_NO_BACKING;
1787 qdict_del(options, "backing");
1788 }
1789
Kevin Wolff4788ad2014-06-03 16:44:19 +02001790 /* Open image file without format layer */
1791 if ((flags & BDRV_O_PROTOCOL) == 0) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02001792 file = bdrv_open_child(filename, options, "file", bs,
1793 &child_file, true, &local_err);
Kevin Wolf1fdd6932015-06-15 14:11:51 +02001794 if (local_err) {
Max Reitz5469a2a2014-02-18 18:33:10 +01001795 goto fail;
1796 }
1797 }
1798
Kevin Wolf76c591b2014-06-04 14:19:44 +02001799 /* Image format probing */
Kevin Wolf38f3ef52014-11-20 16:27:12 +01001800 bs->probed = !drv;
Kevin Wolf76c591b2014-06-04 14:19:44 +02001801 if (!drv && file) {
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +02001802 ret = find_image_format(file, filename, &drv, &local_err);
Kevin Wolf17b005f2014-05-27 10:50:29 +02001803 if (ret < 0) {
Kevin Wolf8bfea152014-04-11 19:16:36 +02001804 goto fail;
Max Reitz2a05cbe2013-12-20 19:28:10 +01001805 }
Kevin Wolf62392eb2015-04-24 16:38:02 +02001806 /*
1807 * This option update would logically belong in bdrv_fill_options(),
1808 * but we first need to open bs->file for the probing to work, while
1809 * opening bs->file already requires the (mostly) final set of options
1810 * so that cache mode etc. can be inherited.
1811 *
1812 * Adding the driver later is somewhat ugly, but it's not an option
1813 * that would ever be inherited, so it's correct. We just need to make
1814 * sure to update both bs->options (which has the full effective
1815 * options for bs) and options (which has file.* already removed).
1816 */
1817 qdict_put(bs->options, "driver", qstring_from_str(drv->format_name));
1818 qdict_put(options, "driver", qstring_from_str(drv->format_name));
Kevin Wolf76c591b2014-06-04 14:19:44 +02001819 } else if (!drv) {
Kevin Wolf17b005f2014-05-27 10:50:29 +02001820 error_setg(errp, "Must specify either driver or file");
Kevin Wolf8bfea152014-04-11 19:16:36 +02001821 goto fail;
Kevin Wolff500a6d2012-11-12 17:35:27 +01001822 }
1823
Max Reitz53a29512015-03-19 14:53:16 -04001824 /* BDRV_O_PROTOCOL must be set iff a protocol BDS is about to be created */
1825 assert(!!(flags & BDRV_O_PROTOCOL) == !!drv->bdrv_file_open);
1826 /* file must be NULL if a protocol BDS is about to be created
1827 * (the inverse results in an error message from bdrv_open_common()) */
1828 assert(!(flags & BDRV_O_PROTOCOL) || !file);
1829
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001830 /* Open the image */
Kevin Wolf82dc8b42016-01-11 19:07:50 +01001831 ret = bdrv_open_common(bs, file, options, &local_err);
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001832 if (ret < 0) {
Kevin Wolf8bfea152014-04-11 19:16:36 +02001833 goto fail;
Christoph Hellwig69873072010-01-20 18:13:25 +01001834 }
1835
Max Reitz2a05cbe2013-12-20 19:28:10 +01001836 if (file && (bs->file != file)) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02001837 bdrv_unref_child(bs, file);
Kevin Wolff500a6d2012-11-12 17:35:27 +01001838 file = NULL;
1839 }
1840
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001841 /* If there is a backing file, use it */
Paolo Bonzini9156df12012-10-18 16:49:17 +02001842 if ((flags & BDRV_O_NO_BACKING) == 0) {
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001843 ret = bdrv_open_backing_file(bs, options, "backing", &local_err);
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001844 if (ret < 0) {
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001845 goto close_and_fail;
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001846 }
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001847 }
1848
Max Reitz91af7012014-07-18 20:24:56 +02001849 bdrv_refresh_filename(bs);
1850
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001851 /* Check if any unknown options were used */
Max Reitz5acd9d82014-02-18 18:33:11 +01001852 if (options && (qdict_size(options) != 0)) {
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001853 const QDictEntry *entry = qdict_first(options);
Max Reitz5acd9d82014-02-18 18:33:11 +01001854 if (flags & BDRV_O_PROTOCOL) {
1855 error_setg(errp, "Block protocol '%s' doesn't support the option "
1856 "'%s'", drv->format_name, entry->key);
1857 } else {
Max Reitzd0e46a52016-03-16 19:54:34 +01001858 error_setg(errp,
1859 "Block format '%s' does not support the option '%s'",
1860 drv->format_name, entry->key);
Max Reitz5acd9d82014-02-18 18:33:11 +01001861 }
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001862
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001863 goto close_and_fail;
1864 }
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001865
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001866 if (!bdrv_key_required(bs)) {
Kevin Wolf5c8cab42016-02-24 15:13:35 +01001867 bdrv_parent_cb_change_media(bs, true);
Markus Armbrusterc3adb582014-03-14 09:22:48 +01001868 } else if (!runstate_check(RUN_STATE_PRELAUNCH)
1869 && !runstate_check(RUN_STATE_INMIGRATE)
1870 && !runstate_check(RUN_STATE_PAUSED)) { /* HACK */
1871 error_setg(errp,
1872 "Guest must be stopped for opening of encrypted image");
Markus Armbrusterc3adb582014-03-14 09:22:48 +01001873 goto close_and_fail;
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001874 }
1875
Markus Armbrusterc3adb582014-03-14 09:22:48 +01001876 QDECREF(options);
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02001877
1878 /* For snapshot=on, create a temporary qcow2 overlay. bs points to the
1879 * temporary snapshot afterwards. */
1880 if (snapshot_flags) {
Max Reitz66836182016-05-17 16:41:27 +02001881 BlockDriverState *snapshot_bs;
1882 snapshot_bs = bdrv_append_temp_snapshot(bs, snapshot_flags,
1883 snapshot_options, &local_err);
Kevin Wolf73176be2016-03-07 13:02:15 +01001884 snapshot_options = NULL;
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02001885 if (local_err) {
1886 goto close_and_fail;
1887 }
Max Reitz5b363932016-05-17 16:41:31 +02001888 /* We are not going to return bs but the overlay on top of it
1889 * (snapshot_bs); thus, we have to drop the strong reference to bs
1890 * (which we obtained by calling bdrv_new()). bs will not be deleted,
1891 * though, because the overlay still has a reference to it. */
1892 bdrv_unref(bs);
1893 bs = snapshot_bs;
Max Reitz66836182016-05-17 16:41:27 +02001894 }
1895
Max Reitz5b363932016-05-17 16:41:31 +02001896 return bs;
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001897
Kevin Wolf8bfea152014-04-11 19:16:36 +02001898fail:
Kevin Wolff500a6d2012-11-12 17:35:27 +01001899 if (file != NULL) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02001900 bdrv_unref_child(bs, file);
Kevin Wolff500a6d2012-11-12 17:35:27 +01001901 }
Kevin Wolf73176be2016-03-07 13:02:15 +01001902 QDECREF(snapshot_options);
Kevin Wolf145f5982015-05-08 16:15:03 +02001903 QDECREF(bs->explicit_options);
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001904 QDECREF(bs->options);
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001905 QDECREF(options);
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001906 bs->options = NULL;
Max Reitz5b363932016-05-17 16:41:31 +02001907 bdrv_unref(bs);
Eduardo Habkost621ff942016-06-13 18:57:56 -03001908 error_propagate(errp, local_err);
Max Reitz5b363932016-05-17 16:41:31 +02001909 return NULL;
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001910
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001911close_and_fail:
Max Reitz5b363932016-05-17 16:41:31 +02001912 bdrv_unref(bs);
Kevin Wolf73176be2016-03-07 13:02:15 +01001913 QDECREF(snapshot_options);
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001914 QDECREF(options);
Eduardo Habkost621ff942016-06-13 18:57:56 -03001915 error_propagate(errp, local_err);
Max Reitz5b363932016-05-17 16:41:31 +02001916 return NULL;
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001917}
1918
Max Reitz5b363932016-05-17 16:41:31 +02001919BlockDriverState *bdrv_open(const char *filename, const char *reference,
1920 QDict *options, int flags, Error **errp)
Kevin Wolff3930ed2015-04-08 13:43:47 +02001921{
Max Reitz5b363932016-05-17 16:41:31 +02001922 return bdrv_open_inherit(filename, reference, options, flags, NULL,
Max Reitzce343772015-08-26 19:47:50 +02001923 NULL, errp);
Kevin Wolff3930ed2015-04-08 13:43:47 +02001924}
1925
Jeff Codye971aa12012-09-20 15:13:19 -04001926typedef struct BlockReopenQueueEntry {
1927 bool prepared;
1928 BDRVReopenState state;
1929 QSIMPLEQ_ENTRY(BlockReopenQueueEntry) entry;
1930} BlockReopenQueueEntry;
1931
1932/*
1933 * Adds a BlockDriverState to a simple queue for an atomic, transactional
1934 * reopen of multiple devices.
1935 *
1936 * bs_queue can either be an existing BlockReopenQueue that has had QSIMPLE_INIT
1937 * already performed, or alternatively may be NULL a new BlockReopenQueue will
1938 * be created and initialized. This newly created BlockReopenQueue should be
1939 * passed back in for subsequent calls that are intended to be of the same
1940 * atomic 'set'.
1941 *
1942 * bs is the BlockDriverState to add to the reopen queue.
1943 *
Kevin Wolf4d2cb092015-04-10 17:50:50 +02001944 * options contains the changed options for the associated bs
1945 * (the BlockReopenQueue takes ownership)
1946 *
Jeff Codye971aa12012-09-20 15:13:19 -04001947 * flags contains the open flags for the associated bs
1948 *
1949 * returns a pointer to bs_queue, which is either the newly allocated
1950 * bs_queue, or the existing bs_queue being used.
1951 *
1952 */
Kevin Wolf28518102015-05-08 17:07:31 +02001953static BlockReopenQueue *bdrv_reopen_queue_child(BlockReopenQueue *bs_queue,
1954 BlockDriverState *bs,
1955 QDict *options,
1956 int flags,
1957 const BdrvChildRole *role,
1958 QDict *parent_options,
1959 int parent_flags)
Jeff Codye971aa12012-09-20 15:13:19 -04001960{
1961 assert(bs != NULL);
1962
1963 BlockReopenQueueEntry *bs_entry;
Kevin Wolf67251a32015-04-09 18:54:04 +02001964 BdrvChild *child;
Kevin Wolf145f5982015-05-08 16:15:03 +02001965 QDict *old_options, *explicit_options;
Kevin Wolf67251a32015-04-09 18:54:04 +02001966
Jeff Codye971aa12012-09-20 15:13:19 -04001967 if (bs_queue == NULL) {
1968 bs_queue = g_new0(BlockReopenQueue, 1);
1969 QSIMPLEQ_INIT(bs_queue);
1970 }
1971
Kevin Wolf4d2cb092015-04-10 17:50:50 +02001972 if (!options) {
1973 options = qdict_new();
1974 }
1975
Alberto Garcia5b7ba052016-09-15 17:53:03 +03001976 /* Check if this BlockDriverState is already in the queue */
1977 QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) {
1978 if (bs == bs_entry->state.bs) {
1979 break;
1980 }
1981 }
1982
Kevin Wolf28518102015-05-08 17:07:31 +02001983 /*
1984 * Precedence of options:
1985 * 1. Explicitly passed in options (highest)
Kevin Wolf91a097e2015-05-08 17:49:53 +02001986 * 2. Set in flags (only for top level)
Kevin Wolf145f5982015-05-08 16:15:03 +02001987 * 3. Retained from explicitly set options of bs
Kevin Wolf8e2160e2015-04-29 17:29:39 +02001988 * 4. Inherited from parent node
Kevin Wolf28518102015-05-08 17:07:31 +02001989 * 5. Retained from effective options of bs
1990 */
1991
Kevin Wolf91a097e2015-05-08 17:49:53 +02001992 if (!parent_options) {
1993 /*
1994 * Any setting represented by flags is always updated. If the
1995 * corresponding QDict option is set, it takes precedence. Otherwise
1996 * the flag is translated into a QDict option. The old setting of bs is
1997 * not considered.
1998 */
1999 update_options_from_flags(options, flags);
2000 }
2001
Kevin Wolf145f5982015-05-08 16:15:03 +02002002 /* Old explicitly set values (don't overwrite by inherited value) */
Alberto Garcia5b7ba052016-09-15 17:53:03 +03002003 if (bs_entry) {
2004 old_options = qdict_clone_shallow(bs_entry->state.explicit_options);
2005 } else {
2006 old_options = qdict_clone_shallow(bs->explicit_options);
2007 }
Kevin Wolf145f5982015-05-08 16:15:03 +02002008 bdrv_join_options(bs, options, old_options);
2009 QDECREF(old_options);
2010
2011 explicit_options = qdict_clone_shallow(options);
2012
Kevin Wolf28518102015-05-08 17:07:31 +02002013 /* Inherit from parent node */
2014 if (parent_options) {
2015 assert(!flags);
Kevin Wolf8e2160e2015-04-29 17:29:39 +02002016 role->inherit_options(&flags, options, parent_flags, parent_options);
Kevin Wolf28518102015-05-08 17:07:31 +02002017 }
2018
2019 /* Old values are used for options that aren't set yet */
Kevin Wolf4d2cb092015-04-10 17:50:50 +02002020 old_options = qdict_clone_shallow(bs->options);
Kevin Wolfcddff5b2015-11-16 16:43:27 +01002021 bdrv_join_options(bs, options, old_options);
Kevin Wolf4d2cb092015-04-10 17:50:50 +02002022 QDECREF(old_options);
2023
Kevin Wolff1f25a22014-04-25 19:04:55 +02002024 /* bdrv_open() masks this flag out */
2025 flags &= ~BDRV_O_PROTOCOL;
2026
Kevin Wolf67251a32015-04-09 18:54:04 +02002027 QLIST_FOREACH(child, &bs->children, next) {
Kevin Wolf4c9dfe52015-05-08 15:14:15 +02002028 QDict *new_child_options;
2029 char *child_key_dot;
Kevin Wolf67251a32015-04-09 18:54:04 +02002030
Kevin Wolf4c9dfe52015-05-08 15:14:15 +02002031 /* reopen can only change the options of block devices that were
2032 * implicitly created and inherited options. For other (referenced)
2033 * block devices, a syntax like "backing.foo" results in an error. */
Kevin Wolf67251a32015-04-09 18:54:04 +02002034 if (child->bs->inherits_from != bs) {
2035 continue;
2036 }
2037
Kevin Wolf4c9dfe52015-05-08 15:14:15 +02002038 child_key_dot = g_strdup_printf("%s.", child->name);
2039 qdict_extract_subqdict(options, &new_child_options, child_key_dot);
2040 g_free(child_key_dot);
2041
Kevin Wolf28518102015-05-08 17:07:31 +02002042 bdrv_reopen_queue_child(bs_queue, child->bs, new_child_options, 0,
2043 child->role, options, flags);
Jeff Codye971aa12012-09-20 15:13:19 -04002044 }
2045
Alberto Garcia5b7ba052016-09-15 17:53:03 +03002046 if (!bs_entry) {
2047 bs_entry = g_new0(BlockReopenQueueEntry, 1);
2048 QSIMPLEQ_INSERT_TAIL(bs_queue, bs_entry, entry);
2049 } else {
2050 QDECREF(bs_entry->state.options);
2051 QDECREF(bs_entry->state.explicit_options);
2052 }
Jeff Codye971aa12012-09-20 15:13:19 -04002053
2054 bs_entry->state.bs = bs;
Kevin Wolf4d2cb092015-04-10 17:50:50 +02002055 bs_entry->state.options = options;
Kevin Wolf145f5982015-05-08 16:15:03 +02002056 bs_entry->state.explicit_options = explicit_options;
Jeff Codye971aa12012-09-20 15:13:19 -04002057 bs_entry->state.flags = flags;
2058
2059 return bs_queue;
2060}
2061
Kevin Wolf28518102015-05-08 17:07:31 +02002062BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue,
2063 BlockDriverState *bs,
2064 QDict *options, int flags)
2065{
2066 return bdrv_reopen_queue_child(bs_queue, bs, options, flags,
2067 NULL, NULL, 0);
2068}
2069
Jeff Codye971aa12012-09-20 15:13:19 -04002070/*
2071 * Reopen multiple BlockDriverStates atomically & transactionally.
2072 *
2073 * The queue passed in (bs_queue) must have been built up previous
2074 * via bdrv_reopen_queue().
2075 *
2076 * Reopens all BDS specified in the queue, with the appropriate
2077 * flags. All devices are prepared for reopen, and failure of any
2078 * device will cause all device changes to be abandonded, and intermediate
2079 * data cleaned up.
2080 *
2081 * If all devices prepare successfully, then the changes are committed
2082 * to all devices.
2083 *
2084 */
Paolo Bonzini720150f2016-10-27 12:49:02 +02002085int bdrv_reopen_multiple(AioContext *ctx, BlockReopenQueue *bs_queue, Error **errp)
Jeff Codye971aa12012-09-20 15:13:19 -04002086{
2087 int ret = -1;
2088 BlockReopenQueueEntry *bs_entry, *next;
2089 Error *local_err = NULL;
2090
2091 assert(bs_queue != NULL);
2092
Paolo Bonzinic9d1a562016-10-27 12:49:05 +02002093 aio_context_release(ctx);
Alberto Garcia40840e42016-10-28 10:08:03 +03002094 bdrv_drain_all_begin();
Paolo Bonzinic9d1a562016-10-27 12:49:05 +02002095 aio_context_acquire(ctx);
Jeff Codye971aa12012-09-20 15:13:19 -04002096
2097 QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) {
2098 if (bdrv_reopen_prepare(&bs_entry->state, bs_queue, &local_err)) {
2099 error_propagate(errp, local_err);
2100 goto cleanup;
2101 }
2102 bs_entry->prepared = true;
2103 }
2104
2105 /* If we reach this point, we have success and just need to apply the
2106 * changes
2107 */
2108 QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) {
2109 bdrv_reopen_commit(&bs_entry->state);
2110 }
2111
2112 ret = 0;
2113
2114cleanup:
2115 QSIMPLEQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) {
2116 if (ret && bs_entry->prepared) {
2117 bdrv_reopen_abort(&bs_entry->state);
Kevin Wolf145f5982015-05-08 16:15:03 +02002118 } else if (ret) {
2119 QDECREF(bs_entry->state.explicit_options);
Jeff Codye971aa12012-09-20 15:13:19 -04002120 }
Kevin Wolf4d2cb092015-04-10 17:50:50 +02002121 QDECREF(bs_entry->state.options);
Jeff Codye971aa12012-09-20 15:13:19 -04002122 g_free(bs_entry);
2123 }
2124 g_free(bs_queue);
Alberto Garcia40840e42016-10-28 10:08:03 +03002125
2126 bdrv_drain_all_end();
2127
Jeff Codye971aa12012-09-20 15:13:19 -04002128 return ret;
2129}
2130
2131
2132/* Reopen a single BlockDriverState with the specified flags. */
2133int bdrv_reopen(BlockDriverState *bs, int bdrv_flags, Error **errp)
2134{
2135 int ret = -1;
2136 Error *local_err = NULL;
Kevin Wolf4d2cb092015-04-10 17:50:50 +02002137 BlockReopenQueue *queue = bdrv_reopen_queue(NULL, bs, NULL, bdrv_flags);
Jeff Codye971aa12012-09-20 15:13:19 -04002138
Paolo Bonzini720150f2016-10-27 12:49:02 +02002139 ret = bdrv_reopen_multiple(bdrv_get_aio_context(bs), queue, &local_err);
Jeff Codye971aa12012-09-20 15:13:19 -04002140 if (local_err != NULL) {
2141 error_propagate(errp, local_err);
2142 }
2143 return ret;
2144}
2145
2146
2147/*
2148 * Prepares a BlockDriverState for reopen. All changes are staged in the
2149 * 'opaque' field of the BDRVReopenState, which is used and allocated by
2150 * the block driver layer .bdrv_reopen_prepare()
2151 *
2152 * bs is the BlockDriverState to reopen
2153 * flags are the new open flags
2154 * queue is the reopen queue
2155 *
2156 * Returns 0 on success, non-zero on error. On error errp will be set
2157 * as well.
2158 *
2159 * On failure, bdrv_reopen_abort() will be called to clean up any data.
2160 * It is the responsibility of the caller to then call the abort() or
2161 * commit() for any other BDS that have been left in a prepare() state
2162 *
2163 */
2164int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue,
2165 Error **errp)
2166{
2167 int ret = -1;
2168 Error *local_err = NULL;
2169 BlockDriver *drv;
Kevin Wolfccf9dc02015-05-08 17:24:56 +02002170 QemuOpts *opts;
2171 const char *value;
Jeff Codye971aa12012-09-20 15:13:19 -04002172
2173 assert(reopen_state != NULL);
2174 assert(reopen_state->bs->drv != NULL);
2175 drv = reopen_state->bs->drv;
2176
Kevin Wolfccf9dc02015-05-08 17:24:56 +02002177 /* Process generic block layer options */
2178 opts = qemu_opts_create(&bdrv_runtime_opts, NULL, 0, &error_abort);
2179 qemu_opts_absorb_qdict(opts, reopen_state->options, &local_err);
2180 if (local_err) {
2181 error_propagate(errp, local_err);
2182 ret = -EINVAL;
2183 goto error;
2184 }
2185
Kevin Wolf91a097e2015-05-08 17:49:53 +02002186 update_flags_from_options(&reopen_state->flags, opts);
2187
Kevin Wolfccf9dc02015-05-08 17:24:56 +02002188 /* node-name and driver must be unchanged. Put them back into the QDict, so
2189 * that they are checked at the end of this function. */
2190 value = qemu_opt_get(opts, "node-name");
2191 if (value) {
2192 qdict_put(reopen_state->options, "node-name", qstring_from_str(value));
2193 }
2194
2195 value = qemu_opt_get(opts, "driver");
2196 if (value) {
2197 qdict_put(reopen_state->options, "driver", qstring_from_str(value));
2198 }
2199
Jeff Codye971aa12012-09-20 15:13:19 -04002200 /* if we are to stay read-only, do not allow permission change
2201 * to r/w */
2202 if (!(reopen_state->bs->open_flags & BDRV_O_ALLOW_RDWR) &&
2203 reopen_state->flags & BDRV_O_RDWR) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03002204 error_setg(errp, "Node '%s' is read only",
2205 bdrv_get_device_or_node_name(reopen_state->bs));
Jeff Codye971aa12012-09-20 15:13:19 -04002206 goto error;
2207 }
2208
2209
2210 ret = bdrv_flush(reopen_state->bs);
2211 if (ret) {
Eric Blake455b0fd2015-11-10 23:51:20 -07002212 error_setg_errno(errp, -ret, "Error flushing drive");
Jeff Codye971aa12012-09-20 15:13:19 -04002213 goto error;
2214 }
2215
2216 if (drv->bdrv_reopen_prepare) {
2217 ret = drv->bdrv_reopen_prepare(reopen_state, queue, &local_err);
2218 if (ret) {
2219 if (local_err != NULL) {
2220 error_propagate(errp, local_err);
2221 } else {
Luiz Capitulinod8b68952013-06-10 11:29:27 -04002222 error_setg(errp, "failed while preparing to reopen image '%s'",
2223 reopen_state->bs->filename);
Jeff Codye971aa12012-09-20 15:13:19 -04002224 }
2225 goto error;
2226 }
2227 } else {
2228 /* It is currently mandatory to have a bdrv_reopen_prepare()
2229 * handler for each supported drv. */
Alberto Garcia81e5f782015-04-08 12:29:19 +03002230 error_setg(errp, "Block format '%s' used by node '%s' "
2231 "does not support reopening files", drv->format_name,
2232 bdrv_get_device_or_node_name(reopen_state->bs));
Jeff Codye971aa12012-09-20 15:13:19 -04002233 ret = -1;
2234 goto error;
2235 }
2236
Kevin Wolf4d2cb092015-04-10 17:50:50 +02002237 /* Options that are not handled are only okay if they are unchanged
2238 * compared to the old state. It is expected that some options are only
2239 * used for the initial open, but not reopen (e.g. filename) */
2240 if (qdict_size(reopen_state->options)) {
2241 const QDictEntry *entry = qdict_first(reopen_state->options);
2242
2243 do {
2244 QString *new_obj = qobject_to_qstring(entry->value);
2245 const char *new = qstring_get_str(new_obj);
2246 const char *old = qdict_get_try_str(reopen_state->bs->options,
2247 entry->key);
2248
2249 if (!old || strcmp(new, old)) {
2250 error_setg(errp, "Cannot change the option '%s'", entry->key);
2251 ret = -EINVAL;
2252 goto error;
2253 }
2254 } while ((entry = qdict_next(reopen_state->options, entry)));
2255 }
2256
Jeff Codye971aa12012-09-20 15:13:19 -04002257 ret = 0;
2258
2259error:
Kevin Wolfccf9dc02015-05-08 17:24:56 +02002260 qemu_opts_del(opts);
Jeff Codye971aa12012-09-20 15:13:19 -04002261 return ret;
2262}
2263
2264/*
2265 * Takes the staged changes for the reopen from bdrv_reopen_prepare(), and
2266 * makes them final by swapping the staging BlockDriverState contents into
2267 * the active BlockDriverState contents.
2268 */
2269void bdrv_reopen_commit(BDRVReopenState *reopen_state)
2270{
2271 BlockDriver *drv;
2272
2273 assert(reopen_state != NULL);
2274 drv = reopen_state->bs->drv;
2275 assert(drv != NULL);
2276
2277 /* If there are any driver level actions to take */
2278 if (drv->bdrv_reopen_commit) {
2279 drv->bdrv_reopen_commit(reopen_state);
2280 }
2281
2282 /* set BDS specific flags now */
Kevin Wolf145f5982015-05-08 16:15:03 +02002283 QDECREF(reopen_state->bs->explicit_options);
2284
2285 reopen_state->bs->explicit_options = reopen_state->explicit_options;
Jeff Codye971aa12012-09-20 15:13:19 -04002286 reopen_state->bs->open_flags = reopen_state->flags;
Jeff Codye971aa12012-09-20 15:13:19 -04002287 reopen_state->bs->read_only = !(reopen_state->flags & BDRV_O_RDWR);
Kevin Wolf355ef4a2013-12-11 20:14:09 +01002288
Kevin Wolf3baca892014-07-16 17:48:16 +02002289 bdrv_refresh_limits(reopen_state->bs, NULL);
Jeff Codye971aa12012-09-20 15:13:19 -04002290}
2291
2292/*
2293 * Abort the reopen, and delete and free the staged changes in
2294 * reopen_state
2295 */
2296void bdrv_reopen_abort(BDRVReopenState *reopen_state)
2297{
2298 BlockDriver *drv;
2299
2300 assert(reopen_state != NULL);
2301 drv = reopen_state->bs->drv;
2302 assert(drv != NULL);
2303
2304 if (drv->bdrv_reopen_abort) {
2305 drv->bdrv_reopen_abort(reopen_state);
2306 }
Kevin Wolf145f5982015-05-08 16:15:03 +02002307
2308 QDECREF(reopen_state->explicit_options);
Jeff Codye971aa12012-09-20 15:13:19 -04002309}
2310
2311
Max Reitz64dff522016-01-29 16:36:10 +01002312static void bdrv_close(BlockDriverState *bs)
bellardfc01f7e2003-06-30 10:03:06 +00002313{
Max Reitz33384422014-06-20 21:57:33 +02002314 BdrvAioNotifier *ban, *ban_next;
2315
Max Reitzca9bd242016-01-29 16:36:14 +01002316 assert(!bs->job);
Max Reitz30f55fb2016-05-17 16:41:32 +02002317 assert(!bs->refcnt);
Alberto Garcia99b7e772015-09-25 16:41:44 +03002318
Paolo Bonzinifc272912015-12-23 11:48:24 +01002319 bdrv_drained_begin(bs); /* complete I/O */
Stefan Hajnoczi58fda172013-07-02 15:36:25 +02002320 bdrv_flush(bs);
Fam Zheng53ec73e2015-05-29 18:53:14 +08002321 bdrv_drain(bs); /* in case flush left pending I/O */
Paolo Bonzinifc272912015-12-23 11:48:24 +01002322
Max Reitzc5acdc92016-01-29 16:36:01 +01002323 bdrv_release_named_dirty_bitmaps(bs);
2324 assert(QLIST_EMPTY(&bs->dirty_bitmaps));
2325
Paolo Bonzini3cbc0022012-10-19 11:36:48 +02002326 if (bs->drv) {
Kevin Wolf6e93e7c2015-04-08 13:49:41 +02002327 BdrvChild *child, *next;
2328
Kevin Wolf9a7dedb2015-06-16 10:58:20 +02002329 bs->drv->bdrv_close(bs);
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02002330 bs->drv = NULL;
Kevin Wolf9a7dedb2015-06-16 10:58:20 +02002331
Kevin Wolf5db15a52015-09-14 15:33:33 +02002332 bdrv_set_backing_hd(bs, NULL);
Kevin Wolf9a7dedb2015-06-16 10:58:20 +02002333
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02002334 if (bs->file != NULL) {
2335 bdrv_unref_child(bs, bs->file);
2336 bs->file = NULL;
2337 }
2338
Kevin Wolf6e93e7c2015-04-08 13:49:41 +02002339 QLIST_FOREACH_SAFE(child, &bs->children, next, next) {
Kevin Wolf33a60402015-06-15 13:51:04 +02002340 /* TODO Remove bdrv_unref() from drivers' close function and use
2341 * bdrv_unref_child() here */
Kevin Wolfbddcec32015-04-09 18:47:50 +02002342 if (child->bs->inherits_from == bs) {
2343 child->bs->inherits_from = NULL;
2344 }
Kevin Wolf33a60402015-06-15 13:51:04 +02002345 bdrv_detach_child(child);
Kevin Wolf6e93e7c2015-04-08 13:49:41 +02002346 }
2347
Anthony Liguori7267c092011-08-20 22:09:37 -05002348 g_free(bs->opaque);
bellardea2384d2004-08-01 21:59:26 +00002349 bs->opaque = NULL;
Stefan Hajnoczi53fec9d2011-11-28 16:08:47 +00002350 bs->copy_on_read = 0;
Paolo Bonzinia275fa42012-05-08 16:51:43 +02002351 bs->backing_file[0] = '\0';
2352 bs->backing_format[0] = '\0';
Paolo Bonzini64058752012-05-08 16:51:49 +02002353 bs->total_sectors = 0;
Eric Blake54115412016-06-23 16:37:26 -06002354 bs->encrypted = false;
2355 bs->valid_key = false;
2356 bs->sg = false;
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01002357 QDECREF(bs->options);
Kevin Wolf145f5982015-05-08 16:15:03 +02002358 QDECREF(bs->explicit_options);
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01002359 bs->options = NULL;
Max Reitz91af7012014-07-18 20:24:56 +02002360 QDECREF(bs->full_open_options);
2361 bs->full_open_options = NULL;
bellardb3380822004-03-14 21:38:54 +00002362 }
Zhi Yong Wu98f90db2011-11-08 13:00:14 +08002363
Max Reitz33384422014-06-20 21:57:33 +02002364 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) {
2365 g_free(ban);
2366 }
2367 QLIST_INIT(&bs->aio_notifiers);
Paolo Bonzinifc272912015-12-23 11:48:24 +01002368 bdrv_drained_end(bs);
bellardb3380822004-03-14 21:38:54 +00002369}
2370
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +09002371void bdrv_close_all(void)
2372{
Kevin Wolfa1a2af02016-04-08 18:26:37 +02002373 block_job_cancel_sync_all();
Kevin Wolfcd7fca92016-07-06 11:22:39 +02002374 nbd_export_close_all();
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +09002375
Max Reitzca9bd242016-01-29 16:36:14 +01002376 /* Drop references from requests still in flight, such as canceled block
2377 * jobs whose AIO context has not been polled yet */
2378 bdrv_drain_all();
Stefan Hajnoczied78cda2014-05-08 16:34:35 +02002379
Max Reitzca9bd242016-01-29 16:36:14 +01002380 blk_remove_all_bs();
2381 blockdev_close_all_bdrv_states();
2382
Kevin Wolfa1a2af02016-04-08 18:26:37 +02002383 assert(QTAILQ_EMPTY(&all_bdrv_states));
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +09002384}
2385
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002386static void change_parent_backing_link(BlockDriverState *from,
2387 BlockDriverState *to)
2388{
Max Reitz9bd910e2016-06-10 20:57:46 +02002389 BdrvChild *c, *next, *to_c;
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002390
2391 QLIST_FOREACH_SAFE(c, &from->parents, next_parent, next) {
Max Reitz9bd910e2016-06-10 20:57:46 +02002392 if (c->role == &child_backing) {
2393 /* @from is generally not allowed to be a backing file, except for
2394 * when @to is the overlay. In that case, @from may not be replaced
2395 * by @to as @to's backing node. */
2396 QLIST_FOREACH(to_c, &to->children, next) {
2397 if (to_c == c) {
2398 break;
2399 }
2400 }
2401 if (to_c) {
2402 continue;
2403 }
2404 }
2405
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002406 assert(c->role != &child_backing);
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002407 bdrv_ref(to);
Kevin Wolfe9740bc2016-05-23 15:52:26 +02002408 bdrv_replace_child(c, to);
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002409 bdrv_unref(from);
2410 }
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002411}
2412
Jeff Cody8802d1f2012-02-28 15:54:06 -05002413/*
2414 * Add new bs contents at the top of an image chain while the chain is
2415 * live, while keeping required fields on the top layer.
2416 *
2417 * This will modify the BlockDriverState fields, and swap contents
2418 * between bs_new and bs_top. Both bs_new and bs_top are modified.
2419 *
Markus Armbrusterbfb197e2014-10-07 13:59:11 +02002420 * bs_new must not be attached to a BlockBackend.
Jeff Codyf6801b82012-03-27 16:30:19 -04002421 *
Jeff Cody8802d1f2012-02-28 15:54:06 -05002422 * This function does not create any image files.
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002423 *
2424 * bdrv_append() takes ownership of a bs_new reference and unrefs it because
2425 * that's what the callers commonly need. bs_new will be referenced by the old
2426 * parents of bs_top after bdrv_append() returns. If the caller needs to keep a
2427 * reference of its own, it must call bdrv_ref().
Jeff Cody8802d1f2012-02-28 15:54:06 -05002428 */
2429void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top)
2430{
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002431 assert(!bdrv_requests_pending(bs_top));
2432 assert(!bdrv_requests_pending(bs_new));
Jeff Cody8802d1f2012-02-28 15:54:06 -05002433
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002434 bdrv_ref(bs_top);
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002435
Kevin Wolf27ccdd52016-03-21 12:56:44 +01002436 change_parent_backing_link(bs_top, bs_new);
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002437 bdrv_set_backing_hd(bs_new, bs_top);
2438 bdrv_unref(bs_top);
2439
2440 /* bs_new is now referenced by its new parents, we don't need the
2441 * additional reference any more. */
2442 bdrv_unref(bs_new);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002443}
2444
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02002445void bdrv_replace_in_backing_chain(BlockDriverState *old, BlockDriverState *new)
2446{
2447 assert(!bdrv_requests_pending(old));
2448 assert(!bdrv_requests_pending(new));
2449
2450 bdrv_ref(old);
2451
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02002452 change_parent_backing_link(old, new);
2453
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02002454 bdrv_unref(old);
2455}
2456
Fam Zheng4f6fd342013-08-23 09:14:47 +08002457static void bdrv_delete(BlockDriverState *bs)
bellardb3380822004-03-14 21:38:54 +00002458{
Paolo Bonzini3e914652012-03-30 13:17:11 +02002459 assert(!bs->job);
Fam Zheng3718d8a2014-05-23 21:29:43 +08002460 assert(bdrv_op_blocker_is_empty(bs));
Fam Zheng4f6fd342013-08-23 09:14:47 +08002461 assert(!bs->refcnt);
Markus Armbruster18846de2010-06-29 16:58:30 +02002462
Stefan Hajnoczie1b5c522013-06-27 15:32:26 +02002463 bdrv_close(bs);
2464
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01002465 /* remove from list, if necessary */
Kevin Wolf63eaaae2016-03-18 10:46:57 +01002466 if (bs->node_name[0] != '\0') {
2467 QTAILQ_REMOVE(&graph_bdrv_states, bs, node_list);
2468 }
Max Reitz2c1d04e2016-01-29 16:36:11 +01002469 QTAILQ_REMOVE(&all_bdrv_states, bs, bs_list);
2470
Anthony Liguori7267c092011-08-20 22:09:37 -05002471 g_free(bs);
bellardfc01f7e2003-06-30 10:03:06 +00002472}
2473
aliguorie97fc192009-04-21 23:11:50 +00002474/*
2475 * Run consistency checks on an image
2476 *
Kevin Wolfe076f332010-06-29 11:43:13 +02002477 * Returns 0 if the check could be completed (it doesn't mean that the image is
Stefan Weila1c72732011-04-28 17:20:38 +02002478 * free of errors) or -errno when an internal error occurred. The results of the
Kevin Wolfe076f332010-06-29 11:43:13 +02002479 * check are stored in res.
aliguorie97fc192009-04-21 23:11:50 +00002480 */
Kevin Wolf4534ff52012-05-11 16:07:02 +02002481int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix)
aliguorie97fc192009-04-21 23:11:50 +00002482{
Max Reitz908bcd52014-08-07 22:47:55 +02002483 if (bs->drv == NULL) {
2484 return -ENOMEDIUM;
2485 }
aliguorie97fc192009-04-21 23:11:50 +00002486 if (bs->drv->bdrv_check == NULL) {
2487 return -ENOTSUP;
2488 }
2489
Kevin Wolfe076f332010-06-29 11:43:13 +02002490 memset(res, 0, sizeof(*res));
Kevin Wolf4534ff52012-05-11 16:07:02 +02002491 return bs->drv->bdrv_check(bs, res, fix);
aliguorie97fc192009-04-21 23:11:50 +00002492}
2493
Kevin Wolf756e6732010-01-12 12:55:17 +01002494/*
2495 * Return values:
2496 * 0 - success
2497 * -EINVAL - backing format specified, but no file
2498 * -ENOSPC - can't update the backing file because no space is left in the
2499 * image file header
2500 * -ENOTSUP - format driver doesn't support changing the backing file
2501 */
2502int bdrv_change_backing_file(BlockDriverState *bs,
2503 const char *backing_file, const char *backing_fmt)
2504{
2505 BlockDriver *drv = bs->drv;
Paolo Bonzini469ef352012-04-12 14:01:02 +02002506 int ret;
Kevin Wolf756e6732010-01-12 12:55:17 +01002507
Paolo Bonzini5f377792012-04-12 14:01:01 +02002508 /* Backing file format doesn't make sense without a backing file */
2509 if (backing_fmt && !backing_file) {
2510 return -EINVAL;
2511 }
2512
Kevin Wolf756e6732010-01-12 12:55:17 +01002513 if (drv->bdrv_change_backing_file != NULL) {
Paolo Bonzini469ef352012-04-12 14:01:02 +02002514 ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
Kevin Wolf756e6732010-01-12 12:55:17 +01002515 } else {
Paolo Bonzini469ef352012-04-12 14:01:02 +02002516 ret = -ENOTSUP;
Kevin Wolf756e6732010-01-12 12:55:17 +01002517 }
Paolo Bonzini469ef352012-04-12 14:01:02 +02002518
2519 if (ret == 0) {
2520 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
2521 pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
2522 }
2523 return ret;
Kevin Wolf756e6732010-01-12 12:55:17 +01002524}
2525
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002526/*
2527 * Finds the image layer in the chain that has 'bs' as its backing file.
2528 *
2529 * active is the current topmost image.
2530 *
2531 * Returns NULL if bs is not found in active's image chain,
2532 * or if active == bs.
Jeff Cody4caf0fc2014-06-25 15:35:26 -04002533 *
2534 * Returns the bottommost base image if bs == NULL.
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002535 */
2536BlockDriverState *bdrv_find_overlay(BlockDriverState *active,
2537 BlockDriverState *bs)
2538{
Kevin Wolf760e0062015-06-17 14:55:21 +02002539 while (active && bs != backing_bs(active)) {
2540 active = backing_bs(active);
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002541 }
2542
Jeff Cody4caf0fc2014-06-25 15:35:26 -04002543 return active;
2544}
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002545
Jeff Cody4caf0fc2014-06-25 15:35:26 -04002546/* Given a BDS, searches for the base layer. */
2547BlockDriverState *bdrv_find_base(BlockDriverState *bs)
2548{
2549 return bdrv_find_overlay(bs, NULL);
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002550}
2551
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002552/*
2553 * Drops images above 'base' up to and including 'top', and sets the image
2554 * above 'top' to have base as its backing file.
2555 *
2556 * Requires that the overlay to 'top' is opened r/w, so that the backing file
2557 * information in 'bs' can be properly updated.
2558 *
2559 * E.g., this will convert the following chain:
2560 * bottom <- base <- intermediate <- top <- active
2561 *
2562 * to
2563 *
2564 * bottom <- base <- active
2565 *
2566 * It is allowed for bottom==base, in which case it converts:
2567 *
2568 * base <- intermediate <- top <- active
2569 *
2570 * to
2571 *
2572 * base <- active
2573 *
Jeff Cody54e26902014-06-25 15:40:10 -04002574 * If backing_file_str is non-NULL, it will be used when modifying top's
2575 * overlay image metadata.
2576 *
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002577 * Error conditions:
2578 * if active == top, that is considered an error
2579 *
2580 */
2581int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top,
Jeff Cody54e26902014-06-25 15:40:10 -04002582 BlockDriverState *base, const char *backing_file_str)
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002583{
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002584 BlockDriverState *new_top_bs = NULL;
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002585 int ret = -EIO;
2586
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002587 if (!top->drv || !base->drv) {
2588 goto exit;
2589 }
2590
2591 new_top_bs = bdrv_find_overlay(active, top);
2592
2593 if (new_top_bs == NULL) {
2594 /* we could not find the image above 'top', this is an error */
2595 goto exit;
2596 }
2597
Kevin Wolf760e0062015-06-17 14:55:21 +02002598 /* special case of new_top_bs->backing->bs already pointing to base - nothing
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002599 * to do, no intermediate images */
Kevin Wolf760e0062015-06-17 14:55:21 +02002600 if (backing_bs(new_top_bs) == base) {
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002601 ret = 0;
2602 goto exit;
2603 }
2604
Kevin Wolf5db15a52015-09-14 15:33:33 +02002605 /* Make sure that base is in the backing chain of top */
2606 if (!bdrv_chain_contains(top, base)) {
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002607 goto exit;
2608 }
2609
2610 /* success - we can delete the intermediate states, and link top->base */
Kevin Wolf5db15a52015-09-14 15:33:33 +02002611 backing_file_str = backing_file_str ? backing_file_str : base->filename;
Jeff Cody54e26902014-06-25 15:40:10 -04002612 ret = bdrv_change_backing_file(new_top_bs, backing_file_str,
Kevin Wolf5db15a52015-09-14 15:33:33 +02002613 base->drv ? base->drv->format_name : "");
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002614 if (ret) {
2615 goto exit;
2616 }
Kevin Wolf5db15a52015-09-14 15:33:33 +02002617 bdrv_set_backing_hd(new_top_bs, base);
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002618
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002619 ret = 0;
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002620exit:
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002621 return ret;
2622}
2623
bellard83f64092006-08-01 16:21:11 +00002624/**
bellard83f64092006-08-01 16:21:11 +00002625 * Truncate file to 'offset' bytes (needed only for file protocols)
2626 */
2627int bdrv_truncate(BlockDriverState *bs, int64_t offset)
2628{
2629 BlockDriver *drv = bs->drv;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01002630 int ret;
bellard83f64092006-08-01 16:21:11 +00002631 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002632 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +00002633 if (!drv->bdrv_truncate)
2634 return -ENOTSUP;
Naphtali Sprei59f26892009-10-26 16:25:16 +02002635 if (bs->read_only)
2636 return -EACCES;
Jeff Cody9c75e162014-06-25 16:55:30 -04002637
Stefan Hajnoczi51762282010-04-19 16:56:41 +01002638 ret = drv->bdrv_truncate(bs, offset);
2639 if (ret == 0) {
2640 ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
John Snowce1ffea2015-04-17 19:50:03 -04002641 bdrv_dirty_bitmap_truncate(bs);
Kevin Wolf5c8cab42016-02-24 15:13:35 +01002642 bdrv_parent_cb_resize(bs);
Evgeny Yakovlev3ff2f672016-07-18 22:39:52 +03002643 ++bs->write_gen;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01002644 }
2645 return ret;
bellard83f64092006-08-01 16:21:11 +00002646}
2647
2648/**
Fam Zheng4a1d5e12011-07-12 19:56:39 +08002649 * Length of a allocated file in bytes. Sparse files are counted by actual
2650 * allocated space. Return < 0 if error or unknown.
2651 */
2652int64_t bdrv_get_allocated_file_size(BlockDriverState *bs)
2653{
2654 BlockDriver *drv = bs->drv;
2655 if (!drv) {
2656 return -ENOMEDIUM;
2657 }
2658 if (drv->bdrv_get_allocated_file_size) {
2659 return drv->bdrv_get_allocated_file_size(bs);
2660 }
2661 if (bs->file) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02002662 return bdrv_get_allocated_file_size(bs->file->bs);
Fam Zheng4a1d5e12011-07-12 19:56:39 +08002663 }
2664 return -ENOTSUP;
2665}
2666
2667/**
Markus Armbruster65a9bb22014-06-26 13:23:17 +02002668 * Return number of sectors on success, -errno on error.
bellard83f64092006-08-01 16:21:11 +00002669 */
Markus Armbruster65a9bb22014-06-26 13:23:17 +02002670int64_t bdrv_nb_sectors(BlockDriverState *bs)
bellard83f64092006-08-01 16:21:11 +00002671{
2672 BlockDriver *drv = bs->drv;
Markus Armbruster65a9bb22014-06-26 13:23:17 +02002673
bellard83f64092006-08-01 16:21:11 +00002674 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002675 return -ENOMEDIUM;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01002676
Kevin Wolfb94a2612013-10-29 12:18:58 +01002677 if (drv->has_variable_length) {
2678 int ret = refresh_total_sectors(bs, bs->total_sectors);
2679 if (ret < 0) {
2680 return ret;
Stefan Hajnoczi46a4e4e2011-03-29 20:04:41 +01002681 }
bellard83f64092006-08-01 16:21:11 +00002682 }
Markus Armbruster65a9bb22014-06-26 13:23:17 +02002683 return bs->total_sectors;
2684}
2685
2686/**
2687 * Return length in bytes on success, -errno on error.
2688 * The length is always a multiple of BDRV_SECTOR_SIZE.
2689 */
2690int64_t bdrv_getlength(BlockDriverState *bs)
2691{
2692 int64_t ret = bdrv_nb_sectors(bs);
2693
Fam Zheng4a9c9ea2015-05-15 16:36:05 +08002694 ret = ret > INT64_MAX / BDRV_SECTOR_SIZE ? -EFBIG : ret;
Markus Armbruster65a9bb22014-06-26 13:23:17 +02002695 return ret < 0 ? ret : ret * BDRV_SECTOR_SIZE;
bellardfc01f7e2003-06-30 10:03:06 +00002696}
2697
bellard19cb3732006-08-19 11:45:59 +00002698/* return 0 as number of sectors if no device present or error */
ths96b8f132007-12-17 01:35:20 +00002699void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
bellardfc01f7e2003-06-30 10:03:06 +00002700{
Markus Armbruster65a9bb22014-06-26 13:23:17 +02002701 int64_t nb_sectors = bdrv_nb_sectors(bs);
2702
2703 *nb_sectors_ptr = nb_sectors < 0 ? 0 : nb_sectors;
bellardfc01f7e2003-06-30 10:03:06 +00002704}
bellardcf989512004-02-16 21:56:36 +00002705
Eric Blake54115412016-06-23 16:37:26 -06002706bool bdrv_is_read_only(BlockDriverState *bs)
bellardb3380822004-03-14 21:38:54 +00002707{
2708 return bs->read_only;
2709}
2710
Eric Blake54115412016-06-23 16:37:26 -06002711bool bdrv_is_sg(BlockDriverState *bs)
ths985a03b2007-12-24 16:10:43 +00002712{
2713 return bs->sg;
2714}
2715
Eric Blake54115412016-06-23 16:37:26 -06002716bool bdrv_is_encrypted(BlockDriverState *bs)
bellardea2384d2004-08-01 21:59:26 +00002717{
Kevin Wolf760e0062015-06-17 14:55:21 +02002718 if (bs->backing && bs->backing->bs->encrypted) {
Eric Blake54115412016-06-23 16:37:26 -06002719 return true;
Kevin Wolf760e0062015-06-17 14:55:21 +02002720 }
bellardea2384d2004-08-01 21:59:26 +00002721 return bs->encrypted;
2722}
2723
Eric Blake54115412016-06-23 16:37:26 -06002724bool bdrv_key_required(BlockDriverState *bs)
aliguoric0f4ce72009-03-05 23:01:01 +00002725{
Kevin Wolf760e0062015-06-17 14:55:21 +02002726 BdrvChild *backing = bs->backing;
aliguoric0f4ce72009-03-05 23:01:01 +00002727
Kevin Wolf760e0062015-06-17 14:55:21 +02002728 if (backing && backing->bs->encrypted && !backing->bs->valid_key) {
Eric Blake54115412016-06-23 16:37:26 -06002729 return true;
Kevin Wolf760e0062015-06-17 14:55:21 +02002730 }
aliguoric0f4ce72009-03-05 23:01:01 +00002731 return (bs->encrypted && !bs->valid_key);
2732}
2733
bellardea2384d2004-08-01 21:59:26 +00002734int bdrv_set_key(BlockDriverState *bs, const char *key)
2735{
2736 int ret;
Kevin Wolf760e0062015-06-17 14:55:21 +02002737 if (bs->backing && bs->backing->bs->encrypted) {
2738 ret = bdrv_set_key(bs->backing->bs, key);
bellardea2384d2004-08-01 21:59:26 +00002739 if (ret < 0)
2740 return ret;
2741 if (!bs->encrypted)
2742 return 0;
2743 }
Shahar Havivifd04a2a2010-03-06 00:26:13 +02002744 if (!bs->encrypted) {
2745 return -EINVAL;
2746 } else if (!bs->drv || !bs->drv->bdrv_set_key) {
2747 return -ENOMEDIUM;
2748 }
aliguoric0f4ce72009-03-05 23:01:01 +00002749 ret = bs->drv->bdrv_set_key(bs, key);
aliguoribb5fc202009-03-05 23:01:15 +00002750 if (ret < 0) {
Eric Blake54115412016-06-23 16:37:26 -06002751 bs->valid_key = false;
aliguoribb5fc202009-03-05 23:01:15 +00002752 } else if (!bs->valid_key) {
Kevin Wolf5c8cab42016-02-24 15:13:35 +01002753 /* call the change callback now, we skipped it on open */
Eric Blake54115412016-06-23 16:37:26 -06002754 bs->valid_key = true;
Kevin Wolf5c8cab42016-02-24 15:13:35 +01002755 bdrv_parent_cb_change_media(bs, true);
aliguoribb5fc202009-03-05 23:01:15 +00002756 }
aliguoric0f4ce72009-03-05 23:01:01 +00002757 return ret;
bellardea2384d2004-08-01 21:59:26 +00002758}
2759
Markus Armbruster4d2855a2015-01-29 10:37:00 +01002760/*
2761 * Provide an encryption key for @bs.
2762 * If @key is non-null:
2763 * If @bs is not encrypted, fail.
2764 * Else if the key is invalid, fail.
2765 * Else set @bs's key to @key, replacing the existing key, if any.
2766 * If @key is null:
2767 * If @bs is encrypted and still lacks a key, fail.
2768 * Else do nothing.
2769 * On failure, store an error object through @errp if non-null.
2770 */
2771void bdrv_add_key(BlockDriverState *bs, const char *key, Error **errp)
2772{
2773 if (key) {
2774 if (!bdrv_is_encrypted(bs)) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03002775 error_setg(errp, "Node '%s' is not encrypted",
2776 bdrv_get_device_or_node_name(bs));
Markus Armbruster4d2855a2015-01-29 10:37:00 +01002777 } else if (bdrv_set_key(bs, key) < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002778 error_setg(errp, QERR_INVALID_PASSWORD);
Markus Armbruster4d2855a2015-01-29 10:37:00 +01002779 }
2780 } else {
2781 if (bdrv_key_required(bs)) {
Markus Armbrusterb1ca6392015-01-29 10:37:01 +01002782 error_set(errp, ERROR_CLASS_DEVICE_ENCRYPTED,
2783 "'%s' (%s) is encrypted",
Alberto Garcia81e5f782015-04-08 12:29:19 +03002784 bdrv_get_device_or_node_name(bs),
Markus Armbruster4d2855a2015-01-29 10:37:00 +01002785 bdrv_get_encrypted_filename(bs));
2786 }
2787 }
2788}
2789
Markus Armbrusterf8d6bba2012-06-13 10:11:48 +02002790const char *bdrv_get_format_name(BlockDriverState *bs)
bellardea2384d2004-08-01 21:59:26 +00002791{
Markus Armbrusterf8d6bba2012-06-13 10:11:48 +02002792 return bs->drv ? bs->drv->format_name : NULL;
bellardea2384d2004-08-01 21:59:26 +00002793}
2794
Stefan Hajnocziada42402014-08-27 12:08:55 +01002795static int qsort_strcmp(const void *a, const void *b)
2796{
2797 return strcmp(a, b);
2798}
2799
ths5fafdf22007-09-16 21:08:06 +00002800void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
bellardea2384d2004-08-01 21:59:26 +00002801 void *opaque)
2802{
2803 BlockDriver *drv;
Jeff Codye855e4f2014-04-28 18:29:54 -04002804 int count = 0;
Stefan Hajnocziada42402014-08-27 12:08:55 +01002805 int i;
Jeff Codye855e4f2014-04-28 18:29:54 -04002806 const char **formats = NULL;
bellardea2384d2004-08-01 21:59:26 +00002807
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +01002808 QLIST_FOREACH(drv, &bdrv_drivers, list) {
Jeff Codye855e4f2014-04-28 18:29:54 -04002809 if (drv->format_name) {
2810 bool found = false;
2811 int i = count;
2812 while (formats && i && !found) {
2813 found = !strcmp(formats[--i], drv->format_name);
2814 }
2815
2816 if (!found) {
Markus Armbruster5839e532014-08-19 10:31:08 +02002817 formats = g_renew(const char *, formats, count + 1);
Jeff Codye855e4f2014-04-28 18:29:54 -04002818 formats[count++] = drv->format_name;
Jeff Codye855e4f2014-04-28 18:29:54 -04002819 }
2820 }
bellardea2384d2004-08-01 21:59:26 +00002821 }
Stefan Hajnocziada42402014-08-27 12:08:55 +01002822
2823 qsort(formats, count, sizeof(formats[0]), qsort_strcmp);
2824
2825 for (i = 0; i < count; i++) {
2826 it(opaque, formats[i]);
2827 }
2828
Jeff Codye855e4f2014-04-28 18:29:54 -04002829 g_free(formats);
bellardea2384d2004-08-01 21:59:26 +00002830}
2831
Benoît Canetdc364f42014-01-23 21:31:32 +01002832/* This function is to find a node in the bs graph */
2833BlockDriverState *bdrv_find_node(const char *node_name)
2834{
2835 BlockDriverState *bs;
2836
2837 assert(node_name);
2838
2839 QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) {
2840 if (!strcmp(node_name, bs->node_name)) {
2841 return bs;
2842 }
2843 }
2844 return NULL;
2845}
2846
Benoît Canetc13163f2014-01-23 21:31:34 +01002847/* Put this QMP function here so it can access the static graph_bdrv_states. */
Alberto Garciad5a8ee62015-04-17 14:52:43 +03002848BlockDeviceInfoList *bdrv_named_nodes_list(Error **errp)
Benoît Canetc13163f2014-01-23 21:31:34 +01002849{
2850 BlockDeviceInfoList *list, *entry;
2851 BlockDriverState *bs;
2852
2853 list = NULL;
2854 QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) {
Kevin Wolfc83f9fb2016-03-03 11:37:48 +01002855 BlockDeviceInfo *info = bdrv_block_device_info(NULL, bs, errp);
Alberto Garciad5a8ee62015-04-17 14:52:43 +03002856 if (!info) {
2857 qapi_free_BlockDeviceInfoList(list);
2858 return NULL;
2859 }
Benoît Canetc13163f2014-01-23 21:31:34 +01002860 entry = g_malloc0(sizeof(*entry));
Alberto Garciad5a8ee62015-04-17 14:52:43 +03002861 entry->value = info;
Benoît Canetc13163f2014-01-23 21:31:34 +01002862 entry->next = list;
2863 list = entry;
2864 }
2865
2866 return list;
2867}
2868
Benoît Canet12d3ba82014-01-23 21:31:35 +01002869BlockDriverState *bdrv_lookup_bs(const char *device,
2870 const char *node_name,
2871 Error **errp)
2872{
Markus Armbruster7f06d472014-10-07 13:59:12 +02002873 BlockBackend *blk;
2874 BlockDriverState *bs;
Benoît Canet12d3ba82014-01-23 21:31:35 +01002875
Benoît Canet12d3ba82014-01-23 21:31:35 +01002876 if (device) {
Markus Armbruster7f06d472014-10-07 13:59:12 +02002877 blk = blk_by_name(device);
Benoît Canet12d3ba82014-01-23 21:31:35 +01002878
Markus Armbruster7f06d472014-10-07 13:59:12 +02002879 if (blk) {
Alberto Garcia9f4ed6f2015-10-26 16:46:49 +02002880 bs = blk_bs(blk);
2881 if (!bs) {
Max Reitz5433c242015-10-19 17:53:29 +02002882 error_setg(errp, "Device '%s' has no medium", device);
Max Reitz5433c242015-10-19 17:53:29 +02002883 }
2884
Alberto Garcia9f4ed6f2015-10-26 16:46:49 +02002885 return bs;
Benoît Canet12d3ba82014-01-23 21:31:35 +01002886 }
Benoît Canet12d3ba82014-01-23 21:31:35 +01002887 }
2888
Benoît Canetdd67fa52014-02-12 17:15:06 +01002889 if (node_name) {
2890 bs = bdrv_find_node(node_name);
Benoît Canet12d3ba82014-01-23 21:31:35 +01002891
Benoît Canetdd67fa52014-02-12 17:15:06 +01002892 if (bs) {
2893 return bs;
2894 }
Benoît Canet12d3ba82014-01-23 21:31:35 +01002895 }
2896
Benoît Canetdd67fa52014-02-12 17:15:06 +01002897 error_setg(errp, "Cannot find device=%s nor node_name=%s",
2898 device ? device : "",
2899 node_name ? node_name : "");
2900 return NULL;
Benoît Canet12d3ba82014-01-23 21:31:35 +01002901}
2902
Jeff Cody5a6684d2014-06-25 15:40:09 -04002903/* If 'base' is in the same chain as 'top', return true. Otherwise,
2904 * return false. If either argument is NULL, return false. */
2905bool bdrv_chain_contains(BlockDriverState *top, BlockDriverState *base)
2906{
2907 while (top && top != base) {
Kevin Wolf760e0062015-06-17 14:55:21 +02002908 top = backing_bs(top);
Jeff Cody5a6684d2014-06-25 15:40:09 -04002909 }
2910
2911 return top != NULL;
2912}
2913
Fam Zheng04df7652014-10-31 11:32:54 +08002914BlockDriverState *bdrv_next_node(BlockDriverState *bs)
2915{
2916 if (!bs) {
2917 return QTAILQ_FIRST(&graph_bdrv_states);
2918 }
2919 return QTAILQ_NEXT(bs, node_list);
2920}
2921
Fam Zheng20a9e772014-10-31 11:32:55 +08002922const char *bdrv_get_node_name(const BlockDriverState *bs)
2923{
2924 return bs->node_name;
2925}
2926
Kevin Wolf1f0c4612016-03-22 18:38:44 +01002927const char *bdrv_get_parent_name(const BlockDriverState *bs)
Kevin Wolf4c265bf2016-02-26 10:22:16 +01002928{
2929 BdrvChild *c;
2930 const char *name;
2931
2932 /* If multiple parents have a name, just pick the first one. */
2933 QLIST_FOREACH(c, &bs->parents, next_parent) {
2934 if (c->role->get_name) {
2935 name = c->role->get_name(c);
2936 if (name && *name) {
2937 return name;
2938 }
2939 }
2940 }
2941
2942 return NULL;
2943}
2944
Markus Armbruster7f06d472014-10-07 13:59:12 +02002945/* TODO check what callers really want: bs->node_name or blk_name() */
Markus Armbrusterbfb197e2014-10-07 13:59:11 +02002946const char *bdrv_get_device_name(const BlockDriverState *bs)
bellardea2384d2004-08-01 21:59:26 +00002947{
Kevin Wolf4c265bf2016-02-26 10:22:16 +01002948 return bdrv_get_parent_name(bs) ?: "";
bellardea2384d2004-08-01 21:59:26 +00002949}
2950
Alberto Garcia9b2aa842015-04-08 12:29:18 +03002951/* This can be used to identify nodes that might not have a device
2952 * name associated. Since node and device names live in the same
2953 * namespace, the result is unambiguous. The exception is if both are
2954 * absent, then this returns an empty (non-null) string. */
2955const char *bdrv_get_device_or_node_name(const BlockDriverState *bs)
2956{
Kevin Wolf4c265bf2016-02-26 10:22:16 +01002957 return bdrv_get_parent_name(bs) ?: bs->node_name;
Alberto Garcia9b2aa842015-04-08 12:29:18 +03002958}
2959
Markus Armbrusterc8433282012-06-05 16:49:24 +02002960int bdrv_get_flags(BlockDriverState *bs)
2961{
2962 return bs->open_flags;
2963}
2964
Peter Lieven3ac21622013-06-28 12:47:42 +02002965int bdrv_has_zero_init_1(BlockDriverState *bs)
2966{
2967 return 1;
2968}
2969
Kevin Wolff2feebb2010-04-14 17:30:35 +02002970int bdrv_has_zero_init(BlockDriverState *bs)
2971{
2972 assert(bs->drv);
2973
Paolo Bonzini11212d82013-09-04 19:00:27 +02002974 /* If BS is a copy on write image, it is initialized to
2975 the contents of the base image, which may not be zeroes. */
Kevin Wolf760e0062015-06-17 14:55:21 +02002976 if (bs->backing) {
Paolo Bonzini11212d82013-09-04 19:00:27 +02002977 return 0;
2978 }
Kevin Wolf336c1c12010-07-28 11:26:29 +02002979 if (bs->drv->bdrv_has_zero_init) {
2980 return bs->drv->bdrv_has_zero_init(bs);
Kevin Wolff2feebb2010-04-14 17:30:35 +02002981 }
2982
Peter Lieven3ac21622013-06-28 12:47:42 +02002983 /* safe default */
2984 return 0;
Kevin Wolff2feebb2010-04-14 17:30:35 +02002985}
2986
Peter Lieven4ce78692013-10-24 12:06:54 +02002987bool bdrv_unallocated_blocks_are_zero(BlockDriverState *bs)
2988{
2989 BlockDriverInfo bdi;
2990
Kevin Wolf760e0062015-06-17 14:55:21 +02002991 if (bs->backing) {
Peter Lieven4ce78692013-10-24 12:06:54 +02002992 return false;
2993 }
2994
2995 if (bdrv_get_info(bs, &bdi) == 0) {
2996 return bdi.unallocated_blocks_are_zero;
2997 }
2998
2999 return false;
3000}
3001
3002bool bdrv_can_write_zeroes_with_unmap(BlockDriverState *bs)
3003{
3004 BlockDriverInfo bdi;
3005
Denis V. Lunev2f0342e2016-07-14 16:33:26 +03003006 if (!(bs->open_flags & BDRV_O_UNMAP)) {
Peter Lieven4ce78692013-10-24 12:06:54 +02003007 return false;
3008 }
3009
3010 if (bdrv_get_info(bs, &bdi) == 0) {
3011 return bdi.can_write_zeroes_with_unmap;
3012 }
3013
3014 return false;
3015}
3016
aliguori045df332009-03-05 23:00:48 +00003017const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
3018{
Kevin Wolf760e0062015-06-17 14:55:21 +02003019 if (bs->backing && bs->backing->bs->encrypted)
aliguori045df332009-03-05 23:00:48 +00003020 return bs->backing_file;
3021 else if (bs->encrypted)
3022 return bs->filename;
3023 else
3024 return NULL;
3025}
3026
ths5fafdf22007-09-16 21:08:06 +00003027void bdrv_get_backing_filename(BlockDriverState *bs,
bellard83f64092006-08-01 16:21:11 +00003028 char *filename, int filename_size)
bellardea2384d2004-08-01 21:59:26 +00003029{
Kevin Wolf3574c602011-10-26 11:02:11 +02003030 pstrcpy(filename, filename_size, bs->backing_file);
bellardea2384d2004-08-01 21:59:26 +00003031}
3032
bellardfaea38e2006-08-05 21:31:00 +00003033int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
3034{
3035 BlockDriver *drv = bs->drv;
3036 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00003037 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00003038 if (!drv->bdrv_get_info)
3039 return -ENOTSUP;
3040 memset(bdi, 0, sizeof(*bdi));
3041 return drv->bdrv_get_info(bs, bdi);
3042}
3043
Max Reitzeae041f2013-10-09 10:46:16 +02003044ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs)
3045{
3046 BlockDriver *drv = bs->drv;
3047 if (drv && drv->bdrv_get_specific_info) {
3048 return drv->bdrv_get_specific_info(bs);
3049 }
3050 return NULL;
3051}
3052
Eric Blakea31939e2015-11-18 01:52:54 -07003053void bdrv_debug_event(BlockDriverState *bs, BlkdebugEvent event)
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01003054{
Kevin Wolfbf736fe2013-06-05 15:17:55 +02003055 if (!bs || !bs->drv || !bs->drv->bdrv_debug_event) {
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01003056 return;
3057 }
3058
Kevin Wolfbf736fe2013-06-05 15:17:55 +02003059 bs->drv->bdrv_debug_event(bs, event);
Kevin Wolf41c695c2012-12-06 14:32:58 +01003060}
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01003061
Kevin Wolf41c695c2012-12-06 14:32:58 +01003062int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event,
3063 const char *tag)
3064{
3065 while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003066 bs = bs->file ? bs->file->bs : NULL;
Kevin Wolf41c695c2012-12-06 14:32:58 +01003067 }
3068
3069 if (bs && bs->drv && bs->drv->bdrv_debug_breakpoint) {
3070 return bs->drv->bdrv_debug_breakpoint(bs, event, tag);
3071 }
3072
3073 return -ENOTSUP;
3074}
3075
Fam Zheng4cc70e92013-11-20 10:01:54 +08003076int bdrv_debug_remove_breakpoint(BlockDriverState *bs, const char *tag)
3077{
3078 while (bs && bs->drv && !bs->drv->bdrv_debug_remove_breakpoint) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003079 bs = bs->file ? bs->file->bs : NULL;
Fam Zheng4cc70e92013-11-20 10:01:54 +08003080 }
3081
3082 if (bs && bs->drv && bs->drv->bdrv_debug_remove_breakpoint) {
3083 return bs->drv->bdrv_debug_remove_breakpoint(bs, tag);
3084 }
3085
3086 return -ENOTSUP;
3087}
3088
Kevin Wolf41c695c2012-12-06 14:32:58 +01003089int bdrv_debug_resume(BlockDriverState *bs, const char *tag)
3090{
Max Reitz938789e2014-03-10 23:44:08 +01003091 while (bs && (!bs->drv || !bs->drv->bdrv_debug_resume)) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003092 bs = bs->file ? bs->file->bs : NULL;
Kevin Wolf41c695c2012-12-06 14:32:58 +01003093 }
3094
3095 if (bs && bs->drv && bs->drv->bdrv_debug_resume) {
3096 return bs->drv->bdrv_debug_resume(bs, tag);
3097 }
3098
3099 return -ENOTSUP;
3100}
3101
3102bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag)
3103{
3104 while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003105 bs = bs->file ? bs->file->bs : NULL;
Kevin Wolf41c695c2012-12-06 14:32:58 +01003106 }
3107
3108 if (bs && bs->drv && bs->drv->bdrv_debug_is_suspended) {
3109 return bs->drv->bdrv_debug_is_suspended(bs, tag);
3110 }
3111
3112 return false;
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01003113}
3114
Jeff Codyb1b1d782012-10-16 15:49:09 -04003115/* backing_file can either be relative, or absolute, or a protocol. If it is
3116 * relative, it must be relative to the chain. So, passing in bs->filename
3117 * from a BDS as backing_file should not be done, as that may be relative to
3118 * the CWD rather than the chain. */
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00003119BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs,
3120 const char *backing_file)
3121{
Jeff Codyb1b1d782012-10-16 15:49:09 -04003122 char *filename_full = NULL;
3123 char *backing_file_full = NULL;
3124 char *filename_tmp = NULL;
3125 int is_protocol = 0;
3126 BlockDriverState *curr_bs = NULL;
3127 BlockDriverState *retval = NULL;
3128
3129 if (!bs || !bs->drv || !backing_file) {
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00003130 return NULL;
3131 }
3132
Jeff Codyb1b1d782012-10-16 15:49:09 -04003133 filename_full = g_malloc(PATH_MAX);
3134 backing_file_full = g_malloc(PATH_MAX);
3135 filename_tmp = g_malloc(PATH_MAX);
3136
3137 is_protocol = path_has_protocol(backing_file);
3138
Kevin Wolf760e0062015-06-17 14:55:21 +02003139 for (curr_bs = bs; curr_bs->backing; curr_bs = curr_bs->backing->bs) {
Jeff Codyb1b1d782012-10-16 15:49:09 -04003140
3141 /* If either of the filename paths is actually a protocol, then
3142 * compare unmodified paths; otherwise make paths relative */
3143 if (is_protocol || path_has_protocol(curr_bs->backing_file)) {
3144 if (strcmp(backing_file, curr_bs->backing_file) == 0) {
Kevin Wolf760e0062015-06-17 14:55:21 +02003145 retval = curr_bs->backing->bs;
Jeff Codyb1b1d782012-10-16 15:49:09 -04003146 break;
3147 }
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00003148 } else {
Jeff Codyb1b1d782012-10-16 15:49:09 -04003149 /* If not an absolute filename path, make it relative to the current
3150 * image's filename path */
3151 path_combine(filename_tmp, PATH_MAX, curr_bs->filename,
3152 backing_file);
3153
3154 /* We are going to compare absolute pathnames */
3155 if (!realpath(filename_tmp, filename_full)) {
3156 continue;
3157 }
3158
3159 /* We need to make sure the backing filename we are comparing against
3160 * is relative to the current image filename (or absolute) */
3161 path_combine(filename_tmp, PATH_MAX, curr_bs->filename,
3162 curr_bs->backing_file);
3163
3164 if (!realpath(filename_tmp, backing_file_full)) {
3165 continue;
3166 }
3167
3168 if (strcmp(backing_file_full, filename_full) == 0) {
Kevin Wolf760e0062015-06-17 14:55:21 +02003169 retval = curr_bs->backing->bs;
Jeff Codyb1b1d782012-10-16 15:49:09 -04003170 break;
3171 }
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00003172 }
3173 }
3174
Jeff Codyb1b1d782012-10-16 15:49:09 -04003175 g_free(filename_full);
3176 g_free(backing_file_full);
3177 g_free(filename_tmp);
3178 return retval;
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00003179}
3180
Benoît Canetf198fd12012-08-02 10:22:47 +02003181int bdrv_get_backing_file_depth(BlockDriverState *bs)
3182{
3183 if (!bs->drv) {
3184 return 0;
3185 }
3186
Kevin Wolf760e0062015-06-17 14:55:21 +02003187 if (!bs->backing) {
Benoît Canetf198fd12012-08-02 10:22:47 +02003188 return 0;
3189 }
3190
Kevin Wolf760e0062015-06-17 14:55:21 +02003191 return 1 + bdrv_get_backing_file_depth(bs->backing->bs);
Benoît Canetf198fd12012-08-02 10:22:47 +02003192}
3193
bellardea2384d2004-08-01 21:59:26 +00003194void bdrv_init(void)
3195{
Anthony Liguori5efa9d52009-05-09 17:03:42 -05003196 module_call_init(MODULE_INIT_BLOCK);
bellardea2384d2004-08-01 21:59:26 +00003197}
pbrookce1a14d2006-08-07 02:38:06 +00003198
Markus Armbrustereb852012009-10-27 18:41:44 +01003199void bdrv_init_with_whitelist(void)
3200{
3201 use_bdrv_whitelist = 1;
3202 bdrv_init();
3203}
3204
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003205void bdrv_invalidate_cache(BlockDriverState *bs, Error **errp)
Anthony Liguori0f154232011-11-14 15:09:45 -06003206{
Fam Zheng0d1c5c92016-05-11 10:45:33 +08003207 BdrvChild *child;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003208 Error *local_err = NULL;
3209 int ret;
3210
Kevin Wolf3456a8d2014-03-11 10:58:39 +01003211 if (!bs->drv) {
3212 return;
Anthony Liguori0f154232011-11-14 15:09:45 -06003213 }
Kevin Wolf3456a8d2014-03-11 10:58:39 +01003214
Kevin Wolf04c01a52016-01-13 15:56:06 +01003215 if (!(bs->open_flags & BDRV_O_INACTIVE)) {
Alexey Kardashevskiy7ea2d262014-10-09 13:50:46 +11003216 return;
3217 }
Kevin Wolf04c01a52016-01-13 15:56:06 +01003218 bs->open_flags &= ~BDRV_O_INACTIVE;
Alexey Kardashevskiy7ea2d262014-10-09 13:50:46 +11003219
Kevin Wolf3456a8d2014-03-11 10:58:39 +01003220 if (bs->drv->bdrv_invalidate_cache) {
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003221 bs->drv->bdrv_invalidate_cache(bs, &local_err);
Fam Zheng0d1c5c92016-05-11 10:45:33 +08003222 if (local_err) {
3223 bs->open_flags |= BDRV_O_INACTIVE;
3224 error_propagate(errp, local_err);
3225 return;
3226 }
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003227 }
Fam Zheng0d1c5c92016-05-11 10:45:33 +08003228
3229 QLIST_FOREACH(child, &bs->children, next) {
3230 bdrv_invalidate_cache(child->bs, &local_err);
3231 if (local_err) {
3232 bs->open_flags |= BDRV_O_INACTIVE;
3233 error_propagate(errp, local_err);
3234 return;
3235 }
Kevin Wolf3456a8d2014-03-11 10:58:39 +01003236 }
3237
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003238 ret = refresh_total_sectors(bs, bs->total_sectors);
3239 if (ret < 0) {
Kevin Wolf04c01a52016-01-13 15:56:06 +01003240 bs->open_flags |= BDRV_O_INACTIVE;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003241 error_setg_errno(errp, -ret, "Could not refresh total sector count");
3242 return;
3243 }
Anthony Liguori0f154232011-11-14 15:09:45 -06003244}
3245
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003246void bdrv_invalidate_cache_all(Error **errp)
Anthony Liguori0f154232011-11-14 15:09:45 -06003247{
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003248 BlockDriverState *bs;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003249 Error *local_err = NULL;
Kevin Wolf88be7b42016-05-20 18:49:07 +02003250 BdrvNextIterator it;
Anthony Liguori0f154232011-11-14 15:09:45 -06003251
Kevin Wolf88be7b42016-05-20 18:49:07 +02003252 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Stefan Hajnoczied78cda2014-05-08 16:34:35 +02003253 AioContext *aio_context = bdrv_get_aio_context(bs);
3254
3255 aio_context_acquire(aio_context);
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003256 bdrv_invalidate_cache(bs, &local_err);
Stefan Hajnoczied78cda2014-05-08 16:34:35 +02003257 aio_context_release(aio_context);
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003258 if (local_err) {
3259 error_propagate(errp, local_err);
3260 return;
3261 }
Anthony Liguori0f154232011-11-14 15:09:45 -06003262 }
3263}
3264
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003265static int bdrv_inactivate_recurse(BlockDriverState *bs,
3266 bool setting_flag)
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003267{
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003268 BdrvChild *child;
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003269 int ret;
3270
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003271 if (!setting_flag && bs->drv->bdrv_inactivate) {
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003272 ret = bs->drv->bdrv_inactivate(bs);
3273 if (ret < 0) {
3274 return ret;
3275 }
3276 }
3277
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003278 QLIST_FOREACH(child, &bs->children, next) {
3279 ret = bdrv_inactivate_recurse(child->bs, setting_flag);
3280 if (ret < 0) {
3281 return ret;
3282 }
3283 }
3284
3285 if (setting_flag) {
3286 bs->open_flags |= BDRV_O_INACTIVE;
3287 }
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003288 return 0;
3289}
3290
3291int bdrv_inactivate_all(void)
3292{
Max Reitz79720af2016-03-16 19:54:44 +01003293 BlockDriverState *bs = NULL;
Kevin Wolf88be7b42016-05-20 18:49:07 +02003294 BdrvNextIterator it;
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003295 int ret = 0;
3296 int pass;
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003297
Kevin Wolf88be7b42016-05-20 18:49:07 +02003298 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003299 aio_context_acquire(bdrv_get_aio_context(bs));
3300 }
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003301
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003302 /* We do two passes of inactivation. The first pass calls to drivers'
3303 * .bdrv_inactivate callbacks recursively so all cache is flushed to disk;
3304 * the second pass sets the BDRV_O_INACTIVE flag so that no further write
3305 * is allowed. */
3306 for (pass = 0; pass < 2; pass++) {
Kevin Wolf88be7b42016-05-20 18:49:07 +02003307 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003308 ret = bdrv_inactivate_recurse(bs, pass);
3309 if (ret < 0) {
3310 goto out;
3311 }
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003312 }
3313 }
3314
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003315out:
Kevin Wolf88be7b42016-05-20 18:49:07 +02003316 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003317 aio_context_release(bdrv_get_aio_context(bs));
3318 }
3319
3320 return ret;
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003321}
3322
Kevin Wolff9f05dc2011-07-15 13:50:26 +02003323/**************************************************************/
bellard19cb3732006-08-19 11:45:59 +00003324/* removable device support */
3325
3326/**
3327 * Return TRUE if the media is present
3328 */
Max Reitze031f752015-10-19 17:53:11 +02003329bool bdrv_is_inserted(BlockDriverState *bs)
bellard19cb3732006-08-19 11:45:59 +00003330{
3331 BlockDriver *drv = bs->drv;
Max Reitz28d7a782015-10-19 17:53:13 +02003332 BdrvChild *child;
Markus Armbrustera1aff5b2011-09-06 18:58:41 +02003333
Max Reitze031f752015-10-19 17:53:11 +02003334 if (!drv) {
3335 return false;
3336 }
Max Reitz28d7a782015-10-19 17:53:13 +02003337 if (drv->bdrv_is_inserted) {
3338 return drv->bdrv_is_inserted(bs);
Max Reitze031f752015-10-19 17:53:11 +02003339 }
Max Reitz28d7a782015-10-19 17:53:13 +02003340 QLIST_FOREACH(child, &bs->children, next) {
3341 if (!bdrv_is_inserted(child->bs)) {
3342 return false;
3343 }
3344 }
3345 return true;
bellard19cb3732006-08-19 11:45:59 +00003346}
3347
3348/**
Markus Armbruster8e49ca42011-08-03 15:08:08 +02003349 * Return whether the media changed since the last call to this
3350 * function, or -ENOTSUP if we don't know. Most drivers don't know.
bellard19cb3732006-08-19 11:45:59 +00003351 */
3352int bdrv_media_changed(BlockDriverState *bs)
3353{
3354 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +00003355
Markus Armbruster8e49ca42011-08-03 15:08:08 +02003356 if (drv && drv->bdrv_media_changed) {
3357 return drv->bdrv_media_changed(bs);
3358 }
3359 return -ENOTSUP;
bellard19cb3732006-08-19 11:45:59 +00003360}
3361
3362/**
3363 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
3364 */
Luiz Capitulinof36f3942012-02-03 16:24:53 -02003365void bdrv_eject(BlockDriverState *bs, bool eject_flag)
bellard19cb3732006-08-19 11:45:59 +00003366{
3367 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +00003368
Markus Armbruster822e1cd2011-07-20 18:23:42 +02003369 if (drv && drv->bdrv_eject) {
3370 drv->bdrv_eject(bs, eject_flag);
bellard19cb3732006-08-19 11:45:59 +00003371 }
bellard19cb3732006-08-19 11:45:59 +00003372}
3373
bellard19cb3732006-08-19 11:45:59 +00003374/**
3375 * Lock or unlock the media (if it is locked, the user won't be able
3376 * to eject it manually).
3377 */
Markus Armbruster025e8492011-09-06 18:58:47 +02003378void bdrv_lock_medium(BlockDriverState *bs, bool locked)
bellard19cb3732006-08-19 11:45:59 +00003379{
3380 BlockDriver *drv = bs->drv;
3381
Markus Armbruster025e8492011-09-06 18:58:47 +02003382 trace_bdrv_lock_medium(bs, locked);
Stefan Hajnoczib8c6d092011-03-29 20:04:40 +01003383
Markus Armbruster025e8492011-09-06 18:58:47 +02003384 if (drv && drv->bdrv_lock_medium) {
3385 drv->bdrv_lock_medium(bs, locked);
bellard19cb3732006-08-19 11:45:59 +00003386 }
3387}
ths985a03b2007-12-24 16:10:43 +00003388
Fam Zheng9fcb0252013-08-23 09:14:46 +08003389/* Get a reference to bs */
3390void bdrv_ref(BlockDriverState *bs)
3391{
3392 bs->refcnt++;
3393}
3394
3395/* Release a previously grabbed reference to bs.
3396 * If after releasing, reference count is zero, the BlockDriverState is
3397 * deleted. */
3398void bdrv_unref(BlockDriverState *bs)
3399{
Jeff Cody9a4d5ca2014-07-23 17:22:57 -04003400 if (!bs) {
3401 return;
3402 }
Fam Zheng9fcb0252013-08-23 09:14:46 +08003403 assert(bs->refcnt > 0);
3404 if (--bs->refcnt == 0) {
3405 bdrv_delete(bs);
3406 }
3407}
3408
Fam Zhengfbe40ff2014-05-23 21:29:42 +08003409struct BdrvOpBlocker {
3410 Error *reason;
3411 QLIST_ENTRY(BdrvOpBlocker) list;
3412};
3413
3414bool bdrv_op_is_blocked(BlockDriverState *bs, BlockOpType op, Error **errp)
3415{
3416 BdrvOpBlocker *blocker;
3417 assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
3418 if (!QLIST_EMPTY(&bs->op_blockers[op])) {
3419 blocker = QLIST_FIRST(&bs->op_blockers[op]);
3420 if (errp) {
Markus Armbrustere43bfd92015-12-18 16:35:15 +01003421 *errp = error_copy(blocker->reason);
3422 error_prepend(errp, "Node '%s' is busy: ",
3423 bdrv_get_device_or_node_name(bs));
Fam Zhengfbe40ff2014-05-23 21:29:42 +08003424 }
3425 return true;
3426 }
3427 return false;
3428}
3429
3430void bdrv_op_block(BlockDriverState *bs, BlockOpType op, Error *reason)
3431{
3432 BdrvOpBlocker *blocker;
3433 assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
3434
Markus Armbruster5839e532014-08-19 10:31:08 +02003435 blocker = g_new0(BdrvOpBlocker, 1);
Fam Zhengfbe40ff2014-05-23 21:29:42 +08003436 blocker->reason = reason;
3437 QLIST_INSERT_HEAD(&bs->op_blockers[op], blocker, list);
3438}
3439
3440void bdrv_op_unblock(BlockDriverState *bs, BlockOpType op, Error *reason)
3441{
3442 BdrvOpBlocker *blocker, *next;
3443 assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
3444 QLIST_FOREACH_SAFE(blocker, &bs->op_blockers[op], list, next) {
3445 if (blocker->reason == reason) {
3446 QLIST_REMOVE(blocker, list);
3447 g_free(blocker);
3448 }
3449 }
3450}
3451
3452void bdrv_op_block_all(BlockDriverState *bs, Error *reason)
3453{
3454 int i;
3455 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
3456 bdrv_op_block(bs, i, reason);
3457 }
3458}
3459
3460void bdrv_op_unblock_all(BlockDriverState *bs, Error *reason)
3461{
3462 int i;
3463 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
3464 bdrv_op_unblock(bs, i, reason);
3465 }
3466}
3467
3468bool bdrv_op_blocker_is_empty(BlockDriverState *bs)
3469{
3470 int i;
3471
3472 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
3473 if (!QLIST_EMPTY(&bs->op_blockers[i])) {
3474 return false;
3475 }
3476 }
3477 return true;
3478}
3479
Luiz Capitulinod92ada22012-11-30 10:52:09 -02003480void bdrv_img_create(const char *filename, const char *fmt,
3481 const char *base_filename, const char *base_fmt,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003482 char *options, uint64_t img_size, int flags,
3483 Error **errp, bool quiet)
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003484{
Chunyan Liu83d05212014-06-05 17:20:51 +08003485 QemuOptsList *create_opts = NULL;
3486 QemuOpts *opts = NULL;
3487 const char *backing_fmt, *backing_file;
3488 int64_t size;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003489 BlockDriver *drv, *proto_drv;
Max Reitzcc84d902013-09-06 17:14:26 +02003490 Error *local_err = NULL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003491 int ret = 0;
3492
3493 /* Find driver and parse its options */
3494 drv = bdrv_find_format(fmt);
3495 if (!drv) {
Luiz Capitulino71c79812012-11-30 10:52:04 -02003496 error_setg(errp, "Unknown file format '%s'", fmt);
Luiz Capitulinod92ada22012-11-30 10:52:09 -02003497 return;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003498 }
3499
Max Reitzb65a5e12015-02-05 13:58:12 -05003500 proto_drv = bdrv_find_protocol(filename, true, errp);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003501 if (!proto_drv) {
Luiz Capitulinod92ada22012-11-30 10:52:09 -02003502 return;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003503 }
3504
Max Reitzc6149722014-12-02 18:32:45 +01003505 if (!drv->create_opts) {
3506 error_setg(errp, "Format driver '%s' does not support image creation",
3507 drv->format_name);
3508 return;
3509 }
3510
3511 if (!proto_drv->create_opts) {
3512 error_setg(errp, "Protocol driver '%s' does not support image creation",
3513 proto_drv->format_name);
3514 return;
3515 }
3516
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003517 create_opts = qemu_opts_append(create_opts, drv->create_opts);
3518 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003519
3520 /* Create parameter list with default values */
Chunyan Liu83d05212014-06-05 17:20:51 +08003521 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbruster39101f22015-02-12 16:46:36 +01003522 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003523
3524 /* Parse -o options */
3525 if (options) {
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003526 qemu_opts_do_parse(opts, options, NULL, &local_err);
3527 if (local_err) {
3528 error_report_err(local_err);
3529 local_err = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003530 error_setg(errp, "Invalid options for file format '%s'", fmt);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003531 goto out;
3532 }
3533 }
3534
3535 if (base_filename) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003536 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &local_err);
Markus Armbruster6be41942015-02-12 17:49:02 +01003537 if (local_err) {
Luiz Capitulino71c79812012-11-30 10:52:04 -02003538 error_setg(errp, "Backing file not supported for file format '%s'",
3539 fmt);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003540 goto out;
3541 }
3542 }
3543
3544 if (base_fmt) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003545 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &local_err);
Markus Armbruster6be41942015-02-12 17:49:02 +01003546 if (local_err) {
Luiz Capitulino71c79812012-11-30 10:52:04 -02003547 error_setg(errp, "Backing file format not supported for file "
3548 "format '%s'", fmt);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003549 goto out;
3550 }
3551 }
3552
Chunyan Liu83d05212014-06-05 17:20:51 +08003553 backing_file = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
3554 if (backing_file) {
3555 if (!strcmp(filename, backing_file)) {
Luiz Capitulino71c79812012-11-30 10:52:04 -02003556 error_setg(errp, "Error: Trying to create an image with the "
3557 "same filename as the backing file");
Jes Sorensen792da932010-12-16 13:52:17 +01003558 goto out;
3559 }
3560 }
3561
Chunyan Liu83d05212014-06-05 17:20:51 +08003562 backing_fmt = qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003563
3564 // The size for the image must always be specified, with one exception:
3565 // If we are using a backing file, we can obtain the size from there
Chunyan Liu83d05212014-06-05 17:20:51 +08003566 size = qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 0);
3567 if (size == -1) {
3568 if (backing_file) {
Max Reitz66f6b812013-12-03 14:57:52 +01003569 BlockDriverState *bs;
Max Reitz29168012014-11-26 17:20:27 +01003570 char *full_backing = g_new0(char, PATH_MAX);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003571 int64_t size;
Paolo Bonzini63090da2012-04-12 14:01:03 +02003572 int back_flags;
Max Reitze6641712015-08-26 19:47:48 +02003573 QDict *backing_options = NULL;
Paolo Bonzini63090da2012-04-12 14:01:03 +02003574
Max Reitz29168012014-11-26 17:20:27 +01003575 bdrv_get_full_backing_filename_from_filename(filename, backing_file,
3576 full_backing, PATH_MAX,
3577 &local_err);
3578 if (local_err) {
3579 g_free(full_backing);
3580 goto out;
3581 }
3582
Paolo Bonzini63090da2012-04-12 14:01:03 +02003583 /* backing files always opened read-only */
Kevin Wolf61de4c62016-03-18 17:46:45 +01003584 back_flags = flags;
Kevin Wolfbfd18d12016-03-04 14:28:01 +01003585 back_flags &= ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003586
Max Reitze6641712015-08-26 19:47:48 +02003587 if (backing_fmt) {
3588 backing_options = qdict_new();
3589 qdict_put(backing_options, "driver",
3590 qstring_from_str(backing_fmt));
3591 }
3592
Max Reitz5b363932016-05-17 16:41:31 +02003593 bs = bdrv_open(full_backing, NULL, backing_options, back_flags,
3594 &local_err);
Max Reitz29168012014-11-26 17:20:27 +01003595 g_free(full_backing);
Max Reitz5b363932016-05-17 16:41:31 +02003596 if (!bs) {
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003597 goto out;
3598 }
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003599 size = bdrv_getlength(bs);
3600 if (size < 0) {
3601 error_setg_errno(errp, -size, "Could not get size of '%s'",
3602 backing_file);
3603 bdrv_unref(bs);
3604 goto out;
3605 }
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003606
Markus Armbruster39101f22015-02-12 16:46:36 +01003607 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, size, &error_abort);
Max Reitz66f6b812013-12-03 14:57:52 +01003608
3609 bdrv_unref(bs);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003610 } else {
Luiz Capitulino71c79812012-11-30 10:52:04 -02003611 error_setg(errp, "Image creation needs a size parameter");
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003612 goto out;
3613 }
3614 }
3615
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003616 if (!quiet) {
Kővágó, Zoltánfe646692015-07-07 16:42:10 +02003617 printf("Formatting '%s', fmt=%s ", filename, fmt);
Fam Zheng43c5d8f2014-12-09 15:38:04 +08003618 qemu_opts_print(opts, " ");
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003619 puts("");
3620 }
Chunyan Liu83d05212014-06-05 17:20:51 +08003621
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003622 ret = bdrv_create(drv, filename, opts, &local_err);
Chunyan Liu83d05212014-06-05 17:20:51 +08003623
Max Reitzcc84d902013-09-06 17:14:26 +02003624 if (ret == -EFBIG) {
3625 /* This is generally a better message than whatever the driver would
3626 * deliver (especially because of the cluster_size_hint), since that
3627 * is most probably not much different from "image too large". */
3628 const char *cluster_size_hint = "";
Chunyan Liu83d05212014-06-05 17:20:51 +08003629 if (qemu_opt_get_size(opts, BLOCK_OPT_CLUSTER_SIZE, 0)) {
Max Reitzcc84d902013-09-06 17:14:26 +02003630 cluster_size_hint = " (try using a larger cluster size)";
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003631 }
Max Reitzcc84d902013-09-06 17:14:26 +02003632 error_setg(errp, "The image size is too large for file format '%s'"
3633 "%s", fmt, cluster_size_hint);
3634 error_free(local_err);
3635 local_err = NULL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003636 }
3637
3638out:
Chunyan Liu83d05212014-06-05 17:20:51 +08003639 qemu_opts_del(opts);
3640 qemu_opts_free(create_opts);
Eduardo Habkost621ff942016-06-13 18:57:56 -03003641 error_propagate(errp, local_err);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003642}
Stefan Hajnoczi85d126f2013-03-07 13:41:48 +01003643
3644AioContext *bdrv_get_aio_context(BlockDriverState *bs)
3645{
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003646 return bs->aio_context;
3647}
3648
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003649static void bdrv_do_remove_aio_context_notifier(BdrvAioNotifier *ban)
3650{
3651 QLIST_REMOVE(ban, list);
3652 g_free(ban);
3653}
3654
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003655void bdrv_detach_aio_context(BlockDriverState *bs)
3656{
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003657 BdrvAioNotifier *baf, *baf_tmp;
Max Reitzb97511c2016-05-17 13:38:04 +02003658 BdrvChild *child;
Max Reitz33384422014-06-20 21:57:33 +02003659
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003660 if (!bs->drv) {
3661 return;
3662 }
3663
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003664 assert(!bs->walking_aio_notifiers);
3665 bs->walking_aio_notifiers = true;
3666 QLIST_FOREACH_SAFE(baf, &bs->aio_notifiers, list, baf_tmp) {
3667 if (baf->deleted) {
3668 bdrv_do_remove_aio_context_notifier(baf);
3669 } else {
3670 baf->detach_aio_context(baf->opaque);
3671 }
Max Reitz33384422014-06-20 21:57:33 +02003672 }
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003673 /* Never mind iterating again to check for ->deleted. bdrv_close() will
3674 * remove remaining aio notifiers if we aren't called again.
3675 */
3676 bs->walking_aio_notifiers = false;
Max Reitz33384422014-06-20 21:57:33 +02003677
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003678 if (bs->drv->bdrv_detach_aio_context) {
3679 bs->drv->bdrv_detach_aio_context(bs);
3680 }
Max Reitzb97511c2016-05-17 13:38:04 +02003681 QLIST_FOREACH(child, &bs->children, next) {
3682 bdrv_detach_aio_context(child->bs);
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003683 }
3684
3685 bs->aio_context = NULL;
3686}
3687
3688void bdrv_attach_aio_context(BlockDriverState *bs,
3689 AioContext *new_context)
3690{
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003691 BdrvAioNotifier *ban, *ban_tmp;
Max Reitzb97511c2016-05-17 13:38:04 +02003692 BdrvChild *child;
Max Reitz33384422014-06-20 21:57:33 +02003693
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003694 if (!bs->drv) {
3695 return;
3696 }
3697
3698 bs->aio_context = new_context;
3699
Max Reitzb97511c2016-05-17 13:38:04 +02003700 QLIST_FOREACH(child, &bs->children, next) {
3701 bdrv_attach_aio_context(child->bs, new_context);
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003702 }
3703 if (bs->drv->bdrv_attach_aio_context) {
3704 bs->drv->bdrv_attach_aio_context(bs, new_context);
3705 }
Max Reitz33384422014-06-20 21:57:33 +02003706
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003707 assert(!bs->walking_aio_notifiers);
3708 bs->walking_aio_notifiers = true;
3709 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_tmp) {
3710 if (ban->deleted) {
3711 bdrv_do_remove_aio_context_notifier(ban);
3712 } else {
3713 ban->attached_aio_context(new_context, ban->opaque);
3714 }
Max Reitz33384422014-06-20 21:57:33 +02003715 }
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003716 bs->walking_aio_notifiers = false;
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003717}
3718
3719void bdrv_set_aio_context(BlockDriverState *bs, AioContext *new_context)
3720{
Fam Zheng53ec73e2015-05-29 18:53:14 +08003721 bdrv_drain(bs); /* ensure there are no in-flight requests */
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003722
3723 bdrv_detach_aio_context(bs);
3724
3725 /* This function executes in the old AioContext so acquire the new one in
3726 * case it runs in a different thread.
3727 */
3728 aio_context_acquire(new_context);
3729 bdrv_attach_aio_context(bs, new_context);
3730 aio_context_release(new_context);
Stefan Hajnoczi85d126f2013-03-07 13:41:48 +01003731}
Stefan Hajnoczid616b222013-06-24 17:13:10 +02003732
Max Reitz33384422014-06-20 21:57:33 +02003733void bdrv_add_aio_context_notifier(BlockDriverState *bs,
3734 void (*attached_aio_context)(AioContext *new_context, void *opaque),
3735 void (*detach_aio_context)(void *opaque), void *opaque)
3736{
3737 BdrvAioNotifier *ban = g_new(BdrvAioNotifier, 1);
3738 *ban = (BdrvAioNotifier){
3739 .attached_aio_context = attached_aio_context,
3740 .detach_aio_context = detach_aio_context,
3741 .opaque = opaque
3742 };
3743
3744 QLIST_INSERT_HEAD(&bs->aio_notifiers, ban, list);
3745}
3746
3747void bdrv_remove_aio_context_notifier(BlockDriverState *bs,
3748 void (*attached_aio_context)(AioContext *,
3749 void *),
3750 void (*detach_aio_context)(void *),
3751 void *opaque)
3752{
3753 BdrvAioNotifier *ban, *ban_next;
3754
3755 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) {
3756 if (ban->attached_aio_context == attached_aio_context &&
3757 ban->detach_aio_context == detach_aio_context &&
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003758 ban->opaque == opaque &&
3759 ban->deleted == false)
Max Reitz33384422014-06-20 21:57:33 +02003760 {
Stefan Hajnoczie8a095d2016-06-16 17:56:26 +01003761 if (bs->walking_aio_notifiers) {
3762 ban->deleted = true;
3763 } else {
3764 bdrv_do_remove_aio_context_notifier(ban);
3765 }
Max Reitz33384422014-06-20 21:57:33 +02003766 return;
3767 }
3768 }
3769
3770 abort();
3771}
3772
Max Reitz77485432014-10-27 11:12:50 +01003773int bdrv_amend_options(BlockDriverState *bs, QemuOpts *opts,
Max Reitz8b139762015-07-27 17:51:32 +02003774 BlockDriverAmendStatusCB *status_cb, void *cb_opaque)
Max Reitz6f176b42013-09-03 10:09:50 +02003775{
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003776 if (!bs->drv->bdrv_amend_options) {
Max Reitz6f176b42013-09-03 10:09:50 +02003777 return -ENOTSUP;
3778 }
Max Reitz8b139762015-07-27 17:51:32 +02003779 return bs->drv->bdrv_amend_options(bs, opts, status_cb, cb_opaque);
Max Reitz6f176b42013-09-03 10:09:50 +02003780}
Benoît Canetf6186f42013-10-02 14:33:48 +02003781
Benoît Canetb5042a32014-03-03 19:11:34 +01003782/* This function will be called by the bdrv_recurse_is_first_non_filter method
3783 * of block filter and by bdrv_is_first_non_filter.
3784 * It is used to test if the given bs is the candidate or recurse more in the
3785 * node graph.
Benoît Canet212a5a82014-01-23 21:31:36 +01003786 */
Benoît Canet212a5a82014-01-23 21:31:36 +01003787bool bdrv_recurse_is_first_non_filter(BlockDriverState *bs,
3788 BlockDriverState *candidate)
Benoît Canetf6186f42013-10-02 14:33:48 +02003789{
Benoît Canetb5042a32014-03-03 19:11:34 +01003790 /* return false if basic checks fails */
3791 if (!bs || !bs->drv) {
3792 return false;
3793 }
3794
3795 /* the code reached a non block filter driver -> check if the bs is
3796 * the same as the candidate. It's the recursion termination condition.
3797 */
3798 if (!bs->drv->is_filter) {
3799 return bs == candidate;
3800 }
3801 /* Down this path the driver is a block filter driver */
3802
3803 /* If the block filter recursion method is defined use it to recurse down
3804 * the node graph.
3805 */
3806 if (bs->drv->bdrv_recurse_is_first_non_filter) {
Benoît Canet212a5a82014-01-23 21:31:36 +01003807 return bs->drv->bdrv_recurse_is_first_non_filter(bs, candidate);
3808 }
3809
Benoît Canetb5042a32014-03-03 19:11:34 +01003810 /* the driver is a block filter but don't allow to recurse -> return false
3811 */
3812 return false;
Benoît Canet212a5a82014-01-23 21:31:36 +01003813}
3814
3815/* This function checks if the candidate is the first non filter bs down it's
3816 * bs chain. Since we don't have pointers to parents it explore all bs chains
3817 * from the top. Some filters can choose not to pass down the recursion.
3818 */
3819bool bdrv_is_first_non_filter(BlockDriverState *candidate)
3820{
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003821 BlockDriverState *bs;
Kevin Wolf88be7b42016-05-20 18:49:07 +02003822 BdrvNextIterator it;
Benoît Canet212a5a82014-01-23 21:31:36 +01003823
3824 /* walk down the bs forest recursively */
Kevin Wolf88be7b42016-05-20 18:49:07 +02003825 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Benoît Canet212a5a82014-01-23 21:31:36 +01003826 bool perm;
3827
Benoît Canetb5042a32014-03-03 19:11:34 +01003828 /* try to recurse in this top level bs */
Kevin Wolfe6dc8a12014-02-04 11:45:31 +01003829 perm = bdrv_recurse_is_first_non_filter(bs, candidate);
Benoît Canet212a5a82014-01-23 21:31:36 +01003830
3831 /* candidate is the first non filter */
3832 if (perm) {
3833 return true;
3834 }
3835 }
3836
3837 return false;
Benoît Canetf6186f42013-10-02 14:33:48 +02003838}
Benoît Canet09158f02014-06-27 18:25:25 +02003839
Wen Congyange12f3782015-07-17 10:12:22 +08003840BlockDriverState *check_to_replace_node(BlockDriverState *parent_bs,
3841 const char *node_name, Error **errp)
Benoît Canet09158f02014-06-27 18:25:25 +02003842{
3843 BlockDriverState *to_replace_bs = bdrv_find_node(node_name);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003844 AioContext *aio_context;
3845
Benoît Canet09158f02014-06-27 18:25:25 +02003846 if (!to_replace_bs) {
3847 error_setg(errp, "Node name '%s' not found", node_name);
3848 return NULL;
3849 }
3850
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003851 aio_context = bdrv_get_aio_context(to_replace_bs);
3852 aio_context_acquire(aio_context);
3853
Benoît Canet09158f02014-06-27 18:25:25 +02003854 if (bdrv_op_is_blocked(to_replace_bs, BLOCK_OP_TYPE_REPLACE, errp)) {
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003855 to_replace_bs = NULL;
3856 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003857 }
3858
3859 /* We don't want arbitrary node of the BDS chain to be replaced only the top
3860 * most non filter in order to prevent data corruption.
3861 * Another benefit is that this tests exclude backing files which are
3862 * blocked by the backing blockers.
3863 */
Wen Congyange12f3782015-07-17 10:12:22 +08003864 if (!bdrv_recurse_is_first_non_filter(parent_bs, to_replace_bs)) {
Benoît Canet09158f02014-06-27 18:25:25 +02003865 error_setg(errp, "Only top most non filter can be replaced");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003866 to_replace_bs = NULL;
3867 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003868 }
3869
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003870out:
3871 aio_context_release(aio_context);
Benoît Canet09158f02014-06-27 18:25:25 +02003872 return to_replace_bs;
3873}
Ming Lei448ad912014-07-04 18:04:33 +08003874
Max Reitz91af7012014-07-18 20:24:56 +02003875static bool append_open_options(QDict *d, BlockDriverState *bs)
3876{
3877 const QDictEntry *entry;
Kevin Wolf9e700c12015-04-24 15:20:28 +02003878 QemuOptDesc *desc;
Kevin Wolf260fecf2015-04-27 13:46:22 +02003879 BdrvChild *child;
Max Reitz91af7012014-07-18 20:24:56 +02003880 bool found_any = false;
Kevin Wolf260fecf2015-04-27 13:46:22 +02003881 const char *p;
Max Reitz91af7012014-07-18 20:24:56 +02003882
3883 for (entry = qdict_first(bs->options); entry;
3884 entry = qdict_next(bs->options, entry))
3885 {
Kevin Wolf260fecf2015-04-27 13:46:22 +02003886 /* Exclude options for children */
3887 QLIST_FOREACH(child, &bs->children, next) {
3888 if (strstart(qdict_entry_key(entry), child->name, &p)
3889 && (!*p || *p == '.'))
3890 {
3891 break;
3892 }
3893 }
3894 if (child) {
Kevin Wolf9e700c12015-04-24 15:20:28 +02003895 continue;
Max Reitz91af7012014-07-18 20:24:56 +02003896 }
Kevin Wolf9e700c12015-04-24 15:20:28 +02003897
3898 /* And exclude all non-driver-specific options */
3899 for (desc = bdrv_runtime_opts.desc; desc->name; desc++) {
3900 if (!strcmp(qdict_entry_key(entry), desc->name)) {
3901 break;
3902 }
3903 }
3904 if (desc->name) {
3905 continue;
3906 }
3907
3908 qobject_incref(qdict_entry_value(entry));
3909 qdict_put_obj(d, qdict_entry_key(entry), qdict_entry_value(entry));
3910 found_any = true;
Max Reitz91af7012014-07-18 20:24:56 +02003911 }
3912
3913 return found_any;
3914}
3915
3916/* Updates the following BDS fields:
3917 * - exact_filename: A filename which may be used for opening a block device
3918 * which (mostly) equals the given BDS (even without any
3919 * other options; so reading and writing must return the same
3920 * results, but caching etc. may be different)
3921 * - full_open_options: Options which, when given when opening a block device
3922 * (without a filename), result in a BDS (mostly)
3923 * equalling the given one
3924 * - filename: If exact_filename is set, it is copied here. Otherwise,
3925 * full_open_options is converted to a JSON object, prefixed with
3926 * "json:" (for use through the JSON pseudo protocol) and put here.
3927 */
3928void bdrv_refresh_filename(BlockDriverState *bs)
3929{
3930 BlockDriver *drv = bs->drv;
3931 QDict *opts;
3932
3933 if (!drv) {
3934 return;
3935 }
3936
3937 /* This BDS's file name will most probably depend on its file's name, so
3938 * refresh that first */
3939 if (bs->file) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003940 bdrv_refresh_filename(bs->file->bs);
Max Reitz91af7012014-07-18 20:24:56 +02003941 }
3942
3943 if (drv->bdrv_refresh_filename) {
3944 /* Obsolete information is of no use here, so drop the old file name
3945 * information before refreshing it */
3946 bs->exact_filename[0] = '\0';
3947 if (bs->full_open_options) {
3948 QDECREF(bs->full_open_options);
3949 bs->full_open_options = NULL;
3950 }
3951
Kevin Wolf4cdd01d2015-04-27 13:50:54 +02003952 opts = qdict_new();
3953 append_open_options(opts, bs);
3954 drv->bdrv_refresh_filename(bs, opts);
3955 QDECREF(opts);
Max Reitz91af7012014-07-18 20:24:56 +02003956 } else if (bs->file) {
3957 /* Try to reconstruct valid information from the underlying file */
3958 bool has_open_options;
3959
3960 bs->exact_filename[0] = '\0';
3961 if (bs->full_open_options) {
3962 QDECREF(bs->full_open_options);
3963 bs->full_open_options = NULL;
3964 }
3965
3966 opts = qdict_new();
3967 has_open_options = append_open_options(opts, bs);
3968
3969 /* If no specific options have been given for this BDS, the filename of
3970 * the underlying file should suffice for this one as well */
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003971 if (bs->file->bs->exact_filename[0] && !has_open_options) {
3972 strcpy(bs->exact_filename, bs->file->bs->exact_filename);
Max Reitz91af7012014-07-18 20:24:56 +02003973 }
3974 /* Reconstructing the full options QDict is simple for most format block
3975 * drivers, as long as the full options are known for the underlying
3976 * file BDS. The full options QDict of that file BDS should somehow
3977 * contain a representation of the filename, therefore the following
3978 * suffices without querying the (exact_)filename of this BDS. */
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003979 if (bs->file->bs->full_open_options) {
Max Reitz91af7012014-07-18 20:24:56 +02003980 qdict_put_obj(opts, "driver",
3981 QOBJECT(qstring_from_str(drv->format_name)));
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003982 QINCREF(bs->file->bs->full_open_options);
3983 qdict_put_obj(opts, "file",
3984 QOBJECT(bs->file->bs->full_open_options));
Max Reitz91af7012014-07-18 20:24:56 +02003985
3986 bs->full_open_options = opts;
3987 } else {
3988 QDECREF(opts);
3989 }
3990 } else if (!bs->full_open_options && qdict_size(bs->options)) {
3991 /* There is no underlying file BDS (at least referenced by BDS.file),
3992 * so the full options QDict should be equal to the options given
3993 * specifically for this block device when it was opened (plus the
3994 * driver specification).
3995 * Because those options don't change, there is no need to update
3996 * full_open_options when it's already set. */
3997
3998 opts = qdict_new();
3999 append_open_options(opts, bs);
4000 qdict_put_obj(opts, "driver",
4001 QOBJECT(qstring_from_str(drv->format_name)));
4002
4003 if (bs->exact_filename[0]) {
4004 /* This may not work for all block protocol drivers (some may
4005 * require this filename to be parsed), but we have to find some
4006 * default solution here, so just include it. If some block driver
4007 * does not support pure options without any filename at all or
4008 * needs some special format of the options QDict, it needs to
4009 * implement the driver-specific bdrv_refresh_filename() function.
4010 */
4011 qdict_put_obj(opts, "filename",
4012 QOBJECT(qstring_from_str(bs->exact_filename)));
4013 }
4014
4015 bs->full_open_options = opts;
4016 }
4017
4018 if (bs->exact_filename[0]) {
4019 pstrcpy(bs->filename, sizeof(bs->filename), bs->exact_filename);
4020 } else if (bs->full_open_options) {
4021 QString *json = qobject_to_json(QOBJECT(bs->full_open_options));
4022 snprintf(bs->filename, sizeof(bs->filename), "json:%s",
4023 qstring_get_str(json));
4024 QDECREF(json);
4025 }
4026}
Wen Congyange06018a2016-05-10 15:36:37 +08004027
4028/*
4029 * Hot add/remove a BDS's child. So the user can take a child offline when
4030 * it is broken and take a new child online
4031 */
4032void bdrv_add_child(BlockDriverState *parent_bs, BlockDriverState *child_bs,
4033 Error **errp)
4034{
4035
4036 if (!parent_bs->drv || !parent_bs->drv->bdrv_add_child) {
4037 error_setg(errp, "The node %s does not support adding a child",
4038 bdrv_get_device_or_node_name(parent_bs));
4039 return;
4040 }
4041
4042 if (!QLIST_EMPTY(&child_bs->parents)) {
4043 error_setg(errp, "The node %s already has a parent",
4044 child_bs->node_name);
4045 return;
4046 }
4047
4048 parent_bs->drv->bdrv_add_child(parent_bs, child_bs, errp);
4049}
4050
4051void bdrv_del_child(BlockDriverState *parent_bs, BdrvChild *child, Error **errp)
4052{
4053 BdrvChild *tmp;
4054
4055 if (!parent_bs->drv || !parent_bs->drv->bdrv_del_child) {
4056 error_setg(errp, "The node %s does not support removing a child",
4057 bdrv_get_device_or_node_name(parent_bs));
4058 return;
4059 }
4060
4061 QLIST_FOREACH(tmp, &parent_bs->children, next) {
4062 if (tmp == child) {
4063 break;
4064 }
4065 }
4066
4067 if (!tmp) {
4068 error_setg(errp, "The node %s does not have a child named %s",
4069 bdrv_get_device_or_node_name(parent_bs),
4070 bdrv_get_device_or_node_name(child->bs));
4071 return;
4072 }
4073
4074 parent_bs->drv->bdrv_del_child(parent_bs, child, errp);
4075}