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 */ 24e688df6bSMarkus Armbruster 25d38ea87aSPeter Maydell #include "qemu/osdep.h" 260ab8ed18SDaniel P. Berrange #include "block/trace.h" 27737e150eSPaolo Bonzini #include "block/block_int.h" 28737e150eSPaolo Bonzini #include "block/blockjob.h" 29cd7fca95SKevin Wolf #include "block/nbd.h" 30609f45eaSMax Reitz #include "block/qdict.h" 31d49b6836SMarkus Armbruster #include "qemu/error-report.h" 3288d88798SMarc Mari #include "module_block.h" 33db725815SMarkus Armbruster #include "qemu/main-loop.h" 341de7afc9SPaolo Bonzini #include "qemu/module.h" 35e688df6bSMarkus Armbruster #include "qapi/error.h" 36452fcdbcSMarkus Armbruster #include "qapi/qmp/qdict.h" 377b1b5d19SPaolo Bonzini #include "qapi/qmp/qjson.h" 38e59a0cf1SMax Reitz #include "qapi/qmp/qnull.h" 39fc81fa1eSMarkus Armbruster #include "qapi/qmp/qstring.h" 40e1d74bc6SKevin Wolf #include "qapi/qobject-output-visitor.h" 41e1d74bc6SKevin Wolf #include "qapi/qapi-visit-block-core.h" 42bfb197e0SMarkus Armbruster #include "sysemu/block-backend.h" 439c17d615SPaolo Bonzini #include "sysemu/sysemu.h" 441de7afc9SPaolo Bonzini #include "qemu/notify.h" 45922a01a0SMarkus Armbruster #include "qemu/option.h" 4610817bf0SDaniel P. Berrange #include "qemu/coroutine.h" 47c13163fbSBenoît Canet #include "block/qapi.h" 481de7afc9SPaolo Bonzini #include "qemu/timer.h" 49f348b6d1SVeronia Bahaa #include "qemu/cutils.h" 50f348b6d1SVeronia Bahaa #include "qemu/id.h" 51fc01f7e7Sbellard 5271e72a19SJuan Quintela #ifdef CONFIG_BSD 537674e7bfSbellard #include <sys/ioctl.h> 5472cf2d4fSBlue Swirl #include <sys/queue.h> 55c5e97233Sblueswir1 #ifndef __DragonFly__ 567674e7bfSbellard #include <sys/disk.h> 577674e7bfSbellard #endif 58c5e97233Sblueswir1 #endif 597674e7bfSbellard 6049dc768dSaliguori #ifdef _WIN32 6149dc768dSaliguori #include <windows.h> 6249dc768dSaliguori #endif 6349dc768dSaliguori 641c9805a3SStefan Hajnoczi #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */ 651c9805a3SStefan Hajnoczi 66dc364f4cSBenoît Canet static QTAILQ_HEAD(, BlockDriverState) graph_bdrv_states = 67dc364f4cSBenoît Canet QTAILQ_HEAD_INITIALIZER(graph_bdrv_states); 68dc364f4cSBenoît Canet 692c1d04e0SMax Reitz static QTAILQ_HEAD(, BlockDriverState) all_bdrv_states = 702c1d04e0SMax Reitz QTAILQ_HEAD_INITIALIZER(all_bdrv_states); 712c1d04e0SMax Reitz 728a22f02aSStefan Hajnoczi static QLIST_HEAD(, BlockDriver) bdrv_drivers = 738a22f02aSStefan Hajnoczi QLIST_HEAD_INITIALIZER(bdrv_drivers); 74ea2384d3Sbellard 755b363937SMax Reitz static BlockDriverState *bdrv_open_inherit(const char *filename, 765b363937SMax Reitz const char *reference, 775b363937SMax Reitz QDict *options, int flags, 78f3930ed0SKevin Wolf BlockDriverState *parent, 795b363937SMax Reitz const BdrvChildRole *child_role, 805b363937SMax Reitz Error **errp); 81f3930ed0SKevin Wolf 82eb852011SMarkus Armbruster /* If non-zero, use only whitelisted block drivers */ 83eb852011SMarkus Armbruster static int use_bdrv_whitelist; 84eb852011SMarkus Armbruster 859e0b22f4SStefan Hajnoczi #ifdef _WIN32 869e0b22f4SStefan Hajnoczi static int is_windows_drive_prefix(const char *filename) 879e0b22f4SStefan Hajnoczi { 889e0b22f4SStefan Hajnoczi return (((filename[0] >= 'a' && filename[0] <= 'z') || 899e0b22f4SStefan Hajnoczi (filename[0] >= 'A' && filename[0] <= 'Z')) && 909e0b22f4SStefan Hajnoczi filename[1] == ':'); 919e0b22f4SStefan Hajnoczi } 929e0b22f4SStefan Hajnoczi 939e0b22f4SStefan Hajnoczi int is_windows_drive(const char *filename) 949e0b22f4SStefan Hajnoczi { 959e0b22f4SStefan Hajnoczi if (is_windows_drive_prefix(filename) && 969e0b22f4SStefan Hajnoczi filename[2] == '\0') 979e0b22f4SStefan Hajnoczi return 1; 989e0b22f4SStefan Hajnoczi if (strstart(filename, "\\\\.\\", NULL) || 999e0b22f4SStefan Hajnoczi strstart(filename, "//./", NULL)) 1009e0b22f4SStefan Hajnoczi return 1; 1019e0b22f4SStefan Hajnoczi return 0; 1029e0b22f4SStefan Hajnoczi } 1039e0b22f4SStefan Hajnoczi #endif 1049e0b22f4SStefan Hajnoczi 105339064d5SKevin Wolf size_t bdrv_opt_mem_align(BlockDriverState *bs) 106339064d5SKevin Wolf { 107339064d5SKevin Wolf if (!bs || !bs->drv) { 108459b4e66SDenis V. Lunev /* page size or 4k (hdd sector size) should be on the safe side */ 109038adc2fSWei Yang return MAX(4096, qemu_real_host_page_size); 110339064d5SKevin Wolf } 111339064d5SKevin Wolf 112339064d5SKevin Wolf return bs->bl.opt_mem_alignment; 113339064d5SKevin Wolf } 114339064d5SKevin Wolf 1154196d2f0SDenis V. Lunev size_t bdrv_min_mem_align(BlockDriverState *bs) 1164196d2f0SDenis V. Lunev { 1174196d2f0SDenis V. Lunev if (!bs || !bs->drv) { 118459b4e66SDenis V. Lunev /* page size or 4k (hdd sector size) should be on the safe side */ 119038adc2fSWei Yang return MAX(4096, qemu_real_host_page_size); 1204196d2f0SDenis V. Lunev } 1214196d2f0SDenis V. Lunev 1224196d2f0SDenis V. Lunev return bs->bl.min_mem_alignment; 1234196d2f0SDenis V. Lunev } 1244196d2f0SDenis V. Lunev 1259e0b22f4SStefan Hajnoczi /* check if the path starts with "<protocol>:" */ 1265c98415bSMax Reitz int path_has_protocol(const char *path) 1279e0b22f4SStefan Hajnoczi { 128947995c0SPaolo Bonzini const char *p; 129947995c0SPaolo Bonzini 1309e0b22f4SStefan Hajnoczi #ifdef _WIN32 1319e0b22f4SStefan Hajnoczi if (is_windows_drive(path) || 1329e0b22f4SStefan Hajnoczi is_windows_drive_prefix(path)) { 1339e0b22f4SStefan Hajnoczi return 0; 1349e0b22f4SStefan Hajnoczi } 135947995c0SPaolo Bonzini p = path + strcspn(path, ":/\\"); 136947995c0SPaolo Bonzini #else 137947995c0SPaolo Bonzini p = path + strcspn(path, ":/"); 1389e0b22f4SStefan Hajnoczi #endif 1399e0b22f4SStefan Hajnoczi 140947995c0SPaolo Bonzini return *p == ':'; 1419e0b22f4SStefan Hajnoczi } 1429e0b22f4SStefan Hajnoczi 14383f64091Sbellard int path_is_absolute(const char *path) 14483f64091Sbellard { 14521664424Sbellard #ifdef _WIN32 14621664424Sbellard /* specific case for names like: "\\.\d:" */ 147f53f4da9SPaolo Bonzini if (is_windows_drive(path) || is_windows_drive_prefix(path)) { 14821664424Sbellard return 1; 149f53f4da9SPaolo Bonzini } 150f53f4da9SPaolo Bonzini return (*path == '/' || *path == '\\'); 1513b9f94e1Sbellard #else 152f53f4da9SPaolo Bonzini return (*path == '/'); 1533b9f94e1Sbellard #endif 15483f64091Sbellard } 15583f64091Sbellard 156009b03aaSMax Reitz /* if filename is absolute, just return its duplicate. Otherwise, build a 15783f64091Sbellard path to it by considering it is relative to base_path. URL are 15883f64091Sbellard supported. */ 159009b03aaSMax Reitz char *path_combine(const char *base_path, const char *filename) 16083f64091Sbellard { 161009b03aaSMax Reitz const char *protocol_stripped = NULL; 16283f64091Sbellard const char *p, *p1; 163009b03aaSMax Reitz char *result; 16483f64091Sbellard int len; 16583f64091Sbellard 16683f64091Sbellard if (path_is_absolute(filename)) { 167009b03aaSMax Reitz return g_strdup(filename); 168009b03aaSMax Reitz } 1690d54a6feSMax Reitz 1700d54a6feSMax Reitz if (path_has_protocol(base_path)) { 1710d54a6feSMax Reitz protocol_stripped = strchr(base_path, ':'); 1720d54a6feSMax Reitz if (protocol_stripped) { 1730d54a6feSMax Reitz protocol_stripped++; 1740d54a6feSMax Reitz } 1750d54a6feSMax Reitz } 1760d54a6feSMax Reitz p = protocol_stripped ?: base_path; 1770d54a6feSMax Reitz 1783b9f94e1Sbellard p1 = strrchr(base_path, '/'); 1793b9f94e1Sbellard #ifdef _WIN32 1803b9f94e1Sbellard { 1813b9f94e1Sbellard const char *p2; 1823b9f94e1Sbellard p2 = strrchr(base_path, '\\'); 183009b03aaSMax Reitz if (!p1 || p2 > p1) { 1843b9f94e1Sbellard p1 = p2; 1853b9f94e1Sbellard } 18683f64091Sbellard } 187009b03aaSMax Reitz #endif 188009b03aaSMax Reitz if (p1) { 189009b03aaSMax Reitz p1++; 190009b03aaSMax Reitz } else { 191009b03aaSMax Reitz p1 = base_path; 192009b03aaSMax Reitz } 193009b03aaSMax Reitz if (p1 > p) { 194009b03aaSMax Reitz p = p1; 195009b03aaSMax Reitz } 196009b03aaSMax Reitz len = p - base_path; 197009b03aaSMax Reitz 198009b03aaSMax Reitz result = g_malloc(len + strlen(filename) + 1); 199009b03aaSMax Reitz memcpy(result, base_path, len); 200009b03aaSMax Reitz strcpy(result + len, filename); 201009b03aaSMax Reitz 202009b03aaSMax Reitz return result; 203009b03aaSMax Reitz } 204009b03aaSMax Reitz 20503c320d8SMax Reitz /* 20603c320d8SMax Reitz * Helper function for bdrv_parse_filename() implementations to remove optional 20703c320d8SMax Reitz * protocol prefixes (especially "file:") from a filename and for putting the 20803c320d8SMax Reitz * stripped filename into the options QDict if there is such a prefix. 20903c320d8SMax Reitz */ 21003c320d8SMax Reitz void bdrv_parse_filename_strip_prefix(const char *filename, const char *prefix, 21103c320d8SMax Reitz QDict *options) 21203c320d8SMax Reitz { 21303c320d8SMax Reitz if (strstart(filename, prefix, &filename)) { 21403c320d8SMax Reitz /* Stripping the explicit protocol prefix may result in a protocol 21503c320d8SMax Reitz * prefix being (wrongly) detected (if the filename contains a colon) */ 21603c320d8SMax Reitz if (path_has_protocol(filename)) { 21703c320d8SMax Reitz QString *fat_filename; 21803c320d8SMax Reitz 21903c320d8SMax Reitz /* This means there is some colon before the first slash; therefore, 22003c320d8SMax Reitz * this cannot be an absolute path */ 22103c320d8SMax Reitz assert(!path_is_absolute(filename)); 22203c320d8SMax Reitz 22303c320d8SMax Reitz /* And we can thus fix the protocol detection issue by prefixing it 22403c320d8SMax Reitz * by "./" */ 22503c320d8SMax Reitz fat_filename = qstring_from_str("./"); 22603c320d8SMax Reitz qstring_append(fat_filename, filename); 22703c320d8SMax Reitz 22803c320d8SMax Reitz assert(!path_has_protocol(qstring_get_str(fat_filename))); 22903c320d8SMax Reitz 23003c320d8SMax Reitz qdict_put(options, "filename", fat_filename); 23103c320d8SMax Reitz } else { 23203c320d8SMax Reitz /* If no protocol prefix was detected, we can use the shortened 23303c320d8SMax Reitz * filename as-is */ 23403c320d8SMax Reitz qdict_put_str(options, "filename", filename); 23503c320d8SMax Reitz } 23603c320d8SMax Reitz } 23703c320d8SMax Reitz } 23803c320d8SMax Reitz 23903c320d8SMax Reitz 2409c5e6594SKevin Wolf /* Returns whether the image file is opened as read-only. Note that this can 2419c5e6594SKevin Wolf * return false and writing to the image file is still not possible because the 2429c5e6594SKevin Wolf * image is inactivated. */ 24393ed524eSJeff Cody bool bdrv_is_read_only(BlockDriverState *bs) 24493ed524eSJeff Cody { 24593ed524eSJeff Cody return bs->read_only; 24693ed524eSJeff Cody } 24793ed524eSJeff Cody 24854a32bfeSKevin Wolf int bdrv_can_set_read_only(BlockDriverState *bs, bool read_only, 24954a32bfeSKevin Wolf bool ignore_allow_rdw, Error **errp) 250fe5241bfSJeff Cody { 251e2b8247aSJeff Cody /* Do not set read_only if copy_on_read is enabled */ 252e2b8247aSJeff Cody if (bs->copy_on_read && read_only) { 253e2b8247aSJeff Cody error_setg(errp, "Can't set node '%s' to r/o with copy-on-read enabled", 254e2b8247aSJeff Cody bdrv_get_device_or_node_name(bs)); 255e2b8247aSJeff Cody return -EINVAL; 256e2b8247aSJeff Cody } 257e2b8247aSJeff Cody 258d6fcdf06SJeff Cody /* Do not clear read_only if it is prohibited */ 25954a32bfeSKevin Wolf if (!read_only && !(bs->open_flags & BDRV_O_ALLOW_RDWR) && 26054a32bfeSKevin Wolf !ignore_allow_rdw) 26154a32bfeSKevin Wolf { 262d6fcdf06SJeff Cody error_setg(errp, "Node '%s' is read only", 263d6fcdf06SJeff Cody bdrv_get_device_or_node_name(bs)); 264d6fcdf06SJeff Cody return -EPERM; 265d6fcdf06SJeff Cody } 266d6fcdf06SJeff Cody 26745803a03SJeff Cody return 0; 26845803a03SJeff Cody } 26945803a03SJeff Cody 270eaa2410fSKevin Wolf /* 271eaa2410fSKevin Wolf * Called by a driver that can only provide a read-only image. 272eaa2410fSKevin Wolf * 273eaa2410fSKevin Wolf * Returns 0 if the node is already read-only or it could switch the node to 274eaa2410fSKevin Wolf * read-only because BDRV_O_AUTO_RDONLY is set. 275eaa2410fSKevin Wolf * 276eaa2410fSKevin Wolf * Returns -EACCES if the node is read-write and BDRV_O_AUTO_RDONLY is not set 277eaa2410fSKevin Wolf * or bdrv_can_set_read_only() forbids making the node read-only. If @errmsg 278eaa2410fSKevin Wolf * is not NULL, it is used as the error message for the Error object. 279eaa2410fSKevin Wolf */ 280eaa2410fSKevin Wolf int bdrv_apply_auto_read_only(BlockDriverState *bs, const char *errmsg, 281eaa2410fSKevin Wolf Error **errp) 28245803a03SJeff Cody { 28345803a03SJeff Cody int ret = 0; 28445803a03SJeff Cody 285eaa2410fSKevin Wolf if (!(bs->open_flags & BDRV_O_RDWR)) { 286eaa2410fSKevin Wolf return 0; 287eaa2410fSKevin Wolf } 288eaa2410fSKevin Wolf if (!(bs->open_flags & BDRV_O_AUTO_RDONLY)) { 289eaa2410fSKevin Wolf goto fail; 290eaa2410fSKevin Wolf } 291eaa2410fSKevin Wolf 292eaa2410fSKevin Wolf ret = bdrv_can_set_read_only(bs, true, false, NULL); 29345803a03SJeff Cody if (ret < 0) { 294eaa2410fSKevin Wolf goto fail; 29545803a03SJeff Cody } 29645803a03SJeff Cody 297eaa2410fSKevin Wolf bs->read_only = true; 298eeae6a59SKevin Wolf bs->open_flags &= ~BDRV_O_RDWR; 299eeae6a59SKevin Wolf 300e2b8247aSJeff Cody return 0; 301eaa2410fSKevin Wolf 302eaa2410fSKevin Wolf fail: 303eaa2410fSKevin Wolf error_setg(errp, "%s", errmsg ?: "Image is read-only"); 304eaa2410fSKevin Wolf return -EACCES; 305fe5241bfSJeff Cody } 306fe5241bfSJeff Cody 307645ae7d8SMax Reitz /* 308645ae7d8SMax Reitz * If @backing is empty, this function returns NULL without setting 309645ae7d8SMax Reitz * @errp. In all other cases, NULL will only be returned with @errp 310645ae7d8SMax Reitz * set. 311645ae7d8SMax Reitz * 312645ae7d8SMax Reitz * Therefore, a return value of NULL without @errp set means that 313645ae7d8SMax Reitz * there is no backing file; if @errp is set, there is one but its 314645ae7d8SMax Reitz * absolute filename cannot be generated. 315645ae7d8SMax Reitz */ 316645ae7d8SMax Reitz char *bdrv_get_full_backing_filename_from_filename(const char *backed, 3170a82855aSMax Reitz const char *backing, 3189f07429eSMax Reitz Error **errp) 3190a82855aSMax Reitz { 320645ae7d8SMax Reitz if (backing[0] == '\0') { 321645ae7d8SMax Reitz return NULL; 322645ae7d8SMax Reitz } else if (path_has_protocol(backing) || path_is_absolute(backing)) { 323645ae7d8SMax Reitz return g_strdup(backing); 3249f07429eSMax Reitz } else if (backed[0] == '\0' || strstart(backed, "json:", NULL)) { 3259f07429eSMax Reitz error_setg(errp, "Cannot use relative backing file names for '%s'", 3269f07429eSMax Reitz backed); 327645ae7d8SMax Reitz return NULL; 3280a82855aSMax Reitz } else { 329645ae7d8SMax Reitz return path_combine(backed, backing); 3300a82855aSMax Reitz } 3310a82855aSMax Reitz } 3320a82855aSMax Reitz 3339f4793d8SMax Reitz /* 3349f4793d8SMax Reitz * If @filename is empty or NULL, this function returns NULL without 3359f4793d8SMax Reitz * setting @errp. In all other cases, NULL will only be returned with 3369f4793d8SMax Reitz * @errp set. 3379f4793d8SMax Reitz */ 3389f4793d8SMax Reitz static char *bdrv_make_absolute_filename(BlockDriverState *relative_to, 3399f4793d8SMax Reitz const char *filename, Error **errp) 3409f4793d8SMax Reitz { 3418df68616SMax Reitz char *dir, *full_name; 3429f4793d8SMax Reitz 3438df68616SMax Reitz if (!filename || filename[0] == '\0') { 3448df68616SMax Reitz return NULL; 3458df68616SMax Reitz } else if (path_has_protocol(filename) || path_is_absolute(filename)) { 3468df68616SMax Reitz return g_strdup(filename); 3478df68616SMax Reitz } 3489f4793d8SMax Reitz 3498df68616SMax Reitz dir = bdrv_dirname(relative_to, errp); 3508df68616SMax Reitz if (!dir) { 3518df68616SMax Reitz return NULL; 3528df68616SMax Reitz } 3539f4793d8SMax Reitz 3548df68616SMax Reitz full_name = g_strconcat(dir, filename, NULL); 3558df68616SMax Reitz g_free(dir); 3568df68616SMax Reitz return full_name; 3579f4793d8SMax Reitz } 3589f4793d8SMax Reitz 3596b6833c1SMax Reitz char *bdrv_get_full_backing_filename(BlockDriverState *bs, Error **errp) 360dc5a1371SPaolo Bonzini { 3619f4793d8SMax Reitz return bdrv_make_absolute_filename(bs, bs->backing_file, errp); 362dc5a1371SPaolo Bonzini } 363dc5a1371SPaolo Bonzini 3640eb7217eSStefan Hajnoczi void bdrv_register(BlockDriver *bdrv) 3650eb7217eSStefan Hajnoczi { 3668a22f02aSStefan Hajnoczi QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list); 367ea2384d3Sbellard } 368b338082bSbellard 369e4e9986bSMarkus Armbruster BlockDriverState *bdrv_new(void) 370e4e9986bSMarkus Armbruster { 371e4e9986bSMarkus Armbruster BlockDriverState *bs; 372e4e9986bSMarkus Armbruster int i; 373e4e9986bSMarkus Armbruster 3745839e53bSMarkus Armbruster bs = g_new0(BlockDriverState, 1); 375e4654d2dSFam Zheng QLIST_INIT(&bs->dirty_bitmaps); 376fbe40ff7SFam Zheng for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) { 377fbe40ff7SFam Zheng QLIST_INIT(&bs->op_blockers[i]); 378fbe40ff7SFam Zheng } 379d616b224SStefan Hajnoczi notifier_with_return_list_init(&bs->before_write_notifiers); 3803783fa3dSPaolo Bonzini qemu_co_mutex_init(&bs->reqs_lock); 3812119882cSPaolo Bonzini qemu_mutex_init(&bs->dirty_bitmap_mutex); 3829fcb0251SFam Zheng bs->refcnt = 1; 383dcd04228SStefan Hajnoczi bs->aio_context = qemu_get_aio_context(); 384d7d512f6SPaolo Bonzini 3853ff2f67aSEvgeny Yakovlev qemu_co_queue_init(&bs->flush_queue); 3863ff2f67aSEvgeny Yakovlev 3870f12264eSKevin Wolf for (i = 0; i < bdrv_drain_all_count; i++) { 3880f12264eSKevin Wolf bdrv_drained_begin(bs); 3890f12264eSKevin Wolf } 3900f12264eSKevin Wolf 3912c1d04e0SMax Reitz QTAILQ_INSERT_TAIL(&all_bdrv_states, bs, bs_list); 3922c1d04e0SMax Reitz 393b338082bSbellard return bs; 394b338082bSbellard } 395b338082bSbellard 39688d88798SMarc Mari static BlockDriver *bdrv_do_find_format(const char *format_name) 397ea2384d3Sbellard { 398ea2384d3Sbellard BlockDriver *drv1; 39988d88798SMarc Mari 4008a22f02aSStefan Hajnoczi QLIST_FOREACH(drv1, &bdrv_drivers, list) { 4018a22f02aSStefan Hajnoczi if (!strcmp(drv1->format_name, format_name)) { 402ea2384d3Sbellard return drv1; 403ea2384d3Sbellard } 4048a22f02aSStefan Hajnoczi } 40588d88798SMarc Mari 406ea2384d3Sbellard return NULL; 407ea2384d3Sbellard } 408ea2384d3Sbellard 40988d88798SMarc Mari BlockDriver *bdrv_find_format(const char *format_name) 41088d88798SMarc Mari { 41188d88798SMarc Mari BlockDriver *drv1; 41288d88798SMarc Mari int i; 41388d88798SMarc Mari 41488d88798SMarc Mari drv1 = bdrv_do_find_format(format_name); 41588d88798SMarc Mari if (drv1) { 41688d88798SMarc Mari return drv1; 41788d88798SMarc Mari } 41888d88798SMarc Mari 41988d88798SMarc Mari /* The driver isn't registered, maybe we need to load a module */ 42088d88798SMarc Mari for (i = 0; i < (int)ARRAY_SIZE(block_driver_modules); ++i) { 42188d88798SMarc Mari if (!strcmp(block_driver_modules[i].format_name, format_name)) { 42288d88798SMarc Mari block_module_load_one(block_driver_modules[i].library_name); 42388d88798SMarc Mari break; 42488d88798SMarc Mari } 42588d88798SMarc Mari } 42688d88798SMarc Mari 42788d88798SMarc Mari return bdrv_do_find_format(format_name); 42888d88798SMarc Mari } 42988d88798SMarc Mari 4309ac404c5SAndrey Shinkevich static int bdrv_format_is_whitelisted(const char *format_name, bool read_only) 431eb852011SMarkus Armbruster { 432b64ec4e4SFam Zheng static const char *whitelist_rw[] = { 433b64ec4e4SFam Zheng CONFIG_BDRV_RW_WHITELIST 434b64ec4e4SFam Zheng }; 435b64ec4e4SFam Zheng static const char *whitelist_ro[] = { 436b64ec4e4SFam Zheng CONFIG_BDRV_RO_WHITELIST 437eb852011SMarkus Armbruster }; 438eb852011SMarkus Armbruster const char **p; 439eb852011SMarkus Armbruster 440b64ec4e4SFam Zheng if (!whitelist_rw[0] && !whitelist_ro[0]) { 441eb852011SMarkus Armbruster return 1; /* no whitelist, anything goes */ 442b64ec4e4SFam Zheng } 443eb852011SMarkus Armbruster 444b64ec4e4SFam Zheng for (p = whitelist_rw; *p; p++) { 4459ac404c5SAndrey Shinkevich if (!strcmp(format_name, *p)) { 446eb852011SMarkus Armbruster return 1; 447eb852011SMarkus Armbruster } 448eb852011SMarkus Armbruster } 449b64ec4e4SFam Zheng if (read_only) { 450b64ec4e4SFam Zheng for (p = whitelist_ro; *p; p++) { 4519ac404c5SAndrey Shinkevich if (!strcmp(format_name, *p)) { 452b64ec4e4SFam Zheng return 1; 453b64ec4e4SFam Zheng } 454b64ec4e4SFam Zheng } 455b64ec4e4SFam Zheng } 456eb852011SMarkus Armbruster return 0; 457eb852011SMarkus Armbruster } 458eb852011SMarkus Armbruster 4599ac404c5SAndrey Shinkevich int bdrv_is_whitelisted(BlockDriver *drv, bool read_only) 4609ac404c5SAndrey Shinkevich { 4619ac404c5SAndrey Shinkevich return bdrv_format_is_whitelisted(drv->format_name, read_only); 4629ac404c5SAndrey Shinkevich } 4639ac404c5SAndrey Shinkevich 464e6ff69bfSDaniel P. Berrange bool bdrv_uses_whitelist(void) 465e6ff69bfSDaniel P. Berrange { 466e6ff69bfSDaniel P. Berrange return use_bdrv_whitelist; 467e6ff69bfSDaniel P. Berrange } 468e6ff69bfSDaniel P. Berrange 4695b7e1542SZhi Yong Wu typedef struct CreateCo { 4705b7e1542SZhi Yong Wu BlockDriver *drv; 4715b7e1542SZhi Yong Wu char *filename; 47283d0521aSChunyan Liu QemuOpts *opts; 4735b7e1542SZhi Yong Wu int ret; 474cc84d90fSMax Reitz Error *err; 4755b7e1542SZhi Yong Wu } CreateCo; 4765b7e1542SZhi Yong Wu 4775b7e1542SZhi Yong Wu static void coroutine_fn bdrv_create_co_entry(void *opaque) 4785b7e1542SZhi Yong Wu { 479cc84d90fSMax Reitz Error *local_err = NULL; 480cc84d90fSMax Reitz int ret; 481cc84d90fSMax Reitz 4825b7e1542SZhi Yong Wu CreateCo *cco = opaque; 4835b7e1542SZhi Yong Wu assert(cco->drv); 4845b7e1542SZhi Yong Wu 485efc75e2aSStefan Hajnoczi ret = cco->drv->bdrv_co_create_opts(cco->filename, cco->opts, &local_err); 486cc84d90fSMax Reitz error_propagate(&cco->err, local_err); 487cc84d90fSMax Reitz cco->ret = ret; 4885b7e1542SZhi Yong Wu } 4895b7e1542SZhi Yong Wu 4900e7e1989SKevin Wolf int bdrv_create(BlockDriver *drv, const char* filename, 49183d0521aSChunyan Liu QemuOpts *opts, Error **errp) 492ea2384d3Sbellard { 4935b7e1542SZhi Yong Wu int ret; 4940e7e1989SKevin Wolf 4955b7e1542SZhi Yong Wu Coroutine *co; 4965b7e1542SZhi Yong Wu CreateCo cco = { 4975b7e1542SZhi Yong Wu .drv = drv, 4985b7e1542SZhi Yong Wu .filename = g_strdup(filename), 49983d0521aSChunyan Liu .opts = opts, 5005b7e1542SZhi Yong Wu .ret = NOT_DONE, 501cc84d90fSMax Reitz .err = NULL, 5025b7e1542SZhi Yong Wu }; 5035b7e1542SZhi Yong Wu 504efc75e2aSStefan Hajnoczi if (!drv->bdrv_co_create_opts) { 505cc84d90fSMax Reitz error_setg(errp, "Driver '%s' does not support image creation", drv->format_name); 50680168bffSLuiz Capitulino ret = -ENOTSUP; 50780168bffSLuiz Capitulino goto out; 5085b7e1542SZhi Yong Wu } 5095b7e1542SZhi Yong Wu 5105b7e1542SZhi Yong Wu if (qemu_in_coroutine()) { 5115b7e1542SZhi Yong Wu /* Fast-path if already in coroutine context */ 5125b7e1542SZhi Yong Wu bdrv_create_co_entry(&cco); 5135b7e1542SZhi Yong Wu } else { 5140b8b8753SPaolo Bonzini co = qemu_coroutine_create(bdrv_create_co_entry, &cco); 5150b8b8753SPaolo Bonzini qemu_coroutine_enter(co); 5165b7e1542SZhi Yong Wu while (cco.ret == NOT_DONE) { 517b47ec2c4SPaolo Bonzini aio_poll(qemu_get_aio_context(), true); 5185b7e1542SZhi Yong Wu } 5195b7e1542SZhi Yong Wu } 5205b7e1542SZhi Yong Wu 5215b7e1542SZhi Yong Wu ret = cco.ret; 522cc84d90fSMax Reitz if (ret < 0) { 52384d18f06SMarkus Armbruster if (cco.err) { 524cc84d90fSMax Reitz error_propagate(errp, cco.err); 525cc84d90fSMax Reitz } else { 526cc84d90fSMax Reitz error_setg_errno(errp, -ret, "Could not create image"); 527cc84d90fSMax Reitz } 528cc84d90fSMax Reitz } 5295b7e1542SZhi Yong Wu 53080168bffSLuiz Capitulino out: 53180168bffSLuiz Capitulino g_free(cco.filename); 5325b7e1542SZhi Yong Wu return ret; 533ea2384d3Sbellard } 534ea2384d3Sbellard 535c282e1fdSChunyan Liu int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp) 53684a12e66SChristoph Hellwig { 53784a12e66SChristoph Hellwig BlockDriver *drv; 538cc84d90fSMax Reitz Error *local_err = NULL; 539cc84d90fSMax Reitz int ret; 54084a12e66SChristoph Hellwig 541b65a5e12SMax Reitz drv = bdrv_find_protocol(filename, true, errp); 54284a12e66SChristoph Hellwig if (drv == NULL) { 54316905d71SStefan Hajnoczi return -ENOENT; 54484a12e66SChristoph Hellwig } 54584a12e66SChristoph Hellwig 546c282e1fdSChunyan Liu ret = bdrv_create(drv, filename, opts, &local_err); 547cc84d90fSMax Reitz error_propagate(errp, local_err); 548cc84d90fSMax Reitz return ret; 54984a12e66SChristoph Hellwig } 55084a12e66SChristoph Hellwig 551892b7de8SEkaterina Tumanova /** 552892b7de8SEkaterina Tumanova * Try to get @bs's logical and physical block size. 553892b7de8SEkaterina Tumanova * On success, store them in @bsz struct and return 0. 554892b7de8SEkaterina Tumanova * On failure return -errno. 555892b7de8SEkaterina Tumanova * @bs must not be empty. 556892b7de8SEkaterina Tumanova */ 557892b7de8SEkaterina Tumanova int bdrv_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz) 558892b7de8SEkaterina Tumanova { 559892b7de8SEkaterina Tumanova BlockDriver *drv = bs->drv; 560892b7de8SEkaterina Tumanova 561892b7de8SEkaterina Tumanova if (drv && drv->bdrv_probe_blocksizes) { 562892b7de8SEkaterina Tumanova return drv->bdrv_probe_blocksizes(bs, bsz); 5635a612c00SManos Pitsidianakis } else if (drv && drv->is_filter && bs->file) { 5645a612c00SManos Pitsidianakis return bdrv_probe_blocksizes(bs->file->bs, bsz); 565892b7de8SEkaterina Tumanova } 566892b7de8SEkaterina Tumanova 567892b7de8SEkaterina Tumanova return -ENOTSUP; 568892b7de8SEkaterina Tumanova } 569892b7de8SEkaterina Tumanova 570892b7de8SEkaterina Tumanova /** 571892b7de8SEkaterina Tumanova * Try to get @bs's geometry (cyls, heads, sectors). 572892b7de8SEkaterina Tumanova * On success, store them in @geo struct and return 0. 573892b7de8SEkaterina Tumanova * On failure return -errno. 574892b7de8SEkaterina Tumanova * @bs must not be empty. 575892b7de8SEkaterina Tumanova */ 576892b7de8SEkaterina Tumanova int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo) 577892b7de8SEkaterina Tumanova { 578892b7de8SEkaterina Tumanova BlockDriver *drv = bs->drv; 579892b7de8SEkaterina Tumanova 580892b7de8SEkaterina Tumanova if (drv && drv->bdrv_probe_geometry) { 581892b7de8SEkaterina Tumanova return drv->bdrv_probe_geometry(bs, geo); 5825a612c00SManos Pitsidianakis } else if (drv && drv->is_filter && bs->file) { 5835a612c00SManos Pitsidianakis return bdrv_probe_geometry(bs->file->bs, geo); 584892b7de8SEkaterina Tumanova } 585892b7de8SEkaterina Tumanova 586892b7de8SEkaterina Tumanova return -ENOTSUP; 587892b7de8SEkaterina Tumanova } 588892b7de8SEkaterina Tumanova 589eba25057SJim Meyering /* 590eba25057SJim Meyering * Create a uniquely-named empty temporary file. 591eba25057SJim Meyering * Return 0 upon success, otherwise a negative errno value. 592eba25057SJim Meyering */ 593eba25057SJim Meyering int get_tmp_filename(char *filename, int size) 594eba25057SJim Meyering { 595d5249393Sbellard #ifdef _WIN32 5963b9f94e1Sbellard char temp_dir[MAX_PATH]; 597eba25057SJim Meyering /* GetTempFileName requires that its output buffer (4th param) 598eba25057SJim Meyering have length MAX_PATH or greater. */ 599eba25057SJim Meyering assert(size >= MAX_PATH); 600eba25057SJim Meyering return (GetTempPath(MAX_PATH, temp_dir) 601eba25057SJim Meyering && GetTempFileName(temp_dir, "qem", 0, filename) 602eba25057SJim Meyering ? 0 : -GetLastError()); 603d5249393Sbellard #else 604ea2384d3Sbellard int fd; 6057ccfb2ebSblueswir1 const char *tmpdir; 6060badc1eeSaurel32 tmpdir = getenv("TMPDIR"); 60769bef793SAmit Shah if (!tmpdir) { 60869bef793SAmit Shah tmpdir = "/var/tmp"; 60969bef793SAmit Shah } 610eba25057SJim Meyering if (snprintf(filename, size, "%s/vl.XXXXXX", tmpdir) >= size) { 611eba25057SJim Meyering return -EOVERFLOW; 612ea2384d3Sbellard } 613eba25057SJim Meyering fd = mkstemp(filename); 614fe235a06SDunrong Huang if (fd < 0) { 615fe235a06SDunrong Huang return -errno; 616fe235a06SDunrong Huang } 617fe235a06SDunrong Huang if (close(fd) != 0) { 618fe235a06SDunrong Huang unlink(filename); 619eba25057SJim Meyering return -errno; 620eba25057SJim Meyering } 621eba25057SJim Meyering return 0; 622d5249393Sbellard #endif 623eba25057SJim Meyering } 624ea2384d3Sbellard 625f3a5d3f8SChristoph Hellwig /* 626f3a5d3f8SChristoph Hellwig * Detect host devices. By convention, /dev/cdrom[N] is always 627f3a5d3f8SChristoph Hellwig * recognized as a host CDROM. 628f3a5d3f8SChristoph Hellwig */ 629f3a5d3f8SChristoph Hellwig static BlockDriver *find_hdev_driver(const char *filename) 630f3a5d3f8SChristoph Hellwig { 631508c7cb3SChristoph Hellwig int score_max = 0, score; 632508c7cb3SChristoph Hellwig BlockDriver *drv = NULL, *d; 633f3a5d3f8SChristoph Hellwig 6348a22f02aSStefan Hajnoczi QLIST_FOREACH(d, &bdrv_drivers, list) { 635508c7cb3SChristoph Hellwig if (d->bdrv_probe_device) { 636508c7cb3SChristoph Hellwig score = d->bdrv_probe_device(filename); 637508c7cb3SChristoph Hellwig if (score > score_max) { 638508c7cb3SChristoph Hellwig score_max = score; 639508c7cb3SChristoph Hellwig drv = d; 640f3a5d3f8SChristoph Hellwig } 641508c7cb3SChristoph Hellwig } 642f3a5d3f8SChristoph Hellwig } 643f3a5d3f8SChristoph Hellwig 644508c7cb3SChristoph Hellwig return drv; 645f3a5d3f8SChristoph Hellwig } 646f3a5d3f8SChristoph Hellwig 64788d88798SMarc Mari static BlockDriver *bdrv_do_find_protocol(const char *protocol) 64888d88798SMarc Mari { 64988d88798SMarc Mari BlockDriver *drv1; 65088d88798SMarc Mari 65188d88798SMarc Mari QLIST_FOREACH(drv1, &bdrv_drivers, list) { 65288d88798SMarc Mari if (drv1->protocol_name && !strcmp(drv1->protocol_name, protocol)) { 65388d88798SMarc Mari return drv1; 65488d88798SMarc Mari } 65588d88798SMarc Mari } 65688d88798SMarc Mari 65788d88798SMarc Mari return NULL; 65888d88798SMarc Mari } 65988d88798SMarc Mari 66098289620SKevin Wolf BlockDriver *bdrv_find_protocol(const char *filename, 661b65a5e12SMax Reitz bool allow_protocol_prefix, 662b65a5e12SMax Reitz Error **errp) 66384a12e66SChristoph Hellwig { 66484a12e66SChristoph Hellwig BlockDriver *drv1; 66584a12e66SChristoph Hellwig char protocol[128]; 66684a12e66SChristoph Hellwig int len; 66784a12e66SChristoph Hellwig const char *p; 66888d88798SMarc Mari int i; 66984a12e66SChristoph Hellwig 67066f82ceeSKevin Wolf /* TODO Drivers without bdrv_file_open must be specified explicitly */ 67166f82ceeSKevin Wolf 67239508e7aSChristoph Hellwig /* 67339508e7aSChristoph Hellwig * XXX(hch): we really should not let host device detection 67439508e7aSChristoph Hellwig * override an explicit protocol specification, but moving this 67539508e7aSChristoph Hellwig * later breaks access to device names with colons in them. 67639508e7aSChristoph Hellwig * Thanks to the brain-dead persistent naming schemes on udev- 67739508e7aSChristoph Hellwig * based Linux systems those actually are quite common. 67839508e7aSChristoph Hellwig */ 67984a12e66SChristoph Hellwig drv1 = find_hdev_driver(filename); 68039508e7aSChristoph Hellwig if (drv1) { 68184a12e66SChristoph Hellwig return drv1; 68284a12e66SChristoph Hellwig } 68339508e7aSChristoph Hellwig 68498289620SKevin Wolf if (!path_has_protocol(filename) || !allow_protocol_prefix) { 685ef810437SMax Reitz return &bdrv_file; 68639508e7aSChristoph Hellwig } 68798289620SKevin Wolf 6889e0b22f4SStefan Hajnoczi p = strchr(filename, ':'); 6899e0b22f4SStefan Hajnoczi assert(p != NULL); 69084a12e66SChristoph Hellwig len = p - filename; 69184a12e66SChristoph Hellwig if (len > sizeof(protocol) - 1) 69284a12e66SChristoph Hellwig len = sizeof(protocol) - 1; 69384a12e66SChristoph Hellwig memcpy(protocol, filename, len); 69484a12e66SChristoph Hellwig protocol[len] = '\0'; 69588d88798SMarc Mari 69688d88798SMarc Mari drv1 = bdrv_do_find_protocol(protocol); 69788d88798SMarc Mari if (drv1) { 69884a12e66SChristoph Hellwig return drv1; 69984a12e66SChristoph Hellwig } 70088d88798SMarc Mari 70188d88798SMarc Mari for (i = 0; i < (int)ARRAY_SIZE(block_driver_modules); ++i) { 70288d88798SMarc Mari if (block_driver_modules[i].protocol_name && 70388d88798SMarc Mari !strcmp(block_driver_modules[i].protocol_name, protocol)) { 70488d88798SMarc Mari block_module_load_one(block_driver_modules[i].library_name); 70588d88798SMarc Mari break; 70688d88798SMarc Mari } 70784a12e66SChristoph Hellwig } 708b65a5e12SMax Reitz 70988d88798SMarc Mari drv1 = bdrv_do_find_protocol(protocol); 71088d88798SMarc Mari if (!drv1) { 711b65a5e12SMax Reitz error_setg(errp, "Unknown protocol '%s'", protocol); 71288d88798SMarc Mari } 71388d88798SMarc Mari return drv1; 71484a12e66SChristoph Hellwig } 71584a12e66SChristoph Hellwig 716c6684249SMarkus Armbruster /* 717c6684249SMarkus Armbruster * Guess image format by probing its contents. 718c6684249SMarkus Armbruster * This is not a good idea when your image is raw (CVE-2008-2004), but 719c6684249SMarkus Armbruster * we do it anyway for backward compatibility. 720c6684249SMarkus Armbruster * 721c6684249SMarkus Armbruster * @buf contains the image's first @buf_size bytes. 7227cddd372SKevin Wolf * @buf_size is the buffer size in bytes (generally BLOCK_PROBE_BUF_SIZE, 7237cddd372SKevin Wolf * but can be smaller if the image file is smaller) 724c6684249SMarkus Armbruster * @filename is its filename. 725c6684249SMarkus Armbruster * 726c6684249SMarkus Armbruster * For all block drivers, call the bdrv_probe() method to get its 727c6684249SMarkus Armbruster * probing score. 728c6684249SMarkus Armbruster * Return the first block driver with the highest probing score. 729c6684249SMarkus Armbruster */ 73038f3ef57SKevin Wolf BlockDriver *bdrv_probe_all(const uint8_t *buf, int buf_size, 731c6684249SMarkus Armbruster const char *filename) 732c6684249SMarkus Armbruster { 733c6684249SMarkus Armbruster int score_max = 0, score; 734c6684249SMarkus Armbruster BlockDriver *drv = NULL, *d; 735c6684249SMarkus Armbruster 736c6684249SMarkus Armbruster QLIST_FOREACH(d, &bdrv_drivers, list) { 737c6684249SMarkus Armbruster if (d->bdrv_probe) { 738c6684249SMarkus Armbruster score = d->bdrv_probe(buf, buf_size, filename); 739c6684249SMarkus Armbruster if (score > score_max) { 740c6684249SMarkus Armbruster score_max = score; 741c6684249SMarkus Armbruster drv = d; 742c6684249SMarkus Armbruster } 743c6684249SMarkus Armbruster } 744c6684249SMarkus Armbruster } 745c6684249SMarkus Armbruster 746c6684249SMarkus Armbruster return drv; 747c6684249SMarkus Armbruster } 748c6684249SMarkus Armbruster 7495696c6e3SKevin Wolf static int find_image_format(BlockBackend *file, const char *filename, 75034b5d2c6SMax Reitz BlockDriver **pdrv, Error **errp) 751ea2384d3Sbellard { 752c6684249SMarkus Armbruster BlockDriver *drv; 7537cddd372SKevin Wolf uint8_t buf[BLOCK_PROBE_BUF_SIZE]; 754f500a6d3SKevin Wolf int ret = 0; 755f8ea0b00SNicholas Bellinger 75608a00559SKevin Wolf /* Return the raw BlockDriver * to scsi-generic devices or empty drives */ 7575696c6e3SKevin Wolf if (blk_is_sg(file) || !blk_is_inserted(file) || blk_getlength(file) == 0) { 758ef810437SMax Reitz *pdrv = &bdrv_raw; 759c98ac35dSStefan Weil return ret; 7601a396859SNicholas A. Bellinger } 761f8ea0b00SNicholas Bellinger 7625696c6e3SKevin Wolf ret = blk_pread(file, 0, buf, sizeof(buf)); 763ea2384d3Sbellard if (ret < 0) { 76434b5d2c6SMax Reitz error_setg_errno(errp, -ret, "Could not read image for determining its " 76534b5d2c6SMax Reitz "format"); 766c98ac35dSStefan Weil *pdrv = NULL; 767c98ac35dSStefan Weil return ret; 768ea2384d3Sbellard } 769ea2384d3Sbellard 770c6684249SMarkus Armbruster drv = bdrv_probe_all(buf, ret, filename); 771c98ac35dSStefan Weil if (!drv) { 77234b5d2c6SMax Reitz error_setg(errp, "Could not determine image format: No compatible " 77334b5d2c6SMax Reitz "driver found"); 774c98ac35dSStefan Weil ret = -ENOENT; 775c98ac35dSStefan Weil } 776c98ac35dSStefan Weil *pdrv = drv; 777c98ac35dSStefan Weil return ret; 778ea2384d3Sbellard } 779ea2384d3Sbellard 78051762288SStefan Hajnoczi /** 78151762288SStefan Hajnoczi * Set the current 'total_sectors' value 78265a9bb25SMarkus Armbruster * Return 0 on success, -errno on error. 78351762288SStefan Hajnoczi */ 7843d9f2d2aSKevin Wolf int refresh_total_sectors(BlockDriverState *bs, int64_t hint) 78551762288SStefan Hajnoczi { 78651762288SStefan Hajnoczi BlockDriver *drv = bs->drv; 78751762288SStefan Hajnoczi 788d470ad42SMax Reitz if (!drv) { 789d470ad42SMax Reitz return -ENOMEDIUM; 790d470ad42SMax Reitz } 791d470ad42SMax Reitz 792396759adSNicholas Bellinger /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */ 793b192af8aSDimitris Aragiorgis if (bdrv_is_sg(bs)) 794396759adSNicholas Bellinger return 0; 795396759adSNicholas Bellinger 79651762288SStefan Hajnoczi /* query actual device if possible, otherwise just trust the hint */ 79751762288SStefan Hajnoczi if (drv->bdrv_getlength) { 79851762288SStefan Hajnoczi int64_t length = drv->bdrv_getlength(bs); 79951762288SStefan Hajnoczi if (length < 0) { 80051762288SStefan Hajnoczi return length; 80151762288SStefan Hajnoczi } 8027e382003SFam Zheng hint = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE); 80351762288SStefan Hajnoczi } 80451762288SStefan Hajnoczi 80551762288SStefan Hajnoczi bs->total_sectors = hint; 80651762288SStefan Hajnoczi return 0; 80751762288SStefan Hajnoczi } 80851762288SStefan Hajnoczi 809c3993cdcSStefan Hajnoczi /** 810cddff5baSKevin Wolf * Combines a QDict of new block driver @options with any missing options taken 811cddff5baSKevin Wolf * from @old_options, so that leaving out an option defaults to its old value. 812cddff5baSKevin Wolf */ 813cddff5baSKevin Wolf static void bdrv_join_options(BlockDriverState *bs, QDict *options, 814cddff5baSKevin Wolf QDict *old_options) 815cddff5baSKevin Wolf { 816cddff5baSKevin Wolf if (bs->drv && bs->drv->bdrv_join_options) { 817cddff5baSKevin Wolf bs->drv->bdrv_join_options(options, old_options); 818cddff5baSKevin Wolf } else { 819cddff5baSKevin Wolf qdict_join(options, old_options, false); 820cddff5baSKevin Wolf } 821cddff5baSKevin Wolf } 822cddff5baSKevin Wolf 823543770bdSAlberto Garcia static BlockdevDetectZeroesOptions bdrv_parse_detect_zeroes(QemuOpts *opts, 824543770bdSAlberto Garcia int open_flags, 825543770bdSAlberto Garcia Error **errp) 826543770bdSAlberto Garcia { 827543770bdSAlberto Garcia Error *local_err = NULL; 828543770bdSAlberto Garcia char *value = qemu_opt_get_del(opts, "detect-zeroes"); 829543770bdSAlberto Garcia BlockdevDetectZeroesOptions detect_zeroes = 830543770bdSAlberto Garcia qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup, value, 831543770bdSAlberto Garcia BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, &local_err); 832543770bdSAlberto Garcia g_free(value); 833543770bdSAlberto Garcia if (local_err) { 834543770bdSAlberto Garcia error_propagate(errp, local_err); 835543770bdSAlberto Garcia return detect_zeroes; 836543770bdSAlberto Garcia } 837543770bdSAlberto Garcia 838543770bdSAlberto Garcia if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP && 839543770bdSAlberto Garcia !(open_flags & BDRV_O_UNMAP)) 840543770bdSAlberto Garcia { 841543770bdSAlberto Garcia error_setg(errp, "setting detect-zeroes to unmap is not allowed " 842543770bdSAlberto Garcia "without setting discard operation to unmap"); 843543770bdSAlberto Garcia } 844543770bdSAlberto Garcia 845543770bdSAlberto Garcia return detect_zeroes; 846543770bdSAlberto Garcia } 847543770bdSAlberto Garcia 848cddff5baSKevin Wolf /** 849f80f2673SAarushi Mehta * Set open flags for aio engine 850f80f2673SAarushi Mehta * 851f80f2673SAarushi Mehta * Return 0 on success, -1 if the engine specified is invalid 852f80f2673SAarushi Mehta */ 853f80f2673SAarushi Mehta int bdrv_parse_aio(const char *mode, int *flags) 854f80f2673SAarushi Mehta { 855f80f2673SAarushi Mehta if (!strcmp(mode, "threads")) { 856f80f2673SAarushi Mehta /* do nothing, default */ 857f80f2673SAarushi Mehta } else if (!strcmp(mode, "native")) { 858f80f2673SAarushi Mehta *flags |= BDRV_O_NATIVE_AIO; 859f80f2673SAarushi Mehta #ifdef CONFIG_LINUX_IO_URING 860f80f2673SAarushi Mehta } else if (!strcmp(mode, "io_uring")) { 861f80f2673SAarushi Mehta *flags |= BDRV_O_IO_URING; 862f80f2673SAarushi Mehta #endif 863f80f2673SAarushi Mehta } else { 864f80f2673SAarushi Mehta return -1; 865f80f2673SAarushi Mehta } 866f80f2673SAarushi Mehta 867f80f2673SAarushi Mehta return 0; 868f80f2673SAarushi Mehta } 869f80f2673SAarushi Mehta 870f80f2673SAarushi Mehta /** 8719e8f1835SPaolo Bonzini * Set open flags for a given discard mode 8729e8f1835SPaolo Bonzini * 8739e8f1835SPaolo Bonzini * Return 0 on success, -1 if the discard mode was invalid. 8749e8f1835SPaolo Bonzini */ 8759e8f1835SPaolo Bonzini int bdrv_parse_discard_flags(const char *mode, int *flags) 8769e8f1835SPaolo Bonzini { 8779e8f1835SPaolo Bonzini *flags &= ~BDRV_O_UNMAP; 8789e8f1835SPaolo Bonzini 8799e8f1835SPaolo Bonzini if (!strcmp(mode, "off") || !strcmp(mode, "ignore")) { 8809e8f1835SPaolo Bonzini /* do nothing */ 8819e8f1835SPaolo Bonzini } else if (!strcmp(mode, "on") || !strcmp(mode, "unmap")) { 8829e8f1835SPaolo Bonzini *flags |= BDRV_O_UNMAP; 8839e8f1835SPaolo Bonzini } else { 8849e8f1835SPaolo Bonzini return -1; 8859e8f1835SPaolo Bonzini } 8869e8f1835SPaolo Bonzini 8879e8f1835SPaolo Bonzini return 0; 8889e8f1835SPaolo Bonzini } 8899e8f1835SPaolo Bonzini 8909e8f1835SPaolo Bonzini /** 891c3993cdcSStefan Hajnoczi * Set open flags for a given cache mode 892c3993cdcSStefan Hajnoczi * 893c3993cdcSStefan Hajnoczi * Return 0 on success, -1 if the cache mode was invalid. 894c3993cdcSStefan Hajnoczi */ 89553e8ae01SKevin Wolf int bdrv_parse_cache_mode(const char *mode, int *flags, bool *writethrough) 896c3993cdcSStefan Hajnoczi { 897c3993cdcSStefan Hajnoczi *flags &= ~BDRV_O_CACHE_MASK; 898c3993cdcSStefan Hajnoczi 899c3993cdcSStefan Hajnoczi if (!strcmp(mode, "off") || !strcmp(mode, "none")) { 90053e8ae01SKevin Wolf *writethrough = false; 90153e8ae01SKevin Wolf *flags |= BDRV_O_NOCACHE; 90292196b2fSStefan Hajnoczi } else if (!strcmp(mode, "directsync")) { 90353e8ae01SKevin Wolf *writethrough = true; 90492196b2fSStefan Hajnoczi *flags |= BDRV_O_NOCACHE; 905c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "writeback")) { 90653e8ae01SKevin Wolf *writethrough = false; 907c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "unsafe")) { 90853e8ae01SKevin Wolf *writethrough = false; 909c3993cdcSStefan Hajnoczi *flags |= BDRV_O_NO_FLUSH; 910c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "writethrough")) { 91153e8ae01SKevin Wolf *writethrough = true; 912c3993cdcSStefan Hajnoczi } else { 913c3993cdcSStefan Hajnoczi return -1; 914c3993cdcSStefan Hajnoczi } 915c3993cdcSStefan Hajnoczi 916c3993cdcSStefan Hajnoczi return 0; 917c3993cdcSStefan Hajnoczi } 918c3993cdcSStefan Hajnoczi 919b5411555SKevin Wolf static char *bdrv_child_get_parent_desc(BdrvChild *c) 920b5411555SKevin Wolf { 921b5411555SKevin Wolf BlockDriverState *parent = c->opaque; 922b5411555SKevin Wolf return g_strdup(bdrv_get_device_or_node_name(parent)); 923b5411555SKevin Wolf } 924b5411555SKevin Wolf 92520018e12SKevin Wolf static void bdrv_child_cb_drained_begin(BdrvChild *child) 92620018e12SKevin Wolf { 92720018e12SKevin Wolf BlockDriverState *bs = child->opaque; 9286cd5c9d7SKevin Wolf bdrv_do_drained_begin_quiesce(bs, NULL, false); 92920018e12SKevin Wolf } 93020018e12SKevin Wolf 93189bd0305SKevin Wolf static bool bdrv_child_cb_drained_poll(BdrvChild *child) 93289bd0305SKevin Wolf { 93389bd0305SKevin Wolf BlockDriverState *bs = child->opaque; 9346cd5c9d7SKevin Wolf return bdrv_drain_poll(bs, false, NULL, false); 93589bd0305SKevin Wolf } 93689bd0305SKevin Wolf 937e037c09cSMax Reitz static void bdrv_child_cb_drained_end(BdrvChild *child, 938e037c09cSMax Reitz int *drained_end_counter) 93920018e12SKevin Wolf { 94020018e12SKevin Wolf BlockDriverState *bs = child->opaque; 941e037c09cSMax Reitz bdrv_drained_end_no_poll(bs, drained_end_counter); 94220018e12SKevin Wolf } 94320018e12SKevin Wolf 944d736f119SKevin Wolf static void bdrv_child_cb_attach(BdrvChild *child) 945d736f119SKevin Wolf { 946d736f119SKevin Wolf BlockDriverState *bs = child->opaque; 947d736f119SKevin Wolf bdrv_apply_subtree_drain(child, bs); 948d736f119SKevin Wolf } 949d736f119SKevin Wolf 950d736f119SKevin Wolf static void bdrv_child_cb_detach(BdrvChild *child) 951d736f119SKevin Wolf { 952d736f119SKevin Wolf BlockDriverState *bs = child->opaque; 953d736f119SKevin Wolf bdrv_unapply_subtree_drain(child, bs); 954d736f119SKevin Wolf } 955d736f119SKevin Wolf 95638701b6aSKevin Wolf static int bdrv_child_cb_inactivate(BdrvChild *child) 95738701b6aSKevin Wolf { 95838701b6aSKevin Wolf BlockDriverState *bs = child->opaque; 95938701b6aSKevin Wolf assert(bs->open_flags & BDRV_O_INACTIVE); 96038701b6aSKevin Wolf return 0; 96138701b6aSKevin Wolf } 96238701b6aSKevin Wolf 9635d231849SKevin Wolf static bool bdrv_child_cb_can_set_aio_ctx(BdrvChild *child, AioContext *ctx, 9645d231849SKevin Wolf GSList **ignore, Error **errp) 9655d231849SKevin Wolf { 9665d231849SKevin Wolf BlockDriverState *bs = child->opaque; 9675d231849SKevin Wolf return bdrv_can_set_aio_context(bs, ctx, ignore, errp); 9685d231849SKevin Wolf } 9695d231849SKevin Wolf 97053a7d041SKevin Wolf static void bdrv_child_cb_set_aio_ctx(BdrvChild *child, AioContext *ctx, 97153a7d041SKevin Wolf GSList **ignore) 97253a7d041SKevin Wolf { 97353a7d041SKevin Wolf BlockDriverState *bs = child->opaque; 97453a7d041SKevin Wolf return bdrv_set_aio_context_ignore(bs, ctx, ignore); 97553a7d041SKevin Wolf } 97653a7d041SKevin Wolf 9770b50cc88SKevin Wolf /* 97873176beeSKevin Wolf * Returns the options and flags that a temporary snapshot should get, based on 97973176beeSKevin Wolf * the originally requested flags (the originally requested image will have 98073176beeSKevin Wolf * flags like a backing file) 981b1e6fc08SKevin Wolf */ 98273176beeSKevin Wolf static void bdrv_temp_snapshot_options(int *child_flags, QDict *child_options, 98373176beeSKevin Wolf int parent_flags, QDict *parent_options) 984b1e6fc08SKevin Wolf { 98573176beeSKevin Wolf *child_flags = (parent_flags & ~BDRV_O_SNAPSHOT) | BDRV_O_TEMPORARY; 98673176beeSKevin Wolf 98773176beeSKevin Wolf /* For temporary files, unconditional cache=unsafe is fine */ 98873176beeSKevin Wolf qdict_set_default_str(child_options, BDRV_OPT_CACHE_DIRECT, "off"); 98973176beeSKevin Wolf qdict_set_default_str(child_options, BDRV_OPT_CACHE_NO_FLUSH, "on"); 99041869044SKevin Wolf 9913f48686fSKevin Wolf /* Copy the read-only and discard options from the parent */ 992f87a0e29SAlberto Garcia qdict_copy_default(child_options, parent_options, BDRV_OPT_READ_ONLY); 9933f48686fSKevin Wolf qdict_copy_default(child_options, parent_options, BDRV_OPT_DISCARD); 994f87a0e29SAlberto Garcia 99541869044SKevin Wolf /* aio=native doesn't work for cache.direct=off, so disable it for the 99641869044SKevin Wolf * temporary snapshot */ 99741869044SKevin Wolf *child_flags &= ~BDRV_O_NATIVE_AIO; 998b1e6fc08SKevin Wolf } 999b1e6fc08SKevin Wolf 1000b1e6fc08SKevin Wolf /* 10018e2160e2SKevin Wolf * Returns the options and flags that bs->file should get if a protocol driver 10028e2160e2SKevin Wolf * is expected, based on the given options and flags for the parent BDS 10030b50cc88SKevin Wolf */ 10048e2160e2SKevin Wolf static void bdrv_inherited_options(int *child_flags, QDict *child_options, 10058e2160e2SKevin Wolf int parent_flags, QDict *parent_options) 10060b50cc88SKevin Wolf { 10078e2160e2SKevin Wolf int flags = parent_flags; 10088e2160e2SKevin Wolf 10090b50cc88SKevin Wolf /* Enable protocol handling, disable format probing for bs->file */ 10100b50cc88SKevin Wolf flags |= BDRV_O_PROTOCOL; 10110b50cc88SKevin Wolf 101291a097e7SKevin Wolf /* If the cache mode isn't explicitly set, inherit direct and no-flush from 101391a097e7SKevin Wolf * the parent. */ 101491a097e7SKevin Wolf qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_DIRECT); 101591a097e7SKevin Wolf qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_NO_FLUSH); 10165a9347c6SFam Zheng qdict_copy_default(child_options, parent_options, BDRV_OPT_FORCE_SHARE); 101791a097e7SKevin Wolf 1018f87a0e29SAlberto Garcia /* Inherit the read-only option from the parent if it's not set */ 1019f87a0e29SAlberto Garcia qdict_copy_default(child_options, parent_options, BDRV_OPT_READ_ONLY); 1020e35bdc12SKevin Wolf qdict_copy_default(child_options, parent_options, BDRV_OPT_AUTO_READ_ONLY); 1021f87a0e29SAlberto Garcia 10220b50cc88SKevin Wolf /* Our block drivers take care to send flushes and respect unmap policy, 102391a097e7SKevin Wolf * so we can default to enable both on lower layers regardless of the 102491a097e7SKevin Wolf * corresponding parent options. */ 1025818584a4SKevin Wolf qdict_set_default_str(child_options, BDRV_OPT_DISCARD, "unmap"); 10260b50cc88SKevin Wolf 10270b50cc88SKevin Wolf /* Clear flags that only apply to the top layer */ 1028abb06c5aSDaniel P. Berrange flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ | 1029abb06c5aSDaniel P. Berrange BDRV_O_NO_IO); 10300b50cc88SKevin Wolf 10318e2160e2SKevin Wolf *child_flags = flags; 10320b50cc88SKevin Wolf } 10330b50cc88SKevin Wolf 1034f3930ed0SKevin Wolf const BdrvChildRole child_file = { 10356cd5c9d7SKevin Wolf .parent_is_bds = true, 1036b5411555SKevin Wolf .get_parent_desc = bdrv_child_get_parent_desc, 10378e2160e2SKevin Wolf .inherit_options = bdrv_inherited_options, 103820018e12SKevin Wolf .drained_begin = bdrv_child_cb_drained_begin, 103989bd0305SKevin Wolf .drained_poll = bdrv_child_cb_drained_poll, 104020018e12SKevin Wolf .drained_end = bdrv_child_cb_drained_end, 1041d736f119SKevin Wolf .attach = bdrv_child_cb_attach, 1042d736f119SKevin Wolf .detach = bdrv_child_cb_detach, 104338701b6aSKevin Wolf .inactivate = bdrv_child_cb_inactivate, 10445d231849SKevin Wolf .can_set_aio_ctx = bdrv_child_cb_can_set_aio_ctx, 104553a7d041SKevin Wolf .set_aio_ctx = bdrv_child_cb_set_aio_ctx, 1046f3930ed0SKevin Wolf }; 1047f3930ed0SKevin Wolf 1048f3930ed0SKevin Wolf /* 10498e2160e2SKevin Wolf * Returns the options and flags that bs->file should get if the use of formats 10508e2160e2SKevin Wolf * (and not only protocols) is permitted for it, based on the given options and 10518e2160e2SKevin Wolf * flags for the parent BDS 1052f3930ed0SKevin Wolf */ 10538e2160e2SKevin Wolf static void bdrv_inherited_fmt_options(int *child_flags, QDict *child_options, 10548e2160e2SKevin Wolf int parent_flags, QDict *parent_options) 1055f3930ed0SKevin Wolf { 10568e2160e2SKevin Wolf child_file.inherit_options(child_flags, child_options, 10578e2160e2SKevin Wolf parent_flags, parent_options); 10588e2160e2SKevin Wolf 1059abb06c5aSDaniel P. Berrange *child_flags &= ~(BDRV_O_PROTOCOL | BDRV_O_NO_IO); 1060f3930ed0SKevin Wolf } 1061f3930ed0SKevin Wolf 1062f3930ed0SKevin Wolf const BdrvChildRole child_format = { 10636cd5c9d7SKevin Wolf .parent_is_bds = true, 1064b5411555SKevin Wolf .get_parent_desc = bdrv_child_get_parent_desc, 10658e2160e2SKevin Wolf .inherit_options = bdrv_inherited_fmt_options, 106620018e12SKevin Wolf .drained_begin = bdrv_child_cb_drained_begin, 106789bd0305SKevin Wolf .drained_poll = bdrv_child_cb_drained_poll, 106820018e12SKevin Wolf .drained_end = bdrv_child_cb_drained_end, 1069d736f119SKevin Wolf .attach = bdrv_child_cb_attach, 1070d736f119SKevin Wolf .detach = bdrv_child_cb_detach, 107138701b6aSKevin Wolf .inactivate = bdrv_child_cb_inactivate, 10725d231849SKevin Wolf .can_set_aio_ctx = bdrv_child_cb_can_set_aio_ctx, 107353a7d041SKevin Wolf .set_aio_ctx = bdrv_child_cb_set_aio_ctx, 1074f3930ed0SKevin Wolf }; 1075f3930ed0SKevin Wolf 1076db95dbbaSKevin Wolf static void bdrv_backing_attach(BdrvChild *c) 1077db95dbbaSKevin Wolf { 1078db95dbbaSKevin Wolf BlockDriverState *parent = c->opaque; 1079db95dbbaSKevin Wolf BlockDriverState *backing_hd = c->bs; 1080db95dbbaSKevin Wolf 1081db95dbbaSKevin Wolf assert(!parent->backing_blocker); 1082db95dbbaSKevin Wolf error_setg(&parent->backing_blocker, 1083db95dbbaSKevin Wolf "node is used as backing hd of '%s'", 1084db95dbbaSKevin Wolf bdrv_get_device_or_node_name(parent)); 1085db95dbbaSKevin Wolf 1086f30c66baSMax Reitz bdrv_refresh_filename(backing_hd); 1087f30c66baSMax Reitz 1088db95dbbaSKevin Wolf parent->open_flags &= ~BDRV_O_NO_BACKING; 1089db95dbbaSKevin Wolf pstrcpy(parent->backing_file, sizeof(parent->backing_file), 1090db95dbbaSKevin Wolf backing_hd->filename); 1091db95dbbaSKevin Wolf pstrcpy(parent->backing_format, sizeof(parent->backing_format), 1092db95dbbaSKevin Wolf backing_hd->drv ? backing_hd->drv->format_name : ""); 1093db95dbbaSKevin Wolf 1094db95dbbaSKevin Wolf bdrv_op_block_all(backing_hd, parent->backing_blocker); 1095db95dbbaSKevin Wolf /* Otherwise we won't be able to commit or stream */ 1096db95dbbaSKevin Wolf bdrv_op_unblock(backing_hd, BLOCK_OP_TYPE_COMMIT_TARGET, 1097db95dbbaSKevin Wolf parent->backing_blocker); 1098db95dbbaSKevin Wolf bdrv_op_unblock(backing_hd, BLOCK_OP_TYPE_STREAM, 1099db95dbbaSKevin Wolf parent->backing_blocker); 1100db95dbbaSKevin Wolf /* 1101db95dbbaSKevin Wolf * We do backup in 3 ways: 1102db95dbbaSKevin Wolf * 1. drive backup 1103db95dbbaSKevin Wolf * The target bs is new opened, and the source is top BDS 1104db95dbbaSKevin Wolf * 2. blockdev backup 1105db95dbbaSKevin Wolf * Both the source and the target are top BDSes. 1106db95dbbaSKevin Wolf * 3. internal backup(used for block replication) 1107db95dbbaSKevin Wolf * Both the source and the target are backing file 1108db95dbbaSKevin Wolf * 1109db95dbbaSKevin Wolf * In case 1 and 2, neither the source nor the target is the backing file. 1110db95dbbaSKevin Wolf * In case 3, we will block the top BDS, so there is only one block job 1111db95dbbaSKevin Wolf * for the top BDS and its backing chain. 1112db95dbbaSKevin Wolf */ 1113db95dbbaSKevin Wolf bdrv_op_unblock(backing_hd, BLOCK_OP_TYPE_BACKUP_SOURCE, 1114db95dbbaSKevin Wolf parent->backing_blocker); 1115db95dbbaSKevin Wolf bdrv_op_unblock(backing_hd, BLOCK_OP_TYPE_BACKUP_TARGET, 1116db95dbbaSKevin Wolf parent->backing_blocker); 1117d736f119SKevin Wolf 1118d736f119SKevin Wolf bdrv_child_cb_attach(c); 1119db95dbbaSKevin Wolf } 1120db95dbbaSKevin Wolf 1121db95dbbaSKevin Wolf static void bdrv_backing_detach(BdrvChild *c) 1122db95dbbaSKevin Wolf { 1123db95dbbaSKevin Wolf BlockDriverState *parent = c->opaque; 1124db95dbbaSKevin Wolf 1125db95dbbaSKevin Wolf assert(parent->backing_blocker); 1126db95dbbaSKevin Wolf bdrv_op_unblock_all(c->bs, parent->backing_blocker); 1127db95dbbaSKevin Wolf error_free(parent->backing_blocker); 1128db95dbbaSKevin Wolf parent->backing_blocker = NULL; 1129d736f119SKevin Wolf 1130d736f119SKevin Wolf bdrv_child_cb_detach(c); 1131db95dbbaSKevin Wolf } 1132db95dbbaSKevin Wolf 1133317fc44eSKevin Wolf /* 11348e2160e2SKevin Wolf * Returns the options and flags that bs->backing should get, based on the 11358e2160e2SKevin Wolf * given options and flags for the parent BDS 1136317fc44eSKevin Wolf */ 11378e2160e2SKevin Wolf static void bdrv_backing_options(int *child_flags, QDict *child_options, 11388e2160e2SKevin Wolf int parent_flags, QDict *parent_options) 1139317fc44eSKevin Wolf { 11408e2160e2SKevin Wolf int flags = parent_flags; 11418e2160e2SKevin Wolf 1142b8816a43SKevin Wolf /* The cache mode is inherited unmodified for backing files; except WCE, 1143b8816a43SKevin Wolf * which is only applied on the top level (BlockBackend) */ 114491a097e7SKevin Wolf qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_DIRECT); 114591a097e7SKevin Wolf qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_NO_FLUSH); 11465a9347c6SFam Zheng qdict_copy_default(child_options, parent_options, BDRV_OPT_FORCE_SHARE); 114791a097e7SKevin Wolf 1148317fc44eSKevin Wolf /* backing files always opened read-only */ 1149f87a0e29SAlberto Garcia qdict_set_default_str(child_options, BDRV_OPT_READ_ONLY, "on"); 1150e35bdc12SKevin Wolf qdict_set_default_str(child_options, BDRV_OPT_AUTO_READ_ONLY, "off"); 1151f87a0e29SAlberto Garcia flags &= ~BDRV_O_COPY_ON_READ; 1152317fc44eSKevin Wolf 1153317fc44eSKevin Wolf /* snapshot=on is handled on the top layer */ 11548bfea15dSKevin Wolf flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_TEMPORARY); 1155317fc44eSKevin Wolf 11568e2160e2SKevin Wolf *child_flags = flags; 1157317fc44eSKevin Wolf } 1158317fc44eSKevin Wolf 11596858eba0SKevin Wolf static int bdrv_backing_update_filename(BdrvChild *c, BlockDriverState *base, 11606858eba0SKevin Wolf const char *filename, Error **errp) 11616858eba0SKevin Wolf { 11626858eba0SKevin Wolf BlockDriverState *parent = c->opaque; 1163e94d3dbaSAlberto Garcia bool read_only = bdrv_is_read_only(parent); 11646858eba0SKevin Wolf int ret; 11656858eba0SKevin Wolf 1166e94d3dbaSAlberto Garcia if (read_only) { 1167e94d3dbaSAlberto Garcia ret = bdrv_reopen_set_read_only(parent, false, errp); 116861f09ceaSKevin Wolf if (ret < 0) { 116961f09ceaSKevin Wolf return ret; 117061f09ceaSKevin Wolf } 117161f09ceaSKevin Wolf } 117261f09ceaSKevin Wolf 11736858eba0SKevin Wolf ret = bdrv_change_backing_file(parent, filename, 11746858eba0SKevin Wolf base->drv ? base->drv->format_name : ""); 11756858eba0SKevin Wolf if (ret < 0) { 117664730694SKevin Wolf error_setg_errno(errp, -ret, "Could not update backing file link"); 11776858eba0SKevin Wolf } 11786858eba0SKevin Wolf 1179e94d3dbaSAlberto Garcia if (read_only) { 1180e94d3dbaSAlberto Garcia bdrv_reopen_set_read_only(parent, true, NULL); 118161f09ceaSKevin Wolf } 118261f09ceaSKevin Wolf 11836858eba0SKevin Wolf return ret; 11846858eba0SKevin Wolf } 11856858eba0SKevin Wolf 118691ef3825SKevin Wolf const BdrvChildRole child_backing = { 11876cd5c9d7SKevin Wolf .parent_is_bds = true, 1188b5411555SKevin Wolf .get_parent_desc = bdrv_child_get_parent_desc, 1189db95dbbaSKevin Wolf .attach = bdrv_backing_attach, 1190db95dbbaSKevin Wolf .detach = bdrv_backing_detach, 11918e2160e2SKevin Wolf .inherit_options = bdrv_backing_options, 119220018e12SKevin Wolf .drained_begin = bdrv_child_cb_drained_begin, 119389bd0305SKevin Wolf .drained_poll = bdrv_child_cb_drained_poll, 119420018e12SKevin Wolf .drained_end = bdrv_child_cb_drained_end, 119538701b6aSKevin Wolf .inactivate = bdrv_child_cb_inactivate, 11966858eba0SKevin Wolf .update_filename = bdrv_backing_update_filename, 11975d231849SKevin Wolf .can_set_aio_ctx = bdrv_child_cb_can_set_aio_ctx, 119853a7d041SKevin Wolf .set_aio_ctx = bdrv_child_cb_set_aio_ctx, 1199f3930ed0SKevin Wolf }; 1200f3930ed0SKevin Wolf 12017b272452SKevin Wolf static int bdrv_open_flags(BlockDriverState *bs, int flags) 12027b272452SKevin Wolf { 120361de4c68SKevin Wolf int open_flags = flags; 12047b272452SKevin Wolf 12057b272452SKevin Wolf /* 12067b272452SKevin Wolf * Clear flags that are internal to the block layer before opening the 12077b272452SKevin Wolf * image. 12087b272452SKevin Wolf */ 120920cca275SKevin Wolf open_flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_PROTOCOL); 12107b272452SKevin Wolf 12117b272452SKevin Wolf return open_flags; 12127b272452SKevin Wolf } 12137b272452SKevin Wolf 121491a097e7SKevin Wolf static void update_flags_from_options(int *flags, QemuOpts *opts) 121591a097e7SKevin Wolf { 12162a3d4331SAlberto Garcia *flags &= ~(BDRV_O_CACHE_MASK | BDRV_O_RDWR | BDRV_O_AUTO_RDONLY); 121791a097e7SKevin Wolf 121857f9db9aSAlberto Garcia if (qemu_opt_get_bool_del(opts, BDRV_OPT_CACHE_NO_FLUSH, false)) { 121991a097e7SKevin Wolf *flags |= BDRV_O_NO_FLUSH; 122091a097e7SKevin Wolf } 122191a097e7SKevin Wolf 122257f9db9aSAlberto Garcia if (qemu_opt_get_bool_del(opts, BDRV_OPT_CACHE_DIRECT, false)) { 122391a097e7SKevin Wolf *flags |= BDRV_O_NOCACHE; 122491a097e7SKevin Wolf } 1225f87a0e29SAlberto Garcia 122657f9db9aSAlberto Garcia if (!qemu_opt_get_bool_del(opts, BDRV_OPT_READ_ONLY, false)) { 1227f87a0e29SAlberto Garcia *flags |= BDRV_O_RDWR; 1228f87a0e29SAlberto Garcia } 1229f87a0e29SAlberto Garcia 1230e35bdc12SKevin Wolf if (qemu_opt_get_bool_del(opts, BDRV_OPT_AUTO_READ_ONLY, false)) { 1231e35bdc12SKevin Wolf *flags |= BDRV_O_AUTO_RDONLY; 1232e35bdc12SKevin Wolf } 123391a097e7SKevin Wolf } 123491a097e7SKevin Wolf 123591a097e7SKevin Wolf static void update_options_from_flags(QDict *options, int flags) 123691a097e7SKevin Wolf { 123791a097e7SKevin Wolf if (!qdict_haskey(options, BDRV_OPT_CACHE_DIRECT)) { 123846f5ac20SEric Blake qdict_put_bool(options, BDRV_OPT_CACHE_DIRECT, flags & BDRV_O_NOCACHE); 123991a097e7SKevin Wolf } 124091a097e7SKevin Wolf if (!qdict_haskey(options, BDRV_OPT_CACHE_NO_FLUSH)) { 124146f5ac20SEric Blake qdict_put_bool(options, BDRV_OPT_CACHE_NO_FLUSH, 124246f5ac20SEric Blake flags & BDRV_O_NO_FLUSH); 124391a097e7SKevin Wolf } 1244f87a0e29SAlberto Garcia if (!qdict_haskey(options, BDRV_OPT_READ_ONLY)) { 124546f5ac20SEric Blake qdict_put_bool(options, BDRV_OPT_READ_ONLY, !(flags & BDRV_O_RDWR)); 1246f87a0e29SAlberto Garcia } 1247e35bdc12SKevin Wolf if (!qdict_haskey(options, BDRV_OPT_AUTO_READ_ONLY)) { 1248e35bdc12SKevin Wolf qdict_put_bool(options, BDRV_OPT_AUTO_READ_ONLY, 1249e35bdc12SKevin Wolf flags & BDRV_O_AUTO_RDONLY); 1250e35bdc12SKevin Wolf } 125191a097e7SKevin Wolf } 125291a097e7SKevin Wolf 1253636ea370SKevin Wolf static void bdrv_assign_node_name(BlockDriverState *bs, 12546913c0c2SBenoît Canet const char *node_name, 12556913c0c2SBenoît Canet Error **errp) 12566913c0c2SBenoît Canet { 125715489c76SJeff Cody char *gen_node_name = NULL; 12586913c0c2SBenoît Canet 125915489c76SJeff Cody if (!node_name) { 126015489c76SJeff Cody node_name = gen_node_name = id_generate(ID_BLOCK); 126115489c76SJeff Cody } else if (!id_wellformed(node_name)) { 126215489c76SJeff Cody /* 126315489c76SJeff Cody * Check for empty string or invalid characters, but not if it is 126415489c76SJeff Cody * generated (generated names use characters not available to the user) 126515489c76SJeff Cody */ 12669aebf3b8SKevin Wolf error_setg(errp, "Invalid node name"); 1267636ea370SKevin Wolf return; 12686913c0c2SBenoît Canet } 12696913c0c2SBenoît Canet 12700c5e94eeSBenoît Canet /* takes care of avoiding namespaces collisions */ 12717f06d47eSMarkus Armbruster if (blk_by_name(node_name)) { 12720c5e94eeSBenoît Canet error_setg(errp, "node-name=%s is conflicting with a device id", 12730c5e94eeSBenoît Canet node_name); 127415489c76SJeff Cody goto out; 12750c5e94eeSBenoît Canet } 12760c5e94eeSBenoît Canet 12776913c0c2SBenoît Canet /* takes care of avoiding duplicates node names */ 12786913c0c2SBenoît Canet if (bdrv_find_node(node_name)) { 12796913c0c2SBenoît Canet error_setg(errp, "Duplicate node name"); 128015489c76SJeff Cody goto out; 12816913c0c2SBenoît Canet } 12826913c0c2SBenoît Canet 1283824808ddSKevin Wolf /* Make sure that the node name isn't truncated */ 1284824808ddSKevin Wolf if (strlen(node_name) >= sizeof(bs->node_name)) { 1285824808ddSKevin Wolf error_setg(errp, "Node name too long"); 1286824808ddSKevin Wolf goto out; 1287824808ddSKevin Wolf } 1288824808ddSKevin Wolf 12896913c0c2SBenoît Canet /* copy node name into the bs and insert it into the graph list */ 12906913c0c2SBenoît Canet pstrcpy(bs->node_name, sizeof(bs->node_name), node_name); 12916913c0c2SBenoît Canet QTAILQ_INSERT_TAIL(&graph_bdrv_states, bs, node_list); 129215489c76SJeff Cody out: 129315489c76SJeff Cody g_free(gen_node_name); 12946913c0c2SBenoît Canet } 12956913c0c2SBenoît Canet 129601a56501SKevin Wolf static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv, 129701a56501SKevin Wolf const char *node_name, QDict *options, 129801a56501SKevin Wolf int open_flags, Error **errp) 129901a56501SKevin Wolf { 130001a56501SKevin Wolf Error *local_err = NULL; 13010f12264eSKevin Wolf int i, ret; 130201a56501SKevin Wolf 130301a56501SKevin Wolf bdrv_assign_node_name(bs, node_name, &local_err); 130401a56501SKevin Wolf if (local_err) { 130501a56501SKevin Wolf error_propagate(errp, local_err); 130601a56501SKevin Wolf return -EINVAL; 130701a56501SKevin Wolf } 130801a56501SKevin Wolf 130901a56501SKevin Wolf bs->drv = drv; 1310680c7f96SKevin Wolf bs->read_only = !(bs->open_flags & BDRV_O_RDWR); 131101a56501SKevin Wolf bs->opaque = g_malloc0(drv->instance_size); 131201a56501SKevin Wolf 131301a56501SKevin Wolf if (drv->bdrv_file_open) { 131401a56501SKevin Wolf assert(!drv->bdrv_needs_filename || bs->filename[0]); 131501a56501SKevin Wolf ret = drv->bdrv_file_open(bs, options, open_flags, &local_err); 1316680c7f96SKevin Wolf } else if (drv->bdrv_open) { 131701a56501SKevin Wolf ret = drv->bdrv_open(bs, options, open_flags, &local_err); 1318680c7f96SKevin Wolf } else { 1319680c7f96SKevin Wolf ret = 0; 132001a56501SKevin Wolf } 132101a56501SKevin Wolf 132201a56501SKevin Wolf if (ret < 0) { 132301a56501SKevin Wolf if (local_err) { 132401a56501SKevin Wolf error_propagate(errp, local_err); 132501a56501SKevin Wolf } else if (bs->filename[0]) { 132601a56501SKevin Wolf error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename); 132701a56501SKevin Wolf } else { 132801a56501SKevin Wolf error_setg_errno(errp, -ret, "Could not open image"); 132901a56501SKevin Wolf } 1330180ca19aSManos Pitsidianakis goto open_failed; 133101a56501SKevin Wolf } 133201a56501SKevin Wolf 133301a56501SKevin Wolf ret = refresh_total_sectors(bs, bs->total_sectors); 133401a56501SKevin Wolf if (ret < 0) { 133501a56501SKevin Wolf error_setg_errno(errp, -ret, "Could not refresh total sector count"); 1336180ca19aSManos Pitsidianakis return ret; 133701a56501SKevin Wolf } 133801a56501SKevin Wolf 133901a56501SKevin Wolf bdrv_refresh_limits(bs, &local_err); 134001a56501SKevin Wolf if (local_err) { 134101a56501SKevin Wolf error_propagate(errp, local_err); 1342180ca19aSManos Pitsidianakis return -EINVAL; 134301a56501SKevin Wolf } 134401a56501SKevin Wolf 134501a56501SKevin Wolf assert(bdrv_opt_mem_align(bs) != 0); 134601a56501SKevin Wolf assert(bdrv_min_mem_align(bs) != 0); 134701a56501SKevin Wolf assert(is_power_of_2(bs->bl.request_alignment)); 134801a56501SKevin Wolf 13490f12264eSKevin Wolf for (i = 0; i < bs->quiesce_counter; i++) { 13500f12264eSKevin Wolf if (drv->bdrv_co_drain_begin) { 13510f12264eSKevin Wolf drv->bdrv_co_drain_begin(bs); 13520f12264eSKevin Wolf } 13530f12264eSKevin Wolf } 13540f12264eSKevin Wolf 135501a56501SKevin Wolf return 0; 1356180ca19aSManos Pitsidianakis open_failed: 1357180ca19aSManos Pitsidianakis bs->drv = NULL; 1358180ca19aSManos Pitsidianakis if (bs->file != NULL) { 1359180ca19aSManos Pitsidianakis bdrv_unref_child(bs, bs->file); 1360180ca19aSManos Pitsidianakis bs->file = NULL; 1361180ca19aSManos Pitsidianakis } 136201a56501SKevin Wolf g_free(bs->opaque); 136301a56501SKevin Wolf bs->opaque = NULL; 136401a56501SKevin Wolf return ret; 136501a56501SKevin Wolf } 136601a56501SKevin Wolf 1367680c7f96SKevin Wolf BlockDriverState *bdrv_new_open_driver(BlockDriver *drv, const char *node_name, 1368680c7f96SKevin Wolf int flags, Error **errp) 1369680c7f96SKevin Wolf { 1370680c7f96SKevin Wolf BlockDriverState *bs; 1371680c7f96SKevin Wolf int ret; 1372680c7f96SKevin Wolf 1373680c7f96SKevin Wolf bs = bdrv_new(); 1374680c7f96SKevin Wolf bs->open_flags = flags; 1375680c7f96SKevin Wolf bs->explicit_options = qdict_new(); 1376680c7f96SKevin Wolf bs->options = qdict_new(); 1377680c7f96SKevin Wolf bs->opaque = NULL; 1378680c7f96SKevin Wolf 1379680c7f96SKevin Wolf update_options_from_flags(bs->options, flags); 1380680c7f96SKevin Wolf 1381680c7f96SKevin Wolf ret = bdrv_open_driver(bs, drv, node_name, bs->options, flags, errp); 1382680c7f96SKevin Wolf if (ret < 0) { 1383cb3e7f08SMarc-André Lureau qobject_unref(bs->explicit_options); 1384180ca19aSManos Pitsidianakis bs->explicit_options = NULL; 1385cb3e7f08SMarc-André Lureau qobject_unref(bs->options); 1386180ca19aSManos Pitsidianakis bs->options = NULL; 1387680c7f96SKevin Wolf bdrv_unref(bs); 1388680c7f96SKevin Wolf return NULL; 1389680c7f96SKevin Wolf } 1390680c7f96SKevin Wolf 1391680c7f96SKevin Wolf return bs; 1392680c7f96SKevin Wolf } 1393680c7f96SKevin Wolf 1394c5f3014bSKevin Wolf QemuOptsList bdrv_runtime_opts = { 139518edf289SKevin Wolf .name = "bdrv_common", 139618edf289SKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(bdrv_runtime_opts.head), 139718edf289SKevin Wolf .desc = { 139818edf289SKevin Wolf { 139918edf289SKevin Wolf .name = "node-name", 140018edf289SKevin Wolf .type = QEMU_OPT_STRING, 140118edf289SKevin Wolf .help = "Node name of the block device node", 140218edf289SKevin Wolf }, 140362392ebbSKevin Wolf { 140462392ebbSKevin Wolf .name = "driver", 140562392ebbSKevin Wolf .type = QEMU_OPT_STRING, 140662392ebbSKevin Wolf .help = "Block driver to use for the node", 140762392ebbSKevin Wolf }, 140891a097e7SKevin Wolf { 140991a097e7SKevin Wolf .name = BDRV_OPT_CACHE_DIRECT, 141091a097e7SKevin Wolf .type = QEMU_OPT_BOOL, 141191a097e7SKevin Wolf .help = "Bypass software writeback cache on the host", 141291a097e7SKevin Wolf }, 141391a097e7SKevin Wolf { 141491a097e7SKevin Wolf .name = BDRV_OPT_CACHE_NO_FLUSH, 141591a097e7SKevin Wolf .type = QEMU_OPT_BOOL, 141691a097e7SKevin Wolf .help = "Ignore flush requests", 141791a097e7SKevin Wolf }, 1418f87a0e29SAlberto Garcia { 1419f87a0e29SAlberto Garcia .name = BDRV_OPT_READ_ONLY, 1420f87a0e29SAlberto Garcia .type = QEMU_OPT_BOOL, 1421f87a0e29SAlberto Garcia .help = "Node is opened in read-only mode", 1422f87a0e29SAlberto Garcia }, 1423692e01a2SKevin Wolf { 1424e35bdc12SKevin Wolf .name = BDRV_OPT_AUTO_READ_ONLY, 1425e35bdc12SKevin Wolf .type = QEMU_OPT_BOOL, 1426e35bdc12SKevin Wolf .help = "Node can become read-only if opening read-write fails", 1427e35bdc12SKevin Wolf }, 1428e35bdc12SKevin Wolf { 1429692e01a2SKevin Wolf .name = "detect-zeroes", 1430692e01a2SKevin Wolf .type = QEMU_OPT_STRING, 1431692e01a2SKevin Wolf .help = "try to optimize zero writes (off, on, unmap)", 1432692e01a2SKevin Wolf }, 1433818584a4SKevin Wolf { 1434415bbca8SAlberto Garcia .name = BDRV_OPT_DISCARD, 1435818584a4SKevin Wolf .type = QEMU_OPT_STRING, 1436818584a4SKevin Wolf .help = "discard operation (ignore/off, unmap/on)", 1437818584a4SKevin Wolf }, 14385a9347c6SFam Zheng { 14395a9347c6SFam Zheng .name = BDRV_OPT_FORCE_SHARE, 14405a9347c6SFam Zheng .type = QEMU_OPT_BOOL, 14415a9347c6SFam Zheng .help = "always accept other writers (default: off)", 14425a9347c6SFam Zheng }, 144318edf289SKevin Wolf { /* end of list */ } 144418edf289SKevin Wolf }, 144518edf289SKevin Wolf }; 144618edf289SKevin Wolf 1447b6ce07aaSKevin Wolf /* 144857915332SKevin Wolf * Common part for opening disk images and files 1449b6ad491aSKevin Wolf * 1450b6ad491aSKevin Wolf * Removes all processed options from *options. 145157915332SKevin Wolf */ 14525696c6e3SKevin Wolf static int bdrv_open_common(BlockDriverState *bs, BlockBackend *file, 145382dc8b41SKevin Wolf QDict *options, Error **errp) 145457915332SKevin Wolf { 145557915332SKevin Wolf int ret, open_flags; 1456035fccdfSKevin Wolf const char *filename; 145762392ebbSKevin Wolf const char *driver_name = NULL; 14586913c0c2SBenoît Canet const char *node_name = NULL; 1459818584a4SKevin Wolf const char *discard; 146018edf289SKevin Wolf QemuOpts *opts; 146162392ebbSKevin Wolf BlockDriver *drv; 146234b5d2c6SMax Reitz Error *local_err = NULL; 146357915332SKevin Wolf 14646405875cSPaolo Bonzini assert(bs->file == NULL); 1465707ff828SKevin Wolf assert(options != NULL && bs->options != options); 146657915332SKevin Wolf 146762392ebbSKevin Wolf opts = qemu_opts_create(&bdrv_runtime_opts, NULL, 0, &error_abort); 146862392ebbSKevin Wolf qemu_opts_absorb_qdict(opts, options, &local_err); 146962392ebbSKevin Wolf if (local_err) { 147062392ebbSKevin Wolf error_propagate(errp, local_err); 147162392ebbSKevin Wolf ret = -EINVAL; 147262392ebbSKevin Wolf goto fail_opts; 147362392ebbSKevin Wolf } 147462392ebbSKevin Wolf 14759b7e8691SAlberto Garcia update_flags_from_options(&bs->open_flags, opts); 14769b7e8691SAlberto Garcia 147762392ebbSKevin Wolf driver_name = qemu_opt_get(opts, "driver"); 147862392ebbSKevin Wolf drv = bdrv_find_format(driver_name); 147962392ebbSKevin Wolf assert(drv != NULL); 148062392ebbSKevin Wolf 14815a9347c6SFam Zheng bs->force_share = qemu_opt_get_bool(opts, BDRV_OPT_FORCE_SHARE, false); 14825a9347c6SFam Zheng 14835a9347c6SFam Zheng if (bs->force_share && (bs->open_flags & BDRV_O_RDWR)) { 14845a9347c6SFam Zheng error_setg(errp, 14855a9347c6SFam Zheng BDRV_OPT_FORCE_SHARE 14865a9347c6SFam Zheng "=on can only be used with read-only images"); 14875a9347c6SFam Zheng ret = -EINVAL; 14885a9347c6SFam Zheng goto fail_opts; 14895a9347c6SFam Zheng } 14905a9347c6SFam Zheng 149145673671SKevin Wolf if (file != NULL) { 1492f30c66baSMax Reitz bdrv_refresh_filename(blk_bs(file)); 14935696c6e3SKevin Wolf filename = blk_bs(file)->filename; 149445673671SKevin Wolf } else { 1495129c7d1cSMarkus Armbruster /* 1496129c7d1cSMarkus Armbruster * Caution: while qdict_get_try_str() is fine, getting 1497129c7d1cSMarkus Armbruster * non-string types would require more care. When @options 1498129c7d1cSMarkus Armbruster * come from -blockdev or blockdev_add, its members are typed 1499129c7d1cSMarkus Armbruster * according to the QAPI schema, but when they come from 1500129c7d1cSMarkus Armbruster * -drive, they're all QString. 1501129c7d1cSMarkus Armbruster */ 150245673671SKevin Wolf filename = qdict_get_try_str(options, "filename"); 150345673671SKevin Wolf } 150445673671SKevin Wolf 15054a008240SMax Reitz if (drv->bdrv_needs_filename && (!filename || !filename[0])) { 1506765003dbSKevin Wolf error_setg(errp, "The '%s' block driver requires a file name", 1507765003dbSKevin Wolf drv->format_name); 150818edf289SKevin Wolf ret = -EINVAL; 150918edf289SKevin Wolf goto fail_opts; 151018edf289SKevin Wolf } 151118edf289SKevin Wolf 151282dc8b41SKevin Wolf trace_bdrv_open_common(bs, filename ?: "", bs->open_flags, 151382dc8b41SKevin Wolf drv->format_name); 151462392ebbSKevin Wolf 151582dc8b41SKevin Wolf bs->read_only = !(bs->open_flags & BDRV_O_RDWR); 1516b64ec4e4SFam Zheng 1517b64ec4e4SFam Zheng if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, bs->read_only)) { 15188be25de6SKevin Wolf if (!bs->read_only && bdrv_is_whitelisted(drv, true)) { 15198be25de6SKevin Wolf ret = bdrv_apply_auto_read_only(bs, NULL, NULL); 15208be25de6SKevin Wolf } else { 15218be25de6SKevin Wolf ret = -ENOTSUP; 15228be25de6SKevin Wolf } 15238be25de6SKevin Wolf if (ret < 0) { 15248f94a6e4SKevin Wolf error_setg(errp, 15258f94a6e4SKevin Wolf !bs->read_only && bdrv_is_whitelisted(drv, true) 15268f94a6e4SKevin Wolf ? "Driver '%s' can only be used for read-only devices" 15278f94a6e4SKevin Wolf : "Driver '%s' is not whitelisted", 15288f94a6e4SKevin Wolf drv->format_name); 152918edf289SKevin Wolf goto fail_opts; 1530b64ec4e4SFam Zheng } 15318be25de6SKevin Wolf } 153257915332SKevin Wolf 1533d3faa13eSPaolo Bonzini /* bdrv_new() and bdrv_close() make it so */ 1534d3faa13eSPaolo Bonzini assert(atomic_read(&bs->copy_on_read) == 0); 1535d3faa13eSPaolo Bonzini 153682dc8b41SKevin Wolf if (bs->open_flags & BDRV_O_COPY_ON_READ) { 15370ebd24e0SKevin Wolf if (!bs->read_only) { 153853fec9d3SStefan Hajnoczi bdrv_enable_copy_on_read(bs); 15390ebd24e0SKevin Wolf } else { 15400ebd24e0SKevin Wolf error_setg(errp, "Can't use copy-on-read on read-only device"); 154118edf289SKevin Wolf ret = -EINVAL; 154218edf289SKevin Wolf goto fail_opts; 15430ebd24e0SKevin Wolf } 154453fec9d3SStefan Hajnoczi } 154553fec9d3SStefan Hajnoczi 1546415bbca8SAlberto Garcia discard = qemu_opt_get(opts, BDRV_OPT_DISCARD); 1547818584a4SKevin Wolf if (discard != NULL) { 1548818584a4SKevin Wolf if (bdrv_parse_discard_flags(discard, &bs->open_flags) != 0) { 1549818584a4SKevin Wolf error_setg(errp, "Invalid discard option"); 1550818584a4SKevin Wolf ret = -EINVAL; 1551818584a4SKevin Wolf goto fail_opts; 1552818584a4SKevin Wolf } 1553818584a4SKevin Wolf } 1554818584a4SKevin Wolf 1555543770bdSAlberto Garcia bs->detect_zeroes = 1556543770bdSAlberto Garcia bdrv_parse_detect_zeroes(opts, bs->open_flags, &local_err); 1557692e01a2SKevin Wolf if (local_err) { 1558692e01a2SKevin Wolf error_propagate(errp, local_err); 1559692e01a2SKevin Wolf ret = -EINVAL; 1560692e01a2SKevin Wolf goto fail_opts; 1561692e01a2SKevin Wolf } 1562692e01a2SKevin Wolf 1563c2ad1b0cSKevin Wolf if (filename != NULL) { 156457915332SKevin Wolf pstrcpy(bs->filename, sizeof(bs->filename), filename); 1565c2ad1b0cSKevin Wolf } else { 1566c2ad1b0cSKevin Wolf bs->filename[0] = '\0'; 1567c2ad1b0cSKevin Wolf } 156891af7014SMax Reitz pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), bs->filename); 156957915332SKevin Wolf 157066f82ceeSKevin Wolf /* Open the image, either directly or using a protocol */ 157182dc8b41SKevin Wolf open_flags = bdrv_open_flags(bs, bs->open_flags); 157201a56501SKevin Wolf node_name = qemu_opt_get(opts, "node-name"); 157366f82ceeSKevin Wolf 157401a56501SKevin Wolf assert(!drv->bdrv_file_open || file == NULL); 157501a56501SKevin Wolf ret = bdrv_open_driver(bs, drv, node_name, options, open_flags, errp); 157657915332SKevin Wolf if (ret < 0) { 157701a56501SKevin Wolf goto fail_opts; 157834b5d2c6SMax Reitz } 157918edf289SKevin Wolf 158018edf289SKevin Wolf qemu_opts_del(opts); 158157915332SKevin Wolf return 0; 158257915332SKevin Wolf 158318edf289SKevin Wolf fail_opts: 158418edf289SKevin Wolf qemu_opts_del(opts); 158557915332SKevin Wolf return ret; 158657915332SKevin Wolf } 158757915332SKevin Wolf 15885e5c4f63SKevin Wolf static QDict *parse_json_filename(const char *filename, Error **errp) 15895e5c4f63SKevin Wolf { 15905e5c4f63SKevin Wolf QObject *options_obj; 15915e5c4f63SKevin Wolf QDict *options; 15925e5c4f63SKevin Wolf int ret; 15935e5c4f63SKevin Wolf 15945e5c4f63SKevin Wolf ret = strstart(filename, "json:", &filename); 15955e5c4f63SKevin Wolf assert(ret); 15965e5c4f63SKevin Wolf 15975577fff7SMarkus Armbruster options_obj = qobject_from_json(filename, errp); 15985e5c4f63SKevin Wolf if (!options_obj) { 15995577fff7SMarkus Armbruster error_prepend(errp, "Could not parse the JSON options: "); 16005577fff7SMarkus Armbruster return NULL; 16015577fff7SMarkus Armbruster } 16025e5c4f63SKevin Wolf 16037dc847ebSMax Reitz options = qobject_to(QDict, options_obj); 1604ca6b6e1eSMarkus Armbruster if (!options) { 1605cb3e7f08SMarc-André Lureau qobject_unref(options_obj); 16065e5c4f63SKevin Wolf error_setg(errp, "Invalid JSON object given"); 16075e5c4f63SKevin Wolf return NULL; 16085e5c4f63SKevin Wolf } 16095e5c4f63SKevin Wolf 16105e5c4f63SKevin Wolf qdict_flatten(options); 16115e5c4f63SKevin Wolf 16125e5c4f63SKevin Wolf return options; 16135e5c4f63SKevin Wolf } 16145e5c4f63SKevin Wolf 1615de3b53f0SKevin Wolf static void parse_json_protocol(QDict *options, const char **pfilename, 1616de3b53f0SKevin Wolf Error **errp) 1617de3b53f0SKevin Wolf { 1618de3b53f0SKevin Wolf QDict *json_options; 1619de3b53f0SKevin Wolf Error *local_err = NULL; 1620de3b53f0SKevin Wolf 1621de3b53f0SKevin Wolf /* Parse json: pseudo-protocol */ 1622de3b53f0SKevin Wolf if (!*pfilename || !g_str_has_prefix(*pfilename, "json:")) { 1623de3b53f0SKevin Wolf return; 1624de3b53f0SKevin Wolf } 1625de3b53f0SKevin Wolf 1626de3b53f0SKevin Wolf json_options = parse_json_filename(*pfilename, &local_err); 1627de3b53f0SKevin Wolf if (local_err) { 1628de3b53f0SKevin Wolf error_propagate(errp, local_err); 1629de3b53f0SKevin Wolf return; 1630de3b53f0SKevin Wolf } 1631de3b53f0SKevin Wolf 1632de3b53f0SKevin Wolf /* Options given in the filename have lower priority than options 1633de3b53f0SKevin Wolf * specified directly */ 1634de3b53f0SKevin Wolf qdict_join(options, json_options, false); 1635cb3e7f08SMarc-André Lureau qobject_unref(json_options); 1636de3b53f0SKevin Wolf *pfilename = NULL; 1637de3b53f0SKevin Wolf } 1638de3b53f0SKevin Wolf 163957915332SKevin Wolf /* 1640f54120ffSKevin Wolf * Fills in default options for opening images and converts the legacy 1641f54120ffSKevin Wolf * filename/flags pair to option QDict entries. 164253a29513SMax Reitz * The BDRV_O_PROTOCOL flag in *flags will be set or cleared accordingly if a 164353a29513SMax Reitz * block driver has been specified explicitly. 1644f54120ffSKevin Wolf */ 1645de3b53f0SKevin Wolf static int bdrv_fill_options(QDict **options, const char *filename, 1646053e1578SMax Reitz int *flags, Error **errp) 1647f54120ffSKevin Wolf { 1648f54120ffSKevin Wolf const char *drvname; 164953a29513SMax Reitz bool protocol = *flags & BDRV_O_PROTOCOL; 1650f54120ffSKevin Wolf bool parse_filename = false; 1651053e1578SMax Reitz BlockDriver *drv = NULL; 1652f54120ffSKevin Wolf Error *local_err = NULL; 1653f54120ffSKevin Wolf 1654129c7d1cSMarkus Armbruster /* 1655129c7d1cSMarkus Armbruster * Caution: while qdict_get_try_str() is fine, getting non-string 1656129c7d1cSMarkus Armbruster * types would require more care. When @options come from 1657129c7d1cSMarkus Armbruster * -blockdev or blockdev_add, its members are typed according to 1658129c7d1cSMarkus Armbruster * the QAPI schema, but when they come from -drive, they're all 1659129c7d1cSMarkus Armbruster * QString. 1660129c7d1cSMarkus Armbruster */ 166153a29513SMax Reitz drvname = qdict_get_try_str(*options, "driver"); 1662053e1578SMax Reitz if (drvname) { 1663053e1578SMax Reitz drv = bdrv_find_format(drvname); 1664053e1578SMax Reitz if (!drv) { 1665053e1578SMax Reitz error_setg(errp, "Unknown driver '%s'", drvname); 1666053e1578SMax Reitz return -ENOENT; 1667053e1578SMax Reitz } 166853a29513SMax Reitz /* If the user has explicitly specified the driver, this choice should 166953a29513SMax Reitz * override the BDRV_O_PROTOCOL flag */ 1670053e1578SMax Reitz protocol = drv->bdrv_file_open; 167153a29513SMax Reitz } 167253a29513SMax Reitz 167353a29513SMax Reitz if (protocol) { 167453a29513SMax Reitz *flags |= BDRV_O_PROTOCOL; 167553a29513SMax Reitz } else { 167653a29513SMax Reitz *flags &= ~BDRV_O_PROTOCOL; 167753a29513SMax Reitz } 167853a29513SMax Reitz 167991a097e7SKevin Wolf /* Translate cache options from flags into options */ 168091a097e7SKevin Wolf update_options_from_flags(*options, *flags); 168191a097e7SKevin Wolf 1682f54120ffSKevin Wolf /* Fetch the file name from the options QDict if necessary */ 168317b005f1SKevin Wolf if (protocol && filename) { 1684f54120ffSKevin Wolf if (!qdict_haskey(*options, "filename")) { 168546f5ac20SEric Blake qdict_put_str(*options, "filename", filename); 1686f54120ffSKevin Wolf parse_filename = true; 1687f54120ffSKevin Wolf } else { 1688f54120ffSKevin Wolf error_setg(errp, "Can't specify 'file' and 'filename' options at " 1689f54120ffSKevin Wolf "the same time"); 1690f54120ffSKevin Wolf return -EINVAL; 1691f54120ffSKevin Wolf } 1692f54120ffSKevin Wolf } 1693f54120ffSKevin Wolf 1694f54120ffSKevin Wolf /* Find the right block driver */ 1695129c7d1cSMarkus Armbruster /* See cautionary note on accessing @options above */ 1696f54120ffSKevin Wolf filename = qdict_get_try_str(*options, "filename"); 1697f54120ffSKevin Wolf 169817b005f1SKevin Wolf if (!drvname && protocol) { 1699f54120ffSKevin Wolf if (filename) { 1700b65a5e12SMax Reitz drv = bdrv_find_protocol(filename, parse_filename, errp); 1701f54120ffSKevin Wolf if (!drv) { 1702f54120ffSKevin Wolf return -EINVAL; 1703f54120ffSKevin Wolf } 1704f54120ffSKevin Wolf 1705f54120ffSKevin Wolf drvname = drv->format_name; 170646f5ac20SEric Blake qdict_put_str(*options, "driver", drvname); 1707f54120ffSKevin Wolf } else { 1708f54120ffSKevin Wolf error_setg(errp, "Must specify either driver or file"); 1709f54120ffSKevin Wolf return -EINVAL; 1710f54120ffSKevin Wolf } 171117b005f1SKevin Wolf } 171217b005f1SKevin Wolf 171317b005f1SKevin Wolf assert(drv || !protocol); 1714f54120ffSKevin Wolf 1715f54120ffSKevin Wolf /* Driver-specific filename parsing */ 171617b005f1SKevin Wolf if (drv && drv->bdrv_parse_filename && parse_filename) { 1717f54120ffSKevin Wolf drv->bdrv_parse_filename(filename, *options, &local_err); 1718f54120ffSKevin Wolf if (local_err) { 1719f54120ffSKevin Wolf error_propagate(errp, local_err); 1720f54120ffSKevin Wolf return -EINVAL; 1721f54120ffSKevin Wolf } 1722f54120ffSKevin Wolf 1723f54120ffSKevin Wolf if (!drv->bdrv_needs_filename) { 1724f54120ffSKevin Wolf qdict_del(*options, "filename"); 1725f54120ffSKevin Wolf } 1726f54120ffSKevin Wolf } 1727f54120ffSKevin Wolf 1728f54120ffSKevin Wolf return 0; 1729f54120ffSKevin Wolf } 1730f54120ffSKevin Wolf 17313121fb45SKevin Wolf static int bdrv_child_check_perm(BdrvChild *c, BlockReopenQueue *q, 17323121fb45SKevin Wolf uint64_t perm, uint64_t shared, 17339eab1544SMax Reitz GSList *ignore_children, 17349eab1544SMax Reitz bool *tighten_restrictions, Error **errp); 1735c1cef672SFam Zheng static void bdrv_child_abort_perm_update(BdrvChild *c); 1736c1cef672SFam Zheng static void bdrv_child_set_perm(BdrvChild *c, uint64_t perm, uint64_t shared); 1737481e0eeeSMax Reitz static void bdrv_get_cumulative_perm(BlockDriverState *bs, uint64_t *perm, 1738481e0eeeSMax Reitz uint64_t *shared_perm); 1739c1cef672SFam Zheng 1740148eb13cSKevin Wolf typedef struct BlockReopenQueueEntry { 1741148eb13cSKevin Wolf bool prepared; 174269b736e7SKevin Wolf bool perms_checked; 1743148eb13cSKevin Wolf BDRVReopenState state; 1744859443b0SVladimir Sementsov-Ogievskiy QTAILQ_ENTRY(BlockReopenQueueEntry) entry; 1745148eb13cSKevin Wolf } BlockReopenQueueEntry; 1746148eb13cSKevin Wolf 1747148eb13cSKevin Wolf /* 1748148eb13cSKevin Wolf * Return the flags that @bs will have after the reopens in @q have 1749148eb13cSKevin Wolf * successfully completed. If @q is NULL (or @bs is not contained in @q), 1750148eb13cSKevin Wolf * return the current flags. 1751148eb13cSKevin Wolf */ 1752148eb13cSKevin Wolf static int bdrv_reopen_get_flags(BlockReopenQueue *q, BlockDriverState *bs) 1753148eb13cSKevin Wolf { 1754148eb13cSKevin Wolf BlockReopenQueueEntry *entry; 1755148eb13cSKevin Wolf 1756148eb13cSKevin Wolf if (q != NULL) { 1757859443b0SVladimir Sementsov-Ogievskiy QTAILQ_FOREACH(entry, q, entry) { 1758148eb13cSKevin Wolf if (entry->state.bs == bs) { 1759148eb13cSKevin Wolf return entry->state.flags; 1760148eb13cSKevin Wolf } 1761148eb13cSKevin Wolf } 1762148eb13cSKevin Wolf } 1763148eb13cSKevin Wolf 1764148eb13cSKevin Wolf return bs->open_flags; 1765148eb13cSKevin Wolf } 1766148eb13cSKevin Wolf 1767148eb13cSKevin Wolf /* Returns whether the image file can be written to after the reopen queue @q 1768148eb13cSKevin Wolf * has been successfully applied, or right now if @q is NULL. */ 1769cc022140SMax Reitz static bool bdrv_is_writable_after_reopen(BlockDriverState *bs, 1770cc022140SMax Reitz BlockReopenQueue *q) 1771148eb13cSKevin Wolf { 1772148eb13cSKevin Wolf int flags = bdrv_reopen_get_flags(q, bs); 1773148eb13cSKevin Wolf 1774148eb13cSKevin Wolf return (flags & (BDRV_O_RDWR | BDRV_O_INACTIVE)) == BDRV_O_RDWR; 1775148eb13cSKevin Wolf } 1776148eb13cSKevin Wolf 1777cc022140SMax Reitz /* 1778cc022140SMax Reitz * Return whether the BDS can be written to. This is not necessarily 1779cc022140SMax Reitz * the same as !bdrv_is_read_only(bs), as inactivated images may not 1780cc022140SMax Reitz * be written to but do not count as read-only images. 1781cc022140SMax Reitz */ 1782cc022140SMax Reitz bool bdrv_is_writable(BlockDriverState *bs) 1783cc022140SMax Reitz { 1784cc022140SMax Reitz return bdrv_is_writable_after_reopen(bs, NULL); 1785cc022140SMax Reitz } 1786cc022140SMax Reitz 1787ffd1a5a2SFam Zheng static void bdrv_child_perm(BlockDriverState *bs, BlockDriverState *child_bs, 1788e0995dc3SKevin Wolf BdrvChild *c, const BdrvChildRole *role, 1789e0995dc3SKevin Wolf BlockReopenQueue *reopen_queue, 1790ffd1a5a2SFam Zheng uint64_t parent_perm, uint64_t parent_shared, 1791ffd1a5a2SFam Zheng uint64_t *nperm, uint64_t *nshared) 1792ffd1a5a2SFam Zheng { 17930b3ca76eSAlberto Garcia assert(bs->drv && bs->drv->bdrv_child_perm); 1794e0995dc3SKevin Wolf bs->drv->bdrv_child_perm(bs, c, role, reopen_queue, 1795ffd1a5a2SFam Zheng parent_perm, parent_shared, 1796ffd1a5a2SFam Zheng nperm, nshared); 1797e0995dc3SKevin Wolf /* TODO Take force_share from reopen_queue */ 1798ffd1a5a2SFam Zheng if (child_bs && child_bs->force_share) { 1799ffd1a5a2SFam Zheng *nshared = BLK_PERM_ALL; 1800ffd1a5a2SFam Zheng } 1801ffd1a5a2SFam Zheng } 1802ffd1a5a2SFam Zheng 180333a610c3SKevin Wolf /* 180433a610c3SKevin Wolf * Check whether permissions on this node can be changed in a way that 180533a610c3SKevin Wolf * @cumulative_perms and @cumulative_shared_perms are the new cumulative 180633a610c3SKevin Wolf * permissions of all its parents. This involves checking whether all necessary 180733a610c3SKevin Wolf * permission changes to child nodes can be performed. 180833a610c3SKevin Wolf * 18099eab1544SMax Reitz * Will set *tighten_restrictions to true if and only if new permissions have to 18109eab1544SMax Reitz * be taken or currently shared permissions are to be unshared. Otherwise, 18119eab1544SMax Reitz * errors are not fatal as long as the caller accepts that the restrictions 18129eab1544SMax Reitz * remain tighter than they need to be. The caller still has to abort the 18139eab1544SMax Reitz * transaction. 18149eab1544SMax Reitz * @tighten_restrictions cannot be used together with @q: When reopening, we may 18159eab1544SMax Reitz * encounter fatal errors even though no restrictions are to be tightened. For 18169eab1544SMax Reitz * example, changing a node from RW to RO will fail if the WRITE permission is 18179eab1544SMax Reitz * to be kept. 18189eab1544SMax Reitz * 181933a610c3SKevin Wolf * A call to this function must always be followed by a call to bdrv_set_perm() 182033a610c3SKevin Wolf * or bdrv_abort_perm_update(). 182133a610c3SKevin Wolf */ 18223121fb45SKevin Wolf static int bdrv_check_perm(BlockDriverState *bs, BlockReopenQueue *q, 18233121fb45SKevin Wolf uint64_t cumulative_perms, 182446181129SKevin Wolf uint64_t cumulative_shared_perms, 18259eab1544SMax Reitz GSList *ignore_children, 18269eab1544SMax Reitz bool *tighten_restrictions, Error **errp) 182733a610c3SKevin Wolf { 182833a610c3SKevin Wolf BlockDriver *drv = bs->drv; 182933a610c3SKevin Wolf BdrvChild *c; 183033a610c3SKevin Wolf int ret; 183133a610c3SKevin Wolf 18329eab1544SMax Reitz assert(!q || !tighten_restrictions); 18339eab1544SMax Reitz 18349eab1544SMax Reitz if (tighten_restrictions) { 18359eab1544SMax Reitz uint64_t current_perms, current_shared; 18369eab1544SMax Reitz uint64_t added_perms, removed_shared_perms; 18379eab1544SMax Reitz 18389eab1544SMax Reitz bdrv_get_cumulative_perm(bs, ¤t_perms, ¤t_shared); 18399eab1544SMax Reitz 18409eab1544SMax Reitz added_perms = cumulative_perms & ~current_perms; 18419eab1544SMax Reitz removed_shared_perms = current_shared & ~cumulative_shared_perms; 18429eab1544SMax Reitz 18439eab1544SMax Reitz *tighten_restrictions = added_perms || removed_shared_perms; 18449eab1544SMax Reitz } 18459eab1544SMax Reitz 184633a610c3SKevin Wolf /* Write permissions never work with read-only images */ 184733a610c3SKevin Wolf if ((cumulative_perms & (BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED)) && 1848cc022140SMax Reitz !bdrv_is_writable_after_reopen(bs, q)) 184933a610c3SKevin Wolf { 1850481e0eeeSMax Reitz if (!bdrv_is_writable_after_reopen(bs, NULL)) { 185133a610c3SKevin Wolf error_setg(errp, "Block node is read-only"); 1852481e0eeeSMax Reitz } else { 1853481e0eeeSMax Reitz uint64_t current_perms, current_shared; 1854481e0eeeSMax Reitz bdrv_get_cumulative_perm(bs, ¤t_perms, ¤t_shared); 1855481e0eeeSMax Reitz if (current_perms & (BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED)) { 1856481e0eeeSMax Reitz error_setg(errp, "Cannot make block node read-only, there is " 1857481e0eeeSMax Reitz "a writer on it"); 1858481e0eeeSMax Reitz } else { 1859481e0eeeSMax Reitz error_setg(errp, "Cannot make block node read-only and create " 1860481e0eeeSMax Reitz "a writer on it"); 1861481e0eeeSMax Reitz } 1862481e0eeeSMax Reitz } 1863481e0eeeSMax Reitz 186433a610c3SKevin Wolf return -EPERM; 186533a610c3SKevin Wolf } 186633a610c3SKevin Wolf 186733a610c3SKevin Wolf /* Check this node */ 186833a610c3SKevin Wolf if (!drv) { 186933a610c3SKevin Wolf return 0; 187033a610c3SKevin Wolf } 187133a610c3SKevin Wolf 187233a610c3SKevin Wolf if (drv->bdrv_check_perm) { 187333a610c3SKevin Wolf return drv->bdrv_check_perm(bs, cumulative_perms, 187433a610c3SKevin Wolf cumulative_shared_perms, errp); 187533a610c3SKevin Wolf } 187633a610c3SKevin Wolf 187778e421c9SKevin Wolf /* Drivers that never have children can omit .bdrv_child_perm() */ 187833a610c3SKevin Wolf if (!drv->bdrv_child_perm) { 187978e421c9SKevin Wolf assert(QLIST_EMPTY(&bs->children)); 188033a610c3SKevin Wolf return 0; 188133a610c3SKevin Wolf } 188233a610c3SKevin Wolf 188333a610c3SKevin Wolf /* Check all children */ 188433a610c3SKevin Wolf QLIST_FOREACH(c, &bs->children, next) { 188533a610c3SKevin Wolf uint64_t cur_perm, cur_shared; 18869eab1544SMax Reitz bool child_tighten_restr; 18879eab1544SMax Reitz 18883121fb45SKevin Wolf bdrv_child_perm(bs, c->bs, c, c->role, q, 188933a610c3SKevin Wolf cumulative_perms, cumulative_shared_perms, 189033a610c3SKevin Wolf &cur_perm, &cur_shared); 18919eab1544SMax Reitz ret = bdrv_child_check_perm(c, q, cur_perm, cur_shared, ignore_children, 18929eab1544SMax Reitz tighten_restrictions ? &child_tighten_restr 18939eab1544SMax Reitz : NULL, 18949eab1544SMax Reitz errp); 18959eab1544SMax Reitz if (tighten_restrictions) { 18969eab1544SMax Reitz *tighten_restrictions |= child_tighten_restr; 18979eab1544SMax Reitz } 189833a610c3SKevin Wolf if (ret < 0) { 189933a610c3SKevin Wolf return ret; 190033a610c3SKevin Wolf } 190133a610c3SKevin Wolf } 190233a610c3SKevin Wolf 190333a610c3SKevin Wolf return 0; 190433a610c3SKevin Wolf } 190533a610c3SKevin Wolf 190633a610c3SKevin Wolf /* 190733a610c3SKevin Wolf * Notifies drivers that after a previous bdrv_check_perm() call, the 190833a610c3SKevin Wolf * permission update is not performed and any preparations made for it (e.g. 190933a610c3SKevin Wolf * taken file locks) need to be undone. 191033a610c3SKevin Wolf * 191133a610c3SKevin Wolf * This function recursively notifies all child nodes. 191233a610c3SKevin Wolf */ 191333a610c3SKevin Wolf static void bdrv_abort_perm_update(BlockDriverState *bs) 191433a610c3SKevin Wolf { 191533a610c3SKevin Wolf BlockDriver *drv = bs->drv; 191633a610c3SKevin Wolf BdrvChild *c; 191733a610c3SKevin Wolf 191833a610c3SKevin Wolf if (!drv) { 191933a610c3SKevin Wolf return; 192033a610c3SKevin Wolf } 192133a610c3SKevin Wolf 192233a610c3SKevin Wolf if (drv->bdrv_abort_perm_update) { 192333a610c3SKevin Wolf drv->bdrv_abort_perm_update(bs); 192433a610c3SKevin Wolf } 192533a610c3SKevin Wolf 192633a610c3SKevin Wolf QLIST_FOREACH(c, &bs->children, next) { 192733a610c3SKevin Wolf bdrv_child_abort_perm_update(c); 192833a610c3SKevin Wolf } 192933a610c3SKevin Wolf } 193033a610c3SKevin Wolf 193133a610c3SKevin Wolf static void bdrv_set_perm(BlockDriverState *bs, uint64_t cumulative_perms, 193233a610c3SKevin Wolf uint64_t cumulative_shared_perms) 193333a610c3SKevin Wolf { 193433a610c3SKevin Wolf BlockDriver *drv = bs->drv; 193533a610c3SKevin Wolf BdrvChild *c; 193633a610c3SKevin Wolf 193733a610c3SKevin Wolf if (!drv) { 193833a610c3SKevin Wolf return; 193933a610c3SKevin Wolf } 194033a610c3SKevin Wolf 194133a610c3SKevin Wolf /* Update this node */ 194233a610c3SKevin Wolf if (drv->bdrv_set_perm) { 194333a610c3SKevin Wolf drv->bdrv_set_perm(bs, cumulative_perms, cumulative_shared_perms); 194433a610c3SKevin Wolf } 194533a610c3SKevin Wolf 194678e421c9SKevin Wolf /* Drivers that never have children can omit .bdrv_child_perm() */ 194733a610c3SKevin Wolf if (!drv->bdrv_child_perm) { 194878e421c9SKevin Wolf assert(QLIST_EMPTY(&bs->children)); 194933a610c3SKevin Wolf return; 195033a610c3SKevin Wolf } 195133a610c3SKevin Wolf 195233a610c3SKevin Wolf /* Update all children */ 195333a610c3SKevin Wolf QLIST_FOREACH(c, &bs->children, next) { 195433a610c3SKevin Wolf uint64_t cur_perm, cur_shared; 1955e0995dc3SKevin Wolf bdrv_child_perm(bs, c->bs, c, c->role, NULL, 195633a610c3SKevin Wolf cumulative_perms, cumulative_shared_perms, 195733a610c3SKevin Wolf &cur_perm, &cur_shared); 195833a610c3SKevin Wolf bdrv_child_set_perm(c, cur_perm, cur_shared); 195933a610c3SKevin Wolf } 196033a610c3SKevin Wolf } 196133a610c3SKevin Wolf 196233a610c3SKevin Wolf static void bdrv_get_cumulative_perm(BlockDriverState *bs, uint64_t *perm, 196333a610c3SKevin Wolf uint64_t *shared_perm) 196433a610c3SKevin Wolf { 196533a610c3SKevin Wolf BdrvChild *c; 196633a610c3SKevin Wolf uint64_t cumulative_perms = 0; 196733a610c3SKevin Wolf uint64_t cumulative_shared_perms = BLK_PERM_ALL; 196833a610c3SKevin Wolf 196933a610c3SKevin Wolf QLIST_FOREACH(c, &bs->parents, next_parent) { 197033a610c3SKevin Wolf cumulative_perms |= c->perm; 197133a610c3SKevin Wolf cumulative_shared_perms &= c->shared_perm; 197233a610c3SKevin Wolf } 197333a610c3SKevin Wolf 197433a610c3SKevin Wolf *perm = cumulative_perms; 197533a610c3SKevin Wolf *shared_perm = cumulative_shared_perms; 197633a610c3SKevin Wolf } 197733a610c3SKevin Wolf 1978d083319fSKevin Wolf static char *bdrv_child_user_desc(BdrvChild *c) 1979d083319fSKevin Wolf { 1980d083319fSKevin Wolf if (c->role->get_parent_desc) { 1981d083319fSKevin Wolf return c->role->get_parent_desc(c); 1982d083319fSKevin Wolf } 1983d083319fSKevin Wolf 1984d083319fSKevin Wolf return g_strdup("another user"); 1985d083319fSKevin Wolf } 1986d083319fSKevin Wolf 19875176196cSFam Zheng char *bdrv_perm_names(uint64_t perm) 1988d083319fSKevin Wolf { 1989d083319fSKevin Wolf struct perm_name { 1990d083319fSKevin Wolf uint64_t perm; 1991d083319fSKevin Wolf const char *name; 1992d083319fSKevin Wolf } permissions[] = { 1993d083319fSKevin Wolf { BLK_PERM_CONSISTENT_READ, "consistent read" }, 1994d083319fSKevin Wolf { BLK_PERM_WRITE, "write" }, 1995d083319fSKevin Wolf { BLK_PERM_WRITE_UNCHANGED, "write unchanged" }, 1996d083319fSKevin Wolf { BLK_PERM_RESIZE, "resize" }, 1997d083319fSKevin Wolf { BLK_PERM_GRAPH_MOD, "change children" }, 1998d083319fSKevin Wolf { 0, NULL } 1999d083319fSKevin Wolf }; 2000d083319fSKevin Wolf 2001*e2a7423aSAlberto Garcia GString *result = g_string_sized_new(30); 2002d083319fSKevin Wolf struct perm_name *p; 2003d083319fSKevin Wolf 2004d083319fSKevin Wolf for (p = permissions; p->name; p++) { 2005d083319fSKevin Wolf if (perm & p->perm) { 2006*e2a7423aSAlberto Garcia if (result->len > 0) { 2007*e2a7423aSAlberto Garcia g_string_append(result, ", "); 2008*e2a7423aSAlberto Garcia } 2009*e2a7423aSAlberto Garcia g_string_append(result, p->name); 2010d083319fSKevin Wolf } 2011d083319fSKevin Wolf } 2012d083319fSKevin Wolf 2013*e2a7423aSAlberto Garcia return g_string_free(result, FALSE); 2014d083319fSKevin Wolf } 2015d083319fSKevin Wolf 201633a610c3SKevin Wolf /* 201733a610c3SKevin Wolf * Checks whether a new reference to @bs can be added if the new user requires 201846181129SKevin Wolf * @new_used_perm/@new_shared_perm as its permissions. If @ignore_children is 201946181129SKevin Wolf * set, the BdrvChild objects in this list are ignored in the calculations; 202046181129SKevin Wolf * this allows checking permission updates for an existing reference. 202133a610c3SKevin Wolf * 20229eab1544SMax Reitz * See bdrv_check_perm() for the semantics of @tighten_restrictions. 20239eab1544SMax Reitz * 202433a610c3SKevin Wolf * Needs to be followed by a call to either bdrv_set_perm() or 202533a610c3SKevin Wolf * bdrv_abort_perm_update(). */ 20263121fb45SKevin Wolf static int bdrv_check_update_perm(BlockDriverState *bs, BlockReopenQueue *q, 20273121fb45SKevin Wolf uint64_t new_used_perm, 2028d5e6f437SKevin Wolf uint64_t new_shared_perm, 20299eab1544SMax Reitz GSList *ignore_children, 20309eab1544SMax Reitz bool *tighten_restrictions, 20319eab1544SMax Reitz Error **errp) 2032d5e6f437SKevin Wolf { 2033d5e6f437SKevin Wolf BdrvChild *c; 203433a610c3SKevin Wolf uint64_t cumulative_perms = new_used_perm; 203533a610c3SKevin Wolf uint64_t cumulative_shared_perms = new_shared_perm; 2036d5e6f437SKevin Wolf 20379eab1544SMax Reitz assert(!q || !tighten_restrictions); 20389eab1544SMax Reitz 2039d5e6f437SKevin Wolf /* There is no reason why anyone couldn't tolerate write_unchanged */ 2040d5e6f437SKevin Wolf assert(new_shared_perm & BLK_PERM_WRITE_UNCHANGED); 2041d5e6f437SKevin Wolf 2042d5e6f437SKevin Wolf QLIST_FOREACH(c, &bs->parents, next_parent) { 204346181129SKevin Wolf if (g_slist_find(ignore_children, c)) { 2044d5e6f437SKevin Wolf continue; 2045d5e6f437SKevin Wolf } 2046d5e6f437SKevin Wolf 2047d083319fSKevin Wolf if ((new_used_perm & c->shared_perm) != new_used_perm) { 2048d083319fSKevin Wolf char *user = bdrv_child_user_desc(c); 2049d083319fSKevin Wolf char *perm_names = bdrv_perm_names(new_used_perm & ~c->shared_perm); 20509eab1544SMax Reitz 20519eab1544SMax Reitz if (tighten_restrictions) { 20529eab1544SMax Reitz *tighten_restrictions = true; 20539eab1544SMax Reitz } 20549eab1544SMax Reitz 2055d083319fSKevin Wolf error_setg(errp, "Conflicts with use by %s as '%s', which does not " 2056d083319fSKevin Wolf "allow '%s' on %s", 2057d083319fSKevin Wolf user, c->name, perm_names, bdrv_get_node_name(c->bs)); 2058d083319fSKevin Wolf g_free(user); 2059d083319fSKevin Wolf g_free(perm_names); 2060d083319fSKevin Wolf return -EPERM; 2061d5e6f437SKevin Wolf } 2062d083319fSKevin Wolf 2063d083319fSKevin Wolf if ((c->perm & new_shared_perm) != c->perm) { 2064d083319fSKevin Wolf char *user = bdrv_child_user_desc(c); 2065d083319fSKevin Wolf char *perm_names = bdrv_perm_names(c->perm & ~new_shared_perm); 20669eab1544SMax Reitz 20679eab1544SMax Reitz if (tighten_restrictions) { 20689eab1544SMax Reitz *tighten_restrictions = true; 20699eab1544SMax Reitz } 20709eab1544SMax Reitz 2071d083319fSKevin Wolf error_setg(errp, "Conflicts with use by %s as '%s', which uses " 2072d083319fSKevin Wolf "'%s' on %s", 2073d083319fSKevin Wolf user, c->name, perm_names, bdrv_get_node_name(c->bs)); 2074d083319fSKevin Wolf g_free(user); 2075d083319fSKevin Wolf g_free(perm_names); 2076d5e6f437SKevin Wolf return -EPERM; 2077d5e6f437SKevin Wolf } 207833a610c3SKevin Wolf 207933a610c3SKevin Wolf cumulative_perms |= c->perm; 208033a610c3SKevin Wolf cumulative_shared_perms &= c->shared_perm; 2081d5e6f437SKevin Wolf } 2082d5e6f437SKevin Wolf 20833121fb45SKevin Wolf return bdrv_check_perm(bs, q, cumulative_perms, cumulative_shared_perms, 20849eab1544SMax Reitz ignore_children, tighten_restrictions, errp); 208533a610c3SKevin Wolf } 208633a610c3SKevin Wolf 208733a610c3SKevin Wolf /* Needs to be followed by a call to either bdrv_child_set_perm() or 208833a610c3SKevin Wolf * bdrv_child_abort_perm_update(). */ 20893121fb45SKevin Wolf static int bdrv_child_check_perm(BdrvChild *c, BlockReopenQueue *q, 20903121fb45SKevin Wolf uint64_t perm, uint64_t shared, 20919eab1544SMax Reitz GSList *ignore_children, 20929eab1544SMax Reitz bool *tighten_restrictions, Error **errp) 209333a610c3SKevin Wolf { 209446181129SKevin Wolf int ret; 209546181129SKevin Wolf 209646181129SKevin Wolf ignore_children = g_slist_prepend(g_slist_copy(ignore_children), c); 20979eab1544SMax Reitz ret = bdrv_check_update_perm(c->bs, q, perm, shared, ignore_children, 20989eab1544SMax Reitz tighten_restrictions, errp); 209946181129SKevin Wolf g_slist_free(ignore_children); 210046181129SKevin Wolf 2101f962e961SVladimir Sementsov-Ogievskiy if (ret < 0) { 210246181129SKevin Wolf return ret; 210333a610c3SKevin Wolf } 210433a610c3SKevin Wolf 2105f962e961SVladimir Sementsov-Ogievskiy if (!c->has_backup_perm) { 2106f962e961SVladimir Sementsov-Ogievskiy c->has_backup_perm = true; 2107f962e961SVladimir Sementsov-Ogievskiy c->backup_perm = c->perm; 2108f962e961SVladimir Sementsov-Ogievskiy c->backup_shared_perm = c->shared_perm; 2109f962e961SVladimir Sementsov-Ogievskiy } 2110f962e961SVladimir Sementsov-Ogievskiy /* 2111f962e961SVladimir Sementsov-Ogievskiy * Note: it's OK if c->has_backup_perm was already set, as we can find the 2112f962e961SVladimir Sementsov-Ogievskiy * same child twice during check_perm procedure 2113f962e961SVladimir Sementsov-Ogievskiy */ 2114f962e961SVladimir Sementsov-Ogievskiy 2115f962e961SVladimir Sementsov-Ogievskiy c->perm = perm; 2116f962e961SVladimir Sementsov-Ogievskiy c->shared_perm = shared; 2117f962e961SVladimir Sementsov-Ogievskiy 2118f962e961SVladimir Sementsov-Ogievskiy return 0; 2119f962e961SVladimir Sementsov-Ogievskiy } 2120f962e961SVladimir Sementsov-Ogievskiy 2121c1cef672SFam Zheng static void bdrv_child_set_perm(BdrvChild *c, uint64_t perm, uint64_t shared) 212233a610c3SKevin Wolf { 212333a610c3SKevin Wolf uint64_t cumulative_perms, cumulative_shared_perms; 212433a610c3SKevin Wolf 2125f962e961SVladimir Sementsov-Ogievskiy c->has_backup_perm = false; 2126f962e961SVladimir Sementsov-Ogievskiy 212733a610c3SKevin Wolf c->perm = perm; 212833a610c3SKevin Wolf c->shared_perm = shared; 212933a610c3SKevin Wolf 213033a610c3SKevin Wolf bdrv_get_cumulative_perm(c->bs, &cumulative_perms, 213133a610c3SKevin Wolf &cumulative_shared_perms); 213233a610c3SKevin Wolf bdrv_set_perm(c->bs, cumulative_perms, cumulative_shared_perms); 213333a610c3SKevin Wolf } 213433a610c3SKevin Wolf 2135c1cef672SFam Zheng static void bdrv_child_abort_perm_update(BdrvChild *c) 213633a610c3SKevin Wolf { 2137f962e961SVladimir Sementsov-Ogievskiy if (c->has_backup_perm) { 2138f962e961SVladimir Sementsov-Ogievskiy c->perm = c->backup_perm; 2139f962e961SVladimir Sementsov-Ogievskiy c->shared_perm = c->backup_shared_perm; 2140f962e961SVladimir Sementsov-Ogievskiy c->has_backup_perm = false; 2141f962e961SVladimir Sementsov-Ogievskiy } 2142f962e961SVladimir Sementsov-Ogievskiy 214333a610c3SKevin Wolf bdrv_abort_perm_update(c->bs); 214433a610c3SKevin Wolf } 214533a610c3SKevin Wolf 214633a610c3SKevin Wolf int bdrv_child_try_set_perm(BdrvChild *c, uint64_t perm, uint64_t shared, 214733a610c3SKevin Wolf Error **errp) 214833a610c3SKevin Wolf { 21491046779eSMax Reitz Error *local_err = NULL; 215033a610c3SKevin Wolf int ret; 21511046779eSMax Reitz bool tighten_restrictions; 215233a610c3SKevin Wolf 21531046779eSMax Reitz ret = bdrv_child_check_perm(c, NULL, perm, shared, NULL, 21541046779eSMax Reitz &tighten_restrictions, &local_err); 215533a610c3SKevin Wolf if (ret < 0) { 215633a610c3SKevin Wolf bdrv_child_abort_perm_update(c); 21571046779eSMax Reitz if (tighten_restrictions) { 21581046779eSMax Reitz error_propagate(errp, local_err); 21591046779eSMax Reitz } else { 21601046779eSMax Reitz /* 21611046779eSMax Reitz * Our caller may intend to only loosen restrictions and 21621046779eSMax Reitz * does not expect this function to fail. Errors are not 21631046779eSMax Reitz * fatal in such a case, so we can just hide them from our 21641046779eSMax Reitz * caller. 21651046779eSMax Reitz */ 21661046779eSMax Reitz error_free(local_err); 21671046779eSMax Reitz ret = 0; 21681046779eSMax Reitz } 216933a610c3SKevin Wolf return ret; 217033a610c3SKevin Wolf } 217133a610c3SKevin Wolf 217233a610c3SKevin Wolf bdrv_child_set_perm(c, perm, shared); 217333a610c3SKevin Wolf 2174d5e6f437SKevin Wolf return 0; 2175d5e6f437SKevin Wolf } 2176d5e6f437SKevin Wolf 2177c1087f12SMax Reitz int bdrv_child_refresh_perms(BlockDriverState *bs, BdrvChild *c, Error **errp) 2178c1087f12SMax Reitz { 2179c1087f12SMax Reitz uint64_t parent_perms, parent_shared; 2180c1087f12SMax Reitz uint64_t perms, shared; 2181c1087f12SMax Reitz 2182c1087f12SMax Reitz bdrv_get_cumulative_perm(bs, &parent_perms, &parent_shared); 2183c1087f12SMax Reitz bdrv_child_perm(bs, c->bs, c, c->role, NULL, parent_perms, parent_shared, 2184c1087f12SMax Reitz &perms, &shared); 2185c1087f12SMax Reitz 2186c1087f12SMax Reitz return bdrv_child_try_set_perm(c, perms, shared, errp); 2187c1087f12SMax Reitz } 2188c1087f12SMax Reitz 21896a1b9ee1SKevin Wolf void bdrv_filter_default_perms(BlockDriverState *bs, BdrvChild *c, 21906a1b9ee1SKevin Wolf const BdrvChildRole *role, 2191e0995dc3SKevin Wolf BlockReopenQueue *reopen_queue, 21926a1b9ee1SKevin Wolf uint64_t perm, uint64_t shared, 21936a1b9ee1SKevin Wolf uint64_t *nperm, uint64_t *nshared) 21946a1b9ee1SKevin Wolf { 21956a1b9ee1SKevin Wolf *nperm = perm & DEFAULT_PERM_PASSTHROUGH; 21966a1b9ee1SKevin Wolf *nshared = (shared & DEFAULT_PERM_PASSTHROUGH) | DEFAULT_PERM_UNCHANGED; 21976a1b9ee1SKevin Wolf } 21986a1b9ee1SKevin Wolf 21996b1a044aSKevin Wolf void bdrv_format_default_perms(BlockDriverState *bs, BdrvChild *c, 22006b1a044aSKevin Wolf const BdrvChildRole *role, 2201e0995dc3SKevin Wolf BlockReopenQueue *reopen_queue, 22026b1a044aSKevin Wolf uint64_t perm, uint64_t shared, 22036b1a044aSKevin Wolf uint64_t *nperm, uint64_t *nshared) 22046b1a044aSKevin Wolf { 22056b1a044aSKevin Wolf bool backing = (role == &child_backing); 22066b1a044aSKevin Wolf assert(role == &child_backing || role == &child_file); 22076b1a044aSKevin Wolf 22086b1a044aSKevin Wolf if (!backing) { 22095fbfabd3SKevin Wolf int flags = bdrv_reopen_get_flags(reopen_queue, bs); 22105fbfabd3SKevin Wolf 22116b1a044aSKevin Wolf /* Apart from the modifications below, the same permissions are 22126b1a044aSKevin Wolf * forwarded and left alone as for filters */ 2213e0995dc3SKevin Wolf bdrv_filter_default_perms(bs, c, role, reopen_queue, perm, shared, 2214e0995dc3SKevin Wolf &perm, &shared); 22156b1a044aSKevin Wolf 22166b1a044aSKevin Wolf /* Format drivers may touch metadata even if the guest doesn't write */ 2217cc022140SMax Reitz if (bdrv_is_writable_after_reopen(bs, reopen_queue)) { 22186b1a044aSKevin Wolf perm |= BLK_PERM_WRITE | BLK_PERM_RESIZE; 22196b1a044aSKevin Wolf } 22206b1a044aSKevin Wolf 22216b1a044aSKevin Wolf /* bs->file always needs to be consistent because of the metadata. We 22226b1a044aSKevin Wolf * can never allow other users to resize or write to it. */ 22235fbfabd3SKevin Wolf if (!(flags & BDRV_O_NO_IO)) { 22246b1a044aSKevin Wolf perm |= BLK_PERM_CONSISTENT_READ; 22255fbfabd3SKevin Wolf } 22266b1a044aSKevin Wolf shared &= ~(BLK_PERM_WRITE | BLK_PERM_RESIZE); 22276b1a044aSKevin Wolf } else { 22286b1a044aSKevin Wolf /* We want consistent read from backing files if the parent needs it. 22296b1a044aSKevin Wolf * No other operations are performed on backing files. */ 22306b1a044aSKevin Wolf perm &= BLK_PERM_CONSISTENT_READ; 22316b1a044aSKevin Wolf 22326b1a044aSKevin Wolf /* If the parent can deal with changing data, we're okay with a 22336b1a044aSKevin Wolf * writable and resizable backing file. */ 22346b1a044aSKevin Wolf /* TODO Require !(perm & BLK_PERM_CONSISTENT_READ), too? */ 22356b1a044aSKevin Wolf if (shared & BLK_PERM_WRITE) { 22366b1a044aSKevin Wolf shared = BLK_PERM_WRITE | BLK_PERM_RESIZE; 22376b1a044aSKevin Wolf } else { 22386b1a044aSKevin Wolf shared = 0; 22396b1a044aSKevin Wolf } 22406b1a044aSKevin Wolf 22416b1a044aSKevin Wolf shared |= BLK_PERM_CONSISTENT_READ | BLK_PERM_GRAPH_MOD | 22426b1a044aSKevin Wolf BLK_PERM_WRITE_UNCHANGED; 22436b1a044aSKevin Wolf } 22446b1a044aSKevin Wolf 22459c5e6594SKevin Wolf if (bs->open_flags & BDRV_O_INACTIVE) { 22469c5e6594SKevin Wolf shared |= BLK_PERM_WRITE | BLK_PERM_RESIZE; 22479c5e6594SKevin Wolf } 22489c5e6594SKevin Wolf 22496b1a044aSKevin Wolf *nperm = perm; 22506b1a044aSKevin Wolf *nshared = shared; 22516b1a044aSKevin Wolf } 22526b1a044aSKevin Wolf 22537b1d9c4dSMax Reitz uint64_t bdrv_qapi_perm_to_blk_perm(BlockPermission qapi_perm) 22547b1d9c4dSMax Reitz { 22557b1d9c4dSMax Reitz static const uint64_t permissions[] = { 22567b1d9c4dSMax Reitz [BLOCK_PERMISSION_CONSISTENT_READ] = BLK_PERM_CONSISTENT_READ, 22577b1d9c4dSMax Reitz [BLOCK_PERMISSION_WRITE] = BLK_PERM_WRITE, 22587b1d9c4dSMax Reitz [BLOCK_PERMISSION_WRITE_UNCHANGED] = BLK_PERM_WRITE_UNCHANGED, 22597b1d9c4dSMax Reitz [BLOCK_PERMISSION_RESIZE] = BLK_PERM_RESIZE, 22607b1d9c4dSMax Reitz [BLOCK_PERMISSION_GRAPH_MOD] = BLK_PERM_GRAPH_MOD, 22617b1d9c4dSMax Reitz }; 22627b1d9c4dSMax Reitz 22637b1d9c4dSMax Reitz QEMU_BUILD_BUG_ON(ARRAY_SIZE(permissions) != BLOCK_PERMISSION__MAX); 22647b1d9c4dSMax Reitz QEMU_BUILD_BUG_ON(1UL << ARRAY_SIZE(permissions) != BLK_PERM_ALL + 1); 22657b1d9c4dSMax Reitz 22667b1d9c4dSMax Reitz assert(qapi_perm < BLOCK_PERMISSION__MAX); 22677b1d9c4dSMax Reitz 22687b1d9c4dSMax Reitz return permissions[qapi_perm]; 22697b1d9c4dSMax Reitz } 22707b1d9c4dSMax Reitz 22718ee03995SKevin Wolf static void bdrv_replace_child_noperm(BdrvChild *child, 22728ee03995SKevin Wolf BlockDriverState *new_bs) 2273e9740bc6SKevin Wolf { 2274e9740bc6SKevin Wolf BlockDriverState *old_bs = child->bs; 2275debc2927SMax Reitz int new_bs_quiesce_counter; 2276debc2927SMax Reitz int drain_saldo; 2277e9740bc6SKevin Wolf 22782cad1ebeSAlberto Garcia assert(!child->frozen); 22792cad1ebeSAlberto Garcia 2280bb2614e9SFam Zheng if (old_bs && new_bs) { 2281bb2614e9SFam Zheng assert(bdrv_get_aio_context(old_bs) == bdrv_get_aio_context(new_bs)); 2282bb2614e9SFam Zheng } 2283debc2927SMax Reitz 2284debc2927SMax Reitz new_bs_quiesce_counter = (new_bs ? new_bs->quiesce_counter : 0); 2285debc2927SMax Reitz drain_saldo = new_bs_quiesce_counter - child->parent_quiesce_counter; 2286debc2927SMax Reitz 2287debc2927SMax Reitz /* 2288debc2927SMax Reitz * If the new child node is drained but the old one was not, flush 2289debc2927SMax Reitz * all outstanding requests to the old child node. 2290debc2927SMax Reitz */ 2291debc2927SMax Reitz while (drain_saldo > 0 && child->role->drained_begin) { 2292debc2927SMax Reitz bdrv_parent_drained_begin_single(child, true); 2293debc2927SMax Reitz drain_saldo--; 2294debc2927SMax Reitz } 2295debc2927SMax Reitz 2296e9740bc6SKevin Wolf if (old_bs) { 2297d736f119SKevin Wolf /* Detach first so that the recursive drain sections coming from @child 2298d736f119SKevin Wolf * are already gone and we only end the drain sections that came from 2299d736f119SKevin Wolf * elsewhere. */ 2300d736f119SKevin Wolf if (child->role->detach) { 2301d736f119SKevin Wolf child->role->detach(child); 2302d736f119SKevin Wolf } 230336fe1331SKevin Wolf QLIST_REMOVE(child, next_parent); 2304e9740bc6SKevin Wolf } 2305e9740bc6SKevin Wolf 2306e9740bc6SKevin Wolf child->bs = new_bs; 230736fe1331SKevin Wolf 230836fe1331SKevin Wolf if (new_bs) { 230936fe1331SKevin Wolf QLIST_INSERT_HEAD(&new_bs->parents, child, next_parent); 2310debc2927SMax Reitz 2311debc2927SMax Reitz /* 2312debc2927SMax Reitz * Detaching the old node may have led to the new node's 2313debc2927SMax Reitz * quiesce_counter having been decreased. Not a problem, we 2314debc2927SMax Reitz * just need to recognize this here and then invoke 2315debc2927SMax Reitz * drained_end appropriately more often. 2316debc2927SMax Reitz */ 2317debc2927SMax Reitz assert(new_bs->quiesce_counter <= new_bs_quiesce_counter); 2318debc2927SMax Reitz drain_saldo += new_bs->quiesce_counter - new_bs_quiesce_counter; 231933a610c3SKevin Wolf 2320d736f119SKevin Wolf /* Attach only after starting new drained sections, so that recursive 2321d736f119SKevin Wolf * drain sections coming from @child don't get an extra .drained_begin 2322d736f119SKevin Wolf * callback. */ 2323db95dbbaSKevin Wolf if (child->role->attach) { 2324db95dbbaSKevin Wolf child->role->attach(child); 2325db95dbbaSKevin Wolf } 232636fe1331SKevin Wolf } 2327debc2927SMax Reitz 2328debc2927SMax Reitz /* 2329debc2927SMax Reitz * If the old child node was drained but the new one is not, allow 2330debc2927SMax Reitz * requests to come in only after the new node has been attached. 2331debc2927SMax Reitz */ 2332debc2927SMax Reitz while (drain_saldo < 0 && child->role->drained_end) { 2333debc2927SMax Reitz bdrv_parent_drained_end_single(child); 2334debc2927SMax Reitz drain_saldo++; 2335debc2927SMax Reitz } 2336e9740bc6SKevin Wolf } 2337e9740bc6SKevin Wolf 2338466787fbSKevin Wolf /* 2339466787fbSKevin Wolf * Updates @child to change its reference to point to @new_bs, including 2340466787fbSKevin Wolf * checking and applying the necessary permisson updates both to the old node 2341466787fbSKevin Wolf * and to @new_bs. 2342466787fbSKevin Wolf * 2343466787fbSKevin Wolf * NULL is passed as @new_bs for removing the reference before freeing @child. 2344466787fbSKevin Wolf * 2345466787fbSKevin Wolf * If @new_bs is not NULL, bdrv_check_perm() must be called beforehand, as this 2346466787fbSKevin Wolf * function uses bdrv_set_perm() to update the permissions according to the new 2347466787fbSKevin Wolf * reference that @new_bs gets. 2348466787fbSKevin Wolf */ 2349466787fbSKevin Wolf static void bdrv_replace_child(BdrvChild *child, BlockDriverState *new_bs) 23508ee03995SKevin Wolf { 23518ee03995SKevin Wolf BlockDriverState *old_bs = child->bs; 23528ee03995SKevin Wolf uint64_t perm, shared_perm; 23538ee03995SKevin Wolf 23548aecf1d1SKevin Wolf bdrv_replace_child_noperm(child, new_bs); 23558aecf1d1SKevin Wolf 235687ace5f8SMax Reitz /* 235787ace5f8SMax Reitz * Start with the new node's permissions. If @new_bs is a (direct 235887ace5f8SMax Reitz * or indirect) child of @old_bs, we must complete the permission 235987ace5f8SMax Reitz * update on @new_bs before we loosen the restrictions on @old_bs. 236087ace5f8SMax Reitz * Otherwise, bdrv_check_perm() on @old_bs would re-initiate 236187ace5f8SMax Reitz * updating the permissions of @new_bs, and thus not purely loosen 236287ace5f8SMax Reitz * restrictions. 236387ace5f8SMax Reitz */ 236487ace5f8SMax Reitz if (new_bs) { 236587ace5f8SMax Reitz bdrv_get_cumulative_perm(new_bs, &perm, &shared_perm); 236687ace5f8SMax Reitz bdrv_set_perm(new_bs, perm, shared_perm); 236787ace5f8SMax Reitz } 236887ace5f8SMax Reitz 23698ee03995SKevin Wolf if (old_bs) { 23708ee03995SKevin Wolf /* Update permissions for old node. This is guaranteed to succeed 23718ee03995SKevin Wolf * because we're just taking a parent away, so we're loosening 23728ee03995SKevin Wolf * restrictions. */ 23731046779eSMax Reitz bool tighten_restrictions; 23741046779eSMax Reitz int ret; 23751046779eSMax Reitz 23768ee03995SKevin Wolf bdrv_get_cumulative_perm(old_bs, &perm, &shared_perm); 23771046779eSMax Reitz ret = bdrv_check_perm(old_bs, NULL, perm, shared_perm, NULL, 23781046779eSMax Reitz &tighten_restrictions, NULL); 23791046779eSMax Reitz assert(tighten_restrictions == false); 23801046779eSMax Reitz if (ret < 0) { 23811046779eSMax Reitz /* We only tried to loosen restrictions, so errors are not fatal */ 23821046779eSMax Reitz bdrv_abort_perm_update(old_bs); 23831046779eSMax Reitz } else { 23848ee03995SKevin Wolf bdrv_set_perm(old_bs, perm, shared_perm); 23851046779eSMax Reitz } 2386ad943dcbSKevin Wolf 2387ad943dcbSKevin Wolf /* When the parent requiring a non-default AioContext is removed, the 2388ad943dcbSKevin Wolf * node moves back to the main AioContext */ 2389ad943dcbSKevin Wolf bdrv_try_set_aio_context(old_bs, qemu_get_aio_context(), NULL); 23908ee03995SKevin Wolf } 2391f54120ffSKevin Wolf } 2392f54120ffSKevin Wolf 2393b441dc71SAlberto Garcia /* 2394b441dc71SAlberto Garcia * This function steals the reference to child_bs from the caller. 2395b441dc71SAlberto Garcia * That reference is later dropped by bdrv_root_unref_child(). 2396b441dc71SAlberto Garcia * 2397b441dc71SAlberto Garcia * On failure NULL is returned, errp is set and the reference to 2398b441dc71SAlberto Garcia * child_bs is also dropped. 2399132ada80SKevin Wolf * 2400132ada80SKevin Wolf * The caller must hold the AioContext lock @child_bs, but not that of @ctx 2401132ada80SKevin Wolf * (unless @child_bs is already in @ctx). 2402b441dc71SAlberto Garcia */ 2403f21d96d0SKevin Wolf BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs, 2404260fecf1SKevin Wolf const char *child_name, 240536fe1331SKevin Wolf const BdrvChildRole *child_role, 2406132ada80SKevin Wolf AioContext *ctx, 2407d5e6f437SKevin Wolf uint64_t perm, uint64_t shared_perm, 2408d5e6f437SKevin Wolf void *opaque, Error **errp) 2409df581792SKevin Wolf { 2410d5e6f437SKevin Wolf BdrvChild *child; 2411132ada80SKevin Wolf Error *local_err = NULL; 2412d5e6f437SKevin Wolf int ret; 2413d5e6f437SKevin Wolf 24149eab1544SMax Reitz ret = bdrv_check_update_perm(child_bs, NULL, perm, shared_perm, NULL, NULL, 24159eab1544SMax Reitz errp); 2416d5e6f437SKevin Wolf if (ret < 0) { 241733a610c3SKevin Wolf bdrv_abort_perm_update(child_bs); 2418b441dc71SAlberto Garcia bdrv_unref(child_bs); 2419d5e6f437SKevin Wolf return NULL; 2420d5e6f437SKevin Wolf } 2421d5e6f437SKevin Wolf 2422d5e6f437SKevin Wolf child = g_new(BdrvChild, 1); 2423df581792SKevin Wolf *child = (BdrvChild) { 2424e9740bc6SKevin Wolf .bs = NULL, 2425260fecf1SKevin Wolf .name = g_strdup(child_name), 2426df581792SKevin Wolf .role = child_role, 2427d5e6f437SKevin Wolf .perm = perm, 2428d5e6f437SKevin Wolf .shared_perm = shared_perm, 242936fe1331SKevin Wolf .opaque = opaque, 2430df581792SKevin Wolf }; 2431df581792SKevin Wolf 2432132ada80SKevin Wolf /* If the AioContexts don't match, first try to move the subtree of 2433132ada80SKevin Wolf * child_bs into the AioContext of the new parent. If this doesn't work, 2434132ada80SKevin Wolf * try moving the parent into the AioContext of child_bs instead. */ 2435132ada80SKevin Wolf if (bdrv_get_aio_context(child_bs) != ctx) { 2436132ada80SKevin Wolf ret = bdrv_try_set_aio_context(child_bs, ctx, &local_err); 2437132ada80SKevin Wolf if (ret < 0 && child_role->can_set_aio_ctx) { 2438132ada80SKevin Wolf GSList *ignore = g_slist_prepend(NULL, child);; 2439132ada80SKevin Wolf ctx = bdrv_get_aio_context(child_bs); 2440132ada80SKevin Wolf if (child_role->can_set_aio_ctx(child, ctx, &ignore, NULL)) { 2441132ada80SKevin Wolf error_free(local_err); 2442132ada80SKevin Wolf ret = 0; 2443132ada80SKevin Wolf g_slist_free(ignore); 2444132ada80SKevin Wolf ignore = g_slist_prepend(NULL, child);; 2445132ada80SKevin Wolf child_role->set_aio_ctx(child, ctx, &ignore); 2446132ada80SKevin Wolf } 2447132ada80SKevin Wolf g_slist_free(ignore); 2448132ada80SKevin Wolf } 2449132ada80SKevin Wolf if (ret < 0) { 2450132ada80SKevin Wolf error_propagate(errp, local_err); 2451132ada80SKevin Wolf g_free(child); 2452132ada80SKevin Wolf bdrv_abort_perm_update(child_bs); 2453132ada80SKevin Wolf return NULL; 2454132ada80SKevin Wolf } 2455132ada80SKevin Wolf } 2456132ada80SKevin Wolf 245733a610c3SKevin Wolf /* This performs the matching bdrv_set_perm() for the above check. */ 2458466787fbSKevin Wolf bdrv_replace_child(child, child_bs); 2459b4b059f6SKevin Wolf 2460b4b059f6SKevin Wolf return child; 2461df581792SKevin Wolf } 2462df581792SKevin Wolf 2463b441dc71SAlberto Garcia /* 2464b441dc71SAlberto Garcia * This function transfers the reference to child_bs from the caller 2465b441dc71SAlberto Garcia * to parent_bs. That reference is later dropped by parent_bs on 2466b441dc71SAlberto Garcia * bdrv_close() or if someone calls bdrv_unref_child(). 2467b441dc71SAlberto Garcia * 2468b441dc71SAlberto Garcia * On failure NULL is returned, errp is set and the reference to 2469b441dc71SAlberto Garcia * child_bs is also dropped. 2470132ada80SKevin Wolf * 2471132ada80SKevin Wolf * If @parent_bs and @child_bs are in different AioContexts, the caller must 2472132ada80SKevin Wolf * hold the AioContext lock for @child_bs, but not for @parent_bs. 2473b441dc71SAlberto Garcia */ 247498292c61SWen Congyang BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs, 2475f21d96d0SKevin Wolf BlockDriverState *child_bs, 2476f21d96d0SKevin Wolf const char *child_name, 24778b2ff529SKevin Wolf const BdrvChildRole *child_role, 24788b2ff529SKevin Wolf Error **errp) 2479f21d96d0SKevin Wolf { 2480d5e6f437SKevin Wolf BdrvChild *child; 2481f68c598bSKevin Wolf uint64_t perm, shared_perm; 2482d5e6f437SKevin Wolf 2483f68c598bSKevin Wolf bdrv_get_cumulative_perm(parent_bs, &perm, &shared_perm); 2484f68c598bSKevin Wolf 2485f68c598bSKevin Wolf assert(parent_bs->drv); 2486e0995dc3SKevin Wolf bdrv_child_perm(parent_bs, child_bs, NULL, child_role, NULL, 2487f68c598bSKevin Wolf perm, shared_perm, &perm, &shared_perm); 2488f68c598bSKevin Wolf 2489d5e6f437SKevin Wolf child = bdrv_root_attach_child(child_bs, child_name, child_role, 2490132ada80SKevin Wolf bdrv_get_aio_context(parent_bs), 2491f68c598bSKevin Wolf perm, shared_perm, parent_bs, errp); 2492d5e6f437SKevin Wolf if (child == NULL) { 2493d5e6f437SKevin Wolf return NULL; 2494d5e6f437SKevin Wolf } 2495d5e6f437SKevin Wolf 2496f21d96d0SKevin Wolf QLIST_INSERT_HEAD(&parent_bs->children, child, next); 2497f21d96d0SKevin Wolf return child; 2498f21d96d0SKevin Wolf } 2499f21d96d0SKevin Wolf 25003f09bfbcSKevin Wolf static void bdrv_detach_child(BdrvChild *child) 250133a60407SKevin Wolf { 2502f21d96d0SKevin Wolf if (child->next.le_prev) { 250333a60407SKevin Wolf QLIST_REMOVE(child, next); 2504f21d96d0SKevin Wolf child->next.le_prev = NULL; 2505f21d96d0SKevin Wolf } 2506e9740bc6SKevin Wolf 2507466787fbSKevin Wolf bdrv_replace_child(child, NULL); 2508e9740bc6SKevin Wolf 2509260fecf1SKevin Wolf g_free(child->name); 251033a60407SKevin Wolf g_free(child); 251133a60407SKevin Wolf } 251233a60407SKevin Wolf 2513f21d96d0SKevin Wolf void bdrv_root_unref_child(BdrvChild *child) 251433a60407SKevin Wolf { 2515779020cbSKevin Wolf BlockDriverState *child_bs; 2516779020cbSKevin Wolf 2517f21d96d0SKevin Wolf child_bs = child->bs; 2518f21d96d0SKevin Wolf bdrv_detach_child(child); 2519f21d96d0SKevin Wolf bdrv_unref(child_bs); 2520f21d96d0SKevin Wolf } 2521f21d96d0SKevin Wolf 25223cf746b3SMax Reitz /** 25233cf746b3SMax Reitz * Clear all inherits_from pointers from children and grandchildren of 25243cf746b3SMax Reitz * @root that point to @root, where necessary. 25253cf746b3SMax Reitz */ 25263cf746b3SMax Reitz static void bdrv_unset_inherits_from(BlockDriverState *root, BdrvChild *child) 2527f21d96d0SKevin Wolf { 25284e4bf5c4SKevin Wolf BdrvChild *c; 25294e4bf5c4SKevin Wolf 25303cf746b3SMax Reitz if (child->bs->inherits_from == root) { 25313cf746b3SMax Reitz /* 25323cf746b3SMax Reitz * Remove inherits_from only when the last reference between root and 25333cf746b3SMax Reitz * child->bs goes away. 25343cf746b3SMax Reitz */ 25353cf746b3SMax Reitz QLIST_FOREACH(c, &root->children, next) { 25364e4bf5c4SKevin Wolf if (c != child && c->bs == child->bs) { 25374e4bf5c4SKevin Wolf break; 25384e4bf5c4SKevin Wolf } 25394e4bf5c4SKevin Wolf } 25404e4bf5c4SKevin Wolf if (c == NULL) { 254133a60407SKevin Wolf child->bs->inherits_from = NULL; 254233a60407SKevin Wolf } 25434e4bf5c4SKevin Wolf } 254433a60407SKevin Wolf 25453cf746b3SMax Reitz QLIST_FOREACH(c, &child->bs->children, next) { 25463cf746b3SMax Reitz bdrv_unset_inherits_from(root, c); 25473cf746b3SMax Reitz } 25483cf746b3SMax Reitz } 25493cf746b3SMax Reitz 25503cf746b3SMax Reitz void bdrv_unref_child(BlockDriverState *parent, BdrvChild *child) 25513cf746b3SMax Reitz { 25523cf746b3SMax Reitz if (child == NULL) { 25533cf746b3SMax Reitz return; 25543cf746b3SMax Reitz } 25553cf746b3SMax Reitz 25563cf746b3SMax Reitz bdrv_unset_inherits_from(parent, child); 2557f21d96d0SKevin Wolf bdrv_root_unref_child(child); 255833a60407SKevin Wolf } 255933a60407SKevin Wolf 25605c8cab48SKevin Wolf 25615c8cab48SKevin Wolf static void bdrv_parent_cb_change_media(BlockDriverState *bs, bool load) 25625c8cab48SKevin Wolf { 25635c8cab48SKevin Wolf BdrvChild *c; 25645c8cab48SKevin Wolf QLIST_FOREACH(c, &bs->parents, next_parent) { 25655c8cab48SKevin Wolf if (c->role->change_media) { 25665c8cab48SKevin Wolf c->role->change_media(c, load); 25675c8cab48SKevin Wolf } 25685c8cab48SKevin Wolf } 25695c8cab48SKevin Wolf } 25705c8cab48SKevin Wolf 25710065c455SAlberto Garcia /* Return true if you can reach parent going through child->inherits_from 25720065c455SAlberto Garcia * recursively. If parent or child are NULL, return false */ 25730065c455SAlberto Garcia static bool bdrv_inherits_from_recursive(BlockDriverState *child, 25740065c455SAlberto Garcia BlockDriverState *parent) 25750065c455SAlberto Garcia { 25760065c455SAlberto Garcia while (child && child != parent) { 25770065c455SAlberto Garcia child = child->inherits_from; 25780065c455SAlberto Garcia } 25790065c455SAlberto Garcia 25800065c455SAlberto Garcia return child != NULL; 25810065c455SAlberto Garcia } 25820065c455SAlberto Garcia 25835db15a57SKevin Wolf /* 25845db15a57SKevin Wolf * Sets the backing file link of a BDS. A new reference is created; callers 25855db15a57SKevin Wolf * which don't need their own reference any more must call bdrv_unref(). 25865db15a57SKevin Wolf */ 258712fa4af6SKevin Wolf void bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd, 258812fa4af6SKevin Wolf Error **errp) 25898d24cce1SFam Zheng { 25900065c455SAlberto Garcia bool update_inherits_from = bdrv_chain_contains(bs, backing_hd) && 25910065c455SAlberto Garcia bdrv_inherits_from_recursive(backing_hd, bs); 25920065c455SAlberto Garcia 25932cad1ebeSAlberto Garcia if (bdrv_is_backing_chain_frozen(bs, backing_bs(bs), errp)) { 25942cad1ebeSAlberto Garcia return; 25952cad1ebeSAlberto Garcia } 25962cad1ebeSAlberto Garcia 25975db15a57SKevin Wolf if (backing_hd) { 25985db15a57SKevin Wolf bdrv_ref(backing_hd); 25995db15a57SKevin Wolf } 26008d24cce1SFam Zheng 2601760e0063SKevin Wolf if (bs->backing) { 26025db15a57SKevin Wolf bdrv_unref_child(bs, bs->backing); 2603826b6ca0SFam Zheng } 2604826b6ca0SFam Zheng 26058d24cce1SFam Zheng if (!backing_hd) { 2606760e0063SKevin Wolf bs->backing = NULL; 26078d24cce1SFam Zheng goto out; 26088d24cce1SFam Zheng } 260912fa4af6SKevin Wolf 26108b2ff529SKevin Wolf bs->backing = bdrv_attach_child(bs, backing_hd, "backing", &child_backing, 261112fa4af6SKevin Wolf errp); 26120065c455SAlberto Garcia /* If backing_hd was already part of bs's backing chain, and 26130065c455SAlberto Garcia * inherits_from pointed recursively to bs then let's update it to 26140065c455SAlberto Garcia * point directly to bs (else it will become NULL). */ 2615b441dc71SAlberto Garcia if (bs->backing && update_inherits_from) { 26160065c455SAlberto Garcia backing_hd->inherits_from = bs; 26170065c455SAlberto Garcia } 2618826b6ca0SFam Zheng 26198d24cce1SFam Zheng out: 26203baca891SKevin Wolf bdrv_refresh_limits(bs, NULL); 26218d24cce1SFam Zheng } 26228d24cce1SFam Zheng 262331ca6d07SKevin Wolf /* 262431ca6d07SKevin Wolf * Opens the backing file for a BlockDriverState if not yet open 262531ca6d07SKevin Wolf * 2626d9b7b057SKevin Wolf * bdref_key specifies the key for the image's BlockdevRef in the options QDict. 2627d9b7b057SKevin Wolf * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict 2628d9b7b057SKevin Wolf * itself, all options starting with "${bdref_key}." are considered part of the 2629d9b7b057SKevin Wolf * BlockdevRef. 2630d9b7b057SKevin Wolf * 2631d9b7b057SKevin Wolf * TODO Can this be unified with bdrv_open_image()? 263231ca6d07SKevin Wolf */ 2633d9b7b057SKevin Wolf int bdrv_open_backing_file(BlockDriverState *bs, QDict *parent_options, 2634d9b7b057SKevin Wolf const char *bdref_key, Error **errp) 26359156df12SPaolo Bonzini { 26366b6833c1SMax Reitz char *backing_filename = NULL; 2637d9b7b057SKevin Wolf char *bdref_key_dot; 2638d9b7b057SKevin Wolf const char *reference = NULL; 2639317fc44eSKevin Wolf int ret = 0; 2640998c2019SMax Reitz bool implicit_backing = false; 26418d24cce1SFam Zheng BlockDriverState *backing_hd; 2642d9b7b057SKevin Wolf QDict *options; 2643d9b7b057SKevin Wolf QDict *tmp_parent_options = NULL; 264434b5d2c6SMax Reitz Error *local_err = NULL; 26459156df12SPaolo Bonzini 2646760e0063SKevin Wolf if (bs->backing != NULL) { 26471ba4b6a5SBenoît Canet goto free_exit; 26489156df12SPaolo Bonzini } 26499156df12SPaolo Bonzini 265031ca6d07SKevin Wolf /* NULL means an empty set of options */ 2651d9b7b057SKevin Wolf if (parent_options == NULL) { 2652d9b7b057SKevin Wolf tmp_parent_options = qdict_new(); 2653d9b7b057SKevin Wolf parent_options = tmp_parent_options; 265431ca6d07SKevin Wolf } 265531ca6d07SKevin Wolf 26569156df12SPaolo Bonzini bs->open_flags &= ~BDRV_O_NO_BACKING; 2657d9b7b057SKevin Wolf 2658d9b7b057SKevin Wolf bdref_key_dot = g_strdup_printf("%s.", bdref_key); 2659d9b7b057SKevin Wolf qdict_extract_subqdict(parent_options, &options, bdref_key_dot); 2660d9b7b057SKevin Wolf g_free(bdref_key_dot); 2661d9b7b057SKevin Wolf 2662129c7d1cSMarkus Armbruster /* 2663129c7d1cSMarkus Armbruster * Caution: while qdict_get_try_str() is fine, getting non-string 2664129c7d1cSMarkus Armbruster * types would require more care. When @parent_options come from 2665129c7d1cSMarkus Armbruster * -blockdev or blockdev_add, its members are typed according to 2666129c7d1cSMarkus Armbruster * the QAPI schema, but when they come from -drive, they're all 2667129c7d1cSMarkus Armbruster * QString. 2668129c7d1cSMarkus Armbruster */ 2669d9b7b057SKevin Wolf reference = qdict_get_try_str(parent_options, bdref_key); 2670d9b7b057SKevin Wolf if (reference || qdict_haskey(options, "file.filename")) { 26716b6833c1SMax Reitz /* keep backing_filename NULL */ 26721cb6f506SKevin Wolf } else if (bs->backing_file[0] == '\0' && qdict_size(options) == 0) { 2673cb3e7f08SMarc-André Lureau qobject_unref(options); 26741ba4b6a5SBenoît Canet goto free_exit; 2675dbecebddSFam Zheng } else { 2676998c2019SMax Reitz if (qdict_size(options) == 0) { 2677998c2019SMax Reitz /* If the user specifies options that do not modify the 2678998c2019SMax Reitz * backing file's behavior, we might still consider it the 2679998c2019SMax Reitz * implicit backing file. But it's easier this way, and 2680998c2019SMax Reitz * just specifying some of the backing BDS's options is 2681998c2019SMax Reitz * only possible with -drive anyway (otherwise the QAPI 2682998c2019SMax Reitz * schema forces the user to specify everything). */ 2683998c2019SMax Reitz implicit_backing = !strcmp(bs->auto_backing_file, bs->backing_file); 2684998c2019SMax Reitz } 2685998c2019SMax Reitz 26866b6833c1SMax Reitz backing_filename = bdrv_get_full_backing_filename(bs, &local_err); 26879f07429eSMax Reitz if (local_err) { 26889f07429eSMax Reitz ret = -EINVAL; 26899f07429eSMax Reitz error_propagate(errp, local_err); 2690cb3e7f08SMarc-André Lureau qobject_unref(options); 26919f07429eSMax Reitz goto free_exit; 26929f07429eSMax Reitz } 26939156df12SPaolo Bonzini } 26949156df12SPaolo Bonzini 26958ee79e70SKevin Wolf if (!bs->drv || !bs->drv->supports_backing) { 26968ee79e70SKevin Wolf ret = -EINVAL; 26978ee79e70SKevin Wolf error_setg(errp, "Driver doesn't support backing files"); 2698cb3e7f08SMarc-André Lureau qobject_unref(options); 26998ee79e70SKevin Wolf goto free_exit; 27008ee79e70SKevin Wolf } 27018ee79e70SKevin Wolf 27026bff597bSPeter Krempa if (!reference && 27036bff597bSPeter Krempa bs->backing_format[0] != '\0' && !qdict_haskey(options, "driver")) { 270446f5ac20SEric Blake qdict_put_str(options, "driver", bs->backing_format); 27059156df12SPaolo Bonzini } 27069156df12SPaolo Bonzini 27076b6833c1SMax Reitz backing_hd = bdrv_open_inherit(backing_filename, reference, options, 0, bs, 27086b6833c1SMax Reitz &child_backing, errp); 27095b363937SMax Reitz if (!backing_hd) { 27109156df12SPaolo Bonzini bs->open_flags |= BDRV_O_NO_BACKING; 2711e43bfd9cSMarkus Armbruster error_prepend(errp, "Could not open backing file: "); 27125b363937SMax Reitz ret = -EINVAL; 27131ba4b6a5SBenoît Canet goto free_exit; 27149156df12SPaolo Bonzini } 2715df581792SKevin Wolf 2716998c2019SMax Reitz if (implicit_backing) { 2717998c2019SMax Reitz bdrv_refresh_filename(backing_hd); 2718998c2019SMax Reitz pstrcpy(bs->auto_backing_file, sizeof(bs->auto_backing_file), 2719998c2019SMax Reitz backing_hd->filename); 2720998c2019SMax Reitz } 2721998c2019SMax Reitz 27225db15a57SKevin Wolf /* Hook up the backing file link; drop our reference, bs owns the 27235db15a57SKevin Wolf * backing_hd reference now */ 272412fa4af6SKevin Wolf bdrv_set_backing_hd(bs, backing_hd, &local_err); 27255db15a57SKevin Wolf bdrv_unref(backing_hd); 272612fa4af6SKevin Wolf if (local_err) { 27278cd1a3e4SFam Zheng error_propagate(errp, local_err); 272812fa4af6SKevin Wolf ret = -EINVAL; 272912fa4af6SKevin Wolf goto free_exit; 273012fa4af6SKevin Wolf } 2731d80ac658SPeter Feiner 2732d9b7b057SKevin Wolf qdict_del(parent_options, bdref_key); 2733d9b7b057SKevin Wolf 27341ba4b6a5SBenoît Canet free_exit: 27351ba4b6a5SBenoît Canet g_free(backing_filename); 2736cb3e7f08SMarc-André Lureau qobject_unref(tmp_parent_options); 27371ba4b6a5SBenoît Canet return ret; 27389156df12SPaolo Bonzini } 27399156df12SPaolo Bonzini 27402d6b86afSKevin Wolf static BlockDriverState * 27412d6b86afSKevin Wolf bdrv_open_child_bs(const char *filename, QDict *options, const char *bdref_key, 27422d6b86afSKevin Wolf BlockDriverState *parent, const BdrvChildRole *child_role, 2743f7d9fd8cSMax Reitz bool allow_none, Error **errp) 2744da557aacSMax Reitz { 27452d6b86afSKevin Wolf BlockDriverState *bs = NULL; 2746da557aacSMax Reitz QDict *image_options; 2747da557aacSMax Reitz char *bdref_key_dot; 2748da557aacSMax Reitz const char *reference; 2749da557aacSMax Reitz 2750df581792SKevin Wolf assert(child_role != NULL); 2751f67503e5SMax Reitz 2752da557aacSMax Reitz bdref_key_dot = g_strdup_printf("%s.", bdref_key); 2753da557aacSMax Reitz qdict_extract_subqdict(options, &image_options, bdref_key_dot); 2754da557aacSMax Reitz g_free(bdref_key_dot); 2755da557aacSMax Reitz 2756129c7d1cSMarkus Armbruster /* 2757129c7d1cSMarkus Armbruster * Caution: while qdict_get_try_str() is fine, getting non-string 2758129c7d1cSMarkus Armbruster * types would require more care. When @options come from 2759129c7d1cSMarkus Armbruster * -blockdev or blockdev_add, its members are typed according to 2760129c7d1cSMarkus Armbruster * the QAPI schema, but when they come from -drive, they're all 2761129c7d1cSMarkus Armbruster * QString. 2762129c7d1cSMarkus Armbruster */ 2763da557aacSMax Reitz reference = qdict_get_try_str(options, bdref_key); 2764da557aacSMax Reitz if (!filename && !reference && !qdict_size(image_options)) { 2765b4b059f6SKevin Wolf if (!allow_none) { 2766da557aacSMax Reitz error_setg(errp, "A block device must be specified for \"%s\"", 2767da557aacSMax Reitz bdref_key); 2768da557aacSMax Reitz } 2769cb3e7f08SMarc-André Lureau qobject_unref(image_options); 2770da557aacSMax Reitz goto done; 2771da557aacSMax Reitz } 2772da557aacSMax Reitz 27735b363937SMax Reitz bs = bdrv_open_inherit(filename, reference, image_options, 0, 2774ce343771SMax Reitz parent, child_role, errp); 27755b363937SMax Reitz if (!bs) { 2776df581792SKevin Wolf goto done; 2777df581792SKevin Wolf } 2778df581792SKevin Wolf 2779da557aacSMax Reitz done: 2780da557aacSMax Reitz qdict_del(options, bdref_key); 27812d6b86afSKevin Wolf return bs; 27822d6b86afSKevin Wolf } 27832d6b86afSKevin Wolf 27842d6b86afSKevin Wolf /* 27852d6b86afSKevin Wolf * Opens a disk image whose options are given as BlockdevRef in another block 27862d6b86afSKevin Wolf * device's options. 27872d6b86afSKevin Wolf * 27882d6b86afSKevin Wolf * If allow_none is true, no image will be opened if filename is false and no 27892d6b86afSKevin Wolf * BlockdevRef is given. NULL will be returned, but errp remains unset. 27902d6b86afSKevin Wolf * 27912d6b86afSKevin Wolf * bdrev_key specifies the key for the image's BlockdevRef in the options QDict. 27922d6b86afSKevin Wolf * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict 27932d6b86afSKevin Wolf * itself, all options starting with "${bdref_key}." are considered part of the 27942d6b86afSKevin Wolf * BlockdevRef. 27952d6b86afSKevin Wolf * 27962d6b86afSKevin Wolf * The BlockdevRef will be removed from the options QDict. 27972d6b86afSKevin Wolf */ 27982d6b86afSKevin Wolf BdrvChild *bdrv_open_child(const char *filename, 27992d6b86afSKevin Wolf QDict *options, const char *bdref_key, 28002d6b86afSKevin Wolf BlockDriverState *parent, 28012d6b86afSKevin Wolf const BdrvChildRole *child_role, 28022d6b86afSKevin Wolf bool allow_none, Error **errp) 28032d6b86afSKevin Wolf { 28042d6b86afSKevin Wolf BlockDriverState *bs; 28052d6b86afSKevin Wolf 28062d6b86afSKevin Wolf bs = bdrv_open_child_bs(filename, options, bdref_key, parent, child_role, 28072d6b86afSKevin Wolf allow_none, errp); 28082d6b86afSKevin Wolf if (bs == NULL) { 28092d6b86afSKevin Wolf return NULL; 28102d6b86afSKevin Wolf } 28112d6b86afSKevin Wolf 2812b441dc71SAlberto Garcia return bdrv_attach_child(parent, bs, bdref_key, child_role, errp); 2813b4b059f6SKevin Wolf } 2814b4b059f6SKevin Wolf 2815e1d74bc6SKevin Wolf /* TODO Future callers may need to specify parent/child_role in order for 2816e1d74bc6SKevin Wolf * option inheritance to work. Existing callers use it for the root node. */ 2817e1d74bc6SKevin Wolf BlockDriverState *bdrv_open_blockdev_ref(BlockdevRef *ref, Error **errp) 2818e1d74bc6SKevin Wolf { 2819e1d74bc6SKevin Wolf BlockDriverState *bs = NULL; 2820e1d74bc6SKevin Wolf Error *local_err = NULL; 2821e1d74bc6SKevin Wolf QObject *obj = NULL; 2822e1d74bc6SKevin Wolf QDict *qdict = NULL; 2823e1d74bc6SKevin Wolf const char *reference = NULL; 2824e1d74bc6SKevin Wolf Visitor *v = NULL; 2825e1d74bc6SKevin Wolf 2826e1d74bc6SKevin Wolf if (ref->type == QTYPE_QSTRING) { 2827e1d74bc6SKevin Wolf reference = ref->u.reference; 2828e1d74bc6SKevin Wolf } else { 2829e1d74bc6SKevin Wolf BlockdevOptions *options = &ref->u.definition; 2830e1d74bc6SKevin Wolf assert(ref->type == QTYPE_QDICT); 2831e1d74bc6SKevin Wolf 2832e1d74bc6SKevin Wolf v = qobject_output_visitor_new(&obj); 2833e1d74bc6SKevin Wolf visit_type_BlockdevOptions(v, NULL, &options, &local_err); 2834e1d74bc6SKevin Wolf if (local_err) { 2835e1d74bc6SKevin Wolf error_propagate(errp, local_err); 2836e1d74bc6SKevin Wolf goto fail; 2837e1d74bc6SKevin Wolf } 2838e1d74bc6SKevin Wolf visit_complete(v, &obj); 2839e1d74bc6SKevin Wolf 28407dc847ebSMax Reitz qdict = qobject_to(QDict, obj); 2841e1d74bc6SKevin Wolf qdict_flatten(qdict); 2842e1d74bc6SKevin Wolf 2843e1d74bc6SKevin Wolf /* bdrv_open_inherit() defaults to the values in bdrv_flags (for 2844e1d74bc6SKevin Wolf * compatibility with other callers) rather than what we want as the 2845e1d74bc6SKevin Wolf * real defaults. Apply the defaults here instead. */ 2846e1d74bc6SKevin Wolf qdict_set_default_str(qdict, BDRV_OPT_CACHE_DIRECT, "off"); 2847e1d74bc6SKevin Wolf qdict_set_default_str(qdict, BDRV_OPT_CACHE_NO_FLUSH, "off"); 2848e1d74bc6SKevin Wolf qdict_set_default_str(qdict, BDRV_OPT_READ_ONLY, "off"); 2849e35bdc12SKevin Wolf qdict_set_default_str(qdict, BDRV_OPT_AUTO_READ_ONLY, "off"); 2850e35bdc12SKevin Wolf 2851e1d74bc6SKevin Wolf } 2852e1d74bc6SKevin Wolf 2853e1d74bc6SKevin Wolf bs = bdrv_open_inherit(NULL, reference, qdict, 0, NULL, NULL, errp); 2854e1d74bc6SKevin Wolf obj = NULL; 2855e1d74bc6SKevin Wolf 2856e1d74bc6SKevin Wolf fail: 2857cb3e7f08SMarc-André Lureau qobject_unref(obj); 2858e1d74bc6SKevin Wolf visit_free(v); 2859e1d74bc6SKevin Wolf return bs; 2860e1d74bc6SKevin Wolf } 2861e1d74bc6SKevin Wolf 286266836189SMax Reitz static BlockDriverState *bdrv_append_temp_snapshot(BlockDriverState *bs, 286366836189SMax Reitz int flags, 286466836189SMax Reitz QDict *snapshot_options, 286566836189SMax Reitz Error **errp) 2866b998875dSKevin Wolf { 2867b998875dSKevin Wolf /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */ 28681ba4b6a5SBenoît Canet char *tmp_filename = g_malloc0(PATH_MAX + 1); 2869b998875dSKevin Wolf int64_t total_size; 287083d0521aSChunyan Liu QemuOpts *opts = NULL; 2871ff6ed714SEric Blake BlockDriverState *bs_snapshot = NULL; 2872b2c2832cSKevin Wolf Error *local_err = NULL; 2873b998875dSKevin Wolf int ret; 2874b998875dSKevin Wolf 2875b998875dSKevin Wolf /* if snapshot, we create a temporary backing file and open it 2876b998875dSKevin Wolf instead of opening 'filename' directly */ 2877b998875dSKevin Wolf 2878b998875dSKevin Wolf /* Get the required size from the image */ 2879f187743aSKevin Wolf total_size = bdrv_getlength(bs); 2880f187743aSKevin Wolf if (total_size < 0) { 2881f187743aSKevin Wolf error_setg_errno(errp, -total_size, "Could not get image size"); 28821ba4b6a5SBenoît Canet goto out; 2883f187743aSKevin Wolf } 2884b998875dSKevin Wolf 2885b998875dSKevin Wolf /* Create the temporary image */ 28861ba4b6a5SBenoît Canet ret = get_tmp_filename(tmp_filename, PATH_MAX + 1); 2887b998875dSKevin Wolf if (ret < 0) { 2888b998875dSKevin Wolf error_setg_errno(errp, -ret, "Could not get temporary filename"); 28891ba4b6a5SBenoît Canet goto out; 2890b998875dSKevin Wolf } 2891b998875dSKevin Wolf 2892ef810437SMax Reitz opts = qemu_opts_create(bdrv_qcow2.create_opts, NULL, 0, 2893c282e1fdSChunyan Liu &error_abort); 289439101f25SMarkus Armbruster qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_size, &error_abort); 2895e43bfd9cSMarkus Armbruster ret = bdrv_create(&bdrv_qcow2, tmp_filename, opts, errp); 289683d0521aSChunyan Liu qemu_opts_del(opts); 2897b998875dSKevin Wolf if (ret < 0) { 2898e43bfd9cSMarkus Armbruster error_prepend(errp, "Could not create temporary overlay '%s': ", 2899e43bfd9cSMarkus Armbruster tmp_filename); 29001ba4b6a5SBenoît Canet goto out; 2901b998875dSKevin Wolf } 2902b998875dSKevin Wolf 290373176beeSKevin Wolf /* Prepare options QDict for the temporary file */ 290446f5ac20SEric Blake qdict_put_str(snapshot_options, "file.driver", "file"); 290546f5ac20SEric Blake qdict_put_str(snapshot_options, "file.filename", tmp_filename); 290646f5ac20SEric Blake qdict_put_str(snapshot_options, "driver", "qcow2"); 2907b998875dSKevin Wolf 29085b363937SMax Reitz bs_snapshot = bdrv_open(NULL, NULL, snapshot_options, flags, errp); 290973176beeSKevin Wolf snapshot_options = NULL; 29105b363937SMax Reitz if (!bs_snapshot) { 29111ba4b6a5SBenoît Canet goto out; 2912b998875dSKevin Wolf } 2913b998875dSKevin Wolf 2914ff6ed714SEric Blake /* bdrv_append() consumes a strong reference to bs_snapshot 2915ff6ed714SEric Blake * (i.e. it will call bdrv_unref() on it) even on error, so in 2916ff6ed714SEric Blake * order to be able to return one, we have to increase 2917ff6ed714SEric Blake * bs_snapshot's refcount here */ 291866836189SMax Reitz bdrv_ref(bs_snapshot); 2919b2c2832cSKevin Wolf bdrv_append(bs_snapshot, bs, &local_err); 2920b2c2832cSKevin Wolf if (local_err) { 2921b2c2832cSKevin Wolf error_propagate(errp, local_err); 2922ff6ed714SEric Blake bs_snapshot = NULL; 2923b2c2832cSKevin Wolf goto out; 2924b2c2832cSKevin Wolf } 29251ba4b6a5SBenoît Canet 29261ba4b6a5SBenoît Canet out: 2927cb3e7f08SMarc-André Lureau qobject_unref(snapshot_options); 29281ba4b6a5SBenoît Canet g_free(tmp_filename); 2929ff6ed714SEric Blake return bs_snapshot; 2930b998875dSKevin Wolf } 2931b998875dSKevin Wolf 2932da557aacSMax Reitz /* 2933b6ce07aaSKevin Wolf * Opens a disk image (raw, qcow2, vmdk, ...) 2934de9c0cecSKevin Wolf * 2935de9c0cecSKevin Wolf * options is a QDict of options to pass to the block drivers, or NULL for an 2936de9c0cecSKevin Wolf * empty set of options. The reference to the QDict belongs to the block layer 2937de9c0cecSKevin Wolf * after the call (even on failure), so if the caller intends to reuse the 2938cb3e7f08SMarc-André Lureau * dictionary, it needs to use qobject_ref() before calling bdrv_open. 2939f67503e5SMax Reitz * 2940f67503e5SMax Reitz * If *pbs is NULL, a new BDS will be created with a pointer to it stored there. 2941f67503e5SMax Reitz * If it is not NULL, the referenced BDS will be reused. 2942ddf5636dSMax Reitz * 2943ddf5636dSMax Reitz * The reference parameter may be used to specify an existing block device which 2944ddf5636dSMax Reitz * should be opened. If specified, neither options nor a filename may be given, 2945ddf5636dSMax Reitz * nor can an existing BDS be reused (that is, *pbs has to be NULL). 2946b6ce07aaSKevin Wolf */ 29475b363937SMax Reitz static BlockDriverState *bdrv_open_inherit(const char *filename, 29485b363937SMax Reitz const char *reference, 29495b363937SMax Reitz QDict *options, int flags, 2950f3930ed0SKevin Wolf BlockDriverState *parent, 29515b363937SMax Reitz const BdrvChildRole *child_role, 29525b363937SMax Reitz Error **errp) 2953ea2384d3Sbellard { 2954b6ce07aaSKevin Wolf int ret; 29555696c6e3SKevin Wolf BlockBackend *file = NULL; 29569a4f4c31SKevin Wolf BlockDriverState *bs; 2957ce343771SMax Reitz BlockDriver *drv = NULL; 29582f624b80SAlberto Garcia BdrvChild *child; 295974fe54f2SKevin Wolf const char *drvname; 29603e8c2e57SAlberto Garcia const char *backing; 296134b5d2c6SMax Reitz Error *local_err = NULL; 296273176beeSKevin Wolf QDict *snapshot_options = NULL; 2963b1e6fc08SKevin Wolf int snapshot_flags = 0; 296433e3963eSbellard 2965f3930ed0SKevin Wolf assert(!child_role || !flags); 2966f3930ed0SKevin Wolf assert(!child_role == !parent); 2967f67503e5SMax Reitz 2968ddf5636dSMax Reitz if (reference) { 2969ddf5636dSMax Reitz bool options_non_empty = options ? qdict_size(options) : false; 2970cb3e7f08SMarc-André Lureau qobject_unref(options); 2971ddf5636dSMax Reitz 2972ddf5636dSMax Reitz if (filename || options_non_empty) { 2973ddf5636dSMax Reitz error_setg(errp, "Cannot reference an existing block device with " 2974ddf5636dSMax Reitz "additional options or a new filename"); 29755b363937SMax Reitz return NULL; 2976ddf5636dSMax Reitz } 2977ddf5636dSMax Reitz 2978ddf5636dSMax Reitz bs = bdrv_lookup_bs(reference, reference, errp); 2979ddf5636dSMax Reitz if (!bs) { 29805b363937SMax Reitz return NULL; 2981ddf5636dSMax Reitz } 298276b22320SKevin Wolf 2983ddf5636dSMax Reitz bdrv_ref(bs); 29845b363937SMax Reitz return bs; 2985ddf5636dSMax Reitz } 2986ddf5636dSMax Reitz 2987e4e9986bSMarkus Armbruster bs = bdrv_new(); 2988f67503e5SMax Reitz 2989de9c0cecSKevin Wolf /* NULL means an empty set of options */ 2990de9c0cecSKevin Wolf if (options == NULL) { 2991de9c0cecSKevin Wolf options = qdict_new(); 2992de9c0cecSKevin Wolf } 2993de9c0cecSKevin Wolf 2994145f598eSKevin Wolf /* json: syntax counts as explicit options, as if in the QDict */ 2995de3b53f0SKevin Wolf parse_json_protocol(options, &filename, &local_err); 2996de3b53f0SKevin Wolf if (local_err) { 2997de3b53f0SKevin Wolf goto fail; 2998de3b53f0SKevin Wolf } 2999de3b53f0SKevin Wolf 3000145f598eSKevin Wolf bs->explicit_options = qdict_clone_shallow(options); 3001145f598eSKevin Wolf 3002f3930ed0SKevin Wolf if (child_role) { 3003bddcec37SKevin Wolf bs->inherits_from = parent; 30048e2160e2SKevin Wolf child_role->inherit_options(&flags, options, 30058e2160e2SKevin Wolf parent->open_flags, parent->options); 3006f3930ed0SKevin Wolf } 3007f3930ed0SKevin Wolf 3008de3b53f0SKevin Wolf ret = bdrv_fill_options(&options, filename, &flags, &local_err); 3009462f5bcfSKevin Wolf if (local_err) { 3010462f5bcfSKevin Wolf goto fail; 3011462f5bcfSKevin Wolf } 3012462f5bcfSKevin Wolf 3013129c7d1cSMarkus Armbruster /* 3014129c7d1cSMarkus Armbruster * Set the BDRV_O_RDWR and BDRV_O_ALLOW_RDWR flags. 3015129c7d1cSMarkus Armbruster * Caution: getting a boolean member of @options requires care. 3016129c7d1cSMarkus Armbruster * When @options come from -blockdev or blockdev_add, members are 3017129c7d1cSMarkus Armbruster * typed according to the QAPI schema, but when they come from 3018129c7d1cSMarkus Armbruster * -drive, they're all QString. 3019129c7d1cSMarkus Armbruster */ 3020f87a0e29SAlberto Garcia if (g_strcmp0(qdict_get_try_str(options, BDRV_OPT_READ_ONLY), "on") && 3021f87a0e29SAlberto Garcia !qdict_get_try_bool(options, BDRV_OPT_READ_ONLY, false)) { 3022f87a0e29SAlberto Garcia flags |= (BDRV_O_RDWR | BDRV_O_ALLOW_RDWR); 3023f87a0e29SAlberto Garcia } else { 3024f87a0e29SAlberto Garcia flags &= ~BDRV_O_RDWR; 302514499ea5SAlberto Garcia } 302614499ea5SAlberto Garcia 302714499ea5SAlberto Garcia if (flags & BDRV_O_SNAPSHOT) { 302814499ea5SAlberto Garcia snapshot_options = qdict_new(); 302914499ea5SAlberto Garcia bdrv_temp_snapshot_options(&snapshot_flags, snapshot_options, 303014499ea5SAlberto Garcia flags, options); 3031f87a0e29SAlberto Garcia /* Let bdrv_backing_options() override "read-only" */ 3032f87a0e29SAlberto Garcia qdict_del(options, BDRV_OPT_READ_ONLY); 303314499ea5SAlberto Garcia bdrv_backing_options(&flags, options, flags, options); 303414499ea5SAlberto Garcia } 303514499ea5SAlberto Garcia 303662392ebbSKevin Wolf bs->open_flags = flags; 303762392ebbSKevin Wolf bs->options = options; 303862392ebbSKevin Wolf options = qdict_clone_shallow(options); 303962392ebbSKevin Wolf 304076c591b0SKevin Wolf /* Find the right image format driver */ 3041129c7d1cSMarkus Armbruster /* See cautionary note on accessing @options above */ 304276c591b0SKevin Wolf drvname = qdict_get_try_str(options, "driver"); 304376c591b0SKevin Wolf if (drvname) { 304476c591b0SKevin Wolf drv = bdrv_find_format(drvname); 304576c591b0SKevin Wolf if (!drv) { 304676c591b0SKevin Wolf error_setg(errp, "Unknown driver: '%s'", drvname); 304776c591b0SKevin Wolf goto fail; 304876c591b0SKevin Wolf } 304976c591b0SKevin Wolf } 305076c591b0SKevin Wolf 305176c591b0SKevin Wolf assert(drvname || !(flags & BDRV_O_PROTOCOL)); 305276c591b0SKevin Wolf 3053129c7d1cSMarkus Armbruster /* See cautionary note on accessing @options above */ 30543e8c2e57SAlberto Garcia backing = qdict_get_try_str(options, "backing"); 3055e59a0cf1SMax Reitz if (qobject_to(QNull, qdict_get(options, "backing")) != NULL || 3056e59a0cf1SMax Reitz (backing && *backing == '\0')) 3057e59a0cf1SMax Reitz { 30584f7be280SMax Reitz if (backing) { 30594f7be280SMax Reitz warn_report("Use of \"backing\": \"\" is deprecated; " 30604f7be280SMax Reitz "use \"backing\": null instead"); 30614f7be280SMax Reitz } 30623e8c2e57SAlberto Garcia flags |= BDRV_O_NO_BACKING; 3063ae0f57f0SKevin Wolf qdict_del(bs->explicit_options, "backing"); 3064ae0f57f0SKevin Wolf qdict_del(bs->options, "backing"); 30653e8c2e57SAlberto Garcia qdict_del(options, "backing"); 30663e8c2e57SAlberto Garcia } 30673e8c2e57SAlberto Garcia 30685696c6e3SKevin Wolf /* Open image file without format layer. This BlockBackend is only used for 30694e4bf5c4SKevin Wolf * probing, the block drivers will do their own bdrv_open_child() for the 30704e4bf5c4SKevin Wolf * same BDS, which is why we put the node name back into options. */ 3071f4788adcSKevin Wolf if ((flags & BDRV_O_PROTOCOL) == 0) { 30725696c6e3SKevin Wolf BlockDriverState *file_bs; 30735696c6e3SKevin Wolf 30745696c6e3SKevin Wolf file_bs = bdrv_open_child_bs(filename, options, "file", bs, 30751fdd6933SKevin Wolf &child_file, true, &local_err); 30761fdd6933SKevin Wolf if (local_err) { 30778bfea15dSKevin Wolf goto fail; 3078f500a6d3SKevin Wolf } 30795696c6e3SKevin Wolf if (file_bs != NULL) { 3080dacaa162SKevin Wolf /* Not requesting BLK_PERM_CONSISTENT_READ because we're only 3081dacaa162SKevin Wolf * looking at the header to guess the image format. This works even 3082dacaa162SKevin Wolf * in cases where a guest would not see a consistent state. */ 3083d861ab3aSKevin Wolf file = blk_new(bdrv_get_aio_context(file_bs), 0, BLK_PERM_ALL); 3084d7086422SKevin Wolf blk_insert_bs(file, file_bs, &local_err); 30855696c6e3SKevin Wolf bdrv_unref(file_bs); 3086d7086422SKevin Wolf if (local_err) { 3087d7086422SKevin Wolf goto fail; 3088d7086422SKevin Wolf } 30895696c6e3SKevin Wolf 309046f5ac20SEric Blake qdict_put_str(options, "file", bdrv_get_node_name(file_bs)); 30914e4bf5c4SKevin Wolf } 3092f4788adcSKevin Wolf } 3093f500a6d3SKevin Wolf 309476c591b0SKevin Wolf /* Image format probing */ 309538f3ef57SKevin Wolf bs->probed = !drv; 309676c591b0SKevin Wolf if (!drv && file) { 3097cf2ab8fcSKevin Wolf ret = find_image_format(file, filename, &drv, &local_err); 309817b005f1SKevin Wolf if (ret < 0) { 309917b005f1SKevin Wolf goto fail; 310017b005f1SKevin Wolf } 310162392ebbSKevin Wolf /* 310262392ebbSKevin Wolf * This option update would logically belong in bdrv_fill_options(), 310362392ebbSKevin Wolf * but we first need to open bs->file for the probing to work, while 310462392ebbSKevin Wolf * opening bs->file already requires the (mostly) final set of options 310562392ebbSKevin Wolf * so that cache mode etc. can be inherited. 310662392ebbSKevin Wolf * 310762392ebbSKevin Wolf * Adding the driver later is somewhat ugly, but it's not an option 310862392ebbSKevin Wolf * that would ever be inherited, so it's correct. We just need to make 310962392ebbSKevin Wolf * sure to update both bs->options (which has the full effective 311062392ebbSKevin Wolf * options for bs) and options (which has file.* already removed). 311162392ebbSKevin Wolf */ 311246f5ac20SEric Blake qdict_put_str(bs->options, "driver", drv->format_name); 311346f5ac20SEric Blake qdict_put_str(options, "driver", drv->format_name); 311476c591b0SKevin Wolf } else if (!drv) { 31152a05cbe4SMax Reitz error_setg(errp, "Must specify either driver or file"); 31168bfea15dSKevin Wolf goto fail; 31172a05cbe4SMax Reitz } 3118f500a6d3SKevin Wolf 311953a29513SMax Reitz /* BDRV_O_PROTOCOL must be set iff a protocol BDS is about to be created */ 312053a29513SMax Reitz assert(!!(flags & BDRV_O_PROTOCOL) == !!drv->bdrv_file_open); 312153a29513SMax Reitz /* file must be NULL if a protocol BDS is about to be created 312253a29513SMax Reitz * (the inverse results in an error message from bdrv_open_common()) */ 312353a29513SMax Reitz assert(!(flags & BDRV_O_PROTOCOL) || !file); 312453a29513SMax Reitz 3125b6ce07aaSKevin Wolf /* Open the image */ 312682dc8b41SKevin Wolf ret = bdrv_open_common(bs, file, options, &local_err); 3127b6ce07aaSKevin Wolf if (ret < 0) { 31288bfea15dSKevin Wolf goto fail; 31296987307cSChristoph Hellwig } 31306987307cSChristoph Hellwig 31314e4bf5c4SKevin Wolf if (file) { 31325696c6e3SKevin Wolf blk_unref(file); 3133f500a6d3SKevin Wolf file = NULL; 3134f500a6d3SKevin Wolf } 3135f500a6d3SKevin Wolf 3136b6ce07aaSKevin Wolf /* If there is a backing file, use it */ 31379156df12SPaolo Bonzini if ((flags & BDRV_O_NO_BACKING) == 0) { 3138d9b7b057SKevin Wolf ret = bdrv_open_backing_file(bs, options, "backing", &local_err); 3139b6ce07aaSKevin Wolf if (ret < 0) { 3140b6ad491aSKevin Wolf goto close_and_fail; 3141b6ce07aaSKevin Wolf } 3142b6ce07aaSKevin Wolf } 3143b6ce07aaSKevin Wolf 314450196d7aSAlberto Garcia /* Remove all children options and references 314550196d7aSAlberto Garcia * from bs->options and bs->explicit_options */ 31462f624b80SAlberto Garcia QLIST_FOREACH(child, &bs->children, next) { 31472f624b80SAlberto Garcia char *child_key_dot; 31482f624b80SAlberto Garcia child_key_dot = g_strdup_printf("%s.", child->name); 31492f624b80SAlberto Garcia qdict_extract_subqdict(bs->explicit_options, NULL, child_key_dot); 31502f624b80SAlberto Garcia qdict_extract_subqdict(bs->options, NULL, child_key_dot); 315150196d7aSAlberto Garcia qdict_del(bs->explicit_options, child->name); 315250196d7aSAlberto Garcia qdict_del(bs->options, child->name); 31532f624b80SAlberto Garcia g_free(child_key_dot); 31542f624b80SAlberto Garcia } 31552f624b80SAlberto Garcia 3156b6ad491aSKevin Wolf /* Check if any unknown options were used */ 31577ad2757fSPaolo Bonzini if (qdict_size(options) != 0) { 3158b6ad491aSKevin Wolf const QDictEntry *entry = qdict_first(options); 31595acd9d81SMax Reitz if (flags & BDRV_O_PROTOCOL) { 31605acd9d81SMax Reitz error_setg(errp, "Block protocol '%s' doesn't support the option " 31615acd9d81SMax Reitz "'%s'", drv->format_name, entry->key); 31625acd9d81SMax Reitz } else { 3163d0e46a55SMax Reitz error_setg(errp, 3164d0e46a55SMax Reitz "Block format '%s' does not support the option '%s'", 3165d0e46a55SMax Reitz drv->format_name, entry->key); 31665acd9d81SMax Reitz } 3167b6ad491aSKevin Wolf 3168b6ad491aSKevin Wolf goto close_and_fail; 3169b6ad491aSKevin Wolf } 3170b6ad491aSKevin Wolf 31715c8cab48SKevin Wolf bdrv_parent_cb_change_media(bs, true); 3172b6ce07aaSKevin Wolf 3173cb3e7f08SMarc-André Lureau qobject_unref(options); 31748961be33SAlberto Garcia options = NULL; 3175dd62f1caSKevin Wolf 3176dd62f1caSKevin Wolf /* For snapshot=on, create a temporary qcow2 overlay. bs points to the 3177dd62f1caSKevin Wolf * temporary snapshot afterwards. */ 3178dd62f1caSKevin Wolf if (snapshot_flags) { 317966836189SMax Reitz BlockDriverState *snapshot_bs; 318066836189SMax Reitz snapshot_bs = bdrv_append_temp_snapshot(bs, snapshot_flags, 318166836189SMax Reitz snapshot_options, &local_err); 318273176beeSKevin Wolf snapshot_options = NULL; 3183dd62f1caSKevin Wolf if (local_err) { 3184dd62f1caSKevin Wolf goto close_and_fail; 3185dd62f1caSKevin Wolf } 318666836189SMax Reitz /* We are not going to return bs but the overlay on top of it 318766836189SMax Reitz * (snapshot_bs); thus, we have to drop the strong reference to bs 31885b363937SMax Reitz * (which we obtained by calling bdrv_new()). bs will not be deleted, 31895b363937SMax Reitz * though, because the overlay still has a reference to it. */ 319066836189SMax Reitz bdrv_unref(bs); 319166836189SMax Reitz bs = snapshot_bs; 319266836189SMax Reitz } 319366836189SMax Reitz 31945b363937SMax Reitz return bs; 3195b6ce07aaSKevin Wolf 31968bfea15dSKevin Wolf fail: 31975696c6e3SKevin Wolf blk_unref(file); 3198cb3e7f08SMarc-André Lureau qobject_unref(snapshot_options); 3199cb3e7f08SMarc-André Lureau qobject_unref(bs->explicit_options); 3200cb3e7f08SMarc-André Lureau qobject_unref(bs->options); 3201cb3e7f08SMarc-André Lureau qobject_unref(options); 3202de9c0cecSKevin Wolf bs->options = NULL; 3203998cbd6aSManos Pitsidianakis bs->explicit_options = NULL; 3204f67503e5SMax Reitz bdrv_unref(bs); 320534b5d2c6SMax Reitz error_propagate(errp, local_err); 32065b363937SMax Reitz return NULL; 3207de9c0cecSKevin Wolf 3208b6ad491aSKevin Wolf close_and_fail: 3209f67503e5SMax Reitz bdrv_unref(bs); 3210cb3e7f08SMarc-André Lureau qobject_unref(snapshot_options); 3211cb3e7f08SMarc-André Lureau qobject_unref(options); 321234b5d2c6SMax Reitz error_propagate(errp, local_err); 32135b363937SMax Reitz return NULL; 3214b6ce07aaSKevin Wolf } 3215b6ce07aaSKevin Wolf 32165b363937SMax Reitz BlockDriverState *bdrv_open(const char *filename, const char *reference, 32175b363937SMax Reitz QDict *options, int flags, Error **errp) 3218f3930ed0SKevin Wolf { 32195b363937SMax Reitz return bdrv_open_inherit(filename, reference, options, flags, NULL, 3220ce343771SMax Reitz NULL, errp); 3221f3930ed0SKevin Wolf } 3222f3930ed0SKevin Wolf 3223faf116b4SAlberto Garcia /* Return true if the NULL-terminated @list contains @str */ 3224faf116b4SAlberto Garcia static bool is_str_in_list(const char *str, const char *const *list) 3225faf116b4SAlberto Garcia { 3226faf116b4SAlberto Garcia if (str && list) { 3227faf116b4SAlberto Garcia int i; 3228faf116b4SAlberto Garcia for (i = 0; list[i] != NULL; i++) { 3229faf116b4SAlberto Garcia if (!strcmp(str, list[i])) { 3230faf116b4SAlberto Garcia return true; 3231faf116b4SAlberto Garcia } 3232faf116b4SAlberto Garcia } 3233faf116b4SAlberto Garcia } 3234faf116b4SAlberto Garcia return false; 3235faf116b4SAlberto Garcia } 3236faf116b4SAlberto Garcia 3237faf116b4SAlberto Garcia /* 3238faf116b4SAlberto Garcia * Check that every option set in @bs->options is also set in 3239faf116b4SAlberto Garcia * @new_opts. 3240faf116b4SAlberto Garcia * 3241faf116b4SAlberto Garcia * Options listed in the common_options list and in 3242faf116b4SAlberto Garcia * @bs->drv->mutable_opts are skipped. 3243faf116b4SAlberto Garcia * 3244faf116b4SAlberto Garcia * Return 0 on success, otherwise return -EINVAL and set @errp. 3245faf116b4SAlberto Garcia */ 3246faf116b4SAlberto Garcia static int bdrv_reset_options_allowed(BlockDriverState *bs, 3247faf116b4SAlberto Garcia const QDict *new_opts, Error **errp) 3248faf116b4SAlberto Garcia { 3249faf116b4SAlberto Garcia const QDictEntry *e; 3250faf116b4SAlberto Garcia /* These options are common to all block drivers and are handled 3251faf116b4SAlberto Garcia * in bdrv_reopen_prepare() so they can be left out of @new_opts */ 3252faf116b4SAlberto Garcia const char *const common_options[] = { 3253faf116b4SAlberto Garcia "node-name", "discard", "cache.direct", "cache.no-flush", 3254faf116b4SAlberto Garcia "read-only", "auto-read-only", "detect-zeroes", NULL 3255faf116b4SAlberto Garcia }; 3256faf116b4SAlberto Garcia 3257faf116b4SAlberto Garcia for (e = qdict_first(bs->options); e; e = qdict_next(bs->options, e)) { 3258faf116b4SAlberto Garcia if (!qdict_haskey(new_opts, e->key) && 3259faf116b4SAlberto Garcia !is_str_in_list(e->key, common_options) && 3260faf116b4SAlberto Garcia !is_str_in_list(e->key, bs->drv->mutable_opts)) { 3261faf116b4SAlberto Garcia error_setg(errp, "Option '%s' cannot be reset " 3262faf116b4SAlberto Garcia "to its default value", e->key); 3263faf116b4SAlberto Garcia return -EINVAL; 3264faf116b4SAlberto Garcia } 3265faf116b4SAlberto Garcia } 3266faf116b4SAlberto Garcia 3267faf116b4SAlberto Garcia return 0; 3268faf116b4SAlberto Garcia } 3269faf116b4SAlberto Garcia 3270e971aa12SJeff Cody /* 3271cb828c31SAlberto Garcia * Returns true if @child can be reached recursively from @bs 3272cb828c31SAlberto Garcia */ 3273cb828c31SAlberto Garcia static bool bdrv_recurse_has_child(BlockDriverState *bs, 3274cb828c31SAlberto Garcia BlockDriverState *child) 3275cb828c31SAlberto Garcia { 3276cb828c31SAlberto Garcia BdrvChild *c; 3277cb828c31SAlberto Garcia 3278cb828c31SAlberto Garcia if (bs == child) { 3279cb828c31SAlberto Garcia return true; 3280cb828c31SAlberto Garcia } 3281cb828c31SAlberto Garcia 3282cb828c31SAlberto Garcia QLIST_FOREACH(c, &bs->children, next) { 3283cb828c31SAlberto Garcia if (bdrv_recurse_has_child(c->bs, child)) { 3284cb828c31SAlberto Garcia return true; 3285cb828c31SAlberto Garcia } 3286cb828c31SAlberto Garcia } 3287cb828c31SAlberto Garcia 3288cb828c31SAlberto Garcia return false; 3289cb828c31SAlberto Garcia } 3290cb828c31SAlberto Garcia 3291cb828c31SAlberto Garcia /* 3292e971aa12SJeff Cody * Adds a BlockDriverState to a simple queue for an atomic, transactional 3293e971aa12SJeff Cody * reopen of multiple devices. 3294e971aa12SJeff Cody * 3295859443b0SVladimir Sementsov-Ogievskiy * bs_queue can either be an existing BlockReopenQueue that has had QTAILQ_INIT 3296e971aa12SJeff Cody * already performed, or alternatively may be NULL a new BlockReopenQueue will 3297e971aa12SJeff Cody * be created and initialized. This newly created BlockReopenQueue should be 3298e971aa12SJeff Cody * passed back in for subsequent calls that are intended to be of the same 3299e971aa12SJeff Cody * atomic 'set'. 3300e971aa12SJeff Cody * 3301e971aa12SJeff Cody * bs is the BlockDriverState to add to the reopen queue. 3302e971aa12SJeff Cody * 33034d2cb092SKevin Wolf * options contains the changed options for the associated bs 33044d2cb092SKevin Wolf * (the BlockReopenQueue takes ownership) 33054d2cb092SKevin Wolf * 3306e971aa12SJeff Cody * flags contains the open flags for the associated bs 3307e971aa12SJeff Cody * 3308e971aa12SJeff Cody * returns a pointer to bs_queue, which is either the newly allocated 3309e971aa12SJeff Cody * bs_queue, or the existing bs_queue being used. 3310e971aa12SJeff Cody * 33111a63a907SKevin Wolf * bs must be drained between bdrv_reopen_queue() and bdrv_reopen_multiple(). 3312e971aa12SJeff Cody */ 331328518102SKevin Wolf static BlockReopenQueue *bdrv_reopen_queue_child(BlockReopenQueue *bs_queue, 33144d2cb092SKevin Wolf BlockDriverState *bs, 331528518102SKevin Wolf QDict *options, 331628518102SKevin Wolf const BdrvChildRole *role, 331728518102SKevin Wolf QDict *parent_options, 3318077e8e20SAlberto Garcia int parent_flags, 3319077e8e20SAlberto Garcia bool keep_old_opts) 3320e971aa12SJeff Cody { 3321e971aa12SJeff Cody assert(bs != NULL); 3322e971aa12SJeff Cody 3323e971aa12SJeff Cody BlockReopenQueueEntry *bs_entry; 332467251a31SKevin Wolf BdrvChild *child; 33259aa09dddSAlberto Garcia QDict *old_options, *explicit_options, *options_copy; 33269aa09dddSAlberto Garcia int flags; 33279aa09dddSAlberto Garcia QemuOpts *opts; 332867251a31SKevin Wolf 33291a63a907SKevin Wolf /* Make sure that the caller remembered to use a drained section. This is 33301a63a907SKevin Wolf * important to avoid graph changes between the recursive queuing here and 33311a63a907SKevin Wolf * bdrv_reopen_multiple(). */ 33321a63a907SKevin Wolf assert(bs->quiesce_counter > 0); 33331a63a907SKevin Wolf 3334e971aa12SJeff Cody if (bs_queue == NULL) { 3335e971aa12SJeff Cody bs_queue = g_new0(BlockReopenQueue, 1); 3336859443b0SVladimir Sementsov-Ogievskiy QTAILQ_INIT(bs_queue); 3337e971aa12SJeff Cody } 3338e971aa12SJeff Cody 33394d2cb092SKevin Wolf if (!options) { 33404d2cb092SKevin Wolf options = qdict_new(); 33414d2cb092SKevin Wolf } 33424d2cb092SKevin Wolf 33435b7ba05fSAlberto Garcia /* Check if this BlockDriverState is already in the queue */ 3344859443b0SVladimir Sementsov-Ogievskiy QTAILQ_FOREACH(bs_entry, bs_queue, entry) { 33455b7ba05fSAlberto Garcia if (bs == bs_entry->state.bs) { 33465b7ba05fSAlberto Garcia break; 33475b7ba05fSAlberto Garcia } 33485b7ba05fSAlberto Garcia } 33495b7ba05fSAlberto Garcia 335028518102SKevin Wolf /* 335128518102SKevin Wolf * Precedence of options: 335228518102SKevin Wolf * 1. Explicitly passed in options (highest) 33539aa09dddSAlberto Garcia * 2. Retained from explicitly set options of bs 33549aa09dddSAlberto Garcia * 3. Inherited from parent node 33559aa09dddSAlberto Garcia * 4. Retained from effective options of bs 335628518102SKevin Wolf */ 335728518102SKevin Wolf 3358145f598eSKevin Wolf /* Old explicitly set values (don't overwrite by inherited value) */ 3359077e8e20SAlberto Garcia if (bs_entry || keep_old_opts) { 3360077e8e20SAlberto Garcia old_options = qdict_clone_shallow(bs_entry ? 3361077e8e20SAlberto Garcia bs_entry->state.explicit_options : 3362077e8e20SAlberto Garcia bs->explicit_options); 3363145f598eSKevin Wolf bdrv_join_options(bs, options, old_options); 3364cb3e7f08SMarc-André Lureau qobject_unref(old_options); 3365077e8e20SAlberto Garcia } 3366145f598eSKevin Wolf 3367145f598eSKevin Wolf explicit_options = qdict_clone_shallow(options); 3368145f598eSKevin Wolf 336928518102SKevin Wolf /* Inherit from parent node */ 337028518102SKevin Wolf if (parent_options) { 33719aa09dddSAlberto Garcia flags = 0; 33728e2160e2SKevin Wolf role->inherit_options(&flags, options, parent_flags, parent_options); 33739aa09dddSAlberto Garcia } else { 33749aa09dddSAlberto Garcia flags = bdrv_get_flags(bs); 337528518102SKevin Wolf } 337628518102SKevin Wolf 3377077e8e20SAlberto Garcia if (keep_old_opts) { 337828518102SKevin Wolf /* Old values are used for options that aren't set yet */ 33794d2cb092SKevin Wolf old_options = qdict_clone_shallow(bs->options); 3380cddff5baSKevin Wolf bdrv_join_options(bs, options, old_options); 3381cb3e7f08SMarc-André Lureau qobject_unref(old_options); 3382077e8e20SAlberto Garcia } 33834d2cb092SKevin Wolf 33849aa09dddSAlberto Garcia /* We have the final set of options so let's update the flags */ 33859aa09dddSAlberto Garcia options_copy = qdict_clone_shallow(options); 33869aa09dddSAlberto Garcia opts = qemu_opts_create(&bdrv_runtime_opts, NULL, 0, &error_abort); 33879aa09dddSAlberto Garcia qemu_opts_absorb_qdict(opts, options_copy, NULL); 33889aa09dddSAlberto Garcia update_flags_from_options(&flags, opts); 33899aa09dddSAlberto Garcia qemu_opts_del(opts); 33909aa09dddSAlberto Garcia qobject_unref(options_copy); 33919aa09dddSAlberto Garcia 3392fd452021SKevin Wolf /* bdrv_open_inherit() sets and clears some additional flags internally */ 3393f1f25a2eSKevin Wolf flags &= ~BDRV_O_PROTOCOL; 3394fd452021SKevin Wolf if (flags & BDRV_O_RDWR) { 3395fd452021SKevin Wolf flags |= BDRV_O_ALLOW_RDWR; 3396fd452021SKevin Wolf } 3397f1f25a2eSKevin Wolf 33981857c97bSKevin Wolf if (!bs_entry) { 33991857c97bSKevin Wolf bs_entry = g_new0(BlockReopenQueueEntry, 1); 3400859443b0SVladimir Sementsov-Ogievskiy QTAILQ_INSERT_TAIL(bs_queue, bs_entry, entry); 34011857c97bSKevin Wolf } else { 3402cb3e7f08SMarc-André Lureau qobject_unref(bs_entry->state.options); 3403cb3e7f08SMarc-André Lureau qobject_unref(bs_entry->state.explicit_options); 34041857c97bSKevin Wolf } 34051857c97bSKevin Wolf 34061857c97bSKevin Wolf bs_entry->state.bs = bs; 34071857c97bSKevin Wolf bs_entry->state.options = options; 34081857c97bSKevin Wolf bs_entry->state.explicit_options = explicit_options; 34091857c97bSKevin Wolf bs_entry->state.flags = flags; 34101857c97bSKevin Wolf 341130450259SKevin Wolf /* This needs to be overwritten in bdrv_reopen_prepare() */ 341230450259SKevin Wolf bs_entry->state.perm = UINT64_MAX; 341330450259SKevin Wolf bs_entry->state.shared_perm = 0; 341430450259SKevin Wolf 34158546632eSAlberto Garcia /* 34168546632eSAlberto Garcia * If keep_old_opts is false then it means that unspecified 34178546632eSAlberto Garcia * options must be reset to their original value. We don't allow 34188546632eSAlberto Garcia * resetting 'backing' but we need to know if the option is 34198546632eSAlberto Garcia * missing in order to decide if we have to return an error. 34208546632eSAlberto Garcia */ 34218546632eSAlberto Garcia if (!keep_old_opts) { 34228546632eSAlberto Garcia bs_entry->state.backing_missing = 34238546632eSAlberto Garcia !qdict_haskey(options, "backing") && 34248546632eSAlberto Garcia !qdict_haskey(options, "backing.driver"); 34258546632eSAlberto Garcia } 34268546632eSAlberto Garcia 342767251a31SKevin Wolf QLIST_FOREACH(child, &bs->children, next) { 34288546632eSAlberto Garcia QDict *new_child_options = NULL; 34298546632eSAlberto Garcia bool child_keep_old = keep_old_opts; 343067251a31SKevin Wolf 34314c9dfe5dSKevin Wolf /* reopen can only change the options of block devices that were 34324c9dfe5dSKevin Wolf * implicitly created and inherited options. For other (referenced) 34334c9dfe5dSKevin Wolf * block devices, a syntax like "backing.foo" results in an error. */ 343467251a31SKevin Wolf if (child->bs->inherits_from != bs) { 343567251a31SKevin Wolf continue; 343667251a31SKevin Wolf } 343767251a31SKevin Wolf 34388546632eSAlberto Garcia /* Check if the options contain a child reference */ 34398546632eSAlberto Garcia if (qdict_haskey(options, child->name)) { 34408546632eSAlberto Garcia const char *childref = qdict_get_try_str(options, child->name); 34418546632eSAlberto Garcia /* 34428546632eSAlberto Garcia * The current child must not be reopened if the child 34438546632eSAlberto Garcia * reference is null or points to a different node. 34448546632eSAlberto Garcia */ 34458546632eSAlberto Garcia if (g_strcmp0(childref, child->bs->node_name)) { 34468546632eSAlberto Garcia continue; 34478546632eSAlberto Garcia } 34488546632eSAlberto Garcia /* 34498546632eSAlberto Garcia * If the child reference points to the current child then 34508546632eSAlberto Garcia * reopen it with its existing set of options (note that 34518546632eSAlberto Garcia * it can still inherit new options from the parent). 34528546632eSAlberto Garcia */ 34538546632eSAlberto Garcia child_keep_old = true; 34548546632eSAlberto Garcia } else { 34558546632eSAlberto Garcia /* Extract child options ("child-name.*") */ 34568546632eSAlberto Garcia char *child_key_dot = g_strdup_printf("%s.", child->name); 34572f624b80SAlberto Garcia qdict_extract_subqdict(explicit_options, NULL, child_key_dot); 34584c9dfe5dSKevin Wolf qdict_extract_subqdict(options, &new_child_options, child_key_dot); 34594c9dfe5dSKevin Wolf g_free(child_key_dot); 34608546632eSAlberto Garcia } 34614c9dfe5dSKevin Wolf 34629aa09dddSAlberto Garcia bdrv_reopen_queue_child(bs_queue, child->bs, new_child_options, 34638546632eSAlberto Garcia child->role, options, flags, child_keep_old); 3464e971aa12SJeff Cody } 3465e971aa12SJeff Cody 3466e971aa12SJeff Cody return bs_queue; 3467e971aa12SJeff Cody } 3468e971aa12SJeff Cody 346928518102SKevin Wolf BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue, 347028518102SKevin Wolf BlockDriverState *bs, 3471077e8e20SAlberto Garcia QDict *options, bool keep_old_opts) 347228518102SKevin Wolf { 3473077e8e20SAlberto Garcia return bdrv_reopen_queue_child(bs_queue, bs, options, NULL, NULL, 0, 3474077e8e20SAlberto Garcia keep_old_opts); 347528518102SKevin Wolf } 347628518102SKevin Wolf 3477e971aa12SJeff Cody /* 3478e971aa12SJeff Cody * Reopen multiple BlockDriverStates atomically & transactionally. 3479e971aa12SJeff Cody * 3480e971aa12SJeff Cody * The queue passed in (bs_queue) must have been built up previous 3481e971aa12SJeff Cody * via bdrv_reopen_queue(). 3482e971aa12SJeff Cody * 3483e971aa12SJeff Cody * Reopens all BDS specified in the queue, with the appropriate 3484e971aa12SJeff Cody * flags. All devices are prepared for reopen, and failure of any 348550d6a8a3SStefan Weil * device will cause all device changes to be abandoned, and intermediate 3486e971aa12SJeff Cody * data cleaned up. 3487e971aa12SJeff Cody * 3488e971aa12SJeff Cody * If all devices prepare successfully, then the changes are committed 3489e971aa12SJeff Cody * to all devices. 3490e971aa12SJeff Cody * 34911a63a907SKevin Wolf * All affected nodes must be drained between bdrv_reopen_queue() and 34921a63a907SKevin Wolf * bdrv_reopen_multiple(). 3493e971aa12SJeff Cody */ 34945019aeceSAlberto Garcia int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp) 3495e971aa12SJeff Cody { 3496e971aa12SJeff Cody int ret = -1; 3497e971aa12SJeff Cody BlockReopenQueueEntry *bs_entry, *next; 3498e971aa12SJeff Cody 3499e971aa12SJeff Cody assert(bs_queue != NULL); 3500e971aa12SJeff Cody 3501859443b0SVladimir Sementsov-Ogievskiy QTAILQ_FOREACH(bs_entry, bs_queue, entry) { 35021a63a907SKevin Wolf assert(bs_entry->state.bs->quiesce_counter > 0); 3503a4615ab3SKevin Wolf if (bdrv_reopen_prepare(&bs_entry->state, bs_queue, errp)) { 3504e971aa12SJeff Cody goto cleanup; 3505e971aa12SJeff Cody } 3506e971aa12SJeff Cody bs_entry->prepared = true; 3507e971aa12SJeff Cody } 3508e971aa12SJeff Cody 3509859443b0SVladimir Sementsov-Ogievskiy QTAILQ_FOREACH(bs_entry, bs_queue, entry) { 351069b736e7SKevin Wolf BDRVReopenState *state = &bs_entry->state; 351169b736e7SKevin Wolf ret = bdrv_check_perm(state->bs, bs_queue, state->perm, 35129eab1544SMax Reitz state->shared_perm, NULL, NULL, errp); 351369b736e7SKevin Wolf if (ret < 0) { 351469b736e7SKevin Wolf goto cleanup_perm; 351569b736e7SKevin Wolf } 3516cb828c31SAlberto Garcia /* Check if new_backing_bs would accept the new permissions */ 3517cb828c31SAlberto Garcia if (state->replace_backing_bs && state->new_backing_bs) { 3518cb828c31SAlberto Garcia uint64_t nperm, nshared; 3519cb828c31SAlberto Garcia bdrv_child_perm(state->bs, state->new_backing_bs, 3520cb828c31SAlberto Garcia NULL, &child_backing, bs_queue, 3521cb828c31SAlberto Garcia state->perm, state->shared_perm, 3522cb828c31SAlberto Garcia &nperm, &nshared); 3523cb828c31SAlberto Garcia ret = bdrv_check_update_perm(state->new_backing_bs, NULL, 35249eab1544SMax Reitz nperm, nshared, NULL, NULL, errp); 3525cb828c31SAlberto Garcia if (ret < 0) { 3526cb828c31SAlberto Garcia goto cleanup_perm; 3527cb828c31SAlberto Garcia } 3528cb828c31SAlberto Garcia } 352969b736e7SKevin Wolf bs_entry->perms_checked = true; 353069b736e7SKevin Wolf } 353169b736e7SKevin Wolf 3532fcd6a4f4SVladimir Sementsov-Ogievskiy /* 3533fcd6a4f4SVladimir Sementsov-Ogievskiy * If we reach this point, we have success and just need to apply the 3534fcd6a4f4SVladimir Sementsov-Ogievskiy * changes. 3535fcd6a4f4SVladimir Sementsov-Ogievskiy * 3536fcd6a4f4SVladimir Sementsov-Ogievskiy * Reverse order is used to comfort qcow2 driver: on commit it need to write 3537fcd6a4f4SVladimir Sementsov-Ogievskiy * IN_USE flag to the image, to mark bitmaps in the image as invalid. But 3538fcd6a4f4SVladimir Sementsov-Ogievskiy * children are usually goes after parents in reopen-queue, so go from last 3539fcd6a4f4SVladimir Sementsov-Ogievskiy * to first element. 3540e971aa12SJeff Cody */ 3541fcd6a4f4SVladimir Sementsov-Ogievskiy QTAILQ_FOREACH_REVERSE(bs_entry, bs_queue, entry) { 3542e971aa12SJeff Cody bdrv_reopen_commit(&bs_entry->state); 3543e971aa12SJeff Cody } 3544e971aa12SJeff Cody 3545e971aa12SJeff Cody ret = 0; 354669b736e7SKevin Wolf cleanup_perm: 3547859443b0SVladimir Sementsov-Ogievskiy QTAILQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) { 354869b736e7SKevin Wolf BDRVReopenState *state = &bs_entry->state; 3549e971aa12SJeff Cody 355069b736e7SKevin Wolf if (!bs_entry->perms_checked) { 355169b736e7SKevin Wolf continue; 355269b736e7SKevin Wolf } 355369b736e7SKevin Wolf 355469b736e7SKevin Wolf if (ret == 0) { 355569b736e7SKevin Wolf bdrv_set_perm(state->bs, state->perm, state->shared_perm); 355669b736e7SKevin Wolf } else { 355769b736e7SKevin Wolf bdrv_abort_perm_update(state->bs); 3558cb828c31SAlberto Garcia if (state->replace_backing_bs && state->new_backing_bs) { 3559cb828c31SAlberto Garcia bdrv_abort_perm_update(state->new_backing_bs); 3560cb828c31SAlberto Garcia } 356169b736e7SKevin Wolf } 356269b736e7SKevin Wolf } 3563e971aa12SJeff Cody cleanup: 3564859443b0SVladimir Sementsov-Ogievskiy QTAILQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) { 35651bab38e7SAlberto Garcia if (ret) { 35661bab38e7SAlberto Garcia if (bs_entry->prepared) { 3567e971aa12SJeff Cody bdrv_reopen_abort(&bs_entry->state); 35681bab38e7SAlberto Garcia } 3569cb3e7f08SMarc-André Lureau qobject_unref(bs_entry->state.explicit_options); 3570cb3e7f08SMarc-André Lureau qobject_unref(bs_entry->state.options); 35714c8350feSAlberto Garcia } 3572cb828c31SAlberto Garcia if (bs_entry->state.new_backing_bs) { 3573cb828c31SAlberto Garcia bdrv_unref(bs_entry->state.new_backing_bs); 3574cb828c31SAlberto Garcia } 3575e971aa12SJeff Cody g_free(bs_entry); 3576e971aa12SJeff Cody } 3577e971aa12SJeff Cody g_free(bs_queue); 357840840e41SAlberto Garcia 3579e971aa12SJeff Cody return ret; 3580e971aa12SJeff Cody } 3581e971aa12SJeff Cody 35826e1000a8SAlberto Garcia int bdrv_reopen_set_read_only(BlockDriverState *bs, bool read_only, 35836e1000a8SAlberto Garcia Error **errp) 35846e1000a8SAlberto Garcia { 35856e1000a8SAlberto Garcia int ret; 35866e1000a8SAlberto Garcia BlockReopenQueue *queue; 35876e1000a8SAlberto Garcia QDict *opts = qdict_new(); 35886e1000a8SAlberto Garcia 35896e1000a8SAlberto Garcia qdict_put_bool(opts, BDRV_OPT_READ_ONLY, read_only); 35906e1000a8SAlberto Garcia 35916e1000a8SAlberto Garcia bdrv_subtree_drained_begin(bs); 3592077e8e20SAlberto Garcia queue = bdrv_reopen_queue(NULL, bs, opts, true); 35935019aeceSAlberto Garcia ret = bdrv_reopen_multiple(queue, errp); 35946e1000a8SAlberto Garcia bdrv_subtree_drained_end(bs); 35956e1000a8SAlberto Garcia 35966e1000a8SAlberto Garcia return ret; 35976e1000a8SAlberto Garcia } 35986e1000a8SAlberto Garcia 359930450259SKevin Wolf static BlockReopenQueueEntry *find_parent_in_reopen_queue(BlockReopenQueue *q, 360030450259SKevin Wolf BdrvChild *c) 360130450259SKevin Wolf { 360230450259SKevin Wolf BlockReopenQueueEntry *entry; 360330450259SKevin Wolf 3604859443b0SVladimir Sementsov-Ogievskiy QTAILQ_FOREACH(entry, q, entry) { 360530450259SKevin Wolf BlockDriverState *bs = entry->state.bs; 360630450259SKevin Wolf BdrvChild *child; 360730450259SKevin Wolf 360830450259SKevin Wolf QLIST_FOREACH(child, &bs->children, next) { 360930450259SKevin Wolf if (child == c) { 361030450259SKevin Wolf return entry; 361130450259SKevin Wolf } 361230450259SKevin Wolf } 361330450259SKevin Wolf } 361430450259SKevin Wolf 361530450259SKevin Wolf return NULL; 361630450259SKevin Wolf } 361730450259SKevin Wolf 361830450259SKevin Wolf static void bdrv_reopen_perm(BlockReopenQueue *q, BlockDriverState *bs, 361930450259SKevin Wolf uint64_t *perm, uint64_t *shared) 362030450259SKevin Wolf { 362130450259SKevin Wolf BdrvChild *c; 362230450259SKevin Wolf BlockReopenQueueEntry *parent; 362330450259SKevin Wolf uint64_t cumulative_perms = 0; 362430450259SKevin Wolf uint64_t cumulative_shared_perms = BLK_PERM_ALL; 362530450259SKevin Wolf 362630450259SKevin Wolf QLIST_FOREACH(c, &bs->parents, next_parent) { 362730450259SKevin Wolf parent = find_parent_in_reopen_queue(q, c); 362830450259SKevin Wolf if (!parent) { 362930450259SKevin Wolf cumulative_perms |= c->perm; 363030450259SKevin Wolf cumulative_shared_perms &= c->shared_perm; 363130450259SKevin Wolf } else { 363230450259SKevin Wolf uint64_t nperm, nshared; 363330450259SKevin Wolf 363430450259SKevin Wolf bdrv_child_perm(parent->state.bs, bs, c, c->role, q, 363530450259SKevin Wolf parent->state.perm, parent->state.shared_perm, 363630450259SKevin Wolf &nperm, &nshared); 363730450259SKevin Wolf 363830450259SKevin Wolf cumulative_perms |= nperm; 363930450259SKevin Wolf cumulative_shared_perms &= nshared; 364030450259SKevin Wolf } 364130450259SKevin Wolf } 364230450259SKevin Wolf *perm = cumulative_perms; 364330450259SKevin Wolf *shared = cumulative_shared_perms; 364430450259SKevin Wolf } 3645e971aa12SJeff Cody 3646e971aa12SJeff Cody /* 3647cb828c31SAlberto Garcia * Take a BDRVReopenState and check if the value of 'backing' in the 3648cb828c31SAlberto Garcia * reopen_state->options QDict is valid or not. 3649cb828c31SAlberto Garcia * 3650cb828c31SAlberto Garcia * If 'backing' is missing from the QDict then return 0. 3651cb828c31SAlberto Garcia * 3652cb828c31SAlberto Garcia * If 'backing' contains the node name of the backing file of 3653cb828c31SAlberto Garcia * reopen_state->bs then return 0. 3654cb828c31SAlberto Garcia * 3655cb828c31SAlberto Garcia * If 'backing' contains a different node name (or is null) then check 3656cb828c31SAlberto Garcia * whether the current backing file can be replaced with the new one. 3657cb828c31SAlberto Garcia * If that's the case then reopen_state->replace_backing_bs is set to 3658cb828c31SAlberto Garcia * true and reopen_state->new_backing_bs contains a pointer to the new 3659cb828c31SAlberto Garcia * backing BlockDriverState (or NULL). 3660cb828c31SAlberto Garcia * 3661cb828c31SAlberto Garcia * Return 0 on success, otherwise return < 0 and set @errp. 3662cb828c31SAlberto Garcia */ 3663cb828c31SAlberto Garcia static int bdrv_reopen_parse_backing(BDRVReopenState *reopen_state, 3664cb828c31SAlberto Garcia Error **errp) 3665cb828c31SAlberto Garcia { 3666cb828c31SAlberto Garcia BlockDriverState *bs = reopen_state->bs; 3667cb828c31SAlberto Garcia BlockDriverState *overlay_bs, *new_backing_bs; 3668cb828c31SAlberto Garcia QObject *value; 3669cb828c31SAlberto Garcia const char *str; 3670cb828c31SAlberto Garcia 3671cb828c31SAlberto Garcia value = qdict_get(reopen_state->options, "backing"); 3672cb828c31SAlberto Garcia if (value == NULL) { 3673cb828c31SAlberto Garcia return 0; 3674cb828c31SAlberto Garcia } 3675cb828c31SAlberto Garcia 3676cb828c31SAlberto Garcia switch (qobject_type(value)) { 3677cb828c31SAlberto Garcia case QTYPE_QNULL: 3678cb828c31SAlberto Garcia new_backing_bs = NULL; 3679cb828c31SAlberto Garcia break; 3680cb828c31SAlberto Garcia case QTYPE_QSTRING: 3681cb828c31SAlberto Garcia str = qobject_get_try_str(value); 3682cb828c31SAlberto Garcia new_backing_bs = bdrv_lookup_bs(NULL, str, errp); 3683cb828c31SAlberto Garcia if (new_backing_bs == NULL) { 3684cb828c31SAlberto Garcia return -EINVAL; 3685cb828c31SAlberto Garcia } else if (bdrv_recurse_has_child(new_backing_bs, bs)) { 3686cb828c31SAlberto Garcia error_setg(errp, "Making '%s' a backing file of '%s' " 3687cb828c31SAlberto Garcia "would create a cycle", str, bs->node_name); 3688cb828c31SAlberto Garcia return -EINVAL; 3689cb828c31SAlberto Garcia } 3690cb828c31SAlberto Garcia break; 3691cb828c31SAlberto Garcia default: 3692cb828c31SAlberto Garcia /* 'backing' does not allow any other data type */ 3693cb828c31SAlberto Garcia g_assert_not_reached(); 3694cb828c31SAlberto Garcia } 3695cb828c31SAlberto Garcia 3696cb828c31SAlberto Garcia /* 3697cb828c31SAlberto Garcia * TODO: before removing the x- prefix from x-blockdev-reopen we 3698cb828c31SAlberto Garcia * should move the new backing file into the right AioContext 3699cb828c31SAlberto Garcia * instead of returning an error. 3700cb828c31SAlberto Garcia */ 3701cb828c31SAlberto Garcia if (new_backing_bs) { 3702cb828c31SAlberto Garcia if (bdrv_get_aio_context(new_backing_bs) != bdrv_get_aio_context(bs)) { 3703cb828c31SAlberto Garcia error_setg(errp, "Cannot use a new backing file " 3704cb828c31SAlberto Garcia "with a different AioContext"); 3705cb828c31SAlberto Garcia return -EINVAL; 3706cb828c31SAlberto Garcia } 3707cb828c31SAlberto Garcia } 3708cb828c31SAlberto Garcia 3709cb828c31SAlberto Garcia /* 3710cb828c31SAlberto Garcia * Find the "actual" backing file by skipping all links that point 3711cb828c31SAlberto Garcia * to an implicit node, if any (e.g. a commit filter node). 3712cb828c31SAlberto Garcia */ 3713cb828c31SAlberto Garcia overlay_bs = bs; 3714cb828c31SAlberto Garcia while (backing_bs(overlay_bs) && backing_bs(overlay_bs)->implicit) { 3715cb828c31SAlberto Garcia overlay_bs = backing_bs(overlay_bs); 3716cb828c31SAlberto Garcia } 3717cb828c31SAlberto Garcia 3718cb828c31SAlberto Garcia /* If we want to replace the backing file we need some extra checks */ 3719cb828c31SAlberto Garcia if (new_backing_bs != backing_bs(overlay_bs)) { 3720cb828c31SAlberto Garcia /* Check for implicit nodes between bs and its backing file */ 3721cb828c31SAlberto Garcia if (bs != overlay_bs) { 3722cb828c31SAlberto Garcia error_setg(errp, "Cannot change backing link if '%s' has " 3723cb828c31SAlberto Garcia "an implicit backing file", bs->node_name); 3724cb828c31SAlberto Garcia return -EPERM; 3725cb828c31SAlberto Garcia } 3726cb828c31SAlberto Garcia /* Check if the backing link that we want to replace is frozen */ 3727cb828c31SAlberto Garcia if (bdrv_is_backing_chain_frozen(overlay_bs, backing_bs(overlay_bs), 3728cb828c31SAlberto Garcia errp)) { 3729cb828c31SAlberto Garcia return -EPERM; 3730cb828c31SAlberto Garcia } 3731cb828c31SAlberto Garcia reopen_state->replace_backing_bs = true; 3732cb828c31SAlberto Garcia if (new_backing_bs) { 3733cb828c31SAlberto Garcia bdrv_ref(new_backing_bs); 3734cb828c31SAlberto Garcia reopen_state->new_backing_bs = new_backing_bs; 3735cb828c31SAlberto Garcia } 3736cb828c31SAlberto Garcia } 3737cb828c31SAlberto Garcia 3738cb828c31SAlberto Garcia return 0; 3739cb828c31SAlberto Garcia } 3740cb828c31SAlberto Garcia 3741cb828c31SAlberto Garcia /* 3742e971aa12SJeff Cody * Prepares a BlockDriverState for reopen. All changes are staged in the 3743e971aa12SJeff Cody * 'opaque' field of the BDRVReopenState, which is used and allocated by 3744e971aa12SJeff Cody * the block driver layer .bdrv_reopen_prepare() 3745e971aa12SJeff Cody * 3746e971aa12SJeff Cody * bs is the BlockDriverState to reopen 3747e971aa12SJeff Cody * flags are the new open flags 3748e971aa12SJeff Cody * queue is the reopen queue 3749e971aa12SJeff Cody * 3750e971aa12SJeff Cody * Returns 0 on success, non-zero on error. On error errp will be set 3751e971aa12SJeff Cody * as well. 3752e971aa12SJeff Cody * 3753e971aa12SJeff Cody * On failure, bdrv_reopen_abort() will be called to clean up any data. 3754e971aa12SJeff Cody * It is the responsibility of the caller to then call the abort() or 3755e971aa12SJeff Cody * commit() for any other BDS that have been left in a prepare() state 3756e971aa12SJeff Cody * 3757e971aa12SJeff Cody */ 3758e971aa12SJeff Cody int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue, 3759e971aa12SJeff Cody Error **errp) 3760e971aa12SJeff Cody { 3761e971aa12SJeff Cody int ret = -1; 3762e6d79c41SAlberto Garcia int old_flags; 3763e971aa12SJeff Cody Error *local_err = NULL; 3764e971aa12SJeff Cody BlockDriver *drv; 3765ccf9dc07SKevin Wolf QemuOpts *opts; 37664c8350feSAlberto Garcia QDict *orig_reopen_opts; 3767593b3071SAlberto Garcia char *discard = NULL; 37683d8ce171SJeff Cody bool read_only; 37699ad08c44SMax Reitz bool drv_prepared = false; 3770e971aa12SJeff Cody 3771e971aa12SJeff Cody assert(reopen_state != NULL); 3772e971aa12SJeff Cody assert(reopen_state->bs->drv != NULL); 3773e971aa12SJeff Cody drv = reopen_state->bs->drv; 3774e971aa12SJeff Cody 37754c8350feSAlberto Garcia /* This function and each driver's bdrv_reopen_prepare() remove 37764c8350feSAlberto Garcia * entries from reopen_state->options as they are processed, so 37774c8350feSAlberto Garcia * we need to make a copy of the original QDict. */ 37784c8350feSAlberto Garcia orig_reopen_opts = qdict_clone_shallow(reopen_state->options); 37794c8350feSAlberto Garcia 3780ccf9dc07SKevin Wolf /* Process generic block layer options */ 3781ccf9dc07SKevin Wolf opts = qemu_opts_create(&bdrv_runtime_opts, NULL, 0, &error_abort); 3782ccf9dc07SKevin Wolf qemu_opts_absorb_qdict(opts, reopen_state->options, &local_err); 3783ccf9dc07SKevin Wolf if (local_err) { 3784ccf9dc07SKevin Wolf error_propagate(errp, local_err); 3785ccf9dc07SKevin Wolf ret = -EINVAL; 3786ccf9dc07SKevin Wolf goto error; 3787ccf9dc07SKevin Wolf } 3788ccf9dc07SKevin Wolf 3789e6d79c41SAlberto Garcia /* This was already called in bdrv_reopen_queue_child() so the flags 3790e6d79c41SAlberto Garcia * are up-to-date. This time we simply want to remove the options from 3791e6d79c41SAlberto Garcia * QemuOpts in order to indicate that they have been processed. */ 3792e6d79c41SAlberto Garcia old_flags = reopen_state->flags; 379391a097e7SKevin Wolf update_flags_from_options(&reopen_state->flags, opts); 3794e6d79c41SAlberto Garcia assert(old_flags == reopen_state->flags); 379591a097e7SKevin Wolf 3796415bbca8SAlberto Garcia discard = qemu_opt_get_del(opts, BDRV_OPT_DISCARD); 3797593b3071SAlberto Garcia if (discard != NULL) { 3798593b3071SAlberto Garcia if (bdrv_parse_discard_flags(discard, &reopen_state->flags) != 0) { 3799593b3071SAlberto Garcia error_setg(errp, "Invalid discard option"); 3800593b3071SAlberto Garcia ret = -EINVAL; 3801593b3071SAlberto Garcia goto error; 3802593b3071SAlberto Garcia } 3803593b3071SAlberto Garcia } 3804593b3071SAlberto Garcia 3805543770bdSAlberto Garcia reopen_state->detect_zeroes = 3806543770bdSAlberto Garcia bdrv_parse_detect_zeroes(opts, reopen_state->flags, &local_err); 3807543770bdSAlberto Garcia if (local_err) { 3808543770bdSAlberto Garcia error_propagate(errp, local_err); 3809543770bdSAlberto Garcia ret = -EINVAL; 3810543770bdSAlberto Garcia goto error; 3811543770bdSAlberto Garcia } 3812543770bdSAlberto Garcia 381357f9db9aSAlberto Garcia /* All other options (including node-name and driver) must be unchanged. 381457f9db9aSAlberto Garcia * Put them back into the QDict, so that they are checked at the end 381557f9db9aSAlberto Garcia * of this function. */ 381657f9db9aSAlberto Garcia qemu_opts_to_qdict(opts, reopen_state->options); 3817ccf9dc07SKevin Wolf 38183d8ce171SJeff Cody /* If we are to stay read-only, do not allow permission change 38193d8ce171SJeff Cody * to r/w. Attempting to set to r/w may fail if either BDRV_O_ALLOW_RDWR is 38203d8ce171SJeff Cody * not set, or if the BDS still has copy_on_read enabled */ 38213d8ce171SJeff Cody read_only = !(reopen_state->flags & BDRV_O_RDWR); 382254a32bfeSKevin Wolf ret = bdrv_can_set_read_only(reopen_state->bs, read_only, true, &local_err); 38233d8ce171SJeff Cody if (local_err) { 38243d8ce171SJeff Cody error_propagate(errp, local_err); 3825e971aa12SJeff Cody goto error; 3826e971aa12SJeff Cody } 3827e971aa12SJeff Cody 382830450259SKevin Wolf /* Calculate required permissions after reopening */ 382930450259SKevin Wolf bdrv_reopen_perm(queue, reopen_state->bs, 383030450259SKevin Wolf &reopen_state->perm, &reopen_state->shared_perm); 3831e971aa12SJeff Cody 3832e971aa12SJeff Cody ret = bdrv_flush(reopen_state->bs); 3833e971aa12SJeff Cody if (ret) { 3834455b0fdeSEric Blake error_setg_errno(errp, -ret, "Error flushing drive"); 3835e971aa12SJeff Cody goto error; 3836e971aa12SJeff Cody } 3837e971aa12SJeff Cody 3838e971aa12SJeff Cody if (drv->bdrv_reopen_prepare) { 3839faf116b4SAlberto Garcia /* 3840faf116b4SAlberto Garcia * If a driver-specific option is missing, it means that we 3841faf116b4SAlberto Garcia * should reset it to its default value. 3842faf116b4SAlberto Garcia * But not all options allow that, so we need to check it first. 3843faf116b4SAlberto Garcia */ 3844faf116b4SAlberto Garcia ret = bdrv_reset_options_allowed(reopen_state->bs, 3845faf116b4SAlberto Garcia reopen_state->options, errp); 3846faf116b4SAlberto Garcia if (ret) { 3847faf116b4SAlberto Garcia goto error; 3848faf116b4SAlberto Garcia } 3849faf116b4SAlberto Garcia 3850e971aa12SJeff Cody ret = drv->bdrv_reopen_prepare(reopen_state, queue, &local_err); 3851e971aa12SJeff Cody if (ret) { 3852e971aa12SJeff Cody if (local_err != NULL) { 3853e971aa12SJeff Cody error_propagate(errp, local_err); 3854e971aa12SJeff Cody } else { 3855f30c66baSMax Reitz bdrv_refresh_filename(reopen_state->bs); 3856d8b6895fSLuiz Capitulino error_setg(errp, "failed while preparing to reopen image '%s'", 3857e971aa12SJeff Cody reopen_state->bs->filename); 3858e971aa12SJeff Cody } 3859e971aa12SJeff Cody goto error; 3860e971aa12SJeff Cody } 3861e971aa12SJeff Cody } else { 3862e971aa12SJeff Cody /* It is currently mandatory to have a bdrv_reopen_prepare() 3863e971aa12SJeff Cody * handler for each supported drv. */ 386481e5f78aSAlberto Garcia error_setg(errp, "Block format '%s' used by node '%s' " 386581e5f78aSAlberto Garcia "does not support reopening files", drv->format_name, 386681e5f78aSAlberto Garcia bdrv_get_device_or_node_name(reopen_state->bs)); 3867e971aa12SJeff Cody ret = -1; 3868e971aa12SJeff Cody goto error; 3869e971aa12SJeff Cody } 3870e971aa12SJeff Cody 38719ad08c44SMax Reitz drv_prepared = true; 38729ad08c44SMax Reitz 3873bacd9b87SAlberto Garcia /* 3874bacd9b87SAlberto Garcia * We must provide the 'backing' option if the BDS has a backing 3875bacd9b87SAlberto Garcia * file or if the image file has a backing file name as part of 3876bacd9b87SAlberto Garcia * its metadata. Otherwise the 'backing' option can be omitted. 3877bacd9b87SAlberto Garcia */ 3878bacd9b87SAlberto Garcia if (drv->supports_backing && reopen_state->backing_missing && 3879bacd9b87SAlberto Garcia (backing_bs(reopen_state->bs) || reopen_state->bs->backing_file[0])) { 38808546632eSAlberto Garcia error_setg(errp, "backing is missing for '%s'", 38818546632eSAlberto Garcia reopen_state->bs->node_name); 38828546632eSAlberto Garcia ret = -EINVAL; 38838546632eSAlberto Garcia goto error; 38848546632eSAlberto Garcia } 38858546632eSAlberto Garcia 3886cb828c31SAlberto Garcia /* 3887cb828c31SAlberto Garcia * Allow changing the 'backing' option. The new value can be 3888cb828c31SAlberto Garcia * either a reference to an existing node (using its node name) 3889cb828c31SAlberto Garcia * or NULL to simply detach the current backing file. 3890cb828c31SAlberto Garcia */ 3891cb828c31SAlberto Garcia ret = bdrv_reopen_parse_backing(reopen_state, errp); 3892cb828c31SAlberto Garcia if (ret < 0) { 3893cb828c31SAlberto Garcia goto error; 3894cb828c31SAlberto Garcia } 3895cb828c31SAlberto Garcia qdict_del(reopen_state->options, "backing"); 3896cb828c31SAlberto Garcia 38974d2cb092SKevin Wolf /* Options that are not handled are only okay if they are unchanged 38984d2cb092SKevin Wolf * compared to the old state. It is expected that some options are only 38994d2cb092SKevin Wolf * used for the initial open, but not reopen (e.g. filename) */ 39004d2cb092SKevin Wolf if (qdict_size(reopen_state->options)) { 39014d2cb092SKevin Wolf const QDictEntry *entry = qdict_first(reopen_state->options); 39024d2cb092SKevin Wolf 39034d2cb092SKevin Wolf do { 390454fd1b0dSMax Reitz QObject *new = entry->value; 390554fd1b0dSMax Reitz QObject *old = qdict_get(reopen_state->bs->options, entry->key); 39064d2cb092SKevin Wolf 3907db905283SAlberto Garcia /* Allow child references (child_name=node_name) as long as they 3908db905283SAlberto Garcia * point to the current child (i.e. everything stays the same). */ 3909db905283SAlberto Garcia if (qobject_type(new) == QTYPE_QSTRING) { 3910db905283SAlberto Garcia BdrvChild *child; 3911db905283SAlberto Garcia QLIST_FOREACH(child, &reopen_state->bs->children, next) { 3912db905283SAlberto Garcia if (!strcmp(child->name, entry->key)) { 3913db905283SAlberto Garcia break; 3914db905283SAlberto Garcia } 3915db905283SAlberto Garcia } 3916db905283SAlberto Garcia 3917db905283SAlberto Garcia if (child) { 3918db905283SAlberto Garcia const char *str = qobject_get_try_str(new); 3919db905283SAlberto Garcia if (!strcmp(child->bs->node_name, str)) { 3920db905283SAlberto Garcia continue; /* Found child with this name, skip option */ 3921db905283SAlberto Garcia } 3922db905283SAlberto Garcia } 3923db905283SAlberto Garcia } 3924db905283SAlberto Garcia 392554fd1b0dSMax Reitz /* 392654fd1b0dSMax Reitz * TODO: When using -drive to specify blockdev options, all values 392754fd1b0dSMax Reitz * will be strings; however, when using -blockdev, blockdev-add or 392854fd1b0dSMax Reitz * filenames using the json:{} pseudo-protocol, they will be 392954fd1b0dSMax Reitz * correctly typed. 393054fd1b0dSMax Reitz * In contrast, reopening options are (currently) always strings 393154fd1b0dSMax Reitz * (because you can only specify them through qemu-io; all other 393254fd1b0dSMax Reitz * callers do not specify any options). 393354fd1b0dSMax Reitz * Therefore, when using anything other than -drive to create a BDS, 393454fd1b0dSMax Reitz * this cannot detect non-string options as unchanged, because 393554fd1b0dSMax Reitz * qobject_is_equal() always returns false for objects of different 393654fd1b0dSMax Reitz * type. In the future, this should be remedied by correctly typing 393754fd1b0dSMax Reitz * all options. For now, this is not too big of an issue because 393854fd1b0dSMax Reitz * the user can simply omit options which cannot be changed anyway, 393954fd1b0dSMax Reitz * so they will stay unchanged. 394054fd1b0dSMax Reitz */ 394154fd1b0dSMax Reitz if (!qobject_is_equal(new, old)) { 39424d2cb092SKevin Wolf error_setg(errp, "Cannot change the option '%s'", entry->key); 39434d2cb092SKevin Wolf ret = -EINVAL; 39444d2cb092SKevin Wolf goto error; 39454d2cb092SKevin Wolf } 39464d2cb092SKevin Wolf } while ((entry = qdict_next(reopen_state->options, entry))); 39474d2cb092SKevin Wolf } 39484d2cb092SKevin Wolf 3949e971aa12SJeff Cody ret = 0; 3950e971aa12SJeff Cody 39514c8350feSAlberto Garcia /* Restore the original reopen_state->options QDict */ 39524c8350feSAlberto Garcia qobject_unref(reopen_state->options); 39534c8350feSAlberto Garcia reopen_state->options = qobject_ref(orig_reopen_opts); 39544c8350feSAlberto Garcia 3955e971aa12SJeff Cody error: 39569ad08c44SMax Reitz if (ret < 0 && drv_prepared) { 39579ad08c44SMax Reitz /* drv->bdrv_reopen_prepare() has succeeded, so we need to 39589ad08c44SMax Reitz * call drv->bdrv_reopen_abort() before signaling an error 39599ad08c44SMax Reitz * (bdrv_reopen_multiple() will not call bdrv_reopen_abort() 39609ad08c44SMax Reitz * when the respective bdrv_reopen_prepare() has failed) */ 39619ad08c44SMax Reitz if (drv->bdrv_reopen_abort) { 39629ad08c44SMax Reitz drv->bdrv_reopen_abort(reopen_state); 39639ad08c44SMax Reitz } 39649ad08c44SMax Reitz } 3965ccf9dc07SKevin Wolf qemu_opts_del(opts); 39664c8350feSAlberto Garcia qobject_unref(orig_reopen_opts); 3967593b3071SAlberto Garcia g_free(discard); 3968e971aa12SJeff Cody return ret; 3969e971aa12SJeff Cody } 3970e971aa12SJeff Cody 3971e971aa12SJeff Cody /* 3972e971aa12SJeff Cody * Takes the staged changes for the reopen from bdrv_reopen_prepare(), and 3973e971aa12SJeff Cody * makes them final by swapping the staging BlockDriverState contents into 3974e971aa12SJeff Cody * the active BlockDriverState contents. 3975e971aa12SJeff Cody */ 3976e971aa12SJeff Cody void bdrv_reopen_commit(BDRVReopenState *reopen_state) 3977e971aa12SJeff Cody { 3978e971aa12SJeff Cody BlockDriver *drv; 397950bf65baSVladimir Sementsov-Ogievskiy BlockDriverState *bs; 398050196d7aSAlberto Garcia BdrvChild *child; 3981e971aa12SJeff Cody 3982e971aa12SJeff Cody assert(reopen_state != NULL); 398350bf65baSVladimir Sementsov-Ogievskiy bs = reopen_state->bs; 398450bf65baSVladimir Sementsov-Ogievskiy drv = bs->drv; 3985e971aa12SJeff Cody assert(drv != NULL); 3986e971aa12SJeff Cody 3987e971aa12SJeff Cody /* If there are any driver level actions to take */ 3988e971aa12SJeff Cody if (drv->bdrv_reopen_commit) { 3989e971aa12SJeff Cody drv->bdrv_reopen_commit(reopen_state); 3990e971aa12SJeff Cody } 3991e971aa12SJeff Cody 3992e971aa12SJeff Cody /* set BDS specific flags now */ 3993cb3e7f08SMarc-André Lureau qobject_unref(bs->explicit_options); 39944c8350feSAlberto Garcia qobject_unref(bs->options); 3995145f598eSKevin Wolf 399650bf65baSVladimir Sementsov-Ogievskiy bs->explicit_options = reopen_state->explicit_options; 39974c8350feSAlberto Garcia bs->options = reopen_state->options; 399850bf65baSVladimir Sementsov-Ogievskiy bs->open_flags = reopen_state->flags; 399950bf65baSVladimir Sementsov-Ogievskiy bs->read_only = !(reopen_state->flags & BDRV_O_RDWR); 4000543770bdSAlberto Garcia bs->detect_zeroes = reopen_state->detect_zeroes; 4001355ef4acSKevin Wolf 4002cb828c31SAlberto Garcia if (reopen_state->replace_backing_bs) { 4003cb828c31SAlberto Garcia qdict_del(bs->explicit_options, "backing"); 4004cb828c31SAlberto Garcia qdict_del(bs->options, "backing"); 4005cb828c31SAlberto Garcia } 4006cb828c31SAlberto Garcia 400750196d7aSAlberto Garcia /* Remove child references from bs->options and bs->explicit_options. 400850196d7aSAlberto Garcia * Child options were already removed in bdrv_reopen_queue_child() */ 400950196d7aSAlberto Garcia QLIST_FOREACH(child, &bs->children, next) { 401050196d7aSAlberto Garcia qdict_del(bs->explicit_options, child->name); 401150196d7aSAlberto Garcia qdict_del(bs->options, child->name); 401250196d7aSAlberto Garcia } 401350196d7aSAlberto Garcia 4014cb828c31SAlberto Garcia /* 4015cb828c31SAlberto Garcia * Change the backing file if a new one was specified. We do this 4016cb828c31SAlberto Garcia * after updating bs->options, so bdrv_refresh_filename() (called 4017cb828c31SAlberto Garcia * from bdrv_set_backing_hd()) has the new values. 4018cb828c31SAlberto Garcia */ 4019cb828c31SAlberto Garcia if (reopen_state->replace_backing_bs) { 4020cb828c31SAlberto Garcia BlockDriverState *old_backing_bs = backing_bs(bs); 4021cb828c31SAlberto Garcia assert(!old_backing_bs || !old_backing_bs->implicit); 4022cb828c31SAlberto Garcia /* Abort the permission update on the backing bs we're detaching */ 4023cb828c31SAlberto Garcia if (old_backing_bs) { 4024cb828c31SAlberto Garcia bdrv_abort_perm_update(old_backing_bs); 4025cb828c31SAlberto Garcia } 4026cb828c31SAlberto Garcia bdrv_set_backing_hd(bs, reopen_state->new_backing_bs, &error_abort); 4027cb828c31SAlberto Garcia } 4028cb828c31SAlberto Garcia 402950bf65baSVladimir Sementsov-Ogievskiy bdrv_refresh_limits(bs, NULL); 4030e971aa12SJeff Cody } 4031e971aa12SJeff Cody 4032e971aa12SJeff Cody /* 4033e971aa12SJeff Cody * Abort the reopen, and delete and free the staged changes in 4034e971aa12SJeff Cody * reopen_state 4035e971aa12SJeff Cody */ 4036e971aa12SJeff Cody void bdrv_reopen_abort(BDRVReopenState *reopen_state) 4037e971aa12SJeff Cody { 4038e971aa12SJeff Cody BlockDriver *drv; 4039e971aa12SJeff Cody 4040e971aa12SJeff Cody assert(reopen_state != NULL); 4041e971aa12SJeff Cody drv = reopen_state->bs->drv; 4042e971aa12SJeff Cody assert(drv != NULL); 4043e971aa12SJeff Cody 4044e971aa12SJeff Cody if (drv->bdrv_reopen_abort) { 4045e971aa12SJeff Cody drv->bdrv_reopen_abort(reopen_state); 4046e971aa12SJeff Cody } 4047e971aa12SJeff Cody } 4048e971aa12SJeff Cody 4049e971aa12SJeff Cody 405064dff520SMax Reitz static void bdrv_close(BlockDriverState *bs) 4051fc01f7e7Sbellard { 405233384421SMax Reitz BdrvAioNotifier *ban, *ban_next; 405350a3efb0SAlberto Garcia BdrvChild *child, *next; 405433384421SMax Reitz 405530f55fb8SMax Reitz assert(!bs->refcnt); 405699b7e775SAlberto Garcia 4057fc27291dSPaolo Bonzini bdrv_drained_begin(bs); /* complete I/O */ 405858fda173SStefan Hajnoczi bdrv_flush(bs); 405953ec73e2SFam Zheng bdrv_drain(bs); /* in case flush left pending I/O */ 4060fc27291dSPaolo Bonzini 40613cbc002cSPaolo Bonzini if (bs->drv) { 40623c005293SVladimir Sementsov-Ogievskiy if (bs->drv->bdrv_close) { 40639a7dedbcSKevin Wolf bs->drv->bdrv_close(bs); 40643c005293SVladimir Sementsov-Ogievskiy } 40659a4f4c31SKevin Wolf bs->drv = NULL; 406650a3efb0SAlberto Garcia } 40679a7dedbcSKevin Wolf 40686e93e7c4SKevin Wolf QLIST_FOREACH_SAFE(child, &bs->children, next, next) { 4069dd4118c7SAlberto Garcia bdrv_unref_child(bs, child); 40706e93e7c4SKevin Wolf } 40716e93e7c4SKevin Wolf 4072dd4118c7SAlberto Garcia bs->backing = NULL; 4073dd4118c7SAlberto Garcia bs->file = NULL; 40747267c094SAnthony Liguori g_free(bs->opaque); 4075ea2384d3Sbellard bs->opaque = NULL; 4076d3faa13eSPaolo Bonzini atomic_set(&bs->copy_on_read, 0); 4077a275fa42SPaolo Bonzini bs->backing_file[0] = '\0'; 4078a275fa42SPaolo Bonzini bs->backing_format[0] = '\0'; 40796405875cSPaolo Bonzini bs->total_sectors = 0; 408054115412SEric Blake bs->encrypted = false; 408154115412SEric Blake bs->sg = false; 4082cb3e7f08SMarc-André Lureau qobject_unref(bs->options); 4083cb3e7f08SMarc-André Lureau qobject_unref(bs->explicit_options); 4084de9c0cecSKevin Wolf bs->options = NULL; 4085998cbd6aSManos Pitsidianakis bs->explicit_options = NULL; 4086cb3e7f08SMarc-André Lureau qobject_unref(bs->full_open_options); 408791af7014SMax Reitz bs->full_open_options = NULL; 408866f82ceeSKevin Wolf 4089cca43ae1SVladimir Sementsov-Ogievskiy bdrv_release_named_dirty_bitmaps(bs); 4090cca43ae1SVladimir Sementsov-Ogievskiy assert(QLIST_EMPTY(&bs->dirty_bitmaps)); 4091cca43ae1SVladimir Sementsov-Ogievskiy 409233384421SMax Reitz QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) { 409333384421SMax Reitz g_free(ban); 409433384421SMax Reitz } 409533384421SMax Reitz QLIST_INIT(&bs->aio_notifiers); 4096fc27291dSPaolo Bonzini bdrv_drained_end(bs); 4097b338082bSbellard } 4098b338082bSbellard 40992bc93fedSMORITA Kazutaka void bdrv_close_all(void) 41002bc93fedSMORITA Kazutaka { 4101b3b5299dSKevin Wolf assert(job_next(NULL) == NULL); 4102cd7fca95SKevin Wolf nbd_export_close_all(); 41032bc93fedSMORITA Kazutaka 4104ca9bd24cSMax Reitz /* Drop references from requests still in flight, such as canceled block 4105ca9bd24cSMax Reitz * jobs whose AIO context has not been polled yet */ 4106ca9bd24cSMax Reitz bdrv_drain_all(); 4107ca9bd24cSMax Reitz 4108ca9bd24cSMax Reitz blk_remove_all_bs(); 4109ca9bd24cSMax Reitz blockdev_close_all_bdrv_states(); 4110ca9bd24cSMax Reitz 4111a1a2af07SKevin Wolf assert(QTAILQ_EMPTY(&all_bdrv_states)); 41122bc93fedSMORITA Kazutaka } 41132bc93fedSMORITA Kazutaka 4114d0ac0380SKevin Wolf static bool should_update_child(BdrvChild *c, BlockDriverState *to) 4115dd62f1caSKevin Wolf { 41162f30b7c3SVladimir Sementsov-Ogievskiy GQueue *queue; 41172f30b7c3SVladimir Sementsov-Ogievskiy GHashTable *found; 41182f30b7c3SVladimir Sementsov-Ogievskiy bool ret; 4119dd62f1caSKevin Wolf 412026de9438SKevin Wolf if (c->role->stay_at_node) { 4121d0ac0380SKevin Wolf return false; 412226de9438SKevin Wolf } 4123d0ac0380SKevin Wolf 4124ec9f10feSMax Reitz /* If the child @c belongs to the BDS @to, replacing the current 4125ec9f10feSMax Reitz * c->bs by @to would mean to create a loop. 4126ec9f10feSMax Reitz * 4127ec9f10feSMax Reitz * Such a case occurs when appending a BDS to a backing chain. 4128ec9f10feSMax Reitz * For instance, imagine the following chain: 4129ec9f10feSMax Reitz * 4130ec9f10feSMax Reitz * guest device -> node A -> further backing chain... 4131ec9f10feSMax Reitz * 4132ec9f10feSMax Reitz * Now we create a new BDS B which we want to put on top of this 4133ec9f10feSMax Reitz * chain, so we first attach A as its backing node: 4134ec9f10feSMax Reitz * 4135ec9f10feSMax Reitz * node B 4136ec9f10feSMax Reitz * | 4137ec9f10feSMax Reitz * v 4138ec9f10feSMax Reitz * guest device -> node A -> further backing chain... 4139ec9f10feSMax Reitz * 4140ec9f10feSMax Reitz * Finally we want to replace A by B. When doing that, we want to 4141ec9f10feSMax Reitz * replace all pointers to A by pointers to B -- except for the 4142ec9f10feSMax Reitz * pointer from B because (1) that would create a loop, and (2) 4143ec9f10feSMax Reitz * that pointer should simply stay intact: 4144ec9f10feSMax Reitz * 4145ec9f10feSMax Reitz * guest device -> node B 4146ec9f10feSMax Reitz * | 4147ec9f10feSMax Reitz * v 4148ec9f10feSMax Reitz * node A -> further backing chain... 4149ec9f10feSMax Reitz * 4150ec9f10feSMax Reitz * In general, when replacing a node A (c->bs) by a node B (@to), 4151ec9f10feSMax Reitz * if A is a child of B, that means we cannot replace A by B there 4152ec9f10feSMax Reitz * because that would create a loop. Silently detaching A from B 4153ec9f10feSMax Reitz * is also not really an option. So overall just leaving A in 41542f30b7c3SVladimir Sementsov-Ogievskiy * place there is the most sensible choice. 41552f30b7c3SVladimir Sementsov-Ogievskiy * 41562f30b7c3SVladimir Sementsov-Ogievskiy * We would also create a loop in any cases where @c is only 41572f30b7c3SVladimir Sementsov-Ogievskiy * indirectly referenced by @to. Prevent this by returning false 41582f30b7c3SVladimir Sementsov-Ogievskiy * if @c is found (by breadth-first search) anywhere in the whole 41592f30b7c3SVladimir Sementsov-Ogievskiy * subtree of @to. 41602f30b7c3SVladimir Sementsov-Ogievskiy */ 41612f30b7c3SVladimir Sementsov-Ogievskiy 41622f30b7c3SVladimir Sementsov-Ogievskiy ret = true; 41632f30b7c3SVladimir Sementsov-Ogievskiy found = g_hash_table_new(NULL, NULL); 41642f30b7c3SVladimir Sementsov-Ogievskiy g_hash_table_add(found, to); 41652f30b7c3SVladimir Sementsov-Ogievskiy queue = g_queue_new(); 41662f30b7c3SVladimir Sementsov-Ogievskiy g_queue_push_tail(queue, to); 41672f30b7c3SVladimir Sementsov-Ogievskiy 41682f30b7c3SVladimir Sementsov-Ogievskiy while (!g_queue_is_empty(queue)) { 41692f30b7c3SVladimir Sementsov-Ogievskiy BlockDriverState *v = g_queue_pop_head(queue); 41702f30b7c3SVladimir Sementsov-Ogievskiy BdrvChild *c2; 41712f30b7c3SVladimir Sementsov-Ogievskiy 41722f30b7c3SVladimir Sementsov-Ogievskiy QLIST_FOREACH(c2, &v->children, next) { 41732f30b7c3SVladimir Sementsov-Ogievskiy if (c2 == c) { 41742f30b7c3SVladimir Sementsov-Ogievskiy ret = false; 41752f30b7c3SVladimir Sementsov-Ogievskiy break; 41762f30b7c3SVladimir Sementsov-Ogievskiy } 41772f30b7c3SVladimir Sementsov-Ogievskiy 41782f30b7c3SVladimir Sementsov-Ogievskiy if (g_hash_table_contains(found, c2->bs)) { 41792f30b7c3SVladimir Sementsov-Ogievskiy continue; 41802f30b7c3SVladimir Sementsov-Ogievskiy } 41812f30b7c3SVladimir Sementsov-Ogievskiy 41822f30b7c3SVladimir Sementsov-Ogievskiy g_queue_push_tail(queue, c2->bs); 41832f30b7c3SVladimir Sementsov-Ogievskiy g_hash_table_add(found, c2->bs); 41849bd910e2SMax Reitz } 41859bd910e2SMax Reitz } 41869bd910e2SMax Reitz 41872f30b7c3SVladimir Sementsov-Ogievskiy g_queue_free(queue); 41882f30b7c3SVladimir Sementsov-Ogievskiy g_hash_table_destroy(found); 41892f30b7c3SVladimir Sementsov-Ogievskiy 41902f30b7c3SVladimir Sementsov-Ogievskiy return ret; 4191d0ac0380SKevin Wolf } 4192d0ac0380SKevin Wolf 41935fe31c25SKevin Wolf void bdrv_replace_node(BlockDriverState *from, BlockDriverState *to, 41945fe31c25SKevin Wolf Error **errp) 4195d0ac0380SKevin Wolf { 4196d0ac0380SKevin Wolf BdrvChild *c, *next; 4197234ac1a9SKevin Wolf GSList *list = NULL, *p; 4198234ac1a9SKevin Wolf uint64_t perm = 0, shared = BLK_PERM_ALL; 4199234ac1a9SKevin Wolf int ret; 4200d0ac0380SKevin Wolf 4201234ac1a9SKevin Wolf /* Make sure that @from doesn't go away until we have successfully attached 4202234ac1a9SKevin Wolf * all of its parents to @to. */ 4203234ac1a9SKevin Wolf bdrv_ref(from); 4204234ac1a9SKevin Wolf 4205f871abd6SKevin Wolf assert(qemu_get_current_aio_context() == qemu_get_aio_context()); 4206f871abd6SKevin Wolf bdrv_drained_begin(from); 4207f871abd6SKevin Wolf 4208234ac1a9SKevin Wolf /* Put all parents into @list and calculate their cumulative permissions */ 4209d0ac0380SKevin Wolf QLIST_FOREACH_SAFE(c, &from->parents, next_parent, next) { 4210ec9f10feSMax Reitz assert(c->bs == from); 4211d0ac0380SKevin Wolf if (!should_update_child(c, to)) { 4212d0ac0380SKevin Wolf continue; 4213d0ac0380SKevin Wolf } 42142cad1ebeSAlberto Garcia if (c->frozen) { 42152cad1ebeSAlberto Garcia error_setg(errp, "Cannot change '%s' link to '%s'", 42162cad1ebeSAlberto Garcia c->name, from->node_name); 42172cad1ebeSAlberto Garcia goto out; 42182cad1ebeSAlberto Garcia } 4219234ac1a9SKevin Wolf list = g_slist_prepend(list, c); 4220234ac1a9SKevin Wolf perm |= c->perm; 4221234ac1a9SKevin Wolf shared &= c->shared_perm; 4222234ac1a9SKevin Wolf } 4223234ac1a9SKevin Wolf 4224234ac1a9SKevin Wolf /* Check whether the required permissions can be granted on @to, ignoring 4225234ac1a9SKevin Wolf * all BdrvChild in @list so that they can't block themselves. */ 42269eab1544SMax Reitz ret = bdrv_check_update_perm(to, NULL, perm, shared, list, NULL, errp); 4227234ac1a9SKevin Wolf if (ret < 0) { 4228234ac1a9SKevin Wolf bdrv_abort_perm_update(to); 4229234ac1a9SKevin Wolf goto out; 4230234ac1a9SKevin Wolf } 4231234ac1a9SKevin Wolf 4232234ac1a9SKevin Wolf /* Now actually perform the change. We performed the permission check for 4233234ac1a9SKevin Wolf * all elements of @list at once, so set the permissions all at once at the 4234234ac1a9SKevin Wolf * very end. */ 4235234ac1a9SKevin Wolf for (p = list; p != NULL; p = p->next) { 4236234ac1a9SKevin Wolf c = p->data; 4237d0ac0380SKevin Wolf 4238dd62f1caSKevin Wolf bdrv_ref(to); 4239234ac1a9SKevin Wolf bdrv_replace_child_noperm(c, to); 4240dd62f1caSKevin Wolf bdrv_unref(from); 4241dd62f1caSKevin Wolf } 4242234ac1a9SKevin Wolf 4243b503de61SVladimir Sementsov-Ogievskiy bdrv_get_cumulative_perm(to, &perm, &shared); 4244b503de61SVladimir Sementsov-Ogievskiy bdrv_set_perm(to, perm, shared); 4245234ac1a9SKevin Wolf 4246234ac1a9SKevin Wolf out: 4247234ac1a9SKevin Wolf g_slist_free(list); 4248f871abd6SKevin Wolf bdrv_drained_end(from); 4249234ac1a9SKevin Wolf bdrv_unref(from); 4250dd62f1caSKevin Wolf } 4251dd62f1caSKevin Wolf 42528802d1fdSJeff Cody /* 42538802d1fdSJeff Cody * Add new bs contents at the top of an image chain while the chain is 42548802d1fdSJeff Cody * live, while keeping required fields on the top layer. 42558802d1fdSJeff Cody * 42568802d1fdSJeff Cody * This will modify the BlockDriverState fields, and swap contents 42578802d1fdSJeff Cody * between bs_new and bs_top. Both bs_new and bs_top are modified. 42588802d1fdSJeff Cody * 4259bfb197e0SMarkus Armbruster * bs_new must not be attached to a BlockBackend. 4260f6801b83SJeff Cody * 42618802d1fdSJeff Cody * This function does not create any image files. 4262dd62f1caSKevin Wolf * 4263dd62f1caSKevin Wolf * bdrv_append() takes ownership of a bs_new reference and unrefs it because 4264dd62f1caSKevin Wolf * that's what the callers commonly need. bs_new will be referenced by the old 4265dd62f1caSKevin Wolf * parents of bs_top after bdrv_append() returns. If the caller needs to keep a 4266dd62f1caSKevin Wolf * reference of its own, it must call bdrv_ref(). 42678802d1fdSJeff Cody */ 4268b2c2832cSKevin Wolf void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top, 4269b2c2832cSKevin Wolf Error **errp) 42708802d1fdSJeff Cody { 4271b2c2832cSKevin Wolf Error *local_err = NULL; 4272b2c2832cSKevin Wolf 4273b2c2832cSKevin Wolf bdrv_set_backing_hd(bs_new, bs_top, &local_err); 4274b2c2832cSKevin Wolf if (local_err) { 4275b2c2832cSKevin Wolf error_propagate(errp, local_err); 4276b2c2832cSKevin Wolf goto out; 4277b2c2832cSKevin Wolf } 4278dd62f1caSKevin Wolf 42795fe31c25SKevin Wolf bdrv_replace_node(bs_top, bs_new, &local_err); 4280234ac1a9SKevin Wolf if (local_err) { 4281234ac1a9SKevin Wolf error_propagate(errp, local_err); 4282234ac1a9SKevin Wolf bdrv_set_backing_hd(bs_new, NULL, &error_abort); 4283234ac1a9SKevin Wolf goto out; 4284234ac1a9SKevin Wolf } 4285dd62f1caSKevin Wolf 4286dd62f1caSKevin Wolf /* bs_new is now referenced by its new parents, we don't need the 4287dd62f1caSKevin Wolf * additional reference any more. */ 4288b2c2832cSKevin Wolf out: 4289dd62f1caSKevin Wolf bdrv_unref(bs_new); 42908802d1fdSJeff Cody } 42918802d1fdSJeff Cody 42924f6fd349SFam Zheng static void bdrv_delete(BlockDriverState *bs) 4293b338082bSbellard { 42943718d8abSFam Zheng assert(bdrv_op_blocker_is_empty(bs)); 42954f6fd349SFam Zheng assert(!bs->refcnt); 429618846deeSMarkus Armbruster 42971b7bdbc1SStefan Hajnoczi /* remove from list, if necessary */ 429863eaaae0SKevin Wolf if (bs->node_name[0] != '\0') { 429963eaaae0SKevin Wolf QTAILQ_REMOVE(&graph_bdrv_states, bs, node_list); 430063eaaae0SKevin Wolf } 43012c1d04e0SMax Reitz QTAILQ_REMOVE(&all_bdrv_states, bs, bs_list); 43022c1d04e0SMax Reitz 430330c321f9SAnton Kuchin bdrv_close(bs); 430430c321f9SAnton Kuchin 43057267c094SAnthony Liguori g_free(bs); 4306fc01f7e7Sbellard } 4307fc01f7e7Sbellard 4308e97fc193Saliguori /* 4309e97fc193Saliguori * Run consistency checks on an image 4310e97fc193Saliguori * 4311e076f338SKevin Wolf * Returns 0 if the check could be completed (it doesn't mean that the image is 4312a1c7273bSStefan Weil * free of errors) or -errno when an internal error occurred. The results of the 4313e076f338SKevin Wolf * check are stored in res. 4314e97fc193Saliguori */ 43152fd61638SPaolo Bonzini static int coroutine_fn bdrv_co_check(BlockDriverState *bs, 43162fd61638SPaolo Bonzini BdrvCheckResult *res, BdrvCheckMode fix) 4317e97fc193Saliguori { 4318908bcd54SMax Reitz if (bs->drv == NULL) { 4319908bcd54SMax Reitz return -ENOMEDIUM; 4320908bcd54SMax Reitz } 43212fd61638SPaolo Bonzini if (bs->drv->bdrv_co_check == NULL) { 4322e97fc193Saliguori return -ENOTSUP; 4323e97fc193Saliguori } 4324e97fc193Saliguori 4325e076f338SKevin Wolf memset(res, 0, sizeof(*res)); 43262fd61638SPaolo Bonzini return bs->drv->bdrv_co_check(bs, res, fix); 43272fd61638SPaolo Bonzini } 43282fd61638SPaolo Bonzini 43292fd61638SPaolo Bonzini typedef struct CheckCo { 43302fd61638SPaolo Bonzini BlockDriverState *bs; 43312fd61638SPaolo Bonzini BdrvCheckResult *res; 43322fd61638SPaolo Bonzini BdrvCheckMode fix; 43332fd61638SPaolo Bonzini int ret; 43342fd61638SPaolo Bonzini } CheckCo; 43352fd61638SPaolo Bonzini 433666a5bdf3SNikita Alekseev static void coroutine_fn bdrv_check_co_entry(void *opaque) 43372fd61638SPaolo Bonzini { 43382fd61638SPaolo Bonzini CheckCo *cco = opaque; 43392fd61638SPaolo Bonzini cco->ret = bdrv_co_check(cco->bs, cco->res, cco->fix); 43404720cbeeSKevin Wolf aio_wait_kick(); 43412fd61638SPaolo Bonzini } 43422fd61638SPaolo Bonzini 43432fd61638SPaolo Bonzini int bdrv_check(BlockDriverState *bs, 43442fd61638SPaolo Bonzini BdrvCheckResult *res, BdrvCheckMode fix) 43452fd61638SPaolo Bonzini { 43462fd61638SPaolo Bonzini Coroutine *co; 43472fd61638SPaolo Bonzini CheckCo cco = { 43482fd61638SPaolo Bonzini .bs = bs, 43492fd61638SPaolo Bonzini .res = res, 43502fd61638SPaolo Bonzini .ret = -EINPROGRESS, 43512fd61638SPaolo Bonzini .fix = fix, 43522fd61638SPaolo Bonzini }; 43532fd61638SPaolo Bonzini 43542fd61638SPaolo Bonzini if (qemu_in_coroutine()) { 43552fd61638SPaolo Bonzini /* Fast-path if already in coroutine context */ 43562fd61638SPaolo Bonzini bdrv_check_co_entry(&cco); 43572fd61638SPaolo Bonzini } else { 43582fd61638SPaolo Bonzini co = qemu_coroutine_create(bdrv_check_co_entry, &cco); 43594720cbeeSKevin Wolf bdrv_coroutine_enter(bs, co); 43602fd61638SPaolo Bonzini BDRV_POLL_WHILE(bs, cco.ret == -EINPROGRESS); 43612fd61638SPaolo Bonzini } 43622fd61638SPaolo Bonzini 43632fd61638SPaolo Bonzini return cco.ret; 4364e97fc193Saliguori } 4365e97fc193Saliguori 4366756e6736SKevin Wolf /* 4367756e6736SKevin Wolf * Return values: 4368756e6736SKevin Wolf * 0 - success 4369756e6736SKevin Wolf * -EINVAL - backing format specified, but no file 4370756e6736SKevin Wolf * -ENOSPC - can't update the backing file because no space is left in the 4371756e6736SKevin Wolf * image file header 4372756e6736SKevin Wolf * -ENOTSUP - format driver doesn't support changing the backing file 4373756e6736SKevin Wolf */ 4374756e6736SKevin Wolf int bdrv_change_backing_file(BlockDriverState *bs, 4375756e6736SKevin Wolf const char *backing_file, const char *backing_fmt) 4376756e6736SKevin Wolf { 4377756e6736SKevin Wolf BlockDriver *drv = bs->drv; 4378469ef350SPaolo Bonzini int ret; 4379756e6736SKevin Wolf 4380d470ad42SMax Reitz if (!drv) { 4381d470ad42SMax Reitz return -ENOMEDIUM; 4382d470ad42SMax Reitz } 4383d470ad42SMax Reitz 43845f377794SPaolo Bonzini /* Backing file format doesn't make sense without a backing file */ 43855f377794SPaolo Bonzini if (backing_fmt && !backing_file) { 43865f377794SPaolo Bonzini return -EINVAL; 43875f377794SPaolo Bonzini } 43885f377794SPaolo Bonzini 4389756e6736SKevin Wolf if (drv->bdrv_change_backing_file != NULL) { 4390469ef350SPaolo Bonzini ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt); 4391756e6736SKevin Wolf } else { 4392469ef350SPaolo Bonzini ret = -ENOTSUP; 4393756e6736SKevin Wolf } 4394469ef350SPaolo Bonzini 4395469ef350SPaolo Bonzini if (ret == 0) { 4396469ef350SPaolo Bonzini pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: ""); 4397469ef350SPaolo Bonzini pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: ""); 4398998c2019SMax Reitz pstrcpy(bs->auto_backing_file, sizeof(bs->auto_backing_file), 4399998c2019SMax Reitz backing_file ?: ""); 4400469ef350SPaolo Bonzini } 4401469ef350SPaolo Bonzini return ret; 4402756e6736SKevin Wolf } 4403756e6736SKevin Wolf 44046ebdcee2SJeff Cody /* 44056ebdcee2SJeff Cody * Finds the image layer in the chain that has 'bs' as its backing file. 44066ebdcee2SJeff Cody * 44076ebdcee2SJeff Cody * active is the current topmost image. 44086ebdcee2SJeff Cody * 44096ebdcee2SJeff Cody * Returns NULL if bs is not found in active's image chain, 44106ebdcee2SJeff Cody * or if active == bs. 44114caf0fcdSJeff Cody * 44124caf0fcdSJeff Cody * Returns the bottommost base image if bs == NULL. 44136ebdcee2SJeff Cody */ 44146ebdcee2SJeff Cody BlockDriverState *bdrv_find_overlay(BlockDriverState *active, 44156ebdcee2SJeff Cody BlockDriverState *bs) 44166ebdcee2SJeff Cody { 4417760e0063SKevin Wolf while (active && bs != backing_bs(active)) { 4418760e0063SKevin Wolf active = backing_bs(active); 44196ebdcee2SJeff Cody } 44206ebdcee2SJeff Cody 44214caf0fcdSJeff Cody return active; 44226ebdcee2SJeff Cody } 44236ebdcee2SJeff Cody 44244caf0fcdSJeff Cody /* Given a BDS, searches for the base layer. */ 44254caf0fcdSJeff Cody BlockDriverState *bdrv_find_base(BlockDriverState *bs) 44264caf0fcdSJeff Cody { 44274caf0fcdSJeff Cody return bdrv_find_overlay(bs, NULL); 44286ebdcee2SJeff Cody } 44296ebdcee2SJeff Cody 44306ebdcee2SJeff Cody /* 44312cad1ebeSAlberto Garcia * Return true if at least one of the backing links between @bs and 44322cad1ebeSAlberto Garcia * @base is frozen. @errp is set if that's the case. 44330f0998f6SAlberto Garcia * @base must be reachable from @bs, or NULL. 44342cad1ebeSAlberto Garcia */ 44352cad1ebeSAlberto Garcia bool bdrv_is_backing_chain_frozen(BlockDriverState *bs, BlockDriverState *base, 44362cad1ebeSAlberto Garcia Error **errp) 44372cad1ebeSAlberto Garcia { 44382cad1ebeSAlberto Garcia BlockDriverState *i; 44392cad1ebeSAlberto Garcia 44400f0998f6SAlberto Garcia for (i = bs; i != base; i = backing_bs(i)) { 44410f0998f6SAlberto Garcia if (i->backing && i->backing->frozen) { 44422cad1ebeSAlberto Garcia error_setg(errp, "Cannot change '%s' link from '%s' to '%s'", 44432cad1ebeSAlberto Garcia i->backing->name, i->node_name, 44442cad1ebeSAlberto Garcia backing_bs(i)->node_name); 44452cad1ebeSAlberto Garcia return true; 44462cad1ebeSAlberto Garcia } 44472cad1ebeSAlberto Garcia } 44482cad1ebeSAlberto Garcia 44492cad1ebeSAlberto Garcia return false; 44502cad1ebeSAlberto Garcia } 44512cad1ebeSAlberto Garcia 44522cad1ebeSAlberto Garcia /* 44532cad1ebeSAlberto Garcia * Freeze all backing links between @bs and @base. 44542cad1ebeSAlberto Garcia * If any of the links is already frozen the operation is aborted and 44552cad1ebeSAlberto Garcia * none of the links are modified. 44560f0998f6SAlberto Garcia * @base must be reachable from @bs, or NULL. 44572cad1ebeSAlberto Garcia * Returns 0 on success. On failure returns < 0 and sets @errp. 44582cad1ebeSAlberto Garcia */ 44592cad1ebeSAlberto Garcia int bdrv_freeze_backing_chain(BlockDriverState *bs, BlockDriverState *base, 44602cad1ebeSAlberto Garcia Error **errp) 44612cad1ebeSAlberto Garcia { 44622cad1ebeSAlberto Garcia BlockDriverState *i; 44632cad1ebeSAlberto Garcia 44642cad1ebeSAlberto Garcia if (bdrv_is_backing_chain_frozen(bs, base, errp)) { 44652cad1ebeSAlberto Garcia return -EPERM; 44662cad1ebeSAlberto Garcia } 44672cad1ebeSAlberto Garcia 44680f0998f6SAlberto Garcia for (i = bs; i != base; i = backing_bs(i)) { 4469e5182c1cSMax Reitz if (i->backing && backing_bs(i)->never_freeze) { 4470e5182c1cSMax Reitz error_setg(errp, "Cannot freeze '%s' link to '%s'", 4471e5182c1cSMax Reitz i->backing->name, backing_bs(i)->node_name); 4472e5182c1cSMax Reitz return -EPERM; 4473e5182c1cSMax Reitz } 4474e5182c1cSMax Reitz } 4475e5182c1cSMax Reitz 4476e5182c1cSMax Reitz for (i = bs; i != base; i = backing_bs(i)) { 44770f0998f6SAlberto Garcia if (i->backing) { 44782cad1ebeSAlberto Garcia i->backing->frozen = true; 44792cad1ebeSAlberto Garcia } 44800f0998f6SAlberto Garcia } 44812cad1ebeSAlberto Garcia 44822cad1ebeSAlberto Garcia return 0; 44832cad1ebeSAlberto Garcia } 44842cad1ebeSAlberto Garcia 44852cad1ebeSAlberto Garcia /* 44862cad1ebeSAlberto Garcia * Unfreeze all backing links between @bs and @base. The caller must 44872cad1ebeSAlberto Garcia * ensure that all links are frozen before using this function. 44880f0998f6SAlberto Garcia * @base must be reachable from @bs, or NULL. 44892cad1ebeSAlberto Garcia */ 44902cad1ebeSAlberto Garcia void bdrv_unfreeze_backing_chain(BlockDriverState *bs, BlockDriverState *base) 44912cad1ebeSAlberto Garcia { 44922cad1ebeSAlberto Garcia BlockDriverState *i; 44932cad1ebeSAlberto Garcia 44940f0998f6SAlberto Garcia for (i = bs; i != base; i = backing_bs(i)) { 44950f0998f6SAlberto Garcia if (i->backing) { 44962cad1ebeSAlberto Garcia assert(i->backing->frozen); 44972cad1ebeSAlberto Garcia i->backing->frozen = false; 44982cad1ebeSAlberto Garcia } 44992cad1ebeSAlberto Garcia } 45000f0998f6SAlberto Garcia } 45012cad1ebeSAlberto Garcia 45022cad1ebeSAlberto Garcia /* 45036ebdcee2SJeff Cody * Drops images above 'base' up to and including 'top', and sets the image 45046ebdcee2SJeff Cody * above 'top' to have base as its backing file. 45056ebdcee2SJeff Cody * 45066ebdcee2SJeff Cody * Requires that the overlay to 'top' is opened r/w, so that the backing file 45076ebdcee2SJeff Cody * information in 'bs' can be properly updated. 45086ebdcee2SJeff Cody * 45096ebdcee2SJeff Cody * E.g., this will convert the following chain: 45106ebdcee2SJeff Cody * bottom <- base <- intermediate <- top <- active 45116ebdcee2SJeff Cody * 45126ebdcee2SJeff Cody * to 45136ebdcee2SJeff Cody * 45146ebdcee2SJeff Cody * bottom <- base <- active 45156ebdcee2SJeff Cody * 45166ebdcee2SJeff Cody * It is allowed for bottom==base, in which case it converts: 45176ebdcee2SJeff Cody * 45186ebdcee2SJeff Cody * base <- intermediate <- top <- active 45196ebdcee2SJeff Cody * 45206ebdcee2SJeff Cody * to 45216ebdcee2SJeff Cody * 45226ebdcee2SJeff Cody * base <- active 45236ebdcee2SJeff Cody * 452454e26900SJeff Cody * If backing_file_str is non-NULL, it will be used when modifying top's 452554e26900SJeff Cody * overlay image metadata. 452654e26900SJeff Cody * 45276ebdcee2SJeff Cody * Error conditions: 45286ebdcee2SJeff Cody * if active == top, that is considered an error 45296ebdcee2SJeff Cody * 45306ebdcee2SJeff Cody */ 4531bde70715SKevin Wolf int bdrv_drop_intermediate(BlockDriverState *top, BlockDriverState *base, 4532bde70715SKevin Wolf const char *backing_file_str) 45336ebdcee2SJeff Cody { 45346bd858b3SAlberto Garcia BlockDriverState *explicit_top = top; 45356bd858b3SAlberto Garcia bool update_inherits_from; 453661f09ceaSKevin Wolf BdrvChild *c, *next; 453712fa4af6SKevin Wolf Error *local_err = NULL; 45386ebdcee2SJeff Cody int ret = -EIO; 45396ebdcee2SJeff Cody 45406858eba0SKevin Wolf bdrv_ref(top); 4541637d54a5SMax Reitz bdrv_subtree_drained_begin(top); 45426858eba0SKevin Wolf 45436ebdcee2SJeff Cody if (!top->drv || !base->drv) { 45446ebdcee2SJeff Cody goto exit; 45456ebdcee2SJeff Cody } 45466ebdcee2SJeff Cody 45475db15a57SKevin Wolf /* Make sure that base is in the backing chain of top */ 45485db15a57SKevin Wolf if (!bdrv_chain_contains(top, base)) { 45496ebdcee2SJeff Cody goto exit; 45506ebdcee2SJeff Cody } 45516ebdcee2SJeff Cody 45522cad1ebeSAlberto Garcia /* This function changes all links that point to top and makes 45532cad1ebeSAlberto Garcia * them point to base. Check that none of them is frozen. */ 45542cad1ebeSAlberto Garcia QLIST_FOREACH(c, &top->parents, next_parent) { 45552cad1ebeSAlberto Garcia if (c->frozen) { 45562cad1ebeSAlberto Garcia goto exit; 45572cad1ebeSAlberto Garcia } 45582cad1ebeSAlberto Garcia } 45592cad1ebeSAlberto Garcia 45606bd858b3SAlberto Garcia /* If 'base' recursively inherits from 'top' then we should set 45616bd858b3SAlberto Garcia * base->inherits_from to top->inherits_from after 'top' and all 45626bd858b3SAlberto Garcia * other intermediate nodes have been dropped. 45636bd858b3SAlberto Garcia * If 'top' is an implicit node (e.g. "commit_top") we should skip 45646bd858b3SAlberto Garcia * it because no one inherits from it. We use explicit_top for that. */ 45656bd858b3SAlberto Garcia while (explicit_top && explicit_top->implicit) { 45666bd858b3SAlberto Garcia explicit_top = backing_bs(explicit_top); 45676bd858b3SAlberto Garcia } 45686bd858b3SAlberto Garcia update_inherits_from = bdrv_inherits_from_recursive(base, explicit_top); 45696bd858b3SAlberto Garcia 45706ebdcee2SJeff Cody /* success - we can delete the intermediate states, and link top->base */ 4571bde70715SKevin Wolf /* TODO Check graph modification op blockers (BLK_PERM_GRAPH_MOD) once 4572bde70715SKevin Wolf * we've figured out how they should work. */ 4573f30c66baSMax Reitz if (!backing_file_str) { 4574f30c66baSMax Reitz bdrv_refresh_filename(base); 4575f30c66baSMax Reitz backing_file_str = base->filename; 4576f30c66baSMax Reitz } 457712fa4af6SKevin Wolf 457861f09ceaSKevin Wolf QLIST_FOREACH_SAFE(c, &top->parents, next_parent, next) { 457961f09ceaSKevin Wolf /* Check whether we are allowed to switch c from top to base */ 458061f09ceaSKevin Wolf GSList *ignore_children = g_slist_prepend(NULL, c); 45812345bde6SKevin Wolf ret = bdrv_check_update_perm(base, NULL, c->perm, c->shared_perm, 45829eab1544SMax Reitz ignore_children, NULL, &local_err); 45832c860e79SFam Zheng g_slist_free(ignore_children); 45842345bde6SKevin Wolf if (ret < 0) { 458512fa4af6SKevin Wolf error_report_err(local_err); 458612fa4af6SKevin Wolf goto exit; 458712fa4af6SKevin Wolf } 458861f09ceaSKevin Wolf 458961f09ceaSKevin Wolf /* If so, update the backing file path in the image file */ 459061f09ceaSKevin Wolf if (c->role->update_filename) { 459161f09ceaSKevin Wolf ret = c->role->update_filename(c, base, backing_file_str, 459261f09ceaSKevin Wolf &local_err); 459361f09ceaSKevin Wolf if (ret < 0) { 459461f09ceaSKevin Wolf bdrv_abort_perm_update(base); 459561f09ceaSKevin Wolf error_report_err(local_err); 459661f09ceaSKevin Wolf goto exit; 459761f09ceaSKevin Wolf } 459861f09ceaSKevin Wolf } 459961f09ceaSKevin Wolf 460061f09ceaSKevin Wolf /* Do the actual switch in the in-memory graph. 460161f09ceaSKevin Wolf * Completes bdrv_check_update_perm() transaction internally. */ 460261f09ceaSKevin Wolf bdrv_ref(base); 460361f09ceaSKevin Wolf bdrv_replace_child(c, base); 460461f09ceaSKevin Wolf bdrv_unref(top); 460561f09ceaSKevin Wolf } 46066ebdcee2SJeff Cody 46076bd858b3SAlberto Garcia if (update_inherits_from) { 46086bd858b3SAlberto Garcia base->inherits_from = explicit_top->inherits_from; 46096bd858b3SAlberto Garcia } 46106bd858b3SAlberto Garcia 46116ebdcee2SJeff Cody ret = 0; 46126ebdcee2SJeff Cody exit: 4613637d54a5SMax Reitz bdrv_subtree_drained_end(top); 46146858eba0SKevin Wolf bdrv_unref(top); 46156ebdcee2SJeff Cody return ret; 46166ebdcee2SJeff Cody } 46176ebdcee2SJeff Cody 461883f64091Sbellard /** 46194a1d5e1fSFam Zheng * Length of a allocated file in bytes. Sparse files are counted by actual 46204a1d5e1fSFam Zheng * allocated space. Return < 0 if error or unknown. 46214a1d5e1fSFam Zheng */ 46224a1d5e1fSFam Zheng int64_t bdrv_get_allocated_file_size(BlockDriverState *bs) 46234a1d5e1fSFam Zheng { 46244a1d5e1fSFam Zheng BlockDriver *drv = bs->drv; 46254a1d5e1fSFam Zheng if (!drv) { 46264a1d5e1fSFam Zheng return -ENOMEDIUM; 46274a1d5e1fSFam Zheng } 46284a1d5e1fSFam Zheng if (drv->bdrv_get_allocated_file_size) { 46294a1d5e1fSFam Zheng return drv->bdrv_get_allocated_file_size(bs); 46304a1d5e1fSFam Zheng } 46314a1d5e1fSFam Zheng if (bs->file) { 46329a4f4c31SKevin Wolf return bdrv_get_allocated_file_size(bs->file->bs); 46334a1d5e1fSFam Zheng } 46344a1d5e1fSFam Zheng return -ENOTSUP; 46354a1d5e1fSFam Zheng } 46364a1d5e1fSFam Zheng 463790880ff1SStefan Hajnoczi /* 463890880ff1SStefan Hajnoczi * bdrv_measure: 463990880ff1SStefan Hajnoczi * @drv: Format driver 464090880ff1SStefan Hajnoczi * @opts: Creation options for new image 464190880ff1SStefan Hajnoczi * @in_bs: Existing image containing data for new image (may be NULL) 464290880ff1SStefan Hajnoczi * @errp: Error object 464390880ff1SStefan Hajnoczi * Returns: A #BlockMeasureInfo (free using qapi_free_BlockMeasureInfo()) 464490880ff1SStefan Hajnoczi * or NULL on error 464590880ff1SStefan Hajnoczi * 464690880ff1SStefan Hajnoczi * Calculate file size required to create a new image. 464790880ff1SStefan Hajnoczi * 464890880ff1SStefan Hajnoczi * If @in_bs is given then space for allocated clusters and zero clusters 464990880ff1SStefan Hajnoczi * from that image are included in the calculation. If @opts contains a 465090880ff1SStefan Hajnoczi * backing file that is shared by @in_bs then backing clusters may be omitted 465190880ff1SStefan Hajnoczi * from the calculation. 465290880ff1SStefan Hajnoczi * 465390880ff1SStefan Hajnoczi * If @in_bs is NULL then the calculation includes no allocated clusters 465490880ff1SStefan Hajnoczi * unless a preallocation option is given in @opts. 465590880ff1SStefan Hajnoczi * 465690880ff1SStefan Hajnoczi * Note that @in_bs may use a different BlockDriver from @drv. 465790880ff1SStefan Hajnoczi * 465890880ff1SStefan Hajnoczi * If an error occurs the @errp pointer is set. 465990880ff1SStefan Hajnoczi */ 466090880ff1SStefan Hajnoczi BlockMeasureInfo *bdrv_measure(BlockDriver *drv, QemuOpts *opts, 466190880ff1SStefan Hajnoczi BlockDriverState *in_bs, Error **errp) 466290880ff1SStefan Hajnoczi { 466390880ff1SStefan Hajnoczi if (!drv->bdrv_measure) { 466490880ff1SStefan Hajnoczi error_setg(errp, "Block driver '%s' does not support size measurement", 466590880ff1SStefan Hajnoczi drv->format_name); 466690880ff1SStefan Hajnoczi return NULL; 466790880ff1SStefan Hajnoczi } 466890880ff1SStefan Hajnoczi 466990880ff1SStefan Hajnoczi return drv->bdrv_measure(opts, in_bs, errp); 467090880ff1SStefan Hajnoczi } 467190880ff1SStefan Hajnoczi 46724a1d5e1fSFam Zheng /** 467365a9bb25SMarkus Armbruster * Return number of sectors on success, -errno on error. 467483f64091Sbellard */ 467565a9bb25SMarkus Armbruster int64_t bdrv_nb_sectors(BlockDriverState *bs) 467683f64091Sbellard { 467783f64091Sbellard BlockDriver *drv = bs->drv; 467865a9bb25SMarkus Armbruster 467983f64091Sbellard if (!drv) 468019cb3738Sbellard return -ENOMEDIUM; 468151762288SStefan Hajnoczi 4682b94a2610SKevin Wolf if (drv->has_variable_length) { 4683b94a2610SKevin Wolf int ret = refresh_total_sectors(bs, bs->total_sectors); 4684b94a2610SKevin Wolf if (ret < 0) { 4685b94a2610SKevin Wolf return ret; 4686fc01f7e7Sbellard } 468746a4e4e6SStefan Hajnoczi } 468865a9bb25SMarkus Armbruster return bs->total_sectors; 468965a9bb25SMarkus Armbruster } 469065a9bb25SMarkus Armbruster 469165a9bb25SMarkus Armbruster /** 469265a9bb25SMarkus Armbruster * Return length in bytes on success, -errno on error. 469365a9bb25SMarkus Armbruster * The length is always a multiple of BDRV_SECTOR_SIZE. 469465a9bb25SMarkus Armbruster */ 469565a9bb25SMarkus Armbruster int64_t bdrv_getlength(BlockDriverState *bs) 469665a9bb25SMarkus Armbruster { 469765a9bb25SMarkus Armbruster int64_t ret = bdrv_nb_sectors(bs); 469865a9bb25SMarkus Armbruster 46994a9c9ea0SFam Zheng ret = ret > INT64_MAX / BDRV_SECTOR_SIZE ? -EFBIG : ret; 470065a9bb25SMarkus Armbruster return ret < 0 ? ret : ret * BDRV_SECTOR_SIZE; 470146a4e4e6SStefan Hajnoczi } 4702fc01f7e7Sbellard 470319cb3738Sbellard /* return 0 as number of sectors if no device present or error */ 470496b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) 4705fc01f7e7Sbellard { 470665a9bb25SMarkus Armbruster int64_t nb_sectors = bdrv_nb_sectors(bs); 470765a9bb25SMarkus Armbruster 470865a9bb25SMarkus Armbruster *nb_sectors_ptr = nb_sectors < 0 ? 0 : nb_sectors; 4709fc01f7e7Sbellard } 4710cf98951bSbellard 471154115412SEric Blake bool bdrv_is_sg(BlockDriverState *bs) 4712985a03b0Sths { 4713985a03b0Sths return bs->sg; 4714985a03b0Sths } 4715985a03b0Sths 471654115412SEric Blake bool bdrv_is_encrypted(BlockDriverState *bs) 4717ea2384d3Sbellard { 4718760e0063SKevin Wolf if (bs->backing && bs->backing->bs->encrypted) { 471954115412SEric Blake return true; 4720760e0063SKevin Wolf } 4721ea2384d3Sbellard return bs->encrypted; 4722ea2384d3Sbellard } 4723ea2384d3Sbellard 4724f8d6bba1SMarkus Armbruster const char *bdrv_get_format_name(BlockDriverState *bs) 4725ea2384d3Sbellard { 4726f8d6bba1SMarkus Armbruster return bs->drv ? bs->drv->format_name : NULL; 4727ea2384d3Sbellard } 4728ea2384d3Sbellard 4729ada42401SStefan Hajnoczi static int qsort_strcmp(const void *a, const void *b) 4730ada42401SStefan Hajnoczi { 4731ceff5bd7SMax Reitz return strcmp(*(char *const *)a, *(char *const *)b); 4732ada42401SStefan Hajnoczi } 4733ada42401SStefan Hajnoczi 4734ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name), 47359ac404c5SAndrey Shinkevich void *opaque, bool read_only) 4736ea2384d3Sbellard { 4737ea2384d3Sbellard BlockDriver *drv; 4738e855e4fbSJeff Cody int count = 0; 4739ada42401SStefan Hajnoczi int i; 4740e855e4fbSJeff Cody const char **formats = NULL; 4741ea2384d3Sbellard 47428a22f02aSStefan Hajnoczi QLIST_FOREACH(drv, &bdrv_drivers, list) { 4743e855e4fbSJeff Cody if (drv->format_name) { 4744e855e4fbSJeff Cody bool found = false; 4745e855e4fbSJeff Cody int i = count; 47469ac404c5SAndrey Shinkevich 47479ac404c5SAndrey Shinkevich if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, read_only)) { 47489ac404c5SAndrey Shinkevich continue; 47499ac404c5SAndrey Shinkevich } 47509ac404c5SAndrey Shinkevich 4751e855e4fbSJeff Cody while (formats && i && !found) { 4752e855e4fbSJeff Cody found = !strcmp(formats[--i], drv->format_name); 4753e855e4fbSJeff Cody } 4754e855e4fbSJeff Cody 4755e855e4fbSJeff Cody if (!found) { 47565839e53bSMarkus Armbruster formats = g_renew(const char *, formats, count + 1); 4757e855e4fbSJeff Cody formats[count++] = drv->format_name; 4758ea2384d3Sbellard } 4759ea2384d3Sbellard } 4760e855e4fbSJeff Cody } 4761ada42401SStefan Hajnoczi 4762eb0df69fSMax Reitz for (i = 0; i < (int)ARRAY_SIZE(block_driver_modules); i++) { 4763eb0df69fSMax Reitz const char *format_name = block_driver_modules[i].format_name; 4764eb0df69fSMax Reitz 4765eb0df69fSMax Reitz if (format_name) { 4766eb0df69fSMax Reitz bool found = false; 4767eb0df69fSMax Reitz int j = count; 4768eb0df69fSMax Reitz 47699ac404c5SAndrey Shinkevich if (use_bdrv_whitelist && 47709ac404c5SAndrey Shinkevich !bdrv_format_is_whitelisted(format_name, read_only)) { 47719ac404c5SAndrey Shinkevich continue; 47729ac404c5SAndrey Shinkevich } 47739ac404c5SAndrey Shinkevich 4774eb0df69fSMax Reitz while (formats && j && !found) { 4775eb0df69fSMax Reitz found = !strcmp(formats[--j], format_name); 4776eb0df69fSMax Reitz } 4777eb0df69fSMax Reitz 4778eb0df69fSMax Reitz if (!found) { 4779eb0df69fSMax Reitz formats = g_renew(const char *, formats, count + 1); 4780eb0df69fSMax Reitz formats[count++] = format_name; 4781eb0df69fSMax Reitz } 4782eb0df69fSMax Reitz } 4783eb0df69fSMax Reitz } 4784eb0df69fSMax Reitz 4785ada42401SStefan Hajnoczi qsort(formats, count, sizeof(formats[0]), qsort_strcmp); 4786ada42401SStefan Hajnoczi 4787ada42401SStefan Hajnoczi for (i = 0; i < count; i++) { 4788ada42401SStefan Hajnoczi it(opaque, formats[i]); 4789ada42401SStefan Hajnoczi } 4790ada42401SStefan Hajnoczi 4791e855e4fbSJeff Cody g_free(formats); 4792e855e4fbSJeff Cody } 4793ea2384d3Sbellard 4794dc364f4cSBenoît Canet /* This function is to find a node in the bs graph */ 4795dc364f4cSBenoît Canet BlockDriverState *bdrv_find_node(const char *node_name) 4796dc364f4cSBenoît Canet { 4797dc364f4cSBenoît Canet BlockDriverState *bs; 4798dc364f4cSBenoît Canet 4799dc364f4cSBenoît Canet assert(node_name); 4800dc364f4cSBenoît Canet 4801dc364f4cSBenoît Canet QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) { 4802dc364f4cSBenoît Canet if (!strcmp(node_name, bs->node_name)) { 4803dc364f4cSBenoît Canet return bs; 4804dc364f4cSBenoît Canet } 4805dc364f4cSBenoît Canet } 4806dc364f4cSBenoît Canet return NULL; 4807dc364f4cSBenoît Canet } 4808dc364f4cSBenoît Canet 4809c13163fbSBenoît Canet /* Put this QMP function here so it can access the static graph_bdrv_states. */ 4810d5a8ee60SAlberto Garcia BlockDeviceInfoList *bdrv_named_nodes_list(Error **errp) 4811c13163fbSBenoît Canet { 4812c13163fbSBenoît Canet BlockDeviceInfoList *list, *entry; 4813c13163fbSBenoît Canet BlockDriverState *bs; 4814c13163fbSBenoît Canet 4815c13163fbSBenoît Canet list = NULL; 4816c13163fbSBenoît Canet QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) { 4817c83f9fbaSKevin Wolf BlockDeviceInfo *info = bdrv_block_device_info(NULL, bs, errp); 4818d5a8ee60SAlberto Garcia if (!info) { 4819d5a8ee60SAlberto Garcia qapi_free_BlockDeviceInfoList(list); 4820d5a8ee60SAlberto Garcia return NULL; 4821d5a8ee60SAlberto Garcia } 4822c13163fbSBenoît Canet entry = g_malloc0(sizeof(*entry)); 4823d5a8ee60SAlberto Garcia entry->value = info; 4824c13163fbSBenoît Canet entry->next = list; 4825c13163fbSBenoît Canet list = entry; 4826c13163fbSBenoît Canet } 4827c13163fbSBenoît Canet 4828c13163fbSBenoît Canet return list; 4829c13163fbSBenoît Canet } 4830c13163fbSBenoît Canet 48315d3b4e99SVladimir Sementsov-Ogievskiy #define QAPI_LIST_ADD(list, element) do { \ 48325d3b4e99SVladimir Sementsov-Ogievskiy typeof(list) _tmp = g_new(typeof(*(list)), 1); \ 48335d3b4e99SVladimir Sementsov-Ogievskiy _tmp->value = (element); \ 48345d3b4e99SVladimir Sementsov-Ogievskiy _tmp->next = (list); \ 48355d3b4e99SVladimir Sementsov-Ogievskiy (list) = _tmp; \ 48365d3b4e99SVladimir Sementsov-Ogievskiy } while (0) 48375d3b4e99SVladimir Sementsov-Ogievskiy 48385d3b4e99SVladimir Sementsov-Ogievskiy typedef struct XDbgBlockGraphConstructor { 48395d3b4e99SVladimir Sementsov-Ogievskiy XDbgBlockGraph *graph; 48405d3b4e99SVladimir Sementsov-Ogievskiy GHashTable *graph_nodes; 48415d3b4e99SVladimir Sementsov-Ogievskiy } XDbgBlockGraphConstructor; 48425d3b4e99SVladimir Sementsov-Ogievskiy 48435d3b4e99SVladimir Sementsov-Ogievskiy static XDbgBlockGraphConstructor *xdbg_graph_new(void) 48445d3b4e99SVladimir Sementsov-Ogievskiy { 48455d3b4e99SVladimir Sementsov-Ogievskiy XDbgBlockGraphConstructor *gr = g_new(XDbgBlockGraphConstructor, 1); 48465d3b4e99SVladimir Sementsov-Ogievskiy 48475d3b4e99SVladimir Sementsov-Ogievskiy gr->graph = g_new0(XDbgBlockGraph, 1); 48485d3b4e99SVladimir Sementsov-Ogievskiy gr->graph_nodes = g_hash_table_new(NULL, NULL); 48495d3b4e99SVladimir Sementsov-Ogievskiy 48505d3b4e99SVladimir Sementsov-Ogievskiy return gr; 48515d3b4e99SVladimir Sementsov-Ogievskiy } 48525d3b4e99SVladimir Sementsov-Ogievskiy 48535d3b4e99SVladimir Sementsov-Ogievskiy static XDbgBlockGraph *xdbg_graph_finalize(XDbgBlockGraphConstructor *gr) 48545d3b4e99SVladimir Sementsov-Ogievskiy { 48555d3b4e99SVladimir Sementsov-Ogievskiy XDbgBlockGraph *graph = gr->graph; 48565d3b4e99SVladimir Sementsov-Ogievskiy 48575d3b4e99SVladimir Sementsov-Ogievskiy g_hash_table_destroy(gr->graph_nodes); 48585d3b4e99SVladimir Sementsov-Ogievskiy g_free(gr); 48595d3b4e99SVladimir Sementsov-Ogievskiy 48605d3b4e99SVladimir Sementsov-Ogievskiy return graph; 48615d3b4e99SVladimir Sementsov-Ogievskiy } 48625d3b4e99SVladimir Sementsov-Ogievskiy 48635d3b4e99SVladimir Sementsov-Ogievskiy static uintptr_t xdbg_graph_node_num(XDbgBlockGraphConstructor *gr, void *node) 48645d3b4e99SVladimir Sementsov-Ogievskiy { 48655d3b4e99SVladimir Sementsov-Ogievskiy uintptr_t ret = (uintptr_t)g_hash_table_lookup(gr->graph_nodes, node); 48665d3b4e99SVladimir Sementsov-Ogievskiy 48675d3b4e99SVladimir Sementsov-Ogievskiy if (ret != 0) { 48685d3b4e99SVladimir Sementsov-Ogievskiy return ret; 48695d3b4e99SVladimir Sementsov-Ogievskiy } 48705d3b4e99SVladimir Sementsov-Ogievskiy 48715d3b4e99SVladimir Sementsov-Ogievskiy /* 48725d3b4e99SVladimir Sementsov-Ogievskiy * Start counting from 1, not 0, because 0 interferes with not-found (NULL) 48735d3b4e99SVladimir Sementsov-Ogievskiy * answer of g_hash_table_lookup. 48745d3b4e99SVladimir Sementsov-Ogievskiy */ 48755d3b4e99SVladimir Sementsov-Ogievskiy ret = g_hash_table_size(gr->graph_nodes) + 1; 48765d3b4e99SVladimir Sementsov-Ogievskiy g_hash_table_insert(gr->graph_nodes, node, (void *)ret); 48775d3b4e99SVladimir Sementsov-Ogievskiy 48785d3b4e99SVladimir Sementsov-Ogievskiy return ret; 48795d3b4e99SVladimir Sementsov-Ogievskiy } 48805d3b4e99SVladimir Sementsov-Ogievskiy 48815d3b4e99SVladimir Sementsov-Ogievskiy static void xdbg_graph_add_node(XDbgBlockGraphConstructor *gr, void *node, 48825d3b4e99SVladimir Sementsov-Ogievskiy XDbgBlockGraphNodeType type, const char *name) 48835d3b4e99SVladimir Sementsov-Ogievskiy { 48845d3b4e99SVladimir Sementsov-Ogievskiy XDbgBlockGraphNode *n; 48855d3b4e99SVladimir Sementsov-Ogievskiy 48865d3b4e99SVladimir Sementsov-Ogievskiy n = g_new0(XDbgBlockGraphNode, 1); 48875d3b4e99SVladimir Sementsov-Ogievskiy 48885d3b4e99SVladimir Sementsov-Ogievskiy n->id = xdbg_graph_node_num(gr, node); 48895d3b4e99SVladimir Sementsov-Ogievskiy n->type = type; 48905d3b4e99SVladimir Sementsov-Ogievskiy n->name = g_strdup(name); 48915d3b4e99SVladimir Sementsov-Ogievskiy 48925d3b4e99SVladimir Sementsov-Ogievskiy QAPI_LIST_ADD(gr->graph->nodes, n); 48935d3b4e99SVladimir Sementsov-Ogievskiy } 48945d3b4e99SVladimir Sementsov-Ogievskiy 48955d3b4e99SVladimir Sementsov-Ogievskiy static void xdbg_graph_add_edge(XDbgBlockGraphConstructor *gr, void *parent, 48965d3b4e99SVladimir Sementsov-Ogievskiy const BdrvChild *child) 48975d3b4e99SVladimir Sementsov-Ogievskiy { 4898cdb1cec8SMax Reitz BlockPermission qapi_perm; 48995d3b4e99SVladimir Sementsov-Ogievskiy XDbgBlockGraphEdge *edge; 49005d3b4e99SVladimir Sementsov-Ogievskiy 49015d3b4e99SVladimir Sementsov-Ogievskiy edge = g_new0(XDbgBlockGraphEdge, 1); 49025d3b4e99SVladimir Sementsov-Ogievskiy 49035d3b4e99SVladimir Sementsov-Ogievskiy edge->parent = xdbg_graph_node_num(gr, parent); 49045d3b4e99SVladimir Sementsov-Ogievskiy edge->child = xdbg_graph_node_num(gr, child->bs); 49055d3b4e99SVladimir Sementsov-Ogievskiy edge->name = g_strdup(child->name); 49065d3b4e99SVladimir Sementsov-Ogievskiy 4907cdb1cec8SMax Reitz for (qapi_perm = 0; qapi_perm < BLOCK_PERMISSION__MAX; qapi_perm++) { 4908cdb1cec8SMax Reitz uint64_t flag = bdrv_qapi_perm_to_blk_perm(qapi_perm); 4909cdb1cec8SMax Reitz 4910cdb1cec8SMax Reitz if (flag & child->perm) { 4911cdb1cec8SMax Reitz QAPI_LIST_ADD(edge->perm, qapi_perm); 49125d3b4e99SVladimir Sementsov-Ogievskiy } 4913cdb1cec8SMax Reitz if (flag & child->shared_perm) { 4914cdb1cec8SMax Reitz QAPI_LIST_ADD(edge->shared_perm, qapi_perm); 49155d3b4e99SVladimir Sementsov-Ogievskiy } 49165d3b4e99SVladimir Sementsov-Ogievskiy } 49175d3b4e99SVladimir Sementsov-Ogievskiy 49185d3b4e99SVladimir Sementsov-Ogievskiy QAPI_LIST_ADD(gr->graph->edges, edge); 49195d3b4e99SVladimir Sementsov-Ogievskiy } 49205d3b4e99SVladimir Sementsov-Ogievskiy 49215d3b4e99SVladimir Sementsov-Ogievskiy 49225d3b4e99SVladimir Sementsov-Ogievskiy XDbgBlockGraph *bdrv_get_xdbg_block_graph(Error **errp) 49235d3b4e99SVladimir Sementsov-Ogievskiy { 49245d3b4e99SVladimir Sementsov-Ogievskiy BlockBackend *blk; 49255d3b4e99SVladimir Sementsov-Ogievskiy BlockJob *job; 49265d3b4e99SVladimir Sementsov-Ogievskiy BlockDriverState *bs; 49275d3b4e99SVladimir Sementsov-Ogievskiy BdrvChild *child; 49285d3b4e99SVladimir Sementsov-Ogievskiy XDbgBlockGraphConstructor *gr = xdbg_graph_new(); 49295d3b4e99SVladimir Sementsov-Ogievskiy 49305d3b4e99SVladimir Sementsov-Ogievskiy for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) { 49315d3b4e99SVladimir Sementsov-Ogievskiy char *allocated_name = NULL; 49325d3b4e99SVladimir Sementsov-Ogievskiy const char *name = blk_name(blk); 49335d3b4e99SVladimir Sementsov-Ogievskiy 49345d3b4e99SVladimir Sementsov-Ogievskiy if (!*name) { 49355d3b4e99SVladimir Sementsov-Ogievskiy name = allocated_name = blk_get_attached_dev_id(blk); 49365d3b4e99SVladimir Sementsov-Ogievskiy } 49375d3b4e99SVladimir Sementsov-Ogievskiy xdbg_graph_add_node(gr, blk, X_DBG_BLOCK_GRAPH_NODE_TYPE_BLOCK_BACKEND, 49385d3b4e99SVladimir Sementsov-Ogievskiy name); 49395d3b4e99SVladimir Sementsov-Ogievskiy g_free(allocated_name); 49405d3b4e99SVladimir Sementsov-Ogievskiy if (blk_root(blk)) { 49415d3b4e99SVladimir Sementsov-Ogievskiy xdbg_graph_add_edge(gr, blk, blk_root(blk)); 49425d3b4e99SVladimir Sementsov-Ogievskiy } 49435d3b4e99SVladimir Sementsov-Ogievskiy } 49445d3b4e99SVladimir Sementsov-Ogievskiy 49455d3b4e99SVladimir Sementsov-Ogievskiy for (job = block_job_next(NULL); job; job = block_job_next(job)) { 49465d3b4e99SVladimir Sementsov-Ogievskiy GSList *el; 49475d3b4e99SVladimir Sementsov-Ogievskiy 49485d3b4e99SVladimir Sementsov-Ogievskiy xdbg_graph_add_node(gr, job, X_DBG_BLOCK_GRAPH_NODE_TYPE_BLOCK_JOB, 49495d3b4e99SVladimir Sementsov-Ogievskiy job->job.id); 49505d3b4e99SVladimir Sementsov-Ogievskiy for (el = job->nodes; el; el = el->next) { 49515d3b4e99SVladimir Sementsov-Ogievskiy xdbg_graph_add_edge(gr, job, (BdrvChild *)el->data); 49525d3b4e99SVladimir Sementsov-Ogievskiy } 49535d3b4e99SVladimir Sementsov-Ogievskiy } 49545d3b4e99SVladimir Sementsov-Ogievskiy 49555d3b4e99SVladimir Sementsov-Ogievskiy QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) { 49565d3b4e99SVladimir Sementsov-Ogievskiy xdbg_graph_add_node(gr, bs, X_DBG_BLOCK_GRAPH_NODE_TYPE_BLOCK_DRIVER, 49575d3b4e99SVladimir Sementsov-Ogievskiy bs->node_name); 49585d3b4e99SVladimir Sementsov-Ogievskiy QLIST_FOREACH(child, &bs->children, next) { 49595d3b4e99SVladimir Sementsov-Ogievskiy xdbg_graph_add_edge(gr, bs, child); 49605d3b4e99SVladimir Sementsov-Ogievskiy } 49615d3b4e99SVladimir Sementsov-Ogievskiy } 49625d3b4e99SVladimir Sementsov-Ogievskiy 49635d3b4e99SVladimir Sementsov-Ogievskiy return xdbg_graph_finalize(gr); 49645d3b4e99SVladimir Sementsov-Ogievskiy } 49655d3b4e99SVladimir Sementsov-Ogievskiy 496612d3ba82SBenoît Canet BlockDriverState *bdrv_lookup_bs(const char *device, 496712d3ba82SBenoît Canet const char *node_name, 496812d3ba82SBenoît Canet Error **errp) 496912d3ba82SBenoît Canet { 49707f06d47eSMarkus Armbruster BlockBackend *blk; 49717f06d47eSMarkus Armbruster BlockDriverState *bs; 497212d3ba82SBenoît Canet 497312d3ba82SBenoît Canet if (device) { 49747f06d47eSMarkus Armbruster blk = blk_by_name(device); 497512d3ba82SBenoît Canet 49767f06d47eSMarkus Armbruster if (blk) { 49779f4ed6fbSAlberto Garcia bs = blk_bs(blk); 49789f4ed6fbSAlberto Garcia if (!bs) { 49795433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 49805433c24fSMax Reitz } 49815433c24fSMax Reitz 49829f4ed6fbSAlberto Garcia return bs; 498312d3ba82SBenoît Canet } 4984dd67fa50SBenoît Canet } 498512d3ba82SBenoît Canet 4986dd67fa50SBenoît Canet if (node_name) { 498712d3ba82SBenoît Canet bs = bdrv_find_node(node_name); 498812d3ba82SBenoît Canet 4989dd67fa50SBenoît Canet if (bs) { 4990dd67fa50SBenoît Canet return bs; 4991dd67fa50SBenoît Canet } 499212d3ba82SBenoît Canet } 499312d3ba82SBenoît Canet 4994dd67fa50SBenoît Canet error_setg(errp, "Cannot find device=%s nor node_name=%s", 4995dd67fa50SBenoît Canet device ? device : "", 4996dd67fa50SBenoît Canet node_name ? node_name : ""); 4997dd67fa50SBenoît Canet return NULL; 499812d3ba82SBenoît Canet } 499912d3ba82SBenoît Canet 50005a6684d2SJeff Cody /* If 'base' is in the same chain as 'top', return true. Otherwise, 50015a6684d2SJeff Cody * return false. If either argument is NULL, return false. */ 50025a6684d2SJeff Cody bool bdrv_chain_contains(BlockDriverState *top, BlockDriverState *base) 50035a6684d2SJeff Cody { 50045a6684d2SJeff Cody while (top && top != base) { 5005760e0063SKevin Wolf top = backing_bs(top); 50065a6684d2SJeff Cody } 50075a6684d2SJeff Cody 50085a6684d2SJeff Cody return top != NULL; 50095a6684d2SJeff Cody } 50105a6684d2SJeff Cody 501104df765aSFam Zheng BlockDriverState *bdrv_next_node(BlockDriverState *bs) 501204df765aSFam Zheng { 501304df765aSFam Zheng if (!bs) { 501404df765aSFam Zheng return QTAILQ_FIRST(&graph_bdrv_states); 501504df765aSFam Zheng } 501604df765aSFam Zheng return QTAILQ_NEXT(bs, node_list); 501704df765aSFam Zheng } 501804df765aSFam Zheng 50190f12264eSKevin Wolf BlockDriverState *bdrv_next_all_states(BlockDriverState *bs) 50200f12264eSKevin Wolf { 50210f12264eSKevin Wolf if (!bs) { 50220f12264eSKevin Wolf return QTAILQ_FIRST(&all_bdrv_states); 50230f12264eSKevin Wolf } 50240f12264eSKevin Wolf return QTAILQ_NEXT(bs, bs_list); 50250f12264eSKevin Wolf } 50260f12264eSKevin Wolf 502720a9e77dSFam Zheng const char *bdrv_get_node_name(const BlockDriverState *bs) 502820a9e77dSFam Zheng { 502920a9e77dSFam Zheng return bs->node_name; 503020a9e77dSFam Zheng } 503120a9e77dSFam Zheng 50321f0c461bSKevin Wolf const char *bdrv_get_parent_name(const BlockDriverState *bs) 50334c265bf9SKevin Wolf { 50344c265bf9SKevin Wolf BdrvChild *c; 50354c265bf9SKevin Wolf const char *name; 50364c265bf9SKevin Wolf 50374c265bf9SKevin Wolf /* If multiple parents have a name, just pick the first one. */ 50384c265bf9SKevin Wolf QLIST_FOREACH(c, &bs->parents, next_parent) { 50394c265bf9SKevin Wolf if (c->role->get_name) { 50404c265bf9SKevin Wolf name = c->role->get_name(c); 50414c265bf9SKevin Wolf if (name && *name) { 50424c265bf9SKevin Wolf return name; 50434c265bf9SKevin Wolf } 50444c265bf9SKevin Wolf } 50454c265bf9SKevin Wolf } 50464c265bf9SKevin Wolf 50474c265bf9SKevin Wolf return NULL; 50484c265bf9SKevin Wolf } 50494c265bf9SKevin Wolf 50507f06d47eSMarkus Armbruster /* TODO check what callers really want: bs->node_name or blk_name() */ 5051bfb197e0SMarkus Armbruster const char *bdrv_get_device_name(const BlockDriverState *bs) 5052ea2384d3Sbellard { 50534c265bf9SKevin Wolf return bdrv_get_parent_name(bs) ?: ""; 5054ea2384d3Sbellard } 5055ea2384d3Sbellard 50569b2aa84fSAlberto Garcia /* This can be used to identify nodes that might not have a device 50579b2aa84fSAlberto Garcia * name associated. Since node and device names live in the same 50589b2aa84fSAlberto Garcia * namespace, the result is unambiguous. The exception is if both are 50599b2aa84fSAlberto Garcia * absent, then this returns an empty (non-null) string. */ 50609b2aa84fSAlberto Garcia const char *bdrv_get_device_or_node_name(const BlockDriverState *bs) 50619b2aa84fSAlberto Garcia { 50624c265bf9SKevin Wolf return bdrv_get_parent_name(bs) ?: bs->node_name; 50639b2aa84fSAlberto Garcia } 50649b2aa84fSAlberto Garcia 5065c8433287SMarkus Armbruster int bdrv_get_flags(BlockDriverState *bs) 5066c8433287SMarkus Armbruster { 5067c8433287SMarkus Armbruster return bs->open_flags; 5068c8433287SMarkus Armbruster } 5069c8433287SMarkus Armbruster 50703ac21627SPeter Lieven int bdrv_has_zero_init_1(BlockDriverState *bs) 50713ac21627SPeter Lieven { 50723ac21627SPeter Lieven return 1; 50733ac21627SPeter Lieven } 50743ac21627SPeter Lieven 5075f2feebbdSKevin Wolf int bdrv_has_zero_init(BlockDriverState *bs) 5076f2feebbdSKevin Wolf { 5077d470ad42SMax Reitz if (!bs->drv) { 5078d470ad42SMax Reitz return 0; 5079d470ad42SMax Reitz } 5080f2feebbdSKevin Wolf 508111212d8fSPaolo Bonzini /* If BS is a copy on write image, it is initialized to 508211212d8fSPaolo Bonzini the contents of the base image, which may not be zeroes. */ 5083760e0063SKevin Wolf if (bs->backing) { 508411212d8fSPaolo Bonzini return 0; 508511212d8fSPaolo Bonzini } 5086336c1c12SKevin Wolf if (bs->drv->bdrv_has_zero_init) { 5087336c1c12SKevin Wolf return bs->drv->bdrv_has_zero_init(bs); 5088f2feebbdSKevin Wolf } 50895a612c00SManos Pitsidianakis if (bs->file && bs->drv->is_filter) { 50905a612c00SManos Pitsidianakis return bdrv_has_zero_init(bs->file->bs); 50915a612c00SManos Pitsidianakis } 5092f2feebbdSKevin Wolf 50933ac21627SPeter Lieven /* safe default */ 50943ac21627SPeter Lieven return 0; 5095f2feebbdSKevin Wolf } 5096f2feebbdSKevin Wolf 5097ceaca56fSMax Reitz int bdrv_has_zero_init_truncate(BlockDriverState *bs) 5098ceaca56fSMax Reitz { 5099ceaca56fSMax Reitz if (!bs->drv) { 5100ceaca56fSMax Reitz return 0; 5101ceaca56fSMax Reitz } 5102ceaca56fSMax Reitz 5103ceaca56fSMax Reitz if (bs->backing) { 5104ceaca56fSMax Reitz /* Depends on the backing image length, but better safe than sorry */ 5105ceaca56fSMax Reitz return 0; 5106ceaca56fSMax Reitz } 5107ceaca56fSMax Reitz if (bs->drv->bdrv_has_zero_init_truncate) { 5108ceaca56fSMax Reitz return bs->drv->bdrv_has_zero_init_truncate(bs); 5109ceaca56fSMax Reitz } 5110ceaca56fSMax Reitz if (bs->file && bs->drv->is_filter) { 5111ceaca56fSMax Reitz return bdrv_has_zero_init_truncate(bs->file->bs); 5112ceaca56fSMax Reitz } 5113ceaca56fSMax Reitz 5114ceaca56fSMax Reitz /* safe default */ 5115ceaca56fSMax Reitz return 0; 5116ceaca56fSMax Reitz } 5117ceaca56fSMax Reitz 51184ce78691SPeter Lieven bool bdrv_unallocated_blocks_are_zero(BlockDriverState *bs) 51194ce78691SPeter Lieven { 51204ce78691SPeter Lieven BlockDriverInfo bdi; 51214ce78691SPeter Lieven 5122760e0063SKevin Wolf if (bs->backing) { 51234ce78691SPeter Lieven return false; 51244ce78691SPeter Lieven } 51254ce78691SPeter Lieven 51264ce78691SPeter Lieven if (bdrv_get_info(bs, &bdi) == 0) { 51274ce78691SPeter Lieven return bdi.unallocated_blocks_are_zero; 51284ce78691SPeter Lieven } 51294ce78691SPeter Lieven 51304ce78691SPeter Lieven return false; 51314ce78691SPeter Lieven } 51324ce78691SPeter Lieven 51334ce78691SPeter Lieven bool bdrv_can_write_zeroes_with_unmap(BlockDriverState *bs) 51344ce78691SPeter Lieven { 51352f0342efSDenis V. Lunev if (!(bs->open_flags & BDRV_O_UNMAP)) { 51364ce78691SPeter Lieven return false; 51374ce78691SPeter Lieven } 51384ce78691SPeter Lieven 5139e24d813bSEric Blake return bs->supported_zero_flags & BDRV_REQ_MAY_UNMAP; 51404ce78691SPeter Lieven } 51414ce78691SPeter Lieven 514283f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs, 514383f64091Sbellard char *filename, int filename_size) 514483f64091Sbellard { 514583f64091Sbellard pstrcpy(filename, filename_size, bs->backing_file); 514683f64091Sbellard } 514783f64091Sbellard 5148faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 5149faea38e7Sbellard { 5150faea38e7Sbellard BlockDriver *drv = bs->drv; 51515a612c00SManos Pitsidianakis /* if bs->drv == NULL, bs is closed, so there's nothing to do here */ 51525a612c00SManos Pitsidianakis if (!drv) { 515319cb3738Sbellard return -ENOMEDIUM; 51545a612c00SManos Pitsidianakis } 51555a612c00SManos Pitsidianakis if (!drv->bdrv_get_info) { 51565a612c00SManos Pitsidianakis if (bs->file && drv->is_filter) { 51575a612c00SManos Pitsidianakis return bdrv_get_info(bs->file->bs, bdi); 51585a612c00SManos Pitsidianakis } 5159faea38e7Sbellard return -ENOTSUP; 51605a612c00SManos Pitsidianakis } 5161faea38e7Sbellard memset(bdi, 0, sizeof(*bdi)); 5162faea38e7Sbellard return drv->bdrv_get_info(bs, bdi); 5163faea38e7Sbellard } 5164faea38e7Sbellard 51651bf6e9caSAndrey Shinkevich ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs, 51661bf6e9caSAndrey Shinkevich Error **errp) 5167eae041feSMax Reitz { 5168eae041feSMax Reitz BlockDriver *drv = bs->drv; 5169eae041feSMax Reitz if (drv && drv->bdrv_get_specific_info) { 51701bf6e9caSAndrey Shinkevich return drv->bdrv_get_specific_info(bs, errp); 5171eae041feSMax Reitz } 5172eae041feSMax Reitz return NULL; 5173eae041feSMax Reitz } 5174eae041feSMax Reitz 5175d9245599SAnton Nefedov BlockStatsSpecific *bdrv_get_specific_stats(BlockDriverState *bs) 5176d9245599SAnton Nefedov { 5177d9245599SAnton Nefedov BlockDriver *drv = bs->drv; 5178d9245599SAnton Nefedov if (!drv || !drv->bdrv_get_specific_stats) { 5179d9245599SAnton Nefedov return NULL; 5180d9245599SAnton Nefedov } 5181d9245599SAnton Nefedov return drv->bdrv_get_specific_stats(bs); 5182d9245599SAnton Nefedov } 5183d9245599SAnton Nefedov 5184a31939e6SEric Blake void bdrv_debug_event(BlockDriverState *bs, BlkdebugEvent event) 51858b9b0cc2SKevin Wolf { 5186bf736fe3SKevin Wolf if (!bs || !bs->drv || !bs->drv->bdrv_debug_event) { 51878b9b0cc2SKevin Wolf return; 51888b9b0cc2SKevin Wolf } 51898b9b0cc2SKevin Wolf 5190bf736fe3SKevin Wolf bs->drv->bdrv_debug_event(bs, event); 519141c695c7SKevin Wolf } 51928b9b0cc2SKevin Wolf 5193d10529a2SVladimir Sementsov-Ogievskiy static BlockDriverState *bdrv_find_debug_node(BlockDriverState *bs) 519441c695c7SKevin Wolf { 519541c695c7SKevin Wolf while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) { 5196d10529a2SVladimir Sementsov-Ogievskiy if (bs->file) { 5197d10529a2SVladimir Sementsov-Ogievskiy bs = bs->file->bs; 5198d10529a2SVladimir Sementsov-Ogievskiy continue; 5199d10529a2SVladimir Sementsov-Ogievskiy } 5200d10529a2SVladimir Sementsov-Ogievskiy 5201d10529a2SVladimir Sementsov-Ogievskiy if (bs->drv->is_filter && bs->backing) { 5202d10529a2SVladimir Sementsov-Ogievskiy bs = bs->backing->bs; 5203d10529a2SVladimir Sementsov-Ogievskiy continue; 5204d10529a2SVladimir Sementsov-Ogievskiy } 5205d10529a2SVladimir Sementsov-Ogievskiy 5206d10529a2SVladimir Sementsov-Ogievskiy break; 520741c695c7SKevin Wolf } 520841c695c7SKevin Wolf 520941c695c7SKevin Wolf if (bs && bs->drv && bs->drv->bdrv_debug_breakpoint) { 5210d10529a2SVladimir Sementsov-Ogievskiy assert(bs->drv->bdrv_debug_remove_breakpoint); 5211d10529a2SVladimir Sementsov-Ogievskiy return bs; 5212d10529a2SVladimir Sementsov-Ogievskiy } 5213d10529a2SVladimir Sementsov-Ogievskiy 5214d10529a2SVladimir Sementsov-Ogievskiy return NULL; 5215d10529a2SVladimir Sementsov-Ogievskiy } 5216d10529a2SVladimir Sementsov-Ogievskiy 5217d10529a2SVladimir Sementsov-Ogievskiy int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event, 5218d10529a2SVladimir Sementsov-Ogievskiy const char *tag) 5219d10529a2SVladimir Sementsov-Ogievskiy { 5220d10529a2SVladimir Sementsov-Ogievskiy bs = bdrv_find_debug_node(bs); 5221d10529a2SVladimir Sementsov-Ogievskiy if (bs) { 522241c695c7SKevin Wolf return bs->drv->bdrv_debug_breakpoint(bs, event, tag); 522341c695c7SKevin Wolf } 522441c695c7SKevin Wolf 522541c695c7SKevin Wolf return -ENOTSUP; 522641c695c7SKevin Wolf } 522741c695c7SKevin Wolf 52284cc70e93SFam Zheng int bdrv_debug_remove_breakpoint(BlockDriverState *bs, const char *tag) 52294cc70e93SFam Zheng { 5230d10529a2SVladimir Sementsov-Ogievskiy bs = bdrv_find_debug_node(bs); 5231d10529a2SVladimir Sementsov-Ogievskiy if (bs) { 52324cc70e93SFam Zheng return bs->drv->bdrv_debug_remove_breakpoint(bs, tag); 52334cc70e93SFam Zheng } 52344cc70e93SFam Zheng 52354cc70e93SFam Zheng return -ENOTSUP; 52364cc70e93SFam Zheng } 52374cc70e93SFam Zheng 523841c695c7SKevin Wolf int bdrv_debug_resume(BlockDriverState *bs, const char *tag) 523941c695c7SKevin Wolf { 5240938789eaSMax Reitz while (bs && (!bs->drv || !bs->drv->bdrv_debug_resume)) { 52419a4f4c31SKevin Wolf bs = bs->file ? bs->file->bs : NULL; 524241c695c7SKevin Wolf } 524341c695c7SKevin Wolf 524441c695c7SKevin Wolf if (bs && bs->drv && bs->drv->bdrv_debug_resume) { 524541c695c7SKevin Wolf return bs->drv->bdrv_debug_resume(bs, tag); 524641c695c7SKevin Wolf } 524741c695c7SKevin Wolf 524841c695c7SKevin Wolf return -ENOTSUP; 524941c695c7SKevin Wolf } 525041c695c7SKevin Wolf 525141c695c7SKevin Wolf bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag) 525241c695c7SKevin Wolf { 525341c695c7SKevin Wolf while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) { 52549a4f4c31SKevin Wolf bs = bs->file ? bs->file->bs : NULL; 525541c695c7SKevin Wolf } 525641c695c7SKevin Wolf 525741c695c7SKevin Wolf if (bs && bs->drv && bs->drv->bdrv_debug_is_suspended) { 525841c695c7SKevin Wolf return bs->drv->bdrv_debug_is_suspended(bs, tag); 525941c695c7SKevin Wolf } 526041c695c7SKevin Wolf 526141c695c7SKevin Wolf return false; 52628b9b0cc2SKevin Wolf } 52638b9b0cc2SKevin Wolf 5264b1b1d783SJeff Cody /* backing_file can either be relative, or absolute, or a protocol. If it is 5265b1b1d783SJeff Cody * relative, it must be relative to the chain. So, passing in bs->filename 5266b1b1d783SJeff Cody * from a BDS as backing_file should not be done, as that may be relative to 5267b1b1d783SJeff Cody * the CWD rather than the chain. */ 5268e8a6bb9cSMarcelo Tosatti BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs, 5269e8a6bb9cSMarcelo Tosatti const char *backing_file) 5270e8a6bb9cSMarcelo Tosatti { 5271b1b1d783SJeff Cody char *filename_full = NULL; 5272b1b1d783SJeff Cody char *backing_file_full = NULL; 5273b1b1d783SJeff Cody char *filename_tmp = NULL; 5274b1b1d783SJeff Cody int is_protocol = 0; 5275b1b1d783SJeff Cody BlockDriverState *curr_bs = NULL; 5276b1b1d783SJeff Cody BlockDriverState *retval = NULL; 5277b1b1d783SJeff Cody 5278b1b1d783SJeff Cody if (!bs || !bs->drv || !backing_file) { 5279e8a6bb9cSMarcelo Tosatti return NULL; 5280e8a6bb9cSMarcelo Tosatti } 5281e8a6bb9cSMarcelo Tosatti 5282b1b1d783SJeff Cody filename_full = g_malloc(PATH_MAX); 5283b1b1d783SJeff Cody backing_file_full = g_malloc(PATH_MAX); 5284b1b1d783SJeff Cody 5285b1b1d783SJeff Cody is_protocol = path_has_protocol(backing_file); 5286b1b1d783SJeff Cody 5287760e0063SKevin Wolf for (curr_bs = bs; curr_bs->backing; curr_bs = curr_bs->backing->bs) { 5288b1b1d783SJeff Cody 5289b1b1d783SJeff Cody /* If either of the filename paths is actually a protocol, then 5290b1b1d783SJeff Cody * compare unmodified paths; otherwise make paths relative */ 5291b1b1d783SJeff Cody if (is_protocol || path_has_protocol(curr_bs->backing_file)) { 52926b6833c1SMax Reitz char *backing_file_full_ret; 52936b6833c1SMax Reitz 5294b1b1d783SJeff Cody if (strcmp(backing_file, curr_bs->backing_file) == 0) { 5295760e0063SKevin Wolf retval = curr_bs->backing->bs; 5296b1b1d783SJeff Cody break; 5297b1b1d783SJeff Cody } 5298418661e0SJeff Cody /* Also check against the full backing filename for the image */ 52996b6833c1SMax Reitz backing_file_full_ret = bdrv_get_full_backing_filename(curr_bs, 53006b6833c1SMax Reitz NULL); 53016b6833c1SMax Reitz if (backing_file_full_ret) { 53026b6833c1SMax Reitz bool equal = strcmp(backing_file, backing_file_full_ret) == 0; 53036b6833c1SMax Reitz g_free(backing_file_full_ret); 53046b6833c1SMax Reitz if (equal) { 5305418661e0SJeff Cody retval = curr_bs->backing->bs; 5306418661e0SJeff Cody break; 5307418661e0SJeff Cody } 5308418661e0SJeff Cody } 5309e8a6bb9cSMarcelo Tosatti } else { 5310b1b1d783SJeff Cody /* If not an absolute filename path, make it relative to the current 5311b1b1d783SJeff Cody * image's filename path */ 53122d9158ceSMax Reitz filename_tmp = bdrv_make_absolute_filename(curr_bs, backing_file, 53132d9158ceSMax Reitz NULL); 53142d9158ceSMax Reitz /* We are going to compare canonicalized absolute pathnames */ 53152d9158ceSMax Reitz if (!filename_tmp || !realpath(filename_tmp, filename_full)) { 53162d9158ceSMax Reitz g_free(filename_tmp); 5317b1b1d783SJeff Cody continue; 5318b1b1d783SJeff Cody } 53192d9158ceSMax Reitz g_free(filename_tmp); 5320b1b1d783SJeff Cody 5321b1b1d783SJeff Cody /* We need to make sure the backing filename we are comparing against 5322b1b1d783SJeff Cody * is relative to the current image filename (or absolute) */ 53232d9158ceSMax Reitz filename_tmp = bdrv_get_full_backing_filename(curr_bs, NULL); 53242d9158ceSMax Reitz if (!filename_tmp || !realpath(filename_tmp, backing_file_full)) { 53252d9158ceSMax Reitz g_free(filename_tmp); 5326b1b1d783SJeff Cody continue; 5327b1b1d783SJeff Cody } 53282d9158ceSMax Reitz g_free(filename_tmp); 5329b1b1d783SJeff Cody 5330b1b1d783SJeff Cody if (strcmp(backing_file_full, filename_full) == 0) { 5331760e0063SKevin Wolf retval = curr_bs->backing->bs; 5332b1b1d783SJeff Cody break; 5333b1b1d783SJeff Cody } 5334e8a6bb9cSMarcelo Tosatti } 5335e8a6bb9cSMarcelo Tosatti } 5336e8a6bb9cSMarcelo Tosatti 5337b1b1d783SJeff Cody g_free(filename_full); 5338b1b1d783SJeff Cody g_free(backing_file_full); 5339b1b1d783SJeff Cody return retval; 5340e8a6bb9cSMarcelo Tosatti } 5341e8a6bb9cSMarcelo Tosatti 5342ea2384d3Sbellard void bdrv_init(void) 5343ea2384d3Sbellard { 53445efa9d5aSAnthony Liguori module_call_init(MODULE_INIT_BLOCK); 5345ea2384d3Sbellard } 5346ce1a14dcSpbrook 5347eb852011SMarkus Armbruster void bdrv_init_with_whitelist(void) 5348eb852011SMarkus Armbruster { 5349eb852011SMarkus Armbruster use_bdrv_whitelist = 1; 5350eb852011SMarkus Armbruster bdrv_init(); 5351eb852011SMarkus Armbruster } 5352eb852011SMarkus Armbruster 53532b148f39SPaolo Bonzini static void coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, 53542b148f39SPaolo Bonzini Error **errp) 53550f15423cSAnthony Liguori { 53564417ab7aSKevin Wolf BdrvChild *child, *parent; 53579c5e6594SKevin Wolf uint64_t perm, shared_perm; 53585a8a30dbSKevin Wolf Error *local_err = NULL; 53595a8a30dbSKevin Wolf int ret; 53609c98f145SVladimir Sementsov-Ogievskiy BdrvDirtyBitmap *bm; 53615a8a30dbSKevin Wolf 53623456a8d1SKevin Wolf if (!bs->drv) { 53633456a8d1SKevin Wolf return; 53640f15423cSAnthony Liguori } 53653456a8d1SKevin Wolf 536616e977d5SVladimir Sementsov-Ogievskiy QLIST_FOREACH(child, &bs->children, next) { 53672b148f39SPaolo Bonzini bdrv_co_invalidate_cache(child->bs, &local_err); 53685a8a30dbSKevin Wolf if (local_err) { 53695a8a30dbSKevin Wolf error_propagate(errp, local_err); 53705a8a30dbSKevin Wolf return; 53713456a8d1SKevin Wolf } 53720d1c5c91SFam Zheng } 53730d1c5c91SFam Zheng 5374dafe0960SKevin Wolf /* 5375dafe0960SKevin Wolf * Update permissions, they may differ for inactive nodes. 5376dafe0960SKevin Wolf * 5377dafe0960SKevin Wolf * Note that the required permissions of inactive images are always a 5378dafe0960SKevin Wolf * subset of the permissions required after activating the image. This 5379dafe0960SKevin Wolf * allows us to just get the permissions upfront without restricting 5380dafe0960SKevin Wolf * drv->bdrv_invalidate_cache(). 5381dafe0960SKevin Wolf * 5382dafe0960SKevin Wolf * It also means that in error cases, we don't have to try and revert to 5383dafe0960SKevin Wolf * the old permissions (which is an operation that could fail, too). We can 5384dafe0960SKevin Wolf * just keep the extended permissions for the next time that an activation 5385dafe0960SKevin Wolf * of the image is tried. 5386dafe0960SKevin Wolf */ 53877bb4941aSKevin Wolf if (bs->open_flags & BDRV_O_INACTIVE) { 538816e977d5SVladimir Sementsov-Ogievskiy bs->open_flags &= ~BDRV_O_INACTIVE; 5389dafe0960SKevin Wolf bdrv_get_cumulative_perm(bs, &perm, &shared_perm); 53909eab1544SMax Reitz ret = bdrv_check_perm(bs, NULL, perm, shared_perm, NULL, NULL, &local_err); 5391dafe0960SKevin Wolf if (ret < 0) { 5392dafe0960SKevin Wolf bs->open_flags |= BDRV_O_INACTIVE; 5393dafe0960SKevin Wolf error_propagate(errp, local_err); 5394dafe0960SKevin Wolf return; 5395dafe0960SKevin Wolf } 5396dafe0960SKevin Wolf bdrv_set_perm(bs, perm, shared_perm); 5397dafe0960SKevin Wolf 53982b148f39SPaolo Bonzini if (bs->drv->bdrv_co_invalidate_cache) { 53992b148f39SPaolo Bonzini bs->drv->bdrv_co_invalidate_cache(bs, &local_err); 54000d1c5c91SFam Zheng if (local_err) { 54010d1c5c91SFam Zheng bs->open_flags |= BDRV_O_INACTIVE; 54020d1c5c91SFam Zheng error_propagate(errp, local_err); 54030d1c5c91SFam Zheng return; 54040d1c5c91SFam Zheng } 54050d1c5c91SFam Zheng } 54063456a8d1SKevin Wolf 5407ef9041a7SVladimir Sementsov-Ogievskiy FOR_EACH_DIRTY_BITMAP(bs, bm) { 5408c4e4b0faSJohn Snow bdrv_dirty_bitmap_skip_store(bm, false); 54099c98f145SVladimir Sementsov-Ogievskiy } 54109c98f145SVladimir Sementsov-Ogievskiy 54115a8a30dbSKevin Wolf ret = refresh_total_sectors(bs, bs->total_sectors); 54125a8a30dbSKevin Wolf if (ret < 0) { 541304c01a5cSKevin Wolf bs->open_flags |= BDRV_O_INACTIVE; 54145a8a30dbSKevin Wolf error_setg_errno(errp, -ret, "Could not refresh total sector count"); 54155a8a30dbSKevin Wolf return; 54165a8a30dbSKevin Wolf } 54177bb4941aSKevin Wolf } 54184417ab7aSKevin Wolf 54194417ab7aSKevin Wolf QLIST_FOREACH(parent, &bs->parents, next_parent) { 54204417ab7aSKevin Wolf if (parent->role->activate) { 54214417ab7aSKevin Wolf parent->role->activate(parent, &local_err); 54224417ab7aSKevin Wolf if (local_err) { 542378fc3b3aSKevin Wolf bs->open_flags |= BDRV_O_INACTIVE; 54244417ab7aSKevin Wolf error_propagate(errp, local_err); 54254417ab7aSKevin Wolf return; 54264417ab7aSKevin Wolf } 54274417ab7aSKevin Wolf } 54284417ab7aSKevin Wolf } 54290f15423cSAnthony Liguori } 54300f15423cSAnthony Liguori 54312b148f39SPaolo Bonzini typedef struct InvalidateCacheCo { 54322b148f39SPaolo Bonzini BlockDriverState *bs; 54332b148f39SPaolo Bonzini Error **errp; 54342b148f39SPaolo Bonzini bool done; 54352b148f39SPaolo Bonzini } InvalidateCacheCo; 54362b148f39SPaolo Bonzini 54372b148f39SPaolo Bonzini static void coroutine_fn bdrv_invalidate_cache_co_entry(void *opaque) 54382b148f39SPaolo Bonzini { 54392b148f39SPaolo Bonzini InvalidateCacheCo *ico = opaque; 54402b148f39SPaolo Bonzini bdrv_co_invalidate_cache(ico->bs, ico->errp); 54412b148f39SPaolo Bonzini ico->done = true; 54424720cbeeSKevin Wolf aio_wait_kick(); 54432b148f39SPaolo Bonzini } 54442b148f39SPaolo Bonzini 54452b148f39SPaolo Bonzini void bdrv_invalidate_cache(BlockDriverState *bs, Error **errp) 54462b148f39SPaolo Bonzini { 54472b148f39SPaolo Bonzini Coroutine *co; 54482b148f39SPaolo Bonzini InvalidateCacheCo ico = { 54492b148f39SPaolo Bonzini .bs = bs, 54502b148f39SPaolo Bonzini .done = false, 54512b148f39SPaolo Bonzini .errp = errp 54522b148f39SPaolo Bonzini }; 54532b148f39SPaolo Bonzini 54542b148f39SPaolo Bonzini if (qemu_in_coroutine()) { 54552b148f39SPaolo Bonzini /* Fast-path if already in coroutine context */ 54562b148f39SPaolo Bonzini bdrv_invalidate_cache_co_entry(&ico); 54572b148f39SPaolo Bonzini } else { 54582b148f39SPaolo Bonzini co = qemu_coroutine_create(bdrv_invalidate_cache_co_entry, &ico); 54594720cbeeSKevin Wolf bdrv_coroutine_enter(bs, co); 54602b148f39SPaolo Bonzini BDRV_POLL_WHILE(bs, !ico.done); 54612b148f39SPaolo Bonzini } 54622b148f39SPaolo Bonzini } 54632b148f39SPaolo Bonzini 54645a8a30dbSKevin Wolf void bdrv_invalidate_cache_all(Error **errp) 54650f15423cSAnthony Liguori { 54667c8eece4SKevin Wolf BlockDriverState *bs; 54675a8a30dbSKevin Wolf Error *local_err = NULL; 546888be7b4bSKevin Wolf BdrvNextIterator it; 54690f15423cSAnthony Liguori 547088be7b4bSKevin Wolf for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) { 5471ed78cda3SStefan Hajnoczi AioContext *aio_context = bdrv_get_aio_context(bs); 5472ed78cda3SStefan Hajnoczi 5473ed78cda3SStefan Hajnoczi aio_context_acquire(aio_context); 54745a8a30dbSKevin Wolf bdrv_invalidate_cache(bs, &local_err); 5475ed78cda3SStefan Hajnoczi aio_context_release(aio_context); 54765a8a30dbSKevin Wolf if (local_err) { 54775a8a30dbSKevin Wolf error_propagate(errp, local_err); 54785e003f17SMax Reitz bdrv_next_cleanup(&it); 54795a8a30dbSKevin Wolf return; 54805a8a30dbSKevin Wolf } 54810f15423cSAnthony Liguori } 54820f15423cSAnthony Liguori } 54830f15423cSAnthony Liguori 54849e37271fSKevin Wolf static bool bdrv_has_bds_parent(BlockDriverState *bs, bool only_active) 54859e37271fSKevin Wolf { 54869e37271fSKevin Wolf BdrvChild *parent; 54879e37271fSKevin Wolf 54889e37271fSKevin Wolf QLIST_FOREACH(parent, &bs->parents, next_parent) { 54899e37271fSKevin Wolf if (parent->role->parent_is_bds) { 54909e37271fSKevin Wolf BlockDriverState *parent_bs = parent->opaque; 54919e37271fSKevin Wolf if (!only_active || !(parent_bs->open_flags & BDRV_O_INACTIVE)) { 54929e37271fSKevin Wolf return true; 54939e37271fSKevin Wolf } 54949e37271fSKevin Wolf } 54959e37271fSKevin Wolf } 54969e37271fSKevin Wolf 54979e37271fSKevin Wolf return false; 54989e37271fSKevin Wolf } 54999e37271fSKevin Wolf 55009e37271fSKevin Wolf static int bdrv_inactivate_recurse(BlockDriverState *bs) 550176b1c7feSKevin Wolf { 5502cfa1a572SKevin Wolf BdrvChild *child, *parent; 55031046779eSMax Reitz bool tighten_restrictions; 55049e37271fSKevin Wolf uint64_t perm, shared_perm; 550576b1c7feSKevin Wolf int ret; 550676b1c7feSKevin Wolf 5507d470ad42SMax Reitz if (!bs->drv) { 5508d470ad42SMax Reitz return -ENOMEDIUM; 5509d470ad42SMax Reitz } 5510d470ad42SMax Reitz 55119e37271fSKevin Wolf /* Make sure that we don't inactivate a child before its parent. 55129e37271fSKevin Wolf * It will be covered by recursion from the yet active parent. */ 55139e37271fSKevin Wolf if (bdrv_has_bds_parent(bs, true)) { 55149e37271fSKevin Wolf return 0; 55159e37271fSKevin Wolf } 55169e37271fSKevin Wolf 55179e37271fSKevin Wolf assert(!(bs->open_flags & BDRV_O_INACTIVE)); 55189e37271fSKevin Wolf 55199e37271fSKevin Wolf /* Inactivate this node */ 55209e37271fSKevin Wolf if (bs->drv->bdrv_inactivate) { 552176b1c7feSKevin Wolf ret = bs->drv->bdrv_inactivate(bs); 552276b1c7feSKevin Wolf if (ret < 0) { 552376b1c7feSKevin Wolf return ret; 552476b1c7feSKevin Wolf } 552576b1c7feSKevin Wolf } 552676b1c7feSKevin Wolf 5527cfa1a572SKevin Wolf QLIST_FOREACH(parent, &bs->parents, next_parent) { 5528cfa1a572SKevin Wolf if (parent->role->inactivate) { 5529cfa1a572SKevin Wolf ret = parent->role->inactivate(parent); 5530cfa1a572SKevin Wolf if (ret < 0) { 5531cfa1a572SKevin Wolf return ret; 5532cfa1a572SKevin Wolf } 5533cfa1a572SKevin Wolf } 5534cfa1a572SKevin Wolf } 55359c5e6594SKevin Wolf 55367d5b5261SStefan Hajnoczi bs->open_flags |= BDRV_O_INACTIVE; 55377d5b5261SStefan Hajnoczi 55389c5e6594SKevin Wolf /* Update permissions, they may differ for inactive nodes */ 55399c5e6594SKevin Wolf bdrv_get_cumulative_perm(bs, &perm, &shared_perm); 55401046779eSMax Reitz ret = bdrv_check_perm(bs, NULL, perm, shared_perm, NULL, 55411046779eSMax Reitz &tighten_restrictions, NULL); 55421046779eSMax Reitz assert(tighten_restrictions == false); 55431046779eSMax Reitz if (ret < 0) { 55441046779eSMax Reitz /* We only tried to loosen restrictions, so errors are not fatal */ 55451046779eSMax Reitz bdrv_abort_perm_update(bs); 55461046779eSMax Reitz } else { 55479c5e6594SKevin Wolf bdrv_set_perm(bs, perm, shared_perm); 55481046779eSMax Reitz } 554938701b6aSKevin Wolf 55509e37271fSKevin Wolf 55519e37271fSKevin Wolf /* Recursively inactivate children */ 555238701b6aSKevin Wolf QLIST_FOREACH(child, &bs->children, next) { 55539e37271fSKevin Wolf ret = bdrv_inactivate_recurse(child->bs); 555438701b6aSKevin Wolf if (ret < 0) { 555538701b6aSKevin Wolf return ret; 555638701b6aSKevin Wolf } 555738701b6aSKevin Wolf } 555838701b6aSKevin Wolf 555976b1c7feSKevin Wolf return 0; 556076b1c7feSKevin Wolf } 556176b1c7feSKevin Wolf 556276b1c7feSKevin Wolf int bdrv_inactivate_all(void) 556376b1c7feSKevin Wolf { 556479720af6SMax Reitz BlockDriverState *bs = NULL; 556588be7b4bSKevin Wolf BdrvNextIterator it; 5566aad0b7a0SFam Zheng int ret = 0; 5567bd6458e4SPaolo Bonzini GSList *aio_ctxs = NULL, *ctx; 556876b1c7feSKevin Wolf 556988be7b4bSKevin Wolf for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) { 5570bd6458e4SPaolo Bonzini AioContext *aio_context = bdrv_get_aio_context(bs); 5571bd6458e4SPaolo Bonzini 5572bd6458e4SPaolo Bonzini if (!g_slist_find(aio_ctxs, aio_context)) { 5573bd6458e4SPaolo Bonzini aio_ctxs = g_slist_prepend(aio_ctxs, aio_context); 5574bd6458e4SPaolo Bonzini aio_context_acquire(aio_context); 5575bd6458e4SPaolo Bonzini } 5576aad0b7a0SFam Zheng } 557776b1c7feSKevin Wolf 557888be7b4bSKevin Wolf for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) { 55799e37271fSKevin Wolf /* Nodes with BDS parents are covered by recursion from the last 55809e37271fSKevin Wolf * parent that gets inactivated. Don't inactivate them a second 55819e37271fSKevin Wolf * time if that has already happened. */ 55829e37271fSKevin Wolf if (bdrv_has_bds_parent(bs, false)) { 55839e37271fSKevin Wolf continue; 55849e37271fSKevin Wolf } 55859e37271fSKevin Wolf ret = bdrv_inactivate_recurse(bs); 558676b1c7feSKevin Wolf if (ret < 0) { 55875e003f17SMax Reitz bdrv_next_cleanup(&it); 5588aad0b7a0SFam Zheng goto out; 5589aad0b7a0SFam Zheng } 559076b1c7feSKevin Wolf } 559176b1c7feSKevin Wolf 5592aad0b7a0SFam Zheng out: 5593bd6458e4SPaolo Bonzini for (ctx = aio_ctxs; ctx != NULL; ctx = ctx->next) { 5594bd6458e4SPaolo Bonzini AioContext *aio_context = ctx->data; 5595bd6458e4SPaolo Bonzini aio_context_release(aio_context); 5596aad0b7a0SFam Zheng } 5597bd6458e4SPaolo Bonzini g_slist_free(aio_ctxs); 5598aad0b7a0SFam Zheng 5599aad0b7a0SFam Zheng return ret; 560076b1c7feSKevin Wolf } 560176b1c7feSKevin Wolf 5602f9f05dc5SKevin Wolf /**************************************************************/ 560319cb3738Sbellard /* removable device support */ 560419cb3738Sbellard 560519cb3738Sbellard /** 560619cb3738Sbellard * Return TRUE if the media is present 560719cb3738Sbellard */ 5608e031f750SMax Reitz bool bdrv_is_inserted(BlockDriverState *bs) 560919cb3738Sbellard { 561019cb3738Sbellard BlockDriver *drv = bs->drv; 561128d7a789SMax Reitz BdrvChild *child; 5612a1aff5bfSMarkus Armbruster 5613e031f750SMax Reitz if (!drv) { 5614e031f750SMax Reitz return false; 5615e031f750SMax Reitz } 561628d7a789SMax Reitz if (drv->bdrv_is_inserted) { 5617a1aff5bfSMarkus Armbruster return drv->bdrv_is_inserted(bs); 561819cb3738Sbellard } 561928d7a789SMax Reitz QLIST_FOREACH(child, &bs->children, next) { 562028d7a789SMax Reitz if (!bdrv_is_inserted(child->bs)) { 562128d7a789SMax Reitz return false; 562228d7a789SMax Reitz } 562328d7a789SMax Reitz } 562428d7a789SMax Reitz return true; 562528d7a789SMax Reitz } 562619cb3738Sbellard 562719cb3738Sbellard /** 562819cb3738Sbellard * If eject_flag is TRUE, eject the media. Otherwise, close the tray 562919cb3738Sbellard */ 5630f36f3949SLuiz Capitulino void bdrv_eject(BlockDriverState *bs, bool eject_flag) 563119cb3738Sbellard { 563219cb3738Sbellard BlockDriver *drv = bs->drv; 563319cb3738Sbellard 5634822e1cd1SMarkus Armbruster if (drv && drv->bdrv_eject) { 5635822e1cd1SMarkus Armbruster drv->bdrv_eject(bs, eject_flag); 563619cb3738Sbellard } 563719cb3738Sbellard } 563819cb3738Sbellard 563919cb3738Sbellard /** 564019cb3738Sbellard * Lock or unlock the media (if it is locked, the user won't be able 564119cb3738Sbellard * to eject it manually). 564219cb3738Sbellard */ 5643025e849aSMarkus Armbruster void bdrv_lock_medium(BlockDriverState *bs, bool locked) 564419cb3738Sbellard { 564519cb3738Sbellard BlockDriver *drv = bs->drv; 564619cb3738Sbellard 5647025e849aSMarkus Armbruster trace_bdrv_lock_medium(bs, locked); 5648b8c6d095SStefan Hajnoczi 5649025e849aSMarkus Armbruster if (drv && drv->bdrv_lock_medium) { 5650025e849aSMarkus Armbruster drv->bdrv_lock_medium(bs, locked); 565119cb3738Sbellard } 565219cb3738Sbellard } 5653985a03b0Sths 56549fcb0251SFam Zheng /* Get a reference to bs */ 56559fcb0251SFam Zheng void bdrv_ref(BlockDriverState *bs) 56569fcb0251SFam Zheng { 56579fcb0251SFam Zheng bs->refcnt++; 56589fcb0251SFam Zheng } 56599fcb0251SFam Zheng 56609fcb0251SFam Zheng /* Release a previously grabbed reference to bs. 56619fcb0251SFam Zheng * If after releasing, reference count is zero, the BlockDriverState is 56629fcb0251SFam Zheng * deleted. */ 56639fcb0251SFam Zheng void bdrv_unref(BlockDriverState *bs) 56649fcb0251SFam Zheng { 56659a4d5ca6SJeff Cody if (!bs) { 56669a4d5ca6SJeff Cody return; 56679a4d5ca6SJeff Cody } 56689fcb0251SFam Zheng assert(bs->refcnt > 0); 56699fcb0251SFam Zheng if (--bs->refcnt == 0) { 56709fcb0251SFam Zheng bdrv_delete(bs); 56719fcb0251SFam Zheng } 56729fcb0251SFam Zheng } 56739fcb0251SFam Zheng 5674fbe40ff7SFam Zheng struct BdrvOpBlocker { 5675fbe40ff7SFam Zheng Error *reason; 5676fbe40ff7SFam Zheng QLIST_ENTRY(BdrvOpBlocker) list; 5677fbe40ff7SFam Zheng }; 5678fbe40ff7SFam Zheng 5679fbe40ff7SFam Zheng bool bdrv_op_is_blocked(BlockDriverState *bs, BlockOpType op, Error **errp) 5680fbe40ff7SFam Zheng { 5681fbe40ff7SFam Zheng BdrvOpBlocker *blocker; 5682fbe40ff7SFam Zheng assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX); 5683fbe40ff7SFam Zheng if (!QLIST_EMPTY(&bs->op_blockers[op])) { 5684fbe40ff7SFam Zheng blocker = QLIST_FIRST(&bs->op_blockers[op]); 56854b576648SMarkus Armbruster error_propagate_prepend(errp, error_copy(blocker->reason), 56864b576648SMarkus Armbruster "Node '%s' is busy: ", 5687e43bfd9cSMarkus Armbruster bdrv_get_device_or_node_name(bs)); 5688fbe40ff7SFam Zheng return true; 5689fbe40ff7SFam Zheng } 5690fbe40ff7SFam Zheng return false; 5691fbe40ff7SFam Zheng } 5692fbe40ff7SFam Zheng 5693fbe40ff7SFam Zheng void bdrv_op_block(BlockDriverState *bs, BlockOpType op, Error *reason) 5694fbe40ff7SFam Zheng { 5695fbe40ff7SFam Zheng BdrvOpBlocker *blocker; 5696fbe40ff7SFam Zheng assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX); 5697fbe40ff7SFam Zheng 56985839e53bSMarkus Armbruster blocker = g_new0(BdrvOpBlocker, 1); 5699fbe40ff7SFam Zheng blocker->reason = reason; 5700fbe40ff7SFam Zheng QLIST_INSERT_HEAD(&bs->op_blockers[op], blocker, list); 5701fbe40ff7SFam Zheng } 5702fbe40ff7SFam Zheng 5703fbe40ff7SFam Zheng void bdrv_op_unblock(BlockDriverState *bs, BlockOpType op, Error *reason) 5704fbe40ff7SFam Zheng { 5705fbe40ff7SFam Zheng BdrvOpBlocker *blocker, *next; 5706fbe40ff7SFam Zheng assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX); 5707fbe40ff7SFam Zheng QLIST_FOREACH_SAFE(blocker, &bs->op_blockers[op], list, next) { 5708fbe40ff7SFam Zheng if (blocker->reason == reason) { 5709fbe40ff7SFam Zheng QLIST_REMOVE(blocker, list); 5710fbe40ff7SFam Zheng g_free(blocker); 5711fbe40ff7SFam Zheng } 5712fbe40ff7SFam Zheng } 5713fbe40ff7SFam Zheng } 5714fbe40ff7SFam Zheng 5715fbe40ff7SFam Zheng void bdrv_op_block_all(BlockDriverState *bs, Error *reason) 5716fbe40ff7SFam Zheng { 5717fbe40ff7SFam Zheng int i; 5718fbe40ff7SFam Zheng for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) { 5719fbe40ff7SFam Zheng bdrv_op_block(bs, i, reason); 5720fbe40ff7SFam Zheng } 5721fbe40ff7SFam Zheng } 5722fbe40ff7SFam Zheng 5723fbe40ff7SFam Zheng void bdrv_op_unblock_all(BlockDriverState *bs, Error *reason) 5724fbe40ff7SFam Zheng { 5725fbe40ff7SFam Zheng int i; 5726fbe40ff7SFam Zheng for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) { 5727fbe40ff7SFam Zheng bdrv_op_unblock(bs, i, reason); 5728fbe40ff7SFam Zheng } 5729fbe40ff7SFam Zheng } 5730fbe40ff7SFam Zheng 5731fbe40ff7SFam Zheng bool bdrv_op_blocker_is_empty(BlockDriverState *bs) 5732fbe40ff7SFam Zheng { 5733fbe40ff7SFam Zheng int i; 5734fbe40ff7SFam Zheng 5735fbe40ff7SFam Zheng for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) { 5736fbe40ff7SFam Zheng if (!QLIST_EMPTY(&bs->op_blockers[i])) { 5737fbe40ff7SFam Zheng return false; 5738fbe40ff7SFam Zheng } 5739fbe40ff7SFam Zheng } 5740fbe40ff7SFam Zheng return true; 5741fbe40ff7SFam Zheng } 5742fbe40ff7SFam Zheng 5743d92ada22SLuiz Capitulino void bdrv_img_create(const char *filename, const char *fmt, 5744f88e1a42SJes Sorensen const char *base_filename, const char *base_fmt, 57459217283dSFam Zheng char *options, uint64_t img_size, int flags, bool quiet, 57469217283dSFam Zheng Error **errp) 5747f88e1a42SJes Sorensen { 574883d0521aSChunyan Liu QemuOptsList *create_opts = NULL; 574983d0521aSChunyan Liu QemuOpts *opts = NULL; 575083d0521aSChunyan Liu const char *backing_fmt, *backing_file; 575183d0521aSChunyan Liu int64_t size; 5752f88e1a42SJes Sorensen BlockDriver *drv, *proto_drv; 5753cc84d90fSMax Reitz Error *local_err = NULL; 5754f88e1a42SJes Sorensen int ret = 0; 5755f88e1a42SJes Sorensen 5756f88e1a42SJes Sorensen /* Find driver and parse its options */ 5757f88e1a42SJes Sorensen drv = bdrv_find_format(fmt); 5758f88e1a42SJes Sorensen if (!drv) { 575971c79813SLuiz Capitulino error_setg(errp, "Unknown file format '%s'", fmt); 5760d92ada22SLuiz Capitulino return; 5761f88e1a42SJes Sorensen } 5762f88e1a42SJes Sorensen 5763b65a5e12SMax Reitz proto_drv = bdrv_find_protocol(filename, true, errp); 5764f88e1a42SJes Sorensen if (!proto_drv) { 5765d92ada22SLuiz Capitulino return; 5766f88e1a42SJes Sorensen } 5767f88e1a42SJes Sorensen 5768c6149724SMax Reitz if (!drv->create_opts) { 5769c6149724SMax Reitz error_setg(errp, "Format driver '%s' does not support image creation", 5770c6149724SMax Reitz drv->format_name); 5771c6149724SMax Reitz return; 5772c6149724SMax Reitz } 5773c6149724SMax Reitz 5774c6149724SMax Reitz if (!proto_drv->create_opts) { 5775c6149724SMax Reitz error_setg(errp, "Protocol driver '%s' does not support image creation", 5776c6149724SMax Reitz proto_drv->format_name); 5777c6149724SMax Reitz return; 5778c6149724SMax Reitz } 5779c6149724SMax Reitz 5780f6dc1c31SKevin Wolf /* Create parameter list */ 5781c282e1fdSChunyan Liu create_opts = qemu_opts_append(create_opts, drv->create_opts); 5782c282e1fdSChunyan Liu create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); 5783f88e1a42SJes Sorensen 578483d0521aSChunyan Liu opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); 5785f88e1a42SJes Sorensen 5786f88e1a42SJes Sorensen /* Parse -o options */ 5787f88e1a42SJes Sorensen if (options) { 5788dc523cd3SMarkus Armbruster qemu_opts_do_parse(opts, options, NULL, &local_err); 5789dc523cd3SMarkus Armbruster if (local_err) { 5790f88e1a42SJes Sorensen goto out; 5791f88e1a42SJes Sorensen } 5792f88e1a42SJes Sorensen } 5793f88e1a42SJes Sorensen 5794f6dc1c31SKevin Wolf if (!qemu_opt_get(opts, BLOCK_OPT_SIZE)) { 5795f6dc1c31SKevin Wolf qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort); 5796f6dc1c31SKevin Wolf } else if (img_size != UINT64_C(-1)) { 5797f6dc1c31SKevin Wolf error_setg(errp, "The image size must be specified only once"); 5798f6dc1c31SKevin Wolf goto out; 5799f6dc1c31SKevin Wolf } 5800f6dc1c31SKevin Wolf 5801f88e1a42SJes Sorensen if (base_filename) { 5802f43e47dbSMarkus Armbruster qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &local_err); 58036be4194bSMarkus Armbruster if (local_err) { 580471c79813SLuiz Capitulino error_setg(errp, "Backing file not supported for file format '%s'", 580571c79813SLuiz Capitulino fmt); 5806f88e1a42SJes Sorensen goto out; 5807f88e1a42SJes Sorensen } 5808f88e1a42SJes Sorensen } 5809f88e1a42SJes Sorensen 5810f88e1a42SJes Sorensen if (base_fmt) { 5811f43e47dbSMarkus Armbruster qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &local_err); 58126be4194bSMarkus Armbruster if (local_err) { 581371c79813SLuiz Capitulino error_setg(errp, "Backing file format not supported for file " 581471c79813SLuiz Capitulino "format '%s'", fmt); 5815f88e1a42SJes Sorensen goto out; 5816f88e1a42SJes Sorensen } 5817f88e1a42SJes Sorensen } 5818f88e1a42SJes Sorensen 581983d0521aSChunyan Liu backing_file = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE); 582083d0521aSChunyan Liu if (backing_file) { 582183d0521aSChunyan Liu if (!strcmp(filename, backing_file)) { 582271c79813SLuiz Capitulino error_setg(errp, "Error: Trying to create an image with the " 582371c79813SLuiz Capitulino "same filename as the backing file"); 5824792da93aSJes Sorensen goto out; 5825792da93aSJes Sorensen } 5826792da93aSJes Sorensen } 5827792da93aSJes Sorensen 582883d0521aSChunyan Liu backing_fmt = qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT); 5829f88e1a42SJes Sorensen 58306e6e55f5SJohn Snow /* The size for the image must always be specified, unless we have a backing 58316e6e55f5SJohn Snow * file and we have not been forbidden from opening it. */ 5832a8b42a1cSEric Blake size = qemu_opt_get_size(opts, BLOCK_OPT_SIZE, img_size); 58336e6e55f5SJohn Snow if (backing_file && !(flags & BDRV_O_NO_BACKING)) { 583466f6b814SMax Reitz BlockDriverState *bs; 5835645ae7d8SMax Reitz char *full_backing; 583663090dacSPaolo Bonzini int back_flags; 5837e6641719SMax Reitz QDict *backing_options = NULL; 583863090dacSPaolo Bonzini 5839645ae7d8SMax Reitz full_backing = 584029168018SMax Reitz bdrv_get_full_backing_filename_from_filename(filename, backing_file, 584129168018SMax Reitz &local_err); 584229168018SMax Reitz if (local_err) { 584329168018SMax Reitz goto out; 584429168018SMax Reitz } 5845645ae7d8SMax Reitz assert(full_backing); 584629168018SMax Reitz 584763090dacSPaolo Bonzini /* backing files always opened read-only */ 584861de4c68SKevin Wolf back_flags = flags; 5849bfd18d1eSKevin Wolf back_flags &= ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 5850f88e1a42SJes Sorensen 5851e6641719SMax Reitz backing_options = qdict_new(); 5852cc954f01SFam Zheng if (backing_fmt) { 585346f5ac20SEric Blake qdict_put_str(backing_options, "driver", backing_fmt); 5854e6641719SMax Reitz } 5855cc954f01SFam Zheng qdict_put_bool(backing_options, BDRV_OPT_FORCE_SHARE, true); 5856e6641719SMax Reitz 58575b363937SMax Reitz bs = bdrv_open(full_backing, NULL, backing_options, back_flags, 58585b363937SMax Reitz &local_err); 585929168018SMax Reitz g_free(full_backing); 58606e6e55f5SJohn Snow if (!bs && size != -1) { 58616e6e55f5SJohn Snow /* Couldn't open BS, but we have a size, so it's nonfatal */ 58626e6e55f5SJohn Snow warn_reportf_err(local_err, 58636e6e55f5SJohn Snow "Could not verify backing image. " 58646e6e55f5SJohn Snow "This may become an error in future versions.\n"); 58656e6e55f5SJohn Snow local_err = NULL; 58666e6e55f5SJohn Snow } else if (!bs) { 58676e6e55f5SJohn Snow /* Couldn't open bs, do not have size */ 58686e6e55f5SJohn Snow error_append_hint(&local_err, 58696e6e55f5SJohn Snow "Could not open backing image to determine size.\n"); 5870f88e1a42SJes Sorensen goto out; 58716e6e55f5SJohn Snow } else { 58726e6e55f5SJohn Snow if (size == -1) { 58736e6e55f5SJohn Snow /* Opened BS, have no size */ 587452bf1e72SMarkus Armbruster size = bdrv_getlength(bs); 587552bf1e72SMarkus Armbruster if (size < 0) { 587652bf1e72SMarkus Armbruster error_setg_errno(errp, -size, "Could not get size of '%s'", 587752bf1e72SMarkus Armbruster backing_file); 587852bf1e72SMarkus Armbruster bdrv_unref(bs); 587952bf1e72SMarkus Armbruster goto out; 588052bf1e72SMarkus Armbruster } 588139101f25SMarkus Armbruster qemu_opt_set_number(opts, BLOCK_OPT_SIZE, size, &error_abort); 58826e6e55f5SJohn Snow } 588366f6b814SMax Reitz bdrv_unref(bs); 58846e6e55f5SJohn Snow } 58856e6e55f5SJohn Snow } /* (backing_file && !(flags & BDRV_O_NO_BACKING)) */ 58866e6e55f5SJohn Snow 58876e6e55f5SJohn Snow if (size == -1) { 588871c79813SLuiz Capitulino error_setg(errp, "Image creation needs a size parameter"); 5889f88e1a42SJes Sorensen goto out; 5890f88e1a42SJes Sorensen } 5891f88e1a42SJes Sorensen 5892f382d43aSMiroslav Rezanina if (!quiet) { 5893f88e1a42SJes Sorensen printf("Formatting '%s', fmt=%s ", filename, fmt); 589443c5d8f8SFam Zheng qemu_opts_print(opts, " "); 5895f88e1a42SJes Sorensen puts(""); 5896f382d43aSMiroslav Rezanina } 589783d0521aSChunyan Liu 5898c282e1fdSChunyan Liu ret = bdrv_create(drv, filename, opts, &local_err); 589983d0521aSChunyan Liu 5900cc84d90fSMax Reitz if (ret == -EFBIG) { 5901cc84d90fSMax Reitz /* This is generally a better message than whatever the driver would 5902cc84d90fSMax Reitz * deliver (especially because of the cluster_size_hint), since that 5903cc84d90fSMax Reitz * is most probably not much different from "image too large". */ 5904f3f4d2c0SKevin Wolf const char *cluster_size_hint = ""; 590583d0521aSChunyan Liu if (qemu_opt_get_size(opts, BLOCK_OPT_CLUSTER_SIZE, 0)) { 5906f3f4d2c0SKevin Wolf cluster_size_hint = " (try using a larger cluster size)"; 5907f3f4d2c0SKevin Wolf } 5908cc84d90fSMax Reitz error_setg(errp, "The image size is too large for file format '%s'" 5909cc84d90fSMax Reitz "%s", fmt, cluster_size_hint); 5910cc84d90fSMax Reitz error_free(local_err); 5911cc84d90fSMax Reitz local_err = NULL; 5912f88e1a42SJes Sorensen } 5913f88e1a42SJes Sorensen 5914f88e1a42SJes Sorensen out: 591583d0521aSChunyan Liu qemu_opts_del(opts); 591683d0521aSChunyan Liu qemu_opts_free(create_opts); 5917cc84d90fSMax Reitz error_propagate(errp, local_err); 5918cc84d90fSMax Reitz } 591985d126f3SStefan Hajnoczi 592085d126f3SStefan Hajnoczi AioContext *bdrv_get_aio_context(BlockDriverState *bs) 592185d126f3SStefan Hajnoczi { 592233f2a757SStefan Hajnoczi return bs ? bs->aio_context : qemu_get_aio_context(); 5923dcd04228SStefan Hajnoczi } 5924dcd04228SStefan Hajnoczi 5925052a7572SFam Zheng void bdrv_coroutine_enter(BlockDriverState *bs, Coroutine *co) 5926052a7572SFam Zheng { 5927052a7572SFam Zheng aio_co_enter(bdrv_get_aio_context(bs), co); 5928052a7572SFam Zheng } 5929052a7572SFam Zheng 5930e8a095daSStefan Hajnoczi static void bdrv_do_remove_aio_context_notifier(BdrvAioNotifier *ban) 5931e8a095daSStefan Hajnoczi { 5932e8a095daSStefan Hajnoczi QLIST_REMOVE(ban, list); 5933e8a095daSStefan Hajnoczi g_free(ban); 5934e8a095daSStefan Hajnoczi } 5935e8a095daSStefan Hajnoczi 5936a3a683c3SKevin Wolf static void bdrv_detach_aio_context(BlockDriverState *bs) 5937dcd04228SStefan Hajnoczi { 5938e8a095daSStefan Hajnoczi BdrvAioNotifier *baf, *baf_tmp; 593933384421SMax Reitz 5940e8a095daSStefan Hajnoczi assert(!bs->walking_aio_notifiers); 5941e8a095daSStefan Hajnoczi bs->walking_aio_notifiers = true; 5942e8a095daSStefan Hajnoczi QLIST_FOREACH_SAFE(baf, &bs->aio_notifiers, list, baf_tmp) { 5943e8a095daSStefan Hajnoczi if (baf->deleted) { 5944e8a095daSStefan Hajnoczi bdrv_do_remove_aio_context_notifier(baf); 5945e8a095daSStefan Hajnoczi } else { 594633384421SMax Reitz baf->detach_aio_context(baf->opaque); 594733384421SMax Reitz } 5948e8a095daSStefan Hajnoczi } 5949e8a095daSStefan Hajnoczi /* Never mind iterating again to check for ->deleted. bdrv_close() will 5950e8a095daSStefan Hajnoczi * remove remaining aio notifiers if we aren't called again. 5951e8a095daSStefan Hajnoczi */ 5952e8a095daSStefan Hajnoczi bs->walking_aio_notifiers = false; 595333384421SMax Reitz 59541bffe1aeSKevin Wolf if (bs->drv && bs->drv->bdrv_detach_aio_context) { 5955dcd04228SStefan Hajnoczi bs->drv->bdrv_detach_aio_context(bs); 5956dcd04228SStefan Hajnoczi } 5957dcd04228SStefan Hajnoczi 5958e64f25f3SKevin Wolf if (bs->quiesce_counter) { 5959e64f25f3SKevin Wolf aio_enable_external(bs->aio_context); 5960e64f25f3SKevin Wolf } 5961dcd04228SStefan Hajnoczi bs->aio_context = NULL; 5962dcd04228SStefan Hajnoczi } 5963dcd04228SStefan Hajnoczi 5964a3a683c3SKevin Wolf static void bdrv_attach_aio_context(BlockDriverState *bs, 5965dcd04228SStefan Hajnoczi AioContext *new_context) 5966dcd04228SStefan Hajnoczi { 5967e8a095daSStefan Hajnoczi BdrvAioNotifier *ban, *ban_tmp; 596833384421SMax Reitz 5969e64f25f3SKevin Wolf if (bs->quiesce_counter) { 5970e64f25f3SKevin Wolf aio_disable_external(new_context); 5971e64f25f3SKevin Wolf } 5972e64f25f3SKevin Wolf 5973dcd04228SStefan Hajnoczi bs->aio_context = new_context; 5974dcd04228SStefan Hajnoczi 59751bffe1aeSKevin Wolf if (bs->drv && bs->drv->bdrv_attach_aio_context) { 5976dcd04228SStefan Hajnoczi bs->drv->bdrv_attach_aio_context(bs, new_context); 5977dcd04228SStefan Hajnoczi } 597833384421SMax Reitz 5979e8a095daSStefan Hajnoczi assert(!bs->walking_aio_notifiers); 5980e8a095daSStefan Hajnoczi bs->walking_aio_notifiers = true; 5981e8a095daSStefan Hajnoczi QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_tmp) { 5982e8a095daSStefan Hajnoczi if (ban->deleted) { 5983e8a095daSStefan Hajnoczi bdrv_do_remove_aio_context_notifier(ban); 5984e8a095daSStefan Hajnoczi } else { 598533384421SMax Reitz ban->attached_aio_context(new_context, ban->opaque); 598633384421SMax Reitz } 5987dcd04228SStefan Hajnoczi } 5988e8a095daSStefan Hajnoczi bs->walking_aio_notifiers = false; 5989e8a095daSStefan Hajnoczi } 5990dcd04228SStefan Hajnoczi 599142a65f02SKevin Wolf /* 599242a65f02SKevin Wolf * Changes the AioContext used for fd handlers, timers, and BHs by this 599342a65f02SKevin Wolf * BlockDriverState and all its children and parents. 599442a65f02SKevin Wolf * 599543eaaaefSMax Reitz * Must be called from the main AioContext. 599643eaaaefSMax Reitz * 599742a65f02SKevin Wolf * The caller must own the AioContext lock for the old AioContext of bs, but it 599842a65f02SKevin Wolf * must not own the AioContext lock for new_context (unless new_context is the 599942a65f02SKevin Wolf * same as the current context of bs). 600042a65f02SKevin Wolf * 600142a65f02SKevin Wolf * @ignore will accumulate all visited BdrvChild object. The caller is 600242a65f02SKevin Wolf * responsible for freeing the list afterwards. 600342a65f02SKevin Wolf */ 600453a7d041SKevin Wolf void bdrv_set_aio_context_ignore(BlockDriverState *bs, 600553a7d041SKevin Wolf AioContext *new_context, GSList **ignore) 6006dcd04228SStefan Hajnoczi { 6007e037c09cSMax Reitz AioContext *old_context = bdrv_get_aio_context(bs); 60080d83708aSKevin Wolf BdrvChild *child; 60090d83708aSKevin Wolf 601043eaaaefSMax Reitz g_assert(qemu_get_current_aio_context() == qemu_get_aio_context()); 601143eaaaefSMax Reitz 6012e037c09cSMax Reitz if (old_context == new_context) { 601357830a49SDenis Plotnikov return; 601457830a49SDenis Plotnikov } 601557830a49SDenis Plotnikov 6016d70d5954SKevin Wolf bdrv_drained_begin(bs); 60170d83708aSKevin Wolf 60180d83708aSKevin Wolf QLIST_FOREACH(child, &bs->children, next) { 601953a7d041SKevin Wolf if (g_slist_find(*ignore, child)) { 602053a7d041SKevin Wolf continue; 602153a7d041SKevin Wolf } 602253a7d041SKevin Wolf *ignore = g_slist_prepend(*ignore, child); 602353a7d041SKevin Wolf bdrv_set_aio_context_ignore(child->bs, new_context, ignore); 602453a7d041SKevin Wolf } 602553a7d041SKevin Wolf QLIST_FOREACH(child, &bs->parents, next_parent) { 602653a7d041SKevin Wolf if (g_slist_find(*ignore, child)) { 602753a7d041SKevin Wolf continue; 602853a7d041SKevin Wolf } 602942a65f02SKevin Wolf assert(child->role->set_aio_ctx); 603053a7d041SKevin Wolf *ignore = g_slist_prepend(*ignore, child); 603153a7d041SKevin Wolf child->role->set_aio_ctx(child, new_context, ignore); 603253a7d041SKevin Wolf } 60330d83708aSKevin Wolf 6034dcd04228SStefan Hajnoczi bdrv_detach_aio_context(bs); 6035dcd04228SStefan Hajnoczi 6036e037c09cSMax Reitz /* Acquire the new context, if necessary */ 603743eaaaefSMax Reitz if (qemu_get_aio_context() != new_context) { 6038dcd04228SStefan Hajnoczi aio_context_acquire(new_context); 6039e037c09cSMax Reitz } 6040e037c09cSMax Reitz 6041dcd04228SStefan Hajnoczi bdrv_attach_aio_context(bs, new_context); 6042e037c09cSMax Reitz 6043e037c09cSMax Reitz /* 6044e037c09cSMax Reitz * If this function was recursively called from 6045e037c09cSMax Reitz * bdrv_set_aio_context_ignore(), there may be nodes in the 6046e037c09cSMax Reitz * subtree that have not yet been moved to the new AioContext. 6047e037c09cSMax Reitz * Release the old one so bdrv_drained_end() can poll them. 6048e037c09cSMax Reitz */ 604943eaaaefSMax Reitz if (qemu_get_aio_context() != old_context) { 6050e037c09cSMax Reitz aio_context_release(old_context); 6051e037c09cSMax Reitz } 6052e037c09cSMax Reitz 6053d70d5954SKevin Wolf bdrv_drained_end(bs); 6054e037c09cSMax Reitz 605543eaaaefSMax Reitz if (qemu_get_aio_context() != old_context) { 6056e037c09cSMax Reitz aio_context_acquire(old_context); 6057e037c09cSMax Reitz } 605843eaaaefSMax Reitz if (qemu_get_aio_context() != new_context) { 6059dcd04228SStefan Hajnoczi aio_context_release(new_context); 606085d126f3SStefan Hajnoczi } 6061e037c09cSMax Reitz } 6062d616b224SStefan Hajnoczi 60635d231849SKevin Wolf static bool bdrv_parent_can_set_aio_context(BdrvChild *c, AioContext *ctx, 60645d231849SKevin Wolf GSList **ignore, Error **errp) 60655d231849SKevin Wolf { 60665d231849SKevin Wolf if (g_slist_find(*ignore, c)) { 60675d231849SKevin Wolf return true; 60685d231849SKevin Wolf } 60695d231849SKevin Wolf *ignore = g_slist_prepend(*ignore, c); 60705d231849SKevin Wolf 60715d231849SKevin Wolf /* A BdrvChildRole that doesn't handle AioContext changes cannot 60725d231849SKevin Wolf * tolerate any AioContext changes */ 60735d231849SKevin Wolf if (!c->role->can_set_aio_ctx) { 60745d231849SKevin Wolf char *user = bdrv_child_user_desc(c); 60755d231849SKevin Wolf error_setg(errp, "Changing iothreads is not supported by %s", user); 60765d231849SKevin Wolf g_free(user); 60775d231849SKevin Wolf return false; 60785d231849SKevin Wolf } 60795d231849SKevin Wolf if (!c->role->can_set_aio_ctx(c, ctx, ignore, errp)) { 60805d231849SKevin Wolf assert(!errp || *errp); 60815d231849SKevin Wolf return false; 60825d231849SKevin Wolf } 60835d231849SKevin Wolf return true; 60845d231849SKevin Wolf } 60855d231849SKevin Wolf 60865d231849SKevin Wolf bool bdrv_child_can_set_aio_context(BdrvChild *c, AioContext *ctx, 60875d231849SKevin Wolf GSList **ignore, Error **errp) 60885d231849SKevin Wolf { 60895d231849SKevin Wolf if (g_slist_find(*ignore, c)) { 60905d231849SKevin Wolf return true; 60915d231849SKevin Wolf } 60925d231849SKevin Wolf *ignore = g_slist_prepend(*ignore, c); 60935d231849SKevin Wolf return bdrv_can_set_aio_context(c->bs, ctx, ignore, errp); 60945d231849SKevin Wolf } 60955d231849SKevin Wolf 60965d231849SKevin Wolf /* @ignore will accumulate all visited BdrvChild object. The caller is 60975d231849SKevin Wolf * responsible for freeing the list afterwards. */ 60985d231849SKevin Wolf bool bdrv_can_set_aio_context(BlockDriverState *bs, AioContext *ctx, 60995d231849SKevin Wolf GSList **ignore, Error **errp) 61005d231849SKevin Wolf { 61015d231849SKevin Wolf BdrvChild *c; 61025d231849SKevin Wolf 61035d231849SKevin Wolf if (bdrv_get_aio_context(bs) == ctx) { 61045d231849SKevin Wolf return true; 61055d231849SKevin Wolf } 61065d231849SKevin Wolf 61075d231849SKevin Wolf QLIST_FOREACH(c, &bs->parents, next_parent) { 61085d231849SKevin Wolf if (!bdrv_parent_can_set_aio_context(c, ctx, ignore, errp)) { 61095d231849SKevin Wolf return false; 61105d231849SKevin Wolf } 61115d231849SKevin Wolf } 61125d231849SKevin Wolf QLIST_FOREACH(c, &bs->children, next) { 61135d231849SKevin Wolf if (!bdrv_child_can_set_aio_context(c, ctx, ignore, errp)) { 61145d231849SKevin Wolf return false; 61155d231849SKevin Wolf } 61165d231849SKevin Wolf } 61175d231849SKevin Wolf 61185d231849SKevin Wolf return true; 61195d231849SKevin Wolf } 61205d231849SKevin Wolf 61215d231849SKevin Wolf int bdrv_child_try_set_aio_context(BlockDriverState *bs, AioContext *ctx, 61225d231849SKevin Wolf BdrvChild *ignore_child, Error **errp) 61235d231849SKevin Wolf { 61245d231849SKevin Wolf GSList *ignore; 61255d231849SKevin Wolf bool ret; 61265d231849SKevin Wolf 61275d231849SKevin Wolf ignore = ignore_child ? g_slist_prepend(NULL, ignore_child) : NULL; 61285d231849SKevin Wolf ret = bdrv_can_set_aio_context(bs, ctx, &ignore, errp); 61295d231849SKevin Wolf g_slist_free(ignore); 61305d231849SKevin Wolf 61315d231849SKevin Wolf if (!ret) { 61325d231849SKevin Wolf return -EPERM; 61335d231849SKevin Wolf } 61345d231849SKevin Wolf 613553a7d041SKevin Wolf ignore = ignore_child ? g_slist_prepend(NULL, ignore_child) : NULL; 613653a7d041SKevin Wolf bdrv_set_aio_context_ignore(bs, ctx, &ignore); 613753a7d041SKevin Wolf g_slist_free(ignore); 613853a7d041SKevin Wolf 61395d231849SKevin Wolf return 0; 61405d231849SKevin Wolf } 61415d231849SKevin Wolf 61425d231849SKevin Wolf int bdrv_try_set_aio_context(BlockDriverState *bs, AioContext *ctx, 61435d231849SKevin Wolf Error **errp) 61445d231849SKevin Wolf { 61455d231849SKevin Wolf return bdrv_child_try_set_aio_context(bs, ctx, NULL, errp); 61465d231849SKevin Wolf } 61475d231849SKevin Wolf 614833384421SMax Reitz void bdrv_add_aio_context_notifier(BlockDriverState *bs, 614933384421SMax Reitz void (*attached_aio_context)(AioContext *new_context, void *opaque), 615033384421SMax Reitz void (*detach_aio_context)(void *opaque), void *opaque) 615133384421SMax Reitz { 615233384421SMax Reitz BdrvAioNotifier *ban = g_new(BdrvAioNotifier, 1); 615333384421SMax Reitz *ban = (BdrvAioNotifier){ 615433384421SMax Reitz .attached_aio_context = attached_aio_context, 615533384421SMax Reitz .detach_aio_context = detach_aio_context, 615633384421SMax Reitz .opaque = opaque 615733384421SMax Reitz }; 615833384421SMax Reitz 615933384421SMax Reitz QLIST_INSERT_HEAD(&bs->aio_notifiers, ban, list); 616033384421SMax Reitz } 616133384421SMax Reitz 616233384421SMax Reitz void bdrv_remove_aio_context_notifier(BlockDriverState *bs, 616333384421SMax Reitz void (*attached_aio_context)(AioContext *, 616433384421SMax Reitz void *), 616533384421SMax Reitz void (*detach_aio_context)(void *), 616633384421SMax Reitz void *opaque) 616733384421SMax Reitz { 616833384421SMax Reitz BdrvAioNotifier *ban, *ban_next; 616933384421SMax Reitz 617033384421SMax Reitz QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) { 617133384421SMax Reitz if (ban->attached_aio_context == attached_aio_context && 617233384421SMax Reitz ban->detach_aio_context == detach_aio_context && 6173e8a095daSStefan Hajnoczi ban->opaque == opaque && 6174e8a095daSStefan Hajnoczi ban->deleted == false) 617533384421SMax Reitz { 6176e8a095daSStefan Hajnoczi if (bs->walking_aio_notifiers) { 6177e8a095daSStefan Hajnoczi ban->deleted = true; 6178e8a095daSStefan Hajnoczi } else { 6179e8a095daSStefan Hajnoczi bdrv_do_remove_aio_context_notifier(ban); 6180e8a095daSStefan Hajnoczi } 618133384421SMax Reitz return; 618233384421SMax Reitz } 618333384421SMax Reitz } 618433384421SMax Reitz 618533384421SMax Reitz abort(); 618633384421SMax Reitz } 618733384421SMax Reitz 618877485434SMax Reitz int bdrv_amend_options(BlockDriverState *bs, QemuOpts *opts, 6189d1402b50SMax Reitz BlockDriverAmendStatusCB *status_cb, void *cb_opaque, 6190d1402b50SMax Reitz Error **errp) 61916f176b48SMax Reitz { 6192d470ad42SMax Reitz if (!bs->drv) { 6193d1402b50SMax Reitz error_setg(errp, "Node is ejected"); 6194d470ad42SMax Reitz return -ENOMEDIUM; 6195d470ad42SMax Reitz } 6196c282e1fdSChunyan Liu if (!bs->drv->bdrv_amend_options) { 6197d1402b50SMax Reitz error_setg(errp, "Block driver '%s' does not support option amendment", 6198d1402b50SMax Reitz bs->drv->format_name); 61996f176b48SMax Reitz return -ENOTSUP; 62006f176b48SMax Reitz } 6201d1402b50SMax Reitz return bs->drv->bdrv_amend_options(bs, opts, status_cb, cb_opaque, errp); 62026f176b48SMax Reitz } 6203f6186f49SBenoît Canet 6204b5042a36SBenoît Canet /* This function will be called by the bdrv_recurse_is_first_non_filter method 6205b5042a36SBenoît Canet * of block filter and by bdrv_is_first_non_filter. 6206b5042a36SBenoît Canet * It is used to test if the given bs is the candidate or recurse more in the 6207b5042a36SBenoît Canet * node graph. 6208212a5a8fSBenoît Canet */ 6209212a5a8fSBenoît Canet bool bdrv_recurse_is_first_non_filter(BlockDriverState *bs, 6210212a5a8fSBenoît Canet BlockDriverState *candidate) 6211f6186f49SBenoît Canet { 6212b5042a36SBenoît Canet /* return false if basic checks fails */ 6213b5042a36SBenoît Canet if (!bs || !bs->drv) { 6214b5042a36SBenoît Canet return false; 6215b5042a36SBenoît Canet } 6216b5042a36SBenoît Canet 6217b5042a36SBenoît Canet /* the code reached a non block filter driver -> check if the bs is 6218b5042a36SBenoît Canet * the same as the candidate. It's the recursion termination condition. 6219b5042a36SBenoît Canet */ 6220b5042a36SBenoît Canet if (!bs->drv->is_filter) { 6221b5042a36SBenoît Canet return bs == candidate; 6222b5042a36SBenoît Canet } 6223b5042a36SBenoît Canet /* Down this path the driver is a block filter driver */ 6224b5042a36SBenoît Canet 6225b5042a36SBenoît Canet /* If the block filter recursion method is defined use it to recurse down 6226b5042a36SBenoît Canet * the node graph. 6227b5042a36SBenoît Canet */ 6228b5042a36SBenoît Canet if (bs->drv->bdrv_recurse_is_first_non_filter) { 6229212a5a8fSBenoît Canet return bs->drv->bdrv_recurse_is_first_non_filter(bs, candidate); 6230212a5a8fSBenoît Canet } 6231212a5a8fSBenoît Canet 6232b5042a36SBenoît Canet /* the driver is a block filter but don't allow to recurse -> return false 6233b5042a36SBenoît Canet */ 6234b5042a36SBenoît Canet return false; 6235212a5a8fSBenoît Canet } 6236212a5a8fSBenoît Canet 6237212a5a8fSBenoît Canet /* This function checks if the candidate is the first non filter bs down it's 6238212a5a8fSBenoît Canet * bs chain. Since we don't have pointers to parents it explore all bs chains 6239212a5a8fSBenoît Canet * from the top. Some filters can choose not to pass down the recursion. 6240212a5a8fSBenoît Canet */ 6241212a5a8fSBenoît Canet bool bdrv_is_first_non_filter(BlockDriverState *candidate) 6242212a5a8fSBenoît Canet { 62437c8eece4SKevin Wolf BlockDriverState *bs; 624488be7b4bSKevin Wolf BdrvNextIterator it; 6245212a5a8fSBenoît Canet 6246212a5a8fSBenoît Canet /* walk down the bs forest recursively */ 624788be7b4bSKevin Wolf for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) { 6248212a5a8fSBenoît Canet bool perm; 6249212a5a8fSBenoît Canet 6250b5042a36SBenoît Canet /* try to recurse in this top level bs */ 6251e6dc8a1fSKevin Wolf perm = bdrv_recurse_is_first_non_filter(bs, candidate); 6252212a5a8fSBenoît Canet 6253212a5a8fSBenoît Canet /* candidate is the first non filter */ 6254212a5a8fSBenoît Canet if (perm) { 62555e003f17SMax Reitz bdrv_next_cleanup(&it); 6256212a5a8fSBenoît Canet return true; 6257212a5a8fSBenoît Canet } 6258212a5a8fSBenoît Canet } 6259212a5a8fSBenoît Canet 6260212a5a8fSBenoît Canet return false; 6261f6186f49SBenoît Canet } 626209158f00SBenoît Canet 6263e12f3784SWen Congyang BlockDriverState *check_to_replace_node(BlockDriverState *parent_bs, 6264e12f3784SWen Congyang const char *node_name, Error **errp) 626509158f00SBenoît Canet { 626609158f00SBenoît Canet BlockDriverState *to_replace_bs = bdrv_find_node(node_name); 62675a7e7a0bSStefan Hajnoczi AioContext *aio_context; 62685a7e7a0bSStefan Hajnoczi 626909158f00SBenoît Canet if (!to_replace_bs) { 627009158f00SBenoît Canet error_setg(errp, "Node name '%s' not found", node_name); 627109158f00SBenoît Canet return NULL; 627209158f00SBenoît Canet } 627309158f00SBenoît Canet 62745a7e7a0bSStefan Hajnoczi aio_context = bdrv_get_aio_context(to_replace_bs); 62755a7e7a0bSStefan Hajnoczi aio_context_acquire(aio_context); 62765a7e7a0bSStefan Hajnoczi 627709158f00SBenoît Canet if (bdrv_op_is_blocked(to_replace_bs, BLOCK_OP_TYPE_REPLACE, errp)) { 62785a7e7a0bSStefan Hajnoczi to_replace_bs = NULL; 62795a7e7a0bSStefan Hajnoczi goto out; 628009158f00SBenoît Canet } 628109158f00SBenoît Canet 628209158f00SBenoît Canet /* We don't want arbitrary node of the BDS chain to be replaced only the top 628309158f00SBenoît Canet * most non filter in order to prevent data corruption. 628409158f00SBenoît Canet * Another benefit is that this tests exclude backing files which are 628509158f00SBenoît Canet * blocked by the backing blockers. 628609158f00SBenoît Canet */ 6287e12f3784SWen Congyang if (!bdrv_recurse_is_first_non_filter(parent_bs, to_replace_bs)) { 628809158f00SBenoît Canet error_setg(errp, "Only top most non filter can be replaced"); 62895a7e7a0bSStefan Hajnoczi to_replace_bs = NULL; 62905a7e7a0bSStefan Hajnoczi goto out; 629109158f00SBenoît Canet } 629209158f00SBenoît Canet 62935a7e7a0bSStefan Hajnoczi out: 62945a7e7a0bSStefan Hajnoczi aio_context_release(aio_context); 629509158f00SBenoît Canet return to_replace_bs; 629609158f00SBenoît Canet } 6297448ad91dSMing Lei 629897e2f021SMax Reitz /** 629997e2f021SMax Reitz * Iterates through the list of runtime option keys that are said to 630097e2f021SMax Reitz * be "strong" for a BDS. An option is called "strong" if it changes 630197e2f021SMax Reitz * a BDS's data. For example, the null block driver's "size" and 630297e2f021SMax Reitz * "read-zeroes" options are strong, but its "latency-ns" option is 630397e2f021SMax Reitz * not. 630497e2f021SMax Reitz * 630597e2f021SMax Reitz * If a key returned by this function ends with a dot, all options 630697e2f021SMax Reitz * starting with that prefix are strong. 630797e2f021SMax Reitz */ 630897e2f021SMax Reitz static const char *const *strong_options(BlockDriverState *bs, 630997e2f021SMax Reitz const char *const *curopt) 631097e2f021SMax Reitz { 631197e2f021SMax Reitz static const char *const global_options[] = { 631297e2f021SMax Reitz "driver", "filename", NULL 631397e2f021SMax Reitz }; 631497e2f021SMax Reitz 631597e2f021SMax Reitz if (!curopt) { 631697e2f021SMax Reitz return &global_options[0]; 631797e2f021SMax Reitz } 631897e2f021SMax Reitz 631997e2f021SMax Reitz curopt++; 632097e2f021SMax Reitz if (curopt == &global_options[ARRAY_SIZE(global_options) - 1] && bs->drv) { 632197e2f021SMax Reitz curopt = bs->drv->strong_runtime_opts; 632297e2f021SMax Reitz } 632397e2f021SMax Reitz 632497e2f021SMax Reitz return (curopt && *curopt) ? curopt : NULL; 632597e2f021SMax Reitz } 632697e2f021SMax Reitz 632797e2f021SMax Reitz /** 632897e2f021SMax Reitz * Copies all strong runtime options from bs->options to the given 632997e2f021SMax Reitz * QDict. The set of strong option keys is determined by invoking 633097e2f021SMax Reitz * strong_options(). 633197e2f021SMax Reitz * 633297e2f021SMax Reitz * Returns true iff any strong option was present in bs->options (and 633397e2f021SMax Reitz * thus copied to the target QDict) with the exception of "filename" 633497e2f021SMax Reitz * and "driver". The caller is expected to use this value to decide 633597e2f021SMax Reitz * whether the existence of strong options prevents the generation of 633697e2f021SMax Reitz * a plain filename. 633797e2f021SMax Reitz */ 633897e2f021SMax Reitz static bool append_strong_runtime_options(QDict *d, BlockDriverState *bs) 633997e2f021SMax Reitz { 634097e2f021SMax Reitz bool found_any = false; 634197e2f021SMax Reitz const char *const *option_name = NULL; 634297e2f021SMax Reitz 634397e2f021SMax Reitz if (!bs->drv) { 634497e2f021SMax Reitz return false; 634597e2f021SMax Reitz } 634697e2f021SMax Reitz 634797e2f021SMax Reitz while ((option_name = strong_options(bs, option_name))) { 634897e2f021SMax Reitz bool option_given = false; 634997e2f021SMax Reitz 635097e2f021SMax Reitz assert(strlen(*option_name) > 0); 635197e2f021SMax Reitz if ((*option_name)[strlen(*option_name) - 1] != '.') { 635297e2f021SMax Reitz QObject *entry = qdict_get(bs->options, *option_name); 635397e2f021SMax Reitz if (!entry) { 635497e2f021SMax Reitz continue; 635597e2f021SMax Reitz } 635697e2f021SMax Reitz 635797e2f021SMax Reitz qdict_put_obj(d, *option_name, qobject_ref(entry)); 635897e2f021SMax Reitz option_given = true; 635997e2f021SMax Reitz } else { 636097e2f021SMax Reitz const QDictEntry *entry; 636197e2f021SMax Reitz for (entry = qdict_first(bs->options); entry; 636297e2f021SMax Reitz entry = qdict_next(bs->options, entry)) 636397e2f021SMax Reitz { 636497e2f021SMax Reitz if (strstart(qdict_entry_key(entry), *option_name, NULL)) { 636597e2f021SMax Reitz qdict_put_obj(d, qdict_entry_key(entry), 636697e2f021SMax Reitz qobject_ref(qdict_entry_value(entry))); 636797e2f021SMax Reitz option_given = true; 636897e2f021SMax Reitz } 636997e2f021SMax Reitz } 637097e2f021SMax Reitz } 637197e2f021SMax Reitz 637297e2f021SMax Reitz /* While "driver" and "filename" need to be included in a JSON filename, 637397e2f021SMax Reitz * their existence does not prohibit generation of a plain filename. */ 637497e2f021SMax Reitz if (!found_any && option_given && 637597e2f021SMax Reitz strcmp(*option_name, "driver") && strcmp(*option_name, "filename")) 637697e2f021SMax Reitz { 637797e2f021SMax Reitz found_any = true; 637897e2f021SMax Reitz } 637997e2f021SMax Reitz } 638097e2f021SMax Reitz 638162a01a27SMax Reitz if (!qdict_haskey(d, "driver")) { 638262a01a27SMax Reitz /* Drivers created with bdrv_new_open_driver() may not have a 638362a01a27SMax Reitz * @driver option. Add it here. */ 638462a01a27SMax Reitz qdict_put_str(d, "driver", bs->drv->format_name); 638562a01a27SMax Reitz } 638662a01a27SMax Reitz 638797e2f021SMax Reitz return found_any; 638897e2f021SMax Reitz } 638997e2f021SMax Reitz 639090993623SMax Reitz /* Note: This function may return false positives; it may return true 639190993623SMax Reitz * even if opening the backing file specified by bs's image header 639290993623SMax Reitz * would result in exactly bs->backing. */ 639390993623SMax Reitz static bool bdrv_backing_overridden(BlockDriverState *bs) 639490993623SMax Reitz { 639590993623SMax Reitz if (bs->backing) { 639690993623SMax Reitz return strcmp(bs->auto_backing_file, 639790993623SMax Reitz bs->backing->bs->filename); 639890993623SMax Reitz } else { 639990993623SMax Reitz /* No backing BDS, so if the image header reports any backing 640090993623SMax Reitz * file, it must have been suppressed */ 640190993623SMax Reitz return bs->auto_backing_file[0] != '\0'; 640290993623SMax Reitz } 640390993623SMax Reitz } 640490993623SMax Reitz 640591af7014SMax Reitz /* Updates the following BDS fields: 640691af7014SMax Reitz * - exact_filename: A filename which may be used for opening a block device 640791af7014SMax Reitz * which (mostly) equals the given BDS (even without any 640891af7014SMax Reitz * other options; so reading and writing must return the same 640991af7014SMax Reitz * results, but caching etc. may be different) 641091af7014SMax Reitz * - full_open_options: Options which, when given when opening a block device 641191af7014SMax Reitz * (without a filename), result in a BDS (mostly) 641291af7014SMax Reitz * equalling the given one 641391af7014SMax Reitz * - filename: If exact_filename is set, it is copied here. Otherwise, 641491af7014SMax Reitz * full_open_options is converted to a JSON object, prefixed with 641591af7014SMax Reitz * "json:" (for use through the JSON pseudo protocol) and put here. 641691af7014SMax Reitz */ 641791af7014SMax Reitz void bdrv_refresh_filename(BlockDriverState *bs) 641891af7014SMax Reitz { 641991af7014SMax Reitz BlockDriver *drv = bs->drv; 6420e24518e3SMax Reitz BdrvChild *child; 642191af7014SMax Reitz QDict *opts; 642290993623SMax Reitz bool backing_overridden; 6423998b3a1eSMax Reitz bool generate_json_filename; /* Whether our default implementation should 6424998b3a1eSMax Reitz fill exact_filename (false) or not (true) */ 642591af7014SMax Reitz 642691af7014SMax Reitz if (!drv) { 642791af7014SMax Reitz return; 642891af7014SMax Reitz } 642991af7014SMax Reitz 6430e24518e3SMax Reitz /* This BDS's file name may depend on any of its children's file names, so 6431e24518e3SMax Reitz * refresh those first */ 6432e24518e3SMax Reitz QLIST_FOREACH(child, &bs->children, next) { 6433e24518e3SMax Reitz bdrv_refresh_filename(child->bs); 643491af7014SMax Reitz } 643591af7014SMax Reitz 6436bb808d5fSMax Reitz if (bs->implicit) { 6437bb808d5fSMax Reitz /* For implicit nodes, just copy everything from the single child */ 6438bb808d5fSMax Reitz child = QLIST_FIRST(&bs->children); 6439bb808d5fSMax Reitz assert(QLIST_NEXT(child, next) == NULL); 6440bb808d5fSMax Reitz 6441bb808d5fSMax Reitz pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), 6442bb808d5fSMax Reitz child->bs->exact_filename); 6443bb808d5fSMax Reitz pstrcpy(bs->filename, sizeof(bs->filename), child->bs->filename); 6444bb808d5fSMax Reitz 6445bb808d5fSMax Reitz bs->full_open_options = qobject_ref(child->bs->full_open_options); 6446bb808d5fSMax Reitz 6447bb808d5fSMax Reitz return; 6448bb808d5fSMax Reitz } 6449bb808d5fSMax Reitz 645090993623SMax Reitz backing_overridden = bdrv_backing_overridden(bs); 645190993623SMax Reitz 645290993623SMax Reitz if (bs->open_flags & BDRV_O_NO_IO) { 645390993623SMax Reitz /* Without I/O, the backing file does not change anything. 645490993623SMax Reitz * Therefore, in such a case (primarily qemu-img), we can 645590993623SMax Reitz * pretend the backing file has not been overridden even if 645690993623SMax Reitz * it technically has been. */ 645790993623SMax Reitz backing_overridden = false; 645890993623SMax Reitz } 645990993623SMax Reitz 646097e2f021SMax Reitz /* Gather the options QDict */ 646197e2f021SMax Reitz opts = qdict_new(); 6462998b3a1eSMax Reitz generate_json_filename = append_strong_runtime_options(opts, bs); 6463998b3a1eSMax Reitz generate_json_filename |= backing_overridden; 646497e2f021SMax Reitz 646597e2f021SMax Reitz if (drv->bdrv_gather_child_options) { 646697e2f021SMax Reitz /* Some block drivers may not want to present all of their children's 646797e2f021SMax Reitz * options, or name them differently from BdrvChild.name */ 646897e2f021SMax Reitz drv->bdrv_gather_child_options(bs, opts, backing_overridden); 646997e2f021SMax Reitz } else { 647097e2f021SMax Reitz QLIST_FOREACH(child, &bs->children, next) { 647197e2f021SMax Reitz if (child->role == &child_backing && !backing_overridden) { 647297e2f021SMax Reitz /* We can skip the backing BDS if it has not been overridden */ 647397e2f021SMax Reitz continue; 647497e2f021SMax Reitz } 647597e2f021SMax Reitz 647697e2f021SMax Reitz qdict_put(opts, child->name, 647797e2f021SMax Reitz qobject_ref(child->bs->full_open_options)); 647897e2f021SMax Reitz } 647997e2f021SMax Reitz 648097e2f021SMax Reitz if (backing_overridden && !bs->backing) { 648197e2f021SMax Reitz /* Force no backing file */ 648297e2f021SMax Reitz qdict_put_null(opts, "backing"); 648397e2f021SMax Reitz } 648497e2f021SMax Reitz } 648597e2f021SMax Reitz 648697e2f021SMax Reitz qobject_unref(bs->full_open_options); 648797e2f021SMax Reitz bs->full_open_options = opts; 648897e2f021SMax Reitz 6489998b3a1eSMax Reitz if (drv->bdrv_refresh_filename) { 6490998b3a1eSMax Reitz /* Obsolete information is of no use here, so drop the old file name 6491998b3a1eSMax Reitz * information before refreshing it */ 6492998b3a1eSMax Reitz bs->exact_filename[0] = '\0'; 6493998b3a1eSMax Reitz 6494998b3a1eSMax Reitz drv->bdrv_refresh_filename(bs); 6495998b3a1eSMax Reitz } else if (bs->file) { 6496998b3a1eSMax Reitz /* Try to reconstruct valid information from the underlying file */ 6497998b3a1eSMax Reitz 6498998b3a1eSMax Reitz bs->exact_filename[0] = '\0'; 6499998b3a1eSMax Reitz 6500fb695c74SMax Reitz /* 6501fb695c74SMax Reitz * We can use the underlying file's filename if: 6502fb695c74SMax Reitz * - it has a filename, 6503fb695c74SMax Reitz * - the file is a protocol BDS, and 6504fb695c74SMax Reitz * - opening that file (as this BDS's format) will automatically create 6505fb695c74SMax Reitz * the BDS tree we have right now, that is: 6506fb695c74SMax Reitz * - the user did not significantly change this BDS's behavior with 6507fb695c74SMax Reitz * some explicit (strong) options 6508fb695c74SMax Reitz * - no non-file child of this BDS has been overridden by the user 6509fb695c74SMax Reitz * Both of these conditions are represented by generate_json_filename. 6510fb695c74SMax Reitz */ 6511fb695c74SMax Reitz if (bs->file->bs->exact_filename[0] && 6512fb695c74SMax Reitz bs->file->bs->drv->bdrv_file_open && 6513fb695c74SMax Reitz !generate_json_filename) 6514fb695c74SMax Reitz { 6515998b3a1eSMax Reitz strcpy(bs->exact_filename, bs->file->bs->exact_filename); 6516998b3a1eSMax Reitz } 6517998b3a1eSMax Reitz } 6518998b3a1eSMax Reitz 651991af7014SMax Reitz if (bs->exact_filename[0]) { 652091af7014SMax Reitz pstrcpy(bs->filename, sizeof(bs->filename), bs->exact_filename); 652197e2f021SMax Reitz } else { 652291af7014SMax Reitz QString *json = qobject_to_json(QOBJECT(bs->full_open_options)); 652391af7014SMax Reitz snprintf(bs->filename, sizeof(bs->filename), "json:%s", 652491af7014SMax Reitz qstring_get_str(json)); 6525cb3e7f08SMarc-André Lureau qobject_unref(json); 652691af7014SMax Reitz } 652791af7014SMax Reitz } 6528e06018adSWen Congyang 65291e89d0f9SMax Reitz char *bdrv_dirname(BlockDriverState *bs, Error **errp) 65301e89d0f9SMax Reitz { 65311e89d0f9SMax Reitz BlockDriver *drv = bs->drv; 65321e89d0f9SMax Reitz 65331e89d0f9SMax Reitz if (!drv) { 65341e89d0f9SMax Reitz error_setg(errp, "Node '%s' is ejected", bs->node_name); 65351e89d0f9SMax Reitz return NULL; 65361e89d0f9SMax Reitz } 65371e89d0f9SMax Reitz 65381e89d0f9SMax Reitz if (drv->bdrv_dirname) { 65391e89d0f9SMax Reitz return drv->bdrv_dirname(bs, errp); 65401e89d0f9SMax Reitz } 65411e89d0f9SMax Reitz 65421e89d0f9SMax Reitz if (bs->file) { 65431e89d0f9SMax Reitz return bdrv_dirname(bs->file->bs, errp); 65441e89d0f9SMax Reitz } 65451e89d0f9SMax Reitz 65461e89d0f9SMax Reitz bdrv_refresh_filename(bs); 65471e89d0f9SMax Reitz if (bs->exact_filename[0] != '\0') { 65481e89d0f9SMax Reitz return path_combine(bs->exact_filename, ""); 65491e89d0f9SMax Reitz } 65501e89d0f9SMax Reitz 65511e89d0f9SMax Reitz error_setg(errp, "Cannot generate a base directory for %s nodes", 65521e89d0f9SMax Reitz drv->format_name); 65531e89d0f9SMax Reitz return NULL; 65541e89d0f9SMax Reitz } 65551e89d0f9SMax Reitz 6556e06018adSWen Congyang /* 6557e06018adSWen Congyang * Hot add/remove a BDS's child. So the user can take a child offline when 6558e06018adSWen Congyang * it is broken and take a new child online 6559e06018adSWen Congyang */ 6560e06018adSWen Congyang void bdrv_add_child(BlockDriverState *parent_bs, BlockDriverState *child_bs, 6561e06018adSWen Congyang Error **errp) 6562e06018adSWen Congyang { 6563e06018adSWen Congyang 6564e06018adSWen Congyang if (!parent_bs->drv || !parent_bs->drv->bdrv_add_child) { 6565e06018adSWen Congyang error_setg(errp, "The node %s does not support adding a child", 6566e06018adSWen Congyang bdrv_get_device_or_node_name(parent_bs)); 6567e06018adSWen Congyang return; 6568e06018adSWen Congyang } 6569e06018adSWen Congyang 6570e06018adSWen Congyang if (!QLIST_EMPTY(&child_bs->parents)) { 6571e06018adSWen Congyang error_setg(errp, "The node %s already has a parent", 6572e06018adSWen Congyang child_bs->node_name); 6573e06018adSWen Congyang return; 6574e06018adSWen Congyang } 6575e06018adSWen Congyang 6576e06018adSWen Congyang parent_bs->drv->bdrv_add_child(parent_bs, child_bs, errp); 6577e06018adSWen Congyang } 6578e06018adSWen Congyang 6579e06018adSWen Congyang void bdrv_del_child(BlockDriverState *parent_bs, BdrvChild *child, Error **errp) 6580e06018adSWen Congyang { 6581e06018adSWen Congyang BdrvChild *tmp; 6582e06018adSWen Congyang 6583e06018adSWen Congyang if (!parent_bs->drv || !parent_bs->drv->bdrv_del_child) { 6584e06018adSWen Congyang error_setg(errp, "The node %s does not support removing a child", 6585e06018adSWen Congyang bdrv_get_device_or_node_name(parent_bs)); 6586e06018adSWen Congyang return; 6587e06018adSWen Congyang } 6588e06018adSWen Congyang 6589e06018adSWen Congyang QLIST_FOREACH(tmp, &parent_bs->children, next) { 6590e06018adSWen Congyang if (tmp == child) { 6591e06018adSWen Congyang break; 6592e06018adSWen Congyang } 6593e06018adSWen Congyang } 6594e06018adSWen Congyang 6595e06018adSWen Congyang if (!tmp) { 6596e06018adSWen Congyang error_setg(errp, "The node %s does not have a child named %s", 6597e06018adSWen Congyang bdrv_get_device_or_node_name(parent_bs), 6598e06018adSWen Congyang bdrv_get_device_or_node_name(child->bs)); 6599e06018adSWen Congyang return; 6600e06018adSWen Congyang } 6601e06018adSWen Congyang 6602e06018adSWen Congyang parent_bs->drv->bdrv_del_child(parent_bs, child, errp); 6603e06018adSWen Congyang } 6604