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 */ 243990d09aSblueswir1 #include "config-host.h" 25faf07963Spbrook #include "qemu-common.h" 266d519a5fSStefan Hajnoczi #include "trace.h" 27737e150eSPaolo Bonzini #include "block/block_int.h" 28737e150eSPaolo Bonzini #include "block/blockjob.h" 291de7afc9SPaolo Bonzini #include "qemu/module.h" 307b1b5d19SPaolo Bonzini #include "qapi/qmp/qjson.h" 31bfb197e0SMarkus Armbruster #include "sysemu/block-backend.h" 329c17d615SPaolo Bonzini #include "sysemu/sysemu.h" 331de7afc9SPaolo Bonzini #include "qemu/notify.h" 34737e150eSPaolo Bonzini #include "block/coroutine.h" 35c13163fbSBenoît Canet #include "block/qapi.h" 36b2023818SLuiz Capitulino #include "qmp-commands.h" 371de7afc9SPaolo Bonzini #include "qemu/timer.h" 38a5ee7bd4SWenchao Xia #include "qapi-event.h" 39*db628338SAlberto Garcia #include "block/throttle-groups.h" 40fc01f7e7Sbellard 4171e72a19SJuan Quintela #ifdef CONFIG_BSD 427674e7bfSbellard #include <sys/types.h> 437674e7bfSbellard #include <sys/stat.h> 447674e7bfSbellard #include <sys/ioctl.h> 4572cf2d4fSBlue Swirl #include <sys/queue.h> 46c5e97233Sblueswir1 #ifndef __DragonFly__ 477674e7bfSbellard #include <sys/disk.h> 487674e7bfSbellard #endif 49c5e97233Sblueswir1 #endif 507674e7bfSbellard 5149dc768dSaliguori #ifdef _WIN32 5249dc768dSaliguori #include <windows.h> 5349dc768dSaliguori #endif 5449dc768dSaliguori 559bd2b08fSJohn Snow /** 569bd2b08fSJohn Snow * A BdrvDirtyBitmap can be in three possible states: 579bd2b08fSJohn Snow * (1) successor is NULL and disabled is false: full r/w mode 589bd2b08fSJohn Snow * (2) successor is NULL and disabled is true: read only mode ("disabled") 599bd2b08fSJohn Snow * (3) successor is set: frozen mode. 609bd2b08fSJohn Snow * A frozen bitmap cannot be renamed, deleted, anonymized, cleared, set, 619bd2b08fSJohn Snow * or enabled. A frozen bitmap can only abdicate() or reclaim(). 629bd2b08fSJohn Snow */ 63e4654d2dSFam Zheng struct BdrvDirtyBitmap { 64aa0c7ca5SJohn Snow HBitmap *bitmap; /* Dirty sector bitmap implementation */ 65aa0c7ca5SJohn Snow BdrvDirtyBitmap *successor; /* Anonymous child; implies frozen status */ 66aa0c7ca5SJohn Snow char *name; /* Optional non-empty unique ID */ 67aa0c7ca5SJohn Snow int64_t size; /* Size of the bitmap (Number of sectors) */ 68aa0c7ca5SJohn Snow bool disabled; /* Bitmap is read-only */ 69e4654d2dSFam Zheng QLIST_ENTRY(BdrvDirtyBitmap) list; 70e4654d2dSFam Zheng }; 71e4654d2dSFam Zheng 721c9805a3SStefan Hajnoczi #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */ 731c9805a3SStefan Hajnoczi 741b7bdbc1SStefan Hajnoczi static QTAILQ_HEAD(, BlockDriverState) bdrv_states = 751b7bdbc1SStefan Hajnoczi QTAILQ_HEAD_INITIALIZER(bdrv_states); 767ee930d0Sblueswir1 77dc364f4cSBenoît Canet static QTAILQ_HEAD(, BlockDriverState) graph_bdrv_states = 78dc364f4cSBenoît Canet QTAILQ_HEAD_INITIALIZER(graph_bdrv_states); 79dc364f4cSBenoît Canet 808a22f02aSStefan Hajnoczi static QLIST_HEAD(, BlockDriver) bdrv_drivers = 818a22f02aSStefan Hajnoczi QLIST_HEAD_INITIALIZER(bdrv_drivers); 82ea2384d3Sbellard 83ce1ffea8SJohn Snow static void bdrv_dirty_bitmap_truncate(BlockDriverState *bs); 84eb852011SMarkus Armbruster /* If non-zero, use only whitelisted block drivers */ 85eb852011SMarkus Armbruster static int use_bdrv_whitelist; 86eb852011SMarkus Armbruster 879e0b22f4SStefan Hajnoczi #ifdef _WIN32 889e0b22f4SStefan Hajnoczi static int is_windows_drive_prefix(const char *filename) 899e0b22f4SStefan Hajnoczi { 909e0b22f4SStefan Hajnoczi return (((filename[0] >= 'a' && filename[0] <= 'z') || 919e0b22f4SStefan Hajnoczi (filename[0] >= 'A' && filename[0] <= 'Z')) && 929e0b22f4SStefan Hajnoczi filename[1] == ':'); 939e0b22f4SStefan Hajnoczi } 949e0b22f4SStefan Hajnoczi 959e0b22f4SStefan Hajnoczi int is_windows_drive(const char *filename) 969e0b22f4SStefan Hajnoczi { 979e0b22f4SStefan Hajnoczi if (is_windows_drive_prefix(filename) && 989e0b22f4SStefan Hajnoczi filename[2] == '\0') 999e0b22f4SStefan Hajnoczi return 1; 1009e0b22f4SStefan Hajnoczi if (strstart(filename, "\\\\.\\", NULL) || 1019e0b22f4SStefan Hajnoczi strstart(filename, "//./", NULL)) 1029e0b22f4SStefan Hajnoczi return 1; 1039e0b22f4SStefan Hajnoczi return 0; 1049e0b22f4SStefan Hajnoczi } 1059e0b22f4SStefan Hajnoczi #endif 1069e0b22f4SStefan Hajnoczi 107339064d5SKevin Wolf size_t bdrv_opt_mem_align(BlockDriverState *bs) 108339064d5SKevin Wolf { 109339064d5SKevin Wolf if (!bs || !bs->drv) { 110459b4e66SDenis V. Lunev /* page size or 4k (hdd sector size) should be on the safe side */ 111459b4e66SDenis V. Lunev return MAX(4096, getpagesize()); 112339064d5SKevin Wolf } 113339064d5SKevin Wolf 114339064d5SKevin Wolf return bs->bl.opt_mem_alignment; 115339064d5SKevin Wolf } 116339064d5SKevin Wolf 1174196d2f0SDenis V. Lunev size_t bdrv_min_mem_align(BlockDriverState *bs) 1184196d2f0SDenis V. Lunev { 1194196d2f0SDenis V. Lunev if (!bs || !bs->drv) { 120459b4e66SDenis V. Lunev /* page size or 4k (hdd sector size) should be on the safe side */ 121459b4e66SDenis V. Lunev return MAX(4096, getpagesize()); 1224196d2f0SDenis V. Lunev } 1234196d2f0SDenis V. Lunev 1244196d2f0SDenis V. Lunev return bs->bl.min_mem_alignment; 1254196d2f0SDenis V. Lunev } 1264196d2f0SDenis V. Lunev 1279e0b22f4SStefan Hajnoczi /* check if the path starts with "<protocol>:" */ 1285c98415bSMax Reitz int path_has_protocol(const char *path) 1299e0b22f4SStefan Hajnoczi { 130947995c0SPaolo Bonzini const char *p; 131947995c0SPaolo Bonzini 1329e0b22f4SStefan Hajnoczi #ifdef _WIN32 1339e0b22f4SStefan Hajnoczi if (is_windows_drive(path) || 1349e0b22f4SStefan Hajnoczi is_windows_drive_prefix(path)) { 1359e0b22f4SStefan Hajnoczi return 0; 1369e0b22f4SStefan Hajnoczi } 137947995c0SPaolo Bonzini p = path + strcspn(path, ":/\\"); 138947995c0SPaolo Bonzini #else 139947995c0SPaolo Bonzini p = path + strcspn(path, ":/"); 1409e0b22f4SStefan Hajnoczi #endif 1419e0b22f4SStefan Hajnoczi 142947995c0SPaolo Bonzini return *p == ':'; 1439e0b22f4SStefan Hajnoczi } 1449e0b22f4SStefan Hajnoczi 14583f64091Sbellard int path_is_absolute(const char *path) 14683f64091Sbellard { 14721664424Sbellard #ifdef _WIN32 14821664424Sbellard /* specific case for names like: "\\.\d:" */ 149f53f4da9SPaolo Bonzini if (is_windows_drive(path) || is_windows_drive_prefix(path)) { 15021664424Sbellard return 1; 151f53f4da9SPaolo Bonzini } 152f53f4da9SPaolo Bonzini return (*path == '/' || *path == '\\'); 1533b9f94e1Sbellard #else 154f53f4da9SPaolo Bonzini return (*path == '/'); 1553b9f94e1Sbellard #endif 15683f64091Sbellard } 15783f64091Sbellard 15883f64091Sbellard /* if filename is absolute, just copy it to dest. Otherwise, build a 15983f64091Sbellard path to it by considering it is relative to base_path. URL are 16083f64091Sbellard supported. */ 16183f64091Sbellard void path_combine(char *dest, int dest_size, 16283f64091Sbellard const char *base_path, 16383f64091Sbellard const char *filename) 16483f64091Sbellard { 16583f64091Sbellard const char *p, *p1; 16683f64091Sbellard int len; 16783f64091Sbellard 16883f64091Sbellard if (dest_size <= 0) 16983f64091Sbellard return; 17083f64091Sbellard if (path_is_absolute(filename)) { 17183f64091Sbellard pstrcpy(dest, dest_size, filename); 17283f64091Sbellard } else { 17383f64091Sbellard p = strchr(base_path, ':'); 17483f64091Sbellard if (p) 17583f64091Sbellard p++; 17683f64091Sbellard else 17783f64091Sbellard p = base_path; 1783b9f94e1Sbellard p1 = strrchr(base_path, '/'); 1793b9f94e1Sbellard #ifdef _WIN32 1803b9f94e1Sbellard { 1813b9f94e1Sbellard const char *p2; 1823b9f94e1Sbellard p2 = strrchr(base_path, '\\'); 1833b9f94e1Sbellard if (!p1 || p2 > p1) 1843b9f94e1Sbellard p1 = p2; 1853b9f94e1Sbellard } 1863b9f94e1Sbellard #endif 18783f64091Sbellard if (p1) 18883f64091Sbellard p1++; 18983f64091Sbellard else 19083f64091Sbellard p1 = base_path; 19183f64091Sbellard if (p1 > p) 19283f64091Sbellard p = p1; 19383f64091Sbellard len = p - base_path; 19483f64091Sbellard if (len > dest_size - 1) 19583f64091Sbellard len = dest_size - 1; 19683f64091Sbellard memcpy(dest, base_path, len); 19783f64091Sbellard dest[len] = '\0'; 19883f64091Sbellard pstrcat(dest, dest_size, filename); 19983f64091Sbellard } 20083f64091Sbellard } 20183f64091Sbellard 2020a82855aSMax Reitz void bdrv_get_full_backing_filename_from_filename(const char *backed, 2030a82855aSMax Reitz const char *backing, 2049f07429eSMax Reitz char *dest, size_t sz, 2059f07429eSMax Reitz Error **errp) 2060a82855aSMax Reitz { 2079f07429eSMax Reitz if (backing[0] == '\0' || path_has_protocol(backing) || 2089f07429eSMax Reitz path_is_absolute(backing)) 2099f07429eSMax Reitz { 2100a82855aSMax Reitz pstrcpy(dest, sz, backing); 2119f07429eSMax Reitz } else if (backed[0] == '\0' || strstart(backed, "json:", NULL)) { 2129f07429eSMax Reitz error_setg(errp, "Cannot use relative backing file names for '%s'", 2139f07429eSMax Reitz backed); 2140a82855aSMax Reitz } else { 2150a82855aSMax Reitz path_combine(dest, sz, backed, backing); 2160a82855aSMax Reitz } 2170a82855aSMax Reitz } 2180a82855aSMax Reitz 2199f07429eSMax Reitz void bdrv_get_full_backing_filename(BlockDriverState *bs, char *dest, size_t sz, 2209f07429eSMax Reitz Error **errp) 221dc5a1371SPaolo Bonzini { 2229f07429eSMax Reitz char *backed = bs->exact_filename[0] ? bs->exact_filename : bs->filename; 2239f07429eSMax Reitz 2249f07429eSMax Reitz bdrv_get_full_backing_filename_from_filename(backed, bs->backing_file, 2259f07429eSMax Reitz dest, sz, errp); 226dc5a1371SPaolo Bonzini } 227dc5a1371SPaolo Bonzini 2280eb7217eSStefan Hajnoczi void bdrv_register(BlockDriver *bdrv) 2290eb7217eSStefan Hajnoczi { 2300eb7217eSStefan Hajnoczi bdrv_setup_io_funcs(bdrv); 231b2e12bc6SChristoph Hellwig 2328a22f02aSStefan Hajnoczi QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list); 233ea2384d3Sbellard } 234b338082bSbellard 2357f06d47eSMarkus Armbruster BlockDriverState *bdrv_new_root(void) 236fc01f7e7Sbellard { 2377f06d47eSMarkus Armbruster BlockDriverState *bs = bdrv_new(); 238e4e9986bSMarkus Armbruster 239e4e9986bSMarkus Armbruster QTAILQ_INSERT_TAIL(&bdrv_states, bs, device_list); 240e4e9986bSMarkus Armbruster return bs; 241e4e9986bSMarkus Armbruster } 242e4e9986bSMarkus Armbruster 243e4e9986bSMarkus Armbruster BlockDriverState *bdrv_new(void) 244e4e9986bSMarkus Armbruster { 245e4e9986bSMarkus Armbruster BlockDriverState *bs; 246e4e9986bSMarkus Armbruster int i; 247e4e9986bSMarkus Armbruster 2485839e53bSMarkus Armbruster bs = g_new0(BlockDriverState, 1); 249e4654d2dSFam Zheng QLIST_INIT(&bs->dirty_bitmaps); 250fbe40ff7SFam Zheng for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) { 251fbe40ff7SFam Zheng QLIST_INIT(&bs->op_blockers[i]); 252fbe40ff7SFam Zheng } 25328a7282aSLuiz Capitulino bdrv_iostatus_disable(bs); 254d7d512f6SPaolo Bonzini notifier_list_init(&bs->close_notifiers); 255d616b224SStefan Hajnoczi notifier_with_return_list_init(&bs->before_write_notifiers); 256cc0681c4SBenoît Canet qemu_co_queue_init(&bs->throttled_reqs[0]); 257cc0681c4SBenoît Canet qemu_co_queue_init(&bs->throttled_reqs[1]); 2589fcb0251SFam Zheng bs->refcnt = 1; 259dcd04228SStefan Hajnoczi bs->aio_context = qemu_get_aio_context(); 260d7d512f6SPaolo Bonzini 261b338082bSbellard return bs; 262b338082bSbellard } 263b338082bSbellard 264d7d512f6SPaolo Bonzini void bdrv_add_close_notifier(BlockDriverState *bs, Notifier *notify) 265d7d512f6SPaolo Bonzini { 266d7d512f6SPaolo Bonzini notifier_list_add(&bs->close_notifiers, notify); 267d7d512f6SPaolo Bonzini } 268d7d512f6SPaolo Bonzini 269ea2384d3Sbellard BlockDriver *bdrv_find_format(const char *format_name) 270ea2384d3Sbellard { 271ea2384d3Sbellard BlockDriver *drv1; 2728a22f02aSStefan Hajnoczi QLIST_FOREACH(drv1, &bdrv_drivers, list) { 2738a22f02aSStefan Hajnoczi if (!strcmp(drv1->format_name, format_name)) { 274ea2384d3Sbellard return drv1; 275ea2384d3Sbellard } 2768a22f02aSStefan Hajnoczi } 277ea2384d3Sbellard return NULL; 278ea2384d3Sbellard } 279ea2384d3Sbellard 280b64ec4e4SFam Zheng static int bdrv_is_whitelisted(BlockDriver *drv, bool read_only) 281eb852011SMarkus Armbruster { 282b64ec4e4SFam Zheng static const char *whitelist_rw[] = { 283b64ec4e4SFam Zheng CONFIG_BDRV_RW_WHITELIST 284b64ec4e4SFam Zheng }; 285b64ec4e4SFam Zheng static const char *whitelist_ro[] = { 286b64ec4e4SFam Zheng CONFIG_BDRV_RO_WHITELIST 287eb852011SMarkus Armbruster }; 288eb852011SMarkus Armbruster const char **p; 289eb852011SMarkus Armbruster 290b64ec4e4SFam Zheng if (!whitelist_rw[0] && !whitelist_ro[0]) { 291eb852011SMarkus Armbruster return 1; /* no whitelist, anything goes */ 292b64ec4e4SFam Zheng } 293eb852011SMarkus Armbruster 294b64ec4e4SFam Zheng for (p = whitelist_rw; *p; p++) { 295eb852011SMarkus Armbruster if (!strcmp(drv->format_name, *p)) { 296eb852011SMarkus Armbruster return 1; 297eb852011SMarkus Armbruster } 298eb852011SMarkus Armbruster } 299b64ec4e4SFam Zheng if (read_only) { 300b64ec4e4SFam Zheng for (p = whitelist_ro; *p; p++) { 301b64ec4e4SFam Zheng if (!strcmp(drv->format_name, *p)) { 302b64ec4e4SFam Zheng return 1; 303b64ec4e4SFam Zheng } 304b64ec4e4SFam Zheng } 305b64ec4e4SFam Zheng } 306eb852011SMarkus Armbruster return 0; 307eb852011SMarkus Armbruster } 308eb852011SMarkus Armbruster 309b64ec4e4SFam Zheng BlockDriver *bdrv_find_whitelisted_format(const char *format_name, 310b64ec4e4SFam Zheng bool read_only) 311eb852011SMarkus Armbruster { 312eb852011SMarkus Armbruster BlockDriver *drv = bdrv_find_format(format_name); 313b64ec4e4SFam Zheng return drv && bdrv_is_whitelisted(drv, read_only) ? drv : NULL; 314eb852011SMarkus Armbruster } 315eb852011SMarkus Armbruster 3165b7e1542SZhi Yong Wu typedef struct CreateCo { 3175b7e1542SZhi Yong Wu BlockDriver *drv; 3185b7e1542SZhi Yong Wu char *filename; 31983d0521aSChunyan Liu QemuOpts *opts; 3205b7e1542SZhi Yong Wu int ret; 321cc84d90fSMax Reitz Error *err; 3225b7e1542SZhi Yong Wu } CreateCo; 3235b7e1542SZhi Yong Wu 3245b7e1542SZhi Yong Wu static void coroutine_fn bdrv_create_co_entry(void *opaque) 3255b7e1542SZhi Yong Wu { 326cc84d90fSMax Reitz Error *local_err = NULL; 327cc84d90fSMax Reitz int ret; 328cc84d90fSMax Reitz 3295b7e1542SZhi Yong Wu CreateCo *cco = opaque; 3305b7e1542SZhi Yong Wu assert(cco->drv); 3315b7e1542SZhi Yong Wu 332c282e1fdSChunyan Liu ret = cco->drv->bdrv_create(cco->filename, cco->opts, &local_err); 33384d18f06SMarkus Armbruster if (local_err) { 334cc84d90fSMax Reitz error_propagate(&cco->err, local_err); 335cc84d90fSMax Reitz } 336cc84d90fSMax Reitz cco->ret = ret; 3375b7e1542SZhi Yong Wu } 3385b7e1542SZhi Yong Wu 3390e7e1989SKevin Wolf int bdrv_create(BlockDriver *drv, const char* filename, 34083d0521aSChunyan Liu QemuOpts *opts, Error **errp) 341ea2384d3Sbellard { 3425b7e1542SZhi Yong Wu int ret; 3430e7e1989SKevin Wolf 3445b7e1542SZhi Yong Wu Coroutine *co; 3455b7e1542SZhi Yong Wu CreateCo cco = { 3465b7e1542SZhi Yong Wu .drv = drv, 3475b7e1542SZhi Yong Wu .filename = g_strdup(filename), 34883d0521aSChunyan Liu .opts = opts, 3495b7e1542SZhi Yong Wu .ret = NOT_DONE, 350cc84d90fSMax Reitz .err = NULL, 3515b7e1542SZhi Yong Wu }; 3525b7e1542SZhi Yong Wu 353c282e1fdSChunyan Liu if (!drv->bdrv_create) { 354cc84d90fSMax Reitz error_setg(errp, "Driver '%s' does not support image creation", drv->format_name); 35580168bffSLuiz Capitulino ret = -ENOTSUP; 35680168bffSLuiz Capitulino goto out; 3575b7e1542SZhi Yong Wu } 3585b7e1542SZhi Yong Wu 3595b7e1542SZhi Yong Wu if (qemu_in_coroutine()) { 3605b7e1542SZhi Yong Wu /* Fast-path if already in coroutine context */ 3615b7e1542SZhi Yong Wu bdrv_create_co_entry(&cco); 3625b7e1542SZhi Yong Wu } else { 3635b7e1542SZhi Yong Wu co = qemu_coroutine_create(bdrv_create_co_entry); 3645b7e1542SZhi Yong Wu qemu_coroutine_enter(co, &cco); 3655b7e1542SZhi Yong Wu while (cco.ret == NOT_DONE) { 366b47ec2c4SPaolo Bonzini aio_poll(qemu_get_aio_context(), true); 3675b7e1542SZhi Yong Wu } 3685b7e1542SZhi Yong Wu } 3695b7e1542SZhi Yong Wu 3705b7e1542SZhi Yong Wu ret = cco.ret; 371cc84d90fSMax Reitz if (ret < 0) { 37284d18f06SMarkus Armbruster if (cco.err) { 373cc84d90fSMax Reitz error_propagate(errp, cco.err); 374cc84d90fSMax Reitz } else { 375cc84d90fSMax Reitz error_setg_errno(errp, -ret, "Could not create image"); 376cc84d90fSMax Reitz } 377cc84d90fSMax Reitz } 3785b7e1542SZhi Yong Wu 37980168bffSLuiz Capitulino out: 38080168bffSLuiz Capitulino g_free(cco.filename); 3815b7e1542SZhi Yong Wu return ret; 382ea2384d3Sbellard } 383ea2384d3Sbellard 384c282e1fdSChunyan Liu int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp) 38584a12e66SChristoph Hellwig { 38684a12e66SChristoph Hellwig BlockDriver *drv; 387cc84d90fSMax Reitz Error *local_err = NULL; 388cc84d90fSMax Reitz int ret; 38984a12e66SChristoph Hellwig 390b65a5e12SMax Reitz drv = bdrv_find_protocol(filename, true, errp); 39184a12e66SChristoph Hellwig if (drv == NULL) { 39216905d71SStefan Hajnoczi return -ENOENT; 39384a12e66SChristoph Hellwig } 39484a12e66SChristoph Hellwig 395c282e1fdSChunyan Liu ret = bdrv_create(drv, filename, opts, &local_err); 39684d18f06SMarkus Armbruster if (local_err) { 397cc84d90fSMax Reitz error_propagate(errp, local_err); 398cc84d90fSMax Reitz } 399cc84d90fSMax Reitz return ret; 40084a12e66SChristoph Hellwig } 40184a12e66SChristoph Hellwig 402892b7de8SEkaterina Tumanova /** 403892b7de8SEkaterina Tumanova * Try to get @bs's logical and physical block size. 404892b7de8SEkaterina Tumanova * On success, store them in @bsz struct and return 0. 405892b7de8SEkaterina Tumanova * On failure return -errno. 406892b7de8SEkaterina Tumanova * @bs must not be empty. 407892b7de8SEkaterina Tumanova */ 408892b7de8SEkaterina Tumanova int bdrv_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz) 409892b7de8SEkaterina Tumanova { 410892b7de8SEkaterina Tumanova BlockDriver *drv = bs->drv; 411892b7de8SEkaterina Tumanova 412892b7de8SEkaterina Tumanova if (drv && drv->bdrv_probe_blocksizes) { 413892b7de8SEkaterina Tumanova return drv->bdrv_probe_blocksizes(bs, bsz); 414892b7de8SEkaterina Tumanova } 415892b7de8SEkaterina Tumanova 416892b7de8SEkaterina Tumanova return -ENOTSUP; 417892b7de8SEkaterina Tumanova } 418892b7de8SEkaterina Tumanova 419892b7de8SEkaterina Tumanova /** 420892b7de8SEkaterina Tumanova * Try to get @bs's geometry (cyls, heads, sectors). 421892b7de8SEkaterina Tumanova * On success, store them in @geo struct and return 0. 422892b7de8SEkaterina Tumanova * On failure return -errno. 423892b7de8SEkaterina Tumanova * @bs must not be empty. 424892b7de8SEkaterina Tumanova */ 425892b7de8SEkaterina Tumanova int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo) 426892b7de8SEkaterina Tumanova { 427892b7de8SEkaterina Tumanova BlockDriver *drv = bs->drv; 428892b7de8SEkaterina Tumanova 429892b7de8SEkaterina Tumanova if (drv && drv->bdrv_probe_geometry) { 430892b7de8SEkaterina Tumanova return drv->bdrv_probe_geometry(bs, geo); 431892b7de8SEkaterina Tumanova } 432892b7de8SEkaterina Tumanova 433892b7de8SEkaterina Tumanova return -ENOTSUP; 434892b7de8SEkaterina Tumanova } 435892b7de8SEkaterina Tumanova 436eba25057SJim Meyering /* 437eba25057SJim Meyering * Create a uniquely-named empty temporary file. 438eba25057SJim Meyering * Return 0 upon success, otherwise a negative errno value. 439eba25057SJim Meyering */ 440eba25057SJim Meyering int get_tmp_filename(char *filename, int size) 441eba25057SJim Meyering { 442d5249393Sbellard #ifdef _WIN32 4433b9f94e1Sbellard char temp_dir[MAX_PATH]; 444eba25057SJim Meyering /* GetTempFileName requires that its output buffer (4th param) 445eba25057SJim Meyering have length MAX_PATH or greater. */ 446eba25057SJim Meyering assert(size >= MAX_PATH); 447eba25057SJim Meyering return (GetTempPath(MAX_PATH, temp_dir) 448eba25057SJim Meyering && GetTempFileName(temp_dir, "qem", 0, filename) 449eba25057SJim Meyering ? 0 : -GetLastError()); 450d5249393Sbellard #else 451ea2384d3Sbellard int fd; 4527ccfb2ebSblueswir1 const char *tmpdir; 4530badc1eeSaurel32 tmpdir = getenv("TMPDIR"); 45469bef793SAmit Shah if (!tmpdir) { 45569bef793SAmit Shah tmpdir = "/var/tmp"; 45669bef793SAmit Shah } 457eba25057SJim Meyering if (snprintf(filename, size, "%s/vl.XXXXXX", tmpdir) >= size) { 458eba25057SJim Meyering return -EOVERFLOW; 459ea2384d3Sbellard } 460eba25057SJim Meyering fd = mkstemp(filename); 461fe235a06SDunrong Huang if (fd < 0) { 462fe235a06SDunrong Huang return -errno; 463fe235a06SDunrong Huang } 464fe235a06SDunrong Huang if (close(fd) != 0) { 465fe235a06SDunrong Huang unlink(filename); 466eba25057SJim Meyering return -errno; 467eba25057SJim Meyering } 468eba25057SJim Meyering return 0; 469d5249393Sbellard #endif 470eba25057SJim Meyering } 471ea2384d3Sbellard 472f3a5d3f8SChristoph Hellwig /* 473f3a5d3f8SChristoph Hellwig * Detect host devices. By convention, /dev/cdrom[N] is always 474f3a5d3f8SChristoph Hellwig * recognized as a host CDROM. 475f3a5d3f8SChristoph Hellwig */ 476f3a5d3f8SChristoph Hellwig static BlockDriver *find_hdev_driver(const char *filename) 477f3a5d3f8SChristoph Hellwig { 478508c7cb3SChristoph Hellwig int score_max = 0, score; 479508c7cb3SChristoph Hellwig BlockDriver *drv = NULL, *d; 480f3a5d3f8SChristoph Hellwig 4818a22f02aSStefan Hajnoczi QLIST_FOREACH(d, &bdrv_drivers, list) { 482508c7cb3SChristoph Hellwig if (d->bdrv_probe_device) { 483508c7cb3SChristoph Hellwig score = d->bdrv_probe_device(filename); 484508c7cb3SChristoph Hellwig if (score > score_max) { 485508c7cb3SChristoph Hellwig score_max = score; 486508c7cb3SChristoph Hellwig drv = d; 487f3a5d3f8SChristoph Hellwig } 488508c7cb3SChristoph Hellwig } 489f3a5d3f8SChristoph Hellwig } 490f3a5d3f8SChristoph Hellwig 491508c7cb3SChristoph Hellwig return drv; 492f3a5d3f8SChristoph Hellwig } 493f3a5d3f8SChristoph Hellwig 49498289620SKevin Wolf BlockDriver *bdrv_find_protocol(const char *filename, 495b65a5e12SMax Reitz bool allow_protocol_prefix, 496b65a5e12SMax Reitz Error **errp) 49784a12e66SChristoph Hellwig { 49884a12e66SChristoph Hellwig BlockDriver *drv1; 49984a12e66SChristoph Hellwig char protocol[128]; 50084a12e66SChristoph Hellwig int len; 50184a12e66SChristoph Hellwig const char *p; 50284a12e66SChristoph Hellwig 50366f82ceeSKevin Wolf /* TODO Drivers without bdrv_file_open must be specified explicitly */ 50466f82ceeSKevin Wolf 50539508e7aSChristoph Hellwig /* 50639508e7aSChristoph Hellwig * XXX(hch): we really should not let host device detection 50739508e7aSChristoph Hellwig * override an explicit protocol specification, but moving this 50839508e7aSChristoph Hellwig * later breaks access to device names with colons in them. 50939508e7aSChristoph Hellwig * Thanks to the brain-dead persistent naming schemes on udev- 51039508e7aSChristoph Hellwig * based Linux systems those actually are quite common. 51139508e7aSChristoph Hellwig */ 51284a12e66SChristoph Hellwig drv1 = find_hdev_driver(filename); 51339508e7aSChristoph Hellwig if (drv1) { 51484a12e66SChristoph Hellwig return drv1; 51584a12e66SChristoph Hellwig } 51639508e7aSChristoph Hellwig 51798289620SKevin Wolf if (!path_has_protocol(filename) || !allow_protocol_prefix) { 518ef810437SMax Reitz return &bdrv_file; 51939508e7aSChristoph Hellwig } 52098289620SKevin Wolf 5219e0b22f4SStefan Hajnoczi p = strchr(filename, ':'); 5229e0b22f4SStefan Hajnoczi assert(p != NULL); 52384a12e66SChristoph Hellwig len = p - filename; 52484a12e66SChristoph Hellwig if (len > sizeof(protocol) - 1) 52584a12e66SChristoph Hellwig len = sizeof(protocol) - 1; 52684a12e66SChristoph Hellwig memcpy(protocol, filename, len); 52784a12e66SChristoph Hellwig protocol[len] = '\0'; 52884a12e66SChristoph Hellwig QLIST_FOREACH(drv1, &bdrv_drivers, list) { 52984a12e66SChristoph Hellwig if (drv1->protocol_name && 53084a12e66SChristoph Hellwig !strcmp(drv1->protocol_name, protocol)) { 53184a12e66SChristoph Hellwig return drv1; 53284a12e66SChristoph Hellwig } 53384a12e66SChristoph Hellwig } 534b65a5e12SMax Reitz 535b65a5e12SMax Reitz error_setg(errp, "Unknown protocol '%s'", protocol); 53684a12e66SChristoph Hellwig return NULL; 53784a12e66SChristoph Hellwig } 53884a12e66SChristoph Hellwig 539c6684249SMarkus Armbruster /* 540c6684249SMarkus Armbruster * Guess image format by probing its contents. 541c6684249SMarkus Armbruster * This is not a good idea when your image is raw (CVE-2008-2004), but 542c6684249SMarkus Armbruster * we do it anyway for backward compatibility. 543c6684249SMarkus Armbruster * 544c6684249SMarkus Armbruster * @buf contains the image's first @buf_size bytes. 5457cddd372SKevin Wolf * @buf_size is the buffer size in bytes (generally BLOCK_PROBE_BUF_SIZE, 5467cddd372SKevin Wolf * but can be smaller if the image file is smaller) 547c6684249SMarkus Armbruster * @filename is its filename. 548c6684249SMarkus Armbruster * 549c6684249SMarkus Armbruster * For all block drivers, call the bdrv_probe() method to get its 550c6684249SMarkus Armbruster * probing score. 551c6684249SMarkus Armbruster * Return the first block driver with the highest probing score. 552c6684249SMarkus Armbruster */ 55338f3ef57SKevin Wolf BlockDriver *bdrv_probe_all(const uint8_t *buf, int buf_size, 554c6684249SMarkus Armbruster const char *filename) 555c6684249SMarkus Armbruster { 556c6684249SMarkus Armbruster int score_max = 0, score; 557c6684249SMarkus Armbruster BlockDriver *drv = NULL, *d; 558c6684249SMarkus Armbruster 559c6684249SMarkus Armbruster QLIST_FOREACH(d, &bdrv_drivers, list) { 560c6684249SMarkus Armbruster if (d->bdrv_probe) { 561c6684249SMarkus Armbruster score = d->bdrv_probe(buf, buf_size, filename); 562c6684249SMarkus Armbruster if (score > score_max) { 563c6684249SMarkus Armbruster score_max = score; 564c6684249SMarkus Armbruster drv = d; 565c6684249SMarkus Armbruster } 566c6684249SMarkus Armbruster } 567c6684249SMarkus Armbruster } 568c6684249SMarkus Armbruster 569c6684249SMarkus Armbruster return drv; 570c6684249SMarkus Armbruster } 571c6684249SMarkus Armbruster 572f500a6d3SKevin Wolf static int find_image_format(BlockDriverState *bs, const char *filename, 57334b5d2c6SMax Reitz BlockDriver **pdrv, Error **errp) 574ea2384d3Sbellard { 575c6684249SMarkus Armbruster BlockDriver *drv; 5767cddd372SKevin Wolf uint8_t buf[BLOCK_PROBE_BUF_SIZE]; 577f500a6d3SKevin Wolf int ret = 0; 578f8ea0b00SNicholas Bellinger 57908a00559SKevin Wolf /* Return the raw BlockDriver * to scsi-generic devices or empty drives */ 5808e895599SPaolo Bonzini if (bs->sg || !bdrv_is_inserted(bs) || bdrv_getlength(bs) == 0) { 581ef810437SMax Reitz *pdrv = &bdrv_raw; 582c98ac35dSStefan Weil return ret; 5831a396859SNicholas A. Bellinger } 584f8ea0b00SNicholas Bellinger 58583f64091Sbellard ret = bdrv_pread(bs, 0, buf, sizeof(buf)); 586ea2384d3Sbellard if (ret < 0) { 58734b5d2c6SMax Reitz error_setg_errno(errp, -ret, "Could not read image for determining its " 58834b5d2c6SMax Reitz "format"); 589c98ac35dSStefan Weil *pdrv = NULL; 590c98ac35dSStefan Weil return ret; 591ea2384d3Sbellard } 592ea2384d3Sbellard 593c6684249SMarkus Armbruster drv = bdrv_probe_all(buf, ret, filename); 594c98ac35dSStefan Weil if (!drv) { 59534b5d2c6SMax Reitz error_setg(errp, "Could not determine image format: No compatible " 59634b5d2c6SMax Reitz "driver found"); 597c98ac35dSStefan Weil ret = -ENOENT; 598c98ac35dSStefan Weil } 599c98ac35dSStefan Weil *pdrv = drv; 600c98ac35dSStefan Weil return ret; 601ea2384d3Sbellard } 602ea2384d3Sbellard 60351762288SStefan Hajnoczi /** 60451762288SStefan Hajnoczi * Set the current 'total_sectors' value 60565a9bb25SMarkus Armbruster * Return 0 on success, -errno on error. 60651762288SStefan Hajnoczi */ 60751762288SStefan Hajnoczi static int refresh_total_sectors(BlockDriverState *bs, int64_t hint) 60851762288SStefan Hajnoczi { 60951762288SStefan Hajnoczi BlockDriver *drv = bs->drv; 61051762288SStefan Hajnoczi 611396759adSNicholas Bellinger /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */ 612396759adSNicholas Bellinger if (bs->sg) 613396759adSNicholas Bellinger return 0; 614396759adSNicholas Bellinger 61551762288SStefan Hajnoczi /* query actual device if possible, otherwise just trust the hint */ 61651762288SStefan Hajnoczi if (drv->bdrv_getlength) { 61751762288SStefan Hajnoczi int64_t length = drv->bdrv_getlength(bs); 61851762288SStefan Hajnoczi if (length < 0) { 61951762288SStefan Hajnoczi return length; 62051762288SStefan Hajnoczi } 6217e382003SFam Zheng hint = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE); 62251762288SStefan Hajnoczi } 62351762288SStefan Hajnoczi 62451762288SStefan Hajnoczi bs->total_sectors = hint; 62551762288SStefan Hajnoczi return 0; 62651762288SStefan Hajnoczi } 62751762288SStefan Hajnoczi 628c3993cdcSStefan Hajnoczi /** 6299e8f1835SPaolo Bonzini * Set open flags for a given discard mode 6309e8f1835SPaolo Bonzini * 6319e8f1835SPaolo Bonzini * Return 0 on success, -1 if the discard mode was invalid. 6329e8f1835SPaolo Bonzini */ 6339e8f1835SPaolo Bonzini int bdrv_parse_discard_flags(const char *mode, int *flags) 6349e8f1835SPaolo Bonzini { 6359e8f1835SPaolo Bonzini *flags &= ~BDRV_O_UNMAP; 6369e8f1835SPaolo Bonzini 6379e8f1835SPaolo Bonzini if (!strcmp(mode, "off") || !strcmp(mode, "ignore")) { 6389e8f1835SPaolo Bonzini /* do nothing */ 6399e8f1835SPaolo Bonzini } else if (!strcmp(mode, "on") || !strcmp(mode, "unmap")) { 6409e8f1835SPaolo Bonzini *flags |= BDRV_O_UNMAP; 6419e8f1835SPaolo Bonzini } else { 6429e8f1835SPaolo Bonzini return -1; 6439e8f1835SPaolo Bonzini } 6449e8f1835SPaolo Bonzini 6459e8f1835SPaolo Bonzini return 0; 6469e8f1835SPaolo Bonzini } 6479e8f1835SPaolo Bonzini 6489e8f1835SPaolo Bonzini /** 649c3993cdcSStefan Hajnoczi * Set open flags for a given cache mode 650c3993cdcSStefan Hajnoczi * 651c3993cdcSStefan Hajnoczi * Return 0 on success, -1 if the cache mode was invalid. 652c3993cdcSStefan Hajnoczi */ 653c3993cdcSStefan Hajnoczi int bdrv_parse_cache_flags(const char *mode, int *flags) 654c3993cdcSStefan Hajnoczi { 655c3993cdcSStefan Hajnoczi *flags &= ~BDRV_O_CACHE_MASK; 656c3993cdcSStefan Hajnoczi 657c3993cdcSStefan Hajnoczi if (!strcmp(mode, "off") || !strcmp(mode, "none")) { 658c3993cdcSStefan Hajnoczi *flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; 65992196b2fSStefan Hajnoczi } else if (!strcmp(mode, "directsync")) { 66092196b2fSStefan Hajnoczi *flags |= BDRV_O_NOCACHE; 661c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "writeback")) { 662c3993cdcSStefan Hajnoczi *flags |= BDRV_O_CACHE_WB; 663c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "unsafe")) { 664c3993cdcSStefan Hajnoczi *flags |= BDRV_O_CACHE_WB; 665c3993cdcSStefan Hajnoczi *flags |= BDRV_O_NO_FLUSH; 666c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "writethrough")) { 667c3993cdcSStefan Hajnoczi /* this is the default */ 668c3993cdcSStefan Hajnoczi } else { 669c3993cdcSStefan Hajnoczi return -1; 670c3993cdcSStefan Hajnoczi } 671c3993cdcSStefan Hajnoczi 672c3993cdcSStefan Hajnoczi return 0; 673c3993cdcSStefan Hajnoczi } 674c3993cdcSStefan Hajnoczi 6750b50cc88SKevin Wolf /* 676b1e6fc08SKevin Wolf * Returns the flags that a temporary snapshot should get, based on the 677b1e6fc08SKevin Wolf * originally requested flags (the originally requested image will have flags 678b1e6fc08SKevin Wolf * like a backing file) 679b1e6fc08SKevin Wolf */ 680b1e6fc08SKevin Wolf static int bdrv_temp_snapshot_flags(int flags) 681b1e6fc08SKevin Wolf { 682b1e6fc08SKevin Wolf return (flags & ~BDRV_O_SNAPSHOT) | BDRV_O_TEMPORARY; 683b1e6fc08SKevin Wolf } 684b1e6fc08SKevin Wolf 685b1e6fc08SKevin Wolf /* 6860b50cc88SKevin Wolf * Returns the flags that bs->file should get, based on the given flags for 6870b50cc88SKevin Wolf * the parent BDS 6880b50cc88SKevin Wolf */ 6890b50cc88SKevin Wolf static int bdrv_inherited_flags(int flags) 6900b50cc88SKevin Wolf { 6910b50cc88SKevin Wolf /* Enable protocol handling, disable format probing for bs->file */ 6920b50cc88SKevin Wolf flags |= BDRV_O_PROTOCOL; 6930b50cc88SKevin Wolf 6940b50cc88SKevin Wolf /* Our block drivers take care to send flushes and respect unmap policy, 6950b50cc88SKevin Wolf * so we can enable both unconditionally on lower layers. */ 6960b50cc88SKevin Wolf flags |= BDRV_O_CACHE_WB | BDRV_O_UNMAP; 6970b50cc88SKevin Wolf 6980b50cc88SKevin Wolf /* Clear flags that only apply to the top layer */ 6995669b44dSKevin Wolf flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ); 7000b50cc88SKevin Wolf 7010b50cc88SKevin Wolf return flags; 7020b50cc88SKevin Wolf } 7030b50cc88SKevin Wolf 704317fc44eSKevin Wolf /* 705317fc44eSKevin Wolf * Returns the flags that bs->backing_hd should get, based on the given flags 706317fc44eSKevin Wolf * for the parent BDS 707317fc44eSKevin Wolf */ 708317fc44eSKevin Wolf static int bdrv_backing_flags(int flags) 709317fc44eSKevin Wolf { 710317fc44eSKevin Wolf /* backing files always opened read-only */ 711317fc44eSKevin Wolf flags &= ~(BDRV_O_RDWR | BDRV_O_COPY_ON_READ); 712317fc44eSKevin Wolf 713317fc44eSKevin Wolf /* snapshot=on is handled on the top layer */ 7148bfea15dSKevin Wolf flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_TEMPORARY); 715317fc44eSKevin Wolf 716317fc44eSKevin Wolf return flags; 717317fc44eSKevin Wolf } 718317fc44eSKevin Wolf 7197b272452SKevin Wolf static int bdrv_open_flags(BlockDriverState *bs, int flags) 7207b272452SKevin Wolf { 7217b272452SKevin Wolf int open_flags = flags | BDRV_O_CACHE_WB; 7227b272452SKevin Wolf 7237b272452SKevin Wolf /* 7247b272452SKevin Wolf * Clear flags that are internal to the block layer before opening the 7257b272452SKevin Wolf * image. 7267b272452SKevin Wolf */ 72720cca275SKevin Wolf open_flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_PROTOCOL); 7287b272452SKevin Wolf 7297b272452SKevin Wolf /* 7307b272452SKevin Wolf * Snapshots should be writable. 7317b272452SKevin Wolf */ 7328bfea15dSKevin Wolf if (flags & BDRV_O_TEMPORARY) { 7337b272452SKevin Wolf open_flags |= BDRV_O_RDWR; 7347b272452SKevin Wolf } 7357b272452SKevin Wolf 7367b272452SKevin Wolf return open_flags; 7377b272452SKevin Wolf } 7387b272452SKevin Wolf 739636ea370SKevin Wolf static void bdrv_assign_node_name(BlockDriverState *bs, 7406913c0c2SBenoît Canet const char *node_name, 7416913c0c2SBenoît Canet Error **errp) 7426913c0c2SBenoît Canet { 7436913c0c2SBenoît Canet if (!node_name) { 744636ea370SKevin Wolf return; 7456913c0c2SBenoît Canet } 7466913c0c2SBenoît Canet 7479aebf3b8SKevin Wolf /* Check for empty string or invalid characters */ 748f5bebbbbSMarkus Armbruster if (!id_wellformed(node_name)) { 7499aebf3b8SKevin Wolf error_setg(errp, "Invalid node name"); 750636ea370SKevin Wolf return; 7516913c0c2SBenoît Canet } 7526913c0c2SBenoît Canet 7530c5e94eeSBenoît Canet /* takes care of avoiding namespaces collisions */ 7547f06d47eSMarkus Armbruster if (blk_by_name(node_name)) { 7550c5e94eeSBenoît Canet error_setg(errp, "node-name=%s is conflicting with a device id", 7560c5e94eeSBenoît Canet node_name); 757636ea370SKevin Wolf return; 7580c5e94eeSBenoît Canet } 7590c5e94eeSBenoît Canet 7606913c0c2SBenoît Canet /* takes care of avoiding duplicates node names */ 7616913c0c2SBenoît Canet if (bdrv_find_node(node_name)) { 7626913c0c2SBenoît Canet error_setg(errp, "Duplicate node name"); 763636ea370SKevin Wolf return; 7646913c0c2SBenoît Canet } 7656913c0c2SBenoît Canet 7666913c0c2SBenoît Canet /* copy node name into the bs and insert it into the graph list */ 7676913c0c2SBenoît Canet pstrcpy(bs->node_name, sizeof(bs->node_name), node_name); 7686913c0c2SBenoît Canet QTAILQ_INSERT_TAIL(&graph_bdrv_states, bs, node_list); 7696913c0c2SBenoît Canet } 7706913c0c2SBenoît Canet 771b6ce07aaSKevin Wolf /* 77257915332SKevin Wolf * Common part for opening disk images and files 773b6ad491aSKevin Wolf * 774b6ad491aSKevin Wolf * Removes all processed options from *options. 77557915332SKevin Wolf */ 776f500a6d3SKevin Wolf static int bdrv_open_common(BlockDriverState *bs, BlockDriverState *file, 77734b5d2c6SMax Reitz QDict *options, int flags, BlockDriver *drv, Error **errp) 77857915332SKevin Wolf { 77957915332SKevin Wolf int ret, open_flags; 780035fccdfSKevin Wolf const char *filename; 7816913c0c2SBenoît Canet const char *node_name = NULL; 78234b5d2c6SMax Reitz Error *local_err = NULL; 78357915332SKevin Wolf 78457915332SKevin Wolf assert(drv != NULL); 7856405875cSPaolo Bonzini assert(bs->file == NULL); 786707ff828SKevin Wolf assert(options != NULL && bs->options != options); 78757915332SKevin Wolf 78845673671SKevin Wolf if (file != NULL) { 78945673671SKevin Wolf filename = file->filename; 79045673671SKevin Wolf } else { 79145673671SKevin Wolf filename = qdict_get_try_str(options, "filename"); 79245673671SKevin Wolf } 79345673671SKevin Wolf 794765003dbSKevin Wolf if (drv->bdrv_needs_filename && !filename) { 795765003dbSKevin Wolf error_setg(errp, "The '%s' block driver requires a file name", 796765003dbSKevin Wolf drv->format_name); 797765003dbSKevin Wolf return -EINVAL; 798765003dbSKevin Wolf } 799765003dbSKevin Wolf 80045673671SKevin Wolf trace_bdrv_open_common(bs, filename ?: "", flags, drv->format_name); 80128dcee10SStefan Hajnoczi 8026913c0c2SBenoît Canet node_name = qdict_get_try_str(options, "node-name"); 803636ea370SKevin Wolf bdrv_assign_node_name(bs, node_name, &local_err); 8040fb6395cSMarkus Armbruster if (local_err) { 805636ea370SKevin Wolf error_propagate(errp, local_err); 806636ea370SKevin Wolf return -EINVAL; 8076913c0c2SBenoît Canet } 8086913c0c2SBenoît Canet qdict_del(options, "node-name"); 8096913c0c2SBenoît Canet 8105d186eb0SKevin Wolf /* bdrv_open() with directly using a protocol as drv. This layer is already 8115d186eb0SKevin Wolf * opened, so assign it to bs (while file becomes a closed BlockDriverState) 8125d186eb0SKevin Wolf * and return immediately. */ 8135d186eb0SKevin Wolf if (file != NULL && drv->bdrv_file_open) { 8145d186eb0SKevin Wolf bdrv_swap(file, bs); 8155d186eb0SKevin Wolf return 0; 8165d186eb0SKevin Wolf } 8175d186eb0SKevin Wolf 81857915332SKevin Wolf bs->open_flags = flags; 8191b7fd729SPaolo Bonzini bs->guest_block_size = 512; 820c25f53b0SPaolo Bonzini bs->request_alignment = 512; 8210d51b4deSAsias He bs->zero_beyond_eof = true; 822b64ec4e4SFam Zheng open_flags = bdrv_open_flags(bs, flags); 823b64ec4e4SFam Zheng bs->read_only = !(open_flags & BDRV_O_RDWR); 824b64ec4e4SFam Zheng 825b64ec4e4SFam Zheng if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, bs->read_only)) { 8268f94a6e4SKevin Wolf error_setg(errp, 8278f94a6e4SKevin Wolf !bs->read_only && bdrv_is_whitelisted(drv, true) 8288f94a6e4SKevin Wolf ? "Driver '%s' can only be used for read-only devices" 8298f94a6e4SKevin Wolf : "Driver '%s' is not whitelisted", 8308f94a6e4SKevin Wolf drv->format_name); 831b64ec4e4SFam Zheng return -ENOTSUP; 832b64ec4e4SFam Zheng } 83357915332SKevin Wolf 83453fec9d3SStefan Hajnoczi assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */ 8350ebd24e0SKevin Wolf if (flags & BDRV_O_COPY_ON_READ) { 8360ebd24e0SKevin Wolf if (!bs->read_only) { 83753fec9d3SStefan Hajnoczi bdrv_enable_copy_on_read(bs); 8380ebd24e0SKevin Wolf } else { 8390ebd24e0SKevin Wolf error_setg(errp, "Can't use copy-on-read on read-only device"); 8400ebd24e0SKevin Wolf return -EINVAL; 8410ebd24e0SKevin Wolf } 84253fec9d3SStefan Hajnoczi } 84353fec9d3SStefan Hajnoczi 844c2ad1b0cSKevin Wolf if (filename != NULL) { 84557915332SKevin Wolf pstrcpy(bs->filename, sizeof(bs->filename), filename); 846c2ad1b0cSKevin Wolf } else { 847c2ad1b0cSKevin Wolf bs->filename[0] = '\0'; 848c2ad1b0cSKevin Wolf } 84991af7014SMax Reitz pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), bs->filename); 85057915332SKevin Wolf 85157915332SKevin Wolf bs->drv = drv; 8527267c094SAnthony Liguori bs->opaque = g_malloc0(drv->instance_size); 85357915332SKevin Wolf 85403f541bdSStefan Hajnoczi bs->enable_write_cache = !!(flags & BDRV_O_CACHE_WB); 855e7c63796SStefan Hajnoczi 85666f82ceeSKevin Wolf /* Open the image, either directly or using a protocol */ 85766f82ceeSKevin Wolf if (drv->bdrv_file_open) { 8585d186eb0SKevin Wolf assert(file == NULL); 859030be321SBenoît Canet assert(!drv->bdrv_needs_filename || filename != NULL); 86034b5d2c6SMax Reitz ret = drv->bdrv_file_open(bs, options, open_flags, &local_err); 861f500a6d3SKevin Wolf } else { 8622af5ef70SKevin Wolf if (file == NULL) { 86334b5d2c6SMax Reitz error_setg(errp, "Can't use '%s' as a block driver for the " 86434b5d2c6SMax Reitz "protocol level", drv->format_name); 8652af5ef70SKevin Wolf ret = -EINVAL; 8662af5ef70SKevin Wolf goto free_and_fail; 8672af5ef70SKevin Wolf } 868f500a6d3SKevin Wolf bs->file = file; 86934b5d2c6SMax Reitz ret = drv->bdrv_open(bs, options, open_flags, &local_err); 87066f82ceeSKevin Wolf } 87166f82ceeSKevin Wolf 87257915332SKevin Wolf if (ret < 0) { 87384d18f06SMarkus Armbruster if (local_err) { 87434b5d2c6SMax Reitz error_propagate(errp, local_err); 8752fa9aa59SDunrong Huang } else if (bs->filename[0]) { 8762fa9aa59SDunrong Huang error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename); 87734b5d2c6SMax Reitz } else { 87834b5d2c6SMax Reitz error_setg_errno(errp, -ret, "Could not open image"); 87934b5d2c6SMax Reitz } 88057915332SKevin Wolf goto free_and_fail; 88157915332SKevin Wolf } 88257915332SKevin Wolf 883a1f688f4SMarkus Armbruster if (bs->encrypted) { 884a1f688f4SMarkus Armbruster error_report("Encrypted images are deprecated"); 885a1f688f4SMarkus Armbruster error_printf("Support for them will be removed in a future release.\n" 886a1f688f4SMarkus Armbruster "You can use 'qemu-img convert' to convert your image" 887a1f688f4SMarkus Armbruster " to an unencrypted one.\n"); 888a1f688f4SMarkus Armbruster } 889a1f688f4SMarkus Armbruster 89051762288SStefan Hajnoczi ret = refresh_total_sectors(bs, bs->total_sectors); 89151762288SStefan Hajnoczi if (ret < 0) { 89234b5d2c6SMax Reitz error_setg_errno(errp, -ret, "Could not refresh total sector count"); 89351762288SStefan Hajnoczi goto free_and_fail; 89457915332SKevin Wolf } 89551762288SStefan Hajnoczi 8963baca891SKevin Wolf bdrv_refresh_limits(bs, &local_err); 8973baca891SKevin Wolf if (local_err) { 8983baca891SKevin Wolf error_propagate(errp, local_err); 8993baca891SKevin Wolf ret = -EINVAL; 9003baca891SKevin Wolf goto free_and_fail; 9013baca891SKevin Wolf } 9023baca891SKevin Wolf 903c25f53b0SPaolo Bonzini assert(bdrv_opt_mem_align(bs) != 0); 9044196d2f0SDenis V. Lunev assert(bdrv_min_mem_align(bs) != 0); 90547ea2de2SKevin Wolf assert((bs->request_alignment != 0) || bs->sg); 90657915332SKevin Wolf return 0; 90757915332SKevin Wolf 90857915332SKevin Wolf free_and_fail: 90966f82ceeSKevin Wolf bs->file = NULL; 9107267c094SAnthony Liguori g_free(bs->opaque); 91157915332SKevin Wolf bs->opaque = NULL; 91257915332SKevin Wolf bs->drv = NULL; 91357915332SKevin Wolf return ret; 91457915332SKevin Wolf } 91557915332SKevin Wolf 9165e5c4f63SKevin Wolf static QDict *parse_json_filename(const char *filename, Error **errp) 9175e5c4f63SKevin Wolf { 9185e5c4f63SKevin Wolf QObject *options_obj; 9195e5c4f63SKevin Wolf QDict *options; 9205e5c4f63SKevin Wolf int ret; 9215e5c4f63SKevin Wolf 9225e5c4f63SKevin Wolf ret = strstart(filename, "json:", &filename); 9235e5c4f63SKevin Wolf assert(ret); 9245e5c4f63SKevin Wolf 9255e5c4f63SKevin Wolf options_obj = qobject_from_json(filename); 9265e5c4f63SKevin Wolf if (!options_obj) { 9275e5c4f63SKevin Wolf error_setg(errp, "Could not parse the JSON options"); 9285e5c4f63SKevin Wolf return NULL; 9295e5c4f63SKevin Wolf } 9305e5c4f63SKevin Wolf 9315e5c4f63SKevin Wolf if (qobject_type(options_obj) != QTYPE_QDICT) { 9325e5c4f63SKevin Wolf qobject_decref(options_obj); 9335e5c4f63SKevin Wolf error_setg(errp, "Invalid JSON object given"); 9345e5c4f63SKevin Wolf return NULL; 9355e5c4f63SKevin Wolf } 9365e5c4f63SKevin Wolf 9375e5c4f63SKevin Wolf options = qobject_to_qdict(options_obj); 9385e5c4f63SKevin Wolf qdict_flatten(options); 9395e5c4f63SKevin Wolf 9405e5c4f63SKevin Wolf return options; 9415e5c4f63SKevin Wolf } 9425e5c4f63SKevin Wolf 94357915332SKevin Wolf /* 944f54120ffSKevin Wolf * Fills in default options for opening images and converts the legacy 945f54120ffSKevin Wolf * filename/flags pair to option QDict entries. 946f54120ffSKevin Wolf */ 9475e5c4f63SKevin Wolf static int bdrv_fill_options(QDict **options, const char **pfilename, int flags, 94817b005f1SKevin Wolf BlockDriver *drv, Error **errp) 949f54120ffSKevin Wolf { 9505e5c4f63SKevin Wolf const char *filename = *pfilename; 951f54120ffSKevin Wolf const char *drvname; 952462f5bcfSKevin Wolf bool protocol = flags & BDRV_O_PROTOCOL; 953f54120ffSKevin Wolf bool parse_filename = false; 954f54120ffSKevin Wolf Error *local_err = NULL; 955f54120ffSKevin Wolf 9565e5c4f63SKevin Wolf /* Parse json: pseudo-protocol */ 9575e5c4f63SKevin Wolf if (filename && g_str_has_prefix(filename, "json:")) { 9585e5c4f63SKevin Wolf QDict *json_options = parse_json_filename(filename, &local_err); 9595e5c4f63SKevin Wolf if (local_err) { 9605e5c4f63SKevin Wolf error_propagate(errp, local_err); 9615e5c4f63SKevin Wolf return -EINVAL; 9625e5c4f63SKevin Wolf } 9635e5c4f63SKevin Wolf 9645e5c4f63SKevin Wolf /* Options given in the filename have lower priority than options 9655e5c4f63SKevin Wolf * specified directly */ 9665e5c4f63SKevin Wolf qdict_join(*options, json_options, false); 9675e5c4f63SKevin Wolf QDECREF(json_options); 9685e5c4f63SKevin Wolf *pfilename = filename = NULL; 9695e5c4f63SKevin Wolf } 9705e5c4f63SKevin Wolf 971f54120ffSKevin Wolf /* Fetch the file name from the options QDict if necessary */ 97217b005f1SKevin Wolf if (protocol && filename) { 973f54120ffSKevin Wolf if (!qdict_haskey(*options, "filename")) { 974f54120ffSKevin Wolf qdict_put(*options, "filename", qstring_from_str(filename)); 975f54120ffSKevin Wolf parse_filename = true; 976f54120ffSKevin Wolf } else { 977f54120ffSKevin Wolf error_setg(errp, "Can't specify 'file' and 'filename' options at " 978f54120ffSKevin Wolf "the same time"); 979f54120ffSKevin Wolf return -EINVAL; 980f54120ffSKevin Wolf } 981f54120ffSKevin Wolf } 982f54120ffSKevin Wolf 983f54120ffSKevin Wolf /* Find the right block driver */ 984f54120ffSKevin Wolf filename = qdict_get_try_str(*options, "filename"); 985f54120ffSKevin Wolf drvname = qdict_get_try_str(*options, "driver"); 986f54120ffSKevin Wolf 98717b005f1SKevin Wolf if (drv) { 98817b005f1SKevin Wolf if (drvname) { 98917b005f1SKevin Wolf error_setg(errp, "Driver specified twice"); 99017b005f1SKevin Wolf return -EINVAL; 99117b005f1SKevin Wolf } 99217b005f1SKevin Wolf drvname = drv->format_name; 99317b005f1SKevin Wolf qdict_put(*options, "driver", qstring_from_str(drvname)); 99417b005f1SKevin Wolf } else { 99517b005f1SKevin Wolf if (!drvname && protocol) { 996f54120ffSKevin Wolf if (filename) { 997b65a5e12SMax Reitz drv = bdrv_find_protocol(filename, parse_filename, errp); 998f54120ffSKevin Wolf if (!drv) { 999f54120ffSKevin Wolf return -EINVAL; 1000f54120ffSKevin Wolf } 1001f54120ffSKevin Wolf 1002f54120ffSKevin Wolf drvname = drv->format_name; 1003f54120ffSKevin Wolf qdict_put(*options, "driver", qstring_from_str(drvname)); 1004f54120ffSKevin Wolf } else { 1005f54120ffSKevin Wolf error_setg(errp, "Must specify either driver or file"); 1006f54120ffSKevin Wolf return -EINVAL; 1007f54120ffSKevin Wolf } 100817b005f1SKevin Wolf } else if (drvname) { 1009f54120ffSKevin Wolf drv = bdrv_find_format(drvname); 1010f54120ffSKevin Wolf if (!drv) { 1011f54120ffSKevin Wolf error_setg(errp, "Unknown driver '%s'", drvname); 1012f54120ffSKevin Wolf return -ENOENT; 1013f54120ffSKevin Wolf } 101417b005f1SKevin Wolf } 101517b005f1SKevin Wolf } 101617b005f1SKevin Wolf 101717b005f1SKevin Wolf assert(drv || !protocol); 1018f54120ffSKevin Wolf 1019f54120ffSKevin Wolf /* Driver-specific filename parsing */ 102017b005f1SKevin Wolf if (drv && drv->bdrv_parse_filename && parse_filename) { 1021f54120ffSKevin Wolf drv->bdrv_parse_filename(filename, *options, &local_err); 1022f54120ffSKevin Wolf if (local_err) { 1023f54120ffSKevin Wolf error_propagate(errp, local_err); 1024f54120ffSKevin Wolf return -EINVAL; 1025f54120ffSKevin Wolf } 1026f54120ffSKevin Wolf 1027f54120ffSKevin Wolf if (!drv->bdrv_needs_filename) { 1028f54120ffSKevin Wolf qdict_del(*options, "filename"); 1029f54120ffSKevin Wolf } 1030f54120ffSKevin Wolf } 1031f54120ffSKevin Wolf 1032f54120ffSKevin Wolf return 0; 1033f54120ffSKevin Wolf } 1034f54120ffSKevin Wolf 10358d24cce1SFam Zheng void bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd) 10368d24cce1SFam Zheng { 10378d24cce1SFam Zheng 1038826b6ca0SFam Zheng if (bs->backing_hd) { 1039826b6ca0SFam Zheng assert(bs->backing_blocker); 1040826b6ca0SFam Zheng bdrv_op_unblock_all(bs->backing_hd, bs->backing_blocker); 1041826b6ca0SFam Zheng } else if (backing_hd) { 1042826b6ca0SFam Zheng error_setg(&bs->backing_blocker, 104381e5f78aSAlberto Garcia "node is used as backing hd of '%s'", 104481e5f78aSAlberto Garcia bdrv_get_device_or_node_name(bs)); 1045826b6ca0SFam Zheng } 1046826b6ca0SFam Zheng 10478d24cce1SFam Zheng bs->backing_hd = backing_hd; 10488d24cce1SFam Zheng if (!backing_hd) { 1049826b6ca0SFam Zheng error_free(bs->backing_blocker); 1050826b6ca0SFam Zheng bs->backing_blocker = NULL; 10518d24cce1SFam Zheng goto out; 10528d24cce1SFam Zheng } 10538d24cce1SFam Zheng bs->open_flags &= ~BDRV_O_NO_BACKING; 10548d24cce1SFam Zheng pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_hd->filename); 10558d24cce1SFam Zheng pstrcpy(bs->backing_format, sizeof(bs->backing_format), 10568d24cce1SFam Zheng backing_hd->drv ? backing_hd->drv->format_name : ""); 1057826b6ca0SFam Zheng 1058826b6ca0SFam Zheng bdrv_op_block_all(bs->backing_hd, bs->backing_blocker); 1059826b6ca0SFam Zheng /* Otherwise we won't be able to commit due to check in bdrv_commit */ 1060bb00021dSFam Zheng bdrv_op_unblock(bs->backing_hd, BLOCK_OP_TYPE_COMMIT_TARGET, 1061826b6ca0SFam Zheng bs->backing_blocker); 10628d24cce1SFam Zheng out: 10633baca891SKevin Wolf bdrv_refresh_limits(bs, NULL); 10648d24cce1SFam Zheng } 10658d24cce1SFam Zheng 106631ca6d07SKevin Wolf /* 106731ca6d07SKevin Wolf * Opens the backing file for a BlockDriverState if not yet open 106831ca6d07SKevin Wolf * 106931ca6d07SKevin Wolf * options is a QDict of options to pass to the block drivers, or NULL for an 107031ca6d07SKevin Wolf * empty set of options. The reference to the QDict is transferred to this 107131ca6d07SKevin Wolf * function (even on failure), so if the caller intends to reuse the dictionary, 107231ca6d07SKevin Wolf * it needs to use QINCREF() before calling bdrv_file_open. 107331ca6d07SKevin Wolf */ 107434b5d2c6SMax Reitz int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp) 10759156df12SPaolo Bonzini { 10761ba4b6a5SBenoît Canet char *backing_filename = g_malloc0(PATH_MAX); 1077317fc44eSKevin Wolf int ret = 0; 10788d24cce1SFam Zheng BlockDriverState *backing_hd; 107934b5d2c6SMax Reitz Error *local_err = NULL; 10809156df12SPaolo Bonzini 10819156df12SPaolo Bonzini if (bs->backing_hd != NULL) { 108231ca6d07SKevin Wolf QDECREF(options); 10831ba4b6a5SBenoît Canet goto free_exit; 10849156df12SPaolo Bonzini } 10859156df12SPaolo Bonzini 108631ca6d07SKevin Wolf /* NULL means an empty set of options */ 108731ca6d07SKevin Wolf if (options == NULL) { 108831ca6d07SKevin Wolf options = qdict_new(); 108931ca6d07SKevin Wolf } 109031ca6d07SKevin Wolf 10919156df12SPaolo Bonzini bs->open_flags &= ~BDRV_O_NO_BACKING; 10921cb6f506SKevin Wolf if (qdict_haskey(options, "file.filename")) { 10931cb6f506SKevin Wolf backing_filename[0] = '\0'; 10941cb6f506SKevin Wolf } else if (bs->backing_file[0] == '\0' && qdict_size(options) == 0) { 109531ca6d07SKevin Wolf QDECREF(options); 10961ba4b6a5SBenoît Canet goto free_exit; 1097dbecebddSFam Zheng } else { 10989f07429eSMax Reitz bdrv_get_full_backing_filename(bs, backing_filename, PATH_MAX, 10999f07429eSMax Reitz &local_err); 11009f07429eSMax Reitz if (local_err) { 11019f07429eSMax Reitz ret = -EINVAL; 11029f07429eSMax Reitz error_propagate(errp, local_err); 11039f07429eSMax Reitz QDECREF(options); 11049f07429eSMax Reitz goto free_exit; 11059f07429eSMax Reitz } 11069156df12SPaolo Bonzini } 11079156df12SPaolo Bonzini 11088ee79e70SKevin Wolf if (!bs->drv || !bs->drv->supports_backing) { 11098ee79e70SKevin Wolf ret = -EINVAL; 11108ee79e70SKevin Wolf error_setg(errp, "Driver doesn't support backing files"); 11118ee79e70SKevin Wolf QDECREF(options); 11128ee79e70SKevin Wolf goto free_exit; 11138ee79e70SKevin Wolf } 11148ee79e70SKevin Wolf 1115e4e9986bSMarkus Armbruster backing_hd = bdrv_new(); 11168d24cce1SFam Zheng 1117c5f6e493SKevin Wolf if (bs->backing_format[0] != '\0' && !qdict_haskey(options, "driver")) { 1118c5f6e493SKevin Wolf qdict_put(options, "driver", qstring_from_str(bs->backing_format)); 11199156df12SPaolo Bonzini } 11209156df12SPaolo Bonzini 1121f67503e5SMax Reitz assert(bs->backing_hd == NULL); 11228d24cce1SFam Zheng ret = bdrv_open(&backing_hd, 1123ddf5636dSMax Reitz *backing_filename ? backing_filename : NULL, NULL, options, 1124c5f6e493SKevin Wolf bdrv_backing_flags(bs->open_flags), NULL, &local_err); 11259156df12SPaolo Bonzini if (ret < 0) { 11268d24cce1SFam Zheng bdrv_unref(backing_hd); 11278d24cce1SFam Zheng backing_hd = NULL; 11289156df12SPaolo Bonzini bs->open_flags |= BDRV_O_NO_BACKING; 1129b04b6b6eSFam Zheng error_setg(errp, "Could not open backing file: %s", 1130b04b6b6eSFam Zheng error_get_pretty(local_err)); 1131b04b6b6eSFam Zheng error_free(local_err); 11321ba4b6a5SBenoît Canet goto free_exit; 11339156df12SPaolo Bonzini } 11348d24cce1SFam Zheng bdrv_set_backing_hd(bs, backing_hd); 1135d80ac658SPeter Feiner 11361ba4b6a5SBenoît Canet free_exit: 11371ba4b6a5SBenoît Canet g_free(backing_filename); 11381ba4b6a5SBenoît Canet return ret; 11399156df12SPaolo Bonzini } 11409156df12SPaolo Bonzini 1141b6ce07aaSKevin Wolf /* 1142da557aacSMax Reitz * Opens a disk image whose options are given as BlockdevRef in another block 1143da557aacSMax Reitz * device's options. 1144da557aacSMax Reitz * 1145da557aacSMax Reitz * If allow_none is true, no image will be opened if filename is false and no 1146da557aacSMax Reitz * BlockdevRef is given. *pbs will remain unchanged and 0 will be returned. 1147da557aacSMax Reitz * 1148da557aacSMax Reitz * bdrev_key specifies the key for the image's BlockdevRef in the options QDict. 1149da557aacSMax Reitz * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict 1150da557aacSMax Reitz * itself, all options starting with "${bdref_key}." are considered part of the 1151da557aacSMax Reitz * BlockdevRef. 1152da557aacSMax Reitz * 1153da557aacSMax Reitz * The BlockdevRef will be removed from the options QDict. 1154f67503e5SMax Reitz * 1155f67503e5SMax Reitz * To conform with the behavior of bdrv_open(), *pbs has to be NULL. 1156da557aacSMax Reitz */ 1157da557aacSMax Reitz int bdrv_open_image(BlockDriverState **pbs, const char *filename, 1158da557aacSMax Reitz QDict *options, const char *bdref_key, int flags, 1159f7d9fd8cSMax Reitz bool allow_none, Error **errp) 1160da557aacSMax Reitz { 1161da557aacSMax Reitz QDict *image_options; 1162da557aacSMax Reitz int ret; 1163da557aacSMax Reitz char *bdref_key_dot; 1164da557aacSMax Reitz const char *reference; 1165da557aacSMax Reitz 1166f67503e5SMax Reitz assert(pbs); 1167f67503e5SMax Reitz assert(*pbs == NULL); 1168f67503e5SMax Reitz 1169da557aacSMax Reitz bdref_key_dot = g_strdup_printf("%s.", bdref_key); 1170da557aacSMax Reitz qdict_extract_subqdict(options, &image_options, bdref_key_dot); 1171da557aacSMax Reitz g_free(bdref_key_dot); 1172da557aacSMax Reitz 1173da557aacSMax Reitz reference = qdict_get_try_str(options, bdref_key); 1174da557aacSMax Reitz if (!filename && !reference && !qdict_size(image_options)) { 1175da557aacSMax Reitz if (allow_none) { 1176da557aacSMax Reitz ret = 0; 1177da557aacSMax Reitz } else { 1178da557aacSMax Reitz error_setg(errp, "A block device must be specified for \"%s\"", 1179da557aacSMax Reitz bdref_key); 1180da557aacSMax Reitz ret = -EINVAL; 1181da557aacSMax Reitz } 1182b20e61e0SMarkus Armbruster QDECREF(image_options); 1183da557aacSMax Reitz goto done; 1184da557aacSMax Reitz } 1185da557aacSMax Reitz 1186f7d9fd8cSMax Reitz ret = bdrv_open(pbs, filename, reference, image_options, flags, NULL, errp); 1187da557aacSMax Reitz 1188da557aacSMax Reitz done: 1189da557aacSMax Reitz qdict_del(options, bdref_key); 1190da557aacSMax Reitz return ret; 1191da557aacSMax Reitz } 1192da557aacSMax Reitz 11936b8aeca5SChen Gang int bdrv_append_temp_snapshot(BlockDriverState *bs, int flags, Error **errp) 1194b998875dSKevin Wolf { 1195b998875dSKevin Wolf /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */ 11961ba4b6a5SBenoît Canet char *tmp_filename = g_malloc0(PATH_MAX + 1); 1197b998875dSKevin Wolf int64_t total_size; 119883d0521aSChunyan Liu QemuOpts *opts = NULL; 1199b998875dSKevin Wolf QDict *snapshot_options; 1200b998875dSKevin Wolf BlockDriverState *bs_snapshot; 1201b998875dSKevin Wolf Error *local_err; 1202b998875dSKevin Wolf int ret; 1203b998875dSKevin Wolf 1204b998875dSKevin Wolf /* if snapshot, we create a temporary backing file and open it 1205b998875dSKevin Wolf instead of opening 'filename' directly */ 1206b998875dSKevin Wolf 1207b998875dSKevin Wolf /* Get the required size from the image */ 1208f187743aSKevin Wolf total_size = bdrv_getlength(bs); 1209f187743aSKevin Wolf if (total_size < 0) { 12106b8aeca5SChen Gang ret = total_size; 1211f187743aSKevin Wolf error_setg_errno(errp, -total_size, "Could not get image size"); 12121ba4b6a5SBenoît Canet goto out; 1213f187743aSKevin Wolf } 1214b998875dSKevin Wolf 1215b998875dSKevin Wolf /* Create the temporary image */ 12161ba4b6a5SBenoît Canet ret = get_tmp_filename(tmp_filename, PATH_MAX + 1); 1217b998875dSKevin Wolf if (ret < 0) { 1218b998875dSKevin Wolf error_setg_errno(errp, -ret, "Could not get temporary filename"); 12191ba4b6a5SBenoît Canet goto out; 1220b998875dSKevin Wolf } 1221b998875dSKevin Wolf 1222ef810437SMax Reitz opts = qemu_opts_create(bdrv_qcow2.create_opts, NULL, 0, 1223c282e1fdSChunyan Liu &error_abort); 122439101f25SMarkus Armbruster qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_size, &error_abort); 1225ef810437SMax Reitz ret = bdrv_create(&bdrv_qcow2, tmp_filename, opts, &local_err); 122683d0521aSChunyan Liu qemu_opts_del(opts); 1227b998875dSKevin Wolf if (ret < 0) { 1228b998875dSKevin Wolf error_setg_errno(errp, -ret, "Could not create temporary overlay " 1229b998875dSKevin Wolf "'%s': %s", tmp_filename, 1230b998875dSKevin Wolf error_get_pretty(local_err)); 1231b998875dSKevin Wolf error_free(local_err); 12321ba4b6a5SBenoît Canet goto out; 1233b998875dSKevin Wolf } 1234b998875dSKevin Wolf 1235b998875dSKevin Wolf /* Prepare a new options QDict for the temporary file */ 1236b998875dSKevin Wolf snapshot_options = qdict_new(); 1237b998875dSKevin Wolf qdict_put(snapshot_options, "file.driver", 1238b998875dSKevin Wolf qstring_from_str("file")); 1239b998875dSKevin Wolf qdict_put(snapshot_options, "file.filename", 1240b998875dSKevin Wolf qstring_from_str(tmp_filename)); 1241b998875dSKevin Wolf 1242e4e9986bSMarkus Armbruster bs_snapshot = bdrv_new(); 1243b998875dSKevin Wolf 1244b998875dSKevin Wolf ret = bdrv_open(&bs_snapshot, NULL, NULL, snapshot_options, 1245ef810437SMax Reitz flags, &bdrv_qcow2, &local_err); 1246b998875dSKevin Wolf if (ret < 0) { 1247b998875dSKevin Wolf error_propagate(errp, local_err); 12481ba4b6a5SBenoît Canet goto out; 1249b998875dSKevin Wolf } 1250b998875dSKevin Wolf 1251b998875dSKevin Wolf bdrv_append(bs_snapshot, bs); 12521ba4b6a5SBenoît Canet 12531ba4b6a5SBenoît Canet out: 12541ba4b6a5SBenoît Canet g_free(tmp_filename); 12556b8aeca5SChen Gang return ret; 1256b998875dSKevin Wolf } 1257b998875dSKevin Wolf 1258da557aacSMax Reitz /* 1259b6ce07aaSKevin Wolf * Opens a disk image (raw, qcow2, vmdk, ...) 1260de9c0cecSKevin Wolf * 1261de9c0cecSKevin Wolf * options is a QDict of options to pass to the block drivers, or NULL for an 1262de9c0cecSKevin Wolf * empty set of options. The reference to the QDict belongs to the block layer 1263de9c0cecSKevin Wolf * after the call (even on failure), so if the caller intends to reuse the 1264de9c0cecSKevin Wolf * dictionary, it needs to use QINCREF() before calling bdrv_open. 1265f67503e5SMax Reitz * 1266f67503e5SMax Reitz * If *pbs is NULL, a new BDS will be created with a pointer to it stored there. 1267f67503e5SMax Reitz * If it is not NULL, the referenced BDS will be reused. 1268ddf5636dSMax Reitz * 1269ddf5636dSMax Reitz * The reference parameter may be used to specify an existing block device which 1270ddf5636dSMax Reitz * should be opened. If specified, neither options nor a filename may be given, 1271ddf5636dSMax Reitz * nor can an existing BDS be reused (that is, *pbs has to be NULL). 1272b6ce07aaSKevin Wolf */ 1273ddf5636dSMax Reitz int bdrv_open(BlockDriverState **pbs, const char *filename, 1274ddf5636dSMax Reitz const char *reference, QDict *options, int flags, 1275ddf5636dSMax Reitz BlockDriver *drv, Error **errp) 1276ea2384d3Sbellard { 1277b6ce07aaSKevin Wolf int ret; 1278f67503e5SMax Reitz BlockDriverState *file = NULL, *bs; 127974fe54f2SKevin Wolf const char *drvname; 128034b5d2c6SMax Reitz Error *local_err = NULL; 1281b1e6fc08SKevin Wolf int snapshot_flags = 0; 128233e3963eSbellard 1283f67503e5SMax Reitz assert(pbs); 1284f67503e5SMax Reitz 1285ddf5636dSMax Reitz if (reference) { 1286ddf5636dSMax Reitz bool options_non_empty = options ? qdict_size(options) : false; 1287ddf5636dSMax Reitz QDECREF(options); 1288ddf5636dSMax Reitz 1289ddf5636dSMax Reitz if (*pbs) { 1290ddf5636dSMax Reitz error_setg(errp, "Cannot reuse an existing BDS when referencing " 1291ddf5636dSMax Reitz "another block device"); 1292ddf5636dSMax Reitz return -EINVAL; 1293ddf5636dSMax Reitz } 1294ddf5636dSMax Reitz 1295ddf5636dSMax Reitz if (filename || options_non_empty) { 1296ddf5636dSMax Reitz error_setg(errp, "Cannot reference an existing block device with " 1297ddf5636dSMax Reitz "additional options or a new filename"); 1298ddf5636dSMax Reitz return -EINVAL; 1299ddf5636dSMax Reitz } 1300ddf5636dSMax Reitz 1301ddf5636dSMax Reitz bs = bdrv_lookup_bs(reference, reference, errp); 1302ddf5636dSMax Reitz if (!bs) { 1303ddf5636dSMax Reitz return -ENODEV; 1304ddf5636dSMax Reitz } 1305ddf5636dSMax Reitz bdrv_ref(bs); 1306ddf5636dSMax Reitz *pbs = bs; 1307ddf5636dSMax Reitz return 0; 1308ddf5636dSMax Reitz } 1309ddf5636dSMax Reitz 1310f67503e5SMax Reitz if (*pbs) { 1311f67503e5SMax Reitz bs = *pbs; 1312f67503e5SMax Reitz } else { 1313e4e9986bSMarkus Armbruster bs = bdrv_new(); 1314f67503e5SMax Reitz } 1315f67503e5SMax Reitz 1316de9c0cecSKevin Wolf /* NULL means an empty set of options */ 1317de9c0cecSKevin Wolf if (options == NULL) { 1318de9c0cecSKevin Wolf options = qdict_new(); 1319de9c0cecSKevin Wolf } 1320de9c0cecSKevin Wolf 132117b005f1SKevin Wolf ret = bdrv_fill_options(&options, &filename, flags, drv, &local_err); 1322462f5bcfSKevin Wolf if (local_err) { 1323462f5bcfSKevin Wolf goto fail; 1324462f5bcfSKevin Wolf } 1325462f5bcfSKevin Wolf 132676c591b0SKevin Wolf /* Find the right image format driver */ 132776c591b0SKevin Wolf drv = NULL; 132876c591b0SKevin Wolf drvname = qdict_get_try_str(options, "driver"); 132976c591b0SKevin Wolf if (drvname) { 133076c591b0SKevin Wolf drv = bdrv_find_format(drvname); 133176c591b0SKevin Wolf qdict_del(options, "driver"); 133276c591b0SKevin Wolf if (!drv) { 133376c591b0SKevin Wolf error_setg(errp, "Unknown driver: '%s'", drvname); 133476c591b0SKevin Wolf ret = -EINVAL; 133576c591b0SKevin Wolf goto fail; 133676c591b0SKevin Wolf } 133776c591b0SKevin Wolf } 133876c591b0SKevin Wolf 133976c591b0SKevin Wolf assert(drvname || !(flags & BDRV_O_PROTOCOL)); 134076c591b0SKevin Wolf if (drv && !drv->bdrv_file_open) { 134176c591b0SKevin Wolf /* If the user explicitly wants a format driver here, we'll need to add 134276c591b0SKevin Wolf * another layer for the protocol in bs->file */ 134376c591b0SKevin Wolf flags &= ~BDRV_O_PROTOCOL; 134476c591b0SKevin Wolf } 134576c591b0SKevin Wolf 1346de9c0cecSKevin Wolf bs->options = options; 1347b6ad491aSKevin Wolf options = qdict_clone_shallow(options); 1348de9c0cecSKevin Wolf 1349f500a6d3SKevin Wolf /* Open image file without format layer */ 1350f4788adcSKevin Wolf if ((flags & BDRV_O_PROTOCOL) == 0) { 1351be028adcSJeff Cody if (flags & BDRV_O_RDWR) { 1352be028adcSJeff Cody flags |= BDRV_O_ALLOW_RDWR; 1353be028adcSJeff Cody } 1354b1e6fc08SKevin Wolf if (flags & BDRV_O_SNAPSHOT) { 1355b1e6fc08SKevin Wolf snapshot_flags = bdrv_temp_snapshot_flags(flags); 1356b1e6fc08SKevin Wolf flags = bdrv_backing_flags(flags); 1357b1e6fc08SKevin Wolf } 1358be028adcSJeff Cody 1359f67503e5SMax Reitz assert(file == NULL); 1360054963f8SMax Reitz ret = bdrv_open_image(&file, filename, options, "file", 13610b50cc88SKevin Wolf bdrv_inherited_flags(flags), 13620b50cc88SKevin Wolf true, &local_err); 1363f500a6d3SKevin Wolf if (ret < 0) { 13648bfea15dSKevin Wolf goto fail; 1365f500a6d3SKevin Wolf } 1366f4788adcSKevin Wolf } 1367f500a6d3SKevin Wolf 136876c591b0SKevin Wolf /* Image format probing */ 136938f3ef57SKevin Wolf bs->probed = !drv; 137076c591b0SKevin Wolf if (!drv && file) { 137134b5d2c6SMax Reitz ret = find_image_format(file, filename, &drv, &local_err); 137217b005f1SKevin Wolf if (ret < 0) { 137317b005f1SKevin Wolf goto fail; 137417b005f1SKevin Wolf } 137576c591b0SKevin Wolf } else if (!drv) { 13762a05cbe4SMax Reitz error_setg(errp, "Must specify either driver or file"); 13772a05cbe4SMax Reitz ret = -EINVAL; 13788bfea15dSKevin Wolf goto fail; 13792a05cbe4SMax Reitz } 1380f500a6d3SKevin Wolf 1381b6ce07aaSKevin Wolf /* Open the image */ 138234b5d2c6SMax Reitz ret = bdrv_open_common(bs, file, options, flags, drv, &local_err); 1383b6ce07aaSKevin Wolf if (ret < 0) { 13848bfea15dSKevin Wolf goto fail; 13856987307cSChristoph Hellwig } 13866987307cSChristoph Hellwig 13872a05cbe4SMax Reitz if (file && (bs->file != file)) { 13884f6fd349SFam Zheng bdrv_unref(file); 1389f500a6d3SKevin Wolf file = NULL; 1390f500a6d3SKevin Wolf } 1391f500a6d3SKevin Wolf 1392b6ce07aaSKevin Wolf /* If there is a backing file, use it */ 13939156df12SPaolo Bonzini if ((flags & BDRV_O_NO_BACKING) == 0) { 139431ca6d07SKevin Wolf QDict *backing_options; 139531ca6d07SKevin Wolf 13965726d872SBenoît Canet qdict_extract_subqdict(options, &backing_options, "backing."); 139734b5d2c6SMax Reitz ret = bdrv_open_backing_file(bs, backing_options, &local_err); 1398b6ce07aaSKevin Wolf if (ret < 0) { 1399b6ad491aSKevin Wolf goto close_and_fail; 1400b6ce07aaSKevin Wolf } 1401b6ce07aaSKevin Wolf } 1402b6ce07aaSKevin Wolf 140391af7014SMax Reitz bdrv_refresh_filename(bs); 140491af7014SMax Reitz 1405b998875dSKevin Wolf /* For snapshot=on, create a temporary qcow2 overlay. bs points to the 1406b998875dSKevin Wolf * temporary snapshot afterwards. */ 1407b1e6fc08SKevin Wolf if (snapshot_flags) { 14086b8aeca5SChen Gang ret = bdrv_append_temp_snapshot(bs, snapshot_flags, &local_err); 1409b998875dSKevin Wolf if (local_err) { 1410b998875dSKevin Wolf goto close_and_fail; 1411b998875dSKevin Wolf } 1412b998875dSKevin Wolf } 1413b998875dSKevin Wolf 1414b6ad491aSKevin Wolf /* Check if any unknown options were used */ 14155acd9d81SMax Reitz if (options && (qdict_size(options) != 0)) { 1416b6ad491aSKevin Wolf const QDictEntry *entry = qdict_first(options); 14175acd9d81SMax Reitz if (flags & BDRV_O_PROTOCOL) { 14185acd9d81SMax Reitz error_setg(errp, "Block protocol '%s' doesn't support the option " 14195acd9d81SMax Reitz "'%s'", drv->format_name, entry->key); 14205acd9d81SMax Reitz } else { 142134b5d2c6SMax Reitz error_setg(errp, "Block format '%s' used by device '%s' doesn't " 14225acd9d81SMax Reitz "support the option '%s'", drv->format_name, 1423bfb197e0SMarkus Armbruster bdrv_get_device_name(bs), entry->key); 14245acd9d81SMax Reitz } 1425b6ad491aSKevin Wolf 1426b6ad491aSKevin Wolf ret = -EINVAL; 1427b6ad491aSKevin Wolf goto close_and_fail; 1428b6ad491aSKevin Wolf } 1429b6ad491aSKevin Wolf 1430b6ce07aaSKevin Wolf if (!bdrv_key_required(bs)) { 1431a7f53e26SMarkus Armbruster if (bs->blk) { 1432a7f53e26SMarkus Armbruster blk_dev_change_media_cb(bs->blk, true); 1433a7f53e26SMarkus Armbruster } 1434c3adb58fSMarkus Armbruster } else if (!runstate_check(RUN_STATE_PRELAUNCH) 1435c3adb58fSMarkus Armbruster && !runstate_check(RUN_STATE_INMIGRATE) 1436c3adb58fSMarkus Armbruster && !runstate_check(RUN_STATE_PAUSED)) { /* HACK */ 1437c3adb58fSMarkus Armbruster error_setg(errp, 1438c3adb58fSMarkus Armbruster "Guest must be stopped for opening of encrypted image"); 1439c3adb58fSMarkus Armbruster ret = -EBUSY; 1440c3adb58fSMarkus Armbruster goto close_and_fail; 1441b6ce07aaSKevin Wolf } 1442b6ce07aaSKevin Wolf 1443c3adb58fSMarkus Armbruster QDECREF(options); 1444f67503e5SMax Reitz *pbs = bs; 1445b6ce07aaSKevin Wolf return 0; 1446b6ce07aaSKevin Wolf 14478bfea15dSKevin Wolf fail: 1448f500a6d3SKevin Wolf if (file != NULL) { 14494f6fd349SFam Zheng bdrv_unref(file); 1450f500a6d3SKevin Wolf } 1451de9c0cecSKevin Wolf QDECREF(bs->options); 1452b6ad491aSKevin Wolf QDECREF(options); 1453de9c0cecSKevin Wolf bs->options = NULL; 1454f67503e5SMax Reitz if (!*pbs) { 1455f67503e5SMax Reitz /* If *pbs is NULL, a new BDS has been created in this function and 1456f67503e5SMax Reitz needs to be freed now. Otherwise, it does not need to be closed, 1457f67503e5SMax Reitz since it has not really been opened yet. */ 1458f67503e5SMax Reitz bdrv_unref(bs); 1459f67503e5SMax Reitz } 146084d18f06SMarkus Armbruster if (local_err) { 146134b5d2c6SMax Reitz error_propagate(errp, local_err); 146234b5d2c6SMax Reitz } 1463b6ad491aSKevin Wolf return ret; 1464de9c0cecSKevin Wolf 1465b6ad491aSKevin Wolf close_and_fail: 1466f67503e5SMax Reitz /* See fail path, but now the BDS has to be always closed */ 1467f67503e5SMax Reitz if (*pbs) { 1468b6ad491aSKevin Wolf bdrv_close(bs); 1469f67503e5SMax Reitz } else { 1470f67503e5SMax Reitz bdrv_unref(bs); 1471f67503e5SMax Reitz } 1472b6ad491aSKevin Wolf QDECREF(options); 147384d18f06SMarkus Armbruster if (local_err) { 147434b5d2c6SMax Reitz error_propagate(errp, local_err); 147534b5d2c6SMax Reitz } 1476b6ce07aaSKevin Wolf return ret; 1477b6ce07aaSKevin Wolf } 1478b6ce07aaSKevin Wolf 1479e971aa12SJeff Cody typedef struct BlockReopenQueueEntry { 1480e971aa12SJeff Cody bool prepared; 1481e971aa12SJeff Cody BDRVReopenState state; 1482e971aa12SJeff Cody QSIMPLEQ_ENTRY(BlockReopenQueueEntry) entry; 1483e971aa12SJeff Cody } BlockReopenQueueEntry; 1484e971aa12SJeff Cody 1485e971aa12SJeff Cody /* 1486e971aa12SJeff Cody * Adds a BlockDriverState to a simple queue for an atomic, transactional 1487e971aa12SJeff Cody * reopen of multiple devices. 1488e971aa12SJeff Cody * 1489e971aa12SJeff Cody * bs_queue can either be an existing BlockReopenQueue that has had QSIMPLE_INIT 1490e971aa12SJeff Cody * already performed, or alternatively may be NULL a new BlockReopenQueue will 1491e971aa12SJeff Cody * be created and initialized. This newly created BlockReopenQueue should be 1492e971aa12SJeff Cody * passed back in for subsequent calls that are intended to be of the same 1493e971aa12SJeff Cody * atomic 'set'. 1494e971aa12SJeff Cody * 1495e971aa12SJeff Cody * bs is the BlockDriverState to add to the reopen queue. 1496e971aa12SJeff Cody * 1497e971aa12SJeff Cody * flags contains the open flags for the associated bs 1498e971aa12SJeff Cody * 1499e971aa12SJeff Cody * returns a pointer to bs_queue, which is either the newly allocated 1500e971aa12SJeff Cody * bs_queue, or the existing bs_queue being used. 1501e971aa12SJeff Cody * 1502e971aa12SJeff Cody */ 1503e971aa12SJeff Cody BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue, 1504e971aa12SJeff Cody BlockDriverState *bs, int flags) 1505e971aa12SJeff Cody { 1506e971aa12SJeff Cody assert(bs != NULL); 1507e971aa12SJeff Cody 1508e971aa12SJeff Cody BlockReopenQueueEntry *bs_entry; 1509e971aa12SJeff Cody if (bs_queue == NULL) { 1510e971aa12SJeff Cody bs_queue = g_new0(BlockReopenQueue, 1); 1511e971aa12SJeff Cody QSIMPLEQ_INIT(bs_queue); 1512e971aa12SJeff Cody } 1513e971aa12SJeff Cody 1514f1f25a2eSKevin Wolf /* bdrv_open() masks this flag out */ 1515f1f25a2eSKevin Wolf flags &= ~BDRV_O_PROTOCOL; 1516f1f25a2eSKevin Wolf 1517e971aa12SJeff Cody if (bs->file) { 1518f1f25a2eSKevin Wolf bdrv_reopen_queue(bs_queue, bs->file, bdrv_inherited_flags(flags)); 1519e971aa12SJeff Cody } 1520e971aa12SJeff Cody 1521e971aa12SJeff Cody bs_entry = g_new0(BlockReopenQueueEntry, 1); 1522e971aa12SJeff Cody QSIMPLEQ_INSERT_TAIL(bs_queue, bs_entry, entry); 1523e971aa12SJeff Cody 1524e971aa12SJeff Cody bs_entry->state.bs = bs; 1525e971aa12SJeff Cody bs_entry->state.flags = flags; 1526e971aa12SJeff Cody 1527e971aa12SJeff Cody return bs_queue; 1528e971aa12SJeff Cody } 1529e971aa12SJeff Cody 1530e971aa12SJeff Cody /* 1531e971aa12SJeff Cody * Reopen multiple BlockDriverStates atomically & transactionally. 1532e971aa12SJeff Cody * 1533e971aa12SJeff Cody * The queue passed in (bs_queue) must have been built up previous 1534e971aa12SJeff Cody * via bdrv_reopen_queue(). 1535e971aa12SJeff Cody * 1536e971aa12SJeff Cody * Reopens all BDS specified in the queue, with the appropriate 1537e971aa12SJeff Cody * flags. All devices are prepared for reopen, and failure of any 1538e971aa12SJeff Cody * device will cause all device changes to be abandonded, and intermediate 1539e971aa12SJeff Cody * data cleaned up. 1540e971aa12SJeff Cody * 1541e971aa12SJeff Cody * If all devices prepare successfully, then the changes are committed 1542e971aa12SJeff Cody * to all devices. 1543e971aa12SJeff Cody * 1544e971aa12SJeff Cody */ 1545e971aa12SJeff Cody int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp) 1546e971aa12SJeff Cody { 1547e971aa12SJeff Cody int ret = -1; 1548e971aa12SJeff Cody BlockReopenQueueEntry *bs_entry, *next; 1549e971aa12SJeff Cody Error *local_err = NULL; 1550e971aa12SJeff Cody 1551e971aa12SJeff Cody assert(bs_queue != NULL); 1552e971aa12SJeff Cody 1553e971aa12SJeff Cody bdrv_drain_all(); 1554e971aa12SJeff Cody 1555e971aa12SJeff Cody QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) { 1556e971aa12SJeff Cody if (bdrv_reopen_prepare(&bs_entry->state, bs_queue, &local_err)) { 1557e971aa12SJeff Cody error_propagate(errp, local_err); 1558e971aa12SJeff Cody goto cleanup; 1559e971aa12SJeff Cody } 1560e971aa12SJeff Cody bs_entry->prepared = true; 1561e971aa12SJeff Cody } 1562e971aa12SJeff Cody 1563e971aa12SJeff Cody /* If we reach this point, we have success and just need to apply the 1564e971aa12SJeff Cody * changes 1565e971aa12SJeff Cody */ 1566e971aa12SJeff Cody QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) { 1567e971aa12SJeff Cody bdrv_reopen_commit(&bs_entry->state); 1568e971aa12SJeff Cody } 1569e971aa12SJeff Cody 1570e971aa12SJeff Cody ret = 0; 1571e971aa12SJeff Cody 1572e971aa12SJeff Cody cleanup: 1573e971aa12SJeff Cody QSIMPLEQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) { 1574e971aa12SJeff Cody if (ret && bs_entry->prepared) { 1575e971aa12SJeff Cody bdrv_reopen_abort(&bs_entry->state); 1576e971aa12SJeff Cody } 1577e971aa12SJeff Cody g_free(bs_entry); 1578e971aa12SJeff Cody } 1579e971aa12SJeff Cody g_free(bs_queue); 1580e971aa12SJeff Cody return ret; 1581e971aa12SJeff Cody } 1582e971aa12SJeff Cody 1583e971aa12SJeff Cody 1584e971aa12SJeff Cody /* Reopen a single BlockDriverState with the specified flags. */ 1585e971aa12SJeff Cody int bdrv_reopen(BlockDriverState *bs, int bdrv_flags, Error **errp) 1586e971aa12SJeff Cody { 1587e971aa12SJeff Cody int ret = -1; 1588e971aa12SJeff Cody Error *local_err = NULL; 1589e971aa12SJeff Cody BlockReopenQueue *queue = bdrv_reopen_queue(NULL, bs, bdrv_flags); 1590e971aa12SJeff Cody 1591e971aa12SJeff Cody ret = bdrv_reopen_multiple(queue, &local_err); 1592e971aa12SJeff Cody if (local_err != NULL) { 1593e971aa12SJeff Cody error_propagate(errp, local_err); 1594e971aa12SJeff Cody } 1595e971aa12SJeff Cody return ret; 1596e971aa12SJeff Cody } 1597e971aa12SJeff Cody 1598e971aa12SJeff Cody 1599e971aa12SJeff Cody /* 1600e971aa12SJeff Cody * Prepares a BlockDriverState for reopen. All changes are staged in the 1601e971aa12SJeff Cody * 'opaque' field of the BDRVReopenState, which is used and allocated by 1602e971aa12SJeff Cody * the block driver layer .bdrv_reopen_prepare() 1603e971aa12SJeff Cody * 1604e971aa12SJeff Cody * bs is the BlockDriverState to reopen 1605e971aa12SJeff Cody * flags are the new open flags 1606e971aa12SJeff Cody * queue is the reopen queue 1607e971aa12SJeff Cody * 1608e971aa12SJeff Cody * Returns 0 on success, non-zero on error. On error errp will be set 1609e971aa12SJeff Cody * as well. 1610e971aa12SJeff Cody * 1611e971aa12SJeff Cody * On failure, bdrv_reopen_abort() will be called to clean up any data. 1612e971aa12SJeff Cody * It is the responsibility of the caller to then call the abort() or 1613e971aa12SJeff Cody * commit() for any other BDS that have been left in a prepare() state 1614e971aa12SJeff Cody * 1615e971aa12SJeff Cody */ 1616e971aa12SJeff Cody int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue, 1617e971aa12SJeff Cody Error **errp) 1618e971aa12SJeff Cody { 1619e971aa12SJeff Cody int ret = -1; 1620e971aa12SJeff Cody Error *local_err = NULL; 1621e971aa12SJeff Cody BlockDriver *drv; 1622e971aa12SJeff Cody 1623e971aa12SJeff Cody assert(reopen_state != NULL); 1624e971aa12SJeff Cody assert(reopen_state->bs->drv != NULL); 1625e971aa12SJeff Cody drv = reopen_state->bs->drv; 1626e971aa12SJeff Cody 1627e971aa12SJeff Cody /* if we are to stay read-only, do not allow permission change 1628e971aa12SJeff Cody * to r/w */ 1629e971aa12SJeff Cody if (!(reopen_state->bs->open_flags & BDRV_O_ALLOW_RDWR) && 1630e971aa12SJeff Cody reopen_state->flags & BDRV_O_RDWR) { 163181e5f78aSAlberto Garcia error_setg(errp, "Node '%s' is read only", 163281e5f78aSAlberto Garcia bdrv_get_device_or_node_name(reopen_state->bs)); 1633e971aa12SJeff Cody goto error; 1634e971aa12SJeff Cody } 1635e971aa12SJeff Cody 1636e971aa12SJeff Cody 1637e971aa12SJeff Cody ret = bdrv_flush(reopen_state->bs); 1638e971aa12SJeff Cody if (ret) { 1639e971aa12SJeff Cody error_set(errp, ERROR_CLASS_GENERIC_ERROR, "Error (%s) flushing drive", 1640e971aa12SJeff Cody strerror(-ret)); 1641e971aa12SJeff Cody goto error; 1642e971aa12SJeff Cody } 1643e971aa12SJeff Cody 1644e971aa12SJeff Cody if (drv->bdrv_reopen_prepare) { 1645e971aa12SJeff Cody ret = drv->bdrv_reopen_prepare(reopen_state, queue, &local_err); 1646e971aa12SJeff Cody if (ret) { 1647e971aa12SJeff Cody if (local_err != NULL) { 1648e971aa12SJeff Cody error_propagate(errp, local_err); 1649e971aa12SJeff Cody } else { 1650d8b6895fSLuiz Capitulino error_setg(errp, "failed while preparing to reopen image '%s'", 1651e971aa12SJeff Cody reopen_state->bs->filename); 1652e971aa12SJeff Cody } 1653e971aa12SJeff Cody goto error; 1654e971aa12SJeff Cody } 1655e971aa12SJeff Cody } else { 1656e971aa12SJeff Cody /* It is currently mandatory to have a bdrv_reopen_prepare() 1657e971aa12SJeff Cody * handler for each supported drv. */ 165881e5f78aSAlberto Garcia error_setg(errp, "Block format '%s' used by node '%s' " 165981e5f78aSAlberto Garcia "does not support reopening files", drv->format_name, 166081e5f78aSAlberto Garcia bdrv_get_device_or_node_name(reopen_state->bs)); 1661e971aa12SJeff Cody ret = -1; 1662e971aa12SJeff Cody goto error; 1663e971aa12SJeff Cody } 1664e971aa12SJeff Cody 1665e971aa12SJeff Cody ret = 0; 1666e971aa12SJeff Cody 1667e971aa12SJeff Cody error: 1668e971aa12SJeff Cody return ret; 1669e971aa12SJeff Cody } 1670e971aa12SJeff Cody 1671e971aa12SJeff Cody /* 1672e971aa12SJeff Cody * Takes the staged changes for the reopen from bdrv_reopen_prepare(), and 1673e971aa12SJeff Cody * makes them final by swapping the staging BlockDriverState contents into 1674e971aa12SJeff Cody * the active BlockDriverState contents. 1675e971aa12SJeff Cody */ 1676e971aa12SJeff Cody void bdrv_reopen_commit(BDRVReopenState *reopen_state) 1677e971aa12SJeff Cody { 1678e971aa12SJeff Cody BlockDriver *drv; 1679e971aa12SJeff Cody 1680e971aa12SJeff Cody assert(reopen_state != NULL); 1681e971aa12SJeff Cody drv = reopen_state->bs->drv; 1682e971aa12SJeff Cody assert(drv != NULL); 1683e971aa12SJeff Cody 1684e971aa12SJeff Cody /* If there are any driver level actions to take */ 1685e971aa12SJeff Cody if (drv->bdrv_reopen_commit) { 1686e971aa12SJeff Cody drv->bdrv_reopen_commit(reopen_state); 1687e971aa12SJeff Cody } 1688e971aa12SJeff Cody 1689e971aa12SJeff Cody /* set BDS specific flags now */ 1690e971aa12SJeff Cody reopen_state->bs->open_flags = reopen_state->flags; 1691e971aa12SJeff Cody reopen_state->bs->enable_write_cache = !!(reopen_state->flags & 1692e971aa12SJeff Cody BDRV_O_CACHE_WB); 1693e971aa12SJeff Cody reopen_state->bs->read_only = !(reopen_state->flags & BDRV_O_RDWR); 1694355ef4acSKevin Wolf 16953baca891SKevin Wolf bdrv_refresh_limits(reopen_state->bs, NULL); 1696e971aa12SJeff Cody } 1697e971aa12SJeff Cody 1698e971aa12SJeff Cody /* 1699e971aa12SJeff Cody * Abort the reopen, and delete and free the staged changes in 1700e971aa12SJeff Cody * reopen_state 1701e971aa12SJeff Cody */ 1702e971aa12SJeff Cody void bdrv_reopen_abort(BDRVReopenState *reopen_state) 1703e971aa12SJeff Cody { 1704e971aa12SJeff Cody BlockDriver *drv; 1705e971aa12SJeff Cody 1706e971aa12SJeff Cody assert(reopen_state != NULL); 1707e971aa12SJeff Cody drv = reopen_state->bs->drv; 1708e971aa12SJeff Cody assert(drv != NULL); 1709e971aa12SJeff Cody 1710e971aa12SJeff Cody if (drv->bdrv_reopen_abort) { 1711e971aa12SJeff Cody drv->bdrv_reopen_abort(reopen_state); 1712e971aa12SJeff Cody } 1713e971aa12SJeff Cody } 1714e971aa12SJeff Cody 1715e971aa12SJeff Cody 1716fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs) 1717fc01f7e7Sbellard { 171833384421SMax Reitz BdrvAioNotifier *ban, *ban_next; 171933384421SMax Reitz 17203e914655SPaolo Bonzini if (bs->job) { 17213e914655SPaolo Bonzini block_job_cancel_sync(bs->job); 17223e914655SPaolo Bonzini } 172358fda173SStefan Hajnoczi bdrv_drain_all(); /* complete I/O */ 172458fda173SStefan Hajnoczi bdrv_flush(bs); 172558fda173SStefan Hajnoczi bdrv_drain_all(); /* in case flush left pending I/O */ 1726d7d512f6SPaolo Bonzini notifier_list_notify(&bs->close_notifiers, bs); 17277094f12fSKevin Wolf 17283cbc002cSPaolo Bonzini if (bs->drv) { 1729557df6acSStefan Hajnoczi if (bs->backing_hd) { 1730826b6ca0SFam Zheng BlockDriverState *backing_hd = bs->backing_hd; 1731826b6ca0SFam Zheng bdrv_set_backing_hd(bs, NULL); 1732826b6ca0SFam Zheng bdrv_unref(backing_hd); 1733557df6acSStefan Hajnoczi } 1734ea2384d3Sbellard bs->drv->bdrv_close(bs); 17357267c094SAnthony Liguori g_free(bs->opaque); 1736ea2384d3Sbellard bs->opaque = NULL; 1737ea2384d3Sbellard bs->drv = NULL; 173853fec9d3SStefan Hajnoczi bs->copy_on_read = 0; 1739a275fa42SPaolo Bonzini bs->backing_file[0] = '\0'; 1740a275fa42SPaolo Bonzini bs->backing_format[0] = '\0'; 17416405875cSPaolo Bonzini bs->total_sectors = 0; 17426405875cSPaolo Bonzini bs->encrypted = 0; 17436405875cSPaolo Bonzini bs->valid_key = 0; 17446405875cSPaolo Bonzini bs->sg = 0; 17450d51b4deSAsias He bs->zero_beyond_eof = false; 1746de9c0cecSKevin Wolf QDECREF(bs->options); 1747de9c0cecSKevin Wolf bs->options = NULL; 174891af7014SMax Reitz QDECREF(bs->full_open_options); 174991af7014SMax Reitz bs->full_open_options = NULL; 1750b338082bSbellard 175166f82ceeSKevin Wolf if (bs->file != NULL) { 17524f6fd349SFam Zheng bdrv_unref(bs->file); 17530ac9377dSPaolo Bonzini bs->file = NULL; 175466f82ceeSKevin Wolf } 17559ca11154SPavel Hrdina } 175666f82ceeSKevin Wolf 1757a7f53e26SMarkus Armbruster if (bs->blk) { 1758a7f53e26SMarkus Armbruster blk_dev_change_media_cb(bs->blk, false); 1759a7f53e26SMarkus Armbruster } 176098f90dbaSZhi Yong Wu 176198f90dbaSZhi Yong Wu /*throttling disk I/O limits*/ 176298f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 176398f90dbaSZhi Yong Wu bdrv_io_limits_disable(bs); 176498f90dbaSZhi Yong Wu } 176533384421SMax Reitz 176633384421SMax Reitz QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) { 176733384421SMax Reitz g_free(ban); 176833384421SMax Reitz } 176933384421SMax Reitz QLIST_INIT(&bs->aio_notifiers); 1770b338082bSbellard } 1771b338082bSbellard 17722bc93fedSMORITA Kazutaka void bdrv_close_all(void) 17732bc93fedSMORITA Kazutaka { 17742bc93fedSMORITA Kazutaka BlockDriverState *bs; 17752bc93fedSMORITA Kazutaka 1776dc364f4cSBenoît Canet QTAILQ_FOREACH(bs, &bdrv_states, device_list) { 1777ed78cda3SStefan Hajnoczi AioContext *aio_context = bdrv_get_aio_context(bs); 1778ed78cda3SStefan Hajnoczi 1779ed78cda3SStefan Hajnoczi aio_context_acquire(aio_context); 17802bc93fedSMORITA Kazutaka bdrv_close(bs); 1781ed78cda3SStefan Hajnoczi aio_context_release(aio_context); 17822bc93fedSMORITA Kazutaka } 17832bc93fedSMORITA Kazutaka } 17842bc93fedSMORITA Kazutaka 1785dc364f4cSBenoît Canet /* make a BlockDriverState anonymous by removing from bdrv_state and 1786dc364f4cSBenoît Canet * graph_bdrv_state list. 1787d22b2f41SRyan Harper Also, NULL terminate the device_name to prevent double remove */ 1788d22b2f41SRyan Harper void bdrv_make_anon(BlockDriverState *bs) 1789d22b2f41SRyan Harper { 1790bfb197e0SMarkus Armbruster /* 1791bfb197e0SMarkus Armbruster * Take care to remove bs from bdrv_states only when it's actually 1792bfb197e0SMarkus Armbruster * in it. Note that bs->device_list.tqe_prev is initially null, 1793bfb197e0SMarkus Armbruster * and gets set to non-null by QTAILQ_INSERT_TAIL(). Establish 1794bfb197e0SMarkus Armbruster * the useful invariant "bs in bdrv_states iff bs->tqe_prev" by 1795bfb197e0SMarkus Armbruster * resetting it to null on remove. 1796bfb197e0SMarkus Armbruster */ 1797bfb197e0SMarkus Armbruster if (bs->device_list.tqe_prev) { 1798dc364f4cSBenoît Canet QTAILQ_REMOVE(&bdrv_states, bs, device_list); 1799bfb197e0SMarkus Armbruster bs->device_list.tqe_prev = NULL; 1800d22b2f41SRyan Harper } 1801dc364f4cSBenoît Canet if (bs->node_name[0] != '\0') { 1802dc364f4cSBenoît Canet QTAILQ_REMOVE(&graph_bdrv_states, bs, node_list); 1803dc364f4cSBenoît Canet } 1804dc364f4cSBenoît Canet bs->node_name[0] = '\0'; 1805d22b2f41SRyan Harper } 1806d22b2f41SRyan Harper 1807e023b2e2SPaolo Bonzini static void bdrv_rebind(BlockDriverState *bs) 1808e023b2e2SPaolo Bonzini { 1809e023b2e2SPaolo Bonzini if (bs->drv && bs->drv->bdrv_rebind) { 1810e023b2e2SPaolo Bonzini bs->drv->bdrv_rebind(bs); 1811e023b2e2SPaolo Bonzini } 1812e023b2e2SPaolo Bonzini } 1813e023b2e2SPaolo Bonzini 18144ddc07caSPaolo Bonzini static void bdrv_move_feature_fields(BlockDriverState *bs_dest, 18154ddc07caSPaolo Bonzini BlockDriverState *bs_src) 18164ddc07caSPaolo Bonzini { 18174ddc07caSPaolo Bonzini /* move some fields that need to stay attached to the device */ 18184ddc07caSPaolo Bonzini 18194ddc07caSPaolo Bonzini /* dev info */ 18201b7fd729SPaolo Bonzini bs_dest->guest_block_size = bs_src->guest_block_size; 18214ddc07caSPaolo Bonzini bs_dest->copy_on_read = bs_src->copy_on_read; 18224ddc07caSPaolo Bonzini 18234ddc07caSPaolo Bonzini bs_dest->enable_write_cache = bs_src->enable_write_cache; 18244ddc07caSPaolo Bonzini 1825cc0681c4SBenoît Canet /* i/o throttled req */ 182676f4afb4SAlberto Garcia bs_dest->throttle_state = bs_src->throttle_state, 182776f4afb4SAlberto Garcia bs_dest->io_limits_enabled = bs_src->io_limits_enabled; 182876f4afb4SAlberto Garcia bs_dest->pending_reqs[0] = bs_src->pending_reqs[0]; 182976f4afb4SAlberto Garcia bs_dest->pending_reqs[1] = bs_src->pending_reqs[1]; 183076f4afb4SAlberto Garcia bs_dest->throttled_reqs[0] = bs_src->throttled_reqs[0]; 183176f4afb4SAlberto Garcia bs_dest->throttled_reqs[1] = bs_src->throttled_reqs[1]; 183276f4afb4SAlberto Garcia memcpy(&bs_dest->round_robin, 183376f4afb4SAlberto Garcia &bs_src->round_robin, 183476f4afb4SAlberto Garcia sizeof(bs_dest->round_robin)); 18350e5b0a2dSBenoît Canet memcpy(&bs_dest->throttle_timers, 18360e5b0a2dSBenoît Canet &bs_src->throttle_timers, 18370e5b0a2dSBenoît Canet sizeof(ThrottleTimers)); 18384ddc07caSPaolo Bonzini 18394ddc07caSPaolo Bonzini /* r/w error */ 18404ddc07caSPaolo Bonzini bs_dest->on_read_error = bs_src->on_read_error; 18414ddc07caSPaolo Bonzini bs_dest->on_write_error = bs_src->on_write_error; 18424ddc07caSPaolo Bonzini 18434ddc07caSPaolo Bonzini /* i/o status */ 18444ddc07caSPaolo Bonzini bs_dest->iostatus_enabled = bs_src->iostatus_enabled; 18454ddc07caSPaolo Bonzini bs_dest->iostatus = bs_src->iostatus; 18464ddc07caSPaolo Bonzini 18474ddc07caSPaolo Bonzini /* dirty bitmap */ 1848e4654d2dSFam Zheng bs_dest->dirty_bitmaps = bs_src->dirty_bitmaps; 18494ddc07caSPaolo Bonzini 18509fcb0251SFam Zheng /* reference count */ 18519fcb0251SFam Zheng bs_dest->refcnt = bs_src->refcnt; 18529fcb0251SFam Zheng 18534ddc07caSPaolo Bonzini /* job */ 18544ddc07caSPaolo Bonzini bs_dest->job = bs_src->job; 18554ddc07caSPaolo Bonzini 18564ddc07caSPaolo Bonzini /* keep the same entry in bdrv_states */ 1857dc364f4cSBenoît Canet bs_dest->device_list = bs_src->device_list; 18587e7d56d9SMarkus Armbruster bs_dest->blk = bs_src->blk; 18597e7d56d9SMarkus Armbruster 1860fbe40ff7SFam Zheng memcpy(bs_dest->op_blockers, bs_src->op_blockers, 1861fbe40ff7SFam Zheng sizeof(bs_dest->op_blockers)); 18624ddc07caSPaolo Bonzini } 18634ddc07caSPaolo Bonzini 18644ddc07caSPaolo Bonzini /* 18654ddc07caSPaolo Bonzini * Swap bs contents for two image chains while they are live, 18664ddc07caSPaolo Bonzini * while keeping required fields on the BlockDriverState that is 18674ddc07caSPaolo Bonzini * actually attached to a device. 18684ddc07caSPaolo Bonzini * 18694ddc07caSPaolo Bonzini * This will modify the BlockDriverState fields, and swap contents 18704ddc07caSPaolo Bonzini * between bs_new and bs_old. Both bs_new and bs_old are modified. 18714ddc07caSPaolo Bonzini * 1872bfb197e0SMarkus Armbruster * bs_new must not be attached to a BlockBackend. 18734ddc07caSPaolo Bonzini * 18744ddc07caSPaolo Bonzini * This function does not create any image files. 18754ddc07caSPaolo Bonzini */ 18764ddc07caSPaolo Bonzini void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old) 18774ddc07caSPaolo Bonzini { 18784ddc07caSPaolo Bonzini BlockDriverState tmp; 18794ddc07caSPaolo Bonzini 188090ce8a06SBenoît Canet /* The code needs to swap the node_name but simply swapping node_list won't 188190ce8a06SBenoît Canet * work so first remove the nodes from the graph list, do the swap then 188290ce8a06SBenoît Canet * insert them back if needed. 188390ce8a06SBenoît Canet */ 188490ce8a06SBenoît Canet if (bs_new->node_name[0] != '\0') { 188590ce8a06SBenoît Canet QTAILQ_REMOVE(&graph_bdrv_states, bs_new, node_list); 188690ce8a06SBenoît Canet } 188790ce8a06SBenoît Canet if (bs_old->node_name[0] != '\0') { 188890ce8a06SBenoît Canet QTAILQ_REMOVE(&graph_bdrv_states, bs_old, node_list); 188990ce8a06SBenoît Canet } 189090ce8a06SBenoît Canet 1891*db628338SAlberto Garcia /* If the BlockDriverState is part of a throttling group acquire 1892*db628338SAlberto Garcia * its lock since we're going to mess with the protected fields. 1893*db628338SAlberto Garcia * Otherwise there's no need to worry since no one else can touch 1894*db628338SAlberto Garcia * them. */ 1895*db628338SAlberto Garcia if (bs_old->throttle_state) { 1896*db628338SAlberto Garcia throttle_group_lock(bs_old); 1897*db628338SAlberto Garcia } 1898*db628338SAlberto Garcia 1899bfb197e0SMarkus Armbruster /* bs_new must be unattached and shouldn't have anything fancy enabled */ 19007e7d56d9SMarkus Armbruster assert(!bs_new->blk); 1901e4654d2dSFam Zheng assert(QLIST_EMPTY(&bs_new->dirty_bitmaps)); 19024ddc07caSPaolo Bonzini assert(bs_new->job == NULL); 19034ddc07caSPaolo Bonzini assert(bs_new->io_limits_enabled == false); 1904*db628338SAlberto Garcia assert(bs_new->throttle_state == NULL); 19050e5b0a2dSBenoît Canet assert(!throttle_timers_are_initialized(&bs_new->throttle_timers)); 19064ddc07caSPaolo Bonzini 19074ddc07caSPaolo Bonzini tmp = *bs_new; 19084ddc07caSPaolo Bonzini *bs_new = *bs_old; 19094ddc07caSPaolo Bonzini *bs_old = tmp; 19104ddc07caSPaolo Bonzini 19114ddc07caSPaolo Bonzini /* there are some fields that should not be swapped, move them back */ 19124ddc07caSPaolo Bonzini bdrv_move_feature_fields(&tmp, bs_old); 19134ddc07caSPaolo Bonzini bdrv_move_feature_fields(bs_old, bs_new); 19144ddc07caSPaolo Bonzini bdrv_move_feature_fields(bs_new, &tmp); 19154ddc07caSPaolo Bonzini 1916bfb197e0SMarkus Armbruster /* bs_new must remain unattached */ 19177e7d56d9SMarkus Armbruster assert(!bs_new->blk); 19184ddc07caSPaolo Bonzini 19194ddc07caSPaolo Bonzini /* Check a few fields that should remain attached to the device */ 19204ddc07caSPaolo Bonzini assert(bs_new->job == NULL); 19214ddc07caSPaolo Bonzini assert(bs_new->io_limits_enabled == false); 1922*db628338SAlberto Garcia assert(bs_new->throttle_state == NULL); 19230e5b0a2dSBenoît Canet assert(!throttle_timers_are_initialized(&bs_new->throttle_timers)); 19244ddc07caSPaolo Bonzini 1925*db628338SAlberto Garcia /* Release the ThrottleGroup lock */ 1926*db628338SAlberto Garcia if (bs_old->throttle_state) { 1927*db628338SAlberto Garcia throttle_group_unlock(bs_old); 1928*db628338SAlberto Garcia } 1929*db628338SAlberto Garcia 193090ce8a06SBenoît Canet /* insert the nodes back into the graph node list if needed */ 193190ce8a06SBenoît Canet if (bs_new->node_name[0] != '\0') { 193290ce8a06SBenoît Canet QTAILQ_INSERT_TAIL(&graph_bdrv_states, bs_new, node_list); 193390ce8a06SBenoît Canet } 193490ce8a06SBenoît Canet if (bs_old->node_name[0] != '\0') { 193590ce8a06SBenoît Canet QTAILQ_INSERT_TAIL(&graph_bdrv_states, bs_old, node_list); 193690ce8a06SBenoît Canet } 193790ce8a06SBenoît Canet 19384ddc07caSPaolo Bonzini bdrv_rebind(bs_new); 19394ddc07caSPaolo Bonzini bdrv_rebind(bs_old); 19404ddc07caSPaolo Bonzini } 19414ddc07caSPaolo Bonzini 19428802d1fdSJeff Cody /* 19438802d1fdSJeff Cody * Add new bs contents at the top of an image chain while the chain is 19448802d1fdSJeff Cody * live, while keeping required fields on the top layer. 19458802d1fdSJeff Cody * 19468802d1fdSJeff Cody * This will modify the BlockDriverState fields, and swap contents 19478802d1fdSJeff Cody * between bs_new and bs_top. Both bs_new and bs_top are modified. 19488802d1fdSJeff Cody * 1949bfb197e0SMarkus Armbruster * bs_new must not be attached to a BlockBackend. 1950f6801b83SJeff Cody * 19518802d1fdSJeff Cody * This function does not create any image files. 19528802d1fdSJeff Cody */ 19538802d1fdSJeff Cody void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top) 19548802d1fdSJeff Cody { 19554ddc07caSPaolo Bonzini bdrv_swap(bs_new, bs_top); 19568802d1fdSJeff Cody 19578802d1fdSJeff Cody /* The contents of 'tmp' will become bs_top, as we are 19588802d1fdSJeff Cody * swapping bs_new and bs_top contents. */ 19598d24cce1SFam Zheng bdrv_set_backing_hd(bs_top, bs_new); 19608802d1fdSJeff Cody } 19618802d1fdSJeff Cody 19624f6fd349SFam Zheng static void bdrv_delete(BlockDriverState *bs) 1963b338082bSbellard { 19643e914655SPaolo Bonzini assert(!bs->job); 19653718d8abSFam Zheng assert(bdrv_op_blocker_is_empty(bs)); 19664f6fd349SFam Zheng assert(!bs->refcnt); 1967e4654d2dSFam Zheng assert(QLIST_EMPTY(&bs->dirty_bitmaps)); 196818846deeSMarkus Armbruster 1969e1b5c52eSStefan Hajnoczi bdrv_close(bs); 1970e1b5c52eSStefan Hajnoczi 19711b7bdbc1SStefan Hajnoczi /* remove from list, if necessary */ 1972d22b2f41SRyan Harper bdrv_make_anon(bs); 197334c6f050Saurel32 19747267c094SAnthony Liguori g_free(bs); 1975fc01f7e7Sbellard } 1976fc01f7e7Sbellard 1977e97fc193Saliguori /* 1978e97fc193Saliguori * Run consistency checks on an image 1979e97fc193Saliguori * 1980e076f338SKevin Wolf * Returns 0 if the check could be completed (it doesn't mean that the image is 1981a1c7273bSStefan Weil * free of errors) or -errno when an internal error occurred. The results of the 1982e076f338SKevin Wolf * check are stored in res. 1983e97fc193Saliguori */ 19844534ff54SKevin Wolf int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix) 1985e97fc193Saliguori { 1986908bcd54SMax Reitz if (bs->drv == NULL) { 1987908bcd54SMax Reitz return -ENOMEDIUM; 1988908bcd54SMax Reitz } 1989e97fc193Saliguori if (bs->drv->bdrv_check == NULL) { 1990e97fc193Saliguori return -ENOTSUP; 1991e97fc193Saliguori } 1992e97fc193Saliguori 1993e076f338SKevin Wolf memset(res, 0, sizeof(*res)); 19944534ff54SKevin Wolf return bs->drv->bdrv_check(bs, res, fix); 1995e97fc193Saliguori } 1996e97fc193Saliguori 19978a426614SKevin Wolf #define COMMIT_BUF_SECTORS 2048 19988a426614SKevin Wolf 199933e3963eSbellard /* commit COW file into the raw image */ 200033e3963eSbellard int bdrv_commit(BlockDriverState *bs) 200133e3963eSbellard { 200219cb3738Sbellard BlockDriver *drv = bs->drv; 200372706ea4SJeff Cody int64_t sector, total_sectors, length, backing_length; 20048a426614SKevin Wolf int n, ro, open_flags; 20050bce597dSJeff Cody int ret = 0; 200672706ea4SJeff Cody uint8_t *buf = NULL; 200733e3963eSbellard 200819cb3738Sbellard if (!drv) 200919cb3738Sbellard return -ENOMEDIUM; 201033e3963eSbellard 20114dca4b63SNaphtali Sprei if (!bs->backing_hd) { 20124dca4b63SNaphtali Sprei return -ENOTSUP; 20134dca4b63SNaphtali Sprei } 20144dca4b63SNaphtali Sprei 2015bb00021dSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, NULL) || 2016bb00021dSFam Zheng bdrv_op_is_blocked(bs->backing_hd, BLOCK_OP_TYPE_COMMIT_TARGET, NULL)) { 20172d3735d3SStefan Hajnoczi return -EBUSY; 20182d3735d3SStefan Hajnoczi } 20192d3735d3SStefan Hajnoczi 20204dca4b63SNaphtali Sprei ro = bs->backing_hd->read_only; 20214dca4b63SNaphtali Sprei open_flags = bs->backing_hd->open_flags; 20224dca4b63SNaphtali Sprei 20234dca4b63SNaphtali Sprei if (ro) { 20240bce597dSJeff Cody if (bdrv_reopen(bs->backing_hd, open_flags | BDRV_O_RDWR, NULL)) { 20250bce597dSJeff Cody return -EACCES; 20264dca4b63SNaphtali Sprei } 2027ea2384d3Sbellard } 2028ea2384d3Sbellard 202972706ea4SJeff Cody length = bdrv_getlength(bs); 203072706ea4SJeff Cody if (length < 0) { 203172706ea4SJeff Cody ret = length; 203272706ea4SJeff Cody goto ro_cleanup; 203372706ea4SJeff Cody } 203472706ea4SJeff Cody 203572706ea4SJeff Cody backing_length = bdrv_getlength(bs->backing_hd); 203672706ea4SJeff Cody if (backing_length < 0) { 203772706ea4SJeff Cody ret = backing_length; 203872706ea4SJeff Cody goto ro_cleanup; 203972706ea4SJeff Cody } 204072706ea4SJeff Cody 204172706ea4SJeff Cody /* If our top snapshot is larger than the backing file image, 204272706ea4SJeff Cody * grow the backing file image if possible. If not possible, 204372706ea4SJeff Cody * we must return an error */ 204472706ea4SJeff Cody if (length > backing_length) { 204572706ea4SJeff Cody ret = bdrv_truncate(bs->backing_hd, length); 204672706ea4SJeff Cody if (ret < 0) { 204772706ea4SJeff Cody goto ro_cleanup; 204872706ea4SJeff Cody } 204972706ea4SJeff Cody } 205072706ea4SJeff Cody 205172706ea4SJeff Cody total_sectors = length >> BDRV_SECTOR_BITS; 2052857d4f46SKevin Wolf 2053857d4f46SKevin Wolf /* qemu_try_blockalign() for bs will choose an alignment that works for 2054857d4f46SKevin Wolf * bs->backing_hd as well, so no need to compare the alignment manually. */ 2055857d4f46SKevin Wolf buf = qemu_try_blockalign(bs, COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE); 2056857d4f46SKevin Wolf if (buf == NULL) { 2057857d4f46SKevin Wolf ret = -ENOMEM; 2058857d4f46SKevin Wolf goto ro_cleanup; 2059857d4f46SKevin Wolf } 20608a426614SKevin Wolf 20618a426614SKevin Wolf for (sector = 0; sector < total_sectors; sector += n) { 2062d663640cSPaolo Bonzini ret = bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n); 2063d663640cSPaolo Bonzini if (ret < 0) { 2064d663640cSPaolo Bonzini goto ro_cleanup; 2065d663640cSPaolo Bonzini } 2066d663640cSPaolo Bonzini if (ret) { 2067dabfa6ccSKevin Wolf ret = bdrv_read(bs, sector, buf, n); 2068dabfa6ccSKevin Wolf if (ret < 0) { 20694dca4b63SNaphtali Sprei goto ro_cleanup; 207033e3963eSbellard } 207133e3963eSbellard 2072dabfa6ccSKevin Wolf ret = bdrv_write(bs->backing_hd, sector, buf, n); 2073dabfa6ccSKevin Wolf if (ret < 0) { 20744dca4b63SNaphtali Sprei goto ro_cleanup; 207533e3963eSbellard } 207633e3963eSbellard } 207733e3963eSbellard } 207895389c86Sbellard 20791d44952fSChristoph Hellwig if (drv->bdrv_make_empty) { 20801d44952fSChristoph Hellwig ret = drv->bdrv_make_empty(bs); 2081dabfa6ccSKevin Wolf if (ret < 0) { 2082dabfa6ccSKevin Wolf goto ro_cleanup; 2083dabfa6ccSKevin Wolf } 20841d44952fSChristoph Hellwig bdrv_flush(bs); 20851d44952fSChristoph Hellwig } 208695389c86Sbellard 20873f5075aeSChristoph Hellwig /* 20883f5075aeSChristoph Hellwig * Make sure all data we wrote to the backing device is actually 20893f5075aeSChristoph Hellwig * stable on disk. 20903f5075aeSChristoph Hellwig */ 2091dabfa6ccSKevin Wolf if (bs->backing_hd) { 20923f5075aeSChristoph Hellwig bdrv_flush(bs->backing_hd); 2093dabfa6ccSKevin Wolf } 20944dca4b63SNaphtali Sprei 2095dabfa6ccSKevin Wolf ret = 0; 20964dca4b63SNaphtali Sprei ro_cleanup: 2097857d4f46SKevin Wolf qemu_vfree(buf); 20984dca4b63SNaphtali Sprei 20994dca4b63SNaphtali Sprei if (ro) { 21000bce597dSJeff Cody /* ignoring error return here */ 21010bce597dSJeff Cody bdrv_reopen(bs->backing_hd, open_flags & ~BDRV_O_RDWR, NULL); 21024dca4b63SNaphtali Sprei } 21034dca4b63SNaphtali Sprei 21041d44952fSChristoph Hellwig return ret; 210533e3963eSbellard } 210633e3963eSbellard 2107e8877497SStefan Hajnoczi int bdrv_commit_all(void) 21086ab4b5abSMarkus Armbruster { 21096ab4b5abSMarkus Armbruster BlockDriverState *bs; 21106ab4b5abSMarkus Armbruster 2111dc364f4cSBenoît Canet QTAILQ_FOREACH(bs, &bdrv_states, device_list) { 2112ed78cda3SStefan Hajnoczi AioContext *aio_context = bdrv_get_aio_context(bs); 2113ed78cda3SStefan Hajnoczi 2114ed78cda3SStefan Hajnoczi aio_context_acquire(aio_context); 2115272d2d8eSJeff Cody if (bs->drv && bs->backing_hd) { 2116e8877497SStefan Hajnoczi int ret = bdrv_commit(bs); 2117e8877497SStefan Hajnoczi if (ret < 0) { 2118ed78cda3SStefan Hajnoczi aio_context_release(aio_context); 2119e8877497SStefan Hajnoczi return ret; 21206ab4b5abSMarkus Armbruster } 21216ab4b5abSMarkus Armbruster } 2122ed78cda3SStefan Hajnoczi aio_context_release(aio_context); 2123272d2d8eSJeff Cody } 2124e8877497SStefan Hajnoczi return 0; 2125e8877497SStefan Hajnoczi } 21266ab4b5abSMarkus Armbruster 2127756e6736SKevin Wolf /* 2128756e6736SKevin Wolf * Return values: 2129756e6736SKevin Wolf * 0 - success 2130756e6736SKevin Wolf * -EINVAL - backing format specified, but no file 2131756e6736SKevin Wolf * -ENOSPC - can't update the backing file because no space is left in the 2132756e6736SKevin Wolf * image file header 2133756e6736SKevin Wolf * -ENOTSUP - format driver doesn't support changing the backing file 2134756e6736SKevin Wolf */ 2135756e6736SKevin Wolf int bdrv_change_backing_file(BlockDriverState *bs, 2136756e6736SKevin Wolf const char *backing_file, const char *backing_fmt) 2137756e6736SKevin Wolf { 2138756e6736SKevin Wolf BlockDriver *drv = bs->drv; 2139469ef350SPaolo Bonzini int ret; 2140756e6736SKevin Wolf 21415f377794SPaolo Bonzini /* Backing file format doesn't make sense without a backing file */ 21425f377794SPaolo Bonzini if (backing_fmt && !backing_file) { 21435f377794SPaolo Bonzini return -EINVAL; 21445f377794SPaolo Bonzini } 21455f377794SPaolo Bonzini 2146756e6736SKevin Wolf if (drv->bdrv_change_backing_file != NULL) { 2147469ef350SPaolo Bonzini ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt); 2148756e6736SKevin Wolf } else { 2149469ef350SPaolo Bonzini ret = -ENOTSUP; 2150756e6736SKevin Wolf } 2151469ef350SPaolo Bonzini 2152469ef350SPaolo Bonzini if (ret == 0) { 2153469ef350SPaolo Bonzini pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: ""); 2154469ef350SPaolo Bonzini pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: ""); 2155469ef350SPaolo Bonzini } 2156469ef350SPaolo Bonzini return ret; 2157756e6736SKevin Wolf } 2158756e6736SKevin Wolf 21596ebdcee2SJeff Cody /* 21606ebdcee2SJeff Cody * Finds the image layer in the chain that has 'bs' as its backing file. 21616ebdcee2SJeff Cody * 21626ebdcee2SJeff Cody * active is the current topmost image. 21636ebdcee2SJeff Cody * 21646ebdcee2SJeff Cody * Returns NULL if bs is not found in active's image chain, 21656ebdcee2SJeff Cody * or if active == bs. 21664caf0fcdSJeff Cody * 21674caf0fcdSJeff Cody * Returns the bottommost base image if bs == NULL. 21686ebdcee2SJeff Cody */ 21696ebdcee2SJeff Cody BlockDriverState *bdrv_find_overlay(BlockDriverState *active, 21706ebdcee2SJeff Cody BlockDriverState *bs) 21716ebdcee2SJeff Cody { 21724caf0fcdSJeff Cody while (active && bs != active->backing_hd) { 21734caf0fcdSJeff Cody active = active->backing_hd; 21746ebdcee2SJeff Cody } 21756ebdcee2SJeff Cody 21764caf0fcdSJeff Cody return active; 21776ebdcee2SJeff Cody } 21786ebdcee2SJeff Cody 21794caf0fcdSJeff Cody /* Given a BDS, searches for the base layer. */ 21804caf0fcdSJeff Cody BlockDriverState *bdrv_find_base(BlockDriverState *bs) 21814caf0fcdSJeff Cody { 21824caf0fcdSJeff Cody return bdrv_find_overlay(bs, NULL); 21836ebdcee2SJeff Cody } 21846ebdcee2SJeff Cody 21856ebdcee2SJeff Cody typedef struct BlkIntermediateStates { 21866ebdcee2SJeff Cody BlockDriverState *bs; 21876ebdcee2SJeff Cody QSIMPLEQ_ENTRY(BlkIntermediateStates) entry; 21886ebdcee2SJeff Cody } BlkIntermediateStates; 21896ebdcee2SJeff Cody 21906ebdcee2SJeff Cody 21916ebdcee2SJeff Cody /* 21926ebdcee2SJeff Cody * Drops images above 'base' up to and including 'top', and sets the image 21936ebdcee2SJeff Cody * above 'top' to have base as its backing file. 21946ebdcee2SJeff Cody * 21956ebdcee2SJeff Cody * Requires that the overlay to 'top' is opened r/w, so that the backing file 21966ebdcee2SJeff Cody * information in 'bs' can be properly updated. 21976ebdcee2SJeff Cody * 21986ebdcee2SJeff Cody * E.g., this will convert the following chain: 21996ebdcee2SJeff Cody * bottom <- base <- intermediate <- top <- active 22006ebdcee2SJeff Cody * 22016ebdcee2SJeff Cody * to 22026ebdcee2SJeff Cody * 22036ebdcee2SJeff Cody * bottom <- base <- active 22046ebdcee2SJeff Cody * 22056ebdcee2SJeff Cody * It is allowed for bottom==base, in which case it converts: 22066ebdcee2SJeff Cody * 22076ebdcee2SJeff Cody * base <- intermediate <- top <- active 22086ebdcee2SJeff Cody * 22096ebdcee2SJeff Cody * to 22106ebdcee2SJeff Cody * 22116ebdcee2SJeff Cody * base <- active 22126ebdcee2SJeff Cody * 221354e26900SJeff Cody * If backing_file_str is non-NULL, it will be used when modifying top's 221454e26900SJeff Cody * overlay image metadata. 221554e26900SJeff Cody * 22166ebdcee2SJeff Cody * Error conditions: 22176ebdcee2SJeff Cody * if active == top, that is considered an error 22186ebdcee2SJeff Cody * 22196ebdcee2SJeff Cody */ 22206ebdcee2SJeff Cody int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top, 222154e26900SJeff Cody BlockDriverState *base, const char *backing_file_str) 22226ebdcee2SJeff Cody { 22236ebdcee2SJeff Cody BlockDriverState *intermediate; 22246ebdcee2SJeff Cody BlockDriverState *base_bs = NULL; 22256ebdcee2SJeff Cody BlockDriverState *new_top_bs = NULL; 22266ebdcee2SJeff Cody BlkIntermediateStates *intermediate_state, *next; 22276ebdcee2SJeff Cody int ret = -EIO; 22286ebdcee2SJeff Cody 22296ebdcee2SJeff Cody QSIMPLEQ_HEAD(states_to_delete, BlkIntermediateStates) states_to_delete; 22306ebdcee2SJeff Cody QSIMPLEQ_INIT(&states_to_delete); 22316ebdcee2SJeff Cody 22326ebdcee2SJeff Cody if (!top->drv || !base->drv) { 22336ebdcee2SJeff Cody goto exit; 22346ebdcee2SJeff Cody } 22356ebdcee2SJeff Cody 22366ebdcee2SJeff Cody new_top_bs = bdrv_find_overlay(active, top); 22376ebdcee2SJeff Cody 22386ebdcee2SJeff Cody if (new_top_bs == NULL) { 22396ebdcee2SJeff Cody /* we could not find the image above 'top', this is an error */ 22406ebdcee2SJeff Cody goto exit; 22416ebdcee2SJeff Cody } 22426ebdcee2SJeff Cody 22436ebdcee2SJeff Cody /* special case of new_top_bs->backing_hd already pointing to base - nothing 22446ebdcee2SJeff Cody * to do, no intermediate images */ 22456ebdcee2SJeff Cody if (new_top_bs->backing_hd == base) { 22466ebdcee2SJeff Cody ret = 0; 22476ebdcee2SJeff Cody goto exit; 22486ebdcee2SJeff Cody } 22496ebdcee2SJeff Cody 22506ebdcee2SJeff Cody intermediate = top; 22516ebdcee2SJeff Cody 22526ebdcee2SJeff Cody /* now we will go down through the list, and add each BDS we find 22536ebdcee2SJeff Cody * into our deletion queue, until we hit the 'base' 22546ebdcee2SJeff Cody */ 22556ebdcee2SJeff Cody while (intermediate) { 22565839e53bSMarkus Armbruster intermediate_state = g_new0(BlkIntermediateStates, 1); 22576ebdcee2SJeff Cody intermediate_state->bs = intermediate; 22586ebdcee2SJeff Cody QSIMPLEQ_INSERT_TAIL(&states_to_delete, intermediate_state, entry); 22596ebdcee2SJeff Cody 22606ebdcee2SJeff Cody if (intermediate->backing_hd == base) { 22616ebdcee2SJeff Cody base_bs = intermediate->backing_hd; 22626ebdcee2SJeff Cody break; 22636ebdcee2SJeff Cody } 22646ebdcee2SJeff Cody intermediate = intermediate->backing_hd; 22656ebdcee2SJeff Cody } 22666ebdcee2SJeff Cody if (base_bs == NULL) { 22676ebdcee2SJeff Cody /* something went wrong, we did not end at the base. safely 22686ebdcee2SJeff Cody * unravel everything, and exit with error */ 22696ebdcee2SJeff Cody goto exit; 22706ebdcee2SJeff Cody } 22716ebdcee2SJeff Cody 22726ebdcee2SJeff Cody /* success - we can delete the intermediate states, and link top->base */ 227354e26900SJeff Cody backing_file_str = backing_file_str ? backing_file_str : base_bs->filename; 227454e26900SJeff Cody ret = bdrv_change_backing_file(new_top_bs, backing_file_str, 22756ebdcee2SJeff Cody base_bs->drv ? base_bs->drv->format_name : ""); 22766ebdcee2SJeff Cody if (ret) { 22776ebdcee2SJeff Cody goto exit; 22786ebdcee2SJeff Cody } 2279920beae1SFam Zheng bdrv_set_backing_hd(new_top_bs, base_bs); 22806ebdcee2SJeff Cody 22816ebdcee2SJeff Cody QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) { 22826ebdcee2SJeff Cody /* so that bdrv_close() does not recursively close the chain */ 2283920beae1SFam Zheng bdrv_set_backing_hd(intermediate_state->bs, NULL); 22844f6fd349SFam Zheng bdrv_unref(intermediate_state->bs); 22856ebdcee2SJeff Cody } 22866ebdcee2SJeff Cody ret = 0; 22876ebdcee2SJeff Cody 22886ebdcee2SJeff Cody exit: 22896ebdcee2SJeff Cody QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) { 22906ebdcee2SJeff Cody g_free(intermediate_state); 22916ebdcee2SJeff Cody } 22926ebdcee2SJeff Cody return ret; 22936ebdcee2SJeff Cody } 22946ebdcee2SJeff Cody 229583f64091Sbellard /** 229683f64091Sbellard * Truncate file to 'offset' bytes (needed only for file protocols) 229783f64091Sbellard */ 229883f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset) 229983f64091Sbellard { 230083f64091Sbellard BlockDriver *drv = bs->drv; 230151762288SStefan Hajnoczi int ret; 230283f64091Sbellard if (!drv) 230319cb3738Sbellard return -ENOMEDIUM; 230483f64091Sbellard if (!drv->bdrv_truncate) 230583f64091Sbellard return -ENOTSUP; 230659f2689dSNaphtali Sprei if (bs->read_only) 230759f2689dSNaphtali Sprei return -EACCES; 23089c75e168SJeff Cody 230951762288SStefan Hajnoczi ret = drv->bdrv_truncate(bs, offset); 231051762288SStefan Hajnoczi if (ret == 0) { 231151762288SStefan Hajnoczi ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); 2312ce1ffea8SJohn Snow bdrv_dirty_bitmap_truncate(bs); 2313a7f53e26SMarkus Armbruster if (bs->blk) { 2314a7f53e26SMarkus Armbruster blk_dev_resize_cb(bs->blk); 2315a7f53e26SMarkus Armbruster } 231651762288SStefan Hajnoczi } 231751762288SStefan Hajnoczi return ret; 231883f64091Sbellard } 231983f64091Sbellard 232083f64091Sbellard /** 23214a1d5e1fSFam Zheng * Length of a allocated file in bytes. Sparse files are counted by actual 23224a1d5e1fSFam Zheng * allocated space. Return < 0 if error or unknown. 23234a1d5e1fSFam Zheng */ 23244a1d5e1fSFam Zheng int64_t bdrv_get_allocated_file_size(BlockDriverState *bs) 23254a1d5e1fSFam Zheng { 23264a1d5e1fSFam Zheng BlockDriver *drv = bs->drv; 23274a1d5e1fSFam Zheng if (!drv) { 23284a1d5e1fSFam Zheng return -ENOMEDIUM; 23294a1d5e1fSFam Zheng } 23304a1d5e1fSFam Zheng if (drv->bdrv_get_allocated_file_size) { 23314a1d5e1fSFam Zheng return drv->bdrv_get_allocated_file_size(bs); 23324a1d5e1fSFam Zheng } 23334a1d5e1fSFam Zheng if (bs->file) { 23344a1d5e1fSFam Zheng return bdrv_get_allocated_file_size(bs->file); 23354a1d5e1fSFam Zheng } 23364a1d5e1fSFam Zheng return -ENOTSUP; 23374a1d5e1fSFam Zheng } 23384a1d5e1fSFam Zheng 23394a1d5e1fSFam Zheng /** 234065a9bb25SMarkus Armbruster * Return number of sectors on success, -errno on error. 234183f64091Sbellard */ 234265a9bb25SMarkus Armbruster int64_t bdrv_nb_sectors(BlockDriverState *bs) 234383f64091Sbellard { 234483f64091Sbellard BlockDriver *drv = bs->drv; 234565a9bb25SMarkus Armbruster 234683f64091Sbellard if (!drv) 234719cb3738Sbellard return -ENOMEDIUM; 234851762288SStefan Hajnoczi 2349b94a2610SKevin Wolf if (drv->has_variable_length) { 2350b94a2610SKevin Wolf int ret = refresh_total_sectors(bs, bs->total_sectors); 2351b94a2610SKevin Wolf if (ret < 0) { 2352b94a2610SKevin Wolf return ret; 2353fc01f7e7Sbellard } 235446a4e4e6SStefan Hajnoczi } 235565a9bb25SMarkus Armbruster return bs->total_sectors; 235665a9bb25SMarkus Armbruster } 235765a9bb25SMarkus Armbruster 235865a9bb25SMarkus Armbruster /** 235965a9bb25SMarkus Armbruster * Return length in bytes on success, -errno on error. 236065a9bb25SMarkus Armbruster * The length is always a multiple of BDRV_SECTOR_SIZE. 236165a9bb25SMarkus Armbruster */ 236265a9bb25SMarkus Armbruster int64_t bdrv_getlength(BlockDriverState *bs) 236365a9bb25SMarkus Armbruster { 236465a9bb25SMarkus Armbruster int64_t ret = bdrv_nb_sectors(bs); 236565a9bb25SMarkus Armbruster 23664a9c9ea0SFam Zheng ret = ret > INT64_MAX / BDRV_SECTOR_SIZE ? -EFBIG : ret; 236765a9bb25SMarkus Armbruster return ret < 0 ? ret : ret * BDRV_SECTOR_SIZE; 236846a4e4e6SStefan Hajnoczi } 2369fc01f7e7Sbellard 237019cb3738Sbellard /* return 0 as number of sectors if no device present or error */ 237196b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) 2372fc01f7e7Sbellard { 237365a9bb25SMarkus Armbruster int64_t nb_sectors = bdrv_nb_sectors(bs); 237465a9bb25SMarkus Armbruster 237565a9bb25SMarkus Armbruster *nb_sectors_ptr = nb_sectors < 0 ? 0 : nb_sectors; 2376fc01f7e7Sbellard } 2377cf98951bSbellard 2378ff06f5f3SPaolo Bonzini void bdrv_set_on_error(BlockDriverState *bs, BlockdevOnError on_read_error, 2379ff06f5f3SPaolo Bonzini BlockdevOnError on_write_error) 2380abd7f68dSMarkus Armbruster { 2381abd7f68dSMarkus Armbruster bs->on_read_error = on_read_error; 2382abd7f68dSMarkus Armbruster bs->on_write_error = on_write_error; 2383abd7f68dSMarkus Armbruster } 2384abd7f68dSMarkus Armbruster 23851ceee0d5SPaolo Bonzini BlockdevOnError bdrv_get_on_error(BlockDriverState *bs, bool is_read) 2386abd7f68dSMarkus Armbruster { 2387abd7f68dSMarkus Armbruster return is_read ? bs->on_read_error : bs->on_write_error; 2388abd7f68dSMarkus Armbruster } 2389abd7f68dSMarkus Armbruster 23903e1caa5fSPaolo Bonzini BlockErrorAction bdrv_get_error_action(BlockDriverState *bs, bool is_read, int error) 23913e1caa5fSPaolo Bonzini { 23923e1caa5fSPaolo Bonzini BlockdevOnError on_err = is_read ? bs->on_read_error : bs->on_write_error; 23933e1caa5fSPaolo Bonzini 23943e1caa5fSPaolo Bonzini switch (on_err) { 23953e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_ENOSPC: 2396a589569fSWenchao Xia return (error == ENOSPC) ? 2397a589569fSWenchao Xia BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT; 23983e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_STOP: 2399a589569fSWenchao Xia return BLOCK_ERROR_ACTION_STOP; 24003e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_REPORT: 2401a589569fSWenchao Xia return BLOCK_ERROR_ACTION_REPORT; 24023e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_IGNORE: 2403a589569fSWenchao Xia return BLOCK_ERROR_ACTION_IGNORE; 24043e1caa5fSPaolo Bonzini default: 24053e1caa5fSPaolo Bonzini abort(); 24063e1caa5fSPaolo Bonzini } 24073e1caa5fSPaolo Bonzini } 24083e1caa5fSPaolo Bonzini 2409c7c2ff0cSLuiz Capitulino static void send_qmp_error_event(BlockDriverState *bs, 2410c7c2ff0cSLuiz Capitulino BlockErrorAction action, 2411c7c2ff0cSLuiz Capitulino bool is_read, int error) 2412c7c2ff0cSLuiz Capitulino { 2413573742a5SPeter Maydell IoOperationType optype; 2414c7c2ff0cSLuiz Capitulino 2415573742a5SPeter Maydell optype = is_read ? IO_OPERATION_TYPE_READ : IO_OPERATION_TYPE_WRITE; 2416573742a5SPeter Maydell qapi_event_send_block_io_error(bdrv_get_device_name(bs), optype, action, 2417c7c2ff0cSLuiz Capitulino bdrv_iostatus_is_enabled(bs), 2418624ff573SLuiz Capitulino error == ENOSPC, strerror(error), 2419624ff573SLuiz Capitulino &error_abort); 2420c7c2ff0cSLuiz Capitulino } 2421c7c2ff0cSLuiz Capitulino 24223e1caa5fSPaolo Bonzini /* This is done by device models because, while the block layer knows 24233e1caa5fSPaolo Bonzini * about the error, it does not know whether an operation comes from 24243e1caa5fSPaolo Bonzini * the device or the block layer (from a job, for example). 24253e1caa5fSPaolo Bonzini */ 24263e1caa5fSPaolo Bonzini void bdrv_error_action(BlockDriverState *bs, BlockErrorAction action, 24273e1caa5fSPaolo Bonzini bool is_read, int error) 24283e1caa5fSPaolo Bonzini { 24293e1caa5fSPaolo Bonzini assert(error >= 0); 24302bd3bce8SPaolo Bonzini 2431a589569fSWenchao Xia if (action == BLOCK_ERROR_ACTION_STOP) { 24322bd3bce8SPaolo Bonzini /* First set the iostatus, so that "info block" returns an iostatus 24332bd3bce8SPaolo Bonzini * that matches the events raised so far (an additional error iostatus 24342bd3bce8SPaolo Bonzini * is fine, but not a lost one). 24352bd3bce8SPaolo Bonzini */ 24363e1caa5fSPaolo Bonzini bdrv_iostatus_set_err(bs, error); 24372bd3bce8SPaolo Bonzini 24382bd3bce8SPaolo Bonzini /* Then raise the request to stop the VM and the event. 24392bd3bce8SPaolo Bonzini * qemu_system_vmstop_request_prepare has two effects. First, 24402bd3bce8SPaolo Bonzini * it ensures that the STOP event always comes after the 24412bd3bce8SPaolo Bonzini * BLOCK_IO_ERROR event. Second, it ensures that even if management 24422bd3bce8SPaolo Bonzini * can observe the STOP event and do a "cont" before the STOP 24432bd3bce8SPaolo Bonzini * event is issued, the VM will not stop. In this case, vm_start() 24442bd3bce8SPaolo Bonzini * also ensures that the STOP/RESUME pair of events is emitted. 24452bd3bce8SPaolo Bonzini */ 24462bd3bce8SPaolo Bonzini qemu_system_vmstop_request_prepare(); 2447c7c2ff0cSLuiz Capitulino send_qmp_error_event(bs, action, is_read, error); 24482bd3bce8SPaolo Bonzini qemu_system_vmstop_request(RUN_STATE_IO_ERROR); 24492bd3bce8SPaolo Bonzini } else { 2450c7c2ff0cSLuiz Capitulino send_qmp_error_event(bs, action, is_read, error); 24513e1caa5fSPaolo Bonzini } 24523e1caa5fSPaolo Bonzini } 24533e1caa5fSPaolo Bonzini 2454b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs) 2455b338082bSbellard { 2456b338082bSbellard return bs->read_only; 2457b338082bSbellard } 2458b338082bSbellard 2459985a03b0Sths int bdrv_is_sg(BlockDriverState *bs) 2460985a03b0Sths { 2461985a03b0Sths return bs->sg; 2462985a03b0Sths } 2463985a03b0Sths 2464e900a7b7SChristoph Hellwig int bdrv_enable_write_cache(BlockDriverState *bs) 2465e900a7b7SChristoph Hellwig { 2466e900a7b7SChristoph Hellwig return bs->enable_write_cache; 2467e900a7b7SChristoph Hellwig } 2468e900a7b7SChristoph Hellwig 2469425b0148SPaolo Bonzini void bdrv_set_enable_write_cache(BlockDriverState *bs, bool wce) 2470425b0148SPaolo Bonzini { 2471425b0148SPaolo Bonzini bs->enable_write_cache = wce; 247255b110f2SJeff Cody 247355b110f2SJeff Cody /* so a reopen() will preserve wce */ 247455b110f2SJeff Cody if (wce) { 247555b110f2SJeff Cody bs->open_flags |= BDRV_O_CACHE_WB; 247655b110f2SJeff Cody } else { 247755b110f2SJeff Cody bs->open_flags &= ~BDRV_O_CACHE_WB; 247855b110f2SJeff Cody } 2479425b0148SPaolo Bonzini } 2480425b0148SPaolo Bonzini 2481ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs) 2482ea2384d3Sbellard { 2483ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) 2484ea2384d3Sbellard return 1; 2485ea2384d3Sbellard return bs->encrypted; 2486ea2384d3Sbellard } 2487ea2384d3Sbellard 2488c0f4ce77Saliguori int bdrv_key_required(BlockDriverState *bs) 2489c0f4ce77Saliguori { 2490c0f4ce77Saliguori BlockDriverState *backing_hd = bs->backing_hd; 2491c0f4ce77Saliguori 2492c0f4ce77Saliguori if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key) 2493c0f4ce77Saliguori return 1; 2494c0f4ce77Saliguori return (bs->encrypted && !bs->valid_key); 2495c0f4ce77Saliguori } 2496c0f4ce77Saliguori 2497ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key) 2498ea2384d3Sbellard { 2499ea2384d3Sbellard int ret; 2500ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) { 2501ea2384d3Sbellard ret = bdrv_set_key(bs->backing_hd, key); 2502ea2384d3Sbellard if (ret < 0) 2503ea2384d3Sbellard return ret; 2504ea2384d3Sbellard if (!bs->encrypted) 2505ea2384d3Sbellard return 0; 2506ea2384d3Sbellard } 2507fd04a2aeSShahar Havivi if (!bs->encrypted) { 2508fd04a2aeSShahar Havivi return -EINVAL; 2509fd04a2aeSShahar Havivi } else if (!bs->drv || !bs->drv->bdrv_set_key) { 2510fd04a2aeSShahar Havivi return -ENOMEDIUM; 2511fd04a2aeSShahar Havivi } 2512c0f4ce77Saliguori ret = bs->drv->bdrv_set_key(bs, key); 2513bb5fc20fSaliguori if (ret < 0) { 2514bb5fc20fSaliguori bs->valid_key = 0; 2515bb5fc20fSaliguori } else if (!bs->valid_key) { 2516bb5fc20fSaliguori bs->valid_key = 1; 2517a7f53e26SMarkus Armbruster if (bs->blk) { 2518bb5fc20fSaliguori /* call the change callback now, we skipped it on open */ 2519a7f53e26SMarkus Armbruster blk_dev_change_media_cb(bs->blk, true); 2520a7f53e26SMarkus Armbruster } 2521bb5fc20fSaliguori } 2522c0f4ce77Saliguori return ret; 2523ea2384d3Sbellard } 2524ea2384d3Sbellard 25254d2855a3SMarkus Armbruster /* 25264d2855a3SMarkus Armbruster * Provide an encryption key for @bs. 25274d2855a3SMarkus Armbruster * If @key is non-null: 25284d2855a3SMarkus Armbruster * If @bs is not encrypted, fail. 25294d2855a3SMarkus Armbruster * Else if the key is invalid, fail. 25304d2855a3SMarkus Armbruster * Else set @bs's key to @key, replacing the existing key, if any. 25314d2855a3SMarkus Armbruster * If @key is null: 25324d2855a3SMarkus Armbruster * If @bs is encrypted and still lacks a key, fail. 25334d2855a3SMarkus Armbruster * Else do nothing. 25344d2855a3SMarkus Armbruster * On failure, store an error object through @errp if non-null. 25354d2855a3SMarkus Armbruster */ 25364d2855a3SMarkus Armbruster void bdrv_add_key(BlockDriverState *bs, const char *key, Error **errp) 25374d2855a3SMarkus Armbruster { 25384d2855a3SMarkus Armbruster if (key) { 25394d2855a3SMarkus Armbruster if (!bdrv_is_encrypted(bs)) { 254081e5f78aSAlberto Garcia error_setg(errp, "Node '%s' is not encrypted", 254181e5f78aSAlberto Garcia bdrv_get_device_or_node_name(bs)); 25424d2855a3SMarkus Armbruster } else if (bdrv_set_key(bs, key) < 0) { 25434d2855a3SMarkus Armbruster error_set(errp, QERR_INVALID_PASSWORD); 25444d2855a3SMarkus Armbruster } 25454d2855a3SMarkus Armbruster } else { 25464d2855a3SMarkus Armbruster if (bdrv_key_required(bs)) { 2547b1ca6391SMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_ENCRYPTED, 2548b1ca6391SMarkus Armbruster "'%s' (%s) is encrypted", 254981e5f78aSAlberto Garcia bdrv_get_device_or_node_name(bs), 25504d2855a3SMarkus Armbruster bdrv_get_encrypted_filename(bs)); 25514d2855a3SMarkus Armbruster } 25524d2855a3SMarkus Armbruster } 25534d2855a3SMarkus Armbruster } 25544d2855a3SMarkus Armbruster 2555f8d6bba1SMarkus Armbruster const char *bdrv_get_format_name(BlockDriverState *bs) 2556ea2384d3Sbellard { 2557f8d6bba1SMarkus Armbruster return bs->drv ? bs->drv->format_name : NULL; 2558ea2384d3Sbellard } 2559ea2384d3Sbellard 2560ada42401SStefan Hajnoczi static int qsort_strcmp(const void *a, const void *b) 2561ada42401SStefan Hajnoczi { 2562ada42401SStefan Hajnoczi return strcmp(a, b); 2563ada42401SStefan Hajnoczi } 2564ada42401SStefan Hajnoczi 2565ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name), 2566ea2384d3Sbellard void *opaque) 2567ea2384d3Sbellard { 2568ea2384d3Sbellard BlockDriver *drv; 2569e855e4fbSJeff Cody int count = 0; 2570ada42401SStefan Hajnoczi int i; 2571e855e4fbSJeff Cody const char **formats = NULL; 2572ea2384d3Sbellard 25738a22f02aSStefan Hajnoczi QLIST_FOREACH(drv, &bdrv_drivers, list) { 2574e855e4fbSJeff Cody if (drv->format_name) { 2575e855e4fbSJeff Cody bool found = false; 2576e855e4fbSJeff Cody int i = count; 2577e855e4fbSJeff Cody while (formats && i && !found) { 2578e855e4fbSJeff Cody found = !strcmp(formats[--i], drv->format_name); 2579e855e4fbSJeff Cody } 2580e855e4fbSJeff Cody 2581e855e4fbSJeff Cody if (!found) { 25825839e53bSMarkus Armbruster formats = g_renew(const char *, formats, count + 1); 2583e855e4fbSJeff Cody formats[count++] = drv->format_name; 2584ea2384d3Sbellard } 2585ea2384d3Sbellard } 2586e855e4fbSJeff Cody } 2587ada42401SStefan Hajnoczi 2588ada42401SStefan Hajnoczi qsort(formats, count, sizeof(formats[0]), qsort_strcmp); 2589ada42401SStefan Hajnoczi 2590ada42401SStefan Hajnoczi for (i = 0; i < count; i++) { 2591ada42401SStefan Hajnoczi it(opaque, formats[i]); 2592ada42401SStefan Hajnoczi } 2593ada42401SStefan Hajnoczi 2594e855e4fbSJeff Cody g_free(formats); 2595e855e4fbSJeff Cody } 2596ea2384d3Sbellard 2597dc364f4cSBenoît Canet /* This function is to find a node in the bs graph */ 2598dc364f4cSBenoît Canet BlockDriverState *bdrv_find_node(const char *node_name) 2599dc364f4cSBenoît Canet { 2600dc364f4cSBenoît Canet BlockDriverState *bs; 2601dc364f4cSBenoît Canet 2602dc364f4cSBenoît Canet assert(node_name); 2603dc364f4cSBenoît Canet 2604dc364f4cSBenoît Canet QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) { 2605dc364f4cSBenoît Canet if (!strcmp(node_name, bs->node_name)) { 2606dc364f4cSBenoît Canet return bs; 2607dc364f4cSBenoît Canet } 2608dc364f4cSBenoît Canet } 2609dc364f4cSBenoît Canet return NULL; 2610dc364f4cSBenoît Canet } 2611dc364f4cSBenoît Canet 2612c13163fbSBenoît Canet /* Put this QMP function here so it can access the static graph_bdrv_states. */ 2613d5a8ee60SAlberto Garcia BlockDeviceInfoList *bdrv_named_nodes_list(Error **errp) 2614c13163fbSBenoît Canet { 2615c13163fbSBenoît Canet BlockDeviceInfoList *list, *entry; 2616c13163fbSBenoît Canet BlockDriverState *bs; 2617c13163fbSBenoît Canet 2618c13163fbSBenoît Canet list = NULL; 2619c13163fbSBenoît Canet QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) { 2620d5a8ee60SAlberto Garcia BlockDeviceInfo *info = bdrv_block_device_info(bs, errp); 2621d5a8ee60SAlberto Garcia if (!info) { 2622d5a8ee60SAlberto Garcia qapi_free_BlockDeviceInfoList(list); 2623d5a8ee60SAlberto Garcia return NULL; 2624d5a8ee60SAlberto Garcia } 2625c13163fbSBenoît Canet entry = g_malloc0(sizeof(*entry)); 2626d5a8ee60SAlberto Garcia entry->value = info; 2627c13163fbSBenoît Canet entry->next = list; 2628c13163fbSBenoît Canet list = entry; 2629c13163fbSBenoît Canet } 2630c13163fbSBenoît Canet 2631c13163fbSBenoît Canet return list; 2632c13163fbSBenoît Canet } 2633c13163fbSBenoît Canet 263412d3ba82SBenoît Canet BlockDriverState *bdrv_lookup_bs(const char *device, 263512d3ba82SBenoît Canet const char *node_name, 263612d3ba82SBenoît Canet Error **errp) 263712d3ba82SBenoît Canet { 26387f06d47eSMarkus Armbruster BlockBackend *blk; 26397f06d47eSMarkus Armbruster BlockDriverState *bs; 264012d3ba82SBenoît Canet 264112d3ba82SBenoît Canet if (device) { 26427f06d47eSMarkus Armbruster blk = blk_by_name(device); 264312d3ba82SBenoît Canet 26447f06d47eSMarkus Armbruster if (blk) { 26457f06d47eSMarkus Armbruster return blk_bs(blk); 264612d3ba82SBenoît Canet } 2647dd67fa50SBenoît Canet } 264812d3ba82SBenoît Canet 2649dd67fa50SBenoît Canet if (node_name) { 265012d3ba82SBenoît Canet bs = bdrv_find_node(node_name); 265112d3ba82SBenoît Canet 2652dd67fa50SBenoît Canet if (bs) { 2653dd67fa50SBenoît Canet return bs; 2654dd67fa50SBenoît Canet } 265512d3ba82SBenoît Canet } 265612d3ba82SBenoît Canet 2657dd67fa50SBenoît Canet error_setg(errp, "Cannot find device=%s nor node_name=%s", 2658dd67fa50SBenoît Canet device ? device : "", 2659dd67fa50SBenoît Canet node_name ? node_name : ""); 2660dd67fa50SBenoît Canet return NULL; 266112d3ba82SBenoît Canet } 266212d3ba82SBenoît Canet 26635a6684d2SJeff Cody /* If 'base' is in the same chain as 'top', return true. Otherwise, 26645a6684d2SJeff Cody * return false. If either argument is NULL, return false. */ 26655a6684d2SJeff Cody bool bdrv_chain_contains(BlockDriverState *top, BlockDriverState *base) 26665a6684d2SJeff Cody { 26675a6684d2SJeff Cody while (top && top != base) { 26685a6684d2SJeff Cody top = top->backing_hd; 26695a6684d2SJeff Cody } 26705a6684d2SJeff Cody 26715a6684d2SJeff Cody return top != NULL; 26725a6684d2SJeff Cody } 26735a6684d2SJeff Cody 267404df765aSFam Zheng BlockDriverState *bdrv_next_node(BlockDriverState *bs) 267504df765aSFam Zheng { 267604df765aSFam Zheng if (!bs) { 267704df765aSFam Zheng return QTAILQ_FIRST(&graph_bdrv_states); 267804df765aSFam Zheng } 267904df765aSFam Zheng return QTAILQ_NEXT(bs, node_list); 268004df765aSFam Zheng } 268104df765aSFam Zheng 26822f399b0aSMarkus Armbruster BlockDriverState *bdrv_next(BlockDriverState *bs) 26832f399b0aSMarkus Armbruster { 26842f399b0aSMarkus Armbruster if (!bs) { 26852f399b0aSMarkus Armbruster return QTAILQ_FIRST(&bdrv_states); 26862f399b0aSMarkus Armbruster } 2687dc364f4cSBenoît Canet return QTAILQ_NEXT(bs, device_list); 26882f399b0aSMarkus Armbruster } 26892f399b0aSMarkus Armbruster 269020a9e77dSFam Zheng const char *bdrv_get_node_name(const BlockDriverState *bs) 269120a9e77dSFam Zheng { 269220a9e77dSFam Zheng return bs->node_name; 269320a9e77dSFam Zheng } 269420a9e77dSFam Zheng 26957f06d47eSMarkus Armbruster /* TODO check what callers really want: bs->node_name or blk_name() */ 2696bfb197e0SMarkus Armbruster const char *bdrv_get_device_name(const BlockDriverState *bs) 2697ea2384d3Sbellard { 2698bfb197e0SMarkus Armbruster return bs->blk ? blk_name(bs->blk) : ""; 2699ea2384d3Sbellard } 2700ea2384d3Sbellard 27019b2aa84fSAlberto Garcia /* This can be used to identify nodes that might not have a device 27029b2aa84fSAlberto Garcia * name associated. Since node and device names live in the same 27039b2aa84fSAlberto Garcia * namespace, the result is unambiguous. The exception is if both are 27049b2aa84fSAlberto Garcia * absent, then this returns an empty (non-null) string. */ 27059b2aa84fSAlberto Garcia const char *bdrv_get_device_or_node_name(const BlockDriverState *bs) 27069b2aa84fSAlberto Garcia { 27079b2aa84fSAlberto Garcia return bs->blk ? blk_name(bs->blk) : bs->node_name; 27089b2aa84fSAlberto Garcia } 27099b2aa84fSAlberto Garcia 2710c8433287SMarkus Armbruster int bdrv_get_flags(BlockDriverState *bs) 2711c8433287SMarkus Armbruster { 2712c8433287SMarkus Armbruster return bs->open_flags; 2713c8433287SMarkus Armbruster } 2714c8433287SMarkus Armbruster 27153ac21627SPeter Lieven int bdrv_has_zero_init_1(BlockDriverState *bs) 27163ac21627SPeter Lieven { 27173ac21627SPeter Lieven return 1; 27183ac21627SPeter Lieven } 27193ac21627SPeter Lieven 2720f2feebbdSKevin Wolf int bdrv_has_zero_init(BlockDriverState *bs) 2721f2feebbdSKevin Wolf { 2722f2feebbdSKevin Wolf assert(bs->drv); 2723f2feebbdSKevin Wolf 272411212d8fSPaolo Bonzini /* If BS is a copy on write image, it is initialized to 272511212d8fSPaolo Bonzini the contents of the base image, which may not be zeroes. */ 272611212d8fSPaolo Bonzini if (bs->backing_hd) { 272711212d8fSPaolo Bonzini return 0; 272811212d8fSPaolo Bonzini } 2729336c1c12SKevin Wolf if (bs->drv->bdrv_has_zero_init) { 2730336c1c12SKevin Wolf return bs->drv->bdrv_has_zero_init(bs); 2731f2feebbdSKevin Wolf } 2732f2feebbdSKevin Wolf 27333ac21627SPeter Lieven /* safe default */ 27343ac21627SPeter Lieven return 0; 2735f2feebbdSKevin Wolf } 2736f2feebbdSKevin Wolf 27374ce78691SPeter Lieven bool bdrv_unallocated_blocks_are_zero(BlockDriverState *bs) 27384ce78691SPeter Lieven { 27394ce78691SPeter Lieven BlockDriverInfo bdi; 27404ce78691SPeter Lieven 27414ce78691SPeter Lieven if (bs->backing_hd) { 27424ce78691SPeter Lieven return false; 27434ce78691SPeter Lieven } 27444ce78691SPeter Lieven 27454ce78691SPeter Lieven if (bdrv_get_info(bs, &bdi) == 0) { 27464ce78691SPeter Lieven return bdi.unallocated_blocks_are_zero; 27474ce78691SPeter Lieven } 27484ce78691SPeter Lieven 27494ce78691SPeter Lieven return false; 27504ce78691SPeter Lieven } 27514ce78691SPeter Lieven 27524ce78691SPeter Lieven bool bdrv_can_write_zeroes_with_unmap(BlockDriverState *bs) 27534ce78691SPeter Lieven { 27544ce78691SPeter Lieven BlockDriverInfo bdi; 27554ce78691SPeter Lieven 27564ce78691SPeter Lieven if (bs->backing_hd || !(bs->open_flags & BDRV_O_UNMAP)) { 27574ce78691SPeter Lieven return false; 27584ce78691SPeter Lieven } 27594ce78691SPeter Lieven 27604ce78691SPeter Lieven if (bdrv_get_info(bs, &bdi) == 0) { 27614ce78691SPeter Lieven return bdi.can_write_zeroes_with_unmap; 27624ce78691SPeter Lieven } 27634ce78691SPeter Lieven 27644ce78691SPeter Lieven return false; 27654ce78691SPeter Lieven } 27664ce78691SPeter Lieven 2767045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs) 2768045df330Saliguori { 2769045df330Saliguori if (bs->backing_hd && bs->backing_hd->encrypted) 2770045df330Saliguori return bs->backing_file; 2771045df330Saliguori else if (bs->encrypted) 2772045df330Saliguori return bs->filename; 2773045df330Saliguori else 2774045df330Saliguori return NULL; 2775045df330Saliguori } 2776045df330Saliguori 277783f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs, 277883f64091Sbellard char *filename, int filename_size) 277983f64091Sbellard { 278083f64091Sbellard pstrcpy(filename, filename_size, bs->backing_file); 278183f64091Sbellard } 278283f64091Sbellard 2783faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 2784faea38e7Sbellard { 2785faea38e7Sbellard BlockDriver *drv = bs->drv; 2786faea38e7Sbellard if (!drv) 278719cb3738Sbellard return -ENOMEDIUM; 2788faea38e7Sbellard if (!drv->bdrv_get_info) 2789faea38e7Sbellard return -ENOTSUP; 2790faea38e7Sbellard memset(bdi, 0, sizeof(*bdi)); 2791faea38e7Sbellard return drv->bdrv_get_info(bs, bdi); 2792faea38e7Sbellard } 2793faea38e7Sbellard 2794eae041feSMax Reitz ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs) 2795eae041feSMax Reitz { 2796eae041feSMax Reitz BlockDriver *drv = bs->drv; 2797eae041feSMax Reitz if (drv && drv->bdrv_get_specific_info) { 2798eae041feSMax Reitz return drv->bdrv_get_specific_info(bs); 2799eae041feSMax Reitz } 2800eae041feSMax Reitz return NULL; 2801eae041feSMax Reitz } 2802eae041feSMax Reitz 28038b9b0cc2SKevin Wolf void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event) 28048b9b0cc2SKevin Wolf { 2805bf736fe3SKevin Wolf if (!bs || !bs->drv || !bs->drv->bdrv_debug_event) { 28068b9b0cc2SKevin Wolf return; 28078b9b0cc2SKevin Wolf } 28088b9b0cc2SKevin Wolf 2809bf736fe3SKevin Wolf bs->drv->bdrv_debug_event(bs, event); 281041c695c7SKevin Wolf } 28118b9b0cc2SKevin Wolf 281241c695c7SKevin Wolf int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event, 281341c695c7SKevin Wolf const char *tag) 281441c695c7SKevin Wolf { 281541c695c7SKevin Wolf while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) { 281641c695c7SKevin Wolf bs = bs->file; 281741c695c7SKevin Wolf } 281841c695c7SKevin Wolf 281941c695c7SKevin Wolf if (bs && bs->drv && bs->drv->bdrv_debug_breakpoint) { 282041c695c7SKevin Wolf return bs->drv->bdrv_debug_breakpoint(bs, event, tag); 282141c695c7SKevin Wolf } 282241c695c7SKevin Wolf 282341c695c7SKevin Wolf return -ENOTSUP; 282441c695c7SKevin Wolf } 282541c695c7SKevin Wolf 28264cc70e93SFam Zheng int bdrv_debug_remove_breakpoint(BlockDriverState *bs, const char *tag) 28274cc70e93SFam Zheng { 28284cc70e93SFam Zheng while (bs && bs->drv && !bs->drv->bdrv_debug_remove_breakpoint) { 28294cc70e93SFam Zheng bs = bs->file; 28304cc70e93SFam Zheng } 28314cc70e93SFam Zheng 28324cc70e93SFam Zheng if (bs && bs->drv && bs->drv->bdrv_debug_remove_breakpoint) { 28334cc70e93SFam Zheng return bs->drv->bdrv_debug_remove_breakpoint(bs, tag); 28344cc70e93SFam Zheng } 28354cc70e93SFam Zheng 28364cc70e93SFam Zheng return -ENOTSUP; 28374cc70e93SFam Zheng } 28384cc70e93SFam Zheng 283941c695c7SKevin Wolf int bdrv_debug_resume(BlockDriverState *bs, const char *tag) 284041c695c7SKevin Wolf { 2841938789eaSMax Reitz while (bs && (!bs->drv || !bs->drv->bdrv_debug_resume)) { 284241c695c7SKevin Wolf bs = bs->file; 284341c695c7SKevin Wolf } 284441c695c7SKevin Wolf 284541c695c7SKevin Wolf if (bs && bs->drv && bs->drv->bdrv_debug_resume) { 284641c695c7SKevin Wolf return bs->drv->bdrv_debug_resume(bs, tag); 284741c695c7SKevin Wolf } 284841c695c7SKevin Wolf 284941c695c7SKevin Wolf return -ENOTSUP; 285041c695c7SKevin Wolf } 285141c695c7SKevin Wolf 285241c695c7SKevin Wolf bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag) 285341c695c7SKevin Wolf { 285441c695c7SKevin Wolf while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) { 285541c695c7SKevin Wolf bs = bs->file; 285641c695c7SKevin Wolf } 285741c695c7SKevin Wolf 285841c695c7SKevin Wolf if (bs && bs->drv && bs->drv->bdrv_debug_is_suspended) { 285941c695c7SKevin Wolf return bs->drv->bdrv_debug_is_suspended(bs, tag); 286041c695c7SKevin Wolf } 286141c695c7SKevin Wolf 286241c695c7SKevin Wolf return false; 28638b9b0cc2SKevin Wolf } 28648b9b0cc2SKevin Wolf 2865199630b6SBlue Swirl int bdrv_is_snapshot(BlockDriverState *bs) 2866199630b6SBlue Swirl { 2867199630b6SBlue Swirl return !!(bs->open_flags & BDRV_O_SNAPSHOT); 2868199630b6SBlue Swirl } 2869199630b6SBlue Swirl 2870b1b1d783SJeff Cody /* backing_file can either be relative, or absolute, or a protocol. If it is 2871b1b1d783SJeff Cody * relative, it must be relative to the chain. So, passing in bs->filename 2872b1b1d783SJeff Cody * from a BDS as backing_file should not be done, as that may be relative to 2873b1b1d783SJeff Cody * the CWD rather than the chain. */ 2874e8a6bb9cSMarcelo Tosatti BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs, 2875e8a6bb9cSMarcelo Tosatti const char *backing_file) 2876e8a6bb9cSMarcelo Tosatti { 2877b1b1d783SJeff Cody char *filename_full = NULL; 2878b1b1d783SJeff Cody char *backing_file_full = NULL; 2879b1b1d783SJeff Cody char *filename_tmp = NULL; 2880b1b1d783SJeff Cody int is_protocol = 0; 2881b1b1d783SJeff Cody BlockDriverState *curr_bs = NULL; 2882b1b1d783SJeff Cody BlockDriverState *retval = NULL; 2883b1b1d783SJeff Cody 2884b1b1d783SJeff Cody if (!bs || !bs->drv || !backing_file) { 2885e8a6bb9cSMarcelo Tosatti return NULL; 2886e8a6bb9cSMarcelo Tosatti } 2887e8a6bb9cSMarcelo Tosatti 2888b1b1d783SJeff Cody filename_full = g_malloc(PATH_MAX); 2889b1b1d783SJeff Cody backing_file_full = g_malloc(PATH_MAX); 2890b1b1d783SJeff Cody filename_tmp = g_malloc(PATH_MAX); 2891b1b1d783SJeff Cody 2892b1b1d783SJeff Cody is_protocol = path_has_protocol(backing_file); 2893b1b1d783SJeff Cody 2894b1b1d783SJeff Cody for (curr_bs = bs; curr_bs->backing_hd; curr_bs = curr_bs->backing_hd) { 2895b1b1d783SJeff Cody 2896b1b1d783SJeff Cody /* If either of the filename paths is actually a protocol, then 2897b1b1d783SJeff Cody * compare unmodified paths; otherwise make paths relative */ 2898b1b1d783SJeff Cody if (is_protocol || path_has_protocol(curr_bs->backing_file)) { 2899b1b1d783SJeff Cody if (strcmp(backing_file, curr_bs->backing_file) == 0) { 2900b1b1d783SJeff Cody retval = curr_bs->backing_hd; 2901b1b1d783SJeff Cody break; 2902b1b1d783SJeff Cody } 2903e8a6bb9cSMarcelo Tosatti } else { 2904b1b1d783SJeff Cody /* If not an absolute filename path, make it relative to the current 2905b1b1d783SJeff Cody * image's filename path */ 2906b1b1d783SJeff Cody path_combine(filename_tmp, PATH_MAX, curr_bs->filename, 2907b1b1d783SJeff Cody backing_file); 2908b1b1d783SJeff Cody 2909b1b1d783SJeff Cody /* We are going to compare absolute pathnames */ 2910b1b1d783SJeff Cody if (!realpath(filename_tmp, filename_full)) { 2911b1b1d783SJeff Cody continue; 2912b1b1d783SJeff Cody } 2913b1b1d783SJeff Cody 2914b1b1d783SJeff Cody /* We need to make sure the backing filename we are comparing against 2915b1b1d783SJeff Cody * is relative to the current image filename (or absolute) */ 2916b1b1d783SJeff Cody path_combine(filename_tmp, PATH_MAX, curr_bs->filename, 2917b1b1d783SJeff Cody curr_bs->backing_file); 2918b1b1d783SJeff Cody 2919b1b1d783SJeff Cody if (!realpath(filename_tmp, backing_file_full)) { 2920b1b1d783SJeff Cody continue; 2921b1b1d783SJeff Cody } 2922b1b1d783SJeff Cody 2923b1b1d783SJeff Cody if (strcmp(backing_file_full, filename_full) == 0) { 2924b1b1d783SJeff Cody retval = curr_bs->backing_hd; 2925b1b1d783SJeff Cody break; 2926b1b1d783SJeff Cody } 2927e8a6bb9cSMarcelo Tosatti } 2928e8a6bb9cSMarcelo Tosatti } 2929e8a6bb9cSMarcelo Tosatti 2930b1b1d783SJeff Cody g_free(filename_full); 2931b1b1d783SJeff Cody g_free(backing_file_full); 2932b1b1d783SJeff Cody g_free(filename_tmp); 2933b1b1d783SJeff Cody return retval; 2934e8a6bb9cSMarcelo Tosatti } 2935e8a6bb9cSMarcelo Tosatti 2936f198fd1cSBenoît Canet int bdrv_get_backing_file_depth(BlockDriverState *bs) 2937f198fd1cSBenoît Canet { 2938f198fd1cSBenoît Canet if (!bs->drv) { 2939f198fd1cSBenoît Canet return 0; 2940f198fd1cSBenoît Canet } 2941f198fd1cSBenoît Canet 2942f198fd1cSBenoît Canet if (!bs->backing_hd) { 2943f198fd1cSBenoît Canet return 0; 2944f198fd1cSBenoît Canet } 2945f198fd1cSBenoît Canet 2946f198fd1cSBenoît Canet return 1 + bdrv_get_backing_file_depth(bs->backing_hd); 2947f198fd1cSBenoît Canet } 2948f198fd1cSBenoît Canet 2949ea2384d3Sbellard void bdrv_init(void) 2950ea2384d3Sbellard { 29515efa9d5aSAnthony Liguori module_call_init(MODULE_INIT_BLOCK); 2952ea2384d3Sbellard } 2953ce1a14dcSpbrook 2954eb852011SMarkus Armbruster void bdrv_init_with_whitelist(void) 2955eb852011SMarkus Armbruster { 2956eb852011SMarkus Armbruster use_bdrv_whitelist = 1; 2957eb852011SMarkus Armbruster bdrv_init(); 2958eb852011SMarkus Armbruster } 2959eb852011SMarkus Armbruster 29605a8a30dbSKevin Wolf void bdrv_invalidate_cache(BlockDriverState *bs, Error **errp) 29610f15423cSAnthony Liguori { 29625a8a30dbSKevin Wolf Error *local_err = NULL; 29635a8a30dbSKevin Wolf int ret; 29645a8a30dbSKevin Wolf 29653456a8d1SKevin Wolf if (!bs->drv) { 29663456a8d1SKevin Wolf return; 29670f15423cSAnthony Liguori } 29683456a8d1SKevin Wolf 29697ea2d269SAlexey Kardashevskiy if (!(bs->open_flags & BDRV_O_INCOMING)) { 29707ea2d269SAlexey Kardashevskiy return; 29717ea2d269SAlexey Kardashevskiy } 29727ea2d269SAlexey Kardashevskiy bs->open_flags &= ~BDRV_O_INCOMING; 29737ea2d269SAlexey Kardashevskiy 29743456a8d1SKevin Wolf if (bs->drv->bdrv_invalidate_cache) { 29755a8a30dbSKevin Wolf bs->drv->bdrv_invalidate_cache(bs, &local_err); 29763456a8d1SKevin Wolf } else if (bs->file) { 29775a8a30dbSKevin Wolf bdrv_invalidate_cache(bs->file, &local_err); 29785a8a30dbSKevin Wolf } 29795a8a30dbSKevin Wolf if (local_err) { 29805a8a30dbSKevin Wolf error_propagate(errp, local_err); 29815a8a30dbSKevin Wolf return; 29823456a8d1SKevin Wolf } 29833456a8d1SKevin Wolf 29845a8a30dbSKevin Wolf ret = refresh_total_sectors(bs, bs->total_sectors); 29855a8a30dbSKevin Wolf if (ret < 0) { 29865a8a30dbSKevin Wolf error_setg_errno(errp, -ret, "Could not refresh total sector count"); 29875a8a30dbSKevin Wolf return; 29885a8a30dbSKevin Wolf } 29890f15423cSAnthony Liguori } 29900f15423cSAnthony Liguori 29915a8a30dbSKevin Wolf void bdrv_invalidate_cache_all(Error **errp) 29920f15423cSAnthony Liguori { 29930f15423cSAnthony Liguori BlockDriverState *bs; 29945a8a30dbSKevin Wolf Error *local_err = NULL; 29950f15423cSAnthony Liguori 2996dc364f4cSBenoît Canet QTAILQ_FOREACH(bs, &bdrv_states, device_list) { 2997ed78cda3SStefan Hajnoczi AioContext *aio_context = bdrv_get_aio_context(bs); 2998ed78cda3SStefan Hajnoczi 2999ed78cda3SStefan Hajnoczi aio_context_acquire(aio_context); 30005a8a30dbSKevin Wolf bdrv_invalidate_cache(bs, &local_err); 3001ed78cda3SStefan Hajnoczi aio_context_release(aio_context); 30025a8a30dbSKevin Wolf if (local_err) { 30035a8a30dbSKevin Wolf error_propagate(errp, local_err); 30045a8a30dbSKevin Wolf return; 30055a8a30dbSKevin Wolf } 30060f15423cSAnthony Liguori } 30070f15423cSAnthony Liguori } 30080f15423cSAnthony Liguori 3009f9f05dc5SKevin Wolf /**************************************************************/ 301019cb3738Sbellard /* removable device support */ 301119cb3738Sbellard 301219cb3738Sbellard /** 301319cb3738Sbellard * Return TRUE if the media is present 301419cb3738Sbellard */ 301519cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs) 301619cb3738Sbellard { 301719cb3738Sbellard BlockDriver *drv = bs->drv; 3018a1aff5bfSMarkus Armbruster 301919cb3738Sbellard if (!drv) 302019cb3738Sbellard return 0; 302119cb3738Sbellard if (!drv->bdrv_is_inserted) 3022a1aff5bfSMarkus Armbruster return 1; 3023a1aff5bfSMarkus Armbruster return drv->bdrv_is_inserted(bs); 302419cb3738Sbellard } 302519cb3738Sbellard 302619cb3738Sbellard /** 30278e49ca46SMarkus Armbruster * Return whether the media changed since the last call to this 30288e49ca46SMarkus Armbruster * function, or -ENOTSUP if we don't know. Most drivers don't know. 302919cb3738Sbellard */ 303019cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs) 303119cb3738Sbellard { 303219cb3738Sbellard BlockDriver *drv = bs->drv; 303319cb3738Sbellard 30348e49ca46SMarkus Armbruster if (drv && drv->bdrv_media_changed) { 30358e49ca46SMarkus Armbruster return drv->bdrv_media_changed(bs); 30368e49ca46SMarkus Armbruster } 30378e49ca46SMarkus Armbruster return -ENOTSUP; 303819cb3738Sbellard } 303919cb3738Sbellard 304019cb3738Sbellard /** 304119cb3738Sbellard * If eject_flag is TRUE, eject the media. Otherwise, close the tray 304219cb3738Sbellard */ 3043f36f3949SLuiz Capitulino void bdrv_eject(BlockDriverState *bs, bool eject_flag) 304419cb3738Sbellard { 304519cb3738Sbellard BlockDriver *drv = bs->drv; 3046bfb197e0SMarkus Armbruster const char *device_name; 304719cb3738Sbellard 3048822e1cd1SMarkus Armbruster if (drv && drv->bdrv_eject) { 3049822e1cd1SMarkus Armbruster drv->bdrv_eject(bs, eject_flag); 305019cb3738Sbellard } 30516f382ed2SLuiz Capitulino 3052bfb197e0SMarkus Armbruster device_name = bdrv_get_device_name(bs); 3053bfb197e0SMarkus Armbruster if (device_name[0] != '\0') { 3054bfb197e0SMarkus Armbruster qapi_event_send_device_tray_moved(device_name, 3055a5ee7bd4SWenchao Xia eject_flag, &error_abort); 30566f382ed2SLuiz Capitulino } 305719cb3738Sbellard } 305819cb3738Sbellard 305919cb3738Sbellard /** 306019cb3738Sbellard * Lock or unlock the media (if it is locked, the user won't be able 306119cb3738Sbellard * to eject it manually). 306219cb3738Sbellard */ 3063025e849aSMarkus Armbruster void bdrv_lock_medium(BlockDriverState *bs, bool locked) 306419cb3738Sbellard { 306519cb3738Sbellard BlockDriver *drv = bs->drv; 306619cb3738Sbellard 3067025e849aSMarkus Armbruster trace_bdrv_lock_medium(bs, locked); 3068b8c6d095SStefan Hajnoczi 3069025e849aSMarkus Armbruster if (drv && drv->bdrv_lock_medium) { 3070025e849aSMarkus Armbruster drv->bdrv_lock_medium(bs, locked); 307119cb3738Sbellard } 307219cb3738Sbellard } 3073985a03b0Sths 30741b7fd729SPaolo Bonzini void bdrv_set_guest_block_size(BlockDriverState *bs, int align) 30757b6f9300SMarkus Armbruster { 30761b7fd729SPaolo Bonzini bs->guest_block_size = align; 30777b6f9300SMarkus Armbruster } 30787cd1e32aSlirans@il.ibm.com 30790db6e54aSFam Zheng BdrvDirtyBitmap *bdrv_find_dirty_bitmap(BlockDriverState *bs, const char *name) 30800db6e54aSFam Zheng { 30810db6e54aSFam Zheng BdrvDirtyBitmap *bm; 30820db6e54aSFam Zheng 30830db6e54aSFam Zheng assert(name); 30840db6e54aSFam Zheng QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) { 30850db6e54aSFam Zheng if (bm->name && !strcmp(name, bm->name)) { 30860db6e54aSFam Zheng return bm; 30870db6e54aSFam Zheng } 30880db6e54aSFam Zheng } 30890db6e54aSFam Zheng return NULL; 30900db6e54aSFam Zheng } 30910db6e54aSFam Zheng 309220dca810SJohn Snow void bdrv_dirty_bitmap_make_anon(BdrvDirtyBitmap *bitmap) 30930db6e54aSFam Zheng { 30949bd2b08fSJohn Snow assert(!bdrv_dirty_bitmap_frozen(bitmap)); 30950db6e54aSFam Zheng g_free(bitmap->name); 30960db6e54aSFam Zheng bitmap->name = NULL; 30970db6e54aSFam Zheng } 30980db6e54aSFam Zheng 30990db6e54aSFam Zheng BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs, 31005fba6c0eSJohn Snow uint32_t granularity, 31010db6e54aSFam Zheng const char *name, 3102b8afb520SFam Zheng Error **errp) 31037cd1e32aSlirans@il.ibm.com { 31047cd1e32aSlirans@il.ibm.com int64_t bitmap_size; 3105e4654d2dSFam Zheng BdrvDirtyBitmap *bitmap; 31065fba6c0eSJohn Snow uint32_t sector_granularity; 3107a55eb92cSJan Kiszka 310850717e94SPaolo Bonzini assert((granularity & (granularity - 1)) == 0); 310950717e94SPaolo Bonzini 31100db6e54aSFam Zheng if (name && bdrv_find_dirty_bitmap(bs, name)) { 31110db6e54aSFam Zheng error_setg(errp, "Bitmap already exists: %s", name); 31120db6e54aSFam Zheng return NULL; 31130db6e54aSFam Zheng } 31145fba6c0eSJohn Snow sector_granularity = granularity >> BDRV_SECTOR_BITS; 31155fba6c0eSJohn Snow assert(sector_granularity); 311657322b78SMarkus Armbruster bitmap_size = bdrv_nb_sectors(bs); 3117b8afb520SFam Zheng if (bitmap_size < 0) { 3118b8afb520SFam Zheng error_setg_errno(errp, -bitmap_size, "could not get length of device"); 3119b8afb520SFam Zheng errno = -bitmap_size; 3120b8afb520SFam Zheng return NULL; 3121b8afb520SFam Zheng } 31225839e53bSMarkus Armbruster bitmap = g_new0(BdrvDirtyBitmap, 1); 31235fba6c0eSJohn Snow bitmap->bitmap = hbitmap_alloc(bitmap_size, ctz32(sector_granularity)); 3124e74e6b78SJohn Snow bitmap->size = bitmap_size; 31250db6e54aSFam Zheng bitmap->name = g_strdup(name); 3126b8e6fb75SJohn Snow bitmap->disabled = false; 3127e4654d2dSFam Zheng QLIST_INSERT_HEAD(&bs->dirty_bitmaps, bitmap, list); 3128e4654d2dSFam Zheng return bitmap; 3129e4654d2dSFam Zheng } 3130e4654d2dSFam Zheng 31319bd2b08fSJohn Snow bool bdrv_dirty_bitmap_frozen(BdrvDirtyBitmap *bitmap) 31329bd2b08fSJohn Snow { 31339bd2b08fSJohn Snow return bitmap->successor; 31349bd2b08fSJohn Snow } 31359bd2b08fSJohn Snow 3136b8e6fb75SJohn Snow bool bdrv_dirty_bitmap_enabled(BdrvDirtyBitmap *bitmap) 3137b8e6fb75SJohn Snow { 31389bd2b08fSJohn Snow return !(bitmap->disabled || bitmap->successor); 31399bd2b08fSJohn Snow } 31409bd2b08fSJohn Snow 31419abe3bdcSJohn Snow DirtyBitmapStatus bdrv_dirty_bitmap_status(BdrvDirtyBitmap *bitmap) 31429abe3bdcSJohn Snow { 31439abe3bdcSJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 31449abe3bdcSJohn Snow return DIRTY_BITMAP_STATUS_FROZEN; 31459abe3bdcSJohn Snow } else if (!bdrv_dirty_bitmap_enabled(bitmap)) { 31469abe3bdcSJohn Snow return DIRTY_BITMAP_STATUS_DISABLED; 31479abe3bdcSJohn Snow } else { 31489abe3bdcSJohn Snow return DIRTY_BITMAP_STATUS_ACTIVE; 31499abe3bdcSJohn Snow } 31509abe3bdcSJohn Snow } 31519abe3bdcSJohn Snow 31529bd2b08fSJohn Snow /** 31539bd2b08fSJohn Snow * Create a successor bitmap destined to replace this bitmap after an operation. 31549bd2b08fSJohn Snow * Requires that the bitmap is not frozen and has no successor. 31559bd2b08fSJohn Snow */ 31569bd2b08fSJohn Snow int bdrv_dirty_bitmap_create_successor(BlockDriverState *bs, 31579bd2b08fSJohn Snow BdrvDirtyBitmap *bitmap, Error **errp) 31589bd2b08fSJohn Snow { 31599bd2b08fSJohn Snow uint64_t granularity; 31609bd2b08fSJohn Snow BdrvDirtyBitmap *child; 31619bd2b08fSJohn Snow 31629bd2b08fSJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 31639bd2b08fSJohn Snow error_setg(errp, "Cannot create a successor for a bitmap that is " 31649bd2b08fSJohn Snow "currently frozen"); 31659bd2b08fSJohn Snow return -1; 31669bd2b08fSJohn Snow } 31679bd2b08fSJohn Snow assert(!bitmap->successor); 31689bd2b08fSJohn Snow 31699bd2b08fSJohn Snow /* Create an anonymous successor */ 31709bd2b08fSJohn Snow granularity = bdrv_dirty_bitmap_granularity(bitmap); 31719bd2b08fSJohn Snow child = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp); 31729bd2b08fSJohn Snow if (!child) { 31739bd2b08fSJohn Snow return -1; 31749bd2b08fSJohn Snow } 31759bd2b08fSJohn Snow 31769bd2b08fSJohn Snow /* Successor will be on or off based on our current state. */ 31779bd2b08fSJohn Snow child->disabled = bitmap->disabled; 31789bd2b08fSJohn Snow 31799bd2b08fSJohn Snow /* Install the successor and freeze the parent */ 31809bd2b08fSJohn Snow bitmap->successor = child; 31819bd2b08fSJohn Snow return 0; 31829bd2b08fSJohn Snow } 31839bd2b08fSJohn Snow 31849bd2b08fSJohn Snow /** 31859bd2b08fSJohn Snow * For a bitmap with a successor, yield our name to the successor, 31869bd2b08fSJohn Snow * delete the old bitmap, and return a handle to the new bitmap. 31879bd2b08fSJohn Snow */ 31889bd2b08fSJohn Snow BdrvDirtyBitmap *bdrv_dirty_bitmap_abdicate(BlockDriverState *bs, 31899bd2b08fSJohn Snow BdrvDirtyBitmap *bitmap, 31909bd2b08fSJohn Snow Error **errp) 31919bd2b08fSJohn Snow { 31929bd2b08fSJohn Snow char *name; 31939bd2b08fSJohn Snow BdrvDirtyBitmap *successor = bitmap->successor; 31949bd2b08fSJohn Snow 31959bd2b08fSJohn Snow if (successor == NULL) { 31969bd2b08fSJohn Snow error_setg(errp, "Cannot relinquish control if " 31979bd2b08fSJohn Snow "there's no successor present"); 31989bd2b08fSJohn Snow return NULL; 31999bd2b08fSJohn Snow } 32009bd2b08fSJohn Snow 32019bd2b08fSJohn Snow name = bitmap->name; 32029bd2b08fSJohn Snow bitmap->name = NULL; 32039bd2b08fSJohn Snow successor->name = name; 32049bd2b08fSJohn Snow bitmap->successor = NULL; 32059bd2b08fSJohn Snow bdrv_release_dirty_bitmap(bs, bitmap); 32069bd2b08fSJohn Snow 32079bd2b08fSJohn Snow return successor; 32089bd2b08fSJohn Snow } 32099bd2b08fSJohn Snow 32109bd2b08fSJohn Snow /** 32119bd2b08fSJohn Snow * In cases of failure where we can no longer safely delete the parent, 32129bd2b08fSJohn Snow * we may wish to re-join the parent and child/successor. 32139bd2b08fSJohn Snow * The merged parent will be un-frozen, but not explicitly re-enabled. 32149bd2b08fSJohn Snow */ 32159bd2b08fSJohn Snow BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BlockDriverState *bs, 32169bd2b08fSJohn Snow BdrvDirtyBitmap *parent, 32179bd2b08fSJohn Snow Error **errp) 32189bd2b08fSJohn Snow { 32199bd2b08fSJohn Snow BdrvDirtyBitmap *successor = parent->successor; 32209bd2b08fSJohn Snow 32219bd2b08fSJohn Snow if (!successor) { 32229bd2b08fSJohn Snow error_setg(errp, "Cannot reclaim a successor when none is present"); 32239bd2b08fSJohn Snow return NULL; 32249bd2b08fSJohn Snow } 32259bd2b08fSJohn Snow 32269bd2b08fSJohn Snow if (!hbitmap_merge(parent->bitmap, successor->bitmap)) { 32279bd2b08fSJohn Snow error_setg(errp, "Merging of parent and successor bitmap failed"); 32289bd2b08fSJohn Snow return NULL; 32299bd2b08fSJohn Snow } 32309bd2b08fSJohn Snow bdrv_release_dirty_bitmap(bs, successor); 32319bd2b08fSJohn Snow parent->successor = NULL; 32329bd2b08fSJohn Snow 32339bd2b08fSJohn Snow return parent; 3234b8e6fb75SJohn Snow } 3235b8e6fb75SJohn Snow 3236ce1ffea8SJohn Snow /** 3237ce1ffea8SJohn Snow * Truncates _all_ bitmaps attached to a BDS. 3238ce1ffea8SJohn Snow */ 3239ce1ffea8SJohn Snow static void bdrv_dirty_bitmap_truncate(BlockDriverState *bs) 3240ce1ffea8SJohn Snow { 3241ce1ffea8SJohn Snow BdrvDirtyBitmap *bitmap; 3242ce1ffea8SJohn Snow uint64_t size = bdrv_nb_sectors(bs); 3243ce1ffea8SJohn Snow 3244ce1ffea8SJohn Snow QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) { 3245ce1ffea8SJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 3246ce1ffea8SJohn Snow continue; 3247ce1ffea8SJohn Snow } 3248ce1ffea8SJohn Snow hbitmap_truncate(bitmap->bitmap, size); 3249ce1ffea8SJohn Snow } 3250ce1ffea8SJohn Snow } 3251ce1ffea8SJohn Snow 3252e4654d2dSFam Zheng void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap) 3253e4654d2dSFam Zheng { 3254e4654d2dSFam Zheng BdrvDirtyBitmap *bm, *next; 3255e4654d2dSFam Zheng QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) { 3256e4654d2dSFam Zheng if (bm == bitmap) { 32579bd2b08fSJohn Snow assert(!bdrv_dirty_bitmap_frozen(bm)); 3258e4654d2dSFam Zheng QLIST_REMOVE(bitmap, list); 3259e4654d2dSFam Zheng hbitmap_free(bitmap->bitmap); 32600db6e54aSFam Zheng g_free(bitmap->name); 3261e4654d2dSFam Zheng g_free(bitmap); 3262e4654d2dSFam Zheng return; 32637cd1e32aSlirans@il.ibm.com } 32647cd1e32aSlirans@il.ibm.com } 32657cd1e32aSlirans@il.ibm.com } 32667cd1e32aSlirans@il.ibm.com 3267b8e6fb75SJohn Snow void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap *bitmap) 3268b8e6fb75SJohn Snow { 32699bd2b08fSJohn Snow assert(!bdrv_dirty_bitmap_frozen(bitmap)); 3270b8e6fb75SJohn Snow bitmap->disabled = true; 3271b8e6fb75SJohn Snow } 3272b8e6fb75SJohn Snow 3273b8e6fb75SJohn Snow void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap *bitmap) 3274b8e6fb75SJohn Snow { 32759bd2b08fSJohn Snow assert(!bdrv_dirty_bitmap_frozen(bitmap)); 3276b8e6fb75SJohn Snow bitmap->disabled = false; 3277b8e6fb75SJohn Snow } 3278b8e6fb75SJohn Snow 327921b56835SFam Zheng BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs) 328021b56835SFam Zheng { 328121b56835SFam Zheng BdrvDirtyBitmap *bm; 328221b56835SFam Zheng BlockDirtyInfoList *list = NULL; 328321b56835SFam Zheng BlockDirtyInfoList **plist = &list; 328421b56835SFam Zheng 328521b56835SFam Zheng QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) { 32865839e53bSMarkus Armbruster BlockDirtyInfo *info = g_new0(BlockDirtyInfo, 1); 32875839e53bSMarkus Armbruster BlockDirtyInfoList *entry = g_new0(BlockDirtyInfoList, 1); 328820dca810SJohn Snow info->count = bdrv_get_dirty_count(bm); 3289592fdd02SJohn Snow info->granularity = bdrv_dirty_bitmap_granularity(bm); 32900db6e54aSFam Zheng info->has_name = !!bm->name; 32910db6e54aSFam Zheng info->name = g_strdup(bm->name); 32929abe3bdcSJohn Snow info->status = bdrv_dirty_bitmap_status(bm); 329321b56835SFam Zheng entry->value = info; 329421b56835SFam Zheng *plist = entry; 329521b56835SFam Zheng plist = &entry->next; 329621b56835SFam Zheng } 329721b56835SFam Zheng 329821b56835SFam Zheng return list; 329921b56835SFam Zheng } 330021b56835SFam Zheng 3301e4654d2dSFam Zheng int bdrv_get_dirty(BlockDriverState *bs, BdrvDirtyBitmap *bitmap, int64_t sector) 33027cd1e32aSlirans@il.ibm.com { 3303e4654d2dSFam Zheng if (bitmap) { 3304e4654d2dSFam Zheng return hbitmap_get(bitmap->bitmap, sector); 33057cd1e32aSlirans@il.ibm.com } else { 33067cd1e32aSlirans@il.ibm.com return 0; 33077cd1e32aSlirans@il.ibm.com } 33087cd1e32aSlirans@il.ibm.com } 33097cd1e32aSlirans@il.ibm.com 3310341ebc2fSJohn Snow /** 3311341ebc2fSJohn Snow * Chooses a default granularity based on the existing cluster size, 3312341ebc2fSJohn Snow * but clamped between [4K, 64K]. Defaults to 64K in the case that there 3313341ebc2fSJohn Snow * is no cluster size information available. 3314341ebc2fSJohn Snow */ 3315341ebc2fSJohn Snow uint32_t bdrv_get_default_bitmap_granularity(BlockDriverState *bs) 3316341ebc2fSJohn Snow { 3317341ebc2fSJohn Snow BlockDriverInfo bdi; 3318341ebc2fSJohn Snow uint32_t granularity; 3319341ebc2fSJohn Snow 3320341ebc2fSJohn Snow if (bdrv_get_info(bs, &bdi) >= 0 && bdi.cluster_size > 0) { 3321341ebc2fSJohn Snow granularity = MAX(4096, bdi.cluster_size); 3322341ebc2fSJohn Snow granularity = MIN(65536, granularity); 3323341ebc2fSJohn Snow } else { 3324341ebc2fSJohn Snow granularity = 65536; 3325341ebc2fSJohn Snow } 3326341ebc2fSJohn Snow 3327341ebc2fSJohn Snow return granularity; 3328341ebc2fSJohn Snow } 3329341ebc2fSJohn Snow 3330592fdd02SJohn Snow uint32_t bdrv_dirty_bitmap_granularity(BdrvDirtyBitmap *bitmap) 3331592fdd02SJohn Snow { 3332592fdd02SJohn Snow return BDRV_SECTOR_SIZE << hbitmap_granularity(bitmap->bitmap); 3333592fdd02SJohn Snow } 3334592fdd02SJohn Snow 333520dca810SJohn Snow void bdrv_dirty_iter_init(BdrvDirtyBitmap *bitmap, HBitmapIter *hbi) 33361755da16SPaolo Bonzini { 3337e4654d2dSFam Zheng hbitmap_iter_init(hbi, bitmap->bitmap, 0); 33381755da16SPaolo Bonzini } 33391755da16SPaolo Bonzini 334020dca810SJohn Snow void bdrv_set_dirty_bitmap(BdrvDirtyBitmap *bitmap, 3341c4237dfaSVladimir Sementsov-Ogievskiy int64_t cur_sector, int nr_sectors) 3342c4237dfaSVladimir Sementsov-Ogievskiy { 3343b8e6fb75SJohn Snow assert(bdrv_dirty_bitmap_enabled(bitmap)); 3344c4237dfaSVladimir Sementsov-Ogievskiy hbitmap_set(bitmap->bitmap, cur_sector, nr_sectors); 3345c4237dfaSVladimir Sementsov-Ogievskiy } 3346c4237dfaSVladimir Sementsov-Ogievskiy 334720dca810SJohn Snow void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap *bitmap, 3348c4237dfaSVladimir Sementsov-Ogievskiy int64_t cur_sector, int nr_sectors) 3349c4237dfaSVladimir Sementsov-Ogievskiy { 3350b8e6fb75SJohn Snow assert(bdrv_dirty_bitmap_enabled(bitmap)); 3351c4237dfaSVladimir Sementsov-Ogievskiy hbitmap_reset(bitmap->bitmap, cur_sector, nr_sectors); 3352c4237dfaSVladimir Sementsov-Ogievskiy } 3353c4237dfaSVladimir Sementsov-Ogievskiy 3354e74e6b78SJohn Snow void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap) 3355e74e6b78SJohn Snow { 3356e74e6b78SJohn Snow assert(bdrv_dirty_bitmap_enabled(bitmap)); 3357e74e6b78SJohn Snow hbitmap_reset(bitmap->bitmap, 0, bitmap->size); 3358e74e6b78SJohn Snow } 3359e74e6b78SJohn Snow 3360e0c47b6cSStefan Hajnoczi void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector, 33611755da16SPaolo Bonzini int nr_sectors) 33621755da16SPaolo Bonzini { 3363e4654d2dSFam Zheng BdrvDirtyBitmap *bitmap; 3364e4654d2dSFam Zheng QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) { 3365b8e6fb75SJohn Snow if (!bdrv_dirty_bitmap_enabled(bitmap)) { 3366b8e6fb75SJohn Snow continue; 3367b8e6fb75SJohn Snow } 3368e4654d2dSFam Zheng hbitmap_set(bitmap->bitmap, cur_sector, nr_sectors); 3369e4654d2dSFam Zheng } 33701755da16SPaolo Bonzini } 33711755da16SPaolo Bonzini 3372e0c47b6cSStefan Hajnoczi void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector, 3373c4237dfaSVladimir Sementsov-Ogievskiy int nr_sectors) 33747cd1e32aSlirans@il.ibm.com { 3375e4654d2dSFam Zheng BdrvDirtyBitmap *bitmap; 3376e4654d2dSFam Zheng QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) { 3377b8e6fb75SJohn Snow if (!bdrv_dirty_bitmap_enabled(bitmap)) { 3378b8e6fb75SJohn Snow continue; 3379b8e6fb75SJohn Snow } 3380e4654d2dSFam Zheng hbitmap_reset(bitmap->bitmap, cur_sector, nr_sectors); 3381e4654d2dSFam Zheng } 33827cd1e32aSlirans@il.ibm.com } 3383aaa0eb75SLiran Schour 3384d58d8453SJohn Snow /** 3385d58d8453SJohn Snow * Advance an HBitmapIter to an arbitrary offset. 3386d58d8453SJohn Snow */ 3387d58d8453SJohn Snow void bdrv_set_dirty_iter(HBitmapIter *hbi, int64_t offset) 3388d58d8453SJohn Snow { 3389d58d8453SJohn Snow assert(hbi->hb); 3390d58d8453SJohn Snow hbitmap_iter_init(hbi, hbi->hb, offset); 3391d58d8453SJohn Snow } 3392d58d8453SJohn Snow 339320dca810SJohn Snow int64_t bdrv_get_dirty_count(BdrvDirtyBitmap *bitmap) 3394aaa0eb75SLiran Schour { 3395e4654d2dSFam Zheng return hbitmap_count(bitmap->bitmap); 3396aaa0eb75SLiran Schour } 3397f88e1a42SJes Sorensen 33989fcb0251SFam Zheng /* Get a reference to bs */ 33999fcb0251SFam Zheng void bdrv_ref(BlockDriverState *bs) 34009fcb0251SFam Zheng { 34019fcb0251SFam Zheng bs->refcnt++; 34029fcb0251SFam Zheng } 34039fcb0251SFam Zheng 34049fcb0251SFam Zheng /* Release a previously grabbed reference to bs. 34059fcb0251SFam Zheng * If after releasing, reference count is zero, the BlockDriverState is 34069fcb0251SFam Zheng * deleted. */ 34079fcb0251SFam Zheng void bdrv_unref(BlockDriverState *bs) 34089fcb0251SFam Zheng { 34099a4d5ca6SJeff Cody if (!bs) { 34109a4d5ca6SJeff Cody return; 34119a4d5ca6SJeff Cody } 34129fcb0251SFam Zheng assert(bs->refcnt > 0); 34139fcb0251SFam Zheng if (--bs->refcnt == 0) { 34149fcb0251SFam Zheng bdrv_delete(bs); 34159fcb0251SFam Zheng } 34169fcb0251SFam Zheng } 34179fcb0251SFam Zheng 3418fbe40ff7SFam Zheng struct BdrvOpBlocker { 3419fbe40ff7SFam Zheng Error *reason; 3420fbe40ff7SFam Zheng QLIST_ENTRY(BdrvOpBlocker) list; 3421fbe40ff7SFam Zheng }; 3422fbe40ff7SFam Zheng 3423fbe40ff7SFam Zheng bool bdrv_op_is_blocked(BlockDriverState *bs, BlockOpType op, Error **errp) 3424fbe40ff7SFam Zheng { 3425fbe40ff7SFam Zheng BdrvOpBlocker *blocker; 3426fbe40ff7SFam Zheng assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX); 3427fbe40ff7SFam Zheng if (!QLIST_EMPTY(&bs->op_blockers[op])) { 3428fbe40ff7SFam Zheng blocker = QLIST_FIRST(&bs->op_blockers[op]); 3429fbe40ff7SFam Zheng if (errp) { 343081e5f78aSAlberto Garcia error_setg(errp, "Node '%s' is busy: %s", 343181e5f78aSAlberto Garcia bdrv_get_device_or_node_name(bs), 3432bfb197e0SMarkus Armbruster error_get_pretty(blocker->reason)); 3433fbe40ff7SFam Zheng } 3434fbe40ff7SFam Zheng return true; 3435fbe40ff7SFam Zheng } 3436fbe40ff7SFam Zheng return false; 3437fbe40ff7SFam Zheng } 3438fbe40ff7SFam Zheng 3439fbe40ff7SFam Zheng void bdrv_op_block(BlockDriverState *bs, BlockOpType op, Error *reason) 3440fbe40ff7SFam Zheng { 3441fbe40ff7SFam Zheng BdrvOpBlocker *blocker; 3442fbe40ff7SFam Zheng assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX); 3443fbe40ff7SFam Zheng 34445839e53bSMarkus Armbruster blocker = g_new0(BdrvOpBlocker, 1); 3445fbe40ff7SFam Zheng blocker->reason = reason; 3446fbe40ff7SFam Zheng QLIST_INSERT_HEAD(&bs->op_blockers[op], blocker, list); 3447fbe40ff7SFam Zheng } 3448fbe40ff7SFam Zheng 3449fbe40ff7SFam Zheng void bdrv_op_unblock(BlockDriverState *bs, BlockOpType op, Error *reason) 3450fbe40ff7SFam Zheng { 3451fbe40ff7SFam Zheng BdrvOpBlocker *blocker, *next; 3452fbe40ff7SFam Zheng assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX); 3453fbe40ff7SFam Zheng QLIST_FOREACH_SAFE(blocker, &bs->op_blockers[op], list, next) { 3454fbe40ff7SFam Zheng if (blocker->reason == reason) { 3455fbe40ff7SFam Zheng QLIST_REMOVE(blocker, list); 3456fbe40ff7SFam Zheng g_free(blocker); 3457fbe40ff7SFam Zheng } 3458fbe40ff7SFam Zheng } 3459fbe40ff7SFam Zheng } 3460fbe40ff7SFam Zheng 3461fbe40ff7SFam Zheng void bdrv_op_block_all(BlockDriverState *bs, Error *reason) 3462fbe40ff7SFam Zheng { 3463fbe40ff7SFam Zheng int i; 3464fbe40ff7SFam Zheng for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) { 3465fbe40ff7SFam Zheng bdrv_op_block(bs, i, reason); 3466fbe40ff7SFam Zheng } 3467fbe40ff7SFam Zheng } 3468fbe40ff7SFam Zheng 3469fbe40ff7SFam Zheng void bdrv_op_unblock_all(BlockDriverState *bs, Error *reason) 3470fbe40ff7SFam Zheng { 3471fbe40ff7SFam Zheng int i; 3472fbe40ff7SFam Zheng for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) { 3473fbe40ff7SFam Zheng bdrv_op_unblock(bs, i, reason); 3474fbe40ff7SFam Zheng } 3475fbe40ff7SFam Zheng } 3476fbe40ff7SFam Zheng 3477fbe40ff7SFam Zheng bool bdrv_op_blocker_is_empty(BlockDriverState *bs) 3478fbe40ff7SFam Zheng { 3479fbe40ff7SFam Zheng int i; 3480fbe40ff7SFam Zheng 3481fbe40ff7SFam Zheng for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) { 3482fbe40ff7SFam Zheng if (!QLIST_EMPTY(&bs->op_blockers[i])) { 3483fbe40ff7SFam Zheng return false; 3484fbe40ff7SFam Zheng } 3485fbe40ff7SFam Zheng } 3486fbe40ff7SFam Zheng return true; 3487fbe40ff7SFam Zheng } 3488fbe40ff7SFam Zheng 348928a7282aSLuiz Capitulino void bdrv_iostatus_enable(BlockDriverState *bs) 349028a7282aSLuiz Capitulino { 3491d6bf279eSLuiz Capitulino bs->iostatus_enabled = true; 349258e21ef5SLuiz Capitulino bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; 349328a7282aSLuiz Capitulino } 349428a7282aSLuiz Capitulino 349528a7282aSLuiz Capitulino /* The I/O status is only enabled if the drive explicitly 349628a7282aSLuiz Capitulino * enables it _and_ the VM is configured to stop on errors */ 349728a7282aSLuiz Capitulino bool bdrv_iostatus_is_enabled(const BlockDriverState *bs) 349828a7282aSLuiz Capitulino { 3499d6bf279eSLuiz Capitulino return (bs->iostatus_enabled && 350092aa5c6dSPaolo Bonzini (bs->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC || 350192aa5c6dSPaolo Bonzini bs->on_write_error == BLOCKDEV_ON_ERROR_STOP || 350292aa5c6dSPaolo Bonzini bs->on_read_error == BLOCKDEV_ON_ERROR_STOP)); 350328a7282aSLuiz Capitulino } 350428a7282aSLuiz Capitulino 350528a7282aSLuiz Capitulino void bdrv_iostatus_disable(BlockDriverState *bs) 350628a7282aSLuiz Capitulino { 3507d6bf279eSLuiz Capitulino bs->iostatus_enabled = false; 350828a7282aSLuiz Capitulino } 350928a7282aSLuiz Capitulino 351028a7282aSLuiz Capitulino void bdrv_iostatus_reset(BlockDriverState *bs) 351128a7282aSLuiz Capitulino { 351228a7282aSLuiz Capitulino if (bdrv_iostatus_is_enabled(bs)) { 351358e21ef5SLuiz Capitulino bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; 35143bd293c3SPaolo Bonzini if (bs->job) { 35153bd293c3SPaolo Bonzini block_job_iostatus_reset(bs->job); 35163bd293c3SPaolo Bonzini } 351728a7282aSLuiz Capitulino } 351828a7282aSLuiz Capitulino } 351928a7282aSLuiz Capitulino 352028a7282aSLuiz Capitulino void bdrv_iostatus_set_err(BlockDriverState *bs, int error) 352128a7282aSLuiz Capitulino { 35223e1caa5fSPaolo Bonzini assert(bdrv_iostatus_is_enabled(bs)); 35233e1caa5fSPaolo Bonzini if (bs->iostatus == BLOCK_DEVICE_IO_STATUS_OK) { 352458e21ef5SLuiz Capitulino bs->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE : 352558e21ef5SLuiz Capitulino BLOCK_DEVICE_IO_STATUS_FAILED; 352628a7282aSLuiz Capitulino } 352728a7282aSLuiz Capitulino } 352828a7282aSLuiz Capitulino 3529d92ada22SLuiz Capitulino void bdrv_img_create(const char *filename, const char *fmt, 3530f88e1a42SJes Sorensen const char *base_filename, const char *base_fmt, 3531f382d43aSMiroslav Rezanina char *options, uint64_t img_size, int flags, 3532f382d43aSMiroslav Rezanina Error **errp, bool quiet) 3533f88e1a42SJes Sorensen { 353483d0521aSChunyan Liu QemuOptsList *create_opts = NULL; 353583d0521aSChunyan Liu QemuOpts *opts = NULL; 353683d0521aSChunyan Liu const char *backing_fmt, *backing_file; 353783d0521aSChunyan Liu int64_t size; 3538f88e1a42SJes Sorensen BlockDriver *drv, *proto_drv; 353996df67d1SStefan Hajnoczi BlockDriver *backing_drv = NULL; 3540cc84d90fSMax Reitz Error *local_err = NULL; 3541f88e1a42SJes Sorensen int ret = 0; 3542f88e1a42SJes Sorensen 3543f88e1a42SJes Sorensen /* Find driver and parse its options */ 3544f88e1a42SJes Sorensen drv = bdrv_find_format(fmt); 3545f88e1a42SJes Sorensen if (!drv) { 354671c79813SLuiz Capitulino error_setg(errp, "Unknown file format '%s'", fmt); 3547d92ada22SLuiz Capitulino return; 3548f88e1a42SJes Sorensen } 3549f88e1a42SJes Sorensen 3550b65a5e12SMax Reitz proto_drv = bdrv_find_protocol(filename, true, errp); 3551f88e1a42SJes Sorensen if (!proto_drv) { 3552d92ada22SLuiz Capitulino return; 3553f88e1a42SJes Sorensen } 3554f88e1a42SJes Sorensen 3555c6149724SMax Reitz if (!drv->create_opts) { 3556c6149724SMax Reitz error_setg(errp, "Format driver '%s' does not support image creation", 3557c6149724SMax Reitz drv->format_name); 3558c6149724SMax Reitz return; 3559c6149724SMax Reitz } 3560c6149724SMax Reitz 3561c6149724SMax Reitz if (!proto_drv->create_opts) { 3562c6149724SMax Reitz error_setg(errp, "Protocol driver '%s' does not support image creation", 3563c6149724SMax Reitz proto_drv->format_name); 3564c6149724SMax Reitz return; 3565c6149724SMax Reitz } 3566c6149724SMax Reitz 3567c282e1fdSChunyan Liu create_opts = qemu_opts_append(create_opts, drv->create_opts); 3568c282e1fdSChunyan Liu create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); 3569f88e1a42SJes Sorensen 3570f88e1a42SJes Sorensen /* Create parameter list with default values */ 357183d0521aSChunyan Liu opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); 357239101f25SMarkus Armbruster qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort); 3573f88e1a42SJes Sorensen 3574f88e1a42SJes Sorensen /* Parse -o options */ 3575f88e1a42SJes Sorensen if (options) { 3576dc523cd3SMarkus Armbruster qemu_opts_do_parse(opts, options, NULL, &local_err); 3577dc523cd3SMarkus Armbruster if (local_err) { 3578dc523cd3SMarkus Armbruster error_report_err(local_err); 3579dc523cd3SMarkus Armbruster local_err = NULL; 358083d0521aSChunyan Liu error_setg(errp, "Invalid options for file format '%s'", fmt); 3581f88e1a42SJes Sorensen goto out; 3582f88e1a42SJes Sorensen } 3583f88e1a42SJes Sorensen } 3584f88e1a42SJes Sorensen 3585f88e1a42SJes Sorensen if (base_filename) { 3586f43e47dbSMarkus Armbruster qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &local_err); 35876be4194bSMarkus Armbruster if (local_err) { 358871c79813SLuiz Capitulino error_setg(errp, "Backing file not supported for file format '%s'", 358971c79813SLuiz Capitulino fmt); 3590f88e1a42SJes Sorensen goto out; 3591f88e1a42SJes Sorensen } 3592f88e1a42SJes Sorensen } 3593f88e1a42SJes Sorensen 3594f88e1a42SJes Sorensen if (base_fmt) { 3595f43e47dbSMarkus Armbruster qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &local_err); 35966be4194bSMarkus Armbruster if (local_err) { 359771c79813SLuiz Capitulino error_setg(errp, "Backing file format not supported for file " 359871c79813SLuiz Capitulino "format '%s'", fmt); 3599f88e1a42SJes Sorensen goto out; 3600f88e1a42SJes Sorensen } 3601f88e1a42SJes Sorensen } 3602f88e1a42SJes Sorensen 360383d0521aSChunyan Liu backing_file = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE); 360483d0521aSChunyan Liu if (backing_file) { 360583d0521aSChunyan Liu if (!strcmp(filename, backing_file)) { 360671c79813SLuiz Capitulino error_setg(errp, "Error: Trying to create an image with the " 360771c79813SLuiz Capitulino "same filename as the backing file"); 3608792da93aSJes Sorensen goto out; 3609792da93aSJes Sorensen } 3610792da93aSJes Sorensen } 3611792da93aSJes Sorensen 361283d0521aSChunyan Liu backing_fmt = qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT); 361383d0521aSChunyan Liu if (backing_fmt) { 361483d0521aSChunyan Liu backing_drv = bdrv_find_format(backing_fmt); 361596df67d1SStefan Hajnoczi if (!backing_drv) { 361671c79813SLuiz Capitulino error_setg(errp, "Unknown backing file format '%s'", 361783d0521aSChunyan Liu backing_fmt); 3618f88e1a42SJes Sorensen goto out; 3619f88e1a42SJes Sorensen } 3620f88e1a42SJes Sorensen } 3621f88e1a42SJes Sorensen 3622f88e1a42SJes Sorensen // The size for the image must always be specified, with one exception: 3623f88e1a42SJes Sorensen // If we are using a backing file, we can obtain the size from there 362483d0521aSChunyan Liu size = qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 0); 362583d0521aSChunyan Liu if (size == -1) { 362683d0521aSChunyan Liu if (backing_file) { 362766f6b814SMax Reitz BlockDriverState *bs; 362829168018SMax Reitz char *full_backing = g_new0(char, PATH_MAX); 362952bf1e72SMarkus Armbruster int64_t size; 363063090dacSPaolo Bonzini int back_flags; 363163090dacSPaolo Bonzini 363229168018SMax Reitz bdrv_get_full_backing_filename_from_filename(filename, backing_file, 363329168018SMax Reitz full_backing, PATH_MAX, 363429168018SMax Reitz &local_err); 363529168018SMax Reitz if (local_err) { 363629168018SMax Reitz g_free(full_backing); 363729168018SMax Reitz goto out; 363829168018SMax Reitz } 363929168018SMax Reitz 364063090dacSPaolo Bonzini /* backing files always opened read-only */ 364163090dacSPaolo Bonzini back_flags = 364263090dacSPaolo Bonzini flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 3643f88e1a42SJes Sorensen 3644f67503e5SMax Reitz bs = NULL; 364529168018SMax Reitz ret = bdrv_open(&bs, full_backing, NULL, NULL, back_flags, 3646cc84d90fSMax Reitz backing_drv, &local_err); 364729168018SMax Reitz g_free(full_backing); 3648f88e1a42SJes Sorensen if (ret < 0) { 3649f88e1a42SJes Sorensen goto out; 3650f88e1a42SJes Sorensen } 365152bf1e72SMarkus Armbruster size = bdrv_getlength(bs); 365252bf1e72SMarkus Armbruster if (size < 0) { 365352bf1e72SMarkus Armbruster error_setg_errno(errp, -size, "Could not get size of '%s'", 365452bf1e72SMarkus Armbruster backing_file); 365552bf1e72SMarkus Armbruster bdrv_unref(bs); 365652bf1e72SMarkus Armbruster goto out; 365752bf1e72SMarkus Armbruster } 3658f88e1a42SJes Sorensen 365939101f25SMarkus Armbruster qemu_opt_set_number(opts, BLOCK_OPT_SIZE, size, &error_abort); 366066f6b814SMax Reitz 366166f6b814SMax Reitz bdrv_unref(bs); 3662f88e1a42SJes Sorensen } else { 366371c79813SLuiz Capitulino error_setg(errp, "Image creation needs a size parameter"); 3664f88e1a42SJes Sorensen goto out; 3665f88e1a42SJes Sorensen } 3666f88e1a42SJes Sorensen } 3667f88e1a42SJes Sorensen 3668f382d43aSMiroslav Rezanina if (!quiet) { 3669f88e1a42SJes Sorensen printf("Formatting '%s', fmt=%s", filename, fmt); 367043c5d8f8SFam Zheng qemu_opts_print(opts, " "); 3671f88e1a42SJes Sorensen puts(""); 3672f382d43aSMiroslav Rezanina } 367383d0521aSChunyan Liu 3674c282e1fdSChunyan Liu ret = bdrv_create(drv, filename, opts, &local_err); 367583d0521aSChunyan Liu 3676cc84d90fSMax Reitz if (ret == -EFBIG) { 3677cc84d90fSMax Reitz /* This is generally a better message than whatever the driver would 3678cc84d90fSMax Reitz * deliver (especially because of the cluster_size_hint), since that 3679cc84d90fSMax Reitz * is most probably not much different from "image too large". */ 3680f3f4d2c0SKevin Wolf const char *cluster_size_hint = ""; 368183d0521aSChunyan Liu if (qemu_opt_get_size(opts, BLOCK_OPT_CLUSTER_SIZE, 0)) { 3682f3f4d2c0SKevin Wolf cluster_size_hint = " (try using a larger cluster size)"; 3683f3f4d2c0SKevin Wolf } 3684cc84d90fSMax Reitz error_setg(errp, "The image size is too large for file format '%s'" 3685cc84d90fSMax Reitz "%s", fmt, cluster_size_hint); 3686cc84d90fSMax Reitz error_free(local_err); 3687cc84d90fSMax Reitz local_err = NULL; 3688f88e1a42SJes Sorensen } 3689f88e1a42SJes Sorensen 3690f88e1a42SJes Sorensen out: 369183d0521aSChunyan Liu qemu_opts_del(opts); 369283d0521aSChunyan Liu qemu_opts_free(create_opts); 369384d18f06SMarkus Armbruster if (local_err) { 3694cc84d90fSMax Reitz error_propagate(errp, local_err); 3695cc84d90fSMax Reitz } 3696f88e1a42SJes Sorensen } 369785d126f3SStefan Hajnoczi 369885d126f3SStefan Hajnoczi AioContext *bdrv_get_aio_context(BlockDriverState *bs) 369985d126f3SStefan Hajnoczi { 3700dcd04228SStefan Hajnoczi return bs->aio_context; 3701dcd04228SStefan Hajnoczi } 3702dcd04228SStefan Hajnoczi 3703dcd04228SStefan Hajnoczi void bdrv_detach_aio_context(BlockDriverState *bs) 3704dcd04228SStefan Hajnoczi { 370533384421SMax Reitz BdrvAioNotifier *baf; 370633384421SMax Reitz 3707dcd04228SStefan Hajnoczi if (!bs->drv) { 3708dcd04228SStefan Hajnoczi return; 3709dcd04228SStefan Hajnoczi } 3710dcd04228SStefan Hajnoczi 371133384421SMax Reitz QLIST_FOREACH(baf, &bs->aio_notifiers, list) { 371233384421SMax Reitz baf->detach_aio_context(baf->opaque); 371333384421SMax Reitz } 371433384421SMax Reitz 371513af91ebSStefan Hajnoczi if (bs->io_limits_enabled) { 37160e5b0a2dSBenoît Canet throttle_timers_detach_aio_context(&bs->throttle_timers); 371713af91ebSStefan Hajnoczi } 3718dcd04228SStefan Hajnoczi if (bs->drv->bdrv_detach_aio_context) { 3719dcd04228SStefan Hajnoczi bs->drv->bdrv_detach_aio_context(bs); 3720dcd04228SStefan Hajnoczi } 3721dcd04228SStefan Hajnoczi if (bs->file) { 3722dcd04228SStefan Hajnoczi bdrv_detach_aio_context(bs->file); 3723dcd04228SStefan Hajnoczi } 3724dcd04228SStefan Hajnoczi if (bs->backing_hd) { 3725dcd04228SStefan Hajnoczi bdrv_detach_aio_context(bs->backing_hd); 3726dcd04228SStefan Hajnoczi } 3727dcd04228SStefan Hajnoczi 3728dcd04228SStefan Hajnoczi bs->aio_context = NULL; 3729dcd04228SStefan Hajnoczi } 3730dcd04228SStefan Hajnoczi 3731dcd04228SStefan Hajnoczi void bdrv_attach_aio_context(BlockDriverState *bs, 3732dcd04228SStefan Hajnoczi AioContext *new_context) 3733dcd04228SStefan Hajnoczi { 373433384421SMax Reitz BdrvAioNotifier *ban; 373533384421SMax Reitz 3736dcd04228SStefan Hajnoczi if (!bs->drv) { 3737dcd04228SStefan Hajnoczi return; 3738dcd04228SStefan Hajnoczi } 3739dcd04228SStefan Hajnoczi 3740dcd04228SStefan Hajnoczi bs->aio_context = new_context; 3741dcd04228SStefan Hajnoczi 3742dcd04228SStefan Hajnoczi if (bs->backing_hd) { 3743dcd04228SStefan Hajnoczi bdrv_attach_aio_context(bs->backing_hd, new_context); 3744dcd04228SStefan Hajnoczi } 3745dcd04228SStefan Hajnoczi if (bs->file) { 3746dcd04228SStefan Hajnoczi bdrv_attach_aio_context(bs->file, new_context); 3747dcd04228SStefan Hajnoczi } 3748dcd04228SStefan Hajnoczi if (bs->drv->bdrv_attach_aio_context) { 3749dcd04228SStefan Hajnoczi bs->drv->bdrv_attach_aio_context(bs, new_context); 3750dcd04228SStefan Hajnoczi } 375113af91ebSStefan Hajnoczi if (bs->io_limits_enabled) { 37520e5b0a2dSBenoît Canet throttle_timers_attach_aio_context(&bs->throttle_timers, new_context); 375313af91ebSStefan Hajnoczi } 375433384421SMax Reitz 375533384421SMax Reitz QLIST_FOREACH(ban, &bs->aio_notifiers, list) { 375633384421SMax Reitz ban->attached_aio_context(new_context, ban->opaque); 375733384421SMax Reitz } 3758dcd04228SStefan Hajnoczi } 3759dcd04228SStefan Hajnoczi 3760dcd04228SStefan Hajnoczi void bdrv_set_aio_context(BlockDriverState *bs, AioContext *new_context) 3761dcd04228SStefan Hajnoczi { 3762dcd04228SStefan Hajnoczi bdrv_drain_all(); /* ensure there are no in-flight requests */ 3763dcd04228SStefan Hajnoczi 3764dcd04228SStefan Hajnoczi bdrv_detach_aio_context(bs); 3765dcd04228SStefan Hajnoczi 3766dcd04228SStefan Hajnoczi /* This function executes in the old AioContext so acquire the new one in 3767dcd04228SStefan Hajnoczi * case it runs in a different thread. 3768dcd04228SStefan Hajnoczi */ 3769dcd04228SStefan Hajnoczi aio_context_acquire(new_context); 3770dcd04228SStefan Hajnoczi bdrv_attach_aio_context(bs, new_context); 3771dcd04228SStefan Hajnoczi aio_context_release(new_context); 377285d126f3SStefan Hajnoczi } 3773d616b224SStefan Hajnoczi 377433384421SMax Reitz void bdrv_add_aio_context_notifier(BlockDriverState *bs, 377533384421SMax Reitz void (*attached_aio_context)(AioContext *new_context, void *opaque), 377633384421SMax Reitz void (*detach_aio_context)(void *opaque), void *opaque) 377733384421SMax Reitz { 377833384421SMax Reitz BdrvAioNotifier *ban = g_new(BdrvAioNotifier, 1); 377933384421SMax Reitz *ban = (BdrvAioNotifier){ 378033384421SMax Reitz .attached_aio_context = attached_aio_context, 378133384421SMax Reitz .detach_aio_context = detach_aio_context, 378233384421SMax Reitz .opaque = opaque 378333384421SMax Reitz }; 378433384421SMax Reitz 378533384421SMax Reitz QLIST_INSERT_HEAD(&bs->aio_notifiers, ban, list); 378633384421SMax Reitz } 378733384421SMax Reitz 378833384421SMax Reitz void bdrv_remove_aio_context_notifier(BlockDriverState *bs, 378933384421SMax Reitz void (*attached_aio_context)(AioContext *, 379033384421SMax Reitz void *), 379133384421SMax Reitz void (*detach_aio_context)(void *), 379233384421SMax Reitz void *opaque) 379333384421SMax Reitz { 379433384421SMax Reitz BdrvAioNotifier *ban, *ban_next; 379533384421SMax Reitz 379633384421SMax Reitz QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) { 379733384421SMax Reitz if (ban->attached_aio_context == attached_aio_context && 379833384421SMax Reitz ban->detach_aio_context == detach_aio_context && 379933384421SMax Reitz ban->opaque == opaque) 380033384421SMax Reitz { 380133384421SMax Reitz QLIST_REMOVE(ban, list); 380233384421SMax Reitz g_free(ban); 380333384421SMax Reitz 380433384421SMax Reitz return; 380533384421SMax Reitz } 380633384421SMax Reitz } 380733384421SMax Reitz 380833384421SMax Reitz abort(); 380933384421SMax Reitz } 381033384421SMax Reitz 381177485434SMax Reitz int bdrv_amend_options(BlockDriverState *bs, QemuOpts *opts, 381277485434SMax Reitz BlockDriverAmendStatusCB *status_cb) 38136f176b48SMax Reitz { 3814c282e1fdSChunyan Liu if (!bs->drv->bdrv_amend_options) { 38156f176b48SMax Reitz return -ENOTSUP; 38166f176b48SMax Reitz } 381777485434SMax Reitz return bs->drv->bdrv_amend_options(bs, opts, status_cb); 38186f176b48SMax Reitz } 3819f6186f49SBenoît Canet 3820b5042a36SBenoît Canet /* This function will be called by the bdrv_recurse_is_first_non_filter method 3821b5042a36SBenoît Canet * of block filter and by bdrv_is_first_non_filter. 3822b5042a36SBenoît Canet * It is used to test if the given bs is the candidate or recurse more in the 3823b5042a36SBenoît Canet * node graph. 3824212a5a8fSBenoît Canet */ 3825212a5a8fSBenoît Canet bool bdrv_recurse_is_first_non_filter(BlockDriverState *bs, 3826212a5a8fSBenoît Canet BlockDriverState *candidate) 3827f6186f49SBenoît Canet { 3828b5042a36SBenoît Canet /* return false if basic checks fails */ 3829b5042a36SBenoît Canet if (!bs || !bs->drv) { 3830b5042a36SBenoît Canet return false; 3831b5042a36SBenoît Canet } 3832b5042a36SBenoît Canet 3833b5042a36SBenoît Canet /* the code reached a non block filter driver -> check if the bs is 3834b5042a36SBenoît Canet * the same as the candidate. It's the recursion termination condition. 3835b5042a36SBenoît Canet */ 3836b5042a36SBenoît Canet if (!bs->drv->is_filter) { 3837b5042a36SBenoît Canet return bs == candidate; 3838b5042a36SBenoît Canet } 3839b5042a36SBenoît Canet /* Down this path the driver is a block filter driver */ 3840b5042a36SBenoît Canet 3841b5042a36SBenoît Canet /* If the block filter recursion method is defined use it to recurse down 3842b5042a36SBenoît Canet * the node graph. 3843b5042a36SBenoît Canet */ 3844b5042a36SBenoît Canet if (bs->drv->bdrv_recurse_is_first_non_filter) { 3845212a5a8fSBenoît Canet return bs->drv->bdrv_recurse_is_first_non_filter(bs, candidate); 3846212a5a8fSBenoît Canet } 3847212a5a8fSBenoît Canet 3848b5042a36SBenoît Canet /* the driver is a block filter but don't allow to recurse -> return false 3849b5042a36SBenoît Canet */ 3850b5042a36SBenoît Canet return false; 3851212a5a8fSBenoît Canet } 3852212a5a8fSBenoît Canet 3853212a5a8fSBenoît Canet /* This function checks if the candidate is the first non filter bs down it's 3854212a5a8fSBenoît Canet * bs chain. Since we don't have pointers to parents it explore all bs chains 3855212a5a8fSBenoît Canet * from the top. Some filters can choose not to pass down the recursion. 3856212a5a8fSBenoît Canet */ 3857212a5a8fSBenoît Canet bool bdrv_is_first_non_filter(BlockDriverState *candidate) 3858212a5a8fSBenoît Canet { 3859212a5a8fSBenoît Canet BlockDriverState *bs; 3860212a5a8fSBenoît Canet 3861212a5a8fSBenoît Canet /* walk down the bs forest recursively */ 3862212a5a8fSBenoît Canet QTAILQ_FOREACH(bs, &bdrv_states, device_list) { 3863212a5a8fSBenoît Canet bool perm; 3864212a5a8fSBenoît Canet 3865b5042a36SBenoît Canet /* try to recurse in this top level bs */ 3866e6dc8a1fSKevin Wolf perm = bdrv_recurse_is_first_non_filter(bs, candidate); 3867212a5a8fSBenoît Canet 3868212a5a8fSBenoît Canet /* candidate is the first non filter */ 3869212a5a8fSBenoît Canet if (perm) { 3870212a5a8fSBenoît Canet return true; 3871212a5a8fSBenoît Canet } 3872212a5a8fSBenoît Canet } 3873212a5a8fSBenoît Canet 3874212a5a8fSBenoît Canet return false; 3875f6186f49SBenoît Canet } 387609158f00SBenoît Canet 387709158f00SBenoît Canet BlockDriverState *check_to_replace_node(const char *node_name, Error **errp) 387809158f00SBenoît Canet { 387909158f00SBenoît Canet BlockDriverState *to_replace_bs = bdrv_find_node(node_name); 38805a7e7a0bSStefan Hajnoczi AioContext *aio_context; 38815a7e7a0bSStefan Hajnoczi 388209158f00SBenoît Canet if (!to_replace_bs) { 388309158f00SBenoît Canet error_setg(errp, "Node name '%s' not found", node_name); 388409158f00SBenoît Canet return NULL; 388509158f00SBenoît Canet } 388609158f00SBenoît Canet 38875a7e7a0bSStefan Hajnoczi aio_context = bdrv_get_aio_context(to_replace_bs); 38885a7e7a0bSStefan Hajnoczi aio_context_acquire(aio_context); 38895a7e7a0bSStefan Hajnoczi 389009158f00SBenoît Canet if (bdrv_op_is_blocked(to_replace_bs, BLOCK_OP_TYPE_REPLACE, errp)) { 38915a7e7a0bSStefan Hajnoczi to_replace_bs = NULL; 38925a7e7a0bSStefan Hajnoczi goto out; 389309158f00SBenoît Canet } 389409158f00SBenoît Canet 389509158f00SBenoît Canet /* We don't want arbitrary node of the BDS chain to be replaced only the top 389609158f00SBenoît Canet * most non filter in order to prevent data corruption. 389709158f00SBenoît Canet * Another benefit is that this tests exclude backing files which are 389809158f00SBenoît Canet * blocked by the backing blockers. 389909158f00SBenoît Canet */ 390009158f00SBenoît Canet if (!bdrv_is_first_non_filter(to_replace_bs)) { 390109158f00SBenoît Canet error_setg(errp, "Only top most non filter can be replaced"); 39025a7e7a0bSStefan Hajnoczi to_replace_bs = NULL; 39035a7e7a0bSStefan Hajnoczi goto out; 390409158f00SBenoît Canet } 390509158f00SBenoît Canet 39065a7e7a0bSStefan Hajnoczi out: 39075a7e7a0bSStefan Hajnoczi aio_context_release(aio_context); 390809158f00SBenoît Canet return to_replace_bs; 390909158f00SBenoît Canet } 3910448ad91dSMing Lei 391191af7014SMax Reitz static bool append_open_options(QDict *d, BlockDriverState *bs) 391291af7014SMax Reitz { 391391af7014SMax Reitz const QDictEntry *entry; 391491af7014SMax Reitz bool found_any = false; 391591af7014SMax Reitz 391691af7014SMax Reitz for (entry = qdict_first(bs->options); entry; 391791af7014SMax Reitz entry = qdict_next(bs->options, entry)) 391891af7014SMax Reitz { 391991af7014SMax Reitz /* Only take options for this level and exclude all non-driver-specific 392091af7014SMax Reitz * options */ 392191af7014SMax Reitz if (!strchr(qdict_entry_key(entry), '.') && 392291af7014SMax Reitz strcmp(qdict_entry_key(entry), "node-name")) 392391af7014SMax Reitz { 392491af7014SMax Reitz qobject_incref(qdict_entry_value(entry)); 392591af7014SMax Reitz qdict_put_obj(d, qdict_entry_key(entry), qdict_entry_value(entry)); 392691af7014SMax Reitz found_any = true; 392791af7014SMax Reitz } 392891af7014SMax Reitz } 392991af7014SMax Reitz 393091af7014SMax Reitz return found_any; 393191af7014SMax Reitz } 393291af7014SMax Reitz 393391af7014SMax Reitz /* Updates the following BDS fields: 393491af7014SMax Reitz * - exact_filename: A filename which may be used for opening a block device 393591af7014SMax Reitz * which (mostly) equals the given BDS (even without any 393691af7014SMax Reitz * other options; so reading and writing must return the same 393791af7014SMax Reitz * results, but caching etc. may be different) 393891af7014SMax Reitz * - full_open_options: Options which, when given when opening a block device 393991af7014SMax Reitz * (without a filename), result in a BDS (mostly) 394091af7014SMax Reitz * equalling the given one 394191af7014SMax Reitz * - filename: If exact_filename is set, it is copied here. Otherwise, 394291af7014SMax Reitz * full_open_options is converted to a JSON object, prefixed with 394391af7014SMax Reitz * "json:" (for use through the JSON pseudo protocol) and put here. 394491af7014SMax Reitz */ 394591af7014SMax Reitz void bdrv_refresh_filename(BlockDriverState *bs) 394691af7014SMax Reitz { 394791af7014SMax Reitz BlockDriver *drv = bs->drv; 394891af7014SMax Reitz QDict *opts; 394991af7014SMax Reitz 395091af7014SMax Reitz if (!drv) { 395191af7014SMax Reitz return; 395291af7014SMax Reitz } 395391af7014SMax Reitz 395491af7014SMax Reitz /* This BDS's file name will most probably depend on its file's name, so 395591af7014SMax Reitz * refresh that first */ 395691af7014SMax Reitz if (bs->file) { 395791af7014SMax Reitz bdrv_refresh_filename(bs->file); 395891af7014SMax Reitz } 395991af7014SMax Reitz 396091af7014SMax Reitz if (drv->bdrv_refresh_filename) { 396191af7014SMax Reitz /* Obsolete information is of no use here, so drop the old file name 396291af7014SMax Reitz * information before refreshing it */ 396391af7014SMax Reitz bs->exact_filename[0] = '\0'; 396491af7014SMax Reitz if (bs->full_open_options) { 396591af7014SMax Reitz QDECREF(bs->full_open_options); 396691af7014SMax Reitz bs->full_open_options = NULL; 396791af7014SMax Reitz } 396891af7014SMax Reitz 396991af7014SMax Reitz drv->bdrv_refresh_filename(bs); 397091af7014SMax Reitz } else if (bs->file) { 397191af7014SMax Reitz /* Try to reconstruct valid information from the underlying file */ 397291af7014SMax Reitz bool has_open_options; 397391af7014SMax Reitz 397491af7014SMax Reitz bs->exact_filename[0] = '\0'; 397591af7014SMax Reitz if (bs->full_open_options) { 397691af7014SMax Reitz QDECREF(bs->full_open_options); 397791af7014SMax Reitz bs->full_open_options = NULL; 397891af7014SMax Reitz } 397991af7014SMax Reitz 398091af7014SMax Reitz opts = qdict_new(); 398191af7014SMax Reitz has_open_options = append_open_options(opts, bs); 398291af7014SMax Reitz 398391af7014SMax Reitz /* If no specific options have been given for this BDS, the filename of 398491af7014SMax Reitz * the underlying file should suffice for this one as well */ 398591af7014SMax Reitz if (bs->file->exact_filename[0] && !has_open_options) { 398691af7014SMax Reitz strcpy(bs->exact_filename, bs->file->exact_filename); 398791af7014SMax Reitz } 398891af7014SMax Reitz /* Reconstructing the full options QDict is simple for most format block 398991af7014SMax Reitz * drivers, as long as the full options are known for the underlying 399091af7014SMax Reitz * file BDS. The full options QDict of that file BDS should somehow 399191af7014SMax Reitz * contain a representation of the filename, therefore the following 399291af7014SMax Reitz * suffices without querying the (exact_)filename of this BDS. */ 399391af7014SMax Reitz if (bs->file->full_open_options) { 399491af7014SMax Reitz qdict_put_obj(opts, "driver", 399591af7014SMax Reitz QOBJECT(qstring_from_str(drv->format_name))); 399691af7014SMax Reitz QINCREF(bs->file->full_open_options); 399791af7014SMax Reitz qdict_put_obj(opts, "file", QOBJECT(bs->file->full_open_options)); 399891af7014SMax Reitz 399991af7014SMax Reitz bs->full_open_options = opts; 400091af7014SMax Reitz } else { 400191af7014SMax Reitz QDECREF(opts); 400291af7014SMax Reitz } 400391af7014SMax Reitz } else if (!bs->full_open_options && qdict_size(bs->options)) { 400491af7014SMax Reitz /* There is no underlying file BDS (at least referenced by BDS.file), 400591af7014SMax Reitz * so the full options QDict should be equal to the options given 400691af7014SMax Reitz * specifically for this block device when it was opened (plus the 400791af7014SMax Reitz * driver specification). 400891af7014SMax Reitz * Because those options don't change, there is no need to update 400991af7014SMax Reitz * full_open_options when it's already set. */ 401091af7014SMax Reitz 401191af7014SMax Reitz opts = qdict_new(); 401291af7014SMax Reitz append_open_options(opts, bs); 401391af7014SMax Reitz qdict_put_obj(opts, "driver", 401491af7014SMax Reitz QOBJECT(qstring_from_str(drv->format_name))); 401591af7014SMax Reitz 401691af7014SMax Reitz if (bs->exact_filename[0]) { 401791af7014SMax Reitz /* This may not work for all block protocol drivers (some may 401891af7014SMax Reitz * require this filename to be parsed), but we have to find some 401991af7014SMax Reitz * default solution here, so just include it. If some block driver 402091af7014SMax Reitz * does not support pure options without any filename at all or 402191af7014SMax Reitz * needs some special format of the options QDict, it needs to 402291af7014SMax Reitz * implement the driver-specific bdrv_refresh_filename() function. 402391af7014SMax Reitz */ 402491af7014SMax Reitz qdict_put_obj(opts, "filename", 402591af7014SMax Reitz QOBJECT(qstring_from_str(bs->exact_filename))); 402691af7014SMax Reitz } 402791af7014SMax Reitz 402891af7014SMax Reitz bs->full_open_options = opts; 402991af7014SMax Reitz } 403091af7014SMax Reitz 403191af7014SMax Reitz if (bs->exact_filename[0]) { 403291af7014SMax Reitz pstrcpy(bs->filename, sizeof(bs->filename), bs->exact_filename); 403391af7014SMax Reitz } else if (bs->full_open_options) { 403491af7014SMax Reitz QString *json = qobject_to_json(QOBJECT(bs->full_open_options)); 403591af7014SMax Reitz snprintf(bs->filename, sizeof(bs->filename), "json:%s", 403691af7014SMax Reitz qstring_get_str(json)); 403791af7014SMax Reitz QDECREF(json); 403891af7014SMax Reitz } 403991af7014SMax Reitz } 40405366d0c8SBenoît Canet 40415366d0c8SBenoît Canet /* This accessor function purpose is to allow the device models to access the 40425366d0c8SBenoît Canet * BlockAcctStats structure embedded inside a BlockDriverState without being 40435366d0c8SBenoît Canet * aware of the BlockDriverState structure layout. 40445366d0c8SBenoît Canet * It will go away when the BlockAcctStats structure will be moved inside 40455366d0c8SBenoît Canet * the device models. 40465366d0c8SBenoît Canet */ 40475366d0c8SBenoît Canet BlockAcctStats *bdrv_get_stats(BlockDriverState *bs) 40485366d0c8SBenoît Canet { 40495366d0c8SBenoît Canet return &bs->stats; 40505366d0c8SBenoît Canet } 4051