blob: ad4be90598f48705b522334c08a432c22290bc1c [file] [log] [blame]
bellardfc01f7e2003-06-30 10:03:06 +00001/*
2 * QEMU System Emulator block driver
ths5fafdf22007-09-16 21:08:06 +00003 *
bellardfc01f7e2003-06-30 10:03:06 +00004 * Copyright (c) 2003 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellardfc01f7e2003-06-30 10:03:06 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
Peter Maydelld38ea872016-01-29 17:50:05 +000024#include "qemu/osdep.h"
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +010025#include "trace.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010026#include "block/block_int.h"
27#include "block/blockjob.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010028#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010029#include "qemu/module.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010030#include "qapi/qmp/qerror.h"
Kevin Wolf91a097e2015-05-08 17:49:53 +020031#include "qapi/qmp/qbool.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010032#include "qapi/qmp/qjson.h"
Markus Armbrusterbfb197e2014-10-07 13:59:11 +020033#include "sysemu/block-backend.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010034#include "sysemu/sysemu.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010035#include "qemu/notify.h"
Daniel P. Berrange10817bf2015-09-01 14:48:02 +010036#include "qemu/coroutine.h"
Benoît Canetc13163f2014-01-23 21:31:34 +010037#include "block/qapi.h"
Luiz Capitulinob2023812011-09-21 17:16:47 -030038#include "qmp-commands.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010039#include "qemu/timer.h"
Wenchao Xiaa5ee7bd2014-06-18 08:43:44 +020040#include "qapi-event.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020041#include "qemu/cutils.h"
42#include "qemu/id.h"
bellardfc01f7e2003-06-30 10:03:06 +000043
Juan Quintela71e72a12009-07-27 16:12:56 +020044#ifdef CONFIG_BSD
bellard7674e7b2005-04-26 21:59:26 +000045#include <sys/ioctl.h>
Blue Swirl72cf2d42009-09-12 07:36:22 +000046#include <sys/queue.h>
blueswir1c5e97232009-03-07 20:06:23 +000047#ifndef __DragonFly__
bellard7674e7b2005-04-26 21:59:26 +000048#include <sys/disk.h>
49#endif
blueswir1c5e97232009-03-07 20:06:23 +000050#endif
bellard7674e7b2005-04-26 21:59:26 +000051
aliguori49dc7682009-03-08 16:26:59 +000052#ifdef _WIN32
53#include <windows.h>
54#endif
55
Stefan Hajnoczi1c9805a2011-10-13 13:08:22 +010056#define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
57
Benoît Canetdc364f42014-01-23 21:31:32 +010058static QTAILQ_HEAD(, BlockDriverState) graph_bdrv_states =
59 QTAILQ_HEAD_INITIALIZER(graph_bdrv_states);
60
Max Reitz2c1d04e2016-01-29 16:36:11 +010061static QTAILQ_HEAD(, BlockDriverState) all_bdrv_states =
62 QTAILQ_HEAD_INITIALIZER(all_bdrv_states);
63
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +010064static QLIST_HEAD(, BlockDriver) bdrv_drivers =
65 QLIST_HEAD_INITIALIZER(bdrv_drivers);
bellardea2384d2004-08-01 21:59:26 +000066
Max Reitz5b363932016-05-17 16:41:31 +020067static BlockDriverState *bdrv_open_inherit(const char *filename,
68 const char *reference,
69 QDict *options, int flags,
70 BlockDriverState *parent,
71 const BdrvChildRole *child_role,
72 Error **errp);
Kevin Wolff3930ed2015-04-08 13:43:47 +020073
Markus Armbrustereb852012009-10-27 18:41:44 +010074/* If non-zero, use only whitelisted block drivers */
75static int use_bdrv_whitelist;
76
Max Reitz64dff522016-01-29 16:36:10 +010077static void bdrv_close(BlockDriverState *bs);
78
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +000079#ifdef _WIN32
80static int is_windows_drive_prefix(const char *filename)
81{
82 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
83 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
84 filename[1] == ':');
85}
86
87int is_windows_drive(const char *filename)
88{
89 if (is_windows_drive_prefix(filename) &&
90 filename[2] == '\0')
91 return 1;
92 if (strstart(filename, "\\\\.\\", NULL) ||
93 strstart(filename, "//./", NULL))
94 return 1;
95 return 0;
96}
97#endif
98
Kevin Wolf339064d2013-11-28 10:23:32 +010099size_t bdrv_opt_mem_align(BlockDriverState *bs)
100{
101 if (!bs || !bs->drv) {
Denis V. Lunev459b4e62015-05-12 17:30:56 +0300102 /* page size or 4k (hdd sector size) should be on the safe side */
103 return MAX(4096, getpagesize());
Kevin Wolf339064d2013-11-28 10:23:32 +0100104 }
105
106 return bs->bl.opt_mem_alignment;
107}
108
Denis V. Lunev4196d2f2015-05-12 17:30:55 +0300109size_t bdrv_min_mem_align(BlockDriverState *bs)
110{
111 if (!bs || !bs->drv) {
Denis V. Lunev459b4e62015-05-12 17:30:56 +0300112 /* page size or 4k (hdd sector size) should be on the safe side */
113 return MAX(4096, getpagesize());
Denis V. Lunev4196d2f2015-05-12 17:30:55 +0300114 }
115
116 return bs->bl.min_mem_alignment;
117}
118
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000119/* check if the path starts with "<protocol>:" */
Max Reitz5c984152014-12-03 14:57:22 +0100120int path_has_protocol(const char *path)
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000121{
Paolo Bonzini947995c2012-05-08 16:51:48 +0200122 const char *p;
123
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000124#ifdef _WIN32
125 if (is_windows_drive(path) ||
126 is_windows_drive_prefix(path)) {
127 return 0;
128 }
Paolo Bonzini947995c2012-05-08 16:51:48 +0200129 p = path + strcspn(path, ":/\\");
130#else
131 p = path + strcspn(path, ":/");
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000132#endif
133
Paolo Bonzini947995c2012-05-08 16:51:48 +0200134 return *p == ':';
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000135}
136
bellard83f64092006-08-01 16:21:11 +0000137int path_is_absolute(const char *path)
138{
bellard21664422007-01-07 18:22:37 +0000139#ifdef _WIN32
140 /* specific case for names like: "\\.\d:" */
Paolo Bonzinif53f4da2012-05-08 16:51:47 +0200141 if (is_windows_drive(path) || is_windows_drive_prefix(path)) {
bellard21664422007-01-07 18:22:37 +0000142 return 1;
Paolo Bonzinif53f4da2012-05-08 16:51:47 +0200143 }
144 return (*path == '/' || *path == '\\');
bellard3b9f94e2007-01-07 17:27:07 +0000145#else
Paolo Bonzinif53f4da2012-05-08 16:51:47 +0200146 return (*path == '/');
bellard3b9f94e2007-01-07 17:27:07 +0000147#endif
bellard83f64092006-08-01 16:21:11 +0000148}
149
150/* if filename is absolute, just copy it to dest. Otherwise, build a
151 path to it by considering it is relative to base_path. URL are
152 supported. */
153void path_combine(char *dest, int dest_size,
154 const char *base_path,
155 const char *filename)
156{
157 const char *p, *p1;
158 int len;
159
160 if (dest_size <= 0)
161 return;
162 if (path_is_absolute(filename)) {
163 pstrcpy(dest, dest_size, filename);
164 } else {
165 p = strchr(base_path, ':');
166 if (p)
167 p++;
168 else
169 p = base_path;
bellard3b9f94e2007-01-07 17:27:07 +0000170 p1 = strrchr(base_path, '/');
171#ifdef _WIN32
172 {
173 const char *p2;
174 p2 = strrchr(base_path, '\\');
175 if (!p1 || p2 > p1)
176 p1 = p2;
177 }
178#endif
bellard83f64092006-08-01 16:21:11 +0000179 if (p1)
180 p1++;
181 else
182 p1 = base_path;
183 if (p1 > p)
184 p = p1;
185 len = p - base_path;
186 if (len > dest_size - 1)
187 len = dest_size - 1;
188 memcpy(dest, base_path, len);
189 dest[len] = '\0';
190 pstrcat(dest, dest_size, filename);
191 }
192}
193
Max Reitz0a828552014-11-26 17:20:25 +0100194void bdrv_get_full_backing_filename_from_filename(const char *backed,
195 const char *backing,
Max Reitz9f074292014-11-26 17:20:26 +0100196 char *dest, size_t sz,
197 Error **errp)
Max Reitz0a828552014-11-26 17:20:25 +0100198{
Max Reitz9f074292014-11-26 17:20:26 +0100199 if (backing[0] == '\0' || path_has_protocol(backing) ||
200 path_is_absolute(backing))
201 {
Max Reitz0a828552014-11-26 17:20:25 +0100202 pstrcpy(dest, sz, backing);
Max Reitz9f074292014-11-26 17:20:26 +0100203 } else if (backed[0] == '\0' || strstart(backed, "json:", NULL)) {
204 error_setg(errp, "Cannot use relative backing file names for '%s'",
205 backed);
Max Reitz0a828552014-11-26 17:20:25 +0100206 } else {
207 path_combine(dest, sz, backed, backing);
208 }
209}
210
Max Reitz9f074292014-11-26 17:20:26 +0100211void bdrv_get_full_backing_filename(BlockDriverState *bs, char *dest, size_t sz,
212 Error **errp)
Paolo Bonzinidc5a1372012-05-08 16:51:50 +0200213{
Max Reitz9f074292014-11-26 17:20:26 +0100214 char *backed = bs->exact_filename[0] ? bs->exact_filename : bs->filename;
215
216 bdrv_get_full_backing_filename_from_filename(backed, bs->backing_file,
217 dest, sz, errp);
Paolo Bonzinidc5a1372012-05-08 16:51:50 +0200218}
219
Stefan Hajnoczi0eb72172015-04-28 14:27:51 +0100220void bdrv_register(BlockDriver *bdrv)
221{
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100222 QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
bellardea2384d2004-08-01 21:59:26 +0000223}
bellardb3380822004-03-14 21:38:54 +0000224
Markus Armbrustere4e99862014-10-07 13:59:03 +0200225BlockDriverState *bdrv_new(void)
226{
227 BlockDriverState *bs;
228 int i;
229
Markus Armbruster5839e532014-08-19 10:31:08 +0200230 bs = g_new0(BlockDriverState, 1);
Fam Zhenge4654d22013-11-13 18:29:43 +0800231 QLIST_INIT(&bs->dirty_bitmaps);
Fam Zhengfbe40ff2014-05-23 21:29:42 +0800232 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
233 QLIST_INIT(&bs->op_blockers[i]);
234 }
Stefan Hajnoczid616b222013-06-24 17:13:10 +0200235 notifier_with_return_list_init(&bs->before_write_notifiers);
Fam Zheng9fcb0252013-08-23 09:14:46 +0800236 bs->refcnt = 1;
Stefan Hajnoczidcd04222014-05-08 16:34:37 +0200237 bs->aio_context = qemu_get_aio_context();
Paolo Bonzinid7d512f2012-08-23 11:20:36 +0200238
Max Reitz2c1d04e2016-01-29 16:36:11 +0100239 QTAILQ_INSERT_TAIL(&all_bdrv_states, bs, bs_list);
240
bellardb3380822004-03-14 21:38:54 +0000241 return bs;
242}
243
bellardea2384d2004-08-01 21:59:26 +0000244BlockDriver *bdrv_find_format(const char *format_name)
245{
246 BlockDriver *drv1;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100247 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
248 if (!strcmp(drv1->format_name, format_name)) {
bellardea2384d2004-08-01 21:59:26 +0000249 return drv1;
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100250 }
bellardea2384d2004-08-01 21:59:26 +0000251 }
252 return NULL;
253}
254
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800255static int bdrv_is_whitelisted(BlockDriver *drv, bool read_only)
Markus Armbrustereb852012009-10-27 18:41:44 +0100256{
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800257 static const char *whitelist_rw[] = {
258 CONFIG_BDRV_RW_WHITELIST
259 };
260 static const char *whitelist_ro[] = {
261 CONFIG_BDRV_RO_WHITELIST
Markus Armbrustereb852012009-10-27 18:41:44 +0100262 };
263 const char **p;
264
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800265 if (!whitelist_rw[0] && !whitelist_ro[0]) {
Markus Armbrustereb852012009-10-27 18:41:44 +0100266 return 1; /* no whitelist, anything goes */
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800267 }
Markus Armbrustereb852012009-10-27 18:41:44 +0100268
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800269 for (p = whitelist_rw; *p; p++) {
Markus Armbrustereb852012009-10-27 18:41:44 +0100270 if (!strcmp(drv->format_name, *p)) {
271 return 1;
272 }
273 }
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800274 if (read_only) {
275 for (p = whitelist_ro; *p; p++) {
276 if (!strcmp(drv->format_name, *p)) {
277 return 1;
278 }
279 }
280 }
Markus Armbrustereb852012009-10-27 18:41:44 +0100281 return 0;
282}
283
Daniel P. Berrangee6ff69b2016-03-21 14:11:48 +0000284bool bdrv_uses_whitelist(void)
285{
286 return use_bdrv_whitelist;
287}
288
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800289typedef struct CreateCo {
290 BlockDriver *drv;
291 char *filename;
Chunyan Liu83d05212014-06-05 17:20:51 +0800292 QemuOpts *opts;
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800293 int ret;
Max Reitzcc84d902013-09-06 17:14:26 +0200294 Error *err;
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800295} CreateCo;
296
297static void coroutine_fn bdrv_create_co_entry(void *opaque)
298{
Max Reitzcc84d902013-09-06 17:14:26 +0200299 Error *local_err = NULL;
300 int ret;
301
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800302 CreateCo *cco = opaque;
303 assert(cco->drv);
304
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800305 ret = cco->drv->bdrv_create(cco->filename, cco->opts, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100306 if (local_err) {
Max Reitzcc84d902013-09-06 17:14:26 +0200307 error_propagate(&cco->err, local_err);
308 }
309 cco->ret = ret;
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800310}
311
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200312int bdrv_create(BlockDriver *drv, const char* filename,
Chunyan Liu83d05212014-06-05 17:20:51 +0800313 QemuOpts *opts, Error **errp)
bellardea2384d2004-08-01 21:59:26 +0000314{
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800315 int ret;
Kevin Wolf0e7e1982009-05-18 16:42:10 +0200316
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800317 Coroutine *co;
318 CreateCo cco = {
319 .drv = drv,
320 .filename = g_strdup(filename),
Chunyan Liu83d05212014-06-05 17:20:51 +0800321 .opts = opts,
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800322 .ret = NOT_DONE,
Max Reitzcc84d902013-09-06 17:14:26 +0200323 .err = NULL,
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800324 };
325
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800326 if (!drv->bdrv_create) {
Max Reitzcc84d902013-09-06 17:14:26 +0200327 error_setg(errp, "Driver '%s' does not support image creation", drv->format_name);
Luiz Capitulino80168bf2012-10-17 16:45:25 -0300328 ret = -ENOTSUP;
329 goto out;
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800330 }
331
332 if (qemu_in_coroutine()) {
333 /* Fast-path if already in coroutine context */
334 bdrv_create_co_entry(&cco);
335 } else {
336 co = qemu_coroutine_create(bdrv_create_co_entry);
337 qemu_coroutine_enter(co, &cco);
338 while (cco.ret == NOT_DONE) {
Paolo Bonzinib47ec2c2014-07-07 15:18:01 +0200339 aio_poll(qemu_get_aio_context(), true);
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800340 }
341 }
342
343 ret = cco.ret;
Max Reitzcc84d902013-09-06 17:14:26 +0200344 if (ret < 0) {
Markus Armbruster84d18f02014-01-30 15:07:28 +0100345 if (cco.err) {
Max Reitzcc84d902013-09-06 17:14:26 +0200346 error_propagate(errp, cco.err);
347 } else {
348 error_setg_errno(errp, -ret, "Could not create image");
349 }
350 }
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800351
Luiz Capitulino80168bf2012-10-17 16:45:25 -0300352out:
353 g_free(cco.filename);
Zhi Yong Wu5b7e1542012-05-07 16:50:42 +0800354 return ret;
bellardea2384d2004-08-01 21:59:26 +0000355}
356
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800357int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp)
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200358{
359 BlockDriver *drv;
Max Reitzcc84d902013-09-06 17:14:26 +0200360 Error *local_err = NULL;
361 int ret;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200362
Max Reitzb65a5e12015-02-05 13:58:12 -0500363 drv = bdrv_find_protocol(filename, true, errp);
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200364 if (drv == NULL) {
Stefan Hajnoczi16905d72010-11-30 15:14:14 +0000365 return -ENOENT;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200366 }
367
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800368 ret = bdrv_create(drv, filename, opts, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100369 if (local_err) {
Max Reitzcc84d902013-09-06 17:14:26 +0200370 error_propagate(errp, local_err);
371 }
372 return ret;
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200373}
374
Ekaterina Tumanova892b7de2015-02-16 12:47:54 +0100375/**
376 * Try to get @bs's logical and physical block size.
377 * On success, store them in @bsz struct and return 0.
378 * On failure return -errno.
379 * @bs must not be empty.
380 */
381int bdrv_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz)
382{
383 BlockDriver *drv = bs->drv;
384
385 if (drv && drv->bdrv_probe_blocksizes) {
386 return drv->bdrv_probe_blocksizes(bs, bsz);
387 }
388
389 return -ENOTSUP;
390}
391
392/**
393 * Try to get @bs's geometry (cyls, heads, sectors).
394 * On success, store them in @geo struct and return 0.
395 * On failure return -errno.
396 * @bs must not be empty.
397 */
398int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo)
399{
400 BlockDriver *drv = bs->drv;
401
402 if (drv && drv->bdrv_probe_geometry) {
403 return drv->bdrv_probe_geometry(bs, geo);
404 }
405
406 return -ENOTSUP;
407}
408
Jim Meyeringeba25052012-05-28 09:27:54 +0200409/*
410 * Create a uniquely-named empty temporary file.
411 * Return 0 upon success, otherwise a negative errno value.
412 */
413int get_tmp_filename(char *filename, int size)
414{
bellardd5249392004-08-03 21:14:23 +0000415#ifdef _WIN32
bellard3b9f94e2007-01-07 17:27:07 +0000416 char temp_dir[MAX_PATH];
Jim Meyeringeba25052012-05-28 09:27:54 +0200417 /* GetTempFileName requires that its output buffer (4th param)
418 have length MAX_PATH or greater. */
419 assert(size >= MAX_PATH);
420 return (GetTempPath(MAX_PATH, temp_dir)
421 && GetTempFileName(temp_dir, "qem", 0, filename)
422 ? 0 : -GetLastError());
bellardd5249392004-08-03 21:14:23 +0000423#else
bellardea2384d2004-08-01 21:59:26 +0000424 int fd;
blueswir17ccfb2e2008-09-14 06:45:34 +0000425 const char *tmpdir;
aurel320badc1e2008-03-10 00:05:34 +0000426 tmpdir = getenv("TMPDIR");
Amit Shah69bef792014-02-26 15:12:37 +0530427 if (!tmpdir) {
428 tmpdir = "/var/tmp";
429 }
Jim Meyeringeba25052012-05-28 09:27:54 +0200430 if (snprintf(filename, size, "%s/vl.XXXXXX", tmpdir) >= size) {
431 return -EOVERFLOW;
432 }
bellardea2384d2004-08-01 21:59:26 +0000433 fd = mkstemp(filename);
Dunrong Huangfe235a02012-09-05 21:26:22 +0800434 if (fd < 0) {
435 return -errno;
436 }
437 if (close(fd) != 0) {
438 unlink(filename);
Jim Meyeringeba25052012-05-28 09:27:54 +0200439 return -errno;
440 }
441 return 0;
bellardd5249392004-08-03 21:14:23 +0000442#endif
Jim Meyeringeba25052012-05-28 09:27:54 +0200443}
bellardea2384d2004-08-01 21:59:26 +0000444
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200445/*
446 * Detect host devices. By convention, /dev/cdrom[N] is always
447 * recognized as a host CDROM.
448 */
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200449static BlockDriver *find_hdev_driver(const char *filename)
450{
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200451 int score_max = 0, score;
452 BlockDriver *drv = NULL, *d;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200453
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +0100454 QLIST_FOREACH(d, &bdrv_drivers, list) {
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200455 if (d->bdrv_probe_device) {
456 score = d->bdrv_probe_device(filename);
457 if (score > score_max) {
458 score_max = score;
459 drv = d;
460 }
461 }
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200462 }
463
Christoph Hellwig508c7cb2009-06-15 14:04:22 +0200464 return drv;
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200465}
Christoph Hellwigf3a5d3f2009-06-15 13:55:19 +0200466
Kevin Wolf98289622013-07-10 15:47:39 +0200467BlockDriver *bdrv_find_protocol(const char *filename,
Max Reitzb65a5e12015-02-05 13:58:12 -0500468 bool allow_protocol_prefix,
469 Error **errp)
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200470{
471 BlockDriver *drv1;
472 char protocol[128];
473 int len;
474 const char *p;
475
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200476 /* TODO Drivers without bdrv_file_open must be specified explicitly */
477
Christoph Hellwig39508e72010-06-23 12:25:17 +0200478 /*
479 * XXX(hch): we really should not let host device detection
480 * override an explicit protocol specification, but moving this
481 * later breaks access to device names with colons in them.
482 * Thanks to the brain-dead persistent naming schemes on udev-
483 * based Linux systems those actually are quite common.
484 */
485 drv1 = find_hdev_driver(filename);
486 if (drv1) {
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200487 return drv1;
488 }
Christoph Hellwig39508e72010-06-23 12:25:17 +0200489
Kevin Wolf98289622013-07-10 15:47:39 +0200490 if (!path_has_protocol(filename) || !allow_protocol_prefix) {
Max Reitzef810432014-12-02 18:32:42 +0100491 return &bdrv_file;
Christoph Hellwig39508e72010-06-23 12:25:17 +0200492 }
Kevin Wolf98289622013-07-10 15:47:39 +0200493
Stefan Hajnoczi9e0b22f2010-12-09 11:53:00 +0000494 p = strchr(filename, ':');
495 assert(p != NULL);
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200496 len = p - filename;
497 if (len > sizeof(protocol) - 1)
498 len = sizeof(protocol) - 1;
499 memcpy(protocol, filename, len);
500 protocol[len] = '\0';
501 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
502 if (drv1->protocol_name &&
503 !strcmp(drv1->protocol_name, protocol)) {
504 return drv1;
505 }
506 }
Max Reitzb65a5e12015-02-05 13:58:12 -0500507
508 error_setg(errp, "Unknown protocol '%s'", protocol);
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200509 return NULL;
510}
511
Markus Armbrusterc6684242014-11-20 16:27:10 +0100512/*
513 * Guess image format by probing its contents.
514 * This is not a good idea when your image is raw (CVE-2008-2004), but
515 * we do it anyway for backward compatibility.
516 *
517 * @buf contains the image's first @buf_size bytes.
Kevin Wolf7cddd372014-11-20 16:27:11 +0100518 * @buf_size is the buffer size in bytes (generally BLOCK_PROBE_BUF_SIZE,
519 * but can be smaller if the image file is smaller)
Markus Armbrusterc6684242014-11-20 16:27:10 +0100520 * @filename is its filename.
521 *
522 * For all block drivers, call the bdrv_probe() method to get its
523 * probing score.
524 * Return the first block driver with the highest probing score.
525 */
Kevin Wolf38f3ef52014-11-20 16:27:12 +0100526BlockDriver *bdrv_probe_all(const uint8_t *buf, int buf_size,
527 const char *filename)
Markus Armbrusterc6684242014-11-20 16:27:10 +0100528{
529 int score_max = 0, score;
530 BlockDriver *drv = NULL, *d;
531
532 QLIST_FOREACH(d, &bdrv_drivers, list) {
533 if (d->bdrv_probe) {
534 score = d->bdrv_probe(buf, buf_size, filename);
535 if (score > score_max) {
536 score_max = score;
537 drv = d;
538 }
539 }
540 }
541
542 return drv;
543}
544
Kevin Wolff500a6d2012-11-12 17:35:27 +0100545static int find_image_format(BlockDriverState *bs, const char *filename,
Max Reitz34b5d2c2013-09-05 14:45:29 +0200546 BlockDriver **pdrv, Error **errp)
bellardea2384d2004-08-01 21:59:26 +0000547{
Markus Armbrusterc6684242014-11-20 16:27:10 +0100548 BlockDriver *drv;
Kevin Wolf7cddd372014-11-20 16:27:11 +0100549 uint8_t buf[BLOCK_PROBE_BUF_SIZE];
Kevin Wolff500a6d2012-11-12 17:35:27 +0100550 int ret = 0;
Nicholas Bellingerf8ea0b02010-05-17 09:45:57 -0700551
Kevin Wolf08a00552010-06-01 18:37:31 +0200552 /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
Dimitris Aragiorgisb192af82015-06-23 13:44:56 +0300553 if (bdrv_is_sg(bs) || !bdrv_is_inserted(bs) || bdrv_getlength(bs) == 0) {
Max Reitzef810432014-12-02 18:32:42 +0100554 *pdrv = &bdrv_raw;
Stefan Weilc98ac352010-07-21 21:51:51 +0200555 return ret;
Nicholas A. Bellinger1a396852010-05-27 08:56:28 -0700556 }
Nicholas Bellingerf8ea0b02010-05-17 09:45:57 -0700557
bellard83f64092006-08-01 16:21:11 +0000558 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
bellard83f64092006-08-01 16:21:11 +0000559 if (ret < 0) {
Max Reitz34b5d2c2013-09-05 14:45:29 +0200560 error_setg_errno(errp, -ret, "Could not read image for determining its "
561 "format");
Stefan Weilc98ac352010-07-21 21:51:51 +0200562 *pdrv = NULL;
563 return ret;
bellard83f64092006-08-01 16:21:11 +0000564 }
565
Markus Armbrusterc6684242014-11-20 16:27:10 +0100566 drv = bdrv_probe_all(buf, ret, filename);
Stefan Weilc98ac352010-07-21 21:51:51 +0200567 if (!drv) {
Max Reitz34b5d2c2013-09-05 14:45:29 +0200568 error_setg(errp, "Could not determine image format: No compatible "
569 "driver found");
Stefan Weilc98ac352010-07-21 21:51:51 +0200570 ret = -ENOENT;
571 }
572 *pdrv = drv;
573 return ret;
bellardea2384d2004-08-01 21:59:26 +0000574}
575
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100576/**
577 * Set the current 'total_sectors' value
Markus Armbruster65a9bb22014-06-26 13:23:17 +0200578 * Return 0 on success, -errno on error.
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100579 */
580static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
581{
582 BlockDriver *drv = bs->drv;
583
Nicholas Bellinger396759a2010-05-17 09:46:04 -0700584 /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
Dimitris Aragiorgisb192af82015-06-23 13:44:56 +0300585 if (bdrv_is_sg(bs))
Nicholas Bellinger396759a2010-05-17 09:46:04 -0700586 return 0;
587
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100588 /* query actual device if possible, otherwise just trust the hint */
589 if (drv->bdrv_getlength) {
590 int64_t length = drv->bdrv_getlength(bs);
591 if (length < 0) {
592 return length;
593 }
Fam Zheng7e382002013-11-06 19:48:06 +0800594 hint = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE);
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100595 }
596
597 bs->total_sectors = hint;
598 return 0;
599}
600
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100601/**
Kevin Wolfcddff5b2015-11-16 16:43:27 +0100602 * Combines a QDict of new block driver @options with any missing options taken
603 * from @old_options, so that leaving out an option defaults to its old value.
604 */
605static void bdrv_join_options(BlockDriverState *bs, QDict *options,
606 QDict *old_options)
607{
608 if (bs->drv && bs->drv->bdrv_join_options) {
609 bs->drv->bdrv_join_options(options, old_options);
610 } else {
611 qdict_join(options, old_options, false);
612 }
613}
614
615/**
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100616 * Set open flags for a given discard mode
617 *
618 * Return 0 on success, -1 if the discard mode was invalid.
619 */
620int bdrv_parse_discard_flags(const char *mode, int *flags)
621{
622 *flags &= ~BDRV_O_UNMAP;
623
624 if (!strcmp(mode, "off") || !strcmp(mode, "ignore")) {
625 /* do nothing */
626 } else if (!strcmp(mode, "on") || !strcmp(mode, "unmap")) {
627 *flags |= BDRV_O_UNMAP;
628 } else {
629 return -1;
630 }
631
632 return 0;
633}
634
635/**
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100636 * Set open flags for a given cache mode
637 *
638 * Return 0 on success, -1 if the cache mode was invalid.
639 */
Kevin Wolf53e8ae02016-03-18 15:36:58 +0100640int bdrv_parse_cache_mode(const char *mode, int *flags, bool *writethrough)
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100641{
642 *flags &= ~BDRV_O_CACHE_MASK;
643
644 if (!strcmp(mode, "off") || !strcmp(mode, "none")) {
Kevin Wolf53e8ae02016-03-18 15:36:58 +0100645 *writethrough = false;
646 *flags |= BDRV_O_NOCACHE;
Stefan Hajnoczi92196b22011-08-04 12:26:52 +0100647 } else if (!strcmp(mode, "directsync")) {
Kevin Wolf53e8ae02016-03-18 15:36:58 +0100648 *writethrough = true;
Stefan Hajnoczi92196b22011-08-04 12:26:52 +0100649 *flags |= BDRV_O_NOCACHE;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100650 } else if (!strcmp(mode, "writeback")) {
Kevin Wolf53e8ae02016-03-18 15:36:58 +0100651 *writethrough = false;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100652 } else if (!strcmp(mode, "unsafe")) {
Kevin Wolf53e8ae02016-03-18 15:36:58 +0100653 *writethrough = false;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100654 *flags |= BDRV_O_NO_FLUSH;
655 } else if (!strcmp(mode, "writethrough")) {
Kevin Wolf53e8ae02016-03-18 15:36:58 +0100656 *writethrough = true;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100657 } else {
658 return -1;
659 }
660
661 return 0;
662}
663
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200664/*
Kevin Wolf73176be2016-03-07 13:02:15 +0100665 * Returns the options and flags that a temporary snapshot should get, based on
666 * the originally requested flags (the originally requested image will have
667 * flags like a backing file)
Kevin Wolfb1e6fc02014-05-06 12:11:42 +0200668 */
Kevin Wolf73176be2016-03-07 13:02:15 +0100669static void bdrv_temp_snapshot_options(int *child_flags, QDict *child_options,
670 int parent_flags, QDict *parent_options)
Kevin Wolfb1e6fc02014-05-06 12:11:42 +0200671{
Kevin Wolf73176be2016-03-07 13:02:15 +0100672 *child_flags = (parent_flags & ~BDRV_O_SNAPSHOT) | BDRV_O_TEMPORARY;
673
674 /* For temporary files, unconditional cache=unsafe is fine */
Kevin Wolf73176be2016-03-07 13:02:15 +0100675 qdict_set_default_str(child_options, BDRV_OPT_CACHE_DIRECT, "off");
676 qdict_set_default_str(child_options, BDRV_OPT_CACHE_NO_FLUSH, "on");
Kevin Wolfb1e6fc02014-05-06 12:11:42 +0200677}
678
679/*
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200680 * Returns the options and flags that bs->file should get if a protocol driver
681 * is expected, based on the given options and flags for the parent BDS
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200682 */
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200683static void bdrv_inherited_options(int *child_flags, QDict *child_options,
684 int parent_flags, QDict *parent_options)
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200685{
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200686 int flags = parent_flags;
687
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200688 /* Enable protocol handling, disable format probing for bs->file */
689 flags |= BDRV_O_PROTOCOL;
690
Kevin Wolf91a097e2015-05-08 17:49:53 +0200691 /* If the cache mode isn't explicitly set, inherit direct and no-flush from
692 * the parent. */
693 qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_DIRECT);
694 qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_NO_FLUSH);
695
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200696 /* Our block drivers take care to send flushes and respect unmap policy,
Kevin Wolf91a097e2015-05-08 17:49:53 +0200697 * so we can default to enable both on lower layers regardless of the
698 * corresponding parent options. */
Kevin Wolf91a097e2015-05-08 17:49:53 +0200699 flags |= BDRV_O_UNMAP;
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200700
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200701 /* Clear flags that only apply to the top layer */
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000702 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ |
703 BDRV_O_NO_IO);
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200704
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200705 *child_flags = flags;
Kevin Wolf0b50cc82014-04-11 21:29:52 +0200706}
707
Kevin Wolff3930ed2015-04-08 13:43:47 +0200708const BdrvChildRole child_file = {
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200709 .inherit_options = bdrv_inherited_options,
Kevin Wolff3930ed2015-04-08 13:43:47 +0200710};
711
712/*
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200713 * Returns the options and flags that bs->file should get if the use of formats
714 * (and not only protocols) is permitted for it, based on the given options and
715 * flags for the parent BDS
Kevin Wolff3930ed2015-04-08 13:43:47 +0200716 */
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200717static void bdrv_inherited_fmt_options(int *child_flags, QDict *child_options,
718 int parent_flags, QDict *parent_options)
Kevin Wolff3930ed2015-04-08 13:43:47 +0200719{
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200720 child_file.inherit_options(child_flags, child_options,
721 parent_flags, parent_options);
722
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000723 *child_flags &= ~(BDRV_O_PROTOCOL | BDRV_O_NO_IO);
Kevin Wolff3930ed2015-04-08 13:43:47 +0200724}
725
726const BdrvChildRole child_format = {
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200727 .inherit_options = bdrv_inherited_fmt_options,
Kevin Wolff3930ed2015-04-08 13:43:47 +0200728};
729
Kevin Wolf317fc442014-04-25 13:27:34 +0200730/*
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200731 * Returns the options and flags that bs->backing should get, based on the
732 * given options and flags for the parent BDS
Kevin Wolf317fc442014-04-25 13:27:34 +0200733 */
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200734static void bdrv_backing_options(int *child_flags, QDict *child_options,
735 int parent_flags, QDict *parent_options)
Kevin Wolf317fc442014-04-25 13:27:34 +0200736{
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200737 int flags = parent_flags;
738
Kevin Wolfb8816a42016-03-04 14:52:32 +0100739 /* The cache mode is inherited unmodified for backing files; except WCE,
740 * which is only applied on the top level (BlockBackend) */
Kevin Wolf91a097e2015-05-08 17:49:53 +0200741 qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_DIRECT);
742 qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_NO_FLUSH);
743
Kevin Wolf317fc442014-04-25 13:27:34 +0200744 /* backing files always opened read-only */
745 flags &= ~(BDRV_O_RDWR | BDRV_O_COPY_ON_READ);
746
747 /* snapshot=on is handled on the top layer */
Kevin Wolf8bfea152014-04-11 19:16:36 +0200748 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_TEMPORARY);
Kevin Wolf317fc442014-04-25 13:27:34 +0200749
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200750 *child_flags = flags;
Kevin Wolf317fc442014-04-25 13:27:34 +0200751}
752
Kevin Wolff3930ed2015-04-08 13:43:47 +0200753static const BdrvChildRole child_backing = {
Kevin Wolf8e2160e2015-04-29 17:29:39 +0200754 .inherit_options = bdrv_backing_options,
Kevin Wolff3930ed2015-04-08 13:43:47 +0200755};
756
Kevin Wolf7b272452012-11-12 17:05:39 +0100757static int bdrv_open_flags(BlockDriverState *bs, int flags)
758{
Kevin Wolf61de4c62016-03-18 17:46:45 +0100759 int open_flags = flags;
Kevin Wolf7b272452012-11-12 17:05:39 +0100760
761 /*
762 * Clear flags that are internal to the block layer before opening the
763 * image.
764 */
Kevin Wolf20cca272014-06-04 14:33:27 +0200765 open_flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_PROTOCOL);
Kevin Wolf7b272452012-11-12 17:05:39 +0100766
767 /*
768 * Snapshots should be writable.
769 */
Kevin Wolf8bfea152014-04-11 19:16:36 +0200770 if (flags & BDRV_O_TEMPORARY) {
Kevin Wolf7b272452012-11-12 17:05:39 +0100771 open_flags |= BDRV_O_RDWR;
772 }
773
774 return open_flags;
775}
776
Kevin Wolf91a097e2015-05-08 17:49:53 +0200777static void update_flags_from_options(int *flags, QemuOpts *opts)
778{
779 *flags &= ~BDRV_O_CACHE_MASK;
780
Kevin Wolf91a097e2015-05-08 17:49:53 +0200781 assert(qemu_opt_find(opts, BDRV_OPT_CACHE_NO_FLUSH));
782 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, false)) {
783 *flags |= BDRV_O_NO_FLUSH;
784 }
785
786 assert(qemu_opt_find(opts, BDRV_OPT_CACHE_DIRECT));
787 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_DIRECT, false)) {
788 *flags |= BDRV_O_NOCACHE;
789 }
790}
791
792static void update_options_from_flags(QDict *options, int flags)
793{
Kevin Wolf91a097e2015-05-08 17:49:53 +0200794 if (!qdict_haskey(options, BDRV_OPT_CACHE_DIRECT)) {
795 qdict_put(options, BDRV_OPT_CACHE_DIRECT,
796 qbool_from_bool(flags & BDRV_O_NOCACHE));
797 }
798 if (!qdict_haskey(options, BDRV_OPT_CACHE_NO_FLUSH)) {
799 qdict_put(options, BDRV_OPT_CACHE_NO_FLUSH,
800 qbool_from_bool(flags & BDRV_O_NO_FLUSH));
801 }
802}
803
Kevin Wolf636ea372014-01-24 14:11:52 +0100804static void bdrv_assign_node_name(BlockDriverState *bs,
805 const char *node_name,
806 Error **errp)
Benoît Canet6913c0c2014-01-23 21:31:33 +0100807{
Jeff Cody15489c72015-10-12 19:36:50 -0400808 char *gen_node_name = NULL;
Benoît Canet6913c0c2014-01-23 21:31:33 +0100809
Jeff Cody15489c72015-10-12 19:36:50 -0400810 if (!node_name) {
811 node_name = gen_node_name = id_generate(ID_BLOCK);
812 } else if (!id_wellformed(node_name)) {
813 /*
814 * Check for empty string or invalid characters, but not if it is
815 * generated (generated names use characters not available to the user)
816 */
Kevin Wolf9aebf3b2014-09-25 09:54:02 +0200817 error_setg(errp, "Invalid node name");
Kevin Wolf636ea372014-01-24 14:11:52 +0100818 return;
Benoît Canet6913c0c2014-01-23 21:31:33 +0100819 }
820
Benoît Canet0c5e94e2014-02-12 17:15:07 +0100821 /* takes care of avoiding namespaces collisions */
Markus Armbruster7f06d472014-10-07 13:59:12 +0200822 if (blk_by_name(node_name)) {
Benoît Canet0c5e94e2014-02-12 17:15:07 +0100823 error_setg(errp, "node-name=%s is conflicting with a device id",
824 node_name);
Jeff Cody15489c72015-10-12 19:36:50 -0400825 goto out;
Benoît Canet0c5e94e2014-02-12 17:15:07 +0100826 }
827
Benoît Canet6913c0c2014-01-23 21:31:33 +0100828 /* takes care of avoiding duplicates node names */
829 if (bdrv_find_node(node_name)) {
830 error_setg(errp, "Duplicate node name");
Jeff Cody15489c72015-10-12 19:36:50 -0400831 goto out;
Benoît Canet6913c0c2014-01-23 21:31:33 +0100832 }
833
834 /* copy node name into the bs and insert it into the graph list */
835 pstrcpy(bs->node_name, sizeof(bs->node_name), node_name);
836 QTAILQ_INSERT_TAIL(&graph_bdrv_states, bs, node_list);
Jeff Cody15489c72015-10-12 19:36:50 -0400837out:
838 g_free(gen_node_name);
Benoît Canet6913c0c2014-01-23 21:31:33 +0100839}
840
Kevin Wolf18edf282015-04-07 17:12:56 +0200841static QemuOptsList bdrv_runtime_opts = {
842 .name = "bdrv_common",
843 .head = QTAILQ_HEAD_INITIALIZER(bdrv_runtime_opts.head),
844 .desc = {
845 {
846 .name = "node-name",
847 .type = QEMU_OPT_STRING,
848 .help = "Node name of the block device node",
849 },
Kevin Wolf62392eb2015-04-24 16:38:02 +0200850 {
851 .name = "driver",
852 .type = QEMU_OPT_STRING,
853 .help = "Block driver to use for the node",
854 },
Kevin Wolf91a097e2015-05-08 17:49:53 +0200855 {
Kevin Wolf91a097e2015-05-08 17:49:53 +0200856 .name = BDRV_OPT_CACHE_DIRECT,
857 .type = QEMU_OPT_BOOL,
858 .help = "Bypass software writeback cache on the host",
859 },
860 {
861 .name = BDRV_OPT_CACHE_NO_FLUSH,
862 .type = QEMU_OPT_BOOL,
863 .help = "Ignore flush requests",
864 },
Kevin Wolf18edf282015-04-07 17:12:56 +0200865 { /* end of list */ }
866 },
867};
868
Kevin Wolfb6ce07a2010-04-12 16:37:13 +0200869/*
Kevin Wolf57915332010-04-14 15:24:50 +0200870 * Common part for opening disk images and files
Kevin Wolfb6ad4912013-03-15 10:35:04 +0100871 *
872 * Removes all processed options from *options.
Kevin Wolf57915332010-04-14 15:24:50 +0200873 */
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200874static int bdrv_open_common(BlockDriverState *bs, BdrvChild *file,
Kevin Wolf82dc8b42016-01-11 19:07:50 +0100875 QDict *options, Error **errp)
Kevin Wolf57915332010-04-14 15:24:50 +0200876{
877 int ret, open_flags;
Kevin Wolf035fccd2013-04-09 14:34:19 +0200878 const char *filename;
Kevin Wolf62392eb2015-04-24 16:38:02 +0200879 const char *driver_name = NULL;
Benoît Canet6913c0c2014-01-23 21:31:33 +0100880 const char *node_name = NULL;
Kevin Wolf18edf282015-04-07 17:12:56 +0200881 QemuOpts *opts;
Kevin Wolf62392eb2015-04-24 16:38:02 +0200882 BlockDriver *drv;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200883 Error *local_err = NULL;
Kevin Wolf57915332010-04-14 15:24:50 +0200884
Paolo Bonzini64058752012-05-08 16:51:49 +0200885 assert(bs->file == NULL);
Kevin Wolf707ff822013-03-06 12:20:31 +0100886 assert(options != NULL && bs->options != options);
Kevin Wolf57915332010-04-14 15:24:50 +0200887
Kevin Wolf62392eb2015-04-24 16:38:02 +0200888 opts = qemu_opts_create(&bdrv_runtime_opts, NULL, 0, &error_abort);
889 qemu_opts_absorb_qdict(opts, options, &local_err);
890 if (local_err) {
891 error_propagate(errp, local_err);
892 ret = -EINVAL;
893 goto fail_opts;
894 }
895
896 driver_name = qemu_opt_get(opts, "driver");
897 drv = bdrv_find_format(driver_name);
898 assert(drv != NULL);
899
Kevin Wolf45673672013-04-22 17:48:40 +0200900 if (file != NULL) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200901 filename = file->bs->filename;
Kevin Wolf45673672013-04-22 17:48:40 +0200902 } else {
903 filename = qdict_get_try_str(options, "filename");
904 }
905
Kevin Wolf765003d2014-02-03 14:49:42 +0100906 if (drv->bdrv_needs_filename && !filename) {
907 error_setg(errp, "The '%s' block driver requires a file name",
908 drv->format_name);
Kevin Wolf18edf282015-04-07 17:12:56 +0200909 ret = -EINVAL;
910 goto fail_opts;
911 }
912
Kevin Wolf82dc8b42016-01-11 19:07:50 +0100913 trace_bdrv_open_common(bs, filename ?: "", bs->open_flags,
914 drv->format_name);
Kevin Wolf62392eb2015-04-24 16:38:02 +0200915
Kevin Wolf18edf282015-04-07 17:12:56 +0200916 node_name = qemu_opt_get(opts, "node-name");
Kevin Wolf636ea372014-01-24 14:11:52 +0100917 bdrv_assign_node_name(bs, node_name, &local_err);
Markus Armbruster0fb63952014-04-25 16:50:31 +0200918 if (local_err) {
Kevin Wolf636ea372014-01-24 14:11:52 +0100919 error_propagate(errp, local_err);
Kevin Wolf18edf282015-04-07 17:12:56 +0200920 ret = -EINVAL;
921 goto fail_opts;
Kevin Wolf5d186eb2013-03-27 17:28:18 +0100922 }
923
Paolo Bonzinic25f53b2011-11-29 12:42:20 +0100924 bs->request_alignment = 512;
Asias He0d51b4d2013-08-22 15:24:14 +0800925 bs->zero_beyond_eof = true;
Kevin Wolf82dc8b42016-01-11 19:07:50 +0100926 bs->read_only = !(bs->open_flags & BDRV_O_RDWR);
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800927
928 if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, bs->read_only)) {
Kevin Wolf8f94a6e2013-10-10 11:45:55 +0200929 error_setg(errp,
930 !bs->read_only && bdrv_is_whitelisted(drv, true)
931 ? "Driver '%s' can only be used for read-only devices"
932 : "Driver '%s' is not whitelisted",
933 drv->format_name);
Kevin Wolf18edf282015-04-07 17:12:56 +0200934 ret = -ENOTSUP;
935 goto fail_opts;
Fam Zhengb64ec4e2013-05-29 19:35:40 +0800936 }
Kevin Wolf57915332010-04-14 15:24:50 +0200937
Stefan Hajnoczi53fec9d2011-11-28 16:08:47 +0000938 assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */
Kevin Wolf82dc8b42016-01-11 19:07:50 +0100939 if (bs->open_flags & BDRV_O_COPY_ON_READ) {
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200940 if (!bs->read_only) {
941 bdrv_enable_copy_on_read(bs);
942 } else {
943 error_setg(errp, "Can't use copy-on-read on read-only device");
Kevin Wolf18edf282015-04-07 17:12:56 +0200944 ret = -EINVAL;
945 goto fail_opts;
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200946 }
Stefan Hajnoczi53fec9d2011-11-28 16:08:47 +0000947 }
948
Kevin Wolfc2ad1b02013-03-18 16:40:51 +0100949 if (filename != NULL) {
950 pstrcpy(bs->filename, sizeof(bs->filename), filename);
951 } else {
952 bs->filename[0] = '\0';
953 }
Max Reitz91af7012014-07-18 20:24:56 +0200954 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), bs->filename);
Kevin Wolf57915332010-04-14 15:24:50 +0200955
Kevin Wolf57915332010-04-14 15:24:50 +0200956 bs->drv = drv;
Anthony Liguori7267c092011-08-20 22:09:37 -0500957 bs->opaque = g_malloc0(drv->instance_size);
Kevin Wolf57915332010-04-14 15:24:50 +0200958
Kevin Wolf91a097e2015-05-08 17:49:53 +0200959 /* Apply cache mode options */
960 update_flags_from_options(&bs->open_flags, opts);
Kevin Wolf73ac4512016-03-14 15:46:03 +0100961
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200962 /* Open the image, either directly or using a protocol */
Kevin Wolf82dc8b42016-01-11 19:07:50 +0100963 open_flags = bdrv_open_flags(bs, bs->open_flags);
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200964 if (drv->bdrv_file_open) {
Kevin Wolf5d186eb2013-03-27 17:28:18 +0100965 assert(file == NULL);
Benoît Canet030be322013-09-24 17:07:04 +0200966 assert(!drv->bdrv_needs_filename || filename != NULL);
Max Reitz34b5d2c2013-09-05 14:45:29 +0200967 ret = drv->bdrv_file_open(bs, options, open_flags, &local_err);
Kevin Wolff500a6d2012-11-12 17:35:27 +0100968 } else {
Kevin Wolf2af5ef72013-04-09 13:19:18 +0200969 if (file == NULL) {
Max Reitz34b5d2c2013-09-05 14:45:29 +0200970 error_setg(errp, "Can't use '%s' as a block driver for the "
971 "protocol level", drv->format_name);
Kevin Wolf2af5ef72013-04-09 13:19:18 +0200972 ret = -EINVAL;
973 goto free_and_fail;
974 }
Kevin Wolff500a6d2012-11-12 17:35:27 +0100975 bs->file = file;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200976 ret = drv->bdrv_open(bs, options, open_flags, &local_err);
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200977 }
978
Kevin Wolf57915332010-04-14 15:24:50 +0200979 if (ret < 0) {
Markus Armbruster84d18f02014-01-30 15:07:28 +0100980 if (local_err) {
Max Reitz34b5d2c2013-09-05 14:45:29 +0200981 error_propagate(errp, local_err);
Dunrong Huang2fa9aa52013-09-24 18:14:01 +0800982 } else if (bs->filename[0]) {
983 error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename);
Max Reitz34b5d2c2013-09-05 14:45:29 +0200984 } else {
985 error_setg_errno(errp, -ret, "Could not open image");
986 }
Kevin Wolf57915332010-04-14 15:24:50 +0200987 goto free_and_fail;
988 }
989
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100990 ret = refresh_total_sectors(bs, bs->total_sectors);
991 if (ret < 0) {
Max Reitz34b5d2c2013-09-05 14:45:29 +0200992 error_setg_errno(errp, -ret, "Could not refresh total sector count");
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100993 goto free_and_fail;
Kevin Wolf57915332010-04-14 15:24:50 +0200994 }
Stefan Hajnoczi51762282010-04-19 16:56:41 +0100995
Kevin Wolf3baca892014-07-16 17:48:16 +0200996 bdrv_refresh_limits(bs, &local_err);
997 if (local_err) {
998 error_propagate(errp, local_err);
999 ret = -EINVAL;
1000 goto free_and_fail;
1001 }
1002
Paolo Bonzinic25f53b2011-11-29 12:42:20 +01001003 assert(bdrv_opt_mem_align(bs) != 0);
Denis V. Lunev4196d2f2015-05-12 17:30:55 +03001004 assert(bdrv_min_mem_align(bs) != 0);
Dimitris Aragiorgisb192af82015-06-23 13:44:56 +03001005 assert((bs->request_alignment != 0) || bdrv_is_sg(bs));
Kevin Wolf18edf282015-04-07 17:12:56 +02001006
1007 qemu_opts_del(opts);
Kevin Wolf57915332010-04-14 15:24:50 +02001008 return 0;
1009
1010free_and_fail:
Kevin Wolff500a6d2012-11-12 17:35:27 +01001011 bs->file = NULL;
Anthony Liguori7267c092011-08-20 22:09:37 -05001012 g_free(bs->opaque);
Kevin Wolf57915332010-04-14 15:24:50 +02001013 bs->opaque = NULL;
1014 bs->drv = NULL;
Kevin Wolf18edf282015-04-07 17:12:56 +02001015fail_opts:
1016 qemu_opts_del(opts);
Kevin Wolf57915332010-04-14 15:24:50 +02001017 return ret;
1018}
1019
Kevin Wolf5e5c4f62014-05-26 11:45:08 +02001020static QDict *parse_json_filename(const char *filename, Error **errp)
1021{
1022 QObject *options_obj;
1023 QDict *options;
1024 int ret;
1025
1026 ret = strstart(filename, "json:", &filename);
1027 assert(ret);
1028
1029 options_obj = qobject_from_json(filename);
1030 if (!options_obj) {
1031 error_setg(errp, "Could not parse the JSON options");
1032 return NULL;
1033 }
1034
1035 if (qobject_type(options_obj) != QTYPE_QDICT) {
1036 qobject_decref(options_obj);
1037 error_setg(errp, "Invalid JSON object given");
1038 return NULL;
1039 }
1040
1041 options = qobject_to_qdict(options_obj);
1042 qdict_flatten(options);
1043
1044 return options;
1045}
1046
Kevin Wolfde3b53f2015-10-29 15:24:41 +01001047static void parse_json_protocol(QDict *options, const char **pfilename,
1048 Error **errp)
1049{
1050 QDict *json_options;
1051 Error *local_err = NULL;
1052
1053 /* Parse json: pseudo-protocol */
1054 if (!*pfilename || !g_str_has_prefix(*pfilename, "json:")) {
1055 return;
1056 }
1057
1058 json_options = parse_json_filename(*pfilename, &local_err);
1059 if (local_err) {
1060 error_propagate(errp, local_err);
1061 return;
1062 }
1063
1064 /* Options given in the filename have lower priority than options
1065 * specified directly */
1066 qdict_join(options, json_options, false);
1067 QDECREF(json_options);
1068 *pfilename = NULL;
1069}
1070
Kevin Wolf57915332010-04-14 15:24:50 +02001071/*
Kevin Wolff54120f2014-05-26 11:09:59 +02001072 * Fills in default options for opening images and converts the legacy
1073 * filename/flags pair to option QDict entries.
Max Reitz53a29512015-03-19 14:53:16 -04001074 * The BDRV_O_PROTOCOL flag in *flags will be set or cleared accordingly if a
1075 * block driver has been specified explicitly.
Kevin Wolff54120f2014-05-26 11:09:59 +02001076 */
Kevin Wolfde3b53f2015-10-29 15:24:41 +01001077static int bdrv_fill_options(QDict **options, const char *filename,
Max Reitz053e1572015-08-26 19:47:51 +02001078 int *flags, Error **errp)
Kevin Wolff54120f2014-05-26 11:09:59 +02001079{
1080 const char *drvname;
Max Reitz53a29512015-03-19 14:53:16 -04001081 bool protocol = *flags & BDRV_O_PROTOCOL;
Kevin Wolff54120f2014-05-26 11:09:59 +02001082 bool parse_filename = false;
Max Reitz053e1572015-08-26 19:47:51 +02001083 BlockDriver *drv = NULL;
Kevin Wolff54120f2014-05-26 11:09:59 +02001084 Error *local_err = NULL;
Kevin Wolff54120f2014-05-26 11:09:59 +02001085
Max Reitz53a29512015-03-19 14:53:16 -04001086 drvname = qdict_get_try_str(*options, "driver");
Max Reitz053e1572015-08-26 19:47:51 +02001087 if (drvname) {
1088 drv = bdrv_find_format(drvname);
1089 if (!drv) {
1090 error_setg(errp, "Unknown driver '%s'", drvname);
1091 return -ENOENT;
1092 }
1093 /* If the user has explicitly specified the driver, this choice should
1094 * override the BDRV_O_PROTOCOL flag */
1095 protocol = drv->bdrv_file_open;
Max Reitz53a29512015-03-19 14:53:16 -04001096 }
1097
1098 if (protocol) {
1099 *flags |= BDRV_O_PROTOCOL;
1100 } else {
1101 *flags &= ~BDRV_O_PROTOCOL;
1102 }
1103
Kevin Wolf91a097e2015-05-08 17:49:53 +02001104 /* Translate cache options from flags into options */
1105 update_options_from_flags(*options, *flags);
1106
Kevin Wolff54120f2014-05-26 11:09:59 +02001107 /* Fetch the file name from the options QDict if necessary */
Kevin Wolf17b005f2014-05-27 10:50:29 +02001108 if (protocol && filename) {
Kevin Wolff54120f2014-05-26 11:09:59 +02001109 if (!qdict_haskey(*options, "filename")) {
1110 qdict_put(*options, "filename", qstring_from_str(filename));
1111 parse_filename = true;
1112 } else {
1113 error_setg(errp, "Can't specify 'file' and 'filename' options at "
1114 "the same time");
1115 return -EINVAL;
1116 }
1117 }
1118
1119 /* Find the right block driver */
1120 filename = qdict_get_try_str(*options, "filename");
Kevin Wolff54120f2014-05-26 11:09:59 +02001121
Max Reitz053e1572015-08-26 19:47:51 +02001122 if (!drvname && protocol) {
1123 if (filename) {
1124 drv = bdrv_find_protocol(filename, parse_filename, errp);
1125 if (!drv) {
Kevin Wolff54120f2014-05-26 11:09:59 +02001126 return -EINVAL;
1127 }
Max Reitz053e1572015-08-26 19:47:51 +02001128
1129 drvname = drv->format_name;
1130 qdict_put(*options, "driver", qstring_from_str(drvname));
1131 } else {
1132 error_setg(errp, "Must specify either driver or file");
1133 return -EINVAL;
Kevin Wolff54120f2014-05-26 11:09:59 +02001134 }
1135 }
1136
Kevin Wolf17b005f2014-05-27 10:50:29 +02001137 assert(drv || !protocol);
Kevin Wolff54120f2014-05-26 11:09:59 +02001138
1139 /* Driver-specific filename parsing */
Kevin Wolf17b005f2014-05-27 10:50:29 +02001140 if (drv && drv->bdrv_parse_filename && parse_filename) {
Kevin Wolff54120f2014-05-26 11:09:59 +02001141 drv->bdrv_parse_filename(filename, *options, &local_err);
1142 if (local_err) {
1143 error_propagate(errp, local_err);
1144 return -EINVAL;
1145 }
1146
1147 if (!drv->bdrv_needs_filename) {
1148 qdict_del(*options, "filename");
1149 }
1150 }
1151
1152 return 0;
1153}
1154
Kevin Wolff21d96d2016-03-08 13:47:46 +01001155BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs,
1156 const char *child_name,
1157 const BdrvChildRole *child_role)
Kevin Wolfdf581792015-06-15 11:53:47 +02001158{
1159 BdrvChild *child = g_new(BdrvChild, 1);
1160 *child = (BdrvChild) {
1161 .bs = child_bs,
Kevin Wolf260fecf2015-04-27 13:46:22 +02001162 .name = g_strdup(child_name),
Kevin Wolfdf581792015-06-15 11:53:47 +02001163 .role = child_role,
1164 };
1165
Kevin Wolfd42a8a92015-09-17 13:18:23 +02001166 QLIST_INSERT_HEAD(&child_bs->parents, child, next_parent);
Kevin Wolfb4b059f2015-06-15 13:24:19 +02001167
1168 return child;
Kevin Wolfdf581792015-06-15 11:53:47 +02001169}
1170
Wen Congyang98292c62016-05-10 15:36:38 +08001171BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs,
1172 BlockDriverState *child_bs,
1173 const char *child_name,
1174 const BdrvChildRole *child_role)
Kevin Wolff21d96d2016-03-08 13:47:46 +01001175{
1176 BdrvChild *child = bdrv_root_attach_child(child_bs, child_name, child_role);
1177 QLIST_INSERT_HEAD(&parent_bs->children, child, next);
1178 return child;
1179}
1180
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001181static void bdrv_detach_child(BdrvChild *child)
Kevin Wolf33a60402015-06-15 13:51:04 +02001182{
Kevin Wolff21d96d2016-03-08 13:47:46 +01001183 if (child->next.le_prev) {
1184 QLIST_REMOVE(child, next);
1185 child->next.le_prev = NULL;
1186 }
Kevin Wolfd42a8a92015-09-17 13:18:23 +02001187 QLIST_REMOVE(child, next_parent);
Kevin Wolf260fecf2015-04-27 13:46:22 +02001188 g_free(child->name);
Kevin Wolf33a60402015-06-15 13:51:04 +02001189 g_free(child);
1190}
1191
Kevin Wolff21d96d2016-03-08 13:47:46 +01001192void bdrv_root_unref_child(BdrvChild *child)
Kevin Wolf33a60402015-06-15 13:51:04 +02001193{
Kevin Wolf779020c2015-10-13 14:09:44 +02001194 BlockDriverState *child_bs;
1195
Kevin Wolff21d96d2016-03-08 13:47:46 +01001196 child_bs = child->bs;
1197 bdrv_detach_child(child);
1198 bdrv_unref(child_bs);
1199}
1200
1201void bdrv_unref_child(BlockDriverState *parent, BdrvChild *child)
1202{
Kevin Wolf779020c2015-10-13 14:09:44 +02001203 if (child == NULL) {
1204 return;
1205 }
Kevin Wolf33a60402015-06-15 13:51:04 +02001206
1207 if (child->bs->inherits_from == parent) {
1208 child->bs->inherits_from = NULL;
1209 }
1210
Kevin Wolff21d96d2016-03-08 13:47:46 +01001211 bdrv_root_unref_child(child);
Kevin Wolf33a60402015-06-15 13:51:04 +02001212}
1213
Kevin Wolf5c8cab42016-02-24 15:13:35 +01001214
1215static void bdrv_parent_cb_change_media(BlockDriverState *bs, bool load)
1216{
1217 BdrvChild *c;
1218 QLIST_FOREACH(c, &bs->parents, next_parent) {
1219 if (c->role->change_media) {
1220 c->role->change_media(c, load);
1221 }
1222 }
1223}
1224
1225static void bdrv_parent_cb_resize(BlockDriverState *bs)
1226{
1227 BdrvChild *c;
1228 QLIST_FOREACH(c, &bs->parents, next_parent) {
1229 if (c->role->resize) {
1230 c->role->resize(c);
1231 }
1232 }
1233}
1234
Kevin Wolf5db15a52015-09-14 15:33:33 +02001235/*
1236 * Sets the backing file link of a BDS. A new reference is created; callers
1237 * which don't need their own reference any more must call bdrv_unref().
1238 */
Fam Zheng8d24cce2014-05-23 21:29:45 +08001239void bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd)
1240{
Kevin Wolf5db15a52015-09-14 15:33:33 +02001241 if (backing_hd) {
1242 bdrv_ref(backing_hd);
1243 }
Fam Zheng8d24cce2014-05-23 21:29:45 +08001244
Kevin Wolf760e0062015-06-17 14:55:21 +02001245 if (bs->backing) {
Fam Zheng826b6ca2014-05-23 21:29:47 +08001246 assert(bs->backing_blocker);
Kevin Wolf760e0062015-06-17 14:55:21 +02001247 bdrv_op_unblock_all(bs->backing->bs, bs->backing_blocker);
Kevin Wolf5db15a52015-09-14 15:33:33 +02001248 bdrv_unref_child(bs, bs->backing);
Fam Zheng826b6ca2014-05-23 21:29:47 +08001249 } else if (backing_hd) {
1250 error_setg(&bs->backing_blocker,
Alberto Garcia81e5f782015-04-08 12:29:19 +03001251 "node is used as backing hd of '%s'",
1252 bdrv_get_device_or_node_name(bs));
Fam Zheng826b6ca2014-05-23 21:29:47 +08001253 }
1254
Fam Zheng8d24cce2014-05-23 21:29:45 +08001255 if (!backing_hd) {
Fam Zheng826b6ca2014-05-23 21:29:47 +08001256 error_free(bs->backing_blocker);
1257 bs->backing_blocker = NULL;
Kevin Wolf760e0062015-06-17 14:55:21 +02001258 bs->backing = NULL;
Fam Zheng8d24cce2014-05-23 21:29:45 +08001259 goto out;
1260 }
Kevin Wolf260fecf2015-04-27 13:46:22 +02001261 bs->backing = bdrv_attach_child(bs, backing_hd, "backing", &child_backing);
Fam Zheng8d24cce2014-05-23 21:29:45 +08001262 bs->open_flags &= ~BDRV_O_NO_BACKING;
1263 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_hd->filename);
1264 pstrcpy(bs->backing_format, sizeof(bs->backing_format),
1265 backing_hd->drv ? backing_hd->drv->format_name : "");
Fam Zheng826b6ca2014-05-23 21:29:47 +08001266
Kevin Wolf760e0062015-06-17 14:55:21 +02001267 bdrv_op_block_all(backing_hd, bs->backing_blocker);
Fam Zheng826b6ca2014-05-23 21:29:47 +08001268 /* Otherwise we won't be able to commit due to check in bdrv_commit */
Kevin Wolf760e0062015-06-17 14:55:21 +02001269 bdrv_op_unblock(backing_hd, BLOCK_OP_TYPE_COMMIT_TARGET,
Fam Zheng826b6ca2014-05-23 21:29:47 +08001270 bs->backing_blocker);
Fam Zheng8d24cce2014-05-23 21:29:45 +08001271out:
Kevin Wolf3baca892014-07-16 17:48:16 +02001272 bdrv_refresh_limits(bs, NULL);
Fam Zheng8d24cce2014-05-23 21:29:45 +08001273}
1274
Kevin Wolf31ca6d02013-03-28 15:29:24 +01001275/*
1276 * Opens the backing file for a BlockDriverState if not yet open
1277 *
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001278 * bdref_key specifies the key for the image's BlockdevRef in the options QDict.
1279 * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict
1280 * itself, all options starting with "${bdref_key}." are considered part of the
1281 * BlockdevRef.
1282 *
1283 * TODO Can this be unified with bdrv_open_image()?
Kevin Wolf31ca6d02013-03-28 15:29:24 +01001284 */
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001285int bdrv_open_backing_file(BlockDriverState *bs, QDict *parent_options,
1286 const char *bdref_key, Error **errp)
Paolo Bonzini9156df12012-10-18 16:49:17 +02001287{
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001288 char *backing_filename = g_malloc0(PATH_MAX);
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001289 char *bdref_key_dot;
1290 const char *reference = NULL;
Kevin Wolf317fc442014-04-25 13:27:34 +02001291 int ret = 0;
Fam Zheng8d24cce2014-05-23 21:29:45 +08001292 BlockDriverState *backing_hd;
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001293 QDict *options;
1294 QDict *tmp_parent_options = NULL;
Max Reitz34b5d2c2013-09-05 14:45:29 +02001295 Error *local_err = NULL;
Paolo Bonzini9156df12012-10-18 16:49:17 +02001296
Kevin Wolf760e0062015-06-17 14:55:21 +02001297 if (bs->backing != NULL) {
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001298 goto free_exit;
Paolo Bonzini9156df12012-10-18 16:49:17 +02001299 }
1300
Kevin Wolf31ca6d02013-03-28 15:29:24 +01001301 /* NULL means an empty set of options */
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001302 if (parent_options == NULL) {
1303 tmp_parent_options = qdict_new();
1304 parent_options = tmp_parent_options;
Kevin Wolf31ca6d02013-03-28 15:29:24 +01001305 }
1306
Paolo Bonzini9156df12012-10-18 16:49:17 +02001307 bs->open_flags &= ~BDRV_O_NO_BACKING;
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001308
1309 bdref_key_dot = g_strdup_printf("%s.", bdref_key);
1310 qdict_extract_subqdict(parent_options, &options, bdref_key_dot);
1311 g_free(bdref_key_dot);
1312
1313 reference = qdict_get_try_str(parent_options, bdref_key);
1314 if (reference || qdict_haskey(options, "file.filename")) {
Kevin Wolf1cb6f502013-04-12 20:27:07 +02001315 backing_filename[0] = '\0';
1316 } else if (bs->backing_file[0] == '\0' && qdict_size(options) == 0) {
Kevin Wolf31ca6d02013-03-28 15:29:24 +01001317 QDECREF(options);
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001318 goto free_exit;
Fam Zhengdbecebd2013-09-22 20:05:06 +08001319 } else {
Max Reitz9f074292014-11-26 17:20:26 +01001320 bdrv_get_full_backing_filename(bs, backing_filename, PATH_MAX,
1321 &local_err);
1322 if (local_err) {
1323 ret = -EINVAL;
1324 error_propagate(errp, local_err);
1325 QDECREF(options);
1326 goto free_exit;
1327 }
Paolo Bonzini9156df12012-10-18 16:49:17 +02001328 }
1329
Kevin Wolf8ee79e72014-06-04 15:09:35 +02001330 if (!bs->drv || !bs->drv->supports_backing) {
1331 ret = -EINVAL;
1332 error_setg(errp, "Driver doesn't support backing files");
1333 QDECREF(options);
1334 goto free_exit;
1335 }
1336
Kevin Wolfc5f6e492014-11-25 18:12:42 +01001337 if (bs->backing_format[0] != '\0' && !qdict_haskey(options, "driver")) {
1338 qdict_put(options, "driver", qstring_from_str(bs->backing_format));
Paolo Bonzini9156df12012-10-18 16:49:17 +02001339 }
1340
Max Reitz5b363932016-05-17 16:41:31 +02001341 backing_hd = bdrv_open_inherit(*backing_filename ? backing_filename : NULL,
1342 reference, options, 0, bs, &child_backing,
1343 errp);
1344 if (!backing_hd) {
Paolo Bonzini9156df12012-10-18 16:49:17 +02001345 bs->open_flags |= BDRV_O_NO_BACKING;
Markus Armbrustere43bfd92015-12-18 16:35:15 +01001346 error_prepend(errp, "Could not open backing file: ");
Max Reitz5b363932016-05-17 16:41:31 +02001347 ret = -EINVAL;
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001348 goto free_exit;
Paolo Bonzini9156df12012-10-18 16:49:17 +02001349 }
Kevin Wolfdf581792015-06-15 11:53:47 +02001350
Kevin Wolf5db15a52015-09-14 15:33:33 +02001351 /* Hook up the backing file link; drop our reference, bs owns the
1352 * backing_hd reference now */
Fam Zheng8d24cce2014-05-23 21:29:45 +08001353 bdrv_set_backing_hd(bs, backing_hd);
Kevin Wolf5db15a52015-09-14 15:33:33 +02001354 bdrv_unref(backing_hd);
Peter Feinerd80ac652014-01-08 19:43:25 +00001355
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001356 qdict_del(parent_options, bdref_key);
1357
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001358free_exit:
1359 g_free(backing_filename);
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001360 QDECREF(tmp_parent_options);
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001361 return ret;
Paolo Bonzini9156df12012-10-18 16:49:17 +02001362}
1363
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001364/*
Max Reitzda557aa2013-12-20 19:28:11 +01001365 * Opens a disk image whose options are given as BlockdevRef in another block
1366 * device's options.
1367 *
Max Reitzda557aa2013-12-20 19:28:11 +01001368 * If allow_none is true, no image will be opened if filename is false and no
Kevin Wolfb4b059f2015-06-15 13:24:19 +02001369 * BlockdevRef is given. NULL will be returned, but errp remains unset.
Max Reitzda557aa2013-12-20 19:28:11 +01001370 *
1371 * bdrev_key specifies the key for the image's BlockdevRef in the options QDict.
1372 * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict
1373 * itself, all options starting with "${bdref_key}." are considered part of the
1374 * BlockdevRef.
1375 *
1376 * The BlockdevRef will be removed from the options QDict.
1377 */
Kevin Wolfb4b059f2015-06-15 13:24:19 +02001378BdrvChild *bdrv_open_child(const char *filename,
1379 QDict *options, const char *bdref_key,
1380 BlockDriverState* parent,
1381 const BdrvChildRole *child_role,
1382 bool allow_none, Error **errp)
Max Reitzda557aa2013-12-20 19:28:11 +01001383{
Kevin Wolfb4b059f2015-06-15 13:24:19 +02001384 BdrvChild *c = NULL;
1385 BlockDriverState *bs;
Max Reitzda557aa2013-12-20 19:28:11 +01001386 QDict *image_options;
Max Reitzda557aa2013-12-20 19:28:11 +01001387 char *bdref_key_dot;
1388 const char *reference;
1389
Kevin Wolfdf581792015-06-15 11:53:47 +02001390 assert(child_role != NULL);
Max Reitzf67503e2014-02-18 18:33:05 +01001391
Max Reitzda557aa2013-12-20 19:28:11 +01001392 bdref_key_dot = g_strdup_printf("%s.", bdref_key);
1393 qdict_extract_subqdict(options, &image_options, bdref_key_dot);
1394 g_free(bdref_key_dot);
1395
1396 reference = qdict_get_try_str(options, bdref_key);
1397 if (!filename && !reference && !qdict_size(image_options)) {
Kevin Wolfb4b059f2015-06-15 13:24:19 +02001398 if (!allow_none) {
Max Reitzda557aa2013-12-20 19:28:11 +01001399 error_setg(errp, "A block device must be specified for \"%s\"",
1400 bdref_key);
Max Reitzda557aa2013-12-20 19:28:11 +01001401 }
Markus Armbrusterb20e61e2014-05-28 11:16:57 +02001402 QDECREF(image_options);
Max Reitzda557aa2013-12-20 19:28:11 +01001403 goto done;
1404 }
1405
Max Reitz5b363932016-05-17 16:41:31 +02001406 bs = bdrv_open_inherit(filename, reference, image_options, 0,
1407 parent, child_role, errp);
1408 if (!bs) {
Kevin Wolfdf581792015-06-15 11:53:47 +02001409 goto done;
1410 }
1411
Kevin Wolf260fecf2015-04-27 13:46:22 +02001412 c = bdrv_attach_child(parent, bs, bdref_key, child_role);
Max Reitzda557aa2013-12-20 19:28:11 +01001413
1414done:
1415 qdict_del(options, bdref_key);
Kevin Wolfb4b059f2015-06-15 13:24:19 +02001416 return c;
1417}
1418
Max Reitz66836182016-05-17 16:41:27 +02001419static BlockDriverState *bdrv_append_temp_snapshot(BlockDriverState *bs,
1420 int flags,
1421 QDict *snapshot_options,
1422 Error **errp)
Kevin Wolfb9988752014-04-03 12:09:34 +02001423{
1424 /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001425 char *tmp_filename = g_malloc0(PATH_MAX + 1);
Kevin Wolfb9988752014-04-03 12:09:34 +02001426 int64_t total_size;
Chunyan Liu83d05212014-06-05 17:20:51 +08001427 QemuOpts *opts = NULL;
Kevin Wolfb9988752014-04-03 12:09:34 +02001428 BlockDriverState *bs_snapshot;
Kevin Wolfb9988752014-04-03 12:09:34 +02001429 int ret;
1430
1431 /* if snapshot, we create a temporary backing file and open it
1432 instead of opening 'filename' directly */
1433
1434 /* Get the required size from the image */
Kevin Wolff1877432014-04-04 17:07:19 +02001435 total_size = bdrv_getlength(bs);
1436 if (total_size < 0) {
1437 error_setg_errno(errp, -total_size, "Could not get image size");
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001438 goto out;
Kevin Wolff1877432014-04-04 17:07:19 +02001439 }
Kevin Wolfb9988752014-04-03 12:09:34 +02001440
1441 /* Create the temporary image */
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001442 ret = get_tmp_filename(tmp_filename, PATH_MAX + 1);
Kevin Wolfb9988752014-04-03 12:09:34 +02001443 if (ret < 0) {
1444 error_setg_errno(errp, -ret, "Could not get temporary filename");
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001445 goto out;
Kevin Wolfb9988752014-04-03 12:09:34 +02001446 }
1447
Max Reitzef810432014-12-02 18:32:42 +01001448 opts = qemu_opts_create(bdrv_qcow2.create_opts, NULL, 0,
Chunyan Liuc282e1f2014-06-05 17:21:11 +08001449 &error_abort);
Markus Armbruster39101f22015-02-12 16:46:36 +01001450 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_size, &error_abort);
Markus Armbrustere43bfd92015-12-18 16:35:15 +01001451 ret = bdrv_create(&bdrv_qcow2, tmp_filename, opts, errp);
Chunyan Liu83d05212014-06-05 17:20:51 +08001452 qemu_opts_del(opts);
Kevin Wolfb9988752014-04-03 12:09:34 +02001453 if (ret < 0) {
Markus Armbrustere43bfd92015-12-18 16:35:15 +01001454 error_prepend(errp, "Could not create temporary overlay '%s': ",
1455 tmp_filename);
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001456 goto out;
Kevin Wolfb9988752014-04-03 12:09:34 +02001457 }
1458
Kevin Wolf73176be2016-03-07 13:02:15 +01001459 /* Prepare options QDict for the temporary file */
Kevin Wolfb9988752014-04-03 12:09:34 +02001460 qdict_put(snapshot_options, "file.driver",
1461 qstring_from_str("file"));
1462 qdict_put(snapshot_options, "file.filename",
1463 qstring_from_str(tmp_filename));
Max Reitze6641712015-08-26 19:47:48 +02001464 qdict_put(snapshot_options, "driver",
1465 qstring_from_str("qcow2"));
Kevin Wolfb9988752014-04-03 12:09:34 +02001466
Max Reitz5b363932016-05-17 16:41:31 +02001467 bs_snapshot = bdrv_open(NULL, NULL, snapshot_options, flags, errp);
Kevin Wolf73176be2016-03-07 13:02:15 +01001468 snapshot_options = NULL;
Max Reitz5b363932016-05-17 16:41:31 +02001469 if (!bs_snapshot) {
1470 ret = -EINVAL;
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001471 goto out;
Kevin Wolfb9988752014-04-03 12:09:34 +02001472 }
1473
Max Reitz66836182016-05-17 16:41:27 +02001474 /* bdrv_append() consumes a strong reference to bs_snapshot (i.e. it will
1475 * call bdrv_unref() on it), so in order to be able to return one, we have
1476 * to increase bs_snapshot's refcount here */
1477 bdrv_ref(bs_snapshot);
Kevin Wolfb9988752014-04-03 12:09:34 +02001478 bdrv_append(bs_snapshot, bs);
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001479
Max Reitz66836182016-05-17 16:41:27 +02001480 g_free(tmp_filename);
1481 return bs_snapshot;
1482
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001483out:
Kevin Wolf73176be2016-03-07 13:02:15 +01001484 QDECREF(snapshot_options);
Benoît Canet1ba4b6a2014-04-22 17:05:27 +02001485 g_free(tmp_filename);
Max Reitz66836182016-05-17 16:41:27 +02001486 return NULL;
Kevin Wolfb9988752014-04-03 12:09:34 +02001487}
1488
Max Reitzda557aa2013-12-20 19:28:11 +01001489/*
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001490 * Opens a disk image (raw, qcow2, vmdk, ...)
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001491 *
1492 * options is a QDict of options to pass to the block drivers, or NULL for an
1493 * empty set of options. The reference to the QDict belongs to the block layer
1494 * after the call (even on failure), so if the caller intends to reuse the
1495 * dictionary, it needs to use QINCREF() before calling bdrv_open.
Max Reitzf67503e2014-02-18 18:33:05 +01001496 *
1497 * If *pbs is NULL, a new BDS will be created with a pointer to it stored there.
1498 * If it is not NULL, the referenced BDS will be reused.
Max Reitzddf56362014-02-18 18:33:06 +01001499 *
1500 * The reference parameter may be used to specify an existing block device which
1501 * should be opened. If specified, neither options nor a filename may be given,
1502 * nor can an existing BDS be reused (that is, *pbs has to be NULL).
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001503 */
Max Reitz5b363932016-05-17 16:41:31 +02001504static BlockDriverState *bdrv_open_inherit(const char *filename,
1505 const char *reference,
1506 QDict *options, int flags,
1507 BlockDriverState *parent,
1508 const BdrvChildRole *child_role,
1509 Error **errp)
bellardea2384d2004-08-01 21:59:26 +00001510{
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001511 int ret;
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02001512 BdrvChild *file = NULL;
1513 BlockDriverState *bs;
Max Reitzce343772015-08-26 19:47:50 +02001514 BlockDriver *drv = NULL;
Kevin Wolf74fe54f2013-07-09 11:09:02 +02001515 const char *drvname;
Alberto Garcia3e8c2e52015-10-26 14:27:15 +02001516 const char *backing;
Max Reitz34b5d2c2013-09-05 14:45:29 +02001517 Error *local_err = NULL;
Kevin Wolf73176be2016-03-07 13:02:15 +01001518 QDict *snapshot_options = NULL;
Kevin Wolfb1e6fc02014-05-06 12:11:42 +02001519 int snapshot_flags = 0;
bellard712e7872005-04-28 21:09:32 +00001520
Kevin Wolff3930ed2015-04-08 13:43:47 +02001521 assert(!child_role || !flags);
1522 assert(!child_role == !parent);
Max Reitzf67503e2014-02-18 18:33:05 +01001523
Max Reitzddf56362014-02-18 18:33:06 +01001524 if (reference) {
1525 bool options_non_empty = options ? qdict_size(options) : false;
1526 QDECREF(options);
1527
Max Reitzddf56362014-02-18 18:33:06 +01001528 if (filename || options_non_empty) {
1529 error_setg(errp, "Cannot reference an existing block device with "
1530 "additional options or a new filename");
Max Reitz5b363932016-05-17 16:41:31 +02001531 return NULL;
Max Reitzddf56362014-02-18 18:33:06 +01001532 }
1533
1534 bs = bdrv_lookup_bs(reference, reference, errp);
1535 if (!bs) {
Max Reitz5b363932016-05-17 16:41:31 +02001536 return NULL;
Max Reitzddf56362014-02-18 18:33:06 +01001537 }
Kevin Wolf76b22322016-04-04 17:11:13 +02001538
Max Reitzddf56362014-02-18 18:33:06 +01001539 bdrv_ref(bs);
Max Reitz5b363932016-05-17 16:41:31 +02001540 return bs;
Max Reitzddf56362014-02-18 18:33:06 +01001541 }
1542
Max Reitz5b363932016-05-17 16:41:31 +02001543 bs = bdrv_new();
Max Reitzf67503e2014-02-18 18:33:05 +01001544
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001545 /* NULL means an empty set of options */
1546 if (options == NULL) {
1547 options = qdict_new();
1548 }
1549
Kevin Wolf145f5982015-05-08 16:15:03 +02001550 /* json: syntax counts as explicit options, as if in the QDict */
Kevin Wolfde3b53f2015-10-29 15:24:41 +01001551 parse_json_protocol(options, &filename, &local_err);
1552 if (local_err) {
Kevin Wolfde3b53f2015-10-29 15:24:41 +01001553 goto fail;
1554 }
1555
Kevin Wolf145f5982015-05-08 16:15:03 +02001556 bs->explicit_options = qdict_clone_shallow(options);
1557
Kevin Wolff3930ed2015-04-08 13:43:47 +02001558 if (child_role) {
Kevin Wolfbddcec32015-04-09 18:47:50 +02001559 bs->inherits_from = parent;
Kevin Wolf8e2160e2015-04-29 17:29:39 +02001560 child_role->inherit_options(&flags, options,
1561 parent->open_flags, parent->options);
Kevin Wolff3930ed2015-04-08 13:43:47 +02001562 }
1563
Kevin Wolfde3b53f2015-10-29 15:24:41 +01001564 ret = bdrv_fill_options(&options, filename, &flags, &local_err);
Kevin Wolf462f5bc2014-05-26 11:39:55 +02001565 if (local_err) {
1566 goto fail;
1567 }
1568
Kevin Wolf62392eb2015-04-24 16:38:02 +02001569 bs->open_flags = flags;
1570 bs->options = options;
1571 options = qdict_clone_shallow(options);
1572
Kevin Wolf76c591b2014-06-04 14:19:44 +02001573 /* Find the right image format driver */
Kevin Wolf76c591b2014-06-04 14:19:44 +02001574 drvname = qdict_get_try_str(options, "driver");
1575 if (drvname) {
1576 drv = bdrv_find_format(drvname);
Kevin Wolf76c591b2014-06-04 14:19:44 +02001577 if (!drv) {
1578 error_setg(errp, "Unknown driver: '%s'", drvname);
Kevin Wolf76c591b2014-06-04 14:19:44 +02001579 goto fail;
1580 }
1581 }
1582
1583 assert(drvname || !(flags & BDRV_O_PROTOCOL));
Kevin Wolf76c591b2014-06-04 14:19:44 +02001584
Alberto Garcia3e8c2e52015-10-26 14:27:15 +02001585 backing = qdict_get_try_str(options, "backing");
1586 if (backing && *backing == '\0') {
1587 flags |= BDRV_O_NO_BACKING;
1588 qdict_del(options, "backing");
1589 }
1590
Kevin Wolff4788ad2014-06-03 16:44:19 +02001591 /* Open image file without format layer */
1592 if ((flags & BDRV_O_PROTOCOL) == 0) {
1593 if (flags & BDRV_O_RDWR) {
1594 flags |= BDRV_O_ALLOW_RDWR;
1595 }
1596 if (flags & BDRV_O_SNAPSHOT) {
Kevin Wolf73176be2016-03-07 13:02:15 +01001597 snapshot_options = qdict_new();
1598 bdrv_temp_snapshot_options(&snapshot_flags, snapshot_options,
1599 flags, options);
Kevin Wolf8e2160e2015-04-29 17:29:39 +02001600 bdrv_backing_options(&flags, options, flags, options);
Kevin Wolff4788ad2014-06-03 16:44:19 +02001601 }
1602
Kevin Wolff3930ed2015-04-08 13:43:47 +02001603 bs->open_flags = flags;
Kevin Wolf1fdd6932015-06-15 14:11:51 +02001604
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02001605 file = bdrv_open_child(filename, options, "file", bs,
1606 &child_file, true, &local_err);
Kevin Wolf1fdd6932015-06-15 14:11:51 +02001607 if (local_err) {
Max Reitz5469a2a2014-02-18 18:33:10 +01001608 goto fail;
1609 }
1610 }
1611
Kevin Wolf76c591b2014-06-04 14:19:44 +02001612 /* Image format probing */
Kevin Wolf38f3ef52014-11-20 16:27:12 +01001613 bs->probed = !drv;
Kevin Wolf76c591b2014-06-04 14:19:44 +02001614 if (!drv && file) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02001615 ret = find_image_format(file->bs, filename, &drv, &local_err);
Kevin Wolf17b005f2014-05-27 10:50:29 +02001616 if (ret < 0) {
Kevin Wolf8bfea152014-04-11 19:16:36 +02001617 goto fail;
Max Reitz2a05cbe2013-12-20 19:28:10 +01001618 }
Kevin Wolf62392eb2015-04-24 16:38:02 +02001619 /*
1620 * This option update would logically belong in bdrv_fill_options(),
1621 * but we first need to open bs->file for the probing to work, while
1622 * opening bs->file already requires the (mostly) final set of options
1623 * so that cache mode etc. can be inherited.
1624 *
1625 * Adding the driver later is somewhat ugly, but it's not an option
1626 * that would ever be inherited, so it's correct. We just need to make
1627 * sure to update both bs->options (which has the full effective
1628 * options for bs) and options (which has file.* already removed).
1629 */
1630 qdict_put(bs->options, "driver", qstring_from_str(drv->format_name));
1631 qdict_put(options, "driver", qstring_from_str(drv->format_name));
Kevin Wolf76c591b2014-06-04 14:19:44 +02001632 } else if (!drv) {
Kevin Wolf17b005f2014-05-27 10:50:29 +02001633 error_setg(errp, "Must specify either driver or file");
Kevin Wolf8bfea152014-04-11 19:16:36 +02001634 goto fail;
Kevin Wolff500a6d2012-11-12 17:35:27 +01001635 }
1636
Max Reitz53a29512015-03-19 14:53:16 -04001637 /* BDRV_O_PROTOCOL must be set iff a protocol BDS is about to be created */
1638 assert(!!(flags & BDRV_O_PROTOCOL) == !!drv->bdrv_file_open);
1639 /* file must be NULL if a protocol BDS is about to be created
1640 * (the inverse results in an error message from bdrv_open_common()) */
1641 assert(!(flags & BDRV_O_PROTOCOL) || !file);
1642
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001643 /* Open the image */
Kevin Wolf82dc8b42016-01-11 19:07:50 +01001644 ret = bdrv_open_common(bs, file, options, &local_err);
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001645 if (ret < 0) {
Kevin Wolf8bfea152014-04-11 19:16:36 +02001646 goto fail;
Christoph Hellwig69873072010-01-20 18:13:25 +01001647 }
1648
Max Reitz2a05cbe2013-12-20 19:28:10 +01001649 if (file && (bs->file != file)) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02001650 bdrv_unref_child(bs, file);
Kevin Wolff500a6d2012-11-12 17:35:27 +01001651 file = NULL;
1652 }
1653
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001654 /* If there is a backing file, use it */
Paolo Bonzini9156df12012-10-18 16:49:17 +02001655 if ((flags & BDRV_O_NO_BACKING) == 0) {
Kevin Wolfd9b7b052015-01-16 18:23:41 +01001656 ret = bdrv_open_backing_file(bs, options, "backing", &local_err);
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001657 if (ret < 0) {
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001658 goto close_and_fail;
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001659 }
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001660 }
1661
Max Reitz91af7012014-07-18 20:24:56 +02001662 bdrv_refresh_filename(bs);
1663
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001664 /* Check if any unknown options were used */
Max Reitz5acd9d82014-02-18 18:33:11 +01001665 if (options && (qdict_size(options) != 0)) {
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001666 const QDictEntry *entry = qdict_first(options);
Max Reitz5acd9d82014-02-18 18:33:11 +01001667 if (flags & BDRV_O_PROTOCOL) {
1668 error_setg(errp, "Block protocol '%s' doesn't support the option "
1669 "'%s'", drv->format_name, entry->key);
1670 } else {
Max Reitzd0e46a52016-03-16 19:54:34 +01001671 error_setg(errp,
1672 "Block format '%s' does not support the option '%s'",
1673 drv->format_name, entry->key);
Max Reitz5acd9d82014-02-18 18:33:11 +01001674 }
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001675
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001676 goto close_and_fail;
1677 }
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001678
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001679 if (!bdrv_key_required(bs)) {
Kevin Wolf5c8cab42016-02-24 15:13:35 +01001680 bdrv_parent_cb_change_media(bs, true);
Markus Armbrusterc3adb582014-03-14 09:22:48 +01001681 } else if (!runstate_check(RUN_STATE_PRELAUNCH)
1682 && !runstate_check(RUN_STATE_INMIGRATE)
1683 && !runstate_check(RUN_STATE_PAUSED)) { /* HACK */
1684 error_setg(errp,
1685 "Guest must be stopped for opening of encrypted image");
Markus Armbrusterc3adb582014-03-14 09:22:48 +01001686 goto close_and_fail;
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001687 }
1688
Markus Armbrusterc3adb582014-03-14 09:22:48 +01001689 QDECREF(options);
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02001690
1691 /* For snapshot=on, create a temporary qcow2 overlay. bs points to the
1692 * temporary snapshot afterwards. */
1693 if (snapshot_flags) {
Max Reitz66836182016-05-17 16:41:27 +02001694 BlockDriverState *snapshot_bs;
1695 snapshot_bs = bdrv_append_temp_snapshot(bs, snapshot_flags,
1696 snapshot_options, &local_err);
Kevin Wolf73176be2016-03-07 13:02:15 +01001697 snapshot_options = NULL;
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02001698 if (local_err) {
1699 goto close_and_fail;
1700 }
Max Reitz5b363932016-05-17 16:41:31 +02001701 /* We are not going to return bs but the overlay on top of it
1702 * (snapshot_bs); thus, we have to drop the strong reference to bs
1703 * (which we obtained by calling bdrv_new()). bs will not be deleted,
1704 * though, because the overlay still has a reference to it. */
1705 bdrv_unref(bs);
1706 bs = snapshot_bs;
Max Reitz66836182016-05-17 16:41:27 +02001707 }
1708
Max Reitz5b363932016-05-17 16:41:31 +02001709 return bs;
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001710
Kevin Wolf8bfea152014-04-11 19:16:36 +02001711fail:
Kevin Wolff500a6d2012-11-12 17:35:27 +01001712 if (file != NULL) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02001713 bdrv_unref_child(bs, file);
Kevin Wolff500a6d2012-11-12 17:35:27 +01001714 }
Kevin Wolf73176be2016-03-07 13:02:15 +01001715 QDECREF(snapshot_options);
Kevin Wolf145f5982015-05-08 16:15:03 +02001716 QDECREF(bs->explicit_options);
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001717 QDECREF(bs->options);
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001718 QDECREF(options);
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001719 bs->options = NULL;
Max Reitz5b363932016-05-17 16:41:31 +02001720 bdrv_unref(bs);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001721 if (local_err) {
Max Reitz34b5d2c2013-09-05 14:45:29 +02001722 error_propagate(errp, local_err);
1723 }
Max Reitz5b363932016-05-17 16:41:31 +02001724 return NULL;
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01001725
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001726close_and_fail:
Max Reitz5b363932016-05-17 16:41:31 +02001727 bdrv_unref(bs);
Kevin Wolf73176be2016-03-07 13:02:15 +01001728 QDECREF(snapshot_options);
Kevin Wolfb6ad4912013-03-15 10:35:04 +01001729 QDECREF(options);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001730 if (local_err) {
Max Reitz34b5d2c2013-09-05 14:45:29 +02001731 error_propagate(errp, local_err);
1732 }
Max Reitz5b363932016-05-17 16:41:31 +02001733 return NULL;
Kevin Wolfb6ce07a2010-04-12 16:37:13 +02001734}
1735
Max Reitz5b363932016-05-17 16:41:31 +02001736BlockDriverState *bdrv_open(const char *filename, const char *reference,
1737 QDict *options, int flags, Error **errp)
Kevin Wolff3930ed2015-04-08 13:43:47 +02001738{
Max Reitz5b363932016-05-17 16:41:31 +02001739 return bdrv_open_inherit(filename, reference, options, flags, NULL,
Max Reitzce343772015-08-26 19:47:50 +02001740 NULL, errp);
Kevin Wolff3930ed2015-04-08 13:43:47 +02001741}
1742
Jeff Codye971aa12012-09-20 15:13:19 -04001743typedef struct BlockReopenQueueEntry {
1744 bool prepared;
1745 BDRVReopenState state;
1746 QSIMPLEQ_ENTRY(BlockReopenQueueEntry) entry;
1747} BlockReopenQueueEntry;
1748
1749/*
1750 * Adds a BlockDriverState to a simple queue for an atomic, transactional
1751 * reopen of multiple devices.
1752 *
1753 * bs_queue can either be an existing BlockReopenQueue that has had QSIMPLE_INIT
1754 * already performed, or alternatively may be NULL a new BlockReopenQueue will
1755 * be created and initialized. This newly created BlockReopenQueue should be
1756 * passed back in for subsequent calls that are intended to be of the same
1757 * atomic 'set'.
1758 *
1759 * bs is the BlockDriverState to add to the reopen queue.
1760 *
Kevin Wolf4d2cb092015-04-10 17:50:50 +02001761 * options contains the changed options for the associated bs
1762 * (the BlockReopenQueue takes ownership)
1763 *
Jeff Codye971aa12012-09-20 15:13:19 -04001764 * flags contains the open flags for the associated bs
1765 *
1766 * returns a pointer to bs_queue, which is either the newly allocated
1767 * bs_queue, or the existing bs_queue being used.
1768 *
1769 */
Kevin Wolf28518102015-05-08 17:07:31 +02001770static BlockReopenQueue *bdrv_reopen_queue_child(BlockReopenQueue *bs_queue,
1771 BlockDriverState *bs,
1772 QDict *options,
1773 int flags,
1774 const BdrvChildRole *role,
1775 QDict *parent_options,
1776 int parent_flags)
Jeff Codye971aa12012-09-20 15:13:19 -04001777{
1778 assert(bs != NULL);
1779
1780 BlockReopenQueueEntry *bs_entry;
Kevin Wolf67251a32015-04-09 18:54:04 +02001781 BdrvChild *child;
Kevin Wolf145f5982015-05-08 16:15:03 +02001782 QDict *old_options, *explicit_options;
Kevin Wolf67251a32015-04-09 18:54:04 +02001783
Jeff Codye971aa12012-09-20 15:13:19 -04001784 if (bs_queue == NULL) {
1785 bs_queue = g_new0(BlockReopenQueue, 1);
1786 QSIMPLEQ_INIT(bs_queue);
1787 }
1788
Kevin Wolf4d2cb092015-04-10 17:50:50 +02001789 if (!options) {
1790 options = qdict_new();
1791 }
1792
Kevin Wolf28518102015-05-08 17:07:31 +02001793 /*
1794 * Precedence of options:
1795 * 1. Explicitly passed in options (highest)
Kevin Wolf91a097e2015-05-08 17:49:53 +02001796 * 2. Set in flags (only for top level)
Kevin Wolf145f5982015-05-08 16:15:03 +02001797 * 3. Retained from explicitly set options of bs
Kevin Wolf8e2160e2015-04-29 17:29:39 +02001798 * 4. Inherited from parent node
Kevin Wolf28518102015-05-08 17:07:31 +02001799 * 5. Retained from effective options of bs
1800 */
1801
Kevin Wolf91a097e2015-05-08 17:49:53 +02001802 if (!parent_options) {
1803 /*
1804 * Any setting represented by flags is always updated. If the
1805 * corresponding QDict option is set, it takes precedence. Otherwise
1806 * the flag is translated into a QDict option. The old setting of bs is
1807 * not considered.
1808 */
1809 update_options_from_flags(options, flags);
1810 }
1811
Kevin Wolf145f5982015-05-08 16:15:03 +02001812 /* Old explicitly set values (don't overwrite by inherited value) */
1813 old_options = qdict_clone_shallow(bs->explicit_options);
1814 bdrv_join_options(bs, options, old_options);
1815 QDECREF(old_options);
1816
1817 explicit_options = qdict_clone_shallow(options);
1818
Kevin Wolf28518102015-05-08 17:07:31 +02001819 /* Inherit from parent node */
1820 if (parent_options) {
1821 assert(!flags);
Kevin Wolf8e2160e2015-04-29 17:29:39 +02001822 role->inherit_options(&flags, options, parent_flags, parent_options);
Kevin Wolf28518102015-05-08 17:07:31 +02001823 }
1824
1825 /* Old values are used for options that aren't set yet */
Kevin Wolf4d2cb092015-04-10 17:50:50 +02001826 old_options = qdict_clone_shallow(bs->options);
Kevin Wolfcddff5b2015-11-16 16:43:27 +01001827 bdrv_join_options(bs, options, old_options);
Kevin Wolf4d2cb092015-04-10 17:50:50 +02001828 QDECREF(old_options);
1829
Kevin Wolff1f25a22014-04-25 19:04:55 +02001830 /* bdrv_open() masks this flag out */
1831 flags &= ~BDRV_O_PROTOCOL;
1832
Kevin Wolf67251a32015-04-09 18:54:04 +02001833 QLIST_FOREACH(child, &bs->children, next) {
Kevin Wolf4c9dfe52015-05-08 15:14:15 +02001834 QDict *new_child_options;
1835 char *child_key_dot;
Kevin Wolf67251a32015-04-09 18:54:04 +02001836
Kevin Wolf4c9dfe52015-05-08 15:14:15 +02001837 /* reopen can only change the options of block devices that were
1838 * implicitly created and inherited options. For other (referenced)
1839 * block devices, a syntax like "backing.foo" results in an error. */
Kevin Wolf67251a32015-04-09 18:54:04 +02001840 if (child->bs->inherits_from != bs) {
1841 continue;
1842 }
1843
Kevin Wolf4c9dfe52015-05-08 15:14:15 +02001844 child_key_dot = g_strdup_printf("%s.", child->name);
1845 qdict_extract_subqdict(options, &new_child_options, child_key_dot);
1846 g_free(child_key_dot);
1847
Kevin Wolf28518102015-05-08 17:07:31 +02001848 bdrv_reopen_queue_child(bs_queue, child->bs, new_child_options, 0,
1849 child->role, options, flags);
Jeff Codye971aa12012-09-20 15:13:19 -04001850 }
1851
1852 bs_entry = g_new0(BlockReopenQueueEntry, 1);
1853 QSIMPLEQ_INSERT_TAIL(bs_queue, bs_entry, entry);
1854
1855 bs_entry->state.bs = bs;
Kevin Wolf4d2cb092015-04-10 17:50:50 +02001856 bs_entry->state.options = options;
Kevin Wolf145f5982015-05-08 16:15:03 +02001857 bs_entry->state.explicit_options = explicit_options;
Jeff Codye971aa12012-09-20 15:13:19 -04001858 bs_entry->state.flags = flags;
1859
1860 return bs_queue;
1861}
1862
Kevin Wolf28518102015-05-08 17:07:31 +02001863BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue,
1864 BlockDriverState *bs,
1865 QDict *options, int flags)
1866{
1867 return bdrv_reopen_queue_child(bs_queue, bs, options, flags,
1868 NULL, NULL, 0);
1869}
1870
Jeff Codye971aa12012-09-20 15:13:19 -04001871/*
1872 * Reopen multiple BlockDriverStates atomically & transactionally.
1873 *
1874 * The queue passed in (bs_queue) must have been built up previous
1875 * via bdrv_reopen_queue().
1876 *
1877 * Reopens all BDS specified in the queue, with the appropriate
1878 * flags. All devices are prepared for reopen, and failure of any
1879 * device will cause all device changes to be abandonded, and intermediate
1880 * data cleaned up.
1881 *
1882 * If all devices prepare successfully, then the changes are committed
1883 * to all devices.
1884 *
1885 */
1886int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp)
1887{
1888 int ret = -1;
1889 BlockReopenQueueEntry *bs_entry, *next;
1890 Error *local_err = NULL;
1891
1892 assert(bs_queue != NULL);
1893
1894 bdrv_drain_all();
1895
1896 QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) {
1897 if (bdrv_reopen_prepare(&bs_entry->state, bs_queue, &local_err)) {
1898 error_propagate(errp, local_err);
1899 goto cleanup;
1900 }
1901 bs_entry->prepared = true;
1902 }
1903
1904 /* If we reach this point, we have success and just need to apply the
1905 * changes
1906 */
1907 QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) {
1908 bdrv_reopen_commit(&bs_entry->state);
1909 }
1910
1911 ret = 0;
1912
1913cleanup:
1914 QSIMPLEQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) {
1915 if (ret && bs_entry->prepared) {
1916 bdrv_reopen_abort(&bs_entry->state);
Kevin Wolf145f5982015-05-08 16:15:03 +02001917 } else if (ret) {
1918 QDECREF(bs_entry->state.explicit_options);
Jeff Codye971aa12012-09-20 15:13:19 -04001919 }
Kevin Wolf4d2cb092015-04-10 17:50:50 +02001920 QDECREF(bs_entry->state.options);
Jeff Codye971aa12012-09-20 15:13:19 -04001921 g_free(bs_entry);
1922 }
1923 g_free(bs_queue);
1924 return ret;
1925}
1926
1927
1928/* Reopen a single BlockDriverState with the specified flags. */
1929int bdrv_reopen(BlockDriverState *bs, int bdrv_flags, Error **errp)
1930{
1931 int ret = -1;
1932 Error *local_err = NULL;
Kevin Wolf4d2cb092015-04-10 17:50:50 +02001933 BlockReopenQueue *queue = bdrv_reopen_queue(NULL, bs, NULL, bdrv_flags);
Jeff Codye971aa12012-09-20 15:13:19 -04001934
1935 ret = bdrv_reopen_multiple(queue, &local_err);
1936 if (local_err != NULL) {
1937 error_propagate(errp, local_err);
1938 }
1939 return ret;
1940}
1941
1942
1943/*
1944 * Prepares a BlockDriverState for reopen. All changes are staged in the
1945 * 'opaque' field of the BDRVReopenState, which is used and allocated by
1946 * the block driver layer .bdrv_reopen_prepare()
1947 *
1948 * bs is the BlockDriverState to reopen
1949 * flags are the new open flags
1950 * queue is the reopen queue
1951 *
1952 * Returns 0 on success, non-zero on error. On error errp will be set
1953 * as well.
1954 *
1955 * On failure, bdrv_reopen_abort() will be called to clean up any data.
1956 * It is the responsibility of the caller to then call the abort() or
1957 * commit() for any other BDS that have been left in a prepare() state
1958 *
1959 */
1960int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue,
1961 Error **errp)
1962{
1963 int ret = -1;
1964 Error *local_err = NULL;
1965 BlockDriver *drv;
Kevin Wolfccf9dc02015-05-08 17:24:56 +02001966 QemuOpts *opts;
1967 const char *value;
Jeff Codye971aa12012-09-20 15:13:19 -04001968
1969 assert(reopen_state != NULL);
1970 assert(reopen_state->bs->drv != NULL);
1971 drv = reopen_state->bs->drv;
1972
Kevin Wolfccf9dc02015-05-08 17:24:56 +02001973 /* Process generic block layer options */
1974 opts = qemu_opts_create(&bdrv_runtime_opts, NULL, 0, &error_abort);
1975 qemu_opts_absorb_qdict(opts, reopen_state->options, &local_err);
1976 if (local_err) {
1977 error_propagate(errp, local_err);
1978 ret = -EINVAL;
1979 goto error;
1980 }
1981
Kevin Wolf91a097e2015-05-08 17:49:53 +02001982 update_flags_from_options(&reopen_state->flags, opts);
1983
Kevin Wolfccf9dc02015-05-08 17:24:56 +02001984 /* node-name and driver must be unchanged. Put them back into the QDict, so
1985 * that they are checked at the end of this function. */
1986 value = qemu_opt_get(opts, "node-name");
1987 if (value) {
1988 qdict_put(reopen_state->options, "node-name", qstring_from_str(value));
1989 }
1990
1991 value = qemu_opt_get(opts, "driver");
1992 if (value) {
1993 qdict_put(reopen_state->options, "driver", qstring_from_str(value));
1994 }
1995
Jeff Codye971aa12012-09-20 15:13:19 -04001996 /* if we are to stay read-only, do not allow permission change
1997 * to r/w */
1998 if (!(reopen_state->bs->open_flags & BDRV_O_ALLOW_RDWR) &&
1999 reopen_state->flags & BDRV_O_RDWR) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03002000 error_setg(errp, "Node '%s' is read only",
2001 bdrv_get_device_or_node_name(reopen_state->bs));
Jeff Codye971aa12012-09-20 15:13:19 -04002002 goto error;
2003 }
2004
2005
2006 ret = bdrv_flush(reopen_state->bs);
2007 if (ret) {
Eric Blake455b0fd2015-11-10 23:51:20 -07002008 error_setg_errno(errp, -ret, "Error flushing drive");
Jeff Codye971aa12012-09-20 15:13:19 -04002009 goto error;
2010 }
2011
2012 if (drv->bdrv_reopen_prepare) {
2013 ret = drv->bdrv_reopen_prepare(reopen_state, queue, &local_err);
2014 if (ret) {
2015 if (local_err != NULL) {
2016 error_propagate(errp, local_err);
2017 } else {
Luiz Capitulinod8b68952013-06-10 11:29:27 -04002018 error_setg(errp, "failed while preparing to reopen image '%s'",
2019 reopen_state->bs->filename);
Jeff Codye971aa12012-09-20 15:13:19 -04002020 }
2021 goto error;
2022 }
2023 } else {
2024 /* It is currently mandatory to have a bdrv_reopen_prepare()
2025 * handler for each supported drv. */
Alberto Garcia81e5f782015-04-08 12:29:19 +03002026 error_setg(errp, "Block format '%s' used by node '%s' "
2027 "does not support reopening files", drv->format_name,
2028 bdrv_get_device_or_node_name(reopen_state->bs));
Jeff Codye971aa12012-09-20 15:13:19 -04002029 ret = -1;
2030 goto error;
2031 }
2032
Kevin Wolf4d2cb092015-04-10 17:50:50 +02002033 /* Options that are not handled are only okay if they are unchanged
2034 * compared to the old state. It is expected that some options are only
2035 * used for the initial open, but not reopen (e.g. filename) */
2036 if (qdict_size(reopen_state->options)) {
2037 const QDictEntry *entry = qdict_first(reopen_state->options);
2038
2039 do {
2040 QString *new_obj = qobject_to_qstring(entry->value);
2041 const char *new = qstring_get_str(new_obj);
2042 const char *old = qdict_get_try_str(reopen_state->bs->options,
2043 entry->key);
2044
2045 if (!old || strcmp(new, old)) {
2046 error_setg(errp, "Cannot change the option '%s'", entry->key);
2047 ret = -EINVAL;
2048 goto error;
2049 }
2050 } while ((entry = qdict_next(reopen_state->options, entry)));
2051 }
2052
Jeff Codye971aa12012-09-20 15:13:19 -04002053 ret = 0;
2054
2055error:
Kevin Wolfccf9dc02015-05-08 17:24:56 +02002056 qemu_opts_del(opts);
Jeff Codye971aa12012-09-20 15:13:19 -04002057 return ret;
2058}
2059
2060/*
2061 * Takes the staged changes for the reopen from bdrv_reopen_prepare(), and
2062 * makes them final by swapping the staging BlockDriverState contents into
2063 * the active BlockDriverState contents.
2064 */
2065void bdrv_reopen_commit(BDRVReopenState *reopen_state)
2066{
2067 BlockDriver *drv;
2068
2069 assert(reopen_state != NULL);
2070 drv = reopen_state->bs->drv;
2071 assert(drv != NULL);
2072
2073 /* If there are any driver level actions to take */
2074 if (drv->bdrv_reopen_commit) {
2075 drv->bdrv_reopen_commit(reopen_state);
2076 }
2077
2078 /* set BDS specific flags now */
Kevin Wolf145f5982015-05-08 16:15:03 +02002079 QDECREF(reopen_state->bs->explicit_options);
2080
2081 reopen_state->bs->explicit_options = reopen_state->explicit_options;
Jeff Codye971aa12012-09-20 15:13:19 -04002082 reopen_state->bs->open_flags = reopen_state->flags;
Jeff Codye971aa12012-09-20 15:13:19 -04002083 reopen_state->bs->read_only = !(reopen_state->flags & BDRV_O_RDWR);
Kevin Wolf355ef4a2013-12-11 20:14:09 +01002084
Kevin Wolf3baca892014-07-16 17:48:16 +02002085 bdrv_refresh_limits(reopen_state->bs, NULL);
Jeff Codye971aa12012-09-20 15:13:19 -04002086}
2087
2088/*
2089 * Abort the reopen, and delete and free the staged changes in
2090 * reopen_state
2091 */
2092void bdrv_reopen_abort(BDRVReopenState *reopen_state)
2093{
2094 BlockDriver *drv;
2095
2096 assert(reopen_state != NULL);
2097 drv = reopen_state->bs->drv;
2098 assert(drv != NULL);
2099
2100 if (drv->bdrv_reopen_abort) {
2101 drv->bdrv_reopen_abort(reopen_state);
2102 }
Kevin Wolf145f5982015-05-08 16:15:03 +02002103
2104 QDECREF(reopen_state->explicit_options);
Jeff Codye971aa12012-09-20 15:13:19 -04002105}
2106
2107
Max Reitz64dff522016-01-29 16:36:10 +01002108static void bdrv_close(BlockDriverState *bs)
bellardfc01f7e2003-06-30 10:03:06 +00002109{
Max Reitz33384422014-06-20 21:57:33 +02002110 BdrvAioNotifier *ban, *ban_next;
2111
Max Reitzca9bd242016-01-29 16:36:14 +01002112 assert(!bs->job);
Alberto Garcia99b7e772015-09-25 16:41:44 +03002113
Paolo Bonzinifc272912015-12-23 11:48:24 +01002114 bdrv_drained_begin(bs); /* complete I/O */
Stefan Hajnoczi58fda172013-07-02 15:36:25 +02002115 bdrv_flush(bs);
Fam Zheng53ec73e2015-05-29 18:53:14 +08002116 bdrv_drain(bs); /* in case flush left pending I/O */
Paolo Bonzinifc272912015-12-23 11:48:24 +01002117
Max Reitzc5acdc92016-01-29 16:36:01 +01002118 bdrv_release_named_dirty_bitmaps(bs);
2119 assert(QLIST_EMPTY(&bs->dirty_bitmaps));
2120
Kevin Wolf5c8cab42016-02-24 15:13:35 +01002121 bdrv_parent_cb_change_media(bs, false);
Max Reitzb4d02822015-10-19 17:53:15 +02002122
Paolo Bonzini3cbc0022012-10-19 11:36:48 +02002123 if (bs->drv) {
Kevin Wolf6e93e7c2015-04-08 13:49:41 +02002124 BdrvChild *child, *next;
2125
Kevin Wolf9a7dedb2015-06-16 10:58:20 +02002126 bs->drv->bdrv_close(bs);
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02002127 bs->drv = NULL;
Kevin Wolf9a7dedb2015-06-16 10:58:20 +02002128
Kevin Wolf5db15a52015-09-14 15:33:33 +02002129 bdrv_set_backing_hd(bs, NULL);
Kevin Wolf9a7dedb2015-06-16 10:58:20 +02002130
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02002131 if (bs->file != NULL) {
2132 bdrv_unref_child(bs, bs->file);
2133 bs->file = NULL;
2134 }
2135
Kevin Wolf6e93e7c2015-04-08 13:49:41 +02002136 QLIST_FOREACH_SAFE(child, &bs->children, next, next) {
Kevin Wolf33a60402015-06-15 13:51:04 +02002137 /* TODO Remove bdrv_unref() from drivers' close function and use
2138 * bdrv_unref_child() here */
Kevin Wolfbddcec32015-04-09 18:47:50 +02002139 if (child->bs->inherits_from == bs) {
2140 child->bs->inherits_from = NULL;
2141 }
Kevin Wolf33a60402015-06-15 13:51:04 +02002142 bdrv_detach_child(child);
Kevin Wolf6e93e7c2015-04-08 13:49:41 +02002143 }
2144
Anthony Liguori7267c092011-08-20 22:09:37 -05002145 g_free(bs->opaque);
bellardea2384d2004-08-01 21:59:26 +00002146 bs->opaque = NULL;
Stefan Hajnoczi53fec9d2011-11-28 16:08:47 +00002147 bs->copy_on_read = 0;
Paolo Bonzinia275fa42012-05-08 16:51:43 +02002148 bs->backing_file[0] = '\0';
2149 bs->backing_format[0] = '\0';
Paolo Bonzini64058752012-05-08 16:51:49 +02002150 bs->total_sectors = 0;
2151 bs->encrypted = 0;
2152 bs->valid_key = 0;
2153 bs->sg = 0;
Asias He0d51b4d2013-08-22 15:24:14 +08002154 bs->zero_beyond_eof = false;
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01002155 QDECREF(bs->options);
Kevin Wolf145f5982015-05-08 16:15:03 +02002156 QDECREF(bs->explicit_options);
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01002157 bs->options = NULL;
Max Reitz91af7012014-07-18 20:24:56 +02002158 QDECREF(bs->full_open_options);
2159 bs->full_open_options = NULL;
bellardb3380822004-03-14 21:38:54 +00002160 }
Zhi Yong Wu98f90db2011-11-08 13:00:14 +08002161
Max Reitz33384422014-06-20 21:57:33 +02002162 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) {
2163 g_free(ban);
2164 }
2165 QLIST_INIT(&bs->aio_notifiers);
Paolo Bonzinifc272912015-12-23 11:48:24 +01002166 bdrv_drained_end(bs);
bellardb3380822004-03-14 21:38:54 +00002167}
2168
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +09002169void bdrv_close_all(void)
2170{
2171 BlockDriverState *bs;
Max Reitzca9bd242016-01-29 16:36:14 +01002172 AioContext *aio_context;
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +09002173
Max Reitzca9bd242016-01-29 16:36:14 +01002174 /* Drop references from requests still in flight, such as canceled block
2175 * jobs whose AIO context has not been polled yet */
2176 bdrv_drain_all();
Stefan Hajnoczied78cda2014-05-08 16:34:35 +02002177
Max Reitzca9bd242016-01-29 16:36:14 +01002178 blk_remove_all_bs();
2179 blockdev_close_all_bdrv_states();
2180
2181 /* Cancel all block jobs */
2182 while (!QTAILQ_EMPTY(&all_bdrv_states)) {
2183 QTAILQ_FOREACH(bs, &all_bdrv_states, bs_list) {
2184 aio_context = bdrv_get_aio_context(bs);
2185
2186 aio_context_acquire(aio_context);
2187 if (bs->job) {
2188 block_job_cancel_sync(bs->job);
2189 aio_context_release(aio_context);
2190 break;
2191 }
2192 aio_context_release(aio_context);
2193 }
2194
2195 /* All the remaining BlockDriverStates are referenced directly or
2196 * indirectly from block jobs, so there needs to be at least one BDS
2197 * directly used by a block job */
2198 assert(bs);
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +09002199 }
2200}
2201
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002202static void change_parent_backing_link(BlockDriverState *from,
2203 BlockDriverState *to)
2204{
2205 BdrvChild *c, *next;
2206
2207 QLIST_FOREACH_SAFE(c, &from->parents, next_parent, next) {
2208 assert(c->role != &child_backing);
2209 c->bs = to;
2210 QLIST_REMOVE(c, next_parent);
2211 QLIST_INSERT_HEAD(&to->parents, c, next_parent);
2212 bdrv_ref(to);
2213 bdrv_unref(from);
2214 }
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002215}
2216
Jeff Cody8802d1f2012-02-28 15:54:06 -05002217/*
2218 * Add new bs contents at the top of an image chain while the chain is
2219 * live, while keeping required fields on the top layer.
2220 *
2221 * This will modify the BlockDriverState fields, and swap contents
2222 * between bs_new and bs_top. Both bs_new and bs_top are modified.
2223 *
Markus Armbrusterbfb197e2014-10-07 13:59:11 +02002224 * bs_new must not be attached to a BlockBackend.
Jeff Codyf6801b82012-03-27 16:30:19 -04002225 *
Jeff Cody8802d1f2012-02-28 15:54:06 -05002226 * This function does not create any image files.
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002227 *
2228 * bdrv_append() takes ownership of a bs_new reference and unrefs it because
2229 * that's what the callers commonly need. bs_new will be referenced by the old
2230 * parents of bs_top after bdrv_append() returns. If the caller needs to keep a
2231 * reference of its own, it must call bdrv_ref().
Jeff Cody8802d1f2012-02-28 15:54:06 -05002232 */
2233void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top)
2234{
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002235 assert(!bdrv_requests_pending(bs_top));
2236 assert(!bdrv_requests_pending(bs_new));
Jeff Cody8802d1f2012-02-28 15:54:06 -05002237
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002238 bdrv_ref(bs_top);
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002239
Kevin Wolf27ccdd52016-03-21 12:56:44 +01002240 change_parent_backing_link(bs_top, bs_new);
Kevin Wolfdd62f1c2015-06-18 14:09:57 +02002241 bdrv_set_backing_hd(bs_new, bs_top);
2242 bdrv_unref(bs_top);
2243
2244 /* bs_new is now referenced by its new parents, we don't need the
2245 * additional reference any more. */
2246 bdrv_unref(bs_new);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002247}
2248
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02002249void bdrv_replace_in_backing_chain(BlockDriverState *old, BlockDriverState *new)
2250{
2251 assert(!bdrv_requests_pending(old));
2252 assert(!bdrv_requests_pending(new));
2253
2254 bdrv_ref(old);
2255
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02002256 change_parent_backing_link(old, new);
2257
2258 /* Change backing files if a previously independent node is added to the
2259 * chain. For active commit, we replace top by its own (indirect) backing
2260 * file and don't do anything here so we don't build a loop. */
2261 if (new->backing == NULL && !bdrv_chain_contains(backing_bs(old), new)) {
2262 bdrv_set_backing_hd(new, backing_bs(old));
2263 bdrv_set_backing_hd(old, NULL);
2264 }
2265
2266 bdrv_unref(old);
2267}
2268
Fam Zheng4f6fd342013-08-23 09:14:47 +08002269static void bdrv_delete(BlockDriverState *bs)
bellardb3380822004-03-14 21:38:54 +00002270{
Paolo Bonzini3e914652012-03-30 13:17:11 +02002271 assert(!bs->job);
Fam Zheng3718d8a2014-05-23 21:29:43 +08002272 assert(bdrv_op_blocker_is_empty(bs));
Fam Zheng4f6fd342013-08-23 09:14:47 +08002273 assert(!bs->refcnt);
Markus Armbruster18846de2010-06-29 16:58:30 +02002274
Stefan Hajnoczie1b5c522013-06-27 15:32:26 +02002275 bdrv_close(bs);
2276
Stefan Hajnoczi1b7bdbc2010-04-10 07:02:42 +01002277 /* remove from list, if necessary */
Kevin Wolf63eaaae2016-03-18 10:46:57 +01002278 if (bs->node_name[0] != '\0') {
2279 QTAILQ_REMOVE(&graph_bdrv_states, bs, node_list);
2280 }
Max Reitz2c1d04e2016-01-29 16:36:11 +01002281 QTAILQ_REMOVE(&all_bdrv_states, bs, bs_list);
2282
Anthony Liguori7267c092011-08-20 22:09:37 -05002283 g_free(bs);
bellardfc01f7e2003-06-30 10:03:06 +00002284}
2285
aliguorie97fc192009-04-21 23:11:50 +00002286/*
2287 * Run consistency checks on an image
2288 *
Kevin Wolfe076f332010-06-29 11:43:13 +02002289 * Returns 0 if the check could be completed (it doesn't mean that the image is
Stefan Weila1c72732011-04-28 17:20:38 +02002290 * free of errors) or -errno when an internal error occurred. The results of the
Kevin Wolfe076f332010-06-29 11:43:13 +02002291 * check are stored in res.
aliguorie97fc192009-04-21 23:11:50 +00002292 */
Kevin Wolf4534ff52012-05-11 16:07:02 +02002293int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix)
aliguorie97fc192009-04-21 23:11:50 +00002294{
Max Reitz908bcd52014-08-07 22:47:55 +02002295 if (bs->drv == NULL) {
2296 return -ENOMEDIUM;
2297 }
aliguorie97fc192009-04-21 23:11:50 +00002298 if (bs->drv->bdrv_check == NULL) {
2299 return -ENOTSUP;
2300 }
2301
Kevin Wolfe076f332010-06-29 11:43:13 +02002302 memset(res, 0, sizeof(*res));
Kevin Wolf4534ff52012-05-11 16:07:02 +02002303 return bs->drv->bdrv_check(bs, res, fix);
aliguorie97fc192009-04-21 23:11:50 +00002304}
2305
Kevin Wolf8a426612010-07-16 17:17:01 +02002306#define COMMIT_BUF_SECTORS 2048
2307
bellard33e39632003-07-06 17:15:21 +00002308/* commit COW file into the raw image */
2309int bdrv_commit(BlockDriverState *bs)
2310{
bellard19cb3732006-08-19 11:45:59 +00002311 BlockDriver *drv = bs->drv;
Jeff Cody72706ea2014-01-24 09:02:35 -05002312 int64_t sector, total_sectors, length, backing_length;
Kevin Wolf8a426612010-07-16 17:17:01 +02002313 int n, ro, open_flags;
Jeff Cody0bce5972012-09-20 15:13:34 -04002314 int ret = 0;
Jeff Cody72706ea2014-01-24 09:02:35 -05002315 uint8_t *buf = NULL;
bellard33e39632003-07-06 17:15:21 +00002316
bellard19cb3732006-08-19 11:45:59 +00002317 if (!drv)
2318 return -ENOMEDIUM;
Liu Yuan6bb45152014-09-01 13:35:21 +08002319
Kevin Wolf760e0062015-06-17 14:55:21 +02002320 if (!bs->backing) {
Naphtali Sprei4dca4b62010-02-14 13:39:18 +02002321 return -ENOTSUP;
bellard33e39632003-07-06 17:15:21 +00002322 }
2323
Fam Zhengbb000212014-09-11 13:14:00 +08002324 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, NULL) ||
Kevin Wolf760e0062015-06-17 14:55:21 +02002325 bdrv_op_is_blocked(bs->backing->bs, BLOCK_OP_TYPE_COMMIT_TARGET, NULL)) {
Stefan Hajnoczi2d3735d2012-01-18 14:40:41 +00002326 return -EBUSY;
2327 }
2328
Kevin Wolf760e0062015-06-17 14:55:21 +02002329 ro = bs->backing->bs->read_only;
2330 open_flags = bs->backing->bs->open_flags;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +02002331
2332 if (ro) {
Kevin Wolf760e0062015-06-17 14:55:21 +02002333 if (bdrv_reopen(bs->backing->bs, open_flags | BDRV_O_RDWR, NULL)) {
Jeff Cody0bce5972012-09-20 15:13:34 -04002334 return -EACCES;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +02002335 }
bellard33e39632003-07-06 17:15:21 +00002336 }
bellardea2384d2004-08-01 21:59:26 +00002337
Jeff Cody72706ea2014-01-24 09:02:35 -05002338 length = bdrv_getlength(bs);
2339 if (length < 0) {
2340 ret = length;
2341 goto ro_cleanup;
2342 }
2343
Kevin Wolf760e0062015-06-17 14:55:21 +02002344 backing_length = bdrv_getlength(bs->backing->bs);
Jeff Cody72706ea2014-01-24 09:02:35 -05002345 if (backing_length < 0) {
2346 ret = backing_length;
2347 goto ro_cleanup;
2348 }
2349
2350 /* If our top snapshot is larger than the backing file image,
2351 * grow the backing file image if possible. If not possible,
2352 * we must return an error */
2353 if (length > backing_length) {
Kevin Wolf760e0062015-06-17 14:55:21 +02002354 ret = bdrv_truncate(bs->backing->bs, length);
Jeff Cody72706ea2014-01-24 09:02:35 -05002355 if (ret < 0) {
2356 goto ro_cleanup;
2357 }
2358 }
2359
2360 total_sectors = length >> BDRV_SECTOR_BITS;
Kevin Wolf857d4f42014-05-20 13:16:51 +02002361
2362 /* qemu_try_blockalign() for bs will choose an alignment that works for
Kevin Wolf760e0062015-06-17 14:55:21 +02002363 * bs->backing->bs as well, so no need to compare the alignment manually. */
Kevin Wolf857d4f42014-05-20 13:16:51 +02002364 buf = qemu_try_blockalign(bs, COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE);
2365 if (buf == NULL) {
2366 ret = -ENOMEM;
2367 goto ro_cleanup;
2368 }
bellardea2384d2004-08-01 21:59:26 +00002369
Kevin Wolf8a426612010-07-16 17:17:01 +02002370 for (sector = 0; sector < total_sectors; sector += n) {
Paolo Bonzinid6636402013-09-04 19:00:25 +02002371 ret = bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n);
2372 if (ret < 0) {
2373 goto ro_cleanup;
2374 }
2375 if (ret) {
Kevin Wolfdabfa6c2014-01-24 14:00:43 +01002376 ret = bdrv_read(bs, sector, buf, n);
2377 if (ret < 0) {
Kevin Wolf8a426612010-07-16 17:17:01 +02002378 goto ro_cleanup;
2379 }
2380
Kevin Wolf760e0062015-06-17 14:55:21 +02002381 ret = bdrv_write(bs->backing->bs, sector, buf, n);
Kevin Wolfdabfa6c2014-01-24 14:00:43 +01002382 if (ret < 0) {
Kevin Wolf8a426612010-07-16 17:17:01 +02002383 goto ro_cleanup;
2384 }
bellardea2384d2004-08-01 21:59:26 +00002385 }
2386 }
bellard95389c82005-12-18 18:28:15 +00002387
Christoph Hellwig1d449522010-01-17 12:32:30 +01002388 if (drv->bdrv_make_empty) {
2389 ret = drv->bdrv_make_empty(bs);
Kevin Wolfdabfa6c2014-01-24 14:00:43 +01002390 if (ret < 0) {
2391 goto ro_cleanup;
2392 }
Christoph Hellwig1d449522010-01-17 12:32:30 +01002393 bdrv_flush(bs);
2394 }
bellard95389c82005-12-18 18:28:15 +00002395
Christoph Hellwig3f5075a2010-01-12 13:49:23 +01002396 /*
2397 * Make sure all data we wrote to the backing device is actually
2398 * stable on disk.
2399 */
Kevin Wolf760e0062015-06-17 14:55:21 +02002400 if (bs->backing) {
2401 bdrv_flush(bs->backing->bs);
Kevin Wolfdabfa6c2014-01-24 14:00:43 +01002402 }
Naphtali Sprei4dca4b62010-02-14 13:39:18 +02002403
Kevin Wolfdabfa6c2014-01-24 14:00:43 +01002404 ret = 0;
Naphtali Sprei4dca4b62010-02-14 13:39:18 +02002405ro_cleanup:
Kevin Wolf857d4f42014-05-20 13:16:51 +02002406 qemu_vfree(buf);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +02002407
2408 if (ro) {
Jeff Cody0bce5972012-09-20 15:13:34 -04002409 /* ignoring error return here */
Kevin Wolf760e0062015-06-17 14:55:21 +02002410 bdrv_reopen(bs->backing->bs, open_flags & ~BDRV_O_RDWR, NULL);
Naphtali Sprei4dca4b62010-02-14 13:39:18 +02002411 }
2412
Christoph Hellwig1d449522010-01-17 12:32:30 +01002413 return ret;
bellard33e39632003-07-06 17:15:21 +00002414}
2415
Kevin Wolf756e6732010-01-12 12:55:17 +01002416/*
2417 * Return values:
2418 * 0 - success
2419 * -EINVAL - backing format specified, but no file
2420 * -ENOSPC - can't update the backing file because no space is left in the
2421 * image file header
2422 * -ENOTSUP - format driver doesn't support changing the backing file
2423 */
2424int bdrv_change_backing_file(BlockDriverState *bs,
2425 const char *backing_file, const char *backing_fmt)
2426{
2427 BlockDriver *drv = bs->drv;
Paolo Bonzini469ef352012-04-12 14:01:02 +02002428 int ret;
Kevin Wolf756e6732010-01-12 12:55:17 +01002429
Paolo Bonzini5f377792012-04-12 14:01:01 +02002430 /* Backing file format doesn't make sense without a backing file */
2431 if (backing_fmt && !backing_file) {
2432 return -EINVAL;
2433 }
2434
Kevin Wolf756e6732010-01-12 12:55:17 +01002435 if (drv->bdrv_change_backing_file != NULL) {
Paolo Bonzini469ef352012-04-12 14:01:02 +02002436 ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
Kevin Wolf756e6732010-01-12 12:55:17 +01002437 } else {
Paolo Bonzini469ef352012-04-12 14:01:02 +02002438 ret = -ENOTSUP;
Kevin Wolf756e6732010-01-12 12:55:17 +01002439 }
Paolo Bonzini469ef352012-04-12 14:01:02 +02002440
2441 if (ret == 0) {
2442 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
2443 pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
2444 }
2445 return ret;
Kevin Wolf756e6732010-01-12 12:55:17 +01002446}
2447
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002448/*
2449 * Finds the image layer in the chain that has 'bs' as its backing file.
2450 *
2451 * active is the current topmost image.
2452 *
2453 * Returns NULL if bs is not found in active's image chain,
2454 * or if active == bs.
Jeff Cody4caf0fc2014-06-25 15:35:26 -04002455 *
2456 * Returns the bottommost base image if bs == NULL.
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002457 */
2458BlockDriverState *bdrv_find_overlay(BlockDriverState *active,
2459 BlockDriverState *bs)
2460{
Kevin Wolf760e0062015-06-17 14:55:21 +02002461 while (active && bs != backing_bs(active)) {
2462 active = backing_bs(active);
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002463 }
2464
Jeff Cody4caf0fc2014-06-25 15:35:26 -04002465 return active;
2466}
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002467
Jeff Cody4caf0fc2014-06-25 15:35:26 -04002468/* Given a BDS, searches for the base layer. */
2469BlockDriverState *bdrv_find_base(BlockDriverState *bs)
2470{
2471 return bdrv_find_overlay(bs, NULL);
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002472}
2473
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002474/*
2475 * Drops images above 'base' up to and including 'top', and sets the image
2476 * above 'top' to have base as its backing file.
2477 *
2478 * Requires that the overlay to 'top' is opened r/w, so that the backing file
2479 * information in 'bs' can be properly updated.
2480 *
2481 * E.g., this will convert the following chain:
2482 * bottom <- base <- intermediate <- top <- active
2483 *
2484 * to
2485 *
2486 * bottom <- base <- active
2487 *
2488 * It is allowed for bottom==base, in which case it converts:
2489 *
2490 * base <- intermediate <- top <- active
2491 *
2492 * to
2493 *
2494 * base <- active
2495 *
Jeff Cody54e26902014-06-25 15:40:10 -04002496 * If backing_file_str is non-NULL, it will be used when modifying top's
2497 * overlay image metadata.
2498 *
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002499 * Error conditions:
2500 * if active == top, that is considered an error
2501 *
2502 */
2503int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top,
Jeff Cody54e26902014-06-25 15:40:10 -04002504 BlockDriverState *base, const char *backing_file_str)
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002505{
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002506 BlockDriverState *new_top_bs = NULL;
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002507 int ret = -EIO;
2508
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002509 if (!top->drv || !base->drv) {
2510 goto exit;
2511 }
2512
2513 new_top_bs = bdrv_find_overlay(active, top);
2514
2515 if (new_top_bs == NULL) {
2516 /* we could not find the image above 'top', this is an error */
2517 goto exit;
2518 }
2519
Kevin Wolf760e0062015-06-17 14:55:21 +02002520 /* special case of new_top_bs->backing->bs already pointing to base - nothing
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002521 * to do, no intermediate images */
Kevin Wolf760e0062015-06-17 14:55:21 +02002522 if (backing_bs(new_top_bs) == base) {
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002523 ret = 0;
2524 goto exit;
2525 }
2526
Kevin Wolf5db15a52015-09-14 15:33:33 +02002527 /* Make sure that base is in the backing chain of top */
2528 if (!bdrv_chain_contains(top, base)) {
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002529 goto exit;
2530 }
2531
2532 /* success - we can delete the intermediate states, and link top->base */
Kevin Wolf5db15a52015-09-14 15:33:33 +02002533 backing_file_str = backing_file_str ? backing_file_str : base->filename;
Jeff Cody54e26902014-06-25 15:40:10 -04002534 ret = bdrv_change_backing_file(new_top_bs, backing_file_str,
Kevin Wolf5db15a52015-09-14 15:33:33 +02002535 base->drv ? base->drv->format_name : "");
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002536 if (ret) {
2537 goto exit;
2538 }
Kevin Wolf5db15a52015-09-14 15:33:33 +02002539 bdrv_set_backing_hd(new_top_bs, base);
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002540
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002541 ret = 0;
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002542exit:
Jeff Cody6ebdcee2012-09-27 13:29:12 -04002543 return ret;
2544}
2545
bellard83f64092006-08-01 16:21:11 +00002546/**
bellard83f64092006-08-01 16:21:11 +00002547 * Truncate file to 'offset' bytes (needed only for file protocols)
2548 */
2549int bdrv_truncate(BlockDriverState *bs, int64_t offset)
2550{
2551 BlockDriver *drv = bs->drv;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01002552 int ret;
bellard83f64092006-08-01 16:21:11 +00002553 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002554 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +00002555 if (!drv->bdrv_truncate)
2556 return -ENOTSUP;
Naphtali Sprei59f26892009-10-26 16:25:16 +02002557 if (bs->read_only)
2558 return -EACCES;
Jeff Cody9c75e162014-06-25 16:55:30 -04002559
Stefan Hajnoczi51762282010-04-19 16:56:41 +01002560 ret = drv->bdrv_truncate(bs, offset);
2561 if (ret == 0) {
2562 ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
John Snowce1ffea2015-04-17 19:50:03 -04002563 bdrv_dirty_bitmap_truncate(bs);
Kevin Wolf5c8cab42016-02-24 15:13:35 +01002564 bdrv_parent_cb_resize(bs);
Stefan Hajnoczi51762282010-04-19 16:56:41 +01002565 }
2566 return ret;
bellard83f64092006-08-01 16:21:11 +00002567}
2568
2569/**
Fam Zheng4a1d5e12011-07-12 19:56:39 +08002570 * Length of a allocated file in bytes. Sparse files are counted by actual
2571 * allocated space. Return < 0 if error or unknown.
2572 */
2573int64_t bdrv_get_allocated_file_size(BlockDriverState *bs)
2574{
2575 BlockDriver *drv = bs->drv;
2576 if (!drv) {
2577 return -ENOMEDIUM;
2578 }
2579 if (drv->bdrv_get_allocated_file_size) {
2580 return drv->bdrv_get_allocated_file_size(bs);
2581 }
2582 if (bs->file) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02002583 return bdrv_get_allocated_file_size(bs->file->bs);
Fam Zheng4a1d5e12011-07-12 19:56:39 +08002584 }
2585 return -ENOTSUP;
2586}
2587
2588/**
Markus Armbruster65a9bb22014-06-26 13:23:17 +02002589 * Return number of sectors on success, -errno on error.
bellard83f64092006-08-01 16:21:11 +00002590 */
Markus Armbruster65a9bb22014-06-26 13:23:17 +02002591int64_t bdrv_nb_sectors(BlockDriverState *bs)
bellard83f64092006-08-01 16:21:11 +00002592{
2593 BlockDriver *drv = bs->drv;
Markus Armbruster65a9bb22014-06-26 13:23:17 +02002594
bellard83f64092006-08-01 16:21:11 +00002595 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002596 return -ENOMEDIUM;
Stefan Hajnoczi51762282010-04-19 16:56:41 +01002597
Kevin Wolfb94a2612013-10-29 12:18:58 +01002598 if (drv->has_variable_length) {
2599 int ret = refresh_total_sectors(bs, bs->total_sectors);
2600 if (ret < 0) {
2601 return ret;
Stefan Hajnoczi46a4e4e2011-03-29 20:04:41 +01002602 }
bellard83f64092006-08-01 16:21:11 +00002603 }
Markus Armbruster65a9bb22014-06-26 13:23:17 +02002604 return bs->total_sectors;
2605}
2606
2607/**
2608 * Return length in bytes on success, -errno on error.
2609 * The length is always a multiple of BDRV_SECTOR_SIZE.
2610 */
2611int64_t bdrv_getlength(BlockDriverState *bs)
2612{
2613 int64_t ret = bdrv_nb_sectors(bs);
2614
Fam Zheng4a9c9ea2015-05-15 16:36:05 +08002615 ret = ret > INT64_MAX / BDRV_SECTOR_SIZE ? -EFBIG : ret;
Markus Armbruster65a9bb22014-06-26 13:23:17 +02002616 return ret < 0 ? ret : ret * BDRV_SECTOR_SIZE;
bellardfc01f7e2003-06-30 10:03:06 +00002617}
2618
bellard19cb3732006-08-19 11:45:59 +00002619/* return 0 as number of sectors if no device present or error */
ths96b8f132007-12-17 01:35:20 +00002620void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
bellardfc01f7e2003-06-30 10:03:06 +00002621{
Markus Armbruster65a9bb22014-06-26 13:23:17 +02002622 int64_t nb_sectors = bdrv_nb_sectors(bs);
2623
2624 *nb_sectors_ptr = nb_sectors < 0 ? 0 : nb_sectors;
bellardfc01f7e2003-06-30 10:03:06 +00002625}
bellardcf989512004-02-16 21:56:36 +00002626
bellardb3380822004-03-14 21:38:54 +00002627int bdrv_is_read_only(BlockDriverState *bs)
2628{
2629 return bs->read_only;
2630}
2631
ths985a03b2007-12-24 16:10:43 +00002632int bdrv_is_sg(BlockDriverState *bs)
2633{
2634 return bs->sg;
2635}
2636
bellardea2384d2004-08-01 21:59:26 +00002637int bdrv_is_encrypted(BlockDriverState *bs)
2638{
Kevin Wolf760e0062015-06-17 14:55:21 +02002639 if (bs->backing && bs->backing->bs->encrypted) {
bellardea2384d2004-08-01 21:59:26 +00002640 return 1;
Kevin Wolf760e0062015-06-17 14:55:21 +02002641 }
bellardea2384d2004-08-01 21:59:26 +00002642 return bs->encrypted;
2643}
2644
aliguoric0f4ce72009-03-05 23:01:01 +00002645int bdrv_key_required(BlockDriverState *bs)
2646{
Kevin Wolf760e0062015-06-17 14:55:21 +02002647 BdrvChild *backing = bs->backing;
aliguoric0f4ce72009-03-05 23:01:01 +00002648
Kevin Wolf760e0062015-06-17 14:55:21 +02002649 if (backing && backing->bs->encrypted && !backing->bs->valid_key) {
aliguoric0f4ce72009-03-05 23:01:01 +00002650 return 1;
Kevin Wolf760e0062015-06-17 14:55:21 +02002651 }
aliguoric0f4ce72009-03-05 23:01:01 +00002652 return (bs->encrypted && !bs->valid_key);
2653}
2654
bellardea2384d2004-08-01 21:59:26 +00002655int bdrv_set_key(BlockDriverState *bs, const char *key)
2656{
2657 int ret;
Kevin Wolf760e0062015-06-17 14:55:21 +02002658 if (bs->backing && bs->backing->bs->encrypted) {
2659 ret = bdrv_set_key(bs->backing->bs, key);
bellardea2384d2004-08-01 21:59:26 +00002660 if (ret < 0)
2661 return ret;
2662 if (!bs->encrypted)
2663 return 0;
2664 }
Shahar Havivifd04a2a2010-03-06 00:26:13 +02002665 if (!bs->encrypted) {
2666 return -EINVAL;
2667 } else if (!bs->drv || !bs->drv->bdrv_set_key) {
2668 return -ENOMEDIUM;
2669 }
aliguoric0f4ce72009-03-05 23:01:01 +00002670 ret = bs->drv->bdrv_set_key(bs, key);
aliguoribb5fc202009-03-05 23:01:15 +00002671 if (ret < 0) {
2672 bs->valid_key = 0;
2673 } else if (!bs->valid_key) {
Kevin Wolf5c8cab42016-02-24 15:13:35 +01002674 /* call the change callback now, we skipped it on open */
aliguoribb5fc202009-03-05 23:01:15 +00002675 bs->valid_key = 1;
Kevin Wolf5c8cab42016-02-24 15:13:35 +01002676 bdrv_parent_cb_change_media(bs, true);
aliguoribb5fc202009-03-05 23:01:15 +00002677 }
aliguoric0f4ce72009-03-05 23:01:01 +00002678 return ret;
bellardea2384d2004-08-01 21:59:26 +00002679}
2680
Markus Armbruster4d2855a2015-01-29 10:37:00 +01002681/*
2682 * Provide an encryption key for @bs.
2683 * If @key is non-null:
2684 * If @bs is not encrypted, fail.
2685 * Else if the key is invalid, fail.
2686 * Else set @bs's key to @key, replacing the existing key, if any.
2687 * If @key is null:
2688 * If @bs is encrypted and still lacks a key, fail.
2689 * Else do nothing.
2690 * On failure, store an error object through @errp if non-null.
2691 */
2692void bdrv_add_key(BlockDriverState *bs, const char *key, Error **errp)
2693{
2694 if (key) {
2695 if (!bdrv_is_encrypted(bs)) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03002696 error_setg(errp, "Node '%s' is not encrypted",
2697 bdrv_get_device_or_node_name(bs));
Markus Armbruster4d2855a2015-01-29 10:37:00 +01002698 } else if (bdrv_set_key(bs, key) < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002699 error_setg(errp, QERR_INVALID_PASSWORD);
Markus Armbruster4d2855a2015-01-29 10:37:00 +01002700 }
2701 } else {
2702 if (bdrv_key_required(bs)) {
Markus Armbrusterb1ca6392015-01-29 10:37:01 +01002703 error_set(errp, ERROR_CLASS_DEVICE_ENCRYPTED,
2704 "'%s' (%s) is encrypted",
Alberto Garcia81e5f782015-04-08 12:29:19 +03002705 bdrv_get_device_or_node_name(bs),
Markus Armbruster4d2855a2015-01-29 10:37:00 +01002706 bdrv_get_encrypted_filename(bs));
2707 }
2708 }
2709}
2710
Markus Armbrusterf8d6bba2012-06-13 10:11:48 +02002711const char *bdrv_get_format_name(BlockDriverState *bs)
bellardea2384d2004-08-01 21:59:26 +00002712{
Markus Armbrusterf8d6bba2012-06-13 10:11:48 +02002713 return bs->drv ? bs->drv->format_name : NULL;
bellardea2384d2004-08-01 21:59:26 +00002714}
2715
Stefan Hajnocziada42402014-08-27 12:08:55 +01002716static int qsort_strcmp(const void *a, const void *b)
2717{
2718 return strcmp(a, b);
2719}
2720
ths5fafdf22007-09-16 21:08:06 +00002721void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
bellardea2384d2004-08-01 21:59:26 +00002722 void *opaque)
2723{
2724 BlockDriver *drv;
Jeff Codye855e4f2014-04-28 18:29:54 -04002725 int count = 0;
Stefan Hajnocziada42402014-08-27 12:08:55 +01002726 int i;
Jeff Codye855e4f2014-04-28 18:29:54 -04002727 const char **formats = NULL;
bellardea2384d2004-08-01 21:59:26 +00002728
Stefan Hajnoczi8a22f022010-04-13 10:29:33 +01002729 QLIST_FOREACH(drv, &bdrv_drivers, list) {
Jeff Codye855e4f2014-04-28 18:29:54 -04002730 if (drv->format_name) {
2731 bool found = false;
2732 int i = count;
2733 while (formats && i && !found) {
2734 found = !strcmp(formats[--i], drv->format_name);
2735 }
2736
2737 if (!found) {
Markus Armbruster5839e532014-08-19 10:31:08 +02002738 formats = g_renew(const char *, formats, count + 1);
Jeff Codye855e4f2014-04-28 18:29:54 -04002739 formats[count++] = drv->format_name;
Jeff Codye855e4f2014-04-28 18:29:54 -04002740 }
2741 }
bellardea2384d2004-08-01 21:59:26 +00002742 }
Stefan Hajnocziada42402014-08-27 12:08:55 +01002743
2744 qsort(formats, count, sizeof(formats[0]), qsort_strcmp);
2745
2746 for (i = 0; i < count; i++) {
2747 it(opaque, formats[i]);
2748 }
2749
Jeff Codye855e4f2014-04-28 18:29:54 -04002750 g_free(formats);
bellardea2384d2004-08-01 21:59:26 +00002751}
2752
Benoît Canetdc364f42014-01-23 21:31:32 +01002753/* This function is to find a node in the bs graph */
2754BlockDriverState *bdrv_find_node(const char *node_name)
2755{
2756 BlockDriverState *bs;
2757
2758 assert(node_name);
2759
2760 QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) {
2761 if (!strcmp(node_name, bs->node_name)) {
2762 return bs;
2763 }
2764 }
2765 return NULL;
2766}
2767
Benoît Canetc13163f2014-01-23 21:31:34 +01002768/* Put this QMP function here so it can access the static graph_bdrv_states. */
Alberto Garciad5a8ee62015-04-17 14:52:43 +03002769BlockDeviceInfoList *bdrv_named_nodes_list(Error **errp)
Benoît Canetc13163f2014-01-23 21:31:34 +01002770{
2771 BlockDeviceInfoList *list, *entry;
2772 BlockDriverState *bs;
2773
2774 list = NULL;
2775 QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) {
Kevin Wolfc83f9fb2016-03-03 11:37:48 +01002776 BlockDeviceInfo *info = bdrv_block_device_info(NULL, bs, errp);
Alberto Garciad5a8ee62015-04-17 14:52:43 +03002777 if (!info) {
2778 qapi_free_BlockDeviceInfoList(list);
2779 return NULL;
2780 }
Benoît Canetc13163f2014-01-23 21:31:34 +01002781 entry = g_malloc0(sizeof(*entry));
Alberto Garciad5a8ee62015-04-17 14:52:43 +03002782 entry->value = info;
Benoît Canetc13163f2014-01-23 21:31:34 +01002783 entry->next = list;
2784 list = entry;
2785 }
2786
2787 return list;
2788}
2789
Benoît Canet12d3ba82014-01-23 21:31:35 +01002790BlockDriverState *bdrv_lookup_bs(const char *device,
2791 const char *node_name,
2792 Error **errp)
2793{
Markus Armbruster7f06d472014-10-07 13:59:12 +02002794 BlockBackend *blk;
2795 BlockDriverState *bs;
Benoît Canet12d3ba82014-01-23 21:31:35 +01002796
Benoît Canet12d3ba82014-01-23 21:31:35 +01002797 if (device) {
Markus Armbruster7f06d472014-10-07 13:59:12 +02002798 blk = blk_by_name(device);
Benoît Canet12d3ba82014-01-23 21:31:35 +01002799
Markus Armbruster7f06d472014-10-07 13:59:12 +02002800 if (blk) {
Alberto Garcia9f4ed6f2015-10-26 16:46:49 +02002801 bs = blk_bs(blk);
2802 if (!bs) {
Max Reitz5433c242015-10-19 17:53:29 +02002803 error_setg(errp, "Device '%s' has no medium", device);
Max Reitz5433c242015-10-19 17:53:29 +02002804 }
2805
Alberto Garcia9f4ed6f2015-10-26 16:46:49 +02002806 return bs;
Benoît Canet12d3ba82014-01-23 21:31:35 +01002807 }
Benoît Canet12d3ba82014-01-23 21:31:35 +01002808 }
2809
Benoît Canetdd67fa52014-02-12 17:15:06 +01002810 if (node_name) {
2811 bs = bdrv_find_node(node_name);
Benoît Canet12d3ba82014-01-23 21:31:35 +01002812
Benoît Canetdd67fa52014-02-12 17:15:06 +01002813 if (bs) {
2814 return bs;
2815 }
Benoît Canet12d3ba82014-01-23 21:31:35 +01002816 }
2817
Benoît Canetdd67fa52014-02-12 17:15:06 +01002818 error_setg(errp, "Cannot find device=%s nor node_name=%s",
2819 device ? device : "",
2820 node_name ? node_name : "");
2821 return NULL;
Benoît Canet12d3ba82014-01-23 21:31:35 +01002822}
2823
Jeff Cody5a6684d2014-06-25 15:40:09 -04002824/* If 'base' is in the same chain as 'top', return true. Otherwise,
2825 * return false. If either argument is NULL, return false. */
2826bool bdrv_chain_contains(BlockDriverState *top, BlockDriverState *base)
2827{
2828 while (top && top != base) {
Kevin Wolf760e0062015-06-17 14:55:21 +02002829 top = backing_bs(top);
Jeff Cody5a6684d2014-06-25 15:40:09 -04002830 }
2831
2832 return top != NULL;
2833}
2834
Fam Zheng04df7652014-10-31 11:32:54 +08002835BlockDriverState *bdrv_next_node(BlockDriverState *bs)
2836{
2837 if (!bs) {
2838 return QTAILQ_FIRST(&graph_bdrv_states);
2839 }
2840 return QTAILQ_NEXT(bs, node_list);
2841}
2842
Fam Zheng20a9e772014-10-31 11:32:55 +08002843const char *bdrv_get_node_name(const BlockDriverState *bs)
2844{
2845 return bs->node_name;
2846}
2847
Kevin Wolf1f0c4612016-03-22 18:38:44 +01002848const char *bdrv_get_parent_name(const BlockDriverState *bs)
Kevin Wolf4c265bf2016-02-26 10:22:16 +01002849{
2850 BdrvChild *c;
2851 const char *name;
2852
2853 /* If multiple parents have a name, just pick the first one. */
2854 QLIST_FOREACH(c, &bs->parents, next_parent) {
2855 if (c->role->get_name) {
2856 name = c->role->get_name(c);
2857 if (name && *name) {
2858 return name;
2859 }
2860 }
2861 }
2862
2863 return NULL;
2864}
2865
Markus Armbruster7f06d472014-10-07 13:59:12 +02002866/* TODO check what callers really want: bs->node_name or blk_name() */
Markus Armbrusterbfb197e2014-10-07 13:59:11 +02002867const char *bdrv_get_device_name(const BlockDriverState *bs)
bellardea2384d2004-08-01 21:59:26 +00002868{
Kevin Wolf4c265bf2016-02-26 10:22:16 +01002869 return bdrv_get_parent_name(bs) ?: "";
bellardea2384d2004-08-01 21:59:26 +00002870}
2871
Alberto Garcia9b2aa842015-04-08 12:29:18 +03002872/* This can be used to identify nodes that might not have a device
2873 * name associated. Since node and device names live in the same
2874 * namespace, the result is unambiguous. The exception is if both are
2875 * absent, then this returns an empty (non-null) string. */
2876const char *bdrv_get_device_or_node_name(const BlockDriverState *bs)
2877{
Kevin Wolf4c265bf2016-02-26 10:22:16 +01002878 return bdrv_get_parent_name(bs) ?: bs->node_name;
Alberto Garcia9b2aa842015-04-08 12:29:18 +03002879}
2880
Markus Armbrusterc8433282012-06-05 16:49:24 +02002881int bdrv_get_flags(BlockDriverState *bs)
2882{
2883 return bs->open_flags;
2884}
2885
Peter Lieven3ac21622013-06-28 12:47:42 +02002886int bdrv_has_zero_init_1(BlockDriverState *bs)
2887{
2888 return 1;
2889}
2890
Kevin Wolff2feebb2010-04-14 17:30:35 +02002891int bdrv_has_zero_init(BlockDriverState *bs)
2892{
2893 assert(bs->drv);
2894
Paolo Bonzini11212d82013-09-04 19:00:27 +02002895 /* If BS is a copy on write image, it is initialized to
2896 the contents of the base image, which may not be zeroes. */
Kevin Wolf760e0062015-06-17 14:55:21 +02002897 if (bs->backing) {
Paolo Bonzini11212d82013-09-04 19:00:27 +02002898 return 0;
2899 }
Kevin Wolf336c1c12010-07-28 11:26:29 +02002900 if (bs->drv->bdrv_has_zero_init) {
2901 return bs->drv->bdrv_has_zero_init(bs);
Kevin Wolff2feebb2010-04-14 17:30:35 +02002902 }
2903
Peter Lieven3ac21622013-06-28 12:47:42 +02002904 /* safe default */
2905 return 0;
Kevin Wolff2feebb2010-04-14 17:30:35 +02002906}
2907
Peter Lieven4ce78692013-10-24 12:06:54 +02002908bool bdrv_unallocated_blocks_are_zero(BlockDriverState *bs)
2909{
2910 BlockDriverInfo bdi;
2911
Kevin Wolf760e0062015-06-17 14:55:21 +02002912 if (bs->backing) {
Peter Lieven4ce78692013-10-24 12:06:54 +02002913 return false;
2914 }
2915
2916 if (bdrv_get_info(bs, &bdi) == 0) {
2917 return bdi.unallocated_blocks_are_zero;
2918 }
2919
2920 return false;
2921}
2922
2923bool bdrv_can_write_zeroes_with_unmap(BlockDriverState *bs)
2924{
2925 BlockDriverInfo bdi;
2926
Kevin Wolf760e0062015-06-17 14:55:21 +02002927 if (bs->backing || !(bs->open_flags & BDRV_O_UNMAP)) {
Peter Lieven4ce78692013-10-24 12:06:54 +02002928 return false;
2929 }
2930
2931 if (bdrv_get_info(bs, &bdi) == 0) {
2932 return bdi.can_write_zeroes_with_unmap;
2933 }
2934
2935 return false;
2936}
2937
aliguori045df332009-03-05 23:00:48 +00002938const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
2939{
Kevin Wolf760e0062015-06-17 14:55:21 +02002940 if (bs->backing && bs->backing->bs->encrypted)
aliguori045df332009-03-05 23:00:48 +00002941 return bs->backing_file;
2942 else if (bs->encrypted)
2943 return bs->filename;
2944 else
2945 return NULL;
2946}
2947
ths5fafdf22007-09-16 21:08:06 +00002948void bdrv_get_backing_filename(BlockDriverState *bs,
bellard83f64092006-08-01 16:21:11 +00002949 char *filename, int filename_size)
bellardea2384d2004-08-01 21:59:26 +00002950{
Kevin Wolf3574c602011-10-26 11:02:11 +02002951 pstrcpy(filename, filename_size, bs->backing_file);
bellardea2384d2004-08-01 21:59:26 +00002952}
2953
bellardfaea38e2006-08-05 21:31:00 +00002954int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
2955{
2956 BlockDriver *drv = bs->drv;
2957 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00002958 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00002959 if (!drv->bdrv_get_info)
2960 return -ENOTSUP;
2961 memset(bdi, 0, sizeof(*bdi));
2962 return drv->bdrv_get_info(bs, bdi);
2963}
2964
Max Reitzeae041f2013-10-09 10:46:16 +02002965ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs)
2966{
2967 BlockDriver *drv = bs->drv;
2968 if (drv && drv->bdrv_get_specific_info) {
2969 return drv->bdrv_get_specific_info(bs);
2970 }
2971 return NULL;
2972}
2973
Eric Blakea31939e2015-11-18 01:52:54 -07002974void bdrv_debug_event(BlockDriverState *bs, BlkdebugEvent event)
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01002975{
Kevin Wolfbf736fe2013-06-05 15:17:55 +02002976 if (!bs || !bs->drv || !bs->drv->bdrv_debug_event) {
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01002977 return;
2978 }
2979
Kevin Wolfbf736fe2013-06-05 15:17:55 +02002980 bs->drv->bdrv_debug_event(bs, event);
Kevin Wolf41c695c2012-12-06 14:32:58 +01002981}
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01002982
Kevin Wolf41c695c2012-12-06 14:32:58 +01002983int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event,
2984 const char *tag)
2985{
2986 while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02002987 bs = bs->file ? bs->file->bs : NULL;
Kevin Wolf41c695c2012-12-06 14:32:58 +01002988 }
2989
2990 if (bs && bs->drv && bs->drv->bdrv_debug_breakpoint) {
2991 return bs->drv->bdrv_debug_breakpoint(bs, event, tag);
2992 }
2993
2994 return -ENOTSUP;
2995}
2996
Fam Zheng4cc70e92013-11-20 10:01:54 +08002997int bdrv_debug_remove_breakpoint(BlockDriverState *bs, const char *tag)
2998{
2999 while (bs && bs->drv && !bs->drv->bdrv_debug_remove_breakpoint) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003000 bs = bs->file ? bs->file->bs : NULL;
Fam Zheng4cc70e92013-11-20 10:01:54 +08003001 }
3002
3003 if (bs && bs->drv && bs->drv->bdrv_debug_remove_breakpoint) {
3004 return bs->drv->bdrv_debug_remove_breakpoint(bs, tag);
3005 }
3006
3007 return -ENOTSUP;
3008}
3009
Kevin Wolf41c695c2012-12-06 14:32:58 +01003010int bdrv_debug_resume(BlockDriverState *bs, const char *tag)
3011{
Max Reitz938789e2014-03-10 23:44:08 +01003012 while (bs && (!bs->drv || !bs->drv->bdrv_debug_resume)) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003013 bs = bs->file ? bs->file->bs : NULL;
Kevin Wolf41c695c2012-12-06 14:32:58 +01003014 }
3015
3016 if (bs && bs->drv && bs->drv->bdrv_debug_resume) {
3017 return bs->drv->bdrv_debug_resume(bs, tag);
3018 }
3019
3020 return -ENOTSUP;
3021}
3022
3023bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag)
3024{
3025 while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003026 bs = bs->file ? bs->file->bs : NULL;
Kevin Wolf41c695c2012-12-06 14:32:58 +01003027 }
3028
3029 if (bs && bs->drv && bs->drv->bdrv_debug_is_suspended) {
3030 return bs->drv->bdrv_debug_is_suspended(bs, tag);
3031 }
3032
3033 return false;
Kevin Wolf8b9b0cc2010-03-15 17:27:00 +01003034}
3035
Blue Swirl199630b2010-07-25 20:49:34 +00003036int bdrv_is_snapshot(BlockDriverState *bs)
3037{
3038 return !!(bs->open_flags & BDRV_O_SNAPSHOT);
3039}
3040
Jeff Codyb1b1d782012-10-16 15:49:09 -04003041/* backing_file can either be relative, or absolute, or a protocol. If it is
3042 * relative, it must be relative to the chain. So, passing in bs->filename
3043 * from a BDS as backing_file should not be done, as that may be relative to
3044 * the CWD rather than the chain. */
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00003045BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs,
3046 const char *backing_file)
3047{
Jeff Codyb1b1d782012-10-16 15:49:09 -04003048 char *filename_full = NULL;
3049 char *backing_file_full = NULL;
3050 char *filename_tmp = NULL;
3051 int is_protocol = 0;
3052 BlockDriverState *curr_bs = NULL;
3053 BlockDriverState *retval = NULL;
3054
3055 if (!bs || !bs->drv || !backing_file) {
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00003056 return NULL;
3057 }
3058
Jeff Codyb1b1d782012-10-16 15:49:09 -04003059 filename_full = g_malloc(PATH_MAX);
3060 backing_file_full = g_malloc(PATH_MAX);
3061 filename_tmp = g_malloc(PATH_MAX);
3062
3063 is_protocol = path_has_protocol(backing_file);
3064
Kevin Wolf760e0062015-06-17 14:55:21 +02003065 for (curr_bs = bs; curr_bs->backing; curr_bs = curr_bs->backing->bs) {
Jeff Codyb1b1d782012-10-16 15:49:09 -04003066
3067 /* If either of the filename paths is actually a protocol, then
3068 * compare unmodified paths; otherwise make paths relative */
3069 if (is_protocol || path_has_protocol(curr_bs->backing_file)) {
3070 if (strcmp(backing_file, curr_bs->backing_file) == 0) {
Kevin Wolf760e0062015-06-17 14:55:21 +02003071 retval = curr_bs->backing->bs;
Jeff Codyb1b1d782012-10-16 15:49:09 -04003072 break;
3073 }
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00003074 } else {
Jeff Codyb1b1d782012-10-16 15:49:09 -04003075 /* If not an absolute filename path, make it relative to the current
3076 * image's filename path */
3077 path_combine(filename_tmp, PATH_MAX, curr_bs->filename,
3078 backing_file);
3079
3080 /* We are going to compare absolute pathnames */
3081 if (!realpath(filename_tmp, filename_full)) {
3082 continue;
3083 }
3084
3085 /* We need to make sure the backing filename we are comparing against
3086 * is relative to the current image filename (or absolute) */
3087 path_combine(filename_tmp, PATH_MAX, curr_bs->filename,
3088 curr_bs->backing_file);
3089
3090 if (!realpath(filename_tmp, backing_file_full)) {
3091 continue;
3092 }
3093
3094 if (strcmp(backing_file_full, filename_full) == 0) {
Kevin Wolf760e0062015-06-17 14:55:21 +02003095 retval = curr_bs->backing->bs;
Jeff Codyb1b1d782012-10-16 15:49:09 -04003096 break;
3097 }
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00003098 }
3099 }
3100
Jeff Codyb1b1d782012-10-16 15:49:09 -04003101 g_free(filename_full);
3102 g_free(backing_file_full);
3103 g_free(filename_tmp);
3104 return retval;
Marcelo Tosattie8a6bb92012-01-18 14:40:51 +00003105}
3106
Benoît Canetf198fd12012-08-02 10:22:47 +02003107int bdrv_get_backing_file_depth(BlockDriverState *bs)
3108{
3109 if (!bs->drv) {
3110 return 0;
3111 }
3112
Kevin Wolf760e0062015-06-17 14:55:21 +02003113 if (!bs->backing) {
Benoît Canetf198fd12012-08-02 10:22:47 +02003114 return 0;
3115 }
3116
Kevin Wolf760e0062015-06-17 14:55:21 +02003117 return 1 + bdrv_get_backing_file_depth(bs->backing->bs);
Benoît Canetf198fd12012-08-02 10:22:47 +02003118}
3119
bellardea2384d2004-08-01 21:59:26 +00003120void bdrv_init(void)
3121{
Anthony Liguori5efa9d52009-05-09 17:03:42 -05003122 module_call_init(MODULE_INIT_BLOCK);
bellardea2384d2004-08-01 21:59:26 +00003123}
pbrookce1a14d2006-08-07 02:38:06 +00003124
Markus Armbrustereb852012009-10-27 18:41:44 +01003125void bdrv_init_with_whitelist(void)
3126{
3127 use_bdrv_whitelist = 1;
3128 bdrv_init();
3129}
3130
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003131void bdrv_invalidate_cache(BlockDriverState *bs, Error **errp)
Anthony Liguori0f154232011-11-14 15:09:45 -06003132{
Fam Zheng0d1c5c92016-05-11 10:45:33 +08003133 BdrvChild *child;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003134 Error *local_err = NULL;
3135 int ret;
3136
Kevin Wolf3456a8d2014-03-11 10:58:39 +01003137 if (!bs->drv) {
3138 return;
Anthony Liguori0f154232011-11-14 15:09:45 -06003139 }
Kevin Wolf3456a8d2014-03-11 10:58:39 +01003140
Kevin Wolf04c01a52016-01-13 15:56:06 +01003141 if (!(bs->open_flags & BDRV_O_INACTIVE)) {
Alexey Kardashevskiy7ea2d262014-10-09 13:50:46 +11003142 return;
3143 }
Kevin Wolf04c01a52016-01-13 15:56:06 +01003144 bs->open_flags &= ~BDRV_O_INACTIVE;
Alexey Kardashevskiy7ea2d262014-10-09 13:50:46 +11003145
Kevin Wolf3456a8d2014-03-11 10:58:39 +01003146 if (bs->drv->bdrv_invalidate_cache) {
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003147 bs->drv->bdrv_invalidate_cache(bs, &local_err);
Fam Zheng0d1c5c92016-05-11 10:45:33 +08003148 if (local_err) {
3149 bs->open_flags |= BDRV_O_INACTIVE;
3150 error_propagate(errp, local_err);
3151 return;
3152 }
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003153 }
Fam Zheng0d1c5c92016-05-11 10:45:33 +08003154
3155 QLIST_FOREACH(child, &bs->children, next) {
3156 bdrv_invalidate_cache(child->bs, &local_err);
3157 if (local_err) {
3158 bs->open_flags |= BDRV_O_INACTIVE;
3159 error_propagate(errp, local_err);
3160 return;
3161 }
Kevin Wolf3456a8d2014-03-11 10:58:39 +01003162 }
3163
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003164 ret = refresh_total_sectors(bs, bs->total_sectors);
3165 if (ret < 0) {
Kevin Wolf04c01a52016-01-13 15:56:06 +01003166 bs->open_flags |= BDRV_O_INACTIVE;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003167 error_setg_errno(errp, -ret, "Could not refresh total sector count");
3168 return;
3169 }
Anthony Liguori0f154232011-11-14 15:09:45 -06003170}
3171
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003172void bdrv_invalidate_cache_all(Error **errp)
Anthony Liguori0f154232011-11-14 15:09:45 -06003173{
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003174 BlockDriverState *bs;
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003175 Error *local_err = NULL;
Kevin Wolf88be7b42016-05-20 18:49:07 +02003176 BdrvNextIterator it;
Anthony Liguori0f154232011-11-14 15:09:45 -06003177
Kevin Wolf88be7b42016-05-20 18:49:07 +02003178 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Stefan Hajnoczied78cda2014-05-08 16:34:35 +02003179 AioContext *aio_context = bdrv_get_aio_context(bs);
3180
3181 aio_context_acquire(aio_context);
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003182 bdrv_invalidate_cache(bs, &local_err);
Stefan Hajnoczied78cda2014-05-08 16:34:35 +02003183 aio_context_release(aio_context);
Kevin Wolf5a8a30d2014-03-12 15:59:16 +01003184 if (local_err) {
3185 error_propagate(errp, local_err);
3186 return;
3187 }
Anthony Liguori0f154232011-11-14 15:09:45 -06003188 }
3189}
3190
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003191static int bdrv_inactivate_recurse(BlockDriverState *bs,
3192 bool setting_flag)
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003193{
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003194 BdrvChild *child;
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003195 int ret;
3196
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003197 if (!setting_flag && bs->drv->bdrv_inactivate) {
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003198 ret = bs->drv->bdrv_inactivate(bs);
3199 if (ret < 0) {
3200 return ret;
3201 }
3202 }
3203
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003204 QLIST_FOREACH(child, &bs->children, next) {
3205 ret = bdrv_inactivate_recurse(child->bs, setting_flag);
3206 if (ret < 0) {
3207 return ret;
3208 }
3209 }
3210
3211 if (setting_flag) {
3212 bs->open_flags |= BDRV_O_INACTIVE;
3213 }
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003214 return 0;
3215}
3216
3217int bdrv_inactivate_all(void)
3218{
Max Reitz79720af2016-03-16 19:54:44 +01003219 BlockDriverState *bs = NULL;
Kevin Wolf88be7b42016-05-20 18:49:07 +02003220 BdrvNextIterator it;
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003221 int ret = 0;
3222 int pass;
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003223
Kevin Wolf88be7b42016-05-20 18:49:07 +02003224 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003225 aio_context_acquire(bdrv_get_aio_context(bs));
3226 }
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003227
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003228 /* We do two passes of inactivation. The first pass calls to drivers'
3229 * .bdrv_inactivate callbacks recursively so all cache is flushed to disk;
3230 * the second pass sets the BDRV_O_INACTIVE flag so that no further write
3231 * is allowed. */
3232 for (pass = 0; pass < 2; pass++) {
Kevin Wolf88be7b42016-05-20 18:49:07 +02003233 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003234 ret = bdrv_inactivate_recurse(bs, pass);
3235 if (ret < 0) {
3236 goto out;
3237 }
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003238 }
3239 }
3240
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003241out:
Kevin Wolf88be7b42016-05-20 18:49:07 +02003242 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Fam Zhengaad0b7a2016-05-11 10:45:35 +08003243 aio_context_release(bdrv_get_aio_context(bs));
3244 }
3245
3246 return ret;
Kevin Wolf76b1c7f2015-12-22 14:07:08 +01003247}
3248
Kevin Wolff9f05dc2011-07-15 13:50:26 +02003249/**************************************************************/
bellard19cb3732006-08-19 11:45:59 +00003250/* removable device support */
3251
3252/**
3253 * Return TRUE if the media is present
3254 */
Max Reitze031f752015-10-19 17:53:11 +02003255bool bdrv_is_inserted(BlockDriverState *bs)
bellard19cb3732006-08-19 11:45:59 +00003256{
3257 BlockDriver *drv = bs->drv;
Max Reitz28d7a782015-10-19 17:53:13 +02003258 BdrvChild *child;
Markus Armbrustera1aff5b2011-09-06 18:58:41 +02003259
Max Reitze031f752015-10-19 17:53:11 +02003260 if (!drv) {
3261 return false;
3262 }
Max Reitz28d7a782015-10-19 17:53:13 +02003263 if (drv->bdrv_is_inserted) {
3264 return drv->bdrv_is_inserted(bs);
Max Reitze031f752015-10-19 17:53:11 +02003265 }
Max Reitz28d7a782015-10-19 17:53:13 +02003266 QLIST_FOREACH(child, &bs->children, next) {
3267 if (!bdrv_is_inserted(child->bs)) {
3268 return false;
3269 }
3270 }
3271 return true;
bellard19cb3732006-08-19 11:45:59 +00003272}
3273
3274/**
Markus Armbruster8e49ca42011-08-03 15:08:08 +02003275 * Return whether the media changed since the last call to this
3276 * function, or -ENOTSUP if we don't know. Most drivers don't know.
bellard19cb3732006-08-19 11:45:59 +00003277 */
3278int bdrv_media_changed(BlockDriverState *bs)
3279{
3280 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +00003281
Markus Armbruster8e49ca42011-08-03 15:08:08 +02003282 if (drv && drv->bdrv_media_changed) {
3283 return drv->bdrv_media_changed(bs);
3284 }
3285 return -ENOTSUP;
bellard19cb3732006-08-19 11:45:59 +00003286}
3287
3288/**
3289 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
3290 */
Luiz Capitulinof36f3942012-02-03 16:24:53 -02003291void bdrv_eject(BlockDriverState *bs, bool eject_flag)
bellard19cb3732006-08-19 11:45:59 +00003292{
3293 BlockDriver *drv = bs->drv;
Markus Armbrusterbfb197e2014-10-07 13:59:11 +02003294 const char *device_name;
bellard19cb3732006-08-19 11:45:59 +00003295
Markus Armbruster822e1cd2011-07-20 18:23:42 +02003296 if (drv && drv->bdrv_eject) {
3297 drv->bdrv_eject(bs, eject_flag);
bellard19cb3732006-08-19 11:45:59 +00003298 }
Luiz Capitulino6f382ed2012-02-14 13:41:13 -02003299
Markus Armbrusterbfb197e2014-10-07 13:59:11 +02003300 device_name = bdrv_get_device_name(bs);
3301 if (device_name[0] != '\0') {
3302 qapi_event_send_device_tray_moved(device_name,
Wenchao Xiaa5ee7bd2014-06-18 08:43:44 +02003303 eject_flag, &error_abort);
Luiz Capitulino6f382ed2012-02-14 13:41:13 -02003304 }
bellard19cb3732006-08-19 11:45:59 +00003305}
3306
bellard19cb3732006-08-19 11:45:59 +00003307/**
3308 * Lock or unlock the media (if it is locked, the user won't be able
3309 * to eject it manually).
3310 */
Markus Armbruster025e8492011-09-06 18:58:47 +02003311void bdrv_lock_medium(BlockDriverState *bs, bool locked)
bellard19cb3732006-08-19 11:45:59 +00003312{
3313 BlockDriver *drv = bs->drv;
3314
Markus Armbruster025e8492011-09-06 18:58:47 +02003315 trace_bdrv_lock_medium(bs, locked);
Stefan Hajnoczib8c6d092011-03-29 20:04:40 +01003316
Markus Armbruster025e8492011-09-06 18:58:47 +02003317 if (drv && drv->bdrv_lock_medium) {
3318 drv->bdrv_lock_medium(bs, locked);
bellard19cb3732006-08-19 11:45:59 +00003319 }
3320}
ths985a03b2007-12-24 16:10:43 +00003321
Fam Zheng9fcb0252013-08-23 09:14:46 +08003322/* Get a reference to bs */
3323void bdrv_ref(BlockDriverState *bs)
3324{
3325 bs->refcnt++;
3326}
3327
3328/* Release a previously grabbed reference to bs.
3329 * If after releasing, reference count is zero, the BlockDriverState is
3330 * deleted. */
3331void bdrv_unref(BlockDriverState *bs)
3332{
Jeff Cody9a4d5ca2014-07-23 17:22:57 -04003333 if (!bs) {
3334 return;
3335 }
Fam Zheng9fcb0252013-08-23 09:14:46 +08003336 assert(bs->refcnt > 0);
3337 if (--bs->refcnt == 0) {
3338 bdrv_delete(bs);
3339 }
3340}
3341
Fam Zhengfbe40ff2014-05-23 21:29:42 +08003342struct BdrvOpBlocker {
3343 Error *reason;
3344 QLIST_ENTRY(BdrvOpBlocker) list;
3345};
3346
3347bool bdrv_op_is_blocked(BlockDriverState *bs, BlockOpType op, Error **errp)
3348{
3349 BdrvOpBlocker *blocker;
3350 assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
3351 if (!QLIST_EMPTY(&bs->op_blockers[op])) {
3352 blocker = QLIST_FIRST(&bs->op_blockers[op]);
3353 if (errp) {
Markus Armbrustere43bfd92015-12-18 16:35:15 +01003354 *errp = error_copy(blocker->reason);
3355 error_prepend(errp, "Node '%s' is busy: ",
3356 bdrv_get_device_or_node_name(bs));
Fam Zhengfbe40ff2014-05-23 21:29:42 +08003357 }
3358 return true;
3359 }
3360 return false;
3361}
3362
3363void bdrv_op_block(BlockDriverState *bs, BlockOpType op, Error *reason)
3364{
3365 BdrvOpBlocker *blocker;
3366 assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
3367
Markus Armbruster5839e532014-08-19 10:31:08 +02003368 blocker = g_new0(BdrvOpBlocker, 1);
Fam Zhengfbe40ff2014-05-23 21:29:42 +08003369 blocker->reason = reason;
3370 QLIST_INSERT_HEAD(&bs->op_blockers[op], blocker, list);
3371}
3372
3373void bdrv_op_unblock(BlockDriverState *bs, BlockOpType op, Error *reason)
3374{
3375 BdrvOpBlocker *blocker, *next;
3376 assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
3377 QLIST_FOREACH_SAFE(blocker, &bs->op_blockers[op], list, next) {
3378 if (blocker->reason == reason) {
3379 QLIST_REMOVE(blocker, list);
3380 g_free(blocker);
3381 }
3382 }
3383}
3384
3385void bdrv_op_block_all(BlockDriverState *bs, Error *reason)
3386{
3387 int i;
3388 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
3389 bdrv_op_block(bs, i, reason);
3390 }
3391}
3392
3393void bdrv_op_unblock_all(BlockDriverState *bs, Error *reason)
3394{
3395 int i;
3396 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
3397 bdrv_op_unblock(bs, i, reason);
3398 }
3399}
3400
3401bool bdrv_op_blocker_is_empty(BlockDriverState *bs)
3402{
3403 int i;
3404
3405 for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) {
3406 if (!QLIST_EMPTY(&bs->op_blockers[i])) {
3407 return false;
3408 }
3409 }
3410 return true;
3411}
3412
Luiz Capitulinod92ada22012-11-30 10:52:09 -02003413void bdrv_img_create(const char *filename, const char *fmt,
3414 const char *base_filename, const char *base_fmt,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003415 char *options, uint64_t img_size, int flags,
3416 Error **errp, bool quiet)
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003417{
Chunyan Liu83d05212014-06-05 17:20:51 +08003418 QemuOptsList *create_opts = NULL;
3419 QemuOpts *opts = NULL;
3420 const char *backing_fmt, *backing_file;
3421 int64_t size;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003422 BlockDriver *drv, *proto_drv;
Max Reitzcc84d902013-09-06 17:14:26 +02003423 Error *local_err = NULL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003424 int ret = 0;
3425
3426 /* Find driver and parse its options */
3427 drv = bdrv_find_format(fmt);
3428 if (!drv) {
Luiz Capitulino71c79812012-11-30 10:52:04 -02003429 error_setg(errp, "Unknown file format '%s'", fmt);
Luiz Capitulinod92ada22012-11-30 10:52:09 -02003430 return;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003431 }
3432
Max Reitzb65a5e12015-02-05 13:58:12 -05003433 proto_drv = bdrv_find_protocol(filename, true, errp);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003434 if (!proto_drv) {
Luiz Capitulinod92ada22012-11-30 10:52:09 -02003435 return;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003436 }
3437
Max Reitzc6149722014-12-02 18:32:45 +01003438 if (!drv->create_opts) {
3439 error_setg(errp, "Format driver '%s' does not support image creation",
3440 drv->format_name);
3441 return;
3442 }
3443
3444 if (!proto_drv->create_opts) {
3445 error_setg(errp, "Protocol driver '%s' does not support image creation",
3446 proto_drv->format_name);
3447 return;
3448 }
3449
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003450 create_opts = qemu_opts_append(create_opts, drv->create_opts);
3451 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003452
3453 /* Create parameter list with default values */
Chunyan Liu83d05212014-06-05 17:20:51 +08003454 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbruster39101f22015-02-12 16:46:36 +01003455 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003456
3457 /* Parse -o options */
3458 if (options) {
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003459 qemu_opts_do_parse(opts, options, NULL, &local_err);
3460 if (local_err) {
3461 error_report_err(local_err);
3462 local_err = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003463 error_setg(errp, "Invalid options for file format '%s'", fmt);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003464 goto out;
3465 }
3466 }
3467
3468 if (base_filename) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003469 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &local_err);
Markus Armbruster6be41942015-02-12 17:49:02 +01003470 if (local_err) {
Luiz Capitulino71c79812012-11-30 10:52:04 -02003471 error_setg(errp, "Backing file not supported for file format '%s'",
3472 fmt);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003473 goto out;
3474 }
3475 }
3476
3477 if (base_fmt) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003478 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &local_err);
Markus Armbruster6be41942015-02-12 17:49:02 +01003479 if (local_err) {
Luiz Capitulino71c79812012-11-30 10:52:04 -02003480 error_setg(errp, "Backing file format not supported for file "
3481 "format '%s'", fmt);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003482 goto out;
3483 }
3484 }
3485
Chunyan Liu83d05212014-06-05 17:20:51 +08003486 backing_file = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
3487 if (backing_file) {
3488 if (!strcmp(filename, backing_file)) {
Luiz Capitulino71c79812012-11-30 10:52:04 -02003489 error_setg(errp, "Error: Trying to create an image with the "
3490 "same filename as the backing file");
Jes Sorensen792da932010-12-16 13:52:17 +01003491 goto out;
3492 }
3493 }
3494
Chunyan Liu83d05212014-06-05 17:20:51 +08003495 backing_fmt = qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003496
3497 // The size for the image must always be specified, with one exception:
3498 // If we are using a backing file, we can obtain the size from there
Chunyan Liu83d05212014-06-05 17:20:51 +08003499 size = qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 0);
3500 if (size == -1) {
3501 if (backing_file) {
Max Reitz66f6b812013-12-03 14:57:52 +01003502 BlockDriverState *bs;
Max Reitz29168012014-11-26 17:20:27 +01003503 char *full_backing = g_new0(char, PATH_MAX);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003504 int64_t size;
Paolo Bonzini63090da2012-04-12 14:01:03 +02003505 int back_flags;
Max Reitze6641712015-08-26 19:47:48 +02003506 QDict *backing_options = NULL;
Paolo Bonzini63090da2012-04-12 14:01:03 +02003507
Max Reitz29168012014-11-26 17:20:27 +01003508 bdrv_get_full_backing_filename_from_filename(filename, backing_file,
3509 full_backing, PATH_MAX,
3510 &local_err);
3511 if (local_err) {
3512 g_free(full_backing);
3513 goto out;
3514 }
3515
Paolo Bonzini63090da2012-04-12 14:01:03 +02003516 /* backing files always opened read-only */
Kevin Wolf61de4c62016-03-18 17:46:45 +01003517 back_flags = flags;
Kevin Wolfbfd18d12016-03-04 14:28:01 +01003518 back_flags &= ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003519
Max Reitze6641712015-08-26 19:47:48 +02003520 if (backing_fmt) {
3521 backing_options = qdict_new();
3522 qdict_put(backing_options, "driver",
3523 qstring_from_str(backing_fmt));
3524 }
3525
Max Reitz5b363932016-05-17 16:41:31 +02003526 bs = bdrv_open(full_backing, NULL, backing_options, back_flags,
3527 &local_err);
Max Reitz29168012014-11-26 17:20:27 +01003528 g_free(full_backing);
Max Reitz5b363932016-05-17 16:41:31 +02003529 if (!bs) {
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003530 goto out;
3531 }
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003532 size = bdrv_getlength(bs);
3533 if (size < 0) {
3534 error_setg_errno(errp, -size, "Could not get size of '%s'",
3535 backing_file);
3536 bdrv_unref(bs);
3537 goto out;
3538 }
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003539
Markus Armbruster39101f22015-02-12 16:46:36 +01003540 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, size, &error_abort);
Max Reitz66f6b812013-12-03 14:57:52 +01003541
3542 bdrv_unref(bs);
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003543 } else {
Luiz Capitulino71c79812012-11-30 10:52:04 -02003544 error_setg(errp, "Image creation needs a size parameter");
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003545 goto out;
3546 }
3547 }
3548
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003549 if (!quiet) {
Kővágó, Zoltánfe646692015-07-07 16:42:10 +02003550 printf("Formatting '%s', fmt=%s ", filename, fmt);
Fam Zheng43c5d8f2014-12-09 15:38:04 +08003551 qemu_opts_print(opts, " ");
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003552 puts("");
3553 }
Chunyan Liu83d05212014-06-05 17:20:51 +08003554
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003555 ret = bdrv_create(drv, filename, opts, &local_err);
Chunyan Liu83d05212014-06-05 17:20:51 +08003556
Max Reitzcc84d902013-09-06 17:14:26 +02003557 if (ret == -EFBIG) {
3558 /* This is generally a better message than whatever the driver would
3559 * deliver (especially because of the cluster_size_hint), since that
3560 * is most probably not much different from "image too large". */
3561 const char *cluster_size_hint = "";
Chunyan Liu83d05212014-06-05 17:20:51 +08003562 if (qemu_opt_get_size(opts, BLOCK_OPT_CLUSTER_SIZE, 0)) {
Max Reitzcc84d902013-09-06 17:14:26 +02003563 cluster_size_hint = " (try using a larger cluster size)";
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003564 }
Max Reitzcc84d902013-09-06 17:14:26 +02003565 error_setg(errp, "The image size is too large for file format '%s'"
3566 "%s", fmt, cluster_size_hint);
3567 error_free(local_err);
3568 local_err = NULL;
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003569 }
3570
3571out:
Chunyan Liu83d05212014-06-05 17:20:51 +08003572 qemu_opts_del(opts);
3573 qemu_opts_free(create_opts);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003574 if (local_err) {
Max Reitzcc84d902013-09-06 17:14:26 +02003575 error_propagate(errp, local_err);
3576 }
Jes Sorensenf88e1a42010-12-16 13:52:15 +01003577}
Stefan Hajnoczi85d126f2013-03-07 13:41:48 +01003578
3579AioContext *bdrv_get_aio_context(BlockDriverState *bs)
3580{
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003581 return bs->aio_context;
3582}
3583
3584void bdrv_detach_aio_context(BlockDriverState *bs)
3585{
Max Reitz33384422014-06-20 21:57:33 +02003586 BdrvAioNotifier *baf;
Max Reitzb97511c2016-05-17 13:38:04 +02003587 BdrvChild *child;
Max Reitz33384422014-06-20 21:57:33 +02003588
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003589 if (!bs->drv) {
3590 return;
3591 }
3592
Max Reitz33384422014-06-20 21:57:33 +02003593 QLIST_FOREACH(baf, &bs->aio_notifiers, list) {
3594 baf->detach_aio_context(baf->opaque);
3595 }
3596
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003597 if (bs->drv->bdrv_detach_aio_context) {
3598 bs->drv->bdrv_detach_aio_context(bs);
3599 }
Max Reitzb97511c2016-05-17 13:38:04 +02003600 QLIST_FOREACH(child, &bs->children, next) {
3601 bdrv_detach_aio_context(child->bs);
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003602 }
3603
3604 bs->aio_context = NULL;
3605}
3606
3607void bdrv_attach_aio_context(BlockDriverState *bs,
3608 AioContext *new_context)
3609{
Max Reitz33384422014-06-20 21:57:33 +02003610 BdrvAioNotifier *ban;
Max Reitzb97511c2016-05-17 13:38:04 +02003611 BdrvChild *child;
Max Reitz33384422014-06-20 21:57:33 +02003612
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003613 if (!bs->drv) {
3614 return;
3615 }
3616
3617 bs->aio_context = new_context;
3618
Max Reitzb97511c2016-05-17 13:38:04 +02003619 QLIST_FOREACH(child, &bs->children, next) {
3620 bdrv_attach_aio_context(child->bs, new_context);
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003621 }
3622 if (bs->drv->bdrv_attach_aio_context) {
3623 bs->drv->bdrv_attach_aio_context(bs, new_context);
3624 }
Max Reitz33384422014-06-20 21:57:33 +02003625
3626 QLIST_FOREACH(ban, &bs->aio_notifiers, list) {
3627 ban->attached_aio_context(new_context, ban->opaque);
3628 }
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003629}
3630
3631void bdrv_set_aio_context(BlockDriverState *bs, AioContext *new_context)
3632{
Fam Zheng53ec73e2015-05-29 18:53:14 +08003633 bdrv_drain(bs); /* ensure there are no in-flight requests */
Stefan Hajnoczidcd04222014-05-08 16:34:37 +02003634
3635 bdrv_detach_aio_context(bs);
3636
3637 /* This function executes in the old AioContext so acquire the new one in
3638 * case it runs in a different thread.
3639 */
3640 aio_context_acquire(new_context);
3641 bdrv_attach_aio_context(bs, new_context);
3642 aio_context_release(new_context);
Stefan Hajnoczi85d126f2013-03-07 13:41:48 +01003643}
Stefan Hajnoczid616b222013-06-24 17:13:10 +02003644
Max Reitz33384422014-06-20 21:57:33 +02003645void bdrv_add_aio_context_notifier(BlockDriverState *bs,
3646 void (*attached_aio_context)(AioContext *new_context, void *opaque),
3647 void (*detach_aio_context)(void *opaque), void *opaque)
3648{
3649 BdrvAioNotifier *ban = g_new(BdrvAioNotifier, 1);
3650 *ban = (BdrvAioNotifier){
3651 .attached_aio_context = attached_aio_context,
3652 .detach_aio_context = detach_aio_context,
3653 .opaque = opaque
3654 };
3655
3656 QLIST_INSERT_HEAD(&bs->aio_notifiers, ban, list);
3657}
3658
3659void bdrv_remove_aio_context_notifier(BlockDriverState *bs,
3660 void (*attached_aio_context)(AioContext *,
3661 void *),
3662 void (*detach_aio_context)(void *),
3663 void *opaque)
3664{
3665 BdrvAioNotifier *ban, *ban_next;
3666
3667 QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) {
3668 if (ban->attached_aio_context == attached_aio_context &&
3669 ban->detach_aio_context == detach_aio_context &&
3670 ban->opaque == opaque)
3671 {
3672 QLIST_REMOVE(ban, list);
3673 g_free(ban);
3674
3675 return;
3676 }
3677 }
3678
3679 abort();
3680}
3681
Max Reitz77485432014-10-27 11:12:50 +01003682int bdrv_amend_options(BlockDriverState *bs, QemuOpts *opts,
Max Reitz8b139762015-07-27 17:51:32 +02003683 BlockDriverAmendStatusCB *status_cb, void *cb_opaque)
Max Reitz6f176b42013-09-03 10:09:50 +02003684{
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003685 if (!bs->drv->bdrv_amend_options) {
Max Reitz6f176b42013-09-03 10:09:50 +02003686 return -ENOTSUP;
3687 }
Max Reitz8b139762015-07-27 17:51:32 +02003688 return bs->drv->bdrv_amend_options(bs, opts, status_cb, cb_opaque);
Max Reitz6f176b42013-09-03 10:09:50 +02003689}
Benoît Canetf6186f42013-10-02 14:33:48 +02003690
Benoît Canetb5042a32014-03-03 19:11:34 +01003691/* This function will be called by the bdrv_recurse_is_first_non_filter method
3692 * of block filter and by bdrv_is_first_non_filter.
3693 * It is used to test if the given bs is the candidate or recurse more in the
3694 * node graph.
Benoît Canet212a5a82014-01-23 21:31:36 +01003695 */
Benoît Canet212a5a82014-01-23 21:31:36 +01003696bool bdrv_recurse_is_first_non_filter(BlockDriverState *bs,
3697 BlockDriverState *candidate)
Benoît Canetf6186f42013-10-02 14:33:48 +02003698{
Benoît Canetb5042a32014-03-03 19:11:34 +01003699 /* return false if basic checks fails */
3700 if (!bs || !bs->drv) {
3701 return false;
3702 }
3703
3704 /* the code reached a non block filter driver -> check if the bs is
3705 * the same as the candidate. It's the recursion termination condition.
3706 */
3707 if (!bs->drv->is_filter) {
3708 return bs == candidate;
3709 }
3710 /* Down this path the driver is a block filter driver */
3711
3712 /* If the block filter recursion method is defined use it to recurse down
3713 * the node graph.
3714 */
3715 if (bs->drv->bdrv_recurse_is_first_non_filter) {
Benoît Canet212a5a82014-01-23 21:31:36 +01003716 return bs->drv->bdrv_recurse_is_first_non_filter(bs, candidate);
3717 }
3718
Benoît Canetb5042a32014-03-03 19:11:34 +01003719 /* the driver is a block filter but don't allow to recurse -> return false
3720 */
3721 return false;
Benoît Canet212a5a82014-01-23 21:31:36 +01003722}
3723
3724/* This function checks if the candidate is the first non filter bs down it's
3725 * bs chain. Since we don't have pointers to parents it explore all bs chains
3726 * from the top. Some filters can choose not to pass down the recursion.
3727 */
3728bool bdrv_is_first_non_filter(BlockDriverState *candidate)
3729{
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003730 BlockDriverState *bs;
Kevin Wolf88be7b42016-05-20 18:49:07 +02003731 BdrvNextIterator it;
Benoît Canet212a5a82014-01-23 21:31:36 +01003732
3733 /* walk down the bs forest recursively */
Kevin Wolf88be7b42016-05-20 18:49:07 +02003734 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Benoît Canet212a5a82014-01-23 21:31:36 +01003735 bool perm;
3736
Benoît Canetb5042a32014-03-03 19:11:34 +01003737 /* try to recurse in this top level bs */
Kevin Wolfe6dc8a12014-02-04 11:45:31 +01003738 perm = bdrv_recurse_is_first_non_filter(bs, candidate);
Benoît Canet212a5a82014-01-23 21:31:36 +01003739
3740 /* candidate is the first non filter */
3741 if (perm) {
3742 return true;
3743 }
3744 }
3745
3746 return false;
Benoît Canetf6186f42013-10-02 14:33:48 +02003747}
Benoît Canet09158f02014-06-27 18:25:25 +02003748
Wen Congyange12f3782015-07-17 10:12:22 +08003749BlockDriverState *check_to_replace_node(BlockDriverState *parent_bs,
3750 const char *node_name, Error **errp)
Benoît Canet09158f02014-06-27 18:25:25 +02003751{
3752 BlockDriverState *to_replace_bs = bdrv_find_node(node_name);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003753 AioContext *aio_context;
3754
Benoît Canet09158f02014-06-27 18:25:25 +02003755 if (!to_replace_bs) {
3756 error_setg(errp, "Node name '%s' not found", node_name);
3757 return NULL;
3758 }
3759
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003760 aio_context = bdrv_get_aio_context(to_replace_bs);
3761 aio_context_acquire(aio_context);
3762
Benoît Canet09158f02014-06-27 18:25:25 +02003763 if (bdrv_op_is_blocked(to_replace_bs, BLOCK_OP_TYPE_REPLACE, errp)) {
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003764 to_replace_bs = NULL;
3765 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003766 }
3767
3768 /* We don't want arbitrary node of the BDS chain to be replaced only the top
3769 * most non filter in order to prevent data corruption.
3770 * Another benefit is that this tests exclude backing files which are
3771 * blocked by the backing blockers.
3772 */
Wen Congyange12f3782015-07-17 10:12:22 +08003773 if (!bdrv_recurse_is_first_non_filter(parent_bs, to_replace_bs)) {
Benoît Canet09158f02014-06-27 18:25:25 +02003774 error_setg(errp, "Only top most non filter can be replaced");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003775 to_replace_bs = NULL;
3776 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003777 }
3778
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003779out:
3780 aio_context_release(aio_context);
Benoît Canet09158f02014-06-27 18:25:25 +02003781 return to_replace_bs;
3782}
Ming Lei448ad912014-07-04 18:04:33 +08003783
Max Reitz91af7012014-07-18 20:24:56 +02003784static bool append_open_options(QDict *d, BlockDriverState *bs)
3785{
3786 const QDictEntry *entry;
Kevin Wolf9e700c12015-04-24 15:20:28 +02003787 QemuOptDesc *desc;
Kevin Wolf260fecf2015-04-27 13:46:22 +02003788 BdrvChild *child;
Max Reitz91af7012014-07-18 20:24:56 +02003789 bool found_any = false;
Kevin Wolf260fecf2015-04-27 13:46:22 +02003790 const char *p;
Max Reitz91af7012014-07-18 20:24:56 +02003791
3792 for (entry = qdict_first(bs->options); entry;
3793 entry = qdict_next(bs->options, entry))
3794 {
Kevin Wolf260fecf2015-04-27 13:46:22 +02003795 /* Exclude options for children */
3796 QLIST_FOREACH(child, &bs->children, next) {
3797 if (strstart(qdict_entry_key(entry), child->name, &p)
3798 && (!*p || *p == '.'))
3799 {
3800 break;
3801 }
3802 }
3803 if (child) {
Kevin Wolf9e700c12015-04-24 15:20:28 +02003804 continue;
Max Reitz91af7012014-07-18 20:24:56 +02003805 }
Kevin Wolf9e700c12015-04-24 15:20:28 +02003806
3807 /* And exclude all non-driver-specific options */
3808 for (desc = bdrv_runtime_opts.desc; desc->name; desc++) {
3809 if (!strcmp(qdict_entry_key(entry), desc->name)) {
3810 break;
3811 }
3812 }
3813 if (desc->name) {
3814 continue;
3815 }
3816
3817 qobject_incref(qdict_entry_value(entry));
3818 qdict_put_obj(d, qdict_entry_key(entry), qdict_entry_value(entry));
3819 found_any = true;
Max Reitz91af7012014-07-18 20:24:56 +02003820 }
3821
3822 return found_any;
3823}
3824
3825/* Updates the following BDS fields:
3826 * - exact_filename: A filename which may be used for opening a block device
3827 * which (mostly) equals the given BDS (even without any
3828 * other options; so reading and writing must return the same
3829 * results, but caching etc. may be different)
3830 * - full_open_options: Options which, when given when opening a block device
3831 * (without a filename), result in a BDS (mostly)
3832 * equalling the given one
3833 * - filename: If exact_filename is set, it is copied here. Otherwise,
3834 * full_open_options is converted to a JSON object, prefixed with
3835 * "json:" (for use through the JSON pseudo protocol) and put here.
3836 */
3837void bdrv_refresh_filename(BlockDriverState *bs)
3838{
3839 BlockDriver *drv = bs->drv;
3840 QDict *opts;
3841
3842 if (!drv) {
3843 return;
3844 }
3845
3846 /* This BDS's file name will most probably depend on its file's name, so
3847 * refresh that first */
3848 if (bs->file) {
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003849 bdrv_refresh_filename(bs->file->bs);
Max Reitz91af7012014-07-18 20:24:56 +02003850 }
3851
3852 if (drv->bdrv_refresh_filename) {
3853 /* Obsolete information is of no use here, so drop the old file name
3854 * information before refreshing it */
3855 bs->exact_filename[0] = '\0';
3856 if (bs->full_open_options) {
3857 QDECREF(bs->full_open_options);
3858 bs->full_open_options = NULL;
3859 }
3860
Kevin Wolf4cdd01d2015-04-27 13:50:54 +02003861 opts = qdict_new();
3862 append_open_options(opts, bs);
3863 drv->bdrv_refresh_filename(bs, opts);
3864 QDECREF(opts);
Max Reitz91af7012014-07-18 20:24:56 +02003865 } else if (bs->file) {
3866 /* Try to reconstruct valid information from the underlying file */
3867 bool has_open_options;
3868
3869 bs->exact_filename[0] = '\0';
3870 if (bs->full_open_options) {
3871 QDECREF(bs->full_open_options);
3872 bs->full_open_options = NULL;
3873 }
3874
3875 opts = qdict_new();
3876 has_open_options = append_open_options(opts, bs);
3877
3878 /* If no specific options have been given for this BDS, the filename of
3879 * the underlying file should suffice for this one as well */
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003880 if (bs->file->bs->exact_filename[0] && !has_open_options) {
3881 strcpy(bs->exact_filename, bs->file->bs->exact_filename);
Max Reitz91af7012014-07-18 20:24:56 +02003882 }
3883 /* Reconstructing the full options QDict is simple for most format block
3884 * drivers, as long as the full options are known for the underlying
3885 * file BDS. The full options QDict of that file BDS should somehow
3886 * contain a representation of the filename, therefore the following
3887 * suffices without querying the (exact_)filename of this BDS. */
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003888 if (bs->file->bs->full_open_options) {
Max Reitz91af7012014-07-18 20:24:56 +02003889 qdict_put_obj(opts, "driver",
3890 QOBJECT(qstring_from_str(drv->format_name)));
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02003891 QINCREF(bs->file->bs->full_open_options);
3892 qdict_put_obj(opts, "file",
3893 QOBJECT(bs->file->bs->full_open_options));
Max Reitz91af7012014-07-18 20:24:56 +02003894
3895 bs->full_open_options = opts;
3896 } else {
3897 QDECREF(opts);
3898 }
3899 } else if (!bs->full_open_options && qdict_size(bs->options)) {
3900 /* There is no underlying file BDS (at least referenced by BDS.file),
3901 * so the full options QDict should be equal to the options given
3902 * specifically for this block device when it was opened (plus the
3903 * driver specification).
3904 * Because those options don't change, there is no need to update
3905 * full_open_options when it's already set. */
3906
3907 opts = qdict_new();
3908 append_open_options(opts, bs);
3909 qdict_put_obj(opts, "driver",
3910 QOBJECT(qstring_from_str(drv->format_name)));
3911
3912 if (bs->exact_filename[0]) {
3913 /* This may not work for all block protocol drivers (some may
3914 * require this filename to be parsed), but we have to find some
3915 * default solution here, so just include it. If some block driver
3916 * does not support pure options without any filename at all or
3917 * needs some special format of the options QDict, it needs to
3918 * implement the driver-specific bdrv_refresh_filename() function.
3919 */
3920 qdict_put_obj(opts, "filename",
3921 QOBJECT(qstring_from_str(bs->exact_filename)));
3922 }
3923
3924 bs->full_open_options = opts;
3925 }
3926
3927 if (bs->exact_filename[0]) {
3928 pstrcpy(bs->filename, sizeof(bs->filename), bs->exact_filename);
3929 } else if (bs->full_open_options) {
3930 QString *json = qobject_to_json(QOBJECT(bs->full_open_options));
3931 snprintf(bs->filename, sizeof(bs->filename), "json:%s",
3932 qstring_get_str(json));
3933 QDECREF(json);
3934 }
3935}
Wen Congyange06018a2016-05-10 15:36:37 +08003936
3937/*
3938 * Hot add/remove a BDS's child. So the user can take a child offline when
3939 * it is broken and take a new child online
3940 */
3941void bdrv_add_child(BlockDriverState *parent_bs, BlockDriverState *child_bs,
3942 Error **errp)
3943{
3944
3945 if (!parent_bs->drv || !parent_bs->drv->bdrv_add_child) {
3946 error_setg(errp, "The node %s does not support adding a child",
3947 bdrv_get_device_or_node_name(parent_bs));
3948 return;
3949 }
3950
3951 if (!QLIST_EMPTY(&child_bs->parents)) {
3952 error_setg(errp, "The node %s already has a parent",
3953 child_bs->node_name);
3954 return;
3955 }
3956
3957 parent_bs->drv->bdrv_add_child(parent_bs, child_bs, errp);
3958}
3959
3960void bdrv_del_child(BlockDriverState *parent_bs, BdrvChild *child, Error **errp)
3961{
3962 BdrvChild *tmp;
3963
3964 if (!parent_bs->drv || !parent_bs->drv->bdrv_del_child) {
3965 error_setg(errp, "The node %s does not support removing a child",
3966 bdrv_get_device_or_node_name(parent_bs));
3967 return;
3968 }
3969
3970 QLIST_FOREACH(tmp, &parent_bs->children, next) {
3971 if (tmp == child) {
3972 break;
3973 }
3974 }
3975
3976 if (!tmp) {
3977 error_setg(errp, "The node %s does not have a child named %s",
3978 bdrv_get_device_or_node_name(parent_bs),
3979 bdrv_get_device_or_node_name(child->bs));
3980 return;
3981 }
3982
3983 parent_bs->drv->bdrv_del_child(parent_bs, child, errp);
3984}