1fc01f7e7Sbellard /* 2fc01f7e7Sbellard * QEMU System Emulator block driver 3fc01f7e7Sbellard * 4fc01f7e7Sbellard * Copyright (c) 2003 Fabrice Bellard 5fc01f7e7Sbellard * 6fc01f7e7Sbellard * Permission is hereby granted, free of charge, to any person obtaining a copy 7fc01f7e7Sbellard * of this software and associated documentation files (the "Software"), to deal 8fc01f7e7Sbellard * in the Software without restriction, including without limitation the rights 9fc01f7e7Sbellard * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10fc01f7e7Sbellard * copies of the Software, and to permit persons to whom the Software is 11fc01f7e7Sbellard * furnished to do so, subject to the following conditions: 12fc01f7e7Sbellard * 13fc01f7e7Sbellard * The above copyright notice and this permission notice shall be included in 14fc01f7e7Sbellard * all copies or substantial portions of the Software. 15fc01f7e7Sbellard * 16fc01f7e7Sbellard * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17fc01f7e7Sbellard * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18fc01f7e7Sbellard * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19fc01f7e7Sbellard * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20fc01f7e7Sbellard * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21fc01f7e7Sbellard * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22fc01f7e7Sbellard * THE SOFTWARE. 23fc01f7e7Sbellard */ 24d38ea87aSPeter Maydell #include "qemu/osdep.h" 256d519a5fSStefan Hajnoczi #include "trace.h" 26737e150eSPaolo Bonzini #include "block/block_int.h" 27737e150eSPaolo Bonzini #include "block/blockjob.h" 28d49b6836SMarkus Armbruster #include "qemu/error-report.h" 291de7afc9SPaolo Bonzini #include "qemu/module.h" 30cc7a8ea7SMarkus Armbruster #include "qapi/qmp/qerror.h" 3191a097e7SKevin Wolf #include "qapi/qmp/qbool.h" 327b1b5d19SPaolo Bonzini #include "qapi/qmp/qjson.h" 33bfb197e0SMarkus Armbruster #include "sysemu/block-backend.h" 349c17d615SPaolo Bonzini #include "sysemu/sysemu.h" 351de7afc9SPaolo Bonzini #include "qemu/notify.h" 3610817bf0SDaniel P. Berrange #include "qemu/coroutine.h" 37c13163fbSBenoît Canet #include "block/qapi.h" 38b2023818SLuiz Capitulino #include "qmp-commands.h" 391de7afc9SPaolo Bonzini #include "qemu/timer.h" 40a5ee7bd4SWenchao Xia #include "qapi-event.h" 41db628338SAlberto Garcia #include "block/throttle-groups.h" 42f348b6d1SVeronia Bahaa #include "qemu/cutils.h" 43f348b6d1SVeronia Bahaa #include "qemu/id.h" 44fc01f7e7Sbellard 4571e72a19SJuan Quintela #ifdef CONFIG_BSD 467674e7bfSbellard #include <sys/ioctl.h> 4772cf2d4fSBlue Swirl #include <sys/queue.h> 48c5e97233Sblueswir1 #ifndef __DragonFly__ 497674e7bfSbellard #include <sys/disk.h> 507674e7bfSbellard #endif 51c5e97233Sblueswir1 #endif 527674e7bfSbellard 5349dc768dSaliguori #ifdef _WIN32 5449dc768dSaliguori #include <windows.h> 5549dc768dSaliguori #endif 5649dc768dSaliguori 571c9805a3SStefan Hajnoczi #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */ 581c9805a3SStefan Hajnoczi 59dc364f4cSBenoît Canet static QTAILQ_HEAD(, BlockDriverState) graph_bdrv_states = 60dc364f4cSBenoît Canet QTAILQ_HEAD_INITIALIZER(graph_bdrv_states); 61dc364f4cSBenoît Canet 622c1d04e0SMax Reitz static QTAILQ_HEAD(, BlockDriverState) all_bdrv_states = 632c1d04e0SMax Reitz QTAILQ_HEAD_INITIALIZER(all_bdrv_states); 642c1d04e0SMax Reitz 658a22f02aSStefan Hajnoczi static QLIST_HEAD(, BlockDriver) bdrv_drivers = 668a22f02aSStefan Hajnoczi QLIST_HEAD_INITIALIZER(bdrv_drivers); 67ea2384d3Sbellard 68f3930ed0SKevin Wolf static int bdrv_open_inherit(BlockDriverState **pbs, const char *filename, 69f3930ed0SKevin Wolf const char *reference, QDict *options, int flags, 70f3930ed0SKevin Wolf BlockDriverState *parent, 71ce343771SMax Reitz const BdrvChildRole *child_role, Error **errp); 72f3930ed0SKevin Wolf 73eb852011SMarkus Armbruster /* If non-zero, use only whitelisted block drivers */ 74eb852011SMarkus Armbruster static int use_bdrv_whitelist; 75eb852011SMarkus Armbruster 7664dff520SMax Reitz static void bdrv_close(BlockDriverState *bs); 7764dff520SMax Reitz 789e0b22f4SStefan Hajnoczi #ifdef _WIN32 799e0b22f4SStefan Hajnoczi static int is_windows_drive_prefix(const char *filename) 809e0b22f4SStefan Hajnoczi { 819e0b22f4SStefan Hajnoczi return (((filename[0] >= 'a' && filename[0] <= 'z') || 829e0b22f4SStefan Hajnoczi (filename[0] >= 'A' && filename[0] <= 'Z')) && 839e0b22f4SStefan Hajnoczi filename[1] == ':'); 849e0b22f4SStefan Hajnoczi } 859e0b22f4SStefan Hajnoczi 869e0b22f4SStefan Hajnoczi int is_windows_drive(const char *filename) 879e0b22f4SStefan Hajnoczi { 889e0b22f4SStefan Hajnoczi if (is_windows_drive_prefix(filename) && 899e0b22f4SStefan Hajnoczi filename[2] == '\0') 909e0b22f4SStefan Hajnoczi return 1; 919e0b22f4SStefan Hajnoczi if (strstart(filename, "\\\\.\\", NULL) || 929e0b22f4SStefan Hajnoczi strstart(filename, "//./", NULL)) 939e0b22f4SStefan Hajnoczi return 1; 949e0b22f4SStefan Hajnoczi return 0; 959e0b22f4SStefan Hajnoczi } 969e0b22f4SStefan Hajnoczi #endif 979e0b22f4SStefan Hajnoczi 98339064d5SKevin Wolf size_t bdrv_opt_mem_align(BlockDriverState *bs) 99339064d5SKevin Wolf { 100339064d5SKevin Wolf if (!bs || !bs->drv) { 101459b4e66SDenis V. Lunev /* page size or 4k (hdd sector size) should be on the safe side */ 102459b4e66SDenis V. Lunev return MAX(4096, getpagesize()); 103339064d5SKevin Wolf } 104339064d5SKevin Wolf 105339064d5SKevin Wolf return bs->bl.opt_mem_alignment; 106339064d5SKevin Wolf } 107339064d5SKevin Wolf 1084196d2f0SDenis V. Lunev size_t bdrv_min_mem_align(BlockDriverState *bs) 1094196d2f0SDenis V. Lunev { 1104196d2f0SDenis V. Lunev if (!bs || !bs->drv) { 111459b4e66SDenis V. Lunev /* page size or 4k (hdd sector size) should be on the safe side */ 112459b4e66SDenis V. Lunev return MAX(4096, getpagesize()); 1134196d2f0SDenis V. Lunev } 1144196d2f0SDenis V. Lunev 1154196d2f0SDenis V. Lunev return bs->bl.min_mem_alignment; 1164196d2f0SDenis V. Lunev } 1174196d2f0SDenis V. Lunev 1189e0b22f4SStefan Hajnoczi /* check if the path starts with "<protocol>:" */ 1195c98415bSMax Reitz int path_has_protocol(const char *path) 1209e0b22f4SStefan Hajnoczi { 121947995c0SPaolo Bonzini const char *p; 122947995c0SPaolo Bonzini 1239e0b22f4SStefan Hajnoczi #ifdef _WIN32 1249e0b22f4SStefan Hajnoczi if (is_windows_drive(path) || 1259e0b22f4SStefan Hajnoczi is_windows_drive_prefix(path)) { 1269e0b22f4SStefan Hajnoczi return 0; 1279e0b22f4SStefan Hajnoczi } 128947995c0SPaolo Bonzini p = path + strcspn(path, ":/\\"); 129947995c0SPaolo Bonzini #else 130947995c0SPaolo Bonzini p = path + strcspn(path, ":/"); 1319e0b22f4SStefan Hajnoczi #endif 1329e0b22f4SStefan Hajnoczi 133947995c0SPaolo Bonzini return *p == ':'; 1349e0b22f4SStefan Hajnoczi } 1359e0b22f4SStefan Hajnoczi 13683f64091Sbellard int path_is_absolute(const char *path) 13783f64091Sbellard { 13821664424Sbellard #ifdef _WIN32 13921664424Sbellard /* specific case for names like: "\\.\d:" */ 140f53f4da9SPaolo Bonzini if (is_windows_drive(path) || is_windows_drive_prefix(path)) { 14121664424Sbellard return 1; 142f53f4da9SPaolo Bonzini } 143f53f4da9SPaolo Bonzini return (*path == '/' || *path == '\\'); 1443b9f94e1Sbellard #else 145f53f4da9SPaolo Bonzini return (*path == '/'); 1463b9f94e1Sbellard #endif 14783f64091Sbellard } 14883f64091Sbellard 14983f64091Sbellard /* if filename is absolute, just copy it to dest. Otherwise, build a 15083f64091Sbellard path to it by considering it is relative to base_path. URL are 15183f64091Sbellard supported. */ 15283f64091Sbellard void path_combine(char *dest, int dest_size, 15383f64091Sbellard const char *base_path, 15483f64091Sbellard const char *filename) 15583f64091Sbellard { 15683f64091Sbellard const char *p, *p1; 15783f64091Sbellard int len; 15883f64091Sbellard 15983f64091Sbellard if (dest_size <= 0) 16083f64091Sbellard return; 16183f64091Sbellard if (path_is_absolute(filename)) { 16283f64091Sbellard pstrcpy(dest, dest_size, filename); 16383f64091Sbellard } else { 16483f64091Sbellard p = strchr(base_path, ':'); 16583f64091Sbellard if (p) 16683f64091Sbellard p++; 16783f64091Sbellard else 16883f64091Sbellard p = base_path; 1693b9f94e1Sbellard p1 = strrchr(base_path, '/'); 1703b9f94e1Sbellard #ifdef _WIN32 1713b9f94e1Sbellard { 1723b9f94e1Sbellard const char *p2; 1733b9f94e1Sbellard p2 = strrchr(base_path, '\\'); 1743b9f94e1Sbellard if (!p1 || p2 > p1) 1753b9f94e1Sbellard p1 = p2; 1763b9f94e1Sbellard } 1773b9f94e1Sbellard #endif 17883f64091Sbellard if (p1) 17983f64091Sbellard p1++; 18083f64091Sbellard else 18183f64091Sbellard p1 = base_path; 18283f64091Sbellard if (p1 > p) 18383f64091Sbellard p = p1; 18483f64091Sbellard len = p - base_path; 18583f64091Sbellard if (len > dest_size - 1) 18683f64091Sbellard len = dest_size - 1; 18783f64091Sbellard memcpy(dest, base_path, len); 18883f64091Sbellard dest[len] = '\0'; 18983f64091Sbellard pstrcat(dest, dest_size, filename); 19083f64091Sbellard } 19183f64091Sbellard } 19283f64091Sbellard 1930a82855aSMax Reitz void bdrv_get_full_backing_filename_from_filename(const char *backed, 1940a82855aSMax Reitz const char *backing, 1959f07429eSMax Reitz char *dest, size_t sz, 1969f07429eSMax Reitz Error **errp) 1970a82855aSMax Reitz { 1989f07429eSMax Reitz if (backing[0] == '\0' || path_has_protocol(backing) || 1999f07429eSMax Reitz path_is_absolute(backing)) 2009f07429eSMax Reitz { 2010a82855aSMax Reitz pstrcpy(dest, sz, backing); 2029f07429eSMax Reitz } else if (backed[0] == '\0' || strstart(backed, "json:", NULL)) { 2039f07429eSMax Reitz error_setg(errp, "Cannot use relative backing file names for '%s'", 2049f07429eSMax Reitz backed); 2050a82855aSMax Reitz } else { 2060a82855aSMax Reitz path_combine(dest, sz, backed, backing); 2070a82855aSMax Reitz } 2080a82855aSMax Reitz } 2090a82855aSMax Reitz 2109f07429eSMax Reitz void bdrv_get_full_backing_filename(BlockDriverState *bs, char *dest, size_t sz, 2119f07429eSMax Reitz Error **errp) 212dc5a1371SPaolo Bonzini { 2139f07429eSMax Reitz char *backed = bs->exact_filename[0] ? bs->exact_filename : bs->filename; 2149f07429eSMax Reitz 2159f07429eSMax Reitz bdrv_get_full_backing_filename_from_filename(backed, bs->backing_file, 2169f07429eSMax Reitz dest, sz, errp); 217dc5a1371SPaolo Bonzini } 218dc5a1371SPaolo Bonzini 2190eb7217eSStefan Hajnoczi void bdrv_register(BlockDriver *bdrv) 2200eb7217eSStefan Hajnoczi { 2218a22f02aSStefan Hajnoczi QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list); 222ea2384d3Sbellard } 223b338082bSbellard 2247f06d47eSMarkus Armbruster BlockDriverState *bdrv_new_root(void) 225fc01f7e7Sbellard { 2269aaf28c6SMax Reitz return bdrv_new(); 227e4e9986bSMarkus Armbruster } 228e4e9986bSMarkus Armbruster 229e4e9986bSMarkus Armbruster BlockDriverState *bdrv_new(void) 230e4e9986bSMarkus Armbruster { 231e4e9986bSMarkus Armbruster BlockDriverState *bs; 232e4e9986bSMarkus Armbruster int i; 233e4e9986bSMarkus Armbruster 2345839e53bSMarkus Armbruster bs = g_new0(BlockDriverState, 1); 235e4654d2dSFam Zheng QLIST_INIT(&bs->dirty_bitmaps); 236fbe40ff7SFam Zheng for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) { 237fbe40ff7SFam Zheng QLIST_INIT(&bs->op_blockers[i]); 238fbe40ff7SFam Zheng } 239d616b224SStefan Hajnoczi notifier_with_return_list_init(&bs->before_write_notifiers); 240cc0681c4SBenoît Canet qemu_co_queue_init(&bs->throttled_reqs[0]); 241cc0681c4SBenoît Canet qemu_co_queue_init(&bs->throttled_reqs[1]); 2429fcb0251SFam Zheng bs->refcnt = 1; 243dcd04228SStefan Hajnoczi bs->aio_context = qemu_get_aio_context(); 244d7d512f6SPaolo Bonzini 2452c1d04e0SMax Reitz QTAILQ_INSERT_TAIL(&all_bdrv_states, bs, bs_list); 2462c1d04e0SMax Reitz 247b338082bSbellard return bs; 248b338082bSbellard } 249b338082bSbellard 250ea2384d3Sbellard BlockDriver *bdrv_find_format(const char *format_name) 251ea2384d3Sbellard { 252ea2384d3Sbellard BlockDriver *drv1; 2538a22f02aSStefan Hajnoczi QLIST_FOREACH(drv1, &bdrv_drivers, list) { 2548a22f02aSStefan Hajnoczi if (!strcmp(drv1->format_name, format_name)) { 255ea2384d3Sbellard return drv1; 256ea2384d3Sbellard } 2578a22f02aSStefan Hajnoczi } 258ea2384d3Sbellard return NULL; 259ea2384d3Sbellard } 260ea2384d3Sbellard 261b64ec4e4SFam Zheng static int bdrv_is_whitelisted(BlockDriver *drv, bool read_only) 262eb852011SMarkus Armbruster { 263b64ec4e4SFam Zheng static const char *whitelist_rw[] = { 264b64ec4e4SFam Zheng CONFIG_BDRV_RW_WHITELIST 265b64ec4e4SFam Zheng }; 266b64ec4e4SFam Zheng static const char *whitelist_ro[] = { 267b64ec4e4SFam Zheng CONFIG_BDRV_RO_WHITELIST 268eb852011SMarkus Armbruster }; 269eb852011SMarkus Armbruster const char **p; 270eb852011SMarkus Armbruster 271b64ec4e4SFam Zheng if (!whitelist_rw[0] && !whitelist_ro[0]) { 272eb852011SMarkus Armbruster return 1; /* no whitelist, anything goes */ 273b64ec4e4SFam Zheng } 274eb852011SMarkus Armbruster 275b64ec4e4SFam Zheng for (p = whitelist_rw; *p; p++) { 276eb852011SMarkus Armbruster if (!strcmp(drv->format_name, *p)) { 277eb852011SMarkus Armbruster return 1; 278eb852011SMarkus Armbruster } 279eb852011SMarkus Armbruster } 280b64ec4e4SFam Zheng if (read_only) { 281b64ec4e4SFam Zheng for (p = whitelist_ro; *p; p++) { 282b64ec4e4SFam Zheng if (!strcmp(drv->format_name, *p)) { 283b64ec4e4SFam Zheng return 1; 284b64ec4e4SFam Zheng } 285b64ec4e4SFam Zheng } 286b64ec4e4SFam Zheng } 287eb852011SMarkus Armbruster return 0; 288eb852011SMarkus Armbruster } 289eb852011SMarkus Armbruster 290e6ff69bfSDaniel P. Berrange bool bdrv_uses_whitelist(void) 291e6ff69bfSDaniel P. Berrange { 292e6ff69bfSDaniel P. Berrange return use_bdrv_whitelist; 293e6ff69bfSDaniel P. Berrange } 294e6ff69bfSDaniel P. Berrange 2955b7e1542SZhi Yong Wu typedef struct CreateCo { 2965b7e1542SZhi Yong Wu BlockDriver *drv; 2975b7e1542SZhi Yong Wu char *filename; 29883d0521aSChunyan Liu QemuOpts *opts; 2995b7e1542SZhi Yong Wu int ret; 300cc84d90fSMax Reitz Error *err; 3015b7e1542SZhi Yong Wu } CreateCo; 3025b7e1542SZhi Yong Wu 3035b7e1542SZhi Yong Wu static void coroutine_fn bdrv_create_co_entry(void *opaque) 3045b7e1542SZhi Yong Wu { 305cc84d90fSMax Reitz Error *local_err = NULL; 306cc84d90fSMax Reitz int ret; 307cc84d90fSMax Reitz 3085b7e1542SZhi Yong Wu CreateCo *cco = opaque; 3095b7e1542SZhi Yong Wu assert(cco->drv); 3105b7e1542SZhi Yong Wu 311c282e1fdSChunyan Liu ret = cco->drv->bdrv_create(cco->filename, cco->opts, &local_err); 31284d18f06SMarkus Armbruster if (local_err) { 313cc84d90fSMax Reitz error_propagate(&cco->err, local_err); 314cc84d90fSMax Reitz } 315cc84d90fSMax Reitz cco->ret = ret; 3165b7e1542SZhi Yong Wu } 3175b7e1542SZhi Yong Wu 3180e7e1989SKevin Wolf int bdrv_create(BlockDriver *drv, const char* filename, 31983d0521aSChunyan Liu QemuOpts *opts, Error **errp) 320ea2384d3Sbellard { 3215b7e1542SZhi Yong Wu int ret; 3220e7e1989SKevin Wolf 3235b7e1542SZhi Yong Wu Coroutine *co; 3245b7e1542SZhi Yong Wu CreateCo cco = { 3255b7e1542SZhi Yong Wu .drv = drv, 3265b7e1542SZhi Yong Wu .filename = g_strdup(filename), 32783d0521aSChunyan Liu .opts = opts, 3285b7e1542SZhi Yong Wu .ret = NOT_DONE, 329cc84d90fSMax Reitz .err = NULL, 3305b7e1542SZhi Yong Wu }; 3315b7e1542SZhi Yong Wu 332c282e1fdSChunyan Liu if (!drv->bdrv_create) { 333cc84d90fSMax Reitz error_setg(errp, "Driver '%s' does not support image creation", drv->format_name); 33480168bffSLuiz Capitulino ret = -ENOTSUP; 33580168bffSLuiz Capitulino goto out; 3365b7e1542SZhi Yong Wu } 3375b7e1542SZhi Yong Wu 3385b7e1542SZhi Yong Wu if (qemu_in_coroutine()) { 3395b7e1542SZhi Yong Wu /* Fast-path if already in coroutine context */ 3405b7e1542SZhi Yong Wu bdrv_create_co_entry(&cco); 3415b7e1542SZhi Yong Wu } else { 3425b7e1542SZhi Yong Wu co = qemu_coroutine_create(bdrv_create_co_entry); 3435b7e1542SZhi Yong Wu qemu_coroutine_enter(co, &cco); 3445b7e1542SZhi Yong Wu while (cco.ret == NOT_DONE) { 345b47ec2c4SPaolo Bonzini aio_poll(qemu_get_aio_context(), true); 3465b7e1542SZhi Yong Wu } 3475b7e1542SZhi Yong Wu } 3485b7e1542SZhi Yong Wu 3495b7e1542SZhi Yong Wu ret = cco.ret; 350cc84d90fSMax Reitz if (ret < 0) { 35184d18f06SMarkus Armbruster if (cco.err) { 352cc84d90fSMax Reitz error_propagate(errp, cco.err); 353cc84d90fSMax Reitz } else { 354cc84d90fSMax Reitz error_setg_errno(errp, -ret, "Could not create image"); 355cc84d90fSMax Reitz } 356cc84d90fSMax Reitz } 3575b7e1542SZhi Yong Wu 35880168bffSLuiz Capitulino out: 35980168bffSLuiz Capitulino g_free(cco.filename); 3605b7e1542SZhi Yong Wu return ret; 361ea2384d3Sbellard } 362ea2384d3Sbellard 363c282e1fdSChunyan Liu int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp) 36484a12e66SChristoph Hellwig { 36584a12e66SChristoph Hellwig BlockDriver *drv; 366cc84d90fSMax Reitz Error *local_err = NULL; 367cc84d90fSMax Reitz int ret; 36884a12e66SChristoph Hellwig 369b65a5e12SMax Reitz drv = bdrv_find_protocol(filename, true, errp); 37084a12e66SChristoph Hellwig if (drv == NULL) { 37116905d71SStefan Hajnoczi return -ENOENT; 37284a12e66SChristoph Hellwig } 37384a12e66SChristoph Hellwig 374c282e1fdSChunyan Liu ret = bdrv_create(drv, filename, opts, &local_err); 37584d18f06SMarkus Armbruster if (local_err) { 376cc84d90fSMax Reitz error_propagate(errp, local_err); 377cc84d90fSMax Reitz } 378cc84d90fSMax Reitz return ret; 37984a12e66SChristoph Hellwig } 38084a12e66SChristoph Hellwig 381892b7de8SEkaterina Tumanova /** 382892b7de8SEkaterina Tumanova * Try to get @bs's logical and physical block size. 383892b7de8SEkaterina Tumanova * On success, store them in @bsz struct and return 0. 384892b7de8SEkaterina Tumanova * On failure return -errno. 385892b7de8SEkaterina Tumanova * @bs must not be empty. 386892b7de8SEkaterina Tumanova */ 387892b7de8SEkaterina Tumanova int bdrv_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz) 388892b7de8SEkaterina Tumanova { 389892b7de8SEkaterina Tumanova BlockDriver *drv = bs->drv; 390892b7de8SEkaterina Tumanova 391892b7de8SEkaterina Tumanova if (drv && drv->bdrv_probe_blocksizes) { 392892b7de8SEkaterina Tumanova return drv->bdrv_probe_blocksizes(bs, bsz); 393892b7de8SEkaterina Tumanova } 394892b7de8SEkaterina Tumanova 395892b7de8SEkaterina Tumanova return -ENOTSUP; 396892b7de8SEkaterina Tumanova } 397892b7de8SEkaterina Tumanova 398892b7de8SEkaterina Tumanova /** 399892b7de8SEkaterina Tumanova * Try to get @bs's geometry (cyls, heads, sectors). 400892b7de8SEkaterina Tumanova * On success, store them in @geo struct and return 0. 401892b7de8SEkaterina Tumanova * On failure return -errno. 402892b7de8SEkaterina Tumanova * @bs must not be empty. 403892b7de8SEkaterina Tumanova */ 404892b7de8SEkaterina Tumanova int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo) 405892b7de8SEkaterina Tumanova { 406892b7de8SEkaterina Tumanova BlockDriver *drv = bs->drv; 407892b7de8SEkaterina Tumanova 408892b7de8SEkaterina Tumanova if (drv && drv->bdrv_probe_geometry) { 409892b7de8SEkaterina Tumanova return drv->bdrv_probe_geometry(bs, geo); 410892b7de8SEkaterina Tumanova } 411892b7de8SEkaterina Tumanova 412892b7de8SEkaterina Tumanova return -ENOTSUP; 413892b7de8SEkaterina Tumanova } 414892b7de8SEkaterina Tumanova 415eba25057SJim Meyering /* 416eba25057SJim Meyering * Create a uniquely-named empty temporary file. 417eba25057SJim Meyering * Return 0 upon success, otherwise a negative errno value. 418eba25057SJim Meyering */ 419eba25057SJim Meyering int get_tmp_filename(char *filename, int size) 420eba25057SJim Meyering { 421d5249393Sbellard #ifdef _WIN32 4223b9f94e1Sbellard char temp_dir[MAX_PATH]; 423eba25057SJim Meyering /* GetTempFileName requires that its output buffer (4th param) 424eba25057SJim Meyering have length MAX_PATH or greater. */ 425eba25057SJim Meyering assert(size >= MAX_PATH); 426eba25057SJim Meyering return (GetTempPath(MAX_PATH, temp_dir) 427eba25057SJim Meyering && GetTempFileName(temp_dir, "qem", 0, filename) 428eba25057SJim Meyering ? 0 : -GetLastError()); 429d5249393Sbellard #else 430ea2384d3Sbellard int fd; 4317ccfb2ebSblueswir1 const char *tmpdir; 4320badc1eeSaurel32 tmpdir = getenv("TMPDIR"); 43369bef793SAmit Shah if (!tmpdir) { 43469bef793SAmit Shah tmpdir = "/var/tmp"; 43569bef793SAmit Shah } 436eba25057SJim Meyering if (snprintf(filename, size, "%s/vl.XXXXXX", tmpdir) >= size) { 437eba25057SJim Meyering return -EOVERFLOW; 438ea2384d3Sbellard } 439eba25057SJim Meyering fd = mkstemp(filename); 440fe235a06SDunrong Huang if (fd < 0) { 441fe235a06SDunrong Huang return -errno; 442fe235a06SDunrong Huang } 443fe235a06SDunrong Huang if (close(fd) != 0) { 444fe235a06SDunrong Huang unlink(filename); 445eba25057SJim Meyering return -errno; 446eba25057SJim Meyering } 447eba25057SJim Meyering return 0; 448d5249393Sbellard #endif 449eba25057SJim Meyering } 450ea2384d3Sbellard 451f3a5d3f8SChristoph Hellwig /* 452f3a5d3f8SChristoph Hellwig * Detect host devices. By convention, /dev/cdrom[N] is always 453f3a5d3f8SChristoph Hellwig * recognized as a host CDROM. 454f3a5d3f8SChristoph Hellwig */ 455f3a5d3f8SChristoph Hellwig static BlockDriver *find_hdev_driver(const char *filename) 456f3a5d3f8SChristoph Hellwig { 457508c7cb3SChristoph Hellwig int score_max = 0, score; 458508c7cb3SChristoph Hellwig BlockDriver *drv = NULL, *d; 459f3a5d3f8SChristoph Hellwig 4608a22f02aSStefan Hajnoczi QLIST_FOREACH(d, &bdrv_drivers, list) { 461508c7cb3SChristoph Hellwig if (d->bdrv_probe_device) { 462508c7cb3SChristoph Hellwig score = d->bdrv_probe_device(filename); 463508c7cb3SChristoph Hellwig if (score > score_max) { 464508c7cb3SChristoph Hellwig score_max = score; 465508c7cb3SChristoph Hellwig drv = d; 466f3a5d3f8SChristoph Hellwig } 467508c7cb3SChristoph Hellwig } 468f3a5d3f8SChristoph Hellwig } 469f3a5d3f8SChristoph Hellwig 470508c7cb3SChristoph Hellwig return drv; 471f3a5d3f8SChristoph Hellwig } 472f3a5d3f8SChristoph Hellwig 47398289620SKevin Wolf BlockDriver *bdrv_find_protocol(const char *filename, 474b65a5e12SMax Reitz bool allow_protocol_prefix, 475b65a5e12SMax Reitz Error **errp) 47684a12e66SChristoph Hellwig { 47784a12e66SChristoph Hellwig BlockDriver *drv1; 47884a12e66SChristoph Hellwig char protocol[128]; 47984a12e66SChristoph Hellwig int len; 48084a12e66SChristoph Hellwig const char *p; 48184a12e66SChristoph Hellwig 48266f82ceeSKevin Wolf /* TODO Drivers without bdrv_file_open must be specified explicitly */ 48366f82ceeSKevin Wolf 48439508e7aSChristoph Hellwig /* 48539508e7aSChristoph Hellwig * XXX(hch): we really should not let host device detection 48639508e7aSChristoph Hellwig * override an explicit protocol specification, but moving this 48739508e7aSChristoph Hellwig * later breaks access to device names with colons in them. 48839508e7aSChristoph Hellwig * Thanks to the brain-dead persistent naming schemes on udev- 48939508e7aSChristoph Hellwig * based Linux systems those actually are quite common. 49039508e7aSChristoph Hellwig */ 49184a12e66SChristoph Hellwig drv1 = find_hdev_driver(filename); 49239508e7aSChristoph Hellwig if (drv1) { 49384a12e66SChristoph Hellwig return drv1; 49484a12e66SChristoph Hellwig } 49539508e7aSChristoph Hellwig 49698289620SKevin Wolf if (!path_has_protocol(filename) || !allow_protocol_prefix) { 497ef810437SMax Reitz return &bdrv_file; 49839508e7aSChristoph Hellwig } 49998289620SKevin Wolf 5009e0b22f4SStefan Hajnoczi p = strchr(filename, ':'); 5019e0b22f4SStefan Hajnoczi assert(p != NULL); 50284a12e66SChristoph Hellwig len = p - filename; 50384a12e66SChristoph Hellwig if (len > sizeof(protocol) - 1) 50484a12e66SChristoph Hellwig len = sizeof(protocol) - 1; 50584a12e66SChristoph Hellwig memcpy(protocol, filename, len); 50684a12e66SChristoph Hellwig protocol[len] = '\0'; 50784a12e66SChristoph Hellwig QLIST_FOREACH(drv1, &bdrv_drivers, list) { 50884a12e66SChristoph Hellwig if (drv1->protocol_name && 50984a12e66SChristoph Hellwig !strcmp(drv1->protocol_name, protocol)) { 51084a12e66SChristoph Hellwig return drv1; 51184a12e66SChristoph Hellwig } 51284a12e66SChristoph Hellwig } 513b65a5e12SMax Reitz 514b65a5e12SMax Reitz error_setg(errp, "Unknown protocol '%s'", protocol); 51584a12e66SChristoph Hellwig return NULL; 51684a12e66SChristoph Hellwig } 51784a12e66SChristoph Hellwig 518c6684249SMarkus Armbruster /* 519c6684249SMarkus Armbruster * Guess image format by probing its contents. 520c6684249SMarkus Armbruster * This is not a good idea when your image is raw (CVE-2008-2004), but 521c6684249SMarkus Armbruster * we do it anyway for backward compatibility. 522c6684249SMarkus Armbruster * 523c6684249SMarkus Armbruster * @buf contains the image's first @buf_size bytes. 5247cddd372SKevin Wolf * @buf_size is the buffer size in bytes (generally BLOCK_PROBE_BUF_SIZE, 5257cddd372SKevin Wolf * but can be smaller if the image file is smaller) 526c6684249SMarkus Armbruster * @filename is its filename. 527c6684249SMarkus Armbruster * 528c6684249SMarkus Armbruster * For all block drivers, call the bdrv_probe() method to get its 529c6684249SMarkus Armbruster * probing score. 530c6684249SMarkus Armbruster * Return the first block driver with the highest probing score. 531c6684249SMarkus Armbruster */ 53238f3ef57SKevin Wolf BlockDriver *bdrv_probe_all(const uint8_t *buf, int buf_size, 533c6684249SMarkus Armbruster const char *filename) 534c6684249SMarkus Armbruster { 535c6684249SMarkus Armbruster int score_max = 0, score; 536c6684249SMarkus Armbruster BlockDriver *drv = NULL, *d; 537c6684249SMarkus Armbruster 538c6684249SMarkus Armbruster QLIST_FOREACH(d, &bdrv_drivers, list) { 539c6684249SMarkus Armbruster if (d->bdrv_probe) { 540c6684249SMarkus Armbruster score = d->bdrv_probe(buf, buf_size, filename); 541c6684249SMarkus Armbruster if (score > score_max) { 542c6684249SMarkus Armbruster score_max = score; 543c6684249SMarkus Armbruster drv = d; 544c6684249SMarkus Armbruster } 545c6684249SMarkus Armbruster } 546c6684249SMarkus Armbruster } 547c6684249SMarkus Armbruster 548c6684249SMarkus Armbruster return drv; 549c6684249SMarkus Armbruster } 550c6684249SMarkus Armbruster 551f500a6d3SKevin Wolf static int find_image_format(BlockDriverState *bs, const char *filename, 55234b5d2c6SMax Reitz BlockDriver **pdrv, Error **errp) 553ea2384d3Sbellard { 554c6684249SMarkus Armbruster BlockDriver *drv; 5557cddd372SKevin Wolf uint8_t buf[BLOCK_PROBE_BUF_SIZE]; 556f500a6d3SKevin Wolf int ret = 0; 557f8ea0b00SNicholas Bellinger 55808a00559SKevin Wolf /* Return the raw BlockDriver * to scsi-generic devices or empty drives */ 559b192af8aSDimitris Aragiorgis if (bdrv_is_sg(bs) || !bdrv_is_inserted(bs) || bdrv_getlength(bs) == 0) { 560ef810437SMax Reitz *pdrv = &bdrv_raw; 561c98ac35dSStefan Weil return ret; 5621a396859SNicholas A. Bellinger } 563f8ea0b00SNicholas Bellinger 56483f64091Sbellard ret = bdrv_pread(bs, 0, buf, sizeof(buf)); 565ea2384d3Sbellard if (ret < 0) { 56634b5d2c6SMax Reitz error_setg_errno(errp, -ret, "Could not read image for determining its " 56734b5d2c6SMax Reitz "format"); 568c98ac35dSStefan Weil *pdrv = NULL; 569c98ac35dSStefan Weil return ret; 570ea2384d3Sbellard } 571ea2384d3Sbellard 572c6684249SMarkus Armbruster drv = bdrv_probe_all(buf, ret, filename); 573c98ac35dSStefan Weil if (!drv) { 57434b5d2c6SMax Reitz error_setg(errp, "Could not determine image format: No compatible " 57534b5d2c6SMax Reitz "driver found"); 576c98ac35dSStefan Weil ret = -ENOENT; 577c98ac35dSStefan Weil } 578c98ac35dSStefan Weil *pdrv = drv; 579c98ac35dSStefan Weil return ret; 580ea2384d3Sbellard } 581ea2384d3Sbellard 58251762288SStefan Hajnoczi /** 58351762288SStefan Hajnoczi * Set the current 'total_sectors' value 58465a9bb25SMarkus Armbruster * Return 0 on success, -errno on error. 58551762288SStefan Hajnoczi */ 58651762288SStefan Hajnoczi static int refresh_total_sectors(BlockDriverState *bs, int64_t hint) 58751762288SStefan Hajnoczi { 58851762288SStefan Hajnoczi BlockDriver *drv = bs->drv; 58951762288SStefan Hajnoczi 590396759adSNicholas Bellinger /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */ 591b192af8aSDimitris Aragiorgis if (bdrv_is_sg(bs)) 592396759adSNicholas Bellinger return 0; 593396759adSNicholas Bellinger 59451762288SStefan Hajnoczi /* query actual device if possible, otherwise just trust the hint */ 59551762288SStefan Hajnoczi if (drv->bdrv_getlength) { 59651762288SStefan Hajnoczi int64_t length = drv->bdrv_getlength(bs); 59751762288SStefan Hajnoczi if (length < 0) { 59851762288SStefan Hajnoczi return length; 59951762288SStefan Hajnoczi } 6007e382003SFam Zheng hint = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE); 60151762288SStefan Hajnoczi } 60251762288SStefan Hajnoczi 60351762288SStefan Hajnoczi bs->total_sectors = hint; 60451762288SStefan Hajnoczi return 0; 60551762288SStefan Hajnoczi } 60651762288SStefan Hajnoczi 607c3993cdcSStefan Hajnoczi /** 608cddff5baSKevin Wolf * Combines a QDict of new block driver @options with any missing options taken 609cddff5baSKevin Wolf * from @old_options, so that leaving out an option defaults to its old value. 610cddff5baSKevin Wolf */ 611cddff5baSKevin Wolf static void bdrv_join_options(BlockDriverState *bs, QDict *options, 612cddff5baSKevin Wolf QDict *old_options) 613cddff5baSKevin Wolf { 614cddff5baSKevin Wolf if (bs->drv && bs->drv->bdrv_join_options) { 615cddff5baSKevin Wolf bs->drv->bdrv_join_options(options, old_options); 616cddff5baSKevin Wolf } else { 617cddff5baSKevin Wolf qdict_join(options, old_options, false); 618cddff5baSKevin Wolf } 619cddff5baSKevin Wolf } 620cddff5baSKevin Wolf 621cddff5baSKevin Wolf /** 6229e8f1835SPaolo Bonzini * Set open flags for a given discard mode 6239e8f1835SPaolo Bonzini * 6249e8f1835SPaolo Bonzini * Return 0 on success, -1 if the discard mode was invalid. 6259e8f1835SPaolo Bonzini */ 6269e8f1835SPaolo Bonzini int bdrv_parse_discard_flags(const char *mode, int *flags) 6279e8f1835SPaolo Bonzini { 6289e8f1835SPaolo Bonzini *flags &= ~BDRV_O_UNMAP; 6299e8f1835SPaolo Bonzini 6309e8f1835SPaolo Bonzini if (!strcmp(mode, "off") || !strcmp(mode, "ignore")) { 6319e8f1835SPaolo Bonzini /* do nothing */ 6329e8f1835SPaolo Bonzini } else if (!strcmp(mode, "on") || !strcmp(mode, "unmap")) { 6339e8f1835SPaolo Bonzini *flags |= BDRV_O_UNMAP; 6349e8f1835SPaolo Bonzini } else { 6359e8f1835SPaolo Bonzini return -1; 6369e8f1835SPaolo Bonzini } 6379e8f1835SPaolo Bonzini 6389e8f1835SPaolo Bonzini return 0; 6399e8f1835SPaolo Bonzini } 6409e8f1835SPaolo Bonzini 6419e8f1835SPaolo Bonzini /** 642c3993cdcSStefan Hajnoczi * Set open flags for a given cache mode 643c3993cdcSStefan Hajnoczi * 644c3993cdcSStefan Hajnoczi * Return 0 on success, -1 if the cache mode was invalid. 645c3993cdcSStefan Hajnoczi */ 64653e8ae01SKevin Wolf int bdrv_parse_cache_mode(const char *mode, int *flags, bool *writethrough) 647c3993cdcSStefan Hajnoczi { 648c3993cdcSStefan Hajnoczi *flags &= ~BDRV_O_CACHE_MASK; 649c3993cdcSStefan Hajnoczi 650c3993cdcSStefan Hajnoczi if (!strcmp(mode, "off") || !strcmp(mode, "none")) { 65153e8ae01SKevin Wolf *writethrough = false; 65253e8ae01SKevin Wolf *flags |= BDRV_O_NOCACHE; 65392196b2fSStefan Hajnoczi } else if (!strcmp(mode, "directsync")) { 65453e8ae01SKevin Wolf *writethrough = true; 65592196b2fSStefan Hajnoczi *flags |= BDRV_O_NOCACHE; 656c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "writeback")) { 65753e8ae01SKevin Wolf *writethrough = false; 658c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "unsafe")) { 65953e8ae01SKevin Wolf *writethrough = false; 660c3993cdcSStefan Hajnoczi *flags |= BDRV_O_NO_FLUSH; 661c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "writethrough")) { 66253e8ae01SKevin Wolf *writethrough = true; 663c3993cdcSStefan Hajnoczi } else { 664c3993cdcSStefan Hajnoczi return -1; 665c3993cdcSStefan Hajnoczi } 666c3993cdcSStefan Hajnoczi 667c3993cdcSStefan Hajnoczi return 0; 668c3993cdcSStefan Hajnoczi } 669c3993cdcSStefan Hajnoczi 6700b50cc88SKevin Wolf /* 67173176beeSKevin Wolf * Returns the options and flags that a temporary snapshot should get, based on 67273176beeSKevin Wolf * the originally requested flags (the originally requested image will have 67373176beeSKevin Wolf * flags like a backing file) 674b1e6fc08SKevin Wolf */ 67573176beeSKevin Wolf static void bdrv_temp_snapshot_options(int *child_flags, QDict *child_options, 67673176beeSKevin Wolf int parent_flags, QDict *parent_options) 677b1e6fc08SKevin Wolf { 67873176beeSKevin Wolf *child_flags = (parent_flags & ~BDRV_O_SNAPSHOT) | BDRV_O_TEMPORARY; 67973176beeSKevin Wolf 68073176beeSKevin Wolf /* For temporary files, unconditional cache=unsafe is fine */ 68173176beeSKevin Wolf qdict_set_default_str(child_options, BDRV_OPT_CACHE_DIRECT, "off"); 68273176beeSKevin Wolf qdict_set_default_str(child_options, BDRV_OPT_CACHE_NO_FLUSH, "on"); 683b1e6fc08SKevin Wolf } 684b1e6fc08SKevin Wolf 685b1e6fc08SKevin Wolf /* 6868e2160e2SKevin Wolf * Returns the options and flags that bs->file should get if a protocol driver 6878e2160e2SKevin Wolf * is expected, based on the given options and flags for the parent BDS 6880b50cc88SKevin Wolf */ 6898e2160e2SKevin Wolf static void bdrv_inherited_options(int *child_flags, QDict *child_options, 6908e2160e2SKevin Wolf int parent_flags, QDict *parent_options) 6910b50cc88SKevin Wolf { 6928e2160e2SKevin Wolf int flags = parent_flags; 6938e2160e2SKevin Wolf 6940b50cc88SKevin Wolf /* Enable protocol handling, disable format probing for bs->file */ 6950b50cc88SKevin Wolf flags |= BDRV_O_PROTOCOL; 6960b50cc88SKevin Wolf 69791a097e7SKevin Wolf /* If the cache mode isn't explicitly set, inherit direct and no-flush from 69891a097e7SKevin Wolf * the parent. */ 69991a097e7SKevin Wolf qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_DIRECT); 70091a097e7SKevin Wolf qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_NO_FLUSH); 70191a097e7SKevin Wolf 7020b50cc88SKevin Wolf /* Our block drivers take care to send flushes and respect unmap policy, 70391a097e7SKevin Wolf * so we can default to enable both on lower layers regardless of the 70491a097e7SKevin Wolf * corresponding parent options. */ 70591a097e7SKevin Wolf flags |= BDRV_O_UNMAP; 7060b50cc88SKevin Wolf 7070b50cc88SKevin Wolf /* Clear flags that only apply to the top layer */ 708abb06c5aSDaniel P. Berrange flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ | 709abb06c5aSDaniel P. Berrange BDRV_O_NO_IO); 7100b50cc88SKevin Wolf 7118e2160e2SKevin Wolf *child_flags = flags; 7120b50cc88SKevin Wolf } 7130b50cc88SKevin Wolf 714f3930ed0SKevin Wolf const BdrvChildRole child_file = { 7158e2160e2SKevin Wolf .inherit_options = bdrv_inherited_options, 716f3930ed0SKevin Wolf }; 717f3930ed0SKevin Wolf 718f3930ed0SKevin Wolf /* 7198e2160e2SKevin Wolf * Returns the options and flags that bs->file should get if the use of formats 7208e2160e2SKevin Wolf * (and not only protocols) is permitted for it, based on the given options and 7218e2160e2SKevin Wolf * flags for the parent BDS 722f3930ed0SKevin Wolf */ 7238e2160e2SKevin Wolf static void bdrv_inherited_fmt_options(int *child_flags, QDict *child_options, 7248e2160e2SKevin Wolf int parent_flags, QDict *parent_options) 725f3930ed0SKevin Wolf { 7268e2160e2SKevin Wolf child_file.inherit_options(child_flags, child_options, 7278e2160e2SKevin Wolf parent_flags, parent_options); 7288e2160e2SKevin Wolf 729abb06c5aSDaniel P. Berrange *child_flags &= ~(BDRV_O_PROTOCOL | BDRV_O_NO_IO); 730f3930ed0SKevin Wolf } 731f3930ed0SKevin Wolf 732f3930ed0SKevin Wolf const BdrvChildRole child_format = { 7338e2160e2SKevin Wolf .inherit_options = bdrv_inherited_fmt_options, 734f3930ed0SKevin Wolf }; 735f3930ed0SKevin Wolf 736317fc44eSKevin Wolf /* 7378e2160e2SKevin Wolf * Returns the options and flags that bs->backing should get, based on the 7388e2160e2SKevin Wolf * given options and flags for the parent BDS 739317fc44eSKevin Wolf */ 7408e2160e2SKevin Wolf static void bdrv_backing_options(int *child_flags, QDict *child_options, 7418e2160e2SKevin Wolf int parent_flags, QDict *parent_options) 742317fc44eSKevin Wolf { 7438e2160e2SKevin Wolf int flags = parent_flags; 7448e2160e2SKevin Wolf 745b8816a43SKevin Wolf /* The cache mode is inherited unmodified for backing files; except WCE, 746b8816a43SKevin Wolf * which is only applied on the top level (BlockBackend) */ 74791a097e7SKevin Wolf qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_DIRECT); 74891a097e7SKevin Wolf qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_NO_FLUSH); 74991a097e7SKevin Wolf 750317fc44eSKevin Wolf /* backing files always opened read-only */ 751317fc44eSKevin Wolf flags &= ~(BDRV_O_RDWR | BDRV_O_COPY_ON_READ); 752317fc44eSKevin Wolf 753317fc44eSKevin Wolf /* snapshot=on is handled on the top layer */ 7548bfea15dSKevin Wolf flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_TEMPORARY); 755317fc44eSKevin Wolf 7568e2160e2SKevin Wolf *child_flags = flags; 757317fc44eSKevin Wolf } 758317fc44eSKevin Wolf 759f3930ed0SKevin Wolf static const BdrvChildRole child_backing = { 7608e2160e2SKevin Wolf .inherit_options = bdrv_backing_options, 761f3930ed0SKevin Wolf }; 762f3930ed0SKevin Wolf 7637b272452SKevin Wolf static int bdrv_open_flags(BlockDriverState *bs, int flags) 7647b272452SKevin Wolf { 76561de4c68SKevin Wolf int open_flags = flags; 7667b272452SKevin Wolf 7677b272452SKevin Wolf /* 7687b272452SKevin Wolf * Clear flags that are internal to the block layer before opening the 7697b272452SKevin Wolf * image. 7707b272452SKevin Wolf */ 77120cca275SKevin Wolf open_flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_PROTOCOL); 7727b272452SKevin Wolf 7737b272452SKevin Wolf /* 7747b272452SKevin Wolf * Snapshots should be writable. 7757b272452SKevin Wolf */ 7768bfea15dSKevin Wolf if (flags & BDRV_O_TEMPORARY) { 7777b272452SKevin Wolf open_flags |= BDRV_O_RDWR; 7787b272452SKevin Wolf } 7797b272452SKevin Wolf 7807b272452SKevin Wolf return open_flags; 7817b272452SKevin Wolf } 7827b272452SKevin Wolf 78391a097e7SKevin Wolf static void update_flags_from_options(int *flags, QemuOpts *opts) 78491a097e7SKevin Wolf { 78591a097e7SKevin Wolf *flags &= ~BDRV_O_CACHE_MASK; 78691a097e7SKevin Wolf 78791a097e7SKevin Wolf assert(qemu_opt_find(opts, BDRV_OPT_CACHE_NO_FLUSH)); 78891a097e7SKevin Wolf if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, false)) { 78991a097e7SKevin Wolf *flags |= BDRV_O_NO_FLUSH; 79091a097e7SKevin Wolf } 79191a097e7SKevin Wolf 79291a097e7SKevin Wolf assert(qemu_opt_find(opts, BDRV_OPT_CACHE_DIRECT)); 79391a097e7SKevin Wolf if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_DIRECT, false)) { 79491a097e7SKevin Wolf *flags |= BDRV_O_NOCACHE; 79591a097e7SKevin Wolf } 79691a097e7SKevin Wolf } 79791a097e7SKevin Wolf 79891a097e7SKevin Wolf static void update_options_from_flags(QDict *options, int flags) 79991a097e7SKevin Wolf { 80091a097e7SKevin Wolf if (!qdict_haskey(options, BDRV_OPT_CACHE_DIRECT)) { 80191a097e7SKevin Wolf qdict_put(options, BDRV_OPT_CACHE_DIRECT, 80291a097e7SKevin Wolf qbool_from_bool(flags & BDRV_O_NOCACHE)); 80391a097e7SKevin Wolf } 80491a097e7SKevin Wolf if (!qdict_haskey(options, BDRV_OPT_CACHE_NO_FLUSH)) { 80591a097e7SKevin Wolf qdict_put(options, BDRV_OPT_CACHE_NO_FLUSH, 80691a097e7SKevin Wolf qbool_from_bool(flags & BDRV_O_NO_FLUSH)); 80791a097e7SKevin Wolf } 80891a097e7SKevin Wolf } 80991a097e7SKevin Wolf 810636ea370SKevin Wolf static void bdrv_assign_node_name(BlockDriverState *bs, 8116913c0c2SBenoît Canet const char *node_name, 8126913c0c2SBenoît Canet Error **errp) 8136913c0c2SBenoît Canet { 81415489c76SJeff Cody char *gen_node_name = NULL; 8156913c0c2SBenoît Canet 81615489c76SJeff Cody if (!node_name) { 81715489c76SJeff Cody node_name = gen_node_name = id_generate(ID_BLOCK); 81815489c76SJeff Cody } else if (!id_wellformed(node_name)) { 81915489c76SJeff Cody /* 82015489c76SJeff Cody * Check for empty string or invalid characters, but not if it is 82115489c76SJeff Cody * generated (generated names use characters not available to the user) 82215489c76SJeff Cody */ 8239aebf3b8SKevin Wolf error_setg(errp, "Invalid node name"); 824636ea370SKevin Wolf return; 8256913c0c2SBenoît Canet } 8266913c0c2SBenoît Canet 8270c5e94eeSBenoît Canet /* takes care of avoiding namespaces collisions */ 8287f06d47eSMarkus Armbruster if (blk_by_name(node_name)) { 8290c5e94eeSBenoît Canet error_setg(errp, "node-name=%s is conflicting with a device id", 8300c5e94eeSBenoît Canet node_name); 83115489c76SJeff Cody goto out; 8320c5e94eeSBenoît Canet } 8330c5e94eeSBenoît Canet 8346913c0c2SBenoît Canet /* takes care of avoiding duplicates node names */ 8356913c0c2SBenoît Canet if (bdrv_find_node(node_name)) { 8366913c0c2SBenoît Canet error_setg(errp, "Duplicate node name"); 83715489c76SJeff Cody goto out; 8386913c0c2SBenoît Canet } 8396913c0c2SBenoît Canet 8406913c0c2SBenoît Canet /* copy node name into the bs and insert it into the graph list */ 8416913c0c2SBenoît Canet pstrcpy(bs->node_name, sizeof(bs->node_name), node_name); 8426913c0c2SBenoît Canet QTAILQ_INSERT_TAIL(&graph_bdrv_states, bs, node_list); 84315489c76SJeff Cody out: 84415489c76SJeff Cody g_free(gen_node_name); 8456913c0c2SBenoît Canet } 8466913c0c2SBenoît Canet 84718edf289SKevin Wolf static QemuOptsList bdrv_runtime_opts = { 84818edf289SKevin Wolf .name = "bdrv_common", 84918edf289SKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(bdrv_runtime_opts.head), 85018edf289SKevin Wolf .desc = { 85118edf289SKevin Wolf { 85218edf289SKevin Wolf .name = "node-name", 85318edf289SKevin Wolf .type = QEMU_OPT_STRING, 85418edf289SKevin Wolf .help = "Node name of the block device node", 85518edf289SKevin Wolf }, 85662392ebbSKevin Wolf { 85762392ebbSKevin Wolf .name = "driver", 85862392ebbSKevin Wolf .type = QEMU_OPT_STRING, 85962392ebbSKevin Wolf .help = "Block driver to use for the node", 86062392ebbSKevin Wolf }, 86191a097e7SKevin Wolf { 86291a097e7SKevin Wolf .name = BDRV_OPT_CACHE_DIRECT, 86391a097e7SKevin Wolf .type = QEMU_OPT_BOOL, 86491a097e7SKevin Wolf .help = "Bypass software writeback cache on the host", 86591a097e7SKevin Wolf }, 86691a097e7SKevin Wolf { 86791a097e7SKevin Wolf .name = BDRV_OPT_CACHE_NO_FLUSH, 86891a097e7SKevin Wolf .type = QEMU_OPT_BOOL, 86991a097e7SKevin Wolf .help = "Ignore flush requests", 87091a097e7SKevin Wolf }, 87118edf289SKevin Wolf { /* end of list */ } 87218edf289SKevin Wolf }, 87318edf289SKevin Wolf }; 87418edf289SKevin Wolf 875b6ce07aaSKevin Wolf /* 87657915332SKevin Wolf * Common part for opening disk images and files 877b6ad491aSKevin Wolf * 878b6ad491aSKevin Wolf * Removes all processed options from *options. 87957915332SKevin Wolf */ 8809a4f4c31SKevin Wolf static int bdrv_open_common(BlockDriverState *bs, BdrvChild *file, 88182dc8b41SKevin Wolf QDict *options, Error **errp) 88257915332SKevin Wolf { 88357915332SKevin Wolf int ret, open_flags; 884035fccdfSKevin Wolf const char *filename; 88562392ebbSKevin Wolf const char *driver_name = NULL; 8866913c0c2SBenoît Canet const char *node_name = NULL; 88718edf289SKevin Wolf QemuOpts *opts; 88862392ebbSKevin Wolf BlockDriver *drv; 88934b5d2c6SMax Reitz Error *local_err = NULL; 89057915332SKevin Wolf 8916405875cSPaolo Bonzini assert(bs->file == NULL); 892707ff828SKevin Wolf assert(options != NULL && bs->options != options); 89357915332SKevin Wolf 89462392ebbSKevin Wolf opts = qemu_opts_create(&bdrv_runtime_opts, NULL, 0, &error_abort); 89562392ebbSKevin Wolf qemu_opts_absorb_qdict(opts, options, &local_err); 89662392ebbSKevin Wolf if (local_err) { 89762392ebbSKevin Wolf error_propagate(errp, local_err); 89862392ebbSKevin Wolf ret = -EINVAL; 89962392ebbSKevin Wolf goto fail_opts; 90062392ebbSKevin Wolf } 90162392ebbSKevin Wolf 90262392ebbSKevin Wolf driver_name = qemu_opt_get(opts, "driver"); 90362392ebbSKevin Wolf drv = bdrv_find_format(driver_name); 90462392ebbSKevin Wolf assert(drv != NULL); 90562392ebbSKevin Wolf 90645673671SKevin Wolf if (file != NULL) { 9079a4f4c31SKevin Wolf filename = file->bs->filename; 90845673671SKevin Wolf } else { 90945673671SKevin Wolf filename = qdict_get_try_str(options, "filename"); 91045673671SKevin Wolf } 91145673671SKevin Wolf 912765003dbSKevin Wolf if (drv->bdrv_needs_filename && !filename) { 913765003dbSKevin Wolf error_setg(errp, "The '%s' block driver requires a file name", 914765003dbSKevin Wolf drv->format_name); 91518edf289SKevin Wolf ret = -EINVAL; 91618edf289SKevin Wolf goto fail_opts; 91718edf289SKevin Wolf } 91818edf289SKevin Wolf 91982dc8b41SKevin Wolf trace_bdrv_open_common(bs, filename ?: "", bs->open_flags, 92082dc8b41SKevin Wolf drv->format_name); 92162392ebbSKevin Wolf 92218edf289SKevin Wolf node_name = qemu_opt_get(opts, "node-name"); 923636ea370SKevin Wolf bdrv_assign_node_name(bs, node_name, &local_err); 9240fb6395cSMarkus Armbruster if (local_err) { 925636ea370SKevin Wolf error_propagate(errp, local_err); 92618edf289SKevin Wolf ret = -EINVAL; 92718edf289SKevin Wolf goto fail_opts; 9285d186eb0SKevin Wolf } 9295d186eb0SKevin Wolf 930c25f53b0SPaolo Bonzini bs->request_alignment = 512; 9310d51b4deSAsias He bs->zero_beyond_eof = true; 93282dc8b41SKevin Wolf bs->read_only = !(bs->open_flags & BDRV_O_RDWR); 933b64ec4e4SFam Zheng 934b64ec4e4SFam Zheng if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, bs->read_only)) { 9358f94a6e4SKevin Wolf error_setg(errp, 9368f94a6e4SKevin Wolf !bs->read_only && bdrv_is_whitelisted(drv, true) 9378f94a6e4SKevin Wolf ? "Driver '%s' can only be used for read-only devices" 9388f94a6e4SKevin Wolf : "Driver '%s' is not whitelisted", 9398f94a6e4SKevin Wolf drv->format_name); 94018edf289SKevin Wolf ret = -ENOTSUP; 94118edf289SKevin Wolf goto fail_opts; 942b64ec4e4SFam Zheng } 94357915332SKevin Wolf 94453fec9d3SStefan Hajnoczi assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */ 94582dc8b41SKevin Wolf if (bs->open_flags & BDRV_O_COPY_ON_READ) { 9460ebd24e0SKevin Wolf if (!bs->read_only) { 94753fec9d3SStefan Hajnoczi bdrv_enable_copy_on_read(bs); 9480ebd24e0SKevin Wolf } else { 9490ebd24e0SKevin Wolf error_setg(errp, "Can't use copy-on-read on read-only device"); 95018edf289SKevin Wolf ret = -EINVAL; 95118edf289SKevin Wolf goto fail_opts; 9520ebd24e0SKevin Wolf } 95353fec9d3SStefan Hajnoczi } 95453fec9d3SStefan Hajnoczi 955c2ad1b0cSKevin Wolf if (filename != NULL) { 95657915332SKevin Wolf pstrcpy(bs->filename, sizeof(bs->filename), filename); 957c2ad1b0cSKevin Wolf } else { 958c2ad1b0cSKevin Wolf bs->filename[0] = '\0'; 959c2ad1b0cSKevin Wolf } 96091af7014SMax Reitz pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), bs->filename); 96157915332SKevin Wolf 96257915332SKevin Wolf bs->drv = drv; 9637267c094SAnthony Liguori bs->opaque = g_malloc0(drv->instance_size); 96457915332SKevin Wolf 96591a097e7SKevin Wolf /* Apply cache mode options */ 96691a097e7SKevin Wolf update_flags_from_options(&bs->open_flags, opts); 96773ac451fSKevin Wolf 96866f82ceeSKevin Wolf /* Open the image, either directly or using a protocol */ 96982dc8b41SKevin Wolf open_flags = bdrv_open_flags(bs, bs->open_flags); 97066f82ceeSKevin Wolf if (drv->bdrv_file_open) { 9715d186eb0SKevin Wolf assert(file == NULL); 972030be321SBenoît Canet assert(!drv->bdrv_needs_filename || filename != NULL); 97334b5d2c6SMax Reitz ret = drv->bdrv_file_open(bs, options, open_flags, &local_err); 974f500a6d3SKevin Wolf } else { 9752af5ef70SKevin Wolf if (file == NULL) { 97634b5d2c6SMax Reitz error_setg(errp, "Can't use '%s' as a block driver for the " 97734b5d2c6SMax Reitz "protocol level", drv->format_name); 9782af5ef70SKevin Wolf ret = -EINVAL; 9792af5ef70SKevin Wolf goto free_and_fail; 9802af5ef70SKevin Wolf } 981f500a6d3SKevin Wolf bs->file = file; 98234b5d2c6SMax Reitz ret = drv->bdrv_open(bs, options, open_flags, &local_err); 98366f82ceeSKevin Wolf } 98466f82ceeSKevin Wolf 98557915332SKevin Wolf if (ret < 0) { 98684d18f06SMarkus Armbruster if (local_err) { 98734b5d2c6SMax Reitz error_propagate(errp, local_err); 9882fa9aa59SDunrong Huang } else if (bs->filename[0]) { 9892fa9aa59SDunrong Huang error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename); 99034b5d2c6SMax Reitz } else { 99134b5d2c6SMax Reitz error_setg_errno(errp, -ret, "Could not open image"); 99234b5d2c6SMax Reitz } 99357915332SKevin Wolf goto free_and_fail; 99457915332SKevin Wolf } 99557915332SKevin Wolf 99651762288SStefan Hajnoczi ret = refresh_total_sectors(bs, bs->total_sectors); 99751762288SStefan Hajnoczi if (ret < 0) { 99834b5d2c6SMax Reitz error_setg_errno(errp, -ret, "Could not refresh total sector count"); 99951762288SStefan Hajnoczi goto free_and_fail; 100057915332SKevin Wolf } 100151762288SStefan Hajnoczi 10023baca891SKevin Wolf bdrv_refresh_limits(bs, &local_err); 10033baca891SKevin Wolf if (local_err) { 10043baca891SKevin Wolf error_propagate(errp, local_err); 10053baca891SKevin Wolf ret = -EINVAL; 10063baca891SKevin Wolf goto free_and_fail; 10073baca891SKevin Wolf } 10083baca891SKevin Wolf 1009c25f53b0SPaolo Bonzini assert(bdrv_opt_mem_align(bs) != 0); 10104196d2f0SDenis V. Lunev assert(bdrv_min_mem_align(bs) != 0); 1011b192af8aSDimitris Aragiorgis assert((bs->request_alignment != 0) || bdrv_is_sg(bs)); 101218edf289SKevin Wolf 101318edf289SKevin Wolf qemu_opts_del(opts); 101457915332SKevin Wolf return 0; 101557915332SKevin Wolf 101657915332SKevin Wolf free_and_fail: 101766f82ceeSKevin Wolf bs->file = NULL; 10187267c094SAnthony Liguori g_free(bs->opaque); 101957915332SKevin Wolf bs->opaque = NULL; 102057915332SKevin Wolf bs->drv = NULL; 102118edf289SKevin Wolf fail_opts: 102218edf289SKevin Wolf qemu_opts_del(opts); 102357915332SKevin Wolf return ret; 102457915332SKevin Wolf } 102557915332SKevin Wolf 10265e5c4f63SKevin Wolf static QDict *parse_json_filename(const char *filename, Error **errp) 10275e5c4f63SKevin Wolf { 10285e5c4f63SKevin Wolf QObject *options_obj; 10295e5c4f63SKevin Wolf QDict *options; 10305e5c4f63SKevin Wolf int ret; 10315e5c4f63SKevin Wolf 10325e5c4f63SKevin Wolf ret = strstart(filename, "json:", &filename); 10335e5c4f63SKevin Wolf assert(ret); 10345e5c4f63SKevin Wolf 10355e5c4f63SKevin Wolf options_obj = qobject_from_json(filename); 10365e5c4f63SKevin Wolf if (!options_obj) { 10375e5c4f63SKevin Wolf error_setg(errp, "Could not parse the JSON options"); 10385e5c4f63SKevin Wolf return NULL; 10395e5c4f63SKevin Wolf } 10405e5c4f63SKevin Wolf 10415e5c4f63SKevin Wolf if (qobject_type(options_obj) != QTYPE_QDICT) { 10425e5c4f63SKevin Wolf qobject_decref(options_obj); 10435e5c4f63SKevin Wolf error_setg(errp, "Invalid JSON object given"); 10445e5c4f63SKevin Wolf return NULL; 10455e5c4f63SKevin Wolf } 10465e5c4f63SKevin Wolf 10475e5c4f63SKevin Wolf options = qobject_to_qdict(options_obj); 10485e5c4f63SKevin Wolf qdict_flatten(options); 10495e5c4f63SKevin Wolf 10505e5c4f63SKevin Wolf return options; 10515e5c4f63SKevin Wolf } 10525e5c4f63SKevin Wolf 1053de3b53f0SKevin Wolf static void parse_json_protocol(QDict *options, const char **pfilename, 1054de3b53f0SKevin Wolf Error **errp) 1055de3b53f0SKevin Wolf { 1056de3b53f0SKevin Wolf QDict *json_options; 1057de3b53f0SKevin Wolf Error *local_err = NULL; 1058de3b53f0SKevin Wolf 1059de3b53f0SKevin Wolf /* Parse json: pseudo-protocol */ 1060de3b53f0SKevin Wolf if (!*pfilename || !g_str_has_prefix(*pfilename, "json:")) { 1061de3b53f0SKevin Wolf return; 1062de3b53f0SKevin Wolf } 1063de3b53f0SKevin Wolf 1064de3b53f0SKevin Wolf json_options = parse_json_filename(*pfilename, &local_err); 1065de3b53f0SKevin Wolf if (local_err) { 1066de3b53f0SKevin Wolf error_propagate(errp, local_err); 1067de3b53f0SKevin Wolf return; 1068de3b53f0SKevin Wolf } 1069de3b53f0SKevin Wolf 1070de3b53f0SKevin Wolf /* Options given in the filename have lower priority than options 1071de3b53f0SKevin Wolf * specified directly */ 1072de3b53f0SKevin Wolf qdict_join(options, json_options, false); 1073de3b53f0SKevin Wolf QDECREF(json_options); 1074de3b53f0SKevin Wolf *pfilename = NULL; 1075de3b53f0SKevin Wolf } 1076de3b53f0SKevin Wolf 107757915332SKevin Wolf /* 1078f54120ffSKevin Wolf * Fills in default options for opening images and converts the legacy 1079f54120ffSKevin Wolf * filename/flags pair to option QDict entries. 108053a29513SMax Reitz * The BDRV_O_PROTOCOL flag in *flags will be set or cleared accordingly if a 108153a29513SMax Reitz * block driver has been specified explicitly. 1082f54120ffSKevin Wolf */ 1083de3b53f0SKevin Wolf static int bdrv_fill_options(QDict **options, const char *filename, 1084053e1578SMax Reitz int *flags, Error **errp) 1085f54120ffSKevin Wolf { 1086f54120ffSKevin Wolf const char *drvname; 108753a29513SMax Reitz bool protocol = *flags & BDRV_O_PROTOCOL; 1088f54120ffSKevin Wolf bool parse_filename = false; 1089053e1578SMax Reitz BlockDriver *drv = NULL; 1090f54120ffSKevin Wolf Error *local_err = NULL; 1091f54120ffSKevin Wolf 109253a29513SMax Reitz drvname = qdict_get_try_str(*options, "driver"); 1093053e1578SMax Reitz if (drvname) { 1094053e1578SMax Reitz drv = bdrv_find_format(drvname); 1095053e1578SMax Reitz if (!drv) { 1096053e1578SMax Reitz error_setg(errp, "Unknown driver '%s'", drvname); 1097053e1578SMax Reitz return -ENOENT; 1098053e1578SMax Reitz } 109953a29513SMax Reitz /* If the user has explicitly specified the driver, this choice should 110053a29513SMax Reitz * override the BDRV_O_PROTOCOL flag */ 1101053e1578SMax Reitz protocol = drv->bdrv_file_open; 110253a29513SMax Reitz } 110353a29513SMax Reitz 110453a29513SMax Reitz if (protocol) { 110553a29513SMax Reitz *flags |= BDRV_O_PROTOCOL; 110653a29513SMax Reitz } else { 110753a29513SMax Reitz *flags &= ~BDRV_O_PROTOCOL; 110853a29513SMax Reitz } 110953a29513SMax Reitz 111091a097e7SKevin Wolf /* Translate cache options from flags into options */ 111191a097e7SKevin Wolf update_options_from_flags(*options, *flags); 111291a097e7SKevin Wolf 1113f54120ffSKevin Wolf /* Fetch the file name from the options QDict if necessary */ 111417b005f1SKevin Wolf if (protocol && filename) { 1115f54120ffSKevin Wolf if (!qdict_haskey(*options, "filename")) { 1116f54120ffSKevin Wolf qdict_put(*options, "filename", qstring_from_str(filename)); 1117f54120ffSKevin Wolf parse_filename = true; 1118f54120ffSKevin Wolf } else { 1119f54120ffSKevin Wolf error_setg(errp, "Can't specify 'file' and 'filename' options at " 1120f54120ffSKevin Wolf "the same time"); 1121f54120ffSKevin Wolf return -EINVAL; 1122f54120ffSKevin Wolf } 1123f54120ffSKevin Wolf } 1124f54120ffSKevin Wolf 1125f54120ffSKevin Wolf /* Find the right block driver */ 1126f54120ffSKevin Wolf filename = qdict_get_try_str(*options, "filename"); 1127f54120ffSKevin Wolf 112817b005f1SKevin Wolf if (!drvname && protocol) { 1129f54120ffSKevin Wolf if (filename) { 1130b65a5e12SMax Reitz drv = bdrv_find_protocol(filename, parse_filename, errp); 1131f54120ffSKevin Wolf if (!drv) { 1132f54120ffSKevin Wolf return -EINVAL; 1133f54120ffSKevin Wolf } 1134f54120ffSKevin Wolf 1135f54120ffSKevin Wolf drvname = drv->format_name; 1136f54120ffSKevin Wolf qdict_put(*options, "driver", qstring_from_str(drvname)); 1137f54120ffSKevin Wolf } else { 1138f54120ffSKevin Wolf error_setg(errp, "Must specify either driver or file"); 1139f54120ffSKevin Wolf return -EINVAL; 1140f54120ffSKevin Wolf } 114117b005f1SKevin Wolf } 114217b005f1SKevin Wolf 114317b005f1SKevin Wolf assert(drv || !protocol); 1144f54120ffSKevin Wolf 1145f54120ffSKevin Wolf /* Driver-specific filename parsing */ 114617b005f1SKevin Wolf if (drv && drv->bdrv_parse_filename && parse_filename) { 1147f54120ffSKevin Wolf drv->bdrv_parse_filename(filename, *options, &local_err); 1148f54120ffSKevin Wolf if (local_err) { 1149f54120ffSKevin Wolf error_propagate(errp, local_err); 1150f54120ffSKevin Wolf return -EINVAL; 1151f54120ffSKevin Wolf } 1152f54120ffSKevin Wolf 1153f54120ffSKevin Wolf if (!drv->bdrv_needs_filename) { 1154f54120ffSKevin Wolf qdict_del(*options, "filename"); 1155f54120ffSKevin Wolf } 1156f54120ffSKevin Wolf } 1157f54120ffSKevin Wolf 1158f54120ffSKevin Wolf return 0; 1159f54120ffSKevin Wolf } 1160f54120ffSKevin Wolf 1161f21d96d0SKevin Wolf BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs, 1162260fecf1SKevin Wolf const char *child_name, 1163df581792SKevin Wolf const BdrvChildRole *child_role) 1164df581792SKevin Wolf { 1165df581792SKevin Wolf BdrvChild *child = g_new(BdrvChild, 1); 1166df581792SKevin Wolf *child = (BdrvChild) { 1167df581792SKevin Wolf .bs = child_bs, 1168260fecf1SKevin Wolf .name = g_strdup(child_name), 1169df581792SKevin Wolf .role = child_role, 1170df581792SKevin Wolf }; 1171df581792SKevin Wolf 1172d42a8a93SKevin Wolf QLIST_INSERT_HEAD(&child_bs->parents, child, next_parent); 1173b4b059f6SKevin Wolf 1174b4b059f6SKevin Wolf return child; 1175df581792SKevin Wolf } 1176df581792SKevin Wolf 1177f21d96d0SKevin Wolf static BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs, 1178f21d96d0SKevin Wolf BlockDriverState *child_bs, 1179f21d96d0SKevin Wolf const char *child_name, 1180f21d96d0SKevin Wolf const BdrvChildRole *child_role) 1181f21d96d0SKevin Wolf { 1182f21d96d0SKevin Wolf BdrvChild *child = bdrv_root_attach_child(child_bs, child_name, child_role); 1183f21d96d0SKevin Wolf QLIST_INSERT_HEAD(&parent_bs->children, child, next); 1184f21d96d0SKevin Wolf return child; 1185f21d96d0SKevin Wolf } 1186f21d96d0SKevin Wolf 11873f09bfbcSKevin Wolf static void bdrv_detach_child(BdrvChild *child) 118833a60407SKevin Wolf { 1189f21d96d0SKevin Wolf if (child->next.le_prev) { 119033a60407SKevin Wolf QLIST_REMOVE(child, next); 1191f21d96d0SKevin Wolf child->next.le_prev = NULL; 1192f21d96d0SKevin Wolf } 1193d42a8a93SKevin Wolf QLIST_REMOVE(child, next_parent); 1194260fecf1SKevin Wolf g_free(child->name); 119533a60407SKevin Wolf g_free(child); 119633a60407SKevin Wolf } 119733a60407SKevin Wolf 1198f21d96d0SKevin Wolf void bdrv_root_unref_child(BdrvChild *child) 119933a60407SKevin Wolf { 1200779020cbSKevin Wolf BlockDriverState *child_bs; 1201779020cbSKevin Wolf 1202f21d96d0SKevin Wolf child_bs = child->bs; 1203f21d96d0SKevin Wolf bdrv_detach_child(child); 1204f21d96d0SKevin Wolf bdrv_unref(child_bs); 1205f21d96d0SKevin Wolf } 1206f21d96d0SKevin Wolf 1207f21d96d0SKevin Wolf void bdrv_unref_child(BlockDriverState *parent, BdrvChild *child) 1208f21d96d0SKevin Wolf { 1209779020cbSKevin Wolf if (child == NULL) { 1210779020cbSKevin Wolf return; 1211779020cbSKevin Wolf } 121233a60407SKevin Wolf 121333a60407SKevin Wolf if (child->bs->inherits_from == parent) { 121433a60407SKevin Wolf child->bs->inherits_from = NULL; 121533a60407SKevin Wolf } 121633a60407SKevin Wolf 1217f21d96d0SKevin Wolf bdrv_root_unref_child(child); 121833a60407SKevin Wolf } 121933a60407SKevin Wolf 12205db15a57SKevin Wolf /* 12215db15a57SKevin Wolf * Sets the backing file link of a BDS. A new reference is created; callers 12225db15a57SKevin Wolf * which don't need their own reference any more must call bdrv_unref(). 12235db15a57SKevin Wolf */ 12248d24cce1SFam Zheng void bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd) 12258d24cce1SFam Zheng { 12265db15a57SKevin Wolf if (backing_hd) { 12275db15a57SKevin Wolf bdrv_ref(backing_hd); 12285db15a57SKevin Wolf } 12298d24cce1SFam Zheng 1230760e0063SKevin Wolf if (bs->backing) { 1231826b6ca0SFam Zheng assert(bs->backing_blocker); 1232760e0063SKevin Wolf bdrv_op_unblock_all(bs->backing->bs, bs->backing_blocker); 12335db15a57SKevin Wolf bdrv_unref_child(bs, bs->backing); 1234826b6ca0SFam Zheng } else if (backing_hd) { 1235826b6ca0SFam Zheng error_setg(&bs->backing_blocker, 123681e5f78aSAlberto Garcia "node is used as backing hd of '%s'", 123781e5f78aSAlberto Garcia bdrv_get_device_or_node_name(bs)); 1238826b6ca0SFam Zheng } 1239826b6ca0SFam Zheng 12408d24cce1SFam Zheng if (!backing_hd) { 1241826b6ca0SFam Zheng error_free(bs->backing_blocker); 1242826b6ca0SFam Zheng bs->backing_blocker = NULL; 1243760e0063SKevin Wolf bs->backing = NULL; 12448d24cce1SFam Zheng goto out; 12458d24cce1SFam Zheng } 1246260fecf1SKevin Wolf bs->backing = bdrv_attach_child(bs, backing_hd, "backing", &child_backing); 12478d24cce1SFam Zheng bs->open_flags &= ~BDRV_O_NO_BACKING; 12488d24cce1SFam Zheng pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_hd->filename); 12498d24cce1SFam Zheng pstrcpy(bs->backing_format, sizeof(bs->backing_format), 12508d24cce1SFam Zheng backing_hd->drv ? backing_hd->drv->format_name : ""); 1251826b6ca0SFam Zheng 1252760e0063SKevin Wolf bdrv_op_block_all(backing_hd, bs->backing_blocker); 1253826b6ca0SFam Zheng /* Otherwise we won't be able to commit due to check in bdrv_commit */ 1254760e0063SKevin Wolf bdrv_op_unblock(backing_hd, BLOCK_OP_TYPE_COMMIT_TARGET, 1255826b6ca0SFam Zheng bs->backing_blocker); 12568d24cce1SFam Zheng out: 12573baca891SKevin Wolf bdrv_refresh_limits(bs, NULL); 12588d24cce1SFam Zheng } 12598d24cce1SFam Zheng 126031ca6d07SKevin Wolf /* 126131ca6d07SKevin Wolf * Opens the backing file for a BlockDriverState if not yet open 126231ca6d07SKevin Wolf * 1263d9b7b057SKevin Wolf * bdref_key specifies the key for the image's BlockdevRef in the options QDict. 1264d9b7b057SKevin Wolf * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict 1265d9b7b057SKevin Wolf * itself, all options starting with "${bdref_key}." are considered part of the 1266d9b7b057SKevin Wolf * BlockdevRef. 1267d9b7b057SKevin Wolf * 1268d9b7b057SKevin Wolf * TODO Can this be unified with bdrv_open_image()? 126931ca6d07SKevin Wolf */ 1270d9b7b057SKevin Wolf int bdrv_open_backing_file(BlockDriverState *bs, QDict *parent_options, 1271d9b7b057SKevin Wolf const char *bdref_key, Error **errp) 12729156df12SPaolo Bonzini { 12731ba4b6a5SBenoît Canet char *backing_filename = g_malloc0(PATH_MAX); 1274d9b7b057SKevin Wolf char *bdref_key_dot; 1275d9b7b057SKevin Wolf const char *reference = NULL; 1276317fc44eSKevin Wolf int ret = 0; 12778d24cce1SFam Zheng BlockDriverState *backing_hd; 1278d9b7b057SKevin Wolf QDict *options; 1279d9b7b057SKevin Wolf QDict *tmp_parent_options = NULL; 128034b5d2c6SMax Reitz Error *local_err = NULL; 12819156df12SPaolo Bonzini 1282760e0063SKevin Wolf if (bs->backing != NULL) { 12831ba4b6a5SBenoît Canet goto free_exit; 12849156df12SPaolo Bonzini } 12859156df12SPaolo Bonzini 128631ca6d07SKevin Wolf /* NULL means an empty set of options */ 1287d9b7b057SKevin Wolf if (parent_options == NULL) { 1288d9b7b057SKevin Wolf tmp_parent_options = qdict_new(); 1289d9b7b057SKevin Wolf parent_options = tmp_parent_options; 129031ca6d07SKevin Wolf } 129131ca6d07SKevin Wolf 12929156df12SPaolo Bonzini bs->open_flags &= ~BDRV_O_NO_BACKING; 1293d9b7b057SKevin Wolf 1294d9b7b057SKevin Wolf bdref_key_dot = g_strdup_printf("%s.", bdref_key); 1295d9b7b057SKevin Wolf qdict_extract_subqdict(parent_options, &options, bdref_key_dot); 1296d9b7b057SKevin Wolf g_free(bdref_key_dot); 1297d9b7b057SKevin Wolf 1298d9b7b057SKevin Wolf reference = qdict_get_try_str(parent_options, bdref_key); 1299d9b7b057SKevin Wolf if (reference || qdict_haskey(options, "file.filename")) { 13001cb6f506SKevin Wolf backing_filename[0] = '\0'; 13011cb6f506SKevin Wolf } else if (bs->backing_file[0] == '\0' && qdict_size(options) == 0) { 130231ca6d07SKevin Wolf QDECREF(options); 13031ba4b6a5SBenoît Canet goto free_exit; 1304dbecebddSFam Zheng } else { 13059f07429eSMax Reitz bdrv_get_full_backing_filename(bs, backing_filename, PATH_MAX, 13069f07429eSMax Reitz &local_err); 13079f07429eSMax Reitz if (local_err) { 13089f07429eSMax Reitz ret = -EINVAL; 13099f07429eSMax Reitz error_propagate(errp, local_err); 13109f07429eSMax Reitz QDECREF(options); 13119f07429eSMax Reitz goto free_exit; 13129f07429eSMax Reitz } 13139156df12SPaolo Bonzini } 13149156df12SPaolo Bonzini 13158ee79e70SKevin Wolf if (!bs->drv || !bs->drv->supports_backing) { 13168ee79e70SKevin Wolf ret = -EINVAL; 13178ee79e70SKevin Wolf error_setg(errp, "Driver doesn't support backing files"); 13188ee79e70SKevin Wolf QDECREF(options); 13198ee79e70SKevin Wolf goto free_exit; 13208ee79e70SKevin Wolf } 13218ee79e70SKevin Wolf 1322c5f6e493SKevin Wolf if (bs->backing_format[0] != '\0' && !qdict_haskey(options, "driver")) { 1323c5f6e493SKevin Wolf qdict_put(options, "driver", qstring_from_str(bs->backing_format)); 13249156df12SPaolo Bonzini } 13259156df12SPaolo Bonzini 1326d9b7b057SKevin Wolf backing_hd = NULL; 1327f3930ed0SKevin Wolf ret = bdrv_open_inherit(&backing_hd, 1328f3930ed0SKevin Wolf *backing_filename ? backing_filename : NULL, 1329d9b7b057SKevin Wolf reference, options, 0, bs, &child_backing, 1330e43bfd9cSMarkus Armbruster errp); 13319156df12SPaolo Bonzini if (ret < 0) { 13329156df12SPaolo Bonzini bs->open_flags |= BDRV_O_NO_BACKING; 1333e43bfd9cSMarkus Armbruster error_prepend(errp, "Could not open backing file: "); 13341ba4b6a5SBenoît Canet goto free_exit; 13359156df12SPaolo Bonzini } 1336df581792SKevin Wolf 13375db15a57SKevin Wolf /* Hook up the backing file link; drop our reference, bs owns the 13385db15a57SKevin Wolf * backing_hd reference now */ 13398d24cce1SFam Zheng bdrv_set_backing_hd(bs, backing_hd); 13405db15a57SKevin Wolf bdrv_unref(backing_hd); 1341d80ac658SPeter Feiner 1342d9b7b057SKevin Wolf qdict_del(parent_options, bdref_key); 1343d9b7b057SKevin Wolf 13441ba4b6a5SBenoît Canet free_exit: 13451ba4b6a5SBenoît Canet g_free(backing_filename); 1346d9b7b057SKevin Wolf QDECREF(tmp_parent_options); 13471ba4b6a5SBenoît Canet return ret; 13489156df12SPaolo Bonzini } 13499156df12SPaolo Bonzini 1350b6ce07aaSKevin Wolf /* 1351da557aacSMax Reitz * Opens a disk image whose options are given as BlockdevRef in another block 1352da557aacSMax Reitz * device's options. 1353da557aacSMax Reitz * 1354da557aacSMax Reitz * If allow_none is true, no image will be opened if filename is false and no 1355b4b059f6SKevin Wolf * BlockdevRef is given. NULL will be returned, but errp remains unset. 1356da557aacSMax Reitz * 1357da557aacSMax Reitz * bdrev_key specifies the key for the image's BlockdevRef in the options QDict. 1358da557aacSMax Reitz * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict 1359da557aacSMax Reitz * itself, all options starting with "${bdref_key}." are considered part of the 1360da557aacSMax Reitz * BlockdevRef. 1361da557aacSMax Reitz * 1362da557aacSMax Reitz * The BlockdevRef will be removed from the options QDict. 1363da557aacSMax Reitz */ 1364b4b059f6SKevin Wolf BdrvChild *bdrv_open_child(const char *filename, 1365f3930ed0SKevin Wolf QDict *options, const char *bdref_key, 1366b4b059f6SKevin Wolf BlockDriverState* parent, 1367b4b059f6SKevin Wolf const BdrvChildRole *child_role, 1368f7d9fd8cSMax Reitz bool allow_none, Error **errp) 1369da557aacSMax Reitz { 1370b4b059f6SKevin Wolf BdrvChild *c = NULL; 1371b4b059f6SKevin Wolf BlockDriverState *bs; 1372da557aacSMax Reitz QDict *image_options; 1373da557aacSMax Reitz int ret; 1374da557aacSMax Reitz char *bdref_key_dot; 1375da557aacSMax Reitz const char *reference; 1376da557aacSMax Reitz 1377df581792SKevin Wolf assert(child_role != NULL); 1378f67503e5SMax Reitz 1379da557aacSMax Reitz bdref_key_dot = g_strdup_printf("%s.", bdref_key); 1380da557aacSMax Reitz qdict_extract_subqdict(options, &image_options, bdref_key_dot); 1381da557aacSMax Reitz g_free(bdref_key_dot); 1382da557aacSMax Reitz 1383da557aacSMax Reitz reference = qdict_get_try_str(options, bdref_key); 1384da557aacSMax Reitz if (!filename && !reference && !qdict_size(image_options)) { 1385b4b059f6SKevin Wolf if (!allow_none) { 1386da557aacSMax Reitz error_setg(errp, "A block device must be specified for \"%s\"", 1387da557aacSMax Reitz bdref_key); 1388da557aacSMax Reitz } 1389b20e61e0SMarkus Armbruster QDECREF(image_options); 1390da557aacSMax Reitz goto done; 1391da557aacSMax Reitz } 1392da557aacSMax Reitz 1393b4b059f6SKevin Wolf bs = NULL; 1394b4b059f6SKevin Wolf ret = bdrv_open_inherit(&bs, filename, reference, image_options, 0, 1395ce343771SMax Reitz parent, child_role, errp); 1396df581792SKevin Wolf if (ret < 0) { 1397df581792SKevin Wolf goto done; 1398df581792SKevin Wolf } 1399df581792SKevin Wolf 1400260fecf1SKevin Wolf c = bdrv_attach_child(parent, bs, bdref_key, child_role); 1401da557aacSMax Reitz 1402da557aacSMax Reitz done: 1403da557aacSMax Reitz qdict_del(options, bdref_key); 1404b4b059f6SKevin Wolf return c; 1405b4b059f6SKevin Wolf } 1406b4b059f6SKevin Wolf 140773176beeSKevin Wolf static int bdrv_append_temp_snapshot(BlockDriverState *bs, int flags, 140873176beeSKevin Wolf QDict *snapshot_options, Error **errp) 1409b998875dSKevin Wolf { 1410b998875dSKevin Wolf /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */ 14111ba4b6a5SBenoît Canet char *tmp_filename = g_malloc0(PATH_MAX + 1); 1412b998875dSKevin Wolf int64_t total_size; 141383d0521aSChunyan Liu QemuOpts *opts = NULL; 1414b998875dSKevin Wolf BlockDriverState *bs_snapshot; 1415c2e0dbbfSFam Zheng Error *local_err = NULL; 1416b998875dSKevin Wolf int ret; 1417b998875dSKevin Wolf 1418b998875dSKevin Wolf /* if snapshot, we create a temporary backing file and open it 1419b998875dSKevin Wolf instead of opening 'filename' directly */ 1420b998875dSKevin Wolf 1421b998875dSKevin Wolf /* Get the required size from the image */ 1422f187743aSKevin Wolf total_size = bdrv_getlength(bs); 1423f187743aSKevin Wolf if (total_size < 0) { 14246b8aeca5SChen Gang ret = total_size; 1425f187743aSKevin Wolf error_setg_errno(errp, -total_size, "Could not get image size"); 14261ba4b6a5SBenoît Canet goto out; 1427f187743aSKevin Wolf } 1428b998875dSKevin Wolf 1429b998875dSKevin Wolf /* Create the temporary image */ 14301ba4b6a5SBenoît Canet ret = get_tmp_filename(tmp_filename, PATH_MAX + 1); 1431b998875dSKevin Wolf if (ret < 0) { 1432b998875dSKevin Wolf error_setg_errno(errp, -ret, "Could not get temporary filename"); 14331ba4b6a5SBenoît Canet goto out; 1434b998875dSKevin Wolf } 1435b998875dSKevin Wolf 1436ef810437SMax Reitz opts = qemu_opts_create(bdrv_qcow2.create_opts, NULL, 0, 1437c282e1fdSChunyan Liu &error_abort); 143839101f25SMarkus Armbruster qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_size, &error_abort); 1439e43bfd9cSMarkus Armbruster ret = bdrv_create(&bdrv_qcow2, tmp_filename, opts, errp); 144083d0521aSChunyan Liu qemu_opts_del(opts); 1441b998875dSKevin Wolf if (ret < 0) { 1442e43bfd9cSMarkus Armbruster error_prepend(errp, "Could not create temporary overlay '%s': ", 1443e43bfd9cSMarkus Armbruster tmp_filename); 14441ba4b6a5SBenoît Canet goto out; 1445b998875dSKevin Wolf } 1446b998875dSKevin Wolf 144773176beeSKevin Wolf /* Prepare options QDict for the temporary file */ 1448b998875dSKevin Wolf qdict_put(snapshot_options, "file.driver", 1449b998875dSKevin Wolf qstring_from_str("file")); 1450b998875dSKevin Wolf qdict_put(snapshot_options, "file.filename", 1451b998875dSKevin Wolf qstring_from_str(tmp_filename)); 1452e6641719SMax Reitz qdict_put(snapshot_options, "driver", 1453e6641719SMax Reitz qstring_from_str("qcow2")); 1454b998875dSKevin Wolf 1455e4e9986bSMarkus Armbruster bs_snapshot = bdrv_new(); 1456b998875dSKevin Wolf 1457b998875dSKevin Wolf ret = bdrv_open(&bs_snapshot, NULL, NULL, snapshot_options, 14586ebf9aa2SMax Reitz flags, &local_err); 145973176beeSKevin Wolf snapshot_options = NULL; 1460b998875dSKevin Wolf if (ret < 0) { 1461b998875dSKevin Wolf error_propagate(errp, local_err); 14621ba4b6a5SBenoît Canet goto out; 1463b998875dSKevin Wolf } 1464b998875dSKevin Wolf 1465b998875dSKevin Wolf bdrv_append(bs_snapshot, bs); 14661ba4b6a5SBenoît Canet 14671ba4b6a5SBenoît Canet out: 146873176beeSKevin Wolf QDECREF(snapshot_options); 14691ba4b6a5SBenoît Canet g_free(tmp_filename); 14706b8aeca5SChen Gang return ret; 1471b998875dSKevin Wolf } 1472b998875dSKevin Wolf 1473da557aacSMax Reitz /* 1474b6ce07aaSKevin Wolf * Opens a disk image (raw, qcow2, vmdk, ...) 1475de9c0cecSKevin Wolf * 1476de9c0cecSKevin Wolf * options is a QDict of options to pass to the block drivers, or NULL for an 1477de9c0cecSKevin Wolf * empty set of options. The reference to the QDict belongs to the block layer 1478de9c0cecSKevin Wolf * after the call (even on failure), so if the caller intends to reuse the 1479de9c0cecSKevin Wolf * dictionary, it needs to use QINCREF() before calling bdrv_open. 1480f67503e5SMax Reitz * 1481f67503e5SMax Reitz * If *pbs is NULL, a new BDS will be created with a pointer to it stored there. 1482f67503e5SMax Reitz * If it is not NULL, the referenced BDS will be reused. 1483ddf5636dSMax Reitz * 1484ddf5636dSMax Reitz * The reference parameter may be used to specify an existing block device which 1485ddf5636dSMax Reitz * should be opened. If specified, neither options nor a filename may be given, 1486ddf5636dSMax Reitz * nor can an existing BDS be reused (that is, *pbs has to be NULL). 1487b6ce07aaSKevin Wolf */ 1488f3930ed0SKevin Wolf static int bdrv_open_inherit(BlockDriverState **pbs, const char *filename, 1489ddf5636dSMax Reitz const char *reference, QDict *options, int flags, 1490f3930ed0SKevin Wolf BlockDriverState *parent, 1491ce343771SMax Reitz const BdrvChildRole *child_role, Error **errp) 1492ea2384d3Sbellard { 1493b6ce07aaSKevin Wolf int ret; 14949a4f4c31SKevin Wolf BdrvChild *file = NULL; 14959a4f4c31SKevin Wolf BlockDriverState *bs; 1496ce343771SMax Reitz BlockDriver *drv = NULL; 149774fe54f2SKevin Wolf const char *drvname; 14983e8c2e57SAlberto Garcia const char *backing; 149934b5d2c6SMax Reitz Error *local_err = NULL; 150073176beeSKevin Wolf QDict *snapshot_options = NULL; 1501b1e6fc08SKevin Wolf int snapshot_flags = 0; 150233e3963eSbellard 1503f67503e5SMax Reitz assert(pbs); 1504f3930ed0SKevin Wolf assert(!child_role || !flags); 1505f3930ed0SKevin Wolf assert(!child_role == !parent); 1506f67503e5SMax Reitz 1507ddf5636dSMax Reitz if (reference) { 1508ddf5636dSMax Reitz bool options_non_empty = options ? qdict_size(options) : false; 1509ddf5636dSMax Reitz QDECREF(options); 1510ddf5636dSMax Reitz 1511ddf5636dSMax Reitz if (*pbs) { 1512ddf5636dSMax Reitz error_setg(errp, "Cannot reuse an existing BDS when referencing " 1513ddf5636dSMax Reitz "another block device"); 1514ddf5636dSMax Reitz return -EINVAL; 1515ddf5636dSMax Reitz } 1516ddf5636dSMax Reitz 1517ddf5636dSMax Reitz if (filename || options_non_empty) { 1518ddf5636dSMax Reitz error_setg(errp, "Cannot reference an existing block device with " 1519ddf5636dSMax Reitz "additional options or a new filename"); 1520ddf5636dSMax Reitz return -EINVAL; 1521ddf5636dSMax Reitz } 1522ddf5636dSMax Reitz 1523ddf5636dSMax Reitz bs = bdrv_lookup_bs(reference, reference, errp); 1524ddf5636dSMax Reitz if (!bs) { 1525ddf5636dSMax Reitz return -ENODEV; 1526ddf5636dSMax Reitz } 152776b22320SKevin Wolf 152876b22320SKevin Wolf if (bs->throttle_state) { 152976b22320SKevin Wolf error_setg(errp, "Cannot reference an existing block device for " 153076b22320SKevin Wolf "which I/O throttling is enabled"); 153176b22320SKevin Wolf return -EINVAL; 153276b22320SKevin Wolf } 153376b22320SKevin Wolf 1534ddf5636dSMax Reitz bdrv_ref(bs); 1535ddf5636dSMax Reitz *pbs = bs; 1536ddf5636dSMax Reitz return 0; 1537ddf5636dSMax Reitz } 1538ddf5636dSMax Reitz 1539f67503e5SMax Reitz if (*pbs) { 1540f67503e5SMax Reitz bs = *pbs; 1541f67503e5SMax Reitz } else { 1542e4e9986bSMarkus Armbruster bs = bdrv_new(); 1543f67503e5SMax Reitz } 1544f67503e5SMax Reitz 1545de9c0cecSKevin Wolf /* NULL means an empty set of options */ 1546de9c0cecSKevin Wolf if (options == NULL) { 1547de9c0cecSKevin Wolf options = qdict_new(); 1548de9c0cecSKevin Wolf } 1549de9c0cecSKevin Wolf 1550145f598eSKevin Wolf /* json: syntax counts as explicit options, as if in the QDict */ 1551de3b53f0SKevin Wolf parse_json_protocol(options, &filename, &local_err); 1552de3b53f0SKevin Wolf if (local_err) { 1553de3b53f0SKevin Wolf ret = -EINVAL; 1554de3b53f0SKevin Wolf goto fail; 1555de3b53f0SKevin Wolf } 1556de3b53f0SKevin Wolf 1557145f598eSKevin Wolf bs->explicit_options = qdict_clone_shallow(options); 1558145f598eSKevin Wolf 1559f3930ed0SKevin Wolf if (child_role) { 1560bddcec37SKevin Wolf bs->inherits_from = parent; 15618e2160e2SKevin Wolf child_role->inherit_options(&flags, options, 15628e2160e2SKevin Wolf parent->open_flags, parent->options); 1563f3930ed0SKevin Wolf } 1564f3930ed0SKevin Wolf 1565de3b53f0SKevin Wolf ret = bdrv_fill_options(&options, filename, &flags, &local_err); 1566462f5bcfSKevin Wolf if (local_err) { 1567462f5bcfSKevin Wolf goto fail; 1568462f5bcfSKevin Wolf } 1569462f5bcfSKevin Wolf 157062392ebbSKevin Wolf bs->open_flags = flags; 157162392ebbSKevin Wolf bs->options = options; 157262392ebbSKevin Wolf options = qdict_clone_shallow(options); 157362392ebbSKevin Wolf 157476c591b0SKevin Wolf /* Find the right image format driver */ 157576c591b0SKevin Wolf drvname = qdict_get_try_str(options, "driver"); 157676c591b0SKevin Wolf if (drvname) { 157776c591b0SKevin Wolf drv = bdrv_find_format(drvname); 157876c591b0SKevin Wolf if (!drv) { 157976c591b0SKevin Wolf error_setg(errp, "Unknown driver: '%s'", drvname); 158076c591b0SKevin Wolf ret = -EINVAL; 158176c591b0SKevin Wolf goto fail; 158276c591b0SKevin Wolf } 158376c591b0SKevin Wolf } 158476c591b0SKevin Wolf 158576c591b0SKevin Wolf assert(drvname || !(flags & BDRV_O_PROTOCOL)); 158676c591b0SKevin Wolf 15873e8c2e57SAlberto Garcia backing = qdict_get_try_str(options, "backing"); 15883e8c2e57SAlberto Garcia if (backing && *backing == '\0') { 15893e8c2e57SAlberto Garcia flags |= BDRV_O_NO_BACKING; 15903e8c2e57SAlberto Garcia qdict_del(options, "backing"); 15913e8c2e57SAlberto Garcia } 15923e8c2e57SAlberto Garcia 1593f500a6d3SKevin Wolf /* Open image file without format layer */ 1594f4788adcSKevin Wolf if ((flags & BDRV_O_PROTOCOL) == 0) { 1595be028adcSJeff Cody if (flags & BDRV_O_RDWR) { 1596be028adcSJeff Cody flags |= BDRV_O_ALLOW_RDWR; 1597be028adcSJeff Cody } 1598b1e6fc08SKevin Wolf if (flags & BDRV_O_SNAPSHOT) { 159973176beeSKevin Wolf snapshot_options = qdict_new(); 160073176beeSKevin Wolf bdrv_temp_snapshot_options(&snapshot_flags, snapshot_options, 160173176beeSKevin Wolf flags, options); 16028e2160e2SKevin Wolf bdrv_backing_options(&flags, options, flags, options); 1603b1e6fc08SKevin Wolf } 1604be028adcSJeff Cody 1605f3930ed0SKevin Wolf bs->open_flags = flags; 16061fdd6933SKevin Wolf 16079a4f4c31SKevin Wolf file = bdrv_open_child(filename, options, "file", bs, 16081fdd6933SKevin Wolf &child_file, true, &local_err); 16091fdd6933SKevin Wolf if (local_err) { 16101fdd6933SKevin Wolf ret = -EINVAL; 16118bfea15dSKevin Wolf goto fail; 1612f500a6d3SKevin Wolf } 1613f4788adcSKevin Wolf } 1614f500a6d3SKevin Wolf 161576c591b0SKevin Wolf /* Image format probing */ 161638f3ef57SKevin Wolf bs->probed = !drv; 161776c591b0SKevin Wolf if (!drv && file) { 16189a4f4c31SKevin Wolf ret = find_image_format(file->bs, filename, &drv, &local_err); 161917b005f1SKevin Wolf if (ret < 0) { 162017b005f1SKevin Wolf goto fail; 162117b005f1SKevin Wolf } 162262392ebbSKevin Wolf /* 162362392ebbSKevin Wolf * This option update would logically belong in bdrv_fill_options(), 162462392ebbSKevin Wolf * but we first need to open bs->file for the probing to work, while 162562392ebbSKevin Wolf * opening bs->file already requires the (mostly) final set of options 162662392ebbSKevin Wolf * so that cache mode etc. can be inherited. 162762392ebbSKevin Wolf * 162862392ebbSKevin Wolf * Adding the driver later is somewhat ugly, but it's not an option 162962392ebbSKevin Wolf * that would ever be inherited, so it's correct. We just need to make 163062392ebbSKevin Wolf * sure to update both bs->options (which has the full effective 163162392ebbSKevin Wolf * options for bs) and options (which has file.* already removed). 163262392ebbSKevin Wolf */ 163362392ebbSKevin Wolf qdict_put(bs->options, "driver", qstring_from_str(drv->format_name)); 163462392ebbSKevin Wolf qdict_put(options, "driver", qstring_from_str(drv->format_name)); 163576c591b0SKevin Wolf } else if (!drv) { 16362a05cbe4SMax Reitz error_setg(errp, "Must specify either driver or file"); 16372a05cbe4SMax Reitz ret = -EINVAL; 16388bfea15dSKevin Wolf goto fail; 16392a05cbe4SMax Reitz } 1640f500a6d3SKevin Wolf 164153a29513SMax Reitz /* BDRV_O_PROTOCOL must be set iff a protocol BDS is about to be created */ 164253a29513SMax Reitz assert(!!(flags & BDRV_O_PROTOCOL) == !!drv->bdrv_file_open); 164353a29513SMax Reitz /* file must be NULL if a protocol BDS is about to be created 164453a29513SMax Reitz * (the inverse results in an error message from bdrv_open_common()) */ 164553a29513SMax Reitz assert(!(flags & BDRV_O_PROTOCOL) || !file); 164653a29513SMax Reitz 1647b6ce07aaSKevin Wolf /* Open the image */ 164882dc8b41SKevin Wolf ret = bdrv_open_common(bs, file, options, &local_err); 1649b6ce07aaSKevin Wolf if (ret < 0) { 16508bfea15dSKevin Wolf goto fail; 16516987307cSChristoph Hellwig } 16526987307cSChristoph Hellwig 16532a05cbe4SMax Reitz if (file && (bs->file != file)) { 16549a4f4c31SKevin Wolf bdrv_unref_child(bs, file); 1655f500a6d3SKevin Wolf file = NULL; 1656f500a6d3SKevin Wolf } 1657f500a6d3SKevin Wolf 1658b6ce07aaSKevin Wolf /* If there is a backing file, use it */ 16599156df12SPaolo Bonzini if ((flags & BDRV_O_NO_BACKING) == 0) { 1660d9b7b057SKevin Wolf ret = bdrv_open_backing_file(bs, options, "backing", &local_err); 1661b6ce07aaSKevin Wolf if (ret < 0) { 1662b6ad491aSKevin Wolf goto close_and_fail; 1663b6ce07aaSKevin Wolf } 1664b6ce07aaSKevin Wolf } 1665b6ce07aaSKevin Wolf 166691af7014SMax Reitz bdrv_refresh_filename(bs); 166791af7014SMax Reitz 1668b6ad491aSKevin Wolf /* Check if any unknown options were used */ 16695acd9d81SMax Reitz if (options && (qdict_size(options) != 0)) { 1670b6ad491aSKevin Wolf const QDictEntry *entry = qdict_first(options); 16715acd9d81SMax Reitz if (flags & BDRV_O_PROTOCOL) { 16725acd9d81SMax Reitz error_setg(errp, "Block protocol '%s' doesn't support the option " 16735acd9d81SMax Reitz "'%s'", drv->format_name, entry->key); 16745acd9d81SMax Reitz } else { 1675d0e46a55SMax Reitz error_setg(errp, 1676d0e46a55SMax Reitz "Block format '%s' does not support the option '%s'", 1677d0e46a55SMax Reitz drv->format_name, entry->key); 16785acd9d81SMax Reitz } 1679b6ad491aSKevin Wolf 1680b6ad491aSKevin Wolf ret = -EINVAL; 1681b6ad491aSKevin Wolf goto close_and_fail; 1682b6ad491aSKevin Wolf } 1683b6ad491aSKevin Wolf 1684b6ce07aaSKevin Wolf if (!bdrv_key_required(bs)) { 1685a7f53e26SMarkus Armbruster if (bs->blk) { 1686a7f53e26SMarkus Armbruster blk_dev_change_media_cb(bs->blk, true); 1687a7f53e26SMarkus Armbruster } 1688c3adb58fSMarkus Armbruster } else if (!runstate_check(RUN_STATE_PRELAUNCH) 1689c3adb58fSMarkus Armbruster && !runstate_check(RUN_STATE_INMIGRATE) 1690c3adb58fSMarkus Armbruster && !runstate_check(RUN_STATE_PAUSED)) { /* HACK */ 1691c3adb58fSMarkus Armbruster error_setg(errp, 1692c3adb58fSMarkus Armbruster "Guest must be stopped for opening of encrypted image"); 1693c3adb58fSMarkus Armbruster ret = -EBUSY; 1694c3adb58fSMarkus Armbruster goto close_and_fail; 1695b6ce07aaSKevin Wolf } 1696b6ce07aaSKevin Wolf 1697c3adb58fSMarkus Armbruster QDECREF(options); 1698f67503e5SMax Reitz *pbs = bs; 1699dd62f1caSKevin Wolf 1700dd62f1caSKevin Wolf /* For snapshot=on, create a temporary qcow2 overlay. bs points to the 1701dd62f1caSKevin Wolf * temporary snapshot afterwards. */ 1702dd62f1caSKevin Wolf if (snapshot_flags) { 170373176beeSKevin Wolf ret = bdrv_append_temp_snapshot(bs, snapshot_flags, snapshot_options, 170473176beeSKevin Wolf &local_err); 170573176beeSKevin Wolf snapshot_options = NULL; 1706dd62f1caSKevin Wolf if (local_err) { 1707dd62f1caSKevin Wolf goto close_and_fail; 1708dd62f1caSKevin Wolf } 1709dd62f1caSKevin Wolf } 1710dd62f1caSKevin Wolf 1711b6ce07aaSKevin Wolf return 0; 1712b6ce07aaSKevin Wolf 17138bfea15dSKevin Wolf fail: 1714f500a6d3SKevin Wolf if (file != NULL) { 17159a4f4c31SKevin Wolf bdrv_unref_child(bs, file); 1716f500a6d3SKevin Wolf } 171773176beeSKevin Wolf QDECREF(snapshot_options); 1718145f598eSKevin Wolf QDECREF(bs->explicit_options); 1719de9c0cecSKevin Wolf QDECREF(bs->options); 1720b6ad491aSKevin Wolf QDECREF(options); 1721de9c0cecSKevin Wolf bs->options = NULL; 1722f67503e5SMax Reitz if (!*pbs) { 1723f67503e5SMax Reitz /* If *pbs is NULL, a new BDS has been created in this function and 1724f67503e5SMax Reitz needs to be freed now. Otherwise, it does not need to be closed, 1725f67503e5SMax Reitz since it has not really been opened yet. */ 1726f67503e5SMax Reitz bdrv_unref(bs); 1727f67503e5SMax Reitz } 172884d18f06SMarkus Armbruster if (local_err) { 172934b5d2c6SMax Reitz error_propagate(errp, local_err); 173034b5d2c6SMax Reitz } 1731b6ad491aSKevin Wolf return ret; 1732de9c0cecSKevin Wolf 1733b6ad491aSKevin Wolf close_and_fail: 1734f67503e5SMax Reitz /* See fail path, but now the BDS has to be always closed */ 1735f67503e5SMax Reitz if (*pbs) { 1736b6ad491aSKevin Wolf bdrv_close(bs); 1737f67503e5SMax Reitz } else { 1738f67503e5SMax Reitz bdrv_unref(bs); 1739f67503e5SMax Reitz } 174073176beeSKevin Wolf QDECREF(snapshot_options); 1741b6ad491aSKevin Wolf QDECREF(options); 174284d18f06SMarkus Armbruster if (local_err) { 174334b5d2c6SMax Reitz error_propagate(errp, local_err); 174434b5d2c6SMax Reitz } 1745b6ce07aaSKevin Wolf return ret; 1746b6ce07aaSKevin Wolf } 1747b6ce07aaSKevin Wolf 1748f3930ed0SKevin Wolf int bdrv_open(BlockDriverState **pbs, const char *filename, 17496ebf9aa2SMax Reitz const char *reference, QDict *options, int flags, Error **errp) 1750f3930ed0SKevin Wolf { 1751f3930ed0SKevin Wolf return bdrv_open_inherit(pbs, filename, reference, options, flags, NULL, 1752ce343771SMax Reitz NULL, errp); 1753f3930ed0SKevin Wolf } 1754f3930ed0SKevin Wolf 1755e971aa12SJeff Cody typedef struct BlockReopenQueueEntry { 1756e971aa12SJeff Cody bool prepared; 1757e971aa12SJeff Cody BDRVReopenState state; 1758e971aa12SJeff Cody QSIMPLEQ_ENTRY(BlockReopenQueueEntry) entry; 1759e971aa12SJeff Cody } BlockReopenQueueEntry; 1760e971aa12SJeff Cody 1761e971aa12SJeff Cody /* 1762e971aa12SJeff Cody * Adds a BlockDriverState to a simple queue for an atomic, transactional 1763e971aa12SJeff Cody * reopen of multiple devices. 1764e971aa12SJeff Cody * 1765e971aa12SJeff Cody * bs_queue can either be an existing BlockReopenQueue that has had QSIMPLE_INIT 1766e971aa12SJeff Cody * already performed, or alternatively may be NULL a new BlockReopenQueue will 1767e971aa12SJeff Cody * be created and initialized. This newly created BlockReopenQueue should be 1768e971aa12SJeff Cody * passed back in for subsequent calls that are intended to be of the same 1769e971aa12SJeff Cody * atomic 'set'. 1770e971aa12SJeff Cody * 1771e971aa12SJeff Cody * bs is the BlockDriverState to add to the reopen queue. 1772e971aa12SJeff Cody * 17734d2cb092SKevin Wolf * options contains the changed options for the associated bs 17744d2cb092SKevin Wolf * (the BlockReopenQueue takes ownership) 17754d2cb092SKevin Wolf * 1776e971aa12SJeff Cody * flags contains the open flags for the associated bs 1777e971aa12SJeff Cody * 1778e971aa12SJeff Cody * returns a pointer to bs_queue, which is either the newly allocated 1779e971aa12SJeff Cody * bs_queue, or the existing bs_queue being used. 1780e971aa12SJeff Cody * 1781e971aa12SJeff Cody */ 178228518102SKevin Wolf static BlockReopenQueue *bdrv_reopen_queue_child(BlockReopenQueue *bs_queue, 17834d2cb092SKevin Wolf BlockDriverState *bs, 178428518102SKevin Wolf QDict *options, 178528518102SKevin Wolf int flags, 178628518102SKevin Wolf const BdrvChildRole *role, 178728518102SKevin Wolf QDict *parent_options, 178828518102SKevin Wolf int parent_flags) 1789e971aa12SJeff Cody { 1790e971aa12SJeff Cody assert(bs != NULL); 1791e971aa12SJeff Cody 1792e971aa12SJeff Cody BlockReopenQueueEntry *bs_entry; 179367251a31SKevin Wolf BdrvChild *child; 1794145f598eSKevin Wolf QDict *old_options, *explicit_options; 179567251a31SKevin Wolf 1796e971aa12SJeff Cody if (bs_queue == NULL) { 1797e971aa12SJeff Cody bs_queue = g_new0(BlockReopenQueue, 1); 1798e971aa12SJeff Cody QSIMPLEQ_INIT(bs_queue); 1799e971aa12SJeff Cody } 1800e971aa12SJeff Cody 18014d2cb092SKevin Wolf if (!options) { 18024d2cb092SKevin Wolf options = qdict_new(); 18034d2cb092SKevin Wolf } 18044d2cb092SKevin Wolf 180528518102SKevin Wolf /* 180628518102SKevin Wolf * Precedence of options: 180728518102SKevin Wolf * 1. Explicitly passed in options (highest) 180891a097e7SKevin Wolf * 2. Set in flags (only for top level) 1809145f598eSKevin Wolf * 3. Retained from explicitly set options of bs 18108e2160e2SKevin Wolf * 4. Inherited from parent node 181128518102SKevin Wolf * 5. Retained from effective options of bs 181228518102SKevin Wolf */ 181328518102SKevin Wolf 181491a097e7SKevin Wolf if (!parent_options) { 181591a097e7SKevin Wolf /* 181691a097e7SKevin Wolf * Any setting represented by flags is always updated. If the 181791a097e7SKevin Wolf * corresponding QDict option is set, it takes precedence. Otherwise 181891a097e7SKevin Wolf * the flag is translated into a QDict option. The old setting of bs is 181991a097e7SKevin Wolf * not considered. 182091a097e7SKevin Wolf */ 182191a097e7SKevin Wolf update_options_from_flags(options, flags); 182291a097e7SKevin Wolf } 182391a097e7SKevin Wolf 1824145f598eSKevin Wolf /* Old explicitly set values (don't overwrite by inherited value) */ 1825145f598eSKevin Wolf old_options = qdict_clone_shallow(bs->explicit_options); 1826145f598eSKevin Wolf bdrv_join_options(bs, options, old_options); 1827145f598eSKevin Wolf QDECREF(old_options); 1828145f598eSKevin Wolf 1829145f598eSKevin Wolf explicit_options = qdict_clone_shallow(options); 1830145f598eSKevin Wolf 183128518102SKevin Wolf /* Inherit from parent node */ 183228518102SKevin Wolf if (parent_options) { 183328518102SKevin Wolf assert(!flags); 18348e2160e2SKevin Wolf role->inherit_options(&flags, options, parent_flags, parent_options); 183528518102SKevin Wolf } 183628518102SKevin Wolf 183728518102SKevin Wolf /* Old values are used for options that aren't set yet */ 18384d2cb092SKevin Wolf old_options = qdict_clone_shallow(bs->options); 1839cddff5baSKevin Wolf bdrv_join_options(bs, options, old_options); 18404d2cb092SKevin Wolf QDECREF(old_options); 18414d2cb092SKevin Wolf 1842f1f25a2eSKevin Wolf /* bdrv_open() masks this flag out */ 1843f1f25a2eSKevin Wolf flags &= ~BDRV_O_PROTOCOL; 1844f1f25a2eSKevin Wolf 184567251a31SKevin Wolf QLIST_FOREACH(child, &bs->children, next) { 18464c9dfe5dSKevin Wolf QDict *new_child_options; 18474c9dfe5dSKevin Wolf char *child_key_dot; 184867251a31SKevin Wolf 18494c9dfe5dSKevin Wolf /* reopen can only change the options of block devices that were 18504c9dfe5dSKevin Wolf * implicitly created and inherited options. For other (referenced) 18514c9dfe5dSKevin Wolf * block devices, a syntax like "backing.foo" results in an error. */ 185267251a31SKevin Wolf if (child->bs->inherits_from != bs) { 185367251a31SKevin Wolf continue; 185467251a31SKevin Wolf } 185567251a31SKevin Wolf 18564c9dfe5dSKevin Wolf child_key_dot = g_strdup_printf("%s.", child->name); 18574c9dfe5dSKevin Wolf qdict_extract_subqdict(options, &new_child_options, child_key_dot); 18584c9dfe5dSKevin Wolf g_free(child_key_dot); 18594c9dfe5dSKevin Wolf 186028518102SKevin Wolf bdrv_reopen_queue_child(bs_queue, child->bs, new_child_options, 0, 186128518102SKevin Wolf child->role, options, flags); 1862e971aa12SJeff Cody } 1863e971aa12SJeff Cody 1864e971aa12SJeff Cody bs_entry = g_new0(BlockReopenQueueEntry, 1); 1865e971aa12SJeff Cody QSIMPLEQ_INSERT_TAIL(bs_queue, bs_entry, entry); 1866e971aa12SJeff Cody 1867e971aa12SJeff Cody bs_entry->state.bs = bs; 18684d2cb092SKevin Wolf bs_entry->state.options = options; 1869145f598eSKevin Wolf bs_entry->state.explicit_options = explicit_options; 1870e971aa12SJeff Cody bs_entry->state.flags = flags; 1871e971aa12SJeff Cody 1872e971aa12SJeff Cody return bs_queue; 1873e971aa12SJeff Cody } 1874e971aa12SJeff Cody 187528518102SKevin Wolf BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue, 187628518102SKevin Wolf BlockDriverState *bs, 187728518102SKevin Wolf QDict *options, int flags) 187828518102SKevin Wolf { 187928518102SKevin Wolf return bdrv_reopen_queue_child(bs_queue, bs, options, flags, 188028518102SKevin Wolf NULL, NULL, 0); 188128518102SKevin Wolf } 188228518102SKevin Wolf 1883e971aa12SJeff Cody /* 1884e971aa12SJeff Cody * Reopen multiple BlockDriverStates atomically & transactionally. 1885e971aa12SJeff Cody * 1886e971aa12SJeff Cody * The queue passed in (bs_queue) must have been built up previous 1887e971aa12SJeff Cody * via bdrv_reopen_queue(). 1888e971aa12SJeff Cody * 1889e971aa12SJeff Cody * Reopens all BDS specified in the queue, with the appropriate 1890e971aa12SJeff Cody * flags. All devices are prepared for reopen, and failure of any 1891e971aa12SJeff Cody * device will cause all device changes to be abandonded, and intermediate 1892e971aa12SJeff Cody * data cleaned up. 1893e971aa12SJeff Cody * 1894e971aa12SJeff Cody * If all devices prepare successfully, then the changes are committed 1895e971aa12SJeff Cody * to all devices. 1896e971aa12SJeff Cody * 1897e971aa12SJeff Cody */ 1898e971aa12SJeff Cody int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp) 1899e971aa12SJeff Cody { 1900e971aa12SJeff Cody int ret = -1; 1901e971aa12SJeff Cody BlockReopenQueueEntry *bs_entry, *next; 1902e971aa12SJeff Cody Error *local_err = NULL; 1903e971aa12SJeff Cody 1904e971aa12SJeff Cody assert(bs_queue != NULL); 1905e971aa12SJeff Cody 1906e971aa12SJeff Cody bdrv_drain_all(); 1907e971aa12SJeff Cody 1908e971aa12SJeff Cody QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) { 1909e971aa12SJeff Cody if (bdrv_reopen_prepare(&bs_entry->state, bs_queue, &local_err)) { 1910e971aa12SJeff Cody error_propagate(errp, local_err); 1911e971aa12SJeff Cody goto cleanup; 1912e971aa12SJeff Cody } 1913e971aa12SJeff Cody bs_entry->prepared = true; 1914e971aa12SJeff Cody } 1915e971aa12SJeff Cody 1916e971aa12SJeff Cody /* If we reach this point, we have success and just need to apply the 1917e971aa12SJeff Cody * changes 1918e971aa12SJeff Cody */ 1919e971aa12SJeff Cody QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) { 1920e971aa12SJeff Cody bdrv_reopen_commit(&bs_entry->state); 1921e971aa12SJeff Cody } 1922e971aa12SJeff Cody 1923e971aa12SJeff Cody ret = 0; 1924e971aa12SJeff Cody 1925e971aa12SJeff Cody cleanup: 1926e971aa12SJeff Cody QSIMPLEQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) { 1927e971aa12SJeff Cody if (ret && bs_entry->prepared) { 1928e971aa12SJeff Cody bdrv_reopen_abort(&bs_entry->state); 1929145f598eSKevin Wolf } else if (ret) { 1930145f598eSKevin Wolf QDECREF(bs_entry->state.explicit_options); 1931e971aa12SJeff Cody } 19324d2cb092SKevin Wolf QDECREF(bs_entry->state.options); 1933e971aa12SJeff Cody g_free(bs_entry); 1934e971aa12SJeff Cody } 1935e971aa12SJeff Cody g_free(bs_queue); 1936e971aa12SJeff Cody return ret; 1937e971aa12SJeff Cody } 1938e971aa12SJeff Cody 1939e971aa12SJeff Cody 1940e971aa12SJeff Cody /* Reopen a single BlockDriverState with the specified flags. */ 1941e971aa12SJeff Cody int bdrv_reopen(BlockDriverState *bs, int bdrv_flags, Error **errp) 1942e971aa12SJeff Cody { 1943e971aa12SJeff Cody int ret = -1; 1944e971aa12SJeff Cody Error *local_err = NULL; 19454d2cb092SKevin Wolf BlockReopenQueue *queue = bdrv_reopen_queue(NULL, bs, NULL, bdrv_flags); 1946e971aa12SJeff Cody 1947e971aa12SJeff Cody ret = bdrv_reopen_multiple(queue, &local_err); 1948e971aa12SJeff Cody if (local_err != NULL) { 1949e971aa12SJeff Cody error_propagate(errp, local_err); 1950e971aa12SJeff Cody } 1951e971aa12SJeff Cody return ret; 1952e971aa12SJeff Cody } 1953e971aa12SJeff Cody 1954e971aa12SJeff Cody 1955e971aa12SJeff Cody /* 1956e971aa12SJeff Cody * Prepares a BlockDriverState for reopen. All changes are staged in the 1957e971aa12SJeff Cody * 'opaque' field of the BDRVReopenState, which is used and allocated by 1958e971aa12SJeff Cody * the block driver layer .bdrv_reopen_prepare() 1959e971aa12SJeff Cody * 1960e971aa12SJeff Cody * bs is the BlockDriverState to reopen 1961e971aa12SJeff Cody * flags are the new open flags 1962e971aa12SJeff Cody * queue is the reopen queue 1963e971aa12SJeff Cody * 1964e971aa12SJeff Cody * Returns 0 on success, non-zero on error. On error errp will be set 1965e971aa12SJeff Cody * as well. 1966e971aa12SJeff Cody * 1967e971aa12SJeff Cody * On failure, bdrv_reopen_abort() will be called to clean up any data. 1968e971aa12SJeff Cody * It is the responsibility of the caller to then call the abort() or 1969e971aa12SJeff Cody * commit() for any other BDS that have been left in a prepare() state 1970e971aa12SJeff Cody * 1971e971aa12SJeff Cody */ 1972e971aa12SJeff Cody int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue, 1973e971aa12SJeff Cody Error **errp) 1974e971aa12SJeff Cody { 1975e971aa12SJeff Cody int ret = -1; 1976e971aa12SJeff Cody Error *local_err = NULL; 1977e971aa12SJeff Cody BlockDriver *drv; 1978ccf9dc07SKevin Wolf QemuOpts *opts; 1979ccf9dc07SKevin Wolf const char *value; 1980e971aa12SJeff Cody 1981e971aa12SJeff Cody assert(reopen_state != NULL); 1982e971aa12SJeff Cody assert(reopen_state->bs->drv != NULL); 1983e971aa12SJeff Cody drv = reopen_state->bs->drv; 1984e971aa12SJeff Cody 1985ccf9dc07SKevin Wolf /* Process generic block layer options */ 1986ccf9dc07SKevin Wolf opts = qemu_opts_create(&bdrv_runtime_opts, NULL, 0, &error_abort); 1987ccf9dc07SKevin Wolf qemu_opts_absorb_qdict(opts, reopen_state->options, &local_err); 1988ccf9dc07SKevin Wolf if (local_err) { 1989ccf9dc07SKevin Wolf error_propagate(errp, local_err); 1990ccf9dc07SKevin Wolf ret = -EINVAL; 1991ccf9dc07SKevin Wolf goto error; 1992ccf9dc07SKevin Wolf } 1993ccf9dc07SKevin Wolf 199491a097e7SKevin Wolf update_flags_from_options(&reopen_state->flags, opts); 199591a097e7SKevin Wolf 1996ccf9dc07SKevin Wolf /* node-name and driver must be unchanged. Put them back into the QDict, so 1997ccf9dc07SKevin Wolf * that they are checked at the end of this function. */ 1998ccf9dc07SKevin Wolf value = qemu_opt_get(opts, "node-name"); 1999ccf9dc07SKevin Wolf if (value) { 2000ccf9dc07SKevin Wolf qdict_put(reopen_state->options, "node-name", qstring_from_str(value)); 2001ccf9dc07SKevin Wolf } 2002ccf9dc07SKevin Wolf 2003ccf9dc07SKevin Wolf value = qemu_opt_get(opts, "driver"); 2004ccf9dc07SKevin Wolf if (value) { 2005ccf9dc07SKevin Wolf qdict_put(reopen_state->options, "driver", qstring_from_str(value)); 2006ccf9dc07SKevin Wolf } 2007ccf9dc07SKevin Wolf 2008e971aa12SJeff Cody /* if we are to stay read-only, do not allow permission change 2009e971aa12SJeff Cody * to r/w */ 2010e971aa12SJeff Cody if (!(reopen_state->bs->open_flags & BDRV_O_ALLOW_RDWR) && 2011e971aa12SJeff Cody reopen_state->flags & BDRV_O_RDWR) { 201281e5f78aSAlberto Garcia error_setg(errp, "Node '%s' is read only", 201381e5f78aSAlberto Garcia bdrv_get_device_or_node_name(reopen_state->bs)); 2014e971aa12SJeff Cody goto error; 2015e971aa12SJeff Cody } 2016e971aa12SJeff Cody 2017e971aa12SJeff Cody 2018e971aa12SJeff Cody ret = bdrv_flush(reopen_state->bs); 2019e971aa12SJeff Cody if (ret) { 2020455b0fdeSEric Blake error_setg_errno(errp, -ret, "Error flushing drive"); 2021e971aa12SJeff Cody goto error; 2022e971aa12SJeff Cody } 2023e971aa12SJeff Cody 2024e971aa12SJeff Cody if (drv->bdrv_reopen_prepare) { 2025e971aa12SJeff Cody ret = drv->bdrv_reopen_prepare(reopen_state, queue, &local_err); 2026e971aa12SJeff Cody if (ret) { 2027e971aa12SJeff Cody if (local_err != NULL) { 2028e971aa12SJeff Cody error_propagate(errp, local_err); 2029e971aa12SJeff Cody } else { 2030d8b6895fSLuiz Capitulino error_setg(errp, "failed while preparing to reopen image '%s'", 2031e971aa12SJeff Cody reopen_state->bs->filename); 2032e971aa12SJeff Cody } 2033e971aa12SJeff Cody goto error; 2034e971aa12SJeff Cody } 2035e971aa12SJeff Cody } else { 2036e971aa12SJeff Cody /* It is currently mandatory to have a bdrv_reopen_prepare() 2037e971aa12SJeff Cody * handler for each supported drv. */ 203881e5f78aSAlberto Garcia error_setg(errp, "Block format '%s' used by node '%s' " 203981e5f78aSAlberto Garcia "does not support reopening files", drv->format_name, 204081e5f78aSAlberto Garcia bdrv_get_device_or_node_name(reopen_state->bs)); 2041e971aa12SJeff Cody ret = -1; 2042e971aa12SJeff Cody goto error; 2043e971aa12SJeff Cody } 2044e971aa12SJeff Cody 20454d2cb092SKevin Wolf /* Options that are not handled are only okay if they are unchanged 20464d2cb092SKevin Wolf * compared to the old state. It is expected that some options are only 20474d2cb092SKevin Wolf * used for the initial open, but not reopen (e.g. filename) */ 20484d2cb092SKevin Wolf if (qdict_size(reopen_state->options)) { 20494d2cb092SKevin Wolf const QDictEntry *entry = qdict_first(reopen_state->options); 20504d2cb092SKevin Wolf 20514d2cb092SKevin Wolf do { 20524d2cb092SKevin Wolf QString *new_obj = qobject_to_qstring(entry->value); 20534d2cb092SKevin Wolf const char *new = qstring_get_str(new_obj); 20544d2cb092SKevin Wolf const char *old = qdict_get_try_str(reopen_state->bs->options, 20554d2cb092SKevin Wolf entry->key); 20564d2cb092SKevin Wolf 20574d2cb092SKevin Wolf if (!old || strcmp(new, old)) { 20584d2cb092SKevin Wolf error_setg(errp, "Cannot change the option '%s'", entry->key); 20594d2cb092SKevin Wolf ret = -EINVAL; 20604d2cb092SKevin Wolf goto error; 20614d2cb092SKevin Wolf } 20624d2cb092SKevin Wolf } while ((entry = qdict_next(reopen_state->options, entry))); 20634d2cb092SKevin Wolf } 20644d2cb092SKevin Wolf 2065e971aa12SJeff Cody ret = 0; 2066e971aa12SJeff Cody 2067e971aa12SJeff Cody error: 2068ccf9dc07SKevin Wolf qemu_opts_del(opts); 2069e971aa12SJeff Cody return ret; 2070e971aa12SJeff Cody } 2071e971aa12SJeff Cody 2072e971aa12SJeff Cody /* 2073e971aa12SJeff Cody * Takes the staged changes for the reopen from bdrv_reopen_prepare(), and 2074e971aa12SJeff Cody * makes them final by swapping the staging BlockDriverState contents into 2075e971aa12SJeff Cody * the active BlockDriverState contents. 2076e971aa12SJeff Cody */ 2077e971aa12SJeff Cody void bdrv_reopen_commit(BDRVReopenState *reopen_state) 2078e971aa12SJeff Cody { 2079e971aa12SJeff Cody BlockDriver *drv; 2080e971aa12SJeff Cody 2081e971aa12SJeff Cody assert(reopen_state != NULL); 2082e971aa12SJeff Cody drv = reopen_state->bs->drv; 2083e971aa12SJeff Cody assert(drv != NULL); 2084e971aa12SJeff Cody 2085e971aa12SJeff Cody /* If there are any driver level actions to take */ 2086e971aa12SJeff Cody if (drv->bdrv_reopen_commit) { 2087e971aa12SJeff Cody drv->bdrv_reopen_commit(reopen_state); 2088e971aa12SJeff Cody } 2089e971aa12SJeff Cody 2090e971aa12SJeff Cody /* set BDS specific flags now */ 2091145f598eSKevin Wolf QDECREF(reopen_state->bs->explicit_options); 2092145f598eSKevin Wolf 2093145f598eSKevin Wolf reopen_state->bs->explicit_options = reopen_state->explicit_options; 2094e971aa12SJeff Cody reopen_state->bs->open_flags = reopen_state->flags; 2095e971aa12SJeff Cody reopen_state->bs->read_only = !(reopen_state->flags & BDRV_O_RDWR); 2096355ef4acSKevin Wolf 20973baca891SKevin Wolf bdrv_refresh_limits(reopen_state->bs, NULL); 2098e971aa12SJeff Cody } 2099e971aa12SJeff Cody 2100e971aa12SJeff Cody /* 2101e971aa12SJeff Cody * Abort the reopen, and delete and free the staged changes in 2102e971aa12SJeff Cody * reopen_state 2103e971aa12SJeff Cody */ 2104e971aa12SJeff Cody void bdrv_reopen_abort(BDRVReopenState *reopen_state) 2105e971aa12SJeff Cody { 2106e971aa12SJeff Cody BlockDriver *drv; 2107e971aa12SJeff Cody 2108e971aa12SJeff Cody assert(reopen_state != NULL); 2109e971aa12SJeff Cody drv = reopen_state->bs->drv; 2110e971aa12SJeff Cody assert(drv != NULL); 2111e971aa12SJeff Cody 2112e971aa12SJeff Cody if (drv->bdrv_reopen_abort) { 2113e971aa12SJeff Cody drv->bdrv_reopen_abort(reopen_state); 2114e971aa12SJeff Cody } 2115145f598eSKevin Wolf 2116145f598eSKevin Wolf QDECREF(reopen_state->explicit_options); 2117e971aa12SJeff Cody } 2118e971aa12SJeff Cody 2119e971aa12SJeff Cody 212064dff520SMax Reitz static void bdrv_close(BlockDriverState *bs) 2121fc01f7e7Sbellard { 212233384421SMax Reitz BdrvAioNotifier *ban, *ban_next; 212333384421SMax Reitz 2124ca9bd24cSMax Reitz assert(!bs->job); 212599b7e775SAlberto Garcia 212699b7e775SAlberto Garcia /* Disable I/O limits and drain all pending throttled requests */ 2127a0d64a61SAlberto Garcia if (bs->throttle_state) { 212899b7e775SAlberto Garcia bdrv_io_limits_disable(bs); 212999b7e775SAlberto Garcia } 213099b7e775SAlberto Garcia 2131fc27291dSPaolo Bonzini bdrv_drained_begin(bs); /* complete I/O */ 213258fda173SStefan Hajnoczi bdrv_flush(bs); 213353ec73e2SFam Zheng bdrv_drain(bs); /* in case flush left pending I/O */ 2134fc27291dSPaolo Bonzini 2135c5acdc9aSMax Reitz bdrv_release_named_dirty_bitmaps(bs); 2136c5acdc9aSMax Reitz assert(QLIST_EMPTY(&bs->dirty_bitmaps)); 2137c5acdc9aSMax Reitz 2138b4d02820SMax Reitz if (bs->blk) { 2139b4d02820SMax Reitz blk_dev_change_media_cb(bs->blk, false); 2140b4d02820SMax Reitz } 2141b4d02820SMax Reitz 21423cbc002cSPaolo Bonzini if (bs->drv) { 21436e93e7c4SKevin Wolf BdrvChild *child, *next; 21446e93e7c4SKevin Wolf 21459a7dedbcSKevin Wolf bs->drv->bdrv_close(bs); 21469a4f4c31SKevin Wolf bs->drv = NULL; 21479a7dedbcSKevin Wolf 21489a7dedbcSKevin Wolf bdrv_set_backing_hd(bs, NULL); 21499a7dedbcSKevin Wolf 21509a4f4c31SKevin Wolf if (bs->file != NULL) { 21519a4f4c31SKevin Wolf bdrv_unref_child(bs, bs->file); 21529a4f4c31SKevin Wolf bs->file = NULL; 21539a4f4c31SKevin Wolf } 21549a4f4c31SKevin Wolf 21556e93e7c4SKevin Wolf QLIST_FOREACH_SAFE(child, &bs->children, next, next) { 215633a60407SKevin Wolf /* TODO Remove bdrv_unref() from drivers' close function and use 215733a60407SKevin Wolf * bdrv_unref_child() here */ 2158bddcec37SKevin Wolf if (child->bs->inherits_from == bs) { 2159bddcec37SKevin Wolf child->bs->inherits_from = NULL; 2160bddcec37SKevin Wolf } 216133a60407SKevin Wolf bdrv_detach_child(child); 21626e93e7c4SKevin Wolf } 21636e93e7c4SKevin Wolf 21647267c094SAnthony Liguori g_free(bs->opaque); 2165ea2384d3Sbellard bs->opaque = NULL; 216653fec9d3SStefan Hajnoczi bs->copy_on_read = 0; 2167a275fa42SPaolo Bonzini bs->backing_file[0] = '\0'; 2168a275fa42SPaolo Bonzini bs->backing_format[0] = '\0'; 21696405875cSPaolo Bonzini bs->total_sectors = 0; 21706405875cSPaolo Bonzini bs->encrypted = 0; 21716405875cSPaolo Bonzini bs->valid_key = 0; 21726405875cSPaolo Bonzini bs->sg = 0; 21730d51b4deSAsias He bs->zero_beyond_eof = false; 2174de9c0cecSKevin Wolf QDECREF(bs->options); 2175145f598eSKevin Wolf QDECREF(bs->explicit_options); 2176de9c0cecSKevin Wolf bs->options = NULL; 217791af7014SMax Reitz QDECREF(bs->full_open_options); 217891af7014SMax Reitz bs->full_open_options = NULL; 21799ca11154SPavel Hrdina } 218066f82ceeSKevin Wolf 218133384421SMax Reitz QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) { 218233384421SMax Reitz g_free(ban); 218333384421SMax Reitz } 218433384421SMax Reitz QLIST_INIT(&bs->aio_notifiers); 2185fc27291dSPaolo Bonzini bdrv_drained_end(bs); 2186b338082bSbellard } 2187b338082bSbellard 21882bc93fedSMORITA Kazutaka void bdrv_close_all(void) 21892bc93fedSMORITA Kazutaka { 21902bc93fedSMORITA Kazutaka BlockDriverState *bs; 2191ca9bd24cSMax Reitz AioContext *aio_context; 21922bc93fedSMORITA Kazutaka 2193ca9bd24cSMax Reitz /* Drop references from requests still in flight, such as canceled block 2194ca9bd24cSMax Reitz * jobs whose AIO context has not been polled yet */ 2195ca9bd24cSMax Reitz bdrv_drain_all(); 2196ca9bd24cSMax Reitz 2197ca9bd24cSMax Reitz blk_remove_all_bs(); 2198ca9bd24cSMax Reitz blockdev_close_all_bdrv_states(); 2199ca9bd24cSMax Reitz 2200ca9bd24cSMax Reitz /* Cancel all block jobs */ 2201ca9bd24cSMax Reitz while (!QTAILQ_EMPTY(&all_bdrv_states)) { 2202ca9bd24cSMax Reitz QTAILQ_FOREACH(bs, &all_bdrv_states, bs_list) { 2203ca9bd24cSMax Reitz aio_context = bdrv_get_aio_context(bs); 2204ed78cda3SStefan Hajnoczi 2205ed78cda3SStefan Hajnoczi aio_context_acquire(aio_context); 2206ca9bd24cSMax Reitz if (bs->job) { 2207ca9bd24cSMax Reitz block_job_cancel_sync(bs->job); 2208ed78cda3SStefan Hajnoczi aio_context_release(aio_context); 2209ca9bd24cSMax Reitz break; 2210ca9bd24cSMax Reitz } 2211ca9bd24cSMax Reitz aio_context_release(aio_context); 2212ca9bd24cSMax Reitz } 2213ca9bd24cSMax Reitz 2214ca9bd24cSMax Reitz /* All the remaining BlockDriverStates are referenced directly or 2215ca9bd24cSMax Reitz * indirectly from block jobs, so there needs to be at least one BDS 2216ca9bd24cSMax Reitz * directly used by a block job */ 2217ca9bd24cSMax Reitz assert(bs); 22182bc93fedSMORITA Kazutaka } 22192bc93fedSMORITA Kazutaka } 22202bc93fedSMORITA Kazutaka 22218e419aefSKevin Wolf /* Fields that need to stay with the top-level BDS */ 22224ddc07caSPaolo Bonzini static void bdrv_move_feature_fields(BlockDriverState *bs_dest, 22234ddc07caSPaolo Bonzini BlockDriverState *bs_src) 22244ddc07caSPaolo Bonzini { 22254ddc07caSPaolo Bonzini /* move some fields that need to stay attached to the device */ 2226dd62f1caSKevin Wolf } 2227dd62f1caSKevin Wolf 2228dd62f1caSKevin Wolf static void change_parent_backing_link(BlockDriverState *from, 2229dd62f1caSKevin Wolf BlockDriverState *to) 2230dd62f1caSKevin Wolf { 2231dd62f1caSKevin Wolf BdrvChild *c, *next; 2232dd62f1caSKevin Wolf 2233f21d96d0SKevin Wolf if (from->blk) { 2234f21d96d0SKevin Wolf /* FIXME We bypass blk_set_bs(), so we need to make these updates 2235f21d96d0SKevin Wolf * manually. The root problem is not in this change function, but the 2236f21d96d0SKevin Wolf * existence of BlockDriverState.blk. */ 2237f21d96d0SKevin Wolf to->blk = from->blk; 2238f21d96d0SKevin Wolf from->blk = NULL; 2239f21d96d0SKevin Wolf } 2240f21d96d0SKevin Wolf 2241dd62f1caSKevin Wolf QLIST_FOREACH_SAFE(c, &from->parents, next_parent, next) { 2242dd62f1caSKevin Wolf assert(c->role != &child_backing); 2243dd62f1caSKevin Wolf c->bs = to; 2244dd62f1caSKevin Wolf QLIST_REMOVE(c, next_parent); 2245dd62f1caSKevin Wolf QLIST_INSERT_HEAD(&to->parents, c, next_parent); 2246dd62f1caSKevin Wolf bdrv_ref(to); 2247dd62f1caSKevin Wolf bdrv_unref(from); 2248dd62f1caSKevin Wolf } 2249dd62f1caSKevin Wolf } 2250dd62f1caSKevin Wolf 2251dd62f1caSKevin Wolf static void swap_feature_fields(BlockDriverState *bs_top, 2252dd62f1caSKevin Wolf BlockDriverState *bs_new) 2253dd62f1caSKevin Wolf { 2254dd62f1caSKevin Wolf BlockDriverState tmp; 2255dd62f1caSKevin Wolf 2256dd62f1caSKevin Wolf bdrv_move_feature_fields(&tmp, bs_top); 2257dd62f1caSKevin Wolf bdrv_move_feature_fields(bs_top, bs_new); 2258dd62f1caSKevin Wolf bdrv_move_feature_fields(bs_new, &tmp); 2259dd62f1caSKevin Wolf 2260dd62f1caSKevin Wolf assert(!bs_new->throttle_state); 2261dd62f1caSKevin Wolf if (bs_top->throttle_state) { 2262dd62f1caSKevin Wolf bdrv_io_limits_enable(bs_new, throttle_group_get_name(bs_top)); 2263dd62f1caSKevin Wolf bdrv_io_limits_disable(bs_top); 2264dd62f1caSKevin Wolf } 2265dd62f1caSKevin Wolf } 2266dd62f1caSKevin Wolf 22678802d1fdSJeff Cody /* 22688802d1fdSJeff Cody * Add new bs contents at the top of an image chain while the chain is 22698802d1fdSJeff Cody * live, while keeping required fields on the top layer. 22708802d1fdSJeff Cody * 22718802d1fdSJeff Cody * This will modify the BlockDriverState fields, and swap contents 22728802d1fdSJeff Cody * between bs_new and bs_top. Both bs_new and bs_top are modified. 22738802d1fdSJeff Cody * 2274bfb197e0SMarkus Armbruster * bs_new must not be attached to a BlockBackend. 2275f6801b83SJeff Cody * 22768802d1fdSJeff Cody * This function does not create any image files. 2277dd62f1caSKevin Wolf * 2278dd62f1caSKevin Wolf * bdrv_append() takes ownership of a bs_new reference and unrefs it because 2279dd62f1caSKevin Wolf * that's what the callers commonly need. bs_new will be referenced by the old 2280dd62f1caSKevin Wolf * parents of bs_top after bdrv_append() returns. If the caller needs to keep a 2281dd62f1caSKevin Wolf * reference of its own, it must call bdrv_ref(). 22828802d1fdSJeff Cody */ 22838802d1fdSJeff Cody void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top) 22848802d1fdSJeff Cody { 2285dd62f1caSKevin Wolf assert(!bdrv_requests_pending(bs_top)); 2286dd62f1caSKevin Wolf assert(!bdrv_requests_pending(bs_new)); 22878802d1fdSJeff Cody 2288dd62f1caSKevin Wolf bdrv_ref(bs_top); 2289dd62f1caSKevin Wolf change_parent_backing_link(bs_top, bs_new); 2290dd62f1caSKevin Wolf 2291dd62f1caSKevin Wolf /* Some fields always stay on top of the backing file chain */ 2292dd62f1caSKevin Wolf swap_feature_fields(bs_top, bs_new); 2293dd62f1caSKevin Wolf 2294dd62f1caSKevin Wolf bdrv_set_backing_hd(bs_new, bs_top); 2295dd62f1caSKevin Wolf bdrv_unref(bs_top); 2296dd62f1caSKevin Wolf 2297dd62f1caSKevin Wolf /* bs_new is now referenced by its new parents, we don't need the 2298dd62f1caSKevin Wolf * additional reference any more. */ 2299dd62f1caSKevin Wolf bdrv_unref(bs_new); 23008802d1fdSJeff Cody } 23018802d1fdSJeff Cody 23023f09bfbcSKevin Wolf void bdrv_replace_in_backing_chain(BlockDriverState *old, BlockDriverState *new) 23033f09bfbcSKevin Wolf { 23043f09bfbcSKevin Wolf assert(!bdrv_requests_pending(old)); 23053f09bfbcSKevin Wolf assert(!bdrv_requests_pending(new)); 23063f09bfbcSKevin Wolf 23073f09bfbcSKevin Wolf bdrv_ref(old); 23083f09bfbcSKevin Wolf 23093f09bfbcSKevin Wolf if (old->blk) { 23103f09bfbcSKevin Wolf /* As long as these fields aren't in BlockBackend, but in the top-level 23113f09bfbcSKevin Wolf * BlockDriverState, it's not possible for a BDS to have two BBs. 23123f09bfbcSKevin Wolf * 23133f09bfbcSKevin Wolf * We really want to copy the fields from old to new, but we go for a 23143f09bfbcSKevin Wolf * swap instead so that pointers aren't duplicated and cause trouble. 23153f09bfbcSKevin Wolf * (Also, bdrv_swap() used to do the same.) */ 23163f09bfbcSKevin Wolf assert(!new->blk); 23173f09bfbcSKevin Wolf swap_feature_fields(old, new); 23183f09bfbcSKevin Wolf } 23193f09bfbcSKevin Wolf change_parent_backing_link(old, new); 23203f09bfbcSKevin Wolf 23213f09bfbcSKevin Wolf /* Change backing files if a previously independent node is added to the 23223f09bfbcSKevin Wolf * chain. For active commit, we replace top by its own (indirect) backing 23233f09bfbcSKevin Wolf * file and don't do anything here so we don't build a loop. */ 23243f09bfbcSKevin Wolf if (new->backing == NULL && !bdrv_chain_contains(backing_bs(old), new)) { 23253f09bfbcSKevin Wolf bdrv_set_backing_hd(new, backing_bs(old)); 23263f09bfbcSKevin Wolf bdrv_set_backing_hd(old, NULL); 23273f09bfbcSKevin Wolf } 23283f09bfbcSKevin Wolf 23293f09bfbcSKevin Wolf bdrv_unref(old); 23303f09bfbcSKevin Wolf } 23313f09bfbcSKevin Wolf 23324f6fd349SFam Zheng static void bdrv_delete(BlockDriverState *bs) 2333b338082bSbellard { 23343e914655SPaolo Bonzini assert(!bs->job); 23353718d8abSFam Zheng assert(bdrv_op_blocker_is_empty(bs)); 23364f6fd349SFam Zheng assert(!bs->refcnt); 233718846deeSMarkus Armbruster 2338e1b5c52eSStefan Hajnoczi bdrv_close(bs); 2339e1b5c52eSStefan Hajnoczi 23401b7bdbc1SStefan Hajnoczi /* remove from list, if necessary */ 234163eaaae0SKevin Wolf if (bs->node_name[0] != '\0') { 234263eaaae0SKevin Wolf QTAILQ_REMOVE(&graph_bdrv_states, bs, node_list); 234363eaaae0SKevin Wolf } 23442c1d04e0SMax Reitz QTAILQ_REMOVE(&all_bdrv_states, bs, bs_list); 23452c1d04e0SMax Reitz 23467267c094SAnthony Liguori g_free(bs); 2347fc01f7e7Sbellard } 2348fc01f7e7Sbellard 2349e97fc193Saliguori /* 2350e97fc193Saliguori * Run consistency checks on an image 2351e97fc193Saliguori * 2352e076f338SKevin Wolf * Returns 0 if the check could be completed (it doesn't mean that the image is 2353a1c7273bSStefan Weil * free of errors) or -errno when an internal error occurred. The results of the 2354e076f338SKevin Wolf * check are stored in res. 2355e97fc193Saliguori */ 23564534ff54SKevin Wolf int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix) 2357e97fc193Saliguori { 2358908bcd54SMax Reitz if (bs->drv == NULL) { 2359908bcd54SMax Reitz return -ENOMEDIUM; 2360908bcd54SMax Reitz } 2361e97fc193Saliguori if (bs->drv->bdrv_check == NULL) { 2362e97fc193Saliguori return -ENOTSUP; 2363e97fc193Saliguori } 2364e97fc193Saliguori 2365e076f338SKevin Wolf memset(res, 0, sizeof(*res)); 23664534ff54SKevin Wolf return bs->drv->bdrv_check(bs, res, fix); 2367e97fc193Saliguori } 2368e97fc193Saliguori 23698a426614SKevin Wolf #define COMMIT_BUF_SECTORS 2048 23708a426614SKevin Wolf 237133e3963eSbellard /* commit COW file into the raw image */ 237233e3963eSbellard int bdrv_commit(BlockDriverState *bs) 237333e3963eSbellard { 237419cb3738Sbellard BlockDriver *drv = bs->drv; 237572706ea4SJeff Cody int64_t sector, total_sectors, length, backing_length; 23768a426614SKevin Wolf int n, ro, open_flags; 23770bce597dSJeff Cody int ret = 0; 237872706ea4SJeff Cody uint8_t *buf = NULL; 237933e3963eSbellard 238019cb3738Sbellard if (!drv) 238119cb3738Sbellard return -ENOMEDIUM; 238233e3963eSbellard 2383760e0063SKevin Wolf if (!bs->backing) { 23844dca4b63SNaphtali Sprei return -ENOTSUP; 23854dca4b63SNaphtali Sprei } 23864dca4b63SNaphtali Sprei 2387bb00021dSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, NULL) || 2388760e0063SKevin Wolf bdrv_op_is_blocked(bs->backing->bs, BLOCK_OP_TYPE_COMMIT_TARGET, NULL)) { 23892d3735d3SStefan Hajnoczi return -EBUSY; 23902d3735d3SStefan Hajnoczi } 23912d3735d3SStefan Hajnoczi 2392760e0063SKevin Wolf ro = bs->backing->bs->read_only; 2393760e0063SKevin Wolf open_flags = bs->backing->bs->open_flags; 23944dca4b63SNaphtali Sprei 23954dca4b63SNaphtali Sprei if (ro) { 2396760e0063SKevin Wolf if (bdrv_reopen(bs->backing->bs, open_flags | BDRV_O_RDWR, NULL)) { 23970bce597dSJeff Cody return -EACCES; 23984dca4b63SNaphtali Sprei } 2399ea2384d3Sbellard } 2400ea2384d3Sbellard 240172706ea4SJeff Cody length = bdrv_getlength(bs); 240272706ea4SJeff Cody if (length < 0) { 240372706ea4SJeff Cody ret = length; 240472706ea4SJeff Cody goto ro_cleanup; 240572706ea4SJeff Cody } 240672706ea4SJeff Cody 2407760e0063SKevin Wolf backing_length = bdrv_getlength(bs->backing->bs); 240872706ea4SJeff Cody if (backing_length < 0) { 240972706ea4SJeff Cody ret = backing_length; 241072706ea4SJeff Cody goto ro_cleanup; 241172706ea4SJeff Cody } 241272706ea4SJeff Cody 241372706ea4SJeff Cody /* If our top snapshot is larger than the backing file image, 241472706ea4SJeff Cody * grow the backing file image if possible. If not possible, 241572706ea4SJeff Cody * we must return an error */ 241672706ea4SJeff Cody if (length > backing_length) { 2417760e0063SKevin Wolf ret = bdrv_truncate(bs->backing->bs, length); 241872706ea4SJeff Cody if (ret < 0) { 241972706ea4SJeff Cody goto ro_cleanup; 242072706ea4SJeff Cody } 242172706ea4SJeff Cody } 242272706ea4SJeff Cody 242372706ea4SJeff Cody total_sectors = length >> BDRV_SECTOR_BITS; 2424857d4f46SKevin Wolf 2425857d4f46SKevin Wolf /* qemu_try_blockalign() for bs will choose an alignment that works for 2426760e0063SKevin Wolf * bs->backing->bs as well, so no need to compare the alignment manually. */ 2427857d4f46SKevin Wolf buf = qemu_try_blockalign(bs, COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE); 2428857d4f46SKevin Wolf if (buf == NULL) { 2429857d4f46SKevin Wolf ret = -ENOMEM; 2430857d4f46SKevin Wolf goto ro_cleanup; 2431857d4f46SKevin Wolf } 24328a426614SKevin Wolf 24338a426614SKevin Wolf for (sector = 0; sector < total_sectors; sector += n) { 2434d663640cSPaolo Bonzini ret = bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n); 2435d663640cSPaolo Bonzini if (ret < 0) { 2436d663640cSPaolo Bonzini goto ro_cleanup; 2437d663640cSPaolo Bonzini } 2438d663640cSPaolo Bonzini if (ret) { 2439dabfa6ccSKevin Wolf ret = bdrv_read(bs, sector, buf, n); 2440dabfa6ccSKevin Wolf if (ret < 0) { 24414dca4b63SNaphtali Sprei goto ro_cleanup; 244233e3963eSbellard } 244333e3963eSbellard 2444760e0063SKevin Wolf ret = bdrv_write(bs->backing->bs, sector, buf, n); 2445dabfa6ccSKevin Wolf if (ret < 0) { 24464dca4b63SNaphtali Sprei goto ro_cleanup; 244733e3963eSbellard } 244833e3963eSbellard } 244933e3963eSbellard } 245095389c86Sbellard 24511d44952fSChristoph Hellwig if (drv->bdrv_make_empty) { 24521d44952fSChristoph Hellwig ret = drv->bdrv_make_empty(bs); 2453dabfa6ccSKevin Wolf if (ret < 0) { 2454dabfa6ccSKevin Wolf goto ro_cleanup; 2455dabfa6ccSKevin Wolf } 24561d44952fSChristoph Hellwig bdrv_flush(bs); 24571d44952fSChristoph Hellwig } 245895389c86Sbellard 24593f5075aeSChristoph Hellwig /* 24603f5075aeSChristoph Hellwig * Make sure all data we wrote to the backing device is actually 24613f5075aeSChristoph Hellwig * stable on disk. 24623f5075aeSChristoph Hellwig */ 2463760e0063SKevin Wolf if (bs->backing) { 2464760e0063SKevin Wolf bdrv_flush(bs->backing->bs); 2465dabfa6ccSKevin Wolf } 24664dca4b63SNaphtali Sprei 2467dabfa6ccSKevin Wolf ret = 0; 24684dca4b63SNaphtali Sprei ro_cleanup: 2469857d4f46SKevin Wolf qemu_vfree(buf); 24704dca4b63SNaphtali Sprei 24714dca4b63SNaphtali Sprei if (ro) { 24720bce597dSJeff Cody /* ignoring error return here */ 2473760e0063SKevin Wolf bdrv_reopen(bs->backing->bs, open_flags & ~BDRV_O_RDWR, NULL); 24744dca4b63SNaphtali Sprei } 24754dca4b63SNaphtali Sprei 24761d44952fSChristoph Hellwig return ret; 247733e3963eSbellard } 247833e3963eSbellard 2479756e6736SKevin Wolf /* 2480756e6736SKevin Wolf * Return values: 2481756e6736SKevin Wolf * 0 - success 2482756e6736SKevin Wolf * -EINVAL - backing format specified, but no file 2483756e6736SKevin Wolf * -ENOSPC - can't update the backing file because no space is left in the 2484756e6736SKevin Wolf * image file header 2485756e6736SKevin Wolf * -ENOTSUP - format driver doesn't support changing the backing file 2486756e6736SKevin Wolf */ 2487756e6736SKevin Wolf int bdrv_change_backing_file(BlockDriverState *bs, 2488756e6736SKevin Wolf const char *backing_file, const char *backing_fmt) 2489756e6736SKevin Wolf { 2490756e6736SKevin Wolf BlockDriver *drv = bs->drv; 2491469ef350SPaolo Bonzini int ret; 2492756e6736SKevin Wolf 24935f377794SPaolo Bonzini /* Backing file format doesn't make sense without a backing file */ 24945f377794SPaolo Bonzini if (backing_fmt && !backing_file) { 24955f377794SPaolo Bonzini return -EINVAL; 24965f377794SPaolo Bonzini } 24975f377794SPaolo Bonzini 2498756e6736SKevin Wolf if (drv->bdrv_change_backing_file != NULL) { 2499469ef350SPaolo Bonzini ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt); 2500756e6736SKevin Wolf } else { 2501469ef350SPaolo Bonzini ret = -ENOTSUP; 2502756e6736SKevin Wolf } 2503469ef350SPaolo Bonzini 2504469ef350SPaolo Bonzini if (ret == 0) { 2505469ef350SPaolo Bonzini pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: ""); 2506469ef350SPaolo Bonzini pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: ""); 2507469ef350SPaolo Bonzini } 2508469ef350SPaolo Bonzini return ret; 2509756e6736SKevin Wolf } 2510756e6736SKevin Wolf 25116ebdcee2SJeff Cody /* 25126ebdcee2SJeff Cody * Finds the image layer in the chain that has 'bs' as its backing file. 25136ebdcee2SJeff Cody * 25146ebdcee2SJeff Cody * active is the current topmost image. 25156ebdcee2SJeff Cody * 25166ebdcee2SJeff Cody * Returns NULL if bs is not found in active's image chain, 25176ebdcee2SJeff Cody * or if active == bs. 25184caf0fcdSJeff Cody * 25194caf0fcdSJeff Cody * Returns the bottommost base image if bs == NULL. 25206ebdcee2SJeff Cody */ 25216ebdcee2SJeff Cody BlockDriverState *bdrv_find_overlay(BlockDriverState *active, 25226ebdcee2SJeff Cody BlockDriverState *bs) 25236ebdcee2SJeff Cody { 2524760e0063SKevin Wolf while (active && bs != backing_bs(active)) { 2525760e0063SKevin Wolf active = backing_bs(active); 25266ebdcee2SJeff Cody } 25276ebdcee2SJeff Cody 25284caf0fcdSJeff Cody return active; 25296ebdcee2SJeff Cody } 25306ebdcee2SJeff Cody 25314caf0fcdSJeff Cody /* Given a BDS, searches for the base layer. */ 25324caf0fcdSJeff Cody BlockDriverState *bdrv_find_base(BlockDriverState *bs) 25334caf0fcdSJeff Cody { 25344caf0fcdSJeff Cody return bdrv_find_overlay(bs, NULL); 25356ebdcee2SJeff Cody } 25366ebdcee2SJeff Cody 25376ebdcee2SJeff Cody /* 25386ebdcee2SJeff Cody * Drops images above 'base' up to and including 'top', and sets the image 25396ebdcee2SJeff Cody * above 'top' to have base as its backing file. 25406ebdcee2SJeff Cody * 25416ebdcee2SJeff Cody * Requires that the overlay to 'top' is opened r/w, so that the backing file 25426ebdcee2SJeff Cody * information in 'bs' can be properly updated. 25436ebdcee2SJeff Cody * 25446ebdcee2SJeff Cody * E.g., this will convert the following chain: 25456ebdcee2SJeff Cody * bottom <- base <- intermediate <- top <- active 25466ebdcee2SJeff Cody * 25476ebdcee2SJeff Cody * to 25486ebdcee2SJeff Cody * 25496ebdcee2SJeff Cody * bottom <- base <- active 25506ebdcee2SJeff Cody * 25516ebdcee2SJeff Cody * It is allowed for bottom==base, in which case it converts: 25526ebdcee2SJeff Cody * 25536ebdcee2SJeff Cody * base <- intermediate <- top <- active 25546ebdcee2SJeff Cody * 25556ebdcee2SJeff Cody * to 25566ebdcee2SJeff Cody * 25576ebdcee2SJeff Cody * base <- active 25586ebdcee2SJeff Cody * 255954e26900SJeff Cody * If backing_file_str is non-NULL, it will be used when modifying top's 256054e26900SJeff Cody * overlay image metadata. 256154e26900SJeff Cody * 25626ebdcee2SJeff Cody * Error conditions: 25636ebdcee2SJeff Cody * if active == top, that is considered an error 25646ebdcee2SJeff Cody * 25656ebdcee2SJeff Cody */ 25666ebdcee2SJeff Cody int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top, 256754e26900SJeff Cody BlockDriverState *base, const char *backing_file_str) 25686ebdcee2SJeff Cody { 25696ebdcee2SJeff Cody BlockDriverState *new_top_bs = NULL; 25706ebdcee2SJeff Cody int ret = -EIO; 25716ebdcee2SJeff Cody 25726ebdcee2SJeff Cody if (!top->drv || !base->drv) { 25736ebdcee2SJeff Cody goto exit; 25746ebdcee2SJeff Cody } 25756ebdcee2SJeff Cody 25766ebdcee2SJeff Cody new_top_bs = bdrv_find_overlay(active, top); 25776ebdcee2SJeff Cody 25786ebdcee2SJeff Cody if (new_top_bs == NULL) { 25796ebdcee2SJeff Cody /* we could not find the image above 'top', this is an error */ 25806ebdcee2SJeff Cody goto exit; 25816ebdcee2SJeff Cody } 25826ebdcee2SJeff Cody 2583760e0063SKevin Wolf /* special case of new_top_bs->backing->bs already pointing to base - nothing 25846ebdcee2SJeff Cody * to do, no intermediate images */ 2585760e0063SKevin Wolf if (backing_bs(new_top_bs) == base) { 25866ebdcee2SJeff Cody ret = 0; 25876ebdcee2SJeff Cody goto exit; 25886ebdcee2SJeff Cody } 25896ebdcee2SJeff Cody 25905db15a57SKevin Wolf /* Make sure that base is in the backing chain of top */ 25915db15a57SKevin Wolf if (!bdrv_chain_contains(top, base)) { 25926ebdcee2SJeff Cody goto exit; 25936ebdcee2SJeff Cody } 25946ebdcee2SJeff Cody 25956ebdcee2SJeff Cody /* success - we can delete the intermediate states, and link top->base */ 25965db15a57SKevin Wolf backing_file_str = backing_file_str ? backing_file_str : base->filename; 259754e26900SJeff Cody ret = bdrv_change_backing_file(new_top_bs, backing_file_str, 25985db15a57SKevin Wolf base->drv ? base->drv->format_name : ""); 25996ebdcee2SJeff Cody if (ret) { 26006ebdcee2SJeff Cody goto exit; 26016ebdcee2SJeff Cody } 26025db15a57SKevin Wolf bdrv_set_backing_hd(new_top_bs, base); 26036ebdcee2SJeff Cody 26046ebdcee2SJeff Cody ret = 0; 26056ebdcee2SJeff Cody exit: 26066ebdcee2SJeff Cody return ret; 26076ebdcee2SJeff Cody } 26086ebdcee2SJeff Cody 260983f64091Sbellard /** 261083f64091Sbellard * Truncate file to 'offset' bytes (needed only for file protocols) 261183f64091Sbellard */ 261283f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset) 261383f64091Sbellard { 261483f64091Sbellard BlockDriver *drv = bs->drv; 261551762288SStefan Hajnoczi int ret; 261683f64091Sbellard if (!drv) 261719cb3738Sbellard return -ENOMEDIUM; 261883f64091Sbellard if (!drv->bdrv_truncate) 261983f64091Sbellard return -ENOTSUP; 262059f2689dSNaphtali Sprei if (bs->read_only) 262159f2689dSNaphtali Sprei return -EACCES; 26229c75e168SJeff Cody 262351762288SStefan Hajnoczi ret = drv->bdrv_truncate(bs, offset); 262451762288SStefan Hajnoczi if (ret == 0) { 262551762288SStefan Hajnoczi ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); 2626ce1ffea8SJohn Snow bdrv_dirty_bitmap_truncate(bs); 2627a7f53e26SMarkus Armbruster if (bs->blk) { 2628a7f53e26SMarkus Armbruster blk_dev_resize_cb(bs->blk); 2629a7f53e26SMarkus Armbruster } 263051762288SStefan Hajnoczi } 263151762288SStefan Hajnoczi return ret; 263283f64091Sbellard } 263383f64091Sbellard 263483f64091Sbellard /** 26354a1d5e1fSFam Zheng * Length of a allocated file in bytes. Sparse files are counted by actual 26364a1d5e1fSFam Zheng * allocated space. Return < 0 if error or unknown. 26374a1d5e1fSFam Zheng */ 26384a1d5e1fSFam Zheng int64_t bdrv_get_allocated_file_size(BlockDriverState *bs) 26394a1d5e1fSFam Zheng { 26404a1d5e1fSFam Zheng BlockDriver *drv = bs->drv; 26414a1d5e1fSFam Zheng if (!drv) { 26424a1d5e1fSFam Zheng return -ENOMEDIUM; 26434a1d5e1fSFam Zheng } 26444a1d5e1fSFam Zheng if (drv->bdrv_get_allocated_file_size) { 26454a1d5e1fSFam Zheng return drv->bdrv_get_allocated_file_size(bs); 26464a1d5e1fSFam Zheng } 26474a1d5e1fSFam Zheng if (bs->file) { 26489a4f4c31SKevin Wolf return bdrv_get_allocated_file_size(bs->file->bs); 26494a1d5e1fSFam Zheng } 26504a1d5e1fSFam Zheng return -ENOTSUP; 26514a1d5e1fSFam Zheng } 26524a1d5e1fSFam Zheng 26534a1d5e1fSFam Zheng /** 265465a9bb25SMarkus Armbruster * Return number of sectors on success, -errno on error. 265583f64091Sbellard */ 265665a9bb25SMarkus Armbruster int64_t bdrv_nb_sectors(BlockDriverState *bs) 265783f64091Sbellard { 265883f64091Sbellard BlockDriver *drv = bs->drv; 265965a9bb25SMarkus Armbruster 266083f64091Sbellard if (!drv) 266119cb3738Sbellard return -ENOMEDIUM; 266251762288SStefan Hajnoczi 2663b94a2610SKevin Wolf if (drv->has_variable_length) { 2664b94a2610SKevin Wolf int ret = refresh_total_sectors(bs, bs->total_sectors); 2665b94a2610SKevin Wolf if (ret < 0) { 2666b94a2610SKevin Wolf return ret; 2667fc01f7e7Sbellard } 266846a4e4e6SStefan Hajnoczi } 266965a9bb25SMarkus Armbruster return bs->total_sectors; 267065a9bb25SMarkus Armbruster } 267165a9bb25SMarkus Armbruster 267265a9bb25SMarkus Armbruster /** 267365a9bb25SMarkus Armbruster * Return length in bytes on success, -errno on error. 267465a9bb25SMarkus Armbruster * The length is always a multiple of BDRV_SECTOR_SIZE. 267565a9bb25SMarkus Armbruster */ 267665a9bb25SMarkus Armbruster int64_t bdrv_getlength(BlockDriverState *bs) 267765a9bb25SMarkus Armbruster { 267865a9bb25SMarkus Armbruster int64_t ret = bdrv_nb_sectors(bs); 267965a9bb25SMarkus Armbruster 26804a9c9ea0SFam Zheng ret = ret > INT64_MAX / BDRV_SECTOR_SIZE ? -EFBIG : ret; 268165a9bb25SMarkus Armbruster return ret < 0 ? ret : ret * BDRV_SECTOR_SIZE; 268246a4e4e6SStefan Hajnoczi } 2683fc01f7e7Sbellard 268419cb3738Sbellard /* return 0 as number of sectors if no device present or error */ 268596b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) 2686fc01f7e7Sbellard { 268765a9bb25SMarkus Armbruster int64_t nb_sectors = bdrv_nb_sectors(bs); 268865a9bb25SMarkus Armbruster 268965a9bb25SMarkus Armbruster *nb_sectors_ptr = nb_sectors < 0 ? 0 : nb_sectors; 2690fc01f7e7Sbellard } 2691cf98951bSbellard 2692b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs) 2693b338082bSbellard { 2694b338082bSbellard return bs->read_only; 2695b338082bSbellard } 2696b338082bSbellard 2697985a03b0Sths int bdrv_is_sg(BlockDriverState *bs) 2698985a03b0Sths { 2699985a03b0Sths return bs->sg; 2700985a03b0Sths } 2701985a03b0Sths 2702ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs) 2703ea2384d3Sbellard { 2704760e0063SKevin Wolf if (bs->backing && bs->backing->bs->encrypted) { 2705ea2384d3Sbellard return 1; 2706760e0063SKevin Wolf } 2707ea2384d3Sbellard return bs->encrypted; 2708ea2384d3Sbellard } 2709ea2384d3Sbellard 2710c0f4ce77Saliguori int bdrv_key_required(BlockDriverState *bs) 2711c0f4ce77Saliguori { 2712760e0063SKevin Wolf BdrvChild *backing = bs->backing; 2713c0f4ce77Saliguori 2714760e0063SKevin Wolf if (backing && backing->bs->encrypted && !backing->bs->valid_key) { 2715c0f4ce77Saliguori return 1; 2716760e0063SKevin Wolf } 2717c0f4ce77Saliguori return (bs->encrypted && !bs->valid_key); 2718c0f4ce77Saliguori } 2719c0f4ce77Saliguori 2720ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key) 2721ea2384d3Sbellard { 2722ea2384d3Sbellard int ret; 2723760e0063SKevin Wolf if (bs->backing && bs->backing->bs->encrypted) { 2724760e0063SKevin Wolf ret = bdrv_set_key(bs->backing->bs, key); 2725ea2384d3Sbellard if (ret < 0) 2726ea2384d3Sbellard return ret; 2727ea2384d3Sbellard if (!bs->encrypted) 2728ea2384d3Sbellard return 0; 2729ea2384d3Sbellard } 2730fd04a2aeSShahar Havivi if (!bs->encrypted) { 2731fd04a2aeSShahar Havivi return -EINVAL; 2732fd04a2aeSShahar Havivi } else if (!bs->drv || !bs->drv->bdrv_set_key) { 2733fd04a2aeSShahar Havivi return -ENOMEDIUM; 2734fd04a2aeSShahar Havivi } 2735c0f4ce77Saliguori ret = bs->drv->bdrv_set_key(bs, key); 2736bb5fc20fSaliguori if (ret < 0) { 2737bb5fc20fSaliguori bs->valid_key = 0; 2738bb5fc20fSaliguori } else if (!bs->valid_key) { 2739bb5fc20fSaliguori bs->valid_key = 1; 2740a7f53e26SMarkus Armbruster if (bs->blk) { 2741bb5fc20fSaliguori /* call the change callback now, we skipped it on open */ 2742a7f53e26SMarkus Armbruster blk_dev_change_media_cb(bs->blk, true); 2743a7f53e26SMarkus Armbruster } 2744bb5fc20fSaliguori } 2745c0f4ce77Saliguori return ret; 2746ea2384d3Sbellard } 2747ea2384d3Sbellard 27484d2855a3SMarkus Armbruster /* 27494d2855a3SMarkus Armbruster * Provide an encryption key for @bs. 27504d2855a3SMarkus Armbruster * If @key is non-null: 27514d2855a3SMarkus Armbruster * If @bs is not encrypted, fail. 27524d2855a3SMarkus Armbruster * Else if the key is invalid, fail. 27534d2855a3SMarkus Armbruster * Else set @bs's key to @key, replacing the existing key, if any. 27544d2855a3SMarkus Armbruster * If @key is null: 27554d2855a3SMarkus Armbruster * If @bs is encrypted and still lacks a key, fail. 27564d2855a3SMarkus Armbruster * Else do nothing. 27574d2855a3SMarkus Armbruster * On failure, store an error object through @errp if non-null. 27584d2855a3SMarkus Armbruster */ 27594d2855a3SMarkus Armbruster void bdrv_add_key(BlockDriverState *bs, const char *key, Error **errp) 27604d2855a3SMarkus Armbruster { 27614d2855a3SMarkus Armbruster if (key) { 27624d2855a3SMarkus Armbruster if (!bdrv_is_encrypted(bs)) { 276381e5f78aSAlberto Garcia error_setg(errp, "Node '%s' is not encrypted", 276481e5f78aSAlberto Garcia bdrv_get_device_or_node_name(bs)); 27654d2855a3SMarkus Armbruster } else if (bdrv_set_key(bs, key) < 0) { 2766c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PASSWORD); 27674d2855a3SMarkus Armbruster } 27684d2855a3SMarkus Armbruster } else { 27694d2855a3SMarkus Armbruster if (bdrv_key_required(bs)) { 2770b1ca6391SMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_ENCRYPTED, 2771b1ca6391SMarkus Armbruster "'%s' (%s) is encrypted", 277281e5f78aSAlberto Garcia bdrv_get_device_or_node_name(bs), 27734d2855a3SMarkus Armbruster bdrv_get_encrypted_filename(bs)); 27744d2855a3SMarkus Armbruster } 27754d2855a3SMarkus Armbruster } 27764d2855a3SMarkus Armbruster } 27774d2855a3SMarkus Armbruster 2778f8d6bba1SMarkus Armbruster const char *bdrv_get_format_name(BlockDriverState *bs) 2779ea2384d3Sbellard { 2780f8d6bba1SMarkus Armbruster return bs->drv ? bs->drv->format_name : NULL; 2781ea2384d3Sbellard } 2782ea2384d3Sbellard 2783ada42401SStefan Hajnoczi static int qsort_strcmp(const void *a, const void *b) 2784ada42401SStefan Hajnoczi { 2785ada42401SStefan Hajnoczi return strcmp(a, b); 2786ada42401SStefan Hajnoczi } 2787ada42401SStefan Hajnoczi 2788ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name), 2789ea2384d3Sbellard void *opaque) 2790ea2384d3Sbellard { 2791ea2384d3Sbellard BlockDriver *drv; 2792e855e4fbSJeff Cody int count = 0; 2793ada42401SStefan Hajnoczi int i; 2794e855e4fbSJeff Cody const char **formats = NULL; 2795ea2384d3Sbellard 27968a22f02aSStefan Hajnoczi QLIST_FOREACH(drv, &bdrv_drivers, list) { 2797e855e4fbSJeff Cody if (drv->format_name) { 2798e855e4fbSJeff Cody bool found = false; 2799e855e4fbSJeff Cody int i = count; 2800e855e4fbSJeff Cody while (formats && i && !found) { 2801e855e4fbSJeff Cody found = !strcmp(formats[--i], drv->format_name); 2802e855e4fbSJeff Cody } 2803e855e4fbSJeff Cody 2804e855e4fbSJeff Cody if (!found) { 28055839e53bSMarkus Armbruster formats = g_renew(const char *, formats, count + 1); 2806e855e4fbSJeff Cody formats[count++] = drv->format_name; 2807ea2384d3Sbellard } 2808ea2384d3Sbellard } 2809e855e4fbSJeff Cody } 2810ada42401SStefan Hajnoczi 2811ada42401SStefan Hajnoczi qsort(formats, count, sizeof(formats[0]), qsort_strcmp); 2812ada42401SStefan Hajnoczi 2813ada42401SStefan Hajnoczi for (i = 0; i < count; i++) { 2814ada42401SStefan Hajnoczi it(opaque, formats[i]); 2815ada42401SStefan Hajnoczi } 2816ada42401SStefan Hajnoczi 2817e855e4fbSJeff Cody g_free(formats); 2818e855e4fbSJeff Cody } 2819ea2384d3Sbellard 2820dc364f4cSBenoît Canet /* This function is to find a node in the bs graph */ 2821dc364f4cSBenoît Canet BlockDriverState *bdrv_find_node(const char *node_name) 2822dc364f4cSBenoît Canet { 2823dc364f4cSBenoît Canet BlockDriverState *bs; 2824dc364f4cSBenoît Canet 2825dc364f4cSBenoît Canet assert(node_name); 2826dc364f4cSBenoît Canet 2827dc364f4cSBenoît Canet QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) { 2828dc364f4cSBenoît Canet if (!strcmp(node_name, bs->node_name)) { 2829dc364f4cSBenoît Canet return bs; 2830dc364f4cSBenoît Canet } 2831dc364f4cSBenoît Canet } 2832dc364f4cSBenoît Canet return NULL; 2833dc364f4cSBenoît Canet } 2834dc364f4cSBenoît Canet 2835c13163fbSBenoît Canet /* Put this QMP function here so it can access the static graph_bdrv_states. */ 2836d5a8ee60SAlberto Garcia BlockDeviceInfoList *bdrv_named_nodes_list(Error **errp) 2837c13163fbSBenoît Canet { 2838c13163fbSBenoît Canet BlockDeviceInfoList *list, *entry; 2839c13163fbSBenoît Canet BlockDriverState *bs; 2840c13163fbSBenoît Canet 2841c13163fbSBenoît Canet list = NULL; 2842c13163fbSBenoît Canet QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) { 2843c83f9fbaSKevin Wolf BlockDeviceInfo *info = bdrv_block_device_info(NULL, bs, errp); 2844d5a8ee60SAlberto Garcia if (!info) { 2845d5a8ee60SAlberto Garcia qapi_free_BlockDeviceInfoList(list); 2846d5a8ee60SAlberto Garcia return NULL; 2847d5a8ee60SAlberto Garcia } 2848c13163fbSBenoît Canet entry = g_malloc0(sizeof(*entry)); 2849d5a8ee60SAlberto Garcia entry->value = info; 2850c13163fbSBenoît Canet entry->next = list; 2851c13163fbSBenoît Canet list = entry; 2852c13163fbSBenoît Canet } 2853c13163fbSBenoît Canet 2854c13163fbSBenoît Canet return list; 2855c13163fbSBenoît Canet } 2856c13163fbSBenoît Canet 285712d3ba82SBenoît Canet BlockDriverState *bdrv_lookup_bs(const char *device, 285812d3ba82SBenoît Canet const char *node_name, 285912d3ba82SBenoît Canet Error **errp) 286012d3ba82SBenoît Canet { 28617f06d47eSMarkus Armbruster BlockBackend *blk; 28627f06d47eSMarkus Armbruster BlockDriverState *bs; 286312d3ba82SBenoît Canet 286412d3ba82SBenoît Canet if (device) { 28657f06d47eSMarkus Armbruster blk = blk_by_name(device); 286612d3ba82SBenoît Canet 28677f06d47eSMarkus Armbruster if (blk) { 28689f4ed6fbSAlberto Garcia bs = blk_bs(blk); 28699f4ed6fbSAlberto Garcia if (!bs) { 28705433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 28715433c24fSMax Reitz } 28725433c24fSMax Reitz 28739f4ed6fbSAlberto Garcia return bs; 287412d3ba82SBenoît Canet } 2875dd67fa50SBenoît Canet } 287612d3ba82SBenoît Canet 2877dd67fa50SBenoît Canet if (node_name) { 287812d3ba82SBenoît Canet bs = bdrv_find_node(node_name); 287912d3ba82SBenoît Canet 2880dd67fa50SBenoît Canet if (bs) { 2881dd67fa50SBenoît Canet return bs; 2882dd67fa50SBenoît Canet } 288312d3ba82SBenoît Canet } 288412d3ba82SBenoît Canet 2885dd67fa50SBenoît Canet error_setg(errp, "Cannot find device=%s nor node_name=%s", 2886dd67fa50SBenoît Canet device ? device : "", 2887dd67fa50SBenoît Canet node_name ? node_name : ""); 2888dd67fa50SBenoît Canet return NULL; 288912d3ba82SBenoît Canet } 289012d3ba82SBenoît Canet 28915a6684d2SJeff Cody /* If 'base' is in the same chain as 'top', return true. Otherwise, 28925a6684d2SJeff Cody * return false. If either argument is NULL, return false. */ 28935a6684d2SJeff Cody bool bdrv_chain_contains(BlockDriverState *top, BlockDriverState *base) 28945a6684d2SJeff Cody { 28955a6684d2SJeff Cody while (top && top != base) { 2896760e0063SKevin Wolf top = backing_bs(top); 28975a6684d2SJeff Cody } 28985a6684d2SJeff Cody 28995a6684d2SJeff Cody return top != NULL; 29005a6684d2SJeff Cody } 29015a6684d2SJeff Cody 290204df765aSFam Zheng BlockDriverState *bdrv_next_node(BlockDriverState *bs) 290304df765aSFam Zheng { 290404df765aSFam Zheng if (!bs) { 290504df765aSFam Zheng return QTAILQ_FIRST(&graph_bdrv_states); 290604df765aSFam Zheng } 290704df765aSFam Zheng return QTAILQ_NEXT(bs, node_list); 290804df765aSFam Zheng } 290904df765aSFam Zheng 291026260580SMax Reitz /* Iterates over all top-level BlockDriverStates, i.e. BDSs that are owned by 291126260580SMax Reitz * the monitor or attached to a BlockBackend */ 29122f399b0aSMarkus Armbruster BlockDriverState *bdrv_next(BlockDriverState *bs) 29132f399b0aSMarkus Armbruster { 291426260580SMax Reitz if (!bs || bs->blk) { 291526260580SMax Reitz bs = blk_next_root_bs(bs); 291626260580SMax Reitz if (bs) { 291726260580SMax Reitz return bs; 29182f399b0aSMarkus Armbruster } 291926260580SMax Reitz } 292026260580SMax Reitz 292126260580SMax Reitz /* Ignore all BDSs that are attached to a BlockBackend here; they have been 292226260580SMax Reitz * handled by the above block already */ 292326260580SMax Reitz do { 292426260580SMax Reitz bs = bdrv_next_monitor_owned(bs); 292526260580SMax Reitz } while (bs && bs->blk); 292626260580SMax Reitz return bs; 29272f399b0aSMarkus Armbruster } 29282f399b0aSMarkus Armbruster 292920a9e77dSFam Zheng const char *bdrv_get_node_name(const BlockDriverState *bs) 293020a9e77dSFam Zheng { 293120a9e77dSFam Zheng return bs->node_name; 293220a9e77dSFam Zheng } 293320a9e77dSFam Zheng 29347f06d47eSMarkus Armbruster /* TODO check what callers really want: bs->node_name or blk_name() */ 2935bfb197e0SMarkus Armbruster const char *bdrv_get_device_name(const BlockDriverState *bs) 2936ea2384d3Sbellard { 2937bfb197e0SMarkus Armbruster return bs->blk ? blk_name(bs->blk) : ""; 2938ea2384d3Sbellard } 2939ea2384d3Sbellard 29409b2aa84fSAlberto Garcia /* This can be used to identify nodes that might not have a device 29419b2aa84fSAlberto Garcia * name associated. Since node and device names live in the same 29429b2aa84fSAlberto Garcia * namespace, the result is unambiguous. The exception is if both are 29439b2aa84fSAlberto Garcia * absent, then this returns an empty (non-null) string. */ 29449b2aa84fSAlberto Garcia const char *bdrv_get_device_or_node_name(const BlockDriverState *bs) 29459b2aa84fSAlberto Garcia { 29469b2aa84fSAlberto Garcia return bs->blk ? blk_name(bs->blk) : bs->node_name; 29479b2aa84fSAlberto Garcia } 29489b2aa84fSAlberto Garcia 2949c8433287SMarkus Armbruster int bdrv_get_flags(BlockDriverState *bs) 2950c8433287SMarkus Armbruster { 2951c8433287SMarkus Armbruster return bs->open_flags; 2952c8433287SMarkus Armbruster } 2953c8433287SMarkus Armbruster 29543ac21627SPeter Lieven int bdrv_has_zero_init_1(BlockDriverState *bs) 29553ac21627SPeter Lieven { 29563ac21627SPeter Lieven return 1; 29573ac21627SPeter Lieven } 29583ac21627SPeter Lieven 2959f2feebbdSKevin Wolf int bdrv_has_zero_init(BlockDriverState *bs) 2960f2feebbdSKevin Wolf { 2961f2feebbdSKevin Wolf assert(bs->drv); 2962f2feebbdSKevin Wolf 296311212d8fSPaolo Bonzini /* If BS is a copy on write image, it is initialized to 296411212d8fSPaolo Bonzini the contents of the base image, which may not be zeroes. */ 2965760e0063SKevin Wolf if (bs->backing) { 296611212d8fSPaolo Bonzini return 0; 296711212d8fSPaolo Bonzini } 2968336c1c12SKevin Wolf if (bs->drv->bdrv_has_zero_init) { 2969336c1c12SKevin Wolf return bs->drv->bdrv_has_zero_init(bs); 2970f2feebbdSKevin Wolf } 2971f2feebbdSKevin Wolf 29723ac21627SPeter Lieven /* safe default */ 29733ac21627SPeter Lieven return 0; 2974f2feebbdSKevin Wolf } 2975f2feebbdSKevin Wolf 29764ce78691SPeter Lieven bool bdrv_unallocated_blocks_are_zero(BlockDriverState *bs) 29774ce78691SPeter Lieven { 29784ce78691SPeter Lieven BlockDriverInfo bdi; 29794ce78691SPeter Lieven 2980760e0063SKevin Wolf if (bs->backing) { 29814ce78691SPeter Lieven return false; 29824ce78691SPeter Lieven } 29834ce78691SPeter Lieven 29844ce78691SPeter Lieven if (bdrv_get_info(bs, &bdi) == 0) { 29854ce78691SPeter Lieven return bdi.unallocated_blocks_are_zero; 29864ce78691SPeter Lieven } 29874ce78691SPeter Lieven 29884ce78691SPeter Lieven return false; 29894ce78691SPeter Lieven } 29904ce78691SPeter Lieven 29914ce78691SPeter Lieven bool bdrv_can_write_zeroes_with_unmap(BlockDriverState *bs) 29924ce78691SPeter Lieven { 29934ce78691SPeter Lieven BlockDriverInfo bdi; 29944ce78691SPeter Lieven 2995760e0063SKevin Wolf if (bs->backing || !(bs->open_flags & BDRV_O_UNMAP)) { 29964ce78691SPeter Lieven return false; 29974ce78691SPeter Lieven } 29984ce78691SPeter Lieven 29994ce78691SPeter Lieven if (bdrv_get_info(bs, &bdi) == 0) { 30004ce78691SPeter Lieven return bdi.can_write_zeroes_with_unmap; 30014ce78691SPeter Lieven } 30024ce78691SPeter Lieven 30034ce78691SPeter Lieven return false; 30044ce78691SPeter Lieven } 30054ce78691SPeter Lieven 3006045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs) 3007045df330Saliguori { 3008760e0063SKevin Wolf if (bs->backing && bs->backing->bs->encrypted) 3009045df330Saliguori return bs->backing_file; 3010045df330Saliguori else if (bs->encrypted) 3011045df330Saliguori return bs->filename; 3012045df330Saliguori else 3013045df330Saliguori return NULL; 3014045df330Saliguori } 3015045df330Saliguori 301683f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs, 301783f64091Sbellard char *filename, int filename_size) 301883f64091Sbellard { 301983f64091Sbellard pstrcpy(filename, filename_size, bs->backing_file); 302083f64091Sbellard } 302183f64091Sbellard 3022faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 3023faea38e7Sbellard { 3024faea38e7Sbellard BlockDriver *drv = bs->drv; 3025faea38e7Sbellard if (!drv) 302619cb3738Sbellard return -ENOMEDIUM; 3027faea38e7Sbellard if (!drv->bdrv_get_info) 3028faea38e7Sbellard return -ENOTSUP; 3029faea38e7Sbellard memset(bdi, 0, sizeof(*bdi)); 3030faea38e7Sbellard return drv->bdrv_get_info(bs, bdi); 3031faea38e7Sbellard } 3032faea38e7Sbellard 3033eae041feSMax Reitz ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs) 3034eae041feSMax Reitz { 3035eae041feSMax Reitz BlockDriver *drv = bs->drv; 3036eae041feSMax Reitz if (drv && drv->bdrv_get_specific_info) { 3037eae041feSMax Reitz return drv->bdrv_get_specific_info(bs); 3038eae041feSMax Reitz } 3039eae041feSMax Reitz return NULL; 3040eae041feSMax Reitz } 3041eae041feSMax Reitz 3042a31939e6SEric Blake void bdrv_debug_event(BlockDriverState *bs, BlkdebugEvent event) 30438b9b0cc2SKevin Wolf { 3044bf736fe3SKevin Wolf if (!bs || !bs->drv || !bs->drv->bdrv_debug_event) { 30458b9b0cc2SKevin Wolf return; 30468b9b0cc2SKevin Wolf } 30478b9b0cc2SKevin Wolf 3048bf736fe3SKevin Wolf bs->drv->bdrv_debug_event(bs, event); 304941c695c7SKevin Wolf } 30508b9b0cc2SKevin Wolf 305141c695c7SKevin Wolf int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event, 305241c695c7SKevin Wolf const char *tag) 305341c695c7SKevin Wolf { 305441c695c7SKevin Wolf while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) { 30559a4f4c31SKevin Wolf bs = bs->file ? bs->file->bs : NULL; 305641c695c7SKevin Wolf } 305741c695c7SKevin Wolf 305841c695c7SKevin Wolf if (bs && bs->drv && bs->drv->bdrv_debug_breakpoint) { 305941c695c7SKevin Wolf return bs->drv->bdrv_debug_breakpoint(bs, event, tag); 306041c695c7SKevin Wolf } 306141c695c7SKevin Wolf 306241c695c7SKevin Wolf return -ENOTSUP; 306341c695c7SKevin Wolf } 306441c695c7SKevin Wolf 30654cc70e93SFam Zheng int bdrv_debug_remove_breakpoint(BlockDriverState *bs, const char *tag) 30664cc70e93SFam Zheng { 30674cc70e93SFam Zheng while (bs && bs->drv && !bs->drv->bdrv_debug_remove_breakpoint) { 30689a4f4c31SKevin Wolf bs = bs->file ? bs->file->bs : NULL; 30694cc70e93SFam Zheng } 30704cc70e93SFam Zheng 30714cc70e93SFam Zheng if (bs && bs->drv && bs->drv->bdrv_debug_remove_breakpoint) { 30724cc70e93SFam Zheng return bs->drv->bdrv_debug_remove_breakpoint(bs, tag); 30734cc70e93SFam Zheng } 30744cc70e93SFam Zheng 30754cc70e93SFam Zheng return -ENOTSUP; 30764cc70e93SFam Zheng } 30774cc70e93SFam Zheng 307841c695c7SKevin Wolf int bdrv_debug_resume(BlockDriverState *bs, const char *tag) 307941c695c7SKevin Wolf { 3080938789eaSMax Reitz while (bs && (!bs->drv || !bs->drv->bdrv_debug_resume)) { 30819a4f4c31SKevin Wolf bs = bs->file ? bs->file->bs : NULL; 308241c695c7SKevin Wolf } 308341c695c7SKevin Wolf 308441c695c7SKevin Wolf if (bs && bs->drv && bs->drv->bdrv_debug_resume) { 308541c695c7SKevin Wolf return bs->drv->bdrv_debug_resume(bs, tag); 308641c695c7SKevin Wolf } 308741c695c7SKevin Wolf 308841c695c7SKevin Wolf return -ENOTSUP; 308941c695c7SKevin Wolf } 309041c695c7SKevin Wolf 309141c695c7SKevin Wolf bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag) 309241c695c7SKevin Wolf { 309341c695c7SKevin Wolf while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) { 30949a4f4c31SKevin Wolf bs = bs->file ? bs->file->bs : NULL; 309541c695c7SKevin Wolf } 309641c695c7SKevin Wolf 309741c695c7SKevin Wolf if (bs && bs->drv && bs->drv->bdrv_debug_is_suspended) { 309841c695c7SKevin Wolf return bs->drv->bdrv_debug_is_suspended(bs, tag); 309941c695c7SKevin Wolf } 310041c695c7SKevin Wolf 310141c695c7SKevin Wolf return false; 31028b9b0cc2SKevin Wolf } 31038b9b0cc2SKevin Wolf 3104199630b6SBlue Swirl int bdrv_is_snapshot(BlockDriverState *bs) 3105199630b6SBlue Swirl { 3106199630b6SBlue Swirl return !!(bs->open_flags & BDRV_O_SNAPSHOT); 3107199630b6SBlue Swirl } 3108199630b6SBlue Swirl 3109b1b1d783SJeff Cody /* backing_file can either be relative, or absolute, or a protocol. If it is 3110b1b1d783SJeff Cody * relative, it must be relative to the chain. So, passing in bs->filename 3111b1b1d783SJeff Cody * from a BDS as backing_file should not be done, as that may be relative to 3112b1b1d783SJeff Cody * the CWD rather than the chain. */ 3113e8a6bb9cSMarcelo Tosatti BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs, 3114e8a6bb9cSMarcelo Tosatti const char *backing_file) 3115e8a6bb9cSMarcelo Tosatti { 3116b1b1d783SJeff Cody char *filename_full = NULL; 3117b1b1d783SJeff Cody char *backing_file_full = NULL; 3118b1b1d783SJeff Cody char *filename_tmp = NULL; 3119b1b1d783SJeff Cody int is_protocol = 0; 3120b1b1d783SJeff Cody BlockDriverState *curr_bs = NULL; 3121b1b1d783SJeff Cody BlockDriverState *retval = NULL; 3122b1b1d783SJeff Cody 3123b1b1d783SJeff Cody if (!bs || !bs->drv || !backing_file) { 3124e8a6bb9cSMarcelo Tosatti return NULL; 3125e8a6bb9cSMarcelo Tosatti } 3126e8a6bb9cSMarcelo Tosatti 3127b1b1d783SJeff Cody filename_full = g_malloc(PATH_MAX); 3128b1b1d783SJeff Cody backing_file_full = g_malloc(PATH_MAX); 3129b1b1d783SJeff Cody filename_tmp = g_malloc(PATH_MAX); 3130b1b1d783SJeff Cody 3131b1b1d783SJeff Cody is_protocol = path_has_protocol(backing_file); 3132b1b1d783SJeff Cody 3133760e0063SKevin Wolf for (curr_bs = bs; curr_bs->backing; curr_bs = curr_bs->backing->bs) { 3134b1b1d783SJeff Cody 3135b1b1d783SJeff Cody /* If either of the filename paths is actually a protocol, then 3136b1b1d783SJeff Cody * compare unmodified paths; otherwise make paths relative */ 3137b1b1d783SJeff Cody if (is_protocol || path_has_protocol(curr_bs->backing_file)) { 3138b1b1d783SJeff Cody if (strcmp(backing_file, curr_bs->backing_file) == 0) { 3139760e0063SKevin Wolf retval = curr_bs->backing->bs; 3140b1b1d783SJeff Cody break; 3141b1b1d783SJeff Cody } 3142e8a6bb9cSMarcelo Tosatti } else { 3143b1b1d783SJeff Cody /* If not an absolute filename path, make it relative to the current 3144b1b1d783SJeff Cody * image's filename path */ 3145b1b1d783SJeff Cody path_combine(filename_tmp, PATH_MAX, curr_bs->filename, 3146b1b1d783SJeff Cody backing_file); 3147b1b1d783SJeff Cody 3148b1b1d783SJeff Cody /* We are going to compare absolute pathnames */ 3149b1b1d783SJeff Cody if (!realpath(filename_tmp, filename_full)) { 3150b1b1d783SJeff Cody continue; 3151b1b1d783SJeff Cody } 3152b1b1d783SJeff Cody 3153b1b1d783SJeff Cody /* We need to make sure the backing filename we are comparing against 3154b1b1d783SJeff Cody * is relative to the current image filename (or absolute) */ 3155b1b1d783SJeff Cody path_combine(filename_tmp, PATH_MAX, curr_bs->filename, 3156b1b1d783SJeff Cody curr_bs->backing_file); 3157b1b1d783SJeff Cody 3158b1b1d783SJeff Cody if (!realpath(filename_tmp, backing_file_full)) { 3159b1b1d783SJeff Cody continue; 3160b1b1d783SJeff Cody } 3161b1b1d783SJeff Cody 3162b1b1d783SJeff Cody if (strcmp(backing_file_full, filename_full) == 0) { 3163760e0063SKevin Wolf retval = curr_bs->backing->bs; 3164b1b1d783SJeff Cody break; 3165b1b1d783SJeff Cody } 3166e8a6bb9cSMarcelo Tosatti } 3167e8a6bb9cSMarcelo Tosatti } 3168e8a6bb9cSMarcelo Tosatti 3169b1b1d783SJeff Cody g_free(filename_full); 3170b1b1d783SJeff Cody g_free(backing_file_full); 3171b1b1d783SJeff Cody g_free(filename_tmp); 3172b1b1d783SJeff Cody return retval; 3173e8a6bb9cSMarcelo Tosatti } 3174e8a6bb9cSMarcelo Tosatti 3175f198fd1cSBenoît Canet int bdrv_get_backing_file_depth(BlockDriverState *bs) 3176f198fd1cSBenoît Canet { 3177f198fd1cSBenoît Canet if (!bs->drv) { 3178f198fd1cSBenoît Canet return 0; 3179f198fd1cSBenoît Canet } 3180f198fd1cSBenoît Canet 3181760e0063SKevin Wolf if (!bs->backing) { 3182f198fd1cSBenoît Canet return 0; 3183f198fd1cSBenoît Canet } 3184f198fd1cSBenoît Canet 3185760e0063SKevin Wolf return 1 + bdrv_get_backing_file_depth(bs->backing->bs); 3186f198fd1cSBenoît Canet } 3187f198fd1cSBenoît Canet 3188ea2384d3Sbellard void bdrv_init(void) 3189ea2384d3Sbellard { 31905efa9d5aSAnthony Liguori module_call_init(MODULE_INIT_BLOCK); 3191ea2384d3Sbellard } 3192ce1a14dcSpbrook 3193eb852011SMarkus Armbruster void bdrv_init_with_whitelist(void) 3194eb852011SMarkus Armbruster { 3195eb852011SMarkus Armbruster use_bdrv_whitelist = 1; 3196eb852011SMarkus Armbruster bdrv_init(); 3197eb852011SMarkus Armbruster } 3198eb852011SMarkus Armbruster 31995a8a30dbSKevin Wolf void bdrv_invalidate_cache(BlockDriverState *bs, Error **errp) 32000f15423cSAnthony Liguori { 3201*0d1c5c91SFam Zheng BdrvChild *child; 32025a8a30dbSKevin Wolf Error *local_err = NULL; 32035a8a30dbSKevin Wolf int ret; 32045a8a30dbSKevin Wolf 32053456a8d1SKevin Wolf if (!bs->drv) { 32063456a8d1SKevin Wolf return; 32070f15423cSAnthony Liguori } 32083456a8d1SKevin Wolf 320904c01a5cSKevin Wolf if (!(bs->open_flags & BDRV_O_INACTIVE)) { 32107ea2d269SAlexey Kardashevskiy return; 32117ea2d269SAlexey Kardashevskiy } 321204c01a5cSKevin Wolf bs->open_flags &= ~BDRV_O_INACTIVE; 32137ea2d269SAlexey Kardashevskiy 32143456a8d1SKevin Wolf if (bs->drv->bdrv_invalidate_cache) { 32155a8a30dbSKevin Wolf bs->drv->bdrv_invalidate_cache(bs, &local_err); 32165a8a30dbSKevin Wolf if (local_err) { 321704c01a5cSKevin Wolf bs->open_flags |= BDRV_O_INACTIVE; 32185a8a30dbSKevin Wolf error_propagate(errp, local_err); 32195a8a30dbSKevin Wolf return; 32203456a8d1SKevin Wolf } 3221*0d1c5c91SFam Zheng } 3222*0d1c5c91SFam Zheng 3223*0d1c5c91SFam Zheng QLIST_FOREACH(child, &bs->children, next) { 3224*0d1c5c91SFam Zheng bdrv_invalidate_cache(child->bs, &local_err); 3225*0d1c5c91SFam Zheng if (local_err) { 3226*0d1c5c91SFam Zheng bs->open_flags |= BDRV_O_INACTIVE; 3227*0d1c5c91SFam Zheng error_propagate(errp, local_err); 3228*0d1c5c91SFam Zheng return; 3229*0d1c5c91SFam Zheng } 3230*0d1c5c91SFam Zheng } 32313456a8d1SKevin Wolf 32325a8a30dbSKevin Wolf ret = refresh_total_sectors(bs, bs->total_sectors); 32335a8a30dbSKevin Wolf if (ret < 0) { 323404c01a5cSKevin Wolf bs->open_flags |= BDRV_O_INACTIVE; 32355a8a30dbSKevin Wolf error_setg_errno(errp, -ret, "Could not refresh total sector count"); 32365a8a30dbSKevin Wolf return; 32375a8a30dbSKevin Wolf } 32380f15423cSAnthony Liguori } 32390f15423cSAnthony Liguori 32405a8a30dbSKevin Wolf void bdrv_invalidate_cache_all(Error **errp) 32410f15423cSAnthony Liguori { 324279720af6SMax Reitz BlockDriverState *bs = NULL; 32435a8a30dbSKevin Wolf Error *local_err = NULL; 32440f15423cSAnthony Liguori 324579720af6SMax Reitz while ((bs = bdrv_next(bs)) != NULL) { 3246ed78cda3SStefan Hajnoczi AioContext *aio_context = bdrv_get_aio_context(bs); 3247ed78cda3SStefan Hajnoczi 3248ed78cda3SStefan Hajnoczi aio_context_acquire(aio_context); 32495a8a30dbSKevin Wolf bdrv_invalidate_cache(bs, &local_err); 3250ed78cda3SStefan Hajnoczi aio_context_release(aio_context); 32515a8a30dbSKevin Wolf if (local_err) { 32525a8a30dbSKevin Wolf error_propagate(errp, local_err); 32535a8a30dbSKevin Wolf return; 32545a8a30dbSKevin Wolf } 32550f15423cSAnthony Liguori } 32560f15423cSAnthony Liguori } 32570f15423cSAnthony Liguori 325876b1c7feSKevin Wolf static int bdrv_inactivate(BlockDriverState *bs) 325976b1c7feSKevin Wolf { 326076b1c7feSKevin Wolf int ret; 326176b1c7feSKevin Wolf 326276b1c7feSKevin Wolf if (bs->drv->bdrv_inactivate) { 326376b1c7feSKevin Wolf ret = bs->drv->bdrv_inactivate(bs); 326476b1c7feSKevin Wolf if (ret < 0) { 326576b1c7feSKevin Wolf return ret; 326676b1c7feSKevin Wolf } 326776b1c7feSKevin Wolf } 326876b1c7feSKevin Wolf 326976b1c7feSKevin Wolf bs->open_flags |= BDRV_O_INACTIVE; 327076b1c7feSKevin Wolf return 0; 327176b1c7feSKevin Wolf } 327276b1c7feSKevin Wolf 327376b1c7feSKevin Wolf int bdrv_inactivate_all(void) 327476b1c7feSKevin Wolf { 327579720af6SMax Reitz BlockDriverState *bs = NULL; 327676b1c7feSKevin Wolf int ret; 327776b1c7feSKevin Wolf 327879720af6SMax Reitz while ((bs = bdrv_next(bs)) != NULL) { 327976b1c7feSKevin Wolf AioContext *aio_context = bdrv_get_aio_context(bs); 328076b1c7feSKevin Wolf 328176b1c7feSKevin Wolf aio_context_acquire(aio_context); 328276b1c7feSKevin Wolf ret = bdrv_inactivate(bs); 328376b1c7feSKevin Wolf aio_context_release(aio_context); 328476b1c7feSKevin Wolf if (ret < 0) { 328576b1c7feSKevin Wolf return ret; 328676b1c7feSKevin Wolf } 328776b1c7feSKevin Wolf } 328876b1c7feSKevin Wolf 328976b1c7feSKevin Wolf return 0; 329076b1c7feSKevin Wolf } 329176b1c7feSKevin Wolf 3292f9f05dc5SKevin Wolf /**************************************************************/ 329319cb3738Sbellard /* removable device support */ 329419cb3738Sbellard 329519cb3738Sbellard /** 329619cb3738Sbellard * Return TRUE if the media is present 329719cb3738Sbellard */ 3298e031f750SMax Reitz bool bdrv_is_inserted(BlockDriverState *bs) 329919cb3738Sbellard { 330019cb3738Sbellard BlockDriver *drv = bs->drv; 330128d7a789SMax Reitz BdrvChild *child; 3302a1aff5bfSMarkus Armbruster 3303e031f750SMax Reitz if (!drv) { 3304e031f750SMax Reitz return false; 3305e031f750SMax Reitz } 330628d7a789SMax Reitz if (drv->bdrv_is_inserted) { 3307a1aff5bfSMarkus Armbruster return drv->bdrv_is_inserted(bs); 330819cb3738Sbellard } 330928d7a789SMax Reitz QLIST_FOREACH(child, &bs->children, next) { 331028d7a789SMax Reitz if (!bdrv_is_inserted(child->bs)) { 331128d7a789SMax Reitz return false; 331228d7a789SMax Reitz } 331328d7a789SMax Reitz } 331428d7a789SMax Reitz return true; 331528d7a789SMax Reitz } 331619cb3738Sbellard 331719cb3738Sbellard /** 33188e49ca46SMarkus Armbruster * Return whether the media changed since the last call to this 33198e49ca46SMarkus Armbruster * function, or -ENOTSUP if we don't know. Most drivers don't know. 332019cb3738Sbellard */ 332119cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs) 332219cb3738Sbellard { 332319cb3738Sbellard BlockDriver *drv = bs->drv; 332419cb3738Sbellard 33258e49ca46SMarkus Armbruster if (drv && drv->bdrv_media_changed) { 33268e49ca46SMarkus Armbruster return drv->bdrv_media_changed(bs); 33278e49ca46SMarkus Armbruster } 33288e49ca46SMarkus Armbruster return -ENOTSUP; 332919cb3738Sbellard } 333019cb3738Sbellard 333119cb3738Sbellard /** 333219cb3738Sbellard * If eject_flag is TRUE, eject the media. Otherwise, close the tray 333319cb3738Sbellard */ 3334f36f3949SLuiz Capitulino void bdrv_eject(BlockDriverState *bs, bool eject_flag) 333519cb3738Sbellard { 333619cb3738Sbellard BlockDriver *drv = bs->drv; 3337bfb197e0SMarkus Armbruster const char *device_name; 333819cb3738Sbellard 3339822e1cd1SMarkus Armbruster if (drv && drv->bdrv_eject) { 3340822e1cd1SMarkus Armbruster drv->bdrv_eject(bs, eject_flag); 334119cb3738Sbellard } 33426f382ed2SLuiz Capitulino 3343bfb197e0SMarkus Armbruster device_name = bdrv_get_device_name(bs); 3344bfb197e0SMarkus Armbruster if (device_name[0] != '\0') { 3345bfb197e0SMarkus Armbruster qapi_event_send_device_tray_moved(device_name, 3346a5ee7bd4SWenchao Xia eject_flag, &error_abort); 33476f382ed2SLuiz Capitulino } 334819cb3738Sbellard } 334919cb3738Sbellard 335019cb3738Sbellard /** 335119cb3738Sbellard * Lock or unlock the media (if it is locked, the user won't be able 335219cb3738Sbellard * to eject it manually). 335319cb3738Sbellard */ 3354025e849aSMarkus Armbruster void bdrv_lock_medium(BlockDriverState *bs, bool locked) 335519cb3738Sbellard { 335619cb3738Sbellard BlockDriver *drv = bs->drv; 335719cb3738Sbellard 3358025e849aSMarkus Armbruster trace_bdrv_lock_medium(bs, locked); 3359b8c6d095SStefan Hajnoczi 3360025e849aSMarkus Armbruster if (drv && drv->bdrv_lock_medium) { 3361025e849aSMarkus Armbruster drv->bdrv_lock_medium(bs, locked); 336219cb3738Sbellard } 336319cb3738Sbellard } 3364985a03b0Sths 33659fcb0251SFam Zheng /* Get a reference to bs */ 33669fcb0251SFam Zheng void bdrv_ref(BlockDriverState *bs) 33679fcb0251SFam Zheng { 33689fcb0251SFam Zheng bs->refcnt++; 33699fcb0251SFam Zheng } 33709fcb0251SFam Zheng 33719fcb0251SFam Zheng /* Release a previously grabbed reference to bs. 33729fcb0251SFam Zheng * If after releasing, reference count is zero, the BlockDriverState is 33739fcb0251SFam Zheng * deleted. */ 33749fcb0251SFam Zheng void bdrv_unref(BlockDriverState *bs) 33759fcb0251SFam Zheng { 33769a4d5ca6SJeff Cody if (!bs) { 33779a4d5ca6SJeff Cody return; 33789a4d5ca6SJeff Cody } 33799fcb0251SFam Zheng assert(bs->refcnt > 0); 33809fcb0251SFam Zheng if (--bs->refcnt == 0) { 33819fcb0251SFam Zheng bdrv_delete(bs); 33829fcb0251SFam Zheng } 33839fcb0251SFam Zheng } 33849fcb0251SFam Zheng 3385fbe40ff7SFam Zheng struct BdrvOpBlocker { 3386fbe40ff7SFam Zheng Error *reason; 3387fbe40ff7SFam Zheng QLIST_ENTRY(BdrvOpBlocker) list; 3388fbe40ff7SFam Zheng }; 3389fbe40ff7SFam Zheng 3390fbe40ff7SFam Zheng bool bdrv_op_is_blocked(BlockDriverState *bs, BlockOpType op, Error **errp) 3391fbe40ff7SFam Zheng { 3392fbe40ff7SFam Zheng BdrvOpBlocker *blocker; 3393fbe40ff7SFam Zheng assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX); 3394fbe40ff7SFam Zheng if (!QLIST_EMPTY(&bs->op_blockers[op])) { 3395fbe40ff7SFam Zheng blocker = QLIST_FIRST(&bs->op_blockers[op]); 3396fbe40ff7SFam Zheng if (errp) { 3397e43bfd9cSMarkus Armbruster *errp = error_copy(blocker->reason); 3398e43bfd9cSMarkus Armbruster error_prepend(errp, "Node '%s' is busy: ", 3399e43bfd9cSMarkus Armbruster bdrv_get_device_or_node_name(bs)); 3400fbe40ff7SFam Zheng } 3401fbe40ff7SFam Zheng return true; 3402fbe40ff7SFam Zheng } 3403fbe40ff7SFam Zheng return false; 3404fbe40ff7SFam Zheng } 3405fbe40ff7SFam Zheng 3406fbe40ff7SFam Zheng void bdrv_op_block(BlockDriverState *bs, BlockOpType op, Error *reason) 3407fbe40ff7SFam Zheng { 3408fbe40ff7SFam Zheng BdrvOpBlocker *blocker; 3409fbe40ff7SFam Zheng assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX); 3410fbe40ff7SFam Zheng 34115839e53bSMarkus Armbruster blocker = g_new0(BdrvOpBlocker, 1); 3412fbe40ff7SFam Zheng blocker->reason = reason; 3413fbe40ff7SFam Zheng QLIST_INSERT_HEAD(&bs->op_blockers[op], blocker, list); 3414fbe40ff7SFam Zheng } 3415fbe40ff7SFam Zheng 3416fbe40ff7SFam Zheng void bdrv_op_unblock(BlockDriverState *bs, BlockOpType op, Error *reason) 3417fbe40ff7SFam Zheng { 3418fbe40ff7SFam Zheng BdrvOpBlocker *blocker, *next; 3419fbe40ff7SFam Zheng assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX); 3420fbe40ff7SFam Zheng QLIST_FOREACH_SAFE(blocker, &bs->op_blockers[op], list, next) { 3421fbe40ff7SFam Zheng if (blocker->reason == reason) { 3422fbe40ff7SFam Zheng QLIST_REMOVE(blocker, list); 3423fbe40ff7SFam Zheng g_free(blocker); 3424fbe40ff7SFam Zheng } 3425fbe40ff7SFam Zheng } 3426fbe40ff7SFam Zheng } 3427fbe40ff7SFam Zheng 3428fbe40ff7SFam Zheng void bdrv_op_block_all(BlockDriverState *bs, Error *reason) 3429fbe40ff7SFam Zheng { 3430fbe40ff7SFam Zheng int i; 3431fbe40ff7SFam Zheng for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) { 3432fbe40ff7SFam Zheng bdrv_op_block(bs, i, reason); 3433fbe40ff7SFam Zheng } 3434fbe40ff7SFam Zheng } 3435fbe40ff7SFam Zheng 3436fbe40ff7SFam Zheng void bdrv_op_unblock_all(BlockDriverState *bs, Error *reason) 3437fbe40ff7SFam Zheng { 3438fbe40ff7SFam Zheng int i; 3439fbe40ff7SFam Zheng for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) { 3440fbe40ff7SFam Zheng bdrv_op_unblock(bs, i, reason); 3441fbe40ff7SFam Zheng } 3442fbe40ff7SFam Zheng } 3443fbe40ff7SFam Zheng 3444fbe40ff7SFam Zheng bool bdrv_op_blocker_is_empty(BlockDriverState *bs) 3445fbe40ff7SFam Zheng { 3446fbe40ff7SFam Zheng int i; 3447fbe40ff7SFam Zheng 3448fbe40ff7SFam Zheng for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) { 3449fbe40ff7SFam Zheng if (!QLIST_EMPTY(&bs->op_blockers[i])) { 3450fbe40ff7SFam Zheng return false; 3451fbe40ff7SFam Zheng } 3452fbe40ff7SFam Zheng } 3453fbe40ff7SFam Zheng return true; 3454fbe40ff7SFam Zheng } 3455fbe40ff7SFam Zheng 3456d92ada22SLuiz Capitulino void bdrv_img_create(const char *filename, const char *fmt, 3457f88e1a42SJes Sorensen const char *base_filename, const char *base_fmt, 3458f382d43aSMiroslav Rezanina char *options, uint64_t img_size, int flags, 3459f382d43aSMiroslav Rezanina Error **errp, bool quiet) 3460f88e1a42SJes Sorensen { 346183d0521aSChunyan Liu QemuOptsList *create_opts = NULL; 346283d0521aSChunyan Liu QemuOpts *opts = NULL; 346383d0521aSChunyan Liu const char *backing_fmt, *backing_file; 346483d0521aSChunyan Liu int64_t size; 3465f88e1a42SJes Sorensen BlockDriver *drv, *proto_drv; 3466cc84d90fSMax Reitz Error *local_err = NULL; 3467f88e1a42SJes Sorensen int ret = 0; 3468f88e1a42SJes Sorensen 3469f88e1a42SJes Sorensen /* Find driver and parse its options */ 3470f88e1a42SJes Sorensen drv = bdrv_find_format(fmt); 3471f88e1a42SJes Sorensen if (!drv) { 347271c79813SLuiz Capitulino error_setg(errp, "Unknown file format '%s'", fmt); 3473d92ada22SLuiz Capitulino return; 3474f88e1a42SJes Sorensen } 3475f88e1a42SJes Sorensen 3476b65a5e12SMax Reitz proto_drv = bdrv_find_protocol(filename, true, errp); 3477f88e1a42SJes Sorensen if (!proto_drv) { 3478d92ada22SLuiz Capitulino return; 3479f88e1a42SJes Sorensen } 3480f88e1a42SJes Sorensen 3481c6149724SMax Reitz if (!drv->create_opts) { 3482c6149724SMax Reitz error_setg(errp, "Format driver '%s' does not support image creation", 3483c6149724SMax Reitz drv->format_name); 3484c6149724SMax Reitz return; 3485c6149724SMax Reitz } 3486c6149724SMax Reitz 3487c6149724SMax Reitz if (!proto_drv->create_opts) { 3488c6149724SMax Reitz error_setg(errp, "Protocol driver '%s' does not support image creation", 3489c6149724SMax Reitz proto_drv->format_name); 3490c6149724SMax Reitz return; 3491c6149724SMax Reitz } 3492c6149724SMax Reitz 3493c282e1fdSChunyan Liu create_opts = qemu_opts_append(create_opts, drv->create_opts); 3494c282e1fdSChunyan Liu create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); 3495f88e1a42SJes Sorensen 3496f88e1a42SJes Sorensen /* Create parameter list with default values */ 349783d0521aSChunyan Liu opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); 349839101f25SMarkus Armbruster qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort); 3499f88e1a42SJes Sorensen 3500f88e1a42SJes Sorensen /* Parse -o options */ 3501f88e1a42SJes Sorensen if (options) { 3502dc523cd3SMarkus Armbruster qemu_opts_do_parse(opts, options, NULL, &local_err); 3503dc523cd3SMarkus Armbruster if (local_err) { 3504dc523cd3SMarkus Armbruster error_report_err(local_err); 3505dc523cd3SMarkus Armbruster local_err = NULL; 350683d0521aSChunyan Liu error_setg(errp, "Invalid options for file format '%s'", fmt); 3507f88e1a42SJes Sorensen goto out; 3508f88e1a42SJes Sorensen } 3509f88e1a42SJes Sorensen } 3510f88e1a42SJes Sorensen 3511f88e1a42SJes Sorensen if (base_filename) { 3512f43e47dbSMarkus Armbruster qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &local_err); 35136be4194bSMarkus Armbruster if (local_err) { 351471c79813SLuiz Capitulino error_setg(errp, "Backing file not supported for file format '%s'", 351571c79813SLuiz Capitulino fmt); 3516f88e1a42SJes Sorensen goto out; 3517f88e1a42SJes Sorensen } 3518f88e1a42SJes Sorensen } 3519f88e1a42SJes Sorensen 3520f88e1a42SJes Sorensen if (base_fmt) { 3521f43e47dbSMarkus Armbruster qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &local_err); 35226be4194bSMarkus Armbruster if (local_err) { 352371c79813SLuiz Capitulino error_setg(errp, "Backing file format not supported for file " 352471c79813SLuiz Capitulino "format '%s'", fmt); 3525f88e1a42SJes Sorensen goto out; 3526f88e1a42SJes Sorensen } 3527f88e1a42SJes Sorensen } 3528f88e1a42SJes Sorensen 352983d0521aSChunyan Liu backing_file = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE); 353083d0521aSChunyan Liu if (backing_file) { 353183d0521aSChunyan Liu if (!strcmp(filename, backing_file)) { 353271c79813SLuiz Capitulino error_setg(errp, "Error: Trying to create an image with the " 353371c79813SLuiz Capitulino "same filename as the backing file"); 3534792da93aSJes Sorensen goto out; 3535792da93aSJes Sorensen } 3536792da93aSJes Sorensen } 3537792da93aSJes Sorensen 353883d0521aSChunyan Liu backing_fmt = qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT); 3539f88e1a42SJes Sorensen 3540f88e1a42SJes Sorensen // The size for the image must always be specified, with one exception: 3541f88e1a42SJes Sorensen // If we are using a backing file, we can obtain the size from there 354283d0521aSChunyan Liu size = qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 0); 354383d0521aSChunyan Liu if (size == -1) { 354483d0521aSChunyan Liu if (backing_file) { 354566f6b814SMax Reitz BlockDriverState *bs; 354629168018SMax Reitz char *full_backing = g_new0(char, PATH_MAX); 354752bf1e72SMarkus Armbruster int64_t size; 354863090dacSPaolo Bonzini int back_flags; 3549e6641719SMax Reitz QDict *backing_options = NULL; 355063090dacSPaolo Bonzini 355129168018SMax Reitz bdrv_get_full_backing_filename_from_filename(filename, backing_file, 355229168018SMax Reitz full_backing, PATH_MAX, 355329168018SMax Reitz &local_err); 355429168018SMax Reitz if (local_err) { 355529168018SMax Reitz g_free(full_backing); 355629168018SMax Reitz goto out; 355729168018SMax Reitz } 355829168018SMax Reitz 355963090dacSPaolo Bonzini /* backing files always opened read-only */ 356061de4c68SKevin Wolf back_flags = flags; 3561bfd18d1eSKevin Wolf back_flags &= ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 3562f88e1a42SJes Sorensen 3563e6641719SMax Reitz if (backing_fmt) { 3564e6641719SMax Reitz backing_options = qdict_new(); 3565e6641719SMax Reitz qdict_put(backing_options, "driver", 3566e6641719SMax Reitz qstring_from_str(backing_fmt)); 3567e6641719SMax Reitz } 3568e6641719SMax Reitz 3569f67503e5SMax Reitz bs = NULL; 3570e6641719SMax Reitz ret = bdrv_open(&bs, full_backing, NULL, backing_options, 35716ebf9aa2SMax Reitz back_flags, &local_err); 357229168018SMax Reitz g_free(full_backing); 3573f88e1a42SJes Sorensen if (ret < 0) { 3574f88e1a42SJes Sorensen goto out; 3575f88e1a42SJes Sorensen } 357652bf1e72SMarkus Armbruster size = bdrv_getlength(bs); 357752bf1e72SMarkus Armbruster if (size < 0) { 357852bf1e72SMarkus Armbruster error_setg_errno(errp, -size, "Could not get size of '%s'", 357952bf1e72SMarkus Armbruster backing_file); 358052bf1e72SMarkus Armbruster bdrv_unref(bs); 358152bf1e72SMarkus Armbruster goto out; 358252bf1e72SMarkus Armbruster } 3583f88e1a42SJes Sorensen 358439101f25SMarkus Armbruster qemu_opt_set_number(opts, BLOCK_OPT_SIZE, size, &error_abort); 358566f6b814SMax Reitz 358666f6b814SMax Reitz bdrv_unref(bs); 3587f88e1a42SJes Sorensen } else { 358871c79813SLuiz Capitulino error_setg(errp, "Image creation needs a size parameter"); 3589f88e1a42SJes Sorensen goto out; 3590f88e1a42SJes Sorensen } 3591f88e1a42SJes Sorensen } 3592f88e1a42SJes Sorensen 3593f382d43aSMiroslav Rezanina if (!quiet) { 3594f88e1a42SJes Sorensen printf("Formatting '%s', fmt=%s ", filename, fmt); 359543c5d8f8SFam Zheng qemu_opts_print(opts, " "); 3596f88e1a42SJes Sorensen puts(""); 3597f382d43aSMiroslav Rezanina } 359883d0521aSChunyan Liu 3599c282e1fdSChunyan Liu ret = bdrv_create(drv, filename, opts, &local_err); 360083d0521aSChunyan Liu 3601cc84d90fSMax Reitz if (ret == -EFBIG) { 3602cc84d90fSMax Reitz /* This is generally a better message than whatever the driver would 3603cc84d90fSMax Reitz * deliver (especially because of the cluster_size_hint), since that 3604cc84d90fSMax Reitz * is most probably not much different from "image too large". */ 3605f3f4d2c0SKevin Wolf const char *cluster_size_hint = ""; 360683d0521aSChunyan Liu if (qemu_opt_get_size(opts, BLOCK_OPT_CLUSTER_SIZE, 0)) { 3607f3f4d2c0SKevin Wolf cluster_size_hint = " (try using a larger cluster size)"; 3608f3f4d2c0SKevin Wolf } 3609cc84d90fSMax Reitz error_setg(errp, "The image size is too large for file format '%s'" 3610cc84d90fSMax Reitz "%s", fmt, cluster_size_hint); 3611cc84d90fSMax Reitz error_free(local_err); 3612cc84d90fSMax Reitz local_err = NULL; 3613f88e1a42SJes Sorensen } 3614f88e1a42SJes Sorensen 3615f88e1a42SJes Sorensen out: 361683d0521aSChunyan Liu qemu_opts_del(opts); 361783d0521aSChunyan Liu qemu_opts_free(create_opts); 361884d18f06SMarkus Armbruster if (local_err) { 3619cc84d90fSMax Reitz error_propagate(errp, local_err); 3620cc84d90fSMax Reitz } 3621f88e1a42SJes Sorensen } 362285d126f3SStefan Hajnoczi 362385d126f3SStefan Hajnoczi AioContext *bdrv_get_aio_context(BlockDriverState *bs) 362485d126f3SStefan Hajnoczi { 3625dcd04228SStefan Hajnoczi return bs->aio_context; 3626dcd04228SStefan Hajnoczi } 3627dcd04228SStefan Hajnoczi 3628dcd04228SStefan Hajnoczi void bdrv_detach_aio_context(BlockDriverState *bs) 3629dcd04228SStefan Hajnoczi { 363033384421SMax Reitz BdrvAioNotifier *baf; 363133384421SMax Reitz 3632dcd04228SStefan Hajnoczi if (!bs->drv) { 3633dcd04228SStefan Hajnoczi return; 3634dcd04228SStefan Hajnoczi } 3635dcd04228SStefan Hajnoczi 363633384421SMax Reitz QLIST_FOREACH(baf, &bs->aio_notifiers, list) { 363733384421SMax Reitz baf->detach_aio_context(baf->opaque); 363833384421SMax Reitz } 363933384421SMax Reitz 3640a0d64a61SAlberto Garcia if (bs->throttle_state) { 36410e5b0a2dSBenoît Canet throttle_timers_detach_aio_context(&bs->throttle_timers); 364213af91ebSStefan Hajnoczi } 3643dcd04228SStefan Hajnoczi if (bs->drv->bdrv_detach_aio_context) { 3644dcd04228SStefan Hajnoczi bs->drv->bdrv_detach_aio_context(bs); 3645dcd04228SStefan Hajnoczi } 3646dcd04228SStefan Hajnoczi if (bs->file) { 36479a4f4c31SKevin Wolf bdrv_detach_aio_context(bs->file->bs); 3648dcd04228SStefan Hajnoczi } 3649760e0063SKevin Wolf if (bs->backing) { 3650760e0063SKevin Wolf bdrv_detach_aio_context(bs->backing->bs); 3651dcd04228SStefan Hajnoczi } 3652dcd04228SStefan Hajnoczi 3653dcd04228SStefan Hajnoczi bs->aio_context = NULL; 3654dcd04228SStefan Hajnoczi } 3655dcd04228SStefan Hajnoczi 3656dcd04228SStefan Hajnoczi void bdrv_attach_aio_context(BlockDriverState *bs, 3657dcd04228SStefan Hajnoczi AioContext *new_context) 3658dcd04228SStefan Hajnoczi { 365933384421SMax Reitz BdrvAioNotifier *ban; 366033384421SMax Reitz 3661dcd04228SStefan Hajnoczi if (!bs->drv) { 3662dcd04228SStefan Hajnoczi return; 3663dcd04228SStefan Hajnoczi } 3664dcd04228SStefan Hajnoczi 3665dcd04228SStefan Hajnoczi bs->aio_context = new_context; 3666dcd04228SStefan Hajnoczi 3667760e0063SKevin Wolf if (bs->backing) { 3668760e0063SKevin Wolf bdrv_attach_aio_context(bs->backing->bs, new_context); 3669dcd04228SStefan Hajnoczi } 3670dcd04228SStefan Hajnoczi if (bs->file) { 36719a4f4c31SKevin Wolf bdrv_attach_aio_context(bs->file->bs, new_context); 3672dcd04228SStefan Hajnoczi } 3673dcd04228SStefan Hajnoczi if (bs->drv->bdrv_attach_aio_context) { 3674dcd04228SStefan Hajnoczi bs->drv->bdrv_attach_aio_context(bs, new_context); 3675dcd04228SStefan Hajnoczi } 3676a0d64a61SAlberto Garcia if (bs->throttle_state) { 36770e5b0a2dSBenoît Canet throttle_timers_attach_aio_context(&bs->throttle_timers, new_context); 367813af91ebSStefan Hajnoczi } 367933384421SMax Reitz 368033384421SMax Reitz QLIST_FOREACH(ban, &bs->aio_notifiers, list) { 368133384421SMax Reitz ban->attached_aio_context(new_context, ban->opaque); 368233384421SMax Reitz } 3683dcd04228SStefan Hajnoczi } 3684dcd04228SStefan Hajnoczi 3685dcd04228SStefan Hajnoczi void bdrv_set_aio_context(BlockDriverState *bs, AioContext *new_context) 3686dcd04228SStefan Hajnoczi { 368753ec73e2SFam Zheng bdrv_drain(bs); /* ensure there are no in-flight requests */ 3688dcd04228SStefan Hajnoczi 3689dcd04228SStefan Hajnoczi bdrv_detach_aio_context(bs); 3690dcd04228SStefan Hajnoczi 3691dcd04228SStefan Hajnoczi /* This function executes in the old AioContext so acquire the new one in 3692dcd04228SStefan Hajnoczi * case it runs in a different thread. 3693dcd04228SStefan Hajnoczi */ 3694dcd04228SStefan Hajnoczi aio_context_acquire(new_context); 3695dcd04228SStefan Hajnoczi bdrv_attach_aio_context(bs, new_context); 3696dcd04228SStefan Hajnoczi aio_context_release(new_context); 369785d126f3SStefan Hajnoczi } 3698d616b224SStefan Hajnoczi 369933384421SMax Reitz void bdrv_add_aio_context_notifier(BlockDriverState *bs, 370033384421SMax Reitz void (*attached_aio_context)(AioContext *new_context, void *opaque), 370133384421SMax Reitz void (*detach_aio_context)(void *opaque), void *opaque) 370233384421SMax Reitz { 370333384421SMax Reitz BdrvAioNotifier *ban = g_new(BdrvAioNotifier, 1); 370433384421SMax Reitz *ban = (BdrvAioNotifier){ 370533384421SMax Reitz .attached_aio_context = attached_aio_context, 370633384421SMax Reitz .detach_aio_context = detach_aio_context, 370733384421SMax Reitz .opaque = opaque 370833384421SMax Reitz }; 370933384421SMax Reitz 371033384421SMax Reitz QLIST_INSERT_HEAD(&bs->aio_notifiers, ban, list); 371133384421SMax Reitz } 371233384421SMax Reitz 371333384421SMax Reitz void bdrv_remove_aio_context_notifier(BlockDriverState *bs, 371433384421SMax Reitz void (*attached_aio_context)(AioContext *, 371533384421SMax Reitz void *), 371633384421SMax Reitz void (*detach_aio_context)(void *), 371733384421SMax Reitz void *opaque) 371833384421SMax Reitz { 371933384421SMax Reitz BdrvAioNotifier *ban, *ban_next; 372033384421SMax Reitz 372133384421SMax Reitz QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) { 372233384421SMax Reitz if (ban->attached_aio_context == attached_aio_context && 372333384421SMax Reitz ban->detach_aio_context == detach_aio_context && 372433384421SMax Reitz ban->opaque == opaque) 372533384421SMax Reitz { 372633384421SMax Reitz QLIST_REMOVE(ban, list); 372733384421SMax Reitz g_free(ban); 372833384421SMax Reitz 372933384421SMax Reitz return; 373033384421SMax Reitz } 373133384421SMax Reitz } 373233384421SMax Reitz 373333384421SMax Reitz abort(); 373433384421SMax Reitz } 373533384421SMax Reitz 373677485434SMax Reitz int bdrv_amend_options(BlockDriverState *bs, QemuOpts *opts, 37378b13976dSMax Reitz BlockDriverAmendStatusCB *status_cb, void *cb_opaque) 37386f176b48SMax Reitz { 3739c282e1fdSChunyan Liu if (!bs->drv->bdrv_amend_options) { 37406f176b48SMax Reitz return -ENOTSUP; 37416f176b48SMax Reitz } 37428b13976dSMax Reitz return bs->drv->bdrv_amend_options(bs, opts, status_cb, cb_opaque); 37436f176b48SMax Reitz } 3744f6186f49SBenoît Canet 3745b5042a36SBenoît Canet /* This function will be called by the bdrv_recurse_is_first_non_filter method 3746b5042a36SBenoît Canet * of block filter and by bdrv_is_first_non_filter. 3747b5042a36SBenoît Canet * It is used to test if the given bs is the candidate or recurse more in the 3748b5042a36SBenoît Canet * node graph. 3749212a5a8fSBenoît Canet */ 3750212a5a8fSBenoît Canet bool bdrv_recurse_is_first_non_filter(BlockDriverState *bs, 3751212a5a8fSBenoît Canet BlockDriverState *candidate) 3752f6186f49SBenoît Canet { 3753b5042a36SBenoît Canet /* return false if basic checks fails */ 3754b5042a36SBenoît Canet if (!bs || !bs->drv) { 3755b5042a36SBenoît Canet return false; 3756b5042a36SBenoît Canet } 3757b5042a36SBenoît Canet 3758b5042a36SBenoît Canet /* the code reached a non block filter driver -> check if the bs is 3759b5042a36SBenoît Canet * the same as the candidate. It's the recursion termination condition. 3760b5042a36SBenoît Canet */ 3761b5042a36SBenoît Canet if (!bs->drv->is_filter) { 3762b5042a36SBenoît Canet return bs == candidate; 3763b5042a36SBenoît Canet } 3764b5042a36SBenoît Canet /* Down this path the driver is a block filter driver */ 3765b5042a36SBenoît Canet 3766b5042a36SBenoît Canet /* If the block filter recursion method is defined use it to recurse down 3767b5042a36SBenoît Canet * the node graph. 3768b5042a36SBenoît Canet */ 3769b5042a36SBenoît Canet if (bs->drv->bdrv_recurse_is_first_non_filter) { 3770212a5a8fSBenoît Canet return bs->drv->bdrv_recurse_is_first_non_filter(bs, candidate); 3771212a5a8fSBenoît Canet } 3772212a5a8fSBenoît Canet 3773b5042a36SBenoît Canet /* the driver is a block filter but don't allow to recurse -> return false 3774b5042a36SBenoît Canet */ 3775b5042a36SBenoît Canet return false; 3776212a5a8fSBenoît Canet } 3777212a5a8fSBenoît Canet 3778212a5a8fSBenoît Canet /* This function checks if the candidate is the first non filter bs down it's 3779212a5a8fSBenoît Canet * bs chain. Since we don't have pointers to parents it explore all bs chains 3780212a5a8fSBenoît Canet * from the top. Some filters can choose not to pass down the recursion. 3781212a5a8fSBenoît Canet */ 3782212a5a8fSBenoît Canet bool bdrv_is_first_non_filter(BlockDriverState *candidate) 3783212a5a8fSBenoît Canet { 378479720af6SMax Reitz BlockDriverState *bs = NULL; 3785212a5a8fSBenoît Canet 3786212a5a8fSBenoît Canet /* walk down the bs forest recursively */ 378779720af6SMax Reitz while ((bs = bdrv_next(bs)) != NULL) { 3788212a5a8fSBenoît Canet bool perm; 3789212a5a8fSBenoît Canet 3790b5042a36SBenoît Canet /* try to recurse in this top level bs */ 3791e6dc8a1fSKevin Wolf perm = bdrv_recurse_is_first_non_filter(bs, candidate); 3792212a5a8fSBenoît Canet 3793212a5a8fSBenoît Canet /* candidate is the first non filter */ 3794212a5a8fSBenoît Canet if (perm) { 3795212a5a8fSBenoît Canet return true; 3796212a5a8fSBenoît Canet } 3797212a5a8fSBenoît Canet } 3798212a5a8fSBenoît Canet 3799212a5a8fSBenoît Canet return false; 3800f6186f49SBenoît Canet } 380109158f00SBenoît Canet 3802e12f3784SWen Congyang BlockDriverState *check_to_replace_node(BlockDriverState *parent_bs, 3803e12f3784SWen Congyang const char *node_name, Error **errp) 380409158f00SBenoît Canet { 380509158f00SBenoît Canet BlockDriverState *to_replace_bs = bdrv_find_node(node_name); 38065a7e7a0bSStefan Hajnoczi AioContext *aio_context; 38075a7e7a0bSStefan Hajnoczi 380809158f00SBenoît Canet if (!to_replace_bs) { 380909158f00SBenoît Canet error_setg(errp, "Node name '%s' not found", node_name); 381009158f00SBenoît Canet return NULL; 381109158f00SBenoît Canet } 381209158f00SBenoît Canet 38135a7e7a0bSStefan Hajnoczi aio_context = bdrv_get_aio_context(to_replace_bs); 38145a7e7a0bSStefan Hajnoczi aio_context_acquire(aio_context); 38155a7e7a0bSStefan Hajnoczi 381609158f00SBenoît Canet if (bdrv_op_is_blocked(to_replace_bs, BLOCK_OP_TYPE_REPLACE, errp)) { 38175a7e7a0bSStefan Hajnoczi to_replace_bs = NULL; 38185a7e7a0bSStefan Hajnoczi goto out; 381909158f00SBenoît Canet } 382009158f00SBenoît Canet 382109158f00SBenoît Canet /* We don't want arbitrary node of the BDS chain to be replaced only the top 382209158f00SBenoît Canet * most non filter in order to prevent data corruption. 382309158f00SBenoît Canet * Another benefit is that this tests exclude backing files which are 382409158f00SBenoît Canet * blocked by the backing blockers. 382509158f00SBenoît Canet */ 3826e12f3784SWen Congyang if (!bdrv_recurse_is_first_non_filter(parent_bs, to_replace_bs)) { 382709158f00SBenoît Canet error_setg(errp, "Only top most non filter can be replaced"); 38285a7e7a0bSStefan Hajnoczi to_replace_bs = NULL; 38295a7e7a0bSStefan Hajnoczi goto out; 383009158f00SBenoît Canet } 383109158f00SBenoît Canet 38325a7e7a0bSStefan Hajnoczi out: 38335a7e7a0bSStefan Hajnoczi aio_context_release(aio_context); 383409158f00SBenoît Canet return to_replace_bs; 383509158f00SBenoît Canet } 3836448ad91dSMing Lei 383791af7014SMax Reitz static bool append_open_options(QDict *d, BlockDriverState *bs) 383891af7014SMax Reitz { 383991af7014SMax Reitz const QDictEntry *entry; 38409e700c1aSKevin Wolf QemuOptDesc *desc; 3841260fecf1SKevin Wolf BdrvChild *child; 384291af7014SMax Reitz bool found_any = false; 3843260fecf1SKevin Wolf const char *p; 384491af7014SMax Reitz 384591af7014SMax Reitz for (entry = qdict_first(bs->options); entry; 384691af7014SMax Reitz entry = qdict_next(bs->options, entry)) 384791af7014SMax Reitz { 3848260fecf1SKevin Wolf /* Exclude options for children */ 3849260fecf1SKevin Wolf QLIST_FOREACH(child, &bs->children, next) { 3850260fecf1SKevin Wolf if (strstart(qdict_entry_key(entry), child->name, &p) 3851260fecf1SKevin Wolf && (!*p || *p == '.')) 3852260fecf1SKevin Wolf { 3853260fecf1SKevin Wolf break; 3854260fecf1SKevin Wolf } 3855260fecf1SKevin Wolf } 3856260fecf1SKevin Wolf if (child) { 38579e700c1aSKevin Wolf continue; 38589e700c1aSKevin Wolf } 38599e700c1aSKevin Wolf 38609e700c1aSKevin Wolf /* And exclude all non-driver-specific options */ 38619e700c1aSKevin Wolf for (desc = bdrv_runtime_opts.desc; desc->name; desc++) { 38629e700c1aSKevin Wolf if (!strcmp(qdict_entry_key(entry), desc->name)) { 38639e700c1aSKevin Wolf break; 38649e700c1aSKevin Wolf } 38659e700c1aSKevin Wolf } 38669e700c1aSKevin Wolf if (desc->name) { 38679e700c1aSKevin Wolf continue; 38689e700c1aSKevin Wolf } 38699e700c1aSKevin Wolf 387091af7014SMax Reitz qobject_incref(qdict_entry_value(entry)); 387191af7014SMax Reitz qdict_put_obj(d, qdict_entry_key(entry), qdict_entry_value(entry)); 387291af7014SMax Reitz found_any = true; 387391af7014SMax Reitz } 387491af7014SMax Reitz 387591af7014SMax Reitz return found_any; 387691af7014SMax Reitz } 387791af7014SMax Reitz 387891af7014SMax Reitz /* Updates the following BDS fields: 387991af7014SMax Reitz * - exact_filename: A filename which may be used for opening a block device 388091af7014SMax Reitz * which (mostly) equals the given BDS (even without any 388191af7014SMax Reitz * other options; so reading and writing must return the same 388291af7014SMax Reitz * results, but caching etc. may be different) 388391af7014SMax Reitz * - full_open_options: Options which, when given when opening a block device 388491af7014SMax Reitz * (without a filename), result in a BDS (mostly) 388591af7014SMax Reitz * equalling the given one 388691af7014SMax Reitz * - filename: If exact_filename is set, it is copied here. Otherwise, 388791af7014SMax Reitz * full_open_options is converted to a JSON object, prefixed with 388891af7014SMax Reitz * "json:" (for use through the JSON pseudo protocol) and put here. 388991af7014SMax Reitz */ 389091af7014SMax Reitz void bdrv_refresh_filename(BlockDriverState *bs) 389191af7014SMax Reitz { 389291af7014SMax Reitz BlockDriver *drv = bs->drv; 389391af7014SMax Reitz QDict *opts; 389491af7014SMax Reitz 389591af7014SMax Reitz if (!drv) { 389691af7014SMax Reitz return; 389791af7014SMax Reitz } 389891af7014SMax Reitz 389991af7014SMax Reitz /* This BDS's file name will most probably depend on its file's name, so 390091af7014SMax Reitz * refresh that first */ 390191af7014SMax Reitz if (bs->file) { 39029a4f4c31SKevin Wolf bdrv_refresh_filename(bs->file->bs); 390391af7014SMax Reitz } 390491af7014SMax Reitz 390591af7014SMax Reitz if (drv->bdrv_refresh_filename) { 390691af7014SMax Reitz /* Obsolete information is of no use here, so drop the old file name 390791af7014SMax Reitz * information before refreshing it */ 390891af7014SMax Reitz bs->exact_filename[0] = '\0'; 390991af7014SMax Reitz if (bs->full_open_options) { 391091af7014SMax Reitz QDECREF(bs->full_open_options); 391191af7014SMax Reitz bs->full_open_options = NULL; 391291af7014SMax Reitz } 391391af7014SMax Reitz 39144cdd01d3SKevin Wolf opts = qdict_new(); 39154cdd01d3SKevin Wolf append_open_options(opts, bs); 39164cdd01d3SKevin Wolf drv->bdrv_refresh_filename(bs, opts); 39174cdd01d3SKevin Wolf QDECREF(opts); 391891af7014SMax Reitz } else if (bs->file) { 391991af7014SMax Reitz /* Try to reconstruct valid information from the underlying file */ 392091af7014SMax Reitz bool has_open_options; 392191af7014SMax Reitz 392291af7014SMax Reitz bs->exact_filename[0] = '\0'; 392391af7014SMax Reitz if (bs->full_open_options) { 392491af7014SMax Reitz QDECREF(bs->full_open_options); 392591af7014SMax Reitz bs->full_open_options = NULL; 392691af7014SMax Reitz } 392791af7014SMax Reitz 392891af7014SMax Reitz opts = qdict_new(); 392991af7014SMax Reitz has_open_options = append_open_options(opts, bs); 393091af7014SMax Reitz 393191af7014SMax Reitz /* If no specific options have been given for this BDS, the filename of 393291af7014SMax Reitz * the underlying file should suffice for this one as well */ 39339a4f4c31SKevin Wolf if (bs->file->bs->exact_filename[0] && !has_open_options) { 39349a4f4c31SKevin Wolf strcpy(bs->exact_filename, bs->file->bs->exact_filename); 393591af7014SMax Reitz } 393691af7014SMax Reitz /* Reconstructing the full options QDict is simple for most format block 393791af7014SMax Reitz * drivers, as long as the full options are known for the underlying 393891af7014SMax Reitz * file BDS. The full options QDict of that file BDS should somehow 393991af7014SMax Reitz * contain a representation of the filename, therefore the following 394091af7014SMax Reitz * suffices without querying the (exact_)filename of this BDS. */ 39419a4f4c31SKevin Wolf if (bs->file->bs->full_open_options) { 394291af7014SMax Reitz qdict_put_obj(opts, "driver", 394391af7014SMax Reitz QOBJECT(qstring_from_str(drv->format_name))); 39449a4f4c31SKevin Wolf QINCREF(bs->file->bs->full_open_options); 39459a4f4c31SKevin Wolf qdict_put_obj(opts, "file", 39469a4f4c31SKevin Wolf QOBJECT(bs->file->bs->full_open_options)); 394791af7014SMax Reitz 394891af7014SMax Reitz bs->full_open_options = opts; 394991af7014SMax Reitz } else { 395091af7014SMax Reitz QDECREF(opts); 395191af7014SMax Reitz } 395291af7014SMax Reitz } else if (!bs->full_open_options && qdict_size(bs->options)) { 395391af7014SMax Reitz /* There is no underlying file BDS (at least referenced by BDS.file), 395491af7014SMax Reitz * so the full options QDict should be equal to the options given 395591af7014SMax Reitz * specifically for this block device when it was opened (plus the 395691af7014SMax Reitz * driver specification). 395791af7014SMax Reitz * Because those options don't change, there is no need to update 395891af7014SMax Reitz * full_open_options when it's already set. */ 395991af7014SMax Reitz 396091af7014SMax Reitz opts = qdict_new(); 396191af7014SMax Reitz append_open_options(opts, bs); 396291af7014SMax Reitz qdict_put_obj(opts, "driver", 396391af7014SMax Reitz QOBJECT(qstring_from_str(drv->format_name))); 396491af7014SMax Reitz 396591af7014SMax Reitz if (bs->exact_filename[0]) { 396691af7014SMax Reitz /* This may not work for all block protocol drivers (some may 396791af7014SMax Reitz * require this filename to be parsed), but we have to find some 396891af7014SMax Reitz * default solution here, so just include it. If some block driver 396991af7014SMax Reitz * does not support pure options without any filename at all or 397091af7014SMax Reitz * needs some special format of the options QDict, it needs to 397191af7014SMax Reitz * implement the driver-specific bdrv_refresh_filename() function. 397291af7014SMax Reitz */ 397391af7014SMax Reitz qdict_put_obj(opts, "filename", 397491af7014SMax Reitz QOBJECT(qstring_from_str(bs->exact_filename))); 397591af7014SMax Reitz } 397691af7014SMax Reitz 397791af7014SMax Reitz bs->full_open_options = opts; 397891af7014SMax Reitz } 397991af7014SMax Reitz 398091af7014SMax Reitz if (bs->exact_filename[0]) { 398191af7014SMax Reitz pstrcpy(bs->filename, sizeof(bs->filename), bs->exact_filename); 398291af7014SMax Reitz } else if (bs->full_open_options) { 398391af7014SMax Reitz QString *json = qobject_to_json(QOBJECT(bs->full_open_options)); 398491af7014SMax Reitz snprintf(bs->filename, sizeof(bs->filename), "json:%s", 398591af7014SMax Reitz qstring_get_str(json)); 398691af7014SMax Reitz QDECREF(json); 398791af7014SMax Reitz } 398891af7014SMax Reitz } 3989